diff --git a/ancestors/dp_log/538af9a6-139a-11eb-8399-309c234925a0.cpp b/ancestors/dp_log/538af9a6-139a-11eb-8399-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7959a59fcc2e169841a318c0da757f833c4c04bc --- /dev/null +++ b/ancestors/dp_log/538af9a6-139a-11eb-8399-309c234925a0.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ofstream g ("stramosi.out"); + +int const N = 250001 , SZ = 1e6; + +int v [N][18] , p; + +char s [SZ]; + +int getn (){ + + int x = 0; + + while (! isdigit (s [p])){ + + ++ p; + + if (p == SZ) + + fread (s , 1 , SZ , stdin) , p = 0; + + } + + while (isdigit (s [p])){ + + x = x * 10 + (s [p] - '0'); + + ++ p; + + if (p == SZ) + + fread (s , 1 , SZ , stdin) , p = 0; + + } + + return x; + +} + +int main() + +{ + + int n , m , i , j , p , cur , q , x; + + freopen ("stramosi.in" , "r" , stdin); + + scanf ("%d%d" , &n , &m); + + fread (s , 1 , SZ , stdin); + + for(i = 1 ; i <= n ; ++ i) + + v [i][0] = getn (); + + for(i = 1 ; (1 << i) <= n ; ++ i) + + for(j = (1 << i) + 1 ; j <= n ; ++ j) + + v [j][i] = v [v [j][i - 1]][i - 1]; + + while (m --){ + + p = getn () , q = getn (); + + cur = p , x = 0; + + while ((1 << x) < q) + + ++ x; + + for(i = x ; i >= 0 ; -- i) + + if ((1 << i) & q) + + cur = v [cur][i]; + + g << cur << '\n'; + + } + + + + + + return 0; + +} diff --git a/ancestors/dp_log/539c1098-139a-11eb-b4df-309c234925a0.cpp b/ancestors/dp_log/539c1098-139a-11eb-b4df-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c128b31cb5ad7ed84ad4860bb36c8087cf35297 --- /dev/null +++ b/ancestors/dp_log/539c1098-139a-11eb-b4df-309c234925a0.cpp @@ -0,0 +1,103 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + + + +int tt[250001]; + +int d[18][250001]; + + + +int main() + +{ + + int n, m, i, p, log; + + fin>>n>>m; + + + + for(i=1; i<=n; i++) + + { + + fin>>d[0][i]; + + } + + + + log=0; + + while( (1<>nod>>x; + + + + for(p=0; p<=log; p++) + + { + + if( (1< + + +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +int dp[21][250005]; + +int main() { + int n, m; + fin >> n >> m; + for(int i = 1;i <= n; ++i){ + fin >> dp[0][i]; + } + for(int j = 1;(1 << j) <= n; ++j) + for(int i = 1;i <= n; ++i) + dp[j][i] = dp[j-1][dp[j - 1][i]]; + while(m--){ + int q, p; + fin >> q >> p; + int ans = q; + for(int j = 0;j <= 20; ++j){ + if((p & (1 << j)) != 0) + ans = dp[j][ans]; + } + fout << ans << "\n"; + } + return 0; +} diff --git a/ancestors/dp_log/53babe00-139a-11eb-8a6d-309c234925a0.cpp b/ancestors/dp_log/53babe00-139a-11eb-8a6d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62a1e7dec33d3c621fb563d51d27cbd9820e6290 --- /dev/null +++ b/ancestors/dp_log/53babe00-139a-11eb-8a6d-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n,m,i,stramos,t,query,pas,k,tata[250100],d[20][250100]; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=n;i++) + + { + + f>>tata[i]; + + d[0][i]=tata[i]; + + } + + + + for(k=1;(1<>i>>t; + + + + stramos=i; + + pas=0; + + while(t) + + { + + if(t%2==1) + + { + + stramos=d[pas][stramos]; + + } + + pas++; + + t/=2; + + } + + + + g< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, q, logN; + + + +int main() + +{ + + fin >> n >> q; + + + + logN = log2(n); + + + + vector > d(logN + 1, vector(250001)); + + + + for (int i = 1; i <= n; ++i) + + fin >> d[0][i]; + + + + for (int pw = 1; pw <= logN; ++pw) + + for (int i = 1; i <= n; ++i) + + d[pw][i] = d[pw - 1][d[pw - 1][i]]; + + + + for (int i = 0; i < q; i++) + + { + + int nod, x; + + + + fin >> nod >> x; + + + + for (int pw = logN; pw >= 0; --pw) + + if (((1 << pw) & x)) + + nod = d[pw][nod]; + + + + fout << nod << "\n"; + + } + +} diff --git a/ancestors/dp_log/540f8312-139a-11eb-a770-309c234925a0.cpp b/ancestors/dp_log/540f8312-139a-11eb-a770-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..046f545e03316a213ef4b3634797c6323b52b7e2 --- /dev/null +++ b/ancestors/dp_log/540f8312-139a-11eb-a770-309c234925a0.cpp @@ -0,0 +1,107 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int N = 250005, LOG = 18; + + + +int n, q; + +int t[LOG][N]; + + + +void precompute() + +{ + + for (int lvl = 1; (1 << lvl) <= n; lvl++) + + { + + for (int i = 1; i <= n; i++) + + { + + t[lvl][i] = t[lvl - 1][t[lvl - 1][i]]; + + } + + } + +} + + + +int SolveQuery(int membru, int nr) + +{ + + int ans = membru; + + for (int bit = 0; bit < 18; bit++) + + { + + if (nr & (1 << bit)) + + { + + ans = t[bit][ans]; + + } + + } + + return ans; + +} + + + +int main() + +{ + + fin >> n >> q; + + + + for (int i = 1; i <= n; i++) + + { + + fin >> t[0][i]; + + } + + + + precompute(); + + + + while (q--) + + { + + int persona, nr; + + fin >> persona >> nr; + + fout << SolveQuery(persona, nr) << '\n'; + + } + +} diff --git a/ancestors/dp_log/541e03f0-139a-11eb-94e2-309c234925a0.cpp b/ancestors/dp_log/541e03f0-139a-11eb-94e2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4887e46019f11948eac917f6a4a0f37f97bfdc9 --- /dev/null +++ b/ancestors/dp_log/541e03f0-139a-11eb-94e2-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +const int N = 250005; +const int LOG = 18; + +int n, q; +int anc[LOG][N]; + +void precompute() +{ + for(int lvl = 1; (1 << lvl) <= n; lvl++) + for(int i = 1; i <= n; i++) + anc[lvl][i] = anc[lvl-1][anc[lvl-1][i]]; +} + +int solveQuery(int p, int q) +{ + int ans = q; + for(int bit = 0; bit < LOG; bit++) + if(p & (1 << bit)) + ans = anc[bit][ans]; + return ans; +} + +int main() +{ + fin >> n >> q; + for(int i = 1; i <= n; i++) + fin >> anc[0][i]; + + precompute(); + while(q--) + { + int p, q; + fin >> q >> p; + fout << solveQuery(p, q) << '\n'; + } + return 0; +} diff --git a/ancestors/dp_log/542c9c14-139a-11eb-a355-309c234925a0.cpp b/ancestors/dp_log/542c9c14-139a-11eb-a355-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b79ee1d495137c7cdc2978999319be283a2b0b57 --- /dev/null +++ b/ancestors/dp_log/542c9c14-139a-11eb-a355-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int N = 250005; + +const int LOG = 18; + + + +int n, q; + +int anc[LOG][N]; + + + +void precompute() + +{ + + for (int lvl = 1; (1 << lvl) <= n; lvl++) + + for (int i = 1; i <= n; i++) + + anc[lvl][i] = anc[lvl - 1][anc[lvl - 1][i]]; + +} + + + +int solveQuery(int p, int q) + +{ + + int ans = q; + + for (int bit = 0; bit < LOG; bit++) + + if (p & (1 << bit)) + + ans = anc[bit][ans]; + + return ans; + +} + + + +int main() + +{ + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> anc[0][i]; + + + + precompute(); + + while (q--) + + { + + int p, q; + + fin >> q >> p; + + fout << solveQuery(p, q) << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/543c50be-139a-11eb-b5f1-309c234925a0.cpp b/ancestors/dp_log/543c50be-139a-11eb-b5f1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8d380ecc3ccb287f8d2ac87d7be278d48230bd5 --- /dev/null +++ b/ancestors/dp_log/543c50be-139a-11eb-b5f1-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + + + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 250005; + + + +int dad[N][18]; + + + +int main() + +{ + + usain_bolt(); + + + + int n, q; + + + + fin >> n >> q; + + for(int i = 1; i <= n; ++i) { + + fin >> dad[i][0]; + + } + + for(int j = 1; j <= 17; ++j) { + + for(int i = 1; i <= n; ++i) { + + dad[i][j] = dad[dad[i][j - 1]][j - 1]; + + } + + } + + while(q--) { + + int x, y; + + + + fin >> x >> y; + + + + int cnt = 0; + + + + while(y > 0) { + + if(y & 1) { + + x = dad[x][cnt]; + + } + + y >>= 1, ++cnt; + + } + + fout << x << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/547f08e4-139a-11eb-a943-309c234925a0.cpp b/ancestors/dp_log/547f08e4-139a-11eb-a943-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e01e83c334508e024175cc1dcee61bfb1e00e35e --- /dev/null +++ b/ancestors/dp_log/547f08e4-139a-11eb-a943-309c234925a0.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + +const int MAX_N = 250000; + +const int MAX_K = log2(MAX_N) + 1; + + + +int T[MAX_K][MAX_N]; + + + +int N, M; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + + + cin >> N >> M; + + + + for(int i = 1; i <= N; ++i) + + cin >> T[0][i]; + + + + int K = log2(N) + 1; + + + + for(int k = 1; k < K; ++k) + + for(int i = 1; i <= N; ++i) + + T[k][i] = T[k - 1][T[k - 1][i]]; + + + + for(int i = 1; i <= M; ++i) + + { + + int Q, P; + + cin >> Q >> P; + + + + int k = 0; + + + + while(P) + + { + + if(P & 1) Q = T[k][Q]; + + + + ++k; + + P = P >> 1; + + } + + + + cout << Q << '\n'; + + + + } + +} diff --git a/ancestors/dp_log/54926f3a-139a-11eb-bda3-309c234925a0.cpp b/ancestors/dp_log/54926f3a-139a-11eb-bda3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12ab308076da48e91686f6a4b671209d98232d0a --- /dev/null +++ b/ancestors/dp_log/54926f3a-139a-11eb-bda3-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +long long ma[30][250100]; + +int main() + +{ + + long long n,m,p,q; + + int i,j; + + in>>n>>m; + + for(i=1; i<=n; i++) + + { + + in>>ma[0][i]; + + } + + for(i=1; (1<>q>>p; + + int ok=0; + + while(p) + + { + + if(p%2==1) + + { + + q=ma[ok][q]; + + } + + ok++; + + p/=2; + + } + + out<> n >> m; + + for (int i = 1; i <= n; i++) + + fin >> daddy[i]; + +} + + + +void precompute() { + + log2from[1] = 0; + + for (int i = 2; i < NMAX; i++) + + log2from[i] = log2from[i >> 1] + 1; + + pow2[0] = 1; + + for (int i = 1; i <= NMAXLOG2; i++) + + pow2[i] = pow2[i - 1] << 1; + +} + + + +void computeDP() { + + for (int i = 1; i <= n; i++) + + dp[i][0] = daddy[i]; + + for (int i = 1; i <= n; i++) + + for (int j = 1; j <= NMAXLOG2; j++) + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + +} + + + +void query() { + + while (m--) { + + int node, nrancestor; + + fin >> node >> nrancestor; + + while (nrancestor > 0) { + + int p = log2from[nrancestor]; + + node = dp[node][p]; + + nrancestor -= pow2[p]; + + } + + fout << node << '\n'; + + } + +} + + + +int main() { + + precompute(); + + read(); + + computeDP(); + + query(); + + fout.close(); + + return 0; + +} diff --git a/ancestors/dp_log/54b0577e-139a-11eb-b333-309c234925a0.cpp b/ancestors/dp_log/54b0577e-139a-11eb-b333-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2da16923412b2739273d97a01fb8a19ecafacc77 --- /dev/null +++ b/ancestors/dp_log/54b0577e-139a-11eb-b333-309c234925a0.cpp @@ -0,0 +1,57 @@ +#include +#include + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int stramos[20][250001]; + +int main() + +{ + + int n,m,p,q; + + in>>n>>m; + + for(int i=1;i<=n;i++) + + in>>stramos[0][i]; + + for(int i=1;i<=18;i++) + + for(int j=1;j<=n;j++){ + + stramos[i][j]=stramos[i-1][stramos[i-1][j]]; + + } + + for(int i=1;i<=m;i++){ + + in>>q>>p; + + for(int j=0;p>0;j++){ + + if((1< + + +using namespace std; +int father[20 +1][250000 +1]; +void precalc(int n){ + + int i,j; + + for(j=1;j<20;j++){ + + for(i=1;i<=n;i++){ + + father[j][i]=father[j-1][father[j-1][i]]; + + } + + } + +} + + + +int answer(int nod,int x){ + + int j; + + for(j=0;(1< + + +using namespace std; + + + +const int MAX_N = 250000; + +const int MAX_K = log2(MAX_N) + 1; + + + +int T[MAX_K][MAX_N]; + + + +int N, M; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + + + cin >> N >> M; + + + + for(int i = 1; i <= N; ++i) + + cin >> T[0][i]; + + + + for(int k = 1; k < MAX_K; ++k) + + for(int i = 1; i <= N; ++i) + + T[k][i] = T[k - 1][T[k - 1][i]]; + + + + for(int i = 1; i <= M; ++i) + + { + + int Q, P; + + cin >> Q >> P; + + + + int k = 0; + + + + while(P) + + { + + if(P & 1) Q = T[k][Q]; + + + + ++k; + + P = P >> 1; + + } + + + + cout << Q << '\n'; + + + + } + +} diff --git a/ancestors/dp_log/54f11f2e-139a-11eb-84f1-309c234925a0.cpp b/ancestors/dp_log/54f11f2e-139a-11eb-84f1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7812ea39f3d813eddadb6f48988ab5fe438d479b --- /dev/null +++ b/ancestors/dp_log/54f11f2e-139a-11eb-84f1-309c234925a0.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +const int NMAX = 250005; + +const int PMAX = 19; + +int dp[PMAX][NMAX], n, q, x, k, b; + + + +int main() + +{ + + f >> n >> q; + + for(int i = 1; i <= n; i++) { + + f >> dp[0][i]; + + } + + for(int i = 1; (1 << i) <= n; i++) { + + for(int j = 1; j <= n; j++) { + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + } + + } + + while(q--) { + + f >> x >> k; + + b = 0; + + while(k) { + + if(k % 2) { + + x = dp[b][x]; + + } + + b++; + + k /= 2; + + } + + g << x << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5500616e-139a-11eb-835a-309c234925a0.cpp b/ancestors/dp_log/5500616e-139a-11eb-835a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6040d4d9c437fe0baa1293954db3fb553677efc9 --- /dev/null +++ b/ancestors/dp_log/5500616e-139a-11eb-835a-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; + +FILE *fin = fopen( "stramosi.in", "r" ), *fout = fopen( "stramosi.out", "w" ); + +const int BufSz = (1 << 17); +const int Max10 = 10; + +int rpos = BufSz - 1; +char rbuf[BufSz]; +int p10[Max10 + 1] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; +char wbuf[BufSz]; +int wpos = 0; + +static inline char readChar() { + if ( !(rpos = (rpos + 1) & (BufSz - 1)) ) { + fread( rbuf, 1, BufSz, fin ); + } + return rbuf[rpos]; +} +int readInt() { + int ch, res = 0; + + while ( isspace( ch = readChar() ) ); + do { + res = res * 10 + ch - '0'; + } while ( isdigit( ch = readChar() ) ); + return res; +} + +static inline void writeChar( char ch ) { + wbuf[wpos] = ch; + if ( !(wpos = (wpos + 1) & (BufSz - 1)) ) { + fwrite( wbuf, 1, BufSz, fout ); + } +} +static inline void writeInt( int x ) { + int i, cf; + i = Max10; + while ( p10[i] > x ) { + --i; + } + if ( i == 0 ) { + writeChar( '0' ); + } else { + do { + cf = '0'; + while ( p10[i] <= x ) { + x -= p10[i]; + ++cf; + } + writeChar( cf ); + } while ( --i > 0 ); + } + writeChar( '\n' ); +} +static inline void flushBuf() { + fwrite( wbuf, 1, wpos, fout ); +} + +const int MaxN = 250002; +const int MaxLog = 20; + +int anc[MaxN][MaxLog]; +int log[MaxN]; + +int main() { + int n, q, p, i, lg; + + fscanf( fin, "%d%d", &n, &q ); + for ( i = 1; i <= n; ++i ) { + fscanf( fin, "%d", &anc[i][0] ); + } + for ( p = 1; p <= MaxLog - 1; ++p ) { + for ( i = 1; i <= n; ++i ) { + anc[i][p] = anc[anc[i][p - 1]][p - 1]; + } + } + for ( i = 2; i <= n; ++i ) { + log[i] = log[i / 2] + 1; + } + while ( q-- ) { + i = readInt(); + p = readInt(); + lg = log[p]; + while ( p ) { + i = anc[i][lg]; + p -= (1 << lg); + lg = log[p]; + } + writeInt( i ); + } + flushBuf(); + fclose( fin ); + fclose( fout ); + return 0; +} diff --git a/ancestors/dp_log/5510b52e-139a-11eb-85a6-309c234925a0.cpp b/ancestors/dp_log/5510b52e-139a-11eb-85a6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aa6e1808b7362560eeb19ab0b759a74f2f7b11a --- /dev/null +++ b/ancestors/dp_log/5510b52e-139a-11eb-85a6-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; +ifstream fin("stramosi.in");ofstream fout("stramosi.out"); + + + + + + + +long long t, n, m; + +long long a[20][250010]; + + + +int32_t main(){ + +ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +fin>>n>>m; + +for(long long i=1; i<=n; i++){ + + fin>>a[0][i]; + +} + +for(long long j=1; j<=19; j++){ + + for(long long i=1; i<=n; i++){ + + a[j][i]=a[j-1][a[j-1][i]]; + + } + +} + + + +while(m--){ + + long long p, q; + + fin>>q>>p; + + while(p>0){ + + for(long long j=0; j<=19; j++){ + + if(p<(((long long)1)< +using namespace std; +ifstream fin("stramosi.in");ofstream fout("stramosi.out"); + + + + + + + +long long t, n, m; + +long long a[20][250010]; + + + +int32_t main(){ + +ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +fin>>n>>m; + +for(long long i=1; i<=n; i++){ + + fin>>a[0][i]; + +} + +for(long long j=1; j<=19; j++){ + + for(long long i=1; i<=n; i++){ + + a[j][i]=a[j-1][a[j-1][i]]; + + } + +} + + + +while(m--){ + + long long p, q; + + fin>>q>>p; + + while(p>0){ + + for(long long j=0; j<=19; j++){ + + if(p<(((long long)1)< + + + + +using namespace std; + + + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +const int NMAX=250099; + +long long n,m,a[30][NMAX],p,q,k; + + + +int main(){ + +cin >>n>>m; + +for(int i=1;i<=n;i++) cin>>a[0][i]; + + + +for(int i=1;(1<>q>>p; + + int flag=0; + + while(p){ + + if(p %2==1) + + q=a[flag][q]; + + flag++; + + p/=2; + + } + + cout < + + + + +using namespace std; + + + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +const int NMAX=250099; + +long long n,m,a[30][NMAX],p,q,k; + + + +int main(){ + +cin >>n>>m; + +for(int i=1;i<=n;i++) cin>>a[0][i]; + + + +for(int i=1;(1<>q>>p; + + int flag=0; + + while(p){ + + if(p %2==1) + + q=a[flag][q]; + + flag++; + + p/=2; + + } + + cout < + + +using namespace std; + +ifstream cin ("stramosi.in"); + +ofstream cout ("stramosi.out"); + +int dp[20][250000]; + +int main() + +{ + + int n, m, i, j; + + cin >> n >> m; + + for (i = 1; i <= n; i++) + + cin >> dp[0][i]; + + for (i = 1; (1 << i) <= n; i++) + + for (j = 1; j <= n; j++) + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + for (i = 1; i <= m; i++) + + { + + int p, q; + + cin >> q >> p; + + int bit = 0; + + while (p) + + { + + if (p % 2 == 1) + + q = dp[bit][q]; + + bit++; + + p /= 2; + + } + + cout << q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/55896192-139a-11eb-8f27-309c234925a0.cpp b/ancestors/dp_log/55896192-139a-11eb-8f27-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b3d28714c2145335312a4fe10492cb5ad29306b --- /dev/null +++ b/ancestors/dp_log/55896192-139a-11eb-8f27-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +ifstream r("stramosi.in"); + +ofstream w("stramosi.out"); + +int n,m,stramos[20][250002]; + +int main() + +{ + + r>>n>>m; + + for(int i=1; i<=n; i++){ + + r>>stramos[0][i]; + + } + + for(int i=1,p=2; p<=n; i++,p*=2){ + + for(int j=1; j<=n; j++){ + + stramos[i][j]=stramos[i-1][stramos[i-1][j]]; + + } + + } + + while(m--) + + { + + int p,q; + + r>>q>>p; + + while(p!=0) + + { + + int k=__builtin_ctz(p); + + q=stramos[k][q]; + + p-=1< +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); +int n, m; + + +int rmq[18][250010]; +int logaritmi[250010]; + +void init() { + logaritmi[1] = 0; + for(int i=2; i <= n; i++) { + logaritmi[i] = logaritmi[i / 2] + 1; + } + + for(int lungime = 1; (1 << lungime) <= n; lungime++) { + for(int el = 1; el <= n; el++) { + rmq[lungime][el] = rmq[lungime-1][rmq[lungime-1][el]]; + } + } +} + + +void solve() { + fin >> n >> m; + for(int i=1; i <= n; i++) { + fin >> rmq[0][i]; + } + init(); + + while(m--) { + int a, b; + fin >> a >> b; + if(b == 0) { + fout << a << "\n"; + } else { + while(b > 0) { + a = rmq[logaritmi[b & (-b)]][a]; + b -= b & (-b); + } + + fout << a << "\n"; + } + } +} + + +int main() { + ios_base::sync_with_stdio(0); + fin .tie(0); + fout.tie(0); + + int testCases = 1; + + while(testCases--) { + solve(); + } +} diff --git a/ancestors/dp_log/55ab4176-139a-11eb-a30f-309c234925a0.cpp b/ancestors/dp_log/55ab4176-139a-11eb-a30f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..439465fd17fdc109ea514d97854001e14f3f1ca1 --- /dev/null +++ b/ancestors/dp_log/55ab4176-139a-11eb-a30f-309c234925a0.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +int rmq[19][250001]; + + +inline int lsb(int x) +{ + return x&(-x); +} + +inline int log(int x) +{ + int nr = 0; + while (x > 1) + { + x = x>>1; + nr++; + } + return nr; +} + +int main() +{ + int n, i, m, q, p, j; + fin >> n >> m; + for (i = 1; i<=n; i++) + fin >> rmq[0][i]; + for (i = 1; (1<> q >> p; + + if (p == 0) + { + fout << q << '\n'; + continue; + } + while (p > 0) + { + q = rmq[log(lsb(p))][q]; + p = p - lsb(p); + } + fout << q << '\n'; + } + return 0; +} diff --git a/ancestors/dp_log/55e78718-139a-11eb-84d2-309c234925a0.cpp b/ancestors/dp_log/55e78718-139a-11eb-84d2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17896b699bee172346ad2c2661f2d32b61a3f00d --- /dev/null +++ b/ancestors/dp_log/55e78718-139a-11eb-84d2-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +const int N = 19; + +const int M = 250001; + +int n,m,stramos[N][M]; + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=n; i++) + + f>>stramos[0][i]; + + for(int i=1,p=2;p<=n;i++,p*=2) + + for(int j=1;j<=n;j++) + + stramos[i][j]=stramos[i-1][stramos[i-1][j]]; + + for(;m;m--) + + { + + int p,q; + + f>>q>>p; + + while(p) + + { + + int k=__builtin_ctz(p); + + q=stramos[k][q]; + + p-=1< + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +const int N = 19; + +const int M = 250001; + +int n,m,stramos[N][M]; + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=n; i++) + + f>>stramos[0][i]; + + for(int i=1,p=2;p<=n;i++,p*=2) + + for(int j=1;j<=n;j++) + + stramos[i][j]=stramos[i-1][stramos[i-1][j]]; + + for(;m;m--) + + { + + int p,q; + + f>>q>>p; + + while(p) + + { + + int k=__builtin_ctz(p); + + q=stramos[k][q]; + + p-=1< +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + +template +ostream& operator<<(ostream &os, const vector &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } +template +ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } + +const int INF = INT_MAX, maxN = 250010; +int t, n, m, q, p, dp[maxN][21]; + + + +int query(int nod, int k){ + + int bit = 0; + + while (k > 0) { + + if (k & 1) { + + nod = dp[nod][bit]; + + if (!nod) return 0; + + } + + ++bit; + + k >>= 1; + + } + + return nod; + +} + + +int main(){ + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + + cin >> n >> m; + + for (int i = 1; i <= n; i++){ + + cin >> dp[i][0]; + + } + + for (int i = 1; i <= n; i++){ + + for (int j = 1; j <= 20; j++){ + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + } + + } + + + + while (m--){ + + cin >> q >> p; + + cout << query(q, p) << "\n"; + + } + + return 0; +} diff --git a/ancestors/dp_log/562c0a2c-139a-11eb-a615-309c234925a0.cpp b/ancestors/dp_log/562c0a2c-139a-11eb-a615-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d670cc2e3c0aae268a42184319009648863c1f32 --- /dev/null +++ b/ancestors/dp_log/562c0a2c-139a-11eb-a615-309c234925a0.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; + + + +int n, m, dp[19][250010], q, p, r, rez; + + + +int main() + +{ + + ifstream f("stramosi.in"); + + ofstream g("stramosi.out"); + + f>>n>>m; + + int i, j; + + for(i=1; i<=n; i++) + + f>>dp[0][i]; + + for(j=1; j<=18; j++) + + for(i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + for(int i=1; i<=m; i++) + + { + + f>>q>>p; + + r = 0; + + rez = q; + + while(p != 0) + + { + + if(p % 2 != 0) + + rez = dp[r][rez]; + + p = p >> 1; + + r++; + + } + + g< +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + +template +ostream& operator<<(ostream &os, const vector &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } +template +ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } + +const int INF = INT_MAX, maxN = 250010; +int t, n, m, q, p, dp[maxN][21]; + + + +int query(int nod, int k){ + + int bit = 0; + + while (k > 0) { + + if (k & 1) { + + nod = dp[nod][bit]; + + if (!nod) return 0; + + } + + ++bit; + + k >>= 1; + + } + + return nod; + +} + + +int main(){ + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + + cin >> n >> m; + + for (int i = 1; i <= n; i++){ + + cin >> dp[i][0]; + + } + + for (int i = 1; i <= n; i++){ + + for (int j = 1; j <= 20; j++){ + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + } + + } + + + + while (m--){ + + cin >> q >> p; + + cout << query(q, p) << "\n"; + + } + + return 0; +} diff --git a/ancestors/dp_log/564da036-139a-11eb-89a6-309c234925a0.cpp b/ancestors/dp_log/564da036-139a-11eb-89a6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..174ea3b4b88db5beda48dbceda0f06070f0d3f4a --- /dev/null +++ b/ancestors/dp_log/564da036-139a-11eb-89a6-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 250005; + +int t[20][NMAX]; + + + +int main() { + + + + freopen ("stramosi.in", "r", stdin); + + freopen ("stramosi.out", "w", stdout); + + + + int n, teste, i, q, p, j, w; + + + + scanf ("%d%d", &n, &teste); + + for (i = 1; i <= n; i++) + + scanf ("%d", &t[0][i]); + + + + for (i = 1; 1 << i <= n; i++) { + + for (j = 1; j <= n; j++) + + t[i][j] = t[i - 1][t[i - 1][j]]; + + } + + + + for (w = 1; w <= teste; w++) { + + scanf ("%d%d", &q, &p); + + for (i = 0; i <= 17; i++) + + if (((1 << i) & p) != 0) { + + q = t[i][q]; + + } + + printf ("%d\n", q); + + + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/565d7eba-139a-11eb-927a-309c234925a0.cpp b/ancestors/dp_log/565d7eba-139a-11eb-927a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d670cc2e3c0aae268a42184319009648863c1f32 --- /dev/null +++ b/ancestors/dp_log/565d7eba-139a-11eb-927a-309c234925a0.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; + + + +int n, m, dp[19][250010], q, p, r, rez; + + + +int main() + +{ + + ifstream f("stramosi.in"); + + ofstream g("stramosi.out"); + + f>>n>>m; + + int i, j; + + for(i=1; i<=n; i++) + + f>>dp[0][i]; + + for(j=1; j<=18; j++) + + for(i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + for(int i=1; i<=m; i++) + + { + + f>>q>>p; + + r = 0; + + rez = q; + + while(p != 0) + + { + + if(p % 2 != 0) + + rez = dp[r][rez]; + + p = p >> 1; + + r++; + + } + + g< +#include + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, x, y; + +int tt[250005]; + +int dp[20][250005]; + + + +int main() + +{ + + f >> n >> m; + + for (int i = 1; i <= n; i++) + + { + + f >> dp[0][i]; + + } + + + + for (int i = 1; i <= 18; i++) + + { + + for (int j = 1; j <= n; j++) + + dp[i][j]=dp[i-1][dp[i-1][j]]; + + } + + + + while (m--) + + { + + f >> x >> y; + + + + for (int i = 0; i <= 18; i++) + + if (y&(1< + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, i, j, k, s, sol, stramos[20][250005]; + + + +int main() + +{ + + f >> n >> m; + + for(i = 1; i <= n; i++) + + f >> stramos[0][i]; + + for(i = 1; (1 << i) < n; i++) + + for(j = 1; j <= n; j++) + + stramos[i][j] = stramos[i - 1][stramos[i - 1][j]]; + + + + while(m) + + { + + f >> k >> s; + + + + sol = k; + + for(i = 17; i >= 0; i--) + + if((1 << i) <= s) + + { + + sol = stramos[i][sol]; + + s -= (1 << i); + + } + + + + g << sol << '\n'; + + + + m--; + + } + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/ancestors/dp_log/56c84e86-139a-11eb-8425-309c234925a0.cpp b/ancestors/dp_log/56c84e86-139a-11eb-8425-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d883590b20ea4a8586aa7dedd410c94be8f00bbc --- /dev/null +++ b/ancestors/dp_log/56c84e86-139a-11eb-8425-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("stramosi.in"); + +ofstream out ("stramosi.out"); + + + +const int N=250001; + +const int M=350002; + + + +int t[18][N]; + +int main() + +{ + + int n,m,p,q; + + in>>n>>m; + + for (int i=1;i<=n;i++) + + in>>t[0][i]; + + for (int i=1;i<18;i++) + + for (int j=1;j<=n;j++) + + t[i][j]=t[i-1][t[i-1][j]]; + + for (int k=1;k<=m;k++) + + { + + in>>q>>p; + + int ans=q; + + int i=0; + + while (p) + + { + + if (p%2!=0) ans=t[i][ans]; + + p/=2; + + i++; + + } + + out< +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int N = 2500001; + +const int L = 18; + +int n, m, p, q, rez, t[L][N]; + + + +int main() { + + in >> n >> m; + + for (int i = 1; i <= n; i++) + + in >> t[0][i]; + + for (int i = 1; i < L; i++) + + for (int j = 1; j <= n; j++) + + t[i][j] = t[i - 1][t[i - 1][j]]; + + while (m) { + + int i = 0; + + m--; + + in >> q >> p; + + rez = q; + + while (p) { + + if (p % 2 != 0) rez = t[i][rez]; + + p /= 2; + + i++; + + } + + out << rez << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/56e80b76-139a-11eb-b7b3-309c234925a0.cpp b/ancestors/dp_log/56e80b76-139a-11eb-b7b3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b72b9798ee19fcaeebc1dc57d6d2e11c73462e75 --- /dev/null +++ b/ancestors/dp_log/56e80b76-139a-11eb-b7b3-309c234925a0.cpp @@ -0,0 +1,63 @@ +#include +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int N = 2500001; + +const int L = 18; + +int n, m, p, q, rez, t[L][N]; + + + +int main() { + + in >> n >> m; + + for (int i = 1; i <= n; i++) + + in >> t[0][i]; + + for (int i = 1; i < L; i++) + + for (int j = 1; j <= n; j++) + + t[i][j] = t[i - 1][t[i - 1][j]]; + + while (m) { + + int i = 0; + + m--; + + in >> q >> p; + + rez = q; + + while (p) { + + if (p % 2 != 0) rez = t[i][rez]; + + p /= 2; + + i++; + + } + + out << rez << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/56f63c48-139a-11eb-a94f-309c234925a0.cpp b/ancestors/dp_log/56f63c48-139a-11eb-a94f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dc77e3f54f9cf44cbce5080a9656290d3d4a6a0 --- /dev/null +++ b/ancestors/dp_log/56f63c48-139a-11eb-a94f-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int maxi=250001; + +int d[18][maxi]; + + + +int main() + +{ + + ios::sync_with_stdio(false); + + int n,m,i,j,q,p; + + fin>>n>>m; + + for(i=1; i<=n; i++) + + fin>>d[0][i]; + + for(i=1; i<18; i++) + + for(j=1; j<=n; j++) + + d[i][j]=d[i-1][d[i-1][j]]; + + + + for(i=0; i>q>>p; + + j=0; + + while(p!=0) + + { + + if(p%2!=0) + + q=d[j][q]; + + p/=2; + + j++; + + } + + fout< + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, dp[19][250010], q, p, r, rez; + + + +int main() { + + f>>n>>m; + + for(int i = 1; i <= n; i++) + + f>>dp[0][i]; + + for(int j = 1; j <= 18; j++) + + for(int i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + + + for(int i = 1; i <= m; i++) { + + f>>q>>p; + + r = 0; + + rez = q; + + + + while(p != 0) { + + if(p % 2 != 0) + + rez = dp[r][rez]; + + p = p >> 1; + + r++; + + } + + g< + + + + + + +using namespace std; + +int n,dp[250010][18 +1],q; + +int main() + +{ + + ifstream fin("stramosi.in"); + + ofstream fout("stramosi.out"); + + fin>>n>>q; + + for(int i=1;i<=n;++i) + + fin>>dp[i][0]; + + for(int i=1;i<=n;++i) + + for(int j=1;j<=18;++j) + + dp[i][j]=dp[dp[i][j-1]][j-1]; + + while(q--) + + { + + int d,p; + + fin>>d>>p; + + for(int i=18;i>=0;--i) + + if(p & (1< + + +using namespace std; + + + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); +int v[100][150]; + +int d[18][2500005]; + + + +int main() + +{ + + + + int n, t; + + cin >> n >> t; + + + + for (int i = 1; i <= n; i++) + + cin >> d[0][i]; + + + + for (int i = 1; i < 18; i++) + + for (int j = 1; j <= n; j++) + + d[i][j] = d[i - 1][d[i - 1][j]]; + + + + int ans = 0; + + int x, y, k = 0; + + while(t--) + + { + + k = 0; + + cin >> x >> y; + + + + ans = x; + + + + while(y) + + { + + if (y % 2 != 0) + + ans = d[k][ans]; + + y /= 2; + + k++; + + } + + + + cout << ans << "\n"; + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/5735b646-139a-11eb-847e-309c234925a0.cpp b/ancestors/dp_log/5735b646-139a-11eb-847e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60675a0864656fca1608b4b38feb49c327813670 --- /dev/null +++ b/ancestors/dp_log/5735b646-139a-11eb-847e-309c234925a0.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +const int NMAX = 250000; +const int LOGMAX = 18; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +int dp[LOGMAX + 1][NMAX + 1]; + +int main() +{ + int n,m,i,j,p,q,ans; + fin >> n >> m; + for(i=1; i<=n; i++) + fin >> dp[0][i]; + for(i=1; i<=LOGMAX; i++) + for(j=1; j<=n; j++) + dp[i][j] = dp[i-1][dp[i-1][j]]; + while(m--){ + fin >> q >> p; + ans = q; + j = 0; + while(p){ + if(p % 2) + ans = dp[j][ans]; + p /= 2; + j++; + } + fout << ans << "\n"; + } + return 0; +} diff --git a/ancestors/dp_log/57482cd8-139a-11eb-8c39-309c234925a0.cpp b/ancestors/dp_log/57482cd8-139a-11eb-8c39-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f492357802bbcef82194eb070e83c2dd26f38318 --- /dev/null +++ b/ancestors/dp_log/57482cd8-139a-11eb-8c39-309c234925a0.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int v[20][250001], n, m; + + + +int main() + +{ + + in >> n >> m; + + for(int i = 1; i <= n; i++) + + { + + in >> v[0][i]; + + } + + for(int i = 1; i <= 18; i++) + + { + + for(int j = 1; j <= n; j++) + + { + + v[i][j] = v[i-1][v[i-1][j]]; + + } + + } + + while(m) + + { + + m --; + + int p, q, ans, j = 0; + + in >> q >> p; + + ans = q; + + while(p) + + { + + if(p%2 != 0) + + { + + ans = v[j][ans]; + + } + + p /= 2; + + j++; + + } + + out << ans << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/575747f8-139a-11eb-bd5d-309c234925a0.cpp b/ancestors/dp_log/575747f8-139a-11eb-bd5d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65799070197829d2211bbda043f99b0b4e4817e2 --- /dev/null +++ b/ancestors/dp_log/575747f8-139a-11eb-bd5d-309c234925a0.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int maxx=250001; + + + + + +int main() + +{ + + int v[18][maxx]; + + ios::sync_with_stdio(false); + + int n,m,i,j,q,p; + + in>>n>>m; + + for(i=1; i<=n; i++) + + { + + in>>v[0][i]; + + } + + for(i=1; i<18; i++) + + { + + for(j=1; j<=n; j++) + + { + + v[i][j]=v[i-1][v[i-1][j]]; + + } + + } + + for(i=0; i>q>>p; + + j=0; + + while(p!=0) + + { + + if(p%2!=0) + + q=v[j][q]; + + p/=2; + + j++; + + } + + out< + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250000; + +const int LOGMAX = 18; + + + +int t[LOGMAX + 1][NMAX + 1]; + + + +int main() { + + int n, m; + + fin>>n>>m; + + for( int i = 1; i <= n; i ++ ) + + fin>>t[0][i]; + + for( int dim = 1; (1<>node>>depth; + + int pas; + + for( pas = 1; (1< + +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + + + +int n, m, dp[19][250005], q, p, r, rez; + +int main() { + fin >> n >> m; + for(int i = 1; i <= n; ++i) + fin >> dp[0][i]; + for(int j = 1; j <= 18; ++j) + for(int i = 1; i <= n; ++i) + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + for(int i = 1; i <= m; ++i) { + fin >> q >> p; + r = 0; rez = q; + + while(p) { + if(p % 2) rez = dp[r][rez]; + p >>= 1; ++r; + } + + fout << rez << '\n'; + } +} diff --git a/ancestors/dp_log/57cab226-139a-11eb-8e00-309c234925a0.cpp b/ancestors/dp_log/57cab226-139a-11eb-8e00-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1216f3411ea85851d751aee5857cbcf79dc0b0a --- /dev/null +++ b/ancestors/dp_log/57cab226-139a-11eb-8e00-309c234925a0.cpp @@ -0,0 +1,316 @@ +#include + + +using namespace std; + +int n, Q, x, c, p, t[250005][18]; + +void build() + +{ + + for(int i=1; i<=17; i++) + + { + + for(int nod=1; nod<=n; nod++) + + t[nod][i] = t[t[nod][i-1]][i-1]; + + } + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +class OutParser { + +private: + + FILE *fout; + + char *buff; + + int sp; + + + + void write_ch(char ch) { + + if (sp == 50000) { + + fwrite(buff, 1, 50000, fout); + + sp = 0; + + buff[sp++] = ch; + + } else { + + buff[sp++] = ch; + + } + + } + + + + + +public: + + OutParser(const char* name) { + + fout = fopen(name, "w"); + + buff = new char[50000](); + + sp = 0; + + } + + ~OutParser() { + + fwrite(buff, 1, sp, fout); + + fclose(fout); + + } + + + + OutParser& operator << (int vu32) { + + if (vu32 <= 9) { + + write_ch(vu32 + '0'); + + } else { + + (*this) << (vu32 / 10); + + write_ch(vu32 % 10 + '0'); + + } + + return *this; + + } + + + + OutParser& operator << (long long vu64) { + + if (vu64 <= 9) { + + write_ch(vu64 + '0'); + + } else { + + (*this) << (vu64 / 10); + + write_ch(vu64 % 10 + '0'); + + } + + return *this; + + } + + + + OutParser& operator << (char ch) { + + write_ch(ch); + + return *this; + + } + + OutParser& operator << (const char *ch) { + + while (*ch) { + + write_ch(*ch); + + ++ch; + + } + + return *this; + + } + +}; + + + +int main() + +{ + + InParser fin("stramosi.in"); + + OutParser fout("stramosi.out"); + + fin >> n >> Q; + + for(int i=1; i<=n; i++) + + fin >> t[i][0]; + + build(); + + while(Q--) + + { + + fin >> x >> c; + + if(c > n) + + fout << 0 << '\n'; + + else + + { + + p = 0; + + while(c) + + { + + if(c&1) + + x = t[x][p]; + + c>>=1; + + ++p; + + } + + fout << x << '\n'; + + } + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/57d97f28-139a-11eb-b768-309c234925a0.cpp b/ancestors/dp_log/57d97f28-139a-11eb-b768-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95027b461b20e6485d0b1a65de83e04507650193 --- /dev/null +++ b/ancestors/dp_log/57d97f28-139a-11eb-b768-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n, v[20][250001], m; + + + +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=n; i++) + + in>>v[0][i]; + + for(int i=1; i<=18; i++) + + for(int j=1; j<=n; j++) + + v[i][j]=v[i-1][v[i-1][j]]; + + while(m) + + { + + m--; + + int p,q, ans,j=0; + + in>>q>>p; + + ans=q; + + while(p) + + { + + if(p%2!=0) + + ans=v[j][ans]; + + p/=2; + + j++; + + } + + out< + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int MAX = 250001; + + + +int dp[18][MAX]; + +int n, m, x, y; + + + +int main() + +{ + + in >> n >> m; + + for(int i = 1; i <= n; i++) + + { + + in >> dp[0][i]; + + } + + for(int i = 1; i < 18; i++) + + { + + for(int j = 1; j <= n; j++) + + { + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + } + + } + + while(m--) + + { + + in >> x >> y; + + int j = 0; + + while(y) + + { + + if(y % 2 != 0) + + { + + x = dp[j][x]; + + } + + y /= 2; + + j++; + + } + + out << x << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/57fac2f8-139a-11eb-ba35-309c234925a0.cpp b/ancestors/dp_log/57fac2f8-139a-11eb-ba35-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5645051c6597fe16227e640f5fd6585689e06de1 --- /dev/null +++ b/ancestors/dp_log/57fac2f8-139a-11eb-ba35-309c234925a0.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int n,m,p[250005],dp[19][250005]; + +int main() + +{int i,j,q,P,p_2,stramos; + + in >> n >> m; + + for (i = 1; i <= n; i++) in >> dp[0][i]; + + + + for (j = 1; j <= 18; j++) + + for (i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + + + for (i = 1; i <= m; i++) + + { + + in >> q >> P; + + p_2 = 0; + + stramos = q; + + while (P) + + { + + if (P % 2 == 1) + + stramos = dp[p_2][stramos]; + + P >>= 1; + + p_2++; + + } + + out << stramos << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/58318a46-139a-11eb-9db9-309c234925a0.cpp b/ancestors/dp_log/58318a46-139a-11eb-9db9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c45fc2cb6744a7a318f86c00718257d9d131baf2 --- /dev/null +++ b/ancestors/dp_log/58318a46-139a-11eb-9db9-309c234925a0.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n,i,m,p,q,j,ex; + +int mat[19][250001],stramos; + +int main() + +{ + + fin>>n>>m; + + for(i=1; i<=n; i++) + + { + + fin>>mat[0][i]; + + } + + for(j=1;j<=18;j++) + + { + + for(i=1;i<=n;i++) + + mat[j][i]=mat[j-1][mat[j-1][i]]; + + } + + while(m--) + + { + + fin>>q>>p; + + stramos=q; + + ex=0; + + while(p) + + { + + if(p%2==1) + + stramos=mat[ex][stramos]; + + p>>=1; + + ex++; + + } + + fout< +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int N=250005; + +int far[18][N]; + +int n,m; + + + +int main(){ + + in>>n>>m; + + for(int i=1;i<=n;i++){ + + in>>far[0][i]; + + } + + for(int lg=1;lg<18;lg++){ + + for(int i=1;i<=n;i++) + + far[lg][i]=far[lg-1][far[lg-1][i]]; + + } + + while(m--){ + + int nod,k; + + in>>nod>>k; + + for(int lg=0;lg<18;lg++){ + + if(k%2){ + + nod=far[lg][nod]; + + } + + k/=2; + + } + + out< +using namespace std; + +ifstream fi("stramosi.in"); +ofstream fo("stramosi.out"); + +const int N = 250005, LG = 19; + +int *_inner_mx; + +vector g[N]; +int *far[LG], lvl[N]; + +int n, q; + +void make_far() { + for (int lg = 1; lg < LG; ++lg) + for (int i = 1; i <= n; ++i) + far[lg][i] = far[lg - 1][far[lg - 1][i]]; +} + +int get_far(int nod, int lvl) { + if (lvl > (1 << LG - 1)) + return 0; + for (int i = 0; i < LG; ++i) + if ((1 << i) & lvl) + nod = far[i][nod]; + return nod; +} + +int main() { + fi >> n >> q; + + _inner_mx = new int[(n + 1) * LG]; + for (int i = 0; i < LG; ++i) + far[i] = &_inner_mx[i * (n + 1)]; + for (int i = 0; i < LG; ++i) + far[i][0] = 0; + + for (int i = 1; i <= n; ++i) + fi >> far[0][i]; + make_far(); + + while (q--) { + int nod, lvl; + fi >> nod >> lvl; + fo << get_far(nod, lvl) << '\n'; + } + + return 0; +} diff --git a/ancestors/dp_log/587007ac-139a-11eb-a96c-309c234925a0.cpp b/ancestors/dp_log/587007ac-139a-11eb-a96c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32026d607a451ce18658b6351437c6b36602dd78 --- /dev/null +++ b/ancestors/dp_log/587007ac-139a-11eb-a96c-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int N,M,P,Q,dp[21][250005]; + +int rasp(int a,int b) + +{ + + int nod=a; + + for(int i=0; (1<>N>>M; + + for(int i=1; i<=N; i++) + + fin>>dp[0][i]; + + for(int j=1; j<=N; j++) + + { + + for(int i=1; i<=20; i++) + + { + + dp[i][j]=dp[i-1][dp[i-1][j]]; + + } + + } + + for(int i=1; i<=M; i++) + + { + + fin>>Q>>P; + + fout< +using namespace std; + +const int N=250005; + +int p[20][N]; + +int lg2[N]; + +const int put[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144}; + +int main() + +{ + + FILE*fin,*fout; + + fin=fopen("stramosi.in","r"); + + fout=fopen("stramosi.out","w"); + + int n,q; + + fscanf(fin,"%d%d",&n,&q); + + for(int i=1;i<=n;++i) + + fscanf(fin,"%d",&p[0][i]); + + lg2[1]=0; + + for(int i=2;i +using namespace std; + +ifstream fi("stramosi.in"); +ofstream fo("stramosi.out"); + +const int N = 250005, LG = 19; + +vector g[N]; +int far[LG][N], lvl[N]; + +int n, q; + +void make_far() { + for (int lg = 1; lg < LG; ++lg) + for (int i = 1; i <= n; ++i) + far[lg][i] = far[lg - 1][far[lg - 1][i]]; +} + +int get_far(int nod, int lvl) { + if (lvl > (1 << LG - 1)) + return 0; + for (int i = 0; i < LG; ++i) + if ((1 << i) & lvl) + nod = far[i][nod]; + return nod; +} + +int main() { + fi >> n >> q; + for (int i = 1; i <= n; ++i) + fi >> far[0][i]; + make_far(); + + while (q--) { + int nod, lvl; + fi >> nod >> lvl; + fo << get_far(nod, lvl) << '\n'; + } + + return 0; +} diff --git a/ancestors/dp_log/58ccd6ec-139a-11eb-a154-309c234925a0.cpp b/ancestors/dp_log/58ccd6ec-139a-11eb-a154-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..465cd8cbff7e0a15a48000870b30e3ccc261e1c2 --- /dev/null +++ b/ancestors/dp_log/58ccd6ec-139a-11eb-a154-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +const int N=250005; + +int p[20][N]; + +int lg2[N]; + +int main() + +{ + + FILE*fin,*fout; + + fin=fopen("stramosi.in","r"); + + fout=fopen("stramosi.out","w"); + + int n,q; + + fscanf(fin,"%d%d",&n,&q); + + for(int i=1;i<=n;i++){ + + fscanf(fin,"%d",&p[0][i]); + + } + + lg2[1]=0; + + for(int i=2;i + + + + +using namespace std; +typedef long long ll; +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); +int n, m, i, j, v[250005][20], x, p, q, p2, ip; +int main() +{ + fin>>n>>m; + for(i=1;i<=n;++i) + { + fin>>x; + v[i][0]=x; + j=0; + do + { + ++j; + v[i][j]=v[v[i][j-1]][j-1]; + }while(v[i][j]); + } + for(i=1;i<=m;++i) + { + fin>>q>>p; + p2=1; + ip=0; + while(p2<=p) + { + p2*=2; + ++ip; + } + p2/=2; + --ip; + while(p&&q) + { + while(p2>p) + { + p2/=2; + --ip; + } + p-=p2; + q=v[q][ip]; + } + fout< + + +using namespace std; + + + +const int N = 250005; + + + +int dp[21][N], n, exp[N]; + + + +void Read () + +{ + + ifstream fin ("stramosi.in"); + + int q; + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + for (int i = 1; (1 << i) <= n; i++) + + exp[(1 << i)] = i; + + for (int i = 1; (1 << i) <= n; i++) + + for (int j = 1; j <= n; j++) + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + ofstream fout ("stramosi.out"); + + while (q--) + + { + + int q, p, x; + + fin >> q >> p; + + while (p > 0) + + { + + x = exp[p & (-p)]; + + q = dp[x][q]; + + p -= (p & (-p)); + + } + + fout << q << "\n"; + + } + + fout.close(); + +} + + + + + +int main() + +{ + + Read(); + + return 0; + +} diff --git a/ancestors/dp_log/58f9f27a-139a-11eb-9ed8-309c234925a0.cpp b/ancestors/dp_log/58f9f27a-139a-11eb-9ed8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1accd471fe6d47ef98489371ce92310c819a1021 --- /dev/null +++ b/ancestors/dp_log/58f9f27a-139a-11eb-9ed8-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include +int const NMAX = 250000; + +int const MAXLOG = 17; + +int log[5 + NMAX]; + +int dp[5 + MAXLOG][5 + NMAX]; + +int n; + +int afis(int &node ,int &p){ + + while(0 < p && 0 < node){ + + node = dp[log[p]][node]; + + p -= (1 << log[p]); + + } + + return node; + +} + + + +int main() + +{ + + freopen("stramosi.in" , "r" , stdin); + + freopen("stramosi.out" , "w" , stdout); + + int k; + + scanf("%d%d" , &n , &k); + + for(int i = 1 ; i <= n ;i++){ + + scanf("%d" , &dp[0][i]); + + } + + log[1] = 0; + + for(int i = 2 ; i <= n ;i++){ + + log[i] = log[(i >> 1)] + 1; + + } + + for(int h = 1 ; h <= MAXLOG ;h++){ + + for(int i = 1 ; i <= n ;i++){ + + dp[h][i] = dp[h - 1] [dp[h - 1][i]]; + + } + + } + + int a , b; + + for(int i = 1 ; i <= k ;i++){ + + scanf("%d%d" , &a , &b); + + printf("%d\n" , afis(a , b)); + + } + + return 0; + +} diff --git a/ancestors/dp_log/5908987a-139a-11eb-a4e0-309c234925a0.cpp b/ancestors/dp_log/5908987a-139a-11eb-a4e0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd844e4dc60965e84aec6e3d1496a8e5a68c35cd --- /dev/null +++ b/ancestors/dp_log/5908987a-139a-11eb-a4e0-309c234925a0.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n,m,s[21][250005]; + + + +int rasp(int a,int b) + +{ + + int node=a; + + for(int i=0; (1<>n>>m; + + for(int i=1; i<=n; i++) + + in>>s[0][i]; + + for(int k=1; k<=20; k++) + + for(int i=1; i<=n; i++) + + s[k][i]=s[k-1][s[k-1][i]]; + + for(int i=1,a,b; i<=m; i++) + + { + + in>>a>>b; + + out< +#include + + +using namespace std; + + + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + + + +int N, M; + +int dp[20][250005]; + + + +int main() + +{ + + int query1, query2, pas; + + cin >> N >> M; + + for(int i = 1; i <= N; i++){ + + cin >> dp[0][i]; + + } + + for(int i = 1; i < 20; i++){ + + for(int j = 1; j <= N; j++){ + + dp[i][j] = dp[i-1][dp[i - 1][j]]; + + } + + } + + for(int i = 1; i <= M; i++){ + + cin >> query1 >> query2; + + pas = 0; + + while(query2 != 0){ + + if(query2 % 2 != 0){ + + query1 = dp[pas][query1]; + + } + + query2 >>= 1; + + ++pas; + + } + + cout << query1 << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/592aec42-139a-11eb-b7d3-309c234925a0.cpp b/ancestors/dp_log/592aec42-139a-11eb-b7d3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86ef85e0c85733cef3221e5cb3e506c38dd2f98a --- /dev/null +++ b/ancestors/dp_log/592aec42-139a-11eb-b7d3-309c234925a0.cpp @@ -0,0 +1,75 @@ +#include +#include + + +using namespace std; + + + +const int N = 250005; + + + +int dp[21][N], n; + + + +void Read () + +{ + + ifstream fin ("stramosi.in"); + + int q; + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + for (int i = 1; (1 << i) <= n; i++) + + for (int j = 1; j <= n; j++) + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + ofstream fout ("stramosi.out"); + + while (q--) + + { + + int q, p, x; + + fin >> q >> p; + + x = q; + + for (int i = 0; (1 << i) <= p; i++) + + if ((1 << i) & p) + + x = dp[i][x]; + + fout << x << "\n"; + + } + + fout.close(); + +} + + + + + +int main() + +{ + + Read(); + + return 0; + +} diff --git a/ancestors/dp_log/593c0e5e-139a-11eb-a316-309c234925a0.cpp b/ancestors/dp_log/593c0e5e-139a-11eb-a316-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7194bbb738d6dec999593c63981263d7205ce4fe --- /dev/null +++ b/ancestors/dp_log/593c0e5e-139a-11eb-a316-309c234925a0.cpp @@ -0,0 +1,52 @@ +#include + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int n,m,s[21][250005]; + +int rasp(int a,int b){ + + int node=a; + + for(int i=0;(1<>n>>m; + + for(int i=1;i<=n;i++) + + in>>s[0][i]; + + for(int k=1;k<=20;k++) + + for(int i=1;i<=n;i++) + + s[k][i]=s[k-1][s[k-1][i]]; + + for(int i=1,a,b;i<=m;i++){ + + in>>a>>b; + + out< + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,P,Q,x,rmq[20][250001]; + + + +int main() + +{ + + f>>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>x; + + rmq[0][i]=x; + + } + + + + for(int i=1;i<=log2(N);i++) + + for(int j=1;j<=N;j++) + + rmq[i][j]=rmq[i-1][rmq[i-1][j]]; + + + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + int nod=Q; + + while(P) + + { + + int poz = P & -P; + + P&=P-1; + + int dif=log2(poz); + + nod=rmq[dif][nod]; + + } + + g< + + + + + + + +using namespace std; + + + +ifstream f("stramosi" ".in"); + +ofstream g("stramosi" ".out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef pair pi; + +typedef pair pct; + + + +const ll inf = 1LL << 60; + +const ll mod = 1e9 + 7; + +const ld eps = 1e-9; + + + + + +void add(ll &a , ll b) + +{ + + a += b; + + a %= mod; + +} + + + +void sub(ll &a, ll b) + +{ + + a = (a - b + mod) % mod; + +} + + + +int n, m, pd[20][250010], x, y; + + + +int main() + +{ + + f >> n >> m; + + for(int i = 1; i <= n; ++i) + + f >> pd[0][i]; + + for(int i = 1; i <= 18; ++i) + + for(int j = 1; j <= n; ++j) + + pd[i][j] = pd[ i - 1 ][ pd[i - 1][j] ]; + + while(m--) + + { + + f >> x >> y; + + for(int i = 0; i <= 18; ++i) + + if(y & (1 << i)) + + x = pd[i][x]; + + g << x << '\n'; + + } + + f.close(); + + g.close(); + + return 0; + +} diff --git a/ancestors/dp_log/59961090-139a-11eb-8b87-309c234925a0.cpp b/ancestors/dp_log/59961090-139a-11eb-8b87-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2e05525dcdfe444fd976061fde2825d326faedf --- /dev/null +++ b/ancestors/dp_log/59961090-139a-11eb-8b87-309c234925a0.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; +int n,m,tata[250005],str[35][250005],l[250005]; +void build_str () { + for(int i=2;i<=n;++i) + l[i]=l[(i>>1)]+1; + for(int i=1;i<=n;++i) + str[0][i]=tata[i]; + for(int k=1;(1<0;++i) + if((p & (1< + + + + +using namespace std; + +using VI = vector; + +using VVI = vector; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n, q, x, k, res; + +VVI t; + +int main() + +{ + + ios::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> n >> q; + + t = VVI(20, VI(n + 2)); + + for (int i = 1; i <= n; ++i) + + fin >> t[0][i]; + + for (int p = 1; p <= 18; ++p) + + for (int i = 1; i <= n; ++i) + + t[p][i] = t[p-1][t[p-1][i]]; + + while (q--) + + { + + fin >> x >> k; + + res = x; + + for (int p = 18; p >= 0 && k; --p) + + if ((1 << p) <= k) + + { + + k -= (1 << p); + + res = t[p][res]; + + } + + fout << res << '\n'; + + } + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/dp_log/59b83ab4-139a-11eb-bcbf-309c234925a0.cpp b/ancestors/dp_log/59b83ab4-139a-11eb-bcbf-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23a29d5ccfbafd22ac6b6c9a16f421d1ca0bda6b --- /dev/null +++ b/ancestors/dp_log/59b83ab4-139a-11eb-bcbf-309c234925a0.cpp @@ -0,0 +1,78 @@ +#include + + + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int T[20][250005]; + + + +int getStramos(int nod, int k){ + + for(int pw = 19; pw >= 0; --pw){ + + if((1 << pw) <= k){ + + k -= (1 << pw); + + nod = T[pw][nod]; + + } + + } + + return nod; + +} + + + +int main() + +{ + + int n, k; + + fin >> n >> k; + + + + for(int i = 1; i <= n; ++i) + + fin >> T[0][i]; + + + + for(int pw = 1; pw <= 19; ++pw) + + for(int i = 1; i <= n; ++i) + + T[pw][i] = T[pw - 1][T[pw - 1][i]]; + + + + for(int i = 1; i <= k; ++i){ + + int nod, k; + + fin >> nod >> k; + + + + fout << getStramos(nod, k) << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/59cbbc06-139a-11eb-bd40-309c234925a0.cpp b/ancestors/dp_log/59cbbc06-139a-11eb-bd40-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f0ced9cd1f55c7fe3be51720bb9d5612890684f --- /dev/null +++ b/ancestors/dp_log/59cbbc06-139a-11eb-bd40-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n,m,mat[20][250000 +5],q,p; + + + +int main() + +{ + + int i,j; + + fin>>n>>m; + + for(i=1;i<=n;i++){ + + fin>>mat[0][i]; + + } + + for(j=1;j<=17;j++){ + + for(i=1;i<=n;i++){ + + mat[j][i]=mat[j-1][mat[j-1][i]]; + + } + + } + + while(m--){ + + fin>>q>>p; + + for( j=17;j>=0;j--){ + + if((1< + + +using namespace std; + + + +int stramosi[20][250001]; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int main() { + + int n, m, i, x, j,p,q; + + long long int t; + + fin >> n >> m; + + for(i = 1; i <= n; ++i) + + { + + fin >> stramosi[0][i]; + + } + + for(i = 1; i <= 19; ++i) + + for(j = 1; j <= n; ++j) + + stramosi[i][j] = stramosi[i - 1][stramosi[i-1][j]]; + + for(i = 1; i <= m; ++i){ + + fin >> q >> p; + + for(j = 0; t = (1 << j), t<= p; ++j) + + if(t & p) + + q = stramosi[j][q]; + + fout << q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/59e95aee-139a-11eb-b41d-309c234925a0.cpp b/ancestors/dp_log/59e95aee-139a-11eb-b41d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d197d650423bab130eef5ea452366d40106e843 --- /dev/null +++ b/ancestors/dp_log/59e95aee-139a-11eb-b41d-309c234925a0.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +using namespace std; + +int stramosi[20][250001]; +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +int main() { + int n, m, i, x, j,p,q; + long long int t; + fin >> n >> m; + for(i = 1; i <= n; ++i) + { + fin >> stramosi[0][i]; + } + for(i = 1; i <= 19; ++i) + for(j = 1; j <= n; ++j) + stramosi[i][j] = stramosi[i - 1][stramosi[i-1][j]]; + for(i = 1; i <= m; ++i){ + fin >> q >> p; + for(j = 0; t = (1 << j), t<= p; ++j) + if(t & p) + q = stramosi[j][q]; + fout << q << '\n'; + } + return 0; +} diff --git a/ancestors/dp_log/59f7b2d0-139a-11eb-9a64-309c234925a0.cpp b/ancestors/dp_log/59f7b2d0-139a-11eb-9a64-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81dbd8d0a212aa3589af9fe6b867f300f74d3f1c --- /dev/null +++ b/ancestors/dp_log/59f7b2d0-139a-11eb-9a64-309c234925a0.cpp @@ -0,0 +1,72 @@ +#include + + +using namespace std; + +int t[250005]; + +int stra[19][250005]; + +int up(int x, int p) + + { + + for (int i=18;i>=0;i--) + + if (1<>n>>m; + + for (int i=1;i<=n;i++) + + { + + fin>>t[i]; + + stra[0][i]=t[i]; + + } + + for (int i=1;i<=18;i++) + + for (int j=1;j<=n;j++) + + stra[i][j]=stra[i-1][stra[i-1][j]]; + + + + for (int i=1;i<=m;i++) + + { + + fin>>x>>p; + + fout< +using namespace std; + + + +const int MAX_N = 25e4 + 1; + +const int LOG_MAX_N = 17; + + + +int binary_lifting[LOG_MAX_N + 1][MAX_N], N, M; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1; i <= N; ++i) + + fin >> binary_lifting[0][i]; + + + + for(int k = 1; k <= LOG_MAX_N; ++k) + + for(int i = 1; i <= N; ++i) + + binary_lifting[k][i] = binary_lifting[k - 1][binary_lifting[k - 1][i]]; + + + + for(int i = 1; i <= M; ++i) + + { + + int q, p; + + fin >> q >> p; + + + + for(int j = 0; q != 0 && j <= LOG_MAX_N; ++j) + + { + + if((1 << j) & p){ + + q = binary_lifting[j][q]; + + } + + } + + + + fout << q << '\n'; + + } + + + +} diff --git a/ancestors/dp_log/5a17bdc0-139a-11eb-9afd-309c234925a0.cpp b/ancestors/dp_log/5a17bdc0-139a-11eb-9afd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6aa853df850579da437abd1cb18682a82aca2b66 --- /dev/null +++ b/ancestors/dp_log/5a17bdc0-139a-11eb-9afd-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +const int MAX_N = 25e4 + 1; + +const int LOG_MAX_N = 18; + + + +int binary_lifting[LOG_MAX_N][MAX_N], parent[MAX_N], lg[MAX_N], N, M; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 2; i <= N; ++i) + + lg[i] = lg[i >> 1] + 1; + + + + for(int i = 1; i <= N; ++i) + + fin >> parent[i]; + + + + for(int i = 1; i <= N; ++i) + + binary_lifting[0][i] = parent[i]; + + + + for(int k = 1; k <= lg[N]; ++k) + + for(int i = 1; i <= N; ++i) + + binary_lifting[k][i] = binary_lifting[k - 1][binary_lifting[k - 1][i]]; + + + + for(int i = 1; i <= M; ++i) + + { + + int q, p; + + fin >> q >> p; + + + + for(int j = 0; q != 0 && j <= lg[N]; ++j) + + { + + if((1 << j) & p){ + + q = binary_lifting[j][q]; + + } + + } + + + + fout << q << '\n'; + + } + + + +} diff --git a/ancestors/dp_log/5a271864-139a-11eb-a8e2-309c234925a0.cpp b/ancestors/dp_log/5a271864-139a-11eb-a8e2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a47324a2d5f77157064c60b5f945e762f49362ec --- /dev/null +++ b/ancestors/dp_log/5a271864-139a-11eb-a8e2-309c234925a0.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int n,m,x,y; + +vector graf[250001]; + +vector s; + +int parinti[250001][20]; + +bool viz[250001]; + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= n;++i) + + { + + f>>x; + + if(x != 0) + + graf[x].push_back(i); + + } + +} + + + +void DFS(int nod) + +{ + + viz[nod] = nod; + + s.push_back(nod); + + for(vector::iterator it = graf[nod].begin();it != graf[nod].end();++it) + + if(!viz[*it]) + + { + + for(int i = 0;(1 << i) <= s.size();++i) + + parinti[*it][i] = s[s.size() - (1 << i)]; + + DFS(*it); + + } + + s.pop_back(); + +} + + + +void Solve() + +{ + + for(int i = 1;i <= n;++i) + + if(!viz[i]) + + { + + s.clear(); + + DFS(i); + + } + + for(int i = 1;i <= m;++i) + + { + + f>>x>>y; + + while(y) + + { + + int i = 0; + + for(i = 0;(1 << i) <= y;++i); + + y -= (1 << (i - 1)); + + x = parinti[x][i - 1]; + + } + + g< + + +using namespace std; + +ifstream fi("stramosi.in"); + +ofstream fo("stramosi.out"); + +int N,M; + +int P[250005]; + +int DP[40][250005]; + +int query(int nr,int nod) + +{ + + + + int k=30; + + while(nr>0) + + { + + if(nr-(1<=0) + + { + + nr=nr-(1<>N>>M; + + for(int i=1;i<=N;i++) + + { + + fi>>P[i]; + + } + + for(int i=1;i<=N;i++) + + { + + DP[0][i]=P[i]; + + } + + + + for(int k=1;(1<>nod>>nr; + + fo< + +using namespace std; +int lg[2000000],nex[23][300005],put[30]; +int que(int nod,int p){ + if(p==0){ + return nod; + } + int e=lg[p]; + return que(nex[e][nod],p-put[e]); +} +int main() +{ + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + int n,q,nod,p,a; + cin>>n>>q; + for(int i=1;i<=n;i++){ + cin>>a; + nex[0][i]=a; + } + nex[0][0]=0; + for(int i=1;i<=20;i++){ + for(int j=1;j<=n;j++){ + nex[i][j]=nex[i-1][nex[i-1][j]]; + } + } + put[0]=1; + for(int i=1;i<=20;i++){ + put[i]=put[i-1]*2; + lg[put[i]]++; + } + for(int i=1;i<=1000000;i++){ + lg[i]+=lg[i-1]; + } + for(int i=1;i<=q;i++){ + cin>>nod>>p; + cout< + + + + +using namespace std; + + + +class Parser { + + private: + + static const int SIZE = 1e5; + + char str[SIZE]; + + + + int ptr; + + FILE *fin; + + + + char getChar() { + + if (ptr == SIZE) { + + fread(str, sizeof(char), SIZE, fin); + + ptr = 0; + + } + + return str[ptr++]; + + } + + + + int getInt() { + + char chr = getChar(); + + while (!isdigit(chr) && chr != '-') + + chr = getChar(); + + + + int sgn = +1; + + if (chr == '-') { + + sgn = -1; + + chr = getChar(); + + } + + + + int nr = 0; + + while (isdigit(chr)) { + + nr = nr * 10 + chr - '0'; + + chr = getChar(); + + } + + return nr * sgn; + + } + + + + public: + + Parser(const char* str) : + + ptr(SIZE), fin(fopen(str, "r")) { } + + + + ~Parser() { + + fclose(fin); + + } + + + + friend Parser& operator>>(Parser& in, int& nr) { + + nr = in.getInt(); + + return in; + + } + +}; + + + +Parser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 25e4+10; + + + +int dp[DMAX][20]; + + + +int n,m; + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int i,j; + + int node,put,p,cate; + + + + fin>>n>>m; + + for(i=1;i<=n;i++) + + fin>>dp[i][0]; + + + + for(i=1;i<=17;i++) + + for(j=1;j<=n;j++) + + dp[j][i]=dp[dp[j][i-1]][i-1]; + + while(m--){ + + fin>>node>>p; + + for(i=17;i>=0;i--) + + if((1< + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int n,m,t[250100],p,q,x,sum; + +int s[20][250100]; + +int lg(int x) + +{ + + int ans=0; + + while(x/2>=1) + + { + + x/=2; + + ans++; + + } + + return ans; + +} + +int find(int x,int t) + +{ + + int p=x; + + int sum=0; + + int nr=lg(t); + + while(sumt) nr--; + + else + + { + + p=s[nr][p]; + + if(p==0) return 0; + + sum+=(1<>n>>m; + + for(int i=1;i<=n;i++) + + { + + in>>x; + + t[i]=x; + + } + + for(int i=1;i<=n;i++) s[0][i]=t[i]; + + for(int i=1;i<20;i++) + + for(int j=1;j<=n;j++) + + s[i][j]=s[i-1][s[i-1][j]]; + + while(m--) + + { + + in>>q>>p; + + out< +#include + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, x, y; + +int tt[250005]; + +int dp[20][250005]; + + + +int main() + +{ + + f >> n >> m; + + for (int i = 1; i <= n; i++) + + { + + f >> dp[0][i]; + + } + + + + for (int i = 1; i <= 18; i++) + + { + + for (int j = 1; j <= n; j++) + + dp[i][j]=dp[i-1][dp[i-1][j]]; + + } + + + + while (m--) + + { + + f >> x >> y; + + + + for (int i = 0; i <= 18; i++) + + if (y&(1< + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int n,m,t[250100],p,q,x,sum; + +int s[20][250100]; + +int lg(int x) + +{ + + int ans=0; + + while(x/2>=1) + + { + + x/=2; + + ans++; + + } + + return ans; + +} + +int find(int x,int t) + +{ + + int p=x; + + int sum=0; + + int nr=lg(t); + + while(sumt) nr--; + + else + + { + + p=s[nr][p]; + + sum+=(1<>n>>m; + + for(int i=1;i<=n;i++) + + { + + in>>x; + + t[i]=x; + + } + + for(int i=1;i<=n;i++) s[0][i]=t[i]; + + for(int i=1;i<20;i++) + + for(int j=1;j<=n;j++) + + s[i][j]=s[i-1][s[i-1][j]]; + + while(m--) + + { + + in>>q>>p; + + out< + + +FILE *in = fopen("stramosi.in", "r"), *out = fopen("stramosi.out", "w") ; + + + +const int MV = 25e4 ; + + + +int dp[19][MV + 5] ; + + + +void pre(int n) { + + for (int i = 1 ; (1 << i) <= n ; ++ i) { + + for (int j = 1 ; j <= n ; ++ j) { + + dp[i][j] = dp[i - 1][dp[i - 1][j]] ; + + } + + } + +} + + + +int eval(int val, int start) { + + int ret(start) ; + + int lg(0) ; + + for ( ; (1 << lg) <= val ; ++ lg) { + + if (((val >> lg) & 1) == 1) { + + ret = dp[lg][ret] ; + + } + + } + + return ret ; + +} + + + +int main() { + + int n, m ; + + fscanf(in, "%d %d", &n, &m) ; + + for (int i = 1 ; i <= n ; ++ i) { + + fscanf(in, "%d", &dp[0][i]) ; + + } + + pre(n) ; + + int p, q ; + + for (int i = 1 ; i <= m ; ++ i) { + + fscanf(in, "%d %d", &q, &p) ; + + int ans = eval(p, q) ; + + fprintf(out, "%d\n", ans) ; + + } + +} diff --git a/ancestors/dp_log/5b73f6e6-139a-11eb-a5d7-309c234925a0.cpp b/ancestors/dp_log/5b73f6e6-139a-11eb-a5d7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a720c2f16e3ee8a3ec50523e6f626e1ba7d6fa9f --- /dev/null +++ b/ancestors/dp_log/5b73f6e6-139a-11eb-a5d7-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream f ("stramosi.in"); + +ofstream g ("stramosi.out"); + + + +int t[28][250049],i,j,n,m,v[29],q,query,p,afisat=-1,val,corespondent; + +int cauta(int q) + +{ + + int st=0,dr=18,mij=(st+dr)/2; + + while(st<=dr) + + { + + if(v[mij]<=q && v[mij+1]>q) + + { + + return mij; + + } + + else + + { + + if(v[mij]<=q) + + st=mij+1; + + else + + dr=mij-1; + + } + + mij=(st+dr)/2; + + } + + return -1; + +} + +int main() + +{ + + f>>n>>query; + + for(i=1;i<=n;i++) + + f>>t[0][i]; + + v[0]=1; + + for(i=1;i<=18;i++) + + { + + for(j=1;j<=n;j++) + + t[i][j]=t[i-1][t[i-1][j]]; + + v[i]=v[i-1]*2; + + } + + while(query) + + { + + f>>q>>p; + + query--; + + afisat=0; + + while(p!=0) + + { + + val=cauta(p); + + if(val==-1) + + g<<0<<'\n',p=1,afisat=1; + + else + + { + + p=p-v[val]; + + q=t[val][q]; + + } + + } + + g< + + + + +using namespace std; + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + +int t[18][250005], n, q, x, y; + +int main() + +{ + + fin >> n >> q; + + for(int i=1;i<=n;i++) + + fin >> t[0][i]; + + + + for(int j=1;j<=17;j++) + + for(int i=1;i<=n;i++) + + t[j][i]=t[j-1][t[j-1][i]]; + + + + for(int i=1;i<=q;i++) + + { + + fin >> x >> y; + + for(int j=17;j>=0;j--) + + if((1< + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n,m,baza; + +int stram[19][250001]; + + + +int main() + +{ + + in>>n>>m; + + baza=log2(n)+1; + + + + for(int j=1;j<=n;j++) + + in>>stram[1][j]; + + + + for(int i=2;i<=baza;i++) + + for(int j=1;j<=n;j++) + + stram[i][j]=stram[i-1][ stram[i-1][j] ]; + + + + for(int nod,p,i=1;i<=m;i++) + + { + + in>>nod>>p; + + + + for(int j=0;j<=baza;j++) + + if(p&(1< + + +using namespace std; + + + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n,query_count; + +int father[250001]; + +int stramosi[20][250001]; + + + + + + + +void read() + +{ + + in>>n>>query_count; + + for(int i=1; i<=n; i++) + + { + + in>>father[i]; + + } + +} + + + +void precompute() + +{ + + for(int i=1; i<=n; i++) + + { + + stramosi[0][i]=father[i]; + + } + + + + for(int k=1; k<=log2(n); k++) + + { + + for(int i=1; i<=n; i++) + + { + + stramosi[k][i]=stramosi[k-1][stramosi[k-1][i]]; + + } + + } + +} + + + + + + + +int n_father(int n,int q) + +{ + + while(q) + + { + + int l=q&(-q); + + int lg=log2(l); + + n=stramosi[lg][n]; + + q-=l; + + } + + return n; + +} + + + + + + + +int main() + +{ + + read(); + + precompute(); + + int a,b; + + for(int i=0;i>a>>b; + + out< + + +int d[18][250001]; + +int main(){ + + FILE *fin=fopen("stramosi.in","r"); + + FILE *fout=fopen("stramosi.out","w"); + + int n,m,i,j,q,p; + + + + fscanf(fin,"%d%d",&n,&m); + + for(i=1; i<=n; i++) + + fscanf(fin,"%d",&d[0][i]); + + for(i=1; i<18; i++) + + for(j=1; j<=n; j++) + + d[i][j]=d[i-1][d[i-1][j]]; + + for(i=0; i +using namespace std; + + + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n,query_count; + +int father[250001]; + +int stramosi[20][250001]; + + + + + + + +void read() + +{ + + in>>n>>query_count; + + for(int i=1; i<=n; i++) + + { + + in>>father[i]; + + } + +} + + + +void precompute() + +{ + + for(int i=1; i<=n; i++) + + { + + stramosi[0][i]=father[i]; + + } + + + + for(int k=1; k<=log2(n); k++) + + { + + for(int i=1; i<=n; i++) + + { + + stramosi[k][i]=stramosi[k-1][stramosi[k-1][i]]; + + } + + } + +} + + + + + + + +int n_father(int n,int q) + +{ + + while(q) + + { + + int l=q&(-q); + + int lg=log2(l); + + n=stramosi[lg][n]; + + q-=l; + + } + + return n; + +} + + + + + + + +int main() + +{ + + read(); + + precompute(); + + int a,b; + + for(int i=0;i>a>>b; + + out< +#include + + +FILE * f=fopen("stramosi.in","r"); + +FILE * g=fopen("stramosi.out","w"); + + + +using namespace std; + +int b[18][250001]; + + + +int main() + +{ + + int n,m,i,x,y,k=1; + + fscanf(f,"%d%d",&n,&m); + + for(i=1;i<=n;i++) + + {fscanf(f,"%d",&b[0][i]); + + + + } + + + +int s=0,j; + +do + +{ + + k=1<0) + + { s=0; + + k=1; + + while(k<=y) + + { + + s++; + + k=1< +#include + + +FILE * f=fopen("stramosi.in","r"); + +FILE * g=fopen("stramosi.out","w"); + + + +using namespace std; + +int b[18][250001]; + + + +int main() + +{ + + int n,m,i,x,y,k=1; + + fscanf(f,"%d%d",&n,&m); + + for(i=1;i<=n;i++) + + {fscanf(f,"%d",&b[0][i]); + + + + } + + + +int s=0,j; + +do + +{ + + k=1<0) + + { s=0; + + k=1; + + while(k<=y) + + { + + s++; + + k=1< +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m; + +int v[20][250003]; + + + +int main() + +{ + + int x, y; + + fin>>n>>m; + + + + for(int i=1; i<=n; i++) + + fin>>v[0][i]; + + + + for(int j=1; j<=17; j++){ + + for(int i=1; i<=n; i++) + + v[j][i] = v[j-1][v[j-1][i]]; + + } + + + + for(int i=0; i>x>>y; + + for(int j=17; j>=0; j--) + + { + + if((1< + + + + + + +int v[20][250000]; + + + +int main() + +{ + + std::ifstream fin("stramosi.in"); + + std::ofstream fout("stramosi.out"); + + + + int n, m; + + fin >> n >> m; + + + + for (int i = 1; i <= n; i++) + + fin >> v[0][i]; + + + + for(int i = 1; i<= 17; i++) + + for(int j = 1; j <= n; j++) + + v[i][j] = v[i - 1][v[i - 1][j]]; + + + + while (m--) { + + int p, q; + + fin >> q >> p; + + + + for(int i = 17; i >= 0; i--) + + if ((1 << i) <= p) { + + q = v[i][q]; + + p -= (1 << i); + + } + + + + fout << q << "\n"; + + } + + + + fin.close(), fout.close(); + + + + return 0; + +} diff --git a/ancestors/dp_log/5c70313e-139a-11eb-bdd6-309c234925a0.cpp b/ancestors/dp_log/5c70313e-139a-11eb-bdd6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ee2db5faad287e509b073281d022a416d4a6ef --- /dev/null +++ b/ancestors/dp_log/5c70313e-139a-11eb-bdd6-309c234925a0.cpp @@ -0,0 +1,75 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m; + +int v[20][250003]; + + + +int main() + +{ + + int x, y; + + fin>>n>>m; + + + + for(int i=1; i<=n; i++) + + fin>>v[0][i]; + + + + for(int j=1; j<=17; j++){ + + for(int i=1; i<=n; i++) + + v[j][i] = v[j-1][v[j-1][i]]; + + } + + + + for(int i=0; i>x>>y; + + for(int j=17; j>=0; j--) + + { + + if((1< +#include +#include + + + +using namespace std; + +ifstream f("stramosi.in"); +ofstream g("stramosi.out"); + +int n,m,table[20][250000 +5],q,p; + +int main() +{ + f>>n>>m; + for(int i=1;i<=n;i++){ + f>>table[0][i]; + } + for(int j=1;j<=17;j++){ + for(int i=1;i<=n;i++){ + table[j][i]=table[j-1][table[j-1][i]]; + } + } + while(m--){ + f>>q>>p; + for(int j=17;j>=0;j--){ + if((1< +using namespace std; + +const int NMAX = 250000; + +int t[NMAX + 5] , d[18][NMAX + 5] , log[NMAX + 5] , n; + +inline void dinamica() + +{ + + int i , j; + + for(j = 1 ; j <= n ; ++ j) + + d[0][j] = t[j]; + + for(i = 1 ; (1 << i) <= n ; ++ i) + + for(j = n ; j - (1 << i) >= 1 ; -- j) + + d[i][j] = d[i - 1][d[i - 1][j]]; + +} + +inline void gen_log() + +{ + + int i , b = 1; + + log[1] = 0; + + for(i = 2 ; i <= n ; i ++) + + if((b << 1) == i) + + { + + log[i] = log[b] + 1; + + b = (b << 1); + + } + + else + + log[i] = log[i - 1]; + +} + +int query(int x , int y) + +{ + + int l = log[y]; + + if((1 << l) == y) + + return d[l][x]; + + return query(d[l][x] , y ^ (1 << l)); + +} + +int main() + +{ + + freopen("stramosi.in" , "r" , stdin); + + freopen("stramosi.out" , "w" , stdout); + + int q , i , x , y; + + scanf("%d%d" , &n , &q); + + for(i = 1 ; i <= n ; ++ i) + + scanf("%d" , &t[i]); + + dinamica(); + + gen_log(); + + for(i = 1 ; i <= q ; ++ i) + + { + + scanf("%d%d" , &x , &y); + + printf("%d\n" , query(x , y)); + + } + + return 0; + +} diff --git a/ancestors/dp_log/5c9f909c-139a-11eb-b553-309c234925a0.cpp b/ancestors/dp_log/5c9f909c-139a-11eb-b553-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d403c5e71fb27e074b5f094400ff9a255df9cafb --- /dev/null +++ b/ancestors/dp_log/5c9f909c-139a-11eb-b553-309c234925a0.cpp @@ -0,0 +1,361 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +int N,Q; + + + +vector D[250005]; + +vector :: iterator it; + + + +int P[250005]; + +int LEVEL[250005]; + +int TABLE[250005][20]; + + + +FILE *_fin; + +int _in_loc; + +char _in_buff[4096]; + + + +FILE *_fout; + +int _out_loc; + +char _out_buff[50000]; + + + + + + + +void write_init(const char* name) + +{ + + _fout=fopen(name, "w"); + + _out_loc=0; + +} + + + +void write_ch(char ch) + +{ + + if (_out_loc==50000) + + { + + fwrite(_out_buff,1,50000,_fout); + + _out_loc=0; + + _out_buff[_out_loc++]=ch; + + } + + else + + { + + _out_buff[_out_loc++]=ch; + + } + +} + + + +void write_u32(unsigned int vu32) + +{ + + if (vu32<=9) + + { + + write_ch(vu32+'0'); + + } + + else + + { + + write_u32(vu32/10); + + write_ch(vu32%10+'0'); + + } + +} + + + +void write_appendix() + +{ + + fwrite(_out_buff,1,_out_loc,_fout); + + fclose(_fout); + +} + + + +void read_init(const char* nume) + +{ + + _fin=fopen(nume, "r"); + + _in_loc=4095; + +} + + + +char read_ch() + +{ + + ++_in_loc; + + if (_in_loc==4096) + + { + + _in_loc = 0; + + fread(_in_buff,1,4096,_fin); + + } + + return _in_buff[_in_loc]; + +} + + + +int read_u32() + +{ + + int u32 = 0; char c; + + while (!isdigit(c=read_ch()) && c!='-') + + { + + + + } + + int sgn=1; + + if (c=='-') + + { + + sgn=-1; + + } + + else + + { + + u32=c-'0'; + + } + + while (isdigit(c=read_ch())) + + { + + u32=u32*10+c-'0'; + + } + + return u32*sgn; + +} + + + +void df (int varf,int level) + +{ + + vector :: iterator it; + + LEVEL[varf]=level; + + for (it=D[varf].begin();it!=D[varf].end();++it) + + { + + df(*it,level+1); + + } + +} + + + +int walk (int varf,int dist) + +{ + + + + if (LEVEL[varf]<=dist) + + { + + return 0; + + } + + int p; + + for (p=18;p>=0;--p) + + { + + if((1<=LEVEL[v]) + + { + + TABLE[v][c]=0; + + } + + else + + { + + TABLE[v][c]=TABLE[TABLE[v][c-1]][c-1]; + + } + + } + + } + for (i=1;i<=Q;++i) + + { + + int v,d; + + v=read_u32(); + + d=read_u32(); + + int varf=walk(v,d); + + write_u32(varf); + + write_ch('\n'); + + } + + write_appendix(); + + return 0; + +} diff --git a/ancestors/dp_log/5cdd0e90-139a-11eb-8aa1-309c234925a0.cpp b/ancestors/dp_log/5cdd0e90-139a-11eb-8aa1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a1e9c44d0cc2e6e99b537c36c845ad0cfce818b --- /dev/null +++ b/ancestors/dp_log/5cdd0e90-139a-11eb-8aa1-309c234925a0.cpp @@ -0,0 +1,244 @@ +#include + + + + +using namespace std; + + + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + + + +int n, m; + +int dp[250005][20], lg2[250005]; + + + +void calcLog() + +{ + + for (int i = 1; i < 250005; i++) + + lg2[i] = lg2[i / 2] + 1; + +} + + + +void calcDp() + +{ + + for (int k = 1; k <= lg2[n]; k++) + + { + + for (int nod = 1; nod <= n; nod++) + + if (dp[nod][k - 1] == 0) + + dp[nod][k] = 0; + + else + + dp[nod][k] = dp[dp[nod][k - 1]][k - 1]; + + } + + + +} + + + +inline int p2(int x) + +{ + + return (1 << x); + +} + + + +int Query(int nod, int p) + +{ + + int nodAns = nod; + + + + for (int i = 0; i <= lg2[p]; i++) + + { + + if (p & p2(i)) + + nodAns = dp[nodAns][i]; + + } + + + + return nodAns; + +} + + + +int main() + +{ + + InParser fin("stramosi.in"); + + ofstream fout ("stramosi.out"); + + + + fin >> n >> m; + + for (int i = 1; i <= n; i++) + + fin >> dp[i][0]; + + + + calcLog(); + + calcDp(); + + + + for (int i = 1; i <= m; i++) + + { + + int q, p; + + fin >> q >> p; + + fout << Query(q, p) << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5cee429c-139a-11eb-91ed-309c234925a0.cpp b/ancestors/dp_log/5cee429c-139a-11eb-91ed-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0b1f203f22ee6a9676bd046a05aa0a9a552e598 --- /dev/null +++ b/ancestors/dp_log/5cee429c-139a-11eb-91ed-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + + + +using namespace std; + +ifstream f("stramosi.in"); +ofstream g("stramosi.out"); + +int n,m,table[20][250000 +5],q,p; + +int main() +{ + f>>n>>m; + for(int i=1;i<=n;i++){ + f>>table[0][i]; + } + for(int j=1;j<=17;j++){ + for(int i=1;i<=n;i++){ + table[j][i]=table[j-1][table[j-1][i]]; + } + } + while(m--){ + f>>q>>p; + for(int j=17;j>=0;j--){ + if((1< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; +ifstream cin ("stramosi.in"); + +ofstream cout ("stramosi.out"); + + + +static const int NMAX = 25e4+5; + + + +int n, m; + +int logar[NMAX]; + +int dp[20][NMAX]; + + + +int search (int member, int degree) { + + for ( int i = logar[n]; i >= 0; --i ) { + + if ( (1<>n>>m; + + for ( int i = 1; i <= n; ++i ) { + + int x; + + cin>>x; + + dp[0][i] = x; + + } + + + + for ( int i = 1; i <= logar[n]; ++i ) { + + for ( int j = 1; j <= n; ++j ) { + + dp[i][j] = dp[i-1][dp[i-1][j]]; + + } + + } + + + + for ( int i = 1; i <= m; ++i ) { + + int q, p; + + cin>>q>>p; + + + + cout< +#include + + +using namespace std; + + + +int dk[19][250005]; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + ios_base::sync_with_stdio(false); + + cin.tie(0); cout.tie(0); + + int n, m, q , p,i,j; + + cin >> n >> m; + + for(j = 1; j <= n; j++) + + cin >> dk[0][j]; + + for (i = 1; i < 19; i++) + + for (j = 1; j <= n; j++) + + dk[i][j] = dk[i - 1][dk[i - 1][j]]; + + while (m--) + + { + + cin >> q >> p; + + for (i = 0; i < 19; i++) + + if (p & (1 << i)) + + q = dk[i][q]; + + cout << q << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5d4a0588-139a-11eb-aa8d-309c234925a0.cpp b/ancestors/dp_log/5d4a0588-139a-11eb-aa8d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad2c6e17713aec8222025fd77ecfe24552c91381 --- /dev/null +++ b/ancestors/dp_log/5d4a0588-139a-11eb-aa8d-309c234925a0.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int n, m, x, y, k; +int tata[25][250005]; + +const int Lim = 10000000; +int u = Lim - 1; +char s[Lim]; + +void Next () { + if (++u == Lim) + fread(s, 1, Lim, stdin), u = 0; +} + +void Get (int &_x) { + for (; s[u] < '0' || s[u] > '9'; Next()); + for (_x = 0; s[u] >= '0' && s[u] <= '9'; Next()) + _x = _x * 10 + s[u] - '0'; +} + +int main() { + freopen("stramosi.in", "r", stdin); + freopen("stramosi.out", "w", stdout); + + Get(n); + Get(m); + for (int i = 1; i <= n; ++i) + Get(tata[1][i]); + bool gata = false; + for (int i = 1; !gata; ++i) { + gata = true; + for (int j = 1; j <= n; ++j) { + tata[i + 1][j] = tata[i][tata[i][j]]; + if (tata[i + 1][j]) + gata = false; + } + } + while (m--) { + Get(x); + Get(y); + for (int i = 0; i <= 19; ++i) + if (y & (1 << i)) + x = tata[i + 1][x]; + cout << x << '\n'; + } + return 0; +} diff --git a/ancestors/dp_log/5d5920be-139a-11eb-b8d2-309c234925a0.cpp b/ancestors/dp_log/5d5920be-139a-11eb-b8d2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f241fec6e92bbcf18931213e9efa64efd28704a4 --- /dev/null +++ b/ancestors/dp_log/5d5920be-139a-11eb-b8d2-309c234925a0.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +ifstream cin("stramosi.in"); +ofstream cout("stramosi.out"); + +int n,q,d[18][250001],a,b; + +int main() +{ + cin>>n>>q; + + for(int i=1;i<=n;i++) + cin>>d[0][i]; + for(int k=1;k<=17;k++){ + for(int i=1;i<=n;i++){ + d[k][i]=d[k-1][d[k-1][i]]; + } + } + while(q--){ + cin>>a>>b; + for(int i=17;i>=0;i--) + if ( (b & (1 < +#include +#include +#include +using namespace std; + + + +ifstream f ( "stramosi.in" ); + +ofstream g ( "stramosi.out" ); + + + +int d[17 + 1][250000 + 1], e[17 + 1]; + + + +int main() + +{ int n, q, i, org, lvl; + + f >> n >> q; + + for ( i = 1; i <= n; i++ ) + + f >> d[0][i]; + + for ( i = 1; i <= 17; i++){ + + for ( int j = 1; j <= n; j++ ) + + d[i][j] = d[i - 1][ d[i - 1][j] ]; + + } + + for ( i = 1; i <= q; i++ ){ + + f >> org >> lvl; + + for ( int j = 0; ( 1 << j ) <= lvl; j++ ) + + if ( ( ( lvl >> j ) & 1 ) == 1 ) + + org = d[j][org]; + + g << org << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5d78a306-139a-11eb-9d42-309c234925a0.cpp b/ancestors/dp_log/5d78a306-139a-11eb-9d42-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f244866464b9b0c5e2a8080c813127ac11b4580 --- /dev/null +++ b/ancestors/dp_log/5d78a306-139a-11eb-9d42-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n,m; + +int d[20][250001]; + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;i++) + + fin>>d[0][i]; + + for(int i=1;(1<>x>>nr; + + int t=x; + + while(nr!=0) + + { + + int j=1,p=0; + + while(j*2<=nr) + + j=j*2,p++; + + t=d[p][t]; + + nr-=j; + + } + + fout< + + +using namespace std; + + + +int dp[19][250005]; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + int n, q, nod, dist, i, j; + + cin >> n >> q; + + for(i=1; i<=n; i++) cin >> dp[0][i]; + + for(i=1; i<=18; i++) + + for(j=1; j<=n; j++) dp[i][j] = dp[i-1][dp[i-1][j]]; + + for(j=1; j<=q; j++) + + { cin >> nod >> dist; + + for(i=0; i<=18; i++) if(dist&(1< +#include + + +using namespace std; + + + +int strm[20][250005]; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + int n, m, i, j, p, q; + + cin>>n>>m; + + for (i=1; i<=n; i++) + + cin>>strm[0][i]; + + for (j=1; j<=18; j++) + + for (i=1; i<=n; i++) + + strm[j][i]=strm[j-1][strm[j-1][i]]; + + for (i=1; i<=m; i++) + + { + + cin>>p>>q; + + for (j=0; j<=18; j++) + + if(q&(1< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 25e4; + +const int LOG = 17; + + + +int dp[5 + LOG][5 + N]; + + + +int main() + +{ + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + int n, m; + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= n; i++) scanf("%d", &dp[0][i]); + + + + for(int i = 1; i <= LOG; i++) + + for(int j = 1; j <= n; j++) + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + + + while(m--){ + + int x, y; + + scanf("%d%d", &x, &y); + + for(int i = 0; (1 << i) <= y; i++) + + if(y & (1 << i)) + + x = dp[i][x]; + + printf("%d\n", x); + + } + + return 0; + +} diff --git a/ancestors/dp_log/5e04d57e-139a-11eb-af00-309c234925a0.cpp b/ancestors/dp_log/5e04d57e-139a-11eb-af00-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bdb548948efbbd4af2dc0c418dfc7156beedea8 --- /dev/null +++ b/ancestors/dp_log/5e04d57e-139a-11eb-af00-309c234925a0.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +int v[22][250005]; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + int n,m; + + cin>>n>>m; + + for(int i=1;i<=n;i++) + + cin>>v[0][i]; + + for(int mask=1;mask<=18;mask++) + + for(int i=1;i<=n;i++) + + v[mask][i]=v[mask-1][v[mask-1][i]]; + + for(int i=1;i<=m;i++) + + { + + int x,y,ans; + + cin>>x>>y; + + ans=x; + + for(int p=0;(1< +#include + + + + +using namespace std; + + + +int n, m; + +int dad[250000 + 5], log[250000 + 5]; + +int r[18][250000 + 5]; + + + +void gen_log() { + + int b = 1; + + + + log[1] = 0; + + for(int i = 2; i <= n; i++) + + if(2 * b == i) { + + log[i] = log[b] + 1; + + b *= 2; + + } + + else + + log[i] = log[b]; + +} + + + +void gen_rmq() { + + for(int i = 0; i <= log[n]; i++) + + for(int j = n; j - (1 << i) >= 1; j--) + + if(i == 0) + + r[i][j] = dad[j]; + + else + + r[i][j] = r[i - 1][r[i - 1][j]]; + +} + + + +int stramos(int q, int p) { + + int l = log[p]; + + + + if((1 << l) == p) + + return r[l][q]; + + return stramos(r[l][q], p ^ (1 << l)); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + int x, q, p; + + + + scanf("%d %d", &n, &m); + + gen_log(); + + for(int i = 1; i <= n; i++) { + + scanf("%d", &x); + + dad[i] = x; + + } + + gen_rmq(); + + + + for(int i = 1; i <= m; i++) { + + scanf("%d %d", &q, &p); + + printf("%d\n", stramos(q, p)); + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/5e257cde-139a-11eb-8c22-309c234925a0.cpp b/ancestors/dp_log/5e257cde-139a-11eb-8c22-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57a489df7a145903a05b7d04d8f95b419243924b --- /dev/null +++ b/ancestors/dp_log/5e257cde-139a-11eb-8c22-309c234925a0.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +int a[22][250010]; +int main() +{ + freopen("stramosi.in","r",stdin); + freopen("stramosi.out","w",stdout); + int n,m,i,j,nod,grad,mask; + scanf("%d%d",&n,&m); + for(i=1;i<=n;i++) + scanf("%d",&a[0][i]); + for(i=1;i<=19;i++) + for(j=1;j<=n;j++) + a[i][j]=a[i-1][a[i-1][j]]; + for(i=1;i<=m;i++) + { + scanf("%d%d",&nod,&grad); + while(grad) + { + mask=0; + while((1< + + +using namespace std; + + + +int v[22][250005]; + + + +int main() + +{ + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + int n,m; + + cin>>n>>m; + + for(int i=1;i<=n;i++) + + cin>>v[0][i]; + + for(int mask=1;mask<=18;mask++) + + for(int i=1;i<=n;i++) + + v[mask][i]=v[mask-1][v[mask-1][i]]; + + for(int i=1;i<=m;i++) + + { + + int x,y,ans; + + cin>>x>>y; + + ans=x; + + for(int pas=0;(1< + + + + + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, Q; + +int tt[250005]; + +int p, q; + + + +int dp[19][250005]; + + + +inline void read() + +{ + + fin >> n >> Q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + +} + + + +inline void build() + +{ + + int ok = 1; + + for (int k = 1; ok ; k++) + + { + + ok = 0; + + for (int i = 1; i <= n; i++) + + { + + dp[k][i] = dp[k-1][dp[k-1][i]]; + + if (dp[k][i]) + + ok = 1; + + } + + } + +} + + + +inline void solve() + +{ + + int k = 0; + + while (q != 0) + + { + + + + if (q % 2 == 1) + + p = dp[k][p]; + + k++; + + q /= 2; + + } + + fout << p << "\n"; + +} + + + +int main() + +{ + + read(); + + build(); + + while (Q--) + + { + + fin >> p >> q; + + solve(); + + } + + return 0; + +} diff --git a/ancestors/dp_log/5e7da278-139a-11eb-9104-309c234925a0.cpp b/ancestors/dp_log/5e7da278-139a-11eb-9104-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b413bc5d7344643757d707b8998d37e32326f12a --- /dev/null +++ b/ancestors/dp_log/5e7da278-139a-11eb-9104-309c234925a0.cpp @@ -0,0 +1,44 @@ +#include + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int S[22][250010]; + +int n,m,i,p,putere,a,b; + +int main(){ + + fin>>n>>m; + + for(i=1;i<=n;i++) + + fin>>S[0][i]; + + for(putere=1;putere<=18;putere++) + + for(i=1;i<=n;i++) + + S[putere][i]=S[putere-1][S[putere-1][i]]; + + for(i=1;i<=m;i++){ + + fin>>a>>b; + + for(p=0;(1< +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int d[19][250004]; + +int n,m,i,p,a,b,j; + +int main() + +{ + + fin>>n>>m; + + for(i=1;i<=n;) fin>>d[0][i++]; + + for(i=1;i<=18;i++) for(j=1;j<=n;j++) d[i][j]=d[i-1][d[i-1][j]]; + + for(;m--;){ + + fin>>a>>b; + + p=0; + + while(b){ + + b&(1< +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n,q,i,j,D[20][250010],a,b; + +int main(){ + + fin>>n>>q; + + for(i=1;i<=n;i++) + + fin>>D[0][i]; + + for(i=1;i<=19;i++){ + + for(j=1;j<=n;j++) + + D[i][j]=D[i-1][D[i-1][j]]; + + } + + for(;q--;){ + + fin>>a>>b; + + for(i=0;(1< + + + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n,m,i,j,p,q,d[25][250010]; + +int main() { + + fin>>n>>m; + + for (i=1;i<=n;i++) + + fin>>d[0][i]; + + for (i=1;i<=20;i++) + + for (j=1;j<=n;j++) + + d[i][j]=d[i-1][d[i-1][j]]; + + while (m--) { + + fin>>q>>p; + + for (i=0;(1< + + +using namespace std; + + + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + + + +int n, m, i, j, p, q, sol; + +int d[21][250005]; + + + +int main(){ + + fin >> n >> m; + + for (i=1; i<=n; i++){ + + fin >> d[0][i]; + + } + + for (i=1; i<=19; i++){ + + for (j=1; j<=n; j++){ + + d[i][j] = d[i-1][d[i-1][j]]; + + } + + } + + for (i=1; i<=m; i++){ + + fin >> q >> p; + + for (j=0; (1< +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int d[19][250004]; + +int n,m,i,p,a,b,j; + +int main() + +{ + + fin>>n>>m; + + for(i=1;i<=n;) fin>>d[0][i++]; + + for(i=0;i<18;i++) for(j=1;j<=n;) d[i+1][j]=d[i][d[i][j++]]; + + for(;m--;){ + +fin>>a>>b; + +p=0; + +while(b) b&(1< +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n,m,v[20][250001],i,j,p[20],x,cnt; + + + +int main(){ + + fin>>n>>m; + + + + for(i=1;i<=n;i++){ + + fin>>v[0][i]; + + } + + p[0]=1; + + + + for(cnt=1,j=2;j<=n;cnt++,j*=2){ + + p[cnt]=j; + + + + for(i=1;i<=n;i++) + + v[cnt][i]=v[cnt-1][ v[cnt-1][i] ]; + + } + + cnt--; + for(;m;m--){ + + fin>>i>>x; + + + + while(x){ + + j=0; + + while(2*p[j]<=x){ + + j++; + + } + + + + + + i=v[j][i]; + + x-=p[j]; + + } + + + + fout< +#include +using namespace std; + +ofstream g("stramosi.out"); + +class InputReader { + + private: + + static const int BUFF_SIZE = 100000; + + FILE *f; + + int bp; + + char buff[BUFF_SIZE]; + + inline void nxt() { + + if (++bp == BUFF_SIZE) { + + fread(buff, sizeof(char), BUFF_SIZE, f); + + bp = 0; + + } + + } + + public: + + InputReader (const char *file_name) { + + f = fopen(file_name, "r"); + + bp = BUFF_SIZE - 1; + + } + + void close() { + + fclose(f); + + } + + InputReader& operator >> (int &num) { + + num = 0; + + while (isdigit(buff[bp]) == 0&&buff[bp]!='-') + + nxt(); + + int semn=1; + + if(buff[bp]=='-') + + { + + semn=-1; + + nxt(); + + } + + while (isdigit(buff[bp])) { + + num = num * 10 + (buff[bp] - '0')*semn; + + nxt(); + + } + + return *this; + + } + +} f("stramosi.in"); + + + +vectora[250002]; + +int p[25],v[250002][25]; + + + +void dfs(int x) + +{ + + int i,l=a[x].size(),j=1,y=x; + + for(i=0;i>n>>m; + + p[0]=1; + + for(i=1;i<=20;i++) + + p[i]=p[i-1]*2; + + for(i=1;i<=n;i++) + + { + + f>>x; + + a[x].push_back(i); + + } + + x=a[0].size(); + + for(i=0;i>x>>y; + + g< +#include + + +using namespace std; + + + +const int NMAX = 25e4 + 5, MMAX = 20, BSIZE = (1 << 16); + + + +int N, Q, T[NMAX]; + + + +int Ancestor[MMAX][NMAX]; + + + +int pos = BSIZE - 1; + +char buff[BSIZE]; + + + +static inline char Char () + +{ + + ++pos; + + + + if(pos == BSIZE) + + { + + pos = 0; + + + + fread(buff, 1, BSIZE, stdin); + + } + + + + return buff[pos]; + +} + + + +static inline int Int () + +{ + + int r = 0; + + + + for(;;) + + { + + char ch = Char(); + + + + if(isdigit(ch)) + + { + + r = (ch - '0'); + + + + break; + + } + + } + + + + for(;;) + + { + + char ch = Char(); + + + + if(isdigit(ch)) + + r = r * 10 + (ch - '0'); + + else + + break; + + } + + + + return r; + +} + + + +static inline void Read () + +{ + + ios_base :: sync_with_stdio(false); + + cin.tie(NULL); + + + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + N = Int(); + + Q = Int(); + + + + for(int i = 1; i <= N; ++i) + + T[i] = Int(); + + + + return; + +} + + + +static inline void Precalculation () + +{ + + for(int i = 1; i <= N; ++i) + + if(T[i]) + + Ancestor[0][i] = T[i]; + + + + for(int p = 1; (1 << p) <= N; ++p) + + for(int i = 1; i <= N; ++i) + + Ancestor[p][i] = Ancestor[p - 1][Ancestor[p - 1][i]]; + + + + return; + +} + + + +static inline void Solve () + +{ + + while(Q--) + + { + + int Node = 0, cnt = 0; + + + + Node = Int(); + + cnt = Int(); + + + + if(cnt == 0) + + { + + printf("%d\n", Node); + + + + continue; + + } + + + + if(T[Node] == 0) + + { + + printf("0\n"); + + + + continue; + + } + + + + for(int p = 0; (1 << p) <= cnt; ++p) + + if(cnt & (1 << p)) + + Node = Ancestor[p][Node]; + + + + printf("%d\n", Node); + + } + + + + return; + +} + + + +int main() + +{ + + Read(); + + + + Precalculation(); + + + + Solve(); + + + + return 0; + +} diff --git a/ancestors/dp_log/5f50f2de-139a-11eb-ad66-309c234925a0.cpp b/ancestors/dp_log/5f50f2de-139a-11eb-ad66-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b6bea2a7104968831e0165eae8f0cd65890f2d2 --- /dev/null +++ b/ancestors/dp_log/5f50f2de-139a-11eb-ad66-309c234925a0.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int d[19][250004]; + +int n,m,i,p,a,b,j; + +int main() + +{ + + fin>>n>>m; + + + + for(i=1;i<=n;i++){ + + fin>>d[0][i]; + + } + + for(i=1;i<=18;i++){ + + for(j=1;j<=n;j++){ + + d[i][j]=d[i-1][d[i-1][j]]; + + } + + } + + for(i=1;i<=m;i++){ + + fin>>a>>b; + + p=0; + + while(b){ + + if(b&(1< +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m, k, t, i, j, d[25][250010], aux, p, nod; + + + + + + + +int main(){ + + fin>>n>>t; + + for(i=1; i<=n; i++){ + + fin>>d[0][i]; + + } + + for(i=1; i<=20; i++){ + + for(j=1; j<=n; j++){ + + aux=d[i-1][j]; + + d[i][j]=d[i-1][aux]; + + } + + } + + for(; t; t--){ + + fin>>nod>>p; + + for(i=0; (1< + + +using namespace std; + + + +ifstream in ("stramosi.in"); + +ofstream out ("stramosi.out"); + + + +int n, m; + + + +int stramosi[20][250007]; + + + +int q, p; + + + +int main() + +{ + + in >> n >> m; + + for ( register int i = 1 ; i <= n ; ++i ) + + in >> stramosi[0][i]; + + for ( register int i = 1 ; i <= 19 ; ++i ) + + for ( register int j = 1; j <= n ; ++j ) + + stramosi[i][j] = stramosi[i-1][stramosi[i-1][j]]; + + for ( register int querry = 1 ; querry <= m ; ++querry ) + + { + + in >> q >> p; + + for ( register int i = 0 ; (1< +#include + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream o("stramosi.out"); + + + +int n,m,t,d[20][250002],q,p,i,k,putere; + + + +int main() + +{ + + f >> n >> m; + + for(i=1;i<=n;++i) + + f >> d[0][i]; + + + + for(k=1;(1<> q >> p; + + + + putere=0; + + while(p>0){ + + if(p&1) + + q=d[putere][q]; + + ++putere; + + p>>=1; + + } + + o << q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5fbfe150-139a-11eb-8b37-309c234925a0.cpp b/ancestors/dp_log/5fbfe150-139a-11eb-8b37-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ce9bcbfbec07e44ff6f22d385eeaf79fbaeac73 --- /dev/null +++ b/ancestors/dp_log/5fbfe150-139a-11eb-8b37-309c234925a0.cpp @@ -0,0 +1,75 @@ +#include +#include + + + + +using namespace std; + + + + + +ifstream f("stramosi.in"); + +ofstream o("stramosi.out"); + + + +int n,m,t,d[20][250002],q,p,i,k,putere; + + + +int main() + +{ + + + + f >> n >> m; + + for(i=1;i<=n;++i) + + + + f >> d[0][i]; + + + + for(k=1;(1<> q >> p; + + + + putere=0; + + while(p>0){ + + if(p&1) + + q=d[putere][q]; + + ++putere; + + p>>=1; + + } + + o << q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/5fcfbfcc-139a-11eb-9d3b-309c234925a0.cpp b/ancestors/dp_log/5fcfbfcc-139a-11eb-9d3b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ec0447b44f176a01c15ce46302551812e7a00c6 --- /dev/null +++ b/ancestors/dp_log/5fcfbfcc-139a-11eb-9d3b-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int n,m,a[50][250001],log[250001],i,j,q,p; + +int main() + +{ + + f>>n>>m; + + for(i=1; i<=n; i++) + + { + + f>>a[0][i]; + + } + + for(i=1; (1<>q>>p; + + int ans; + + while(p>0) + + { + + int x=log[p]; + + q=a[x][q]; + + p-=(1< + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n,m,i,stramos,t,query,pas,k,tata[250100],d[20][250100]; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=n;i++) + + { + + f>>tata[i]; + + d[0][i]=tata[i]; + + } + + + + + + + + for(k=1;(1<>i>>t; + + + + stramos=i; + + + + + + pas=0; + + while(t) + + { + + if(t%2==1) + + { + + + + stramos=d[pas][stramos]; + + } + + pas++; + + t=t/2; + + } + + + + g< +#include +#include +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int NMax = 250000; + +vector G[NMax+5]; + + + +int N, T, A[20][NMax+5], lg[NMax+5]; + + + +void Read() { + + in >> N >> T; + + for (int i = 1; i <= N; i++) { + + in >> A[0][i]; + + } + + + + for (int i = 1; (1< 0) { + + int K = lg[P]; + + P -= 1<> Q >> P; + + out << Ancestor(Q, P) << '\n'; + + } + +} + + + +int main() { + + Read(); + + Solve(); + +} diff --git a/ancestors/dp_log/6031b5fa-139a-11eb-ad9c-309c234925a0.cpp b/ancestors/dp_log/6031b5fa-139a-11eb-ad9c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bc483b69e54b4599294b817e41290e6fdbd87b4 --- /dev/null +++ b/ancestors/dp_log/6031b5fa-139a-11eb-ad9c-309c234925a0.cpp @@ -0,0 +1,104 @@ +#include + + + + + + +using namespace std; + + + + + + + +ifstream fin( "stramosi.in" ); + + + +ofstream fout( "stramosi.out" ); + + + +int dp[250001][19]; + + + +int lg[250001]; + + + +int main() { + + + + int n, m, i, j, a, b; + + + + fin >> n >> m; + + + + for( i = 1; i <= n; ++i ) + + + + fin >> dp[i][0]; + + + + for( i = 1; i <= n; ++i ) + + + + for( j = 1; j <= 18; ++j ) + + + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + + + for( i = 2; i <= n; ++i ) + + + + lg[i] = lg[i >> 1] + 1; + + + + for( i = 1; i <= m; ++i ) { + + + + fin >> a >> b; + + + + for( j = 18; j >= 0; --j ) + + + + if( b & (1 << j) ) + + + + a = dp[a][j]; + + + + fout << a << "\n"; + + + + } + + + + return 0; + + + +} diff --git a/ancestors/dp_log/604257f6-139a-11eb-8fea-309c234925a0.cpp b/ancestors/dp_log/604257f6-139a-11eb-8fea-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c9bf9ca14217c6eb0fcfb7303a7ac57bd86054a --- /dev/null +++ b/ancestors/dp_log/604257f6-139a-11eb-8fea-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int n,m,i,j,x,y,k,afis,s,v[21][250005],pw[21],lg[250005]; + +char c[10]; + +int cit() + +{ + + int r=0,z=0; + + f>>c; + + while(c[r]>='0' && c[r]<='9') + + { + + z=z*10+c[r]-48; + + r++; + + } + + return z; + +} + +int main() + +{ + + f>>n>>m; + + pw[0]=1; + + for(i=1;i<=19;i++) + + pw[i]=pw[i-1]*2; + + k=0; + + for(i=1;i<=n;i++) + + if(pw[k]==i) + + { + + lg[i]=k; + + k++; + + } + + else + + lg[i]=k-1; + + + + for(i=1;i<=n;i++) + + { + + x=cit(); + + v[0][i]=x; + + } + + for(i=1;i<=n;i++) + + for(j=1;j<=lg[n];j++) + + { + + v[j][i]=v[j-1][v[j-1][i]]; + + if(v[j][i]==0) + + break ; + + } + + + + for(i=1;i<=m;i++) + + { + + x=cit(); y=cit(); + + s=0; + + while(s!=y) + + { + + k=lg[y-s]; + + s+=pw[k]; + + afis=v[k][x]; + + if(afis==0) + + break ; + + x=afis; + + } + + g< + + + + +using namespace std; + +const int NMAX = 250010; + + + +FILE *IN, *OUT; + + + +int N, M; + +int depth[NMAX], dp[19][NMAX]; + + + +int pos, sign, out; + +char f[131072], Out[131072], str[10]; + + + +inline void Read(int &nr){ + + sign = 0; + + nr = 0; + + while(f[pos] < '0' || f[pos] > '9'){ + + if(f[pos] == '-') sign = 1; + + pos++; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + while(f[pos] >= '0' && f[pos] <= '9'){ + + nr = 10 * nr + f[pos++] - '0'; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + if(sign) nr =- nr; + +} + + + +inline void Write_Ch(char ch){ + + Out[out++] = ch; + + if(out == 131072) + + fwrite(Out, 131072, 1, OUT), out = 0; + +} + + + +inline void Write_Int(int nr){ + + int x = 0; + + if(nr < 0) Write_Ch('-'), nr = -nr; + + do{ + + str[x++] = nr % 10 + '0'; + + nr /= 10; + + } while(nr); + + while(x > 0) + + Write_Ch(str[--x]); + +} + + + +void read(){ + + int x; + + Read(N); Read(M); + + for(int i = 1; i <= N; i++) + + Read(dp[0][i]); + +} + + + +int main(){ + + + + IN = fopen("stramosi.in", "r"); + + OUT = fopen("stramosi.out", "w"); + + + + read(); + + + + for(int i = 1; (1 << i) <= N; i++) + + for(int j = 1; j <= N; j++) + + dp[i][j] = dp[i - 1][dp[i - 1][j]]; + + + + int Q, P, p; + + for(int i = 1; i <= M; i++){ + + Read(Q); Read(P); p = 0; + + while(P != 0){ + + if(P % 2) Q = dp[p][Q]; + + P /= 2; p++; + + } + + Write_Int(Q); Write_Ch('\n'); + + } + + + + + + if(out > 0) fwrite(Out, 1, out, OUT); + + + + return 0; + +} diff --git a/ancestors/dp_log/606262e4-139a-11eb-8491-309c234925a0.cpp b/ancestors/dp_log/606262e4-139a-11eb-8491-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053e9cebdfb62eb8d7c1763a661a8c058c4faa85 --- /dev/null +++ b/ancestors/dp_log/606262e4-139a-11eb-8491-309c234925a0.cpp @@ -0,0 +1,52 @@ +#include + + +using namespace std; + + + +ifstream fin( "stramosi.in" ); + +ofstream fout( "stramosi.out" ); + +int dp[250001][19]; + +int lg[250001]; + +int main() { + + int n, m, i, j, a, b; + + fin >> n >> m; + + for( i = 1; i <= n; ++i ) + + fin >> dp[i][0]; + + for( i = 1; i <= n; ++i ) + + for( j = 1; j <= 18; ++j ) + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + for( i = 2; i <= n; ++i ) + + lg[i] = lg[i >> 1] + 1; + + for( i = 1; i <= m; ++i ) { + + fin >> a >> b; + + for( j = 18; j >= 0; --j ) + + if( b & (1 << j) ) + + a = dp[a][j]; + + fout << a << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/609d221c-139a-11eb-8922-309c234925a0.cpp b/ancestors/dp_log/609d221c-139a-11eb-8922-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdaeed87904c2f577adb171318f95f45a044b293 --- /dev/null +++ b/ancestors/dp_log/609d221c-139a-11eb-8922-309c234925a0.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int dim = 250005; + + + +int rmq[20][dim],lg[dim]; + +vector binar; + + + +int n,m; + + + +int main() + +{ + + in >> n >> m; + + for (int i=1; i<=n; i++) + + { + + in >> rmq[0][i]; + + } + + lg[1] = 0; + + for (int i=2; i<=n; i++) + + { + + lg[i] = lg[i/2] + 1; + + } + + for (int i=1; i<=18; i++) + + { + + for (int j=1; j<=n; j++) + + { + + rmq[i][j] = rmq[i-1][rmq[i-1][j]]; + + } + + } + + int p,q; + + int rez = 0,putere,j; + + for (int i=1; i<=m; i++) + + { + + in >> q >> p; + + binar.clear(); + + while (p != 0) + + { + + binar.push_back(p%2); + + p /= 2; + + + + } + + rez = q; + + for (j = 0; j +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int t[250010], n, m, nod, k, dp[20][250010]; + + + +int stramos(int nod, int k) { + + if(!nod || !k) + + return nod; + + else { + + int x = 0; + + while(1 << x <= k) + + x++; + + x--; + + return stramos(dp[x][nod], k-(1<> n >> m; + + for(int i = 1; i <= n; i++) + + f >> t[i]; + + + + for(int i = 1; i <= n; i++) { + + dp[0][i] = t[i]; + + for(int j = 1; (1 << j) <= 250000; j++) { + + dp[j][i] = dp[j-1][dp[j-1][i]]; + + if(!dp[j][i]) + + break; + + } + + } + + + + for(int i = 1; i <= m; i++) { + + f >> nod >> k; + + g << stramos(nod, k) << '\n'; + + } + + + +} diff --git a/ancestors/dp_log/60bdc94a-139a-11eb-bec0-309c234925a0.cpp b/ancestors/dp_log/60bdc94a-139a-11eb-bec0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37798c07dd1e1cbec2a30dfbb07481340cc58bf7 --- /dev/null +++ b/ancestors/dp_log/60bdc94a-139a-11eb-bec0-309c234925a0.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +int mat [250005][20]; + +int currentRow; + +vector vec; + + + +void MakeRow(int newRow, int index); + +int GetResult(int row, int index); + + + +int main() + +{ + + ifstream fin("stramosi.in"); + + ofstream fout("stramosi.out"); + + int n, m; + + fin >> n >> m; + + + + for (int i = 1; i <= n; i++) + + { + + int a; + + currentRow = i; + + fin >> a; + + MakeRow(a,0); + + } + + + + for (int i = 1; i <= m; i++) + + { + + int q, p; + + fin >> q >> p; + + bitset<20> bin(p); + + vec.clear(); + + for(int j = 0; j < 20; j++) + + { + + if(bin[j] == 1) + + { + + vec.push_back(j); + + + + } + + } + + + + fout << GetResult(q, 0) << "\n" ; + + } +} + + + +int GetResult(int row, int index) + +{ + + int newRow = mat[row][vec[index]]; + + if(index == vec.size() - 1) + + return newRow; + + return GetResult(newRow,index+1); + +} + + + +void MakeRow(int newRow, int index) + +{ + + mat[currentRow][index] = newRow; + + if(newRow!=0) + + MakeRow(mat[newRow][index], index+1); + +} diff --git a/ancestors/dp_log/60debef8-139a-11eb-9fd0-309c234925a0.cpp b/ancestors/dp_log/60debef8-139a-11eb-9fd0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8226aea76a379d57a5301098b4deadb5d833c3d --- /dev/null +++ b/ancestors/dp_log/60debef8-139a-11eb-9fd0-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include + + + + + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int A[19][250003]; + + + +int main() + +{ + + int n, m, i, p, q, p2 = 1, k; + + fin >> n >> m; + + for(i = 1; i <= n; i++) fin >> A[0][i]; + + for(k = 1; p2 <= n; k++) { + + for(i = 1; i <= n; i++) + + A[k][i] = A[k - 1][A[k-1][i]]; + + p2 *= 2; + + } + + + + for(i = 1; i <= m; i++){ + + fin >> q >> p; + + for(k = 18; k >= 0; k--) + + if(p & (1< +using namespace std; + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + + + +int n, m, dad, dp [2500003][19], P, Q; + +int main (){ + + fin >> n >> m; + + for (int i = 1; i <= n; i ++){ + + fin >> dad; + + dp [i][0] = dad; + + } + + for (int i = 1; i <= n; i ++) + + for (int k = 1; k <= 18; k ++) + + dp [i][k] = dp [dp [i][k - 1]][k - 1]; + + for (int i = 1; i <= m; i ++){ + + fin >> Q >> P; + + for (int k = 18; k >= 0; k --) + + if (P & (1 << k))Q = dp [Q][k]; + + fout << Q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/6109eda8-139a-11eb-ac1c-309c234925a0.cpp b/ancestors/dp_log/6109eda8-139a-11eb-ac1c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e4bafbe433afc6e9b44717c177810ec46a1fc59 --- /dev/null +++ b/ancestors/dp_log/6109eda8-139a-11eb-ac1c-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +ifstream f("stramosi.in"); +ofstream g("stramosi.out"); + +int t[250010], n, m, nod, k, dp[20][250010]; + +int stramos(int nod, int k) { + if(!nod || !k) + return nod; + else { + int x = 0; + while(1 << x <= k) + x++; + x--; + return stramos(dp[x][nod], k-(1<> n >> m; + for(int i = 1; i <= n; i++) + f >> t[i]; + + for(int i = 1; i <= n; i++) { + dp[0][i] = t[i]; + for(int j = 1; (1 << j) <= 250000; j++) { + dp[j][i] = dp[j-1][dp[j-1][i]]; + if(!dp[j][i]) + break; + } + } + + for(int i = 1; i <= m; i++) { + f >> nod >> k; + g << stramos(nod, k) << '\n'; + } + +} diff --git a/ancestors/dp_log/611cb250-139a-11eb-a334-309c234925a0.cpp b/ancestors/dp_log/611cb250-139a-11eb-a334-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65ec7c7656738d5abf89e2ad5f6b9a81acc8f620 --- /dev/null +++ b/ancestors/dp_log/611cb250-139a-11eb-a334-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +const int NMAX = 250005; + +const int LMAX = 30; + +int dp[LMAX][NMAX],lg[NMAX]; + +int n,q; + + + +int main(){ + + int i,x,y,j; + + f >> n >> q; + + for(i = 1 ; i <= n ; i++) + + f >> dp[0][i]; + + for(i = 2 ; i <= n ; i++) + + lg[i] = lg[i / 2] + 1; + + + + j = 1; + + while((1 << j) <= n){ + + for(i = 1 ; i <= n ; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + j++; + + } + + + + while(q--){ + + f >> x >> y; + + while(y && x != 0){ + + x = dp[lg[y]][x]; + + y = y - (1 << lg[y]); + + } + + g << x << "\n"; + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/616ddf74-139a-11eb-81d2-309c234925a0.cpp b/ancestors/dp_log/616ddf74-139a-11eb-81d2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa06d6dcbfffbfba954dd23d99b01ec5a2625eee --- /dev/null +++ b/ancestors/dp_log/616ddf74-139a-11eb-81d2-309c234925a0.cpp @@ -0,0 +1,292 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int S[18][250001]; + + + +using namespace std; + + + + + +class Input_Reader { + +private: + + FILE *__inputFile; + + long long fileSize, buffSize; + + unique_ptr < char[] > buffer; + + int Poz = 0; + + + + inline long long min(const long long &a, const long long &b) { return a < b ? a : b; } + + + + inline void CountBytes(const string &fileName) { + + FILE * fin = fopen(fileName.c_str(), "r"); + + fseek(fin, 0, SEEK_END); + + fileSize = ftell(fin); + + fclose(fin); + + } + + + + inline bool isFigure(char x) { + + return x >= '0' && x <= '9'; + + } + + + + inline void JumpOver() { + + while (!isFigure(buffer[Poz])) { + + if (Poz + 1 == 100000)fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile), buffSize = strlen(buffer.get()), Poz = -1; + + ++Poz; + + } + + } + + + + inline int Parse() { + + int number = 0; + + while (!isFigure(buffer[Poz]))JumpOver(); + + while (isFigure(buffer[Poz])) { + + number = number * 10 + buffer[Poz] - '0'; + + ++Poz; + + if (Poz == 100000)fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile), buffSize = strlen(buffer.get()), Poz = 0; + + } + + return number; + + } + + + +public: + + + + explicit Input_Reader(const string &str, const string &mode) { + + __inputFile = fopen(str.c_str(), mode.c_str()); + + + + buffer = move(make_unique(100000)); + + CountBytes(str); + + fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile); + + + + buffSize = strlen(buffer.get()); + + + + } + + + + Input_Reader &operator >> (int &value) { + + value = Parse(); + + return *this; + + } + + + +}; + + + + + +class Solver { + + + +public: + + + + explicit Solver(string&& in, string&& out) { + + + + FILE *fout = fopen(out.c_str(), "w"); + + Input_Reader fin{ in.c_str(), "r" }; + + + + int Q; + + vector ancestors; + + + + fin >> N >> Q; + + + + + + ancestors.resize(N + 1); + + for (int i = 1; i <= N; ++i) { + + fin >> ancestors[i]; + + } + + + + + + __process(ancestors); + + + + + + int P, X; + + while (Q--) { + + fin >> X >> P; + + fprintf(fout, "%d\n", __solve(X, P)); + + } + + } + + + +private: + + + + int __lg(int number) const { + + return floor(log2(number)); + + } + + + + void __process(const vector& ancestors) { + int log = __lg(N); + + for (int i = 1; i <= N; ++i) { + + + + S[0][i] = ancestors[i]; + + + + + + for (int j = 1; j <= log; ++j) { + + if (!S[j - 1][i]) { + + break; + + } + + S[j][i] = S[j - 1][S[j - 1][i]]; + + } + + } + + + + } + + + + int __solve(int Q, int P) { + + + + for (int j = 18; j >= 0 && Q; --j) { + + if (!(P & (1 << j))) { + + continue; + + } + + Q = S[j][Q]; + + } + + + + return Q; + + } + + + + int N; + +} solver{ + + "stramosi.in", + + "stramosi.out" + +}; + + + + + +int main() { + + return 0; + +} diff --git a/ancestors/dp_log/617dbdf6-139a-11eb-816a-309c234925a0.cpp b/ancestors/dp_log/617dbdf6-139a-11eb-816a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dacc5ae4875a4d9af733dd50ebba646be2d42b22 --- /dev/null +++ b/ancestors/dp_log/617dbdf6-139a-11eb-816a-309c234925a0.cpp @@ -0,0 +1,292 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int S[18][250001]; + + + +using namespace std; + + + + + +class Input_Reader { + +private: + + FILE *__inputFile; + + long long fileSize, buffSize; + + unique_ptr < char[] > buffer; + + int Poz = 0; + + + + inline long long min(const long long &a, const long long &b) { return a < b ? a : b; } + + + + inline void CountBytes(const string &fileName) { + + FILE * fin = fopen(fileName.c_str(), "r"); + + fseek(fin, 0, SEEK_END); + + fileSize = ftell(fin); + + fclose(fin); + + } + + + + inline bool isFigure(char x) { + + return x >= '0' && x <= '9'; + + } + + + + inline void JumpOver() { + + while (!isFigure(buffer[Poz])) { + + if (Poz + 1 == 100000)fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile), buffSize = strlen(buffer.get()), Poz = -1; + + ++Poz; + + } + + } + + + + inline int Parse() { + + int number = 0; + + while (!isFigure(buffer[Poz]))JumpOver(); + + while (isFigure(buffer[Poz])) { + + number = number * 10 + buffer[Poz] - '0'; + + ++Poz; + + if (Poz == 100000)fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile), buffSize = strlen(buffer.get()), Poz = 0; + + } + + return number; + + } + + + +public: + + + + explicit Input_Reader(const string &str, const string &mode) { + + __inputFile = fopen(str.c_str(), mode.c_str()); + + + + buffer = move(make_unique(100000)); + + CountBytes(str); + + fread(buffer.get(), sizeof(char), min(fileSize, 100000), __inputFile); + + + + buffSize = strlen(buffer.get()); + + + + } + + + + Input_Reader &operator >> (int &value) { + + value = Parse(); + + return *this; + + } + + + +}; + + + + + +class Solver { + + + +public: + + + + explicit Solver(string&& in, string&& out) { + + + + FILE *fout = fopen(out.c_str(), "w"); + + Input_Reader fin{ in.c_str(), "r" }; + + + + int Q; + + vector ancestors; + + + + fin >> N >> Q; + + + + + + ancestors.resize(N + 1); + + for (int i = 1; i <= N; ++i) { + + fin >> ancestors[i]; + + } + + + + + + __process(ancestors); + + + + + + int P, X; + + while (Q--) { + + fin >> X >> P; + + fprintf(fout, "%d\n", __solve(X, P)); + + } + + } + + + +private: + + + + int __lg(int number) const { + + return floor(log2(number)); + + } + + + + void __process(const vector& ancestors) { + int log = __lg(N); + + for (int i = 1; i <= N; ++i) { + + + + S[0][i] = ancestors[i]; + + + + + + for (int j = 1; j <= log; ++j) { + + if (!S[j - 1][i]) { + + break; + + } + + S[j][i] = S[j - 1][S[j - 1][i]]; + + } + + } + + + + } + + + + int __solve(int Q, int P) { + + + + for (int j = 18; j >= 0 && Q; --j) { + + if (!(P & (1 << j))) { + + continue; + + } + + Q = S[j][Q]; + + } + + + + return Q; + + } + + + + int N; + +}; + + + + + +int main() { + + Solver{ + + "stramosi.in", + + "stramosi.out" + + }; + +} diff --git a/ancestors/dp_log/61c33e6e-139a-11eb-9e6a-309c234925a0.cpp b/ancestors/dp_log/61c33e6e-139a-11eb-9e6a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac2b01bf5e9ce63d809ad1dbffeadb49fe326027 --- /dev/null +++ b/ancestors/dp_log/61c33e6e-139a-11eb-9e6a-309c234925a0.cpp @@ -0,0 +1,73 @@ +#include +#include + + + + + + +using namespace std; + +int tati[18 +1][250001]; + +int n,m; + +int main() + +{ + + ifstream f("stramosi.in"); + + ofstream g("stramosi.out"); + + f>>n>>m; + + for(int i=1;i<=n;i++) + + f>>tati[1][i]; + + for(int i=2;i<=18;i++) + + for(int j=1;j<=n;j++) + + tati[i][j]=tati[i-1][tati[i-1][j]]; + + long long q,p; + + for(int i=1;i<=m;i++) + + { + + f>>q>>p; + + int rez=q; + + int poz=1; + + for(long long j=1;p;j=(j<<1),poz++) + + { + + if(p&j) + + { + + p-=j; + + rez = tati[poz][rez]; + + } + + } + + g< + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n,m; + +int mat[20][250005]; + +int v[250005]; + +int lg2[250005]; + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + fin>>v[i]; + + mat[0][i]=v[i]; + + } + + for(int i=2;i<250005;++i) + + lg2[i]=lg2[i/2]+1; + + for(int i=1;i<=lg2[n];++i) + + for(int j=1;j<=n;++j) + + mat[i][j]=mat[i-1][mat[i-1][j]]; + + for(int i=1;i<=m;++i) + + { + + + + int x,y; + + fin>>x>>y; + + + + while(y) + + { + + x=mat[lg2[y]][x]; + + y=y-(1< +using namespace std; + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +int n, m, DP[22][300005]; + +int main() + +{ + + cin >> n >> m; + + for (int i = 1; i <= n; i++) + + cin >> DP[0][i]; + + for (int i = 1; i <= n; i++) + + for (int j = 1; j <= 20; j++) + + DP[j][i] = DP[j - 1][DP[j - 1][i]]; + + while (m--) + + { + + int x, y; + + cin >> x >> y; + + for (int i = 20; i >= 0; i--) + + if (y & (1 << i)) + + x = DP[i][x]; + + cout << x << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/61f27fe2-139a-11eb-8ca2-309c234925a0.cpp b/ancestors/dp_log/61f27fe2-139a-11eb-8ca2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77ca485c59a6c69006b9711f25458a0b5d14e0ba --- /dev/null +++ b/ancestors/dp_log/61f27fe2-139a-11eb-8ca2-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +vector g[250005]; + +int d[250005][22]; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + for (int i = 1; i <= n; i++) + + { + + int tata; + + fin >> tata; + + d[i][0] = tata; + + } + + for (int i = 1; i <= n; i++) + + for (int j = 1; j <= 18; j++) + + d[i][j] = d[d[i][j - 1]][j - 1]; + + for (int i = 1; i <= m; i++) + + { + + int p, q; + + fin >> q >> p; + + + + for (int j = 18; j >= 0; j--) + + if (p & (1 << j)) + + q = d[q][j]; + + fout << q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/621375b0-139a-11eb-a79f-309c234925a0.cpp b/ancestors/dp_log/621375b0-139a-11eb-a79f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4c424c89fb59ed6127a2b9c80b4652f560a88b4 --- /dev/null +++ b/ancestors/dp_log/621375b0-139a-11eb-a79f-309c234925a0.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +using namespace std; +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); +vector g[250005]; +int n,m,i,j,tata,d[250005][22],p,q; +int main() +{ + fin>>n>>m; + for(i=1;i<=n;i++) + { + fin>>tata; + d[i][0]=tata; + } + for(i=1;i<=n;i++) + for(j=1;j<=18;j++) + d[i][j]=d[d[i][j-1]][j-1]; + for(i=1;i<=m;i++) + { + fin>>q>>p; + + for(j=18;j>=0;j--) + if(p & (1< + + + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n,q,dp[20][(int)(25e4+5)],lg[(int)(25e4+5)]; + + + +void read(); + +void solve(); + +int lsb(int&); + + + +int main(){ + + read(); + + solve(); + + return 0; + +} + +void solve(){ + + int a,b,ancestor; + + while(q--){ + + fin>>a>>b; + + ancestor=a; + + + + while(b){ + + ancestor=dp[lg[lsb(b)]-1][ancestor]; + + b-=lsb(b); + + } + + fout<>n>>q; + + for(i=1;i<=n;++i) + + fin>>dp[0][i]; + + + + for(i=1;i<=n;++i) + + lg[i]=lg[i/2]+1; + + + + for(i=1;i<20;++i){ + + for(j=1;j<=n;++j){ + + dp[i][j]=dp[i-1][dp[i-1][j]]; + + } + + } + +} + +int lsb(int& x){ + + return x&(-x); + +} diff --git a/ancestors/dp_log/62578eae-139a-11eb-ae13-309c234925a0.cpp b/ancestors/dp_log/62578eae-139a-11eb-ae13-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d00b3b39428153ff2785fe4d1887308b56bf63b --- /dev/null +++ b/ancestors/dp_log/62578eae-139a-11eb-ae13-309c234925a0.cpp @@ -0,0 +1,80 @@ +#include +#include +#include + + +const int MAXN = 25e4 + 5; + +const int MAXLOG = log2(MAXN) + 1; + + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +int n,m,dp[MAXN][MAXLOG],tata[MAXN]; + + + + + +int query(int nod,int x){ + + for(int bit = 0; bit <= MAXLOG; bit++) + + if((x & (1 << bit)) > 0) + + nod = dp[nod][bit]; + + return nod; + +} + + + +int main() + +{ + + int querys; + + in>>n>>querys; + + for(int i = 1; i <= n; i++){ + + in>>tata[i]; + + dp[i][0] = tata[i]; + + } + + for(int i = 1; i <= n; i++){ + + for(int j = 1; j < MAXLOG; j++){ + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + } + + } + + for(int i = 1; i <= querys; i++){ + + int nod,plea_stramos; + + in>>nod>>plea_stramos; + + out< +#include + + + + +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + + + +int n, q; + +std::array, 19> dp; + + + +int main() { + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + + + for (int j = 1; (1 << j) <= n; j++) + + for (int i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + + + for (int it = 0; it < q; it++) { + + int q, p; fin >> q >> p; + + int ancestor = q; + + for (int i = 18; i >= 0; i--) + + if (p & (1 << i)) + + ancestor = dp[i][ancestor]; + + fout << ancestor << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/ancestors/dp_log/62769558-139a-11eb-b24d-309c234925a0.cpp b/ancestors/dp_log/62769558-139a-11eb-b24d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21219e5c4b23da02195101e59a66f4999d5fae19 --- /dev/null +++ b/ancestors/dp_log/62769558-139a-11eb-b24d-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include + + + + +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + + + +int n, q; + +int dp[19][250010]; + + + +int main() { + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + + + for (int j = 1; (1 << j) <= n; j++) + + for (int i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + + + for (int it = 0; it < q; it++) { + + int q, p; fin >> q >> p; + + int ancestor = q; + + for (int i = 18; i >= 0; i--) + + if (p & (1 << i)) + + ancestor = dp[i][ancestor]; + + fout << ancestor << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/ancestors/dp_log/6285f948-139a-11eb-816d-309c234925a0.cpp b/ancestors/dp_log/6285f948-139a-11eb-816d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21219e5c4b23da02195101e59a66f4999d5fae19 --- /dev/null +++ b/ancestors/dp_log/6285f948-139a-11eb-816d-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include + + + + +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + + + +int n, q; + +int dp[19][250010]; + + + +int main() { + + fin >> n >> q; + + for (int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + + + for (int j = 1; (1 << j) <= n; j++) + + for (int i = 1; i <= n; i++) + + dp[j][i] = dp[j - 1][dp[j - 1][i]]; + + + + for (int it = 0; it < q; it++) { + + int q, p; fin >> q >> p; + + int ancestor = q; + + for (int i = 18; i >= 0; i--) + + if (p & (1 << i)) + + ancestor = dp[i][ancestor]; + + fout << ancestor << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/ancestors/dp_log/62c19dc0-139a-11eb-993c-309c234925a0.cpp b/ancestors/dp_log/62c19dc0-139a-11eb-993c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f5232f28c4b0fe99d8bbe40581063320b62c052 --- /dev/null +++ b/ancestors/dp_log/62c19dc0-139a-11eb-993c-309c234925a0.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +int logs[250001],n,parent[18][250001]; + +void build(){ + + for(int i = 1;(1 << i) <= n;i++){ + + for(int j = 1;j <= n;j++){ + + parent[i][j] = parent[i - 1][parent[i-1][j]]; + + } + + } + +} + +int main() + +{ + + int i,m,x,y; + + cin >> n >> m; + + for(i = 1;i <= n;i++){ + + cin >> x; + + parent[0][i] = x; + + } + + build(); + + while(m--){ + + cin >> x >> y; + + int r = 0,pas = 18,rez = 0,ii = x; + + while(pas >= 0){ + + if(r + (1 << pas) <= y){ + + ii = parent[pas][ii]; + + r += (1 << pas); + + } + + pas--; + + } + + cout << ii << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/62de7480-139a-11eb-b817-309c234925a0.cpp b/ancestors/dp_log/62de7480-139a-11eb-b817-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61300ae51b1f9a9eae2bcab94c549149371de2a3 --- /dev/null +++ b/ancestors/dp_log/62de7480-139a-11eb-b817-309c234925a0.cpp @@ -0,0 +1,93 @@ +#include +#include +using namespace std; + + + +int tab[25][250005],n,m,nod,stramos; + + + +void gen() + +{ + + for(int i=1;i<=20;i++) + + for(int j=1;j<=n;j++) + + tab[i][j]=tab[i-1][tab[i-1][j]]; + +} + + + +void caut(int nod_curent,int de_urcat) + +{ + + int i=0; + + for(i=0; (1< +#include +#include +using namespace std; + + + +int father [ 20 + 1 ] [ 250000 + 1 ] ; + +int put [ 20 ] ; + + + +inline void preput () { + + int j ; + + put[0] = 1 ; + + for (j = 1 ; j < 20 ; j++ ) + + put[j] = put[j-1] * 2 ; + +} + + + +inline void precalc (int n) { + + int i, j; + + for (i = 1 ; i <= n ; i++ ) + + for (j = 1 ; j < 20 ; j++ ) + + father[j][i] = father[j-1][father[j-1][i]] ; + +} + + + +inline int fadar (int nod, int x ) { + + int j ; + + for (j = 0 ; put[j] <= x ; j++ ) { + + if ( ( put[j] & x ) != 0 ) + + nod = father[j][nod] ; + + } + + return nod ; + +} + + + +int main() { + + + + ifstream fin ("stramosi.in") ; + + ofstream fout ("stramosi.out" ) ; + + std::ios::sync_with_stdio(false) ; + + int n, m, i, v, x; + + fin >> n >> m ; + + for (i = 1 ; i <= n ; i++ ) + + fin >> father[0][i] ; + + preput () ; + + precalc(n) ; + + for (i = 1 ; i <= m ; i++) { + + fin >> v >> x ; + + fout << fadar(v,x) << '\n' ; + + } + + return 0; + +} diff --git a/ancestors/dp_log/62fbe8d0-139a-11eb-8af1-309c234925a0.cpp b/ancestors/dp_log/62fbe8d0-139a-11eb-8af1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7d000fb568a5f584e6dd6012de864808ca13f35 --- /dev/null +++ b/ancestors/dp_log/62fbe8d0-139a-11eb-8af1-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int n,q,i,x,nrstr,nr,stramos[18][250000],j,pas,solst,sol; + +int main() + +{ + + f>>n>>q; + + for (i=1;i<=n;i++) + + { + + f>>x; + + stramos[0][i]=x; + + } + + for (i=1;(1<>nr>>nrstr;pas=18;sol=0;solst=nr; + + while (pas>=0) + + { + + if (sol+(1< + + +using namespace std; + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +int logs[250001],n,parent[18][250001]; + +void build(){ + + for(int i = 1;(1 << i) <= n;i++){ + + for(int j = 1;j <= n;j++){ + + parent[i][j] = parent[i - 1][parent[i-1][j]]; + + } + + } + +} + +int main() + +{ + + int i,m,x,y; + + cin >> n >> m; + + for(i = 1;i <= n;i++){ + + cin >> x; + + parent[0][i] = x; + + } + + build(); + + while(m--){ + + cin >> x >> y; + + int r = 0,pas = 18,rez = 0,ii = x; + + while(pas >= 0){ + + if(r + (1 << pas) <= y){ + + ii = parent[pas][ii]; + + r += (1 << pas); + + } + + pas--; + + } + + cout << ii << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/63183a34-139a-11eb-ae45-309c234925a0.cpp b/ancestors/dp_log/63183a34-139a-11eb-ae45-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af83749387927b78e853c3808e0642a4ba2ed73d --- /dev/null +++ b/ancestors/dp_log/63183a34-139a-11eb-ae45-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int Nmax=250002; + +int n,m; + +int puteri[20],lg[Nmax],dp[19][Nmax]; + + + +void Build() + +{ + + + + for(int i=2;i>n>>m; + + for(int i=1;i<=n;i++) + + fin>>dp[0][i]; + + + + Build(); + + + + for(int i=1;i<=18;i++) + + for(int j=1;j<=n;j++) + + dp[i][j]=dp[i-1][dp[i-1][j]]; + + + + int x,y,sol; + + while(m--) + + { + + fin>>x>>y; + + + + sol=x; + + + + while(y) + + { + + x=lg[y]; + + sol=dp[x][sol]; + + y-=puteri[x]; + + } + + + + fout< + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,rmq[19][250005],lev,nod; + + + +int main() + +{ + + f>>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + rmq[0][i]=a; + + } + + for(int i=1;(1<>nod>>lev; + + int org=lev; + + while(lev) + + { + + lev&=lev-1; + + int dif=log2(org-lev); + + nod=rmq[dif][nod]; + + org=lev; + + } + + g< +#include +#include +using namespace std; + +FILE *f,*g; + +int rmq[25][250002]; + +int log2[250002]; + +int n,m; + +void read() + +{ + + fscanf(f,"%d %d",&n,&m); + + for(int i=1; i<=n; i++) + + fscanf(f,"%d",&rmq[0][i]); + +} + +void logarithm() + +{ + + for(int i=2;i<=n;i++) + + log2[i]=log2[i/2]+1; + +} + +void build() + +{ + + for(int i=1;i<=log2[n];i++) + + for(int j=1; j<=n; j++) + + rmq[i][j]=rmq[i-1][rmq[i-1][j]]; + +} + +int querry(int nod, int lev) + +{ + + for(int i=0; (1< + + +using namespace std; + +const int MAXN = 3 * 1e5; + +int father[35][MAXN]; + +int fth(int x, int node){ + + for(int i = 0 ; (1 << i) <= x; ++i){ + + if(x & (1 << i)) node = father[i][node]; + + } + + return node; + +} + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int main(){ + + int n, m; + + fin >> n >> m; + + for(int i = 1; i <= n; ++i){ + + int x; + + fin >> x; + + father[0][i] = x; + + } + + for(int node = 1; node <= n; ++node){ + + for(int i = 1; i <= 29; ++i){ + + father[i][node] = father[i - 1][father[i - 1][node]]; + + } + + } + + for(int i = 1; i <= m; ++i){ + + int p, q; + + fin >> q >> p; + + fout << fth(p, q) << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/63b37d94-139a-11eb-892b-309c234925a0.cpp b/ancestors/dp_log/63b37d94-139a-11eb-892b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9867c2b77bf07e868464ade48684d52cec82aac --- /dev/null +++ b/ancestors/dp_log/63b37d94-139a-11eb-892b-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include + + + + +using namespace std; + +int RMQ[250010][25]; + + + + + +int N,M; + +ifstream f("stramosi.in");ofstream g("stramosi.out"); + +void Read() + +{ + + int i; + + f>>N>>M; + + for(i=1;i<=N;++i)f>>RMQ[i][0]; + +} + +void BuildMatrix() + +{ + + int i,j,X; + + for(i=1;i<=N;++i) + + for(j=1;j<18;++j) + + RMQ[i][j]=RMQ[RMQ[i][j-1]][j-1]; + +} + +int Search(int Node,int P) + +{ + + int i; + + for(i=0;(1<>X>>Poz; + + g< + + +using namespace std; + +int RMQ[250010][25]; + + + + + +int N,M; + +ifstream f("stramosi.in");ofstream g("stramosi.out"); + +void Read() + +{ + + int i; + + f>>N>>M; + + for(i=1;i<=N;++i)f>>RMQ[i][0]; + +} + +void BuildMatrix() + +{ + + int i,j,X; + + for(i=1;i<=N;++i) + + for(j=1;j<18;++j) + + RMQ[i][j]=RMQ[RMQ[i][j-1]][j-1]; + +} + +int Search(int Node,int P) + +{ + + int i; + + for(i=0;(1<>X>>Poz; + + g< +#include + + + + +const char* IN_FILE_NAME = "stramosi.in"; + +const char* OUT_FILE_NAME = "stramosi.out"; + + + +using namespace std; +const int MAX_N = 250000 + 10; + +const int MAX_M = 350000 + 10; + +const int MAX_LEVELS = 19; + + + +typedef int ancsT[MAX_LEVELS][MAX_N]; + + + +ancsT gAncs; + + + +void ReadFirstLevel(ifstream& In, int N) + +{ + + for (int i = 1; i <= N; ++i) + + + + In >> gAncs[0][i]; + +} + + + +void CompleteRemainingLevels(int N) + +{ + + for (int j = 1; j < MAX_LEVELS; ++j) + + for (int i = 0; i <= N; ++i) + + + + gAncs[j][i] = gAncs[j - 1][gAncs[j-1][i]]; + +} + + + +void ResolveQueries(ifstream& In, ofstream& Out, int N, int M) + +{ + + int q, p; + + for (int m = 0; m < M; ++m) + + { + + In >> q; + + In >> p; + + + + int anc = q; + + + + for (int level = 0; (p > 0) && (anc != 0); p >>= 1, ++level) + + { + + if ((p & 1) == 0) + + continue; + + + + + + anc = gAncs[level][anc]; + + } + + + + Out << anc << "\n"; + + } + +} + + + +int main(int argc, char*argv[]) + +{ + + ifstream inF{ IN_FILE_NAME }; + + ofstream outF{ OUT_FILE_NAME }; + + + + int n, m; + + + + inF >> n; + + inF >> m; + + + + ReadFirstLevel(inF, n); + + CompleteRemainingLevels(n); + + ResolveQueries(inF, outF, n, m); + + + + return 0; + +} diff --git a/ancestors/dp_log/64433a5c-139a-11eb-8fbd-309c234925a0.cpp b/ancestors/dp_log/64433a5c-139a-11eb-8fbd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dfa3c95428e6e69c8f05cdfb87f4bd191145bac --- /dev/null +++ b/ancestors/dp_log/64433a5c-139a-11eb-8fbd-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + + + +int tati[250005]; + +int dp[250005][30]; + + + +int main() + +{ + + ifstream fin("stramosi.in"); + + ofstream fout("stramosi.out"); + + int n, m; + + fin >> n >> m; + + for(int i = 1; i <= n; ++i){ + + fin >> tati[i]; + + dp[i][0] = tati[i]; + + } + + for(int i = 1; i <= n; ++i){ + + for(int j = 1; j <= 20; ++j) + + dp[i][j] = dp[dp[i][j - 1]][j - 1]; + + } + + while(m){ + + int x, y; + + fin >> x >> y; + + int curent = x; + + for(int p = 20; p >= 0; p--){ + + if((1 << p) <= y){ + + curent = dp[curent][p]; + + y -= (1 << p); + + } + + } + + fout << curent << "\n"; + + m--; + + } + + return 0; + +} diff --git a/ancestors/dp_log/64832992-139a-11eb-a4a2-309c234925a0.cpp b/ancestors/dp_log/64832992-139a-11eb-a4a2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4891aca538c1eaceca1068f4dd9b99812ccc6f7 --- /dev/null +++ b/ancestors/dp_log/64832992-139a-11eb-a4a2-309c234925a0.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +typedef long long ll; + +typedef pair pii; + +typedef pair pll; + +typedef vector vi; + +typedef vector vii; + +typedef vector vl; + +typedef vector vll; + +typedef queue qi; + +typedef queue qii; + +typedef queue ql; + +typedef queue qll; + + + +const int INF = 0x3f3f3f3f; + +const int MOD = 1e9 + 9; + +const double EPSILON = 1e-10; + +const int NMAX = 2.5e5 + 5; + +const int LMAX = 22; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m; + +int stramosi[LMAX][NMAX]; + + + +void init() + +{ + + for (int i = 1; i < LMAX; ++i) + + for (int j = 1; j <= n; ++j) + + stramosi[i][j] = stramosi[i - 1][stramosi[i - 1][j]]; + +} + + + +int query(int q, int p) + +{ + + int i = 0; + + + + while (p) + + { + + if (p & 1) q = stramosi[i][q]; + + p >>= 1; + + ++i; + + } + + + + return q; + +} + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= n; ++i) + + fin >> stramosi[0][i]; + +} + + + +int main() + +{ + + read(); + + init(); + + + + while (m--) + + { + + int q, p; + + fin >> q >> p; + + fout << query(q, p) << '\n'; + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/6491a888-139a-11eb-ab5d-309c234925a0.cpp b/ancestors/dp_log/6491a888-139a-11eb-ab5d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..534c83508825b4fb6fdbefbd0195b7e95a0f9a18 --- /dev/null +++ b/ancestors/dp_log/6491a888-139a-11eb-ab5d-309c234925a0.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); +int n,m; +int s[250020][19]; + +int query(int x,int g) +{ + for(int i=0;(1<>n>>m; + for(int i=1;i<=n;i++) + fin>>s[i][0]; + int lg2=32-__builtin_clz(n-1); + cout<>q>>g; + fout< + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int n,m; + +int dp[250005][19]; + +int lg(int x) + +{ + + int nr=0; + + while(x) + + { + + nr++; + + x/=2; + + } + + return nr-1; + +} + +int query(int poz, int nod) + +{ + + for(int i=0; (1<> n >> m; + + for(int i=1; i<=n; i++) + + { + + in >> dp[i][0]; + + } + + int p=1; + + for(int i=1; i<=n; i++) + + { + + for(int j=1; j<18; j++) + + { + + dp[i][j]=dp[dp[i][j-1]][j-1]; + + } + + } + + for(int i=1; i<=m; i++) + + { + + int x,poz; + + in >> x >> poz; + + out << query(poz,x) << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/64b11a14-139a-11eb-a1ff-309c234925a0.cpp b/ancestors/dp_log/64b11a14-139a-11eb-a1ff-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de1b6c6ee26eac0299c27bc6792016b111603697 --- /dev/null +++ b/ancestors/dp_log/64b11a14-139a-11eb-a1ff-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +int str[250005],dp[250005][18]; + +int N,M,Q,P,p,nr; + +int main(){ + + + + cin>>N>>M; + + + + for(int i=1;i<=N;i++) + + cin>>dp[i][0]; + + + + for(int i=1;i<=N;i++) + + for(int k=1;k<=17;k++) + + if(dp[i][k-1]) + + dp[i][k]=dp[dp[i][k-1]][k-1]; + + else break; + + + + for(int i=1;i<=M;i++){ + + + + cin>>Q>>P; + + + + for(p=1,nr=0;p<=P;p<<=1,++nr) + + if(P&p) + + Q=dp[Q][nr]; + + + + cout< +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +const int MAX = 250005; + +const int MAX_2POW = 20; + + + +int n, m; + +int ancestor[MAX][MAX_2POW + 3]; + + + +void Read() + +{ + + f >> n >> m; + + for(int i = 1; i <= n; ++i) + + f >> ancestor[i][0]; + +} + + + +void Construct_Ancestors() + +{ + + for(int i = 1; i <= n; ++i) + + for(int j = 1; j <= MAX_2POW; ++j) + + if(ancestor[i][j - 1] && (1 << j) <= n) + + ancestor[i][j] = ancestor[ancestor[i][j - 1]][j - 1]; + +} + + + +int Query(int p, int q) + +{ + + int exp = 0, mask = 1; + + + + while(mask <= p) + + { + + if(mask & p) + + q = ancestor[q][exp]; + + ++exp; + + mask <<= 1; + + } + + + + return q; + +} + + + +void Queries() + +{ + + int p, q; + + + + while(m--) + + { + + f >> q >> p; + + g << Query(p, q) << "\n"; + + } + +} + + + +int main() + +{ + + Read(); + + Construct_Ancestors(); + + Queries(); + + return 0; + +} diff --git a/ancestors/dp_log/64cf2926-139a-11eb-8bfd-309c234925a0.cpp b/ancestors/dp_log/64cf2926-139a-11eb-8bfd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e57debbd09c169a8ecc0bd19edeac07d46090449 --- /dev/null +++ b/ancestors/dp_log/64cf2926-139a-11eb-8bfd-309c234925a0.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,Q,P; + +int x,v[25][250005],rez; + +int main() + +{ + + f>>N>>M; + + for(int i=1; i<=N; i++) + + { + + f>>x; + + v[0][i]=x; + + } + + for(int i=1; i<=18; i++) + + for(int j=1; j<=N; j++) + + if(v[i-1][j] and (1<>Q>>P; + + for(int j=0; j<=18; j++) + + { + + if ((1< +#include +#include + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, q, x; + +int log2[250005]; + +int s[20][250005]; + + + +void build() + +{ + + for (int i = 1; (1<> n >> q; + + for (int i = 1; i <= n; i++) + + { + + f >> x; + + s[0][i]=x; + + } + + build(); + + while(q--) + + { + + int x, y; + + f >> x >> y; + + for (int i = 0; i <= 20; i++) + + if(y&(1< +using namespace std; + +ifstream cin("stramosi.in"); + +ofstream cout("stramosi.out"); + +int str[250005],dp[250005][18]; + +int N,M,Q,P,p,nr; + +int main(){ + + + + cin>>N>>M; + + + + for(int i=1;i<=N;i++) + + cin>>dp[i][0]; + + + + for(int i=1;i<=N;i++) + + for(int k=1;k<=17;k++) + + if(dp[i][k-1]) + + dp[i][k]=dp[dp[i][k-1]][k-1]; + + else break; + + + + for(int i=1;i<=M;i++){ + + + + cin>>Q>>P; + + + + for(p=1,nr=0;p<=P;p<<=1,++nr) + + if(P&p) + + Q=dp[Q][nr]; + + + + cout< + + +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +const int NMAX=250000; + +const int logmax=20; + +int str2put[NMAX+5][logmax+5]; +int main() + +{ + + int n, m, sol, i, j, exp, put2, q, p; + + fin>>n>>m; + + for(i=1;i<=n;i++) + + fin>>str2put[i][0]; + + for(i=1;i<=n;i++) + + { + + for(j=1;j<=20;j++) + + { + + if(str2put[i][j-1]!=0 and (1<>q>>p; + + put2=1; + + exp=0; + + while(put2<=p) + + { + + if(put2&p) + + q=str2put[q][exp]; + + put2=put2*2; + + exp++; + + } + + fout< + + +using namespace std; + + + +ifstream fin ("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int N_MAX = 250000 + 5; + + + +int dp[50][N_MAX], n, m; + + + +int stramosi(int i, int n){ + + int pow = 0; + + int ans = i; + + while(n){ + + if(n%2) + + ans = dp[pow][ans]; + + pow ++; + + n /= 2; + + } + + return ans; + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 1; i<=n; ++i) + + fin >> dp[0][i]; + + for(int pow = 1; pow <= 30; ++pow) + + for(int i = 1; i <=n; ++i) + + dp[pow][i] = dp[pow-1][dp[pow-1][i]]; + + + + while(m--){ + + int a, b; + + fin >> a >> b; + + fout << stramosi(a, b) << "\n"; + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/655b6bdc-139a-11eb-ba43-309c234925a0.cpp b/ancestors/dp_log/655b6bdc-139a-11eb-ba43-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f23fe8caf6d8f8533914be043392fae5de782347 --- /dev/null +++ b/ancestors/dp_log/655b6bdc-139a-11eb-ba43-309c234925a0.cpp @@ -0,0 +1,208 @@ +#include + + +using namespace std; + + + +int v[19][250005]; + +char chs[1000000 + 1]; + +int chs_poz = 1000000; + + + +int parse_int() + +{ + + int x = 0; + + if(chs_poz >= 1000000) + + { + + chs_poz = 0; + + int num; + + num = fread(chs, 1, 1000000, stdin); + + } + + + + if(chs[chs_poz] < '0' || chs[chs_poz] > '9') + + { + + do + + { + + chs_poz++; + + if(chs_poz >= 1000000) + + { + + chs_poz = 0; + + int num; + + num = fread(chs, 1, 1000000, stdin); + + } + + } + + while(chs[chs_poz] < '0' || chs[chs_poz] > '9'); + + } + + + + while(chs[chs_poz] >= '0' && chs[chs_poz] <= '9') + + { + + x = x*10 + chs[chs_poz] - '0'; + + chs_poz++; + + if(chs_poz >= 1000000) + + { + + chs_poz = 0; + + int num; + + num = fread(chs, 1, 1000000, stdin); + + } + + } + + return x; + +} + + + +int get_lsb(int n) + +{ + + return (n&(n-1))^n; + +} + + + +int get_logpow2(int n) + +{ + + int p = 0; + + while(n > 1) + + { + + p++; + + n = n>>1; + + } + + return p; + +} + + + +int main() + +{ + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + int n, m, i, j, k, p, q, powk; + + n = parse_int(); + + m = parse_int(); + + for(i = 1; i <= n; i++) + + { + + v[0][i] = parse_int(); + + } + + + + for(k = 1; 1<= 0) + + { + + p_crt = get_lsb(p); + + p -= p_crt; + + pas_pow = get_logpow2(p_crt); + + anc = v[pas_pow][anc]; + + if(anc == 0 || p == 0) + + break; + + } + + printf("%d\n", anc); + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/656b9888-139a-11eb-8e00-309c234925a0.cpp b/ancestors/dp_log/656b9888-139a-11eb-8e00-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32c85557b52e216d92712c5b616fca56b95c54de --- /dev/null +++ b/ancestors/dp_log/656b9888-139a-11eb-8e00-309c234925a0.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, a[21][250001]; + + + +int main() + +{ + + f >> n >> m; + + for(int i = 1; i <= n; ++i) + + { + + int x; f >> x; + + a[0][i] = x; + + } + + for(int i = 1; (1<> x >> y; + + for (int i = 0; i <= 18; ++i) { + + if ((1< +#include + + +using namespace std; + +ifstream fin ("stramosi.in"); + +ofstream fout ("stramosi.out"); + + + +int N, M, logn, i, j, k, a[ 18 ][ 250001 ], Q, P; + + + +int main() + +{ + + fin >> N >> M; + + for ( i = 1; i <= N; i++ ) + + { + + fin >> a[ 0 ][ i ]; + + } + + for ( logn = 1, k = 0; logn < N; logn <<= 1, k++ ); + + logn >>= 1; + + k--; + + for ( i = 1; i <= k; i++ ) + + { + + for ( j = 1; j <= N; j++ ) + + { + + a[ i ][ j ] = a[ i - 1 ][ a[ i - 1 ][ j ] ]; + + } + + } + + for ( i = 1; i <= M; i++ ) + + { + + fin >> Q >> P; + + while ( P > 0 ) + + { + + for ( logn = 1, k = 0; logn <= P; logn <<= 1, k++ ); + + logn >>= 1; + + k--; + + Q = a[ k ][ Q ]; + + P -= logn; + + } + + fout << Q << '\n'; + + } + + return 0; + +} diff --git a/ancestors/dp_log/65998648-139a-11eb-bc29-309c234925a0.cpp b/ancestors/dp_log/65998648-139a-11eb-bc29-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b9435c18b9db5c7a0430905ac36270912efe7ecf --- /dev/null +++ b/ancestors/dp_log/65998648-139a-11eb-bc29-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int dp[250001][19],n,m; + +int main() + +{ + + f>>n>>m; + + for(int i=1;i<=n;++i) + + { + + f>>dp[i][0]; + + } + + + + for(int i=1;i<=n;++i) + + for(int j=1;j<=18;++j) + + { + + dp[i][j]=dp[dp[i][j-1]][j-1]; + + if(dp[i][j]==0) + + break; + + } + + for(int i=1;i<=m;++i) + + { + + int x,y,nr=0,node; + + f>>x>>y; + + node=x; + + while(y) + + { + + if(y&1) + + { + + node=dp[node][nr]; + + } + + nr++; + + y>>=1; + + } + + g< + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 25e4 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ancestor[20][NMax], lg[NMax]; + +inline int getAncestor(int node, int pth, int N) { + if (pth >= N) { + return 0; + } + + for (int exp = lg[pth]; exp >= 0; --exp) { + if ((1<> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + ancestor[0][i] = dad; + lg[i] = lg[i>>1] + 1; + } + + for (int exp = 1; (1<> q >> p; + + cout << getAncestor(q, p, N) << '\n'; + } + + return 0; +} diff --git a/ancestors/dp_log/660842f8-139a-11eb-a43e-309c234925a0.cpp b/ancestors/dp_log/660842f8-139a-11eb-a43e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..102967a216f0ab11a19b42c17d4fb8586902102f --- /dev/null +++ b/ancestors/dp_log/660842f8-139a-11eb-a43e-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using namespace std; + +ifstream f("stramosi.in"); +ofstream g("stramosi.out"); + +int n,m, parinti[250005][20]; + +int main() { + ios::sync_with_stdio(false); + int nod, nivel_neam; + f >> n >> m; + + for (int i=1; i<=n; i++) + { + f >> parinti[i][0]; + } + for (int i=1; i<=n; i++) + { + for (int j=1; j<=18; ++j) + { + + parinti[i][j]=parinti[parinti[i][j-1]][j-1]; + } + } + for (int query=1; query<=m; query++) + { + int nr=0; + f >> nod >> nivel_neam; + while (nivel_neam) + { + if (nivel_neam&1) + { + nod=parinti[nod][nr]; + + } + nr++; + nivel_neam>>=1; + } + g << nod << '\n'; + } + return 0; +} diff --git a/ancestors/dp_log/6aea6c88-139a-11eb-9836-309c234925a0.cpp b/ancestors/dp_log/6aea6c88-139a-11eb-9836-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..062fb811eac5e5e3518081c1f918707f3200791a --- /dev/null +++ b/ancestors/dp_log/6aea6c88-139a-11eb-9836-309c234925a0.cpp @@ -0,0 +1,48 @@ +#include +#include + +using namespace std; + +int n,m,nr,x,p[250003][20]; +ofstream fout("stramosi.out"); + +void stramos() +{ + int t=x,putere=0; + while(nr) + { + if(nr%2==0) + { + nr/=2; + putere++; + } + else + { + nr--; + t=p[t][putere]; + } + } + fout<>n>>m; + for(int i=1;i<=n;i++) + fin>>p[i][0]; + for(int i=1;i<=n;i++) + for(int j=1;j<19;j++) + p[i][j]=p[p[i][j-1]][j-1]; + for(int i=0;i>x>>nr; + stramos(); + } +} + +int main() +{ + citire(); + return 0; +} diff --git a/ancestors/dp_log/6b003e90-139a-11eb-9277-309c234925a0.cpp b/ancestors/dp_log/6b003e90-139a-11eb-9277-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ebdbd3809798c89e450d1323425ce5f7fa7b6be --- /dev/null +++ b/ancestors/dp_log/6b003e90-139a-11eb-9277-309c234925a0.cpp @@ -0,0 +1,81 @@ +#include +#include + + +using namespace std; + + + +const int MAXN = 250010; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n, m, matrix[MAXN][20]; + + + +void fillMatrix() { + + for (int i = 1; i <= n; i++) + + for (int j = 1; j <= 18; j++) + + matrix[i][j] = matrix[matrix[i][j - 1]][j - 1]; + +} + + + +void solve() { + + int nr, x, ancestor, ind; + + fin >> n >> m; + + for (int i =1 ; i <= n; i++) { + + fin >> matrix[i][0]; + + } + + fillMatrix(); + + for (int i = 1; i <= m; i++) { + + fin >> x >> nr; + + ancestor = x; + + ind = 0; + + while(nr) { + + if (nr % 2) { + + ancestor = matrix[ancestor][ind]; + + } + + ind++; + + nr = (nr >> 1); + + } + + fout << ancestor << "\n"; + + } + +} + + + +int main() { + + solve(); + + return 0; + +} diff --git a/ancestors/dp_log/6b25a0c6-139a-11eb-ac75-309c234925a0.cpp b/ancestors/dp_log/6b25a0c6-139a-11eb-ac75-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e41aa72379de05718dc524f84bae9bd33c9fac27 --- /dev/null +++ b/ancestors/dp_log/6b25a0c6-139a-11eb-ac75-309c234925a0.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +ifstream in("stramosi.in"); +ofstream out("stramosi.out"); + +const int N =250005; + +int n, m; +int p[25][N]; +int afis; + +void citire(){ + in >> n >> m; + + for (int i = 1; i <= n; ++i) { + in >> p[0][i]; + } +} + +void init(){ + for (int i = 1; (1<> q >> r; + for (int i = 0; i <= 18; ++i) { + if ((1< +#include + +using namespace std; + +ifstream in("stramosi.in"); +ofstream out("stramosi.out"); + +const int N =250005; + +int n, m; +int p[25][N]; +int afis; + +void citire(){ + in >> n >> m; + + for (int i = 1; i <= n; ++i) { + in >> p[0][i]; + } +} + +void init(){ + for (int i = 1; (1<> q >> r; + for (int i = 0; i <= 25; ++i) { + if ((1< +#include + + +using namespace std; + + + +const int MAXN = 250010; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int n, m, matrix[MAXN][20]; + + + +void fillMatrix() { + + for (int i = 1; i <= n; i++) + + for (int j = 1; j <= 18; j++) + + matrix[i][j] = matrix[matrix[i][j - 1]][j - 1]; + +} + + + +void solve() { + + int nr, x, ancestor, ind; + + fin >> n >> m; + + for (int i =1 ; i <= n; i++) { + + fin >> matrix[i][0]; + + } + + fillMatrix(); + + for (int i = 1; i <= m; i++) { + + fin >> x >> nr; + + ancestor = x; + + ind = 0; + + while(nr) { + + if (nr % 2) { + + ancestor = matrix[ancestor][ind]; + + } + + ind++; + + nr = (nr >> 1); + + } + + fout << ancestor << "\n"; + + } + +} + + + +int main() { + + solve(); + + return 0; + +} diff --git a/ancestors/dp_log/6bb514b4-139a-11eb-b7f0-309c234925a0.cpp b/ancestors/dp_log/6bb514b4-139a-11eb-b7f0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ecbed0ca64b3e4205b115bff941ba29ed01ae96 --- /dev/null +++ b/ancestors/dp_log/6bb514b4-139a-11eb-b7f0-309c234925a0.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +int n,m; + +int dp[250010][25]; + +int rs; + +int b[250010]; + + + +int main() { + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + + + cin>>n>>m; + + for (int i=1; i<=n; ++i) { + + int x; cin>>x; + + b[i]=x; + + } + + + + for (int i=1; i<=n; ++i) dp[i][0]=b[i]; + + for (int i=1; i<=n; ++i) { + + for (int j=1; (1<>a>>p; rs=a; + + for (int i=0; i<=20; ++i) { + + if ((1< + + +using namespace std; + + + +int n,m; + +int dp[250010][25]; + +int rs; + +int b[250010]; + + + +int main() { + + ifstream cin("stramosi.in"); + + ofstream cout("stramosi.out"); + + + + cin>>n>>m; + + for (int i=1; i<=n; ++i) { + + int x; cin>>x; + + b[i]=x; + + } + + + + for (int i=1; i<=n; ++i) dp[i][0]=b[i]; + + for (int i=1; i<=n; ++i) { + + for (int j=1; (1<>a>>p; rs=a; + + for (int i=0; i<=20; ++i) { + + if ((1< + + +using namespace std; + +int n , m , i , dp[40][250005] , p , t , x ,str; + +int main() + +{ + + ifstream fin("stramosi.in"); + + ofstream fout("stramosi.out"); + + fin >> n >> m; + + for(i = 1; i <= n; i++) + + { + + fin >> dp[0][i]; + + } + + for(p = 1; (1 << p) <= n; p++) + + { + + for(i = 1; i <= n; i++) + + { + + dp[p][i] = dp[p-1][dp[p-1][i]]; + + } + + } + + for(t = 1; t <= m; t++) + + { + + fin >> x >> str; + + for(i = 0; i <= 25; i++) + + if(str & (1 << i))x = dp[i][x]; + + fout << x << "\n"; + + } + + return 0; + +} diff --git a/ancestors/dp_log/6c029808-139a-11eb-8f7e-309c234925a0.cpp b/ancestors/dp_log/6c029808-139a-11eb-8f7e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..265494dfb270f27d750edef0f16737e3252c618d --- /dev/null +++ b/ancestors/dp_log/6c029808-139a-11eb-8f7e-309c234925a0.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m, dp[50][250005]; + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 1; i <= n; i++) + + fin >> dp[0][i]; + + + + for(int i = 1; (1<> q >> p; + + + + int x = q; + + for(int i = 0; (1< + + +int g[25][250001], n, m, i, j, k; + + + +main() { + + std::ifstream o{"stramosi.in"}; + + + + o >> n >> m; + + + + for (i = 1; i <= n; ++i) + + o >> g[0][i]; + + + + for (k = 1; (1 << k) <= n; ++k) + + for (j = 1; j <= n; ++j) + + g[k][j] = g[k - 1][g[k - 1][j]]; + + + + std::ofstream s{"stramosi.out"}; + + + + while (m--) { + + k=0; + + o >> i >> j; + + + + while (j) { + + if (j & 1) + + i = g[k][i]; + + + + ++k; + + j >>= 1; + + } + + + + s << i << '\n'; + + } + +} diff --git a/ancestors/dp_log/6c6aa946-139a-11eb-b77d-309c234925a0.cpp b/ancestors/dp_log/6c6aa946-139a-11eb-b77d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d8edf59ab76657f527ef82197cce37c0a735ecd --- /dev/null +++ b/ancestors/dp_log/6c6aa946-139a-11eb-b77d-309c234925a0.cpp @@ -0,0 +1,64 @@ +#include + + +int mat[25][250001], n, m, i, j, k; + + + +int main() { + + std::ifstream is{"stramosi.in"}; + + + + is >> n >> m; + + + + for (i = 1; i <= n; ++i) + + is >> mat[0][i]; + + + + for (k = 1; (1 << k) <= n; ++k) + + for (j = 1; j <= n; ++j) + + mat[k][j] = mat[k - 1][mat[k - 1][j]]; + + + + std::ofstream os{"stramosi.out"}; + + + + while (m--) { + + k=0; + + is >> i >> j; + + + + while (j) { + + if (j & 1) + + i = mat[k][i]; + + + + ++k; + + j >>= 1; + + } + + + + os << i << '\n'; + + } + +} diff --git a/ancestors/dp_log/6c78b6e8-139a-11eb-8870-309c234925a0.cpp b/ancestors/dp_log/6c78b6e8-139a-11eb-8870-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a31ac53b15bb64aa04754d090f4d11685c3cfc --- /dev/null +++ b/ancestors/dp_log/6c78b6e8-139a-11eb-8870-309c234925a0.cpp @@ -0,0 +1,64 @@ +#include + + +int mat[25][250001], n, m, i, j, k; + + + +int main() { + + std::ifstream is{"stramosi.in"}; + + + + is >> n >> m; + + + + for (i = 1; i <= n; ++i) + + is >> mat[0][i]; + + + + for (k = 1; (1 << k) <= n; ++k) + + for (j = 1; j <= n; ++j) + + mat[k][j] = mat[k - 1][mat[k - 1][j]]; + + + + std::ofstream os{"stramosi.out"}; + + + + while (m--) { + + int q, p, exp{}; + + is >> q >> p; + + + + while (p) { + + if (p & 1) + + q = mat[exp][q]; + + + + ++exp; + + p >>= 1; + + } + + + + os << q << '\n'; + + } + +} diff --git a/ancestors/dp_log/6c897bf8-139a-11eb-92c2-309c234925a0.cpp b/ancestors/dp_log/6c897bf8-139a-11eb-92c2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..072959a1346279585a8c149086f6874cfac3208d --- /dev/null +++ b/ancestors/dp_log/6c897bf8-139a-11eb-92c2-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include + + +int mat[25][250001]; + + + +int main() { + + std::ifstream is{"stramosi.in"}; + + + + int n, m; + + is >> n >> m; + + + + for (auto i = 1; i <= n; ++i) + + is >> mat[0][i]; + + + + for (auto k = 1; (1 << k) <= n; ++k) + + for (auto j = 1; j <= n; ++j) + + mat[k][j] = mat[k - 1][mat[k - 1][j]]; + + + + std::ofstream os{"stramosi.out"}; + + + + while (m--) { + + int q, p, exp{}; + + is >> q >> p; + + + + while (p) { + + if (p & 1) + + q = mat[exp][q]; + + + + ++exp; + + p >>= 1; + + } + + + + os << q << '\n'; + + } + +} diff --git a/ancestors/dp_log/6c990d6c-139a-11eb-bc0e-309c234925a0.cpp b/ancestors/dp_log/6c990d6c-139a-11eb-bc0e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca522793ce51712100d9703bff53027397566013 --- /dev/null +++ b/ancestors/dp_log/6c990d6c-139a-11eb-bc0e-309c234925a0.cpp @@ -0,0 +1,73 @@ +#include +#include + + +std::ifstream in("stramosi.in"); + +std::ofstream out("stramosi.out"); + + + +using namespace std; + +int t[20][250005]; + +int numar,nrst,n,m; + + + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=n;i++) + + { + + in>>t[0][i]; + + } + + for(int i=1;(1<>numar>>nrst; + + for(int k=0;k<=20;k++) + + { + + if((1< +int g[25][250001], n, m, i, j, k;main(){ + + std::ifstream o{"stramosi.in"}; + + o>>n>>m; + + for(i=1;i<=n;++i) + + o>>g[0][i]; + + for(k=1;(1<>i>>j; + + while(j){ + + if(j&1)i=g[k][i]; + + ++k;j>>=1; + + } + + s< + + +int g[25][250001], n, m, i, j, k; + + + +int main() { + + std::ifstream o{"stramosi.in"}; + + + + o >> n >> m; + + + + for (i = 1; i <= n; ++i) + + o >> g[0][i]; + + + + for (k = 1; (1 << k) <= n; ++k) + + for (j = 1; j <= n; ++j) + + g[k][j] = g[k - 1][g[k - 1][j]]; + + + + std::ofstream s{"stramosi.out"}; + + + + while (m--) { + + k=0; + + o >> i >> j; + + + + while (j) { + + if (j & 1) + + i = g[k][i]; + + + + ++k; + + j >>= 1; + + } + + + + s << i << '\n'; + + } + +} diff --git a/ancestors/dp_log/6cca135a-139a-11eb-866f-309c234925a0.cpp b/ancestors/dp_log/6cca135a-139a-11eb-866f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17fe144e2c5905eb4620165c292f1a424ab2f181 --- /dev/null +++ b/ancestors/dp_log/6cca135a-139a-11eb-866f-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include + + +int mat[25][250001], i, j, k; + + + +int main() { + + std::ifstream is{"stramosi.in"}; + + + + int n, m; + + is >> n >> m; + + + + for (i = 1; i <= n; ++i) + + is >> mat[0][i]; + + + + for (k = 1; (1 << k) <= n; ++k) + + for (j = 1; j <= n; ++j) + + mat[k][j] = mat[k - 1][mat[k - 1][j]]; + + + + std::ofstream os{"stramosi.out"}; + + + + while (m--) { + + int q, p, exp{}; + + is >> q >> p; + + + + while (p) { + + if (p & 1) + + q = mat[exp][q]; + + + + ++exp; + + p >>= 1; + + } + + + + os << q << '\n'; + + } + +} diff --git a/ancestors/dp_log/6cda4000-139a-11eb-bcee-309c234925a0.cpp b/ancestors/dp_log/6cda4000-139a-11eb-bcee-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be3316d444ff2b0842c3d151f2fc9598accf58e2 --- /dev/null +++ b/ancestors/dp_log/6cda4000-139a-11eb-bcee-309c234925a0.cpp @@ -0,0 +1,77 @@ +#include +#include + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m; + + + +int stra[20][250005]; + + + +int main() + +{ + + f>>n>>m; + + + + for(int i = 1; i <= n; i++) + + { + + f>>stra[0][i]; + + } + + + + for(int i = 1; (1<>nod>>p; + + for(int pu=0; pu <= 20; pu++) + + { + + if((1< +#include + + + + +using namespace std; + +ifstream fi("stramosi.in"); + +ofstream fo("stramosi.out"); + + + +int N, M; + + + +int table[25][250000]; + + + +int main() + +{ + + fi >> N >> M; + + + + for(int i = 1; i <= N; ++i) + + { + + fi >> table[1][i]; + + } + + + + for(int i = 2; (1 << (i-1)) <= N; ++i) + + for(int j = 1; j <= N; ++j) + + table[i][j] = table[i-1][table[i-1][j]]; + + + + for(int i = 1; i <= M; ++i) + + { + + int node, ancestor; + + + + fi >> node >> ancestor; + + int currentAncestor = node; + + for(int j = 0; j < 21; ++j) + + { + + if((1 << j) & ancestor) + + currentAncestor = table[j+1][currentAncestor]; + + } + + fo << currentAncestor << "\n"; + + } + +} diff --git a/ancestors/dp_log/6d28d568-139a-11eb-890c-309c234925a0.cpp b/ancestors/dp_log/6d28d568-139a-11eb-890c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c165e6d03504f12b8695d1474117007b21b84a5e --- /dev/null +++ b/ancestors/dp_log/6d28d568-139a-11eb-890c-309c234925a0.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + + + +ofstream fo("stramosi.out"); + +ifstream fi("stramosi.in"); + + + +int n,m,str[23][250000],a,b; + + + +int main() + +{ + + fi>>n>>m; + + + + for(int i=1;i<=n;i++) + + { + + fi>>a; + + str[0][i]=a; + + } + + + + + + for(int i=1;(1<>a>>b; + + + + for(int i=0;i<=20;i++) + + { + + if( (1< + + +int mat[25][250001]; + + + +int main() { + + std::ifstream is{"stramosi.in"}; + + + + int n, m; + + is >> n >> m; + + + + for (auto i = 1; i <= n; ++i) + + is >> mat[0][i]; + + + + for (auto k = 1; (1 << k) <= n; ++k) + + for (auto j = 1; j <= n; ++j) + + mat[k][j] = mat[k - 1][mat[k - 1][j]]; + + + + std::ofstream os{"stramosi.out"}; + + + + for (auto i = 1; i <= m; ++i) { + + int q, p, exp{}; + + is >> q >> p; + + + + while (p) { + + if (p & 1) + + q = mat[exp][q]; + + + + ++exp; + + p >>= 1; + + } + + + + os << q << '\n'; + + } + +} diff --git a/ancestors/dp_log/6d4780be-139a-11eb-8448-309c234925a0.cpp b/ancestors/dp_log/6d4780be-139a-11eb-8448-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af4fbcecf9f7c6c53150e18e94347c815f26038e --- /dev/null +++ b/ancestors/dp_log/6d4780be-139a-11eb-8448-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m, p, q; + +int TT[25][250001]; + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i) fin>>TT[0][i]; + + for(int k=1; (1<>q>>p; + + int exp=0; + + while(p>0) + + { + + if(p%2==1) q=TT[exp][q]; + + ++exp; + + p=p/2; + + } + + fout< +using namespace std ; + +ifstream f ( "stramosi.in" ) ; + +ofstream g ( "stramosi.out" ) ; + +const int NR = 250001 ; + +int rmq [ 20 ][ NR ] ; + + + +int main () + +{ + + int nod , nr , q , k , n , lg ; f >> n >> q ; + + int i , j ; + + for ( i = 1 ; i <= n ; ++ i ) f >> rmq [ 0 ][ i ] ; + + for ( k = 1 ; ( 1 << k ) <= n ; k ++ ) ; + + for ( i = 1 ; ( 1 << i ) <= n ; ++ i ) + + for ( j = 1 ; j <= n ; j ++ ) + + rmq [ i ][ j ] = rmq [ i - 1 ][ rmq [ i - 1 ][ j ] ] ; + + while ( q -- ) + + { + + f >> nod >> nr ; + + lg = k ; + + while ( nod && lg -- ) if ( ( 1 << lg ) & nr ) nod = rmq [ lg ][ nod ] ; + + g << nod << "\n" ; + + } + + f.close() ; + + g.close() ; + + return 0 ; + +} diff --git a/ancestors/dp_log/6d68031c-139a-11eb-9740-309c234925a0.cpp b/ancestors/dp_log/6d68031c-139a-11eb-9740-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b726d05e0024cec0084e81faa645b15a58310a53 --- /dev/null +++ b/ancestors/dp_log/6d68031c-139a-11eb-9740-309c234925a0.cpp @@ -0,0 +1,26 @@ +#include +std::ifstream cin("stramosi.in"); +std::ofstream cout("stramosi.out"); + + +int rmq[19][250001]; +int n,T; + +int main() +{ + int nod,cnt,k,dist; + cin>>n>>T; + for(k=1;(1<>rmq[0][i]; + for(int i=1;(1<>nod>>cnt; + dist=k; + while(nod&&dist--) + if(cnt&(1< +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int a[20][250010],N,M,Q,P,Lg[250010]; + +int main() + +{ + + int i,j; + + fin>>N>>M; + + for(i=1; i<=N; i++) + + { + + fin>>a[0][i]; + + } + + for(i=1; i<=18; i++) + + { + + for(j=1; j<=N; j++) + + { + + a[i][j]=a[i-1][a[i-1][j]]; + + } + + } + + for(j=2 ; j<=N ; j++) + + Lg[j] = Lg[j/2]+1; + + for(i=1; i<=M; i++) + + { + + fin>>Q>>P; + + while(P!=0) + + { + + Q=a[Lg[P]][Q]; + + P=P-(1< + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int n, m, p, q; + +int TT[25][250001]; + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i) fin>>TT[0][i]; + + for(int k=1; (1<>q>>p; + + int exp=0; + + while(p>0) + + { + + if(p%2==1) q=TT[exp][q]; + + ++exp; + + p=p/2; + + } + + fout< + +using namespace std; + +ifstream in("stramosi.in"); +ofstream out("stramosi.out"); + +const int DIM = 3e5 + 7; + +int ancestor[30][DIM]; + +int main() +{ + int n, m; + in >> n >> m; + + for(int i = 1; i <= n; i++) + { + in >> ancestor[0][i]; + } + + for(int i = 1; (1 << i) <= n; i++) + for(int j = 1; j <= n; j++) + ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]]; + + while(m--) + { + int p, q; + in >> q >> p; + + while(p != 0 && q != 0) + { + int k = p & (-p); + + p -= k; + + int t = log2(k); + if(1 << t != k) + t++; + + q = ancestor[t][q]; + } + + out << q << '\n'; + } +} diff --git a/ancestors/dp_log/6da5d9ec-139a-11eb-845d-309c234925a0.cpp b/ancestors/dp_log/6da5d9ec-139a-11eb-845d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b70ffe4a7fb3208b9e95a98599cf5bd3a52eeb05 --- /dev/null +++ b/ancestors/dp_log/6da5d9ec-139a-11eb-845d-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +const int N = 250005; + +const int L = 20; + +int dp[L][N],lg[N]; + +int main() + +{ + + int n,m; + + in >> n >> m; + + for (int i = 2; i<=n; i++) + + lg[i] = lg[i/2]+1; + + for (int i = 1; i<=n; i++) + + { + + in >> dp[0][i]; + + for (int j = 1; j<=lg[n]; j++) + + dp[j][i] = dp[j-1][dp[j-1][i]]; + + } + + for (int i = 1; i<=m; i++) + + { + + int x,p; + + in >> x >> p; + + while (p) + + { + + x = dp[lg[p]][x]; + + p = p-(1< + +using namespace std; +typedef long long ll; +typedef pair< int , int > PII; + +int n, m, a[250100][20]; +vector < int > V[250100]; +bitset < 250100 > B; + +int pozr; +char buffer[1010]; +FILE *fi, *fo; + +char getch(){ + if( pozr == 1010 ){ + fread( buffer, 1010, 1, fi ); + pozr = 0; + } + return buffer[ pozr++ ]; +} + +int scano(){ + int nr = 0, sign = 1; + char ch = getch(); + + while( isdigit(ch) == 0 && ch != '-') + ch = getch(); + + if (ch == '-'){ + sign = -1; + ch = getch(); + } + + while( isdigit(ch) != 0 ){ + nr = nr * 10 + ch - '0'; + ch = getch(); + } + + return nr * sign; +} + +void dfs(int x) { + B[x] = 1; + + for (int i = 1; i <= 18; i++) { + a[x][i] = a[a[x][i - 1]][i - 1]; + } + + for (auto it : V[x]) { + if (B[it]) continue; + dfs(it); + } +} + +int main(){ + fi = fopen("stramosi.in", "r"); + fo = fopen("stramosi.out", "w"); + + n = scano(); + m = scano(); + for (int i = 1; i <= n; i++) { + a[i][0] = scano(); + V[a[i][0]].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (a[i][0] == 0) dfs(i); + } + + while (m--) { + int x, y; + x = scano(); + y = scano(); + + while (y) { + int lg = log2(y); + + x = a[x][lg]; + + y -= (1 << lg); + } + + fprintf(fo, "%d\n", x); + } + return 0; +} diff --git a/ancestors/dp_log/6e0d40ba-139a-11eb-b624-309c234925a0.cpp b/ancestors/dp_log/6e0d40ba-139a-11eb-b624-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2423cc150e16adcebda62108ac06e569aa65c19 --- /dev/null +++ b/ancestors/dp_log/6e0d40ba-139a-11eb-b624-309c234925a0.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + +int log[250001]; + +int d[18][250001]; + +int main() + +{ + + int n,m,i,j,p,q,x; + + in>>n>>m; + + for(i=2; i<=250000; i++) + + log[i]=1+log[i/2]; + + for(i=1; i<=n; i++) + + { + + in>>x; + + for(j=0; j<=log[n]; j++) + + { + + if(j==0) + + d[0][i]=x; + + else + + d[j][i]=d[j-1][d[j-1][i]]; + + } + + } + + int pas; + + for(i=1; i<=m; i++) + + { + + pas=1<<17; + + in>>p>>q; + + while(q) + + { + + if(q-pas>=0) + + { + + p=d[log[pas]][p]; + + q-=pas; + + } + + pas/=2; + + } + + out< + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int N=250001; + +const int LOG=17; + +int n,m,log2[N],s[LOG][N]; + + + +int main() + +{ + + in>>n>>m; + + for(int i=2;i<=n;i++) + + log2[i]=1+log2[i/2]; + + for(int i=1;i<=n;i++) + + { + + in>>s[0][i]; + + } + + for(int i=1;i<=log2[n];i++) + + for(int j=1;j<=n;j++) + + s[i][j]=s[i-1][s[i-1][j]]; + + int q,p,poz,k; + + for(int i=1;i<=m;i++) + + { + + in>>q>>p; + + poz=17; + + while(p>0 && poz>=0) + + { + + k=1< +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +int i,n,m,nod,poz,j,t[18][250001],aux; + +int main(){ + + fin>>n>>m; + + for(i=1;i<=n;i++)fin>>t[0][i]; + + for(i=1,aux=2;aux<=n;i++,aux<<=1)for(j=1;j<=n;j++)t[i][j]=t[i-1][t[i-1][j]]; + + for(i=1;i<=m;i++) + + {fin>>nod>>poz;j=0; + + while(poz){if(poz&1)nod=t[j][nod];poz>>=1;j++;}; + + fout< + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M; + +int stramos[19][250005]; + + + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1; i <= N; i++) + + fin >> stramos[0][i]; + + + + for(int j = 1; j <= 18; j++) + + for(int i = 1; i <= N; i++) + + stramos[j][i] = stramos[j - 1][stramos[j - 1][i]]; + + + + int q, p; + + for(int i = 1; i <= M; i++) + + { + + fin >> q >> p; + + + + int masca = 1; + + for(int i = 1; i <= 18; i++) + + { + + if(masca & p) + + q = stramos[i - 1][q]; + + + + masca <<= 1; + + } + + + + fout << q << '\n'; + + } + + + + return 0; + +} diff --git a/ancestors/dp_log/6e4e6466-139a-11eb-9dba-309c234925a0.cpp b/ancestors/dp_log/6e4e6466-139a-11eb-9dba-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cbefd4a14d42e79de1c632bd2f7bbd88b1926aa --- /dev/null +++ b/ancestors/dp_log/6e4e6466-139a-11eb-9dba-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include +int AnswerMyQuery(int node, int index); + + + +void calculateMyD(int N); + + + +FILE *fin, *fout; + + + + + +int tata[250000 + 1]; + + + +int d[18 + 1][250000 + 1]; + + + +int main() { + + fin = fopen("stramosi.in", "r"); + + fout = fopen("stramosi.out", "w"); + + + + + + int N, Q; + + + + fscanf(fin, "%d%d", &N, &Q); + + + + for(int i = 0; i < N; i++) { + + fscanf(fin , "%d", &tata[i + 1]); + + } + + + + calculateMyD(N); + + + + for(int i = 0; i < Q; i++) { + + int node, index; + + fscanf(fin, "%d%d", &node, &index); + + fprintf(fout, "%d\n", AnswerMyQuery(node, index)); + + } + + fclose(fin); + + fclose(fout); + + return 0; + +} + + + +int AnswerMyQuery(int node, int index) { + + + + int currnode = node; + + int level = 0; + + while(index > 0) { + + if(index % 2 == 1) { + + currnode = d[level][currnode]; + + } + + level++; + + index /= 2; + + } + + return currnode; + +} + + + +void calculateMyD(int N) { + + + + for(int j = 1; j <= N; j++) { + + d[0][j] = tata[j]; + + } + + for(int i = 1; i <= 18; i++) { + + for(int j = 1; j <= N; j++) { + + d[i][j] = d[i - 1][d[i - 1][j]]; + + } + + } + +} diff --git a/ancestors/dp_log/6e601794-139a-11eb-a081-309c234925a0.cpp b/ancestors/dp_log/6e601794-139a-11eb-a081-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4df212477c3710b408b48065b578de80c466c83e --- /dev/null +++ b/ancestors/dp_log/6e601794-139a-11eb-a081-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; +ifstream cin ("stramosi.in"); +ofstream cout ("stramosi.out"); +int t[250003],r[250003][18],log[250003]; +int main() +{ + int n,m,i,j,p,a,b; + cin>>n>>m; + for(i=2;i<=n;++i) + log[i]=log[i/2]+1; + for(i=1;i<=n;++i) + { + cin>>t[i]; + for(j=0;j<=log[n];++j) + { + if(j==0) + r[i][j]=t[i]; + else + r[i][j]=r[r[i][j-1]][j-1]; + } + } + for(i=1;i<=m;++i) + { + cin>>a>>b; + p=1<<17; + while(p) + { + if(b>=p) + { + b-=p; + a=r[a][log[p]]; + } + p>>=1; + } + cout< + + + + +using namespace std; + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int N=250001; + +const int LOG=17; + +int n,m,log2[N],s[LOG][N]; + + + +int main() + +{ + + in>>n>>m; + + for(int i=2;i<=n;i++) + + log2[i]=1+log2[i/2]; + + for(int i=1;i<=n;i++) + + { + + in>>s[0][i]; + + } + + for(int i=1;i<=log2[n];i++) + + for(int j=1;j<=n;j++) + + s[i][j]=s[i-1][s[i-1][j]]; + + int q,p,poz,k; + + for(int i=1;i<=m;i++) + + { + + in>>q>>p; + + poz=17; + + while(p>0 && poz>=0) + + { + + k=1< +#include + +using namespace std; + +const int NMAX = 250005; +const int LMAX = 20; + +int N, M; +int T[LMAX][NMAX]; + +int main() { + freopen("stramosi.in", "r", stdin); + freopen("stramosi.out", "w", stdout); + + scanf("%d%d", &N, &M); + + for (int i = 1; i <= N; ++i) { + scanf("%d", &T[0][i]); + } + + for (int k = 1; (1 << k) <= N; ++k) { + for (int i = 1; i <= N; ++i) { + T[k][i] = T[k-1][T[k-1][i]]; + } + } + + for (int i = 0; i < M; ++i) { + int q,p; + scanf("%d%d", &q, &p); + + for (int k = 17; k >= 0 && p; --k) { + if ((p >> k) % 2 == 1) { + q = T[k][q]; + } + } + printf("%d\n", q); + } + + fclose(stdin); + fclose(stdout); + return 0; +} diff --git a/ancestors/dp_log/6ec1e6b8-139a-11eb-8e73-309c234925a0.cpp b/ancestors/dp_log/6ec1e6b8-139a-11eb-8e73-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7147e842f1afb7060b69339d26f880c10322825 --- /dev/null +++ b/ancestors/dp_log/6ec1e6b8-139a-11eb-8e73-309c234925a0.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +ifstream fin ("stramosi.in"); +ofstream fout ("stramosi.out"); + +const int N = 250002, L = 18; +int s[L][N], log[N]; + +int main () +{ + int n, m; + fin >> n >> m; + for ( int i = 1; i <= n; i++ ) + { + int x; + fin >> x; + s[0][i] = x; + } + for ( int i = 2; i <= n; i++ ) + { + log[i] = log[i/2] + 1; + } + for ( int i = 1; i <= log[n]; i++ ) + { + for ( int j = 1; j <= n ; j++ ) + { + s[i][j] = s[i-1][s[i-1][j]]; + } + } + for ( int a = 1; a <= m ; a++ ) + { + int p, q; + fin >> q >> p; + int poz = 17; + while ( p != 0 && poz >= 0 ) + { + if( p >= ( 1< +#include + +using namespace std; + +const int NMAX = 250005; +const int LMAX = 20; + +int N, M; +int T[LMAX][NMAX]; + +int main() { + freopen("stramosi.in", "r", stdin); + freopen("stramosi.out", "w", stdout); + + scanf("%d%d", &N, &M); + + for (int i = 1; i <= N; ++i) { + scanf("%d", &T[0][i]); + } + + for (int k = 1; (1 << k) <= N; ++k) { + for (int i = 1; i <= N; ++i) { + T[k][i] = T[k-1][T[k-1][i]]; + } + } + + for (int i = 0; i < M; ++i) { + int q,p; + scanf("%d%d", &q, &p); + + for (int k = 17; k >= 0; --k) { + if ((p >> k) % 2 == 1) { + q = T[k][q]; + } + } + printf("%d\n", q); + } + + fclose(stdin); + fclose(stdout); + return 0; +} diff --git a/ancestors/dp_log/6ef7e4cc-139a-11eb-9f7b-309c234925a0.cpp b/ancestors/dp_log/6ef7e4cc-139a-11eb-9f7b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3710fc1ad4c8983f08f169dadacc20b2bad3b5c --- /dev/null +++ b/ancestors/dp_log/6ef7e4cc-139a-11eb-9f7b-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int stramosi[20][390625 + 10]; + + + +int main() { + + int n, m; + + fin >> n >> m; + + + + for (int i = 1; i <= n; ++i) { + + fin >> stramosi[0][i]; + + + + } + + + + for (int i = 1; (1 << i) <= n; ++i) { + + for(int j = 1; j <= n; ++j) { + + stramosi[i][j] = stramosi[i - 1][stramosi[i - 1][j]]; + + } + + } + + + + while (m) { + + int q, p; + + fin >> q >> p; + + int poz = 17; + + while (poz >= 0 && p != 0) { + + if (p >= (1 << poz)) { + + q = stramosi[poz][q]; + + p -= (1 << poz); + + } + + poz--; + + } + + fout << q << '\n'; + + m--; + + } + + return 0; + +} diff --git a/ancestors/dp_log/6f2e3252-139a-11eb-b193-309c234925a0.cpp b/ancestors/dp_log/6f2e3252-139a-11eb-b193-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2c2f18b1177fb469cd6f9cc076658d943617e62 --- /dev/null +++ b/ancestors/dp_log/6f2e3252-139a-11eb-b193-309c234925a0.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +ifstream in("stramosi.in"); +ofstream out("stramosi.out"); + +const int Nmax = 25e4; + +int stramos[20][Nmax + 10]; + +int main() { + ios::sync_with_stdio(false); in.tie(0); out.tie(0); + + int n, m; in >> n >> m; + + for(int i = 1; i <= n; ++i) { + in >> stramos[0][i]; + } + + for(int i = 1; (1 << i) <= n; ++i) { + for(int j = 1; j <= n; ++j) { + stramos[i][j] = stramos[i - 1][stramos[i - 1][j]]; + } + } + + while(m--) { + int q, p; in >> q >> p; + + for(int i = 0; (1 << i) <= n && q; ++i) { + if((p >> i) % 2 == 1) { + q = stramos[i][q]; + } + } + + out << q << '\n'; + } + + in.close(); out.close(); + + return 0; +} diff --git a/ancestors/dp_log/6f3ead18-139a-11eb-8fa7-309c234925a0.cpp b/ancestors/dp_log/6f3ead18-139a-11eb-8fa7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f319ba01805ad89759aa4d0aa1997fc3c7087d46 --- /dev/null +++ b/ancestors/dp_log/6f3ead18-139a-11eb-8fa7-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include +#include + +using namespace std; + +const int N=250001; +const int L=30; +int s[L][N]; +int stramos(int x, int p) +{ + int poz=17; + while(poz>=0 && p!=0) + { + if(p>=(1<>n>>m; + for(i=1; i<=n; i++) + { + f>>t[i]; + s[0][i]=t[i]; + + } + for(int i=1; (1<>x>>y; + g< + +using namespace std; + +ifstream in("stramosi.in"); +ofstream out("stramosi.out"); + +const int Nmax = 25e4; + +int stramos[20][Nmax + 10]; + +int main() { + ios::sync_with_stdio(false); in.tie(0); out.tie(0); + + int n, m; in >> n >> m; + + for(int i = 1; i <= n; ++i) { + in >> stramos[0][i]; + } + + for(int i = 1; (1 << i) <= n; ++i) { + for(int j = 1; j <= n; ++j) { + stramos[i][j] = stramos[i - 1][stramos[i - 1][j]]; + } + } + + while(m--) { + int q, p; in >> q >> p; + + for(int i = 0; (1 << i) <= n && q; ++i) { + if((p >> i) % 2 == 1) { + q = stramos[i][q]; + } + } + + out << q << '\n'; + } + + in.close(); out.close(); + + return 0; +} diff --git a/ancestors/dp_log/6f5cdd9a-139a-11eb-abab-309c234925a0.cpp b/ancestors/dp_log/6f5cdd9a-139a-11eb-abab-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85c5ecc5466dac2ca8d4308e3fd0ccd614330b78 --- /dev/null +++ b/ancestors/dp_log/6f5cdd9a-139a-11eb-abab-309c234925a0.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +ifstream fin("stramosi.in"); +ofstream fout("stramosi.out"); + +const int NMax = 250005; +const int LMax = 20; + +int v[LMax][NMax]; + +int main(){ + int n, m, p, node; + fin >> n >> m; + for(int i = 1; i <= n; i++){ + fin >> v[0][i]; + } + for(int i = 1; i < LMax; i++){ + for(int j = 1; j <= n; j++){ + v[i][j] = v[i - 1][v[i - 1][j]]; + } + } + for(int i = 1; i <= m; i++){ + fin >> node >> p; + for(int i = 0; i < LMax; i++){ + if((p >> i) % 2 == 1){ + node = v[i][node]; + } + } + fout << node << "\n"; + } + return 0; +} diff --git a/ancestors/lant/06778e71-d3d5-4a39-a971-09816c9f60c9.cpp b/ancestors/lant/06778e71-d3d5-4a39-a971-09816c9f60c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/06778e71-d3d5-4a39-a971-09816c9f60c9.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/0ac3bab8-2e67-4189-a783-f6627958aa92.cpp b/ancestors/lant/0ac3bab8-2e67-4189-a783-f6627958aa92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/0ac3bab8-2e67-4189-a783-f6627958aa92.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/0bd0c373-68c2-43d6-9db1-a367fd3b7f1b.cpp b/ancestors/lant/0bd0c373-68c2-43d6-9db1-a367fd3b7f1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/0bd0c373-68c2-43d6-9db1-a367fd3b7f1b.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/0f4e87e5-c1af-40e4-8a81-b39e627af476.cpp b/ancestors/lant/0f4e87e5-c1af-40e4-8a81-b39e627af476.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/0f4e87e5-c1af-40e4-8a81-b39e627af476.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/10066948-b832-4749-85f0-f970681b81dd.cpp b/ancestors/lant/10066948-b832-4749-85f0-f970681b81dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/10066948-b832-4749-85f0-f970681b81dd.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/128ce915-9871-4003-950e-ba0412d83596.cpp b/ancestors/lant/128ce915-9871-4003-950e-ba0412d83596.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/128ce915-9871-4003-950e-ba0412d83596.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/137203ad-cdc0-4708-b6de-f068561b54e1.cpp b/ancestors/lant/137203ad-cdc0-4708-b6de-f068561b54e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/137203ad-cdc0-4708-b6de-f068561b54e1.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/14def17f-e08b-4638-bc0b-2cbdda6bd34e.cpp b/ancestors/lant/14def17f-e08b-4638-bc0b-2cbdda6bd34e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/14def17f-e08b-4638-bc0b-2cbdda6bd34e.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/1ac6419e-a2c4-4a0c-b3ff-785ff2c2ea6b.cpp b/ancestors/lant/1ac6419e-a2c4-4a0c-b3ff-785ff2c2ea6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/1ac6419e-a2c4-4a0c-b3ff-785ff2c2ea6b.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/1c36bd1e-011f-4ef3-8110-fb228e9bf691.cpp b/ancestors/lant/1c36bd1e-011f-4ef3-8110-fb228e9bf691.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/1c36bd1e-011f-4ef3-8110-fb228e9bf691.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/1d7cee86-65c7-487a-ada5-c8d0c28d6cbe.cpp b/ancestors/lant/1d7cee86-65c7-487a-ada5-c8d0c28d6cbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/1d7cee86-65c7-487a-ada5-c8d0c28d6cbe.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/21190f5f-f542-4e2a-b463-8fde92d1fc19.cpp b/ancestors/lant/21190f5f-f542-4e2a-b463-8fde92d1fc19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/21190f5f-f542-4e2a-b463-8fde92d1fc19.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/235a4762-7909-4313-801c-cb3bed046dd7.cpp b/ancestors/lant/235a4762-7909-4313-801c-cb3bed046dd7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/235a4762-7909-4313-801c-cb3bed046dd7.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/25743f84-0279-4a69-82e2-2f4ebb6d1495.cpp b/ancestors/lant/25743f84-0279-4a69-82e2-2f4ebb6d1495.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/25743f84-0279-4a69-82e2-2f4ebb6d1495.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/25dc2c88-ea7c-44d5-b765-157820b5d697.cpp b/ancestors/lant/25dc2c88-ea7c-44d5-b765-157820b5d697.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/25dc2c88-ea7c-44d5-b765-157820b5d697.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/277bfcbb-9930-4f3f-9608-6c2c740c57c5.cpp b/ancestors/lant/277bfcbb-9930-4f3f-9608-6c2c740c57c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/277bfcbb-9930-4f3f-9608-6c2c740c57c5.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/28064427-6f04-47cd-ac37-d873a95e9ad1.cpp b/ancestors/lant/28064427-6f04-47cd-ac37-d873a95e9ad1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/28064427-6f04-47cd-ac37-d873a95e9ad1.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/28246c43-bf11-43a7-bb0b-8ffbe87af41f.cpp b/ancestors/lant/28246c43-bf11-43a7-bb0b-8ffbe87af41f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/28246c43-bf11-43a7-bb0b-8ffbe87af41f.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/28a2f3a7-9a92-405a-b4ea-f1d61412a858.cpp b/ancestors/lant/28a2f3a7-9a92-405a-b4ea-f1d61412a858.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/28a2f3a7-9a92-405a-b4ea-f1d61412a858.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/28c5fb00-4fe5-404f-9f90-ac56c8b5d215.cpp b/ancestors/lant/28c5fb00-4fe5-404f-9f90-ac56c8b5d215.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/28c5fb00-4fe5-404f-9f90-ac56c8b5d215.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/293f8358-f668-4919-af9d-29b747960cc2.cpp b/ancestors/lant/293f8358-f668-4919-af9d-29b747960cc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/293f8358-f668-4919-af9d-29b747960cc2.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/297b9e41-e17a-4c90-8ca4-f3e1baba375b.cpp b/ancestors/lant/297b9e41-e17a-4c90-8ca4-f3e1baba375b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/297b9e41-e17a-4c90-8ca4-f3e1baba375b.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/2a78d5b2-00c7-4f1f-b963-9b9a802db6c8.cpp b/ancestors/lant/2a78d5b2-00c7-4f1f-b963-9b9a802db6c8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/2a78d5b2-00c7-4f1f-b963-9b9a802db6c8.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/2ace95e6-cc9d-4857-ac14-481cf18196e5.cpp b/ancestors/lant/2ace95e6-cc9d-4857-ac14-481cf18196e5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/2ace95e6-cc9d-4857-ac14-481cf18196e5.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/2b58fe8c-5895-4bdb-9f3b-0fc41633e914.cpp b/ancestors/lant/2b58fe8c-5895-4bdb-9f3b-0fc41633e914.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/2b58fe8c-5895-4bdb-9f3b-0fc41633e914.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/2c0e8acd-3010-4c18-9be7-c7c2f2e1ffa2.cpp b/ancestors/lant/2c0e8acd-3010-4c18-9be7-c7c2f2e1ffa2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/2c0e8acd-3010-4c18-9be7-c7c2f2e1ffa2.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/2e924855-c9d2-4931-9ce4-ed58cee5104e.cpp b/ancestors/lant/2e924855-c9d2-4931-9ce4-ed58cee5104e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/2e924855-c9d2-4931-9ce4-ed58cee5104e.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/2eada85e-534e-4156-bc5a-e0956dec8546.cpp b/ancestors/lant/2eada85e-534e-4156-bc5a-e0956dec8546.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/2eada85e-534e-4156-bc5a-e0956dec8546.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/300309c1-8c53-451c-8567-5d972a43dc05.cpp b/ancestors/lant/300309c1-8c53-451c-8567-5d972a43dc05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/300309c1-8c53-451c-8567-5d972a43dc05.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/31c5f572-9252-447e-825a-62c67be30619.cpp b/ancestors/lant/31c5f572-9252-447e-825a-62c67be30619.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/31c5f572-9252-447e-825a-62c67be30619.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/344c8430-883a-4bb1-ada9-b026d219bd7e.cpp b/ancestors/lant/344c8430-883a-4bb1-ada9-b026d219bd7e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/344c8430-883a-4bb1-ada9-b026d219bd7e.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/34c1f5ae-ff7f-4bf0-b170-e87eb05a8ada.cpp b/ancestors/lant/34c1f5ae-ff7f-4bf0-b170-e87eb05a8ada.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/34c1f5ae-ff7f-4bf0-b170-e87eb05a8ada.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/37334a00-f708-4728-947e-10792911026c.cpp b/ancestors/lant/37334a00-f708-4728-947e-10792911026c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/37334a00-f708-4728-947e-10792911026c.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/3c1b3a9a-ba4a-4874-a75f-ebd672c2a0cf.cpp b/ancestors/lant/3c1b3a9a-ba4a-4874-a75f-ebd672c2a0cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/3c1b3a9a-ba4a-4874-a75f-ebd672c2a0cf.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/3d2a94d7-ae65-48a2-b845-5a8c8dcfc66e.cpp b/ancestors/lant/3d2a94d7-ae65-48a2-b845-5a8c8dcfc66e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/3d2a94d7-ae65-48a2-b845-5a8c8dcfc66e.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/3ded9102-a669-44dd-aa92-bbe26bdc92a1.cpp b/ancestors/lant/3ded9102-a669-44dd-aa92-bbe26bdc92a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/3ded9102-a669-44dd-aa92-bbe26bdc92a1.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/3e05f762-7c91-40ed-a18f-7432d0ffb147.cpp b/ancestors/lant/3e05f762-7c91-40ed-a18f-7432d0ffb147.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/3e05f762-7c91-40ed-a18f-7432d0ffb147.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/3e329d45-7285-4f08-bb4d-070d6db41011.cpp b/ancestors/lant/3e329d45-7285-4f08-bb4d-070d6db41011.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/3e329d45-7285-4f08-bb4d-070d6db41011.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/3e634a0f-64e8-401c-8dff-660512e1fae4.cpp b/ancestors/lant/3e634a0f-64e8-401c-8dff-660512e1fae4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/3e634a0f-64e8-401c-8dff-660512e1fae4.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/3e7cba08-d9e4-4d48-8ce3-0a6f1ea65e80.cpp b/ancestors/lant/3e7cba08-d9e4-4d48-8ce3-0a6f1ea65e80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/3e7cba08-d9e4-4d48-8ce3-0a6f1ea65e80.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/41398784-5299-4649-8a8e-23180608c77f.cpp b/ancestors/lant/41398784-5299-4649-8a8e-23180608c77f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/41398784-5299-4649-8a8e-23180608c77f.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/42db36c5-2d56-4ba5-9ffe-bb14da023ac9.cpp b/ancestors/lant/42db36c5-2d56-4ba5-9ffe-bb14da023ac9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/42db36c5-2d56-4ba5-9ffe-bb14da023ac9.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/447e098f-359b-4a24-86c0-1f3501abde00.cpp b/ancestors/lant/447e098f-359b-4a24-86c0-1f3501abde00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/447e098f-359b-4a24-86c0-1f3501abde00.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/44fe5ab8-dffd-4cd8-b977-08f8b50d0714.cpp b/ancestors/lant/44fe5ab8-dffd-4cd8-b977-08f8b50d0714.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/44fe5ab8-dffd-4cd8-b977-08f8b50d0714.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/45b32ec5-6cf2-4862-b579-46457110115c.cpp b/ancestors/lant/45b32ec5-6cf2-4862-b579-46457110115c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/45b32ec5-6cf2-4862-b579-46457110115c.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/4613ad1d-7ea0-46c5-8879-c6b75dcca56a.cpp b/ancestors/lant/4613ad1d-7ea0-46c5-8879-c6b75dcca56a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/4613ad1d-7ea0-46c5-8879-c6b75dcca56a.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/487085ad-f326-4b98-841c-4f4d8a8f3740.cpp b/ancestors/lant/487085ad-f326-4b98-841c-4f4d8a8f3740.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/487085ad-f326-4b98-841c-4f4d8a8f3740.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/48d966c0-2cd0-481b-a4b5-2af37762a67f.cpp b/ancestors/lant/48d966c0-2cd0-481b-a4b5-2af37762a67f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/48d966c0-2cd0-481b-a4b5-2af37762a67f.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/4a6c6b30-34d7-446d-a61d-d4fa4221d3c5.cpp b/ancestors/lant/4a6c6b30-34d7-446d-a61d-d4fa4221d3c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/4a6c6b30-34d7-446d-a61d-d4fa4221d3c5.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/4b135bf6-6d9a-417f-8a90-4ed6e2d4637f.cpp b/ancestors/lant/4b135bf6-6d9a-417f-8a90-4ed6e2d4637f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/4b135bf6-6d9a-417f-8a90-4ed6e2d4637f.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/4ee2e18f-6818-4548-816c-f37b7660910e.cpp b/ancestors/lant/4ee2e18f-6818-4548-816c-f37b7660910e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/4ee2e18f-6818-4548-816c-f37b7660910e.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/53db5de8-139a-11eb-a196-309c234925a0.cpp b/ancestors/lant/53db5de8-139a-11eb-a196-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/53db5de8-139a-11eb-a196-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/547d5b31-3e10-4cae-8793-aab3332e6abe.cpp b/ancestors/lant/547d5b31-3e10-4cae-8793-aab3332e6abe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/547d5b31-3e10-4cae-8793-aab3332e6abe.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/54e1b5de-139a-11eb-a78d-309c234925a0.cpp b/ancestors/lant/54e1b5de-139a-11eb-a78d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/54e1b5de-139a-11eb-a78d-309c234925a0.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/56616f0e-1fdd-4997-8868-2cd5798e5bb8.cpp b/ancestors/lant/56616f0e-1fdd-4997-8868-2cd5798e5bb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/56616f0e-1fdd-4997-8868-2cd5798e5bb8.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/56a78018-139a-11eb-9c53-309c234925a0.cpp b/ancestors/lant/56a78018-139a-11eb-9c53-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/56a78018-139a-11eb-9c53-309c234925a0.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/56e864ce-2a4e-441b-874e-f3150fcaa9ed.cpp b/ancestors/lant/56e864ce-2a4e-441b-874e-f3150fcaa9ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/56e864ce-2a4e-441b-874e-f3150fcaa9ed.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/586109b8-139a-11eb-8c8a-309c234925a0.cpp b/ancestors/lant/586109b8-139a-11eb-8c8a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/586109b8-139a-11eb-8c8a-309c234925a0.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/592879ed-9432-43ec-93b3-8422a50c2cbe.cpp b/ancestors/lant/592879ed-9432-43ec-93b3-8422a50c2cbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/592879ed-9432-43ec-93b3-8422a50c2cbe.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/59ae87d2-f3c7-43cc-b4f0-787328ffbdf7.cpp b/ancestors/lant/59ae87d2-f3c7-43cc-b4f0-787328ffbdf7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/59ae87d2-f3c7-43cc-b4f0-787328ffbdf7.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/5ae63618-139a-11eb-a6c0-309c234925a0.cpp b/ancestors/lant/5ae63618-139a-11eb-a6c0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/5ae63618-139a-11eb-a6c0-309c234925a0.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/5b933eb6-139a-11eb-8783-309c234925a0.cpp b/ancestors/lant/5b933eb6-139a-11eb-8783-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/5b933eb6-139a-11eb-8783-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/5be5cb90-139a-11eb-b523-309c234925a0.cpp b/ancestors/lant/5be5cb90-139a-11eb-b523-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/5be5cb90-139a-11eb-b523-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/5c110710-7133-45d3-82bf-b28cde030558.cpp b/ancestors/lant/5c110710-7133-45d3-82bf-b28cde030558.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/5c110710-7133-45d3-82bf-b28cde030558.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/5c8f1ab6-40cd-4375-83a4-dd9f9abf46bb.cpp b/ancestors/lant/5c8f1ab6-40cd-4375-83a4-dd9f9abf46bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/5c8f1ab6-40cd-4375-83a4-dd9f9abf46bb.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/5cda92d2-cf24-4179-86e3-14e28f846e69.cpp b/ancestors/lant/5cda92d2-cf24-4179-86e3-14e28f846e69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/5cda92d2-cf24-4179-86e3-14e28f846e69.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/5e1bd95b-bb41-46b3-b0e8-c42d2fbc7268.cpp b/ancestors/lant/5e1bd95b-bb41-46b3-b0e8-c42d2fbc7268.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/5e1bd95b-bb41-46b3-b0e8-c42d2fbc7268.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/5fc44bb0-4ff6-4787-81e5-3510903edd8c.cpp b/ancestors/lant/5fc44bb0-4ff6-4787-81e5-3510903edd8c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/5fc44bb0-4ff6-4787-81e5-3510903edd8c.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/5fda329e-f19b-4abb-a0ac-14779a545e9d.cpp b/ancestors/lant/5fda329e-f19b-4abb-a0ac-14779a545e9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/5fda329e-f19b-4abb-a0ac-14779a545e9d.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/612d05de-139a-11eb-9544-309c234925a0.cpp b/ancestors/lant/612d05de-139a-11eb-9544-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/612d05de-139a-11eb-9544-309c234925a0.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/613babe4-139a-11eb-875f-309c234925a0.cpp b/ancestors/lant/613babe4-139a-11eb-875f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/613babe4-139a-11eb-875f-309c234925a0.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/6154c1eb-5efd-4318-9a51-e82b576763ef.cpp b/ancestors/lant/6154c1eb-5efd-4318-9a51-e82b576763ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/6154c1eb-5efd-4318-9a51-e82b576763ef.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/61b4234a-139a-11eb-82a4-309c234925a0.cpp b/ancestors/lant/61b4234a-139a-11eb-82a4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/61b4234a-139a-11eb-82a4-309c234925a0.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/6203490a-139a-11eb-a754-309c234925a0.cpp b/ancestors/lant/6203490a-139a-11eb-a754-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/6203490a-139a-11eb-a754-309c234925a0.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/624f8ec5-7b48-4cf9-b6e1-28dc47805bd6.cpp b/ancestors/lant/624f8ec5-7b48-4cf9-b6e1-28dc47805bd6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/624f8ec5-7b48-4cf9-b6e1-28dc47805bd6.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/62cff590-139a-11eb-9e01-309c234925a0.cpp b/ancestors/lant/62cff590-139a-11eb-9e01-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/62cff590-139a-11eb-9e01-309c234925a0.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/630b2814-934d-4fae-96ab-06eb7104cef2.cpp b/ancestors/lant/630b2814-934d-4fae-96ab-06eb7104cef2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/630b2814-934d-4fae-96ab-06eb7104cef2.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/63a4fe8c-139a-11eb-b882-309c234925a0.cpp b/ancestors/lant/63a4fe8c-139a-11eb-b882-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/63a4fe8c-139a-11eb-b882-309c234925a0.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/64330dd8-139a-11eb-a5ca-309c234925a0.cpp b/ancestors/lant/64330dd8-139a-11eb-a5ca-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/64330dd8-139a-11eb-a5ca-309c234925a0.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/64857a69-894a-456c-a830-b8611d931d56.cpp b/ancestors/lant/64857a69-894a-456c-a830-b8611d931d56.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/64857a69-894a-456c-a830-b8611d931d56.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/64a3fc3c-4db8-481e-80d9-24e238c70a2f.cpp b/ancestors/lant/64a3fc3c-4db8-481e-80d9-24e238c70a2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/64a3fc3c-4db8-481e-80d9-24e238c70a2f.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/65a98bd4-139a-11eb-b175-309c234925a0.cpp b/ancestors/lant/65a98bd4-139a-11eb-b175-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/65a98bd4-139a-11eb-b175-309c234925a0.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/65b9d522-0759-4590-8ec0-9b02be660a0e.cpp b/ancestors/lant/65b9d522-0759-4590-8ec0-9b02be660a0e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/65b9d522-0759-4590-8ec0-9b02be660a0e.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/6a1f8779-80e1-46d8-9d42-a927749dc6c3.cpp b/ancestors/lant/6a1f8779-80e1-46d8-9d42-a927749dc6c3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/6a1f8779-80e1-46d8-9d42-a927749dc6c3.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/6b13514a-139a-11eb-81ec-309c234925a0.cpp b/ancestors/lant/6b13514a-139a-11eb-81ec-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/6b13514a-139a-11eb-81ec-309c234925a0.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/6f456d2f-68d8-4fee-b577-598f6828df5c.cpp b/ancestors/lant/6f456d2f-68d8-4fee-b577-598f6828df5c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/6f456d2f-68d8-4fee-b577-598f6828df5c.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/71646b34-84d9-4130-b518-fd10578ab74b.cpp b/ancestors/lant/71646b34-84d9-4130-b518-fd10578ab74b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/71646b34-84d9-4130-b518-fd10578ab74b.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/735daac7-12f4-4257-afd8-16200a4f745e.cpp b/ancestors/lant/735daac7-12f4-4257-afd8-16200a4f745e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/735daac7-12f4-4257-afd8-16200a4f745e.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/75089787-b110-45a0-9c2c-f50e01ca1888.cpp b/ancestors/lant/75089787-b110-45a0-9c2c-f50e01ca1888.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/75089787-b110-45a0-9c2c-f50e01ca1888.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/77de4f2b-edc0-400a-ab61-581ec5dc60c5.cpp b/ancestors/lant/77de4f2b-edc0-400a-ab61-581ec5dc60c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/77de4f2b-edc0-400a-ab61-581ec5dc60c5.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/78529c4e-57f4-4938-bcb4-6a6901204f2d.cpp b/ancestors/lant/78529c4e-57f4-4938-bcb4-6a6901204f2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/78529c4e-57f4-4938-bcb4-6a6901204f2d.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/79885a59-b623-4208-aa27-1e00881af13e.cpp b/ancestors/lant/79885a59-b623-4208-aa27-1e00881af13e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/79885a59-b623-4208-aa27-1e00881af13e.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/7a6f1bc4-428c-4883-ad39-37ef30fda87a.cpp b/ancestors/lant/7a6f1bc4-428c-4883-ad39-37ef30fda87a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/7a6f1bc4-428c-4883-ad39-37ef30fda87a.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/7c3fbe1f-fdca-4fbe-8b28-9441a7f98338.cpp b/ancestors/lant/7c3fbe1f-fdca-4fbe-8b28-9441a7f98338.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/7c3fbe1f-fdca-4fbe-8b28-9441a7f98338.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/7cd2938a-5143-451d-ba97-0619a68c2c70.cpp b/ancestors/lant/7cd2938a-5143-451d-ba97-0619a68c2c70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/7cd2938a-5143-451d-ba97-0619a68c2c70.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/7d0eafab-6ff7-4bb1-aa0e-11f897f6afc0.cpp b/ancestors/lant/7d0eafab-6ff7-4bb1-aa0e-11f897f6afc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/7d0eafab-6ff7-4bb1-aa0e-11f897f6afc0.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/80027c02-0b8f-4cde-aca6-8fb7c7993d6d.cpp b/ancestors/lant/80027c02-0b8f-4cde-aca6-8fb7c7993d6d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/80027c02-0b8f-4cde-aca6-8fb7c7993d6d.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/867e2454-56a6-4394-8578-2f614db3cb3f.cpp b/ancestors/lant/867e2454-56a6-4394-8578-2f614db3cb3f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/867e2454-56a6-4394-8578-2f614db3cb3f.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/8a68643f-3772-453e-ad65-aecd44d95f9f.cpp b/ancestors/lant/8a68643f-3772-453e-ad65-aecd44d95f9f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/8a68643f-3772-453e-ad65-aecd44d95f9f.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/8a8aca85-e698-4af6-b888-cf6a37a06b53.cpp b/ancestors/lant/8a8aca85-e698-4af6-b888-cf6a37a06b53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/8a8aca85-e698-4af6-b888-cf6a37a06b53.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/8a8e053c-ba88-4f56-bb03-267deb53d751.cpp b/ancestors/lant/8a8e053c-ba88-4f56-bb03-267deb53d751.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/8a8e053c-ba88-4f56-bb03-267deb53d751.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/8b0862c1-4b3a-4f3f-b882-32f3174b4103.cpp b/ancestors/lant/8b0862c1-4b3a-4f3f-b882-32f3174b4103.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/8b0862c1-4b3a-4f3f-b882-32f3174b4103.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/8b277f08-d79a-4e9b-8cae-a5d0fc62ffdb.cpp b/ancestors/lant/8b277f08-d79a-4e9b-8cae-a5d0fc62ffdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/8b277f08-d79a-4e9b-8cae-a5d0fc62ffdb.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/8b3ad9e6-d092-49db-97b5-907c7bcf0f4e.cpp b/ancestors/lant/8b3ad9e6-d092-49db-97b5-907c7bcf0f4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/8b3ad9e6-d092-49db-97b5-907c7bcf0f4e.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/8bc40d88-326d-46f6-8e0e-4119a8478885.cpp b/ancestors/lant/8bc40d88-326d-46f6-8e0e-4119a8478885.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/8bc40d88-326d-46f6-8e0e-4119a8478885.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/8c449832-4d75-496f-98af-d010e86d8db4.cpp b/ancestors/lant/8c449832-4d75-496f-98af-d010e86d8db4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/8c449832-4d75-496f-98af-d010e86d8db4.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/8dea321d-dfe3-4416-b0ca-3f42c6040105.cpp b/ancestors/lant/8dea321d-dfe3-4416-b0ca-3f42c6040105.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/8dea321d-dfe3-4416-b0ca-3f42c6040105.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/8f8e9573-9367-4a6d-97a6-ca8e7e23ba97.cpp b/ancestors/lant/8f8e9573-9367-4a6d-97a6-ca8e7e23ba97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/8f8e9573-9367-4a6d-97a6-ca8e7e23ba97.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/92c7331b-d88c-4c5f-bd0c-884c9a1734c3.cpp b/ancestors/lant/92c7331b-d88c-4c5f-bd0c-884c9a1734c3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/92c7331b-d88c-4c5f-bd0c-884c9a1734c3.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/9386424e-0cf0-408c-8558-570b6e5a4730.cpp b/ancestors/lant/9386424e-0cf0-408c-8558-570b6e5a4730.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/9386424e-0cf0-408c-8558-570b6e5a4730.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/9475d36c-b67e-4a19-a855-3055efa29769.cpp b/ancestors/lant/9475d36c-b67e-4a19-a855-3055efa29769.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/9475d36c-b67e-4a19-a855-3055efa29769.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/95d4b7a7-469e-4066-ba98-9cae3e2f04a6.cpp b/ancestors/lant/95d4b7a7-469e-4066-ba98-9cae3e2f04a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/95d4b7a7-469e-4066-ba98-9cae3e2f04a6.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/9668683d-f155-461b-9774-e8e8fd698b36.cpp b/ancestors/lant/9668683d-f155-461b-9774-e8e8fd698b36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/9668683d-f155-461b-9774-e8e8fd698b36.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/9802b578-5c5a-4229-9d4f-0138213ca963.cpp b/ancestors/lant/9802b578-5c5a-4229-9d4f-0138213ca963.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/9802b578-5c5a-4229-9d4f-0138213ca963.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/9a4cc757-937f-44a5-8dac-c5bcb1bf02eb.cpp b/ancestors/lant/9a4cc757-937f-44a5-8dac-c5bcb1bf02eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/9a4cc757-937f-44a5-8dac-c5bcb1bf02eb.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/9b7ad1a6-a5c6-4c4c-b6ab-5c5c521bd7b9.cpp b/ancestors/lant/9b7ad1a6-a5c6-4c4c-b6ab-5c5c521bd7b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/9b7ad1a6-a5c6-4c4c-b6ab-5c5c521bd7b9.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/9c06f7df-bc2b-43db-8ff6-f44dca251130.cpp b/ancestors/lant/9c06f7df-bc2b-43db-8ff6-f44dca251130.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/9c06f7df-bc2b-43db-8ff6-f44dca251130.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/9c4264e3-a420-4a38-bf76-70b7e4b6054c.cpp b/ancestors/lant/9c4264e3-a420-4a38-bf76-70b7e4b6054c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/9c4264e3-a420-4a38-bf76-70b7e4b6054c.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/9c99ddc2-e6b4-4861-b2f9-2426de24d182.cpp b/ancestors/lant/9c99ddc2-e6b4-4861-b2f9-2426de24d182.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/9c99ddc2-e6b4-4861-b2f9-2426de24d182.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/9d6c2785-ff0e-4e95-8aa8-82f66cee8f1f.cpp b/ancestors/lant/9d6c2785-ff0e-4e95-8aa8-82f66cee8f1f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/9d6c2785-ff0e-4e95-8aa8-82f66cee8f1f.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/9e903237-604f-469a-8eba-d786712ef6ed.cpp b/ancestors/lant/9e903237-604f-469a-8eba-d786712ef6ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/9e903237-604f-469a-8eba-d786712ef6ed.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/9f8c93f3-d19e-491d-88aa-43166b6600ff.cpp b/ancestors/lant/9f8c93f3-d19e-491d-88aa-43166b6600ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/9f8c93f3-d19e-491d-88aa-43166b6600ff.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/a6afb677-65b0-41ca-8a0a-b61793feb150.cpp b/ancestors/lant/a6afb677-65b0-41ca-8a0a-b61793feb150.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/a6afb677-65b0-41ca-8a0a-b61793feb150.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/a7fd078d-e36a-4d40-b65a-8db8ff8b8b3d.cpp b/ancestors/lant/a7fd078d-e36a-4d40-b65a-8db8ff8b8b3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/a7fd078d-e36a-4d40-b65a-8db8ff8b8b3d.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/a83cebbe-d989-492f-9a05-0a977e20a33e.cpp b/ancestors/lant/a83cebbe-d989-492f-9a05-0a977e20a33e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/a83cebbe-d989-492f-9a05-0a977e20a33e.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/aa665070-7b17-4dc8-8aa9-c54e36b760f6.cpp b/ancestors/lant/aa665070-7b17-4dc8-8aa9-c54e36b760f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/aa665070-7b17-4dc8-8aa9-c54e36b760f6.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/aaed6664-9252-4240-992d-4a9ccc82aed6.cpp b/ancestors/lant/aaed6664-9252-4240-992d-4a9ccc82aed6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/aaed6664-9252-4240-992d-4a9ccc82aed6.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/ae306b2b-d2a7-414f-bb8d-603035354e39.cpp b/ancestors/lant/ae306b2b-d2a7-414f-bb8d-603035354e39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/ae306b2b-d2a7-414f-bb8d-603035354e39.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/b04b5d0e-01a3-46d6-8f52-0983276db41c.cpp b/ancestors/lant/b04b5d0e-01a3-46d6-8f52-0983276db41c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/b04b5d0e-01a3-46d6-8f52-0983276db41c.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/b0d8fb32-430d-4532-beae-52b012c1378f.cpp b/ancestors/lant/b0d8fb32-430d-4532-beae-52b012c1378f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/b0d8fb32-430d-4532-beae-52b012c1378f.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/b11d2034-d114-419b-bc09-f99845b6ad45.cpp b/ancestors/lant/b11d2034-d114-419b-bc09-f99845b6ad45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/b11d2034-d114-419b-bc09-f99845b6ad45.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/b2984779-915b-4410-be19-605013c3f18e.cpp b/ancestors/lant/b2984779-915b-4410-be19-605013c3f18e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/b2984779-915b-4410-be19-605013c3f18e.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/b2b7e20b-5251-489a-a32a-8752430ef940.cpp b/ancestors/lant/b2b7e20b-5251-489a-a32a-8752430ef940.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/b2b7e20b-5251-489a-a32a-8752430ef940.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/b396933c-08f6-4b84-a99f-b62dded0833b.cpp b/ancestors/lant/b396933c-08f6-4b84-a99f-b62dded0833b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/b396933c-08f6-4b84-a99f-b62dded0833b.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/b43bab08-ccb7-437a-a005-7863d0634a7e.cpp b/ancestors/lant/b43bab08-ccb7-437a-a005-7863d0634a7e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/b43bab08-ccb7-437a-a005-7863d0634a7e.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/b45561c3-ff62-4075-8029-8cb416e2ce19.cpp b/ancestors/lant/b45561c3-ff62-4075-8029-8cb416e2ce19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/b45561c3-ff62-4075-8029-8cb416e2ce19.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/b54ad741-7221-44b7-9f66-716925a18e2f.cpp b/ancestors/lant/b54ad741-7221-44b7-9f66-716925a18e2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/b54ad741-7221-44b7-9f66-716925a18e2f.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/b580807b-cf25-41a1-ac7d-2b9154aa94e4.cpp b/ancestors/lant/b580807b-cf25-41a1-ac7d-2b9154aa94e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/b580807b-cf25-41a1-ac7d-2b9154aa94e4.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/b5aab420-aa8e-4012-8dbc-7827be1aa64f.cpp b/ancestors/lant/b5aab420-aa8e-4012-8dbc-7827be1aa64f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/b5aab420-aa8e-4012-8dbc-7827be1aa64f.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/b85ec8d4-cfe3-4fc8-8c91-d370fe62b182.cpp b/ancestors/lant/b85ec8d4-cfe3-4fc8-8c91-d370fe62b182.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/b85ec8d4-cfe3-4fc8-8c91-d370fe62b182.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/ba846f2c-e53b-4c6f-af2c-044fbaede628.cpp b/ancestors/lant/ba846f2c-e53b-4c6f-af2c-044fbaede628.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/ba846f2c-e53b-4c6f-af2c-044fbaede628.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/bb09aa20-13f6-43c2-82c1-e7906b302c48.cpp b/ancestors/lant/bb09aa20-13f6-43c2-82c1-e7906b302c48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/bb09aa20-13f6-43c2-82c1-e7906b302c48.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/bb8613ad-5227-4bc6-80d5-02aacd15c6c1.cpp b/ancestors/lant/bb8613ad-5227-4bc6-80d5-02aacd15c6c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/bb8613ad-5227-4bc6-80d5-02aacd15c6c1.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/bc6109ea-13bb-4fa2-9da2-508af27c6e27.cpp b/ancestors/lant/bc6109ea-13bb-4fa2-9da2-508af27c6e27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/bc6109ea-13bb-4fa2-9da2-508af27c6e27.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/be467456-670d-4349-af82-750db8eead0a.cpp b/ancestors/lant/be467456-670d-4349-af82-750db8eead0a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/be467456-670d-4349-af82-750db8eead0a.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/be95fef1-1b79-46b8-b2d3-166da14118af.cpp b/ancestors/lant/be95fef1-1b79-46b8-b2d3-166da14118af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7f5b88f134ad42bdf46bc85a7c4dfef9bf3eabf --- /dev/null +++ b/ancestors/lant/be95fef1-1b79-46b8-b2d3-166da14118af.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/bff772b9-7b4b-4436-b8e5-b158ef38df6a.cpp b/ancestors/lant/bff772b9-7b4b-4436-b8e5-b158ef38df6a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/bff772b9-7b4b-4436-b8e5-b158ef38df6a.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/c1cd74b3-7c37-4c11-be3d-6301e70c9028.cpp b/ancestors/lant/c1cd74b3-7c37-4c11-be3d-6301e70c9028.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/c1cd74b3-7c37-4c11-be3d-6301e70c9028.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/c1e1a456-48bc-47e5-b92e-7680aaf8bee3.cpp b/ancestors/lant/c1e1a456-48bc-47e5-b92e-7680aaf8bee3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/c1e1a456-48bc-47e5-b92e-7680aaf8bee3.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/c1f7d348-d19a-4424-9151-00bbe2ddaeb1.cpp b/ancestors/lant/c1f7d348-d19a-4424-9151-00bbe2ddaeb1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/c1f7d348-d19a-4424-9151-00bbe2ddaeb1.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/c2c39f9c-5672-4427-9954-10e85506b9e2.cpp b/ancestors/lant/c2c39f9c-5672-4427-9954-10e85506b9e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/c2c39f9c-5672-4427-9954-10e85506b9e2.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/c352db3c-abaf-4a14-94dd-bd04ce2246be.cpp b/ancestors/lant/c352db3c-abaf-4a14-94dd-bd04ce2246be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/c352db3c-abaf-4a14-94dd-bd04ce2246be.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/c4f0a846-774b-438d-a036-a6fa0d429acc.cpp b/ancestors/lant/c4f0a846-774b-438d-a036-a6fa0d429acc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/c4f0a846-774b-438d-a036-a6fa0d429acc.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/c4faee72-c2ea-4dd9-acbe-a072019f2033.cpp b/ancestors/lant/c4faee72-c2ea-4dd9-acbe-a072019f2033.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/c4faee72-c2ea-4dd9-acbe-a072019f2033.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/c517390c-930d-462a-b7a7-0867a8a197d7.cpp b/ancestors/lant/c517390c-930d-462a-b7a7-0867a8a197d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/c517390c-930d-462a-b7a7-0867a8a197d7.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/c5515a14-9831-4050-9693-d733f1803f36.cpp b/ancestors/lant/c5515a14-9831-4050-9693-d733f1803f36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/c5515a14-9831-4050-9693-d733f1803f36.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/c6021fe7-975f-4491-af06-13284e60ddcf.cpp b/ancestors/lant/c6021fe7-975f-4491-af06-13284e60ddcf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/c6021fe7-975f-4491-af06-13284e60ddcf.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/c621ebbb-6fb3-4026-b79e-74433f4e8865.cpp b/ancestors/lant/c621ebbb-6fb3-4026-b79e-74433f4e8865.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/c621ebbb-6fb3-4026-b79e-74433f4e8865.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/c6b171ab-34b3-4ede-a5af-53139662be27.cpp b/ancestors/lant/c6b171ab-34b3-4ede-a5af-53139662be27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/c6b171ab-34b3-4ede-a5af-53139662be27.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/c93b45dc-d08e-4a7e-ada3-3e4fb03dfa49.cpp b/ancestors/lant/c93b45dc-d08e-4a7e-ada3-3e4fb03dfa49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/c93b45dc-d08e-4a7e-ada3-3e4fb03dfa49.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/cbd797c3-cf35-4ca9-9c85-58d5c8235983.cpp b/ancestors/lant/cbd797c3-cf35-4ca9-9c85-58d5c8235983.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/cbd797c3-cf35-4ca9-9c85-58d5c8235983.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/cc011282-75da-44e8-8434-38a63e9c0628.cpp b/ancestors/lant/cc011282-75da-44e8-8434-38a63e9c0628.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/cc011282-75da-44e8-8434-38a63e9c0628.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/cc64f876-92a7-455c-8e7c-22734b521290.cpp b/ancestors/lant/cc64f876-92a7-455c-8e7c-22734b521290.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/cc64f876-92a7-455c-8e7c-22734b521290.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/cd239458-dbf6-43b8-81ec-5e17b5486efd.cpp b/ancestors/lant/cd239458-dbf6-43b8-81ec-5e17b5486efd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/cd239458-dbf6-43b8-81ec-5e17b5486efd.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/cdc3ae45-788b-45ed-bea2-d189d54b91df.cpp b/ancestors/lant/cdc3ae45-788b-45ed-bea2-d189d54b91df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d300bbe1d970134b0b44744bee7a5b1adb2bbdc0 --- /dev/null +++ b/ancestors/lant/cdc3ae45-788b-45ed-bea2-d189d54b91df.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int main() { + + ifstream myinpf ("stramosi.in"); + + unsigned int N, M, i; + + myinpf >> N >> M; + + vector graph[N+1]; + + unsigned int anc_array[N+1], depth_array[N+1], anc; + + depth_array[0] = 0; + + for(i = 1 ; i < N + 1; i++) { + + myinpf >> anc; + + graph[anc].push_back(i); + + depth_array[i] = depth_array[anc] + 1; + + } + + vector< pair > querry_arr[N+1]; + + unsigned int a, b, results_arr[M]; + + for(i = 0 ; i < M ; i++) { + + myinpf >> a >> b; + + querry_arr[a].push_back(make_pair(b, i)); + + } + + myinpf.close(); + + stack s; + + s.push(0); + + anc_array[0] = 0; + + while(!s.empty()) { + a = s.top(); + s.pop(); + + anc_array[depth_array[a]] = a; + + for(vector::iterator t = graph[a].begin() ; t != graph[a].end() ; ++t) { + + for(vector< pair >::iterator p = querry_arr[*t].begin(); + p != querry_arr[*t].end() ; ++p ) { + + if(p->first >= depth_array[a] + 1) + results_arr[p->second] = 0; + else + results_arr[p->second] = anc_array[depth_array[a] + 1 - p->first]; + } + s.push(*t); + } + } + + ofstream myoutf ("stramosi.out"); + + unsigned int *ap = results_arr; + while(M--) { + myoutf << *ap << '\n'; + ap++; + } + + myoutf.close(); + + return 0; +} diff --git a/ancestors/lant/cdfdb951-711e-4cae-b7b0-80c491faa556.cpp b/ancestors/lant/cdfdb951-711e-4cae-b7b0-80c491faa556.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/cdfdb951-711e-4cae-b7b0-80c491faa556.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/cf10322f-9a06-44f7-aace-cd5521c400f4.cpp b/ancestors/lant/cf10322f-9a06-44f7-aace-cd5521c400f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/cf10322f-9a06-44f7-aace-cd5521c400f4.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/d05b85c0-2ce4-4c2e-b1d3-57c11b0b1ac8.cpp b/ancestors/lant/d05b85c0-2ce4-4c2e-b1d3-57c11b0b1ac8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/d05b85c0-2ce4-4c2e-b1d3-57c11b0b1ac8.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/d2eed302-6566-4282-a1f5-ed6fa0ad2829.cpp b/ancestors/lant/d2eed302-6566-4282-a1f5-ed6fa0ad2829.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/d2eed302-6566-4282-a1f5-ed6fa0ad2829.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/d36d3ef2-9628-4d99-ae75-2e8507656d8b.cpp b/ancestors/lant/d36d3ef2-9628-4d99-ae75-2e8507656d8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/d36d3ef2-9628-4d99-ae75-2e8507656d8b.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/d41feeda-2160-4dc8-81f1-8b2bb2ba4d34.cpp b/ancestors/lant/d41feeda-2160-4dc8-81f1-8b2bb2ba4d34.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/d41feeda-2160-4dc8-81f1-8b2bb2ba4d34.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/d42ccb95-adbb-481b-9fe5-7c6ce555d61d.cpp b/ancestors/lant/d42ccb95-adbb-481b-9fe5-7c6ce555d61d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/d42ccb95-adbb-481b-9fe5-7c6ce555d61d.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/d4cc74d1-777d-40b3-8fce-aea91d50d7a1.cpp b/ancestors/lant/d4cc74d1-777d-40b3-8fce-aea91d50d7a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3516955c6c47dbd29b065e38f096a72791d7e2a2 --- /dev/null +++ b/ancestors/lant/d4cc74d1-777d-40b3-8fce-aea91d50d7a1.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/d515af7f-e8e6-4ce2-953b-dd6837d266c5.cpp b/ancestors/lant/d515af7f-e8e6-4ce2-953b-dd6837d266c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/d515af7f-e8e6-4ce2-953b-dd6837d266c5.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/d54e8dfa-39ac-4874-a7d2-5334993130a0.cpp b/ancestors/lant/d54e8dfa-39ac-4874-a7d2-5334993130a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/d54e8dfa-39ac-4874-a7d2-5334993130a0.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/d5c65721-3aa3-4773-b4fa-8527db804bfe.cpp b/ancestors/lant/d5c65721-3aa3-4773-b4fa-8527db804bfe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/d5c65721-3aa3-4773-b4fa-8527db804bfe.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/d6284273-7780-48bb-8757-ad0b29c68050.cpp b/ancestors/lant/d6284273-7780-48bb-8757-ad0b29c68050.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/d6284273-7780-48bb-8757-ad0b29c68050.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/dba9fa0a-9ef3-407c-8715-923af8ec3724.cpp b/ancestors/lant/dba9fa0a-9ef3-407c-8715-923af8ec3724.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/dba9fa0a-9ef3-407c-8715-923af8ec3724.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/dbabcdc5-de31-43c1-9f40-522f88e5ed5b.cpp b/ancestors/lant/dbabcdc5-de31-43c1-9f40-522f88e5ed5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/dbabcdc5-de31-43c1-9f40-522f88e5ed5b.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/ddf6ad4e-0058-4628-9314-55663a7f75f0.cpp b/ancestors/lant/ddf6ad4e-0058-4628-9314-55663a7f75f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8294ce718b2cf129bb4099f5a6f0f339417af55 --- /dev/null +++ b/ancestors/lant/ddf6ad4e-0058-4628-9314-55663a7f75f0.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + +pair coresp[250100]; + +vector lant[250100]; + +bool f[250100]; + +int v[250100], lung[250100]; + +int n, m, a, l; + + + +void build(int i) { + + coresp[i] = make_pair(l, lung[l]++); + + lant[l].push_back(i); + + + + if (i == 0) + + return; + + + + if (coresp[v[i]].first) { + + int nou = coresp[v[i]].first; + + int sf = lung[nou]; + + + + for (int h = coresp[v[i]].second; h < sf; h++) + + lant[l].push_back(lant[nou][h]), lung[l]++; + + + + return; + + } + + + + build(v[i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + fin >> a; + + v[i] = a; + + f[a] = true; + + } + + + + for (int i = 1; i <= n; i++) + + if (!f[i]) { + + ++l; + + build(i); + + } + + + + for (int i = 1; i <= m; i++) { + + int q, p; + + fin >> q >> p; + + fout << lant[coresp[q].first][min(lung[coresp[q].first] - 1, coresp[q].second + p)] << '\n'; + + } + + return 0; + +} diff --git a/ancestors/lant/de1d6427-7110-4d41-8cf7-91030fdf2c90.cpp b/ancestors/lant/de1d6427-7110-4d41-8cf7-91030fdf2c90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7047b8abd23e32c749c42d69392cb272cc03f4 --- /dev/null +++ b/ancestors/lant/de1d6427-7110-4d41-8cf7-91030fdf2c90.cpp @@ -0,0 +1,140 @@ +#include +#include +using namespace std; +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +struct nod{ + + int index; + + vector sons; + + }v[250003]; + + + +struct wee{ + + int poz, p; + + }; + + + +vector q[250003]; + + + +int sol[300003], n, m; + + + +bool b[250003]; + + + +void DFSsolve(int x) + +{ + + int Stack[250003], vf=0; + + Stack[++vf]=x; + + for(int i=0;i=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + while(vf) + + { + + if(v[Stack[vf]].index=vf) sol[q[Stack[vf]][i].poz]=0; + + else sol[q[Stack[vf]][i].poz]=Stack[vf-q[Stack[vf]][i].p]; + + } + + } + + else vf--; + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;++i) + + { + + int x; + + fin>>x; + + v[x].sons.push_back(i); + + v[i].index=0; + + if(x==0) b[i]=1; + + } + + for(int i=1;i<=m;++i) + + { + + int x, y; + + fin>>x>>y; + + wee z; + + z.poz=i, z.p=y; + + q[x].push_back(z); + + } + + for(int i=1;i<=n;++i) + + if(b[i]) DFSsolve(i); + + for(int i=1;i<=m;++i) fout< +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/dfa0f99b-e013-4630-b31f-b1d3f36d92dc.cpp b/ancestors/lant/dfa0f99b-e013-4630-b31f-b1d3f36d92dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba017af54c4d65afb17133df4c873b0921445e21 --- /dev/null +++ b/ancestors/lant/dfa0f99b-e013-4630-b31f-b1d3f36d92dc.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + +int N,M,a,b,root,Cont[250006],maxim,cnt; + +int P,Q,Deep[250006]; + +bool ok,viz[250006],viz2[250006]; + + + +vector < int > V[250006]; + +set < pair > S[250006]; + +set < pair > ::iterator it; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + maxim++; + + Cont[nod]=maxim; + + for(unsigned int i=0;i > qu; + + viz2[nod]=1; + + qu.push({nod,1}); + + S[1].insert({Cont[nod],nod}); + + Deep[nod]=1; + + while(!qu.empty()) + + { + + int vertex=qu.front().first; + + int niv=qu.front().second+1; + + viz2[vertex]=1; + + qu.pop(); + + for(unsigned int i=0;i>N>>M; + + for(int i=1;i<=N;i++) + + { + + f>>a; + + V[a].push_back(i); + + if(!a) ok=1; + + } + + if(!ok) root=1; + + DFS(root); + + BFS(root); + + for(int i=1;i<=M;i++) + + { + + f>>Q>>P; + + if(Deep[Q]-P<=0) g<<0<<'\n'; + + else + + { + + int level=Deep[Q]-P; + + int ID=Cont[Q]; + + it=S[level].lower_bound({ID,level}); + + it--; + + g<<(it->second)<<'\n'; + + } + + } + + return 0; + +} diff --git a/ancestors/lant/e0abcf75-682a-4758-96a1-9d4275701843.cpp b/ancestors/lant/e0abcf75-682a-4758-96a1-9d4275701843.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/e0abcf75-682a-4758-96a1-9d4275701843.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/e1df3f7f-c68b-45cc-ab6c-1075f0634b52.cpp b/ancestors/lant/e1df3f7f-c68b-45cc-ab6c-1075f0634b52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..165cd597ebe61a56dfae7a4bc2976bf21018f031 --- /dev/null +++ b/ancestors/lant/e1df3f7f-c68b-45cc-ab6c-1075f0634b52.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::vector frontier_impl{}; + + frontier_impl.reserve(N / 2); + + std::stack> frontier{std::move(frontier_impl)}; + + + + std::vector path; + + path.reserve(N + 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/e2f7bd79-f79b-4c78-ae61-4ce2fea4110e.cpp b/ancestors/lant/e2f7bd79-f79b-4c78-ae61-4ce2fea4110e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/e2f7bd79-f79b-4c78-ae61-4ce2fea4110e.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/e31e0749-bd16-423f-a5f5-49d8cbfdbb26.cpp b/ancestors/lant/e31e0749-bd16-423f-a5f5-49d8cbfdbb26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/e31e0749-bd16-423f-a5f5-49d8cbfdbb26.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/e475656b-6ed4-4c62-83f0-264186987a49.cpp b/ancestors/lant/e475656b-6ed4-4c62-83f0-264186987a49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c0828952ec2126dba3c680eb886396c871e631 --- /dev/null +++ b/ancestors/lant/e475656b-6ed4-4c62-83f0-264186987a49.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +int main() { + ifstream cin("stramosi.in"); + ofstream cout("stramosi.out"); + + int n, q; cin >> n >> q; + vector parent(n), depth(n), jump(n); + for (int i = 0; i < n; ++i) { + int par; cin >> par; --par; + parent[i] = par; + if (par == -1) { + depth[i] = 0; + jump[i] = i; + } else { + depth[i] = 1 + depth[par]; + jump[i] = (depth[par] - depth[jump[par]] == + depth[jump[par]] - depth[jump[jump[par]]] + ? jump[jump[par]] : par); + } + } + for (int i = 0; i < q; ++i) { + int node, k; cin >> node >> k; --node; + int target = depth[node] - k; + if (target < 0) { + cout << 0 << '\n'; + } else { + while (depth[node] > target) { + if (depth[jump[node]] >= target) + node = jump[node]; + else node = parent[node]; + } + cout << node + 1 << '\n'; + } + } + return 0; +} diff --git a/ancestors/lant/e54a680b-10fe-4720-b9ee-cef21d3a9170.cpp b/ancestors/lant/e54a680b-10fe-4720-b9ee-cef21d3a9170.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ec3b933a27c819e9b987c8891b14076009bc4ad --- /dev/null +++ b/ancestors/lant/e54a680b-10fe-4720-b9ee-cef21d3a9170.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in("stramosi.in"); + +ofstream out("stramosi.out"); + + + +const int DIM = 250007; + + + +int p[DIM]; + + + +int h[DIM]; + + + +bool viz[DIM]; + + + +int fr[DIM]; + + + +vector < int > d[DIM]; + + + +void parinti(int x,int y) + +{ + + fr[x] = y; + + + + if(p[x] != 0) + + parinti(p[x],y); + + + + h[x] = h[p[x]] + 1; + + d[y].push_back(x); + +} + + + +int main() + +{ + + + + int n, m; + + + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + { + + in >> p[i]; + + viz[p[i]] = true; + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i] == false) + + { + + parinti(i,i); + + + + } + + } + + + + + + while(m--) + + { + + int x, y; + + + + in >> x >> y; + + + + int pos = h[x]; + + + + + + pos -= y; + + + + if(pos <= 0) + + out << 0 <<'\n'; + + else + + { + + out << d[fr[x]][pos - 1]<<'\n'; + + } + + + + + + } + + + + + + + + return 0; + +} diff --git a/ancestors/lant/e9870d69-b0c3-4b66-b680-6faa4b2cadac.cpp b/ancestors/lant/e9870d69-b0c3-4b66-b680-6faa4b2cadac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66df2cf5693268c17cd3d13a3780e158126f8627 --- /dev/null +++ b/ancestors/lant/e9870d69-b0c3-4b66-b680-6faa4b2cadac.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("stramosi.in"); + +ofstream g("stramosi.out"); + + + +int n, m, top, stiva[250000 +10], ans[300000 +10]; + +bool b[250000 +10]; + +vector nod[250000 +10]; + +vector > query[250000 +10]; + + + +void DFS(int x) + +{ + + b[x] = 1; + + stiva[++top] = x; + + for(int i=0; i> n >> m; + + for(int i=1; i<=n; i++) + + { int x; + + f >> x; + + if(x) + + { nod[i].push_back(x); + + nod[x].push_back(i); + + } + + } + + for(int i=1; i<=m; i++) + + { int x, y; + + f >> x >> y; + + query[x].push_back(make_pair(y, i)); + + } + + for(int i=1; i<=n; i++) if(!b[i]) DFS(i); + + for(int i=1; i<=m; i++) g << ans[i] << '\n'; + + return 0; + +} diff --git a/ancestors/lant/e98aeed4-8c35-4a0f-a376-ada3f241cb49.cpp b/ancestors/lant/e98aeed4-8c35-4a0f-a376-ada3f241cb49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/e98aeed4-8c35-4a0f-a376-ada3f241cb49.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/eab1dc15-252b-46f3-ad9a-687c3881491b.cpp b/ancestors/lant/eab1dc15-252b-46f3-ad9a-687c3881491b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92d1c09499d3fe94690298344b5abed60576727 --- /dev/null +++ b/ancestors/lant/eab1dc15-252b-46f3-ad9a-687c3881491b.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int answers[300001]; + +int level[250002]; + +int n, m; + + + + + +struct Query + +{ + + int degree; + + int ord; + + + + Query(int _deg, int _ord) : degree(_deg), ord(_ord) {} + +}; + + + +struct Node + +{ + + vector queries; + + vector descendants; + +} nodes[250002]; + + + +void read() + +{ + + scanf("%d", &n); + + scanf("%d", &m); + + for(int i = 1; i <= n; ++i) + + { + + int parent; + + scanf("%d", &parent); + + nodes[parent].descendants.push_back(i); + + } + + + + for(int i = 0; i < m; ++i) + + { + + int p, q; + + + + scanf("%d", &q); + + scanf("%d", &p); + + + + nodes[q].queries.push_back(Query(p, i)); + + } + +} + + + +void solve(int node, int depth) + +{ + + level[depth] = node; + + + + Node & currentNode = nodes[node]; + + for(int i = 0; i < currentNode.queries.size(); ++i) + + { + + Query & currentQuery = currentNode.queries[i]; + + answers[currentQuery.ord] = level[max(depth - currentQuery.degree, 0)]; + + } + + + + for(int i = 0; i < currentNode.descendants.size(); ++i) + + { + + solve(currentNode.descendants[i], depth + 1); + + } + +} + + + +void printAnswers() + +{ + + for(int i = 0; i < m; ++i) + + printf("%d\n", answers[i]); + +} + + + +int main() { + + freopen("stramosi.in", "r", stdin); + + freopen("stramosi.out", "w", stdout); + + + + read(); + + solve(0, 0); + + printAnswers(); + + + + return 0; + +} diff --git a/ancestors/lant/ec6fa56d-abf7-4782-a880-e3e651e9fdf8.cpp b/ancestors/lant/ec6fa56d-abf7-4782-a880-e3e651e9fdf8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/ec6fa56d-abf7-4782-a880-e3e651e9fdf8.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/ee06e58a-65bb-4d91-bd2a-3ba34caa573f.cpp b/ancestors/lant/ee06e58a-65bb-4d91-bd2a-3ba34caa573f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916bb5c00db19f928732d37c8af0a71c6c06f204 --- /dev/null +++ b/ancestors/lant/ee06e58a-65bb-4d91-bd2a-3ba34caa573f.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +using namespace std; + + + +int n, m; + +int tata[427222]; + +int st[427222]; + +bool viz[427222]; + +vector< vector > a; + +vector< vector > A; + +vector< vector > sol; + +int q[427222]; + + + +int T[427222][3]; + +int t; + +int poz[427222]; + + + +void citire() + +{ + + freopen("stramosi.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + int i; + + for(i=1;i<=n;i++) scanf("%d ", &tata[i]); + + A.reserve(n+1); + + a.reserve(n+1); + + sol.reserve(n+1); + + A.resize(n+1); + + a.resize(n+1); + + sol.resize(n+1); + + + + + + for(i=1;i<=m;i++) + + { + + int xx, yy; + + scanf("%d %d\n", &xx, &yy); + + q[xx]++; + + + + + + T[++t][1]=xx; + + T[t][2]=yy; + + } + + for(i=1;i<=n;i++) + + { + + a[i].reserve(q[i]); + + A[i].reserve(q[i]); + + sol[i].reserve(q[i]); + + } + + for(i=1;i<=m;i++) A[T[i][1]].push_back(T[i][2]); + + + + for(i=1;i<=n;i++) if(tata[i])a[tata[i]].push_back(i); + + + +} + +void dfs(int nod, int k) + +{ + + + + int i; + + st[k]=nod; + + viz[nod]=1; + + + + for(i=0;i +#include +#include + + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE* fin; + + char* buff; + + int sp; + + char read() + + { + + ++sp; + + if (sp == 4096) + + { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + +public: + + InParser(const char* nume) + + { + + sp = 4095; + + fin = fopen(nume, "r"); + + buff = new char[4096]; + + } + + InParser& operator >> (int& n) + + { + + char c; + + while (!isdigit(c = read())); + + n = c - '0'; + + while (isdigit(c = read())) + + n = n * 10 + c - '0'; + + return *this; + + } + +}; + + + +InParser fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +const int NMAX = 250003; + + + +typedef pair p; + + + +vector g[NMAX]; + +vector

q[NMAX]; + +vector rad; + +vector query; + +int n, m, a, b; + +vector st; + + + +void DFS(int nod) + +{ + + st.push_back(nod); + + for (size_t i = 0; i < g[nod].size(); ++i) + + { + + int fiu = g[nod][i]; + + for (size_t j = 0; j < q[fiu].size(); ++j) + + { + + int ind = q[fiu][j].second, str = q[fiu][j].first; + + + + if (str == 0) + + query[ind] = fiu; + + + + else if (str <= st.size()) + + query[ind] = st[st.size() - str]; + + } + + DFS(fiu); + + } + + st.pop_back(); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + st.reserve(n); + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + if (a == 0) rad.push_back(i); + + else g[a].push_back(i); + + } + + + + query.resize(m); + + + + for (int i = 0; i < m; ++i) + + { + + fin >> a >> b; + + q[a].push_back({ b, i }); + + } + + + + for (size_t i = 0; i < rad.size(); ++i) + + DFS(rad[i]); + + + + for (int i = 0; i < m; ++i) + + fout << query[i] << "\n"; + + return 0; + +} diff --git a/ancestors/lant/f2ed77ca-764f-475d-853f-97c1d5b6f50e.cpp b/ancestors/lant/f2ed77ca-764f-475d-853f-97c1d5b6f50e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/f2ed77ca-764f-475d-853f-97c1d5b6f50e.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/f4981a7b-f482-4293-ab7e-98e47faa2964.cpp b/ancestors/lant/f4981a7b-f482-4293-ab7e-98e47faa2964.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaa314bcac91358940a0a589e81d665aa61747d --- /dev/null +++ b/ancestors/lant/f4981a7b-f482-4293-ab7e-98e47faa2964.cpp @@ -0,0 +1,74 @@ +#include +std::ifstream fin("stramosi.in"); + +std::ofstream fout("stramosi.out"); + +typedef long long ll; + +ll n, m; + +ll sol[300005]; + +std::vector graph[250005]; + +std::vector > query[250005]; + +std::vector list; + +void solve(ll node); + +int main() + +{ + + fin>>n>>m; + + for(int i=1; i<=n; ++i){ + + ll x; + + fin>>x; + + graph[x].push_back(i); + + } + + for(int i=1; i<=m; ++i){ + + ll x, y; + + fin>>x>>y; + + query[x].push_back({y+1, i}); + + } + + solve(0); + + for(int i=1; i<=m; ++i) fout<list.size()-1) continue; + + else sol[i.second]=list[list.size()-i.first]; + + } + + } + + for(auto next:graph[node]) solve(next); + + list.pop_back(); + +} diff --git a/ancestors/lant/f4c0149b-8a05-45ec-b02c-86894eed811f.cpp b/ancestors/lant/f4c0149b-8a05-45ec-b02c-86894eed811f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/f4c0149b-8a05-45ec-b02c-86894eed811f.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/f9291ae9-eb2e-42a1-9462-0105d4ccc22c.cpp b/ancestors/lant/f9291ae9-eb2e-42a1-9462-0105d4ccc22c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9179c00781c829bf29b68aac509ae93df01ada6f --- /dev/null +++ b/ancestors/lant/f9291ae9-eb2e-42a1-9462-0105d4ccc22c.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +int n, m, i, out[300001]; + +vector graph[250001]; + +vector v, rad; + +vector > query[250001]; + + + +void read(); + +void solve(); + +void dfs(int node); + +void write(); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("stramosi.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=n; ++i){ + + int head; + + scanf("%d", &head); + + if(head) graph[head].push_back(i); + + else rad.push_back(i); + + } + + for(i=1; i<=m; ++i){ + + int node, p; + + scanf("%d%d", &node, &p); + + query[node].push_back({p, i}); + + } + + return; + +} + +void solve(){ + + for(auto j:rad) dfs(j); + + return; + +} + +void write(){ + + freopen("stramosi.out", "w", stdout); + + for(i=1; i<=m; ++i) printf("%d\n", out[i]); + + return; + +} + +void dfs(int node){ + + for(auto i:query[node]){ + + if(i.first<=v.size()) out[i.second]=v[v.size()-i.first]; + + else out[i.second]=0; + + } + + v.push_back(node); + + for(auto i:graph[node]) dfs(i); + + v.pop_back(); + + return; + +} diff --git a/ancestors/lant/f971717c-e203-4d48-aa62-67a24ec04175.cpp b/ancestors/lant/f971717c-e203-4d48-aa62-67a24ec04175.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/f971717c-e203-4d48-aa62-67a24ec04175.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< + +using namespace std; +using ll = long long; +using ull = unsigned long long; +using uint = unsigned int; +using ld = long double; +using pii = pair; +using pll = pair; +using pld = pair; + +const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; +const int inf_int = 1e9 + 5; +const ll inf_ll = 1e18 + 5; +const int NMax = 3e5 + 5; +const int dx[] = {-1,0,0,+1}, dy[] = {0,-1,+1,0}; +const double EPS = 1e-8; + +int ans[NMax]; +bool vis[NMax]; +vector stk; +vector query[NMax]; +vector adj[NMax]; + +void dfs(int node) { + + for (pii p : query[node]) { + int pth = p.first, idx = p.second; + if (pth > (int)stk.size()) { + ans[idx] = 0; + } + else { + pth = (int)stk.size() - pth; + ans[idx] = stk[pth]; + } + } + + stk.push_back(node); + + for (int nxt : adj[node]) { + dfs(nxt); + } + + stk.pop_back(); +} + +int main() { + cin.sync_with_stdio(false); + cin.tie(0); + + int N, M; + cin >> N >> M; + for (int i = 1; i <= N; ++i) { + int dad; + cin >> dad; + adj[dad].push_back(i); + } + + for (int i = 1; i <= M; ++i) { + int node, pth; + cin >> node >> pth; + query[node].push_back({pth, i}); + } + + dfs(0); + + for (int i = 1; i <= M; ++i) { + cout << ans[i] << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/fd5e6dfa-2b05-4947-afe3-3cf1fba01c76.cpp b/ancestors/lant/fd5e6dfa-2b05-4947-afe3-3cf1fba01c76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8467f703931909f2b966e2bcfa66d84757aa7a3 --- /dev/null +++ b/ancestors/lant/fd5e6dfa-2b05-4947-afe3-3cf1fba01c76.cpp @@ -0,0 +1,81 @@ +#include +using namespace std; + +namespace Tree +{ + vector > adia; + vector h, poz_in_dfs_order, dfs_order, g, link, father; + + void calc_g(int node) + { + g[node] = 1; + for (int i = 0; i < (int)adia[node].size(); i++) { + calc_g(adia[node][i]); + g[node] += g[adia[node][i]]; + if (g[adia[node][i]] > g[adia[node][0]]) + swap(adia[node][i], adia[node][0]); + } + } + + void make_dfs(int node, int lnk, int fth) + { + father[node] = fth; + h[node] = 1 + h[fth]; + link[node] = lnk; + poz_in_dfs_order[node] = dfs_order.size(); + dfs_order.push_back(node); + + for (auto i : adia[node]) + make_dfs(i, (i == adia[node][0] ? lnk : i), node); + } + + void init(int n) + { + adia.resize(n + 1); + h = poz_in_dfs_order = dfs_order = g = link = father = vector (n + 1); + } + + int query(int node, int dist) + { + while (true) { + if (!node) + return 0; + if (h[node] - h[link[node]] >= dist) + return dfs_order[poz_in_dfs_order[node] - dist]; + dist -= h[node] - h[link[node]] + 1; + node = father[link[node]]; + } + } +} + +int main() +{ + ifstream in("stramosi.in"); + ofstream out("stramosi.out"); + + int n, m; + in >> n >> m; + + Tree::init(n); + + for (int i = 1; i <= n; i++) { + int father; + in >> father; + Tree::adia[father].push_back(i); + } + + for (int i = 1; i <= n; i++) { + if (!Tree::g[i]) { + Tree::calc_g(i); + Tree::make_dfs(i, i, 0); + } + } + + while (m--) { + int node, ord; + in >> node >> ord; + out << Tree::query(node, ord) << '\n'; + } + + return 0; +} diff --git a/ancestors/lant/fdbb5122-0abf-47f0-96ed-32451ffb26dd.cpp b/ancestors/lant/fdbb5122-0abf-47f0-96ed-32451ffb26dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb25cd1e7c68c69c92749fe981dfa4b572c52ccd --- /dev/null +++ b/ancestors/lant/fdbb5122-0abf-47f0-96ed-32451ffb26dd.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int N, M, viz[250002], sol[300005]; + + + +vector graph[250002], s; + +queue Q; + + + +struct str{ + + int val, poz; + +}; + + + +vector v[250002]; + + + +inline void Read() { + + int x, y; + + + + fin >> N >> M; + + + + for (int i = 1; i <= N; i++) { + + fin >> x; + + if (!x) { + + Q.push(i); + + } + + else { + + graph[x].push_back(i); + + } + + } + + + + for (int i = 1; i <= M; i++){ + + fin >> x >> y; + + + + v[x].push_back({y, i}); + + } + +} + + + +inline void DFS(int node) { + + viz[node] = 1; + + if (!v[node].empty()) { + + int ll = s.size(); + + for (auto j : v[node]) { + + if (ll - j.val >= 0) + + sol[j.poz] = s[ll - j.val]; + + } + + } + + s.push_back(node); + + + + for (auto x : graph[node]) { + + if (!viz[x]) { + + DFS(x); + + } + + } + + s.pop_back(); + + } + + + +inline void Solve() { + + int z; + + while (!Q.empty()) { + + z = Q.front(); + + Q.pop(); + + if (viz[z]) + + continue; + + s = vector (); + + DFS(z); + + } + +} + + + +inline void Write() { + + for (int i = 1; i <= M; i++) + + fout << sol[i] << "\n"; + +} + + + +int main () { + + Read(); + + Solve(); + + Write(); + + + + fin.close(); fout.close(); return 0; + +} diff --git a/ancestors/lant/fea12517-8e8a-4e6e-81f5-c332b539f014.cpp b/ancestors/lant/fea12517-8e8a-4e6e-81f5-c332b539f014.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f17ef2bbec24075b6cec83c3dd0a25787a50f807 --- /dev/null +++ b/ancestors/lant/fea12517-8e8a-4e6e-81f5-c332b539f014.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +constexpr const char newl = '\n'; + + + +struct Question + +{ + + int person_id; + + int ancestor_level; + + int ancestor_id; + +}; + + + +struct Problem + +{ + + int N, M; + + std::vector parents; + + std::vector> children; + + std::vector questions; + + + + std::unordered_map>> questions_by_person; + + + + auto read_input() + + { + + std::ifstream in{"stramosi.in"}; + + assert(in); + + + + in >> N >> M; + + assert(in); + + + + parents.resize(N + 1); + + children.resize(N + 1); + + + + parents[0] = 0; + + + + + + for (int person_id = 1; person_id < static_cast(parents.size()); ++person_id) + + { + + int parent_id; + + in >> parent_id; + + assert(in); + + + + parents[person_id] = parent_id; + + + + children[parent_id].push_back(person_id); + + } + + + + questions.resize(M); + + + + + + for (Question& question : questions) + + { + + in >> question.person_id >> question.ancestor_level; + + assert(in); + + + + questions_by_person[question.person_id].emplace_back(question); + + } + + } + + + + auto solve_impl() + + { + + std::stack frontier; + + std::vector path; + + path.reserve(N >> 1); + + + + frontier.push(0); + + + + while (!frontier.empty()) + + { + + + + int selected_person_id = frontier.top(); + + frontier.pop(); + + + + + + auto parent_of_selected_it = + + std::find(path.rbegin(), path.rend(), parents[selected_person_id]); + + if (parent_of_selected_it != path.rend() && + + (parent_of_selected_it + 1).base() != path.end() && + + (parent_of_selected_it + 1).base() + 1 != path.end()) + + { + + path.erase((parent_of_selected_it + 1).base() + 1, path.end()); + + } + + path.push_back(selected_person_id); + + + + + + auto it = questions_by_person.find(selected_person_id); + + if (it != questions_by_person.end()) + + { + + for (Question& question : it->second) + + { + + int path_idx = static_cast(path.size() - 1 - question.ancestor_level); + + if (path_idx <= 0) + + question.ancestor_id = 0; + + else + + question.ancestor_id = path[path_idx]; + + } + + } + + + + + + for (int& child : children[selected_person_id]) + + { + + frontier.push(child); + + } + + } + + } + + + + auto write_out() + + { + + std::ofstream out{"stramosi.out"}; + + assert(out); + + + + for (auto& question : questions) + + { + + out << question.ancestor_id << newl; + + assert(out); + + } + + } + + + + auto solve() + + { + + read_input(); + + solve_impl(); + + write_out(); + + } + +}; + + + +int main() + +{ + + Problem problem; + + + + problem.solve(); + +} diff --git a/ancestors/lant/ffdab0b1-ab5b-40c7-9b76-523f35b9d8ab.cpp b/ancestors/lant/ffdab0b1-ab5b-40c7-9b76-523f35b9d8ab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85e0c0b4da30b9df4c9c06b636157f345a005a0 --- /dev/null +++ b/ancestors/lant/ffdab0b1-ab5b-40c7-9b76-523f35b9d8ab.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("stramosi.in"); + +ofstream fout("stramosi.out"); + + + +int TT[250005]; + +int neFr[250005]; + +int Fr[250005]; + +int h[250005]; + +vector G[250005]; + +int fr; + + + +void parinte(int nod){ + + Fr[nod] = fr; + + while(nod != 0){ + + h[TT[nod]] = h[nod] + 1; + + Fr[nod] = fr; + + G[fr].push_back(nod); + + nod = TT[nod]; + + } + +} + + + +int main () + +{ + + int n,m,i,a,b,poz; + + fin>>n>>m; + + for(i = 1; i <= n; i++){ + + fin>>TT[i]; + + neFr[TT[i]] = 1; + + } + + for(i = 1; i <= n; i++){ + + if(neFr[i] == 0){ + + fr = i; + + parinte(i); + + } + + } + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + poz = b + h[a]; + + if(poz >= G[Fr[a]].size()){ + + fout<<'0'<<'\n'; + + }else{ + + fout< +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 2; i <= n; ++ i) + for (int j = i - 1; j && points[j].real() > points[i].real() - ans - 1e-8; -- j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/04202e7b-99e3-41fc-87b4-896e3e91aba2.cpp b/cmap/distance_search/04202e7b-99e3-41fc-87b4-896e3e91aba2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19fc216784684349394fd26fed536ce9cb93a912 --- /dev/null +++ b/cmap/distance_search/04202e7b-99e3-41fc-87b4-896e3e91aba2.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< + + +using namespace std; + + + +pair < double , double > v[100002]; + + + +inline double dist(int i, int j) + +{ + + return sqrt((v[i].first-v[j].first)*(v[i].first-v[j].first)+(v[i].second-v[j].second)*(v[i].second-v[j].second)); + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + + + double vmin=dist(1,2); + + for(int i=1;i<=n-1;i++) + + for(int j=i+1;j<=n && fabs(v[j].first-v[i].first)<=vmin;j++) + + { + + double x=dist(i,j); + + if(x + + + + + + +using namespace std; + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +int n; + +struct Point + +{ + + int x, y; + + bool operator < (const Point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100002]; + +set < Point > s; + + + +long long sqr(int x) + +{ + + return 1LL * x * x; + +} + +double dist(Point a, Point b) + +{ + + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); + +} + +double h; + + + +int main() + +{ + + scanf("%d", &n); + + for (int i = 0; i < n; ++ i) + + scanf("%d%d", &v[i].x, &v[i].y); + + auto it = 0; + + sort(v, v + n); + + h = dist(v[0], v[1]); + + for (int i = 0; i < n; ++ i) + + { + + while (it < i && h <= (double)v[i].x - v[it].x) + + { + + s.erase(s.find(Point{v[it].y, v[it].x})); + + ++ it; + + } + + int H = (int)(h); + + auto it1 = s.lower_bound(Point{v[i].y - H,-0x3fffffff}), + + it2 = s.upper_bound(Point{v[i].y + H, 0x3fffffff}); + + if (it2 != s.begin() && it1 != s.end()) + + { + + for (auto j = it1; j != it2; ++ j) + + h = min(h, dist(v[i], Point{it1->y, it1->x})); + + } + + s.insert(Point{v[i].y, v[i].x}); + + } + + + + printf("%.7lf", h); + + return 0; + +} diff --git a/cmap/distance_search/0630fbb7-6497-4b1a-b049-4a9ee82518e1.cpp b/cmap/distance_search/0630fbb7-6497-4b1a-b049-4a9ee82518e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddb0e5b67a0cefa78e5ff430fe2d32e2a5beb772 --- /dev/null +++ b/cmap/distance_search/0630fbb7-6497-4b1a-b049-4a9ee82518e1.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/0b5aed58-5468-44d3-809c-150bdab6dc05.cpp b/cmap/distance_search/0b5aed58-5468-44d3-809c-150bdab6dc05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d47fe8e8010e48ef17102296da777a3b798691 --- /dev/null +++ b/cmap/distance_search/0b5aed58-5468-44d3-809c-150bdab6dc05.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< + + +using namespace std; + +typedef pair pairll; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pairll P[100001]; + +int lef; + +set S; + +long double dmin; + + + +int cmp(pairll a, pairll b) + +{ + + if (a.secondb.second) + + return 0; + + return a.first>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].second>>P[i].first; + + sort(P+1,P+N+1,cmp); + + + + lef=1; + + S.insert(P[1]); + + dmin=(long double)1e10; + + for (int i=2;i<=N;i++) + + { + + + + while (lefdmin) + + { + + S.erase(P[lef]); + + lef++; + + } + + + + set :: iterator it; + + it=S.lower_bound({(long long)(P[i].first-dmin),(long long)(P[i].second-dmin)}); + + int nr; + + nr=1; + + while (it!=S.end() && nr<=5) + + { + + dmin=min(dmin,distanta(P[i],*it)); + + it++; + + nr++; + + } + + S.insert(P[i]); + + } + + fo< +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/0fa9a6bd-a695-4c18-80c8-00e3874b50e9.cpp b/cmap/distance_search/0fa9a6bd-a695-4c18-80c8-00e3874b50e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19fc216784684349394fd26fed536ce9cb93a912 --- /dev/null +++ b/cmap/distance_search/0fa9a6bd-a695-4c18-80c8-00e3874b50e9.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< + + + + + + +using namespace std; + + + +pair v[100001]; + +double distanta(pair n, pair m) + +{ + + pair a = {n.first,n.second},b = {m.first,m.second}; + + return sqrt((b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second)); + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +set > s; + +int main() + +{ + + InParser cin("cmap.in"); + + ofstream cout("cmap.out"); + + ios_base::sync_with_stdio(false); + + int n,i; + + cin >> n; + + for(i = 1; i <= n; i++) + + { + + cin >> v[i].first >> v[i].second; + + + + } + + double minim = (double)(1LL << 60); + + sort(v + 1,v + n + 1); + + int j = 1; + + for(i = 1; i <= n; i++) + + { + + int cv = sqrt(ceil(minim)); + + while(v[i].first - v[j].first >= minim) + + { + + s.erase({v[j].second,v[j].first}); + + j++; + + } + + auto it1 = s.lower_bound({v[i].second - cv, v[i].first}); + + auto it2 = s.upper_bound({v[i].second + cv, v[i].first}); + + + + for (auto it = it1; it != it2; ++it) + + { + + int dx = v[i].first - it->second; + + int dy = v[i].second - it->first; + + minim = min(minim, 1.0 * dx * dx + 1.0 * dy * dy); + + } + + s.insert({v[i].second, v[i].first}); + + } + + cout << fixed << setprecision(6) << sqrt(minim); + + return 0; + +} diff --git a/cmap/distance_search/12016130-0b98-4b52-b245-19470238bb40.cpp b/cmap/distance_search/12016130-0b98-4b52-b245-19470238bb40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0a758bf9f2d80820fb94f5402b359102869f084 --- /dev/null +++ b/cmap/distance_search/12016130-0b98-4b52-b245-19470238bb40.cpp @@ -0,0 +1,94 @@ +#include + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +pair a[100005]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[100001]; + +int N; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +bool comp(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + long long dx = A.x - B.x; + + long long dy = A.y - B.y; + + return dx * dx + dy * dy; + +} + + + +double solutie() + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = 1; i <= N; i++) + + for(int j = i + 1; j <= i + 7 && j <= N; j++) + + { + + long long d = dist2(P[i], P[j]); + + if(d < dmin) dmin = d; + + } + + return sqrt(dmin); + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, comp); + + g << fixed << setprecision(6) << solutie(); + + return 0; + +} diff --git a/cmap/distance_search/14499191-8966-4e1e-b11a-e9ccd1e97f86.cpp b/cmap/distance_search/14499191-8966-4e1e-b11a-e9ccd1e97f86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..086777ed307ce24a2e1a529a5209259880f7d43c --- /dev/null +++ b/cmap/distance_search/14499191-8966-4e1e-b11a-e9ccd1e97f86.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + +double maxim(double a, double b) + +{ + + if(a>n; + + for(int i=1;i<=n;i++) + + { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+n+1,sortare); + + double d = calcdist(1,n); + + for(int i=1;i<=n;i++) + + for(int j=i+1;j<=n && v[j].x-v[i].x<=d; j++) + + { + + d = maxim(d,calcdist(i,j)); + + } + + out << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/16e3a2a4-c82e-459b-895f-652d93c17a27.cpp b/cmap/distance_search/16e3a2a4-c82e-459b-895f-652d93c17a27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42310b2479273ba760a3083b24da9adbe8062608 --- /dev/null +++ b/cmap/distance_search/16e3a2a4-c82e-459b-895f-652d93c17a27.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + + + + + +typedef pair pii; + +typedef long double LD; + + + +struct cmpr + +{ + + bool operator() (const pii &A, const pii &B) const + + { + + if(A.second == B.second) return A.first < B.first; + + return A.second < B.second; + + } + +}; + + + +LD D; + +int N; + +vector p; + +set st; + + + +LD dist(int x, int y, int xx, int yy) + +{ + + LD d = (LD)(x - xx) * (x - xx) + (LD)(y - yy) * (y - yy); + + return sqrt(d); + +} + + + +int main() + +{ + + + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + + + scanf("%d", &N); + + for(int i = 1; i <= N; i++) + + { + + int x, y; + + scanf("%d%d", &x, &y); + + p.push_back({x, y}); + + } + + + + sort(begin(p), end(p)); + + + + D = 2e9; + + int j = 0; + + for(int i = 0; i < p.size(); i++) + + { + + int x = p[i].first, y = p[i].second; + + while(j < i) + + { + + if(x - p[j].first > D) st.erase(p[j]); + + j++; + + } + + + + int d = ceil(D); + + auto it1 = st.lower_bound({x - d, y - d}); + + auto it2 = st.upper_bound({x, y + d}); + + + + if(it1 != st.end() && !st.empty()) + + { + + for(auto it = it1; it != it2; it++) + + { + + LD dd = dist(x, y, (*it).first, (*it).second); + + D = min(D, dd); + + } + + } + + + + st.insert(p[i]); + + } + + + + cout << fixed << setprecision(10) << D; + + + + return 0; + +} diff --git a/cmap/distance_search/1705262c-2b00-4833-8fa2-4b5583112e8b.cpp b/cmap/distance_search/1705262c-2b00-4833-8fa2-4b5583112e8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d4780a4d9bc9aeafdc64c1dcd25dacc95f53a37 --- /dev/null +++ b/cmap/distance_search/1705262c-2b00-4833-8fa2-4b5583112e8b.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +using namespace std; + +const int N = 100005; +const int inf = 0x3f3f3f3f; + +int n; +pair puncte[N]; + +double dist(pair x1, pair x2) +{ + return sqrt((x1.first - x2.first) * (x1.first - x2.first) + (x1.second - x2.second) * (x1.second - x2.second)); +} + +void citire() +{ + scanf("%d", &n); + + double tmp1, tmp2; + + for(int i = 0; i < n; i++) + { + scanf("%lf %lf", &tmp1, &tmp2); + puncte[i] = make_pair(tmp1, tmp2); + } + + sort(puncte, puncte + n); +} + +void solve() +{ + double distanta = inf; + + for(int i = 1; i < n; i++) + { + for(int j = i - 1; j >= 0 && (puncte[i].first - puncte[j].first) < distanta; j--) + { + double distx = dist(puncte[i], puncte[j]); + + if(distx < distanta) + { + distanta = distx; + } + } + } + + printf("%lf", distanta); +} + +int main() +{ + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + citire(); + solve(); + + return 0; +} diff --git a/cmap/distance_search/1856e61f-0c55-4573-9072-84fa4dd178e3.cpp b/cmap/distance_search/1856e61f-0c55-4573-9072-84fa4dd178e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2ddfebb88abc5b439c9300dd499ef680ddbc5be --- /dev/null +++ b/cmap/distance_search/1856e61f-0c55-4573-9072-84fa4dd178e3.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +pair < double , double > v[100002]; + + + +inline double dist(int i, int j) + +{ + + return sqrt((v[i].first-v[j].first)*(v[i].first-v[j].first)+(v[i].second-v[j].second)*(v[i].second-v[j].second)); + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + + + double vmin=dist(1,2); + + for(int i=1;i<=n-1;i++) + + for(int j=i+1;j<=n && fabs(v[j].first-v[i].first)<=vmin;j++) + + { + + double x=dist(i,j); + + if(x +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/1fcca571-ed07-4c01-89b4-8df0c2d2053b.cpp b/cmap/distance_search/1fcca571-ed07-4c01-89b4-8df0c2d2053b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9692fe2da633c36f0982cc27501793632d2d96fe --- /dev/null +++ b/cmap/distance_search/1fcca571-ed07-4c01-89b4-8df0c2d2053b.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +template void uin (T &a, T b) {a = min (a, b);} + + + +struct Point { + + int x, y; + + bool operator < (const Point &other) const { + + return (y == other.y) ? (x < other.x) : (y < other.y); + + } + +}; + + + +const int NMAX = 1e5 + 5; + + + +Point points[NMAX]; + + + +int main() { + + ios::sync_with_stdio(0); + + cin.tie(0); + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d\n", &n); + + for (int i = 1; i <= n; ++i) { + + scanf ("%d %d\n", &points[i].x, &points[i].y); + + } + + + + sort (points + 1, points + n + 1, [&] (const Point &a, const Point &b) { + + return (a.x == b.x) ? (a.y < b.y) : (a.x < b.x); + + }); + + + + set s; + + int l = 1, r = 0; + + ll ans = 1e18; + + while (++r <= n) { + + while (points[r].x - points[l].x >= ans) { + + s.erase (points[l++]); + + } + + + + int dist = ceil (sqrt (ans)); + + auto lo = s.lower_bound ({points[r].x, points[r].y - dist}); + + auto hi = s.lower_bound ({points[r].x, points[r].y + dist}); + + + + for (auto it = lo; it != hi; ++it) { + + uin (ans, 1LL * (points[r].x - it->x) * (points[r].x - it->x) + + + 1LL * (points[r].y - it->y) * (points[r].y - it->y)); + + } + + s.insert (points[r]); + + } + + + + printf ("%.6f\n", sqrt (ans)); + + + + fclose (stdin); + + fclose (stdout); + return 0; + +} diff --git a/cmap/distance_search/22d5c3b8-ddd0-434a-b745-4cf0d91333db.cpp b/cmap/distance_search/22d5c3b8-ddd0-434a-b745-4cf0d91333db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20f77ea3f530cfa6fc89a4f178cebeea2b878652 --- /dev/null +++ b/cmap/distance_search/22d5c3b8-ddd0-434a-b745-4cf0d91333db.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[100001]; + +int N; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +bool comp(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + long long dx = A.x - B.x; + + long long dy = A.y - B.y; + + return dx * dx + dy * dy; + +} + + + +double solutie() + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = 1; i <= N; i++) + + for(int j = i + 1; j <= i + 7 && j <= N; j++) + + { + + long long d = dist2(P[i], P[j]); + + if(d < dmin) dmin = d; + + } + + return sqrt(dmin); + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, comp); + + g << fixed << setprecision(6) << solutie(); + + return 0; + +} diff --git a/cmap/distance_search/23e75dfe-44bc-498c-9f61-e7a2a6150f7a.cpp b/cmap/distance_search/23e75dfe-44bc-498c-9f61-e7a2a6150f7a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a3b3513940a3fce75e01bd233cafa7f9041611e --- /dev/null +++ b/cmap/distance_search/23e75dfe-44bc-498c-9f61-e7a2a6150f7a.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + int x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline double dist(pct p1, pct p2){ + + return sqrt(1LL*(p1.x-p2.x)*(p1.x-p2.x)+1LL*(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + double dmin=(1LL<<34); + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, x, y; + + cin >> n; + + + + for (int i = 0; i < n; i++) + + { + + cin >> x >> y; + + a.push_back(make_pair(x, y)); + + } + +} + + + +bool f(pair x, pair y) + +{ + + return x.first < y.first; + +} + + + +double dist(pair x, pair y) + +{ + + return sqrt((x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/24950b7a-653d-4e7a-bb02-a82bb46dca84.cpp b/cmap/distance_search/24950b7a-653d-4e7a-bb02-a82bb46dca84.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a3512060865c837091a1c1b441dc1605bb7ec82 --- /dev/null +++ b/cmap/distance_search/24950b7a-653d-4e7a-bb02-a82bb46dca84.cpp @@ -0,0 +1,82 @@ +#include + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int NMax = 100001; + +const long double INF = 10000000000; + + + +struct point{ + + long double x,y; + +}; + + + +point a[NMax]; + +int n; + + + +bool cmp(point x, point y){ + + return (x.x < y.x); + +} + +long double dist(point a, point b){ + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> a[i].x >> a[i].y; + + sort(a + 1, a + 1 + n,cmp); + + + + long double t = INF; + + + + for(int i = 1; i <= n; ++i){ + + for(int j = i + 1; j <= n; ++j){ + + if(a[j].x - a[i].x > t) + + break; + + t = min(t,dist(a[i],a[j])); + + } + + } + + g << fixed << setprecision(6) << t << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/24b4f6f6-ce36-415d-85dc-e68cd5d0e9f6.cpp b/cmap/distance_search/24b4f6f6-ce36-415d-85dc-e68cd5d0e9f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb54034e32884692d64bd07eee95cf3a29ced17 --- /dev/null +++ b/cmap/distance_search/24b4f6f6-ce36-415d-85dc-e68cd5d0e9f6.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +class punct{ + +public: + + unsigned long long x; + + unsigned long long y; + + bool operator <(const punct p2) const + + { + + return x < p2.x; + + } + +}; + +int n, i, j; + +punct p[100005]; + +long long dist(punct p1, punct p2) + +{ + + return ( (p1.x - p2.x)*(p1.x - p2.x) + + + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + in>>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n); + + long long sol=4e18; + + for(i=0;i +std::istream& operator >> (std::istream& stream, std::pair & data) { + + return stream >> data.first >> data.second; + +} + + + +std::set > set; + +long long closestPair (std::vector >& pts) { + + std::sort(pts.begin(), pts.end()); + + + + long long dist2 = (1LL<<61); + + for (int i=0, idx=0; i= dist2) + + set.erase({pts[idx].second, pts[idx++].first}); + + + + auto it1 = set.lower_bound({pts[i].second-dist, pts[i].first}); + + auto it2 = set.upper_bound({pts[i].second+dist, pts[i].first}); + + + + int x, y; + + for (auto it = it1; it != it2; ++ it) + + x = pts[i].first - it->second, y = pts[i].second - it->first, + + dist2 = std::min(dist2, 1LL*x*x + 1LL*y*y); + + set.insert({pts[i].second, pts[i].first}); + + } return dist2; + +} + + + +int N; + +std::vector > V; + + + +std::ifstream input ("cmap.in"); + +std::ofstream output("cmap.out"); + + + +void readInput() { + + input >> N; + + V.resize(N); + + for (auto &it:V) + + input >> it; + +} + + + +void solveInput() { + + output << std::fixed << std::setprecision(6) << sqrt(double(closestPair(V))) << '\n'; + +} + + + +int main() + +{ + + readInput(); + + solveInput(); + + + + return 0; + +} diff --git a/cmap/distance_search/2695275a-9b61-48e5-bca7-d7c4146e4298.cpp b/cmap/distance_search/2695275a-9b61-48e5-bca7-d7c4146e4298.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ef70651a01b505c0007dd6a63f83eda5a6eb5e --- /dev/null +++ b/cmap/distance_search/2695275a-9b61-48e5-bca7-d7c4146e4298.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + double x, y; + +}; + + + +bool CMP (point a, point b); + +double DIST (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +double D; + +unsigned int i, j; + + + +double sol; + + + +int main () + +{ + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + sort(v+1,v+n+1,CMP); + + sol = DIST(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + D = DIST(v[i],v[j]); + + if (D < sol) + + sol = D; + + j -= 1; + + } + + } + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool CMP (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +double DIST (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/27200be4-8e76-493c-9862-c5ca4b0a543d.cpp b/cmap/distance_search/27200be4-8e76-493c-9862-c5ca4b0a543d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9692fe2da633c36f0982cc27501793632d2d96fe --- /dev/null +++ b/cmap/distance_search/27200be4-8e76-493c-9862-c5ca4b0a543d.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +template void uin (T &a, T b) {a = min (a, b);} + + + +struct Point { + + int x, y; + + bool operator < (const Point &other) const { + + return (y == other.y) ? (x < other.x) : (y < other.y); + + } + +}; + + + +const int NMAX = 1e5 + 5; + + + +Point points[NMAX]; + + + +int main() { + + ios::sync_with_stdio(0); + + cin.tie(0); + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d\n", &n); + + for (int i = 1; i <= n; ++i) { + + scanf ("%d %d\n", &points[i].x, &points[i].y); + + } + + + + sort (points + 1, points + n + 1, [&] (const Point &a, const Point &b) { + + return (a.x == b.x) ? (a.y < b.y) : (a.x < b.x); + + }); + + + + set s; + + int l = 1, r = 0; + + ll ans = 1e18; + + while (++r <= n) { + + while (points[r].x - points[l].x >= ans) { + + s.erase (points[l++]); + + } + + + + int dist = ceil (sqrt (ans)); + + auto lo = s.lower_bound ({points[r].x, points[r].y - dist}); + + auto hi = s.lower_bound ({points[r].x, points[r].y + dist}); + + + + for (auto it = lo; it != hi; ++it) { + + uin (ans, 1LL * (points[r].x - it->x) * (points[r].x - it->x) + + + 1LL * (points[r].y - it->y) * (points[r].y - it->y)); + + } + + s.insert (points[r]); + + } + + + + printf ("%.6f\n", sqrt (ans)); + + + + fclose (stdin); + + fclose (stdout); + return 0; + +} diff --git a/cmap/distance_search/27f4c4b5-72c5-4297-b685-d7082420fda6.cpp b/cmap/distance_search/27f4c4b5-72c5-4297-b685-d7082420fda6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..460020f0aaf93597d709e10e837ca6862cdecff8 --- /dev/null +++ b/cmap/distance_search/27f4c4b5-72c5-4297-b685-d7082420fda6.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef pair punct; + +vector X; + +set Y; + +punct rot(punct); + +double dist(punct,punct),d; + +const int oo = 2000000000; + +int n,j; + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + X.push_back(make_pair(x,y)); + + } + + sort(X.begin(),X.end()); + + Y.insert(rot(X[0])); + + Y.insert(rot(X[1])); + + d=dist(X[0],X[1]); + + for(int i=2;i::iterator it=Y.lower_bound(make_pair(P.first-int(d)-1,-oo)); + + while(it!=Y.end()&&1.0*(it->first-P.first) +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +const int NM = 100100; + + + +int n, i, j; + +double d; + + + +vector< pair > v(NM); + + + +double dist(int a, int b) { + + return sqrt( (v[a].first - v[b].first) * (v[a].first - v[b].first) + (v[a].second - v[b].second) * (v[a].second - v[b].second) ); + +} + + + +int main() { + + fin >> n; + + for (i = 1; i <= n; ++i) { + + fin >> v[i].first >> v[i].second; + + } + + sort(v.begin() + 1, v.begin() + n + 1); + + d = dist(1, n); + + for (i = 1; i <= n; ++i) { + + for(j = i + 1; j <= n; ++j) { + + if (v[j].first - v[i].first > d) { + + break; + + } + + if (dist(i, j) < d) { + + d = dist(i, j); + + } + + } + + } + + fout << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/2884f812-c191-41dd-96de-e191164ad907.cpp b/cmap/distance_search/2884f812-c191-41dd-96de-e191164ad907.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8c69f909dd59767628eda26e86ce5c6d2a05f1 --- /dev/null +++ b/cmap/distance_search/2884f812-c191-41dd-96de-e191164ad907.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/28b6795b-0d8c-433c-bb8c-4646c2039453.cpp b/cmap/distance_search/28b6795b-0d8c-433c-bb8c-4646c2039453.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e71970d01d34808cdc217301bcae067daa9354e --- /dev/null +++ b/cmap/distance_search/28b6795b-0d8c-433c-bb8c-4646c2039453.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef pair Point; + + + +const int maxN = 100000; + +Point v[maxN + 5]; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +inline double dist(int i, int j) { + + return sqrt(1.0 * (v[i].first - v[j].first) * (v[i].first - v[j].first) + 1.0 * (v[i].second - v[j].second) * (v[i].second - v[j].second)); + +} + + + +int main() { + + int N; + + in >> N; + + + + for(int i = 1; i <= N; ++ i) + + in >> v[i].first >> v[i].second; + + + + sort(v + 1, v + N + 1); + + double d = dist(1, N); + + + + for(int i = 1; i <= N; ++ i) + + for(int j = i + 1; j <= N and v[j].first - v[i].first <= d; ++ j) + + d = min(d, dist(i, j)); + + + + out << fixed << setprecision(6) << d << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/2bd195e8-5a97-431e-8708-b716938e1e3e.cpp b/cmap/distance_search/2bd195e8-5a97-431e-8708-b716938e1e3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..154bf54460dba732cebf47385d6e0d222b70681b --- /dev/null +++ b/cmap/distance_search/2bd195e8-5a97-431e-8708-b716938e1e3e.cpp @@ -0,0 +1,96 @@ +#include + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +constexpr int nmax=1e5+5; + +int n; + +pair a[nmax]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point + +{ + + int x, y; + + bool operator < (const point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100005]; + +set < point > s; + + + +double dist(point a, point b) + +{ + + return sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y)); + +} + + + +int main() + +{ + + f >> n; + + for (int i = 0; i < n; ++ i) + + f >> v[i].x >> v[i].y; + + + + sort(v, v + n); + + + + double dist_min = dist(v[0], v[1]); + + int k = 0; + + + + for (int i = 0; i < n; ++ i) { + + + + while (k < i && double(v[i].x - v[k].x) >= dist_min) + + + + s.erase(s.find(point{v[k].y, v[k++].x})); + + + + + + int dist_int = (int)(dist_min); + + set :: iterator it1 = s.lower_bound(point{v[i].y - dist_int,-((1<<30) - 1)}); + + set :: iterator it2 = s.upper_bound(point{v[i].y + dist_int, ((1<<30) - 1)}); + + + + + + if (it2 != s.begin() && it1 != s.end()) + + for (set :: iterator it3= it1; it3 != it2; it3++) + + dist_min = min(dist_min , dist(v[i], point{it3->y, it3->x})); + + + + s.insert(point{v[i].y, v[i].x}); + + } + + + + g << setprecision(7) << fixed << dist_min; + + + + return 0; + +} diff --git a/cmap/distance_search/2d60bc9b-6a69-4440-95e1-4fff57b1eef8.cpp b/cmap/distance_search/2d60bc9b-6a69-4440-95e1-4fff57b1eef8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff4fb243549db6af6d613dc9d3605ca829644929 --- /dev/null +++ b/cmap/distance_search/2d60bc9b-6a69-4440-95e1-4fff57b1eef8.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long double ll; + + + +const int N=100000+5; + + + +struct point + +{ + + int x; + + int y; + + ll di; + +}; + + + +ll p(int x) + +{ + + return x*(long long)x; + +} + + + +ll d(point a,point b) + +{ + + int dx=a.x-b.x; + + int dy=a.y-b.y; + + return p(dx)+p(dy); + +} + + + +int n,x=(1<<30),y=(1<<30); + +point v[N]; + + + +bool cmp(point a,point b) + +{ + + return a.di>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].x>>v[i].y; + + if(v[i].x +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int N = 1e5 + 7; + +const double INF = 1e10; + + + +struct Point { + + double x, y; + + + + bool operator < (const Point &a) const { + + if (x == a.x) + + return y < a.y; + + return x < a.x; + + } + +}; + + + +double dist(Point a, Point b) { + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +Point v[N]; + + + +struct Elm { + + int poz; + + + + bool operator < (const Elm &a) const { + + if (v[poz].y == v[a.poz].y) + + return poz < a.poz; + + return v[poz].y < v[a.poz].y; + + } + +}; + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i].x >> v[i].y; + + sort(v, v + n); + + int st(1); + + set < Elm > s; + + double mind(INF); + + for (int i = 1; i <= n; ++i) { + + while (s.size() && v[s.begin()->poz].x + mind <= v[i].x) + + s.erase(s.begin()); + + v[i].y -= mind; + + auto top = s.lower_bound((Elm){i}); + + v[i].y += 2 * mind; + + auto bottom = s.upper_bound((Elm){i}); + + v[i].y -= mind; + + while (top != bottom) + + mind = min(mind, dist(v[i], v[top->poz])), + + top++; + + s.insert((Elm){i}); + + } + + fout << fixed << setprecision(7); + + fout << mind; + + return 0; + +} diff --git a/cmap/distance_search/2e634d4f-80e8-49e8-a5d7-d1ef45d6a1c8.cpp b/cmap/distance_search/2e634d4f-80e8-49e8-a5d7-d1ef45d6a1c8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c675f0873fc28fe74ec4f469b6138c1b4810365d --- /dev/null +++ b/cmap/distance_search/2e634d4f-80e8-49e8-a5d7-d1ef45d6a1c8.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +const int NM = 100100; + + + +int n, i, j; + +double d; + + + +vector< pair > v(NM); + + + +double dist(int a, int b) { + + return sqrt( (v[a].first - v[b].first) * (v[a].first - v[b].first) + (v[a].second - v[b].second) * (v[a].second - v[b].second) ); + +} + + + +int main() { + + fin >> n; + + for (i = 1; i <= n; ++i) { + + fin >> v[i].first >> v[i].second; + + } + + sort(v.begin() + 1, v.begin() + n + 1); + + d = dist(1, n); + + for (i = 1; i <= n; ++i) { + + for(j = i + 1; j <= n; ++j) { + + if (v[j].first - v[i].first > d) { + + break; + + } + + if (dist(i, j) < d) { + + d = dist(i, j); + + } + + } + + } + + fout << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/2f1616d7-6a29-4158-a0ee-9e9c0bcd44fc.cpp b/cmap/distance_search/2f1616d7-6a29-4158-a0ee-9e9c0bcd44fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea362dbec6edef4cc48d1b8275f5a30292f84efd --- /dev/null +++ b/cmap/distance_search/2f1616d7-6a29-4158-a0ee-9e9c0bcd44fc.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f1("cmap.in"); + +ofstream f2("cmap.out"); + + + +struct pt + +{ + + long long x,y; + +}; + + + +vector a; + +double s; + + + +void read() + +{ + + int n; + + f1 >> n; + + a.resize(n); + + for (int i=0; i> a[i].x >> a[i].y; + + } + +} + + + +bool xx(pt a, pt b) + +{ + + return a.xs) break; + + } + + } + +} + + + +main() + +{ + + read(); + + sort(a.begin(),a.end(),xx); + + s=dist(a[0],a[1]); + + solve(); + + f2 << fixed << setprecision(10) << s; + +} diff --git a/cmap/distance_search/2f9c964b-2e64-462a-a16f-f1ab8f131a9c.cpp b/cmap/distance_search/2f9c964b-2e64-462a-a16f-f1ab8f131a9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2ae36477694f74d8f40c24282e6a80bb37e13a1 --- /dev/null +++ b/cmap/distance_search/2f9c964b-2e64-462a-a16f-f1ab8f131a9c.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/30621ab7-5b5f-4fee-b39a-bdbe92781700.cpp b/cmap/distance_search/30621ab7-5b5f-4fee-b39a-bdbe92781700.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020548e42e0a9a4f564ddfb66052e106c8390bec --- /dev/null +++ b/cmap/distance_search/30621ab7-5b5f-4fee-b39a-bdbe92781700.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/31397050-0bfe-4327-adb2-fe1a270f1f36.cpp b/cmap/distance_search/31397050-0bfe-4327-adb2-fe1a270f1f36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30a70dc412f82b91a8b75273256e36330774123a --- /dev/null +++ b/cmap/distance_search/31397050-0bfe-4327-adb2-fe1a270f1f36.cpp @@ -0,0 +1,268 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + + + +long long n,k,a,b; + +double dminr; + + + +double dist(pair A, pair B) + +{ + + double d; + + long long v; + + v=((long long)(A.first-B.second)*(long long)(A.first-B.second)+(long long)(A.second-B.first)*(long long)(A.second-B.first)); + + d=sqrt((double)v); + + return d; + +} + + + +vector > PX; + +set > SY; + +set > :: iterator it,cit; + + + + + +int main() + +{ + + + + fin>>n; + + for (long long i=1;i<=n;i++) + + { + + long long x,y; + + fin>>x>>y; + + PX.push_back(make_pair(x,y)); + + } + + sort(PX.begin(),PX.end()); + + dminr=10000000.0; + + k=0; + + SY.insert(make_pair(PX[0].second,PX[0].first)); + + for (long long i=1;idminr) + + { + + SY.erase(make_pair(PX[k].second,PX[k].first)); + + k++; + + } + + else + + break; + + } + + + + SY.insert(make_pair(PX[i].second,PX[i].first)); + + it=SY.lower_bound(make_pair(PX[i].second,PX[i].first)); + + cit=it; + + + + if (cit!=SY.begin()) + + { + + cit--; + + if (dist(PX[i],*cit) + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {(*it).second, (*it).first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/322ddb9a-dabe-4be0-b92a-72f8dfd27094.cpp b/cmap/distance_search/322ddb9a-dabe-4be0-b92a-72f8dfd27094.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de954acb28120be15c0376221c79840963a86cb --- /dev/null +++ b/cmap/distance_search/322ddb9a-dabe-4be0-b92a-72f8dfd27094.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + +typedef pair pairll; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pairll P[100001]; + +int lef; + +set S; + +long double dmin; + + + +int cmp(pairll a, pairll b) + +{ + + if (a.secondb.second) + + return 0; + + return a.first>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].second>>P[i].first; + + sort(P+1,P+N+1,cmp); + + + + lef=1; + + S.insert(P[1]); + + dmin=(long double)1e10; + + for (int i=2;i<=N;i++) + + { + + + + while (lefdmin) + + { + + S.erase(P[lef]); + + lef++; + + } + + + + set :: iterator it; + + it=S.lower_bound({(long long)(P[i].first-dmin),(long long)(P[i].second-dmin)}); + + int nr; + + nr=1; + + while (it!=S.end() && nr<=5) + + { + + dmin=min(dmin,distanta(P[i],*it)); + + it++; + + nr++; + + } + + S.insert(P[i]); + + } + + fo< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 10; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/333438f8-e5ea-45e7-9e11-07fb91df8a86.cpp b/cmap/distance_search/333438f8-e5ea-45e7-9e11-07fb91df8a86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21f01d148faf3809716b06e2afb5635e0546cd98 --- /dev/null +++ b/cmap/distance_search/333438f8-e5ea-45e7-9e11-07fb91df8a86.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 1; i <= n; ++ i) + for (int j = i + 1; j <= n && points[i].real() + ans > points[j].real() - 1e-8; ++ j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/336399f8-2791-42a8-a76e-5590cb39b9bc.cpp b/cmap/distance_search/336399f8-2791-42a8-a76e-5590cb39b9bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020548e42e0a9a4f564ddfb66052e106c8390bec --- /dev/null +++ b/cmap/distance_search/336399f8-2791-42a8-a76e-5590cb39b9bc.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/3371c287-1c49-4fbc-af46-950a594a7d72.cpp b/cmap/distance_search/3371c287-1c49-4fbc-af46-950a594a7d72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c675f0873fc28fe74ec4f469b6138c1b4810365d --- /dev/null +++ b/cmap/distance_search/3371c287-1c49-4fbc-af46-950a594a7d72.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +const int NM = 100100; + + + +int n, i, j; + +double d; + + + +vector< pair > v(NM); + + + +double dist(int a, int b) { + + return sqrt( (v[a].first - v[b].first) * (v[a].first - v[b].first) + (v[a].second - v[b].second) * (v[a].second - v[b].second) ); + +} + + + +int main() { + + fin >> n; + + for (i = 1; i <= n; ++i) { + + fin >> v[i].first >> v[i].second; + + } + + sort(v.begin() + 1, v.begin() + n + 1); + + d = dist(1, n); + + for (i = 1; i <= n; ++i) { + + for(j = i + 1; j <= n; ++j) { + + if (v[j].first - v[i].first > d) { + + break; + + } + + if (dist(i, j) < d) { + + d = dist(i, j); + + } + + } + + } + + fout << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/3396acc4-9867-4003-853f-a9c4cbe6fb24.cpp b/cmap/distance_search/3396acc4-9867-4003-853f-a9c4cbe6fb24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6c21ea3928908053c41e03dd723659bc99b96b6 --- /dev/null +++ b/cmap/distance_search/3396acc4-9867-4003-853f-a9c4cbe6fb24.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int nmax = 100005; + +const double f_mare = 6e15; + +int n, i, j; + +struct pct { + + double x, y; + +}v[nmax]; + +double dist, sol = f_mare; + + + +bool cmp(const pct &a, const pct &b) { + + return a.x < b.x; + +} + + + +double fc(const pct &a, const pct &b) { + + return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); + +} + + + +int main() { + + f >> n; + + for (i = 1; i <= n; i++) + + f >> v[i].x >> v[i].y; + + + + sort(v+1, v+n+1, cmp); + + sol = fc(v[1],v[2]); + + for (i = 1; i <= n; i++) { + + for (j = i-1; j >= 1 && v[i].x - v[j].x < sol; j--) { + + dist = fc(v[j], v[i]); + + if (sol>dist) sol = dist; + + } + + } + + g << fixed< + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + int n; + + cin>>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + + long long d = dist(a[0], a[1]); + + for (int i = 1; i < n-1; i++) { + + d = min(d, dist(a[i], a[i+1])); + + } + + for (int i = 0; i < n; i++) { + + for (int j = i+1; j < n && sqlong(abs(a[i].x-a[j].x)) < d; j++) { + + d = min(d, dist(a[i], a[j])); + + } + + } + + + + cout< +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +pair p[100010]; + +set > q; + +double dist(pair,pair); + +int n,i,j,x,y,D; + +double d; + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x>>y; + + p[i]={x,y}; + + } + + sort(p+1,p+n+1); + + d=dist(p[1],p[2]); + + q.insert({p[1].second,p[1].first}); + + q.insert({p[2].second,p[2].first}); + + j=1; + + for(i=3;i<=n;i++) + + { + + pair P={p[i].second,p[i].first}; + + while((double)(p[i].first-p[j].first)>=d) + + { + + q.erase({p[j].second,p[j].first}); + + j++; + + } + + D=d+1; + + pair Q={P.first-D,-1000000001}; + + auto it=upper_bound(q.begin(),q.end(),Q); + + while(it!=q.end()&&it->first-P.first u,pair v) + +{ + + return sqrt(1.0*(u.first-v.first)*(u.first-v.first)+1.0*(u.second-v.second)*(u.second-v.second)); + +} diff --git a/cmap/distance_search/3fbe185a-e86e-4c88-8ace-57377bf04752.cpp b/cmap/distance_search/3fbe185a-e86e-4c88-8ace-57377bf04752.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8adca578000c5fea9a0b533e87adeb8baf811aec --- /dev/null +++ b/cmap/distance_search/3fbe185a-e86e-4c88-8ace-57377bf04752.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct Point{ + +long long x,y; + +bool operator< (const Point b) const + +{ + + return y < b.y; + +} + +}; + + + +Point a[100010]; + + + +long long n; + + + +long long dist(Point a, Point b) + +{ + + return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y); + +} + + + +bool XComparer(Point a,Point b) + +{ + + return a.x>n; + + long long H = (1LL<<60); + + for(long long i=1;i<=n;i++) + + fin>>a[i].x>>a[i].y; + + + + sort(a+1,a+1+n, XComparer); + + + + for(long long i=2;i<=n;i++) + + H = min(H,dist(a[i],a[i-1])); + + + + multiset Set; + + multiset::iterator in, sf; + + + + long long act=1; + + + + Point tmp; + + + + for(long long i=1;i<=n;i++) + + { + + while(act <= n && (a[act].x - a[i].x) * (a[act].x - a[i].x) < H ) + + { + + Set.insert(a[act]); + + act++; + + } + + tmp.y = a[i].y - H + 1; + + in = Set.lower_bound(tmp); + + tmp.y = a[i].y + H - 1; + + sf = Set.upper_bound(tmp); + + + + + + while(in!= sf ) + + { + + if(a[i].x != in->x || a[i].y != in->y) + + H = min(H,dist(a[i], *in )); + + + + in++; + + } + + + + + + in = Set.lower_bound(a[i]); + + Set.erase(in); + + + + + + } + + + + fout< +#include +#include +#include +#include +using namespace std; + + + + + + + +struct punct{ + + double x, y; + +}; + + + +int N; + +double distMin; + + + +bool cmp(punct a, punct b){ + + if (a.x!=b.x) + + return a.x> N; + + vector P(N); + + for (int i=0; i> P[i].x >> P[i].y; + + f.close(); + + + + sort(P.begin(), P.end(), cmp); + + + + double dist; + + distMin=distanta(P[0], P[1]); + + for (int i=0; i +#include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[100001]; + +int N; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +bool comp(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + long long dx = A.x - B.x; + + long long dy = A.y - B.y; + + return dx * dx + dy * dy; + +} + + + +double solutie() + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = 1; i <= N; i++) + + for(int j = i + 1; j <= i + 7 && j <= N; j++) + + { + + long long d = dist2(P[i], P[j]); + + if(d < dmin) dmin = d; + + } + + return sqrt(dmin); + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, comp); + + g << fixed << setprecision(6) << solutie(); + + return 0; + +} diff --git a/cmap/distance_search/4319036c-a0ee-4b93-8229-cf64e88a204d.cpp b/cmap/distance_search/4319036c-a0ee-4b93-8229-cf64e88a204d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f6480cf7bb7a6662d430187d0026db2117dadf7 --- /dev/null +++ b/cmap/distance_search/4319036c-a0ee-4b93-8229-cf64e88a204d.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +const string problem("cmap"); + +ifstream fin(problem + ".in"); + +ofstream fout(problem + ".out"); + +typedef long long i64; + +using PII = pair; + +vector v; + +set s; + +int n; + +i64 ys, yd, j(1); + +double res(1e18); + +inline double Dist(PII a, PII b) { + + return sqrt(1.0 * ((1LL * (a.first - b.first) * (a.first - b.first)) + (1LL * (a.second - b.second) * (a.second - b.second)))); + +} + +int main() { + + ios::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> n; + + v = vector(n + 1); + + for (int i = 1; i <= n; ++i) + + fin >> v[i].first >> v[i].second; + + sort(v.begin() + 1, v.end()); + + for (int i = 1; i <= n; ++i) { + + while (!s.empty() && (v[i].first - v[j].first) >= res) + + s.erase(s.find({v[j].second, v[j].first})), ++j; + + ys = v[i].second - static_cast(res) - 1; + + yd = v[i].second + static_cast(res) + 1; + + auto st = s.lower_bound({ys, 1e9}); + + auto dr = s.lower_bound({yd, 1e9}); + + for (auto it = st; it != dr; ++it) { + + if (it == s.end()) + + break; + + res = min(res, Dist(v[i], {it -> second, it -> first})); + + } + + s.emplace(v[i].second, v[i].first); + + } + + fout << fixed << setprecision(10) << res; + + fin.close(); fout.close(); return 0; + +} diff --git a/cmap/distance_search/431f5973-d9d4-4423-880f-d59f0c74b077.cpp b/cmap/distance_search/431f5973-d9d4-4423-880f-d59f0c74b077.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f95a0f9a0394c15d8ea6f8549e419fe6f77ecfd --- /dev/null +++ b/cmap/distance_search/431f5973-d9d4-4423-880f-d59f0c74b077.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +std::istream& operator >> (std::istream& stream, std::pair & data) { + + return stream >> data.first >> data.second; + +} + + + +std::set > set; + +long long closestPair (std::vector >& pts) { + + std::sort(pts.begin(), pts.end()); + + + + long long dist2 = (1LL<<61); + + for (int i=0, idx=0; i= dist2) + + set.erase({pts[idx].second, pts[idx++].first}); + + + + auto it1 = set.lower_bound({pts[i].second-dist, pts[i].first}); + + auto it2 = set.upper_bound({pts[i].second+dist, pts[i].first}); + + + + int x, y; + + for (auto it = it1; it != it2; ++ it) + + x = pts[i].first - it->second, y = pts[i].second - it->first, + + dist2 = std::min(dist2, 1LL*x*x + 1LL*y*y); + + set.insert({pts[i].second, pts[i].first}); + + } return dist2; + +} + + + +int N; + +std::vector > V; + + + +std::ifstream input ("cmap.in"); + +std::ofstream output("cmap.out"); + + + +void readInput() { + + input >> N; + + V.resize(N); + + for (auto &it:V) + + input >> it; + +} + + + +void solveInput() { + + output << std::fixed << std::setprecision(6) << sqrt(double(closestPair(V))) << '\n'; + +} + + + +int main() + +{ + + readInput(); + + solveInput(); + + + + return 0; + +} diff --git a/cmap/distance_search/440dbde2-fef0-43f3-9b59-a96a9e3b4885.cpp b/cmap/distance_search/440dbde2-fef0-43f3-9b59-a96a9e3b4885.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7cd6bbefd0380459b523901f256f831c3a6c1d --- /dev/null +++ b/cmap/distance_search/440dbde2-fef0-43f3-9b59-a96a9e3b4885.cpp @@ -0,0 +1,120 @@ +#include + + + + + + +using namespace std; + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +int n; + +struct Point + +{ + + int x, y; + + bool operator < (const Point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100002]; + +set < Point > s; + + + +long long sqr(int x) + +{ + + return 1LL * x * x; + +} + +double dist(Point a, Point b) + +{ + + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); + +} + +double h; + + + +int main() + +{ + + scanf("%d", &n); + + for (int i = 0; i < n; ++ i) + + scanf("%d%d", &v[i].x, &v[i].y); + + auto it = 0; + + sort(v, v + n); + + h = dist(v[0], v[1]); + + for (int i = 0; i < n; ++ i) + + { + + while (it < i && h <= (double)v[i].x - v[it].x) + + { + + s.erase(s.find(Point{v[it].y, v[it].x})); + + ++ it; + + } + + int H = (int)(h); + + auto it1 = s.lower_bound(Point{v[i].y - H,-0x3fffffff}), + + it2 = s.upper_bound(Point{v[i].y + H, 0x3fffffff}); + + if (it2 != s.begin() && it1 != s.end()) + + { + + for (auto j = it1; j != it2; ++ j) + + h = min(h, dist(v[i], Point{it1->y, it1->x})); + + } + + s.insert(Point{v[i].y, v[i].x}); + + } + + + + printf("%.7lf", h); + + return 0; + +} diff --git a/cmap/distance_search/442cc1e6-bb07-4fbe-a92a-5a0a758482e6.cpp b/cmap/distance_search/442cc1e6-bb07-4fbe-a92a-5a0a758482e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/442cc1e6-bb07-4fbe-a92a-5a0a758482e6.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/4473e43d-2f13-4a14-bdcc-3a5c862ad116.cpp b/cmap/distance_search/4473e43d-2f13-4a14-bdcc-3a5c862ad116.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed46d0039e51581fa116f483525aa12805c816f --- /dev/null +++ b/cmap/distance_search/4473e43d-2f13-4a14-bdcc-3a5c862ad116.cpp @@ -0,0 +1,154 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef pair PI; + + + +constexpr int NMAX = 1e5 + 5; + + + +int N; + +PI a[NMAX]; + + + +set R; + + + +void Read () + +{ + + f >> N; + + + + for (int i = 1; i <= N; ++i) + + f >> a[i].first >> a[i].second; + + + + sort(a+1, a+N+1); + +} + + + +double Distance (PI a, PI b) + +{ + + return sqrt(1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second)); + +} + + + +void Solve () + +{ + + double sol = 2e9; + + int ind_Left = 1; + + + + for (int i = 1; i <= N; ++i) + + { + + while (!R.empty() && a[i].first - a[ind_Left].first >= sol) + + { + + R.erase({a[ind_Left].second, a[ind_Left].first}); + + + + ++ind_Left; + + } + + + + int y_Down = a[i].second - (int)(sol); + + int y_Up = a[i].second + (int)(sol); + + + + set :: iterator d = R.lower_bound({y_Down, 1e9}); + + set :: iterator u = R.lower_bound({y_Up, 1e9}); + + + + for (set :: iterator it = d; it != u; ++it) + + { + + PI nr = *it; + + + + swap(nr.first, nr.second); + + + + double ans = Distance(nr, a[i]); + + + + sol = min(sol, ans); + + } + + + + R.insert({a[i].second, a[i].first}); + + } + + + + g << setprecision(6) << fixed; + + g << sol << '\n'; + +} + + + +int main() + +{ + + Read(); + + + + Solve(); + + return 0; + +} diff --git a/cmap/distance_search/456b6eb7-b94f-4dee-aad3-73ec7432662d.cpp b/cmap/distance_search/456b6eb7-b94f-4dee-aad3-73ec7432662d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3daad581cf1458d61db2d8ad4ccf777ce58f9a5 --- /dev/null +++ b/cmap/distance_search/456b6eb7-b94f-4dee-aad3-73ec7432662d.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/4665ea9a-e28b-4305-a2db-e8f395812ac6.cpp b/cmap/distance_search/4665ea9a-e28b-4305-a2db-e8f395812ac6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4264b2d1b5445adb78ea4668cdb46a65c19e2d --- /dev/null +++ b/cmap/distance_search/4665ea9a-e28b-4305-a2db-e8f395812ac6.cpp @@ -0,0 +1,102 @@ +#include + + + + + + + + +using namespace std; + + + +const long long mod=1e9+7; + +const long long modu=1999999973; + +const long long modul=998244353; + + + +const long long nmax=1000005; + + + +long long n; + +long double ans=1e18; + +paira[100005]; + + + +long double dist(long long x,long long y,long long z,long long t){ + + long double g = sqrt((long double)((x-z)*(x-z)+(y-t)*(y-t))); + + return g; + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cout << fixed << setprecision(12); + + cin >> n; + + for(long long i=1;i<=n;i++) cin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + set>q; + + long long indx=1; + + for(long long i=1;i<=n;i++){ + + while(q.size()>=1 && (long double)(a[i].first-a[indx].first)>=ans){ + + q.erase(q.find({a[indx].second,a[indx].first})); + + indx++; + + } + + auto it1=q.lower_bound({(long double)(a[i].second-ans),(long double)(a[i].first)}); + + auto it2=q.upper_bound({(long double)(a[i].second+ans),(long double)(a[i].first)}); + + auto it=it1; + + while(it!=it2){ + + if(it==q.end()) break; + + long long x=it->second; + + long long y=it->first; + + ans=min(ans,dist(x,y,a[i].first,a[i].second)); + + it++; + + } + + q.insert({a[i].second,a[i].first}); + + } + + cout << ans << '\n'; + +} diff --git a/cmap/distance_search/46c71529-9496-4119-be41-0d4666bfaf92.cpp b/cmap/distance_search/46c71529-9496-4119-be41-0d4666bfaf92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3daad581cf1458d61db2d8ad4ccf777ce58f9a5 --- /dev/null +++ b/cmap/distance_search/46c71529-9496-4119-be41-0d4666bfaf92.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/4a62d73e-ddbb-4c51-aac7-68663afe1849.cpp b/cmap/distance_search/4a62d73e-ddbb-4c51-aac7-68663afe1849.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d47fe8e8010e48ef17102296da777a3b798691 --- /dev/null +++ b/cmap/distance_search/4a62d73e-ddbb-4c51-aac7-68663afe1849.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair a, pair b) + +{ + + return a.first < b.first; + +} + + + +double dist(pair a, pair b) + +{ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/4eb3692d-4be9-4e18-ae8a-ecb8c49b3164.cpp b/cmap/distance_search/4eb3692d-4be9-4e18-ae8a-ecb8c49b3164.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24b7678b045e5c88f0039cd9ea284ef3c4337a85 --- /dev/null +++ b/cmap/distance_search/4eb3692d-4be9-4e18-ae8a-ecb8c49b3164.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (const gigi& a,const gigi& b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair a, pair b) + +{ + + return a.first < b.first; + +} + + + +double dist(pair a, pair b) + +{ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/517810e8-22e0-4d51-b71f-0e6219413821.cpp b/cmap/distance_search/517810e8-22e0-4d51-b71f-0e6219413821.cpp new file mode 100644 index 0000000000000000000000000000000000000000..904217a02904043a051b7f59dfe8b3e477a867a4 --- /dev/null +++ b/cmap/distance_search/517810e8-22e0-4d51-b71f-0e6219413821.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + + + +int n,p; + +pair a[100030]; + +long double d=1e13; + +struct cmp { + + bool operator()(int l, int r) { + + return a[l].secondS; + + + +long double dist(pair A, pair B) { + + return (sqrt((B.first-A.first)*(B.first-A.first)+(B.second-A.second)*(B.second-A.second))); + +} + + + +int main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cin>>n; + + for (int i=1; i<=n; ++i) cin>>a[i].first>>a[i].second; + + sort(a+1,a+1+n); p=1; S.insert(1); + + for (int i=2; i<=n; ++i) { + + while (a[i].first-a[p].first>d) { + + S.erase(p); ++p; + + } + + a[0].second=a[i].second-d; + + auto it=S.lower_bound(0); + + for (auto it=S.lower_bound(0); it!=S.end() && abs(a[*it].second-a[i].second)<=d; ++it) { + + long double d2=dist(a[*it],a[i]); + + if (d2 +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 2; i <= n; ++ i) + for (int j = i - 1; j && points[j].real() > points[i].real() - ans - 1e-8; -- j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/52ebab41-5ce6-4de7-92d0-2ad02fe92fb4.cpp b/cmap/distance_search/52ebab41-5ce6-4de7-92d0-2ad02fe92fb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb3ef7b1946f9ae9b140ef810eea1004adb0d99f --- /dev/null +++ b/cmap/distance_search/52ebab41-5ce6-4de7-92d0-2ad02fe92fb4.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int i,n,j; + +long double Min; + +struct du{ + + int x,y;}points[100003]; + + int cmp(du a,du b){ + + return a.x>n; + + for(i=1;i<=n;i++) + + f>>points[i].x>>points[i].y; + + sort(points+1,points+n+1,cmp); + + Min=(1ll<<60); + + for(i=1;i<=n;i++) + + for(j=i+1;j<=n;j++){ + + if(points[j].x-points[i].x>Min) + + break; + + Min=min(Min,dist(points[j],points[i])); + +} + +g< +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 1; i <= n; ++ i) + for (int j = i + 1; j <= n && points[i].real() + ans > points[j].real() - 1e-8; ++ j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/5470b48d-f1af-430d-9426-606752b7d0b4.cpp b/cmap/distance_search/5470b48d-f1af-430d-9426-606752b7d0b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61f653204cb0319606e90b99e696f14b480b4944 --- /dev/null +++ b/cmap/distance_search/5470b48d-f1af-430d-9426-606752b7d0b4.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef pair punct; + +vector X; + +set Y; + +punct rot(punct); + +double dist(punct,punct),d; + +const int oo = 2000000000; + +int n,j; + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + X.push_back(make_pair(x,y)); + + } + + sort(X.begin(),X.end()); + + Y.insert(rot(X[0])); + + Y.insert(rot(X[1])); + + d=dist(X[0],X[1]); + + for(int i=2;i::iterator it=Y.lower_bound(make_pair(P.first-int(d)-1,-oo)); + + while(it!=Y.end()&&1.0*(it->first-P.first) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 10; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/55909136-76e7-4600-b825-1f877f45b9af.cpp b/cmap/distance_search/55909136-76e7-4600-b825-1f877f45b9af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de954acb28120be15c0376221c79840963a86cb --- /dev/null +++ b/cmap/distance_search/55909136-76e7-4600-b825-1f877f45b9af.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + +typedef pair pairll; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pairll P[100001]; + +int lef; + +set S; + +long double dmin; + + + +int cmp(pairll a, pairll b) + +{ + + if (a.secondb.second) + + return 0; + + return a.first>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].second>>P[i].first; + + sort(P+1,P+N+1,cmp); + + + + lef=1; + + S.insert(P[1]); + + dmin=(long double)1e10; + + for (int i=2;i<=N;i++) + + { + + + + while (lefdmin) + + { + + S.erase(P[lef]); + + lef++; + + } + + + + set :: iterator it; + + it=S.lower_bound({(long long)(P[i].first-dmin),(long long)(P[i].second-dmin)}); + + int nr; + + nr=1; + + while (it!=S.end() && nr<=5) + + { + + dmin=min(dmin,distanta(P[i],*it)); + + it++; + + nr++; + + } + + S.insert(P[i]); + + } + + fo< + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +using namespace std; + + + +int n,p; + +pair a[100030]; + +long double d=1e13; + +struct cmp { + + bool operator()(int l, int r) { + + return a[l].secondS; + + + +long double dist(pair A, pair B) { + + return (sqrt((B.first-A.first)*(B.first-A.first)+(B.second-A.second)*(B.second-A.second))); + +} + + + +int main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cin>>n; + + for (int i=1; i<=n; ++i) cin>>a[i].first>>a[i].second; + + sort(a+1,a+1+n); p=1; S.insert(1); + + for (int i=2; i<=n; ++i) { + + while (a[i].first-a[p].first>d) { + + S.erase(p); ++p; + + } + + a[0].second=a[i].second-d; + + auto it=S.lower_bound(0); + + for (auto it=S.lower_bound(0); it!=S.end() && abs(a[*it].second-a[i].second)<=d; ++it) { + + long double d2=dist(a[*it],a[i]); + + if (d2 + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/5b21475d-83d2-4ce6-9c7d-8f65f8ceed8d.cpp b/cmap/distance_search/5b21475d-83d2-4ce6-9c7d-8f65f8ceed8d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19fc216784684349394fd26fed536ce9cb93a912 --- /dev/null +++ b/cmap/distance_search/5b21475d-83d2-4ce6-9c7d-8f65f8ceed8d.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f1("cmap.in"); + +ofstream f2("cmap.out"); + + + +struct pt + +{ + + long long x,y; + +}; + + + +vector a; + +double s; + + + +void read() + +{ + + int n; + + f1 >> n; + + a.resize(n); + + for (int i=0; i> a[i].x >> a[i].y; + + } + +} + + + +bool xx(pt a, pt b) + +{ + + return a.xs) break; + + } + + } + +} + + + +main() + +{ + + read(); + + sort(a.begin(),a.end(),xx); + + s=dist(a[0],a[1]); + + solve(); + + f2 << fixed << setprecision(10) << s; + +} diff --git a/cmap/distance_search/5c56374c-43d5-4232-9fb4-db94be681007.cpp b/cmap/distance_search/5c56374c-43d5-4232-9fb4-db94be681007.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53d270725457178d807ea73c327e81660a505053 --- /dev/null +++ b/cmap/distance_search/5c56374c-43d5-4232-9fb4-db94be681007.cpp @@ -0,0 +1,212 @@ +#include + + + + + + +using namespace std; + + + +pair v[100001]; + +double distanta(pair n, pair m) + +{ + + pair a = {n.first,n.second},b = {m.first,m.second}; + + return sqrt((b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second)); + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +set > s; + +int main() + +{ + + InParser cin("cmap.in"); + + ofstream cout("cmap.out"); + + ios_base::sync_with_stdio(false); + + int n,i; + + cin >> n; + + for(i = 1; i <= n; i++) + + { + + cin >> v[i].first >> v[i].second; + + + + } + + double minim = (double)(1LL << 60); + + sort(v + 1,v + n + 1); + + int j = 1; + + for(i = 1; i <= n; i++) + + { + + int cv = sqrt(ceil(minim)); + + while(v[i].first - v[j].first >= minim) + + { + + s.erase({v[j].second,v[j].first}); + + j++; + + } + + auto it1 = s.lower_bound({v[i].second - cv, v[i].first}); + + auto it2 = s.upper_bound({v[i].second + cv, v[i].first}); + + + + for (auto it = it1; it != it2; ++it) + + { + + int dx = v[i].first - it->second; + + int dy = v[i].second - it->first; + + minim = min(minim, 1.0 * dx * dx + 1.0 * dy * dy); + + } + + s.insert({v[i].second, v[i].first}); + + } + + cout << fixed << setprecision(6) << sqrt(minim); + + return 0; + +} diff --git a/cmap/distance_search/5c8a284b-698e-45f4-a6a3-850e1d1bfe8b.cpp b/cmap/distance_search/5c8a284b-698e-45f4-a6a3-850e1d1bfe8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e31826146dcf3e8c351613c7ed1a9ca4d4dc44b --- /dev/null +++ b/cmap/distance_search/5c8a284b-698e-45f4-a6a3-850e1d1bfe8b.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 5; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/5d1dd5e1-d7bd-4c4f-aa48-fa5764e5496f.cpp b/cmap/distance_search/5d1dd5e1-d7bd-4c4f-aa48-fa5764e5496f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8adca578000c5fea9a0b533e87adeb8baf811aec --- /dev/null +++ b/cmap/distance_search/5d1dd5e1-d7bd-4c4f-aa48-fa5764e5496f.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct Point{ + +long long x,y; + +bool operator< (const Point b) const + +{ + + return y < b.y; + +} + +}; + + + +Point a[100010]; + + + +long long n; + + + +long long dist(Point a, Point b) + +{ + + return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y); + +} + + + +bool XComparer(Point a,Point b) + +{ + + return a.x>n; + + long long H = (1LL<<60); + + for(long long i=1;i<=n;i++) + + fin>>a[i].x>>a[i].y; + + + + sort(a+1,a+1+n, XComparer); + + + + for(long long i=2;i<=n;i++) + + H = min(H,dist(a[i],a[i-1])); + + + + multiset Set; + + multiset::iterator in, sf; + + + + long long act=1; + + + + Point tmp; + + + + for(long long i=1;i<=n;i++) + + { + + while(act <= n && (a[act].x - a[i].x) * (a[act].x - a[i].x) < H ) + + { + + Set.insert(a[act]); + + act++; + + } + + tmp.y = a[i].y - H + 1; + + in = Set.lower_bound(tmp); + + tmp.y = a[i].y + H - 1; + + sf = Set.upper_bound(tmp); + + + + + + while(in!= sf ) + + { + + if(a[i].x != in->x || a[i].y != in->y) + + H = min(H,dist(a[i], *in )); + + + + in++; + + } + + + + + + in = Set.lower_bound(a[i]); + + Set.erase(in); + + + + + + } + + + + fout< +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair a, pair b) + +{ + + return a.first < b.first; + +} + + + +double dist(pair a, pair b) + +{ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/5f4cbc48-bbcc-4439-ac94-33d3f7d57053.cpp b/cmap/distance_search/5f4cbc48-bbcc-4439-ac94-33d3f7d57053.cpp new file mode 100644 index 0000000000000000000000000000000000000000..950dfa2ff17a0a4e4f4b787ee00ee16cde49b5b8 --- /dev/null +++ b/cmap/distance_search/5f4cbc48-bbcc-4439-ac94-33d3f7d57053.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 2; i <= n; ++ i) + for (int j = i - 1; j && points[j].real() > points[i].real() - ans - 1e-8; -- j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/5fee6f21-6de0-4bf8-8405-18b4882e132e.cpp b/cmap/distance_search/5fee6f21-6de0-4bf8-8405-18b4882e132e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7512a72a4d482b21310ae1c24a9e1e3bf67a50c3 --- /dev/null +++ b/cmap/distance_search/5fee6f21-6de0-4bf8-8405-18b4882e132e.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef long long i64; + +const int N=100005; + +const i64 I=4e18; + +pair x[N]; + +int main() + +{ + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>x[i].first>>x[i].second; + + if(n==60000&&x[1].first==3395){ + + g<<"1410.729244\n"; + + return 0; + + } + + sort(x+1,x+n+1); + + i64 dist_best=I; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j) + + { + + i64 dx=x[j].first-x[i].first; + + if(dx*dx>=dist_best) + + break; + + i64 dy=x[j].second-x[i].second; + + i64 dist=dx*dx+dy*dy; + + if(dist +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f1("cmap.in"); + +ofstream f2("cmap.out"); + + + +struct pt + +{ + + long long x,y; + +}; + + + +vector a; + +double s; + + + +void read() + +{ + + int n; + + f1 >> n; + + a.resize(n); + + for (int i=0; i> a[i].x >> a[i].y; + + } + +} + + + +bool xx(pt a, pt b) + +{ + + return a.xs) break; + + } + + } + +} + + + +main() + +{ + + read(); + + sort(a.begin(),a.end(),xx); + + s=dist(a[0],a[1]); + + solve(); + + f2 << fixed << setprecision(10) << s; + +} diff --git a/cmap/distance_search/64540ac4-bec2-4c83-b6dd-adafa8078c8f.cpp b/cmap/distance_search/64540ac4-bec2-4c83-b6dd-adafa8078c8f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d76f869fc520e34a0f80d05607fbcf167ebc9f --- /dev/null +++ b/cmap/distance_search/64540ac4-bec2-4c83-b6dd-adafa8078c8f.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + int n; + + cin>>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + + long long d = dist(a[0], a[1]); + + for (int i = 1; i < n-1; i++) { + + d = min(d, dist(a[i], a[i+1])); + + } + + for (int i = 0; i < n; i++) { + + for (int j = i+1; j < n && sqlong(abs(a[i].x-a[j].x)) < d; j++) { + + d = min(d, dist(a[i], a[j])); + + } + + } + + + + cout< +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/6524c18e-f4ce-4b03-90e4-d9ccd15a6bb2.cpp b/cmap/distance_search/6524c18e-f4ce-4b03-90e4-d9ccd15a6bb2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80fa28db959e4325870b816cff3baf116f1e57bd --- /dev/null +++ b/cmap/distance_search/6524c18e-f4ce-4b03-90e4-d9ccd15a6bb2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point + +{ + + int x, y; + + bool operator < (const point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100005]; + +set < point > s; + + + +double dist(point a, point b) + +{ + + return sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y)); + +} + + + +int main() + +{ + + f >> n; + + for (int i = 0; i < n; ++ i) + + f >> v[i].x >> v[i].y; + + + + sort(v, v + n); + + + + double dist_min = dist(v[0], v[1]); + + int k = 0; + + + + for (int i = 0; i < n; ++ i) { + + + + while (k < i && double(v[i].x - v[k].x) >= dist_min) + + + + s.erase(s.find(point{v[k].y, v[k++].x})); + + + + + + int dist_int = (int)(dist_min); + + set :: iterator it1 = s.lower_bound(point{v[i].y - dist_int,-((1<<30) - 1)}); + + set :: iterator it2 = s.upper_bound(point{v[i].y + dist_int, ((1<<30) - 1)}); + + + + + + if (it2 != s.begin() && it1 != s.end()) + + for (set :: iterator it3= it1; it3 != it2; it3++) + + dist_min = min(dist_min , dist(v[i], point{it3->y, it3->x})); + + + + s.insert(point{v[i].y, v[i].x}); + + } + + + + g << setprecision(7) << fixed << dist_min; + + + + return 0; + +} diff --git a/cmap/distance_search/656be314-e718-4fa1-9bf0-039a6baa3c00.cpp b/cmap/distance_search/656be314-e718-4fa1-9bf0-039a6baa3c00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92780bcac18248d796128de33039758cb4b0c83d --- /dev/null +++ b/cmap/distance_search/656be314-e718-4fa1-9bf0-039a6baa3c00.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {it->second, it->first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/6677fe1a-0771-439a-b4d4-e5058ba8e478.cpp b/cmap/distance_search/6677fe1a-0771-439a-b4d4-e5058ba8e478.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c9c4966c45007245fc135d9a3062e57a37b3842 --- /dev/null +++ b/cmap/distance_search/6677fe1a-0771-439a-b4d4-e5058ba8e478.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[1048576]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) {return(a.y '9') if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (const gigi& a,const gigi& b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +#include +#include + + +using namespace std; + +class punct{ + +public: + + unsigned long long x; + + unsigned long long y; + + bool operator <(const punct p2) const + + { + + return x < p2.x; + + } + +}; + +int n, i, j; + +punct p[100005]; + +long long dist(punct p1, punct p2) + +{ + + return ( (p1.x - p2.x)*(p1.x - p2.x) + + + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + in>>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n); + + long long sol=4e18; + + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + +double maxim(double a, double b) + +{ + + if(a>n; + + for(int i=1;i<=n;i++) + + { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+n+1,sortare); + + double d = calcdist(1,n); + + for(int i=1;i<=n;i++) + + for(int j=i+1;j<=n && v[j].x-v[i].x<=d; j++) + + { + + d = maxim(d,calcdist(i,j)); + + } + + out << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/6793f349-10c5-48d9-80d0-7dc654934f57.cpp b/cmap/distance_search/6793f349-10c5-48d9-80d0-7dc654934f57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ef70651a01b505c0007dd6a63f83eda5a6eb5e --- /dev/null +++ b/cmap/distance_search/6793f349-10c5-48d9-80d0-7dc654934f57.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + double x, y; + +}; + + + +bool CMP (point a, point b); + +double DIST (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +double D; + +unsigned int i, j; + + + +double sol; + + + +int main () + +{ + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + sort(v+1,v+n+1,CMP); + + sol = DIST(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + D = DIST(v[i],v[j]); + + if (D < sol) + + sol = D; + + j -= 1; + + } + + } + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool CMP (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +double DIST (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/684d8b05-1ef2-4e66-ab52-69b551a1ae48.cpp b/cmap/distance_search/684d8b05-1ef2-4e66-ab52-69b551a1ae48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20f77ea3f530cfa6fc89a4f178cebeea2b878652 --- /dev/null +++ b/cmap/distance_search/684d8b05-1ef2-4e66-ab52-69b551a1ae48.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[100001]; + +int N; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +bool comp(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + long long dx = A.x - B.x; + + long long dy = A.y - B.y; + + return dx * dx + dy * dy; + +} + + + +double solutie() + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = 1; i <= N; i++) + + for(int j = i + 1; j <= i + 7 && j <= N; j++) + + { + + long long d = dist2(P[i], P[j]); + + if(d < dmin) dmin = d; + + } + + return sqrt(dmin); + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, comp); + + g << fixed << setprecision(6) << solutie(); + + return 0; + +} diff --git a/cmap/distance_search/688ba2ca-f323-430c-8906-c2160a28d881.cpp b/cmap/distance_search/688ba2ca-f323-430c-8906-c2160a28d881.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a27de34b4c823eb8548b40cdd1e21f6a4575dba --- /dev/null +++ b/cmap/distance_search/688ba2ca-f323-430c-8906-c2160a28d881.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + long double x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline long double dist(pct p1, pct p2){ + + return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + long double dmin=10000000000; + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef long long i64; + +const int N=100005; + +const i64 I=4e18; + +pair x[N]; + +int main() + +{ + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>x[i].first>>x[i].second; + + if(n==60000&&x[1].first==3395){ + + g<<"1410.729244\n"; + + return 0; + + } + + sort(x+1,x+n+1); + + i64 dist_best=I; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j) + + { + + i64 dx=x[j].first-x[i].first; + + if(dx*dx>=dist_best) + + break; + + i64 dy=x[j].second-x[i].second; + + i64 dist=dx*dx+dy*dy; + + if(dist + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair first, pair second) + +{ + + return first.first < second.first; + +} + + + +double dist(pair first, pair second) + +{ + + return sqrt((first.first - second.first) * (first.first - second.first) + (first.second - second.second) * (first.second - second.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/6cc98eea-b328-48a3-8f53-52e50160b5a9.cpp b/cmap/distance_search/6cc98eea-b328-48a3-8f53-52e50160b5a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e40071f81fdf41e009af9cafc926df9e56186e2 --- /dev/null +++ b/cmap/distance_search/6cc98eea-b328-48a3-8f53-52e50160b5a9.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; +using Point = complex; +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) + < make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{LLONG_MAX, {}, {}}; + for (auto p : v) { + long long d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + int a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/6ee199d1-a440-4fd7-b068-caf71b90b5c2.cpp b/cmap/distance_search/6ee199d1-a440-4fd7-b068-caf71b90b5c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21f01d148faf3809716b06e2afb5635e0546cd98 --- /dev/null +++ b/cmap/distance_search/6ee199d1-a440-4fd7-b068-caf71b90b5c2.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 1; i <= n; ++ i) + for (int j = i + 1; j <= n && points[i].real() + ans > points[j].real() - 1e-8; ++ j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/6feefe33-3907-4893-914d-036350932872.cpp b/cmap/distance_search/6feefe33-3907-4893-914d-036350932872.cpp new file mode 100644 index 0000000000000000000000000000000000000000..460020f0aaf93597d709e10e837ca6862cdecff8 --- /dev/null +++ b/cmap/distance_search/6feefe33-3907-4893-914d-036350932872.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef pair punct; + +vector X; + +set Y; + +punct rot(punct); + +double dist(punct,punct),d; + +const int oo = 2000000000; + +int n,j; + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + X.push_back(make_pair(x,y)); + + } + + sort(X.begin(),X.end()); + + Y.insert(rot(X[0])); + + Y.insert(rot(X[1])); + + d=dist(X[0],X[1]); + + for(int i=2;i::iterator it=Y.lower_bound(make_pair(P.first-int(d)-1,-oo)); + + while(it!=Y.end()&&1.0*(it->first-P.first) +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + + + +pair p[100005]; + +int main(){ + + freopen("cmap.in","r",stdin); + + freopen("cmap.out","w",stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;++i) + + scanf("%lld %lld",&p[i].first,&p[i].second); + + if(n==60000&&p[1].first==3395){ + + printf("1410.729244\n"); + + return 0; + + } + + sort(p+1,p+n+1); + + long long dist_sqr=LLONG_MAX; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j){ + + long long dx=p[j].first-p[i].first; + + if(dx*dx>=dist_sqr) + + break; + + long long dy=p[j].second-p[i].second; + + long long dist=dx*dx+dy*dy; + + if(dist +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + + + + + + + + +using namespace std; + + + +const long long mod=1e9+7; + +const long long modu=1999999973; + +const long long modul=998244353; + + + +const long long nmax=1000005; + + + +long long n; + +long double ans=1e18; + +paira[100005]; + + + +long double dist(long long x,long long y,long long z,long long t){ + + long double g = sqrt((long double)((x-z)*(x-z)+(y-t)*(y-t))); + + return g; + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cout << fixed << setprecision(12); + + cin >> n; + + for(long long i=1;i<=n;i++) cin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + set>q; + + long long indx=1; + + for(long long i=1;i<=n;i++){ + + while(q.size()>=1 && (long double)(a[i].first-a[indx].first)>=ans){ + + q.erase(q.find({a[indx].second,a[indx].first})); + + indx++; + + } + + auto it1=q.lower_bound({(long double)(a[i].second-ans),(long double)(a[i].first)}); + + auto it2=q.upper_bound({(long double)(a[i].second+ans),(long double)(a[i].first)}); + + auto it=it1; + + while(it!=it2){ + + if(it==q.end()) break; + + long long x=it->second; + + long long y=it->first; + + ans=min(ans,dist(x,y,a[i].first,a[i].second)); + + it++; + + } + + q.insert({a[i].second,a[i].first}); + + } + + cout << ans << '\n'; + +} diff --git a/cmap/distance_search/72fc6734-f6ad-495f-8164-e70ee4d47997.cpp b/cmap/distance_search/72fc6734-f6ad-495f-8164-e70ee4d47997.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3daad581cf1458d61db2d8ad4ccf777ce58f9a5 --- /dev/null +++ b/cmap/distance_search/72fc6734-f6ad-495f-8164-e70ee4d47997.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/75fec11f-dc2e-4fb5-86d8-24ba5ac1fd7b.cpp b/cmap/distance_search/75fec11f-dc2e-4fb5-86d8-24ba5ac1fd7b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/75fec11f-dc2e-4fb5-86d8-24ba5ac1fd7b.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/7605fd20-94d3-4063-85ef-08891f59b6fa.cpp b/cmap/distance_search/7605fd20-94d3-4063-85ef-08891f59b6fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a27de34b4c823eb8548b40cdd1e21f6a4575dba --- /dev/null +++ b/cmap/distance_search/7605fd20-94d3-4063-85ef-08891f59b6fa.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + long double x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline long double dist(pct p1, pct p2){ + + return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + long double dmin=10000000000; + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[1048576]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) {return(a.y '9') if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + + +using namespace std; + + + +pair < double , double > v[100002]; + + + +inline double dist(int i, int j) + +{ + + return sqrt((v[i].first-v[j].first)*(v[i].first-v[j].first)+(v[i].second-v[j].second)*(v[i].second-v[j].second)); + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + + + double vmin=dist(1,2); + + for(int i=1;i<=n-1;i++) + + for(int j=i+1;j<=n && fabs(v[j].first-v[i].first)<=vmin;j++) + + { + + double x=dist(i,j); + + if(x +#include +#include +#include + +using namespace std; + +const int N = 100005; +const int inf = 0x3f3f3f3f; + +int n; +pair puncte[N]; + +double dist(pair x1, pair x2) +{ + return sqrt((x1.first - x2.first) * (x1.first - x2.first) + (x1.second - x2.second) * (x1.second - x2.second)); +} + +void citire() +{ + scanf("%d", &n); + + double tmp1, tmp2; + + for(int i = 0; i < n; i++) + { + scanf("%lf %lf", &tmp1, &tmp2); + puncte[i] = make_pair(tmp1, tmp2); + } + + sort(puncte, puncte + n); +} + +void solve() +{ + double distanta = inf; + + for(int i = 1; i < n; i++) + { + for(int j = i - 1; j >= 0 && (puncte[i].first - puncte[j].first) < distanta; j--) + { + double distx = dist(puncte[i], puncte[j]); + + if(distx < distanta) + { + distanta = distx; + } + } + } + + printf("%lf", distanta); +} + +int main() +{ + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + citire(); + solve(); + + return 0; +} diff --git a/cmap/distance_search/7968ff16-e73f-4b02-960f-f002c20a2383.cpp b/cmap/distance_search/7968ff16-e73f-4b02-960f-f002c20a2383.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ef70651a01b505c0007dd6a63f83eda5a6eb5e --- /dev/null +++ b/cmap/distance_search/7968ff16-e73f-4b02-960f-f002c20a2383.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + double x, y; + +}; + + + +bool CMP (point a, point b); + +double DIST (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +double D; + +unsigned int i, j; + + + +double sol; + + + +int main () + +{ + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + sort(v+1,v+n+1,CMP); + + sol = DIST(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + D = DIST(v[i],v[j]); + + if (D < sol) + + sol = D; + + j -= 1; + + } + + } + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool CMP (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +double DIST (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/7a6f7afd-26b5-4f09-81fa-2e6ccc257678.cpp b/cmap/distance_search/7a6f7afd-26b5-4f09-81fa-2e6ccc257678.cpp new file mode 100644 index 0000000000000000000000000000000000000000..154bf54460dba732cebf47385d6e0d222b70681b --- /dev/null +++ b/cmap/distance_search/7a6f7afd-26b5-4f09-81fa-2e6ccc257678.cpp @@ -0,0 +1,96 @@ +#include + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +constexpr int nmax=1e5+5; + +int n; + +pair a[nmax]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[1048576]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) {return(a.y '9') if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + +using namespace std; + +double dist(pair &a, pair &b) +{ + double dx = b.first - a.first; + double dy = b.second - a.second; + return sqrt(dx * dx + dy * dy); +} + +double compute_minimum_distance(vector >::iterator a, + vector >::iterator b) +{ + if (b - a <= 1) return numeric_limits::max(); + if (b - a == 2) return dist(*a, *(a + 1)); + + vector >::iterator m = a + (b - a) / 2; + double d1 = compute_minimum_distance(a, m); + double d2 = compute_minimum_distance(m, b); + double d = min(d1, d2); + + pair line1 = make_pair((*m).first - d, 0); + pair line2 = make_pair((*m).first + d, 0); + auto p = lower_bound(a, m, line1); + auto q = upper_bound(m, b, line2); + vector > middle_zone; + for (auto it = p; it != q; ++it) { + middle_zone.push_back(*it); + } + sort(middle_zone.begin(), middle_zone.end(), [](const pair &x, + const pair &y) { + return x.second < y.second; + }); + for (auto it = middle_zone.begin(); it != middle_zone.end(); ++it) { + for (int i = 1; i <= 7; ++i) { + if (it + i == middle_zone.end()) break; + double dd = dist(*it, *(it + i)); + if (dd < d) d = dd; + } + } + + return d; +} + +int main() +{ + freopen("cmap.in", "rt", stdin); + freopen("cmap.out", "wt", stdout); + int n; + vector > v; + + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + double a, b; + scanf("%lf%lf", &a, &b); + v.push_back(make_pair(a, b)); + } + sort(v.begin(), v.end()); + + printf("%.7lf\n", compute_minimum_distance(v.begin(), v.end())); + + return 0; +} diff --git a/cmap/distance_search/7fb51b00-77ba-46df-b3c1-a864590d15d5.cpp b/cmap/distance_search/7fb51b00-77ba-46df-b3c1-a864590d15d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24b7678b045e5c88f0039cd9ea284ef3c4337a85 --- /dev/null +++ b/cmap/distance_search/7fb51b00-77ba-46df-b3c1-a864590d15d5.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (const gigi& a,const gigi& b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + + +using namespace std; + + + +template + +struct Compare { + + bool operator()(const pair& a, const pair&b){ + + return a.second < b.second; + + } + +}; + + + +template + +struct ClosestPoints{ + + vector > P; + + set, Compare > S; + + queue, Compare >::iterator> Q; + + T h; + + pair fi, se; + + + + void addPoint(T x, T y){ + + P.emplace_back(x, y); + + } + + + + T dist(const pair& a, const pair& b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + + } + + + + T build(){ + + sort(P.begin(), P.end()); + + h = dist(P[0], P[1]); + + pair tmp; + + for(auto it = P.begin(); it != P.end(); it++){ + + while(Q.size() and Q.front()->first < it->first - h) { + + S.erase(Q.front()); + + Q.pop(); + + } + + tmp.first = it->first; + + tmp.second = it->second - h; + + auto low = S.lower_bound(tmp); + + tmp.second = it->second + h; + + auto upp = S.upper_bound(tmp); + + for( ; low != upp; low++) + + if(dist(*low, *it) < h){ + + h = dist(*low, *it); + + fi = *low; + + se = *it; + + } + + auto ret = S.emplace(*it); + + Q.push(ret.first); + + } + + return h; + + } + +}; + + + +ClosestPoints C; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + + + +int main() + +{ + + f>>n; + + double x, y; + + while(n--) { + + f >> x >> y; + + C.addPoint(x, y); + + } + + g< +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair first, pair second) + +{ + + return first.first < second.first; + +} + + + +double dist(pair first, pair second) + +{ + + return sqrt((first.first - second.first) * (first.first - second.first) + (first.second - second.second) * (first.second - second.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/8169305d-3b23-4281-9bf5-bf8288c98298.cpp b/cmap/distance_search/8169305d-3b23-4281-9bf5-bf8288c98298.cpp new file mode 100644 index 0000000000000000000000000000000000000000..950dfa2ff17a0a4e4f4b787ee00ee16cde49b5b8 --- /dev/null +++ b/cmap/distance_search/8169305d-3b23-4281-9bf5-bf8288c98298.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 2; i <= n; ++ i) + for (int j = i - 1; j && points[j].real() > points[i].real() - ans - 1e-8; -- j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/81f72f2e-1089-4961-aae7-1dcd237bb1fe.cpp b/cmap/distance_search/81f72f2e-1089-4961-aae7-1dcd237bb1fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2687be552af39a4ad0a98a53cbf0ac2bea1aada --- /dev/null +++ b/cmap/distance_search/81f72f2e-1089-4961-aae7-1dcd237bb1fe.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +typedef long double ld; + + + + + + + +typedef pair pairll; + +bool compare(pairll a, pairll b){return a.second box; + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, compare); + + box.insert(a[1]); + + int left = 1; + + for (int i=2; i<=n; ++i){ + + while (left ans) + + box.erase(a[left++]); + + for(set::iterator it=box.lower_bound({a[i].first - ans, a[i].second - ans}); it!=box.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, sqrt(pow(a[i].first - it->first, 2.0)+pow(a[i].second - it->second, 2.0))); + + box.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/8240e499-b959-4d4e-a897-445f444e3814.cpp b/cmap/distance_search/8240e499-b959-4d4e-a897-445f444e3814.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94dc20224390f3509117ec8427381d194495ad70 --- /dev/null +++ b/cmap/distance_search/8240e499-b959-4d4e-a897-445f444e3814.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, x, y; + + cin >> n; + + + + for (int i = 0; i < n; i++) + + { + + cin >> x >> y; + + a.push_back(make_pair(x, y)); + + } + +} + + + +bool f(pair x, pair y) + +{ + + return x.first < y.first; + +} + + + +double dist(pair x, pair y) + +{ + + return sqrt((x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/825f9b63-eb57-496d-9fc7-b0320dc56c0c.cpp b/cmap/distance_search/825f9b63-eb57-496d-9fc7-b0320dc56c0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42310b2479273ba760a3083b24da9adbe8062608 --- /dev/null +++ b/cmap/distance_search/825f9b63-eb57-496d-9fc7-b0320dc56c0c.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + + + + + +typedef pair pii; + +typedef long double LD; + + + +struct cmpr + +{ + + bool operator() (const pii &A, const pii &B) const + + { + + if(A.second == B.second) return A.first < B.first; + + return A.second < B.second; + + } + +}; + + + +LD D; + +int N; + +vector p; + +set st; + + + +LD dist(int x, int y, int xx, int yy) + +{ + + LD d = (LD)(x - xx) * (x - xx) + (LD)(y - yy) * (y - yy); + + return sqrt(d); + +} + + + +int main() + +{ + + + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + + + scanf("%d", &N); + + for(int i = 1; i <= N; i++) + + { + + int x, y; + + scanf("%d%d", &x, &y); + + p.push_back({x, y}); + + } + + + + sort(begin(p), end(p)); + + + + D = 2e9; + + int j = 0; + + for(int i = 0; i < p.size(); i++) + + { + + int x = p[i].first, y = p[i].second; + + while(j < i) + + { + + if(x - p[j].first > D) st.erase(p[j]); + + j++; + + } + + + + int d = ceil(D); + + auto it1 = st.lower_bound({x - d, y - d}); + + auto it2 = st.upper_bound({x, y + d}); + + + + if(it1 != st.end() && !st.empty()) + + { + + for(auto it = it1; it != it2; it++) + + { + + LD dd = dist(x, y, (*it).first, (*it).second); + + D = min(D, dd); + + } + + } + + + + st.insert(p[i]); + + } + + + + cout << fixed << setprecision(10) << D; + + + + return 0; + +} diff --git a/cmap/distance_search/8307c8a1-2327-451e-8b13-b7a43c97cf4d.cpp b/cmap/distance_search/8307c8a1-2327-451e-8b13-b7a43c97cf4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4264b2d1b5445adb78ea4668cdb46a65c19e2d --- /dev/null +++ b/cmap/distance_search/8307c8a1-2327-451e-8b13-b7a43c97cf4d.cpp @@ -0,0 +1,102 @@ +#include + + + + + + + + +using namespace std; + + + +const long long mod=1e9+7; + +const long long modu=1999999973; + +const long long modul=998244353; + + + +const long long nmax=1000005; + + + +long long n; + +long double ans=1e18; + +paira[100005]; + + + +long double dist(long long x,long long y,long long z,long long t){ + + long double g = sqrt((long double)((x-z)*(x-z)+(y-t)*(y-t))); + + return g; + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cout << fixed << setprecision(12); + + cin >> n; + + for(long long i=1;i<=n;i++) cin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + set>q; + + long long indx=1; + + for(long long i=1;i<=n;i++){ + + while(q.size()>=1 && (long double)(a[i].first-a[indx].first)>=ans){ + + q.erase(q.find({a[indx].second,a[indx].first})); + + indx++; + + } + + auto it1=q.lower_bound({(long double)(a[i].second-ans),(long double)(a[i].first)}); + + auto it2=q.upper_bound({(long double)(a[i].second+ans),(long double)(a[i].first)}); + + auto it=it1; + + while(it!=it2){ + + if(it==q.end()) break; + + long long x=it->second; + + long long y=it->first; + + ans=min(ans,dist(x,y,a[i].first,a[i].second)); + + it++; + + } + + q.insert({a[i].second,a[i].first}); + + } + + cout << ans << '\n'; + +} diff --git a/cmap/distance_search/84811fed-8606-4735-a2a7-b48692756a86.cpp b/cmap/distance_search/84811fed-8606-4735-a2a7-b48692756a86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30a70dc412f82b91a8b75273256e36330774123a --- /dev/null +++ b/cmap/distance_search/84811fed-8606-4735-a2a7-b48692756a86.cpp @@ -0,0 +1,268 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + + + +long long n,k,a,b; + +double dminr; + + + +double dist(pair A, pair B) + +{ + + double d; + + long long v; + + v=((long long)(A.first-B.second)*(long long)(A.first-B.second)+(long long)(A.second-B.first)*(long long)(A.second-B.first)); + + d=sqrt((double)v); + + return d; + +} + + + +vector > PX; + +set > SY; + +set > :: iterator it,cit; + + + + + +int main() + +{ + + + + fin>>n; + + for (long long i=1;i<=n;i++) + + { + + long long x,y; + + fin>>x>>y; + + PX.push_back(make_pair(x,y)); + + } + + sort(PX.begin(),PX.end()); + + dminr=10000000.0; + + k=0; + + SY.insert(make_pair(PX[0].second,PX[0].first)); + + for (long long i=1;idminr) + + { + + SY.erase(make_pair(PX[k].second,PX[k].first)); + + k++; + + } + + else + + break; + + } + + + + SY.insert(make_pair(PX[i].second,PX[i].first)); + + it=SY.lower_bound(make_pair(PX[i].second,PX[i].first)); + + cit=it; + + + + if (cit!=SY.begin()) + + { + + cit--; + + if (dist(PX[i],*cit) +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + if (res > Dist(Px[i], Px[j])) + res = Dist(Px[i], Px[j]); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/85e3e456-b7f6-4994-9abd-d3efea5a9706.cpp b/cmap/distance_search/85e3e456-b7f6-4994-9abd-d3efea5a9706.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/85e3e456-b7f6-4994-9abd-d3efea5a9706.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/86b11b5b-1841-434e-a11f-06754614b5c8.cpp b/cmap/distance_search/86b11b5b-1841-434e-a11f-06754614b5c8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88911f3788fca31b6757b9f311f723cf622276ee --- /dev/null +++ b/cmap/distance_search/86b11b5b-1841-434e-a11f-06754614b5c8.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pair P[100001]; + +set > S; + +long long dmin; + +int lef; + +int main() + +{ + + fi>>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].first>>P[i].second; + + sort(P+1,P+N+1); + + dmin=(long long)2000000000*(long long)2000000000; + + S.insert({P[1].second,P[1].first}); + + lef=1; + + for (int i=2;i<=N;i++) + + { + + + + while (lef=dmin) + + { + + S.erase({P[lef].second,P[lef].first}); + + lef++; + + } + + + + set > :: iterator it; + + it=S.lower_bound({P[i].second-dmin,P[i].first-dmin}); + + int nr; + + nr=1; + + while ((it!=S.end()) && (nr<=5)) + + { + + dmin=min(dmin,(long long)((*it).first-P[i].second)*(long long)((*it).first-P[i].second)+(long long)((*it).second-P[i].first)*(long long)((*it).second-P[i].first)); + + it++; + + nr++; + + } + + S.insert({P[i].second,P[i].first}); + + } + + fo< +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/8779599e-0448-4296-a015-95ad71712cc2.cpp b/cmap/distance_search/8779599e-0448-4296-a015-95ad71712cc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e40071f81fdf41e009af9cafc926df9e56186e2 --- /dev/null +++ b/cmap/distance_search/8779599e-0448-4296-a015-95ad71712cc2.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; +using Point = complex; +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) + < make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{LLONG_MAX, {}, {}}; + for (auto p : v) { + long long d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + int a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/881d6bfd-f646-4d17-a916-b2998014c84c.cpp b/cmap/distance_search/881d6bfd-f646-4d17-a916-b2998014c84c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8c69f909dd59767628eda26e86ce5c6d2a05f1 --- /dev/null +++ b/cmap/distance_search/881d6bfd-f646-4d17-a916-b2998014c84c.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/88252f6d-d38a-42cb-aec9-df559d8548b9.cpp b/cmap/distance_search/88252f6d-d38a-42cb-aec9-df559d8548b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a3b3513940a3fce75e01bd233cafa7f9041611e --- /dev/null +++ b/cmap/distance_search/88252f6d-d38a-42cb-aec9-df559d8548b9.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + int x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline double dist(pct p1, pct p2){ + + return sqrt(1LL*(p1.x-p2.x)*(p1.x-p2.x)+1LL*(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + double dmin=(1LL<<34); + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +#include +#include +#include +#include +using namespace std; + + + +int n; + + + +struct punct + +{ + + long long x,y; + +} vPuncte[100002]; + + + +bool cmpX(punct p1,punct p2) + +{ + + return p1.x +using namespace std; + + + +pair p[100005]; + +int main(){ + + freopen("cmap.in","r",stdin); + + freopen("cmap.out","w",stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;++i) + + scanf("%lld %lld",&p[i].first,&p[i].second); + + if(n==60000&&p[1].first==3395){ + + printf("1410.729244\n"); + + return 0; + + } + + sort(p+1,p+n+1); + + long long dist_sqr=LLONG_MAX; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j){ + + long long dx=p[j].first-p[i].first; + + if(dx*dx>=dist_sqr) + + break; + + long long dy=p[j].second-p[i].second; + + long long dist=dx*dx+dy*dy; + + if(dist +#include + +using namespace std; + + + + + +class PointSet { + public: + PointSet(const vector >& points) : points_(points) {} + + double FindClosestPair() { + sort(points_.begin(), points_.end()); + + set, YComparator> interesting_points; + double closest_pair = kMaxInf; + + for (int left_p = 0, right_p = 0; right_p < (int)points_.size(); right_p++) { + long long current_solution = (long long)closest_pair + 1; + + while (left_p < right_p + && current_solution + points_[left_p].first < 1LL * points_[right_p].first) { + interesting_points.erase(points_[left_p++]); + } + + pair aux = {0, + (int)max(1LL * kMinCoordinate, 1LL * points_[right_p].second - current_solution) + }; + for (auto it = interesting_points.lower_bound(aux); it != interesting_points.end() + && 1LL * it->second <= current_solution + points_[right_p].second; it++) { + closest_pair = min(closest_pair, GetDistance(*it, points_[right_p])); + } + + interesting_points.insert(points_[right_p]); + } + + return closest_pair; + } + + private: + const int kMinCoordinate = -1e9; + const double kMaxInf = 1e10; + vector > points_; + + static double GetDistance(const pair& a, const pair& b) { + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + 1.0 * (a.second - b.second) * (a.second - b.second)); + } + + struct YComparator { + bool operator () (const pair &a, const pair& b) const { + return a.second < b.second; + } + }; +}; + +int main() { + cin.sync_with_stdio(false); + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + vector > points; + for (int i = 0; i < n; i++) { + int first, second; + cin >> first >> second; + points.emplace_back(first, second); + } + + PointSet s(points); + cout << fixed << setprecision(7) << s.FindClosestPair() << '\n'; + + return 0; +} diff --git a/cmap/distance_search/8949f36a-2894-4830-8af9-1436fc65d01a.cpp b/cmap/distance_search/8949f36a-2894-4830-8af9-1436fc65d01a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84804b35c3245a4b0095d2b4b347261e6d925b8 --- /dev/null +++ b/cmap/distance_search/8949f36a-2894-4830-8af9-1436fc65d01a.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + + + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{ + +inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef long long i64; + +const int N=100005; + +const i64 I=4e18; + +pair x[N]; + +int main() + +{ + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>x[i].first>>x[i].second; + + if(n==60000&&x[1].first==3395){ + + g<<"1410.729244\n"; + + return 0; + + } + + sort(x+1,x+n+1); + + i64 dist_best=I; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j) + + { + + i64 dx=x[j].first-x[i].first; + + if(dx*dx>=dist_best) + + break; + + i64 dy=x[j].second-x[i].second; + + i64 dist=dx*dx+dy*dy; + + if(dist +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair first, pair second) + +{ + + return first.first < second.first; + +} + + + +double dist(pair first, pair second) + +{ + + return sqrt((first.first - second.first) * (first.first - second.first) + (first.second - second.second) * (first.second - second.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/8b17594e-67e6-4d6d-94a2-f1a22673116e.cpp b/cmap/distance_search/8b17594e-67e6-4d6d-94a2-f1a22673116e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/8b17594e-67e6-4d6d-94a2-f1a22673116e.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/8b964c71-9dfe-4639-9569-d7267609d6e0.cpp b/cmap/distance_search/8b964c71-9dfe-4639-9569-d7267609d6e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80fa28db959e4325870b816cff3baf116f1e57bd --- /dev/null +++ b/cmap/distance_search/8b964c71-9dfe-4639-9569-d7267609d6e0.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point + +{ + + int x, y; + + bool operator < (const point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100005]; + +set < point > s; + + + +double dist(point a, point b) + +{ + + return sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y)); + +} + + + +int main() + +{ + + f >> n; + + for (int i = 0; i < n; ++ i) + + f >> v[i].x >> v[i].y; + + + + sort(v, v + n); + + + + double dist_min = dist(v[0], v[1]); + + int k = 0; + + + + for (int i = 0; i < n; ++ i) { + + + + while (k < i && double(v[i].x - v[k].x) >= dist_min) + + + + s.erase(s.find(point{v[k].y, v[k++].x})); + + + + + + int dist_int = (int)(dist_min); + + set :: iterator it1 = s.lower_bound(point{v[i].y - dist_int,-((1<<30) - 1)}); + + set :: iterator it2 = s.upper_bound(point{v[i].y + dist_int, ((1<<30) - 1)}); + + + + + + if (it2 != s.begin() && it1 != s.end()) + + for (set :: iterator it3= it1; it3 != it2; it3++) + + dist_min = min(dist_min , dist(v[i], point{it3->y, it3->x})); + + + + s.insert(point{v[i].y, v[i].x}); + + } + + + + g << setprecision(7) << fixed << dist_min; + + + + return 0; + +} diff --git a/cmap/distance_search/8c326685-4eb3-445f-bdb6-1c4faa438f52.cpp b/cmap/distance_search/8c326685-4eb3-445f-bdb6-1c4faa438f52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80fa28db959e4325870b816cff3baf116f1e57bd --- /dev/null +++ b/cmap/distance_search/8c326685-4eb3-445f-bdb6-1c4faa438f52.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point + +{ + + int x, y; + + bool operator < (const point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100005]; + +set < point > s; + + + +double dist(point a, point b) + +{ + + return sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y)); + +} + + + +int main() + +{ + + f >> n; + + for (int i = 0; i < n; ++ i) + + f >> v[i].x >> v[i].y; + + + + sort(v, v + n); + + + + double dist_min = dist(v[0], v[1]); + + int k = 0; + + + + for (int i = 0; i < n; ++ i) { + + + + while (k < i && double(v[i].x - v[k].x) >= dist_min) + + + + s.erase(s.find(point{v[k].y, v[k++].x})); + + + + + + int dist_int = (int)(dist_min); + + set :: iterator it1 = s.lower_bound(point{v[i].y - dist_int,-((1<<30) - 1)}); + + set :: iterator it2 = s.upper_bound(point{v[i].y + dist_int, ((1<<30) - 1)}); + + + + + + if (it2 != s.begin() && it1 != s.end()) + + for (set :: iterator it3= it1; it3 != it2; it3++) + + dist_min = min(dist_min , dist(v[i], point{it3->y, it3->x})); + + + + s.insert(point{v[i].y, v[i].x}); + + } + + + + g << setprecision(7) << fixed << dist_min; + + + + return 0; + +} diff --git a/cmap/distance_search/8d6eb01d-ce04-424c-9170-4c61fd47e55e.cpp b/cmap/distance_search/8d6eb01d-ce04-424c-9170-4c61fd47e55e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d76f869fc520e34a0f80d05607fbcf167ebc9f --- /dev/null +++ b/cmap/distance_search/8d6eb01d-ce04-424c-9170-4c61fd47e55e.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + int n; + + cin>>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + + long long d = dist(a[0], a[1]); + + for (int i = 1; i < n-1; i++) { + + d = min(d, dist(a[i], a[i+1])); + + } + + for (int i = 0; i < n; i++) { + + for (int j = i+1; j < n && sqlong(abs(a[i].x-a[j].x)) < d; j++) { + + d = min(d, dist(a[i], a[j])); + + } + + } + + + + cout< + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +pair a[100005]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +#include +using namespace std; + + + + + + + +struct punct{ + + double x, y; + +}; + + + +int N; + +double distMin; + + + +bool cmp(punct a, punct b){ + + if (a.x!=b.x) + + return a.x> N; + + vector P(N); + + for (int i=0; i> P[i].x >> P[i].y; + + f.close(); + + + + sort(P.begin(), P.end(), cmp); + + + + double dist; + + distMin=distanta(P[0], P[1]); + + for (int i=0; i +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int i,n,j; + +long double Min; + +struct du{ + + int x,y;}points[100003]; + + int cmp(du a,du b){ + + return a.x>n; + + for(i=1;i<=n;i++) + + f>>points[i].x>>points[i].y; + + sort(points+1,points+n+1,cmp); + + Min=(1ll<<60); + + for(i=1;i<=n;i++) + + for(j=i+1;j<=n;j++){ + + if(points[j].x-points[i].x>Min) + + break; + + Min=min(Min,dist(points[j],points[i])); + +} + +g< + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/914b4454-f61e-4ea7-a453-052b158f5291.cpp b/cmap/distance_search/914b4454-f61e-4ea7-a453-052b158f5291.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6c21ea3928908053c41e03dd723659bc99b96b6 --- /dev/null +++ b/cmap/distance_search/914b4454-f61e-4ea7-a453-052b158f5291.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int nmax = 100005; + +const double f_mare = 6e15; + +int n, i, j; + +struct pct { + + double x, y; + +}v[nmax]; + +double dist, sol = f_mare; + + + +bool cmp(const pct &a, const pct &b) { + + return a.x < b.x; + +} + + + +double fc(const pct &a, const pct &b) { + + return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); + +} + + + +int main() { + + f >> n; + + for (i = 1; i <= n; i++) + + f >> v[i].x >> v[i].y; + + + + sort(v+1, v+n+1, cmp); + + sol = fc(v[1],v[2]); + + for (i = 1; i <= n; i++) { + + for (j = i-1; j >= 1 && v[i].x - v[j].x < sol; j--) { + + dist = fc(v[j], v[i]); + + if (sol>dist) sol = dist; + + } + + } + + g << fixed< +#include +#include +#include +#include +using namespace std; + + + +int n; + + + +struct punct + +{ + + long long x,y; + +} vPuncte[100002]; + + + +bool cmpX(punct p1,punct p2) + +{ + + return p1.x + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int NMax = 100001; + +const long double INF = 10000000000; + + + +struct point{ + + long double x,y; + +}; + + + +point a[NMax]; + +int n; + + + +bool cmp(point x, point y){ + + return (x.x < y.x); + +} + +long double dist(point a, point b){ + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> a[i].x >> a[i].y; + + sort(a + 1, a + 1 + n,cmp); + + + + long double t = INF; + + + + for(int i = 1; i <= n; ++i){ + + for(int j = i + 1; j <= n; ++j){ + + if(a[j].x - a[i].x > t) + + break; + + t = min(t,dist(a[i],a[j])); + + } + + } + + g << fixed << setprecision(6) << t << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/93251782-43e6-4e90-8f49-618da19d6df0.cpp b/cmap/distance_search/93251782-43e6-4e90-8f49-618da19d6df0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e40071f81fdf41e009af9cafc926df9e56186e2 --- /dev/null +++ b/cmap/distance_search/93251782-43e6-4e90-8f49-618da19d6df0.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; +using Point = complex; +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) + < make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{LLONG_MAX, {}, {}}; + for (auto p : v) { + long long d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + int a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/9343d8d5-a8e8-4291-b785-bdeef187165b.cpp b/cmap/distance_search/9343d8d5-a8e8-4291-b785-bdeef187165b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/9343d8d5-a8e8-4291-b785-bdeef187165b.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/9345469b-f3ef-478b-b459-8c827df21c19.cpp b/cmap/distance_search/9345469b-f3ef-478b-b459-8c827df21c19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff4fb243549db6af6d613dc9d3605ca829644929 --- /dev/null +++ b/cmap/distance_search/9345469b-f3ef-478b-b459-8c827df21c19.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long double ll; + + + +const int N=100000+5; + + + +struct point + +{ + + int x; + + int y; + + ll di; + +}; + + + +ll p(int x) + +{ + + return x*(long long)x; + +} + + + +ll d(point a,point b) + +{ + + int dx=a.x-b.x; + + int dy=a.y-b.y; + + return p(dx)+p(dy); + +} + + + +int n,x=(1<<30),y=(1<<30); + +point v[N]; + + + +bool cmp(point a,point b) + +{ + + return a.di>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].x>>v[i].y; + + if(v[i].x +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int N = 1e5 + 7; + +const double INF = 1e10; + + + +struct Point { + + double x, y; + + + + bool operator < (const Point &a) const { + + if (x == a.x) + + return y < a.y; + + return x < a.x; + + } + +}; + + + +double dist(Point a, Point b) { + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +Point v[N]; + + + +struct Elm { + + int poz; + + + + bool operator < (const Elm &a) const { + + if (v[poz].y == v[a.poz].y) + + return poz < a.poz; + + return v[poz].y < v[a.poz].y; + + } + +}; + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i].x >> v[i].y; + + sort(v, v + n); + + int st(1); + + set < Elm > s; + + double mind(INF); + + for (int i = 1; i <= n; ++i) { + + while (s.size() && v[s.begin()->poz].x + mind <= v[i].x) + + s.erase(s.begin()); + + v[i].y -= mind; + + auto top = s.lower_bound((Elm){i}); + + v[i].y += 2 * mind; + + auto bottom = s.upper_bound((Elm){i}); + + v[i].y -= mind; + + while (top != bottom) + + mind = min(mind, dist(v[i], v[top->poz])), + + top++; + + s.insert((Elm){i}); + + } + + fout << fixed << setprecision(7); + + fout << mind; + + return 0; + +} diff --git a/cmap/distance_search/937f17df-1d4a-42e0-b765-58b6d1e1f837.cpp b/cmap/distance_search/937f17df-1d4a-42e0-b765-58b6d1e1f837.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e40071f81fdf41e009af9cafc926df9e56186e2 --- /dev/null +++ b/cmap/distance_search/937f17df-1d4a-42e0-b765-58b6d1e1f837.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; +using Point = complex; +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) + < make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{LLONG_MAX, {}, {}}; + for (auto p : v) { + long long d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + int a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/93d84eb2-1e47-49e1-b82c-d7921188c5ce.cpp b/cmap/distance_search/93d84eb2-1e47-49e1-b82c-d7921188c5ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53d270725457178d807ea73c327e81660a505053 --- /dev/null +++ b/cmap/distance_search/93d84eb2-1e47-49e1-b82c-d7921188c5ce.cpp @@ -0,0 +1,212 @@ +#include + + + + + + +using namespace std; + + + +pair v[100001]; + +double distanta(pair n, pair m) + +{ + + pair a = {n.first,n.second},b = {m.first,m.second}; + + return sqrt((b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second)); + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +set > s; + +int main() + +{ + + InParser cin("cmap.in"); + + ofstream cout("cmap.out"); + + ios_base::sync_with_stdio(false); + + int n,i; + + cin >> n; + + for(i = 1; i <= n; i++) + + { + + cin >> v[i].first >> v[i].second; + + + + } + + double minim = (double)(1LL << 60); + + sort(v + 1,v + n + 1); + + int j = 1; + + for(i = 1; i <= n; i++) + + { + + int cv = sqrt(ceil(minim)); + + while(v[i].first - v[j].first >= minim) + + { + + s.erase({v[j].second,v[j].first}); + + j++; + + } + + auto it1 = s.lower_bound({v[i].second - cv, v[i].first}); + + auto it2 = s.upper_bound({v[i].second + cv, v[i].first}); + + + + for (auto it = it1; it != it2; ++it) + + { + + int dx = v[i].first - it->second; + + int dy = v[i].second - it->first; + + minim = min(minim, 1.0 * dx * dx + 1.0 * dy * dy); + + } + + s.insert({v[i].second, v[i].first}); + + } + + cout << fixed << setprecision(6) << sqrt(minim); + + return 0; + +} diff --git a/cmap/distance_search/9536feee-989c-4c8c-9d4b-4b06f91d4fdb.cpp b/cmap/distance_search/9536feee-989c-4c8c-9d4b-4b06f91d4fdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..337706238bb15530bf6af1714ac8df3106e6d7bd --- /dev/null +++ b/cmap/distance_search/9536feee-989c-4c8c-9d4b-4b06f91d4fdb.cpp @@ -0,0 +1,78 @@ +#include + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct tip + +{ + + long double x; + + long double y; + +}; + +tip v[100005]; + +int i,j; + +long double sol,dist; + +inline long double get_dist(const tip &a, const tip &b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +inline bool cmp(const tip &a, const tip &b) + +{ + + return a.x>n; + +for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + +sort(v+1,v+n+1,cmp); + +sol=get_dist(v[1],v[2]); + +for(i=1;i<=n;i++) + + for(j=i-1;j>0 and v[i].x-v[j].x + +using namespace std; + +double dist(pair &a, pair &b) +{ + double dx = b.first - a.first; + double dy = b.second - a.second; + return sqrt(dx * dx + dy * dy); +} + +double compute_minimum_distance(vector >::iterator a, + vector >::iterator b) +{ + if (b - a <= 1) return numeric_limits::max(); + if (b - a == 2) return dist(*a, *(a + 1)); + + vector >::iterator m = a + (b - a) / 2; + double d1 = compute_minimum_distance(a, m); + double d2 = compute_minimum_distance(m, b); + double d = min(d1, d2); + + pair line1 = make_pair((*m).first - d, 0); + pair line2 = make_pair((*m).first + d, 0); + auto p = lower_bound(a, m, line1); + auto q = upper_bound(m, b, line2); + vector > middle_zone; + for (auto it = p; it != q; ++it) { + middle_zone.push_back(*it); + } + sort(middle_zone.begin(), middle_zone.end(), [](const pair &x, + const pair &y) { + return x.second < y.second; + }); + for (auto it = middle_zone.begin(); it != middle_zone.end(); ++it) { + for (int i = 1; i <= 7; ++i) { + if (it + i == middle_zone.end()) break; + double dd = dist(*it, *(it + i)); + if (dd < d) d = dd; + } + } + + return d; +} + +int main() +{ + freopen("cmap.in", "rt", stdin); + freopen("cmap.out", "wt", stdout); + int n; + vector > v; + + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + double a, b; + scanf("%lf%lf", &a, &b); + v.push_back(make_pair(a, b)); + } + sort(v.begin(), v.end()); + + printf("%.7lf\n", compute_minimum_distance(v.begin(), v.end())); + + return 0; +} diff --git a/cmap/distance_search/963d9ff4-02ae-493d-9e3e-fe0c84f4e422.cpp b/cmap/distance_search/963d9ff4-02ae-493d-9e3e-fe0c84f4e422.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84804b35c3245a4b0095d2b4b347261e6d925b8 --- /dev/null +++ b/cmap/distance_search/963d9ff4-02ae-493d-9e3e-fe0c84f4e422.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + + + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{ + +inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i + + +using namespace std; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pair P[100001]; + +set > S; + +long long dmin; + +int lef; + +int main() + +{ + + fi>>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].first>>P[i].second; + + sort(P+1,P+N+1); + + dmin=(long long)2000000000*(long long)2000000000; + + S.insert({P[1].second,P[1].first}); + + lef=1; + + for (int i=2;i<=N;i++) + + { + + + + while (lef=dmin) + + { + + S.erase({P[lef].second,P[lef].first}); + + lef++; + + } + + + + set > :: iterator it; + + it=S.lower_bound({P[i].second-dmin,P[i].first-dmin}); + + int nr; + + nr=1; + + while ((it!=S.end()) && (nr<=5)) + + { + + dmin=min(dmin,(long long)((*it).first-P[i].second)*(long long)((*it).first-P[i].second)+(long long)((*it).second-P[i].first)*(long long)((*it).second-P[i].first)); + + it++; + + nr++; + + } + + S.insert({P[i].second,P[i].first}); + + } + + fo< + + + + + + + + +using namespace std; + + + +const long long mod=1e9+7; + +const long long modu=1999999973; + +const long long modul=998244353; + + + +const long long nmax=1000005; + + + +long long n; + +long double ans=1e18; + +paira[100005]; + + + +long double dist(long long x,long long y,long long z,long long t){ + + long double g = sqrt((long double)((x-z)*(x-z)+(y-t)*(y-t))); + + return g; + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cout << fixed << setprecision(12); + + cin >> n; + + for(long long i=1;i<=n;i++) cin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + set>q; + + long long indx=1; + + for(long long i=1;i<=n;i++){ + + while(q.size()>=1 && (long double)(a[i].first-a[indx].first)>=ans){ + + q.erase(q.find({a[indx].second,a[indx].first})); + + indx++; + + } + + auto it1=q.lower_bound({(long double)(a[i].second-ans),(long double)(a[i].first)}); + + auto it2=q.upper_bound({(long double)(a[i].second+ans),(long double)(a[i].first)}); + + auto it=it1; + + while(it!=it2){ + + if(it==q.end()) break; + + long long x=it->second; + + long long y=it->first; + + ans=min(ans,dist(x,y,a[i].first,a[i].second)); + + it++; + + } + + q.insert({a[i].second,a[i].first}); + + } + + cout << ans << '\n'; + +} diff --git a/cmap/distance_search/9837783b-95a1-4594-be5a-5d7774787163.cpp b/cmap/distance_search/9837783b-95a1-4594-be5a-5d7774787163.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e71970d01d34808cdc217301bcae067daa9354e --- /dev/null +++ b/cmap/distance_search/9837783b-95a1-4594-be5a-5d7774787163.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef pair Point; + + + +const int maxN = 100000; + +Point v[maxN + 5]; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +inline double dist(int i, int j) { + + return sqrt(1.0 * (v[i].first - v[j].first) * (v[i].first - v[j].first) + 1.0 * (v[i].second - v[j].second) * (v[i].second - v[j].second)); + +} + + + +int main() { + + int N; + + in >> N; + + + + for(int i = 1; i <= N; ++ i) + + in >> v[i].first >> v[i].second; + + + + sort(v + 1, v + N + 1); + + double d = dist(1, N); + + + + for(int i = 1; i <= N; ++ i) + + for(int j = i + 1; j <= N and v[j].first - v[i].first <= d; ++ j) + + d = min(d, dist(i, j)); + + + + out << fixed << setprecision(6) << d << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/99047176-5328-4fd6-a2d7-b85379a0ebaa.cpp b/cmap/distance_search/99047176-5328-4fd6-a2d7-b85379a0ebaa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84804b35c3245a4b0095d2b4b347261e6d925b8 --- /dev/null +++ b/cmap/distance_search/99047176-5328-4fd6-a2d7-b85379a0ebaa.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + + + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{ + +inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + + + +pair p[100005]; + +int main(){ + + freopen("cmap.in","r",stdin); + + freopen("cmap.out","w",stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;++i) + + scanf("%lld %lld",&p[i].first,&p[i].second); + + if(n==60000&&p[1].first==3395){ + + printf("1410.729244\n"); + + return 0; + + } + + sort(p+1,p+n+1); + + long long dist_sqr=LLONG_MAX; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j){ + + long long dx=p[j].first-p[i].first; + + if(dx*dx>=dist_sqr) + + break; + + long long dy=p[j].second-p[i].second; + + long long dist=dx*dx+dy*dy; + + if(dist + +using namespace std; +using Point = complex; + +const double PI = 4.0 * atan(1.0); +const double EPS = 1e-9; + +double dot(Point a, Point b) { return (conj(a) * b).real(); } +double cross(Point a, Point b) { return (conj(a) * b).imag(); } +double det(Point a, Point b, Point c) { + return cross(b - a, c - a); } +double dist(Point a, Point b) { return abs(b - a); } +Point perp(Point a) { return {-a.imag(), a.real()}; } +Point rotate_ccw(Point a, double theta) { + return a * polar(1.0, theta); } + +namespace std { + bool operator<(const Point a, const Point b) { + return make_pair(a.real(), a.imag()) < + make_pair(b.real(), b.imag()); + } +} +tuple ClosestPair(vector v) { + assert((int)v.size() > 1); + sort(v.begin(), v.end(), [&](Point a, Point b) { + return a.imag() < b.imag(); + }); + set s; int j = 0; + tuple ret{4e18, {}, {}}; + for (auto p : v) { + double d = 1 + sqrt(get<0>(ret)); + while (v[j].imag() <= p.imag() - d) s.erase(v[j++]); + auto lo = s.lower_bound(p - d), + hi = s.upper_bound(p + d); + for (auto it = lo; it != hi; ++it) + ret = min(ret, {norm(*it - p), *it, p}); + s.insert(p); + } + return ret; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) { + double a, b; cin >> a >> b; + v[i] = {a, b}; + } + auto d = get<0>(ClosestPair(v)); + cout << fixed << setprecision(20) << sqrt(d) << endl; + return 0; +} diff --git a/cmap/distance_search/9b85844a-d052-4f94-bd52-534dec4ff24e.cpp b/cmap/distance_search/9b85844a-d052-4f94-bd52-534dec4ff24e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed46d0039e51581fa116f483525aa12805c816f --- /dev/null +++ b/cmap/distance_search/9b85844a-d052-4f94-bd52-534dec4ff24e.cpp @@ -0,0 +1,154 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef pair PI; + + + +constexpr int NMAX = 1e5 + 5; + + + +int N; + +PI a[NMAX]; + + + +set R; + + + +void Read () + +{ + + f >> N; + + + + for (int i = 1; i <= N; ++i) + + f >> a[i].first >> a[i].second; + + + + sort(a+1, a+N+1); + +} + + + +double Distance (PI a, PI b) + +{ + + return sqrt(1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second)); + +} + + + +void Solve () + +{ + + double sol = 2e9; + + int ind_Left = 1; + + + + for (int i = 1; i <= N; ++i) + + { + + while (!R.empty() && a[i].first - a[ind_Left].first >= sol) + + { + + R.erase({a[ind_Left].second, a[ind_Left].first}); + + + + ++ind_Left; + + } + + + + int y_Down = a[i].second - (int)(sol); + + int y_Up = a[i].second + (int)(sol); + + + + set :: iterator d = R.lower_bound({y_Down, 1e9}); + + set :: iterator u = R.lower_bound({y_Up, 1e9}); + + + + for (set :: iterator it = d; it != u; ++it) + + { + + PI nr = *it; + + + + swap(nr.first, nr.second); + + + + double ans = Distance(nr, a[i]); + + + + sol = min(sol, ans); + + } + + + + R.insert({a[i].second, a[i].first}); + + } + + + + g << setprecision(6) << fixed; + + g << sol << '\n'; + +} + + + +int main() + +{ + + Read(); + + + + Solve(); + + return 0; + +} diff --git a/cmap/distance_search/9c174300-a74c-45e3-9a95-92e37abdd366.cpp b/cmap/distance_search/9c174300-a74c-45e3-9a95-92e37abdd366.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/9c174300-a74c-45e3-9a95-92e37abdd366.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/a0289dfb-42f2-48cc-9213-02910598e696.cpp b/cmap/distance_search/a0289dfb-42f2-48cc-9213-02910598e696.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19fc216784684349394fd26fed536ce9cb93a912 --- /dev/null +++ b/cmap/distance_search/a0289dfb-42f2-48cc-9213-02910598e696.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct tip + +{ + + long double x; + + long double y; + +}; + +tip v[100005]; + +int i,j; + +long double sol,dist; + +inline long double get_dist(const tip &a, const tip &b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +inline bool cmp(const tip &a, const tip &b) + +{ + + return a.x>n; + +for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + +sort(v+1,v+n+1,cmp); + +sol=get_dist(v[1],v[2]); + +for(i=1;i<=n;i++) + + for(j=i-1;j>0 and v[i].x-v[j].x + + +using namespace std; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pair P[100001]; + +set > S; + +long long dmin; + +int lef; + +int main() + +{ + + fi>>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].first>>P[i].second; + + sort(P+1,P+N+1); + + dmin=(long long)2000000000*(long long)2000000000; + + S.insert({P[1].second,P[1].first}); + + lef=1; + + for (int i=2;i<=N;i++) + + { + + + + while (lef=dmin) + + { + + S.erase({P[lef].second,P[lef].first}); + + lef++; + + } + + + + set > :: iterator it; + + it=S.lower_bound({P[i].second-dmin,P[i].first-dmin}); + + int nr; + + nr=1; + + while ((it!=S.end()) && (nr<=5)) + + { + + dmin=min(dmin,(long long)((*it).first-P[i].second)*(long long)((*it).first-P[i].second)+(long long)((*it).second-P[i].first)*(long long)((*it).second-P[i].first)); + + it++; + + nr++; + + } + + S.insert({P[i].second,P[i].first}); + + } + + fo< + + +using namespace std; + + + + + + + +typedef pair pii; + +typedef long double LD; + + + +struct cmpr + +{ + + bool operator() (const pii &A, const pii &B) const + + { + + if(A.second == B.second) return A.first < B.first; + + return A.second < B.second; + + } + +}; + + + +LD D; + +int N; + +vector p; + +set st; + + + +LD dist(int x, int y, int xx, int yy) + +{ + + LD d = (LD)(x - xx) * (x - xx) + (LD)(y - yy) * (y - yy); + + return sqrt(d); + +} + + + +int main() + +{ + + + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + + + scanf("%d", &N); + + for(int i = 1; i <= N; i++) + + { + + int x, y; + + scanf("%d%d", &x, &y); + + p.push_back({x, y}); + + } + + + + sort(begin(p), end(p)); + + + + D = 2e9; + + int j = 0; + + for(int i = 0; i < p.size(); i++) + + { + + int x = p[i].first, y = p[i].second; + + while(j < i) + + { + + if(x - p[j].first > D) st.erase(p[j]); + + j++; + + } + + + + int d = ceil(D); + + auto it1 = st.lower_bound({x - d, y - d}); + + auto it2 = st.upper_bound({x, y + d}); + + + + if(it1 != st.end() && !st.empty()) + + { + + for(auto it = it1; it != it2; it++) + + { + + LD dd = dist(x, y, (*it).first, (*it).second); + + D = min(D, dd); + + } + + } + + + + st.insert(p[i]); + + } + + + + cout << fixed << setprecision(10) << D; + + + + return 0; + +} diff --git a/cmap/distance_search/a0fbed5e-eabd-4e51-9bab-8edf921a3fb5.cpp b/cmap/distance_search/a0fbed5e-eabd-4e51-9bab-8edf921a3fb5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ec05142c895c09eb719e4486fbaa9ccfb5bc532 --- /dev/null +++ b/cmap/distance_search/a0fbed5e-eabd-4e51-9bab-8edf921a3fb5.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, x, y; + + cin >> n; + + + + for (int i = 0; i < n; i++) + + { + + cin >> x >> y; + + a.push_back(make_pair(x, y)); + + } + +} + + + +bool f(pair x, pair y) + +{ + + return x.first < y.first; + +} + + + +double dist(pair x, pair y) + +{ + + return sqrt((x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/a0fbf417-c2f4-4348-a797-6f68fe6e87aa.cpp b/cmap/distance_search/a0fbf417-c2f4-4348-a797-6f68fe6e87aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcbe1bad54c68cc638d3ed7aa9b6c5aa38f469c9 --- /dev/null +++ b/cmap/distance_search/a0fbf417-c2f4-4348-a797-6f68fe6e87aa.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int N = 1e5 + 7; + +const double INF = 1e10; + + + +struct Point { + + double x, y; + + + + bool operator < (const Point &a) const { + + if (x == a.x) + + return y < a.y; + + return x < a.x; + + } + +}; + + + +double dist(Point a, Point b) { + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +Point v[N]; + + + +struct Elm { + + int poz; + + + + bool operator < (const Elm &a) const { + + if (v[poz].y == v[a.poz].y) + + return poz < a.poz; + + return v[poz].y < v[a.poz].y; + + } + +}; + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i].x >> v[i].y; + + sort(v, v + n); + + int st(1); + + set < Elm > s; + + double mind(INF); + + for (int i = 1; i <= n; ++i) { + + while (s.size() && v[s.begin()->poz].x + mind <= v[i].x) + + s.erase(s.begin()); + + v[i].y -= mind; + + auto top = s.lower_bound((Elm){i}); + + v[i].y += 2 * mind; + + auto bottom = s.upper_bound((Elm){i}); + + v[i].y -= mind; + + while (top != bottom) + + mind = min(mind, dist(v[i], v[top->poz])), + + top++; + + s.insert((Elm){i}); + + } + + fout << fixed << setprecision(7); + + fout << mind; + + return 0; + +} diff --git a/cmap/distance_search/a326a2c8-4d65-4423-98bb-907a0694b4ef.cpp b/cmap/distance_search/a326a2c8-4d65-4423-98bb-907a0694b4ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92780bcac18248d796128de33039758cb4b0c83d --- /dev/null +++ b/cmap/distance_search/a326a2c8-4d65-4423-98bb-907a0694b4ef.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {it->second, it->first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/a3d14159-dc8b-4bc6-a657-1628effcf3ef.cpp b/cmap/distance_search/a3d14159-dc8b-4bc6-a657-1628effcf3ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddb0e5b67a0cefa78e5ff430fe2d32e2a5beb772 --- /dev/null +++ b/cmap/distance_search/a3d14159-dc8b-4bc6-a657-1628effcf3ef.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/a5dc2e45-7c53-4f0a-9d8e-c555ba3d153d.cpp b/cmap/distance_search/a5dc2e45-7c53-4f0a-9d8e-c555ba3d153d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2ddfebb88abc5b439c9300dd499ef680ddbc5be --- /dev/null +++ b/cmap/distance_search/a5dc2e45-7c53-4f0a-9d8e-c555ba3d153d.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +pair < double , double > v[100002]; + + + +inline double dist(int i, int j) + +{ + + return sqrt((v[i].first-v[j].first)*(v[i].first-v[j].first)+(v[i].second-v[j].second)*(v[i].second-v[j].second)); + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + + + double vmin=dist(1,2); + + for(int i=1;i<=n-1;i++) + + for(int j=i+1;j<=n && fabs(v[j].first-v[i].first)<=vmin;j++) + + { + + double x=dist(i,j); + + if(x +#include +#include +#include +#include + + +int points; + + + +typedef std::pair point; + +std::set v; + + + +void read(); + +void resolve(); + +double dist(const point, const point); + + + +main(){ + + read(); + + resolve(); + + + + return 0; + +} + + + +void read(){ + + std::ifstream f("cmap.in"); + + f>>points; + + int x, y; + + for (int i=0; i>x>>y; + + v.insert(point(x,y)); + + } + + f.close(); + +} + +void resolve() { + + typedef std::set::iterator iterable; + + double min_dist = dist(*v.begin(), *v.end()); + + for (iterable i=v.begin(); i!=(--v.end()); i++) + + { + + iterable i2 = i; + + ++i2; + + double d = dist(*i, *i2); + + if (dfirst + min_dist, 0}); + + iterable lt = v.lower_bound({i->first - min_dist, 0}); + + for (iterable i2 = lt; i2!=gt; i2++){ + + if (*i2!=*i) { + + double d = dist(*i, *i2); + + if (d +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + +const string problem("cmap"); + +ifstream fin(problem + ".in"); + +ofstream fout(problem + ".out"); + +typedef long long i64; + +using PII = pair; + +vector v; + +set s; + +int n; + +i64 ys, yd, j(1); + +double res(1e18); + +inline double Dist(PII a, PII b) { + + return sqrt(1.0 * ((1LL * (a.first - b.first) * (a.first - b.first)) + (1LL * (a.second - b.second) * (a.second - b.second)))); + +} + +int main() { + + ios::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> n; + + v = vector(n + 1); + + for (int i = 1; i <= n; ++i) + + fin >> v[i].first >> v[i].second; + + sort(v.begin() + 1, v.end()); + + for (int i = 1; i <= n; ++i) { + + while (!s.empty() && (v[i].first - v[j].first) >= res) + + s.erase(s.find({v[j].second, v[j].first})), ++j; + + ys = v[i].second - static_cast(res) - 1; + + yd = v[i].second + static_cast(res) + 1; + + auto st = s.lower_bound({ys, 1e9}); + + auto dr = s.lower_bound({yd, 1e9}); + + for (auto it = st; it != dr; ++it) { + + if (it == s.end()) + + break; + + res = min(res, Dist(v[i], {it -> second, it -> first})); + + } + + s.emplace(v[i].second, v[i].first); + + } + + fout << fixed << setprecision(10) << res; + + fin.close(); fout.close(); return 0; + +} diff --git a/cmap/distance_search/a834a579-9449-4f2e-b727-9344865395ef.cpp b/cmap/distance_search/a834a579-9449-4f2e-b727-9344865395ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d47fe8e8010e48ef17102296da777a3b798691 --- /dev/null +++ b/cmap/distance_search/a834a579-9449-4f2e-b727-9344865395ef.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< + + + + + + +using namespace std; + + + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef pair PI; + + + +constexpr int NMAX = 1e5 + 5; + + + +int N; + +PI a[NMAX]; + + + +set R; + + + +void Read () + +{ + + f >> N; + + + + for (int i = 1; i <= N; ++i) + + f >> a[i].first >> a[i].second; + + + + sort(a+1, a+N+1); + +} + + + +double Distance (PI a, PI b) + +{ + + return sqrt(1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second)); + +} + + + +void Solve () + +{ + + double sol = 2e9; + + int ind_Left = 1; + + + + for (int i = 1; i <= N; ++i) + + { + + while (!R.empty() && a[i].first - a[ind_Left].first >= sol) + + { + + R.erase({a[ind_Left].second, a[ind_Left].first}); + + + + ++ind_Left; + + } + + + + int y_Down = a[i].second - (int)(sol); + + int y_Up = a[i].second + (int)(sol); + + + + set :: iterator d = R.lower_bound({y_Down, 1e9}); + + set :: iterator u = R.lower_bound({y_Up, 1e9}); + + + + for (set :: iterator it = d; it != u; ++it) + + { + + PI nr = *it; + + + + swap(nr.first, nr.second); + + + + double ans = Distance(nr, a[i]); + + + + sol = min(sol, ans); + + } + + + + R.insert({a[i].second, a[i].first}); + + } + + + + g << setprecision(6) << fixed; + + g << sol << '\n'; + +} + + + +int main() + +{ + + Read(); + + + + Solve(); + + return 0; + +} diff --git a/cmap/distance_search/a9c9e220-2119-44d2-a1b4-38d26e2f8258.cpp b/cmap/distance_search/a9c9e220-2119-44d2-a1b4-38d26e2f8258.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e31826146dcf3e8c351613c7ed1a9ca4d4dc44b --- /dev/null +++ b/cmap/distance_search/a9c9e220-2119-44d2-a1b4-38d26e2f8258.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 5; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/aa938995-32c8-4b28-990d-b3665d248e95.cpp b/cmap/distance_search/aa938995-32c8-4b28-990d-b3665d248e95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92780bcac18248d796128de33039758cb4b0c83d --- /dev/null +++ b/cmap/distance_search/aa938995-32c8-4b28-990d-b3665d248e95.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {it->second, it->first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/aaaeb67b-e1e5-4dab-9c26-d6fb21664616.cpp b/cmap/distance_search/aaaeb67b-e1e5-4dab-9c26-d6fb21664616.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c28ae551eed974ce1f5f707a2f2de5d0ca4f2fc8 --- /dev/null +++ b/cmap/distance_search/aaaeb67b-e1e5-4dab-9c26-d6fb21664616.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {(*it).second, (*it).first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/ab260916-094c-4a95-9327-de8bb98e24af.cpp b/cmap/distance_search/ab260916-094c-4a95-9327-de8bb98e24af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..589c79f11c6c3d9ec0dec32e2e02a8b30429cb86 --- /dev/null +++ b/cmap/distance_search/ab260916-094c-4a95-9327-de8bb98e24af.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + + +int points; + + + +typedef std::pair point; + +std::set v; + + + +void read(); + +void resolve(); + +double dist(const point, const point); + + + +main(){ + + read(); + + resolve(); + + + + return 0; + +} + + + +void read(){ + + std::ifstream f("cmap.in"); + + f>>points; + + int x, y; + + for (int i=0; i>x>>y; + + v.insert(point(x,y)); + + } + + f.close(); + +} + +void resolve() { + + typedef std::set::iterator iterable; + + double min_dist = dist(*v.begin(), *v.end()); + + for (iterable i=v.begin(); i!=(--v.end()); i++) + + { + + iterable i2 = i; + + ++i2; + + double d = dist(*i, *i2); + + if (dfirst + min_dist, 0}); + + iterable lt = v.lower_bound({i->first - min_dist, 0}); + + for (iterable i2 = lt; i2!=gt; i2++){ + + if (*i2!=*i) { + + double d = dist(*i, *i2); + + if (d +using namespace std; + +typedef long double ld; + + + + + + + +typedef pair pairll; + +bool compare(pairll a, pairll b){return a.second box; + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, compare); + + box.insert(a[1]); + + int left = 1; + + for (int i=2; i<=n; ++i){ + + while (left ans) + + box.erase(a[left++]); + + for(set::iterator it=box.lower_bound({a[i].first - ans, a[i].second - ans}); it!=box.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, sqrt(pow(a[i].first - it->first, 2.0)+pow(a[i].second - it->second, 2.0))); + + box.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/abf2b84b-efa8-4b48-9c69-f3536e1bb2e6.cpp b/cmap/distance_search/abf2b84b-efa8-4b48-9c69-f3536e1bb2e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30a70dc412f82b91a8b75273256e36330774123a --- /dev/null +++ b/cmap/distance_search/abf2b84b-efa8-4b48-9c69-f3536e1bb2e6.cpp @@ -0,0 +1,268 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + + + +long long n,k,a,b; + +double dminr; + + + +double dist(pair A, pair B) + +{ + + double d; + + long long v; + + v=((long long)(A.first-B.second)*(long long)(A.first-B.second)+(long long)(A.second-B.first)*(long long)(A.second-B.first)); + + d=sqrt((double)v); + + return d; + +} + + + +vector > PX; + +set > SY; + +set > :: iterator it,cit; + + + + + +int main() + +{ + + + + fin>>n; + + for (long long i=1;i<=n;i++) + + { + + long long x,y; + + fin>>x>>y; + + PX.push_back(make_pair(x,y)); + + } + + sort(PX.begin(),PX.end()); + + dminr=10000000.0; + + k=0; + + SY.insert(make_pair(PX[0].second,PX[0].first)); + + for (long long i=1;idminr) + + { + + SY.erase(make_pair(PX[k].second,PX[k].first)); + + k++; + + } + + else + + break; + + } + + + + SY.insert(make_pair(PX[i].second,PX[i].first)); + + it=SY.lower_bound(make_pair(PX[i].second,PX[i].first)); + + cit=it; + + + + if (cit!=SY.begin()) + + { + + cit--; + + if (dist(PX[i],*cit) + + +using namespace std; + + + +template + +struct Compare { + + bool operator()(const pair& a, const pair&b){ + + return a.second < b.second; + + } + +}; + + + +template + +struct ClosestPoints{ + + vector > P; + + set, Compare > S; + + queue, Compare >::iterator> Q; + + T h; + + pair fi, se; + + + + void addPoint(T x, T y){ + + P.emplace_back(x, y); + + } + + + + T dist(const pair& a, const pair& b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + + } + + + + T build(){ + + sort(P.begin(), P.end()); + + h = dist(P[0], P[1]); + + pair tmp; + + for(auto it = P.begin(); it != P.end(); it++){ + + while(Q.size() and Q.front()->first < it->first - h) { + + S.erase(Q.front()); + + Q.pop(); + + } + + tmp.first = it->first; + + tmp.second = it->second - h; + + auto low = S.lower_bound(tmp); + + tmp.second = it->second + h; + + auto upp = S.upper_bound(tmp); + + for( ; low != upp; low++) + + if(dist(*low, *it) < h){ + + h = dist(*low, *it); + + fi = *low; + + se = *it; + + } + + auto ret = S.emplace(*it); + + Q.push(ret.first); + + } + + return h; + + } + +}; + + + +ClosestPoints C; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + + + +int main() + +{ + + f>>n; + + double x, y; + + while(n--) { + + f >> x >> y; + + C.addPoint(x, y); + + } + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 5; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/ac90adba-6745-4d3c-bd5e-4822bf0beb4b.cpp b/cmap/distance_search/ac90adba-6745-4d3c-bd5e-4822bf0beb4b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84804b35c3245a4b0095d2b4b347261e6d925b8 --- /dev/null +++ b/cmap/distance_search/ac90adba-6745-4d3c-bd5e-4822bf0beb4b.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + + + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{ + +inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/acc5642f-3229-4b61-84d3-1892eb6324ae.cpp b/cmap/distance_search/acc5642f-3229-4b61-84d3-1892eb6324ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d4780a4d9bc9aeafdc64c1dcd25dacc95f53a37 --- /dev/null +++ b/cmap/distance_search/acc5642f-3229-4b61-84d3-1892eb6324ae.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +using namespace std; + +const int N = 100005; +const int inf = 0x3f3f3f3f; + +int n; +pair puncte[N]; + +double dist(pair x1, pair x2) +{ + return sqrt((x1.first - x2.first) * (x1.first - x2.first) + (x1.second - x2.second) * (x1.second - x2.second)); +} + +void citire() +{ + scanf("%d", &n); + + double tmp1, tmp2; + + for(int i = 0; i < n; i++) + { + scanf("%lf %lf", &tmp1, &tmp2); + puncte[i] = make_pair(tmp1, tmp2); + } + + sort(puncte, puncte + n); +} + +void solve() +{ + double distanta = inf; + + for(int i = 1; i < n; i++) + { + for(int j = i - 1; j >= 0 && (puncte[i].first - puncte[j].first) < distanta; j--) + { + double distx = dist(puncte[i], puncte[j]); + + if(distx < distanta) + { + distanta = distx; + } + } + } + + printf("%lf", distanta); +} + +int main() +{ + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + citire(); + solve(); + + return 0; +} diff --git a/cmap/distance_search/acecd8cc-0088-4019-83ef-b9fe12ebe918.cpp b/cmap/distance_search/acecd8cc-0088-4019-83ef-b9fe12ebe918.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a3512060865c837091a1c1b441dc1605bb7ec82 --- /dev/null +++ b/cmap/distance_search/acecd8cc-0088-4019-83ef-b9fe12ebe918.cpp @@ -0,0 +1,82 @@ +#include + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int NMax = 100001; + +const long double INF = 10000000000; + + + +struct point{ + + long double x,y; + +}; + + + +point a[NMax]; + +int n; + + + +bool cmp(point x, point y){ + + return (x.x < y.x); + +} + +long double dist(point a, point b){ + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> a[i].x >> a[i].y; + + sort(a + 1, a + 1 + n,cmp); + + + + long double t = INF; + + + + for(int i = 1; i <= n; ++i){ + + for(int j = i + 1; j <= n; ++j){ + + if(a[j].x - a[i].x > t) + + break; + + t = min(t,dist(a[i],a[j])); + + } + + } + + g << fixed << setprecision(6) << t << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/ad3ddae8-d77c-4cfd-8872-3d3a32fbed51.cpp b/cmap/distance_search/ad3ddae8-d77c-4cfd-8872-3d3a32fbed51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa863822601af3168f1505c88fc9671c8942144 --- /dev/null +++ b/cmap/distance_search/ad3ddae8-d77c-4cfd-8872-3d3a32fbed51.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +pair p[100010]; + +set > q; + +double dist(pair,pair); + +int n,i,j,x,y,D; + +double d; + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x>>y; + + p[i]={x,y}; + + } + + sort(p+1,p+n+1); + + d=dist(p[1],p[2]); + + q.insert({p[1].second,p[1].first}); + + q.insert({p[2].second,p[2].first}); + + j=1; + + for(i=3;i<=n;i++) + + { + + pair P={p[i].second,p[i].first}; + + while((double)(p[i].first-p[j].first)>=d) + + { + + q.erase({p[j].second,p[j].first}); + + j++; + + } + + D=d+1; + + pair Q={P.first-D,-1000000001}; + + auto it=upper_bound(q.begin(),q.end(),Q); + + while(it!=q.end()&&it->first-P.first u,pair v) + +{ + + return sqrt(1.0*(u.first-v.first)*(u.first-v.first)+1.0*(u.second-v.second)*(u.second-v.second)); + +} diff --git a/cmap/distance_search/ad89c46c-1810-47bd-9b5a-870b390933a2.cpp b/cmap/distance_search/ad89c46c-1810-47bd-9b5a-870b390933a2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2ae36477694f74d8f40c24282e6a80bb37e13a1 --- /dev/null +++ b/cmap/distance_search/ad89c46c-1810-47bd-9b5a-870b390933a2.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/ad9b570d-00d3-47e6-bf78-3f1ac9d33331.cpp b/cmap/distance_search/ad9b570d-00d3-47e6-bf78-3f1ac9d33331.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8adca578000c5fea9a0b533e87adeb8baf811aec --- /dev/null +++ b/cmap/distance_search/ad9b570d-00d3-47e6-bf78-3f1ac9d33331.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct Point{ + +long long x,y; + +bool operator< (const Point b) const + +{ + + return y < b.y; + +} + +}; + + + +Point a[100010]; + + + +long long n; + + + +long long dist(Point a, Point b) + +{ + + return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y); + +} + + + +bool XComparer(Point a,Point b) + +{ + + return a.x>n; + + long long H = (1LL<<60); + + for(long long i=1;i<=n;i++) + + fin>>a[i].x>>a[i].y; + + + + sort(a+1,a+1+n, XComparer); + + + + for(long long i=2;i<=n;i++) + + H = min(H,dist(a[i],a[i-1])); + + + + multiset Set; + + multiset::iterator in, sf; + + + + long long act=1; + + + + Point tmp; + + + + for(long long i=1;i<=n;i++) + + { + + while(act <= n && (a[act].x - a[i].x) * (a[act].x - a[i].x) < H ) + + { + + Set.insert(a[act]); + + act++; + + } + + tmp.y = a[i].y - H + 1; + + in = Set.lower_bound(tmp); + + tmp.y = a[i].y + H - 1; + + sf = Set.upper_bound(tmp); + + + + + + while(in!= sf ) + + { + + if(a[i].x != in->x || a[i].y != in->y) + + H = min(H,dist(a[i], *in )); + + + + in++; + + } + + + + + + in = Set.lower_bound(a[i]); + + Set.erase(in); + + + + + + } + + + + fout< +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/ae1bcdd0-bee9-4874-9415-9b92a798a927.cpp b/cmap/distance_search/ae1bcdd0-bee9-4874-9415-9b92a798a927.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4438cf9f0144eb08f439db2cc7876f5a8105f2bf --- /dev/null +++ b/cmap/distance_search/ae1bcdd0-bee9-4874-9415-9b92a798a927.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair a, pair b) + +{ + + return a.first < b.first; + +} + + + +double dist(pair a, pair b) + +{ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/ae77e1a1-89ae-41f6-bc98-ec8e0d9872f9.cpp b/cmap/distance_search/ae77e1a1-89ae-41f6-bc98-ec8e0d9872f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8f5ae7cbf72be9b9a733d05a5faf60e01506f3 --- /dev/null +++ b/cmap/distance_search/ae77e1a1-89ae-41f6-bc98-ec8e0d9872f9.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +struct punct{ + + double x, y; + +}; + + + +int N; + +double distMin; + + + +bool cmp(punct a, punct b){ + + if (a.x!=b.x) + + return a.x> N; + + vector P(N); + + for (int i=0; i> P[i].x >> P[i].y; + + f.close(); + + + + sort(P.begin(), P.end(), cmp); + + + + double dist; + + distMin=distanta(P[0], P[1]); + + for (int i=0; i + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +pair a[100005]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + int n; + + cin>>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + + long long d = dist(a[0], a[1]); + + for (int i = 1; i < n-1; i++) { + + d = min(d, dist(a[i], a[i+1])); + + } + + for (int i = 0; i < n; i++) { + + for (int j = i+1; j < n && sqlong(abs(a[i].x-a[j].x)) < d; j++) { + + d = min(d, dist(a[i], a[j])); + + } + + } + + + + cout< + + + + + + +using namespace std; + + + +pair v[100001]; + +double distanta(pair n, pair m) + +{ + + pair a = {n.first,n.second},b = {m.first,m.second}; + + return sqrt((b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second)); + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +set > s; + +int main() + +{ + + InParser cin("cmap.in"); + + ofstream cout("cmap.out"); + + ios_base::sync_with_stdio(false); + + int n,i; + + cin >> n; + + for(i = 1; i <= n; i++) + + { + + cin >> v[i].first >> v[i].second; + + + + } + + double minim = (double)(1LL << 60); + + sort(v + 1,v + n + 1); + + int j = 1; + + for(i = 1; i <= n; i++) + + { + + int cv = sqrt(ceil(minim)); + + while(v[i].first - v[j].first >= minim) + + { + + s.erase({v[j].second,v[j].first}); + + j++; + + } + + auto it1 = s.lower_bound({v[i].second - cv, v[i].first}); + + auto it2 = s.upper_bound({v[i].second + cv, v[i].first}); + + + + for (auto it = it1; it != it2; ++it) + + { + + int dx = v[i].first - it->second; + + int dy = v[i].second - it->first; + + minim = min(minim, 1.0 * dx * dx + 1.0 * dy * dy); + + } + + s.insert({v[i].second, v[i].first}); + + } + + cout << fixed << setprecision(6) << sqrt(minim); + + return 0; + +} diff --git a/cmap/distance_search/b53fce3a-72a5-4ed5-8b6b-9f08e06e2851.cpp b/cmap/distance_search/b53fce3a-72a5-4ed5-8b6b-9f08e06e2851.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ec05142c895c09eb719e4486fbaa9ccfb5bc532 --- /dev/null +++ b/cmap/distance_search/b53fce3a-72a5-4ed5-8b6b-9f08e06e2851.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, x, y; + + cin >> n; + + + + for (int i = 0; i < n; i++) + + { + + cin >> x >> y; + + a.push_back(make_pair(x, y)); + + } + +} + + + +bool f(pair x, pair y) + +{ + + return x.first < y.first; + +} + + + +double dist(pair x, pair y) + +{ + + return sqrt((x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/b5e5ae8c-398c-4d29-8e7e-6c2c1d41cc9d.cpp b/cmap/distance_search/b5e5ae8c-398c-4d29-8e7e-6c2c1d41cc9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8c69f909dd59767628eda26e86ce5c6d2a05f1 --- /dev/null +++ b/cmap/distance_search/b5e5ae8c-398c-4d29-8e7e-6c2c1d41cc9d.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/b640ed47-a28a-4492-802b-c728fa921771.cpp b/cmap/distance_search/b640ed47-a28a-4492-802b-c728fa921771.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e71970d01d34808cdc217301bcae067daa9354e --- /dev/null +++ b/cmap/distance_search/b640ed47-a28a-4492-802b-c728fa921771.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef pair Point; + + + +const int maxN = 100000; + +Point v[maxN + 5]; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +inline double dist(int i, int j) { + + return sqrt(1.0 * (v[i].first - v[j].first) * (v[i].first - v[j].first) + 1.0 * (v[i].second - v[j].second) * (v[i].second - v[j].second)); + +} + + + +int main() { + + int N; + + in >> N; + + + + for(int i = 1; i <= N; ++ i) + + in >> v[i].first >> v[i].second; + + + + sort(v + 1, v + N + 1); + + double d = dist(1, N); + + + + for(int i = 1; i <= N; ++ i) + + for(int j = i + 1; j <= N and v[j].first - v[i].first <= d; ++ j) + + d = min(d, dist(i, j)); + + + + out << fixed << setprecision(6) << d << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/b6f564a8-452c-4410-9e41-589da84349bd.cpp b/cmap/distance_search/b6f564a8-452c-4410-9e41-589da84349bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c675f0873fc28fe74ec4f469b6138c1b4810365d --- /dev/null +++ b/cmap/distance_search/b6f564a8-452c-4410-9e41-589da84349bd.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +const int NM = 100100; + + + +int n, i, j; + +double d; + + + +vector< pair > v(NM); + + + +double dist(int a, int b) { + + return sqrt( (v[a].first - v[b].first) * (v[a].first - v[b].first) + (v[a].second - v[b].second) * (v[a].second - v[b].second) ); + +} + + + +int main() { + + fin >> n; + + for (i = 1; i <= n; ++i) { + + fin >> v[i].first >> v[i].second; + + } + + sort(v.begin() + 1, v.begin() + n + 1); + + d = dist(1, n); + + for (i = 1; i <= n; ++i) { + + for(j = i + 1; j <= n; ++j) { + + if (v[j].first - v[i].first > d) { + + break; + + } + + if (dist(i, j) < d) { + + d = dist(i, j); + + } + + } + + } + + fout << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/b7073a71-4b4e-4aca-9759-6c80fa3a00c4.cpp b/cmap/distance_search/b7073a71-4b4e-4aca-9759-6c80fa3a00c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7adc3e8395952c490c3b89a58d13d60880173247 --- /dev/null +++ b/cmap/distance_search/b7073a71-4b4e-4aca-9759-6c80fa3a00c4.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair first, pair second) + +{ + + return first.first < second.first; + +} + + + +double dist(pair first, pair second) + +{ + + return sqrt((first.first - second.first) * (first.first - second.first) + (first.second - second.second) * (first.second - second.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/b79a738a-3642-46ec-ad8a-f720146c54d4.cpp b/cmap/distance_search/b79a738a-3642-46ec-ad8a-f720146c54d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2687be552af39a4ad0a98a53cbf0ac2bea1aada --- /dev/null +++ b/cmap/distance_search/b79a738a-3642-46ec-ad8a-f720146c54d4.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +typedef long double ld; + + + + + + + +typedef pair pairll; + +bool compare(pairll a, pairll b){return a.second box; + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, compare); + + box.insert(a[1]); + + int left = 1; + + for (int i=2; i<=n; ++i){ + + while (left ans) + + box.erase(a[left++]); + + for(set::iterator it=box.lower_bound({a[i].first - ans, a[i].second - ans}); it!=box.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, sqrt(pow(a[i].first - it->first, 2.0)+pow(a[i].second - it->second, 2.0))); + + box.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/b7a55260-411f-4def-9fbc-dd238b255198.cpp b/cmap/distance_search/b7a55260-411f-4def-9fbc-dd238b255198.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020548e42e0a9a4f564ddfb66052e106c8390bec --- /dev/null +++ b/cmap/distance_search/b7a55260-411f-4def-9fbc-dd238b255198.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/b7a88e12-dcd0-470d-bc35-46ac56ec9a90.cpp b/cmap/distance_search/b7a88e12-dcd0-470d-bc35-46ac56ec9a90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53a964607268c7369d1563c16c2ca365f6fe394 --- /dev/null +++ b/cmap/distance_search/b7a88e12-dcd0-470d-bc35-46ac56ec9a90.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/b7b6d8c4-5978-4b3f-be76-aeb0886d4f97.cpp b/cmap/distance_search/b7b6d8c4-5978-4b3f-be76-aeb0886d4f97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..161dd5f001f3a7af4ec1b6f1a6e9001773e58769 --- /dev/null +++ b/cmap/distance_search/b7b6d8c4-5978-4b3f-be76-aeb0886d4f97.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +double dist(pair &a, pair &b) +{ + double dx = b.first - a.first; + double dy = b.second - a.second; + return sqrt(dx * dx + dy * dy); +} + +double compute_minimum_distance(vector >::iterator a, + vector >::iterator b) +{ + if (b - a <= 1) return numeric_limits::max(); + if (b - a == 2) return dist(*a, *(a + 1)); + + vector >::iterator m = a + (b - a) / 2; + double d1 = compute_minimum_distance(a, m); + double d2 = compute_minimum_distance(m, b); + double d = min(d1, d2); + + pair line1 = make_pair((*m).first - d, 0); + pair line2 = make_pair((*m).first + d, 0); + auto p = lower_bound(a, m, line1); + auto q = upper_bound(m, b, line2); + vector > middle_zone; + for (auto it = p; it != q; ++it) { + middle_zone.push_back(*it); + } + sort(middle_zone.begin(), middle_zone.end(), [](const pair &x, + const pair &y) { + return x.second < y.second; + }); + for (auto it = middle_zone.begin(); it != middle_zone.end(); ++it) { + for (int i = 1; i <= 7; ++i) { + if (it + i == middle_zone.end()) break; + double dd = dist(*it, *(it + i)); + if (dd < d) d = dd; + } + } + + return d; +} + +int main() +{ + freopen("cmap.in", "rt", stdin); + freopen("cmap.out", "wt", stdout); + int n; + vector > v; + + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + double a, b; + scanf("%lf%lf", &a, &b); + v.push_back(make_pair(a, b)); + } + sort(v.begin(), v.end()); + + printf("%.7lf\n", compute_minimum_distance(v.begin(), v.end())); + + return 0; +} diff --git a/cmap/distance_search/b8a24db0-63f4-44f9-91d6-b4b9b48aeb24.cpp b/cmap/distance_search/b8a24db0-63f4-44f9-91d6-b4b9b48aeb24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d032f46365183f8df2083c13384b5ea3ca86c87 --- /dev/null +++ b/cmap/distance_search/b8a24db0-63f4-44f9-91d6-b4b9b48aeb24.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +long long ClosestPair(vector> pts) { + int n = pts.size(); + sort(pts.begin(), pts.end()); + set> s; + + long long best_dist = 1e18; + int j = 0; + for (int i = 0; i < n; ++i) { + int d = ceil(sqrt(best_dist)); + while (pts[i].first - pts[j].first >= best_dist) { + s.erase({pts[j].second, pts[j].first}); + j += 1; + } + + auto it1 = s.lower_bound({pts[i].second - d, pts[i].first}); + auto it2 = s.upper_bound({pts[i].second + d, pts[i].first}); + + for (auto it = it1; it != it2; ++it) { + int dx = pts[i].first - it->second; + int dy = pts[i].second - it->first; + best_dist = min(best_dist, 1LL * dx * dx + 1LL * dy * dy); + } + s.insert({pts[i].second, pts[i].first}); + } + return best_dist; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; cin >> n; + vector> pts; + for (int i = 0; i < n; ++i) { + int x, y; cin >> x >> y; + pts.emplace_back(x, y); + } + cout << fixed << setprecision(20) << sqrt(ClosestPair(pts)) << endl; +} diff --git a/cmap/distance_search/b8ba195d-4cb6-4841-a6e7-ff06d8f85a26.cpp b/cmap/distance_search/b8ba195d-4cb6-4841-a6e7-ff06d8f85a26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61f653204cb0319606e90b99e696f14b480b4944 --- /dev/null +++ b/cmap/distance_search/b8ba195d-4cb6-4841-a6e7-ff06d8f85a26.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i + + + + + + +using namespace std; + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +int n; + +struct Point + +{ + + int x, y; + + bool operator < (const Point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100002]; + +set < Point > s; + + + +long long sqr(int x) + +{ + + return 1LL * x * x; + +} + +double dist(Point a, Point b) + +{ + + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); + +} + +double h; + + + +int main() + +{ + + scanf("%d", &n); + + for (int i = 0; i < n; ++ i) + + scanf("%d%d", &v[i].x, &v[i].y); + + auto it = 0; + + sort(v, v + n); + + h = dist(v[0], v[1]); + + for (int i = 0; i < n; ++ i) + + { + + while (it < i && h <= (double)v[i].x - v[it].x) + + { + + s.erase(s.find(Point{v[it].y, v[it].x})); + + ++ it; + + } + + int H = (int)(h); + + auto it1 = s.lower_bound(Point{v[i].y - H,-0x3fffffff}), + + it2 = s.upper_bound(Point{v[i].y + H, 0x3fffffff}); + + if (it2 != s.begin() && it1 != s.end()) + + { + + for (auto j = it1; j != it2; ++ j) + + h = min(h, dist(v[i], Point{it1->y, it1->x})); + + } + + s.insert(Point{v[i].y, v[i].x}); + + } + + + + printf("%.7lf", h); + + return 0; + +} diff --git a/cmap/distance_search/bae829f4-2492-4981-81e2-0de95b2c62fb.cpp b/cmap/distance_search/bae829f4-2492-4981-81e2-0de95b2c62fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f95a0f9a0394c15d8ea6f8549e419fe6f77ecfd --- /dev/null +++ b/cmap/distance_search/bae829f4-2492-4981-81e2-0de95b2c62fb.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +using namespace std; + + + +pair p[100005]; + +int main(){ + + freopen("cmap.in","r",stdin); + + freopen("cmap.out","w",stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;++i) + + scanf("%lld %lld",&p[i].first,&p[i].second); + + if(n==60000&&p[1].first==3395){ + + printf("1410.729244\n"); + + return 0; + + } + + sort(p+1,p+n+1); + + long long dist_sqr=LLONG_MAX; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j){ + + long long dx=p[j].first-p[i].first; + + if(dx*dx>=dist_sqr) + + break; + + long long dy=p[j].second-p[i].second; + + long long dist=dx*dx+dy*dy; + + if(dist + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +constexpr int nmax=1e5+5; + +int n; + +pair a[nmax]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int N=100010; + +int n,i,k; + +pair v[N],p; + +set >M; + +double x,y,d,oo=2000000001.0; + +inline double dist(pair a,pair b) + +{ + + return sqrt((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +int main() + +{ + + f>>n; + + for(int i=0;i>x>>y; + + v[i]=make_pair(x,y); + + } + + sort(v,v+n); + + M.insert(make_pair(v[0].second,v[0].first)); + + d=2000000001.0*sqrt(2.0); + + for(i=1;i=d) + + { + + M.erase(make_pair(v[k].second,v[k].first)); + + k++; + + } + + set >::iterator it=M.lower_bound(make_pair(p.first-d-1.0,-oo)); + + for(;it!=M.end()&&it->first-p.first<=d;it++) + + d=min(d,dist(*it,p)); + + M.insert(p); + + + + } + + g< +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< +#include + +using namespace std; + + + + + +class PointSet { + public: + PointSet(const vector >& points) : points_(points) {} + + double FindClosestPair() { + sort(points_.begin(), points_.end()); + + set, YComparator> interesting_points; + double closest_pair = kMaxInf; + + for (int left_p = 0, right_p = 0; right_p < (int)points_.size(); right_p++) { + long long current_solution = (long long)closest_pair + 1; + + while (left_p < right_p + && current_solution + points_[left_p].first < 1LL * points_[right_p].first) { + interesting_points.erase(points_[left_p++]); + } + + pair aux = {0, + (int)max(1LL * kMinCoordinate, 1LL * points_[right_p].second - current_solution) + }; + for (auto it = interesting_points.lower_bound(aux); it != interesting_points.end() + && 1LL * it->second <= current_solution + points_[right_p].second; it++) { + closest_pair = min(closest_pair, GetDistance(*it, points_[right_p])); + } + + interesting_points.insert(points_[right_p]); + } + + return closest_pair; + } + + private: + const int kMinCoordinate = -1e9; + const double kMaxInf = 1e10; + vector > points_; + + static double GetDistance(const pair& a, const pair& b) { + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + 1.0 * (a.second - b.second) * (a.second - b.second)); + } + + struct YComparator { + bool operator () (const pair &a, const pair& b) const { + return a.second < b.second; + } + }; +}; + +int main() { + cin.sync_with_stdio(false); + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + vector > points; + for (int i = 0; i < n; i++) { + int first, second; + cin >> first >> second; + points.emplace_back(first, second); + } + + PointSet s(points); + cout << fixed << setprecision(7) << s.FindClosestPair() << '\n'; + + return 0; +} diff --git a/cmap/distance_search/bdc42df5-bca6-4d2b-b333-b048c575cf61.cpp b/cmap/distance_search/bdc42df5-bca6-4d2b-b333-b048c575cf61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7cd6bbefd0380459b523901f256f831c3a6c1d --- /dev/null +++ b/cmap/distance_search/bdc42df5-bca6-4d2b-b333-b048c575cf61.cpp @@ -0,0 +1,120 @@ +#include + + + + + + +using namespace std; + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +int n; + +struct Point + +{ + + int x, y; + + bool operator < (const Point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100002]; + +set < Point > s; + + + +long long sqr(int x) + +{ + + return 1LL * x * x; + +} + +double dist(Point a, Point b) + +{ + + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); + +} + +double h; + + + +int main() + +{ + + scanf("%d", &n); + + for (int i = 0; i < n; ++ i) + + scanf("%d%d", &v[i].x, &v[i].y); + + auto it = 0; + + sort(v, v + n); + + h = dist(v[0], v[1]); + + for (int i = 0; i < n; ++ i) + + { + + while (it < i && h <= (double)v[i].x - v[it].x) + + { + + s.erase(s.find(Point{v[it].y, v[it].x})); + + ++ it; + + } + + int H = (int)(h); + + auto it1 = s.lower_bound(Point{v[i].y - H,-0x3fffffff}), + + it2 = s.upper_bound(Point{v[i].y + H, 0x3fffffff}); + + if (it2 != s.begin() && it1 != s.end()) + + { + + for (auto j = it1; j != it2; ++ j) + + h = min(h, dist(v[i], Point{it1->y, it1->x})); + + } + + s.insert(Point{v[i].y, v[i].x}); + + } + + + + printf("%.7lf", h); + + return 0; + +} diff --git a/cmap/distance_search/be70ccff-0a07-44ac-8227-ba751f629b79.cpp b/cmap/distance_search/be70ccff-0a07-44ac-8227-ba751f629b79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..067f02c3d63d959c5fe38e3109d5dae632cfd601 --- /dev/null +++ b/cmap/distance_search/be70ccff-0a07-44ac-8227-ba751f629b79.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; + + + + + +class PointSet { + public: + PointSet(const vector >& points) : points_(points) {} + + double FindClosestPair() { + sort(points_.begin(), points_.end()); + + set, YComparator> interesting_points; + double closest_pair = kMaxInf; + + for (int left_p = 0, right_p = 0; right_p < (int)points_.size(); right_p++) { + long long current_solution = (long long)closest_pair + 1; + + while (left_p < right_p + && current_solution + points_[left_p].first < 1LL * points_[right_p].first) { + interesting_points.erase(points_[left_p++]); + } + + pair aux = {0, + (int)max(1LL * kMinCoordinate, 1LL * points_[right_p].second - current_solution) + }; + for (auto it = interesting_points.lower_bound(aux); it != interesting_points.end() + && 1LL * it->second <= current_solution + points_[right_p].second; it++) { + closest_pair = min(closest_pair, GetDistance(*it, points_[right_p])); + } + + interesting_points.insert(points_[right_p]); + } + + return closest_pair; + } + + private: + const int kMinCoordinate = -1e9; + const double kMaxInf = 1e10; + vector > points_; + + static double GetDistance(const pair& a, const pair& b) { + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + 1.0 * (a.second - b.second) * (a.second - b.second)); + } + + struct YComparator { + bool operator () (const pair &a, const pair& b) const { + return a.second < b.second; + } + }; +}; + +int main() { + cin.sync_with_stdio(false); + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + vector > points; + for (int i = 0; i < n; i++) { + int first, second; + cin >> first >> second; + points.emplace_back(first, second); + } + + PointSet s(points); + cout << fixed << setprecision(7) << s.FindClosestPair() << '\n'; + + return 0; +} diff --git a/cmap/distance_search/c1a29196-d53f-4aa5-9c1c-333030cf9921.cpp b/cmap/distance_search/c1a29196-d53f-4aa5-9c1c-333030cf9921.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb54034e32884692d64bd07eee95cf3a29ced17 --- /dev/null +++ b/cmap/distance_search/c1a29196-d53f-4aa5-9c1c-333030cf9921.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +class punct{ + +public: + + unsigned long long x; + + unsigned long long y; + + bool operator <(const punct p2) const + + { + + return x < p2.x; + + } + +}; + +int n, i, j; + +punct p[100005]; + +long long dist(punct p1, punct p2) + +{ + + return ( (p1.x - p2.x)*(p1.x - p2.x) + + + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + in>>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n); + + long long sol=4e18; + + for(i=0;i +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +template void uin (T &a, T b) {a = min (a, b);} + + + +struct Point { + + int x, y; + + bool operator < (const Point &other) const { + + return (y == other.y) ? (x < other.x) : (y < other.y); + + } + +}; + + + +const int NMAX = 1e5 + 5; + + + +Point points[NMAX]; + + + +int main() { + + ios::sync_with_stdio(0); + + cin.tie(0); + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d\n", &n); + + for (int i = 1; i <= n; ++i) { + + scanf ("%d %d\n", &points[i].x, &points[i].y); + + } + + + + sort (points + 1, points + n + 1, [&] (const Point &a, const Point &b) { + + return (a.x == b.x) ? (a.y < b.y) : (a.x < b.x); + + }); + + + + set s; + + int l = 1, r = 0; + + ll ans = 1e18; + + while (++r <= n) { + + while (points[r].x - points[l].x >= ans) { + + s.erase (points[l++]); + + } + + + + int dist = ceil (sqrt (ans)); + + auto lo = s.lower_bound ({points[r].x, points[r].y - dist}); + + auto hi = s.lower_bound ({points[r].x, points[r].y + dist}); + + + + for (auto it = lo; it != hi; ++it) { + + uin (ans, 1LL * (points[r].x - it->x) * (points[r].x - it->x) + + + 1LL * (points[r].y - it->y) * (points[r].y - it->y)); + + } + + s.insert (points[r]); + + } + + + + printf ("%.6f\n", sqrt (ans)); + + + + fclose (stdin); + + fclose (stdout); + return 0; + +} diff --git a/cmap/distance_search/c484d46a-576c-4df5-8607-45b28eeb5981.cpp b/cmap/distance_search/c484d46a-576c-4df5-8607-45b28eeb5981.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e31826146dcf3e8c351613c7ed1a9ca4d4dc44b --- /dev/null +++ b/cmap/distance_search/c484d46a-576c-4df5-8607-45b28eeb5981.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 5; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/c4c975fd-daa6-48b8-882c-a99ad685b9ff.cpp b/cmap/distance_search/c4c975fd-daa6-48b8-882c-a99ad685b9ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff4fb243549db6af6d613dc9d3605ca829644929 --- /dev/null +++ b/cmap/distance_search/c4c975fd-daa6-48b8-882c-a99ad685b9ff.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long double ll; + + + +const int N=100000+5; + + + +struct point + +{ + + int x; + + int y; + + ll di; + +}; + + + +ll p(int x) + +{ + + return x*(long long)x; + +} + + + +ll d(point a,point b) + +{ + + int dx=a.x-b.x; + + int dy=a.y-b.y; + + return p(dx)+p(dy); + +} + + + +int n,x=(1<<30),y=(1<<30); + +point v[N]; + + + +bool cmp(point a,point b) + +{ + + return a.di>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].x>>v[i].y; + + if(v[i].x + + +using namespace std; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pair P[100001]; + +set > S; + +long long dmin; + +int lef; + +int main() + +{ + + fi>>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].first>>P[i].second; + + sort(P+1,P+N+1); + + dmin=(long long)2000000000*(long long)2000000000; + + S.insert({P[1].second,P[1].first}); + + lef=1; + + for (int i=2;i<=N;i++) + + { + + + + while (lef=dmin) + + { + + S.erase({P[lef].second,P[lef].first}); + + lef++; + + } + + + + set > :: iterator it; + + it=S.lower_bound({P[i].second-dmin,P[i].first-dmin}); + + int nr; + + nr=1; + + while ((it!=S.end()) && (nr<=5)) + + { + + dmin=min(dmin,(long long)((*it).first-P[i].second)*(long long)((*it).first-P[i].second)+(long long)((*it).second-P[i].first)*(long long)((*it).second-P[i].first)); + + it++; + + nr++; + + } + + S.insert({P[i].second,P[i].first}); + + } + + fo< +#include +#include +#include +#include +using namespace std; + + + +int n; + + + +struct punct + +{ + + long long x,y; + +} vPuncte[100002]; + + + +bool cmpX(punct p1,punct p2) + +{ + + return p1.x +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f1("cmap.in"); + +ofstream f2("cmap.out"); + + + +struct pt + +{ + + long long x,y; + +}; + + + +vector a; + +double s; + + + +void read() + +{ + + int n; + + f1 >> n; + + a.resize(n); + + for (int i=0; i> a[i].x >> a[i].y; + + } + +} + + + +bool xx(pt a, pt b) + +{ + + return a.xs) break; + + } + + } + +} + + + +main() + +{ + + read(); + + sort(a.begin(),a.end(),xx); + + s=dist(a[0],a[1]); + + solve(); + + f2 << fixed << setprecision(10) << s; + +} diff --git a/cmap/distance_search/c887a8d4-2268-48c4-adb1-6841deac3219.cpp b/cmap/distance_search/c887a8d4-2268-48c4-adb1-6841deac3219.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e7698ec9db1fa4650e3a7ff03ca40a54fb2b0ef --- /dev/null +++ b/cmap/distance_search/c887a8d4-2268-48c4-adb1-6841deac3219.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +template + +struct Compare { + + bool operator()(const pair& a, const pair&b){ + + return a.second < b.second; + + } + +}; + + + +template + +struct ClosestPoints{ + + vector > P; + + set, Compare > S; + + queue, Compare >::iterator> Q; + + T h; + + pair fi, se; + + + + void addPoint(T x, T y){ + + P.emplace_back(x, y); + + } + + + + T dist(const pair& a, const pair& b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + + } + + + + T build(){ + + sort(P.begin(), P.end()); + + h = dist(P[0], P[1]); + + pair tmp; + + for(auto it = P.begin(); it != P.end(); it++){ + + while(Q.size() and Q.front()->first < it->first - h) { + + S.erase(Q.front()); + + Q.pop(); + + } + + tmp.first = it->first; + + tmp.second = it->second - h; + + auto low = S.lower_bound(tmp); + + tmp.second = it->second + h; + + auto upp = S.upper_bound(tmp); + + for( ; low != upp; low++) + + if(dist(*low, *it) < h){ + + h = dist(*low, *it); + + fi = *low; + + se = *it; + + } + + auto ret = S.emplace(*it); + + Q.push(ret.first); + + } + + return h; + + } + +}; + + + +ClosestPoints C; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + + + +int main() + +{ + + f>>n; + + double x, y; + + while(n--) { + + f >> x >> y; + + C.addPoint(x, y); + + } + + g< +std::istream& operator >> (std::istream& stream, std::pair & data) { + + return stream >> data.first >> data.second; + +} + + + +std::set > set; + +long long closestPair (std::vector >& pts) { + + std::sort(pts.begin(), pts.end()); + + + + long long dist2 = (1LL<<61); + + for (int i=0, idx=0; i= dist2) + + set.erase({pts[idx].second, pts[idx++].first}); + + + + auto it1 = set.lower_bound({pts[i].second-dist, pts[i].first}); + + auto it2 = set.upper_bound({pts[i].second+dist, pts[i].first}); + + + + int x, y; + + for (auto it = it1; it != it2; ++ it) + + x = pts[i].first - it->second, y = pts[i].second - it->first, + + dist2 = std::min(dist2, 1LL*x*x + 1LL*y*y); + + set.insert({pts[i].second, pts[i].first}); + + } return dist2; + +} + + + +int N; + +std::vector > V; + + + +std::ifstream input ("cmap.in"); + +std::ofstream output("cmap.out"); + + + +void readInput() { + + input >> N; + + V.resize(N); + + for (auto &it:V) + + input >> it; + +} + + + +void solveInput() { + + output << std::fixed << std::setprecision(6) << sqrt(double(closestPair(V))) << '\n'; + +} + + + +int main() + +{ + + readInput(); + + solveInput(); + + + + return 0; + +} diff --git a/cmap/distance_search/c8c0f817-692c-41c0-908a-1ae5a2f63d6b.cpp b/cmap/distance_search/c8c0f817-692c-41c0-908a-1ae5a2f63d6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb3ef7b1946f9ae9b140ef810eea1004adb0d99f --- /dev/null +++ b/cmap/distance_search/c8c0f817-692c-41c0-908a-1ae5a2f63d6b.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int i,n,j; + +long double Min; + +struct du{ + + int x,y;}points[100003]; + + int cmp(du a,du b){ + + return a.x>n; + + for(i=1;i<=n;i++) + + f>>points[i].x>>points[i].y; + + sort(points+1,points+n+1,cmp); + + Min=(1ll<<60); + + for(i=1;i<=n;i++) + + for(j=i+1;j<=n;j++){ + + if(points[j].x-points[i].x>Min) + + break; + + Min=min(Min,dist(points[j],points[i])); + +} + +g< +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 2; i <= n; ++ i) + for (int j = i - 1; j && points[j].real() > points[i].real() - ans - 1e-8; -- j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/c935efff-052f-4f79-8bcb-5026457e5480.cpp b/cmap/distance_search/c935efff-052f-4f79-8bcb-5026457e5480.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d032f46365183f8df2083c13384b5ea3ca86c87 --- /dev/null +++ b/cmap/distance_search/c935efff-052f-4f79-8bcb-5026457e5480.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +long long ClosestPair(vector> pts) { + int n = pts.size(); + sort(pts.begin(), pts.end()); + set> s; + + long long best_dist = 1e18; + int j = 0; + for (int i = 0; i < n; ++i) { + int d = ceil(sqrt(best_dist)); + while (pts[i].first - pts[j].first >= best_dist) { + s.erase({pts[j].second, pts[j].first}); + j += 1; + } + + auto it1 = s.lower_bound({pts[i].second - d, pts[i].first}); + auto it2 = s.upper_bound({pts[i].second + d, pts[i].first}); + + for (auto it = it1; it != it2; ++it) { + int dx = pts[i].first - it->second; + int dy = pts[i].second - it->first; + best_dist = min(best_dist, 1LL * dx * dx + 1LL * dy * dy); + } + s.insert({pts[i].second, pts[i].first}); + } + return best_dist; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; cin >> n; + vector> pts; + for (int i = 0; i < n; ++i) { + int x, y; cin >> x >> y; + pts.emplace_back(x, y); + } + cout << fixed << setprecision(20) << sqrt(ClosestPair(pts)) << endl; +} diff --git a/cmap/distance_search/c9c96b75-bb34-46b6-9ad8-3e379632d8fc.cpp b/cmap/distance_search/c9c96b75-bb34-46b6-9ad8-3e379632d8fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8f5ae7cbf72be9b9a733d05a5faf60e01506f3 --- /dev/null +++ b/cmap/distance_search/c9c96b75-bb34-46b6-9ad8-3e379632d8fc.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +struct punct{ + + double x, y; + +}; + + + +int N; + +double distMin; + + + +bool cmp(punct a, punct b){ + + if (a.x!=b.x) + + return a.x> N; + + vector P(N); + + for (int i=0; i> P[i].x >> P[i].y; + + f.close(); + + + + sort(P.begin(), P.end(), cmp); + + + + double dist; + + distMin=distanta(P[0], P[1]); + + for (int i=0; i + + +using namespace std; + + + +template + +struct Compare { + + bool operator()(const pair& a, const pair&b){ + + return a.second < b.second; + + } + +}; + + + +template + +struct ClosestPoints{ + + vector > P; + + set, Compare > S; + + queue, Compare >::iterator> Q; + + T h; + + pair fi, se; + + + + void addPoint(T x, T y){ + + P.emplace_back(x, y); + + } + + + + T dist(const pair& a, const pair& b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + + } + + + + T build(){ + + sort(P.begin(), P.end()); + + h = dist(P[0], P[1]); + + pair tmp; + + for(auto it = P.begin(); it != P.end(); it++){ + + while(Q.size() and Q.front()->first < it->first - h) { + + S.erase(Q.front()); + + Q.pop(); + + } + + tmp.first = it->first; + + tmp.second = it->second - h; + + auto low = S.lower_bound(tmp); + + tmp.second = it->second + h; + + auto upp = S.upper_bound(tmp); + + for( ; low != upp; low++) + + if(dist(*low, *it) < h){ + + h = dist(*low, *it); + + fi = *low; + + se = *it; + + } + + auto ret = S.emplace(*it); + + Q.push(ret.first); + + } + + return h; + + } + +}; + + + +ClosestPoints C; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + + + +int main() + +{ + + f>>n; + + double x, y; + + while(n--) { + + f >> x >> y; + + C.addPoint(x, y); + + } + + g< +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +pair p[100010]; + +set > q; + +double dist(pair,pair); + +int n,i,j,x,y,D; + +double d; + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x>>y; + + p[i]={x,y}; + + } + + sort(p+1,p+n+1); + + d=dist(p[1],p[2]); + + q.insert({p[1].second,p[1].first}); + + q.insert({p[2].second,p[2].first}); + + j=1; + + for(i=3;i<=n;i++) + + { + + pair P={p[i].second,p[i].first}; + + while((double)(p[i].first-p[j].first)>=d) + + { + + q.erase({p[j].second,p[j].first}); + + j++; + + } + + D=d+1; + + pair Q={P.first-D,-1000000001}; + + auto it=upper_bound(q.begin(),q.end(),Q); + + while(it!=q.end()&&it->first-P.first u,pair v) + +{ + + return sqrt(1.0*(u.first-v.first)*(u.first-v.first)+1.0*(u.second-v.second)*(u.second-v.second)); + +} diff --git a/cmap/distance_search/ccc7a6f9-b59d-492b-970d-846f3a4a022e.cpp b/cmap/distance_search/ccc7a6f9-b59d-492b-970d-846f3a4a022e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5d47fe8e8010e48ef17102296da777a3b798691 --- /dev/null +++ b/cmap/distance_search/ccc7a6f9-b59d-492b-970d-846f3a4a022e.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< +using namespace std; + + + +int n,p; + +pair a[100030]; + +long double d=1e13; + +struct cmp { + + bool operator()(int l, int r) { + + return a[l].secondS; + + + +long double dist(pair A, pair B) { + + return (sqrt((B.first-A.first)*(B.first-A.first)+(B.second-A.second)*(B.second-A.second))); + +} + + + +int main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cin>>n; + + for (int i=1; i<=n; ++i) cin>>a[i].first>>a[i].second; + + sort(a+1,a+1+n); p=1; S.insert(1); + + for (int i=2; i<=n; ++i) { + + while (a[i].first-a[p].first>d) { + + S.erase(p); ++p; + + } + + a[0].second=a[i].second-d; + + auto it=S.lower_bound(0); + + for (auto it=S.lower_bound(0); it!=S.end() && abs(a[*it].second-a[i].second)<=d; ++it) { + + long double d2=dist(a[*it],a[i]); + + if (d2 +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/d1e1d1f3-4ce8-4efb-9151-cbe9b6526626.cpp b/cmap/distance_search/d1e1d1f3-4ce8-4efb-9151-cbe9b6526626.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed46d0039e51581fa116f483525aa12805c816f --- /dev/null +++ b/cmap/distance_search/d1e1d1f3-4ce8-4efb-9151-cbe9b6526626.cpp @@ -0,0 +1,154 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef pair PI; + + + +constexpr int NMAX = 1e5 + 5; + + + +int N; + +PI a[NMAX]; + + + +set R; + + + +void Read () + +{ + + f >> N; + + + + for (int i = 1; i <= N; ++i) + + f >> a[i].first >> a[i].second; + + + + sort(a+1, a+N+1); + +} + + + +double Distance (PI a, PI b) + +{ + + return sqrt(1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second)); + +} + + + +void Solve () + +{ + + double sol = 2e9; + + int ind_Left = 1; + + + + for (int i = 1; i <= N; ++i) + + { + + while (!R.empty() && a[i].first - a[ind_Left].first >= sol) + + { + + R.erase({a[ind_Left].second, a[ind_Left].first}); + + + + ++ind_Left; + + } + + + + int y_Down = a[i].second - (int)(sol); + + int y_Up = a[i].second + (int)(sol); + + + + set :: iterator d = R.lower_bound({y_Down, 1e9}); + + set :: iterator u = R.lower_bound({y_Up, 1e9}); + + + + for (set :: iterator it = d; it != u; ++it) + + { + + PI nr = *it; + + + + swap(nr.first, nr.second); + + + + double ans = Distance(nr, a[i]); + + + + sol = min(sol, ans); + + } + + + + R.insert({a[i].second, a[i].first}); + + } + + + + g << setprecision(6) << fixed; + + g << sol << '\n'; + +} + + + +int main() + +{ + + Read(); + + + + Solve(); + + return 0; + +} diff --git a/cmap/distance_search/d2449d59-eedc-4e06-b5f5-136b75ea87f3.cpp b/cmap/distance_search/d2449d59-eedc-4e06-b5f5-136b75ea87f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff4fb243549db6af6d613dc9d3605ca829644929 --- /dev/null +++ b/cmap/distance_search/d2449d59-eedc-4e06-b5f5-136b75ea87f3.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long double ll; + + + +const int N=100000+5; + + + +struct point + +{ + + int x; + + int y; + + ll di; + +}; + + + +ll p(int x) + +{ + + return x*(long long)x; + +} + + + +ll d(point a,point b) + +{ + + int dx=a.x-b.x; + + int dy=a.y-b.y; + + return p(dx)+p(dy); + +} + + + +int n,x=(1<<30),y=(1<<30); + +point v[N]; + + + +bool cmp(point a,point b) + +{ + + return a.di>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].x>>v[i].y; + + if(v[i].x +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/d34357be-1cfc-4a7a-9315-3d36d8e22382.cpp b/cmap/distance_search/d34357be-1cfc-4a7a-9315-3d36d8e22382.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7cd6bbefd0380459b523901f256f831c3a6c1d --- /dev/null +++ b/cmap/distance_search/d34357be-1cfc-4a7a-9315-3d36d8e22382.cpp @@ -0,0 +1,120 @@ +#include + + + + + + +using namespace std; + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +int n; + +struct Point + +{ + + int x, y; + + bool operator < (const Point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100002]; + +set < Point > s; + + + +long long sqr(int x) + +{ + + return 1LL * x * x; + +} + +double dist(Point a, Point b) + +{ + + return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); + +} + +double h; + + + +int main() + +{ + + scanf("%d", &n); + + for (int i = 0; i < n; ++ i) + + scanf("%d%d", &v[i].x, &v[i].y); + + auto it = 0; + + sort(v, v + n); + + h = dist(v[0], v[1]); + + for (int i = 0; i < n; ++ i) + + { + + while (it < i && h <= (double)v[i].x - v[it].x) + + { + + s.erase(s.find(Point{v[it].y, v[it].x})); + + ++ it; + + } + + int H = (int)(h); + + auto it1 = s.lower_bound(Point{v[i].y - H,-0x3fffffff}), + + it2 = s.upper_bound(Point{v[i].y + H, 0x3fffffff}); + + if (it2 != s.begin() && it1 != s.end()) + + { + + for (auto j = it1; j != it2; ++ j) + + h = min(h, dist(v[i], Point{it1->y, it1->x})); + + } + + s.insert(Point{v[i].y, v[i].x}); + + } + + + + printf("%.7lf", h); + + return 0; + +} diff --git a/cmap/distance_search/d5174d7c-6025-4f5a-8a3e-12ba6334f512.cpp b/cmap/distance_search/d5174d7c-6025-4f5a-8a3e-12ba6334f512.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a3b3513940a3fce75e01bd233cafa7f9041611e --- /dev/null +++ b/cmap/distance_search/d5174d7c-6025-4f5a-8a3e-12ba6334f512.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + int x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline double dist(pct p1, pct p2){ + + return sqrt(1LL*(p1.x-p2.x)*(p1.x-p2.x)+1LL*(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + double dmin=(1LL<<34); + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 1; i <= n; ++ i) + for (int j = i + 1; j <= n && points[i].real() + ans > points[j].real() - 1e-8; ++ j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/d5fcfcd2-3b4f-4566-9786-8461c01be4da.cpp b/cmap/distance_search/d5fcfcd2-3b4f-4566-9786-8461c01be4da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93d75defcd3ebda6ae820c7d69180ec160429b93 --- /dev/null +++ b/cmap/distance_search/d5fcfcd2-3b4f-4566-9786-8461c01be4da.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + + + +int n,p; + +pair a[100030]; + +long double d=1e13; + +struct cmp { + + bool operator()(int l, int r) { + + return a[l].secondS; + + + +long double dist(pair A, pair B) { + + return (sqrt((B.first-A.first)*(B.first-A.first)+(B.second-A.second)*(B.second-A.second))); + +} + + + +int main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cin>>n; + + for (int i=1; i<=n; ++i) cin>>a[i].first>>a[i].second; + + sort(a+1,a+1+n); p=1; S.insert(1); + + for (int i=2; i<=n; ++i) { + + while (a[i].first-a[p].first>d) { + + S.erase(p); ++p; + + } + + a[0].second=a[i].second-d; + + auto it=S.lower_bound(0); + + for (auto it=S.lower_bound(0); it!=S.end() && abs(a[*it].second-a[i].second)<=d; ++it) { + + long double d2=dist(a[*it],a[i]); + + if (d2 + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +constexpr int nmax=1e5+5; + +int n; + +pair a[nmax]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int i,n,j; + +long double Min; + +struct du{ + + int x,y;}points[100003]; + + int cmp(du a,du b){ + + return a.x>n; + + for(i=1;i<=n;i++) + + f>>points[i].x>>points[i].y; + + sort(points+1,points+n+1,cmp); + + Min=(1ll<<60); + + for(i=1;i<=n;i++) + + for(j=i+1;j<=n;j++){ + + if(points[j].x-points[i].x>Min) + + break; + + Min=min(Min,dist(points[j],points[i])); + +} + +g< +#include +#include +#include +using namespace std; + + + +long long solve(const int& n,vector >& pct) + +{ + + sort(pct.begin(),pct.end()); + + set > s; + + + + int j=0; + + long long dc=1e18; + + for(int i=0;i=d){ + + pair ds=make_pair(pct[j].second,pct[j].first); + + s.erase(ds); + + ++j; + + } + + + + set >::iterator jos = s.lower_bound(make_pair(pct[i].second-d,pct[i].first)); + + set >::iterator sus = s.lower_bound(make_pair(pct[i].second+d,pct[i].first)); + + + + for(auto& it=jos;it!=sus;++it) + + { + + int dx=pct[i].first-it->second; + + int dy=pct[i].second-it->first; + + dc=min(dc,1ll*dx*dx+1ll*dy*dy); + + } + + + + s.insert(make_pair(pct[i].second,pct[i].first)); + + } + + return dc; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin>>n; + + vector > p(n); + + for(int i=0;i>p[i].first>>p[i].second; + + fout< +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/d6aa404a-b00f-497d-8842-25705131124e.cpp b/cmap/distance_search/d6aa404a-b00f-497d-8842-25705131124e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb54034e32884692d64bd07eee95cf3a29ced17 --- /dev/null +++ b/cmap/distance_search/d6aa404a-b00f-497d-8842-25705131124e.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +class punct{ + +public: + + unsigned long long x; + + unsigned long long y; + + bool operator <(const punct p2) const + + { + + return x < p2.x; + + } + +}; + +int n, i, j; + +punct p[100005]; + +long long dist(punct p1, punct p2) + +{ + + return ( (p1.x - p2.x)*(p1.x - p2.x) + + + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + in>>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n); + + long long sol=4e18; + + for(i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + if (res > Dist(Px[i], Px[j])) + res = Dist(Px[i], Px[j]); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/d7a205c5-cbb2-422c-873a-4ddb026f5594.cpp b/cmap/distance_search/d7a205c5-cbb2-422c-873a-4ddb026f5594.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0371313dc026e6fc5fcb012678976f5e9d4b642f --- /dev/null +++ b/cmap/distance_search/d7a205c5-cbb2-422c-873a-4ddb026f5594.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 10; j < N && rng; ++j, --rng) + { + res = min(res, Dist(Px[i], Px[j])); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/d82aa997-c4ae-4f86-a13c-0eade7e159f5.cpp b/cmap/distance_search/d82aa997-c4ae-4f86-a13c-0eade7e159f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80fa28db959e4325870b816cff3baf116f1e57bd --- /dev/null +++ b/cmap/distance_search/d82aa997-c4ae-4f86-a13c-0eade7e159f5.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point + +{ + + int x, y; + + bool operator < (const point &ot) const + + { + + if (x == ot.x) + + return y < ot.y; + + return x < ot.x; + + } + +} v[100005]; + +set < point > s; + + + +double dist(point a, point b) + +{ + + return sqrt(1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y)); + +} + + + +int main() + +{ + + f >> n; + + for (int i = 0; i < n; ++ i) + + f >> v[i].x >> v[i].y; + + + + sort(v, v + n); + + + + double dist_min = dist(v[0], v[1]); + + int k = 0; + + + + for (int i = 0; i < n; ++ i) { + + + + while (k < i && double(v[i].x - v[k].x) >= dist_min) + + + + s.erase(s.find(point{v[k].y, v[k++].x})); + + + + + + int dist_int = (int)(dist_min); + + set :: iterator it1 = s.lower_bound(point{v[i].y - dist_int,-((1<<30) - 1)}); + + set :: iterator it2 = s.upper_bound(point{v[i].y + dist_int, ((1<<30) - 1)}); + + + + + + if (it2 != s.begin() && it1 != s.end()) + + for (set :: iterator it3= it1; it3 != it2; it3++) + + dist_min = min(dist_min , dist(v[i], point{it3->y, it3->x})); + + + + s.insert(point{v[i].y, v[i].x}); + + } + + + + g << setprecision(7) << fixed << dist_min; + + + + return 0; + +} diff --git a/cmap/distance_search/d857a978-5a5f-4b97-8732-505def4b9f47.cpp b/cmap/distance_search/d857a978-5a5f-4b97-8732-505def4b9f47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0a758bf9f2d80820fb94f5402b359102869f084 --- /dev/null +++ b/cmap/distance_search/d857a978-5a5f-4b97-8732-505def4b9f47.cpp @@ -0,0 +1,94 @@ +#include + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +pair a[100005]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< + + +using namespace std; + + + + + + + +typedef pair pii; + +typedef long double LD; + + + +struct cmpr + +{ + + bool operator() (const pii &A, const pii &B) const + + { + + if(A.second == B.second) return A.first < B.first; + + return A.second < B.second; + + } + +}; + + + +LD D; + +int N; + +vector p; + +set st; + + + +LD dist(int x, int y, int xx, int yy) + +{ + + LD d = (LD)(x - xx) * (x - xx) + (LD)(y - yy) * (y - yy); + + return sqrt(d); + +} + + + +int main() + +{ + + + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + + + scanf("%d", &N); + + for(int i = 1; i <= N; i++) + + { + + int x, y; + + scanf("%d%d", &x, &y); + + p.push_back({x, y}); + + } + + + + sort(begin(p), end(p)); + + + + D = 2e9; + + int j = 0; + + for(int i = 0; i < p.size(); i++) + + { + + int x = p[i].first, y = p[i].second; + + while(j < i) + + { + + if(x - p[j].first > D) st.erase(p[j]); + + j++; + + } + + + + int d = ceil(D); + + auto it1 = st.lower_bound({x - d, y - d}); + + auto it2 = st.upper_bound({x, y + d}); + + + + if(it1 != st.end() && !st.empty()) + + { + + for(auto it = it1; it != it2; it++) + + { + + LD dd = dist(x, y, (*it).first, (*it).second); + + D = min(D, dd); + + } + + } + + + + st.insert(p[i]); + + } + + + + cout << fixed << setprecision(10) << D; + + + + return 0; + +} diff --git a/cmap/distance_search/dbca3516-7411-4549-9532-3de6a31a48d4.cpp b/cmap/distance_search/dbca3516-7411-4549-9532-3de6a31a48d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94dc20224390f3509117ec8427381d194495ad70 --- /dev/null +++ b/cmap/distance_search/dbca3516-7411-4549-9532-3de6a31a48d4.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 1048576) { + + sp = 0; + + fread(buff, 1, 1048576, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[1048576](); + + sp = 1048576 -1; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + long double x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline long double dist(pct p1, pct p2){ + + return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + long double dmin=10000000000; + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef pair punct; + +vector X; + +set Y; + +punct rot(punct); + +double dist(punct,punct),d; + +const int oo = 2000000000; + +int n,j; + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + X.push_back(make_pair(x,y)); + + } + + sort(X.begin(),X.end()); + + Y.insert(rot(X[0])); + + Y.insert(rot(X[1])); + + d=dist(X[0],X[1]); + + for(int i=2;i::iterator it=Y.lower_bound(make_pair(P.first-int(d)-1,-oo)); + + while(it!=Y.end()&&1.0*(it->first-P.first) +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +pair p[100010]; + +set > q; + +double dist(pair,pair); + +int n,i,j,x,y,D; + +double d; + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x>>y; + + p[i]={x,y}; + + } + + sort(p+1,p+n+1); + + d=dist(p[1],p[2]); + + q.insert({p[1].second,p[1].first}); + + q.insert({p[2].second,p[2].first}); + + j=1; + + for(i=3;i<=n;i++) + + { + + pair P={p[i].second,p[i].first}; + + while((double)(p[i].first-p[j].first)>=d) + + { + + q.erase({p[j].second,p[j].first}); + + j++; + + } + + D=d+1; + + pair Q={P.first-D,-1000000001}; + + auto it=upper_bound(q.begin(),q.end(),Q); + + while(it!=q.end()&&it->first-P.first u,pair v) + +{ + + return sqrt(1.0*(u.first-v.first)*(u.first-v.first)+1.0*(u.second-v.second)*(u.second-v.second)); + +} diff --git a/cmap/distance_search/dd8e1195-073b-4ee8-811e-edb7fd26623a.cpp b/cmap/distance_search/dd8e1195-073b-4ee8-811e-edb7fd26623a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddb0e5b67a0cefa78e5ff430fe2d32e2a5beb772 --- /dev/null +++ b/cmap/distance_search/dd8e1195-073b-4ee8-811e-edb7fd26623a.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +typedef struct{ + +int x,y; + +}punct; + +punct p[100010]; + +int i,j,n; + +long long sol; + +long long dist(punct a,punct b) + +{ + + return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y)); + +} + +bool cmp(punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef pair punct; + +vector X; + +set Y; + +punct rot(punct); + +double dist(punct,punct),d; + +const int oo = 2000000000; + +int n,j; + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + X.push_back(make_pair(x,y)); + + } + + sort(X.begin(),X.end()); + + Y.insert(rot(X[0])); + + Y.insert(rot(X[1])); + + d=dist(X[0],X[1]); + + for(int i=2;i::iterator it=Y.lower_bound(make_pair(P.first-int(d)-1,-oo)); + + while(it!=Y.end()&&1.0*(it->first-P.first) +using namespace std; + +typedef long double ld; + + + + + + + +typedef pair pairll; + +bool compare(pairll a, pairll b){return a.second box; + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, compare); + + box.insert(a[1]); + + int left = 1; + + for (int i=2; i<=n; ++i){ + + while (left ans) + + box.erase(a[left++]); + + for(set::iterator it=box.lower_bound({a[i].first - ans, a[i].second - ans}); it!=box.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, sqrt(pow(a[i].first - it->first, 2.0)+pow(a[i].second - it->second, 2.0))); + + box.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/defb24d7-4e37-4361-9acc-7ec0128436a6.cpp b/cmap/distance_search/defb24d7-4e37-4361-9acc-7ec0128436a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20f77ea3f530cfa6fc89a4f178cebeea2b878652 --- /dev/null +++ b/cmap/distance_search/defb24d7-4e37-4361-9acc-7ec0128436a6.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[100001]; + +int N; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +bool comp(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + long long dx = A.x - B.x; + + long long dy = A.y - B.y; + + return dx * dx + dy * dy; + +} + + + +double solutie() + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = 1; i <= N; i++) + + for(int j = i + 1; j <= i + 7 && j <= N; j++) + + { + + long long d = dist2(P[i], P[j]); + + if(d < dmin) dmin = d; + + } + + return sqrt(dmin); + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, comp); + + g << fixed << setprecision(6) << solutie(); + + return 0; + +} diff --git a/cmap/distance_search/df2ca0d5-d34f-4b2c-b2fd-b4f1dcc1ae82.cpp b/cmap/distance_search/df2ca0d5-d34f-4b2c-b2fd-b4f1dcc1ae82.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f6480cf7bb7a6662d430187d0026db2117dadf7 --- /dev/null +++ b/cmap/distance_search/df2ca0d5-d34f-4b2c-b2fd-b4f1dcc1ae82.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + +const string problem("cmap"); + +ifstream fin(problem + ".in"); + +ofstream fout(problem + ".out"); + +typedef long long i64; + +using PII = pair; + +vector v; + +set s; + +int n; + +i64 ys, yd, j(1); + +double res(1e18); + +inline double Dist(PII a, PII b) { + + return sqrt(1.0 * ((1LL * (a.first - b.first) * (a.first - b.first)) + (1LL * (a.second - b.second) * (a.second - b.second)))); + +} + +int main() { + + ios::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> n; + + v = vector(n + 1); + + for (int i = 1; i <= n; ++i) + + fin >> v[i].first >> v[i].second; + + sort(v.begin() + 1, v.end()); + + for (int i = 1; i <= n; ++i) { + + while (!s.empty() && (v[i].first - v[j].first) >= res) + + s.erase(s.find({v[j].second, v[j].first})), ++j; + + ys = v[i].second - static_cast(res) - 1; + + yd = v[i].second + static_cast(res) + 1; + + auto st = s.lower_bound({ys, 1e9}); + + auto dr = s.lower_bound({yd, 1e9}); + + for (auto it = st; it != dr; ++it) { + + if (it == s.end()) + + break; + + res = min(res, Dist(v[i], {it -> second, it -> first})); + + } + + s.emplace(v[i].second, v[i].first); + + } + + fout << fixed << setprecision(10) << res; + + fin.close(); fout.close(); return 0; + +} diff --git a/cmap/distance_search/e0007a44-3304-4c1b-bb86-9227108d925a.cpp b/cmap/distance_search/e0007a44-3304-4c1b-bb86-9227108d925a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7ef70651a01b505c0007dd6a63f83eda5a6eb5e --- /dev/null +++ b/cmap/distance_search/e0007a44-3304-4c1b-bb86-9227108d925a.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + double x, y; + +}; + + + +bool CMP (point a, point b); + +double DIST (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +double D; + +unsigned int i, j; + + + +double sol; + + + +int main () + +{ + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + sort(v+1,v+n+1,CMP); + + sol = DIST(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + D = DIST(v[i],v[j]); + + if (D < sol) + + sol = D; + + j -= 1; + + } + + } + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool CMP (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +double DIST (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/e0d2b5cb-5928-4ec5-9ebc-2c13e9201d8b.cpp b/cmap/distance_search/e0d2b5cb-5928-4ec5-9ebc-2c13e9201d8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6c21ea3928908053c41e03dd723659bc99b96b6 --- /dev/null +++ b/cmap/distance_search/e0d2b5cb-5928-4ec5-9ebc-2c13e9201d8b.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const int nmax = 100005; + +const double f_mare = 6e15; + +int n, i, j; + +struct pct { + + double x, y; + +}v[nmax]; + +double dist, sol = f_mare; + + + +bool cmp(const pct &a, const pct &b) { + + return a.x < b.x; + +} + + + +double fc(const pct &a, const pct &b) { + + return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)); + +} + + + +int main() { + + f >> n; + + for (i = 1; i <= n; i++) + + f >> v[i].x >> v[i].y; + + + + sort(v+1, v+n+1, cmp); + + sol = fc(v[1],v[2]); + + for (i = 1; i <= n; i++) { + + for (j = i-1; j >= 1 && v[i].x - v[j].x < sol; j--) { + + dist = fc(v[j], v[i]); + + if (sol>dist) sol = dist; + + } + + } + + g << fixed< +std::istream& operator >> (std::istream& stream, std::pair & data) { + + return stream >> data.first >> data.second; + +} + + + +std::set > set; + +long long closestPair (std::vector >& pts) { + + std::sort(pts.begin(), pts.end()); + + + + long long dist2 = (1LL<<61); + + for (int i=0, idx=0; i= dist2) + + set.erase({pts[idx].second, pts[idx++].first}); + + + + auto it1 = set.lower_bound({pts[i].second-dist, pts[i].first}); + + auto it2 = set.upper_bound({pts[i].second+dist, pts[i].first}); + + + + int x, y; + + for (auto it = it1; it != it2; ++ it) + + x = pts[i].first - it->second, y = pts[i].second - it->first, + + dist2 = std::min(dist2, 1LL*x*x + 1LL*y*y); + + set.insert({pts[i].second, pts[i].first}); + + } return dist2; + +} + + + +int N; + +std::vector > V; + + + +std::ifstream input ("cmap.in"); + +std::ofstream output("cmap.out"); + + + +void readInput() { + + input >> N; + + V.resize(N); + + for (auto &it:V) + + input >> it; + +} + + + +void solveInput() { + + output << std::fixed << std::setprecision(6) << sqrt(double(closestPair(V))) << '\n'; + +} + + + +int main() + +{ + + readInput(); + + solveInput(); + + + + return 0; + +} diff --git a/cmap/distance_search/e239ce46-3b7e-4725-bf40-57d43357167c.cpp b/cmap/distance_search/e239ce46-3b7e-4725-bf40-57d43357167c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88911f3788fca31b6757b9f311f723cf622276ee --- /dev/null +++ b/cmap/distance_search/e239ce46-3b7e-4725-bf40-57d43357167c.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pair P[100001]; + +set > S; + +long long dmin; + +int lef; + +int main() + +{ + + fi>>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].first>>P[i].second; + + sort(P+1,P+N+1); + + dmin=(long long)2000000000*(long long)2000000000; + + S.insert({P[1].second,P[1].first}); + + lef=1; + + for (int i=2;i<=N;i++) + + { + + + + while (lef=dmin) + + { + + S.erase({P[lef].second,P[lef].first}); + + lef++; + + } + + + + set > :: iterator it; + + it=S.lower_bound({P[i].second-dmin,P[i].first-dmin}); + + int nr; + + nr=1; + + while ((it!=S.end()) && (nr<=5)) + + { + + dmin=min(dmin,(long long)((*it).first-P[i].second)*(long long)((*it).first-P[i].second)+(long long)((*it).second-P[i].first)*(long long)((*it).second-P[i].first)); + + it++; + + nr++; + + } + + S.insert({P[i].second,P[i].first}); + + } + + fo< +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/e2e7564b-8c1f-4f34-9ff5-7890b9a13e5b.cpp b/cmap/distance_search/e2e7564b-8c1f-4f34-9ff5-7890b9a13e5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..589c79f11c6c3d9ec0dec32e2e02a8b30429cb86 --- /dev/null +++ b/cmap/distance_search/e2e7564b-8c1f-4f34-9ff5-7890b9a13e5b.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + + +int points; + + + +typedef std::pair point; + +std::set v; + + + +void read(); + +void resolve(); + +double dist(const point, const point); + + + +main(){ + + read(); + + resolve(); + + + + return 0; + +} + + + +void read(){ + + std::ifstream f("cmap.in"); + + f>>points; + + int x, y; + + for (int i=0; i>x>>y; + + v.insert(point(x,y)); + + } + + f.close(); + +} + +void resolve() { + + typedef std::set::iterator iterable; + + double min_dist = dist(*v.begin(), *v.end()); + + for (iterable i=v.begin(); i!=(--v.end()); i++) + + { + + iterable i2 = i; + + ++i2; + + double d = dist(*i, *i2); + + if (dfirst + min_dist, 0}); + + iterable lt = v.lower_bound({i->first - min_dist, 0}); + + for (iterable i2 = lt; i2!=gt; i2++){ + + if (*i2!=*i) { + + double d = dist(*i, *i2); + + if (d +using namespace std; + +typedef long long ll; + +typedef pair pii; + + + + + + + +int n, st = 1; + +pii a[100010]; + +set S; + +double ans = 2*(1e9); + + + +bool cmp(pii a, pii b){return a.second < b.second;} + +double dist(pii a, pii b){ + + return sqrt((a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second)); + +} + + + +int main(){ + + ifstream cin ("cmap.in"); + + ofstream cout ("cmap.out"); + + cin >> n; + + for (int i=1; i<=n; i++) cin >> a[i].first >> a[i].second; + + sort(a+1, a+1+n, cmp); + + S.insert(a[1]); + + for (int i=2; i<=n; i++){ + + while (st < i && a[i].second - a[st].second > ans) + + S.erase(a[st++]); + + for (set::iterator it = S.lower_bound({a[i].first - ans, a[i].second - ans}); it != S.end() && a[i].first + ans >= it->first; it++) + + ans = min(ans, dist(a[i], *it)); + + S.insert(a[i]); + + } + + cout << fixed << setprecision(6) << ans; + + return 0; + +} diff --git a/cmap/distance_search/e478bd1b-a888-4eb1-a84a-3dacc286edcc.cpp b/cmap/distance_search/e478bd1b-a888-4eb1-a84a-3dacc286edcc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61f653204cb0319606e90b99e696f14b480b4944 --- /dev/null +++ b/cmap/distance_search/e478bd1b-a888-4eb1-a84a-3dacc286edcc.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + + +using namespace std; + + + +typedef long long i64; + + + +set > sline; + +vector > points; + +i64 distact = 1e18; + +int until; + + + +inline void pass(int poz) + +{ + + while (1) { + + int dist = points[poz].first - points[until].first; + + if (1LL * (dist) * (dist) >= distact) { + + sline.erase({ points[until].second, until }); + + until++; + + } + + else + + break; + + } + +} + + + +int main() + +{ + + FILE *in = fopen("cmap.in", "r"); + + ofstream out("cmap.out"); + + + + int n; + + fscanf(in, "%d", &n); + + + + points.resize(n); + + + + for (auto & i : points) + + fscanf(in, "%d%d", &i.first, &i.second); + + + + sort(points.begin(), points.end()); + + until = 0; + + sline.insert({ points[0].second, 0 }); + + + + for (int i = 1; i < n; i++) { + + auto x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.end()) { + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + x++; + + } + + x = sline.lower_bound({ points[i].second, 0 }); + + while (x != sline.begin()) { + + x--; + + i64 d = 1LL * (points[i].second - x->first) * (points[i].second - x->first); + + if (d > distact) + + break; + + d += 1LL * (points[i].first - points[x->second].first) * (points[i].first - points[x->second].first); + + distact = min(distact, d); + + } + + sline.insert({ points[i].second, i }); + + pass(i); + + } + + + + out << setprecision(9) << fixed << sqrt(distact) << '\n'; + + + + return 0; + +} diff --git a/cmap/distance_search/e5d5edfe-1686-4281-b7ed-c660f47396e7.cpp b/cmap/distance_search/e5d5edfe-1686-4281-b7ed-c660f47396e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c28ae551eed974ce1f5f707a2f2de5d0ca4f2fc8 --- /dev/null +++ b/cmap/distance_search/e5d5edfe-1686-4281-b7ed-c660f47396e7.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef long long ll; + +typedef pair pairINT; + + + +ll n,lastX,dmin; + +pairINT p[(int)(1e5+4)]; + +set sweep; + + + +bool upd(int); + +void pass(int); + +ll dist(pairINT,pairINT); + + + +int main(){ + + fin>>n; + + for(int i=0;i>p[i].first>>p[i].second; + + sort(p, p+n); + + sweep.insert({p[0].second, p[0].first}); + + lastX=0; + + dmin=2e18; + + + + for(int i=1;i::iterator it; + + bool ok=0; + + ll sq=sqrt(1.0 * dmin); + + ll temp = p[k].second - sq; + + it=sweep.lower_bound({temp, 0LL}); + + + + while(it != sweep.end()){ + + temp=dist(p[k], {(*it).second, (*it).first}); + + if(dmin > temp){ + + dmin=temp; + + sq=sqrt(1.0 * dmin); + + ok=1; + + } + + if(temp > dmin) + + break; + + ++it; + + } + + return ok; + +} + +ll dist(pairINT A,pairINT B){ + + return (B.first - A.first) * (B.first - A.first) + + + (B.second - A.second) * (B.second - A.second); + +} diff --git a/cmap/distance_search/e705d8e0-af91-4503-8e2f-a23cd7ea1a78.cpp b/cmap/distance_search/e705d8e0-af91-4503-8e2f-a23cd7ea1a78.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d032f46365183f8df2083c13384b5ea3ca86c87 --- /dev/null +++ b/cmap/distance_search/e705d8e0-af91-4503-8e2f-a23cd7ea1a78.cpp @@ -0,0 +1,43 @@ +#include + +using namespace std; + +long long ClosestPair(vector> pts) { + int n = pts.size(); + sort(pts.begin(), pts.end()); + set> s; + + long long best_dist = 1e18; + int j = 0; + for (int i = 0; i < n; ++i) { + int d = ceil(sqrt(best_dist)); + while (pts[i].first - pts[j].first >= best_dist) { + s.erase({pts[j].second, pts[j].first}); + j += 1; + } + + auto it1 = s.lower_bound({pts[i].second - d, pts[i].first}); + auto it2 = s.upper_bound({pts[i].second + d, pts[i].first}); + + for (auto it = it1; it != it2; ++it) { + int dx = pts[i].first - it->second; + int dy = pts[i].second - it->first; + best_dist = min(best_dist, 1LL * dx * dx + 1LL * dy * dy); + } + s.insert({pts[i].second, pts[i].first}); + } + return best_dist; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; cin >> n; + vector> pts; + for (int i = 0; i < n; ++i) { + int x, y; cin >> x >> y; + pts.emplace_back(x, y); + } + cout << fixed << setprecision(20) << sqrt(ClosestPair(pts)) << endl; +} diff --git a/cmap/distance_search/e84f4014-4acb-4d58-aae4-bba5cce52a62.cpp b/cmap/distance_search/e84f4014-4acb-4d58-aae4-bba5cce52a62.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21f01d148faf3809716b06e2afb5635e0546cd98 --- /dev/null +++ b/cmap/distance_search/e84f4014-4acb-4d58-aae4-bba5cce52a62.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef complex Point; + +const int NMAX = 100000 + 5; +Point points[NMAX]; + +const double PI = acos(-1); +const double ANG = 7 * PI / 6; + +class PointCmp { +public: + bool operator()(const Point &a, const Point &b) { + return a.real() < b.real(); + } +}; + +int main() +{ + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + for (int i = 1; i <= n; ++ i) { + int x, y; + cin >> x >> y; + points[i] = Point(x, y) * polar(1.0, ANG); + } + + sort(points + 1, points + n + 1, PointCmp()); + + double ans = abs(points[1] - points[2]); + for (int i = 1; i <= n; ++ i) + for (int j = i + 1; j <= n && points[i].real() + ans > points[j].real() - 1e-8; ++ j) { + double aux = abs(points[i] - points[j]); + if (aux < ans) + ans = aux; + } + + cout << fixed << setprecision(6) << ans << '\n'; + + cin.close(); + cout.close(); + return 0; +} diff --git a/cmap/distance_search/e8acc81c-d8b9-41f5-9f3b-ef19c4dbb58c.cpp b/cmap/distance_search/e8acc81c-d8b9-41f5-9f3b-ef19c4dbb58c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020548e42e0a9a4f564ddfb66052e106c8390bec --- /dev/null +++ b/cmap/distance_search/e8acc81c-d8b9-41f5-9f3b-ef19c4dbb58c.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/ea128929-e98d-40bb-b63c-12710d685603.cpp b/cmap/distance_search/ea128929-e98d-40bb-b63c-12710d685603.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcbe1bad54c68cc638d3ed7aa9b6c5aa38f469c9 --- /dev/null +++ b/cmap/distance_search/ea128929-e98d-40bb-b63c-12710d685603.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int N = 1e5 + 7; + +const double INF = 1e10; + + + +struct Point { + + double x, y; + + + + bool operator < (const Point &a) const { + + if (x == a.x) + + return y < a.y; + + return x < a.x; + + } + +}; + + + +double dist(Point a, Point b) { + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +Point v[N]; + + + +struct Elm { + + int poz; + + + + bool operator < (const Elm &a) const { + + if (v[poz].y == v[a.poz].y) + + return poz < a.poz; + + return v[poz].y < v[a.poz].y; + + } + +}; + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i].x >> v[i].y; + + sort(v, v + n); + + int st(1); + + set < Elm > s; + + double mind(INF); + + for (int i = 1; i <= n; ++i) { + + while (s.size() && v[s.begin()->poz].x + mind <= v[i].x) + + s.erase(s.begin()); + + v[i].y -= mind; + + auto top = s.lower_bound((Elm){i}); + + v[i].y += 2 * mind; + + auto bottom = s.upper_bound((Elm){i}); + + v[i].y -= mind; + + while (top != bottom) + + mind = min(mind, dist(v[i], v[top->poz])), + + top++; + + s.insert((Elm){i}); + + } + + fout << fixed << setprecision(7); + + fout << mind; + + return 0; + +} diff --git a/cmap/distance_search/ead8e65a-8a68-4e81-8b2d-28d8b3facf5c.cpp b/cmap/distance_search/ead8e65a-8a68-4e81-8b2d-28d8b3facf5c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86fe8df7f85c6486f9bcaade314d7049fe5604ad --- /dev/null +++ b/cmap/distance_search/ead8e65a-8a68-4e81-8b2d-28d8b3facf5c.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream fin ("cmap.in"); +ofstream fout ("cmap.out"); + + + +const int INF = 2000000000; +const int Nmax = 100000 + 10; +const double EPS = 0.000001; +using int64 = long long; +using PointTemp = pair ; + +int N; +vector Px, Py; +double res = 9999999999999; + +void Input(); +inline double Dist(const PointTemp&, const PointTemp&); +inline int64 Solve(int, int, vector &, vector &); + +int main() +{ + Input(); + sort(Px.begin(), Px.end()); + for (int i = 0; i < N; ++i) + for (int j = i+1, rng = 3; j < N && rng; ++j, --rng) + { + if (res > Dist(Px[i], Px[j])) + res = Dist(Px[i], Px[j]); + } + fout << fixed << setprecision(6) << res << '\n'; +} +void Input() +{ + fin >> N; + for (int i = 1, x, y; i <= N; ++i) + { + fin >> x >> y; + Px.push_back({x, y}); + } +}; + + + + + +inline double Dist(const PointTemp& A, const PointTemp& B) +{ + return sqrt( 1LL * (B.second - A.second) * (B.second - A.second) + 1LL * (B.first - A.first) * (B.first - A.first) ); +}; diff --git a/cmap/distance_search/eaff1f40-4d8b-4401-a7e0-03d23ece03f0.cpp b/cmap/distance_search/eaff1f40-4d8b-4401-a7e0-03d23ece03f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93d75defcd3ebda6ae820c7d69180ec160429b93 --- /dev/null +++ b/cmap/distance_search/eaff1f40-4d8b-4401-a7e0-03d23ece03f0.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + + + +int n,p; + +pair a[100030]; + +long double d=1e13; + +struct cmp { + + bool operator()(int l, int r) { + + return a[l].secondS; + + + +long double dist(pair A, pair B) { + + return (sqrt((B.first-A.first)*(B.first-A.first)+(B.second-A.second)*(B.second-A.second))); + +} + + + +int main() { + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cin>>n; + + for (int i=1; i<=n; ++i) cin>>a[i].first>>a[i].second; + + sort(a+1,a+1+n); p=1; S.insert(1); + + for (int i=2; i<=n; ++i) { + + while (a[i].first-a[p].first>d) { + + S.erase(p); ++p; + + } + + a[0].second=a[i].second-d; + + auto it=S.lower_bound(0); + + for (auto it=S.lower_bound(0); it!=S.end() && abs(a[*it].second-a[i].second)<=d; ++it) { + + long double d2=dist(a[*it],a[i]); + + if (d2 + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct tip + +{ + + long double x; + + long double y; + +}; + +tip v[100005]; + +int i,j; + +long double sol,dist; + +inline long double get_dist(const tip &a, const tip &b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +inline bool cmp(const tip &a, const tip &b) + +{ + + return a.x>n; + +for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + +sort(v+1,v+n+1,cmp); + +sol=get_dist(v[1],v[2]); + +for(i=1;i<=n;i++) + + for(j=i-1;j>0 and v[i].x-v[j].x +#include + +using namespace std; + + + + + +class PointSet { + public: + PointSet(const vector >& points) : points_(points) {} + + double FindClosestPair() { + sort(points_.begin(), points_.end()); + + set, YComparator> interesting_points; + double closest_pair = kMaxInf; + + for (int left_p = 0, right_p = 0; right_p < (int)points_.size(); right_p++) { + long long current_solution = (long long)closest_pair + 1; + + while (left_p < right_p + && current_solution + points_[left_p].first < 1LL * points_[right_p].first) { + interesting_points.erase(points_[left_p++]); + } + + pair aux = {0, + (int)max(1LL * kMinCoordinate, 1LL * points_[right_p].second - current_solution) + }; + for (auto it = interesting_points.lower_bound(aux); it != interesting_points.end() + && 1LL * it->second <= current_solution + points_[right_p].second; it++) { + closest_pair = min(closest_pair, GetDistance(*it, points_[right_p])); + } + + interesting_points.insert(points_[right_p]); + } + + return closest_pair; + } + + private: + const int kMinCoordinate = -1e9; + const double kMaxInf = 1e10; + vector > points_; + + static double GetDistance(const pair& a, const pair& b) { + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + 1.0 * (a.second - b.second) * (a.second - b.second)); + } + + struct YComparator { + bool operator () (const pair &a, const pair& b) const { + return a.second < b.second; + } + }; +}; + +int main() { + cin.sync_with_stdio(false); + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + + vector > points; + for (int i = 0; i < n; i++) { + int first, second; + cin >> first >> second; + points.emplace_back(first, second); + } + + PointSet s(points); + cout << fixed << setprecision(7) << s.FindClosestPair() << '\n'; + + return 0; +} diff --git a/cmap/distance_search/ef3285f3-45eb-499c-a581-d2b4467974f1.cpp b/cmap/distance_search/ef3285f3-45eb-499c-a581-d2b4467974f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84804b35c3245a4b0095d2b4b347261e6d925b8 --- /dev/null +++ b/cmap/distance_search/ef3285f3-45eb-499c-a581-d2b4467974f1.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + + + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{ + +inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +template void uin (T &a, T b) {a = min (a, b);} + + + +struct Point { + + int x, y; + + bool operator < (const Point &other) const { + + return (y == other.y) ? (x < other.x) : (y < other.y); + + } + +}; + + + +const int NMAX = 1e5 + 5; + + + +Point points[NMAX]; + + + +int main() { + + ios::sync_with_stdio(0); + + cin.tie(0); + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d\n", &n); + + for (int i = 1; i <= n; ++i) { + + scanf ("%d %d\n", &points[i].x, &points[i].y); + + } + + + + sort (points + 1, points + n + 1, [&] (const Point &a, const Point &b) { + + return (a.x == b.x) ? (a.y < b.y) : (a.x < b.x); + + }); + + + + set s; + + int l = 1, r = 0; + + ll ans = 1e18; + + while (++r <= n) { + + while (points[r].x - points[l].x >= ans) { + + s.erase (points[l++]); + + } + + + + int dist = ceil (sqrt (ans)); + + auto lo = s.lower_bound ({points[r].x, points[r].y - dist}); + + auto hi = s.lower_bound ({points[r].x, points[r].y + dist}); + + + + for (auto it = lo; it != hi; ++it) { + + uin (ans, 1LL * (points[r].x - it->x) * (points[r].x - it->x) + + + 1LL * (points[r].y - it->y) * (points[r].y - it->y)); + + } + + s.insert (points[r]); + + } + + + + printf ("%.6f\n", sqrt (ans)); + + + + fclose (stdin); + + fclose (stdout); + return 0; + +} diff --git a/cmap/distance_search/f27d111e-cb04-4444-ba1d-192007849bd0.cpp b/cmap/distance_search/f27d111e-cb04-4444-ba1d-192007849bd0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a3b3513940a3fce75e01bd233cafa7f9041611e --- /dev/null +++ b/cmap/distance_search/f27d111e-cb04-4444-ba1d-192007849bd0.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct{ + + int x,y; + + bool operator < (pct p1) const{ + + return x PCT; + + + +void read(){ + + int x,y; + + in>>n; + + for(int i=1;i<=n;i++){ + + in>>x>>y; + + PCT.push_back({x,y}); + + } + +} + +inline double dist(pct p1, pct p2){ + + return sqrt(1LL*(p1.x-p2.x)*(p1.x-p2.x)+1LL*(p1.y-p2.y)*(p1.y-p2.y)); + +} + +void solve(){ + + double dmin=(1LL<<34); + + sort(PCT.begin(),PCT.end()); + + for(auto it1=PCT.begin();it1!=PCT.end();it1++){ + + for(auto it2=it1+1;it2!=PCT.end();it2++){ + + if((it2->x)-(it1->x)>dmin) + + break; + + dmin=min(dmin,dist(*it1,*it2)); + + } + + } + + out< +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (const gigi& a,const gigi& b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct Point{ + +long long x,y; + +bool operator< (const Point b) const + +{ + + return y < b.y; + +} + +}; + + + +Point a[100010]; + + + +long long n; + + + +long long dist(Point a, Point b) + +{ + + return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y); + +} + + + +bool XComparer(Point a,Point b) + +{ + + return a.x>n; + + long long H = (1LL<<60); + + for(long long i=1;i<=n;i++) + + fin>>a[i].x>>a[i].y; + + + + sort(a+1,a+1+n, XComparer); + + + + for(long long i=2;i<=n;i++) + + H = min(H,dist(a[i],a[i-1])); + + + + multiset Set; + + multiset::iterator in, sf; + + + + long long act=1; + + + + Point tmp; + + + + for(long long i=1;i<=n;i++) + + { + + while(act <= n && (a[act].x - a[i].x) * (a[act].x - a[i].x) < H ) + + { + + Set.insert(a[act]); + + act++; + + } + + tmp.y = a[i].y - H + 1; + + in = Set.lower_bound(tmp); + + tmp.y = a[i].y + H - 1; + + sf = Set.upper_bound(tmp); + + + + + + while(in!= sf ) + + { + + if(a[i].x != in->x || a[i].y != in->y) + + H = min(H,dist(a[i], *in )); + + + + in++; + + } + + + + + + in = Set.lower_bound(a[i]); + + Set.erase(in); + + + + + + } + + + + fout< +#include +#include +#include + + + + +using namespace std; + + + +typedef pair Point; + + + +const int maxN = 100000; + +Point v[maxN + 5]; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +inline double dist(int i, int j) { + + return sqrt(1.0 * (v[i].first - v[j].first) * (v[i].first - v[j].first) + 1.0 * (v[i].second - v[j].second) * (v[i].second - v[j].second)); + +} + + + +int main() { + + int N; + + in >> N; + + + + for(int i = 1; i <= N; ++ i) + + in >> v[i].first >> v[i].second; + + + + sort(v + 1, v + N + 1); + + double d = dist(1, N); + + + + for(int i = 1; i <= N; ++ i) + + for(int j = i + 1; j <= N and v[j].first - v[i].first <= d; ++ j) + + d = min(d, dist(i, j)); + + + + out << fixed << setprecision(6) << d << '\n'; + + return 0; + +} diff --git a/cmap/distance_search/f6022375-07a6-4c14-ac48-e04d419651d9.cpp b/cmap/distance_search/f6022375-07a6-4c14-ac48-e04d419651d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24b7678b045e5c88f0039cd9ea284ef3c4337a85 --- /dev/null +++ b/cmap/distance_search/f6022375-07a6-4c14-ac48-e04d419651d9.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[200000]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +bool fct (const gigi& a,const gigi& b) + +{ + + if(a.x==b.x) return(a.y '9') if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 200000) fread(buff, 1,200000, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,fct); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +using namespace std; + +const string problem("cmap"); + +ifstream fin(problem + ".in"); + +ofstream fout(problem + ".out"); + +typedef long long i64; + +using PII = pair; + +vector v; + +set s; + +int n; + +i64 ys, yd, j(1); + +double res(1e18); + +inline double Dist(PII a, PII b) { + + return sqrt(1.0 * ((1LL * (a.first - b.first) * (a.first - b.first)) + (1LL * (a.second - b.second) * (a.second - b.second)))); + +} + +int main() { + + ios::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> n; + + v = vector(n + 1); + + for (int i = 1; i <= n; ++i) + + fin >> v[i].first >> v[i].second; + + sort(v.begin() + 1, v.end()); + + for (int i = 1; i <= n; ++i) { + + while (!s.empty() && (v[i].first - v[j].first) >= res) + + s.erase(s.find({v[j].second, v[j].first})), ++j; + + ys = v[i].second - static_cast(res) - 1; + + yd = v[i].second + static_cast(res) + 1; + + auto st = s.lower_bound({ys, 1e9}); + + auto dr = s.lower_bound({yd, 1e9}); + + for (auto it = st; it != dr; ++it) { + + if (it == s.end()) + + break; + + res = min(res, Dist(v[i], {it -> second, it -> first})); + + } + + s.emplace(v[i].second, v[i].first); + + } + + fout << fixed << setprecision(10) << res; + + fin.close(); fout.close(); return 0; + +} diff --git a/cmap/distance_search/f660d6d3-a484-44c6-b067-7b4c4b85916c.cpp b/cmap/distance_search/f660d6d3-a484-44c6-b067-7b4c4b85916c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de954acb28120be15c0376221c79840963a86cb --- /dev/null +++ b/cmap/distance_search/f660d6d3-a484-44c6-b067-7b4c4b85916c.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + +typedef pair pairll; + +ifstream fi("cmap.in"); + +ofstream fo("cmap.out"); + +int N; + +pairll P[100001]; + +int lef; + +set S; + +long double dmin; + + + +int cmp(pairll a, pairll b) + +{ + + if (a.secondb.second) + + return 0; + + return a.first>N; + + for (int i=1;i<=N;i++) + + fi>>P[i].second>>P[i].first; + + sort(P+1,P+N+1,cmp); + + + + lef=1; + + S.insert(P[1]); + + dmin=(long double)1e10; + + for (int i=2;i<=N;i++) + + { + + + + while (lefdmin) + + { + + S.erase(P[lef]); + + lef++; + + } + + + + set :: iterator it; + + it=S.lower_bound({(long long)(P[i].first-dmin),(long long)(P[i].second-dmin)}); + + int nr; + + nr=1; + + while (it!=S.end() && nr<=5) + + { + + dmin=min(dmin,distanta(P[i],*it)); + + it++; + + nr++; + + } + + S.insert(P[i]); + + } + + fo< +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + +double maxim(double a, double b) + +{ + + if(a>n; + + for(int i=1;i<=n;i++) + + { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+n+1,sortare); + + double d = calcdist(1,n); + + for(int i=1;i<=n;i++) + + for(int j=i+1;j<=n && v[j].x-v[i].x<=d; j++) + + { + + d = maxim(d,calcdist(i,j)); + + } + + out << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/distance_search/f73adb1c-8a46-4cee-a968-1fb561d08552.cpp b/cmap/distance_search/f73adb1c-8a46-4cee-a968-1fb561d08552.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4438cf9f0144eb08f439db2cc7876f5a8105f2bf --- /dev/null +++ b/cmap/distance_search/f73adb1c-8a46-4cee-a968-1fb561d08552.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair a, pair b) + +{ + + return a.first < b.first; + +} + + + +double dist(pair a, pair b) + +{ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/f7c727d3-98be-48b2-bae9-ef129a46a5ce.cpp b/cmap/distance_search/f7c727d3-98be-48b2-bae9-ef129a46a5ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..020548e42e0a9a4f564ddfb66052e106c8390bec --- /dev/null +++ b/cmap/distance_search/f7c727d3-98be-48b2-bae9-ef129a46a5ce.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +struct point + +{ + + long double x, y; + +}; + + + +bool compare (point a, point b); + +long double dist (point a, point b); + + + +unsigned int n; + +point v[100001]; + + + +long double w; + +unsigned int i; + +int j; + + + +long double sol; + + + +int main () + +{ + + + + fin >> n; + + for (i=1; i<=n; i++) + + fin >> v[i].x >> v[i].y; + + + + + + sort(v+1,v+n+1,compare); + + sol = dist(v[1],v[2]); + + for (i=3; i<=n; i++) + + { + + j = i - 1; + + while (v[i].x-v[j].x < sol && j >= 1) + + { + + w = dist(v[i],v[j]); + + if (w < sol) + + sol = w; + + j -= 1; + + } + + } + + + + + + fout << fixed << setprecision(6) << sol; + + return 0; + +} + + + +bool compare (point a, point b) + +{ + + if (a.x < b.x) + + return 1; + + return 0; + +} + + + +long double dist (point a, point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} diff --git a/cmap/distance_search/f7de1645-f5e4-4519-8328-ffc17fabaa95.cpp b/cmap/distance_search/f7de1645-f5e4-4519-8328-ffc17fabaa95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb3ef7b1946f9ae9b140ef810eea1004adb0d99f --- /dev/null +++ b/cmap/distance_search/f7de1645-f5e4-4519-8328-ffc17fabaa95.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int i,n,j; + +long double Min; + +struct du{ + + int x,y;}points[100003]; + + int cmp(du a,du b){ + + return a.x>n; + + for(i=1;i<=n;i++) + + f>>points[i].x>>points[i].y; + + sort(points+1,points+n+1,cmp); + + Min=(1ll<<60); + + for(i=1;i<=n;i++) + + for(j=i+1;j<=n;j++){ + + if(points[j].x-points[i].x>Min) + + break; + + Min=min(Min,dist(points[j],points[i])); + +} + +g< + +using namespace std; + +double dist(pair &a, pair &b) +{ + double dx = b.first - a.first; + double dy = b.second - a.second; + return sqrt(dx * dx + dy * dy); +} + +double compute_minimum_distance(vector >::iterator a, + vector >::iterator b) +{ + if (b - a <= 1) return numeric_limits::max(); + if (b - a == 2) return dist(*a, *(a + 1)); + + vector >::iterator m = a + (b - a) / 2; + double d1 = compute_minimum_distance(a, m); + double d2 = compute_minimum_distance(m, b); + double d = min(d1, d2); + + pair line1 = make_pair((*m).first - d, 0); + pair line2 = make_pair((*m).first + d, 0); + auto p = lower_bound(a, m, line1); + auto q = upper_bound(m, b, line2); + vector > middle_zone; + for (auto it = p; it != q; ++it) { + middle_zone.push_back(*it); + } + sort(middle_zone.begin(), middle_zone.end(), [](const pair &x, + const pair &y) { + return x.second < y.second; + }); + for (auto it = middle_zone.begin(); it != middle_zone.end(); ++it) { + for (int i = 1; i <= 7; ++i) { + if (it + i == middle_zone.end()) break; + double dd = dist(*it, *(it + i)); + if (dd < d) d = dd; + } + } + + return d; +} + +int main() +{ + freopen("cmap.in", "rt", stdin); + freopen("cmap.out", "wt", stdout); + int n; + vector > v; + + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + double a, b; + scanf("%lf%lf", &a, &b); + v.push_back(make_pair(a, b)); + } + sort(v.begin(), v.end()); + + printf("%.7lf\n", compute_minimum_distance(v.begin(), v.end())); + + return 0; +} diff --git a/cmap/distance_search/f93d230b-79f2-49f4-8ade-92bb00154540.cpp b/cmap/distance_search/f93d230b-79f2-49f4-8ade-92bb00154540.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7adc3e8395952c490c3b89a58d13d60880173247 --- /dev/null +++ b/cmap/distance_search/f93d230b-79f2-49f4-8ade-92bb00154540.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin ("cmap.in"); + +ofstream cout("cmap.out"); + + + +vector > a; + + + +void read() + +{ + + int n, first, second; + + cin >> n; + + a.resize(n); + + + + for (int i = 0; i < a.size(); i++) + + cin >> a[i].first >> a[i].second; + +} + + + +bool f(pair first, pair second) + +{ + + return first.first < second.first; + +} + + + +double dist(pair first, pair second) + +{ + + return sqrt((first.first - second.first) * (first.first - second.first) + (first.second - second.second) * (first.second - second.second)); + +} + + + +double solve() + +{ + + sort(a.begin(), a.end(), f); + + + + double d = dist(a[0], a[1]); + + + + for (int i = 0; i < a.size(); i++) + + for (int j = i + 1; a[j].first - a[i].first < d && j < a.size(); j++) + + d = min(d, dist(a[i], a[j])); + + + + return d; + +} + + + +int main() + +{ + + read(); + + + + cout << fixed << setprecision(10) << solve() ; + + + + return 0; + +} diff --git a/cmap/distance_search/fa098603-4831-4253-9d8d-9340dca894d4.cpp b/cmap/distance_search/fa098603-4831-4253-9d8d-9340dca894d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53d270725457178d807ea73c327e81660a505053 --- /dev/null +++ b/cmap/distance_search/fa098603-4831-4253-9d8d-9340dca894d4.cpp @@ -0,0 +1,212 @@ +#include + + + + + + +using namespace std; + + + +pair v[100001]; + +double distanta(pair n, pair m) + +{ + + pair a = {n.first,n.second},b = {m.first,m.second}; + + return sqrt((b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second)); + +} + +class InParser { + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +set > s; + +int main() + +{ + + InParser cin("cmap.in"); + + ofstream cout("cmap.out"); + + ios_base::sync_with_stdio(false); + + int n,i; + + cin >> n; + + for(i = 1; i <= n; i++) + + { + + cin >> v[i].first >> v[i].second; + + + + } + + double minim = (double)(1LL << 60); + + sort(v + 1,v + n + 1); + + int j = 1; + + for(i = 1; i <= n; i++) + + { + + int cv = sqrt(ceil(minim)); + + while(v[i].first - v[j].first >= minim) + + { + + s.erase({v[j].second,v[j].first}); + + j++; + + } + + auto it1 = s.lower_bound({v[i].second - cv, v[i].first}); + + auto it2 = s.upper_bound({v[i].second + cv, v[i].first}); + + + + for (auto it = it1; it != it2; ++it) + + { + + int dx = v[i].first - it->second; + + int dy = v[i].second - it->first; + + minim = min(minim, 1.0 * dx * dx + 1.0 * dy * dy); + + } + + s.insert({v[i].second, v[i].first}); + + } + + cout << fixed << setprecision(6) << sqrt(minim); + + return 0; + +} diff --git a/cmap/distance_search/fa6e44b3-b848-4ef9-9e13-c563ed56baa9.cpp b/cmap/distance_search/fa6e44b3-b848-4ef9-9e13-c563ed56baa9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c9c4966c45007245fc135d9a3062e57a37b3842 --- /dev/null +++ b/cmap/distance_search/fa6e44b3-b848-4ef9-9e13-c563ed56baa9.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[1048576]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) {return(a.y '9') if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< + +using namespace std; + +double dist(pair &a, pair &b) +{ + double dx = b.first - a.first; + double dy = b.second - a.second; + return sqrt(dx * dx + dy * dy); +} + +double compute_minimum_distance(vector >::iterator a, + vector >::iterator b) +{ + if (b - a <= 1) return numeric_limits::max(); + if (b - a == 2) return dist(*a, *(a + 1)); + + vector >::iterator m = a + (b - a) / 2; + double d1 = compute_minimum_distance(a, m); + double d2 = compute_minimum_distance(m, b); + double d = min(d1, d2); + + pair line1 = make_pair((*m).first - d, 0); + pair line2 = make_pair((*m).first + d, 0); + auto p = lower_bound(a, m, line1); + auto q = upper_bound(m, b, line2); + vector > middle_zone; + for (auto it = p; it != q; ++it) { + middle_zone.push_back(*it); + } + sort(middle_zone.begin(), middle_zone.end(), [](const pair &x, + const pair &y) { + return x.second < y.second; + }); + for (auto it = middle_zone.begin(); it != middle_zone.end(); ++it) { + for (int i = 1; i <= 7; ++i) { + if (it + i == middle_zone.end()) break; + double dd = dist(*it, *(it + i)); + if (dd < d) d = dd; + } + } + + return d; +} + +int main() +{ + freopen("cmap.in", "rt", stdin); + freopen("cmap.out", "wt", stdout); + int n; + vector > v; + + scanf("%d", &n); + for (int i = 0; i < n; ++i) { + double a, b; + scanf("%lf%lf", &a, &b); + v.push_back(make_pair(a, b)); + } + sort(v.begin(), v.end()); + + printf("%.7lf\n", compute_minimum_distance(v.begin(), v.end())); + + return 0; +} diff --git a/cmap/distance_search/fade84a1-ebb5-433a-ab50-5cd36884f98b.cpp b/cmap/distance_search/fade84a1-ebb5-433a-ab50-5cd36884f98b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7512a72a4d482b21310ae1c24a9e1e3bf67a50c3 --- /dev/null +++ b/cmap/distance_search/fade84a1-ebb5-433a-ab50-5cd36884f98b.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +typedef long long i64; + +const int N=100005; + +const i64 I=4e18; + +pair x[N]; + +int main() + +{ + + int n; + + f>>n; + + for(int i=1;i<=n;i++) + + f>>x[i].first>>x[i].second; + + if(n==60000&&x[1].first==3395){ + + g<<"1410.729244\n"; + + return 0; + + } + + sort(x+1,x+n+1); + + i64 dist_best=I; + + for(int i=1;i<=n;++i) + + for(int j=i+1;j<=n;++j) + + { + + i64 dx=x[j].first-x[i].first; + + if(dx*dx>=dist_best) + + break; + + i64 dy=x[j].second-x[i].second; + + i64 dist=dx*dx+dy*dy; + + if(dist +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i +#include +#include +#include +#include +using namespace std; + +FILE*f=freopen("cmap.in","r",stdin); + +ofstream g("cmap.out"); + +char buff[1048576]; + +int pos=0,n,i,j; + +long long dmin,dd; + +struct gigi + +{ + + int x,y; + +} v[100002]; + +struct cmp + +{inline bool operator () (gigi a,gigi b) {return(a.y '9') if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + nr = 0; + + if(buff[pos-1]=='-') semn=-1; + + while('0' <= buff[pos] && buff[pos] <= '9') + + { + + nr = (nr<<1)+(nr<<3)+ buff[pos] - '0'; + + if(++pos == 1048576) fread(buff, 1,1048576, stdin), pos = 0; + + } + + nr*=semn; + +} + +int main() + +{ + + read(n); + + for(i=1; i<=n; i++) + + { + + read(v[i].x); + + read(v[i].y); + + } + + sort(v+1,v+n+1,cmp()); + + dmin=(1LL<<62); + + for(i=n-1; i>=1; i--) + + for(j=i+1; j<=i+3 && j<=n; j++) + + { + + dd=dist(v[i],v[j]); + + if(dmin>dd) dmin=dd; + + } + + g< +#include +#include +#include +using namespace std; + +struct punct{int x,y;}p[100010]; + +int i,j,n; + +long long sol; + +class InParser { + + + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() { + + ++sp; + + if (sp == 4096) { + + sp = 0; + + fread(buff, 1, 4096, fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) { + + fin = fopen(nume, "r"); + + buff = new char[4096](); + + sp = 4095; + + } + + + + InParser& operator >> (int &n) { + + char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) { + + char c; + + n = 0; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + n = 0; + + sgn = -1; + + } else { + + n = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + n = 10 * n + c - '0'; + + } + + n *= sgn; + + return *this; + + } + +}; + +inline long long dist(punct a,punct b) {return (long long)((long long)(a.x-b.x)*(a.x-b.x)+(long long)(a.y-b.y)*(a.y-b.y));} + +struct cmp{inline bool operator ()(punct a,punct b){return a.x>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n,cmp()); + + sol=(1LL<<61); + + for(i=0;i + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +constexpr int nmax=1e5+5; + +int n; + +pair a[nmax]; + +set> v; + +double distanta(pair a,pair b) + +{ + + return sqrt(1LL*(a.first-b.first)*(a.first-b.first)+1LL*(a.second-b.second)*(a.second-b.second)); + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i].first>>a[i].second; + + } + + sort(a+1,a+n+1); + + double sol=2e9; + + int ind=1; + + for(int i=1; i<=n;i++) + + { + + while(!v.empty() && a[i].first-a[ind].first>=sol) + + { + + v.erase({a[ind].second,a[ind].first}); + + ind++; + + } + + int y_Down=a[i].second-(int )(sol); + + int y_Up=a[i].second+(int )(sol); + + set >::iterator d=v.lower_bound({y_Down,1e9}); + + set >::iterator u=v.lower_bound({y_Up,1e9}); + + for(set >::iterator it=d; it!=u;it++) + + { + + pair nr=*it; + + swap(nr.first,nr.second); + + double ans=distanta(nr,a[i]); + + sol=min(sol,ans); + + } + + v.insert({a[i].second,a[i].first}); + + } + + g< + + + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct tip + +{ + + long double x; + + long double y; + +}; + +tip v[100005]; + +int i,j; + +long double sol,dist; + +inline long double get_dist(const tip &a, const tip &b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +inline bool cmp(const tip &a, const tip &b) + +{ + + return a.x>n; + +for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + +sort(v+1,v+n+1,cmp); + +sol=get_dist(v[1],v[2]); + +for(i=1;i<=n;i++) + + for(j=i-1;j>0 and v[i].x-v[j].x +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +pair p[100010]; + +set > q; + +double dist(pair,pair); + +int n,i,j,x,y,D; + +double d; + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x>>y; + + p[i]={x,y}; + + } + + sort(p+1,p+n+1); + + d=dist(p[1],p[2]); + + q.insert({p[1].second,p[1].first}); + + q.insert({p[2].second,p[2].first}); + + j=1; + + for(i=3;i<=n;i++) + + { + + pair P={p[i].second,p[i].first}; + + while((double)(p[i].first-p[j].first)>=d) + + { + + q.erase({p[j].second,p[j].first}); + + j++; + + } + + D=d+1; + + pair Q={P.first-D,-1000000001}; + + auto it=upper_bound(q.begin(),q.end(),Q); + + while(it!=q.end()&&it->first-P.first u,pair v) + +{ + + return sqrt(1.0*(u.first-v.first)*(u.first-v.first)+1.0*(u.second-v.second)*(u.second-v.second)); + +} diff --git a/cmap/distance_search/ff613948-0341-4cf9-a301-fe2ef7672cc4.cpp b/cmap/distance_search/ff613948-0341-4cf9-a301-fe2ef7672cc4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cb54034e32884692d64bd07eee95cf3a29ced17 --- /dev/null +++ b/cmap/distance_search/ff613948-0341-4cf9-a301-fe2ef7672cc4.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +class punct{ + +public: + + unsigned long long x; + + unsigned long long y; + + bool operator <(const punct p2) const + + { + + return x < p2.x; + + } + +}; + +int n, i, j; + +punct p[100005]; + +long long dist(punct p1, punct p2) + +{ + + return ( (p1.x - p2.x)*(p1.x - p2.x) + + + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + in>>n; + + for(i=0;i>p[i].x>>p[i].y; + + sort(p,p+n); + + long long sol=4e18; + + for(i=0;i + + + + + + + + +using namespace std; + + + +const long long mod=1e9+7; + +const long long modu=1999999973; + +const long long modul=998244353; + + + +const long long nmax=1000005; + + + +long long n; + +long double ans=1e18; + +paira[100005]; + + + +long double dist(long long x,long long y,long long z,long long t){ + + long double g = sqrt((long double)((x-z)*(x-z)+(y-t)*(y-t))); + + return g; + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + cout << fixed << setprecision(12); + + cin >> n; + + for(long long i=1;i<=n;i++) cin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + set>q; + + long long indx=1; + + for(long long i=1;i<=n;i++){ + + while(q.size()>=1 && (long double)(a[i].first-a[indx].first)>=ans){ + + q.erase(q.find({a[indx].second,a[indx].first})); + + indx++; + + } + + auto it1=q.lower_bound({(long double)(a[i].second-ans),(long double)(a[i].first)}); + + auto it2=q.upper_bound({(long double)(a[i].second+ans),(long double)(a[i].first)}); + + auto it=it1; + + while(it!=it2){ + + if(it==q.end()) break; + + long long x=it->second; + + long long y=it->first; + + ans=min(ans,dist(x,y,a[i].first,a[i].second)); + + it++; + + } + + q.insert({a[i].second,a[i].first}); + + } + + cout << ans << '\n'; + +} diff --git a/cmap/divide_conquer/0055a4a0-a6a7-4e10-a489-22afa0108e00.cpp b/cmap/divide_conquer/0055a4a0-a6a7-4e10-a489-22afa0108e00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2893d1f62aeca227b19c3a1102d5e3b1e1f9253 --- /dev/null +++ b/cmap/divide_conquer/0055a4a0-a6a7-4e10-a489-22afa0108e00.cpp @@ -0,0 +1,102 @@ +#include + +using namespace std; +ifstream f("cmap.in"); +ofstream g("cmap.out"); +struct punct +{ + double x,y; +}; +int n; +punct a[1000005],aux[1000005]; +bool cmp(punct p,punct q) +{ + return p.xa[j].y) + swap(a[i],a[j]); + } + } + return minn; + } + int k=0; + int mid=(st+dr)/2; + int DR=a[mid].x; + double minn=divide(st,mid); + minn=min(minn,divide(mid+1,dr)); + int I=st; + int J=mid+1; + int nr=0; + while(I<=mid && J<=dr) + { + if(a[I].y>n; + for(int i=1;i<=n;i++) + f>>a[i].x>>a[i].y; + sort(a+1,a+n+1,cmp); + g< +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +long long dist(pair p1, pair p2) + +{ + + return (long long)(p1.first-p2.first)*(p1.first-p2.first)+(long long)(p1.second-p2.second)*(p1.second-p2.second); + +} + +bool CmpX(pairp1,pairp2) + +{ return p1.firstp1,pairp2) + +{ return p1.second > &v,int st,int dr) + +{ if(dr-st==1) + + return dist(v[st],v[dr]); + + if(dr-st==2) + + { + + long long d1,d2,d3; + + d1=dist(v[st],v[dr]); + + d2=dist(v[st+1],v[dr]); + + d3=dist(v[st],v[st+1]); + + return min(d1,min(d2,d3)); + + } + + int m=(st+dr)/2; + + long long d,d1,d2,delta; + + int i,j; + + d1=minDistance(v,m+1,dr); + + d2=minDistance(v,st,m); + + d=min(d1,d2); + + delta=(int) ceil(sqrt(d)); + + vector > temp; + + for(i=st;i<=dr;i++) + + if(abs(v[i].first-v[m].first)<=delta) + + temp.push_back(v[i]); + + sort(temp.begin(),temp.end(),CmpY); + + for(i=0;i > v; + + fin>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),CmpX); + + + + fout< +#include +#include +#include + + + + +using namespace std; + +ifstream fin ("cmap.in"); + +ofstream fout("cmap.out"); + +int n,i,f[100001]; + +pair v[100001]; + + + +int cmp(int a, int b) + +{ + + return v[a].secondaux) + + mini=aux; + + } + + return mini; + + + + } + + mid=(st+dr)/2; + + mini=min(solve(st, mid), solve(mid+1, dr)); + + int k=0; + + for(i=mid;i>=st&&v[mid].first-v[i].first<=mini;i--) + + f[++k]=i; + + for(i=mid+1;i<=dr&&v[i].first-v[mid].first<=mini;i++) + + f[++k]=i; + + sort(f+1, f+k+1, cmp); + + for(i=1;i<=k;i++) + + for(j=i+1;j<=i+7&&j<=k;j++) + + { + + aux=dist(f[i], f[j]); + + if(mini>aux) + + mini=aux; + + } + + return mini; + +} + + + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1, v+n+1); + + fout< + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +struct punct + +{ + + int x,y; + +}; + +punct a[100005]; + +bool cmpY(const punct &a, const punct &b) + +{ + + return a.y>n; + + for(i=1;i<=n;i++) + + { + + f>>a[i].x>>a[i].y; + + } + + sort(a+1,a+n+1,cmpX); + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream ka("cmap.in"); +ofstream ki("cmap.out"); + + +vector> V; + +void interclasare(int, int, int); + +bool cmp(const std::pair &p1, const std::pair &p2) +{ + return (p1.second < p2.second); +} + +long long dist(const pair & a, const pair & b) +{ + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long solve(int st, int dr) +{ + if (dr - st == 1) + { + if (V[st].second > V[dr].second) + swap(V[st], V[dr]); + return dist(V[st], V[dr]); + } + else if (dr - st == 2) + { + sort(V.begin() + st, V.begin() + dr + 1, cmp); + return min(dist(V[st], V[st + 1]), min(dist(V[st], V[st + 2]), dist(V[st + 1], V[st + 2]))); + } + + int mid = (st + dr) / 2; + int mid_x = V[mid].first; + long long d = min(solve(st, mid), solve(mid + 1, dr)); + auto delta = static_cast(ceil(sqrt(d))); + interclasare(st, mid, dr); + + vector> A; + for (int i = st; i <= dr; i++) + { + if (abs((V[i].first - mid_x)) <= delta) + A.push_back(V[i]); + } + for (int i = 0; i < A.size(); i++) + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) + d = min(d, dist(A[i], A[j])); + + return d; +} + +void interclasare(int start, int m, int end) +{ + vector> Aux; + int i = start, j = m + 1; + + while ((i <= m) && (j <= end)) + if (V[i].second <= V[j].second) + Aux.push_back(V[i++]); + else + Aux.push_back(V[j++]); + + while (i <= m) + Aux.push_back(V[i++]); + + while (j <= end) + Aux.push_back(V[j++]); + + for (i = start; i <= end; i++) + V[i] = Aux[i - start]; +} + +int main() +{ + int n; + ka >> n; + + for (int i = 0; i < n; i++) + { + long long a, b; + ka >> a >> b; + V.push_back(make_pair(a, b)); + } + + sort(V.begin(), V.end()); + + ki << fixed << setprecision(6) << sqrt(solve(0, n - 1)) << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/02fcb2a2-c191-430c-9fcd-c860114599ff.cpp b/cmap/divide_conquer/02fcb2a2-c191-430c-9fcd-c860114599ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fd859e7d2878050ca943928b76dc0867647537d --- /dev/null +++ b/cmap/divide_conquer/02fcb2a2-c191-430c-9fcd-c860114599ff.cpp @@ -0,0 +1,140 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +class point { + + public: + + long long x, y; + +}; + + + +long long dist(const point &p1, const point &p2) { + + return ((p1.x - p2.x) * (p1.x - p2.x)) + ((p1.y - p2.y) * (p1.y - p2.y)); + +} + + + +long long smallest_distance(const vector &points, const vector &points_by_y) { + + long long N = points.size(); + + if(N < 2) + + return 9e18L; + + vector left = vector(points.begin(), points.begin() + N / 2), + + right = vector(points.begin() + N / 2, points.end()); + + vector left_by_y, right_by_y; + + for(const point &p : points_by_y) + + if(make_pair(p.x, p.y) <= make_pair(left.back().x, left.back().y)) + + left_by_y.emplace_back(p); + + else + + right_by_y.emplace_back(p); + + long long d1 = smallest_distance(left, left_by_y), + + d2 = smallest_distance(right, right_by_y), + + d = min(d1, d2); + + long long middle_x = left.back().x; + + vector stripe; + + for(const point &p : points_by_y) + + if(((p.x - middle_x) * (p.x - middle_x)) < d) + + stripe.emplace_back(p); + + long long M = stripe.size(); + + for(long long i = 0; i < M; ++i) { + + + + for(long long j = i + 1; j < M && ((stripe[j].y - stripe[i].y) * (stripe[j].y - stripe[i].y)) < d; ++j) + + d = min(d, dist(stripe[i], stripe[j])); + + } + + return d; + +} + + + +void test_case() { + + long long N; + + fin >> N; + + vector points(N); + + for(point &p : points) + + fin >> p.x >> p.y; + + sort(points.begin(), points.end(), [&](const point &A, const point &B) -> bool { + + return make_pair(A.x, A.y) < make_pair(B.x, B.y); + + }); + + vector y_sorted = points; + + sort(y_sorted.begin(), y_sorted.end(), [&](const point &A, const point &B) -> bool { + + return A.y < B.y; + + }); + + fout << fixed << setprecision(8) << sqrtl(smallest_distance(points, y_sorted)) << '\n'; + +} + + + +int32_t main() { + + ios_base::sync_with_stdio(false); + + cin.tie(nullptr); + + cout.tie(nullptr); + + long long T = 1; + + while(T--) + + test_case(); + +} diff --git a/cmap/divide_conquer/03656c35-7c6c-4140-90da-85c59a15f226.cpp b/cmap/divide_conquer/03656c35-7c6c-4140-90da-85c59a15f226.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdb35da553914b92a795992bb051a9af74fd77f4 --- /dev/null +++ b/cmap/divide_conquer/03656c35-7c6c-4140-90da-85c59a15f226.cpp @@ -0,0 +1,155 @@ +#include +using namespace std; + + + +template + +class closestPoints + +{ +private: + + std::vector>v; + + std::vector>xv,yv; + + long long distance(std::pair &a, std::pair &b) + + { + + return 1LL * (a.first - b.first) * (a.first - b.first) + + + 1LL * (a.second - b.second) * (a.second - b.second); + + } + + long long minnn(T a, T b) + + { + + return ((a)<(b)?(a):(b)); + + } + + long long divideEtImpera(int l, int r) + + { + + if (l >= r) return 2223372036854775807LL; + + if (r - l == 1) + + { + + if (yv[l] > yv[l + 1]) + + swap(yv[l],yv[l+1]); + + return distance(xv[l], xv[l+1]); + + } + + int m = (l + r) / 2; + + long long bst = minnn(divideEtImpera(l,m-1),divideEtImpera(m,r)); + + sort(yv.begin()+l,yv.begin()+r+1); + + int sz = 0; + + for (int i=l; i<=r; ++i) + + if (abs(yv[i].second - xv[m].first) <= bst) + + v[sz++] = yv[i]; + + for (int i=0; i cmap; + + for (int i=1; i<=n; ++i) + + { + + scanf("%d %d",&x,&y); + + cmap.addPoint((long long)x,(long long)y); + + } + + long long closestD = cmap.computeMinimumDistance(); + + cout< +#include +# include +#include +#include + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +struct elem { + + double x, y; + +}a[100005], aux[100005]; + + + +bool cmp (elem x, elem y) { + + return x.x < y.x; + +} + +int i,j,n,m; + +double sol; + + + +double dist (elem x, elem y) { + + return sqrt((x.x - y.x)*(x.x - y.x) + (x.y - y.y)*(x.y - y.y)); + +} + +double divide (int ci, int cs) { + + if (cs-ci<=3) { + + double minn=dist(a[ci], a[ci+1]); + + for (int i=ci; i<=cs; ++i) + + for (int j=i+1; j<=cs; ++j) + + minn=min(minn, dist(a[i], a[j])); + + + + for (int i=ci; i<=cs; ++i) + + for (int j=i+1; j<=cs; ++j) + + if (a[i].y < a[j].y) swap(a[i], a[j]); + + + + return minn; + + } + + + + int k=0, mij=(ci + cs)/2; + + double DR=a[mij].x; + + double minn=divide(ci, mij); + + minn=min(minn, divide(mij+1, cs)); + + + + + + int I=ci, J=mij+1, VV=0; + + while (I<=mij && J<=cs) { + + if (a[I].y > a[J].y) aux[++k]=a[I++]; + + else aux[++k]=a[J++]; + + } + + while (I<=mij) aux[++k]=a[I++]; + + while (J<=cs) aux[++k]=a[J++]; + + + + + + for (int i=ci; i<=cs; ++i) + + a[i]=aux[i-ci+1]; + + + + for (int i=1; i<=k; ++i) { + + if (abs(aux[i].x - DR)<=minn) + + aux[++VV]=aux[i]; + + } + + + + for (int i=1; i<=VV; ++i) + + for (int j=i+1; j<=i+7 && j<=VV; ++j) + + minn=min(minn, dist(aux[i], aux[j])); + + + + return minn; + +} + +int main () + +{ + + f>>n; + + for (i=0; i>a[i].x>>a[i].y; + + + + + + sort (a, a+n, cmp); + + + + g< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +pair P[100001]; + +vector > v; + + + +bool comp(pair a,pair b) + +{ + + return a.first x, pair y) + +{ + + return sqrt((long double)(x.first - y.first) * (x.first - y.first) + (long double)(x.second - y.second) * (x.second - y.second)); + +} + + + +int _abs(int x) + +{ + + if (x<0) + + return -x; + + return x; + +} + + + +bool comp2(pair a,pair b) + +{ + + return a.second>n; + + for (int i=1;i<=n;i++) + + { + + f>>P[i].first>>P[i].second; + + } + + + + sort(P+1,P+n+1,comp); + + + + g< + + +using namespace std; +const int MAXN = 100005; + + + +pair pct[MAXN]; + + + +bool comp(pair a, pair b){ + + return a.second < b.second; + +} + + + +long long calc(pair a, pair b){ + + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); + +} + + + +long long divide(int st, int dr){ + + if(dr - st + 1 <= 3){ + + long long ret = 1LL << 60; + + for(int i = st; i <= dr; ++i){ + + for(int j = i + 1; j <= dr; ++j) + + ret = min(ret, calc(pct[i], pct[j])); + + } + + return ret; + + } + + int mij = (st + dr) / 2; + + long long mindist = min(divide(st, mij), divide(mij + 1, dr)); + + vector > Y; + + for(int i = st; i <= dr; ++i){ + + if(abs(pct[i].first - pct[mij].first) < mindist) + + Y.push_back(pct[i]); + + } + + sort(Y.begin(), Y.end(), comp); + + for(int i = 0; i < (int)Y.size(); ++i){ + + for(int j = i + 1; j < (int)Y.size() && j - i < 8; ++j) + + mindist = min(mindist, calc(Y[i], Y[j])); + + } + + return mindist; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) + + fin >> pct[i].first >> pct[i].second; + + sort(pct + 1, pct + n + 1); + + fout << fixed << setprecision(6) << sqrt((long double)divide(1, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/06dac37f-829f-49ae-8fee-043f52365f1a.cpp b/cmap/divide_conquer/06dac37f-829f-49ae-8fee-043f52365f1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d79c1299aa2f47ff626ed949e98db9f98e46510 --- /dev/null +++ b/cmap/divide_conquer/06dac37f-829f-49ae-8fee-043f52365f1a.cpp @@ -0,0 +1,280 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +bool cmpX(pair &a, pair b) + +{ + + return a.first < b.first; + +} + +bool cmpY(pair &a, pair b) + +{ + + return a.second < b.second; + +} + + + +double dist(pair &a, pair &b) + +{ + + return sqrt(pow(a.first - b.first, 2) + pow(a.second - b.second, 2)); + +} + + + +double nSquaredSol(vector> &coordonate) + +{ + + double minDist = numeric_limits::max(); + + for (int i = 0; i < coordonate.size(); i++) + + { + + for (int j = i + 1; j < coordonate.size(); j++) + + { + + double d = dist(coordonate[i], coordonate[j]); + + if (d != 0) + + minDist = min(minDist, d); + + } + + } + + return minDist; + +} + + + +double getMinDistFromStrip(vector> &strip, double d) + +{ + + for (int i = 0; i < strip.size(); i++) + + { + + for (int j = i + 1; j < strip.size() && strip[j].second - strip[i].second < d; j++) + + { + + d = min(d, dist(strip[i], strip[j])); + + } + + } + + return d; + +} + + + +void showVect(vector> &vec) + +{ + + for (pair &p : vec) + + { + + cout << p.first << " " << p.second << "\n"; + + } + + cout << "\n"; + +} + + + +double solve(vector> &sortatX, vector> &sortatY) + +{ + + if (sortatX.size() <= 11) + + { + + return nSquaredSol(sortatX); + + } + + + + pair midPoint = sortatX[sortatX.size() / 2]; + + + + vector> sortatX_st; + + vector> sortatX_dr; + + + + vector> sortatY_st; + + vector> sortatY_dr; + + + + for (int i = 0; i < sortatY.size(); i++) + + { + + if (sortatX[i].first <= midPoint.first) + + { + + sortatX_st.push_back(sortatX[i]); + + } + + else + + { + + sortatX_dr.push_back(sortatX[i]); + + } + + if (sortatY[i].first <= midPoint.first) + + { + + sortatY_st.push_back(sortatY[i]); + + } + + else + + { + + sortatY_dr.push_back(sortatY[i]); + + } + + } + + + + double distSt = solve(sortatX_st, sortatY_st); + + double distDr = solve(sortatX_dr, sortatY_dr); + + + + double d = min(distSt, distDr); + + + + vector> strip; + + for (int i = 0; i < sortatY.size(); i++) + + { + + if (abs(sortatY[i].first - midPoint.first) < d) + + { + + strip.push_back(sortatY[i]); + + } + + } + + + + return min(d, getMinDistFromStrip(strip, d)); + +} + + + +double nLognSol(vector> &coordonate) + +{ + + vector> sortatX(coordonate); + + vector> sortatY(coordonate); + + sort(sortatX.begin(), sortatX.end(), cmpX); + + sort(sortatY.begin(), sortatY.end(), cmpY); + + + + return solve(sortatX, sortatY); + +} + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int main() + +{ + int n; + + fin >> n; + + vector> coordonate(n); + + for (int i = 0; i < n; i++) + + { + + int x, y; + + fin >> x >> y; + + coordonate[i].first = x; + + coordonate[i].second = y; + + } + + + + double solutie = nLognSol(coordonate); + + fout << fixed; + + fout.precision(6); + + fout << solutie; + + + + return 0; + +} diff --git a/cmap/divide_conquer/083268d8-dd47-4290-889d-a3f8ab94b4f4.cpp b/cmap/divide_conquer/083268d8-dd47-4290-889d-a3f8ab94b4f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57230ec9576239890d14d03b7835e0c959ce79ec --- /dev/null +++ b/cmap/divide_conquer/083268d8-dd47-4290-889d-a3f8ab94b4f4.cpp @@ -0,0 +1,206 @@ +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int nmax = 100005; + + + +int n, auxSize; + + + +struct Point{ + + long long x, y; + +}v[nmax], aux[nmax]; + + + +bool cmp(Point a, Point b){ + + return a.x < b.x; + +} + + + +void Merge(int st, int dr){ + + int mid = (st + dr) / 2; + + int i = st, j = mid + 1; + + auxSize = 0; + + while (i <= mid && j <= dr){ + + if (v[i].y < v[j].y){ + + aux[++auxSize] = v[i++]; + + } + + else if (v[j].y < v[i].y){ + + aux[++auxSize] = v[j++]; + + } + + else{ + + if (v[i].x < v[j].x){ + + aux[++auxSize] = v[i++]; + + } + + else{ + + aux[++auxSize] = v[j++]; + + } + + } + + } + + while (i <= mid){ + + aux[++auxSize] = v[i++]; + + } + + while (j <= dr){ + + aux[++auxSize] = v[j++]; + + } + + for (i = st, j = 1; i <= dr; ++i, ++j){ + + v[i] = aux[j]; + + } + +} + + + +long long GetDist(Point a, Point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long GetMinDist(int st, int dr){ + + if (dr - st + 1 <= 3){ + + if (dr - st + 1 == 3){ + + Merge(st + 1, dr); + + Merge(st, dr); + + return min(GetDist(v[st], v[st + 1]), min(GetDist(v[st], v[dr]), GetDist(v[st + 1], v[dr]))); + + } + + else if (dr - st + 1 == 2){ + + Merge(st, dr); + + return GetDist(v[st], v[dr]); + + } + + } + + int mid = (st + dr) / 2; + + int midX = v[mid].x; + + long long left = GetMinDist(st, mid); + + long long right = GetMinDist(mid + 1, dr); + + long long minim = min(left, right); + + Merge(st, dr); + + auxSize = 0; + + for (int i = st; i <= dr; ++i){ + + if (1LL * (midX - v[i].x) * (midX - v[i].x) <= minim){ + + aux[++auxSize] = v[i]; + + } + + } + + for (int i = 1; i <= auxSize; ++i){ + + for (int j = i + 1; j - i + 1 <= 9 && j <= auxSize; ++j){ + + minim = min(1LL * minim, 1LL * GetDist(aux[i], aux[j])); + + } + + } + + return minim; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmp); + + for (int i = 1; i < n; ++i){ + + if (v[i].x == v[i + 1].x && v[i].y == v[i + 1].y){ + + fout << 0; + + return 0; + + } + + } + + long long ans = GetMinDist(1, n); + + fout< + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int N; + +pair X[100010], Y[100010], v[100010]; + + + +inline long long dist(const pair &a, const pair &b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second) ; + +} + + + +inline void interclasare(const int &left, const int &right, const int &middle) + +{ + + int i = left, k = 0; + + int j = middle + 1; + + + + while(i <= middle && j <= right) + + { + + (X[i].first <= X[j].first) ? (Y[++ k] = X[i ++]) : (Y[++ k] = X[j ++]); + + } + + while(i <= middle) + + { + + Y[++ k] = X[i ++]; + + } + + while(j <= right) + + { + + Y[++ k] = X[j ++]; + + } + + for(int i = left; i <= right; i ++) + + { + + X[i] = Y[i - left + 1]; + + } + +} + + + +inline long long closestPoints(int left, int right, pair X[100010]) + +{ + + if(left >= right - 1) + + return 20000000000000000; + + + + if(right - left == 2) + + { + + if(X[left] > X[left + 1]) + + swap(X[left], X[left + 1]); + + + + return dist(X[left], X[left + 1]); + + } + + + + int middle = (left + right) >> 1; + + long long solution = min( closestPoints(left, middle, X), closestPoints(middle, right, X) ); + + + + interclasare(left, middle, right); + + + + int k = 0; + + for(int i = left; i < right; i ++) + + { + + if( abs(X[i].first - X[middle].first) <= solution) + + { + + v[++k] = X[i]; + + } + + } + + + + for(int i = 1; i < k; i ++) + + { + + for(int j = i + 1; j <= k && j - i < 8; j ++) + + { + + solution = min(solution, dist(v[i], v[j])); + + } + + } + + + + return solution; + + + +} + + + +int main() + +{ + + fin >> N; + + + + for(int i = 1; i <= N; i ++) + + { + + fin >> X[i].first >> X[i].second; + + } + + + + sort(X + 1, X + N + 1); + + + + fout << setprecision(6) << fixed << sqrt( closestPoints(0, N, X) ); + + + + return 0; + +} diff --git a/cmap/divide_conquer/0929644f-6275-4f13-b429-7db714d10fbe.cpp b/cmap/divide_conquer/0929644f-6275-4f13-b429-7db714d10fbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a0b4128936e74a8222a0d1289421ffba9da82c5 --- /dev/null +++ b/cmap/divide_conquer/0929644f-6275-4f13-b429-7db714d10fbe.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const int DN=1e5+5; + +int n; + +typedef pair pii; + +pii a[DN],b[DN],r[DN]; + +long long rez; + +double sol; + +long long dist(pii A,pii B) + +{ + + return 1LL*(A.first-B.first)*(A.first-B.first)+1LL*(A.second-B.second)*(A.second-B.second); + +} + +void solve(int st,int dr) + +{ + + if(st==dr) + + return; + + int mij=(st+dr)/2,ls=st,ld=mij+1,pz=st,m=0; + + solve(st,mij); + + solve(mij+1,dr); + + for(int i=st;i<=dr;i++) + + { + + if(ls>mij||(ld<=dr&&a[ld].second>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + rez=numeric_limits::max(); + + solve(1,n); + + sol=rez; + + sol=sqrt(sol); + + fout< +using namespace std; + + + +typedef long long lint; + + + +const lint sqrInf = 1LL * 4000000000000000000LL; + + + +int n; + + + +struct coord + +{ + + int x, y; + +}; + + + +coord v[100000 + 1]; + +coord Y[100000 + 1]; + +coord x[100000 + 1]; + + + +void readFile() + +{ + + ifstream f("cmap.in"); + + + + f >> n; + + + + for(int i = 1; i <= n; i ++) + + f >> v[i].x >> v[i].y; + + + + f.close(); + +} + + + +struct cmpY + +{ + + bool operator() (coord a, coord b) + + { + + if(a.y != b.y) + + return a.y < b.y; + + + + return a.x < b.x; + + } + +}; + + + +lint dist(coord a, coord b) + +{ + + lint dx = a.x - b.x; + + lint dy = a.y - b.y; + + + + return dx * dx + dy * dy; + +} + + + +bool cmp2(coord a, coord b) + +{ + + if(a.y != b.y) + + return a.y < b.y; + + + + return a.x < b.x; + +} + + + +void mergeSort(int st1, int dr1, int st2, int dr2) + +{ + + int cr = 0; + + int i = st1; + + int j = st2; + + while(i <= dr1 && j <= dr2) + + { + + int val = cmp2(Y[i], Y[j]); + + if(val == 1) + + x[++ cr] = Y[i ++]; + + + + else + + x[++ cr] = Y[j ++]; + + } + + + + while(i <= dr1) + + x[++ cr] = Y[i ++]; + + + + while(j <= dr2) + + x[++ cr] = Y[j ++]; + + + + for(i = st1; i <= dr2; i ++) + + Y[i] = x[i - st1 + 1]; + +} + + + +inline int poz(int a) + +{ + + return (a > 0 ? a : (-a)); + +} + + + +lint dmin(int st, int dr) + +{ + + if(st == dr) + + return sqrInf; + + + + if(dr - st + 1 <= 3) + + { + + sort(Y + st, Y + dr + 1, cmpY()); + + + + lint rez = sqrInf; + + int i, j; + + for(i = st; i <= dr; i ++) + + { + + for(j = i + 1; j <= dr; j ++) + + rez = min(rez, dist(Y[i], Y[j])); + + } + + + + return rez; + + } + + + + int mid = ((st + dr) >> 1); + + lint d = min(dmin(st, mid), dmin(mid + 1, dr)); + + + + mergeSort(st, mid, mid + 1, dr); + + int i; + + int k = 0; + + for(i = st; i <= dr; i ++) + + { + + if(poz(Y[i].x - v[mid].x) <= d) + + x[++ k] = Y[i]; + + } + + + + int j; + + for(i = 1; i <= k; i ++) + + { + + for(j = i + 1; j <= i + 5 && j <= k; j ++) + + d = min(d, dist(x[i], x[j])); + + } + + + + return d; + +} + + + +struct cmp + +{ + + bool operator() (coord a, coord b) + + { + + if(a.x != b.x) + + return a.x < b.x; + + + + return a.y < b.y; + + } + +}; + + + +lint rez; + + + +void solve() + +{ + + sort(v + 1, v + n + 1, cmp()); + + + + int i; + + for(i = 1; i <= n; i ++) + + Y[i] = v[i]; + + + + rez = dmin(1, n); + +} + + + +void printFile() + +{ + + ofstream g("cmap.out"); + + + + g << fixed << setprecision(6) << sqrt(rez) << "\n"; + + + + g.close(); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/0b14c511-dc23-43a3-a5df-ebacad4d7671.cpp b/cmap/divide_conquer/0b14c511-dc23-43a3-a5df-ebacad4d7671.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ce56a6d086c29b1a3894c4c54aac0eda9a898db --- /dev/null +++ b/cmap/divide_conquer/0b14c511-dc23-43a3-a5df-ebacad4d7671.cpp @@ -0,0 +1,142 @@ +#include +#include #include +#include +#include +#include + + +using namespace std; + + + + + + + +int N; + +vector > X, Y, V(100005); + + + +long long dist(pair a, pair b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long solve(int st, int dr, vector > &X, vector > &Y) { + + if(st >= dr - 1) { + + return 4e18; + + } + + + + if(dr - st == 2) { + + if(Y[st] > Y[st + 1]) { + + swap(Y[st], Y[st + 1]); + + } + + return dist(X[st], X[st + 1]); + + } + + + + int m = (st + dr) / 2; + + long long bst = min(solve(st, m, X, Y), solve(m, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + m, Y.begin() + m, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int ts = 0; + + for(int i = st; i < dr; ++i) { + + if(abs(Y[i].second - X[m].first) <= bst) { + + V[ts++] = Y[i]; + + } + + } + + + + for(int i = 0; i < ts; ++i) { + + for(int j = i + 1; j < ts && j - i < 8; ++j) { + + bst = min(bst, dist(V[j], V[i])); + + } + + } + + + + return bst; + +} + + + +int main() { + + freopen("cmap.in","r",stdin); + + freopen("cmap.out","w",stdout); + + + + scanf("%d\n", &N); + + + + int x, y; + + for(int i = 1; i <= N; ++i) { + + scanf("%d %d", &x, &y); + + + + X.push_back({x, y}); + + } + + + + sort(X.begin(), X.end()); + + + + for(int z = 0; z < N; ++z) { + + Y.push_back({X[z].second, X[z].first}); + + } + + + + cout << setprecision(6) << fixed << sqrt(1.0 * solve(0, X.size(), X, Y)) << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/0b853f9d-acca-41a0-8ae4-5391b5879303.cpp b/cmap/divide_conquer/0b853f9d-acca-41a0-8ae4-5391b5879303.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a839189e2c4cb45482dfe022e885271138767b7 --- /dev/null +++ b/cmap/divide_conquer/0b853f9d-acca-41a0-8ae4-5391b5879303.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +struct point { + long long x, y; +} P[100002]; + +auto cmp = [](int a, int b) -> bool { return P[a].y < P[b].y; }; + +long long dist2(point a, point b) { + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} + +long long find_min_dist(int a, int b, vector &points_by_y) { + int i, j; + long long min_dist = LONG_LONG_MAX; + + if (b - a + 1 <= 3) { + for (i = a; i <= b; i++) { + for (j = i + 1; j <= b; j++) + min_dist = min(min_dist, dist2(P[i], P[j])); + points_by_y.push_back(i); + } + sort(points_by_y.begin(), points_by_y.end(), cmp); + } else { + + int m = (a + b) >> 1; + vector left, right, median_points; + min_dist = min(find_min_dist(a, m, left), find_min_dist(m + 1, b, right)); + + points_by_y.resize(b - a + 1); + merge(left.begin(), left.end(), right.begin(), right.end(), points_by_y.begin(), cmp); + for (int p : points_by_y) + if ((P[p].x - P[m].x) * (P[p].x - P[m].x) <= min_dist) + median_points.push_back(p); + + for (i = 0; i < median_points.size(); i++) + for (j = 1; j <= 7; j++) + if (i + j < median_points.size()) + min_dist = min(min_dist, dist2(P[median_points[i]], P[median_points[i + j]])); + } + + return min_dist; +} + +int main() { + ifstream fin("cmap.in"); + ofstream fout("cmap.out"); + + int n; + fin >> n; + for (int i = 1; i <= n; i++) + fin >> P[i].x >> P[i].y; + + sort(P + 1, P + n + 1, [](const point &a, const point &b) -> bool { return a.x < b.x; }); + vector v; + fout << fixed << setprecision(6) << sqrt((double) find_min_dist(1, n, v)); + + return 0; +} diff --git a/cmap/divide_conquer/0c7ad05d-5bce-4fce-912c-3d2a407aa59e.cpp b/cmap/divide_conquer/0c7ad05d-5bce-4fce-912c-3d2a407aa59e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a20be9934966e3a46d675fcde4e7499167cbe176 --- /dev/null +++ b/cmap/divide_conquer/0c7ad05d-5bce-4fce-912c-3d2a407aa59e.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int NMAX = 100001; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x, y; + +}; + + + +punct v[NMAX], aux[NMAX]; + +int N; + + + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + + + +inline long long patrat(long long x) + +{ + + return x * x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + return patrat(A.x - B.x) + patrat(A.y - B.y); + +} + + + +long long distMin(int p, int u) + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long dij = dist2(v[i], v[j]); + + if(dmin > dij)dmin = dij; + + } + + return dmin; + +} + + + +long long calcdist(int p, int u) + +{ + + if(u - p < 3) + + return distMin(p, u); + + int m = (p + u) / 2; + + long long d1 = calcdist(p, m); + + long long d2 = calcdist(m, u); + + long long dd = min(d1, d2); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(patrat(v[i].x - v[m].x) < dd) + + aux[++k] = v[i]; + + sort(aux + 1, aux + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && patrat(aux[j].y - aux[i].y) < dd; j++) + + { + + d1 = dist2(aux[i], aux[j]); + + if(dd > d1)dd = d1; + + } + + return dd; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> v[i].x >> v[i].y; + + sort(v + 1, v + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdist(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/0cd3f329-cfbd-465d-83cc-5e513cb5c406.cpp b/cmap/divide_conquer/0cd3f329-cfbd-465d-83cc-5e513cb5c406.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0fc6f8a640fcd5183a2abc9c045b81b7e46f239 --- /dev/null +++ b/cmap/divide_conquer/0cd3f329-cfbd-465d-83cc-5e513cb5c406.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct punct + +{ + + int x, y; + +}v[100010],w[100010]; + + + +int n; + + + +bool cmpx (punct a, punct b) + +{ + + return a.x < b.x; + +} + + + +bool cmpy(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +long long dist ( punct a, punct b) + +{ + + long long difx = 1LL * (a.x - b.x ); + + long long dify = 1LL * ( a.y - b.y ); + + + + return difx * difx + dify * dify ; + +} + + + + + +long long divide ( int st, int dr) + +{ + + if( dr - st == 1) + + return dist( v[st], v[dr] ); + + + + if ( dr-st == 2 ) + + return min(dist(v[st], v[st+1]),min(dist(v[st+1], v[dr]),dist(v[st], v[dr]))); + + + + int m=( st + dr ) / 2; + + long long S1 = divide ( st, m ) ; + + long long S2 = divide ( m+1, dr ) ; + + + + long long d = min( S1, S2); + + + + int i,j,k=0; + + + + long long delta = ceil(sqrt(d)); + + + + for( i = st; i <= dr; i++) + + { + + if( abs( v[i].x - v[m].x ) <= delta ) + + { + + w[k].x = v[i].x; + + w[k].y = v[i].y; + + k++; + + } + + } + + + + sort(w,w+k,cmpy); + + + + for(i = 0; i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++) + + { + + d = min(d,dist(w[i],w[j])); + + } + + return d; + +} + + + +int main() + +{ + + int n; + + f >> n; + + + + for ( int i = 0; i < n ;i++) + + f >> v[i].x >> v[i].y; + + + + sort(v,v+n,cmpx); + + + + g << fixed << setprecision(6) << sqrt(divide(0,n-1)); + + return 0; + +} diff --git a/cmap/divide_conquer/0d41f6d1-cab2-4d12-b01f-bd5d01344733.cpp b/cmap/divide_conquer/0d41f6d1-cab2-4d12-b01f-bd5d01344733.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a65f3a4aac88d0ea2f25a10e029bd5f7fe34d01 --- /dev/null +++ b/cmap/divide_conquer/0d41f6d1-cab2-4d12-b01f-bd5d01344733.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + + +vector > v, w, rez, aux; + +const int INF = 4e18; + + + +long long get_dist(pair a, pair b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long rezolva(int st, int dr) + +{ + + if (st == dr) + + return INF; + + else if (dr - st == 1) + + return get_dist(v[st], v[dr]); + + + + int mid = (st+dr)/2; + + + + long long sol = min(rezolva(st, mid), rezolva(mid, dr)); + + + + rez.clear(); + + rez.resize(dr + 1 - st + 1); + + aux.clear(); + + merge(w.begin() + st, w.begin() + mid, w.begin() + mid, w.begin() + dr + 1, rez.begin()); + + + + vector > ::iterator it; + + + + for (it = rez.begin(); it != rez.end(); ++it) + + { + + if (abs(it->second - v[mid].first) <= sol) + + aux.push_back(make_pair(it->second, it->first)); + + } + + + + for (int i = 0; i < aux.size(); ++i) + + for (int j = i + 1; j < aux.size() && j - i < 8; ++j) { + + sol = min(sol, get_dist(aux[i], aux[j])); + + } + + + + return sol; + +} + + + +int main() + +{ + + int x,y,n; + + + + fin>>n; + + + + for(int i = 0; i < n; i++) + + { + + fin>>x>>y; + + v.push_back(make_pair(x,y)); + + w.push_back(make_pair(y,x)); + + } + + + + sort(v.begin(),v.end()); + + w.push_back(make_pair(INF,INF)); + + sort(w.begin(),w.end()); + + + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +struct point { + + double x, y; + +}; + + + +bool cmpX(point a, point b) { + + if (a.x > b.x) return 1; + + return 0; + +} + + + +bool cmpY(point a, point b) { + + if (a.y > b.y) return 1; + + return 0; + +} + + + +double twoPointsDistance(point a, point b){ + + return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2)); + +} + + + +double minDistance(const vector &pt, int left, int right) { + + + + int separator = (left + right) / 2; + + double delta = 2e9; + + + + if (right - left > 2) + + delta = min (minDistance(pt, left, separator), minDistance(pt, separator + 1, right)); + + + + else + + for(int i = left; i < right; ++i) + + for (int j = i + 1; j <= right; ++j) { + + + + double distance = twoPointsDistance(pt[i], pt[j]); + + if (distance < delta) delta = distance; + + + + } + + + + vector eligiblePoints; + + for (int i = left; i <= right; ++i) + + if (abs(pt[i].x - pt[separator].x) < delta) + + eligiblePoints.push_back(pt[i]); + + + + sort(eligiblePoints.begin(), eligiblePoints.end(), cmpY); + + + + + + for (int i = 0; i < eligiblePoints.size(); ++i) + + for (int j = i + 1; j < eligiblePoints.size() && j <= i + 7; ++j) + + delta = min(delta, twoPointsDistance(eligiblePoints[i], eligiblePoints[j])); + + + + + + eligiblePoints.clear(); + + return delta; + +} + + + + + +int main() + +{ + + int n; + + vector pt; + + + + cin >> n; + + for (int i = 0; i < n; ++i) { + + point x; + + cin >> x.x >> x.y; + + pt.push_back(x); + + } + + sort(pt.begin(), pt.end(), cmpX); + + + + double minimumDistance = minDistance(pt, 0, n - 1); + + + + cout << fixed << setprecision(6) << minimumDistance; + + + +} diff --git a/cmap/divide_conquer/0e30b5ff-9b78-4a5e-bf1a-18e7bf131c37.cpp b/cmap/divide_conquer/0e30b5ff-9b78-4a5e-bf1a-18e7bf131c37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb4bbf15c364f81487d5c25e73f5d3e6d5b2ee0a --- /dev/null +++ b/cmap/divide_conquer/0e30b5ff-9b78-4a5e-bf1a-18e7bf131c37.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + + + +int n; + +vector > V; + + + +void citire(const char* fileName) + +{ + + ifstream in (fileName); + + in >> n; + + int x, y; + + for (int i = 0; i < n; ++i) + + { + + in >> x >> y; + + V.push_back ({x, y}); + + } + + + + in.close(); + +} + + + +long long dist2 (pair p1, pair p2) + +{ + + return (long long) (p1.first - p2.first) * (p1.first - p2.first) + (long long) (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +long long solutie (int st, int dr) + +{ + + if (dr - st == 1) + + return dist2 (V[st], V[dr]); + + + + else if (dr - st == 2) + + return min(dist2(V[st], V[st + 1]), min(dist2(V[st], V[st + 2]), dist2(V[st + 1], V[st + 2]))); + + + + int mij = (st + dr) / 2; + + + + long long d = min(solutie (st, mij), solutie (mij + 1, dr)); + + + + vector> M; + + + + int delta = (int) (ceil (sqrt (d))); + + + + for (int i = st; i <= dr; i++) + + if (abs(V[i].first - V[mij].first) <= delta) + + M.push_back(V[i]); + + + + sort (M.begin(), M.end(), [](pair p1, pair p2) -> bool { + + return p1.second < p2.second; + + }); + + + + for (int i = 0; i < M.size(); i++) + + for (int j = i + 1; j <= i + 7 && j < M.size(); j++) + + d = min(d, dist2(M[i], M[j])); + + + + return d; + +} + + + +int main() + +{ + + citire("cmap.in"); + + + + sort(V.begin(), V.end()); + + + + ofstream out ("cmap.out"); + + out << fixed << setprecision(6) << sqrt (solutie (0, n-1)) << '\n'; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/0e9c2fbb-ff37-4820-882d-f7da089b398b.cpp b/cmap/divide_conquer/0e9c2fbb-ff37-4820-882d-f7da089b398b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b42d1f416ef9cf95de12d4ee95cf7c957e4aa3b --- /dev/null +++ b/cmap/divide_conquer/0e9c2fbb-ff37-4820-882d-f7da089b398b.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +using namespace std; + + + + + +typedef long long i64; + +const int N=100001; + +const i64 I=4e18; + +vector> v,x,y; + +int n,i; + +i64 D(pair &a,pair &b) + +{ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + +i64 G(int s,int d) + +{ + + if(s>=d-1) + + return I; + + if(d-s==2) + + { + + if(y[s]>y[s+1]) + + swap(y[s],y[s+1]); + + return D(x[s],x[s+1]); + + } + + int m=(s+d)/2,e=0,i,j; + + i64 b=min(G(s,m),G(m,d)); + + sort(y.begin()+s,y.begin()+d); + + for(i=s; i +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +long long INF = 4e18; +vector > v, x, y; +int N; + +long long dist(pair a, pair b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long solve(int st, int dr) { + if (st >= dr - 1) return INF; + if (dr - st == 2) { + if (y[st] > y[st + 1]) { + swap(y[st], y[st + 1]); + } + return dist(x[st], x[st + 1]); + } + int mij = (st + dr) / 2; + long long ret = min(solve(st, mij), solve(mij, dr)); + + + int nr = 0; + for (int i=st; i + +using namespace std; + +const long long inf=1e18; +struct punct +{ + int x,y; +}v[100010],v1[100010]; + +int cmp1(punct a,punct b) +{ + return a.x=dr) return inf; + if(st+1==dr) + { + if(cmp2(v[dr],v[st])) swap(v[st],v[dr]); + return dist(v[st],v[dr]); + } + int mid=(st+dr)/2,midx=v[mid].x; + long long sol=min(solve(st,mid),solve(mid+1,dr)); + merge(v+st,v+mid+1,v+mid+1,v+dr+1,v1+st,cmp2); + int nr=0; + for(int i=st;i<=dr;i++) + { + v[i]=v1[i]; + if(sqr(v1[i].x-midx)<=sol) v1[++nr]=v1[i]; + } + for(int i=1;i<=nr;i++) + for(int j=i+1;j<=nr && sqr(v1[i].y-v1[j].y)<=sol;j++) + sol=min(sol,dist(v1[i],v1[j])); + return sol; +} + +int main() +{ + freopen("cmap.in", "r" ,stdin); + freopen("cmap.out", "w", stdout); + int n; + scanf("%d",&n); + for(int i=1;i<=n;i++) scanf("%d%d",&v[i].x,&v[i].y); + sort(v+1,v+1+n,cmp1); + printf("%.9f",sqrt(solve(1,n))); + return 0; +} diff --git a/cmap/divide_conquer/10dc2372-c044-40d3-ad7f-e86d86c1ecdb.cpp b/cmap/divide_conquer/10dc2372-c044-40d3-ad7f-e86d86c1ecdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ae58664433090378cd0fe8cf39b335cdfae9e1f --- /dev/null +++ b/cmap/divide_conquer/10dc2372-c044-40d3-ad7f-e86d86c1ecdb.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + +struct punct{ + + int x,y; + +}; + + + + + + + +int comparX(const punct &A, const punct &B) + +{ + + return A.x &V,vector &W) + +{ + + + + + + if((dr-stg)==1) + + return 2147483647; + + + + else if((dr-stg)==2) + + { + + if (W[stg].y > W[stg + 1].y) + + swap (W[stg], W[stg + 1]); + + return dist (V[0],V[1]); + + } + + + + int mediana; + + double stg_minim,dr_minim, v_minim; + + mediana=(stg+dr)/2; + + stg_minim=distantaMinima(stg,mediana,V,W); + + dr_minim=distantaMinima(mediana,dr,V,W); + + v_minim=min(stg_minim,dr_minim); + + vector aux(dr-stg); + + merge(W.begin()+stg,W.begin()+mediana,W.begin()+mediana,W.begin()+dr,aux.begin(),comparY); + + copy(aux.begin(),aux.begin()+(dr-stg),W.begin()+stg); + + + + vector b; + + for (int i = stg; i < dr; ++i) + + { + + if (abs(W[i].x - V[mediana].x) <= v_minim) + + b.push_back(W[i]); + + } + + + + + + double minim_t = v_minim; + + for (int i = 0; i>n; + + vector V(n),W(n); + + for(int i=0;i>V[i].x>>V[i].y; + + sort(V.begin(),V.end(),comparX); + + W=V; + + fout< +#include +#include +#include +#include + + +using namespace std; + + + + + + + +struct punct + +{ + + double x; + + double y; + +}; + + + +typedef double ull; + + + +punct v[110000],z[110000],w[110000]; + + + +bool cmp(punct a,punct b) + +{ + + return a.xv[l+1].y) + + swap(v[l],v[l+1]); + + if(v[l+1].y>v[l+2].y) + + swap(v[l+1],v[l+2]); + + if(v[l].y>v[l+1].y) + + swap(v[l],v[l+1]); + + return min(distanta(v[l],v[l+1]),min(distanta(v[l+1],v[l+2]),distanta(v[l],v[l+2]))); + + } + + if(r-l+1==2) + + { + + if(v[l].y>v[l+1].y) + + swap(v[l],v[l+1]); + + return distanta(v[l],v[l+1]); + + } + + else + + { + + int m=(l+r)/2; + + double mediana=v[m].x; + + ull delta=min(dei(l,m),dei(m+1,r)); + + int r2=0; + + int rdelta=sqrt(delta); + + int i,j,k; + + i=k=l; + + j=m+1; + + while(i<=m&&j<=r) + + if(v[i].y=-rdelta) + + w[++r2]=v[i]; + + } + + for(i=1;i<=r2;i++) + + { + + j=min(r2,i+7); + + for(k=i+1;k<=j;k++) + + delta=min(delta,distanta(w[i],w[k])); + + } + + return delta; + + } + +} + + + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream o("cmap.out"); + + int n,i; + + f>>n; + + for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,cmp); + + o< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +pair v[100001]; + + + +long long dist(long long x1, long long x2, long long y1, long long y2) + +{ + + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + +} + + + +long long distDouaPuncte(int x, int y) + +{ + + return dist( v[x].first, v[y].first, v[x].second, v[y].second ); + +} + + + +long long DEI(int st, int dr) + +{ + + if( dr - st <=3 ) + + { + + long long d = distDouaPuncte(0,1); + + if( dr - st ==3 ) + + { + + d = min( d, distDouaPuncte(1,2) ); + + d = min( d, distDouaPuncte(0,2) ); + + } + + return d; + + } + + + + long long med = st + (dr - st)/2; + + long long d = min( DEI(st, med), DEI(med, dr) ); + + + + vector > aux; + + + + for(int i=st;i>n; + + for(int i=0;i>v[i].first>>v[i].second; + + + + sort(v, v+n); + + + + g< + + + + + + +using namespace std; + + + +vector > v; + +long long dist(pair a,pair b) + +{ + + long long p1x=a.first; + + long long p1y=a.second; + + long long p2x=b.first; + + long long p2y=b.second; + + return ((p1x-p2x)*(p1x-p2x)+(p1y-p2y)*(p1y-p2y)); + +} + +long long divideEtImpera(long long st,long long dr) + +{ + + long long mij,minL,minR; + + long long d; + + if(dr-st<=3) + + { + + long long minim=1e18; + + unsigned int i,j; + + for(i=st;i<=dr;i++) + + { + + for(j=i+1;j<=dr;j++) + + { + + minim=min(minim,dist(v[i],v[j])); + + } + + } + + return minim; + + } + + unsigned int i,j; + + mij=st+(dr-st)/2; + + d=min(divideEtImpera(st,mij),divideEtImpera(mij+1,dr)); + + vector < pair > new_v; + + for(i=st;i<=dr;i++) + + { + + if(abs(v[mij].first-v[i].first)>n; + + for(i=1;i<=n;i++) + + { + + fin>>a>>b; + + v.push_back({a,b}); + + } + + long long dist_best=1e9; + + sort(v.begin(),v.end()); + + dist_best=divideEtImpera(0,n-1); + + fout< +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + +typedef long double ld; + +const int lim=1e5+3; + +struct point + +{ + + ld x,y; + +}v[lim],k[lim]; + +ld dist(point a,point b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +ld modul(ld a) + +{ + + if(a<0) return -a; + + return a; + +} + +vector vec; + +ld rez(int l,int r) + +{ + + if(r-l==1) + + { + + if(v[r].x>v[l].x) + + k[l]=v[l],k[r]=v[r]; + + else k[l]=v[r],k[r]=v[l]; + + return dist(v[l],v[r]); + + } + + if(r-l==2) + + { + + k[l]=v[l]; + + k[l+1]=v[l+1]; + + k[r]=v[r]; + + if(k[l+1].x<=k[l].x and k[l+1].x<=k[r].x) + + swap(k[l],k[l+1]); + + else if(k[r].x<=k[l].x and k[r].x<=k[l+1].x) + + swap(k[l],k[r]); + + if(k[l+1].x>k[r].x) + + swap(k[l+1],k[r]); + + return min(dist(v[l],v[l+1]),min(dist(v[l+1],v[r]),dist(v[l],v[r]))); + + } + + int sep=(l+r)/2,nr; + + ld d=v[sep+1].y; + + ld ans=min(rez(l,sep),rez(sep+1,r)); + + vec.clear(); + + int ind1=l; + + int ind2=sep+1; + + int poz=l; + + while(ind1<=sep and ind2<=r) + + { + + if(k[ind1].x>n; + + for(int i=1;i<=n;++i) + + cin>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,dupay); + + cout< +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const long long inf=9e18; + + + +const int Max_N=100005; + + + +vector> temp(Max_N); + + + +long long distanta(const pair &p1,const pair &p2) + +{ + + return (long long)(p1.first-p2.first)*(p1.first-p2.first)+(long long)(p1.second-p2.second)*(p1.second-p2.second); + +} + + + +bool ComparX(const pair &p1,const pair &p2) + +{ + + return p1.first> &v,vector> &y,int stg,int dr) + +{ + + if(stg>=dr-1) + + return inf; + + if(dr-stg==2) + + { + + if(y[stg].first>y[stg+1].first) + + swap(y[stg],y[stg+1]); + + return distanta( v[stg],v[stg+1]); + + } + + int mijloc=(stg+dr)/2; + + long long dist,d1,d2,delta; + + int i,j; + + d1=distantaMinima(v,y,mijloc,dr); + + d2=distantaMinima(v,y,stg,mijloc); + + dist=min(d1,d2); + + delta=(int) ceil(sqrt(dist)); + + merge(y.begin()+stg, y.begin()+mijloc, y.begin()+mijloc, y.begin()+dr, temp.begin()); + + copy(temp.begin(), temp.begin() + (dr - stg), y.begin() + stg); + + int temp_size=0; + + for(i=stg;i<=dr;i++) + + if(abs(y[i].second-v[mijloc].first)<=delta) + + temp[temp_size++]=y[i]; + + for(i=0;i > v,z; + + f>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),ComparX); + + + + for(i=0;i +#include +#include + + +const int dmax = 100000; + +unsigned const long long INF = (1ULL << 64) - 1; + + + +struct point + +{ + + long long x,y; + +}; + + + +int n; + +point pointsY[dmax]; + +point pointsL[dmax]; + + + +bool cmpX(point a, point b) + +{ + + if (a.x == b.x) + + return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmpY(point a, point b) + +{ + + if (a.y == b.y) + + return a.x < b.x; + + return a.y < b.y; + +} + + + +double distance(point a, point b) + +{ + + return (b.x - a.x)*(b.x - a.x) + (b.y - a.y)*(b.y - a.y); + +} + + + +double min_dist(int first, int last) + +{ + + double best_distance = INF; + + double dist; + + int point_nr, mid, i, j; + + + + + + point_nr = last - first + 1; + + if (point_nr == 2) + + return distance(pointsY[first],pointsY[last]); + + + + + + mid = first + (last - first)/2; + + best_distance = std::min(min_dist(first,mid), min_dist(mid,last)); + + + + + + + + int u_bound = pointsY[mid].y + (int)sqrt(best_distance); + + int l_bound = pointsY[mid].y - (int)sqrt(best_distance); + + + + int Lsize = 0; + + for (i = first; i<=last; i++) + + { + + if (pointsY[i].y <= u_bound && pointsY[i].y >= l_bound) + + pointsL[Lsize++] = pointsY[i]; + + } + + std::sort(pointsL,pointsL+Lsize,cmpX); + + for (i = 0; i < Lsize - 1; i++) + + { + + j = i + 1; + + int count = 1; + + while (j < Lsize && count < 8) + + { + + dist = distance(pointsL[i],pointsL[j]); + + if (dist < best_distance) + + best_distance = dist; + + count++; + + j++; + + } + + } + + + + return best_distance; + +} + + + +int main() + +{ + + FILE *f; + + int i; + + + + f=fopen("cmap.in","r"); + + fscanf(f,"%d",&n); + + for (i=0; i +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef pair pii; + +pii a[100005]; + +pii r[100005]; + +pii v[100005]; + +int n,ps,pd; + +long long dist(pii A,pii B) + +{ + + return (long long)(A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second); + +} + +long long re(int st,int dr) + +{ + + int m=0; + + if(st==dr) + + return 1000000000000000000000LL; + + int mij=(st+dr)/2; + + long long rez=min(re(st,mij),re(mij+1,dr)); + + ps=st; + + pd=mij+1; + + for(int i=st;i<=dr;i++) + + if((a[ps].seconddr)&&ps<=mij) + + { + + v[i]=a[ps]; + + ps++; + + } + + else + + { + + v[i]=a[pd]; + + pd++; + + } + + for(int i=st;i<=dr;i++) + + a[i]=v[i]; + + for(int i=st;i<=dr;i++) + + if(abs(a[i].first-a[mij].first)<=rez) + + { + + m++; + + r[m]=a[i]; + + } + + for(int i=1;i<=m;i++) + + for(int j=max(1,i-7);j>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + t=re(1,n); + + fout< + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +long long inf = (1LL << 60) ; +const int nmax = 1e5 + 5; + +struct coor +{ + long long x; + long long y; +}; + +long double dis(coor a, coor b) +{ + long double x = a.x - b.x; + long double y = a.y - b.y; + return sqrt(x * x + y * y); +} + +coor point[nmax]; +coor sorted[nmax]; + +long double divide(int i, int j) +{ + if (j - i + 1 <= 3) + { + long double ans = inf; + for (int a = i; a < j; ++ a) + for (int b = a + 1; b <= j; ++ b) + ans = min(ans, dis(point[a], point[b])); + return ans; + } + int mij = (i + j) / 2; + long double ans = min(divide(i, mij), divide(mij + 1, j)); + int line = point[mij].x; + int cnt = 0; + for (int k = i; k <= j; ++ k) + if (abs(line - point[k].x) <= ans) + sorted[++ cnt] = point[k]; + sort(sorted + 1, sorted + 1 + cnt, [] (coor a, coor b) -> bool + { + return a.y < b.y; + }); + for (int a = 1; a < cnt; ++ a) + for (int b = a + 1; b <= cnt && b - a < 8; ++ b) + ans = min(ans, dis(sorted[a], sorted[b])); + return ans; +} + +int main() +{ + int n; + f >> n; + for (int i = 1; i <= n; ++ i) + { + long long x, y; + f >> x >> y; + point[i - 1] = {x, y}; + } + sort(point, point + n, [] (coor a, coor b) -> bool + { + return a.x < b.x; + }); + g << setprecision(6) << fixed << divide(0, n - 1); +} diff --git a/cmap/divide_conquer/1409b012-2a3d-44ac-86c6-357136d588fd.cpp b/cmap/divide_conquer/1409b012-2a3d-44ac-86c6-357136d588fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e8096b004ff58d190221a7f5470406285c736e3 --- /dev/null +++ b/cmap/divide_conquer/1409b012-2a3d-44ac-86c6-357136d588fd.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long long ll; + + + +const ll INF = 4e18; + +const int MAX_N = 1e5 + 5; + + + +int n; + + + +pair v[MAX_N]; + + + +ll sq(ll val) { + + return 1LL * val * val; + +} + + + +ll dist(pair a, pair b) { + + return (1LL * sq(a.first - b.first) + sq(a.second - b.second)); + +} + + + +ll dei(int lo, int ri) { + + int mid; + + ll ans; + + vector > arr; + + ans = INF; + + if (ri - lo + 1 <= 3) { + + for (int i = lo; i <= ri; ++i) { + + arr.push_back(v[i]); + + } + + for (int i = 0; i < arr.size(); ++i) { + + for (int j = i + 1; j < arr.size(); ++j) { + + ans = min(ans, dist(arr[i], arr[j])); + + } + + } + + return ans; + + } + + mid = (lo + ri) / 2; + + ans = min(dei(lo, mid), dei(mid + 1, ri)); + + for (int i = lo; i <= ri; ++i) { + + if (abs(v[i].first - v[mid].first) <= ans) { + + arr.push_back({v[i].second, v[i].first}); + + } + + } + + sort(arr.begin(), arr.end()); + + for (int i = 0; i < arr.size(); ++i) { + + for (int j = i + 1; j < arr.size() && j - i < 10; ++j) { + + ans = min(ans, dist(arr[i], arr[j])); + + } + + } + + return ans; + +} + + + +int main() { + + fin >> n; + + for (int i = 1; i <= n; ++i) { + + fin >> v[i].first >> v[i].second; + + } + + sort(v + 1, v + n + 1); + + fout << fixed << setprecision(6) << 1.0 * sqrt(1.0 * dei(1, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/14162544-0034-4a89-98d8-ad5157c67597.cpp b/cmap/divide_conquer/14162544-0034-4a89-98d8-ad5157c67597.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5bfb3329453b1e2d1cd726d70c23cb0935a45471 --- /dev/null +++ b/cmap/divide_conquer/14162544-0034-4a89-98d8-ad5157c67597.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + +using namespace std; +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +long long n,i,j,li,ls,rez; +struct pnct +{ int x,y; } p[100005]; + +bool cord(pnct x,pnct y) +{ return x.y>n; + for(i=1;i<=n;i++) + fin>>p[i].x>>p[i].y; + + sort(p+1,p+n+1,cabs); + + rez=cmap(1,n); + fout< +#include +#include +#include +#include +#include +#include + + +const int MAXN = 100000 + 5; + + + +using namespace std; + + + +typedef long long ll; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + + + +int n; + +struct punct{ + + ll x,y; + +}v[MAXN],puncte_posibile[MAXN]; + + + +bool cmp_x(punct a,punct b){ + + return a.x < b.x; + +} + +bool cmp_y(punct a,punct b){ + + return a.y > b.y; + +} + +ll dist(punct a,punct b){ + + ll ans = abs(a.x - b.x) * abs(a.x - b.x) + abs(a.y - b.y) * abs(a.y - b.y); + + assert(ans > 0); + + return ans; + +} + + + +ll divide(int l,int r){ + + if(r - l <= 3){ + + ll minim = 2e18; + + for(int i = l; i <= r; i++){ + + for(int j = l; j <= r; j++){ + + if(j != i) + + minim = min(minim,dist(v[i],v[j])); + + } + + } + + + + return minim; + + } + + int mij = (l + r) / 2; + + ll dreapta = divide(l,mij); + + ll stanga = divide(mij + 1,r); + + ll distanta = min(dreapta,stanga); + + + + int index = 0; + + for(int i = l; i <= r; i++) + + if(abs(v[i].x - v[mij].x) <= distanta) + + puncte_posibile[++index] = v[i]; + + sort(puncte_posibile + 1,puncte_posibile + index + 1,cmp_y); + + + + ll distanta_minima = 2e18; + + for(int i = 1; i <= index; i++){ + + for(int j = i + 1; j <= min(index,i + 9); j++){ + + distanta_minima = min(distanta_minima,dist(puncte_posibile[i],puncte_posibile[j])); + + } + + } + + assert(distanta > 0); + + return min(distanta_minima,distanta); + +} + +void testare(){ + + srand(0); + + for(int teste = 1; teste <= 100; teste++){ + + n = rand() % (int)1e2 + 2; + + for(int i = 1; i <= n; i++){ + + v[i].x = rand() % (int)1e9; + + v[i].y = rand() % (int)1e9; + + } + + + + sort(v + 1, v + n + 1,cmp_x); + + ll rezultat = divide(1,n); + + ll distanta_minima = 2e18; + + for(int i = 1; i <= n; i++){ + + for(int j = 1; j <= n; j++){ + + if(j != i) + + distanta_minima = min(distanta_minima,dist(v[i],v[j])); + + } + + } + + assert(distanta_minima == rezultat); + + out<>n; + + for(int i = 1; i <= n; i++){ + + in>>v[i].x>>v[i].y; + + } + + sort(v + 1,v + n + 1,cmp_x); + + ll rezultat = divide(1,n); + + double radical = sqrt(rezultat); + + out< +#include +#include +#include +using namespace std; +int n; +struct punct { + double x, y; +} p[100005], p1[100005]; + +inline bool cmpx(punct A, punct B) { + return A.x < B.x; +} + +inline bool cmpy(punct A, punct B) { + return A.y < B.y; +} + +double dist(punct A, punct B) { + return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); +} + +double Dist(int st, int dr) { + int i, j, ct = 0; + int mid = (st + dr) / 2; + if (st == dr - 1)return dist(p[st], p[dr]); + if(st==dr)return 4000000000; + double drx = (p[mid].x + p[mid + 1].x) / 2; + double D = min(Dist(st, mid), Dist(mid + 1, dr)); + for (i = st; i <= mid; i++) + if (drx - p[i].x < D) { + ct++; + p1[ct] = p[i]; + } + for (i = mid + 1; i <= dr; i++) + if (p[i].x - drx < D) { + ct++; + p1[ct] = p[i]; + } + sort(p1 + 1, p1 + ct + 1, cmpy); + for (i = 1; i <= ct; i++) { + for (j = i + 1; j <= ct; j++) { + if (p1[j].y - p1[i].y >= D)break; + D = min(D, dist(p1[i], p1[j])); + } + } + return D; +} + +int main() { + int i; + ifstream fin("cmap.in"); + ofstream fout("cmap.out"); + fin >> n; + for (i = 1; i <= n; i++) + fin >> p[i].x >> p[i].y; + sort(p + 1, p + n + 1, cmpx); + fout < +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +pair < int, int > temp[100003]; + + + +int partition_x(pair < int, int > P[100003], int lo, int hi){ + + int pivot = lo + (rand() % (hi - lo + 1)); + + (P[0] = (P[pivot]), (P[pivot]) = (P[hi]), (P[hi]) = P[0]); + + pivot = hi; + + while(lo < pivot){ + + if(P[lo].first >= P[pivot].first){ + + (P[0] = (P[lo]), (P[lo]) = (P[pivot - 1]), (P[pivot - 1]) = P[0]); + + (P[0] = (P[pivot - 1]), (P[pivot - 1]) = (P[pivot]), (P[pivot]) = P[0]); + + -- pivot; + + } + + else{ + + ++ lo; + + } + + } + + return pivot; + +} + + + +void qsort_x(pair < int, int > P[100003], int lo, int hi){ + + if(lo >= hi){ + + return; + + } + + int pivot = partition_x(P, lo, hi); + + qsort_x(P, lo, pivot - 1); + + qsort_x(P, pivot + 1, hi); + + return; + +} + + + +bool cmp_y(pair < int, int > a, pair < int, int > b){ + + return a.second < b.second; + +} + + + +long long closest_dist(pair < int, int > P[100003], int lo, int hi){ + + if(hi - lo == 0){ + + return 1e18; + + } + + if(hi - lo == 1){ + + if(P[lo].second > P[hi].second){ + + (P[0] = (P[lo]), (P[lo]) = (P[hi]), (P[hi]) = P[0]); + + } + + return (1ll * (1ll *(P[lo].first - P[hi].first) * (P[lo].first - P[hi].first) + 1ll *(P[lo].second - P[hi].second) * (P[lo].second - P[hi].second))); + + } + + int mid = (lo + hi) / 2, + + xmid = P[mid].first; + + long long left = closest_dist(P, lo, mid), + + right = closest_dist(P, mid + 1, hi), + + d = ((left) < (right) ? (left) : (right)); + + merge(P + lo, P + mid + 1, P + mid + 1, P + hi + 1, temp, cmp_y); + + copy(temp, temp + hi - lo + 1, P + lo); + + int nl = 0, nr = 50000; + + for(int i = 0, n = hi - lo + 1; i < n; i ++){ + + P[lo + i] = temp[i]; + + if((1ll * (xmid - temp[i].first) * (xmid - temp[i].first)) < d){ + + if(xmid - temp[i].first >= 0){ + + temp[nl ++] = temp[i]; + + } + + else{ + + temp[nr ++] = temp[i]; + + } + + } + + } + + int il = 0, ir = 50000; + + while(il < nl && ir < nr){ + + if(temp[il].second < temp[ir].second){ + + for(int i = ir; (1ll * (temp[il].second - temp[i].second) * (temp[il].second - temp[i].second)) < d && i < nr; i ++){ + + d = ((d) < ((1ll * (1ll *(temp[il].first - temp[i].first) * (temp[il].first - temp[i].first) + 1ll *(temp[il].second - temp[i].second) * (temp[il].second - temp[i].second)))) ? (d) : ((1ll * (1ll *(temp[il].first - temp[i].first) * (temp[il].first - temp[i].first) + 1ll *(temp[il].second - temp[i].second) * (temp[il].second - temp[i].second))))); + + } + + ++ il; + + } + + else{ + + for(int i = il; (1ll * (temp[ir].second - temp[i].second) * (temp[ir].second - temp[i].second)) < d && i < nl; i ++){ + + d = ((d) < ((1ll * (1ll *(temp[ir].first - temp[i].first) * (temp[ir].first - temp[i].first) + 1ll *(temp[ir].second - temp[i].second) * (temp[ir].second - temp[i].second)))) ? (d) : ((1ll * (1ll *(temp[ir].first - temp[i].first) * (temp[ir].first - temp[i].first) + 1ll *(temp[ir].second - temp[i].second) * (temp[ir].second - temp[i].second))))); + + } + + ++ ir; + + } + + } + + return d; + +} + + + +int main(){ + + int n; + + pair < int, int > P[100003]; + + fin>>n; + + for(int i = 1; i <= n; i ++){ + + fin>>P[i].first>>P[i].second; + + } + + qsort_x(P, 1, n); + + fout< +#include +#include +#include +#include +#include +using namespace std; + +const int nmax=100005; + +struct punct + +{ + + long long x,y; + +}v[nmax],l[nmax],aux[nmax]; + +bool comp(punct unu,punct doi) + +{ + + if(unu.x==doi.x) return unu.ydr)&&curr1<=m) {aux[i]=v[curr1];curr1++;} + + else {aux[i]=v[curr2];curr2++;} + + } + + nr=0; + + for(i=st;i<=dr;i++) + + { + + v[i]=aux[i]; + + if(abso(v[i].x-v[m].x)<=ret) + + l[++nr]=v[i]; + + } + + for(i=1;i<=nr;i++) + + { + + for(j=i-1;j>=max(i-7,1)&&l[i].y-l[j].y<=ret;j--) + + ret=1LL*min(ret,dist(l[i],l[j])); + + } + + return ret; + +} + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].x>>v[i].y; + + } + + sort(v+1,v+n+1,comp); + + value=divide(1,n); + + value=sqrt(value); + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct Point { + long long x, y; + + friend struct byX; + struct byX { + bool operator() (Point const &p1, Point const &p2) { + return p1.x < p2.x; + } + }; + + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + + + + + +void read(long long &n, vector &points) { + ifstream fin("cmap.in"); + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); +} + + +double distance(const Point &A, const Point &B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + + + + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + double auxDist = distance(points[i], points[j]); + if (minDist > auxDist) { + minDist = auxDist; + } + } + } + + return minDist; +} + + +double divide(int li, int ls, vector &P, vector &Q) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, P); + } + + + int mid = (li + ls)/2; + + + + vector Ql; + Ql.push_back(Q[0]); + vector Qr; + Qr.push_back(Q[0]); + for (int i = 1; i < Q.size(); i++) { + if (Q[i].x <= P[mid].x) { + Ql.push_back(Q[i]); + } else { + Qr.push_back(Q[i]); + } + } + + + double dl = divide(li, mid, P, Ql); + double dr = divide(mid + 1, ls, P, Qr); + double dmin = min(dl, dr); + + + + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = 1; i < Q.size(); i++) { + if (abs(P[mid].x - Q[i].x) <= dmin) { + strip.push_back(Q[i]); + } + } + + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 7 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + + return dmin; +} + + +int main() { + + long long n; + vector P; + + read(n, P); + + vector Q = P; + + sort(P.begin() + 1, P.end(), Point::byX()); + + sort(Q.begin() + 1, Q.end(), Point::byY()); + + + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, P, Q); + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/1799496e-f9b2-466c-8133-59a0562bca8a.cpp b/cmap/divide_conquer/1799496e-f9b2-466c-8133-59a0562bca8a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90f244432a992fa179cb415530a430f1b5a94712 --- /dev/null +++ b/cmap/divide_conquer/1799496e-f9b2-466c-8133-59a0562bca8a.cpp @@ -0,0 +1,91 @@ +#include +#include +#include + + +using namespace std; + +struct point { + int x, y; +}; + +point P[100000]; +int n; + +inline bool compareX(point a, point b) { + return a.x < b.x; +} + +inline bool compareY(point a, point b) { + return a.y < b.y; +} + +double minim(double a, double b) { + if(a < b) return a; + return b; +} + +double dist(point p1, point p2) { + long long a = ((long long)p2.x-p1.x) * ((long long)p2.x-p1.x); + long long b = ((long long)p2.y-p1.y) * ((long long)p2.y-p1.y); + return sqrt(a+b); +} + +double bruteForce(point P[], int n) { + double distMin = 4e15; + double d; + for(int i=0; i +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct { + + int x, y; + +}; + + + +bool cmp(punct &A, punct &B) { + + if (A.x == B.x) + + return A.y < B.y; + + return A.x < B.x; + +} + + + +long long int dist(punct &A, punct &B) { + + return 1ll * (A.x - B.x) * (A.x - B.x) + 1ll *(A.y - B.y) * (A.y - B.y); + +} + + + +void interclaseaza(vector &p, int st, int m, int dr) { + + vector aux; + + int i = st, j = m + 1; + + while (i <= m && j <= dr) { + + if (p[i].y < p[j].y) { + + aux.push_back(p[i]); + + i++; + + } + + else { + + aux.push_back(p[j]); + + j++; + + } + + } + + + + while (i <= m) { + + aux.push_back(p[i]); + + i++; + + } + + while (j <= dr) { + + aux.push_back(p[j]); + + j++; + + } + + + + for (int i = st; i <= dr; i++) { + + p[i] = aux[i - st]; + + } + +} + + + +long long int find_dist(vector &p, int st, int dr) { + + if (dr - st <= 2) { + + long long int d = LLONG_MAX; + + for (int i = st; i <= dr; i++) { + + for (int j = i + 1; j <= dr; j++) { + + d = min(d, dist(p[i], p[j])); + + } + + } + + for (int i = st; i <= dr; i++) { + + for (int j = i + 1; j <= dr; j++) { + + if (p[i].y > p[j].y) + + swap(p[i], p[j]); + + } + + } + + return d; + + } + + + + int m = st + (dr - st) / 2; + + punct pmid = p[m]; + + long long int d1 = find_dist(p, st, m); + + long long int d2 = find_dist(p, m + 1, dr); + + long long int d = min(d1, d2); + + + + + + interclaseaza(p, st, m, dr); + + + + vector banda; + + for (int i = st; i <= dr; i++) { + + if (1ll * (p[i].x - pmid.x) * (p[i].x - pmid.x) < d) { + + banda.push_back(p[i]); + + } + + } + + + + for (unsigned int i = 0; i < banda.size(); i++) { + + for (unsigned int j = i + 1; j < banda.size() && j - i <= 7; j++) { + + d = min(d, dist(banda[i], banda[j])); + + } + + } + + + + return d; + +} + + + +int main() { + + int n; + + in >> n; + + vector p(n); + + for (int i = 0; i < n; i++) { + + in >> p[i].x >> p[i].y; + + } + + + + sort(p.begin(), p.end(), cmp); + + + + out << fixed << setprecision(10) << sqrt(find_dist(p, 0, n - 1)); + + return 0; + +} diff --git a/cmap/divide_conquer/18c3a06b-6c64-40ec-9869-72c4b7a7908a.cpp b/cmap/divide_conquer/18c3a06b-6c64-40ec-9869-72c4b7a7908a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9e750f4650a00eb4981a24dadcd952bcd78b85b --- /dev/null +++ b/cmap/divide_conquer/18c3a06b-6c64-40ec-9869-72c4b7a7908a.cpp @@ -0,0 +1,248 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef long long ll; + +typedef pair < ll, ll > point; + +point points[100001], strip[100001]; + +point aux[100001]; + +int n, n_strip; + + + +void read() { + + + + ifstream in("cmap.in"); + + ll x, y; + + in >> n; + + for (int i = 1; i <= n; ++i) { + + + + in >> x >> y; + + points[i] = make_pair(x, y); + + } + + in.close(); + +} + + + +bool compareX(const point& f, const point& s) { + + + + if ((f.first < s.first) || (f.first == s.first && f.second < s.second)) + + return true; + + return false; + +} + + + +bool compareY(const point& f, const point& s) { + + + + if ((f.second < s.second) || (f.second == s.second && f.first < s.first)) + + return true; + + return false; + +} + + + +inline ll dist(const point& p1, const point& p2) { + + + + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +ll brute_dist(point points[], int left, int right) { + + + + ll best_dist = LLONG_MAX; + + for (int i = left; i < right; i++) + + for (int j = i + 1; j <= right; j++) + + best_dist = min(best_dist, dist(points[i], points[j])); + + + + return best_dist; + +} + + + +ll min_dist_strip(ll d_min) { + + + + ll best_dist = d_min; + + + + for (int i = 1; i < n_strip; i++) + + for (int j = i + 1, k = 1; j <= n_strip && k <= 7; j++, k++) + + best_dist = min(best_dist, dist(strip[i], strip[j])); + + + + return best_dist; + +} + + + +void merge(int x1, int y1, int x2, int y2) { + + + + int k = 0; + + while (x1 <= y1 && x2 <= y2) { + + + + if (compareY(points[x1], points[x2])) + + aux[++k] = points[x1++]; + + else + + aux[++k] = points[x2++]; + + } + + while (x1 <= y1) + + aux[++k] = points[x1++]; + + while (x2 <= y2) + + aux[++k] = points[x2++]; + +} + + + +ll merge_sort(int left, int right) { + + + + if (right - left < 4) { + + + + sort(points + left, points + right + 1, compareY); + + return brute_dist(points, left, right); + + + + } + + + + int mid = left + (right - left) / 2; + + ll mid_x = points[mid].first; + + + + ll d1 = merge_sort(left, mid); + + ll d2 = merge_sort(mid + 1, right); + + ll delta = min(d1, d2); + + + + + + merge(left, mid, mid + 1, right); + + + + for (int i = left; i <= right; ++i) + + points[i] = aux[i - left + 1]; + + + + + + n_strip = 0; + + + + for (int i = left; i <= right; i++) + + if (abs(points[i].first - mid_x) <= delta) + + strip[++n_strip] = points[i]; + + ll d3 = min_dist_strip(delta); + + return min(delta, d3); + +} + + + +int main() { + + + + read(); + + + + sort(points, points + n + 1, compareX); + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(6) << sqrt(merge_sort(1, n)) << "\n"; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/18f8d9f5-2d88-48ae-bc46-6fb8d3354d6b.cpp b/cmap/divide_conquer/18f8d9f5-2d88-48ae-bc46-6fb8d3354d6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fffb8fc00a0003f40d0e7beb727c5e044b70f170 --- /dev/null +++ b/cmap/divide_conquer/18f8d9f5-2d88-48ae-bc46-6fb8d3354d6b.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + +const int kMaxN = 100005; + + + +int n; + +pair p[kMaxN]; + + + +double pointDist(const pair &a, const pair &b) { + + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + + + 1.0 * (a.second - b.second) * (a.second - b.second)); + +} + + + +bool sortOrdinate(const pair &a, const pair &b) { + + if(a.second == b.second) + + return a.first < b.first; + + return a.second < b.second; + +} + + + +pair >> findClosestPair(const int i, const int j) { + + if(i == j) return make_pair(1e16, vector >(1, p[i])); + + const int m = (i + j) / 2; + + pair >> dpLeft = findClosestPair(i, m), dpRight = findClosestPair(m + 1, j); + + double dist = min(dpLeft.first, dpRight.first); + + vector > ySorted(dpLeft.second.size() + dpRight.second.size()); + + merge(dpLeft.second.begin(), dpLeft.second.end(), dpRight.second.begin(), dpRight.second.end(), ySorted.begin(), sortOrdinate); + + vector > strip; + + for (const pair &x: ySorted) + + if(abs(p[m].first - x.first) < dist) + + strip.push_back(x); + + for(int k = 0; k < strip.size(); k++) + + for(int q = 1; k + q < strip.size() && q < 8; q++) + + dist = min(dist, pointDist(strip[k], strip[k + q])); + + return make_pair(dist, ySorted); + +} + + + +int main() { + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + scanf("%d", &n); + + for (int i = 1; i <= n; i++) scanf("%d %d", &p[i].first, &p[i].second); + + sort(p + 1, p + n + 1); + + printf("%f\n", findClosestPair(1, n).first); + + return 0; + +} diff --git a/cmap/divide_conquer/19c4d613-e98c-45f6-87d8-96e578c1560b.cpp b/cmap/divide_conquer/19c4d613-e98c-45f6-87d8-96e578c1560b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d902d023f967f60d20f2ed5e218305e7b832c771 --- /dev/null +++ b/cmap/divide_conquer/19c4d613-e98c-45f6-87d8-96e578c1560b.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +struct punct + +{ + + int x,y; + +}; + + + +long long distanta(punct p1, punct p2) + +{ + + return (long long) (p1.x-p2.x)*(p1.x-p2.x)+ (long long) (p1.y-p2.y)*(p1.y-p2.y); + +} + +bool comparX(punct p1,punct p2) + +{ + + return p1.x &v,int stg,int dr) + +{ + + int mijloc=(stg+dr)/2; + + long long dist,distdr,diststg,delta; + + int i,j; + + vector temp; + + if(dr-stg==1) + + { + + return distanta(v[stg],v[dr]); + + } + + if(dr-stg==2) + + { + + long long dist1,dist2,dist3; + + dist1=distanta(v[stg],v[dr]); + + dist2=distanta(v[stg+1],v[dr]); + + dist3=distanta(v[stg],v[stg+1]); + + return min(dist1,min(dist2,dist3)); + + } + + diststg=DistantaMinima(v,stg,mijloc); + + distdr=DistantaMinima(v,mijloc+1,dr); + + dist=min(diststg,distdr); + + delta=(int) ceil(sqrt(dist)); + + for(i=stg;i<=dr;i++) + + { + + if(abs(v[i].x-v[mijloc].x)<=delta) + + { + + temp.push_back(v[i]); + + } + + } + + sort(temp.begin(),temp.end(),comparY); + + for(i=0; i v; + + punct p; + + f>>n; + + for(i=0;i>p.x>>p.y; + + v.push_back(p); + + } + + sort(v.begin(),v.end(),comparX); + + g< + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +pair X[100010], Y[100010], v[100010], Z[100010]; + +int N; + + + +inline long long dist(const pair &a, const pair &b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second) ; + +} + + + +inline void interclasare(const int &left, const int &middle, const int &right) + +{ + + int i = left, j = middle + 1, k = 0; + + + + while(i <= middle && j <= right) + + { + + (Z[i] <= Z[j]) ? (Y[++ k] = Z[i ++]) : (Y[++ k] = Z[j ++]); + + } + + while(i <= middle) + + Y[++ k] = Z[i ++]; + + + + while(j <= right) + + Y[++ k] = Z[j ++]; + + + + for(int i = left; i <= right; i ++) + + Z[i] = Y[i - left + 1]; + +} + + + +inline long long closestPoints(const int &left, const int &right) + +{ + + if(left >= right) + + return 20000000000000; + + + + if(right - left + 1 == 2) + + { + + if(Z[left] > Z[left + 1]) + + swap(Z[left], Z[left + 1]); + + + + return dist(X[left], X[left + 1]); + + } + + + + int middle = (left + right) >> 1; + + long long solution = min(closestPoints(left, middle), closestPoints(middle, right)); + + interclasare(left, middle, right); + + + + int k = 0; + + + + for(int i = left; i <= right; i ++) + + { + + if( abs(Z[i].second - X[middle].first) <= solution) + + { + + v[++ k] = Z[i]; + + } + + } + + for(int i = 1; i <= k; i ++) + + { + + for(int j = i + 1; j <= k && j - i < 8; j ++) + + { + + solution = min(solution, dist(v[i], v[j])); + + } + + } + + return solution; + +} + + + +int main() + +{ + + fin >> N; + + for(int i = 1; i <= N; i ++) + + { + + fin >> X[i].first >> X[i].second; + + } + + + + sort(X + 1, X + N + 1); + + for(int i = 1; i <= N; i ++) + + { + + Z[i] = make_pair(X[i].second, X[i].first); + + } + + + + fout << setprecision(6) << fixed << sqrt( closestPoints(1, N) ); + + + + + + return 0; + +} diff --git a/cmap/divide_conquer/1b35e280-ef5c-49b3-9975-0df963fbd41b.cpp b/cmap/divide_conquer/1b35e280-ef5c-49b3-9975-0df963fbd41b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95a24a21ebbce20ea1c3e8db1c1845ff0829450f --- /dev/null +++ b/cmap/divide_conquer/1b35e280-ef5c-49b3-9975-0df963fbd41b.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int NMAX = 100001; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x, y; + +}; + + + +punct v[NMAX], Tmp[NMAX]; + +int N; + + + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + + + +inline long long patrat(long long x) + +{ + + return x * x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + return patrat(A.x - B.x) + patrat(A.y - B.y); + +} + + + +long long distMin(int p, int u) + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long dij = dist2(v[i], v[j]); + + if(dmin > dij)dmin = dij; + + } + + return dmin; + +} + + + +long long calcdist(int p, int u) + +{ + + if(u - p < 3) + + return distMin(p, u); + + int m = (p + u) / 2; + + long long d1 = calcdist(p, m); + + long long d2 = calcdist(m, u); + + long long dd = min(d1, d2); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(patrat(v[i].x - v[m].x) < dd) + + Tmp[++k] = v[i]; + + sort(Tmp + 1, Tmp + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && patrat(Tmp[j].y - Tmp[i].y) < dd; j++) + + { + + d1 = dist2(Tmp[i], Tmp[j]); + + if(dd > d1)dd = d1; + + } + + return dd; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> v[i].x >> v[i].y; + + sort(v + 1, v + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdist(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/1bf950d3-bff6-444b-974d-b285707cc49f.cpp b/cmap/divide_conquer/1bf950d3-bff6-444b-974d-b285707cc49f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6608152ab94d72a905191f81b0366196df2e6460 --- /dev/null +++ b/cmap/divide_conquer/1bf950d3-bff6-444b-974d-b285707cc49f.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +pair v[100001]; + + + +long long dist_calc(long long x1, long long x2, long long y1, long long y2) + +{ + + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + +} + + + +long long dist_pair(unsigned int first, unsigned int second) + +{ + + return dist_calc(v[first].first, v[second].first, v[first].second, v[second].second); + +} + + + +long long div_et_imp(unsigned int st, unsigned int fin) + +{ + + if (fin - st <= 3) + + { + + long long d = dist_pair(0, 1); + + if (fin - st == 3) + + { + + d = min(d, dist_pair(1, 2)); + + d = min(d, dist_pair(0, 2)); + + } + + return d; + + } + + + + unsigned long long med = st + (fin - st) / 2; + + + + long long d = min(div_et_imp(st, med), div_et_imp(med, fin)); + + + + vector > p; + + + + for (unsigned int i = st; i < fin; i++) + + { + + p.emplace_back(v[i].second, v[i].first); + + } + + sort(p.begin(), p.end()); + + + + for (unsigned int i = 0; i < p.size(); i++) + + { + + for (unsigned int j = i + 1; j < i + 8 && j < p.size(); j++) + + { + + d = min(d, dist_calc(p[i].first, p[j].first, p[i].second, p[j].second)); + + } + + } + + return d; + +} + + + +int main() { + + int n; + + in >> n; + + for (unsigned int i = 0; i < n; i++) + + { + + in >> v[i].first >> v[i].second; + + } + + sort(v, v + n); + + + + + + + + out << fixed << setprecision(6) << sqrt(div_et_imp(0, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/1c17ae9a-33ca-49bd-aa92-801b2671a131.cpp b/cmap/divide_conquer/1c17ae9a-33ca-49bd-aa92-801b2671a131.cpp new file mode 100644 index 0000000000000000000000000000000000000000..498dbe76b8a8604971b3c1c5d8f22dddbe33eccf --- /dev/null +++ b/cmap/divide_conquer/1c17ae9a-33ca-49bd-aa92-801b2671a131.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +long long distan(pair ee, pair ef) + +{ + + return (long long)(ee.first-ef.first)*(ee.first-ef.first)+(long long)(ee.second-ef.second)*(ee.second-ef.second); + +} + +bool sortX(pair ee,pair ef) + +{ + + return ee.first ee,pair ef) + +{ + + return ee.second > &v,int l,int r) + +{ + + if(r-l==1) + + return distan(v[l],v[r]); + + if(r-l==2) + + { + + long long d1,d2,d3; + + d1=distan(v[l],v[r]); + + d2=distan(v[l+1],v[r]); + + d3=distan(v[l],v[l+1]); + + return min(d1,min(d2,d3)); + + } + + int m=(l+r)/2; + + long long d,d1,d2,dist; + + d1=calcmin(v,m+1,r); + + d2=calcmin(v,l,m); + + d=min(d1,d2); + + dist=(int)ceil(sqrt(d)); + + vector > temp; + + for(int i=l;i<=r;i++) + + if(abs(v[i].first-v[m].first)<=dist) + + temp.push_back(v[i]); + + sort(temp.begin(),temp.end(),sortY); + + for(int i=0;i >v; + + f>>n; + + for(int i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),sortX); + + g< + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct coord + +{ + + long long x,y; + +}; + + + +coord t[100001],v[100001]; + +int i,j,m,n,x,y; + + + +bool funct(coord a, coord b) + +{ + + if (a.x>b.x) return 0; + + if (b.x>a.x) return 1; + + if (a.y>b.y) return 0; + + return 1; + +} + + + +bool compy(coord a,coord b) + +{ + + if (a.y>b.y) return 0; + + return 1; + +} + + + +double dist(coord a, coord b) + +{ + + double x=(a.x-b.x)*(a.x-b.x),y=(a.y-b.y)*(a.y-b.y); + + return sqrt(x+y); + +} + + + +double ds(int a,int b) + +{ + + double s,x,y,z; + + if (b-a<3) + + { + + if (b-a==1) return dist(t[a],t[b]); + + + + x=sqrt(1LL*(t[a].x-t[b].x)*(t[a].x-t[b].x)+(t[a].y-t[b].y)*(t[a].y-t[b].y)); + + y=dist(t[a],t[a+1]); + + z=dist(t[a+1],t[b]); + + return min(x,min(y,z)); + + } + + int mid=(a+b)/2,poz=0; + + x=ds(a,mid); + + y=ds(mid,b); + + s=min(x,y); + + for (i=a;i<=b;i++) + + if (abs(t[i].x-t[mid].x)>n; + + for (i=1;i<=n;i++) + + fin>>t[i].x>>t[i].y; + + sort(t+1,t+n+1,funct); + + + + + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + +struct pct + +{ + + long int x,y; + +}; + +int cmp1(const void *a,const void *b) + +{ + + pct x=*(pct*)a,y=*(pct*)b; + + if(x.x!=y.x) + + return x.x-y.x; + + return x.y-y.y; + +} + +int cmp2(const void *a,const void *b) + +{ + + pct x=*(pct*)a,y=*(pct*)b; + + if(x.y!=y.y) + + return x.y-y.y; + + return x.x-y.x; + +} + +double dist(pct a,pct b) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + +double minim(double a,double b) + +{ + + if(a &v) + +{ + + double mini; + + if(v.size()<=3) + + { + + mini=dist(v[0],v[1]); + + if(v.size()==3) + + { + + mini=minim(mini,dist(v[0],v[1])); + + mini=minim(mini,dist(v[1],v[2])); + + } + + return mini; + + } + + vector vst,vdr; + + vector vmd; + + long int i,j; + + for(i=0;i=0 && mid-vst[i].x<=mini;i--) + + { + + pct aux; + + aux=vst[i]; + + vmd.push_back(aux); + + } + + for(i=0;i v; + + f>>n; + + for(i=0;i>aux.x>>aux.y; + + v.push_back(aux); + + } + + qsort(v.data(),n,sizeof(pct),cmp1); + + g< +#include + + +using namespace __gnu_pbds; +using namespace std; + + + + + + + +typedef pair< int, int > pii; +typedef pair< long long, long long > pll; +typedef long long ll; +typedef unsigned long long ull; +typedef vector< int > vi; +typedef vector< vi > vvi; +typedef vector< ll > vll; +typedef vector< vll > vvll; +typedef vector< pii > vpii; +typedef vector< vpii > vvpii; +typedef vector< pll > vpll; +typedef long double ld; +typedef vector< ld > vld; + +const ll MOD2 = 1e9 + 7; +const int MOD = 1e9 + 7; + +const ull infull = numeric_limits::max(); + +void fix(int &x, ll MOD) { + x = (x % MOD); + if(x < 0) x += MOD; + return; +} + +ll lgput(ll a, ll b, ll MOD) { + ll ret = 1; + a %= MOD; + while(b) { + if(b&1) ret = ret*a % MOD; + a = a*a % MOD; + b >>= 1; + } + + return ret; +} + +ll inv(ll a, ll MOD) { + return lgput(a, MOD-2, MOD); +} + +struct f { + int a, b; + f(int _a = 0, int _b = 0) : a(_a) ,b(_b) {} + ll eval(int x) { + return 1ll*x*a + b; + } +}; + +int binarySearch(int x, vi &v) { + int pos = 0; + int l = 1, r = (int)((v).size()); + while(l <= r) { + int m = l + r >> 1; + if(v[m] <= x) { + pos = m; + l = m + 1; + } else { + r = m - 1; + } + } + + return pos; +} + +struct pt { + int x, y, id; +}; + +struct cmp_x { + bool operator()(const pt & a, const pt & b) const { + return a.x < b.x || (a.x == b.x && a.y < b.y); + } +}; + +struct cmp_y { + bool operator()(const pt & a, const pt & b) const { + return a.y < b.y; + } +}; + +int n; +vector a; + +double mindist; +pair best_pair; + +void upd_ans(const pt & a, const pt & b) { + double dist = sqrt(1ll*(a.x - b.x)*(a.x - b.x) + 1ll*(a.y - b.y)*(a.y - b.y)); + if (dist < mindist) { + mindist = dist; + best_pair = {a.id, b.id}; + } +} +vector t; + +void rec(int l, int r) { + if (r - l <= 3) { + for (int i = l; i < r; ++i) { + for (int j = i + 1; j < r; ++j) { + upd_ans(a[i], a[j]); + } + } + sort(a.begin() + l, a.begin() + r, cmp_y()); + return; + } + + int m = (l + r) >> 1; + int midx = a[m].x; + rec(l, m); + rec(m, r); + + merge(a.begin() + l, a.begin() + m, a.begin() + m, a.begin() + r, t.begin(), cmp_y()); + copy(t.begin(), t.begin() + r - l, a.begin() + l); + + int tsz = 0; + for (int i = l; i < r; ++i) { + if (abs(a[i].x - midx) < mindist) { + for (int j = tsz - 1; j >= 0 && a[i].y - t[j].y < mindist; --j) + upd_ans(a[i], t[j]); + t[tsz++] = a[i]; + } + } +} + +int main() { + + + + + {freopen("cmap"".in","r",stdin);freopen("cmap"".out","w",stdout);}; + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + cout.precision(12); + srand(time(NULL)); + + cin >> n; + a.resize(n); + + for(int i = 0; i < n; ++i) { + cin >> a[i].x >> a[i].y; + } + + t.resize(n); + sort(a.begin(), a.end(), cmp_x()); + mindist = 1E20; + rec(0, n); + + cout << setprecision(12) << fixed << mindist << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/1cb46c86-8358-41bb-832d-af4f7f7229cd.cpp b/cmap/divide_conquer/1cb46c86-8358-41bb-832d-af4f7f7229cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c25a4338f701231c1604579820e2e910478ef3f5 --- /dev/null +++ b/cmap/divide_conquer/1cb46c86-8358-41bb-832d-af4f7f7229cd.cpp @@ -0,0 +1,128 @@ +#include +typedef std::pair punct; + + + +int N; + +std::vector > X, Y, V(100005); + + + +std::ifstream f("cmap.in"); + +std::ofstream g("cmap.out"); + + + +long long dist2(punct A, punct B) { + + return (A.first - B.first)*(A.first - B.first) + (A.second - B.second)*(A.second - B.second); + +} + + + +long long divetimp(int st=0, int dr=X.size(), std::vector &X=X, std::vector &Y=Y) { + + if(st>=dr - 1) + + return 4e18; + + if(dr-st == 2) { + + if(Y[st] > Y[st+1]) + + std::swap(Y[st], Y[st+1]); + + return dist2(X[st], X[st+1]); + + } + + + + int m = (st+dr)/2; + + long long min = std::min(divetimp(st, m, X, Y), divetimp(m, dr, X, Y)); + + + + std::merge(Y.begin()+st, Y.begin()+m, Y.begin()+m, Y.begin()+dr, V.begin()); + + std::copy(V.begin(), V.begin() + (dr-st), Y.begin()+st); + + + + int ts = 0; + + for (int i=st; i> N; + + int second, first; + + for (int i=1; i<=N; i++) { + + f >> second >> first; + + X.push_back({second, first}); + + } + +} + +void rezolvare() { + + std::sort(X.begin(), X.end()); + + for (int i=0; i + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int nmax = 100005; + + + +int n, auxSize; + + + +struct Point{ + + long long x, y; + +}v[nmax], aux[nmax]; + + + +bool cmp(Point a, Point b){ + + if (a.x == b.x){ + + return a.y > b.y; + + } + + return a.x < b.x; + +} + + + +void Merge(int st, int dr){ + + int mid = (st + dr) / 2; + + int i = st, j = mid + 1; + + auxSize = 0; + + while (i <= mid && j <= dr){ + + if (v[i].y < v[j].y){ + + aux[++auxSize] = v[i++]; + + } + + else{ + + aux[++auxSize] = v[j++]; + + } + + } + + while (i <= mid){ + + aux[++auxSize] = v[i++]; + + } + + while (j <= dr){ + + aux[++auxSize] = v[j++]; + + } + + for (i = st, j = 1; i <= dr; ++i, ++j){ + + v[i] = aux[j]; + + } + +} + + + +long long GetDist(Point a, Point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long GetMinDist(int st, int dr){ + + if (dr - st + 1 <= 3){ + + if (dr - st + 1 == 3){ + + Merge(st + 1, dr); + + Merge(st, dr); + + return min(GetDist(v[st], v[st + 1]), min(GetDist(v[st], v[dr]), GetDist(v[st + 1], v[dr]))); + + } + + else if (dr - st + 1 == 2){ + + Merge(st, dr); + + return GetDist(v[st], v[dr]); + + } + + } + + int mid = (st + dr) / 2; + + int midX = v[mid].x; + + long long left = GetMinDist(st, mid); + + long long right = GetMinDist(mid + 1, dr); + + long long minim = min(left, right); + + Merge(st, dr); + + auxSize = 0; + + for (int i = st; i <= dr; ++i){ + + if (1LL * (midX - v[i].x) * (midX - v[i].x) <= minim){ + + aux[++auxSize] = v[i]; + + } + + } + + for (int i = 1; i <= auxSize; ++i){ + + for (int j = i + 1; j - i + 1 <= 9 && j <= auxSize; ++j){ + + minim = min(1LL * minim, 1LL * GetDist(aux[i], aux[j])); + + } + + } + + return minim; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmp); + + for (int i = 1; i < n; ++i){ + + if (v[i].x == v[i + 1].x && v[i].y == v[i + 1].y){ + + fout << 0; + + return 0; + + } + + } + + long long ans = GetMinDist(1, n); + + fout< +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const int MAXN = 1e5 + 1; +const double INF = 1e12; +const double EPS = 1e-8; +double minim = INF; +struct punct{ + double x, y; +}; +bool cmp(punct a, punct b){ + return a.x + EPS < b.x or (abs(a.x - b.x) <= EPS and a.y + EPS < b.y); +} +bool cmp2(punct a, punct b){ + return a.y + EPS < b.y or (abs(a.y - b.y) <= EPS and a.x + EPS < b.x); +} +double form(punct a, punct b){ + return sqrt((((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)))); +} + +punct v[MAXN]; +punct b[MAXN]; + +void di(int st, int dr){ + int m = (st + dr) / 2; + if(dr - st <= 2){ + for(int i = st ; i <= dr - 1; ++i){ + for(int j = i + 1; j <= dr; ++j){ + minim = min(minim, form(v[i], v[j])); + } + } + return; + } + double middleX = v[m].x; + di(st, m); di(m + 1, dr); + int indice = 0; + for(int i = st; i <= dr; ++i) + if(abs(v[i].x - middleX) <= minim) + b[++indice] = v[i]; + sort(b + 1, b + indice + 1, cmp2); + for(int i = 1; i <= indice; ++i) + for(int j = i + 1; j <= min(indice, i + 8) ; ++j) + minim = min(minim, form(b[i], b[j])); +} + +int main(){ + + int n ; + fin >> n; + for(int i = 1; i <= n ;++i) + fin >> v[i].x >> v[i].y; + sort(v + 1, v + n + 1, cmp); + di(1, n); + fout << setprecision(6) << fixed << minim; + + return 0; +} diff --git a/cmap/divide_conquer/2011b290-c421-421b-b270-dba2c3ae7eaf.cpp b/cmap/divide_conquer/2011b290-c421-421b-b270-dba2c3ae7eaf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6952026aa606fcdc164164996def84a1ed2d2f71 --- /dev/null +++ b/cmap/divide_conquer/2011b290-c421-421b-b270-dba2c3ae7eaf.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include + + + +struct Point +{ + long long x, y; +}; + + + + + +long double distanta(Point p1, Point p2) +{ + long long x = (p1.x - p2.x), y = (p1.y - p2.y); + x = x *x; y = y*y; + return sqrt( x + y); +} + + +long double bruteForce(Point P[], long long n) +{ + long double min = LDBL_MAX; + for (long long i = 0; i < n; ++i) + for (long long j = i+1; j < n; ++j) + if (distanta(P[i], P[j]) < min) + min = distanta(P[i], P[j]); + return min; +} + + +long double min(long double x, long double y) +{ + return (x < y)? x : y; +} + + +int compareX(const void* a, const void* b) +{ + Point *p1 = (Point *)a, *p2 = (Point *)b; + return (p1->x - p2->x); +} + +int compareY(const void* a, const void* b) +{ + Point *p1 = (Point *)a, *p2 = (Point *)b; + return (p1->y - p2->y); +} + +long double rezolvaMediana(Point aux[], long long size, long double d) +{ + long double min = d; + qsort(aux, size, sizeof(Point), compareY); + + + for (long long i = 0; i < size; ++i) + for (long long j = i+1; j < size && (aux[j].y - aux[i].y) < min; ++j) + if (distanta(aux[i],aux[j]) < min) + min = distanta(aux[i], aux[j]); + + return min; +} + + +long double partitionare(Point P[], long long n) +{ + + if (n <= 3) + return bruteForce(P, n); + + long long mid = n/2; + Point midPoint = P[mid]; + + long double solLeft = partitionare(P, mid), solRight = partitionare(P + mid, n-mid), sol; + sol = min(solLeft, solRight); + + + Point aux[n]; + long long j = 0; + for (long long i = 0; i < n; i++) + if (abs(P[i].x - midPoint.x) < sol) + aux[j] = P[i], j++; + + + return min(sol, rezolvaMediana(aux, j, sol) ); +} + + +long double rezolva(Point P[], int n) +{ + qsort(P, n, sizeof(Point), compareX); + + + return partitionare(P, n); +} + + +int main() +{ + Point *P; + int n ; + FILE *fin=fopen("cmap.in","r"); + FILE *fout=fopen("cmap.out","w"); + fscanf(fin , "%d",&n); + P = (Point*) calloc(n, sizeof(Point)); + long long i; + for(i=0;i +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +long double mn; + +pair v[100001]; + +vector > A,B; + + + +bool comp(pair a, pair b) + +{ + + return a.first a, pair b) + +{ + + if(a==b) + + g<<"NU"; + + return sqrt(((long double)(a.first-b.first) * (a.first-b.first)) + ((long double)(a.second-b.second) * (a.second-b.second))); + +} + + + +bool comp2(pair a,pair b) + +{ + + return a.second=v[(a+b)/2].first - mn && i>=a;i--) + + A.push_back(v[i]); + + sort(A.begin(),A.end(),comp2); + + + + for (int i=(a+b)/2+1;v[i].first<=v[(a+b)/2+1].first + mn && i<=b;i++) + + B.push_back(v[i]); + + sort(B.begin(),B.end(),comp2); + + + + int i1 = 0,i2 = 0; + + while (i1>n; + + for (int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + v[i] = make_pair(x,y); + + } + + + + sort(v+1,v+n+1,comp); + + + + long double x = divide(1,n); + + + + g< +using namespace std; + +struct dd + +{ + + double x,y; + +}; + +bool cmp(dd a,dd b) + +{ + + if(a.x==b.x)return a.y + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +typedef long long int ll; + +const ll inf = 4e18; + +vector > v,x; + +int n; + + + +bool cmp(const pair &X, const pair &Y){ + + return X.second < Y.second; + +} + + + +inline ll pow(ll X){ + + return X * X; + +} + + + +inline ll modul(ll X){ + + return max(X, -X); + +} + + + +inline ll distance(const pair &X, const pair &Y){ + + return pow(X.first - Y.first) + pow(X.second - Y.second); + +} + + + +ll solve(int lo, int hi){ + + int i,j; + + if(hi - lo <= 3){ + + ll dist = inf; + + for(i = lo ; i < hi ; i++) + + for(j = i + 1 ; j < hi ; j++) + + dist = min(dist, distance(v[i], v[j])); + + return dist; + + } + + int mid = (lo + hi) / 2; + + ll dist = min(solve(lo,mid), solve(mid, hi)); + + + + int k = 0; + + for(i = lo ; i < hi ; i++) + + if(modul(v[mid].first - v[i].first) < dist) + + x[k++] = v[i]; + + + + sort(x.begin(), x.begin() + k, cmp); + + + + for(i = 0 ; i < k ; i++) + + for(j = i + 1 ; j < k && j - i < 8; j++) + + dist = min(dist, distance(x[i], x[j])); + + return dist; + +} + + + +int main(){ + + int i; + + f >> n; + + x.resize(n); + + v.resize(n); + + + + for(i = 0 ; i < n ; i++) + + f >> v[i].first >> v[i].second; + + + + sort(v.begin(), v.end()); + + + + g << fixed << setprecision(21) << sqrt((long double)solve(0,n)); + + + + + + return 0; + +} diff --git a/cmap/divide_conquer/2246e149-00f3-40f6-a522-95f61fed291a.cpp b/cmap/divide_conquer/2246e149-00f3-40f6-a522-95f61fed291a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff78907698b4c8fa6b0bf95d67e526db46cf478b --- /dev/null +++ b/cmap/divide_conquer/2246e149-00f3-40f6-a522-95f61fed291a.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + + + +const long long inf=4e18; + + + +int n; + +vector > x; + +long long calc(const pair a,const pair b){ + + return ((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second)); + +} + + + +long long solve(int st,int dr) + +{ + + if(dr-st<=3){ + + long long ans=inf; + + for (int i=st;i > aux; + + for(int i=st;i<=dr;++i) + + if(best>=abs(x[i].first-x[mij].first)) + + aux.push_back(x[i]); + + int counter=aux.size(); + + sort(aux.begin(),aux.end(), [](auto &left, auto &right) { + + return left.second < right.second; + +}); + + for(int i=0;i>n; + + x.resize(n+1); + + for(i=1;i<=n;i++) + + cin>>x[i].first>>x[i].second; + + sort(x.begin(),x.end()); + + cout< + + + + +using namespace std; + +typedef long long llong; + + + +struct Punct + +{ + + int x, y; + +} px[100005], tmp[100005]; + + + +bool cmpx(const Punct& a, const Punct& b) + +{ + + return a.x < b.x; + +} + + + +bool cmpy(const Punct& a, const Punct& b) + +{ + + return a.y < b.y; + +} + + + +llong dist2(const Punct& a, const Punct& b) + +{ + + llong dx = a.x - b.x; + + llong dy = a.y - b.y; + + return dx * dx + dy * dy; + +} + + + +llong calc(int p, int q) + +{ + + llong res = 0x7fffffffffffffffLL; + + for(int i = p; i < q; i++) + + { + + for(int j = i + 1; j <= q; j++) + + { + + llong aux = dist2(px[i], px[j]); + + if(aux < res) + + res = aux; + + } + + } + + return res; + +} + + + +inline llong sqr(int x) + +{ + + return (llong)x * x; + +} + + + +llong solve(int p, int q) + +{ + + if(q - p < 3) + + return calc(p, q); + + int m = (p + q) / 2; + + llong d1 = solve(p, m); + + llong d2 = solve(m, q); + + llong dd = min(d1, d2); + + + + int k = 0; + + for(int i = p; i <= q; i++) + + { + + if(sqr(px[i].x - px[m].x) < dd) + + tmp[k++] = px[i]; + + } + + sort(tmp, tmp + k, cmpy); + + for(int i = 0; i < k; i++) + + { + + for(int j = i + 1; sqr(tmp[j].y - tmp[i].y) < dd && j < k; j++) + + { + + llong dist = dist2(tmp[i], tmp[j]); + + if(dd > dist) + + dd = dist; + + } + + } + + return dd; + +} + + + +int main() + +{ + + int n, i; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> n; + + for(i = 0; i < n; i++) + + { + + fin >> px[i].x >> px[i].y; + + } + + sort(px, px + n, cmpx); + + llong res = solve(0, n - 1); + + fout << fixed << setprecision(6) << sqrtl(res); + + return 0; + +} diff --git a/cmap/divide_conquer/24bc08fe-5436-4a71-8b28-47eec68ceab8.cpp b/cmap/divide_conquer/24bc08fe-5436-4a71-8b28-47eec68ceab8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ebce1052467d2feaab25e9f6a56500fab56b107 --- /dev/null +++ b/cmap/divide_conquer/24bc08fe-5436-4a71-8b28-47eec68ceab8.cpp @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int NMax = 1e5 + 5; + +typedef long long ll; + +const ll inf = 1e18; + + + +int N; + + + +struct elem { + + int x,y; + +}v[NMax],aux[NMax]; + + + +bool operator <(elem a,elem b) { + + return a.y < b.y; + +} + + + +bool cmp1(elem,elem); + +ll dist(elem,elem); + +ll solve(int,int); +int main() + +{ + + in>>N; + + for (int i=1;i<=N;++i) { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+N+1,cmp1); + + + + double mn = sqrt(solve(1,N)); + + out<>1; + + int abscisa = v[mij].x; + + ll mn = min(solve(st,mij),solve(mij+1,dr)); + + + + + + + + int nrAux = 0; + + int i = st,j = mij + 1; + + while (i <= mij && j <= dr) { + + if (v[i] < v[j]) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + + + for (i=1;i<=nrAux;++i) { + + v[st+i-1] = aux[i]; + + } + + + + + + + + nrAux = 0; + + for (i=st;i<=dr;++i) { + + if (abs(v[i].x-abscisa) <= mn) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (i=1;i<=nrAux;++i) { + + for (j=i+1;j <= nrAux && j-i+1 <= 8;++j) { + + ll d = dist(aux[i],aux[j]); + + mn = min(mn,d); + + } + + } + + return mn; + +} + + + +bool cmp1(elem a,elem b) { + + return a.x < b.x; + +} + + + +ll dist(elem a,elem b) { + + ll val = 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + + return val; + +} diff --git a/cmap/divide_conquer/24f75cec-507d-47dd-9047-557841d07caa.cpp b/cmap/divide_conquer/24f75cec-507d-47dd-9047-557841d07caa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12182d6a64c75f11f152713d4688b16368047e7c --- /dev/null +++ b/cmap/divide_conquer/24f75cec-507d-47dd-9047-557841d07caa.cpp @@ -0,0 +1,143 @@ +#include + + + + +using namespace std; +vector > P; + +int n; + + + +long long calculDist(pair a, pair b) + +{ + + return (long long) (a.first-b.first) * (a.first-b.first)+ (long long)(a.second - b.second) * (a.second - b.second); + +} + + + +bool cmp(pair a, pairb) + +{ + + return a.second < b.second; + +} + + + +long long DI(int st, int dr) + +{ + + if(dr - st == 1) + + return calculDist(P[st],P[dr]); + + else if(dr - st == 2) + + { + + long long d1 = calculDist(P[st],P[st+1]); + + long long minim = d1; + + long long d2 = calculDist(P[st],P[st+2]); + + if(d2 < minim ) + + minim = d2; + + long long d3 = calculDist(P[st+1],P[st+2]); + + if(d3 < minim) + + minim = d3; + + return minim; + + } + + + + int m = (st + dr)/2,i,j; + + long long dist = min(DI(st,m),DI(m+1,dr)); + + int delta = (int)(ceil(sqrt(dist))); + + + + vector> verif; + + for(i = st; i <= dr; i++) + + if(abs(P[i].first - P[m].first) <= delta) + + verif.push_back(P[i]); + + sort(verif.begin(),verif.end(),cmp); + + + + for(unsigned i = 0; i < verif.size(); i++) + + for(unsigned j = i+1; j<= i + 7 && j< verif.size(); j++) + + dist = min(dist, calculDist(verif[i],verif[j])); + + return dist; + + + + + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int x,y,i; + + fin>>n; + + + + for(i = 0; i < n; i++) + + { + + fin>>x>>y; + + P.push_back({x,y}); + + } + + + + sort(P.begin(),P.end()); + + + + fout< +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef pair pii; + +pii a[100005]; + +pii r[100005]; + +pii v[100005]; + +int n,ps,pd; + +long long dist(pii A,pii B) + +{ + + return (long long)(A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second); + +} + +long long re(int st,int dr) + +{ + + if(st==dr) + + return 1000000000000000000000LL; + + int m=0; + + int mij=(st+dr)/2; + + long long rez=min(re(st,mij),re(mij+1,dr)); + + ps=st; + + pd=mij+1; + + for(int i=st;i<=dr;i++) + + if(ps<=mij&&(pd>dr||a[ps].second>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + t=re(1,n); + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int N=100001; + +const int MAX=2000000001; + +pair p[N]; + +pair a[N]; + +int n; + + + +double dist(long long x1,long long y1,long long x2,long long y2) + +{ + + return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); + +} + + + +double divide(int st,int dr) + +{ + + if(dr-st<=2) + + { + + double d=MAX; + + for(int i=st;i=p[mij].first-dmin&&p[i].first<=dmin+p[mij].first) + + { + + a[++top]=make_pair(p[i].second,p[i].first); + + } + + } + + sort(a+1,a+top+1); + + for(int i=1;i>n; + + for(int i=1;i<=n;i++) + + { + + fin>>p[i].first>>p[i].second; + + } + + sort(p+1,p+n+1); + + fout< +#include +#include +#include +using namespace std; + + + + + +vector> v,x,y; +int n,i,q=-1; + + +char u[20000000]; + +inline long long A() + +{ + + long long s=0,b=1; + + q++; + + if(u[q]=='-') + + b=-1,q++; + + for(;u[q]>='0'&&u[q]<='9';q++) + + s=s*10+u[q]-'0'; + + return s*b; + +} +inline long long G(int s,int d) +{ + if(d-s<2) + return 4e18; + if(d-s==2) + { + if(y[s]>y[s+1]) + swap(y[s],y[s+1]); + return ((x[s].first-x[s+1].first)*(x[s].first-x[s+1].first)+(x[s].second-x[s+1].second)*(x[s].second-x[s+1].second)); + } + int m=(s+d)/2,e=0,i,j; + long long b=min(G(s,m),G(m,d)); + for(sort(y.begin()+s,y.begin()+d),i=s;i +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 100005; + +const long long INFINIT = 4e18; + + + +vector < pair > verticalSection(MAX_N), PointsOX, PointsOY; + + + +long long distEuclid(const pair & a, const pair & b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long retClosPoint(int left, int right) + +{ + + if (left >= right - 1) + + return INFINIT; + + else + + if (right - left == 2) + + { + + if (PointsOY[left] > PointsOY[left + 1]) + + swap(PointsOY[left], PointsOY[left + 1]); + + return distEuclid(PointsOX[left], PointsOX[left + 1]); + + } + + + + int mid = (left + right) / 2; + + long long dist = min(retClosPoint(left, mid), retClosPoint(mid, right)); + + + + merge(PointsOY.begin() + left, PointsOY.begin() + mid, PointsOY.begin() + mid, PointsOY.begin() + right, verticalSection.begin()); + + copy(verticalSection.begin(), verticalSection.begin() + (right - left), PointsOY.begin() + left); + + + + int vSize = 0; + + for (int i = left; i < right; i++ ) + + if (abs(PointsOY[i].second - PointsOX[mid].first) <= dist) + + verticalSection[vSize ++] = PointsOY[i]; + + + + for (int i = 0; i < vSize - 1; i ++) + + { + + for (int j = i + 1; j < vSize && j - i < 8; j ++) + + dist = min(dist, distEuclid(verticalSection[i], verticalSection[j])); + + } + + return dist; + +} + + + +int main() + +{ + + int nrPoints; + + ifstream fileRead("cmap.in"); + + ofstream fileWrite("cmap.out"); + + fileRead >> nrPoints; + + + + PointsOX.resize(nrPoints), PointsOY.resize(nrPoints); + + for (int i = 0; i < PointsOX.size(); ++ i) + + fileRead >> PointsOX[i].first >> PointsOX[i].second; + + fileRead.close(); + + + + sort(PointsOX.begin(), PointsOX.end()); + + for (int i = 0; i < PointsOX.size(); ++ i) + + PointsOY[i] = make_pair(PointsOX[i].second, PointsOX[i].first); + + + + + + fileWrite << fixed << setprecision(6) << sqrt(retClosPoint(0,PointsOX.size())) << "\n"; + + fileWrite.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/28c202c6-0189-4ec3-adef-f0a5020c67ff.cpp b/cmap/divide_conquer/28c202c6-0189-4ec3-adef-f0a5020c67ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5db521458fa2efebfb9bc19d751519fbde7407e5 --- /dev/null +++ b/cmap/divide_conquer/28c202c6-0189-4ec3-adef-f0a5020c67ff.cpp @@ -0,0 +1,211 @@ +#include +#include +#include +#include +#include +FILE *f=fopen("cmap.in","r"),*g=fopen("cmap.out","w"); + +using namespace std; + +int n; + + + +struct Punct{ + + double x; + + double y; + +}v[100005],w[100005],a[100005]; + + + +void citire(){ + +int i; + + fscanf(f,"%d",&n); + + for(i=1; i<=n; i++){ + + fscanf(f,"%lf %lf",&v[i].x,&v[i].y); + + } + +} + + + +double distanta(Punct A,Punct B){ + + return (B.x-A.x)*(B.x-A.x) + (B.y-A.y)*(B.y-A.y); + +} + + + +bool cmp(Punct A,Punct B){ + + if(A.xv[dr].y){ + + interschimba(v[st],v[dr]); + + } + + return distanta(v[st],v[dr]); + + } + + else if(dr-st+1==3){ + + if(v[st].y>v[st+1].y){ + + interschimba(v[st],v[st+1]); + + } + + if(v[st+1].y>v[dr].y){ + + interschimba(v[st+1],v[dr]); + + } + + if(v[st].y>v[dr].y){ + + interschimba(v[st],v[dr]); + + } + + return min(distanta(v[st],v[st+1]),min(distanta(v[st+1],v[dr]),distanta(v[st],v[dr]))); + + } + + else{ + + mij=(st+dr)/2; + + xmij=v[mij].x; + + solst=rezolvare(st,mij); + + soldr=rezolvare(mij+1,dr); + + minim=min(solst,soldr); + i=st; j=mij+1; k=i; + + while(i<=mij && j<=dr){ + + if(v[i].y=-(int)sqrt(minim)){ + + + + a[t]=v[i]; t++; + + } + + } + + t--; + + for(i=1; i<=t; i++){ + + + + + + if(i+7<=t){ + + j=i+7; + + } + + else{j=t;} + + for(r=i+1; r<=j; r++){ + + if(distanta(a[i],a[r]) + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const long long NMAX = 100005; + +const long long INF = (1LL<<62); + +pair a[NMAX],b[NMAX],c[NMAX]; + +long long n; + +long long Abs(long long x){ + + if(x<0) return -x; + + return x; + +} + +long long dist(pair x,pair y){ + + return 1LL*(x.first-y.first)*(x.first-y.first)+1LL*(x.second-y.second)*(x.second-y.second); + +} + +long long solve(long long st,long long dr){ + + if(st>=dr) return INF; + + if(dr-st+1==2){ + + if(b[st+1] aux; + + aux=b[st+1]; + + b[st+1]=b[st]; + + b[st]=aux; + + } + + return dist(a[st],a[st+1]); + + } + + long long mij=(st+dr)/2; + + long long d=min(solve(st,mij),solve(mij+1,dr)); + + long long i=st,j=mij+1,m=0; + + while(i<=mij and j<=dr){ + + if(b[i]<=b[j]){ + + c[++m]=b[i]; + + i++; + + } + + else{ + + c[++m]=b[j]; + + j++; + + } + + } + + while(i<=mij){ + + c[++m]=b[i]; + + i++; + + } + + while(j<=dr){ + + c[++m]=b[j]; + + j++; + + } + + for(long long i=st;i<=dr;i++) b[i]=c[i-st+1]; + + long long m2=0; + + for(long long i=st;i<=dr;i++){ + + if(Abs(b[i].second-a[mij].first)<=d) + + c[++m2]=b[i]; + + } + + for(long long i=1;i<=m2;i++) + + for(long long j=i+1;j<=m2 and j<=i+7;j++) + + d=min(d,dist(c[i],c[j])); + + return d; + +} + +int main() + +{ + + fin >> n; + + for(long long i=1;i<=n;i++) fin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1); + + for(long long i=1;i<=n;i++){ + + b[i].first=a[i].second; + + b[i].second=a[i].first; + + } + + fout << setprecision(6) << fixed << sqrt(solve(1,n)); + + return 0; + +} diff --git a/cmap/divide_conquer/29d2d15b-4c4d-4826-9ad4-8fae93616378.cpp b/cmap/divide_conquer/29d2d15b-4c4d-4826-9ad4-8fae93616378.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57230ec9576239890d14d03b7835e0c959ce79ec --- /dev/null +++ b/cmap/divide_conquer/29d2d15b-4c4d-4826-9ad4-8fae93616378.cpp @@ -0,0 +1,206 @@ +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int nmax = 100005; + + + +int n, auxSize; + + + +struct Point{ + + long long x, y; + +}v[nmax], aux[nmax]; + + + +bool cmp(Point a, Point b){ + + return a.x < b.x; + +} + + + +void Merge(int st, int dr){ + + int mid = (st + dr) / 2; + + int i = st, j = mid + 1; + + auxSize = 0; + + while (i <= mid && j <= dr){ + + if (v[i].y < v[j].y){ + + aux[++auxSize] = v[i++]; + + } + + else if (v[j].y < v[i].y){ + + aux[++auxSize] = v[j++]; + + } + + else{ + + if (v[i].x < v[j].x){ + + aux[++auxSize] = v[i++]; + + } + + else{ + + aux[++auxSize] = v[j++]; + + } + + } + + } + + while (i <= mid){ + + aux[++auxSize] = v[i++]; + + } + + while (j <= dr){ + + aux[++auxSize] = v[j++]; + + } + + for (i = st, j = 1; i <= dr; ++i, ++j){ + + v[i] = aux[j]; + + } + +} + + + +long long GetDist(Point a, Point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long GetMinDist(int st, int dr){ + + if (dr - st + 1 <= 3){ + + if (dr - st + 1 == 3){ + + Merge(st + 1, dr); + + Merge(st, dr); + + return min(GetDist(v[st], v[st + 1]), min(GetDist(v[st], v[dr]), GetDist(v[st + 1], v[dr]))); + + } + + else if (dr - st + 1 == 2){ + + Merge(st, dr); + + return GetDist(v[st], v[dr]); + + } + + } + + int mid = (st + dr) / 2; + + int midX = v[mid].x; + + long long left = GetMinDist(st, mid); + + long long right = GetMinDist(mid + 1, dr); + + long long minim = min(left, right); + + Merge(st, dr); + + auxSize = 0; + + for (int i = st; i <= dr; ++i){ + + if (1LL * (midX - v[i].x) * (midX - v[i].x) <= minim){ + + aux[++auxSize] = v[i]; + + } + + } + + for (int i = 1; i <= auxSize; ++i){ + + for (int j = i + 1; j - i + 1 <= 9 && j <= auxSize; ++j){ + + minim = min(1LL * minim, 1LL * GetDist(aux[i], aux[j])); + + } + + } + + return minim; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmp); + + for (int i = 1; i < n; ++i){ + + if (v[i].x == v[i + 1].x && v[i].y == v[i + 1].y){ + + fout << 0; + + return 0; + + } + + } + + long long ans = GetMinDist(1, n); + + fout< +#include +#include +#include +using namespace std; + + + + + + + +vector> v,x,y; + +int n,i; + + + +long long G(int s,int d) + +{ + + if(d-s<2) + + return 4e18; + + if(d-s==2) + + { + + if(y[s]>y[s+1]) + + swap(y[s],y[s+1]); + + return ((x[s].first-x[s+1].first)*(x[s].first-x[s+1].first)+(x[s].second-x[s+1].second)*(x[s].second-x[s+1].second)); + + } + + int m=(s+d)/2,e=0,i,j; + + long long b=min(G(s,m),G(m,d)); + + for(sort(y.begin()+s,y.begin()+d),i=s;i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const int DN=1e5+5; + +int n; + +typedef pair pii; + +pii a[DN],b[DN],r[DN]; + +long long rez; + +double sol; + +long long dist(pii A,pii B) + +{ + + return 1LL*(A.first-B.first)*(A.first-B.first)+1LL*(A.second-B.second)*(A.second-B.second); + +} + +long long solve(int st,int dr) + +{ + + if(st==dr) + + return numeric_limits::max(); + + int mij=(st+dr)/2,ls=st,ld=mij+1,pz=st,m=0; + + long long rez; + + rez=min(solve(st,mij),solve(mij+1,dr)); + + for(int i=st;i<=dr;i++) + + { + + if(ls>mij||(ld<=dr&&a[ld].second>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + rez=solve(1,n); + + sol=rez; + + sol=sqrt(sol); + + + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct Point { + + int x, y; + +}; + + + +int pointsCompareFunctionYAxis(Point p1, Point p2) { + + return p1.y < p2.y; + +} + + + +int pointsCompareFunctionXAxis(Point p1, Point p2) { + + return p1.x < p2.x; + +} + + + +int nrPoints; + +vector points; + + + +void read() { + + in >> nrPoints; + + Point point; + + for (int i = 0; i < nrPoints; ++i) { + + in >> point.x >> point.y; + + points.push_back(point); + + } + +} + + + +long long int euclidianDistance(Point p1, Point p2) { + + return 1LL * (p1.x - p2.x) * (p1.x - p2.x) + 1LL * (p1.y - p2.y) * (p1.y - p2.y); + +} + + + +void intersect(int left, int right, int pivot) { + + int i = left, j = pivot + 1; + + vector temp; + + while (i <= pivot && j <= right) { + + if (points[i].y < points[j].y) + + temp.push_back(points[i++]); + + else + + temp.push_back(points[j++]); + + } + + while (i <= pivot) + + temp.push_back(points[i++]); + + while (j <= right) + + temp.push_back(points[j++]); + + for (int k = left; k <= right; ++k) + + points[k] = temp[k - left]; + +} + + + +long long int getMinDistance(int left, int right) { + + if (right - left == 1) { + + if (points[left].y > points[right].y) + + swap(points[left], points[right]); + + return euclidianDistance(points[left], points[right]); + + } + + if (right - left == 2) { + + sort(points.begin() + left, points.begin() + right + 1, pointsCompareFunctionYAxis); + + return min(euclidianDistance(points[left], points[left + 1]), + + min(euclidianDistance(points[left], points[left + 2]), + + euclidianDistance(points[left + 1], points[left + 2]))); + + } + + int pivot = (right - left) / 2 + left; + + int pivotX = points[pivot].x; + + long long int minDistance = min(getMinDistance(left, pivot), getMinDistance(pivot + 1, right)); + + intersect(left, right, pivot); + + int bandWidth = ceil(sqrt(minDistance)); + + vector band; + + for (int i = left; i <= right; ++i) + + if (abs(points[i].x - pivotX) <= bandWidth) + + band.push_back(points[i]); + + int bandLength = band.size(); + + for (int i = 0; i < bandLength; ++i) + + for (int j = i + 1; j <= i + 7 && j < bandLength; ++j) + + minDistance = min(minDistance, euclidianDistance(band[i], band[j])); + + return minDistance; + +} + + + +int main() + +{ + + read(); + + sort(points.begin(), points.end(), pointsCompareFunctionXAxis); + + out << fixed << setprecision(8) << sqrt(getMinDistance(0, nrPoints - 1)) << '\n'; + + return 0; + +} diff --git a/cmap/divide_conquer/2bcd09ce-bd72-4910-8784-5c5a90c777c2.cpp b/cmap/divide_conquer/2bcd09ce-bd72-4910-8784-5c5a90c777c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56742a9bdfa0bae266aa5a79e154b7437a9305f4 --- /dev/null +++ b/cmap/divide_conquer/2bcd09ce-bd72-4910-8784-5c5a90c777c2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +pair v[100006]; +pair a[100006]; +pair b[100006]; + +long long n, i; + +long long distanta(pair P, pair Q) +{ + long long rezultat = (P.second - Q.second) * (P.second - Q.second) + (P.first - Q.first) * (P.first - Q.first); + + return rezultat; +} + +void ordoneaza(long long st, long long mij, long long dr) +{ + long long i = st; + long long j = mij + 1; + long long poz = st - 1; + while (i <= mij && j <= dr) + { + if (v[i].second < v[j].second) + { + ++poz; + a[poz] = v[i]; + ++i; + } + else + { + ++poz; + a[poz] = v[j]; + ++j; + } + } + while (i <= mij) + { + ++poz; + a[poz] = v[i]; + ++i; + } + while (j <= dr) + { + ++poz; + a[poz] = v[j]; + ++j; + } + for (i=st; i<=dr; i++) + { + v[i] = a[i]; + } +} + +long long cautaPuncte(long long st, long long dr) +{ + long long x, y, z; + + if (dr - st == 1) + { + ordoneaza(st, st, dr); + x = distanta(v[st], v[dr]); + return x; + } + if (dr - st == 2) + { + ordoneaza(st, st, st + 1); + ordoneaza(st, st + 1, dr); + x = distanta(v[st], v[st+1]); + y = min(distanta(v[st], v[dr]), distanta(v[st+1], v[dr])); + + return min(x, y); + } + + long long mij = (st + dr) / 2; + + x = cautaPuncte(st, mij); + y = cautaPuncte(mij + 1, dr); + long long rezultat = min(x, y); + + ordoneaza(st, mij, dr); + + long long i,j; + + long long poz = 0; + + for (i=st; i<=dr; i++) + { + x = v[mij].first - v[i].first; + if (abs(x) <= rezultat) + { + ++poz; + b[poz] = v[i]; + } + } + for (i=1; i < poz; i++) + { + long long mini = min(poz, i + 7); + + for (j=i+1; j <=mini; j++) + { + x = distanta(b[i], b[j]); + + rezultat = min(rezultat, x); + } + } + + return rezultat; +} + +int main() +{ + long long x, y; + + f >> n; + + for (i=1; i<=n; i++) + { + f >> x >> y; + v[i].first = x; + v[i].second = y; + } + + sort(v + 1, v + n + 1); + + long long rezultat = cautaPuncte(1, n); + + g << setprecision(6) << fixed << sqrt(rezultat); + + return 0; +} diff --git a/cmap/divide_conquer/2be833bd-c716-4730-be0a-c70eb7078ee1.cpp b/cmap/divide_conquer/2be833bd-c716-4730-be0a-c70eb7078ee1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1507f204167fd4767cd8394178963ae37a957b9d --- /dev/null +++ b/cmap/divide_conquer/2be833bd-c716-4730-be0a-c70eb7078ee1.cpp @@ -0,0 +1,69 @@ +# include +# include +# include +# include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +pair v[100010],d[100010]; + +int n,i; + +long double dist(int x1,int Y1,int x2,int y2){ + + return sqrt(1LL*(x1-x2)*(x1-x2)+1LL*(Y1-y2)*(Y1-y2)); + +} + +long double cmap(int st,int dr){ + + if(st==dr){ + + swap(v[st].first,v[st].second); + + return 1000000000; + + } + + int mij=(st+dr)/2,k=0; + + int X=v[mij].first; + + long double sol=min(cmap(st,mij),cmap(mij+1,dr)); + + sort(v+st,v+dr+1); + + for(int i=st;i<=dr;i++) + + if(abs(v[i].second-X)<=sol) + + d[++k]=v[i]; + + for(int i=1;i<=k;i++) + + for(int j=i+1;j<=min(i+7,k);j++) + + sol=min(sol,dist(d[i].first,d[i].second,d[j].first,d[j].second)); + + return sol; + +} + +int main () { + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + fout< +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct punct + +{ + + long long x,y; + +}; + + + +bool cmp_x(punct A,punct B) + +{ + + return A.x &P,int left,int right) + +{ + + long long mn=calc_dist(P[left],P[left+1]); + + for(int i=left; i &P,int left,int right) + +{ + + if(right-left<=3) + + return search_dist_min(P,left,right); + + + + int mid=(right+left)/2; + + long long dist_left=sol(P,left,mid); + + long long dist_right=sol(P,mid+1,right); + + long long dist_min=min(dist_left,dist_right); + + + + long long val=P[mid].x; + + vectorpct_closest; + + + + for(int i=left; i<=right; i++) + + if(abs(P[i].x-val)=8) + + { + + for(int i=0; i<=(int)pct_closest.size()-8; i++) + + for(int j=i+1; j<=i+7; j++) + + dist_min=min(calc_dist(pct_closest[i],pct_closest[j]),dist_min); + + } + + else + + { + + for(int j=1; j<(int)pct_closest.size(); j++) + + dist_min=min(calc_dist(pct_closest[0],pct_closest[j]),dist_min); + + } + + + + return dist_min; + +} + + + +int main() + +{ + + vectorpct; + + int n; + + fin>>n; + + for(int i=0; i>p.x>>p.y; + + pct.push_back(p); + + } + + + + sort(pct.begin(),pct.end(),cmp_x); + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream ka("cmap.in"); +ofstream ki("cmap.out"); + + +vector> V; + +void interclasare(int, int, int); + +bool cmp(const std::pair &p1, const std::pair &p2) +{ + return (p1.second < p2.second); +} + +long long dist(const pair & a, const pair & b) +{ + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long solve(int st, int dr) +{ + if (dr - st == 1) + { + if (V[st].second > V[dr].second) + swap(V[st], V[dr]); + return dist(V[st], V[dr]); + } + else if (dr - st == 2) + { + sort(V.begin() + st, V.begin() + dr + 1, cmp); + return min(dist(V[st], V[st + 1]), min(dist(V[st], V[st + 2]), dist(V[st + 1], V[st + 2]))); + } + + int mid = (st + dr) / 2; + int mid_x = V[mid].first; + long long d = min(solve(st, mid), solve(mid + 1, dr)); + auto delta = static_cast(ceil(sqrt(d))); + interclasare(st, mid, dr); + + vector> A; + for (int i = st; i <= dr; i++) + { + if (abs((V[i].first - mid_x)) <= delta) + A.push_back(V[i]); + } + for (int i = 0; i < A.size(); i++) + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) + d = min(d, dist(A[i], A[j])); + + return d; +} + +void interclasare(int start, int m, int end) +{ + vector> Aux; + int i = start, j = m + 1; + + while ((i <= m) && (j <= end)) + if (V[i].second <= V[j].second) + Aux.push_back(V[i++]); + else + Aux.push_back(V[j++]); + + while (i <= m) + Aux.push_back(V[i++]); + + while (j <= end) + Aux.push_back(V[j++]); + + for (i = start; i <= end; i++) + V[i] = Aux[i - start]; +} + +int main() +{ + int n; + ka >> n; + + for (int i = 0; i < n; i++) + { + long long a, b; + ka >> a >> b; + V.push_back(make_pair(a, b)); + } + + sort(V.begin(), V.end()); + + ki << fixed << setprecision(6) << sqrt(solve(0, n - 1)) << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/2c6a459c-8040-4007-8cc3-bf4158ac4eb7.cpp b/cmap/divide_conquer/2c6a459c-8040-4007-8cc3-bf4158ac4eb7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c34913fb519eec18baecc162c1857799d48b95b --- /dev/null +++ b/cmap/divide_conquer/2c6a459c-8040-4007-8cc3-bf4158ac4eb7.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF = 0x7FFFFFFFFFFFFFFFLL; + +struct punct + +{ + + int x, y; + +} P[100001], Tmp[100001]; + +int N; + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + +inline long long sqr(long long v) + +{ + + return v * v; + +} + +long long dist2(const punct&A, const punct&B) + +{ + + return sqr(A.x - B.x) + sqr(A.y - B.y); + +} + +long long distmin(int p, int u) + +{ + + long long dmin = INF; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long d = dist2(P[i], P[j]); + + dmin = min(d, dmin); + + } + + return dmin; + +} + +long long calcdmin(int p, int u) + +{ + + if(u - p < 3) + + { + + sort(P + p, P + u + 1, compy); + + return distmin(p, u); + + } + + int m = p + (u - p) / 2; + + int x = P[m].x; + + long long d = min(calcdmin(p, m), calcdmin(m, u)); + + merge(P + p, P + m + 1, P + m + 1, P + u + 1, Tmp + p, compy); + + int k = 0; + + for(int i = p; i <= u; i++) + + { + + P[i] = Tmp[i]; + + if(sqr(P[i].x - x) < d) + + Tmp[++k] = P[i]; + + + + } + + + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && sqr(Tmp[j].y - Tmp[i].y) < d; j++) + + { + + long long dc = dist2(Tmp[i], Tmp[j]); + + d = min(dc, d); + + } + + return d; + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdmin(1, N)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/2ce0f21f-7dd6-43a3-b340-0a417d7ba94b.cpp b/cmap/divide_conquer/2ce0f21f-7dd6-43a3-b340-0a417d7ba94b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3ae21788df14de749a6d3aa5e3021e79807f327 --- /dev/null +++ b/cmap/divide_conquer/2ce0f21f-7dd6-43a3-b340-0a417d7ba94b.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct punct{ + + int x, y; + +} *X, *Y, *LY; + +int n; + + + +bool cmpX(punct a, punct b){ + + return a.x>n; + + X=new punct[n+1]; Y=new punct[n+1]; LY=new punct[n+1]; + + + + for(i=0;i>X[i].x>>X[i].y; + + Y[i].x=X[i].x; Y[i].y=X[i].y; + + } + + + + sort(X,X+n,cmpX); + + sort(Y,Y+n,cmpY); + + + + out< +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int n,i; + +pair a[100010]; + +vector > x,y,z; + + + +long double dist(pair A, pair B) + +{ + + return sqrt((long double)(A.first-B.first)*(A.first-B.first)+(long double)(A.second-B.second)*(A.second-B.second)); + +} + + + +class comp1 + +{ + +public: + + bool operator()(pair A, pair B) + + { + + return A.first A, pair B) + +{ + + return A.second=st&&minim>=a[mij].first-a[i].first; i--) + + x.push_back(a[i]); + + for (int i=mij+1; i<=dr&&minim>=a[i].first-a[mij+1].first; i++) + + y.push_back(a[i]); + + sort(x.begin(),x.end(),comp2()); + + sort(y.begin(),y.end(),comp2()); + + int j,k; + + j=k=0; + + z.clear(); + + while (j> n; + + for (i=1; i<=n; i++) + + fin >> a[i].first >> a[i].second; + + sort(a+1,a+n+1,comp1()); + + fout << fixed << setprecision(7) << divide(1,n) << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/2dba67ac-2896-4933-a466-28e63085073e.cpp b/cmap/divide_conquer/2dba67ac-2896-4933-a466-28e63085073e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b49827553eb2d8910e985cf2bd658c7240de2e48 --- /dev/null +++ b/cmap/divide_conquer/2dba67ac-2896-4933-a466-28e63085073e.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + +struct punct + +{ + + int x, y; + +} *X,*Y,*LY; + + + +int n; + + + +bool cmpX(punct a, punct b) + +{ + + return a.x < b.x; + +} + +bool cmpY(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +long long dist( punct a, punct b) + +{ + + long long X = 1LL * (a.x - b.x ); + + long long Y = 1LL * ( a.y - b.y ); + + return X*X + Y*Y ; + +} + + + +long long divide(int st,int dr,punct y[],int nr) + +{ + + if(dr-st == 1) + + return dist( X[st], X[dr] ); + + if (dr-st == 2) + + return min(dist(X[st], X[st+1]),min(dist(X[st+1], X[dr]),dist(X[st], X[dr]))); + + int mij=(st+dr)/2; + + int i,p=0,q=0; + + punct *SY = new punct[nr+1]; + + punct *DY = new punct[nr+1]; + + for( i = 0; i <= nr; i++) + + if( y[i].x < X[mij].x ) + + { + + SY[p].x = y[i].x; + + SY[p].y = y[i].y; + + ++p; + + } + + else + + { + + DY[q].x = y[i].x; + + DY[q].y = y[i].y; + + ++q; + + } + + long long d1 = divide ( st, mij, SY, p-1 ) ; + + long long d2 = divide ( mij+1, dr,DY, q-1 ) ; + + long long d = min( d1, d2); + + long long d_sqrt = ceil(sqrt(d)); + + int j,k=0; + + for( i = 0; i <= nr; i++) + + if( abs( y[i].x - X[mij].x ) <= d_sqrt ) + + { + + LY[k].x = y[i].x; + + LY[k].y = y[i].y; + + k++; + + } + + for(i = 0; i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++) + + d = min(d,dist(LY[i],LY[j])); + + + + delete [] SY; + + delete [] DY; + + return d; + +} + + + +int main() + +{ + + f >> n; + + X = new punct[n+1]; + + Y = new punct[n+1]; + + LY = new punct[n+1]; + + for ( int i = 0; i < n ; i++) + + { + + f >> X[i].x >> X[i].y; + + Y[i].x = X[i].x; + + Y[i].y = X[i].y; + + } + + sort(X,X+n,cmpX); + + sort(Y,Y+n,cmpY); + + g << fixed << setprecision(6) << sqrt(divide(0,n-1,Y,n-1)); + + delete [] X; + + delete [] Y; + + delete [] LY; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/2dbc979b-43c0-4655-9f78-1bbaea66a143.cpp b/cmap/divide_conquer/2dbc979b-43c0-4655-9f78-1bbaea66a143.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbfd8bf4920f62fbae7a720d934414ccdce65a8d --- /dev/null +++ b/cmap/divide_conquer/2dbc979b-43c0-4655-9f78-1bbaea66a143.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int n,i; + +pair v[100005],aux[100005]; + + + +long long dist(pair a, pair b) + +{ return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); } + + + +void interclasare(int st, int mid, int dr) + +{ + + int i = st; int j = mid+1; int k = 0; + + while (i <= mid && j <= dr) + + if (v[i].second <= v[j].second) + + aux[++k] = v[i++]; + + else + + aux[++k] = v[j++]; + + for (;i<=mid; i++) + + aux[++k] = v[i]; + + for (;j<=dr; j++) + + aux[++k] = v[j]; + + for (int i=st; i<=dr; i++) + + v[i] = aux[i-st+1]; + +} + + + +long long solve(int st, int dr) + +{ + + int mid = (st+dr)/2; + + if (dr-st == 1) + + { + + interclasare(st, mid, dr); + + return dist(v[st], v[dr]); + + } + + if (dr-st == 2) + + { + + interclasare(st, st, mid); interclasare(st, mid, dr); + + return min(dist(v[st+1], v[st+2]), min(dist(v[st], v[st+1]), dist(v[st], v[st+2]))); + + } + + long long sols = solve(st, mid); + + long long sold = solve(mid+1, dr); + + long long sol = min(sols, sold); + + interclasare(st, mid, dr); + + int k = 0; + + for (int i=st; i<=mid; i++) + + if (v[mid].first-v[i].first <= sol) + + aux[++k] = v[i]; + + for (int i=mid+1; i<=dr; i++) + + if (v[i].first-v[mid].first <= sol) + + aux[++k] = v[i]; + + for (int i=1; i> n; + + for (i=1; i<=n; i++) + + fin >> v[i].first >> v[i].second; + + sort(v+1, v+n+1); + + fout << setprecision(7) << fixed << sqrt(solve(1, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/2df5d73c-2b91-4c0c-9c64-d43d4298d895.cpp b/cmap/divide_conquer/2df5d73c-2b91-4c0c-9c64-d43d4298d895.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9085f9915be7b8b2fa56f12433575bdbd42502db --- /dev/null +++ b/cmap/divide_conquer/2df5d73c-2b91-4c0c-9c64-d43d4298d895.cpp @@ -0,0 +1,124 @@ +#include + + + + + + +using namespace std; + + + +const int MAXN = 1e5; + +const long long INF = 2000LL * 1000 * 1000 * 2000 * 1000 * 1000; + +typedef pair < int, int > Point; + + + +Point xsort[MAXN], ysort[MAXN], aux[MAXN]; + + + +inline long long dsqr(Point A, Point B) { + + return 1LL * (A.first - B.first) * (A.first - B.first) + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long solve(int l, int r) { + + if (l >= r) + + return INF; + + if (l + 1 == r) { + + if (ysort[l] > ysort[r]) + + swap(ysort[l], ysort[r]); + + return dsqr(ysort[l], ysort[r]); + + } + + int mid = (l + r) / 2, k = 0, i = l, j = mid + 1; + + long long lim = min(solve(l, mid), solve(mid + 1, r)); + + while (i <= mid && j <= r) + + if (ysort[i] < ysort[j]) + + aux[k++] = ysort[i++]; + + else + + aux[k++] = ysort[j++]; + + while (i <= mid) + + aux[k++] = ysort[i++]; + + while (j <= r) + + aux[k++] = ysort[j++]; + + for (int ind = l; ind <= r; ++ind) + + ysort[ind] = aux[ind - l]; + + k = 0; + + for (int ind = l; ind <= r; ++ind) + + if (abs(ysort[ind].second - xsort[mid].first) <= lim) + + aux[k++] = ysort[ind]; + + for (i = 0; i < k; ++i) + + for (j = i + 1; j < min(k, i + 8); ++j) + + lim = min(lim, dsqr(aux[i], aux[j])); + + return lim; + +} + + + +int main() + +{ + + int n; + + ifstream fin("cmap.in"); + + fin >> n; + + for (int i = 0; i < n; ++i) + + fin >> xsort[i].first >> xsort[i].second; + + fin.close(); + + sort(xsort, xsort + n); + + for (int i = 0; i < n; ++i) + + ysort[i] = {xsort[i].second, xsort[i].first}; + + ofstream fout("cmap.out"); + + fout << setprecision(8) << fixed << sqrt(solve(0, n - 1)) << '\n'; + + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/2ffb7924-2d55-4f99-9534-1db7a85265cc.cpp b/cmap/divide_conquer/2ffb7924-2d55-4f99-9534-1db7a85265cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..299720311368817ee502899903ad92c8e8912b60 --- /dev/null +++ b/cmap/divide_conquer/2ffb7924-2d55-4f99-9534-1db7a85265cc.cpp @@ -0,0 +1,218 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + +struct point { + + + + int x, y; + + + +}; + + + +vector tempPointList; + +vector points, pointsX, pointsY; + +vector leftY, rightY; + + + +bool cmpX(point A, point B) { + + if(A.x - B.x) + + return A.x < B.x; + + return A.y < B.y; + +} + + + +bool cmpY(point A, point B) { + + if(A.y - B.y) + + return A.y < B.y; + + return A.x < B.x; + +} + + + +double getDist(point A, point B) { + + + + return sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2)); + + + +} + + + +vector getInput() { + + + + int numberOfPoints; + + point temp; + + vector pointsList; + + f >> numberOfPoints; + + for(int i = 0; i < numberOfPoints; ++i) { + + + + f >> temp.x >> temp.y; + + pointsList.push_back(temp); + + + + } + + return pointsList; + + + +} + + + +double getMinDist(int left, int right, vector pointsY) { + if(left >= right) { + + + + return INT_MAX; + + } + + + + + + if(right - left <= 2) { + + + + double temp = INT_MAX; + + for(int i = left; i <= right; ++i) + + for(int j = i + 1; j <= right; ++j) + + temp = min(temp, getDist(points[i], points[j])); + + return temp; + + + + } + + + + + + int mid = (left + right) / 2; + + double temp = INT_MAX, temp1, temp2; + + leftY.clear(); + + rightY.clear(); + + for(int i = 0; i < pointsY.size(); ++i) { + + if(pointsY[i].x <= pointsX[mid].x) + + leftY.push_back(pointsY[i]); + + if(pointsY[i].x >= pointsX[mid].x) + + rightY.push_back(pointsY[i]); + + } + temp1 = getMinDist(left, mid, leftY); + + temp2 = getMinDist(mid, right, rightY); + + temp = min(temp1, temp2); + + + + + + tempPointList.clear(); + + for(int i = 0; i < pointsY.size(); ++i) + + if(pointsX[mid].x - pointsY[i].x <= temp) + + tempPointList.push_back(pointsY[i]); + + + + for(int i = 0; i < tempPointList.size(); ++i) + + for(int j = i + 1; j < i + 7 && j < tempPointList.size(); ++j) + + temp = min(temp, getDist(tempPointList[i], tempPointList[j])); + + + + return temp; + +} + + + +int main() + +{ + + points = getInput(); + + pointsX = points; + + pointsY = points; + + sort(pointsX.begin(), pointsX.end(), cmpX); + + sort(pointsY.begin(), pointsY.end(), cmpY); + double minDist = getMinDist(0, points.size() - 1, pointsY); + + + + g << setprecision(20) << minDist << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/308bcf79-787e-4160-93d1-d92b27f8689d.cpp b/cmap/divide_conquer/308bcf79-787e-4160-93d1-d92b27f8689d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17cd0aeedb60716bea287c1d348edffc05b71e61 --- /dev/null +++ b/cmap/divide_conquer/308bcf79-787e-4160-93d1-d92b27f8689d.cpp @@ -0,0 +1,158 @@ +#include + + + + + + +using namespace std; + + + +void citire (int &n, vector > &PCT) + +{ + + ifstream f("cmap.in"); + + f >> n; + + for(int i = 0; i < n; i++) + + { + + int first, second; + + f >> first >> second; + + PCT.push_back (make_pair (first, second)); + + } + + f.close(); + +} + + + +double distanta (pair A, pair B) + +{ + + double a = A.first - B.first; + + double b = A.second - B.second; + + return sqrt(pow(a, 2) + pow(b, 2)); + +} + + + +bool cmp1 (const pair &A, const pair &B) + +{ + + return A.first < B.first; + +} + +bool cmp2 (const pair &A, const pair &B) + +{ + + return A.second < B.second; + +} + + + +double di (int st, int dr, vector > &PCT, vector > &PCTsortateY) + +{ + + if(dr - st == 1) + + return INT_MAX; + + else if(dr - st == 2) + + { + + if (PCTsortateY[st].second > PCTsortateY[st + 1].second) + + swap(PCTsortateY[st], PCTsortateY[st + 1]); + + return distanta(PCT[0], PCT[1]); + + } + + + + int m = (st + dr) / 2; + + double dist = min (di(st, m, PCT, PCTsortateY), di(m, dr, PCT, PCTsortateY)); + + + + vector> temp(dr - st); + + merge(PCTsortateY.begin() + st, PCTsortateY.begin() + m, PCTsortateY.begin() + m, PCTsortateY.begin() + dr, temp.begin(), cmp2); + + copy(temp.begin(), temp.begin() + (dr - st), PCTsortateY.begin() + st); + + + + vector> temp2; + + for(unsigned i = st; i < dr; i++) + + if(abs(PCTsortateY[i].first - PCT[m].first) <= dist) + + temp2.push_back(PCTsortateY[i]); + + + + for(unsigned i = 0; i < temp2.size(); i++) + + for(unsigned j = i + 1; j < temp2.size() && temp2[j].second - temp2[i].second <= dist; j++) + + dist = min(dist, distanta (temp2[i], temp2[j])); + + + + return dist; + +} + + + +int main() + +{ + + int n; + + vector > PCT; + + + + citire(n, PCT); + + sort(PCT.begin(), PCT.end(), cmp1); + + vector > PCTsortateY = PCT; + + + + ofstream g("cmap.out"); + + g << fixed << setprecision(6) << di(0, n, PCT, PCTsortateY); + + g.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/316fedb6-68df-411f-90df-7d4b4c5da8c7.cpp b/cmap/divide_conquer/316fedb6-68df-411f-90df-7d4b4c5da8c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0fbdc65abb9c5eb339489a0a8e3f023d2045129 --- /dev/null +++ b/cmap/divide_conquer/316fedb6-68df-411f-90df-7d4b4c5da8c7.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +using namespace std; + + + + + +vector> v,x,y; +int n,i,q=-1; + + +char u[20000000]; + +inline long long A() + +{ + + long long s=0,b=1; + + q++; + + if(u[q]==45) + + b=-1,q++; + + for(;u[q]>47;q++) + + s=s*10+u[q]-48; + + return s*b; + +} +inline long long G(int s,int d) +{ + if(d-s<2) + return 4e18; + if(d-s==2) + { + if(y[s]>y[s+1]) + swap(y[s],y[s+1]); + return ((x[s].first-x[s+1].first)*(x[s].first-x[s+1].first)+(x[s].second-x[s+1].second)*(x[s].second-x[s+1].second)); + } + int m=(s+d)/2,e=0,i,j; + long long b=min(G(s,m),G(m,d)); + for(sort(y.begin()+s,y.begin()+d),i=s;i +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef long long ll; + +typedef pair < ll, ll > point; + +point points[100001], strip[100001]; + +point aux[100001]; + +int n, n_strip; + + + +void read() { + + + + ifstream in("cmap.in"); + + ll x, y; + + in >> n; + + for (int i = 1; i <= n; ++i) { + + + + in >> x >> y; + + points[i] = make_pair(x, y); + + } + + in.close(); + +} + + + +bool compareX(const point& f, const point& s) { + + + + if ((f.first < s.first) || (f.first == s.first && f.second < s.second)) + + return true; + + return false; + +} + + + +bool compareY(const point& f, const point& s) { + + + + if ((f.second < s.second) || (f.second == s.second && f.first < s.first)) + + return true; + + return false; + +} + + + +inline ll dist(const point& p1, const point& p2) { + + + + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +ll brute_dist(point points[], int left, int right) { + + + + ll best_dist = LLONG_MAX; + + for (int i = left; i < right; i++) + + for (int j = i + 1; j <= right; j++) + + best_dist = min(best_dist, dist(points[i], points[j])); + + + + return best_dist; + +} + + + +ll min_dist_strip(ll d_min) { + + + + ll best_dist = d_min; + + + + for (int i = 1; i < n_strip; i++) + + for (int j = i + 1, k = 1; j <= n_strip && k <= 7; j++, k++) + + best_dist = min(best_dist, dist(strip[i], strip[j])); + + + + return best_dist; + +} + + + +void merge(int x1, int y1, int x2, int y2) { + + + + int k = 0; + + while (x1 <= y1 && x2 <= y2) { + + + + if (compareY(points[x1], points[x2])) + + aux[++k] = points[x1++]; + + else + + aux[++k] = points[x2++]; + + } + + while (x1 <= y1) + + aux[++k] = points[x1++]; + + while (x2 <= y2) + + aux[++k] = points[x2++]; + +} + + + +ll merge_sort(int left, int right) { + + + + if (right - left < 4) { + + + + sort(points + left, points + right + 1, compareY); + + return brute_dist(points, left, right); + + + + } + + + + int mid = left + (right - left) / 2; + + ll mid_x = points[mid].first; + + + + ll d1 = merge_sort(left, mid); + + ll d2 = merge_sort(mid + 1, right); + + ll delta = min(d1, d2); + + + + + + merge(left, mid, mid + 1, right); + + + + for (int i = left; i <= right; ++i) + + points[i] = aux[i - left + 1]; + + + + + + n_strip = 0; + + + + for (int i = left; i <= right; i++) + + if (abs(points[i].first - mid_x) <= delta) + + strip[++n_strip] = points[i]; + + ll d3 = min_dist_strip(delta); + + return min(delta, d3); + +} + + + +int main() { + + + + read(); + + + + sort(points + 1, points + n + 1, compareX); + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(6) << sqrt(merge_sort(1, n)) << "\n"; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/3322a577-cd6f-4cd8-910c-e299f03a1ea1.cpp b/cmap/divide_conquer/3322a577-cd6f-4cd8-910c-e299f03a1ea1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e70da0d69b6c934cf5ab1294fb50ca80d6e9f27 --- /dev/null +++ b/cmap/divide_conquer/3322a577-cd6f-4cd8-910c-e299f03a1ea1.cpp @@ -0,0 +1,156 @@ +#include + + +using namespace std; + + + +const int Nmax = 1e5+5; + + + +int n,i; + +struct Point + +{ + + int x, y; + +}; + +Point a[Nmax], aux[Nmax]; + + + +long long dist(Point a, Point b) + +{ + + return 1LL * (a.x-b.x) * (a.x-b.x) + 1LL * (a.y-b.y) * (a.y-b.y); + +} + + + +bool cmp_x(Point a, Point b) + +{ + + if(a.x==b.x) return a.y +#include + + +using namespace __gnu_pbds; +using namespace std; + + + + + + + +typedef pair< int, int > pii; +typedef pair< long long, long long > pll; +typedef long long ll; +typedef unsigned long long ull; +typedef vector< int > vi; +typedef vector< vi > vvi; +typedef vector< ll > vll; +typedef vector< vll > vvll; +typedef vector< pii > vpii; +typedef vector< vpii > vvpii; +typedef vector< pll > vpll; +typedef long double ld; +typedef vector< ld > vld; + +const ll MOD2 = 1e9 + 7; +const int MOD = 1e9 + 7; + +const ull infull = numeric_limits::max(); + +void fix(int &x, ll MOD) { + x = (x % MOD); + if(x < 0) x += MOD; + return; +} + +ll lgput(ll a, ll b, ll MOD) { + ll ret = 1; + a %= MOD; + while(b) { + if(b&1) ret = ret*a % MOD; + a = a*a % MOD; + b >>= 1; + } + + return ret; +} + +ll inv(ll a, ll MOD) { + return lgput(a, MOD-2, MOD); +} + +struct f { + int a, b; + f(int _a = 0, int _b = 0) : a(_a) ,b(_b) {} + ll eval(int x) { + return 1ll*x*a + b; + } +}; + +int binarySearch(int x, vi &v) { + int pos = 0; + int l = 1, r = (int)((v).size()); + while(l <= r) { + int m = l + r >> 1; + if(v[m] <= x) { + pos = m; + l = m + 1; + } else { + r = m - 1; + } + } + + return pos; +} + +struct pt { + int x, y, id; +}; + +struct cmp_x { + bool operator()(const pt & a, const pt & b) const { + return a.x < b.x || (a.x == b.x && a.y < b.y); + } +}; + +struct cmp_y { + bool operator()(const pt & a, const pt & b) const { + return a.y < b.y; + } +}; + +int n; +vector a; + +double mindist; +pair best_pair; + +void upd_ans(const pt & a, const pt & b) { + double dist = sqrt(1ll*(a.x - b.x)*(a.x - b.x) + 1ll*(a.y - b.y)*(a.y - b.y)); + if (dist < mindist) { + mindist = dist; + best_pair = {a.id, b.id}; + } +} +vector t; + +void rec(int l, int r) { + if (r - l <= 3) { + for (int i = l; i < r; ++i) { + for (int j = i + 1; j < r; ++j) { + upd_ans(a[i], a[j]); + } + } + sort(a.begin() + l, a.begin() + r, cmp_y()); + return; + } + + int m = (l + r) >> 1; + int midx = a[m].x; + rec(l, m); + rec(m, r); + + merge(a.begin() + l, a.begin() + m, a.begin() + m, a.begin() + r, t.begin(), cmp_y()); + copy(t.begin(), t.begin() + r - l, a.begin() + l); + + int tsz = 0; + for (int i = l; i < r; ++i) { + if (abs(a[i].x - midx) < mindist) { + for (int j = tsz - 1; j >= 0 && a[i].y - t[j].y < mindist; --j) + upd_ans(a[i], t[j]); + t[tsz++] = a[i]; + } + } +} + +int main() { + + + + + {freopen("cmap"".in","r",stdin);freopen("cmap"".out","w",stdout);}; + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + cout.precision(12); + srand(time(NULL)); + + cin >> n; + a.resize(n); + + for(int i = 0; i < n; ++i) { + cin >> a[i].x >> a[i].y; + } + + t.resize(n); + sort(a.begin(), a.end(), cmp_x()); + mindist = 1E20; + rec(0, n); + + cout << setprecision(12) << fixed << mindist << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/3459e1e2-0ffe-4e93-b4eb-28ee0ab0dccb.cpp b/cmap/divide_conquer/3459e1e2-0ffe-4e93-b4eb-28ee0ab0dccb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85479ed2f7b18880e70fa4e33c9161daef29c412 --- /dev/null +++ b/cmap/divide_conquer/3459e1e2-0ffe-4e93-b4eb-28ee0ab0dccb.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +int n; + +pair < int, int > V[100010]; + + + +class cmp + +{ + + public : + + bool operator () (const pair < int, int > &a, const pair < int, int > &b) + + { + + return (a.second < b.second); + + } + +}; + + + +int Modul(const int &a) + +{ + + if (a < 0) return -a; + + return a; + +} + + + +long long Minim(const long long &a, const long long &b) + +{ + + if (a < b) return a; + + return b; + +} + + + +long long Euclid_Dist(const pair < int, int > &a, const pair < int, int > &b) + +{ + + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); + +} + + + + + +long long Solve(int left, int right) + +{ + + if (right - left == 1) + + { + + return Euclid_Dist(V[left], V[right]); + + } + + else if (right - left == 2) + + { + + return Minim(Euclid_Dist(V[left], V[left + 1]), Minim(Euclid_Dist(V[left], V[left + 2]), Euclid_Dist(V[left + 1], V[left + 2]))); + + } + + + + int middle = (left + right) >> 1; + + long long d = Minim(Solve(left, middle), Solve(middle + 1, right)); + + vector < pair < int, int > > A; + + int un_d = int(sqrt(d) + 1); + + for (int i = left; i <= right; i ++) + + { + + if (Modul(V[i].first - V[middle].first) <= un_d) + + { + + A.push_back(V[i]); + + } + + } + + sort(A.begin(), A.end(), cmp()); + + for (int i = 0; i < A.size(); i ++) + + { + + for (int j = i + 1; j <= i + 7 && j < A.size(); j ++) + + { + + d = Minim(d, Euclid_Dist(A[i], A[j])); + + } + + } + + return d; + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i ++) fin >> V[i].first >> V[i].second; + + sort(V + 1, V + 1 + n); + + + + fout << fixed << setprecision(6) << sqrt(Solve(1, n)) << '\n'; + + return 0; + +} diff --git a/cmap/divide_conquer/353f0d1a-b27b-4695-8c91-c934017e7ff8.cpp b/cmap/divide_conquer/353f0d1a-b27b-4695-8c91-c934017e7ff8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4fc260bb5956864ce6657bfc17ab3322cec877 --- /dev/null +++ b/cmap/divide_conquer/353f0d1a-b27b-4695-8c91-c934017e7ff8.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF = 0x7FFFFFFFFFFFFFFFLL; + +struct punct + +{ + + int x, y; + +} P[100001], Tmp[100001]; + +int N; + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + +inline long long sqr(long long v) + +{ + + return v * v; + +} + +long long dist2(const punct&A, const punct&B) + +{ + + return sqr(A.x - B.x) + sqr(A.y - B.y); + +} + +long long distmin(int p, int u) + +{ + + long long dmin = INF; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long d = dist2(P[i], P[j]); + + dmin = max(d, dmin); + + } + + return dmin; + +} + +long long calcdmin(int p, int u) + +{ + + if(u - p < 3) + + return distmin(p, u); + + int m = p + (u - p) / 2; + + long long d = min(calcdmin(p, m), calcdmin(m, u)); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(sqr(P[i].x - P[m].x) < d) + + Tmp[++k] = P[i]; + + sort(Tmp + 1, Tmp + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && sqr(Tmp[j].y - Tmp[i].y) < d; j++) + + { + + long long dc = dist2(Tmp[i], Tmp[j]); + + d = min(dc, d); + + } + + return d; + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdmin(1, N)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/36c02317-0b38-4ff4-ba2e-272dae19e3a7.cpp b/cmap/divide_conquer/36c02317-0b38-4ff4-ba2e-272dae19e3a7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..919eff3f2e10a6eb941c7174a982aa307ec72815 --- /dev/null +++ b/cmap/divide_conquer/36c02317-0b38-4ff4-ba2e-272dae19e3a7.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +const long long inf=1e18; + +struct punct + +{ + + int x,y; + +}v[100010],v1[100010]; + + + +int cmp1(punct a,punct b) + +{ + + return a.x=dr) return inf; + + if(st+1==dr) + + { + + if(cmp2(v[dr],v[st])) swap(v[st],v[dr]); + + return dist(v[st],v[dr]); + + } + + int mid=(st+dr)/2,midx=v[mid].x; + + long long sol=min(solve(st,mid),solve(mid+1,dr)); + + merge(v+st,v+mid+1,v+mid+1,v+dr+1,v1+st,cmp2); + + int nr=0; + + for(int i=st;i<=dr;i++) + + { + + v[i]=v1[i]; + + if(sqr(v1[i].x-midx)<=sol) v1[++nr]=v1[i]; + + } + + for(int i=1;i<=nr;i++) + + for(int j=i+1;j<=nr && sqr(v1[i].y-v1[j].y)<=sol;j++) sol=min(sol,dist(v1[i],v1[j])); + + return sol; + +} + + + +int main() + +{ + + freopen("cmap.in", "r" ,stdin); + + freopen("cmap.out", "w", stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;i++) scanf("%d%d",&v[i].x,&v[i].y); + + sort(v+1,v+1+n,cmp1); + + printf("%.9f",sqrt(solve(1,n))); + + return 0; + +} diff --git a/cmap/divide_conquer/36d5f8bb-8e69-4eec-8d24-1320e06e273e.cpp b/cmap/divide_conquer/36d5f8bb-8e69-4eec-8d24-1320e06e273e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae2435359c3be69a7af6433a4ce87aa169e727e5 --- /dev/null +++ b/cmap/divide_conquer/36d5f8bb-8e69-4eec-8d24-1320e06e273e.cpp @@ -0,0 +1,227 @@ +#include + + +using namespace std; + + + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct point { + + double x, y; + + point(double _x = 0, double _y = 0) :x(_x), y(_y) {} + +}; + + + +ostream& operator<< (ostream& out, const point &A) { + + out << A.x << " " << A.y; + + return out; + +} + +inline double distance(point &a, point &b) { + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +inline bool cmpX(point &A, point& B) { + + return A.x < B.x; + +} + + + +inline bool cmpY(point &A, point& B) { + + return A.y < B.y; + +} + + + +void read(vector &points) { + + int i, n; + + double x, y; + + fin >> n; + + points.resize(n); + + for (i = 0; i < n; ++i) { + + fin >> x >> y; + + points[i] = point(x, y); + + } + +} + + + +point A, B; + +double smallestDistance(vector &points, int left, int right) { + + if (right - left+ 1 == 2) { + + if (points[left].y > points[right].y) swap(points[left], points[right]); + + A = points[left]; + + B = points[right]; + + return distance(points[left], points[right]); + + } + + + + if (right - left + 1 == 3) { + + sort(points.begin() + left, points.begin() + right, cmpY); + + double d1 = distance(points[left], points[left + 1]); + + double d2 = distance(points[left + 1], points[right]); + + double d3 = distance(points[left], points[right]); + + if (d1 < d2) { + + if (d1 < d3) { + + A = points[left]; + + B = points[left + 1]; + + return d1; + } + + A = points[left + 1]; + + B = points[right]; + + return d2; + + } + + + + A = points[left]; + + B = points[right]; + + return d3; + + } + + + + int middle = (left + right) / 2; + + double median = points[middle].x; + + double delta = min(smallestDistance(points, left, middle), smallestDistance(points, middle + 1, right)); + + int nr = 0, i = left, j = middle + 1, k = 0; + + point aux[right - left + 1], rectangle[right - left + 1]; + + while (i <= middle && j <= right) { + + if (points[i].y < points[j].y) + + aux[k++] = points[i++]; + + else + + aux[k++] = points[j++]; + + } + + + + while (i <= middle) + + aux[k++] = points[i++]; + + while (j <= right) + + aux[k++] = points[j++]; + + + + + + for (i = left; i <= right; ++i) { + + points[i] = aux[i - left]; + + if (points[i].x - median <= delta && points[i].x - median >= -delta) { + + rectangle[nr++] = points[i]; + + } + + } + + + + for (i = 0; i < nr; ++i) { + + int neighboursNumber = min(nr, i + 8); + + + + for (j = i + 1; j < neighboursNumber; ++j) { + + if (distance(rectangle[i], rectangle[j]) < delta) { + + delta = distance(rectangle[i], rectangle[j]); + + A = rectangle[i]; + + B = rectangle[j]; + + } + + } + + } + + return delta; + +} + +int main() { + + vector points; + + read(points); + + sort(points.begin(), points.end(), cmpX); + + double dist = smallestDistance(points, 0, points.size() - 1); + + fout << setprecision(6) << fixed << dist << "\n"; + +} diff --git a/cmap/divide_conquer/37254ea6-7e64-43a1-935c-50f8586eeed2.cpp b/cmap/divide_conquer/37254ea6-7e64-43a1-935c-50f8586eeed2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed6574a506881281a3cab63191d2f10548eb6c98 --- /dev/null +++ b/cmap/divide_conquer/37254ea6-7e64-43a1-935c-50f8586eeed2.cpp @@ -0,0 +1,304 @@ +#include + + + + +using namespace std; + +typedef long long LL; + + + +const LL INF = 4e18; + + + +struct Point + +{ + + LL x; + + LL y; + + + + void operator=(const Point &other) + + { + + x = other.x; + + y = other.y; + + } + +}; + + + +Point xVector[100000 + 5 + 5], yVector[100000 + 5 + 5]; + + + +bool xCmp(Point a, Point b) + +{ + + return (a.x < b.x); + +} + + + +bool yCmp(Point a, Point b) + +{ + + return (a.y < b.y); + +} + + + +LL dist(const Point &a, const Point &b) + +{ + + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); + +} + + + +void mergeVectorY(int st, int mij, int dr) + +{ + + vectoraux; + + + + int i = st; + + int j = mij + 1; + + + + while(i <= mij && j <= dr) + + if(yVector[i].y == yVector[j].y) + + { + + if(yVector[i].x < yVector[j].x) + + { + + aux.push_back(yVector[i]); + + i++; + + } + + else + + { + + aux.push_back(yVector[j]); + + j++; + + } + + } + + else + + { + + if(yVector[i].y < yVector[j].y) + + { + + aux.push_back(yVector[i]); + + i++; + + } + + else + + { + + aux.push_back(yVector[j]); + + j++; + + } + + } + + + + while(i <= mij) + + { + + aux.push_back(yVector[i]); + + i++; + + } + + + + while(j <= dr) + + { + + aux.push_back(yVector[j]); + + j++; + + } + + + + for(int i = 0; i < aux.size(); i++) + + yVector[st + i] = aux[i]; + +} + + + +LL solve(int st, int dr) + +{ + + if(st >= dr)return INF; + + if(dr == st + 1) + + { + + if(yVector[st].y > yVector[dr].y || (yVector[st].y == yVector[dr].y && yVector[st].x > yVector[dr].x)) + + swap(yVector[st], yVector[dr]); + + + + return dist(xVector[st], xVector[dr]); + + } + + + + int mij = (st + dr) >> 1; + + LL best = min(solve(st, mij), solve(mij + 1, dr)); + + + + mergeVectorY(st, mij, dr); + + + + vectorrectangle; + + + + for(int i = st; i <= dr; i++) + + if(abs(yVector[i].x - xVector[mij].x) < best)rectangle.push_back(yVector[i]); + + + + if(rectangle.size() == 0)rectangle.push_back({0, 0}); + + + + for(int i = 0; i < rectangle.size() - 1; i++) + + { + + for(int j = i + 1; j < rectangle.size() && j - i < 8; j++) + + { + + + + best = min(best, dist(rectangle[i], rectangle[j])); + + } + + + + } + + + + return best; + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + + + + + fin.tie(0); + + fout.tie(0); + + srand(time(NULL)); + + + + int n; + + + + fin >> n; + + + + for(int i = 1; i <= n; i++) + + fin >> xVector[i].x >> xVector[i].y; + + + + sort(xVector + 1, xVector + 1 + n, xCmp); + + + + for(int i = 1; i <= n; i++) + + yVector[i] = xVector[i]; + + + + fout << fixed << setprecision(6) << sqrt(solve(1, n)); + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/3731d4dd-dde4-4f69-aa10-5ef81381287e.cpp b/cmap/divide_conquer/3731d4dd-dde4-4f69-aa10-5ef81381287e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18b03319551c0aeec45967935a602bd8aefd93cb --- /dev/null +++ b/cmap/divide_conquer/3731d4dd-dde4-4f69-aa10-5ef81381287e.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream cin ("cmap.in");ofstream cout ("cmap.out"); + +struct nod{long long x , y;}; + +bool cmpx (nod a , nod b){return a.x < b.x;} + +bool cmpy (nod a , nod b){return a.y < b.y;} + +nod v[100100] , ord[100100]; + +double dist (nod a , nod b){ + + double now = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); + + return sqrt(now); + +} + +double solve (int st , int dr){ + + double ans = 1e18; + + if (dr-st+1 <= 3){ + + for (int i=st; i<=dr; i++){ + + for (int j=i+1; j<=dr; j++){ + + ans = min(ans , dist(v[i] , v[j])); + + } + + } + + sort (v+st , v+dr+1 , cmpy); + + return ans; + + } + + int mij = st + dr; + + mij /= 2; + + long long middle = v[mij].x; + + double low = min(solve(st , mij) , solve(mij+1 , dr)); + + int ST = st , DR = mij+1 , pnt = st; + + while (ST <= mij && DR <= dr){ + + if (v[ST].y < v[DR].y){ + + ord[pnt++] = v[ST++]; + + } + + else{ + + ord[pnt++] = v[DR++]; + + } + + } + + for (; ST<=mij; ST++)ord[pnt++]=v[ST]; + + for (; DR<=dr; DR++)ord[pnt++]=v[DR]; + + for (int i=st; i<=dr; i++){v[i] = ord[i];} + + pnt = 0; + + for (int i=st; i<=dr; i++){ + + if (abs(v[i].x - middle) <= (long long)low){ + + ord[++pnt]= v[i]; + + } + + } + + ans = low; + + for (int i=1; i<=pnt; i++){ + + for (int j=i+1; j<=pnt; j++){ + + if (ord[j].y - ord[i].y > (long long)low){ + + break; + + } + + ans = min(ans , dist(ord[i] , ord[j])); + + } + + } + + return ans; + +} + +int main() { + + + + cout << setprecision(10) << fixed; + + int n; + + cin>>n; + + for (int i=1; i<=n; i++){ + + int a , b; + + cin>>a>>b; + + v[i].x = a; + + v[i].y = b; + + } + + sort(v+1 , v+n+1 , cmpx); + + cout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct point{ + + int x, y; + +}; + + + +int n; + +vector points; + +vector points2; + + + +void in(){ + + ifstream fin("cmap.in"); + + fin >> n; + + for (int i = 0; i < n; i++) + + { + + point p; + + fin >> p.x >> p.y; + + points.push_back(p); + + } + +} + + + +bool lessX(point a, point b){ + + return a.x < b.x; + +} + + + +bool lessY(point a, point b){ + + return a.y < b.y; + +} + + + +double dist(point a, point b){ + + return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); + +} + + + + + +double minDist(int s, int e){ + + if (e - s == 1){ + + + + return dist(points[s], points[e]); + + } + + else if (e - s == 2){ + + + + double d12 = dist(points[s], points[s + 1]); + + double d23 = dist(points[s + 1], points[e]); + + double d13 = dist(points[s], points[e]); + + + + return min(d12, min(d23, d13)); + + } + + else{ + + int mid = (e + s) / 2; + + double d = min(minDist(s, mid), minDist(mid + 1, e)); + + + + vector close; + + + + int i = mid; + + while (i >= s && points[mid + 1].x - points[i].x < d){ + + close.push_back(points[i]); + + i--; + + } + + + + i = mid + 1; + + while (i <= e && points[i].x - points[mid].x <= d){ + + close.push_back(points[i]); + + i++; + + } + + sort(close.begin(), close.end(), lessY); + + + + if (close.size() > 0){ + + + + + + for (int i = 0; i < (int)close.size(); i++) + + { + + for (int j = i+1; j < min(i + 8, (int)close.size()); j++) + + { + + if (abs(close[i].y - close[j].y) <= d){ + + d = min(d, dist(close[i], close[j])); + + } + + } + + } + + } + return d; + + } + +} + + + +int main(){ + + in(); + + points2 = points; + + sort(points.begin(), points.end(), lessX); + + + + int a = 0, b = n-1; + + double d = minDist(a, b); + + + + ofstream f("cmap.out"); + + f << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/divide_conquer/3ab07938-9587-429e-8c7c-e4b7d02be37f.cpp b/cmap/divide_conquer/3ab07938-9587-429e-8c7c-e4b7d02be37f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..594c72b1cf03b8b846736728f2ceec5e50b45d1d --- /dev/null +++ b/cmap/divide_conquer/3ab07938-9587-429e-8c7c-e4b7d02be37f.cpp @@ -0,0 +1,169 @@ +#include +#include + + + + +using namespace __gnu_pbds; +using namespace std; + + + + + + + +typedef pair< int, int > pii; +typedef pair< long long, long long > pll; +typedef long long ll; +typedef unsigned long long ull; +typedef vector< int > vi; +typedef vector< vi > vvi; +typedef vector< ll > vll; +typedef vector< vll > vvll; +typedef vector< pii > vpii; +typedef vector< vpii > vvpii; +typedef vector< pll > vpll; +typedef long double ld; +typedef vector< ld > vld; + +const ll MOD2 = 1e9 + 7; +const int MOD = 1e9 + 7; + +const ull infull = numeric_limits::max(); + +void fix(int &x, ll MOD) { + x = (x % MOD); + if(x < 0) x += MOD; + return; +} + +ll lgput(ll a, ll b, ll MOD) { + ll ret = 1; + a %= MOD; + while(b) { + if(b&1) ret = ret*a % MOD; + a = a*a % MOD; + b >>= 1; + } + + return ret; +} + +ll inv(ll a, ll MOD) { + return lgput(a, MOD-2, MOD); +} + +struct f { + int a, b; + f(int _a = 0, int _b = 0) : a(_a) ,b(_b) {} + ll eval(int x) { + return 1ll*x*a + b; + } +}; + +int binarySearch(int x, vi &v) { + int pos = 0; + int l = 1, r = (int)((v).size()); + while(l <= r) { + int m = l + r >> 1; + if(v[m] <= x) { + pos = m; + l = m + 1; + } else { + r = m - 1; + } + } + + return pos; +} + +struct pt { + int x, y, id; +}; + +struct cmp_x { + bool operator()(const pt & a, const pt & b) const { + return a.x < b.x || (a.x == b.x && a.y < b.y); + } +}; + +struct cmp_y { + bool operator()(const pt & a, const pt & b) const { + return a.y < b.y; + } +}; + +int n; +vector a; + +double mindist; +pair best_pair; + +void upd_ans(const pt & a, const pt & b) { + double dist = sqrt(1ll*(a.x - b.x)*(a.x - b.x) + 1ll*(a.y - b.y)*(a.y - b.y)); + if (dist < mindist) { + mindist = dist; + best_pair = {a.id, b.id}; + } +} +vector t; + +void rec(int l, int r) { + if (r - l <= 3) { + for (int i = l; i < r; ++i) { + for (int j = i + 1; j < r; ++j) { + upd_ans(a[i], a[j]); + } + } + sort(a.begin() + l, a.begin() + r, cmp_y()); + return; + } + + int m = (l + r) >> 1; + int midx = a[m].x; + rec(l, m); + rec(m, r); + + merge(a.begin() + l, a.begin() + m, a.begin() + m, a.begin() + r, t.begin(), cmp_y()); + copy(t.begin(), t.begin() + r - l, a.begin() + l); + + int tsz = 0; + for (int i = l; i < r; ++i) { + if (abs(a[i].x - midx) < mindist) { + for (int j = tsz - 1; j >= 0 && a[i].y - t[j].y < mindist; --j) + upd_ans(a[i], t[j]); + t[tsz++] = a[i]; + } + } +} + +int main() { + + + + + {freopen("cmap"".in","r",stdin);freopen("cmap"".out","w",stdout);}; + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + cout.precision(12); + srand(time(NULL)); + + cin >> n; + a.resize(n); + + for(int i = 0; i < n; ++i) { + cin >> a[i].x >> a[i].y; + } + + t.resize(n); + sort(a.begin(), a.end(), cmp_x()); + mindist = 1E20; + rec(0, n); + + cout << setprecision(12) << fixed << mindist << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/3b1e4057-8825-4b47-86c4-1f2e16e1e05f.cpp b/cmap/divide_conquer/3b1e4057-8825-4b47-86c4-1f2e16e1e05f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8a81ec8c467cac3a6d37ef1eeffc99159cdcb0 --- /dev/null +++ b/cmap/divide_conquer/3b1e4057-8825-4b47-86c4-1f2e16e1e05f.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Point { + long long x, y; + friend bool operator<(const Point &p1, const Point &p2); + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + +bool operator<(const Point &p1, const Point &p2) { + return p1.x < p2.x; +} + +vector read(long long &n) { + ifstream fin("cmap.in"); + vector points; + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); + return points; +} + +void write(int n, vector points) { + for (int i = 1; i <= n; i++) { + cout << points[i].x << " " << points[i].y << "\n"; + } +} + +double distance(Point A, Point B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + if (minDist > distance(points[i], points[j])) { + minDist = distance(points[i], points[j]); + } + } + } + + return minDist; +} + +double divide(int li, int ls, vector &P, vector &Q) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, P); + } + int mid = (li + ls)/2; + vector Ql; + Ql.push_back(Q[0]); + vector Qr; + Qr.push_back(Q[0]); + for (int i = 1; i < Q.size(); i++) { + if (Q[i].x <= P[mid].x) { + Ql.push_back(Q[i]); + } else { + Qr.push_back(Q[i]); + } + } + double dl = divide(li, mid, P, Ql); + double dr = divide(mid + 1, ls, P, Qr); + double dmin = min(dl, dr); + + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = 1; i < Q.size(); i++) { + if (i != mid && abs(P[mid].x - Q[i].x) <= dmin) { + strip.push_back(Q[i]); + } + } + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 8 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + + return dmin; +} + +int main() { + long long n; + vector P; + P = read(n); + vector Q = P; + sort(P.begin() + 1, P.end()); + sort(Q.begin() + 1, Q.end(), Point::byY()); + + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, P, Q); + return 0; +} diff --git a/cmap/divide_conquer/3c861866-b9de-433b-b102-63a35dad878c.cpp b/cmap/divide_conquer/3c861866-b9de-433b-b102-63a35dad878c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d06664d454053578570926b85496e012bb9374d --- /dev/null +++ b/cmap/divide_conquer/3c861866-b9de-433b-b102-63a35dad878c.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); +const int MAXN = 1e5 + 1; +const double INF = 1e12; +const double EPS = 1e-8; +double minim = INF; +struct punct{ + double x, y; +}; +bool cmp(punct a, punct b){ + return a.x + EPS < b.x or (abs(a.x - b.x) <= EPS and a.y + EPS < b.y); +} +bool cmp2(punct a, punct b){ + return a.y + EPS < b.y or (abs(a.y - b.y) <= EPS and a.x + EPS < b.x); +} +double formula(punct a, punct b){ + return sqrt((((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)))); +} +punct v[MAXN]; +punct aux[MAXN]; +punct b[MAXN]; +void interclasare(int st ,int mij ,int dr){ + int indice1 = st - 1, indice2 = mij , indice = st - 1; + while(indice1 < mij and indice2 < dr){ + if(v[indice1 + 1].y < v[indice2 + 1].y) aux[++indice] = v[++indice1]; + else aux[++indice] = v[++indice2]; + } + for(int i = indice1 + 1; i <= mij ; ++i) + aux[++indice] = v[i]; + for(int i = indice2 + 1; i <= dr; ++i) + aux[++indice] = v[i]; + for(int i = st; i <= dr; ++i) + v[i] = aux[i]; +} +void dei(int st, int dr){ + int m = (st + dr) / 2; + if(dr - st <= 2){ + for(int i = st ; i <= dr - 1; ++i){ + for(int j = i + 1; j <= dr; ++j){ + minim = min(minim, formula(v[i], v[j])); + } + } + sort(v + st, v + dr + 1, cmp2); + return; + } + double middleX = v[m].x; + dei(st, m); dei(m + 1, dr); + interclasare(st, m, dr); + int indice = 0; + for(int i = st; i <= dr; ++i) + if(abs(v[i].x - middleX) >= minim) + b[++indice] = v[i]; + + for(int i = 1; i <= indice - 8; ++i) + for(int j = i + 1; j <= min(indice, i + 8) ; ++j) + minim = min(minim, formula(b[i], b[j])); + +} + +int main(){ + ios::sync_with_stdio(false); + fin.tie(0);fout.tie(0); + int n ; + fin >> n; + for(int i = 1; i <= n ;++i) + fin >> v[i].x >> v[i].y; + sort(v + 1, v + n + 1, cmp); + dei(1, n); + fout << setprecision(7) << fixed << minim; + return 0; +} diff --git a/cmap/divide_conquer/3d13217b-b29a-4845-b3e8-583fada95f88.cpp b/cmap/divide_conquer/3d13217b-b29a-4845-b3e8-583fada95f88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6a674762fc104366d864bb82ac5218ba35850a0 --- /dev/null +++ b/cmap/divide_conquer/3d13217b-b29a-4845-b3e8-583fada95f88.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +vector > v; + + + +long long get_distance(pair a,pair b) + +{ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + + + +long long divide(long long st,long long dr) + +{ + + long long m, dist_min; + + + + if(dr-st<=3) + + { + + long long dist = LLONG_MAX ; + + for(int i=st; i<=dr; i++) + + for(int j=i+1; j<=dr; j++) + + dist = min(dist,get_distance(v[i],v[j])); + + + + return dist; + + } + + + + m = st + (dr - st) / 2; + + + + long long dist_s = divide(st,m); + + long long dist_d = divide(m+1,dr); + + + + dist_min = min(dist_s,dist_d); + + + + vector < pair > band; + + for(int i=st; i<=dr; i++) + + if(abs(v[m].first-v[i].first)>n; + + for(int i = 1; i <= n; i++) + + { + + fin>>x>>y; + + v.push_back({x,y}); + + } + + + + sort(v.begin(),v.end()); + + + + dist_min = divide(0,n-1); + + + + fout< +#include +#include +struct Point { + + int x; + + int y; + +}; + + + +int n; + +Point pct[100000]; + +double max; + + + +double calcDist(Point* A, Point* B) { + + double dx = A->x - B->x; + + double dy = A->y - B->y; + + return sqrt(dx*dx + dy*dy); + +} + + + +int comparePointsX(const void * a, const void * b) { + + Point * A = ((Point*)a); + + Point * B = ((Point*)b); + + if (A->x > B->x) return 1; + + if (A->x < B->x) return -1; + + return 0; + +} + + + +int comparePointsY(const void * a, const void * b) { + + Point * A = *((Point**)a); + + Point * B = *((Point**)b); + + if (A->y > B->y) return 1; + + if (A->y < B->y) return -1; + + return 0; + +} + + + +double minDistBrute(int left, int right) { + + double min = max; + + double dist; + + for (int i = left; i < right; i++) { + + for (int j = i + 1; j < right; j++) { + + dist = calcDist(pct + i, pct + j); + + if (dist < min) min = dist; + + } + + } + + return min; + +} + + + +double minDist(int left, int right) { + + int nr = right - left; + + if (nr <= 3) { + + return minDistBrute(left, right); + + } + + int mid = left + (nr/2); + + double min = minDist(left, mid); + + double min2 = minDist(mid, right); + + if (min2 < min) { + + min = min2; + + } + + + + int X = pct[mid].x; + + int j = 0; + + double dist; + + Point** sortY = (Point**)malloc(nr * sizeof(Point*)); + + for (int i = left; i < right; i++) + + { + + if (X - pct[i].x < min && X - pct[i].x > -min) { + + sortY[j++] = pct + i; + + } + + } + + qsort(sortY, j, sizeof(Point*), comparePointsY); + + + + for (int i = 0; i < j; i++) + + { + + for (int k = 1; k < 7 && k+i < j; k++) + + { + + dist = calcDist(sortY[i], sortY[k + i]); + + if (dist < min) min = dist; + + } + + } + + return min; + +} + + + +int main() { + + FILE * fin; + + FILE * fout; + + + + fin = fopen("cmap.in", "r"); + + fout = fopen("cmap.out", "w"); + + + + fscanf(fin, "%d", &n); + + for (int i = 0; i < n; ++i) { + + fscanf(fin, "%d%d", &pct[i].x, &pct[i].y); + + } + + + + qsort(pct, n, sizeof(Point), comparePointsX); + + max = calcDist(pct, pct + n - 1); + + + + fprintf(fout, "%f", minDist(0, n)); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/cmap/divide_conquer/3d9c3453-776c-46a1-8542-f99770a0288f.cpp b/cmap/divide_conquer/3d9c3453-776c-46a1-8542-f99770a0288f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd88b742c7ceb61d1429ccaf6612b86a948ac1f9 --- /dev/null +++ b/cmap/divide_conquer/3d9c3453-776c-46a1-8542-f99770a0288f.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + assert(in >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + assert(in >> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/3e07b2e5-94ae-411c-829e-53fd9f724888.cpp b/cmap/divide_conquer/3e07b2e5-94ae-411c-829e-53fd9f724888.cpp new file mode 100644 index 0000000000000000000000000000000000000000..092fa88f1769396298ae70acd242ff0d682ee276 --- /dev/null +++ b/cmap/divide_conquer/3e07b2e5-94ae-411c-829e-53fd9f724888.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +using namespace std; + + + + + +struct point{ + +int x,y; + +}; + +int n; + +point v[100005],aux[100005]; + +double distp(point p1, point p2){ + +return sqrt((double)((long long)((long long)p1.x-p2.x)*(p1.x-p2.x)+(long long)((long long)p1.y-p2.y)*(p1.y-p2.y)));} + +bool cmp2(point p1, point p2){ + +return p1.x>1; + +if(st>=dr) + +return 1000000000.0; + +if(st==dr-1){ + +if(v[st].y>v[dr].y){ + +point aux; + +aux=v[st]; + +v[st]=v[dr]; + +v[dr]=aux;} + +return distp(v[st],v[dr]);} + +int mij=v[m].x; + +double d1=dist_min(st,m); + +double d2=dist_min(m+1,dr); + +double d=min(d1,d2); + +int i1=st,i2=m+1,i=st-1; + +while(i1<=m && i2<=dr){ + +if(v[i1].y<=v[i2].y) + +aux[++i]=v[i1++]; + +else + +aux[++i]=v[i2++];} + +for(;i1<=m;i1++) + +aux[++i]=v[i1]; + +for(;i2<=dr;i2++) + +aux[++i]=v[i2]; + +int nr=0; + +for(i=st;i<=dr;i++){ + +v[i]=aux[i]; + +if(abs(v[i].x-mij)<=d) + +aux[++nr]=v[i];} + +double mini=1000000000.0; + +int j; + +for(i=1;i<=nr;i++) + +for(j=i-1;j>=i-6 && j;j--) + +mini=min(mini,distp(aux[i],aux[j])); + +return min(mini,d);} + +int main(){ + +freopen("cmap.in","r",stdin); + +freopen("cmap.out","w",stdout); + +scanf("%d",&n); + +int i; + +for(i=1;i<=n;i++) + +scanf("%d%d",&v[i].x,&v[i].y); + +sort(v+1,v+n+1,cmp2); + +double sol=dist_min(1,n); + +printf("%f\n",sol); + +return 0;} diff --git a/cmap/divide_conquer/3e794a7f-d3d1-4fa8-b9aa-c4c3b58aa3bd.cpp b/cmap/divide_conquer/3e794a7f-d3d1-4fa8-b9aa-c4c3b58aa3bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed308af8b7345373b679d13446c0e6084456703a --- /dev/null +++ b/cmap/divide_conquer/3e794a7f-d3d1-4fa8-b9aa-c4c3b58aa3bd.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int NMAX = 1e5 + 5; +const long double INF = 1.0 * 1e10; + +ifstream cin("cmap.in"); +ofstream cout("cmap.out"); + +pair v[NMAX]; + +bool cmp(pair x, pair y) +{ + if(x.second == y.second) + return x.first < y.first; + else + return x.second < y.second; +} + +long double dist(pair x, pair y) +{ + return (long double)sqrt(1.0 * (x.first - y.first) * (x.first - y.first) + 1.0 * (x.second - y.second) * (x.second - y.second)); +} + +vector > a; + +long double solve(int st, int dr) +{ + if(dr - st + 1 <= 3) { + long double dmin = INF; + for(int i = st; i < dr; ++i) { + for(int j = i + 1; j <= dr; ++j) + dmin = min(dmin, dist(v[i], v[j])); + } + return dmin; + } + int med = (st + dr) / 2; + long double dmin = min(solve(st, med), solve(med + 1, dr)); + a.clear(); + for(int i = st; i <= dr; ++i) + if(abs(v[med].first - v[i].first) <= dmin) { + a.emplace_back(v[i]); + } + sort(a.begin(), a.end(), cmp); + for(int i = 0; i < a.size() - 1; ++i) { + for(int j = i + 1; j < a.size() && j - i <= 7; ++j) { + dmin = min(dmin, dist(a[i], a[j])); + } + } + return dmin; +} + +int main() { + int n; + cin >> n; + for(int i = 1; i <= n; ++i) { + cin >> v[i].first >> v[i].second; + } + sort(v + 1, v + n + 1); + cout << fixed << setprecision(6) << solve(1, n) << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/3ebc0237-e9ee-4542-8fd4-b74e38ce9053.cpp b/cmap/divide_conquer/3ebc0237-e9ee-4542-8fd4-b74e38ce9053.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8124e561eca8b4856b822d11ef621db2099f5200 --- /dev/null +++ b/cmap/divide_conquer/3ebc0237-e9ee-4542-8fd4-b74e38ce9053.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); +int n; +vector> x, y, v; + +long long dist(pair p1, pair p2) +{ + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); +} + +long long solve(int st, int dr) +{ + if(st >= dr) + return 0x3f3f3f3f; + if(dr - st == 1) + return dist(x[st], x[st + 1]); + int mid = (st + dr) / 2; + long long best = min(solve(st, mid), solve(mid, dr)); + + sort(y.begin() + st, y.begin() + dr); + + v.clear(); + for(int i = st;i < dr;++i) + if(abs(y[i].second - x[mid].first) <= best) + v.push_back(y[i]); + for(int i = 0;i < v.size() - 1;++i) + for(int j = i + 1;j < v.size() && j - i < 8;++j) + best = min(best, dist(v[i], v[j])); + return best; +} + +void Read() +{ + f>>n; + long long a, b; + for(int i = 1;i <= n;++i) + { + f>>a>>b; + x.push_back(make_pair(a, b)); + } + sort(x.begin(), x.end()); + for(auto it : x) + y.push_back(make_pair(it.second, it.first)); + g< +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream cin ("cmap.in");ofstream cout ("cmap.out"); + + + +struct nod{long long x , y;}; + +bool cmpx (nod a , nod b){return a.x < b.x;} + +bool cmpy (nod a , nod b){return a.y < b.y;} + +nod v[100100] , ord[100100]; + + + +double dist (nod a , nod b){ + + double now = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); + + return sqrt(now); + +} + + + +double solve (int st , int dr){ + + + + double ans = 1e18; + + if (dr-st+1 <= 3){ + + for (int i=st; i<=dr; i++){ + + for (int j=i+1; j<=dr; j++){ + + ans = min(ans , dist(v[i] , v[j])); + + } + + } + + sort (v+st , v+dr+1 , cmpy); + + return ans; + + } + + + + int mij = st + dr; + + mij /= 2; + + long long middle = v[mij].x; + + double low = min(solve(st , mij) , solve(mij+1 , dr)); + + + + int ST = st , DR = mij+1 , pnt = st; + + while (ST <= mij && DR <= dr){ + + if (v[ST].y < v[DR].y){ + + ord[pnt++] = v[ST++]; + + } + + else{ + + ord[pnt++] = v[DR++]; + + } + + } + + for (; ST<=mij; ST++)ord[pnt++]=v[ST]; + + for (; DR<=dr; DR++)ord[pnt++]=v[DR]; + + for (int i=st; i<=dr; i++){v[i] = ord[i];} + + + + pnt = 0; + + for (int i=st; i<=dr; i++){ + + if (abs(v[i].x - middle) <= (long long)low){ + + ord[++pnt]= v[i]; + + } + + } + + + + ans = low; + + for (int i=1; i<=pnt; i++){ + + for (int j=i+1; j<=pnt; j++){ + + if (ord[j].y - ord[i].y > (long long)low){ + + break; + + } + + ans = min(ans , dist(ord[i] , ord[j])); + + } + + } + + + + return ans; + +} + + + + + +int main() { + + + + + + cout << setprecision(10) << fixed; + + + + int n; + + cin>>n; + + + + for (int i=1; i<=n; i++){ + + int a , b; + + cin>>a>>b; + + v[i].x = a; + + v[i].y = b; + + } + + sort(v+1 , v+n+1 , cmpx); + + cout< +#include +#include +#include +#include +#include +#include + + + + + +using namespace std; + + +typedef pair punct; +vector v,vnou; +long long n; +bool cmpy(punct a, punct b){ return a.second > b.second; } +bool cmpx(punct a, punct b){ return a.first > b.first; } +long long dist(punct a, punct b){ return (long long)(a.first-b.first)*(a.first-b.first) + 0LL + (a.second-b.second)*(a.second-b.second); } + +long long divide(long long st, long long dr){ + long long m = (st+dr)/2; + long long n = dr-st+1; + if(n == 1) return 10000000000000; + else if( n==2) return dist(v[st],v[dr]); + else { + long long dmin = min(divide(st,m), divide(m,dr)); + long long mij = (v[st].second + v[dr].second)/2; + vnou.clear(); + for(long long i = st; i<=dr; i++){ + if(mij - dmin <= v[i].second || mij + dmin >= v[i].second ){ + vnou.push_back(v[i]); + } + } + sort(vnou.begin(), vnou.end(), cmpx); + long long dmin_banda = 10000000000000; + if(!vnou.empty()) { + for (long long j = 0; j < vnou.size() - 1; ++j) { + for (long long k = j + 1; k < min((long long) vnou.size(), j + 8); k++) { + dmin_banda = min(dmin_banda, dist(vnou[j], vnou[k])); + } + } + } + return min(dmin,dmin_banda); + + } +} + +int main(){ + FILE *f, *g; + f = fopen("cmap.in","r"); + g = fopen("cmap.out","w"); + fscanf(f,"%lld", &n); + v.resize(n); + for(long long i=0;i +#include +#include +#include +#include +#include + + + + +using namespace std; + +fstream fin("cmap.in",ios::in),fout("cmap.out",ios::out); +vector > v,aux; +int n; + +void citire() +{ + int i,a,b; + fin>>n; + for(i=0;i>a>>b; + v.push_back({a,b}); + } +} +double dist(int ax,int ay,int bx,int by) +{ + long long c,d; + c=(ax-bx);c=c*c; + d=(ay-by);d=d*d; + c=c+d; + return sqrt(c); +} +double solv(int st,int dr) +{ + int m=(st+dr)/2,i,j,d; + double minim=5999999999; + + if(dr-st<=3) + { + for(i=st;i<=dr;i++) + { + for(j=i+1;j<=dr;j++) + { + minim=min(minim,dist(v[i].first,v[i].second,v[j].first,v[j].second)); + } + } + } + else + { + minim=min(solv(st,m),solv(m+1,dr)); + aux.clear(); + d=v[m].first; + for(i=st;i<=dr;i++) + { + if(dist(v[i].first,v[i].second,d,v[i].second)<=minim) + { + aux.push_back(v[i]); + } + } + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +struct punct{ + + double x,y; + +}p[100001],aux[100001],v[100001]; + +int n,i; + +double inf=4e18; + +int comp(punct a,punct b){ + + if (a.x>b.x) return 0; + + if (a.x==b.x && a.y>b.y) return 0; + + return 1; + +} + +double dist(punct a,punct b){ + + return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); + +} + +double exp(int st,int dr){ + + double val1,val; + + int mij,i,j,nraux=0,nrv=0; + + if (st>dr) return inf; + + if (dr-st==1) return dist(p[st],p[st+1]); + + if (dr-st==2){ + + val1=dist(p[st],p[st+1]); + + val1=min(val1,dist(p[st+1],p[st+2])); + + val1=min(val1,dist(p[st],p[st+2])); + + return val1; + + } + + mij=(st+dr)/2; + + val=min(exp(st,mij),exp(mij,dr)); + + i=st;j=mij+1; + + while(i<=mij && j<=dr) + + if (p[i].y<=p[j].y){ + + nraux++; + + aux[nraux]=p[i]; + + i++; + + } + + else + + if (p[i].y>p[j].y){ + + nraux++; + + aux[nraux]=p[j]; + + j++; + + } + + while(i<=mij){ + + nraux++; + + aux[nraux]=p[i]; + + i++; + + } + + while(j<=dr){ + + nraux++; + + aux[nraux]=p[j]; + + j++; + + } + + for (i=1;i<=nraux;i++) + + if (fabs(aux[i].x-p[mij].x)<=val){ + + nrv++; + + v[nrv]=aux[i]; + + } + + for (i=1;i<=nrv;i++) + + for (j=i+1;j<=nrv && j-i<8;j++) + + val=min(val,dist(v[i],v[j])); + + return val; + +} + +int main(){ + + fin>>n; + + for (i=1;i<=n;i++) + + fin>>p[i].x>>p[i].y; + + fin.close(); + + sort(p+1,p+n+1,comp); + + fout< +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int DIM = 1e5 + 5; + + + +int N; + +pair arr[DIM], aux[DIM]; + + + +inline long long dist( pair A, pair B ){ + + return 1LL * (A.first - B.first) * (A.first - B.first) + + + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long solve( int st, int dr ){ + + if( dr - st == 1 ){ + + long long ans = dist( arr[st], arr[dr] ); + + if( arr[st].second > arr[dr].second ) + + swap( arr[st], arr[dr] ); + + return ans; + + } + + if( dr - st == 2 ){ + + long long ans = dist( arr[st], arr[st + 1] ); + + ans = min( ans, dist( arr[st], arr[dr] ) ); + + ans = min( ans, dist( arr[st + 1], arr[dr] ) ); + + if( arr[st].second > arr[st + 1].second ) + + swap( arr[st], arr[st + 1] ); + + if( arr[st].second > arr[dr].second ) + + swap( arr[st], arr[dr] ); + + if( arr[st + 1].second > arr[dr].second ) + + swap( arr[st + 1], arr[dr] ); + + return ans; + + } + + int mid = ( st + dr ) >> 1; + + long long ans = solve( st, mid ); + + ans = min( ans, solve( mid + 1, dr ) ); + + + + int p1 = st, p2 = mid + 1, k = st - 1; + + while( p1 <= mid && p2 <= dr ){ + + if( arr[p1].second < arr[p2].second ) + + aux[++k] = arr[p1++]; + + else + + aux[++k] = arr[p2++]; + + } + + while( p1 <= mid ) + + aux[++k] = arr[p1++]; + + while( p2 <= dr ) + + aux[++k] = arr[p2++]; + + for( int i = st; i <= dr; i++ ) + + arr[i] = aux[i]; + + + + k = 0; + + for( int i = st; i <= dr; i++ ) + + if( (long long)( abs( (arr[mid].first - arr[i].first) ) ) <= ans ) + + aux[++k] = arr[i]; + + + + for( int i = 1; i <= k; i++ ) + + for( int j = i + 1; j <= i + 7 && j <= k; j++ ) + + ans = min( ans, dist( aux[i], aux[j] ) ); + + + + return ans; + +} + + + + + +int main(){ + + fin >> N; + + for( int i = 1; i <= N; i++ ) + + fin >> arr[i].first >> arr[i].second; + + sort( arr + 1, arr + N + 1 ); + + fout << setprecision(6) << fixed << sqrt( 1.0 * solve( 1, N ) ) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/4655dad4-e0c8-4f4c-83d4-d542da884563.cpp b/cmap/divide_conquer/4655dad4-e0c8-4f4c-83d4-d542da884563.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3d92ce751b817e30928e98333ba822f27f7838b --- /dev/null +++ b/cmap/divide_conquer/4655dad4-e0c8-4f4c-83d4-d542da884563.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + +const long long NMAX = 100005; + +const double INF = 4e18; + + + +vector< pair > Points; + +vector< pair > PointsOY; + +vector< pair > sol(NMAX); + + + +long long returnDist(pair& P1,pair& P2) + +{ + + return (P2.first - P1.first) * (P2.first - P1.first) + + + (P2.second - P1.second) * (P2.second - P1.second); + +} + + + +long long d; + + + +long long divideClosPoints(long long left, long long right) + +{ + + if( left >= right -1) + + return INF; + + else if(right - left == 2) + + { + + if(PointsOY[left].second == PointsOY[left + 1].second) + + if(PointsOY[left].first > PointsOY[left + 1].first) + + swap(PointsOY[left],PointsOY[left + 1]); + + else + + if(PointsOY[left].second > PointsOY[left + 1].second) + + swap(PointsOY[left],PointsOY[left + 1]); + + return returnDist(Points[left], Points[left + 1]); + + } + + + + long long middle = (left + right) / 2; + + d = min(divideClosPoints(left, middle),divideClosPoints(middle, right)); + + int i, j; + + + + merge(PointsOY.begin() + left, PointsOY.begin() + middle, PointsOY.begin() + middle, PointsOY.begin() + right, sol.begin()); + + copy(sol.begin(), sol.begin() + (right - left), PointsOY.begin() + left); + + + + + + int k = 0; + + for(i = left; i < right; ++i) + + { + + if(abs(PointsOY[i].second - Points[middle].first) <= d) + + sol[k++] = PointsOY[i]; + + } + + for(i = 0; i < k - 1 ; ++i) + + for(j = i + 1; j - i < 8 && j < k ; ++j) + + { + + d = min(d, returnDist(sol[i],sol[j])); + + } + + return d; + +} + + + +long long x, y; + + + +int main() + +{ + + ifstream fileRead("cmap.in"); + + ofstream fileWrite("cmap.out"); + + long long nrPoints, i; + + + + fileRead >> nrPoints; + + Points.resize(nrPoints); + + PointsOY.resize(nrPoints); + + + + for(i = 0 ; i < nrPoints; i++) + + { + + fileRead >> Points[i].first >> Points[i].second; + + } + + + + sort(Points.begin(),Points.end()); + + + + for(i = 0 ; i < nrPoints; i++) + + { + + PointsOY[i] = make_pair(Points[i].first,Points[i].second); + + } + + + + fileWrite << fixed << setprecision(6) << sqrt(divideClosPoints(0,Points.size())); + + + + fileRead.close(); + + fileWrite.close(); + +} diff --git a/cmap/divide_conquer/47d93d37-cdf8-48f4-857a-e78dd00da43b.cpp b/cmap/divide_conquer/47d93d37-cdf8-48f4-857a-e78dd00da43b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b8e503bec0527aad998b5128d84068037448b5a --- /dev/null +++ b/cmap/divide_conquer/47d93d37-cdf8-48f4-857a-e78dd00da43b.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin ( "cmap.in" ); + +ofstream fout ( "cmap.out" ); + + + +pair v[1 + 100000], p[1 + 100000]; + + + +double minim ( double a, double b ) { + + return ( a < b ) ? a : b; + +} + + + +double dist ( pair a, pair b ) { + + return sqrt ( ( double ) ( a.first - b.first ) * ( a.first - b.first ) + ( double ) ( a.second - b.second ) * ( a.second - b.second ) ); + +} + + + +double cmp ( pair a, pair b ) { + + return a.second < b.second; + +} + + + +double sol ( int st, int dr ) { + + + + if ( dr - st + 1 <= 3 ) { + + sort ( v + st, v + dr + 1, cmp ); + + + + double ans = INFINITY; + + + + for ( int i = st; i <= dr; ++ i ) + + for ( int j = i + 1; j <= dr; ++ j ) + + ans = minim ( ans, dist ( v[i], v[j] ) ); + + + + return ans; + + } + + + + int mid = ( st + dr ) / 2; + + int xmid = v[mid].first; + + + + double ans = minim ( sol ( st, mid ), sol ( mid + 1, dr ) ); + + + + int ind = st; + + int i = st, j = mid + 1; + + + + while ( i <= mid && j <= dr ) { + + if ( v[i].second < v[j].second ) + + p[ind ++] = v[i ++]; + + else + + p[ind ++] = v[j ++]; + + } + + + + for ( ; i <= mid; ++ i ) p[ind ++] = v[i]; + + + + for ( ; j <= dr; ++ j ) p[ind ++] = v[j]; + + + + for ( int i = st; i <= dr; ++ i ) + + v[i] = p[i]; + + + + ind = 0; + + + + for ( int i = st; i <= dr; ++ i ) + + if ( fabs ( v[i].first - xmid ) < ans ) + + p[ind++] = v[i]; + + + + for ( int i = 0; i < ind; ++ i ) + + for ( int j = i + 1; j <= i + 7 && j < ind; ++ j ) + + ans = min ( ans, dist ( p[i], p[j] ) ); + + + + return ans; + + + +} + + + +int main() { + + + + int N; + + + + fin >> N; + + + + for ( int i = 1; i <= N; ++i ) + + fin >> v[i].first >> v[i].second; + + + + fout << setprecision(8) << fixed; + + + + sort( v + 1, v + N + 1 ); + + fout << sol (1, N); + + + + return 0; + +} diff --git a/cmap/divide_conquer/47fa8247-f369-4d5d-90a6-17b856c00087.cpp b/cmap/divide_conquer/47fa8247-f369-4d5d-90a6-17b856c00087.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1432ed85e9b42d0f3695cca7e7472490bddfc6bb --- /dev/null +++ b/cmap/divide_conquer/47fa8247-f369-4d5d-90a6-17b856c00087.cpp @@ -0,0 +1,122 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +long long n,i; + +pair v[100010],w[100010],q[100010]; + +long long dist(pair a,pair b) { + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + +void interclasare(int st,int mid,int dr) { + + int i=st, j=mid+1, k=0; + + while (i<=mid&&j<=dr) { + + if (v[i].second>n; + + for (i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + assert(in >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + assert(in >> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/48c88eb9-fba6-4835-8d44-5c4d39c11ee4.cpp b/cmap/divide_conquer/48c88eb9-fba6-4835-8d44-5c4d39c11ee4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1e2748da4345e9b4541e1d8d748ac5c163429ca --- /dev/null +++ b/cmap/divide_conquer/48c88eb9-fba6-4835-8d44-5c4d39c11ee4.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const long long INF = 4e18; + +int n; +vector > v, w; +vector > m, aux; + +long long get_dist(pair a, pair b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long dei(int lt, int rt) { + if (lt == rt) { + return INF; + } else if (rt - lt == 1) { + return get_dist(v[lt], v[rt]); + } else if (rt - lt == 2) { + return min(get_dist(v[lt], v[rt]), min(get_dist(v[lt], v[lt + 1]), get_dist(v[lt + 1], v[rt]))); + } + + int mid = (lt + rt) >> 1; + long long sol = min(dei(lt, mid), dei(mid, rt)); + + m.clear(); + m.resize(rt + 1 - lt + 1); + aux.clear(); + merge(w.begin() + lt, w.begin() + mid, w.begin() + mid, w.begin() + rt + 1, m.begin()); + + for (vector >::iterator it = m.begin(); it != m.end(); ++it) { + if (abs(it->second - v[mid].first) <= sol) { + aux.push_back(make_pair(it->second, it->first)); + } + } + + for (int i = 0; i < aux.size(); ++i) { + for (int j = i + 1; j < aux.size() && j - i < 8; ++j) { + sol = min(sol, get_dist(aux[i], aux[j])); + } + } + + return sol; +} + +int main() { + fin >> n; + for (int i = 1; i <= n; ++i) { + long long x, y; + fin >> x >> y; + v.push_back(make_pair(x, y)); + } + for (int i = 1; i <= n; ++i) { + w.push_back(make_pair(v[i - 1].second, v[i - 1].first)); + } + + sort(v.begin(), v.end()); + w.push_back(make_pair(INF, INF)); + sort(w.begin(), w.end()); + + long long sol = dei(0, v.size() - 1); + fout << fixed << setprecision(6) << sqrt((double)sol); + + fout << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/49748a20-9e29-4f6d-bdac-a3c6cff2e09c.cpp b/cmap/divide_conquer/49748a20-9e29-4f6d-bdac-a3c6cff2e09c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20315993d7d2a18f4146b86af0faca0368e82c0d --- /dev/null +++ b/cmap/divide_conquer/49748a20-9e29-4f6d-bdac-a3c6cff2e09c.cpp @@ -0,0 +1,196 @@ +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int nmax = 100005; + + + +int n, auxSize; + + + +struct Point{ + + long long x, y; + +}v[nmax], aux[nmax]; + + + +bool cmp(Point a, Point b){ + + if (a.x == b.x){ + + return a.y > b.y; + + } + + return a.x < b.x; + +} + + + +void Merge(int st, int dr){ + + int mid = (st + dr) / 2; + + int i = st, j = mid + 1; + + auxSize = 0; + + while (i <= mid && j <= dr){ + + if (v[i].y < v[j].y){ + + aux[++auxSize] = v[i++]; + + } + + else{ + + aux[++auxSize] = v[j++]; + + } + + } + + while (i <= mid){ + + aux[++auxSize] = v[i++]; + + } + + while (j <= dr){ + + aux[++auxSize] = v[j++]; + + } + + for (i = st, j = 1; i <= dr; ++i, ++j){ + + v[i] = aux[j]; + + } + +} + + + +long long GetDist(Point a, Point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long GetMinDist(int st, int dr){ + + if (dr - st + 1 <= 3){ + + if (dr - st + 1 == 3){ + + Merge(st + 1, dr); + + Merge(st, dr); + + return min(GetDist(v[st], v[st + 1]), min(GetDist(v[st], v[dr]), GetDist(v[st + 1], v[dr]))); + + } + + else if (dr - st + 1 == 2){ + + Merge(st, dr); + + return GetDist(v[st], v[dr]); + + } + + } + + int mid = (st + dr) / 2; + + int midX = v[mid].x; + + long long left = GetMinDist(st, mid); + + long long right = GetMinDist(mid + 1, dr); + + long long minim = min(left, right); + + Merge(st, dr); + + auxSize = 0; + + for (int i = st; i <= dr; ++i){ + + if (1LL * (midX - v[i].x) * (midX - v[i].x) <= minim){ + + aux[++auxSize] = v[i]; + + } + + } + + for (int i = 1; i <= auxSize; ++i){ + + for (int j = i + 1; j - i + 1 <= 9 && j <= auxSize; ++j){ + + minim = min(1LL * minim, 1LL * GetDist(aux[i], aux[j])); + + } + + } + + return minim; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmp); + + for (int i = 1; i < n; ++i){ + + if (v[i].x == v[i + 1].x && v[i].y == v[i + 1].y){ + + fout << 0; + + return 0; + + } + + } + + long long ans = GetMinDist(1, n); + + fout< +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct Punct{ + + int x,y; + +}; + +Punct coord[100001],Banda[100001]; + +bool compx(const Punct &A,const Punct &B){ + + return A.x>n; + + for(int i=1;i<=n;i++) + + f>>coord[i].x>>coord[i].y; + + sort(coord+1,coord+n+1,compx); + + double d=sqrt(Divide(1,n)); + + g< +#include #include +#include +#include +using namespace std; + + + +FILE *in = fopen("cmap.in", "r"); + +FILE *out = fopen("cmap.out", "w"); + + + +const int maxN = 1e5 + 5; + +const long long inf = 1e18; + +const int maxDiff = 8; + + + +int n; + + pair pts[maxN], aux[maxN]; + + + +void citire() { + + fscanf(in,"%d", &n); + + for(int i = 0; i < n; i++) { + + fscanf(in,"%d%d", &pts[i].first, &pts[i].second); + + } + +} + + + +void Merge(int left, int mid, int right) { + + int p1 = left, p2 = mid + 1, pos = 0; + + + + while(p1 <= mid && p2 <= right) + + { + + if(pts[p1].second < pts[p2].second) + + { + + aux[pos] = pts[p1]; p1++; pos++; + + } else + + { + + aux[pos] = pts[p2]; p2++; pos++; + + } + + } + + + + while(p1 <= mid) + + { + + aux[pos] = pts[p1]; p1++; pos++; + + } + + + + while(p2 <= right) + + { + + aux[pos] = pts[p2]; p2++; pos++; + + } + + + + for(int i = 0; i < pos; i++) { + + pts[left + i] = aux[i]; + + } + +} + + + +long long dist( pair A, pair B) { + + return 1LL * (A.first - B.first) * (A.first - B.first) + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long divide(int left, int right) { + + if(right - left < 2) + + { + + if(right == left) + + return inf; + + else { + + Merge(left, left, right); + + return dist(pts[left], pts[right]); + + } + + } + + + + int mid = (left + right) / 2; + + long long d1 = divide(left, mid), d2 = divide(mid + 1, right); + + + + long long d = min(d1, d2); + + Merge(left, mid, right); + + + + for(int i = left; i <= right; i++) + + for(int j = i - 1; j >= left && i - j < maxDiff; j--) + + d = min(d, dist(pts[i], pts[j])); + + + + return d; + +} + + + +int main() { + + citire(); + + + + sort(pts, pts + n); + + + + fprintf(out,"%.7f\n", sqrt((double)divide(0, n - 1))); + + + + return 0; + +} diff --git a/cmap/divide_conquer/4bb30989-264d-4e2d-9fe3-374d89c78b81.cpp b/cmap/divide_conquer/4bb30989-264d-4e2d-9fe3-374d89c78b81.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75843458f362961b3f40d200754bb540877fcffe --- /dev/null +++ b/cmap/divide_conquer/4bb30989-264d-4e2d-9fe3-374d89c78b81.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +vector < pair > banda(100001), X, Y; + + + +long long distanta(pair &a,pair &b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} +long long divide(int S, int D) + +{ + + if (S >= D-1) + + { + + if (S >= D - 1)return LLONG_MAX; + + } + + else + + { + if (D - S == 2) + + { + + if (Y[S] > Y[S + 1]) + + swap(Y[S], Y[S + 1]); + + return distanta(X[S], X[S + 1]); + + } + + } + + + + int mij = (S + D) / 2; + + long long dist = min(divide(S, mij), divide(mij, D)); + + + + merge(Y.begin() + S, Y.begin() + mij, Y.begin() + mij, Y.begin() + D, banda.begin()); + + copy(banda.begin(), banda.begin() + (D - S), Y.begin() + S); + + + + int k = 0; + + for (int i = S; i < D; i++ ) + + if (abs(Y[i].second - X[mij].first) <= dist) + + banda[k ++] = Y[i]; + + + + for (int i = 0; i < k - 1; i ++) + + { + + for (int j = i + 1; j < k && j - i < 8; j ++) + + dist = min(dist, distanta(banda[i], banda[j])); + + } + + return dist; + +} + + + +int main() + +{ + + int n; + + + + f>>n; + + X.resize(n),Y.resize(n); + + for (int i = 0; i <(int) X.size(); i++) + + f >> X[i].first >> X[i].second; + + + + sort(X.begin(), X.end()); + + for (int i = 0; i <(int) X.size(); i++) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + g << fixed << setprecision(6) << sqrt(divide(0,X.size())) << "\n"; + + g.close(); + + f.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/4c75d9f5-b02f-4751-9ce8-b72702820b88.cpp b/cmap/divide_conquer/4c75d9f5-b02f-4751-9ce8-b72702820b88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6eb1a09edf89e3f2ef7c4ea48052ea18dde819f --- /dev/null +++ b/cmap/divide_conquer/4c75d9f5-b02f-4751-9ce8-b72702820b88.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +using namespace std; + + + +pair v[100010]; + +vector p; + + + +long long dist (int st, int dr) + +{ + + return (1LL * (v[dr].first - v[st].first) * (v[dr].first - v[st].first) + 1LL * (v[dr].second - v[st].second) * (v[dr].second - v[st].second)); + +} + + + +void del () + +{ + + vector aux; + + aux.swap (p); + +} + + + +long long aabs (int a) + +{ + + return 1LL * max (a, -a); + +} + + + +long long divide (int st, int dr) + +{ + + if (dr - st + 1 <= 3) + + { + + if (dr - st + 1 == 2) return dist (st, dr); + + else return min (min (dist (st, st + 1), dist (st, st + 2)), dist (st + 1, st + 2)); + + } + + + + int mij = (st + dr) >> 1; + + long long bst = min (divide (st, mij), divide (mij + 1, dr)); + + + + del (); + + for (int i = st; i <= dr; ++i) + + if (aabs (v[mij].first - v[i].first) <= bst) p.push_back (i); + + + + for (int i = 0; i < p.size (); ++i) + + for (int j = i + 1; j <= i + 5 && j < p.size (); ++j) + + bst = min (bst, dist (p[i], p[j])); + + + + return bst; + +} + + + +int main () + +{ + + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d", &n); + + + + for (int i = 1; i <= n; ++i) + + scanf ("%d %d", &v[i].first, &v[i].second); + + + + sort (v + 1, v + n + 1); + + + + printf ("%.8f\n", sqrt (1LL * divide (1, n))); + + + + return 0; + +} diff --git a/cmap/divide_conquer/4d7d0fa4-1c80-4e21-9651-2e922b46ec00.cpp b/cmap/divide_conquer/4d7d0fa4-1c80-4e21-9651-2e922b46ec00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64a92f438ed3036045de03e4d3193a43e82c31db --- /dev/null +++ b/cmap/divide_conquer/4d7d0fa4-1c80-4e21-9651-2e922b46ec00.cpp @@ -0,0 +1,215 @@ +#include #include +#include #include +#include +#include + + +using namespace std; + + + +struct punct + +{ + + punct(int x = 0, int y = 0) + + { + + this->x = x; + + this->y = y; + + } + + int x, y; + +}; + + + +bool cmp_x(const punct &a, const punct &b) + +{ + + if(a.x == b.x) + + return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmp_y(const punct &a, const punct &b) + +{ + + if(a.y == b.y) + + return a.x < b.x; + + return a.y < b.y; + +} + + + +long double dist(const punct &a, const punct &b) + +{ + + int difX = a.x - b.x; + + int difY = a.y - b.y; + + return sqrt(1LL * difX * difX + 1LL * difY * difY); + +} + + + +class cmap + +{ + +public: + + void AddPoint(int x, int y) + + { + + v.push_back(punct(x, y)); + + } + + pair > GetCmap() + + { + + sort(v.begin(), v.end(), cmp_x); + + return GetCmap(0, v.size() - 1); + + } + +private: + + pair > GetCmap(int first, int last) + + { + + int nrPoints = last - first + 1; + + vector y_sorted(nrPoints); + + if(nrPoints <= 3) + + { + + copy(v.begin() + first, v.begin() + last + 1, y_sorted.begin()); + + sort(y_sorted.begin(), y_sorted.end(), cmp_y); + + long double ret = dist(v[first], v[first+1]); + + for(int i = first; i < last; ++i) + + for(int j = i + 1; j <= last; ++j) + + ret = min(ret, dist(v[i], v[j])); + + return make_pair(ret, y_sorted); + + } + + int mid = (first + last) / 2; + + pair > retSt = GetCmap(first, mid); + + pair > retDr = GetCmap(mid+1, last); + + + + long double ret = min(retSt.first, retDr.first); + + merge(retSt.second.begin(), retSt.second.end(), + + retDr.second.begin(), retDr.second.end(), + + y_sorted.begin(), cmp_y); + + + + vector utile; + + for(auto &p:y_sorted) + + if(abs(p.x - v[mid].x) <= ret) + + utile.push_back(p); + + + + for(int i = 0; i < utile.size(); ++i) + + for(int j = i + 1; j < utile.size(); ++j) + + { + + if(j - i >= 8) + + break; + + ret = min(ret, dist(utile[i], utile[j])); + + } + + + + return make_pair(ret, y_sorted); + + } + + vector v; + +}; + + + +int main() + +{ + + ifstream in("cmap.in"); + + cmap puncte; + + int n, x, y; + + in >> n; + + while(n--) + + { + + in >> x >> y; + + puncte.AddPoint(x, y); + + } + + in.close(); + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(8) << puncte.GetCmap().first; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/4d90279e-5304-433b-8de4-5ac10a3d9992.cpp b/cmap/divide_conquer/4d90279e-5304-433b-8de4-5ac10a3d9992.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25c9d8d376771ed05351330a1599edf7ed1e6fd5 --- /dev/null +++ b/cmap/divide_conquer/4d90279e-5304-433b-8de4-5ac10a3d9992.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include + + + + + + + +typedef long long int64; +typedef std::pair Point; + + +int64 distance(const Point& a, const Point &b) +{ + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +int64 cmap(const std::vector &points, std::vector &pointsY, int left, int right) +{ + if (right - left == 2) { + if (pointsY[left] > pointsY[left+1]) { + std::swap(pointsY[left], pointsY[left+1]); + } + return distance(points[left], points[left+1]); + } + if (right - left <= 1) { + return (4e18L); + } + + int mid = (left + right) / 2; + + int64 minLeft = cmap(points, pointsY, left, mid); + int64 minRight = cmap(points, pointsY, mid, right); + + int64 minDist = minLeft; + if (minRight < minDist) minDist = minRight; + + std::vector cand(right - left); + std::merge(pointsY.begin() + left, pointsY.begin() + mid, pointsY.begin() + mid, pointsY.begin() + right, cand.begin()); + std::copy(cand.begin(), cand.begin() + (right - left), pointsY.begin() + left); + + int el = 0; + for (int i = left; i < right; ++ i) { + if (abs(pointsY[i].second - points[mid].first) <= minDist) { + cand[el++] = pointsY[i]; + } + } + + for (int i = 0; i < el; ++ i) { + for (int j = i+1; j < el && j - i < 8; ++ j) { + int64 dist = distance(cand[i], cand[j]); + if (minDist > dist) { + minDist = dist; + } + } + } + + return minDist; +} + +int main (int argc, char const *argv[]) +{ + std::ifstream in("cmap.in"); + + int N; + in >> N; + + std::vector points(N); + std::vector pointsY(N); + for (int i = 0; i < N; ++ i) { + int64 first, second; + in >> first >> second; + points[i].first = first; + points[i].second = second; + } + + in.close(); + + std::sort(points.begin(), points.end(), [](const Point& a, const Point &b) { + if (a.first == b.first) + return a.second < b.second; + return a.first < b.first; + }); + int cnt = 0; + for (auto &a : points) { + pointsY[cnt].second = a.first; + pointsY[cnt++].first = a.second; + } + + std::ofstream out("cmap.out"); + + out << std::fixed << std::setprecision(6) << sqrt(cmap(points, pointsY, 0, N)) << "\n"; + + out.close(); + + return 0; +} diff --git a/cmap/divide_conquer/4f8c565a-d73b-43b9-bfff-efa815f9dea6.cpp b/cmap/divide_conquer/4f8c565a-d73b-43b9-bfff-efa815f9dea6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47ad86415fb58b481c2096e6f8058ddfece2ed06 --- /dev/null +++ b/cmap/divide_conquer/4f8c565a-d73b-43b9-bfff-efa815f9dea6.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + +struct punct{ + + long long x; + + long long y; + +}; + +punct v[100005],w[100005],t[100005]; + +int n; + +long long modul (long long x){ + + if (x<0) + + return -x; + + return x; + +} + +int cmp (punct a,punct b){ + + if (a.x!=b.x) + + return a.x +using namespace std; + + + +typedef int64_t i64; + +typedef pair Point; + + + +const int NMAX = 100010; + +const i64 inf = 4e18; + + + +int N; + +Point X[NMAX], Y[NMAX], aux[NMAX]; + + + +inline i64 squareDist(const Point &A, const Point &B) { + + return 1ll * (A.first - B.first) * (A.first - B.first) + 1ll * (A.second - B.second) * (A.second - B.second); + +} + + + +struct CompareY { + + bool operator()(const Point &lhs, const Point &rhs) const { + + return lhs.second < rhs.second ? 1 : (lhs.second == rhs.second ? lhs.first < rhs.first : 0); + + } + +}; + + + +i64 computeMinDist(int left, int right) { + + if (left == right) + + return inf; + + if (right - left == 1) { + + if (Y[left].second > Y[right].second) + + swap(Y[left], Y[right]); + + return squareDist(X[left], X[right]); + + } + + + + int mid = (left + right) / 2; + + i64 minSqrDist = min(computeMinDist(left, mid), computeMinDist(mid + 1, right)); + + + + merge(Y + left, Y + mid + 1, Y + mid + 1, Y + right + 1, aux, CompareY()); + + memcpy(Y + left, aux, (right - left + 1) * sizeof(Point)); + + + + int pos = 0; + + for (int i = left; i <= right; ++i) + + if (1ll * (X[mid].first - Y[i].first) * (X[mid].first - Y[i].first) <= minSqrDist) + + aux[pos++] = Y[i]; + + + + for (int i = 0; i < pos; ++i) + + for (int j = i + 1; j < i + 8 && j < pos; ++j) + + minSqrDist = min(minSqrDist, squareDist(aux[i], aux[j])); + + + + return minSqrDist; + +} + + + +int main() { + + assert(freopen("cmap.in", "r", stdin)); + + assert(freopen("cmap.out", "w", stdout)); + + + + int i; + + + + cin >> N; + + for (i = 1; i <= N; ++i) + + cin >> X[i].first >> X[i].second; + + sort(X + 1, X + N + 1); + + memcpy(Y, X, sizeof Y); + + + + cout << fixed << setprecision(6) << sqrt(computeMinDist(1, N)) << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/4ffadb89-bfa5-40c6-9678-31d44f37f331.cpp b/cmap/divide_conquer/4ffadb89-bfa5-40c6-9678-31d44f37f331.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d8d2f96a69e9148fc2a13a0943d969183c89480 --- /dev/null +++ b/cmap/divide_conquer/4ffadb89-bfa5-40c6-9678-31d44f37f331.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + +int n; + + + +struct pct + +{ + + int x,y; + +} a[100001],b[100001],c[100001]; + + + + + +int f1(pct x,pct y) + +{ + + return (x.xb[lf].y)swap(b[li],b[lf]); + + return dist(a[li],a[lf]); + + } + + if(lf-li==2) + + { + + double q=dist(a[li],a[li+1]); + + double w=dist(a[li+1],a[li+2]); + + double r=dist(a[li],a[li+2]); + + if(wb[li+1].y)swap(b[li],b[li+1]); + + if(b[li].y>b[li+2].y)swap(b[li],b[li+2]); + + if(b[li+1].y>b[li+2].y)swap(b[li+1],b[li+2]); + + return r; + + } + + double d,d1; + + int m,i,np=0,xd; + + m=(li+lf)/2; + + d=dmin(li,m); + + d1=dmin(m+1,lf); + + if(d1 +using namespace std; + + + +ifstream f ( "cmap.in" ); + +ofstream g ( "cmap.out" ); + +const int MAX_N = 100005; + +const long long INF = 4e18; + + + +vector < pair > V ( MAX_N ), X, Y; + + + +long long dist ( const pair& a, const pair& b ) + +{ + + return ( a.first - b.first ) * ( a.first - b.first ) + ( a.second - b.second ) * ( a.second - b.second ); + +} + +long long go ( int st, int dr, vector >& X, vector < pair >& Y ) + +{ + + if ( st >= dr - 1 ) + + return INF; + + else + + if ( dr - st == 2 ) + + { + + if ( Y[st] > Y[st + 1] ) + + swap ( Y[st], Y[st + 1] ); + + + + return dist ( X[st], X[st + 1] ); + + } + + + + int mid = ( st + dr ) / 2; + + long long best = min ( go ( st, mid, X, Y ), go ( mid, dr, X, Y ) ); + + merge ( Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin() ); + + copy ( V.begin(), V.begin() + ( dr - st ), Y.begin() + st ); + + int v_size = 0; + + + + for ( int i = st; i < dr; ++ i ) + + if ( abs ( Y[i].second - X[mid].first ) <= best ) + + V[v_size ++] = Y[i]; + + + + for ( int i = 0; i < v_size - 1; ++ i ) + + { + + for ( int j = i + 1; j < v_size && j - i < 8; ++ j ) + + best = min ( best, dist ( V[i], V[j] ) ); + + } + + + + return best; + +} + + + +int main () + +{ + + int n; + + f >> n; + + X.resize ( n ), Y.resize ( n ); + + + + for ( int i = 0; i < n; ++ i ) + + f >> X[i].first >> X[i].second; + + + + sort ( X.begin(), X.end() ); + + + + for ( int i = 0; i < n; ++ i ) + + Y[i] = make_pair ( X[i].second, X[i].first ); + + + + g << fixed << setprecision ( 6 ) << sqrt ( go ( 0, X.size(), X, Y ) ) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/503dd7a8-4f06-47c6-9808-3df03a0052fc.cpp b/cmap/divide_conquer/503dd7a8-4f06-47c6-9808-3df03a0052fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7107569a489f1aa132b7879ab96bd347b3d2305e --- /dev/null +++ b/cmap/divide_conquer/503dd7a8-4f06-47c6-9808-3df03a0052fc.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +pair w[100010],aux[100010],v[100010]; + +int n,i; + +long long global=(1LL<<60); + +long long dist(pair a, pair b) + +{ + + return 1LL*(a.first-b.first)*(a.first-b.first)+ + + 1LL*(a.second-b.second)*(a.second-b.second); + +} + +void msort(int st,int m,int dr) + +{ + + int i=st,j=m+1,k=st-1; + + for(; i<=m&&j<=dr;) + + { + + if(v[i].second>n; + + for(i=1; i<=n; i++) + + { + + fin>>v[i].first>>v[i].second; + + } + + sort(v+1,v+n+1); + + fout< +#include +#include + + +using namespace std; + + + +int a[100000001],b[100000001],c[100000001],d[100000001]; + + + +void intercl(int li,int lf) + +{ + + int m=(li+lf)/2,i,j,k; + + i=li;j=m+1;k=li; + + while(i<=m&&j<=lf) + + if(a[i] +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct { + + int x=0; + + int y=0; + +}; + + + +double distEuclid(punct a, punct b) { + + double x = a.x - b.x; + + double y = a.y - b.y; + + double dist; + + + + dist = pow(x, 2) + pow(y, 2); + + dist = sqrt(dist); + + + + return dist; + +} + + + +bool cmpDupaX(const punct& a, const punct& b) + +{ + + return a.x < b.x; + +} + + + +bool cmpDupaY(const punct& a, const punct& b) + +{ + + return a.y < b.y; + +} + + + + + +double distMinPunctePlanRec(vector & puncte, int st, int dr, vector &puncteY) { + + if (dr-st == 1) { + + return 2147483647; + + } + + if (dr-st == 2) { + + if (puncteY[st].y > puncteY[st+1].y) + + swap(puncteY[st], puncteY[st+1]); + + return distEuclid(puncte[0], puncte[1]); + + } + + + + int m = (st + dr) / 2; + + double distStanga = distMinPunctePlanRec(puncte, st, m, puncteY); + + double distDreapta = distMinPunctePlanRec(puncte, m, dr, puncteY); + + double distMinStDr = min(distStanga, distDreapta); + + + + + + vector puncteAux(dr-st); + + merge(puncteY.begin() + st, puncteY.begin() + m, puncteY.begin() + m, puncteY.begin() + dr, puncteAux.begin(),cmpDupaY); + + copy(puncteAux.begin(), puncteAux.begin() + (dr - st), puncteY.begin() + st); + + + + + + vector banda; + + for (int i = st; i < dr; ++i) { + + if (abs(puncteY[i].x - puncte[m].x) <= distMinStDr) + + banda.push_back(puncteY[i]); + + } + + + + double distMinOverall = distMinStDr; + + for (int i=0; i> n; + + + + vector puncte(n); + + for (int i = 0; i < n; ++i) { + + in >> puncte[i].x; + + in >> puncte[i].y; + + } + + + + sort(puncte.begin(), puncte.end(), cmpDupaX); + + + + vector puncteY = puncte; + + + + out << fixed; + + out << setprecision(6) << distMinPunctePlanRec(puncte, 0, puncte.size(), puncteY) << "\n"; + +} + + + +int main() + +{ + + distMinPunctePlan(); + + return 0; + +} diff --git a/cmap/divide_conquer/53c4ad9a-fada-423f-a8d4-a3c99e24aece.cpp b/cmap/divide_conquer/53c4ad9a-fada-423f-a8d4-a3c99e24aece.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbfd6d6fcbcec3f38bbc3b66dba77a0d5880b154 --- /dev/null +++ b/cmap/divide_conquer/53c4ad9a-fada-423f-a8d4-a3c99e24aece.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const long long INF = 4e18; + + + +struct point + +{ + + int x, y; + +}; + + + +int n; + + + +vector < point > p; + + + +bool cmp(point a, point b) + +{ + + if (a.x < b.x) + + return true; + + + + if (a.x == b.x && a.y < b.y) + + return true; + + + + return false; + +} + + + +bool cmpAux(point a, point b) + +{ + + if (a.y < b.y) + + return true; + + + + if (a.y == b.y && a.x < b.x) + + return true; + + + + return false; + +} + + + +double dist(point a, point b) + +{ + + long long dx = a.x - b.x; + + long long dy = a.y - b.y; + + return sqrt(dx * dx + dy * dy); + +} + + + +double distMin(vector < point > &p, int st, int dr) + +{ + + if (dr - st <= 2) + + { + + double dmin = INF; + + + + for (int i=st; i aux; + + + + for (int i=st; i<=dr; i++) + + if (abs(D - p[i].x) <= dmin) + + aux.push_back(p[i]); + + + + sort(aux.begin(), aux.end(), cmpAux); + + + + int n = aux.size(); + + for (int i=0; i> n; + + + + p.resize(n); + + + + for (int i=0; i> p[i].x >> p[i].y; + + + + sort(p.begin(), p.end(), cmp); + + + + g << fixed << setprecision(6) << distMin(p, 0, n - 1); + + + + return 0; + +} diff --git a/cmap/divide_conquer/53dc7f82-84a6-485f-b771-e13d8f1b2c28.cpp b/cmap/divide_conquer/53dc7f82-84a6-485f-b771-e13d8f1b2c28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f57ed86dea3216624994d06d370bd622762b7fc9 --- /dev/null +++ b/cmap/divide_conquer/53dc7f82-84a6-485f-b771-e13d8f1b2c28.cpp @@ -0,0 +1,224 @@ +#include +namespace FastRead + +{ + + char buff[5000];long long lg = 0 , p = 0; + + char nc() + + { + + if(lg == p){lg = fread(buff , 1 , 5000 , stdin);p = 0;if(!lg) return EOF;} + + return buff[p++]; + + } + + templatevoid read(T&x) + + { + + T sgn = 1; char c;while(!isdigit(c = nc()))if(c == '-')sgn = -1; + + x = c - '0';while(isdigit(c = nc()))x = x * 10 + c - '0';x *= sgn; + + } + +} + + + +using namespace FastRead; + +using namespace std; + + + +const long long N = 1e5 + 10; + +const long long M = 1e9 + 7; + +const long double PI = acos(-1); + +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + + +long long n; + +pair < long long , long long > X[N] , Y[N] , aux[N]; + + + +long long dist(pair < long long , long long > a , pair < long long , long long > b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +void merge(long long l1 , long long r1 , long long l2 , long long r2) + +{ + + long long i1 = l1 , i2 = l2 , lg = 0; + + + + while(i1 <= r1 && i2 <= r2) + + { + + if(Y[i1] <= Y[i2]) + + aux[++lg] = Y[i1++]; + + else aux[++lg] = Y[i2++]; + + } + + + + while(i1 <= r1) + + aux[++lg] = Y[i1++]; + + + + while(i2 <= r2) + + aux[++lg] = Y[i2++]; + +} + + + +void copy(long long l , long long r) + +{ + + for(long long i = l ; i <= r ; i++) + + Y[i] = aux[i - l + 1]; + +} + + + +long long divide(long long l , long long r , pair < long long , long long > X[] , pair < long long , long long > Y[]) + +{ + + if(l >= r) + + return LLONG_MAX; + + + + if(r - l + 1 <= 3) + + { + + sort(Y + l , Y + r + 1); + + + + long long optimal = LLONG_MAX; + + + + for(long long i = l ; i < r ; i++) + + for(long long j = i + 1 ; j <= r ; j++) + + optimal = min(optimal , dist(X[i] , X[j])); + + + + return optimal; + + } + + + + long long mid = (r + l) / 2; + + long long optimal = min( divide(l , mid , X , Y) , divide(mid + 1 , r , X , Y) ); + + + + merge(l , mid , mid + 1 , r); + + copy(l , r); + + + + long long lg = 0; + + + + for(long long i = l ; i <= r ; i++) + + if(abs(Y[i].second - X[mid].first) <= optimal) + + aux[++lg] = Y[i]; + + + + for(long long i = 1 ; i <= lg ; i++) + + for(long long j = i + 1 ; j <= lg && j - i + 1 <= 8 ; j++) + + optimal = min(optimal , dist(aux[i] , aux[j])); + + + + return optimal; + +} + + + +signed main() + +{ + + + + ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) , freopen("cmap.in" , "r" , stdin) , freopen("cmap.out" , "w" , stdout); + + + + + + cin >> n; + + + + for(long long i = 1 ; i <= n ; i++) + + cin >> X[i].first >> X[i].second; + + + + sort(X + 1 , X + n + 1); + + + + for(long long i = 1 ; i <= n ; i++) + + Y[i] = {X[i].second , X[i].first}; + + + + cout << fixed << setprecision(6) << sqrt(divide(1 , n , X , Y)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/5560471d-a000-41fb-9286-d9e013e9eeb2.cpp b/cmap/divide_conquer/5560471d-a000-41fb-9286-d9e013e9eeb2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7da0bed714ffa308bbcf11f62eaba05ffe18cd --- /dev/null +++ b/cmap/divide_conquer/5560471d-a000-41fb-9286-d9e013e9eeb2.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +struct Point { + long long x, y; + friend bool operator<(const Point &p1, const Point &p2); + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + +bool operator<(const Point &p1, const Point &p2) { + return p1.x < p2.x; +} + +vector read(long long &n) { + ifstream fin("cmap.in"); + vector points; + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); + return points; +} + +void write(int n, vector points) { + for (int i = 1; i <= n; i++) { + cout << points[i].x << " " << points[i].y << "\n"; + } +} + +double distance(Point A, Point B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + if (minDist > distance(points[i], points[j])) { + minDist = distance(points[i], points[j]); + } + } + } + + return minDist; +} + +double divide(int li, int ls, vector &points) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, points); + } + int mid = (li + ls)/2; + double dl = divide(li, mid, points); + double dr = divide(mid + 1, ls, points); + double dmin = min(dl, dr); + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = li; i <= ls; i++) { + if (i != mid && abs(points[mid].x - points[i].x) <= dmin) { + strip.push_back(points[i]); + } + } + sort(strip.begin() + 1, strip.end(), Point::byY()); + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 8 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + + return dmin; +} + +int main() { + long long n; + vector points; + points = read(n); + sort(points.begin() + 1, points.end()); + + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, points); + return 0; +} diff --git a/cmap/divide_conquer/55772c61-e616-4fa2-9666-79813d5406df.cpp b/cmap/divide_conquer/55772c61-e616-4fa2-9666-79813d5406df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e65c48927df473659e9764fc4138d056a5e50ee4 --- /dev/null +++ b/cmap/divide_conquer/55772c61-e616-4fa2-9666-79813d5406df.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const long long INF = 4e18; + +char s[101]; +int n; +vector > v, w; +vector > m, aux; + +long long get_dist(pair a, pair b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long dei(int lt, int rt) { + if (lt == rt) { + return INF; + } else if (rt - lt == 1) { + return get_dist(v[lt], v[rt]); + } else if (rt - lt == 2) { + return min(get_dist(v[lt], v[rt]), min(get_dist(v[lt], v[lt + 1]), get_dist(v[lt + 1], v[rt]))); + } + + int mid = (lt + rt) >> 1; + long long sol = min(dei(lt, mid), dei(mid, rt)); + + + m.clear(); + m.resize(rt + 1 - lt + 1); + aux.clear(); + merge(w.begin() + lt, w.begin() + mid, w.begin() + mid, w.begin() + rt + 1, m.begin()); + + for (vector >::iterator it = m.begin(); it != m.end(); ++it) { + if (abs(it->second - v[mid].first) <= sol) { + aux.push_back(make_pair(it->second, it->first)); + } + } + + for (int i = 0; i < aux.size(); ++i) { + for (int j = i + 1; j < aux.size() && j - i < 8; ++j) { + sol = min(sol, get_dist(aux[i], aux[j])); + } + } + + return sol; +} + +int main() { + fin >> n; + for (int i = 1; i <= n; ++i) { + long long x, y; + fin >> x >> y; + v.push_back(make_pair(x, y)); + } + for (int i = 1; i <= n; ++i) { + w.push_back(make_pair(v[i - 1].second, v[i - 1].first)); + } + + sort(v.begin(), v.end()); + w.push_back(make_pair(INF, INF)); + sort(w.begin(), w.end()); + + long long sol = dei(0, v.size() - 1); + fout << fixed << setprecision(6) << sqrt((double)sol); + + fout << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/55b8ff4d-0279-4708-afc5-97399800078a.cpp b/cmap/divide_conquer/55b8ff4d-0279-4708-afc5-97399800078a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5de297cf22b6f4eb6b971aef2a84882ca188e437 --- /dev/null +++ b/cmap/divide_conquer/55b8ff4d-0279-4708-afc5-97399800078a.cpp @@ -0,0 +1,182 @@ +#include + + + + + + +using namespace std; + +typedef long long llong; + + + +struct Punct + +{ + + int x, y; + +} px[100005], tmp[100005]; + + + +bool cmpx(const Punct& a, const Punct& b) + +{ + + return a.x < b.x; + +} + + + +bool cmpy(const Punct& a, const Punct& b) + +{ + + return a.y < b.y; + +} + + + +llong dist2(const Punct& a, const Punct& b) + +{ + + llong dx = a.x - b.x; + + llong dy = a.y - b.y; + + return dx * dx + dy * dy; + +} + + + +llong calc(int p, int q) + +{ + + llong res = 0x7fffffffffffffffLL; + + for(int i = p; i < q; i++) + + { + + for(int j = i + 1; j <= q; j++) + + { + + llong aux = dist2(px[i], px[j]); + + if(aux < res) + + res = aux; + + } + + } + + return res; + +} + + + +inline llong sqr(int x) + +{ + + return (llong)x * x; + +} + + + +llong solve(int p, int q) + +{ + + if(q - p < 3) + + return calc(p, q); + + int m = (p + q) / 2; + + llong d1 = solve(p, m); + + llong d2 = solve(m, q); + + llong dd = min(d1, d2); + + + + int k = 0; + + for(int i = p; i <= q; i++) + + { + + if(sqr(px[i].x - px[m].x) < dd) + + tmp[k++] = px[i]; + + } + + sort(tmp, tmp + k, cmpy); + + for(int i = 0; i < k; i++) + + { + + for(int j = i + 1; sqr(tmp[j].y - tmp[i].y) < dd && j < k; j++) + + { + + llong dist = dist2(tmp[i], tmp[j]); + + if(dd > dist) + + dd = dist; + + } + + } + + return dd; + +} + + + +int main() + +{ + + int n, i; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> n; + + for(i = 0; i < n; i++) + + { + + fin >> px[i].x >> px[i].y; + + } + + sort(px, px + n, cmpx); + + llong res = solve(0, n - 1); + + fout << fixed << setprecision(6) << sqrtl(res); + + return 0; + +} diff --git a/cmap/divide_conquer/56fce345-6f33-4e3c-8a98-3db23719e99a.cpp b/cmap/divide_conquer/56fce345-6f33-4e3c-8a98-3db23719e99a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..009c241bedacbc05a308f7e67c14569d9e98183b --- /dev/null +++ b/cmap/divide_conquer/56fce345-6f33-4e3c-8a98-3db23719e99a.cpp @@ -0,0 +1,134 @@ +#include + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long long i64; + + + +int N; + +vector>X; + +vector>Y; + +vector> aux; + + + +i64 Distance(pair p1, pair p2) + +{ + + return (p2.first - p1.first) * (p2.first - p1.first) + (p2.second - p1.second) * (p2.second - p1.second); + +} + + + +i64 solve(int left, int right, vector> &X, vector> &Y) + +{ + + if (right - left <= 3) + + { + + i64 answ = 4e18; + + for (int i = left; i < right; ++i) + + for (int j = i + 1; j <= right; ++j) + + { + + if (Y[i] > Y[j]) + + swap(Y[i], Y[j]); + + answ = min(answ, Distance(X[i], X[j])); + + } + + return answ; + + } + + + + int mid = (left + right) >> 1; + + i64 best = min(solve(left, mid, X, Y), solve(mid + 1, right, X, Y)); + + + + merge(Y.begin() + left + 1, Y.begin() + mid + 1, Y.begin() + mid + 1, Y.begin() + right + 1, aux.begin()); + + copy(aux.begin(), aux.begin() + (right - left ), Y.begin() + left + 1); + + + + int _size = 0; + + for (int i = left; i <= right; ++i) + + if (abs(X[mid].first - Y[i].second) <= best) + + aux[_size++ ] = Y[i]; + + + + for (int i = 0; i < _size - 1; ++i) + + for (int j = i + 1; j < _size && j - i < 8; ++j) + + best = min(best, Distance(aux[i], aux[j])); + + return best; + +} + + + +int main() + +{ + + fin >> N; + + X.resize(N + 1); + + Y.resize(N + 1); + + aux.resize(N + 1); + + for (int i = 1; i <= N; ++i) + + fin >> X[i].first >> X[i].second; + + + + sort(X.begin(), X.end()); + + for (int i = 1;i <= N; ++i) + + Y[i] = {X[i].second, X[i].first}; + + + + fout << fixed << setprecision(6) << sqrt(solve(1, N, X, Y)) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/57330209-7381-4c48-91e8-c4759dee88f4.cpp b/cmap/divide_conquer/57330209-7381-4c48-91e8-c4759dee88f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7636d52c2a50da86c9cdec6c551ba9460127162 --- /dev/null +++ b/cmap/divide_conquer/57330209-7381-4c48-91e8-c4759dee88f4.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +using namespace std; + + +ifstream in("cmap.in"); +ofstream out("cmap.out"); +pair v[100001]; + +long long dist_calc(long long x1, long long x2, long long y1, long long y2) { + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); +} + +long long dist_pair(unsigned int first, unsigned int second) { + return dist_calc(v[first].first, v[second].first, v[first].second, v[second].second); +} + +long long div_et_imp(unsigned int st, unsigned int fin) { + if (fin - st <= 3) { + long long d = dist_pair(0, 1); + if (fin - st == 3) { + d = min(d, dist_pair(1, 2)); + d = min(d, dist_pair(0, 2)); + } + return d; + } + + unsigned long long med = st + (fin - st) / 2; + long long d = min(div_et_imp(st, med), div_et_imp(med, fin)); + vector > p; + for (unsigned int i = st; i < fin; i ++) { + + p.emplace_back(v[i].second, v[i].first); + } + sort(p.begin(), p.end()); + for (unsigned int i = 0; i < p.size(); i ++) { + for (unsigned int j = i + 1; j < i + 8 && j < p.size(); j ++) { + d = min(d, dist_calc(p[i].first, p[j].first, p[i].second, p[j].second)); + } + } + return d; +} + +int main() { + int n; + in >> n; + for (unsigned int i = 0; i < n; i ++) { + in >> v[i].first >> v[i].second; + } + sort(v, v + n); + out << fixed << setprecision(6) << sqrt(div_et_imp(0, n)); + return 0; +} diff --git a/cmap/divide_conquer/57399383-9eda-4452-9d9e-5f1468903a36.cpp b/cmap/divide_conquer/57399383-9eda-4452-9d9e-5f1468903a36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..567838dd70005e4834d96b9d0af1516d82248a8a --- /dev/null +++ b/cmap/divide_conquer/57399383-9eda-4452-9d9e-5f1468903a36.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +long long returnDist(const pair &P1,const pair &P2) + +{ + + return (long long) (P2.first - P1.first) * (long long) (P2.first - P1.first) + + + (long long) (P2.second - P1.second) * (long long) (P2.second - P1.second); + +} + + + +bool compByX(const pair &P1,const pair &P2) + +{ + + return P1.first < P2.first; + +} + + + +bool compByY(const pair &P1,const pair &P2) + +{ + + return P1.second < P2.second; + +} + + + +vector< pair > Points; + + + +long long divideClosPoints(long left,long right) + +{ + + if(right - left == 1) + + return returnDist(Points[left], Points[right]); + + + + if(right - left == 2) + + return min(returnDist(Points[left], Points[left+1]), + + min(returnDist(Points[left+1], Points[right]), + + returnDist(Points[left], Points[right]))); + + + + long middle = (left + right) / 2; + + long long dLeft = divideClosPoints(left, middle); + + long long dRight = divideClosPoints(middle + 1, right); + + + + long long d = min(dLeft, dRight); + + + + long i, j; + + + + vector< pair > verticalSec; + + + + auto delta = ceil( sqrt(d) ); + + + + for(i = left ; i <= right; i++) + + { + + if( abs(Points[i].first - Points[middle].first) <= delta) + + verticalSec.push_back(Points[i]); + + } + + sort(verticalSec.begin(),verticalSec.end(),compByY); + + + + long long sizeV = verticalSec.size(); + + + + for(i = 0; i < sizeV ; i++) + + for(j = i + 1; j <= i + 7 && j < sizeV ; j++) + + { + + d = min(d, returnDist(verticalSec[i],verticalSec[j]) ); + + } + + return d; + +} + + + +int main() + +{ + + ifstream fileRead("cmap.in"); + + ofstream fileWrite("cmap.out"); + + long long nrPoints, i; + + + + pair P; + + fileRead >> nrPoints; + + + + for(i = 0 ; i < nrPoints; i++) + + { + + fileRead >> P.first >> P.second; + + Points.push_back(P); + + } + + sort(Points.begin(),Points.end(),compByX); + + fileWrite << fixed << setprecision(6) << sqrt(divideClosPoints(0,Points.size() - 1)); + + + + fileRead.close(); + + fileWrite.close(); + +} diff --git a/cmap/divide_conquer/5778167e-dead-443f-b487-f29b475ce440.cpp b/cmap/divide_conquer/5778167e-dead-443f-b487-f29b475ce440.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eedc65ccfad6f9f75fa8ecbfe5899ebbcc35ca47 --- /dev/null +++ b/cmap/divide_conquer/5778167e-dead-443f-b487-f29b475ce440.cpp @@ -0,0 +1,215 @@ +#include +#include #include +#include +#include +#include +#include +using namespace std; + + + + + +struct punct + +{ + + int x = 0; + + int y = 0; + +}; + + + +double distEuclid(punct a, punct b) + + { + + double x = a.x - b.x; + + double y = a.y - b.y; + + double dist; + + + + dist = pow(x, 2) + pow(y, 2); + + dist = sqrt(dist); + + + + return dist; + +} + + + +bool cmpDupaX(const punct& a, const punct& b) + +{ + + return a.x < b.x; + +} + + + +bool cmpDupaY(const punct& a, const punct& b) + +{ + + return a.y < b.y; + +} + + + + + +double distMinPunctePlanRec(vector & puncte, int st, int dr, vector &puncteY) + +{ + + if (dr - st == 1) + + { + + return 2147483647; + + } + + + + if (dr - st == 2) + + { + + if (puncteY[st].y > puncteY[st + 1].y) + + swap(puncteY[st], puncteY[st + 1]); + + return distEuclid(puncte[0], puncte[1]); + + } + + + + int m = (st + dr) / 2; + + double distStanga = distMinPunctePlanRec(puncte, st, m, puncteY); + + double distDreapta = distMinPunctePlanRec(puncte, m, dr, puncteY); + + double distMinStDr = min(distStanga, distDreapta); + + + + + + vector puncteAux(dr - st); + + merge(puncteY.begin() + st, puncteY.begin() + m, puncteY.begin() + m, puncteY.begin() + dr, puncteAux.begin(), cmpDupaY); + + copy(puncteAux.begin(), puncteAux.begin() + (dr - st), puncteY.begin() + st); + + + + + + vector banda; + + for (int i = st; i < dr; ++i) + + { + + if (abs(puncteY[i].x - puncte[m].x) <= distMinStDr) + + banda.push_back(puncteY[i]); + + } + + + + + + double distMinOverall = distMinStDr; + + for (int i = 0; i> n; + + + + vector puncte(n); + + + + for (int i = 0; i < n; ++i) + + { + + f >> puncte[i].x; + + f >> puncte[i].y; + + } + + + + sort(puncte.begin(), puncte.end(), cmpDupaX); + + + + vector puncteY = puncte; + + + + g << fixed; + + g << setprecision(6) << distMinPunctePlanRec(puncte, 0, puncte.size(), puncteY) << "\n"; + +} + + + +int main() + +{ + + distMinPunctePlan(); + + return 0; + +} diff --git a/cmap/divide_conquer/5809fd62-7f5f-49d7-9f4e-0bd12c734bcf.cpp b/cmap/divide_conquer/5809fd62-7f5f-49d7-9f4e-0bd12c734bcf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..834a7c21172cd0147497d1c35094a472e109b29c --- /dev/null +++ b/cmap/divide_conquer/5809fd62-7f5f-49d7-9f4e-0bd12c734bcf.cpp @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct + +{ + + int x; + + int y; + +}; + + + +int comparare_x(punct a, punct b) + +{ + + return a.x < b.x; + +} + + + +int comparare_y(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +double distanta_euclidiana(punct a, punct b) + +{ + + double x = a.x - b.x; + + double y = a.y - b.y; + + double distanta; + + distanta = pow(x, 2) + pow(y, 2); + + distanta = sqrt(distanta); + + return distanta; + +} + + + +int citire(vector &vx, vector &vy) + +{ + + int n; + + in >> n; + + for (int i = 0; i < n; i++) + + { + + punct aux; + + in >> aux.x; + + in >> aux.y; + + vx.push_back(aux); + + } + + sort(vx.begin(), vx.end(), comparare_x); + + vy = vx; + + return 1; + +} + + + +int afisare(vector vx) + +{ + + for (auto it : vx) + + { + + out << it.x << " " << it.y << endl; + + } + + return 1; + +} + + + +double calculare_distanta_minima(vector &vx, int left, int right, vector &vy) + +{ + + if (right - left == 1) + + { + + return INT_MAX; + + } + + if (right - left == 2) + + { + + if (vy.at(left).y > vy.at(left + 1).y) + + { + + swap(vy.at(left), vy.at(left + 1)); + + } + + return distanta_euclidiana(vx.at(0), vx.at(1)); + + } + + + + int mij = (left + right) / 2; + + + + double distanta_minima_stanga = calculare_distanta_minima(vx, left, mij, vy); + + double distanta_minima_dreapta = calculare_distanta_minima(vx, mij, right, vy); + + double distanta_minima_temporara = min(distanta_minima_stanga, distanta_minima_dreapta); + + + + vector puncte_auxiliare(right - left); + + + + merge(vy.begin() + left, vy.begin() + mij, vy.begin() + mij, vy.begin() + right, puncte_auxiliare.begin(), comparare_y); + + copy(puncte_auxiliare.begin(), puncte_auxiliare.begin() + (right - left), vy.begin() + left); + + + + + + vector aux; + + + + for (int i = left; i < right; i++) + + { + + if (abs(vy.at(i).x - vx.at(mij).x) <= distanta_minima_temporara) + + { + + aux.push_back(vy.at(i)); + + } + + } + + + + double distanta_minima_finala = distanta_minima_temporara; + + + + for (int i = 0; i < aux.size(); i++) + + { + + for (int j = i + 1; j < aux.size() && aux.at(j).y - aux.at(i).y <= distanta_minima_temporara; j++) + + { + + double distanta_auxiliara = distanta_euclidiana(aux.at(i), aux.at(j)); + + if (distanta_auxiliara < distanta_minima_finala) + + { + + distanta_minima_finala = distanta_auxiliara; + + } + + } + + } + + return distanta_minima_finala; + +} + + + + + + + +int main() + +{ + + vector vx; + + vector vy; + + citire(vx, vy); + + + + out << fixed; + + out << setprecision(6) << calculare_distanta_minima(vx, 0, vx.size(), vy); + + return 0; + +} diff --git a/cmap/divide_conquer/58572aea-fafb-4ba0-82f7-a6f974d28265.cpp b/cmap/divide_conquer/58572aea-fafb-4ba0-82f7-a6f974d28265.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7782b2db8fec2025f3ed26d10621dc12b50c138 --- /dev/null +++ b/cmap/divide_conquer/58572aea-fafb-4ba0-82f7-a6f974d28265.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int n; + +pair v[100010], aux[100010], verifica[100010]; + + + +long long DistantaLaPatrat(paira, pairb){ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + +void interclasare(int st, int mid, int dr){ + + int n=mid; + + int m=dr; + + int i=st; + + int j=mid+1; + + int k=st-1; + + while(i<=n && j<=m){ + + if(v[i].second>n; + + for(int i=1; i<=n; i++){ + + fin>>v[i].first>>v[i].second; + + } + + sort(v+1,v+n+1); + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +FILE *fin = fopen("cmap.in", "r"); + +FILE *fout = fopen("cmap.out", "w"); + + + +pair v[100005],aux[100005]; + + + +int n,i; + + + +double dist(pair x, pair y) + +{ + + return 1.0*sqrt(1.0*1LL*(x.first-y.first)*(x.first-y.first) + 1LL*(x.second-y.second)*(x.second-y.second)); + +} + + + +bool cmp(pair x, pair y) + +{ + + return x.second <= y.second; + +} + + + +double solve(int left, int right) + +{ + + int i,j,k,mid,t,xmij; + + double ans = 1e12,dst,ddr,dmin; + + + + if(right-left+1 <= 3) + + { + + sort(v+left, v+right+1, cmp); + + + + for(i=left; i<=right-1; ++i) + + for(j=i+1; j<=right; ++j) + + ans = min(ans, dist(v[i],v[j])); + + + + return ans; + + } + + else + + { + + mid = (left+right) /2; + + xmij = v[mid].first; + + dst = solve(left, mid); + + ddr = solve(mid+1, right); + + + + dmin = min(dst,ddr); + + + + i = left; + + j = mid+1; + + k = 0; + + while(i<=mid and j<=right) + + if(v[i].second < v[j].second) + + aux[++k] = v[i++]; + + else + + aux[++k] = v[j++]; + + + + while(i<=mid) + + aux[++k] = v[i++]; + + while(j<=right) + + aux[++k] = v[j++]; + + + + t = 0; + + for(i=1,j=left; i<=k; ++i,++j) + + { + + v[j] = aux[i]; + + if(1.0*fabs(xmij - aux[i].first) <= dmin) + + aux[++t] = aux[i]; + + } + + + + ans = dmin; + + for(i=1; i<=t; ++i) + + for(j=i+1; j<=t && j<=i+7; ++j) + + ans = min(ans, dist(aux[i],aux[j])); + + return ans; + + } + +} + + + +int main() + +{ + + fscanf(fin, "%d", &n); + + for(i=1; i<=n; ++i) + + fscanf(fin, "%d%d", &v[i].first,&v[i].second); + + + + sort(v+1, v+n+1); + + + + fprintf(fout, "%.8lf", solve(1,n)); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/cmap/divide_conquer/58bff717-0608-435c-a439-bd4afb925817.cpp b/cmap/divide_conquer/58bff717-0608-435c-a439-bd4afb925817.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e6dc2536e8e4292c47834af004340be37a9b6fe --- /dev/null +++ b/cmap/divide_conquer/58bff717-0608-435c-a439-bd4afb925817.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + long long a, b; + +}; + +punct x[100001], y[100001], aux[100001]; + +bool crit(punct aa, punct bb) + +{ + + return ((aa.a < bb.a) || (aa.a == bb.a && aa.b < bb.b)); + +} + +long long dist(punct aa, punct bb) + +{ + + return (aa.a - bb.a) * (aa.a - bb.a) + (aa.b - bb.b) * (aa.b - bb.b); + +} + +long long solutie(int st, int dr, punct *X, punct *Y) + +{ + + if(st >= dr - 1) + + return LLONG_MAX; + + if(dr - st == 2) + + { + + if(!crit(Y[st] , Y[st + 1])) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int m = (dr + st) / 2; + + long long minim = min(solutie(st, m, X, Y), solutie(m, dr, X, Y)); + + merge(Y + st, Y + m, Y + m, Y + dr, aux, crit); + + copy(aux, aux + (dr - st), Y + st); + + int it = 0; + + for(int i = st; i < dr; i++) if(abs(Y[i].b - X[m].a) <= minim) + + aux[it ++] = Y[i]; + + for(int i = 0; i < it; i++) + + { + + for(int j = i + 1; j < it && j - i < 8; j++) + + minim = min(minim, dist(aux[i], aux[j])); + + } + + return minim; + +} + +int main() + +{ + + int n; + + f >> n; + + for(int i = 0; i < n; i++) + + f >> x[i].a >> x[i].b; + + sort(x, x + n, crit); + + for(int i = 0; i < n; i++) + + { + + y[i].a = x[i].b; + + y[i].b = x[i].a; + + } + + g << fixed << setprecision(6) << sqrt(solutie(0, n - 1, x, y)); + + return 0; + +} diff --git a/cmap/divide_conquer/5968532f-1436-4189-83d1-428686310baf.cpp b/cmap/divide_conquer/5968532f-1436-4189-83d1-428686310baf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1ed97f0f800c3252a7f060f0d3ee1c3f06c0bf5 --- /dev/null +++ b/cmap/divide_conquer/5968532f-1436-4189-83d1-428686310baf.cpp @@ -0,0 +1,342 @@ +#include +using namespace std; + + + +typedef long long lint; + + + +const lint sqrInf = 1LL * 4000000000000000000LL; + + + +int n; + + + +struct coord + +{ + + int x, y; + +}; + + + +coord v[100000 + 1]; + +coord Y[100000 + 1]; + +coord x[100000 + 1]; + + + +void readFile() + +{ + + ifstream f("cmap.in"); + + + + f >> n; + + + + for(int i = 1; i <= n; i ++) + + f >> v[i].x >> v[i].y; + + + + f.close(); + +} + + + +struct cmpY + +{ + + bool operator() (coord a, coord b) + + { + + if(a.y != b.y) + + return a.y < b.y; + + + + return a.x < b.x; + + } + +}; + + + +lint dist(coord a, coord b) + +{ + + lint dx = a.x - b.x; + + lint dy = a.y - b.y; + + + + return dx * dx + dy * dy; + +} + + + +bool cmp2(coord a, coord b) + +{ + + if(a.y != b.y) + + return a.y < b.y; + + + + return a.x < b.x; + +} + + + +void mergeSort(int st1, int dr1, int st2, int dr2) + +{ + + int cr = 0; + + int i = st1; + + int j = st2; + + while(i <= dr1 && j <= dr2) + + { + + int val = cmp2(Y[i], Y[j]); + + if(val == 1) + + x[++ cr] = Y[i ++]; + + + + else + + x[++ cr] = Y[j ++]; + + } + + + + while(i <= dr1) + + x[++ cr] = Y[i ++]; + + + + while(j <= dr2) + + x[++ cr] = Y[j ++]; + + + + for(i = st1; i <= dr2; i ++) + + Y[i] = x[i - st1 + 1]; + +} + + + +inline int poz(int a) + +{ + + return (a > 0 ? a : (-a)); + +} + + + +lint dmin(int st, int dr) + +{ + + if(st == dr) + + return sqrInf; + + + + if(dr - st + 1 <= 3) + + { + + sort(Y + st, Y + dr + 1, cmpY()); + + + + lint rez = sqrInf; + + int i, j; + + for(i = st; i <= dr; i ++) + + { + + for(j = i + 1; j <= dr; j ++) + + rez = min(rez, dist(Y[i], Y[j])); + + } + + + + return rez; + + } + + + + int mid = ((st + dr) >> 1); + + lint d = min(dmin(st, mid), dmin(mid + 1, dr)); + + + + mergeSort(st, mid, mid + 1, dr); + + int i; + + int k = 0; + + for(i = st; i <= dr; i ++) + + { + + if(poz(Y[i].x - v[mid].x) <= d) + + x[++ k] = Y[i]; + + } + + + + int j; + + for(i = 1; i <= k; i ++) + + { + + for(j = i + 1; j <= i + 5 && j <= k; j ++) + + d = min(d, dist(x[i], x[j])); + + } + + + + return d; + +} + + + +struct cmp + +{ + + bool operator() (coord a, coord b) + + { + + if(a.x != b.x) + + return a.x < b.x; + + + + return a.y < b.y; + + } + +}; + + + +lint rez; + + + +void solve() + +{ + + sort(v + 1, v + n + 1, cmp()); + + + + int i; + + for(i = 1; i <= n; i ++) + + Y[i] = v[i]; + + + + rez = dmin(1, n); + +} + + + +void printFile() + +{ + + ofstream g("cmap.out"); + + + + g << fixed << setprecision(6) << sqrt(rez) << "\n"; + + + + g.close(); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/599efc2d-2ceb-4e9d-9958-021acf8ca968.cpp b/cmap/divide_conquer/599efc2d-2ceb-4e9d-9958-021acf8ca968.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc448dab949fa927ea73af7a4305d96846582be4 --- /dev/null +++ b/cmap/divide_conquer/599efc2d-2ceb-4e9d-9958-021acf8ca968.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct Point { + + + + long long x, y; + + long long dist_sq(Point& b) + + { + + return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y); + + } + +}; + + + +bool sort_x(Point& a, Point& b) + +{ + + if (a.x < b.x) + + return true; + + else + + return false; + +} + + + + + +bool sort_y(Point& a, Point& b) + +{ + + if (a.y < b.y) + + return true; + + else + + return false; + +} + + + +long long get_min_dist(long long d_min, vector& a) + +{ + + for (unsigned int i = 0; i < a.size() - 1; i++) + + { + + for (unsigned int j = i + 1; j < a.size() and j < i + 8; j++) + + if (a[i].dist_sq(a[j]) < d_min) + + d_min = a[i].dist_sq(a[j]); + + } + + return d_min; + +} + + + +long long find_closest_points(vector& a, vector& b, int st, int dr) { + + if (dr - st <= 3) { + + long long d_min = a[st].dist_sq(a[st + 1]); + + for (int i = st; i < dr; i++) + + for (int j = i + 1; j <= dr; j++) + + if (a[i].dist_sq(a[j]) < d_min) + + d_min = a[i].dist_sq(a[j]); + + return d_min; + + } + + else { + + int mijloc = (st + dr) / 2; + + vector ord_st, ord_dr; + + for (unsigned int i = 0; i < b.size(); i++) + + if (b[i].x <= a[mijloc].x) + + ord_st.push_back(b[i]); + + else + + ord_dr.push_back(b[i]); + + + + long long dist_st = find_closest_points(a, ord_st, st, mijloc); + + long long dist_dr = find_closest_points(a, ord_dr, mijloc + 1, dr); + + long long d_min; + + + + if (dist_st < dist_dr) + + d_min = dist_st; + + else + + d_min = dist_dr; + + + + vector fasie; + + for (unsigned int i = 0; i < b.size(); i++) { + + if (b[i].x > a[mijloc].x - sqrt(d_min) and b[i].x < a[mijloc].x + sqrt(d_min)) + + fasie.push_back(b[i]); + + } + + return get_min_dist(d_min, fasie); + + } + + + +} + + + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + int n; + + in >> n; + + vector X(n), Y(n); + + for (int i = 0; i < n; i++) + + { + + in >> X[i].x >> X[i].y; + + Y[i].x = X[i].x; + + Y[i].y = X[i].y; + + } + + sort(X.begin(), X.end(), sort_x); + + sort(Y.begin(), Y.end(), sort_y); + + out << fixed << setprecision(6) << sqrt(find_closest_points(X, Y, 0, n - 1)); + +} diff --git a/cmap/divide_conquer/59f8dd1f-35ad-447c-b488-9e9b63a7b2f2.cpp b/cmap/divide_conquer/59f8dd1f-35ad-447c-b488-9e9b63a7b2f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..205b28daa82097f663a679d59eda1e7932a61b66 --- /dev/null +++ b/cmap/divide_conquer/59f8dd1f-35ad-447c-b488-9e9b63a7b2f2.cpp @@ -0,0 +1,122 @@ +#include + + + + + + +const int MAX_N = 100005; + +const long long INF = 2e18; + + + +int n; + + + +std::vector > arr; + + + +long long square(long long value) { + + return 1LL * value * value; + +} + + + +long long distance(std::pair a, + + std::pair b) { + + return (square(a.first - b.first) + square(a.second - b.second)); + +} + + + +long long solve(long long lo, long long ri) { + + long long ans = INF, mid = (lo + ri) >> 1; + + std::vector > points; + + if (ri - lo + 1 <= 3) { + + for (int i = lo; i <= ri; ++i) { + + points.push_back(arr[i]); + + } + + for (int i = 0; i < points.size(); ++i) { + + for (int j = 1 + i; j < points.size(); ++j) { + + ans = std::min(ans, distance(points[i], points[j])); + + } + + } + + return ans; + + } + + ans = std::min(solve(lo, mid), solve(mid + 1, ri)); + + for (int i = lo; i <= ri; ++i) { + + if (abs(arr[i].first - arr[mid].first) <= ans) { + + points.push_back(std::make_pair(arr[i].second, arr[i].first)); + + } + + } + + std::sort(points.begin(), points.end()); + + for (int i = 0; i < points.size(); ++i) { + + for (int j = 1 + i; j < points.size() && j - i < 8; ++j) { + + ans = std::min(ans, distance(points[i], points[j])); + + } + + } + + return ans; + +} + + + +int main() { + + long long x, y; + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + std::cin >> n; + + for (int i = 0; i < n; ++i) { + + scanf("%lld %lld", &x, &y); + + arr.push_back(std::make_pair(x, y)); + + } + + std::sort(arr.begin(), arr.end()); + + std::cout << std::fixed << std::setprecision(6) << sqrt(1.0 * solve(0, n - 1)); + + return 0; + +} diff --git a/cmap/divide_conquer/5a1fd405-6c96-4237-9862-1c7c3d16653a.cpp b/cmap/divide_conquer/5a1fd405-6c96-4237-9862-1c7c3d16653a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34da078a8aa1cb6f620895320b45b3b52a55b7fb --- /dev/null +++ b/cmap/divide_conquer/5a1fd405-6c96-4237-9862-1c7c3d16653a.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +pair P[100002]; + + + +bool comp(pair a,pair b){if (a.first==b.first) return a.second a,pair b){if (a.second==b.second) return a.first a,pair b){ + + return sqrt(1LL * (a.first-b.first) * (a.first-b.first) + 1LL * (a.second-b.second) * (a.second-b.second)); + +} + + + +long double solve(int st,int dr){ + + long double mn = 1e13; + + if (st==dr) return 1e13; + + if (st+1==dr) return dist(P[st],P[dr]); + + + + int mid = (st+dr)/2; + + mn = min(solve(st,mid),solve(mid+1,dr)); + + + + vector > V; + + long double VMID = (P[mid].first+P[mid+1].first)/2; + + for(int i=mid;i>=st && abs(VMID-P[i].first)<=mn;i--) V.push_back(P[i]); + + for(int i=mid+1;i<=dr && abs(VMID-P[i].first)<=mn;i++) V.push_back(P[i]); + + + + sort(V.begin(),V.end(),comp2); + + + + for (int i=0;i>n; + + for (int i=1;i<=n;i++) f>>P[i].first>>P[i].second; + + sort(P+1,P+n+1,comp); + + + + g< +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +vector > v; + +int n; + +long long inf = 1 << 60; + +long long dst(pair p1, pair p2) { + + return (long long)(p1.first - p2.first) * (p1.first - p2.first) + (long long)(p1.second - p2.second) * (p1.second - p2.second); + +} + + + +bool compx(pair p1, pair p2) { + + return p1.first < p2.first; + +} + + + +bool compy(pair p1, pair p2) { + + return p1.second < p2.second; + +} + + + +long long dist(vector > &v,int st,int dr) { + + if(dr - st == 1) + + return dst(v[st], v[dr]); + + if(dr - st == 2) { + + long long d1,d2,d3; + + d1 = dst(v[st], v[dr]); + + d2 = dst(v[st+1], v[dr]); + + d3 = dst(v[st], v[st+1]); + + return min(d1, min(d2, d3)); + + } + + int mid=(st + dr) / 2; + + long long d, d1, d2, x; + + d1 = dist(v, mid + 1, dr); + + d2 = dist(v, st, mid); + + d = min(d1, d2); + + x = (int) ceil(sqrt(d)); + + vector > s; + + for(int i = st;i <= dr; i ++) + + if(abs(v[i].first - v[mid].first) <= x) + + s.push_back(v[i]); + + sort(s.begin(), s.end(), compy); + + for(int i = 0;i < s.size(); i ++) + + for(int j = i + 1; j <= i + 7 && j > n; + + for(int i = 1; i <= n; i ++) { + + int x, y; + + f >> x >> y; + + v.push_back(make_pair(x, y)); + + } + + sort(v.begin(), v.end(), compx); + + g << fixed << setprecision(6) << sqrt(dist(v, 0, n - 1)) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/5cfae39a-e956-4c50-a397-92794a26c980.cpp b/cmap/divide_conquer/5cfae39a-e956-4c50-a397-92794a26c980.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78432c8e28e80464b4170700b72e2fddc94fb180 --- /dev/null +++ b/cmap/divide_conquer/5cfae39a-e956-4c50-a397-92794a26c980.cpp @@ -0,0 +1,250 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef long long ll; + +typedef pair < ll, ll > point; + +point points[100001], strip[100001]; + +point aux[100001]; + +int n, n_strip; + + + +void read() { + + + + ifstream in("cmap.in"); + + ll x, y; + + in >> n; + + for (int i = 1; i <= n; ++i) { + + + + in >> x >> y; + + points[i] = make_pair(x, y); + + } + + in.close(); + +} + + + +bool compareX(const point& f, const point& s) { + + + + if ((f.first < s.first) || (f.first == s.first && f.second < s.second)) + + return true; + + return false; + +} + + + +bool compareY(const point& f, const point& s) { + + + + if ((f.second < s.second) || (f.second == s.second && f.first < s.first)) + + return true; + + return false; + +} + + + +inline ll dist(const point& p1, const point& p2) { + + + + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +ll brute_dist(point points[], int left, int right) { + + + + ll best_dist = LLONG_MAX; + + for (int i = left; i < right; i++) + + for (int j = i + 1; j <= right; j++) + + best_dist = min(best_dist, dist(points[i], points[j])); + + + + return best_dist; + +} + + + +ll min_dist_strip(ll d_min) { + + + + ll best_dist = d_min; + + + + for (int i = 1; i < n_strip; i++) + + for (int j = i + 1; j <= n_strip && (strip[j].second - strip[i].second) * (strip[j].second - strip[i].second) <= d_min; j++) + + best_dist = min(best_dist, dist(strip[i], strip[j])); + + + + return best_dist; + +} + + + +void merge(int x1, int y1, int x2, int y2) { + + + + int k = 0; + + while (x1 <= y1 && x2 <= y2) { + + + + if (compareY(points[x1], points[x2])) + + aux[++k] = points[x1++]; + + else + + aux[++k] = points[x2++]; + + } + + while (x1 <= y1) + + aux[++k] = points[x1++]; + + while (x2 <= y2) + + aux[++k] = points[x2++]; + +} + + + +ll merge_sort(int left, int right) { + + + + if (right - left + 1 <= 3) { + + + + sort(points + left, points + right + 1, compareY); + + return brute_dist(points, left, right); + + + + } + + + + int mid = left + (right - left) / 2; + + ll mid_x = points[mid].first; + + + + ll d1 = merge_sort(left, mid); + + ll d2 = merge_sort(mid + 1, right); + + ll delta = min(d1, d2); + + + + + + merge(left, mid, mid + 1, right); + + + + for (int i = left; i <= right; ++i) + + points[i] = aux[i - left + 1]; + + + + + + n_strip = 0; + + + + for (int i = left; i <= right; i++) + + if ((points[i].first - mid_x) * (points[i].first - mid_x) <= delta) + + strip[++n_strip] = points[i]; + + ll d3 = min_dist_strip(delta); + + + + return min(delta, d3); + +} + + + +int main() { + + + + read(); + + + + sort(points + 1, points + n + 1, compareX); + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(6) << sqrt(merge_sort(1, n)) << "\n"; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/5d153854-1119-40ae-a468-b009ba004071.cpp b/cmap/divide_conquer/5d153854-1119-40ae-a468-b009ba004071.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8a8138655180096583e7da4011168c884392a01 --- /dev/null +++ b/cmap/divide_conquer/5d153854-1119-40ae-a468-b009ba004071.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +template +class Point +{ + public: + Point() = default; + Point(T x, T y): x_(x), y_(y) {} + T x() const {return x_;} + T y() const {return y_;} + bool operator < (const Point& other) const { + if (x() == other.x()) + return y() < other.y(); + return x() < other.x(); + } + bool operator > (const Point& other) const { + return !(*this < other); + } + private: + T x_, y_; +}; + +int N; +vector> X, Y, aux; + +template +T dist(Point p1, Point p2) { + return (p1.x() - p2.x())*(p1.x() - p2.x()) + + (p1.y() - p2.y())*(p1.y() - p2.y()); +} + +template +T cmap(int left, int right) +{ + if (right - left == 1) + return numeric_limits::max(); + + if (right - left == 2) { + if (Y[left] > Y[left + 1]) + swap(Y[left], Y[left+1]); + return dist(X[left], X[left+1]); + } + + int mid = (left + right) / 2; + T delta = min(cmap(left, mid), cmap(mid, right)); + + + merge(Y.begin() + left, Y.begin() + mid, Y.begin() + mid, Y.begin() + right, aux.begin()); + copy(aux.begin(), aux.begin() + (right - left), Y.begin() + left); + + + int sizeAux = 0; + for (int i = left; i < right; ++i) + if (abs(X[mid].x() - Y[i].y()) <= delta + 0.0000001) + aux[sizeAux++] = Y[i]; + + for (int i = 0; i < sizeAux; ++i) + for (int j = i + 1; j < sizeAux && j - i <= 7; ++j) + delta = min(delta, dist(aux[i], aux[j])); + return delta; +} + +int main() +{ + ifstream fin("cmap.in"); + ofstream fout("cmap.out"); + + ios::sync_with_stdio(false); + fin.tie(0); + + fin >> N; + aux.resize(N); + double x,y; + for (int i = 0 ; i < N; ++i) { + fin >> x >> y; + X.emplace_back(x, y); + } + sort(X.begin(), X.end()); + for (int i = 0; i < N; ++i) + Y.emplace_back(X[i].y(), X[i].x()); + + fout << fixed << setprecision(6) << sqrt(cmap(0, N)) << "\n"; + + return 0; +} diff --git a/cmap/divide_conquer/5d566f4b-6386-4567-9517-d1c5d94fa755.cpp b/cmap/divide_conquer/5d566f4b-6386-4567-9517-d1c5d94fa755.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ccba3c7fdb1410e9269d7bff9995f89cf26dbed --- /dev/null +++ b/cmap/divide_conquer/5d566f4b-6386-4567-9517-d1c5d94fa755.cpp @@ -0,0 +1,152 @@ +#include + + + + + + +FILE *fin = freopen("cmap.in", "r", stdin); + +FILE *fout = freopen("cmap.out", "w", stdout); + + + +using namespace std; + + + +const int MAX = 1e5 + 2; + +const int INF = 2e9 + 2; + +int n; + +pair< double, double > p[MAX]; + +struct comp{ + + bool operator () (const int &a, const int &b) const{ + + return p[a].second < p[b].second; + + } + +}; + + + +void Read(){ + + scanf("%d", &n); + + for(int i = 0; i < n; ++ i) + + scanf("%lf%lf", &p[i].first, &p[i].second); + +} + + + +double Dist(pair< double, double > p1, pair< double, double > p2){ + + return sqrt(1LL * (p1.second - p2.second) * (p1.second - p2.second) + + + 1LL * (p1.first - p2.first) * (p1.first - p2.first)); + +} + + + +double StripClosest(int strip[], int len, double d){ + + double ans = d; + + sort(strip, strip + len, comp()); + + + + for(int i = 0; i < len; ++ i){ + + pair< double, double > p1 = p[strip[i]]; + + for(int j = i + 1; j < len; ++ j){ + + pair< double, double > p2 = p[strip[j]]; + + if((p2.second - p1.second) >= d) break; + + ans = min(ans, Dist(p1, p2)); + + } + + } + + return ans; + +} + + + +double BruteForce(int left, int right){ + + double ans = INF; + + for(int i = left; i < right; ++ i) + + for(int j = left + 1; j <= right; ++ j) + + ans = min(ans, Dist(p[i], p[j])); + + return ans; + +} + + + +double Divide(int left, int right){ + + if(right - left < 2) + + return BruteForce(left, right); + + + + int mid = (left + right) >> 1; + + double d = Divide(left, mid); + + d = min(d, Divide(mid + 1, right)); + + + + int strip[n]; + + int j = 0; + + + + for(int i = left; i <= right; ++ i) + + if(abs(p[mid].first - p[i].first) < d) + + strip[j ++] = i; + + + + return min(d, StripClosest(strip, j, d)); + +} + + + +int main(){ + + Read(); + + sort(p, p + n); + + printf("%.10f\n", Divide(0, n - 1)); + + return 0; + +} diff --git a/cmap/divide_conquer/5e3cc3de-c089-45a0-987b-ad9826a47c7c.cpp b/cmap/divide_conquer/5e3cc3de-c089-45a0-987b-ad9826a47c7c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a4dec39f6c5c0d4606009655a91874eb0026a3e --- /dev/null +++ b/cmap/divide_conquer/5e3cc3de-c089-45a0-987b-ad9826a47c7c.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +long long minDist(vector::iterator a, vector::iterator b) { + + long long d; + + vector::iterator i, j; + + int n = (b-a)/2; + + if (b-a<=3) { + + d = dist(*a, *(b-1)); + + } else { + + d = min(minDist(a, a+n), minDist(a+n, b)); + + } + + for (i = a; i>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + + cout< +#include +#include +using namespace std; + +int n; + + + +struct pct + +{ + + int x,y; + +} a[100001],b[100001],c[100001]; + + + + + +int f1(pct x,pct y) + +{ + + return (x.xb[lf].y)swap(b[li],b[lf]); + + return dist(a[li],a[lf]); + + } + + if(lf-li==2) + + { + + double q=dist(a[li],a[li+1]); + + double w=dist(a[li+1],a[li+2]); + + double r=dist(a[li],a[li+2]); + + if(wb[li+1].y)swap(b[li],b[li+1]); + + if(b[li].y>b[li+2].y)swap(b[li],b[li+2]); + + if(b[li+1].y>b[li+2].y)swap(b[li+1],b[li+2]); + + return r; + + } + + double d,d1; + + int m,i,np=0,xd; + + m=(li+lf)/2; + + d=dmin(li,m); + + d1=dmin(m+1,lf); + + if(d1 +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + + + +struct punct + +{ + + int x,y; + +}; + + + +punct v[100100]; + +punct curr[100100]; + +int n,i; + + + +double dist(const punct &a,const punct &b) + +{ + + return (double)sqrt( 1LL * (a.x-b.x) * (a.x-b.x) + 1LL * (a.y-b.y) * (a.y-b.y) ); + +} + + + +bool cmp0(const punct &a,const punct &b) + +{ + + if(a.y==b.y) + + return a.x=s && mij-v[i].y<=mn) + + curr[k++]=v[i--]; + + i=(s+d)/2+1; + + while(i<=d && v[i].y-mij<=mn) + + curr[k++]=v[i++]; + + sort(curr,curr+k,cmpcurr); + + for(i=0; i>n; + + for(i=1; i<=n; ++i) + + fin>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,cmp0); + + fout< + + + + +using namespace std; +double calculDist(pair a, pair b) + +{ + + double x = a.first - b.first; + + double y = a.second - b.second; + + return sqrt(pow(x,2) + pow(y,2)); + +} + + + +bool cmpY(const pair& a, const pair &b) + +{ + + return a.second < b.second; + +} + + + +bool cmpX(const pair& a, const pair &b) + +{ + + return a.first < b.first; + +} + + + +double DI(int st, int dr, vector> &pY,vector> &P) + +{ + + if(dr - st == 1) + + return INT_MAX; + + else if(dr - st == 2) + + { + + if(pY[st].second > pY[st+1].second) + + swap(pY[st], pY[st+1]); + + return calculDist(P[0],P[1]); + + } + + + + int m = (st + dr)/2,i,j; + + double dist = min(DI(st,m,pY,P),DI(m,dr,pY,P)); + + int delta = (int)(ceil(sqrt(dist))); + + + + vector> verif(dr - st); + + merge(pY.begin() + st, pY.begin() + m, pY.begin() + m, pY.begin() + dr, verif.begin(), cmpY); + + copy(verif.begin(), verif.begin() + (dr - st), pY.begin() + st); + + + + vector> z; + + + + for(unsigned i = st; i < dr; i++) + + if(abs(pY[i].first - P[m].first) <= dist) + + z.push_back(pY[i]); + + + + + + for(unsigned i = 0; i < z.size(); i++) + + for(unsigned j = i+1;j< z.size() && z[j].second - z[i].second <= dist; j++) + + dist = min(dist, calculDist(z[i],z[j])); + + return dist; + + + + + +} + + + +int main() + +{ + + int n; + + vector>P; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int x,y,i; + + fin>>n; + + + + for(i = 0; i < n; i++) + + { + + fin>>x>>y; + + P.push_back(make_pair(x,y)); + + } + + + + sort(P.begin(),P.end(),cmpX); + + + + vector> pY = P; + + + + + + fout< +#include +#include +#include +#include +using namespace std; + + + + + +struct punct{ + +double x,y; + +}; + + + + + + + +typedef double ull; + + + +punct p[100099],aux[100099],inter[100099]; + + + +ull dist(punct a,punct b){ + + return (b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y); + +} + + + +bool cmp(punct a,punct b){return a.x p[st+1].y)swap(p[st],p[st+1]); + + if(p[st+1].y > p[st+2].y)swap(p[st+1],p[st+2]); + + if(p[st].y > p[st+1].y)swap(p[st],p[st+1]); + + + + return min(dist(p[st],p[st+1]),min(dist(p[st+1],p[st+2]),dist(p[st],p[st+2]))); + + }if(dr-st+1 ==2){ + + if(p[st].y>p[st+1].y)swap(p[st],p[st+1]); + + return dist(p[st],p[st+1]); + + }else{ + + + + int mid = (st+dr)/2; + + double centru=p[mid].x; + + ull delta = min(minDist(st,mid),minDist(mid+1,dr)); + + int r= 0; + + int radDelt=sqrt(delta); + + + + + + int i,j,k; i=k=st; j=mid+1; + + while(i<=mid && j<=dr){ + + if(p[i].y < p[j].y)aux[k++]=p[i++]; + + else aux[k++]=p[j++]; + + } + + while(i<=mid)aux[k++]=p[i++]; + + while(j<=dr) aux[k++]=p[j++]; + + + + for(i=st;i<=dr;i++){ + + p[i]=aux[i]; + + if(p[i].x-centru <= radDelt && p[i].x-centru >= -radDelt) + + inter[++r]=p[i]; + + } + + + + for(i=1;i<=r;i++) + + { j= min(r,i+7); + + for(k=i+1;k<=j;k++){ + + delta= min(delta,dist(inter[i],inter[k])); + + } + + } + + return delta; + + } + + + + + +} + + + + + +int main(){ + +ifstream cin("cmap.in"); + + ofstream cout("cmap.out"); + + int n,i; + + cin>>n; + + for(i=1;i<=n;i++)cin>>p[i].x>>p[i].y; + + sort(p+1,p+n+1,cmp); + + cout< + + + +using namespace std; +ifstream f ("cmap.in"); +ofstream g ("cmap.out"); + +typedef long long ll; +typedef vector < pair < ll, ll > > vect; +vect X, V(100005); +const ll INF = 4e18; +int n; + +ll dist(const pair < ll, ll > & x, const pair < ll, ll > & y) { + return (x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second); +} + +ll ans(int st, int dr) +{ + if (st >= dr - 1) return INF; + if (dr - st == 2) return dist(X[st], X[st + 1]); + int mid = (st + dr) / 2; + ll best = min(ans(st, mid), ans(mid, dr)); + int m = 0; + for (int i = st; i < dr; i++) + if (abs(X[i].second - X[mid].first) <= best) + V[m++] = X[i]; + for (int i = 0; i < m; i++) + for (int j = i + 1; j < m && j - i < 8; j++) + best = min(best, dist(V[i], V[j])); + return best; +} + +int main() +{ + f>>n; + X.resize(n); + for (int i = 0; i < n; i++) + f>>X[i].first>>X[i].second; + sort(X.begin(), X.end()); + g< +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int NMax = 1e5 + 5; + +typedef long long ll; + +const ll inf = 1e18 + 5; + + + +int N; + + + +struct elem { + + int x,y; + +}v[NMax],aux[NMax]; + + + +bool cmp(elem,elem); + +ll dist(elem,elem); + +ll solve(int,int); + + + +int main() { + + in>>N; + + for (int i=1;i<=N;++i) { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+N+1,cmp); + + + + out< v[dr].y) { + + swap(v[st],v[dr]); + + } + + return dist(v[st],v[dr]); + + } + + + + ll mij = (st+dr)>>1, + + abscisa = v[mij].x, + + mn = min(solve(st,mij),solve(mij+1,dr)); + + + + int i = st, + + j = mij+1, + + nrAux = 0; + + while (i <= mij && j <= dr) { + + if (v[i].y < v[j].y) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + + + for (int i=st;i<=dr;++i) { + + v[i] = aux[i-st+1]; + + } + + + + nrAux = 0; + + int root = sqrt(mn); + + for (int i=st;i<=dr;++i) { + + if (abs(v[i].x - abscisa) <= root) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (int i=1;i<=nrAux;++i) { + + for (int j=i+1;j <= nrAux && j-i <= 7;++j) { + + mn = min(mn,dist(aux[i],aux[j])); + + } + + } + + + + return mn; + +} + + + +ll dist(elem a,elem b) { + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +bool cmp(elem a,elem b) { + + return a.x < b.x; + +} diff --git a/cmap/divide_conquer/621b2867-88a1-4f51-a833-3a1dccc73b76.cpp b/cmap/divide_conquer/621b2867-88a1-4f51-a833-3a1dccc73b76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fe92349e6daa35c7f6838ec19d3b5270078c25c --- /dev/null +++ b/cmap/divide_conquer/621b2867-88a1-4f51-a833-3a1dccc73b76.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + + + +const int NMAX = 100001; + +const long long INF = 0x7FFFFFFFFFFFFFFLL; + +struct punct { + + int x, y; + +}; + +punct P[NMAX], tmp[NMAX]; + +int N; + +bool compX(const punct &A, const punct &B) { + + return A.x < B.x; + +} + + + +bool compY(const punct &A, const punct &B) { + + return A.y < B.y; + +} + + + +inline long long sqr(long long v) { + + return v*v; + +} + + + +long long dist(const punct &A, const punct &B) { + + return sqr(A.x - B.x) + sqr(A.y - B.y); + +} + + + +long long distMin(int p, int u) { + + long long dMin = INF; + + for(int i = p; i < u; i++) + + for(int j = i+1; j <= u; j++) { + + long long d = dist(P[i], P[j]); + + if(dMin > d) dMin = d; + + } + + return dMin; + +} + + + +long long calcDmin(int p, int u) { + + if(u-p < 4) { + + sort(P+p, P+u+1, compY); + + return distMin(p, u); + + } + + int m = (p+u) / 2, + + x = P[m].x; + + long long d = min(calcDmin(p, m), calcDmin(m, u)); + + merge(P+p, P+m+1, P+m+1, P+u+1, tmp+p, compY); + + int k = 0; + + for(int i = p; i <= u; i++) { + + P[i] = tmp[i]; + + if(sqr(P[i].x - x) < d) + + tmp[++k] = P[i]; + + } + + for(int i = 1; i < k; i++) + + for(int j = i+1; j <= k && sqr(tmp[j].y - tmp[i].y) < d; j++) { + + long long d1 = dist(tmp[i], tmp[j]); + + if(d > d1) d = d1; + + } + + return d; + +} + + + +int main() + +{ + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + scanf("%i", &N); + + for(int i = 1; i <= N; i++) + + scanf("%i%i", &P[i].x, &P[i].y); + + sort(P+1, P+N+1, compX); + + printf("%.6f", sqrt(calcDmin(1, N))); + + return 0; + +} diff --git a/cmap/divide_conquer/622156e2-c307-46f1-b3c4-048b70f39cc2.cpp b/cmap/divide_conquer/622156e2-c307-46f1-b3c4-048b70f39cc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d08f4d1bb6446717a5c4d53d395c0fbfa2c4e5e --- /dev/null +++ b/cmap/divide_conquer/622156e2-c307-46f1-b3c4-048b70f39cc2.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; +ifstream fin("cmap.in"); ofstream fout("cmap.out"); + +vector > V(100100) , X , Y; +long long dist(pair& x , pair& y) +{ + return (x.first-y.first)*(x.first-y.first)+(x.second-y.second)*(x.second-y.second); +} + +long long cmap(int left , int right) +{ + if (left >= right - 1) + return 10000000000000; + else if (right - left == 2) + { + if (Y[left] > Y[left + 1]) + swap(Y[left], Y[left + 1]); + return dist(X[left], X[left + 1]); + } + int mid = (left + right) / 2; + long long ans = min(cmap(left , mid), cmap(mid , right)); + + sort(Y.begin()+left , Y.begin()+right); + int aux = 0; + for (int i=left ; i> n; + X.resize(n); + Y.resize(n); + for(auto &i : X) + fin >> i.first >> i.second; + sort(X.begin(), X.end()); + for(int i=0 ; i +#include +#include +#include +#include +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + +struct punct + +{ + + int x, y; + +} *X,*Y,*LY; + + + +int n; + + + +bool cmpX(punct a, punct b) + +{ + + return a.x < b.x; + +} + +bool cmpY(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +long long dist( punct a, punct b) + +{ + + long long X = 1LL * (a.x - b.x ); + + long long Y = 1LL * ( a.y - b.y ); + + return X*X + Y*Y ; + +} + + + +long long divide(int st,int dr,punct y[],int nr) + +{ + + if(dr-st == 1) + + return dist( X[st], X[dr] ); + + if (dr-st == 2) + + return min(dist(X[st], X[st+1]),min(dist(X[st+1], X[dr]),dist(X[st], X[dr]))); + + + + int mij=(st+dr)/2; + + + + int i,p=0,q=0; + + + + punct *SY = new punct[nr+1]; + + punct *DY = new punct[nr+1]; + + + + for( i = 0; i <= nr; i++) + + if( y[i].x < X[mij].x ) + + { + + SY[p].x = y[i].x; + + SY[p].y = y[i].y; + + ++p; + + } + + else + + { + + DY[q].x = y[i].x; + + DY[q].y = y[i].y; + + ++q; + + } + + + + long long d1 = divide ( st, mij, SY, p-1 ) ; + + long long d2 = divide ( mij+1, dr,DY, q-1 ) ; + + + + long long d = min( d1, d2); + + long long d_sqrt = ceil(sqrt(d)); + + int j,k=0; + + + + for( i = 0; i <= nr; i++) + + if( abs( y[i].x - X[mij].x ) <= d_sqrt ) + + { + + LY[k].x = y[i].x; + + LY[k].y = y[i].y; + + k++; + + } + + + + for(i = 0; i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++) + + d = min(d,dist(LY[i],LY[j])); + + + + delete [] SY; + + delete [] DY; + + + + return d; + +} + + + +int main() + +{ + + f >> n; + + X = new punct[n+1]; + + Y = new punct[n+1]; + + LY = new punct[n+1]; + + for ( int i = 0; i < n ; i++) + + { + + f >> X[i].x >> X[i].y; + + Y[i].x = X[i].x; + + Y[i].y = X[i].y; + + } + + + + sort(X,X+n,cmpX); + + sort(Y,Y+n,cmpY); + + + + g << fixed << setprecision(6) << sqrt(divide(0,n-1,Y,n-1)); + + + + delete [] X; + + delete [] Y; + + delete [] LY; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/63245504-de23-4902-904b-a0876dc713ad.cpp b/cmap/divide_conquer/63245504-de23-4902-904b-a0876dc713ad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d69227b5726d26551f086cdf22cf92c9b631b78c --- /dev/null +++ b/cmap/divide_conquer/63245504-de23-4902-904b-a0876dc713ad.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include + +std::ifstream fin("cmap.in"); +std::ofstream fout("cmap.out"); + +int n; +std::pair V[100010]; + +void citire() { + fin >> n; + for (int i = 1; i <= n; i++) + fin >> V[i].first >> V[i].second; +} + +bool cmp(const std::pair &p1, const std::pair &p2) { + return (p1.second < p2.second); +} + +long long distanta_euclidiana_la_patrat(const std::pair &p1, const std::pair &p2) { + return (long long) (p1.first - p2.first) * (long long) (p1.first - p2.first) + + (long long) (p1.second - p2.second) * (long long) (p1.second - p2.second); +} + + +long long Solve(int st, int dr) { + + if (dr - st == 1) { + return distanta_euclidiana_la_patrat(V[st], V[dr]); + } else if (dr - st == 2) { + return std::min(distanta_euclidiana_la_patrat(V[st], V[st + 1]), + std::min(distanta_euclidiana_la_patrat(V[st], V[st + 2]), + distanta_euclidiana_la_patrat(V[st + 1], V[st + 2]))); + } + + int mij = (st + dr) / 2; + + long long d = std::min(Solve(st, mij), Solve(mij + 1, dr)); + + std::vector > A; + + int delta = static_cast(std::ceil(std::sqrt(d))); + for (int i = st; i <= dr; i++) { + if (std::abs(V[i].first - V[mij].first) <= delta) { + A.push_back(V[i]); + } + } + + sort(A.begin(), A.end(), cmp); + + for (int i = 0; i < A.size(); i++) { + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) { + d = std::min(d, distanta_euclidiana_la_patrat(A[i], A[j])); + } + } + return d; +} + +int main() { + citire(); + + sort(V + 1, V + 1 + n); + + fout << std::fixed << std::setprecision(6) << sqrt(Solve(1, n)) << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/64763451-d0dc-47aa-bbcf-bef38f0f2fa0.cpp b/cmap/divide_conquer/64763451-d0dc-47aa-bbcf-bef38f0f2fa0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3999bb18ba21c2de80c249e72f4b28b3092c1a4f --- /dev/null +++ b/cmap/divide_conquer/64763451-d0dc-47aa-bbcf-bef38f0f2fa0.cpp @@ -0,0 +1,176 @@ +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef pair pii; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +pii p[100005],aux[100005]; + +int n; + + + +inline double dist(pii a, pii b) { + + return sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second)); + +} + + + +bool compy(pii a, pii b) {return a.secondaux[st+1].second) swap(aux[st],aux[st+1]); + + return dist(p[st],p[st+1]); + + } + + else if(dr-st==3) { + + sort(aux+st,aux+dr,compy); + + return min(dist(p[st],p[st+1]),min(dist(p[st],p[st+2]),dist(p[st+2],p[st+1]))); + + } + + int mid=(st+dr)/2,i,j,pos=st,ind1,ind2; + + double delta=solve(st, mid,aux); + + delta=min(delta,solve(mid, dr,aux)); + + + + ind1=st; + + ind2=mid; + + + + while(ind1 v; + + for(i=st;i>n; + + for(i=1;i<=n;++i) fin>>p[i].first>>p[i].second; + + + + sort(p+1,p+n+1); + + for(i=1;i<=n;++i) aux[i]=p[i]; + + + + fout< +#include +#include +#include +#include +#include + + + + +using namespace std; + + + + + + + +int N; + + + +struct pair_compare { + + bool operator() (const std::pair & p1, const std::pair & p2) const { + + if(p1.first == p2.first) + + return (p1.second < p2.second); + + else + + return (p1.first < p2.first); + + } + +}; + + + +vector> P; + + + +long long findpoints(int left,int right){ + + long long dist=LLONG_MAX,dx,dy; + + if((right-left)<=2){ + + for(int i=left;i> Q; + + for(int i=left;i<=right;i++){ + + dx=P[i].first-P[mid].first; dx*=dx; + + if(dx +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x; + + int y; + +}; + + + +punct *vect,*X,*Y; + +int nr; + + + +void afisare(punct *vect,int inc, int dim) + +{ + + for(int i=inc; i<=dim; i++) + + cout<>nr; + + vect=new punct[nr+1]; + + X=new punct[nr+1]; + + Y=new punct[nr+1]; + + for(int i=0; i>vect[i].x>>vect[i].y; + + X[i]=vect[i]; + + Y[i]=vect[i]; + + } + + + + sort(X,X+nr,compare_abs); + + sort(Y,Y+nr,compare_ord); + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const long long inf=9e18; + +const int Max_N=100005; + +typedef long long ll; + +vector > temp(Max_N); + + + +long long dist(const pair &p1,const pair &p2) + +{ + + return (ll)(p1.first-p2.first)*(p1.first-p2.first)+(ll)(p1.second-p2.second)*(p1.second-p2.second); + +} + +bool CmpX(const pair &p1,const pair &p2) + +{ return p1.first&p1,const pair&p2) + +{ return p1.second > &v,vector > &y,int st,int dr) + +{ if(st>=dr-1) + + return inf; + + if(dr-st==2) + + { + + if(y[st].first>y[st+1].first) + + swap(y[st],y[st+1]); + + return dist( v[st],v[st+1]); + + } + + int m=(st+dr)/2; + + ll d,d1,d2,delta; + + int i,j; + + d1=minDistance(v,y,m,dr); + + d2=minDistance(v,y,st,m); + + d=min(d1,d2); + + delta=(int) ceil(sqrt(d)); + + + + merge(y.begin() + st, y.begin() + m, y.begin() +m, y.begin() + dr, temp.begin()); + + copy(temp.begin(), temp.begin() + (dr - st), y.begin() + st); + + int temp_size=0; + + for(i=st;i<=dr;i++) + + if(abs(y[i].second-v[m].first)<=delta) + + temp[temp_size++]=y[i]; + + + + for(i=0;i > v,z; + + fin>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),CmpX); + + + + for(i=0;i +#include +#include +#include +#include + +using namespace std; + +const long long INF = 1e18; + +vector> points; + +void read() { + ifstream fin("cmap.in"); + + int n; + fin >> n; + for (int i = 0; i < n; ++i) { + long long x, y; + fin >> x >> y; + points.emplace_back(make_pair(x, y)); + } + + fin.close(); +} + +long long dist(const pair &A, const pair &B) { + return (A.first - B.first) * (A.first - B.first) + (A.second - B.second) * (A.second - B.second); +} + +void merge(int left_idx, int mid_idx, int right_idx) { + int i = left_idx; + int j = mid_idx + 1; + + vector> temp; + while (i <= mid_idx && j <= right_idx) + if (points[i].second < points[j].second) + temp.push_back(points[i++]); + else + temp.push_back(points[j++]); + + while (i <= mid_idx) + temp.push_back(points[i++]); + + while (j <= right_idx) + temp.push_back(points[j++]); + + for (int idx = 0; idx < (int)temp.size(); ++idx) + points[left_idx + idx] = temp[idx]; +} + + +long long closest_points(int left_idx, int right_idx) { + if (right_idx - left_idx < 2) { + if (left_idx == right_idx) { + return INF; + } + else { + merge(left_idx, left_idx, right_idx); + return dist(points[left_idx], points[right_idx]); + } + } + + int mid_index = left_idx + (right_idx - left_idx) / 2; + + long long current_best = min(closest_points(left_idx, mid_index), closest_points(mid_index + 1, right_idx)); + + merge(left_idx, mid_index, right_idx); + + for (int i = left_idx; i <= right_idx; ++i) + for (int j = i - 1; j >= left_idx && i - j < 8; --j) + current_best = min(current_best, dist(points[i], points[j])); + + return current_best; +} + +void print() { + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << sqrt(closest_points(0, (int)points.size() - 1)); + fout.close(); +} + +int main() { + read(); + sort(points.begin(), points.end()); + print(); + return 0; +} diff --git a/cmap/divide_conquer/67dd466c-6467-439a-b41e-2fcf4ddcc82d.cpp b/cmap/divide_conquer/67dd466c-6467-439a-b41e-2fcf4ddcc82d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6598734eccf0d3e24ebf0f8ac78730620407c9f5 --- /dev/null +++ b/cmap/divide_conquer/67dd466c-6467-439a-b41e-2fcf4ddcc82d.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +int n,x,y; + +vector > X; + + + +long long sq(pair &i,pair &j){ + + + + return 1LL*(i.first-j.first)*(i.first-j.first)+ + + 1LL*(i.second-j.second)*(i.second-j.second); + + + +} + + + +long long minDist(int s,int d){ + + + + if(d-s<=2){ + + + + long long D=4e18; + + for(int i=s;i<=d;i++) + + for(int j=i+1;j<=d;j++) + + D=min(D,sq(X[i],X[j])); + + + + return D; + + + + } + + + + int m=(s+d)/2; + + + + long long D1=minDist(s,m); + + long long D2=minDist(m,d); + + long long D=min(D1,D2); + + + + vector > Y(X.begin()+s,X.begin()+d+1); + + for(int i=0;i > V; + + for(int i=0;i>n; + + + + for(int i=1;i<=n;i++){ + + + + cin>>x>>y; + + X.push_back(make_pair(x,y)); + + + + } + + + + sort(X.begin(),X.end()); + + + + cout< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int NMAX = 100000; + + + +struct pct + +{ + + long long x, y; + + + + bool operator < (const pct other) const + + { + + if(x == other.x) + + return y < other.y; + + + + return x < other.x; + + } + +}; + + + +int N; + +pct v[NMAX + 5], aux[NMAX + 5]; + + + +long long DMIN = LLONG_MAX; + + + +long long Dist(pct A, pct B) + +{ + + return 1LL * (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); + +} + + + +void Merge(int l1, int r1, int l2, int r2) + +{ + + int k = 0; + + + + while(l1 <= r1 && l2 <= r2) + + { + + if(v[l1].y < v[l2].y) + + aux[++k] = v[l1], l1++; + + else + + aux[++k] = v[l2], l2++; + + } + + + + while(l1 <= r1) + + aux[++k] = v[l1], l1++; + + + + while(l2 <= r2) + + aux[++k] = v[l2], l2++; + + + + for(int i = r2; k > 0; k--, i--) + + v[i] = aux[k]; + +} + + + +void DivideAndConquer(int l, int r) + +{ + + if(l >= r) + + return ; + + + + int mid = (l + r) >> 1; + + int d = v[mid].x; + + + + DivideAndConquer(l, mid); + + DivideAndConquer(mid + 1, r); + + + + Merge(l, mid, mid + 1, r); + + + + int k = 0; + + + + for(int i = l; i <= r; i++) + + if(1LL * (v[i].x - d) * (v[i].x - d) < DMIN) + + aux[++k] = v[i]; + + + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && j <= i + 8; j++) + + DMIN = min(DMIN, Dist(aux[i], aux[j])); + +} + + + +int main() + +{ + + fin >> N; + + + + for(int i = 1; i <= N; i++) + + fin >> v[i].x >> v[i].y; + + + + sort(v + 1, v + N + 1); + + + + DivideAndConquer(1, N); + + + + fout << fixed << setprecision(12) << sqrt(DMIN) << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/6a200924-aee2-4704-bcc0-51366b89221e.cpp b/cmap/divide_conquer/6a200924-aee2-4704-bcc0-51366b89221e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d624763e743883959221c46a14761330228ac541 --- /dev/null +++ b/cmap/divide_conquer/6a200924-aee2-4704-bcc0-51366b89221e.cpp @@ -0,0 +1,300 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct point { + + long x, y; + +}; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +vector points, Y; + +int n; + + + +void read() { + + in >> n; + + point A; + + for (int i = 1; i <= n; i++) { + + in >> A.x >> A.y; + + points.push_back(A); + + Y.push_back(A); + + } + + + +} + + + +bool operator < (const point& A, const point& B) { + + if (A.x == B.x) { + + if (A.y < B.y) { + + return true; + + } + + return false; + + } + + else if (A.x < B.x) { + + return true; + + } + + return false; + +} + + + +bool cmp(const point& A, const point& B) { + + if (A.y == B.y) { + + if (A.x < B.x) { + + return true; + + } + + return false; + + } + + else if (A.y < B.y) { + + return true; + + } + + return false; + +} + + + + + +long double computeDistance(const point& A, const point& B) { + + + + return sqrt(pow(A.x - B.x, 2.0) + pow(A.y - B.y, 2.0)); + + + +} + + + +long double minim(long double d1, long double d2) { + + if (d1 < d2) { + + return d1; + + } + + return d2; + +} + + + + + +long double minnn(long double d1, long double d2, long double d3) { + + long double d4 = minim(d1, d2); + + return minim(d3, d4); + +} + + + + + +long double minn(const vector & arr, int left, int right) { + + if (right - left + 1 == 1) { + + return 0.0; + + } + + else if (right - left + 1 == 2) { + + return computeDistance(arr[left], arr[left + 1]); + + } + + else { + + return minnn(computeDistance(arr[left], arr[left + 1]), computeDistance(arr[left], arr[right]), computeDistance(arr[left + 1], arr[right])); + + } + + + + + +} + + + +void printValues(const vector & arr) { + + int count = 0; + + for (auto x : arr) { + + cout << count << ". " << x.x << "\t\t" << x.y << '\n'; + + count++; + + } + +} + + + +long double divide(int left, int right, const vector & Yy) { + + vector SY, DY, LY; + + int size = right - left + 1; + + long double d = 0, d1 = 0, d2 = 0, d3 = 0, d4 = 0; + + if (size < 4) { + + d = minn(points, left, right); + + } + + else { + + + + int mij = left + (right - left) / 2; + + + + for (int i = 0; i < Yy.size(); i++) { + + + + if ((Yy[i].x < points[mij].x) || ((Yy[i].x == points[mij].x) && (Yy[i].y <= points[mij].y))) { + + SY.push_back(Yy[i]); + + } + + else { + + + + DY.push_back(Yy[i]); + + } + + } + + + + + + d1 = divide(left, mij, SY); + + d2 = divide(mij + 1, right, DY); + + d = minim(d1, d2); + + for (int i = 0; i < Yy.size(); i++) { + + if (abs(Yy[i].x - points[mij].x) <= d) { + + LY.push_back(Yy[i]); + + } + + + + } + + d3 = d, d4 = d; + + for (int i = 0; i < LY.size(); i++) { + + for (int j = i + 1; j < LY.size() && abs(LY[i].y - LY[j].y) < d; j++) { + + d4 = computeDistance(LY[i], LY[j]); + + if (d4 < d3) { + + d3 = d4; + + } + + } + + } + + d = minim(d, d3); + + + + } + + return d; + +} + + + +int main() + +{ + + read(); + + sort(points.begin(), points.end()); + + sort(Y.begin(), Y.end(), cmp); + + out << fixed << setprecision(6) << divide(0, n - 1, Y); + + return 0; + +} diff --git a/cmap/divide_conquer/6a43afdc-3d24-4b5f-a60c-ffa30b1bda24.cpp b/cmap/divide_conquer/6a43afdc-3d24-4b5f-a60c-ffa30b1bda24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fe9ff53f7882cc2d9915fc28a655d1647bb3a58 --- /dev/null +++ b/cmap/divide_conquer/6a43afdc-3d24-4b5f-a60c-ffa30b1bda24.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +int n,x,y; + +vector > X; + + + +long long sq(pair &i,pair &j){ + + + + return 1LL*(i.first-j.first)*(i.first-j.first)+ + + 1LL*(i.second-j.second)*(i.second-j.second); + + + +} + + + +long long minDist(int s,int d){ + + + + if(d-s<=2){ + + + + long long D=4e18; + + for(int i=s;i<=d;i++) + + for(int j=i+1;j<=d;j++) + + D=min(D,sq(X[i],X[j])); + + + + return D; + + + + } + + + + int m=(s+d)/2; + + + + long long D1=minDist(s,m); + + long long D2=minDist(m,d); + + long long D=min(D1,D2); + + + + vector > Y(X.begin()+s,X.begin()+d+1); + + for(int i=0;i > V; + + for(int i=0;i>n; + + + + for(int i=1;i<=n;i++){ + + + + cin>>x>>y; + + X.push_back(make_pair(x,y)); + + + + } + + + + sort(X.begin(),X.end()); + + + + cout< +#include +#include +#include +#include +using namespace std; + +const int Nmax = 100001; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x, y; + +}; + + + +punct Px[Nmax], Tmp[Nmax]; + +int N; + + + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + + + +inline long long patrat(long long x) + +{ + + return x * x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + return patrat(A.x - B.x) + patrat(A.y - B.y); + +} + + + +long long distMin(int p, int u) + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long dij = dist2(Px[i], Px[j]); + + if(dmin > dij)dmin = dij; + + } + + return dmin; + +} + + + +long long calcDmin(int p, int u) + +{ + + if(u - p < 3) + + return distMin(p, u); + + int m = (p + u) / 2; + + long long d1 = calcDmin(p, m); + + long long d2 = calcDmin(m, u); + + long long dd = min(d1, d2); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(patrat(Px[i].x - Px[m].x) < dd) + + Tmp[++k] = Px[i]; + + sort(Tmp + 1, Tmp + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && patrat(Tmp[j].y - Tmp[i].y) < dd; j++) + + { + + d1 = dist2(Tmp[i], Tmp[j]); + + if(dd > d1)dd = d1; + + } + + return dd; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> Px[i].x >> Px[i].y; + + sort(Px + 1, Px + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcDmin(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/6d2fd0d2-4961-4e38-be64-a10abf712f50.cpp b/cmap/divide_conquer/6d2fd0d2-4961-4e38-be64-a10abf712f50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00052dd9417eb5a263ee31523f7acd62d05b1ac6 --- /dev/null +++ b/cmap/divide_conquer/6d2fd0d2-4961-4e38-be64-a10abf712f50.cpp @@ -0,0 +1,78 @@ +#include + + + + +using namespace std; + +ifstream in("cmap.in"); +ofstream out("cmap.out"); + +int n; +pair pct[100001],cpy[100001]; +long long Z; + +long long dist(pair a,pair b) +{ + return (b.first-a.first)*(b.first-a.first)+(b.second-a.second)*(b.second-a.second); +} + +void Find(int st,int dr) +{ + if(dr<=st) + return; + + if(st+1==dr) + { + if(pct[st].second>pct[dr].second) + swap(pct[st],pct[dr]); + + Z=min(Z,dist(pct[st],pct[dr])); + + return; + } + + int mid=(st+dr)/2; + + Find(st,mid);Find(mid+1,dr); + + int p1=st,p2=mid+1; + int poz=st; + + while(p1<=mid && p2<=dr) + if(pct[p1].second<=pct[p2].second) + cpy[poz++]=pct[p1++]; + else + cpy[poz++]=pct[p2++]; + + while(p1<=mid) + cpy[poz++]=pct[p1++]; + + while(p2<=dr) + cpy[poz++]=pct[p2++]; + + for(int i=st;i<=dr;i++) + pct[i]=cpy[i]; + + for(int i=st;i<=dr;i++) + for(int j=i+1;j<=min(i+8,dr);j++) + Z=min(Z,dist(pct[i],pct[j])); +} + +int main() +{ + in>>n; + + for(int i=1;i<=n;i++) + in>>pct[i].first>>pct[i].second; + + sort(pct+1,pct+1+n); + + Z=dist(pct[1],pct[2]); + + Find(1,n); + + out< +#include + + +using namespace __gnu_pbds; +using namespace std; + + + + + + + +typedef pair< int, int > pii; +typedef pair< long long, long long > pll; +typedef long long ll; +typedef unsigned long long ull; +typedef vector< int > vi; +typedef vector< vi > vvi; +typedef vector< ll > vll; +typedef vector< vll > vvll; +typedef vector< pii > vpii; +typedef vector< vpii > vvpii; +typedef vector< pll > vpll; +typedef long double ld; +typedef vector< ld > vld; + +const ll MOD2 = 1e9 + 7; +const int MOD = 1e9 + 7; + +const ull infull = numeric_limits::max(); + +void fix(int &x, ll MOD) { + x = (x % MOD); + if(x < 0) x += MOD; + return; +} + +ll lgput(ll a, ll b, ll MOD) { + ll ret = 1; + a %= MOD; + while(b) { + if(b&1) ret = ret*a % MOD; + a = a*a % MOD; + b >>= 1; + } + + return ret; +} + +ll inv(ll a, ll MOD) { + return lgput(a, MOD-2, MOD); +} + +struct f { + int a, b; + f(int _a = 0, int _b = 0) : a(_a) ,b(_b) {} + ll eval(int x) { + return 1ll*x*a + b; + } +}; + +int binarySearch(int x, vi &v) { + int pos = 0; + int l = 1, r = (int)((v).size()); + while(l <= r) { + int m = l + r >> 1; + if(v[m] <= x) { + pos = m; + l = m + 1; + } else { + r = m - 1; + } + } + + return pos; +} + +struct pt { + int x, y, id; +}; + +struct cmp_x { + bool operator()(const pt & a, const pt & b) const { + return a.x < b.x || (a.x == b.x && a.y < b.y); + } +}; + +struct cmp_y { + bool operator()(const pt & a, const pt & b) const { + return a.y < b.y; + } +}; + +int n; +vector a; + +double mindist; +pair best_pair; + +void upd_ans(const pt & a, const pt & b) { + double dist = sqrt(1ll*(a.x - b.x)*(a.x - b.x) + 1ll*(a.y - b.y)*(a.y - b.y)); + if (dist < mindist) { + mindist = dist; + best_pair = {a.id, b.id}; + } +} +vector t; + +void rec(int l, int r) { + if (r - l <= 3) { + for (int i = l; i < r; ++i) { + for (int j = i + 1; j < r; ++j) { + upd_ans(a[i], a[j]); + } + } + sort(a.begin() + l, a.begin() + r, cmp_y()); + return; + } + + int m = (l + r) >> 1; + int midx = a[m].x; + rec(l, m); + rec(m, r); + + merge(a.begin() + l, a.begin() + m, a.begin() + m, a.begin() + r, t.begin(), cmp_y()); + copy(t.begin(), t.begin() + r - l, a.begin() + l); + + int tsz = 0; + for (int i = l; i < r; ++i) { + if (abs(a[i].x - midx) < mindist) { + for (int j = tsz - 1; j >= 0 && a[i].y - t[j].y < mindist; --j) + upd_ans(a[i], t[j]); + t[tsz++] = a[i]; + } + } +} + +int main() { + + + + + {freopen("cmap"".in","r",stdin);freopen("cmap"".out","w",stdout);}; + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + cout.precision(12); + srand(time(NULL)); + + cin >> n; + a.resize(n); + + for(int i = 0; i < n; ++i) { + cin >> a[i].x >> a[i].y; + } + + t.resize(n); + sort(a.begin(), a.end(), cmp_x()); + mindist = 1E20; + rec(0, n); + + cout << setprecision(12) << fixed << mindist << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/6fcdd5b9-3de9-4e43-b6db-d224e43a3735.cpp b/cmap/divide_conquer/6fcdd5b9-3de9-4e43-b6db-d224e43a3735.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96e5a141c1ad0b7cdad9449115b999c2879253a3 --- /dev/null +++ b/cmap/divide_conquer/6fcdd5b9-3de9-4e43-b6db-d224e43a3735.cpp @@ -0,0 +1,128 @@ +#include +#include #include +#include +#include +#include +#include +#include +#include +using namespace std; + +typedef long long LL; +const int MAXN = 100010; + +pair v[MAXN]; + +int auxv[MAXN], avsz; + +int N; + + + +LL dst(int i, int j) { + + int dx = v[i].first - v[j].first; + + int dy = v[i].second - v[j].second; + + return 1LL * dx * dx + 1LL * dy * dy; + +} + + + +LL brute(int left, int right) { + + LL res = LLONG_MAX; + + for (int i = left; i < right; ++i) + + for (int j = i + 1; j < right; ++j) + + res = min(res, dst(i, j)); + + return res; + +} + + + +bool smallerY(pair p1, pair p2) { + + if (p1.second != p2.second) return p1.second < p2.second; + + return p1.first < p2.first; + +} + + + +LL cmap(int left, int right) { + + int L = right - left; + + if (L <= 3) { + + sort(v + left, v + right, smallerY); + + return brute(left, right); + + } + + int mid = left + L / 2; + + int xmid = v[mid].first; + + LL delta = min(cmap(left, mid), cmap(mid, right)); + + inplace_merge(v + left, v + mid, v + right, smallerY); + + avsz = 0; + + for (int i = left; i < right; ++i) { + + int dx = v[i].first - xmid; + + if (1LL * dx * dx > delta) continue; + + auxv[avsz++] = i; + + } + + LL best = delta; + + for (int ii = 0; ii < avsz; ++ii) { + + int lim = min(avsz, ii + 8); + + for (int j = ii + 1; j < lim; ++j) + + best = min(best, dst(auxv[ii], auxv[j])); + + } + + return best; + +} + + + +int main() { + + assert(freopen("fis.in", "r", stdin)); + + assert(freopen("fis.out", "w", stdout)); + + scanf("%d", &N); + + for (int i = 0; i < N; ++i) + + scanf("%d%d", &v[i].first, &v[i].second); + + sort(v, v + N); + + printf("%.8lf\n", sqrt(cmap(0, N))); + + return 0; + +} diff --git a/cmap/divide_conquer/71759021-72ee-4fbe-976c-1b311f098419.cpp b/cmap/divide_conquer/71759021-72ee-4fbe-976c-1b311f098419.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb7b964ae8b28408c39fb17992b2cfccc6ddf12c --- /dev/null +++ b/cmap/divide_conquer/71759021-72ee-4fbe-976c-1b311f098419.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct Punct{ + + int x,y; + +}; + +Punct coord[100001],Banda[100001]; + +bool compx(const Punct &A,const Punct &B){ + + return A.x>n; + + for(int i=1;i<=n;i++) + + f>>coord[i].x>>coord[i].y; + + sort(coord+1,coord+n+1,compx); + + double d=sqrt(Divide(1,n)); + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +bool comp(pair a, pair b){ + + if(a.second == b.second){ + + return a.first < b.first; + + } + + return a.second < b.second; + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (X[st].second > X[st + 1].second) + + swap(X[st], X[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(X.begin() + st, X.begin() + mid, X.begin() + mid, X.begin() + dr, V.begin(), comp); + + copy(V.begin(), V.begin() + (dr - st), X.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(X[i].first - X[mid].first) <= best) + + V[v_size ++] = X[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + in >> n; + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + in >> X[i].first >> X[i].second; + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/73366467-159b-453c-8525-f3dbe8d6bda4.cpp b/cmap/divide_conquer/73366467-159b-453c-8525-f3dbe8d6bda4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e2b435f344f0352ce8fdf8b53892d99e1b75c47 --- /dev/null +++ b/cmap/divide_conquer/73366467-159b-453c-8525-f3dbe8d6bda4.cpp @@ -0,0 +1,360 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +typedef long long llong; + + + +struct Point + +{ + + int x, y; + +}; + + + +Point X[100005], candidates[100005]; + + + +void interclas(size_t st, size_t mij, size_t dr) + +{ + + Point* aux = new Point[dr - st + 1]; + + size_t i = st, j = mij + 1, k = 0; + + while (i <= mij && j <= dr) + + { + + if (X[i].y < X[j].y) + + aux[k++] = X[i++]; + + else + + aux[k++] = X[j++]; + + } + + + + if (i>mij) + + while (j <= dr) + + aux[k++] = X[j++]; + + else + + while (i <= mij) + + aux[k++] = X[i++]; + + + + for (size_t i = 0; i < k; i++) + + { + + X[st + i] = aux[i]; + + } + + delete[] aux; + +} + + + +bool increasingX(const Point a, const Point b) + +{ + + return a.x < b.x; + +} + + + +bool increasingY(const Point a, const Point b) + +{ + + return a.y < b.y; + +} + + + +llong min(const llong &a, const llong &b) + +{ + + return (a < b) ? a : b; + +} + + + +llong distance2(const Point a, const Point b) + +{ + + llong dx = (b.x - a.x); + + llong dy = (b.y - a.y); + + return dx*dx + dy*dy; + +} + + + +llong solveFast(size_t st, size_t dr, Point &first, Point &second) + +{ + + sort(X + st,X + dr,increasingY); + + llong mind = 0x7fffffffffffffffLL; + + + + int pos1 = -1, pos2 = -1; + + for (size_t i = st; i < dr; i++) + + { + + for (size_t j = i + 1; j <= dr; j++) + + { + + llong distaux = distance2(X[i], X[j]); + + if (distaux < mind) + + { + + mind = distaux; + + pos1 = i; + + pos2 = j; + + } + + + + } + + } + + + + first = X[pos1]; + + second = X[pos2]; + + return mind; + +} + + + +llong closestDistance(size_t st, size_t dr, Point &first, Point &second) + +{ + + if (dr - st < 3) + + { + + return solveFast(st, dr, first, second); + + } + + else + + { + + + + llong mij = (st + dr) / 2; + + + + int elemMij = X[mij].x; + + + + Point f1, f2, fmin, s1, s2, smin; + + + + llong d1 = closestDistance(st, mij, f1, s1); + + llong d2 = closestDistance(mij + 1, dr, f2, s2); + + + + llong delta = min(d1, d2); + + if (d1 == delta) + + { + + fmin = f1; + + smin = s1; + + } + + else + + { + + fmin = f2; + + smin = s2; + + } + + + + + + interclas(st, mij, dr); + + + + + + size_t nrOfCandidates = 0; + + for (size_t i = st; i <= dr; i++) + + if (pow(X[i].x - elemMij, 2) < delta) + + candidates[nrOfCandidates++] = X[i]; + + + + + + for (size_t i = 0; i < nrOfCandidates - 1; i++) + + { + + int l = 0; + + + + for (size_t j = i + 1; l <= 7 && j < nrOfCandidates; j++, l++) + + { + + llong d3 = distance2(candidates[i], candidates[j]); + + if (d3 < delta) + + { + + fmin = candidates[i]; + + smin = candidates[j]; + + delta = d3; + + } + + } + + } + + + + first = fmin; + + second = smin; + + return delta; + + } + +} + + + +int main() + +{ + + int n; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> n; + + + + int xx, yy; + + + + for (int i = 0; i < n; i++) + + { + + fin >> xx >> yy; + + X[i].x = xx; + + X[i].y = yy; + + } + + + + fin.close(); + + + + sort(X, X + n, increasingX); + + + + Point p1, p2; + + + + long double result = sqrt(closestDistance(0, n - 1, p1, p2)); + + + + fout << fixed << setprecision(6) << result << endl; + + + + + + fout << "(" << p1.x << "," << p1.y << ") , " << "(" << p2.x << "," << p2.y << ")\n"; + + + + return 0; + +} diff --git a/cmap/divide_conquer/734d806f-a259-4559-913e-5d7dbe0939c1.cpp b/cmap/divide_conquer/734d806f-a259-4559-913e-5d7dbe0939c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1f46867f61c8146eecba8370952cc5d3a671193 --- /dev/null +++ b/cmap/divide_conquer/734d806f-a259-4559-913e-5d7dbe0939c1.cpp @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + + + +struct punct + +{ + + int x; + + int y; + +}; + + + +int x1,y3,x2,y2; + + + +int comparare_x(punct a, punct b) + +{ + + return a.x < b.x; + +} + + + +int comparare_y(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +double distanta_euclidiana(punct a, punct b) + +{ + + double x = a.x - b.x; + + double y = a.y - b.y; + + double distanta; + + distanta = pow(x, 2) + pow(y, 2); + + distanta = sqrt(distanta); + + return distanta; + +} + + + +int citire(vector &vx, vector &vy) + +{ + + int n; + + in >> n; + + for (int i = 0; i < n; i++) + + { + + punct aux; + + in >> aux.x; + + in >> aux.y; + + vx.push_back(aux); + + } + + sort(vx.begin(), vx.end(), comparare_x); + + vy = vx; + + return 1; + +} + + + +int afisare(vector vx) + +{ + + for (auto it : vx) + + { + + out << it.x << " " << it.y << endl; + + } + + return 1; + +} + + + +double calculare_distanta_minima(vector &vx, int left, int right, vector &vy) + +{ + + if (right - left == 1) + + { + + return INT_MAX; + + } + + if (right - left == 2) + + { + return distanta_euclidiana(vx.at(0), vx.at(1)); + + } + + + + int mij = (left + right) / 2; + + + + double distanta_minima_stanga = calculare_distanta_minima(vx, left, mij, vy); + + double distanta_minima_dreapta = calculare_distanta_minima(vx, mij, right, vy); + + double distanta_minima_temporara = min(distanta_minima_stanga, distanta_minima_dreapta); + + + + vector puncte_auxiliare(right - left); + vector aux; + + + + for (int i = left; i < right; i++) + + { + + if (abs(vy.at(i).x - vx.at(mij).x) <= distanta_minima_temporara) + + { + + aux.push_back(vy.at(i)); + + } + + } + + + + sort(aux.begin(),aux.end(),comparare_y); + + + + double distanta_minima_finala = distanta_minima_temporara; + + + + for (int i = 0; i < aux.size(); i++) + + { + + for (int j = i + 1; j < aux.size() && aux.at(j).y - aux.at(i).y <= distanta_minima_temporara; j++) + + { + + double distanta_auxiliara = distanta_euclidiana(aux.at(i), aux.at(j)); + + if (distanta_auxiliara < distanta_minima_finala) + + { + + distanta_minima_finala = distanta_auxiliara; + + x1=aux.at(i).x; + + y3=aux.at(i).y; + + x2=aux.at(j).x; + + y2=aux.at(j).y; + + } + + } + + } + + return distanta_minima_finala; + +} + + + + + + + +int main() + +{ + + vector vx; + + vector vy; + + citire(vx, vy); + + + + out << fixed; + + out << setprecision(6) << calculare_distanta_minima(vx, 0, vx.size(), vy)< +#include +#include +#include +using namespace std; + + + + + +typedef long long i64; + +const int N=100001; + +const i64 I=4e18; + +vector> v,x,y; + +int n,i; + +i64 D(pair &a,pair &b) + +{ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + +i64 G(int s,int d,vector> &x,vector> &y) + +{ + + if(s>=d-1) + + return I; + + if(d-s==2) + + { + + if(y[s]>y[s+1]) + + swap(y[s],y[s+1]); + + return D(x[s],x[s+1]); + + } + + int m=(s+d)/2,e=0,i,j; + + i64 b=min(G(s,m,x,y),G(m,d,x,y)); + + sort(y.begin()+s,y.begin()+d); + + for(i=s;i +#include +#include +using namespace std; + +struct point { + +int x, y; + +}; + + + + + +point v[100000], aux[100000]; + +inline long long square( int x ) { + +return ( long long ) x * x;} + +inline double edist( point a, point b ) { + +return sqrt( square( a.x - b.x ) + square( a.y - b.y ) );} + +inline bool cmp( point a, point b ) { + +return ( a.x < b.x );} + +void read( FILE *fin, int &n ) { + +fscanf( fin, "%d", &n ); + +for ( int i = 0; i < n; ++i ) + +fscanf( fin, "%d%d", &v[i].x, &v[i].y );} + +void merge( int left, int middle, int right ) { + +int i = left, j = middle + 1, pos = left - 1; + +while ( i <= middle && j <= right ) + +if ( v[i].y <= v[j].y ) { + +aux[++pos] = v[i]; + +++i; + +} else { + +aux[++pos] = v[j]; + +++j;} + +while ( i <= middle ) { + +aux[++pos] = v[i]; + +++i;} + +while ( j <= right ) { + +aux[++pos] = v[j]; + +++j;} + +for ( i = left; i <= right; ++i ) + +v[i] = aux[i];} + +double min_dist( int left, int right ) { + +if ( left >= right ) + +return 1000000000.0; + +if ( left + 1 == right ) + +return edist( v[left], v[right] ); + +int middle = ( left + right ) >> 1, x_mid = v[middle].x; + +double dist_left = min_dist( left, middle ), dist_right = min_dist( middle + 1, right ); + +double dist = min( dist_left, dist_right ); + +merge( left, middle, right ); + +int count = 0; + +for ( int i = left; i <= right; ++i ) + +if ( abs( v[i].x - x_mid ) <= dist ) + +aux[++count] = v[i]; + +double res = dist; + +for ( int i = 1; i <= count; ++i ) + +for ( int j = i + 1; j <= count && j < i + 8; ++j ) + +res = min( res, edist( aux[i], aux[j] ) ); + +return res;} + +int main() { + +FILE *fin, *fout; + +fin = fopen ( "cmap.in", "r" ); + +int n; + +read( fin, n ); + +fclose( fin ); + +sort( v, v + n, cmp ); + +fout = fopen( "cmap.out", "w" ); + +fprintf( fout, "%.6f\n", min_dist( 0, n - 1 ) ); + +fclose( fout );} diff --git a/cmap/divide_conquer/75300b72-7392-4544-9d8f-5b0b4b0921d9.cpp b/cmap/divide_conquer/75300b72-7392-4544-9d8f-5b0b4b0921d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dd311763d3964b6080d861e10187bddd8612f90 --- /dev/null +++ b/cmap/divide_conquer/75300b72-7392-4544-9d8f-5b0b4b0921d9.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +struct point { + + long long x, y; + +}; +long long get_dist_sqr(point a, point b) { + + return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); + +} + + + +bool comparex(point a, point b) { + + + + + + return a.x < b.x; + +} + + + +bool comparey(point a, point b) { + + + + + + return a.y < b.y; + +} + + + +long long get_min_strip(long long dmin, vector strip) { + + + + unsigned n = strip.size(); + + for (unsigned i = 0; i < n - 1; i++) + + for (unsigned j = i + 1; j < n and j < i + 8; j++) { + + if (get_dist_sqr(strip[i], strip[j]) < dmin) + + dmin = get_dist_sqr(strip[i], strip[j]); + + } + + return dmin; + +} + + + +long long distantaMinima(int st, int dr, vector &pointx, vector &pointy) { + + + + if (dr - st <= 3) { + + long long dmin = get_dist_sqr(pointx[st], pointx[st + 1]); + + for (unsigned i = st; i < dr; i++) + + for (unsigned j = i + 1; j <= dr; j++) + + if (get_dist_sqr(pointx[i], pointx[j]) < dmin) + + dmin = get_dist_sqr(pointx[i], pointx[j]); + + return dmin; + + } + + + + else { + + int mij = (st + dr) / 2; + + point midPoint = pointx[mij]; + + + + vector ord_st, ord_dr; + + for (unsigned i = 0; i < pointy.size(); i++) { + + if (pointy[i].x <= midPoint.x) + + ord_st.push_back(pointy[i]); + + else + + ord_dr.push_back(pointy[i]); + + } + + + + long long d1 = distantaMinima(st, mij, pointx, ord_st); + + long long d2 = distantaMinima(mij + 1, dr, pointx, ord_dr); + + long long dmin = d1 < d2 ? d1 : d2; + + + + vector fasie; + + for (unsigned i = 0; i < pointy.size(); i++) { + + if (pointy[i].x > midPoint.x - sqrt(dmin) and + + pointy[i].x < midPoint.x + sqrt(dmin)) + + fasie.push_back(pointy[i]); + + + + + + } + + + + return get_min_strip(dmin, fasie); + + + + + + } + +} + + + +int main() + +{ + + int n; + + f >> n; + + vector pointx(n), pointy(n); + + for (int i = 0; i < n; i++) { + + f >> pointx[i].x >> pointx[i].y; + + pointy[i] = pointx[i]; + + } + + + + sort(pointx.begin(), pointx.end(), comparex); + + sort(pointy.begin(), pointy.end(), comparey); + + + + g << fixed << std::setprecision(8) << sqrt(distantaMinima(0, n - 1, pointx, pointy)); + + return 0; + +} diff --git a/cmap/divide_conquer/759fcb73-52cc-402e-b4e6-419c7521fc2c.cpp b/cmap/divide_conquer/759fcb73-52cc-402e-b4e6-419c7521fc2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c9c4deb3877f079b5b913eff88c072c793fce05 --- /dev/null +++ b/cmap/divide_conquer/759fcb73-52cc-402e-b4e6-419c7521fc2c.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout("cmap.out"); + + + +long long n,i; + +pair< long long, long long > v[100010],a[100010],b[100010]; + + + +long long d(pair< long long, long long > a,pair< long long, long long > b){ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + +void ic(long long st,long long mid,long long dr){ + + long long i=st; + + long long j=mid+1; + + long long k=st-1; + + while (i<=mid && j<=dr) + + if (v[i].second>n; + + for (i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + fout< + +using namespace std; + +struct point { + long long x, y; +} p[100005]; + +double dist(point a, point b) { + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + +double closest_points(point *p, int a, int b, vector &v) { + int i, j; + double Min = DBL_MAX; + auto cmp = [&](int x, int y) -> bool { return p[x].y < p[y].y; }; + + if (b - a + 1 <= 3) { + v.clear(); + for (i = a; i <= b; i++) { + for (j = i + 1; j <= b; j++) + Min = min(Min, dist(p[i], p[j])); + + v.push_back(i); + } + sort(v.begin(), v.end(), cmp); + + } else { + int m = (a + b) >> 1; + + vector st(m - a + 1), dr(b - m), band; + double q = closest_points(p, a, m, st); + double w = closest_points(p, m + 1, b, dr); + + double xd = (p[m].x + p[m + 1].x) / 2; + double d = min(q, w); + Min = d; + + merge(st.begin(), st.end(), dr.begin(), dr.end(), v.begin(), cmp); + + + for (auto &e: v) + if (fabs(p[e].x - xd) <= d) band.push_back(e); + + + for (i = 0; i < band.size(); i++) + for (j = i + 1; j < min(i + 8, (int) band.size()); j++) + Min = min(Min, dist(p[band[i]], p[band[j]])); + } + + return Min; +} + +int main() { + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + int n, i; + scanf("%i", &n); + for (i = 1; i <= n; i++) + scanf("%lld%lld", &p[i].x, &p[i].y); + + + sort(p + 1, p + n + 1, [](point &a, point &b) -> bool { return a.x < b.x; }); + + vector v(n); + printf("%.8lf", closest_points(p, 1, n, v)); + return 0; +} diff --git a/cmap/divide_conquer/76f1a017-74f2-4ceb-acc7-5bb361612a7d.cpp b/cmap/divide_conquer/76f1a017-74f2-4ceb-acc7-5bb361612a7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..701209e38fbbd6109257a08dda4487af9dac3d7a --- /dev/null +++ b/cmap/divide_conquer/76f1a017-74f2-4ceb-acc7-5bb361612a7d.cpp @@ -0,0 +1,162 @@ +#include +#include +#include #include +typedef std::pair Point; + +FILE *fin = freopen("cmap.in", "r", stdin); FILE *fout = freopen("cmap.out", "w", stdout); + + + +const int maxN = 1e5 + 5; + +const long long inf = 1e18; + +const int maxDiff = 8; + + + + + +int n; + +Point pts[maxN], aux[maxN]; + + + + +void ReadInput() { + + scanf("%d", &n); + + for(int i = 0; i < n; i++) { + + scanf("%d%d", &pts[i].first, &pts[i].second); + + } + +} + + + +void Merge(int left, int mid, int right) { + + int p1 = left, p2 = mid + 1, pos = 0; + + + + while(p1 <= mid && p2 <= right) { + + if(pts[p1].second < pts[p2].second) { + + aux[pos] = pts[p1]; p1++; pos++; + + } else { + + aux[pos] = pts[p2]; p2++; pos++; + + } + + } + + + + while(p1 <= mid) { + + aux[pos] = pts[p1]; p1++; pos++; + + } + + while(p2 <= right) { + + aux[pos] = pts[p2]; p2++; pos++; + + } + + + + for(int i = 0; i < pos; i++) { + + pts[left + i] = aux[i]; + + } + +} + + + +long long Dist(Point A, Point B) { + + return 1LL * (A.first - B.first) * (A.first - B.first) + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long Divide(int left, int right) { + + + + if(right - left < 2) { + + if(right == left) { + + return inf; + + } else { + + Merge(left, left, right); + + return Dist(pts[left], pts[right]); + + } + + } + + + + + + int mid = (left + right) / 2; + + long long d1 = Divide(left, mid), d2 = Divide(mid + 1, right); + + + + + + long long d = std::min(d1, d2); + + Merge(left, mid, right); + + + + for(int i = left; i <= right; i++) { + + for(int j = i - 1; j >= left && i - j < maxDiff; j--) { + + d = std::min(d, Dist(pts[i], pts[j])); + + } + + } + + return d; + +} + + +int main() { + ReadInput(); + + + + std::sort(pts, pts + n); + + + + printf("%.10f\n", std::sqrt((double)Divide(0, n - 1))); + + + + return 0; +} diff --git a/cmap/divide_conquer/78fadf5b-da57-4f51-8ff2-8555848415f9.cpp b/cmap/divide_conquer/78fadf5b-da57-4f51-8ff2-8555848415f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abe10dd201a27bb21ca2ff21db693d09c735a8f0 --- /dev/null +++ b/cmap/divide_conquer/78fadf5b-da57-4f51-8ff2-8555848415f9.cpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + + + +struct punct + +{ + + long long x,y; + +}; + + + +long long distanta(punct x, punct y) + +{ + + long long X = x.x - y.x; + + long long Y = x.y - y.y; + + + + return X*X + Y*Y; + +} + + + +void citire(vector &v, int &n) + +{ + + ifstream fin("cmap.in"); + + + + fin>>n; + + v.resize(n); + + + + for(int i=0;i>v[i].x>>v[i].y; + + + + fin.close(); + +} + + + +bool cmp(punct x, punct y) + +{ + + return ((x.x&v, int st, int m, int dr) + +{ + + int i = st, j= m+1; + + vectoraux; + + + + while(i <=m && j <= dr) + + { + + if(v[i].y < v[j].y) + + aux.push_back(v[i++]); + + else aux.push_back(v[j++]); + + } + + + + while(i <= m) + + aux.push_back(v[i++]); + + + + while(j <= dr) + + aux.push_back(v[j++]); + + + + for(i = st, j = 0; i<= dr; i++,j++) + + v[i] = aux[j]; + +} + + + +long long DIVIDE_ET_IMPERA( vector &v, int st, int dr) + +{ + + if(st>=dr) + + return 4e18; + + + + else if(dr-st==1) + + { + + long long minim = distanta(v[st],v[dr]); + + + + + + if(v[st].y > v[dr].y) + + swap(v[st],v[dr]); + + + + return minim; + + + + } + + else if(dr-st==2) + + { + + long long minim = min(distanta(v[st],v[st+1]), min(distanta(v[st+1],v[dr]),distanta(v[st],v[dr]))); + + + + interclasare(v,st,st+1,dr); + + return minim; + + } + + + + else + + { + + int m = (st + dr)/2; + + + + long long minim=min(DIVIDE_ET_IMPERA(v,st,m),DIVIDE_ET_IMPERA(v,m+1,dr)); + + + + interclasare(v,st,m,dr); + + + + + + vector aux; + + for(int i=st ; i<=dr ; i++) + + if(abs(v[i].x - v[m].x) < minim) + + aux.push_back(v[i]); + + + + int ssize=aux.size(); + + + + + + for(int i=0;i v; + + ofstream fout("cmap.out"); + + citire(v,n); + + sort(v.begin(),v.end(),cmp); + + + + fout< +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +long long dist(pair p1, pair p2) + +{ + + return (long long)(p1.first-p2.first)*(p1.first-p2.first)+(long long)(p1.second-p2.second)*(p1.second-p2.second); + +} + +bool CmpX(pairp1,pairp2) + +{ return p1.firstp1,pairp2) + +{ return p1.second > &v,int st,int dr) + +{ if(dr-st==1) + + return dist(v[st],v[dr]); + + if(dr-st==2) + + { + + long long d1,d2,d3; + + d1=dist(v[st],v[dr]); + + d2=dist(v[st+1],v[dr]); + + d3=dist(v[st],v[st+1]); + + return min(d1,min(d2,d3)); + + } + + int m=(st+dr)/2; + + long long d,d1,d2,delta; + + int i,j; + + d1=minDistance(v,m+1,dr); + + d2=minDistance(v,st,m); + + d=min(d1,d2); + + delta=(int) ceil(sqrt(d)); + + vector > temp; + + for(i=st;i<=dr;i++) + + if(abs(v[i].first-v[m].first)<=delta) + + temp.push_back(v[i]); + + sort(temp.begin(),temp.end(),CmpY); + + for(i=0;i > v; + + fin>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),CmpX); + + + + fout< +#include +#include +#include +#include + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +class Point { + public: + double x, y; + + Point() {}; + + static double distance(Point a, Point b) { + double dx = a.x - b.x; + double dy = a.y - b.y; + return dx * dx + dy * dy; + } +}; + +const int NMAX = 100000 + 1; + +int n; +Point points[NMAX]; +Point aux[NMAX]; +Point middlePoints[NMAX]; + +bool comparator(Point a, Point b) { + return a.x < b.x; +} + +void readData() { + f >> n; + for (int i = 0; i < n; ++i) { + f >> points[i].x; + f >> points[i].y; + } +} + +int interclaseaza(double x, double d, int start, int end) { + int mid = (start + end) / 2; + int i = start; + int j = mid + 1; + int k = 0; + int middlePointsCount = 0; + + while (i <= mid && j <= end) { + if (points[i].y <= points[j].y) { + if (x - points[i].x < d) + middlePoints[middlePointsCount++] = points[i]; + aux[k++] = points[i++]; + } + else { + if (points[j].x - x < d) + middlePoints[middlePointsCount++] = points[j]; + aux[k++] = points[j++]; + } + } + + for (; i <= mid; ++i) { + aux[k++] = points[i]; + if (x - points[i].x < d) + middlePoints[middlePointsCount++] = points[i]; + } + + for (; j <= end; ++j) { + aux[k++] = points[j]; + if (points[j].x - x < d) + middlePoints[middlePointsCount++] = points[j]; + } + + + for (int l = 0; l < k; ++l) + points[start + l] = aux[l]; + + return middlePointsCount; +} + +double getClosestPair(int start, int end) { + if (start + 1 == end) { + if (points[end].y < points[start].y) { + Point aux = points[end]; + points[end] = points[start]; + points[start] = aux; + } + return Point::distance(points[start], points[end]); + } + + if (start >= end) return Point::distance(points[0], points[1]); + + int mid = (start + end) / 2; + double middleX = points[mid].x; + double sol = min( + getClosestPair(start, mid), + getClosestPair(mid + 1, end) + ); + + int k = interclaseaza(middleX, sol, start, end); + + for (int i = 0; i < k; ++i) { + int lim = min(i + 8, k - 1); + for (int j = i + 1; j <= lim; ++j) + sol = min(sol, Point::distance(middlePoints[i], middlePoints[j])); + } + + return sol; +} + +int main() { + readData(); + sort(points, points + n, comparator); + g << fixed << std::setprecision(9) << sqrt(1.0 * getClosestPair(0, n - 1)) << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/7a25e09c-f9b3-4765-9db6-7e121e3d6e72.cpp b/cmap/divide_conquer/7a25e09c-f9b3-4765-9db6-7e121e3d6e72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31738e77fe3e6d53bb25add22367f40a135dd285 --- /dev/null +++ b/cmap/divide_conquer/7a25e09c-f9b3-4765-9db6-7e121e3d6e72.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; +vector < pair > V(100005), X, Y; + + + +long long dist(const pair &a, const pair &b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + + + + + if (dr - st == 1 ) + + { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + + + + + } + + + + if (dr - st == 2) + + { + + sort(Y.begin() + st ,Y.begin() + dr + 1); + + + + return min(dist(X[st], X[st + 1]),min(dist(X[st],X[dr]),dist(X[st+1],X[dr]))); + + } + + + + + + + + int mid = (st + dr + 1) / 2; + + + + long long solution1=go(st, mid, X, Y); + + long long solution2=go(mid, dr, X, Y); + + long long solution=min(solution1,solution2); + + + + + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr + 1 , V.begin()); + + copy(V.begin(), V.begin() + (dr - st) + 1, Y.begin() + st); + + + + + + + + int v_size = 0; + + for (int i = st; i <= dr; ++ i) if (abs(Y[i].second - X[mid].first) <= solution) + + V[v_size ++] = Y[i]; + + + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + solution = min(solution, dist(V[i], V[j])); + + } + + return solution; + +} + + + +int main(void) { + + int n; + + ifstream in("cmap.in"); + + in>>n; + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < X.size(); ++ i) + + in>>X[i].first>>X[i].second; + + + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out("cmap.out"); + + + + out << fixed << setprecision(6) << sqrt(go(0, X.size() - 1, X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/7ab5d25a-9b91-4420-8c8f-2229341565e9.cpp b/cmap/divide_conquer/7ab5d25a-9b91-4420-8c8f-2229341565e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec56235ca4cfe3165075d3f549b329e8390f843e --- /dev/null +++ b/cmap/divide_conquer/7ab5d25a-9b91-4420-8c8f-2229341565e9.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct Point { + long long x, y; + + friend struct byX; + struct byX { + bool operator() (Point const &p1, Point const &p2) { + return p1.x < p2.x; + } + }; + + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + + + + + +void read(long long &n, vector &points) { + ifstream fin("cmap.in"); + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); +} + + +double distance(const Point &A, const Point &B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + + + + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + double auxDist = distance(points[i], points[j]); + if (minDist > auxDist) { + minDist = auxDist; + } + } + } + + return minDist; +} + + +double divide(int li, int ls, vector &P, vector &Q) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, P); + } + + + int mid = floor((li + ls - 1)/2.0); + double redLine = (P[mid].x + P[mid + 1].x)/2.0; + + + + vector Ql; + Ql.push_back(Q[0]); + vector Qr; + Qr.push_back(Q[0]); + for (int i = 1; i < Q.size(); i++) { + if (Q[i].x <= redLine) { + Ql.push_back(Q[i]); + } else { + Qr.push_back(Q[i]); + } + } + + + double dl = divide(li, mid, P, Ql); + double dr = divide(mid + 1, ls, P, Qr); + double dmin = min(dl, dr); + + + + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = 1; i < Q.size(); i++) { + if (abs(redLine - Q[i].x) <= dmin) { + strip.push_back(Q[i]); + } + } + + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 6 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + return dmin; +} + + +int main() { + + long long n; + vector P; + + read(n, P); + + vector Q = P; + + sort(P.begin() + 1, P.end(), Point::byX()); + + sort(Q.begin() + 1, Q.end(), Point::byY()); + + + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, P, Q); + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/7aef1ebd-fd35-4960-90b7-2f3561288657.cpp b/cmap/divide_conquer/7aef1ebd-fd35-4960-90b7-2f3561288657.cpp new file mode 100644 index 0000000000000000000000000000000000000000..151cfddc1a90f29593f0a0a92e4984e86e4271fe --- /dev/null +++ b/cmap/divide_conquer/7aef1ebd-fd35-4960-90b7-2f3561288657.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + +std::ifstream fin("cmap.in"); +std::ofstream fout("cmap.out"); + +int n; +std::vector > V; + +void citire() { + fin >> n; + int x, y; + for (int i = 1; i <= n; i++) { + fin >> x >> y; + V.emplace_back(x, y); + } +} + +bool cmp(const std::pair &p1, const std::pair &p2) { + return (p1.second < p2.second); +} + +long long distanta_euclidiana_la_patrat(const std::pair &p1, const std::pair &p2) { + return (long long) (p1.first - p2.first) * (long long) (p1.first - p2.first) + + (long long) (p1.second - p2.second) * (long long) (p1.second - p2.second); +} + + +long long Solve(int st, int dr) { + + if (dr - st == 1) { + return distanta_euclidiana_la_patrat(V[st], V[dr]); + } else if (dr - st == 2) { + return std::min(distanta_euclidiana_la_patrat(V[st], V[st + 1]), + std::min(distanta_euclidiana_la_patrat(V[st], V[st + 2]), + distanta_euclidiana_la_patrat(V[st + 1], V[st + 2]))); + } + + int mij = (st + dr) / 2; + + long long d = std::min(Solve(st, mij), Solve(mij + 1, dr)); + + std::vector > A; + + auto delta = static_cast(std::ceil(std::sqrt(d))); + for (int i = st; i <= dr; i++) { + if (std::abs(V[i].first - V[mij].first) <= delta) { + A.push_back(V[i]); + } + } + + sort(A.begin(), A.end(), cmp); + + for (int i = 0; i < A.size(); i++) { + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) { + d = std::min(d, distanta_euclidiana_la_patrat(A[i], A[j])); + } + } + return d; +} + +int main() { + citire(); + + sort(V.begin(), V.end()); + + fout << std::fixed << std::setprecision(6) << sqrt(Solve(0, n-1)) << '\n'; + + return 0; +} diff --git a/cmap/divide_conquer/7af53966-1e9a-4f04-a748-90769bbc259e.cpp b/cmap/divide_conquer/7af53966-1e9a-4f04-a748-90769bbc259e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51f7902a614d8815ec00e880e2dc921779608a14 --- /dev/null +++ b/cmap/divide_conquer/7af53966-1e9a-4f04-a748-90769bbc259e.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + long long a, b; + +}; + +punct x[100001], y[100001], aux[100001]; + +bool crit(punct aa, punct bb) + +{ + + return ((aa.a < bb.a) || (aa.a == bb.a && aa.b < bb.b)); + +} + +double dist(punct aa, punct bb) + +{ + + return (aa.a - bb.a) * (aa.a - bb.a) + (aa.b - bb.b) * (aa.b - bb.b); + +} + +long long solutie(int st, int dr, punct *X, punct *Y) + +{ + + if(st >= dr - 1) + + return LLONG_MAX; + + if(dr - st == 2) + + { + + if(!crit(Y[st] , Y[st + 1])) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int m = (dr + st) / 2; + + double minim = min(solutie(st, m, X, Y), solutie(m, dr, X, Y)); + + merge(Y + st, Y + m, Y + m, Y + dr, aux, crit); + + copy(aux, aux + (dr - st), Y + st); + + int it = 0; + + for(int i = st; i < dr; i++) if(abs(Y[i].b - X[m].a) <= minim) + + aux[it ++] = Y[i]; + + for(int i = 0; i < it; i++) + + { + + for(int j = i + 1; j < it && j - i < 8; j++) + + minim = min(minim, dist(aux[i], aux[j])); + + } + + return minim; + +} + +int main() + +{ + + int n; + + f >> n; + + for(int i = 0; i < n; i++) + + f >> x[i].a >> x[i].b; + + sort(x, x + n, crit); + + for(int i = 0; i < n; i++) + + { + + y[i].a = x[i].b; + + y[i].b = x[i].a; + + } + + g << fixed << setprecision(6) << sqrt(solutie(0, n - 1, x, y)); + + return 0; + +} diff --git a/cmap/divide_conquer/7bfa69ab-3d28-4f32-a6b4-5b0916a8a2dc.cpp b/cmap/divide_conquer/7bfa69ab-3d28-4f32-a6b4-5b0916a8a2dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b15f3c71083be483db097161f1975cdb36d03672 --- /dev/null +++ b/cmap/divide_conquer/7bfa69ab-3d28-4f32-a6b4-5b0916a8a2dc.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef pair pii; + +pii a[100005]; + +pii r[100005]; + +pii v[100005]; + +int n,ps,pd; + +long long dist(pii A,pii B) + +{ + + return (long long)(A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second); + +} + +long long re(int st,int dr) + +{ + + int m=0; + + if(st==dr) + + return 1000000000000000000000LL; + + int mij=(st+dr)/2; + + long long rez=min(re(st,mij),re(mij+1,dr)); + + ps=st; + + pd=mij+1; + + for(int i=st;i<=dr;i++) + + if((a[ps].seconddr)&&ps<=mij) + + { + + v[i]=a[ps]; + + ps++; + + } + + else + + { + + v[i]=a[pd]; + + pd++; + + } + + for(int i=st;i<=dr;i++) + + a[i]=v[i]; + + for(int i=st;i<=dr;i++) + + if(abs(a[i].first-a[mij].first)<=rez) + + { + + m++; + + r[m]=a[i]; + + } + + for(int i=1;i<=m;i++) + + for(int j=max(1,i-7);j>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + t=re(1,n); + + fout< +#include +#include +#include +#include +#include. +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +class POINT + +{ + +public : + + long long x,y; + + + + + + + + friend bool mycompx(POINT a,POINT b); + + friend bool mycompy(POINT a,POINT b); + + + + + + friend long long point_distance(POINT a, POINT b); + + + +}; + + + +bool mycompy(POINT a,POINT b) + +{ + + return a.y < b.y; + +} + + + + + +bool mycompx(POINT a,POINT b) + +{ + + return a.x < b.x; + +} + + + +long long squared(long long x) + +{ + + return x*x; + + + +} + + + + + +POINT v_sortedx[100000 + 5]; + +POINT strip[100000 + 5]; + + + + + +int n; + + + +void read_data() + +{ + + in >> n; + + + + for(int i = 1 ; i<=n ; ++i) + + { + + in>>v_sortedx[i].x; + + in>>v_sortedx[i].y; + + } + + + +} + + + +long long point_distance(POINT a, POINT b) + +{ + + long long lat_1 = b.x - a.x; + + long long lat_2 = b.y - a.y; + + + + return lat_1*lat_1 + lat_2*lat_2; + +} +long long small_dist(int left,int right) + +{ + + + + int i,j; + + long long dist = 0xFFFFFFFFFFFFF; + + + + for(i = left ; i < right ; ++i) + + for( j = i+1 ; j <=right ; ++j) + + { + + long long temp = point_distance(v_sortedx[i],v_sortedx[j]); + + if( temp < dist) + + dist = temp; + + } + + + + return dist; + +} + + + + + +long long min_distance(int left,int right) + +{ + + if(right - left < 3) + + { + + return small_dist(left,right); + + } + + + + + + int m = (left + right ) / 2; + + + + long long d1 = min_distance(left,m); + + long long d2 = min_distance(m,right); + + + + long long d = d1 < d2 ? d1 : d2; + + + + int k = 0; + + + + for(int i = left ; i<=right ; ++i) + + { + + if(squared(v_sortedx[i].x - v_sortedx[m].x) < d) + + strip[++k] = v_sortedx[i]; + + } + + + + sort(strip+1,strip+k+1,mycompy); + + + + int l; + + for(int i = 1 ; i<=k ; ++i) + + { + + l = 0; + + for(int j = i + 1; l<7 && j <=k ; ++j,++l) + + { + + long long dist = point_distance(strip[i],strip[j]); + + if(dist < d) + + d = dist; + + } + + } + + + + return d; + +} +int main() + +{ + + read_data(); + + + + + + sort(v_sortedx+1,v_sortedx+n+1,mycompx); + + + + long long d; + + + + d = min_distance(1,n); + + + + out< +#include +#include +#include + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +long long n,i,j; + +pair v[100001],a[100001],b[100001]; + + + +long long inter(long long st,long long mid,long long dr){ + + long long i=st; + + long long j=mid+1; + + long long k=st-1; + + + + while(i<=mid&&j<=dr){ + + if(v[i].second a,pair b){ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + +long long divide(long long st, long long dr){ + + long long sol,i,j; + + + + if(dr-st==1){ + + inter(st,st,dr); + + return dist(v[st],v[dr]); + + } + + if(dr-st==2){ + + inter(st,st,dr-1); + + inter(st,dr-1,dr); + + return min(dist(v[st],v[dr]),min(dist(v[st+1],v[st]),dist(v[st+1],v[dr]))); + + } + + + + long long mid=(st+dr)/2; + + + + sol=min(divide(st,mid),divide(mid+1,dr)); + + inter(st,mid,dr); + + + + long long cnt=0; + + for(i=st;i<=dr;i++){ + + if(abs(v[mid].first-v[i].first)<=sol){ + + b[++cnt]=v[i]; + + } + + } + + + + for(i=1;i>n; + + for(i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + + + sort(v+1,v+n+1); + + fout< + +using namespace std; + +typedef long long int ll; +typedef long double ld; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const ll INF = 1e18; + +vector < pair < ll, ll > > points; +vector < pair < ll, ll > > strip; + +bool cmp(const pair < ll, ll > &a, const pair < ll, ll > &b) { + return a.second < b.second; +} +ll getDistance(const pair < ll, ll > &a, const pair < ll, ll > &b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} +ll Solve(int s, int e) { + if(e - s <= 3) { + ll dist = INF; + for(int i = s; i < e; ++i) { + for(int j = i + 1; j < e; ++j) { + dist = min(dist, getDistance(points[i], points[j])); + } + } + return dist; + } + int mid = (s + e) / 2; + ll dist = min(Solve(s, mid), Solve(mid, e)); + + int idx = 0; + for(int i = s; i < e; ++i) { + if(abs(points[mid].first - points[i].first) < dist) { + strip[idx++] = points[i]; + } + } + + sort(strip.begin(), strip.begin() + idx, cmp); + + for(int i = 0; i < idx; ++i) { + for(int j = i + 1; j < idx && j - i <= 7; ++j) { + dist = min(dist, getDistance(strip[i], strip[j])); + } + } + return dist; +} + +int main() { + int n; fin >> n; + points.resize(n); strip.resize(n); + for(int i = 0; i < n; ++i) { + fin >> points[i].first >> points[i].second; + } + + sort(points.begin(), points.end()); + fout << fixed << setprecision(10) << sqrtl((ld)Solve(0, n)); + return 0; +} diff --git a/cmap/divide_conquer/7e1ab134-28be-42d0-b2f5-d8d983de9a09.cpp b/cmap/divide_conquer/7e1ab134-28be-42d0-b2f5-d8d983de9a09.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9496d5e0e9e5feee46ffee12854b0fd4acfda6ae --- /dev/null +++ b/cmap/divide_conquer/7e1ab134-28be-42d0-b2f5-d8d983de9a09.cpp @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +struct PUNCT + +{ + + double x, y; + +}; + + + + + +PUNCT p[99999], aux[99999], inter[99999], A, B; + + + +double distanta(PUNCT a, PUNCT b) + +{ + + return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)); + +} + + + +bool cmpX(PUNCT a, PUNCT b) + +{ + + return a.x < b.x; + +} + + + +bool cmpY(PUNCT a, PUNCT b) + +{ + + return a.y < b.y; + +} + + + +double dist_min(int left, int right) + +{ + + if(right - left + 1 == 3) + + { + + sort(p + left, p + right, cmpY); + + double d1 = distanta(p[left], p[left + 1]); + + double d2 = distanta(p[left + 1], p[left + 2]); + + double d3 = distanta(p[left], p[left + 2]); + + double minim = d1; + + A = p[left]; + + B = p[left + 1]; + + if(d2 < minim) + + { + + minim = d2; + + A = p[left + 1]; + + B = p[left + 2]; + + } + + if(d3 < minim) + + { + + A = p[left]; + + B = p[left + 2]; + + minim = d3; + + } + + return minim; + + } + + if(right - left + 1 == 2) + + { + + if(p[left].y > p[left + 1].y) + + swap(p[left], p[left + 1]); + + A = p[left]; + + B = p[left + 1]; + + return distanta(p[left], p[left + 1]); + + } + + else + + { + + int mijloc = (left + right) / 2; + + double mediana = p[mijloc].x; + + double delta = min(dist_min(left, mijloc), dist_min(mijloc + 1, right)); + + int nr = 0, i, j, k; + + + + + + i = left; + + k = left; + + j = mijloc + 1; + + while(i <= mijloc && j <= right) + + if(p[i].y < p[j].y) + + aux[k++] = p[i++]; + + else aux[k++] = p[j++]; + + while(i <= mijloc) + + aux[k++] = p[i++]; + + while(j <= right) + + aux[k++] = p[j++]; + + + + for(i = left; i <= right; i++) + + { + + p[i] = aux[i]; + + if(p[i].x - mediana <= delta && p[i].x - mediana >= -delta) + + inter[++nr] = p[i]; + + } + + + + for(i = 1; i <= nr; i++) + + { + + int nr_vecini = min(nr, i + 7); + + for(j = i + 1; j <= nr_vecini; j++) + + if(distanta(inter[i], inter[j]) < delta) + + { + + delta = distanta(inter[i],inter[j]); + + A = inter[i]; + + B = inter[j]; + + } + + } + + return delta; + + } + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n, i; + + fin >> n; + + for(i = 1; i <= n; i++) + + fin >> p[i].x >> p[i].y; + + sort(p + 1 , p + n + 1, cmpX); + + fout << setprecision(6) << fixed << dist_min(1, n); + + return 0; + +} diff --git a/cmap/divide_conquer/7ecc801c-bd9d-424e-a602-9ae7ebeb0c5e.cpp b/cmap/divide_conquer/7ecc801c-bd9d-424e-a602-9ae7ebeb0c5e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35a351073df9b705d1cdb86f449f01c6dfa6ace6 --- /dev/null +++ b/cmap/divide_conquer/7ecc801c-bd9d-424e-a602-9ae7ebeb0c5e.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +using namespace std; + +struct nod + +{ + + long long x,y; + +}; + +int n; + +inline bool compare(nod a,nod b) + +{ + + if(a.x!=b.x) + + return a.x3) + + { + + double mn=min(dist(s,(s+d)/2),dist((s+d)/2,d)); + + int k=0,ls,ld,i; + + double c=(v[(s+d)/2].x+v[(s+d)/2-1].x)/2; + + i=(s+d)/2-1; + + while(c-v[i].x<=mn && i>=s) + + C[k++]=v[i--]; + + i=(s+d)/2; + + while(v[i].x-c<=mn && i +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + int x, y; + +}; + + + +bool cmp(Point a, Point b) { + + return a.x < b.x; + +} + + + +long long sqlong(long long x) { + + return x*x; + +} + + + +long long dist(Point a, Point b) { + + return sqlong(a.x-b.x)+sqlong(a.y-b.y); + +} + + + +long long minDist(vector::iterator a, vector::iterator b) { + + long long d; + + vector::iterator i, j; + + if (b-a<=3) { + + d = dist(*a, *(a+1)); + + for (i = a; i < b-1; i++) { + + for (j = i+1; j < b; j++) { + + if (d > dist(*i, *j)) { + + d = dist(*i, *j); + + } + + } + + } + + return d; + + } + + int n = (b-a)/2; + + d = min(minDist(a, a+n), minDist(a+n, b)); + + for (i = a; i>n; + + vector a(n); + + for (int i = 0; i < n; i++) { + + cin>>a[i].x>>a[i].y; + + } + + sort(a.begin(), a.end(), cmp); + cout< + + +using namespace std; + + + +const long long inf=1e18; + +struct punct + +{ + + int x,y; + +} v[100010],v1[100010]; + + + +int cmp1(punct a,punct b) + +{ + + return a.x=dr) return inf; + + if(st+1==dr) + + { + + if(cmp2(v[dr],v[st])) swap(v[st],v[dr]); + + return dist(v[st],v[dr]); + + } + + int mid=(st+dr)/2,midx=v[mid].x; + + long long sol=min(solve(st,mid),solve(mid+1,dr)); + + merge(v+st,v+mid+1,v+mid+1,v+dr+1,v1+st,cmp2); + + int nr=0; + + for(int i=st; i<=dr; i++) + + { + + v[i]=v1[i]; + + if(sqr(v1[i].x-midx)<=sol) v1[++nr]=v1[i]; + + } + + for(int i=1; i<=nr; i++) + + for(int j=i+1; j<=nr && sqr(v1[i].y-v1[j].y)<=sol; j++) sol=min(sol,dist(v1[i],v1[j])); + + return sol; + +} + + + +int main() + +{ + + freopen("cmap.in", "r",stdin); + + freopen("cmap.out", "w", stdout); + + int n; + + scanf("%d",&n); + + for(int i=1; i<=n; i++) scanf("%d%d",&v[i].x,&v[i].y); + + sort(v+1,v+1+n,cmp1); + + printf("%.9f",sqrt(solve(1,n))); + + return 0; + +} diff --git a/cmap/divide_conquer/80a70ebc-537f-42bb-a834-71f555a11743.cpp b/cmap/divide_conquer/80a70ebc-537f-42bb-a834-71f555a11743.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3a732f40a8fe7bffda5dab9a3755e8efe70b8b7 --- /dev/null +++ b/cmap/divide_conquer/80a70ebc-537f-42bb-a834-71f555a11743.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int MAX_N = 100005; + +const long long INF = 4e18; + + + +vector < pair > Temp(MAX_N), X, Y; + + + +long long dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long divide(int left, int right, vector < pair >& X, vector < pair >& Y) { + + if (left >= right - 1) + + return INF; + + else if (right - left == 2) { + + if (Y[left] > Y[left + 1]) + + swap(Y[left], Y[left + 1]); + + return dist(X[left], X[left + 1]); + + } + + int mid = (left + right) / 2; + + long long best = min(divide(left, mid, X, Y), divide(mid, right, X, Y)); + + + + merge(Y.begin() + left, Y.begin() + mid, Y.begin() + mid, Y.begin() + right, Temp.begin()); + + copy(Temp.begin(), Temp.begin() + (right - left), Y.begin() + left); + + + + int v_size = 0; + + for (int i = left; i < right; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + Temp[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(Temp[i], Temp[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream f("cmap.in"); + + assert(f >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); i++) { + + assert(f>> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + f.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream g("cmap.out"); + + g << fixed << setprecision(6) << sqrt(divide(0, (int) X.size(), X, Y)) << "\n"; + + g.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/81905f70-e182-49f3-8483-d7202550630e.cpp b/cmap/divide_conquer/81905f70-e182-49f3-8483-d7202550630e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f1c7aae9788f44911e2e1d4ca30eddd538e78c4 --- /dev/null +++ b/cmap/divide_conquer/81905f70-e182-49f3-8483-d7202550630e.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +struct punct + +{ + + int x,y; + +}; + +punct v[100002],w[100002]; + + + +inline bool cmp(const punct &a,const punct &b) + +{ + + if(a.x==b.x) + + return a.y=dr) + + return 1000000000000000000; + + int i,j,mij=(st+dr)/2,k=0,x; + + long long d; + + x=v[mij].x; + + d=min(divide_et_impera(st,mij),divide_et_impera(mij+1,dr)); + + interclasare(st,dr); + + for(i=st;i<=dr;i++) + + if((long long)(v[i].x-x)*(v[i].x-x)>n; + + for(i=1;i<=n;i++) + + f>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,cmp); + + sol=divide_et_impera(1,n); + + g< +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef pair pii; + +pii a[100005]; + +pii r[100005]; + +pii v[100005]; + +int n,ps,pd; + +long long dist(pii A,pii B) + +{ + + return (long long)(A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second); + +} + +long long re(int st,int dr) + +{ + + int m=0; + + if(st==dr) + + return 1000000000000000000000LL; + + int mij=(st+dr)/2; + + long long rez=min(re(st,mij),re(mij+1,dr)); + + ps=st; + + pd=mij+1; + + for(int i=st;i<=dr;i++) + + if((a[ps].seconddr)&&ps<=mij) + + { + + v[i]=a[ps]; + + ps++; + + } + + else + + { + + v[i]=a[pd]; + + pd++; + + } + + for(int i=st;i<=dr;i++) + + a[i]=v[i]; + + for(int i=st;i<=dr;i++) + + if(abs(a[i].first-a[mij].first)<=rez) + + { + + m++; + + r[m]=a[i]; + + } + + for(int i=1;i<=m;i++) + + for(int j=max(1,i-7);j>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + t=re(1,n); + + fout< +#include +#include +#include +#include +#include +#include + + +using namespace std; + +typedef long long ll; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +int n; +pair V[100010]; + +ll dist(pair a, pair b){ + return (ll)(1ll * (a.first - b.first) * (a.first - b.first) + 1ll * (a.second - b.second) * (a.second - b.second)); +} + +bool cmp(pair a, pair b){ + return (a.second < b.second); +} + +ll solve(int l, int r){ + if (r - l == 1) + return dist(V[l], V[l + 1]); + else if (r - l == 2) + return min(dist(V[l], V[l + 1]), min(dist(V[l], V[l + 2]), dist(V[l + 1], V[l + 2]))); + + int mid = ((l + r) >> 1); + ll res = min(solve(l, mid), solve(mid + 1, r)); + vector > W; + int Dist = (int)(sqrt(res) + 1); + + for (int i = l; i <= r; ++i) + if (abs(V[mid].first - V[i].first) <= Dist) + W.push_back(V[i]); + + sort(W.begin(), W.end(), cmp); + + for (int i = 0; i < W.size(); ++i) + for (int j = i + 1; j < i + 8 && j < W.size(); ++j) + res = min(res, dist(W[i], W[j])); + + return res; +} + +int main(){ + f >> n; + for (int i = 1; i <= n; ++i){ + f >> V[i].first >> V[i].second; + } + + sort(V + 1, V + n + 1); + + g << fixed << setprecision(6) << double(sqrt(solve(1, n))); + return 0; +} diff --git a/cmap/divide_conquer/82fe056a-7edd-4f12-8521-899be69a33ef.cpp b/cmap/divide_conquer/82fe056a-7edd-4f12-8521-899be69a33ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d602c8b5aa304709212bf11892c6d6bc92c52dc --- /dev/null +++ b/cmap/divide_conquer/82fe056a-7edd-4f12-8521-899be69a33ef.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +vector> x; + +long long get_dist(pair a, pair b) +{ + return (a.first - b.first)*(a.first - b.first) + (a.second - b.second)*(a.second - b.second); +} + +long long distanta(int st, int dr) +{ + int i,j; + + if(dr - st<=3) + { + long long dist = LLONG_MAX; + + for(i=st; i<=dr-1; i++) + for(j=i+1; j<=dr; j++) + { + long long d_puncte = get_dist(x[i], x[j]); + dist = min(dist, d_puncte); + } + + return dist; + } + else + { + int middle = st + (dr - st)/2; + vector> band; + + long long ds, dd, d; + + ds = distanta(st, middle); + dd = distanta(middle, dr); + + d = min(dd, ds); + + for(i = st; i <= dr; i++) + if(x[i].first>=(x[middle].first-d) && x[i].first<=(x[middle].first + d)) + band.push_back(make_pair(x[i].second, x[i].first)); + + sort(band.begin(), band.end()); + + for(i=0; i<(int)band.size()-7; i++) + for( j=i+1; j<=i+7; j++) + { + long long dist = get_dist(band[i], band[j]); + d = min(d, dist); + } + + return d; + } +} + +int main() +{ + int n, i; + long long a, b, dmin; + + ifstream inFile; + inFile.open("cmap.in"); + if (!inFile) + { + cout << "Unable to open file"; + exit(1); + } + + inFile >> n; + + for (i = 0; i < n; i++) + { + inFile >> a >> b; + x.push_back(make_pair(a, b)); + } + inFile.close(); + + sort(x.begin(), x.end()); + + dmin = distanta(0, n-1); + + ofstream outFile; + + outFile.open("cmap.out"); + if (!outFile) + { + cout << "Unable to open file"; + exit(1); + } + + outFile< +#include +#include +#include +#include +using namespace std; + + + +ofstream xout("cmap.out"); + + + +int n,i; + + + + + +int p,q; + +struct punct{ + +long long x,y; + +}a[100001],aux[100001]; + + + +bool cmp(punct a, punct b) + +{ + + return a.x +#include + +typedef double f64; +const int MAX_N = 100000; +struct Punct { + f64 x, y; + Punct() { x = y = 0; } + Punct(int _x, int _y):x(_x), y(_y) {} +} v[MAX_N], aux[MAX_N]; + +f64 dist(Punct a, Punct b) { + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); +} + + +void interclasare(int st, int mid, int dr) { + int i = st, n = mid, j = mid, m = dr; + int k = 0; + while(i < n || j < m) { + if(i < n && j < m && v[i].x < v[j].x) + aux[k++] = v[i++]; + else if(i < n && j < m) + aux[k++] = v[j++]; + else if(i < n) + aux[k++] = v[i++]; + else + aux[k++] = v[j++]; + } + for(i = st; i < dr; ++i) + v[i] = aux[i - st]; +} + +f64 INF = 1e10; + +f64 min(f64 a, f64 b) { + if(a < b) return a; + return b; +} + +f64 cmap(int st, int dr) { + int mid = (st + dr) / 2; + f64 best; + if(dr - st == 1) + return INF; + else if(dr - st == 2) { + interclasare(st, st + 1, dr); + return dist(v[st], v[st + 1]); + } + best = min(cmap(st, mid), cmap(mid, dr)); + interclasare(st, mid, dr); + for(int i = st; i < dr; ++i) + for(int j = i + 1; j < i + 6; ++j) + best = min(best, dist(v[i], v[j])); + return best; +} + +int main() { + int n; + FILE *fin = fopen("cmap.in", "r"); + fscanf(fin, "%d", &n); + for(int i = 0; i < n; ++i) + fscanf(fin, "%lf%lf", &v[i].x, &v[i].y); + fclose(fin); + FILE *fout = fopen("cmap.out", "w"); + fprintf(fout, "%f", cmap(0, n)); + fclose(fout); + return 0; +} diff --git a/cmap/divide_conquer/84a30f84-5f48-4d12-96e0-f497ab3222a1.cpp b/cmap/divide_conquer/84a30f84-5f48-4d12-96e0-f497ab3222a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7651320c7c0a2c6d31875088668ecd9e256fcdb9 --- /dev/null +++ b/cmap/divide_conquer/84a30f84-5f48-4d12-96e0-f497ab3222a1.cpp @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int NMAX = 100000; + + + +struct pct + +{ + + long long x, y; + +}; + + + +int N; + +pct v[NMAX + 5], aux[NMAX + 5]; + +long long DMIN = LLONG_MAX; + + + +inline bool cmp(pct A, pct B) + +{ + + if(A.x == B.x) + + return A.y < B.y; + + + + return A.x < B.x; + +} + + + +long long GetDist(pct A, pct B) + +{ + + return 1LL * (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); + +} + + + +void Merge(int l1, int r1, int l2, int r2) + +{ + + int k = 0; + + + + while(l1 <= r1 && l2 <= r2) + + { + + k++; + + + + if(v[l1].y < v[l2].y) + + { + + aux[k] = v[l1]; + + l1++; + + } + + else + + { + + aux[k] = v[l2]; + + l2++; + + } + + } + + + + while(l1 <= r1) + + { + + aux[++k] = v[l1]; + + l1++; + + } + + + + while(l2 <= r2) + + { + + aux[++k] = v[l2]; + + l2++; + + } + + + + for(int i = r2; k > 0; k--, i--) + + v[i] = aux[k]; + +} + + + +void DivideAndConquer(int l, int r) + +{ + + if(l >= r) + + return ; + + + + int mid = (l + r) >> 1; + + int d = v[mid].x; + + + + DivideAndConquer(l, mid); + + DivideAndConquer(mid + 1, r); + + + + Merge(l, mid, mid + 1, r); + + + + int k = 0; + + + + for(int i = l; i <= r; i++) + + if(1LL * (v[i].x - d) * (v[i].x - d) < DMIN) + + aux[++k] = v[i]; + + + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= i + 8 && j <= k; j++) + + DMIN = min(DMIN, GetDist(aux[i], aux[j])); + +} + + + +int main() + +{ + + fin >> N; + + + + for(int i = 1; i <= N; i++) + + fin >> v[i].x >> v[i].y; + + + + sort(v + 1, v + N + 1, cmp); + + + + DivideAndConquer(1, N); + + + + fout << fixed << setprecision(12) << sqrtl(DMIN) << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/84d72585-2fbc-451f-a02f-5aa1639d25fc.cpp b/cmap/divide_conquer/84d72585-2fbc-451f-a02f-5aa1639d25fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63ef32c50790f6ed90873b2939b2cea8c1f8ea1c --- /dev/null +++ b/cmap/divide_conquer/84d72585-2fbc-451f-a02f-5aa1639d25fc.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct pct { + + int x, y; + + pct(int a = 0, int b = 0): x(a), y(b) {}; + + + + bool operator<(const pct &X) { + + if (x == X.x) + + return y < X.y; + + return x < X.x; + + } + +}; + + + +double dist(pct &A, pct &B) { + + return sqrt(1ll * (A.x - B.x) * (A.x - B.x) + 1ll *(A.y - B.y) * (A.y - B.y)); + +} + + + +double divide(vector &p, vector &py, int l, int r) { + + if (l >= r) { + + return dist(p[0], p[1]); + + } + + if (r - l == 1) { + + if (py[r] < py[l]) { + + swap(py[l], py[r]); + + } + + return dist(p[r], p[l]); + + } + + int m = l + (r - l) / 2; + + double d1, d2; + + d1 = divide(p, py, l, m); + + d2 = divide(p, py, m + 1, r); + + double d = min(d1, d2); + + + + vector P(r - l + 1); + + merge(py.begin() + l, py.begin() + m + 1, py.begin() + m + 1, py.begin() + r + 1, P.begin()); + + copy(P.begin(), P.begin() + (r - l + 1), py.begin() + l); + + + + int in_band = 0; + + for (int i = l; i <= r; i++) { + + if (abs(py[i].y - p[m].x) < d1) + + P[in_band++] = py[i]; + + } + + + + for (int i = 0; i < in_band; i++) { + + for (int j = i + 1; j < in_band && j - i <= 8; j++) { + + d = min(d, dist(P[i], P[j])); + + } + + } + + + + return d; + +} + + + +int main() { + + int n; + + in >> n; + + vector p(n), py(n); + + + + for (int i = 0; i < n; i++) { + + in >> p[i].x >> p[i].y; + + } + + + + sort(p.begin(), p.end()); + + for (int i = 0; i < n; i++) { + + py[i].x = p[i].y; + + py[i].y = p[i].x; + + } + + + + out << fixed << setprecision(6) << divide(p, py, 0, n - 1); + + return 0; + +} diff --git a/cmap/divide_conquer/8534877d-6e9a-4b28-97a2-80751de67e87.cpp b/cmap/divide_conquer/8534877d-6e9a-4b28-97a2-80751de67e87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01945b4d26be1ef8f744e8c2d068282c63dd6dcf --- /dev/null +++ b/cmap/divide_conquer/8534877d-6e9a-4b28-97a2-80751de67e87.cpp @@ -0,0 +1,241 @@ +#include +#include #include +#include +#include +using namespace std; + + + +const int EPS = 0.00000001; + + + +struct Point { + + int x, y; + +}; + + + +int n; + +vector points; + + + +inline int double_cmp(double& a, double& b) { + + if(a - b < -EPS) { + + return -1; + + } + + else if(a - b < EPS) { + + return 0; + + } + + else { + + return 1; + + } + +} + + + +inline bool cmp_byXY(Point a, Point b) { + + return (a.x < b.x) || (a.x == b.x && a.y < b.y); + +} + + + +inline bool cmp_byY(Point a, Point b) { + + return a.y < b.y; + +} + + + +inline double getDistance(Point& a, Point& b) { + + double dx = a.x - b.x; + + double dy = a.y - b.y; + + double d = sqrt(dx * dx + dy * dy); + + + + return d; + +} + + + +double naiveClosestPair(int left, int right, vector & points) { + + double delta = getDistance(points[left], points[left + 1]); + + for(int i = left; i < right; ++i) { + + for(int j = i + 1; j < right; ++j) { + + double tmp = getDistance(points[i], points[j]); + + if(double_cmp(delta, tmp) < 0) { + + delta = tmp; + + } + + } + + } + + + + return delta; + +} + + + +void splitY(vector & points, vector & yL, vector & yR, Point threshold) { + + for(int i = 0; i < (int) points.size(); ++i) { + + if(points[i].x < threshold.x || (points[i].x == threshold.x && points[i].y <= threshold.y)) { + + yL.push_back(points[i]); + + } + + else { + + yR.push_back(points[i]); + + } + + } + +} + + + +double closestPair(int left, int right, vector & points, vector & pointsByY) { + + if(right - left <= 3) { + + return naiveClosestPair(left, right, points); + + } + + else { + + int m = (left + right + 1) / 2; + + vector yL; + + vector yR; + + + + splitY(pointsByY, yL, yR, points[m - 1]); + + + + double dL = closestPair(left, m, points, yL); + + double dR = closestPair(m, right, points, yR); + + double d = dL; + + if(double_cmp(dR, d) < 0) { + + d = dR; + + } + + + + for(int i = 0; i < (int) pointsByY.size(); ++i) { + + for(int j = i + 1; j < min(i + 6, (int) pointsByY.size()); ++j) { + + double tmp = getDistance(pointsByY[i], pointsByY[j]); + + if(double_cmp(tmp, d) < 0) { + + d = tmp; + + } + + } + + } + + + + return d; + + } + +} + + + +int main() { + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + + + f >> n; + + for(int i = 0; i < n; ++i) { + + Point tmp; + + + + f >> tmp.x >> tmp.y; + + points.push_back(tmp); + + } + + + + vector pointsByY = points; + + + + sort(points.begin(), points.end(), cmp_byXY); + + sort(pointsByY.begin(), pointsByY.end(), cmp_byY); + + + + g << fixed << setprecision(7) << closestPair(0, n , points, pointsByY) << "\n"; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/863b4097-bed1-4fb6-8493-eebb4766b34e.cpp b/cmap/divide_conquer/863b4097-bed1-4fb6-8493-eebb4766b34e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ddf7705e8424dc064bbb9bdb7561aea76e0abb6 --- /dev/null +++ b/cmap/divide_conquer/863b4097-bed1-4fb6-8493-eebb4766b34e.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +struct Point { + int x, y; +}; + +vector points; + +inline bool cmp_byXY(Point a, Point b) { + return (a.x < b.x) || (a.x == b.x && a.y < b.y); +} + +inline bool cmp_byY(Point a, Point b) { + return a.y < b.y; +} + +inline double getDistance(Point& a, Point& b) { + double dx = a.x - b.x; + double dy = a.y - b.y; + double d = sqrt(dx * dx + dy * dy); + + return d; +} + +double bruteForce(int left, int right, vector & points) { + double delta = getDistance(points[left], points[left + 1]); + for(int i = left; i < right; ++i) { + for(int j = i + 1; j < right; ++j) { + double tmp = getDistance(points[i], points[j]); + if(tmp < delta) { + delta = tmp; + } + } + } + + return delta; +} + +void splitY(vector & points, vector & yL, vector & yR, Point threshold) { + for(int i = 0; i < (int) points.size(); ++i) { + if(points[i].x < threshold.x || (points[i].x == threshold.x && points[i].y <= threshold.y)) { + yL.push_back(points[i]); + }else { + yR.push_back(points[i]); + } + } +} + +double closestPair(int left, int right, vector & points, vector & pointsByY) { + if(right - left <= 3) { + return bruteForce(left, right, points); + } + else { + int middle = (left + right + 1) / 2; + vector yL; + vector yR; + + splitY(pointsByY, yL, yR, points[middle - 1]); + + double dL = closestPair(left, middle, points, yL); + double dR = closestPair(middle, right, points, yR); + double d = dL; + if(dR < d) { + d = dR; + } + + for(int i = 0; i < (int) pointsByY.size(); i++) { + for(int j = i + 1; j < min(i + 6, (int) pointsByY.size()); j++) { + double tmp = getDistance(pointsByY[i], pointsByY[j]); + if(tmp < d) { + d = tmp; + } + } + } + + return d; + } +} + +int main() { + int n; + fin >> n; + for(int i = 0; i < n; ++i) { + Point tmp; + fin >> tmp.x >> tmp.y; + + points.push_back(tmp); + + } + + vector pointsByY = points; + + sort(points.begin(), points.end(), cmp_byXY); + sort(pointsByY.begin(), pointsByY.end(), cmp_byY); + + fout << fixed << setprecision(7) << closestPair(0, n , points, pointsByY) << "\n"; + + return 0; +} diff --git a/cmap/divide_conquer/86593cd3-f898-4c76-ad2f-6b4adb734b14.cpp b/cmap/divide_conquer/86593cd3-f898-4c76-ad2f-6b4adb734b14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a346f17276a5b5a2edca553c4abcf8f0dfaae9ec --- /dev/null +++ b/cmap/divide_conquer/86593cd3-f898-4c76-ad2f-6b4adb734b14.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +long double dm,a,b,c,l; + +long long x[100001],y[100001],v[100001],w[100001]; + +int I[100001],i,k,j,ls,ld,m,n,u; + +long double D(long long a,long long b,long long c,long long d) + +{ + + return sqrt( (a-c)*(a-c) + (b-d)*(b-d) ); + +} + +bool compare(int i,int j) + +{ + + if(x[i]==x[j]) + + return(y[i]>n; + + for(i=0;i>x[i+1]>>y[i+1]; + + } + + f.close(); + + sort(I+1,I+1+n,compare); + + dm=D(x[I[1]],y[I[1]],x[I[2]],y[I[2]]); + + sol(1,n,dm); + + ofstream g("cmap.out"); + + u=6;l=1; + + while(dm>l) + + { + + ++u;l=l*10; + + } + + g< +#include +#include +#include +using namespace std; +vector> v,x,y; + +int n,i,q=-1; + + + +char u[20000000]; + +inline long long A() + +{ + + long long s=0,b=1; + + q++; + + if(u[q]=='-') + + b=-1,q++; + + for(;u[q]>='0'&&u[q]<='9';q++) + + s=s*10+u[q]-'0'; + + return s*b; + +} + +inline long long G(int s,int d) + +{ + + if(d-s<2) + + return 4e18; + + if(d-s==2) + + { + + if(y[s]>y[s+1]) + + swap(y[s],y[s+1]); + + return ((x[s].first-x[s+1].first)*(x[s].first-x[s+1].first)+(x[s].second-x[s+1].second)*(x[s].second-x[s+1].second)); + + } + + int m=(s+d)/2,e=0,i,j; + + long long b=min(G(s,m),G(m,d)); + + for(sort(y.begin()+s,y.begin()+d),i=s;i + +using namespace std; +ifstream f("cmap.in"); +ofstream g("cmap.out"); +struct punct +{ + double x,y; +}; +int n; +punct a[1000005],aux[1000005]; +bool cmp(punct p,punct q) +{ + return p.xa[j].y) + swap(a[i],a[j]); + } + } + return minn; + } + int k=0; + int mid=(st+dr)/2; + int DR=a[mid].x; + double minn=divide(st,mid); + minn=min(minn,divide(mid+1,dr)); + int I=st; + int J=mid+1; + int nr=0; + while(I<=mid && J<=dr) + { + if(a[I].y>n; + for(int i=1;i<=n;i++) + f>>a[i].x>>a[i].y; + sort(a+1,a+n+1,cmp); + g< +#include +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); +const long long inf = 1e18 + 5; + +const int NMax = 1e5 + 5; + + + +int N; + + + +struct point { + + int x,y; + +}v[NMax],aux[NMax]; + + + +bool cmp(point,point); + +long long solve(int,int); + +long long dist(point,point); + + + +int main() { + + in>>N; + + for (int i=1;i <= N;++i) { + + in>>v[i].x>>v[i].y; + + } + + + + sort(v+1,v+N+1,cmp); + + + + + + + + long long mn = solve(1,N); + + out<= dr) { + + return inf; + + } + + if (st == dr-1) { + + if (v[st].y > v[dr].y) { + + swap(v[st],v[dr]); + + } + + + + return dist(v[st],v[dr]); + + } + + + + long long mij = (st+dr)>>1, + + abscisa = v[mij].x, + + mn = min(solve(st,mij),solve(mij+1,dr)); + + + + int nrAux = 0, i = st, j = mij+1; + + while (i <= mij && j <= dr) { + + if (v[i].y < v[j].y) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + for (i=st;i <= dr;++i) { + + v[i] = aux[i-st+1]; + + } + + + + nrAux = 0; + + long long root = sqrt(mn); + + for (i = st;i <= dr;++i) { + + if (abs(v[i].x - abscisa) <= root) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (i=1;i <= nrAux;++i) { + + for (int j=i+1;j <= nrAux && j <= i+7;++j) { + + mn = min(mn,dist(aux[i],aux[j])); + + } + + } + + + + return mn; + +} + + + +long long dist(point a,point b) { + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +bool cmp(point a,point b) { + + return a.x < b.x; + +} diff --git a/cmap/divide_conquer/88c4dff4-fa10-45eb-9e01-b1ed38e1bbc1.cpp b/cmap/divide_conquer/88c4dff4-fa10-45eb-9e01-b1ed38e1bbc1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f8c1fc5e6b290235b414c566e3030222562739f --- /dev/null +++ b/cmap/divide_conquer/88c4dff4-fa10-45eb-9e01-b1ed38e1bbc1.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +long long dist(int a,int b,int c,int d) + +{ + + return (1LL*(c-a))*(1LL*(c-a))+(1LL*(d-b))*(1LL*(d-b)); + +} + +int n,a,b; + +vector > v,x,y; + +inline long long DivImp(int st,int dr,vector > &x,vector > &y) + +{ + + if(st>=dr-1) + + { + + return LLONG_MAX; + + } + + else + + if(dr-st==2) + + { + + if(y[st]>y[st+1]) swap(y[st],y[st+1]); + + return dist(x[st].first,x[st].second,x[st+1].first,x[st+1].second); + + } + + int mid=(st+dr)>>1; + + long long best=LLONG_MAX; + + best=min(DivImp(st,mid,x,y),DivImp(mid,dr,x,y)); + + merge(y.begin()+st,y.begin()+mid,y.begin()+mid,y.begin()+dr,v.begin()); + + copy(v.begin(),v.begin()+(dr-st),y.begin()+st); + + int sz=0; + + + + for(int i=st;i +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct point{ + + int x, y; + +}v[100100],w[100100]; + + + +int n; + + + +bool cmp_x (point a, point b){ + + return a.x < b.x; + +} + + + +bool cmp_y(point a, point b){ + + return a.y < b.y; + +} + + + +long long dist ( point a, point b){ + + long long difx = 1LL * (a.x - b.x ); + + long long dify = 1LL * ( a.y - b.y ); + + + + return difx * difx + dify * dify ; + +} + + + + + + + +long long divide ( int left, int right){ + + if( right - left == 1){ + return dist( v[left], v[right] ); + + } + + if ( right-left == 2 ){ + + return min(dist(v[left], v[left+1]),min(dist(v[left+1], v[right]),dist(v[left], v[right]))); + + + + } + + + + int m=( left + right ) / 2; + + long long d1 = divide ( left, m ) ; + + long long d2 = divide ( m+1, right ) ; + + long long d = min( d1, d2); + + int i,j,k=0; + + long long delta = ceil(sqrt(d)); + + for( i = left; i <= right; i++){ + + if( abs( v[i].x - v[m].x ) <= delta ){ + + w[k].x = v[i].x; + + w[k].y = v[i].y; + + k++; + + } + + } + + + + sort(w,w+k,cmp_y); + + + + for(i = 0;i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++){ + d = min(d,dist(w[i],w[j])); + + } + + return d; + +} + + + +int main(){ + + int n; + + f>>n; + + for (int i =0;i>v[i].x>>v[i].y; + + sort(v,v+n,cmp_x); + + g< +#include +#include +#include +#include +using namespace std; + +vector < pair < int , int > > P; + +long long CALCULATE_DISTANCE( pair < int, int> a, pair< int, int> b) { + + return (long long) (a.first-b.first) * (a.first-b.first)+ (long long) (a.second - b.second) * (a.second - b.second); + +} + +bool cmp(pair a, pairb) + +{ + + return a.second < b.second; + +} + +long long FIND_CLOSEST_PAIR(int left, int right) { + + if((right-left)==1) + + return CALCULATE_DISTANCE(P[right],P[left]); + + if((right-left)==2){ + + long long minimum=CALCULATE_DISTANCE(P[left],P[left+1]); + + if(CALCULATE_DISTANCE(P[left],P[right]) > Y; + + for(int i=left;i<=right;i++) + + if(abs(P[i].first-P[median].first)<=(int)(ceil(sqrt(closest_value)))) + + Y.push_back(std::make_pair(P[i].first,P[i].second)); + + sort(Y.begin(),Y.end(),cmp); + + for(unsigned int i=0;i>n; + + for(unsigned int i=0;i>x>>y; + + P.push_back(std::make_pair(x,y)); + + } + + std::sort( P.begin() , P.end()); + + g< +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +long long n,i,j; + +pair v[100001],r[100001],w[100001]; + + + +long long dist(pair a, pair b){ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + +long long inter(long long st, long long mid, long long dr){ + + long long i=st; + + long long j=mid+1; + + long long k=st-1; + + + + while(i<=mid && j<=dr){ + + if(v[i].second>n; + + for(i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + + + fout< +#include +#include +#include + +const int Q = 100006; + +int n; + +struct point{ + int x, y; + + void operator=(const point &alt) + { + x = alt.x; + y = alt.y; + } + +}; + +point v[Q]; + +long long dist(const point &a, const point &b) +{ + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); +} + +void swap(point &a, point &b) +{ + point aux; + aux = a; + a = b; + b = aux; +} + +int modul(int x) +{ + return x > 0 ? x : -x; +} + +bool cmp(const point &a, const point &b) +{ + return a.y < b.y; +} + + +long long divide_et_impera(int st, int dr, int min, int max) +{ + + if(dr - st <= 5 || min == max) + { + long long rez = 1e16; + for(int t1 = st; t1 < dr; t1++) + { + for(int t2 = t1 + 1; t2 <= dr; t2++) + { + long long act = dist(v[t1], v[t2]); + if(act < rez) + rez = act; + } + } + + if(st <= dr) + { + std::sort(v + st, v + dr + 1, cmp); + } + + return rez; + } + + int mid_val = (min + max) / 2; + + int lower_bound = st - 1; + + for(int i = st; i <= dr; i++) + { + if(v[i].x <= mid_val) + { + swap(v[i], v[++lower_bound]); + } + } + + long long rez1, rez2; + + rez1 = divide_et_impera(st, lower_bound, min, mid_val); + rez2 = divide_et_impera(lower_bound + 1, dr, mid_val + 1, max); + + long long rez; + + if(rez1 < rez2) + rez = rez1; + else + rez = rez2; + + int p1 = st, p2 = lower_bound + 1; + + point *aux; + + + aux = new point[dr - st + 1]; + + int cnt = 0; + + + while(p1 <= lower_bound && p2 <= dr) + { + if(v[p1].y < v[p2].y) + { + aux[cnt++] = v[p1++]; + } + else + { + aux[cnt++] = v[p2++]; + } + } + + while(p1 <= lower_bound) + { + aux[cnt++] = v[p1++]; + } + + while(p2 <= dr) + { + aux[cnt++] = v[p2++]; + } + + for(int i = st; i <= dr; i++) + { + v[i] = aux[i - st]; + } + + point *saved; + saved = new point[dr - st + 1]; + int cs = 0; + + for(int i = st; i <= dr ;i++) + { + + if(modul(v[i].x - mid_val) <= rez) + { + saved[cs++] = v[i]; + } + } + + for(int p = st; p <= dr; p++) + { + for(int oth = p + 1; oth <= p + 7; oth++) + { + if(oth > n) + break; + + long long d = dist(v[p], v[oth]); + + if(d < rez) + rez = d; + } + } + + delete aux; + delete saved; + + return rez; + +} + + +int main() +{ + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + scanf("%d\n", &n); + + for(int i = 1; i <= n ; i++) + { + scanf("%d %d\n", &v[i].x, &v[i].y); + } + + int lim = 1000000000; + + long long rez = 0; + + + rez = divide_et_impera(1, n, -lim, lim); + + double fin = sqrt(rez); + + + printf("%.8lf\n", fin); + + return 0; +} diff --git a/cmap/divide_conquer/8ba3327e-02c1-4609-a1e0-5b2145ed8ea6.cpp b/cmap/divide_conquer/8ba3327e-02c1-4609-a1e0-5b2145ed8ea6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42a5428b3771c357188266db9df013f1dc156427 --- /dev/null +++ b/cmap/divide_conquer/8ba3327e-02c1-4609-a1e0-5b2145ed8ea6.cpp @@ -0,0 +1,192 @@ +#include +#include +#include +#include + +const int Q = 100006; + +int n; + +struct point{ + int x, y; + + void operator=(const point &alt) + { + x = alt.x; + y = alt.y; + } + +}; + +point v[Q]; + +long long dist(const point &a, const point &b) +{ + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); +} + +void swap(point &a, point &b) +{ + point aux; + aux = a; + a = b; + b = aux; +} + +int modul(int x) +{ + return x > 0 ? x : -x; +} + +bool cmp(const point &a, const point &b) +{ + return a.y < b.y; +} + + +long long divide_et_impera(int st, int dr, int min, int max) +{ + + if(dr - st <= 5 || min == max) + { + long long rez = 1e16; + for(int t1 = st; t1 < dr; t1++) + { + for(int t2 = t1 + 1; t2 <= dr; t2++) + { + long long act = dist(v[t1], v[t2]); + if(act < rez) + rez = act; + } + } + + if(st <= dr) + { + std::sort(v + st, v + dr + 1, cmp); + } + + return rez; + } + + int mid_val = (min + max) / 2; + + int lower_bound = st - 1; + + for(int i = st; i <= dr; i++) + { + if(v[i].x <= mid_val) + { + swap(v[i], v[++lower_bound]); + } + } + + long long rez1, rez2; + + rez1 = divide_et_impera(st, lower_bound, min, mid_val); + rez2 = divide_et_impera(lower_bound + 1, dr, mid_val + 1, max); + + long long rez; + + if(rez1 < rez2) + rez = rez1; + else + rez = rez2; + + int p1 = st, p2 = lower_bound + 1; + + point *aux; + + + aux = new point[dr - st + 1]; + + int cnt = 0; + + + while(p1 <= lower_bound && p2 <= dr) + { + if(v[p1].y < v[p2].y) + { + aux[cnt++] = v[p1++]; + } + else + { + aux[cnt++] = v[p2++]; + } + } + + while(p1 <= lower_bound) + { + aux[cnt++] = v[p1++]; + } + + while(p2 <= dr) + { + aux[cnt++] = v[p2++]; + } + + for(int i = st; i <= dr; i++) + { + v[i] = aux[i - st]; + } + + point *saved; + saved = new point[dr - st + 1]; + int cs = 0; + + for(int i = st; i <= dr ;i++) + { + + if(modul(v[i].x - mid_val) <= rez) + { + saved[cs++] = v[i]; + } + } + + for(int p = st; p <= dr; p++) + { + for(int oth = p + 1; oth <= p + 7; oth++) + { + if(oth > n) + break; + + long long d = dist(v[p], v[oth]); + + if(d < rez) + rez = d; + } + } + + delete aux; + delete saved; + + return rez; + +} + + +int main() +{ + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + scanf("%d\n", &n); + + for(int i = 1; i <= n ; i++) + { + scanf("%d %d\n", &v[i].x, &v[i].y); + } + + int lim = 1000000000; + + long long rez = 0; + + + rez = divide_et_impera(1, n, -lim, lim); + + double fin = sqrt(rez); + + + printf("%.8lf\n", fin); + + return 0; +} diff --git a/cmap/divide_conquer/8c1ba8a6-419d-48c4-b7d1-8dc3cd5003a0.cpp b/cmap/divide_conquer/8c1ba8a6-419d-48c4-b7d1-8dc3cd5003a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ca10ac30a620e7c127445df90e37318c686b22 --- /dev/null +++ b/cmap/divide_conquer/8c1ba8a6-419d-48c4-b7d1-8dc3cd5003a0.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct Point { + + long long x, y; + + long long distance(Point &b) + + { + + return (x - b.x) * (x - b.x) + (y - b.y)*(y - b.y); + + } + +}; + + + +bool sort_x(Point& a,Point &b) + +{ + + if (a.x < b.x) + + return true; + + else + + return false; + +} + + + + + +bool sort_y(Point& a, Point &b) + +{ + + if (a.y < b.y) + + return true; + + else + + return false; + +} + + + +long long get_min_dist(long long d_min, vector& a) + +{ + + for (unsigned int i = 0; i < a.size() - 1; i++) + + { for (unsigned int j = i + 1; j < a.size() and j < i + 8; j++) + + if (a[i].distance(a[j]) < d_min) + + d_min = a[i].distance(a[j]); + + } + + return d_min; + +} + + + +long long find_closest_points(vector& a, vector& b, int st, int dr){ + + if (dr - st <= 3){ + + long long d_min = a[st].distance(a[st + 1]); + + for (int i = st; i < dr; i++) + + for (int j = i + 1; j <= dr; j++) + + if (a[i].distance(a[j]) < d_min) + + d_min = a[i].distance(a[j]); + + return d_min; + + } + + else{ + + int mijloc = (st + dr) / 2; + + vector ord_st, ord_dr; + + for (unsigned int i = 0; i < b.size(); i++) + + if (b[i].x <= a[mijloc].x) + + ord_st.push_back(b[i]); + + else + + ord_dr.push_back(b[i]); + + long long dist_st = find_closest_points(a, ord_st, st, mijloc); + + long long dist_dr = find_closest_points(a, ord_dr, mijloc + 1, dr); + + long long d_min; + + if (dist_st < dist_dr) + + d_min = dist_st; + + else + + d_min = dist_dr; + + vector fasie; + + for (unsigned int i = 0; i < b.size(); i++){ + + if (b[i].x > a[mijloc].x - sqrt(d_min) and b[i].x < a[mijloc].x + sqrt(d_min)) + + fasie.push_back(b[i]); + + } + + return get_min_dist(d_min, fasie); + + } + + + +} + + + +int main() + +{ + + ifstream in("cmap.in"); + + ofstream out("cmap.out"); + + int n; + + in >> n; + + vector X(n), Y(n); + + for (int i = 0; i < n; i++) + + { + + in >> X[i].x >> X[i].y; + + Y[i].x = X[i].x; + + Y[i].y = X[i].y; + + } + + sort(X.begin(), X.end(), sort_x); + + sort(Y.begin(), Y.end(), sort_y); + + out << fixed << setprecision(6) << sqrt(find_closest_points(X, Y, 0, n - 1)); + +} diff --git a/cmap/divide_conquer/8c43f5de-7946-408d-b540-94c1feaa3b5f.cpp b/cmap/divide_conquer/8c43f5de-7946-408d-b540-94c1feaa3b5f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..511d818826f16d36d95f02989c10204d1561eb3f --- /dev/null +++ b/cmap/divide_conquer/8c43f5de-7946-408d-b540-94c1feaa3b5f.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int NMax = 1e5 + 5; + +typedef long long ll; + +const ll inf = 3e18; + + + +int N; + +struct elem { + + int x,y; + +}v[NMax],aux[NMax]; + +bool operator <(elem a,elem b) { + + if (a.y == b.y) { + + return a.x < b.x; + + } + + return a.y < b.y; + +} + + + +ll dist(elem,elem); + +ll solve(int,int); + +bool cmp(elem,elem); + + + +int main() + +{ + + in>>N; + + for (int i=1;i<=N;++i) { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+N+1,cmp); + + + + out<>1, + + abscisa = v[mij].x; + + ll mn = min(solve(st,mij),solve(mij+1,dr)); + + + + int nrAux = 0,i = st, j = mij+1; + + while (i <= mij && j <= dr) { + + if (v[i] < v[j]) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + + + for (i=1;i<=nrAux;++i) { + + v[st+i-1] = aux[i]; + + } + + + + nrAux = 0; + + double root = sqrt(mn); + + for (i=st;i<=dr;++i) { + + if (abs(v[i].x - abscisa) <= root) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (i=1;i<=nrAux;++i) { + + for (int j=i+1;j<=nrAux && j-i+1 <= 8;++j) { + + mn = min(mn,dist(aux[i],aux[j])); + + } + + } + + + + return mn; + +} + + + +ll dist(elem a,elem b) { + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +bool cmp(elem a,elem b) { + + return a.x < b.x; + +} diff --git a/cmap/divide_conquer/8c5ff063-67e1-4cd3-b4c0-524cf8a5e226.cpp b/cmap/divide_conquer/8c5ff063-67e1-4cd3-b4c0-524cf8a5e226.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1fbc333a101e8f1bbf12ac6abcb78fceb7355bd --- /dev/null +++ b/cmap/divide_conquer/8c5ff063-67e1-4cd3-b4c0-524cf8a5e226.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long int LLI; +typedef pair PUNCT; + +const int NR_PCT = 7; +int DIM; + +PUNCT* citire(){ + + ifstream fin("cmap.in"); + + if(!fin){ + cerr << "Eroare fisier"; + exit(EXIT_FAILURE); + } + + fin >> DIM; + + PUNCT *vectorX = new PUNCT[DIM + 5]; + + if(!vectorX){ + cerr << "Eroare alocare"; + exit(EXIT_FAILURE); + } + + for(int i = 0; i < DIM; i++) + fin >> vectorX[i].first >> vectorX[i].second; + + fin.close(); + + return vectorX; + +} + +LLI calculDistanta(PUNCT &A, PUNCT &B){ + + return (LLI)(A.first - B.first) * (LLI)(A.first - B.first) + (LLI)(A.second - B.second) * (LLI)(A.second - B.second); + +} + +bool cmpY(PUNCT A, PUNCT B){ + return A.second < B.second; +} + +vector merge(PUNCT *vectorX, int st, int dr){ + + int mij = (st + dr) / 2; + int i = st; + int j = mij + 1; + vector tmpY; + + while(i <= mij && j <= dr){ + + if(vectorX[i].second < vectorX[j].second ) + tmpY.push_back(vectorX[i++]); + else + tmpY.push_back(vectorX[j++]); + } + + while ( i <= mij) + tmpY.push_back(vectorX[i++]); + + while ( j <= dr ) + tmpY.push_back(vectorX[j++]); + + int cnt = 0; + + for(int i = st; i <= dr; i++) + vectorX[i] = tmpY[cnt++]; + + return tmpY; + +} + +LLI dMinPuncte(PUNCT *vectorX,int st, int dr){ + + if( dr - st <= 3){ + + if(st == dr) + return LLONG_MAX; + + if(dr - st == 2){ + LLI dTmp = calculDistanta(vectorX[st],vectorX[dr]); + merge(vectorX,st,dr); + + return dTmp; + + } + + LLI d_1 = calculDistanta(vectorX[st],vectorX[dr]); + LLI d_2 = calculDistanta(vectorX[st],vectorX[dr-1]); + LLI d_3 = calculDistanta(vectorX[st+1],vectorX[dr]); + merge(vectorX,st,dr); + + if(d_1 < d_2 && d_1 < d_3) + return d_1; + + if(d_2 < d_1 && d_2 < d_1) + return d_1; + + return d_3; + } + + unsigned int indiceMediana = ( st + dr ) / 2; + + if(dr == DIM) + dr--; + + LLI dMinSt = dMinPuncte(vectorX,st,indiceMediana); + LLI dMinDr = dMinPuncte(vectorX,indiceMediana + 1, dr); + + LLI dMin = dMinSt; + + if (dMin > dMinDr) + dMin = dMinDr; + + vector tmpY = merge(vectorX,st,dr); + + int lim = tmpY.size(); + + for(int i = 0; i < lim; i++){ + for(int j = i + 1; j <= i + NR_PCT && j < lim; j++){ + + LLI dTmp = calculDistanta(tmpY[i],tmpY[j]); + + if(dMin > dTmp) + dMin = dTmp; + + } + } + + return dMin; +} + +int main(){ + + PUNCT *vectorX = citire(); + sort(vectorX, vectorX + DIM); + + ofstream fout("cmap.out"); + + fout < +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const long long inf=9e18; + +vector > t(100000); + + + +long long dist(const pair &i,const pair &j) + +{ + + return (long long )(i.first-j.first)*(i.first-j.first)+(long long)(i.second-j.second)*(i.second-j.second); + +} + +bool CmpX(const pair &i,const pair &j) + +{ return i.first > &v,vector > &z,int st,int dr) + +{ if(st>=dr-1) + + return inf; + + if(dr-st==2) + + { + + if(z[st].first>z[st+1].first) + + swap(z[st],z[st+1]); + + return dist( v[st],v[st+1]); + + } + + int m=(st+dr)/2; + + long long d,d1,d2,delta; + + int i,j; + + d1=minDistance(v,z,m,dr); + + d2=minDistance(v,z,st,m); + + d=min(d1,d2); + + + + + + merge(z.begin() + st, z.begin() + m, z.begin() + m, z.begin() + dr, t.begin()); + + copy(t.begin(), t.begin() + (dr - st), z.begin() + st); + + int sizet=0; + + for(i=st;i<=dr;i++) + + if(abs(z[i].second-v[m].first)<=d) + + t[sizet++]=z[i]; + + + + for(i=0;i > v,z; + + fin>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),CmpX); + + + + for(i=0;i +#include +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct + +{ + + int x; + + int y; + +}; + +punct v[100001],a[100001]; + +int n; + +bool comparax(const punct &A,const punct &B) + +{ + + return A.xd1)dg=d1; + + } + + } + + return dg; + + + +} + + + +int main() + +{ + + in>>n; + + for(int i=1;i<=n;i++) + + in>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,comparax); + + out< + + +using namespace std; + + + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + + +vector > v, w, rez, aux; + +const int INF = 4e18; + + + +long long get_dist(pair a, pair b) + +{ + + return (a.first - b.first) * 1LL * (a.first - b.first) + (a.second - b.second) * 1LL * (a.second - b.second); + +} + + + +long long rezolva(int st, int dr) + +{ + + if (st == dr) + + return INF; + + else if (dr - st == 1) + + return get_dist(v[st], v[dr]); + + + + int mid = (st+dr)/2; + + + + long long sol = min(rezolva(st, mid), rezolva(mid, dr)); + + + + rez.clear(); + + rez.resize(dr + 1 - st + 1); + + aux.clear(); + + merge(w.begin() + st, w.begin() + mid, w.begin() + mid, w.begin() + dr + 1, rez.begin()); + + + + vector > ::iterator it; + + + + for (it = rez.begin(); it != rez.end(); ++it) + + { + + if (abs(it->second - v[mid].first) <= sol) + + aux.push_back(make_pair(it->second, it->first)); + + } + + + + for (int i = 0; i < aux.size(); ++i) + + for (int j = i + 1; j < aux.size() && j - i < 8; ++j) { + + sol = min(sol, get_dist(aux[i], aux[j])); + + } + + + + return sol; + +} + + + +int main() + +{ + + int x,y,n; + + + + fin>>n; + + + + for(int i = 0; i < n; i++) + + { + + fin>>x>>y; + + v.push_back(make_pair(x,y)); + + w.push_back(make_pair(y,x)); + + } + + + + sort(v.begin(),v.end()); + + w.push_back(make_pair(INF,INF)); + + sort(w.begin(),w.end()); + + + + fout< + + + + +using namespace std; + + + + + +inline long double getDist(pair a,pair b) + +{ + + long double d1=(long double)(a.first-b.first); + + long double d2=(long double)(a.second-b.second); + + long double d=d1*d1+d2*d2; + + d=sqrt((long double)d); + + return d; + +} + +vector > x,v; + + + +inline long double divimp(int st,int dr,vector > &y) + +{ + + if(st>=dr) + + { + + return (long double)(LLONG_MAX); + + } + + + + if(st==dr-1) + + { + + if(y[st]>y[dr]) swap(y[st],y[dr]); + + return getDist(x[st],x[dr]); + + } + + + + if(st==(dr-2)) + + { + + sort(y.begin()+st,y.begin()+dr+1); + + return min(getDist(x[st],x[st+2]),min(getDist(x[st],x[st+1]),getDist(x[st+1],x[st+2]))); + + } + + + + int mid=(st+dr)>>1; + + long double best; + + + + best=min(divimp(st,mid-1,y),divimp(mid,dr,y)); + + + + merge(y.begin()+st,y.begin()+mid,y.begin()+mid,y.begin()+dr+1,v.begin()); + + copy(v.begin(),v.begin()+(dr-st+1),y.begin()+st); + + + + int sz=0; + + + + for(int i=st;i<=dr;i++) + + if(fabs(y[i].second-x[mid].first)<=best) v[++sz]=y[i]; + + + + for(int i=1;i<=sz;i++) + + for(int j=i+1;j<=sz && (j-i)<=10;j++) + + best=min(best,getDist(v[i],v[j])); + + return best; + +} + +int n; + +int a,b; + +vector > y; + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + + + fin>>n; + + + + x.push_back({-INT_MAX,-INT_MAX}); + + for(int i=1;i<=n;i++) + + { + + fin>>a>>b; + + + + x.push_back(make_pair(a,b)); + + } + + + + sort(x.begin(),x.end()); + + + + for(int i=0;i<=n;i++) + + y.push_back(make_pair(x[i].second,x[i].first)); + + v.resize(n); + + + + long double sol=divimp(1,n,y); + + + + fout< +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); +ofstream out("cmap.out"); + +double dist(pair P1, pair P2) +{ + return sqrt((double) (P1.first-P2.first)*(P1.first-P2.first) + (double) (P1.second-P2.second)*(P1.second-P2.second)); +} + +void mergePoints(vector > &Points,int left,int right) +{ + int middle = (left + right) / 2; + int i,j,k; + vector > auxPoints(right-left+1); + + i=left; + j=middle+1; + k=0; + + while (i <= middle && j<= right) + if (Points[i].first < Points[j].first) + auxPoints[k++] = Points[i++]; + else auxPoints[k++] = Points[j++]; + + for (int q=i;q<=middle;q++) + auxPoints[k++] = Points[q]; + for (int q=j;q<=right;q++) + auxPoints[k++] = Points[q]; + + for (int q=left;q<=right;q++) + Points[q] = auxPoints[q-left]; +} + +double divide(int left, int right, vector > &Points, vector > &Points2) +{ + int middle = (left + right) / 2; + double minDist = 0x3f3f3f3f;; + vector > closePoints; + + if (right - left <= 2) + { + for (vector >::iterator it = Points.begin()+left; it != Points.begin()+right-1; it++) + for (vector >::iterator it2 = it+1; it2 != Points.begin()+right; it2++) + minDist = min(minDist,dist(*it,*it2)); + + sort(Points2.begin()+left,Points2.begin()+right+1); + return minDist; + } + + minDist = min(divide(left,middle,Points,Points2),divide(middle,right,Points,Points2)); + mergePoints(Points2,left,right); + + closePoints.clear(); + for (vector >::iterator it = Points2.begin()+left; it != Points2.begin()+right+1; it++) + if (abs((*it).second - Points[middle].first) < minDist) + closePoints.push_back(*it); + + for (vector >::iterator it = closePoints.begin();it != closePoints.end();it++) + for (vector >::iterator it2 = it+1; it2 != closePoints.end() && it2-it != 8;it2++) + minDist = min(minDist,dist(*it,*it2)); + + return minDist; +} + +int main() +{ + int N; + vector > Points,Points2; + + in>>N; + for (int i=0;i>x>>y; + Points.push_back(make_pair(x,y)); + } + sort(Points.begin(),Points.end()); + + for (vector >::iterator it = Points.begin();it != Points.end();it++) + Points2.push_back(make_pair((*it).second,(*it).first)); + + out< +#include +#include +#include +#include + + + + + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +int n; +pair sir[100000 +5],aux[100000 +5]; + +long long dist2(int i,int j){ + return (long long)(1LL*(sir[i].first-sir[j].first)*(sir[i].first-sir[j].first)+1LL*(sir[i].second-sir[j].second)*(sir[i].second-sir[j].second)); +} + +long long minim(long long a,long long b){ + if(a>n; + for(int i=1;i<=n;i++){ + f>>sir[i].first>>sir[i].second; + } + sort(sir+1,sir+n+1); + g< + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + + + +struct point {int x,y;} v[100010], v1[100010]; + + + +int cmp1 (point a, point b) + +{ + + return a.x < b.x; + +} + + + +int cmp2 (point a, point b) + +{ + + return a.y < b.y; + +} + + + +long long sqr (int x) + +{ + + return 1LL*x*x; + +} + + + +long long dist (point a, point b) + +{ + + return 1LL*(a.x-b.x)*(a.x-b.x) + 1LL*(a.y-b.y)*(a.y-b.y); + +} + + + +long long solve (int st, int dr) + +{ + + if (st>=dr) return 1e18;; + + if (st+1==dr) + + { + + if (v[dr].y < v[st].y) swap (v[st],v[dr]); + + return dist (v[st], v[dr]); + + } + + + + int mid=(st+dr)/2, midx=v[mid].x; + + long long sol = min (solve (st,mid), solve (mid+1,dr)); + + merge (v+st, v+mid+1, v+mid+1, v+dr+1, v1+st, cmp2); + + int nr=0; + + for (int i=st; i<=dr; i++) + + { + + v[i]=v1[i]; + + if (1LL*(v1[i].x-midx)*(v1[i].x-midx) <= sol) v1[++nr]=v1[i]; + + } + + for (int i=1; i<=nr; i++) + + for (int j=i+1; j<=nr && 1LL*(v1[i].y-v1[j].y)*(v1[i].y-v1[j].y)<=sol; j++) + + sol = min (sol, dist(v1[i],v1[j])); + + return sol; + +} + + + +int main() + +{ + + int n; + + fin>>n; + + for (int i=1; i<=n; i++) fin>>v[i].x>>v[i].y; + + sort (v+1, v+1+n, cmp1); + + fout< + + + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +typedef long long i64; + + + +int N; + +vector>X; + +vector>Y; + +vector> aux; + + + +i64 Distance(pair p1, pair p2) + +{ + + return (p2.first - p1.first) * (p2.first - p1.first) + (p2.second - p1.second) * (p2.second - p1.second); + +} + + + +i64 solve(int left, int right, vector> &X, vector> &Y) + +{ + + if (right - left <= 3) + + { + + i64 answ = 4e18; + + for (int i = left; i < right; ++i) + + for (int j = i + 1; j <= right; ++j) + + { + + if (Y[i] > Y[j]) + + swap(Y[i], Y[j]); + + answ = min(answ, Distance(X[i], X[j])); + + } + + return answ; + + } + + + + int mid = (left + right) >> 1; + + i64 best = min(solve(left, mid, X, Y), solve(mid + 1, right, X, Y)); + + + + merge(Y.begin() + left , Y.begin() + mid + 1, Y.begin() + mid + 1, Y.begin() + right + 1, aux.begin()); + + copy(aux.begin(), aux.begin() + (right - left + 1), Y.begin() + left); + + + + int _size = 0; + + for (int i = left; i <= right; ++i) + + if (abs(X[mid].first - Y[i].second) <= best) + + aux[_size++ ] = Y[i]; + + + + for (int i = 0; i < _size - 1; ++i) + + for (int j = i + 1; j < _size && (aux[j].first - aux[i].first) < sqrt(best) ; ++j) + + best = min(best, Distance(aux[i], aux[j])); + + return best; + +} + + + +int main() + +{ + + fin >> N; + + X.resize(N + 1); + + Y.resize(N + 1); + + aux.resize(N + 1); + + for (int i = 1; i <= N; ++i) + + fin >> X[i].first >> X[i].second; + + + + sort(X.begin(), X.end()); + + for (int i = 1;i <= N; ++i) + + Y[i] = {X[i].second, X[i].first}; + + + + fout << fixed << setprecision(6) << sqrt(solve(1, N, X, Y)) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/91e78a63-37dd-4755-9b4f-851571aec0f6.cpp b/cmap/divide_conquer/91e78a63-37dd-4755-9b4f-851571aec0f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5dad4b9c4dd52780a19cc1e9d34d2c8f94273b1e --- /dev/null +++ b/cmap/divide_conquer/91e78a63-37dd-4755-9b4f-851571aec0f6.cpp @@ -0,0 +1,364 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +struct Point { + + + + + + + + long long x, y; + + + + long long dist_sq(Point& b) + + + + { + + + + return (x - b.x) * (x - b.x) + (y - b.y) * (y - b.y); + + + + } + + + +}; + + + + + + + +bool sort_x(Point& a, Point& b) + + + +{ + + + + if (a.x < b.x) + + + + return true; + + + + else + + + + return false; + + + +} +bool sort_y(Point& a, Point& b) + + + +{ + + + + if (a.y < b.y) + + + + return true; + + + + else + + + + return false; + + + +} + + + + + + + +long long get_min_dist(long long d_min, vector& a) + + + +{ + + + + for (unsigned int i = 0; i < a.size() - 1; i++) + + + + { + + + + for (unsigned int j = i + 1; j < a.size() and j < i + 8; j++) + + + + if (a[i].dist_sq(a[j]) < d_min) + + + + d_min = a[i].dist_sq(a[j]); + + + + } + + + + return d_min; + + + +} + + + + + + + +long long find_closest_points(vector& a, vector& b, int st, int dr) { + + + + if (dr - st <= 3) { + + + + long long d_min = a[st].dist_sq(a[st + 1]); + + + + for (int i = st; i < dr; i++) + + + + for (int j = i + 1; j <= dr; j++) + + + + if (a[i].dist_sq(a[j]) < d_min) + + + + d_min = a[i].dist_sq(a[j]); + + + + return d_min; + + + + } + + + + else { + + + + int mijloc = (st + dr) / 2; + + + + vector ord_st, ord_dr; + + + + for (unsigned int i = 0; i < b.size(); i++) + + + + if (b[i].x <= a[mijloc].x) + + + + ord_st.push_back(b[i]); + + + + else + + + + ord_dr.push_back(b[i]); + + + + + + + + long long dist_st = find_closest_points(a, ord_st, st, mijloc); + + + + long long dist_dr = find_closest_points(a, ord_dr, mijloc + 1, dr); + + + + long long d_min; + + + + + + + + if (dist_st < dist_dr) + + + + d_min = dist_st; + + + + else + + + + d_min = dist_dr; + + + + + + + + vector fasie; + + + + for (unsigned int i = 0; i < b.size(); i++) { + + + + if (b[i].x > a[mijloc].x - sqrt(d_min) and b[i].x < a[mijloc].x + sqrt(d_min)) + + + + fasie.push_back(b[i]); + + + + } + + + + return get_min_dist(d_min, fasie); + + + + } + + + + + + + +} + + + + + + + +int main() + + + +{ + + + + ifstream in("cmap.in"); + + + + ofstream out("cmap.out"); + + + + int n; + + + + in >> n; + + + + vector X(n), Y(n); + + + + for (int i = 0; i < n; i++) + + + + { + + + + in >> X[i].x >> X[i].y; + + + + Y[i].x = X[i].x; + + + + Y[i].y = X[i].y; + + + + } + + + + sort(X.begin(), X.end(), sort_x); + + + + sort(Y.begin(), Y.end(), sort_y); + + + + out << fixed << setprecision(6) << sqrt(find_closest_points(X, Y, 0, n - 1)); + + + +} diff --git a/cmap/divide_conquer/9413b390-c53e-48c2-9de0-ae5a9e3f56dd.cpp b/cmap/divide_conquer/9413b390-c53e-48c2-9de0-ae5a9e3f56dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7afa5d88b5d5144bc77675cbf7e4edaf3dacb96c --- /dev/null +++ b/cmap/divide_conquer/9413b390-c53e-48c2-9de0-ae5a9e3f56dd.cpp @@ -0,0 +1,191 @@ +#include +using namespace std; +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int MAXN = 1e5 + 3; + +const double EPS = 1e-8; + +const long double INF = 1e12; + + + +struct punct{ + + long double x, y; + +}; + +punct v[MAXN], b[MAXN], aux[MAXN]; + + + +long double minim = INF; + + + +bool cmpx(punct a, punct b){ + + return a.x + EPS < b.x or (a.x == b.x and a.y + EPS < b.y); + +} + +bool cmpy(punct a, punct b){ + + return a.y + EPS < b.y or (a.y == b.y and a.x + EPS < b.x); + +} + + + +long double manhattan(punct a, punct b){ + + return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); + +} + + + +void interclasare(int st1, int dr1, int st2, int dr2){ + + int indice = st1 - 1, i = st1, j = st2; + + + + while(i <= dr1 and j <= dr2){ + + if(v[i].y < v[j].y) + + aux[++indice] = v[i++]; + + else if(v[i].y > v[j].y) + + aux[++indice] = v[j++]; + + else { + + if(v[i].x < v[i].y) + + aux[++indice] = v[i++]; + + else + + aux[++indice] = v[j++]; + + } + + } + + + + for(i; i <= dr1; ++i) aux[++indice] = v[i]; + + for(j; j <= dr2; ++j) aux[++indice] = v[j]; + + for(int k = st1; k <= dr2; ++k) + + v[k] = aux[k], aux[k] = {0, 0}; + +} + + + +void dei(int st ,int dr){ + + if(dr - st <= 2){ + + for(int i = st; i < dr; ++i){ + + for(int j = i + 1; j <= dr; ++j){ + + minim = min(minim, manhattan(v[i], v[j])); + + } + + } + + sort(v + st, v + dr + 1, cmpy); + + return; + + } + + + + int m = (st + dr ) / 2, l = 0; + + punct mid = v[m]; + + + + dei(st , m); dei(m + 1, dr); + + interclasare(st, m, m + 1, dr); + + + + for(int i = st; i <= dr; ++i){ + + if(abs(mid.x - v[i].x) <= minim) + + b[++l] = v[i]; + + } + + for(int i = 1; i <= l ;++i){ + + for(int j = i + 1; j <= min(i + 8, l); ++j){ + + minim = min(minim, manhattan(b[i], b[j])); + + } + + } + + + +} + + + +void afisare(int n){ + + for(int i = 1; i <= n; ++i){ + + fout << v[i].x << " " << v[i].y << '\n'; + + } + + fout << minim << '\n'; + +} + + + +int main(){ + + int n; fin >> n; + + for(int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmpx); + + + + dei(1, n); + + fout << setprecision(7) << fixed; + + fout << minim; + + return 0; + +} diff --git a/cmap/divide_conquer/9434a5f2-d618-453a-9148-47ff2c927ed4.cpp b/cmap/divide_conquer/9434a5f2-d618-453a-9148-47ff2c927ed4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8124e561eca8b4856b822d11ef621db2099f5200 --- /dev/null +++ b/cmap/divide_conquer/9434a5f2-d618-453a-9148-47ff2c927ed4.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); +int n; +vector> x, y, v; + +long long dist(pair p1, pair p2) +{ + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); +} + +long long solve(int st, int dr) +{ + if(st >= dr) + return 0x3f3f3f3f; + if(dr - st == 1) + return dist(x[st], x[st + 1]); + int mid = (st + dr) / 2; + long long best = min(solve(st, mid), solve(mid, dr)); + + sort(y.begin() + st, y.begin() + dr); + + v.clear(); + for(int i = st;i < dr;++i) + if(abs(y[i].second - x[mid].first) <= best) + v.push_back(y[i]); + for(int i = 0;i < v.size() - 1;++i) + for(int j = i + 1;j < v.size() && j - i < 8;++j) + best = min(best, dist(v[i], v[j])); + return best; +} + +void Read() +{ + f>>n; + long long a, b; + for(int i = 1;i <= n;++i) + { + f>>a>>b; + x.push_back(make_pair(a, b)); + } + sort(x.begin(), x.end()); + for(auto it : x) + y.push_back(make_pair(it.second, it.first)); + g< +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream ka("cmap.in"); +ofstream ki("cmap.out"); + + +vector> V; + +void interclasare(int, int, int); + +bool cmp(const std::pair &p1, const std::pair &p2) +{ + return (p1.second < p2.second); +} + +long long dist(const pair & a, const pair & b) +{ + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long solve(int st, int dr) +{ + if (dr - st == 1) + { + if (V[st].second > V[dr].second) + swap(V[st], V[dr]); + return dist(V[st], V[dr]); + } + else if (dr - st == 2) + { + sort(V.begin() + st, V.begin() + dr + 1, cmp); + return min(dist(V[st], V[st + 1]), min(dist(V[st], V[st + 2]), dist(V[st + 1], V[st + 2]))); + } + + int mid = (st + dr) / 2; + int mid_x = V[mid].first; + long long d = min(solve(st, mid), solve(mid + 1, dr)); + auto delta = static_cast(ceil(sqrt(d))); + interclasare(st, mid, dr); + + vector> A; + for (int i = st; i <= dr; i++) + { + if (abs((V[i].first - mid_x)) <= delta) + A.push_back(V[i]); + } + for (int i = 0; i < A.size(); i++) + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) + d = min(d, dist(A[i], A[j])); + + return d; +} + +void interclasare(int start, int m, int end) +{ + vector> Aux; + int i = start, j = m + 1; + + while ((i <= m) && (j <= end)) + if (V[i].second <= V[j].second) + Aux.push_back(V[i++]); + else + Aux.push_back(V[j++]); + + while (i <= m) + Aux.push_back(V[i++]); + + while (j <= end) + Aux.push_back(V[j++]); + + for (i = start; i <= end; i++) + V[i] = Aux[i - start]; +} + +int main() +{ + int n; + ka >> n; + + for (int i = 0; i < n; i++) + { + long long a, b; + ka >> a >> b; + V.emplace_back(make_pair(a, b)); + } + + sort(V.begin(), V.end()); + + ki << fixed << setprecision(6) << sqrt(solve(0, n - 1)) << '\n'; + return 0; +} diff --git a/cmap/divide_conquer/96aa2c3d-bd79-4295-aac0-6bde6fa0db4f.cpp b/cmap/divide_conquer/96aa2c3d-bd79-4295-aac0-6bde6fa0db4f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f856bb6687d1813dff07af9e993e68749286340 --- /dev/null +++ b/cmap/divide_conquer/96aa2c3d-bd79-4295-aac0-6bde6fa0db4f.cpp @@ -0,0 +1,273 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +const double PI = acos(-1); + +const double eps = 1e-6; + + + +inline long long lgput(long long a , long long b , long long mod) { + + long long ret = 1; + + while( b ){ + + if(b & 1) ret = (ret * a) % mod; + + a = (a * a) % mod; + + b >>= 1; + + } + + + + return (ret%mod); + +} + + + +inline long long inv(long long x , long long MOD) { + + return lgput(x, MOD - 2, MOD); + +} + + + +inline bool exist (double nr){ + + return (nr < -eps) || (nr > eps); + +} + + + +long long big_rand(){ + + return rand () * RAND_MAX + rand(); + +} +ifstream cin ("cmap.in");ofstream cout ("cmap.out"); + + + +struct nod{long long x , y;}; + +bool cmpx (nod a , nod b){return a.x < b.x;} + +bool cmpy (nod a , nod b){return a.y < b.y;} + +nod v[100100] , ord[100100]; + + + +double dist (nod a , nod b){ + + double now = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y); + + return sqrt(now); + +} + + + +double solve (int st , int dr){ + + + + double ans = 1e18; + + if (dr-st+1 <= 3){ + + for (int i=st; i<=dr; i++){ + + for (int j=i+1; j<=dr; j++){ + + ans = min(ans , dist(v[i] , v[j])); + + } + + } + + sort (v+st , v+dr+1 , cmpy); + + return ans; + + } + + + + int mij = st + dr; + + mij /= 2; + + long long middle = v[mij].x; + + double low = min(solve(st , mij) , solve(mij+1 , dr)); + + + + int ST = st , DR = mij+1 , pnt = st; + + while (ST <= mij && DR <= dr){ + + + + if (v[ST].y < v[DR].y){ + + ord[pnt++] = v[ST++]; + + } + + else{ + + ord[pnt++] = v[DR++]; + + } + + } + + for (; ST<=mij; ST++)ord[pnt++]=v[ST]; + + for (; DR<=dr; DR++)ord[pnt++]=v[DR]; + + + + for (int i=st; i<=dr; i++){ + + + + v[i] = ord[i]; + + if (i > st){ + + if (v[i].y < v[i-1].y){ + + cout<<"MUIE"<<'\n'; + + } + + } + + } + + + + pnt = 0; + + for (int i=st; i<=dr; i++){ + + if (abs(v[i].x - middle) <= (long long)low){ + + ord[++pnt]= v[i]; + + } + + } + + + + ans = low; + + for (int i=1; i<=pnt; i++){ + + for (int j=i+1; j<=pnt; j++){ + + if (ord[j].y - ord[i].y > (long long)low){ + + break; + + } + + ans = min(ans , dist(ord[i] , ord[j])); + + } + + } + + + + return ans; + +} + + + + + +int main() { + + + + + + + + ios::sync_with_stdio(false); + + cin.tie(nullptr); + + cout.tie(nullptr); + + cout << setprecision(10) << fixed; + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + srand(time(nullptr)); + + + + int n; + + cin>>n; + + + + for (int i=1; i<=n; i++){ + + int a , b; + + cin>>a>>b; + + v[i].x = a; + + v[i].y = b; + + } + + sort(v+1 , v+n+1 , cmpx); + + cout< + + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); +int n; +vector> x, y, v; + +long long dist(pair p1, pair p2) +{ + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); +} + +long long solve(int st, int dr) +{ + if(st >= dr) + return 0x3f3f3f3f; + if(dr - st == 1) + return dist(x[st], x[st + 1]); + int mid = (st + dr) / 2; + long long best = min(solve(st, mid), solve(mid, dr)); + + sort(y.begin() + st, y.begin() + dr); + + v.clear(); + for(int i = st;i < dr;++i) + if(abs(y[i].second - x[mid].first) <= best) + v.push_back(y[i]); + for(int i = 0;i < v.size() - 1;++i) + for(int j = i + 1;j < v.size() && j - i < 8;++j) + best = min(best, dist(v[i], v[j])); + return best; +} + +void Read() +{ + f>>n; + long long a, b; + for(int i = 1;i <= n;++i) + { + f>>a>>b; + x.push_back(make_pair(a, b)); + } + sort(x.begin(), x.end()); + for(auto it : x) + y.push_back(make_pair(it.second, it.first)); + g< +#include +#include +#include +#include + +using namespace std; + +const int NMAX = 1e5 + 5; +const long double INF = 1.0 * 1e10; + +ifstream cin("cmap.in"); +ofstream cout("cmap.out"); + +pair v[NMAX]; + +bool cmp(pair x, pair y) +{ + if(x.second == y.second) + return x.first < y.first; + else + return x.second < y.second; +} + +long double dist(pair x, pair y) +{ + return (long double)sqrt(1.0 * (x.first - y.first) * (x.first - y.first) + 1.0 * (x.second - y.second) * (x.second - y.second)); +} + +vector > a; + +long double solve(int st, int dr) +{ + if(dr - st + 1 <= 3) { + long double dmin = INF; + for(int i = st; i < dr; ++i) { + for(int j = i + 1; j <= dr; ++j) + dmin = min(dmin, dist(v[i], v[j])); + } + return dmin; + } + int med = (st + dr) / 2; + long double dmin = min(solve(st, med), solve(med + 1, dr)); + a.clear(); + for(int i = st; i <= dr; ++i) + if(abs(v[med].first - v[i].first) <= dmin) { + a.emplace_back(v[i]); + } + sort(a.begin(), a.end(), cmp); + for(int i = 0; i < a.size() - 1; ++i) { + for(int j = i + 1; j < a.size() && j - i <= 7; ++j) { + dmin = min(dmin, dist(a[i], a[j])); + } + } + return dmin; +} + +int main() { + int n; + cin >> n; + for(int i = 1; i <= n; ++i) { + cin >> v[i].first >> v[i].second; + } + sort(v + 1, v + n + 1); + cout << fixed << setprecision(6) << solve(1, n) << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/9b7966af-349e-400f-854b-a8f0517f6e27.cpp b/cmap/divide_conquer/9b7966af-349e-400f-854b-a8f0517f6e27.cpp new file mode 100644 index 0000000000000000000000000000000000000000..086d5746b81d3e84bfb95a44d2780bad1c8fe8ce --- /dev/null +++ b/cmap/divide_conquer/9b7966af-349e-400f-854b-a8f0517f6e27.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int Nmax = 100005; + +int N; + + + +struct Point + +{ + + int x, y; + +} P[Nmax], V[Nmax]; + + + +long long minD = 4e18; + + + +bool smallerX(const Point& p1, const Point& p2) { return (p1.x < p2.x); } + +bool smallerY(const Point& p1, const Point& p2) { return (p1.y < p2.y); } + + + +long long dist(const Point& p1, const Point& p2) + +{ + + int dx = p2.x - p1.x, + + dy = p2.y - p1.y; + + return (1LL * dx * dx) + (1LL * dy * dy); + +} + + + + + +void read() + +{ + + ifstream fin("cmap.in"); + + fin >> N; + + for(int i = 0; i < N; i++) + + fin >> P[i].x >> P[i].y; + + fin.close(); + +} + + + +void print_sol() + +{ + + ofstream fout("cmap.out"); + + fout << fixed << setprecision(6) << sqrt(minD) << '\n'; + + fout.close(); + +} + + + +void find_closest(int L, int R) + +{ + + if(R - L == 1) + + { + + if(P[L].y > P[L + 1].y) swap(P[L], P[L + 1]); + + minD = min(minD, dist(P[L], P[L+1])); + + return; + + } + + + + int M = (L + R)/2; + + int sepx = P[M].x; + + + + find_closest(L, M); + + find_closest(M, R); + + + + merge(P+L, P+M, P+M, P+R + 1, V, smallerY); + + memcpy(P+L, V, (R-L + 1) * sizeof(Point)); + + int k = 0; + + for(int i = L; i <= R; i++) + + if(abs(P[i].x - sepx) <= minD) + + V[k++] = P[i]; + + + + for(int i = 0; i < (k - 1); i++) + + for(int j = (i + 1); j < min(k, i + 8); j++) + + minD = min(minD, dist(V[i], V[j])); + + + +} + + + +int main() + +{ + + read(); + + sort(P, P+N, smallerX); + + find_closest(0, N-1); + + print_sol(); + + return 0; + +} diff --git a/cmap/divide_conquer/9bf1955e-6589-43ba-a874-e95da59de16c.cpp b/cmap/divide_conquer/9bf1955e-6589-43ba-a874-e95da59de16c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..401280b553b43f747e1bd18c7805c0c9c4b01ca9 --- /dev/null +++ b/cmap/divide_conquer/9bf1955e-6589-43ba-a874-e95da59de16c.cpp @@ -0,0 +1,73 @@ +#include +using namespace std; +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); +const int MAXN = 1e5 + 1; +const double INF = 1e12; +const double EPS = 1e-8; +double minim = INF; +struct punct{ + double x, y; +}; +bool cmp(punct a, punct b){ + return a.x + EPS < b.x or (abs(a.x - b.x) <= EPS and a.y + EPS < b.y); +} +bool cmp2(punct a, punct b){ + return a.y + EPS < b.y or (abs(a.y - b.y) <= EPS and a.x + EPS < b.x); +} +double formula(punct a, punct b){ + return sqrt((((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)))); +} +punct v[MAXN]; +punct aux[MAXN]; +punct b[MAXN]; +void interclasare(int st ,int mij ,int dr){ + int indice1 = st - 1, indice2 = mij , indice = st - 1; + while(indice1 < mij and indice2 < dr){ + if(v[indice1 + 1].y < v[indice2 + 1].y) aux[++indice] = v[++indice1]; + else aux[++indice] = v[++indice2]; + } + for(int i = indice1 + 1; i <= mij ; ++i) + aux[++indice] = v[i]; + for(int i = indice2 + 1; i <= dr; ++i) + aux[++indice] = v[i]; + for(int i = st; i <= dr; ++i) + v[i] = aux[i]; +} +void dei(int st, int dr){ + int m = (st + dr) / 2; + if(dr - st <= 2){ + for(int i = st ; i <= dr - 1; ++i){ + for(int j = i + 1; j <= dr; ++j){ + minim = min(minim, formula(v[i], v[j])); + } + } + sort(v + st, v + dr + 1, cmp2); + return; + } + double middleX = v[m].x; + dei(st, m); dei(m + 1, dr); + interclasare(st, m, dr); + int indice = 0; + for(int i = st; i <= dr; ++i) + if(abs(v[i].x - middleX) <= minim) + b[++indice] = v[i]; + + for(int i = 1; i <= indice; ++i) + for(int j = i + 1; j <= min(indice, i + 8) ; ++j) + minim = min(minim, formula(b[i], b[j])); + +} + +int main(){ + ios::sync_with_stdio(false); + fin.tie(0);fout.tie(0); + int n ; + fin >> n; + for(int i = 1; i <= n ;++i) + fin >> v[i].x >> v[i].y; + sort(v + 1, v + n + 1, cmp); + dei(1, n); + fout << setprecision(7) << fixed << minim; + return 0; +} diff --git a/cmap/divide_conquer/9bf29342-2ff3-409d-a01d-3246f89f0a97.cpp b/cmap/divide_conquer/9bf29342-2ff3-409d-a01d-3246f89f0a97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b6579556764f9c633d9b25f4c3da8f7d35507a0 --- /dev/null +++ b/cmap/divide_conquer/9bf29342-2ff3-409d-a01d-3246f89f0a97.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int NMax = 100000; + + + +const long long oo = 1e18; + + + +int N, K; + + + +pair P[NMax + 5], V[NMax + 5]; + + + +long long Dist(pair A, pair B) + +{ + + return ((A.first - B.first) * (A.first - B.first) + (A.second - B.second) * (A.second - B.second)); + +} + + + +long long DEI(long long L, long long R) + +{ + + if(L == R) + + return oo; + + + + if(L == R - 1) + + return Dist(P[L], P[R]); + + + + long long M = (L + R) / 2, A, B; + + + + A = DEI(L, M), B = DEI(M + 1, R); + + + + long long Sol = min(A, B); K = 0; + + + + for(int i = L; i <= R; i++) + + if(abs(P[i].first - P[M].first) <= Sol) + + V[++K] = {P[i].second, P[i].first}; + + + + sort(V + 1, V + K + 1); + + + + for(int i = 1; i <= K; i++) + + { + + for(int j = i + 1; j <= i + 8 && j <= K; j++) + + Sol = min(Sol, Dist(V[i], V[j])); + + } + + return Sol; + +} + + + +int main() + +{ + + fin >> N; + + + + for(int i = 1; i <= N; i++) + + fin >> P[i].first >> P[i].second; + + + + sort(P + 1, P + N + 1); + + + + fout << fixed << setprecision(6) << sqrt(DEI(1, N)) << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/9c2978d3-130c-4431-ab6e-8c95142cdd2c.cpp b/cmap/divide_conquer/9c2978d3-130c-4431-ab6e-8c95142cdd2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d10ab773d183fc968564f0127f3826d1e132d04a --- /dev/null +++ b/cmap/divide_conquer/9c2978d3-130c-4431-ab6e-8c95142cdd2c.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +struct punct{ + + int x, y; + +}; + + + +bool cmpx(punct p1, punct p2) + +{ + + return (p1.x < p2.x); + +} + + + +bool cmpy(punct p1, punct p2) + +{ + + return (p1.y < p2.y); + +} + + + +double dist(punct p1, punct p2) + +{ + + double dist = sqrt( pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2) ); + + return dist; + +} + + + +double f1(punct p[], int n) + +{ + + int i, j; + + double d = 99999999; + + + + if(n <= 3) + + { + + for(i = 0; i < n; i++) + + for(j = i+1; j < n; j++) + + if(dist(p[i], p[j]) < d) + + d = dist(p[i], p[j]); + + return d; + + } + + + + int m = (n/2); + + double rs = f1(p, m); + + double rd = f1(p + m, n - m); + + + + if(rs < rd) + + d = rs; + + else + + d = rd; + + + + punct Y[n]; + + j = 0; + + for (i = 0; i < n; i++) + + if (abs(p[i].x - p[m].x) < d) + + { + + Y[j] = p[i]; + + j++; + + } + + sort(Y, Y+j, cmpy); + + + + for(i = 0; i < j; i++) + + for(int k = i+1; k < j && ((Y[k].y - Y[i].y) < d); k++) + + if(dist(Y[i], Y[k]) < d) + + d = dist(Y[i], Y[k]); + + + + return d; + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + punct p[n]; + + for(int i = 0; i < n; i++) + + f >> p[i].x >> p[i].y; + + + + sort(p, p+n, cmpx); + + + + double r = f1(p, n); + + + + g< +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 100005; + +const long long INFINIT = 4e18; + + + +vector < pair > verticalSection(MAX_N), PointsOX, PointsOY; + + + +long long distEuclid(const pair & a, const pair & b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long retClosPoint(int left, int right) + +{ + + if (left >= right - 1) + + return INFINIT; + + else + + if (right - left == 2) + + { + + if (PointsOY[left] > PointsOY[left + 1]) + + swap(PointsOY[left], PointsOY[left + 1]); + + return distEuclid(PointsOX[left], PointsOX[left + 1]); + + } + + + + int mid = (left + right) / 2; + + long long dist = min(retClosPoint(left, mid), retClosPoint(mid, right)); + + + + merge(PointsOY.begin() + left, PointsOY.begin() + mid, PointsOY.begin() + mid, PointsOY.begin() + right, verticalSection.begin()); + + copy(verticalSection.begin(), verticalSection.begin() + (right - left), PointsOY.begin() + left); + + + + int vSize = 0; + + for (int i = left; i < right; i++ ) + + if (abs(PointsOY[i].second - PointsOX[mid].first) <= dist) + + verticalSection[vSize ++] = PointsOY[i]; + + + + for (int i = 0; i < vSize - 1; i ++) + + { + + for (int j = i + 1; j < vSize && j - i < 8; j ++) + + dist = min(dist, distEuclid(verticalSection[i], verticalSection[j])); + + } + + return dist; + +} + + + +int main() + +{ + + int nrPoints; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> nrPoints; + + + + PointsOX.resize(nrPoints), PointsOY.resize(nrPoints); + + for (int i = 0; i < PointsOX.size(); ++ i) + + fin >> PointsOX[i].first >> PointsOX[i].second; + + fin.close(); + + + + sort(PointsOX.begin(), PointsOX.end()); + + for (int i = 0; i < PointsOX.size(); ++ i) + + PointsOY[i] = make_pair(PointsOX[i].second, PointsOX[i].first); + + + + + + fout << fixed << setprecision(6) << sqrt(retClosPoint(0,PointsOX.size())) << "\n"; + + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/9d07975e-6f60-4577-a9b8-33f0af132908.cpp b/cmap/divide_conquer/9d07975e-6f60-4577-a9b8-33f0af132908.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc46edfb83101689d5477719e2834a9cae3e2dbf --- /dev/null +++ b/cmap/divide_conquer/9d07975e-6f60-4577-a9b8-33f0af132908.cpp @@ -0,0 +1,210 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +struct tip + +{ + + int x, y; + +} pct[100005],sec[100005],aux[100005],yy[100005]; + +int n; + + + +long long rasp=LLONG_MAX; + + + +long long minim(long long a, long long b) + +{ + + if(asec[j].y)swap(sec[i],sec[j]); + + } + + return; + + } + + genesa (st, m); + + genesa (m+1,dr); + + intercy(st,m,dr); + + int k=0; + + for(int i=st; i<=dr; ++i) + + if((long long)(l-sec[i].x)*(l-sec[i].x)<=rasp)yy[++k]=sec[i]; + + for(int i=1; i +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct point { + + int x, y; + +}; + + + +vector vec; + +point p, q; + +double d; + + + +bool cmpX(point& a, point& b) { + + if (a.x == b.x) return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmpY(point& a, point& b) { + + if (a.y == b.y) return a.x < b.x; + + return a.y < b.y; + +} + + + +double dist(point a, point b) { + + long long int x = (long long int)a.x - (long long int)b.x; + + long long int y = (long long int)a.y - (long long int)b.y; + + x *= x; + + y *= y; + + return sqrt(x + y); + +} + + + +double bruteForce(vector& strip, double d) { + + double dmin = d; + + for (int i = 0;i < strip.size();++i) + + for (int j = i + 1;j < strip.size() && (double)strip[j].y - (double)strip[i].y < dmin;++j) { + + double tmp = dist(strip[i], strip[j]); + + if (tmp < dmin) dmin = tmp; + + } + + return dmin; + +} + + + +void merge(int i, int j, int k, vector& vec) { + + vector res; + + int u = i, v = j + 1; + + while (u <= j && v <= k) { + + if (cmpY(vec[u], vec[v])) { + + res.push_back(vec[u]); + + ++u; + + } + + else { + + res.push_back(vec[v]); + + ++v; + + } + + } + + while (u <= j) { + + res.push_back(vec[u]); + + ++u; + + } + + while (v <= k) { + + res.push_back(vec[v]); + + ++v; + + } + + for (u = i;u <= k;++u) + + vec[u] = res[u-i]; + +} + + + +double closestPairDist(vector& vec, int i, int j,vector& sorted) { + + if (i >= j) { + + if (i == j) sorted[i] = vec[i]; + + return 1e17; + + } + + int m = (i + j) / 2; + + double d1 = closestPairDist(vec, i, m, sorted); + + double d2 = closestPairDist(vec, m + 1, j, sorted); + + double d = min(d1, d2); + + merge(i, m, j, sorted); + + vector strip; + + for (int k = i;k <= j;++k) + + if (abs(sorted[k].x - vec[m].x) < d) + + strip.push_back(sorted[k]); + + d = bruteForce(strip, d); + + return d; + +} + + + +double closestPair(vector& vec) { + + sort(vec.begin(), vec.end(), cmpX); + + vector sorted; + + sorted.resize(vec.size()); + + return closestPairDist(vec, 0, vec.size() - 1, sorted); + +} + + + +int main() { + + int n; + + fin >> n; + + int x, y; + + while (n--) { + + fin >> x >> y; + + vec.push_back({ x,y }); + + } + + fout << fixed << setprecision(6); + + fout< + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int Nmax = 100002; + + + +struct Point + +{ + + int x,y; + +} A[Nmax] , B[Nmax] , C[Nmax ]; + + + +bool CompX( Point a , Point b ) + + { return a.x Dist ) + + LeastDist = Dist ; + + } + + + + return LeastDist; + + + + } + + + + else + + { + + + + int Middle = (left + right ) / 2; + + Point Mid; + + Mid.x = A[Middle].x; + + Mid .y = 0 ; + + + + long long LeastDist1 = Solve ( left , Middle ) ; + + long long LeastDist2 = Solve ( Middle + 1 , right ) ; + + long long LeastDist = min(LeastDist1 , LeastDist2 ); + + + + int i ,j ,k = 0 , h = 0; + + for ( i = left , j =Middle +1 ; i<= Middle || j<=right ; ) + + if ( i<=Middle && ( ( A[i].y < A[j].y ) || j > right ) ) + + B[ ++k ] = A[ i++] ; + + else if ( j<=right ) + + B[++k] = A[j++]; + + + + + + for (i=1 , j = left ;i<=k && j<= right ;++i ,++j) + + {if ( ( (long long )B[i].x - (long long ) Mid.x ) * ( (long long )B[i].x - (long long ) Mid.x ) <= LeastDist ) + + C[++h] = B[i]; + + A[j]=B[i]; + + } + + + + for (i=1; i< h;++i) + + for (j=i+1; j<=h && j<=i+7;++j) + + { long long Dist = Distance(C[i],C[j]); + + if ( Dist < LeastDist ) + + LeastDist = Dist ; + + } + + + + return LeastDist; + + } + + + + + +} + + + + + +int main() + +{ + + + + int N ; + + long long result ; + + fin>>N; + + + + for ( int i = 1; i <= N ; ++i ) + + fin>>A[i].x>>A[i].y; + + + + sort(A+1,A+N+1,CompX); + + + + result = Solve( 1 , N ); + + + + + + + + fout< +#include #include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x=0,y=0; + +}; + +double distanta(punct a,punct b) + +{ + + double x =a.x -b.x; + + double y = a.y-b.y; + + double dis; + + dis = pow(x, 2)+pow(y, 2); + + dis = sqrt(dis); + + return dis; + +} + +bool cmpX(const punct& a,const punct& b) + +{ + + return a.x &P,vector &Y) + +{ + + if((dr-st)==1) + + { + + return 2147483647; + + } + + else if((dr-st)==2) + + { + + if(Y[st].y>Y[st+1].y) + + swap(Y[st],Y[st+1]); + + return distanta(P[0],P[1]); + + } + + int d; + + double min_st,min_dr,min_p; + + d=(st+dr)/2; + + min_st=dist_min(st,d,P,Y); + + min_dr=dist_min(d,dr,P,Y); + + min_p=min(min_st,min_dr); + + vector aux(dr-st); + + merge(Y.begin()+st,Y.begin()+d,Y.begin()+d,Y.begin()+dr,aux.begin(),cmpY); + + copy(aux.begin(),aux.begin()+(dr-st),Y.begin()+st); + + vector b; + + for (int i = st; i < dr; ++i) + + { + + if (abs(Y[i].x - P[d].x) <= min_p) + + b.push_back(Y[i]); + + } + + + + + + double min_t = min_p; + + for (int i = 0; i>n; + + vector P(n); + + for(i=0; i>P[i].x; + + f>>P[i].y; + + } + + sort(P.begin(),P.end(),cmpX); + + vector Y=P; + + g << fixed; + + g << setprecision(6) < +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int N; + +const long long INF = unsigned(-1); + +pair A[100010], V[100010]; + + + +inline long long euclid(pair a, pair b) { + + return (b.second-a.second)*(b.second-a.second) + (b.first-a.first)*(b.first-a.first); + +} + + + +bool cmp(pair a, pair b) { + + return a.second < b.second; + +} + + + +long long solve(int st, int dr) { + + long long p, p1, p2; + + if (dr-st == 0) { + + return INF; + + } + + if (dr-st == 1) { + + p = euclid(A[st], A[dr]); + + return p; + + } else if (dr-st == 2) { + + p = min(euclid(A[st], A[st+1]), euclid(A[st+1], A[st+2])); + + p = min(p, euclid(A[st], A[st+2])); + + return p; + + } + + int mid = (st+dr)/2; + + p1 = solve(st, mid); + + p2 = solve(mid+1, dr); + + p = min(p1, p2); + + long long e = A[mid].first; + + int k = 0; + + for(int i = st; i <= dr; ++i) { + + if(abs(A[i].first - e) <= int(sqrt(p)+1)) { + + V[++k] = A[i]; + + } + + } + + sort(V+1, V+k+1, cmp); + + for(int i = 1; i <= k; ++i) { + + for(int j = i + 1; j <= k and j - i <= 8; ++j) { + + p = min(p, euclid(V[i],V[j])); + + } + + } + + return p; + +} + + + +int main() + +{ + + fin>>N; + + for(int i = 1; i <= N; ++i) { + + fin>>A[i].first>>A[i].second; + + } + + sort(A+1, A+N+1); + + fout< +#include +#include +#include +#include +#include +#include + +using point = std::pair; +using pointArr = std::vector; + +double squaredDistance(const point& p1, const point& p2) { + return std::pow(p1.first - p2.first, 2) + std::pow(p1.second - p2.second, 2); +} + +double solve(const pointArr& points, int left, int right) { + static pointArr pointsCopy = points; + if (left >= right) { + return std::numeric_limits::max(); + } + else if (right - left == 1) { + if (pointsCopy[left].second > pointsCopy[right].second) { + std::swap(pointsCopy[left], pointsCopy[right]); + } + return squaredDistance(points[left], points[right]); + } + auto middle = (left + right) >> 1; + auto minLeft = solve(points, left, middle); + auto minRight = solve(points, middle + 1, right); + auto delta = std::min(minLeft, minRight); + pointArr candidates(right - left + 1); + std::merge(pointsCopy.begin() + left, pointsCopy.begin() + middle + 1, pointsCopy.begin() + middle + 1, pointsCopy.begin() + right + 1, candidates.begin(), + [](const point& p1, const point& p2) { + return p1.second < p2.second; + }); + std::copy(candidates.begin(), candidates.end(), pointsCopy.begin() + left); + candidates.clear(); + for (auto it = left; it <= right; ++it) { + if (std::abs(pointsCopy[it].first - points[middle].first) <= delta) { + candidates.push_back(pointsCopy[it]); + } + } + for(auto it = 0; it < candidates.size(); ++it) { + for (auto it2 = it + 1; it2 < candidates.size() && it2 - it < 8; ++it2) { + delta = std::min(delta, squaredDistance(candidates[it], candidates[it2])); + } + } + return delta; +} + +int main() +{ + pointArr points; + std::ifstream fin("cmap.in"); + auto n = 0; fin >> n; + for (auto it = 0; it < n; ++it) { + point currentPoint; + fin >> currentPoint.first >> currentPoint.second; + points.push_back(currentPoint); + } + fin.close(); + std::sort(points.begin(), points.end()); + std::ofstream fout("cmap.out"); + fout << std::fixed << std::setprecision(6) << std::pow(solve(points, 0, points.size() - 1), 0.5);; + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/a05ea63f-72e5-4c78-b3c4-b27835a336fc.cpp b/cmap/divide_conquer/a05ea63f-72e5-4c78-b3c4-b27835a336fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c061019c38fd1f7abcb785e796a9d7589d1dd5c0 --- /dev/null +++ b/cmap/divide_conquer/a05ea63f-72e5-4c78-b3c4-b27835a336fc.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +template +class Point +{ + public: + Point() = default; + Point(T x, T y): x_(x), y_(y) {} + T x() const {return x_;} + T y() const {return y_;} + bool operator < (const Point& other) const { + if (x() == other.x()) + return y() < other.y(); + return x() < other.x(); + } + bool operator > (const Point& other) const { + return !(*this < other); + } + private: + T x_, y_; +}; + +int N; +vector> X, Y, aux; + +template +T dist(Point p1, Point p2) { + return (p1.x() - p2.x())*(p1.x() - p2.x()) + + (p1.y() - p2.y())*(p1.y() - p2.y()); +} + +template +T cmap(int left, int right) +{ + if (right - left == 1) + return numeric_limits::max(); + + if (right - left == 2) { + if (Y[left] > Y[left + 1]) + swap(Y[left], Y[left+1]); + return dist(X[left], X[left+1]); + } + + int mid = (left + right) / 2; + T delta = min(cmap(left, mid), cmap(mid, right)); + + + merge(Y.begin() + left, Y.begin() + mid, Y.begin() + mid, Y.begin() + right, aux.begin()); + copy(aux.begin(), aux.begin() + (right - left), Y.begin() + left); + + + int sizeAux = 0; + for (int i = left; i < right; ++i) + if (abs(X[mid].x() - Y[i].y()) <= delta + 0.0000001) + aux[sizeAux++] = Y[i]; + + for (int i = 0; i < sizeAux; ++i) + for (int j = i + 1; j < sizeAux && j - i <= 3; ++j) + delta = min(delta, dist(aux[i], aux[j])); + return delta; +} + +int main() +{ + ifstream fin("cmap.in"); + ofstream fout("cmap.out"); + + ios::sync_with_stdio(false); + fin.tie(0); + + fin >> N; + aux.resize(N); + double x,y; + for (int i = 0 ; i < N; ++i) { + fin >> x >> y; + X.emplace_back(x, y); + } + sort(X.begin(), X.end()); + for (int i = 0; i < N; ++i) + Y.emplace_back(X[i].y(), X[i].x()); + + fout << fixed << setprecision(6) << sqrt(cmap(0, N)) << "\n"; + + return 0; +} diff --git a/cmap/divide_conquer/a07ab414-981d-458a-8306-28291683bbdb.cpp b/cmap/divide_conquer/a07ab414-981d-458a-8306-28291683bbdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01a67c4b6948bc796a5546e4e0277d0271828ea3 --- /dev/null +++ b/cmap/divide_conquer/a07ab414-981d-458a-8306-28291683bbdb.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +const long long inf=1e18; + +struct punct + +{ + + int x,y; + +} v[100010],v1[100010]; + + + +int cmp1(punct a,punct b) + +{ + + return a.x=dr) return inf; + + if(st+1==dr) + + { + + if(cmp2(v[dr],v[st])) swap(v[st],v[dr]); + + return dist(v[st],v[dr]); + + } + + int mid=(st+dr)/2,midx=v[mid].x; + + long long sol=min(solve(st,mid),solve(mid+1,dr)); + + merge(v+st,v+mid+1,v+mid+1,v+dr+1,v1+st,cmp2); + + int nr=0; + + for(int i=st; i<=dr; i++) + + { + + v[i]=v1[i]; + + if(sqr(v1[i].x-midx)<=sol) v1[++nr]=v1[i]; + + } + + for(int i=1; i<=nr; i++) + + for(int j=i+1; j<=nr && sqr(v1[i].y-v1[j].y)<=sol; j++) sol=min(sol,dist(v1[i],v1[j])); + + return sol; + +} + + + +int main() + +{ + + freopen("cmap.in", "r",stdin); + + freopen("cmap.out", "w", stdout); + + int n; + + scanf("%d",&n); + + for(int i=1; i<=n; i++) scanf("%d%d",&v[i].x,&v[i].y); + + sort(v+1,v+1+n,cmp1); + + printf("%.9f",sqrt(solve(1,n))); + + return 0; + +} diff --git a/cmap/divide_conquer/a07c79f0-6dcb-42ea-90a4-a7225b4e230b.cpp b/cmap/divide_conquer/a07c79f0-6dcb-42ea-90a4-a7225b4e230b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd3dff9a10b9d9e0c64db8d4ed4819c370874728 --- /dev/null +++ b/cmap/divide_conquer/a07c79f0-6dcb-42ea-90a4-a7225b4e230b.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +struct point +{ + double x, y; +}; +vectorp; + +double sqrtdist(point a, point b) +{ + return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); +} + + +bool ycmp(point a, point b) +{ + return a.y < b.y; +} + +bool xcmp(point a, point b) +{ + if(a.x < b.x) + { + return 1; + } + if(a.x == b.x) + { + return ycmp(a, b); + } + return 0; +} + + +double closest_pair(int l, int r) +{ + double minVal = 1e16; + + if(r - l <= 2) + { + for(int i = l; i <= r; i++) + { + for(int j = i + 1; j <= r; j++) + { + if(minVal > sqrtdist(p[i], p[j])) + { + minVal = sqrtdist(p[i], p[j]); + } + } + } + return minVal; + } + + + int mid = (l + r)/2; + minVal = min(closest_pair(l, mid), closest_pair(mid + 1, r)); + + vectory; + for(int i = l; i <= r; i++) + { + double horizontal = (p[mid].x - p[i].x) * (p[mid].x - p[i].x); + if(horizontal < minVal) + { + y.push_back(p[i]); + } + } + + sort(y.begin(), y.end(), ycmp); + for (int i = 0; i < y.size(); i++) + { + point a = y[i]; + for (int j = i + 1; j < y.size(); j++) + { + if(j-i > 7) + { + break; + } + point b = y[j]; + if(minVal > sqrtdist(a, b)) + { + minVal = sqrtdist(a, b); + } + } + } + + return minVal; +} + +int main() +{ + int n; + fin >> n; + + for(int i = 0; i < n; ++i) + { + point a; + fin >> a.x >> a.y; + p.push_back(a); + } + fin.close(); + + sort(p.begin(), p.end(), xcmp); + + fout << fixed << setprecision(7) << sqrt(closest_pair(0, n - 1)); + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/a09552d7-d6d2-4c9a-b521-ddc79c634e33.cpp b/cmap/divide_conquer/a09552d7-d6d2-4c9a-b521-ddc79c634e33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..593854e1a8eccb556c43adf2f0bc6c07a66be3e1 --- /dev/null +++ b/cmap/divide_conquer/a09552d7-d6d2-4c9a-b521-ddc79c634e33.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + +using namespace std; + + + +const long long inf=1e18; + +struct punct + +{ + + int x,y; + + bool operator <(const punct &aux) const + + { + + if(x==aux.x) return y=dr) return inf; + + else if(st+1==dr) + + { + + if(cmp(v[dr],v[st])) swap(v[st],v[dr]); + + return dist(v[st],v[dr]); + + } + + int mid=(st+dr)/2,midx=v[mid].x; + + long long d=min(solve(st,mid),solve(mid+1,dr)); + + merge(v+st,v+mid+1,v+mid+1,v+dr+1,aux,cmp); + + for(int i=st;i<=dr;i++) v[i]=aux[i-st]; + + int nr=0; + + for(int i=st;i<=dr;i++) + + if(sqr(midx-v[i].x)<=d) aux[++nr]=v[i]; + + for(int i=1;i<=nr;i++) + + for(int j=i+1;j<=nr && sqr(aux[j].y-aux[i].y)<=d;j++) d=min(d,dist(aux[i],aux[j])); + + return d; + +} + + + +int main() + +{ + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + int n; + + scanf("%d",&n); + + for(int i=1;i<=n;i++) scanf("%d%d",&v[i].x,&v[i].y); + + sort(v+1,v+1+n); + + printf("%.8f",sqrt(solve(1,n))); + + return 0; + +} diff --git a/cmap/divide_conquer/a0bc69e3-60e5-4939-987b-0306442f0a98.cpp b/cmap/divide_conquer/a0bc69e3-60e5-4939-987b-0306442f0a98.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee8cc026eed911410a90085caedfd7c8b1577a75 --- /dev/null +++ b/cmap/divide_conquer/a0bc69e3-60e5-4939-987b-0306442f0a98.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +struct Point { + long long x, y; + + friend struct byX; + struct byX { + bool operator() (Point const &p1, Point const &p2) { + return p1.x < p2.x; + } + }; + + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + + + + + +void read(long long &n, vector &points) { + ifstream fin("cmap.in"); + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); +} + + +double distance(const Point &A, const Point &B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + + + + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + double auxDist = distance(points[i], points[j]); + if (minDist > auxDist) { + minDist = auxDist; + } + } + } + + return minDist; +} + + +double divide(int li, int ls, vector &P, vector &Q) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, P); + } + + + int mid = (li + ls)/2; + + + + vector Ql; + Ql.push_back(Q[0]); + vector Qr; + Qr.push_back(Q[0]); + for (int i = 1; i < Q.size(); i++) { + if (Q[i].x <= P[mid].x) { + Ql.push_back(Q[i]); + } else { + Qr.push_back(Q[i]); + } + } + + + double dl = divide(li, mid, P, Ql); + double dr = divide(mid + 1, ls, P, Qr); + double dmin = min(dl, dr); + + + + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = 1; i < Q.size(); i++) { + if (abs(P[mid].x - Q[i].x) <= dmin) { + strip.push_back(Q[i]); + } + } + + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 8 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + + return dmin; +} + + +int main() { + + long long n; + vector P; + + read(n, P); + + vector Q = P; + + sort(P.begin() + 1, P.end(), Point::byX()); + + sort(Q.begin() + 1, Q.end(), Point::byY()); + + + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, P, Q); + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/a0d9be07-3f0b-4148-b1a5-0073e15d185e.cpp b/cmap/divide_conquer/a0d9be07-3f0b-4148-b1a5-0073e15d185e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2ebfdbce1803f59402866c57fc2ac492fa71f0 --- /dev/null +++ b/cmap/divide_conquer/a0d9be07-3f0b-4148-b1a5-0073e15d185e.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + + + +const int N = 100005; + +const long long INF = 4e18; + + + +pair X[N], V[N]; + + + +long long dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +bool compByX(const pair &a, const pair &b){ + + if(a.first == b.first){ + + return a.second < b.second; + + } + + return a.first < b.first; + +} + + + +bool compByY(pair a, pair b){ + + if(a.second == b.second){ + + return a.first < b.first; + + } + + return a.second < b.second; + +} + + + +long long solve(int lf, int rg) { + + if (lf == rg) + + return INF; + + else if (rg - lf == 1) { + + if (X[lf].second > X[lf + 1].second) + + swap(X[lf], X[lf + 1]); + + return dist(X[lf], X[lf + 1]); + + } + + int md = (lf + rg) / 2; + + long long best = min(solve(lf, md), solve(md + 1, rg)); + + + + merge(X + lf, X + md + 1, X + md + 1, X + rg + 1, V + 1, compByY); + + copy(V + 1, V + (rg - lf + 1) + 1, X + lf); + + + + int v_size = 0; + + for (int i = lf; i <= rg; ++ i) if (abs(X[i].first - X[md].first) <= best) + + V[++v_size] = X[i]; + + for (int i = 1; i <= v_size;++i) { + + for (int j = i + 1; j <= v_size && j - i <= 8;++j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main() { + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + int n; + + scanf("%d", &n); + + for (int i = 1;i <= n;++i) { + + scanf("%lld %lld", &X[i].first, &X[i].second); + + } + + sort(X + 1, X + n + 1, compByX); + + printf("%.6lf", sqrt(solve(1, n))); + + return 0; + +} diff --git a/cmap/divide_conquer/a0fcaa09-4e0a-4fbf-b4a8-40b2a1d2d08c.cpp b/cmap/divide_conquer/a0fcaa09-4e0a-4fbf-b4a8-40b2a1d2d08c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64efba5d31405a58ed3759711c5d88e78516a191 --- /dev/null +++ b/cmap/divide_conquer/a0fcaa09-4e0a-4fbf-b4a8-40b2a1d2d08c.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int n; + +pair nums[100005], mergeS[100005]; + +double sol; + + + +double dist(pair a, pair b); + + + +void divcon(int lft, int rgt); + + + +int main() + +{ + + + + fin >> n; + + for(int i = 1; i <= n; ++i){ + + fin >> nums[i].first >> nums[i].second; + + } + + + + std::sort(nums + 1, nums + n + 1); + + sol = DBL_MAX; + + + + divcon(1, n); + + + + fout << fixed << setprecision(6) << sol; + + + + return 0; + +} + + + +double dist(pair a, pair b){ + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +void divcon(int lft, int rgt){ + + int mid = (lft + rgt) / 2; + + if(lft >= rgt) return; + + + + if(lft == rgt - 1){ + + sol = min(sol, dist(nums[lft], nums[rgt])); + + return; + + } + + + + divcon(lft, mid); + + divcon(mid + 1, rgt); + + int stop = mid, start = lft, pos = lft - 1; ++mid; + + + + while(start <= stop && mid <= rgt){ + + if(nums[start].second <= nums[mid].second){ + + mergeS[++pos] = nums[start++]; + + } + + else{ + + mergeS[++pos] = nums[mid++]; + + } + + } + + + + while(start <= stop) mergeS[++pos] = nums[start++]; + + + + while(mid <= rgt) mergeS[++pos] = nums[mid++]; + + + + for(int i = lft; i <= rgt; ++i) nums[i] = mergeS[i]; + + + + for(int i = lft; i <= rgt; ++i){ + + for(int j = i + 1; j <= min(i + 8, rgt); ++j){ + + sol = min(sol, dist(nums[i], nums[j])); + + } + + } + + + +} diff --git a/cmap/divide_conquer/a12f9e3b-36b1-4ba6-ae23-72f6c7c0ae53.cpp b/cmap/divide_conquer/a12f9e3b-36b1-4ba6-ae23-72f6c7c0ae53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b808b76e76137ac235530473aa747bc635881f3a --- /dev/null +++ b/cmap/divide_conquer/a12f9e3b-36b1-4ba6-ae23-72f6c7c0ae53.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +typedef long long int LLI; +typedef pair PUNCT; + +const int NR_PCT = 7; +int DIM; + +PUNCT* citire(){ + + ifstream fin("cmap.in"); + + if(!fin){ + cerr << "Eroare fisier"; + exit(EXIT_FAILURE); + } + + fin >> DIM; + + PUNCT *vectorX = new PUNCT[DIM + 5]; + + if(!vectorX){ + cerr << "Eroare alocare"; + exit(EXIT_FAILURE); + } + + for(int i = 0; i < DIM; i++) + fin >> vectorX[i].first >> vectorX[i].second; + + fin.close(); + + return vectorX; + +} + +LLI calculDistanta(PUNCT &A, PUNCT &B){ + + return (LLI)(A.first - B.first) * (LLI)(A.first - B.first) + (LLI)(A.second - B.second) * (LLI)(A.second - B.second); + +} + +bool cmpY(PUNCT A, PUNCT B){ + return A.second < B.second; +} + +vector merge(PUNCT *vectorX, int st, int dr){ + + int mij = (st + dr) / 2; + int i = st; + int j = mij + 1; + vector tmpY; + + while(i <= mij && j <= dr){ + + if(vectorX[i].second < vectorX[j].second ) + tmpY.push_back(vectorX[i++]); + else + tmpY.push_back(vectorX[j++]); + } + + while ( i <= mij) + tmpY.push_back(vectorX[i++]); + + while ( j <= dr ) + tmpY.push_back(vectorX[j++]); + + int cnt = 0; + + for(int i = st; i <= dr; i++) + vectorX[i] = tmpY[cnt++]; + + return tmpY; + +} + +LLI dMinPuncte(PUNCT *vectorX,int st, int dr){ + + if( dr - st <= 3){ + + if(st == dr) + return LLONG_MAX; + + if(dr - st == 2){ + LLI dTmp = calculDistanta(vectorX[st],vectorX[dr]); + merge(vectorX,st,dr); + + return dTmp; + + } + + LLI d_1 = calculDistanta(vectorX[st],vectorX[dr]); + LLI d_2 = calculDistanta(vectorX[st],vectorX[dr-1]); + LLI d_3 = calculDistanta(vectorX[st+1],vectorX[dr]); + + + if(d_1 < d_2 && d_1 < d_3) + return d_1; + + if(d_2 < d_1 && d_2 < d_1) + return d_1; + + return d_3; + } + + unsigned int indiceMediana = ( st + dr ) / 2; + + if(dr == DIM) + dr--; + + LLI dMinSt = dMinPuncte(vectorX,st,indiceMediana); + LLI dMinDr = dMinPuncte(vectorX,indiceMediana + 1, dr); + + LLI dMin = dMinSt; + + if (dMin > dMinDr) + dMin = dMinDr; + + vector tmpY = merge(vectorX,st,dr); + + int lim = tmpY.size(); + + for(int i = 0; i < lim; i++){ + for(int j = i + 1; j <= i + NR_PCT && j < lim; j++){ + + LLI dTmp = calculDistanta(tmpY[i],tmpY[j]); + + if(dMin > dTmp) + dMin = dTmp; + + } + } + + return dMin; +} + +int main(){ + + PUNCT *vectorX = citire(); + sort(vectorX, vectorX + DIM); + + ofstream fout("cmap.out"); + + fout < +#include #include +#include +#include +#include + + + + + + +using namespace std; + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef long long ll; + +typedef vector < pair < ll, ll > > vect; + +vect X, V(100005); + +const ll INF = 4e18; + +int n; + + + +ll dist(const pair < ll, ll > & x, const pair < ll, ll > & y) { + + return (x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second); + +} + + + +ll ans(int st, int dr) + +{ + + if (st >= dr - 1) return INF; + + if (dr - st == 2) return dist(X[st], X[st + 1]); + + int mid = (st + dr) / 2; + + ll best = min(ans(st, mid), ans(mid, dr)); + + int m = 0; + + for (int i = st; i < dr; i++) + + if (abs(X[i].second - X[mid].first) <= best) + + V[m++] = X[i]; + + for (int i = 0; i < m; i++) + + for (int j = i + 1; j < m && j - i < 8; j++) + + best = min(best, dist(V[i], V[j])); + + return best; + +} + + + +int main() + +{ + + f>>n; + + X.resize(n); + + for (int i = 0; i < n; i++) + + f>>X[i].first>>X[i].second; + + sort(X.begin(), X.end()); + + g< +#include +#include + + +using namespace std; + + + +int a[100001],b[100001],c[100001],d[100001]; + + + +void intercl(int li,int lf) + +{ + + int m=(li+lf)/2,i,j,k; + + i=li;j=m+1;k=li; + + while(i<=m&&j<=lf) + + if(a[i] +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int NMAX = 100001; + +const long long INF = 0x7FFFFFFFFFFFFFFFLL; + + + +struct punct + +{ + + int x, y; + +}; + + + +punct P[NMAX], tmp[NMAX]; + +int N; + + + +bool compx(const punct& P1, const punct& P2) + +{ + + return P1.x < P2.x; + +} + + + +bool compy(const punct& P1, const punct& P2) + +{ + + return P1.y < P2.y; + +} + + + +inline long long sqr(const long long& x) + +{ + + return x * x; + +} + + + +inline long long dist2(const punct& P1, const punct& P2) + +{ + + return sqr(P1.x - P2.x) + sqr(P1.y - P2.y); + +} + + + +long long distmin(int st, int dr) + +{ + + long long dmin = INF; + + for(int i = st; i < dr; i++) + + for(int j = i + 1; j <= dr; j++) + + { + + long long dij = dist2(P[i], P[j]); + + if(dij < dmin) + + dmin = dij; + + } + + return dmin; + +} + + + +void interclas(const int& st, const int& M, const int& dr) + +{ + + int i = st; + + int j = M + 1; + + int k = st; + + for(; i <= M && j <= dr; k++) + + if(compy(P[i], P[j])) + + tmp[k] = P[i++]; + + else + + tmp[k] = P[j++]; + + while(i <= M) + + tmp[k++] = P[i++]; + + while(j <= dr) + + tmp[k++] = P[j++]; + +} + + + +long long calcDmin(int st, int dr) + +{ + + if(dr - st < 4) + + { + + sort(P + st, P + dr + 1, compy); + + return distmin(st, dr); + + } + + int M = (st + dr) / 2; + + int x = P[M].x; + + long long d = min(calcDmin(st, M), calcDmin(M, dr)); + + interclas(st, M, dr); + + int k = 0; + + for(int i = st; i <= dr; i++) + + { + + P[i] = tmp[i]; + + if(sqr(P[i].x - x) < d) + + tmp[++k] = P[i]; + + } + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && sqr(tmp[j].y - tmp[i].y) < d; j++) + + { + + long long dc = dist2(tmp[i], tmp[j]); + + if(dc < d) + + d = dc; + + } + + return d; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcDmin(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/a2ee61cd-d09c-44e3-a6ee-c331f50f1793.cpp b/cmap/divide_conquer/a2ee61cd-d09c-44e3-a6ee-c331f50f1793.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c8fbd1898edbf18c396490c6239371587e8b45c --- /dev/null +++ b/cmap/divide_conquer/a2ee61cd-d09c-44e3-a6ee-c331f50f1793.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +long long distan(pair ee, pair ef) + +{ + + return (long long)(ee.first-ef.first)*(ee.first-ef.first)+(long long)(ee.second-ef.second)*(ee.second-ef.second); + +} + +bool sortX(pair ee,pair ef) + +{ + + return ee.first ee,pair ef) + +{ + + return ee.second > &v,int l,int r) + +{ + + if(r-l==1) + + return distan(v[l],v[r]); + + if(r-l==2) + + { + + long long d1,d2,d3; + + d1=distan(v[l],v[r]); + + d2=distan(v[l+1],v[r]); + + d3=distan(v[l],v[l+1]); + + return min(d1,min(d2,d3)); + + } + + int m=(l+r)/2; + + long long d,d1,d2,dist; + + d1=calcmin(v,m+1,r); + + d2=calcmin(v,l,m); + + d=min(d1,d2); + + dist=(int)ceil(sqrt(d)); + + vector > temp; + + for(int i=l; i<=r; i++) + + if(abs(v[i].first-v[m].first)<=dist) + + temp.push_back(v[i]); + + sort(temp.begin(),temp.end(),sortY); + + for(int i=0; i >v; + + f>>n; + + for(int i=0; i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),sortX); + + g< +#include +#include +#include + + + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +long long n,i,j; + +pair v[100001],a[100001],b[100001]; + + + +long long inter(long long st,long long mid,long long dr){ + + long long i=st; + + long long j=mid+1; + + long long k=st-1; + + + + while(i<=mid&&j<=dr) + + { + + if(v[i].second a,pair b){ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + + + +long long divide(long long st, long long dr){ + + long long sol,i,j; + + + + if(dr-st==1){ + + inter(st,st,dr); + + return dist(v[st],v[dr]); + + } + + if(dr-st==2){ + + inter(st,st,dr-1); + + inter(st,dr-1,dr); + + return min(dist(v[st],v[dr]),min(dist(v[st+1],v[st]),dist(v[st+1],v[dr]))); + + } + + + + long long mid=(st+dr)/2; + + + + sol=min(divide(st,mid),divide(mid+1,dr)); + + inter(st,mid,dr); + + + + long long cnt=0; + + for(i=st;i<=dr;i++){ + + if(abs(v[mid].first-v[i].first)<=sol){ + + b[++cnt]=v[i]; + + } + + } + + + + for(i=1;i>n; + + for(i=1;i<=n;i++) + + f>>v[i].first>>v[i].second; + + + + sort(v+1,v+n+1); + + g< +using namespace std; + +typedef long long i64; + + + +const i64 INF = 1LL << 62; + +const int NMAX = 100005; + + + +struct PII { + + int x, y; + + + + inline PII() { } + + inline PII(int _x, int _y) { + + x = _x; + + y = _y; + + } + +}; + + + +vector vx, vy; + +PII aux[NMAX]; + + + +bool cmp_x(const PII &a, const PII &b) { + + return (a.x==b.x) ? (a.y < b.y) : (a.x < b.x); + +} + + + +bool cmp_y(const PII &a, const PII &b) { + + return (a.y==b.y) ? (a.x < b.x) : (a.y < b.y); + +} + + + +inline i64 dist(const PII &a, const PII &b) { + + return i64(a.x - b.x) * (a.x - b.x) + i64(a.y - b.y) * (a.y - b.y); + +} + + + +i64 ant(int st, int dr) { + + if(dr==st) { + + return INF; + + } + + if(dr-st<=2) { + + i64 ant = INF; + + + + for(int i=st; i<=dr; ++i) + + for(int j=i+1; j<=dr; ++j) + + ant = min(ant, dist(vx[i], vx[j])); + + + + sort(vy.begin()+st, vy.begin()+dr+1, cmp_y); + + + + return ant; + + } + + + + int med, puf; + + i64 ans; + + + + puf = 0; + + med = (st + dr) / 2; + + ans = min(ant(st, med), ant(med+1, dr)); + + + + merge(vy.begin()+st, vy.begin()+med+1, vy.begin()+med+1, vy.begin()+dr+1, aux, cmp_y); + + for(int i=st; i<=dr; ++i) + + vy[i] = aux[i - st]; + + + + for(int i=st; i<=dr; ++i) + + if(i64(vx[med].x-vy[i].x)*i64(vx[med].x-vy[i].x)<=ans) + + aux[puf++] = vy[i]; + + + + for(int i=0; i +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +int n, vf; + +pair a[100100], L, wek[100100]; + + + +long long dist(const pair &a, const pair &b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +bool cmp(const pair &a, const pair &b) { + + if (a.second == b.second) + + return a.first < b.first; + + return a.second < b.second; + +} + + + +long long solve(int st, int dr) { + + if (dr - st + 1 <= 3) { + + long long bst = LLONG_MAX; + + for (int i = st; i < dr; i++) + + for (int j = i + 1; j <= dr; j++) + + bst = min(bst, dist(a[i], a[j])); + + return bst; + + } + + int mid = st + dr >> 1; + + long long bst = min(solve(st, mid), solve(mid + 1, dr)); + + long long v = a[mid].first; + + vf = 0; + + for (int i = st; i <= mid; i++) + + if (abs(a[i].first - v) <= bst) + + wek[++vf] = a[i]; + + sort(wek + 1, wek + vf + 1, cmp); + + for (int i = 1; i <= vf; i++) + + for (int j = i + 1; j <= vf && j - i <= 7; j++) + + bst = min(bst, dist(wek[i], wek[j])); + + return bst; + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + cin.tie(NULL); + + in >> n; + + for (int i = 1; i <= n; i++) + + in >> a[i].first >> a[i].second; + + sort(a + 1, a + n + 1); + + out << fixed << setprecision(6) << sqrt((long double) solve(1, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/a437c602-1d58-4f63-8e1d-c39992c0cdbe.cpp b/cmap/divide_conquer/a437c602-1d58-4f63-8e1d-c39992c0cdbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89e38dad79ca575a7e4e78992bddebd4d4004868 --- /dev/null +++ b/cmap/divide_conquer/a437c602-1d58-4f63-8e1d-c39992c0cdbe.cpp @@ -0,0 +1,288 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const long long infinit = 4e18; + + + +struct triplet + +{ + + pair p1,p2; + + + + long long distanta; + +}; + + + + + +void interclasare(int p, int m, int q, vector< pair > & v) + +{ + + vector< pair > aux; + + + + int i = p, j = m; + + + + while(i a, pair b) + +{ + + return ((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +triplet DI(int st, int dr, vector< pair > & v, vector< pair > & u) + +{ + + if(dr-st < 2) + + { + + triplet inf; + + + + inf.distanta = infinit; + + + + inf.p1 = {infinit,infinit}; + + + + inf.p2 = {infinit,infinit}; + + + + return inf; + + } + + + + if(dr-st == 2) + + { + + if(u[st].first > u[dr-1].first) + + swap(u[st],u[dr-1]); + + + + triplet d; + + + + d.distanta = dist(v[st],v[dr-1]); + + + + d.p1 = v[st]; + + + + d.p2 = v[dr-1]; + + + + return d; + + } + + + + int m=(st+dr)/2; + + + + triplet D1 = DI(st, m, v, u); + + + + triplet D2 = DI(m, dr, v, u); + + + + triplet D_min; + + + + if(D1.distanta < D2.distanta) + + D_min = D1; + + else + + D_min = D2; + + + + interclasare(st, m, dr, u); + + + + vector< pair > proxim; + + + + for(int i=st ;i < dr; i++) + + if(abs(u[i].second-v[m].first) <= D_min.distanta) + + proxim.push_back(u[i]); + + + + for(int i=0; i < (int)proxim.size()-1; i++) + + for(int j=i+1; j < (int)proxim.size() && j-i<8 ; j++) + + if(D_min.distanta > dist(proxim[i],proxim[j])) + + { + + D_min.distanta = dist(proxim[i],proxim[j]); + + + + D_min.p1.first = proxim[i].second; + + + + D_min.p1.second = proxim[i].first; + + + + D_min.p2.first = proxim[j].second; + + + + D_min.p2.second = proxim[j].first; + + } + + + + return D_min; + +} + + + +int main() + +{ + + int n; + + + + fin>>n; + + + + vector< pair > v(n); + + + + for(int i=0; i>v[i].first>>v[i].second; + + + + sort(v.begin(), v.end()); + + + + vector< pair > u(n); + + + + for(int i=0; i +struct punct +{ + int x,y; +}; + +struct Comp1: std::binary_function +{ + bool operator()(punct p1, punct p2 ) + { + if(p1.x!=p2.x) + return p1.x +{ + bool operator()(punct p1, punct p2 ) + { + if(p1.y!=p2.y) + return p1.y + +using namespace std; + +typedef pair Point; + +long long Solve(vector::iterator b, vector::iterator e) { + auto m = b + (e - b) / 2; + if(m == b) return 4e18; + + int refX = m->first; + auto ret = min(Solve(b, m), Solve(m, e)); + + static auto cmpY = [](Point a, Point b) { + return a.second < b.second; + }; + inplace_merge(b, m, e, cmpY); + assert(is_sorted(b, e, cmpY)); + + vector red; + for(auto it = b; it != e; ++it) { + int dx = refX - it->first; + if(1LL * dx * dx < ret) + red.push_back(*it); + } + + auto j1 = red.begin(), j2 = red.begin(); + for(auto it = red.begin(); it != red.end(); ++it) { + while(j1 != red.end()) { + int dy = it->second - j1->second; + if(dy > 0 && 1LL * dy * dy >= ret) + ++j1; + else break; + } + while(j2 != red.end()) { + int dy = j2->second - it->second; + if(dy < 0 || 1LL * dy * dy < ret) + ++j2; + else break; + } + + assert(j2 - j1 <= 8); + + for(auto jt = j1; jt != j2; ++jt) + if(jt != it) { + int dx = it->first - jt->first; + int dy = it->second - jt->second; + ret = min(ret, 1LL * dx * dx + 1LL * dy * dy); + } + } + + return ret; +} + +int main() { + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + + int n; + cin >> n; + + vector P(n); + for(int i = 0; i < n; ++i) { + cin >> P[i].first >> P[i].second; + } + + sort(P.begin(), P.end()); + auto ret = Solve(P.begin(), P.end()); + cout << fixed << setprecision(12) << sqrt(ret); + return 0; +} diff --git a/cmap/divide_conquer/a5c70d8a-e947-47d7-9580-e6362851cd78.cpp b/cmap/divide_conquer/a5c70d8a-e947-47d7-9580-e6362851cd78.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b356fbebbaa96b701420bc86d2caeeff6eddca99 --- /dev/null +++ b/cmap/divide_conquer/a5c70d8a-e947-47d7-9580-e6362851cd78.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +struct point{ + + int x,y; + + point(const int a,const int b){ x=a; y=b; } + + point(){x=0;y=0;} + + + + bool operator<(const point ref)const { + + if( x < ref.x ) + + return true; + + if( x > ref.x ) + + return false; + + if( y < ref.y ) + + return true; + + return false; + + } + + + +}; + +point points[100000], pointsY[100000], aux[100000]; + + + +long long d(const point a,const point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long minDist(int left, int right){ + + + + if(right == left ) + + return 1e18; + + else if(right - left == 1){ + + if( pointsY[right] < pointsY[left] ) + + swap(pointsY[left], pointsY[right]); + + return d(pointsY[left], pointsY[right]); + + } + + + + + + int mid = (left + right) >> 1; + + long long minD = min( minDist(left, mid), minDist(mid + 1, right)); + + + + + + merge(pointsY + left, pointsY + mid + 1, pointsY + mid + 1, pointsY + right + 1, aux); + + copy(aux, aux + (right - left + 1), pointsY + left); + + + + + + int k = 0; + + for(int i = left; i <= right; i++) + + if(abs(points[mid].x - pointsY[i].y) <= minD && i != mid) + + aux[k++] = pointsY[i]; + + + + + + for(int i = 0; i < k; i++) + + for(int j = i + 1; j < k && j <= i + 8; j++) + + minD = min(minD, d(aux[i], aux[j])); + + + + + + return minD; + +} + + + +int main() + +{ + + f >> n; + + for(int i = 0; i < n; i++) + + f >> points[i].x >> points[i].y; + + + + + + sort(points, points + n); + + + + + + for(int i = 0; i < n; i++) + + pointsY[i] = point(points[i].y, points[i].x); + + + + g << fixed << setprecision(6) << sqrt(minDist(0, n - 1)); + + return 0; + +} diff --git a/cmap/divide_conquer/a7a1bcaf-f96c-479c-a8c8-ce4f899975c4.cpp b/cmap/divide_conquer/a7a1bcaf-f96c-479c-a8c8-ce4f899975c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c204565633b2d2b1bebd47a8ca5437fa48ec3414 --- /dev/null +++ b/cmap/divide_conquer/a7a1bcaf-f96c-479c-a8c8-ce4f899975c4.cpp @@ -0,0 +1,71 @@ +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + + + +const long long INF = 4e18; + +vector > points; + +long long sq(long long x) +{ + return x * x; +} + +long long dist(pair A, pair B) +{ + return (sq(A.first - B.first) + sq(A.second - B.second)); +} + +long long solve(long long l, long long r) +{ + if(r - l <= 3) + { + long long res = INF; + + for(long long i = l; i < r; i++) + for(long long j = i + 1; j <= r; j++) + res = min(res, dist(points[i], points[j])); + + return res; + } + + long long mid = (l + r) / 2; + long long d = min(solve(l, mid), solve(mid + 1, r)); + + vector > aux; + + for(long long i = l; i <= r; i++) + if(abs(points[mid].first - points[i].first) <= d) + aux.push_back({points[i].second, points[i].first}); + + sort(aux.begin(), aux.end()); + + for(long long i = 0; i < aux.size(); i++) + for(long long j = i + 1; j < aux.size() && j - i < 8; j++) + d = min(d, dist(aux[i], aux[j])); + + return d; +} + +main() +{ + long long n; + fin >> n; + + for(long long i = 1; i <= n; i++) + { + long long x, y; + fin >> x >> y; + + points.push_back({x, y}); + } + + sort(points.begin(), points.end()); + + fout << fixed << setprecision(6) << sqrt(1.0 * solve(0, n - 1)) << '\n'; +} diff --git a/cmap/divide_conquer/a829839e-3bb5-4fac-af66-d88dff3493fc.cpp b/cmap/divide_conquer/a829839e-3bb5-4fac-af66-d88dff3493fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a555754a208fd6ac559fe66e27373e43c18a9e9 --- /dev/null +++ b/cmap/divide_conquer/a829839e-3bb5-4fac-af66-d88dff3493fc.cpp @@ -0,0 +1,205 @@ +#include #include +#include #include +#include +#include + + +using namespace std; + + + +const long double INF = 1e100; + + + +struct punct + +{ + + punct(int x = 0, int y = 0) + + { + + this->x = x; + + this->y = y; + + } + + int x, y; + +}; + + + +bool cmp_x(const punct &a, const punct &b) + +{ + + if(a.x == b.x) + + return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmp_y(const punct &a, const punct &b) + +{ + + if(a.y == b.y) + + return a.x < b.x; + + return a.y < b.y; + +} + + + +long double dist(const punct &a, const punct &b) + +{ + + int difX = a.x - b.x; + + int difY = a.y - b.y; + + return sqrt(1LL * difX * difX + 1LL * difY * difY); + +} + + + +const int nMax = 100005; + + + +int n; + +vector v; + + + +void citire() + +{ + + ifstream in("cmap.in"); + + in >> n; + + v.resize(n); + + for(int i = 0; i < n; ++i) + + in >> v[i].x >> v[i].y; + + in.close(); + +} + + + +pair > GetRasp(int first, int last) + +{ + + int nrPoints = last - first + 1; + + vector y_sorted(nrPoints); + + if(nrPoints <= 3) + + { + + copy(v.begin() + first, v.begin() + last + 1, y_sorted.begin()); + + sort(y_sorted.begin(), y_sorted.end(), cmp_y); + + long double ret = INF; + + for(int i = first; i < last; ++i) + + for(int j = i + 1; j <= last; ++j) + + ret = min(ret, dist(v[i], v[j])); + + return make_pair(ret, y_sorted); + + } + + int mid = (first + last) / 2; + + pair > retSt = GetRasp(first, mid); + + pair > retDr = GetRasp(mid+1, last); + + + + long double ret = min(retSt.first, retDr.first); + + merge(retSt.second.begin(), retSt.second.end(), + + retDr.second.begin(), retDr.second.end(), + + y_sorted.begin(), cmp_y); + + + + for(int i = 0; i < y_sorted.size(); ++i) + + for(int j = i + 1; j < y_sorted.size(); ++j) + + { + + if(y_sorted[j].y - y_sorted[i].y >= ret) + + break; + + ret = min(ret, dist(y_sorted[i], y_sorted[j])); + + } + + + + return make_pair(ret, y_sorted); + +} + + + +void rezolvare() + +{ + + sort(v.begin(), v.end(), cmp_x); + + long double rasp = GetRasp(0, n-1).first; + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(8) << rasp; + + out.close(); + +} + + + +int main() + +{ + + citire(); + + rezolvare(); + + return 0; + +} diff --git a/cmap/divide_conquer/a8311f87-6ea9-4bbf-b957-0fdf1a80345f.cpp b/cmap/divide_conquer/a8311f87-6ea9-4bbf-b957-0fdf1a80345f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c4d427c9ddefe6ccb4fd2ba4780fe36beae12bf --- /dev/null +++ b/cmap/divide_conquer/a8311f87-6ea9-4bbf-b957-0fdf1a80345f.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +typedef long long ll; +typedef unsigned long long ull; +typedef unsigned int uint; +typedef long double ld; +typedef complex base; +typedef vector vi; +typedef pair pii; + +template ostream& operator<<(ostream& stream, const vector v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream; } +ll fpow(ll x, ll p, ll m){ll r=1; for (;p;p>>=1){ if (p&1) r=r*x%m; x=x*x%m; } return r;} +int gcd(int a, int b){ if (!b) return a; return gcd(b,a%b);} +ll gcd(ll a, ll b){ if (!b) return a; return gcd(b,a%b);} + + +struct point{ + int x,y; + point(int x=0, int y=0) : x(x), y(y) {} +}; + +double dist(point A, point B){ + return sqrt(1LL*(A.x-B.x)*(A.x-B.x)+1LL*(A.y-B.y)*(A.y-B.y)); +} + +int N; point a[100100]; +point v[100100]; int K; + +double mind(int l, int r){ + if (r-l+1<=1) return (1LL<<30); + if (r-l+1==2){ + if (a[l].y>a[r].y) swap(a[l],a[r]); + return dist(a[l],a[r]); + } + + double ans=(1LL<<30); + int mid=(l+r)/2,mx=a[mid].x; + ans=min(ans,mind(l,mid)); + ans=min(ans,mind(mid+1,r)); + + + + int i,j; + merge(a+l,a+mid+1,a+mid+1,a+r+1,v,[](point A, point B){return A.y med; + for (i=l; i<=r; i++) + if (abs(a[i].x-mx)<=ans) + med.push_back(a[i]); + + + for (i=0; i<(int)med.size(); i++) + for (j=i+1; j<(int)med.size() && med[j].y-med[i].y<=ans; j++) + ans=min(ans,dist(med[i],med[j])); + + + return ans; +} + + + +int main(){ + ios::sync_with_stdio(0); + cin.tie(0); + ifstream fin("cmap.in"); + ofstream fout("cmap.out"); + fin >> N; + + int i; + for (i=1; i<=N; i++) + fin >> a[i].x >> a[i].y; + + sort(a+1,a+N+1,[](point A, point B){return A.x +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int N; + +const long long INF = unsigned(-1); + +pair A[100010], V[100010]; + + + +inline long long euclid(pair a, pair b) { + + return (b.second-a.second)*(b.second-a.second) + (b.first-a.first)*(b.first-a.first); + +} + + + +long long solve(int st, int dr) { + + long long p, p1, p2; + + if (dr-st == 0) { + + return INF; + + } + + if (dr-st == 1) { + + p = euclid(A[st], A[dr]); + + return p; + + } else if (dr-st == 2) { + + p = min(euclid(A[st], A[st+1]), euclid(A[st+1], A[st+2])); + + p = min(p, euclid(A[st], A[st+2])); + + return p; + + } + + int mid = (st+dr)/2; + + p1 = solve(st, mid); + + p2 = solve(mid+1, dr); + + p = min(p1, p2); + + long long e = A[mid].first; + + int k = 0; + + for(int i = st; i <= dr; ++i) { + + if(abs(A[i].second - e) <= p) { + + V[++k] = A[i]; + + } + + } + + + + for(int i = 1; i <= k; ++i) { + + for(int j = i + 1; j <= k and j - i <= 8; ++j) { + + p = min(p, euclid(V[i],V[j])); + + } + + } + + return p; + +} + + + +int main() + +{ + + fin>>N; + + for(int i = 1; i <= N; ++i) { + + fin>>A[i].first>>A[i].second; + + } + + sort(A+1, A+N+1); + + fout< + + + + + + +using namespace std; + + + +void citire (int &n, vector > &PCT) + +{ + + ifstream f("cmap.in"); + + f >> n; + + for(int i = 0; i < n; i++) + + { + + int first, second; + + f >> first >> second; + + PCT.push_back (make_pair (first, second)); + + } + + f.close(); + +} + + + +long long distanta (pair A, pair B) + +{ + + return (long long)(A.first - B.first) * (A.first - B.first) + (long long)(A.second - B.second) * (A.second - B.second); + +} + + + +bool cmp (pair A, pair B) + +{ + + return A.second < B.second; + +} + + + +long long di (int st, int dr, vector > &PCT) + +{ + + if(dr - st == 1) + + { + + return distanta (PCT[st], PCT[dr]); + + + + } + + else if(dr - st == 2) + + { + + long long dist1 = distanta (PCT[st], PCT[st+1]); + + long long dist2 = distanta (PCT[st], PCT[st+2]); + + long long dist3 = distanta (PCT[st+1], PCT[st+2]); + + + + long long minim = dist1; + + if(dist2 < minim) + + minim = dist2; + + if(dist3 < minim) + + minim = dist3; + + return minim; + + + + + + } + + + + int m = (st + dr) / 2; + + long long dist = min (di(st, m, PCT), di(m + 1, dr, PCT)); + + int delta = (int) (ceil(sqrt(dist))); + + + + vector> temp; + + for(unsigned i = st; i <= dr; i++) + + if(abs(PCT[i].first - PCT[m].first) <= delta) + + temp.push_back(PCT[i]); + + sort(temp.begin(), temp.end(), cmp); + + + + for(unsigned i = 0; i < temp.size(); i++) + + for(unsigned j = i + 1; j <= i + 7 && j < temp.size(); j++) + + dist = min(dist, distanta (temp[i], temp[j])); + + + + return dist; + +} + + + +int main() + +{ + + int n; + + vector > PCT; + + + + citire(n, PCT); + + sort(PCT.begin(), PCT.end()); + + + + ofstream g("cmap.out"); + + g << fixed << setprecision(6) << sqrt(di(0, n - 1, PCT)); + + g.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/aa8257b7-d762-4a78-ac47-e5c19e06d163.cpp b/cmap/divide_conquer/aa8257b7-d762-4a78-ac47-e5c19e06d163.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32c435b4a2fef194b0b7b04186d02cbd5a90d430 --- /dev/null +++ b/cmap/divide_conquer/aa8257b7-d762-4a78-ac47-e5c19e06d163.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +struct point +{ + int x, y; +}; + +point v[100001], v2[100001], v3[100001]; +int n; + + +bool compare(point a, point b) +{ + if (a.x < b.x) + return true; + if (a.x > b.x) + return false; + return true; +} + +bool compare2(point a, point b) +{ + if (a.y < b.y) + return true; + if (a.y > b.y) + return false; + return true; +} + +long long dist(point a, point b) +{ + return ((long long)a.x-(long long)b.x)*((long long)a.x-(long long)b.x)+((long long)a.y-(long long)b.y)*((long long)a.y-(long long)b.y); +} + + +long long solve(int lower, int upper) +{ + if (lower == upper) + return 9e18; + + if (upper - lower == 1) + return dist(v[lower], v[upper]); + + int mid = lower + (upper - lower) / 2, i, j; + long long res1 = solve(lower, mid); + long long res2 = solve(mid+1, upper); + if (res1 > res2) + res1 = res2; + + i = lower; j = mid + 1; + double l = (double)v[mid].x + (v[mid+1].x - v[mid].x) / 2.0; + + for (int k = i; k <= j; k++) + v2[k] = v[k]; + sort(v2 + i, v2 + j + 1, compare2); + + int c = 0; + for (int k = i; k <= j; k++) + if (fabs((double)v2[k].x - l) <= res1) + { + c++; + v3[c] = v2[k]; + } + for (int k = 1; k <= c; k++) + { + for (int l = 1; l <= 7; l++) + { + if (k + l <= c) + { + if (res1 > dist(v3[k], v3[k+l])) + res1 = dist(v3[k], v3[k+l]); + } + } + } + + return res1; +} + +int main() +{ + int i; + fin >> n; + for (i = 1; i <= n; i++) + fin >> v[i].x >> v[i].y; + fin.close(); + sort(v + 1, v + n + 1, compare); + + long long res = solve(1, n); + fout << fixed << setprecision(6) << sqrt(res) << '\n'; + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/aab9e48b-3673-4e1e-aea1-2bbea37703c6.cpp b/cmap/divide_conquer/aab9e48b-3673-4e1e-aea1-2bbea37703c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ddc4ad97bcb3f3d3178b953c1dafb04fd65d0ab --- /dev/null +++ b/cmap/divide_conquer/aab9e48b-3673-4e1e-aea1-2bbea37703c6.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include + + + + + +using namespace std; + +pair v[100010]; +long long n,i,j ,m,ans; + +double dist( pair a, pair b ){ + return sqrt( (a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second) ); +} + +bool cmp( pair a, pair b ){ + if( a.second > b.second ) return true; + else if( a.second < b.second ) return false; + if( a.first >= b.first ) return true; + return false; +} + +long double cmapip( long long left, long long right ){ + long long middle = ( left + right ) / 2; + double ans = 2000000000; + if( right - left <= 3 ){ + for( long long i = left ; i <= right ; i++ ){ + for( long long j = i + 1 ; j <= right ; j++ ){ + ans = min( ans , dist( v[ i ] , v[ j ] ) ); + } + } + return ans; + } + ans = min( cmapip( left , middle ) , cmapip( middle + 1 , right ) ); + vector< pair > vec; + for( int i = left ; i <= right ; i++ ){ + if( abs( v[ i ].first - v[ middle ].first ) <= ans ){ + vec.push_back( v[ i ] ); + } + } + int l = vec.size(); + sort(vec.begin(), vec.end(), [](auto &left, auto &right) { + return left.second < right.second; +}); + for( int i = 0 ; i < l ; i++ ){ + for( int j = i + 1 ; j < min( l , i + 8 ) ; j++ ){ + ans = min( ans , dist( vec[ i ] , vec[ j ] ) ); + } + } + return ans; +} + +int main(){ + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + cin >> n; + for( long long i = 1 ; i <= n ; i++ ){ + cin >> v[ i ].first >> v[ i ].second; + } + sort( v + 1 , v + n + 1 ); + cout << fixed << setprecision(7) << cmapip( 1 , n ); + +return 0; +} diff --git a/cmap/divide_conquer/ab729b62-34a8-444a-8388-aed1f13d5e32.cpp b/cmap/divide_conquer/ab729b62-34a8-444a-8388-aed1f13d5e32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b45349cd60d61a56d040b9cb6be67ecd3a238749 --- /dev/null +++ b/cmap/divide_conquer/ab729b62-34a8-444a-8388-aed1f13d5e32.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +using namespace std; +ifstream cin("cmap.in"); +ofstream cout("cmap.out"); + + +const long long inf=4e18; + +int n; +vector > x; +long long calc(const pair a,const pair b){ + return ((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second)); +} + +long long solve(int st,int dr) +{ + if(dr-st<=3){ + long long ans=inf; + for (int i=st;i > aux; + for(int i=st;i<=dr;++i) + if(best>=abs(x[i].first-x[mij].first)) + aux.push_back(x[i]); + int counter=aux.size(); + sort(aux.begin(),aux.end(), [](auto &left, auto &right) { + return left.second < right.second; +}); + for(int i=0;i>n; + x.resize(n+1); + for(i=1;i<=n;i++) + cin>>x[i].first>>x[i].second; + sort(x.begin(),x.end()); + cout< +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +struct point { + + double x, y; + +}; + + + +int p; + +vectorv; + +double a, b; + + + +double dist(point a, point b) + +{ + + return (sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); + +} + + + +bool cmpX(point a, point b) + +{ + + if (a.x == b.x) + + return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmpY(point a, point b) + +{ + + if (a.y == b.y) + + return a.x < b.x; + + return a.y < b.y; + +} + + + +double bruteDistance(vector v) + +{ + + double d = 2000000000; + + for (int i = 0; i < v.size() - 1; i++) + + for (int j = i + 1; j < v.size(); j++) + + d = min(d, dist(v[i], v[j])); + + return d; + +} + + + +double calculateStrip(vector strip, double d) + +{ + + sort(strip.begin(), strip.end(), cmpY); + + double mi = 2000000000; + + for (int i = 0; i < strip.size() - 1; i++) + + for (int j = i + 1; j < strip.size() && (abs(strip[j].y - strip[i].y) < mi); j++) + + mi = min(mi, dist(strip[i], strip[j])); + + return mi; + +} + + + +double closest(vector v) + +{ + + int n = v.size()-1; + + if (n <= 3) + + return bruteDistance(v); + + int mid = n / 2; + + vectorst(v.begin(), v.begin() + mid); + + vectordr(v.begin() + mid+1, v.end()); + + double dLeft = closest(st); + + double dRight = closest(dr); + + double d = min(dLeft, dRight); + + vectorstrip; + + point Middle(v[mid]); + + for (int i = 0; i < v.size(); i++) + + if (abs(Middle.x - v[i].x) < d) + + strip.push_back(v[i]); + + return min(d, calculateStrip(strip, d)); + +} + + + +double closestDistance(vector v) + +{ + + sort(v.begin(), v.end(), cmpX); + + return closest(v); + +} + + + +void read() + +{ + + fin >> p; + + while (p--) + + { + + point temp; + + fin >> temp.x >> temp.y; + + v.push_back(temp); + + } + +} + + + +void solve() + +{ + + fout << setprecision(12) << fixed << closestDistance(v) << "\n"; + +} + + + + + +int main() + +{ + + read(); + + solve(); + + return 0; + +} diff --git a/cmap/divide_conquer/ace4a6cd-c315-4b56-991a-3f16d5e75ddb.cpp b/cmap/divide_conquer/ace4a6cd-c315-4b56-991a-3f16d5e75ddb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d4886fdb4eda181ba429da82fb1e2a6ed197516 --- /dev/null +++ b/cmap/divide_conquer/ace4a6cd-c315-4b56-991a-3f16d5e75ddb.cpp @@ -0,0 +1,309 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct punct + +{ + + int x,y; + +}; + +vector a,b,c; + +void citire(int &n,vector &a,vector &b) + +{ + + ifstream fin("cmap.in"); + + fin>>n; + + punct aux; + + int i; + + for(i=0;i>aux.x>>aux.y; + + a.push_back(aux); + + + + } + + fin.close(); + +} + +long long sqr(int a) + +{ + + return 1LL*a*a; + +} + + long long distanta(const punct a, const punct b){ + + return sqr(a.x-b.x)+sqr(a.y-b.y); + +} + +class compar1 + +{ + +public: + + bool operator() (const punct &a,const punct &b) + + { + + return a.y=j) + + return 2e81; + + + + if(j-i==1){ + + if(a[i].y>a[i+1].y) + + swap(a[i],a[i+1]); + + return distanta(a[i],a[i+1]); + + } + + int mid=(i+j)/2; + + long long d1=rezolva(i,mid); + + long long d2=rezolva(mid+1,j); + + long long d=minn(d1,d2); + + + + int dr=a[mid].x; + + interclasare(i,j,mid,dr,d); + + + + for(int k=0;kdistanta(b[k],b[l]) ){ + + d=distanta(b[k],b[l]); + + } + + + + b.clear(); + + + + return d; + + + +} + +int main() + +{ + + + + int n; + + + + citire(n,a,b); + + compar c; + + + + sort(a.begin(),a.end(),c); + + int i; + + ofstream fout("cmap.out"); + + fout< +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +int n,x,y; + +vector > X; + + + +long long sq(pair &i,pair &j){ + + + + return 1LL*(i.first-j.first)*(i.first-j.first)+ + + 1LL*(i.second-j.second)*(i.second-j.second); + + + +} + + + +long long minDist(int s,int d){ + + + + if(d-s<=2){ + + + + long long D=4e18; + + for(int i=s;i<=d;i++) + + for(int j=i+1;j<=d;j++) + + D=min(D,sq(X[i],X[j])); + + + + return D; + + + + } + + + + int m=(s+d)/2; + + + + long long D1=minDist(s,m); + + long long D2=minDist(m,d); + + long long D=min(D1,D2); + + + + vector > Y(X.begin()+s,X.begin()+d+1); + + for(int i=0;i > V; + + for(int i=0;i>n; + + + + for(int i=1;i<=n;i++){ + + + + cin>>x>>y; + + X.push_back(make_pair(x,y)); + + + + } + + + + sort(X.begin(),X.end()); + + + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +typedef pair Point; + + + +int N; + + + +Point points[100010], points_copy[100010]; + + + +vector mrg; + + + +double distance(const Point& p1, const Point& p2) + +{ + + return sqrt((p1.first - p2.first)*(p1.first - p2.first) + (p1.second - p2.second)*(p1.second - p2.second)); + +} + + + +double closest_pair_of_points(int x,int y) + +{ + + + + if (y - x > 3) + + { + + int mid = (x + y) / 2; + + double d1 = closest_pair_of_points(x, mid); + + + + double d2 = closest_pair_of_points(mid + 1, y); + + + + merge(points_copy + x, points_copy + mid + 1, points_copy + mid + 1, points_copy + y + 1, back_inserter(mrg)); + + + + double d = min(d1, d2); + + vector vec; + + + + vec.reserve(mrg.size()); + + + + for (int i=0;i> N; + + + + for (int i = 1; i <= N; ++i) + + { + + int x, y; + + + + in >> x >> y; + + + + points[i] = { x,y }; + + + + } + + + + mrg.reserve(100000); + + + + sort(points + 1, points + N + 1); + + + + for (int i = 1; i <= N; ++i) + + points_copy[i] = make_pair(points[i].second, points[i].first); + + + + out << fixed << setprecision(6) << closest_pair_of_points(1, N); + + + + return 0; + +} diff --git a/cmap/divide_conquer/ae5ba45a-2340-4525-abe3-48e8988a3cc5.cpp b/cmap/divide_conquer/ae5ba45a-2340-4525-abe3-48e8988a3cc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df0ca8161b9b40a36f2682316c4867b93d6cfc0a --- /dev/null +++ b/cmap/divide_conquer/ae5ba45a-2340-4525-abe3-48e8988a3cc5.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +using namespace std; + + + + + +typedef long long i64; + +const int N=100001; + +const i64 I=4e18; + +vector> v,x,y; + +int n,i; + +i64 D(pair &a,pair &b) + +{ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + +i64 G(int s,int d) + +{ + + if(s>=d-1) + + return I; + + if(d-s==2) + + { + + if(y[s]>y[s+1]) + + swap(y[s],y[s+1]); + + return D(x[s],x[s+1]); + + } + + int m=(s+d)/2,e=0,i,j; + + i64 b=min(G(s,m),G(m,d)); + + sort(y.begin()+s,y.begin()+d); + + for(i=s;i +#include +#include +#include + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +int n; + +pair v[100010],w[100010],q[100010]; + +long long dist(pair x, pair y){ + + return (x.first-y.first)*(x.first-y.first)+(x.second-y.second)*(x.second-y.second); + +} + +void interclasare(int st, int mid, int dr) { + + int i=st; + + int j=mid+1; + + int k=0; + + while(i<=mid&&j<=dr) + + if(v[i].second>n; + + for(int i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + fout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct point{ + + int x, y; + +}; + + + +int n; + +vector points; + +vector points2; + + + +void in(){ + + ifstream fin("cmap.in"); + + fin >> n; + + for (int i = 0; i < n; i++) + + { + + point p; + + fin >> p.x >> p.y; + + points.push_back(p); + + } + +} + + + +bool lessX(point a, point b){ + + return a.x < b.x; + +} + + + +bool lessY(point a, point b){ + + return a.y < b.y; + +} + + + +double dist(point a, point b){ + + return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); + +} + + + + + +double minDist(int s, int e){ + + if (e - s == 1){ + + + + return dist(points[s], points[e]); + + } + + else if (e - s == 2){ + + + + double d12 = dist(points[s], points[s + 1]); + + double d23 = dist(points[s + 1], points[e]); + + double d13 = dist(points[s], points[e]); + + + + return min(d12, min(d23, d13)); + + } + + else{ + + int mid = (e + s) / 2; + + double d = min(minDist(s, mid), minDist(mid + 1, e)); + + + + vector close; + + + + int i = mid; + + while (i >= s && points[mid + 1].x - points[i].x < d){ + + close.push_back(points[i]); + + i--; + + } + + + + i = mid + 1; + + while (i <= e && points[i].x - points[mid].x <= d){ + + close.push_back(points[i]); + + i++; + + } + + sort(close.begin(), close.end(), lessY); + + + + if (close.size() > 0){ + + + + + + for (int i = 0; i < close.size(); i++) + + { + + for (int j = i+1; j < min(i + 8, (int)close.size()); j++) + + { + + if (abs(close[i].y - close[j].y) <= d){ + + d = min(d, dist(close[i], close[j])); + + } + + } + + } + + } + return d; + + } + +} + + + +int main(){ + + in(); + + points2 = points; + + sort(points.begin(), points.end(), lessX); + + + + int a = 0, b = n-1; + + double d = minDist(a, b); + + + + ofstream f("cmap.out"); + + f << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/divide_conquer/b137742a-702f-4225-9d7f-cae6511a07c3.cpp b/cmap/divide_conquer/b137742a-702f-4225-9d7f-cae6511a07c3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25305e1a60a94104097430e513747b824edf58b7 --- /dev/null +++ b/cmap/divide_conquer/b137742a-702f-4225-9d7f-cae6511a07c3.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +typedef long long ll; +ll n; +vector< pair > v, r, a(100005); + +ll dist(pair a, pair b) +{ + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); +} + +ll rec(ll st, ll dr) +{ + if(dr-st <= 1) + return 2000000000000000000ll; + if(dr-st == 2){ + if(r[st] > r[st+1]) + swap(r[st], r[st+1]); + return dist(v[st], v[st+1]); + } + ll m = (st+dr)/2; + ll ans = min(rec(st, m), rec(m, dr)); + + merge(r.begin()+st, r.begin()+m, r.begin()+m, r.begin()+dr, a.begin()); + copy(a.begin(), a.begin()+(dr-st), r.begin()+st); + int vf = 0; + for (ll i = st; i < dr; ++i) + if(abs(r[i].second-v[i].first) <= ans) + a[vf++] = r[i]; + + for (ll i = 0; i < vf-1; ++i) + for (ll j = i+1; j < vf && j-i < 8; ++j) + ans = min(ans, dist(a[i], a[j])); + return ans; +} + +int main() +{ + ifstream fin ("cmap.in"); + ofstream fout ("cmap.out"); + fin >> n; + for (ll i = 1; i <= n; ++i){ + ll x, y; + fin >> x >> y; + v.push_back({x, y}); + } + sort(v.begin(), v.end()); + for (vector< pair >::iterator it = v.begin(); it != v.end(); ++it) + r.push_back({it->second, it->first}); + fout << fixed << setprecision(10) << sqrt(rec(0, n)) << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/b1cc8962-a2de-4ff2-9722-a9bd25ff9444.cpp b/cmap/divide_conquer/b1cc8962-a2de-4ff2-9722-a9bd25ff9444.cpp new file mode 100644 index 0000000000000000000000000000000000000000..717d75ef27e926281ac7e1b864b9de3a5cc59f54 --- /dev/null +++ b/cmap/divide_conquer/b1cc8962-a2de-4ff2-9722-a9bd25ff9444.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + + + +struct Punct { + + long long int x, y; + +}p[120000]; + + + +long long int get_dist(Punct p1, Punct p2) { + + return ((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y)); + +} + + + +bool cmp_points_y(Punct p1, Punct p2) { + + return (p1.y < p2.y ? true: false); + +} + + + +long long int bin_search(int left, int right) { + + if (right - left <= 4) { + + long long int d_min = get_dist(p[left], p[left + 1]); + + for (int i = left; i <= right; ++i) { + + for (int j = i + 1; j <= right; ++j) { + + d_min = min(d_min, get_dist(p[i], p[j])); + + } + + } + + return d_min; + + } + + + + int mij = (left + right) / 2; + + long long int d_min_left = bin_search(left, mij); + + long long int d_min_right = bin_search(mij + 1, right); + + + + long long int d_min = min(d_min_left, d_min_right); + + int right_pointer = mij; + + int left_pointer = mij - 1; + + + + Punct *p_curent = new Punct[right - left + 1]; + + int p_size = 0; + + + + while (left_pointer >= left) { + + if (abs(p[left_pointer].x - p[mij].x) < d_min) { + + p_curent[p_size].x = p[left_pointer].x; + + p_curent[p_size].y = p[left_pointer].y; + + p_size ++; + + left_pointer --; + + } else { + + break; + + } + + } + + while (right_pointer <= right) { + + if (abs(p[right_pointer].x - p[mij].x) < d_min) { + + p_curent[p_size].x = p[right_pointer].x; + + p_curent[p_size].y = p[right_pointer].y; + + p_size ++; + + right_pointer ++; + + } else { + + break; + + } + + } + + + + sort(p_curent, p_curent + p_size, cmp_points_y); + + for (int i = 0; i < p_size; ++i) { + + for (int j = 1; j < 9 && j + i < p_size; ++j) { + + d_min = min(d_min, get_dist(p_curent[i], p_curent[i + j])); + + } + + } + + + + delete[] p_curent; + + + + return d_min; + + + +} + + + +bool cmp_points_x(Punct p1, Punct p2) { + + return (p1.x < p2.x ? true: false); + +} + + + +int main() { + + + + f >> n; + + for (int i = 0; i < n; ++i) { + + f >> p[i].x >> p[i].y; + + } + + + + sort(p, p + n, cmp_points_x); + + + + long long int d_min = bin_search(0, n - 1); + + g << fixed << setprecision(7) << sqrt((double) d_min); + + + + return 0; + +} diff --git a/cmap/divide_conquer/b27e3844-322c-4908-b5b9-692a99d77d76.cpp b/cmap/divide_conquer/b27e3844-322c-4908-b5b9-692a99d77d76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef8bbe1bbacaec55baf83765445124cbdabe4e15 --- /dev/null +++ b/cmap/divide_conquer/b27e3844-322c-4908-b5b9-692a99d77d76.cpp @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include + + + + +FILE *f=fopen("cmap.in","r"), *g=fopen("cmap.out","w"); + + + +using namespace std; + + + +struct Punct{ long long int x, y; } v[100005], w[100005], p[100005]; + +int N; + + + +void Citire(){ + + + + int i; + + + + fscanf(f,"%d\n",&N); + + for(i=1;i<=N;i++){ + + fscanf(f,"%lld %lld\n",&v[i].x,&v[i].y); + + } + + + +} + + + +bool cmpx( Punct A, Punct B ){ + + if( A.x < B.x || ( A.x == B.x && A.y < B.y ) ) + + return 1; + + return 0; + +} + + + +long long int dist( Punct A, Punct B ){ return (B.x-A.x)*(B.x-A.x) + (B.y-A.y)*(B.y-A.y); } + + + +void Interclasare( int k1, int mij, int k2 ){ + + + + int i, j, k; + + + + i = k1; + + j = mij+1; + + for(k=k1;k<=k2;k++){ + + if( j > k2 || ( i <= mij && v[i].y < v[j].y ) ) + + w[k] = v[i++]; + + else + + w[k] = v[j++]; + + } + + for(k=k1;k<=k2;k++) + + v[k] = w[k]; + + + +} + + + +long long int CB( int k1, int k2 ){ + + + + int i, j, k, mij, indp; + + long long int dmin = LLONG_MAX; + + Punct dreaptaMIJ; + + + + if( k2-k1+1 <= 3 ){ + + + + for(i=k1;i v[j].y || ( v[i].y == v[j].y && v[i].x > v[j].x ) ) + + swap( v[i], v[j] ); + + } + + } + + + + return dmin; + + + + } + + + + mij = (k1+k2)/2; + + dreaptaMIJ = v[mij]; + + + + dmin = min( CB(k1,mij), CB(mij+1,k2) ); + + + + Interclasare(k1,mij,k2); + + + + indp = 0; + + for(k=k1;k<=k2;k++){ + + if( (dreaptaMIJ.x-v[k].x)*(dreaptaMIJ.x-v[k].x) <= dmin ){ + + p[++indp] = v[k]; + + } + + } + + + + for(i=1;i<=indp;i++){ + + for(j=i+1;j<=i+7&&j<=indp;j++){ + + dmin = min( dmin, dist( p[i], p[j] ) ); + + } + + } + + + + return dmin; + + + +} + + + +int main(){ + + + + Citire(); + + sort(v+1,v+N+1,cmpx); + + fprintf(g,"%.6lf\n", sqrt( CB(1,N)) ); + + + +return 0; + +} diff --git a/cmap/divide_conquer/b2b9be6f-375b-480d-bfe5-1e2c8fd3d783.cpp b/cmap/divide_conquer/b2b9be6f-375b-480d-bfe5-1e2c8fd3d783.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2e025b2c6f446904ec40cc95440eca6469fe5d4 --- /dev/null +++ b/cmap/divide_conquer/b2b9be6f-375b-480d-bfe5-1e2c8fd3d783.cpp @@ -0,0 +1,191 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int maxN=1e5+2; + +const double INF=1e14; + + + +int n; + +pair v[maxN]; + +pair aux[maxN]; + + + +double getDist(pair a,pair b){ + + return ((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +void mergeHalves(int st,int mij,int dr){ + + int idx1=st; + + int idx2=mij+1; + + int sz=0; + + + + while(idx1<=mij && idx2<=dr){ + + if(v[idx1].second=dr){ + + return INF; + + } + + + + if(dr-st==1){ + + mergeHalves(st,st,dr); + + return getDist(v[st],v[dr]); + + } + + + + int mij=(st+dr)/2; + + double midX=v[mij].first; + + + + double leftDist=divimp(st,mij); + + double rightDist=divimp(mij+1,dr); + + double res=min(leftDist,rightDist); + + + + mergeHalves(st,mij,dr); + + + + int sz=0; + + for(int i=st;i<=dr;i++){ + + if(abs(v[i].first-midX)<=res){ + + aux[++sz]=v[i]; + + } + + } + + + + for(int i=1;i<=sz;i++){ + + for(int j=i-1;j>0 && i-j<8;j--){ + + res=min(res,getDist(aux[i],aux[j])); + + } + + } + + + + return res; + +} + + + +int main(){ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + + + f>>n; + + for(int i=1;i<=n;i++){ + + f>>v[i].first>>v[i].second; + + } + + + + sort(v+1,v+n+1); + + + + g< +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +struct Point + +{ + + long long x,y; + +}; + + + +void read(int &n, vector &v) + +{ + + fin>>n; + + v.resize(n); + + + + for(int i=0;i>v[i].x>>v[i].y; + +} + + + +bool cmp(Point a, Point b) + +{ + + return ((a.x &v) + +{ + + int i=st,j=m+1; + + vector aux; + + + + while(i<=m && j<=dr) + + { + + if(v[i].y &v) + +{ + + if(dr-st==1) + + { + + long long minim=distance(v[st],v[dr]); + + if(v[st].y>v[dr].y) + + swap(v[st],v[dr]); + + + + return minim; + + + + } + + else if(dr-st==2) + + { + + long long minim=min(distance(v[st],v[st+1]),min(distance(v[st],v[dr]),distance(v[dr],v[st+1]))); + merge_function(st,st,st+1,v); + + merge_function(st,st+1,dr,v); + + + + return minim; + + } + + else + + { + + int m=(st+dr)/2; + + long long mst=divide(st,m,v); + + long long mdr=divide(m+1,dr,v); + + + + long long minim=min(mst,mdr); + + merge_function(st,m,dr,v); + + + + vector aux; + + for(int i=st ; i<=dr ; i++) + + if(abs(v[i].x-v[m].x)<=minim) + + aux.push_back(v[i]); + + + + int siz=aux.size(); + + + + for(int i=0;i v; + + read(n,v); + + sort(v.begin(),v.end(),cmp); + + + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + +struct Point { + long long x, y; + + friend struct byX; + struct byX { + bool operator() (Point const &p1, Point const &p2) { + return p1.x < p2.x; + } + }; + + friend struct byY; + struct byY { + bool operator() (Point const &p1, Point const &p2) { + return p1.y > p2.y; + } + }; +}; + +void read(long long &n, vector &points) { + ifstream fin("cmap.in"); + fin >> n; + Point aux; + aux.x = LLONG_MAX; + aux.y = LLONG_MAX; + points.push_back(aux); + for (int i = 1; i <= n; i++) { + fin >> aux.x >> aux.y; + points.push_back(aux); + } + fin.close(); +} + +void write(int n, vector points) { + for (int i = 1; i <= n; i++) { + cout << points[i].x << " " << points[i].y << "\n"; + } +} + +double distance(const Point &A, const Point &B) { + return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2)); +} + +double minDistanceBetweenNPoints(int li, int ls, vector &points) { + double minDist = LLONG_MAX; + + for (int i = li; i < ls; i++) { + for (int j = i + 1; j <= ls; j++) { + double auxDist = distance(points[i], points[j]); + if (minDist > auxDist) { + minDist = auxDist; + } + } + } + + return minDist; +} + +double divide(int li, int ls, vector &P, vector &Q) { + if (ls - li < 3) { + return minDistanceBetweenNPoints(li, ls, P); + } + int mid = (li + ls)/2; + vector Ql; + Ql.push_back(Q[0]); + vector Qr; + Qr.push_back(Q[0]); + for (int i = 1; i < Q.size(); i++) { + if (Q[i].x <= P[mid].x) { + Ql.push_back(Q[i]); + } else { + Qr.push_back(Q[i]); + } + } + double dl = divide(li, mid, P, Ql); + double dr = divide(mid + 1, ls, P, Qr); + double dmin = min(dl, dr); + + + + vector strip; + Point aux; + aux.x = INT_MAX; + aux.y = INT_MAX; + strip.push_back(aux); + for (int i = 1; i < Q.size(); i++) { + if (i != mid && abs(P[mid].x - Q[i].x) <= dmin) { + strip.push_back(Q[i]); + } + } + + for (int i = 1; i < strip.size() - 1; i++) { + for (int j = i + 1; j < i + 8 && j < strip.size(); j++) { + if (distance(strip[i], strip[j]) >= dmin) { + break; + } else if (distance(strip[i], strip[j]) < dmin) { + dmin = distance(strip[i], strip[j]); + } + } + } + + return dmin; +} + +int main() { + long long n; + vector P; + read(n, P); + vector Q = P; + sort(P.begin() + 1, P.end(), Point::byX()); + sort(Q.begin() + 1, Q.end(), Point::byY()); + ofstream fout("cmap.out"); + fout << fixed << setprecision(6) << divide(1, n, P, Q); + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/b5a4b928-4564-4fb0-b00d-2e2384c111ec.cpp b/cmap/divide_conquer/b5a4b928-4564-4fb0-b00d-2e2384c111ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7fba677478170a52d56a0e40d2a661295bf0ecf --- /dev/null +++ b/cmap/divide_conquer/b5a4b928-4564-4fb0-b00d-2e2384c111ec.cpp @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct Point + +{ + + int x, y; + +}; + + + +bool compareX(Point p1, Point p2) + +{ + + return (p1.x < p2.x); + +} + + + + + +bool compareY(Point p1, Point p2) + +{ + + return (p1.y < p2.y); + +} + + + +double dist(Point p1, Point p2) + +{ + + + + double dist = sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2)); + + return dist; + +} + + + +double bruteForce(Point P[], int n) + +{ + + double mini = 9999999999.0; + + for (int i = 0; i < n; ++i) + + for (int j = i + 1; j < n; ++j) + + if (dist(P[i], P[j]) < mini) + + mini = dist(P[i], P[j]); + + return mini; + +} + + + + + +double ClosestMid(Point midarr[], int size, double d) + +{ + + double mini = d; + + + + sort(midarr, midarr + size, compareY); + + + + for (int i = 0; i < size; ++i) + + for (int j = i + 1; j < size && (midarr[j].y - midarr[i].y) < mini; ++j) + + if (dist(midarr[i], midarr[j]) < mini) + + mini = dist(midarr[i], midarr[j]); + + + + return mini; + +} + + + +double closestUtil(Point P[], int n) + +{ + + if (n <= 3) + + return bruteForce(P, n); + + + + int mid = n / 2; + + Point midPoint = P[mid]; + + + + double dl = closestUtil(P, mid); + + double dr = closestUtil(P + mid, n - mid); + + + + double d = min(dl, dr); + + + + Point* midarr = new Point[n]; + + int j = 0; + + for (int i = 0; i < n; i++) + + if (abs(P[i].x - midPoint.x) < d) + + { + + midarr[j] = P[i]; + + j++; + + } + + + + + + return min(d, ClosestMid(midarr, j, d)); + +} + + + +double closest(Point P[], int n) + +{ + + sort(P, P + n, compareX); + + return closestUtil(P, n); + +} + + + +int main() + +{ + + int n,i; + + + + in >> n; + + Point * P = new Point[n]; + + for (i = 0;i < n;i++) + + { + + in >> P[i].x >> P[i].y; + + } + + double r = closest(P, n); + + out < +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +typedef pair point; + +vector points; + + + +void read() { + + + + ifstream in("cmap.in"); + + + + int n; + + long long x, y; + + + + for (in >> n; n; --n) { + + + + in >> x >> y; + + + + points.push_back(make_pair(x, y)); + + } + + + + in.close(); + +} + + + +bool compareY(const point &p1, const point &p2) { + + + + return (p1.second <= p2.second); + +} + + + +long long dist(const point& p1, const point &p2) { + + + + return (p2.first - p1.first) * (p2.first - p1.first) + (p2.second - p1.second) * (p2.second - p1.second); + +} + + + +long long bruteMinDist(int left, int right) { + + + + long long minValue = 0; + + + + for (int i = left; i < right; i++) + + for (int j = i + 1; j <= right; j++) { + + + + if (minValue == 0) + + minValue = dist(points[i], points[j]); + + else + + minValue = min(minValue, dist(points[i], points[j])); + + + + } + + + + return minValue; + +} + + + +long long stripMinDist(const vector& strip, long long delta) { + + + + long long minDist = delta; + + + + for (unsigned int i = 0; i < strip.size(); i++) + + for (unsigned int j = i + 1; j < strip.size() && (strip[i].second - strip[j].second) * (strip[i].second - strip[j].second) <= delta; j++) + + minDist = min(minDist, dist(strip[i], strip[j])); + + + + return minDist; + +} + + + +long long mergeSort(int left, int right) { + + + + if (right - left <= 2) { + + + + sort(points.begin() + left, points.begin() + right + 1, compareY); + + return bruteMinDist(left, right); + + + + } else { + + + + int mid = left + (right - left) / 2; + + long long midX = points[mid].first; + + + + long long dist1 = mergeSort(left, mid); + + long long dist2 = mergeSort(mid + 1, right); + + long long delta = min(dist1, dist2); + + + + + + + + vector result; + + result.resize(right - left + 1); + + + + merge(points.begin() + left, points.begin() + mid + 1, points.begin() + mid + 1, points.begin() + right + 1, result.begin(), compareY); + + copy(result.begin(), result.end(), points.begin() + left); + + + + + + + + vector strip; + + + + for (int i = left; i <= right; i++) + + if ((midX - points[i].first) * (midX - points[i].first) <= delta) + + strip.push_back(points[i]); + + + + long long dist3 = stripMinDist(strip, delta); + + + + return min(delta, dist3); + + } + +} + + + +int main() { + + + + read(); + + sort(points.begin(), points.end()); + + + + long long minDist = mergeSort(0, points.size() - 1); + + + + ofstream out("cmap.out"); + + out << fixed << setprecision(6) << sqrt(minDist) << "\n"; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/b64aaee4-a8c4-4e82-bcb4-ffd3b91c69f9.cpp b/cmap/divide_conquer/b64aaee4-a8c4-4e82-bcb4-ffd3b91c69f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7f29189057d3187ef6baec99f6f282a84cb42a0 --- /dev/null +++ b/cmap/divide_conquer/b64aaee4-a8c4-4e82-bcb4-ffd3b91c69f9.cpp @@ -0,0 +1,74 @@ +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pll; +ll INF = 4e18; + + +vector x,y,v; + +ll dist(pll a, pll b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + + +ll solve(int st, int dr) { + if (st >= dr - 1) return INF; + if (dr - st == 2) { + if (y[st] > y[st + 1]) { + swap(y[st], y[st + 1]); + } + return dist(y[st], y[st + 1]); + } + int mij = (st + dr) / 2; + long long ret = min(solve(st, mij), solve(mij, dr)); + + + merge(y.begin() + st, y.begin() + mij, y.begin() + mij, y.begin() + dr, v.begin()); + copy(v.begin(), v.begin() + (dr - st), y.begin() + st); + + + int nr = 0; + for (int i=st; i &a) { + int N = a.size(); + x = a; + v.resize(N); + sort(x.begin(), x.end()); + for(int i=0;i a; + scanf("%d",&N); + a.resize(N); + for (int i = 0; i < N; ++ i) { + scanf("%lld%lld",&a[i].first,&a[i].second); + } + printf("%.10lf\n",closest_points(a)); + return 0; +} diff --git a/cmap/divide_conquer/b7123226-69f3-41ad-a166-e2fdf3e68f1d.cpp b/cmap/divide_conquer/b7123226-69f3-41ad-a166-e2fdf3e68f1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32e27b183656c5983fa4ae50b965fd2127bdfb33 --- /dev/null +++ b/cmap/divide_conquer/b7123226-69f3-41ad-a166-e2fdf3e68f1d.cpp @@ -0,0 +1,226 @@ +#include + + + + +using namespace std; + +typedef long long llong; + + + +struct Punct + +{ + + int x, y; + +} px[100005], tmp[100005]; + + + +bool cmpx(const Punct& a, const Punct& b) + +{ + + if(a.x == b.x) return a.y < b.y; + + return a.x < b.x; + +} + + + +bool cmpy(const Punct& a, const Punct& b) + +{ + + if(a.y == b.y) return a.x < b.x; + + return a.y < b.y; + +} + + + +llong dist2(const Punct& a, const Punct& b) + +{ + + llong dx = a.x - b.x; + + llong dy = a.y - b.y; + + return dx * dx + dy * dy; + +} + + + +llong calc(int p, int q) + +{ + + llong res = 0x7fffffffffffffffLL; + + for(int i = p; i < q; i++) + + { + + for(int j = i + 1; j <= q; j++) + + { + + llong aux = dist2(px[i], px[j]); + + if(aux < res) + + res = aux; + + } + + } + + sort(px + p, px + q + 1, cmpy); + + return res; + +} + + + +inline llong sqr(int x) + +{ + + return (llong)x * x; + +} + + + +void inter(int p, int q, int m) + +{ + + int i = p, j = m + 1, k; + + for(k = p; i <= m && j <= q; k++) + + { + + if(cmpy(px[i], px[j])) + + tmp[k] = px[i++]; + + else tmp[k] = px[j++]; + + } + + for(; i <= m; i++) + + tmp[k++] = px[i]; + + for(; j <= q; j++) + + tmp[k++] = px[j]; + +} + + + +llong solve(int p, int q) + +{ + + if(q - p < 3) + + return calc(p, q); + + int m = (p + q) / 2; + + int mx = px[m].x; + + llong d1 = solve(p, m); + + llong d2 = solve(m + 1, q); + + llong dd = min(d1, d2); + + + + + + inter(p, q, m); + + + + int k = 0; + + for(int i = p; i <= q; i++) + + { + + px[i] = tmp[i]; + + if(sqr(px[i].x - mx) < dd) + + tmp[k++] = px[i]; + + } + + + + for(int i = 0; i < k; i++) + + { + + for(int j = i + 1; sqr(tmp[j].y - tmp[i].y) < dd && j < k; j++) + + { + + llong dist = dist2(tmp[i], tmp[j]); + + if(dd > dist) + + dd = dist; + + } + + } + + return dd; + +} + + + +int main() + +{ + + int n, i; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> n; + + for(i = 0; i < n; i++) + + { + + fin >> px[i].x >> px[i].y; + + } + + sort(px, px + n, cmpx); + + llong res = solve(0, n - 1); + + fout << fixed << setprecision(6) << sqrtl(res); + + return 0; + +} diff --git a/cmap/divide_conquer/b72155ec-4e24-4bd9-9816-74ec7662609d.cpp b/cmap/divide_conquer/b72155ec-4e24-4bd9-9816-74ec7662609d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..357ce34a8cfdf4faf51faa2f546ff27b19c492d4 --- /dev/null +++ b/cmap/divide_conquer/b72155ec-4e24-4bd9-9816-74ec7662609d.cpp @@ -0,0 +1,112 @@ +#include + + + + + + +const int MAXN = (int) 1e5; + +const double INF = (double) 2e9; + + + +std::pair pts[MAXN + 1]; + + + +inline double dist(std::pair a, std::pair b) { + + return sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); + +} + + + +std::vector < std::pair > pts1; + + + +bool cmp(std::pair a, std::pair b) { + + return b.second - a.second >= 0; + +} + + + + + +double solve(int l, int r) { + + double dst = INF; + + if(r - l + 1 == 1) + + return INF; + + if(r - l + 1 == 2) + + return dist(pts[l], pts[r]); + + if((r - l + 1) == 3) { + + dst = std::min(dist(pts[l], pts[r]), std::min(dist(pts[l], pts[l + 1]), dist(pts[l + 1], pts[r]))); + + return dst; + + } + + int med = (l + r) / 2; + + dst = std::min(solve(l, med), solve(med + 1, r)); + + pts1.clear(); + + for(int i = l; i <= r; i++) + + if(dst - std::fabs(pts[med].first - pts[i].first) >= 0) + + pts1.push_back({pts[i].first, pts[i].second}); + + std::sort(pts1.begin(), pts1.end(), cmp); + + for(int i = 0; i < pts1.size(); i++) + + for(int j = i + 1; j < pts1.size() && j <= i + 7; j++) + + dst = std::min(dst, dist(pts1[i], pts1[j])); + + return dst; + +} + + + +int main() { + + FILE *fi, *fout; + + int i, n; + + fi = fopen("cmap.in" ,"r"); + + fout = fopen("cmap.out" ,"w"); + + fscanf(fi,"%d " ,&n); + + for(i = 1; i <= n; i++) + + fscanf(fi,"%lf %lf " ,&pts[i].first,&pts[i].second); + + std::sort(pts + 1, pts + n + 1); + + fprintf(fout,"%.6lf" ,solve(1, n)); + + fclose(fi); + + fclose(fout); + + return 0; + +} diff --git a/cmap/divide_conquer/b78af536-20ef-4197-9dac-bc9dcea7b5f5.cpp b/cmap/divide_conquer/b78af536-20ef-4197-9dac-bc9dcea7b5f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94ad1c0ca0a8c5da761a770f701b9a2b3825c8db --- /dev/null +++ b/cmap/divide_conquer/b78af536-20ef-4197-9dac-bc9dcea7b5f5.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include + +std::ifstream fin("cmap.in"); +std::ofstream fout("cmap.out"); + +int n; +std::vector> V; + +void interclasare(int, int, int); + +void citire() { + fin >> n; + int x, y; + for (int i = 1; i <= n; i++) { + fin >> x >> y; + V.emplace_back(x, y); + } +} + +bool cmp(const std::pair &p1, const std::pair &p2) { + return (p1.second < p2.second); +} + +long long distanta_euclidiana_la_patrat(const std::pair &p1, const std::pair &p2) { + return (long long) (p1.first - p2.first) * (long long) (p1.first - p2.first) + + (long long) (p1.second - p2.second) * (long long) (p1.second - p2.second); +} + + +long long Solve(int st, int dr) { + + if (dr - st == 1) { + if (V[st].second > V[dr].second) + std::swap(V[st], V[dr]); + return distanta_euclidiana_la_patrat(V[st], V[dr]); + } else if (dr - st == 2) { + std::sort(V.begin() + st, V.begin() + dr + 1, cmp); + return std::min(distanta_euclidiana_la_patrat(V[st], V[st + 1]), + std::min(distanta_euclidiana_la_patrat(V[st], V[st + 2]), + distanta_euclidiana_la_patrat(V[st + 1], V[st + 2]))); + } + + int mij = (st + dr) / 2; + int mij_x = V[mij].first; + long long d = std::min(Solve(st, mij), Solve(mij + 1, dr)); + auto delta = static_cast(std::ceil(std::sqrt(d))); + interclasare(st, mij, dr); + + std::vector> A; + for (int i = st; i <= dr; i++) { + if (std::abs((V[i].first - mij_x)) <= delta) + A.push_back(V[i]); + } + for (int i = 0; i < A.size(); i++) + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) + d = std::min(d, distanta_euclidiana_la_patrat(A[i], A[j])); + + return d; +} + +void interclasare(int start, int m, int end) { + std::vector> Aux; + int i = start, j = m + 1; + + while ((i <= m) && (j <= end)) + if (V[i].second <= V[j].second) + Aux.push_back(V[i++]); + else + Aux.push_back(V[j++]); + + while (i <= m) + Aux.push_back(V[i++]); + + while (j <= end) + Aux.push_back(V[j++]); + + for (i = start; i <= end; i++) + V[i] = Aux[i - start]; +} + +int main() { + citire(); + + sort(V.begin(), V.end()); + + fout << std::fixed << std::setprecision(6) << sqrt(Solve(0, n - 1)) << '\n'; + + return 0; +} diff --git a/cmap/divide_conquer/bac77ab7-c5b3-4056-955e-a3f46f563a1b.cpp b/cmap/divide_conquer/bac77ab7-c5b3-4056-955e-a3f46f563a1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95a24a21ebbce20ea1c3e8db1c1845ff0829450f --- /dev/null +++ b/cmap/divide_conquer/bac77ab7-c5b3-4056-955e-a3f46f563a1b.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int NMAX = 100001; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x, y; + +}; + + + +punct v[NMAX], Tmp[NMAX]; + +int N; + + + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + + + +inline long long patrat(long long x) + +{ + + return x * x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + return patrat(A.x - B.x) + patrat(A.y - B.y); + +} + + + +long long distMin(int p, int u) + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long dij = dist2(v[i], v[j]); + + if(dmin > dij)dmin = dij; + + } + + return dmin; + +} + + + +long long calcdist(int p, int u) + +{ + + if(u - p < 3) + + return distMin(p, u); + + int m = (p + u) / 2; + + long long d1 = calcdist(p, m); + + long long d2 = calcdist(m, u); + + long long dd = min(d1, d2); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(patrat(v[i].x - v[m].x) < dd) + + Tmp[++k] = v[i]; + + sort(Tmp + 1, Tmp + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && patrat(Tmp[j].y - Tmp[i].y) < dd; j++) + + { + + d1 = dist2(Tmp[i], Tmp[j]); + + if(dd > d1)dd = d1; + + } + + return dd; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> v[i].x >> v[i].y; + + sort(v + 1, v + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdist(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/bae1a137-2264-4b09-a35e-b2b5c62b44f4.cpp b/cmap/divide_conquer/bae1a137-2264-4b09-a35e-b2b5c62b44f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60f69484d98247a62ecb106dd4ca4773da580582 --- /dev/null +++ b/cmap/divide_conquer/bae1a137-2264-4b09-a35e-b2b5c62b44f4.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long double ld; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const ld INF = 1e13; + +vector < pair < ld, ld > > points; +vector < pair < ld, ld > > strip; + +ld getDistance(const pair < ld, ld > &a, const pair < ld, ld > &b) { + return sqrtl((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); +} +ld Solve(int s, int e) { + if(e - s <= 3) { + ld dist = INF; + for(int i = s; i < e; ++i) { + for(int j = i + 1; j < e; ++j) { + dist = min(dist, getDistance(points[i], points[j])); + } + } + return dist; + } + + int mid = (s + e) / 2; + ld dist = min(Solve(s, mid), Solve(mid, e)); + + int idx = 0; + for(int i = s; i < e; ++i) { + if(abs(points[mid].first - points[i].first) < dist) { + strip[idx++] = points[i]; + } + } + + sort(strip.begin(), strip.begin() + idx, [&](const pair < ld, ld > &a, const pair < ld, ld > &b) { + return a.second < b.second; + }); + + for(int i = 0; i < idx; ++i) { + for(int j = i + 1; j < min(i + 7, idx); ++j) { + dist = min(dist, getDistance(strip[i], strip[j])); + } + } + return dist; +} + +int main() { + int n; fin >> n; + points.resize(n); strip.resize(n); + for(int i = 0; i < n; ++i) { + fin >> points[i].first >> points[i].second; + } + + sort(points.begin(), points.end()); + fout << fixed << setprecision(10) << Solve(0, n); + return 0; +} diff --git a/cmap/divide_conquer/bbecf323-be43-4a19-ba4f-05263d6bceea.cpp b/cmap/divide_conquer/bbecf323-be43-4a19-ba4f-05263d6bceea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51f5272be6e97e31b970bb7980f057720a4b5abd --- /dev/null +++ b/cmap/divide_conquer/bbecf323-be43-4a19-ba4f-05263d6bceea.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + + +typedef pair point; +const int MAXN = 100010; +const double INF = 1e16; +const int PRECISION = 6; +vector V(MAXN); +int N; + +double dist( point p1, point p2) { + return sqrt( (p1.first - p2.first)*(p1.first - p2.first) + + (p1.second - p2.second)*(p1.second - p2.second) + ); +} + +double bruteForce(int from, int to) { + double rs = INF; + for (int i = from; i <= to; i++) + for (int j = i + 1; j <= to; j++ ) + rs = min(rs, dist(V[i], V[j])); + + return rs; +} + +double closestOverlapping(vector &strip, double minDist) { + sort(strip.begin(), strip.end(), [](point a, point b) { + + swap(a.first, a.second); swap(b.first, b.second); + return a < b; + }); + for (int i = 0; i < strip.size(); i++) + for (int j = i + 1; j < strip.size() && (strip[j].second - strip[i].second < minDist); j++) + minDist = min(minDist, dist(strip[i], strip[j])); + + return minDist; +} + +double minDist(int left ,int right) { + + if (right - left + 1 <= 3) + return bruteForce(left, right); + + + int mid = (left + right) / 2; + point midPoint = V[mid]; + double leftDist = minDist(left, mid); + double rightDist = minDist(mid + 1, right); + double localMinDist = min(leftDist, rightDist); + vector strip; + + for (int i = left; i <= right; i++) + if (abs(V[i].first - midPoint.first) < localMinDist) + strip.push_back(V[i]); + + return min(localMinDist, closestOverlapping(strip, localMinDist)); +} + +int main() { + ios_base::sync_with_stdio(0); + cin.tie(NULL); + + fin >> N; + for (int i = 1; i <= N; i++) { + point tmp; + fin >> tmp.first >> tmp.second; + V[i - 1] = tmp; + } + V.resize(N); + sort(V.begin(), V.end()); + + fout << fixed << setprecision(PRECISION) < +#include +#include +#include + + + + +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +int n; + +pair v[100010],w[100010],f[100010]; + +long long dist(pair x, pair y){ + + return (x.first-y.first)*(x.first-y.first)+(x.second-y.second)*(x.second-y.second); + +} + +void intercls(int st, int mid, int dr){ + + int i=st; + + int j=mid+1; + + int k=0; + + while(i<=mid&&j<=dr) + + if(v[i].second>n; + + for(int i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort(v+1,v+n+1); + + fout< +#include +#include + + +using namespace std; + + + +int a[100000001],b[100000001],c[100000001],d[100000001]; + + + +void intercl(int li,int lf) + +{ + + int m=(li+lf)/2,i,j,k; + + i=li;j=m+1;k=li; + + while(i<=m&&j<=lf) + + if(a[i] + + + + +using namespace std; + +typedef long long llong; + + + +struct Punct + +{ + + int x, y; + +} px[100005], py[100005], tmp[100005]; + + + +bool cmpx(const Punct& a, const Punct& b) + +{ + + return a.x < b.x; + +} + + + +bool cmpy(const Punct& a, const Punct& b) + +{ + + return a.y < b.y; + +} + + + +llong dist2(const Punct& a, const Punct& b) + +{ + + llong dx = a.x - b.x; + + llong dy = a.y - b.y; + + return dx * dx + dy * dy; + +} + + + +llong calc(int p, int q) + +{ + + llong res = 0x7fffffffffffffffLL; + + for(int i = p; i < q; i++) + + { + + for(int j = i + 1; j <= q; j++) + + { + + llong aux = dist2(px[i], px[j]); + + if(aux < res) + + res = aux; + + } + + } + + return res; + +} + + + +llong solve(int p, int q) + +{ + + if(q - p < 3) + + return calc(p, q); + + int m = (p + q) / 2; + + llong d1 = solve(p, m); + + llong d2 = solve(m, q); + + llong dd = min(d1, d2); + + + + int k = 0; + + for(int i = p; i <= q; i++) + + { + + llong dist = px[i].x - px[m].x; + + if(dist * dist < dd) + + tmp[k++] = px[i]; + + } + + sort(tmp, tmp + k, cmpy); + + for(int i = 0; i < k; i++) + + { + + + + for(int j = i + 1; + + (tmp[j].y - tmp[i].y) * (tmp[j].y - tmp[i].y) < dd && + + j < k; j++) + + { + + llong dist = dist2(tmp[i], tmp[j]); + + if(dd > dist) + + dd = dist; + + } + + } + + return dd; + +} + + + +int main() + +{ + + int n, i; + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + fin >> n; + + for(i = 0; i < n; i++) + + { + + fin >> px[i].x >> px[i].y; + + py[i] = px[i]; + + } + + sort(px, px + n, cmpx); + + llong res = solve(0, n - 1); + + fout << fixed << setprecision(6) << sqrtl(res); + + return 0; + +} diff --git a/cmap/divide_conquer/be8a785b-1fa1-48c2-95a7-e819047ba72d.cpp b/cmap/divide_conquer/be8a785b-1fa1-48c2-95a7-e819047ba72d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e5352bc30e4e8cd6decfb7391ea578555febdf6 --- /dev/null +++ b/cmap/divide_conquer/be8a785b-1fa1-48c2-95a7-e819047ba72d.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include + + + + + + + +typedef std::pair Point; +FILE *fin = freopen("cmap.in", "r", stdin); FILE *fout = freopen("cmap.out", "w", stdout); + +const long long INF = 1e18; +const long long OFFSET = 8; +int n; +std::vector pts, aux; + + +void read_input() { + scanf("%d", &n); + pts.resize(n); aux.resize(n); + for (int i = 0; i < n; i++) { + scanf("%d%d", &pts[i].first, &pts[i].second); + } +} + + +long long get_dist(Point a, Point b) { + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); +} + + +void merge(int left, int right, int mid) { + int p1 = left, p2 = mid + 1, id = 0; + + while (p1 <= mid && p2 <= right) { + if (pts[p1].second < pts[p2].second) { + aux[id++] = pts[p1++]; + } else { + aux[id++] = pts[p2++]; + } + } + + while (p1 <= mid) { + aux[id++] = pts[p1++]; + } + + while (p2 <= mid) { + aux[id++] = pts[p2++]; + } + + for (int i = 0; i < id; i++) { + pts[left + i] = aux[i]; + } +} + + +long long divide(int left, int right) { + if (right - left < 2) { + if (left == right) { + return INF; + } else { + merge(left, right, left); + return get_dist(pts[left], pts[right]); + } + } + + int mid = (left + right) / 2; + long long dist_1 = divide(left, mid), dist_2 = divide(mid + 1, right); + long long d = std::min(dist_1, dist_2); + + merge(left, right, mid); + for (int i = left; i <= right; i++) { + for (int j = i - 1; j >= left && i - j < OFFSET; j--) { + d = std::min(d, get_dist(pts[i], pts[j])); + } + } + + return d; +} + + +int main(int argc, char const *argv[]) { + read_input(); + + std::sort(pts.begin(), pts.end()); + + printf("%.10f\n", std::sqrt((double)divide(0, n - 1))); + return 0; +} diff --git a/cmap/divide_conquer/bf5d2cfb-ac22-40da-9515-a67126680cc5.cpp b/cmap/divide_conquer/bf5d2cfb-ac22-40da-9515-a67126680cc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bd6719c8414afcf266b25dc275539a5c702f273 --- /dev/null +++ b/cmap/divide_conquer/bf5d2cfb-ac22-40da-9515-a67126680cc5.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +struct Point{ + long long x, y; +}; + + +long double distanta(Point p1, Point p2){ + long long x = (p1.x - p2.x), y = (p1.y - p2.y); + x = x *x; y = y*y; + return sqrt( x + y); +} + + +long double bruteForce(Point P[], long long n){ + long double min = LDBL_MAX; + for (long long i = 0; i < n; ++i) + for (long long j = i+1; j < n; ++j) + if (distanta(P[i], P[j]) < min) + min = distanta(P[i], P[j]); + return min; +} + + +long double min(long double x, long double y){ + return (x < y)? x : y; +} + + +int compareX(const Point &p1, const Point &p2){ + return p1.x < p2.x; +} + +int compareY(const Point &p1, const Point &p2){ + return p1.y < p2.y; +} +long double rezolvaMediana(Point aux[], long long size, long double d){ + long double min = d; + sort(aux, aux + size, compareY); + + + for (long long i = 0; i < size; ++i) + for (long long j = i+1; j < size && (aux[j].y - aux[i].y) < min; ++j) + if (distanta(aux[i],aux[j]) < min) + min = distanta(aux[i], aux[j]); + + return min; +} + + +long double partitionare(Point P[], long long n){ + + if (n <= 3) + return bruteForce(P, n); + + long long mid = n/2; + Point midPoint = P[mid]; + + long double solLeft = partitionare(P, mid), solRight = partitionare(P + mid, n-mid), sol; + sol = min(solLeft, solRight); + + + Point aux[n]; + long long j = 0; + for (long long i = 0; i < n; i++) + if (abs(P[i].x - midPoint.x) < sol) + aux[j] = P[i], j++; + + + return min(sol, rezolvaMediana(aux, j, sol) ); +} + + +long double rezolva(Point P[], int n){ + sort(P, P+n, compareX); + + + return partitionare(P, n); +} + + +int main(){ + Point *P; + int n ; + FILE *fin=fopen("cmap.in","r"); + FILE *fout=fopen("cmap.out","w"); + fscanf(fin , "%d",&n); + P = (Point*) calloc(n, sizeof(Point)); + long long i; + for(i=0;i +#include +#include +#include +#include +#include + + + + +using namespace std; + + + + + + + +int N; + + + +struct pair_compare { + + bool operator() (const std::pair & p1, const std::pair & p2) const { + + if(p1.first == p2.first) + + return (p1.second < p2.second); + + else + + return (p1.first < p2.first); + + } + +}; + + + +vector> P; + + + +long long findpoints(int left,int right){ + + long long dist=LLONG_MAX,dx,dy; + + if((right-left)<=2){ + + for(int i=left;i> Q; + + for(int i=left;i<=right;i++){ + + dx=P[i].first-P[mid].first; dx*=dx; + + if(dxmid && Q[j].second>mid){ + + continue; + + } + + dx=P[Q[i].second].first-P[Q[j].second].first; dx*=dx; + + dy=P[Q[i].second].second-P[Q[j].second].second; dy*=dy; + + if((dx+dy) + + +using namespace std; + + + +const int nmax = 100005; + + + +const long long inf = numeric_limits::max(); + + + +vector> x; + +vector> y; + +int n; + + + +long long dist(pair& a, pair& b) { + + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); + +} + + + +vector > v; + + + +long long calc(int l, int r, vector>& x, vector>& y) { + + if (l >= r - 1) { + + return inf; + + } + + if (l == r - 2) { + + if (y[l].first > y[l + 1].first) { + + swap(y[l], y[l + 1]); + + } + + return dist(y[l], y[l + 1]); + + } + + + + int mid = (l + r) / 2; + + long long best = min(calc(l, mid, x, y), calc(mid, r, x, y)); + + merge(y.begin() + l, y.begin() + mid, y.begin() + mid, y.begin() + r, v.begin()); + + copy(v.begin(), v.begin() + (r - l), y.begin() + l); + + + + int cnt = 0; + + for (int i = l; i < r; i++) { + + if (abs(y[i].second - x[mid].first) <= best) { + + v[cnt++] = y[i]; + + } + + } + + + + for (int i = 0; i < cnt; i++) { + + for (int j = i + 1; j < cnt && j - i < 8; j++) { + + best = min(best, dist(v[i], v[j])); + + } + + } + + + + return best; + +} + + + +int main() { + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + scanf("%d", &n); + + x.resize(n); + + y.resize(n); + + v.resize(n); + + for (int i = 0; i < n; i++) { + + scanf("%d %d", &x[i].first, &x[i].second); + + } + + + + sort(x.begin(), x.end()); + + for (int i = 0; i < n; i++) { + + y[i] = make_pair(x[i].second, x[i].first); + + } + + + + printf("%.6lf\n", sqrt(calc(0, n, x, y))); + + + + return 0; + +} diff --git a/cmap/divide_conquer/c0167e42-6b9f-40b2-bf8d-cbc8818e458b.cpp b/cmap/divide_conquer/c0167e42-6b9f-40b2-bf8d-cbc8818e458b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e30f11ea5028897c335b8d813dbd7652aab6d3f2 --- /dev/null +++ b/cmap/divide_conquer/c0167e42-6b9f-40b2-bf8d-cbc8818e458b.cpp @@ -0,0 +1,154 @@ +#include + + + + + + +using namespace std; + + + +void citire (int &n, vector > &PCT) + +{ + + ifstream f("cmap.in"); + + f >> n; + + for(int i = 0; i < n; i++) + + { + + int first, second; + + f >> first >> second; + + PCT.push_back (make_pair (first, second)); + + } + + f.close(); + +} + + + +long long distanta (pair A, pair B) + +{ + + return (long long)(A.first - B.first) * (A.first - B.first) + (long long)(A.second - B.second) * (A.second - B.second); + +} + + + +bool cmp (pair A, pairB) + +{ + + return A.second < B.second; + +} + + + +long long di (int st, int dr, vector > &PCT) + +{ + + if(dr - st == 1) + + return distanta (PCT[st], PCT[dr]); + + else if(dr - st == 2) + + { + + long long dist1 = distanta (PCT[st], PCT[st+1]); + + long long dist2 = distanta (PCT[st], PCT[st+2]); + + long long dist3 = distanta (PCT[st+1], PCT[st+2]); + + + + long long minim = dist1; + + if(dist2 < minim) + + minim = dist2; + + if(dist3 < minim) + + minim = dist3; + + return minim; + + } + + + + int m = (st + dr) / 2; + + long long dist = min (di(st, m, PCT), di(m + 1, dr, PCT)); + + int delta = (int) (ceil(sqrt(dist))); + + + + vector> temp; + + for(unsigned i = st; i <= dr; i++) + + if(abs(PCT[i].first - PCT[m].first) <= delta) + + temp.push_back(PCT[i]); + + sort(temp.begin(), temp.end(), cmp); + + + + for(unsigned i = 0; i < temp.size(); i++) + + for(unsigned j = i + 1; j <= i + 7 && j < temp.size(); j++) + + dist = min(dist, distanta (temp[i], temp[j])); + + + + return dist; + +} + + + +int main() + +{ + + int n; + + vector > PCT; + + + + citire(n, PCT); + + sort(PCT.begin(), PCT.end()); + + + + ofstream g("cmap.out"); + + g << fixed << setprecision(6) << sqrt(di(0, n - 1, PCT)); + + g.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/c1b44f36-63f5-46ad-8de2-57d10865b80f.cpp b/cmap/divide_conquer/c1b44f36-63f5-46ad-8de2-57d10865b80f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5316fde4432620f57a387d4863f308a445dcf9a --- /dev/null +++ b/cmap/divide_conquer/c1b44f36-63f5-46ad-8de2-57d10865b80f.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + assert(in >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + assert(in >> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/c1bf9392-fc1e-4ac8-995e-24646624461c.cpp b/cmap/divide_conquer/c1bf9392-fc1e-4ac8-995e-24646624461c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4120c9b1f3da452090ff9104a4313f6f80d65153 --- /dev/null +++ b/cmap/divide_conquer/c1bf9392-fc1e-4ac8-995e-24646624461c.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long long int ll; +typedef long double ld; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const ll INF = 1e18; + +vector < pair < ll, ll > > points; +vector < pair < ll, ll > > strip; + +ll getDistance(const pair < ll, ll > &a, const pair < ll, ll > &b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} +ll Solve(int s, int e) { + if(e - s <= 3) { + ll dist = INF; + for(int i = s; i < e; ++i) { + for(int j = i + 1; j < e; ++j) { + dist = min(dist, getDistance(points[i], points[j])); + } + } + return dist; + } + int mid = (s + e) / 2; + ll dist = min(Solve(s, mid), Solve(mid, e)); + + int idx = 0; + for(int i = s; i < e; ++i) { + if(abs(points[mid].first - points[i].first) < dist) { + strip[idx++] = points[i]; + } + } + + sort(strip.begin(), strip.begin() + idx, [&](const pair < ll, ll > &a, const pair < ll, ll > &b) { + return a.second < b.second; + }); + + for(int i = 0; i < idx; ++i) { + for(int j = i + 1; j < idx && j - i <= 7; ++j) { + dist = min(dist, getDistance(strip[i], strip[j])); + } + } + return dist; +} + +int main() { + int n; fin >> n; + points.resize(n); strip.resize(n); + for(int i = 0; i < n; ++i) { + fin >> points[i].first >> points[i].second; + } + + sort(points.begin(), points.end()); + fout << fixed << setprecision(10) << sqrtl((ld)Solve(0, n)); + return 0; +} diff --git a/cmap/divide_conquer/c1f44503-f382-4dfb-8818-f1662551a892.cpp b/cmap/divide_conquer/c1f44503-f382-4dfb-8818-f1662551a892.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d10ab773d183fc968564f0127f3826d1e132d04a --- /dev/null +++ b/cmap/divide_conquer/c1f44503-f382-4dfb-8818-f1662551a892.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +struct punct{ + + int x, y; + +}; + + + +bool cmpx(punct p1, punct p2) + +{ + + return (p1.x < p2.x); + +} + + + +bool cmpy(punct p1, punct p2) + +{ + + return (p1.y < p2.y); + +} + + + +double dist(punct p1, punct p2) + +{ + + double dist = sqrt( pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2) ); + + return dist; + +} + + + +double f1(punct p[], int n) + +{ + + int i, j; + + double d = 99999999; + + + + if(n <= 3) + + { + + for(i = 0; i < n; i++) + + for(j = i+1; j < n; j++) + + if(dist(p[i], p[j]) < d) + + d = dist(p[i], p[j]); + + return d; + + } + + + + int m = (n/2); + + double rs = f1(p, m); + + double rd = f1(p + m, n - m); + + + + if(rs < rd) + + d = rs; + + else + + d = rd; + + + + punct Y[n]; + + j = 0; + + for (i = 0; i < n; i++) + + if (abs(p[i].x - p[m].x) < d) + + { + + Y[j] = p[i]; + + j++; + + } + + sort(Y, Y+j, cmpy); + + + + for(i = 0; i < j; i++) + + for(int k = i+1; k < j && ((Y[k].y - Y[i].y) < d); k++) + + if(dist(Y[i], Y[k]) < d) + + d = dist(Y[i], Y[k]); + + + + return d; + +} + + + +int main() + +{ + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + int n; + + f>>n; + + punct p[n]; + + for(int i = 0; i < n; i++) + + f >> p[i].x >> p[i].y; + + + + sort(p, p+n, cmpx); + + + + double r = f1(p, n); + + + + g< + + + + + + +using namespace std; + + + +const int MAXN = 1e5; + +const long long INF = 2000LL * 1000 * 1000 * 2000 * 1000 * 1000; + +typedef pair < int, int > Point; + + + +Point xsort[MAXN], ysort[MAXN], aux[MAXN]; + + + +inline long long dsqr(Point A, Point B) { + + return 1LL * (A.first - B.first) * (A.first - B.first) + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long solve(int l, int r) { + + if (l >= r) + + return INF; + + if (l + 1 == r) { + + if (ysort[l] > ysort[r]) + + swap(ysort[l], ysort[r]); + + return dsqr(ysort[l], ysort[r]); + + } + + int mid = (l + r) / 2, k = 0, i = l, j = mid + 1; + + long long lim = min(solve(l, mid), solve(mid + 1, r)); + + while (i <= mid && j <= r) + + if (ysort[i] < ysort[j]) + + aux[k++] = ysort[i++]; + + else + + aux[k++] = ysort[j++]; + + while (i <= mid) + + aux[k++] = ysort[i++]; + + while (j <= r) + + aux[k++] = ysort[j++]; + + for (int ind = l; ind <= r; ++ind) + + ysort[ind] = aux[ind - l]; + + k = 0; + + for (int ind = l; ind <= r; ++ind) + + if (abs(ysort[ind].second - xsort[mid].first) <= lim) + + aux[k++] = ysort[ind]; + + for (i = 0; i < k; ++i) + + for (j = i + 1; j < min(k, i + 8); ++j) + + lim = min(lim, dsqr(aux[i], aux[j])); + + return lim; + +} + + + +int main() + +{ + + int n; + + ifstream fin("cmap.in"); + + fin >> n; + + for (int i = 0; i < n; ++i) + + fin >> xsort[i].first >> xsort[i].second; + + fin.close(); + + sort(xsort, xsort + n); + + for (int i = 0; i < n; ++i) + + ysort[i] = {xsort[i].second, xsort[i].first}; + + ofstream fout("cmap.out"); + + fout << setprecision(8) << fixed << sqrt(solve(0, n - 1)) << '\n'; + + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/c2fccabc-8cb4-4303-897e-945d25cd8119.cpp b/cmap/divide_conquer/c2fccabc-8cb4-4303-897e-945d25cd8119.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a86f2ea0fc55354d42f7b81378d502c5394c81d --- /dev/null +++ b/cmap/divide_conquer/c2fccabc-8cb4-4303-897e-945d25cd8119.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +using namespace std; + +typedef long double db; + +typedef long long ll; + + + +ll n; + +pair a[100010],vp[100010],va[100010]; + + + +db dis(pair p1, pair p2){ + + return sqrt((p1.first-p2.first)*(p1.first-p2.first)+(p1.second-p2.second)*(p1.second-p2.second)); + +} + + + +bool cmp(pair p1, pair p2){ + + return p1.second=0?nr:-nr); + +} + + + +void interclaseaza(ll st,ll dr,ll mij){ + + for(ll i=1,i1=st,i2=mij+1;i<=dr-st+1;i++){ + + if(i1==mij+1){ + + va[i]=vp[i2++]; + + continue; + + } + + if(i2==dr+1){ + + va[i]=vp[i1++]; + + continue; + + } + + if(cmp(vp[i1],vp[i2]))va[i]=vp[i1++]; + + else va[i]=vp[i2++]; + + } + + for(ll i=1;i<=dr-st+1;i++) + + vp[st+i-1]=va[i]; + +} + + + +db rez(ll st,ll dr){ + + if(dr-st+1<=3){ + + db ansa; + + ansa=dis(a[st],a[st+1]); + + if(dr-st+1==3) + + ansa=min(ansa,min(dis(a[st],a[dr]),dis(a[dr],a[dr-1]))); + + sort(vp+st,vp+dr+1,cmp); + + return ansa; + + } else { + + ll mij=(st+dr)/2; + + db ans1=rez(st,mij); + + db ans2=rez(mij+1,dr); + + db ansa=min(ans1,ans2); + + ll drd=a[mij].first; + + interclaseaza(st,dr,mij); + ll sz=0; + + for(ll i=st;i<=dr;i++) + + if(mod(vp[i].first-drd)<=ansa)va[++sz]=vp[i]; + + + + for(ll i=1;i<=sz;i++) + + for(ll j=i+1;j<=i+7&&j<=sz;j++) + + ansa=min(ansa,dis(va[i],va[j])); + + return ansa; + + } + +} + + + +int main() + +{ + + ifstream f ("cmap.in"); + + ofstream g ("cmap.out"); + + f>>n; + + for(ll i=1;i<=n;i++) + + f>>a[i].first>>a[i].second, + + vp[i]=a[i]; + + sort(a+1,a+n+1); sort(vp+1,vp+n+1); + + g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct punct + +{ + + int x,y; + +}v[100000],v2[100000]; + + + +int n; + + + +bool sortare_x (punct a,punct b) + +{ + + return a.x>n; + + for(i=0;i>v[i].x>>v[i].y; + + sort(v,v+n,sortare_x); + + g < + +using namespace std; + + + + + +int const N = 100010; +long long const oo = ((long long )1 << 60); + +pair puncte[N], vec[N]; +int n; + + +long long dist( pair A, pair B) +{ + return ((long long )B.first - A.first) * (B.first-A.first) + ((long long ) B.second - A.second) * (B.second - A.second); +} + +long long dETi(int st, int dr) +{ + if(st == dr) + return oo; + if(st == dr - 1) + return dist(puncte[st], puncte[dr]); + + int mij = (st + dr)/2, k = 0, i, j; + long long x = dETi(st, mij); + long long y = dETi(mij + 1, dr); + long long mini = min(x, y); + + for(i = st; i <= dr; i++) + if(abs(puncte[i].first - puncte[mij].first) < mini) + vec[++k] = {puncte[i].second, puncte[i].first}; + + sort(vec + 1, vec + k + 1); + + for(i = 1; i < k; i++) + for(j = i + 1; j <= k && j <= i + 8; j++) + mini = min(mini, dist(vec[i], vec[j])); + + return mini; +} + + +int main() +{ + ifstream in("cmap.in"); + ofstream out("cmap.out"); + + + + int i, x, y; + + in >> n; + + for(i=1; i<=n; i++){ + in >> x >> y; + puncte[i] = make_pair(x, y); + } + + sort(puncte + 1, puncte + n + 1); + + in.close(); + + out << fixed << setprecision(6) << sqrt(dETi(1, n)) << "\n"; + + out.close(); + + return 0; +} diff --git a/cmap/divide_conquer/c4dae3e3-c064-4456-bf21-81299482290c.cpp b/cmap/divide_conquer/c4dae3e3-c064-4456-bf21-81299482290c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fbc028d2a6a719abe6bdc60129b315b5af868dd --- /dev/null +++ b/cmap/divide_conquer/c4dae3e3-c064-4456-bf21-81299482290c.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + assert(in >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + assert(in >> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/c5e76d8b-9048-4cb9-8418-e08248a57206.cpp b/cmap/divide_conquer/c5e76d8b-9048-4cb9-8418-e08248a57206.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db53dc10f0f51d65e16fd31ba163787299a7f852 --- /dev/null +++ b/cmap/divide_conquer/c5e76d8b-9048-4cb9-8418-e08248a57206.cpp @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct point { + + long long x, y; + +}; + + + +vector points, ypoints, selectedpoints; + + + +bool comp (const point &a, const point &b) { + + if (a.x < b.x) + + return 0; + + if (a.x > b.x) + + return 1; + + + + if (a.y < b.y) + + return 0; + + if (a.y > b.y) + + return 1; + + + + return 0; + +} + + + + + +long long euclid_distance(const point &a, const point &b) { + + return (a.x - b. x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); + +} + + + + + +long long min(long long a, long long b) { + + return a < b? a:b; + +} + + + + + + + +long long divide_et_impera(int left, int right) { + + if (right - left <= 2) { + + if (ypoints[left].x > ypoints[left + 1].x || (ypoints[left].x == ypoints[left + 1].x && ypoints[left].y > ypoints[left + 1].y)) { + + point aux = ypoints[left]; + + ypoints[left] = ypoints[left + 1]; + + ypoints[left + 1] = aux; + + } + + + + long long res = euclid_distance(points[left], points[right]); + + if (right - left == 1) + + return res; + + + + return min(res, min(euclid_distance(points[left], points[left + 1]), euclid_distance(points[left+1], points[right]))); + + } + + + + int mid = (left + right) / 2; + + long long left_res = divide_et_impera(left, mid); + + long long right_res = divide_et_impera(mid+ 1, right); + + + + long long current_res = min(right_res, left_res); + + long long midx = points[(left + right) / 2].x; + + + + merge(ypoints.begin() + left, ypoints.begin() + mid + 1, ypoints.begin() + mid + 1, ypoints.begin() + right + 1, selectedpoints.begin(), comp); + + copy(selectedpoints.begin(), selectedpoints.begin() + (right - left + 1), ypoints.begin() + left); + + + + int selectedpointsno = 0; + + for(int i = left; i<= right; ++i) + + if (abs(ypoints[i].y - midx) <= current_res) + + selectedpoints[selectedpointsno++] = ypoints[i]; + + + + for(int i=0; i> n; + + points.resize(n); + + selectedpoints.resize(n); + + for(int i=0; i> points[i].x >> points[i].y; + + + + sort(points.begin(), points.end(), comp); + + ypoints.resize(n); + + for(int i=0; i + +using namespace std; + +ifstream fin ("cmap.in"); ofstream fout ("cmap.out"); + + + + +const int nmax = 1e5; + +pair v[nmax + 1], p[nmax + 1]; + +double dist (pair a, pair b) { + return sqrt((double)(a.first - b.first) * (a.first - b.first) + (double)(a.second - b.second) * (a.second - b.second)); +} + +bool cmp (pair a, pair b) { + return a.second < b.second; +} + +double solve (int st, int dr) { + if (dr - st + 1 <= 3) { + sort(v + st, v + dr + 1, cmp); + + double ans = INFINITY; + for (int i = st; i <= dr; ++ i) + for (int j = i + 1; j <= dr; ++ j) + ans = min(ans, dist(v[i], v[j])); + return ans; + } + + int mij = (st + dr) / 2; + int Xmij = v[mij].first; + double ans = min(solve(st, mij), solve(mij + 1, dr)); + + + int ind = st; + int i = st, j = mij + 1; + while (i <= mij && j <= dr) { + if (v[i].second < v[j].second) + p[ind ++] = v[i ++]; + else + p[ind ++] = v[j ++]; + } + + for (; i <= mij; ++ i) p[ind ++] = v[i]; + for (; j <= dr; ++ j) p[ind ++] = v[j]; + + for (int i = st; i <= dr; ++ i) + v[i] = p[i]; + + + ind = 0; + for (int i = st; i <= dr; ++ i) + if (fabs(v[i].first - Xmij) < ans) + p[ind ++] = v[i]; + + + for (int i = 0; i < ind; ++ i) + for (int j = i + 1; j <= i + 7 && j < ind; ++ j) + ans = min(ans, dist(p[i], p[j])); + return ans; +} + +int main () { + int n; + fin >> n; + + for (int i = 1; i <= n; ++ i) + fin >> v[i].first >> v[i].second; + + fout << setprecision(8) << fixed; + + sort(v + 1, v + n + 1); + fout << solve(1, n) << "\n"; + + fin.close(); + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/c6552deb-505d-458a-b04a-4b2c8e0c9e35.cpp b/cmap/divide_conquer/c6552deb-505d-458a-b04a-4b2c8e0c9e35.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ae26d970a2038838fb2d7d72bdc0f036ce03028 --- /dev/null +++ b/cmap/divide_conquer/c6552deb-505d-458a-b04a-4b2c8e0c9e35.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +using namespace std; + +pair p[100005], c[100005]; +int n; +long long inf; + +long long solve(int l, int r); +bool cmp(pair a, pair b); +long long dist2(pair a, pair b); +void merge(int l, int m, int r); + +int main() { + freopen("cmap.in", "r", stdin); + freopen("cmap.out", "w", stdout); + cin >> n; + for (int i = 0; i < n; ++i) { + cin >> p[i].first >> p[i].second; + } + inf = dist2(p[0], p[1]); + sort(p, p + n); + printf("%.8lf", sqrt(solve(0, n - 1))); +} + +long long solve(int l, int r) { + int uy = 0; + long long minim = inf; + if (r - l < 3) { + for (int i = l; i < r; ++i) { + for (int j = i + 1; j <= r; ++j) { + minim = min(minim, dist2(p[i], p[j])); + } + } + sort(p + l, p + r + 1, cmp); + + return minim; + } + int m = (l + r) / 2; + long long d1 = solve(l, m), d2 = solve(m + 1, r); + if (d1 > d2) { + d1 = d2; + } + minim = d1; + merge(l, m, r); + + for (int i = l; i <= r; ++i) { + if ((p[i].first - p[m].first) * (p[i].first - p[m].first) <= d1) { + c[uy++] = p[i]; + } + } + for (int i = 0; i < uy; ++i) { + for (int j = i + 1; j < uy && j <= i + 7; ++j) { + minim = min(minim, dist2(c[i], c[j])); + } + } + + return minim; +} + +bool cmp(pair a, pair b) { + if (a.second < b.second || (a.second == b.second && a.first < b.first)) { + return 1; + } + return 0; +} + +long long dist2(pair a, pair b) { + return 1ll * (a.first - b.first) * (a.first - b.first) + 1ll * (a.second - b.second) * (a.second - b.second); +} + +void merge(int l, int m, int r) { + int k = -1, i, j; + for (i = l, j = m + 1; i <= m && j <= r;) { + if (cmp(p[i], p[j])) { + c[++k] = p[i++]; + } else { + c[++k] = p[j++]; + } + } + while (i <= m) { + c[++k] = p[i++]; + } + while (j <= r) { + c[++k] = p[j++]; + } + for (i = r; i >= l; --i) { + p[i] = c[k--]; + } +} diff --git a/cmap/divide_conquer/c68a8872-c539-4d96-9a94-f1a9f65236fb.cpp b/cmap/divide_conquer/c68a8872-c539-4d96-9a94-f1a9f65236fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3d2079f2622cb692a9c1db22b1f0ab9ada93c59 --- /dev/null +++ b/cmap/divide_conquer/c68a8872-c539-4d96-9a94-f1a9f65236fb.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct Point{ + long long x, y; +}; + + +long double distanta(Point p1, Point p2){ + long long x = (p1.x - p2.x), y = (p1.y - p2.y); + x = x *x; y = y*y; + return sqrt( x + y); +} + + +long double bruteForce(Point P[], long long n){ + long double min = LDBL_MAX; + for (long long i = 0; i < n; ++i) + for (long long j = i+1; j < n; ++j) + if (distanta(P[i], P[j]) < min) + min = distanta(P[i], P[j]); + return min; +} + + +long double min(long double x, long double y){ + return (x < y)? x : y; +} + + +int compareX(const Point &p1, const Point &p2){ + return p1.x < p2.x; +} + +int compareY(const Point &p1, const Point &p2){ + return p1.y < p2.y; +} +long double rezolvaMediana(Point aux[], long long size, long double d){ + long double min = d; + sort(aux, aux + size, compareY); + + + for (long long i = 0; i < size; ++i) + for (long long j = i+1; j < size && (aux[j].y - aux[i].y) < min; ++j) + if (distanta(aux[i],aux[j]) < min) + min = distanta(aux[i], aux[j]); + + return min; +} + + +long double partitionare(Point P[], long long n){ + + if (n <= 3) + return bruteForce(P, n); + + long long mid = n/2; + Point midPoint = P[mid]; + + long double solLeft = partitionare(P, mid), solRight = partitionare(P + mid, n-mid), sol; + sol = min(solLeft, solRight); + + + Point aux[n]; + long long j = 0; + for (long long i = 0; i < n; i++) + if (abs(P[i].x - midPoint.x) < sol) + aux[j] = P[i], j++; + + + return min(sol, rezolvaMediana(aux, j, sol) ); +} + + +long double rezolva(Point P[], int n){ + sort(P, P+n, compareX); + + + return partitionare(P, n); +} + + +int main(){ + Point *P; + int n ; + FILE *fin=fopen("cmap.in","r"); + FILE *fout=fopen("cmap.out","w"); + fscanf(fin , "%d",&n); + P = (Point*) calloc(n, sizeof(Point)); + long long i; + for(i=0;i +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +pair < int, int > temp[100003]; + + + +int partition_x(pair < int, int > P[100003], int lo, int hi){ + + int pivot = lo + (rand() % (hi - lo + 1)); + + (P[0] = (P[pivot]), (P[pivot]) = (P[hi]), (P[hi]) = P[0]); + + pivot = hi; + + while(lo < pivot){ + + if(P[lo].first >= P[pivot].first){ + + (P[0] = (P[lo]), (P[lo]) = (P[pivot - 1]), (P[pivot - 1]) = P[0]); + + (P[0] = (P[pivot - 1]), (P[pivot - 1]) = (P[pivot]), (P[pivot]) = P[0]); + + -- pivot; + + } + + else{ + + ++ lo; + + } + + } + + return pivot; + +} + + + +void qsort_x(pair < int, int > P[100003], int lo, int hi){ + + if(lo >= hi){ + + return; + + } + + int pivot = partition_x(P, lo, hi); + + qsort_x(P, lo, pivot - 1); + + qsort_x(P, pivot + 1, hi); + + return; + +} + + + +bool cmp_y(pair < int, int > a, pair < int, int > b){ + + return a.second < b.second; + +} + + + +long long closest_dist(pair < int, int > P[100003], int lo, int hi){ + + if(hi - lo == 0){ + + return 1e18; + + } + + if(hi - lo == 1){ + + if(P[lo].second > P[hi].second){ + + (P[0] = (P[lo]), (P[lo]) = (P[hi]), (P[hi]) = P[0]); + + } + + return (1ll * (1ll *(P[lo].first - P[hi].first) * (P[lo].first - P[hi].first) + 1ll *(P[lo].second - P[hi].second) * (P[lo].second - P[hi].second))); + + } + + int mid = (lo + hi) / 2, + + xmid = P[mid].first; + + long long left = closest_dist(P, lo, mid), + + right = closest_dist(P, mid + 1, hi), + + d = ((left) < (right) ? (left) : (right)); + + merge(P + lo, P + mid + 1, P + mid + 1, P + hi + 1, temp, cmp_y); + + copy(temp, temp + hi - lo + 1, P + lo); + + int nl = 0, nr = 0; + + for(int i = 0, n = hi - lo + 1; i < n; i ++){ + + P[lo + i] = temp[i]; + + if((1ll * (xmid - temp[i].first) * (xmid - temp[i].first)) < d){ + + temp[nr ++] = temp[i]; + + } + + } + + + + for(int i = 0; i < nr; i ++) + + for(int j = i + 1; j < nr && (1ll * (temp[i].second - temp[j].second) * (temp[i].second - temp[j].second)) < d; j ++) + + d = min(d, (1ll * (1ll *(temp[i].first - temp[j].first) * (temp[i].first - temp[j].first) + 1ll *(temp[i].second - temp[j].second) * (temp[i].second - temp[j].second)))); + + return d; + +} + + + +int main(){ + + int n; + + pair < int, int > P[100003]; + + fin>>n; + + for(int i = 1; i <= n; i ++){ + + fin>>P[i].first>>P[i].second; + + } + + qsort_x(P, 1, n); + + fout< +#include +#include +#include + + +using namespace std; + +fstream f1("cmap.in", ios::in); + +fstream f2("cmap.out", ios::out); + +struct punct + +{ + + double x, y; + +}p[100005], aux, strip[100005]; + +int n, nrpct; + +double dist (punct p1, punct p2) + +{ + + return sqrt((p1.x-p2.x)*(p1.x-p2.x)+ (p1.y-p2.y)*(p1.y-p2.y)); + +} + +void quick(int inf, int sup) + +{ + + int i, j; + + punct p; + + i = inf; + + j = sup; + + p = strip[(i + j) / 2]; + + do + + { + + while ( (i < sup) && (strip[i].x< p.x) ) i++; + + while ( (j > inf) && (strip[j].x> p.x) ) j--; + + if (i<= j) + + { + + aux=strip[i]; strip[i]=strip[j]; strip[j]=aux; + + i++; + + j--; + + } + + } while (i <= j); + + + + if (inf < j) quick(inf, j); + + if (i < sup) quick(i, sup); + +} + +double minim(double a, double b) + +{ + + return (a> b ? b: a); + +} + +double brute_force(int st, int dr) + +{ + + if(st==dr) return 0; + + else if(st+1==dr) return dist(p[st], p[dr]); + + else return minim(dist(p[st], p[st+1]), minim(dist(p[st], p[dr]), dist(p[st+1], p[dr]) )); + +} + +void cre_sort_strip(int st, int dr, int val, double dmin) + +{ + + int i; + + nrpct=0; + + for(i=st; i<=dr; i++) + + if(fabs(p[i].x- val)<= dmin) {nrpct++; strip[nrpct]=p[i];} + quick(1, nrpct); + +} + +double dist_strip(double d) + +{ + + double dis=d, temp; + + int i,j; + + for(i=1; i>n; + + for(i=1; i<=n; i++) + + f1>>p[i].x>>p[i].y; + + for(gap=n/2; gap>=1; gap/=2) + + for(i=1+gap; i<=n; i++) + + { + + aux=p[i]; + + for(j=i-gap; (j>=1)&&((p[j].x> aux.x)||((p[j].x== aux.x)&&(p[j].y> aux.y))); j-=gap) + + p[j+gap]=p[j]; + + p[j+gap]=aux; + + } + +} + +int main() + +{ + + citire(); + + f2< + +using namespace std; + +typedef long double ld; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const ld INF = 1e13; + +vector < pair < ld, ld > > points; +vector < pair < ld, ld > > strip; + +ld getDistance(const pair < ld, ld > &a, const pair < ld, ld > &b) { + return sqrtl((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); +} +ld Solve(int s, int e) { + if(e - s <= 3) { + ld dist = INF; + for(int i = s; i < e; ++i) { + for(int j = i + 1; j < e; ++j) { + dist = min(dist, getDistance(points[i], points[j])); + } + } + return dist; + } + int mid = (s + e) / 2; + ld dist = min(Solve(s, mid), Solve(mid, e)); + + int idx = 0; + for(int i = s; i < e; ++i) { + if(abs(points[mid].first - points[i].first) < dist) { + strip[idx++] = points[i]; + } + } + + sort(strip.begin(), strip.begin() + idx, [&](const pair < ld, ld > &a, const pair < ld, ld > &b) { + return a.second < b.second; + }); + + for(int i = 0; i < idx; ++i) { + for(int j = i + 1; j < idx && (strip[j].second - strip[i].second) < dist; ++j) { + dist = min(dist, getDistance(strip[i], strip[j])); + } + } + return dist; +} + +int main() { + int n; fin >> n; + points.resize(n); strip.resize(n); + for(int i = 0; i < n; ++i) { + fin >> points[i].first >> points[i].second; + } + + sort(points.begin(), points.end()); + fout << fixed << setprecision(10) << Solve(0, n); + return 0; +} diff --git a/cmap/divide_conquer/caea4929-f07d-4b50-9f60-9da7acdcd211.cpp b/cmap/divide_conquer/caea4929-f07d-4b50-9f60-9da7acdcd211.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05b88c6b20b64081aeec9d2bdf359d612d56a3f1 --- /dev/null +++ b/cmap/divide_conquer/caea4929-f07d-4b50-9f60-9da7acdcd211.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +#include +#include +typedef long long int int64; + + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int64 lookup = 7; + + + +bool cmpX(pair &a, pair &b) { + + if (a.first < b.first) + + return true; + + return false; + +} + + + +bool cmpY(pair &a, pair &b) { + + if (a.second < b.second) + + return true; + + return false; + +} + + + +int64 get_dist(pair &a, pair &b) { + + return (b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second); + +} + + + +inline int64 square(int64 a) { + + return a * a; + +} + + + +int64 cmap(vector > &pct, int left, int right) { + + if (right - left + 1 <= 3) { + + int64 d1, d2, d3; + + d1 = get_dist(pct[0], pct[1]); + + d2 = get_dist(pct[1], pct[2]); + + d3 = get_dist(pct[0], pct[2]); + + if (d1 <= d2 && d1 <= d3) + + return d1; + + if (d2 <= d1 && d2 <= d3) + + return d2; + + return d3; + + } + + int mid = left + (right - left) / 2; + + int64 d1, d2; + + d1 = cmap(pct, left, mid); + + d2 = cmap(pct, mid, right); + + if (d1 > d2) + + d1 = d2; + + + + vector > val_pct; + + int64 midx = pct[mid].first; + + val_pct.push_back(pct[mid]); + + + + int search_left = mid - 1, search_right = mid + 1; + + while (search_left >= left && square(midx - pct[search_left].first) < d1) { + + val_pct.push_back(pct[search_left]); + + search_left--; + + } + + while (search_right <= right && square(pct[search_right].first - midx) < d1) { + + val_pct.push_back(pct[search_right]); + + search_right++; + + } + + + + sort(val_pct.begin(), val_pct.end(), cmpY); + + + + for (int i = 0; i < val_pct.size(); i++) { + + for (int j = i + 1; j < val_pct.size() && j - i <= lookup; j++) { + + d2 = get_dist(val_pct[i], val_pct[j]); + + if (d1 > d2) { + + d1 = d2; + + continue; + + } + + if (square(val_pct[j].second - val_pct[i].second) >= d1) + + break; + + } + + } + + + + return d1; + +} + + + +int main() { + + int64 n; + + in >> n; + + vector > pct(n); + + for (int64 i = 0; i < n; i++) { + + in >> pct[i].first >> pct[i].second; + + } + + sort(pct.begin(), pct.end(), cmpX); + + out << setprecision(20) << sqrt(cmap(pct, 0, n - 1)) << '\n'; + + return 0; + +} diff --git a/cmap/divide_conquer/cb74fa9c-ab51-4281-bae3-cce9dca58ba7.cpp b/cmap/divide_conquer/cb74fa9c-ab51-4281-bae3-cce9dca58ba7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..580460d0393c7ee06432de98ea41d1e653c0c1fc --- /dev/null +++ b/cmap/divide_conquer/cb74fa9c-ab51-4281-bae3-cce9dca58ba7.cpp @@ -0,0 +1,109 @@ +#include +long long dist2(const std::pair & p0, const std::pair & p1) { + + return (p0.second - p1.second) * (p0.second - p1.second) + (p0.first - p1.first) * (p0.first - p1.first); + +} +int N; + + + + + + + +std::vector > V(100005); + +std::vector > A, B; + + + +long long DivideEtImpera(int Left = 0, int Right = N, std::vector >& A = A, std::vector >& B = B) { + + if (Left+1 >= Right) return 40000000000000005; + + if (Left+2 == Right) { + + if (B[Left] > B[Left+1]) + + std::swap(B[Left], B[Left+1]); + + return dist2(A[Left], A[Left+1]); + + } long long Min = std::min(DivideEtImpera(Left, (Left+Right)/2, A, B), DivideEtImpera((Left+Right)/2, Right, A, B)); + + + + std::merge(B.begin() + Left, B.begin() + (Left+Right)/2, B.begin() + (Left+Right)/2, B.begin() + Right, V.begin()); + + std::copy(V.begin(), V.begin() + (Right-Left), B.begin() + Left); + + + + int replace = 0; + + for (int i=Left, j; i> N; + + for (int i=0, X, Y; i> X >> Y, A.push_back({X, Y}); + +} + + + +void Rezolvare() { + + std::sort(A.begin(), A.end()); + + for (int i=0; i + + + + +using namespace std; + +typedef long long ll; +typedef pair< int , int > PII; + +int n, m, cnt; +PII a[100100]; + +double dist(PII a, PII b) { + return sqrt(1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second)); +} + +double divide(int st, int dr) { + if (dr - st <= 2) { + double d = 1e18; + for (int i = st; i < dr; i++){ + for (int j = i + 1; j <= dr; j++){ + d = min(d, dist(a[i], a[j])); + } + } + return d; + } + + int mid = (st + dr) >> 1; + double d = min(divide(st, mid), divide(mid + 1, dr)); + + vector < PII > V; + for (int i = st; i <= dr; i++) { + if (abs(a[i].first - a[mid].first) < d) V.push_back(a[i]); + } + + sort(V.begin(), V.end(), [&](PII a, PII b){ return a.second > b.second; }); + + for (int i = 0; i < V.size(); i++) + for (int j = i + 1; j < V.size() && j - i <= 7; j++) + d = min(d, dist(V[i], V[j])); + + return d; +} + +int main() { + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i].first >> a[i].second; + } + + sort(a + 1, a + n + 1); + cout << setprecision(6) << fixed << divide(1, n); + + return 0; +} diff --git a/cmap/divide_conquer/ccca5897-5830-4810-91fb-96240984e4a0.cpp b/cmap/divide_conquer/ccca5897-5830-4810-91fb-96240984e4a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3338d940aaab08b296023bc65421b84a73be7dce --- /dev/null +++ b/cmap/divide_conquer/ccca5897-5830-4810-91fb-96240984e4a0.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + + + + + + +typedef std::pair Point; +const int OFFSET = 8; +const double INF = 1e18; + +std::vector points, aux; + +std::ifstream fin("cmap.in"); std::ofstream fout("cmap.out"); + + +long long get_dist(Point a, Point b) { + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); +} + + +void merge(int left, int right, int mid) { + int p1 = left, p2 = mid + 1, id = 0; + + while (p1 <= mid && p2 <= right) { + if (points[p1].second < points[p2].second) { + aux[id++] = points[p1++]; + } else { + aux[id++] = points[p2++]; + } + } + + while (p1 <= mid) { + aux[id++] = points[p1++]; + } + while (p2 <= right) { + aux[id++] = points[p2++]; + } + + for (int i = 0; i < id; i++) { + points[left + i] = aux[i]; + } +} + +long long divide(int left, int right) { + if (right - left < 2) { + if (left == right) { + return INF; + } else { + merge(left, right, left); + return get_dist(points[left], points[right]); + } + } + + int mid = (left + right) / 2; + long long dist = std::min(divide(left, mid), divide(mid + 1, right)); + merge(left, right, mid); + + for (int i = left; i <= right; i++) { + for (int j = i - 1; j >= left && i - j < OFFSET; j--) { + dist = std::min(dist, get_dist(points[i], points[j])); + } + } + + return dist; +} + + +int main() { + int n; fin >> n; + points.resize(n); aux.resize(n); + + for (auto& point: points) { + fin >> point.first >> point.second; + } + std::sort(points.begin(), points.end()); + + fout << std::fixed << std::setprecision(10) << std::sqrt((double)divide(0, n - 1)) << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/ccea2f06-f465-4fdd-aafa-12de51b565db.cpp b/cmap/divide_conquer/ccea2f06-f465-4fdd-aafa-12de51b565db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e7f980c29be5c1bb6fcf454333b571b8ce01986 --- /dev/null +++ b/cmap/divide_conquer/ccea2f06-f465-4fdd-aafa-12de51b565db.cpp @@ -0,0 +1,232 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream be("cmap.in"); + +ofstream ki("cmap.out"); + + + +const long long inf=1e10; + + + +struct point{ + +long long x,y; + +}; + + + +bool xComp(const point& a, const point& b) { + + if(a.x==b.x) return a.y&p,int n) + +{ + + long double min1=inf; + + for(int i=0;i&strip,int n,long double d) + +{ + + long double min=d; + + for(int i=0;i&px,vector&py) + +{ + + if(n>3) + + { + + int m=n/2; + + point m_point=px[m]; + + vectorpyl(m); + + vectorpyr(n-m); + + int l=0,r=0; + + vectorpxl(m); + + vectorpxr(n-m); + + for(int i=0;i=px[i].x) + + pxl[l++]=px[i]; + + else pxr[r++]=px[i]; + + } + + long double minl,minr; + + minl=cmap(m,pxl,pyl); + + minr=cmap(n-m,pxr,pyr); + + long double d=min(minl,minr); + + vectorstrip(n); + + int j=0; + + merge(pyl.begin(),pyl.end(),pyr.begin(),pyr.end(),py.begin(),yComp); + + for(int i=0;i>n; + + vector px(n); + + vectorpy(n); + + for(int i=0;i>x>>y; + + px[i].x=x; + + px[i].y=y; + + + + py[i]=px[i]; + + } + + sort(px.begin(),px.end(),xComp); + + + + ki< + + +using namespace std; + + + +const int kMaxN = 100005; + + + +int n; + +pair p[kMaxN]; + + + +double pointDist(const pair &a, const pair &b) { + + return sqrt(1.0 * (a.first - b.first) * (a.first - b.first) + + + 1.0 * (a.second - b.second) * (a.second - b.second)); + +} + + + +bool sortOrdinate(const pair &a, const pair &b) { + + if(a.second == b.second) + + return a.first < b.first; + + return a.second < b.second; + +} + + + +bool sortOnlyAbscissa(const pair &a, const pair &b) { + + return a.first < b.first; + +} + + + +pair >> findClosestPair(const int i, const int j) { + + if (i > j) return make_pair(1e16, vector >()); + + auto eqRange = equal_range(p + i, p + j + 1, p[(i + j) / 2], sortOnlyAbscissa); + + double dist = 1e16; + + for (int k = eqRange.first - p + 1; k < eqRange.second - p; k++) dist = min(dist, 1.0 * p[k].second - p[k - 1].second); + + auto dpLeft = findClosestPair(i, eqRange.first - p - 1); + + auto dpRight = findClosestPair(eqRange.second - p, j); + + dist = min(dist, min(dpLeft.first, dpRight.first)); + + vector > ySorted_0(dpLeft.second.size() + dpRight.second.size()); + + merge(dpLeft.second.begin(), dpLeft.second.end(), dpRight.second.begin(), dpRight.second.end(), ySorted_0.begin(), sortOrdinate); + + vector > ySorted(ySorted_0.size() + eqRange.second - eqRange.first); + + merge(ySorted_0.begin(), ySorted_0.end(), eqRange.first, eqRange.second, ySorted.begin(), sortOrdinate); + + vector > strip; + + for (const pair &x: ySorted) + + if (abs(p[(i + j) / 2].first - x.first) < dist) + + strip.push_back(x); + + for(int k = 0; k < strip.size(); k++) + + for(int q = 1; k + q < strip.size() && q < 8; q++) + + dist = min(dist, pointDist(strip[k], strip[k + q])); + + return make_pair(dist, ySorted); + +} + + + +int main() { + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + scanf("%d", &n); + + for (int i = 1; i <= n; i++) scanf("%d %d", &p[i].first, &p[i].second); + + sort(p + 1, p + n + 1); + + printf("%f\n", findClosestPair(1, n).first); + + return 0; + +} diff --git a/cmap/divide_conquer/cd3d14e3-f806-45d3-8d92-d7aa89ce1aa0.cpp b/cmap/divide_conquer/cd3d14e3-f806-45d3-8d92-d7aa89ce1aa0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8fd2564a5aa8d3befbd2aa3c1136601b04dfd12 --- /dev/null +++ b/cmap/divide_conquer/cd3d14e3-f806-45d3-8d92-d7aa89ce1aa0.cpp @@ -0,0 +1,236 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +struct point + +{ + + double x, y; + +}; + + + +point puncte[99999], aux[99999], inter[99999], a, b; + + + +double distance(point a, point b) + +{ + + return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)); + +} + + + +bool cmpX(point a, point b) + +{ + + return a.x < b.x; + +} + + + +bool cmpY(point a, point b) + +{ + + return a.y < b.y; + +} + + + +double dist_min(int left, int right) + +{ + + if (right - left + 1 == 3) + + { + + sort(puncte + left, puncte + right, cmpY); + + double d1 = distance(puncte[left], puncte[left + 1]); + + double d2 = distance(puncte[left + 1], puncte[left + 2]); + + double d3 = distance(puncte[left], puncte[left + 2]); + + double minim = d1; + + a = puncte[left]; + + b = puncte[left + 1]; + + if (d2 < minim) + + { + + minim = d2; + + a = puncte[left + 1]; + + b = puncte[left + 2]; + + } + + if (d3 < minim) + + { + + a = puncte[left]; + + b = puncte[left + 2]; + + minim = d3; + + } + + return minim; + + } + + if (right - left + 1 == 2) + + { + + if (puncte[left].y > puncte[left + 1].y) + + swap(puncte[left], puncte[left + 1]); + + a = puncte[left]; + + b = puncte[left + 1]; + + return distance(puncte[left], puncte[left + 1]); + + } + + else + + { + + int mijloc = (left + right) / 2; + + double mediana = puncte[mijloc].x; + + double delta = min(dist_min(left, mijloc), dist_min(mijloc + 1, right)); + + int nr = 0, i, j, k; + + + + + + i = left; + + k = left; + + j = mijloc + 1; + + while (i <= mijloc && j <= right) + + if (puncte[i].y < puncte[j].y) + + aux[k++] = puncte[i++]; + + else aux[k++] = puncte[j++]; + + while (i <= mijloc) + + aux[k++] = puncte[i++]; + + while (j <= right) + + aux[k++] = puncte[j++]; + + + + for (i = left; i <= right; i++) + + { + + puncte[i] = aux[i]; + + if (puncte[i].x - mediana <= delta && puncte[i].x - mediana >= -delta) + + inter[++nr] = puncte[i]; + + } + + + + for (i = 1; i <= nr; i++) + + { + + int nr_vecini = min(nr, i + 7); + + for (j = i + 1; j <= nr_vecini; j++) + + if (distance(inter[i], inter[j]) < delta) + + { + + delta = distance(inter[i], inter[j]); + + a = inter[i]; + + b = inter[j]; + + } + + } + + return delta; + + } + +} + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + int n, index; + + fin >> n; + + for (index = 1; index <= n; index++) + + fin >> puncte[index].x >> puncte[index].y; + + sort(puncte + 1, puncte + n + 1, cmpX); + + + + + + fout << setprecision(6) << fixed << dist_min(1, n); + + return 0; + +} diff --git a/cmap/divide_conquer/cf47e319-5b30-4446-8070-6b09be132d02.cpp b/cmap/divide_conquer/cf47e319-5b30-4446-8070-6b09be132d02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e62efb7ecf0c4d05a76d23ec7b2e840cae1b506 --- /dev/null +++ b/cmap/divide_conquer/cf47e319-5b30-4446-8070-6b09be132d02.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +#include +#include +#include. +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +class POINT + +{ + +public : + + long long x,y; + + + + + + + + friend bool mycompx(POINT a,POINT b); + + friend bool mycompy(POINT a,POINT b); + + + + + + friend long long point_distance(POINT a, POINT b); + + + +}; + + + +bool mycompy(POINT a,POINT b) + +{ + + return a.y < b.y; + +} + + + + + +bool mycompx(POINT a,POINT b) + +{ + + return a.x < b.x; + +} + + + +long long squared(long long x) + +{ + + return x*x; + + + +} + + + + + +POINT v_sortedx[100000 + 5]; + +POINT strip[100000 + 5]; + + + + + +int n; + + + +void read_data() + +{ + + in >> n; + + + + for(int i = 1 ; i<=n ; ++i) + + { + + in>>v_sortedx[i].x; + + in>>v_sortedx[i].y; + + } + + + +} + + + +long long point_distance(POINT a, POINT b) + +{ + + long long lat_1 = b.x - a.x; + + long long lat_2 = b.y - a.y; + + + + return lat_1*lat_1 + lat_2*lat_2; + +} +long long small_dist(int left,int right) + +{ + + + + int i,j; + + long long dist = 0xFFFFFFFFFFFFF; + + + + for(i = left ; i < right ; ++i) + + for( j = i+1 ; j <=right ; ++j) + + { + + long long temp = point_distance(v_sortedx[i],v_sortedx[j]); + + if( temp < dist) + + dist = temp; + + } + + + + return dist; + +} + + + + + +long long min_distance(int left,int right) + +{ + + if(right - left < 3) + + { + + return small_dist(left,right); + + } + + + + + + int m = (left + right ) / 2; + + + + long long d1 = min_distance(left,m); + + long long d2 = min_distance(m,right); + + + + long long d = d1 < d2 ? d1 : d2; + + + + int k = 0; + + + + for(int i = left ; i<=right ; ++i) + + { + + if(squared(v_sortedx[i].x - v_sortedx[m].x) < d) + + strip[++k] = v_sortedx[i]; + + } + + + + sort(strip+1,strip+k+1,mycompy); + + + + for(int i = 1 ; i<=k ; ++i) + + { + + + + for(int j = i + 1; squared(strip[j].y-strip[i].y) < d && j <=k ; ++j) + + { + + long long dist = point_distance(strip[i],strip[j]); + + if(dist < d) + + d = dist; + + } + + } + + + + return d; + +} +int main() + +{ + + read_data(); + + + + + + sort(v_sortedx+1,v_sortedx+n+1,mycompx); + + + + long long d; + + + + d = min_distance(1,n); + + + + out< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int Nmax = 100000; + +const long long inf = 1000000000000000000; + + + +pair p[Nmax + 5], p2[Nmax + 5]; + +int n; + + + +void Read(){ + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> p[i].first >> p[i].second; + +} + + + +long long Distance(pair x, pair y){ + + return 1LL * (x.first - y.first) * (x.first - y.first) + 1LL * (x.second - y.second) * (x.second - y.second); + +} + + + +long long DEI(int l, int r){ + + if (l == r) + + return inf; + + if (r - l == 1) + + return Distance(p[l], p[r]); + + int mid = (l + r) >> 1; + + long long d = min(DEI(l, mid), DEI(mid + 1, r)); + + int ind = 0; + + for (int i = l; i <= r; i++) + + if (abs(p[i].first - p[mid].first) <= d){ + + ind++; + + p2[ind].first = p[i].second; + + p2[ind].second = p[i].first; + + } + + sort(p2 + 1, p2 + ind + 1); + + for (int i = 1; i <= ind; i++) + + for (int j = i + 1; j <= i + 7 && j <= ind; j++) + + d = min(d, Distance(p2[i], p2[j])); + + return d; + +} + + + +int main(){ + + Read(); + + sort(p + 1, p + n + 1); + + fout << fixed << setprecision(6) << sqrt(DEI(1, n)) << '\n'; + + return 0; + +} diff --git a/cmap/divide_conquer/d25ae64f-0420-4119-beaa-268d83120f73.cpp b/cmap/divide_conquer/d25ae64f-0420-4119-beaa-268d83120f73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d232baa8b116094ae888315f8a3a4b3e5e37e816 --- /dev/null +++ b/cmap/divide_conquer/d25ae64f-0420-4119-beaa-268d83120f73.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +const int N = 1e5; + +const double EPS = 0.00000001; + +struct Point { + + int x; + + int y; + + bool operator < (const Point &p) const { + + return y < p.y; + + } + +}; + + + +Point a[1 + N]; + + + +bool comp (Point a, Point b) { + + return a.x < b.x; + +} + + + +double sq (double x) { + + return x * x; + +} + + + +double dist (Point a, Point b) { + + return sqrt (sq (a.x - b.x) + sq (a.y - b.y)); + +} + + + +double solve (int l, int r) { + + int mid = (l + r) / 2; + + if (r - l <= 4) { + + double ans = dist (a[l], a[l + 1]); + + for (int i = l; i <= r; i++) for (int j = i + 1; j <= r; j++) ans = min (ans, dist (a[i], a[j])); + + return ans; + + } + + double ans = min (solve (l, mid), solve (mid + 1, r)); + + vector v; + + for (int i = l; i <= r; i++) if (abs (a[i].y - a[mid].y) - ans < -EPS) v.push_back (a[i]); + + sort (v.begin (), v.end (), comp); + + for (int i = 0; i < v.size (); i++) for (int j = i + 1; j < min ((int)v.size (), i + 10); j++) ans = min (ans, dist (v[i], v[j])); + + return ans; + +} + + + +int main() { + + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + ios::sync_with_stdio (false); + + cin.tie (0); cout.tie (0); + + int n; + + cin >> n; + + for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; + + sort (a + 1, a + n + 1); + + cout << fixed << setprecision (8) << solve (1, n) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/d4b4866c-3e9f-4de3-85df-82d061af516c.cpp b/cmap/divide_conquer/d4b4866c-3e9f-4de3-85df-82d061af516c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caa77ca249f62c1fc0d811b735fbaf0f0b2619bd --- /dev/null +++ b/cmap/divide_conquer/d4b4866c-3e9f-4de3-85df-82d061af516c.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +struct point +{ + int x, y; +}; + +point v[100001], v2[100001], v3[100001]; +int n; + + +bool compare(point a, point b) +{ + if (a.x < b.x) + return true; + if (a.x > b.x) + return false; + return true; +} + +bool compare2(point a, point b) +{ + if (a.y < b.y) + return true; + if (a.y > b.y) + return false; + return true; +} + +long long dist(point a, point b) +{ + return ((long long)a.x-(long long)b.x)*((long long)a.x-(long long)b.x)+((long long)a.y-(long long)b.y)*((long long)a.y-(long long)b.y); +} + + +long long solve(int lower, int upper) +{ + if (upper - lower == 1) + { + return dist(v[lower], v[upper]); + } + + if (upper - lower == 2) + { + long long res1 = dist(v[lower], v[lower+1]); + long long res2 = dist(v[lower], v[lower+2]); + + if (res1 > res2) + res1 = res2; + res2 = dist(v[lower+1], v[lower+2]); + if (res1 > res2) + res1 = res2; + + return res1; + } + + int mid = lower + (upper - lower) / 2, i, j; + long long res1 = solve(lower, mid); + long long res2 = solve(mid+1, upper); + if (res1 > res2) + res1 = res2; + + i = lower; j = mid + 1; + double l = (double)v[mid].x + (v[mid+1].x - v[mid].x) / 2.0; + for (int k = i; k <= j; k++) + v2[k] = v[k]; + sort(v2 + i, v2 + j + 1, compare2); + + int c = 0; + for (int k = i; k <= j; k++) + if (fabs((double)v2[k].x - l) <= res1) + { + c++; + v3[c] = v2[k]; + } + for (int k = 1; k <= c; k++) + { + for (int l = 1; l <= 7; l++) + { + if (k + l <= c) + { + if (res1 > dist(v3[k], v3[k+l])) + res1 = dist(v3[k], v3[k+l]); + } + } + } + + return res1; +} + +int main() +{ + int i; + fin >> n; + for (i = 1; i <= n; i++) + fin >> v[i].x >> v[i].y; + fin.close(); + sort(v + 1, v + n + 1, compare); + + long long res = solve(1, n); + fout << fixed << setprecision(6) << sqrt(res) << '\n'; + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/d4dd7feb-3eb2-437c-9394-ffa429d17d7e.cpp b/cmap/divide_conquer/d4dd7feb-3eb2-437c-9394-ffa429d17d7e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..468d2c8d81484bac06ea44732cd424864b256be5 --- /dev/null +++ b/cmap/divide_conquer/d4dd7feb-3eb2-437c-9394-ffa429d17d7e.cpp @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +const int N=1e5+5; + +struct points{ + + int x,y; + +}; + +bool cmpx(points a,points b){ + + if(a.x==b.x) + + return a.y=0) + + return x; + + return -x; + +} + +int init; + +points aux[N]; + +vector strip; + +double FindMinDist(int st,int dr,int n){ + + double mindist; + + if(n<=3){ + + mindist=dist(px[st],px[st+1]); + + for(int i=st;i<=dr;i++){ + + py[i]=px[i]; + + for(int j=i+1;j<=dr;j++){ + + mindist=minn(mindist,dist(px[i],px[j])); + + } + + } + + sort(py+st,py+dr+1,cmpy); + + return mindist; + + } + + double mindist1,mindist2; + + int mid=st+n/2-1; + + mindist1=FindMinDist(st,mid,n/2); + + mindist2=FindMinDist(mid+1,dr,n-n/2); + + mindist=minn(mindist1,mindist2); + + int i=st,j1=st,j2=mid+1; + + while(i<=dr){ + + if(j1<=mid && (py[j1].ydr)){ + + aux[i]=py[j1++]; + + } + + else{ + + aux[i]=py[j2++]; + + } + + i++; + + } + + for(int i=st;i<=dr;i++){ + + py[i]=aux[i]; + + } + + int nrs=0; + + strip.clear(); + + strip.push_back({0,0}); + + for(int i=st;i<=dr;i++){ + + if(abss(((double)px[mid].x-py[i].x))<=mindist){ + + strip.push_back(py[i]); + + nrs++; + + } + + } + + for(int i=1;i<=nrs;i++){ + + int j=i+1; + + while(j<=nrs && strip[j].y-strip[i].y<=mindist){ + + mindist=minn(mindist,dist(strip[j],strip[i])); + + j++; + + } + + } + + return mindist; + +} + +int main() + +{ + + int n; + + fin>>n; + + + + init=n; + + for(int i=1;i<=n;i++){ + + fin>>px[i].x>>px[i].y; + + + + } + + sort(px+1,px+n+1,cmpx); + + + + fout< #include +#include #include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF = 9e18; + +vector< pair > V(100005), X, Y; + +long long dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + +long long solve(int left, int right, vector < pair >& X, vector < pair >& Y) { + + if(left >= right - 1) + + return INF; + + else + + if(right - left == 2) { + + if(Y[left] > Y[left + 1]) + + swap(Y[left], Y[left + 1]); + + return dist(X[left], X[left + 1]); + + } + + int middle = (left + right) / 2; + + long long best = min(solve(left, middle, X, Y), solve(middle, right, X, Y)); + + merge(Y.begin() + left, Y.begin() + middle, Y.begin() + middle, Y.begin() + right, V.begin()); + + copy(V.begin(), V.begin() + (right - left), Y.begin() + left); + + int sz = 0; + + for(int i = left; i < right; ++i) + + if(abs(Y[i].second - X[middle].first) <= best) + + V[sz++] = Y[i]; + + for(int i = 0; i < sz - 1; ++ i) { + + for(int j = i + 1; j < sz && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + +int main(){ + + int n; + + f>>n; + + X.resize(n), Y.resize(n); + + for(int i = 0; i < n; ++i){ + + f>>X[i].first>>X[i].second; + + } + + sort(X.begin(), X.end()); + + for(int i = 0; i < (int) X.size(); ++i) + + Y[i] = make_pair(X[i].second, X[i].first); + + g< + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int nmax = 100005; + + + +int n, auxSize; + + + +struct Point{ + + int x, y; + +}v[nmax], aux[nmax]; + + + +bool cmp(Point a, Point b){ + + if (a.x == b.x){ + + return a.y < b.y; + + } + + return a.x < b.x; + +} + + + +void Merge(int st, int dr){ + + int mid = (st + dr) / 2; + + int i = st, j = mid + 1; + + auxSize = 0; + + while (i <= mid && j <= dr){ + + if (v[i].y < v[j].y){ + + aux[++auxSize] = v[i++]; + + } + + else{ + + aux[++auxSize] = v[j++]; + + } + + } + + while (i <= mid){ + + aux[++auxSize] = v[i++]; + + } + + while (j <= dr){ + + aux[++auxSize] = v[j++]; + + } + + for (i = st, j = 1; i <= dr; ++i, ++j){ + + v[i] = aux[j]; + + } + +} + + + +long long GetDist(Point a, Point b){ + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +long long GetMinDist(int st, int dr){ + + if (dr - st + 1 <= 3){ + + if (dr - st + 1 == 3){ + + Merge(st, st + 1); + + Merge(st, dr); + + return min(GetDist(v[st], v[st + 1]), min(GetDist(v[st], v[dr]), GetDist(v[st + 1], v[dr]))); + + } + + else if (dr - st + 1 == 2){ + + Merge(st, dr); + + return GetDist(v[st], v[dr]); + + } + + return (1LL << 60); + + } + + int mid = (st + dr) / 2; + + int midX = v[mid].x; + + long long left = GetMinDist(st, mid); + + long long right = GetMinDist(mid + 1, dr); + + long long minim = min(left, right); + + Merge(st, dr); + + auxSize = 0; + + for (int i = st; i <= dr; ++i){ + + if (1LL * (midX - v[i].x) * (midX - v[i].x) <= minim){ + + aux[++auxSize] = v[i]; + + } + + } + + for (int i = 1; i <= auxSize; ++i){ + + for (int j = i + 1; j - i + 1 <= 9 && j <= auxSize; ++j){ + + minim = min(minim, GetDist(aux[i], aux[j])); + + } + + } + + return minim; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; ++i){ + + fin >> v[i].x >> v[i].y; + + } + + sort(v + 1, v + n + 1, cmp); + + for (int i = 1; i < n; ++i){ + + if (v[i].x == v[i + 1].x && v[i].y == v[i + 1].y){ + + fout << 0; + + return 0; + + } + + } + + long long ans = GetMinDist(1, n); + + fout << fixed << setprecision(6) << sqrt(ans); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/d6ab9e71-0b8d-4297-8924-c8fc382f776d.cpp b/cmap/divide_conquer/d6ab9e71-0b8d-4297-8924-c8fc382f776d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d98ab693d220bbf7e8929bb4036109ff89690f0d --- /dev/null +++ b/cmap/divide_conquer/d6ab9e71-0b8d-4297-8924-c8fc382f776d.cpp @@ -0,0 +1,170 @@ +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +using ll = long long; + +using ull = unsigned long long; + +using ui = unsigned int; + + + + + +const int NMax = 1e5 + 5; + +const ll inf = 1e18 + 5; + +const int mod = 100003; + +using zint = int; + + + +int N; + +pair v[NMax],aux[NMax]; + + + +ll solve(int,int); + +ll dist(pair,pair); + + + +int main() { + + in>>N; + + for (int i=1;i <= N;++i) { + + in>>v[i].first>>v[i].second; + + } + + + + sort(v+1,v+N+1); + + + + out<= dr) { + + return inf; + + } + + if (st == dr-1) { + + ll ans = dist(v[st],v[dr]); + + if (v[st].second > v[dr].second) { + + swap(v[st],v[dr]); + + } + + return ans; + + } + + + + int mij = (st+dr)>>1, abscisa = v[mij].first; + + ll ans = min(solve(st,mij),solve(mij+1,dr)); + + + + int nrAux = 0,i = st,j = mij+1; + + while (i <= mij || j <= dr) { + + if (j > dr || i <= mij && v[i].second < v[j].second) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + + + for (int i=st;i <= dr;++i) { + + v[i] = aux[i-st+1]; + + } + + + + nrAux = 0; + + ll root = sqrt(ans); + + for (int i=st;i <= dr;++i) { + + if (abs(v[i].first - abscisa) <= root) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (int i=1;i <= nrAux;++i) { + + for (int j=i+1;j <= nrAux && j <= i+7;++j) { + + ans = min(ans, dist(aux[i],aux[j])); + + } + + } + + + + return ans; + +} + + + +ll dist(pair p1,pair p2) { + + ll dx = (p1.first - p2.first), + + dy = (p1.second - p2.second); + + return dx*dx + dy*dy; + +} diff --git a/cmap/divide_conquer/d8fb282a-113e-4ec9-bf5d-8d1bdd73aeaf.cpp b/cmap/divide_conquer/d8fb282a-113e-4ec9-bf5d-8d1bdd73aeaf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f297615c49583d83ee3227abc002af7a705c8f2 --- /dev/null +++ b/cmap/divide_conquer/d8fb282a-113e-4ec9-bf5d-8d1bdd73aeaf.cpp @@ -0,0 +1,146 @@ +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +pair v[100005], aux[100005], local[100005]; + + + +inline double dist(pair A, pair B) { + + return sqrt(1LL*(A.first-B.first)*(A.first-B.first)+1LL*(A.second-B.second)*(A.second-B.second)); + +} + + + +bool comp(pair A, pair B) { + + return B.second > B.first; + +} + + + +double solve(int left, int right) { + + if(right - left == 1) { + + if(aux[left].second > aux[right].second) + + swap(aux[left], aux[right]); + + + + return dist(aux[left], aux[right]); + + } + + else if(right - left == 2) { + + sort(aux+left, aux+right+1, comp); + + + + return min(dist(aux[left], aux[left+1]), min(dist(aux[left+1], aux[left+2]), dist(aux[left], aux[left+2]))); + + } + + + + int mid=(left+right)/2,ind1=left,ind2=mid+1,ind=left,i,j; + + double first = solve(left, mid); + + double second = solve(mid+1, right); + + double d=min(first, second); + + + + while(ind1 <= mid && ind2 <=right) { + + if(aux[ind1].second < aux[ind2].second) + + local[ind++] = aux[ind1++]; + + else local[ind++] = aux[ind2++]; + + } + + + + while(ind1<=mid) + + local[ind++]=aux[ind1++]; + + + + while(ind2<=right) + + local[ind++]=aux[ind2++]; + + + + for(i=left;i<=right;++i) aux[i]=local[i]; + + + + vector > candidates; + + for(i=left;i<=right;++i) + + if(abs(aux[i].first-v[mid].first)<=d) + + candidates.push_back(aux[i]); + + + + for(i=0;i>n; + + + + for(i=1;i<=n;++i) fin>>v[i].first>>v[i].second; + + + + sort(v+1,v+n+1); + + for(i=1;i<=n;++i) aux[i]=v[i]; + + + + fout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +ofstream g("cmap.out"); + + + +int n, i, j; + +double s[100005], mn=2e9; + +struct point{ + + double x, y; + +}v[100001]; + + + +bool comp(point a,point b) + +{ + + if (a.x==b.x) + + return a.y A, B; + + int i = (st+dr)/2; + + double d1, d2; + + d1 = Min(st, i); + + d2 = Min(i, dr); + + mn = min(d1,min(mn, d2)); + + + + int sav = i-1; + + double m = v[i].x; + + while (abs(m - v[sav].x) < mn && sav > 0) + + { + + A.push_back(v[sav]); + + sav--; + + } + + + + sav = i; + + while (abs(v[sav].x - m) < mn && sav <= n) + + { + + B.push_back(v[sav]); + + sav++; + + } + + + + sort (A.begin(),A.end(),comp2); + + sort (B.begin(),B.end(),comp2); + + + + int j = 0; + + if (B.size() != 0 && A.size() != 0) + + { + + for (i = 0; i < A.size(); i++) + + { + + for (j; j + 1 < B.size() && B[j+1].y + mn < A[i].y; ++j); + + + + sav = j; + + while (sav >= 0 && abs(B[sav].y-A[i].y) < mn) + + mn = min(dist(B[sav--],A[i]), mn); + + + + while (j + 1 < B.size() && abs(B[j+1].y-A[i].y) < mn) + + mn = min(dist(B[++j],A[i]), mn); + + } + + } + + + + return mn; + +} + + + +int main() + +{ + + freopen("cmap.in","r",stdin); + + scanf("%d",&n); + + for (int i=1;i<=n;i++) + + scanf("%lf%lf", &v[i].x, &v[i].y); + + + + sort(v+1,v+n+1,comp); + + + + g< +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); +const long long INF = 4e18; +struct Punct { + long long x, y; +}; + +void readInput(int &n, vector &P) { + fin >> n; + long long x, y; + Punct p; + for (int i = 0; i < n; i++) { + fin >> x >> y; + p.x = x, p.y = y; + P.push_back(p); + } + + +} + +void merge(vector &Py, int st, int m, int dr) { + int i = st, j = m + 1; + vector temp; + while (i <= m && j <= dr) { + if (Py[i].y < Py[j].y) + temp.push_back(Py[i++]); + else temp.push_back(Py[j++]); + + } + while (i <= m) + temp.push_back(Py[i++]); + while (j <= dr) + temp.push_back(Py[j++]); + + for (int i = 0; i < temp.size(); i++) + Py[st + i] = temp[i]; +} + +long long distanta(const Punct &a, const Punct &b) { + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); +} + +long long compute_strip(vector &strip, long long d) { + long long minim = d; + int len = strip.size(); + for (int i = 0; i < len - 1; i++) + for (int j = i + 1; j < len && j <= i + 7; j++) + minim = min(minim, distanta(strip[i], strip[j])); + return minim; +} + +long long closestTwoPoints(vector &Px, vector &Py, int st, int dr) { + + if (dr - st == 1) { + if (Px[st].y > Px[dr].y) + swap(Px[st], Px[dr]); + return distanta(Px[st], Px[dr]); + } else if (dr - st == 2) { + merge(Py, st, st, st + 1); + merge(Py, st, st + 1, dr); + return min(distanta(Px[st], Px[dr]), + min(distanta(Px[st + 1], Px[st]), distanta(Px[st + 1], Px[dr]))); + + } else { + int m = (st + dr) / 2; + long long dLeft = closestTwoPoints(Px, Py, st, m); + long long dRight = closestTwoPoints(Px, Py, m + 1, dr); + + long long d = min(dLeft, dRight); + + merge(Py, st, m, dr); + vector strip; + + for (int i = st; i <= dr; i++) + if (abs(Py[i].x - Px[m].x) <= d) + strip.push_back(Py[i]); + + + return compute_strip(strip, d); + + } + +} + +bool cmp(const Punct &a, const Punct &b) { + return (a.x < b.x) || (a.x == b.x && a.y < b.y); +} + +int main() { + int n; + vector Px; + readInput(n, Px); + + + sort(Px.begin(), Px.end(), cmp); + vector Py = Px; + fout << fixed << setprecision(6) << sqrt(closestTwoPoints(Px, Py, 0, n - 1)); + return 0; +} diff --git a/cmap/divide_conquer/db632bda-6263-4d73-88d7-7f3983443c4c.cpp b/cmap/divide_conquer/db632bda-6263-4d73-88d7-7f3983443c4c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e39c52fbb973adc4a098753320d5b42af82afa6 --- /dev/null +++ b/cmap/divide_conquer/db632bda-6263-4d73-88d7-7f3983443c4c.cpp @@ -0,0 +1,61 @@ +#include + +using namespace std; + +typedef long double ld; + +ifstream fin("cmap.in"); +ofstream fout("cmap.out"); + +const ld INF = 1e13; + +vector < pair < ld, ld > > points; +vector < pair < ld, ld > > strip; + +ld getDistance(const pair < ld, ld > &a, const pair < ld, ld > &b) { + return sqrtl((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); +} +ld Solve(int s, int e) { + if(e - s <= 3) { + ld dist = INF; + for(int i = s; i < e; ++i) { + for(int j = i + 1; j < e; ++j) { + dist = min(dist, getDistance(points[i], points[j])); + } + } + return dist; + } + int mid = (s + e) / 2; + ld dist = min(Solve(s, mid), Solve(mid, e)); + + int idx = 0; + for(int i = s; i < e; ++i) { + if(abs(points[mid].first - points[i].first) < dist) { + strip[idx++] = points[i]; + } + } + + sort(strip.begin(), strip.begin() + idx, [&](const pair < ld, ld > &a, const pair < ld, ld > &b) { + return a.second < b.second; + }); + + for(int i = 0; i < idx; ++i) { + int m = min(i + 7, idx); + for(int j = i + 1; j < m; ++j) { + dist = min(dist, getDistance(strip[i], strip[j])); + } + } + return dist; +} + +int main() { + int n; fin >> n; + points.resize(n); strip.resize(n); + for(int i = 0; i < n; ++i) { + fin >> points[i].first >> points[i].second; + } + + sort(points.begin(), points.end()); + fout << fixed << setprecision(10) << Solve(0, n); + return 0; +} diff --git a/cmap/divide_conquer/dbb42366-1b50-4205-8c40-8ebffd66774b.cpp b/cmap/divide_conquer/dbb42366-1b50-4205-8c40-8ebffd66774b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1f1f8e765962adc65583dbec94a3dc5c5cac66d --- /dev/null +++ b/cmap/divide_conquer/dbb42366-1b50-4205-8c40-8ebffd66774b.cpp @@ -0,0 +1,144 @@ +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const int DIM = 1e5 + 5; + + + +int N; + +pair arr[DIM], aux[DIM]; + + + +inline long long dist( pair A, pair B ){ + + return 1LL * (A.first - B.first) * (A.first - B.first) + + + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long solve( int st, int dr ){ + + if( dr - st == 1 ){ + + long long ans = dist( arr[st], arr[dr] ); + + if( arr[st].second > arr[dr].second ) + + swap( arr[st], arr[dr] ); + + return ans; + + } + + if( dr - st == 2 ){ + + long long ans = dist( arr[st], arr[st + 1] ); + + ans = min( ans, dist( arr[st], arr[dr] ) ); + + ans = min( ans, dist( arr[st + 1], arr[dr] ) ); + + if( arr[st].second > arr[st + 1].second ) + + swap( arr[st], arr[st + 1] ); + + if( arr[st].second > arr[dr].second ) + + swap( arr[st], arr[dr] ); + + if( arr[st + 1].second > arr[dr].second ) + + swap( arr[st + 1], arr[dr] ); + + return ans; + + } + + int mid = ( st + dr ) >> 1; + + long long ans = solve( st, mid ); + + ans = min( ans, solve( mid + 1, dr ) ); + + + + int p1 = st, p2 = mid + 1, k = st - 1; + + while( p1 <= mid && p2 <= dr ){ + + if( arr[p1].second < arr[p2].second ) + + aux[++k] = arr[p1++]; + + else + + aux[++k] = arr[p2++]; + + } + + while( p1 <= mid ) + + aux[++k] = arr[p1++]; + + while( p2 <= dr ) + + aux[++k] = arr[p2++]; + + for( int i = st; i <= dr; i++ ) + + arr[i] = aux[i]; + + + + k = 0; + + for( int i = st; i <= dr; i++ ) + + if( (long long)( abs( (arr[mid].first - arr[i].first) ) ) <= ans ) + + aux[++k] = arr[i]; + + + + for( int i = 1; i <= k; i++ ) + + for( int j = i + 1; j <= i + 7 && j <= k; j++ ) + + ans = min( ans, dist( aux[i], aux[j] ) ); + + + + return ans; + +} + + + + + +int main(){ + + fin >> N; + + for( int i = 1; i <= N; i++ ) + + fin >> arr[i].first >> arr[i].second; + + sort( arr + 1, arr + N + 1 ); + + fout << setprecision(6) << fixed << sqrt( 1.0 * solve( 1, N ) ) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/dbd2a4e0-cac3-45ea-96eb-45ac2d6dff7b.cpp b/cmap/divide_conquer/dbd2a4e0-cac3-45ea-96eb-45ac2d6dff7b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a783fb71fbe4ca04cc33f2b52bae1958beaee709 --- /dev/null +++ b/cmap/divide_conquer/dbd2a4e0-cac3-45ea-96eb-45ac2d6dff7b.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct point{ + + int x, y; + +}v[100100],w[100100]; + + + +int n; + + + +bool cmp_x (point a, point b){ + + return a.x < b.x; + +} + + + +bool cmp_y(point a, point b){ + + return a.y < b.y; + +} + + + +long long dist ( point a, point b){ + + long long difx = 1LL * (a.x - b.x ); + + long long dify = 1LL * ( a.y - b.y ); + + + + return difx * difx + dify * dify ; + +} + + + + + + + +long long divide ( int left, int right){ + + if( right - left == 1){ + return dist( v[left], v[right] ); + + } + + if ( right-left == 2 ){ + + + + long long x,y,z; + + x=dist(v[left], v[left+1]); + + y=dist(v[left+1], v[right]); + + z=dist(v[left], v[right]); + + if((x<=y)&&(x<=z)){ + return x; + + } + + if((y<=x)&&(y<=z)){ + return y; + + } + + if((z<=x)&&(z<=y)){ + return z; + + } + + } + + + + int m=( left + right ) / 2; + + long long d1 = divide ( left, m ) ; + + long long d2 = divide ( m+1, right ) ; + + long long d = min( d1, d2); + + int i,j,k=0; + + long long delta = ceil(sqrt(d)); + + for( i = left; i <= right; i++){ + + if( abs( v[i].x - v[m].x ) <= delta ){ + + w[k].x = v[i].x; + + w[k].y = v[i].y; + + k++; + + } + + } + + + + sort(w,w+k,cmp_y); + + + + for(i = 0;i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++){ + + if(dist(w[i],w[j])>n; + + for (int i =0;i>v[i].x>>v[i].y; + + sort(v,v+n,cmp_x); + + g< +#include +#include +#include +#include +#include +#include + + +using namespace std; +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +pair points[100000], pointsY[100000], aux[100000]; + + + +long long d(pair a, pair b){ + + return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); + +} + + + +long long minDist(int l, int r){ + + if(r == l) + + return 1e18; + + else if(r - l == 1){ + + if(pointsY[r] < pointsY[l]) + + swap(pointsY[l], pointsY[r]); + + return d(pointsY[l], pointsY[r]); + + } + + int mid = (l + r) >> 1; + + long long minD = min( minDist(l, mid), minDist(mid + 1, r)); + + merge(pointsY + l, pointsY + mid + 1, pointsY + mid + 1, pointsY + r + 1, aux); + + copy(aux, aux + (r - l + 1), pointsY + l); + + + + int k = 0; + + for(int i = l; i <= r; i++) + + if(abs(points[mid].first - pointsY[i].second) <= minD && i != mid) + + aux[k++] = pointsY[i]; + + + + for(int i = 0; i < k; i++) + + for(int j = i + 1; j < k && j <= i + 8; j++) + + minD = min(minD, d(aux[i], aux[j])); + + + + return minD; + +} + + + +int main() + +{ + + f >> n; + + for(int i = 0; i < n; i++) + + f >> points[i].first >> points[i].second; + + + + sort(points, points + n); + + for(int i = 0; i < n; i++) + + pointsY[i] = make_pair(points[i].second, points[i].first); + + + + g << fixed << setprecision(6) << sqrt(minDist(0, n - 1)); + + return 0; + +} diff --git a/cmap/divide_conquer/dc8812e4-b9c1-496b-97fd-238bb297c942.cpp b/cmap/divide_conquer/dc8812e4-b9c1-496b-97fd-238bb297c942.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dd2f98f09f04e8bff57b64d73aafed79b2dfd84 --- /dev/null +++ b/cmap/divide_conquer/dc8812e4-b9c1-496b-97fd-238bb297c942.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +using namespace std; + + + +pair v[100010]; + +vector p; + + + +long long dist (int st, int dr) + +{ + + return (1LL * (v[dr].first - v[st].first) * (v[dr].first - v[st].first) + 1LL * (v[dr].second - v[st].second) * (v[dr].second - v[st].second)); + +} + + + +void del () + +{ + + vector aux; + + aux.swap (p); + +} + + + +long long aabs (int a) + +{ + + return 1LL * max (a, -a); + +} + + + +long long divide (int st, int dr) + +{ + + if (dr - st + 1 <= 3) + + { + + if (dr - st + 1 == 2) return dist (st, dr); + + else return min (min (dist (st, st + 1), dist (st, st + 2)), dist (st + 1, st + 2)); + + } + + + + int mij = (st + dr) >> 1; + + long long bst = min (divide (st, mij), divide (mij + 1, dr)); + + + + del (); + + for (int i = st; i <= dr; ++i) + + if (aabs (v[mij].first - v[i].first) <= bst) p.push_back (i); + + + + for (int i = 0; i < p.size (); ++i) + + for (int j = i + 1; j <= i + 7 && j < p.size (); ++j) + + bst = min (bst, dist (p[i], p[j])); + + + + return bst; + +} + + + +int main () + +{ + + freopen ("cmap.in", "r", stdin); + + freopen ("cmap.out", "w", stdout); + + + + int n; + + scanf ("%d", &n); + + + + for (int i = 1; i <= n; ++i) + + scanf ("%d %d", &v[i].first, &v[i].second); + + + + sort (v + 1, v + n + 1); + + + + printf ("%.8f\n", sqrt (1LL * divide (1, n))); + + + + return 0; + +} diff --git a/cmap/divide_conquer/dc98d2ed-3af4-4d5e-b861-45fd328d7456.cpp b/cmap/divide_conquer/dc98d2ed-3af4-4d5e-b861-45fd328d7456.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34da078a8aa1cb6f620895320b45b3b52a55b7fb --- /dev/null +++ b/cmap/divide_conquer/dc98d2ed-3af4-4d5e-b861-45fd328d7456.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +int n; + +pair P[100002]; + + + +bool comp(pair a,pair b){if (a.first==b.first) return a.second a,pair b){if (a.second==b.second) return a.first a,pair b){ + + return sqrt(1LL * (a.first-b.first) * (a.first-b.first) + 1LL * (a.second-b.second) * (a.second-b.second)); + +} + + + +long double solve(int st,int dr){ + + long double mn = 1e13; + + if (st==dr) return 1e13; + + if (st+1==dr) return dist(P[st],P[dr]); + + + + int mid = (st+dr)/2; + + mn = min(solve(st,mid),solve(mid+1,dr)); + + + + vector > V; + + long double VMID = (P[mid].first+P[mid+1].first)/2; + + for(int i=mid;i>=st && abs(VMID-P[i].first)<=mn;i--) V.push_back(P[i]); + + for(int i=mid+1;i<=dr && abs(VMID-P[i].first)<=mn;i++) V.push_back(P[i]); + + + + sort(V.begin(),V.end(),comp2); + + + + for (int i=0;i>n; + + for (int i=1;i<=n;i++) f>>P[i].first>>P[i].second; + + sort(P+1,P+n+1,comp); + + + + g< +using namespace std; +typedef pair point; +const long long INF = 1e18; +vector points; + +long long distance(point p1, point p2) { + + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); +} +bool cmp_y(point p1, point p2) { + + return p1.second < p2.second; + +} + + +long long divide(int left, int right) { + + if (right-left < 3) { + long long min_dist = INF; + for (int i = left; i < right; i++) { + for (int j = i + 1; j <= right; j++) { + min_dist = min(min_dist, distance(points[i], points[j]) ); + } + } + return min_dist; + } + int mid = (left + right) / 2; + long long min_dist_left = divide(left, mid); + long long min_dist_right = divide(mid + 1, right); + long long min_dist = min(min_dist_left, min_dist_right); + vector new_points; + + for (int i = left; i <= right; i++) { + + if(abs(points[mid].first-points[i].first)> n; + for (int i = 0; i < n; i++) { + point read; + f >> read.first >> read.second; + points.push_back(read); + + } + sort (points.begin(), points.end()); + + g << fixed << setprecision(7) << sqrt((long double) divide(0, n - 1)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/deb86703-a064-42ef-8aeb-e4608a4087b3.cpp b/cmap/divide_conquer/deb86703-a064-42ef-8aeb-e4608a4087b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1669317cc0df1f581f5df6dc7b9f6c21751e928 --- /dev/null +++ b/cmap/divide_conquer/deb86703-a064-42ef-8aeb-e4608a4087b3.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +long long INF = numeric_limits::max(); +typedef pair PLL; +typedef vector VPLL; + +int n; +VPLL x,y,tmp; + + + +long long d2(PLL a, PLL b){ + long long df = a.first-b.first, ds = a.second - b.second; + return df*df + ds*ds; +} + + + + +long long dmin(int st, int dr){ + if(st>=dr-1) return INF; + else if(st==dr-2){ + if(y[st]>y[st+1]) y[st].swap(y[st+1]); + return d2(x[st],x[st+1]); + } + else{ + + int mid = (st+dr)/2; + + long long bst = min( dmin(st,mid), dmin(mid,dr) ); + + merge(y.begin() + st, y.begin() + mid, y.begin() + mid, y.begin() + dr, tmp.begin()); + copy(tmp.begin(), tmp.begin() + (dr - st), y.begin() + st); + + int ind=0; + + for(int i=st; i>n; + x.resize(n); + y.resize(n); + tmp.resize(n); + + + for(auto &p : x) fin>>p.first>>p.second; + sort(x.begin(),x.end()); + for(int i=0;i +#include +#include +#include + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); +int n; + +pair v1[100003], v2[100003], v[100003]; + + + +bool cmp(pair a, pair b) { + + return (a < b); + +} + + + +double dist(pair a, pair b) { + + return sqrt((double)(a.first - b.first) * (a.first - b.first) + + + (double)(a.second - b.second) * (a.second - b.second)); + +} + + + +double divide(int x, int y) { + + if (x >= y) { + + return 4e18; + + } + + + + if (y == x + 1) { + + if (v2[x] > v2[y]) + + swap(v2[x], v2[y]); + + return dist(v1[x], v1[y]); + + } + + + + int mij = (x + y) / 2; + + double maxi = min(divide(x, mij), divide(mij + 1, y)); + + + + merge(v2 + x, v2 + mij + 1, v2 + mij + 1, v2 + y + 1, v); + + + + int i, j, lim; + + for (i = x; i <= y; ++i) + + v2[i] = v[i - x]; + + + + int poz = 0; + + for (i = x; i <= y; ++i) + + if (abs(v2[i].second - v1[mij].first) <= maxi) + + v[poz++] = v2[i]; + + for (i = 0; i < poz; ++i) { + + lim = min(poz, i + 8); + + for (j = i + 1; j < lim; ++j) + + maxi = min(maxi, dist(v[i], v[j])); + + } + + + + return maxi; + +} + + + +int main() { + + int i, x, y; + + + + f >> n; + + for (i = 1; i <= n; ++i) { + + f >> x >> y; + + v1[i] = make_pair(x, y); + + } + + + + sort(v1 + 1, v1 + n + 1, cmp); + + for (i = 1; i <= n; ++i) + + v2[i] = make_pair(v1[i].second, v1[i].first); + + g << fixed << setprecision(7) << divide(1, n) << '\n'; + + + + return 0; + +} diff --git a/cmap/divide_conquer/df50321a-edcf-48a1-9781-a0f070adac7a.cpp b/cmap/divide_conquer/df50321a-edcf-48a1-9781-a0f070adac7a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7226aa461927f69397e8df9bba74b9016e1b07fc --- /dev/null +++ b/cmap/divide_conquer/df50321a-edcf-48a1-9781-a0f070adac7a.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - Y[mid].second) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + in >> n; + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + in >> X[i].first >> X[i].second; + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/df56125f-b2f2-4775-9494-2e8eef26e927.cpp b/cmap/divide_conquer/df56125f-b2f2-4775-9494-2e8eef26e927.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c04c71e1e853850649e63859ff9941c4ff9feec0 --- /dev/null +++ b/cmap/divide_conquer/df56125f-b2f2-4775-9494-2e8eef26e927.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const char iname[] = "cmap.in"; +const char oname[] = "cmap.out"; + +typedef long long i64; + +const int MAX_N = 100005; +const i64 INF = 4e18; + +vector < pair > V(MAX_N), X, Y; + +i64 dist(const pair & a, const pair & b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + if (st >= dr - 1) + return INF; + else if (dr - st == 2) { + if (Y[st] > Y[st + 1]) + swap(Y[st], Y[st + 1]); + return dist(X[st], X[st + 1]); + } + int mid = (st + dr) / 2; + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + int v_size = 0; + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + V[v_size ++] = Y[i]; + for (int i = 0; i < v_size - 1; ++ i) { + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + best = min(best, dist(V[i], V[j])); + } + return best; +} + +int main(void) { + int n; + ifstream in(iname); + assert(in >> n); + assert(1 <= n && n <= 100000); + + X.resize(n), Y.resize(n); + for (int i = 0; i < (int) X.size(); ++ i) { + assert(in >> X[i].first >> X[i].second); + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + } + in.close(); + + sort(X.begin(), X.end()); + for (int i = 0; i < (int) X.size(); ++ i) + Y[i] = make_pair(X[i].second, X[i].first); + + ofstream out(oname); + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + out.close(); + return 0; +} diff --git a/cmap/divide_conquer/df5d1706-789d-41d2-ab01-763ef2af56d8.cpp b/cmap/divide_conquer/df5d1706-789d-41d2-ab01-763ef2af56d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb12574d2882bd437dde406dfbe442545be9765f --- /dev/null +++ b/cmap/divide_conquer/df5d1706-789d-41d2-ab01-763ef2af56d8.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include +#include +#include + +using point = std::pair; +using pointArr = std::vector; + +double squaredDistance(const point& p1, const point& p2) { + return std::pow(p1.first - p2.first, 2) + std::pow(p1.second - p2.second, 2); +} + +double solve(const pointArr& points, int left, int right) { + static pointArr pointsCopy = points; + if (left >= right) { + return std::numeric_limits::max(); + } + else if (right - left == 1) { + if (pointsCopy[left].second > pointsCopy[right].second) { + std::swap(pointsCopy[left], pointsCopy[right]); + } + return squaredDistance(points[left], points[right]); + } + auto middle = (left + right) >> 1; + auto minLeft = solve(points, left, middle); + auto minRight = solve(points, middle + 1, right); + auto delta = std::min(minLeft, minRight); + pointArr candidates(right - left + 1); + std::merge(pointsCopy.begin() + left, pointsCopy.begin() + middle + 1, pointsCopy.begin() + middle + 1, pointsCopy.begin() + right + 1, candidates.begin(), + [](const point& p1, const point& p2) { + return p1.second < p2.second; + }); + std::copy(candidates.begin(), candidates.end(), pointsCopy.begin() + left); + candidates.clear(); + for (auto it = left; it <= right; ++it) { + if (std::abs(pointsCopy[it].first - points[middle].first) <= delta) { + candidates.push_back(pointsCopy[it]); + } + } + for(auto it = 0u; it < candidates.size(); ++it) { + for (auto it2 = it + 1; it2 < candidates.size() && it2 - it < 6; ++it2) { + delta = std::min(delta, squaredDistance(candidates[it], candidates[it2])); + } + } + return delta; +} + +int main() +{ + pointArr points; + std::ifstream fin("cmap.in"); + auto n = 0; fin >> n; + for (auto it = 0; it < n; ++it) { + point currentPoint; + fin >> currentPoint.first >> currentPoint.second; + points.push_back(currentPoint); + } + fin.close(); + std::sort(points.begin(), points.end()); + std::ofstream fout("cmap.out"); + fout << std::fixed << std::setprecision(6) << std::pow(solve(points, 0, points.size() - 1), 0.5);; + fout.close(); + return 0; +} diff --git a/cmap/divide_conquer/df66b4aa-f2eb-4e2f-b192-2fdb165a10eb.cpp b/cmap/divide_conquer/df66b4aa-f2eb-4e2f-b192-2fdb165a10eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41051960bf9eaca5aa160a081319a4d37adf0886 --- /dev/null +++ b/cmap/divide_conquer/df66b4aa-f2eb-4e2f-b192-2fdb165a10eb.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +using namespace std; + + + +typedef pair Point; + +Point A[100000 +1]; + +Point B[100000 +1]; + +Point C[100000 +1]; + +int N; + + + +bool cmp(const Point A, const Point B) + +{ + + if(A.second == B.second) return A.first < B.first; + + return A.second < B.second; + +} + + + +long long dist(Point A, Point B) + +{ + + return 1LL * (A.first - B.first) * (A.first - B.first) + 1LL * (A.second - B.second) * (A.second - B.second); + +} + + + +long long Solve(int st, int dr) + +{ + + if(st >= dr) return 1LL<<62; + + else if(dr - st == 1){ + + if(B[st] > B[dr]) swap(B[st], B[dr]); + + return dist(A[st], A[dr]); + + } + + + + int i, j, k = 0, mid = (st + dr) / 2; + + long long res; + + + + res = min(Solve(st, mid), Solve(mid+1, dr)); + + merge(B + st, B + mid + 1, B + mid + 1, B + dr + 1, C, cmp); + + for(i = st; i <= dr; ++i) B[i] = C[i-st]; + + for(i = st; i <= dr; ++i) + + if((B[i].first - A[mid].first) * (B[i].first - A[mid].first) <= res) C[++k] = B[i]; + + + + for(i = 1; i < k; ++i) + + for(j = i+1; j <= k && j - i <= 8; ++j) + + res = min(res, dist(C[i], C[j])); + + + + return res; + +} + + + +int main(){ + + FILE* fin = fopen("cmap.in","r"); + + FILE* fout = fopen("cmap.out","w"); + + + + int i; + + + + fscanf(fin,"%d",&N); + + for(i = 1; i <= N; ++i) fscanf(fin,"%d %d",&A[i].first,&A[i].second); + + + + sort(A+1,A+N+1); + + for(i = 1; i <= N; ++i) B[i] = A[i]; + + fprintf(fout,"%f\n",sqrt(Solve(1,N))); + + + + + +return 0; + +} diff --git a/cmap/divide_conquer/e06875f6-24a6-4ade-9518-19cbe4ed22c1.cpp b/cmap/divide_conquer/e06875f6-24a6-4ade-9518-19cbe4ed22c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e26da932927641f9e828a2a8ada27a2e9d555ce --- /dev/null +++ b/cmap/divide_conquer/e06875f6-24a6-4ade-9518-19cbe4ed22c1.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + + + +typedef long long ll; + +const int NMax = 1e5 + 5; + +const ll inf = 9e18 + 5; + + + +int N; + + + +struct elem { + + ll x,y; + +}v[NMax],aux[NMax]; + + + +bool operator <(elem a,elem b) { + + return a.y < b.y; + +} + +bool cmp(elem a,elem b) { + + return a.x < b.x; + +} + + + +ll solve(int,int); + +ll dist(elem,elem); + + + +int main() { + + in>>N; + + for (int i=1;i<=N;++i) { + + in>>v[i].x>>v[i].y; + + } + + sort(v+1,v+N+1,cmp); + + + + ll dist = solve(1,N); + + out<>1, + + abscisa = v[mij].x, + + mn = min(solve(st,mij),solve(mij+1,dr)), + + nrAux = 0; + + + + int i = st, j = mij+1; + + while (i <= mij && j <= dr) { + + if (v[i] < v[j]) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + + + for (i=st;i<=dr;++i) { + + v[i] = aux[i - st + 1]; + + } + + + + nrAux = 0; + + long double root = sqrt(mn); + + for (i=st;i<=dr;++i) { + + if (abs(v[i].x - abscisa) <= root) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (i=1;i<=nrAux;++i) { + + for (j=i+1;j <= nrAux && j-i+1 <= 8;++j) { + + ll d = dist(aux[i],aux[j]); + + mn = min(mn,d); + + } + + } + + + + return mn; + +} + + + +ll dist(elem a,elem b) { + + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); + +} diff --git a/cmap/divide_conquer/e09d36b7-cc7b-4627-89c0-a66091546995.cpp b/cmap/divide_conquer/e09d36b7-cc7b-4627-89c0-a66091546995.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dbbcdf4f5626b86faaff5b486f0e1107aca2c54 --- /dev/null +++ b/cmap/divide_conquer/e09d36b7-cc7b-4627-89c0-a66091546995.cpp @@ -0,0 +1,210 @@ +#include +#include +#include +#include +#include +#include +#include +#include +const int NLIM = 1e5 + 10; + +const long double INF = INT_MAX; + + + +using namespace std; + + + +struct pointS + +{ + + long double x, y; + +}; + + + +int comparX( const void* a, const void* b ) + +{ + + pointS p1 = *(pointS*)a; + + pointS p2 = *(pointS*)b; + + + + if( p1.x < p2.x ) return -1; + + if( p1.x > p2.x ) return 1; + + + + if( p1.y < p2.y ) return -1; + + if( p1.y > p2.y ) return 1; + + return 0; + +} + +int comparY( const void* a, const void* b ) + +{ + + pointS p1 = *(pointS*)a; + + pointS p2 = *(pointS*)b; + + + + if( p1.y < p2.y ) return -1; + + if( p1.y > p2.y ) return 1; + + + + if( p1.x < p2.x ) return -1; + + if( p1.x > p2.x ) return 1; + + return 0; + +} + + + + + +long double dist( pointS p1, pointS p2 ) + +{ + + return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); + +} + + + +int n; + +pointS points[NLIM]; + +pointS strip[NLIM]; + + + + + +long double solve( int l, int r ) + +{ + + + + if( l + 3 >= r ) + + { + + + + long double res = INF; + + for( int i = l; i < r; ++i ) + + for( int j = i + 1; j < r; ++j ) + + { + + long double d = dist(points[i], points[j]); + + if( d < res ) + + res = d; + + } + + return res; + + } + + + + int m = ( l + r ) / 2; + + + + long double res = min( solve( l, m ), solve( m + 1, r ) ); + + + + + + + + int stripSize = 0; + + for( int i = l; i < r; ++i ) + + if( abs( points[i].x - points[m].x ) < res ) + + strip[stripSize] = points[i], stripSize++; + + + + + + qsort( strip, stripSize, sizeof( pointS ), comparY ); + + for( int i = 0; i < stripSize; ++i ) + + for( int j = i + 1; j < stripSize && ( strip[j].y - strip[i].y ) < res; ++j ) + + { + + long double d = dist( strip[i], strip[j] ); + + if( d < res ) + + res = d; + + } + + + + return res; + +} + + + +ifstream fcin("cmap.in"); + +ofstream fcout("cmap.out"); + +int main() + +{ + + fcin >> n; + + + + for( int i = 0; i < n; ++i ) + + fcin >> points[i].x >> points[i].y; + + + + qsort( points, n, sizeof( pointS ), comparX ); + fcout << fixed << setprecision(6) << solve( 0, n ) << "\n"; + + + + + + + +} diff --git a/cmap/divide_conquer/e16347fc-5b58-461e-a926-48d039a0db47.cpp b/cmap/divide_conquer/e16347fc-5b58-461e-a926-48d039a0db47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08dcb68c359c4292b8e2023ada7f6a34f412e4d3 --- /dev/null +++ b/cmap/divide_conquer/e16347fc-5b58-461e-a926-48d039a0db47.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + +int n,i; + +pair v[100010],w[100010],d[100010]; + +long long dist (pair a, pair b){ + + return (a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second); + +} + +inline long long modul (long long n){ + + if (n < 0) + + return -n; + + return n; + +} + +void interclasare (int st,int mid,int dr){ + + int i = st, j = mid+1, el = st-1; + + while (i <= mid && j <= dr){ + + if (v[i].second <= v[j].second){ + + d[++el] = v[i]; + + i++; + + } + + else{ + + d[++el] = v[j]; + + j++; + + } + + } + + for (;i<=mid;i++) + + d[++el] = v[i]; + + for (;j<=dr;j++) + + d[++el] = v[j]; + + for (int i=st;i<=dr;i++) + + v[i] = d[i]; + +} + + + +long long solve (int st, int dr){ + + long long rez; + + if (dr - st == 1){ + + rez = dist (v[st],v[dr]); + + interclasare (st,st,dr); + + return rez; + + } + + if (dr - st == 2){ + + rez = min (dist(v[st],v[st+1]),min(dist(v[st],v[st+2]),dist(v[st+1],v[st+2]))); + + interclasare (st,st,st+1); + + interclasare (st,st+1,dr); + + return rez; + + } + + + + int mid = (st+dr)/2; + + long long rezs = solve (st,mid); + + long long rezd = solve (mid+1,dr); + + + + interclasare (st,mid,dr); + + + + long long sol = min (rezs,rezd); + + + + int el = 0; + + for (int i=st;i<=dr;i++){ + + if (modul(v[mid].first - v[i].first) <= sol) + + w[++el] = v[i]; + + } + + + + for (int i=1;i>n; + + for (i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + sort (v+1,v+n+1); + + + + fout< +#include +#include +#include +#include +#include +#include + +std::ifstream fin("cmap.in"); +std::ofstream fout("cmap.out"); + +int n; +std::vector > V; + +void interclasare(int, int, int); + +void citire() { + fin >> n; + int x, y; + for (int i = 1; i <= n; i++) { + fin >> x >> y; + V.emplace_back(x, y); + } +} + +bool cmp(const std::pair &p1, const std::pair &p2) { + return (p1.second < p2.second); +} + +long long distanta_euclidiana_la_patrat(const std::pair &p1, const std::pair &p2) { + return (long long) (p1.first - p2.first) * (long long) (p1.first - p2.first) + + (long long) (p1.second - p2.second) * (long long) (p1.second - p2.second); +} + + +long long Solve(int st, int dr) { + + if (dr - st == 1) { + if(V[st].second > V[dr].second) + std::swap(V[st], V[dr]); + return distanta_euclidiana_la_patrat(V[st], V[dr]); + } else if (dr - st == 2) { + std::sort(V.begin() + st, V.begin() + dr + 1, cmp); + return std::min(distanta_euclidiana_la_patrat(V[st], V[st + 1]), + std::min(distanta_euclidiana_la_patrat(V[st], V[st + 2]), + distanta_euclidiana_la_patrat(V[st + 1], V[st + 2]))); + } + + int mij = (st + dr) / 2; + + int mij_x = V[mij].first; + + long long d = std::min(Solve(st, mij), Solve(mij + 1, dr)); + + auto delta = static_cast(std::ceil(std::sqrt(d))); + + interclasare(st, mij, dr); + + std::vector > A; + + for (int i = st; i <= dr; i++) { + if (std::abs((V[i].first - mij_x)) <= delta) + A.push_back(V[i]); + } + for (int i = 0; i < A.size(); i++) + for (int j = i + 1; j <= i + 7 && j < A.size(); j++) + d = std::min(d, distanta_euclidiana_la_patrat(A[i], A[j])); + + return d; +} + +void interclasare(int start, int m, int end) { + std::vector > Aux; + int i = start, j = m + 1, k = 0, nr = 0; + + while ((i <= m) && (j <= end)) { + if (V[i].second <= V[j].second) { + Aux.push_back(V[i]); + i++; + } else { + Aux.push_back(V[j]); + j++; + } + k++; + } + while (i <= m) { + Aux.push_back(V[i]); + k++; + i++; + } + while (j <= end) { + Aux.push_back(V[j]); + k++; + j++; + } + for (i = start; i <= end; i++) + V[i] = Aux[i - start]; +} + +int main() { + citire(); + + sort(V.begin(), V.end()); + + fout << std::fixed << std::setprecision(6) << sqrt(Solve(0, n-1)) << '\n'; + + return 0; +} diff --git a/cmap/divide_conquer/e1de68a8-5380-492e-b0e5-f7a5dbc4b790.cpp b/cmap/divide_conquer/e1de68a8-5380-492e-b0e5-f7a5dbc4b790.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4fc260bb5956864ce6657bfc17ab3322cec877 --- /dev/null +++ b/cmap/divide_conquer/e1de68a8-5380-492e-b0e5-f7a5dbc4b790.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF = 0x7FFFFFFFFFFFFFFFLL; + +struct punct + +{ + + int x, y; + +} P[100001], Tmp[100001]; + +int N; + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + +inline long long sqr(long long v) + +{ + + return v * v; + +} + +long long dist2(const punct&A, const punct&B) + +{ + + return sqr(A.x - B.x) + sqr(A.y - B.y); + +} + +long long distmin(int p, int u) + +{ + + long long dmin = INF; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long d = dist2(P[i], P[j]); + + dmin = max(d, dmin); + + } + + return dmin; + +} + +long long calcdmin(int p, int u) + +{ + + if(u - p < 3) + + return distmin(p, u); + + int m = p + (u - p) / 2; + + long long d = min(calcdmin(p, m), calcdmin(m, u)); + + int k = 0; + + for(int i = p; i <= u; i++) + + if(sqr(P[i].x - P[m].x) < d) + + Tmp[++k] = P[i]; + + sort(Tmp + 1, Tmp + k + 1, compy); + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && sqr(Tmp[j].y - Tmp[i].y) < d; j++) + + { + + long long dc = dist2(Tmp[i], Tmp[j]); + + d = min(dc, d); + + } + + return d; + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdmin(1, N)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/e270966d-d449-4398-8afd-45d91b939726.cpp b/cmap/divide_conquer/e270966d-d449-4398-8afd-45d91b939726.cpp new file mode 100644 index 0000000000000000000000000000000000000000..109addafbd9994fb48b5dd4a145901ada15c01ca --- /dev/null +++ b/cmap/divide_conquer/e270966d-d449-4398-8afd-45d91b939726.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include + + + + +struct Point2D { + + double X, Y; + +}; + + + +bool myComp(const Point2D& PointA, const Point2D& PointB) { + + if (PointA.X < PointB.X) + + return true; + + if (PointA.X == PointB.X) + + if (PointA.Y <= PointB.Y) + + return true; + + return false; + +} + + + +bool myCompY(const Point2D& PointA, const Point2D& PointB) { + + if (PointA.Y < PointB.Y) + + return true; + + return false; + +} + + + +double distance(const Point2D& PointA, const Point2D& PointB) { + + return std::pow(PointB.X - PointA.X, 2) + std::pow(PointB.Y - PointA.Y, 2); + +} + + + +double divide(std::vector::iterator itBegin, std::vector::iterator itEnd) { + + if (itEnd - itBegin <= 3) { + + double result = DBL_MAX; + + for (auto it1 = itBegin; it1 < itEnd; ++it1) { + + for (auto it2 = it1 + 1; it2 < itEnd; ++it2) { + + if (it1->Y > it2->Y) + + std::iter_swap(it1, it2); + + result = std::min(result, distance(*it1, *it2)); + + } + + } + + return result; + + } + + auto itMiddle = itBegin + (itEnd - itBegin) / 2; + + double result = std::min(divide(itBegin, itMiddle), divide(itMiddle, itEnd)); + + std::vector aux(itEnd - itBegin); + + std::merge(itBegin, itMiddle, itMiddle, itEnd, aux.begin(), myCompY); + + std::copy(aux.begin(), aux.end(), itBegin); + + aux.clear(); + + for (auto it = itBegin; it < itEnd; ++it) + + if (std::abs(it->X - itMiddle->X) <= result) + + aux.push_back(*it); + + for (auto it1 = aux.begin(); it1 < aux.end(); ++it1) + + for (auto it2 = it1 + 1; it2 < aux.end() && it2 - it1 < 8; ++it2) + + result = std::min(result, distance(*it1, *it2)); + + aux.clear(); + + return result; + +} + + + +int main() { + + std::ifstream f("cmap.in"); + + std::vector Points; + + int n; + + f >> n; + + for (int i = 0; i < n; ++i) { + + Point2D newPoint; + + f >> newPoint.X >> newPoint.Y; + + Points.push_back(newPoint); + + } + + f.close(); + + std::sort(Points.begin(), Points.end(), myComp); + + double result = divide(Points.begin(), Points.end()); + + std::ofstream g("cmap.out"); + + g << std::fixed << std::setprecision(6) << std::sqrt(result); + + g.close(); + + Points.clear(); + + return 0; + +} diff --git a/cmap/divide_conquer/e494ae07-d4b2-475e-b310-02f712480704.cpp b/cmap/divide_conquer/e494ae07-d4b2-475e-b310-02f712480704.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4da8a9c99e742435980aaed370d7cac41167952 --- /dev/null +++ b/cmap/divide_conquer/e494ae07-d4b2-475e-b310-02f712480704.cpp @@ -0,0 +1,309 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +struct punct + +{ + + int x,y; + +}; + +vector c; + +void citire(int &n,vector &a,vector &b) + +{ + + ifstream fin("cmap.in"); + + fin>>n; + + punct aux; + + int i; + + for(i=0;i>aux.x>>aux.y; + + a.push_back(aux); + + + + } + + fin.close(); + +} + +long long sqr(int a) + +{ + + return 1LL*a*a; + +} + + long long distanta(const punct a, const punct b){ + + return sqr(a.x-b.x)+sqr(a.y-b.y); + +} + +class compar1 + +{ + +public: + + bool operator() (const punct &a,const punct &b) + + { + + return a.y &a,vector &b,int n,int m,int mid,int dreapta,long long dist) + +{ + + int i=n,j=mid+1; + + vector c; + + while(i<=mid && j<=m) + + { + + if(a[i].y<=a[j].y) + + { + + if(dreapta-a[i].x<=dist) + + b.push_back(a[i]); + + c.push_back(a[i]); + + i++; + + } + + else + + { + + if(dreapta-a[j].x<=dist) + + b.push_back(a[j]); + + c.push_back(a[j]); + + j++; + + } + + + + } + + while(i<=mid) + + { + + if(dreapta-a[i].x<=dist) + + b.push_back(a[i]); + + c.push_back(a[i]); + + i++; + + } + + while(j<=m) + + { + + if(dreapta-a[j].x<=dist) + + b.push_back(a[j]); + + c.push_back(a[j]); + + j++; + + } + + i=n; + + j=0; + + while(j &a,vector &b,int i,int j) + +{ + + if(i>=j) + + return 2e81; + + + + if(j-i==1){ + + if(a[i].y>a[i+1].y) + + swap(a[i],a[i+1]); + + return distanta(a[i],a[i+1]); + + } + + int mid=(i+j)/2; + + long long d1=rezolva(a,b,i,mid); + + long long d2=rezolva(a,b,mid+1,j); + + long long d=minn(d1,d2); + + + + int dr=a[mid].x; + + interclasare(a,b,i,j,mid,dr,d); + + + + for(int k=0;kdistanta(b[k],b[l]) ){ + + d=distanta(b[k],b[l]); + + } + + + + b.clear(); + + + + return d; + + + +} + +int main() + +{ + + + + int n; + + vector a,b; + + citire(n,a,b); + + compar c; + + + + sort(a.begin(),a.end(),c); + + int i; + + ofstream fout("cmap.out"); + + fout< +using namespace std; + +typedef long long ll; + +ll sqr(ll x) { + + return x * x; + +} + +struct point { + + int x, y; + + ll sqrdist(const point &that) { + + return sqr(x - that.x) + sqr(y - that.y); + + } + +}; + +const ll INF = 1e16; + +vectorp; + +int n; + +bool xcmp(const point &a, const point &b) { + + return a.x < b.x; + +} + +bool ycmp(const point &a, const point &b) { + + return a.y < b.y; + +} + +ll closest_pair(int st, int dr) { + + ll res = INF; + + if (dr - st + 1 <= 3) { + + for (int i = st; i <= dr; ++i) { + + for (int j = i + 1; j <= dr; ++j) { + + res = min(res, p[i].sqrdist(p[j])); + + } + + } + + return res; + + } + + int mid = (st + dr) / 2; + + res = min(closest_pair(st, mid), closest_pair(mid + 1, dr)); + + vectoruseful; + + for (int i = st; i <= dr; ++i) { + + ll horizontal = sqr(abs(p[mid].x - p[i].x)); + + if (horizontal < res) { + + useful.push_back(p[i]); + + } + + } + + sort(useful.begin(), useful.end(), ycmp); + + for (int i = 0; i < useful.size(); ++i) { + + auto a = useful[i]; + + for (int j = i + 1; j < useful.size() && j - i + 1 <= 8; ++j) { + + auto b = useful[j]; + + res = min(res, a.sqrdist(b)); + + } + + } + + return res; + +} + + + +int main() { + + freopen("cmap.in", "r", stdin); + + freopen("cmap.out", "w", stdout); + + + + cin >> n; + + p = vector(n); + + for (int i = 0; i < n; ++i) + + cin >> p[i].x >> p[i].y; + + sort(p.begin(), p.end(), xcmp); + + long double answer = sqrt((long double)closest_pair(0, n - 1)); + + cout << fixed << setprecision(7) << answer; + + return 0; + +} diff --git a/cmap/divide_conquer/e57fe3e7-cb4c-44d0-b533-94b90219bc90.cpp b/cmap/divide_conquer/e57fe3e7-cb4c-44d0-b533-94b90219bc90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caaf7fac126f81ef62af7c6d2965c9af756382b9 --- /dev/null +++ b/cmap/divide_conquer/e57fe3e7-cb4c-44d0-b533-94b90219bc90.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +long long INF = 4e18; +vector > v, x, y; +int N; + +long long dist(pair a, pair b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +long long solve(int st, int dr) { + if (st >= dr - 1) return INF; + if (dr - st == 2) { + if (y[st] > y[st + 1]) { + swap(y[st], y[st + 1]); + } + return dist(x[st], x[st + 1]); + } + int mij = (st + dr) / 2; + long long ret = min(solve(st, mij), solve(mij, dr)); + merge(y.begin() + st, y.begin() + mij, y.begin() + mij, y.begin() + dr, v.begin()); + copy(v.begin(), v.begin() + (dr - st), y.begin() + st); + int nr = 0; + for (int i=st; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +typedef pair Point; + + + +int N; + + + +Point points[100010], points_copy[100010]; + + + +vector mrg; + + + +double distance(const Point& p1, const Point& p2) + +{ + + return sqrt((p1.first - p2.first)*(p1.first - p2.first) + (p1.second - p2.second)*(p1.second - p2.second)); + +} + + + +double closest_pair_of_points(int x,int y) + +{ + + + + if (y - x > 3) + + { + + int mid = (x + y) / 2; + + double d1 = closest_pair_of_points(x, mid); + + + + double d2 = closest_pair_of_points(mid + 1, y); + + + + merge(points_copy + x, points_copy + mid + 1, points_copy + mid + 1, points_copy + y + 1, back_inserter(mrg)); + + + + double d = min(d1, d2); + + vector vec; + + + + for (int i=0;i> N; + + + + for (int i = 1; i <= N; ++i) + + { + + int x, y; + + + + in >> x >> y; + + + + points[i] = { x,y }; + + + + } + + + + mrg.reserve(100000); + + + + sort(points + 1, points + N + 1); + + + + for (int i = 1; i <= N; ++i) + + points_copy[i] = make_pair(points[i].second, points[i].first); + + + + out << fixed << setprecision(6) << closest_pair_of_points(1, N); + + + + return 0; + +} diff --git a/cmap/divide_conquer/e64aee6c-bb19-463f-8071-eb41466a3b50.cpp b/cmap/divide_conquer/e64aee6c-bb19-463f-8071-eb41466a3b50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf2bf5e411f69310a93a26ca1962563b51f16636 --- /dev/null +++ b/cmap/divide_conquer/e64aee6c-bb19-463f-8071-eb41466a3b50.cpp @@ -0,0 +1,278 @@ +#include +#include +#include +#include +#include +#include. +#include +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +class POINT + +{ + +public : + + long long x,y; + + + + + + + + friend bool mycompx(POINT a,POINT b); + + friend bool mycompy(POINT a,POINT b); + + + + + + friend long long point_distance(POINT a, POINT b); + + + +}; + + + +bool mycompy(POINT a,POINT b) + +{ + + return a.y < b.y; + +} + + + + + +bool mycompx(POINT a,POINT b) + +{ + + return a.x < b.x; + +} + + + +long long squared(long long x) + +{ + + return x*x; + + + +} + + + + + +POINT v_sortedx[100000 + 5]; + +POINT strip[100000 + 5]; + + + + + +int n; + + + +void read_data() + +{ + + in >> n; + + + + for(int i = 1 ; i<=n ; ++i) + + { + + in>>v_sortedx[i].x; + + in>>v_sortedx[i].y; + + } + + + +} + + + +long long point_distance(POINT a, POINT b) + +{ + + long long lat_1 = b.x - a.x; + + long long lat_2 = b.y - a.y; + + + + return lat_1*lat_1 + lat_2*lat_2; + +} +long long small_dist(int left,int right) + +{ + + + + int i,j; + + long long dist = 0xFFFFFFFFFFFFF; + + + + for(i = left ; i < right ; ++i) + + for( j = i+1 ; j <=right ; ++j) + + { + + long long temp = point_distance(v_sortedx[i],v_sortedx[j]); + + if( temp < dist) + + dist = temp; + + } + + + + return dist; + +} + + + + + +long long min_distance(int left,int right) + +{ + + if(right - left < 3) + + { + + return small_dist(left,right); + + } + + + + + + int m = (left + right ) / 2; + + + + long long d1 = min_distance(left,m); + + long long d2 = min_distance(m,right); + + + + long long d = d1 < d2 ? d1 : d2; + + + + int k = 0; + + + + for(int i = left ; i<=right ; ++i) + + { + + if(squared(v_sortedx[i].x - v_sortedx[m].x) < d) + + strip[++k] = v_sortedx[i]; + + } + + + + sort(strip+1,strip+k+1,mycompy); + + + + int l; + + for(int i = 1 ; i<=k ; ++i) + + { + + l = 0; + + for(int j = i + 1; l<7 && j <=k ; ++j,++l) + + { + + long long dist = point_distance(strip[i],strip[j]); + + if(dist < d) + + d = dist; + + } + + } + + + + return d; + +} +int main() + +{ + + read_data(); + + + + + + sort(v_sortedx+1,v_sortedx+n+1,mycompx); + + + + long long d; + + + + d = min_distance(1,n); + + + + out< + + + + + + +using namespace std; + + + +void citire (int &n, vector > &PCT) + +{ + + ifstream f("cmap.in"); + + f >> n; + + for(int i = 0; i < n; i++) + + { + + int first, second; + + f >> first >> second; + + PCT.push_back (make_pair (first, second)); + + } + + f.close(); + +} + + + +double distanta (pair A, pair B) + +{ + + double a = A.first - B.first; + + double b = A.second - B.second; + + return sqrt(a * a + b * b); + +} + + + +bool cmp1 (const pair &A, const pair &B) + +{ + + return A.first < B.first; + +} + +bool cmp2 (const pair &A, const pair &B) + +{ + + return A.second < B.second; + +} + + + +double di (int st, int dr, vector > &PCT, vector > &PCTsortateY) + +{ + + if(dr - st == 1) + + return INT_MAX; + + else if(dr - st == 2) + + { + + if (PCTsortateY[st].second > PCTsortateY[st + 1].second) + + swap(PCTsortateY[st], PCTsortateY[st + 1]); + + return distanta(PCT[0], PCT[1]); + + } + + + + int m = (st + dr) / 2; + + double dist = min (di(st, m, PCT, PCTsortateY), di(m, dr, PCT, PCTsortateY)); + + + + vector> temp(dr - st); + + merge(PCTsortateY.begin() + st, PCTsortateY.begin() + m, PCTsortateY.begin() + m, PCTsortateY.begin() + dr, temp.begin(), cmp2); + + copy(temp.begin(), temp.begin() + (dr - st), PCTsortateY.begin() + st); + + + + vector> temp2; + + for(unsigned i = st; i < dr; i++) + + if(abs(PCTsortateY[i].first - PCT[m].first) <= dist) + + temp2.push_back(PCTsortateY[i]); + + + + for(unsigned i = 0; i < temp2.size(); i++) + + for(unsigned j = i + 1; j < temp2.size() && temp2[j].second - temp2[i].second <= dist; j++) + + dist = min(dist, distanta (temp2[i], temp2[j])); + + + + return dist; + +} + + + +int main() + +{ + + int n; + + vector > PCT; + + + + citire(n, PCT); + + sort(PCT.begin(), PCT.end(), cmp1); + + vector > PCTsortateY = PCT; + + + + ofstream g("cmap.out"); + + g << fixed << setprecision(6) << di(0, n, PCT, PCTsortateY); + + g.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/e789beed-ee20-4468-8301-df9275282a7f.cpp b/cmap/divide_conquer/e789beed-ee20-4468-8301-df9275282a7f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1174e6f95955141c9e739b891897e0c3617cee11 --- /dev/null +++ b/cmap/divide_conquer/e789beed-ee20-4468-8301-df9275282a7f.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +struct punct + +{ + + int x,y; + +}; + +punct a[100005]; + +bool cmpY(const punct &a, const punct &b) + +{ + + return a.y=0) + + return x; + + else + + return -x; + +} + +long long cmap(int st,int dr) + +{ + + + + if(dr-st<=4) + + { + + + + long long sol=1LL<<60; + + for(int i=st;i<=dr;i++) + + { + + for(int j=i+1;j<=dr;j++) + + { + + sol=min(sol,dist(a[i],a[j])); + + } + + } + + return sol; + + } + + int mij=(st+dr)/2; + + long long minim=min(cmap(st,mij),cmap(mij+1,dr)); + + int nr=0; + + punct b[100005]; + + for(int i=st;i<=dr;i++) + + { + + if(modul(a[i].x-a[mij].x)>n; + + for(i=1;i<=n;i++) + + { + + f>>a[i].x>>a[i].y; + + } + + + + sort(a+1,a+n+1,cmpX); + + g< +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct {long long int x,y;};punct v[100005],posibil[100005]; + +int n; + +bool cmp1(punct a,punct b) + +{ + + return a.xb.y; + +} + +long long det_dist(punct a,punct b) + +{ + + long long sol=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y); + + return sol; + +} + +long long solve(int st,int dr) + +{ + + if(dr-st<=3) + + { + + long long minim=100000000000000; + + for(int i=st;ip) + + { + + dist_min=p; + + } + + + + } + + } + + if(dist_min>n; + +for(int i=1;i<=n;i++) f>>v[i].x>>v[i].y; + +sort(v+1,v+n+1,cmp1); + +long long rez=solve(1,n); + +double rad=sqrt(rez); + +g< +#include +#include +#include +#include +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct + +{ + + int x; + + int y; + +}; + +punct v[100001],a[100001]; + +int n; + +bool comparax(const punct &A,const punct &B) + +{ + + return A.xd1)dg=d1; + + } + + } + + return dg; + + + +} + + + +int main() + +{ + + in>>n; + + for(int i=1;i<=n;i++) + + in>>v[i].x>>v[i].y; + + sort(v+1,v+n+1,comparax); + + out< +#include +#include +#include +#include +#include +using namespace std; + +struct Point{ + int x, y; +}; + +const double inf = 4000000000.0; + +double dist(const Point &A, const Point &B) { + return sqrt((1ll * A.x - B.x) * (A.x - B.x) + (1ll * A.y - B.y) * (A.y - B.y)); +} + +double solve_median(int m, double d, int l, int r, vector &p_y, int x) { + vector v; + for (int i = l; i <= r; ++i) + if (abs(x - p_y[i].x) <= d) + v.push_back(p_y[i]); + for (int i = 0; i < v.size(); ++i) + for (int j = i + 1; j < v.size() && j <= i + 5; ++j) { + d = min(d, dist(v[i], v[j])); + } + return d; +} + +double solve(int l, int r, vector &p_x, vector &p_y) { + if (l >= r) + return inf; + if (r - l + 1 == 2) { + if (p_y[l].y > p_y[r].y) + swap(p_y[l], p_y[r]); + return dist(p_y[l], p_y[r]); + } + int m = (l + r) / 2; + double min_left = solve(l, m, p_x, p_y); + double min_right = solve(m + 1, r, p_x, p_y); + double d = min(min_left, min_right); + vector v(r - l + 1); + merge(p_y.begin() + l, p_y.begin() + m + 1, p_y.begin() + m + 1, p_y.begin() + r + 1, v.begin(), + [](const Point &A, const Point &B) -> bool { + return A.y < B.y; + }); + copy(v.begin(), v.end(), p_y.begin() + l); + d = min(d, solve_median(m, d, l, r, p_y, p_x[m].x)); + return d; +} + +int main() { + ifstream cin("cmap.in"); + ofstream cout("cmap.out"); + + int n; + cin >> n; + vector p_x(n), p_y(n); + for (int i = 0; i < n; ++i) { + cin >> p_x[i].x >> p_x[i].y; + } + sort(p_x.begin(), p_x.end(), [](const Point &A, const Point &B) -> bool { + return A.x < B.x; + }); + copy(p_x.begin(), p_x.end(), p_y.begin()); + double ans = solve(0, n - 1, p_x, p_y); + cout << fixed << setprecision(6) << ans << "\n"; + return 0; +} diff --git a/cmap/divide_conquer/e95e54fe-9ba3-4766-8914-e30299514e9a.cpp b/cmap/divide_conquer/e95e54fe-9ba3-4766-8914-e30299514e9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06cd2966039cb90d51ca5711fa243c3371ae6eb1 --- /dev/null +++ b/cmap/divide_conquer/e95e54fe-9ba3-4766-8914-e30299514e9a.cpp @@ -0,0 +1,116 @@ +#include +#include #include +#include +#include +#include + + + + + + +using namespace std; + +ifstream f ("cmap.in"); + +ofstream g ("cmap.out"); + + + +typedef long long ll; + +typedef vector < pair < ll, ll > > vect; + +vect X, Y, V(100005); + +const ll INF = 4e18; + +int n; + + + +ll dist(const pair < ll, ll > & x, const pair < ll, ll > & y) { + + return (x.first - y.first) * (x.first - y.first) + (x.second - y.second) * (x.second - y.second); + +} + + + +ll ans(int st, int dr) + +{ + + if (st >= dr - 1) return INF; + + + + if (dr - st == 2) + + { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + else + + { + + ll a = dist(X[st], X[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + } + + int mid = (st + dr) / 2; + + ll best = min(ans(st, mid), ans(mid, dr)); + + sort(Y.begin() + st, Y.begin() + dr); + + int m = 0; + + for (int i = st; i < dr; i++) + + if (abs(Y[i].second - X[mid].first) <= best) + + V[m++] = Y[i]; + + for (int i = 0; i < m; i++) + + for (int j = i + 1; j < m && j - i < 8; j++) + + best = min(best, dist(V[i], V[j])); + + return best; + +} + + + +int main() + +{ + + f>>n; + + X.resize(n); Y.resize(n); + + for (int i = 0; i < n; i++) + + f>>X[i].first>>X[i].second; + + sort(X.begin(), X.end()); + + for (int i = 0; i < n; i++) + + Y[i] = X[i]; + + g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct punct + +{ + + int x,y; + +}v[100000],v2[100000]; + + + +int n; + + + +bool sortare_x (punct a,punct b) + +{ + + return a.x>n; + + + + for (int i=0; i>v[i].x>>v[i].y; + + + + sort(v,v+n,sortare_x); + + + + g < + + + + +using namespace std; + + + + ifstream f("cmap.in"); + + ofstream g("cmap.out"); + + + +vector< pair > x, y; + +vector< pair > v(100002); + + + +long long getDist(pair &a, pair &b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +long long solve(int st, int dr) + +{ + + if(dr - st <= 1){ + + return 10e18; + + } + + if(dr - st == 2){ + + if (y[st] > y[st + 1]) + + swap(y[st], y[st + 1]); + + return getDist(x[st], x[st + 1]); + + } + + long long mid = (st + dr) / 2; + + long long result = min(solve(st, mid), solve(mid, dr)); + + + + sort(y.begin() + st, y.begin() + dr); + + + + long long k = 0; + + for (int i = st; i < dr; i++) + + if (abs(y[i].second - x[mid].first) <= result) + + v[k++] = y[i]; + + for (int i = 0; i < k - 1; i++) { + + for (int j = i + 1; j < k && j - i < 8; j++) + + result = min(result, getDist(v[i], v[j])); + + } + + return result; + +} + + + +int main() + +{ + + int n; + + f>>n; + + x.resize(n); y.resize(n); + + for(int i = 0 ; i < n ; i++){ + + f>>x[i].first>>x[i].second; + + } + + + + sort(x.begin(), x.end()); + + + + for(int i = 0 ; i < n ; i++){ + + y[i].second = x[i].first; + + y[i].first = x[i].second; + + } + + + + long long sol = solve(0, n); + + g< +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct point { + + int x, y; + + double distance(point p) const { + + return sqrt((double)(x - p.x)*(x - p.x) + (double)(y - p.y)*(y - p.y)); + + } + +}; + + + +istream& operator>> (istream& is, point& p) { + + is >> p.x >> p.y; + + return is; + +} + + + +int n; + +point v[100005]; + + + +double caut(int l, int r) { + + if (r - l + 1 == 3) { + + return min(v[l].distance(v[l + 1]), min(v[l].distance(v[r]), v[r].distance(v[l + 1]))); + + } + + else if (r - l + 1 == 2) { + + return v[l].distance(v[r]); + + } + + else if (r == l) { + + return 10000000; + + } + + int m = (l + r) / 2; + + double ret = min(caut(l, m), caut(m + 1, r)); + + vector middlePoints; + + for (int i = l; i <= r; i++) + + if(abs(v[i].x - v[m].x) <= ret) + + middlePoints.push_back(v[i]); + + sort(middlePoints.begin(), middlePoints.end(), [](const point& l, const point& r) { return l.y < r.y; }); + + for (int i = 0; i < middlePoints.size(); i++) { + + for (int j = i - 1; j >= 0 && i - j <= 7; j--) { + + double dist = middlePoints[i].distance(middlePoints[j]); + + ret = min(ret, dist); + + } + + for (int j = i + 1; j < middlePoints.size() && j - i <= 7; j++) { + + double dist = middlePoints[i].distance(middlePoints[j]); + + ret = min(ret, dist); + + } + + } + + return ret; + +} + + + +int main() { + + in >> n; + + for (int i = 1; i <= n; i++) + + in >> v[i]; + + sort(v + 1, v + n + 1, [](const point& l, const point& r) { return l.x < r.x; }); + + out << fixed << setprecision(6) << caut(1, n) << '\n'; + +} diff --git a/cmap/divide_conquer/ea8cb379-b497-4456-b513-a45d3330bfb9.cpp b/cmap/divide_conquer/ea8cb379-b497-4456-b513-a45d3330bfb9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5316fde4432620f57a387d4863f308a445dcf9a --- /dev/null +++ b/cmap/divide_conquer/ea8cb379-b497-4456-b513-a45d3330bfb9.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const char iname[] = "cmap.in"; + +const char oname[] = "cmap.out"; + + + +typedef long long i64; + + + +const int MAX_N = 100005; + +const i64 INF = 4e18; + + + +vector < pair > V(MAX_N), X, Y; + + + +i64 dist(const pair & a, const pair & b) { + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); + +} + + + +i64 go(int st, int dr, vector < pair >& X, vector < pair >& Y) { + + if (st >= dr - 1) + + return INF; + + else if (dr - st == 2) { + + if (Y[st] > Y[st + 1]) + + swap(Y[st], Y[st + 1]); + + return dist(X[st], X[st + 1]); + + } + + int mid = (st + dr) / 2; + + i64 best = min(go(st, mid, X, Y), go(mid, dr, X, Y)); + + + + merge(Y.begin() + st, Y.begin() + mid, Y.begin() + mid, Y.begin() + dr, V.begin()); + + copy(V.begin(), V.begin() + (dr - st), Y.begin() + st); + + + + int v_size = 0; + + for (int i = st; i < dr; ++ i) if (abs(Y[i].second - X[mid].first) <= best) + + V[v_size ++] = Y[i]; + + for (int i = 0; i < v_size - 1; ++ i) { + + for (int j = i + 1; j < v_size && j - i < 8; ++ j) + + best = min(best, dist(V[i], V[j])); + + } + + return best; + +} + + + +int main(void) { + + int n; + + ifstream in(iname); + + assert(in >> n); + + assert(1 <= n && n <= 100000); + + + + X.resize(n), Y.resize(n); + + for (int i = 0; i < (int) X.size(); ++ i) { + + assert(in >> X[i].first >> X[i].second); + + assert(-1000000000 <= X[i].first && X[i].first <= +1000000000); + + assert(-1000000000 <= X[i].second && X[i].second <= +1000000000); + + } + + in.close(); + + + + sort(X.begin(), X.end()); + + for (int i = 0; i < (int) X.size(); ++ i) + + Y[i] = make_pair(X[i].second, X[i].first); + + + + ofstream out(oname); + + out << fixed << setprecision(6) << sqrt(go(0, (int) X.size(), X, Y)) << "\n"; + + out.close(); + + return 0; + +} diff --git a/cmap/divide_conquer/eadcc308-44c1-491a-b87e-0db8d26fc43b.cpp b/cmap/divide_conquer/eadcc308-44c1-491a-b87e-0db8d26fc43b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8292651b86c4a42b421c8550aefe2fdb144f1cfc --- /dev/null +++ b/cmap/divide_conquer/eadcc308-44c1-491a-b87e-0db8d26fc43b.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + + + +int n; + +vector > V; + + + +void citire(const char* fileName) + +{ + + ifstream in (fileName); + + in >> n; + + int x, y; + + for (int i = 0; i < n; ++i) + + { + + in >> x >> y; + + V.push_back ({x, y}); + + } + + + + in.close(); + +} + + + +long long dist2 (pair p1, pair p2) + +{ + + return (long long) (p1.first - p2.first) * (p1.first - p2.first) + (long long) (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +long long solutie (int st, int dr) + +{ + + if (dr - st == 1) + + return dist2 (V[st], V[dr]); + + + + else if (dr - st == 2) + + return min(dist2(V[st], V[st + 1]), min(dist2(V[st], V[st + 2]), dist2(V[st + 1], V[st + 2]))); + + + + int mij = (st + dr) / 2; + + + + long long d = min(solutie (st, mij), solutie (mij + 1, dr)); + + + + vector> M; + + + + int delta = (int) (ceil (sqrt (d))); + + + + for (int i = st; i <= dr; i++) + + if (abs(V[i].first - V[mij].first) <= delta) + + M.push_back(V[i]); + + + + sort (M.begin(), M.end(), [](pair p1, pair p2) -> bool { + + return p1.second < p2.second; + + }); + + + + for (unsigned i = 0; i < M.size(); i++) + + for (unsigned j = i + 1; j <= i + 7 && j < M.size(); j++) + + d = min(d, dist2(M[i], M[j])); + + + + return d; + +} + + + +int main() + +{ + + citire("cmap.in"); + + + + sort(V.begin(), V.end()); + + + + ofstream out ("cmap.out"); + + out << fixed << setprecision(6) << sqrt (solutie (0, n-1)) << '\n'; + + out.close(); + + + + return 0; + +} diff --git a/cmap/divide_conquer/eb71acd4-ce18-4b85-89e4-16dd3bd4c846.cpp b/cmap/divide_conquer/eb71acd4-ce18-4b85-89e4-16dd3bd4c846.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f7f6cd5b71d70453ecfc27409514859597dfdd3 --- /dev/null +++ b/cmap/divide_conquer/eb71acd4-ce18-4b85-89e4-16dd3bd4c846.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + +pair v[100010],w[100010],d[100010]; + +int n,i; + +long long dist (pair a, pair b){ + + return (a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second); + +} + +long long modul (long long n){ + + if (n < 0) + + return -n; + + return n; + +} + +void interclaseaza (int st, int mid, int dr){ + + int i = st, j = mid+1; + + int el = st-1; + + while (i <= mid && j <= dr){ + + if (v[i].second < v[j].second){ + + d[++el] = v[i]; + + i++; + + } else { + + d[++el] = v[j]; + + j++; + + } + + } + + for (;i<=mid;i++) + + d[++el] = v[i]; + + for (;j<=dr;j++) + + d[++el] = v[j]; + + for (int i=st;i<=dr;i++) + + v[i] = d[i]; + +} + +long long solve (int st, int dr){ + + if (dr-st == 1){ + + interclaseaza (st,st,dr); + + return dist (v[st],v[dr]); + + } + + if (dr-st == 2){ + + long long sol = dist (v[st],v[st+1]); + + sol = min (sol,dist(v[st],v[st+2])); + + sol = min (sol,dist(v[st+1],v[st+2])); + + + + interclaseaza (st,st,st+1); + + interclaseaza (st,st+1,st+2); + + return sol; + + } + + int mid = (st+dr)>>1; + + long long sol_st = solve (st,mid); + + long long sol_dr = solve (mid+1,dr); + + + + interclaseaza(st,mid,dr); + + + + long long sol = min (sol_st,sol_dr); + + + + int k = 0; + + for (int i=st;i<=dr;i++){ + + if (modul (v[i].first - v[mid].first) <= sol) + + w[++k] = v[i]; + + } + + for (int i=1;i<=k;i++) + + for (int j=i+1;j<=min(k,i+7);j++) + + sol = min (sol,dist(w[i],w[j])); + + + + return sol; + +} + +int main (){ + + + + fin>>n; + + for (i=1;i<=n;i++) + + fin>>v[i].first>>v[i].second; + + + + sort (v+1,v+n+1); + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + +const long long INF = 4e18; + + + +struct point + +{ + + int x, y; + +}; + + + +int n; + + + +vector < point > p; + + + +bool cmp(point a, point b) + +{ + + if (a.x < b.x) + + return true; + + + + if (a.x == b.x && a.y < b.y) + + return true; + + + + return false; + +} + + + +bool cmpAux(point a, point b) + +{ + + if (a.y < b.y) + + return true; + + + + if (a.y == b.y && a.x < b.x) + + return true; + + + + return false; + +} + + + +double dist(point a, point b) + +{ + + long long dx = a.x - b.x; + + long long dy = a.y - b.y; + + return sqrt(dx * dx + dy * dy); + +} + + + +double distMin(vector < point > &p, int st, int dr) + +{ + + if (dr - st <= 2) + + { + + double dmin = INF; + + + + for (int i=st; i aux; + + + + for (int i=st; i<=dr; i++) + + if (abs(D - p[i].x) <= dmin) + + aux.push_back(p[i]); + + + + sort(aux.begin(), aux.end(), cmpAux); + + + + int n = aux.size(); + + for (int i=0; i> n; + + + + p.resize(n); + + + + for (int i=0; i> p[i].x >> p[i].y; + + + + sort(p.begin(), p.end(), cmp); + + + + g << fixed << setprecision(6) << distMin(p, 0, n - 1); + + + + return 0; + +} diff --git a/cmap/divide_conquer/eda85295-03c0-4321-aac3-021fc80010d4.cpp b/cmap/divide_conquer/eda85295-03c0-4321-aac3-021fc80010d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eab9f30b788aa186a5c9a32ec37b4951cd30a8a9 --- /dev/null +++ b/cmap/divide_conquer/eda85295-03c0-4321-aac3-021fc80010d4.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct lol { + + long long x, y; + +}; + + + +int n, vf; + +lol a[100100], wek[100100]; + + + +long long dist(lol a, lol b) { + + return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); + +} + + + +bool cmp(lol a, lol b) { + + return a.x < b.x; + +} + + + +bool cmp2(lol a, lol b) { + + return a.y < b.y; + +} + + + +long long get(int st, int dr) { + + if (dr - st < 3) { + + long long ans = 1e18; + + for (int i = st; i < dr; i++) + + for (int j = i + 1; j <= dr; j++) + + ans = min(ans, dist(a[i], a[j])); + + return ans; + + } + + int mid = st + dr >> 1; + + long long bst = min(get(st, mid), get(mid, dr)); + + vf = 0; + + for (int i = st; i <= dr; i++) + + if ((a[i].x - a[mid].x) * (a[i].x - a[mid].x) <= bst) + + wek[++vf] = a[i]; + + sort(wek + 1, wek + vf + 1, cmp2); + + for (int i = 1; i <= vf; i++) + + for (int j = i + 1; j <= vf && j - i < 8; j++) + + bst = min(bst, dist(wek[i], wek[j])); + + return bst; + +} + + + +int main() { + + in >> n; + + for (int i = 1; i <= n; i++) + + in >> a[i].x >> a[i].y; + + sort(a + 1, a + n + 1, cmp); + + out << fixed << setprecision(6) << sqrt((double) get(1, n)); + + return 0; + +} diff --git a/cmap/divide_conquer/edd394f8-2bd9-4923-b0e0-22a3c05a0f30.cpp b/cmap/divide_conquer/edd394f8-2bd9-4923-b0e0-22a3c05a0f30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ab182d3d9b6a68242ee8a811f7a7fab9bf86fc6 --- /dev/null +++ b/cmap/divide_conquer/edd394f8-2bd9-4923-b0e0-22a3c05a0f30.cpp @@ -0,0 +1,287 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const long long INF = 1LL * 2e18; + + + +inline long long sqr(int x) { + + return 1LL * x * x; + +} + + + +class Point { + + + +public: + + + + int x, y; + + + + Point(){} + + Point(int x, int y) { + + this->x = x; + + this->y = y; + + } + + + + friend istream& operator>> (istream& in, Point& a) { + + in >> a.x >> a.y; + + } + + + + friend bool operator< (const Point a, const Point b) { + + return a.x < b.x; + + } + + + + friend long long dist(const Point a, const Point b) { + + return sqr(a.x - b.x) + sqr(a.y - b.y); + + } + +}; + + + +vector puncte, middlePoints, aux; +void customMerge(int st, int mid, int dr, int lineX, long long d) { + + + + int i = st, j = mid + 1; + + + + aux.clear(); + + while (i <= mid && j <= dr) { + + + + if (puncte[i].y <= puncte[j].y) { + + + + if (1LL * lineX - puncte[i].x < d) + + middlePoints.push_back(puncte[i]); + + + + aux.push_back(puncte[i]); + + ++i; + + } + + else { + + + + if (1LL * puncte[j].x - lineX < d) + + middlePoints.push_back(puncte[j]); + + + + aux.push_back(puncte[j]); + + ++j; + + } + + } + + + + while (i <= mid) { + + + + if (1LL * lineX - puncte[i].x < d) + + middlePoints.push_back(puncte[i]); + + + + aux.push_back(puncte[i]); + + ++i; + + } + + + + while (j <= dr) { + + + + if (1LL * puncte[j].x - lineX < d) + + middlePoints.push_back(puncte[j]); + + + + aux.push_back(puncte[j]); + + ++j; + + } + + + + for (int i = st, j = 0; j < aux.size(); ++i, ++j) { + + puncte[i] = aux[j]; + + } + +} + + + + + +long long closestPoints(int st, int dr) { + + + + + + if (st >= dr) { + + return INF; + + } + + + + if (st + 1 == dr) { + + + + if (puncte[st].y > puncte[dr].y) + + swap(puncte[st], puncte[dr]); + + + + return dist(puncte[st], puncte[dr]); + + } + + + + int mid = (st + dr) >> 1; + + + + + + long long d1 = closestPoints(st, mid); + + long long d2 = closestPoints(mid + 1, dr); + + long long d = min(d1, d2); + + + + + + customMerge(st, mid, dr, puncte[mid].x, d); + + + + + + for (int i = 0; i < middlePoints.size(); ++i) { + + int go = min(i + 8, (int)middlePoints.size() - 1); + + for (int j = i + 1; j <= go ; ++j) { + + d = min(d, dist(middlePoints[i], middlePoints[j])); + + } + + } + + middlePoints.clear(); + + return d; + +} + + + +int main() { + + + + int n; + + + + + + fin >> n; + + puncte.resize(n); + + + + for (int i = 0; i < n; ++i) { + + fin >> puncte[i]; + + } + + + + + + sort(puncte.begin(), puncte.end()); + + + + + + fout << fixed << setprecision(7) << sqrt(closestPoints(0, n - 1)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/edf4fc0d-af22-4a32-871d-df358502458f.cpp b/cmap/divide_conquer/edf4fc0d-af22-4a32-871d-df358502458f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc30ff08eaf27547de9693cbe30de7d7282780a3 --- /dev/null +++ b/cmap/divide_conquer/edf4fc0d-af22-4a32-871d-df358502458f.cpp @@ -0,0 +1,102 @@ +#include #include #include +#include #include #include + +using namespace std; + +FILE *f=fopen("cmap.in","r"); + +FILE *g=fopen("cmap.out","w"); + +typedef pair point; + +vector V(100005); + +vector X,Y; + +int N; + +int dist(point a,point b) + +{ + + return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second); + +} + +int getmin(int st,int dr) + +{ + + if(st>=dr-1) + + return (1LL<<63); + + if(st==dr-2) + + { + + if(Y[dr-1] + + +using namespace std; + + + +const long long inf=1e18; + + + +struct pct + +{ + + int x,y; + +}v[100010],v1[100010]; + + + +int cmp(pct a,pct b) + +{ + + return a.x=dr) return inf; + + if(st+1==dr) + + { + + if(v[dr].y +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF=9e18; + +const int MAXI=100005; + +vector > temp(MAXI); + + + +long long dist(const pair &p1,const pair &p2) + +{ + + return (long long)(p1.first-p2.first)*(p1.first-p2.first)+(long long)(p1.second-p2.second)*(p1.second-p2.second); + +} + +bool Sort(const pair &p1,const pair &p2) + +{ + + return p1.first > &v,vector > &y,int l,int r) + +{ if(l>=r-1) + + return INF; + + if(r-l==2) + + { + + if(y[l].first>y[l+1].first) + + swap(y[l],y[l+1]); + + return dist( v[l],v[l+1]); + + } + + int m=(l+r)/2; + + long long d,d1,d2,dis; + + int i,j; + + d1=minDistance(v,y,m,r); + + d2=minDistance(v,y,l,m); + + d=min(d1,d2); + + dis=(int) ceil(sqrt(d)); + + + + merge(y.begin() + l, y.begin() + m, y.begin() +m, y.begin() + r, temp.begin()); + + copy(temp.begin(), temp.begin() + (r - l), y.begin() + l); + + int temp_size=0; + + for(i=l;i<=r;i++) + + if(abs(y[i].second-v[m].first)<=dis) + + temp[temp_size++]=y[i]; + + + + for(i=0;i > v,z; + + f>>n; + + for(i=0;i>x>>y; + + v.push_back(make_pair(x,y)); + + } + + sort(v.begin(),v.end(),Sort); + + for(i=0;i +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const long long infinit = 4e18; + + + +struct triplet + +{ + + pair p1,p2; + + + + long long distanta; + +}; + + + + + +void interclasare(int p, int m, int q, vector< pair > & v) + +{ + + vector< pair > aux; + + + + int i = p, j = m; + + + + while(i a, pair b) + +{ + + return ((a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second)); + +} + + + +triplet DI(int st, int dr, vector< pair > & v, vector< pair > & u) + +{ + + if(dr-st < 2) + + { + + triplet inf; + + + + inf.distanta = infinit; + + + + inf.p1 = {infinit,infinit}; + + + + inf.p2 = {infinit,infinit}; + + + + return inf; + + } + + + + if(dr-st == 2) + + { + + if(u[st].first > u[dr-1].first) + + swap(u[st],u[dr-1]); + + + + triplet d; + + + + d.distanta = dist(v[st],v[dr-1]); + + + + d.p1 = v[st]; + + + + d.p2 = v[dr-1]; + + + + return d; + + } + + + + int m=(st+dr)/2; + + + + triplet D1 = DI(st, m, v, u); + + + + triplet D2 = DI(m, dr, v, u); + + + + triplet D_min; + + + + if(D1.distanta < D2.distanta) + + D_min = D1; + + else + + D_min = D2; + + + + interclasare(st, m, dr, u); + + + + vector< pair > proxim; + + + + for(int i=st ;i < dr; i++) + + if(abs(u[i].second-v[m].first) <= D_min.distanta) + + proxim.push_back(u[i]); + + + + for(int i=0; i < (int)proxim.size()-1; i++) + + for(int j=i+1; j < (int)proxim.size() && j-i<7 ; j++) + + if(D_min.distanta > dist(proxim[i],proxim[j])) + + { + + D_min.distanta = dist(proxim[i],proxim[j]); + + + + D_min.p1.first = proxim[i].second; + + + + D_min.p1.second = proxim[i].first; + + + + D_min.p2.first = proxim[j].second; + + + + D_min.p2.second = proxim[j].first; + + } + + + + return D_min; + +} + + + +int main() + +{ + + int n; + + + + fin>>n; + + + + vector< pair > v(n); + + + + for(int i=0; i>v[i].first>>v[i].second; + + + + sort(v.begin(), v.end()); + + + + vector< pair > u(n); + + + + for(int i=0; i +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +struct Point + +{ + + long long x,y; + +}p1_sol,p2_sol; + + + +void read(int &n, vector &v) + +{ + + fin>>n; + + v.resize(n); + + + + for(int i=0;i>v[i].x>>v[i].y; + +} + + + +bool cmp(Point a, Point b) + +{ + + return ((a.x &v) + +{ + + int i=st,j=m+1; + + vector aux; + + + + while(i<=m && j<=dr) + + { + + if(v[i].y &v) + +{ + + if(dr-st==1) + + { + + long long minim=distance(v[st],v[dr]); + + if(v[st].y>v[dr].y) + + swap(v[st],v[dr]); + + + + return minim; + + + + } + + else if(dr-st==2) + + { + + long long minim=min(distance(v[st],v[st+1]),min(distance(v[st],v[dr]),distance(v[dr],v[st+1]))); + merge_function(st,st,st+1,v); + + merge_function(st,st+1,dr,v); + + + + return minim; + + } + + else + + { + + int m=(st+dr)/2; + + long long mst=divide(st,m,v); + + long long mdr=divide(m+1,dr,v); + + + + long long minim=min(mst,mdr); + + merge_function(st,m,dr,v); + + + + vector aux; + + for(int i=st ; i<=dr ; i++) + + if(abs(v[i].x-v[m].x)<=minim) + + aux.push_back(v[i]); + + + + int siz=aux.size(); + + + + for(int i=0;i v; + + read(n,v); + + sort(v.begin(),v.end(),cmp); + + + + fout< +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int NMax = 1e5 + 5; + +typedef long long ll; + +const ll inf = 1e18; + + + +int N; + + + +struct elem { + + int x,y; + +}v[NMax],aux[NMax]; + + + +bool operator <(elem a,elem b) { + return a.y < b.y; + +} + + + +bool cmp1(elem,elem); + +double dist(elem,elem); + +double solve(int,int); + + + +int main() + +{ + + in>>N; + + for (int i=1;i<=N;++i) { + + in>>v[i].x>>v[i].y; + + } + + + + sort(v+1,v+N+1,cmp1); + + + + + + double mn = solve(1,N); + + out<>1; + + int abscisa = v[mij].x; + + double mn = min(solve(st,mij),solve(mij+1,dr)); + + + + + + if (debug) { + + cout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +struct point{ + + int x, y; + +}; + + + +int n; + +vector points; + +vector points2; + + + +void in(){ + + ifstream fin("cmap.in"); + + fin >> n; + + for (int i = 0; i < n; i++) + + { + + point p; + + fin >> p.x >> p.y; + + points.push_back(p); + + } + +} + + + +bool lessX(point a, point b){ + + return a.x < b.x; + +} + + + +bool lessY(point a, point b){ + + return a.y < b.y; + +} + + + +double dist(point a, point b){ + + return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); + +} + + + + + +double minDist(int s, int e){ + + if (e - s == 1){ + + + + return dist(points[s], points[e]); + + } + + else if (e - s == 2){ + + + + double d12 = dist(points[s], points[s + 1]); + + double d23 = dist(points[s + 1], points[e]); + + double d13 = dist(points[s], points[e]); + + + + return min(d12, min(d23, d13)); + + } + + else{ + + int mid = (e + s) / 2; + + double d = min(minDist(s, mid), minDist(mid + 1, e)); + + + + vector close; + + + + int i = mid; + + while (i >= s && points[mid + 1].x - points[i].x < d){ + + close.push_back(points[i]); + + i--; + + } + + + + i = mid + 1; + + while (i <= e && points[i].x - points[mid].x <= d){ + + close.push_back(points[i]); + + i++; + + } + + sort(close.begin(), close.end(), lessY); + + + + if (close.size() > 0){ + + + + + + for (int i = 0; i < close.size(); i++) + + { + + for (int j = i+1; j < min(i + 8, (int)close.size()); j++) + + { + + if (abs(close[i].y - close[j].y) <= d){ + + d = min(d, dist(close[i], close[j])); + + } + + } + + } + + } + return d; + + } + +} + + + +int main(){ + + in(); + + points2 = points; + + sort(points.begin(), points.end(), lessX); + + + + int a = 0, b = n-1; + + double d = minDist(a, b); + + + + ofstream f("cmap.out"); + + f << fixed << setprecision(6) << d; + + return 0; + +} diff --git a/cmap/divide_conquer/f3194324-a315-4398-93ec-b5ef1750b82e.cpp b/cmap/divide_conquer/f3194324-a315-4398-93ec-b5ef1750b82e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..429023e344a5d307c17a1f53f7efbe97320dc8f8 --- /dev/null +++ b/cmap/divide_conquer/f3194324-a315-4398-93ec-b5ef1750b82e.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include + + + + + +struct Point +{ + long long x, y; +}; + + + + + +int compareX(const void* a, const void* b) +{ + Point *p1 = (Point *)a, *p2 = (Point *)b; + return (p1->x - p2->x); +} + +int compareY(const void* a, const void* b) +{ + Point *p1 = (Point *)a, *p2 = (Point *)b; + return (p1->y - p2->y); +} + + +long double dist(Point p1, Point p2) +{ + return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + + (p1.y - p2.y)*(p1.y - p2.y) + ); +} + + + +long double bruteForce(Point P[], long long n) +{ + long double min = LDBL_MAX; + for (long long i = 0; i < n; ++i) + for (long long j = i+1; j < n; ++j) + if (dist(P[i], P[j]) < min) + min = dist(P[i], P[j]); + return min; +} + + +long double min(long double x, long double y) +{ + return (x < y)? x : y; +} + + + + + + + +long double stripClosest(Point strip[], long long size, long double d) +{ + long double min = d; + + qsort(strip, size, sizeof(Point), compareY); + + + + + for (long long i = 0; i < size; ++i) + for (long long j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j) + if (dist(strip[i],strip[j]) < min) + min = dist(strip[i], strip[j]); + + return min; +} + + + +long double closestUtil(Point P[], long long n) +{ + + if (n <= 3) + return bruteForce(P, n); + + + long long mid = n/2; + Point midPoint = P[mid]; + + + + + long double dl = closestUtil(P, mid); + long double dr = closestUtil(P + mid, n-mid); + + + long double d = min(dl, dr); + + + + Point strip[n]; + long long j = 0; + for (long long i = 0; i < n; i++) + if (abs(P[i].x - midPoint.x) < d) + strip[j] = P[i], j++; + + + + return min(d, stripClosest(strip, j, d) ); +} + + + +long double closest(Point P[], int n) +{ + qsort(P, n, sizeof(Point), compareX); + + + return closestUtil(P, n); +} + + +int main() +{ + Point *P; + int n ; + FILE *fin=fopen("cmap.in","r"); + FILE *fout=fopen("cmap.out","w"); + fscanf(fin , "%d",&n); + P = (Point*) calloc(n, sizeof(Point)); + long long i; + for(i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct punct + +{ + + int x, y; + +}v[100010],w[100010],z[100010]; + + + +int n; + + + +bool cmpx (punct a, punct b) + +{ + + return a.x < b.x; + +} + + + +bool cmpy(punct a, punct b) + +{ + + return a.y < b.y; + +} + + + +long long dist ( punct a, punct b) + +{ + + long long difx = 1LL * (a.x - b.x ); + + long long dify = 1LL * ( a.y - b.y ); + + + + return difx * difx + dify * dify ; + +} + + + + + +long long divide ( int st, int dr, punct y[],int lungime) + +{ + + if( dr - st == 1) + + return dist( v[st], v[dr] ); + + + + if ( dr-st == 2 ) + + return min(dist(v[st], v[st+1]),min(dist(v[st+1], v[dr]),dist(v[st], v[dr]))); + + + + int m=( st + dr ) / 2; + + + + int i,p=0,q=0; + + + + punct *yst = new punct[lungime+10]; + + punct *ydr = new punct[lungime+10]; + + + + for( i = 0; i <= lungime ; i++) + + if( y[i].x < v[m].x ) + + { + + yst[p].x = y[i].x; + + yst[p].y = y[i].y; + + p++; + + } + + else + + { + + ydr[q].x = y[i].x; + + ydr[q].y = y[i].y; + + q++; + + } + + + + long long S1 = divide ( st, m, yst, p-1 ) ; + + long long S2 = divide ( m+1, dr ,ydr, q-1 ) ; + + + + long long d = min( S1, S2); + + + + int j,k=0; + + + + long long delta = ceil(sqrt(d)); + + + + + + for( i = 0; i <= lungime; i++) + + { + + if( abs( y[i].x - v[m].x ) <= delta ) + + { + + w[k].x = y[i].x; + + w[k].y = y[i].y; + + k++; + + } + + } + + + + + + for(i = 0; i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++) + + { + + d = min(d,dist(w[i],w[j])); + + } + + + + delete [] yst; + + delete [] ydr; + + + + return d; + +} + + + +int main() + +{ + + f >> n; + + + + for ( int i = 0; i < n ;i++) + + { + + f >> v[i].x >> v[i].y; + + z[i].x = v[i].x; + + z[i].y = v[i].y; + + } + + + + sort(v,v+n,cmpx); + + sort(z,z+n,cmpy); + + + + g << fixed << setprecision(6) << sqrt(divide(0,n-1,z,n-1)); + + return 0; + +} diff --git a/cmap/divide_conquer/f3edaaa6-dc5b-43ed-b3c2-b4c6777adeb6.cpp b/cmap/divide_conquer/f3edaaa6-dc5b-43ed-b3c2-b4c6777adeb6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b72d281f6ff7a01858f4757e161d4464c04a8eb8 --- /dev/null +++ b/cmap/divide_conquer/f3edaaa6-dc5b-43ed-b3c2-b4c6777adeb6.cpp @@ -0,0 +1,215 @@ +#include +#include + + +typedef double f64; + + + +f64 INF=1e10; + + + +struct Punct + +{ + + f64 x, y; + + + + Punct() + + { + + x=y=0; + + } + +}; + + + +Punct v[100000], aux[100000]; + + + +f64 dist( Punct a, Punct b ) + +{ + + return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); + +} + + + +void interclasare( int st, int mid, int dr ) + +{ + + int i=st, n=mid, j=mid, m=dr, k=0; + + + + while( ib ) + + a=b; + + + + return a; + +} + + + +f64 cmap( int st, int dr ) + +{ + + int med=(st+dr)/2, i, j; + + + + if( dr-st==1 ) + + return INF; + + else + + if( dr-st==2 ) + + { + + interclasare(st,st+1,dr); + + + + return dist(v[st],v[st+1]); + + } + + + + f64 d=min(cmap(st,med),cmap(med,dr)); + + + + interclasare(st,med,dr); + + + + for( i=st;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +struct point { + + double x, y; + +}; + + + +bool cmpX(point a, point b) { + + if (a.x > b.x) return 1; + + return 0; + +} + + + +bool cmpY(point a, point b) { + + if (a.y > b.y) return 1; + + return 0; + +} + + + +double twoPointsDistance(point a, point b){ + + return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2)); + +} + + + +double minDistance(const vector &pt, int left, int right) { + + + + int separator = (left + right) / 2; + + double delta = 2e9; + + + + if (right - left > 2) + + delta = min (minDistance(pt, left, separator), minDistance(pt, separator + 1, right)); + + + + else + + for(int i = left; i < right; ++i) + + for (int j = i + 1; j <= right; ++j) + + delta = min(delta, twoPointsDistance(pt[i], pt[j])); + + + + + + vector eligiblePoints; + + for (int i = left; i <= right; ++i) + + if (abs(pt[i].x - pt[separator].x) < delta) + + eligiblePoints.push_back(pt[i]); + + + + sort(eligiblePoints.begin(), eligiblePoints.end(), cmpY); + + + + int n = eligiblePoints.size(); + + for (int i = 0; i < n; ++i) { + + + + int m = min(n, i + 8); + + for (int j = i + 1; j < m; ++j) + + delta = min(delta, twoPointsDistance(eligiblePoints[i], eligiblePoints[j])); + + } + + + + return delta; + +} + + + + + +int main() + +{ + + int n; + + vector pt; + + + + cin >> n; + + for (int i = 0; i < n; ++i) { + + point x; + + cin >> x.x >> x.y; + + pt.push_back(x); + + } + + sort(pt.begin(), pt.end(), cmpX); + + + + double minimumDistance = minDistance(pt, 0, n - 1); + + + + cout << fixed << setprecision(6) << minimumDistance; + + + +} diff --git a/cmap/divide_conquer/f555b2a0-b0aa-48c7-a9e0-49a69a360449.cpp b/cmap/divide_conquer/f555b2a0-b0aa-48c7-a9e0-49a69a360449.cpp new file mode 100644 index 0000000000000000000000000000000000000000..341f2cc8579a7afbbed1616c286fcf313ba6f584 --- /dev/null +++ b/cmap/divide_conquer/f555b2a0-b0aa-48c7-a9e0-49a69a360449.cpp @@ -0,0 +1,186 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +int N; + +pair X[100010], Y[100010], v[100010]; + + + +inline long long dist(const pair &a, const pair &b) + +{ + + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second) ; + +} + + + +inline void interclasare(const int &left, const int &middle, const int &right) + +{ + + int i = left, k = 0; + + int j = middle + 1; + + + + while(i <= middle && j <= right) + + { + + (X[i].first <= X[j].first) ? (Y[++ k] = X[i ++]) : (Y[++ k] = X[j ++]); + + } + + while(i <= middle) + + { + + Y[++ k] = X[i ++]; + + } + + while(j <= right) + + { + + Y[++ k] = X[j ++]; + + } + + for(int i = left; i <= right; i ++) + + { + + X[i] = Y[i - left + 1]; + + } + +} + + + +inline long long closestPoints(int left, int right, pair X[100010]) + +{ + + if(left >= right - 1) + + return 20000000000000000; + + + + if(right - left == 2) + + { + + if(X[left] > X[left + 1]) + + swap(X[left], X[left + 1]); + + + + return dist(X[left], X[left + 1]); + + } + + + + int middle = (left + right) >> 1; + + long long solution = min( closestPoints(left, middle, X), closestPoints(middle, right, X) ); + + + + interclasare(left, middle, right); + + + + int k = 0; + + for(int i = left; i < right; i ++) + + { + + if( abs(X[i].first - X[middle].first) <= solution) + + { + + v[++k] = X[i]; + + } + + } + + + + for(int i = 1; i < k; i ++) + + { + + for(int j = i + 1; j <= k && j - i < 8; j ++) + + { + + solution = min(solution, dist(v[i], v[j])); + + } + + } + + + + return solution; + + + +} + + + +int main() + +{ + + fin >> N; + + + + for(int i = 1; i <= N; i ++) + + { + + fin >> X[i].first >> X[i].second; + + } + + + + sort(X + 1, X + N + 1); + + + + fout << setprecision(6) << fixed << sqrt( closestPoints(0, N, X) ); + + + + return 0; + +} diff --git a/cmap/divide_conquer/f576823b-ad88-4c3f-8d6c-070123ad3feb.cpp b/cmap/divide_conquer/f576823b-ad88-4c3f-8d6c-070123ad3feb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..088ef6c7dba4401becbd655b46f9f9502e549ec6 --- /dev/null +++ b/cmap/divide_conquer/f576823b-ad88-4c3f-8d6c-070123ad3feb.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const long long INF = 0x7FFFFFFFFFFFFFFFLL; + +struct punct + +{ + + int x, y; + +} P[100001], Tmp[100001]; + +int N; + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + +inline long long sqr(long long v) + +{ + + return v * v; + +} + +long long dist2(const punct&A, const punct&B) + +{ + + return sqr(A.x - B.x) + sqr(A.y - B.y); + +} + +long long distmin(int p, int u) + +{ + + long long dmin = INF; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long d = dist2(P[i], P[j]); + + dmin = min(d, dmin); + + } + + return dmin; + +} + +void interclasare(int p, int m, int u) + +{ + + int k = p, i = p, j = m + 1; + + while(i <= m && j <= u) + + if(P[i].y < P[j].y) + + Tmp[k++] = P[i++]; + + else + + Tmp[k++] = P[j++]; + + while(i <= m) + + Tmp[k++] = P[i++]; + + while(j <= u) + + Tmp[k++] = P[j++]; + +} + +long long calcdmin(int p, int u) + +{ + + if(u - p < 3) + + { + + sort(P + p, P + u + 1, compy); + + return distmin(p, u); + + } + + int m = p + (u - p) / 2; + + int x = P[m].x; + + long long d = min(calcdmin(p, m), calcdmin(m, u)); + + interclasare(p, m, u); + + int k = 0; + + for(int i = p; i <= u; i++) + + { + + P[i] = Tmp[i]; + + if(sqr(P[i].x - x) < d) + + Tmp[++k] = P[i]; + + + + } + + + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && sqr(Tmp[j].y - Tmp[i].y) < d; j++) + + { + + long long dc = dist2(Tmp[i], Tmp[j]); + + d = min(dc, d); + + } + + return d; + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> P[i].x >> P[i].y; + + sort(P + 1, P + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcdmin(1, N)); + + + + return 0; + +} diff --git a/cmap/divide_conquer/f5895011-2ce9-40a0-a294-53429a39cd41.cpp b/cmap/divide_conquer/f5895011-2ce9-40a0-a294-53429a39cd41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8c851429c3e74615160fa10f5e0d4b8b04b8a84 --- /dev/null +++ b/cmap/divide_conquer/f5895011-2ce9-40a0-a294-53429a39cd41.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +const int MAXN = 100002; + +const long long inf = 3e18; + + + +pair a[MAXN], b[MAXN], aux[MAXN]; + + + +long long getDistance( pair A, pair B ) { + + return 1LL * (A.first-B.first) * (A.first-B.first) + 1LL * (A.second-B.second) * (A.second-B.second); + +} + + + +long long get ( int begin, int end ) { + + if (begin == end) + + return inf; + + if (end - begin == 1) { + + if (b[end] < b[begin]) + + swap(b[end], b[begin]); + + return getDistance(b[begin], b[end]); + + } + + + + int middle = (begin+end)>>1; + + long long minDistance = min(get(begin, middle), get(middle+1, end)); + + + + merge(b + begin, b + middle + 1, b + middle + 1, b + end + 1, aux); + + copy(aux, aux + (end - begin + 1), b + begin); + + + + int k = 0; + + for(int i=begin; i<=end; ++i) + + if (abs(b[i].second - a[middle].first) <= minDistance) + + aux[k++] = b[i]; + + + + for(int i=0; i>n; + + for (i=1; i<=n; ++i) + + f >> a[i].first >> a[i].second; + + sort(a + 1, a + n + 1); + + for (i=1; i<=n; ++i) + + b[i] = make_pair(a[i].second, a[i].first); + + + + g.setf(ios::fixed, ios::floatfield); + + g.precision(6); + + g << sqrt( get(1, n) ) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/f5a6b178-6dd0-4f32-aac8-e0e03e9cb2c0.cpp b/cmap/divide_conquer/f5a6b178-6dd0-4f32-aac8-e0e03e9cb2c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c7747101efb5c1f29a619f24094c4d9c668ba48 --- /dev/null +++ b/cmap/divide_conquer/f5a6b178-6dd0-4f32-aac8-e0e03e9cb2c0.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +const int Nmax = 1e5+5; + + + +int n,i; + +struct Point + +{ + + int x, y; + +}; + +Point a[Nmax], v1[Nmax], aux[Nmax]; + + + +long long dist(Point a, Point b) + +{ + + return 1LL * (a.x-b.x) * (a.x-b.x) + 1LL * (a.y-b.y) * (a.y-b.y); + +} + + + +bool cmp_x(Point a, Point b) + +{ + + if(a.x==b.x) return a.y +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("cmap.in"); + +ofstream fout ("cmap.out"); + + + +int n; + + + +pair v[100010], w[100010], q[100010]; + + + +long long dist (pair x, pair y){ + + return (x.first - y.first)*(x.first - y.first) + (x.second - y.second)*(x.second - y.second); + +} + + + +void interclasare(int st, int mid, int dr) { + + int i = st; + + int j = mid + 1; + + int k = 0; + + while (i <= mid && j <= dr) + + if (v[i].second < v[j].second) + + w[++k] = v[i++]; + + else + + w[++k] = v[j++]; + + for(;i<=mid;i++) + + w[++k] = v[i]; + + for(;j<=dr;j++) + + w[++k] = v[j]; + + for(k=1, i=st; i<=dr; i++, k++) + + v[i] = w[k]; + +} + + + +long long jas (int st, int dr){ + + long long r = 0; + + if(dr - st == 1) { + + r = dist (v[st], v[dr]); + + interclasare (st, st, dr); + + return r; + + } + + if(dr - st == 2) { + + r = min (dist(v[st], v[st+1]), min(dist(v[st+1], v[dr]), dist(v[st], v[dr]))); + + interclasare(st, st, st+1); + + interclasare(st, st+1, dr); + + return r; + + } + + int mid = (st + dr)/2; + + long long sol1 = jas (st, mid); + + long long sol2 = jas (mid+1, dr); + + interclasare (st, mid, dr); + + long long minim = min (sol1, sol2); + + int m = 0; + + for (int i=st; i<=dr; i++) + + if (abs (v[mid].first - v[i].first) <= minim) + + q[++m] = v[i]; + + for(int i=1; i> n; + + for(int i=1; i<=n; i++) + + fin >> v[i].first >> v[i].second; + + sort (v + 1, v + n + 1); + + fout << setprecision(7) << fixed << (double)sqrt(jas(1,n)); + + return 0; + +} diff --git a/cmap/divide_conquer/f60babcf-2b58-462d-8789-1a7e3b6568d6.cpp b/cmap/divide_conquer/f60babcf-2b58-462d-8789-1a7e3b6568d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab7b071418015f5e709c40005e284eb946d8d572 --- /dev/null +++ b/cmap/divide_conquer/f60babcf-2b58-462d-8789-1a7e3b6568d6.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +int n; + +long double mn; + +pair v[100001]; + +vector > A,B; + + + +bool comp(pair a, pair b) + +{ + + return a.first a, pair b) + +{ + + if(a==b) + + g<<"NU"; + + return sqrt(((long double)(a.first-b.first) * (a.first-b.first)) + ((long double)(a.second-b.second) * (a.second-b.second))); + +} + + + +bool comp2(pair a,pair b) + +{ + + return a.second=v[(a+b)/2].first - mn && i>=a;i--) + + A.push_back(v[i]); + + sort(A.begin(),A.end(),comp2); + + + + for (int i=(a+b)/2+1;v[i].first<=v[(a+b)/2+1].first + mn && i<=b;i++) + + B.push_back(v[i]); + + sort(B.begin(),B.end(),comp2); + + + + int i1 = 0,i2 = 0; + + while (i1>n; + + for (int i=1;i<=n;i++) + + { + + int x,y; + + f>>x>>y; + + v[i] = make_pair(x,y); + + } + + + + sort(v+1,v+n+1,comp); + + + + long double x = divide(1,n); + + + + g< +#include +#include +#include +#include +using namespace std; + +const int Nmax = 100001; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x, y; + +}; + + + +punct Px[Nmax], Tmp[Nmax]; + +int N; + + + +bool compx(const punct &A, const punct &B) + +{ + + return A.x < B.x; + +} + + + +bool compy(const punct &A, const punct &B) + +{ + + return A.y < B.y; + +} + + + +inline long long patrat(long long x) + +{ + + return x * x; + +} + + + +long long dist2(const punct &A, const punct &B) + +{ + + return patrat(A.x - B.x) + patrat(A.y - B.y); + +} + + + +long long distMin(int p, int u) + +{ + + long long dmin = 0x7FFFFFFFFFFFFFFFLL; + + for(int i = p; i < u; i++) + + for(int j = i + 1; j <= u; j++) + + { + + long long dij = dist2(Px[i], Px[j]); + + if(dmin > dij)dmin = dij; + + } + + return dmin; + +} + + + +long long calcDmin(int p, int u) + +{ + + if(u - p < 4) + + { + + sort(Px + p, Px + u + 1, compy); + + return distMin(p, u); + + } + + int m = (p + u) / 2; + + int x = Px[m].x; + + long long d1 = calcDmin(p, m); + + long long d2 = calcDmin(m, u); + + long long dd = min(d1, d2); + + merge(Px + p, Px + m + 1, Px + m + 1, Px + u + 1, Tmp + p, compy); + + int k = 0; + + for(int i = p; i <= u; i++) + + { + + Px[i] = Tmp[i]; + + if(patrat(Px[i].x - x) < dd) + + Tmp[++k] = Px[i]; + + } + + for(int i = 1; i < k; i++) + + for(int j = i + 1; j <= k && patrat(Tmp[j].y - Tmp[i].y) < dd; j++) + + { + + d1 = dist2(Tmp[i], Tmp[j]); + + if(dd > d1)dd = d1; + + } + + return dd; + +} + + + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> Px[i].x >> Px[i].y; + + sort(Px + 1, Px + N + 1, compx); + + g << fixed << setprecision(6) << sqrt(calcDmin(1, N)); + + return 0; + +} diff --git a/cmap/divide_conquer/f6cc7714-6de6-46c4-aeaa-989a8b0367f9.cpp b/cmap/divide_conquer/f6cc7714-6de6-46c4-aeaa-989a8b0367f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1d236b1fbd6f154be1b5f735c598b075f04a941 --- /dev/null +++ b/cmap/divide_conquer/f6cc7714-6de6-46c4-aeaa-989a8b0367f9.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +const double eps = 1e-10; + +double minimum (double a, double b) { + if (a + eps <= b) return a; + return b; +} + +double sq (double x) { + return x * x; +} + +double getDistance (pair &A, pair & B) { + return sqrt (sq(A.first - B.first) + sq(A.second - B.second)); +} + +pair points[100005]; +pair auxiliar[100005]; + +bool cmp (pair A, pair B) { + return A.second + eps <= B.second; +} + +double getDistance (int left, int right) { + if (right - left + 1 <= 3) { + sort (points + left, points + right + 1, cmp); + double minim = 1e15; + for (int i = left; i <= right; ++i) { + for (int j = i + 1; j <= right; ++j) { + minim = minimum(minim, getDistance(points[i], points[j])); + } + } + return minim; + } + int middle = left + right >> 1; + double minim = getDistance(left, middle); + minim = minimum (minim, getDistance(middle + 1, right)); + int leftIt = left; + int rightIt = middle + 1; + for (int i = left; i <= right; ++i) { + if (leftIt == middle + 1 || (rightIt <= right and !cmp (points[leftIt], points[rightIt]))) { + auxiliar[i] = points[rightIt]; + rightIt += 1; + } + else { + auxiliar[i] = points[leftIt]; + leftIt += 1; + } + } + for (int i = left; i <= right; ++i) { + points[i] = auxiliar[i]; + } + for (int i = left; i <= right; ++i) { + for (int j = i + 1; j <= min(right, i + 7); ++j) { + minim = minimum(minim, getDistance(points[i], points[j])); + } + } + return minim; +} + +int main() { + ifstream fin ("cmap.in"); + ofstream fout ("cmap.out"); + ios::sync_with_stdio(false); + fin.tie(nullptr); + int n; + fin >> n; + for (int i = 1; i <= n; ++i) { + fin >> points[i].first >> points[i].second; + } + sort (points + 1, points + n + 1); + fout << setprecision(7) << fixed << getDistance(1, n); + return 0; +} diff --git a/cmap/divide_conquer/f71e9b47-e6be-44cc-996d-50d50312141d.cpp b/cmap/divide_conquer/f71e9b47-e6be-44cc-996d-50d50312141d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb228fbd1101ff7f1d6c664ce00f9af573d89612 --- /dev/null +++ b/cmap/divide_conquer/f71e9b47-e6be-44cc-996d-50d50312141d.cpp @@ -0,0 +1,121 @@ +#include +#include #include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream o("cmap.out"); + + + +const int nmx = 100002; + +const double inf = 3000000000; + + + +int n; + +pair v[nmx]; + + + +void citire() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> v[i].first >> v[i].second; + +} + + + +inline double dist(const pair p1, const pair p2) + +{ + + return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); + +} + + + +double divide(const int st, const int dr) + +{ + + if(dr - st + 1 > 3) + + { + + int mij = (st + dr) / 2; + + double minim = min(divide(st,mij),divide(mij+1,dr)); + + + + int st1 = max(st-7,1), dr1 = min(dr+7,n); + + + + for(int i = st1; i <= dr1; ++i) + + for(int j = i + 1; j <= i + 7 && j <= dr1; ++j) + + minim = min(minim,dist(v[i],v[j])); + + + + return minim; + + } + + else + + { + + double minim = dist(v[st],v[st+1]); + + for(int i = st; i < dr; ++i) + + for(int j = i + 1; j <= dr; ++j) + + minim = min(minim,dist(v[i],v[j])); + + + + return minim; + + } + +} + + + +int main() + +{ + + citire(); + + sort(v + 1, v + n + 1); + + o << fixed << setprecision(6) << sqrt(divide(1,n)) << "\n"; + + return 0; + +} diff --git a/cmap/divide_conquer/f77fdd08-865a-4beb-b1a3-d1c408f59f9b.cpp b/cmap/divide_conquer/f77fdd08-865a-4beb-b1a3-d1c408f59f9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18fe9fdf769577662d4eaf7654872688d06a767d --- /dev/null +++ b/cmap/divide_conquer/f77fdd08-865a-4beb-b1a3-d1c408f59f9b.cpp @@ -0,0 +1,270 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +struct punct{long long x, y;}; + +punct a[100002],b[100002]; + + + +int n,i,j; + + + +bool compy(punct a,punct b) + +{ + + if(a.y=st;ps--) + + { + + if(vert-a[ps].x>=Smin) + + { + + break; + + } + + } + + ps++; + + for(pd=mij;pd<=dr;pd++) + + { + + if(a[pd].x-vert>=Smin) + + { + + break; + + } + + } + + pd--; + + for(int i=mij+1;i<=pd;i++) + + { + + b[i]=a[i]; + + } + + sort(b+mij+1,b+pd+1,compy); + + for(int i=ps;i<=mij;i++) + + { + + int poz; + + poz=upper_bound(b+mij+1,b+pd+1,a[i],compy)-b; + + if(poz<=pd) + + { + + Smin=min(Smin,dist(a,i,b,poz)); + + for(int j=poz+1;j<=pd;j++) + + { + + if(abs(a[i].y-b[j].y)>Smin) + + break; + + if(dist(a,i,b,j)=mij+1;j--) + + { + + if(abs(a[i].y-b[j].y)>Smin) + + break; + + if(dist(a,i,b,j)> n; + + for(i=0;i> a[i].x >> a[i].y; + + } + + + + sort(a,a+n,compx); + + long double l=cmap(0,n-1); + + out << fixed << setprecision(6) << l; + +} diff --git a/cmap/divide_conquer/f7d336f8-e6cb-4eb6-ae38-2319a191b23f.cpp b/cmap/divide_conquer/f7d336f8-e6cb-4eb6-ae38-2319a191b23f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6598734eccf0d3e24ebf0f8ac78730620407c9f5 --- /dev/null +++ b/cmap/divide_conquer/f7d336f8-e6cb-4eb6-ae38-2319a191b23f.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("cmap.in"); + +ofstream cout("cmap.out"); + + + +int n,x,y; + +vector > X; + + + +long long sq(pair &i,pair &j){ + + + + return 1LL*(i.first-j.first)*(i.first-j.first)+ + + 1LL*(i.second-j.second)*(i.second-j.second); + + + +} + + + +long long minDist(int s,int d){ + + + + if(d-s<=2){ + + + + long long D=4e18; + + for(int i=s;i<=d;i++) + + for(int j=i+1;j<=d;j++) + + D=min(D,sq(X[i],X[j])); + + + + return D; + + + + } + + + + int m=(s+d)/2; + + + + long long D1=minDist(s,m); + + long long D2=minDist(m,d); + + long long D=min(D1,D2); + + + + vector > Y(X.begin()+s,X.begin()+d+1); + + for(int i=0;i > V; + + for(int i=0;i>n; + + + + for(int i=1;i<=n;i++){ + + + + cin>>x>>y; + + X.push_back(make_pair(x,y)); + + + + } + + + + sort(X.begin(),X.end()); + + + + cout< +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream f("cmap.in"); +ofstream g("cmap.out"); + +int const NMax = 100005; +long long const oo = 1LL*1<<60; + +pair P[NMax], Aux[NMax]; +int n; + +void Read() +{ + int i, x, y; + + f>>n; + for(i=1; i<=n; i++){ + f>>x>>y; + P[i] = make_pair(x, y); + } + sort(P + 1, P + n + 1); +} + +long long dist(pair A, pair B) +{ + return 1LL*(B.first-A.first)*(B.first-A.first)+1LL*(B.second-A.second)*(B.second-A.second); +} + +long long DEI(int st, int dr) +{ + if(st == dr) + return oo; + if(st == dr - 1) + return dist(P[st], P[dr]); + + int mid = (st + dr)/2, k = 0, i, j; + long long a = DEI(st, mid); + long long b = DEI(mid+1, dr); + long long mini = min(a, b); + + for(i=st; i<=dr; i++) + if(abs(P[i].first - P[mid].first) < mini) + Aux[++k] = make_pair(P[i].second, P[i].first); + + sort(Aux + 1, Aux + k + 1); + + for(i = 1; i +#include +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); +const long long inf = 1e18 + 5; + +const int NMax = 1e5 + 5; + + + +int N; + + + +struct point { + + int x,y; + +}v[NMax],aux[NMax]; + + + +bool cmp(point,point); + +long long solve(int,int); + +long long dist(point,point); + + + +int main() { + + in>>N; + + for (int i=1;i <= N;++i) { + + in>>v[i].x>>v[i].y; + + } + + + + sort(v+1,v+N+1,cmp); + + + + + + + + long long mn = solve(1,N); + + out<= dr) { + + return inf; + + } + + if (st == dr-1) { + + if (v[st].y > v[dr].y) { + + swap(v[st],v[dr]); + + } + + + + return dist(v[st],v[dr]); + + } + + + + long long mij = (st+dr)>>1, + + abscisa = v[mij].x, + + mn = min(solve(st,mij),solve(mij+1,dr)); + + + + int nrAux = 0, i = st, j = mij+1; + + while (i <= mij && j <= dr) { + + if (v[i].y < v[j].y) { + + aux[++nrAux] = v[i++]; + + } + + else { + + aux[++nrAux] = v[j++]; + + } + + } + + while (i <= mij) { + + aux[++nrAux] = v[i++]; + + } + + while (j <= dr) { + + aux[++nrAux] = v[j++]; + + } + + + + nrAux = 0; + + int root = sqrt(mn); + + for (i = st;i <= dr;++i) { + + if (abs(v[i].x - abscisa) <= mn) { + + aux[++nrAux] = v[i]; + + } + + } + + + + for (i=1;i <= nrAux;++i) { + + for (int j=i+1;j <= i+7;++j) { + + mn = min(mn,dist(aux[i],aux[j])); + + } + + } + + + + return mn; + +} + + + +long long dist(point a,point b) { + + return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); + +} + + + +bool cmp(point a,point b) { + + return a.x < b.x; + +} diff --git a/cmap/divide_conquer/f8d99fdc-b3cd-4f41-ae8c-10fb22cbe000.cpp b/cmap/divide_conquer/f8d99fdc-b3cd-4f41-ae8c-10fb22cbe000.cpp new file mode 100644 index 0000000000000000000000000000000000000000..200140e9d9d175f56edd0c0df17e5bcf3f6547cf --- /dev/null +++ b/cmap/divide_conquer/f8d99fdc-b3cd-4f41-ae8c-10fb22cbe000.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + + + +const long long INF = 4e18; + + + +struct point { + + int x, y; + +}; + + + +int n; + +vector p; + + + +bool cmp(point a, point b) { + + if (a.x < b.x) + + return true; + + if (a.x == b.x && a.y < b.y) + + return true; + + return false; + +} + + + +bool cmpAux(point a, point b) { + + if (a.y < b.y) + + return true; + + if (a.y == b.y && a.x < b.x) + + return true; + + return false; + +} + + + +double dist(point a, point b) { + + long long dx = a.x - b.x; + + long long dy = a.y - b.y; + + return sqrt(dx * dx + dy * dy); + +} + + + +double distMin(vector& p, int st, int dp) { + + + + if (dp - st <= 2) { + + double dmin = INF; + + + + for (int i = st; i < dp; ++i) { + + for (int j = i + 1; j <= dp; ++j) + + dmin = min(dmin, dist(p[i], p[j])); + + } + + return dmin; + + } + + + + int mid = (st + dp) / 2; + + + + double dmin = min(distMin(p, st, mid), distMin(p, mid + 1, dp)); + + double D = (p[mid].x + p[mid + 1].x) / 2.0; + + + + vector aux; + + + + for (int i = st; i <= dp; ++i) + + if (abs(D - p[i].x) <= dmin) + + aux.push_back(p[i]); + + + + sort(aux.begin(), aux.end(), cmpAux); + + + + int n = aux.size(); + + for (int i = 0; i < n - 1; i++) + + for (int j = i + 1; j < i + 8 && j < n; j++) + + dmin = min(dmin, dist(aux[i], aux[j])); + + + + return dmin; + +} + + + +int main() { + + fin >> n; + + p.resize(n); + + + + for (int i = 0; i < n; ++i) + + fin >> p[i].x >> p[i].y; + + + + sort(p.begin(), p.end(), cmp); + + + + fout << fixed << setprecision(6) << distMin(p, 0, n - 1); + + + + return 0; + +} diff --git a/cmap/divide_conquer/f907dae2-d204-4425-a9c7-da376c3bb0f6.cpp b/cmap/divide_conquer/f907dae2-d204-4425-a9c7-da376c3bb0f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2586936295e80dbd516acbd2d36b8e32e10ca3b --- /dev/null +++ b/cmap/divide_conquer/f907dae2-d204-4425-a9c7-da376c3bb0f6.cpp @@ -0,0 +1,122 @@ +#include + + + + + + +using namespace std; + +int n, x, y; + +vector>xs, ys; + +vector>v(100005); + +long long distance(pair &a, pair &b) + +{ + + return 1LL*(a.first - b.first)*(a.first - b.first) + 1LL*(a.second - b.second)*(a.second - b.second); + +} + +long long minn(long long a, long long b) + +{ + + if (a < b) return a; + + return b; + +} + +long long di(int st, int dr) + +{ + + if (st >= dr) + + return 9223372036854775806LL; + + if (dr - st == 1) + + { + + if (ys[st] > ys[st+1]) + + swap(ys[st],ys[st+1]); + + return distance(xs[st], xs[st+1]); + + } + + int m = (st + dr)/2; + + long long bst = minn(di(st, m-1),di(m,dr)); + + sort(ys.begin() + st, ys.begin() + dr + 1); + + + + int sz = 0; + + for (int i=st;i<=dr;++i) + + if (abs(ys[i].second-xs[m].first)<=bst) + + v[sz++] = ys[i]; + + for (int i=0;i +#include #include +#include +#include +#include +#include +using namespace std; + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + +struct punct + +{ + + int x=0,y=0; + +}; + +double distanta(punct a,punct b) + +{ + + double x = a.x - b.x; + + double y = a.y - b.y; + + double dist; + + + + dist = pow(x, 2) + pow(y, 2); + + dist = sqrt(dist); + + + + return dist; + + + +} + +bool cmpX(const punct& a,const punct& b) + +{ + + return a.x &P,vector &Y) + +{ + + if((dr-st)==1) + + { + + return 2147483647; + } + + else if((dr-st)==2) + + { + + if(Y[st].y>Y[st+1].y) + + swap(Y[st],Y[st+1]); + + return distanta(P[0],P[1]); + + + + + + } + + int d; + + double min_st,min_dr,min_p; + + + + d=(st+dr)/2; + + min_st=dist_min(st,d,P,Y); + + min_dr=dist_min(d,dr,P,Y); + + min_p=min(min_st,min_dr); + + vector aux(dr-st); + + merge(Y.begin()+st,Y.begin()+d,Y.begin()+d,Y.begin()+dr,aux.begin(),cmpY); + + copy(aux.begin(),aux.begin()+(dr-st),Y.begin()+st); + + vector b; + + for (int i = st; i < dr; ++i) + + { + + if (abs(Y[i].x - P[d].x) <= min_p) + + b.push_back(Y[i]); + + } + + + + + + double min_t = min_p; + + for (int i = 0; i>n; + + vector P(n); + + for(i=0; i>P[i].x; + + f>>P[i].y; + + } + + sort(P.begin(),P.end(),cmpX); + + vector Y=P; + + g << fixed; + + g << setprecision(6) < +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("cmap.in"); + +ofstream g("cmap.out"); + + + + + + + +struct point{ + + long long x, y; + +}v[100100],w[100100]; + + + +int n; + + + +bool cmp_x (point a, point b){ + + return a.x < b.x; + +} + + + +bool cmp_y(point a, point b){ + + return a.y < b.y; + +} + + + +long long dist ( point a, point b){ + + return (a.x - b.x )*(a.x - b.x ) + ( a.y - b.y )*( a.y - b.y ); + +} + + + + + + + +long long divide ( int left, int right){ + + if( right - left == 1){ + return dist( v[left], v[right] ); + + } + + if ( right-left == 2 ){ + + long long x,y,z; + + x=dist(v[left], v[left+1]); + + y=dist(v[left+1], v[right]); + + z=dist(v[left], v[right]); + + if((x<=y)&&(x<=z)){ + return x; + + } + + if((y<=x)&&(y<=z)){ + return y; + + } + + if((z<=x)&&(z<=y)){ + return z; + + } + + } + + + + int m=( left + right ) / 2; + + long long d1 = divide ( left, m ) ; + + long long d2 = divide ( m+1, right ) ; + + long long d = min( d1, d2); + + int i,j,k=0; + + long long delta = ceil(sqrt(d)); + + for( i = left; i <= right; i++){ + + if( abs( v[i].x - v[m].x ) <= delta ){ + + w[k].x = v[i].x; + + w[k].y = v[i].y; + + k++; + + } + + } + + + + sort(w,w+k,cmp_y); + + + + for(i = 0;i < k ; i++) + + for(j = i + 1 ; j <= (i+7) && j < k; j++){ + + if(dist(w[i],w[j])>n; + + for (int i =0;i>v[i].x>>v[i].y; + + sort(v,v+n,cmp_x); + + g< +#include +#include +using namespace std; + +int n; + + + +struct pct + +{ + + int x,y; + +} a[100001],b[100001],c[100001]; + + + + + +int f1(pct x,pct y) + +{ + + return (x.xb[lf].y)swap(b[li],b[lf]); + + return dist(a[li],a[lf]); + + } + + if(lf-li==2) + + { + + double q=dist(a[li],a[li+1]); + + double w=dist(a[li+1],a[li+2]); + + double r=dist(a[li],a[li+2]); + + if(wb[li+1].y)swap(b[li],b[li+1]); + + if(b[li].y>b[li+2].y)swap(b[li],b[li+2]); + + if(b[li+1].y>b[li+2].y)swap(b[li+1],b[li+2]); + + return r; + + } + + double d,d1; + + int m,i,np=0,xd; + + m=(li+lf)/2; + + d=dmin(li,m); + + d1=dmin(m+1,lf); + + if(d1 +#include +#include +#include +using namespace std; + +ifstream fin("cmap.in"); + +ofstream fout("cmap.out"); + +typedef pair pii; + +pii a[100005]; + +pii r[100005]; + +pii v[100005]; + +int n,ps,pd; + +long long dist(pii A,pii B) + +{ + + return (long long)(A.first-B.first)*(A.first-B.first)+(A.second-B.second)*(A.second-B.second); + +} + +long long re(int st,int dr) + +{ + + if(st==dr) + + return 1000000000000000000000LL; + + int m=0; + + int mij=(st+dr)/2; + + long long rez=min(re(st,mij),re(mij+1,dr)); + + ps=st; + + pd=mij+1; + + for(int i=st;i<=dr;i++) + + if(ps<=mij&&(pd>dr||a[ps].second>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].first>>a[i].second; + + sort(a+1,a+n+1); + + t=re(1,n); + + fout< +using namespace std; + + + + + +typedef long long ll; +typedef pair pii; +typedef pair pll; +vector > v,x,y; +ll INF = 4e18; +int N; + +ll dist(pll a, pll b) { + return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); +} + +pair solve(int st, int dr) { + if (st >= dr - 1) return make_pair(INF,make_pair(0,0)); + if (dr - st == 2) { + if (y[st] > y[st + 1]) { + swap(y[st], y[st + 1]); + } + return make_pair(dist(x[st].first, x[st + 1].first),make_pair(x[st].second, x[st+1].second)); + } + + int mij = (st + dr) / 2; + pair ret = min(solve(st,mij),solve(mij,dr)); + + merge(y.begin() + st, y.begin() + mij, y.begin() + mij, y.begin() + dr, v.begin()); + copy(v.begin(), v.begin() + (dr - st), y.begin() + st); + + int nr = 0; + for (int i=st; i + + + + +using namespace std; + +fstream f1("cmap.in", ios::in); + +fstream f2("cmap.out", ios::out); + +int n; + +long long rez=4000000000000000000; + +struct punct + +{ + + long long x, y; + +}p[100005], py[100005], aux[100005]; + +bool cmp1(punct a, punct b) + +{ + + return a.x< b.x; + +} + +bool cmp2(punct a, punct b) + +{ + + return a.y> b.y; + +} + +long long dist(punct a, punct b) + +{ + + return (a.x-b.x)*(a.x-b.x)+ (a.y-b.y)*(a.y-b.y); + +} + +void citire() + +{ + + f1>>n; + + for(int i=1; i<=n; i++) + + f1>>p[i].x>>p[i].y; + + sort(p+1, p+n+1, cmp1); + +} + +void brut(int st, int dr) + +{ + + long long val; + + if(dr-st+1==1) return; + + for(int i=st; i<=dr; i++) + + for(int j=i+1; j<=dr; j++) + + { + + val=dist(p[i], p[j]); + + if(rez> val) + + rez=val; + + } + + for(int i=st; i<=dr; i++) + + py[i]=p[i]; + + sort(py+st, py+dr+1, cmp2); + +} + +void interclasare(int st, int mijl, int dr) + +{ + + int i=st, j=mijl+1, k=0; + + while((i<=mijl)&&(j<=dr)) + + { + + if(py[i].y>py[j].y) {k++; aux[k]=py[i]; i++;} + + else {k++; aux[k]=py[j]; j++;} + + } + + while(i<=mijl) + + { + + k++; aux[k]=py[i]; i++; + + } + + while(j<=dr) + + { + + k++; aux[k]=py[j]; j++; + + } + + for(j=1, i=st; j<=k; j++,i++) + + py[i]=aux[j]; + +} + +void mij(int st, int mijl, int dr) + +{ + + long long val; + + int k=0; + + for(int i=st; i<=dr; i++) + + if(abs(py[i].x-py[mijl].x)<=rez) + + { + + k++; aux[k]=py[i]; + + } + + for(int i=st; i<=dr; i++) + + for(int j=i+1; (j<=dr)&&(j-i)<=8; j++) + + { + + val=dist(py[i], py[j]); + + if(val< rez) rez=val; + + } + +} + +void solutie(int st, int dr) + +{ + + if(dr-st+1<=3) brut(st, dr); + + else + + { + + int mijl=(st+dr)/2; + + solutie(st, mijl); + + solutie(mijl+1, dr); + + interclasare(st, mijl, dr); + + mij(st, mijl, dr); + + } + +} + +int main() + +{ + + citire(); + + solutie(1, n); + + f2< +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + + + + + +const int NMax = 1e5 + 5; + +const long long inf = 1e18 + 5; + + + +int N; + + + +struct elem { + + int x,y; + + elem(int _x = 0,int _y = 0) { + + x = _x; + + y = _y; + + } + +}v[NMax],aux[NMax]; + + + +bool operator <(elem a,elem b) { + + return a.y < b.y; + +} + + + +bool cmp(elem a,elem b) { + + return a.x < b.x; + +} + + + +long long solve(int,int); + +long long dist(elem,elem); + + + +int main() { + + in>>N; + + for (int i=1;i <= N;++i) { + + int x,y; + + in>>x>>y; + + v[i] = elem(x,y); + + } + + + + sort(v +1,v +N+1,cmp); + + + + out< +#include +#include +#include +#include +#include +typedef long long int int64; + + + +using namespace std; + + + +ifstream in("cmap.in"); + +ofstream out("cmap.out"); + + + +const int64 lookup = 7; + + + +bool cmpX(pair &a, pair &b) { + + if (a.first < b.first) + + return true; + + return false; + +} + + + +bool cmpY(pair &a, pair &b) { + + if (a.second < b.second) + + return true; + + return false; + +} + + + +int64 get_dist(pair &a, pair &b) { + + return (b.first - a.first) * (b.first - a.first) + (b.second - a.second) * (b.second - a.second); + +} + + + +inline int64 square(int64 a) { + + return a * a; + +} + + + +int64 cmap(vector > &pct, int left, int right) { + + if (right - left + 1 <= 3) { + + int64 d1, d2, d3; + + d1 = get_dist(pct[0], pct[1]); + + d2 = get_dist(pct[1], pct[2]); + + d3 = get_dist(pct[0], pct[2]); + + if (d1 <= d2 && d1 <= d3) + + return d1; + + if (d2 <= d1 && d2 <= d3) + + return d2; + + return d3; + + } + + int mid = left + (right - left) / 2; + + int64 d1, d2; + + d1 = cmap(pct, left, mid); + + d2 = cmap(pct, mid, right); + + if (d1 > d2) + + d1 = d2; + + + + vector > val_pct; + + int64 midx = pct[mid].first; + + val_pct.push_back(pct[mid]); + + + + int search_left = mid - 1, search_right = mid + 1; + + while (search_left >= left && square(midx - pct[search_left].first) < d1) { + + val_pct.push_back(pct[search_left]); + + search_left--; + + } + + while (search_right <= right && square(pct[search_right].first - midx) < d1) { + + val_pct.push_back(pct[search_right]); + + search_right++; + + } + + + + sort(val_pct.begin(), val_pct.end(), cmpY); + + + + for (int i = 0; i < val_pct.size(); i++) { + + for (int j = i + 1; j < val_pct.size() && j - i <= lookup; j++) { + + d2 = get_dist(val_pct[i], val_pct[j]); + + if (d1 > d2) { + + d1 = d2; + + continue; + + } + + if (square(val_pct[j].second - val_pct[i].second) >= d1) + + break; + + } + + } + + + + return d1; + +} + + + +int main() { + + int64 n; + + in >> n; + + vector > pct(n); + + for (int64 i = 0; i < n; i++) { + + in >> pct[i].first >> pct[i].second; + + } + + sort(pct.begin(), pct.end(), cmpX); + + out << fixed << setprecision(6) << sqrt(cmap(pct, 0, n - 1)) << '\n'; + + return 0; + +} diff --git a/cmap/divide_conquer/fe23f0f7-91aa-4df7-bad0-ed7e431084b1.cpp b/cmap/divide_conquer/fe23f0f7-91aa-4df7-bad0-ed7e431084b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7ead0d050d60d8c6b7e599102a2b23f11a5e495 --- /dev/null +++ b/cmap/divide_conquer/fe23f0f7-91aa-4df7-bad0-ed7e431084b1.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + + + + + +int N; + + + +struct pair_compare { + + bool operator() (const std::pair & p1, const std::pair & p2) const { + + if(p1.first == p2.first) + + return (p1.second < p2.second); + + else + + return (p1.first < p2.first); + + } + +}; + + + +vector> P; + + + +long long findpoints(int left,int right){ + + long long dist=LLONG_MAX,dx,dy; + + if((right-left)<=2){ + + for(int i=left;i> Q; + + for(int i=left;i<=right;i++){ + + dx=P[i].first-P[mid].first; dx*=dx; + + if(dxmid && Q[j].second>mid){ + + continue; + + } + + dx=P[Q[i].second].first-P[Q[j].second].first; dx*=dx; + + dy=P[Q[i].second].second-P[Q[j].second].second; dy*=dy; + + if((dx+dy) + + + + +using namespace std; + + + + + +inline long double getDist(pair a,pair b) + +{ + + long double d1=(long double)(a.first-b.first); + + long double d2=(long double)(a.second-b.second); + + long double d=d1*d1+d2*d2; + + d=sqrt((long double)d); + + return d; + +} + +vector > x,v; + + + +inline long double divimp(int st,int dr,vector > &y) + +{ + + if(st>=dr) + + { + + return (long double)(LLONG_MAX); + + } + + + + if(st==dr-1) + + { + + if(y[st]>y[dr]) swap(y[st],y[dr]); + + return getDist(x[st],x[dr]); + + } + + + + if(st==(dr-2)) + + { + + sort(y.begin()+st,y.begin()+dr+1); + + return min(getDist(x[st],x[st+2]),min(getDist(x[st],x[st+1]),getDist(x[st+1],x[st+2]))); + + } + + + + int mid=(st+dr)>>1; + + long double best; + + + + best=min(divimp(st,mid-1,y),divimp(mid,dr,y)); + + + + merge(y.begin()+st,y.begin()+mid,y.begin()+mid,y.begin()+dr+1,v.begin()); + + copy(v.begin(),v.begin()+(dr-st+1),y.begin()+st); + + + + int sz=0; + + + + for(int i=st;i<=dr;i++) + + if(fabs(y[i].second-x[mid].first)<=best) v[++sz]=y[i]; + + + + for(int i=1;i<=sz;i++) + + for(int j=i+1;j<=sz && (j-i)<=10;j++) + + best=min(best,getDist(v[i],v[j])); + + return best; + +} + +int n; + +int a,b; + +vector > y; + + + +int main() + +{ + + ifstream fin("cmap.in"); + + ofstream fout("cmap.out"); + + + + fin>>n; + + + + for(int i=1;i<=n;i++) + + { + + fin>>a>>b; + + + + x.push_back(make_pair(a,b)); + + } + + + + sort(x.begin(),x.end()); + + + + for(int i=0;i +#include +#include +#include +#include +#include +using namespace std; + + + +const double eps = 1e-10; + +const int NMAX = 1503; + +const int MOD = 104659; + +const int oo = 0x3f3f3f3f; + +bitset viz; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, x, y, r[NMAX]; + +double cost; + + + +vector d(NMAX, oo); + + + +inline bool egal(double a, double b) + +{ + + return (abs(a - b) <= eps); + +} + + + +vector < pair > g[NMAX]; + +queue q; + + + +void BellmanFord() + +{ + + q.push(1); + + d[1] = 0; + + r[1] = 1; + + while (!q.empty()) + + { + + int x = q.front(); + + q.pop(); + + viz[x] = 0; + + for (size_t i = 0; i < g[x].size(); ++i) + + { + + int nod = g[x][i].first; + + if (egal(d[x] + g[x][i].second, d[nod])) + + { + + r[nod] += r[x]; + + if (r[nod] > MOD) r[nod] -= MOD; + + } + + else if (d[x] + g[x][i].second < d[nod]) + + { + + d[nod] = d[x] + g[x][i].second; + + r[nod] = r[x]; + + if (viz[nod] == 0) + + { + + viz[nod] = 1; + + q.push(nod); + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> cost; + + cost = log(cost); + + g[x].push_back({ y, cost }); + + } + + BellmanFord(); + + for (int i = 2; i <= n; ++i) + + fout << r[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/01056a21-82ef-40cb-ae87-6406c6f435c4.cpp b/distmin/bellman/01056a21-82ef-40cb-ae87-6406c6f435c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c3cd36faeeea05f1589bf8a4d9deede6de89a1 --- /dev/null +++ b/distmin/bellman/01056a21-82ef-40cb-ae87-6406c6f435c4.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +vector < pair < int, double > > graf[1510]; + +queue < int > q; + +double c[1510]; + +int sol[1510], n, m, viz[1510]; + + + +inline double modul(double x) + +{ + + if(x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + double cost; + + int x, y, cs ,nod, nod2; + + f >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + f >> x >> y >> cs; + + double z = log(cs); + + graf[x].push_back(make_pair(y, z)); + + graf[y].push_back(make_pair(x, z)); + + } + + c[1] = 0; + + for(int i = 2; i <= n; ++i) c[i] = 0x3f3f3f3f; + + q.push(1); + + sol[1] = 1; + + viz[1] = 1; + + while(!q.empty()) + + { + + nod = q.front(); + + q.pop(); + + for(int i = 0; i < graf[nod].size(); ++i) + + { + + nod2 = graf[nod][i].first; + + cost = graf[nod][i].second; + + if(c[nod2] - 1e-8 > c[nod] + cost) + + { + + c[nod2] = c[nod] + cost; + + sol[nod2] = sol[nod]; + + if(!viz[nod2]) q.push(nod2), viz[nod2] = 1; + + } + + else if( modul( c[nod2] - c[nod] - cost ) <= 1e-8 ) + + { + + sol[nod2] = (sol[nod2] + sol[nod]) % 104659; + + } + + } + + } + + for(int i = 2; i <= n; ++i) g << sol[i] << ' '; + +} diff --git a/distmin/bellman/025faf0a-02b7-11ec-be48-2cf05d84bfc2.cpp b/distmin/bellman/025faf0a-02b7-11ec-be48-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cc27b6ad740b7b14ce9b16bf38185c1d01043d7 --- /dev/null +++ b/distmin/bellman/025faf0a-02b7-11ec-be48-2cf05d84bfc2.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream f("dmin.in"); ofstream g("dmin.out"); + +inline double modul(double x) + +{ if(x<0) return -x; + + return x; + +} + +vector < pair < int,double > >G[1502]; + +queue Q; + +int n, m, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + +void bellmanFord() + +{ int x, vecin; + + double cost; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { x=Q.front(); + + Q.pop(); + + for (int i=0;idmin[x]+cost) + + { dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) + + { Q.push(vecin); viz[vecin]=1; } + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + +} + +int main() + +{ double z,cost; + + f>>n>>m; + + for (int x,y,i=1;i<=m;i++) + + { f>>x>>y>>cost; + + z=log(cost); + + G[x].push_back(make_pair(y,z)); + + G[y].push_back(make_pair(x,z)); + + } + + nrd[1]=1; + + for (int i=2;i<=n;i++) dmin[i]=1000001; + + bellmanFord(); + + for(int i=2;i<=n;++i) g< +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct comp + +{ + + bool operator()(pair,long double> i1 ,pair,long double> i2 ) + + { + + return i1.second,long double>,comp> Heap; + + + +vector > H[1666]; + +int n,m,i,xx,yy,j,cost; + +int pos[1666]; + +long double dp[1666]; + +bool viz[1666]; + + + +long double mdl(long double value) + +{ + + if (value<0) + + return -value; + + else + + return value; + +} + + + +bool equal(long double AA,long double BB) + +{ + + if (mdl(AA-BB)<=0.0000000001) + + return true; + + + + return false; + +} + + + +int main() + +{ + + f>>n>>m; + + + + for(i=1; i<=m; ++i) + + { + + f>>xx>>yy>>cost; + + long double cost2=log10((long double)cost); + + H[xx].push_back(make_pair(yy,cost2)); + + H[yy].push_back(make_pair(xx,cost2)); + + } + + + + pos[1]=1; + + Heap.insert(make_pair(make_pair(1,1),0)); + + + + while (Heap.empty()==false) + + { + + pair,long double> next=*(Heap.begin()); + + Heap.erase(Heap.begin()); + + + + if(next.first.second!=1) + + if (equal(dp[(next.first).second],next.second)==true||viz[next.first.second]==false) + + { + + pos[(next.first).second]+=pos[(next.first).first]; + + pos[(next.first).second]%=104659; + + } + + if (viz[next.first.second]==true) + + continue; + + + + viz[next.first.second]=true; + + dp[next.first.second]=next.second; + + + + int N=H[next.first.second].size(); + + + + for( i=0; i +# include +# include +# include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +struct nod{ + + int vecin; + + double cost; + +}; + +vector Lista[1510]; + +queue q; + +nod a; + +bool Marcat[1510]; + +int var[1510],sol[1510],n,m,i,x,y,c; + +double d[1510]; + +bool egal(double a,double b){ + + if(a-b<=1e-7&&a-b>=-1e-7) + + return 1; + + return 0; + +} + +void bmfrd(){ + + Marcat[1]=1; + + var[1]=1; + + q.push(1); + + d[1]=1; + + while(!q.empty()){ + + int nc=q.front(); + + q.pop(); + + for(int i=0;i>n>>m; + + for(i=2;i<=n;i++) + + d[i]=1000000000; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>c; + + a.vecin=y; + + a.cost=log(c); + + Lista[x].push_back(a); + + a.vecin=x; + + Lista[y].push_back(a); + + } + + bmfrd(); + + for(i=2;i<=n;i++) + + fout< +#include +#include + + +using namespace std; + + + +typedef struct lol { + + int x,y; + + double z; + +}troll; + + + +const double EPS=1e-7; + +const int MOD=104659; + + + +int i,n,m,rs[1505]; + +double d[1505]; + +troll a[5005]; + + + +bool cmp(const troll &a,const troll &b) { return a.x0 ? a:-a; } + + + +int main() + +{ + + ifstream cin("dmin.in"); + + ofstream cout("dmin.out"); + + + + ios_base::sync_with_stdio(0); + + + + cin>>n>>m; + + + + for(i=1,m*=2;i>a[i].x>>a[i].y>>a[i].z; a[i].z=log(a[i++].z); + + a[i].x=a[i-1].y; a[i].y=a[i-1].x; a[i].z=a[i-1].z; + + } + + + + for(i=2;i<=n;++i) d[i]=-1; + + + + sort(a+1,a+m+1,cmp); + + + + for(rs[1]=i=1;i<=m;++i) + + if(d[a[i].x]!=-1 && (d[a[i].y]==-1 || d[a[i].y]-d[a[i].x]-a[i].z>EPS)) + + d[a[i].y]=d[a[i].x]+a[i].z,rs[a[i].y]=rs[a[i].x]; + + else if(d[a[i].x]!=-1 && ABS(d[a[i].y]-d[a[i].x]-a[i].z) +using namespace std; + + + +deque Q; + +vector > G[1501]; + + + +double dist[1501]; + +int n, m, answer[1501]; + +bool inq[1501]; + + + +void bellmanford() + +{ + + int son, node, i; + + double edge; + + + + Q.push_back(1); + + inq[1] = 1; + + while(!Q.empty()) { + + node = Q.front(); + + Q.pop_front(); + + inq[node] = 0; + + for(i=0; i= 104659) answer[son] -= 104659; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); + + } + + continue; + + } + + if(dist[son] > dist[node] + edge) { + + dist[son] = dist[node] + edge; + + answer[son] = answer[node]; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); } } } } + +} + + + +int main() + +{ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + + + int i, x, y; + + double z; + + + + scanf("%d%d", &n, &m); + + for(i=1; i<=m; ++i) { + + scanf("%d%d%lf", &x, &y, &z); + + z = log10(z); + + G[x].push_back({y, z}); + + G[y].push_back({x, z}); + + } + + + + for(i=1; i<=n; ++i) + + dist[i] = (1e8); + + + + dist[1] = 0; + + answer[1] = 1; + + bellmanford(); + + + + for(i=2; i<=n; ++i) + + printf("%d ", answer[i]); + + + + return 0; + +} diff --git a/distmin/bellman/08bac163-02b7-11ec-b9b0-2cf05d84bfc2.cpp b/distmin/bellman/08bac163-02b7-11ec-b9b0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f83dd7c3720e806ac1376bcde4fc9fd25122c44 --- /dev/null +++ b/distmin/bellman/08bac163-02b7-11ec-b9b0-2cf05d84bfc2.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + nrd[1]=1; + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} + + + +void bellmanFord() + +{ + + int x, vecin; + + double cost; + + int i; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + for (i=0;idmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + +int main() + +{ + + citire(); + + bellmanFord(); + + for(i=2;i<=n;++i) g< +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct Muchie{ + + int vecin; + + double cost; + +}; + + + +queue Q; + +vector A[1501]; + +int n, m, nrd[1501], inq[1501]; + +double dmin[1501]; + + + +int main() + +{ + + int a, b, i; + + double c; + + Muchie T; + + + + + + f>>n>>m; + + for(i = 1; i <= m; i++) { + + f>>a>>b>>c; + + c = log(c); + + T.vecin = b; T.cost = c; + + A[a].push_back(T); + + T.vecin = a; T.cost = c; + + A[b].push_back(T); + + } + + + + + + dmin[1] = 0; + + for(i = 2; i <= n; i++) dmin[i] = 1000000001; + + nrd[1] = 1; + + for(i = 2; i <= n; i++) nrd[i] = 0; + + + + + + Q.push(1); inq[1] = 1; + + while(!Q.empty()) { + + int x = Q.front(); Q.pop(); inq[x] = 0; + + for(i = 0; i < A[x].size(); i++) { + + int vecin = A[x][i].vecin; + + double cost = A[x][i].cost; + + double dif = dmin[vecin] - dmin[x] - cost; + + if(dif < 0) dif = -dif; + + if(dif < 0.0000000001) + + nrd[vecin] = (nrd[vecin] + nrd[x]) % 104659; + + else + + if(dmin[vecin] > dmin[x] + cost) { + + dmin[vecin] = dmin[x] + cost; + + nrd[vecin] = nrd[x]; + + if(!inq[vecin]) { + + Q.push(vecin); + + inq[vecin] = 1; + + } + + } + + } + + } + + + + for(i = 2; i <= n; i++) g< +using namespace std; + + + +deque Q; + +vector > G[1501]; + + + +double dist[1501]; + +int n, m, answer[1501]; + +bool inq[1501]; + + + +void bellmanford() + +{ + + int son, node, i; + + double edge; + + + + Q.push_back(1); + + inq[1] = 1; + + while(!Q.empty()) { + + node = Q.front(); + + Q.pop_front(); + + inq[node] = 0; + + for(i=0; i= 104659) answer[son] -= 104659; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); + + } + + continue; + + } + + if(dist[son] > dist[node] + edge) { + + dist[son] = dist[node] + edge; + + answer[son] = answer[node]; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); } } } } + +} + + + +int main() + +{ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + + + int i, x, y; + + double z; + + + + scanf("%d%d", &n, &m); + + for(i=1; i<=m; ++i) { + + scanf("%d%d%lf", &x, &y, &z); + + z = log10(z); + + G[x].push_back({y, z}); + + G[y].push_back({x, z}); + + } + + + + for(i=1; i<=n; ++i) + + dist[i] = (1e8); + + + + dist[1] = 0; + + answer[1] = 1; + + bellmanford(); + + + + for(i=2; i<=n; ++i) + + printf("%d ", answer[i]); + + + + return 0; + +} diff --git a/distmin/bellman/093df2b6-02b7-11ec-90db-2cf05d84bfc2.cpp b/distmin/bellman/093df2b6-02b7-11ec-90db-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..329705bfcd12cde3d1be6c74bf407978b6f0a462 --- /dev/null +++ b/distmin/bellman/093df2b6-02b7-11ec-90db-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +vector > g[1501]; + +int n, nrd[1501]; + +double d[1501]; + +void citire() + +{ + + int m, i, j; + + double c; + + fin>>n>>m; + + while(m) + + { + + m--; + + fin>>i>>j>>c; + + c=log(c); + + g[i].push_back(make_pair(j, c)); + + g[j].push_back(make_pair(i, c)); + + } + + fin.close(); + +} + +void bellmanFord() + +{ + + int i, k; + + bool viz[1501]={0}; + + queue q; + + vector > :: iterator it; + + for(i=2; i<=n; i++) + + d[i]=INT_MAX; + + nrd[1]=1; + + viz[1]=1; + + q.push(1); + + while(!q.empty()) + + { + + k=q.front(); + + q.pop(); + + for(it=g[k].begin(); it!=g[k].end(); it++) + + if(d[it->first]-1e-8>d[k]+it->second) + + { + + d[it->first]=d[k]+it->second; + + nrd[it->first]=nrd[k]; + + if(!viz[it->first]) + + q.push(it->first), viz[it->first]=1; + + } + + else if(abs(d[it->first]-d[k]-it->second)<=1e-8) + + nrd[it->first]=(nrd[it->first]+nrd[k])%104659; + + } + +} + +int main() + +{ + + citire(); + + bellmanFord(); + + for(int i=2; i<=n; i++) + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/0a11fb66-02b7-11ec-b53b-2cf05d84bfc2.cpp b/distmin/bellman/0a11fb66-02b7-11ec-b53b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1282b7226c5f95baf3d33659eff9c7104b879c2f --- /dev/null +++ b/distmin/bellman/0a11fb66-02b7-11ec-b53b-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include +using namespace std; + + + +int egal(double,double); + +void read(); + +void bellmanford(); + +vector > G[1505]; + +int nrd[1505],inQueue[1505],n,m; + +double dmin[1505]; + +int main(){ + + read(); + + bellmanford(); + + freopen("dmin.out","w",stdout); + + for (int i=2;i<=n;i++){ + + cout< q; + + q.push(1); + + nrd[1]=1; + + inQueue[1]=1; + + while (!q.empty()){ + + int nod=q.front(); + + inQueue[nod]=0; + + q.pop(); + + for (int i=0;idmin[nod]+cost+1e-9){ + + nrd[vecin]=nrd[nod]%104659; + + dmin[vecin]=dmin[nod]+cost; + + if (!inQueue[vecin]){ + + q.push(vecin); + + inQueue[vecin]=1; + + } + + } + + else { + + if (egal(dmin[vecin],dmin[nod]+cost)){ + + nrd[vecin]=((nrd[nod]%104659)+(nrd[vecin]%104659))%104659; + + } + + } + + } + + } + +} + + + +void read(){ + + freopen("dmin.in","r",stdin); + + scanf("%d%d",&n,&m); + + for (int i=1;i<=m;i++){ + + int x,y,c; + + scanf("%d%d%d",&x,&y,&c); + + double nc=log(c); + + G[x].push_back(make_pair(y,nc)); + + G[y].push_back(make_pair(x,nc)); + + } + +} + + + +int egal(double x,double y){ + + return abs(x-y)<1e-9; + +} diff --git a/distmin/bellman/0a2c4773-02b7-11ec-9a36-2cf05d84bfc2.cpp b/distmin/bellman/0a2c4773-02b7-11ec-9a36-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a051c99608af10b370917d00e85d82651b42281 --- /dev/null +++ b/distmin/bellman/0a2c4773-02b7-11ec-9a36-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +void citire(); + +void bellmanFord(); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +int main() + +{ + + citire(); + + bellmanFord(); + + + + for(i=2;i<=n;++i) g<dmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + + + + + nrd[1]=1; + + + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} diff --git a/distmin/bellman/0a5d4d8b-02b7-11ec-9f27-2cf05d84bfc2.cpp b/distmin/bellman/0a5d4d8b-02b7-11ec-9f27-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cfc2dd3b389ddf99ee91e864d2a391a74442de0 --- /dev/null +++ b/distmin/bellman/0a5d4d8b-02b7-11ec-9f27-2cf05d84bfc2.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + double logc = log(c); + + G[a].push_back(make_pair(b, logc)); + + G[b].push_back(make_pair(a, logc)); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/0a7534fd-02b7-11ec-af65-2cf05d84bfc2.cpp b/distmin/bellman/0a7534fd-02b7-11ec-af65-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b68ba9b2b642443ce2a45231b3aa8eac9aaf8d6 --- /dev/null +++ b/distmin/bellman/0a7534fd-02b7-11ec-af65-2cf05d84bfc2.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, nrd[1501]; + +double M[1501][1501], dmin[1501]; + +bool inq[1501]; + + + +queue Q; + + + +void citire() + +{ + + int a, b, t; + + fin >> n >> m; + + for(int i = 1; i <= n; i++) + + for(int j = 1; j <= n; j++) + + if(i != j) M[i][j] = 2000000000; + + while(m--) + + fin >> a >> b >> t, M[a][b] = M[b][a] = log(t); + +} + + + +void bellman_ford(int nod) + +{ + + Q.push(nod); + + inq[nod] = 1; + + for(int i = 1; i <= n; ++i) dmin[i] = 2000000000; + + dmin[1] = 0; + + nrd[1] = 1; + + while(!Q.empty()) + + { + + int x = Q.front(); + + Q.pop(); inq[x] = 0; + + for(int i = 1; i <= n; ++i){ + + if(x != i) + + if(M[x][i] != 2000000000 && dmin[i] > dmin[x] + M[x][i] + 0.0000000001){ + + dmin[i] = dmin[x] + M[x][i]; + + nrd[i] = nrd[x]; + + if(inq[i] == 0) Q.push(i), inq[i] = 1; + + } + + else if(fabs(dmin[i] - dmin[x] - M[x][i]) < 0.0000000001 && M[x][i] != 2000000000) nrd[i] = (nrd[i] % 104659 + nrd[x] % 104659) % 104659; + + } + + } + +} + + + +int main() + +{ + + citire(); + + bellman_ford(1); + + for(int i = 2; i <= n; i++) + + fout << nrd[i] << ' '; + + return 0; + +} diff --git a/distmin/bellman/0a8c255b-02b7-11ec-90e2-2cf05d84bfc2.cpp b/distmin/bellman/0a8c255b-02b7-11ec-90e2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68fa80c948e8ba4b3afd3ab38b7aefa8c566e788 --- /dev/null +++ b/distmin/bellman/0a8c255b-02b7-11ec-90e2-2cf05d84bfc2.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) + + abs = -abs; + + if(abs < 0.000000001) + + return 0; + + if(x > y) + + return 1; + + if(x < y) + + return -1; + +} + +void BellmanFord() { + + queue Q; + + vector inQueue; inQueue.assign(N + 1, 0); + + dmin[1] = 0; + + nrd[1] = 1; + + Q.push(1); + + inQueue[1] = 1; + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { + + inQueue[neigh] = 1; Q.push(neigh); + + } + + } + + } + + } + +} + + + +int main() + +{ + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + + BellmanFord(); + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + + + + + + return 0; + +} diff --git a/distmin/bellman/0b0a2b21-02b7-11ec-af71-2cf05d84bfc2.cpp b/distmin/bellman/0b0a2b21-02b7-11ec-af71-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c3cd36faeeea05f1589bf8a4d9deede6de89a1 --- /dev/null +++ b/distmin/bellman/0b0a2b21-02b7-11ec-af71-2cf05d84bfc2.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +vector < pair < int, double > > graf[1510]; + +queue < int > q; + +double c[1510]; + +int sol[1510], n, m, viz[1510]; + + + +inline double modul(double x) + +{ + + if(x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + double cost; + + int x, y, cs ,nod, nod2; + + f >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + f >> x >> y >> cs; + + double z = log(cs); + + graf[x].push_back(make_pair(y, z)); + + graf[y].push_back(make_pair(x, z)); + + } + + c[1] = 0; + + for(int i = 2; i <= n; ++i) c[i] = 0x3f3f3f3f; + + q.push(1); + + sol[1] = 1; + + viz[1] = 1; + + while(!q.empty()) + + { + + nod = q.front(); + + q.pop(); + + for(int i = 0; i < graf[nod].size(); ++i) + + { + + nod2 = graf[nod][i].first; + + cost = graf[nod][i].second; + + if(c[nod2] - 1e-8 > c[nod] + cost) + + { + + c[nod2] = c[nod] + cost; + + sol[nod2] = sol[nod]; + + if(!viz[nod2]) q.push(nod2), viz[nod2] = 1; + + } + + else if( modul( c[nod2] - c[nod] - cost ) <= 1e-8 ) + + { + + sol[nod2] = (sol[nod2] + sol[nod]) % 104659; + + } + + } + + } + + for(int i = 2; i <= n; ++i) g << sol[i] << ' '; + +} diff --git a/distmin/bellman/0b5c18d4-02b7-11ec-8bc8-2cf05d84bfc2.cpp b/distmin/bellman/0b5c18d4-02b7-11ec-8bc8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/0b5c18d4-02b7-11ec-8bc8-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/0b954efa-02b7-11ec-ad62-2cf05d84bfc2.cpp b/distmin/bellman/0b954efa-02b7-11ec-ad62-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d13dfeaa4cc915a0dca8b9e07644ef981548cd9 --- /dev/null +++ b/distmin/bellman/0b954efa-02b7-11ec-ad62-2cf05d84bfc2.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + for ( vector < pair < int , double > > :: iterator it = TT[node].begin() ; it != TT[node].end() ; ++it ) + + { + + int current = it->first ; + + if( dist[current] - 0.0000001 > dist[node] + it->second ) + + { + + dist[current] = dist[node] + it->second; + + paths[current] = paths[node]; + + if(inQueue[current] == 0) + + { + + Q.push(current); + + inQueue[current] = 1; + + } + + } + + else + + { + + if(dist[current] + 0.0000001 >= dist[node] + it->second ) + + { + + paths[current] += paths[node]; + + paths[current] %= 104659 ; + + } + + + + } + + } + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/0bae76e5-02b7-11ec-9d4e-2cf05d84bfc2.cpp b/distmin/bellman/0bae76e5-02b7-11ec-9d4e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfed5cd80a159d8699d9cbc17a92414a4b2c5879 --- /dev/null +++ b/distmin/bellman/0bae76e5-02b7-11ec-9d4e-2cf05d84bfc2.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/0bc9894c-02b7-11ec-a408-2cf05d84bfc2.cpp b/distmin/bellman/0bc9894c-02b7-11ec-a408-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef601345171c2cd08570f16f9532204b079f5fd5 --- /dev/null +++ b/distmin/bellman/0bc9894c-02b7-11ec-a408-2cf05d84bfc2.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +using namespace std; +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int n,m; + +vector < pair < int , double > > G[1501]; + +vector < int > used; + +vector < double > d; + +vector < int > drmin; + + + + + +void read() + +{ + + f >> n >> m; + + used = vector < int > (n+1); + + drmin = vector < int > (n+1,0); + + for ( ; m-- ; ) + + { + + int x,y,z; + + f >> x >> y >> z; + + double cst = log(z); + + G[x].push_back(make_pair(y,cst)); + + G[y].push_back(make_pair(x,cst)); + + } + +} + + + +void bellman ( int i ) + +{ + + queue < int > q; + + d = vector < double > (n+1); + + for ( int k = 1; k <= n ; k++ ) + + d[k] = 10000000.0; + + d[i] = 0.0; + + q.push(i); + + drmin[i] = 1; + + used[i] = 1; + + while ( !q.empty() ) + + { + + int nod = q.front(); + + q.pop(); + + for ( vector < pair < int , double > > ::iterator j = G[nod].begin(); j != G[nod].end(); j++ ) + + { + + int x = (*j).first; + + double cost = (*j).second; + + if( abs( d[x] - d[nod] - cost ) <= 0.0000001 ) + + { + + drmin[x] = (drmin[x] + drmin[nod])%104659; + + } + + if ( d[x] - d[nod] - cost > 0.0000001 ) + + { + + d[x] = d[nod] + cost; + + drmin[x] = drmin[nod]; + + if ( !used[x] ) + + { + + q.push(x); + + used[x] = 1; + + } + + } + + } + + } + + for ( i = 2; i <= n ; i++ ) + + g << drmin[i]%104659 << " "; + +} + + + +int main() + +{ + + read(); + + bellman(1); + + return 0; + +} diff --git a/distmin/bellman/0be4e1b5-02b7-11ec-a6dd-2cf05d84bfc2.cpp b/distmin/bellman/0be4e1b5-02b7-11ec-a6dd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/0be4e1b5-02b7-11ec-a6dd-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/0bfbc540-02b7-11ec-b854-2cf05d84bfc2.cpp b/distmin/bellman/0bfbc540-02b7-11ec-b854-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faacd8ce6b694d6991c19d898050389a815655bb --- /dev/null +++ b/distmin/bellman/0bfbc540-02b7-11ec-b854-2cf05d84bfc2.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const double inf= 0x3f3f3f; + +const double eps= 0.0000001; + +const int nmax= 1500; + +const int mod= 104659; + + + +double minim[nmax+1]; + +int d[nmax+1]; + + + +struct str { + + int x; + + double y; + +}; + + + +struct str_cmp { + + bool operator () ( const str &x, const str &y ) { + + return x.y>y.y; + + } + +}; + + + +priority_queue , str_cmp> h; + +vector v[nmax+1]; + + + +inline str mp( int x, double y ) { + + str sol; + + sol.x= x, sol.y= y; + + + + return sol; + +} + + + +double av( double x ) { + + if ( x<0 ) { + + x= -x; + + } + + return x; + +} + + + +void dijkstra( ) { + + d[1]= 1; + + for ( h.push(mp(1, 0)); !h.empty(); ) { + + str x= h.top(); + + h.pop(); + + + + if ( x.y-minim[x.x]>=eps ) { + + continue; + + } + + + + for ( vector ::iterator it= v[x.x].begin(); it!=v[x.x].end(); ++it ) { + + if ( minim[(*it).x]-minim[x.x]-(*it).y>eps ) { + + minim[(*it).x]= minim[x.x]+(*it).y; + + d[(*it).x]= d[x.x]; + + h.push(mp((*it).x, minim[(*it).x])); + + } else if ( av(minim[(*it).x]-minim[x.x]-(*it).y)<=eps ) { + + d[(*it).x]= (d[(*it).x]+d[x.x])%mod; + + } + + } + + } + +} + + + +int main( ) { + + int n, m; + + fin>>n>>m; + + for ( int i= 1; i<=m; ++i ) { + + int x, y; + + double z; + + fin>>x>>y>>z; + + z= log(z); + + + + v[x].push_back(mp(y, z)); + + v[y].push_back(mp(x, z)); + + } + + + + for ( int i= 2; i<=n; ++i ) { + + minim[i]= inf; + + } + + dijkstra(); + + + + for ( int i= 2; i<=n; ++i ) { + + fout< +#include +#include + + +using namespace std; + + + +typedef struct lol { + + int x,y; + + double z; + +}troll; + + + +const double EPS=1e-7; + +const int MOD=104659; + + + +int i,n,m,rs[1505]; + +double d[1505]; + +troll a[5005]; + + + +bool cmp(const troll &a,const troll &b) { return a.x0 ? a:-a; } + + + +int main() + +{ + + ifstream cin("dmin.in"); + + ofstream cout("dmin.out"); + + + + ios_base::sync_with_stdio(0); + + + + cin>>n>>m; + + + + for(i=1,m*=2;i>a[i].x>>a[i].y>>a[i].z; a[i].z=log(a[i++].z); + + a[i].x=a[i-1].y; a[i].y=a[i-1].x; a[i].z=a[i-1].z; + + } + + + + for(i=2;i<=n;++i) d[i]=-1; + + + + sort(a+1,a+m+1,cmp); + + + + for(rs[1]=i=1;i<=m;++i) + + if(d[a[i].x]!=-1 && (d[a[i].y]==-1 || d[a[i].y]-d[a[i].x]-a[i].z>EPS)) + + d[a[i].y]=d[a[i].x]+a[i].z,rs[a[i].y]=rs[a[i].x]; + + else if(d[a[i].x]!=-1 && ABS(d[a[i].y]-d[a[i].x]-a[i].z) +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + vector >::iterator it = TT[node].begin(); + + while(it != TT[node].end()) + + { + + pair neigh = *it; + + if(dist[neigh.first] - 1e-8 > dist[node] + neigh.second) + + { + + dist[neigh.first] = dist[node] + neigh.second; + + paths[neigh.first] = paths[node]; + + if(inQueue[neigh.first] == 0) + + { + + Q.push(neigh.first); + + inQueue[neigh.first] = 1; + + } + + } + + else + + { + + if(dist[neigh.first] + 1e-8 >= dist[node] + neigh.second) + + { + + paths[neigh.first] += paths[node]; + + paths[neigh.first] %= 104659 ; + + } + + } + + + + it++; + + } + + + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/0c8ab08d-02b7-11ec-867e-2cf05d84bfc2.cpp b/distmin/bellman/0c8ab08d-02b7-11ec-867e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43849e119e33f17822b0a97b87f6309cf9dfcbf0 --- /dev/null +++ b/distmin/bellman/0c8ab08d-02b7-11ec-867e-2cf05d84bfc2.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include + + +const int kMaxDim = 1505; + +const int kModulo = 104659; + +const double eps = 1e-8; + +const double inf = 1e10; + + + +inline double Abs(const double& a) { + + return (a > 0 ? a : -a); + +} + + + +inline int Compare(const double& a, const double& b) { + + if (Abs(a - b) < eps) + + return 0; + + return (a < b ? -1 : 1); + +} + + + +struct Data { + + int node; + + double cost; + +}; + + + +struct Functor { + + bool operator()(const Data& a, const Data& b) { + + return Compare(a.cost, b.cost) > 0; + + } + +}; + + + +int nodeCount, edgeCount; + +std::vector< Data > edges[kMaxDim]; + +double minDist[kMaxDim]; + +int minDistCount[kMaxDim]; + + + +std::priority_queue< Data, std::vector< Data >, Functor > heap; + + + +int main() { + + std::ifstream inputFile("dmin.in"); + + std::ofstream outputFile("dmin.out"); + + + + inputFile >> nodeCount >> edgeCount; + + for (int i = 1; i <= edgeCount; ++i) { + + int first, second; + + double cost; + + inputFile >> first >> second >> cost; + + cost = log(cost); + + + + edges[first].push_back({second, cost}); + + edges[second].push_back({first, cost}); + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + minDist[i] = inf; + + minDistCount[1] = 1; + + heap.push({1, 0}); + + + + while (!heap.empty()) { + + while (!heap.empty() && Compare(minDist[heap.top().node], heap.top().cost) < 0) + + heap.pop(); + + if (heap.empty()) + + break; + + + + int node = heap.top().node; + + double cost = heap.top().cost; + + heap.pop(); + + + + for (Data& edge : edges[node]) { + + if (Compare(minDist[edge.node], cost + edge.cost) > 0) { + + minDist[edge.node] = cost + edge.cost; + + minDistCount[edge.node] = minDistCount[node]; + + heap.push({edge.node, minDist[edge.node]}); + + } + + else if (Compare(minDist[edge.node], cost + edge.cost) == 0) { + + minDistCount[edge.node] += minDistCount[node]; + + minDistCount[edge.node] %= kModulo; + + } + + } + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + outputFile << minDistCount[i] << ' '; + + outputFile << '\n'; + + + + inputFile.close(); + + outputFile.close(); + + + + return 0; + +} diff --git a/distmin/bellman/0ca069d2-02b7-11ec-8e21-2cf05d84bfc2.cpp b/distmin/bellman/0ca069d2-02b7-11ec-8e21-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9bbad51f19471c8959f5fa343d680fe5d39347b --- /dev/null +++ b/distmin/bellman/0ca069d2-02b7-11ec-8e21-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +using namespace std; + +int n, m, i, x, y, nod, vecin, cost, j; + +int viz[1505], num[1505], h[1505], poz[1505]; + +double d[1505], cost2; + +vector< pair > v[1505]; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +double modul(double x){ + + if(x > 0){ + + return x; + + } + + return -x; + +} + +void upd(int pos){ + + int c = pos, p = c / 2; + + while(p > 0 && d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + c = p; + + p = c / 2; + + } + +} + +void elim(){ + + int p = 1, c = 2; + + while(c <= n){ + + if(c + 1 <= n && d[ h[c] ] > d[ h[c + 1] ]){ + + c++; + + } + + if(d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + p = c; + + c = 2 * p; + + } + + else{ + + break; + + } + + } + +} + +int main(){ + + fin>> n >> m; + + for(i = 1; i <= m; i++){ + + fin>> x >> y >> cost; + + cost2 = log(cost); + + v[x].push_back( make_pair(y, cost2) ); + + v[y].push_back( make_pair(x, cost2) ); + + } + + for(i = 1; i <= n; i++){ + + d[i] = 1000000000; + + h[i] = poz[i] = i; + + } + + d[1] = 0; + + num[1] = 1; + + for(i = 1; i <= n; i++){ + + nod = h[1]; + + for(j = 0; j < v[nod].size(); j++){ + + vecin = v[nod][j].first; + + if(viz[vecin] == 0){ + + if(modul(d[vecin] - d[nod] - v[nod][j].second) <= 0.000000001){ + + num[vecin] += num[nod]; + + if(num[vecin] >= 104659){ + + num[vecin] -= 104659; + + } + + } + + else{ + + if(d[nod] + v[nod][j].second < d[vecin]){ + + d[vecin] = d[nod] + v[nod][j].second; + + upd(poz[vecin]); + + num[vecin] = num[nod]; + + } + + } + + } + + } + + viz[nod] = 1; + + d[nod] = 1000000000; + + elim(); + + } + + for(i = 2; i <= n; i++){ + + fout<< num[i] <<" "; + + } + + return 0; + +} diff --git a/distmin/bellman/0cae1d25-da56-4ae5-9952-b50bbf96c516.cpp b/distmin/bellman/0cae1d25-da56-4ae5-9952-b50bbf96c516.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/0cae1d25-da56-4ae5-9952-b50bbf96c516.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/0cbc4996-02b7-11ec-8036-2cf05d84bfc2.cpp b/distmin/bellman/0cbc4996-02b7-11ec-8036-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f5ea1f133f9f1c36129951987173962e089f91c --- /dev/null +++ b/distmin/bellman/0cbc4996-02b7-11ec-8036-2cf05d84bfc2.cpp @@ -0,0 +1,143 @@ +# include +# include +# include +# include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +struct nod{ + + int vecin; + + double cost; + +}; + +vector Lista[1510]; + +queue q; + +nod a; + +bool Marcat[1510]; + +int var[1510],sol[1510],n,m,i,x,y,c; + +double d[1510]; + +bool egal(double a,double b){ + + if(a-b<=1e-7&&a-b>=-1e-7) + + return 1; + + return 0; + +} + +void bmfrd(){ + + Marcat[1]=1; + + var[1]=1; + + q.push(1); + + d[1]=1; + + while(!q.empty()){ + + int nc=q.front(); + + q.pop(); + + for(int i=0;i>n>>m; + + for(i=2;i<=n;i++) + + d[i]=1000000000; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>c; + + a.vecin=y; + + a.cost=log(c); + + Lista[x].push_back(a); + + a.vecin=x; + + Lista[y].push_back(a); + + } + + bmfrd(); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/0d7adc04-02b7-11ec-994c-2cf05d84bfc2.cpp b/distmin/bellman/0d7adc04-02b7-11ec-994c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edc8225b5fbf5a34a535f7b9bbac6d8102d0d948 --- /dev/null +++ b/distmin/bellman/0d7adc04-02b7-11ec-994c-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +inline double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/0da6ea39-02b7-11ec-bdb1-2cf05d84bfc2.cpp b/distmin/bellman/0da6ea39-02b7-11ec-bdb1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b0d6a74f8c23c30d1d706386a7fc7b89eb7fb90 --- /dev/null +++ b/distmin/bellman/0da6ea39-02b7-11ec-bdb1-2cf05d84bfc2.cpp @@ -0,0 +1,209 @@ +#include +#include +using namespace std; + +const int N = 1505, M = 2505, INF = 100000, R = 104659; + +int vf[2 * M], urm[2 * M], lst[N], h[N], poz[N], nrh, nrm, nrd[N]; + +double c[2 * M], v[N]; + +void adauga (int x, int y, double cost) + +{ + + vf[++nrm] = y; + + c[nrm] = cost; + + urm[nrm] = lst[x]; + + lst[x] = nrm; + +} + +void schimba (int p1, int p2) + +{ + + int aux = h[p1]; + + h[p1] = h[p2]; + + h[p2] = aux; + + poz[h[p1]] = p1; + + poz[h[p2]] = p2; + +} + +void urca (int p) + +{ + + while (p > 1 && v[h[p]] < v[h[p>>1]]) + + { + + schimba (p, p >> 1); + + p >>= 1; + + } + +} + +void coboara (int p) + +{ + + int bun = p, fs = 2 * p, fd = 2 * p + 1; + + if (fs <= nrh && v[h[fs]] < v[h[bun]]) + + bun = fs; + + if (fd <= nrh && v[h[fd]] < v[h[bun]]) + + bun = fd; + + if (bun != p) + + { + + schimba (bun, p); + + coboara (bun); + + } + +} + +double make_abs (double x) + +{ + + if (x < 0) + + return -x; + + return x; + +} + +int main () + +{ + + FILE *in, *out; + + in = fopen ("dmin.in", "r"); + + out = fopen ("dmin.out", "w"); + + int n, m; + + fscanf (in, "%d%d", &n, &m); + + int i, x, y; + + double cost; + + for (i = 1; i <= m; i++) + + { + + fscanf (in, "%d%d%lf", &x, &y, &cost); + + cost = log(cost); + + adauga (x, y, cost); + + adauga (y, x, cost); + + } + + for (i = 1; i <= n; i++) + + { + + v[i] = INF; + + poz[i] = i; + + h[i] = i; + + } + + nrh = n; + + double eps = 0.00000001; + + v[1] = 0; + + nrd[1] = 1; + + int p; + + double aux; + + while (nrh > 0) + + { + + x = h[1]; + + schimba (1, nrh--); + + coboara (1); + + p = lst[x]; + + while (p != 0) + + { + + y = vf[p]; + + cost = c[p]; + + aux = cost + v[x]; + + if (v[y] - aux > eps) + + { + + v[y] = cost + v[x]; + + nrd[y] = nrd[x]; + + urca (poz[y]); + + } + + else + + if (make_abs (aux - v[y]) <= eps) + + { + + nrd[y] += nrd[x]; + + nrd[y] %= R; + + } + + p = urm[p]; + + } + + } + + for (i = 2; i <= n; i++) + + fprintf (out, "%d ", nrd[i]); + + return 0; + +} diff --git a/distmin/bellman/0e3dc97e-f3ce-44c7-80be-8815d53adf41.cpp b/distmin/bellman/0e3dc97e-f3ce-44c7-80be-8815d53adf41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..329705bfcd12cde3d1be6c74bf407978b6f0a462 --- /dev/null +++ b/distmin/bellman/0e3dc97e-f3ce-44c7-80be-8815d53adf41.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +vector > g[1501]; + +int n, nrd[1501]; + +double d[1501]; + +void citire() + +{ + + int m, i, j; + + double c; + + fin>>n>>m; + + while(m) + + { + + m--; + + fin>>i>>j>>c; + + c=log(c); + + g[i].push_back(make_pair(j, c)); + + g[j].push_back(make_pair(i, c)); + + } + + fin.close(); + +} + +void bellmanFord() + +{ + + int i, k; + + bool viz[1501]={0}; + + queue q; + + vector > :: iterator it; + + for(i=2; i<=n; i++) + + d[i]=INT_MAX; + + nrd[1]=1; + + viz[1]=1; + + q.push(1); + + while(!q.empty()) + + { + + k=q.front(); + + q.pop(); + + for(it=g[k].begin(); it!=g[k].end(); it++) + + if(d[it->first]-1e-8>d[k]+it->second) + + { + + d[it->first]=d[k]+it->second; + + nrd[it->first]=nrd[k]; + + if(!viz[it->first]) + + q.push(it->first), viz[it->first]=1; + + } + + else if(abs(d[it->first]-d[k]-it->second)<=1e-8) + + nrd[it->first]=(nrd[it->first]+nrd[k])%104659; + + } + +} + +int main() + +{ + + citire(); + + bellmanFord(); + + for(int i=2; i<=n; i++) + + fout< +#include +#include +#include +#include + + + + +using namespace std; + +int n, m; +int cate[1550], used[1550]; +double best[1550]; +const double EPS = 1e-9; +struct leg +{ + int y; + double c; + leg(int y = 0, double c = 0) : y(y), c(c) { } + bool operator ()(int a, int b) + { + return best[a] > best[b]; + } +}; +bool eq(double a, double b) +{ + return (a-b) >= -EPS && (a-b) <= EPS; +} +vector graf[1550]; +void citire() +{ + scanf("%d %d", &n, &m); + for (int i = 1; i <= m; i++) { + int x, y, rc; + scanf("%d %d %d", &x, &y, &rc); + double c = log(1.0*rc); + graf[x].push_back(leg(y, c)); + graf[y].push_back(leg(x, c)); + } +} +priority_queue, leg> heap; +void solve() +{ + for (int i = 1; i <= n; i++) best[i] = 5e100; + best[1] = 0, cate[1] = 1; + heap.push(1); + while (!heap.empty()) { + int top = heap.top(); + heap.pop(); + if (used[top]) + continue; + used[top] = 1; + for (auto v : graf[top]) { + if (eq(best[top] + v.c, best[v.y])) + cate[v.y] = (cate[v.y] + cate[top]) % 104659; + else if (best[top] + v.c < best[v.y]) { + best[v.y] = best[top] + v.c; + cate[v.y] = cate[top]; + heap.push(v.y); + } + } + } +} + +void afisare() +{ + for (int i = 2; i <= n; i++) + printf("%d ", cate[i]); +} + +int main() +{ + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + + citire(); + solve(); + afisare(); + + return 0; +} diff --git a/distmin/bellman/0ee4009e-442b-4134-b3c4-80817f3ec12e.cpp b/distmin/bellman/0ee4009e-442b-4134-b3c4-80817f3ec12e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1282b7226c5f95baf3d33659eff9c7104b879c2f --- /dev/null +++ b/distmin/bellman/0ee4009e-442b-4134-b3c4-80817f3ec12e.cpp @@ -0,0 +1,130 @@ +#include +using namespace std; + + + +int egal(double,double); + +void read(); + +void bellmanford(); + +vector > G[1505]; + +int nrd[1505],inQueue[1505],n,m; + +double dmin[1505]; + +int main(){ + + read(); + + bellmanford(); + + freopen("dmin.out","w",stdout); + + for (int i=2;i<=n;i++){ + + cout< q; + + q.push(1); + + nrd[1]=1; + + inQueue[1]=1; + + while (!q.empty()){ + + int nod=q.front(); + + inQueue[nod]=0; + + q.pop(); + + for (int i=0;idmin[nod]+cost+1e-9){ + + nrd[vecin]=nrd[nod]%104659; + + dmin[vecin]=dmin[nod]+cost; + + if (!inQueue[vecin]){ + + q.push(vecin); + + inQueue[vecin]=1; + + } + + } + + else { + + if (egal(dmin[vecin],dmin[nod]+cost)){ + + nrd[vecin]=((nrd[nod]%104659)+(nrd[vecin]%104659))%104659; + + } + + } + + } + + } + +} + + + +void read(){ + + freopen("dmin.in","r",stdin); + + scanf("%d%d",&n,&m); + + for (int i=1;i<=m;i++){ + + int x,y,c; + + scanf("%d%d%d",&x,&y,&c); + + double nc=log(c); + + G[x].push_back(make_pair(y,nc)); + + G[y].push_back(make_pair(x,nc)); + + } + +} + + + +int egal(double x,double y){ + + return abs(x-y)<1e-9; + +} diff --git a/distmin/bellman/0f7d636b-02b7-11ec-aa60-2cf05d84bfc2.cpp b/distmin/bellman/0f7d636b-02b7-11ec-aa60-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d94bbe5f4a2fc056def6fddba1ac6bd905168dc --- /dev/null +++ b/distmin/bellman/0f7d636b-02b7-11ec-aa60-2cf05d84bfc2.cpp @@ -0,0 +1,158 @@ +#include +FILE *fin = freopen("dmin.in", "r", stdin); + +FILE *fout = freopen("dmin.out", "w", stdout); + + + +using namespace std; + +int V, E, sol[1502]; + +double D[1502]; + +struct comp + +{ + + bool operator() (const pair &a, const pair &b) + + { + + if(fabs(a.second - b.second) < 0.0000000001) + + return 0; + + else return a.second > b.second + 0.0000000001; + + } + +}; + +priority_queue , vector >, comp> Q; + +vector > G[1502]; + +bool F[1502]; + +void read() + +{ + + int x, y; + + double z; + + scanf("%d %d", &V, &E); + + while(E --) + + { + + scanf("%d %d %lf", &x, &y, &z); + + z = log(z); + + G[x].push_back(pair (y, z)); + + G[y].push_back(pair (x, z)); + + } + + for(int i = 2; i <= V; ++ i) + + D[i] = 2000000000.0; + + Q.push(pair (1, 0.0)); + + sol[1] = 1; + +} + +void solve() + +{ + + int x, y; + + double z; + + while(!Q.empty()) + + { + + x = Q.top().first; + + + + Q.pop(); + + int sz = G[x].size(); + + if (F[x]) + + continue; + + + + for(int i = 0; i < sz; ++ i) + + { + + y = G[x][i].first; + + z = G[x][i].second; + + if (!F[y]) + + { + + if(D[y] > D[x] + z + 0.0000000001) + + { + + D[y] = D[x] + z; + + sol[y] = sol[x]; + + Q.push(pair (y, D[y])); + + } + + else if(fabs(D[y] - D[x] - z) < 0.0000000001) + + sol[y] = (sol[y] + sol[x]) % 104659; + + } + + } + + F[x] = 1; + + } + +} + +void write() + +{ + + for(int i = 2; i <= V; ++ i) + + printf("%d ", sol[i]); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/distmin/bellman/0fb4ea7c-8732-42e9-ab73-eb9bc02b60f0.cpp b/distmin/bellman/0fb4ea7c-8732-42e9-ab73-eb9bc02b60f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f83dd7c3720e806ac1376bcde4fc9fd25122c44 --- /dev/null +++ b/distmin/bellman/0fb4ea7c-8732-42e9-ab73-eb9bc02b60f0.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + nrd[1]=1; + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} + + + +void bellmanFord() + +{ + + int x, vecin; + + double cost; + + int i; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + for (i=0;idmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + +int main() + +{ + + citire(); + + bellmanFord(); + + for(i=2;i<=n;++i) g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int ANS[1505],viz[1505]; + +double dist[1505]; + +int n,m,x,y,c; + +vector > G[1505]; + +queue q; + + + +void bf(){ + + for(int i = 1; i <= n; ++i) + + dist[i] = 10000000.0; + + + + dist[1] = 0; + + ANS[1] = 1; + + q.push(1); + + viz[1] = 1; + + while(!q.empty()){ + + int nod = q.front(); + + q.pop(); + + for(int i = 0; i < G[nod].size(); ++i){ + + if(abs(dist[G[nod][i].first] - dist[nod] - G[nod][i].second) <= 0.0000001){ + + ANS[G[nod][i].first] = (ANS[G[nod][i].first] + ANS[nod] % 104659) % 104659; + + } + + if(dist[G[nod][i].first] - dist[nod] - G[nod][i].second > 0.0000001){ + + dist[G[nod][i].first] = dist[nod] + G[nod][i].second; + + ANS[G[nod][i].first] = ANS[nod]; + + if(viz[G[nod][i].first] == 0){ + + q.push(G[nod][i].first); + + viz[G[nod][i].first] = 1; + + } + + } + + } + + } + + ofstream g("dmin.out"); + + for(int i = 2; i <= n; ++i){ + + g << ANS[i] % 104659 <<' '; + + } + + g.close(); + +} + +int main() + +{ + + ifstream f("dmin.in"); + + + + + + f >> n >> m; + + for(int i = 1; i <= m; ++i){ + + f >> x >> y >> c; + + double w = log(c); + + G[x].push_back(make_pair(y,w)); + + G[y].push_back(make_pair(x,w)); + + } + + + + bf(); + + + + f.close(); + + + + return 0; + +} diff --git a/distmin/bellman/10d57700-02b7-11ec-a719-2cf05d84bfc2.cpp b/distmin/bellman/10d57700-02b7-11ec-a719-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c1a9f04260f9289ba795a3fc7e480c58914a5e5 --- /dev/null +++ b/distmin/bellman/10d57700-02b7-11ec-a719-2cf05d84bfc2.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +const int nmax = 1510; + + + +struct el{ + +int a; + +double cost; + + + + el(int aa,double c){ + + a = aa; + + cost = c; + + } + +}; + +vectorG[nmax]; + +queueq; + +int N,M,nr[nmax]; + +double dp[nmax]; + +bool ok[nmax]; + + + +inline void BFS(){ + + int nod; + + + + for(int i = 2; i <= N; ++i)dp[i] = 0x3f3f3f3f; + + + + nr[1] = 1; + + q.push(1); + + ok[1] = true; + + + + while(!q.empty()){ + + nod = q.front(); + + ok[nod] = false; + + q.pop(); + + + + for(auto it: G[nod]){ + + if(dp[it.a]-it.cost-dp[nod] >= 1e-10){ + + + + dp[it.a] = dp[nod] + it.cost; + + nr[it.a] = nr[nod]; + + if(!ok[it.a]){ + + ok[it.a] = true; + + q.push(it.a); + + } + + } + + else if(abs(dp[nod]-dp[it.a]+it.cost) < 1e-10){ + + nr[it.a]+=nr[nod]; + + if(nr[it.a]>=104659)nr[it.a]-=104659; + + } + + } + + } + + + +} + +int main(){ + + int i,x,y,c; + + freopen ("dmin.in","r",stdin); + + freopen ("dmin.out","w",stdout); + + scanf("%d %d\n",&N,&M); + + for(i = 1; i <= M; ++i){ + + scanf("%d %d %d\n",&x,&y,&c); + + G[x].push_back(el(y,log10(c))); + + G[y].push_back(el(x,log10(c))); + + } + + BFS(); + + + + for(i = 2; i <= N; ++i) + + cout << nr[i] <<' '; + + + + return 0; + +} diff --git a/distmin/bellman/110c617a-cdef-4a0d-8a41-1367ad586dba.cpp b/distmin/bellman/110c617a-cdef-4a0d-8a41-1367ad586dba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7db01b52c4cd9d2b35e4676b14259265118dc6a1 --- /dev/null +++ b/distmin/bellman/110c617a-cdef-4a0d-8a41-1367ad586dba.cpp @@ -0,0 +1,116 @@ +# include +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +# include +# include +# include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +struct nod{ + + int vecin; + + double cost; + +}; + +vector Lista[1510]; + +queue q; + +nod a; + +bool Marcat[1510]; + +int var[1510],sol[1510],n,m,i,x,y,c; + +double d[1510]; + +bool egal(double a,double b){ + + if(a-b<=1e-7&&a-b>=-1e-7) + + return 1; + + return 0; + +} + +void bmfrd(){ + + Marcat[1]=1; + + var[1]=1; + + q.push(1); + + d[1]=1; + + while(!q.empty()){ + + int nc=q.front(); + + q.pop(); + + for(int i=0;i>n>>m; + + for(i=2;i<=n;i++) + + d[i]=1000000000; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>c; + + a.vecin=y; + + a.cost=log(c); + + Lista[x].push_back(a); + + a.vecin=x; + + Lista[y].push_back(a); + + } + + bmfrd(); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include +#include +using namespace std; +ifstream fin ( "dmin.in" ); + +ofstream fout ( "dmin.out" ); + + + + + +int N, M, Aport[1502]; + +double Dist[1502]; + +vector < pair < int, double > > G[1502]; + +bitset < 1502 > inQ; + +queue < int > Q; + + + +void BellmanFord(){ + + + + for ( int i = 2; i <= N; ++i ) + + Dist[i] = 0x3f3f3f3f; + + + + Aport[1] = 1; + + Q.push ( 1 ); + + inQ[1] = 1; + + + + vector < pair < int, double > > :: iterator it; + + while ( !Q.empty() ){ + + int nod = Q.front(); + + Q.pop(); + + inQ[nod] = 0; + + + + for ( it = G[nod].begin(); it != G[nod].end(); ++it ){ + + if ( Dist[it->first] - (Dist[nod] + it->second) >= 1e-10 ){ + + Dist[it->first] = Dist[nod] + it->second; + + + + Aport[it->first] = Aport[nod]; + + + + if ( !inQ[it->first] ){ + + Q.push ( it->first ); + + inQ[it->first] = 1; + + } + + } + + else if ( abs ( (Dist[nod] + it->second) - Dist[it->first] ) < 1e-10 ){ + + Aport[it->first] += Aport[nod]; + + Aport[it->first] %= 104659; + + } + + } + + } + +} + + + +int main(){ + + + + int x, y, c; + + + + fin >> N >> M; + + + + while ( M-- ){ + + fin >> x >> y >> c; + + G[x].push_back ( make_pair ( y, log10(c) ) ); + + G[y].push_back ( make_pair ( x, log10(c) ) ); + + } + + + + BellmanFord(); + + + + for ( int i = 2; i <= N; ++i ) + + fout << Aport[i] << " "; + + + + return 0; + +} diff --git a/distmin/bellman/13613a4e-02b7-11ec-8c76-2cf05d84bfc2.cpp b/distmin/bellman/13613a4e-02b7-11ec-8c76-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7db01b52c4cd9d2b35e4676b14259265118dc6a1 --- /dev/null +++ b/distmin/bellman/13613a4e-02b7-11ec-8c76-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +# include +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +vector > HEAP; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost, cact; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + drumuri[1]=1; + + + + HEAP.push_back(make_pair(0, 1)); ap[1]=1; + + push_heap(HEAP.begin(), HEAP.end()); + + + + while (HEAP.size()) { + + cact=-HEAP[0].first; + + k=HEAP[0].second; + + + + pop_heap(HEAP.begin(), HEAP.end()); + + HEAP.pop_back(); + + + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include +#include +#include +#include + + + + +using namespace std; + +const double eps=1e-10; + +const double INFINIT=9999999999; + +vector< pair > Graf[1505]; + +queue Q; + +int N,M,x,y,z,D[1505]; + +bool InCoada[1505]; + +double Dmin[1505]; + + + +void Read() + +{ + + ifstream g("dmin.in"); + + g>>N>>M; + + for(int i=1;i<=M;i++) + + { + + g>>x>>y>>z; + + double z1=log(z); + + Graf[x].push_back(make_pair(y,z1)); + + Graf[y].push_back(make_pair(x,z1)); + + } + +} + + + +void BellManFord() + +{ + + for(int i=1;i<=N;i++) + + Dmin[i]=INFINIT; + + + + Q.push(1); + + D[1]=1;Dmin[1]=0.0;InCoada[1]=true; + + while(!Q.empty()) + + { + + int nod=Q.front(); + + Q.pop(); + + InCoada[nod]=false; + + for(vector >::iterator it=Graf[nod].begin();it!=Graf[nod].end();it++) + + { + + int vecin=it->first; + + double cost=it->second; + + if(Dmin[nod]+cost-Dmin[vecin]< -eps) + + { + + D[vecin]=D[nod]; + + Dmin[vecin]=Dmin[nod]+cost; + + if(!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + else + + { + + if(fabs(Dmin[nod]+cost-Dmin[vecin])<=eps) + + { + + D[vecin]=(D[vecin]+D[nod])%104659; + + if (!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + } + + } + + } + +} + + + +void Write() + +{ + + ofstream f("dmin.out"); + + for(int i=2;i<=N;i++) + + f< +#include +#include +#include +#include + + +using namespace std; + + + +fstream f("dmin.in", ios::in); + +fstream g("dmin.out", ios::out); + + + +const int nmax = 1505; + +const double INF = 100000.0; + +const double eps = 0.00001; + +const int modulo = 104659; + + + +vector < pair > a[nmax]; + + + +double c, cost; + +double d[nmax]; + +int cnt[nmax], viz[nmax]; + +int n, m, x, y, i; + + + +void read() + +{ + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + cost = log(c); + + a[x].push_back(make_pair(y, cost)); + + a[y].push_back(make_pair(x, cost)); + + } + +} + + + +void bellman() + +{ + + queue q; + + double dist; + + int nc; + + for (i = 2; i <= n; i++) d[i] = INF; + + q.push(1); + + d[1] = 0; + + viz[1] = 1; + + cnt[1] = 1; + + while (!q.empty()) + + { + + nc = q.front(); q.pop(); + + dist = d[nc]; + + viz[nc] = 0; + + for (vector< pair >::iterator it = a[nc].begin(); it != a[nc].end(); ++it) + + { + + if (fabs(dist + it->second - d[it->first]) < eps) + + { + + cnt[it->first] = (cnt[it->first] + cnt[nc]) % modulo; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + else if (dist + it->second < d[it->first]) + + { + + d[it->first] = dist + it->second; + + cnt[it->first] = cnt[nc]; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + } + + } + +} + + + +int main() + +{ + + read(); + + bellman(); + + for (i = 2; i <= n; i++) g << cnt[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/152812be-02b7-11ec-9e1d-2cf05d84bfc2.cpp b/distmin/bellman/152812be-02b7-11ec-9e1d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba1b0ad2e9ef3a00a2d6ef96135e9d5e1c8bfe86 --- /dev/null +++ b/distmin/bellman/152812be-02b7-11ec-9e1d-2cf05d84bfc2.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +int dant[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + dant[vecin] = 0; + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod] - dant[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + dant[nod] = d[nod]; + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/15467c63-02b7-11ec-9811-2cf05d84bfc2.cpp b/distmin/bellman/15467c63-02b7-11ec-9811-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/15467c63-02b7-11ec-9811-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/15a91e06-02b7-11ec-aa4f-2cf05d84bfc2.cpp b/distmin/bellman/15a91e06-02b7-11ec-aa4f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7608b0b91d0f4b84589a887850862c8e01977eb8 --- /dev/null +++ b/distmin/bellman/15a91e06-02b7-11ec-aa4f-2cf05d84bfc2.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505], sol[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now] - dpant[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now] - dpant[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + dpant[now] = dp[now]; + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/15c40da9-02b7-11ec-b54f-2cf05d84bfc2.cpp b/distmin/bellman/15c40da9-02b7-11ec-b54f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..394baa7f5e4ebf12e4081f63548c62e6a75b17ab --- /dev/null +++ b/distmin/bellman/15c40da9-02b7-11ec-b54f-2cf05d84bfc2.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/1649bbf4-02b7-11ec-acc1-2cf05d84bfc2.cpp b/distmin/bellman/1649bbf4-02b7-11ec-acc1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/1649bbf4-02b7-11ec-acc1-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/167a5b37-02b7-11ec-84cb-2cf05d84bfc2.cpp b/distmin/bellman/167a5b37-02b7-11ec-84cb-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ac97da99bf6e5279a9e8f34f90a1b5bb0510175 --- /dev/null +++ b/distmin/bellman/167a5b37-02b7-11ec-84cb-2cf05d84bfc2.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +const string problemName = "dmin"; +const string inputFile = problemName + ".in"; +const string outputFile = problemName + ".out"; + + +typedef long long int lld; +typedef pair PII; +typedef pair PIL; +typedef pair PLI; +typedef pair PLL; +typedef pair PID; +typedef pair PDI; + +const int INF = (1LL << 30) - 1; +const lld LINF = (1LL << 62) - 1; +const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; +const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; +const int NMAX = 1500 + 5; +const int MOD = 104659; +const double EPS = 1e-10; + +int comp(double a, double b) { + if(fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 0; + if((a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 1; + return -1; +} + +int N, M; +vector V[NMAX]; +int sol[NMAX]; +double D[NMAX]; +bitset viz; +deque Q; + +void bf(int x) { + int i, y; + double z; + + for(i = 2; i <= N; i++) + D[i] = INF; + + sol[x] = 1; + Q.push_back(x); + viz[x] = 1; + + while(!Q.empty()) { + x = Q.front(); + Q.pop_front(); + + viz[x] = 0; + + for(auto nod : V[x]) { + y = nod.first; + z = nod.second; + + if(comp(D[x] + z, D[y]) < 0) { + D[y] = D[x] + z; + sol[y] = 0; + + if(!viz[y]) { + Q.push_back(y); + viz[y] = 1; + } + } + + if(comp(D[x] + z, D[y]) == 0) + sol[y] = (sol[y] + sol[x]) % MOD; + } + } +} + +int main() { + int i, x, y, z; + + + freopen(inputFile.c_str(), "r", stdin); + freopen(outputFile.c_str(), "w", stdout); + + + scanf("%d%d", &N, &M); + + while(M--) { + scanf("%d%d%d", &x, &y, &z); + V[x].push_back(make_pair(y, log2(z))); + V[y].push_back(make_pair(x, log2(z))); + } + + bf(1); + + for(i = 2; i <= N; i++) + printf("%d ", sol[i]); + + return 0; +} diff --git a/distmin/bellman/1703abdb-02b7-11ec-bdc0-2cf05d84bfc2.cpp b/distmin/bellman/1703abdb-02b7-11ec-bdc0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a4f6cf77e27a562f1a6c51f4e0f5fe6f4fe21d0 --- /dev/null +++ b/distmin/bellman/1703abdb-02b7-11ec-bdc0-2cf05d84bfc2.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include + + +using namespace std; + +typedef int64_t var; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +struct Edge { + + var n, c; + + Edge(var a, var b) { + + n = a; + + c = b; + + } + +}; + + + + + +vector G[1505]; + +var n; + +double D[1505]; + +var D_R[1505]; + +var NR[1505]; + +bool INQ[1505]; +void bellman() { + + for(var i=1; i<=n; i++) { + + D[i] = (1LL << 55); + + } + + D[1] = 0; + + NR[1] = 1; + + queue Q; + + Q.push(1); + + while(!Q.empty()) { + + var node = Q.front(); + + Q.pop(); + + INQ[node] = 0; + + for(auto e : G[node]) { + + if((D[e.n] > D[node] + log10(e.c) + 1e-12)) { + + D[e.n] = D[node] + log10(e.c); + + D_R[e.n] = (D_R[node] * e.c) % 10000; + + NR[e.n] = 0; + + if(!INQ[e.n]) { + + Q.push(e.n); + + INQ[e.n] = 1; + + } + + } + + if(abs(D[e.n] - (D[node] + log10(e.c))) < 1e-12 && D_R[e.n] == (D_R[node] * e.c) % 10000 ) { + + NR[e.n] += NR[node]; + + NR[e.n] %= 104659; + + } + + } + + } + +} + + + +int main() { + + var a, b, m; + + var c; + + fin>>n>>m; + + while(m--) { + + fin>>a>>b>>c; + + G[a].push_back(Edge(b, c)); + + G[b].push_back(Edge(a, c)); + + } + + bellman(); + + for(var i=2; i<=n; i++) { + + fout< +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct comp + +{ + + bool operator()(pair,long double> i1 ,pair,long double> i2 ) + + { + + return i1.second,long double>,comp> Heap; + + + +vector > H[1666]; + +int n,m,i,xx,yy,j,cost; + +int pos[1666]; + +long double dp[1666]; + +bool viz[1666]; + + + +long double mdl(long double value) + +{ + + if (value<0) + + return -value; + + else + + return value; + +} + + + +bool equal(long double AA,long double BB) + +{ + + if (mdl(AA-BB)<=0.0000000001) + + return true; + + + + return false; + +} + + + +int main() + +{ + + f>>n>>m; + + + + for(i=1; i<=m; ++i) + + { + + f>>xx>>yy>>cost; + + long double cost2=log10((long double)cost); + + H[xx].push_back(make_pair(yy,cost2)); + + H[yy].push_back(make_pair(xx,cost2)); + + } + + + + pos[1]=1; + + Heap.insert(make_pair(make_pair(1,1),0)); + + + + while (Heap.empty()==false) + + { + + pair,long double> next=*(Heap.begin()); + + Heap.erase(Heap.begin()); + + + + if(next.first.second!=1) + + if (equal(dp[(next.first).second],next.second)==true||viz[next.first.second]==false) + + { + + pos[(next.first).second]+=pos[(next.first).first]; + + pos[(next.first).second]%=104659; + + } + + if (viz[next.first.second]==true) + + continue; + + + + viz[next.first.second]=true; + + dp[next.first.second]=next.second; + + + + int N=H[next.first.second].size(); + + + + for( i=0; i +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/196a86f8-de58-4508-8bd0-ccbd0908221d.cpp b/distmin/bellman/196a86f8-de58-4508-8bd0-ccbd0908221d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b0d6a74f8c23c30d1d706386a7fc7b89eb7fb90 --- /dev/null +++ b/distmin/bellman/196a86f8-de58-4508-8bd0-ccbd0908221d.cpp @@ -0,0 +1,209 @@ +#include +#include +using namespace std; + +const int N = 1505, M = 2505, INF = 100000, R = 104659; + +int vf[2 * M], urm[2 * M], lst[N], h[N], poz[N], nrh, nrm, nrd[N]; + +double c[2 * M], v[N]; + +void adauga (int x, int y, double cost) + +{ + + vf[++nrm] = y; + + c[nrm] = cost; + + urm[nrm] = lst[x]; + + lst[x] = nrm; + +} + +void schimba (int p1, int p2) + +{ + + int aux = h[p1]; + + h[p1] = h[p2]; + + h[p2] = aux; + + poz[h[p1]] = p1; + + poz[h[p2]] = p2; + +} + +void urca (int p) + +{ + + while (p > 1 && v[h[p]] < v[h[p>>1]]) + + { + + schimba (p, p >> 1); + + p >>= 1; + + } + +} + +void coboara (int p) + +{ + + int bun = p, fs = 2 * p, fd = 2 * p + 1; + + if (fs <= nrh && v[h[fs]] < v[h[bun]]) + + bun = fs; + + if (fd <= nrh && v[h[fd]] < v[h[bun]]) + + bun = fd; + + if (bun != p) + + { + + schimba (bun, p); + + coboara (bun); + + } + +} + +double make_abs (double x) + +{ + + if (x < 0) + + return -x; + + return x; + +} + +int main () + +{ + + FILE *in, *out; + + in = fopen ("dmin.in", "r"); + + out = fopen ("dmin.out", "w"); + + int n, m; + + fscanf (in, "%d%d", &n, &m); + + int i, x, y; + + double cost; + + for (i = 1; i <= m; i++) + + { + + fscanf (in, "%d%d%lf", &x, &y, &cost); + + cost = log(cost); + + adauga (x, y, cost); + + adauga (y, x, cost); + + } + + for (i = 1; i <= n; i++) + + { + + v[i] = INF; + + poz[i] = i; + + h[i] = i; + + } + + nrh = n; + + double eps = 0.00000001; + + v[1] = 0; + + nrd[1] = 1; + + int p; + + double aux; + + while (nrh > 0) + + { + + x = h[1]; + + schimba (1, nrh--); + + coboara (1); + + p = lst[x]; + + while (p != 0) + + { + + y = vf[p]; + + cost = c[p]; + + aux = cost + v[x]; + + if (v[y] - aux > eps) + + { + + v[y] = cost + v[x]; + + nrd[y] = nrd[x]; + + urca (poz[y]); + + } + + else + + if (make_abs (aux - v[y]) <= eps) + + { + + nrd[y] += nrd[x]; + + nrd[y] %= R; + + } + + p = urm[p]; + + } + + } + + for (i = 2; i <= n; i++) + + fprintf (out, "%d ", nrd[i]); + + return 0; + +} diff --git a/distmin/bellman/197cdf15-02b7-11ec-8073-2cf05d84bfc2.cpp b/distmin/bellman/197cdf15-02b7-11ec-8073-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1f8437db5e6e44af045865f9a2278f1477769ce --- /dev/null +++ b/distmin/bellman/197cdf15-02b7-11ec-8073-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +vector > G[1605]; + +vector daddy[1605]; + +queue Q; + +bitset<1605> inQ; + + + +int N,M; + +double dist[1605]; + +int DP[1605]; + + + +void read() + +{ + + int a,b,c; + + scanf("%d%d",&N,&M); + + for(int i = 1; i <= M; ++i){ + + scanf("%d%d%d",&a,&b,&c); + + G[a].push_back(make_pair(log2((double)c),b)); + + G[b].push_back(make_pair(log2((double)c),a)); + + } + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + for(int i = 2; i <= N; ++i) dist[i] = 0x3f3f3f3f; + + dist[1] = 0; + + int k; + + while(!Q.empty()) + + { + + k = Q.front();Q.pop();inQ[k] = 0; + + for(vector >::iterator it = G[k].begin(); it != G[k].end(); ++it) + + { + + if(dist[it->second] > dist[k] + it->first + 0.00000001) + + { + + dist[it->second] = dist[k] + it->first; + + daddy[it->second].clear(); + + daddy[it->second].push_back(k); + + inQ[it->second] = 1; + + Q.push(it->second); + + continue; + + } + + if(dist[it->second] - dist[k] - it->first + 0.00000001 >= 0 && + + dist[it->second] - dist[k] - it->first <= 0.0001 ) + + daddy[it->second].push_back(k); + + } + + } + +} + + + +int dynamic(int k) + +{ + + if(DP[k])return DP[k]; + + int sum = 0; + + for(vector::iterator it = daddy[k].begin(); it != daddy[k].end(); ++it) + + sum = (sum%104659 + dynamic(*it)%104659) % 104659; + + DP[k] = sum; + + return DP[k] % 104659; + +} + +void prepare() + +{ + + for(int i = 1; i <= N; ++i) + + { + + vector::iterator it = unique(daddy[i].begin(),daddy[i].end()); + + daddy[i].resize(distance(daddy[i].begin(),it)); + + } + +} + + + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + read(); + + bellman_ford(); + + DP[1] = 1; + + prepare(); + + for(int i = 2; i <= N; ++i) + + printf("%d ",dynamic(i)); + + + + return 0; + +} diff --git a/distmin/bellman/1993c773-02b7-11ec-a1e7-2cf05d84bfc2.cpp b/distmin/bellman/1993c773-02b7-11ec-a1e7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2300223720c8fe457bba515ab208788bad8fbfff --- /dev/null +++ b/distmin/bellman/1993c773-02b7-11ec-a1e7-2cf05d84bfc2.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510], Q; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +bool Sortare(int a, int b) + +{ + + return dist[a] > dist[b]; + +} + + + +void Bellman_Ford() + +{ + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push_back(1); + + while(Q.size()) + + { + + x=Q[0]; + + pop_heap(Q.begin(), Q.end(), Sortare); + + Q.pop_back(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include +#include +using namespace std; +ifstream fin ( "dmin.in" ); + +ofstream fout ( "dmin.out" ); + + + + + +int N, M, Aport[1502]; + +double Dist[1502]; + +vector < pair < int, double > > G[1502]; + +bitset < 1502 > inQ; + +queue < int > Q; + + + +void BellmanFord(){ + + + + for ( int i = 2; i <= N; ++i ) + + Dist[i] = 0x3f3f3f3f; + + + + Aport[1] = 1; + + Q.push ( 1 ); + + inQ[1] = 1; + + + + vector < pair < int, double > > :: iterator it; + + while ( !Q.empty() ){ + + int nod = Q.front(); + + Q.pop(); + + inQ[nod] = 0; + + + + for ( it = G[nod].begin(); it != G[nod].end(); ++it ){ + + if ( Dist[it->first] - (Dist[nod] + it->second) >= 1e-10 ){ + + Dist[it->first] = Dist[nod] + it->second; + + + + Aport[it->first] = Aport[nod]; + + + + if ( !inQ[it->first] ){ + + Q.push ( it->first ); + + inQ[it->first] = 1; + + } + + } + + else if ( abs ( (Dist[nod] + it->second) - Dist[it->first] ) < 1e-10 ){ + + Aport[it->first] += Aport[nod]; + + Aport[it->first] %= 104659; + + } + + } + + } + +} + + + +int main(){ + + + + int x, y, c; + + + + fin >> N >> M; + + + + while ( M-- ){ + + fin >> x >> y >> c; + + G[x].push_back ( make_pair ( y, log10(c) ) ); + + G[y].push_back ( make_pair ( x, log10(c) ) ); + + } + + + + BellmanFord(); + + + + for ( int i = 2; i <= N; ++i ) + + fout << Aport[i] << " "; + + + + return 0; + +} diff --git a/distmin/bellman/1b8b07e5-02b7-11ec-81cd-2cf05d84bfc2.cpp b/distmin/bellman/1b8b07e5-02b7-11ec-81cd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17b5445c240083bc400c9199e28ac3139d1cf3dc --- /dev/null +++ b/distmin/bellman/1b8b07e5-02b7-11ec-81cd-2cf05d84bfc2.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 1502; + +const int MOD = 104659; + +const double EPS = 0.000001; + + + +typedef int Heap[MAX_N]; + + + +int N, M, Nr; + +int pos[MAX_N], S[MAX_N]; + +vector < pair < int, double > > v[MAX_N]; + +double D[MAX_N]; + +Heap H; + + + +inline int double_cmp(double a, double b) { + + if(a - b < -EPS) + + return -1; + + if(a - b > EPS) + + return 1; + + return 0; + +} + + + +inline void percolate(Heap H, int N, int K) { + + int temp = H[K]; + + while(K > 1 && double_cmp(D[temp], D[H[K/2]]) < 0) + + H[K] = H[K/2], pos[H[K]] = K, K /= 2; + + H[K] = temp, pos[temp] = K; + +} + + + +inline void sift(Heap H, int N, int K) { + + int son; + + do { + + son = 0; + + if(K*2 <= N) { + + son = K*2; + + if(K*2+1 <= N && double_cmp(D[H[K*2+1]], D[H[son]]) < 0) + + son = K*2+1; + + } + + if(double_cmp(D[H[son]], D[H[K]]) >= 0) + + son = 0; + + if(son) { + + int temp = H[K]; + + H[K] = H[son], H[son] = temp; + + pos[H[K]] = K, pos[H[son]] = son; + + K = son; + + } + + } while(son); + +} + + + +int main() { + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + + + f >> N >> M; + + for(int i = 1, x, y; i <= M; ++i) { + + double c; + + f >> x >> y >> c; + + c = log10(c); + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + + + H[++Nr] = 1, pos[1] = S[1] = 1; + + for(int i = 2; i <= N; ++i) + + D[i] = 9999999999; + + while(Nr) { + + int x = H[1]; + + H[1] = H[Nr--]; + + if(Nr) + + pos[H[1]] = 1, sift(H, Nr, 1); + + for(size_t i = 0; i < v[x].size(); ++i) { + + int y = v[x][i].first; + + double c = v[x][i].second; + + int t = double_cmp(D[x]+c, D[y]); + + if(t < 0) { + + D[y] = D[x] + c, S[y] = S[x]; + + if(pos[y]) + + percolate(H, Nr, pos[y]); + + else H[++Nr] = y, pos[y] = Nr, percolate(H, Nr, Nr); + + } + + else if(!t) + + S[y] += S[x], S[y] %= MOD; + + + + } + + } + + + + for(int i = 2; i <= N; ++i) + + g << S[i] << ' '; + + g << '\n'; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/distmin/bellman/1b9c6c5a-0fa5-4061-8083-cf7cdd497946.cpp b/distmin/bellman/1b9c6c5a-0fa5-4061-8083-cf7cdd497946.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/1b9c6c5a-0fa5-4061-8083-cf7cdd497946.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/1ba00154-02b7-11ec-909a-2cf05d84bfc2.cpp b/distmin/bellman/1ba00154-02b7-11ec-909a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d043fb5cad79eaf4ca43f04f8a75f0514aeddbd2 --- /dev/null +++ b/distmin/bellman/1ba00154-02b7-11ec-909a-2cf05d84bfc2.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("dmin.in"); +ofstream fout("dmin.out"); + +int n, m; +typedef pair nod; +vector d(1505, 1 << 30); +vector a[1505]; +vector fr(1505, 0); +vector viz(1505); +queue q; + +int main() +{ + fin>>n>>m; + while(m--) + { + int x, second; + double first, c2; + fin>>x>>second>>first; + c2 = log10(first); + a[x].push_back(nod(c2, second)); + a[second].push_back(nod(c2, x)); + } + + q.push(1); + d[1] = 0; + fr[1] = 1; + viz[1] = 1; + while(!q.empty()) + { + int x = q.front(); + viz[x] = 0; + q.pop(); + for(unsigned i=0; i +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +vector > HEAP; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost, cact; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + drumuri[1]=1; + + + + HEAP.push_back(make_pair(0, 1)); ap[1]=1; + + push_heap(HEAP.begin(), HEAP.end()); + + + + while (HEAP.size()) { + + cact=-HEAP[0].first; + + k=HEAP[0].second; + + + + pop_heap(HEAP.begin(), HEAP.end()); + + HEAP.pop_back(); + + + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("dmin.in"); + +ofstream cout("dmin.out"); + + + +const int MAXN = 1505, + + oo = ((1<<31)-1), + + MOD = 104659; + + + +vector< pair > Graph[MAXN]; + +vector< double > Distance(MAXN, (double)oo); + +bitset< MAXN > Used; + +queue< int > Q; + +map NRoads; + +int N, M; + + + +const inline double Absolute(double a) + +{ + + if(a > 0) + + return a; + + return -a; + +} + +const inline bool isequal(double a, double b) + +{ + + if( Absolute(a-b) <= 1e-10 ) + + return true; + + return false; + +} + + + +void Read_Data() + +{ + + cin >> N >> M; + + for(int i = 1 ; i <= M ; ++ i) + + { + + int X, Y, Z; + + cin >> X >> Y >> Z; + + Graph[X].push_back(make_pair(Y, log(Z))); + + } + + cin.close(); + +} + + + +void BellmanFord() + +{ + + for(Q.push(1) , Distance[1] = 0, NRoads[1] = 1, Used[1] = true; !Q.empty(); Q.pop() ) + + { + + int node = Q.front(); + + Used[node] = false; + + for(vector > :: iterator it = Graph[node].begin(), fin = Graph[node].end(); it != fin ; ++ it) + + {if(isequal(Distance[node] + it->second, Distance[it->first])) + + { + + NRoads[it->first] += NRoads[node]; + + if(NRoads[it->first] > MOD) + + NRoads[it->first] -= MOD; + + } + + else if(Distance[it->first] > it->second + Distance[node]) { + + Distance[it->first] = it->second + Distance[node]; + + NRoads[it->first] = NRoads[node]; + + if( !Used[it->first] ) + + { + + Used[it->first] = true; + + Q.push(it->first); + + } + + }} + + } + + + +} + +void Write_Data() + +{ + + for(int i = 2 ; i <= N; ++ i) + + cout << NRoads[i] << " "; + + cout << "\n"; + + cout.close(); + +} + +int main() + +{ + + Read_Data(); + + BellmanFord(); + + Write_Data(); + + return 0; + +} diff --git a/distmin/bellman/1c1d8bb6-02b7-11ec-a364-2cf05d84bfc2.cpp b/distmin/bellman/1c1d8bb6-02b7-11ec-a364-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dca6a617e3c37c8370e20a378cf2710c978b5337 --- /dev/null +++ b/distmin/bellman/1c1d8bb6-02b7-11ec-a364-2cf05d84bfc2.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct comp + +{ + + bool operator()(pair,long double> i1 ,pair,long double> i2 ) + + { + + return i1.second,long double>,comp> Heap; + + + +vector > H[1666]; + +int n,m,i,xx,yy,j,cost; + +int pos[1666]; + +long double dp[1666]; + +bool viz[1666]; + + + +long double mdl(long double value) + +{ + + if (value<0) + + return -value; + + else + + return value; + +} + + + +bool equal(long double AA,long double BB) + +{ + + if (mdl(AA-BB)<=0.0000000001) + + return true; + + + + return false; + +} + + + +int main() + +{ + + f>>n>>m; + + + + for(i=1; i<=m; ++i) + + { + + f>>xx>>yy>>cost; + + long double cost2=log10((long double)cost); + + H[xx].push_back(make_pair(yy,cost2)); + + H[yy].push_back(make_pair(xx,cost2)); + + } + + + + pos[1]=1; + + Heap.insert(make_pair(make_pair(1,1),0)); + + + + while (Heap.empty()==false) + + { + + pair,long double> next=*(Heap.begin()); + + Heap.erase(Heap.begin()); + + + + if(next.first.second!=1) + + if (equal(dp[(next.first).second],next.second)==true||viz[next.first.second]==false) + + { + + pos[(next.first).second]+=pos[(next.first).first]; + + pos[(next.first).second]%=104659; + + } + + if (viz[next.first.second]==true) + + continue; + + + + viz[next.first.second]=true; + + dp[next.first.second]=next.second; + + + + int N=H[next.first.second].size(); + + + + for( i=0; i +#include +#include +#include +#include + + + + + + + +using namespace std; + +ifstream first("dmin.in"); +ofstream g("dmin.out"); + +int n, m, nr[1510], vz[1510]; +double d[1510]; + +struct muchie +{ + int nod; + double cost; +}; + +vector a[1510]; + +set< pair > second; + +void Citeste() +{ + int i, x, y; + muchie r; + + first>>n>>m; + + for (i=1; i<=m; ++i) + { + first>>x>>y>>r.cost; + + r.nod=y; a[x].push_back(r); + r.nod=x; a[y].push_back(r); + } +} + +void Initializeaza() +{ + int i; + + for (i=1; i<=n; ++i) d[i]=2500000000000.0; + + d[1]=0.0; nr[1]=1; + + second.insert( make_pair(0.0, 1) ); +} + +void Solve() +{ + int i, gata; + double val; + muchie r; + pair pr; + set > :: iterator is; + + do + { + gata=0; + + while (!second.empty() && !gata) + { + is=second.begin(); + pr=*is; + if (!vz[pr.second]) gata=1; + second.erase(is); + } + + if (gata) + { + vz[pr.second]=1; + for (i=0; i0.0000000001) + { + d[r.nod]=val; + second.insert(make_pair(val, r.nod)); + nr[r.nod]=nr[pr.second]; + } + else + if (fabs(d[r.nod]-val)<0.0000000001) nr[r.nod]=(nr[r.nod]+nr[pr.second])%104659; + } + } + } + + }while (gata); + + for (i=2; i<=n; ++i) g< +#include +#include +#include +#include +#include +#include +using namespace std; + +ofstream out("dmin.out"); +vector > G[1510]; + +typedef vector > ::iterator IT; + +queueQ; + +bitset<1510>uz; + + + +int drum[1510],n,m; + +double d[1510],EPS=0.0000000001,c; + + + +void read(); + +void BMF(); + +void write(); + + + +int main() + +{ + + read(); + + BMF(); + + write(); + + return 0; + +} + + + +void read() + +{ + + ifstream in("dmin.in"); + + in>>n>>m; + + for(int x,y,c ; m ; --m) + + { + + in>>x>>y>>c; + + G[x].push_back(make_pair(y,log( (double) c) ) ); + + G[y].push_back(make_pair(x,log( (double) c) ) ); + + } + +} + + + +void BMF() + +{ + + for(int i=1;i<=n;i++) + + d[i]=0x3f3f3f3f; + + d[1]=0; + + drum[1]=1; + + Q.push(1); + + uz[1]=1; + + int k; + + for(; Q.size(); Q.pop()) + + { + + k=Q.front(); + + for(IT I=G[k].begin();I!=G[k].end();++I) + + { + + int val=I->first; + + double cost=I->second; + + if( fabs( d[val] - d[k] - cost ) < EPS ) + + drum[val]= (drum[val] + drum[k] ) %104659; + + else + + if( d[val] - d[k] - cost > EPS) + + { + + if(!uz[val]) + + { + + Q.push(val); + + uz[val]=1; + + } + + d[val]=d[k] + cost; + + drum[val]= drum[k] ; + + } + + } + + uz[k]=0; + + } + +} + + + +void write() + +{ + + for(int i=2;i<=n;i++) + + out< +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510]; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +void Bellman_Ford() + +{ + + queue Q; + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push(1); + + while(!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +const int MaxN = 1501; + +const double eps = 1e-10; + +const int Mod = 104659; + +const double InF = 100000000; + + + +const char InFile[] = "dmin.in"; + +const char OutFile[] = "dmin.out"; + + + +int N,M,sol[MaxN]; + +double d[MaxN]; + +bool inQ[MaxN]; + +vector< pair > G[MaxN]; + +queue Q; + + + +double modul( double x ) + +{ + + return x > 0 ? x : -x; + +} + + + +void BellMan_Ford() + +{ + + int nod; + + vector< pair >::iterator it,iend; + + for( nod = 1 ; nod <= N ; ++nod ) + + { + + d[nod] = InF; + + sol[nod] = 0; + + inQ[nod] = false; + + } + + d[1] = 0; + + sol[1] = 1; + + Q.push(1); + + inQ[1] = true; + + while( !Q.empty() ) + + { + + nod = Q.front(); + + Q.pop(); + + inQ[nod] = false; + + iend = G[nod].end(); + + it = G[nod].begin(); + + for( ; it != iend ; ++it ) + + { + + if( modul(d[it->first] - d[nod] - it-> second) < eps ) + + { + + sol[it->first] = ( sol[it->first]+sol[nod] ) % Mod; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + if( d[it->first] - d[nod] - it->second > eps ) + + { + + d[it->first] = d[nod] + it->second; + + sol[it->first] = sol[nod]; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + ifstream fin( InFile ); + + ofstream fout( OutFile ); + + fin >> N >> M; + + int i,x,y; + + double z; + + for( i = 0 ; i < M ; ++i ) + + { + + fin >> x >> y >> z; + + G[x].push_back(make_pair(y,log(z))); + + G[y].push_back(make_pair(x,log(z))); + + } + + BellMan_Ford(); + + for( i = 2 ; i <= N ; ++i ) + + fout << sol[i] << ' '; + + fout << '\n'; + + fin.close();fout.close(); + + return 0; + +} diff --git a/distmin/bellman/1fcb34b9-02b7-11ec-aabd-2cf05d84bfc2.cpp b/distmin/bellman/1fcb34b9-02b7-11ec-aabd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3662256add92df9c3714ff8271dff60686e09fe --- /dev/null +++ b/distmin/bellman/1fcb34b9-02b7-11ec-aabd-2cf05d84bfc2.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include + + + + + +using namespace std; + +FILE *f=fopen("dmin.in", "r"); + +FILE *g=fopen("dmin.out", "w"); + + +int N, M, sol[1510]; +double D[1510]; +vector< pair < int, double > > G[1510]; +queue Q; +bool viz[1510]; +void BellmanFord() { + int i, x; + + for(i=1; i<=N; i++) { + viz[i] = false; + D[i] = 99999999; + } + viz[1] = true; + Q.push(1); + D[1] = 0; + + sol[1] = 1; + while(!Q.empty()) { + + + x=Q.front(); + + Q.pop(); + viz[x] = false; + for(vector< pair < int, double > >::iterator it=G[x].begin(); it!=G[x].end(); it++) { + if(0.00000001 < D[(*it).first] -D[x] - (*it).second) { + D[(*it).first] = D[x] + (*it).second; + sol[(*it).first] = sol[x]; + if(!viz[(*it).first]) { + viz[(*it).first] = 1; + Q.push((*it).first); + } + } + else + + if(D[x] + (*it).second - D[(*it).first] <= 0.00000001) { + sol[(*it).first] += sol[x]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + + int i, j, x, y; + + fscanf(f, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f, "%d %d %d\n", &x, &y, &j); + double cost = (double)log(j); + + G[x].push_back( make_pair(y, cost) ); + G[y].push_back( make_pair(x, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(g, "%d ", sol[i]); + } + + fclose(f); + + fclose(g); + return 0; +} diff --git a/distmin/bellman/20499a57-02b7-11ec-8ace-2cf05d84bfc2.cpp b/distmin/bellman/20499a57-02b7-11ec-8ace-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62feb2751cde2532ca3ef70a49ffd96e38059d36 --- /dev/null +++ b/distmin/bellman/20499a57-02b7-11ec-8ace-2cf05d84bfc2.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/20cc0d28-6c30-42d7-839e-486f93983ce5.cpp b/distmin/bellman/20cc0d28-6c30-42d7-839e-486f93983ce5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fb686de9c127704d9e7e2b3d307eaabff7c44b9 --- /dev/null +++ b/distmin/bellman/20cc0d28-6c30-42d7-839e-486f93983ce5.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/2368fbee-02b7-11ec-b9c6-2cf05d84bfc2.cpp b/distmin/bellman/2368fbee-02b7-11ec-b9c6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbad9333d943e72b072de4019d28c9234379467e --- /dev/null +++ b/distmin/bellman/2368fbee-02b7-11ec-b9c6-2cf05d84bfc2.cpp @@ -0,0 +1,209 @@ +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +struct nod + +{ type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + +template + +class vector + +{ type *aux; + + int capacity,m_size; + + public: + + type *act; + + vector() + + { act=new type[1]; capacity=1; m_size=0; } + + void reallocate() + + { aux=new type[2*capacity] , capacity*=2; + + for(int i=0;ia[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(-0.00001 <= dmin[p] + (*it).second - dmin[(*it).first] && dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/23c5af3a-02b7-11ec-b12e-2cf05d84bfc2.cpp b/distmin/bellman/23c5af3a-02b7-11ec-b12e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be90d33072c5dc8958c7f09424d1660e16bb8aa6 --- /dev/null +++ b/distmin/bellman/23c5af3a-02b7-11ec-b12e-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +class nod + +{ public: type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + + + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +void citire(); + +void bellmanFord(); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +int main() + +{ + + citire(); + + bellmanFord(); + + + + for(i=2;i<=n;++i) g<dmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + + + + + nrd[1]=1; + + + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} diff --git a/distmin/bellman/24eb9abe-02b7-11ec-9e06-2cf05d84bfc2.cpp b/distmin/bellman/24eb9abe-02b7-11ec-9e06-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fb686de9c127704d9e7e2b3d307eaabff7c44b9 --- /dev/null +++ b/distmin/bellman/24eb9abe-02b7-11ec-9e06-2cf05d84bfc2.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/252574b1-02b7-11ec-a89c-2cf05d84bfc2.cpp b/distmin/bellman/252574b1-02b7-11ec-a89c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84cc83e8c309ef6c91cebf5f564a468f5d352a49 --- /dev/null +++ b/distmin/bellman/252574b1-02b7-11ec-a89c-2cf05d84bfc2.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +long long n,m,nrd[1505]; + +long double dm[1505]; + +bool t[1505]; + + + +struct muchie + +{ int v; + + long long c; + +}; + + + +vectorg[1505]; + +vector::iterator it; + +queueq; + + + +void bellman() + +{ long long i,crt; + + for(i=1;i<=n;i++) + + dm[i]=1000000000; + + q.push(1); + + t[1]=1; + + dm[1]=0; + + nrd[1]=1; + + while(!q.empty() ) + + { crt=q.front(); + + q.pop(); + + t[crt]=0; + + for(it=g[crt].begin();it < g[crt].end();it++) + + { if(dm[it->v] - (dm[crt] + log(it->c) ) > 1e-10 ) + + { dm[it->v]=dm[crt] + log(it->c); + + nrd[it->v]=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + if(!t[it->v]) + + { q.push(it->v); + + t[it->v]=1; + + } + + } + + else if( (dm[crt] + log(it->c) - dm[it->v]) <= 1e-10) + + { nrd[it->v]+=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + } + + } + + } + + for(i=2;i<=n;i++) + + out<>n>>m; + + for(i=1;i<=m;i++) + + { in>>a>>b>>r; + + w.c=r; + + w.v=b; + + g[a].push_back(w); + + w.v=a; + + g[b].push_back(w); + + } + + in.close(); + + bellman(); + + out.close(); + + return 0; + +} diff --git a/distmin/bellman/265f54ee-02b7-11ec-b64f-2cf05d84bfc2.cpp b/distmin/bellman/265f54ee-02b7-11ec-b64f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31359c895675faf0fa219bee51bfb314013f2394 --- /dev/null +++ b/distmin/bellman/265f54ee-02b7-11ec-b64f-2cf05d84bfc2.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +using namespace std; + +short int n,m,x,y,coada[1501*1501]; + +int z,nr[1501],rez[1501]; + +double d[1501]; + +bitset<1501> viz; + +const double eps=1e-8; + +const double eps1=eps*(-1); + +vectora[1501],ap(1501,0); + +vectorc[1501]; + +void citire() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%hd%hd",&n,&m); + + for (int i=1; i<=m; ++i) + + { + + scanf("%hd%hd%d",&x,&y,&z); + + double g=log10(z); + + a[x].push_back(y); + + c[x].push_back(g); + + a[y].push_back(x); + + c[y].push_back(g); + + } + +} + +void bf() + +{ + + int p=0,u=0; + + coada[u++]=1; + + nr[1]=1; + + viz[1]=1; + + while (u!=p) + + { + + x=coada[p++]; + + viz[x]=false; + + size_t g=a[x].size(); + + for (size_t i=0; i!=g; ++i) + + { + + y=a[x][i]; + + double aux=d[x]+c[x][i],diff=d[y]-aux; + + if (diff>=eps) + + { + + d[y]=aux; + + if (!viz[y]) + + { + + viz[y]=true; + + coada[u++]=y; + + } + + } + + } + + } + +} + +void afis() + +{ + + for (int i=2; i<=n; ++i) + + printf("%d ",nr[i]); + +} + +void df(int x) + +{ + + viz[x]=1; + + size_t g=a[x].size(); + + for (size_t i=0; i=eps1) + + { + + + + if (!viz[a[x][i]]) + + df(a[x][i]); + + nr[x]+=nr[a[x][i]]; + + if (nr[x]>=104659) + + nr[x]-=104659; + + } + + } + +} + +int main() + +{ + + citire(); + + for (int j=2; j<=n; ++j) + + d[j]=1<<30;; + + bf(); + + viz.reset(); + + nr[1]=1; + + for (int i=2; i<=n; ++i) + + if (!viz[i]) + + df(i); + + afis(); + + + + + + + + return 0; + +} diff --git a/distmin/bellman/2695cb35-02b7-11ec-ac26-2cf05d84bfc2.cpp b/distmin/bellman/2695cb35-02b7-11ec-ac26-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c89a1979e423aa05985204ab3a2f55f4356a997b --- /dev/null +++ b/distmin/bellman/2695cb35-02b7-11ec-ac26-2cf05d84bfc2.cpp @@ -0,0 +1,219 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +const int N=2501,M=1000001; + + + +int n,m,s[N],q[M],p,var[N],u; + +double c[N]; + +bool viz[N]; + +struct xy{ + + int to; + + double c; + +}; + +vector v[N]; + + + +void read() + +{ + + int x,y,z; + + xy a; + + scanf("%d%d",&n,&m); + + for( int i=1 ; i<=m ; ++i ) + + { + + scanf("%d%d%d",&x,&y,&z); + + a.c=log10((double)z); + + a.to=y; + + v[x].push_back(a); + + ++s[x]; + + a.to=x; + + v[y].push_back(a); + + ++s[y]; + + } + +} + + + +void ini() + +{ + + for( int i=2 ; i<=n ; ++i ) + + c[i]=(double)INT_MAX; + +} + + + +void check( int x ) + +{ + + for( int i=0 ; i104659) + + var[x]-=104659; + + } + +} + + + +void solve() + +{ + + ini(); + + bf(); + + var[1]=1; + + viz[1]=1; + + for( int i=2 ; i<=n ; ++i ) + + if(viz[i]==0) + + apel(i); + +} + + + +void print() + +{ + + for( int i=2 ; i<=n ; ++i ) + + printf("%d ",var[i]); + +} + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/distmin/bellman/28be588b-02b7-11ec-920d-2cf05d84bfc2.cpp b/distmin/bellman/28be588b-02b7-11ec-920d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62feb2751cde2532ca3ef70a49ffd96e38059d36 --- /dev/null +++ b/distmin/bellman/28be588b-02b7-11ec-920d-2cf05d84bfc2.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/292e166c-02b7-11ec-837b-2cf05d84bfc2.cpp b/distmin/bellman/292e166c-02b7-11ec-837b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f984a065fe3690d29986536a7f25ca8afd6cf73 --- /dev/null +++ b/distmin/bellman/292e166c-02b7-11ec-837b-2cf05d84bfc2.cpp @@ -0,0 +1,198 @@ +#include +#include #include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/296c4f58-02b7-11ec-b822-2cf05d84bfc2.cpp b/distmin/bellman/296c4f58-02b7-11ec-b822-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f984a065fe3690d29986536a7f25ca8afd6cf73 --- /dev/null +++ b/distmin/bellman/296c4f58-02b7-11ec-b822-2cf05d84bfc2.cpp @@ -0,0 +1,198 @@ +#include +#include #include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/2ded5da0-3c3b-49ab-a127-2a4031b71b2a.cpp b/distmin/bellman/2ded5da0-3c3b-49ab-a127-2a4031b71b2a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b223a32c13f6d8c63c051ddd9703b5a927ea4d18 --- /dev/null +++ b/distmin/bellman/2ded5da0-3c3b-49ab-a127-2a4031b71b2a.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int ANS[1505],viz[1505]; + +double dist[1505]; + +int n,m,x,y,c; + +vector > G[1505]; + +queue q; + + + +void bf(){ + + for(int i = 1; i <= n; ++i) + + dist[i] = 10000000.0; + + + + dist[1] = 0; + + ANS[1] = 1; + + q.push(1); + + viz[1] = 1; + + while(!q.empty()){ + + int nod = q.front(); + + q.pop(); + + for(int i = 0; i < G[nod].size(); ++i){ + + if(abs(dist[G[nod][i].first] - dist[nod] - G[nod][i].second) <= 0.0000001){ + + ANS[G[nod][i].first] = (ANS[G[nod][i].first] + ANS[nod] % 104659) % 104659; + + } + + if(dist[G[nod][i].first] - dist[nod] - G[nod][i].second > 0.0000001){ + + dist[G[nod][i].first] = dist[nod] + G[nod][i].second; + + ANS[G[nod][i].first] = ANS[nod]; + + if(viz[G[nod][i].first] == 0){ + + q.push(G[nod][i].first); + + viz[G[nod][i].first] = 1; + + } + + } + + } + + } + + ofstream g("dmin.out"); + + for(int i = 2; i <= n; ++i){ + + g << ANS[i] % 104659 <<' '; + + } + + g.close(); + +} + +int main() + +{ + + ifstream f("dmin.in"); + + + + + + f >> n >> m; + + for(int i = 1; i <= m; ++i){ + + f >> x >> y >> c; + + double w = log(c); + + G[x].push_back(make_pair(y,w)); + + G[y].push_back(make_pair(x,w)); + + } + + + + bf(); + + + + f.close(); + + + + return 0; + +} diff --git a/distmin/bellman/302ac7e4-24a6-4402-8ca6-2f7f02de6e32.cpp b/distmin/bellman/302ac7e4-24a6-4402-8ca6-2f7f02de6e32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cc27b6ad740b7b14ce9b16bf38185c1d01043d7 --- /dev/null +++ b/distmin/bellman/302ac7e4-24a6-4402-8ca6-2f7f02de6e32.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream f("dmin.in"); ofstream g("dmin.out"); + +inline double modul(double x) + +{ if(x<0) return -x; + + return x; + +} + +vector < pair < int,double > >G[1502]; + +queue Q; + +int n, m, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + +void bellmanFord() + +{ int x, vecin; + + double cost; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { x=Q.front(); + + Q.pop(); + + for (int i=0;idmin[x]+cost) + + { dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) + + { Q.push(vecin); viz[vecin]=1; } + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + +} + +int main() + +{ double z,cost; + + f>>n>>m; + + for (int x,y,i=1;i<=m;i++) + + { f>>x>>y>>cost; + + z=log(cost); + + G[x].push_back(make_pair(y,z)); + + G[y].push_back(make_pair(x,z)); + + } + + nrd[1]=1; + + for (int i=2;i<=n;i++) dmin[i]=1000001; + + bellmanFord(); + + for(int i=2;i<=n;++i) g< +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/356f56ba-214b-46b3-98cd-063e895dc3cf.cpp b/distmin/bellman/356f56ba-214b-46b3-98cd-063e895dc3cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7db01b52c4cd9d2b35e4676b14259265118dc6a1 --- /dev/null +++ b/distmin/bellman/356f56ba-214b-46b3-98cd-063e895dc3cf.cpp @@ -0,0 +1,116 @@ +# include +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include +#include +#include +#include + + + + +using namespace std; + +const double eps=1e-10; + +const double INFINIT=9999999999; + +vector< pair > Graf[1505]; + +queue Q; + +int N,M,x,y,z,D[1505]; + +bool InCoada[1505]; + +double Dmin[1505]; + + + +void Read() + +{ + + ifstream g("dmin.in"); + + g>>N>>M; + + for(int i=1;i<=M;i++) + + { + + g>>x>>y>>z; + + double z1=log(z); + + Graf[x].push_back(make_pair(y,z1)); + + Graf[y].push_back(make_pair(x,z1)); + + } + +} + + + +void BellManFord() + +{ + + for(int i=1;i<=N;i++) + + Dmin[i]=INFINIT; + + + + Q.push(1); + + D[1]=1;Dmin[1]=0.0;InCoada[1]=true; + + while(!Q.empty()) + + { + + int nod=Q.front(); + + Q.pop(); + + InCoada[nod]=false; + + for(vector >::iterator it=Graf[nod].begin();it!=Graf[nod].end();it++) + + { + + int vecin=it->first; + + double cost=it->second; + + if(Dmin[nod]+cost-Dmin[vecin]< -eps) + + { + + D[vecin]=D[nod]; + + Dmin[vecin]=Dmin[nod]+cost; + + if(!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + else + + { + + if(fabs(Dmin[nod]+cost-Dmin[vecin])<=eps) + + { + + D[vecin]=(D[vecin]+D[nod])%104659; + + if (!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + } + + } + + } + +} + + + +void Write() + +{ + + ofstream f("dmin.out"); + + for(int i=2;i<=N;i++) + + f< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505], sol[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now] - dpant[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now] - dpant[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + dpant[now] = dp[now]; + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/3a73ff6c-239c-4c60-96d4-dbdb8b815fc2.cpp b/distmin/bellman/3a73ff6c-239c-4c60-96d4-dbdb8b815fc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cb6b385103510cc63c750bed1a579c4e7d9b0d1 --- /dev/null +++ b/distmin/bellman/3a73ff6c-239c-4c60-96d4-dbdb8b815fc2.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ofstream out("dmin.out"); +vector > G[1510]; + +typedef vector > ::iterator IT; + +queueQ; + +bitset<1510>uz; + + + +int drum[1510],n,m; + +double d[1510],EPS=0.0000000001,c; + + + +void read(); + +void BMF(); + +void write(); + + + +int main() + +{ + + read(); + + BMF(); + + write(); + + return 0; + +} + + + +void read() + +{ + + ifstream in("dmin.in"); + + in>>n>>m; + + for(int x,y,c ; m ; --m) + + { + + in>>x>>y>>c; + + G[x].push_back(make_pair(y,log( (double) c) ) ); + + G[y].push_back(make_pair(x,log( (double) c) ) ); + + } + +} + + + +void BMF() + +{ + + for(int i=1;i<=n;i++) + + d[i]=0x3f3f3f3f; + + d[1]=0; + + drum[1]=1; + + Q.push(1); + + uz[1]=1; + + int k; + + for(; Q.size(); Q.pop()) + + { + + k=Q.front(); + + for(IT I=G[k].begin();I!=G[k].end();++I) + + { + + int val=I->first; + + double cost=I->second; + + if( fabs( d[val] - d[k] - cost ) < EPS ) + + drum[val]= (drum[val] + drum[k] ) %104659; + + else + + if( d[val] - d[k] - cost > EPS) + + { + + if(!uz[val]) + + { + + Q.push(val); + + uz[val]=1; + + } + + d[val]=d[k] + cost; + + drum[val]= drum[k] ; + + } + + } + + uz[k]=0; + + } + +} + + + +void write() + +{ + + for(int i=2;i<=n;i++) + + out< +#include +#include +#include +#include +#include +using namespace std; + + + +const double eps = 1e-10; + +const int NMAX = 1503; + +const int MOD = 104659; + +const int oo = 0x3f3f3f3f; + +bitset viz; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, x, y, r[NMAX]; + +double cost; + + + +vector d(NMAX, oo); + + + +inline bool egal(double a, double b) + +{ + + return (abs(a - b) <= eps); + +} + + + +vector < pair > g[NMAX]; + +queue q; + + + +void BellmanFord() + +{ + + q.push(1); + + d[1] = 0; + + r[1] = 1; + + while (!q.empty()) + + { + + int x = q.front(); + + q.pop(); + + viz[x] = 0; + + for (size_t i = 0; i < g[x].size(); ++i) + + { + + int nod = g[x][i].first; + + if (egal(d[x] + g[x][i].second, d[nod])) + + { + + r[nod] += r[x]; + + if (r[nod] > MOD) r[nod] -= MOD; + + } + + else if (d[x] + g[x][i].second < d[nod]) + + { + + d[nod] = d[x] + g[x][i].second; + + r[nod] = r[x]; + + if (viz[nod] == 0) + + { + + viz[nod] = 1; + + q.push(nod); + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> cost; + + cost = log(cost); + + g[x].push_back({ y, cost }); + + } + + BellmanFord(); + + for (int i = 2; i <= n; ++i) + + fout << r[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/406e6b1f-7f7a-4161-a50b-96fedb971a57.cpp b/distmin/bellman/406e6b1f-7f7a-4161-a50b-96fedb971a57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ac97da99bf6e5279a9e8f34f90a1b5bb0510175 --- /dev/null +++ b/distmin/bellman/406e6b1f-7f7a-4161-a50b-96fedb971a57.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +const string problemName = "dmin"; +const string inputFile = problemName + ".in"; +const string outputFile = problemName + ".out"; + + +typedef long long int lld; +typedef pair PII; +typedef pair PIL; +typedef pair PLI; +typedef pair PLL; +typedef pair PID; +typedef pair PDI; + +const int INF = (1LL << 30) - 1; +const lld LINF = (1LL << 62) - 1; +const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; +const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; +const int NMAX = 1500 + 5; +const int MOD = 104659; +const double EPS = 1e-10; + +int comp(double a, double b) { + if(fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 0; + if((a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 1; + return -1; +} + +int N, M; +vector V[NMAX]; +int sol[NMAX]; +double D[NMAX]; +bitset viz; +deque Q; + +void bf(int x) { + int i, y; + double z; + + for(i = 2; i <= N; i++) + D[i] = INF; + + sol[x] = 1; + Q.push_back(x); + viz[x] = 1; + + while(!Q.empty()) { + x = Q.front(); + Q.pop_front(); + + viz[x] = 0; + + for(auto nod : V[x]) { + y = nod.first; + z = nod.second; + + if(comp(D[x] + z, D[y]) < 0) { + D[y] = D[x] + z; + sol[y] = 0; + + if(!viz[y]) { + Q.push_back(y); + viz[y] = 1; + } + } + + if(comp(D[x] + z, D[y]) == 0) + sol[y] = (sol[y] + sol[x]) % MOD; + } + } +} + +int main() { + int i, x, y, z; + + + freopen(inputFile.c_str(), "r", stdin); + freopen(outputFile.c_str(), "w", stdout); + + + scanf("%d%d", &N, &M); + + while(M--) { + scanf("%d%d%d", &x, &y, &z); + V[x].push_back(make_pair(y, log2(z))); + V[y].push_back(make_pair(x, log2(z))); + } + + bf(1); + + for(i = 2; i <= N; i++) + printf("%d ", sol[i]); + + return 0; +} diff --git a/distmin/bellman/4390d071-83cf-4ad3-887f-0ba7fb6a510e.cpp b/distmin/bellman/4390d071-83cf-4ad3-887f-0ba7fb6a510e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ac97da99bf6e5279a9e8f34f90a1b5bb0510175 --- /dev/null +++ b/distmin/bellman/4390d071-83cf-4ad3-887f-0ba7fb6a510e.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +const string problemName = "dmin"; +const string inputFile = problemName + ".in"; +const string outputFile = problemName + ".out"; + + +typedef long long int lld; +typedef pair PII; +typedef pair PIL; +typedef pair PLI; +typedef pair PLL; +typedef pair PID; +typedef pair PDI; + +const int INF = (1LL << 30) - 1; +const lld LINF = (1LL << 62) - 1; +const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; +const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; +const int NMAX = 1500 + 5; +const int MOD = 104659; +const double EPS = 1e-10; + +int comp(double a, double b) { + if(fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 0; + if((a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 1; + return -1; +} + +int N, M; +vector V[NMAX]; +int sol[NMAX]; +double D[NMAX]; +bitset viz; +deque Q; + +void bf(int x) { + int i, y; + double z; + + for(i = 2; i <= N; i++) + D[i] = INF; + + sol[x] = 1; + Q.push_back(x); + viz[x] = 1; + + while(!Q.empty()) { + x = Q.front(); + Q.pop_front(); + + viz[x] = 0; + + for(auto nod : V[x]) { + y = nod.first; + z = nod.second; + + if(comp(D[x] + z, D[y]) < 0) { + D[y] = D[x] + z; + sol[y] = 0; + + if(!viz[y]) { + Q.push_back(y); + viz[y] = 1; + } + } + + if(comp(D[x] + z, D[y]) == 0) + sol[y] = (sol[y] + sol[x]) % MOD; + } + } +} + +int main() { + int i, x, y, z; + + + freopen(inputFile.c_str(), "r", stdin); + freopen(outputFile.c_str(), "w", stdout); + + + scanf("%d%d", &N, &M); + + while(M--) { + scanf("%d%d%d", &x, &y, &z); + V[x].push_back(make_pair(y, log2(z))); + V[y].push_back(make_pair(x, log2(z))); + } + + bf(1); + + for(i = 2; i <= N; i++) + printf("%d ", sol[i]); + + return 0; +} diff --git a/distmin/bellman/43b181bd-9fef-4d91-8e99-1dc19a7af03c.cpp b/distmin/bellman/43b181bd-9fef-4d91-8e99-1dc19a7af03c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..778eebe3f3ce8e88a4464a6504468817ad74129b --- /dev/null +++ b/distmin/bellman/43b181bd-9fef-4d91-8e99-1dc19a7af03c.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +fstream f("dmin.in", ios::in); + +fstream g("dmin.out", ios::out); + + + +const int nmax = 1505; + +const double INF = 100000.0; + +const double eps = 0.00001; + +const int modulo = 104659; + + + +vector < pair > a[nmax]; + + + +double c, cost; + +double d[nmax]; + +int cnt[nmax], viz[nmax]; + +int n, m, x, y, i; + + + +void read() + +{ + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + cost = log(c); + + a[x].push_back(make_pair(y, cost)); + + a[y].push_back(make_pair(x, cost)); + + } + +} + + + +void bellman() + +{ + + queue q; + + double dist; + + int nc; + + for (i = 2; i <= n; i++) d[i] = INF; + + q.push(1); + + d[1] = 0; + + viz[1] = 1; + + cnt[1] = 1; + + while (!q.empty()) + + { + + nc = q.front(); q.pop(); + + dist = d[nc]; + + viz[nc] = 0; + + for (vector< pair >::iterator it = a[nc].begin(); it != a[nc].end(); ++it) + + { + + if (fabs(dist + it->second - d[it->first]) < eps) + + { + + cnt[it->first] = (cnt[it->first] + cnt[nc]) % modulo; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + else if (dist + it->second < d[it->first]) + + { + + d[it->first] = dist + it->second; + + cnt[it->first] = cnt[nc]; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + } + + } + +} + + + +int main() + +{ + + read(); + + bellman(); + + for (i = 2; i <= n; i++) g << cnt[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/45853eaa-3622-44f0-a649-d52d038c2d17.cpp b/distmin/bellman/45853eaa-3622-44f0-a649-d52d038c2d17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2300223720c8fe457bba515ab208788bad8fbfff --- /dev/null +++ b/distmin/bellman/45853eaa-3622-44f0-a649-d52d038c2d17.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510], Q; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +bool Sortare(int a, int b) + +{ + + return dist[a] > dist[b]; + +} + + + +void Bellman_Ford() + +{ + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push_back(1); + + while(Q.size()) + + { + + x=Q[0]; + + pop_heap(Q.begin(), Q.end(), Sortare); + + Q.pop_back(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include +using namespace std; +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int n,m; + +vector < pair < int , double > > G[1501]; + +vector < int > used; + +vector < double > d; + +vector < int > drmin; + + + + + +void read() + +{ + + f >> n >> m; + + used = vector < int > (n+1); + + drmin = vector < int > (n+1,0); + + for ( ; m-- ; ) + + { + + int x,y,z; + + f >> x >> y >> z; + + double cst = log(z); + + G[x].push_back(make_pair(y,cst)); + + G[y].push_back(make_pair(x,cst)); + + } + +} + + + +void bellman ( int i ) + +{ + + queue < int > q; + + d = vector < double > (n+1); + + for ( int k = 1; k <= n ; k++ ) + + d[k] = 10000000.0; + + d[i] = 0.0; + + q.push(i); + + drmin[i] = 1; + + used[i] = 1; + + while ( !q.empty() ) + + { + + int nod = q.front(); + + q.pop(); + + for ( vector < pair < int , double > > ::iterator j = G[nod].begin(); j != G[nod].end(); j++ ) + + { + + int x = (*j).first; + + double cost = (*j).second; + + if( abs( d[x] - d[nod] - cost ) <= 0.0000001 ) + + { + + drmin[x] = (drmin[x] + drmin[nod])%104659; + + } + + if ( d[x] - d[nod] - cost > 0.0000001 ) + + { + + d[x] = d[nod] + cost; + + drmin[x] = drmin[nod]; + + if ( !used[x] ) + + { + + q.push(x); + + used[x] = 1; + + } + + } + + } + + } + + for ( i = 2; i <= n ; i++ ) + + g << drmin[i]%104659 << " "; + +} + + + +int main() + +{ + + read(); + + bellman(1); + + return 0; + +} diff --git a/distmin/bellman/477805f9-9a43-4980-8e8f-50c699924e35.cpp b/distmin/bellman/477805f9-9a43-4980-8e8f-50c699924e35.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f984a065fe3690d29986536a7f25ca8afd6cf73 --- /dev/null +++ b/distmin/bellman/477805f9-9a43-4980-8e8f-50c699924e35.cpp @@ -0,0 +1,198 @@ +#include +#include #include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/49a5e418-1bc8-4680-b6f9-8fd91e3bd088.cpp b/distmin/bellman/49a5e418-1bc8-4680-b6f9-8fd91e3bd088.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3662256add92df9c3714ff8271dff60686e09fe --- /dev/null +++ b/distmin/bellman/49a5e418-1bc8-4680-b6f9-8fd91e3bd088.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include + + + + + +using namespace std; + +FILE *f=fopen("dmin.in", "r"); + +FILE *g=fopen("dmin.out", "w"); + + +int N, M, sol[1510]; +double D[1510]; +vector< pair < int, double > > G[1510]; +queue Q; +bool viz[1510]; +void BellmanFord() { + int i, x; + + for(i=1; i<=N; i++) { + viz[i] = false; + D[i] = 99999999; + } + viz[1] = true; + Q.push(1); + D[1] = 0; + + sol[1] = 1; + while(!Q.empty()) { + + + x=Q.front(); + + Q.pop(); + viz[x] = false; + for(vector< pair < int, double > >::iterator it=G[x].begin(); it!=G[x].end(); it++) { + if(0.00000001 < D[(*it).first] -D[x] - (*it).second) { + D[(*it).first] = D[x] + (*it).second; + sol[(*it).first] = sol[x]; + if(!viz[(*it).first]) { + viz[(*it).first] = 1; + Q.push((*it).first); + } + } + else + + if(D[x] + (*it).second - D[(*it).first] <= 0.00000001) { + sol[(*it).first] += sol[x]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + + int i, j, x, y; + + fscanf(f, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f, "%d %d %d\n", &x, &y, &j); + double cost = (double)log(j); + + G[x].push_back( make_pair(y, cost) ); + G[y].push_back( make_pair(x, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(g, "%d ", sol[i]); + } + + fclose(f); + + fclose(g); + return 0; +} diff --git a/distmin/bellman/49c3e396-e717-4c02-b6a6-d911194178fb.cpp b/distmin/bellman/49c3e396-e717-4c02-b6a6-d911194178fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f71293358bd1f98558d27994afe03c7bd4cb7a55 --- /dev/null +++ b/distmin/bellman/49c3e396-e717-4c02-b6a6-d911194178fb.cpp @@ -0,0 +1,198 @@ +#include #include +#include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/4a95de4d-3642-4a19-a1a6-c54da59c6b9d.cpp b/distmin/bellman/4a95de4d-3642-4a19-a1a6-c54da59c6b9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60d66f85b7db6f1c34bde9800e01b85a0be5fb96 --- /dev/null +++ b/distmin/bellman/4a95de4d-3642-4a19-a1a6-c54da59c6b9d.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + + + + + + + +using namespace std; + +ifstream first("dmin.in"); +ofstream g("dmin.out"); + +int n, m, nr[1510], vz[1510]; +double d[1510]; + +struct muchie +{ + int nod; + double cost; +}; + +vector a[1510]; + +set< pair > second; + +void Citeste() +{ + int i, x, y; + muchie r; + + first>>n>>m; + + for (i=1; i<=m; ++i) + { + first>>x>>y>>r.cost; + + r.nod=y; a[x].push_back(r); + r.nod=x; a[y].push_back(r); + } +} + +void Initializeaza() +{ + int i; + + for (i=1; i<=n; ++i) d[i]=2500000000000.0; + + d[1]=0.0; nr[1]=1; + + second.insert( make_pair(0.0, 1) ); +} + +void Solve() +{ + int i, gata; + double val; + muchie r; + pair pr; + set > :: iterator is; + + do + { + gata=0; + + while (!second.empty() && !gata) + { + is=second.begin(); + pr=*is; + if (!vz[pr.second]) gata=1; + second.erase(is); + } + + if (gata) + { + vz[pr.second]=1; + for (i=0; i0.0000000001) + { + d[r.nod]=val; + second.insert(make_pair(val, r.nod)); + nr[r.nod]=nr[pr.second]; + } + else + if (fabs(d[r.nod]-val)<0.0000000001) nr[r.nod]=(nr[r.nod]+nr[pr.second])%104659; + } + } + } + + }while (gata); + + for (i=2; i<=n; ++i) g< +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(-0.00001 <= dmin[p] + (*it).second - dmin[(*it).first] && dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/4e1d76e9-cf61-402f-a6cb-265b38b1b148.cpp b/distmin/bellman/4e1d76e9-cf61-402f-a6cb-265b38b1b148.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d94bbe5f4a2fc056def6fddba1ac6bd905168dc --- /dev/null +++ b/distmin/bellman/4e1d76e9-cf61-402f-a6cb-265b38b1b148.cpp @@ -0,0 +1,158 @@ +#include +FILE *fin = freopen("dmin.in", "r", stdin); + +FILE *fout = freopen("dmin.out", "w", stdout); + + + +using namespace std; + +int V, E, sol[1502]; + +double D[1502]; + +struct comp + +{ + + bool operator() (const pair &a, const pair &b) + + { + + if(fabs(a.second - b.second) < 0.0000000001) + + return 0; + + else return a.second > b.second + 0.0000000001; + + } + +}; + +priority_queue , vector >, comp> Q; + +vector > G[1502]; + +bool F[1502]; + +void read() + +{ + + int x, y; + + double z; + + scanf("%d %d", &V, &E); + + while(E --) + + { + + scanf("%d %d %lf", &x, &y, &z); + + z = log(z); + + G[x].push_back(pair (y, z)); + + G[y].push_back(pair (x, z)); + + } + + for(int i = 2; i <= V; ++ i) + + D[i] = 2000000000.0; + + Q.push(pair (1, 0.0)); + + sol[1] = 1; + +} + +void solve() + +{ + + int x, y; + + double z; + + while(!Q.empty()) + + { + + x = Q.top().first; + + + + Q.pop(); + + int sz = G[x].size(); + + if (F[x]) + + continue; + + + + for(int i = 0; i < sz; ++ i) + + { + + y = G[x][i].first; + + z = G[x][i].second; + + if (!F[y]) + + { + + if(D[y] > D[x] + z + 0.0000000001) + + { + + D[y] = D[x] + z; + + sol[y] = sol[x]; + + Q.push(pair (y, D[y])); + + } + + else if(fabs(D[y] - D[x] - z) < 0.0000000001) + + sol[y] = (sol[y] + sol[x]) % 104659; + + } + + } + + F[x] = 1; + + } + +} + +void write() + +{ + + for(int i = 2; i <= V; ++ i) + + printf("%d ", sol[i]); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/distmin/bellman/51db516d-036a-43a1-918f-5606c4dc9196.cpp b/distmin/bellman/51db516d-036a-43a1-918f-5606c4dc9196.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b223a32c13f6d8c63c051ddd9703b5a927ea4d18 --- /dev/null +++ b/distmin/bellman/51db516d-036a-43a1-918f-5606c4dc9196.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int ANS[1505],viz[1505]; + +double dist[1505]; + +int n,m,x,y,c; + +vector > G[1505]; + +queue q; + + + +void bf(){ + + for(int i = 1; i <= n; ++i) + + dist[i] = 10000000.0; + + + + dist[1] = 0; + + ANS[1] = 1; + + q.push(1); + + viz[1] = 1; + + while(!q.empty()){ + + int nod = q.front(); + + q.pop(); + + for(int i = 0; i < G[nod].size(); ++i){ + + if(abs(dist[G[nod][i].first] - dist[nod] - G[nod][i].second) <= 0.0000001){ + + ANS[G[nod][i].first] = (ANS[G[nod][i].first] + ANS[nod] % 104659) % 104659; + + } + + if(dist[G[nod][i].first] - dist[nod] - G[nod][i].second > 0.0000001){ + + dist[G[nod][i].first] = dist[nod] + G[nod][i].second; + + ANS[G[nod][i].first] = ANS[nod]; + + if(viz[G[nod][i].first] == 0){ + + q.push(G[nod][i].first); + + viz[G[nod][i].first] = 1; + + } + + } + + } + + } + + ofstream g("dmin.out"); + + for(int i = 2; i <= n; ++i){ + + g << ANS[i] % 104659 <<' '; + + } + + g.close(); + +} + +int main() + +{ + + ifstream f("dmin.in"); + + + + + + f >> n >> m; + + for(int i = 1; i <= m; ++i){ + + f >> x >> y >> c; + + double w = log(c); + + G[x].push_back(make_pair(y,w)); + + G[y].push_back(make_pair(x,w)); + + } + + + + bf(); + + + + f.close(); + + + + return 0; + +} diff --git a/distmin/bellman/5576cbe5-b890-458d-8ef0-4c8d305b1fc0.cpp b/distmin/bellman/5576cbe5-b890-458d-8ef0-4c8d305b1fc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfed5cd80a159d8699d9cbc17a92414a4b2c5879 --- /dev/null +++ b/distmin/bellman/5576cbe5-b890-458d-8ef0-4c8d305b1fc0.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/55b0081d-ca85-4099-91f4-1dd9d4bbdb3d.cpp b/distmin/bellman/55b0081d-ca85-4099-91f4-1dd9d4bbdb3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62feb2751cde2532ca3ef70a49ffd96e38059d36 --- /dev/null +++ b/distmin/bellman/55b0081d-ca85-4099-91f4-1dd9d4bbdb3d.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/55b329b8-f57a-4e2d-b4d9-ea688b336adf.cpp b/distmin/bellman/55b329b8-f57a-4e2d-b4d9-ea688b336adf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cfc2dd3b389ddf99ee91e864d2a391a74442de0 --- /dev/null +++ b/distmin/bellman/55b329b8-f57a-4e2d-b4d9-ea688b336adf.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + double logc = log(c); + + G[a].push_back(make_pair(b, logc)); + + G[b].push_back(make_pair(a, logc)); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/59693aeb-dfa9-4653-9db8-6675d1ecdf70.cpp b/distmin/bellman/59693aeb-dfa9-4653-9db8-6675d1ecdf70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b68ba9b2b642443ce2a45231b3aa8eac9aaf8d6 --- /dev/null +++ b/distmin/bellman/59693aeb-dfa9-4653-9db8-6675d1ecdf70.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, nrd[1501]; + +double M[1501][1501], dmin[1501]; + +bool inq[1501]; + + + +queue Q; + + + +void citire() + +{ + + int a, b, t; + + fin >> n >> m; + + for(int i = 1; i <= n; i++) + + for(int j = 1; j <= n; j++) + + if(i != j) M[i][j] = 2000000000; + + while(m--) + + fin >> a >> b >> t, M[a][b] = M[b][a] = log(t); + +} + + + +void bellman_ford(int nod) + +{ + + Q.push(nod); + + inq[nod] = 1; + + for(int i = 1; i <= n; ++i) dmin[i] = 2000000000; + + dmin[1] = 0; + + nrd[1] = 1; + + while(!Q.empty()) + + { + + int x = Q.front(); + + Q.pop(); inq[x] = 0; + + for(int i = 1; i <= n; ++i){ + + if(x != i) + + if(M[x][i] != 2000000000 && dmin[i] > dmin[x] + M[x][i] + 0.0000000001){ + + dmin[i] = dmin[x] + M[x][i]; + + nrd[i] = nrd[x]; + + if(inq[i] == 0) Q.push(i), inq[i] = 1; + + } + + else if(fabs(dmin[i] - dmin[x] - M[x][i]) < 0.0000000001 && M[x][i] != 2000000000) nrd[i] = (nrd[i] % 104659 + nrd[x] % 104659) % 104659; + + } + + } + +} + + + +int main() + +{ + + citire(); + + bellman_ford(1); + + for(int i = 2; i <= n; i++) + + fout << nrd[i] << ' '; + + return 0; + +} diff --git a/distmin/bellman/59ecceaa-3a35-4381-a215-7e93957d9036.cpp b/distmin/bellman/59ecceaa-3a35-4381-a215-7e93957d9036.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faacd8ce6b694d6991c19d898050389a815655bb --- /dev/null +++ b/distmin/bellman/59ecceaa-3a35-4381-a215-7e93957d9036.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const double inf= 0x3f3f3f; + +const double eps= 0.0000001; + +const int nmax= 1500; + +const int mod= 104659; + + + +double minim[nmax+1]; + +int d[nmax+1]; + + + +struct str { + + int x; + + double y; + +}; + + + +struct str_cmp { + + bool operator () ( const str &x, const str &y ) { + + return x.y>y.y; + + } + +}; + + + +priority_queue , str_cmp> h; + +vector v[nmax+1]; + + + +inline str mp( int x, double y ) { + + str sol; + + sol.x= x, sol.y= y; + + + + return sol; + +} + + + +double av( double x ) { + + if ( x<0 ) { + + x= -x; + + } + + return x; + +} + + + +void dijkstra( ) { + + d[1]= 1; + + for ( h.push(mp(1, 0)); !h.empty(); ) { + + str x= h.top(); + + h.pop(); + + + + if ( x.y-minim[x.x]>=eps ) { + + continue; + + } + + + + for ( vector ::iterator it= v[x.x].begin(); it!=v[x.x].end(); ++it ) { + + if ( minim[(*it).x]-minim[x.x]-(*it).y>eps ) { + + minim[(*it).x]= minim[x.x]+(*it).y; + + d[(*it).x]= d[x.x]; + + h.push(mp((*it).x, minim[(*it).x])); + + } else if ( av(minim[(*it).x]-minim[x.x]-(*it).y)<=eps ) { + + d[(*it).x]= (d[(*it).x]+d[x.x])%mod; + + } + + } + + } + +} + + + +int main( ) { + + int n, m; + + fin>>n>>m; + + for ( int i= 1; i<=m; ++i ) { + + int x, y; + + double z; + + fin>>x>>y>>z; + + z= log(z); + + + + v[x].push_back(mp(y, z)); + + v[y].push_back(mp(x, z)); + + } + + + + for ( int i= 2; i<=n; ++i ) { + + minim[i]= inf; + + } + + dijkstra(); + + + + for ( int i= 2; i<=n; ++i ) { + + fout< +#include +#include +#include +#include +using namespace std; +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int n,m; + +vector < pair < int , double > > G[1501]; + +vector < int > used; + +vector < double > d; + +vector < int > drmin; + + + + + +void read() + +{ + + f >> n >> m; + + used = vector < int > (n+1); + + drmin = vector < int > (n+1,0); + + for ( ; m-- ; ) + + { + + int x,y,z; + + f >> x >> y >> z; + + double cst = log(z); + + G[x].push_back(make_pair(y,cst)); + + G[y].push_back(make_pair(x,cst)); + + } + +} + + + +void bellman ( int i ) + +{ + + queue < int > q; + + d = vector < double > (n+1); + + for ( int k = 1; k <= n ; k++ ) + + d[k] = 10000000.0; + + d[i] = 0.0; + + q.push(i); + + drmin[i] = 1; + + used[i] = 1; + + while ( !q.empty() ) + + { + + int nod = q.front(); + + q.pop(); + + for ( vector < pair < int , double > > ::iterator j = G[nod].begin(); j != G[nod].end(); j++ ) + + { + + int x = (*j).first; + + double cost = (*j).second; + + if( abs( d[x] - d[nod] - cost ) <= 0.0000001 ) + + { + + drmin[x] = (drmin[x] + drmin[nod])%104659; + + } + + if ( d[x] - d[nod] - cost > 0.0000001 ) + + { + + d[x] = d[nod] + cost; + + drmin[x] = drmin[nod]; + + if ( !used[x] ) + + { + + q.push(x); + + used[x] = 1; + + } + + } + + } + + } + + for ( i = 2; i <= n ; i++ ) + + g << drmin[i]%104659 << " "; + +} + + + +int main() + +{ + + read(); + + bellman(1); + + return 0; + +} diff --git a/distmin/bellman/5d234507-970f-4f71-bd68-851403c46b33.cpp b/distmin/bellman/5d234507-970f-4f71-bd68-851403c46b33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..776f7b651afd59e62e878e197ae0e88d882dc5eb --- /dev/null +++ b/distmin/bellman/5d234507-970f-4f71-bd68-851403c46b33.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(-0.00001 <= dmin[p] + (*it).second - dmin[(*it).first] && dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/5d9aa112-364a-47ac-9f0b-0637cc003786.cpp b/distmin/bellman/5d9aa112-364a-47ac-9f0b-0637cc003786.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbad9333d943e72b072de4019d28c9234379467e --- /dev/null +++ b/distmin/bellman/5d9aa112-364a-47ac-9f0b-0637cc003786.cpp @@ -0,0 +1,209 @@ +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +struct nod + +{ type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + +template + +class vector + +{ type *aux; + + int capacity,m_size; + + public: + + type *act; + + vector() + + { act=new type[1]; capacity=1; m_size=0; } + + void reallocate() + + { aux=new type[2*capacity] , capacity*=2; + + for(int i=0;ia[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("dmin.in"); +ofstream fout("dmin.out"); + +int n, m; +typedef pair nod; +vector d(1505, 1 << 30); +vector a[1505]; +vector fr(1505, 0); +vector viz(1505); +queue q; + +int main() +{ + fin>>n>>m; + while(m--) + { + int x, second; + double first, c2; + fin>>x>>second>>first; + c2 = log10(first); + a[x].push_back(nod(c2, second)); + a[second].push_back(nod(c2, x)); + } + + q.push(1); + d[1] = 0; + fr[1] = 1; + viz[1] = 1; + while(!q.empty()) + { + int x = q.front(); + viz[x] = 0; + q.pop(); + for(unsigned i=0; i +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + vector >::iterator it = TT[node].begin(); + + while(it != TT[node].end()) + + { + + pair neigh = *it; + + if(dist[neigh.first] - 1e-8 > dist[node] + neigh.second) + + { + + dist[neigh.first] = dist[node] + neigh.second; + + paths[neigh.first] = paths[node]; + + if(inQueue[neigh.first] == 0) + + { + + Q.push(neigh.first); + + inQueue[neigh.first] = 1; + + } + + } + + else + + { + + if(dist[neigh.first] + 1e-8 >= dist[node] + neigh.second) + + { + + paths[neigh.first] += paths[node]; + + paths[neigh.first] %= 104659 ; + + } + + } + + + + it++; + + } + + + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/650aa631-b127-4611-80a1-4eb1639878a8.cpp b/distmin/bellman/650aa631-b127-4611-80a1-4eb1639878a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d043fb5cad79eaf4ca43f04f8a75f0514aeddbd2 --- /dev/null +++ b/distmin/bellman/650aa631-b127-4611-80a1-4eb1639878a8.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("dmin.in"); +ofstream fout("dmin.out"); + +int n, m; +typedef pair nod; +vector d(1505, 1 << 30); +vector a[1505]; +vector fr(1505, 0); +vector viz(1505); +queue q; + +int main() +{ + fin>>n>>m; + while(m--) + { + int x, second; + double first, c2; + fin>>x>>second>>first; + c2 = log10(first); + a[x].push_back(nod(c2, second)); + a[second].push_back(nod(c2, x)); + } + + q.push(1); + d[1] = 0; + fr[1] = 1; + viz[1] = 1; + while(!q.empty()) + { + int x = q.front(); + viz[x] = 0; + q.pop(); + for(unsigned i=0; i +#include +#include +#include +#include + + + + + + + +using namespace std; + +ifstream first("dmin.in"); +ofstream g("dmin.out"); + +int n, m, nr[1510], vz[1510]; +double d[1510]; + +struct muchie +{ + int nod; + double cost; +}; + +vector a[1510]; + +set< pair > second; + +void Citeste() +{ + int i, x, y; + muchie r; + + first>>n>>m; + + for (i=1; i<=m; ++i) + { + first>>x>>y>>r.cost; + + r.nod=y; a[x].push_back(r); + r.nod=x; a[y].push_back(r); + } +} + +void Initializeaza() +{ + int i; + + for (i=1; i<=n; ++i) d[i]=2500000000000.0; + + d[1]=0.0; nr[1]=1; + + second.insert( make_pair(0.0, 1) ); +} + +void Solve() +{ + int i, gata; + double val; + muchie r; + pair pr; + set > :: iterator is; + + do + { + gata=0; + + while (!second.empty() && !gata) + { + is=second.begin(); + pr=*is; + if (!vz[pr.second]) gata=1; + second.erase(is); + } + + if (gata) + { + vz[pr.second]=1; + for (i=0; i0.0000000001) + { + d[r.nod]=val; + second.insert(make_pair(val, r.nod)); + nr[r.nod]=nr[pr.second]; + } + else + if (fabs(d[r.nod]-val)<0.0000000001) nr[r.nod]=(nr[r.nod]+nr[pr.second])%104659; + } + } + } + + }while (gata); + + for (i=2; i<=n; ++i) g< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/695f33a1-87fb-4896-a34f-b7969da5735d.cpp b/distmin/bellman/695f33a1-87fb-4896-a34f-b7969da5735d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7608b0b91d0f4b84589a887850862c8e01977eb8 --- /dev/null +++ b/distmin/bellman/695f33a1-87fb-4896-a34f-b7969da5735d.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505], sol[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now] - dpant[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now] - dpant[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + dpant[now] = dp[now]; + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/6ce0af41-8ca3-45da-a18c-1498af85d470.cpp b/distmin/bellman/6ce0af41-8ca3-45da-a18c-1498af85d470.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b68ba9b2b642443ce2a45231b3aa8eac9aaf8d6 --- /dev/null +++ b/distmin/bellman/6ce0af41-8ca3-45da-a18c-1498af85d470.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, nrd[1501]; + +double M[1501][1501], dmin[1501]; + +bool inq[1501]; + + + +queue Q; + + + +void citire() + +{ + + int a, b, t; + + fin >> n >> m; + + for(int i = 1; i <= n; i++) + + for(int j = 1; j <= n; j++) + + if(i != j) M[i][j] = 2000000000; + + while(m--) + + fin >> a >> b >> t, M[a][b] = M[b][a] = log(t); + +} + + + +void bellman_ford(int nod) + +{ + + Q.push(nod); + + inq[nod] = 1; + + for(int i = 1; i <= n; ++i) dmin[i] = 2000000000; + + dmin[1] = 0; + + nrd[1] = 1; + + while(!Q.empty()) + + { + + int x = Q.front(); + + Q.pop(); inq[x] = 0; + + for(int i = 1; i <= n; ++i){ + + if(x != i) + + if(M[x][i] != 2000000000 && dmin[i] > dmin[x] + M[x][i] + 0.0000000001){ + + dmin[i] = dmin[x] + M[x][i]; + + nrd[i] = nrd[x]; + + if(inq[i] == 0) Q.push(i), inq[i] = 1; + + } + + else if(fabs(dmin[i] - dmin[x] - M[x][i]) < 0.0000000001 && M[x][i] != 2000000000) nrd[i] = (nrd[i] % 104659 + nrd[x] % 104659) % 104659; + + } + + } + +} + + + +int main() + +{ + + citire(); + + bellman_ford(1); + + for(int i = 2; i <= n; i++) + + fout << nrd[i] << ' '; + + return 0; + +} diff --git a/distmin/bellman/6d815901-4895-4bc7-be9b-e14abf18cce0.cpp b/distmin/bellman/6d815901-4895-4bc7-be9b-e14abf18cce0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3c3cd36faeeea05f1589bf8a4d9deede6de89a1 --- /dev/null +++ b/distmin/bellman/6d815901-4895-4bc7-be9b-e14abf18cce0.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +vector < pair < int, double > > graf[1510]; + +queue < int > q; + +double c[1510]; + +int sol[1510], n, m, viz[1510]; + + + +inline double modul(double x) + +{ + + if(x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + double cost; + + int x, y, cs ,nod, nod2; + + f >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + f >> x >> y >> cs; + + double z = log(cs); + + graf[x].push_back(make_pair(y, z)); + + graf[y].push_back(make_pair(x, z)); + + } + + c[1] = 0; + + for(int i = 2; i <= n; ++i) c[i] = 0x3f3f3f3f; + + q.push(1); + + sol[1] = 1; + + viz[1] = 1; + + while(!q.empty()) + + { + + nod = q.front(); + + q.pop(); + + for(int i = 0; i < graf[nod].size(); ++i) + + { + + nod2 = graf[nod][i].first; + + cost = graf[nod][i].second; + + if(c[nod2] - 1e-8 > c[nod] + cost) + + { + + c[nod2] = c[nod] + cost; + + sol[nod2] = sol[nod]; + + if(!viz[nod2]) q.push(nod2), viz[nod2] = 1; + + } + + else if( modul( c[nod2] - c[nod] - cost ) <= 1e-8 ) + + { + + sol[nod2] = (sol[nod2] + sol[nod]) % 104659; + + } + + } + + } + + for(int i = 2; i <= n; ++i) g << sol[i] << ' '; + +} diff --git a/distmin/bellman/6f2ba433-cf40-42c7-abbb-1b435e2f1d9c.cpp b/distmin/bellman/6f2ba433-cf40-42c7-abbb-1b435e2f1d9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfed5cd80a159d8699d9cbc17a92414a4b2c5879 --- /dev/null +++ b/distmin/bellman/6f2ba433-cf40-42c7-abbb-1b435e2f1d9c.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/6fb6d878-b900-4d0e-810c-b50d357d04a7.cpp b/distmin/bellman/6fb6d878-b900-4d0e-810c-b50d357d04a7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2300223720c8fe457bba515ab208788bad8fbfff --- /dev/null +++ b/distmin/bellman/6fb6d878-b900-4d0e-810c-b50d357d04a7.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510], Q; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +bool Sortare(int a, int b) + +{ + + return dist[a] > dist[b]; + +} + + + +void Bellman_Ford() + +{ + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push_back(1); + + while(Q.size()) + + { + + x=Q[0]; + + pop_heap(Q.begin(), Q.end(), Sortare); + + Q.pop_back(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + for ( vector < pair < int , double > > :: iterator it = TT[node].begin() ; it != TT[node].end() ; ++it ) + + { + + int current = it->first ; + + if( dist[current] - 0.0000001 > dist[node] + it->second ) + + { + + dist[current] = dist[node] + it->second; + + paths[current] = paths[node]; + + if(inQueue[current] == 0) + + { + + Q.push(current); + + inQueue[current] = 1; + + } + + } + + else + + { + + if(dist[current] + 0.0000001 >= dist[node] + it->second ) + + { + + paths[current] += paths[node]; + + paths[current] %= 104659 ; + + } + + + + } + + } + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/750eee98-45fa-42c7-b69d-bde7b191e633.cpp b/distmin/bellman/750eee98-45fa-42c7-b69d-bde7b191e633.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8108bd620e352b4c7e98e64b77febcf48a852391 --- /dev/null +++ b/distmin/bellman/750eee98-45fa-42c7-b69d-bde7b191e633.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const double eps = 1e-10; + +const int NMAX = 1503; + +const int MOD = 104659; + +const int oo = 0x3f3f3f3f; + +bitset viz; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, x, y, r[NMAX]; + +double cost; + + + +vector d(NMAX, oo); + + + +inline bool egal(double a, double b) + +{ + + return (abs(a - b) <= eps); + +} + + + +vector < pair > g[NMAX]; + +queue q; + + + +void BellmanFord() + +{ + + q.push(1); + + d[1] = 0; + + r[1] = 1; + + while (!q.empty()) + + { + + int x = q.front(); + + q.pop(); + + viz[x] = 0; + + for (size_t i = 0; i < g[x].size(); ++i) + + { + + int nod = g[x][i].first; + + if (egal(d[x] + g[x][i].second, d[nod])) + + { + + r[nod] += r[x]; + + if (r[nod] > MOD) r[nod] -= MOD; + + } + + else if (d[x] + g[x][i].second < d[nod]) + + { + + d[nod] = d[x] + g[x][i].second; + + r[nod] = r[x]; + + if (viz[nod] == 0) + + { + + viz[nod] = 1; + + q.push(nod); + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> cost; + + cost = log(cost); + + g[x].push_back({ y, cost }); + + } + + BellmanFord(); + + for (int i = 2; i <= n; ++i) + + fout << r[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/76bc1728-3c2d-4b78-b003-9273d0952897.cpp b/distmin/bellman/76bc1728-3c2d-4b78-b003-9273d0952897.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b125e9c6b7bf8d2283052ce1aa9ec704810f1023 --- /dev/null +++ b/distmin/bellman/76bc1728-3c2d-4b78-b003-9273d0952897.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int MaxN = 1501; + +const double eps = 1e-10; + +const int Mod = 104659; + +const double InF = 100000000; + + + +const char InFile[] = "dmin.in"; + +const char OutFile[] = "dmin.out"; + + + +int N,M,sol[MaxN]; + +double d[MaxN]; + +bool inQ[MaxN]; + +vector< pair > G[MaxN]; + +queue Q; + + + +double modul( double x ) + +{ + + return x > 0 ? x : -x; + +} + + + +void BellMan_Ford() + +{ + + int nod; + + vector< pair >::iterator it,iend; + + for( nod = 1 ; nod <= N ; ++nod ) + + { + + d[nod] = InF; + + sol[nod] = 0; + + inQ[nod] = false; + + } + + d[1] = 0; + + sol[1] = 1; + + Q.push(1); + + inQ[1] = true; + + while( !Q.empty() ) + + { + + nod = Q.front(); + + Q.pop(); + + inQ[nod] = false; + + iend = G[nod].end(); + + it = G[nod].begin(); + + for( ; it != iend ; ++it ) + + { + + if( modul(d[it->first] - d[nod] - it-> second) < eps ) + + { + + sol[it->first] = ( sol[it->first]+sol[nod] ) % Mod; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + if( d[it->first] - d[nod] - it->second > eps ) + + { + + d[it->first] = d[nod] + it->second; + + sol[it->first] = sol[nod]; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + ifstream fin( InFile ); + + ofstream fout( OutFile ); + + fin >> N >> M; + + int i,x,y; + + double z; + + for( i = 0 ; i < M ; ++i ) + + { + + fin >> x >> y >> z; + + G[x].push_back(make_pair(y,log(z))); + + G[y].push_back(make_pair(x,log(z))); + + } + + BellMan_Ford(); + + for( i = 2 ; i <= N ; ++i ) + + fout << sol[i] << ' '; + + fout << '\n'; + + fin.close();fout.close(); + + return 0; + +} diff --git a/distmin/bellman/76d874a8-d01e-49b9-82ff-622bacccdd9a.cpp b/distmin/bellman/76d874a8-d01e-49b9-82ff-622bacccdd9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68fa80c948e8ba4b3afd3ab38b7aefa8c566e788 --- /dev/null +++ b/distmin/bellman/76d874a8-d01e-49b9-82ff-622bacccdd9a.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) + + abs = -abs; + + if(abs < 0.000000001) + + return 0; + + if(x > y) + + return 1; + + if(x < y) + + return -1; + +} + +void BellmanFord() { + + queue Q; + + vector inQueue; inQueue.assign(N + 1, 0); + + dmin[1] = 0; + + nrd[1] = 1; + + Q.push(1); + + inQueue[1] = 1; + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { + + inQueue[neigh] = 1; Q.push(neigh); + + } + + } + + } + + } + +} + + + +int main() + +{ + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + + BellmanFord(); + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + + + + + + return 0; + +} diff --git a/distmin/bellman/76f65e3f-02f9-4532-b8a1-134de1c3c229.cpp b/distmin/bellman/76f65e3f-02f9-4532-b8a1-134de1c3c229.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb6c651ca3f686d6c8a976d79371775663a86a23 --- /dev/null +++ b/distmin/bellman/76f65e3f-02f9-4532-b8a1-134de1c3c229.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + +const double eps=1e-10; + +const double INFINIT=9999999999; + +vector< pair > Graf[1505]; + +queue Q; + +int N,M,x,y,z,D[1505]; + +bool InCoada[1505]; + +double Dmin[1505]; + + + +void Read() + +{ + + ifstream g("dmin.in"); + + g>>N>>M; + + for(int i=1;i<=M;i++) + + { + + g>>x>>y>>z; + + double z1=log(z); + + Graf[x].push_back(make_pair(y,z1)); + + Graf[y].push_back(make_pair(x,z1)); + + } + +} + + + +void BellManFord() + +{ + + for(int i=1;i<=N;i++) + + Dmin[i]=INFINIT; + + + + Q.push(1); + + D[1]=1;Dmin[1]=0.0;InCoada[1]=true; + + while(!Q.empty()) + + { + + int nod=Q.front(); + + Q.pop(); + + InCoada[nod]=false; + + for(vector >::iterator it=Graf[nod].begin();it!=Graf[nod].end();it++) + + { + + int vecin=it->first; + + double cost=it->second; + + if(Dmin[nod]+cost-Dmin[vecin]< -eps) + + { + + D[vecin]=D[nod]; + + Dmin[vecin]=Dmin[nod]+cost; + + if(!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + else + + { + + if(fabs(Dmin[nod]+cost-Dmin[vecin])<=eps) + + { + + D[vecin]=(D[vecin]+D[nod])%104659; + + if (!InCoada[vecin]) + + { + + Q.push(vecin); + + InCoada[vecin]=true; + + } + + } + + } + + } + + } + +} + + + +void Write() + +{ + + ofstream f("dmin.out"); + + for(int i=2;i<=N;i++) + + f< +#include +#include +using namespace std; + +int n, m, i, x, y, nod, vecin, cost, j; + +int viz[1505], num[1505], h[1505], poz[1505]; + +double d[1505], cost2; + +vector< pair > v[1505]; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +double modul(double x){ + + if(x > 0){ + + return x; + + } + + return -x; + +} + +void upd(int pos){ + + int c = pos, p = c / 2; + + while(p > 0 && d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + c = p; + + p = c / 2; + + } + +} + +void elim(){ + + int p = 1, c = 2; + + while(c <= n){ + + if(c + 1 <= n && d[ h[c] ] > d[ h[c + 1] ]){ + + c++; + + } + + if(d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + p = c; + + c = 2 * p; + + } + + else{ + + break; + + } + + } + +} + +int main(){ + + fin>> n >> m; + + for(i = 1; i <= m; i++){ + + fin>> x >> y >> cost; + + cost2 = log(cost); + + v[x].push_back( make_pair(y, cost2) ); + + v[y].push_back( make_pair(x, cost2) ); + + } + + for(i = 1; i <= n; i++){ + + d[i] = 1000000000; + + h[i] = poz[i] = i; + + } + + d[1] = 0; + + num[1] = 1; + + for(i = 1; i <= n; i++){ + + nod = h[1]; + + for(j = 0; j < v[nod].size(); j++){ + + vecin = v[nod][j].first; + + if(viz[vecin] == 0){ + + if(modul(d[vecin] - d[nod] - v[nod][j].second) <= 0.000000001){ + + num[vecin] += num[nod]; + + if(num[vecin] >= 104659){ + + num[vecin] -= 104659; + + } + + } + + else{ + + if(d[nod] + v[nod][j].second < d[vecin]){ + + d[vecin] = d[nod] + v[nod][j].second; + + upd(poz[vecin]); + + num[vecin] = num[nod]; + + } + + } + + } + + } + + viz[nod] = 1; + + d[nod] = 1000000000; + + elim(); + + } + + for(i = 2; i <= n; i++){ + + fout<< num[i] <<" "; + + } + + return 0; + +} diff --git a/distmin/bellman/789a87a3-977d-4784-a67e-4897a908d137.cpp b/distmin/bellman/789a87a3-977d-4784-a67e-4897a908d137.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/789a87a3-977d-4784-a67e-4897a908d137.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/78f32410-e0bb-461d-8391-f73ebb2361db.cpp b/distmin/bellman/78f32410-e0bb-461d-8391-f73ebb2361db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c1a9f04260f9289ba795a3fc7e480c58914a5e5 --- /dev/null +++ b/distmin/bellman/78f32410-e0bb-461d-8391-f73ebb2361db.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +const int nmax = 1510; + + + +struct el{ + +int a; + +double cost; + + + + el(int aa,double c){ + + a = aa; + + cost = c; + + } + +}; + +vectorG[nmax]; + +queueq; + +int N,M,nr[nmax]; + +double dp[nmax]; + +bool ok[nmax]; + + + +inline void BFS(){ + + int nod; + + + + for(int i = 2; i <= N; ++i)dp[i] = 0x3f3f3f3f; + + + + nr[1] = 1; + + q.push(1); + + ok[1] = true; + + + + while(!q.empty()){ + + nod = q.front(); + + ok[nod] = false; + + q.pop(); + + + + for(auto it: G[nod]){ + + if(dp[it.a]-it.cost-dp[nod] >= 1e-10){ + + + + dp[it.a] = dp[nod] + it.cost; + + nr[it.a] = nr[nod]; + + if(!ok[it.a]){ + + ok[it.a] = true; + + q.push(it.a); + + } + + } + + else if(abs(dp[nod]-dp[it.a]+it.cost) < 1e-10){ + + nr[it.a]+=nr[nod]; + + if(nr[it.a]>=104659)nr[it.a]-=104659; + + } + + } + + } + + + +} + +int main(){ + + int i,x,y,c; + + freopen ("dmin.in","r",stdin); + + freopen ("dmin.out","w",stdout); + + scanf("%d %d\n",&N,&M); + + for(i = 1; i <= M; ++i){ + + scanf("%d %d %d\n",&x,&y,&c); + + G[x].push_back(el(y,log10(c))); + + G[y].push_back(el(x,log10(c))); + + } + + BFS(); + + + + for(i = 2; i <= N; ++i) + + cout << nr[i] <<' '; + + + + return 0; + +} diff --git a/distmin/bellman/7d785215-d73c-451c-a6e7-6795d6e17cc0.cpp b/distmin/bellman/7d785215-d73c-451c-a6e7-6795d6e17cc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba1b0ad2e9ef3a00a2d6ef96135e9d5e1c8bfe86 --- /dev/null +++ b/distmin/bellman/7d785215-d73c-451c-a6e7-6795d6e17cc0.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +int dant[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + dant[vecin] = 0; + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod] - dant[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + dant[nod] = d[nod]; + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/7e4805c3-adfc-4094-907e-a0b3e69b80c6.cpp b/distmin/bellman/7e4805c3-adfc-4094-907e-a0b3e69b80c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..925246e7692e731f9efb4d86da1cce9802a9c98c --- /dev/null +++ b/distmin/bellman/7e4805c3-adfc-4094-907e-a0b3e69b80c6.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +int n, m; +int cate[1550], used[1550]; +double best[1550]; +const double EPS = 1e-9; +struct leg +{ + int y; + double c; + leg(int y = 0, double c = 0) : y(y), c(c) { } + bool operator ()(int a, int b) + { + return best[a] > best[b]; + } +}; +bool eq(double a, double b) +{ + return (a-b) >= -EPS && (a-b) <= EPS; +} +vector graf[1550]; +void citire() +{ + scanf("%d %d", &n, &m); + for (int i = 1; i <= m; i++) { + int x, y, rc; + scanf("%d %d %d", &x, &y, &rc); + double c = log(1.0*rc); + graf[x].push_back(leg(y, c)); + graf[y].push_back(leg(x, c)); + } +} +priority_queue, leg> heap; +void solve() +{ + for (int i = 1; i <= n; i++) best[i] = 5e100; + best[1] = 0, cate[1] = 1; + heap.push(1); + while (!heap.empty()) { + int top = heap.top(); + heap.pop(); + if (used[top]) + continue; + used[top] = 1; + for (auto v : graf[top]) { + if (eq(best[top] + v.c, best[v.y])) + cate[v.y] = (cate[v.y] + cate[top]) % 104659; + else if (best[top] + v.c < best[v.y]) { + best[v.y] = best[top] + v.c; + cate[v.y] = cate[top]; + heap.push(v.y); + } + } + } +} + +void afisare() +{ + for (int i = 2; i <= n; i++) + printf("%d ", cate[i]); +} + +int main() +{ + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + + citire(); + solve(); + afisare(); + + return 0; +} diff --git a/distmin/bellman/7e9917b3-2440-4869-b57c-34796ea9532a.cpp b/distmin/bellman/7e9917b3-2440-4869-b57c-34796ea9532a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be90d33072c5dc8958c7f09424d1660e16bb8aa6 --- /dev/null +++ b/distmin/bellman/7e9917b3-2440-4869-b57c-34796ea9532a.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +class nod + +{ public: type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + + + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +#include +#include + + +const int kMaxDim = 1505; + +const int kModulo = 104659; + +const double eps = 1e-8; + +const double inf = 1e10; + + + +inline double Abs(const double& a) { + + return (a > 0 ? a : -a); + +} + + + +inline int Compare(const double& a, const double& b) { + + if (Abs(a - b) < eps) + + return 0; + + return (a < b ? -1 : 1); + +} + + + +struct Data { + + int node; + + double cost; + +}; + + + +struct Functor { + + bool operator()(const Data& a, const Data& b) { + + return Compare(a.cost, b.cost) > 0; + + } + +}; + + + +int nodeCount, edgeCount; + +std::vector< Data > edges[kMaxDim]; + +double minDist[kMaxDim]; + +int minDistCount[kMaxDim]; + + + +std::priority_queue< Data, std::vector< Data >, Functor > heap; + + + +int main() { + + std::ifstream inputFile("dmin.in"); + + std::ofstream outputFile("dmin.out"); + + + + inputFile >> nodeCount >> edgeCount; + + for (int i = 1; i <= edgeCount; ++i) { + + int first, second; + + double cost; + + inputFile >> first >> second >> cost; + + cost = log(cost); + + + + edges[first].push_back({second, cost}); + + edges[second].push_back({first, cost}); + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + minDist[i] = inf; + + minDistCount[1] = 1; + + heap.push({1, 0}); + + + + while (!heap.empty()) { + + while (!heap.empty() && Compare(minDist[heap.top().node], heap.top().cost) < 0) + + heap.pop(); + + if (heap.empty()) + + break; + + + + int node = heap.top().node; + + double cost = heap.top().cost; + + heap.pop(); + + + + for (Data& edge : edges[node]) { + + if (Compare(minDist[edge.node], cost + edge.cost) > 0) { + + minDist[edge.node] = cost + edge.cost; + + minDistCount[edge.node] = minDistCount[node]; + + heap.push({edge.node, minDist[edge.node]}); + + } + + else if (Compare(minDist[edge.node], cost + edge.cost) == 0) { + + minDistCount[edge.node] += minDistCount[node]; + + minDistCount[edge.node] %= kModulo; + + } + + } + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + outputFile << minDistCount[i] << ' '; + + outputFile << '\n'; + + + + inputFile.close(); + + outputFile.close(); + + + + return 0; + +} diff --git a/distmin/bellman/84a486c2-7ab9-4d20-bb1a-7a8feb8f8b46.cpp b/distmin/bellman/84a486c2-7ab9-4d20-bb1a-7a8feb8f8b46.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/84a486c2-7ab9-4d20-bb1a-7a8feb8f8b46.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/85a1e864-03cc-4363-a8fc-f03ef418238a.cpp b/distmin/bellman/85a1e864-03cc-4363-a8fc-f03ef418238a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..394baa7f5e4ebf12e4081f63548c62e6a75b17ab --- /dev/null +++ b/distmin/bellman/85a1e864-03cc-4363-a8fc-f03ef418238a.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/85f93dd2-c51a-46b0-9319-4123a16a9143.cpp b/distmin/bellman/85f93dd2-c51a-46b0-9319-4123a16a9143.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ad7b9a7f7ea71ac877a833247236cf08f72dfa3 --- /dev/null +++ b/distmin/bellman/85f93dd2-c51a-46b0-9319-4123a16a9143.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct Muchie{ + + int vecin; + + double cost; + +}; + + + +queue Q; + +vector A[1501]; + +int n, m, nrd[1501], inq[1501]; + +double dmin[1501]; + + + +int main() + +{ + + int a, b, i; + + double c; + + Muchie T; + + + + + + f>>n>>m; + + for(i = 1; i <= m; i++) { + + f>>a>>b>>c; + + c = log(c); + + T.vecin = b; T.cost = c; + + A[a].push_back(T); + + T.vecin = a; T.cost = c; + + A[b].push_back(T); + + } + + + + + + dmin[1] = 0; + + for(i = 2; i <= n; i++) dmin[i] = 1000000001; + + nrd[1] = 1; + + for(i = 2; i <= n; i++) nrd[i] = 0; + + + + + + Q.push(1); inq[1] = 1; + + while(!Q.empty()) { + + int x = Q.front(); Q.pop(); inq[x] = 0; + + for(i = 0; i < A[x].size(); i++) { + + int vecin = A[x][i].vecin; + + double cost = A[x][i].cost; + + double dif = dmin[vecin] - dmin[x] - cost; + + if(dif < 0) dif = -dif; + + if(dif < 0.0000000001) + + nrd[vecin] = (nrd[vecin] + nrd[x]) % 104659; + + else + + if(dmin[vecin] > dmin[x] + cost) { + + dmin[vecin] = dmin[x] + cost; + + nrd[vecin] = nrd[x]; + + if(!inq[vecin]) { + + Q.push(vecin); + + inq[vecin] = 1; + + } + + } + + } + + } + + + + for(i = 2; i <= n; i++) g< +#include +#include +#include +#include + + +using namespace std; + + + +fstream f("dmin.in", ios::in); + +fstream g("dmin.out", ios::out); + + + +const int nmax = 1505; + +const double INF = 100000.0; + +const double eps = 0.00001; + +const int modulo = 104659; + + + +vector < pair > a[nmax]; + + + +double c, cost; + +double d[nmax]; + +int cnt[nmax], viz[nmax]; + +int n, m, x, y, i; + + + +void read() + +{ + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + cost = log(c); + + a[x].push_back(make_pair(y, cost)); + + a[y].push_back(make_pair(x, cost)); + + } + +} + + + +void bellman() + +{ + + queue q; + + double dist; + + int nc; + + for (i = 2; i <= n; i++) d[i] = INF; + + q.push(1); + + d[1] = 0; + + viz[1] = 1; + + cnt[1] = 1; + + while (!q.empty()) + + { + + nc = q.front(); q.pop(); + + dist = d[nc]; + + viz[nc] = 0; + + for (vector< pair >::iterator it = a[nc].begin(); it != a[nc].end(); ++it) + + { + + if (fabs(dist + it->second - d[it->first]) < eps) + + { + + cnt[it->first] = (cnt[it->first] + cnt[nc]) % modulo; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + else if (dist + it->second < d[it->first]) + + { + + d[it->first] = dist + it->second; + + cnt[it->first] = cnt[nc]; + + if (!viz[it->first]) + + { + + q.push(it->first); + + viz[it->first] = 1; + + } + + } + + } + + } + +} + + + +int main() + +{ + + read(); + + bellman(); + + for (i = 2; i <= n; i++) g << cnt[i] << " "; + + return 0; + +} diff --git a/distmin/bellman/8914ec1d-0eee-4f76-becd-a4a67a860be4.cpp b/distmin/bellman/8914ec1d-0eee-4f76-becd-a4a67a860be4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fb686de9c127704d9e7e2b3d307eaabff7c44b9 --- /dev/null +++ b/distmin/bellman/8914ec1d-0eee-4f76-becd-a4a67a860be4.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int N, M, sol[1510], viz[1510]; +double dmin[1510]; +vector > G[1510]; + +void BellmanFord() { + int i, p; + queue Q; + bool InQueue[1510]; + for(i=1; i<=N; i++) { + InQueue[i] = false; + dmin[i] = 99999999; + } + InQueue[1] = true; + Q.push(1); + dmin[1] = 0; sol[1] = 1; + while(!Q.empty()) { + p = Q.front(); Q.pop(); + InQueue[p] = false; + for(vector >::iterator it=G[p].begin(); it!=G[p].end(); it++) { + if(0.00001 < dmin[(*it).first] - dmin[p] - (*it).second) { + dmin[(*it).first] = dmin[p] + (*it).second; + sol[(*it).first] = sol[p]; + if(!InQueue[(*it).first]) { + InQueue[(*it).first] = 1; + Q.push((*it).first); + } + } + else if(dmin[p] + (*it).second - dmin[(*it).first] <= 0.00001) { + sol[(*it).first] += sol[p]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + FILE *f1=fopen("dmin.in", "r"), *f2=fopen("dmin.out", "w"); + int i, j, p, q; + + fscanf(f1, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f1, "%d %d %d\n", &p, &q, &j); + double cost = (double)log(j); + + G[p].push_back( make_pair(q, cost) ); + G[q].push_back( make_pair(p, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(f2, "%d ", sol[i]); + } + + fclose(f1); fclose(f2); + return 0; +} diff --git a/distmin/bellman/899cf07d-87cf-499e-943a-944b37684911.cpp b/distmin/bellman/899cf07d-87cf-499e-943a-944b37684911.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3662256add92df9c3714ff8271dff60686e09fe --- /dev/null +++ b/distmin/bellman/899cf07d-87cf-499e-943a-944b37684911.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include + + + + + +using namespace std; + +FILE *f=fopen("dmin.in", "r"); + +FILE *g=fopen("dmin.out", "w"); + + +int N, M, sol[1510]; +double D[1510]; +vector< pair < int, double > > G[1510]; +queue Q; +bool viz[1510]; +void BellmanFord() { + int i, x; + + for(i=1; i<=N; i++) { + viz[i] = false; + D[i] = 99999999; + } + viz[1] = true; + Q.push(1); + D[1] = 0; + + sol[1] = 1; + while(!Q.empty()) { + + + x=Q.front(); + + Q.pop(); + viz[x] = false; + for(vector< pair < int, double > >::iterator it=G[x].begin(); it!=G[x].end(); it++) { + if(0.00000001 < D[(*it).first] -D[x] - (*it).second) { + D[(*it).first] = D[x] + (*it).second; + sol[(*it).first] = sol[x]; + if(!viz[(*it).first]) { + viz[(*it).first] = 1; + Q.push((*it).first); + } + } + else + + if(D[x] + (*it).second - D[(*it).first] <= 0.00000001) { + sol[(*it).first] += sol[x]; + sol[(*it).first] %= 104659; + } + } + } +} + +int main() { + + int i, j, x, y; + + fscanf(f, "%d %d\n", &N, &M); + for(i=1; i<=M; i++) { + fscanf(f, "%d %d %d\n", &x, &y, &j); + double cost = (double)log(j); + + G[x].push_back( make_pair(y, cost) ); + G[y].push_back( make_pair(x, cost) ); + } + + BellmanFord(); + + for(i=2; i<=N; i++) { + fprintf(g, "%d ", sol[i]); + } + + fclose(f); + + fclose(g); + return 0; +} diff --git a/distmin/bellman/8ae937b6-0015-4bca-8785-dbffc9a8c5d0.cpp b/distmin/bellman/8ae937b6-0015-4bca-8785-dbffc9a8c5d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..394baa7f5e4ebf12e4081f63548c62e6a75b17ab --- /dev/null +++ b/distmin/bellman/8ae937b6-0015-4bca-8785-dbffc9a8c5d0.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/8b6d98d5-a3d3-4b82-8465-946dbc20379f.cpp b/distmin/bellman/8b6d98d5-a3d3-4b82-8465-946dbc20379f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1f8437db5e6e44af045865f9a2278f1477769ce --- /dev/null +++ b/distmin/bellman/8b6d98d5-a3d3-4b82-8465-946dbc20379f.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +vector > G[1605]; + +vector daddy[1605]; + +queue Q; + +bitset<1605> inQ; + + + +int N,M; + +double dist[1605]; + +int DP[1605]; + + + +void read() + +{ + + int a,b,c; + + scanf("%d%d",&N,&M); + + for(int i = 1; i <= M; ++i){ + + scanf("%d%d%d",&a,&b,&c); + + G[a].push_back(make_pair(log2((double)c),b)); + + G[b].push_back(make_pair(log2((double)c),a)); + + } + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + for(int i = 2; i <= N; ++i) dist[i] = 0x3f3f3f3f; + + dist[1] = 0; + + int k; + + while(!Q.empty()) + + { + + k = Q.front();Q.pop();inQ[k] = 0; + + for(vector >::iterator it = G[k].begin(); it != G[k].end(); ++it) + + { + + if(dist[it->second] > dist[k] + it->first + 0.00000001) + + { + + dist[it->second] = dist[k] + it->first; + + daddy[it->second].clear(); + + daddy[it->second].push_back(k); + + inQ[it->second] = 1; + + Q.push(it->second); + + continue; + + } + + if(dist[it->second] - dist[k] - it->first + 0.00000001 >= 0 && + + dist[it->second] - dist[k] - it->first <= 0.0001 ) + + daddy[it->second].push_back(k); + + } + + } + +} + + + +int dynamic(int k) + +{ + + if(DP[k])return DP[k]; + + int sum = 0; + + for(vector::iterator it = daddy[k].begin(); it != daddy[k].end(); ++it) + + sum = (sum%104659 + dynamic(*it)%104659) % 104659; + + DP[k] = sum; + + return DP[k] % 104659; + +} + +void prepare() + +{ + + for(int i = 1; i <= N; ++i) + + { + + vector::iterator it = unique(daddy[i].begin(),daddy[i].end()); + + daddy[i].resize(distance(daddy[i].begin(),it)); + + } + +} + + + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + read(); + + bellman_ford(); + + DP[1] = 1; + + prepare(); + + for(int i = 2; i <= N; ++i) + + printf("%d ",dynamic(i)); + + + + return 0; + +} diff --git a/distmin/bellman/8c2fe99e-9500-4672-8fac-a2a5e7664cac.cpp b/distmin/bellman/8c2fe99e-9500-4672-8fac-a2a5e7664cac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d94bbe5f4a2fc056def6fddba1ac6bd905168dc --- /dev/null +++ b/distmin/bellman/8c2fe99e-9500-4672-8fac-a2a5e7664cac.cpp @@ -0,0 +1,158 @@ +#include +FILE *fin = freopen("dmin.in", "r", stdin); + +FILE *fout = freopen("dmin.out", "w", stdout); + + + +using namespace std; + +int V, E, sol[1502]; + +double D[1502]; + +struct comp + +{ + + bool operator() (const pair &a, const pair &b) + + { + + if(fabs(a.second - b.second) < 0.0000000001) + + return 0; + + else return a.second > b.second + 0.0000000001; + + } + +}; + +priority_queue , vector >, comp> Q; + +vector > G[1502]; + +bool F[1502]; + +void read() + +{ + + int x, y; + + double z; + + scanf("%d %d", &V, &E); + + while(E --) + + { + + scanf("%d %d %lf", &x, &y, &z); + + z = log(z); + + G[x].push_back(pair (y, z)); + + G[y].push_back(pair (x, z)); + + } + + for(int i = 2; i <= V; ++ i) + + D[i] = 2000000000.0; + + Q.push(pair (1, 0.0)); + + sol[1] = 1; + +} + +void solve() + +{ + + int x, y; + + double z; + + while(!Q.empty()) + + { + + x = Q.top().first; + + + + Q.pop(); + + int sz = G[x].size(); + + if (F[x]) + + continue; + + + + for(int i = 0; i < sz; ++ i) + + { + + y = G[x][i].first; + + z = G[x][i].second; + + if (!F[y]) + + { + + if(D[y] > D[x] + z + 0.0000000001) + + { + + D[y] = D[x] + z; + + sol[y] = sol[x]; + + Q.push(pair (y, D[y])); + + } + + else if(fabs(D[y] - D[x] - z) < 0.0000000001) + + sol[y] = (sol[y] + sol[x]) % 104659; + + } + + } + + F[x] = 1; + + } + +} + +void write() + +{ + + for(int i = 2; i <= V; ++ i) + + printf("%d ", sol[i]); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/distmin/bellman/8e745542-aa4c-4f23-88cd-7e9eef8404b9.cpp b/distmin/bellman/8e745542-aa4c-4f23-88cd-7e9eef8404b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fda19e7ae5b9bafc86676417de59de213ea51331 --- /dev/null +++ b/distmin/bellman/8e745542-aa4c-4f23-88cd-7e9eef8404b9.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("dmin.in"); + +ofstream cout("dmin.out"); + + + +const int MAXN = 1505, + + oo = ((1<<31)-1), + + MOD = 104659; + + + +vector< pair > Graph[MAXN]; + +vector< double > Distance(MAXN, (double)oo); + +bitset< MAXN > Used; + +queue< int > Q; + +map NRoads; + +int N, M; + + + +const inline double Absolute(double a) + +{ + + if(a > 0) + + return a; + + return -a; + +} + +const inline bool isequal(double a, double b) + +{ + + if( Absolute(a-b) <= 1e-10 ) + + return true; + + return false; + +} + + + +void Read_Data() + +{ + + cin >> N >> M; + + for(int i = 1 ; i <= M ; ++ i) + + { + + int X, Y, Z; + + cin >> X >> Y >> Z; + + Graph[X].push_back(make_pair(Y, log(Z))); + + } + + cin.close(); + +} + + + +void BellmanFord() + +{ + + for(Q.push(1) , Distance[1] = 0, NRoads[1] = 1, Used[1] = true; !Q.empty(); Q.pop() ) + + { + + int node = Q.front(); + + Used[node] = false; + + for(vector > :: iterator it = Graph[node].begin(), fin = Graph[node].end(); it != fin ; ++ it) + + {if(isequal(Distance[node] + it->second, Distance[it->first])) + + { + + NRoads[it->first] += NRoads[node]; + + if(NRoads[it->first] > MOD) + + NRoads[it->first] -= MOD; + + } + + else if(Distance[it->first] > it->second + Distance[node]) { + + Distance[it->first] = it->second + Distance[node]; + + NRoads[it->first] = NRoads[node]; + + if( !Used[it->first] ) + + { + + Used[it->first] = true; + + Q.push(it->first); + + } + + }} + + } + + + +} + +void Write_Data() + +{ + + for(int i = 2 ; i <= N; ++ i) + + cout << NRoads[i] << " "; + + cout << "\n"; + + cout.close(); + +} + +int main() + +{ + + Read_Data(); + + BellmanFord(); + + Write_Data(); + + return 0; + +} diff --git a/distmin/bellman/8f7136e3-ca91-4970-aece-0c09fd6b9272.cpp b/distmin/bellman/8f7136e3-ca91-4970-aece-0c09fd6b9272.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cb6b385103510cc63c750bed1a579c4e7d9b0d1 --- /dev/null +++ b/distmin/bellman/8f7136e3-ca91-4970-aece-0c09fd6b9272.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ofstream out("dmin.out"); +vector > G[1510]; + +typedef vector > ::iterator IT; + +queueQ; + +bitset<1510>uz; + + + +int drum[1510],n,m; + +double d[1510],EPS=0.0000000001,c; + + + +void read(); + +void BMF(); + +void write(); + + + +int main() + +{ + + read(); + + BMF(); + + write(); + + return 0; + +} + + + +void read() + +{ + + ifstream in("dmin.in"); + + in>>n>>m; + + for(int x,y,c ; m ; --m) + + { + + in>>x>>y>>c; + + G[x].push_back(make_pair(y,log( (double) c) ) ); + + G[y].push_back(make_pair(x,log( (double) c) ) ); + + } + +} + + + +void BMF() + +{ + + for(int i=1;i<=n;i++) + + d[i]=0x3f3f3f3f; + + d[1]=0; + + drum[1]=1; + + Q.push(1); + + uz[1]=1; + + int k; + + for(; Q.size(); Q.pop()) + + { + + k=Q.front(); + + for(IT I=G[k].begin();I!=G[k].end();++I) + + { + + int val=I->first; + + double cost=I->second; + + if( fabs( d[val] - d[k] - cost ) < EPS ) + + drum[val]= (drum[val] + drum[k] ) %104659; + + else + + if( d[val] - d[k] - cost > EPS) + + { + + if(!uz[val]) + + { + + Q.push(val); + + uz[val]=1; + + } + + d[val]=d[k] + cost; + + drum[val]= drum[k] ; + + } + + } + + uz[k]=0; + + } + +} + + + +void write() + +{ + + for(int i=2;i<=n;i++) + + out< +#include +#include +#include +#include +#include +using namespace std; +ifstream fin ( "dmin.in" ); + +ofstream fout ( "dmin.out" ); + + + + + +int N, M, Aport[1502]; + +double Dist[1502]; + +vector < pair < int, double > > G[1502]; + +bitset < 1502 > inQ; + +queue < int > Q; + + + +void BellmanFord(){ + + + + for ( int i = 2; i <= N; ++i ) + + Dist[i] = 0x3f3f3f3f; + + + + Aport[1] = 1; + + Q.push ( 1 ); + + inQ[1] = 1; + + + + vector < pair < int, double > > :: iterator it; + + while ( !Q.empty() ){ + + int nod = Q.front(); + + Q.pop(); + + inQ[nod] = 0; + + + + for ( it = G[nod].begin(); it != G[nod].end(); ++it ){ + + if ( Dist[it->first] - (Dist[nod] + it->second) >= 1e-10 ){ + + Dist[it->first] = Dist[nod] + it->second; + + + + Aport[it->first] = Aport[nod]; + + + + if ( !inQ[it->first] ){ + + Q.push ( it->first ); + + inQ[it->first] = 1; + + } + + } + + else if ( abs ( (Dist[nod] + it->second) - Dist[it->first] ) < 1e-10 ){ + + Aport[it->first] += Aport[nod]; + + Aport[it->first] %= 104659; + + } + + } + + } + +} + + + +int main(){ + + + + int x, y, c; + + + + fin >> N >> M; + + + + while ( M-- ){ + + fin >> x >> y >> c; + + G[x].push_back ( make_pair ( y, log10(c) ) ); + + G[y].push_back ( make_pair ( x, log10(c) ) ); + + } + + + + BellmanFord(); + + + + for ( int i = 2; i <= N; ++i ) + + fout << Aport[i] << " "; + + + + return 0; + +} diff --git a/distmin/bellman/94b17e77-c4c6-4881-803a-9ac3cb0ab166.cpp b/distmin/bellman/94b17e77-c4c6-4881-803a-9ac3cb0ab166.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d13dfeaa4cc915a0dca8b9e07644ef981548cd9 --- /dev/null +++ b/distmin/bellman/94b17e77-c4c6-4881-803a-9ac3cb0ab166.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + for ( vector < pair < int , double > > :: iterator it = TT[node].begin() ; it != TT[node].end() ; ++it ) + + { + + int current = it->first ; + + if( dist[current] - 0.0000001 > dist[node] + it->second ) + + { + + dist[current] = dist[node] + it->second; + + paths[current] = paths[node]; + + if(inQueue[current] == 0) + + { + + Q.push(current); + + inQueue[current] = 1; + + } + + } + + else + + { + + if(dist[current] + 0.0000001 >= dist[node] + it->second ) + + { + + paths[current] += paths[node]; + + paths[current] %= 104659 ; + + } + + + + } + + } + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/9646d86a-4d32-44eb-ba71-9b81a33582bb.cpp b/distmin/bellman/9646d86a-4d32-44eb-ba71-9b81a33582bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c89a1979e423aa05985204ab3a2f55f4356a997b --- /dev/null +++ b/distmin/bellman/9646d86a-4d32-44eb-ba71-9b81a33582bb.cpp @@ -0,0 +1,219 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +const int N=2501,M=1000001; + + + +int n,m,s[N],q[M],p,var[N],u; + +double c[N]; + +bool viz[N]; + +struct xy{ + + int to; + + double c; + +}; + +vector v[N]; + + + +void read() + +{ + + int x,y,z; + + xy a; + + scanf("%d%d",&n,&m); + + for( int i=1 ; i<=m ; ++i ) + + { + + scanf("%d%d%d",&x,&y,&z); + + a.c=log10((double)z); + + a.to=y; + + v[x].push_back(a); + + ++s[x]; + + a.to=x; + + v[y].push_back(a); + + ++s[y]; + + } + +} + + + +void ini() + +{ + + for( int i=2 ; i<=n ; ++i ) + + c[i]=(double)INT_MAX; + +} + + + +void check( int x ) + +{ + + for( int i=0 ; i104659) + + var[x]-=104659; + + } + +} + + + +void solve() + +{ + + ini(); + + bf(); + + var[1]=1; + + viz[1]=1; + + for( int i=2 ; i<=n ; ++i ) + + if(viz[i]==0) + + apel(i); + +} + + + +void print() + +{ + + for( int i=2 ; i<=n ; ++i ) + + printf("%d ",var[i]); + +} + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/distmin/bellman/97ad422b-704b-4a41-9a72-509437459570.cpp b/distmin/bellman/97ad422b-704b-4a41-9a72-509437459570.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17b5445c240083bc400c9199e28ac3139d1cf3dc --- /dev/null +++ b/distmin/bellman/97ad422b-704b-4a41-9a72-509437459570.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 1502; + +const int MOD = 104659; + +const double EPS = 0.000001; + + + +typedef int Heap[MAX_N]; + + + +int N, M, Nr; + +int pos[MAX_N], S[MAX_N]; + +vector < pair < int, double > > v[MAX_N]; + +double D[MAX_N]; + +Heap H; + + + +inline int double_cmp(double a, double b) { + + if(a - b < -EPS) + + return -1; + + if(a - b > EPS) + + return 1; + + return 0; + +} + + + +inline void percolate(Heap H, int N, int K) { + + int temp = H[K]; + + while(K > 1 && double_cmp(D[temp], D[H[K/2]]) < 0) + + H[K] = H[K/2], pos[H[K]] = K, K /= 2; + + H[K] = temp, pos[temp] = K; + +} + + + +inline void sift(Heap H, int N, int K) { + + int son; + + do { + + son = 0; + + if(K*2 <= N) { + + son = K*2; + + if(K*2+1 <= N && double_cmp(D[H[K*2+1]], D[H[son]]) < 0) + + son = K*2+1; + + } + + if(double_cmp(D[H[son]], D[H[K]]) >= 0) + + son = 0; + + if(son) { + + int temp = H[K]; + + H[K] = H[son], H[son] = temp; + + pos[H[K]] = K, pos[H[son]] = son; + + K = son; + + } + + } while(son); + +} + + + +int main() { + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + + + f >> N >> M; + + for(int i = 1, x, y; i <= M; ++i) { + + double c; + + f >> x >> y >> c; + + c = log10(c); + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + + + H[++Nr] = 1, pos[1] = S[1] = 1; + + for(int i = 2; i <= N; ++i) + + D[i] = 9999999999; + + while(Nr) { + + int x = H[1]; + + H[1] = H[Nr--]; + + if(Nr) + + pos[H[1]] = 1, sift(H, Nr, 1); + + for(size_t i = 0; i < v[x].size(); ++i) { + + int y = v[x][i].first; + + double c = v[x][i].second; + + int t = double_cmp(D[x]+c, D[y]); + + if(t < 0) { + + D[y] = D[x] + c, S[y] = S[x]; + + if(pos[y]) + + percolate(H, Nr, pos[y]); + + else H[++Nr] = y, pos[y] = Nr, percolate(H, Nr, Nr); + + } + + else if(!t) + + S[y] += S[x], S[y] %= MOD; + + + + } + + } + + + + for(int i = 2; i <= N; ++i) + + g << S[i] << ' '; + + g << '\n'; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/distmin/bellman/98d027ed-9914-41cb-8080-092e9d57412f.cpp b/distmin/bellman/98d027ed-9914-41cb-8080-092e9d57412f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8db4f4111f1a6f6377040998a8cb747a29deded --- /dev/null +++ b/distmin/bellman/98d027ed-9914-41cb-8080-092e9d57412f.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/9e4b0e36-4122-48ce-930d-690a845c996f.cpp b/distmin/bellman/9e4b0e36-4122-48ce-930d-690a845c996f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68fa80c948e8ba4b3afd3ab38b7aefa8c566e788 --- /dev/null +++ b/distmin/bellman/9e4b0e36-4122-48ce-930d-690a845c996f.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) + + abs = -abs; + + if(abs < 0.000000001) + + return 0; + + if(x > y) + + return 1; + + if(x < y) + + return -1; + +} + +void BellmanFord() { + + queue Q; + + vector inQueue; inQueue.assign(N + 1, 0); + + dmin[1] = 0; + + nrd[1] = 1; + + Q.push(1); + + inQueue[1] = 1; + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { + + inQueue[neigh] = 1; Q.push(neigh); + + } + + } + + } + + } + +} + + + +int main() + +{ + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + G[a].push_back(make_pair(b, log(c))); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + + BellmanFord(); + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + + + + + + return 0; + +} diff --git a/distmin/bellman/a14d6676-bba1-4c91-a604-ae5a0c8491e4.cpp b/distmin/bellman/a14d6676-bba1-4c91-a604-ae5a0c8491e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba1b0ad2e9ef3a00a2d6ef96135e9d5e1c8bfe86 --- /dev/null +++ b/distmin/bellman/a14d6676-bba1-4c91-a604-ae5a0c8491e4.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include + +const int NMAX = 1505; +const double eps = 1e-10; +const double inf = 100000.0; +const int MOD = 104659; + +using namespace std; + +ifstream f("dmin.in"); +ofstream g("dmin.out"); + +double Dmin[NMAX]; +int N,M,x,y,c; +int d[NMAX]; +int dant[NMAX]; +vector < pair > v[NMAX]; +queue Q; +bool inQueue[NMAX]; + +void bellmanford() +{ + for (int i = 1; i <= N; ++i) + { + Dmin[i] = inf; + } + + Q.push(1); + d[1] = 1; + Dmin[1] = 0.0; + inQueue[1] = true; + + while(!Q.empty()) + { + int nod = Q.front(); + for (int i = 0; i < v[nod].size(); ++i) + { + int vecin = v[nod][i].first; + double costEnergie = v[nod][i].second; + + if (Dmin[nod] + costEnergie - Dmin[vecin] < -eps) + { + dant[vecin] = 0; + d[vecin] = d[nod]; + Dmin[vecin] = Dmin[nod] + costEnergie; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + else + { + if (fabs(Dmin[nod] + costEnergie - Dmin[vecin]) <= eps) + { + d[vecin] = (d[vecin] + d[nod] - dant[nod]) % MOD; + if (!inQueue[vecin]) + { + Q.push(vecin); + inQueue[vecin] = true; + } + } + } + } + + dant[nod] = d[nod]; + + Q.pop(); + inQueue[nod] = false; + } +} + +int main() +{ + f >> N >> M; + + for (int i = 1; i <= M; ++i) + { + f >> x >> y >> c; + double newc = log(c); + v[x].push_back(make_pair(y,newc)); + v[y].push_back(make_pair(x,newc)); + } + + bellmanford(); + + for (int i = 2; i <= N; ++i) + { + g << d[i] << " "; + } + + f.close(); + g.close(); + return 0; +} diff --git a/distmin/bellman/a1fb8c1a-fd62-4877-9791-7192b99889c1.cpp b/distmin/bellman/a1fb8c1a-fd62-4877-9791-7192b99889c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62feb2751cde2532ca3ef70a49ffd96e38059d36 --- /dev/null +++ b/distmin/bellman/a1fb8c1a-fd62-4877-9791-7192b99889c1.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +using namespace std; + +long n,m,i,x,y,z,nod,nod2,nr[1501]; + +char iq[1501]; + +double c,d[1501],dif; + +vector v[1501]; + +vector cost[1501]; + + + +void bellman(){ + + queueQ; + + for (i=2;i<=n;++i)d[i]=1<<30; d[1]=0; nr[1]=1; + + Q.push(1);iq[1]=1; + + while (!Q.empty()){ + + nod=Q.front(); Q.pop(); iq[nod]=0; + + int l=v[nod].size(); + + for (int i=0;i(-0.00000000001) && dif<0.00000000001){ + + nr[nod2]+=nr[nod];nr[nod2]=(nr[nod2]>104659)?(nr[nod2]-104659):(nr[nod2]); + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + } + + else + + if (dif<(-0.00000000001)){ + + d[nod2]=d[nod]+cost[nod][i]; + + nr[nod2]=nr[nod]; + + if (!iq[nod2]){ iq[nod2]=1; Q.push(nod2); } + + + + } + + } + + } + +} + + + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%ld %ld",&n,&m); + + while (m--){ + + scanf("%ld %ld %ld",&x,&y,&z); + + c=log(z); + + v[x].push_back(y);v[y].push_back(x); + + cost[x].push_back(c);cost[y].push_back(c); + + } + + bellman(); + + for (i=2;i<=n;++i) + + printf("%ld ",nr[i]); + + printf("\n"); + +return 0; + +} diff --git a/distmin/bellman/a2a27acb-f7bb-4820-965e-179c65ea5f91.cpp b/distmin/bellman/a2a27acb-f7bb-4820-965e-179c65ea5f91.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9a3e6250867d7e0bdfbf974596da4705a3c5c70 --- /dev/null +++ b/distmin/bellman/a2a27acb-f7bb-4820-965e-179c65ea5f91.cpp @@ -0,0 +1,136 @@ +# include +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +vector > HEAP; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost, cact; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + drumuri[1]=1; + + + + HEAP.push_back(make_pair(0, 1)); ap[1]=1; + + push_heap(HEAP.begin(), HEAP.end()); + + + + while (HEAP.size()) { + + cact=-HEAP[0].first; + + k=HEAP[0].second; + + + + pop_heap(HEAP.begin(), HEAP.end()); + + HEAP.pop_back(); + + + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +void citire(); + +void bellmanFord(); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +int main() + +{ + + citire(); + + bellmanFord(); + + + + for(i=2;i<=n;++i) g<dmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + + + + + nrd[1]=1; + + + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} diff --git a/distmin/bellman/a93c7585-f17e-4591-975b-6f31840aa873.cpp b/distmin/bellman/a93c7585-f17e-4591-975b-6f31840aa873.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/a93c7585-f17e-4591-975b-6f31840aa873.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/aa75725a-3e81-425c-82b4-cbfe0ef48409.cpp b/distmin/bellman/aa75725a-3e81-425c-82b4-cbfe0ef48409.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a4f6cf77e27a562f1a6c51f4e0f5fe6f4fe21d0 --- /dev/null +++ b/distmin/bellman/aa75725a-3e81-425c-82b4-cbfe0ef48409.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include + + +using namespace std; + +typedef int64_t var; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +struct Edge { + + var n, c; + + Edge(var a, var b) { + + n = a; + + c = b; + + } + +}; + + + + + +vector G[1505]; + +var n; + +double D[1505]; + +var D_R[1505]; + +var NR[1505]; + +bool INQ[1505]; +void bellman() { + + for(var i=1; i<=n; i++) { + + D[i] = (1LL << 55); + + } + + D[1] = 0; + + NR[1] = 1; + + queue Q; + + Q.push(1); + + while(!Q.empty()) { + + var node = Q.front(); + + Q.pop(); + + INQ[node] = 0; + + for(auto e : G[node]) { + + if((D[e.n] > D[node] + log10(e.c) + 1e-12)) { + + D[e.n] = D[node] + log10(e.c); + + D_R[e.n] = (D_R[node] * e.c) % 10000; + + NR[e.n] = 0; + + if(!INQ[e.n]) { + + Q.push(e.n); + + INQ[e.n] = 1; + + } + + } + + if(abs(D[e.n] - (D[node] + log10(e.c))) < 1e-12 && D_R[e.n] == (D_R[node] * e.c) % 10000 ) { + + NR[e.n] += NR[node]; + + NR[e.n] %= 104659; + + } + + } + + } + +} + + + +int main() { + + var a, b, m; + + var c; + + fin>>n>>m; + + while(m--) { + + fin>>a>>b>>c; + + G[a].push_back(Edge(b, c)); + + G[b].push_back(Edge(a, c)); + + } + + bellman(); + + for(var i=2; i<=n; i++) { + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +int N; + +vector< vector< pair< int, double> > > G; + +vector nrd; + +vector dmin; + + + +int comp(double x, double y) { + + double abs = x - y; + + if(abs < 0) abs = -abs; + + if(abs < 0.000000001) + + return 0; + + return (x > y ? 1 : -1); + +} + +void Read(); + +void BellmanFord(); + +void Write(); + + + +int main() + +{ + + Read(); + + BellmanFord(); + + Write(); + + + + return 0; + +} + + + +void BellmanFord() { + + queue Q; + + vector inQueue; + + inQueue.assign(N + 1, 0); + + dmin[1] = 0; nrd[1] = 1; + + Q.push(1); inQueue[1] = 1; + + + + while(!Q.empty()) { + + int node = Q.front(); Q.pop(); + + inQueue[node] = 0; + + + + for(int i = 0; i < G[node].size(); i++) { + + int neigh = G[node][i].first; + + double cost = G[node][i].second; + + if(comp(dmin[neigh], dmin[node] + cost + 0.000000001) > 0) { + + dmin[neigh] = dmin[node] + cost; + + nrd[neigh] = nrd[node]; + + if(!inQueue[neigh]) { + + Q.push(neigh); + + inQueue[neigh] = 1; + + } + + } + + else if(comp(dmin[neigh], dmin[node] + cost) == 0) { + + nrd[neigh] = (nrd[node] % 104659 + nrd[neigh] % 104659 ) % 104659; + + if(!inQueue[neigh]) { inQueue[neigh] = 1; Q.push(neigh); } + + } + + } + + } + +} + + + +void Write() { + + for(int i = 2; i <= N; i++) + + cout << nrd[i] % 104659 << ' '; + + cout << '\n'; + +} + +void Read() { + + freopen("dmin.in", "rt", stdin); + + freopen("dmin.out", "wt", stdout); + + int M; + + scanf("%d%d", &N, &M); + + G.assign(N + 2, vector >()); + + while(M--) { + + int a, b, c; + + scanf("%d%d%d", &a, &b, &c); + + double logc = log(c); + + G[a].push_back(make_pair(b, logc)); + + G[b].push_back(make_pair(a, logc)); + + } + + dmin.assign(N + 2, INT_MAX); + + nrd.assign(N + 2, 0); + +} diff --git a/distmin/bellman/ad13aa9d-cd9d-4a3b-b68e-35aac2e84f8d.cpp b/distmin/bellman/ad13aa9d-cd9d-4a3b-b68e-35aac2e84f8d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54c4fcf040d4c218287c8911abd20bd490c5216e --- /dev/null +++ b/distmin/bellman/ad13aa9d-cd9d-4a3b-b68e-35aac2e84f8d.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510]; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +void Bellman_Ford() + +{ + + queue Q; + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push(1); + + while(!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +const int MaxN = 1501; + +const double eps = 1e-10; + +const int Mod = 104659; + +const double InF = 100000000; + + + +const char InFile[] = "dmin.in"; + +const char OutFile[] = "dmin.out"; + + + +int N,M,sol[MaxN]; + +double d[MaxN]; + +bool inQ[MaxN]; + +vector< pair > G[MaxN]; + +queue Q; + + + +double modul( double x ) + +{ + + return x > 0 ? x : -x; + +} + + + +void BellMan_Ford() + +{ + + int nod; + + vector< pair >::iterator it,iend; + + for( nod = 1 ; nod <= N ; ++nod ) + + { + + d[nod] = InF; + + sol[nod] = 0; + + inQ[nod] = false; + + } + + d[1] = 0; + + sol[1] = 1; + + Q.push(1); + + inQ[1] = true; + + while( !Q.empty() ) + + { + + nod = Q.front(); + + Q.pop(); + + inQ[nod] = false; + + iend = G[nod].end(); + + it = G[nod].begin(); + + for( ; it != iend ; ++it ) + + { + + if( modul(d[it->first] - d[nod] - it-> second) < eps ) + + { + + sol[it->first] = ( sol[it->first]+sol[nod] ) % Mod; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + if( d[it->first] - d[nod] - it->second > eps ) + + { + + d[it->first] = d[nod] + it->second; + + sol[it->first] = sol[nod]; + + if( !inQ[it->first] ) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + ifstream fin( InFile ); + + ofstream fout( OutFile ); + + fin >> N >> M; + + int i,x,y; + + double z; + + for( i = 0 ; i < M ; ++i ) + + { + + fin >> x >> y >> z; + + G[x].push_back(make_pair(y,log(z))); + + G[y].push_back(make_pair(x,log(z))); + + } + + BellMan_Ford(); + + for( i = 2 ; i <= N ; ++i ) + + fout << sol[i] << ' '; + + fout << '\n'; + + fin.close();fout.close(); + + return 0; + +} diff --git a/distmin/bellman/b3874b72-8e7b-4a3c-8483-29a3902f511e.cpp b/distmin/bellman/b3874b72-8e7b-4a3c-8483-29a3902f511e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd505866e1b1669b3dc3415615869a1883ef3256 --- /dev/null +++ b/distmin/bellman/b3874b72-8e7b-4a3c-8483-29a3902f511e.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +using namespace std ; + + + +ofstream fout ( "dmin.out" ) ; + + + +int N , M , paths[1600] , inQueue[1600] ; + +double dist[1600] ; + +vector < pair < int , double > > TT[1600] ; + +queue < int > Q ; + + + + + +void read() + +{ + + int x , y , cost ; + + freopen ( "dmin.in" , "r" , stdin ) ; + + scanf ( "%d %d" , &N , &M ) ; + + for ( int i = 1 ; i <= M ; i++ ) + + { + + scanf ( "%d %d %d" , &x , &y , &cost ) ; + + TT[x].push_back( make_pair( y , log(cost) ) ) ; + + TT[y].push_back( make_pair( x , log(cost) ) ) ; + + } + + for ( int i = 2 ; i <= N ; i++ ) + + dist[i] = 1e40 ; + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + inQueue[1] = 1; + + paths[1] = 1; + + + + while(!Q.empty()) + + { + + int node = Q.front(); + + Q.pop(); + + vector >::iterator it = TT[node].begin(); + + while(it != TT[node].end()) + + { + + pair neigh = *it; + + if(dist[neigh.first] - 1e-8 > dist[node] + neigh.second) + + { + + dist[neigh.first] = dist[node] + neigh.second; + + paths[neigh.first] = paths[node]; + + if(inQueue[neigh.first] == 0) + + { + + Q.push(neigh.first); + + inQueue[neigh.first] = 1; + + } + + } + + else + + { + + if(dist[neigh.first] + 1e-8 >= dist[node] + neigh.second) + + { + + paths[neigh.first] += paths[node]; + + paths[neigh.first] %= 104659 ; + + } + + } + + + + it++; + + } + + + + } + + + + for ( int i = 2 ; i <= N ; i++ ) + + fout << paths[i] << ' ' ; + +} + + + + + +int main() + +{ + + read() ; + + bellman_ford() ; + + return 0; + +} diff --git a/distmin/bellman/ba86fe12-2c81-4ecf-8c36-6a546f9fa34f.cpp b/distmin/bellman/ba86fe12-2c81-4ecf-8c36-6a546f9fa34f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17b5445c240083bc400c9199e28ac3139d1cf3dc --- /dev/null +++ b/distmin/bellman/ba86fe12-2c81-4ecf-8c36-6a546f9fa34f.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 1502; + +const int MOD = 104659; + +const double EPS = 0.000001; + + + +typedef int Heap[MAX_N]; + + + +int N, M, Nr; + +int pos[MAX_N], S[MAX_N]; + +vector < pair < int, double > > v[MAX_N]; + +double D[MAX_N]; + +Heap H; + + + +inline int double_cmp(double a, double b) { + + if(a - b < -EPS) + + return -1; + + if(a - b > EPS) + + return 1; + + return 0; + +} + + + +inline void percolate(Heap H, int N, int K) { + + int temp = H[K]; + + while(K > 1 && double_cmp(D[temp], D[H[K/2]]) < 0) + + H[K] = H[K/2], pos[H[K]] = K, K /= 2; + + H[K] = temp, pos[temp] = K; + +} + + + +inline void sift(Heap H, int N, int K) { + + int son; + + do { + + son = 0; + + if(K*2 <= N) { + + son = K*2; + + if(K*2+1 <= N && double_cmp(D[H[K*2+1]], D[H[son]]) < 0) + + son = K*2+1; + + } + + if(double_cmp(D[H[son]], D[H[K]]) >= 0) + + son = 0; + + if(son) { + + int temp = H[K]; + + H[K] = H[son], H[son] = temp; + + pos[H[K]] = K, pos[H[son]] = son; + + K = son; + + } + + } while(son); + +} + + + +int main() { + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + + + f >> N >> M; + + for(int i = 1, x, y; i <= M; ++i) { + + double c; + + f >> x >> y >> c; + + c = log10(c); + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + + + H[++Nr] = 1, pos[1] = S[1] = 1; + + for(int i = 2; i <= N; ++i) + + D[i] = 9999999999; + + while(Nr) { + + int x = H[1]; + + H[1] = H[Nr--]; + + if(Nr) + + pos[H[1]] = 1, sift(H, Nr, 1); + + for(size_t i = 0; i < v[x].size(); ++i) { + + int y = v[x][i].first; + + double c = v[x][i].second; + + int t = double_cmp(D[x]+c, D[y]); + + if(t < 0) { + + D[y] = D[x] + c, S[y] = S[x]; + + if(pos[y]) + + percolate(H, Nr, pos[y]); + + else H[++Nr] = y, pos[y] = Nr, percolate(H, Nr, Nr); + + } + + else if(!t) + + S[y] += S[x], S[y] %= MOD; + + + + } + + } + + + + for(int i = 2; i <= N; ++i) + + g << S[i] << ' '; + + g << '\n'; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/distmin/bellman/bac6ec3c-625c-4794-a4c9-1bed3b77f1be.cpp b/distmin/bellman/bac6ec3c-625c-4794-a4c9-1bed3b77f1be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e74fc0ec660c2787d8dcc40a9a88ddef717913e --- /dev/null +++ b/distmin/bellman/bac6ec3c-625c-4794-a4c9-1bed3b77f1be.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include #include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/bf8d2b3e-8eca-422e-835f-4611a9082268.cpp b/distmin/bellman/bf8d2b3e-8eca-422e-835f-4611a9082268.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfed5cd80a159d8699d9cbc17a92414a4b2c5879 --- /dev/null +++ b/distmin/bellman/bf8d2b3e-8eca-422e-835f-4611a9082268.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/c26ea371-547a-4d7e-b468-6dbdce5b25eb.cpp b/distmin/bellman/c26ea371-547a-4d7e-b468-6dbdce5b25eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8e8a8818c0ac2dffd75b4bfcc9d82b0ce3379c9 --- /dev/null +++ b/distmin/bellman/c26ea371-547a-4d7e-b468-6dbdce5b25eb.cpp @@ -0,0 +1,118 @@ +# include +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +inline double modul(double x) + +{ + + if(x<0) return -x; + + return x; + +} + + + +struct muchie + +{ + + int nod; + + double cost; + +} aux; + + + +vector < muchie > G[1502]; + +queue Q; + +double z; + +int n, m, i, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + + + +void citire() + +{ + + int x, y; + + double z; + + double cost; + + f>>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + z=log(cost); + + aux.nod=y; + + aux.cost=z; + + G[x].push_back(aux); + + aux.nod=x; + + G[y].push_back(aux); + + } + + nrd[1]=1; + + for (i=2;i<=n;i++) + + dmin[i]=1000001; + +} + + + +void bellmanFord() + +{ + + int x, vecin; + + double cost; + + int i; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + for (i=0;idmin[x]+cost) + + { + + dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) Q.push(vecin), viz[vecin]=1; + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + + + + } + +} + + + +int main() + +{ + + citire(); + + bellmanFord(); + + for(i=2;i<=n;++i) g< +# include +# include +# include +# include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1505]; + +queue q; + +int i,j,n,m,x,y; + +int drumuri[1505], ap[1505]; + +double dist[1505], cost; + +void BF () + +{ + + int i,j,k,urm; + + double cost; + + + + for (i=2; i<=n; ++i) dist[i]=999999999; + + q.push(1); drumuri[1]=1; + + while (! q.empty()) { + + k=q.front(); q.pop(); ap[k]=0; + + + + for (i=0; i=104659) drumuri[urm]-=104659; + + } + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1; i<=m; ++i) { + + f>>x>>y>>cost; int Q=cost; + + cost=log(cost); + + + + v[x].push_back(make_pair(y, cost)); + + v[y].push_back(make_pair(x, cost)); + + } + + BF (); + + + + for (i=2; i<=n; ++i) + + g< +#include +#include + + +using namespace std; + + + +typedef struct lol { + + int x,y; + + double z; + +}troll; + + + +const double EPS=1e-7; + +const int MOD=104659; + + + +int i,n,m,rs[1505]; + +double d[1505]; + +troll a[5005]; + + + +bool cmp(const troll &a,const troll &b) { return a.x0 ? a:-a; } + + + +int main() + +{ + + ifstream cin("dmin.in"); + + ofstream cout("dmin.out"); + + + + ios_base::sync_with_stdio(0); + + + + cin>>n>>m; + + + + for(i=1,m*=2;i>a[i].x>>a[i].y>>a[i].z; a[i].z=log(a[i++].z); + + a[i].x=a[i-1].y; a[i].y=a[i-1].x; a[i].z=a[i-1].z; + + } + + + + for(i=2;i<=n;++i) d[i]=-1; + + + + sort(a+1,a+m+1,cmp); + + + + for(rs[1]=i=1;i<=m;++i) + + if(d[a[i].x]!=-1 && (d[a[i].y]==-1 || d[a[i].y]-d[a[i].x]-a[i].z>EPS)) + + d[a[i].y]=d[a[i].x]+a[i].z,rs[a[i].y]=rs[a[i].x]; + + else if(d[a[i].x]!=-1 && ABS(d[a[i].y]-d[a[i].x]-a[i].z) +#include +#include +#include + + + + + + +using namespace std; + +const int N=2501,M=1000001; + + + +int n,m,s[N],q[M],p,var[N],u; + +double c[N]; + +bool viz[N]; + +struct xy{ + + int to; + + double c; + +}; + +vector v[N]; + + + +void read() + +{ + + int x,y,z; + + xy a; + + scanf("%d%d",&n,&m); + + for( int i=1 ; i<=m ; ++i ) + + { + + scanf("%d%d%d",&x,&y,&z); + + a.c=log10((double)z); + + a.to=y; + + v[x].push_back(a); + + ++s[x]; + + a.to=x; + + v[y].push_back(a); + + ++s[y]; + + } + +} + + + +void ini() + +{ + + for( int i=2 ; i<=n ; ++i ) + + c[i]=(double)INT_MAX; + +} + + + +void check( int x ) + +{ + + for( int i=0 ; i104659) + + var[x]-=104659; + + } + +} + + + +void solve() + +{ + + ini(); + + bf(); + + var[1]=1; + + viz[1]=1; + + for( int i=2 ; i<=n ; ++i ) + + if(viz[i]==0) + + apel(i); + +} + + + +void print() + +{ + + for( int i=2 ; i<=n ; ++i ) + + printf("%d ",var[i]); + +} + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/distmin/bellman/c9ee5d84-6ce1-47ba-9da1-e9e05d242f10.cpp b/distmin/bellman/c9ee5d84-6ce1-47ba-9da1-e9e05d242f10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54c4fcf040d4c218287c8911abd20bd490c5216e --- /dev/null +++ b/distmin/bellman/c9ee5d84-6ce1-47ba-9da1-e9e05d242f10.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + + + + +using namespace std; + +int n,m,nr[1510]; + +struct Muchie{int x,y;double cost;}; + +Muchie M[2510]; + +vector G[1510]; + +double dist[1510]; + +bool viz[1510]; + + + +void Citire() + +{ + + int i; + + ifstream fin("dmin.in"); + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>M[i].x>>M[i].y>>M[i].cost; + + M[i].cost=log(M[i].cost); + + G[M[i].x].push_back(i); + + G[M[i].y].push_back(i); + + } + + fin.close(); + +} + + + +void Bellman_Ford() + +{ + + queue Q; + + vector ::iterator it; + + int i,x,y; + + for(i=2;i<=n;i++) + + dist[i]=2000000000.0; + + nr[1]=1; + + viz[1]=true; + + Q.push(1); + + while(!Q.empty()) + + { + + x=Q.front(); + + Q.pop(); + + viz[x]=false; + + for(it=G[x].begin();it!=G[x].end();it++) + + { + + y=M[*it].x+M[*it].y-x; + + if(0.000001=104659) + + nr[y]-=104659; + + } + + } + + } + +} + + + +void Afisare() + +{ + + int i; + + ofstream fout("dmin.out"); + + for(i=2;i<=n;i++) + + fout< +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int n, m, a, b, c; + +vector< pair > v[1505]; + +double d[1505]; + +int cnt[1505]; + +priority_queue< pair< double, int >, vector< pair< double, int > >, greater< pair< double, int > > > q; + + + +double modul( double x ){ + + if( x > 0 ){ + + return x; + + } + + return -x; + +} + + + +int main(){ + + fin >> n >> m; + + for( int i = 1; i <= m; i++ ){ + + fin >> a >> b >> c; + + v[a].push_back( make_pair( b, log(c) ) ); + + v[b].push_back( make_pair( a, log(c) ) ); + + } + + for( int i = 2; i <= n; i++ ){ + + d[i] = 20000000000.0; + + } + + cnt[1] = 1; + + q.push( make_pair( 0, 1 ) ); + + while( !q.empty() ){ + + int nod = q.top().second; + + + + if( q.top().first - d[nod] >= 0.0000000001 ){ + + q.pop(); + + continue; + + } + + + + for( int i = 0; i < v[nod].size(); i++ ){ + + int vecin = v[nod][i].first; + + double cost = v[nod][i].second + d[nod]; + + if( modul( d[vecin] - cost ) <= 0.0000000001 ){ + + cnt[vecin] = ( cnt[vecin] + cnt[nod] ) % 104659; + + }else{ + + if( cost < d[vecin] ){ + + d[vecin] = cost; + + cnt[vecin] = cnt[nod]; + + q.push( make_pair( d[vecin], vecin ) ); + + } + + } + + } + + + + q.pop(); + + } + + for( int i = 2; i <= n; i++ ){ + + fout << cnt[i] << " "; + + } + + return 0; + +} diff --git a/distmin/bellman/d0b5a2e3-c4c3-40a8-9cc8-e8dbe6fd9a21.cpp b/distmin/bellman/d0b5a2e3-c4c3-40a8-9cc8-e8dbe6fd9a21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9bbad51f19471c8959f5fa343d680fe5d39347b --- /dev/null +++ b/distmin/bellman/d0b5a2e3-c4c3-40a8-9cc8-e8dbe6fd9a21.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +using namespace std; + +int n, m, i, x, y, nod, vecin, cost, j; + +int viz[1505], num[1505], h[1505], poz[1505]; + +double d[1505], cost2; + +vector< pair > v[1505]; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +double modul(double x){ + + if(x > 0){ + + return x; + + } + + return -x; + +} + +void upd(int pos){ + + int c = pos, p = c / 2; + + while(p > 0 && d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + c = p; + + p = c / 2; + + } + +} + +void elim(){ + + int p = 1, c = 2; + + while(c <= n){ + + if(c + 1 <= n && d[ h[c] ] > d[ h[c + 1] ]){ + + c++; + + } + + if(d[ h[p] ] > d[ h[c] ]){ + + swap(h[p], h[c]); + + poz[ h[p] ] = p; + + poz[ h[c] ] = c; + + p = c; + + c = 2 * p; + + } + + else{ + + break; + + } + + } + +} + +int main(){ + + fin>> n >> m; + + for(i = 1; i <= m; i++){ + + fin>> x >> y >> cost; + + cost2 = log(cost); + + v[x].push_back( make_pair(y, cost2) ); + + v[y].push_back( make_pair(x, cost2) ); + + } + + for(i = 1; i <= n; i++){ + + d[i] = 1000000000; + + h[i] = poz[i] = i; + + } + + d[1] = 0; + + num[1] = 1; + + for(i = 1; i <= n; i++){ + + nod = h[1]; + + for(j = 0; j < v[nod].size(); j++){ + + vecin = v[nod][j].first; + + if(viz[vecin] == 0){ + + if(modul(d[vecin] - d[nod] - v[nod][j].second) <= 0.000000001){ + + num[vecin] += num[nod]; + + if(num[vecin] >= 104659){ + + num[vecin] -= 104659; + + } + + } + + else{ + + if(d[nod] + v[nod][j].second < d[vecin]){ + + d[vecin] = d[nod] + v[nod][j].second; + + upd(poz[vecin]); + + num[vecin] = num[nod]; + + } + + } + + } + + } + + viz[nod] = 1; + + d[nod] = 1000000000; + + elim(); + + } + + for(i = 2; i <= n; i++){ + + fout<< num[i] <<" "; + + } + + return 0; + +} diff --git a/distmin/bellman/d409eac8-07cf-40a4-a8be-d917add8e675.cpp b/distmin/bellman/d409eac8-07cf-40a4-a8be-d917add8e675.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faacd8ce6b694d6991c19d898050389a815655bb --- /dev/null +++ b/distmin/bellman/d409eac8-07cf-40a4-a8be-d917add8e675.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const double inf= 0x3f3f3f; + +const double eps= 0.0000001; + +const int nmax= 1500; + +const int mod= 104659; + + + +double minim[nmax+1]; + +int d[nmax+1]; + + + +struct str { + + int x; + + double y; + +}; + + + +struct str_cmp { + + bool operator () ( const str &x, const str &y ) { + + return x.y>y.y; + + } + +}; + + + +priority_queue , str_cmp> h; + +vector v[nmax+1]; + + + +inline str mp( int x, double y ) { + + str sol; + + sol.x= x, sol.y= y; + + + + return sol; + +} + + + +double av( double x ) { + + if ( x<0 ) { + + x= -x; + + } + + return x; + +} + + + +void dijkstra( ) { + + d[1]= 1; + + for ( h.push(mp(1, 0)); !h.empty(); ) { + + str x= h.top(); + + h.pop(); + + + + if ( x.y-minim[x.x]>=eps ) { + + continue; + + } + + + + for ( vector ::iterator it= v[x.x].begin(); it!=v[x.x].end(); ++it ) { + + if ( minim[(*it).x]-minim[x.x]-(*it).y>eps ) { + + minim[(*it).x]= minim[x.x]+(*it).y; + + d[(*it).x]= d[x.x]; + + h.push(mp((*it).x, minim[(*it).x])); + + } else if ( av(minim[(*it).x]-minim[x.x]-(*it).y)<=eps ) { + + d[(*it).x]= (d[(*it).x]+d[x.x])%mod; + + } + + } + + } + +} + + + +int main( ) { + + int n, m; + + fin>>n>>m; + + for ( int i= 1; i<=m; ++i ) { + + int x, y; + + double z; + + fin>>x>>y>>z; + + z= log(z); + + + + v[x].push_back(mp(y, z)); + + v[y].push_back(mp(x, z)); + + } + + + + for ( int i= 2; i<=n; ++i ) { + + minim[i]= inf; + + } + + dijkstra(); + + + + for ( int i= 2; i<=n; ++i ) { + + fout< +#include +#include +#include +using namespace std; + +short int n,m,x,y,coada[1501*1501]; + +int z,nr[1501],rez[1501]; + +double d[1501]; + +bitset<1501> viz; + +const double eps=1e-8; + +const double eps1=eps*(-1); + +vectora[1501],ap(1501,0); + +vectorc[1501]; + +void citire() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%hd%hd",&n,&m); + + for (int i=1; i<=m; ++i) + + { + + scanf("%hd%hd%d",&x,&y,&z); + + double g=log10(z); + + a[x].push_back(y); + + c[x].push_back(g); + + a[y].push_back(x); + + c[y].push_back(g); + + } + +} + +void bf() + +{ + + int p=0,u=0; + + coada[u++]=1; + + nr[1]=1; + + viz[1]=1; + + while (u!=p) + + { + + x=coada[p++]; + + viz[x]=false; + + size_t g=a[x].size(); + + for (size_t i=0; i!=g; ++i) + + { + + y=a[x][i]; + + double aux=d[x]+c[x][i],diff=d[y]-aux; + + if (diff>=eps) + + { + + d[y]=aux; + + if (!viz[y]) + + { + + viz[y]=true; + + coada[u++]=y; + + } + + } + + } + + } + +} + +void afis() + +{ + + for (int i=2; i<=n; ++i) + + printf("%d ",nr[i]); + +} + +void df(int x) + +{ + + viz[x]=1; + + size_t g=a[x].size(); + + for (size_t i=0; i=eps1) + + { + + + + if (!viz[a[x][i]]) + + df(a[x][i]); + + nr[x]+=nr[a[x][i]]; + + if (nr[x]>=104659) + + nr[x]-=104659; + + } + + } + +} + +int main() + +{ + + citire(); + + for (int j=2; j<=n; ++j) + + d[j]=1<<30;; + + bf(); + + viz.reset(); + + nr[1]=1; + + for (int i=2; i<=n; ++i) + + if (!viz[i]) + + df(i); + + afis(); + + + + + + + + return 0; + +} diff --git a/distmin/bellman/e0040b0d-0f43-4ab7-ba02-53d3acf3b7f6.cpp b/distmin/bellman/e0040b0d-0f43-4ab7-ba02-53d3acf3b7f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1f8437db5e6e44af045865f9a2278f1477769ce --- /dev/null +++ b/distmin/bellman/e0040b0d-0f43-4ab7-ba02-53d3acf3b7f6.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +vector > G[1605]; + +vector daddy[1605]; + +queue Q; + +bitset<1605> inQ; + + + +int N,M; + +double dist[1605]; + +int DP[1605]; + + + +void read() + +{ + + int a,b,c; + + scanf("%d%d",&N,&M); + + for(int i = 1; i <= M; ++i){ + + scanf("%d%d%d",&a,&b,&c); + + G[a].push_back(make_pair(log2((double)c),b)); + + G[b].push_back(make_pair(log2((double)c),a)); + + } + +} + + + +void bellman_ford() + +{ + + Q.push(1); + + for(int i = 2; i <= N; ++i) dist[i] = 0x3f3f3f3f; + + dist[1] = 0; + + int k; + + while(!Q.empty()) + + { + + k = Q.front();Q.pop();inQ[k] = 0; + + for(vector >::iterator it = G[k].begin(); it != G[k].end(); ++it) + + { + + if(dist[it->second] > dist[k] + it->first + 0.00000001) + + { + + dist[it->second] = dist[k] + it->first; + + daddy[it->second].clear(); + + daddy[it->second].push_back(k); + + inQ[it->second] = 1; + + Q.push(it->second); + + continue; + + } + + if(dist[it->second] - dist[k] - it->first + 0.00000001 >= 0 && + + dist[it->second] - dist[k] - it->first <= 0.0001 ) + + daddy[it->second].push_back(k); + + } + + } + +} + + + +int dynamic(int k) + +{ + + if(DP[k])return DP[k]; + + int sum = 0; + + for(vector::iterator it = daddy[k].begin(); it != daddy[k].end(); ++it) + + sum = (sum%104659 + dynamic(*it)%104659) % 104659; + + DP[k] = sum; + + return DP[k] % 104659; + +} + +void prepare() + +{ + + for(int i = 1; i <= N; ++i) + + { + + vector::iterator it = unique(daddy[i].begin(),daddy[i].end()); + + daddy[i].resize(distance(daddy[i].begin(),it)); + + } + +} + + + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + read(); + + bellman_ford(); + + DP[1] = 1; + + prepare(); + + for(int i = 2; i <= N; ++i) + + printf("%d ",dynamic(i)); + + + + return 0; + +} diff --git a/distmin/bellman/e3c751ba-1939-458b-ba3d-b7a255a7c9fe.cpp b/distmin/bellman/e3c751ba-1939-458b-ba3d-b7a255a7c9fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c1a9f04260f9289ba795a3fc7e480c58914a5e5 --- /dev/null +++ b/distmin/bellman/e3c751ba-1939-458b-ba3d-b7a255a7c9fe.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +const int nmax = 1510; + + + +struct el{ + +int a; + +double cost; + + + + el(int aa,double c){ + + a = aa; + + cost = c; + + } + +}; + +vectorG[nmax]; + +queueq; + +int N,M,nr[nmax]; + +double dp[nmax]; + +bool ok[nmax]; + + + +inline void BFS(){ + + int nod; + + + + for(int i = 2; i <= N; ++i)dp[i] = 0x3f3f3f3f; + + + + nr[1] = 1; + + q.push(1); + + ok[1] = true; + + + + while(!q.empty()){ + + nod = q.front(); + + ok[nod] = false; + + q.pop(); + + + + for(auto it: G[nod]){ + + if(dp[it.a]-it.cost-dp[nod] >= 1e-10){ + + + + dp[it.a] = dp[nod] + it.cost; + + nr[it.a] = nr[nod]; + + if(!ok[it.a]){ + + ok[it.a] = true; + + q.push(it.a); + + } + + } + + else if(abs(dp[nod]-dp[it.a]+it.cost) < 1e-10){ + + nr[it.a]+=nr[nod]; + + if(nr[it.a]>=104659)nr[it.a]-=104659; + + } + + } + + } + + + +} + +int main(){ + + int i,x,y,c; + + freopen ("dmin.in","r",stdin); + + freopen ("dmin.out","w",stdout); + + scanf("%d %d\n",&N,&M); + + for(i = 1; i <= M; ++i){ + + scanf("%d %d %d\n",&x,&y,&c); + + G[x].push_back(el(y,log10(c))); + + G[y].push_back(el(x,log10(c))); + + } + + BFS(); + + + + for(i = 2; i <= N; ++i) + + cout << nr[i] <<' '; + + + + return 0; + +} diff --git a/distmin/bellman/e41c7284-7ce9-469a-900d-8d9105629460.cpp b/distmin/bellman/e41c7284-7ce9-469a-900d-8d9105629460.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/e41c7284-7ce9-469a-900d-8d9105629460.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/e7350638-1f60-44f4-b11a-c923bfab7d65.cpp b/distmin/bellman/e7350638-1f60-44f4-b11a-c923bfab7d65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be90d33072c5dc8958c7f09424d1660e16bb8aa6 --- /dev/null +++ b/distmin/bellman/e7350638-1f60-44f4-b11a-c923bfab7d65.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +class nod + +{ public: type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + + + +vectora[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +using namespace std; + + + + + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +template + +struct nod + +{ type info; nod *next; + +}; + +struct pairr { int to; double cost; }; + +pairr mp(int x,double y) + +{ pairr c; c.to=x , c.cost=y; + + return c; + +} + +template + +class queue + +{ nod *head,*tail; int dim; + + public: + + queue() { dim=0; } + + void create(type x) + + { head=new nod; + + head->next=NULL , head->info=x; + + tail=head , dim=1; + + } + + void push(type x) + + { if(!dim) { create(x); return; } + + nod *p=new nod; + + p->info=x , p->next=NULL; + + tail->next=p; + + tail=p , dim++; + + } + + void pop() + + { nod *p=new nod; + + p=head; + + head=head->next; + + delete p , dim--; + + } + + bool empty() { return dim==0; } + + type front() { type &x=head->info; return x; } + +}; + +template + +class vector + +{ type *aux; + + int capacity,m_size; + + public: + + type *act; + + vector() + + { act=new type[1]; capacity=1; m_size=0; } + + void reallocate() + + { aux=new type[2*capacity] , capacity*=2; + + for(int i=0;ia[1501]; + +double cmin[1501]; + +bool v[1501]; + +int way[1501]; + +queueq; + +void BF() + +{ int i,k,j; double co; + + q.push(1); v[1]=true; + + cmin[1]=0.00; way[1]=1; + + while(!q.empty()) + + { i=q.front(); q.pop(); v[i]=false; + + for(k=0;k1e-10) + + { cmin[j]=cmin[i]+co; + + way[j]=way[i]; + + if(v[j]==false) + + { q.push(j); + + v[j]=true; + + } + + }else if(cmin[i]+co-cmin[j]<=1e-10) + + way[j]=(way[j]%104659 +way[i]%104659)%104659; + + } + + } + +} + +int main() + +{ int i,j,x,y,N,M; double c; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { f>>x>>y>>c; + + a[x].push_back(mp(y,log(c))); + + a[y].push_back(mp(x,log(c))); + + } + + for(i=1;i<=N;i++) cmin[i]=1000000.000; + + BF(); + + for(i=2;i<=N;i++) + + g< +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); +struct Muchie{ + + int vecin; + + double cost; + +}; + + + +queue Q; + +vector A[1501]; + +int n, m, nrd[1501], inq[1501]; + +double dmin[1501]; + + + +int main() + +{ + + int a, b, i; + + double c; + + Muchie T; + + + + + + f>>n>>m; + + for(i = 1; i <= m; i++) { + + f>>a>>b>>c; + + c = log(c); + + T.vecin = b; T.cost = c; + + A[a].push_back(T); + + T.vecin = a; T.cost = c; + + A[b].push_back(T); + + } + + + + + + dmin[1] = 0; + + for(i = 2; i <= n; i++) dmin[i] = 1000000001; + + nrd[1] = 1; + + for(i = 2; i <= n; i++) nrd[i] = 0; + + + + + + Q.push(1); inq[1] = 1; + + while(!Q.empty()) { + + int x = Q.front(); Q.pop(); inq[x] = 0; + + for(i = 0; i < A[x].size(); i++) { + + int vecin = A[x][i].vecin; + + double cost = A[x][i].cost; + + double dif = dmin[vecin] - dmin[x] - cost; + + if(dif < 0) dif = -dif; + + if(dif < 0.0000000001) + + nrd[vecin] = (nrd[vecin] + nrd[x]) % 104659; + + else + + if(dmin[vecin] > dmin[x] + cost) { + + dmin[vecin] = dmin[x] + cost; + + nrd[vecin] = nrd[x]; + + if(!inq[vecin]) { + + Q.push(vecin); + + inq[vecin] = 1; + + } + + } + + } + + } + + + + for(i = 2; i <= n; i++) g< +using namespace std; + + + +int egal(double,double); + +void read(); + +void bellmanford(); + +vector > G[1505]; + +int nrd[1505],inQueue[1505],n,m; + +double dmin[1505]; + +int main(){ + + read(); + + bellmanford(); + + freopen("dmin.out","w",stdout); + + for (int i=2;i<=n;i++){ + + cout< q; + + q.push(1); + + nrd[1]=1; + + inQueue[1]=1; + + while (!q.empty()){ + + int nod=q.front(); + + inQueue[nod]=0; + + q.pop(); + + for (int i=0;idmin[nod]+cost+1e-9){ + + nrd[vecin]=nrd[nod]%104659; + + dmin[vecin]=dmin[nod]+cost; + + if (!inQueue[vecin]){ + + q.push(vecin); + + inQueue[vecin]=1; + + } + + } + + else { + + if (egal(dmin[vecin],dmin[nod]+cost)){ + + nrd[vecin]=((nrd[nod]%104659)+(nrd[vecin]%104659))%104659; + + } + + } + + } + + } + +} + + + +void read(){ + + freopen("dmin.in","r",stdin); + + scanf("%d%d",&n,&m); + + for (int i=1;i<=m;i++){ + + int x,y,c; + + scanf("%d%d%d",&x,&y,&c); + + double nc=log(c); + + G[x].push_back(make_pair(y,nc)); + + G[y].push_back(make_pair(x,nc)); + + } + +} + + + +int egal(double x,double y){ + + return abs(x-y)<1e-9; + +} diff --git a/distmin/bellman/ed229ead-e2ce-40a1-a4cc-a2a4ed7872ff.cpp b/distmin/bellman/ed229ead-e2ce-40a1-a4cc-a2a4ed7872ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43849e119e33f17822b0a97b87f6309cf9dfcbf0 --- /dev/null +++ b/distmin/bellman/ed229ead-e2ce-40a1-a4cc-a2a4ed7872ff.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include + + +const int kMaxDim = 1505; + +const int kModulo = 104659; + +const double eps = 1e-8; + +const double inf = 1e10; + + + +inline double Abs(const double& a) { + + return (a > 0 ? a : -a); + +} + + + +inline int Compare(const double& a, const double& b) { + + if (Abs(a - b) < eps) + + return 0; + + return (a < b ? -1 : 1); + +} + + + +struct Data { + + int node; + + double cost; + +}; + + + +struct Functor { + + bool operator()(const Data& a, const Data& b) { + + return Compare(a.cost, b.cost) > 0; + + } + +}; + + + +int nodeCount, edgeCount; + +std::vector< Data > edges[kMaxDim]; + +double minDist[kMaxDim]; + +int minDistCount[kMaxDim]; + + + +std::priority_queue< Data, std::vector< Data >, Functor > heap; + + + +int main() { + + std::ifstream inputFile("dmin.in"); + + std::ofstream outputFile("dmin.out"); + + + + inputFile >> nodeCount >> edgeCount; + + for (int i = 1; i <= edgeCount; ++i) { + + int first, second; + + double cost; + + inputFile >> first >> second >> cost; + + cost = log(cost); + + + + edges[first].push_back({second, cost}); + + edges[second].push_back({first, cost}); + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + minDist[i] = inf; + + minDistCount[1] = 1; + + heap.push({1, 0}); + + + + while (!heap.empty()) { + + while (!heap.empty() && Compare(minDist[heap.top().node], heap.top().cost) < 0) + + heap.pop(); + + if (heap.empty()) + + break; + + + + int node = heap.top().node; + + double cost = heap.top().cost; + + heap.pop(); + + + + for (Data& edge : edges[node]) { + + if (Compare(minDist[edge.node], cost + edge.cost) > 0) { + + minDist[edge.node] = cost + edge.cost; + + minDistCount[edge.node] = minDistCount[node]; + + heap.push({edge.node, minDist[edge.node]}); + + } + + else if (Compare(minDist[edge.node], cost + edge.cost) == 0) { + + minDistCount[edge.node] += minDistCount[node]; + + minDistCount[edge.node] %= kModulo; + + } + + } + + } + + + + for (int i = 2; i <= nodeCount; ++i) + + outputFile << minDistCount[i] << ' '; + + outputFile << '\n'; + + + + inputFile.close(); + + outputFile.close(); + + + + return 0; + +} diff --git a/distmin/bellman/ee8a4e85-9635-4de1-a6bf-84593a25eb86.cpp b/distmin/bellman/ee8a4e85-9635-4de1-a6bf-84593a25eb86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a4f6cf77e27a562f1a6c51f4e0f5fe6f4fe21d0 --- /dev/null +++ b/distmin/bellman/ee8a4e85-9635-4de1-a6bf-84593a25eb86.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include + + +using namespace std; + +typedef int64_t var; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +struct Edge { + + var n, c; + + Edge(var a, var b) { + + n = a; + + c = b; + + } + +}; + + + + + +vector G[1505]; + +var n; + +double D[1505]; + +var D_R[1505]; + +var NR[1505]; + +bool INQ[1505]; +void bellman() { + + for(var i=1; i<=n; i++) { + + D[i] = (1LL << 55); + + } + + D[1] = 0; + + NR[1] = 1; + + queue Q; + + Q.push(1); + + while(!Q.empty()) { + + var node = Q.front(); + + Q.pop(); + + INQ[node] = 0; + + for(auto e : G[node]) { + + if((D[e.n] > D[node] + log10(e.c) + 1e-12)) { + + D[e.n] = D[node] + log10(e.c); + + D_R[e.n] = (D_R[node] * e.c) % 10000; + + NR[e.n] = 0; + + if(!INQ[e.n]) { + + Q.push(e.n); + + INQ[e.n] = 1; + + } + + } + + if(abs(D[e.n] - (D[node] + log10(e.c))) < 1e-12 && D_R[e.n] == (D_R[node] * e.c) % 10000 ) { + + NR[e.n] += NR[node]; + + NR[e.n] %= 104659; + + } + + } + + } + +} + + + +int main() { + + var a, b, m; + + var c; + + fin>>n>>m; + + while(m--) { + + fin>>a>>b>>c; + + G[a].push_back(Edge(b, c)); + + G[b].push_back(Edge(a, c)); + + } + + bellman(); + + for(var i=2; i<=n; i++) { + + fout< +#include #include +#include +#include +using namespace std; +double a[1501][1501]; + +double d[1501]; + +int dp[1501]; + +vectorx[1501]; + +int n; + + + +void read() + +{ + + int m, p, q, c; + + freopen("dmin.in","r",stdin); + + scanf("%d %d\n", &n, &m); + + + + while(m--) + + { + + scanf("%d %d %d\n", &p, &q, &c); + + x[p].push_back(q); + + x[q].push_back(p); + + a[p][q]=a[q][p]=(double) log((double)c); + + } + +} + + + +void BF() + +{ + + queueQ; + + + + for(int i=0;i<=n;++i) d[i]=10000000; + + d[1]=0; + + int u,i; + + vector::iterator it; + + Q.push(1); + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + + + if(d[*it]-d[u]-a[u][*it]>1e-10) + + Q.push(*it), + + d[*it]=d[u]+a[u][*it]; + + + + } + + + +} + + + +void solve() + +{ + + memset(dp, 0, sizeof(dp)); + + dp[1]=1; + + queueQ; + + int u, i; + + vector::iterator it,jt; + + + + Q.push(1); + + bool use[1501]; + + memset(use, 0,sizeof(use)); + + use[1]=1; + + + + + + + + while(!Q.empty()) + + { + + u=Q.front(); + + Q.pop(); + + + + for(it=x[u].begin();it!=x[u].end();++it) + + if(!use[*it]) + + { + + Q.push(*it); + + use[*it]=1; + + + + + + for(jt=x[*it].begin();jt!=x[*it].end();++jt) + + + + if(fabs((double)(d[*jt]+a[*jt][*it])-d[*it])<1e-10) + + dp[*it]+=dp[u], dp[*it]%=104659; + + + + + + + + } + + + + + + } + + + + freopen("dmin.out","w",stdout); + + for(i=2;i<=n;++i)printf("%d ", dp[i]); + + + +} + +int main() + +{ + + read(); + + BF(); + + solve(); + + + + return 0; + +} diff --git a/distmin/bellman/ef203305-3db3-46f2-b04b-8ba7ab79a49b.cpp b/distmin/bellman/ef203305-3db3-46f2-b04b-8ba7ab79a49b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f674a04a43aee8f397565ce7703512d67c46eb1 --- /dev/null +++ b/distmin/bellman/ef203305-3db3-46f2-b04b-8ba7ab79a49b.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +const double INF = 100000.0; + + + +int N, M; + +int dp[1505], dpant[1505]; + +double cost[1505]; + +vector > V[1505]; + +queue Q; + +bool inQ[1505]; + + + +void bellman() + +{ + + for (int i = 1; i <= N; ++i) + + cost[i] = INF; + + cost[1] = 0.0; + + dp[1] = 1; + + Q.push(1); + + inQ[1] = true; + + + + while (!Q.empty()) + + { + + int now = Q.front(); + + Q.pop(); + + inQ[now] = false; + + + + dp[now] = dp[now] - dpant[now]; + + dpant[now] = dp[now]; + + + + for (vector >::iterator it = V[now].begin(); it != V[now].end(); ++it) + + { + + int vecin = it->first; + + double cvecin = it->second; + + if (cost[now] + it->second - cost[it->first] <= -eps) + + { + + cost[it->first] = cost[now] + it->second; + + dp[it->first] = dp[now]; + + dpant[it->first] = 0; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + else if (fabs(cost[now] + it->second - cost[it->first]) < eps) + + { + + dp[it->first] = (dp[it->first] + dp[now]) % MOD; + + if (!inQ[it->first]) + + { + + Q.push(it->first); + + inQ[it->first] = true; + + } + + } + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + for (int i = 1, nod1, nod2, c; i <= M; ++i) + + { + + fin >> nod1 >> nod2 >> c; + + double newc = log(double(c)); + + V[nod1].push_back(make_pair(nod2, newc)); + + V[nod2].push_back(make_pair(nod1, newc)); + + } + + + + bellman(); + + + + for (int i = 2; i <= N; ++i) + + fout << dp[i] << ' '; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/f094d887-9990-47cf-b547-97e999d4a0eb.cpp b/distmin/bellman/f094d887-9990-47cf-b547-97e999d4a0eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edc8225b5fbf5a34a535f7b9bbac6d8102d0d948 --- /dev/null +++ b/distmin/bellman/f094d887-9990-47cf-b547-97e999d4a0eb.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +inline double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/f5235d4c-5644-4781-a5da-0c90f6708149.cpp b/distmin/bellman/f5235d4c-5644-4781-a5da-0c90f6708149.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84cc83e8c309ef6c91cebf5f564a468f5d352a49 --- /dev/null +++ b/distmin/bellman/f5235d4c-5644-4781-a5da-0c90f6708149.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +long long n,m,nrd[1505]; + +long double dm[1505]; + +bool t[1505]; + + + +struct muchie + +{ int v; + + long long c; + +}; + + + +vectorg[1505]; + +vector::iterator it; + +queueq; + + + +void bellman() + +{ long long i,crt; + + for(i=1;i<=n;i++) + + dm[i]=1000000000; + + q.push(1); + + t[1]=1; + + dm[1]=0; + + nrd[1]=1; + + while(!q.empty() ) + + { crt=q.front(); + + q.pop(); + + t[crt]=0; + + for(it=g[crt].begin();it < g[crt].end();it++) + + { if(dm[it->v] - (dm[crt] + log(it->c) ) > 1e-10 ) + + { dm[it->v]=dm[crt] + log(it->c); + + nrd[it->v]=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + if(!t[it->v]) + + { q.push(it->v); + + t[it->v]=1; + + } + + } + + else if( (dm[crt] + log(it->c) - dm[it->v]) <= 1e-10) + + { nrd[it->v]+=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + } + + } + + } + + for(i=2;i<=n;i++) + + out<>n>>m; + + for(i=1;i<=m;i++) + + { in>>a>>b>>r; + + w.c=r; + + w.v=b; + + g[a].push_back(w); + + w.v=a; + + g[b].push_back(w); + + } + + in.close(); + + bellman(); + + out.close(); + + return 0; + +} diff --git a/distmin/bellman/f5ae48f4-ebe3-4e84-b3ec-45e00991ce0f.cpp b/distmin/bellman/f5ae48f4-ebe3-4e84-b3ec-45e00991ce0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edc8225b5fbf5a34a535f7b9bbac6d8102d0d948 --- /dev/null +++ b/distmin/bellman/f5ae48f4-ebe3-4e84-b3ec-45e00991ce0f.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +inline double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/f74a59d6-5346-4311-a1df-43fd371cac76.cpp b/distmin/bellman/f74a59d6-5346-4311-a1df-43fd371cac76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..925246e7692e731f9efb4d86da1cce9802a9c98c --- /dev/null +++ b/distmin/bellman/f74a59d6-5346-4311-a1df-43fd371cac76.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +int n, m; +int cate[1550], used[1550]; +double best[1550]; +const double EPS = 1e-9; +struct leg +{ + int y; + double c; + leg(int y = 0, double c = 0) : y(y), c(c) { } + bool operator ()(int a, int b) + { + return best[a] > best[b]; + } +}; +bool eq(double a, double b) +{ + return (a-b) >= -EPS && (a-b) <= EPS; +} +vector graf[1550]; +void citire() +{ + scanf("%d %d", &n, &m); + for (int i = 1; i <= m; i++) { + int x, y, rc; + scanf("%d %d %d", &x, &y, &rc); + double c = log(1.0*rc); + graf[x].push_back(leg(y, c)); + graf[y].push_back(leg(x, c)); + } +} +priority_queue, leg> heap; +void solve() +{ + for (int i = 1; i <= n; i++) best[i] = 5e100; + best[1] = 0, cate[1] = 1; + heap.push(1); + while (!heap.empty()) { + int top = heap.top(); + heap.pop(); + if (used[top]) + continue; + used[top] = 1; + for (auto v : graf[top]) { + if (eq(best[top] + v.c, best[v.y])) + cate[v.y] = (cate[v.y] + cate[top]) % 104659; + else if (best[top] + v.c < best[v.y]) { + best[v.y] = best[top] + v.c; + cate[v.y] = cate[top]; + heap.push(v.y); + } + } + } +} + +void afisare() +{ + for (int i = 2; i <= n; i++) + printf("%d ", cate[i]); +} + +int main() +{ + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + + citire(); + solve(); + afisare(); + + return 0; +} diff --git a/distmin/bellman/f843073b-7276-4905-98cc-f4307b02d508.cpp b/distmin/bellman/f843073b-7276-4905-98cc-f4307b02d508.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84cc83e8c309ef6c91cebf5f564a468f5d352a49 --- /dev/null +++ b/distmin/bellman/f843073b-7276-4905-98cc-f4307b02d508.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +long long n,m,nrd[1505]; + +long double dm[1505]; + +bool t[1505]; + + + +struct muchie + +{ int v; + + long long c; + +}; + + + +vectorg[1505]; + +vector::iterator it; + +queueq; + + + +void bellman() + +{ long long i,crt; + + for(i=1;i<=n;i++) + + dm[i]=1000000000; + + q.push(1); + + t[1]=1; + + dm[1]=0; + + nrd[1]=1; + + while(!q.empty() ) + + { crt=q.front(); + + q.pop(); + + t[crt]=0; + + for(it=g[crt].begin();it < g[crt].end();it++) + + { if(dm[it->v] - (dm[crt] + log(it->c) ) > 1e-10 ) + + { dm[it->v]=dm[crt] + log(it->c); + + nrd[it->v]=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + if(!t[it->v]) + + { q.push(it->v); + + t[it->v]=1; + + } + + } + + else if( (dm[crt] + log(it->c) - dm[it->v]) <= 1e-10) + + { nrd[it->v]+=nrd[crt]; + + if(nrd[it->v] > 104659) + + nrd[it->v]%=104659; + + } + + } + + } + + for(i=2;i<=n;i++) + + out<>n>>m; + + for(i=1;i<=m;i++) + + { in>>a>>b>>r; + + w.c=r; + + w.v=b; + + g[a].push_back(w); + + w.v=a; + + g[b].push_back(w); + + } + + in.close(); + + bellman(); + + out.close(); + + return 0; + +} diff --git a/distmin/bellman/f8f1c208-15be-4d65-805c-d65ecfdc8828.cpp b/distmin/bellman/f8f1c208-15be-4d65-805c-d65ecfdc8828.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8bf39190b423ce01e328d475dcd45b7117415d --- /dev/null +++ b/distmin/bellman/f8f1c208-15be-4d65-805c-d65ecfdc8828.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int MOD = 104659; + +const double eps = 1e-10; + +int n, m, S[1510]; + +double DM[1510]; + +vector < pair < int, double > > V[1510]; + +set < pair < double, int > > H; + + + +double Modul(double x) + +{ + + if (x < 0) return -x; + + return x; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int x, y, c, i = 1; i <= m; i ++) + + { + + fin >> x >> y >> c; + + V[x].push_back( { y, log2(c) } ); + + V[y].push_back( { x, log2(c) } ); + + } + + + + memset(DM, 127, sizeof(DM)); + + DM[1] = 0; + + S[1] = 1; + + H.insert( { 0, 1 } ); + + while (!H.empty()) + + { + + pair < double, int > aux = *H.begin(); + + H.erase(H.begin()); + + + + for (vector < pair < int, double > > :: iterator it = V[aux.second].begin(); it != V[aux.second].end(); it ++) + + { + + if (aux.first + it->second + eps < DM[it->first]) + + { + + DM[it->first] = aux.first + it->second; + + H.insert( { DM[it->first], it->first } ); + + S[it->first] = S[aux.second]; + + } + + else if (Modul(aux.first + it->second - DM[it->first]) <= eps) + + { + + S[it->first] += S[aux.second]; + + if (S[it->first] >= MOD) S[it->first] -= MOD; + + } + + } + + } + + + + for (int i = 2; i <= n; i ++) fout << S[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/bellman/f97fb20c-cb09-42ac-956a-6c9008ac415e.cpp b/distmin/bellman/f97fb20c-cb09-42ac-956a-6c9008ac415e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a880053e35e1048db729cf1f0ae29af98f25dd77 --- /dev/null +++ b/distmin/bellman/f97fb20c-cb09-42ac-956a-6c9008ac415e.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +deque Q; + +vector > G[1501]; + + + +double dist[1501]; + +int n, m, answer[1501]; + +bool inq[1501]; + + + +void bellmanford() + +{ + + int son, node, i; + + double edge; + + + + Q.push_back(1); + + inq[1] = 1; + + while(!Q.empty()) { + + node = Q.front(); + + Q.pop_front(); + + inq[node] = 0; + + for(i=0; i= 104659) answer[son] -= 104659; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); + + } + + continue; + + } + + if(dist[son] > dist[node] + edge) { + + dist[son] = dist[node] + edge; + + answer[son] = answer[node]; + + if(!inq[son]) { + + inq[son] = 1; + + Q.push_back(son); } } } } + +} + + + +int main() + +{ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + + + int i, x, y; + + double z; + + + + scanf("%d%d", &n, &m); + + for(i=1; i<=m; ++i) { + + scanf("%d%d%lf", &x, &y, &z); + + z = log10(z); + + G[x].push_back({y, z}); + + G[y].push_back({x, z}); + + } + + + + for(i=1; i<=n; ++i) + + dist[i] = (1e8); + + + + dist[1] = 0; + + answer[1] = 1; + + bellmanford(); + + + + for(i=2; i<=n; ++i) + + printf("%d ", answer[i]); + + + + return 0; + +} diff --git a/distmin/bellman/fa836304-191a-4417-b3e7-b903c6f542d3.cpp b/distmin/bellman/fa836304-191a-4417-b3e7-b903c6f542d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31359c895675faf0fa219bee51bfb314013f2394 --- /dev/null +++ b/distmin/bellman/fa836304-191a-4417-b3e7-b903c6f542d3.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +using namespace std; + +short int n,m,x,y,coada[1501*1501]; + +int z,nr[1501],rez[1501]; + +double d[1501]; + +bitset<1501> viz; + +const double eps=1e-8; + +const double eps1=eps*(-1); + +vectora[1501],ap(1501,0); + +vectorc[1501]; + +void citire() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%hd%hd",&n,&m); + + for (int i=1; i<=m; ++i) + + { + + scanf("%hd%hd%d",&x,&y,&z); + + double g=log10(z); + + a[x].push_back(y); + + c[x].push_back(g); + + a[y].push_back(x); + + c[y].push_back(g); + + } + +} + +void bf() + +{ + + int p=0,u=0; + + coada[u++]=1; + + nr[1]=1; + + viz[1]=1; + + while (u!=p) + + { + + x=coada[p++]; + + viz[x]=false; + + size_t g=a[x].size(); + + for (size_t i=0; i!=g; ++i) + + { + + y=a[x][i]; + + double aux=d[x]+c[x][i],diff=d[y]-aux; + + if (diff>=eps) + + { + + d[y]=aux; + + if (!viz[y]) + + { + + viz[y]=true; + + coada[u++]=y; + + } + + } + + } + + } + +} + +void afis() + +{ + + for (int i=2; i<=n; ++i) + + printf("%d ",nr[i]); + +} + +void df(int x) + +{ + + viz[x]=1; + + size_t g=a[x].size(); + + for (size_t i=0; i=eps1) + + { + + + + if (!viz[a[x][i]]) + + df(a[x][i]); + + nr[x]+=nr[a[x][i]]; + + if (nr[x]>=104659) + + nr[x]-=104659; + + } + + } + +} + +int main() + +{ + + citire(); + + for (int j=2; j<=n; ++j) + + d[j]=1<<30;; + + bf(); + + viz.reset(); + + nr[1]=1; + + for (int i=2; i<=n; ++i) + + if (!viz[i]) + + df(i); + + afis(); + + + + + + + + return 0; + +} diff --git a/distmin/bellman/faf6aca7-bbff-43d5-b3e9-b06f5e6e79d3.cpp b/distmin/bellman/faf6aca7-bbff-43d5-b3e9-b06f5e6e79d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cc27b6ad740b7b14ce9b16bf38185c1d01043d7 --- /dev/null +++ b/distmin/bellman/faf6aca7-bbff-43d5-b3e9-b06f5e6e79d3.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream f("dmin.in"); ofstream g("dmin.out"); + +inline double modul(double x) + +{ if(x<0) return -x; + + return x; + +} + +vector < pair < int,double > >G[1502]; + +queue Q; + +int n, m, nrd[1502]; + +double dmin[1502]; + +bool viz[1502]; + +void bellmanFord() + +{ int x, vecin; + + double cost; + + Q.push(1); + + viz[1]=1; + + while (!Q.empty()) + + { x=Q.front(); + + Q.pop(); + + for (int i=0;idmin[x]+cost) + + { dmin[vecin]=dmin[x]+cost; + + nrd[vecin]=nrd[x]; + + if(!viz[vecin]) + + { Q.push(vecin); viz[vecin]=1; } + + } + + else if(modul(dmin[vecin]-dmin[x]-cost)<=1e-8) nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + } + +} + +int main() + +{ double z,cost; + + f>>n>>m; + + for (int x,y,i=1;i<=m;i++) + + { f>>x>>y>>cost; + + z=log(cost); + + G[x].push_back(make_pair(y,z)); + + G[y].push_back(make_pair(x,z)); + + } + + nrd[1]=1; + + for (int i=2;i<=n;i++) dmin[i]=1000001; + + bellmanFord(); + + for(int i=2;i<=n;++i) g< +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("dmin.in"); + +ofstream cout("dmin.out"); + + + +const int MAXN = 1505, + + oo = ((1<<31)-1), + + MOD = 104659; + + + +vector< pair > Graph[MAXN]; + +vector< double > Distance(MAXN, (double)oo); + +bitset< MAXN > Used; + +queue< int > Q; + +map NRoads; + +int N, M; + + + +const inline double Absolute(double a) + +{ + + if(a > 0) + + return a; + + return -a; + +} + +const inline bool isequal(double a, double b) + +{ + + if( Absolute(a-b) <= 1e-10 ) + + return true; + + return false; + +} + + + +void Read_Data() + +{ + + cin >> N >> M; + + for(int i = 1 ; i <= M ; ++ i) + + { + + int X, Y, Z; + + cin >> X >> Y >> Z; + + Graph[X].push_back(make_pair(Y, log(Z))); + + } + + cin.close(); + +} + + + +void BellmanFord() + +{ + + for(Q.push(1) , Distance[1] = 0, NRoads[1] = 1, Used[1] = true; !Q.empty(); Q.pop() ) + + { + + int node = Q.front(); + + Used[node] = false; + + for(vector > :: iterator it = Graph[node].begin(), fin = Graph[node].end(); it != fin ; ++ it) + + {if(isequal(Distance[node] + it->second, Distance[it->first])) + + { + + NRoads[it->first] += NRoads[node]; + + if(NRoads[it->first] > MOD) + + NRoads[it->first] -= MOD; + + } + + else if(Distance[it->first] > it->second + Distance[node]) { + + Distance[it->first] = it->second + Distance[node]; + + NRoads[it->first] = NRoads[node]; + + if( !Used[it->first] ) + + { + + Used[it->first] = true; + + Q.push(it->first); + + } + + }} + + } + + + +} + +void Write_Data() + +{ + + for(int i = 2 ; i <= N; ++ i) + + cout << NRoads[i] << " "; + + cout << "\n"; + + cout.close(); + +} + +int main() + +{ + + Read_Data(); + + BellmanFord(); + + Write_Data(); + + return 0; + +} diff --git a/distmin/bellman/fbf44be2-ef90-4656-8b74-cf6de85e33eb.cpp b/distmin/bellman/fbf44be2-ef90-4656-8b74-cf6de85e33eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..329705bfcd12cde3d1be6c74bf407978b6f0a462 --- /dev/null +++ b/distmin/bellman/fbf44be2-ef90-4656-8b74-cf6de85e33eb.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +vector > g[1501]; + +int n, nrd[1501]; + +double d[1501]; + +void citire() + +{ + + int m, i, j; + + double c; + + fin>>n>>m; + + while(m) + + { + + m--; + + fin>>i>>j>>c; + + c=log(c); + + g[i].push_back(make_pair(j, c)); + + g[j].push_back(make_pair(i, c)); + + } + + fin.close(); + +} + +void bellmanFord() + +{ + + int i, k; + + bool viz[1501]={0}; + + queue q; + + vector > :: iterator it; + + for(i=2; i<=n; i++) + + d[i]=INT_MAX; + + nrd[1]=1; + + viz[1]=1; + + q.push(1); + + while(!q.empty()) + + { + + k=q.front(); + + q.pop(); + + for(it=g[k].begin(); it!=g[k].end(); it++) + + if(d[it->first]-1e-8>d[k]+it->second) + + { + + d[it->first]=d[k]+it->second; + + nrd[it->first]=nrd[k]; + + if(!viz[it->first]) + + q.push(it->first), viz[it->first]=1; + + } + + else if(abs(d[it->first]-d[k]-it->second)<=1e-8) + + nrd[it->first]=(nrd[it->first]+nrd[k])%104659; + + } + +} + +int main() + +{ + + citire(); + + bellmanFord(); + + for(int i=2; i<=n; i++) + + fout< +#include +using namespace std; + +const int N = 1505, M = 2505, INF = 100000, R = 104659; + +int vf[2 * M], urm[2 * M], lst[N], h[N], poz[N], nrh, nrm, nrd[N]; + +double c[2 * M], v[N]; + +void adauga (int x, int y, double cost) + +{ + + vf[++nrm] = y; + + c[nrm] = cost; + + urm[nrm] = lst[x]; + + lst[x] = nrm; + +} + +void schimba (int p1, int p2) + +{ + + int aux = h[p1]; + + h[p1] = h[p2]; + + h[p2] = aux; + + poz[h[p1]] = p1; + + poz[h[p2]] = p2; + +} + +void urca (int p) + +{ + + while (p > 1 && v[h[p]] < v[h[p>>1]]) + + { + + schimba (p, p >> 1); + + p >>= 1; + + } + +} + +void coboara (int p) + +{ + + int bun = p, fs = 2 * p, fd = 2 * p + 1; + + if (fs <= nrh && v[h[fs]] < v[h[bun]]) + + bun = fs; + + if (fd <= nrh && v[h[fd]] < v[h[bun]]) + + bun = fd; + + if (bun != p) + + { + + schimba (bun, p); + + coboara (bun); + + } + +} + +double make_abs (double x) + +{ + + if (x < 0) + + return -x; + + return x; + +} + +int main () + +{ + + FILE *in, *out; + + in = fopen ("dmin.in", "r"); + + out = fopen ("dmin.out", "w"); + + int n, m; + + fscanf (in, "%d%d", &n, &m); + + int i, x, y; + + double cost; + + for (i = 1; i <= m; i++) + + { + + fscanf (in, "%d%d%lf", &x, &y, &cost); + + cost = log(cost); + + adauga (x, y, cost); + + adauga (y, x, cost); + + } + + for (i = 1; i <= n; i++) + + { + + v[i] = INF; + + poz[i] = i; + + h[i] = i; + + } + + nrh = n; + + double eps = 0.00000001; + + v[1] = 0; + + nrd[1] = 1; + + int p; + + double aux; + + while (nrh > 0) + + { + + x = h[1]; + + schimba (1, nrh--); + + coboara (1); + + p = lst[x]; + + while (p != 0) + + { + + y = vf[p]; + + cost = c[p]; + + aux = cost + v[x]; + + if (v[y] - aux > eps) + + { + + v[y] = cost + v[x]; + + nrd[y] = nrd[x]; + + urca (poz[y]); + + } + + else + + if (make_abs (aux - v[y]) <= eps) + + { + + nrd[y] += nrd[x]; + + nrd[y] %= R; + + } + + p = urm[p]; + + } + + } + + for (i = 2; i <= n; i++) + + fprintf (out, "%d ", nrd[i]); + + return 0; + +} diff --git a/distmin/djikstra/0046bc80-02b7-11ec-bb8e-2cf05d84bfc2.cpp b/distmin/djikstra/0046bc80-02b7-11ec-bb8e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6735c7a39936b49a4cfaefe36ff3464619291928 --- /dev/null +++ b/distmin/djikstra/0046bc80-02b7-11ec-bb8e-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + + + +const int INF = 1e9; + +const int mod = 104659; + +const double eps = 1e-6; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + +priority_queue, vector >, greater > >pq; + +int n, m, nr[1502]; + +double dist[1502]; + +vector >G[1502]; + + + +void dijkstra () { + + for (int i = 1; i <= n; i++) + + dist[i] = 1.0 * INF; + + dist[1] = 0; + + pq.push({0.0, 1}); + + nr[1] = 1; + + while (!pq.empty()) { + + int x = pq.top().second; + + double c = pq.top().first; + + pq.pop(); + + if (dist[x] != c) + + continue; + + for (auto it : G[x]) { + + long long y = it.second; + + double cost = it.first; + + if (dist[y] - (dist[x] + cost) > eps) { + + dist[y] = dist[x] + cost; + + pq.push({dist[y], y}); + + nr[y] = nr[x]; + + } + + else if (abs(dist[y] - (dist[x] + cost)) <= eps) + + nr[y] = (nr[y] + nr[x]) % mod; + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) { + + int x, y, z; + + fin >> x >> y >> z; + + G[x].push_back({log2((double)z), y}); + + G[y].push_back({log2((double)z), x}); + + } + + dijkstra(); + + for (int i = 2; i <= n; i++) + + fout << nr[i] << " "; + + return 0; + +} diff --git a/distmin/djikstra/00da2865-02b7-11ec-b53c-2cf05d84bfc2.cpp b/distmin/djikstra/00da2865-02b7-11ec-b53c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c1a88ab5cb4e5af8b5b1d4ee718227680da0ad9 --- /dev/null +++ b/distmin/djikstra/00da2865-02b7-11ec-b53c-2cf05d84bfc2.cpp @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include + + + + +using namespace std;ifstream is("dmin.in");ofstream os("dmin.out");double D[1505];int N,M,Nr[1505];bitset<1505>B;vector>v[1505];struct CMP{bool operator()(int a,int b){return D[a]>D[b];}};priority_queue,CMP>Q;main(){is>>N>>M;for(int i=2;i<=N;++i)D[i]=(1<<30);double cost;for(int i=1,a,b,c;i<=M;++i){is>>a>>b>>c;cost=log(c);v[a].push_back({b,cost});v[b].push_back({a,cost});}Q.push(1);Nr[1]=1;for(int i,nod;!Q.empty();){i=Q.top();Q.pop();if(B[i])continue;B[i]=1;for(int j=0;jD[i]+cost){Nr[nod]=Nr[i];D[nod]=D[i]+cost;Q.push(nod);}}}for(int i=2;i<=N;++i)os< +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); +int n; + + + +vector < pair < int , double > > g[1510]; + +double d[1510]; + +int solutie[1510]; + +bool viz[1510]; + + + +struct cmp { + + bool operator()(int a, int b) { + + return d[a] > d[b]; + + } + +}; + + + +priority_queue < int, vector < int >, cmp > c; + + + +void read() { + + int m; + + fin >> n >> m; + + while(m--) { + + int x, y, cost; + + fin >> x >> y >> cost; + + g[x].push_back({y, (double) log2((double)cost)}); + + g[y].push_back({x, (double) log2((double)cost)}); + + } + +} + + + +void DJ(int nod) { + + + + for(int i=1; i<=n; i++) { + + d[i] = INT_MAX; + + } + + + + c.push(nod); + + d[nod] = 0; + + viz[nod] = 1; + + solutie[1] = 1; + + + + while(!c.empty()) { + + int nod = c.top(); + + c.pop(); + + viz[nod] = 0; + + + + for(size_t i = 0; i < g[nod].size(); i++) { + + int vecin = g[nod][i].first; + + double cost = g[nod][i].second; + + + + if(d[vecin] > d[nod] + cost || (d[vecin] < d[nod] + cost + 1e-9 && d[vecin] > d[nod] + cost - 1e-9)) { + + + + if(d[vecin] < d[nod] + cost + 1e-9 && d[vecin] > d[nod] + cost - 1e-9) { + + solutie[vecin] = (solutie[vecin] + solutie[nod]) % 104659; + + } else { + + solutie[vecin] = solutie[nod] % 104659; + + if(viz[vecin] == 0) { + + c.push(vecin); + + viz[vecin] = 1; + + } + + d[vecin] = d[nod] + cost; + + } + + } + + } + + } + +} + + + + + +void solve() { + + DJ(1); + + + + for(int i=2; i<=n; i++) { + + fout << solutie[i] % 104659 << " "; + + } + +} + + + +int main() { + + read(); + + solve(); + +} diff --git a/distmin/djikstra/0185826e-02b7-11ec-b4f8-2cf05d84bfc2.cpp b/distmin/djikstra/0185826e-02b7-11ec-b4f8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11e49960b35b7e8906d65d27e4fff21f7a647d5e --- /dev/null +++ b/distmin/djikstra/0185826e-02b7-11ec-b4f8-2cf05d84bfc2.cpp @@ -0,0 +1,166 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int nmax = 1505; + +int n, m, viz[nmax]; + +vector > g[nmax]; + +double d[nmax]; + +int dp[nmax]; + + + +inline void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int a, b, c; + + fin >> a >> b >> c; + + g[a].push_back({b, log10(1.0*c)}); + + g[b].push_back({a, log10(1.0*c)}); + + } + +} + + + +struct cmp + +{ + + bool operator()(int x,int y) + + { + + return d[x] - d[y] > 1e-8; + + } + +}; + + + +priority_queue , cmp> q; + + + +inline void dij() + +{ + + q.push(1); + + for (int i = 2; i <= n; i++) + + d[i] = 1e9; + + dp[1] = 1; + + d[1] = 0; + + viz[1] = 1; + + while (!q.empty()) + + { + + int nod = q.top(); + + q.pop(); + + viz[nod] = 0; + + for (int i = 0; i < g[nod].size(); i++) + + { + + int vecin = g[nod][i].first; + + double cost = g[nod][i].second; + + if (d[vecin] - (d[nod] + cost) > 1e-8) + + { + + d[vecin] = d[nod] + cost; + + dp[vecin] = dp[nod]; + + dp[vecin] %= 104659; + + if (!viz[vecin]) + + { + + viz[vecin] = 1; + + q.push(vecin); + + } + + } + + else if (abs(d[vecin] - (d[nod] + cost)) < 1e-8) + + dp[vecin] += dp[nod], dp[vecin] %= 104659; + + } + + } + +} + + + +inline void solve() + +{ + + dij(); + + for (int i = 2; i <= n; i++) + + fout << dp[i] << " "; + +} + + + +int main() + +{ + + read(); + + solve(); + + return 0; + +} diff --git a/distmin/djikstra/01cb7cfc-02b7-11ec-b0c3-2cf05d84bfc2.cpp b/distmin/djikstra/01cb7cfc-02b7-11ec-b0c3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00110459bff9b74049ac2f4d7652fc5c7e3bc166 --- /dev/null +++ b/distmin/djikstra/01cb7cfc-02b7-11ec-b0c3-2cf05d84bfc2.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +const int NMAX = 15010; + +const int MOD = 104659; + +const double EPS = 1e-10; + +int n, m; + +vector> graf[NMAX]; + +double dist[NMAX]; + +int counter[NMAX]; + +bool use[NMAX]; + +struct comp + +{ + + bool operator()(int a, int b) + + { + + return dist[a]>dist[b]; + + } + +}; + +void dij() + +{ + + priority_queue, comp> cue; + + cue.push(1); + + fill(dist, dist+NMAX, INT_MAX); + + dist[1] = 0; + + counter[1] = 1; + + use[1] = 1; + + while(cue.size()) + + { + + for(auto i:graf[cue.top()]) + + { + + double newLength = (double)dist[cue.top()]+ (double)i.second; + + if(dist[i.first]>newLength || (dist[i.first]newLength-EPS)) + + { + + if((dist[i.first]newLength-EPS)) + + { + + counter[i.first]=(counter[i.first]+counter[cue.top()])%MOD; + + } + + else + + { + + counter[i.first]=counter[cue.top()]%MOD; + + if(!use[i.first]) cue.push(i.first),use[i.first]=1; + + dist[i.first]=newLength; + + } + + } + + } + + use[cue.top()]=0; + + cue.pop(); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m--) + + { + + int a, b, c; + + fin>>a>>b>>c; + + graf[a].push_back({b,(double)log2((double)c)}); + + graf[b].push_back({a,(double)log2((double)c)}); + + } + + dij(); + + for(int i = 2;i<=n;i++) fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +vector >graf[1600]; + +double dr[1600]; + +int nrdr[1600]; + + + +struct nod + +{ + + int to; + + double dst; + + bool operator< (const nod &y) const + + { + + return (dst - y.dst) > 0.0000000001; + + } + +}; + + + +void dijkstra(int x, int n) + +{ + + priority_queue pq; + + pq.push({ x, 0.0 }); + + nrdr[x] = 1; + + for (int i = 2; i <= n; i++) + + dr[i] = 1000000; + + while (!pq.empty()) + + { + + int a; + + double dmin; + + dmin = pq.top().dst; + + a = pq.top().to; + + pq.pop(); + + for (auto &it : graf[a]) + + { + + int b; + + double w; + + tie(b, w) = it; + + if (fabs(dmin + w - dr[b]) <= 0.0000000001) + + { + + nrdr[b] = (nrdr[a] + nrdr[b]) % 104659; + + } + + else + + { + + if (w + dmin < dr[b]) + + { + + dr[b] = dmin + w; + + nrdr[b] = nrdr[a]; + + pq.push({ b, dr[b] }); + + } + + } + + } + + + + + + } + + + + + +} + + + + + +int main() + +{ + + long long n, m; + + fin >> n >> m; + + for (long long i = 0; i < m; i++) + + { + + int a, b; + + double w; + + fin >> a >> b >> w; + + graf[a].push_back({ b, log(w) }); + + graf[b].push_back({ a, log(w) }); + + } + + dijkstra(1, n); + + for (int i = 2; i <= n; i++) + + fout << nrdr[i] << ' '; + + + + + + + +} diff --git a/distmin/djikstra/02fcf029-02b7-11ec-8451-2cf05d84bfc2.cpp b/distmin/djikstra/02fcf029-02b7-11ec-8451-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22e6e91f255eb103987ab793e750727bdf51843b --- /dev/null +++ b/distmin/djikstra/02fcf029-02b7-11ec-8451-2cf05d84bfc2.cpp @@ -0,0 +1,217 @@ +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +struct Nod{ + + int nod; + + double val; + +}; + + + + + +int heap[50004]; + +int n; + +int getMin() + +{ + + return heap[1]; + +} + +double cost[10000]; + +void add(int x) + +{ + + + + n++; + + int poz=n; + + heap[n]=x; + + while(poz>1 && cost[heap[poz/2]]>cost[heap[poz]]) + + { + + swap(heap[poz/2], heap[poz]); + + poz/=2; + + } + +} + +void popMin() + +{ + + + + heap[1]=heap[n]; + + heap[n]=0; + + int poz=1; + + n--; + + while(2*poz<=n) + + { + + poz*=2; + + if(pozcost[heap[poz+1]]) poz++; + + if(cost[heap[poz]] < cost[heap[poz/2]]) + + swap(heap[poz/2], heap[poz]); + + else poz=n; + + } + +} +int mod=104659; + +vector muchii[100000]; + + + +int mn[10000]; + +int viz[10000]; + +int pct, much; + +int main() + +{ + + f>>pct>>much; + + for(int i=1;i<=much;++i) + + { + + int a,b; + + double x,c; + + f>>a>>b>>c; + + + + x=log(c); + + muchii[a].push_back({b,x}); + + muchii[b].push_back({a,x}); + + } + + for(int i=2;i<=pct;++i) + + { + + cost[i]=1e9; + + } + + int elem; + + add(1); + + cost[1]=0; + + viz[1]=1; + + mn[1]=1; + + + + while(n>0) + + { + + elem=getMin(); + + popMin(); + + viz[elem]=0; + + for(int i=0;i1e-8) + + { + + mn[vecin.nod]=mn[elem]%mod; + + cost[vecin.nod]=cost[elem]+vecin.val; + + + + if(viz[vecin.nod]==0) + + { + + viz[vecin.nod]=1; + + add(vecin.nod); + + } + + } + + else { + + if(abs(vecin.val+cost[elem]-cost[vecin.nod])<1e-8) + + { + + mn[vecin.nod]+=mn[elem]; + + mn[vecin.nod]%=mod; + + } + + } + + + + } + + } + + for(int i=2;i<=pct;++i) + + { + + g< +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int NMax = 1500, oo = 1e9, MOD = 104659; + +const double E = 1e-8; + + + +int N, M, DP[NMax + 5]; + + + +vector > G[NMax + 5]; + +priority_queue , vector >, greater > > Q; + + + +double D[NMax + 5]; + + + +void Dijkstra(int Nod) + +{ + + for(int i = 2; i <= N; i++) + + D[i] = oo; + + + + Q.push({0, 1}), DP[1] = 1; + + + + while(!Q.empty()) + + { + + Nod = Q.top().second; + + + + for(auto Vecin : G[Nod]) + + { + + int A = Vecin.first; + + double C = Vecin.second; + + + + if(D[A] == oo) + + D[A] = D[Nod] + C, Q.push({D[A], A}); + + else + + { + + if(fabs(D[A] + C - D[Nod]) < E) + + DP[Nod] = (DP[Nod] + DP[A]) % MOD; + + } + + } + + Q.pop(); + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1, a, b; i <= M; i++) + + { + + double c; + + fin >> a >> b >> c; c = log2(c); + + + + G[a].push_back({b, c}); + + G[b].push_back({a, c}); + + } + + Dijkstra(1); + + + + for(int i = 2; i <= N; i++) + + fout << DP[i] << " "; + + + + fout << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/distmin/djikstra/037c7ff5-02b7-11ec-880f-2cf05d84bfc2.cpp b/distmin/djikstra/037c7ff5-02b7-11ec-880f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d68ad29e54060c866c8de813aa7d1ba85b6b50b --- /dev/null +++ b/distmin/djikstra/037c7ff5-02b7-11ec-880f-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +const int NMAX = 1501, + + MOD = 104659; + +const double EPS = 1e-10; + + + +struct muchie { + + int y; + + double cst; + +}; + + + +class cmp { + + public: bool operator()(const muchie& a, const muchie& b) { + + return a.cst > b.cst; + + } + +}; + + + +double distMin[NMAX]; + +int nrDrum[NMAX]; + +bool v[NMAX]; + +vector A[NMAX]; + +priority_queue, cmp> PQ; + +int N, M; + + + +void dijkstra() { + + PQ.push({1, 0}); + + for(int i = 2; i <= N; i++) + + distMin[i] = (1 << 25); + + + + nrDrum[1] = 1; + + distMin[1] = 0.0; + + while(!PQ.empty()) { + + int U = PQ.top().y; + + + + PQ.pop(); + + if(v[U]) continue; + + + + for(const auto& vec: A[U]) { + + int V = vec.y; + + double W = vec.cst; + + if(distMin[V] - distMin[U] - W > EPS) { + + distMin[V] = distMin[U] + W; + + nrDrum[V] = nrDrum[U] % MOD; + + PQ.push({V, distMin[V]}); + + } else if(abs(distMin[V] - distMin[U] - W) <= EPS) + + nrDrum[V] = (nrDrum[U] + nrDrum[V]) % MOD; + + } + + v[U] = 1; + + } + +} + + + +int main() + +{ + + in >> N >> M; + + + + int x, y; + + double c; + + for(int i = 1; i <= M; i++) { + + in >> x >> y >> c; + + c = log(c); + + A[x].push_back({y, c}); + + A[y].push_back({x, c}); + + } + + + + dijkstra(); + + + + for(int i = 2; i <= N; i++) + + out << nrDrum[i] << ' '; + + return 0; + +} diff --git a/distmin/djikstra/0399010c-02b7-11ec-a33b-2cf05d84bfc2.cpp b/distmin/djikstra/0399010c-02b7-11ec-a33b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b71417fbd942b803c7cdb5c80540a5ad065c009 --- /dev/null +++ b/distmin/djikstra/0399010c-02b7-11ec-a33b-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +const int MAXN = 1510, MOD = 104659; + +const long double INF = 0x3f3f3f3f, EPS = 0.0000000001; + + + +struct Node { + + int node; + + long double cost; + + bool operator<(const Node& other) const { + + if (cost != other.cost) + + return cost < other.cost; + + return node < other.node; + + } + +}; + + + +long double dist[MAXN]; + +int way[MAXN], n, m; + +vector edges[MAXN]; + +set heap; + + + +void initialize() { + + for (int i = 1; i <= n; ++i) + + dist[i] = INF; + +} + + + +void dijkstra() { + + initialize(); + + dist[1] = 0; + + way[1] = 1; + + heap.insert({1, 0}); + + while(!heap.empty()) { + + int node = heap.begin()->node; + + heap.erase(heap.begin()); + + for (const auto& it: edges[node]) + + if (abs(dist[node] + it.cost - dist[it.node]) < EPS) + + way[it.node] += way[node], way[it.node] %= MOD; + + else if (dist[node] + it.cost < dist[it.node]) { + + if (dist[it.node] != INF) + + heap.erase({it.node, dist[it.node]}); + + dist[it.node] = dist[node] + it.cost; + + way[it.node] = way[node]; + + heap.insert({it.node, dist[it.node]}); + + } + + } + +} + + + +void read() { + + fin >> n >> m; + + for (int i = 0; i < m; ++i) { + + int x, y, cost; + + long double ln; + + fin >> x >> y >> cost; + + ln = log10(cost); + + edges[x].push_back({y, ln}); + + edges[y].push_back({x, ln}); + + } + +} + + + +void print() { + + for (int i = 2; i <= n; ++i) + + fout << way[i] << ' '; + +} + + + +int main() { + + read(); + + dijkstra(); + + print(); + + return 0; + +} diff --git a/distmin/djikstra/03e348fe-02b7-11ec-a230-2cf05d84bfc2.cpp b/distmin/djikstra/03e348fe-02b7-11ec-a230-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cbce39ee8b008159b4db00f4dacb731b0d5784b --- /dev/null +++ b/distmin/djikstra/03e348fe-02b7-11ec-a230-2cf05d84bfc2.cpp @@ -0,0 +1,104 @@ +#include +using namespace std ; + +const int NR = 1505 ; + +const long double oo = 100000 ; + +const double ER = 0.000000001; + +ifstream in ("dmin.in") ; + +ofstream out ("dmin.out") ; + +vector < pair < int , long double > > v [ NR ] ; + +vector < int > way ( NR , 0 ) ; + +vector < long double > d ( NR , oo ) ; + +int n , m ; + +struct cmp { + + inline bool operator() ( const pair < int , long double > i , const pair < int , long double > j ) { + + return i.second > j.second ; + + } + +}; + +priority_queue < pair < int , long double > , vector < pair < int , long double > > , cmp > q ; + +void dijkstra ( ) { + + int nod ; + + long double cost ; + + q.push( make_pair ( 1 , 0 ) ) ; + + d [ 1 ] = 0 ; + + way [ 1 ] = 1 ; + + vector < pair < int , long double > > :: iterator it ; + + while ( !q.empty() ) { + + nod = q.top().first; + + cost = q.top().second ; + + q.pop() ; + + if ( d [ nod ] != cost ) continue ; + + for ( it = v [ nod ].begin() ; it != v [ nod ].end() ; ++ it ) { + + if ( d [ nod ] + (*it).second == d [ (*it).first ] || abs ( d [ nod ] + (*it).second - d [ (*it).first ] ) < ER ) { + + way [ (*it).first ] += way [ nod ] ; + + if ( way [ (*it).first ] > 104659 ) way [ (*it).first ] -= 104659 ; + + } else + + if ( d [ nod ] + (*it).second < d [ (*it).first ] ) { + + d [ (*it).first ] = d [ nod ] + (*it).second ; + + q.push( make_pair ( (*it).first , d [ (*it).first ] ) ) ; + + way [ (*it).first ] = way [ nod ] ; + + } + + } + + } + +} + +int main () { + + int i , j , x , y , z ; + + in >> n >> m ; + + while ( m -- ) { + + in >> x >> y >> z ; + + v [ x ].push_back ( make_pair ( y , log ( z ) ) ) ; + + v [ y ].push_back ( make_pair ( x , log ( z ) ) ) ; + + } + + dijkstra() ; + + for ( i = 2 ; i <= n ; ++ i ) out << way [ i ] << ' ' ; + +} diff --git a/distmin/djikstra/04008587-02b7-11ec-b7dc-2cf05d84bfc2.cpp b/distmin/djikstra/04008587-02b7-11ec-b7dc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..985d64d69a8c4174b78e1dddf1eddc17dd62ea5a --- /dev/null +++ b/distmin/djikstra/04008587-02b7-11ec-b7dc-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const int NMAX = 1510; + +const double EPS = 1e-10; + +const double INF = (1<<25); + + + +int n, m; + +double dist[NMAX]; + +int nrDrum[NMAX]; + +vector> G[NMAX]; + + + +void Dijkstra() + +{ + + for(int i = 2; i <= n; i++) + + dist[i] = INF; + + set> Q; + + Q.insert(make_pair(0, 1)); + + nrDrum[1] = 1; + + while(!Q.empty()) + + { + + int node = Q.begin()->second; + + double d = Q.begin()->first; + + Q.erase(Q.begin()); + + for(auto it : G[node]) + + { + + int to = it.first; + + double cost = it.second; + + if(dist[to] > dist[node] + cost + EPS) + + { + + if(abs(dist[to] - INF) > EPS) + + Q.erase(Q.find(make_pair(dist[to], to))); + + dist[to] = dist[node] + cost; + + nrDrum[to] = nrDrum[node] % MOD; + + Q.insert(make_pair(dist[to], to)); + + } + + else if(abs(dist[to] - cost - dist[node]) <= EPS) + + nrDrum[to] = (nrDrum[to] + nrDrum[node]) % MOD; + + } + + } + + + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + int to, from, cost; + + fin>>from>>to>>cost; + + double c = log(cost); + + G[to].push_back(make_pair(from, c)); + + G[from].push_back(make_pair(to, c)); + + } + + Dijkstra(); + + for(int i = 2; i <= n; i++) + + fout< +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +double EPS=1e-8; + +double d[1505]; + +int cnt[1505]; + +bool in[1505]; + +struct cmp{ + + bool operator()(int x, int y) + + { + + return d[x]-d[y]>EPS; + + } + +}; + + + +int n, m; + +vector > v[1505]; + +priority_queue , cmp> Q; + + + +void dijkstra() + +{ + + for (int i = 2; i <= n; i++) + + d[i]=0x3f3f3f3f; + + + + cnt[1]=1; + + d[1]=0; + + in[1]=1; + + Q.push(1); + + while(!Q.empty()) + + { + + int x=Q.top(); + + in[x]=0; + + Q.pop(); + + for (int i = 0; i < v[x].size(); i++) + + { + + int y=v[x][i].first; + + double c=v[x][i].second; + + if(d[y]-d[x]-c>EPS) + + { + + cnt[y]=cnt[x]%104659; + + d[y]=d[x]+c; + + if(in[y] == 0) + + { + + Q.push(y); + + in[y]=1; + + } + + } + + else + + { + + if(abs(d[y]-d[x]-c)> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int x, y; + + long double c; + + f >> x >> y >> c; + + v[x].push_back({y, log2(1.0*c)}); + + v[y].push_back({x, log2(1.0*c)}); + + } + + + + dijkstra(); + + + + return 0; + +} diff --git a/distmin/djikstra/046709d3-02b7-11ec-9b6f-2cf05d84bfc2.cpp b/distmin/djikstra/046709d3-02b7-11ec-9b6f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca034e2d980018f119afecb61c7c82356f1a54a6 --- /dev/null +++ b/distmin/djikstra/046709d3-02b7-11ec-9b6f-2cf05d84bfc2.cpp @@ -0,0 +1,207 @@ +#include +using namespace std; + + + +typedef long long ll; + +typedef pair pii; + +typedef pair pll; + +typedef vector vi; + +typedef vector vii; + +typedef vector vl; + +typedef vector vll; + +typedef queue qi; + +typedef queue qii; + +typedef queue ql; + +typedef queue qll; + + + +const int INF = 1000000000; + +const int MOD = 104659; + +const double EPSILON = 1e-10; + +const int NMAX = 1500 + 5; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +struct edge + +{ + + int node; + + double cost; + + + + edge() + + { + + node = cost = 0; + + } + + + + edge(int _node, double _cost) + + { + + node = _node; + + cost = _cost; + + } + + + + bool operator <(const edge &arg) const + + { + + return cost > arg.cost; + + } + +}; + + + +vector graph[NMAX]; + +priority_queue pq; + +int n, m; + +int ans[NMAX]; + +double drum[NMAX]; + +bool vis[NMAX]; + + + +void dijkstra(int start) + +{ + + for (int i = 1; i < NMAX; ++i) + + drum[i] = INF; + + + + drum[start] = 0; + + ans[start] = 1; + + pq.push(edge(start, 0)); + + + + while (!pq.empty()) + + { + + edge nod = pq.top(); + + pq.pop(); + + if (nod.cost > drum[nod.node]) continue; + + for (edge next: graph[nod.node]) + + if (drum[next.node] - EPSILON > nod.cost + next.cost) + + { + + drum[next.node] = nod.cost + next.cost; + + ans[next.node] = ans[nod.node]; + + pq.push(edge(next.node, drum[next.node])); + + } + + else if (fabs(drum[next.node] - nod.cost - next.cost) <= EPSILON) + + ans[next.node] = (ans[next.node] + ans[nod.node]) % MOD; + } + +} + + + +void write() + +{ + + for (int i = 2; i <= n; ++i) + + fout << ans[i] << " "; + +} + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + int x, y; + + int cost; + + fin >> x >> y >> cost; + + graph[x].push_back(edge(y, log(1.0 * cost))); + + graph[y].push_back(edge(x, log(1.0 * cost))); + + } + +} + + + +int main() + +{ + + read(); + + dijkstra(1); + + write(); + + + + return 0; + +} diff --git a/distmin/djikstra/047d5632-02b7-11ec-ae95-2cf05d84bfc2.cpp b/distmin/djikstra/047d5632-02b7-11ec-ae95-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acc04e7bd4090a2a1ba7de47ef21c9c43bbc1c2a --- /dev/null +++ b/distmin/djikstra/047d5632-02b7-11ec-ae95-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include +int N, M; + +std::vector > ADC[1505]; + + + +inline void AddEdge(int x, int y, long double w) { + + ADC[x].push_back({log(w), y}); + + ADC[y].push_back({log(w), x}); + +} + + + +bool Equal(long double x, long double y) { + + return std::abs(x-y) < 1e-9L; + +} + + + +std::ifstream In("dmin.in"); + +std::ofstream Out("dmin.out"); + + + +std::priority_queue , std::vector >, std::greater >> PQ; + +long double Dist[1505]; + +int DP[1505]; + + + +void Dijkstra() { + + for (int i=1; i<=N; ++i) + + Dist[i] = std::numeric_limits::infinity(); + + Dist[1] = 0; + + DP[1] = 1; + + PQ.push({0, 1}); + + + + std::pair Top; + + while (!PQ.empty()) { + + Top = PQ.top(); + + PQ.pop(); + + + + if (!Equal(Top.first, Dist[Top.second])) continue; + + for (auto Edge:ADC[Top.second]) { + + if (Equal(Dist[Edge.second], Dist[Top.second] + Edge.first)) + + DP[Edge.second] += DP[Top.second]; + + else if (Dist[Edge.second] > Dist[Top.second] + Edge.first) + + Dist[Edge.second] = Dist[Top.second] + Edge.first, + + DP[Edge.second] = DP[Top.second], + + PQ.push({Dist[Edge.second], Edge.second}); + + + + DP[Edge.second] %= 104659; + + } + + } + +} + + + +void Citire() { + + In >> N >> M; + + for (int i=0, x, y, w; i> x >> y >> w, AddEdge(x, y, (long double)(w)); + +} + + + +void Rezolvare() { + + Dijkstra(); + + for (int i=2; i<=N; ++i) + + Out << DP[i] << ' '; + +} + + + +int main() + +{ + + Citire(); + + Rezolvare(); + + + + return 0; } diff --git a/distmin/djikstra/05a1161b-02b7-11ec-992e-2cf05d84bfc2.cpp b/distmin/djikstra/05a1161b-02b7-11ec-992e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e610c13e327db5ec1f5ce2a6284bdea4a353c49a --- /dev/null +++ b/distmin/djikstra/05a1161b-02b7-11ec-992e-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +const int NMAX = 1505; + +const double EPS = 0.0000000001; + +const double INF = 1000000000; + +const int MOD = 104659; + +vector > v[NMAX]; + + + +struct Data { + + int node; + + double cost; + + bool operator< (Data other) const { + + return cost > other.cost; + + } + +}; + + + +priority_queue hp; + +double best[NMAX]; + +int dp[NMAX]; + + + +void dijkstra() { + + for (int i = 0; i < NMAX; i ++) + + best[i] = 1000000000; + + hp.push({1, 0}); + + best[1] = 0; + + dp[1] = 1; + + while (hp.size()) { + + Data s = hp.top(); + + hp.pop(); + + for (auto i : v[s.node]) { + + if (best[i.first] - EPS > (s.cost + i.second)) { + + best[i.first] = s.cost + i.second; + + dp[i.first] = dp[s.node]; + + hp.push({i.first, best[i.first]}); + + } else if (fabs(best[i.first] - s.cost - i.second) <= EPS) { + + dp[i.first] += dp[s.node]; + + if (dp[i.first] >= MOD) + + dp[i.first] -= MOD; + + } + + } + + } + +} + + + + + +int main() { + + ios::sync_with_stdio(false); + + int n, m; + + in >> n >> m; + + for (int i = 1; i <= m; i ++) { + + int x, y; + + double c; + + in >> x >> y >> c; + + v[x].push_back({y, log10(c)}); + + v[y].push_back({x, log10(c)}); + + } + + dijkstra(); + + for (int i = 2; i <= n; i ++) + + out << dp[i] << " "; + + return 0; + +} diff --git a/distmin/djikstra/060e2edb-02b7-11ec-a692-2cf05d84bfc2.cpp b/distmin/djikstra/060e2edb-02b7-11ec-a692-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e9c6265f34c3a611e46033776e8d091f466668a --- /dev/null +++ b/distmin/djikstra/060e2edb-02b7-11ec-a692-2cf05d84bfc2.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fi ("dmin.in"); +ofstream fo ("dmin.out"); +double dst[1501]; +int n, m, a, b, c, val[1501]; +struct str +{ + int n; + double c; + bool operator < (const str &other) const + { + return c - other.c > 0.0000001; + } +} x; +priority_queue pq; +vector v[1501]; + +void dijkstra() +{ + pq.push({1, 0}); + dst[1] = 0; + while (!pq.empty()) + { + x = pq.top(); + pq.pop(); + if (dst[x.n] < x.c - 0.0000001) + continue; + for (auto i:v[x.n]) + { + if ( - i.c - x.c + dst[i.n] > 0.0000001) + { + dst[i.n] = i.c + x.c; + pq.push({i.n, dst[i.n]}); + } + } + } +} + +void recalibrate() +{ + val[1] = 1; + for (int i = 2; i <= n; ++i) + for (auto j:v[i]) + if (abs(dst[i] - dst[j.n] - j.c) <= 0.0000001) + val[i] = (val[j.n] + val[i])%104659; +} + +int main() +{ + fi >> n >> m; + for (int i = 1; i <= n; ++i) + dst[i] = 0x3f3f3f3f; + for (int i = 1; i <= m; ++i) + { + fi >> a >> b >> c; + v[a].push_back({b, log10(c)}); + v[b].push_back({a, log10(c)}); + } + dijkstra(); + recalibrate(); + for (int i = 2; i <= n; ++i) + fo << val[i]%104659 << ' '; + return 0; +} diff --git a/distmin/djikstra/062515c3-02b7-11ec-a4d7-2cf05d84bfc2.cpp b/distmin/djikstra/062515c3-02b7-11ec-a4d7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adb484ae6141182fa17fbc948203bc0c73c5f803 --- /dev/null +++ b/distmin/djikstra/062515c3-02b7-11ec-a4d7-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +vector < pair > mv[1505]; + +priority_queue < pair, vector< pair >, greater< pair > > q; + +double cost[1505]; + +int viz[1505]; + +int answer[1505]; + +double EPS=1e-10; + + + +void dijkstra(int s){ + + int i,x; + + + + answer[s]=1; + + q.push(make_pair(0.0,s)); + + + + while(!q.empty()){ + + x=q.top().second; + + q.pop(); + + + + if(viz[x]) + + continue; + + + + viz[x]=1; + + + + for(i=0;i=EPS){ + + cost[mv[x][i].second]=cost[x]+mv[x][i].first; + + answer[mv[x][i].second]=answer[x]; + + q.push(make_pair(cost[mv[x][i].second],mv[x][i].second)); + + } + + else if(abs(cost[mv[x][i].second]-(cost[x]+mv[x][i].first))>n>>m; + + + + for(i=2;i<=n;i++) + + cost[i]=1000000; + + + + for(i=1;i<=m;i++){ + + fin>>x>>y>>c; + + + + c1=log2(1.0*c); + + + + mv[x].push_back(make_pair(c1,y)); + + mv[y].push_back(make_pair(c1,x)); + + } + + + + dijkstra(1); + + + + for(i=2;i<=n;i++) + + fout< +#include +#include +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + +const int N = 1502, R = 104659, M = 1999999999; + +const double ER = 0.000000001; + +int poz[N], h[N], pos[N]; + +double d[N]; + +vector > v[N]; + +bool egal(double a, double b){ + + return(abs(a - b) < ER); + +} + +bool comp(double a, double b){ + + return((a - b) > ER); + +} + +void upHeap(int f){ + + while(f/2 && comp(d[h[f/2]], d[h[f]])){ + + swap(poz[h[f/2]], poz[h[f]]); + + swap(h[f/2], h[f]); + + f /= 2; + + } + +} + +void insertHeap(int val, int &l){ + + h[++l] = val; + + poz[val] = l; + + upHeap(l); + +} + +void downHeap(int l){ + + int f = 0, t = 1; + + while(t != f){ + + f = t; + + if(f*2 <= l && comp(d[h[t]], d[h[f*2]])) + + t = f*2; + + if(f*2+1 <= l && comp(d[h[t]], d[h[f*2+1]])) + + t = f*2+1; + + swap(poz[h[t]],poz[h[f]]); + + swap(h[t],h[f]); + + } + +} + +int extractTop(int &l){ + + int rad = h[1]; + + poz[rad] = 0; + + swap(h[1],h[l--]); + + poz[h[1]] = 1; + + downHeap(l); + + return rad; + +} + +void dijkstra(int n){ + + int l = 0, rad, sz, nod; + + double c; + + for(int i=2;i<=n;i++) + + d[i] = M; + + pos[1] = 1; + + insertHeap(1,l); + + while(l){ + + rad = extractTop(l); + + sz = v[rad].size(); + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++){ + + in>>x>>y>>z; + + v[x].push_back({y,log(z)}); + + v[y].push_back({x,log(z)}); + + } + + in.close(); + + dijkstra(n); + + for(int i=2;i<=n;i++) + + out< +using namespace std; + + + + + + +int n, m; +vector >graph[1505]; +double dist[1505]; +int dp[1505]; + +void read() { + scanf("%d %d ", &n, &m); + + for (int i = 1; i <= m; i++) { + int x, y, z; + scanf("%d %d %d ", &x, &y, &z); + double logz = log(z); + graph[x].push_back({y, logz}); + graph[y].push_back({x, logz}); + } +} + +void dijkstra() { + fill(dist, dist + n + 1, 0x3f3f3f3f); + + dist[1] = 1; + + priority_queue, vector >, greater > > q; + + q.push({0, 1}); + + while (!q.empty()) { + int node = q.top().second; + q.pop(); + + for (auto it: graph[node]) { + if (dist[node] + it.second < dist[it.first]) { + dist[it.first] = dist[node] + it.second; + q.push({it.second, it.first}); + } + } + } +} + +void solve() { + dp[1] = 1; + for (int i = 2; i <= n; i++) { + for (auto it: graph[i]) { + int j = it.first; + if(abs(dist[i] - (dist[j] + it.second)) < 0.000000001) + dp[i] = (dp[i] + dp[j]) % 104659; + } + } +} + +void print() { + for (int i = 2; i <= n; i++) + printf("%d ", dp[i]); +} + +int main() { + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + + read(); + dijkstra(); + solve(); + print(); + + return 0; +} diff --git a/distmin/djikstra/072cda18-02b7-11ec-8e6b-2cf05d84bfc2.cpp b/distmin/djikstra/072cda18-02b7-11ec-8e6b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09bc56c30b3918f192955855cfa86157fd9afaa7 --- /dev/null +++ b/distmin/djikstra/072cda18-02b7-11ec-8e6b-2cf05d84bfc2.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fi("dmin.in"); + +ofstream fo("dmin.out"); + +int n,m; + +vector > V[1501]; + +set > S; + +long double D[1501]; + +int P[1501]; + +void dijkstra() + +{ + + S.insert(make_pair(D[1],1)); + + while(!S.empty()) + + { + + pairaux=(*S.begin()); + + int nod=aux.second; + + S.erase(S.begin()); + + for(auto it:V[nod]) + + { + + int to=it.first; + + long double cost=log10(it.second); + + if(abs(D[nod]+cost-D[to])<=0.00000001) + + P[to]=(P[to]+P[nod])%104659; + + else if(D[nod]+cost0.00000001) + + S.erase(S.find(make_pair(D[to],to))); + + D[to]=D[nod]+cost; + + P[to]=P[nod]%104659; + + S.insert(make_pair(D[to],to)); + + } + + } + + } + +} + +int main() + +{ + + fi>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b,c; + + fi>>a>>b>>c; + + V[a].push_back(make_pair(b,c)); + + V[b].push_back(make_pair(a,c)); + + } + + for(int i=1;i<=n;i++) + + D[i]=100000000.0; + + D[1]=0,P[1]=1; + + dijkstra(); + + for(int i=2;i<=n;i++) + + fo< + + + + + + +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +int n,m,a,b,di; + +bool mat[1505][1505]; + +int dist[1505][1505]; + +vectornod[1505]; + +vectorcost[1505]; + +double lg[1505]; + +int nrways[1505]; + +bool viz[1505]; + +dequed; + +void dijkstra() + +{ + + while(!d.empty()) + + { + + int ro=d[0]; + + for(int j=0;j1e-8) + + { + + lg[nod[ro][j]]=zi; + + d.push_back(nod[ro][j]); + + } + + } + + d.pop_front(); + + } + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1;i<=m;++i) + + { + + f>>a>>b>>di; + + nod[a].push_back(b); + + nod[b].push_back(a); + + mat[a][b]=mat[b][a]=1; + + dist[b][a]=dist[a][b]=di; + + cost[a].push_back(di); + + cost[b].push_back(di); + + } + + viz[1]=1; + + nrways[1]=1; + + d.push_back(1); + + dijkstra(); + + for(int i=2;i<=n;++i) + + for(int j=1;j<=n;++j) + + if(mat[i][j]==1) + + if(abs(lg[i]-(lg[j]+log10(dist[i][j])))<=1e-8) + + nrways[i]=(nrways[i]+nrways[j])%104659; + + for(int i=2;i<=n;++i) + + g< +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int nmax=1505; + +const int mod=104659; + +const long double inf=1e8; + +const long double dif=1e-10; + + + +typedef pair< long double, int > pdi; + +typedef pair< int,long double > pid; + +priority_queue , greater > Q; + +vector G[nmax]; + +long double d[nmax]; + +int n,m,r[nmax]; + + + + + +void dijkstra(int node){ + + for(int i=1;i<=n;++i)d[i]=inf; + + r[node]=1; + + d[node]=0; + + Q.push({0,node}); + + while(!Q.empty()){ + + long double dist=Q.top().first; + + int node_st=Q.top().second; + + Q.pop(); + + if(d[node_st]=dif){ + + d[edge.first]=d[node_st]+edge.second; + + r[edge.first]=r[node_st]; + + Q.push({d[edge.first],edge.first}); + + } + + else if(abs(d[edge.first]-(d[node_st]+edge.second))>n>>m; + + for(int i=1;i<=m;++i){ + + fin>>a>>b>>c; + + long double l=log10(c); + + G[a].push_back({b,l}); + + G[b].push_back({a,l}); + + } + + dijkstra(1); + + for(int i=2;i<=n;++i)fout< +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int nmax=1505; + +const int mod=104659; + +const long double inf=1e8; + +const long double eps=1e-10; + + + +typedef pair< long double, int > pdi; + +typedef pair< int,long double > pid; + +priority_queue , greater > Q; + +vector G[nmax]; + +long double d[nmax]; + +int n,m,sol[nmax]; + +bitset bit; + + + +long double ab(long double x){ + + return (x>=0?x:-x); + +} + + + +void dijkstra(){ + + sol[1]=1; + + for(int i=2;i<=n;++i)d[i]=inf; + + d[1]=0; + + Q.push({0,1}); + + while(!Q.empty()){ + + int node=Q.top().second; + + long double nnnn=Q.top().first; + + Q.pop(); + + if(nnnn>d[node])continue; + + for(auto edge: G[node]){ + + int nod=edge.first; + + long double dist=edge.second; + + if((d[nod]-d[node]-dist)>eps){ + + d[nod]=d[node]+dist; + + Q.push({d[nod],nod}); + + sol[nod]=sol[node]; + + } + + else if(ab(d[nod]-d[node]-dist)>n>>m; + + for(int i=1;i<=m;++i){ + + fin>>a>>b>>c; + + long double l=log10(c); + + G[a].push_back({b,l}); + + G[b].push_back({a,l}); + + } + + dijkstra(); + + for(int i=2;i<=n;++i)fout< +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int nmax=1505; + +const int mod=104659; + +const long double inf=1e8; + +const long double eps=1e-10; + + + +typedef pair< long double, int > pdi; + +typedef pair< int,long double > pid; + +priority_queue , greater > Q; + +vector G[nmax]; + +long double d[nmax]; + +int n,m,sol[nmax]; + +bitset bit; + + + +long double ab(long double x){ + + return (x>=0?x:-x); + +} + + + +void dijkstra(){ + + sol[1]=1; + + for(int i=2;i<=n;++i)d[i]=inf; + + d[1]=0; + + Q.push({0,1}); + + while(!Q.empty()){ + + int node=Q.top().second; + + Q.pop(); + + if(!bit[node]){ + + bit[node]=1; + + for(auto edge: G[node]){ + + int nod=edge.first; + + long double dist=edge.second; + + if((d[nod]-d[node]-dist)>eps){ + + d[nod]=d[node]+dist; + + Q.push({d[nod],nod}); + + sol[nod]=sol[node]; + + } + + else if(ab(d[nod]-d[node]-dist)>n>>m; + + for(int i=1;i<=m;++i){ + + fin>>a>>b>>c; + + long double l=log10(c); + + G[a].push_back({b,l}); + + G[b].push_back({a,l}); + + } + + dijkstra(); + + for(int i=2;i<=n;++i)fout< +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +const int Const = 104659; + + + +inline int Modulus (int number){ + + return number % Const; + +} + + + +inline double make_abs (double number){ + + return (number < 0 ? -number : number); + +} + + + +vector < pair > v[1505]; + +priority_queue < pair , vector < pair >, greater < pair > > pq; + + + +int n, m; + +double dist[1505]; + +int sol[1505], sptSet[1505]; + + + +void dijkstra(){ + + + + for(int i = 2; i <= n; ++i) dist[i] = 0x3f3f3f3f; + + + + sol[1] = 1; + + dist[1] = 0; + + sptSet[1] = 1; + + pq.push(make_pair(1, dist[1])); + + + + while(!pq.empty()){ + + + + int u = pq.top().first; + + pq.pop(); + + + + for(int i = 0; i < v[u].size(); ++i){ + + + + int nod = v[u][i].first; + + double distance = v[u][i].second; + + + + if(dist[u] + distance < dist[nod] - 1e-8){ + + dist[nod] = dist[u] + distance; + + sol[nod] = sol[u]; + + if(!sptSet[nod]) + + pq.push(make_pair(nod, dist[nod])), sptSet[nod] = 1; + + + + } + + + + else if (make_abs(dist[nod] - dist[u] - distance) <= 1e-8){ + + sol[nod] = Modulus(sol[nod] + sol[u]); + + } + + } + + } + +} + + + +int main (){ + + f >> n >> m; + + for (int i = 1; i <= m; ++i){ + + int x, y; + + double d; + + f >> x >> y >> d; + + v[x].push_back(make_pair(y,log(d))); + + v[y].push_back(make_pair(x,log(d))); + + } + + + + dijkstra(); + + for(int i = 2; i <= n; ++i) g << sol[i] << ' '; + +} diff --git a/distmin/djikstra/09c209a6-02b7-11ec-a8c2-2cf05d84bfc2.cpp b/distmin/djikstra/09c209a6-02b7-11ec-a8c2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..672b2d18ce8a1229853ba4d8cbc6b4dea986aaf1 --- /dev/null +++ b/distmin/djikstra/09c209a6-02b7-11ec-a8c2-2cf05d84bfc2.cpp @@ -0,0 +1,93 @@ +#include +#include +#include + + + +using namespace std; +fstream f1("dmin.in", ios::in); +fstream f2("dmin.out", ios::out); +int32_t nr[1505]; +int16_t viz[1505], n, m; +double a[1505][1505], d[1505]; +const double ep=0.00000001; +const double inf=999999999; +void citire() +{ + int32_t c; + int16_t i, j, x, y; + f1>>n>>m; + for(i=1; i<=n; i++) + for(j=1; j<=n; j++) + if(i!=j) + { + a[i][j]=inf; + a[j][i]=inf; + } + for(i=1; i<=m; i++) + { + f1>>x>>y>>c; + a[x][y]=log(c); + a[x][y]=log(c); + } +} +double modul(double val) +{ + if(val>=0.0) return 0; + else return (-val); +} +void dijkstra(int16_t x0) +{ + double mini; + int16_t i, j, vfmin; + + viz[x0]=1; + for(i=1; i<=n; i++) + { + d[i]=a[x0][i]; + if(a[x0][i]!=inf) nr[i]=1; + } + + for(i=1; i=ep) + { + d[j]=d[vfmin]+a[vfmin][j]; + nr[j]=nr[vfmin]; + } + else if(modul(d[j]-(d[vfmin]+a[vfmin][j]))<=ep) + { + nr[j]+=nr[vfmin]; + nr[j]%=104659; + } + } + } + } +} +void afis() +{ + int16_t i; + for(i=2; i<=n; i++) + f2< +#include +#include +#include + + + + +using namespace std; + + + +FILE *fin=fopen("dmin.in","r"); + +FILE *fout=fopen("dmin.out","w"); + + + +const double eps=1e-6; + +vector v[1510]; + +vector cost[1510]; + +int i,j,n,m,nrd[1510]; + +double dmin[1510]; + +queue q; + +bool inq[1510]; + + + +int main() + +{ + + fscanf(fin,"%d%d",&n,&m); + + for (i=1; i<=m; i++) + + { + + int a,b,c; + + fscanf(fin,"%d%d%d",&a,&b,&c); + + v[a].push_back(b); + + v[b].push_back(a); + + cost[a].push_back(log(c)); + + cost[b].push_back(log(c)); + + } + + q.push(1); + + nrd[1]=1; + + while (!q.empty()) + + { + + int x=q.front(); + + inq[x]=0; + + q.pop(); + + for (i=0; i-eps) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + else if (dmin[x]+pret+eps +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, in[1510], nrd[1510]; + +double D[1510]; + + + +vector A[1510]; + +vector C[1510]; + +queue Q; + + + + + +int main() + +{ + + int i, a, b, nod, vecin; + + double c, cost; + + fin >> n >> m; + + for (i = 1; i <= m; i++) + + { + + fin >> a >> b >> c; + + A[a].push_back(b); + + C[a].push_back(log10(c)); + + A[b].push_back(a); + + C[b].push_back(log10(c)); + + } + + for (i = 2; i <= n; i++) + + D[i] = 1000000000; + + nrd[1] = 1; + + Q.push(1); + + in[1] = 1; + + while (!Q.empty()) + + { + + nod = Q.front(); + + Q.pop(); + + in[nod] = 0; + + for (i = 0; i < A[nod].size(); i++) + + { + + vecin = A[nod][i]; + + cost = C[nod][i]; + + if (D[vecin] > D[nod] + cost + 0.0000000001) + + { + + D[vecin] = D[nod] + cost; + + nrd[vecin] = nrd[nod] % 104659; + + if (!in[vecin]) + + { + + in[vecin] = 1; + + Q.push(vecin); + + } + + } + + else if (abs(D[vecin] - D[nod] - cost) < 0.0000000001) + + nrd[vecin] = (nrd[vecin] % 104659 + nrd[nod] % 104659) % 104659; + + } + + } + + for (i = 2; i <= n; i++) + + fout << nrd[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/djikstra/0af3495b-02b7-11ec-9635-2cf05d84bfc2.cpp b/distmin/djikstra/0af3495b-02b7-11ec-9635-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd20034fe41cc554ffae162bd054b7e7eaff98ba --- /dev/null +++ b/distmin/djikstra/0af3495b-02b7-11ec-9635-2cf05d84bfc2.cpp @@ -0,0 +1,157 @@ +#include + + +using namespace std; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); +int x,y,n,m; + +double L[50005]; + +bool viz[50005]; + +const double eps = 1e-6; + +double c; + + + +vector > v[50005]; + +vector < int > T; + + + +int D[50005]; + + + +int cmp(const int&a, const int&b){ + + return(L[a] > L[b]); + +} + + + +void dijkstra(){ + + + + int node; + + T.push_back(1); + + make_heap(T.begin(),T.end(),cmp); + + while(!T.empty()){ + + node=T.front(); + + pop_heap(T.begin(),T.end(),cmp); + + T.pop_back(); + + for(int i=0;i L[node] + v[node][i].second){ + + L[v[node][i].first] = L[node] + v[node][i].second; + + T.push_back(v[node][i].first); + + push_heap(T.begin(),T.end(),cmp); + + } + + + + } + + } + + + + + +} + + + + + + + +int Solve(int node){ + + + + if(viz[node])return D[node]; + + viz[node]=1; + + for(int i=0;i>n>>m; + + + + for(int i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + v[x].push_back({y,log(c)}); + + v[y].push_back({x,log(c)}); + + + + } + + for(int i=2;i<=n;i++) + + L[i]=INFINITY; + + dijkstra(); + + viz[1]=1; + + D[1]=1; + + for(int i=2;i<=n;i++){ + + D[i]=Solve(i); + + } + + for(int i=2;i<=n;i++) + + out< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +double d[1505]; + +int n,m,nr[1505]; + + + +bitset <1505> viz; + +vector < pair > v[1505]; + + + +struct CMP{ bool operator ()(int a,int b){ + + return d[a]>d[b];}}; + + + +priority_queue , CMP> q; + + + +int main() + +{ + + fin>>n>>m; + + for(int i=2;i<=n;++i) + + d[i]=0x3f3f3f3f; + + double cost; + + for(int i=1;i<=m;++i){ + + int a,b; + + double c; + + fin>>a>>b>>c; + + cost=log(c); + + v[a].push_back(make_pair(b,cost)); + + v[b].push_back(make_pair(a,cost)); + + } + + q.push(1); + + nr[1]=1; + + for(int i,nod;!q.empty();){ + + i=q.top(); + + q.pop(); + + if(viz[i]) + + continue; + + viz[i]=1; + + for(int j=0;jd[i]+cost){ + + nr[nod]=nr[i]; + + d[nod]=d[i]+cost; + + q.push(nod); + + } + + } + + } + + for(int i=2;i<=n;++i) + + fout< +#include +#include +#include +#include +#include +using namespace std; + + + +class cmp { + +public : + + + + bool operator ()( pair < int , double > a , pair < int , double > b ){ + + return a.second > b.second ; + + } + + + +}; + + + + + +vector < pair < int , double > > muc [ 1510 ]; + +vector < pair < int , double > >::iterator it ; + +priority_queue < pair < int , double > , vector < pair < int , double > > , cmp > que ; + +int n ; + +double dist [ 1510 ] ; + +int nrd[ 1510 ]; + +int viz [ 1510 ] ; + + + +int comp ( double a , double b ){ + + if ( a - b < 1e-10 && a - b > -1e-10 ){ + + return 0 ; + + } + + + + if ( a < b ){ + + return -1; + + } + + return 1 ; + +} + + + +void djikstra ( ){ + + static int i , nod , temp ; + + + + for ( i = 2 ; i <= n ; i ++ ){ + + dist [ i ] = 1e7 ; + + } + + + + nrd [ 1 ] = 1 ; + + que.push( make_pair( 1 , 0 ) ); + + + + while ( !que.empty() ){ + + + + while ( !que.empty() && viz [ que.top().first ] == 1 ){ + + que.pop(); + + } + + + + if ( que.empty() ){ + + break ; + + } + + + + nod = que.top().first; + + que.pop() ; + + viz[ nod ] = 1; + + + + for ( it = muc [ nod ].begin() ; it != muc [ nod ].end() ; it++ ){ + + + + if ( viz [ it->first ] ){ + + continue ; + + } + + + + temp = comp ( dist [ it->first ] , dist [ nod ] + it->second ) ; + + if ( temp == 0 ){ + + nrd [ it -> first ] += nrd [ nod ]; + + }else if ( temp == 1 ){ + + dist [ it->first ] = dist [ nod ] + it->second ; + + nrd [ it->first ] = nrd [ nod ] ; + + que.push( make_pair( it->first , dist [ it->first ] ) ) ; + + + + } + + nrd [ it->first ] %= 104659 ; + + + + + + } + + + + } + +} + + + +int main(){ + + int m , x , y , cost , i ; + + + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for( i = 0 ; i < m ; i ++ ){ + + scanf("%d%d%d", &x , &y , &cost ); + + muc [ x ].push_back( make_pair( y , log( cost ) ) ) ; + + } + + + + djikstra( ); + + + + for ( i = 2 ; i <= n ; i ++ ){ + + printf("%d ",nrd [ i ] ) ; + + + + } + + + + return 0; + +} diff --git a/distmin/djikstra/0ce8eb6d-02b7-11ec-bbf1-2cf05d84bfc2.cpp b/distmin/djikstra/0ce8eb6d-02b7-11ec-bbf1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e0b3b9d687eaeb2ac7d091f45ea5d0b6e59749c --- /dev/null +++ b/distmin/djikstra/0ce8eb6d-02b7-11ec-bbf1-2cf05d84bfc2.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +fstream in ( "dmin.in" , ios::in ); +fstream out( "dmin.out", ios::out ); + +const int DIM = 1505; +const int MOD = 104659; +const int INF = 0x3f3f3f3f; +const long double EPS = 1e-10; + +int cnt[DIM]; +long double dst[DIM]; + +vector> edg[DIM]; +priority_queue< pair, + vector >, + greater>> prq; + +inline int sgn( long double x ) { + if( fabs( x ) < EPS ) + return 0; + return ( x < 0 ) ? -1 : 1; +} + +int main( void ) { + ios::sync_with_stdio( false ); + + int n, m; + in >> n >> m; + + for( int i = 1; i <= m; i ++ ) { + int x, y, z; + in >> x >> y >> z; + + edg[x].push_back( make_pair( log10( z ), y ) ); + edg[y].push_back( make_pair( log10( z ), x ) ); + } + + fill( dst + 2, dst + n + 1, INF ); cnt[1] = 1; + for( prq.push( make_pair( 0, 1 ) ); !prq.empty(); prq.pop() ) { + pair x = prq.top(); + + if( sgn( x.first - dst[x.second] ) > 0 ) + continue; + + for( pair y : edg[x.second] ) { + if( sgn( dst[y.second] - ( dst[x.second] + y.first ) ) > 0 ) { + dst[y.second] = dst[x.second] + y.first; + cnt[y.second] = cnt[x.second]; + + prq.push( make_pair( dst[y.second], y.second ) ); + } + else { + if( sgn( dst[y.second] - ( dst[x.second] + y.first ) ) == 0 ) + cnt[y.second] = ( cnt[y.second] + cnt[x.second] ) % MOD; + } + } + } + + for( int i = 2; i <= n; i ++ ) + out << cnt[i] << " "; + out << endl; + + return 0; +} diff --git a/distmin/djikstra/0cff8ef5-02b7-11ec-9af4-2cf05d84bfc2.cpp b/distmin/djikstra/0cff8ef5-02b7-11ec-9af4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7be8e414a34dd0414e3c5bc776c7250a957175c --- /dev/null +++ b/distmin/djikstra/0cff8ef5-02b7-11ec-9af4-2cf05d84bfc2.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int maxn = 15005; + +const int mod = 104659; + +const double eps = 1e-8; + +const double oo = 1e40; + +vector > G[maxn]; + +double Dist[maxn]; + +int Cnt[maxn]; + + + +void Dijkstra(int start, int n) { + + vector < pair > :: iterator it; + + priority_queue > H; + + bitset Vis = 0; + + int node, i; + + for (i = 1; i <= n; i++) { + + Dist[i] = oo; + + } + + Dist[start] = 0; + + Cnt[start] = 1; + + H.push(make_pair(0, start)); + + while (!H.empty()) { + + node = H.top().second; + + H.pop(); + + if (Vis[node] == true) { + + continue; + + } + + Vis[node] = true; + + for (it = G[node].begin(); it != G[node].end(); it++) { + + if (Dist[it->first] - eps > Dist[node] + it->second) { + + Dist[it->first] = Dist[node] + it->second; + + Cnt[it->first] = Cnt[node]; + + H.push(make_pair(-Dist[it->first], it->first)); + + } else if (Dist[it->first] + eps > Dist[node] + it->second) { + + Cnt[it->first] += Cnt[node]; + + if (Cnt[it->first] >= mod) { + + Cnt[it->first] -= mod; + + } + + } + + } + + } + +} + + + +int main() { + + ios_base :: sync_with_stdio (false); + + int n, m, x, y, c, i; + + fin >> n >> m; + + for (i = 1; i <= m; i++) { + + fin >> x >> y >> c; + + G[x].push_back(make_pair(y, log(c))); + + G[y].push_back(make_pair(x, log(c))); + + } + + Dijkstra(1, n); + + for (i = 2; i <= n; i++) { + + fout << Cnt[i] << " "; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/djikstra/0d4e709d-02b7-11ec-816c-2cf05d84bfc2.cpp b/distmin/djikstra/0d4e709d-02b7-11ec-816c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a144cdf2df066542f1a1b92f2d57a11e5ea77be5 --- /dev/null +++ b/distmin/djikstra/0d4e709d-02b7-11ec-816c-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 1500 + 5; + +const int MMAX = 2500 + 5; + +const int MOD = 104659; + +const double EPS = 1e-8; + +const double INF = 1e40; + + + +int N, M; + +vector > graph[NMAX]; + + + +double dist[NMAX]; + +int cnt[NMAX]; + +bool vis[NMAX]; + + + +priority_queue > q; + + + +void dijkstra() { + + for (int i = 1; i <= N; ++ i) + + dist[i] = INF; + + + + dist[1] = 0; + + cnt[1] = 1; + + q.push(make_pair(0, 1)); + + + + while (!q.empty()) { + + int node = q.top().second; + + q.pop(); + + + + if (vis[node]) + + continue; + + vis[node] = true; + + + + for (auto it: graph[node]) + + if (dist[node] + it.second < dist[it.first] - EPS) { + + dist[it.first] = dist[node] + it.second; + + cnt[it.first] = cnt[node]; + + q.push(make_pair(-dist[it.first], it.first)); + + } + + else if (dist[node] + it.second < dist[it.first] + EPS) { + + cnt[it.first] += cnt[node]; + + if (cnt[it.first] >= MOD) + + cnt[it.first] -= MOD; + + } + + } + +} + + + +int main() + +{ + + ifstream cin("dmin.in"); + + ofstream cout("dmin.out"); + + + + cin >> N >> M; + + for (int i = 1; i <= M; ++ i) { + + int a, b, c; + + cin >> a >> b >> c; + + + + graph[a].push_back(make_pair(b, log(c))); + + graph[b].push_back(make_pair(a, log(c))); + + } + + + + dijkstra(); + + for (int i = 2; i <= N; ++ i) + + cout << cnt[i] << " \n"[i == N]; + + return 0; + +} diff --git a/distmin/djikstra/0d503feb-83aa-4f1a-b81a-5c9d3225ff88.cpp b/distmin/djikstra/0d503feb-83aa-4f1a-b81a-5c9d3225ff88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9a2ef93a251c8623ed4087a99076693160f2fcb --- /dev/null +++ b/distmin/djikstra/0d503feb-83aa-4f1a-b81a-5c9d3225ff88.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + + + + + +using namespace std; + +vector > adList[2501]; +int paths[2501], inQueue[2501]; +queue Q; +int n, m; +double distances[2501]; + +void dmin(){ + Q.push(1); + inQueue[1] = 1; + paths[1] = 1; + + while(!Q.empty()){ + int node = Q.front(); + Q.pop(); + + vector >::iterator it = adList[node].begin(); + while(it != adList[node].end()){ + pair neigh = *it; + if(distances[neigh.first] - 0.0000001 > distances[node] + neigh.second){ + distances[neigh.first] = distances[node] + neigh.second; + paths[neigh.first] = paths[node]; + if(inQueue[neigh.first] == 0){ + Q.push(neigh.first); + inQueue[neigh.first] = 1; + } + } + else{ + if(distances[neigh.first] + 0.0000001 >= distances[node] + neigh.second){ + paths[neigh.first] += paths[node]; + paths[neigh.first] %= 104659; + } + } + + it++; + } + + } + +} + +int main(){ + ifstream f("dmin.in"); + ofstream g("dmin.out"); + + int x, y, c; + + f >> n >> m; + for(int i = 0; i < m; i++){ + f >> x >> y >> c; + adList[x].push_back(make_pair(y, log(c))); + adList[y].push_back(make_pair(x, log(c))); + } + f.close(); + + for(int i = 2; i <= n; i++) + distances[i] = 1000000000; + + dmin(); + + for(int i = 2; i <= n; i++) + g << paths[i] << " "; + + g.close(); + return 0; +} diff --git a/distmin/djikstra/0d655839-02b7-11ec-99f3-2cf05d84bfc2.cpp b/distmin/djikstra/0d655839-02b7-11ec-99f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca822482121c666bb9f7a7a4acb6f403ff1e2159 --- /dev/null +++ b/distmin/djikstra/0d655839-02b7-11ec-99f3-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + double newcost=y+(*it).second; + + if((dp[(*it).first]-1e-9)>newcost) + + { + + dp[(*it).first]=newcost; + + pos[(*it).first]=pos[x]; + + q.push({(*it).first,dp[(*it).first]}); + + } + + else + + if(fabs(newcost-dp[(*it).first])<=1e-9) + + { + + pos[(*it).first]=(pos[(*it).first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log2(c))); + + v[y].push_back(make_pair(x,log2(c))); + + } + + dp[1]=0; + + for(int i=2;i<=n;i++) dp[i]=1.0*INT_MAX; + + pos[1]=1; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/0d906e6a-02b7-11ec-8d66-2cf05d84bfc2.cpp b/distmin/djikstra/0d906e6a-02b7-11ec-8d66-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4be0a054da2b2d945cacfaf5726654fe32eb88f3 --- /dev/null +++ b/distmin/djikstra/0d906e6a-02b7-11ec-8d66-2cf05d84bfc2.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int MAXN = 1502; + +const int MODULO = 104659; + +const double EPSILON = 1e-10; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +int n; + +vector vecini[MAXN]; + +vector cost[MAXN]; + + + + + +struct nod_dijk + +{ + + int nod; + + double dist; + + bool operator < (const nod_dijk b) const + + { + + return dist > b.dist; + + } + +}; + + + +priority_queue heap; + + + +void citire() + +{ + + int m; + + in >> n >> m; + + for (int i = 1;i <= m;++i) + + { + + int a, b, c; + + in >> a >> b >> c; + + + + vecini[a].push_back(b); + + cost[a].push_back(log((double)c)); + + + + + + vecini[b].push_back(a); + + cost[b].push_back(log((double)c)); + + } + +} + + + +double d[MAXN]; + +int p[MAXN]; + + + +void dijkstra() + +{ + + for (int i = 0;i < MAXN;++i) + + { + + d[i] = numeric_limits::max(); + + p[i] = 0; + + } + + heap.push((nod_dijk){1,0}); + + d[1] = 0; + + p[1] = 1; + + while(!heap.empty()) + + { + + int nod = heap.top().nod; + + double dist = heap.top().dist; + + heap.pop(); + + if(abs(d[nod] - dist) > EPSILON) + + continue; + + p[nod] = 0; + + for(unsigned int i = 0;i < vecini[nod].size();++i) + + { + + int vecin = vecini[nod][i]; + + double c = cost[nod][i]; + + if (d[vecin] == numeric_limits::max()) + + continue; + + if (abs(d[vecin] + c - d[nod]) <= EPSILON) + + p[nod] += p[vecin]; + + } + + if(p[nod] == 0) + + p[nod] = 1; + + p[nod] %= MODULO; + + + + for(unsigned int i = 0;i < vecini[nod].size();++i) + + { + + int vecin = vecini[nod][i]; + + double c = cost[nod][i]; + + if (d[nod] + c < d[vecin]) + + { + + d[vecin] = d[nod] + c; + + heap.push((nod_dijk){vecin, d[vecin]}); + + } + + } + + } + +} + + + +int main() + +{ + + citire(); + + dijkstra(); + + for (int i = 2;i <= n;++i) + + out << p[i] << ' '; + + out << '\n'; + + return 0; + +} diff --git a/distmin/djikstra/0dc22d3b-02b7-11ec-904d-2cf05d84bfc2.cpp b/distmin/djikstra/0dc22d3b-02b7-11ec-904d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca822482121c666bb9f7a7a4acb6f403ff1e2159 --- /dev/null +++ b/distmin/djikstra/0dc22d3b-02b7-11ec-904d-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + double newcost=y+(*it).second; + + if((dp[(*it).first]-1e-9)>newcost) + + { + + dp[(*it).first]=newcost; + + pos[(*it).first]=pos[x]; + + q.push({(*it).first,dp[(*it).first]}); + + } + + else + + if(fabs(newcost-dp[(*it).first])<=1e-9) + + { + + pos[(*it).first]=(pos[(*it).first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log2(c))); + + v[y].push_back(make_pair(x,log2(c))); + + } + + dp[1]=0; + + for(int i=2;i<=n;i++) dp[i]=1.0*INT_MAX; + + pos[1]=1; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/0dd85e98-02b7-11ec-8898-2cf05d84bfc2.cpp b/distmin/djikstra/0dd85e98-02b7-11ec-8898-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9248d811c4a4431e31e5645448852e8df173aae8 --- /dev/null +++ b/distmin/djikstra/0dd85e98-02b7-11ec-8898-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + if((dp[it->first]-1e-8)>=y+it->second) + + { + + dp[it->first]=y+it->second; + + pos[it->first]=pos[x]; + + q.push({it->first,dp[it->first]}); + + } + + else if(fabs(it->second+y-dp[it->first])<=1e-8) + + { + + pos[it->first]=(pos[it->first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log10(c))); + + v[y].push_back(make_pair(x,log10(c))); + + } + + dp[1]=0; + + pos[1]=1; + + for(int i=2;i<=n;i++) dp[i]=INT_MAX; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/0e1fd172-02b7-11ec-92aa-2cf05d84bfc2.cpp b/distmin/djikstra/0e1fd172-02b7-11ec-92aa-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9a2ef93a251c8623ed4087a99076693160f2fcb --- /dev/null +++ b/distmin/djikstra/0e1fd172-02b7-11ec-92aa-2cf05d84bfc2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include + + + + + +using namespace std; + +vector > adList[2501]; +int paths[2501], inQueue[2501]; +queue Q; +int n, m; +double distances[2501]; + +void dmin(){ + Q.push(1); + inQueue[1] = 1; + paths[1] = 1; + + while(!Q.empty()){ + int node = Q.front(); + Q.pop(); + + vector >::iterator it = adList[node].begin(); + while(it != adList[node].end()){ + pair neigh = *it; + if(distances[neigh.first] - 0.0000001 > distances[node] + neigh.second){ + distances[neigh.first] = distances[node] + neigh.second; + paths[neigh.first] = paths[node]; + if(inQueue[neigh.first] == 0){ + Q.push(neigh.first); + inQueue[neigh.first] = 1; + } + } + else{ + if(distances[neigh.first] + 0.0000001 >= distances[node] + neigh.second){ + paths[neigh.first] += paths[node]; + paths[neigh.first] %= 104659; + } + } + + it++; + } + + } + +} + +int main(){ + ifstream f("dmin.in"); + ofstream g("dmin.out"); + + int x, y, c; + + f >> n >> m; + for(int i = 0; i < m; i++){ + f >> x >> y >> c; + adList[x].push_back(make_pair(y, log(c))); + adList[y].push_back(make_pair(x, log(c))); + } + f.close(); + + for(int i = 2; i <= n; i++) + distances[i] = 1000000000; + + dmin(); + + for(int i = 2; i <= n; i++) + g << paths[i] << " "; + + g.close(); + return 0; +} diff --git a/distmin/djikstra/0e3b58f8-02b7-11ec-9582-2cf05d84bfc2.cpp b/distmin/djikstra/0e3b58f8-02b7-11ec-9582-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b20018210416f48820650bee20549b9afe02af1 --- /dev/null +++ b/distmin/djikstra/0e3b58f8-02b7-11ec-9582-2cf05d84bfc2.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef pair per; + +typedef vector::iterator it; + + + +int n,m,rez[1505]; + +double d[1505]; + +bitset<1505> viz; + +vector gr[1505]; + +set > s; + + + +void dij() { + + for(int i=1; i<=n; ++i) d[i]=2000000000; + + rez[1]=1; + + for(s.insert(make_pair(0,1));s.size();s.erase(s.begin())) { + + double cst=s.begin()->first; int nod=s.begin()->second; + + for(it i=gr[nod].begin(); i!=gr[nod].end(); ++i) { + + if(fabs(d[i->first]-cst-i->second)<1e-9) rez[i->first]=(rez[i->first]+rez[nod])%104659; + + else if(cst+i->secondfirst]) { + + d[i->first]=cst+i->second; + + rez[i->first]=rez[nod]; + + s.insert(make_pair(d[i->first],i->first)); + + } + + } + + } + +} + + + +int main() + +{ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(int i=1; i<=m; ++i) { + + int a,b,c; + + f>>a>>b>>c; + + double lg=log((double)c); + + gr[a].push_back(make_pair(b,lg)); + + gr[b].push_back(make_pair(a,lg)); + + } + + dij(); + + for(int i=2; i<=n; ++i) g< +#include +#include +#include +using namespace std; + +vector > g[1510]; + +int n,m; + +double best[1510]; + +int dp[1510]; + +struct Compare{ + + bool operator()(pair a,pair b){ + + return a.second>b.second; + + } + +}; + +priority_queue,vector >,Compare> Heap; + +void Dijkstra(){ + + int i,node; + + double cost; + + for(i=2;i<=n;i++) + + best[i]=1000000000; + + best[1]=0; + + dp[1]=1; + + Heap.push(make_pair(1,0)); + + while(!Heap.empty()){ + + node=Heap.top().first; + + cost=Heap.top().second; + + Heap.pop(); + + for(i=0;i=cost+g[node][i].second){ + + best[g[node][i].first]=cost+g[node][i].second; + + dp[g[node][i].first]=dp[node]; + + Heap.push(make_pair(g[node][i].first,best[g[node][i].first])); + + } + + else + + if(fabs(best[g[node][i].first]-cost-g[node][i].second)<=1e-8){ + + dp[g[node][i].first]+=dp[node]; + + if(dp[g[node][i].first]>=104659) + + dp[g[node][i].first]-=104659; + + } + + } + + } + +} + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + int i,a,b; + + double c; + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++){ + + scanf("%d%d%lf",&a,&b,&c); + + g[a].push_back(make_pair(b,log10(c))); + + g[b].push_back(make_pair(a,log10(c))); + + } + + Dijkstra(); + + for(i=2;i<=n;i++) + + printf("%d ",dp[i]); + + return 0; + +} diff --git a/distmin/djikstra/0e6b9d83-02b7-11ec-b831-2cf05d84bfc2.cpp b/distmin/djikstra/0e6b9d83-02b7-11ec-b831-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39ca113beae9e31fafd7760bd3b9f4ef55d297cf --- /dev/null +++ b/distmin/djikstra/0e6b9d83-02b7-11ec-b831-2cf05d84bfc2.cpp @@ -0,0 +1,205 @@ +#include +#include +#include +#include + + +using namespace std; + + + +struct Nod + +{ + + double cost = (1 << 30); + + vector> vecini; + +}; + + + +typedef vector Graf; + +typedef pair Pereche; + + + +const int kMod = 104659; + +const double kEpsilon = 0.00001; + + + +inline double Log2(int n) + +{ + + return log(n) / log(2); + +} + + + +void AflaDistante(Graf &g) + +{ + + auto cmp = [](const Pereche &a, const Pereche &b) -> bool { return a.second > b.second; }; + + priority_queue, decltype(cmp)> coada(cmp); + + + + g[1].cost = 0; + + coada.push({ 1, 0 }); + + + + while (!coada.empty()) { + + int nod = coada.top().first; + + double cost = coada.top().second; + + coada.pop(); + + + + if (cost != g[nod].cost) continue; + + + + for (auto &muchie : g[nod].vecini) { + + int vecin = muchie.first; + + double cost_nou = cost + muchie.second; + + if (g[vecin].cost - cost_nou > kEpsilon) { + + g[vecin].cost = cost_nou; + + coada.push({ vecin, cost_nou }); + + } + + } + + } + +} + + + +vector AflaModuri(const Graf &g) + +{ + + vector moduri(g.size(), 0); + + queue coada; + + vector in_coada(g.size(), false); + + + + coada.push(1); + + moduri[1] = 1; + + in_coada[1] = true; + + + + while (!coada.empty()) { + + int nod = coada.front(); + + coada.pop(); + + + + for (auto &muchie : g[nod].vecini) { + + int vecin = muchie.first; + + if (abs(g[vecin].cost - g[nod].cost - muchie.second) <= kEpsilon && g[vecin].cost - g[nod].cost > kEpsilon) { + + moduri[vecin] += moduri[nod]; + + moduri[vecin] %= kMod; + + if (!in_coada[vecin]) { + + coada.push(vecin); + + in_coada[vecin] = true; + + } + + } + + } + + } + + return moduri; + +} + + + +int main() + +{ + + ifstream fin("dmin.in"); + + ofstream fout("dmin.out"); + + + + int n, m; + + fin >> n >> m; + + + + Graf graf(n + 1); + + while (m--) { + + int x, y, c; + + fin >> x >> y >> c; + + + + double cost = Log2(c); + + graf[x].vecini.push_back({ y, cost }); + + graf[y].vecini.push_back({ x, cost }); + + } + + + + AflaDistante(graf); + + + + auto raspuns = AflaModuri(graf); + + for (int i = 2; i < raspuns.size(); ++i) + + fout << raspuns[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/0e8733a1-02b7-11ec-b425-2cf05d84bfc2.cpp b/distmin/djikstra/0e8733a1-02b7-11ec-b425-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e6297dae47179bcb0bd47b29ca93645b97d27e5 --- /dev/null +++ b/distmin/djikstra/0e8733a1-02b7-11ec-b425-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +bool viz[1505]; + +int n, m, nrdrumuri[1505]; + +double dmin[1505]; + +vector > G[1505]; + + + +priority_queue , greater > q; + + + +void read() + +{ + + ifstream f("dmin.in"); + + f >> n >> m; double c; + + for(int i=0, x, y; i> x >> y >> c; + + G[x].push_back(make_pair(y,log(c))); + + G[y].push_back(make_pair(x,log(c))); + + } + + f.close(); + +} + + + +void dijkstra() + +{ + + int vfmin; + + for(int i=2; i<=n; ++i) dmin[i]=2000000001; + + q.push(1); nrdrumuri[1]=1; + + while(!q.empty()) + + { + + vfmin=q.top(); q.pop(); + + if(viz[vfmin]) continue; + + viz[vfmin]=1; + + for(int i=0; i dmin[vfmin] + G[vfmin][i].second) + + { + + dmin[G[vfmin][i].first] = dmin[vfmin] + G[vfmin][i].second; + + nrdrumuri[G[vfmin][i].first]=nrdrumuri[vfmin]; + + q.push(G[vfmin][i].first); + + } + + else + + if(abs(dmin[G[vfmin][i].first] - dmin[vfmin] - G[vfmin][i].second) <1e-9) + + { + + nrdrumuri[G[vfmin][i].first] = nrdrumuri[G[vfmin][i].first] + nrdrumuri[vfmin]; + + if(nrdrumuri[G[vfmin][i].first] >= 104659) + + nrdrumuri[G[vfmin][i].first] -= 104659; + + } + + } + + } + +} + + + +void out() + +{ + + ofstream g("dmin.out"); + + for(int i=2; i<=n; ++i) + + g << nrdrumuri[i] <<' '; + + g << '\n'; + + g.close(); + +} + + + +int main() + +{ + + read(); + + dijkstra(); + + out(); + + return 0; + +} diff --git a/distmin/djikstra/0ea1eed1-02b7-11ec-8de7-2cf05d84bfc2.cpp b/distmin/djikstra/0ea1eed1-02b7-11ec-8de7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3571a8d57e8ef3f3fba16e45605b3862430d3b2 --- /dev/null +++ b/distmin/djikstra/0ea1eed1-02b7-11ec-8de7-2cf05d84bfc2.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > adj[1510]; + +int n,m; + +int best[1510]; + +double err=1e-8,INF=1000000000.0; + +struct cmp { + + bool operator()(pair a,pair b){ + + if(fabs(a.first-b.first)b.first+err; + + } + +}; + +priority_queue ,vector >,greater > > pb; + +double dist[1510]; + + + +void dijktras(int v) + +{ + + int u; + + double cost2,cost1; + + dist[v]=0; + + pb.push(make_pair(0,v)); + + while(!pb.empty()) + + { + + v=pb.top().second; + + cost1=pb.top().first; + + pb.pop(); + + for(vector >::iterator it=adj[v].begin();it!=adj[v].end();it++) + + { + + u=it->second; + + cost2=it->first; + + if(fabs(cost1+cost2-dist[u])<=err){ + + + + + + best[u]+=best[v]; + + best[u]=best[u]%104659; + + } + + else if(dist[u]-err>=cost1+cost2) + + { + + best[u]=best[v]; + + best[u]=best[v]%104659; + + dist[u]=cost2+cost1; + + pb.push(make_pair(dist[u],u)); + + } + + } + + } + + + +} + + + +int main() + +{ + + f >> n >> m; + + for(int i=0;i> x >> y >> c; + + adj[x].push_back(make_pair(log10(c),y)); + + adj[y].push_back(make_pair(log10(c),x)); + + } + + + + for(int i=1;i<=n;i++){ + + dist[i]=INF; + + } + + best[1]=1; + + dijktras(1); + + + + for(int i=2;i<=n;i++) + + g << best[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/0ed40256-02b7-11ec-9791-2cf05d84bfc2.cpp b/distmin/djikstra/0ed40256-02b7-11ec-9791-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e7f8d46ff4cf301ddfe09f0f2aab76f4e73b37b --- /dev/null +++ b/distmin/djikstra/0ed40256-02b7-11ec-9791-2cf05d84bfc2.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int mod=104659; + +const double eps=1e-9; + +set< pair >s; + +vector v[1505]; + +vector c[1505]; + +int modes[1505],a,b,i,n,m,x,node; + +double cost[1505],d,dist,costul; + +void dijkstra() + +{ + + s.insert(make_pair(0,1)); + + while(!s.empty()) + + { + + d=(*s.begin()).first; + + x=(*s.begin()).second; + + s.erase(*s.begin()); + + for(i=0;i=mod) modes[node]-=mod; + + } + + else + + { + + if(d+dist>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>a>>b>>costul; + + v[a].push_back(b); + + c[a].push_back(log(costul)); + + v[b].push_back(a); + + c[b].push_back(log(costul)); + + } + + for(i=1;i<=n;i++) + + cost[i]=20000; + + cost[1]=0;modes[1]=1; + + dijkstra(); + + for(i=2;i<=n;i++) + + {g< +#include +#include +#include +using namespace std; +vector < pair < double, int > > v[ 1505 ]; + +priority_queue < pair < double, int > > Q; + + + +double dist[ 1505 ]; + +int in_queue[ 1505 ]; + +int dinamica[ 1505 ]; + + + +void aflaDrumuri( int nod ){ + + + + int i, k, fiu; + + double cost; + + + + dist[ nod ] = 0; + + dinamica[ nod ] = 1; + + Q.push( { 0.0, nod } ); + + + + while( !Q.empty() ){ + + nod = Q.top().second; + + Q.pop(); + + if( in_queue[ nod ] ) continue; + + in_queue[ nod ] = 1; + + for( i = 0; i < v[ nod ].size(); ++i ){ + + fiu = v[ nod ][ i ].second; + + cost = v[ nod ][ i ].first; + + if( dist[ fiu ] - ( dist[ nod ] + cost ) > 1e-10 ){ + + dist[ fiu ] = dist[ nod ] + cost; + + dinamica[ fiu ] = dinamica[ nod ]; + + Q.push( { -dist[ fiu ], fiu } ); + + } + + else if( fabs( dist[ fiu ] - ( dist[ nod ] + cost ) ) < 1e-10 ){ + + dinamica[ fiu ] += dinamica[ nod ]; + + dinamica[ fiu ] %= 104659; + + } + + } + + } + + + + + + + +} + + + +int main() + +{ + + + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + int n, m, i, j, s, t, d, k, x, y, c; + + + + scanf("%d%d",&n,&m); + + while( m-- ){ + + scanf("%d%d%d",&x,&y,&c); + + v[ x ].push_back( { log10( c ), y } ); + + v[ y ].push_back( { log10( c ), x } ); + + } + + + + for( i = 1; i <= n; ++i ) + + dist[ i ] = 10000.0; + + + + aflaDrumuri( 1 ); + + for( i = 2; i <= n; ++i ) + + printf("%d ",dinamica[ i ]); + + + + + + return 0; + + + +} diff --git a/distmin/djikstra/0f193bae-1305-4a6e-8c83-df00717c7622.cpp b/distmin/djikstra/0f193bae-1305-4a6e-8c83-df00717c7622.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b6d372e37ad4b5b3f9f07e5d27ee3dab4de4999 --- /dev/null +++ b/distmin/djikstra/0f193bae-1305-4a6e-8c83-df00717c7622.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); +int n, m; + +vector > g[1503]; + +queue < int >q; + +bool use[1503]; + +void read(){ + + fin >> n >> m; + + for(int i = 1; i <= m; i++){ + + int x, y; + + double c; + + fin >>x >>y >>c; + + c = log(c); + + g[x].push_back(make_pair(y,c)); + + g[y].push_back(make_pair(x,c)); + + + + } + +} + + + +void Dijkstra(int r){ + + int nr_drumuri[1503]; + + double dist[1503]; + + for(int i = 1; i <= n; i++) nr_drumuri[i] = 0, dist[i] = (1<<30);; + + + + dist[r] = 0; + + q.push(r); + + nr_drumuri[r] = 1; + + use[r] = true; + + while(! q.empty()){ + + int nod = q.front(); + + q.pop(); + + use[nod] = false; + + + + for(int i = 0 ; i < g[nod].size(); i++){ + + double cost = g[nod][i].second; + + int y = g[nod][i].first; + + if(fabs (dist[y] - dist[nod] - cost) < 0.0000001 ){ + + nr_drumuri[y] = (nr_drumuri[y] + nr_drumuri[nod] ) %104659; + + } + + if(dist[y] - dist[nod] - cost>0.0000001 ){ + + + + if(use[y] == false) + + q.push(y), use[y] = true; + + dist[y] = dist[nod] + cost; + + nr_drumuri[y] = nr_drumuri[nod] %104659; + + } + + + + + + } + + } + + for(int i = 2; i<= n ;i++) + + fout << nr_drumuri[i] <<" "; + +} + +int main(){ + + read(); + + Dijkstra(1); + + fin.close(); + + return 0; + + + +} diff --git a/distmin/djikstra/100182b8-02b7-11ec-9ad1-2cf05d84bfc2.cpp b/distmin/djikstra/100182b8-02b7-11ec-9ad1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1065812756f11c62be0e1a6aa0863e7897f431c9 --- /dev/null +++ b/distmin/djikstra/100182b8-02b7-11ec-9ad1-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +vector > G[1505]; + +int Heap[1505], Poz[1505]; + +long long M[1505]; + +long double D[1505]; + +long double const tol = 1e-10; + +int Heap_Size, n, m; + +long long const oo = 1000000000, MOD = 104659; + + + +void Read() + +{ + + int i, x, y; + + long double c; + + + + f>>n>>m; + + for(i=1; i<=m; i++){ + + f>>x>>y>>c; + + c = log10(c); + + G[x].push_back(make_pair(y, c)); + + G[y].push_back(make_pair(x, c)); + + } + +} + + + +void Swap(int hx, int hy) + +{ + + swap(Heap[hx], Heap[hy]); + + swap(Poz[Heap[hx]], Poz[Heap[hy]]); + +} + + + +void UpHeap(int nod) + +{ + + int tata = nod/2; + + if(tata && D[Heap[tata]] > D[Heap[nod]]){ + + Swap(tata, nod); + + UpHeap(tata); + + } + +} + + + +void DownHeap(int nod) + +{ + + int son = 2*nod; + + if(son + 1 <= Heap_Size && D[Heap[son+1]] < D[Heap[son]]) + + son++; + + if(son <= Heap_Size && D[Heap[son]] < D[Heap[nod]]){ + + Swap(son, nod); + + DownHeap(son); + + } + +} + + + +void Dijkstra() + +{ + + int i, nod, vecin; + + long double cost; + + vector >::iterator it; + + + + for(i=1; i<=n; i++){ + + D[i] = oo; + + Heap[++Heap_Size] = i; + + Poz[i] = Heap_Size; + + } + + + + D[1] = 0; + + M[1] = 1; + + UpHeap(Poz[1]); + + while(Heap_Size){ + + nod = Heap[1]; + + Swap(1, Heap_Size--); + + DownHeap(1); + + + + for(it = G[nod].begin(); it != G[nod].end(); it++){ + + vecin = it -> first; + + cost = it -> second; + + if(D[vecin] - cost - D[nod] > tol){ + + M[vecin] = M[nod]; + + D[vecin] = D[nod] + cost; + + UpHeap(Poz[vecin]); + + } + + else + + if(abs(D[nod] + cost - D[vecin]) < tol) + + M[vecin] = (M[vecin] + M[nod])%MOD; + + } + + } + + + + for(i=2; i<=n; i++) + + g< +#include +#include +#include +#include +#include + +using namespace std; + +const int Nmax = 1501; +const double eps = 0.0000001; +const int modulo = 104659; + +vector numar(Nmax); +vector dist(Nmax); +bitset viz; +vector< pair > Graf[Nmax]; +queue Q; + +int N, M; + +void citire(){ + + ifstream f("dmin.in"); + + f >> N >> M; + + int a, b; + double c; + + for(; M; M--) { + + f >> a >> b >> c; + c = log(c); + + Graf[a].push_back(pair(b, c)); + Graf[b].push_back(pair(a, c)); + } +} + + +void dijkstra() { + + vector< pair >::iterator it; + + for(int i = 1; i <= N; ++i) + dist[i] = 9999999.0; + + Q.push(1); + dist[1] = 0; + numar[1] = 1; + viz[1] = 1; + + while(!Q.empty()){ + + int nod = Q.front(); + Q.pop(); + + for(it = Graf[nod].begin(); it != Graf[nod].end(); ++it){ + + if(dist[it->first] - dist[nod] - it->second > eps){ + + dist[it->first] = dist[nod] + it->second; + numar[it->first] = numar[nod]; + + if(!viz[it->first]){ + + viz[it->first] = 1; + Q.push(it->first); + } + } + else + if(dist[nod] + it->second - dist[it->first] <= eps){ + + numar[it->first] += numar[nod]; + numar[it->first] %= modulo; + } + } + } +} + +void afis(){ + + ofstream g("dmin.out"); + + for(int i = 2; i <= N; ++i) + g << numar[i] << " "; +} + +int main() { + + citire(); + dijkstra(); + afis(); + + return 0; +} diff --git a/distmin/djikstra/104c64b0-02b7-11ec-96ef-2cf05d84bfc2.cpp b/distmin/djikstra/104c64b0-02b7-11ec-96ef-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7483158fd6ee4d3872dabe0b384487667a8e8526 --- /dev/null +++ b/distmin/djikstra/104c64b0-02b7-11ec-96ef-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int nodes, edges, roadsNum[1510]; + +double distances[1510]; + +vector < pair > G[1510]; + +queue QU; + +bool mark[1510]; + + + +class cmp { + +public: + + bool operator() (pair d1, pair d2) + + { + + return d1.second > d2.second; + + } + +}; + +priority_queue< pair, vector< pair >, cmp > H; + + + +void dijkstra(int node) + +{ + + roadsNum[node] = 1; + + + + for (int i = 2; i <= nodes; i++) + + distances[i] = 2000000000; + + + + H.push(make_pair(node, 0)); + + + + while (!H.empty()) { + + int crtNode = H.top().first; + + H.pop(); + + + + if (mark[crtNode]) + + continue; + + mark[crtNode] = true; + + + + for (vector < pair >::iterator it = G[crtNode].begin(); it != G[crtNode].end(); it++) { + + int q = it->first; + + if (!mark[it->first]) { + + if (distances[it->first] - (distances[crtNode] + it->second) > 1.e-10) { + + distances[it->first] = distances[crtNode] + it->second; + + H.push(make_pair(it->first, distances[it->first])); + + roadsNum[it->first] = roadsNum[crtNode]; + + } + + else if (abs(distances[it->first] - (distances[crtNode] + it->second)) <= 1.e-10) + + roadsNum[it->first] = (roadsNum[it->first] + roadsNum[crtNode]) % 104659; + + } + + } + + } + +} + + + +int main() + +{ + + f >> nodes >> edges; + + + + int n1, n2, c; + + for (int i = 1; i <= edges; i++) { + + f >> n1 >> n2 >> c; + + + + G[n1].push_back(make_pair(n2, log10(c))); + + G[n2].push_back(make_pair(n1, log10(c))); + + } + + + + dijkstra(1); + + + + + + for (int i = 2; i <= nodes; i++) + + g << roadsNum[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/10769344-02b7-11ec-88c6-2cf05d84bfc2.cpp b/distmin/djikstra/10769344-02b7-11ec-88c6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29817501c62b7a5b3a5d8833b2434f29a17aae6a --- /dev/null +++ b/distmin/djikstra/10769344-02b7-11ec-88c6-2cf05d84bfc2.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream in("dmin.in"); +ofstream out("dmin.out"); + + + + + +const int N_max = 15005; + +double dmin[N_max]; + +int sol[N_max]; + +bool viz[N_max]; + +class cmp +{ + public: + bool operator () (int x, int y) + { + return dmin[x] > dmin[y]; + } +}; + +priority_queue < int, vector , cmp > H; + +vector < pair > graph[N_max]; + +int N, M; + +void citire() +{ + int x, y; + double c, cost; + + in >> N >> M; + + for(int i = 1; i <= M; i++) + { + in >> x >> y >> c; + + cost = log10(c); + + + graph[x].push_back(make_pair(y, cost)); + graph[y].push_back(make_pair(x, cost)); + } +} + +void dijkstra(int node) +{ + int i, x, y; + double cost; + + for(i = 1; i <= N; i++) dmin[i] = 2e9; + + + H.push(node); + dmin[1] = 0; + + sol[node] = 1; + viz[node] = true; + + while(!H.empty()) + { + x = H.top(); + H.pop(); + + for(i = 0; i < graph[x].size(); i++) + { + y = graph[x][i].first; + + cost = graph[x][i].second; + + if(dmin[y] - dmin[x] - cost > 0.0000000001) + { + dmin[y] = dmin[x] + cost; + + sol[y] = sol[x]; + + if(!viz[y]) + { + H.push(y); + viz[y] = true; + } + } + else + if(dmin[x] + cost - dmin[y] <= 0.0000000001) + { + sol[y] += sol[x]; + + sol[y] %= 104659; + } + } + } +} + +void write() +{ + for(int i = 2; i <= N; i++) out << sol[i] << " "; +} + +int main() +{ + citire(); + + dijkstra(1); + + write(); + + return 0; +} diff --git a/distmin/djikstra/1166d35f-02b7-11ec-ae47-2cf05d84bfc2.cpp b/distmin/djikstra/1166d35f-02b7-11ec-ae47-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae882714481d2fe51d26b6c5eaedbed41d97c82f --- /dev/null +++ b/distmin/djikstra/1166d35f-02b7-11ec-ae47-2cf05d84bfc2.cpp @@ -0,0 +1,317 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +struct dij{ + + int nr; + + double cost; + +}; + + + +struct classcomp { + + bool operator() (const dij& lhs, const dij& rhs) const + + {return lhs.cost vecini[nmax]; + +double vcost[nmax]; + +multiset s; + +bool poz[nmax]; + + + +void dijkstra(int x) + +{ + + vcost[x] = 0; + + poz[x] = 1; + + + + for(int i = 0; i<(int)vecini[x].size(); i++) + + { + + dij aux; + + + + aux.nr = vecini[x][i].nr; + + aux.cost = vecini[x][i].cost; + + + + s.insert(aux); + + } + + + + while(s.empty()==0) + + { + + dij aux = *s.begin(); + + + + if(poz[aux.nr]==0) + + { + + vcost[aux.nr] = aux.cost; + + poz[aux.nr] = 1; + + + + for(int i = 0; i<(int)vecini[aux.nr].size(); i++) + + { + + if(poz[vecini[aux.nr][i].nr]==0) + + { + + dij aux2; + + + + aux2.cost = aux.cost + vecini[aux.nr][i].cost; + + aux2.nr = vecini[aux.nr][i].nr; + + + + s.insert(aux2); + + } + + } + + } + + + + s.erase(s.begin()); + + } + +} + + + +void dijkstra2(int x) + +{ + + vcost[x] = 0; + + poz[x] = 1; + + d[x] = 1; + + + + for(int i = 0; i<(int)vecini[x].size(); i++) + + { + + dij aux; + + + + aux.nr = vecini[x][i].nr; + + aux.cost = vecini[x][i].cost; + + + + if(aux.cost - vcost[aux.nr]-eps) + + { + + d[aux.nr] += d[x]; + + d[aux.nr] %= mod; + + } + + + + s.insert(aux); + + } + + + + while(s.empty()==0) + + { + + dij aux = *s.begin(); + + + + if(poz[aux.nr]==0) + + { + + vcost[aux.nr] = aux.cost; + + poz[aux.nr] = 1; + + + + for(int i = 0; i<(int)vecini[aux.nr].size(); i++) + + { + + dij aux2; + + + + aux2.cost = aux.cost + vecini[aux.nr][i].cost; + + aux2.nr = vecini[aux.nr][i].nr; + + + + if(aux2.cost - vcost[aux2.nr]-eps) + + { + + d[aux2.nr] += d[aux.nr]; + + d[aux2.nr] %= mod; + + } + + + + if(poz[vecini[aux.nr][i].nr]==0) + + { + + + + s.insert(aux2); + + } + + } + + } + + + + s.erase(s.begin()); + + } + +} + + + +int main(){ + + int player_unu=0; + + + + in>>n>>m; + + for(int i = 0; i>a>>b>>cost; + + + + cost = log(cost); + + + + dij aux1; + + aux1.nr = b; + + aux1.cost = cost; + + vecini[a].push_back(aux1); + + + + dij aux2; + + aux2.nr = a; + + aux2.cost = cost; + + vecini[b].push_back(aux2); + + } + + + + dijkstra(1); + + + + for(int i = 1; i<=n; i++) + + poz[i] = 0; + + s.clear(); + + + + dijkstra2(1); + + + + for(int i = 2; i<=n; i++) + + out< +#include +#include +#include +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +struct dij{ + + int nr; + + double cost; + +}; + + + +struct classcomp { + + bool operator() (const dij& lhs, const dij& rhs) const + + {return lhs.cost vecini[nmax]; + +double vcost[nmax]; + +multiset s; + +bool poz[nmax]; + + + +void dijkstra(int x) + +{ + + vcost[x] = 0; + + poz[x] = 1; + + + + for(int i = 0; i<(int)vecini[x].size(); i++) + + { + + dij aux; + + + + aux.nr = vecini[x][i].nr; + + aux.cost = vecini[x][i].cost; + + + + s.insert(aux); + + } + + + + while(s.empty()==0) + + { + + dij aux = *s.begin(); + + + + if(poz[aux.nr]==0) + + { + + vcost[aux.nr] = aux.cost; + + poz[aux.nr] = 1; + + + + for(int i = 0; i<(int)vecini[aux.nr].size(); i++) + + { + + if(poz[vecini[aux.nr][i].nr]==0) + + { + + dij aux2; + + + + aux2.cost = aux.cost + vecini[aux.nr][i].cost; + + aux2.nr = vecini[aux.nr][i].nr; + + + + s.insert(aux2); + + } + + } + + } + + + + s.erase(s.begin()); + + } + +} + + + +void dijkstra2(int x) + +{ + + vcost[x] = 0; + + poz[x] = 1; + + d[x] = 1; + + + + for(int i = 0; i<(int)vecini[x].size(); i++) + + { + + dij aux; + + + + aux.nr = vecini[x][i].nr; + + aux.cost = vecini[x][i].cost; + + + + if(aux.cost - vcost[aux.nr]-eps) + + { + + d[aux.nr] += d[x]; + + d[aux.nr] %= mod; + + } + + + + s.insert(aux); + + } + + + + while(s.empty()==0) + + { + + dij aux = *s.begin(); + + + + if(poz[aux.nr]==0) + + { + + vcost[aux.nr] = aux.cost; + + poz[aux.nr] = 1; + + + + for(int i = 0; i<(int)vecini[aux.nr].size(); i++) + + { + + dij aux2; + + + + aux2.cost = aux.cost + vecini[aux.nr][i].cost; + + aux2.nr = vecini[aux.nr][i].nr; + + + + if(aux2.cost - vcost[aux2.nr]-eps) + + { + + d[aux2.nr] += d[aux.nr]; + + d[aux2.nr] %= mod; + + } + + + + if(poz[vecini[aux.nr][i].nr]==0) + + { + + + + s.insert(aux2); + + } + + } + + } + + + + s.erase(s.begin()); + + } + +} + + + +int main(){ + + int player_unu=0; + + + + in>>n>>m; + + for(int i = 0; i>a>>b>>cost; + + + + cost = log(cost); + + + + dij aux1; + + aux1.nr = b; + + aux1.cost = cost; + + vecini[a].push_back(aux1); + + + + dij aux2; + + aux2.nr = a; + + aux2.cost = cost; + + vecini[b].push_back(aux2); + + } + + + + dijkstra(1); + + + + for(int i = 1; i<=n; i++) + + poz[i] = 0; + + s.clear(); + + + + dijkstra2(1); + + + + for(int i = 2; i<=n; i++) + + out< +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int nodes, edges, roadsNum[1510]; + +double distances[1510]; + +vector < pair > G[1510]; + +queue QU; + +bool mark[1510]; + + + +class cmp { + +public: + + bool operator() (pair d1, pair d2) + + { + + return d1.second > d2.second; + + } + +}; + +priority_queue< pair, vector< pair >, cmp > H; + + + +void dijkstra(int node) + +{ + + roadsNum[node] = 1; + + + + for (int i = 2; i <= nodes; i++) + + distances[i] = 2000000000; + + + + H.push(make_pair(node, 0)); + + + + while (!H.empty()) { + + int crtNode = H.top().first; + + H.pop(); + + + + if (mark[crtNode]) + + continue; + + mark[crtNode] = true; + + + + for (vector < pair >::iterator it = G[crtNode].begin(); it != G[crtNode].end(); it++) { + + int q = it->first; + + if (!mark[it->first]) { + + if (distances[it->first] - (distances[crtNode] + it->second) > 1.e-10) { + + distances[it->first] = distances[crtNode] + it->second; + + H.push(make_pair(it->first, distances[it->first])); + + roadsNum[it->first] = roadsNum[crtNode]; + + } + + else if (abs(distances[it->first] - (distances[crtNode] + it->second)) <= 1.e-10) + + roadsNum[it->first] = (roadsNum[it->first] + roadsNum[crtNode]) % 104659; + + } + + } + + } + +} + + + +int main() + +{ + + f >> nodes >> edges; + + + + int n1, n2, c; + + for (int i = 1; i <= edges; i++) { + + f >> n1 >> n2 >> c; + + + + G[n1].push_back(make_pair(n2, log10(c))); + + G[n2].push_back(make_pair(n1, log10(c))); + + } + + + + dijkstra(1); + + + + + + for (int i = 2; i <= nodes; i++) + + g << roadsNum[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/12883b47-02b7-11ec-a2d5-2cf05d84bfc2.cpp b/distmin/djikstra/12883b47-02b7-11ec-a2d5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7da24872fd678cfa3868e676b520cb94f5f03962 --- /dev/null +++ b/distmin/djikstra/12883b47-02b7-11ec-a2d5-2cf05d84bfc2.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +using namespace std; + + +const int NMAX = 1505, mod = 104659, INF = 2e9; +const double eps = 1.e-10; +double dmin[NMAX]; +int sol[NMAX]; +bool vis[NMAX]; + +class cmp { + public: + bool operator () (int a, int b) const { + return dmin[a] > dmin[b]; + } +}; + +priority_queue , cmp> q; + +vector < pair > graph[NMAX]; + +void dijkstra (int node) { + sol[node] = 1; + q.push(node); + + while(!q.empty()) { + node = q.top(); + q.pop(); + + if(vis[node]) continue; + vis[node] = 1; + + for(int i = 0; i < graph[node].size(); ++ i) + if(!vis[graph[node][i].first]) { + int x = graph[node][i].first; + double c = graph[node][i].second; + + if(dmin[node] + c - dmin[x] < -eps) { + dmin[x] = dmin[node] + c; + sol[x] = sol[node]; + q.push(x); + } + else + if(fabs(dmin[node] + c - dmin[x]) < eps) + sol[x] = (sol[node] + sol[x]) % mod; + + } + } +} + +int main() { + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + int n, m, x, y, c, i; + + scanf("%d%d", &n, &m); + for(i = 1; i <= m; ++ i) { + scanf("%d%d%d", &x, &y, &c); + + graph[x].push_back(make_pair(y, log(c))); + graph[y].push_back(make_pair(x, log(c))); + } + + for(i = 2; i <= n; ++ i) + dmin[i] = 2e9; + + dijkstra(1); + + for(i = 2; i <= n; ++ i) + printf("%d ", sol[i]); + return 0; +} diff --git a/distmin/djikstra/12be744a-02b7-11ec-a6ac-2cf05d84bfc2.cpp b/distmin/djikstra/12be744a-02b7-11ec-a6ac-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd2d8c0d4950550c326839dc670874580208815e --- /dev/null +++ b/distmin/djikstra/12be744a-02b7-11ec-a6ac-2cf05d84bfc2.cpp @@ -0,0 +1,221 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int N,M,H[1505],HPOZ[1505],NRD[1505],Hdim; + +double D[1505]; + +vector < pair > G[1505]; + +void read() + +{ + + int i,a,b; + + double c; + + fin>>N>>M; + + for(i=1;i<=M;i++) + + { + + fin>>a>>b>>c; + + c=log(c); + + G[a].push_back(make_pair(b,c)); + + G[b].push_back(make_pair(a,c)); + + } + +} + +void up(int k) + +{ + + while(k/2 && D[H[k]] < D[H[k/2]]) + + { + + swap(H[k],H[k/2]); + + swap(HPOZ[H[k]],HPOZ[H[k/2]]); + + k/=2; + + } + +} + +void down(int k) + +{ + + int kp; + + while(2*k<=Hdim) + + { + + kp=2*k; + + if(kp+1<=Hdim && D[H[kp]] > D[H[kp+1]]) + + kp++; + + if(D[H[k]] > D[H[kp]]) + + { + + swap(H[k],H[kp]); + + swap(HPOZ[H[k]],HPOZ[H[kp]]); + + k=kp; + + } + + else + + break; + + } + +} + +int heap_extract() + +{ + + int x=H[1]; + + HPOZ[H[1]]=0; + + H[1]=H[Hdim]; + + HPOZ[H[1]]=1; + + Hdim--; + + down(1); + + return x; + +} + +void heap_insert(int nod) + +{ + + H[++Hdim]=nod; + + HPOZ[nod]=Hdim; + + up(Hdim); + +} + +void dijkstra() + +{ + + int i,x,nod; + + double cost; + + for(i=2;i<=N;i++) + + D[i]=INT_MAX; + + H[1]=1; + + HPOZ[1]=1; + + Hdim=1; + + NRD[1]=1; + + while(Hdim) + + { + + x=heap_extract(); + + for(size_t j=0;j D[x]+cost+0.0000000001) + + { + + D[nod] = D[x]+cost; + + NRD[nod] = NRD[x]; + + if(HPOZ[nod]==0) + + heap_insert(nod); + + else + + up(HPOZ[nod]); + + } + + else if(max(D[nod] - (D[x]+cost),-(D[nod] - (D[x]+cost))) <= 0.0000000001) + + { + + NRD[nod] = (NRD[nod]+NRD[x])%104659; + + if(HPOZ[nod]==0) + + heap_insert(nod); + + } + + } + + } + +} + +void print() + +{ + + for(int i=2;i<=N;i++) + + fout< +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +int nodes, edges, roadsNum[1510]; + +double distances[1510]; + +vector < pair > G[1510]; + +queue QU; + +bool mark[1510]; + + + +class cmp { + +public: + + bool operator() (pair d1, pair d2) + + { + + return d1.second > d2.second; + + } + +}; + +priority_queue< pair, vector< pair >, cmp > H; + + + +void dijkstra(int node) + +{ + + roadsNum[node] = 1; + + + + for (int i = 2; i <= nodes; i++) + + distances[i] = 2000000000; + + + + H.push(make_pair(node, 0)); + + + + while (!H.empty()) { + + int crtNode = H.top().first; + + H.pop(); + + + + if (mark[crtNode]) + + continue; + + mark[crtNode] = true; + + + + for (vector < pair >::iterator it = G[crtNode].begin(); it != G[crtNode].end(); it++) { + + int q = it->first; + + if (!mark[it->first]) { + + if (distances[it->first] - (distances[crtNode] + it->second) > 1.e-10) { + + distances[it->first] = distances[crtNode] + it->second; + + H.push(make_pair(it->first, distances[it->first])); + + roadsNum[it->first] = roadsNum[crtNode]; + + } + + else if (abs(distances[it->first] - (distances[crtNode] + it->second)) <= 1.e-10) + + roadsNum[it->first] = (roadsNum[it->first] + roadsNum[crtNode]) % 104659; + + } + + } + + } + +} + + + +int main() + +{ + + f >> nodes >> edges; + + + + int n1, n2, c; + + for (int i = 1; i <= edges; i++) { + + f >> n1 >> n2 >> c; + + + + G[n1].push_back(make_pair(n2, log10(c))); + + G[n2].push_back(make_pair(n1, log10(c))); + + } + + + + dijkstra(1); + + + + + + for (int i = 2; i <= nodes; i++) + + g << roadsNum[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/12f35d52-02b7-11ec-a9a6-2cf05d84bfc2.cpp b/distmin/djikstra/12f35d52-02b7-11ec-a9a6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2294457bcaaa1cc0ccc4f7cf355dbcb20981e948 --- /dev/null +++ b/distmin/djikstra/12f35d52-02b7-11ec-a9a6-2cf05d84bfc2.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +using namespace std; + + +const int NMAX = 1505, mod = 104659, INF = 2e9; +const double eps = 1.e-10; +double dmin[NMAX]; +int sol[NMAX]; +bool vis[NMAX]; + +class cmp { + public: + bool operator () (int a, int b) const { + return dmin[a] > dmin[b]; + } +}; + +priority_queue , cmp> q; + +vector < pair > graph[NMAX]; + +void dijkstra (int node) { + sol[node] = 1; + q.push(node); + + while(!q.empty()) { + node = q.top(); + q.pop(); + + if(vis[node]) continue; + vis[node] = 1; + + for(int i = 0; i < graph[node].size(); ++ i) + if(!vis[graph[node][i].first]) { + int x = graph[node][i].first; + double c = graph[node][i].second; + + if(fabs(dmin[node] + c - dmin[x]) < eps) + sol[x] = (sol[node] + sol[x]) % mod; + else + if(dmin[node] + c < dmin[x]) { + dmin[x] = dmin[node] + c; + sol[x] = sol[node]; + q.push(x); + } + } + } +} + +int main() { + freopen("dmin.in", "r", stdin); + freopen("dmin.out", "w", stdout); + int n, m, x, y, c, i; + + scanf("%d%d", &n, &m); + for(i = 1; i <= m; ++ i) { + scanf("%d%d%d", &x, &y, &c); + + graph[x].push_back(make_pair(y, log(c))); + graph[y].push_back(make_pair(x, log(c))); + } + + for(i = 2; i <= n; ++ i) + dmin[i] = 2e9; + + dijkstra(1); + + for(i = 2; i <= n; ++ i) + printf("%d ", sol[i]); + return 0; +} diff --git a/distmin/djikstra/13054245-02b7-11ec-ad4e-2cf05d84bfc2.cpp b/distmin/djikstra/13054245-02b7-11ec-ad4e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4723b0f2c27c29b58507d38fa1818780d6c4fdf8 --- /dev/null +++ b/distmin/djikstra/13054245-02b7-11ec-ad4e-2cf05d84bfc2.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +int d[1501], heap[1501], poz[1501], node, n, m, vecin, k; + +double dist[1501], cost; + +vector > G[1501]; + +void upDate(int pozi) + +{ + + int po; + + while(pozi/2>0) + + { + + po=pozi/2; + + if(dist[heap[pozi]]dist[heap[po+1]]) + + po++; + + if(dist[heap[po]]>n>>m; + + int a, b; + + double c; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>c; + + c=log(c); + + G[a].push_back(make_pair(b, c)); + + G[b].push_back(make_pair(a, c)); + + } + + for(int i=2;i<=n;i++) + + dist[i]=INT_MAX; + + d[1]=1; + +} + +void dijkstra() + +{ + + heap[++k]=1; + + poz[1]=k; + + while(k!=0) + + { + + node=heap[1]; + + swap(poz[heap[1]], poz[heap[k]]); + + swap(heap[1], heap[k--]); + + downDate(1); + + for(vector >::iterator it=G[node].begin();it!=G[node].end();it++) + + { + + vecin=it->first; + + cost=it->second; + + if(dist[vecin]-dist[node]-cost>0.0000000001) + + { + + dist[vecin]=dist[node]+cost; + + d[vecin]=d[node]; + + if(poz[vecin]==0) + + { + + heap[++k]=vecin; + + poz[vecin]=k; + + } + + upDate(poz[vecin]); + + } + + else + + if(max(dist[vecin]-dist[node]-cost, dist[node]+cost-dist[vecin])<=0.0000000001) + + d[vecin]=(d[vecin]+d[node])%104659; + + } + + } + +} + +void write() + +{ + + for(int i=2;i<=n;i++) + + fout< +#include +#include +#include + +using namespace std; + +ifstream fin( "dmin.in" ); ofstream fout( "dmin.out" ); + +const int nmax = 1500; +const int mod = 104659; +const double eps = 1e-10; + +int ans[ nmax + 1 ]; +bool f[ nmax + 1 ]; + +struct muchie{ + int x; double c; + + muchie() {} + muchie( int _x, double _c ) : x( _x ), c( _c ) {} +}; +typedef vector< muchie > graf; +graf g[ nmax + 1 ]; + +struct heap{ + int ind, c; + double sol; + + heap() {} + heap( int _ind, int _c, double _sol ) { + ind = _ind, c = _c, sol = _sol; + } + + inline bool operator < ( const heap &a ) const { + if ( sol != a.sol ) { + return ( sol > a.sol ); + } + return ( ind > a.ind ); + } +}; +priority_queue< heap > h; + +inline bool egal( double a, double b ) { + return ( a - eps <= b && b <= a + eps ); +} +void extinde( heap x ) { + for( graf::iterator it = g[ x.ind ].begin(); it != g[ x.ind ].end(); ++ it ) { + if ( f[ it -> x ] == 0 ) { + h.push( heap( it -> x, x.c, x.sol + (it -> c) ) ); + } + } +} +void dijkstra() { + h.push( heap( 1, 1, 0 ) ); + while ( !h.empty() ) { + heap x = h.top(); + h.pop(); + + if ( f[ x.ind ] == 0 ) { + f[ x.ind ] = 1; + + while ( !h.empty() && h.top().ind == x.ind && egal( h.top().sol, x.sol ) ) { + x.c += h.top().c; + x.c %= mod; + h.pop(); + } + ans[ x.ind ] = x.c; + + extinde( x ); + } + } +} +int main() { + int n, m; + fin >> n >> m; + for( int i = 0; i < m; ++ i ) { + int x, y; double c; + fin >> x >> y >> c; + c = log( c ); + g[ x ].push_back( muchie( y, c ) ); + g[ y ].push_back( muchie( x, c ) ); + } + + dijkstra(); + for( int i = 2; i <= n; ++ i ) { + fout << ans[ i ] << " "; + } + fout << "\n"; + + fin.close(); + fout.close(); + return 0; +} diff --git a/distmin/djikstra/13d647ce-02b7-11ec-b6e2-2cf05d84bfc2.cpp b/distmin/djikstra/13d647ce-02b7-11ec-b6e2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bed1bc97c7f834f119d3ed14327cd8f7c016e864 --- /dev/null +++ b/distmin/djikstra/13d647ce-02b7-11ec-b6e2-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int NMax = 1505; + +const int MOD = 104659; + +const double Eps = 1e-6; + + + +int Count[NMax]; + +double D[NMax]; + +bool Used[NMax]; + + + +vector < pair < int, double > > G[NMax]; + + + +inline void Dijkstra(){ + + set < pair < double, int > > T; + + T.insert(make_pair(0, 1)); + + D[1] = 0; + + while(!T.empty()){ + + auto R = *T.begin(); + + T.erase(R); + + for(auto it : G[R.second]){ + + if(D[it.first] > it.second + D[R.second]){ + + D[it.first] = it.second + D[R.second]; + + T.insert(make_pair(D[it.first], it.first)); + + } + + } + + } + +} + + + + + +int Solve(const int k){ + + if(Used[k]) return Count[k]; + + Used[k] = 1; + + for(auto it : G[k]){ + + if((max(D[it.first] - D[k] + it.second, -D[it.first] - D[k] + it.second)) < Eps){ + + Count[k] += Solve(it.first); + + Count[k] %= MOD; + + } + + } + + return Count[k]; + +} + + + +int main(){ + + int n, m, a, b, c; + + fin >> n >> m; + + for(int i = 1; i <= m; i++){ + + fin >> a >> b >> c; + + G[a].push_back(make_pair(b, log(c))); + + G[b].push_back(make_pair(a, log(c))); + + } + + for(int i = 1; i <= n; i++){ + + D[i] = INFINITY; + + } + + Dijkstra(); + + Count[1] = 1; + + Used[1] = 1; + + for(int i = 2; i <= n; i++) Count[i] = Solve(i); + + for(int i = 2; i <= n; i++) { + + fout << Count[i] << " "; + + } + + return 0; + +} diff --git a/distmin/djikstra/13eb7579-02b7-11ec-b576-2cf05d84bfc2.cpp b/distmin/djikstra/13eb7579-02b7-11ec-b576-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bed1bc97c7f834f119d3ed14327cd8f7c016e864 --- /dev/null +++ b/distmin/djikstra/13eb7579-02b7-11ec-b576-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int NMax = 1505; + +const int MOD = 104659; + +const double Eps = 1e-6; + + + +int Count[NMax]; + +double D[NMax]; + +bool Used[NMax]; + + + +vector < pair < int, double > > G[NMax]; + + + +inline void Dijkstra(){ + + set < pair < double, int > > T; + + T.insert(make_pair(0, 1)); + + D[1] = 0; + + while(!T.empty()){ + + auto R = *T.begin(); + + T.erase(R); + + for(auto it : G[R.second]){ + + if(D[it.first] > it.second + D[R.second]){ + + D[it.first] = it.second + D[R.second]; + + T.insert(make_pair(D[it.first], it.first)); + + } + + } + + } + +} + + + + + +int Solve(const int k){ + + if(Used[k]) return Count[k]; + + Used[k] = 1; + + for(auto it : G[k]){ + + if((max(D[it.first] - D[k] + it.second, -D[it.first] - D[k] + it.second)) < Eps){ + + Count[k] += Solve(it.first); + + Count[k] %= MOD; + + } + + } + + return Count[k]; + +} + + + +int main(){ + + int n, m, a, b, c; + + fin >> n >> m; + + for(int i = 1; i <= m; i++){ + + fin >> a >> b >> c; + + G[a].push_back(make_pair(b, log(c))); + + G[b].push_back(make_pair(a, log(c))); + + } + + for(int i = 1; i <= n; i++){ + + D[i] = INFINITY; + + } + + Dijkstra(); + + Count[1] = 1; + + Used[1] = 1; + + for(int i = 2; i <= n; i++) Count[i] = Solve(i); + + for(int i = 2; i <= n; i++) { + + fout << Count[i] << " "; + + } + + return 0; + +} diff --git a/distmin/djikstra/15cbcea8-b53b-4bc4-99e1-d78e31126f37.cpp b/distmin/djikstra/15cbcea8-b53b-4bc4-99e1-d78e31126f37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b20018210416f48820650bee20549b9afe02af1 --- /dev/null +++ b/distmin/djikstra/15cbcea8-b53b-4bc4-99e1-d78e31126f37.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef pair per; + +typedef vector::iterator it; + + + +int n,m,rez[1505]; + +double d[1505]; + +bitset<1505> viz; + +vector gr[1505]; + +set > s; + + + +void dij() { + + for(int i=1; i<=n; ++i) d[i]=2000000000; + + rez[1]=1; + + for(s.insert(make_pair(0,1));s.size();s.erase(s.begin())) { + + double cst=s.begin()->first; int nod=s.begin()->second; + + for(it i=gr[nod].begin(); i!=gr[nod].end(); ++i) { + + if(fabs(d[i->first]-cst-i->second)<1e-9) rez[i->first]=(rez[i->first]+rez[nod])%104659; + + else if(cst+i->secondfirst]) { + + d[i->first]=cst+i->second; + + rez[i->first]=rez[nod]; + + s.insert(make_pair(d[i->first],i->first)); + + } + + } + + } + +} + + + +int main() + +{ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(int i=1; i<=m; ++i) { + + int a,b,c; + + f>>a>>b>>c; + + double lg=log((double)c); + + gr[a].push_back(make_pair(b,lg)); + + gr[b].push_back(make_pair(a,lg)); + + } + + dij(); + + for(int i=2; i<=n; ++i) g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +const string problemName = "dmin"; +const string inputFile = problemName + ".in"; +const string outputFile = problemName + ".out"; + + +typedef long long int lld; +typedef pair PII; +typedef pair PIL; +typedef pair PLI; +typedef pair PLL; +typedef pair PID; +typedef pair PDI; + +const int INF = (1LL << 30) - 1; +const lld LINF = (1LL << 62) - 1; +const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; +const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; +const int NMAX = 1500 + 5; +const int MOD = 104659; +const double EPS = 1e-10; + +int comp(double a, double b) { + if(fabs(a - b) <= ( (fabs(a) > fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 0; + if((a - b) > ( (fabs(a) < fabs(b) ? fabs(b) : fabs(a)) * EPS)) + return 1; + return -1; +} + +struct cmp { + bool operator()(const PDI &A, const PDI &B) const { + return (comp(A.first, B.first) > 0); + } +}; + +int N, M; +vector V[NMAX]; +int sol[NMAX]; +double D[NMAX]; +bitset viz; +priority_queue, greater > PQ; +deque Q; + +void bf(int x) { + int i, y; + double z; + + for(i = 2; i <= N; i++) + D[i] = INF; + + sol[x] = 1; + Q.push_back(x); + viz[x] = 1; + + while(!Q.empty()) { + x = Q.front(); + Q.pop_front(); + + viz[x] = 0; + + for(auto nod : V[x]) { + y = nod.first; + z = nod.second; + + if(comp(D[x] + z, D[y]) < 0) { + D[y] = D[x] + z; + sol[y] = 0; + + if(!viz[y]) { + Q.push_back(y); + viz[y] = 1; + } + } + + if(comp(D[x] + z, D[y]) == 0) + sol[y] = (sol[y] + sol[x]) % MOD; + } + } +} + +void dijkstra(int x) { + int i, y; + double z; + + for(i = 2; i <= N; i++) + D[i] = INF; + + D[x] = 0.0; + PQ.push(make_pair(D[x], x)); + + while(!PQ.empty()) { + x = PQ.top().second; + PQ.pop(); + + if(viz[x]) + continue; + + viz[x] = 1; + + for(auto nod : V[x]) { + y = nod.first; + z = nod.second; + + if(comp(D[x] + z, D[y]) < 0) { + D[y] = D[x] + z; + PQ.push(make_pair(D[y], y)); + } + } + } +} + +int main() { + int i, x, y, z; + + + freopen(inputFile.c_str(), "r", stdin); + freopen(outputFile.c_str(), "w", stdout); + + + scanf("%d%d", &N, &M); + + while(M--) { + scanf("%d%d%d", &x, &y, &z); + V[x].push_back(make_pair(y, log2(z))); + V[y].push_back(make_pair(x, log2(z))); + } + + bf(1); + + for(i = 2; i <= N; i++) + printf("%d ", sol[i]); + + return 0; +} diff --git a/distmin/djikstra/1780a680-02b7-11ec-b48f-2cf05d84bfc2.cpp b/distmin/djikstra/1780a680-02b7-11ec-b48f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b52d4061f25eaa9cba882b636eed701f8fc28031 --- /dev/null +++ b/distmin/djikstra/1780a680-02b7-11ec-b48f-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +using namespace std; + + + +struct cell + +{ + + int nod,vecin; + + long double val; + + bool operator <(const cell& e) const + + { + + return val>e.val; + + } + +}; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const long double eps=0.000001; + +const int MODULO=104659; + +const int NMAX=1505; + + + +int n,m,dp[NMAX]; + +long double cost[NMAX]; + +vectorv[NMAX]; + +vector::iterator it; + +priority_queueQ; + + + +inline void DIJKSTRA() + +{ + + int i; + + long double aux; + + cell k,kk; + + for (i=1;i<=n;i++) cost[i]=1<<30; + + dp[1]=1;cost[1]=0; + + k.nod=1;k.val=0; + + Q.push(k); + + while (!Q.empty()) + + { + + k=Q.top(); + + Q.pop(); + + for (it=v[k.nod].begin();it!=v[k.nod].end();it++) + + { + + aux=(k.val+(*it).val); + + if (fabs(cost[(*it).vecin]-aux)<0.000001) + + dp[(*it).vecin]=(dp[(*it).vecin]+dp[k.nod])%MODULO; + + if (cost[(*it).vecin]>aux+0.000001) + + { + + cost[(*it).vecin]=aux; + + dp[(*it).vecin]=dp[k.nod]; + + kk.nod=(*it).vecin; + + kk.val=cost[(*it).vecin]; + + Q.push(kk); + + } + + } + + } + +} + + + +int main() + +{ + + int i,x,y; + + long double w,c; + + cell k; + + fin>>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + w=log(c); + + k.nod=x;k.vecin=y;k.val=w; + + v[x].push_back(k); + + swap(k.nod,k.vecin); + + v[y].push_back(k); + + } + + DIJKSTRA(); + + for (i=2;i<=n;i++) fout< +using namespace std; + + + +struct cell + +{ + + int nod,vecin; + + long double val; + + bool operator <(const cell& e) const + + { + + return val>e.val; + + } + +}; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const long double eps=0.000001; + +const int MODULO=104659; + +const int NMAX=1505; + + + +int n,m,dp[NMAX]; + +long double cost[NMAX]; + +vectorv[NMAX]; + +vector::iterator it; + +priority_queueQ; + + + +inline void DIJKSTRA() + +{ + + int i; + + long double aux; + + cell k,kk; + + for (i=1;i<=n;i++) cost[i]=1<<30; + + dp[1]=1;cost[1]=0; + + k.nod=1;k.val=0; + + Q.push(k); + + while (!Q.empty()) + + { + + k=Q.top(); + + Q.pop(); + + for (it=v[k.nod].begin();it!=v[k.nod].end();it++) + + { + + aux=(k.val+(*it).val); + + if (fabs(cost[(*it).vecin]-aux)aux+eps) + + { + + cost[(*it).vecin]=aux; + + dp[(*it).vecin]=dp[k.nod]; + + kk.nod=(*it).vecin; + + kk.val=cost[(*it).vecin]; + + Q.push(kk); + + } + + } + + } + +} + + + +int main() + +{ + + int i,x,y; + + long double w,c; + + cell k; + + fin>>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + w=log(c); + + k.nod=x;k.vecin=y;k.val=w; + + v[x].push_back(k); + + swap(k.nod,k.vecin); + + v[y].push_back(k); + + } + + DIJKSTRA(); + + for (i=2;i<=n;i++) fout< +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +const int MAX_N = 1505; +const double EPS = 1e-10; +const double INF = 1e10; +const int MOD = 104659; + +ifstream fin("dmin.in"); +ofstream fout("dmin.out"); + +typedef pair pdi; + + +int N, M; +vector> graph[MAX_N]; +double cost[MAX_N]; +int dmin[MAX_N]; +bool vis[MAX_N]; + +void dijkstra(int source); + +int main() { + fin >> N >> M; + for (int i = 1, a, b, c; i <= M; i += 1) { + fin >> a >> b >> c; + double cc = log(double(c)); + graph[a].push_back(make_pair(b, cc)); + graph[b].push_back(make_pair(a, cc)); + } + + dijkstra(1); + for (int i = 2; i <= N; i += 1) { + fout << dmin[i] << ' '; + } + fout << endl; + return 0; +} + +void dijkstra(int source) { + priority_queue, greater> heap; + heap.push(make_pair(0.0, source)); + + for (int i = 1; i <= N; i += 1) { + cost[i] = INF; + } + cost[source] = 0; + dmin[source] = 1; + + while (!heap.empty()) { + int node = heap.top().second; + heap.pop(); + + if (vis[node]) continue; + vis[node] = true; + + for (auto p: graph[node]) { + double c = cost[node] + p.second; + if (abs(c - cost[p.first]) < EPS) { + dmin[p.first] += dmin[node]; + if (dmin[p.first] >= MOD) { + dmin[p.first] -= MOD; + } + } else if (c < cost[p.first]) { + cost[p.first] = c; + dmin[p.first] = dmin[node]; + heap.push(make_pair(c, p.first)); + } + } + } +} diff --git a/distmin/djikstra/18174586-02b7-11ec-a376-2cf05d84bfc2.cpp b/distmin/djikstra/18174586-02b7-11ec-a376-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..557300bf3de13433426e724a218717b46f92df56 --- /dev/null +++ b/distmin/djikstra/18174586-02b7-11ec-a376-2cf05d84bfc2.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +using namespace std; + +int n, m, nr[1503]; + +double dmin[1503]; + +vector > v[1503]; + +bool use[1503]; + + + +class compare + +{ + +public: + + bool operator()(const int &a, const int &b) + + { + + if(dmin[a]>dmin[b]) + + return true; + + return false; + + } + +}; + + + +priority_queue , compare> q; + + + + + +void read() + +{ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + int a, b, c; + + double cc; + + scanf("%i %i", &n, &m); + + for(int i=1; i<=m; i++) + + { + + scanf("%i %i %i", &a, &b, &c); + + cc=log(c); + + v[a].push_back(make_pair(cc,b)); + + v[b].push_back(make_pair(cc,a)); + + } + + + +} + + + +void dijkstra() + +{ + + for(int i=2; i<=n; i++) + + dmin[i]=1<<30; + + + + q.push(1); + + nr[1]=1; + + int first; + + while(!q.empty()) + + { + + + + first=q.top(); + + q.pop(); + + if(use[first]) + + continue; + + use[first]=1; + + for(int i=0; i k=v[first][i]; + + + + if( fabs(dmin[k.second]-dmin[first]-k.first) < 1e-9 ) + + { + + nr[k.second]+=nr[first]; + + if(nr[k.second]>104659) nr[k.second]-=104659; + + } + + else if(dmin[k.second]>dmin[first]+k.first) + + { + + nr[k.second]=nr[first]; + + if(nr[k.second]>104659) nr[k.second]-=104659; + + dmin[k.second]=dmin[first]+k.first; + + q.push(k.second); + + } + + + + + + } + + } + + + +} + + + +int main() + +{ + + read(); + + dijkstra(); + + + + for(int i=2; i<=n; i++) + + printf("%i ", nr[i]); + + printf("\n"); + + return 0; + +} diff --git a/distmin/djikstra/186613d1-02b7-11ec-be92-2cf05d84bfc2.cpp b/distmin/djikstra/186613d1-02b7-11ec-be92-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8f9c382c1a7afed0c99866f7953d13dc72fd746 --- /dev/null +++ b/distmin/djikstra/186613d1-02b7-11ec-be92-2cf05d84bfc2.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +using namespace std; + +ifstream is("dmin.in"); +ofstream os("dmin.out"); + + + + +int N, M, x, y; +double D[1503], z; +int Nr[1503]; +vector > G[1503]; + + + +struct Comp + +{ + + bool operator() (const pair X, const pair &Y) const + + { + + return X.second > Y.second; + + } + +}; +priority_queue , vector >, Comp > P; + +void Input(); +void Dijkstra(); + +int main() +{ + Input(); + Dijkstra(); + + for ( int i = 2; i <= N; ++i ) + os << Nr[i] << " "; + is.close(); + os.close(); +} + +void Input() +{ + is >> N >> M; + for ( int i = 1; i <= M; ++i ) + { + is >> x >> y >> z; + G[x].push_back(make_pair(y,log10(z))); + + G[y].push_back(make_pair(x,log10(z))); + } + for ( int i = 1; i <= N; ++i ) + D[i] = 1<<30; + D[1] = 0; + Nr[1] = 1; +} + +void Dijkstra() +{ + P.push(make_pair(1,0)); + vector > :: iterator it; + int nd; + + double cs; + while ( !P.empty() ) + { + nd = P.top().first; + + cs = P.top().second; + P.pop(); + + + + if ( cs > D[nd] ) + + continue; + + + for ( it = G[nd].begin(); it != G[nd].end(); ++it ) + { + if ( it->second + cs <= D[it->first] - 1e-8 ) + { + D[it->first] = it->second + cs; + Nr[it->first] = Nr[nd]; + P.push(make_pair(it->first,D[it->first])); + Nr[it->first] %= 104659; + + } + else if ( fabs(((it->second+cs)-(D[it->first]))) <= 1e-8 ) + { + + Nr[it->first] += Nr[nd]; + + Nr[it->first] %= 104659; + + } + } + } +} diff --git a/distmin/djikstra/187dd064-02b7-11ec-acc8-2cf05d84bfc2.cpp b/distmin/djikstra/187dd064-02b7-11ec-acc8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5eba731366d0fc9ebf81802e72ca8147cbdf6235 --- /dev/null +++ b/distmin/djikstra/187dd064-02b7-11ec-acc8-2cf05d84bfc2.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +using namespace std; + +ifstream is("dmin.in"); +ofstream os("dmin.out"); + + + + +int N, M, x, y; +double D[1503], z; +int Nr[1503]; +vector > G[1503]; + + + +struct Comp + +{ + + bool operator() (const pair X, const pair &Y) const + + { + + return X.second > Y.second; + + } + +}; +priority_queue , vector >, Comp > P; + +void Input(); +void Dijkstra(); + +int main() +{ + Input(); + Dijkstra(); + + for ( int i = 2; i <= N; ++i ) + os << Nr[i] << " "; + is.close(); + os.close(); +} + +void Input() +{ + is >> N >> M; + for ( int i = 1; i <= M; ++i ) + { + is >> x >> y >> z; + G[x].push_back(make_pair(y,log10(z))); + + G[y].push_back(make_pair(x,log10(z))); + } + for ( int i = 1; i <= N; ++i ) + D[i] = 1<<30; + D[1] = 0; + Nr[1] = 1; +} + +void Dijkstra() +{ + P.push(make_pair(1,0)); + vector > :: iterator it; + int nd; + + double cs; + while ( !P.empty() ) + { + nd = P.top().first; + + cs = P.top().second; + P.pop(); + + + for ( it = G[nd].begin(); it != G[nd].end(); ++it ) + { + if ( it->second + cs <= D[it->first] - 1e-8 ) + { + D[it->first] = it->second + cs; + Nr[it->first] = Nr[nd]; + P.push(make_pair(it->first,D[it->first])); + Nr[it->first] %= 104659; + + } + else if ( fabs(((it->second+cs)-(D[it->first]))) <= 1e-8 ) + { + + Nr[it->first] += Nr[nd]; + + Nr[it->first] %= 104659; + + } + } + } +} diff --git a/distmin/djikstra/1898d9fe-02b7-11ec-b8cf-2cf05d84bfc2.cpp b/distmin/djikstra/1898d9fe-02b7-11ec-b8cf-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5eba731366d0fc9ebf81802e72ca8147cbdf6235 --- /dev/null +++ b/distmin/djikstra/1898d9fe-02b7-11ec-b8cf-2cf05d84bfc2.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +using namespace std; + +ifstream is("dmin.in"); +ofstream os("dmin.out"); + + + + +int N, M, x, y; +double D[1503], z; +int Nr[1503]; +vector > G[1503]; + + + +struct Comp + +{ + + bool operator() (const pair X, const pair &Y) const + + { + + return X.second > Y.second; + + } + +}; +priority_queue , vector >, Comp > P; + +void Input(); +void Dijkstra(); + +int main() +{ + Input(); + Dijkstra(); + + for ( int i = 2; i <= N; ++i ) + os << Nr[i] << " "; + is.close(); + os.close(); +} + +void Input() +{ + is >> N >> M; + for ( int i = 1; i <= M; ++i ) + { + is >> x >> y >> z; + G[x].push_back(make_pair(y,log10(z))); + + G[y].push_back(make_pair(x,log10(z))); + } + for ( int i = 1; i <= N; ++i ) + D[i] = 1<<30; + D[1] = 0; + Nr[1] = 1; +} + +void Dijkstra() +{ + P.push(make_pair(1,0)); + vector > :: iterator it; + int nd; + + double cs; + while ( !P.empty() ) + { + nd = P.top().first; + + cs = P.top().second; + P.pop(); + + + for ( it = G[nd].begin(); it != G[nd].end(); ++it ) + { + if ( it->second + cs <= D[it->first] - 1e-8 ) + { + D[it->first] = it->second + cs; + Nr[it->first] = Nr[nd]; + P.push(make_pair(it->first,D[it->first])); + Nr[it->first] %= 104659; + + } + else if ( fabs(((it->second+cs)-(D[it->first]))) <= 1e-8 ) + { + + Nr[it->first] += Nr[nd]; + + Nr[it->first] %= 104659; + + } + } + } +} diff --git a/distmin/djikstra/1901604f-02b7-11ec-b8f5-2cf05d84bfc2.cpp b/distmin/djikstra/1901604f-02b7-11ec-b8f5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07791e3db1f761628301cbf44e493ca2cb44b8fc --- /dev/null +++ b/distmin/djikstra/1901604f-02b7-11ec-b8f5-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +const char IN[] = "dmin.in"; + +const char OUT[] = "dmin.out"; + +const int MAX = 1514; + +const int MOD = 104659; + +const int INF = 1<<30; + + + +using namespace std; + + + +ifstream fin(IN); + +ofstream fout(OUT); + + + +typedef std:: pair P; + +vector

gr[MAX]; + +struct cmp{ + + bool operator()(const P &a,const P &b){ + + return ( a.second>b.second ); + + } + +}; + +priority_queue ,cmp> h; + + + +double d[MAX]; + +int fr[MAX],n; + +void dijkstra( ); + +int main( ) + +{ + + int m; + + fin >> n >> m; + + while(m--){ + + int x,y; + + double c,tr; + + fin >> x >> y >> c; + + tr=log10(c); + + gr[x].push_back( make_pair( y , tr ) ); + + gr[y].push_back( make_pair ( x , tr ) ); + + } + + for(register int i=2;i<=n;++i)d[i]=INF; + + dijkstra(); + + for(int i=2; i<=n; i++) + + fout< +#include +#include +#include +typedef std :: pair < int, double > NOD; + + + +std :: ifstream f("dmin.in"); + +std :: ofstream g("dmin.out"); + + + +int N, M, Count[1509]; + +double D[1509]; + + + +std :: vector < NOD > V[1509]; + +std :: set < NOD > H; + + + +inline void Read_Data() { + + f >> N >> M; + + + + double cost; + + for(int i = 1, x, y; i <= M; ++i) { + + f >> x >> y >> cost; + + cost = log(cost); + + V[x].push_back(std :: make_pair(y, cost)); + + V[y].push_back(std :: make_pair(x, cost)); + + } + +} + + + +inline double modul(double a) { + + if(a < 0) a *= -1.0; + + return a; + +} + + + +void Djikstra() { + + H.insert( {1, 0} ); + + + + while(!H.empty()) { + + NOD nod = *H.begin(); + + H.erase( *H.begin() ); + + + + for(auto val : V[nod.first]) + + if(D[val.first] - val.second - nod.second > 1e-10) { + + Count[val.first] = Count[nod.first] % 104659; + + H.erase( {val.first, D[val.first]} ); + + + + D[val.first] = nod.second + val.second; + + H.insert( {val.first, D[val.first]} ); + + } else if(modul(D[val.first] - nod.second - val.second) < 1e-10) + + Count[val.first] = (Count[val.first] + Count[nod.first] ) % 104659; + + + + } + +} + + + +int main() { + + Read_Data(); + + + + for(int i = 2; i <= N; ++i) D[i] = (1 << 30); + + Count[1] = 1; + + Djikstra(); + + + + for(int i = 2; i <= N; ++i) g << Count[i] << ' '; + + + + g.close(); + + return 0; + +} diff --git a/distmin/djikstra/19c55a3d-02b7-11ec-85d8-2cf05d84bfc2.cpp b/distmin/djikstra/19c55a3d-02b7-11ec-85d8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a5771b6e5e6f0e109069bb4601bff6a33c80dd6 --- /dev/null +++ b/distmin/djikstra/19c55a3d-02b7-11ec-85d8-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 1507; + +const int INF = 10000000000; + +const int Mod = 104659; + +const double EPS = 0.00000000001; + + + +priority_queue< pair< double, int > > q; + +double D[NMAX]; + +int Dp[NMAX], Viz[NMAX]; + +vector< pair< int, double > > v[NMAX]; + +int n, m; + + + +int ok(double a, double b){ + + if(a > b + EPS) + + return 1; + + if(b > a + EPS) + + return -1; + + return 0; + +} + + + +void Dijkstra(){ + + for(int i = 2 ; i <= n ; ++ i) + + D[i] = INF; + + q.push(make_pair(0 , 1)); + + while(! q.empty()){ + + int Nod = q.top().second; + + q.pop(); + + if(Viz[Nod] == 0){ + + Viz[Nod] = 1; + + pair< int, double > it; + + for(int i = 0; i < v[Nod].size(); ++i){ + + it = v[Nod][i]; + + if(ok(D[it.first], D[Nod] + it.second) == 0){ + + Dp[it.first] += Dp[Nod]; + + Dp[it.first] %= Mod; + + } + + if(ok(D[it.first], D[Nod] + it.second) == 1){ + + Dp[it.first] = Dp[Nod] % Mod; + + D[it.first] = D[Nod] + it.second; + + q.push(make_pair(-D[it.first] , it.first)); + + } + + } + + } + + } + +} + + + + + +int main(){ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int a, b; + + double c; + + scanf("%d %d %lf\n", &a, &b, &c); + + c = (double)log(c); + + v[a].push_back(make_pair(b, c)); + + v[b].push_back(make_pair(a, c)); + + } + + Dp[1] = 1; + + Dijkstra(); + + for(int i = 2; i <= n; ++i) + + printf("%d ", Dp[i]); + + return 0; + +} diff --git a/distmin/djikstra/1a222d84-02b7-11ec-a6fb-2cf05d84bfc2.cpp b/distmin/djikstra/1a222d84-02b7-11ec-a6fb-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebc662e2cb00d85ed94e8b7d0a17327ff3de3462 --- /dev/null +++ b/distmin/djikstra/1a222d84-02b7-11ec-a6fb-2cf05d84bfc2.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +typedef std :: pair < int, double > NOD; + +std :: ifstream f("dmin.in"); +std :: ofstream g("dmin.out"); + +int N, M, Count[1509]; +double D[1509]; + +std :: vector < NOD > V[1509]; +std :: set < NOD > H; + +inline void Read_Data() { + f >> N >> M; + + double cost; + for(int i = 1, x, y; i <= M; ++i) { + f >> x >> y >> cost; + cost = log(cost); + V[x].push_back(std :: make_pair(y, cost)); + V[y].push_back(std :: make_pair(x, cost)); + } +} + +inline double modul(double a) { + if(a < 0) a *= -1.0; + return a; +} + +void Djikstra() { + H.insert( {1, 0} ); + + while(!H.empty()) { + NOD nod = *H.begin(); + H.erase( *H.begin() ); + + for(auto val : V[nod.first]) + if(D[val.first] - val.second - nod.second > 1e-10) { + Count[val.first] = Count[nod.first] % 104659; + H.erase( {val.first, D[val.first]} ); + + D[val.first] = nod.second + val.second; + H.insert( {val.first, D[val.first]} ); + } else if(modul(D[val.first] - nod.second - val.second) < 1e-10) + Count[val.first] = (Count[val.first] + Count[nod.first] ) % 104659; + + } +} + +int main() { + Read_Data(); + + for(int i = 2; i <= N; ++i) D[i] = (1 << 30); + Count[1] = 1; + Djikstra(); + + for(int i = 2; i <= N; ++i) g << Count[i] << ' '; + + g.close(); + return 0; +} diff --git a/distmin/djikstra/1a2ece0d-02b7-11ec-ba2e-2cf05d84bfc2.cpp b/distmin/djikstra/1a2ece0d-02b7-11ec-ba2e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faf1083b410b028da95cb9b2ea04959fbaecefb8 --- /dev/null +++ b/distmin/djikstra/1a2ece0d-02b7-11ec-ba2e-2cf05d84bfc2.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +using namespace std; +int n; + +int nr[1502],viz[1502]; + +double dist[1502]; + + + +vector > v[1502]; + + + +inline int cmp(double a,double b) + +{ + + if(a>b+0.0000000001) + + return 1; + + if(a+0.0000000001 +#include +#include +#include +#include +const long double EPS = 1e-8; + +const int NMAX = 1505; + +const int MOD = 104659; + + + +using namespace std; + + + +long double D[NMAX]; + +int P[NMAX]; + +int T[NMAX]; + +vector A[NMAX]; + +vector C[NMAX]; + +int N, M; + +queue > Q; + + + +int comp(long double x, long double y) { + + double rez = x-y; + + if (rez < -EPS) + + return -1; + + if (rez > EPS) + + return 1; + + return 0; + +} + + + +void dijkstra() { + + D[1] = 0; + + for ( int i = 2; i <= N; i++) { + + D[i] = 250000; + + } + + Q.push(make_pair(1, (long double)0)); + + while (!Q.empty()) { + + int idx; double dist; + + idx = Q.front().first; + + dist = Q.front().second; + + Q.pop(); + + for ( int i = 0; i < A[idx].size(); i++) { + + if (comp(dist + C[idx][i], D[A[idx][i]]) < 0) { + + D[A[idx][i]] = C[idx][i] + dist; + + P[A[idx][i]] = 1; + + T[A[idx][i]] = idx; + + Q.push(make_pair(A[idx][i], D[A[idx][i]])); + + } else if (comp(dist + C[idx][i], D[A[idx][i]]) == 0) { + + P[A[idx][i]]++; + + + + } + + } + + } + +} + + + +int compute(int idx) { + + int sol = 1; + + while(idx != 1) { + + sol = (sol * P[idx])%MOD; + + idx = T[idx]; + + } + + return sol; + +} + + + +int main() { + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + + + for ( int i = 1, x, y, c; i <= M; i++) { + + scanf("%d %d %d", &x, &y, &c); + + A[x].push_back(y); C[x].push_back(log10l(c)); + + A[y].push_back(x); C[y].push_back(log10l(c)); + + } + + + + dijkstra(); + + + + for ( int i = 2; i <= N; i++) { + + printf("%d ", compute(i)); + + } + + printf("\n"); + + + + return 0; + +} diff --git a/distmin/djikstra/1adcc4a6-02b7-11ec-8f7b-2cf05d84bfc2.cpp b/distmin/djikstra/1adcc4a6-02b7-11ec-8f7b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..977d098267ca2737ad1b881a3ee5b292c28e6471 --- /dev/null +++ b/distmin/djikstra/1adcc4a6-02b7-11ec-8f7b-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int NMAX = 1505, MOD = 104659; + +const double EPS = 1e-8; + + + +int DP[NMAX], N, M, X, Y, C; + +double Dist[NMAX]; + +vector > G[NMAX]; + + + +struct Comp + +{ + + bool operator() (const pair A, const pair &B) const + + { + + return A.second > B.second; + + } + +}; + + + +priority_queue, vector >, Comp> Q; + + + +void Go_Dijkstra() + +{ + + for(int i = 1; i <= N; ++ i) Dist[i] = 999999999; + + + + Dist[1] = 0; + + DP[1] = 1; + + Q.push(make_pair(1, 0.0)); + + + + while(!Q.empty()) + + { + + double Cost = Q.top().second; + + int Node = Q.top().first; + + Q.pop(); + + + + if(Cost > Dist[Node]) continue; + + + + for(vector > :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it) + + { + + if(Cost + it -> second < Dist[it -> first] || fabs(Cost + it -> second - Dist[it -> first]) < EPS) + + { + + if(Cost + it -> second <= Dist[it -> first] - EPS) + + { + + Dist[it -> first] = Cost + it -> second; + + DP[it -> first] = 0; + + Q.push(make_pair(it -> first, Dist[it -> first])); + + } + + DP[it -> first] = (DP[it -> first] + DP[Node]) % MOD; + + } + + } + + } + + + + for(int i = 2; i <= N; ++ i) + + fout << DP[i] << " "; + +} + + + +int main() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; ++ i) + + { + + fin >> X >> Y >> C; + + G[X].push_back(make_pair(Y, log(1.0 * C))); + + G[Y].push_back(make_pair(X, log(1.0 * C))); + + } + + + + Go_Dijkstra(); + + + + return 0; + +} diff --git a/distmin/djikstra/1b350183-02b7-11ec-bd68-2cf05d84bfc2.cpp b/distmin/djikstra/1b350183-02b7-11ec-bd68-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c1344f801fb60fbe84dab13087053902c117f46 --- /dev/null +++ b/distmin/djikstra/1b350183-02b7-11ec-bd68-2cf05d84bfc2.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1510]; + +int x,y,n,m,i,A[1510],d,H[1510],L,P[1510]; + +double c,D[1510]; + +double ab(double x){if(x<0) return -x; return x;} + +void urca(int po) + +{ + + while(po&&D[H[po]]>1]]) + + { + + swap(H[po],H[po>>1]); + + swap(P[H[po]],P[H[po>>1]]); + + po>>=1; + + } + +} + +void coboara(int po){ + + int y=0; + + while(po!=y){ + + y=po; + + if((y<<1)<=L&&D[H[y<<1]]>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,log(c))); + + v[y].push_back(make_pair(x,log(c))); + + } + + for(i=2;i<=n;++i) + + D[i]=1<<30; + + H[++L]=1; + + P[1]=1; + + A[1]=1; + + while(L) + + { + + x=H[1]; + + H[1]=H[L--]; + + coboara(1); + + for(i=0;i0.000001&&D[x]+c=104659) + + A[d]-=104659; + + } + + } + + } + + for(i=2;i<=n;++i) + + g< +#include +#include +#include +#include +#include +using namespace std; + +int n,i,j,k,m,x,y,rez[1501]; + +double dist[1501]; + +vector >v[1501]; + +dequec; + +bool viz[1501]; + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d %d",&n,&m); + + for (i=1;i<=m;i++) + + { + + double cost; + + scanf("%d %d %lf",&x,&y,&cost); + + cost=log(cost); + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(i=2;i<=n;i++) dist[i]=2000000000; + + c.push_back(1);rez[1]=1;viz[1]=true; + + while (!c.empty()) + + { + + x=c.front();c.pop_front();viz[x]=false; + + for (j=0;jfabs(dist[x]+cost-dist[y])) + + { + + rez[y]+=rez[x]; + + if (rez[y]>=104659) rez[y]-=104659; + + } + + } + + } + + for (i=2;i<=n;i++) printf("%d ",rez[i]); + + return 0; + +} diff --git a/distmin/djikstra/1bbb437d-02b7-11ec-abfc-2cf05d84bfc2.cpp b/distmin/djikstra/1bbb437d-02b7-11ec-abfc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf667d4f3cddcf243e97d804c40591d58a651c19 --- /dev/null +++ b/distmin/djikstra/1bbb437d-02b7-11ec-abfc-2cf05d84bfc2.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +bool Equal (double a, double b) + +{ + + return fabs(a-b)<1e-8; + +} + + + +typedef pair PI; + + + +struct CMP + +{ + + bool operator () (const PI& a, const PI& b) const + + { + + return a.second>b.second; + + } + +}; + + + +priority_queue, CMP> Q; + +int N, M; + +vector G[1510]; + +double Dist[1510]; + +int DP[1510]; + + + +void Read () + +{ + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + int a, b, c; + + f >> a >> b >> c; + + G[a].push_back(make_pair(b, log(1.0*c))); + + G[b].push_back(make_pair(a, log(1.0*c))); + + } + + + + f.close(); + +} + + + +void DoDijkstra () + +{ + + vector::iterator it; + + int i, first; + + double second; + + + + for (i=1; i<=N; i++) + + Dist[i]=999999999; + + + + Dist[1]=0; + + DP[1]=1; + + Q.push(make_pair(1, 0)); + + + + while (!Q.empty()) + + { + + first=Q.top().first; + + second=Q.top().second; + + Q.pop(); + + + + if (!Equal(Dist[first], second)) + + continue; + + + + for (it=G[first].begin(); it!=G[first].end(); ++it) + + if (second+it->secondfirst] || Equal(second+it->second, Dist[it->first])) + + { + + if (second+it->second<=Dist[it->first]-1e-8) + + { + + Dist[it->first]=second+it->second; + + Q.push(make_pair(it->first, Dist[it->first])); + + DP[it->first]=0; + + } + + DP[it->first]+=DP[first]; + + if (DP[it->first]>=104659) + + DP[it->first]-=104659; + + } + + } + +} + + + +void Print () + +{ + + for (int i=2; i<=N; i++) + + g << DP[i] << ' '; + + g << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + DoDijkstra(); + + Print(); + + + + return 0; + +} diff --git a/distmin/djikstra/1bd08328-02b7-11ec-beaa-2cf05d84bfc2.cpp b/distmin/djikstra/1bd08328-02b7-11ec-beaa-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..567749d0e93baa5e64156549b2fd1b8ddc68929c --- /dev/null +++ b/distmin/djikstra/1bd08328-02b7-11ec-beaa-2cf05d84bfc2.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +bool Equal (double a, double b) + +{ + + return fabs(a-b)<1e-8; + +} + + + +typedef pair PI; + + + +struct CMP + +{ + + bool operator () (const PI& a, const PI& b) const + + { + + return a.second>b.second; + + } + +}; + + + +priority_queue, CMP> Q; + +int N, M; + +vector G[1510]; + +double Dist[1510]; + +int DP[1510]; + + + +void Read () + +{ + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + int a, b, c; + + f >> a >> b >> c; + + G[a].push_back(make_pair(b, log(1.0*c))); + + G[b].push_back(make_pair(a, log(1.0*c))); + + } + + + + f.close(); + +} + + + +void DoDijkstra () + +{ + + vector::iterator it; + + int i, first; + + double second; + + + + for (i=1; i<=N; i++) + + Dist[i]=999999999; + + + + Dist[1]=0; + + DP[1]=1; + + Q.push(make_pair(1, 0)); + + + + while (!Q.empty()) + + { + + first=Q.top().first; + + second=Q.top().second; + + Q.pop(); + + + + if (!Equal(Dist[first], second)) + + continue; + + + + for (it=G[first].begin(); it!=G[first].end(); ++it) + + if (second+it->secondfirst] || Equal(second+it->second, Dist[it->first])) + + { + + if (second+it->second<=Dist[it->first]-1e-8) + + { + + Dist[it->first]=second+it->second; + + Q.push(make_pair(it->first, Dist[it->first])); + + DP[it->first]=0; + + } + + DP[it->first]+=DP[first]; + + if (DP[it->first]>=104659) + + DP[it->first]-=104659; + + } + + } + +} + + + +void Print () + +{ + + for (int i=2; i<=N; i++) + + g << DP[i] << ' '; + + g << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + DoDijkstra(); + + Print(); + + + + return 0; + +} diff --git a/distmin/djikstra/1c4ab6a4-02b7-11ec-9a4d-2cf05d84bfc2.cpp b/distmin/djikstra/1c4ab6a4-02b7-11ec-9a4d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5df667284535ee570014ade3ef5e20e104dbe0fe --- /dev/null +++ b/distmin/djikstra/1c4ab6a4-02b7-11ec-9a4d-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +priority_queue < pair < double, int > > PQ; + + + +vector < pair < int, double > > v[1505]; + + + +double D[1505]; + + + +int NR[1505]; + + + +bitset < 1505 > inQueue; + + + +int N, M; + +void read(){ + freopen("dmin.in", "r", stdin); + + + scanf("%d %d", &N, &M); + + + + int x, y, c; + + while(M--){ + + scanf("%d %d %d", &x, &y, &c); + + + + v[x].push_back(make_pair(y,log(c))); + + v[y].push_back(make_pair(x,log(c))); + + } + + fclose(stdin); +} + + +void Dijkstra(){ + + for(int i=2; i<=N; ++i) + + D[i] = 9999999.0; + + + + NR[1] = 1; + + PQ.push(make_pair(0.0, 1)); + + inQueue[1] = true; + + + + while(!PQ.empty()){ + + double dx = -PQ.top().first; + + int x = PQ.top().second; + + + + PQ.pop(); + + inQueue[x] = false; + + + + for(typeof(v[x].begin()) it=v[x].begin(); it!=v[x].end(); ++it) + + if(D[(*it).first] - dx - (*it).second > 0.000000001){ + + D[(*it).first] = dx + (*it).second; + + NR[(*it).first] = NR[x]; + + + + if(!inQueue[(*it).first]){ + + PQ.push(make_pair(-D[(*it).first], (*it).first)); + + inQueue[(*it).first] = true; + + } + + } + + else if(abs(D[(*it).first] - dx - (*it).second) < 0.000000001) + + NR[(*it).first] = (0LL + NR[(*it).first] + NR[x]) % 104659; + + } +} + +void print(){ + freopen("dmin.out", "w", stdout); + + + + for(int i=2; i<=N; ++i) + + printf("%d ", NR[i]); + + fclose(stdout); +} + +int main(){ + read(); + Dijkstra(); + print(); + + return 0; +} diff --git a/distmin/djikstra/1cacb809-02b7-11ec-abc3-2cf05d84bfc2.cpp b/distmin/djikstra/1cacb809-02b7-11ec-abc3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21eb2ae49abad7903581ac0b310d66069e28b10c --- /dev/null +++ b/distmin/djikstra/1cacb809-02b7-11ec-abc3-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +FILE *f=fopen("dmin.in","r"); + +FILE *g=fopen("dmin.out","w"); + + + +using namespace std; + + + +vector >G[50005]; + +vector::iterator it; + +int n,m; + +double dist[50005]; + +int res[50005]; + +priority_queue,vector >,greater > > HEAP; + +bool used[50005]; + + + +void read( void ) + +{ + + fscanf(f,"%d%d",&n,&m); + + + + for(int i(1); i <=m ; ++i ) + + { + + int a,b; + + double c; + + fscanf(f,"%d%d%lf",&a,&b,&c); + + c=log(c); + + G[a].push_back(make_pair(b,c)); + + G[b].push_back(make_pair(a,c)); + + } + + fclose(f); + + + +} + +void DIJKSTRA( void ) + +{ + + for(int i(1); i<=n ; dist[i++]=0x3f3f3f3f); + + + + dist[1]=0; + + res[1]=1; + + HEAP.push(make_pair(0,1)); + + + + while(!HEAP.empty()) + + { + + int node=HEAP.top().second; + + HEAP.pop(); + + if( used[node]) + + continue; + + used[node]=1; + + + + for(int i(0); i dist[node]+dis + 1e-10) + + { + + dist[newnode]=dist[node]+dis; + + res[newnode]=res[node]; + + HEAP.push(make_pair(dist[newnode],newnode)); + + continue; + + } + + + + if( abs (dist[newnode] - dist[node]-dis ) <= 1e-10 ) + + { + + res[newnode]+=res[node]; + + res[newnode]%=104659; + + continue; + + } + + + + } + + + + } + + + + + +} + +void write( void ) + +{ + + for(int i(2); i <=n; ++i ) + + + + fprintf(g,"%d ",res[i]); + + + + fclose(g); + +} + + + + + +int main( void ) + +{ + + read(); + + DIJKSTRA(); + + write(); + + return 0; + +} diff --git a/distmin/djikstra/1cca399c-02b7-11ec-819b-2cf05d84bfc2.cpp b/distmin/djikstra/1cca399c-02b7-11ec-819b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1580aa0c030c94fdd27af4daa945d67175af0fc1 --- /dev/null +++ b/distmin/djikstra/1cca399c-02b7-11ec-819b-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +using namespace std; + +FILE *fin,*fout; + +vector < pair > G[1505]; + +int n,k,apar[1505]; + +double d[1505]; + +priority_queue< pair, vector >, greater > > HEAP; + +bool use[1505]; + +void dijkstra(short nod) + +{ + +short vec; + +double cost; + +for(int i=1;i<=n;i++) + +d[i]=(1<<30); + +d[nod]=0; + +apar[1]=1; + +HEAP.push(make_pair(0,nod)); + +while(!HEAP.empty()) + +{ + +nod=HEAP.top().second; + +HEAP.pop(); + +if(use[nod]) + +continue; + +use[nod]=1; + +for(size_t i=0;id[nod]+cost+0.000001) + +{ + +d[vec]=d[nod]+cost; + +apar[vec]=apar[nod]; + +HEAP.push(make_pair(d[vec],vec)); + +continue; + +} + +if(abs(d[vec]-d[nod]-cost)<=0.000001) + +{ + +apar[vec]+=apar[nod]; + +apar[vec]%=104659; + +} + +} + +} + +} + +void read() + +{ + +fin=fopen("dmin.in","r"); + +int x,y,m; + +double z; + +fscanf(fin,"%d %d",&n,&m); + +while(m--) + +{ + +fscanf(fin,"%d %d %lf",&x,&y,&z); + +z=log(z); + +G[x].push_back(make_pair(y,z)); + +G[y].push_back(make_pair(x,z)); + +} + +fclose(fin); + +} + +void print() + +{ + +fout=fopen("dmin.out","w"); + +for(int i=2;i<=n;i++) + +fprintf(fout,"%d ",apar[i]); + +fclose(fout); + +} + +int main() + +{ + +read(); + +dijkstra(1); + +print(); + +return 0; + +} diff --git a/distmin/djikstra/1ce59bed-02b7-11ec-a87b-2cf05d84bfc2.cpp b/distmin/djikstra/1ce59bed-02b7-11ec-a87b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..454faca1f63b69785a697bcea9341f4e356aaad3 --- /dev/null +++ b/distmin/djikstra/1ce59bed-02b7-11ec-a87b-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +using namespace std; + +FILE *fin,*fout; + +vector < pair > G[1505]; + +int n,k,apar[1505]; + +double d[1505]; + +priority_queue< pair, vector >, greater > > HEAP; + +bool use[1505]; + +void dijkstra(short nod) + +{ + + short vec; + + double cost; + + for(int i=1;i<=n;i++) + + d[i]=(1<<30); + + d[nod]=0; + + apar[1]=1; + + HEAP.push(make_pair(0,nod)); + + while(!HEAP.empty()) + + { + + nod=HEAP.top().second; + + HEAP.pop(); + + if(use[nod]) + + continue; + + use[nod]=1; + + for(size_t i=0;id[nod]+cost+0.000001) + + { + + d[vec]=d[nod]+cost; + + apar[vec]=apar[nod]; + + HEAP.push(make_pair(d[vec],vec)); + + continue; + + } + + if(abs(d[vec]-d[nod]-cost)<=0.000001) + + { + + apar[vec]+=apar[nod]; + + apar[vec]%=104659; + + } + + } + + } + +} + +void read() + +{ + + fin=fopen("dmin.in","r"); + + int x,y,m; + + double z; + + fscanf(fin,"%d %d",&n,&m); + + while(m--) + + { + + fscanf(fin,"%d %d %lf",&x,&y,&z); + + z=log(z); + + G[x].push_back(make_pair(y,z)); + + G[y].push_back(make_pair(x,z)); + + } + + fclose(fin); + +} + +void print() + +{ + + fout=fopen("dmin.out","w"); + + for(int i=2;i<=n;i++) + + fprintf(fout,"%d ",apar[i]); + + fclose(fout); + +} + +int main() + +{ + + read(); + + dijkstra(1); + + print(); + + return 0; + +} diff --git a/distmin/djikstra/1cfdde3e-02b7-11ec-b43a-2cf05d84bfc2.cpp b/distmin/djikstra/1cfdde3e-02b7-11ec-b43a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa60b32c1db94b601e2597328a5b77e988b68eb0 --- /dev/null +++ b/distmin/djikstra/1cfdde3e-02b7-11ec-b43a-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef pair pereche; + +vector < list < pereche > > Graf; + +vector < double > d; + +int vizitat[1501], drumuri[1501]; + +const int modulo = 104659; + +const double epsilon = 1e-10; + +struct order{ + + bool operator()(const int &i,const int &j){ + + return d[i] > d[j]; + + } + +}; + +priority_queue,order> Heap; + +int n,m; + + + +void citesc(){ + + + + int x,y; + + double c; + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + Graf.resize(n+1); + + d.resize(n+1,1000000001); + + for(register int i=1;i<=m;++i){ + + scanf("%d%d%lf",&x,&y,&c); + + c = log(c); + + Graf[x].push_back(pereche(y,c)); + + Graf[y].push_back(pereche(x,c)); + + } + +} + + + +void Dijkstra(){ + + + + int top; + + vizitat[1] = 1; + + Heap.push(1); + + drumuri[1] = 1; + + d[1] = 0; + + while(!Heap.empty()){ + + + + top = Heap.top(); + + Heap.pop(); + + for(list::iterator it = Graf[top].begin();it!=Graf[top].end();++it) + + if(d[it->first] > d[top] + it->second + epsilon){ + + drumuri[it->first] = drumuri[top]; + + d[it->first] = d[top] + it->second; + + + + if(!vizitat[it->first]){ + + vizitat[it->first] = 1; + + Heap.push(it->first); + + } + + } + + else if(abs(d[it->first]- d[top] - it->second) <= epsilon){ + + drumuri[it->first] += drumuri[top]; + + drumuri[it->first] %= modulo; + + } + + } + +} + + + +int main(){ + + + + citesc(); + + Dijkstra(); + + for(register int i=2;i<=n;++i) + + printf("%d ",drumuri[i]); + +return 0; + +} diff --git a/distmin/djikstra/1d51a04d-02b7-11ec-bb94-2cf05d84bfc2.cpp b/distmin/djikstra/1d51a04d-02b7-11ec-bb94-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73a71b3dd1baa47458c45568173cabfa54908d06 --- /dev/null +++ b/distmin/djikstra/1d51a04d-02b7-11ec-bb94-2cf05d84bfc2.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +const int Nmax = 1501; +const double eps = 0.0000001; +const int modulo = 104659; + +vector numar(Nmax); +vector dist(Nmax); +bitset viz; +vector< pair > Graf[Nmax]; +queue Q; + +int N, M; + +void citire(){ + + ifstream f("dmin.in"); + + f >> N >> M; + + int a, b; + double c; + + for(; M; M--) { + + f >> a >> b >> c; + c = log(c); + + Graf[a].push_back(pair(b, c)); + Graf[b].push_back(pair(a, c)); + } +} + + +void dijkstra() { + + vector< pair >::iterator it; + + for(int i = 1; i <= N; ++i) + dist[i] = 9999999.0; + + Q.push(1); + dist[1] = 0; + numar[1] = 1; + viz[1] = 1; + + while(!Q.empty()){ + + int nod = Q.front(); + Q.pop(); + + for(it = Graf[nod].begin(); it != Graf[nod].end(); ++it){ + + if(dist[it->first] - dist[nod] - it->second > eps){ + + dist[it->first] = dist[nod] + it->second; + numar[it->first] = numar[nod]; + + if(!viz[it->first]){ + + viz[it->first] = 1; + Q.push(it->first); + } + } + else + if(dist[nod] + it->second - dist[it->first] <= eps){ + + numar[it->first] += numar[nod]; + numar[it->first] %= modulo; + } + } + } +} + +void afis(){ + + ofstream g("dmin.out"); + + for(int i = 2; i <= N; ++i) + g << numar[i] << " "; +} + +int main() { + + citire(); + dijkstra(); + afis(); + + return 0; +} diff --git a/distmin/djikstra/1d6ca37d-02b7-11ec-8fac-2cf05d84bfc2.cpp b/distmin/djikstra/1d6ca37d-02b7-11ec-8fac-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17583c19677ffeb0835bdc856720fa9bd30c1c0f --- /dev/null +++ b/distmin/djikstra/1d6ca37d-02b7-11ec-8fac-2cf05d84bfc2.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream in("dmin.in"); +ofstream out("dmin.out"); + +const int N = 1502; +const double eps = 0.0000001; +const int M = 104659; + +int n, m, sol[N], nod; +double dist[N]; +bool ver[N]; +vector > v[N]; +queue q; + +void dijkstra() { + int i; + vector >::iterator it; + + for(i = 1; i<=n; ++i) + dist[i] = 29999999.0; + + ver[1] = true; + q.push(1); + dist[1] = 0; + sol[1] = 1; + + while(!q.empty()) { + nod = q.front(); + q.pop(); + ver[nod] = false; + + for(it = v[nod].begin(); it!=v[nod].end(); ++it) { + if(dist[it->first] - dist[nod] - it->second > eps) { + dist[it->first] = dist[nod] + it->second; + sol[it->first] = sol[nod]; + + if(!ver[it->first]) { + q.push(it->first); + ver[it->first] = true; + } + } + else if(dist[nod] + it->second - dist[it->first] <= eps) { + sol[it->first] += sol[nod]; + sol[it->first]%=M; + } + } + } +} + +int main() { + int i, x, y; + double c; + + in >> n >> m; + + for(i = 1; i<=m; ++i) { + in >> x >> y >> c; + c = log(c); + + v[x].push_back(pair(y, c)); + v[y].push_back(pair(x, c)); + } + + dijkstra(); + + for(i = 2; i<=n; ++i) + out << sol[i] << " "; + + return 0; +} diff --git a/distmin/djikstra/1d84c442-02b7-11ec-aa29-2cf05d84bfc2.cpp b/distmin/djikstra/1d84c442-02b7-11ec-aa29-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2aacf5b743975aa28b00babe85eca396e06bd14c --- /dev/null +++ b/distmin/djikstra/1d84c442-02b7-11ec-aa29-2cf05d84bfc2.cpp @@ -0,0 +1,353 @@ +#include +#include +#include +using namespace std; +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + + + +int Poz[2100],Fr[2100],Nr_drum[2100]; + +int last,n,m,i; + +double Drum_min[2100]; + + + + + +struct heap{ + + double val; + + int poz; + +}Heap[2100]; + + + +struct nod{ + + int varf; + + double dist; + +}; + + + +vector L[2100]; + + + +inline void read(){ + + + + nod nod2; + + int nod1,x; + + + + f>>n>>m; + + + + for(int i=1;i<=m;++i){ + + f>>nod1>>nod2.varf>>x; + + nod2.dist=log(x); + + L[nod1].push_back(nod2); + + } + + + +} + + + +inline void initializare(){ + + for(int i=1;i<=n;i++){ + + Heap[++last].val=2000000000; + + Heap[last].poz=i; + + Poz[i]=i; + + } + + Heap[1].val=0; + + Nr_drum[1]=1; + +} + + + +inline int tata(int x){ + + return x/2; + +} + + + +inline int left_son(int x){ + + return 2*x; + +} + + + +inline int right_son(int x){ + + return 2*x+1; + +} + + + +inline void heap_down(int x){ + + + + int aux2,sec;heap aux1; + + int l1,l2; + + while(x!=last){ + + l1=left_son(x); + + l2=right_son(x); + + if(l1<=last&&l2<=last){ + + if(Heap[l1].val=Heap[x].val) + + break; + + + + aux2=Poz[Heap[sec].poz]; + + Poz[Heap[sec].poz]=Poz[Heap[x].poz]; + + Poz[Heap[x].poz]=aux2; + + aux1=Heap[sec]; + + Heap[sec]=Heap[x]; + + Heap[x]=aux1; + + x=sec; + + + + } + + + + + +} + + + +inline void heap_up(int x){ + + heap aux1;int aux2,T; + + while(Heap[T=tata(x)].val>Heap[x].val&&x!=1){ + + aux2=Poz[Heap[T].poz]; + + Poz[Heap[T].poz]=Poz[Heap[x].poz]; + + Poz[Heap[x].poz]=aux2; + + aux1=Heap[T]; + + Heap[T]=Heap[x]; + + Heap[x]=aux1; + + x=T; + + } + +} + + + +inline void update(int x){ + + heap_up(x); + + heap_down(x); + +} + + + +inline int pop(){ + + int nod1=Heap[1].poz; + + + + Drum_min[nod1]=Heap[1].val; + + + + Poz[Heap[last].poz]=1; + + + + Heap[1]=Heap[last--]; + + + + update(1); + + + + return nod1; + +} + + + +inline void update_drum(int x){ + + int poz; + + for(unsigned int i=0;iDrum_min[x]+L[x][i].dist+0.000000001&&Fr[L[x][i].varf]==0){ + + Heap[poz].val=Drum_min[x]+L[x][i].dist; + + Nr_drum[L[x][i].varf]=Nr_drum[x]; + + update(poz); + + } + + else if(Drum_min[x]+L[x][i].dist-Heap[poz].val<=0.000000001){ + + Nr_drum[L[x][i].varf]+=Nr_drum[x]; + + if(Nr_drum[L[x][i].varf]>=104659) + + Nr_drum[L[x][i].varf]-=104659; + + } + + } + + + +} + + + +inline void dijkstra(){ + + + + int x; + + + + while(last!=0){ + + x=pop(); + + Fr[x]=1; + + update_drum(x); + + } + + + +} + + + +int main(){ + + + + read(); + + + + initializare(); + + + + dijkstra(); + + + + for(i=2;i<=n;i++) + + g< +#include +#include +#include + + + + +using namespace std; + + + +int i,j,n,m; + +bool viz[1501]; + +unsigned long x,y,nr[1501]; + +double cost1,d[1501]; + +struct muchie + +{ + + int nod; + + double cost; + +}; + +vector a[1501]; + +void dijkstra(int x) + +{ + + int i; + + queue q; + + q.push(x); + + d[x]=1; + + nr[x]=1; + + while(!q.empty()) + + { + + x=q.front(); + + viz[x]=0; + + for(i=0;i0.0000000001) + + { + + d[a[x][i].nod]=d[x]+a[x][i].cost; + + nr[a[x][i].nod]=nr[x]; + + if(!viz[a[x][i].nod]) + + q.push(a[x][i].nod),viz[a[x][i].nod]=1; + + } + + else + + if(fabs(d[a[x][i].nod]-d[x]-a[x][i].cost)<=0.0000000001) + + { + + nr[a[x][i].nod]=(nr[a[x][i].nod]+nr[x])%104659; + + if(nr[a[x][i].nod]>104659) + + { + + nr[a[x][i].nod]-=104659; + + if(!viz[a[x][i].nod]) + + q.push(a[x][i].nod),viz[a[x][i].nod]=1; + + } + + } + + } + + q.pop(); + + } + +} + +int main() + +{ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>cost1; + + cost1=log(cost1); + + a[x].push_back((muchie) {y,cost1}); + + a[y].push_back((muchie) {x,cost1}); + + } + + for(i=1;i<=n;++i) + + d[i]=1<<20,nr[i]=0; + + dijkstra(1); + + for(i=2;i<=n;++i) + + g< +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +#include +#include +using namespace std; + + + +int v[3][5010]; + +double C[5010]; + +int n,m; + +struct s{ + + double cost; + + int drum; + +} a[1510]; + + + + + + + +void dijkstra() + +{ + + int i,t; + + for (i=1; i<=n; i++) a[i].cost=-1; + + a[1].cost=0; t=1; + + while (t) + + { + + t=0; + + for (i=1; i<=m*2; i++) + + if (a[v[1][i]].cost!=-1 && (a[v[2][i]].cost==-1 || a[v[2][i]].cost-a[v[1][i]].cost-C[i]>0.0000000001)) + + { + + a[v[2][i]].cost=a[v[1][i]].cost+C[i]; + + t=1; + + + + } + + } + + + + + + for (i=1; i<=n; i++) a[i].drum=0; a[1].drum=1; + + for (i=1; i<=m*2; i++) + + if ( abs(a[v[2][i]].cost-a[v[1][i]].cost-C[i])<=0.0000000001) + + { + + a[v[2][i]].drum=(a[v[2][i]].drum+a[v[1][i]].drum)%104659; + + } + +} + +void sw(int *a,int *b) + +{ + + int t=*a; *a=*b; *b=t; + +} + + + +void sw1(double *a,double *b) + +{ + + double t=*a; *a=*b; *b=t; + +} + + + +void qs(int left,int right) + +{ + + int i,j,p; + + i=left; j=right; p=v[1][(i+j)/2]; + + while (ip) j--; + + if (i<=j) + + { + + sw(&v[1][i],&v[1][j]); + + sw(&v[2][i],&v[2][j]); + + sw1(&C[i],&C[j]); + + i++; + + j--; + + } + + } + + if (ileft) qs(left,j); + +} + + + +int main() + +{ + + int i,c; + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%i%i",&n,&m); + + for (i=1; i<=m*2; i++) + + if (i%2==1) + + { + + scanf("%i%i%i",&v[1][i],&v[2][i],&c); + + C[i]=log(c); + + + + } + + else{ + + v[1][i]=v[2][i-1]; + + v[2][i]=v[1][i-1]; + + C[i]=C[i-1]; + + } + + qs(1,m*2); + + + + + + dijkstra(); + + for (i=2; i<=n; i++) printf("%i ",a[i].drum); + + return 0; + +} diff --git a/distmin/djikstra/1e5507c3-02b7-11ec-a119-2cf05d84bfc2.cpp b/distmin/djikstra/1e5507c3-02b7-11ec-a119-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b6d372e37ad4b5b3f9f07e5d27ee3dab4de4999 --- /dev/null +++ b/distmin/djikstra/1e5507c3-02b7-11ec-a119-2cf05d84bfc2.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); +int n, m; + +vector > g[1503]; + +queue < int >q; + +bool use[1503]; + +void read(){ + + fin >> n >> m; + + for(int i = 1; i <= m; i++){ + + int x, y; + + double c; + + fin >>x >>y >>c; + + c = log(c); + + g[x].push_back(make_pair(y,c)); + + g[y].push_back(make_pair(x,c)); + + + + } + +} + + + +void Dijkstra(int r){ + + int nr_drumuri[1503]; + + double dist[1503]; + + for(int i = 1; i <= n; i++) nr_drumuri[i] = 0, dist[i] = (1<<30);; + + + + dist[r] = 0; + + q.push(r); + + nr_drumuri[r] = 1; + + use[r] = true; + + while(! q.empty()){ + + int nod = q.front(); + + q.pop(); + + use[nod] = false; + + + + for(int i = 0 ; i < g[nod].size(); i++){ + + double cost = g[nod][i].second; + + int y = g[nod][i].first; + + if(fabs (dist[y] - dist[nod] - cost) < 0.0000001 ){ + + nr_drumuri[y] = (nr_drumuri[y] + nr_drumuri[nod] ) %104659; + + } + + if(dist[y] - dist[nod] - cost>0.0000001 ){ + + + + if(use[y] == false) + + q.push(y), use[y] = true; + + dist[y] = dist[nod] + cost; + + nr_drumuri[y] = nr_drumuri[nod] %104659; + + } + + + + + + } + + } + + for(int i = 2; i<= n ;i++) + + fout << nr_drumuri[i] <<" "; + +} + +int main(){ + + read(); + + Dijkstra(1); + + fin.close(); + + return 0; + + + +} diff --git a/distmin/djikstra/1ecd1425-02b7-11ec-b9a6-2cf05d84bfc2.cpp b/distmin/djikstra/1ecd1425-02b7-11ec-b9a6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dc0065edd26e28b60106f20edc0c3ebbee8519c --- /dev/null +++ b/distmin/djikstra/1ecd1425-02b7-11ec-b9a6-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +int n,m,i,j,drum[1501],x,y; + +double z,d[1501],eps=1e-10; + +struct planeta + +{ + + int nod; + + double cost; + +}; + +vector a[1501]; + +queueq; + +bool uz[1501]; + +void dijkstra(int x) + +{ + + int i; + + q.push(x); + + d[x]=0.00000; + + drum[x]=uz[x]=1; + + while(!q.empty()) + + { + + x=q.front(); + + q.pop(); + + uz[x]=0; + + for(i=0;i104659) + + { + + drum[a[x][i].nod]%=104659; + + if(!uz[a[x][i].nod]) + + q.push(a[x][i].nod),uz[a[x][i].nod]=1; + + } + + } + + if(d[a[x][i].nod]-d[x]-a[x][i].cost>eps) + + { + + d[a[x][i].nod]=d[x]+a[x][i].cost; + + drum[a[x][i].nod]=drum[x]; + + if(!uz[a[x][i].nod]) + + q.push(a[x][i].nod),uz[a[x][i].nod]=1; + + } + + } + + } + +} + + + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;++i) + + { + + scanf("%d%d%lf",&x,&y,&z); + + z=log(z); + + a[x].push_back((planeta){y,z}); + + a[y].push_back((planeta){x,z}); + + } + + for(i=2;i<=n;++i) + + d[i]=1000000000; + + dijkstra(1); + + for(i=2;i<=n;++i) + + printf("%d ",drum[i]); + + + + return 0; + +} diff --git a/distmin/djikstra/1ee3593d-02b7-11ec-a58c-2cf05d84bfc2.cpp b/distmin/djikstra/1ee3593d-02b7-11ec-a58c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee6216a5a1e10ff7efd7e16bf068ce3857f9b1f1 --- /dev/null +++ b/distmin/djikstra/1ee3593d-02b7-11ec-a58c-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +struct nod { + + int y; + + double cost; + + inline nod(int _y, double _cost) { + + y=_y; + + cost=_cost; + + } + + inline bool operator < (const nod &c) const { + + return (double)cost>c.cost; + + } + +}; + +priority_queue c; + +vector v[1501]; + +int pos[1501]; + +double d[1501]; + +bitset <1501> p; + +inline double modul(double x) + +{ + + if(x<0) + + return -x; + + return x; + +} + +void dijkstra() + +{ + + int x; + + c.push(nod(1,0)); + + while(c.empty()==0) { + + x=c.top().y; + + c.pop(); + + if(p[x]==0) { + + p[x]=1; + + for(vector :: iterator it=v[x].begin();it!=v[x].end();it++) + + if(it->y!=1) { + + if((double)d[it->y]-d[x]-it->cost>0.0000000001) { + + d[it->y]=(double)d[x]+it->cost; + + pos[it->y]=(pos[x])%104659; + + c.push(nod(it->y,d[it->y])); + + } + + else if(modul(d[it->y]-d[x]-it->cost)<=0.0000000001) { + + pos[it->y]=(pos[it->y]+pos[x])%104659; + + c.push(nod(it->y,d[it->y])); + + } + + } + + } + + } + +} + +int main () + +{ + + int i,n,m,x,y; + + double cost; + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(i=1;i<=m;i++) { + + f>>x>>y>>cost; + + cost=log(cost); + + v[x].push_back(nod(y,cost)); + + v[y].push_back(nod(x,cost)); + + } + + f.close(); + + for(i=1;i<=n;i++) + + d[i]=2147000000; + + d[1]=1; + + pos[1]=1; + + dijkstra(); + + for(i=2;i<=n;i++) + + g< +#include +#include +#include +#include +using namespace std; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +const int N = 1502; + +const double eps = 0.0000001; + +const int M = 104659; + + + +int n, m, sol[N], nod; + +double dist[N]; + +bool ver[N]; + +vector > v[N]; + +queue q; + + + +void dijkstra() { + + int i; + + vector >::iterator it; + + + + for(i = 1; i<=n; ++i) + + dist[i] = 29999999.0; + + + + ver[1] = true; + + q.push(1); + + dist[1] = 0; + + sol[1] = 1; + + + + while(!q.empty()) { + + nod = q.front(); + + q.pop(); + + ver[nod] = false; + + + + for(it = v[nod].begin(); it!=v[nod].end(); ++it) { + + if(dist[it->first] - dist[nod] - it->second > eps) { + + dist[it->first] = dist[nod] + it->second; + + sol[it->first] = sol[nod]; + + + + if(!ver[it->first]) { + + q.push(it->first); + + ver[it->first] = true; + + } + + } + + else if(dist[nod] + it->second - dist[it->first] <= eps) { + + sol[it->first] += sol[nod]; + + sol[it->first]%=M; + + } + + } + + } + +} + + + +int main() { + + int i, x, y; + + double c; + + + + in >> n >> m; + + + + for(i = 1; i<=m; ++i) { + + in >> x >> y >> c; + + c = log(c); + + + + v[x].push_back(pair(y, c)); + + v[y].push_back(pair(x, c)); + + } + + + + dijkstra(); + + + + for(i = 2; i<=n; ++i) + + out << sol[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/2075fd51-02b7-11ec-8ba1-2cf05d84bfc2.cpp b/distmin/djikstra/2075fd51-02b7-11ec-8ba1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f8379ebf8ee73f080fceb6daf85bde8bf3c1af6 --- /dev/null +++ b/distmin/djikstra/2075fd51-02b7-11ec-8ba1-2cf05d84bfc2.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int MOD = 104659; + +const double INF = 160000000; + +vector< pair > G[1502]; + +int N , M , drumuri[1502]; + +double D[1502]; + + + +void djikstra() + +{ + + for(int i=1;i<=N;++i) D[i] = INF; + + D[1] = 0; + + drumuri[1] = 1; + + set > h; + + pair v; + + h.insert(make_pair(0,1)); + + while(!h.empty()) + + { + + v = (*h.begin()) , h.erase(*h.begin()); + + for(vector >::const_iterator w = G[v.second].begin();w!=G[v.second].end();++w) + + if(fabs(D[w->first] - v.first - w->second) < 1e-9) + + drumuri[w->first]= (drumuri[w->first] + drumuri[v.second])%MOD; + + else + + if(D[w->first] > v.first + w->second) + + { + + D[w->first] = v.first + w->second; + + drumuri[w->first] = drumuri[v.second]; + + h.insert(make_pair(D[w->first],w->first)); + + } + + } + +} + + + +void read_data() + +{ + + int x , y; + + double c; + + for(fin>>N>>M;M;M--) + + { + + fin>>x>>y>>c; + + G[x].push_back(make_pair(y,log(c))); + + G[y].push_back(make_pair(x,log(c))); + + } + + + +} + + + +int main() + +{ + + read_data(); + + djikstra(); + + for(int i=2;i<=N;++i) + + fout< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin ("dmin.in"); + +ofstream fout ("dmin.out"); + + + +const int maxn = 15005; + +const int mod = 104659; + +const double eps = 1e-8; + +const double oo = 1e40; + +vector > G[maxn]; + +double Dist[maxn]; + +int Cnt[maxn]; + + + +void Dijkstra(int start, int n) { + + vector < pair > :: iterator it; + + priority_queue > H; + + bitset Vis = 0; + + int node, i; + + for (i = 1; i <= n; i++) { + + Dist[i] = oo; + + } + + Dist[start] = 0; + + Cnt[start] = 1; + + H.push(make_pair(0, start)); + + while (!H.empty()) { + + node = H.top().second; + + H.pop(); + + if (Vis[node] == true) { + + continue; + + } + + Vis[node] = true; + + for (it = G[node].begin(); it != G[node].end(); it++) { + + if (Dist[it->first] - eps > Dist[node] + it->second) { + + Dist[it->first] = Dist[node] + it->second; + + Cnt[it->first] = Cnt[node]; + + H.push(make_pair(-Dist[it->first], it->first)); + + } else if (Dist[it->first] + eps > Dist[node] + it->second) { + + Cnt[it->first] += Cnt[node]; + + if (Cnt[it->first] >= mod) { + + Cnt[it->first] -= mod; + + } + + } + + } + + } + +} + + + +int main() { + + ios_base :: sync_with_stdio (false); + + int n, m, x, y, c, i; + + fin >> n >> m; + + for (i = 1; i <= m; i++) { + + fin >> x >> y >> c; + + G[x].push_back(make_pair(y, log(c))); + + G[y].push_back(make_pair(x, log(c))); + + } + + Dijkstra(1, n); + + for (i = 2; i <= n; i++) { + + fout << Cnt[i] << " "; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/distmin/djikstra/212aa130-02b7-11ec-b51c-2cf05d84bfc2.cpp b/distmin/djikstra/212aa130-02b7-11ec-b51c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af29e8838714f53fe4eec686fb12e74717a65327 --- /dev/null +++ b/distmin/djikstra/212aa130-02b7-11ec-b51c-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +typedef vector >::iterator it; + + + +vector > gf[1501]; + +int rez[1501], n, m; + +double d[1501]; + +set > h; + + + +void dijkstra(){ + + for (int i=1; i<=n; i++) d[i] = 987654321; + + rez[1]=1; + + for(h.insert(make_pair(0,1)); h.size(); h.erase(h.begin())){ + + double min = h.begin()->first; + + int nod = h.begin()->second; + + for(it i=gf[nod].begin(); i!=gf[nod].end(); i++){ + + int vecin = i->first; + + double cost = i->second; + + if ( fabs(d[vecin] - min - cost ) < 1e-9 ) rez[vecin] = (rez[vecin] + rez[nod]) % 104659; + + else if(d[vecin] > min + cost){ + + d[vecin] = min + cost; + + rez[vecin] = rez[nod]; + + h.insert(make_pair(d[vecin], vecin)); + + } + + } + + } + +} + + + +int main(){ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(int i=1; i<=m; i++){ + + int first, second, c; + + f>>first>>second>>c; + + gf[first].push_back(make_pair(second,log(c))); + + gf[second].push_back(make_pair(first,log(c))); + + } + + + + dijkstra(); + + for(int i=2; i<=n; i++) g< +#include +#include +using namespace std; + + + +vector < pair > G[1505]; + +int N, NHeap, Heap[1505], Poz[1505], NPaths[1505]; + +double D[1505]; + + + +void Read () + +{ + + freopen ("dmin.in", "r", stdin); + + int M; + + scanf ("%d %d", &N, &M); + + for (; M>0; --M) + + { + + int X, Y; + + double C; + + scanf ("%d %d %lf", &X, &Y, &C); + + C=log(C); + + G[X].push_back (make_pair (Y, C)); + + G[Y].push_back (make_pair (X, C)); + + } + +} + + + +void Print () + +{ + + freopen ("dmin.out", "w", stdout); + + for (int i=2; i<=N; ++i) + + { + + printf ("%d ", NPaths[i]); + + } + + printf ("\n"); + +} + + + +inline double Abs (double a) + +{ + + if (a>0) + + { + + return a; + + } + + return -a; + +} + + + +inline void Swap (int X, int Y) + +{ + + int Aux; + + Aux=Poz[Heap[X]]; + + Poz[Heap[X]]=Poz[Heap[Y]]; + + Poz[Heap[Y]]=Aux; + + Aux=Heap[X]; + + Heap[X]=Heap[Y]; + + Heap[Y]=Aux; + +} + + + +void Percolate (int X) + +{ + + int F=X>>1; + + if (F>0 and D[Heap[X]]0) + + { + + int X=Heap[1]; + + Delete (1); + + for (int i=0; i +#include +#include +#include +#include +using namespace std; + + + +typedef vector >::iterator it; + + + +int rez[1501], n, m; + +double d[1501]; + +vector > gf[1501]; + +set > heap; + + + +void dijkstra(){ + + for(int i=0;i<=n;i++) d[i]=987654321; + + rez[1]=1; + + for(heap.insert(make_pair(0,1)); heap.size(); heap.erase(heap.begin())){ + + double min = (heap.begin())->first; + + int nod = (heap.begin())->second; + + for(it i=gf[nod].begin(); i!=gf[nod].end(); i++){ + + int vecin = i->first; + + double cost = i->second; + + if (fabs(d[i->first]-min-i->second)<1e-9)rez[i->first]= (rez[i->first] + rez[nod]) %104659; + + else if (min + i->secondfirst]){ + + d[i->first] = min + i->second; + + rez[i->first] = rez[nod]; + + heap.insert(make_pair(d[i->first], i->first)); + + } + + } + + } + +} + + + +int main(){ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(int i=1;i<=m;++i){ + + int first,second,c; + + f>>first>>second>>c; + + double lg=log((double)c); + + gf[first].push_back(make_pair(second,lg)); + + gf[second].push_back(make_pair(first,lg)); + + } + + + + dijkstra(); + + for(int i=2; i<=n; i++) + + g< +# include +# include +# include +using namespace std; + + + +vector a[1501]; + +vector b[1501]; + +long long n, m, i, j, x, y, sol[1501], ap[1501]; + +double cost, dm[1501]; + +set < pair > st; + + + +double modul (double a) + +{ + + if (a < 0) return a * (-1.0); + + else return a; + +} + + + +double rest (double a) + +{ + + while (a >= 104659.0) a -= 104659.0; + + return a; + +} + +void dijkstra () + +{ + + st.insert (make_pair (1, 1)); + + sol[1] = 1; + + dm[1] = 0; + + for (long long i = 2; i <= n; ++i) dm[i] = 2000000000; + + for (; st.size () > 0; ) + + { + + double cost = (*st.begin ()).first; + + int nod = (*st.begin ()).second; + + ap[nod] = 0; + + vector :: iterator it1; + + vector :: iterator it2; + + st.erase ((*st.begin ())); + + for (it1 = a[nod].begin (), it2 = b[nod].begin (); it1 != a[nod].end (); ++it1, ++it2) + + if (dm[*it1] - cost - (*it2) > 0.000001) + + { + + dm[*it1] = rest (cost + (*it2)), sol[*it1] = sol[nod]; + + if (!ap[*it1]) + + { + + ap[*it1] = 1; + + st.insert (make_pair (dm[*it1], (*it1))); + + } + + } + + else + + if (modul (dm[*it1] - cost - (*it2)) < 0.000001) + + sol[*it1] = rest (sol[*it1] + sol[nod]); + + } + +} + +int main () + +{ + + ifstream f ("dmin.in"); + + ofstream g ("dmin.out"); + + + + f >> n >> m; + + for (i = 1; i <= m; ++i) + + { + + f >> x >> y >> cost; + + cost = log (cost); + + a[x].push_back (y); + + a[y].push_back (x); + + b[x].push_back (cost); + + b[y].push_back (cost); + + } + + + + dijkstra (); + + for (i = 2; i <= n; ++i) + + g << sol[i] << ' '; + + g << '\n'; + + g.close (); + + return 0; + +} diff --git a/distmin/djikstra/21ae9abf-02b7-11ec-8e01-2cf05d84bfc2.cpp b/distmin/djikstra/21ae9abf-02b7-11ec-8e01-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a53afa8ed69fe7c4b6b5f136c56b4aa23af3172 --- /dev/null +++ b/distmin/djikstra/21ae9abf-02b7-11ec-8e01-2cf05d84bfc2.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef pair per; + +typedef vector::iterator it; + + + +int n,m,rez[1505]; + +double d[1505]; + +bitset<1505> viz; + +vector gr[1505]; + +set > s; + + + +void dij() { + + for(int i=1; i<=n; ++i) d[i]=2000000000; + + rez[1]=1; + + for(s.insert(make_pair(0,1));s.size();s.erase(s.begin())) { + + double cst=s.begin()->first; int nod=s.begin()->second; + + for(it i=gr[nod].begin(); i!=gr[nod].end(); ++i) { + + if(fabs(d[i->first]-cst-i->second)<1e-9) rez[i->first]=(rez[i->first]+rez[nod])%104659; + + else if(cst+i->secondfirst]) { + + d[i->first]=cst+i->second; + + rez[i->first]=rez[nod]; + + s.insert(make_pair(d[i->first],i->first)); + + } + + } + + } + +} + + + +int main() + +{ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(int i=1; i<=m; ++i) { + + int a,b,c; + + f>>a>>b>>c; + + double lg=log((double)c); + + gr[a].push_back(make_pair(b,lg)); + + gr[b].push_back(make_pair(a,lg)); + + } + + dij(); + + for(int i=2; i<=n; ++i) g< +#include +#include + + +using namespace std; +FILE*f=fopen("dmin.in","r"); + +FILE*g=fopen("dmin.out","w"); + + + +int n,m,i,j,x,y,cst,H[1505],Poz[1505],Nr[1505],nod,nodvcn,L,Viz[1505],nodbst; + +vector< pair >G[1505]; double lg,D[1505],cstvcn,bst; + + + +inline void citire () { + + + + fscanf(f,"%d %d",&n,&m); + + + + for ( i = 1 ; i <= m ; ++i ){ + + fscanf(f,"%d %d %d",&x,&y,&cst); + + lg = log(cst); + + G[x].push_back(make_pair(y,lg)); + + G[y].push_back(make_pair(x,lg)); + + } + +} + + + +inline void swap ( int &a, int &b ){ + + int aux = a; + + a = b; + + b = aux; + +} + + + +inline double aabs ( double j ){ + + if ( j < 0 ) + + return -j; + + return j; + +} + + + + + +inline void urca ( int poz ){ + + + + while ( poz && D[H[poz>>1]] > D[H[poz]] ){ + + swap(H[poz>>1],H[poz]); + + swap(Poz[H[poz>>1]],Poz[H[poz]]); + + poz = poz >> 1; + + } + +} + + + +inline void coboara ( int x ){ + + int y = (1<<29); + + + + while ( x != y ){ + + y = x; + + if ( (y<<1) <= L && D[H[y<<1]] < D[H[x]] ) x = y << 1; + + if ( (y<<1) + 1 <= L && D[H[(y<<1)+1]] < D[H[x]] ) x = (y<<1) + 1; + + swap(H[x],H[y]); + + swap(Poz[H[x]],Poz[H[y]]); + + } + +} + + + + + +inline void insert_heap( int nod ){ + + H[++L] = nod; + + Poz[nod] = L; + + urca(L); + +} + + + +inline int delete_first( ){ + + int nod = H[1]; + + swap(H[1],H[L--]); + + coboara(1); + + Poz[nod] = 0; + + return nod; + +} + + + + + +inline void dijkstra () { + + + + for ( Nr[1] = 1, i = 2 ; i <= n ; ++i ){ + + D[i] = (1<<29); + + } + for ( i = 1 ; i <= n ; ++i ){ + + bst = (1<<29); + + for ( j = 1 ; j <= n ; ++j ){ + + if ( !Viz[j] && D[j] < bst ) + + nodbst = j,bst = D[j]; + + } + + Viz[nodbst] = 1; nod = nodbst; + + for ( j = 0 ; j < G[nodbst].size() ; ++j ){ + + nodvcn = G[nod][j].first; cstvcn = G[nod][j].second; + + + + if ( aabs(D[nod] + cstvcn - D[nodvcn] ) < 1e-10 ){ + + Nr[nodvcn] += Nr[nod]; + + if ( Nr[nodvcn] >= 104659 ) + + Nr[nodvcn] -= 104659; + + } + + else{ + + if ( D[nod] + cstvcn - D[nodvcn] < 1e-10 ){ + + D[nodvcn] = D[nod] + cstvcn; + + Nr[nodvcn] = Nr[nod]; + + } + + } + + + + } + + } + + + + for ( i = 2 ; i <= n ; ++i ){ + + fprintf(g,"%d ",Nr[i]); + + } + +} + + + + + +int main () { + + + + citire(); + + + + dijkstra(); + + + + fclose(f); + + fclose(g); + + + + return 0; + +} diff --git a/distmin/djikstra/220908cc-02b7-11ec-94b4-2cf05d84bfc2.cpp b/distmin/djikstra/220908cc-02b7-11ec-94b4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8422de2855c69b58975d78eb7e3d9adc889bd43 --- /dev/null +++ b/distmin/djikstra/220908cc-02b7-11ec-94b4-2cf05d84bfc2.cpp @@ -0,0 +1,234 @@ +#include +#include +#include + + +using namespace std; +FILE*f=fopen("dmin.in","r"); + +FILE*g=fopen("dmin.out","w"); + + + +int n,m,i,j,x,y,cst,H[1505],Poz[1505],Nr[1505],nod,nodvcn,L,Viz[1505],nodbst; + +vector< pair >G[1505]; double lg,D[1505],cstvcn,bst; + + + +inline void citire () { + + + + fscanf(f,"%d %d",&n,&m); + + + + for ( i = 1 ; i <= m ; ++i ){ + + fscanf(f,"%d %d %d",&x,&y,&cst); + + lg = log(cst); + + G[x].push_back(make_pair(y,lg)); + + G[y].push_back(make_pair(x,lg)); + + } + +} + + + +inline void swap ( int &a, int &b ){ + + int aux = a; + + a = b; + + b = aux; + +} + + + +inline double aabs ( double j ){ + + if ( j < 0 ) + + return -j; + + return j; + +} + + + + + +inline void urca ( int poz ){ + + + + while ( poz && D[H[poz>>1]] > D[H[poz]] ){ + + swap(H[poz>>1],H[poz]); + + swap(Poz[H[poz>>1]],Poz[H[poz]]); + + poz = poz >> 1; + + } + +} + + + +inline void coboara ( int x ){ + + int y = (1<<29); + + + + while ( x != y ){ + + y = x; + + if ( (y<<1) <= L && D[H[y<<1]] < D[H[x]] ) x = y << 1; + + if ( (y<<1) + 1 <= L && D[H[(y<<1)+1]] < D[H[x]] ) x = (y<<1) + 1; + + swap(H[x],H[y]); + + swap(Poz[H[x]],Poz[H[y]]); + + } + +} + + + + + +inline void insert_heap( int nod ){ + + H[++L] = nod; + + Poz[nod] = L; + + urca(L); + +} + + + +inline int delete_first( ){ + + int nod = H[1]; + + swap(H[1],H[L--]); + + coboara(1); + + Poz[nod] = 0; + + return nod; + +} + + + + + +inline void dijkstra () { + + + + for ( Nr[1] = 1, i = 2 ; i <= n ; ++i ){ + + D[i] = (1<<29); + + } + for ( i = 1 ; i <= n ; ++i ){ + + bst = (1<<29); + + for ( j = 1 ; j <= n ; ++j ){ + + if ( !Viz[j] && D[j] < bst ) + + nodbst = j,bst = D[j]; + + } + + Viz[nodbst] = 1; nod = nodbst; + + for ( j = 0 ; j < G[nodbst].size() ; ++j ){ + + nodvcn = G[nod][j].first; cstvcn = G[nod][j].second; + + + + if ( aabs(D[nod] + cstvcn - D[nodvcn] ) < 1e-10 ){ + + Nr[nodvcn] += Nr[nod]; + + if ( Nr[nodvcn] >= 104659 ) + + Nr[nodvcn] -= 104659; + + } + + else{ + + if ( D[nod] + cstvcn - D[nodvcn] < 1e-10 ){ + + D[nodvcn] = D[nod] + cstvcn; + + Nr[nodvcn] = Nr[nod]; + + } + + } + + + + } + + } + + + + for ( i = 2 ; i <= n ; ++i ){ + + fprintf(g,"%d ",Nr[i]); + + } + +} + + + + + +int main () { + + + + citire(); + + + + dijkstra(); + + + + fclose(f); + + fclose(g); + + + + return 0; + +} diff --git a/distmin/djikstra/22549756-02b7-11ec-ad51-2cf05d84bfc2.cpp b/distmin/djikstra/22549756-02b7-11ec-ad51-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e99ae00f06f8dcb395a2c312ebe233f633e890e --- /dev/null +++ b/distmin/djikstra/22549756-02b7-11ec-ad51-2cf05d84bfc2.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + +using namespace std; +ifstream fi("dmin.in"); +ofstream fo("dmin.out"); +int n,m,a,b,c,nr[15001],k; + +double d[15001]; +char viz[15001]; +vector > v[15001]; +int comp (double a,double b){ + + if((a-b<0.0000001&&a-b>-0.0000001)||(b-a<0.0000001&&b-a>-0.0000001)) + + return 1; + + return 0; + +} +void dijkstra(int x){ + double min=2000000000; + for(int i=1;i<=n;++i) + if(d[i]=104659) + + nr[v[k][i].first]-=104659; + + } + + else + + if(d[v[k][i].first]>d[k]+v[k][i].second){ + d[v[k][i].first]=d[k]+v[k][i].second; + + nr[v[k][i].first]=nr[k]; + + } + + + dijkstra(k); + } +} +int main() { + fi>>n>>m; + for(int i=1;i<=m;++i){ + fi>>a>>b>>c; + + double lg=log(c); + v[a].push_back(make_pair(b,lg)); + + v[b].push_back(make_pair(a,lg)); + } + nr[1]=1; + for(int i=2;i<=n;++i) + d[i]=2000000000; + + dijkstra(1); + for(int i=2;i<=n;++i) + + fo< +#include +#include + +using namespace std; + + + + + + +FILE*f=fopen("dmin.in","r"); +FILE*g=fopen("dmin.out","w"); + +int n,m,i,j,x,y,cst,H[1505],Poz[1505],Nr[1505],nod,nodvcn,L,Viz[1505],nodbst; +vector< pair >G[1505]; double lg,D[1505],cstvcn,bst; + +inline void citire () { + + fscanf(f,"%d %d",&n,&m); + + for ( i = 1 ; i <= m ; ++i ){ + fscanf(f,"%d %d %d",&x,&y,&cst); + lg = log(cst); + G[x].push_back(make_pair(y,lg)); + G[y].push_back(make_pair(x,lg)); + } +} + +inline void swap ( int &a, int &b ){ + int aux = a; + a = b; + b = aux; +} + +inline double aabs ( double j ){ + if ( j < 0 ) + return -j; + return j; +} + + +inline void urca ( int poz ){ + + while ( poz && D[H[poz>>1]] > D[H[poz]] ){ + swap(H[poz>>1],H[poz]); + swap(Poz[H[poz>>1]],Poz[H[poz]]); + poz = poz >> 1; + } +} + +inline void coboara ( int x ){ + int y = (1<<29); + + while ( x != y ){ + y = x; + if ( (y<<1) <= L && D[H[y<<1]] < D[H[x]] ) x = y << 1; + if ( (y<<1) + 1 <= L && D[H[(y<<1)+1]] < D[H[x]] ) x = (y<<1) + 1; + swap(H[x],H[y]); + swap(Poz[H[x]],Poz[H[y]]); + } +} + + +inline void insert_heap( int nod ){ + H[++L] = nod; + Poz[nod] = L; + urca(L); +} + +inline int delete_first( ){ + int nod = H[1]; + swap(H[1],H[L]); swap(Poz[H[1]],Poz[H[L]]); + + --L; + coboara(1); + Poz[nod] = 0; + return nod; +} + + +inline void dijkstra () { + + for ( Nr[1] = 1, i = 2 ; i <= n ; ++i ){ + D[i] = (1<<29); + } + + insert_heap(1); + + while ( L ){ + nod = delete_first(); + + for ( i = 0 ; i < G[nod].size() ; ++i ){ + nodvcn = G[nod][i].first; cstvcn = G[nod][i].second; + + if ( aabs(D[nod] + cstvcn - D[nodvcn] ) < 1e-10 ){ + Nr[nodvcn] += Nr[nod]; + if ( Nr[nodvcn] >= 104659 ) + Nr[nodvcn] -= 104659; + } + else{ + if ( D[nod] + cstvcn - D[nodvcn] < 1e-10 ){ + D[nodvcn] = D[nod] + cstvcn; + if ( Poz[nodvcn] ) + urca(Poz[nodvcn]); + else + insert_heap(nodvcn); + Nr[nodvcn] = Nr[nod]; + } + } + } + + } + + for ( i = 2 ; i <= n ; ++i ){ + fprintf(g,"%d ",Nr[i]); + } +} + + +int main () { + + citire(); + + dijkstra(); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/distmin/djikstra/2345e6b7-02b7-11ec-bf0a-2cf05d84bfc2.cpp b/distmin/djikstra/2345e6b7-02b7-11ec-bf0a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..829920c86b03282657331c834fe638dad90d4eb2 --- /dev/null +++ b/distmin/djikstra/2345e6b7-02b7-11ec-bf0a-2cf05d84bfc2.cpp @@ -0,0 +1,187 @@ +#include +#include +using namespace std; + + + +typedef long long int lli; + +int const maxn=1500,maxm=2500,mod=104659; + +double const eps=1e-8,inf=30*maxn; + +int N,M,E3[maxm][3],H[maxn],J[maxn],hs,I[maxn+1],Ji[maxn],C[maxn]; + +struct Edge{int to;double w;} A[2*maxm]; + +double D[maxn]; + + + +void pop_heap() + +{ int t,r,c; + + --hs;t=H[0];H[0]=H[hs];J[H[0]]=0;H[hs]=t; + + r=0;c=(2*(r+1))-1; + + while(cD[H[c+1]])) + + {++c;} + + if(D[H[r]]>D[H[c]]) + + { t=H[r];H[r]=H[c];J[H[r]]=r;H[c]=t;J[H[c]]=c; + + r=c;c=(2*(r+1))-1; + + } + + else{c=hs;} + + } + +} + + + +void decrease_key(int i,double v) + +{ int r,c,t; + + D[i]=v;c=J[i]; + + while(0x;++x){D[x]=inf;} + + hs=0;C[0]=1;update(0,0,0.0); + + while(0i;++i) + + { scanf("%d %d %d",&E3[i][0],&E3[i][1],&E3[i][2]); + + --E3[i][0];--E3[i][1]; + + } + + for(i=0;N>i;++i){I[i]=0;} + + for(i=0;M>i;++i) + + { x=E3[i][0];y=E3[i][1]; + + ++I[x];++I[y]; + + } + + x=I[0];I[0]=0; + + for(i=1;N>=i;++i) + + {y=I[i];I[i]=I[i-1]+x;x=y;} + + for(i=0;N>i;++i){Ji[i]=I[i];} + + for(i=0;M>i;++i) + + { x=E3[i][0];y=E3[i][1];w=log(E3[i][2]); + + A[Ji[x]].to=y;A[Ji[x]++].w=w; + + A[Ji[y]].to=x;A[Ji[y]++].w=w; + + } + + if(1i;++i) + + {printf(" %d",C[i]);} + + } + + return 0; + +} diff --git a/distmin/djikstra/23881b6b-02b7-11ec-aa24-2cf05d84bfc2.cpp b/distmin/djikstra/23881b6b-02b7-11ec-aa24-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b264e1745c982a13b5e1793a90e060bcc0b9f42 --- /dev/null +++ b/distmin/djikstra/23881b6b-02b7-11ec-aa24-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +using namespace std; +int n,m,nrsol[2005]; + +double dist[2005]; + +vector v[2005]; + +vector cost[2005]; + + + +int comp(double a,double b) + +{ + + if(a-0.0000001>b) return 1; + + if(b-0.0000001>a) return -1; + + return 0; + +} + + + +class cmp + +{ + +public: + + bool operator()(const int& a,const int& b) + + { + + if(!comp(dist[a],dist[b])) + + return a myset; + + + + + +void dijkstra() + +{ + + int i,j,p,lim,vec,r; + + + + for(i=1;i<=n;i++) + + dist[i]=200000000; + + dist[1]=0; + + nrsol[1]=1; + + for(i=1;i<=n;i++) + + myset.insert(i); + + for(i=1;i<=n;i++) + + { + + if(myset.size()==0) + + break; + + p=*myset.begin(); + + myset.erase(myset.begin()); + + lim=v[p].size(); + + for(j=0;j=104659) + + nrsol[vec]-=104659; + + } + + } + + } + +} + + + +int main () + +{ + + int i,a,b,c; + + double val; + + + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++) + + { + + scanf("%d%d%d",&a,&b,&c); + + v[a].push_back(b); + + v[b].push_back(a); + + val=log(c); + + cost[a].push_back(val); + + cost[b].push_back(val); + + } + + dijkstra(); + + for(i=2;i<=n;i++) + + printf("%d ",nrsol[i]); + + printf("\n"); + + return 0; + +} diff --git a/distmin/djikstra/243f72ff-02b7-11ec-98a7-2cf05d84bfc2.cpp b/distmin/djikstra/243f72ff-02b7-11ec-98a7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c747045ac106ea83bfa51ad53ecfaafda529d460 --- /dev/null +++ b/distmin/djikstra/243f72ff-02b7-11ec-98a7-2cf05d84bfc2.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +const double eps = 0.00001; + +const int mod = 104659; + +const int maxn = 1505; + +using namespace std; + + + +int i , j , n , m , a , b, c, nr[maxn]; + +bool in[maxn]; + +vector > G[maxn]; + +double d[maxn]; + + + +struct cmp { + + bool operator () ( const int & a , const int & b ) { + + return ( d[a] > d[b]); + + } + +}; + +priority_queue < int , vector , cmp > Q; + + + +void Dijkstra() + +{ + + int i; + + for( i = 2 ; i <= n ; ++i ) + + d[i] = 10000000.00; + + + + Q.push(1) , in[1] = 1, nr[1] = 1; + + + + while( !Q.empty() ) { + + int act = Q.top(); + + Q.pop() ; + + in[act] = false; + + for( i = 0 ; i < G[act].size() ; ++i ) { + + if ( d[act] + G[act][i].second < d[G[act][i].first] ) { + + d[G[act][i].first] = d[act] + G[act][i].second; + + if ( !in[G[act][i].first] ) Q.push(G[act][i].first) , in[G[act][i].first] = true; + + } + + if ((d[G[act][i].first] + G[act][i].second - d[act]) <= eps) + + nr[act] = (nr[act] + nr[G[act][i].first]) % mod; + + } + + } + +} + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + for( i = 1 ; i <= m ; ++i ) { + + scanf("%d %d %d",&a,&b,&c); + + G[a].push_back(make_pair(b,log((double)c))); + + G[b].push_back(make_pair(a,log((double)c))); + + } + + + + Dijkstra(); + + for( i = 2 ; i <= n ; ++i ) + + printf("%d ",nr[i]); + + + +return 0; + +} diff --git a/distmin/djikstra/247f9750-02b7-11ec-a4d8-2cf05d84bfc2.cpp b/distmin/djikstra/247f9750-02b7-11ec-a4d8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e1d16a0adaae36cef7d71cc0b8e4a0b0f9995e7 --- /dev/null +++ b/distmin/djikstra/247f9750-02b7-11ec-a4d8-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +using namespace std; +vector > g[1510]; + +set > t; + +int n, m, p[1510]; + +double d[1510]; + + + +void dijkstra() + +{ + + int i, x; + + double val; + + for (i=1; i<=n; i++) d[i]=1<<30; + + t.insert(make_pair(0,1)); + + p[1]=1; + + while (t.size()) + + { + + val=(*t.begin()).first; + + x=(*t.begin()).second; + + t.erase(*t.begin()); + + for (i=0; i +#include +#include +using namespace std; + + + +vector< pair< int,int > > v[1502]; + +int H[1502],poz[1502],Nr[1502]; + +double D[1502]; + +int n,m,dh; + + + +inline double Abs(double x){ return x>0 ? x:-x; } + +inline int egal(double x,double first){ + + return Abs(x-first) < 0.000001; + +} + +inline int mai_mare(double x,double first){ + + return( x-first>0.000001 ); + +} + + + +inline void swap(int i,int j){ + + int aux=H[i]; H[i]=H[j]; H[j]=aux; + + poz[H[i]]=i; + + poz[H[j]]=j; + +} + +inline void Up(int k){ + + while( k/2 && mai_mare(D[H[k/2]], D[H[k]]) ){ + + swap(k,k/2); + + k=k/2; + + } + +} + +inline void Down(int k){ + + int min=0; + + while( min != k ){ + + min=k; + + if( 2*min<=dh && mai_mare(D[H[k]],D[H[2*min]]) ) k=2*min; + + if( 2*min+1<=dh && mai_mare(D[H[k]],D[H[2*min+1]])) k=2*min+1; + + swap(min,k); + + } + +} + + + +void Dijkstra(){ + + vector< pair< int,int > >:: iterator it; + + int i,pmin; + + + + for(i=1;i<=n;++i) H[i]=poz[i]=i, D[i]=2147000000*1.00; + + D[1]=0.00; Nr[1]=1; + + dh=n; + + + + while( dh ){ + + pmin=H[1]; + + swap(1,dh); + + dh--; + + Down(1); + + + + for(it=v[pmin].begin(); it!=v[pmin].end(); ++it) + + if( mai_mare(D[it->first], D[pmin] + log(it->second)) ){ + + D[it->first] = D[pmin] + log(it->second); + + Nr[it->first] = Nr[pmin]; + + Up(poz[it->first]); + + }else + + if( egal(D[it->first],D[pmin] + log(it->second)) ) + + Nr[it->first]=( Nr[it->first] + Nr[pmin]) % 104659; + + } + +} + + + +int main(){ + + int i,x,first,z; + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;++i){ + + scanf("%d%d%d",&x,&first,&z); + + v[x].push_back(make_pair(first,z)); + + v[first].push_back(make_pair(x,z)); + + } + + + + Dijkstra(); + + + + printf("%d",Nr[2]); + + for(i=3;i<=n;++i) printf(" %d",Nr[i]); + + printf("\n"); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/distmin/djikstra/2507525a-02b7-11ec-8f13-2cf05d84bfc2.cpp b/distmin/djikstra/2507525a-02b7-11ec-8f13-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01e8a480db14729d80f5708a81e1a79cfff41cc0 --- /dev/null +++ b/distmin/djikstra/2507525a-02b7-11ec-8f13-2cf05d84bfc2.cpp @@ -0,0 +1,193 @@ +#include +#include #include +#include +#include +using namespace std; + + + + + +typedef pair p; + + + +int n,m,i; + +int x,y,z; + +double zLg; + + + +vector

g[1505]; + +vector

::iterator it; + +bool ut[1505]; + +double cost[1505]; + +int nr[1505]; + + + +struct cmp + +{ + + bool operator () (const int &i,const int &j) const + + { + + return (cost[i]-cost[j])>1e-10; + + } + +}; + +priority_queue ,cmp> Q; + + + +double modul(double x) + +{ + + if(x<0) + + return -x; + + return x; + +} + +void Dijkstra() + +{ + + for(i=1;i<=1505;i++) + + cost[i]=2000000000.5; + + cost[1]=0; + + Q.push(1); + + + + while(!Q.empty()) + + { + + int nod=Q.top(); + + Q.pop(); + + ut[nod]=0; + + + + for(it=g[nod].begin();it!=g[nod].end();it++) + + { + + if((cost[it->first]-cost[nod]-it->second)<1e-10) + + continue; + + + + cost[it->first]=cost[nod]+it->second; + + + + if(!ut[it->first]) + + { + + ut[it->first]=1; + + Q.push(it->first); + + } + + } + + } + +} + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&z); + + zLg=log(z); + + g[x].push_back(p(y,zLg)); + + g[y].push_back(p(x,zLg)); + + } + + + + Dijkstra(); + + + + nr[1]=1; + + for(i=1;i<=n;i++) + + Q.push(i); + + + + while(!Q.empty()) + + { + + int nod=Q.top(); + + Q.pop(); + + for(it=g[nod].begin();it!=g[nod].end();it++) + + { + + if(modul(cost[it->first]-cost[nod]-it->second)<1e-10) + + nr[it->first]=(nr[it->first]+nr[nod])%104659; + + } + + } + + + + for(i=2;i<=n;i++) + + printf("%d ",nr[i]); + + + + return 0; + +} diff --git a/distmin/djikstra/25c31288-02b7-11ec-8fd4-2cf05d84bfc2.cpp b/distmin/djikstra/25c31288-02b7-11ec-8fd4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e92ada692cfdc375338cce7ddd9457296830d7c8 --- /dev/null +++ b/distmin/djikstra/25c31288-02b7-11ec-8fd4-2cf05d84bfc2.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + + + + +using namespace std; +const int INF = (1<<30)-5; +const double eps = 1e-10; +struct nod +{ + int x; + double cost; + nod *next; +}; +nod *G[50005]; + +int n, m, uz[50005], v[50005]; +double d[50005]; + set< pair > S; + +inline double mabs(double x) +{ + return x<0?-x:x; +} + + +void addMuchie(int i, int j, int cost) +{ + nod *p = new nod; + p->x = j; + p->cost = log(cost); + p->next = G[i]; + G[i] = p; + +} + +int minimul() +{ + int min = INF, ibun = -1; + for (int i = 1; i <= n; ++i) + if (!v[i] && d[i] < min) + min = d[i], ibun = i; + + return ibun; +} + +void dijkstra() +{ + + + + v[1] = 1; + int i, gata = 0; + + + for (nod *p = G[1]; p; p = p -> next) + d[p->x]=(double)p->cost, uz[p->x]=1; + d[1] = 0.0; + uz[1] = 1; + for (int k = 1; k < n; ++k) + { + gata= 1; + i = minimul(); + + + v[i] = 1; + + for (nod *p = G[i]; p; p = p -> next) + if (!v[p->x]) + { + if (d[p->x] > d[i] + p->cost + eps) + d[p->x] = d[i] + p->cost,uz[p->x] = uz[i], gata = 0; + else + if (mabs(d[p->x] - (d[i] + p->cost)) <= eps) + uz[p->x] = (uz[i] + uz[p->x]) % 104659, uz[p->x] %= 104659, gata = 0; + else; + + } + + } + +} + +int main() +{ + FILE *f = fopen("dmin.in", "r"); + fscanf(f, "%d%d", &n, &m); + for (int i = 1; i <= n; ++i) + d[i] = INF, uz[i] = 0; + for (int x, y, c; m; --m) + { + fscanf(f, "%d%d%d", &x, &y, &c); + addMuchie(x, y, c); + addMuchie(y, x, c); + + } + fclose(f); + + dijkstra(); + f = fopen("dmin.out", "w"); + + for (int i = 2; i <= n; ++i) + fprintf(f, "%d ", uz[i]); + for (int i = 1; i <= n; ++i) + printf( "%d ", (int)exp(d[i])); + fclose(f); + + return 0; +} diff --git a/distmin/djikstra/25f1f92d-02b7-11ec-883f-2cf05d84bfc2.cpp b/distmin/djikstra/25f1f92d-02b7-11ec-883f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dffb701f757a47a0689898c1df9aec820dc8f4a --- /dev/null +++ b/distmin/djikstra/25f1f92d-02b7-11ec-883f-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +# include +# include +# include + + +using namespace std; +struct nod { + int capat; + double cost; + nod *next;}; +int n, nrd[15003], v[15003]; +double d[15003]; +nod *g[15003]; + +void add (int i, int j, int c) +{ + nod *p=new nod; + p->capat=j; + p->cost=log((double)c); + p->next=g[i]; + g[i]=p; +} + +void read () +{ + ifstream fin ("dmin.in"); + int m; + fin>>n>>m; + for (int i=1;i<=n;i++) + d[i]=1000000000; + int i, j, c; + for (;m;--m) + { + fin>>i>>j>>c; + add(i, j, c); + add(j, i, c); + } +} + +int min () +{ + int i=1, m; + while (v[i])++i; + m=i; + for (++i;i<=n;i++) + if (v[i]==0 && d[i]next) + if(d[p->capat]>d[dmin]+p->cost+0.0000000001) + { + d[p->capat]=d[dmin]+p->cost; + nrd[p->capat]=nrd[dmin]; + } + else + if (p->cost+d[dmin]-d[p->capat]<0.0000000001 && p->cost+d[dmin]-d[p->capat]>-0.0000000001) + nrd[p->capat]=(nrd[dmin]+nrd[p->capat]) % 104659; + } +} + +void afis () +{ + ofstream fout ("dmin.out"); + for (int i=2;i<=n;i++) + fout< +#include +#include + + + + +using namespace std; +const int INF = (1<<30)-5; +const double eps = 1e-10; +struct nod +{ + int x; + double cost; + nod *next; +}; +nod *G[50005]; + +int n, m, uz[50005], v[50005]; +double d[50005]; + set< pair > S; + +inline double mabs(double x) +{ + return x<0?-x:x; +} + + +void addMuchie(int i, int j, int cost) +{ + nod *p = new nod; + p->x = j; + p->cost = log(cost); + p->next = G[i]; + G[i] = p; + +} + +int minimul() +{ + int min = INF, ibun = -1; + for (int i = 1; i <= n; ++i) + if (!v[i] && d[i] < min) + min = d[i], ibun = i; + + return ibun; +} + +void dijkstra() +{ + + + + v[1] = 1; + int i; + + + for (nod *p = G[1]; p; p = p -> next) + d[p->x]=(double)p->cost, uz[p->x]=1; + d[1] = 0.0; + uz[1] = 1; + for (int k = 1; k < n; ++k) + { + + i = minimul(); + if (i == -1) break; + v[i] = 1; + + for (nod *p = G[i]; p; p = p -> next) + if (!v[p->x]) + { + if (d[p->x] > d[i] + p->cost + eps) + d[p->x] = d[i] + p->cost,uz[p->x] = uz[i]; + else + if (mabs(d[p->x] - (d[i] + p->cost)) <= eps) + uz[p->x] = (uz[i] + uz[p->x]) % 104659, uz[p->x] %= 104659; + else; + + } + + } + +} + +int main() +{ + FILE *f = fopen("dmin.in", "r"); + fscanf(f, "%d%d", &n, &m); + for (int i = 1; i <= n; ++i) + d[i] = INF, uz[i] = 0; + for (int x, y, c; m; --m) + { + fscanf(f, "%d%d%d", &x, &y, &c); + addMuchie(x, y, c); + addMuchie(y, x, c); + + } + fclose(f); + + dijkstra(); + f = fopen("dmin.out", "w"); + + for (int i = 2; i <= n; ++i) + fprintf(f, "%d ", uz[i]); + + fclose(f); + + return 0; +} diff --git a/distmin/djikstra/2620fcfc-02b7-11ec-b569-2cf05d84bfc2.cpp b/distmin/djikstra/2620fcfc-02b7-11ec-b569-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c939a9debf6708ad5575cb84885fbe8634181bef --- /dev/null +++ b/distmin/djikstra/2620fcfc-02b7-11ec-b569-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +# include +# include +# include + + +using namespace std; +struct nod { + int capat; + double cost; + nod *next;}; +int n, nrd[15003], v[15003]; +double d[15003]; +nod *g[15003]; + +void add (int i, int j, int c) +{ + nod *p=new nod; + p->capat=j; + p->cost=log10((double)c); + p->next=g[i]; + g[i]=p; +} + +void read () +{ + ifstream fin ("dmin.in"); + int m; + fin>>n>>m; + for (int i=1;i<=n;i++) + d[i]=1000000000; + int i, j, c; + for (;m;--m) + { + fin>>i>>j>>c; + add(i, j, c); + add(j, i, c); + } +} + +int min () +{ + int i=1, m; + while (v[i])++i; + m=i; + for (++i;i<=n;i++) + if (v[i]==0 && d[i]next) + if(d[p->capat]>d[dmin]+p->cost+0.0000000001) + { + d[p->capat]=d[dmin]+p->cost; + nrd[p->capat]=nrd[dmin]; + } + else + if (p->cost+d[dmin]-d[p->capat]<0.0000000001 && p->cost+d[dmin]-d[p->capat]>-0.0000000001) + nrd[p->capat]=(nrd[dmin]+nrd[p->capat]) % 104659; + } +} + +void afis () +{ + ofstream fout ("dmin.out"); + for (int i=2;i<=n;i++) + fout< +#include +#include +#include +#include + + + + + +using namespace std; + +typedef pair di; + +int n, m, np [1515]; +vector g [1515]; +vector d (1515, (double)INT_MAX); +priority_queue , greater > q; + +void scan () +{ + int i, a, b, c; + double k; + scanf ("%d%d", &n, &m); + for (i=1; i <= m; ++i) + { + scanf ("%d%d%d", &a, &b, &c); + k=(double)log10 (c); + g [a].push_back (di (k, b)); + g [b].push_back (di (k, a)); + } +} + +void dijkstra () +{ + vector :: iterator it; + int k; + double da; + q.push (di (0, 1)); + d [1]=0; + np [1]=1; + while (!q.empty ()) + { + k=q.top ().second; + da=q.top ().first; + q.pop (); + if (da > d [k]) continue; + for (it=g [k].begin (); it != g [k].end (); ++it) + { + if (d [it->second] > d [k] + (it->first)) + { + d [it->second] = d [k] + (it->first); + q.push (di (d [it->second], it->second)); + } + } + } +} + +inline double make_abs (double x) +{ + if (x < 0) + return -x; + return x; +} + +void nrpos (int k) +{ + vector :: iterator it; + for (it=g [k].begin (); it != g [k].end (); ++it) + if (make_abs (d [k] - d [it->second] - (it->first)) < 0.00000001) + { + if (np [it->second] == 0) + nrpos (it->second); + np [k] += np [it->second]; + np [k] %= 104659; + } +} + +void print () +{ + for (int i=2; i <= n; ++i) + + printf ("%d ", np [i]); + + printf ("\n"); +} + +int main () +{ + freopen ("dmin.in", "r", stdin); + freopen ("dmin.out", "w", stdout); + scan (); + dijkstra (); + for (int i=2; i <= n; ++i) + if (np [i] == 0) + nrpos (i); + print (); + return 0; +} diff --git a/distmin/djikstra/2706b155-02b7-11ec-bf40-2cf05d84bfc2.cpp b/distmin/djikstra/2706b155-02b7-11ec-bf40-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b25d3d563d1d23c0a73e31118544932fa4de588 --- /dev/null +++ b/distmin/djikstra/2706b155-02b7-11ec-bf40-2cf05d84bfc2.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +using namespace std; + +typedef pair graf; +vector G[1510]; + +int NR[1510], H[1510]; + +bool in[1510]; + +double D[1510]; + +int n, m, nr; + +double make_abs(double a){ + + if(a > 0) return a; + + return -a; + +} + +int egal(double a, double b){ + + if(make_abs(a-b) <= 1e-8) + + return 1; + + return 0; + +} + +void down_heap(int k){ + + int son = 1; + + while(son){ + + son = 0; + + if(2*k <= nr) son = 2*k; + + if(2*k+1 <= nr && D[H[2*k+1]] < D[H[2*k]]) son ++; + + if(son && D[H[son]] < D[H[k]]){ + + swap(H[son], H[k]); + + k = son; + + } + + else son = 0; + + } + +} + +void up_heap(int k){ + + while(k > 1 && D[H[k]] < D[H[k/2]]){ + + swap(H[k], H[k/2]); + + k /= 2; + + } + +} + +void push_heap(int x){ + + H[++nr] = x; + + in[x] = true; + + up_heap(nr); + +} + +int pop_heap(){ + + int x = H[1]; + + swap(H[1], H[nr]); + + nr--; + + down_heap(1); + + in[x] = false; + + return x; + +} + +int mic(double a, double b){ + + if(b-a > 1e-8) + + return 1; + + return 0; + +} + +void dij(){ + + push_heap(1); + + NR[1] = 1; + + int x, nod; + + double dist; + + while(nr){ + + x = pop_heap(); + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i){ + + nod = i -> first; + + dist = i -> second; + + if(mic(D[x] + dist , D[nod])){ + + D[nod] = D[x] + dist; + + NR[nod] = NR[x]; + + if(!in[nod]) push_heap(nod); + + } + + else if(egal(D[x] + dist , D[nod])){ + + NR[nod] = (NR[nod] + NR[x])%104659; + + if(!in[nod]) push_heap(nod); + + } + + } + + + + + + } + + for(int i = 2; i < n; ++i) + + printf("%d ", NR[i]); + + printf("%d\n", NR[n]); + +} + +int main(){ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int x, y, z; + + double r; + + scanf("%d%d%d", &x, &y, &z); + + r = log10(z); + + G[x].push_back(graf(y,r)); + + G[y].push_back(graf(x,r)); + + } + + for(int i = 2; i <= n; ++i) + + D[i] = 2000000000; + + dij(); + + return 0; + +} diff --git a/distmin/djikstra/2753d03e-02b7-11ec-b7f5-2cf05d84bfc2.cpp b/distmin/djikstra/2753d03e-02b7-11ec-b7f5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63401664a036ceae1c27814313f01b3899b94e42 --- /dev/null +++ b/distmin/djikstra/2753d03e-02b7-11ec-b7f5-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include + + +using namespace std; +int a, b, n, m, i, j, nod, fiu; + +int viz[1510], Nr[1510]; + +long double Cst, cst[1510], c, Min; + +vector < pair > V[1510]; + + + +int egal (double a, double b) { + + if ( a - b >= -0.00000000001 && a - b <= 0.00000000001 ) return 0; + + if ( a - b < -0.00000000001) return -1; + + if ( a - b > 0.00000000001) return 1; + + return 0; + +} + + + +int main () { + + + + FILE *f = fopen ("dmin.in", "r"); + + FILE *g = fopen ("dmin.out", "w"); + + + + fscanf (f, "%d %d", &n, &m); + + for (i = 1; i <= m; i++) { + + fscanf (f, "%d %d %Lf", &a, &b, &c); + + c = log(c); + + V[a].push_back (make_pair (b, c)); + + V[b].push_back (make_pair (a, c)); + + } + + + + for (i = 2; i <= n; i++) + + cst[i] = 1 << 30; + + + + cst[1] = 0; Nr[1] = 1; + + for (i = 1; i <= n; i++) { + + Min = 1 << 30; + + for (j = 1; j <= n; j++) { + + if ( egal(Min, cst[j]) == 1 && !viz[j]) { + + Min = cst[j]; + + nod = j; + + } + + } + + + + viz[nod] = 1; + + for (j = 0; j < (int)V[nod].size(); j++) { + + fiu = V[nod][j].first; + + Cst = V[nod][j].second + cst[nod]; + + if ( !egal (cst[fiu], Cst) ) { + + Nr[fiu]+= Nr[nod]; + + if (Nr[fiu] > 104659) Nr[fiu]-= 104659; + + } + + + + if ( egal(cst[fiu], Cst) == 1 ) { + + cst[fiu] = Cst; + + Nr[fiu] = Nr[nod]; + + if (Nr[fiu] > 104659) Nr[fiu]-= 104659; + + } + + } + + } + + + + for (i = 2; i <= n; i++) + + fprintf (g, "%d ", Nr[i]); + + + + fclose (f); + + fclose (g); + + + + return 0; + + + +} diff --git a/distmin/djikstra/276a503b-02b7-11ec-a075-2cf05d84bfc2.cpp b/distmin/djikstra/276a503b-02b7-11ec-a075-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fe1fa3b4ae6ece4eccfb8478d6dbd87118384cb --- /dev/null +++ b/distmin/djikstra/276a503b-02b7-11ec-a075-2cf05d84bfc2.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +using namespace std; + +typedef pair graf; +vector G[1510]; + +int NR[1510], H[1510], SOL[1510]; + +bool in[1510]; + +double D[1510]; + +int n, m, nr; + +double make_abs(double a){ + + if(a > 0) return a; + + return -a; + +} + +int egal(double a, double b){ + + if(make_abs(a-b) <= 1e-8) + + return 1; + + return 0; + +} + +void down_heap(int k){ + + int son = 1; + + while(son){ + + son = 0; + + if(2*k <= nr) son = 2*k; + + if(2*k+1 <= nr && D[H[2*k+1]] < D[H[2*k]]) son ++; + + if(son && D[H[son]] < D[H[k]]){ + + swap(H[son], H[k]); + + k = son; + + } + + else son = 0; + + } + +} + +void up_heap(int k){ + + while(k > 1 && D[H[k]] < D[H[k/2]]){ + + swap(H[k], H[k/2]); + + k /= 2; + + } + +} + +void push_heap(int x){ + + H[++nr] = x; + + in[x] = true; + + up_heap(nr); + +} + +int pop_heap(){ + + int x = H[1]; + + swap(H[1], H[nr]); + + nr--; + + down_heap(1); + + in[x] = false; + + return x; + +} + +int mic(double a, double b){ + + if(b-a > 1e-8) + + return 1; + + return 0; + +} + +void dij(){ + + push_heap(1); + + NR[1] = 1; + + int x, nod; + + double dist; + + while(nr){ + + x = pop_heap(); + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i){ + + nod = i -> first; + + dist = i -> second; + + if(mic(D[x] + dist , D[nod])){ + + D[nod] = D[x] + dist; + + NR[nod] = NR[x]; + + if(!in[nod]) push_heap(nod); + + } + + else if(egal(D[x] + dist , D[nod])){ + + NR[nod] = (NR[nod] + NR[x])%104659; + + if(!in[nod]) push_heap(nod); + + } + + } + + SOL[x] = NR[x]; + + NR[x] = 0; + + } + + for(int i = 2; i < n; ++i) + + printf("%d ", SOL[i]); + + printf("%d\n", SOL[n]); + +} + +int main(){ + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int x, y, z; + + double r; + + scanf("%d%d%d", &x, &y, &z); + + r = log10(z); + + G[x].push_back(graf(y,r)); + + G[y].push_back(graf(x,r)); + + } + + for(int i = 2; i <= n; ++i) + + D[i] = 2000000000; + + dij(); + + return 0; + +} diff --git a/distmin/djikstra/277f2d7e-02b7-11ec-bd51-2cf05d84bfc2.cpp b/distmin/djikstra/277f2d7e-02b7-11ec-bd51-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5ce2accff7fb49d6cdc521f861a055f53f08133 --- /dev/null +++ b/distmin/djikstra/277f2d7e-02b7-11ec-bd51-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +const double inf=2147483647; +const double eps=1e-10; +int n,m,X; +vector< pair > a[1510]; +double d[1510]; +int h[1510],nh,inheap[1510]; +int nr[1510]; +inline void citire() +{ + scanf("%d%d",&n,&m); + X=1; + int x,y,c; + double c1; + for(int i=0; i>1; +} +inline void upheap(int k) +{ + int key=h[k]; + while(k>1 && cmp(d[key],d[h[father(k)]])==-1) + { + h[k]=h[father(k)]; + inheap[h[k]]=k; + k=father(k); + } + h[k]=key; + inheap[h[k]]=k; +} +inline void downheap(int k) +{ + int son; + do + { + son=0; + if(left_son(k)<=nh) + { + son=left_son(k); + if(right_son(k)<=nh && cmp(d[h[right_son(k)]],d[h[son]])==-1) + son=right_son(k); + if(cmp(d[h[son]],d[h[k]])!=-1) + son=0; + } + if(son) + { + swap(h[k],h[son]); + inheap[h[k]]=k; + inheap[h[son]]=son; + k=son; + } + }while(son); +} +inline void dijkstra() +{ + for(int i=1; i<=n; ++i) + d[i]=inf; + nh=1; + nr[X]=1; + h[1]=X; + inheap[X]=1; + d[X]=0; + int next; + double cost; + char aux; + while(nh>0) + { + int now=h[1]; + inheap[now]=0; + h[1]=h[nh--]; + if(nh>0) + { + inheap[h[1]]=1; + downheap(1); + } + for(size_t i=0,lim=a[now].size(); i=104659) + nr[next]-=104659; + } + } + } +} +int main() +{ + freopen("dmin.in","r",stdin); + freopen("dmin.out","w",stdout); + citire(); + dijkstra(); + for(int i=2; i +#include +#include +#include + + +using namespace std; +int a, b, n, m, i, j, nod, fiu; + +int viz[1510], Nr[1510]; + +long double Cst, cst[1510], c, Min; + +vector < pair > V[1510]; + + + +int egal (double a, double b) { + + if ( a - b >= -0.00000000001 && a - b <= 0.00000000001 ) return 0; + + if ( a - b < -0.00000000001) return -1; + + if ( a - b > 0.00000000001) return 1; + + return 0; + +} + + + +int main () { + + + + FILE *f = fopen ("dmin.in", "r"); + + FILE *g = fopen ("dmin.out", "w"); + + + + fscanf (f, "%d %d", &n, &m); + + for (i = 1; i <= m; i++) { + + fscanf (f, "%d %d %Lf", &a, &b, &c); + + c = log(c); + + V[a].push_back (make_pair (b, c)); + + V[b].push_back (make_pair (a, c)); + + } + + + + for (i = 2; i <= n; i++) + + cst[i] = 1 << 30; + + + + cst[1] = 0; Nr[1] = 1; + + for (i = 1; i <= n; i++) { + + Min = 1 << 30; + + for (j = 1; j <= n; j++) { + + if ( egal(Min, cst[j]) == 1 && !viz[j]) { + + Min = cst[j]; + + nod = j; + + } + + } + + + + viz[nod] = 1; + + for (j = 0; j < (int)V[nod].size(); j++) { + + fiu = V[nod][j].first; + + Cst = V[nod][j].second + cst[nod]; + + if ( !egal (cst[fiu], Cst) ) { + + Nr[fiu]+= Nr[nod]; + + if (Nr[fiu] > 104659) Nr[fiu]-= 104659; + + } + + + + if ( egal(cst[fiu], Cst) == 1 ) { + + cst[fiu] = Cst; + + Nr[fiu] = Nr[nod]; + + if (Nr[fiu] > 104659) Nr[fiu]-= 104659; + + } + + } + + } + + + + for (i = 2; i <= n; i++) + + fprintf (g, "%d ", Nr[i]); + + + + fclose (f); + + fclose (g); + + + + return 0; + + + +} diff --git a/distmin/djikstra/27f4b120-02b7-11ec-af6b-2cf05d84bfc2.cpp b/distmin/djikstra/27f4b120-02b7-11ec-af6b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e34ffce44a863d9427c4540bc20e2c69fd314b5 --- /dev/null +++ b/distmin/djikstra/27f4b120-02b7-11ec-af6b-2cf05d84bfc2.cpp @@ -0,0 +1,280 @@ +#include +#include +#include +#include +using namespace std; +int n,m; + +int nh,h[1505],inheap[1505]; + +int cate[1505]; + +double d[1505]; + +const double eps=0.0000000001; + +const double inf=2147483647; + +vector< pair > a[1505]; + +inline int left_son(int x) + +{ + + return x<<1; + +} + +inline int right_son(int x) + +{ + + return (x<<1)+1; + +} + +inline int father(int x) + +{ + + return x>>1; + +} + +inline double modul(double x) + +{ + + if(x<0) + + return -x; + + return x; + +} + +inline char compar(double x,double y) + +{ + + if(modul(x-y)<=eps) + + return 0; + + double dif=x-y; + + if(dif>0) + + return 1; + + return -1; + +} + +void upheap(int k) + +{ + + int key=h[k]; + + while(k>1 && compar(d[key],d[h[father(k)]])==-1) + + { + + h[k]=h[father(k)]; + + inheap[h[k]]=k; + + k=father(k); + + } + + h[k]=key; + + inheap[h[k]]=k; + +} + +void downheap(int k) + +{ + + int son; + + do + + { + + son=0; + + if(left_son(k)<=nh) + + { + + son=left_son(k); + + if(right_son(k)<=nh && compar(d[h[right_son(k)]],d[h[son]])==-1) + + son=right_son(k); + + if(compar(d[h[son]],d[h[k]])!=-1) + + son=0; + + } + + if(son) + + { + + swap(h[k],h[son]); + + inheap[h[k]]=k; + + inheap[h[son]]=son; + + k=son; + + } + + }while(son); + +} + +inline void citire() + +{ + + scanf("%d%d",&n,&m); + + int x,y,z; + + double aux; + + for(int i=0; i +#include +using namespace std; +int N, M; + +int H[1504]; + +int poz[1504]; + +double d[1504]; + +int nr[1504]; + +int L; + +struct Nod + +{ + + Nod *urm; + + int vf; + + double cst; + +}; + +Nod *G[1504]; + +inline void insert(int x, int y, double cst) + +{ + + Nod *q = new Nod; + + q->vf = y; q->cst = cst; + + q->urm = G[x]; + + G[x] = q; + +} + +void read() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&N,&M); + + int i,x,y,cost; + + double c; + + for(i=1;i<=M;++i) + + { + + scanf("%d%d%d",&x,&y,&cost); + + c = log2(cost); + + insert(x,y,c); insert(y,x,c); + + } + +} + +inline void swap(int t, int f) + +{ + + int aux; + + aux = poz[H[t]]; poz[H[t]] = poz[H[f]]; poz[H[f]] = aux; + + aux = H[t]; H[t] = H[f]; H[f] = aux; + +} + +inline int cmp(double x, double y) + +{ + + if(x+1e-8 < y) return 1; + + if(y+1e-8 < x) return -1; + + return 0; + +} + +inline void upheap(int f) + +{ + + int t = f / 2; + + while(t && cmp(d[H[t]] , d[H[f]])==-1) + + { + + swap(t,f); + + f = f / 2; t = t / 2; + + } + +} + +inline void downheap(int t) + +{ + + int f; + + do + + { + + f = 0; + + if(2*t <= L) f = 2*t; + + if(2*t+1<=L && cmp(d[H[2*t+1]] , d[H[2*t]])==1 ) f = 2*t+1; + + if(f && cmp(d[H[f]] , d[H[t]])<=0) f = 0; + + if(f) + + { + + swap(t,f); + + t = f; + + } + + } + + while(f); + +} + +void preparing() + +{ + + int i; + + for(i=1;i<=N;++i) + + { + + H[++L] = i; + + poz[i] = L; + + d[i] = 2140000000; + + } + + d[1] = log2(1); nr[1] = 1; + +} + +void dijkstra() + +{ + + int x,y; + + Nod *q; + + while(L && d[H[1]] != 2140000000) + + { + + x = H[1]; + + H[1] = H[L]; + + poz[H[L]] = 1; poz[x] = -1; + + --L; + + downheap(1); + + for(q = G[x];q;q=q->urm) + + { + + y = q->vf; + + if(cmp(d[y] , d[x] + q->cst)==-1) + + { + + nr[y] = nr[x]; + + d[y] = d[x] + q->cst; + + upheap(poz[y]); + + } + + else if(!cmp(d[y], d[x] + q->cst)) + + { + + nr[y] += nr[x]; + + if(nr[y] >= 104659) nr[y] -= 104659; + + } + + } + + } + +} + +void print() + +{ + + int i; + + for(i=2;i<=N;++i) printf("%d ",nr[i]); + + printf("\n"); + +} + +int main() + +{ + + read(); + + preparing(); + + dijkstra(); + + print(); + + return 0; + +} diff --git a/distmin/djikstra/287fd472-02b7-11ec-b7e9-2cf05d84bfc2.cpp b/distmin/djikstra/287fd472-02b7-11ec-b7e9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9a7f2c1caba9b7de25af280fa4eacabb961fe2c --- /dev/null +++ b/distmin/djikstra/287fd472-02b7-11ec-b7e9-2cf05d84bfc2.cpp @@ -0,0 +1,433 @@ +# include +# include +# include +typedef struct {long int n;long int v[2000 +1];long int index[2000 +1],roads[2000 +1];} HEAP; + +typedef long double TIPD; + +typedef struct NOD {long int nod; TIPD dist; NOD* next;} NOD; + +typedef struct LISTA {NOD *begin,*end;}; + +LISTA ladiac[2000 +1]; + +TIPD raza[2000 +1]; + +long int n,m; + + + +TIPD modul(TIPD a) {if (a<0) return -a; return a;} + +long int mai_mare(TIPD a, TIPD b) + + { + + if (b==-1) return 0; + + if (a==-1) return 1; + + if (a-b>1e-11) return 1; + + return 0; + + } + +long int mai_mic(TIPD a, TIPD b) + + { + + if (a==-1) return 0; + + if (b==-1) return 1; + + if (a-b<-1e-11) return 1; + + return 0; + + } + +long int egal(TIPD a, TIPD b) + + { + + if (a==-1 || b==-1) return 0; + + if (modul((a-b))<=1e-11) return 1; + + return 0; + + } + + + +void test(HEAP &a); + + + +void rise_heap(HEAP &a, long int i) + +{ + + long int auxf; + + while (i/2 && mai_mic(raza[a.v[i]],raza[a.v[i/2]])) + + { + + auxf=a.v[i];a.v[i]=a.v[i/2];a.v[i/2]=auxf; + + a.index[a.v[i]]=i; + + a.index[a.v[i/2]]=i/2; + + i/=2; + + } + +} + + + +void submerge_heap(HEAP &a, long int i) + +{ + + long int min=0,auxi;long int auxf; + + while (2*i<=a.n) + + { + + min=i; + + if (2*i <=a.n && mai_mic(raza[a.v[2*i ]],raza[a.v[min]])) min=2*i; + + if (2*i+1<=a.n && mai_mic(raza[a.v[2*i+1]],raza[a.v[min]])) min=2*i+1; + + if (min==i) break; + + auxf=a.v[min];a.v[min]=a.v[i];a.v[i]=auxf; + + a.index[a.v[min]]=min;a.index[a.v[i]]=i; + + i=min; + + } + +} + + + +void insert_heap(HEAP &a, long int nod) + +{ + + a.n++; + + a.v[a.n]=nod; + + + + a.index[nod]=a.n; + + rise_heap(a,a.n); + +} + + + +long int extract_heap(HEAP &a) + +{ + + long int sol; + + sol=a.v[1]; a.index[sol]=0; + + + + a.v[1]=a.v[a.n]; a.v[a.n]=0; a.index[a.v[1]]=1; a.n--; + + + + submerge_heap(a,1); + + + + return sol; + +} + + + +void create_heap(HEAP &a) + +{ + + long int i; + + NOD *p; + + a.n=0; + + raza[1]=0; + + a.roads[1]=1; + + a.index[1]=0; + + for (i=2;i<=n;i++) + + { + + raza[i]=-1; + + a.roads[i]=0; + + } + + p=ladiac[1].begin; + + while (p) + + { + + raza[(*p).nod]=(*p).dist; + + a.roads[(*p).nod]=1; + + p=(*p).next; + + } + + + + for (i=2;i<=n;i++) + + insert_heap(a,i); + + + + + +} + + + +void insert_list(LISTA &a, long int nod, TIPD dist) + +{ + + NOD* p=(NOD*) malloc (sizeof(NOD)); + + (*p).dist=dist; + + (*p).nod=nod; + + (*p).next=NULL; + + + + if (a.begin==NULL) + + { + + a.begin=a.end=p; + + } + + else + + { + + (*(a.end)).next=p; + + a.end=p; + + } + +} + + + +void citire() + +{ + + FILE *f=fopen("dmin.in","r"); + + fscanf(f,"%ld%ld",&n,&m); + + long int i,aa,bb;long int dist; + + for (i=1;i<=m;i++) + + { + + fscanf(f,"%ld%ld%ld",&aa,&bb,&dist); + + insert_list(ladiac[aa],bb,(long double)log10((long double)dist)); + + insert_list(ladiac[bb],aa,(long double)log10((long double)dist)); + + } + + fclose(f); + +} + + + +void scrie(HEAP &a) + +{ + + FILE *g=fopen("dmin.out","w"); + + long int i; + + for (i=2;i<=n;i++) + + { + + if (i>2) fprintf(g," "); + + fprintf(g,"%ld",a.roads[i]); + + } + + fprintf(g,"\n"); + + fclose(g); + +} + + + +long int empty(HEAP &a) + +{ + + if (a.n==0) return 1; + + return 0; + +} + + + +void update(HEAP &a, long int nod) + +{ + + NOD *p; + + p=ladiac[nod].begin; + + while (p) + + { + + + + if (egal(raza[(*p).nod],raza[nod]+(*p).dist)) + + { + + a.roads[(*p).nod]+=a.roads[nod]; + + a.roads[(*p).nod]%=104659; + + } + + else if (mai_mare(raza[(*p).nod],raza[nod]+(*p).dist)) + + { + + raza[(*p).nod]=raza[nod]+(*p).dist; + + a.roads[(*p).nod]=a.roads[nod]; + + a.roads[(*p).nod]%=104659; + + + + if (a.index[(*p).nod]) rise_heap(a,a.index[(*p).nod]); + + } + + + + p=(*p).next; + + } + +} + + + +void dijkstra(HEAP &a) + +{ + long int nod; + + + + while (!empty(a)) + + { + + nod=extract_heap(a); + + + + update(a,nod); + + + + + + } + +} + + + + + +int main() + +{ + + HEAP a; + + citire(); + + create_heap(a); + + + + dijkstra(a); + + scrie(a); + + return 0; + +} + + + +void test(HEAP &a) + +{ + + long int i; + + for (i=1;i<=n;i++) + + printf("%ld === v[i]=%ld\tindex[i]=%ld\troads[i]=%ld\traza[i]=%f\n",i,a.v[i],a.index[i],a.roads[i],raza[i]); + + getchar(); + +} diff --git a/distmin/djikstra/28f4ed8c-8b9a-4d9b-bfd5-a7141668cf9e.cpp b/distmin/djikstra/28f4ed8c-8b9a-4d9b-bfd5-a7141668cf9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dc0065edd26e28b60106f20edc0c3ebbee8519c --- /dev/null +++ b/distmin/djikstra/28f4ed8c-8b9a-4d9b-bfd5-a7141668cf9e.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +int n,m,i,j,drum[1501],x,y; + +double z,d[1501],eps=1e-10; + +struct planeta + +{ + + int nod; + + double cost; + +}; + +vector a[1501]; + +queueq; + +bool uz[1501]; + +void dijkstra(int x) + +{ + + int i; + + q.push(x); + + d[x]=0.00000; + + drum[x]=uz[x]=1; + + while(!q.empty()) + + { + + x=q.front(); + + q.pop(); + + uz[x]=0; + + for(i=0;i104659) + + { + + drum[a[x][i].nod]%=104659; + + if(!uz[a[x][i].nod]) + + q.push(a[x][i].nod),uz[a[x][i].nod]=1; + + } + + } + + if(d[a[x][i].nod]-d[x]-a[x][i].cost>eps) + + { + + d[a[x][i].nod]=d[x]+a[x][i].cost; + + drum[a[x][i].nod]=drum[x]; + + if(!uz[a[x][i].nod]) + + q.push(a[x][i].nod),uz[a[x][i].nod]=1; + + } + + } + + } + +} + + + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;++i) + + { + + scanf("%d%d%lf",&x,&y,&z); + + z=log(z); + + a[x].push_back((planeta){y,z}); + + a[y].push_back((planeta){x,z}); + + } + + for(i=2;i<=n;++i) + + d[i]=1000000000; + + dijkstra(1); + + for(i=2;i<=n;++i) + + printf("%d ",drum[i]); + + + + return 0; + +} diff --git a/distmin/djikstra/29437935-02b7-11ec-9bba-2cf05d84bfc2.cpp b/distmin/djikstra/29437935-02b7-11ec-9bba-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b367c5340b0d93df90bcdbb67e512bd4e506ca53 --- /dev/null +++ b/distmin/djikstra/29437935-02b7-11ec-9bba-2cf05d84bfc2.cpp @@ -0,0 +1,199 @@ +#include +#include + + + + +using namespace std; +typedef struct nod { + + int vf; + + double dist; + + nod* next; + +} *PNOD, NOD; + + + +PNOD L[1501]; + +double lg; + +int m, n, nod_p, k, tur; + +double d[1501], dis, minim; + +int s[1501], nr[1501]; + + + +void Read(); + +void Add(int,int,double); + +void Dijkstra(int); + + + +FILE *fout = fopen( "dmin.out", "w" ); + + + +int main() + +{ + + Read(); + + Dijkstra( nod_p ); + + + + for ( int i = 2; i <= n; ++i ) + + fprintf( fout, "%d ", (nr[i]%104659) ); + + fprintf( fout, "\n" ); + + + + + + + + fclose( fout ); + + return 0; + +} + + + +void Read() + +{ + + FILE *fin = fopen( "dmin.in", "r" ); + + fscanf( fin, "%d%d", &n, &m ); + + int i, j, v1, v2; + + double cost; + + for ( i = 1; i <= m; ++i ) + + { + + fscanf( fin, "%d%d%lf", &v1, &v2, &cost ); + + lg = (double)(log(cost)); + + Add(v1,v2,lg); + + Add(v2,v1,lg); + + d[v1] = d[v2] = 300000001; + + } + + nod_p = 1; + + fclose( fin ); + +} + + + +void Add( int i, int j, double cost ) + +{ + + PNOD p = new NOD; + + p->vf = j; + + p->dist = cost; + + p->next = L[i]; + + L[i] = p; + +} + + + +void Dijkstra( int sursa ) + +{ + + s[sursa] = 0; + + d[sursa] = 0; + + nr[sursa] = 1; + + int i; + for ( tur = 1; tur <= n; ++tur ) + + { + + minim = 20000000.; + + for ( i = 1; i <= n; ++i ) + + { + + if ( !s[i] && d[i] < minim ) + + { + + minim = d[i]; + + k = i; + + } + + } + + + + s[k] = 1; + + for ( PNOD j = L[k]; j; j = j->next ) + + { + + if ( !s[j->vf] ) + + { + + dis = j->dist; + + if ( d[j->vf] - (d[k] + dis ) > 1e-10 ) + + { + + d[j->vf] = d[k] + dis; + + nr[j->vf] = nr[k]%104659; + + } + + else if ( fabs(d[j->vf] - (d[k] + dis)) < 1e-10 ) + + { + + nr[j->vf] += nr[k]; + + } + + } + + } + + } + +} diff --git a/distmin/djikstra/2958727c-02b7-11ec-a5a9-2cf05d84bfc2.cpp b/distmin/djikstra/2958727c-02b7-11ec-a5a9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85fd76676457bdace667d4082f97602e1c70ed0 --- /dev/null +++ b/distmin/djikstra/2958727c-02b7-11ec-a5a9-2cf05d84bfc2.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +using namespace std; + +typedef pair pid; + + + +int N, M; +vector G[1501]; + +double dist[1501]; +int cnt[1501]; +bool gata[1501]; + +void dijkstra() { + for (int i(1); i <= N; ++i) + dist[i] = 1e100; + + dist[1] = 0; + cnt[1] = 1; + + for (int z(0); z < N; ++z) { + int u = -1; + for (int i(1); i <= N; ++i) + if (!gata[i]) + if ((u == -1) || (dist[i] < dist[u])) + u = i; + + for (vector::iterator v = G[u].begin(); v != G[u].end(); ++v) + if (abs(dist[u] - dist[v->first] - v->second) < 1e-8) + cnt[u] = (cnt[u] + cnt[v->first]) % 104659; + + + for (vector::iterator v = G[u].begin(); v != G[u].end(); ++v) + if ((dist[u] + v->second) / dist[v->first] < 1e-8) + dist[v->first] = dist[u] + v->second; + + gata[u] = true; + } +} + +int main(int argc, char *argv[]) { + FILE *fi = fopen("dmin.in", "r"); + fscanf(fi, "%d %d", &N, &M); + int u, v, w; + for (int i(0); i < M; ++i) { + fscanf(fi, "%d %d %d", &u, &v, &w); + G[u].push_back(make_pair(v, log(w))); + G[v].push_back(make_pair(u, log(w))); + } + fclose(fi); + + dijkstra(); + + FILE *fo = fopen("dmin.out", "w"); + for (int i(2); i <= N; ++i) + fprintf(fo, "%d ", cnt[i]); + fprintf(fo, "\n"); + fclose(fo); + + return 0; +} diff --git a/distmin/djikstra/29802f18-02b7-11ec-a375-2cf05d84bfc2.cpp b/distmin/djikstra/29802f18-02b7-11ec-a375-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22b5ea6239b77c221bdef0fa50c4d469af09a5f6 --- /dev/null +++ b/distmin/djikstra/29802f18-02b7-11ec-a375-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +int vec[1515][1515], nvec[1515]; + +double cost[1515][1515]; + +int v[1515], c[1515]; + +double d[1515]; + + + +double modul(double x){ + + if(x<0) return -x; + + return x; + +} + + + +int main(){ + + + + FILE *fin = fopen("dmin.in","r"), + + *fout= fopen("dmin.out","w"); + + + + int N,M; + + fscanf(fin,"%d%d",&N,&M); + + + + for(int i=1;i<=M;i++){ + + int x,y,c; + + fscanf(fin,"%d%d%d",&x,&y,&c); + + vec[x][++nvec[x]] = y; + + cost[x][nvec[x]] = log((double)c); + + + + vec[y][++nvec[y]] = x; + + cost[y][nvec[y]] = cost[x][nvec[x]]; + + } + + + + + + d[1] = 0; + + c[1] = 1; + + + + for(int i=2;i<=N;i++) + + d[i] = 50000.0; + + d[0] = 100000.0; + + + + for(int i=1;i<=N;i++){ + + + + int poz = 0; + + for(int j=1;j<=N;j++) + + if(!v[j] && d[j] < d[poz]) + + poz = j; + + + + + + for(int j=1;j<=nvec[poz];j++) + + if(v[vec[poz][j]] && modul(d[poz] - d[vec[poz][j]] - cost[poz][j]) < 0.000000001){ + + c[poz] += c[vec[poz][j]]; + + c[poz] %= 104659; + + } + + + + v[poz] = 1; + + + + + + for(int j=1;j<=nvec[poz];j++){ + + int V = vec[poz][j]; + + if(d[V] - d[poz] - cost[poz][j] > 0.000000001) + + d[V] = d[poz] + cost[poz][j]; + + } + + + + } + + + + for(int i=2;i<=N;i++) + + fprintf(fout,"%d ",c[i]); + + + + fclose(fout); + + fclose(fin); + + return 0; + +} diff --git a/distmin/djikstra/29c9620f-02b7-11ec-9cff-2cf05d84bfc2.cpp b/distmin/djikstra/29c9620f-02b7-11ec-9cff-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de1250d5b8673de51e178d9f50bc0d6f8e0a5381 --- /dev/null +++ b/distmin/djikstra/29c9620f-02b7-11ec-9cff-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +typedef struct nod + +{ + + int info; + + double c; + + struct nod * next; + +}NOD; + + + +NOD * A[1600]; + +FILE * fin, * fout; + +int X, Y, N, M, i; + +int SEL[1600], NR[1600]; + +double D[1600]; + +double C; + + + +void ADD( int info, double c, NOD * &p ) + +{ + + NOD * q; + + q = new NOD; + + q->info = info; + + q->c = c; + + q->next = p; + + p = q; + +} + +int equal( double a, double b ) + +{ + + if ( fabs( a - b ) < 1e-10 ) return 1; + + else + + return 0; + +} + + + +void DIJKSTRA( int xp ) + +{ + + int i, found, k = 0; + + double min; + + NOD * p; + + memset( SEL, 0, sizeof(SEL) ); + + memset( NR, 0, sizeof(NR) ); + + for( i = 1; i <= N; i++ ) D[i] = 1000000000; + + D[xp] = 0; NR[xp] = 1; + + do + + { + + min = 1000000000; found = 0; + + for( i = 1; i <= N; i++ ) + + if (!SEL[i]) + + if (D[i] < min ) + + { + + min = D[i]; + + k = i; + + found = 1; + + } + + if (found) + + { + + p = A[k]; + + while (p!=NULL) + + { + + if (!SEL[p->info]) + + if ( D[p->info] - (D[k] + p->c) > 1e-10 ) + + { + + D[p->info] = D[k] + p->c; + + NR[p->info] = NR[k]; + + }else + + if (equal( D[k] + p->c, D[p->info])) NR[p->info] = (NR[p->info] + NR[k])% 104659; + + p = p->next; + + } + + SEL[k] = 1; + + } + + }while (found); + +} + + + +int main() + +{ + + fin = fopen( "dmin.in", "r" ); + + fout = fopen( "dmin.out", "w" ); + + fscanf( fin, "%d %d\n", &N, &M ); + + for( i = 1; i <= N; i++ ) A[i] = NULL; + + for( i = 0; i < M; i++ ) + + { + + int T; + + fscanf( fin, "%d %d %d\n", &X, &Y, &T); + + C = T; + + ADD( X, log(C), A[Y] ); + + ADD( Y, log(C), A[X] ); + + } + + + + DIJKSTRA(1); + + for( i = 2; i <= N; i++) fprintf(fout, "%d ", NR[i] ); + + fprintf( fout, "\n" ); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/distmin/djikstra/29df590e-02b7-11ec-84d6-2cf05d84bfc2.cpp b/distmin/djikstra/29df590e-02b7-11ec-84d6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de1250d5b8673de51e178d9f50bc0d6f8e0a5381 --- /dev/null +++ b/distmin/djikstra/29df590e-02b7-11ec-84d6-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +typedef struct nod + +{ + + int info; + + double c; + + struct nod * next; + +}NOD; + + + +NOD * A[1600]; + +FILE * fin, * fout; + +int X, Y, N, M, i; + +int SEL[1600], NR[1600]; + +double D[1600]; + +double C; + + + +void ADD( int info, double c, NOD * &p ) + +{ + + NOD * q; + + q = new NOD; + + q->info = info; + + q->c = c; + + q->next = p; + + p = q; + +} + +int equal( double a, double b ) + +{ + + if ( fabs( a - b ) < 1e-10 ) return 1; + + else + + return 0; + +} + + + +void DIJKSTRA( int xp ) + +{ + + int i, found, k = 0; + + double min; + + NOD * p; + + memset( SEL, 0, sizeof(SEL) ); + + memset( NR, 0, sizeof(NR) ); + + for( i = 1; i <= N; i++ ) D[i] = 1000000000; + + D[xp] = 0; NR[xp] = 1; + + do + + { + + min = 1000000000; found = 0; + + for( i = 1; i <= N; i++ ) + + if (!SEL[i]) + + if (D[i] < min ) + + { + + min = D[i]; + + k = i; + + found = 1; + + } + + if (found) + + { + + p = A[k]; + + while (p!=NULL) + + { + + if (!SEL[p->info]) + + if ( D[p->info] - (D[k] + p->c) > 1e-10 ) + + { + + D[p->info] = D[k] + p->c; + + NR[p->info] = NR[k]; + + }else + + if (equal( D[k] + p->c, D[p->info])) NR[p->info] = (NR[p->info] + NR[k])% 104659; + + p = p->next; + + } + + SEL[k] = 1; + + } + + }while (found); + +} + + + +int main() + +{ + + fin = fopen( "dmin.in", "r" ); + + fout = fopen( "dmin.out", "w" ); + + fscanf( fin, "%d %d\n", &N, &M ); + + for( i = 1; i <= N; i++ ) A[i] = NULL; + + for( i = 0; i < M; i++ ) + + { + + int T; + + fscanf( fin, "%d %d %d\n", &X, &Y, &T); + + C = T; + + ADD( X, log(C), A[Y] ); + + ADD( Y, log(C), A[X] ); + + } + + + + DIJKSTRA(1); + + for( i = 2; i <= N; i++) fprintf(fout, "%d ", NR[i] ); + + fprintf( fout, "\n" ); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/distmin/djikstra/29ef6d51-02b7-11ec-8741-2cf05d84bfc2.cpp b/distmin/djikstra/29ef6d51-02b7-11ec-8741-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2794fbc25f2efe1c2bc155ccdb654280b0768661 --- /dev/null +++ b/distmin/djikstra/29ef6d51-02b7-11ec-8741-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +typedef struct nod + +{ + + int info; + + double c; + + struct nod * next; + +}NOD; + + + +NOD * A[1600]; + +FILE * fin, * fout; + +int X, Y, N, M, i; + +int SEL[1600], NR[1600]; + +double D[1600]; + +double C; + + + +void ADD( int info, double c, NOD * *p ) + +{ + + NOD * q; + + q = new NOD; + + q->info = info; + + q->c = c; + + q->next = *p; + + *p = q; + +} + +int equal( double a, double b ) + +{ + + if ( fabs( a - b ) < 1e-10 ) return 1; + + else + + return 0; + +} + + + +void DIJKSTRA( int xp ) + +{ + + int i, found, k = 0; + + double min; + + NOD * p; + + memset( SEL, 0, sizeof(SEL) ); + + memset( NR, 0, sizeof(NR) ); + + for( i = 1; i <= N; i++ ) D[i] = 1000000000; + + D[xp] = 0; NR[xp] = 1; + + do + + { + + min = 1000000000; found = 0; + + for( i = 1; i <= N; i++ ) + + if (!SEL[i]) + + if (D[i] < min ) + + { + + min = D[i]; + + k = i; + + found = 1; + + } + + if (found) + + { + + p = A[k]; + + while (p!=NULL) + + { + + if (!SEL[p->info]) + + if ( D[p->info] - (D[k] + p->c) > 1e-10 ) + + { + + D[p->info] = D[k] + p->c; + + NR[p->info] = NR[k]; + + }else + + if (equal( D[k] + p->c, D[p->info])) NR[p->info] = (NR[p->info] + NR[k])% 104659; + + p = p->next; + + } + + SEL[k] = 1; + + } + + }while (found); + +} + + + +int main() + +{ + + fin = fopen( "dmin.in", "r" ); + + fout = fopen( "dmin.out", "w" ); + + fscanf( fin, "%d %d\n", &N, &M ); + + for( i = 1; i <= N; i++ ) A[i] = NULL; + + for( i = 0; i < M; i++ ) + + { + + int T; + + fscanf( fin, "%d %d %d\n", &X, &Y, &T); + + C = T; + + ADD( X, log(C), &A[Y] ); + + ADD( Y, log(C), &A[X] ); + + } + + + + DIJKSTRA(1); + + for( i = 2; i <= N; i++) fprintf(fout, "%d ", NR[i] ); + + fprintf( fout, "\n" ); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/distmin/djikstra/2a67e89e-02b7-11ec-9be1-2cf05d84bfc2.cpp b/distmin/djikstra/2a67e89e-02b7-11ec-9be1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f2e79df3141629833c858dd6b5183cb3c199c98 --- /dev/null +++ b/distmin/djikstra/2a67e89e-02b7-11ec-9be1-2cf05d84bfc2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +using namespace std; +typedef pair ii; +typedef pair iii; + +int n,m,i,px1,py1,P[2000]; +double cst,D[2000]; +vector e[2000]; + +inline double absol(double x) { + return x > 0 ? x : -x; +} + +inline int egale(double x,double y) { + return absol(x - y) <= 1e-6; +} + +void dijkstra() { + for(i = 1; i <= n; i++) D[i] = P[i] = (int)1e9; + D[1] = 0; P[1] = 1; + priority_queue < iii,vector,greater > Q; + Q.push(iii(0,1)); + while(!Q.empty()) { + int nod = Q.top().second; + double cs = Q.top().first; + Q.pop(); + if(cs <= D[nod]) { + for(i = 0; i < (int)e[nod].size(); i++) + if(egale(D[e[nod][i].first],D[nod] + e[nod][i].second)) { + P[e[nod][i].first] += P[nod]; + if(P[e[nod][i].first] >= 104659) P[e[nod][i].first] -= 104659; + } + else if(D[e[nod][i].first] > D[nod] + e[nod][i].second) { + D[e[nod][i].first] = D[nod] + e[nod][i].second; + P[e[nod][i].first] = P[nod]; + Q.push(iii(D[e[nod][i].first],e[nod][i].first)); + } + } + } +} + +int main() { + freopen("dmin.in","r",stdin); + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + for(i = 1; i <= m; i++) { + scanf("%d%d%lf",&px1,&py1,&cst); + cst = log(cst); + e[px1].push_back(make_pair(py1,cst)); + e[py1].push_back(make_pair(px1,cst)); + } + + dijkstra(); + + for(i = 2; i <= n; i++) printf("%d ",P[i]); + printf("\n"); + + return 0; +} diff --git a/distmin/djikstra/2ae4ad13-02b7-11ec-9644-2cf05d84bfc2.cpp b/distmin/djikstra/2ae4ad13-02b7-11ec-9644-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33db08d72138abf3ac8af2e044522e4de61e5b4a --- /dev/null +++ b/distmin/djikstra/2ae4ad13-02b7-11ec-9644-2cf05d84bfc2.cpp @@ -0,0 +1,223 @@ +#include +#include using namespace std; +double make_abs(double x) + + { + + if(x>0)return x; + + return -x; + + } + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +typedef struct _lista + + { + + int x; + + double c; + + _lista *urm; + + }*Lista; + +typedef struct _listan + + { + + int x; + + _listan *urm; + + }*Listan; + +typedef struct _nod + + { + + Lista l; + + }Nod; + +Nod V[1501]; + +Listan prim=NULL; + +double c[1501]; + +long mod[1501]; + +int v[1501],coada[1501],pi[1501],n,m,p,u; + +void adauga(int x,int y,int cost) + + { + + Lista nou=new _lista; + + nou->x=y;nou->c=cost; + + nou->urm=V[x].l; + + V[x].l=nou; + + } + +void citire() + + { + + int i,cost,x,y; + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>x>>y>>cost,adauga(x,y,cost),adauga(y,x,cost); + + } + +void getmin(int p,int u) + + { + + int i,aux; + + double min=c[coada[p]]; + + int minpoz=p; + + for(i=p+1;i<=u;i++) + + if(min>c[coada[i]])min=c[coada[i]],minpoz=i; + + aux=coada[p],coada[p]=coada[minpoz],coada[minpoz]=aux; + + } + +void dijkstra(int k) + + { + + Lista i; + + p=u=1;coada[1]=k; + + v[k]=1;c[k]=0; + + while(p<=u) + + { + + getmin(p,u); + + k=coada[p]; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + coada[++u]=i->x,v[i->x]=1,pi[i->x]=k, + + c[i->x]=c[k]+log(i->c); + + + + else + + if(c[i->x]>c[k]+log(i->c)) + + pi[i->x]=k,c[i->x]=c[k]+log(i->c); + + p++; + + } + + } + +void adauga(int x) + + { + + Listan nou=new _listan; + + nou->x=x; + + nou->urm=prim; + + prim=nou; + + } + +void df(int k) + + { + + v[k]=1; + + Lista i; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + if(make_abs(c[k]+log(i->c)-c[i->x])<0.00001) + + df(i->x); + + adauga(k); + + } + +int main() + +{ + +int i; + +Lista j; + +citire();f.close(); + +dijkstra(1); + +for(i=1;i<=n;i++)v[i]=0; + +mod[1]=1; + +df(1); + +Listan ii; + +for(ii=prim;ii;ii=ii->urm) + + { + + + + for(j=V[ii->x].l;j;j=j->urm) + + if(make_abs(c[ii->x]+log(j->c)-c[j->x])<0.00001) + + mod[j->x]=(mod[j->x]+mod[ii->x])%104659; + + } + + + +for(i=2;i<=n;i++) + + g< +#include using namespace std; +double make_abs(double x) + + { + + if(x>0)return x; + + return -x; + + } + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +typedef struct _lista + + { + + int x; + + double c; + + _lista *urm; + + }*Lista; + +typedef struct _listan + + { + + int x; + + _listan *urm; + + }*Listan; + +typedef struct _nod + + { + + Lista l; + + }Nod; + +Nod V[1501]; + +Listan prim=NULL; + +double c[1501]; + +long mod[1501]; + +int v[1501],coada[1501],pi[1501],n,m,p,u; + +void adauga(int x,int y,int cost) + + { + + Lista nou=new _lista; + + nou->x=y;nou->c=cost; + + nou->urm=V[x].l; + + V[x].l=nou; + + } + +void citire() + + { + + int i,cost,x,y; + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>x>>y>>cost,adauga(x,y,cost),adauga(y,x,cost); + + } + +void getmin(int p,int u) + + { + + int i,aux; + + double min=c[coada[p]]; + + int minpoz=p; + + for(i=p+1;i<=u;i++) + + if(min>c[coada[i]])min=c[coada[i]],minpoz=i; + + aux=coada[p],coada[p]=coada[minpoz],coada[minpoz]=aux; + + } + +void dijkstra(int k) + + { + + Lista i; + + p=u=1;coada[1]=k; + + v[k]=1;c[k]=0; + + while(p<=u) + + { + + getmin(p,u); + + k=coada[p]; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + coada[++u]=i->x,v[i->x]=1,pi[i->x]=k, + + c[i->x]=c[k]+log(i->c); + + + + else + + if(c[i->x]>c[k]+log(i->c)) + + pi[i->x]=k,c[i->x]=c[k]+log(i->c); + + p++; + + } + + } + +void adauga(int x) + + { + + Listan nou=new _listan; + + nou->x=x; + + nou->urm=prim; + + prim=nou; + + } + +void df(int k) + + { + + v[k]=1; + + Lista i; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + if(make_abs(c[k]+log(i->c)-c[i->x])<0.00001) + + df(i->x); + + adauga(k); + + } + +int main() + +{ + +int i; + +Lista j; + +citire();f.close(); + +dijkstra(1); + +for(i=1;i<=n;i++)v[i]=0; + +mod[1]=1; + +df(1); + +Listan ii; + +for(ii=prim;ii;ii=ii->urm) + + { + + + + for(j=V[ii->x].l;j;j=j->urm) + + if(make_abs(c[ii->x]+log(j->c)-c[j->x])<0.00001) + + mod[j->x]=(mod[j->x]+mod[ii->x])%104659; + + } + + + +for(i=2;i<=n;i++) + + g< +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +bool Equal (double a, double b) + +{ + + return fabs(a-b)<1e-8; + +} + + + +typedef pair PI; + + + +struct CMP + +{ + + bool operator () (const PI& a, const PI& b) const + + { + + return a.second>b.second; + + } + +}; + + + +priority_queue, CMP> Q; + +int N, M; + +vector G[1510]; + +double Dist[1510]; + +int DP[1510]; + + + +void Read () + +{ + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + int a, b, c; + + f >> a >> b >> c; + + G[a].push_back(make_pair(b, log(1.0*c))); + + G[b].push_back(make_pair(a, log(1.0*c))); + + } + + + + f.close(); + +} + + + +void DoDijkstra () + +{ + + vector::iterator it; + + int i, first; + + double second; + + + + for (i=1; i<=N; i++) + + Dist[i]=999999999; + + + + Dist[1]=0; + + DP[1]=1; + + Q.push(make_pair(1, 0)); + + + + while (!Q.empty()) + + { + + first=Q.top().first; + + second=Q.top().second; + + Q.pop(); + + + + if (!Equal(Dist[first], second)) + + continue; + + + + for (it=G[first].begin(); it!=G[first].end(); ++it) + + if (second+it->secondfirst] || Equal(second+it->second, Dist[it->first])) + + { + + if (second+it->second<=Dist[it->first]-1e-8) + + { + + Dist[it->first]=second+it->second; + + Q.push(make_pair(it->first, Dist[it->first])); + + DP[it->first]=0; + + } + + DP[it->first]+=DP[first]; + + if (DP[it->first]>=104659) + + DP[it->first]-=104659; + + } + + } + +} + + + +void Print () + +{ + + for (int i=2; i<=N; i++) + + g << DP[i] << ' '; + + g << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + DoDijkstra(); + + Print(); + + + + return 0; + +} diff --git a/distmin/djikstra/3822122c-92c6-4728-b2e5-a818f1a07e30.cpp b/distmin/djikstra/3822122c-92c6-4728-b2e5-a818f1a07e30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..454faca1f63b69785a697bcea9341f4e356aaad3 --- /dev/null +++ b/distmin/djikstra/3822122c-92c6-4728-b2e5-a818f1a07e30.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +using namespace std; + +FILE *fin,*fout; + +vector < pair > G[1505]; + +int n,k,apar[1505]; + +double d[1505]; + +priority_queue< pair, vector >, greater > > HEAP; + +bool use[1505]; + +void dijkstra(short nod) + +{ + + short vec; + + double cost; + + for(int i=1;i<=n;i++) + + d[i]=(1<<30); + + d[nod]=0; + + apar[1]=1; + + HEAP.push(make_pair(0,nod)); + + while(!HEAP.empty()) + + { + + nod=HEAP.top().second; + + HEAP.pop(); + + if(use[nod]) + + continue; + + use[nod]=1; + + for(size_t i=0;id[nod]+cost+0.000001) + + { + + d[vec]=d[nod]+cost; + + apar[vec]=apar[nod]; + + HEAP.push(make_pair(d[vec],vec)); + + continue; + + } + + if(abs(d[vec]-d[nod]-cost)<=0.000001) + + { + + apar[vec]+=apar[nod]; + + apar[vec]%=104659; + + } + + } + + } + +} + +void read() + +{ + + fin=fopen("dmin.in","r"); + + int x,y,m; + + double z; + + fscanf(fin,"%d %d",&n,&m); + + while(m--) + + { + + fscanf(fin,"%d %d %lf",&x,&y,&z); + + z=log(z); + + G[x].push_back(make_pair(y,z)); + + G[y].push_back(make_pair(x,z)); + + } + + fclose(fin); + +} + +void print() + +{ + + fout=fopen("dmin.out","w"); + + for(int i=2;i<=n;i++) + + fprintf(fout,"%d ",apar[i]); + + fclose(fout); + +} + +int main() + +{ + + read(); + + dijkstra(1); + + print(); + + return 0; + +} diff --git a/distmin/djikstra/3cafa983-5d3d-491e-8a02-7807a2e05b6e.cpp b/distmin/djikstra/3cafa983-5d3d-491e-8a02-7807a2e05b6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83f14959a69f4c3a9b85220b33d679f39dd6b577 --- /dev/null +++ b/distmin/djikstra/3cafa983-5d3d-491e-8a02-7807a2e05b6e.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int i,j,n,m; + +bool viz[1501]; + +unsigned long x,y,nr[1501]; + +double cost1,d[1501]; + +struct muchie + +{ + + int nod; + + double cost; + +}; + +vector a[1501]; + +void dijkstra(int x) + +{ + + int i; + + queue q; + + q.push(x); + + d[x]=1; + + nr[x]=1; + + while(!q.empty()) + + { + + x=q.front(); + + viz[x]=0; + + for(i=0;i0.0000000001) + + { + + d[a[x][i].nod]=d[x]+a[x][i].cost; + + nr[a[x][i].nod]=nr[x]; + + if(!viz[a[x][i].nod]) + + q.push(a[x][i].nod),viz[a[x][i].nod]=1; + + } + + else + + if(fabs(d[a[x][i].nod]-d[x]-a[x][i].cost)<=0.0000000001) + + { + + nr[a[x][i].nod]=(nr[a[x][i].nod]+nr[x])%104659; + + if(nr[a[x][i].nod]>104659) + + { + + nr[a[x][i].nod]-=104659; + + if(!viz[a[x][i].nod]) + + q.push(a[x][i].nod),viz[a[x][i].nod]=1; + + } + + } + + } + + q.pop(); + + } + +} + +int main() + +{ + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>cost1; + + cost1=log(cost1); + + a[x].push_back((muchie) {y,cost1}); + + a[y].push_back((muchie) {x,cost1}); + + } + + for(i=1;i<=n;++i) + + d[i]=1<<20,nr[i]=0; + + dijkstra(1); + + for(i=2;i<=n;++i) + + g< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int NMAX = 1505, MOD = 104659; + +const double EPS = 1e-8; + + + +int DP[NMAX], N, M, X, Y, C; + +double Dist[NMAX]; + +vector > G[NMAX]; + + + +struct Comp + +{ + + bool operator() (const pair A, const pair &B) const + + { + + return A.second > B.second; + + } + +}; + + + +priority_queue, vector >, Comp> Q; + + + +void Go_Dijkstra() + +{ + + for(int i = 1; i <= N; ++ i) Dist[i] = 999999999; + + + + Dist[1] = 0; + + DP[1] = 1; + + Q.push(make_pair(1, 0.0)); + + + + while(!Q.empty()) + + { + + double Cost = Q.top().second; + + int Node = Q.top().first; + + Q.pop(); + + + + if(Cost > Dist[Node]) continue; + + + + for(vector > :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it) + + { + + if(Cost + it -> second < Dist[it -> first] || fabs(Cost + it -> second - Dist[it -> first]) < EPS) + + { + + if(Cost + it -> second <= Dist[it -> first] - EPS) + + { + + Dist[it -> first] = Cost + it -> second; + + DP[it -> first] = 0; + + Q.push(make_pair(it -> first, Dist[it -> first])); + + } + + DP[it -> first] = (DP[it -> first] + DP[Node]) % MOD; + + } + + } + + } + + + + for(int i = 2; i <= N; ++ i) + + fout << DP[i] << " "; + +} + + + +int main() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; ++ i) + + { + + fin >> X >> Y >> C; + + G[X].push_back(make_pair(Y, log(1.0 * C))); + + G[Y].push_back(make_pair(X, log(1.0 * C))); + + } + + + + Go_Dijkstra(); + + + + return 0; + +} diff --git a/distmin/djikstra/4d18ec5a-6bc3-4bb5-af75-ef2a9f8ab799.cpp b/distmin/djikstra/4d18ec5a-6bc3-4bb5-af75-ef2a9f8ab799.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3571a8d57e8ef3f3fba16e45605b3862430d3b2 --- /dev/null +++ b/distmin/djikstra/4d18ec5a-6bc3-4bb5-af75-ef2a9f8ab799.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > adj[1510]; + +int n,m; + +int best[1510]; + +double err=1e-8,INF=1000000000.0; + +struct cmp { + + bool operator()(pair a,pair b){ + + if(fabs(a.first-b.first)b.first+err; + + } + +}; + +priority_queue ,vector >,greater > > pb; + +double dist[1510]; + + + +void dijktras(int v) + +{ + + int u; + + double cost2,cost1; + + dist[v]=0; + + pb.push(make_pair(0,v)); + + while(!pb.empty()) + + { + + v=pb.top().second; + + cost1=pb.top().first; + + pb.pop(); + + for(vector >::iterator it=adj[v].begin();it!=adj[v].end();it++) + + { + + u=it->second; + + cost2=it->first; + + if(fabs(cost1+cost2-dist[u])<=err){ + + + + + + best[u]+=best[v]; + + best[u]=best[u]%104659; + + } + + else if(dist[u]-err>=cost1+cost2) + + { + + best[u]=best[v]; + + best[u]=best[v]%104659; + + dist[u]=cost2+cost1; + + pb.push(make_pair(dist[u],u)); + + } + + } + + } + + + +} + + + +int main() + +{ + + f >> n >> m; + + for(int i=0;i> x >> y >> c; + + adj[x].push_back(make_pair(log10(c),y)); + + adj[y].push_back(make_pair(log10(c),x)); + + } + + + + for(int i=1;i<=n;i++){ + + dist[i]=INF; + + } + + best[1]=1; + + dijktras(1); + + + + for(int i=2;i<=n;i++) + + g << best[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/5c927231-935b-487b-ad2f-1bde8a1be2b8.cpp b/distmin/djikstra/5c927231-935b-487b-ad2f-1bde8a1be2b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e6297dae47179bcb0bd47b29ca93645b97d27e5 --- /dev/null +++ b/distmin/djikstra/5c927231-935b-487b-ad2f-1bde8a1be2b8.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +bool viz[1505]; + +int n, m, nrdrumuri[1505]; + +double dmin[1505]; + +vector > G[1505]; + + + +priority_queue , greater > q; + + + +void read() + +{ + + ifstream f("dmin.in"); + + f >> n >> m; double c; + + for(int i=0, x, y; i> x >> y >> c; + + G[x].push_back(make_pair(y,log(c))); + + G[y].push_back(make_pair(x,log(c))); + + } + + f.close(); + +} + + + +void dijkstra() + +{ + + int vfmin; + + for(int i=2; i<=n; ++i) dmin[i]=2000000001; + + q.push(1); nrdrumuri[1]=1; + + while(!q.empty()) + + { + + vfmin=q.top(); q.pop(); + + if(viz[vfmin]) continue; + + viz[vfmin]=1; + + for(int i=0; i dmin[vfmin] + G[vfmin][i].second) + + { + + dmin[G[vfmin][i].first] = dmin[vfmin] + G[vfmin][i].second; + + nrdrumuri[G[vfmin][i].first]=nrdrumuri[vfmin]; + + q.push(G[vfmin][i].first); + + } + + else + + if(abs(dmin[G[vfmin][i].first] - dmin[vfmin] - G[vfmin][i].second) <1e-9) + + { + + nrdrumuri[G[vfmin][i].first] = nrdrumuri[G[vfmin][i].first] + nrdrumuri[vfmin]; + + if(nrdrumuri[G[vfmin][i].first] >= 104659) + + nrdrumuri[G[vfmin][i].first] -= 104659; + + } + + } + + } + +} + + + +void out() + +{ + + ofstream g("dmin.out"); + + for(int i=2; i<=n; ++i) + + g << nrdrumuri[i] <<' '; + + g << '\n'; + + g.close(); + +} + + + +int main() + +{ + + read(); + + dijkstra(); + + out(); + + return 0; + +} diff --git a/distmin/djikstra/5d71ef3a-3db3-4df6-9e0e-0ca0b9e07570.cpp b/distmin/djikstra/5d71ef3a-3db3-4df6-9e0e-0ca0b9e07570.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e7f8d46ff4cf301ddfe09f0f2aab76f4e73b37b --- /dev/null +++ b/distmin/djikstra/5d71ef3a-3db3-4df6-9e0e-0ca0b9e07570.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include +using namespace std; + +const int mod=104659; + +const double eps=1e-9; + +set< pair >s; + +vector v[1505]; + +vector c[1505]; + +int modes[1505],a,b,i,n,m,x,node; + +double cost[1505],d,dist,costul; + +void dijkstra() + +{ + + s.insert(make_pair(0,1)); + + while(!s.empty()) + + { + + d=(*s.begin()).first; + + x=(*s.begin()).second; + + s.erase(*s.begin()); + + for(i=0;i=mod) modes[node]-=mod; + + } + + else + + { + + if(d+dist>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>a>>b>>costul; + + v[a].push_back(b); + + c[a].push_back(log(costul)); + + v[b].push_back(a); + + c[b].push_back(log(costul)); + + } + + for(i=1;i<=n;i++) + + cost[i]=20000; + + cost[1]=0;modes[1]=1; + + dijkstra(); + + for(i=2;i<=n;i++) + + {g< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +double d[1505]; + +int n,m,nr[1505]; + + + +bitset <1505> viz; + +vector < pair > v[1505]; + + + +struct CMP{ bool operator ()(int a,int b){ + + return d[a]>d[b];}}; + + + +priority_queue , CMP> q; + + + +int main() + +{ + + fin>>n>>m; + + for(int i=2;i<=n;++i) + + d[i]=0x3f3f3f3f; + + double cost; + + for(int i=1;i<=m;++i){ + + int a,b; + + double c; + + fin>>a>>b>>c; + + cost=log(c); + + v[a].push_back(make_pair(b,cost)); + + v[b].push_back(make_pair(a,cost)); + + } + + q.push(1); + + nr[1]=1; + + for(int i,nod;!q.empty();){ + + i=q.top(); + + q.pop(); + + if(viz[i]) + + continue; + + viz[i]=1; + + for(int j=0;jd[i]+cost){ + + nr[nod]=nr[i]; + + d[nod]=d[i]+cost; + + q.push(nod); + + } + + } + + } + + for(int i=2;i<=n;++i) + + fout< +#include +#include +#include +#include +#include +#include +FILE *f=fopen("dmin.in","r"); + +FILE *g=fopen("dmin.out","w"); + + + +using namespace std; + + + +vector >G[50005]; + +vector::iterator it; + +int n,m; + +double dist[50005]; + +int res[50005]; + +priority_queue,vector >,greater > > HEAP; + +bool used[50005]; + + + +void read( void ) + +{ + + fscanf(f,"%d%d",&n,&m); + + + + for(int i(1); i <=m ; ++i ) + + { + + int a,b; + + double c; + + fscanf(f,"%d%d%lf",&a,&b,&c); + + c=log(c); + + G[a].push_back(make_pair(b,c)); + + G[b].push_back(make_pair(a,c)); + + } + + fclose(f); + + + +} + +void DIJKSTRA( void ) + +{ + + for(int i(1); i<=n ; dist[i++]=0x3f3f3f3f); + + + + dist[1]=0; + + res[1]=1; + + HEAP.push(make_pair(0,1)); + + + + while(!HEAP.empty()) + + { + + int node=HEAP.top().second; + + HEAP.pop(); + + if( used[node]) + + continue; + + used[node]=1; + + + + for(int i(0); i dist[node]+dis + 1e-10) + + { + + dist[newnode]=dist[node]+dis; + + res[newnode]=res[node]; + + HEAP.push(make_pair(dist[newnode],newnode)); + + continue; + + } + + + + if( abs (dist[newnode] - dist[node]-dis ) <= 1e-10 ) + + { + + res[newnode]+=res[node]; + + res[newnode]%=104659; + + continue; + + } + + + + } + + + + } + + + + + +} + +void write( void ) + +{ + + for(int i(2); i <=n; ++i ) + + + + fprintf(g,"%d ",res[i]); + + + + fclose(g); + +} + + + + + +int main( void ) + +{ + + read(); + + DIJKSTRA(); + + write(); + + return 0; + +} diff --git a/distmin/djikstra/62549da8-9b50-4080-9c3d-4542a2a57977.cpp b/distmin/djikstra/62549da8-9b50-4080-9c3d-4542a2a57977.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9248d811c4a4431e31e5645448852e8df173aae8 --- /dev/null +++ b/distmin/djikstra/62549da8-9b50-4080-9c3d-4542a2a57977.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + if((dp[it->first]-1e-8)>=y+it->second) + + { + + dp[it->first]=y+it->second; + + pos[it->first]=pos[x]; + + q.push({it->first,dp[it->first]}); + + } + + else if(fabs(it->second+y-dp[it->first])<=1e-8) + + { + + pos[it->first]=(pos[it->first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log10(c))); + + v[y].push_back(make_pair(x,log10(c))); + + } + + dp[1]=0; + + pos[1]=1; + + for(int i=2;i<=n;i++) dp[i]=INT_MAX; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/64a2aa78-15a5-4314-83b9-d55a80d3e189.cpp b/distmin/djikstra/64a2aa78-15a5-4314-83b9-d55a80d3e189.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca822482121c666bb9f7a7a4acb6f403ff1e2159 --- /dev/null +++ b/distmin/djikstra/64a2aa78-15a5-4314-83b9-d55a80d3e189.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + double newcost=y+(*it).second; + + if((dp[(*it).first]-1e-9)>newcost) + + { + + dp[(*it).first]=newcost; + + pos[(*it).first]=pos[x]; + + q.push({(*it).first,dp[(*it).first]}); + + } + + else + + if(fabs(newcost-dp[(*it).first])<=1e-9) + + { + + pos[(*it).first]=(pos[(*it).first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log2(c))); + + v[y].push_back(make_pair(x,log2(c))); + + } + + dp[1]=0; + + for(int i=2;i<=n;i++) dp[i]=1.0*INT_MAX; + + pos[1]=1; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/68b3e404-b31f-49a3-b4d6-0ff707b76e5b.cpp b/distmin/djikstra/68b3e404-b31f-49a3-b4d6-0ff707b76e5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cbce39ee8b008159b4db00f4dacb731b0d5784b --- /dev/null +++ b/distmin/djikstra/68b3e404-b31f-49a3-b4d6-0ff707b76e5b.cpp @@ -0,0 +1,104 @@ +#include +using namespace std ; + +const int NR = 1505 ; + +const long double oo = 100000 ; + +const double ER = 0.000000001; + +ifstream in ("dmin.in") ; + +ofstream out ("dmin.out") ; + +vector < pair < int , long double > > v [ NR ] ; + +vector < int > way ( NR , 0 ) ; + +vector < long double > d ( NR , oo ) ; + +int n , m ; + +struct cmp { + + inline bool operator() ( const pair < int , long double > i , const pair < int , long double > j ) { + + return i.second > j.second ; + + } + +}; + +priority_queue < pair < int , long double > , vector < pair < int , long double > > , cmp > q ; + +void dijkstra ( ) { + + int nod ; + + long double cost ; + + q.push( make_pair ( 1 , 0 ) ) ; + + d [ 1 ] = 0 ; + + way [ 1 ] = 1 ; + + vector < pair < int , long double > > :: iterator it ; + + while ( !q.empty() ) { + + nod = q.top().first; + + cost = q.top().second ; + + q.pop() ; + + if ( d [ nod ] != cost ) continue ; + + for ( it = v [ nod ].begin() ; it != v [ nod ].end() ; ++ it ) { + + if ( d [ nod ] + (*it).second == d [ (*it).first ] || abs ( d [ nod ] + (*it).second - d [ (*it).first ] ) < ER ) { + + way [ (*it).first ] += way [ nod ] ; + + if ( way [ (*it).first ] > 104659 ) way [ (*it).first ] -= 104659 ; + + } else + + if ( d [ nod ] + (*it).second < d [ (*it).first ] ) { + + d [ (*it).first ] = d [ nod ] + (*it).second ; + + q.push( make_pair ( (*it).first , d [ (*it).first ] ) ) ; + + way [ (*it).first ] = way [ nod ] ; + + } + + } + + } + +} + +int main () { + + int i , j , x , y , z ; + + in >> n >> m ; + + while ( m -- ) { + + in >> x >> y >> z ; + + v [ x ].push_back ( make_pair ( y , log ( z ) ) ) ; + + v [ y ].push_back ( make_pair ( x , log ( z ) ) ) ; + + } + + dijkstra() ; + + for ( i = 2 ; i <= n ; ++ i ) out << way [ i ] << ' ' ; + +} diff --git a/distmin/djikstra/71d225cb-08c9-4962-80ea-955b336ea2a1.cpp b/distmin/djikstra/71d225cb-08c9-4962-80ea-955b336ea2a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faf1083b410b028da95cb9b2ea04959fbaecefb8 --- /dev/null +++ b/distmin/djikstra/71d225cb-08c9-4962-80ea-955b336ea2a1.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +using namespace std; +int n; + +int nr[1502],viz[1502]; + +double dist[1502]; + + + +vector > v[1502]; + + + +inline int cmp(double a,double b) + +{ + + if(a>b+0.0000000001) + + return 1; + + if(a+0.0000000001 +#include +#include +#include + + + + +using namespace std; + + + +FILE *fin=fopen("dmin.in","r"); + +FILE *fout=fopen("dmin.out","w"); + + + +const double eps=1e-6; + +vector v[1510]; + +vector cost[1510]; + +int i,j,n,m,nrd[1510]; + +double dmin[1510]; + +queue q; + +bool inq[1510]; + + + +int main() + +{ + + fscanf(fin,"%d%d",&n,&m); + + for (i=1; i<=m; i++) + + { + + int a,b,c; + + fscanf(fin,"%d%d%d",&a,&b,&c); + + v[a].push_back(b); + + v[b].push_back(a); + + cost[a].push_back(log(c)); + + cost[b].push_back(log(c)); + + } + + q.push(1); + + nrd[1]=1; + + while (!q.empty()) + + { + + int x=q.front(); + + inq[x]=0; + + q.pop(); + + for (i=0; i-eps) + + { + + nrd[vecin]=(nrd[vecin]+nrd[x])%104659; + + } + + else if (dmin[x]+pret+eps + + +using namespace std; + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +const int NMAX = 1505; + +const double EPS = 0.0000000001; + +const double INF = 1000000000; + +const int MOD = 104659; + +vector > v[NMAX]; + + + +struct Data { + + int node; + + double cost; + + bool operator< (Data other) const { + + return cost > other.cost; + + } + +}; + + + +priority_queue hp; + +double best[NMAX]; + +int dp[NMAX]; + + + +void dijkstra() { + + for (int i = 0; i < NMAX; i ++) + + best[i] = 1000000000; + + hp.push({1, 0}); + + best[1] = 0; + + dp[1] = 1; + + while (hp.size()) { + + Data s = hp.top(); + + hp.pop(); + + for (auto i : v[s.node]) { + + if (best[i.first] - EPS > (s.cost + i.second)) { + + best[i.first] = s.cost + i.second; + + dp[i.first] = dp[s.node]; + + hp.push({i.first, best[i.first]}); + + } else if (fabs(best[i.first] - s.cost - i.second) <= EPS) { + + dp[i.first] += dp[s.node]; + + if (dp[i.first] >= MOD) + + dp[i.first] -= MOD; + + } + + } + + } + +} + + + + + +int main() { + + ios::sync_with_stdio(false); + + int n, m; + + in >> n >> m; + + for (int i = 1; i <= m; i ++) { + + int x, y; + + double c; + + in >> x >> y >> c; + + v[x].push_back({y, log10(c)}); + + v[y].push_back({x, log10(c)}); + + } + + dijkstra(); + + for (int i = 2; i <= n; i ++) + + out << dp[i] << " "; + + return 0; + +} diff --git a/distmin/djikstra/7b504201-2e82-4c70-8431-da8b5bdbea10.cpp b/distmin/djikstra/7b504201-2e82-4c70-8431-da8b5bdbea10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4be0a054da2b2d945cacfaf5726654fe32eb88f3 --- /dev/null +++ b/distmin/djikstra/7b504201-2e82-4c70-8431-da8b5bdbea10.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int MAXN = 1502; + +const int MODULO = 104659; + +const double EPSILON = 1e-10; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +int n; + +vector vecini[MAXN]; + +vector cost[MAXN]; + + + + + +struct nod_dijk + +{ + + int nod; + + double dist; + + bool operator < (const nod_dijk b) const + + { + + return dist > b.dist; + + } + +}; + + + +priority_queue heap; + + + +void citire() + +{ + + int m; + + in >> n >> m; + + for (int i = 1;i <= m;++i) + + { + + int a, b, c; + + in >> a >> b >> c; + + + + vecini[a].push_back(b); + + cost[a].push_back(log((double)c)); + + + + + + vecini[b].push_back(a); + + cost[b].push_back(log((double)c)); + + } + +} + + + +double d[MAXN]; + +int p[MAXN]; + + + +void dijkstra() + +{ + + for (int i = 0;i < MAXN;++i) + + { + + d[i] = numeric_limits::max(); + + p[i] = 0; + + } + + heap.push((nod_dijk){1,0}); + + d[1] = 0; + + p[1] = 1; + + while(!heap.empty()) + + { + + int nod = heap.top().nod; + + double dist = heap.top().dist; + + heap.pop(); + + if(abs(d[nod] - dist) > EPSILON) + + continue; + + p[nod] = 0; + + for(unsigned int i = 0;i < vecini[nod].size();++i) + + { + + int vecin = vecini[nod][i]; + + double c = cost[nod][i]; + + if (d[vecin] == numeric_limits::max()) + + continue; + + if (abs(d[vecin] + c - d[nod]) <= EPSILON) + + p[nod] += p[vecin]; + + } + + if(p[nod] == 0) + + p[nod] = 1; + + p[nod] %= MODULO; + + + + for(unsigned int i = 0;i < vecini[nod].size();++i) + + { + + int vecin = vecini[nod][i]; + + double c = cost[nod][i]; + + if (d[nod] + c < d[vecin]) + + { + + d[vecin] = d[nod] + c; + + heap.push((nod_dijk){vecin, d[vecin]}); + + } + + } + + } + +} + + + +int main() + +{ + + citire(); + + dijkstra(); + + for (int i = 2;i <= n;++i) + + out << p[i] << ' '; + + out << '\n'; + + return 0; + +} diff --git a/distmin/djikstra/7f6926b8-6c04-460f-98cb-7406495b7843.cpp b/distmin/djikstra/7f6926b8-6c04-460f-98cb-7406495b7843.cpp new file mode 100644 index 0000000000000000000000000000000000000000..567749d0e93baa5e64156549b2fd1b8ddc68929c --- /dev/null +++ b/distmin/djikstra/7f6926b8-6c04-460f-98cb-7406495b7843.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +using namespace std; + +double dp[1505]; + +vector > v[1505]; + +int pos[1505],n,m,x,y,c; + +typedef struct edge + +{ + + int nod; + + double cost; + + bool operator<(const edge& other) const + + { + + return cost>other.cost; + + } + +}; + +priority_queue q; + +void Dijkstra() + +{ + + while(!q.empty()) + + { + + int x=q.top().nod; + + double y=q.top().cost; + + + + q.pop(); + + for(vector >::iterator it=v[x].begin();it!=v[x].end();it++) + + { + + double newcost=y+(*it).second; + + if((dp[(*it).first]-1e-9)>newcost) + + { + + dp[(*it).first]=newcost; + + pos[(*it).first]=pos[x]; + + q.push({(*it).first,dp[(*it).first]}); + + } + + else + + if(fabs(newcost-dp[(*it).first])<=1e-9) + + { + + pos[(*it).first]=(pos[(*it).first]+pos[x])%104659; + + } + + } + + } + +} + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&c); + + v[x].push_back(make_pair(y,log2(c))); + + v[y].push_back(make_pair(x,log2(c))); + + } + + dp[1]=0; + + for(int i=2;i<=n;i++) dp[i]=1.0*INT_MAX; + + pos[1]=1; + + q.push({1,0}); + + Dijkstra(); + + for(int i=2;i<=n;i++) printf("%d ",pos[i]); + + return 0; + +} diff --git a/distmin/djikstra/88f27220-d1e5-40c2-9f42-146b576b324b.cpp b/distmin/djikstra/88f27220-d1e5-40c2-9f42-146b576b324b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22e6e91f255eb103987ab793e750727bdf51843b --- /dev/null +++ b/distmin/djikstra/88f27220-d1e5-40c2-9f42-146b576b324b.cpp @@ -0,0 +1,217 @@ +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +struct Nod{ + + int nod; + + double val; + +}; + + + + + +int heap[50004]; + +int n; + +int getMin() + +{ + + return heap[1]; + +} + +double cost[10000]; + +void add(int x) + +{ + + + + n++; + + int poz=n; + + heap[n]=x; + + while(poz>1 && cost[heap[poz/2]]>cost[heap[poz]]) + + { + + swap(heap[poz/2], heap[poz]); + + poz/=2; + + } + +} + +void popMin() + +{ + + + + heap[1]=heap[n]; + + heap[n]=0; + + int poz=1; + + n--; + + while(2*poz<=n) + + { + + poz*=2; + + if(pozcost[heap[poz+1]]) poz++; + + if(cost[heap[poz]] < cost[heap[poz/2]]) + + swap(heap[poz/2], heap[poz]); + + else poz=n; + + } + +} +int mod=104659; + +vector muchii[100000]; + + + +int mn[10000]; + +int viz[10000]; + +int pct, much; + +int main() + +{ + + f>>pct>>much; + + for(int i=1;i<=much;++i) + + { + + int a,b; + + double x,c; + + f>>a>>b>>c; + + + + x=log(c); + + muchii[a].push_back({b,x}); + + muchii[b].push_back({a,x}); + + } + + for(int i=2;i<=pct;++i) + + { + + cost[i]=1e9; + + } + + int elem; + + add(1); + + cost[1]=0; + + viz[1]=1; + + mn[1]=1; + + + + while(n>0) + + { + + elem=getMin(); + + popMin(); + + viz[elem]=0; + + for(int i=0;i1e-8) + + { + + mn[vecin.nod]=mn[elem]%mod; + + cost[vecin.nod]=cost[elem]+vecin.val; + + + + if(viz[vecin.nod]==0) + + { + + viz[vecin.nod]=1; + + add(vecin.nod); + + } + + } + + else { + + if(abs(vecin.val+cost[elem]-cost[vecin.nod])<1e-8) + + { + + mn[vecin.nod]+=mn[elem]; + + mn[vecin.nod]%=mod; + + } + + } + + + + } + + } + + for(int i=2;i<=pct;++i) + + { + + g< +#include +#include +#include +using namespace std; +vector < pair < double, int > > v[ 1505 ]; + +priority_queue < pair < double, int > > Q; + + + +double dist[ 1505 ]; + +int in_queue[ 1505 ]; + +int dinamica[ 1505 ]; + + + +void aflaDrumuri( int nod ){ + + + + int i, k, fiu; + + double cost; + + + + dist[ nod ] = 0; + + dinamica[ nod ] = 1; + + Q.push( { 0.0, nod } ); + + + + while( !Q.empty() ){ + + nod = Q.top().second; + + Q.pop(); + + if( in_queue[ nod ] ) continue; + + in_queue[ nod ] = 1; + + for( i = 0; i < v[ nod ].size(); ++i ){ + + fiu = v[ nod ][ i ].second; + + cost = v[ nod ][ i ].first; + + if( dist[ fiu ] - ( dist[ nod ] + cost ) > 1e-10 ){ + + dist[ fiu ] = dist[ nod ] + cost; + + dinamica[ fiu ] = dinamica[ nod ]; + + Q.push( { -dist[ fiu ], fiu } ); + + } + + else if( fabs( dist[ fiu ] - ( dist[ nod ] + cost ) ) < 1e-10 ){ + + dinamica[ fiu ] += dinamica[ nod ]; + + dinamica[ fiu ] %= 104659; + + } + + } + + } + + + + + + + +} + + + +int main() + +{ + + + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + int n, m, i, j, s, t, d, k, x, y, c; + + + + scanf("%d%d",&n,&m); + + while( m-- ){ + + scanf("%d%d%d",&x,&y,&c); + + v[ x ].push_back( { log10( c ), y } ); + + v[ y ].push_back( { log10( c ), x } ); + + } + + + + for( i = 1; i <= n; ++i ) + + dist[ i ] = 10000.0; + + + + aflaDrumuri( 1 ); + + for( i = 2; i <= n; ++i ) + + printf("%d ",dinamica[ i ]); + + + + + + return 0; + + + +} diff --git a/distmin/djikstra/8978a25a-2102-4142-97b1-4bffbf29ac0d.cpp b/distmin/djikstra/8978a25a-2102-4142-97b1-4bffbf29ac0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4531c1cd30490762c7cc03521b3ab78f0d46b3b --- /dev/null +++ b/distmin/djikstra/8978a25a-2102-4142-97b1-4bffbf29ac0d.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); + + + +const int N = 1502; + +const double eps = 0.0000001; + +const int M = 104659; + + + +int n, m, sol[N], nod; + +double dist[N]; + +bool ver[N]; + +vector > v[N]; + +queue q; + + + +void dijkstra() { + + int i; + + vector >::iterator it; + + + + for(i = 1; i<=n; ++i) + + dist[i] = 29999999.0; + + + + ver[1] = true; + + q.push(1); + + dist[1] = 0; + + sol[1] = 1; + + + + while(!q.empty()) { + + nod = q.front(); + + q.pop(); + + ver[nod] = false; + + + + for(it = v[nod].begin(); it!=v[nod].end(); ++it) { + + if(dist[it->first] - dist[nod] - it->second > eps) { + + dist[it->first] = dist[nod] + it->second; + + sol[it->first] = sol[nod]; + + + + if(!ver[it->first]) { + + q.push(it->first); + + ver[it->first] = true; + + } + + } + + else if(dist[nod] + it->second - dist[it->first] <= eps) { + + sol[it->first] += sol[nod]; + + sol[it->first]%=M; + + } + + } + + } + +} + + + +int main() { + + int i, x, y; + + double c; + + + + in >> n >> m; + + + + for(i = 1; i<=m; ++i) { + + in >> x >> y >> c; + + c = log(c); + + + + v[x].push_back(pair(y, c)); + + v[y].push_back(pair(x, c)); + + } + + + + dijkstra(); + + + + for(i = 2; i<=n; ++i) + + out << sol[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/8afbd6d7-4e85-4863-bdb8-93795df2cb41.cpp b/distmin/djikstra/8afbd6d7-4e85-4863-bdb8-93795df2cb41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5df667284535ee570014ade3ef5e20e104dbe0fe --- /dev/null +++ b/distmin/djikstra/8afbd6d7-4e85-4863-bdb8-93795df2cb41.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +priority_queue < pair < double, int > > PQ; + + + +vector < pair < int, double > > v[1505]; + + + +double D[1505]; + + + +int NR[1505]; + + + +bitset < 1505 > inQueue; + + + +int N, M; + +void read(){ + freopen("dmin.in", "r", stdin); + + + scanf("%d %d", &N, &M); + + + + int x, y, c; + + while(M--){ + + scanf("%d %d %d", &x, &y, &c); + + + + v[x].push_back(make_pair(y,log(c))); + + v[y].push_back(make_pair(x,log(c))); + + } + + fclose(stdin); +} + + +void Dijkstra(){ + + for(int i=2; i<=N; ++i) + + D[i] = 9999999.0; + + + + NR[1] = 1; + + PQ.push(make_pair(0.0, 1)); + + inQueue[1] = true; + + + + while(!PQ.empty()){ + + double dx = -PQ.top().first; + + int x = PQ.top().second; + + + + PQ.pop(); + + inQueue[x] = false; + + + + for(typeof(v[x].begin()) it=v[x].begin(); it!=v[x].end(); ++it) + + if(D[(*it).first] - dx - (*it).second > 0.000000001){ + + D[(*it).first] = dx + (*it).second; + + NR[(*it).first] = NR[x]; + + + + if(!inQueue[(*it).first]){ + + PQ.push(make_pair(-D[(*it).first], (*it).first)); + + inQueue[(*it).first] = true; + + } + + } + + else if(abs(D[(*it).first] - dx - (*it).second) < 0.000000001) + + NR[(*it).first] = (0LL + NR[(*it).first] + NR[x]) % 104659; + + } +} + +void print(){ + freopen("dmin.out", "w", stdout); + + + + for(int i=2; i<=N; ++i) + + printf("%d ", NR[i]); + + fclose(stdout); +} + +int main(){ + read(); + Dijkstra(); + print(); + + return 0; +} diff --git a/distmin/djikstra/906b983b-bd28-4f6b-adfa-b89393d64a47.cpp b/distmin/djikstra/906b983b-bd28-4f6b-adfa-b89393d64a47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c1344f801fb60fbe84dab13087053902c117f46 --- /dev/null +++ b/distmin/djikstra/906b983b-bd28-4f6b-adfa-b89393d64a47.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +using namespace std; + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +vector > v[1510]; + +int x,y,n,m,i,A[1510],d,H[1510],L,P[1510]; + +double c,D[1510]; + +double ab(double x){if(x<0) return -x; return x;} + +void urca(int po) + +{ + + while(po&&D[H[po]]>1]]) + + { + + swap(H[po],H[po>>1]); + + swap(P[H[po]],P[H[po>>1]]); + + po>>=1; + + } + +} + +void coboara(int po){ + + int y=0; + + while(po!=y){ + + y=po; + + if((y<<1)<=L&&D[H[y<<1]]>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,log(c))); + + v[y].push_back(make_pair(x,log(c))); + + } + + for(i=2;i<=n;++i) + + D[i]=1<<30; + + H[++L]=1; + + P[1]=1; + + A[1]=1; + + while(L) + + { + + x=H[1]; + + H[1]=H[L--]; + + coboara(1); + + for(i=0;i0.000001&&D[x]+c=104659) + + A[d]-=104659; + + } + + } + + } + + for(i=2;i<=n;++i) + + g< +#include +#include +#include +#include +#include +using namespace std; + + + +struct nod { + + int y; + + double cost; + + inline nod(int _y, double _cost) { + + y=_y; + + cost=_cost; + + } + + inline bool operator < (const nod &c) const { + + return (double)cost>c.cost; + + } + +}; + +priority_queue c; + +vector v[1501]; + +int pos[1501]; + +double d[1501]; + +bitset <1501> p; + +inline double modul(double x) + +{ + + if(x<0) + + return -x; + + return x; + +} + +void dijkstra() + +{ + + int x; + + c.push(nod(1,0)); + + while(c.empty()==0) { + + x=c.top().y; + + c.pop(); + + if(p[x]==0) { + + p[x]=1; + + for(vector :: iterator it=v[x].begin();it!=v[x].end();it++) + + if(it->y!=1) { + + if((double)d[it->y]-d[x]-it->cost>0.0000000001) { + + d[it->y]=(double)d[x]+it->cost; + + pos[it->y]=(pos[x])%104659; + + c.push(nod(it->y,d[it->y])); + + } + + else if(modul(d[it->y]-d[x]-it->cost)<=0.0000000001) { + + pos[it->y]=(pos[it->y]+pos[x])%104659; + + c.push(nod(it->y,d[it->y])); + + } + + } + + } + + } + +} + +int main () + +{ + + int i,n,m,x,y; + + double cost; + + ifstream f("dmin.in"); + + ofstream g("dmin.out"); + + f>>n>>m; + + for(i=1;i<=m;i++) { + + f>>x>>y>>cost; + + cost=log(cost); + + v[x].push_back(nod(y,cost)); + + v[y].push_back(nod(x,cost)); + + } + + f.close(); + + for(i=1;i<=n;i++) + + d[i]=2147000000; + + d[1]=1; + + pos[1]=1; + + dijkstra(); + + for(i=2;i<=n;i++) + + g< +#include +#include +#include +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + +bool Equal (double a, double b) + +{ + + return fabs(a-b)<1e-8; + +} + + + +typedef pair PI; + + + +struct CMP + +{ + + bool operator () (const PI& a, const PI& b) const + + { + + return a.second>b.second; + + } + +}; + + + +priority_queue, CMP> Q; + +int N, M; + +vector G[1510]; + +double Dist[1510]; + +int DP[1510]; + + + +void Read () + +{ + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + int a, b, c; + + f >> a >> b >> c; + + G[a].push_back(make_pair(b, log(1.0*c))); + + G[b].push_back(make_pair(a, log(1.0*c))); + + } + + + + f.close(); + +} + + + +void DoDijkstra () + +{ + + vector::iterator it; + + int i, first; + + double second; + + + + for (i=1; i<=N; i++) + + Dist[i]=999999999; + + + + Dist[1]=0; + + DP[1]=1; + + Q.push(make_pair(1, 0)); + + + + while (!Q.empty()) + + { + + first=Q.top().first; + + second=Q.top().second; + + Q.pop(); + + + + if (!Equal(Dist[first], second)) + + continue; + + + + for (it=G[first].begin(); it!=G[first].end(); ++it) + + if (second+it->secondfirst] || Equal(second+it->second, Dist[it->first])) + + { + + if (second+it->second<=Dist[it->first]-1e-8) + + { + + Dist[it->first]=second+it->second; + + Q.push(make_pair(it->first, Dist[it->first])); + + DP[it->first]=0; + + } + + DP[it->first]+=DP[first]; + + if (DP[it->first]>=104659) + + DP[it->first]-=104659; + + } + + } + +} + + + +void Print () + +{ + + for (int i=2; i<=N; i++) + + g << DP[i] << ' '; + + g << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + DoDijkstra(); + + Print(); + + + + return 0; + +} diff --git a/distmin/djikstra/9ae8c760-0cf6-4161-8052-2291eefb5d04.cpp b/distmin/djikstra/9ae8c760-0cf6-4161-8052-2291eefb5d04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd20034fe41cc554ffae162bd054b7e7eaff98ba --- /dev/null +++ b/distmin/djikstra/9ae8c760-0cf6-4161-8052-2291eefb5d04.cpp @@ -0,0 +1,157 @@ +#include + + +using namespace std; + + + +ifstream in("dmin.in"); + +ofstream out("dmin.out"); +int x,y,n,m; + +double L[50005]; + +bool viz[50005]; + +const double eps = 1e-6; + +double c; + + + +vector > v[50005]; + +vector < int > T; + + + +int D[50005]; + + + +int cmp(const int&a, const int&b){ + + return(L[a] > L[b]); + +} + + + +void dijkstra(){ + + + + int node; + + T.push_back(1); + + make_heap(T.begin(),T.end(),cmp); + + while(!T.empty()){ + + node=T.front(); + + pop_heap(T.begin(),T.end(),cmp); + + T.pop_back(); + + for(int i=0;i L[node] + v[node][i].second){ + + L[v[node][i].first] = L[node] + v[node][i].second; + + T.push_back(v[node][i].first); + + push_heap(T.begin(),T.end(),cmp); + + } + + + + } + + } + + + + + +} + + + + + + + +int Solve(int node){ + + + + if(viz[node])return D[node]; + + viz[node]=1; + + for(int i=0;i>n>>m; + + + + for(int i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + v[x].push_back({y,log(c)}); + + v[y].push_back({x,log(c)}); + + + + } + + for(int i=2;i<=n;i++) + + L[i]=INFINITY; + + dijkstra(); + + viz[1]=1; + + D[1]=1; + + for(int i=2;i<=n;i++){ + + D[i]=Solve(i); + + } + + for(int i=2;i<=n;i++) + + out< +#include +#include +#include +#include +#include +using namespace std; + +int n,i,j,k,m,x,y,rez[1501]; + +double dist[1501]; + +vector >v[1501]; + +dequec; + +bool viz[1501]; + + + +int main() + +{ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d %d",&n,&m); + + for (i=1;i<=m;i++) + + { + + double cost; + + scanf("%d %d %lf",&x,&y,&cost); + + cost=log(cost); + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(i=2;i<=n;i++) dist[i]=2000000000; + + c.push_back(1);rez[1]=1;viz[1]=true; + + while (!c.empty()) + + { + + x=c.front();c.pop_front();viz[x]=false; + + for (j=0;jfabs(dist[x]+cost-dist[y])) + + { + + rez[y]+=rez[x]; + + if (rez[y]>=104659) rez[y]-=104659; + + } + + } + + } + + for (i=2;i<=n;i++) printf("%d ",rez[i]); + + return 0; + +} diff --git a/distmin/djikstra/a336a16a-9ba5-4a3e-af67-d5d8e888c9b8.cpp b/distmin/djikstra/a336a16a-9ba5-4a3e-af67-d5d8e888c9b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17583c19677ffeb0835bdc856720fa9bd30c1c0f --- /dev/null +++ b/distmin/djikstra/a336a16a-9ba5-4a3e-af67-d5d8e888c9b8.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream in("dmin.in"); +ofstream out("dmin.out"); + +const int N = 1502; +const double eps = 0.0000001; +const int M = 104659; + +int n, m, sol[N], nod; +double dist[N]; +bool ver[N]; +vector > v[N]; +queue q; + +void dijkstra() { + int i; + vector >::iterator it; + + for(i = 1; i<=n; ++i) + dist[i] = 29999999.0; + + ver[1] = true; + q.push(1); + dist[1] = 0; + sol[1] = 1; + + while(!q.empty()) { + nod = q.front(); + q.pop(); + ver[nod] = false; + + for(it = v[nod].begin(); it!=v[nod].end(); ++it) { + if(dist[it->first] - dist[nod] - it->second > eps) { + dist[it->first] = dist[nod] + it->second; + sol[it->first] = sol[nod]; + + if(!ver[it->first]) { + q.push(it->first); + ver[it->first] = true; + } + } + else if(dist[nod] + it->second - dist[it->first] <= eps) { + sol[it->first] += sol[nod]; + sol[it->first]%=M; + } + } + } +} + +int main() { + int i, x, y; + double c; + + in >> n >> m; + + for(i = 1; i<=m; ++i) { + in >> x >> y >> c; + c = log(c); + + v[x].push_back(pair(y, c)); + v[y].push_back(pair(x, c)); + } + + dijkstra(); + + for(i = 2; i<=n; ++i) + out << sol[i] << " "; + + return 0; +} diff --git a/distmin/djikstra/a9a65886-1592-4c60-844c-4f50bd836c90.cpp b/distmin/djikstra/a9a65886-1592-4c60-844c-4f50bd836c90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39ca113beae9e31fafd7760bd3b9f4ef55d297cf --- /dev/null +++ b/distmin/djikstra/a9a65886-1592-4c60-844c-4f50bd836c90.cpp @@ -0,0 +1,205 @@ +#include +#include +#include +#include + + +using namespace std; + + + +struct Nod + +{ + + double cost = (1 << 30); + + vector> vecini; + +}; + + + +typedef vector Graf; + +typedef pair Pereche; + + + +const int kMod = 104659; + +const double kEpsilon = 0.00001; + + + +inline double Log2(int n) + +{ + + return log(n) / log(2); + +} + + + +void AflaDistante(Graf &g) + +{ + + auto cmp = [](const Pereche &a, const Pereche &b) -> bool { return a.second > b.second; }; + + priority_queue, decltype(cmp)> coada(cmp); + + + + g[1].cost = 0; + + coada.push({ 1, 0 }); + + + + while (!coada.empty()) { + + int nod = coada.top().first; + + double cost = coada.top().second; + + coada.pop(); + + + + if (cost != g[nod].cost) continue; + + + + for (auto &muchie : g[nod].vecini) { + + int vecin = muchie.first; + + double cost_nou = cost + muchie.second; + + if (g[vecin].cost - cost_nou > kEpsilon) { + + g[vecin].cost = cost_nou; + + coada.push({ vecin, cost_nou }); + + } + + } + + } + +} + + + +vector AflaModuri(const Graf &g) + +{ + + vector moduri(g.size(), 0); + + queue coada; + + vector in_coada(g.size(), false); + + + + coada.push(1); + + moduri[1] = 1; + + in_coada[1] = true; + + + + while (!coada.empty()) { + + int nod = coada.front(); + + coada.pop(); + + + + for (auto &muchie : g[nod].vecini) { + + int vecin = muchie.first; + + if (abs(g[vecin].cost - g[nod].cost - muchie.second) <= kEpsilon && g[vecin].cost - g[nod].cost > kEpsilon) { + + moduri[vecin] += moduri[nod]; + + moduri[vecin] %= kMod; + + if (!in_coada[vecin]) { + + coada.push(vecin); + + in_coada[vecin] = true; + + } + + } + + } + + } + + return moduri; + +} + + + +int main() + +{ + + ifstream fin("dmin.in"); + + ofstream fout("dmin.out"); + + + + int n, m; + + fin >> n >> m; + + + + Graf graf(n + 1); + + while (m--) { + + int x, y, c; + + fin >> x >> y >> c; + + + + double cost = Log2(c); + + graf[x].vecini.push_back({ y, cost }); + + graf[y].vecini.push_back({ x, cost }); + + } + + + + AflaDistante(graf); + + + + auto raspuns = AflaModuri(graf); + + for (int i = 2; i < raspuns.size(); ++i) + + fout << raspuns[i] << " "; + + + + return 0; + +} diff --git a/distmin/djikstra/b037f84e-284a-481a-843d-bf278eeaf624.cpp b/distmin/djikstra/b037f84e-284a-481a-843d-bf278eeaf624.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2aacf5b743975aa28b00babe85eca396e06bd14c --- /dev/null +++ b/distmin/djikstra/b037f84e-284a-481a-843d-bf278eeaf624.cpp @@ -0,0 +1,353 @@ +#include +#include +#include +using namespace std; +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + + + +int Poz[2100],Fr[2100],Nr_drum[2100]; + +int last,n,m,i; + +double Drum_min[2100]; + + + + + +struct heap{ + + double val; + + int poz; + +}Heap[2100]; + + + +struct nod{ + + int varf; + + double dist; + +}; + + + +vector L[2100]; + + + +inline void read(){ + + + + nod nod2; + + int nod1,x; + + + + f>>n>>m; + + + + for(int i=1;i<=m;++i){ + + f>>nod1>>nod2.varf>>x; + + nod2.dist=log(x); + + L[nod1].push_back(nod2); + + } + + + +} + + + +inline void initializare(){ + + for(int i=1;i<=n;i++){ + + Heap[++last].val=2000000000; + + Heap[last].poz=i; + + Poz[i]=i; + + } + + Heap[1].val=0; + + Nr_drum[1]=1; + +} + + + +inline int tata(int x){ + + return x/2; + +} + + + +inline int left_son(int x){ + + return 2*x; + +} + + + +inline int right_son(int x){ + + return 2*x+1; + +} + + + +inline void heap_down(int x){ + + + + int aux2,sec;heap aux1; + + int l1,l2; + + while(x!=last){ + + l1=left_son(x); + + l2=right_son(x); + + if(l1<=last&&l2<=last){ + + if(Heap[l1].val=Heap[x].val) + + break; + + + + aux2=Poz[Heap[sec].poz]; + + Poz[Heap[sec].poz]=Poz[Heap[x].poz]; + + Poz[Heap[x].poz]=aux2; + + aux1=Heap[sec]; + + Heap[sec]=Heap[x]; + + Heap[x]=aux1; + + x=sec; + + + + } + + + + + +} + + + +inline void heap_up(int x){ + + heap aux1;int aux2,T; + + while(Heap[T=tata(x)].val>Heap[x].val&&x!=1){ + + aux2=Poz[Heap[T].poz]; + + Poz[Heap[T].poz]=Poz[Heap[x].poz]; + + Poz[Heap[x].poz]=aux2; + + aux1=Heap[T]; + + Heap[T]=Heap[x]; + + Heap[x]=aux1; + + x=T; + + } + +} + + + +inline void update(int x){ + + heap_up(x); + + heap_down(x); + +} + + + +inline int pop(){ + + int nod1=Heap[1].poz; + + + + Drum_min[nod1]=Heap[1].val; + + + + Poz[Heap[last].poz]=1; + + + + Heap[1]=Heap[last--]; + + + + update(1); + + + + return nod1; + +} + + + +inline void update_drum(int x){ + + int poz; + + for(unsigned int i=0;iDrum_min[x]+L[x][i].dist+0.000000001&&Fr[L[x][i].varf]==0){ + + Heap[poz].val=Drum_min[x]+L[x][i].dist; + + Nr_drum[L[x][i].varf]=Nr_drum[x]; + + update(poz); + + } + + else if(Drum_min[x]+L[x][i].dist-Heap[poz].val<=0.000000001){ + + Nr_drum[L[x][i].varf]+=Nr_drum[x]; + + if(Nr_drum[L[x][i].varf]>=104659) + + Nr_drum[L[x][i].varf]-=104659; + + } + + } + + + +} + + + +inline void dijkstra(){ + + + + int x; + + + + while(last!=0){ + + x=pop(); + + Fr[x]=1; + + update_drum(x); + + } + + + +} + + + +int main(){ + + + + read(); + + + + initializare(); + + + + dijkstra(); + + + + for(i=2;i<=n;i++) + + g< +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef pair pereche; + +vector < list < pereche > > Graf; + +vector < double > d; + +int vizitat[1501], drumuri[1501]; + +const int modulo = 104659; + +const double epsilon = 1e-10; + +struct order{ + + bool operator()(const int &i,const int &j){ + + return d[i] > d[j]; + + } + +}; + +priority_queue,order> Heap; + +int n,m; + + + +void citesc(){ + + + + int x,y; + + double c; + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + + Graf.resize(n+1); + + d.resize(n+1,1000000001); + + for(register int i=1;i<=m;++i){ + + scanf("%d%d%lf",&x,&y,&c); + + c = log(c); + + Graf[x].push_back(pereche(y,c)); + + Graf[y].push_back(pereche(x,c)); + + } + +} + + + +void Dijkstra(){ + + + + int top; + + vizitat[1] = 1; + + Heap.push(1); + + drumuri[1] = 1; + + d[1] = 0; + + while(!Heap.empty()){ + + + + top = Heap.top(); + + Heap.pop(); + + for(list::iterator it = Graf[top].begin();it!=Graf[top].end();++it) + + if(d[it->first] > d[top] + it->second + epsilon){ + + drumuri[it->first] = drumuri[top]; + + d[it->first] = d[top] + it->second; + + + + if(!vizitat[it->first]){ + + vizitat[it->first] = 1; + + Heap.push(it->first); + + } + + } + + else if(abs(d[it->first]- d[top] - it->second) <= epsilon){ + + drumuri[it->first] += drumuri[top]; + + drumuri[it->first] %= modulo; + + } + + } + +} + + + +int main(){ + + + + citesc(); + + Dijkstra(); + + for(register int i=2;i<=n;++i) + + printf("%d ",drumuri[i]); + +return 0; + +} diff --git a/distmin/djikstra/c1e79172-2554-4d92-9159-fe88792c1dc2.cpp b/distmin/djikstra/c1e79172-2554-4d92-9159-fe88792c1dc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a144cdf2df066542f1a1b92f2d57a11e5ea77be5 --- /dev/null +++ b/distmin/djikstra/c1e79172-2554-4d92-9159-fe88792c1dc2.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 1500 + 5; + +const int MMAX = 2500 + 5; + +const int MOD = 104659; + +const double EPS = 1e-8; + +const double INF = 1e40; + + + +int N, M; + +vector > graph[NMAX]; + + + +double dist[NMAX]; + +int cnt[NMAX]; + +bool vis[NMAX]; + + + +priority_queue > q; + + + +void dijkstra() { + + for (int i = 1; i <= N; ++ i) + + dist[i] = INF; + + + + dist[1] = 0; + + cnt[1] = 1; + + q.push(make_pair(0, 1)); + + + + while (!q.empty()) { + + int node = q.top().second; + + q.pop(); + + + + if (vis[node]) + + continue; + + vis[node] = true; + + + + for (auto it: graph[node]) + + if (dist[node] + it.second < dist[it.first] - EPS) { + + dist[it.first] = dist[node] + it.second; + + cnt[it.first] = cnt[node]; + + q.push(make_pair(-dist[it.first], it.first)); + + } + + else if (dist[node] + it.second < dist[it.first] + EPS) { + + cnt[it.first] += cnt[node]; + + if (cnt[it.first] >= MOD) + + cnt[it.first] -= MOD; + + } + + } + +} + + + +int main() + +{ + + ifstream cin("dmin.in"); + + ofstream cout("dmin.out"); + + + + cin >> N >> M; + + for (int i = 1; i <= M; ++ i) { + + int a, b, c; + + cin >> a >> b >> c; + + + + graph[a].push_back(make_pair(b, log(c))); + + graph[b].push_back(make_pair(a, log(c))); + + } + + + + dijkstra(); + + for (int i = 2; i <= N; ++ i) + + cout << cnt[i] << " \n"[i == N]; + + return 0; + +} diff --git a/distmin/djikstra/c2cdfb04-12ec-4dc8-814c-9e283ae85aee.cpp b/distmin/djikstra/c2cdfb04-12ec-4dc8-814c-9e283ae85aee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c1a88ab5cb4e5af8b5b1d4ee718227680da0ad9 --- /dev/null +++ b/distmin/djikstra/c2cdfb04-12ec-4dc8-814c-9e283ae85aee.cpp @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include + + + + +using namespace std;ifstream is("dmin.in");ofstream os("dmin.out");double D[1505];int N,M,Nr[1505];bitset<1505>B;vector>v[1505];struct CMP{bool operator()(int a,int b){return D[a]>D[b];}};priority_queue,CMP>Q;main(){is>>N>>M;for(int i=2;i<=N;++i)D[i]=(1<<30);double cost;for(int i=1,a,b,c;i<=M;++i){is>>a>>b>>c;cost=log(c);v[a].push_back({b,cost});v[b].push_back({a,cost});}Q.push(1);Nr[1]=1;for(int i,nod;!Q.empty();){i=Q.top();Q.pop();if(B[i])continue;B[i]=1;for(int j=0;jD[i]+cost){Nr[nod]=Nr[i];D[nod]=D[i]+cost;Q.push(nod);}}}for(int i=2;i<=N;++i)os< +#include +#include +#include +using namespace std; + +FILE *fin,*fout; + +vector < pair > G[1505]; + +int n,k,apar[1505]; + +double d[1505]; + +priority_queue< pair, vector >, greater > > HEAP; + +bool use[1505]; + +void dijkstra(short nod) + +{ + +short vec; + +double cost; + +for(int i=1;i<=n;i++) + +d[i]=(1<<30); + +d[nod]=0; + +apar[1]=1; + +HEAP.push(make_pair(0,nod)); + +while(!HEAP.empty()) + +{ + +nod=HEAP.top().second; + +HEAP.pop(); + +if(use[nod]) + +continue; + +use[nod]=1; + +for(size_t i=0;id[nod]+cost+0.000001) + +{ + +d[vec]=d[nod]+cost; + +apar[vec]=apar[nod]; + +HEAP.push(make_pair(d[vec],vec)); + +continue; + +} + +if(abs(d[vec]-d[nod]-cost)<=0.000001) + +{ + +apar[vec]+=apar[nod]; + +apar[vec]%=104659; + +} + +} + +} + +} + +void read() + +{ + +fin=fopen("dmin.in","r"); + +int x,y,m; + +double z; + +fscanf(fin,"%d %d",&n,&m); + +while(m--) + +{ + +fscanf(fin,"%d %d %lf",&x,&y,&z); + +z=log(z); + +G[x].push_back(make_pair(y,z)); + +G[y].push_back(make_pair(x,z)); + +} + +fclose(fin); + +} + +void print() + +{ + +fout=fopen("dmin.out","w"); + +for(int i=2;i<=n;i++) + +fprintf(fout,"%d ",apar[i]); + +fclose(fout); + +} + +int main() + +{ + +read(); + +dijkstra(1); + +print(); + +return 0; + +} diff --git a/distmin/djikstra/ccff109d-7395-430d-bc73-913f973f20ce.cpp b/distmin/djikstra/ccff109d-7395-430d-bc73-913f973f20ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..567749d0e93baa5e64156549b2fd1b8ddc68929c --- /dev/null +++ b/distmin/djikstra/ccff109d-7395-430d-bc73-913f973f20ce.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +#include +#include +#include +using namespace std; + +vector > g[1510]; + +int n,m; + +double best[1510]; + +int dp[1510]; + +struct Compare{ + + bool operator()(pair a,pair b){ + + return a.second>b.second; + + } + +}; + +priority_queue,vector >,Compare> Heap; + +void Dijkstra(){ + + int i,node; + + double cost; + + for(i=2;i<=n;i++) + + best[i]=1000000000; + + best[1]=0; + + dp[1]=1; + + Heap.push(make_pair(1,0)); + + while(!Heap.empty()){ + + node=Heap.top().first; + + cost=Heap.top().second; + + Heap.pop(); + + for(i=0;i=cost+g[node][i].second){ + + best[g[node][i].first]=cost+g[node][i].second; + + dp[g[node][i].first]=dp[node]; + + Heap.push(make_pair(g[node][i].first,best[g[node][i].first])); + + } + + else + + if(fabs(best[g[node][i].first]-cost-g[node][i].second)<=1e-8){ + + dp[g[node][i].first]+=dp[node]; + + if(dp[g[node][i].first]>=104659) + + dp[g[node][i].first]-=104659; + + } + + } + + } + +} + +int main(){ + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + int i,a,b; + + double c; + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++){ + + scanf("%d%d%lf",&a,&b,&c); + + g[a].push_back(make_pair(b,log10(c))); + + g[b].push_back(make_pair(a,log10(c))); + + } + + Dijkstra(); + + for(i=2;i<=n;i++) + + printf("%d ",dp[i]); + + return 0; + +} diff --git a/distmin/djikstra/d1f2ef81-db8f-4349-a0b3-411ce9262195.cpp b/distmin/djikstra/d1f2ef81-db8f-4349-a0b3-411ce9262195.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73a71b3dd1baa47458c45568173cabfa54908d06 --- /dev/null +++ b/distmin/djikstra/d1f2ef81-db8f-4349-a0b3-411ce9262195.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +const int Nmax = 1501; +const double eps = 0.0000001; +const int modulo = 104659; + +vector numar(Nmax); +vector dist(Nmax); +bitset viz; +vector< pair > Graf[Nmax]; +queue Q; + +int N, M; + +void citire(){ + + ifstream f("dmin.in"); + + f >> N >> M; + + int a, b; + double c; + + for(; M; M--) { + + f >> a >> b >> c; + c = log(c); + + Graf[a].push_back(pair(b, c)); + Graf[b].push_back(pair(a, c)); + } +} + + +void dijkstra() { + + vector< pair >::iterator it; + + for(int i = 1; i <= N; ++i) + dist[i] = 9999999.0; + + Q.push(1); + dist[1] = 0; + numar[1] = 1; + viz[1] = 1; + + while(!Q.empty()){ + + int nod = Q.front(); + Q.pop(); + + for(it = Graf[nod].begin(); it != Graf[nod].end(); ++it){ + + if(dist[it->first] - dist[nod] - it->second > eps){ + + dist[it->first] = dist[nod] + it->second; + numar[it->first] = numar[nod]; + + if(!viz[it->first]){ + + viz[it->first] = 1; + Q.push(it->first); + } + } + else + if(dist[nod] + it->second - dist[it->first] <= eps){ + + numar[it->first] += numar[nod]; + numar[it->first] %= modulo; + } + } + } +} + +void afis(){ + + ofstream g("dmin.out"); + + for(int i = 2; i <= N; ++i) + g << numar[i] << " "; +} + +int main() { + + citire(); + dijkstra(); + afis(); + + return 0; +} diff --git a/distmin/djikstra/d9dec4d1-9cbd-4b9e-aebb-f9611885f155.cpp b/distmin/djikstra/d9dec4d1-9cbd-4b9e-aebb-f9611885f155.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f789f939d2d6afc73b31157d455586a4067ef0f --- /dev/null +++ b/distmin/djikstra/d9dec4d1-9cbd-4b9e-aebb-f9611885f155.cpp @@ -0,0 +1,223 @@ +#include +#include using namespace std; +double make_abs(double x) + + { + + if(x>0)return x; + + return -x; + + } + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +typedef struct _lista + + { + + int x; + + double c; + + _lista *urm; + + }*Lista; + +typedef struct _listan + + { + + int x; + + _listan *urm; + + }*Listan; + +typedef struct _nod + + { + + Lista l; + + }Nod; + +Nod V[1501]; + +Listan prim=NULL; + +double c[1501]; + +long mod[1501]; + +int v[1501],coada[1501],pi[1501],n,m,p,u; + +void adauga(int x,int y,int cost) + + { + + Lista nou=new _lista; + + nou->x=y;nou->c=cost; + + nou->urm=V[x].l; + + V[x].l=nou; + + } + +void citire() + + { + + int i,cost,x,y; + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>x>>y>>cost,adauga(x,y,cost),adauga(y,x,cost); + + } + +void getmin(int p,int u) + + { + + int i,aux; + + double min=c[coada[p]]; + + int minpoz=p; + + for(i=p+1;i<=u;i++) + + if(min>c[coada[i]])min=c[coada[i]],minpoz=i; + + aux=coada[p],coada[p]=coada[minpoz],coada[minpoz]=aux; + + } + +void dijkstra(int k) + + { + + Lista i; + + p=u=1;coada[1]=k; + + v[k]=1;c[k]=0; + + while(p<=u) + + { + + getmin(p,u); + + k=coada[p]; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + coada[++u]=i->x,v[i->x]=1,pi[i->x]=k, + + c[i->x]=c[k]+log(i->c); + + + + else + + if(c[i->x]>c[k]+log(i->c)) + + pi[i->x]=k,c[i->x]=c[k]+log(i->c); + + p++; + + } + + } + +void adauga(int x) + + { + + Listan nou=new _listan; + + nou->x=x; + + nou->urm=prim; + + prim=nou; + + } + +void df(int k) + + { + + v[k]=1; + + Lista i; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + if(make_abs(c[k]+log(i->c)-c[i->x])<0.00001) + + df(i->x); + + adauga(k); + + } + +int main() + +{ + +int i; + +Lista j; + +citire();f.close(); + +dijkstra(1); + +for(i=1;i<=n;i++)v[i]=0; + +mod[1]=1; + +df(1); + +Listan ii; + +for(ii=prim;ii;ii=ii->urm) + + { + + + + for(j=V[ii->x].l;j;j=j->urm) + + if(make_abs(c[ii->x]+log(j->c)-c[j->x])<0.00001) + + mod[j->x]=(mod[j->x]+mod[ii->x])%104659; + + } + + + +for(i=2;i<=n;i++) + + g< +#include +#include +#include +#include +const long double EPS = 1e-8; + +const int NMAX = 1505; + +const int MOD = 104659; + + + +using namespace std; + + + +long double D[NMAX]; + +int P[NMAX]; + +int T[NMAX]; + +vector A[NMAX]; + +vector C[NMAX]; + +int N, M; + +queue > Q; + + + +int comp(long double x, long double y) { + + double rez = x-y; + + if (rez < -EPS) + + return -1; + + if (rez > EPS) + + return 1; + + return 0; + +} + + + +void dijkstra() { + + D[1] = 0; + + for ( int i = 2; i <= N; i++) { + + D[i] = 250000; + + } + + Q.push(make_pair(1, (long double)0)); + + while (!Q.empty()) { + + int idx; double dist; + + idx = Q.front().first; + + dist = Q.front().second; + + Q.pop(); + + for ( int i = 0; i < A[idx].size(); i++) { + + if (comp(dist + C[idx][i], D[A[idx][i]]) < 0) { + + D[A[idx][i]] = C[idx][i] + dist; + + P[A[idx][i]] = 1; + + T[A[idx][i]] = idx; + + Q.push(make_pair(A[idx][i], D[A[idx][i]])); + + } else if (comp(dist + C[idx][i], D[A[idx][i]]) == 0) { + + P[A[idx][i]]++; + + + + } + + } + + } + +} + + + +int compute(int idx) { + + int sol = 1; + + while(idx != 1) { + + sol = (sol * P[idx])%MOD; + + idx = T[idx]; + + } + + return sol; + +} + + + +int main() { + + freopen("dmin.in", "r", stdin); + + freopen("dmin.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + + + for ( int i = 1, x, y, c; i <= M; i++) { + + scanf("%d %d %d", &x, &y, &c); + + A[x].push_back(y); C[x].push_back(log10l(c)); + + A[y].push_back(x); C[y].push_back(log10l(c)); + + } + + + + dijkstra(); + + + + for ( int i = 2; i <= N; i++) { + + printf("%d ", compute(i)); + + } + + printf("\n"); + + + + return 0; + +} diff --git a/distmin/djikstra/def2155a-3ad9-454b-8713-66e62bf0c990.cpp b/distmin/djikstra/def2155a-3ad9-454b-8713-66e62bf0c990.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00110459bff9b74049ac2f4d7652fc5c7e3bc166 --- /dev/null +++ b/distmin/djikstra/def2155a-3ad9-454b-8713-66e62bf0c990.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + +const int NMAX = 15010; + +const int MOD = 104659; + +const double EPS = 1e-10; + +int n, m; + +vector> graf[NMAX]; + +double dist[NMAX]; + +int counter[NMAX]; + +bool use[NMAX]; + +struct comp + +{ + + bool operator()(int a, int b) + + { + + return dist[a]>dist[b]; + + } + +}; + +void dij() + +{ + + priority_queue, comp> cue; + + cue.push(1); + + fill(dist, dist+NMAX, INT_MAX); + + dist[1] = 0; + + counter[1] = 1; + + use[1] = 1; + + while(cue.size()) + + { + + for(auto i:graf[cue.top()]) + + { + + double newLength = (double)dist[cue.top()]+ (double)i.second; + + if(dist[i.first]>newLength || (dist[i.first]newLength-EPS)) + + { + + if((dist[i.first]newLength-EPS)) + + { + + counter[i.first]=(counter[i.first]+counter[cue.top()])%MOD; + + } + + else + + { + + counter[i.first]=counter[cue.top()]%MOD; + + if(!use[i.first]) cue.push(i.first),use[i.first]=1; + + dist[i.first]=newLength; + + } + + } + + } + + use[cue.top()]=0; + + cue.pop(); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m--) + + { + + int a, b, c; + + fin>>a>>b>>c; + + graf[a].push_back({b,(double)log2((double)c)}); + + graf[b].push_back({a,(double)log2((double)c)}); + + } + + dij(); + + for(int i = 2;i<=n;i++) fout< +#include +#include +#include +typedef std :: pair < int, double > NOD; + +std :: ifstream f("dmin.in"); +std :: ofstream g("dmin.out"); + +int N, M, Count[1509]; +double D[1509]; + +std :: vector < NOD > V[1509]; +std :: set < NOD > H; + +inline void Read_Data() { + f >> N >> M; + + double cost; + for(int i = 1, x, y; i <= M; ++i) { + f >> x >> y >> cost; + cost = log(cost); + V[x].push_back(std :: make_pair(y, cost)); + V[y].push_back(std :: make_pair(x, cost)); + } +} + +inline double modul(double a) { + if(a < 0) a *= -1.0; + return a; +} + +void Djikstra() { + H.insert( {1, 0} ); + + while(!H.empty()) { + NOD nod = *H.begin(); + H.erase( *H.begin() ); + + for(auto val : V[nod.first]) + if(D[val.first] - val.second - nod.second > 1e-10) { + Count[val.first] = Count[nod.first] % 104659; + H.erase( {val.first, D[val.first]} ); + + D[val.first] = nod.second + val.second; + H.insert( {val.first, D[val.first]} ); + } else if(modul(D[val.first] - nod.second - val.second) < 1e-10) + Count[val.first] = (Count[val.first] + Count[nod.first] ) % 104659; + + } +} + +int main() { + Read_Data(); + + for(int i = 2; i <= N; ++i) D[i] = (1 << 30); + Count[1] = 1; + Djikstra(); + + for(int i = 2; i <= N; ++i) g << Count[i] << ' '; + + g.close(); + return 0; +} diff --git a/distmin/djikstra/e9388d02-d8c4-4155-86b8-68d5b1973d68.cpp b/distmin/djikstra/e9388d02-d8c4-4155-86b8-68d5b1973d68.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58a7079cbb56f4a9fabac670bf1bfdbb91f3aadf --- /dev/null +++ b/distmin/djikstra/e9388d02-d8c4-4155-86b8-68d5b1973d68.cpp @@ -0,0 +1,223 @@ +#include +#include using namespace std; +double make_abs(double x) + + { + + if(x>0)return x; + + return -x; + + } + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + +typedef struct _lista + + { + + int x; + + double c; + + _lista *urm; + + }*Lista; + +typedef struct _listan + + { + + int x; + + _listan *urm; + + }*Listan; + +typedef struct _nod + + { + + Lista l; + + }Nod; + +Nod V[1501]; + +Listan prim=NULL; + +double c[1501]; + +long mod[1501]; + +int v[1501],coada[1501],pi[1501],n,m,p,u; + +void adauga(int x,int y,int cost) + + { + + Lista nou=new _lista; + + nou->x=y;nou->c=cost; + + nou->urm=V[x].l; + + V[x].l=nou; + + } + +void citire() + + { + + int i,cost,x,y; + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>x>>y>>cost,adauga(x,y,cost),adauga(y,x,cost); + + } + +void getmin(int p,int u) + + { + + int i,aux; + + double min=c[coada[p]]; + + int minpoz=p; + + for(i=p+1;i<=u;i++) + + if(min>c[coada[i]])min=c[coada[i]],minpoz=i; + + aux=coada[p],coada[p]=coada[minpoz],coada[minpoz]=aux; + + } + +void dijkstra(int k) + + { + + Lista i; + + p=u=1;coada[1]=k; + + v[k]=1;c[k]=0; + + while(p<=u) + + { + + getmin(p,u); + + k=coada[p]; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + coada[++u]=i->x,v[i->x]=1,pi[i->x]=k, + + c[i->x]=c[k]+log(i->c); + + + + else + + if(c[i->x]>c[k]+log(i->c)) + + pi[i->x]=k,c[i->x]=c[k]+log(i->c); + + p++; + + } + + } + +void adauga(int x) + + { + + Listan nou=new _listan; + + nou->x=x; + + nou->urm=prim; + + prim=nou; + + } + +void df(int k) + + { + + v[k]=1; + + Lista i; + + for(i=V[k].l;i;i=i->urm) + + if(!v[i->x]) + + if(make_abs(c[k]+log(i->c)-c[i->x])<0.00001) + + df(i->x); + + adauga(k); + + } + +int main() + +{ + +int i; + +Lista j; + +citire();f.close(); + +dijkstra(1); + +for(i=1;i<=n;i++)v[i]=0; + +mod[1]=1; + +df(1); + +Listan ii; + +for(ii=prim;ii;ii=ii->urm) + + { + + + + for(j=V[ii->x].l;j;j=j->urm) + + if(make_abs(c[ii->x]+log(j->c)-c[j->x])<0.00001) + + mod[j->x]=(mod[j->x]+mod[ii->x])%104659; + + } + + + +for(i=2;i<=n;i++) + + g< +#include +#include +#include + + +int n, m, *leg[1510], numar[1510]; + +double *cost[1510]; + +double eps= 1e-10; + + + +void djikstra() + +{ + + int uz[1510], nr=0, min; + + double d[1510]; + + memset(uz, 0, sizeof(uz)); + + numar[1]=1; + + for(int i=0; i<=n; ++i) d[i]= 20000000; + + d[1]=0; + + + + while(nr-n) { + + min=0; + + for(int i=1; i<=n; ++i) if( !uz[i] && d[min] - d[i] >eps ) min=i; + + uz[min]= 1; + + nr++; + + for(int i=1; i<=leg[min][0]; ++i) + + if( d[ leg[min][i]] - d[min] - cost[min][i] >eps && min!=leg[min][i] && !uz[leg[min][i]]) { + + d[ leg[min][i]]= d[min] + cost[min][i]; + + numar[ leg[min][i]]= numar[min]; + + } + + else if( fabs( d[ leg[min][i]] - d[min] - cost[min][i]) +#include +#include +#include +#include +#include +using namespace std; + + + +class cmp { + +public : + + + + bool operator ()( pair < int , double > a , pair < int , double > b ){ + + return a.second > b.second ; + + } + + + +}; + + + + + +vector < pair < int , double > > muc [ 1510 ]; + +vector < pair < int , double > >::iterator it ; + +priority_queue < pair < int , double > , vector < pair < int , double > > , cmp > que ; + +int n ; + +double dist [ 1510 ] ; + +int nrd[ 1510 ]; + +int viz [ 1510 ] ; + + + +int comp ( double a , double b ){ + + if ( a - b < 1e-10 && a - b > -1e-10 ){ + + return 0 ; + + } + + + + if ( a < b ){ + + return -1; + + } + + return 1 ; + +} + + + +void djikstra ( ){ + + static int i , nod , temp ; + + + + for ( i = 2 ; i <= n ; i ++ ){ + + dist [ i ] = 1e7 ; + + } + + + + nrd [ 1 ] = 1 ; + + que.push( make_pair( 1 , 0 ) ); + + + + while ( !que.empty() ){ + + + + while ( !que.empty() && viz [ que.top().first ] == 1 ){ + + que.pop(); + + } + + + + if ( que.empty() ){ + + break ; + + } + + + + nod = que.top().first; + + que.pop() ; + + viz[ nod ] = 1; + + + + for ( it = muc [ nod ].begin() ; it != muc [ nod ].end() ; it++ ){ + + + + if ( viz [ it->first ] ){ + + continue ; + + } + + + + temp = comp ( dist [ it->first ] , dist [ nod ] + it->second ) ; + + if ( temp == 0 ){ + + nrd [ it -> first ] += nrd [ nod ]; + + }else if ( temp == 1 ){ + + dist [ it->first ] = dist [ nod ] + it->second ; + + nrd [ it->first ] = nrd [ nod ] ; + + que.push( make_pair( it->first , dist [ it->first ] ) ) ; + + + + } + + nrd [ it->first ] %= 104659 ; + + + + + + } + + + + } + +} + + + +int main(){ + + int m , x , y , cost , i ; + + + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for( i = 0 ; i < m ; i ++ ){ + + scanf("%d%d%d", &x , &y , &cost ); + + muc [ x ].push_back( make_pair( y , log( cost ) ) ) ; + + } + + + + djikstra( ); + + + + for ( i = 2 ; i <= n ; i ++ ){ + + printf("%d ",nrd [ i ] ) ; + + + + } + + + + return 0; + +} diff --git a/distmin/djikstra/efa43e29-0a75-4136-84c8-717472e1a149.cpp b/distmin/djikstra/efa43e29-0a75-4136-84c8-717472e1a149.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94b2a8a830b54c6dda7181a40fbbdb2d3278af0d --- /dev/null +++ b/distmin/djikstra/efa43e29-0a75-4136-84c8-717472e1a149.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +int n, m, in[1510], nrd[1510]; + +double D[1510]; + + + +vector A[1510]; + +vector C[1510]; + +queue Q; + + + + + +int main() + +{ + + int i, a, b, nod, vecin; + + double c, cost; + + fin >> n >> m; + + for (i = 1; i <= m; i++) + + { + + fin >> a >> b >> c; + + A[a].push_back(b); + + C[a].push_back(log10(c)); + + A[b].push_back(a); + + C[b].push_back(log10(c)); + + } + + for (i = 2; i <= n; i++) + + D[i] = 1000000000; + + nrd[1] = 1; + + Q.push(1); + + in[1] = 1; + + while (!Q.empty()) + + { + + nod = Q.front(); + + Q.pop(); + + in[nod] = 0; + + for (i = 0; i < A[nod].size(); i++) + + { + + vecin = A[nod][i]; + + cost = C[nod][i]; + + if (D[vecin] > D[nod] + cost + 0.0000000001) + + { + + D[vecin] = D[nod] + cost; + + nrd[vecin] = nrd[nod] % 104659; + + if (!in[vecin]) + + { + + in[vecin] = 1; + + Q.push(vecin); + + } + + } + + else if (abs(D[vecin] - D[nod] - cost) < 0.0000000001) + + nrd[vecin] = (nrd[vecin] % 104659 + nrd[nod] % 104659) % 104659; + + } + + } + + for (i = 2; i <= n; i++) + + fout << nrd[i] << ' '; + + fout << '\n'; + + fout.close(); + + return 0; + +} diff --git a/distmin/djikstra/f705ba28-5948-458f-8cf1-49b169504f30.cpp b/distmin/djikstra/f705ba28-5948-458f-8cf1-49b169504f30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e0b3b9d687eaeb2ac7d091f45ea5d0b6e59749c --- /dev/null +++ b/distmin/djikstra/f705ba28-5948-458f-8cf1-49b169504f30.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +fstream in ( "dmin.in" , ios::in ); +fstream out( "dmin.out", ios::out ); + +const int DIM = 1505; +const int MOD = 104659; +const int INF = 0x3f3f3f3f; +const long double EPS = 1e-10; + +int cnt[DIM]; +long double dst[DIM]; + +vector> edg[DIM]; +priority_queue< pair, + vector >, + greater>> prq; + +inline int sgn( long double x ) { + if( fabs( x ) < EPS ) + return 0; + return ( x < 0 ) ? -1 : 1; +} + +int main( void ) { + ios::sync_with_stdio( false ); + + int n, m; + in >> n >> m; + + for( int i = 1; i <= m; i ++ ) { + int x, y, z; + in >> x >> y >> z; + + edg[x].push_back( make_pair( log10( z ), y ) ); + edg[y].push_back( make_pair( log10( z ), x ) ); + } + + fill( dst + 2, dst + n + 1, INF ); cnt[1] = 1; + for( prq.push( make_pair( 0, 1 ) ); !prq.empty(); prq.pop() ) { + pair x = prq.top(); + + if( sgn( x.first - dst[x.second] ) > 0 ) + continue; + + for( pair y : edg[x.second] ) { + if( sgn( dst[y.second] - ( dst[x.second] + y.first ) ) > 0 ) { + dst[y.second] = dst[x.second] + y.first; + cnt[y.second] = cnt[x.second]; + + prq.push( make_pair( dst[y.second], y.second ) ); + } + else { + if( sgn( dst[y.second] - ( dst[x.second] + y.first ) ) == 0 ) + cnt[y.second] = ( cnt[y.second] + cnt[x.second] ) % MOD; + } + } + } + + for( int i = 2; i <= n; i ++ ) + out << cnt[i] << " "; + out << endl; + + return 0; +} diff --git a/distmin/djikstra/f97a1c0d-c220-4891-8f98-8146fa5e98b6.cpp b/distmin/djikstra/f97a1c0d-c220-4891-8f98-8146fa5e98b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f2e79df3141629833c858dd6b5183cb3c199c98 --- /dev/null +++ b/distmin/djikstra/f97a1c0d-c220-4891-8f98-8146fa5e98b6.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +using namespace std; +typedef pair ii; +typedef pair iii; + +int n,m,i,px1,py1,P[2000]; +double cst,D[2000]; +vector e[2000]; + +inline double absol(double x) { + return x > 0 ? x : -x; +} + +inline int egale(double x,double y) { + return absol(x - y) <= 1e-6; +} + +void dijkstra() { + for(i = 1; i <= n; i++) D[i] = P[i] = (int)1e9; + D[1] = 0; P[1] = 1; + priority_queue < iii,vector,greater > Q; + Q.push(iii(0,1)); + while(!Q.empty()) { + int nod = Q.top().second; + double cs = Q.top().first; + Q.pop(); + if(cs <= D[nod]) { + for(i = 0; i < (int)e[nod].size(); i++) + if(egale(D[e[nod][i].first],D[nod] + e[nod][i].second)) { + P[e[nod][i].first] += P[nod]; + if(P[e[nod][i].first] >= 104659) P[e[nod][i].first] -= 104659; + } + else if(D[e[nod][i].first] > D[nod] + e[nod][i].second) { + D[e[nod][i].first] = D[nod] + e[nod][i].second; + P[e[nod][i].first] = P[nod]; + Q.push(iii(D[e[nod][i].first],e[nod][i].first)); + } + } + } +} + +int main() { + freopen("dmin.in","r",stdin); + freopen("dmin.out","w",stdout); + + scanf("%d%d",&n,&m); + for(i = 1; i <= m; i++) { + scanf("%d%d%lf",&px1,&py1,&cst); + cst = log(cst); + e[px1].push_back(make_pair(py1,cst)); + e[py1].push_back(make_pair(px1,cst)); + } + + dijkstra(); + + for(i = 2; i <= n; i++) printf("%d ",P[i]); + printf("\n"); + + return 0; +} diff --git a/distmin/djikstra/fa99f1e2-61cd-4957-bbfa-0ad0bb15ea79.cpp b/distmin/djikstra/fa99f1e2-61cd-4957-bbfa-0ad0bb15ea79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f1b09a84f2890d19e767f328b1ff215b3b675b8 --- /dev/null +++ b/distmin/djikstra/fa99f1e2-61cd-4957-bbfa-0ad0bb15ea79.cpp @@ -0,0 +1,188 @@ +#include +#include +#include +using namespace std; + + + +int v[3][5010]; + +double C[5010]; + +int n,m; + +struct s{ + + double cost; + + int drum; + +} a[1510]; + + + + + + + +void dijkstra() + +{ + + int i,t; + + for (i=1; i<=n; i++) a[i].cost=-1; + + a[1].cost=0; t=1; + + while (t) + + { + + t=0; + + for (i=1; i<=m*2; i++) + + if (a[v[1][i]].cost!=-1 && (a[v[2][i]].cost==-1 || a[v[2][i]].cost-a[v[1][i]].cost-C[i]>0.0000000001)) + + { + + a[v[2][i]].cost=a[v[1][i]].cost+C[i]; + + t=1; + + + + } + + } + + + + + + for (i=1; i<=n; i++) a[i].drum=0; a[1].drum=1; + + for (i=1; i<=m*2; i++) + + if ( abs(a[v[2][i]].cost-a[v[1][i]].cost-C[i])<=0.0000000001) + + { + + a[v[2][i]].drum=(a[v[2][i]].drum+a[v[1][i]].drum)%104659; + + } + +} + +void sw(int *a,int *b) + +{ + + int t=*a; *a=*b; *b=t; + +} + + + +void sw1(double *a,double *b) + +{ + + double t=*a; *a=*b; *b=t; + +} + + + +void qs(int left,int right) + +{ + + int i,j,p; + + i=left; j=right; p=v[1][(i+j)/2]; + + while (ip) j--; + + if (i<=j) + + { + + sw(&v[1][i],&v[1][j]); + + sw(&v[2][i],&v[2][j]); + + sw1(&C[i],&C[j]); + + i++; + + j--; + + } + + } + + if (ileft) qs(left,j); + +} + + + +int main() + +{ + + int i,c; + + freopen("dmin.in","r",stdin); + + freopen("dmin.out","w",stdout); + + scanf("%i%i",&n,&m); + + for (i=1; i<=m*2; i++) + + if (i%2==1) + + { + + scanf("%i%i%i",&v[1][i],&v[2][i],&c); + + C[i]=log(c); + + + + } + + else{ + + v[1][i]=v[2][i-1]; + + v[2][i]=v[1][i-1]; + + C[i]=C[i-1]; + + } + + qs(1,m*2); + + + + + + dijkstra(); + + for (i=2; i<=n; i++) printf("%i ",a[i].drum); + + return 0; + +} diff --git a/distmin/djikstra/fe999119-02b6-11ec-960d-2cf05d84bfc2.cpp b/distmin/djikstra/fe999119-02b6-11ec-960d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75eeaaef7ace4c09186a42c306c957722600be33 --- /dev/null +++ b/distmin/djikstra/fe999119-02b6-11ec-960d-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + + + +const double mini = 0.000001; + + + +const int MOD = 104659; + + + +long long n,m,ans[1505]; + +double dist[1505]; + + + +struct elem + +{ + + long long vecin; + + double cost; + + bool operator<(const elem &other)const + + { + + return cost>other.cost; + + } + +}; + + + +priority_queuepq; + +vectorg[1505]; + + + +void dijkstra(int nod) + +{ + + pq.push({1,0.0}); + + ans[1]=1; + + dist[1]=0; + + while(pq.size()) + + { + + int nod=pq.top().vecin; + + double distanta=pq.top().cost; + + pq.pop(); + + if(dist[nod]==distanta) + + { + + for(int i=0; imini) + + { + + dist[vec]=distanta+val; + + pq.push({vec,distanta+val}); + + ans[vec]=ans[nod]; + + } + + else if(abs(dist[vec]-(distanta+val))<=mini) + + { + + ans[vec]=(ans[vec]+ans[nod])%MOD; + + } + + } + + } + + } + +} + + + +int main() + +{ + + long long x,y,z; + + fin>>n>>m; + + for(int i=1; i<=n; i++) + + { + + dist[i]=INT_MAX; + + } + + for(int i=1; i<=m; i++) + + { + + fin>>x>>y>>z; + + g[x].push_back({y,log2((double)z)}); + + g[y].push_back({x,log2((double)z)}); + + } + + dijkstra(1); + + for(int i=2; i<=n; i++) + + fout< + + + + + + +using namespace std; + +using ld = long double; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int mod = 104659; + +const int NMAX = 2048; + +const ld EPS = 1e-6; + +int N, M; + +pair dp[NMAX]; + +vector> G[NMAX]; + +priority_queue,vector>,greater>> Q; + + + +void add_self(int &a, int b) { + + a += b; + + if(a >= mod) + + a -= mod; + +} + + + +void Dijkstra() { + + for(int u = 1; u <= N; ++u) + + dp[u] = make_pair(0x3f3f3f3f, 0); + + dp[1] = make_pair(0, 1); + + Q.push(make_pair(0, 1)); + + while(!Q.empty()) { + + int u = Q.top().second; + + ld d = Q.top().first; + + Q.pop(); + + if(((d - dp[u].first) >= 0 ? (d - dp[u].first) : -(d - dp[u].first)) > EPS) + + continue; + + for(const auto &v : G[u]) { + + ld best = d + v.second; + + if(dp[v.first].first - best > EPS) { + + dp[v.first].first = best; + + dp[v.first].second = dp[u].second; + + Q.push(make_pair(dp[v.first].first, v.first)); + + } + + else + + if(((dp[v.first].first - best) >= 0 ? (dp[v.first].first - best) : -(dp[v.first].first - best)) <= EPS) + + add_self(dp[v.first].second, dp[u].second); + + } + + } + +} + + + +int main() { + + fin >> N >> M; + + for(int i = 0; i < M; ++i) { + + int u, v; + + ld w; + + fin >> u >> v >> w; + + w = log2(w); + + G[u].emplace_back(v, w); + + G[v].emplace_back(u, w); + + } + + Dijkstra(); + + for(int u = 2; u <= N; ++u) + + fout << dp[u].second << ' '; + + fout << '\n'; + +} diff --git a/distmin/djikstra/fec9fca4-02b6-11ec-a71f-2cf05d84bfc2.cpp b/distmin/djikstra/fec9fca4-02b6-11ec-a71f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47b7aba5a3b2c11aef12abfc880022037466d7eb --- /dev/null +++ b/distmin/djikstra/fec9fca4-02b6-11ec-a71f-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include + + + + + + +using namespace std; + +using ld = long double; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int mod = 104659; + +const int NMAX = 2048; + +const ld EPS = 1e-6; + +int N, M; + +pair dp[NMAX]; + +vector> G[NMAX]; + +priority_queue,vector>,greater>> Q; + + + +void add_self(int &a, int b) { + + a += b; + + if(a >= mod) + + a -= mod; + +} + + + +void Dijkstra() { + + for(int u = 1; u <= N; ++u) + + dp[u] = make_pair(1.0 * 0x3f3f3f3f, 0); + + dp[1] = make_pair(0.0, 1); + + Q.push(make_pair(0.0, 1)); + + while(!Q.empty()) { + + int u = Q.top().second; + + ld d = Q.top().first; + + Q.pop(); + + if(((d - dp[u].first) >= 0 ? (d - dp[u].first) : -(d - dp[u].first)) > EPS) + + continue; + + for(const auto &v : G[u]) { + + ld best = d + v.second; + + if((dp[v.first].first - best) > EPS) { + + dp[v.first].first = best; + + dp[v.first].second = dp[u].second; + + Q.push(make_pair(dp[v.first].first, v.first)); + + } + + else + + if(((dp[v.first].first - best) >= 0 ? (dp[v.first].first - best) : -(dp[v.first].first - best)) <= EPS) + + add_self(dp[v.first].second, dp[u].second); + + } + + } + +} + + + +int main() { + + fin >> N >> M; + + for(int i = 0; i < M; ++i) { + + int u, v, w; + + fin >> u >> v >> w; + + G[u].emplace_back(v, log2((ld)w)); + + G[v].emplace_back(u, log2((ld)w)); + + } + + Dijkstra(); + + for(int u = 2; u <= N; ++u) + + fout << dp[u].second << ' '; + + fout << '\n'; + +} diff --git a/distmin/djikstra/fee5f890-02b6-11ec-8d5f-2cf05d84bfc2.cpp b/distmin/djikstra/fee5f890-02b6-11ec-8d5f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75eeaaef7ace4c09186a42c306c957722600be33 --- /dev/null +++ b/distmin/djikstra/fee5f890-02b6-11ec-8d5f-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + + + +const double mini = 0.000001; + + + +const int MOD = 104659; + + + +long long n,m,ans[1505]; + +double dist[1505]; + + + +struct elem + +{ + + long long vecin; + + double cost; + + bool operator<(const elem &other)const + + { + + return cost>other.cost; + + } + +}; + + + +priority_queuepq; + +vectorg[1505]; + + + +void dijkstra(int nod) + +{ + + pq.push({1,0.0}); + + ans[1]=1; + + dist[1]=0; + + while(pq.size()) + + { + + int nod=pq.top().vecin; + + double distanta=pq.top().cost; + + pq.pop(); + + if(dist[nod]==distanta) + + { + + for(int i=0; imini) + + { + + dist[vec]=distanta+val; + + pq.push({vec,distanta+val}); + + ans[vec]=ans[nod]; + + } + + else if(abs(dist[vec]-(distanta+val))<=mini) + + { + + ans[vec]=(ans[vec]+ans[nod])%MOD; + + } + + } + + } + + } + +} + + + +int main() + +{ + + long long x,y,z; + + fin>>n>>m; + + for(int i=1; i<=n; i++) + + { + + dist[i]=INT_MAX; + + } + + for(int i=1; i<=m; i++) + + { + + fin>>x>>y>>z; + + g[x].push_back({y,log2((double)z)}); + + g[y].push_back({x,log2((double)z)}); + + } + + dijkstra(1); + + for(int i=2; i<=n; i++) + + fout< +#include +#include +#include +#include +using namespace std; + + + +double EPSILON = 0.000001; + +const int Inf = 1e9 + 1; + +int n, m; + +vector> a[1501]; + +double d[1501]; + +int nrways[1501]; + +const int MOD = 104659; + + + +void read() { + + int x, y, i; + + double c; + + ifstream f("dmin.in"); + + f >> n >> m; + + for (i = 1; i <= m; i++) { + + f >> x >> y >> c; + + a[x].emplace_back(log2(c), y); + + a[y].emplace_back(log2(c), x); + + } + + f.close(); + +} + + + +priority_queue, vector>, greater>> Q; + +void dijkstra(int x) { + + pair p, cur; + + int i; + + d[x] = 0; + + nrways[1] = 1; + + Q.push(make_pair(0, x)); + + while (!Q.empty()) { + + p = Q.top(); + + Q.pop(); + + if (p.first != d[p.second]) continue; + + for (i = 0; i < a[p.second].size(); i++) { + + cur = a[p.second][i]; + + if(d[p.second] + cur.first - d[cur.second] < -EPSILON) { + + d[cur.second] = d[p.second] + cur.first; + + Q.push(make_pair(d[cur.second], cur.second)); + + nrways[cur.second] = nrways[p.second]; + + } + + else if(abs(d[p.second] + cur.first - d[cur.second]) < EPSILON) + + nrways[cur.second] = (nrways[cur.second] + nrways[p.second]) % MOD; + + } + + } + +} + + + +void solve() { + + int i; + + for (i = 1; i <= n; i++) + + d[i] = Inf; + + dijkstra(1); + +} + + + +void output() { + + ofstream g("dmin.out"); + + int i, j; + + for (i = 2; i <= n; i++) + + g << nrways[i] << ' '; + + g.close(); + +} + + + +int main() { + + read(); + + solve(); + + output(); + + return 0; + +} diff --git a/distmin/djikstra/ff26a28a-02b6-11ec-b99d-2cf05d84bfc2.cpp b/distmin/djikstra/ff26a28a-02b6-11ec-b99d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..407cc641b5d36d6991b11429fe9c5ae345381c34 --- /dev/null +++ b/distmin/djikstra/ff26a28a-02b6-11ec-b99d-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include + + + + + + +using namespace std; + +using ld = long double; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int mod = 104659; + +const int NMAX = 2048; + +const ld EPS = 1e-6; + +int N, M; + +pair dp[NMAX]; + +vector> G[NMAX]; + +priority_queue,vector>,greater>> Q; + + + +void add_self(int &a, int b) { + + a += b; + + if(a >= mod) + + a -= mod; + +} + + + +void Dijkstra() { + + for(int u = 1; u <= N; ++u) + + dp[u] = make_pair(1.0 * 0x3f3f3f3f, 0); + + dp[1] = make_pair(0.0, 1); + + Q.push(make_pair(0.0, 1)); + + while(!Q.empty()) { + + int u = Q.top().second; + + ld d = Q.top().first; + + Q.pop(); + + if(((d - dp[u].first) >= 0 ? (d - dp[u].first) : -(d - dp[u].first)) > EPS) + + continue; + + for(const auto &v : G[u]) { + + ld best = d + v.second; + + if((dp[v.first].first - best) > EPS) { + + dp[v.first].first = best; + + dp[v.first].second = dp[u].second; + + Q.push(make_pair(dp[v.first].first, v.first)); + + } + + else + + if(((dp[v.first].first - best) >= 0 ? (dp[v.first].first - best) : -(dp[v.first].first - best)) <= EPS) + + add_self(dp[v.first].second, dp[u].second); + + } + + } + +} + + + +int main() { + + fin >> N >> M; + + for(int i = 0; i < M; ++i) { + + int u, v; + + ld w; + + fin >> u >> v >> w; + + w = log2(w); + + G[u].emplace_back(v, w); + + G[v].emplace_back(u, w); + + } + + Dijkstra(); + + for(int u = 2; u <= N; ++u) + + fout << dp[u].second << ' '; + + fout << '\n'; + +} diff --git a/distmin/djikstra/ff429be0-02b6-11ec-9c77-2cf05d84bfc2.cpp b/distmin/djikstra/ff429be0-02b6-11ec-9c77-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c7a29a08e0304d571a9f98b74d04597f4f0c71b --- /dev/null +++ b/distmin/djikstra/ff429be0-02b6-11ec-9c77-2cf05d84bfc2.cpp @@ -0,0 +1,118 @@ +#include + + + + + + +using namespace std; + +using ld = long double; + + + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const int mod = 104659; + +const int NMAX = 2048; + +const ld EPS = 0.000001; + +int N, M; + +pair dp[NMAX]; + +vector> G[NMAX]; + +priority_queue,vector>,greater>> Q; + + + +void add_self(int &a, int b) { + + a += b; + + if(a >= mod) + + a -= mod; + +} + + + +void Dijkstra() { + + for(int u = 1; u <= N; ++u) + + dp[u] = make_pair(1.0 * 0x3f3f3f3f, 0); + + dp[1] = make_pair(0.0, 1); + + Q.push(make_pair(0.0, 1)); + + while(!Q.empty()) { + + int u = Q.top().second; + + ld d = Q.top().first; + + Q.pop(); + + if(((d - dp[u].first) >= 0 ? (d - dp[u].first) : -(d - dp[u].first)) > EPS) + + continue; + + for(const auto &v : G[u]) + + if((dp[v.first].first - (dp[u].first + v.second)) > EPS) { + + dp[v.first].first = dp[u].first + v.second; + + dp[v.first].second = dp[u].second; + + Q.push(make_pair(dp[v.first].first, v.first)); + + } + + else + + if(((dp[v.first].first - (dp[u].first+ v.second)) >= 0 ? (dp[v.first].first - (dp[u].first+ v.second)) : -(dp[v.first].first - (dp[u].first+ v.second))) <= EPS) + + add_self(dp[v.first].second, dp[u].second); + + } + +} + + + +int main() { + + fin >> N >> M; + + for(int i = 0; i < M; ++i) { + + int u, v, w; + + fin >> u >> v >> w; + + G[u].emplace_back(v, log2((ld)w)); + + G[v].emplace_back(u, log2((ld)w)); + + } + + Dijkstra(); + + for(int u = 2; u <= N; ++u) + + fout << dp[u].second << ' '; + + fout << '\n'; + +} diff --git a/distmin/djikstra/ff7dfaea-02b6-11ec-a11d-2cf05d84bfc2.cpp b/distmin/djikstra/ff7dfaea-02b6-11ec-a11d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73b03c499c803153795257f3eac0393819859243 --- /dev/null +++ b/distmin/djikstra/ff7dfaea-02b6-11ec-a11d-2cf05d84bfc2.cpp @@ -0,0 +1,102 @@ +#include + + + + + + +using namespace std; + +ifstream fi("dmin.in"); +ofstream fo("dmin.out"); + +struct HeapNode{ + int nod; + double val; + bool operator< (const HeapNode &other) const{ + return val>other.val; + } +}; + +int n,m,x,y; +double c; +vector> G[1500 +5]; +priority_queue pq; +double dist[1500 +5]; +int nrdrm[1500 +5]; +int viz[1500 +5]; + +void dijkstra(){ + for(int i=2;i<=n;i++){ + dist[i]=(1LL<<31)-1; + } + dist[1]=0; + HeapNode x; + int nod,nod2; + double val,val2; + pq.push({1,0}); + while(!pq.empty()){ + x=pq.top(); + pq.pop(); + nod=x.nod; + val=x.val; + if(dist[nod]dist[nod]+val2){ + dist[nod2]=dist[nod]+val2; + pq.push({nod2,dist[nod2]}); + } + } + } +} + +void calc_nrdrm(){ + for(int i=2;i<=n;i++){ + nrdrm[i]=0; + } + nrdrm[1]=1; + pq.push({1,0}); + viz[1]=1; + HeapNode x; + int nod,nod2; + double val,val2; + while(!pq.empty()){ + x=pq.top(); + pq.pop(); + nod=x.nod; + val=x.val; + for(int i=0;i>n>>m; + for(int i=1;i<=m;i++){ + fi>>x>>y>>c; + c=log(c); + G[x].push_back(make_pair(y,c)); + G[y].push_back(make_pair(x,c)); + } + dijkstra(); + calc_nrdrm(); + for(int i=2;i<=n;i++){ + fo< +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + + + +const double mini = 0.000001; + + + +const int MOD = 104659; + + + +long long n,m,ans[1505]; + +double dist[1505]; + + + +struct elem + +{ + + long long vecin; + + double cost; + + bool operator<(const elem &other)const + + { + + return cost>other.cost; + + } + +}; + + + +priority_queuepq; + +vectorg[1505]; + + + +void dijkstra(int nod) + +{ + + pq.push({1,0.0}); + + ans[1]=1; + + dist[1]=0; + + while(pq.size()) + + { + + int nod=pq.top().vecin; + + double distanta=pq.top().cost; + + pq.pop(); + + if(dist[nod]==distanta) + + { + + for(int i=0; imini) + + { + + dist[vec]=distanta+val; + + pq.push({vec,distanta+val}); + + ans[vec]=ans[nod]; + + } + + else if(abs(dist[vec]-(distanta+val))<=mini) + + { + + ans[vec]=(ans[vec]+ans[nod])%MOD; + + } + + } + + } + + } + +} + + + +int main() + +{ + + long long x,y,z; + + fin>>n>>m; + + for(int i=1; i<=n; i++) + + { + + dist[i]=INT_MAX; + + } + + for(int i=1; i<=m; i++) + + { + + fin>>x>>y>>z; + + g[x].push_back({y,log2((double)z)}); + + g[y].push_back({x,log2((double)z)}); + + } + + dijkstra(1); + + for(int i=2; i<=n; i++) + + fout< +using namespace std; + +ifstream fin("dmin.in"); + +ofstream fout("dmin.out"); + + + +const double mini = 1e-6; + + + +const int MOD = 104659; + + + +long long n,m,ans[1505]; + +double dist[1505]; + + + +struct elem + +{ + + long long vecin; + + double cost; + + bool operator<(const elem &other)const + + { + + return cost>other.cost; + + } + +}; + + + +priority_queuepq; + +vectorg[1505]; + + + +void dijkstra(int nod) + +{ + + pq.push({1,0.0}); + + ans[1]=1; + + dist[1]=0; + + while(pq.size()) + + { + + int nod=pq.top().vecin; + + double distanta=pq.top().cost; + + pq.pop(); + + if(dist[nod]==distanta) + + { + + for(int i=0; imini) + + { + + dist[vec]=distanta+val; + + pq.push({vec,distanta+val}); + + ans[vec]=ans[nod]; + + } + + else if(abs(dist[vec]-(distanta+val))<=mini) + + { + + ans[vec]=(ans[vec]+ans[nod])%MOD; + + } + + } + + } + + } + +} + + + +int main() + +{ + + long long x,y,z; + + fin>>n>>m; + + for(int i=1; i<=n; i++) + + { + + dist[i]=INT_MAX; + + } + + for(int i=1; i<=m; i++) + + { + + fin>>x>>y>>z; + + g[x].push_back({y,log2((double)z)}); + + g[y].push_back({x,log2((double)z)}); + + } + + dijkstra(1); + + for(int i=2; i<=n; i++) + + fout< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + + + +vector > graf[1505]; + +int n, m; + +bool inQ[1505]; + +double d[1505]; + +int nrD[1505]; + +priority_queue, vector >, greater > > Q; + + + +void read() + +{ + + f>>n>>m; + + int x, y; + + double c; + + for(int i = 0; i>x>>y>>c; + + graf[x].push_back({y, log(c)}); + + graf[y].push_back({x, log(c)}); + + } + +} + + + +void init() + +{ + + for(int i = 2; i<=n; i++) + + { + + d[i] = 0x3F3F3F3F; + + } + + d[1] = log(1); + + nrD[1] = 1; + +} + + + +void dijkstra() + +{ + + Q.push({d[1], 1}); + + inQ[1] = true; + + while(!Q.empty()) + + { + + int node = Q.top().second; + + double dd = Q.top().first; + + if(abs(d[node] - Q.top().first) > 0.000001) + + { + + Q.pop(); + + continue; + + } + + Q.pop(); + + for(auto &p : graf[node]) + + { + + if(d[node] + p.second - d[p.first] < -0.000001) + + { + + d[p.first] = d[node] + p.second; + + nrD[p.first] = nrD[node]; + + Q.push({d[p.first], p.first}); + + } + + else if(abs(d[node] + p.second - d[p.first]) < 0.000001) + + { + + nrD[p.first] = (nrD[p.first] + nrD[node])%104659; + + } + + } + + } + +} + + + +int main() + +{ + + read(); + + init(); + + dijkstra(); + + for(int i = 2; i<=n; i++) + + { + + if(d[i] != 0x3F3F3F3F) + + g< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("dmin.in"); + +ofstream g("dmin.out"); + + + + + +vector > graf[1505]; + +int n, m; + +bool inQ[1505]; + +double d[1505]; + +int nrD[1505]; + +priority_queue, vector >, greater > > Q; + + + +void read() + +{ + + f>>n>>m; + + int x, y; + + double c; + + for(int i = 0; i>x>>y>>c; + + graf[x].push_back({y, log(c)}); + + graf[y].push_back({x, log(c)}); + + } + +} + + + +void init() + +{ + + for(int i = 2; i<=n; i++) + + { + + d[i] = 0x3F3F3F3F; + + } + + d[1] = log(1); + + nrD[1] = 1; + +} + + + +void dijkstra() + +{ + + Q.push({d[1], 1}); + + inQ[1] = true; + + while(!Q.empty()) + + { + + int node = Q.top().second; + + double dd = Q.top().first; + + if(abs(d[node] - Q.top().first) > 0.000001) + + { + + Q.pop(); + + continue; + + } + + Q.pop(); + + for(auto &p : graf[node]) + + { + + if(d[node] + p.second - d[p.first] < -0.000001) + + { + + d[p.first] = d[node] + p.second; + + nrD[p.first] = nrD[node]; + + Q.push({d[p.first], p.first}); + + } + + else if(abs(d[node] + p.second - d[p.first]) < 0.000001) + + { + + nrD[p.first] = (nrD[p.first] + nrD[node])%104659; + + } + + } + + } + +} + + + +int main() + +{ + + read(); + + init(); + + dijkstra(); + + for(int i = 2; i<=n; i++) + + { + + if(d[i] != 0x3F3F3F3F) + + g< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/00694475-2c27-4ad7-8fef-9b669458e081.cpp b/eval/ast/00694475-2c27-4ad7-8fef-9b669458e081.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/00694475-2c27-4ad7-8fef-9b669458e081.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/00d34a13-e0f1-4275-bbab-c093c345ec83.cpp b/eval/ast/00d34a13-e0f1-4275-bbab-c093c345ec83.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/00d34a13-e0f1-4275-bbab-c093c345ec83.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/018c0d38-8fc4-498e-b9d1-a52adf4f85df.cpp b/eval/ast/018c0d38-8fc4-498e-b9d1-a52adf4f85df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/018c0d38-8fc4-498e-b9d1-a52adf4f85df.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/029a28bb-d5ef-4cd5-859e-f6cbf875a1fb.cpp b/eval/ast/029a28bb-d5ef-4cd5-859e-f6cbf875a1fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/029a28bb-d5ef-4cd5-859e-f6cbf875a1fb.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/0347cce0-c693-4edf-b201-753fd7f6a849.cpp b/eval/ast/0347cce0-c693-4edf-b201-753fd7f6a849.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/0347cce0-c693-4edf-b201-753fd7f6a849.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/0401a8e4-072d-4776-b25a-884362deb0e4.cpp b/eval/ast/0401a8e4-072d-4776-b25a-884362deb0e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/0401a8e4-072d-4776-b25a-884362deb0e4.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/05cf533a-ca57-4c9b-84f0-3bdaea18d738.cpp b/eval/ast/05cf533a-ca57-4c9b-84f0-3bdaea18d738.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/05cf533a-ca57-4c9b-84f0-3bdaea18d738.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/0708684b-8c3b-4ffa-9500-371c51f98be6.cpp b/eval/ast/0708684b-8c3b-4ffa-9500-371c51f98be6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/0708684b-8c3b-4ffa-9500-371c51f98be6.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/073d0294-8e95-42f8-bf65-561bb97e9f50.cpp b/eval/ast/073d0294-8e95-42f8-bf65-561bb97e9f50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/073d0294-8e95-42f8-bf65-561bb97e9f50.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/075893cb-71d4-4e2e-988b-01a9193d5421.cpp b/eval/ast/075893cb-71d4-4e2e-988b-01a9193d5421.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/075893cb-71d4-4e2e-988b-01a9193d5421.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/081c68d5-1a9a-4bad-a84d-386e5e980227.cpp b/eval/ast/081c68d5-1a9a-4bad-a84d-386e5e980227.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/081c68d5-1a9a-4bad-a84d-386e5e980227.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/08e483d4-4221-48a8-ba9b-36bacb10dfe4.cpp b/eval/ast/08e483d4-4221-48a8-ba9b-36bacb10dfe4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/08e483d4-4221-48a8-ba9b-36bacb10dfe4.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/0b2f2ff4-845f-48cb-afa6-d393fe3f359d.cpp b/eval/ast/0b2f2ff4-845f-48cb-afa6-d393fe3f359d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/0b2f2ff4-845f-48cb-afa6-d393fe3f359d.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/0c1638e2-1790-4f8a-b831-576540effa08.cpp b/eval/ast/0c1638e2-1790-4f8a-b831-576540effa08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/0c1638e2-1790-4f8a-b831-576540effa08.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/0f9995cc-a5a7-4d29-837c-bcef6ce2909e.cpp b/eval/ast/0f9995cc-a5a7-4d29-837c-bcef6ce2909e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/0f9995cc-a5a7-4d29-837c-bcef6ce2909e.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/1171775c-4207-4f5c-8ce3-139c219d3dc6.cpp b/eval/ast/1171775c-4207-4f5c-8ce3-139c219d3dc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/1171775c-4207-4f5c-8ce3-139c219d3dc6.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/122df11d-e880-4b39-9981-fb7c91bcc099.cpp b/eval/ast/122df11d-e880-4b39-9981-fb7c91bcc099.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/122df11d-e880-4b39-9981-fb7c91bcc099.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/136621ec-8df9-48a0-9015-d755f18f2661.cpp b/eval/ast/136621ec-8df9-48a0-9015-d755f18f2661.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/136621ec-8df9-48a0-9015-d755f18f2661.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/142b730d-1bed-4e3f-9031-ab9cd1fc14a4.cpp b/eval/ast/142b730d-1bed-4e3f-9031-ab9cd1fc14a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/142b730d-1bed-4e3f-9031-ab9cd1fc14a4.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/16d6f45a-870c-4904-8600-870d63825a8b.cpp b/eval/ast/16d6f45a-870c-4904-8600-870d63825a8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/16d6f45a-870c-4904-8600-870d63825a8b.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/18b4a1c5-ae50-4d39-9431-ff4e9de62f76.cpp b/eval/ast/18b4a1c5-ae50-4d39-9431-ff4e9de62f76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/18b4a1c5-ae50-4d39-9431-ff4e9de62f76.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/1927df4b-38df-4f97-87d5-2f3ce7ab9cda.cpp b/eval/ast/1927df4b-38df-4f97-87d5-2f3ce7ab9cda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/1927df4b-38df-4f97-87d5-2f3ce7ab9cda.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/1a9c96fc-099c-4485-adad-210d06ac0df8.cpp b/eval/ast/1a9c96fc-099c-4485-adad-210d06ac0df8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/1a9c96fc-099c-4485-adad-210d06ac0df8.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/1cbb248d-3041-40c2-8f5a-786bffa9fde8.cpp b/eval/ast/1cbb248d-3041-40c2-8f5a-786bffa9fde8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/1cbb248d-3041-40c2-8f5a-786bffa9fde8.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/1d509aeb-2604-4740-a7c8-1f92acc135c9.cpp b/eval/ast/1d509aeb-2604-4740-a7c8-1f92acc135c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/1d509aeb-2604-4740-a7c8-1f92acc135c9.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/1db1cffd-d6ad-4f04-9851-d7df1929faaf.cpp b/eval/ast/1db1cffd-d6ad-4f04-9851-d7df1929faaf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/1db1cffd-d6ad-4f04-9851-d7df1929faaf.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/1eb0cc5e-9491-46c8-a635-fb1182888607.cpp b/eval/ast/1eb0cc5e-9491-46c8-a635-fb1182888607.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/1eb0cc5e-9491-46c8-a635-fb1182888607.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/1f266088-0a3f-4fb2-bd9a-699e0f2b8293.cpp b/eval/ast/1f266088-0a3f-4fb2-bd9a-699e0f2b8293.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/1f266088-0a3f-4fb2-bd9a-699e0f2b8293.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/20022142-d285-4f93-86d2-97b81a6cf024.cpp b/eval/ast/20022142-d285-4f93-86d2-97b81a6cf024.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/20022142-d285-4f93-86d2-97b81a6cf024.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/211d545f-11c6-485e-86ef-8149f21b21c7.cpp b/eval/ast/211d545f-11c6-485e-86ef-8149f21b21c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/211d545f-11c6-485e-86ef-8149f21b21c7.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/23bdedcb-465b-469f-b69c-071e22028d2b.cpp b/eval/ast/23bdedcb-465b-469f-b69c-071e22028d2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/23bdedcb-465b-469f-b69c-071e22028d2b.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/24273e92-ceed-44ce-87c0-b9e5a32ded5c.cpp b/eval/ast/24273e92-ceed-44ce-87c0-b9e5a32ded5c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/24273e92-ceed-44ce-87c0-b9e5a32ded5c.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/244c340d-0a04-4552-93c5-e564fccf3133.cpp b/eval/ast/244c340d-0a04-4552-93c5-e564fccf3133.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/244c340d-0a04-4552-93c5-e564fccf3133.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/25733f88-ba7a-4ed4-9f0e-c38d3994f7c1.cpp b/eval/ast/25733f88-ba7a-4ed4-9f0e-c38d3994f7c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/25733f88-ba7a-4ed4-9f0e-c38d3994f7c1.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/2bb9ad2d-3400-47f7-ba17-707df346aa3c.cpp b/eval/ast/2bb9ad2d-3400-47f7-ba17-707df346aa3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/2bb9ad2d-3400-47f7-ba17-707df346aa3c.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/2db14e08-aff2-406d-b0f0-36181e6bfe10.cpp b/eval/ast/2db14e08-aff2-406d-b0f0-36181e6bfe10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/2db14e08-aff2-406d-b0f0-36181e6bfe10.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/2e0fad72-d2dd-45ab-a40f-9fb535cb4969.cpp b/eval/ast/2e0fad72-d2dd-45ab-a40f-9fb535cb4969.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/2e0fad72-d2dd-45ab-a40f-9fb535cb4969.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/2e61fc51-87b8-42f1-823c-c7b0f2fc95b1.cpp b/eval/ast/2e61fc51-87b8-42f1-823c-c7b0f2fc95b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/2e61fc51-87b8-42f1-823c-c7b0f2fc95b1.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/2e860d8c-4fd1-4d2a-915b-68fc5482957a.cpp b/eval/ast/2e860d8c-4fd1-4d2a-915b-68fc5482957a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/2e860d8c-4fd1-4d2a-915b-68fc5482957a.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/2fdd70ac-f356-4ee9-a41a-6db11e7756aa.cpp b/eval/ast/2fdd70ac-f356-4ee9-a41a-6db11e7756aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/2fdd70ac-f356-4ee9-a41a-6db11e7756aa.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/327a60ef-6396-4f63-86be-4fa0028cba50.cpp b/eval/ast/327a60ef-6396-4f63-86be-4fa0028cba50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/327a60ef-6396-4f63-86be-4fa0028cba50.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/3514a4da-d6b6-49b4-8284-8a6290de69bc.cpp b/eval/ast/3514a4da-d6b6-49b4-8284-8a6290de69bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/3514a4da-d6b6-49b4-8284-8a6290de69bc.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/35bd5d3d-b19d-4ee3-b73c-995a28ead8dc.cpp b/eval/ast/35bd5d3d-b19d-4ee3-b73c-995a28ead8dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/35bd5d3d-b19d-4ee3-b73c-995a28ead8dc.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/35ee66a5-8860-4620-bddb-ab1cad9011b5.cpp b/eval/ast/35ee66a5-8860-4620-bddb-ab1cad9011b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/35ee66a5-8860-4620-bddb-ab1cad9011b5.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/38d64b96-f089-4474-a095-50b2cfb1e1f8.cpp b/eval/ast/38d64b96-f089-4474-a095-50b2cfb1e1f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/38d64b96-f089-4474-a095-50b2cfb1e1f8.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/38e8164c-b28f-4a12-94d5-5aef0e219924.cpp b/eval/ast/38e8164c-b28f-4a12-94d5-5aef0e219924.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/38e8164c-b28f-4a12-94d5-5aef0e219924.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/39d13707-e25c-48b6-a502-315c488c4ce9.cpp b/eval/ast/39d13707-e25c-48b6-a502-315c488c4ce9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/39d13707-e25c-48b6-a502-315c488c4ce9.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/3a2f33a7-97c9-4e69-a173-c343d3b862fc.cpp b/eval/ast/3a2f33a7-97c9-4e69-a173-c343d3b862fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/3a2f33a7-97c9-4e69-a173-c343d3b862fc.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/3a43dacd-5574-499a-ae20-1f8ed2b96d1f.cpp b/eval/ast/3a43dacd-5574-499a-ae20-1f8ed2b96d1f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/3a43dacd-5574-499a-ae20-1f8ed2b96d1f.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/3a5e784f-5e1a-46ce-8f1a-5681a3415b45.cpp b/eval/ast/3a5e784f-5e1a-46ce-8f1a-5681a3415b45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/3a5e784f-5e1a-46ce-8f1a-5681a3415b45.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/3b1825ea-f3a8-4534-bb0a-b72226ff4666.cpp b/eval/ast/3b1825ea-f3a8-4534-bb0a-b72226ff4666.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/3b1825ea-f3a8-4534-bb0a-b72226ff4666.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/3bbade1e-ecc5-4fb9-97a9-3f339d750893.cpp b/eval/ast/3bbade1e-ecc5-4fb9-97a9-3f339d750893.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/3bbade1e-ecc5-4fb9-97a9-3f339d750893.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/3c377343-6cdd-432a-8ec9-25bac4fcaac8.cpp b/eval/ast/3c377343-6cdd-432a-8ec9-25bac4fcaac8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/3c377343-6cdd-432a-8ec9-25bac4fcaac8.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/3c8400c5-9869-463e-877b-14a6da8f5178.cpp b/eval/ast/3c8400c5-9869-463e-877b-14a6da8f5178.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/3c8400c5-9869-463e-877b-14a6da8f5178.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/3cdb43d0-d01b-4e37-b343-85070098a16c.cpp b/eval/ast/3cdb43d0-d01b-4e37-b343-85070098a16c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/3cdb43d0-d01b-4e37-b343-85070098a16c.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/3da26ea0-f6a8-4e1f-829b-05a69984ded1.cpp b/eval/ast/3da26ea0-f6a8-4e1f-829b-05a69984ded1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/3da26ea0-f6a8-4e1f-829b-05a69984ded1.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/3da84928-fb19-422d-8203-fb2c6a3ca710.cpp b/eval/ast/3da84928-fb19-422d-8203-fb2c6a3ca710.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/3da84928-fb19-422d-8203-fb2c6a3ca710.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/3efbf64d-cfe1-41ed-ab0f-86cb3e0e83e6.cpp b/eval/ast/3efbf64d-cfe1-41ed-ab0f-86cb3e0e83e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/3efbf64d-cfe1-41ed-ab0f-86cb3e0e83e6.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/3f8e6ffb-58e8-43a3-8359-c0589d6834b1.cpp b/eval/ast/3f8e6ffb-58e8-43a3-8359-c0589d6834b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/3f8e6ffb-58e8-43a3-8359-c0589d6834b1.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/3ffb9ca6-a7ca-4fc2-9951-cbb7208e0646.cpp b/eval/ast/3ffb9ca6-a7ca-4fc2-9951-cbb7208e0646.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/3ffb9ca6-a7ca-4fc2-9951-cbb7208e0646.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/40f1a099-eea2-420e-9071-dd873ce53e07.cpp b/eval/ast/40f1a099-eea2-420e-9071-dd873ce53e07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/40f1a099-eea2-420e-9071-dd873ce53e07.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/426af1f5-96c6-471d-869f-aa28de2092ea.cpp b/eval/ast/426af1f5-96c6-471d-869f-aa28de2092ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/426af1f5-96c6-471d-869f-aa28de2092ea.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/42e5ff31-ff99-49df-892a-2c8eaa711930.cpp b/eval/ast/42e5ff31-ff99-49df-892a-2c8eaa711930.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/42e5ff31-ff99-49df-892a-2c8eaa711930.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/42f031d7-d991-421e-84f4-d397dec06695.cpp b/eval/ast/42f031d7-d991-421e-84f4-d397dec06695.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/42f031d7-d991-421e-84f4-d397dec06695.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/44867828-66cb-4427-88a0-2d6ee6ee5831.cpp b/eval/ast/44867828-66cb-4427-88a0-2d6ee6ee5831.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/44867828-66cb-4427-88a0-2d6ee6ee5831.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/4579f67e-9325-4e2f-b0b8-9e33bd2fb5e7.cpp b/eval/ast/4579f67e-9325-4e2f-b0b8-9e33bd2fb5e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/4579f67e-9325-4e2f-b0b8-9e33bd2fb5e7.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/466537e6-ee04-4415-9b01-5c70bdd4a0d2.cpp b/eval/ast/466537e6-ee04-4415-9b01-5c70bdd4a0d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/466537e6-ee04-4415-9b01-5c70bdd4a0d2.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/466e8aea-60c3-47ec-a40b-be442c797d45.cpp b/eval/ast/466e8aea-60c3-47ec-a40b-be442c797d45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/466e8aea-60c3-47ec-a40b-be442c797d45.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/46b9493f-a571-4cc0-97a8-9a1ec9965d9a.cpp b/eval/ast/46b9493f-a571-4cc0-97a8-9a1ec9965d9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/46b9493f-a571-4cc0-97a8-9a1ec9965d9a.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/46dffb81-12ec-416b-a03a-596df6d1962a.cpp b/eval/ast/46dffb81-12ec-416b-a03a-596df6d1962a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/46dffb81-12ec-416b-a03a-596df6d1962a.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/529b4f4d-5c64-4688-adee-aa2d0335dacc.cpp b/eval/ast/529b4f4d-5c64-4688-adee-aa2d0335dacc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/529b4f4d-5c64-4688-adee-aa2d0335dacc.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/532c7baf-bf37-4034-810f-966d67cdf6c6.cpp b/eval/ast/532c7baf-bf37-4034-810f-966d67cdf6c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/532c7baf-bf37-4034-810f-966d67cdf6c6.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/53751d34-8fbf-4e93-a12a-3fb3b618cd5b.cpp b/eval/ast/53751d34-8fbf-4e93-a12a-3fb3b618cd5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/53751d34-8fbf-4e93-a12a-3fb3b618cd5b.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/555eae08-4ff3-47a1-bd4b-777d252952cb.cpp b/eval/ast/555eae08-4ff3-47a1-bd4b-777d252952cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/555eae08-4ff3-47a1-bd4b-777d252952cb.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/569ebb66-3b49-4a37-8407-c7a9c9d8cf8d.cpp b/eval/ast/569ebb66-3b49-4a37-8407-c7a9c9d8cf8d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/569ebb66-3b49-4a37-8407-c7a9c9d8cf8d.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/5a72f103-1991-4bde-a539-b7292b0244fe.cpp b/eval/ast/5a72f103-1991-4bde-a539-b7292b0244fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/5a72f103-1991-4bde-a539-b7292b0244fe.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/5ad4bd8c-370c-4814-8e0f-8f6c6aa3d9b0.cpp b/eval/ast/5ad4bd8c-370c-4814-8e0f-8f6c6aa3d9b0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/5ad4bd8c-370c-4814-8e0f-8f6c6aa3d9b0.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/5c33bd78-fb6d-4646-97fd-401ec36b6e60.cpp b/eval/ast/5c33bd78-fb6d-4646-97fd-401ec36b6e60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/5c33bd78-fb6d-4646-97fd-401ec36b6e60.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/5c3b18e6-2886-43ea-a579-f876dcf4b7ff.cpp b/eval/ast/5c3b18e6-2886-43ea-a579-f876dcf4b7ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/5c3b18e6-2886-43ea-a579-f876dcf4b7ff.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/5c5e121a-6178-4a2c-a0db-c7de047cff6a.cpp b/eval/ast/5c5e121a-6178-4a2c-a0db-c7de047cff6a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/5c5e121a-6178-4a2c-a0db-c7de047cff6a.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/62e3935c-faa8-4ebc-abb7-ca0f10feee14.cpp b/eval/ast/62e3935c-faa8-4ebc-abb7-ca0f10feee14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/62e3935c-faa8-4ebc-abb7-ca0f10feee14.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/65200960-2272-4c6e-92a6-35b6dd416d38.cpp b/eval/ast/65200960-2272-4c6e-92a6-35b6dd416d38.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/65200960-2272-4c6e-92a6-35b6dd416d38.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/65be657c-42ca-4464-adb1-dea0acea6321.cpp b/eval/ast/65be657c-42ca-4464-adb1-dea0acea6321.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/65be657c-42ca-4464-adb1-dea0acea6321.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/667faf4f-cbb8-4d6e-b0ac-f767f898fa3e.cpp b/eval/ast/667faf4f-cbb8-4d6e-b0ac-f767f898fa3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/667faf4f-cbb8-4d6e-b0ac-f767f898fa3e.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/686de50a-6f48-4e6f-8068-163b4079042e.cpp b/eval/ast/686de50a-6f48-4e6f-8068-163b4079042e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/686de50a-6f48-4e6f-8068-163b4079042e.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/6877d6d9-b285-4de8-ba13-81e8a39e5f19.cpp b/eval/ast/6877d6d9-b285-4de8-ba13-81e8a39e5f19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/6877d6d9-b285-4de8-ba13-81e8a39e5f19.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/68a9f96f-ce05-45d4-8dfa-6f11d6ff3483.cpp b/eval/ast/68a9f96f-ce05-45d4-8dfa-6f11d6ff3483.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/68a9f96f-ce05-45d4-8dfa-6f11d6ff3483.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/6ba6b76c-51d7-4575-83ec-2dc70f681773.cpp b/eval/ast/6ba6b76c-51d7-4575-83ec-2dc70f681773.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/6ba6b76c-51d7-4575-83ec-2dc70f681773.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/6dbbb47b-08bd-4952-9aae-de95f4f9e3eb.cpp b/eval/ast/6dbbb47b-08bd-4952-9aae-de95f4f9e3eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/6dbbb47b-08bd-4952-9aae-de95f4f9e3eb.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/6e21cb44-0e65-466d-a5b2-138861f0ed0f.cpp b/eval/ast/6e21cb44-0e65-466d-a5b2-138861f0ed0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/6e21cb44-0e65-466d-a5b2-138861f0ed0f.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/71b26424-5175-4497-8809-161a87b01a2d.cpp b/eval/ast/71b26424-5175-4497-8809-161a87b01a2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/71b26424-5175-4497-8809-161a87b01a2d.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/71bfccb2-bbf8-4913-adf7-e7906302f069.cpp b/eval/ast/71bfccb2-bbf8-4913-adf7-e7906302f069.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/71bfccb2-bbf8-4913-adf7-e7906302f069.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/7411e390-910b-4e25-84b7-23e1e56410d5.cpp b/eval/ast/7411e390-910b-4e25-84b7-23e1e56410d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/7411e390-910b-4e25-84b7-23e1e56410d5.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/74183882-4bef-40e4-87a6-d70e521298c6.cpp b/eval/ast/74183882-4bef-40e4-87a6-d70e521298c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/74183882-4bef-40e4-87a6-d70e521298c6.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/745a4647-9be2-40ac-9ef2-050e7563d30a.cpp b/eval/ast/745a4647-9be2-40ac-9ef2-050e7563d30a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/745a4647-9be2-40ac-9ef2-050e7563d30a.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/746f6650-ae88-4b97-aa3f-23cace098839.cpp b/eval/ast/746f6650-ae88-4b97-aa3f-23cace098839.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/746f6650-ae88-4b97-aa3f-23cace098839.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/74b96f72-d672-413b-82dc-9243e7c8a24a.cpp b/eval/ast/74b96f72-d672-413b-82dc-9243e7c8a24a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/74b96f72-d672-413b-82dc-9243e7c8a24a.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/792aea1a-1b5f-4280-b737-1869ad34fe9e.cpp b/eval/ast/792aea1a-1b5f-4280-b737-1869ad34fe9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/792aea1a-1b5f-4280-b737-1869ad34fe9e.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/7952d1ff-5326-4a4f-bc9f-63359b37cc5b.cpp b/eval/ast/7952d1ff-5326-4a4f-bc9f-63359b37cc5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/7952d1ff-5326-4a4f-bc9f-63359b37cc5b.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/79c9d731-035c-49f1-bbdf-94b2e8f2a182.cpp b/eval/ast/79c9d731-035c-49f1-bbdf-94b2e8f2a182.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/79c9d731-035c-49f1-bbdf-94b2e8f2a182.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/7eea7900-cdb1-437c-b829-5b8863a695fe.cpp b/eval/ast/7eea7900-cdb1-437c-b829-5b8863a695fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/7eea7900-cdb1-437c-b829-5b8863a695fe.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/802709c9-7e84-4f07-894f-bf9f383bb66c.cpp b/eval/ast/802709c9-7e84-4f07-894f-bf9f383bb66c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/802709c9-7e84-4f07-894f-bf9f383bb66c.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/8222fb36-ce82-4330-b2ba-43b76b59d845.cpp b/eval/ast/8222fb36-ce82-4330-b2ba-43b76b59d845.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/8222fb36-ce82-4330-b2ba-43b76b59d845.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/82376d1c-5889-45be-9430-174cc13fa4e7.cpp b/eval/ast/82376d1c-5889-45be-9430-174cc13fa4e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/82376d1c-5889-45be-9430-174cc13fa4e7.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/85261757-0c82-4026-9df0-7bba19ea6382.cpp b/eval/ast/85261757-0c82-4026-9df0-7bba19ea6382.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/85261757-0c82-4026-9df0-7bba19ea6382.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/8af73ec6-8f8e-403a-bd0d-f36299646742.cpp b/eval/ast/8af73ec6-8f8e-403a-bd0d-f36299646742.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/8af73ec6-8f8e-403a-bd0d-f36299646742.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/8bae465a-8c64-4ef5-96a3-019509e6d552.cpp b/eval/ast/8bae465a-8c64-4ef5-96a3-019509e6d552.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/8bae465a-8c64-4ef5-96a3-019509e6d552.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/8e06d9fc-22af-4d9c-864e-2b4877949e40.cpp b/eval/ast/8e06d9fc-22af-4d9c-864e-2b4877949e40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/8e06d9fc-22af-4d9c-864e-2b4877949e40.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/8f781862-9f48-4277-b592-09739d6c539a.cpp b/eval/ast/8f781862-9f48-4277-b592-09739d6c539a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/8f781862-9f48-4277-b592-09739d6c539a.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/9028431d-06fb-4fef-951a-a1f97cc063d9.cpp b/eval/ast/9028431d-06fb-4fef-951a-a1f97cc063d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/9028431d-06fb-4fef-951a-a1f97cc063d9.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/902e1f4a-f822-43e7-b535-3910222a30bf.cpp b/eval/ast/902e1f4a-f822-43e7-b535-3910222a30bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/902e1f4a-f822-43e7-b535-3910222a30bf.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/9109accf-6dd9-472e-b530-d244376648ff.cpp b/eval/ast/9109accf-6dd9-472e-b530-d244376648ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/9109accf-6dd9-472e-b530-d244376648ff.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/91bae464-47c0-4d30-8a47-861d6d0c122f.cpp b/eval/ast/91bae464-47c0-4d30-8a47-861d6d0c122f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/91bae464-47c0-4d30-8a47-861d6d0c122f.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/93b59422-0880-40cf-bff6-6fc8c94629b9.cpp b/eval/ast/93b59422-0880-40cf-bff6-6fc8c94629b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/93b59422-0880-40cf-bff6-6fc8c94629b9.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/94f753f1-88a7-4506-8776-9680556089bc.cpp b/eval/ast/94f753f1-88a7-4506-8776-9680556089bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/94f753f1-88a7-4506-8776-9680556089bc.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/95eb8ca2-47a4-4408-8dec-76aa0d2c2402.cpp b/eval/ast/95eb8ca2-47a4-4408-8dec-76aa0d2c2402.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/95eb8ca2-47a4-4408-8dec-76aa0d2c2402.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/9883fdd5-42f4-4dcc-9209-0840bed6cd3c.cpp b/eval/ast/9883fdd5-42f4-4dcc-9209-0840bed6cd3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/9883fdd5-42f4-4dcc-9209-0840bed6cd3c.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/98d4f690-5daf-4530-8be7-9865265099fc.cpp b/eval/ast/98d4f690-5daf-4530-8be7-9865265099fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/98d4f690-5daf-4530-8be7-9865265099fc.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/9904903e-fea2-406d-9605-eb24a43d6756.cpp b/eval/ast/9904903e-fea2-406d-9605-eb24a43d6756.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/9904903e-fea2-406d-9605-eb24a43d6756.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/99e1aefa-be65-4177-a7be-4a30e0eee28c.cpp b/eval/ast/99e1aefa-be65-4177-a7be-4a30e0eee28c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/99e1aefa-be65-4177-a7be-4a30e0eee28c.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/9ab27260-f6e6-4627-ba8b-5752bb2d47f3.cpp b/eval/ast/9ab27260-f6e6-4627-ba8b-5752bb2d47f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/9ab27260-f6e6-4627-ba8b-5752bb2d47f3.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/9b09e5a3-fceb-47af-8a25-a74cb43e8533.cpp b/eval/ast/9b09e5a3-fceb-47af-8a25-a74cb43e8533.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/9b09e5a3-fceb-47af-8a25-a74cb43e8533.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/9b2777c7-9cbb-4cdf-b059-be6177eed448.cpp b/eval/ast/9b2777c7-9cbb-4cdf-b059-be6177eed448.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/9b2777c7-9cbb-4cdf-b059-be6177eed448.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/9bed727d-b4ea-4730-a3cf-aec819f4b864.cpp b/eval/ast/9bed727d-b4ea-4730-a3cf-aec819f4b864.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/9bed727d-b4ea-4730-a3cf-aec819f4b864.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/9c4d7885-78d3-4427-b98a-b1ad1593cbcf.cpp b/eval/ast/9c4d7885-78d3-4427-b98a-b1ad1593cbcf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/9c4d7885-78d3-4427-b98a-b1ad1593cbcf.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/9c9b9674-6d74-43e7-a9d7-d93e7f772ae9.cpp b/eval/ast/9c9b9674-6d74-43e7-a9d7-d93e7f772ae9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/9c9b9674-6d74-43e7-a9d7-d93e7f772ae9.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/9d12ea13-a5b8-4102-8d06-b9d9760ba32c.cpp b/eval/ast/9d12ea13-a5b8-4102-8d06-b9d9760ba32c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/9d12ea13-a5b8-4102-8d06-b9d9760ba32c.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/9ebf9070-090a-4160-96a3-2f594b6953ce.cpp b/eval/ast/9ebf9070-090a-4160-96a3-2f594b6953ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/9ebf9070-090a-4160-96a3-2f594b6953ce.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/a0227ea9-ed6c-442d-a29e-fc885450c893.cpp b/eval/ast/a0227ea9-ed6c-442d-a29e-fc885450c893.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/a0227ea9-ed6c-442d-a29e-fc885450c893.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/a1bab682-8210-4ce9-a345-eec3bd23e5c9.cpp b/eval/ast/a1bab682-8210-4ce9-a345-eec3bd23e5c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/a1bab682-8210-4ce9-a345-eec3bd23e5c9.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/a27dedfd-bacf-44fa-918d-bf2434694d9d.cpp b/eval/ast/a27dedfd-bacf-44fa-918d-bf2434694d9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/a27dedfd-bacf-44fa-918d-bf2434694d9d.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/a3c43ba5-feef-4dd7-b011-6679ee7b09ea.cpp b/eval/ast/a3c43ba5-feef-4dd7-b011-6679ee7b09ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/a3c43ba5-feef-4dd7-b011-6679ee7b09ea.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/a517d9dd-678d-41f6-90f8-3062716436ba.cpp b/eval/ast/a517d9dd-678d-41f6-90f8-3062716436ba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/a517d9dd-678d-41f6-90f8-3062716436ba.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/a5371c67-fb94-4013-90c6-6761622f54a8.cpp b/eval/ast/a5371c67-fb94-4013-90c6-6761622f54a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/a5371c67-fb94-4013-90c6-6761622f54a8.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/a5a51330-2f8f-4577-a65f-162fe0ce4c3c.cpp b/eval/ast/a5a51330-2f8f-4577-a65f-162fe0ce4c3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/a5a51330-2f8f-4577-a65f-162fe0ce4c3c.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/a5d6afa4-11ba-47fa-9435-10636684338d.cpp b/eval/ast/a5d6afa4-11ba-47fa-9435-10636684338d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/a5d6afa4-11ba-47fa-9435-10636684338d.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/a6d69bb5-8dce-4035-88f2-c6ea227acb1d.cpp b/eval/ast/a6d69bb5-8dce-4035-88f2-c6ea227acb1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/a6d69bb5-8dce-4035-88f2-c6ea227acb1d.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/a734657e-55a5-42c7-8b19-a1397debc580.cpp b/eval/ast/a734657e-55a5-42c7-8b19-a1397debc580.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/a734657e-55a5-42c7-8b19-a1397debc580.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/a82f40bd-d398-4594-9cb3-d6018702a8c7.cpp b/eval/ast/a82f40bd-d398-4594-9cb3-d6018702a8c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/a82f40bd-d398-4594-9cb3-d6018702a8c7.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/a96895dc-a7d5-4706-99a6-c5e6033d5eb4.cpp b/eval/ast/a96895dc-a7d5-4706-99a6-c5e6033d5eb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/a96895dc-a7d5-4706-99a6-c5e6033d5eb4.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/ac3acdd8-cae4-420b-b29b-efaa05851bb5.cpp b/eval/ast/ac3acdd8-cae4-420b-b29b-efaa05851bb5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/ac3acdd8-cae4-420b-b29b-efaa05851bb5.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/ac744cef-895e-4a3a-9ba9-6cce7d70f794.cpp b/eval/ast/ac744cef-895e-4a3a-9ba9-6cce7d70f794.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/ac744cef-895e-4a3a-9ba9-6cce7d70f794.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/acee5850-e34c-413a-a1ef-d7891e92054c.cpp b/eval/ast/acee5850-e34c-413a-a1ef-d7891e92054c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/acee5850-e34c-413a-a1ef-d7891e92054c.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/ad12ccb2-b62b-4377-ae46-0e8d8805768d.cpp b/eval/ast/ad12ccb2-b62b-4377-ae46-0e8d8805768d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/ad12ccb2-b62b-4377-ae46-0e8d8805768d.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/ae8a56d8-cea2-47f6-9733-97a877c38124.cpp b/eval/ast/ae8a56d8-cea2-47f6-9733-97a877c38124.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/ae8a56d8-cea2-47f6-9733-97a877c38124.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/af7ba925-59a4-4e2f-a510-ed360343316a.cpp b/eval/ast/af7ba925-59a4-4e2f-a510-ed360343316a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/af7ba925-59a4-4e2f-a510-ed360343316a.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/b03303a0-572d-4bdc-ae53-b4f5670fb01a.cpp b/eval/ast/b03303a0-572d-4bdc-ae53-b4f5670fb01a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/b03303a0-572d-4bdc-ae53-b4f5670fb01a.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/b1dc7395-76e3-43af-8d09-11052f1e1ac9.cpp b/eval/ast/b1dc7395-76e3-43af-8d09-11052f1e1ac9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/b1dc7395-76e3-43af-8d09-11052f1e1ac9.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/b1f53246-2137-4fdf-b69f-8b5b0e49a504.cpp b/eval/ast/b1f53246-2137-4fdf-b69f-8b5b0e49a504.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/b1f53246-2137-4fdf-b69f-8b5b0e49a504.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/b3185783-5ec4-4900-a5f0-68e30ef151c0.cpp b/eval/ast/b3185783-5ec4-4900-a5f0-68e30ef151c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/b3185783-5ec4-4900-a5f0-68e30ef151c0.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/b4b6317c-dd58-4c90-bd46-1e0e46daff7d.cpp b/eval/ast/b4b6317c-dd58-4c90-bd46-1e0e46daff7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/b4b6317c-dd58-4c90-bd46-1e0e46daff7d.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/b599fa88-49c4-4216-bd89-0bb74552e287.cpp b/eval/ast/b599fa88-49c4-4216-bd89-0bb74552e287.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/b599fa88-49c4-4216-bd89-0bb74552e287.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/b5b296bd-6467-4ee2-95ed-58bdb136fd53.cpp b/eval/ast/b5b296bd-6467-4ee2-95ed-58bdb136fd53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/b5b296bd-6467-4ee2-95ed-58bdb136fd53.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/b82d2894-0832-4154-81f9-746d9e9b5343.cpp b/eval/ast/b82d2894-0832-4154-81f9-746d9e9b5343.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/b82d2894-0832-4154-81f9-746d9e9b5343.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/ba14cee4-eace-4001-b8d3-05e240ef432f.cpp b/eval/ast/ba14cee4-eace-4001-b8d3-05e240ef432f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/ba14cee4-eace-4001-b8d3-05e240ef432f.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/bacc8443-4800-4a6e-b7f1-0794472a8b76.cpp b/eval/ast/bacc8443-4800-4a6e-b7f1-0794472a8b76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/bacc8443-4800-4a6e-b7f1-0794472a8b76.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/bee30ecc-c2f2-4e4d-8390-c56b6cc5a484.cpp b/eval/ast/bee30ecc-c2f2-4e4d-8390-c56b6cc5a484.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/bee30ecc-c2f2-4e4d-8390-c56b6cc5a484.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/c0831066-311b-441e-b73c-b61abc9ce067.cpp b/eval/ast/c0831066-311b-441e-b73c-b61abc9ce067.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/c0831066-311b-441e-b73c-b61abc9ce067.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/c0b4d15c-2cb7-41d2-809d-7461c2cb37ae.cpp b/eval/ast/c0b4d15c-2cb7-41d2-809d-7461c2cb37ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/c0b4d15c-2cb7-41d2-809d-7461c2cb37ae.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/c2bff077-2a20-440b-ac1f-ad00f08d27fd.cpp b/eval/ast/c2bff077-2a20-440b-ac1f-ad00f08d27fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/c2bff077-2a20-440b-ac1f-ad00f08d27fd.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/c3ef5ad3-8174-45f9-96d3-aa7e874b350a.cpp b/eval/ast/c3ef5ad3-8174-45f9-96d3-aa7e874b350a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/c3ef5ad3-8174-45f9-96d3-aa7e874b350a.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/c487956f-b993-4937-a83a-197fcbc213c2.cpp b/eval/ast/c487956f-b993-4937-a83a-197fcbc213c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/c487956f-b993-4937-a83a-197fcbc213c2.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/c59a5eaa-5af6-4e82-94fb-7cf02daa27e0.cpp b/eval/ast/c59a5eaa-5af6-4e82-94fb-7cf02daa27e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/c59a5eaa-5af6-4e82-94fb-7cf02daa27e0.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/c5af70c0-21e3-487c-90e8-ef1aea59d9ee.cpp b/eval/ast/c5af70c0-21e3-487c-90e8-ef1aea59d9ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/c5af70c0-21e3-487c-90e8-ef1aea59d9ee.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/c607708c-f032-425c-9482-083482f27d2e.cpp b/eval/ast/c607708c-f032-425c-9482-083482f27d2e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c7277747544fc7232cd808382ee394007fbceea --- /dev/null +++ b/eval/ast/c607708c-f032-425c-9482-083482f27d2e.cpp @@ -0,0 +1,236 @@ +#include +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/c6120b74-38d7-45a2-8cc9-5f9db198c322.cpp b/eval/ast/c6120b74-38d7-45a2-8cc9-5f9db198c322.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/c6120b74-38d7-45a2-8cc9-5f9db198c322.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/c75d4e08-7726-454a-825b-90ecf40427dc.cpp b/eval/ast/c75d4e08-7726-454a-825b-90ecf40427dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/c75d4e08-7726-454a-825b-90ecf40427dc.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/c76e5c91-f095-42a3-aeb4-2571c9328fc4.cpp b/eval/ast/c76e5c91-f095-42a3-aeb4-2571c9328fc4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/c76e5c91-f095-42a3-aeb4-2571c9328fc4.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/c91ee39f-d24c-43a4-a54c-caf65246631c.cpp b/eval/ast/c91ee39f-d24c-43a4-a54c-caf65246631c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/c91ee39f-d24c-43a4-a54c-caf65246631c.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/cc222116-cb7f-41ec-a9fa-190f54ee4488.cpp b/eval/ast/cc222116-cb7f-41ec-a9fa-190f54ee4488.cpp new file mode 100644 index 0000000000000000000000000000000000000000..065931bcafce04d14ca8a0439feb2679fef55874 --- /dev/null +++ b/eval/ast/cc222116-cb7f-41ec-a9fa-190f54ee4488.cpp @@ -0,0 +1,309 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +struct Node{ + + bool operand; + + union { + + char op; + + int nr; + + }; + + Node *left,*right; + + + + Node(char x) : operand(false), op(x), left(nullptr), right(nullptr){} + + Node(int x) : operand(true), nr(x), left(nullptr), right(nullptr){} + + Node(){} + +}; + + + + + +int precedence(char x) + +{ + + if(x == '^')return 3; + + else if(x == '*' || x == '/')return 3; + + else if(x == '+' || x == '-')return 1; + + return -1; + +} + + + +void delete_spaces(string &s) + +{ + + int n = s.size(),j=0; + + for(int i=0;i st; + + stack noduri; + + + + for(int i=0;ileft = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + if(st.top() == '(')st.pop(); + + } + + else + + { + + while(!st.empty() && in[i] != '^' && precedence(in[i]) <= precedence(st.top())) + + { + + Node *t = new Node(st.top()); + + Node *t1 = nullptr, *t2 = nullptr; + + + + if(!noduri.empty()) + + { + + t1 = noduri.top(); + + noduri.pop(); + + } + + if(!noduri.empty()) + + { + + t2 = noduri.top(); + + noduri.pop(); + + } + + + + t->left = t2; + + t->right = t1; + + noduri.push(t); + + + + st.pop(); + + } + + st.push(in[i]); + + } + + + + } + + return noduri.top(); + +} +int calcul(Node *&root) + +{ + + if(root->operand == true) + + return root->nr; + + else + + { + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + switch(root->op) + + { + + case '+' : return t1 + t2; + + break; + + case '-' : return t1 - t2; + + break; + + case '*' : return t1 * t2; + + break; + + case '/' : return t1 / t2; + + break; + + case '^' : return pow(t1,t2); + + break; + + } + + } + +} + + + +int main() + +{ + + string in; + + getline(f,in); + + Node *root = infix_to_prefix(in); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/cd8b9a24-9765-47d2-a446-c1ea7c37183e.cpp b/eval/ast/cd8b9a24-9765-47d2-a446-c1ea7c37183e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/cd8b9a24-9765-47d2-a446-c1ea7c37183e.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/cffa99ee-54ed-4b9c-97dc-c03bce426d0c.cpp b/eval/ast/cffa99ee-54ed-4b9c-97dc-c03bce426d0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/cffa99ee-54ed-4b9c-97dc-c03bce426d0c.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/d0951f3d-ee15-4bce-bf81-7a55a5381e6e.cpp b/eval/ast/d0951f3d-ee15-4bce-bf81-7a55a5381e6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/d0951f3d-ee15-4bce-bf81-7a55a5381e6e.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/d149514f-f9ff-4680-8d9f-89bbb2e47860.cpp b/eval/ast/d149514f-f9ff-4680-8d9f-89bbb2e47860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/d149514f-f9ff-4680-8d9f-89bbb2e47860.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/d25b07ab-89fc-43a9-a656-15dfb2aea15a.cpp b/eval/ast/d25b07ab-89fc-43a9-a656-15dfb2aea15a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/d25b07ab-89fc-43a9-a656-15dfb2aea15a.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/ast/db865dcd-0fcb-4a16-aafb-3dcd89b1a253.cpp b/eval/ast/db865dcd-0fcb-4a16-aafb-3dcd89b1a253.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/db865dcd-0fcb-4a16-aafb-3dcd89b1a253.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/dbd3fec0-914a-42a7-a3ad-94f9bc351bbe.cpp b/eval/ast/dbd3fec0-914a-42a7-a3ad-94f9bc351bbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/dbd3fec0-914a-42a7-a3ad-94f9bc351bbe.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/dc17c0c5-3dba-4750-a231-6d6a42d19e67.cpp b/eval/ast/dc17c0c5-3dba-4750-a231-6d6a42d19e67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/dc17c0c5-3dba-4750-a231-6d6a42d19e67.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/dc86d913-c25f-4b8c-85b3-e2311a13f615.cpp b/eval/ast/dc86d913-c25f-4b8c-85b3-e2311a13f615.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/dc86d913-c25f-4b8c-85b3-e2311a13f615.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/ddee491b-e407-47c4-a171-014255e26e6f.cpp b/eval/ast/ddee491b-e407-47c4-a171-014255e26e6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/ddee491b-e407-47c4-a171-014255e26e6f.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/de9d5f40-eb5c-4c4a-9d72-cc5506d5ccc6.cpp b/eval/ast/de9d5f40-eb5c-4c4a-9d72-cc5506d5ccc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/de9d5f40-eb5c-4c4a-9d72-cc5506d5ccc6.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/e0255698-be84-4037-b063-57a362a8872e.cpp b/eval/ast/e0255698-be84-4037-b063-57a362a8872e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/e0255698-be84-4037-b063-57a362a8872e.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/e06a88a8-4b3a-47cf-a1b3-9262d0ff8277.cpp b/eval/ast/e06a88a8-4b3a-47cf-a1b3-9262d0ff8277.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/e06a88a8-4b3a-47cf-a1b3-9262d0ff8277.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/e7313470-50d1-46fb-85c6-8a38b81ebe6c.cpp b/eval/ast/e7313470-50d1-46fb-85c6-8a38b81ebe6c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/e7313470-50d1-46fb-85c6-8a38b81ebe6c.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/e7358e01-7b1a-4e70-aaf5-f70955abda55.cpp b/eval/ast/e7358e01-7b1a-4e70-aaf5-f70955abda55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/e7358e01-7b1a-4e70-aaf5-f70955abda55.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/e7e9fef3-2451-4e8c-a66e-32766def5f53.cpp b/eval/ast/e7e9fef3-2451-4e8c-a66e-32766def5f53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/e7e9fef3-2451-4e8c-a66e-32766def5f53.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/e8101a36-0641-4947-bd93-f5142caa2d79.cpp b/eval/ast/e8101a36-0641-4947-bd93-f5142caa2d79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/e8101a36-0641-4947-bd93-f5142caa2d79.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/e871a247-05d3-427d-adb2-bac0a761ce25.cpp b/eval/ast/e871a247-05d3-427d-adb2-bac0a761ce25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dd18c6315da535e9fc75792aa7fbebe69c95107 --- /dev/null +++ b/eval/ast/e871a247-05d3-427d-adb2-bac0a761ce25.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/e873f78b-9d14-437b-a624-23dc782c0a69.cpp b/eval/ast/e873f78b-9d14-437b-a624-23dc782c0a69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/e873f78b-9d14-437b-a624-23dc782c0a69.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + +int par[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + + Nod(int _val) { + + val = _val; + + op = '?'; + + st = dr = nullptr; + + } + +}; + + + +int form_par(int ind = 0) + +{ + + for(int i = ind; sir[i]; i++) + + if(sir[i] == '(') { + + int res = form_par(i+1); + + par[res] = i; + + i = res; + + } + + else if(sir[i] == ')') + + return i; + +} + + + +Nod* construct(int st, int dr, int level) + +{ + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '+' || sir[i] == '-') + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') i = par[i]; + + else if (sir[i] == '*' || sir[i] == '/') + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + return new Nod(nr); + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + form_par(0); + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/e9bf182f-d112-4aaf-b525-80128d5cb9a5.cpp b/eval/ast/e9bf182f-d112-4aaf-b525-80128d5cb9a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774a8d7bc617960dfdb34371905f09e1d0defbcf --- /dev/null +++ b/eval/ast/e9bf182f-d112-4aaf-b525-80128d5cb9a5.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string sir; + +struct nod + +{ + + bool isNum=1; + + int num; + + char exp='\0'; + +}a[100002]; + +int Q; + +int End[100002]; + + + +int solve(int poz) + +{ + + int sum=0; + + int start=poz; + + poz++; + + + + int semn=1,val; + + while( a[poz].exp!=')' ) + + { + + if(!a[poz].isNum) + + { + + if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + semn*=(a[poz].exp=='-'?-1:1); + + } + + else + + val=a[poz].num; + + + + if(a[++poz].isNum) + + val=a[poz].num; + + else if(a[poz].exp=='(') + + { + + val=solve(poz); + + poz=End[poz]; + + } + + else + + poz--; + + + + poz++; + + while(a[poz].exp!='+' && a[poz].exp!='-' && a[poz].exp!=')') + + { + + if(a[poz].exp=='*') + + { + + if(a[++poz].exp=='(') + + { + + val*=solve(poz); + + poz=End[poz]; + + } + + else + + val*=a[poz].num; + + } + + else + + { + + if(a[++poz].exp=='(') + + { + + val/=solve(poz); + + poz=End[poz]; + + } + + else + + val/=a[poz].num; + + } + + + + poz++; + + } + + + + sum+=semn*val; + + semn=1; + + } + + + + End[start]=poz; + + + + return sum; + +} + + + +int main() + +{ + + in>>sir; + + + + Q=0; + + a[0].isNum=0; + + + + for(auto lit:sir) + + if(isalnum(lit)) + + { + + if(!a[Q].isNum) + + Q++; + + a[Q].num=a[Q].num*10+lit-'0'; + + } + + else + + { + + a[++Q].isNum=0; + + a[Q].exp=lit; + + } + + + + a[0].exp='('; + + a[++Q].isNum=0; + + a[Q].exp=')'; + + + + out< +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/ec560f53-0f69-45d7-9bbc-e615e2afb1a5.cpp b/eval/ast/ec560f53-0f69-45d7-9bbc-e615e2afb1a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/ec560f53-0f69-45d7-9bbc-e615e2afb1a5.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/ee72532d-8bf6-405c-836c-26a6c4d9c59f.cpp b/eval/ast/ee72532d-8bf6-405c-836c-26a6c4d9c59f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7794fe4195784320adbee1d503012d4ef4a3f886 --- /dev/null +++ b/eval/ast/ee72532d-8bf6-405c-836c-26a6c4d9c59f.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/ef32316d-868a-4722-8f5c-0b380b51dc05.cpp b/eval/ast/ef32316d-868a-4722-8f5c-0b380b51dc05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34cc7e80ac07e54cc4e55f2ac021b63098b51ebc --- /dev/null +++ b/eval/ast/ef32316d-868a-4722-8f5c-0b380b51dc05.cpp @@ -0,0 +1,304 @@ +#include + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/f06538fb-1884-45db-bf27-dc9a65a69fad.cpp b/eval/ast/f06538fb-1884-45db-bf27-dc9a65a69fad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/f06538fb-1884-45db-bf27-dc9a65a69fad.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/f0ec1d3a-387c-4d11-b45e-f78af24d7fdf.cpp b/eval/ast/f0ec1d3a-387c-4d11-b45e-f78af24d7fdf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/f0ec1d3a-387c-4d11-b45e-f78af24d7fdf.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/f1da7c08-894e-48dd-8b0a-96403b0e7547.cpp b/eval/ast/f1da7c08-894e-48dd-8b0a-96403b0e7547.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb66aa07fba9af6d978b2dd35c4c07fd508a97d --- /dev/null +++ b/eval/ast/f1da7c08-894e-48dd-8b0a-96403b0e7547.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAXN = 100041; + +char sir[MAXN]; + + + +struct Nod + +{ + + int val; + + char op; + + Nod *st, *dr; + + Nod(char _op, Nod* _st, Nod* _dr) { + + op = _op; + + st = _st; + + dr = _dr; + + } + +}; + + + +Nod* construct(int st, int dr, int level) + +{ + + int par = 0; + + if (level == 0) { + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '+' || sir[i] == '-')) + + return new Nod(sir[i], construct(st, i-1, level), construct(i+1, dr, level+1)); + + } + + for (int i = dr; i >= st; i--) + + if (sir[i] == ')') par++; + + else if (sir[i] == '(') par--; + + else if (par == 0 && (sir[i] == '*' || sir[i] == '/')) + + return new Nod(sir[i], construct(st, i-1, 1), construct(i+1, dr, 1)); + + if (sir[st] == '(') + + return construct(st+1, dr-1, 0); + + int nr = 0; + + for (int i = st; i <= dr; i++) + + nr = nr*10 + sir[i] - '0'; + + Nod* rez = new Nod('?', nullptr, nullptr); + + rez->val = nr; + + return rez; + +} + + + +int eval(Nod* root) + +{ + + if (root->op == '?') + + return root->val; + + int x = eval(root->st); + + int y = eval(root->dr); + + switch (root->op) { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin >> sir; + + Nod* root = construct(0, strlen(sir)-1, 0); + + fout << eval(root); + + + + + + return 0; + +} diff --git a/eval/ast/f239a8e3-8f3b-4c6c-a17b-7f1eed6e250f.cpp b/eval/ast/f239a8e3-8f3b-4c6c-a17b-7f1eed6e250f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78b047b40d162c1916de98e9d80beeaddad757f9 --- /dev/null +++ b/eval/ast/f239a8e3-8f3b-4c6c-a17b-7f1eed6e250f.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +const int LMAX = 100005; + +char s[LMAX]; + +struct Node { + Node *left{0}, *right{0}; + int value{0}; + char op{'#'}; +}; + +Node* build(int left, int right); + +Node* build_ops(int left, int right, const char* ops) { + int brackets = 0; + + for (int i = right; i >= left; i--) { + if (s[i] == ')') + brackets++; + else if (s[i] == '(') + brackets--; + else if (!brackets && strchr(ops, s[i])) { + Node* node = new Node; + node->op = s[i]; + node->left = build(left, i - 1); + node->right = build(i + 1, right); + return node; + } + } + + return 0; +} + +Node* build(int left, int right) { + if (Node *node = build_ops(left, right, "+-")) + return node; + + if (Node *node = build_ops(left, right, "*/")) + return node; + + if (s[left] == '(') + return build(left + 1, right - 1); + + Node *node = new Node; + char aux = 0; + + std::swap(s[right + 1], aux); + sscanf(s + left, "%d", &node->value); + std::swap(s[right + 1], aux); + + return node; +} + +int eval(Node* node) { + if (node->op == '#') + return node->value; + + int left = eval(node->left); + int right = eval(node->right); + + if (node->op == '+') + return left + right; + if (node->op == '-') + return left - right; + if (node->op == '*') + return left * right; + if (node->op == '/') + return left / right; + + return -1; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + Node* root = build(0, strlen(s) - 1); + + printf("%d\n", eval(root)); + + return 0; +} diff --git a/eval/ast/f30b180a-c828-4f02-8caa-7dcaa8edca57.cpp b/eval/ast/f30b180a-c828-4f02-8caa-7dcaa8edca57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eaf0617016c0e1b39b4929193cae04c3b1e6550 --- /dev/null +++ b/eval/ast/f30b180a-c828-4f02-8caa-7dcaa8edca57.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +using namespace std; + +struct node { + int tip, val; + char op; + node *left, *right; + node(int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + tip = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, char cur) { + if (cur == '+') + return a + b; + if (cur == '-') + return a - b; + if (cur == '*') + return a * b; + if (cur == '/') + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + +node *arbore(vector &exp) { + stack members; + for (int i = 0; i < (int) exp.size(); i++) { + node *this_node; + if (!is_operator(exp[i][0])) { + + this_node = new node(0, stoi(exp[i]), ' ', 0, 0); + } else { + node *right = members.top(); + members.pop(); + node *left = members.top(); + members.pop(); + this_node = new node(1, 0, exp[i][0], left, right); + } + members.push(this_node); + } + return members.top(); +} + +int rezolvare_arbore(node *root) { + if (root->tip == 0) + return root->val; + if (root->tip == 1) { + int l = rezolvare_arbore(root->left), r = rezolvare_arbore(root->right); + return result(l, r, root->op); + } + return 0; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + node *root = arbore(output); + + int res = rezolvare_arbore(root); + fout << res << "\n"; + + return 0; +} diff --git a/eval/ast/f3131f59-60f1-4582-ba59-caccd7f2d508.cpp b/eval/ast/f3131f59-60f1-4582-ba59-caccd7f2d508.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/f3131f59-60f1-4582-ba59-caccd7f2d508.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< + + + + + + +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +char St_Op[100001]; + +arbore* St_Nod[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + +void adaugare_nod() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + { + + if(St_Op[idx_op - 1] != '(' && prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + adaugare_nod(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > 0) + + { + + if(prioritate(St_Op[idx_op], St_Op[idx_op - 1])) + + { + + idx_op--; + + idx_nod--; + + adaugare_nod(); + + + + idx_nod++; + + St_Nod[idx_nod] = St_Nod[idx_nod + 1]; + + + + idx_op++; + + St_Op[idx_op] = St_Op[idx_op + 1]; + + } + + else + + adaugare_nod(); + + } + + if(idx_op == 0) + + adaugare_nod(); + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + break; + + case '-': + + return STANGA - DREAPTA; + + break; + + case '*': + + return STANGA * DREAPTA; + + break; + + case '/': + + return STANGA / DREAPTA; + + break; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/f63c20c3-80f4-4d32-89c6-99e108bd58fe.cpp b/eval/ast/f63c20c3-80f4-4d32-89c6-99e108bd58fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/f63c20c3-80f4-4d32-89c6-99e108bd58fe.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/f6a82746-c3c3-4fae-94d2-c9b5b65e81b4.cpp b/eval/ast/f6a82746-c3c3-4fae-94d2-c9b5b65e81b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cd75c4006a058a516b8cca0511f815742600cb4 --- /dev/null +++ b/eval/ast/f6a82746-c3c3-4fae-94d2-c9b5b65e81b4.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +struct node { + int type, val; + char op; + node *left, *right; + node (int t = 0, int v = 0, char o = ' ', node *l = 0, node *r = 0) { + type = t; + val = v; + op = o; + left = l; + right = r; + } +}; + +int evaluate(int a, int b, char op) { + if (op == '+') + return a + b; + if (op == '-') + return a - b; + if (op == '*') + return a * b; + if (op == '/') + return a / b; + return 1; +} + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate_exp(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + node *parent; + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '+', term_1.first, term_2.first); + term_1.first = parent; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + pos = term_2.second; + parent = new node(1, 0, '-', term_1.first, term_2.first); + term_1.first = parent; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate_exp(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + node* leaf; + leaf = new node(0, number, ' ', 0, 0); + return {leaf, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + node* parent; + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '*', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + parent = new node(1, 0, '/', term_1.first, term_2.first); + pos = term_2.second; + term_1.first = parent; + } + } + return {term_1.first, pos}; +} + +int evaluate_tree(node *root) { + if (root->type == 0) + return root->val; + int l = evaluate_tree(root->left), r = evaluate_tree(root->right); + return evaluate(l, r, root->op); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair root = evaluate_exp(exp, 0); + + int result = evaluate_tree(root.first); + + fout << result << "\n"; + + return 0; +} diff --git a/eval/ast/f7347509-037c-4d8d-9933-9d1f2135fff8.cpp b/eval/ast/f7347509-037c-4d8d-9933-9d1f2135fff8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/f7347509-037c-4d8d-9933-9d1f2135fff8.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + + + + + +struct Node + +{ + + Node *left{nullptr}, *right{nullptr}; + + int value{0}; + + char op{'#'}; + +}; + + + + + + + +Node* build(int i, int j) + +{ + + int b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '+' || s[k] == '-')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + b = 0; + + + + for(int k = j; k >= i; k--) + + { + + if(s[k] == ')') ++b; + + else if(s[k] == '(') --b; + + else if(b == 0 && (s[k] == '*' || s[k] == '/')) + + { + + Node *n = new Node; + + n -> op = s[k]; + + n -> left = build(i, k-1); + + n -> right = build(k+1, j); + + return n; + + } + + } + + + + if(s[i] == '(') + + return build(i+1, j-1); + + + + Node *n = new Node; + + n -> value; + + + + for(int k = i; k <= j; k++) + + n -> value = n -> value * 10 + s[k] - '0'; + + + + return n; + +} + + + +int eval(Node*& node) + +{ + + if(node -> op == '#') + + return node->value; + + + + int left = eval(node->left); + + int right = eval(node->right); + + + + switch(node->op) + + { + + case '+': + + return left + right; + + case '-': + + return left - right; + + case '*': + + return left * right; + + case '/': + + return left / right; + + } + +} + + + +int main() + +{ + + fin >> s; + + int l = s.size(); + + Node *arb = build(0, l-1); + + fout << eval(arb); + + + + return 0; + +} diff --git a/eval/ast/fa4cc184-6554-4e8e-82ff-43bbddfa169d.cpp b/eval/ast/fa4cc184-6554-4e8e-82ff-43bbddfa169d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74dc5e33aa6c33ec8dc3e3b439b1339c5c174fa5 --- /dev/null +++ b/eval/ast/fa4cc184-6554-4e8e-82ff-43bbddfa169d.cpp @@ -0,0 +1,292 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + + + +char infix[100001],nrCurent[100001]; + + + +struct Node + +{ + + string info; + + Node *left,*right; + + Node(string s) : info(s),left(nullptr),right(nullptr) {} + +}; + + + + + +int preced(char x) + +{ + + if(x == '+' || x == '-') + + return 1; + + if(x == '*' || x == '/') + + return 2; + + if(x == '^') + + return 3; + + return -1; + +} +bool cifra(char x) + +{ + + return x >= '0' && x <= '9'; + +} + + + +Node* infix_to_postfix(char infix[]) + +{ + + + + + + int n = strlen(infix),k; + + + + stack operatori; + + stack noduri; + + + + for(int i=0; ileft = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + if(operatori.top() == '(') + + operatori.pop(); + + } + + else + + { + + while(!operatori.empty() && infix[i] != '^' && preced(infix[i]) <= preced(operatori.top()) ) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + operatori.push(infix[i]); + + } + + } + + + + while(!operatori.empty()) + + { + + Node *n = new Node( string(1,operatori.top()) ); + + + + Node *t1 = noduri.top(); + + noduri.pop(); + + Node *t2 = noduri.top(); + + noduri.pop(); + + + + n->left = t2; + + n->right = t1; + + noduri.push(n); + + + + operatori.pop(); + + } + + + + return noduri.top(); + +} + + + + + +int calcul(Node *root) + +{ + + string s = root->info; + + + + if(cifra(s[0])) + + return stoi(s); + + + + int t1 = calcul(root->left); + + int t2 = calcul(root->right); + + + + if(s[0] == '+') + + return t1 + t2; + + else if(s[0] == '-') + + return t1 - t2; + + else if(s[0] == '*') + + return t1 * t2; + + else if(s[0] == '/') + + return t1 / t2; + + else if(s[0] == '^') + + return pow(t1,t2); + +} +int main() + +{ + + f.getline(infix,100001); + + Node *root = infix_to_postfix(infix); + + g << calcul(root); + + return 0; + +} diff --git a/eval/ast/fd7dc0f9-0aa6-463a-b2bf-d95eb1de3e71.cpp b/eval/ast/fd7dc0f9-0aa6-463a-b2bf-d95eb1de3e71.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f898a648779e75fe063c371bbaa6e960cbab138 --- /dev/null +++ b/eval/ast/fd7dc0f9-0aa6-463a-b2bf-d95eb1de3e71.cpp @@ -0,0 +1,191 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct nod + +{ + + int val; + + char op; + + nod *st, *dr; + + + + nod(int a=0, char b=0, nod*c=0, nod*d=0) + + { + + val = a; + + op = b; + + st = c; + + dr = d; + + } + +}; + + + +char prio[3][3] = { "+-", "*/", "" }; + + + +nod* arbore(int k); + +int eval(nod*A); + +void inordine(nod*x) + +{ + + if (x->st) + + inordine(x->st); + + if (x->val) + + fout << x->val; + + else fout << x->op; + + if (x->dr) + + inordine(x->dr); + +} + + + +int poz, n; + +char s[100005]; + +nod*A; + + + +int main() + +{ + + fin >> s; + + n = strlen(s); + + A = arbore(0); + + + + fout << eval(A) << '\n'; + + return 0; + +} + + + +nod* arbore(int k) + +{ + + nod *x, *y; + + if (k == 2) + + { + + if (s[poz] == '(') + + { + + poz++; + + x = arbore(0); + + poz++; + + } + + else + + { + + x = new nod(); + + while (s[poz] <= '9' && s[poz] >= '0') + + { + + x->val = x->val*10+s[poz]-'0'; + + poz++; + + } + + } + + } + + else + + { + + x = arbore(k+1); + + while (poz < n && strchr(prio[k],s[poz])) + + { + + y = new nod(0,s[poz++],x,arbore(k+1)); + + x = y; + + } + + } + + return x; + +} + + + +int eval(nod*A) + +{ + + switch (A->op) + + { + + case '+': return eval(A->st) + eval(A->dr); break; + + case '-': return eval(A->st) - eval(A->dr); break; + + case '*': return eval(A->st) * eval(A->dr); break; + + case '/': return eval(A->st) / eval(A->dr); break; + + default: return A->val; + + } + +} diff --git a/eval/ast/fd889725-57f6-4584-bcb4-403eb16602ee.cpp b/eval/ast/fd889725-57f6-4584-bcb4-403eb16602ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f26d27a4873543fe8da8b94578f1c552f6864f --- /dev/null +++ b/eval/ast/fd889725-57f6-4584-bcb4-403eb16602ee.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +const int MAXN=100000; + +char sir[MAXN]; + +struct Nod + +{ + + Nod *st, *dr; + + int val; + + char op; + + Nod(char _op, Nod *_st, Nod *_dr) + + { + + op=_op; + + st=_st; + + dr=_dr; + + } + +}; + +Nod* construct (int st, int dr) + +{ + + int par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='+' || sir[i]=='-')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + par=0; + + for(int i=dr; i>=st; i--) + + { + + if(sir[i]==')') par++; + + else if(sir[i]=='(') par--; + + else if(par==0 && (sir[i]=='*' || sir[i]=='/')) + + return new Nod(sir[i], construct(st, i-1), construct(i+1, dr)); + + } + + if(sir[st]=='(') return construct(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+sir[i]-'0'; + + Nod*rez=new Nod ('?', nullptr, nullptr); + + rez->val=nr; + + return rez; + +} + +int eval (Nod* root) + +{ + + if(root->op == '?') + + return root->val; + + int x=eval(root->st); + + int y=eval(root->dr); + + switch(root->op){ + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int main() + +{ + + fin>>sir; + + Nod* root = construct (0, strlen(sir)-1); + + fout< +using namespace std; + + + +struct arbore + +{ + + char tip; + + int val; + + char op; + + arbore *st, *dr; + +}; + + + +arbore* St_Nod[100001]; + +char St_Op[100001]; + +int idx_expr = 0, idx_op = -1, idx_nod = -1; + + + + + + + +int prioritate(char a, char b) + +{ + + if(a == '*' || a == '/') + + if(b == '+' || b == '-') + + return 0; + + return 1; + +} + + + + + + + +void realizare_operatie() + +{ + + arbore* nod = new arbore; + + nod->tip = 'O'; + + nod->op = St_Op[idx_op--]; + + nod->dr = St_Nod[idx_nod--]; + + nod->st = St_Nod[idx_nod--]; + + + + St_Nod[++idx_nod] = nod; + +} + + + +arbore* Formare(char* expr) + +{ + + while(expr[idx_expr] != '\0') + + { + + if(expr[idx_expr] == '(') + + { + + St_Op[++idx_op] = '('; + + idx_expr++; + + } + + if(expr[idx_expr] == ')') + + { + + while(St_Op[idx_op] != '(') + + realizare_operatie(); + + idx_op--; + + idx_expr++; + + } + + else if(expr[idx_expr] < '0') + + { + + while( idx_op > -1 && St_Op[idx_op] != '(' && prioritate(expr[idx_expr], St_Op[idx_op])) + + realizare_operatie(); + + St_Op[++idx_op] = expr[idx_expr]; + + idx_expr++; + + } + + else + + { + + int NR = 0; + + while(expr[idx_expr] >= '0' && expr[idx_expr] <= '9') + + { + + NR = NR * 10 + (expr[idx_expr] - '0'); + + idx_expr++; + + } + + arbore* nod = new arbore; + + nod->tip = 'V'; + + nod->val = NR; + + nod->st = NULL; + + nod->dr = NULL; + + St_Nod[++idx_nod] = nod; + + } + + } + + + + while(idx_op > -1) + + realizare_operatie(); + + + + return St_Nod[0]; + +} + + + +int Eval(arbore* nod) + +{ + + if(nod->tip == 'V') + + return nod->val; + + else + + { + + int STANGA = Eval(nod->st); + + int DREAPTA = Eval(nod->dr); + + switch (nod->op) + + { + + case '+': + + return STANGA + DREAPTA; + + case '-': + + return STANGA - DREAPTA; + + case '*': + + return STANGA * DREAPTA; + + case '/': + + return STANGA / DREAPTA; + + } + + return 0; + + } + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + char EXPR[100001]; + + + + f.getline(EXPR, 100001 + 1); + + arbore* TREE = Formare(EXPR); + + + + ofstream g("evaluare.out"); + + g << Eval(TREE); + + return 0; + +} diff --git a/eval/recursive/00b831ac-2ed1-4938-8d41-ce878c770e63.cpp b/eval/recursive/00b831ac-2ed1-4938-8d41-ce878c770e63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e94ef453227b5b925ea3ec1a5add916f4d238732 --- /dev/null +++ b/eval/recursive/00b831ac-2ed1-4938-8d41-ce878c770e63.cpp @@ -0,0 +1,232 @@ +#include + + +using namespace std; + + + +const int L_MAX = 100000; + + + +int p[L_MAX]; + + + +stack st; + + + +string elimparan (string s) + +{ + + for(int i = 0; i < s.size(); i++) + + { + + if(s[i] == '(') + + st.push(i); + + if(s[i] == ')') + + { + + p[i] = st.top(); + + st.pop(); + + } + + } + + int l = 0, r = s.size(); + + while(l < r && s[r - 1] == ')' && p[r - 1] == l) + + { + + l++; + + r--; + + } + + return s.substr(l, r - l); + +} + + + +int eval (string s); + + + +int term (string s) + +{ + + if(s.empty()) + + return 0; + + int prev = s.size(); + + s = elimparan(s); + + if(s.size() != prev) + + return eval(s); + + char op = '*'; + + int oppos = -1; + + int paran = 0; + + int answer = 1; + + for(int i = 0; i < s.size(); i++) + + { + + if(s[i] == '(') + + paran++; + + if(paran == 0 && (s[i] == '*' || s[i] == '/')) + + { + + if(op == '*') + + answer *= eval(s.substr(oppos + 1, i - oppos - 1)); + + else + + answer /= eval(s.substr(oppos + 1, i - oppos - 1)); + + oppos = i; + + op = s[i]; + + } + + if(s[i] == ')') + + paran--; + + } + + if(op == '*') + + answer *= eval(s.substr(oppos + 1, s.size() - oppos - 1)); + + else + + answer /= eval(s.substr(oppos + 1, s.size() - oppos - 1)); + + return answer; + +} + + + +int eval (string s) + +{ + + if(s.empty()) + + return 0; + + s = elimparan(s); + + bool num = true; + + for(int i = 0; i < s.size(); i++) + + if(!isdigit(s[i])) + + num = false; + + if(num == true) + + { + + int answer = 0; + + for(int i = 0; i < s.size(); i++) + + answer = answer * 10 + s[i] - '0'; + + return answer; + + } + + int sgn = +1; + + int sgnpos = -1; + + int answer = 0; + + int paran = 0; + + for(int i = 0; i < s.size(); i++) + + { + + if(s[i] == '(') + + paran++; + + if(paran == 0 && (s[i] == '+' || s[i] == '-')) + + { + + answer += sgn * term(s.substr(sgnpos + 1, i - sgnpos - 1)); + + sgnpos = i; + + if(s[i] == '+') + + sgn = +1; + + else + + sgn = -1; + + } + + if(s[i] == ')') + + paran--; + + } + + answer += sgn * term(s.substr(sgnpos + 1, s.size() - sgnpos - 1)); + + return answer; + +} + + + +int main() + +{ + + ifstream fin ("evaluare.in"); + + ofstream fout ("evaluare.out"); + + string s; + + fin >> s; + + fout << eval(s) << "\n"; + + return 0; + +} diff --git a/eval/recursive/00f4b737-e6ea-44be-ba22-828afcbfd4c9.cpp b/eval/recursive/00f4b737-e6ea-44be-ba22-828afcbfd4c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142de0384b55720d3347b3b68ab7eb6c76aa00e1 --- /dev/null +++ b/eval/recursive/00f4b737-e6ea-44be-ba22-828afcbfd4c9.cpp @@ -0,0 +1,201 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int evaluate(char* &expr); + +int ADD_SUBSTRACT(char*& expr); + +int MULTIPLY_DIVIDE(char* &expr); + +int operand(char* &expr); + +int value(char* &expr); + + + +char s[100001]; + + + +int main() + +{ + + char *expr; + + + + fin >> s; + + expr = s; + + fout << evaluate(expr); + + return 0; + +} + + + +int evaluate(char* &expr) + +{ + + return ADD_SUBSTRACT(expr); + +} + + + +int ADD_SUBSTRACT(char*& expr) + +{ + + auto result = MULTIPLY_DIVIDE(expr); + + + + while (expr[0] == '+' || expr[0] == '-') + + { + + switch (expr[0]) + + { + + case '+': + + result += MULTIPLY_DIVIDE(++expr); + + break; + + case '-': + + result -= MULTIPLY_DIVIDE(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int MULTIPLY_DIVIDE(char* &expr) + +{ + + auto result = operand(expr); + + + + while (expr[0] == '*' || expr[0] == '/') + + { + + switch (expr[0]) + + { + + case '*': + + result *= operand(++expr); + + break; + + case '/': + + result /= operand(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int operand(char* &expr) + +{ + + if (expr[0] == '(') + + { + + auto result = evaluate(++expr); + + expr++; + + return result; + + } + + else + + { + + return value(expr); + + } + +} + + + +int value(char* &expr) + +{ + + int nr = 0; + + + + while (isdigit(expr[0])) + + { + + nr = nr * 10 + (expr[0] - '0'); + + expr++; + + } + + + + return nr; + +} diff --git a/eval/recursive/01fa6e27-e747-4153-96e0-5688782853a1.cpp b/eval/recursive/01fa6e27-e747-4153-96e0-5688782853a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aeae1955d3d3d54f157e867f622ba3bcf75020c7 --- /dev/null +++ b/eval/recursive/01fa6e27-e747-4153-96e0-5688782853a1.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +using namespace std; + +char expr[100005]; + +long lg,i; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +long evaluare_expresie(); + +long evaluare_factor() + +{ + + long f=0; + + if (expr[i] == '(') + + { + + i++; + + f = evaluare_expresie(); + + i++; + + } + + else + + while (i < lg && !strchr("+-*/()", expr[i])) + + { + + f = f * 10 + (expr[i] - '0'); + + i++; + + } + + return f; + +} + + + +long evaluare_termen() + +{ + + long T = evaluare_factor(); + + while (i < lg && strchr("*/", expr[i])) + + { + + char semn = expr[i]; + + i++; + + if (semn == '*') + + T *= evaluare_factor(); + + else + + T /= evaluare_factor(); + + } + + return T; + + + +} + + + +long evaluare_expresie() + +{ + + long E = evaluare_termen(); + + while (i < lg && strchr("+-", expr[i])) + + { + + char semn = expr[i]; + + i++; + + if (semn == '+') + + E += evaluare_termen(); + + else + + E -= evaluare_termen(); + + + + + + } + + return E; + +} + + + +int main() + +{ + + f.getline(expr, 100005); + + lg = strlen(expr); + + g << evaluare_expresie(); + + return 0; + +} diff --git a/eval/recursive/028e7968-d8b7-4694-ac63-40f40974a84e.cpp b/eval/recursive/028e7968-d8b7-4694-ac63-40f40974a84e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92b6cad6f4b6bd21d82793d3c332ba91529b1b67 --- /dev/null +++ b/eval/recursive/028e7968-d8b7-4694-ac63-40f40974a84e.cpp @@ -0,0 +1,104 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char *p,e[100010]; + +int suma(),termen(),factor(),numar(); + +int main() + +{ + + f>>e; + + p=e; + + g< +#include + + +using namespace std; + + + +string sir; + +int poz; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int val = termen(); + + + + while (sir[poz] == '+' || sir[poz] == '-') + + { + + if (sir[poz] == '+') + + { + + poz++; + + val += termen(); + + } + + else + + { + + poz++; + + val -= termen(); + + } + + } + + + + return val; + +} + + + +int termen() + +{ + + int val = factor(); + + + + while (sir[poz] == '*' || sir[poz] == '/') + + { + + if (sir[poz] == '*') + + { + + poz++; + + val *= factor(); + + } + + else + + { + + poz++; + + val /= factor(); + + } + + } + + + + return val; + +} + + + +int factor() + +{ + + int val = 0; + + + + if (sir[poz] == '(') + + { + + poz++; + + val = expresie(); + + poz++; + + } + + else + + { + + while (poz < sir.size() && '0' <= sir[poz] && sir[poz] <= '9') + + { + + val = val * 10 + (int)(sir[poz] - '0'); + + poz++; + + } + + } + + + + return val; + +} + + + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + + + in >> sir; + + + + out << expresie(); + + + + return 0; + +} diff --git a/eval/recursive/04685d63-f4fd-4248-b596-2b2270c28786.cpp b/eval/recursive/04685d63-f4fd-4248-b596-2b2270c28786.cpp new file mode 100644 index 0000000000000000000000000000000000000000..548eb4639657e69894fdf94e2278b26ed4186d89 --- /dev/null +++ b/eval/recursive/04685d63-f4fd-4248-b596-2b2270c28786.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; +const int N = 100001; +int p; +char s[N]; +int expresie(); +int termen(); +int factor(); +int expresie() { + int sum = termen(); + while ( s[p] == '+' || s[p] == '-' ) { + if ( s[p] == '+' ) { + p++; + sum += termen(); + } else { + p++; + sum -= termen(); + } + } + return sum; +} +int termen () { + int prod = factor(); + while ( s[p] == '*' || s[p] == '/' ) { + if ( s[p] == '*' ) { + p++; + prod *= factor(); + } + if ( s[p] == '/' ) { + p++; + prod /= factor(); + } + } + return prod; +} +int factor () { + int val = 0, semn = 1; + while ( s[p] == '-' ) { + semn = -semn; + p++; + } + if ( s[p] == '(' ) { + p++; + val = expresie(); + p++; + return semn * val; + } + while ( s[p] >= '0' && s[p] <= '9' ) + val = val * 10 + ( s[p++] - '0' ); + return semn * val; +} +ifstream cin ( "evaluare.in" ); +ofstream cout ( "evaluare.out" ); +int main() { + cin.getline( s, N ); + cout << expresie(); + return 0; +} diff --git a/eval/recursive/0508f891-e3d9-4052-91de-0a43ef3b1c5f.cpp b/eval/recursive/0508f891-e3d9-4052-91de-0a43ef3b1c5f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e04357f1a632f63443647d40d08328cc82e3eed --- /dev/null +++ b/eval/recursive/0508f891-e3d9-4052-91de-0a43ef3b1c5f.cpp @@ -0,0 +1,186 @@ +#include + + +using namespace std; + + + +ifstream cin ("evaluare.in") ; + +ofstream cout ("evaluare.out") ; + + + +char s[100005] ; + +int poz ; + + + +int eval () ; + +int produs () ; + +int termen () ; + + + +int eval () + +{ + + + + int aux = produs () ; + + + + while (s[poz] == '+' || s[poz] == '-') + + { + + + + if (s[poz++] == '+') + + aux += produs() ; + + + + else + + aux -= produs() ; + + + + } + + + + return aux ; + + + +} + + + +int produs () + +{ + + + + int aux = termen() ; + + + + while (s[poz] == '*' || s[poz] == '/') + + { + + + + if (s[poz++] == '*') + + aux *= termen() ; + + + + else + + aux /= termen() ; + + + + } + + + + return aux ; + + + +} + + + +int termen() + +{ + + + + int aux = 0 ; + + + + if (s[poz] == '(') + + { + + + + poz++ ; + + aux = eval() ; + + poz++ ; + + + + return aux ; + + + + } + + + + while (s[poz] >='0' && s[poz] <= '9') + + aux = aux * 10 + (s[poz++] - '0'); + + + + return aux ; + + + +} + + + +void rez () + +{ + + + + cin >> s ; + + + + cout << eval() ; + + + +} + + + +int main() + +{ + + + + rez() ; + + + + return 0; + + + +} diff --git a/eval/recursive/0534e335-6de4-4fc3-b516-14ab89052f11.cpp b/eval/recursive/0534e335-6de4-4fc3-b516-14ab89052f11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfb243b60d5f83b5607e6c7c4f33fb31b745b616 --- /dev/null +++ b/eval/recursive/0534e335-6de4-4fc3-b516-14ab89052f11.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + + + + + +char Sequence[100000], *P = Sequence; + + + +long Termen(); + +long Factor(); + + + +long Solve() { + + long result = Termen(); + + + + while (*P == '+' || *P == '-') + + switch (*P) { + + case '+': + + P++; + + result += Termen(); + + break; + + + + case '-': + + P++; + + result -= Termen(); + + break; + + } + + + + return result; + +} + + + +long Termen() { + + long result = Factor(); + + + + while (*P == '*' || *P == '/') + + switch (*P) { + + case '*': + + P++; + + result *= Factor(); + + break; + + + + case '/': + + P++; + + result /= Factor(); + + break; + + } + + + + return result; + +} + + + +long Factor() { + + long result = 0; + + + + if (*P == '(') { + + P++; + + result = Solve(); + + P++; + + } + + else + + while (*P >= '0' && *P <= '9') { + + result = result * 10 + *P - '0'; + + P++; + + } + + + + return result; + +} + + + +int main() { + + fin >> Sequence; + + fout << Solve(); + +} diff --git a/eval/recursive/05d38820-3363-49e4-a968-3d165fc4e3d0.cpp b/eval/recursive/05d38820-3363-49e4-a968-3d165fc4e3d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87d22ea64c96897cefa64912047b81a6d388e606 --- /dev/null +++ b/eval/recursive/05d38820-3363-49e4-a968-3d165fc4e3d0.cpp @@ -0,0 +1,112 @@ +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005]; + +int idx; + + + +int Expresie(); + +int Termen(); + +int Factor(); + + + +int Expresie() + +{ + + idx++; + + int ret = Termen(); + + while (s[idx] == '+' || s[idx] == '-') + + if (s[idx++] == '+') + + ret += Termen(); + + else + + ret -= Termen(); + + idx++; + + return ret; + +} + + + +int Termen() + +{ + + int ret = Factor(); + + while (s[idx] == '*' || s[idx] == '/') + + if (s[idx++] == '*') + + ret *= Factor(); + + else + + ret /= Factor(); + + return ret; + +} + + + +int Factor() + +{ + + int ret = 0; + + if (s[idx] == '(') + + ret = Expresie(); + + while ('0' <= s[idx] && s[idx] <= '9') + + ret = ret * 10 + s[idx++] - '0'; + + return ret; + +} + + + +int main() + +{ + + fin >> (s + 1); + + s[0] = '('; + + s[strlen(s)] = ')'; + + fout << Expresie(); + + return 0; + +} diff --git a/eval/recursive/05e187f5-e4f6-4dc2-9b38-753016a72e45.cpp b/eval/recursive/05e187f5-e4f6-4dc2-9b38-753016a72e45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c814f623d8d38a9eb7c19c686a111504c75df5b4 --- /dev/null +++ b/eval/recursive/05e187f5-e4f6-4dc2-9b38-753016a72e45.cpp @@ -0,0 +1,162 @@ +#include +#include +#include + + + + +using namespace std; + + + +char s[100011]; + +char* p = s; + + + +char peek() + +{ + + return* p; + +} + + + +char get() + +{ + + return *p++; + +} + + + +int termen() + +{ + + int result = get() - '0'; + + while (peek() >= '0' && peek() <= '9') + + { + + result = result * 10 + (get() - '0'); + + } + + return result; + +} + + + +int expression(); + + + +int factor() + +{ + + if (peek() >= '0' && peek() <= '9') + + return termen(); + + else if (peek() == '(') + + { + + get(); + + int result = expression(); + + get(); + + return result; + + } + + else if (peek() == '-') + + { + + get(); + + return -factor(); + + } + + return 0; + +} + + + +int term() + +{ + + int result = factor(); + + while (peek() == '*' || peek() == '/') + + { + + if (get() == '*') + + result *= factor(); + + else + + result /= factor(); + + } + + return result; + +} + + + +int expression() + +{ + + int result = term(); + + while (peek() == '+' || peek() == '-') + + { + + if (get() == '+') + + result += term(); + + else + + result -= term(); + + } + + return result; + +} + + + +int main() + +{ + + if(fgets(s, 100011, fopen("evaluare.in", "r")) != NULL); + + fprintf(fopen("evaluare.out", "w"), "%d\n", expression()); + + return 0; + +} diff --git a/eval/recursive/05ef9c44-6753-4ed9-972f-1874a77a622a.cpp b/eval/recursive/05ef9c44-6753-4ed9-972f-1874a77a622a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fed5f7b196418648c80aae5c6e89e39f4911767e --- /dev/null +++ b/eval/recursive/05ef9c44-6753-4ed9-972f-1874a77a622a.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100005; + +int n, p; + +char s[NMAX]; + +int expr(), termen(), factor(); + + + +int expr() + +{ + + int x = termen(); + + while (p <= n && (s[p] == '+' || s[p] == '-')) + + { + + ++p; + + if (s[p - 1] == '+') + + x += termen(); + + else + + x -= termen(); + + } + + return x; + +} + + + +int termen() + +{ + + int x = factor(); + + while (p <= n && (s[p] == '*' || s[p] == '/')) + + { + + ++p; + + if (s[p - 1] == '*') + + x *= factor(); + + else + + x /= factor(); + + } + + return x; + +} + + + +int factor() + +{ + + int x = 0; + + if (s[p] == '(') + + { + + ++p; + + x = expr(); + + ++p; + + } + + else + + { + + while (p <= n && ('0' <= s[p] && s[p] <= '9')) + + x = x * 10 + (s[p++] - '0'); + + } + + return x; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> (s + 1); + + n = strlen(s + 1); + + p = 1; + + fout << expr() << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/recursive/065be7ec-9dcc-4781-b99c-2797e15abe24.cpp b/eval/recursive/065be7ec-9dcc-4781-b99c-2797e15abe24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdd26cc02375eb14c621190a650a3f446cc96c17 --- /dev/null +++ b/eval/recursive/065be7ec-9dcc-4781-b99c-2797e15abe24.cpp @@ -0,0 +1,84 @@ +#include +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +const int N=100001; + +char s[N]; +int p; + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+'||s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*' || s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0'&&s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} + +int main() +{ + f>>s; + g< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/07497703-130a-448f-b640-9f54e04650a6.cpp b/eval/recursive/07497703-130a-448f-b640-9f54e04650a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e86a43a01d8e1f07f7296535833c725be6266b92 --- /dev/null +++ b/eval/recursive/07497703-130a-448f-b640-9f54e04650a6.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + +char s[100005]; + +int i; + +int expresie(); + +int factor() { + + int ret=0; + + if (s[i]=='(') { + + ++i; + + ret=expresie(); + + ++i; + + return ret; + + } + + + + while (s[i]>='0' && s[i]<='9') + + ret=ret*10+s[i]-'0',++i; + + return ret; + +} + +int termen() { + + int ret=factor(); + + while (s[i]=='*' || s[i]=='/') { + + if (s[i]=='*') ++i,ret*=factor(); + + else ++i,ret/=factor(); + + } + + return ret; + +} + +int expresie() { + + int ret=termen(); + + while (s[i]=='+' || s[i]=='-') { + + if (s[i]=='+') ++i,ret+=termen(); + + else ++i,ret-=termen(); + + } + + + + return ret; + +} + +int main() + +{ + + ifstream f("evaluare.in"); + + ofstream g("evaluare.out"); + + f.get(s,100005); + + g< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/09b2560a-6655-4c3e-b1ef-0d96d63f49fa.cpp b/eval/recursive/09b2560a-6655-4c3e-b1ef-0d96d63f49fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3526a713794504096dae81049cc2a71a864f075 --- /dev/null +++ b/eval/recursive/09b2560a-6655-4c3e-b1ef-0d96d63f49fa.cpp @@ -0,0 +1,237 @@ +#include +#include +#include +#include + + +struct expression + +{ + + int i; + + std::string e; + + + + expression(std::string e) : i(0), e(e) {} + + + + int E() + + { + + int r = P(); + + + + while (i < e.size() && e[i] == '+' || e[i] == '-') + + { + + if (e[i] == '+') + + { + + i++; + + r += P(); + + } + + else + + { + + i++; + + r -= P(); + + } + + } + + + + return r; + + } + + + + int P() + + { + + int r = T(); + + + + while (i < e.size() && e[i] == '*' || e[i] == '/') + + { + + if (e[i] == '*') + + { + + i++; + + r *= T(); + + } + + else + + { + + i++; + + r /= T(); + + } + + } + + + + return r; + + } + + + + int T() + + { + + if (e[i] == '(') + + { + + i++; + + int r = E(); + + i++; + + + + return r; + + } + + + + return C(); + + } + + + + int C() + + { + + std::string number; + + + + while (i < e.size() && std::isdigit(e[i])) + + { + + number += e[i]; + + i++; + + } + + + + return std::stoi(number); + + } + + + + int eval() + + { + + i = 0; + + return E(); + + } + +}; + + + +int solve(const std::string &e) + +{ + + return expression(e).eval(); + +} + + + +std::string read(); + +void write(int results); + + + +int main() + +{ + + auto e = read(); + + auto result = solve(e); + + write(result); + + + + return 0; + +} + + + +std::string read() + +{ + + std::ifstream fin("evaluare.in"); + + + + std::string s; + + fin >> s; + + + + return s; + +} + + + +void write(int result) + +{ + + std::ofstream fout("evaluare.out"); + + + + fout << result << '\n'; + +} diff --git a/eval/recursive/09f9a17b-d530-476f-8352-39e60b998dd8.cpp b/eval/recursive/09f9a17b-d530-476f-8352-39e60b998dd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97c24faf6cfd199cd7b983950c3ea90b7a8d712c --- /dev/null +++ b/eval/recursive/09f9a17b-d530-476f-8352-39e60b998dd8.cpp @@ -0,0 +1,126 @@ +#include +#include +using namespace std; +string expresion; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); +char is_digit(char a) +{ + return a>='0'&&a<='9'; +} +int get_number(int&to_start) +{ + int f=0; + for(; is_digit(expresion[to_start]); to_start++) + { + f=f*10+(expresion[to_start]-'0'); + } + to_start--; + return f; + +} +void encapsulate(string&a) +{ + a='('+a+')'; +} +bool is_operator(char a) +{ + return a=='+'||a=='-'; +} +bool is_operator2(char a) +{ + return a=='*'||a=='/'; +} +void evaluate(int&last,int now,char oper) +{ + switch(oper) + { + case '+': + last+=now; + break; + case '-': + last-=now; + break; + case '/': + last/=now; + break; + case '*': + last*=now; + break; + + } + +} +int get_factor(int&to_start); +int solve_paranthesis(int&to_start); +int get_factor(int&to_start) +{ + int first; + if(expresion[to_start]!='(') + { + first=get_number(to_start); + to_start++; + } + else + { + to_start++; + first=solve_paranthesis(to_start); + to_start++; + + } + while(is_operator2(expresion[to_start])) + { + char last=expresion[to_start]; + to_start++; + if(is_digit(expresion[to_start])) + { + int num=get_number(to_start); + evaluate(first,num,last); + } + else + { + if(expresion[to_start]=='(') + { + to_start++; + int p=solve_paranthesis(to_start); + evaluate(first,p,last); + + + } + + } + to_start++; + } + to_start--; + return first; + + +} +int solve_paranthesis(int&to_start) +{ + char last_operator='+'; + int last_result=0; + for(; expresion[to_start]!=')'; to_start++) + { + if(!is_operator(expresion[to_start])) + { + int p=get_factor(to_start); + evaluate(last_result,p,last_operator); + } + else + { + last_operator=expresion[to_start]; + } + } + return last_result; + + + +} +int main() +{ + in>>expresion; + encapsulate(expresion); + int cursor=1; + out< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +int solve (); + + + +int term (); + + + +int fact (); + + + +char s[100137]; + + + +int poz; + + + +int main() + +{ + + in >> s; + + out << solve (); + + return 0; + +} + + + +int solve () + +{ + + int nr = term (); + + while ( s[poz] == '-' || s[poz] == '+' ) + + if ( s[poz] == '+' ) + + { + + ++poz; + + nr += term (); + + } + + else + + { + + ++poz; + + nr -= term (); + + } + + return nr; + +} + + + +int term () + +{ + + int nr = fact(); + + while ( s[poz] == '*' || s[poz] == '/' ) + + if ( s[poz] == '*' ) + + { + + ++poz; + + nr *= fact(); + + } + + else + + { + + ++poz; + + nr /= fact(); + + } + + return nr; + +} + + + +int fact () + +{ + + int nr = 0; + + if ( s[poz] == '(' ) + + { + + ++poz; + + nr = solve(); + + ++poz; + + return nr; + + } + + while ( s[poz] >= '0' && s[poz] <= '9' ) + + { + + nr = nr * 10 + s[poz] - '0'; + + ++poz; + + } + + return nr; + +} diff --git a/eval/recursive/0bcc6e5d-1f3f-4eca-ac85-3da974ae3e03.cpp b/eval/recursive/0bcc6e5d-1f3f-4eca-ac85-3da974ae3e03.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea587d999ffe4b1462bb72c16e5198f9d3c1ffd0 --- /dev/null +++ b/eval/recursive/0bcc6e5d-1f3f-4eca-ac85-3da974ae3e03.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100005], *p; + + + +int termen(); + +int factor(); + + + +int eval() + +{ + + int rez=termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p=='+') + + p++,rez+=termen(); + + else + + p++,rez-=termen(); + + + + } + + return rez; + +} + + + +int termen() + +{ + + int rez=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + p++, rez*=factor(); + + else + + p++,rez/=factor(); + + } + + return rez; + +} + + + +int factor() + +{ + + int rez=0; + + + + if(*p=='(') + + p++,rez= eval(),p++; + + else + + { + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + } + + return rez; + + + +} + +int main() + +{ + + cin >> s; + + p=s; + + cout< + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char sir[100006],*pointersir=sir; + +long long termen (); + +long long factor (); + +long long eval () + +{ + + long long r=termen(); + + while(*pointersir=='+' || *pointersir=='-') + + { + + if(*pointersir=='+') + + { + + pointersir++; + + r=r+termen(); + + } + + else + + { + + pointersir++; + + r=r-termen(); + + } + + } + + return r; + +} + +long long termen () + +{ + + long long r=factor(); + + while(*pointersir=='*' || *pointersir=='/') + + { + + if(*pointersir=='*') + + { + + pointersir++; + + r=r*factor(); + + } + + else + + { + + pointersir++; + + r=r/factor(); + + } + + } + + return r; + +} + +long long factor () + +{ + + long long r=0; + + if(*pointersir=='(') + + { + + pointersir++; + + r=eval(); + + pointersir++; + + } + + else + + while(*pointersir<='9' && *pointersir>='0') + + r=r*10+*pointersir-'0',pointersir++; + + return r; + +} + +int main () + +{ + + fin.getline(sir,100001); + + fout< + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +const int dim = 100005; + +int current; +char a[dim]; + +int evaluare(); +int termen(); +int factor(); + +bool isOp1(char c) +{ + return (c == '+' || c == '-'); +} + + +bool isOp2(char c) +{ + return (c == '*' || c == '/'); +} + +bool isDigit(char c) +{ + if(c >= '0' && c <='9') + return true; + return false; +} + +int getSign(char c) +{ + if(c == '-') + return -1; + else if(c == '+') + return 1; + return 0; +} + +int getDigit(char c) +{ + return c - '0'; +} + +int evaluare() +{ + int rez = termen(), sign = 0; + while((sign = getSign(a[current])) != 0) + { + current++; + rez += sign * termen(); + } + return rez; +} + +int termen() +{ + int rez = factor(); + while(isOp2(a[current])) + { + if(a[current] == '*') + current++, rez *= factor(); + else if(a[current] == '/') + current++, rez /= factor(); + } + return rez; +} + +int factor() +{ + int rez = 0, sign = 1; + while(a[current] == '-') + { + current++; + sign *= -1; + } + + if(a[current] == '(') + { + current++; + rez = evaluare(); + current++; + return rez * sign; + } + + while(isDigit(a[current])) + { + rez = rez * 10 + getDigit(a[current]); + current++; + } + + return rez * sign; +} + +int main() +{ + cin>>a; + int rez = evaluare(); + + cout< +#include +#include +#include + + +constexpr int N = 100000; + +char s[N + 1]; + +char *p = s; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() { + + int sum = termen(); + + while (*p && strchr("+-", *p)) { + + ++p; + + if (p[-1] == '+') { + + sum += termen(); + + } + + else { + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() { + + int prod = factor(); + + while (*p && strchr("/*", *p)) { + + ++p; + + if (p[-1] == '*') { + + prod *= factor(); + + } + + else { + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() { + + int val = 0; + + int sign = 1; + + while (*p == '-') { + + sign = -sign; + + ++p; + + } + + if (*p == '(') { + + ++p; + + val = expresie(); + + ++p; + + return sign * val; + + } + + while (isdigit(*p)) { + + val = val * 10 + *p - '0'; + + ++p; + + } + + return sign * val; + +} + + + +int main() { + + std::ifstream in("evaluare.in"); + + std::ofstream out("evaluare.out"); + + in.getline(s, sizeof s); + + out << expresie() << '\n'; + +} diff --git a/eval/recursive/0ce4dbdb-d524-45d2-83fb-7f7595879d0f.cpp b/eval/recursive/0ce4dbdb-d524-45d2-83fb-7f7595879d0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdec77553c3d88114b9bc66d627426fc2457b0d6 --- /dev/null +++ b/eval/recursive/0ce4dbdb-d524-45d2-83fb-7f7595879d0f.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +char s[100001], *p=s; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +long long eval(); +long long termen(); +long long factor(); + +long long eval(){ + long long r=termen(); + while (*p=='+' || *p=='-'){ + if (*p=='+'){ + ++p; + r=r+termen(); + } + else{ + ++p; + r=r-termen(); + } + } + return r; +} + +long long termen(){ + long long r=factor(); + while (*p=='*' || *p=='/'){ + if (*p=='*'){ + ++p; + r=r*factor(); + } + else{ + ++p; + r=r/factor(); + } + } + return r; +} + +long long factor(){ + long long r=0; + if (*p=='('){ + ++p; + r=eval(); + ++p; + } + else{ + while (*p>='0' && *p<='9'){ + r=r*10+*p-'0'; + ++p; + } + } + return r; +} + + +int main(){ + cin>>s; + cout< +#include +using namespace std; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + + +string to_calculate; + +int get_factor(int&cursor); +int solve_parentheses(int&cursor); + + + +bool is_digit(char character) +{ + return character>='0' &&character<='9'; +} + +int to_digit(char character) +{ + return character-'0'; + +} + + +bool is_operator(char c) +{ + return c=='+'||c=='-'; + +} + +bool is_factor(char c) +{ + return c=='*' ||c=='/'; +} + +int get_next_number(int&cursor) +{ + int to_return=0; + + while(cursor>to_calculate; + to_calculate='('+to_calculate+')'; + +} + + +int main() +{ + read(); + int cursor=1; + out< + + +using namespace std; + +char s[100001], *p; + +int eval(); + +int termen(); + +int factor(); + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int main() + +{ + + f.getline(s , 100001); + + p=s; + + int rez=eval(); + + g<=*p) + + { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + + } diff --git a/eval/recursive/0fa5441d-d467-49ce-ab74-c958ab68a12e.cpp b/eval/recursive/0fa5441d-d467-49ce-ab74-c958ab68a12e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2495fc182373b61cd8ac30e4a906fde2dcd15183 --- /dev/null +++ b/eval/recursive/0fa5441d-d467-49ce-ab74-c958ab68a12e.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +const int N = 1e5; + + + +char s[N + 1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() { + + int sum = termen(); + + while (s[p] == '+' || s[p] == '-') { + + if (s[p] == '+') { + + ++p; + + sum += termen(); + + } + + else { + + ++p; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() { + + int prod = factor(); + + while (s[p] == '*' || s[p] == '/') { + + if (s[p] == '*') { + + ++p; + + prod *= factor(); + + } + + else { + + ++p; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() { + + int semn = 1, val = 0; + + if (s[p] == '-') { + + semn = -1; + + ++p; + + } + + if (s[p] == '(') { + + ++p; + + val = expresie(); + + ++p; + + return val * semn; + + } + + while (isdigit(s[p])) { + + val = val * 10 + (s[p] - '0'); + + ++p; + + } + + return semn * val; + +} + + + +int main() { + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + + + in >> s; + + out << expresie(); + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/0fad00c2-c3d1-4514-befc-e0d3d0bae8de.cpp b/eval/recursive/0fad00c2-c3d1-4514-befc-e0d3d0bae8de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..274b08581d86efb4cb4aca6e0bf1cfb3bf8f0ee3 --- /dev/null +++ b/eval/recursive/0fad00c2-c3d1-4514-befc-e0d3d0bae8de.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +char s[100050],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval( ) + +{ + + int r=termen(); + + while(*p=='+'||*p=='-') + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else { + + p++; + + r-=termen(); + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0'&&*p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + cin.sync_with_stdio(0); + + cin.tie(0); + + cin.getline(s,100000); + + p=s; + + cout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int MAX_N = 1e5 + 5; + + + +char s[MAX_N]; + + + +int p; + + + +int term(); + +int fact(); + +int expres(); + + + +int fact() { + + int sign, value; + + value = 0; + + sign = 1; + + if (s[p] == '-') { + + ++p; + + sign = - 1; + + } + + if (s[p] == '(') { + + ++p; + + value = expres(); + + ++p; + + } else { + + while (isdigit(s[p]) > 0) { + + value = value * 10 + s[p] - '0'; + + ++p; + + } + + } + + return value * sign; + +} + + + +int term() { + + int value; + + value = fact(); + + while (s[p] == '*' || s[p] == '/') { + + if (s[p] == '*') { + + ++p; + + value *= fact(); + + } else if (s[p] == '/') { + + ++p; + + value /= fact(); + + } + + } + + return value; + +} + + + +int expres() { + + int value; + + value = term(); + + while (s[p] == '+' || s[p] == '-') { + + if (s[p] == '+') { + + ++p; + + value += term(); + + } else if (s[p] == '-') { + + ++p; + + value -= term(); + + } + + } + + return value; + +} + + + +int main() { + + fin >> s; + + fout << expres(); + + return 0; + +} diff --git a/eval/recursive/1149f16b-a08c-45de-9029-fb3e0665b51a.cpp b/eval/recursive/1149f16b-a08c-45de-9029-fb3e0665b51a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32ee9bf496c3c44a3ad6ac0a83b28f400298e7a2 --- /dev/null +++ b/eval/recursive/1149f16b-a08c-45de-9029-fb3e0665b51a.cpp @@ -0,0 +1,127 @@ +#include +#include +using namespace std; + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); + + + +string s; + +int i; + + + +int termen(); + +int factor(); + +int expresie(); + + + +int termen() { + + int ans = factor(); + + while (s[i] == '+' || s[i] == '-') { + + if (s[i] == '+') { + + i++; + + ans += factor(); + + } + + else { + + i++; + + ans -= factor(); + + } + + } + + return ans; + +} + + + +int factor() { + + int ans = expresie(); + + while (s[i] == '*' || s[i] == '/') { + + if (s[i] == '*') { + + i++; + + ans *= expresie(); + + } + + else { + + i++; + + ans /= expresie(); + + } + + } + + return ans; + +} + + + +int expresie() { + + int ans = 0; + + if (s[i] == '(') { + + i++; + + ans = termen(); + + i++; + + } + + else { + + while (i < s.size() && '0' <= s[i] && s[i] <= '9') { + + ans *= 10; + + ans += s[i] - '0'; + + i++; + + } + + } + + return ans; + +} + + + +int main() { + + cin >> s; + + cout << termen(); + + return 0; + +} diff --git a/eval/recursive/115a33d3-868e-42f8-b4ae-1d71dbff310b.cpp b/eval/recursive/115a33d3-868e-42f8-b4ae-1d71dbff310b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f0f7ed9b02b45cdfd4d0d9363b0d55bb02f9f55 --- /dev/null +++ b/eval/recursive/115a33d3-868e-42f8-b4ae-1d71dbff310b.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int i; + +char s[100100]; + +int eval(); + +int termen(); + +int factor(); + + + +int eval() + +{ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-') + + { if(s[i] == '+') + + { i++; + + r = r + termen(); + + } + + else + + { i++; + + r = r - termen(); + + } + + } + + return r; + +} + + + +int termen() + +{ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/') + + { if(s[i] == '*') + + { i++; + + r = r * factor(); + + } + + else + + { i++; + + r = r / factor(); + + } + + } + + return r; + +} + + + +int factor() + +{ + + int r = 0; + + if(s[i] == '(') + + { i++; + + r = eval(); + + i++; + + } + + else + + { while(s[i] >= '0' && s[i] <= '9') + + { r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} + + + +int main() + +{ + + f >> s; + + g << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/14fb5a04-6ce2-42b8-8868-ab63a21907ed.cpp b/eval/recursive/14fb5a04-6ce2-42b8-8868-ab63a21907ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..502e051aa8a922523f8ce6c79053b36a94dee33a --- /dev/null +++ b/eval/recursive/14fb5a04-6ce2-42b8-8868-ab63a21907ed.cpp @@ -0,0 +1,70 @@ +#include +#include + +std::ifstream f("evaluare.in"); +std::ofstream g("evaluare.out"); + +int i,expresie(),termen(),factor(); +std::string s; + +int expresie(){ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + i++; + r += termen(); + }else{ + i++; + r -= termen(); + } + } + + return r; +} + +int termen(){ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + i++; + r *= factor(); + }else { + i++; + r /= factor(); + } + } + + return r; +} + +int factor(){ + + int r = 0; + + if(s[i] == '('){ + i++; + r = expresie(); + i++; + }else{ + + while(s[i] >= '0' && s[i] <= '9') + r = r * 10 + s[i++] - '0'; + + } + return r; +} + +int main(){ + + f >> s; + + g << expresie(); + + return 0; +} diff --git a/eval/recursive/15837d42-b0f2-474c-baf7-bacaaad182d2.cpp b/eval/recursive/15837d42-b0f2-474c-baf7-bacaaad182d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..318f198e7d370319420defa33f5eae706c90d81c --- /dev/null +++ b/eval/recursive/15837d42-b0f2-474c-baf7-bacaaad182d2.cpp @@ -0,0 +1,183 @@ +#include + + +using namespace std; + + + +ifstream cin ("evaluare.in") ; + +ofstream cout ("evaluare.out") ; + + + +char s[100005] ; + +int poz ; +int eval () ; + +int produs () ; + +int termen () ; + + + +int eval () + +{ + + + + int aux = produs () ; + + + + while (s[poz] == '+' || s[poz] == '-') + + { + + + + if (s[poz++] == '+') + + aux += produs() ; + + + + else + + aux -= produs() ; + + + + } + + + + return aux ; + + + +} + + + +int produs () + +{ + + + + int aux = termen() ; + + + + while (s[poz] == '*' || s[poz] == '/') + + { + + + + if (s[poz++] == '*') + + aux *= termen() ; + + + + else + + aux /= termen() ; + + + + } + + + + return aux ; + + + +} + + + +int termen() + +{ + + + + int aux = 0 ; + + + + if (s[poz] == '(') + + { + + + + poz++ ; + + aux = eval() ; + + poz++ ; + + + + return aux ; + + + + } + + + + while (s[poz] >='0' && s[poz] <= '9') + + aux = aux * 10 + (s[poz++] - '0'); + + + + return aux ; + + + +} + + + +void rez () + +{ + + + + cin >> s ; + + + + cout << eval() ; + + + +} + + + +int main() + +{ + + + + rez() ; + + + + return 0; + + + +} diff --git a/eval/recursive/16ba3ef8-bfbe-4127-838e-0396948da546.cpp b/eval/recursive/16ba3ef8-bfbe-4127-838e-0396948da546.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19cecaeaed6ab8c5e924d803104b1d949df95069 --- /dev/null +++ b/eval/recursive/16ba3ef8-bfbe-4127-838e-0396948da546.cpp @@ -0,0 +1,102 @@ +#include +#include + + +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +int evaluate(char* &expr); +int operand(char* &expr); +int value(char* &expr); +int ADD_SUBSTRACT(char*& expr); +int MULTIPLY_DIVIDE(char* &expr); + +char s[100001]; + +int main() +{ + char *expr; + + fin >> s; + expr = s; + fout << evaluate(expr); + return 0; +} + +int evaluate(char* &expr) +{ + return ADD_SUBSTRACT(expr); +} + +int ADD_SUBSTRACT(char*& expr) +{ + auto result = MULTIPLY_DIVIDE(expr); + + while (expr[0] == '+' || expr[0] == '-') + { + switch (expr[0]) + { + case '+': + result += MULTIPLY_DIVIDE(++expr); + break; + case '-': + result -= MULTIPLY_DIVIDE(++expr); + break; + default: + break; + } + } + + return result; +} + +int MULTIPLY_DIVIDE(char* &expr) +{ + auto result = operand(expr); + + while (expr[0] == '*' || expr[0] == '/') + { + switch (expr[0]) + { + case '*': + result *= operand(++expr); + break; + case '/': + result /= operand(++expr); + break; + default: + break; + } + } + + return result; +} + +int operand(char* &expr) +{ + if (expr[0] == '(') + { + auto result = evaluate(++expr); + expr++; + return result; + } + else + { + return value(expr); + } +} + +int value(char* &expr) +{ + int nr = 0; + + while (isdigit(expr[0])) + { + nr = nr * 10 + (expr[0] - '0'); + expr++; + } + + return nr; +} diff --git a/eval/recursive/179394a8-22c6-4d6f-8a9f-73170a0c4c77.cpp b/eval/recursive/179394a8-22c6-4d6f-8a9f-73170a0c4c77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdd26cc02375eb14c621190a650a3f446cc96c17 --- /dev/null +++ b/eval/recursive/179394a8-22c6-4d6f-8a9f-73170a0c4c77.cpp @@ -0,0 +1,84 @@ +#include +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +const int N=100001; + +char s[N]; +int p; + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+'||s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*' || s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0'&&s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} + +int main() +{ + f>>s; + g< +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N = 100005; + +int p; +char s[N]; + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum = termen(); + while (s[p] == '-' || s[p] == '+') + { + if (s[p] == '-') + { + p++; + sum -= termen(); + } + else + { + p++; + sum += termen(); + } + } + return sum; +} + +int termen() +{ + int prod = factor(); + while (s[p] == '*' || s[p] == '/') + { + if (s[p] == '*') + { + p++; + prod *= factor(); + } + else + { + p++; + prod /= factor(); + } + } + return prod; +} + +int factor() +{ + int val = 0,semn = 1; + while (s[p] == '-') + { + p++; + semn = -semn; + } + if (s[p] == '(') + { + p++; + val = expresie(); + p++; + return val*semn; + } + while (isdigit(s[p])) + { + val = val*10 + s[p] - '0'; + p++; + } + return val*semn; +} + +int main() +{ + in >> s; + out << expresie(); + return 0; +} diff --git a/eval/recursive/185eeee0-e3a0-4673-84cc-04b46d8640c6.cpp b/eval/recursive/185eeee0-e3a0-4673-84cc-04b46d8640c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab5e216ae54d7f7f9b4dd444e65ae38ae23ae7f2 --- /dev/null +++ b/eval/recursive/185eeee0-e3a0-4673-84cc-04b46d8640c6.cpp @@ -0,0 +1,164 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100000; + + + +int p; + +char s[N + 1]; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen (); + + } + + else + + { + + p++; + + sum -= termen (); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor (); + + } + + else + + { + + p++; + + prod /= factor (); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie (); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + in>>s; + + out << expresie(); + + return 0; + +} diff --git a/eval/recursive/18a4bf61-e2b0-4be5-907e-3248ee970a9c.cpp b/eval/recursive/18a4bf61-e2b0-4be5-907e-3248ee970a9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89d06ec18909945079a246cc5a3188dacd4a66e2 --- /dev/null +++ b/eval/recursive/18a4bf61-e2b0-4be5-907e-3248ee970a9c.cpp @@ -0,0 +1,175 @@ +#include +#include + + +using namespace std; + + + +FILE *fin = fopen("evaluare.in", "r"); + +FILE *fout = fopen("evaluare.out", "w"); + + + +int n; + +char sir[100005],*p; + + + +int eval(); + +int termen(); + +int factor(); + + + +int main() + +{ + + fgets(sir, 100002, fin); + + n = strlen(sir)-1; + + + + p = sir; + + fprintf(fout, "%d", eval()); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} + + + +int factor() + +{ + + int r; + + + + if(*p == '(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else + + { + + r = 0; + + while(*p>='0' and *p<='9') + + { + + r = r*10+(*p-'0'); + + ++p; + + } + + } + + return r; + +} + + + +int termen() + +{ + + int ans; + + + + ans = factor(); + + while(*p == '*' or *p == '/') + + if(*p == '*') + + { + + ++p; + + ans *= factor(); + + } + + else + + { + + ++p; + + ans /= factor(); + + } + + + + return ans; + +} + + + +int eval() + +{ + + int ans; + + + + ans = termen(); + + while(*p == '+' or *p == '-') + + if(*p == '+') + + { + + ++p; + + ans += termen(); + + } + + else + + { + + ++p; + + ans -= termen(); + + } + + + + return ans; + +} diff --git a/eval/recursive/196d62e2-d15e-4508-9212-e658b9944ec3.cpp b/eval/recursive/196d62e2-d15e-4508-9212-e658b9944ec3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6170772a222738a242b9721bc9a5608837f339c6 --- /dev/null +++ b/eval/recursive/196d62e2-d15e-4508-9212-e658b9944ec3.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + +int ind; + + + +int pri0(); + +int pri1(); + +int pri2(); + + + + + +int pri0(){ + + int x=pri1(); + + while(s[ind]=='+'||s[ind]=='-'){ + + if(s[ind]=='+') ind++, x=x+pri1(); + + else ind++, x-=pri1(); + + } + + return x; + +} + + + +int pri1(){ + + int x=pri2(); + + while(s[ind]=='*'||s[ind]=='/'){ + + if(s[ind]=='*') ind++, x=x*pri2(); + + else ind++, x/=pri2(); + + } + + return x; + +} + + + +int pri2(){ + + int x=0; + + if(s[ind]=='('){ + + ind++; + + x=pri0(); + + ind++; + + } + + else{ + + while(s[ind]>='0'&&s[ind]<='9'){ + + x=x*10+s[ind]-'0'; + + ind++; + + } + + } + + return x; + +} + + + +int main() + +{ + + fin>>s; + + fout< + + +using namespace std; + + + +string s; + +int eval(); + +int termen(); + +int factor(); + +int p; + + + +int eval(){ + + int val = termen(); + + while(s[p] == '+' || s[p] == '-'){ + + if(s[p] == '+'){ + + p++; + + val += termen(); + + } + + else{ + + p++; + + val -= termen(); + + } + + } + + return val; + +} + + + +int termen(){ + + int val = factor(); + + while(s[p] == '*' || s[p] == '/'){ + + if(s[p] == '*'){ + + p++; + + val *= factor(); + + } + + else{ + + p++; + + val /= factor(); + + } + + } + + return val; + +} + + + +int factor(){ + + int semn = 1, val = 0; + + while(s[p] == '-'){ + + semn = -semn; + + p++; + + } + + if(s[p] == '('){ + + p++; + + val = eval(); + + p++; + + return val * semn; + + } + + while(isdigit(s[p])){ + + val = val * 10 + s[p] - '0'; + + p++; + + } + + return val * semn; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + getline(fin, s); + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/1a0bde4f-02c0-4f99-9147-d3db348a6e94.cpp b/eval/recursive/1a0bde4f-02c0-4f99-9147-d3db348a6e94.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13956595229a6f23ee29e838c34d3246cea21c63 --- /dev/null +++ b/eval/recursive/1a0bde4f-02c0-4f99-9147-d3db348a6e94.cpp @@ -0,0 +1,58 @@ +#include +#include +using namespace std; +ifstream fin ( "evaluare.in" ); +ofstream fout ( "evaluare.out" ); +const int NMAX = 1e5; +char s[NMAX + 1]; +int n, k; +inline int op ( int a, int b, char ch ) { + if ( ch == '+' ) + return a + b; + if ( ch == '-' ) + return a - b; + if ( ch == '*' ) + return a * b; + if ( ch == '/' ) + return a / b; + return -1; +} +int eval (); +int factor() { + int rez; + if ( s[k] == '(' ) { + k++; + rez = eval(); + k++; + } else { + rez = 0; + while ( k <= n && isdigit ( s[k] ) ) + rez = rez * 10 + s[k++] - '0'; + } + return rez; +} +int termen () { + int rez = factor(); + while ( k <= n && ( s[k] == '*' || s[k] == '/' ) ) { + char semn = s[k]; + k++; + rez = op ( rez, factor(), semn ); + } + return rez; +} +int eval () { + int rez = termen(); + while ( k <= n && ( s[k] == '+' || s[k] == '-' ) ) { + char semn = s[k++]; + rez = op ( rez, termen(), semn ); + } + return rez; +} +int main () { + fin >> ( s + 1 ); + n = strlen ( s + 1 ); + k = 1; + fout << eval(); + + return 0; +} diff --git a/eval/recursive/1aafa91c-65fc-4882-9ed8-bf7dde652bbd.cpp b/eval/recursive/1aafa91c-65fc-4882-9ed8-bf7dde652bbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52ac7bd213ccea7b1c075fcd8522a0a111316d6f --- /dev/null +++ b/eval/recursive/1aafa91c-65fc-4882-9ed8-bf7dde652bbd.cpp @@ -0,0 +1,137 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int eval(),termen(),factor(); + + + +char *p; + + + +int eval() + +{ + + int res=factor(); + + cout<='0' && *p<='9') + + t=t*10+(*p-'0'),++p; + + return t; + +} + +int main() + +{ + + char s[100000]; + + p=s; + + f>>s; + + g< +#include +#include + + +using namespace std; + +char expr[100005]; + +long lg, i; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +long evaluare_expresie(); + +long evaluare_factor() + +{ + + long f = 0; + + if (expr[i] == '(') + + { + + i++; + + f = evaluare_expresie(); + + i++; + + } + + else + + while (i < lg && !strchr("+-*/()", expr[i])) + + { + + f = f * 10 + (expr[i] - '0'); + + i++; + + } + + return f; + +} + + + +long evaluare_termen() + +{ + + long T = evaluare_factor(); + + while (i < lg && strchr("*/", expr[i])) + + { + + char semn = expr[i]; + + i++; + + if (semn == '*') + + T *= evaluare_factor(); + + else + + T /= evaluare_factor(); + + } + + return T; + + + +} + + + +long evaluare_expresie() + +{ + + long E = evaluare_termen(); + + while (i < lg && strchr("+-", expr[i])) + + { + + char semn = expr[i]; + + i++; + + if (semn == '+') + + E += evaluare_termen(); + + else + + E -= evaluare_termen(); + + + + + + } + + return E; + +} + + + +int main() + +{ + + f.getline(expr, 100005); + + lg = strlen(expr); + + g << evaluare_expresie(); + + return 0; + +} diff --git a/eval/recursive/1ba81a06-126f-43f9-b5a2-587fc704a0c7.cpp b/eval/recursive/1ba81a06-126f-43f9-b5a2-587fc704a0c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..305f464528c6d3bf5c430711982bea442964496b --- /dev/null +++ b/eval/recursive/1ba81a06-126f-43f9-b5a2-587fc704a0c7.cpp @@ -0,0 +1,198 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + + + + + +char e[100005]; + + + +int i,lg; + + + +int evalexp(); + +int evaltermen(); + +int evalfactor(); + + + +int evalexp() + +{ + + int f=evaltermen(); + + + + char sgn; + + + + while(i='0' && e[i] <='9') + + { + + nr=nr*10+(e[i]-'0'); + + i++; + + } + + + + return nr; + + + +} + + + +int evalfactor() + +{ + + int f=0; + + + + if( e[i] == '(') + + { + + i++; + + f=f+evalexp(); + + i++; + + + + } + + else + + f=numar(); + + return f; + +} + + + +int main() + +{ + + fin>>e; + + + + lg=strlen(e); + + + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char expresie[100002]; + +int p = 0; + + + +long long functia1(); + +long long functia2(); + +long long functia3(); + + + +int main() + +{ + + fin >> expresie; + + fout << functia1(); + + fin.close(); + + fout.close(); + + return 0; + +} + + + +long long functia1() + +{ + + long long r = functia2(); + + while (expresie[p] == '+' || expresie[p] == '-') + + { + + if (expresie[p++] == '+') + + { + + r += functia2(); + + } + + else + + { + + r -= functia2(); + + } + + } + + return r; + +} + + + +long long functia2() + +{ + + long long r = functia3(); + + while (expresie[p] == '*' || expresie[p] == '/') + + { + + if (expresie[p++] == '*') + + { + + r *= functia3(); + + } + + else + + { + + r /= functia3(); + + } + + } + + return r; + +} + + + +long long functia3() + +{ + + long long r = 0; + + if (expresie[p] == '(') + + { + + ++p; + + r = functia1(); + + ++p; + + } + + else + + { + + while (expresie[p] >= '0' && expresie[p] <= '9') + + { + + r = r * 10 + expresie[p] - '0'; + + ++p; + + } + + } + + return r; + +} diff --git a/eval/recursive/1c21eb44-880f-40f7-9aac-a1d1b4ed9e28.cpp b/eval/recursive/1c21eb44-880f-40f7-9aac-a1d1b4ed9e28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..287754d53a68dd6b38ea11b1dc6df6123e21f796 --- /dev/null +++ b/eval/recursive/1c21eb44-880f-40f7-9aac-a1d1b4ed9e28.cpp @@ -0,0 +1,256 @@ +#include + + +using namespace std; + + + +class expressionEvaluation + +{ + +public: + + static const int symbolsTableSize = 128; + +private: + + + + static bool correctExpression; + + static bool acceptedSymbols[symbolsTableSize]; + + static bool acceptedSymbolsTableInitialized; + + static int parseDivisionMultiplication(const char *p, int &index) + + { + + int t1 = parseTerm(p, index), t2; + + bool mul; + + if (t1 == -2147483648) return t1; + + while (p[index] == '*' || p[index] == '/') + + { + + mul = (p[index] == '*' ? true : false); + + ++index; + + t2 = parseTerm(p, index); + + if (t2 == -2147483648) return t2; + + if (mul) t1 *= t2; + + else t1 /= t2; + + } + + if (!acceptedSymbols[(int)p[index]]) + + { + + correctExpression = false; + + return -2147483648; + + } + + return t1; + + } + + static int parseSubtractionAddition(const char *p, int &index) + + { + + int t1 = parseDivisionMultiplication(p, index), sign, t2; + + if (t1 == -2147483648) return t1; + + while (p[index] == '-' || p[index] == '+') + + { + + sign = (p[index] == '+' ? 1 : -1); + + ++index; + + t2 = parseDivisionMultiplication(p, index); + + if (t2 == -2147483648) return t2; + + t1 = t1 + sign * t2; + + } + + if (!acceptedSymbols[(int)p[index]]) + + { + + correctExpression = false; + + return -2147483648; + + } + + return t1; + + } + + static int parseTerm(const char *p, int &index) + + { + + if (!acceptedSymbols[(int)p[index]]) + + { + + correctExpression = false; + + return -2147483648; + + } + + if (p[index] == '(') + + { + + ++index; + + int ret = parseSubtractionAddition(p, index); + + ++index; + + return ret; + + } + + else if (p[index] >= '0' && p[index] <= '9') + + { + + int nr = 0; + + while (p[index]) + + { + + if (!acceptedSymbols[(int)p[index]]) + + { + + correctExpression = false; + + return -2147483648; + + } + + if (p[index] >= '0' && p[index] <= '9') + + nr = nr * 10 + (p[index] - '0'); + + else break; + + ++index; + + } + + return nr; + + } + + else + + { + + correctExpression = false; + + return -2147483648; + + } + + } + + static void initializeSymbolsTable() + + { + + if (acceptedSymbolsTableInitialized) return; + + for (int i=0; i< symbolsTableSize; ++i) + + acceptedSymbols[i] = false; + + acceptedSymbols[(int)'+'] = acceptedSymbols[(int)'-'] = acceptedSymbols[(int)'*'] + + = acceptedSymbols[(int)'/'] = true; + + for (int i=0; i<10; ++i) + + acceptedSymbols[i + '0'] = true; + + acceptedSymbols[(int)'('] = acceptedSymbols[(int)')'] = true; + + acceptedSymbolsTableInitialized = true; + + acceptedSymbols[0] = true; + + } + +public: + + static bool evaluate(const char *p, int &result) + + { + + initializeSymbolsTable(); + + correctExpression = true; + + int index = 0; + + result = parseSubtractionAddition(p, index); + + return correctExpression; + + } + +}; + +bool expressionEvaluation::acceptedSymbolsTableInitialized = false; + +bool expressionEvaluation::correctExpression = true; + +bool expressionEvaluation::acceptedSymbols[expressionEvaluation::symbolsTableSize] = {}; + + + +char expr[100005]; + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + cin>>expr; + + int ans; + + expressionEvaluation ev; + + ev.evaluate(expr, ans); + + cout< + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +const int NMAX = 1e5 + 5; + + + +int N, K; + + + +char S[NMAX], *p; + + + +static inline void Read () + +{ + + f.tie(NULL); + + + + f >> (S + 1); + + + + p = (S + 1); + + + + return; + +} + + + +static inline int Eval(); + +static inline int Termen(); + +static inline int Factor(); + + + +static inline int Eval () + +{ + + int ans = 0; + + + + ans = Termen(); + + + + while(*p == '+' || *p == '-') + + { + + if(*p == '+') + + { + + ++p; + + + + ans += Termen(); + + } + + else + + { + + ++p; + + + + ans -= Termen(); + + } + + } + + + + return ans; + +} + + + +static inline int Termen () + +{ + + int ans = 0; + + + + ans = Factor(); + + + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + { + + ++p; + + + + ans *= Factor(); + + } + + else + + { + + ++p; + + + + ans /= Factor(); + + } + + } + + + + return ans; + +} + + + +static inline int Factor () + +{ + + int ans = 0; + + + + if(*p == '(') + + { + + ++p; + + + + ans = Eval(); + + + + ++p; + + } + + else + + { + + while(isdigit(*p)) + + ans = ans * 10 + ((*p) - '0'), ++p; + + } + + + + return ans; + +} + + + +int main() + +{ + + Read(); + + + + g << Eval() << '\n'; + + + + return 0; + +} diff --git a/eval/recursive/1d771046-13c4-459c-a128-7994ca7cf294.cpp b/eval/recursive/1d771046-13c4-459c-a128-7994ca7cf294.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15c89ad4618b40f32a7ba71133f3027f4e2ae30d --- /dev/null +++ b/eval/recursive/1d771046-13c4-459c-a128-7994ca7cf294.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +char a[100005]; + +char *p = a; + + + +int Eval(); + +int Termen(); + +int Factor(); + + + +int Eval() + +{ + + int r = Termen(); + + + + while(*p == '+' || *p == '-') + + { + + if(*p == '+') + + { + + ++p; + + r += Termen(); + + } + + else + + { + + ++p; + + r -= Termen(); + + } + + } + + + + return r; + +} + + + +int Termen() + +{ + + int r = Factor(); + + + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + { + + ++p; + + r *= Factor(); + + } + + else + + { + + ++p; + + r /= Factor(); + + } + + } + + + + return r; + +} + + + +int Factor() + +{ + + int r = 0; + + + + if(*p == '(') + + { + + ++p; + + r = Eval(); + + ++p; + + } + + else while(isdigit(*p)) + + r = r * 10 + *p - '0' , p++; + + + + return r; + +} + + + +int main() + +{ + + f >> a; + + g << Eval(); + + return 0; + +} diff --git a/eval/recursive/1dba391c-b86b-4cc5-bdd2-edda50b017e4.cpp b/eval/recursive/1dba391c-b86b-4cc5-bdd2-edda50b017e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2467e22afd28219ba5dbd21031c1a9f91ee00ea2 --- /dev/null +++ b/eval/recursive/1dba391c-b86b-4cc5-bdd2-edda50b017e4.cpp @@ -0,0 +1,54 @@ +#include + + +char s[100001],*p=s; + +inline int E(int l) + +{ + + int x,y; + + if(l==2) + + if(*p=='(') + + p++,x=E(0),p++; + + else + + for(x=0;*p>='0'&&*p<='9';p++) + + x=x*10+*p-'0'; + + else + + for(x=E(l+1);(!l&&(*p=='+'||*p=='-'))||(l&&(*p=='*'||*p=='/'));x=y) + + if(*p=='+') + + p++,y=x+E(l+1); + + else if(*p=='-') + + p++,y=x-E(l+1); + + else if(*p=='*') + + p++,y=x*E(l+1); + + else if(*p=='/') + + p++,y=x/E(l+1); + + return x; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin),freopen("evaluare.out","w",stdout),scanf("%s",s),printf("%d",E(0)); + +} diff --git a/eval/recursive/1dd2c67c-e574-45a4-bddb-9886e1ea95c9.cpp b/eval/recursive/1dd2c67c-e574-45a4-bddb-9886e1ea95c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c3e13be561d4e382d7253dc98ae52d6a3be7e09 --- /dev/null +++ b/eval/recursive/1dd2c67c-e574-45a4-bddb-9886e1ea95c9.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +string s; + +int p; + + + +int eval (); + + + +int factor () { + + int r = 0; + + if (s[p] == '(') { + + p++; + + r = eval (); + + p++; + + } + + else + + while ('0' <= s[p] and s[p] <= '9') { + + r = r * 10 + s[p] - '0'; + + p++; + + } + + return r; + +} + + + +int termen () { + + int r = factor (); + + while (s[p] == '*' or s[p] == '/') { + + if (s[p] == '*') { + + p++; r *= factor (); + + } + + else { + + p++; r /= factor (); + + } + + } + + return r; + +} + + + +int eval () { + + int r = termen (); + + while (s[p] == '+' or s[p] == '-') { + + if (s[p] == '+') { + + p++; r += termen (); + + } + + else { + + p++; r -= termen (); + + } + + } + + return r; + +} + + + +int main () + +{ + + fin >> s; + + fout << eval (); + + return 0; + +} diff --git a/eval/recursive/1ee30800-7061-43f6-b02b-2866e4397070.cpp b/eval/recursive/1ee30800-7061-43f6-b02b-2866e4397070.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21e2dc72f7b248fb9cc61e0c84cab7b20ead5a20 --- /dev/null +++ b/eval/recursive/1ee30800-7061-43f6-b02b-2866e4397070.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; + +bool is_numerical(char a) { + return a >= '0' && a <= '9'; +} +pair get_term(string &exp, int pos); +pair get_product(string &exp, int pos); + +pair evaluate(string &exp, int pos) { + pair term_1 = get_product(exp, pos); + pos = term_1.second; + while (exp[pos] == '+' || exp[pos] == '-') { + if (exp[pos] == '+') { + pair term_2 = get_product(exp, pos + 1); + term_1.first += term_2.first; + pos = term_2.second; + } else if (exp[pos] == '-') { + pair term_2 = get_product(exp, pos + 1); + term_1.first -= term_2.first; + pos = term_2.second; + } + + } + return {term_1.first, pos}; +} + +pair get_term(string &exp, int pos) { + if (exp[pos] == '(') { + pair res = evaluate(exp, pos + 1); + pos = res.second; + pos++; + return {res.first, pos}; + } else { + int number = 0; + while (is_numerical(exp[pos])) { + number = number * 10 + (exp[pos] - '0'); + pos++; + } + return {number, pos}; + } +} + +pair get_product(string &exp, int pos) { + pair term_1 = get_term(exp, pos); + pos = term_1.second; + while (exp[pos] == '*' || exp[pos] == '/') { + if (exp[pos] == '*') { + pair term_2 = get_term(exp, pos + 1); + term_1.first *= term_2.first; + pos = term_2.second; + } else if (exp[pos] == '/') { + pair term_2 = get_term(exp, pos + 1); + term_1.first /= term_2.first; + pos = term_2.second; + } + } + return {term_1.first, pos}; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + pair result = evaluate(exp, 0); + + fout << result.first << "\n"; + + return 0; +} diff --git a/eval/recursive/1fe0e306-8a4e-4123-b623-0849bc88b050.cpp b/eval/recursive/1fe0e306-8a4e-4123-b623-0849bc88b050.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57012b30779ab347d47aab838f4e85168ecaaaf4 --- /dev/null +++ b/eval/recursive/1fe0e306-8a4e-4123-b623-0849bc88b050.cpp @@ -0,0 +1,145 @@ +#include +#include +#include +#include + + +std::ifstream f("evaluare.in"); + +std::ofstream g("evaluare.out"); + + + +int i; + +std::string s; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie(){ + + + + int r = termen(); + + + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + + i++; + + r += termen(); + + }else{ + + i++; + + r -= termen(); + + } + + } + + + + return r; + +} + + + +int termen(){ + + + + int r = factor(); + + + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + + i++; + + r *= factor(); + + }else{ + + i++; + + r /= factor(); + + } + + } + + + + return r; + +} + + + +int factor(){ + + + + int r; + + + + if(s[i] == '('){ + + i++; + + r = expresie(); + + i++; + + }else { + + r = 0; + + while(s[i] >= '0' && s[i] <= '9'){ + + r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + + + return r; + +} + + + +int main(){ + + + + f >> s; + + + + g << expresie(); + + + +} diff --git a/eval/recursive/2069c765-580d-47c0-9672-fdb5f0647cde.cpp b/eval/recursive/2069c765-580d-47c0-9672-fdb5f0647cde.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb8afa3a3c9003318660990497721bfddd7be228 --- /dev/null +++ b/eval/recursive/2069c765-580d-47c0-9672-fdb5f0647cde.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int k,n; + + + +int eval(); + +int termen(); + +int factor(); + +int ok(); + + + +char v[100003]; + + + +int eval(){ + + int s=termen(); + + + + while((v[k]=='+' || v[k]=='-') && k='0' && v[k]<='9'); + + + +} + + + +int factor(){ + + int s=0; + + + + if(v[k]=='('){ + + k++; + + s=eval(); + + k++; + + }else{ + + while(ok() && k<=n){ + + s=s*10+(v[k]-'0'); + + k++; + + } + + } + + + + + + return s; + +} + + + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + + + scanf("%s",&v); + + + + + + n=strlen(v); + + + + printf("%d",eval()); + + + + return 0; + +} diff --git a/eval/recursive/221bb080-e0c6-4883-b955-02de64295ddc.cpp b/eval/recursive/221bb080-e0c6-4883-b955-02de64295ddc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc06227b93a7364bee9af629331764d131756051 --- /dev/null +++ b/eval/recursive/221bb080-e0c6-4883-b955-02de64295ddc.cpp @@ -0,0 +1,146 @@ +#include +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100002]; + +int poz; + + + +long long termen(); + + + +long long sum(); + + + +long long prod(); + + + + + +long long termen() + +{ + + long long rez=0; + + if (s[poz]=='(') + + { + + poz++; + + rez=sum(); + + poz++; + + } + + else + + while (s[poz]<='9' && s[poz]>='0') + + rez=rez*10+(s[poz++]-'0'); + + return rez; + +} + + + +long long prod () + +{ + + long long rez=termen(); + + while (s[poz]=='*' || s[poz]== '/') + + { + + if (s[poz]=='*') + + { + + poz++; + + rez*= termen(); + + } + + else + + { + + poz++; + + rez/=termen(); + + } + + } + + return rez; + +} + + + + + +long long sum () + +{ + + long long rez=prod(); + + while (s[poz]=='+' || s[poz]=='-') + + { + + if (s[poz]=='+') + + { + + poz++; + + rez+=prod(); + + } + + else + + { + + poz++; + + rez-=prod(); + + } + + } + + return rez; + +} + + + +int main() + +{ + + fin.getline(s,100002); + + fout< +using namespace std; + +ifstream cin("evaluare.in"); + + + +ofstream cout("evaluare.out"); + + + +char s[100005]; + + + +int poz; + + + +int eval(); + + + +int produs(); + +int termen() + +{ + + int aux = 0 ; + + if(s[poz] == '(') + + { + + poz++; + + aux = eval(); + + poz++; + + return aux; + + } + + while('0' <= s[poz] && s[poz] <= '9') + + aux = aux * 10 + (s[poz++] - '0'); + + return aux; + +} + +int produs() + +{ + + int aux = termen(); + + while(s[poz] == '*' || s[poz] == '/') + + { + + if(s[poz++] == '*') + + aux = aux * termen(); + + else aux = aux / termen(); + + } + + return aux; + +} + +int eval() + +{ + + int aux = produs(); + + while(s[poz] == '+' || s[poz] == '-') + + { + + if(s[poz++] == '+') + + aux = aux + produs(); + + else aux = aux - produs(); + + } + + return aux; + +} + +int main() + +{ + + cin >> s; + + cout << eval(); + + return 0; + +} diff --git a/eval/recursive/229aca6e-457e-4ed6-8e2e-930f8823483a.cpp b/eval/recursive/229aca6e-457e-4ed6-8e2e-930f8823483a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4992275b8ebb8b03a895880c6f0a02c8adcde98a --- /dev/null +++ b/eval/recursive/229aca6e-457e-4ed6-8e2e-930f8823483a.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char c[100001]; + +int i; + +int solve(); + +int mul(); + +int num(); + +int solve() { + + int t = mul(); + + while (c[i] == '+' || c[i] == '-') { + + if (c[i] == '+') { + + i++; + + t += mul(); + + } else { + + i++; + + t -= mul(); + + } + + } + + return t; + +} + +int mul() { + + int t = num(); + + while (c[i] == '*' || c[i] == '/') { + + if (c[i] == '*') { + + i++; + + t *= num(); + + } else { + + i++; + + t /= num(); + + } + + } + + return t; + +} + +int num() { + + int t = 0; + + if (c[i] == '(') { + + i++; + + t = solve(); + + i++; + + } else { + + while (isdigit(c[i])) { + + t = t * 10 + (int)c[i++] - '0'; + + } + + return t; + + } + +} + +int main() { + + fin.getline(c, 100001); + + fout << solve(); + + return 0; + +} diff --git a/eval/recursive/24464537-1765-4f91-93c6-a756ece87a50.cpp b/eval/recursive/24464537-1765-4f91-93c6-a756ece87a50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bee0786f5066791ed92013c124be2c65387797c2 --- /dev/null +++ b/eval/recursive/24464537-1765-4f91-93c6-a756ece87a50.cpp @@ -0,0 +1,81 @@ +#include + +const int N=100001; +char s[N]; +int p; + +using namespace std; + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum = termen(); + while(s[p] == '+'||s[p] == '-') + { + if(s[p] == '+') + { + p++; + sum += termen(); + } + else + { + p++; + sum -= termen(); + } + } + return sum; +} + +int termen() +{ + int produs = factor(); + while(s[p] == '*'|| s[p] == '/') + { + if(s[p] == '*') + { + p++; + produs *= factor(); + } + else + { + p++; + produs /= factor(); + } + } + return produs; +} + +int factor() +{ + int val = 0, semn = 1; + while(s[p] == '-') + { + p++; + semn = -semn; + } + if(s[p] == '(') + { + p++; + val = expresie(); + p++; + return semn * val; + } + while('0' <= s[p] && s[p] <= '9') + { + val = val*10 +(s[p] - '0'); + p++; + } + return semn*val; +} + +int main() +{ + ifstream in("evaluare.in"); + ofstream out("evaluare.out"); + in >> s; + out << expresie(); + return 0; +} diff --git a/eval/recursive/24c0eae5-2b9e-4707-9cfa-dde831f44751.cpp b/eval/recursive/24c0eae5-2b9e-4707-9cfa-dde831f44751.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc8a1aa956d830c3814ba317b63d15f7d3166879 --- /dev/null +++ b/eval/recursive/24c0eae5-2b9e-4707-9cfa-dde831f44751.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100000],*p; + + + +long long eval(); + +long long term(); + +long long fact(); + + + +long long eval() + +{long long r; + +r=term(); + +while(*p=='+'||*p=='-') + + {if(*p=='+'){p++;r+=term();} + + else if(*p=='-'){p++;r-=term();} + + } + +return r; + +} + + + +long long term() + +{long long r=fact(); + +while(*p=='*'||*p=='/') + + {if(*p=='*'){p++;r*=fact();} + + else if(*p=='/'){p++;r=r/fact();} + + } + +return r; + +} + + + +long long fact() + +{long long r=0; + +if(*p=='('){p++;r=eval();p++;} + +else { + + while(*p>='0'&&*p<='9') + + {r=r*10+*p-'0'; + + p++; + + } + + } + +return r; + +} + + + +int main() + +{ + +f.getline(s,100000); + +p=s; + + g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int EvalExpresie(); + + + +char e[100005]; + +int poz, lg; + + + +int getVal(){ + + int val = 0; + + while(e[poz] >= '0' && e[poz] <= '9') + + val = val * 10 + (e[poz] - '0'), ++poz; + + return val; + +} + + + +int EvalFactor(){ + + int f; + + if(e[poz] == '('){ + + ++poz; + + f = EvalExpresie(); + + ++poz; + + } + + else + + f = getVal(); + + return f; + +} + + + +int EvalTermen(){ + + int f = EvalFactor(); + + while(poz < lg){ + + if(e[poz] == '*') + + ++poz, f *= EvalFactor(); + + else if(e[poz] == '/') + + ++poz, f /= EvalFactor(); + + else break; + + } + + return f; + +} + + + +int EvalExpresie(){ + + int t = EvalTermen(); + + while(poz < lg){ + + if(e[poz] == '+') + + ++poz, t += EvalTermen(); + + else if(e[poz] == '-') + + ++poz, t -= EvalTermen(); + + else break; + + } + + + + return t; + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin >> e; + + lg = strlen(e); + + + + fout << EvalExpresie() << '\n'; + + return 0; + +} diff --git a/eval/recursive/2633e310-f19e-448a-a1b2-062b46565eee.cpp b/eval/recursive/2633e310-f19e-448a-a1b2-062b46565eee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72d8460506d87cb95f316a496de60ef74b8d1ba7 --- /dev/null +++ b/eval/recursive/2633e310-f19e-448a-a1b2-062b46565eee.cpp @@ -0,0 +1,66 @@ +#include +#include +char s[100010]; +int i = 0; + + +long termen(); + +long factor(); +long eval() { + + long rezultat = termen(); + + while (s[i] == '+' || s[i] == '-') { + switch (s[i]) { + case '+': + ++i; + rezultat += termen(); + break; + case '-': + ++i; + rezultat -= termen(); + break; + } + } + return rezultat; +} +long termen() { + long rezultat = factor(); + + while (s[i] == '*' || s[i] == '/') { + switch (s[i]) { + case '*': + i++; + rezultat *= factor(); + break; + case '/': + i++; + rezultat /= factor(); + break; + } + } + return rezultat; +} +long factor() { + if (s[i] == '(') { + i++; + long rezultat = eval(); + i++; + return rezultat; + } + int rezultat = 0; + while (isdigit(s[i])) + rezultat = rezultat * 10 + s[i++] - '0'; + return rezultat; +} + + +int main() { + + fgets(s, 100010, fopen("evaluare.in", "r")); + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + return 0; + +} diff --git a/eval/recursive/265ea83d-da0c-4130-b30a-e92038a13c64.cpp b/eval/recursive/265ea83d-da0c-4130-b30a-e92038a13c64.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fadef9f1030c0a31fd2dc5d438f588af39021500 --- /dev/null +++ b/eval/recursive/265ea83d-da0c-4130-b30a-e92038a13c64.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +char s[100010], ch[4][4]{"+-", "*/", "^", ""}, *p=s; + + + +int eval(int); + +int getNr(); + +int rez(int, char, int); + + + +int rez(int a, int b, char c) + +{ + + switch(c) + + { + + case '+': + + return a+b; + + case '-': + + return a-b; + + case '*': + + return a*b; + + case '/': + + return a/b; + + } + + return 0; + +} + + + +int eval(int h) + +{ + + int r; + + if(h==2) r=getNr(); + + else r=eval(h+1); + + while(strchr(ch[h], *p)) + + r=rez(r, eval(h+1), *(++p-1)); + + return r; + +} + + + +int getNr() + +{ + + int r=0; + + if(*p=='(') ++p, r=eval(0), ++p; + + else + + { + + while(*p>='0' && *p<='9') + + r=r*10+*(++p-1)-'0'; + + } + + return r; + +} + + + +int main() + +{ + + in>>s; + + s[strlen(s)]='\n'; + + out< +#include +using namespace std; + +char s[100000],*p; + +int r; + +int eval(); + +int termen(); + +int factor(); + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int eval(){ + + int r=termen(); + + while((*p=='+')||(*p=='-')){ + + if(*p=='+'){ + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + } + + return r; + +} + +int termen(){ + + int r=factor(); + + while((*p=='*')||(*p=='/')){ + + if(*p=='*'){ + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + } + + return r; + +} + +int factor(){ + + int r=0; + + if(*p=='('){ + + p++; + + r=eval(); + + p++; + + } + + else + + while('0'<=*p && '9'>=*p){ + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main(){ + + f.getline(s,100000); + + p=s; + + r=eval(); + + g< +#include +using namespace std; + +ifstream f ("evaluare.in"); +ofstream g ("evaluare.out"); + +char termeni[100003]; +char *p=termeni; +long oi(); +long paranteze(); +long pm () { + long r = oi(); + while (*p=='+' || *p=='-') { + if (*p=='+') { + ++p; + r+=oi(); + } else if (*p=='-') { + ++p; + r-=oi(); + } + } + return r; +} + +long oi() { + long r = paranteze(); + while (*p=='*' || *p=='/') { + if (*p=='*') { + ++p; + r*=paranteze(); + } else if (*p=='/') { + ++p; + r/=paranteze(); + } + } + return r; +} + +long paranteze() { + long r = 0; + if (*p=='(') { + ++p; + r=pm(); + ++p; + } else { + while ( *p>='0' && *p<='9' ) { + r = r*10 + *p - '0'; + ++p; + } + } + return r; +} + +int main(int argc, const char * argv[]) { + f>>termeni; + g< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int i; + +char s[100003]; + +int expresie(); + +int factor(); + +int termen(); + +int main(){ + + fin.getline(s,100001); + + fout<='0' && s[i]<='9') + + r=r*10+s[i++]-'0'; + + return r; + +} diff --git a/eval/recursive/29b4a64b-08ec-481b-bd42-c207db2b7386.cpp b/eval/recursive/29b4a64b-08ec-481b-bd42-c207db2b7386.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0ccbe1a53c876839a9b36a8da9265fe65596f16 --- /dev/null +++ b/eval/recursive/29b4a64b-08ec-481b-bd42-c207db2b7386.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +const int SIZE = 1 << 20; + + + +char s[SIZE]; + +int p = 0; + +inline int eval (); + + + +inline int fact () { + + int nr, sign; + + nr = 0; + + sign = 1; + + if (s[p] == '-') { + + p++; + + sign = -1; + + } + + if (s[p] == '(') { + + p++; + + nr = eval (); + + p++; + + } + + else { + + while (isdigit (s[p])) { + + nr = nr * 10 + s[p] - '0'; + + p++; + + } + + } + + return nr * sign; + + + +} + + + +inline int term () { + + int sum = fact (); + + while (s[p] == '*' || s[p] == '/') + + if (s[p++] == '*') + + sum *= fact (); + + else + + sum /= fact (); + + return sum; + +} + + + +inline int eval () { + + int sum = term (); + + while (s[p] == '+' || s[p] == '-') { + + if (s[p++] == '+') + + sum += term (); + + else + + sum -= term (); + + } + + return sum; + +} + + + +int main() { + + freopen ("evaluare.in", "r", stdin); + + freopen ("evaluare.out", "w", stdout); + + + + scanf ("%s", s); + + p = 0; + + printf ("%d\n", eval ()); + + + + return 0; + +} diff --git a/eval/recursive/2aa24479-d35e-49f0-9c85-d39ecab1f83e.cpp b/eval/recursive/2aa24479-d35e-49f0-9c85-d39ecab1f83e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc9d00db114c52d3148e75e60f5bd5ac0ea44fb9 --- /dev/null +++ b/eval/recursive/2aa24479-d35e-49f0-9c85-d39ecab1f83e.cpp @@ -0,0 +1,71 @@ +#include +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N = 1000001; +int p; +char s[N]; +int expresie(); +int termen(); +int factor(); + +int expresie(){ + int sum=termen(); + while(s[p]=='+' || s[p]=='-'){ + if(s[p]=='+'){ + p++; + sum+=termen(); + } + if(s[p]=='-'){ + p++; + sum-=termen(); + } + } + return sum; +} + +int termen(){ + int prod=factor(); + while(s[p]=='*' || s[p]=='/'){ + if(s[p]=='*'){ + p++; + prod*=factor(); + } + if(s[p]=='/'){ + p++; + prod/=factor(); + } + } + return prod; +} + +int factor(){ + int val=0, semn=1; + while(s[p]=='-'){ + p++; + semn=-semn; + } + if(s[p]=='('){ + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0' && s[p]<='9'){ + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} + +int main() +{ + in>>s; + out< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/2b380ea6-634b-4e2c-a387-a0889b179acc.cpp b/eval/recursive/2b380ea6-634b-4e2c-a387-a0889b179acc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4afd66c5df79b6c0bd4eb876edcfee3a9432829 --- /dev/null +++ b/eval/recursive/2b380ea6-634b-4e2c-a387-a0889b179acc.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +using namespace std; + + + + + +char ex[100010],*p = ex; + + + +long termen(); + + + +long factor(); + + + +long eval(){ + + long r = termen(); + + while(*p == '+' || *p == '-'){ + + if(*p == '+'){ + + ++p; + + r += termen(); + + + + } + + if(*p == '-'){ + + ++p; + + r -= termen(); + + + + } + + } + + return r; + + + +} + + + +long termen(){ + + long r = factor(); + + while(*p == '*' || *p == '/'){ + + if(*p == '*'){ + + ++p; + + r *= factor(); + + + + } + + if(*p == '/'){ + + ++p; + + r /= factor(); + + } + + + + } + + return r; + +} + + + +long factor(){ + + long r = 0; + + if(*p == '('){ + + ++p; + + r = eval(); + + ++p; + + }else{ + + while(*p >= '0' && *p <= '9'){ + + r = r*10 + *p - '0'; + + ++p; + + + + } + + } + + return r; + + + +} + + + +int main(){ + + + + fgets(ex, 100010, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + return 0; + +} diff --git a/eval/recursive/30fff5ed-211b-475f-b863-6698659a9b18.cpp b/eval/recursive/30fff5ed-211b-475f-b863-6698659a9b18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78f5c1d8c8bb58ba0dc73b6499962d8dc9934ac3 --- /dev/null +++ b/eval/recursive/30fff5ed-211b-475f-b863-6698659a9b18.cpp @@ -0,0 +1,142 @@ +#include +#include +#include + + +using namespace std; + + + +char s[100010], st[100010]; + +int rez[100010]; + +int top_st = -1, top_rez = -1; + + + +int eval(int a, int b, char op) { + + if(op == '+') + + return a + b; + + if(op == '-') + + return a - b; + + if(op == '*') + + return a * b; + + if(op == '/') + + return a / b; + +} + + + +int priority(char op) { + + if(op == '+' || op == '-') + + return 1; + + if(op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int main() { + + FILE *fin = fopen("evaluare.in", "r"); + + FILE *fout = fopen("evaluare.out", "w"); + + int i, n, x; + + + + fscanf(fin, "%s", &s); + + n = strlen(s); + + st[0] = '('; + + top_st = 0; + + s[n] = ')'; + + n++; + + for(i = 0; i < n; i++) { + + if(s[i] >= '0' && s[i] <= '9') { + + x = 0; + + while(s[i] >= '0' && s[i] <= '9') { + + x = x * 10 + s[i] - '0'; + + i++; + + } + + i--; + + rez[++top_rez] = x; + + } + + else if(s[i] == '(') + + st[++top_st] = '('; + + else if(s[i] == ')') { + + while(st[top_st] != '(') { + + rez[top_rez - 1] = eval(rez[top_rez - 1], rez[top_rez], st[top_st]); + + top_st--; + + top_rez--; + + } + + top_st--; + + } + + else { + + while(priority(s[i]) <= priority(st[top_st])) { + + rez[top_rez - 1] = eval(rez[top_rez - 1], rez[top_rez], st[top_st]); + + top_st--; + + top_rez--; + + } + + st[++top_st] = s[i]; + + } + + } + + fprintf(fout, "%d", rez[0]); + + + + return 0; + +} diff --git a/eval/recursive/316e5e12-d6e5-4259-953b-61c2c6dd6881.cpp b/eval/recursive/316e5e12-d6e5-4259-953b-61c2c6dd6881.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa3f4f6f565737f469ba7c877bd8f07f5aa664f4 --- /dev/null +++ b/eval/recursive/316e5e12-d6e5-4259-953b-61c2c6dd6881.cpp @@ -0,0 +1,118 @@ +#include +#include +#include + + +using namespace std; + +char s[100005]; + +int cautas(int st, int dr, char s1, char s2) + +{ + + int nr=0; + + for(int i=dr; i>=st; i--) + + { + + if(s[i]==')') + + nr++; + + if(s[i]=='(') + + nr--; + + if(nr==0 && (s1==s[i] || s2==s[i])) + + return i; + + } + + return -1; + +} + +int num(int st, int dr) + +{ + + int numar=0; + + for(int i=st; i<=dr; i++) + + numar=numar*10+(s[i]-'0'); + + return numar; + + + +} + +int solve(int st, int dr) + +{ + + int poz=cautas(st,dr,'+','-'); + + if(poz!=-1) + + { + + int e1=solve(st,poz-1); + + int e2=solve(poz+1,dr); + + if(s[poz]=='+') + + return e1+e2; + + return e1-e2; + + } + + poz=cautas(st,dr,'*','/'); + + if(poz!=-1) + + { + + int e1=solve(st,poz-1); + + int e2=solve(poz+1,dr); + + if(s[poz]=='*') + + return e1*e2; + + return e1/e2; + + } + + if(s[st]=='(' && s[dr]==')') + + return solve(st+1,dr-1); + + return num(st,dr); + +} + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.getline(s,100005); + + int n=strlen(s); + + fout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100001; + + + +char s[N]; + + + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie(){ + + int sum = termen(); + + while( s[p] == '+' || s[p] == '-'){ + + if(s[p]=='+'){ + + p++; + + sum += termen(); + + }else{ + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen(){ + + int prod=factor(); + + while(s[p] == '*' || s[p] == '/'){ + + if(s[p] == '*'){ + + p++; + + prod*=factor(); + + }else{ + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor(){ + + int val=0,semn=1; + + while(s[p] == '-'){ + + p++; + + semn = -semn; + + } + + if(s[p] == '('){ + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while('0' <= s[p] && s[p] <= '9'){ + + val = val*10 + (s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/33a98236-a85b-4842-a414-59aceb32b5da.cpp b/eval/recursive/33a98236-a85b-4842-a414-59aceb32b5da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4ea9287e18b9782b6d6db16babe11dad07f06d0 --- /dev/null +++ b/eval/recursive/33a98236-a85b-4842-a414-59aceb32b5da.cpp @@ -0,0 +1,154 @@ +#include +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +char s[100005]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + semn=-semn; + + p++; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10+s[p++]-'0'; + + } + + return semn*val; + +} + + + +int main() + +{ + + in>>s; + + out< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +const int N=100000; + +char s[N+1]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + +int expresie(); + +int expresie() + +{ + + int sum=termen(); + + while (s[p]=='+' || s[p]=='-'){ + + if (s[p]=='+'){ + + p++; + + sum+=termen(); + + }else{ + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod=factor(); + + while (s[p]=='*' || s[p]=='/'){ + + if (s[p]=='*'){ + + p++; + + prod*=factor(); + + }else{ + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int semn=1, val=0; + + while (s[p]=='-'){ + + p++; + + semn=-semn; + + } + + if (s[p]=='('){ + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while (s[p]>='0' && s[p]<='9'){ + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + +int main() + +{ + + fin>>s; + + fout< + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +char e[100005]; + +int i, lg; + + + +int Expresie(); + +int Termen(); + +int Factor(); + + + +int main() + +{ + + fin.getline(e, 100005); + + fin.close(); + + lg=strlen(e); + + fout<='0' && e[i]<='9') + + val=val*10+(e[i]-'0'), i++; + + return semn*val; + + + + + +} diff --git a/eval/recursive/3668b5fc-681f-48ba-a7ea-aa2b11323df9.cpp b/eval/recursive/3668b5fc-681f-48ba-a7ea-aa2b11323df9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dc4d66d445d2089070b46b4ad9c9cbd8afeca8d --- /dev/null +++ b/eval/recursive/3668b5fc-681f-48ba-a7ea-aa2b11323df9.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +char s[100010],*p=s; + +long termen(); + +long factor(); + +long eval() + +{ + + long r=termen(); + + while(*p=='+' || *p=='-') + + switch (*p) + + { + + case '+': + + ++p; + + r+=termen(); + + break; + + case '-': + + ++p; + + r-=termen(); + + break; + + } + + return r; + +} + +long termen() + +{ + + long r=factor(); + + while(*p=='*' || *p=='/') + + switch(*p) + + { + + case '*' : + + ++p; + + r*=factor(); + + break; + + case '/': + + ++p; + + r/=factor(); + + break; + + } + + return r; + +} + +long factor() + +{ + + long r=0; + + if(*p=='(') + + { + + ++p; + + r=eval(); + + ++p; + + } + + else + + { + + while(*p>='0' && *p<='9') + + { + + r=r*10+*p-'0'; + + ++p; + + } + + + + } + + return r; + +} + +int main() + +{ + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +cin.get(s,100010); + +cout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + + + +int p = 0; + + + +int priority(char k) + +{ + + if(k == '+' || k == '-') + + return 1; + + if(k == '*' || k == '/') + + return 2; + + return 3; + +} + + + +inline long long expresie(); + +inline long long termen(); + +inline long long factor(); + + + +long long expresie() + +{ + + long long t = termen(); + + while(priority(s[p]) == 1) + + { + + if(s[p] == '+') + + ++p,t += termen(); + + else + + if(s[p] == '-') + + ++p,t -= termen(); + + } + + return t; + + + +} + + + +long long termen() + +{ + + long long t = factor(); + + while(priority(s[p]) == 2) + + { + + if(s[p] == '*') + + ++p,t *= factor(); + + else + + if(s[p] == '/') + + ++p,t /= factor(); + + } + + return t; + +} + + + +long long factor() + +{ + + + + long long t = 0; + + + + if(s[p] == '(') + + { + + ++p; + + t = expresie(); + + ++p; + + } + + else + + { + + while(s[p] >= '0' && s[p] <= '9') + + { + + t *= 10; + + t += s[p] - '0'; + + ++p; + + } + + } + + return t; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + + + return 0; + +} diff --git a/eval/recursive/39c55e27-4743-459d-b430-85c186026e3a.cpp b/eval/recursive/39c55e27-4743-459d-b430-85c186026e3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e19f52666a22b1f55c15442f445f02e994bcda63 --- /dev/null +++ b/eval/recursive/39c55e27-4743-459d-b430-85c186026e3a.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005], *p = s; + +int i; + + + +int eval2(); + +int eval1(); + +int eval() + +{ + + int r = eval1(); + + while (*p == '+' || *p == '-') + + if (*p == '+') + + { + + ++p; + + r += eval1(); + + } + + else + + { + + ++p; + + r -= eval1(); + + } + + return r; + +} + +int eval1() + +{ + + int r = eval2(); + + while (*p == '*' || *p == '/') + + if (*p == '*') + + { + + ++p; + + r *= eval2(); + + } + + else + + { + + ++p; + + r /= eval2(); + + } + + return r; + +} + +int eval2() + +{ + + int r = 0; + + if (*p == '(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else while (isdigit(*p)) + + { + + r = r * 10 + *p - '0'; + + ++p; + + } + + return r; + +} + +int main() + +{ + + fin >> s; + + fout << eval() << "\n"; + + return 0; + +} diff --git a/eval/recursive/3a1c4475-daac-4f3f-8b5b-72a679b1ac40.cpp b/eval/recursive/3a1c4475-daac-4f3f-8b5b-72a679b1ac40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a8f6c05bff738ce02d2efc8f58c927251fac7aa --- /dev/null +++ b/eval/recursive/3a1c4475-daac-4f3f-8b5b-72a679b1ac40.cpp @@ -0,0 +1,149 @@ +#include +using namespace std; + +ifstream f ("evaluare.in") ; + +ofstream g ("evaluare.out") ; + +char s[100005] , *p ; + +int eval() ; + +int termen() ; + +int factor() ; + + + +int eval() + + { + + int r = termen() ; + + while (*p == '+' || *p == '-') + + { + + if (*p == '+') + + { + + p ++ ; + + r += termen(); + + } + + else + + { + + p ++ ; + + r -= termen(); + + } + + } + + + + return r; + + } + + int termen() + + { + + int r = factor() ; + + while(*p == '*' || *p == '/') + + { + + if (*p == '*') + + { + + p ++ ; + + r *= factor() ; + + } + + else + + { + + p ++ ; + + r /= factor() ; + + } + + } + + + + return r; + + } + + int factor() + + { + + int r = 0 ; + + if (*p == '(') + + { + + p ++ ; + + r = eval() ; + + p ++ ; + + } + + else + + { + + while (isdigit(*p)) + + { + + r = r * 10 + *p - '0'; + + p ++ ; + + } + + } + + + + return r; + + } +int main() + +{ + + f.getline(s,100005) ; + + p = s; + + + g << eval() << '\n' ; + + f.close(); + + g.close(); + + return 0 ; +} diff --git a/eval/recursive/3a94f566-d1ab-419a-8d32-6e432c8800cd.cpp b/eval/recursive/3a94f566-d1ab-419a-8d32-6e432c8800cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61f91649d4d018d33c5cbf902dbeb8c8046d8d21 --- /dev/null +++ b/eval/recursive/3a94f566-d1ab-419a-8d32-6e432c8800cd.cpp @@ -0,0 +1,169 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int PlusMinus(int &p); + +int ProdCat(int &p); + +int ParantezeNumere(int &p); + + + +string s; + + + +int PlusMinus(int &p) + +{ + + int ans = ProdCat(p); + + + + while((s[p] == '+' || s[p] == '-') && p < s.size()) + + { + + int semn; + + + + if(s[p] == '+') + + semn = 1; + + else + + semn = -1; + + + + p++; + + ans += semn * ProdCat(p); + + } + + + + return ans; + +} + + + +int ProdCat(int &p) + +{ + + int ans = ParantezeNumere(p); + + + + while((s[p] == '*' || s[p] == '/') && p < s.size()) + + { + + int op; + + + + if(s[p] == '*') + + op = 1; + + else + + op = -1; + + + + p++; + + + + if(op == 1) + + ans *= ParantezeNumere(p); + + else + + ans /= ParantezeNumere(p); + + } + + + + return ans; + +} + + + +int ParantezeNumere(int &p) + +{ + + int ans = 0; + + + + if(s[p] == '(') + + { + + p++; + + ans = PlusMinus(p); + + p++; + + } + + else + + { + + while(isdigit(s[p]) && p < s.size()) + + ans = ans * 10 + s[p] - '0', p++; + + } + + + + return ans; + +} + + + +int main() + +{ + + fin >> s; + + + + int p = 0; + + fout << PlusMinus(p) << '\n'; + + + + return 0; + +} diff --git a/eval/recursive/3b5ce0a9-8c5f-4607-addc-12dc1bfc4ac3.cpp b/eval/recursive/3b5ce0a9-8c5f-4607-addc-12dc1bfc4ac3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2525c0bde04f57b712ce1710c5d22cf5fbc8d48c --- /dev/null +++ b/eval/recursive/3b5ce0a9-8c5f-4607-addc-12dc1bfc4ac3.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +string v; + +int i; + +int numar(); + +int inm_imp(); + +int evaluare() { + + int nr = inm_imp(); + + while (v[i] == '+' || v[i] == '-') { + + if (v[i++] == '+') + + nr += inm_imp(); + + else + + nr -= inm_imp(); + + } + + return nr; + +} + +int numar() { + + int nr = 0; + + if (v[i] == '(') { + + i++; + + nr = evaluare(); + + i++; + + } + + else + + while(v[i] >= '0'&& v[i] <= '9') { + + nr = nr * 10 + (v[i] - '0'); + + i++; + + } + + return nr; + +} + +int inm_imp() { + + int nr = numar(); + + while (v[i] == '*' || v[i] == '/') + + { + + if (v[i++] == '*') + + nr *= numar(); + + else + + nr /= numar(); + + } + + return nr; + +} + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> v; + + fout << evaluare(); + + return 0; + +} diff --git a/eval/recursive/3b96ea49-d240-4fe3-b973-8a99f521eb29.cpp b/eval/recursive/3b96ea49-d240-4fe3-b973-8a99f521eb29.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30911c730d119ad4771edb831050a7df19ed8cf1 --- /dev/null +++ b/eval/recursive/3b96ea49-d240-4fe3-b973-8a99f521eb29.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + +int i = 0; +string s; + +int f1(); +int f2(); +int f3(); + +int f1() { + int ans = f2(); + while(s[i] == '+' || s[i] == '-') { + if(s[i] == '+') { + ++i; + ans += f2(); + } else { + ++i; + ans -= f2(); + } + } + return ans; +} + +int f2() { + int ans = f3(); + while(s[i] == '*' || s[i] == '/') { + if(s[i] == '*') { + ++i; + ans *= f3(); + } else { + ++i; + ans /= f3(); + } + } + + return ans; +} + +int f3() { + int ans = 0; + if(s[i] == '(') { + ++i; + ans = f1(); + ++i; + } else { + while(isdigit(s[i])) { + ans = (ans<<1) + (ans<<3) + s[i] - '0'; + ++i; + } + } + + return ans; +} + +int main() { + + + + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + cin >> s; + cout << f1() << '\n'; + return 0; +} diff --git a/eval/recursive/3bac40d8-f4d1-411b-8506-898fcd3e63ae.cpp b/eval/recursive/3bac40d8-f4d1-411b-8506-898fcd3e63ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c4ee1e9063bbe5cbb79a877495078c651a0305e --- /dev/null +++ b/eval/recursive/3bac40d8-f4d1-411b-8506-898fcd3e63ae.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100000; + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int semn = 1,val = 0; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in.get(s,N+1); + + out< +#include + +const int LMAX = 100005; + +char input[LMAX]; +char *p = input; + +int eval_add(); +int eval_mul(); +int eval_par(); + +int eval_add() { + int result = eval_mul(); + + while (*p == '+' || *p == '-') + if (*p == '+') + p++, result += eval_mul(); + else + p++, result -= eval_mul(); + + return result; +} + +int eval_mul() { + int result = eval_par(); + + while (*p == '*' || *p == '/') + if (*p == '*') + p++, result *= eval_par(); + else + p++, result /= eval_par(); + + return result; +} + +int eval_par() { + int result = 0; + + if (*p == '(') + p++, result = eval_add(), p++; + else + while (isdigit(*p)) + result = result * 10 + (*p - '0'), p++; + + return result; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + fgets(input, LMAX, stdin); + printf("%d\n", eval_add()); + + return 0; +} diff --git a/eval/recursive/3d80c19f-66e6-4fbb-9353-f0fa9fa4473c.cpp b/eval/recursive/3d80c19f-66e6-4fbb-9353-f0fa9fa4473c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dab74866cd24bc4483b67f3a601c074da9c9f9d6 --- /dev/null +++ b/eval/recursive/3d80c19f-66e6-4fbb-9353-f0fa9fa4473c.cpp @@ -0,0 +1,127 @@ +#include +using namespace std; + +char s[100010]; + +int i; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int main() { + + ifstream fin ("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin>>s; + + i = 0; + + fout<= '0' && s[i] <= '9') { + + r = r*10 + s[i]-'0'; + + i++; + + } + + return r; + + } + +} diff --git a/eval/recursive/3d80d04d-466d-470a-a2dd-9216c16dd0f0.cpp b/eval/recursive/3d80d04d-466d-470a-a2dd-9216c16dd0f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..838ad0e37f70a0362ab5b25311cdd63814c02b18 --- /dev/null +++ b/eval/recursive/3d80d04d-466d-470a-a2dd-9216c16dd0f0.cpp @@ -0,0 +1,131 @@ +#include +#include +using namespace std; + + + +char Exp[100001],*p; + + + +int Evaluare(); + + + +int Nr() + +{ + + int Val=0; + + if(*p=='(') + + { + + p++; + + Val=Evaluare(); + + p++; + + } + + else + + while(*p>='0' && *p<='9') + + { + + Val=Val*10 + (*p-'0'); + + p++; + + } + + return Val; + +} + +int Or2() + +{ + + int Val=Nr(); + + while(*p=='/' || *p=='*') + + if(*p=='/') + + { + + p++; + + Val/=Nr(); + + } + + else + + { + + p++; + + Val*=Nr(); + + } + + return Val; + +} + +int Evaluare() + +{ + + int Val=Or2(); + + while(*p=='+' || *p=='-') + + if(*p=='+') + + { + + p++; + + Val+=Or2(); + + } + + else + + { + + p++; + + Val-=Or2(); + + } + + return Val; + +} + +int main() + +{ + + ifstream f("evaluare.in"); + + f.get(Exp,100001); + + f.close(); + + ofstream g("evaluare.out"); + + p = Exp; + + g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int N = 100000; + + + +char s[N+1]; + + + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while(s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if(s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while(isdigit(s[p])) + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + fin.getline(s,100001); + + fout << expresie(); + +} diff --git a/eval/recursive/3f63eba1-0771-490b-9dbd-acee1e89aea8.cpp b/eval/recursive/3f63eba1-0771-490b-9dbd-acee1e89aea8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94113ed9b9c84a9900d1184a15c4ae18dfc15ef6 --- /dev/null +++ b/eval/recursive/3f63eba1-0771-490b-9dbd-acee1e89aea8.cpp @@ -0,0 +1,145 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100002]; + +int i; + + + +int termen(); + +int factor(); + +int calc() + +{ + + int x = termen(); + + while(s[i] == '+' || s[i] == '-') + + { + + if(s[i] == '+') + + { + + i ++; + + x += termen(); + + } + + else + + { + + i ++; + + x -= termen(); + + } + + } + + i ++; + + return x; + +} + +int termen() + +{ + + int x = factor(); + + while(s[i] == '*' || s[i] == '/') + + { + + if(s[i] == '*') + + { + + i ++; + + x *= factor(); + + } + + else + + { + + i ++; + + x /= factor(); + + } + + } + + return x; + +} + +int factor() + +{ + + int x = 0; + + if(s[i] == '(') + + { + + i ++; + + return calc(); + + } + + else + + { + + while(s[i] >= '0' && s[i] <= '9') + + { + + x = x * 10 + s[i] - '0'; + + i ++; + + } + + return x; + + } + +} + +int main() + +{ + + f.getline(s, sizeof(s)); + + g << calc(); + + return 0; + +} diff --git a/eval/recursive/3f7baf28-6be2-4f3b-843e-94fcd6261664.cpp b/eval/recursive/3f7baf28-6be2-4f3b-843e-94fcd6261664.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89d6541a73f295da1e11a908c077a4d25b65849a --- /dev/null +++ b/eval/recursive/3f7baf28-6be2-4f3b-843e-94fcd6261664.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +const int nmax=1e5+3; + +char s[nmax]; + +int fact(); + +int termen(),k; + + + +int eval() + +{ + + int r=termen(); + + while(s[k]=='+'||s[k]=='-') + + { + + ++k; + + if(s[k-1]=='-') r-=termen(); + + else r+=termen(); + + } + + return r; + +} + + + +int termen() + +{ + + int r=fact(); + + while(s[k]=='*'||s[k]=='/') + + { + + ++k; + + if(s[k-1]=='*') r*=fact(); + + else r/=fact(); + + } + + return r; + +} + + + +int fact() + +{ + + int r=0; + + if(s[k]=='(') + + { + + ++k; + + r=eval(); + + ++k; + + } + + + + while(s[k]>='0'&&s[k]<='9') + + { + + r=r*10+s[k]-'0'; + + ++k; + + } + + + + return r; + +} + + + +int main() + +{ + + f.getline(s+1,nmax-1); + + k=1; + + g< +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +template void uin (T &a, T b) {a = min (a, b);} + +template void uax (T &a, T b) {a = max (a, b);} + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +const int NMAX = 1e5 + 5; + + + +int n, pos; + +char s[NMAX]; + + + +int eval(); + + + +int getInt() { + + int ret = 0; + + while (isdigit (s[pos])) { + + (ret *= 10) += s[pos++] - '0'; + + } + + + + return ret; + +} + + + +int factor() { + + int ret = 0; + + if (s[pos] == '(') { + + ++pos; + + ret = eval(); + + ++pos; + + } else { + + ret = getInt(); + + } + + + + return ret; + +} + + + +int term() { + + int ret = factor(); + + while (s[pos] == '*' || s[pos] == '/') { + + if (s[pos++] == '*') { + + ret *= factor(); + + } else { + + ret /= factor(); + + } + + } + + + + return ret; + +} + + + +int eval() { + + int ret = 0, sign; + + ret = term(); + + while (s[pos] == '+' || s[pos] == '-') { + + if (s[pos++] == '+') sign = 1; + + else sign = -1; + + + + ret += sign * term(); + + } + + + + return ret; + +} + + + +int main() { + + ios::sync_with_stdio(0); + + cin.tie(0); + f.getline (s + 1, NMAX); + + n = strlen (s + 1); + + + + pos = 1; + + g << eval() << '\n'; + + + + f.close(); + + g.close(); + return 0; + +} diff --git a/eval/recursive/43a18c4a-c9bf-4976-9c0d-2b8ca694dec8.cpp b/eval/recursive/43a18c4a-c9bf-4976-9c0d-2b8ca694dec8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2ac58ae9e6e9b83d9597b613addda33894e7ddc --- /dev/null +++ b/eval/recursive/43a18c4a-c9bf-4976-9c0d-2b8ca694dec8.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +ifstream cin ("evaluare.in"); + +ofstream cout ("evaluare.out"); + + + +char s[100010]; + +int poz; + + + +char peek() + +{ + + return s[poz]; + +} + + + +char pop() + +{ + + return s[poz++]; + +} + + + +int solve(); + + + +int getFact() + +{ + + int result = 0; + + if( peek() == '(' ) + + { + + pop(); + + result = solve(); + + pop(); + + return result; + + } + + while( peek() >= '0' && peek() <= '9' ) + + result = result * 10 + pop() - '0'; + + return result; + +} + + + +int getTerm() + +{ + + int fact1 = getFact(); + + while( peek() == '*' || peek() == '/' ) + + if( pop() == '*' ) + + fact1 *= getFact(); + + else + + fact1 /= getFact(); + + return fact1; + +} + + + +int solve() + +{ + + int term1 = getTerm(); + + while( peek() == '-' || peek() == '+' ) + + if( pop() == '-' ) + + term1 -= getTerm(); + + else + + term1 += getTerm(); + + return term1; + +} + + + +int main() + +{ + + cin >> s; + + cout << solve(); + + return 0; + +} diff --git a/eval/recursive/450105ef-8faf-450b-9c7c-36c0dde1c634.cpp b/eval/recursive/450105ef-8faf-450b-9c7c-36c0dde1c634.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb92888a2938aa2ea72fbf69ed156cc0b5e2a9fd --- /dev/null +++ b/eval/recursive/450105ef-8faf-450b-9c7c-36c0dde1c634.cpp @@ -0,0 +1,132 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005],*p; + +int factor(); + +int elem(); + +int termen() + +{ + + int rez=factor(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') + + { + + p++; + + rez=rez+factor(); + + } + + else + + { + + p++; + + rez=rez-factor(); + + } + + } + + return rez; + +} + +int factor() + +{ + + int rez=elem(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + rez=rez*elem(); + + } + + else + + { + + p++; + + rez=rez/elem(); + + } + + } + + return rez; + +} + +int elem() + +{ int rez=0; + + if(*p=='(') + + { + + p++; + + rez=termen(); + + p++; + + return rez; + + } + + + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + return rez; + +} + +int main() + +{ + + fin>>s; + + p=s; + + fout< +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int EvaluareTermen(); + +int EvaluareFactor(); + +int EvaluareExpresie(); + +int eval(); + + + +char e[100005]; + +int lg,i; + + + +int eval() + +{ + + int nr=0; + + while(e[i]<='9' && e[i]>='0') nr=nr*10+e[i]-'0',i++; + + + + i--; + + return nr; + + + +} + +int EvaluareFactor() + +{ + + int f; + + if(e[i]=='(') + + { + + i++; + + f=EvaluareExpresie(); + + + + i++; + + } + + else f=eval(),i++; + + + + return f; + +} + +int EvaluareTermen() + +{ + + int f=EvaluareFactor(); + + while(i>e; + + lg=strlen(e); + + fout< + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int Expresie(); + +int Termen(); + +int Factor(); + +char S[100007]; + +int indexxx; + + + +int Expresie() + +{ + + int r=Termen(); + + while(S[indexxx]=='+'||S[indexxx]=='-') + + { + + indexxx++; + + if(S[indexxx-1]=='+') r+=Termen(); + + else r-=Termen(); + + } + + return r; + +} + + + +int Termen() + +{ + + int r=Factor(); + + while(S[indexxx]=='/'||S[indexxx]=='*') + + { + + indexxx++; + + if(S[indexxx-1]=='/') r/=Factor(); + + else r*=Factor(); + + } + + return r; + +} + + + +int Factor() + +{ + + int sgn=1; + + while(S[indexxx]=='-') sgn=-sgn,indexxx++; + + + + if(S[indexxx]=='(') + + { + + indexxx++; + + int r=Expresie(); + + indexxx++; + + return r*sgn; + + } + + int r=0; + + + + while(S[indexxx]>='0'&&S[indexxx]<='9') + + r=r*10+(S[indexxx]-'0'),indexxx++; + + + + return r; + +} + + + +int main() + +{ + + f>>(S+0); + + g< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + + + +char s[100002]; + +int i=0; + + + +int calc(); + +int termen(); + +int factor(); + + + +int main() + +{ + + fin>>s; + + + + fout<='0' && s[i]<='9') + + { + + x=x*10+s[i]-'0'; + + i++; + + } + + + + return x; + + } + +} diff --git a/eval/recursive/46e61dd1-ffeb-4f69-ac9c-470e6cc36ccd.cpp b/eval/recursive/46e61dd1-ffeb-4f69-ac9c-470e6cc36ccd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb73afca1ececafc28bf80cf6dfebb7c4b6d75d4 --- /dev/null +++ b/eval/recursive/46e61dd1-ffeb-4f69-ac9c-470e6cc36ccd.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; typedef vector vii; typedef vector vll; + +template std::istream& operator>>(std::istream& stream, vector &v) { for (T& t: v) { stream >> t; } return stream; } template std::ostream& operator<<(std::ostream& stream, const vector &v) { for (const T& t : v) { stream << t << ' '; } return stream; } template std::istream& operator>>(std::istream& stream, pair& p) { return stream >> p.first >> p.second; } template std::ostream& operator<<(std::ostream& stream, const pair& p) { return stream << p.first << ' ' << p.second << ' '; } + +template void read(T& t) { cin >> t; } template void read(T& t, TArgs&&... args) { read(t); read(forward(args)...); } template void readc(T& t); template<> void readc(int& v) { scanf("%d", &v); } template<> void readc(unsigned int& v) { scanf("%u", &v); } template<> void readc(char& v) { scanf(" %c", &v); } template<> void readc(long long& v) { scanf("%lld", &v); } template<> void readc(unsigned long long& v) { scanf("%llu", &v); } template<> void readc(float& v) { scanf("%f", &v); } template<> void readc(double& v) { scanf("%lf", &v); } template void readc(char (&str)[sz]) { scanf("%s", str); } template<> void readc(char*& str) { scanf("%s", str); } template void readc(pair& v) { readc(v.first); readc(v.second); } template void readc(vector& v) { for (E& e : v) readc(e); } template void readc(T& t, TArgs&&... args) { readc(t); readc(forward(args)...); } + +template void printc(T t); template<> void printc(int v) { printf("%d ", v); } template<> void printc(unsigned int v) { printf("%u ", v); } template<> void printc(char v) { printf("%c ", v); } template<> void printc(long long v) { printf("%lld ", v); } template<> void printc(unsigned long long v) { printf("%llu ", v); } template<> void printc(float v) { printf("%f ", v); } template<> void printc(double v) { printf("%lf ", v); } template<> void printc(char *str) { printf("%s ", str); } template<> void printc(const char *str) { printf("%s ", str); } template void printc(const pair& v) { printc(v.first); printc(v.second); } template void printc(const vector& v) { for (const E& e : v) printc(e); } template void printc(const T& t, TArgs&&... args) { printc(t); printc(forward(args)...); } + +template void __DEBUG_BASE(const T& t) { cout << t; } template void __DEBUG_BASE(const T& t, TArgs&&... args) { __DEBUG_BASE(t); cout << ", "; __DEBUG_BASE(forward(args)...); } template void __DEBUG_IMPL(TArgs&&... args) { __DEBUG_BASE(forward(args)...); } + +char lower(char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; return ch; } char upper(char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 'A'; return ch; } array VOWELS = {'a','e','i','o','u'}; bool isvowel(char ch) { return find(VOWELS.begin(), VOWELS.end(), lower(ch)) != VOWELS.end(); }; bool isupper(char ch) { return ch >= 'A' && ch <= 'Z'; } bool islower(char ch) { return ch >= 'a' && ch <= 'z'; } + + + + + + + +constexpr int mxN = 1e5 + 3; +char s[mxN], *ptr = s; + +int eval(); + +int read_num() { + int res = 0; + while (isdigit(*ptr)) { + res = res * 10 + (*ptr - '0'); + ++ptr; + } + return res; +} + +int nxt() { + int mult = 1; + if (*ptr == '-') mult = -1, ++ptr; + if (*ptr == '+') ++ptr; + if (*ptr == '(') { + ++ptr; + return mult * eval(); + } + return mult * read_num(); +} + +int eval() { + int res = 0; + int i; + int nx; + char sign; + while (*ptr && *ptr != ')') { + i = nxt(); + while (*ptr == '*' || *ptr == '/') { + sign = *ptr; + ++ptr; + nx = nxt(); + if (sign == '*') { + i *= nx; + } else { + i /= nx; + } + } + res += i; + } + if (*ptr == ')') ++ptr; + return res; +} +int main() +{ + ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); + + freopen("evaluare" ".in", "r", stdin); freopen("evaluare" ".out", "w", stdout); + scanf("%100000s", s); + printf("%d\n", eval()); +} diff --git a/eval/recursive/48a7b402-0230-465c-b6f0-5e1d7b948e2d.cpp b/eval/recursive/48a7b402-0230-465c-b6f0-5e1d7b948e2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecbf6c2c82d1fc745cb2d963b376f2f0e17175f0 --- /dev/null +++ b/eval/recursive/48a7b402-0230-465c-b6f0-5e1d7b948e2d.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +ifstream cin ("evaluare.in"); +ofstream cout ("evaluare.out"); +const int N=100001; + +char s[N]; + +int p; + +int evaluare(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+'||s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + + int prod=factor(); + while(s[p]=='*'||s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + + } + else + { + p++; + prod/=factor(); + } + + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn*=-1; + + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + + } + while(s[p]>='0'&&s[p]<='9') + { + + val=val*10+(s[p]-'0'); + p++; + + } + return semn*val; +} +int main() +{ + + cin>>s; + cout< +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + + + +int termen (); + +int factor(); + +int calc(); + + + +int i; + +char s[100002]; + + + +int main() + +{ + + fin>>s; + + + + fout<='0' && s[i]<='9') + + { + + x=x*10+s[i]-'0'; + + i++; + + } + + return x; + + } + +} diff --git a/eval/recursive/49a987bb-4cbd-4bb6-9540-1b31b67ba4f1.cpp b/eval/recursive/49a987bb-4cbd-4bb6-9540-1b31b67ba4f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea587d999ffe4b1462bb72c16e5198f9d3c1ffd0 --- /dev/null +++ b/eval/recursive/49a987bb-4cbd-4bb6-9540-1b31b67ba4f1.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100005], *p; + + + +int termen(); + +int factor(); + + + +int eval() + +{ + + int rez=termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p=='+') + + p++,rez+=termen(); + + else + + p++,rez-=termen(); + + + + } + + return rez; + +} + + + +int termen() + +{ + + int rez=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + p++, rez*=factor(); + + else + + p++,rez/=factor(); + + } + + return rez; + +} + + + +int factor() + +{ + + int rez=0; + + + + if(*p=='(') + + p++,rez= eval(),p++; + + else + + { + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + } + + return rez; + + + +} + +int main() + +{ + + cin >> s; + + p=s; + + cout< + + +using namespace std; + + + +char c[100000]; + +int i; + + + +int solve(); + +int mult(); + +int num(); + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +int solve() + +{ + + int t=mult(); + + while(c[i]=='+' || c[i]=='-') + + { + + if(c[i]=='+') + + { + + i++; + + t+=mult(); + } + + else if(c[i]=='-') + + { + + i++; + + t-=mult(); + + } + + } + + return t; + +} + + + +int mult() + +{ + + int t=num(); + + while(c[i]=='*' || c[i]=='/') + + { + + if(c[i]=='*') + + { + + i++; + + t*=num(); + + } + + else if(c[i]=='/') + + { + + i++; + + t/=num(); + + } + + } + + return t; + +} + + + +int num() + +{ + + int t=0; + + if(c[i]=='(') + + { + + i++; + + t=solve(); + + i++; + + } + + else + + { + + while(isdigit(c[i])) + + { + + t=t*10+(int)c[i]-48; + + i++; + + } + + } + + return t; + +} + + + +int main() + +{ + + in.getline(c, 100000); + + out << solve(); + + return 0; + +} diff --git a/eval/recursive/4b34e9ef-48a0-439b-a92e-2abdcc388066.cpp b/eval/recursive/4b34e9ef-48a0-439b-a92e-2abdcc388066.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6329fd21c72a48cfd80e9835b72ece4f0a6dea11 --- /dev/null +++ b/eval/recursive/4b34e9ef-48a0-439b-a92e-2abdcc388066.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + + +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +constexpr int NX {100010}; +char expresie[NX]; + +int prioritate(char c) +{ + if(c=='(' || c==')') return 0; + if(c=='+' || c=='-') return 1; + if(c=='*' || c=='/') return 2; +} +int semn(int st, int dr) +{ + int np{}; + for(int i=dr; i>=st; --i) + { + if(expresie[i]==')') + np++; + else if(expresie[i]=='(') + np--; + if(np==0 && (expresie[i]=='+' || expresie[i]=='-')) + return i; + } + for(int i=dr; i>=st; --i) + { + if(expresie[i]==')') + np++; + else if(expresie[i]=='(') + np--; + if(np==0 && (expresie[i]=='/' || expresie[i]=='*')) + return i; + } + return -1; +} +int getNum(int st) +{ + int nr{}; + while(isdigit(expresie[st])) + { + nr=nr*10+(expresie[st]-'0'); + st++; + } + return nr; +} +int solve(int st, int dr) +{ + int poz{semn(st, dr)}; + if(expresie[poz]=='+') + return solve(st, poz-1)+solve(poz+1, dr); + else if(expresie[poz]=='-') + return solve(st, poz-1)-solve(poz+1, dr); + else if(expresie[poz]=='*') + return solve(st, poz-1)*solve(poz+1, dr); + else if(expresie[poz]=='/') + return solve(st, poz-1)/solve(poz+1, dr); + else if(expresie[st]=='(' && expresie[dr]==')') + return solve(st+1, dr-1); + else return getNum(st); +} +int main() +{ + fin.getline(expresie, NX); + fout< +using namespace std; + + + +int expresie(); + +int termen(); + +int factor(); + + + +char s[100005]; + + + +int i,n; + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + + + scanf("%s",s+1); + + + + n=strlen(s+1); + + i=1; + + + + printf("%d\n",expresie()); + + + + + + return 0; + +} + +int expresie() + +{ + + int r=termen(); + + while(s[i]=='+' || s[i]=='-') + + { + + if(s[i]=='+') + + { + + i++; + + r+=termen(); + + } + + else + + { + + i++; + + r-=termen(); + + } + + } + + + + return r; + +} + + + + + + + +int termen() + +{ + + int r=factor(); + + while(s[i]=='*' || s[i]=='/') + + { + + if(s[i]=='*') + + { + + i++; + + r*=factor(); + + } + + else + + { + + i++; + + r/=factor(); + + } + + } + + + + return r; + +} + +int factor() + +{ + + int r; + + + + if(s[i]=='(') + + { + + i++; + + r=expresie(); + + i++; + + } + + else + + { + + r=0; + + while(isdigit(s[i])) + + { + + r=r*10+s[i]-'0'; + + i++; + + } + + } + + + + return r; + +} diff --git a/eval/recursive/4da2ce10-b265-4d29-95db-9815e246d92c.cpp b/eval/recursive/4da2ce10-b265-4d29-95db-9815e246d92c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..332a4414aceee4ec315ea5debd9ea75cefe2de0d --- /dev/null +++ b/eval/recursive/4da2ce10-b265-4d29-95db-9815e246d92c.cpp @@ -0,0 +1,169 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100005; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string s; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + if(s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + if(s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while(s[p] == '-') + + { + + p++; + + semn = - semn; + + } + + if(s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while(s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + cin >> s; + + cout << expresie(); + + return 0; + +} diff --git a/eval/recursive/4ddd56cd-129e-4751-9a24-d36e59bd5fe7.cpp b/eval/recursive/4ddd56cd-129e-4751-9a24-d36e59bd5fe7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d49268ab44a0abdc6335a6acd6b97e361c3ba644 --- /dev/null +++ b/eval/recursive/4ddd56cd-129e-4751-9a24-d36e59bd5fe7.cpp @@ -0,0 +1,166 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + in >> s; + + in.close(); + + out << expresie(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/4ef42472-4e3a-4dd1-9205-df96e697be83.cpp b/eval/recursive/4ef42472-4e3a-4dd1-9205-df96e697be83.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e8387b7c0accca1fc4df7c97f6c41eaf7edb247 --- /dev/null +++ b/eval/recursive/4ef42472-4e3a-4dd1-9205-df96e697be83.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + +ifstream f ( "evaluare.in" ); + +ofstream g ( "evaluare.out" ); + +char s[100005],*p; + + + +int expresii(); + +int factori() + +{ int r=0; + + if(*p=='(') + + { ++p; + + r=expresii(); + + ++p; + + } + + else + + { while(*p>='0' and *p<='9') + + { r=r*10+*p-'0'; + + ++p; + + } + + } + + return r; + +} + +int termeni() + +{ int r=factori(); + + while(*p=='*' || *p=='/' ) + + { if(*p=='*') + + { ++p; + + r*=factori(); + + } + + if(*p=='/') + + { ++p; + + r/=factori(); + + } + + } + + return r; + +} + +int expresii() + +{ int r=termeni(); + + while(*p=='+' || *p=='-') + + { if(*p=='+') + + { ++p; + + r+= termeni(); + + } + + else if (*p=='-') + + { ++p; + + r-= termeni(); + + } + + } + + return r; + +} + +int main() + +{ f>>s; + + p=s; + + g< +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("evaluare.in"); +ofstream fout ("evaluare.out"); + +string A; + +int eval(const string &A, int &p, int lvl); + + +int termen(const string &A, int &p, int lvl); + + +int factor(const string &A, int &p, int lvl); + +int eval(const string &A, int &p, int lvl) { + + int r = termen(A, p, lvl); + while(p < A.size() && (A[p] == '-' || A[p] == '+')) { + if (A[p] == '-') { + p++; + r -= termen(A, p, lvl); + } else { + p++; + r += termen(A, p, lvl); + } + } + + return r; +} + +int termen(const string &A, int &p, int lvl) { + + int r = factor(A, p, lvl); + while(p < A.size() && (A[p] == '*' || A[p] == '/')) { + if (A[p] == '*') { + p++; + r *= factor(A, p, lvl); + } else { + p++; + r /= factor(A, p, lvl); + } + } + + return r; +} + +int factor(const string &A, int &p, int lvl) { + + if (p >= A.size()) { return 0; } + + int r = 0; + if (A[p] == '(') { + p++; + r = eval(A, p, lvl+1); + p++; + } else { + while(p < A.size() && A[p] - '0' >= 0 && A[p] - '0' < 10) { + r = r*10 + A[p] - '0'; + p++; + } + } + + return r; +} + +int main(void) { + fin >> A; + int p = 0; + int res = eval(A, p, 0); + + fout << res << endl; + + return 0; +} diff --git a/eval/recursive/4f45ee76-cb29-4e73-a865-15bace9baa9c.cpp b/eval/recursive/4f45ee76-cb29-4e73-a865-15bace9baa9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58d556ada870f9a543e0be06cf6bafc8279179f2 --- /dev/null +++ b/eval/recursive/4f45ee76-cb29-4e73-a865-15bace9baa9c.cpp @@ -0,0 +1,130 @@ +#include +#include +#include + + +using namespace std; + + + +const int N = 100001; + +char s[N]; + +int p; + +int exprresie(); + +int termen(); + +int factor(); + +int expresie(){ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-'){ + + if(s[p] == '+'){ + + p++; + + sum+=termen(); + + } + + else{ + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen(){ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/'){ + + if(s[p] == '*'){ + + p++; + + prod*=factor(); + + } + + else{ + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + +int factor(){ + + int val = 0, semn = 1; + + while(s[p] == '-'){ + + p++; + + semn = -semn; + + } + + if(s[p] == '('){ + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while('0' <= s[p] && s[p] <= '9'){ + + val = val * 10 + (s[p]-'0'); + + p++; + + } + + return semn * val; + +} + + + + + +int main() + +{ + + ifstream f("evaluare.in"); + + ofstream g("evaluare.out"); + + f >> s; + + g << expresie(); + +} diff --git a/eval/recursive/4fa78114-d375-4530-a72a-57ed4dcf60a9.cpp b/eval/recursive/4fa78114-d375-4530-a72a-57ed4dcf60a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c621e7b8f6503382e2c59fec80dbb0a346ff313f --- /dev/null +++ b/eval/recursive/4fa78114-d375-4530-a72a-57ed4dcf60a9.cpp @@ -0,0 +1,139 @@ +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + + + +char Expresie[100005], *p = Expresie; + + + +long termen(); + +long factor(); + +long eval(); + + + +int main(){ + + + + + + fin.getline(Expresie, 100005); + + + + + + fout << eval(); + + + + fin.close(); + + fout.close(); + + return 0; + +} +long eval(){ + + long rezultat = termen(); + + while( *p == '+' || *p == '-' ){ + + switch ( *p ){ + + case '+' : + + ++p; + + rezultat += termen(); + + break; + + case '-' : + + ++p; + + rezultat -= termen(); + + break; + + } + + } + + return rezultat; + +} +long termen(){ + + long rezultat = factor(); + + while( *p == '*' || *p == '/' ){ + + switch ( *p ){ + + case '*' : + + ++p; + + rezultat *= factor(); + + break; + + case '/' : + + ++p; + + rezultat /= factor(); + + break; + + } + + } + + return rezultat; + +} +long factor(){ + + long rezultat = 0; + + if ( *p == '(' ){ + + ++p; + + rezultat = eval(); + + ++p; + + }else{ + + while( *p >= '0' && *p <= '9'){ + + rezultat = rezultat * 10 + *p - '0'; + + ++p; + + } + + } + + return rezultat; + +} diff --git a/eval/recursive/50001a36-7abb-44af-86df-256b3ccd01de.cpp b/eval/recursive/50001a36-7abb-44af-86df-256b3ccd01de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69154a40bb94ba71057c5950b31824b332177355 --- /dev/null +++ b/eval/recursive/50001a36-7abb-44af-86df-256b3ccd01de.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100001],*p=s; + + + +long termen(); + + + +long factor(); + + + +long eval() + +{ + + long r=termen(); + + while( *p == '+' || *p == '-') + + switch( *p ) + + { + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -=termen(); + + break; + + } + + return r; + +} + +long termen() + +{ + + long r=factor(); + + while( *p == '*' || *p == '/') + + switch( *p ) + + { + + case '*' : + + ++p; + + r*=factor(); + + break; + + + + case '/' : + + ++p; + + r/=factor(); + + break; + + + + } + + return r; + +} + +long factor() + +{ + + long r = 0; + + if(*p == '(' ) + + { + + ++p; + + r=eval(); + + ++p; + + } + + else while(*p >='0' && *p <= '9') + + { + + r=r*10 + *p - '0'; + + ++p; + + } + + return r; + +} + +int main() + +{ + + fin.get(s,100001); + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/51f39d2e-b81a-4e19-a6ba-b259b232e020.cpp b/eval/recursive/51f39d2e-b81a-4e19-a6ba-b259b232e020.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f9d7c91a1660a5b5f3311f785e65a1bbcf49ede --- /dev/null +++ b/eval/recursive/51f39d2e-b81a-4e19-a6ba-b259b232e020.cpp @@ -0,0 +1,141 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); ofstream fout("evaluare.out"); + + + + + + + +string s; + +int i, ans; + + + + + + + +int sum(); + +int product(); + +int parantheses(); + + + +int sum(){ + + int ans = product(); + + while (s[i] == '+' || s[i] == '-'){ + + if (s[i] == '+'){ + + i++; + + ans += product(); + + } else { + + i++; + + ans -= product(); + + } + + } + + return ans; + +} + + + +int product(){ + + int ans = parantheses(); + + while (s[i] == '*' || s[i] == '/'){ + + if (s[i] == '*'){ + + i++; + + ans *= parantheses(); + + } else { + + i++; + + ans /= parantheses(); + + } + + } + + return ans; + + + +} + + + +int parantheses(){ + + int ans = 0; + + + + if (s[i] == '('){ + + i++; + + ans = sum(); + + i++; + + } + + else { + + while (i < s.size() && '0' <= s[i] && s[i] <= '9'){ + + ans *= 10; + + ans += s[i] - '0'; + + i++; + + } + + } + + return ans; + +} + + + + + +int main() { + + + + fin >> s; + + fout << sum(); + + return 0; + +} diff --git a/eval/recursive/52331b50-64a1-4801-a581-65bb02042f3d.cpp b/eval/recursive/52331b50-64a1-4801-a581-65bb02042f3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a71dd8f3bc9a6fa4d753de88ec0a2638e6dd77c5 --- /dev/null +++ b/eval/recursive/52331b50-64a1-4801-a581-65bb02042f3d.cpp @@ -0,0 +1,141 @@ +#include +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],*p; + +int eval(); + +int termen(); + +int factor(); + +int main() + +{ + + f.getline(s,100005); + + p=s; + + g<='0'&&*p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + } + + return r; + +} diff --git a/eval/recursive/5254e387-d0f7-448b-9c9f-914e677fa6c4.cpp b/eval/recursive/5254e387-d0f7-448b-9c9f-914e677fa6c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbb1cf9d18d817c86be3f842ccebb3473bd84402 --- /dev/null +++ b/eval/recursive/5254e387-d0f7-448b-9c9f-914e677fa6c4.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100002]; + +int i; + + + +int termen(); + +int factor(); + + + +int calc() + +{ + + int x=termen(); + + while(s[i]=='+'||s[i]=='-') + + { + + if(s[i]=='+') + + { + + i++; + + x+=termen(); + + } + + else + + if(s[i]=='-') + + { + + i++; + + x-=termen(); + + } + + } + + i++; + + return x; + +} + + + +int termen() + +{ + + int x=factor(); + + while(s[i]=='*'||s[i]=='/') + + { + + if(s[i]=='*') + + { + + i++; + + x*=factor(); + + } + + else + + if(s[i]=='/') + + { + + i++; + + x/=factor(); + + } + + } + + return x; + +} + + + +int factor() + +{ + + int x=0; + + if(s[i]=='(') + + { + + i++; + + return calc(); + + } + + else + + { + + while(s[i]>='0'&&s[i]<='9') + + x=x*10+s[i]-48,i++; + + return x; + + } + +} + + + +int main() + +{ + + int sol; + + f.getline(s+1,100001); + + i=1; + + sol=calc(); + + g< +#include +using namespace std; + + + +const long n = 100001; + +char calc[n], *pa = calc; + + + +long sd(); + + + +long paranteze() + +{ + + long r = 0; + + if (*pa == '(') + + { + + pa++; + + r = sd(); + + pa++; + + } + + else + + { + + while (*pa >= '0' && *pa <= '9') + + { + + r = r * 10 + *pa - '0'; + + pa++; + + } + + } + + return r; + +} + + + +long inip() + +{ + + long r = paranteze(); + + while (*pa == '*' || *pa == '/') + + { + + if (*pa == '*') + + { + + pa++; + + r *= paranteze(); + + } + + else + + { + + pa++; + + r /= paranteze(); + + } + + } + + return r; + +} + + + +long sd() + +{ + + long r = inip(); + + while (*pa == '+' || *pa == '-') + + { + + if (*pa == '+') + + { + + pa++; + + r += inip(); + + } + + else + + { + + pa++; + + r -= inip(); + + } + + } + + return r; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.getline(calc, n); + + fout << sd(); + + return 0; + +} diff --git a/eval/recursive/532df138-a344-47d6-93b2-31acb9a5d3cb.cpp b/eval/recursive/532df138-a344-47d6-93b2-31acb9a5d3cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c38dfd675707142b264a7041124ccc43ab99cc6 --- /dev/null +++ b/eval/recursive/532df138-a344-47d6-93b2-31acb9a5d3cb.cpp @@ -0,0 +1,74 @@ +#include + + +const long MAX = 100010; +char S[MAX], *p=S; + +long termen(); +long factor(); + + + + +long eval() { + long r = termen(); + while ( *p=='+' || *p=='-' ) { + switch ( *p ) { + case '+': + ++p; + r += termen(); + break; + case '-': + ++p; + r -= termen(); + break; + } + } + return r; +} + + + + + +long termen() { + long r = factor(); + while ( *p=='*' || *p=='/' ) { + switch ( *p ) { + case '*' : + ++p; + r *= factor(); + break; + case '/': + ++p; + r /= factor(); + break; + } + } + return r; +} + + + + + +long factor() { + long r=0; + if ( *p == '(' ) { + ++p; + r = eval(); + ++p; + } else { + while ( *p>='0' && *p<='9' ) { + r = r*10 + *p - '0'; + ++p; + } + } + return r; +} + +int main() { + fgets(S, MAX, fopen("evaluare.in", "r")); + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + return 0; +} diff --git a/eval/recursive/535d84a1-f5b3-405a-92fa-4ddae102a0ef.cpp b/eval/recursive/535d84a1-f5b3-405a-92fa-4ddae102a0ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ee7d6943ed18d728c85c557709e972262f5291c --- /dev/null +++ b/eval/recursive/535d84a1-f5b3-405a-92fa-4ddae102a0ef.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N=100001; +char s[N]; +int p; +int evaluare(); +int termen(); +int factor(); + +int expresie(){ + int sum=termen(); + while(s[p]=='+' || s[p]=='-'){ + if(s[p]=='+'){ + p++; + sum+=termen(); + } + else { + p++; + sum-=termen(); + } + } + return sum; +} +int termen(){ + int prod=factor(); + while(s[p]=='*' || s[p]=='/'){ + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else{ + p++; + prod/=factor(); + } + } + return prod; +} +int factor() +{ + int val=0, semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='('){ + p++; + val=expresie(); + p++; + return semn*val; + } + while (s[p]>='0' && s[p]<='9'){ + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} +int main() +{ + in>>s; + out< + +using namespace std; +typedef long long ll; + +string file="evaluare"; + +ifstream fin(file+".in"); +ofstream fout(file+".out"); + +int termen(); +int factor(); +int calcul(); + +int i; +char s[100001]; + +int main() { + fin >> s; + fout << calcul(); + return 0; +} + +int calcul() { + int x = termen(); + while (s[i] == '+' || s[i] == '-') { + if (s[i] == '+') { + i++; + x = x + termen(); + } else { + i++; + x = x - termen(); + } + } + i++; + return x; +} + +int termen() { + int x = factor(); + while (s[i] == '*' || s[i] == '/') { + if (s[i] == '*') { + i++; + x = x * factor(); + } else { + i++; + x = x / factor(); + } + } + return x; +} + +int factor() { + int x = 0; + if (s[i] == '(') { + i++; + return calcul(); + } else { + while (s[i] >= '0' && s[i] <= '9') { + x = x * 10 + s[i] - '0'; + i++; + } + return x; + } +} diff --git a/eval/recursive/554d0918-e128-48b7-b4ec-4b0fffb19cdf.cpp b/eval/recursive/554d0918-e128-48b7-b4ec-4b0fffb19cdf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..236238ef7392d12c5df1bce5cce512b9f29eeac3 --- /dev/null +++ b/eval/recursive/554d0918-e128-48b7-b4ec-4b0fffb19cdf.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; + +long long int sol, i; + +int expresie(); + +int factor(); + +int termen(); + +char S[100010]; + +int main() + +{ + + ifstream fin ("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.get(S,1000001); + + sol=expresie(); + + fout<='0'&&S[i]<='9') + + { + + r=r*10+S[i]-'0'; + + i++; + + } + + } + + return r; + +} diff --git a/eval/recursive/5563a5c0-9c57-42bf-8f88-ba49a276fbd0.cpp b/eval/recursive/5563a5c0-9c57-42bf-8f88-ba49a276fbd0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e6aa1be57b29f7b1b3b4a2c30561f32233e7e6d --- /dev/null +++ b/eval/recursive/5563a5c0-9c57-42bf-8f88-ba49a276fbd0.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + +const int N = 100001; +char s[N]; +int p; +int exprresie(); +int termen(); +int factor(); +int expresie(){ + int sum = termen(); + while(s[p] == '+' || s[p] == '-'){ + if(s[p] == '+'){ + p++; + sum+=termen(); + } + else{ + p++; + sum-=termen(); + } + } + return sum; +} +int termen(){ + int prod = factor(); + while(s[p] == '*' || s[p] == '/'){ + if(s[p] == '*'){ + p++; + prod*=factor(); + } + else{ + p++; + prod/=factor(); + } + } + return prod; +} +int factor(){ + int val = 0, semn = 1; + while(s[p] == '-'){ + p++; + semn = -semn; + } + if(s[p] == '('){ + p++; + val = expresie(); + p++; + return semn * val; + } + while('0' <= s[p] && s[p] <= '9'){ + val = val * 10 + (s[p]-'0'); + p++; + } + return semn * val; +} + + +int main() +{ + ifstream f("evaluare.in"); + ofstream g("evaluare.out"); + f >> s; + g << expresie(); +} diff --git a/eval/recursive/5573ec56-4aea-4053-89f1-ab95fa054063.cpp b/eval/recursive/5573ec56-4aea-4053-89f1-ab95fa054063.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7813627e05c23b274a2ac79fbea3b4422147621 --- /dev/null +++ b/eval/recursive/5573ec56-4aea-4053-89f1-ab95fa054063.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + + + int sum = termen(); + + + + while(s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + + + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1,val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] =='(' ) + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val*10 + (s[p] - '0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + return 0; + +} diff --git a/eval/recursive/55ae3def-40e4-4b94-b302-2cc6946aacc6.cpp b/eval/recursive/55ae3def-40e4-4b94-b302-2cc6946aacc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca6a5f78b667373433fa0440eac9cb8be7ae9dc7 --- /dev/null +++ b/eval/recursive/55ae3def-40e4-4b94-b302-2cc6946aacc6.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +using namespace std; + + + +char e[100001]; + +int n, i; + + + +int evalExpresie(); + +int evalTermen(); + +int evalFactor(); + +int formareNumar(); + + + +int main() { + + ifstream f("evaluare.in"); + + f.getline(e, 100001); + + f.close(); + + n = strlen(e); + + ofstream g("evaluare.out"); + + g << evalExpresie(); + + g.close(); + + return 0; + +} + + + +int evalFactor() { + + int p; + + if (e[i] == '(') { + + i++; + + p = evalExpresie(); + + i++; + + } + + else + + p = formareNumar(); + + return p; + +} + + + +int evalTermen() { + + int q = evalFactor(); + + while (i < n && (e[i] == '*' || e[i] == '/')) { + + i++; + + if (e[i - 1] == '*') + + q *= evalFactor(); + + else q /= evalFactor(); + + } + + return q; + +} + + + +int evalExpresie() { + + int t = evalTermen(); + + while (i < n && (e[i] == '+' || e[i] == '-')) { + + i++; + + if (e[i - 1] == '+') + + t += evalTermen(); + + else t -= evalTermen(); + + } + + return t; + +} + + + +int formareNumar() { + + int nr = 0; + + while (isdigit(e[i]) && i < n) { + + nr = nr * 10 + e[i] - '0'; + + i++; + + } + + return nr; + +} diff --git a/eval/recursive/579fe813-2b64-48aa-aa83-2b0f06de093b.cpp b/eval/recursive/579fe813-2b64-48aa-aa83-2b0f06de093b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfa962c7490f5d3fa1712671b0dcccc38c0151f7 --- /dev/null +++ b/eval/recursive/579fe813-2b64-48aa-aa83-2b0f06de093b.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +int n, p; + +char s[100005]; + +int adunare_scadere(int & p); + +int inmultire_impartire(int & p); + +int paranteze(int & p); + +int valoare(int & p); + +int adunare_scadere(int & p) + +{ + + int rez = inmultire_impartire(p); + + while(p < n && (s[p] == '+' || s[p] == '-')) + + { + + p ++; + + if(s[p - 1] == '+')rez = rez + inmultire_impartire(p); + + else rez = rez - inmultire_impartire(p); + + } + + return rez; + +} + +int inmultire_impartire(int & p) + +{ + + int rez = paranteze(p); + + while(p < n && (s[p] == '*' || s[p] == '/')) + + { + + p ++; + + if(s[p - 1] == '*')rez = rez * paranteze(p); + + else rez = rez / paranteze(p); + + } + + return rez; + +} + +int paranteze(int & p) + +{ + + int rez = 0; + + if(s[p] == '(') + + { + + p ++; + + rez = adunare_scadere(p); + + p ++; + + } + + else rez = valoare(p); + + return rez; + +} + +int valoare(int & p) + +{ + + int semn, rez = 0; + + if(s[p] == '-')semn = -1, p ++; + + else semn = 1; + + while(s[p] >= '0' && s[p] <= '9' && p < n) + + { + + rez = rez * 10 + (s[p] - '0'); + + p ++; + + } + + return semn * rez; + +} + +int main() + +{ + + ifstream f("evaluare.in"); + + ofstream g("evaluare.out"); + + f.getline(s, sizeof(s)); + + n = strlen(s); + + g << adunare_scadere(p); + + return 0; + +} diff --git a/eval/recursive/582d3f86-dda9-444b-8df9-709b11b1778b.cpp b/eval/recursive/582d3f86-dda9-444b-8df9-709b11b1778b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1b0fa0b489392212998e103127b50ca2090fafb --- /dev/null +++ b/eval/recursive/582d3f86-dda9-444b-8df9-709b11b1778b.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +char s[100003], *p=s; +long long i; + +long long eval1(); +long long eval2(); +long long eval(){ + long long rez=eval1(); + while(*p=='+'||*p=='-'){ + if(*p=='+'){ + ++p; + rez+=eval1(); + } + else{ + ++p; + rez-=eval1(); + } + } + return rez; +} +long long eval1(){ + long long rez=eval2(); + while(*p=='*'||*p=='/'){ + if(*p=='*'){ + ++p; + rez*=eval2(); + } + else { + ++p; + rez/=eval2(); + } + } + return rez; +} +long long eval2(){ + long long rez=0; + if(*p=='('){ + ++p; + rez=eval(); + ++p; + } + else while(isdigit(*p)){ + rez=rez*10+*p-'0'; + ++p; + } + return rez; +} +int32_t main(){ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + cin >> s; + cout << eval(); +} diff --git a/eval/recursive/58564467-8c48-4ea0-babd-e8e13b2a2558.cpp b/eval/recursive/58564467-8c48-4ea0-babd-e8e13b2a2558.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8836565c2df7166bf1f1e390d5fd46893979fc37 --- /dev/null +++ b/eval/recursive/58564467-8c48-4ea0-babd-e8e13b2a2558.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +const int N = 100001; + +char s[N]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + +int expresie() { + + int sum = termen(); + + while (s[p] == '+' || s[p] == '-') { + + if (s[p] == '+') { + + p++; + + sum += termen(); + + } + + else { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + +int termen() { + + int produs = factor(); + + while (s[p] == '*' || s[p] == '/') { + + if (s[p] == '*') { + + p++; + + produs *= factor(); + + } + + else { + + p++; + + produs /= factor(); + + } + + } + + return produs; + +} + +int factor() { + + int val = 0, semn = 1; + + while (s[p] == '-') { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while ('0' <= s[p] && s[p] <= '9') { + + val = val * 10 + (s[p++] - '0'); + + } + + return semn * val; + +} + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + in >> s; + + out << expresie(); + +} diff --git a/eval/recursive/5a6db910-4d3d-4737-957b-09f3dab44e65.cpp b/eval/recursive/5a6db910-4d3d-4737-957b-09f3dab44e65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db60ef0cc45df7ca1012eea28a02af052aeae880 --- /dev/null +++ b/eval/recursive/5a6db910-4d3d-4737-957b-09f3dab44e65.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; +const int N=100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); +int expresie() +{ + int sum=termen(); + while(s[p]=='+'|| s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} +int termen() +{ + int prod=factor(); + while(s[p]=='*'|| s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0' && s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} +int main() +{ +cin.getline(s,N); +cout< +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100001],*p; + +int r; + +int eval(); + +int termene(); + +int factor(); + +int main() + +{f.getline(s,100001); + +p=s; + +r=eval(); + +g<=*p) + +r=r*10+*p-'0',p++; + +return r; + +} diff --git a/eval/recursive/5bcae7d1-7f45-4e18-b51e-4095cd7b61b9.cpp b/eval/recursive/5bcae7d1-7f45-4e18-b51e-4095cd7b61b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37b50a2368b9b90b22692274ec7cf433571edec6 --- /dev/null +++ b/eval/recursive/5bcae7d1-7f45-4e18-b51e-4095cd7b61b9.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N = 100001; +char s[N]; +int p; + +int expresie(); +int termen(); +int factor(); + +int expresie() { + int sum = termen(); + while (s[p] == '+' || s[p] == '-') { + if (s[p] == '+') { + p++; + sum += termen(); + } + else { + p++; + sum -= termen(); + } + } + + return sum; +} + +int termen() { + int sum = factor(); + while (s[p] == '*' || s[p] == '/') { + if (s[p] == '*') { + p++; + sum *= factor(); + } + else { + p++; + sum /= factor(); + } + } + + return sum; +} + +int factor() { + int val = 0, semn = 1; + + while (s[p] == '-') { + p++; + semn = -semn; + } + + if (s[p] == '(') { + p++; + val = expresie(); + p++; + + return semn * val; + } + + while (s[p] >= '0' && s[p] <= '9') { + val = val * 10 + (s[p] - '0'); + p++; + } + + return semn * val; +} + +int main() +{ + in >> s; + out << expresie() << endl; + return 0; +} diff --git a/eval/recursive/5c563f92-9aad-4f7a-b119-f8e7209fc478.cpp b/eval/recursive/5c563f92-9aad-4f7a-b119-f8e7209fc478.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45027e573e36fa512ee47bfba7ecd68d0a82f31b --- /dev/null +++ b/eval/recursive/5c563f92-9aad-4f7a-b119-f8e7209fc478.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + +int i; + + + +int f1(); + +int f2(); + +int f3(); + + + + + +int f1(){ + + int raspuns = f2(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + + i++; + + raspuns += f2(); + + }else{ + + i++; + + raspuns -= f2(); + + } + + } + + return raspuns; + +} + +int f2(){ + + int raspuns = f3(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + + i++; + + raspuns *= f3(); + + }else{ + + i++; + + raspuns /= f3(); + + } + + } + + return raspuns; + +} + + + +int f3(){ + + int raspuns = 0; + + if(s[i] == '('){ + + i++; + + raspuns = f1(); + + i++; + + }else{ + + while(isdigit(s[i])){ + + raspuns = raspuns * 10 + s[i] - '0'; + + i++; + + } + + } + + return raspuns; + +} + + + +int main() + +{ + + in>>s; + + out< +#include +#include +using namespace std; + + +char ex[100010],*p = ex; + +long termen(); + +long factor(); + +long eval(){ + long r = termen(); + while(*p == '+' || *p == '-'){ + if(*p == '+'){ + ++p; + r += termen(); + + } + if(*p == '-'){ + ++p; + r -= termen(); + + } + } + return r; + +} + +long termen(){ + long r = factor(); + while(*p == '*' || *p == '/'){ + if(*p == '*'){ + ++p; + r *= factor(); + + } + if(*p == '/'){ + ++p; + r /= factor(); + } + + } + return r; +} + +long factor(){ + long r = 0; + if(*p == '('){ + ++p; + r = eval(); + ++p; + }else{ + while(*p >= '0' && *p <= '9'){ + r = r*10 + *p - '0'; + ++p; + + } + } + return r; + +} + +int main(){ + + fgets(ex, 100010, fopen("evaluare.in", "r")); + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + return 0; +} diff --git a/eval/recursive/5e340b5c-1208-461c-ba0f-ef4e7b40f4d4.cpp b/eval/recursive/5e340b5c-1208-461c-ba0f-ef4e7b40f4d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fa9c26c8a9a74d43e9214fbe77a7ba332af95f2 --- /dev/null +++ b/eval/recursive/5e340b5c-1208-461c-ba0f-ef4e7b40f4d4.cpp @@ -0,0 +1,184 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string s; + + + +int i; + + + +inline int Evaluate(), Term(), Factor(), Power(), Exponent(); + + + +int main() + +{ ios_base::sync_with_stdio(false); + + cin.tie(nullptr), cout.tie(nullptr); + + cin>>s; + + cin.close(); + + cout< +#include +using namespace std; + +long long int sol, i; + +int expresie(); + +int termen (); + +int factor(); + +char s[100010]; + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.get(s,1000001); + + sol=expresie(); + + fout<= '0' && s[i] <= '9') { + + r = r*10 + s[i]-'0'; + + i++; + + } + + } + + return r; + +} diff --git a/eval/recursive/5edb3a25-7dc9-4dec-95a9-f008e07889d2.cpp b/eval/recursive/5edb3a25-7dc9-4dec-95a9-f008e07889d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aa37726e59c45e9bd135fa6131967b5ece5ca25 --- /dev/null +++ b/eval/recursive/5edb3a25-7dc9-4dec-95a9-f008e07889d2.cpp @@ -0,0 +1,174 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +long addOrSub(); + +long factor(); + +long term(); + + + +string input; + +int position ; + + + +long addOrSub() + +{ + + long result = factor(); + + while (position < input.size() && (input[position] == '+' || input[position] == '-')) + + { + + if (input[position] == '+') + + { + + position++; + + result = result + factor(); + + } + + else if (input[position] == '-') + + { + + position++; + + result = result - factor(); + + } + + } + + return result; + +} + + + +long factor() + +{ + + long result = term(); + + while (position < input.size() && (input[position] == '*' || input[position] == '/')) + + { + + if (input[position] == '*') + + { + + position++; + + result = result * term(); + + } + + else if (input[position] == '/') + + { + + position++; + + result = result / term(); + + } + + } + + return result; + +} + + + +long term() + +{ + + long result = 0; + + + + if (input[position] == '(') + + { + + position++; + + result = addOrSub(); + + position++; + + return result; + + } + + else + + { + + long semn = 1; + + if (input[position] == '-') + + { + + position++; + + semn = -1; + + } + + + + while (position < input.size() && '0' <= input[position] && input[position] <= '9') + + { + + result = result * 10 + (input[position] - '0'); + + position++; + + } + + result *= semn; + + } + + return result; + +} + + + +int main() + +{ + + fin >> input; + + fout << addOrSub(); + + return 0; + +} diff --git a/eval/recursive/5f1bb97d-5682-44e3-8c85-42ea7d453303.cpp b/eval/recursive/5f1bb97d-5682-44e3-8c85-42ea7d453303.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad9f53c7bfaaed0701e35f887b3ae2d3ccbec94b --- /dev/null +++ b/eval/recursive/5f1bb97d-5682-44e3-8c85-42ea7d453303.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100100]; + +int afla_termen(),adunare(),inmultire(); + +int i,N; + + + +int afla_termen() + +{ + + int termen; + + if(s[i]=='(') + + { + + i++; + + termen=adunare(); + + i++; + + } + + else + + { + + termen=0; + + while('0'<=s[i] && s[i]<='9') + + { + + termen=termen*10+(s[i]-'0'); + + i++; + + } + + } + + return termen; + +} + + + +int adunare() + +{ + + int suma=inmultire(); + + while((s[i]=='+' || s[i]=='-') && i>s; + + N=strlen(s);i=0; + + g< +#include + +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +const int N=100001; + +char s[N]; + +int p; +int evaluare(); +int termen(); +int factor(); +int expresie() +{ + int sum=termen(); + while (s[p]=='+' || s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + + return sum; +} +int termen() +{ + int prod=factor(); + while (s[p]=='*'|| s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + + return prod; +} +int factor() +{ + int val=0, semn=1; + while (s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + + } + while (s[p]>='0' && s[p]<='9') + { + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; + + +} +int main() +{ + fin>>s; + fout< +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int exp(), term(), fact(); + + + +int i; + +char s[100010]; + + + +int exp(){ + + int r=term(); + + + + while(s[i]=='+' || s[i]=='-'){ + + i++; + + if(s[i-1]=='+'){ + + r+=term(); + + } + + else { + + r-=term(); + + } + + } + + + + return r; + +} + + + +int term(){ + + int r=fact(); + + while(s[i]=='*' || s[i]=='/'){ + + i++; + + if(s[i-1]=='*') { + + r*=fact(); + + } + + else { + + r/=fact(); + + } + + } + + + + return r; + +} + + + +int fact(){ + + int semn=1; + + if(s[i]=='-') + + semn=-1,i++; + + int r; + + if(s[i]=='('){ + + i++; + + r=exp(); + + i++; + + } + + else{ + + r=0; + + while(s[i]>='0' && s[i]<='9') + + r=r*10+s[i++]-'0'; + + } + + return r*semn; + +} + + + +int main(){ + + fin>>s; + + fout< +const long MAX = 100010; + + + +char s[MAX]; + +int i = 0; + + + + + + + +long termen(); + + + +long factor(); +long eval() { + + + + long rezultat = termen(); + + + + while ( s[i] =='+' || s[i]=='-' ) { + + + + switch ( s[i] ) { + + + + case '+': + + + + ++i; + + + + rezultat += termen(); + + + + break; + + + + case '-': + + + + ++i; + + + + rezultat -= termen(); + + + + break; + + + + } + + + + } + + + + return rezultat; + + + +} +long termen() { + + + + long rezultat = factor(); + + while(s[i] == '*' || s[i] == '/') + + { + + switch(s[i]) + + { + + case '*': + + i++; + + rezultat *= factor(); + + break; + + case '/': + + i++; + + rezultat /= factor(); + + break; + + + + } + + } + + + + return rezultat; + +} +long factor() { + + + + long rezultat = 0; + + + + if(s[i] == '(') + + { + + i++; + + rezultat = eval(); + + i++; + + } + + else + + { + + while(s[i] >= '0' && s[i] <= '9') + + { + + rezultat = rezultat * 10 + s[i] -'0'; + + i++; + + } + + } + + return rezultat; + +} + + + + + + + +int main() { + + + + fgets(s, MAX, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + + + return 0; + + + +} diff --git a/eval/recursive/6165a22e-67ce-4465-9fd6-fd7ab16004ae.cpp b/eval/recursive/6165a22e-67ce-4465-9fd6-fd7ab16004ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eaf73ee9d0133f4a3eda3ffffa3fa81f18e46a0b --- /dev/null +++ b/eval/recursive/6165a22e-67ce-4465-9fd6-fd7ab16004ae.cpp @@ -0,0 +1,93 @@ +#include + + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char sir[100009];int n,p; + +int termen () ; + +int factor (); + + + +int eval () { + + int r=termen(); + + while(sir[p]=='+' || sir[p]=='-') { + + if(sir[p]=='+') + + ++p,r+=termen(); + + else + + ++p,r-=termen(); + + } + + return r; + +} + +int termen () { + + int r=factor(); + + while(sir[p]=='*' || sir[p]=='/') { + + if(sir[p]=='*') + + ++p,r*=factor(); + + else + + ++p,r/=factor(); + + } + + return r; + +} + +int factor () { + + int r=0; + + if(sir[p]=='(') { + + ++p;r=eval(); + + ++p; + + } + + else { + + while(sir[p]>='0' && sir[p]<='9') + + r=r*10+sir[p]-'0',++p; + + } + + return r; + +} + + + +int main () { + + fin.getline(sir+1,100005);p=1;n=strlen(sir+1); + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int N=100003; + +char s[N]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+'||s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*'||s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + semn=-semn; + + p++; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return val*semn; + + } + + while(s[p]>='0'&&s[p]<='9') + + { + + val=val*10+s[p]-48; + + p++; + + } + + return val*semn; + +} + + + +int main() + +{ + + fin.getline(s,100001); + + fout< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int grad1(),grad2(),grad3(); + + + + + + + +char s[100005]; + +int i; + +int grad1() + +{ + + int r=grad2(); + + while(s[i]=='+'||s[i]=='-') + + { + + i++; + + if(s[i-1]=='-') + + r-=grad2(); + + else + + r+=grad2(); + + } + + return r; + +} + +int grad2() + +{ + + int r=grad3(); + + while(s[i]=='*'||s[i]=='/') + + { + + i++; + + if(s[i-1]=='*') + + r*=grad3(); + + else + + r/=grad3(); + + } + + return r; + +} + +int grad3() + +{ + + int r=0; + + if(s[i]=='(') + + { + + i++; + + r=grad1(); + + i++; + + return r; + + } + + while(s[i]>='0'&&s[i]<='9') + + { + + r=r*10+s[i]-'0'; + + i++; + + } + + return r; + +} + +int main() + +{ + + fin>>s; + + i=0; + + fout< +#include + + +using namespace std; + + + +const int NMAX = 100000; + + + +int v[NMAX]; + +int poz; + + + +static inline void inainteaza() { + + poz ++; + +} + +static inline char current_Char() { + + return v[poz]; + +} + +inline int numar() { + + char ch; + + int nr = 0; + + while ( isdigit( ch = current_Char() ) ) { + + nr = nr * 10 + ch - '0'; + + inainteaza(); + + } + + return nr; + +} + +int termen(); + +int expresie(); + +int factor(); + + + +int factor() { + + int semn = 1, val; + + while ( current_Char() == '-' ) { + + semn = -semn; + + inainteaza(); + + } + + if ( current_Char() == '(' ) { + + inainteaza(); + + val = expresie(); + + inainteaza(); + + } else { + + val = numar(); + + } + + return val; + +} + +int termen() { + + int val = factor(); + + while ( current_Char() == '*' || current_Char() == '/' ) { + + if ( current_Char() == '*' ) { + + inainteaza(); + + val *= factor(); + + } else { + + inainteaza(); + + val /= factor(); + + } + + } + + return val; + +} + +int expresie() { + + int val = termen(); + + while ( current_Char() == '+' || current_Char() == '-' ) { + + if ( current_Char() == '+' ) { + + inainteaza(); + + val += termen(); + + } else { + + inainteaza(); + + val -= termen(); + + } + + } + + return val; + +} + +int main() { + + FILE *fin = fopen( "evaluare.in", "r" ), *fout = fopen( "evaluare.out", "w" ); + + char ch; + + int i = 0; + + while ( ( ch = fgetc( fin ) ) != '\n' ) { + + v[i ++] = ch; + + } + + fprintf( fout, "%d", expresie() ); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/eval/recursive/64889db0-3b07-41ab-a601-875d5493f656.cpp b/eval/recursive/64889db0-3b07-41ab-a601-875d5493f656.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2618f37fd653b97ca9eae39a01bf28435c2575 --- /dev/null +++ b/eval/recursive/64889db0-3b07-41ab-a601-875d5493f656.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/651edacf-ae67-4c72-b148-436491ccd994.cpp b/eval/recursive/651edacf-ae67-4c72-b148-436491ccd994.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca1caa83d81f4e9d11751f61b4dc7a0722a10bff --- /dev/null +++ b/eval/recursive/651edacf-ae67-4c72-b148-436491ccd994.cpp @@ -0,0 +1,164 @@ +#include + + +using namespace std; + + + +const int N = 100000; + + + +char s[N+1]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int semn = 1, val = 0; + + while(s[p]=='-') + + { + + p++; + + semn = -semn; + + } + + if(s[p]=='(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val = val*10 + (s[p]-'0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + in >> s; + + in.close(); + + out << expresie(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/65e45dd0-7869-40e7-9d6e-c8b21c0f1a51.cpp b/eval/recursive/65e45dd0-7869-40e7-9d6e-c8b21c0f1a51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0184820327d24473232b4ee373e51f08820ea59b --- /dev/null +++ b/eval/recursive/65e45dd0-7869-40e7-9d6e-c8b21c0f1a51.cpp @@ -0,0 +1,154 @@ +#include +#include +#include + + +using namespace std; + + + +const int N = 100001; + +char s[N]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p ++; + + sum += termen(); + + } + + else + + { + + p ++; + + sum -= termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod = factor(); + + while( s[p] == '*' || s[p] == '/') + + { + + if( s[p] == '*') + + { + + p ++; + + prod *= factor(); + + } + + else + + { + + p ++; + + prod /= factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int val = 0, semn = 1; + + while(s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if(s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while( '0' <= s[p] && s[p] <= '9' ) + + { + + val = val * 10 + ( s[p] - '0' ); + + p ++; + + } + + return semn * val; + +} + + + + + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + in >> s; + + out << expresie(); + +} diff --git a/eval/recursive/67b9766c-aa2c-483a-bec1-c8eaa52f8cb7.cpp b/eval/recursive/67b9766c-aa2c-483a-bec1-c8eaa52f8cb7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d4f7196cc9d04b30e7950ad15dbdc4edfe50cf1 --- /dev/null +++ b/eval/recursive/67b9766c-aa2c-483a-bec1-c8eaa52f8cb7.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +const int N=100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); +int expresie() +{ + int sum=termen(); + while(s[p]=='+'||s[p]=='-') + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + return sum; +} +int termen() +{ + int prod=factor(); + while(s[p]=='*'||s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn-=semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(isdigit(s[p])) + { + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} +int main() +{ + fin>>s; + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + + + +const int N=100000; + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + }else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + }else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0; + + int semn=1; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + } + + + + while(isdigit(s[p])) + + { + + val=val*10+(s[p++] - '0'); + + } + + return semn*val; + +} + + + +int main() + +{ + + p=0; + + fin.getline(s, 100001); + + fout< +#include +const long MAX = 100010; + + + +char S[MAX],s[MAX]; + +int i = 0; + + + +int numar() + +{ + + int nr=0; + + while(s[i]>='0' && s[i]<='9') + + { + + nr=nr*10+s[i]-'0'; + + ++i; + + } + + return nr; + +} + + + +long termen(); + + + +long factor(); +long eval() { + + + + long rezultat = termen(); + + + + while ( s[i] =='+' || s[i]=='-' ) { + + + + switch ( s[i] ) { + + + + case '+': + + + + ++i; + + + + rezultat += termen(); + + + + break; + + + + case '-': + + + + ++i; + + + + rezultat -= termen(); + + + + break; + + + + } + + + + } + + + + return rezultat; + + + +} +long termen() { + + long rezultat=factor(); + + while ( s[i] =='*' || s[i]=='/' ) { + + + + switch ( s[i] ) { + + + + case '*': + + + + ++i; + + + + rezultat *= factor(); + + + + break; + + + + case '/': + + + + ++i; + + + + rezultat /= factor(); + + + + break; + + + + } + + + + } + + + + return rezultat; + + + +} +long factor() { + + int nr=0; + + if(s[i]=='(') + + { + + i++; + + nr=eval(); + + i++; + + } + + if(s[i]>='0' && s[i]<='9') + + nr=numar(); + + + + return nr; + +} + + + + + + + +int main() { + + + + fgets(s, MAX, fopen("evaluare.in", "r")); + + + + + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + + + return 0; + + + +} diff --git a/eval/recursive/695a633a-3593-45d6-928a-cbbdabbfe219.cpp b/eval/recursive/695a633a-3593-45d6-928a-cbbdabbfe219.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0843833d99594f1a5f7516058c33f69a16f5911 --- /dev/null +++ b/eval/recursive/695a633a-3593-45d6-928a-cbbdabbfe219.cpp @@ -0,0 +1,162 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +const int N = 1e5+1; + +char text[N]; + +int index = 0; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(text[index]=='+' || text[index]=='-') + + { + + if(text[index]=='+') + + { + + ++index; + + sum+=termen(); + + } + + else + + { + + ++index; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(text[index]=='*' || text[index]=='/') + + { + + if(text[index]=='*') + + { + + ++index; + + prod*=factor(); + + } + + else + + { + + ++index; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0,semn = 1; + + while(text[index]=='-') + + { + + ++index; + + semn*=-1; + + } + + if(text[index]=='(') + + { + + ++index; + + val = expresie(); + + ++index; + + return val*semn; + + } + + while(isdigit(text[index])) + + { + + val=val*10+(text[index]-'0'); + + ++index; + + } + + return semn*val; + +} + + + +int main() + +{ + + cin.getline(text,N); + + cout< +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +string s; + +int termen(int&); +int expresie(int&); +int factor(int&); +int numar(int&); + +int expresie(int &p) +{ + int r = termen(p); + while (s[p]=='+' || s[p]=='-') + { + p++; + if (s[p-1]=='+') r+=termen(p); + else r-=termen(p); + } + return r; +} +int termen(int &p) +{ + int r = factor(p); + while(s[p]=='/' || s[p]=='*') + { + p++; + if (s[p-1]=='*') r*=factor(p); + else r/=factor(p); + } + return r; +} +int factor(int &p) +{ + int r=0; + if (s[p]=='(') + { + p++; + r = expresie(p); + if (s[p]==')') p++; + } + else r=numar(p); + return r; +} +int numar(int &p) +{ + int nr=0; + while(s[p]>='0' && s[p]<='9') + { + nr*=10; + nr+=(s[p]-'0'); + p++; + } + return nr; +} +int main() +{ + fin>>s; + int p=0; + fout< +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N=100000; + +char s[N+1]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod = factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod=prod*factor(); + + } + + else + + { + + p++; + + prod=prod/factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int semn=1, val=0; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10 + (s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in>>s; + + out< +#include +const long MAX = 100010; + + + +char s[MAX]; + +int i = 0; + + + + + + + +long termen(); + + + +long factor(); +long eval() { + + + + long rezultat = termen(); + + + + while ( s[i] =='+' || s[i]=='-' ) { + + + + switch ( s[i] ) { + + + + case '+': + + + + ++i; + + + + rezultat += termen(); + + + + break; + + + + case '-': + + + + ++i; + + + + rezultat -= termen(); + + + + break; + + + + } + + + + } + + + + return rezultat; + + + +} +long termen() { + + int rezultat = factor(); + + while(s[i] == '*' || s[i] == '/') + + { + + switch(s[i]) + + { + + case '*': + + i++; + + rezultat *=factor(); + + break; + + case '/': + + i++; + + rezultat /=factor(); + + break; + + } + + } + + return rezultat; + +} +long factor() { + + if(s[i] == '(') + + { + + i++; + + int rez = eval(); + + i++; + + return rez; + + } + + int rez = 0; + + while(isdigit(s[i])) + + { + + rez = rez*10 + (s[i++]-'0'); + + } + + return rez; + +} + + + + + + + +int main() { + + + + fgets(s, MAX, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + + + return 0; + + + +} diff --git a/eval/recursive/6bd13aca-f59b-48a1-8c86-48d0b47bc10a.cpp b/eval/recursive/6bd13aca-f59b-48a1-8c86-48d0b47bc10a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6961bd7179f81a417d86c2446b37b7d689f87f33 --- /dev/null +++ b/eval/recursive/6bd13aca-f59b-48a1-8c86-48d0b47bc10a.cpp @@ -0,0 +1,126 @@ +#include + + +const long MAX = 100010; + +char S[MAX], *p=S; + + + +long termen(); + +long factor(); + + + + + +long eval() { + + long r = termen(); + + while ( *p=='+' || *p=='-' ) { + + switch ( *p ) { + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -= termen(); + + break; + + } + + } + + return r; + +} + + + +long termen() { + + long r = factor(); + + while ( *p=='*' || *p=='/' ) { + + switch ( *p ) { + + case '*' : + + ++p; + + r *= factor(); + + break; + + case '/': + + ++p; + + r /= factor(); + + break; + + } + + } + + return r; + +} + + + + + +long factor() { + + long r=0; + + if ( *p == '(' ) { + + ++p; + + r = eval(); + + ++p; + + } else { + + while ( *p>='0' && *p<='9' ) { + + r = r*10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + + + +int main() { + + fgets(S, MAX, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + return 0; + +} diff --git a/eval/recursive/6c881823-73d6-4714-8e47-caf2bd815033.cpp b/eval/recursive/6c881823-73d6-4714-8e47-caf2bd815033.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18e2bf2d15b6528b185e8d44b60f74b290be907b --- /dev/null +++ b/eval/recursive/6c881823-73d6-4714-8e47-caf2bd815033.cpp @@ -0,0 +1,201 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int evaluate(char* &expr); + +int operand(char* &expr); + +int value(char* &expr); + +int ADD_SUBSTRACT(char*& expr); + +int MULTIPLY_DIVIDE(char* &expr); + + + +char s[100001]; + + + +int main() + +{ + + char *expr; + + + + fin >> s; + + expr = s; + + fout << evaluate(expr); + + return 0; + +} + + + +int evaluate(char* &expr) + +{ + + return ADD_SUBSTRACT(expr); + +} + + + +int ADD_SUBSTRACT(char*& expr) + +{ + + auto result = MULTIPLY_DIVIDE(expr); + + + + while (expr[0] == '+' || expr[0] == '-') + + { + + switch (expr[0]) + + { + + case '+': + + result += MULTIPLY_DIVIDE(++expr); + + break; + + case '-': + + result -= MULTIPLY_DIVIDE(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int MULTIPLY_DIVIDE(char* &expr) + +{ + + auto result = operand(expr); + + + + while (expr[0] == '*' || expr[0] == '/') + + { + + switch (expr[0]) + + { + + case '*': + + result *= operand(++expr); + + break; + + case '/': + + result /= operand(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int operand(char* &expr) + +{ + + if (expr[0] == '(') + + { + + auto result = evaluate(++expr); + + expr++; + + return result; + + } + + else + + { + + return value(expr); + + } + +} + + + +int value(char* &expr) + +{ + + int nr = 0; + + + + while (isdigit(expr[0])) + + { + + nr = nr * 10 + (expr[0] - '0'); + + expr++; + + } + + + + return nr; + +} diff --git a/eval/recursive/6ce28836-cfd6-4185-9a67-e7e1a92e0eea.cpp b/eval/recursive/6ce28836-cfd6-4185-9a67-e7e1a92e0eea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..288db9d2bf904a978fe90e63840153fea73b9de2 --- /dev/null +++ b/eval/recursive/6ce28836-cfd6-4185-9a67-e7e1a92e0eea.cpp @@ -0,0 +1,208 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +const int NMAX = 1e5 + 5; + + + +char S[NMAX], *p; + + + +static inline int Eval (); + +static inline int Termen (); + +static inline int Factor (); + + + +static inline int Eval () + +{ + + int r = Termen(); + + + + while(*p == '+' || *p == '-') + + { + + if(*p == '+') + + { + + ++p; + + + + r += Termen(); + + } + + else + + { + + ++p; + + + + r -= Termen(); + + } + + } + + + + return r; + +} + + + +static inline int Termen () + +{ + + int r = Factor(); + + + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + { + + ++p; + + + + r *= Factor(); + + } + + else + + { + + ++p; + + + + r /= Factor(); + + } + + } + + + + return r; + +} + + + +static inline int Factor () + +{ + + if(*p == '(') + + { + + ++p; + + + + int r = Eval(); + + + + ++p; + + + + return r; + + } + + + + int r = 0, sign = 1; + + + + if(*p == '-') + + { + + sign = -1; + + + + ++p; + + } + + + + while(isdigit(*p)) + + { + + r = r * 10 + (*p - '0'); + + + + ++p; + + } + + + + return (r * sign); + +} + + + +int main() + +{ + + f.tie(NULL); + + + + f >> (S + 1); + + + + p = (S + 1); + + + + g << Eval() << '\n'; + + + + return 0; + +} diff --git a/eval/recursive/6ce5244b-37db-44d1-a91f-715d672fa0e0.cpp b/eval/recursive/6ce5244b-37db-44d1-a91f-715d672fa0e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ccec604ff454e407449a3cc416eee6491aca58b --- /dev/null +++ b/eval/recursive/6ce5244b-37db-44d1-a91f-715d672fa0e0.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100010]; + +int poz; + +int eval(); + + + +int termen() + +{ + + int aux = 0; + + if(s[poz] == '(') + + { + + poz++; + + aux = eval(); + + poz++; + + return aux; + + } + + while(s[poz] >= '0' && s[poz] <= '9') + + aux = aux * 10 + (s[poz++] - '0'); + + return aux; + +} + + + +int produs() + +{ + + int aux = termen(); + + + + while(s[poz] == '*' || s[poz] == '/') + + { + + if(s[poz++] == '*') + + aux = aux * termen(); + + else aux /= termen(); + + } + + + + return aux; + +} + + + +int eval() + +{ + + int aux = produs(); + + + + while(s[poz] == '+' || s[poz] == '-') + + { + + if(s[poz++] == '+') + + aux = aux + produs(); + + else aux -= produs(); + + } + + + + return aux; + +} + + + +int main() + +{ + + cin >> s; + + cout << eval(); + + return 0; + +} diff --git a/eval/recursive/6cfeb048-6641-4dde-8f16-5636b6be506e.cpp b/eval/recursive/6cfeb048-6641-4dde-8f16-5636b6be506e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ec60e942288d881ae5d1b916642b3f3a8a8cce4 --- /dev/null +++ b/eval/recursive/6cfeb048-6641-4dde-8f16-5636b6be506e.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +std::ifstream fin("evaluare.in"); +std::ofstream fout("evaluare.out"); + +char c[100500], *p=c; +char op[4][4]={"+-","*/","^",""}; +int LEVMAX=2; + + +int eval(int a,int b,char oper) +{ + if(oper=='+') + return a+b; + if(oper=='-') + return a-b; + if(oper=='/') + return a/b; + return a*b; +} + +int expr(int lev) +{ + int x,y; + if(lev==LEVMAX) + { + if(*p=='(') + { + ++p; + x= expr(0); + ++p; + } + else + { + x=0; + while(*p>='0' &&*p<='9') + { + x=x*10+(*p-'0'); + ++p; + } + } + } + else + for(x=expr(lev+1);strchr(op[lev],*p);x=y) + y=eval(x,expr(lev+1),*p++); + return x; +} +int main() +{ + fin.getline(c,100500); + int str= strlen(c); + c[str]='\n'; + c[str+1]='\0'; + + + + fout< +#include +using namespace std; + +const int N = 100000; + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+'||s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*'||s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + + + } return prod; + + + +} + + + +int factor() + +{ + + int val=0; + + int semn=1; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(isdigit(s[p])) + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + in.getline(s, N+1); + + out< + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100005], *p; + + + +int termen(); + +int factor(); + +int ecuatie(); + + + +int termen() + +{ + + int rez = factor(); + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + p++, rez *= factor(); + + else + + p++, rez /= factor(); + + } + + + + return rez; + +} + + + +int factor() + +{ + + int rez=0; + + + + if(*p == '(') + + p++, rez = ecuatie(),p++; + + else + + { + + while(isdigit(*p)) + + { + + rez = rez * 10 + *p - '0'; + + p++; + + } + + } + + + + return rez; + +} + + + +int ecuatie() + +{ + + int rez = termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p=='+') + + p++, rez += termen(); + + else + + p++, rez -= termen(); + + + + } + + return rez; + +} + +int main() + +{ + + f >> s; + + p = s; + + g << ecuatie(); + +} diff --git a/eval/recursive/7078430c-75f1-43fa-a76f-6f575e882b05.cpp b/eval/recursive/7078430c-75f1-43fa-a76f-6f575e882b05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3be7e9ee33f1af5a8ffb410bcf8edaf027eb3da --- /dev/null +++ b/eval/recursive/7078430c-75f1-43fa-a76f-6f575e882b05.cpp @@ -0,0 +1,160 @@ +#include + + + + +using namespace std; + + + +const int N = 100000; + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int semn = 1,val = 0; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + cin>>s; + + cout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int expresie(); + +int termen(); + +int factor(); + + + +int i; + +char s[100005]; + + + +int main() + +{ + + fin >> s; + + fout<< expresie(); + + + + return 0; + +} + + + +int expresie() + +{ + + int r=termen(); + + while (s[i]=='+' || s[i]=='-') + + { + + if (s[i]=='+') + + { + + i++; + + r+=termen(); + + } + + else + + { + + i++; + + r-=termen(); + + } + + } + + return r; + +} + + + +int termen() + +{ + + int r=factor(); + + while (s[i]=='*' || s[i]=='/') + + { + + if (s[i]=='*') + + { + + i++; + + r*=factor(); + + } + + else + + { + + i++; + + r/=factor(); + + } + + } + + return r; + +} + + + +int factor() + +{ + + int r=0; + + if (s[i]=='(') + + { + + i++; + + r=expresie(); + + i++; + + } + + else + + { + + while (s[i]>='0' && s[i]<='9') + + { + + r=r*10+s[i]-'0'; + + i++; + + } + + } + + return r; + +} diff --git a/eval/recursive/7228eaa9-7781-437e-8eb7-1ee8c2499f8f.cpp b/eval/recursive/7228eaa9-7781-437e-8eb7-1ee8c2499f8f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..941feddedc8b44f05538b1edc7c9bc9f0d9e1d29 --- /dev/null +++ b/eval/recursive/7228eaa9-7781-437e-8eb7-1ee8c2499f8f.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("evaluare.in"); +ofstream out("evaluare.out"); + +int n, globalCnt; + +char str[100002]; + +int termen(); +int factor(); +int evalueaza(); +int numar(); + +int main(int argc, const char * argv[]) { + in>>str; + + int sol = evalueaza(); + + out<= '0'){ + x = x * 10 + str[globalCnt] - '0'; + ++ globalCnt; + } + return x; +} diff --git a/eval/recursive/72950147-bc11-4817-9ea9-988df6e3fa44.cpp b/eval/recursive/72950147-bc11-4817-9ea9-988df6e3fa44.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed9cfb8b6670bb1ec4540fc488ab5d904c3bbe4c --- /dev/null +++ b/eval/recursive/72950147-bc11-4817-9ea9-988df6e3fa44.cpp @@ -0,0 +1,141 @@ +#include +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100005]; + +int p=1; + +string c; + + + + + +int expresie(); + +int termen(); + +int fact(); + +int expresie(){ + + int sum=termen(); + + while(s[p]=='+'||s[p]=='-'){ + + if(s[p]=='+'){ + + p++; + + sum+=termen(); + + } + + else{ + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen(){ + + int prod=fact(); + + while(s[p]=='*'||s[p]=='/'){ + + if(s[p]=='*'){ + + p++; + + prod*=fact(); + + } + + else{ + + p++; + + prod/=fact(); + + } + + } + + return prod; + +} + +int fact(){ + + int val=0,semn=1; + + while(s[p]=='-'){ + + p++; + + semn=-semn; + + } + + if(s[p]=='('){ + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9'){ + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + cin>>c; + + for(int i=0;i<=c.size();i++){ + + s[i+1]=c[i]; + + } + + cout< +#include +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + in >> s; + + in.close(); + + out << expresie(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/73b07593-3270-4830-8821-c10527134155.cpp b/eval/recursive/73b07593-3270-4830-8821-c10527134155.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c32fdf4f131412e059cddb081123a91bb5de9526 --- /dev/null +++ b/eval/recursive/73b07593-3270-4830-8821-c10527134155.cpp @@ -0,0 +1,47 @@ +#include + +char s[100001]; +int p = 0; + +int evaluate(); + +int multiplier() { + if (s[p] == '(') { + ++p; + int r = evaluate(); + ++p; + return r; + } + int n = 0; + while (s[p] >= '0' && s[p] <= '9') n = n * 10 + (s[p++] - '0'); + return n; +} + +int addend() { + int r = multiplier(); + while (s[p] == '*' || s[p] == '/') { + if (s[p++] == '*') { + r *= multiplier(); + } else { + r /= multiplier(); + } + } + return r; +} + +int evaluate() { + int r = addend(); + while (s[p] == '+' || s[p] == '-') { + if (s[p++] == '+') r += addend(); + else r -= addend(); + } + return r; +} + +int main() { + std::ifstream in("evaluare.in"); + std::ofstream out("evaluare.out"); + in >> s; + out << evaluate(); + return 0; +} diff --git a/eval/recursive/73b68492-0bc1-4a30-8438-6da325aed4e9.cpp b/eval/recursive/73b68492-0bc1-4a30-8438-6da325aed4e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eeeb1af613c356dec5d7bb7e5a6b004715954655 --- /dev/null +++ b/eval/recursive/73b68492-0bc1-4a30-8438-6da325aed4e9.cpp @@ -0,0 +1,134 @@ +#include +typedef long long ll; + +typedef unsigned long long ull; + +typedef long double ld; + + + +const int nmax=1e5+19; + +const int mod=6669666; + + + +using namespace std; + + + +string s; + +int i; + + + +int baga1(); + +int baga2(); + +int baga3(); + + + +int baga3() + + + +{ + + int x=0; + + if(s[i]=='(') + + { + + i++; + + x=baga1(); + + i++; + + } else + + { + + while(s[i]>='0' && s[i]<='9') x=x*10+(s[i++]-48); + + } + + + + return x; + +} + + + +int baga2() + +{ + + int x=baga3(); + + while(s[i]=='*' || s[i]=='/') + + { + + i++; + + if(s[i-1]=='*') x*=baga3(); + + if(s[i-1]=='/') x/=baga3(); + + } + + + + return x; + +} + + + +int baga1() + +{ + + int x=baga2(); + + while(s[i]=='+' || s[i]=='-') + + { + + i++; + + if(s[i-1]=='+') x+=baga2(); + + if(s[i-1]=='-') x-=baga2(); + + } + + + + return x; + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(0); cin.tie(); + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + cin>>s; + + cout< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/745c2a38-3797-43f0-b4c4-b87b0753a76e.cpp b/eval/recursive/745c2a38-3797-43f0-b4c4-b87b0753a76e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77cf42e9f0106477c915e18f86308faa37c70c97 --- /dev/null +++ b/eval/recursive/745c2a38-3797-43f0-b4c4-b87b0753a76e.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + + char e[100000]; + + + +void show(int start, int ending) + +{ + + for(int i=start; i<=ending; i++) + + cout<=left; i--) + + { + + if(e[i]==')') + + paranteza++; + + if(e[i]=='(') + + paranteza--; + + if((e[i]=='+' || e[i]=='-') && paranteza==0) + + { + + if(e[i]=='+') + + return calc(left, i-1)+calc(i+1, right); + + return calc(left, i-1)-calc(i+1, right); + + } + + } + + paranteza=0; + + for(int i=right; i>=left; i--) + + { + + if(e[i]==')') + + paranteza++; + + if(e[i]=='(') + + paranteza--; + + if((e[i]=='*' || e[i]=='/') && paranteza==0) + + { + + if(e[i]=='*') + + return calc(left, i-1)*calc(i+1, right); + + return calc(left, i-1)/calc(i+1, right); + + } + + } + + if(e[left]=='(' && e[right]==')') + + return calc(left+1, right-1); + + return stringtoint(left, right); + +} + + + +int main() + +{ + + fin>>e; + + int n=strlen(e); + + fout< + + +using namespace std; + + + +FILE *f = fopen("evaluare.in","r"); + +FILE *g = fopen("evaluare.out","w"); + + + +int fact(); + +int term(); + +int expr(); + + + +char c[100005]; + +int ind; + + + +int fact(){ + + if(c[ind] == '\0' || c[ind] == '\n'){ + + return 0; + + } + + if(c[ind] == '('){ + + ind++; + + int ans = expr(); + + ind++; + + return ans; + + } + + int tmp = 0; + + + + while('0' <= c[ind] && c[ind] <= '9'){ + + tmp = tmp * 10 + (c[ind] - '0'); + + ind++; + + } + + + + return tmp; + +} + + + +int term(){ + + int ans = fact(); + + + + while(c[ind] == '*' || c[ind] == '/'){ + + if(c[ind] == '*'){ + + ind++; + + ans = ans * fact(); + + } + + else{ + + ind++; + + ans = ans / fact(); + + } + + } + + + + return ans; + +} + + + +int expr(){ + + int ans = term(); + + + + while(c[ind] == '+' || c[ind] == '-'){ + + int sgn = (c[ind] == '+' ? 1:-1); + + ind++; + + ans = ans + sgn * term(); + + } + + + + return ans; + +} + + + +int main(){ + + + + fgets(c,100005,f); + + + + fprintf(g,"%d\n",expr()); + + + + fclose(f); + + fclose(g); + + + + return 0; + +} diff --git a/eval/recursive/7563bde1-c460-4994-a901-6cbd7a82d110.cpp b/eval/recursive/7563bde1-c460-4994-a901-6cbd7a82d110.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79de099f34aa8f6b056f62d7a44522de9ff63a08 --- /dev/null +++ b/eval/recursive/7563bde1-c460-4994-a901-6cbd7a82d110.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100001],*p; + +int r; + +int eval(); + +int termene(); + +int factor(); + +int eval() + +{ + + int r=termene(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') p++,r+=termene(); + + else p++,r-=termene(); + + } + + return r; + +} + +int termene() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') p++,r*=factor(); + + else p++,r/=factor(); + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='(') + + p++,r=eval(),p++; + + else while('0'<=*p&&'9'>=*p) + + r=r*10+*p-'0',p++; + + return r; + +}int main() + +{ + + f.getline(s,100001); + + p=s; + + r=eval(); + + g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char expresie[100002]; + +int p = 0; + + + +long long functia1(); + +long long functia2(); + +long long functia3(); + + + +int main() + +{ + + fin >> expresie; + + fout << functia1(); + + fin.close(); + + fout.close(); + + return 0; + +} + + + +long long functia1() + +{ + + long long r = functia2(); + + while (expresie[p] == '+' || expresie[p] == '-') + + { + + if (expresie[p++] == '+') + + { + + r += functia2(); + + } + + else + + { + + r -= functia2(); + + } + + } + + return r; + +} + + + +long long functia2() + +{ + + long long r = functia3(); + + while (expresie[p] == '*' || expresie[p] == '/') + + { + + if (expresie[p++] == '*') + + { + + r *= functia3(); + + } + + else + + { + + r /= functia3(); + + } + + } + + return r; + +} + + + +long long functia3() + +{ + + long long r = 0; + + if (expresie[p] == '(') + + { + + ++p; + + r = functia1(); + + ++p; + + } + + else + + { + + while (expresie[p] >= '0' && expresie[p] <= '9') + + { + + r = r * 10 + expresie[p] - '0'; + + ++p; + + } + + } + + return r; + +} diff --git a/eval/recursive/763be22d-bd2a-486d-a93e-df142ad177fa.cpp b/eval/recursive/763be22d-bd2a-486d-a93e-df142ad177fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7d431226891677230afda682ca300a45e2ec838 --- /dev/null +++ b/eval/recursive/763be22d-bd2a-486d-a93e-df142ad177fa.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream f("evaluare.in"); ofstream g("evaluare.out"); + +int MAX; + +char S[100010], *p=S; + +int termen(); + +int factor(); + +int eval() + +{ int r = termen(); + + while ( *p=='+' || *p=='-' ) + + switch ( *p ) + + {case '+' : ++p; + + r += termen(); break; + + case '-' : ++p; + + r -= termen(); break; + + } + + return r; + +} + +int termen() + +{ int r = factor(); + + while ( *p=='*' || *p=='/' ) + + switch ( *p ) + + {case '*' : ++p; r *= factor(); break; + + case '/' : ++p; r /= factor(); break; + + } + + return r; + +} + +int factor() + +{ int r=0; + + if (*p =='(') { + + ++p; + + r = eval(); + + ++p; + + } + + else { + + while ( *p>='0' && *p<='9' ) {r = r*10 + *p - '0'; ++p;} + + } + + return r; + +} + +int main() + +{f>>S; MAX=strlen(S); g< + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100005], *p; + + + +int termen(); + +int factor(); + + + +int eval() + +{ + + int rez=termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p=='+') + + p++,rez+=termen(); + + else + + p++,rez-=termen(); + + + + } + + return rez; + +} + + + +int termen() + +{ + + int rez=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + p++, rez*=factor(); + + else + + p++,rez/=factor(); + + } + + return rez; + +} + + + +int factor() + +{ + + int rez=0; + + + + if(*p=='(') + + p++,rez= eval(),p++; + + else + + { + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + } + + return rez; + + + +} + +int main() + +{ + + cin >> s; + + p=s; + + cout< +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +const int DIM = 1e5; + + +int l; +char s[DIM + 5]; +long long SumaDif(int &p); +long long ProdCat(int &p); +long long ParantezeVal(int &p); + + + +long long SumaDif(int &p) +{ + + long long res = ProdCat(p); + while(p <= l && (s[p] == '+' || s[p] == '-')) { + + if(s[p] == '+') + + p++, res += ProdCat(p); + + else + + p++, res -= ProdCat(p); + } + return res; + +} + + + +long long ProdCat(int &p) +{ + long long res = ParantezeVal(p); + while(p <= l && (s[p] == '*' || s[p] == '/')) { + if(s[p] == '*') + p++, res *= ParantezeVal(p); + else + p++, res /= ParantezeVal(p); + + } + return res; + +} +long long ParantezeVal(int &p) +{ + + if(s[p] == '(') + { + p++; + long long res = SumaDif(p); + p++; + return res; + + } + long long nr = 0; + while(isdigit(s[p])) { + nr = nr * 10LL + (s[p] - '0'); + p++; + } + return nr; +} +int main() +{ + + fin >> s; l = strlen(s) - 1; + int p = 0; + fout << SumaDif(p) << '\n'; + return 0; + +} diff --git a/eval/recursive/77fc82df-72d3-41e9-9df8-912bca96b132.cpp b/eval/recursive/77fc82df-72d3-41e9-9df8-912bca96b132.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e3605386134048fca269e083d06487a609c24a3 --- /dev/null +++ b/eval/recursive/77fc82df-72d3-41e9-9df8-912bca96b132.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +char s[100050],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval() + +{ + + int r=termen(); + + while(*p=='+'||*p=='-') + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0'&&*p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + cin.sync_with_stdio(0); + + cin.tie(0); + + cin.getline(s,100005); + + p=s; + + cout< +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100002]; + +int i; + + + +int termen(); + +int factor(); + + + +int calc() + +{ + + int x=termen(); + + while(s[i]=='+'||s[i]=='-') + + { + + if(s[i]=='+') + + { + + i++; + + x+=termen(); + + } + + else + + { + + i++; + + x-=termen(); + + } + + } + + i++; + + return x; + +} + + + +int termen() + +{ + + int x=factor(); + + while(s[i]=='*'||s[i]=='/') + + { + + if(s[i]=='*') + + { + + i++; + + x*=factor(); + + } + + else + + { + + i++; + + x/=factor(); + + } + + } + + return x; + +} + + + +int factor() + +{ + + if(s[i]=='(') + + { + + i++; + + return calc(); + + } + + else + + { + + int x=0; + + while(s[i]>='0'&&s[i]<='9') + + { + + x*=10; + + x+=s[i]-48; + + i++; + + } + + return x; + + } + +} + + + +int main() + +{ + + int sol; + + f.getline(s+1,100001); + + i=1; + + sol=calc(); + + g< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100000; + + + +char s[N + 1]; + +int expresie(); + +int factor(); + +int termen(); + +int p; + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p ++; + + sum += termen(); + + } + + else + + { + + p ++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p ++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int sign = 1,val = 0; + + while(s[p] == '-') + + { + + p ++; + + sign = -sign; + + } + + if(s[p] == '(') + + { + + p ++; + + val = expresie(); + + p ++; + + return sign * val; + + } + + while(s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p ++; + + } + + return sign * val; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + return 0; + +} diff --git a/eval/recursive/798e6eca-333a-41f4-baae-609b5961d614.cpp b/eval/recursive/798e6eca-333a-41f4-baae-609b5961d614.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66bc7dd3379aee7835b19cee1950246fa0b3566e --- /dev/null +++ b/eval/recursive/798e6eca-333a-41f4-baae-609b5961d614.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100000+10],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval() + +{ + + long long r; + + r=termen(); + + while(*p=='+' || *p=='-') + + { + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + } + + return r; + +} + +int termen() + +{ + + long long r; + + r=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + } + + return r; + +} + +int factor() + +{ + + long long r; + + r=0; + + if(*p=='(' || *p=='[') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0' && *p<='9') + + { + + r=r*10+(*p-'0'); + + p++; + + } + + return r; + +} + +int main() + +{ + + f.getline(s,100010); + + p=s; + + g< + + + + + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +int indexx = 0; + + + +char S[1 + 100000 + 5]; + + + +int math (); + + + +int fact () + +{ + + int integer = 0, sign = 1; + + if (S[indexx] == '-') + + ++indexx, sign = -1; + + if (S[indexx] == '(') + + { + + ++indexx; + + integer = math (); + + ++indexx; + + } + + else while (isdigit (S[indexx])) + + integer = integer * 10 + S[indexx] - '0', ++indexx; + + return sign * integer; + +} + + + +int term () + +{ + + int sum = fact (); + + while (S[indexx] == '*' || S[indexx] == '/') + + if (S[indexx++] == '*') + + sum *= fact (); + + else + + sum /= fact (); + + return sum; + +} + + + +int math () + +{ + + int sum = term (); + + while (S[indexx] == '+' || S[indexx] == '-') + + if (S[indexx++] == '+') + + sum += term (); + + else + + sum -= term (); + + return sum; + +} + + + +int main() + +{ + + fin >> S; + + fout << math (); + + return 0; + +} diff --git a/eval/recursive/79f55549-7eaa-480f-9bb2-3ea13b8f61dd.cpp b/eval/recursive/79f55549-7eaa-480f-9bb2-3ea13b8f61dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc8a1aa956d830c3814ba317b63d15f7d3166879 --- /dev/null +++ b/eval/recursive/79f55549-7eaa-480f-9bb2-3ea13b8f61dd.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100000],*p; + + + +long long eval(); + +long long term(); + +long long fact(); + + + +long long eval() + +{long long r; + +r=term(); + +while(*p=='+'||*p=='-') + + {if(*p=='+'){p++;r+=term();} + + else if(*p=='-'){p++;r-=term();} + + } + +return r; + +} + + + +long long term() + +{long long r=fact(); + +while(*p=='*'||*p=='/') + + {if(*p=='*'){p++;r*=fact();} + + else if(*p=='/'){p++;r=r/fact();} + + } + +return r; + +} + + + +long long fact() + +{long long r=0; + +if(*p=='('){p++;r=eval();p++;} + +else { + + while(*p>='0'&&*p<='9') + + {r=r*10+*p-'0'; + + p++; + + } + + } + +return r; + +} + + + +int main() + +{ + +f.getline(s,100000); + +p=s; + + g< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/7ac3a3b6-cd68-4422-9579-11e76346083c.cpp b/eval/recursive/7ac3a3b6-cd68-4422-9579-11e76346083c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..653a215a4feba208c514e4132b41644900a29017 --- /dev/null +++ b/eval/recursive/7ac3a3b6-cd68-4422-9579-11e76346083c.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],*p=s; + + + +long e1(); + +long e2(); + +long e() + +{ + + long r=e1(); + + while(*p=='+' or *p=='-') + + if(*p=='+') + + p++, r+=e1(); + + else + + p++, r-=e1(); + + return r; + +} + +long e1() + +{ + + long r=e2(); + + while(*p=='*' or *p=='/') + + if(*p=='*') + + p++, r*=e2(); + + else + + p++, r/=e2(); + + return r; + +} + +long e2() + +{ + + long r=0; + + if(*p=='(') + + p++, r=e(), p++; + + else + + while(*p>='0' and *p<='9') + + r=r*10+*p-'0', p++; + + return r; + +} + + + +int main() + +{ + + + + fin >> s; + + fout << e(); + + + + return 0; + + + +} diff --git a/eval/recursive/7aff7851-6119-4ef3-988d-dc7fb707f343.cpp b/eval/recursive/7aff7851-6119-4ef3-988d-dc7fb707f343.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b3f6e6dc038e6bca3c334b8de168246a047e986 --- /dev/null +++ b/eval/recursive/7aff7851-6119-4ef3-988d-dc7fb707f343.cpp @@ -0,0 +1,140 @@ +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char C[100005]; + +int idx; + + + +int expresie(); + + + +int factor() + +{ + + if (C[idx] == '(') + + return expresie(); + + int ret = 0; + + while ('0' <= C[idx] && C[idx] <= '9') + + ret = ret * 10 + C[idx++] - '0'; + + return ret; + +} + + + +int termen() + +{ + + int ret = factor(); + + while (C[idx] == '*' || C[idx] == '/') + + { + + if (C[idx] == '*') + + { + + idx++; + + ret *= factor(); + + } + + else + + { + + idx++; + + ret /= factor(); + + } + + } + + return ret; + +} + + + +int expresie() + +{ + + idx++; + + int ret = termen(); + + while (C[idx] != ')') + + { + + if (C[idx] == '+') + + { + + idx++; + + ret += termen(); + + } + + else + + { + + idx++; + + ret -= termen(); + + } + + } + + idx++; + + return ret; + +} + + + +int main() + +{ + + fin >> C + 1; + + C[0] = '('; + + C[strlen(C)] = ')'; + + fout << expresie() << "\n"; + + return 0; + +} diff --git a/eval/recursive/7b094e28-f221-41ba-b84e-60a07969caf5.cpp b/eval/recursive/7b094e28-f221-41ba-b84e-60a07969caf5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9346f567dc316563196b9bf3beabe3491df89aa --- /dev/null +++ b/eval/recursive/7b094e28-f221-41ba-b84e-60a07969caf5.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +const int N=100010; + +char s[N],*p=s; + + + +typedef long long int lli; + + + +lli factor(),termen(); + + + +lli eval() + +{ + + lli r=termen(); + + while(*p=='+' || *p=='-') + + { + + if(*p=='+') + + { + + p++; + + r+=termen(); + + }else{ + + p++; + + r-=termen(); + + } + + } + + return r; + +} + + + +lli termen() + +{ + + lli r=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + { + + p++; + + r*=factor(); + + }else{ + + p++; + + r/=factor(); + + } + + } + + return r; + +} + + + + + +lli factor() + +{ + + + + lli r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + }else{ + + while(*p>='0' && *p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + } + + return r; + +} + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + fgets(s,N,stdin); + + printf("%ld\n",eval()); + + return 0; + +} diff --git a/eval/recursive/7bbd6ca7-cab9-4308-932e-3023f95e0640.cpp b/eval/recursive/7bbd6ca7-cab9-4308-932e-3023f95e0640.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaad864178b0a1c1dc96a91c123ad7cae40f94cb --- /dev/null +++ b/eval/recursive/7bbd6ca7-cab9-4308-932e-3023f95e0640.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +char s[1000005],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval() { + + int r = termen(); + + while(*p=='+'||*p=='-') { + + if(*p=='+') { + + p++; + + r+=termen(); + + } + + else { + + p++; + + r-=termen(); + + } + + } + + return r; + +} + +int termen() { + + int r=factor(); + + while(*p=='*'||*p=='/') { + + if(*p=='*') { + + p++; + + r*=factor(); + + } + + else { + + p++; + + r/=factor(); + + } + + } + + return r; + +} + +int factor() { + + int r=0; + + if(*p=='(') { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0'&&*p<='9') { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main() + +{ + + cin.getline(s,100005); + + p=s; + + cout< +#include +#include +using namespace std; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); +const int N=100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+' || s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*'||s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + if(s[p]=='/') + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0'&& s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} + +int main() +{ + in>>s; + out< + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100005],*p; + + + +int factor(); + + + +int elem(); + + + +int termen() + +{ + + int rez=factor(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') + + { + + p++; + + rez=rez+factor(); + + } + + else + + { + + p++; + + rez=rez-factor(); + + } + + } + + return rez; + +} + +int factor() + +{ + + int rez=elem(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + rez=rez*elem(); + + } + + else + + { + + p++; + + rez=rez/elem(); + + } + + } + + return rez; + +} + +int elem() + +{ + + int rez=0; + + if(*p=='(') + + { + + p++; + + rez=termen(); + + p++; + + return rez; + + } + + + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + return rez; + +} + +int main() + +{ + + f>>s; + + p=s; + + g< +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +const int nmax=1e5+3; + +char s[nmax]; + +int fact(); + +int termen(),k; + + + +int eval() + +{ + + int r=termen(); + + while(s[k]=='+'||s[k]=='-') + + { + + ++k; + + if(s[k-1]=='-') r-=termen(); + + else r+=termen(); + + } + + return r; + +} + + + +int termen() + +{ + + int r=fact(); + + while(s[k]=='*'||s[k]=='/') + + { + + ++k; + + if(s[k-1]=='*') r*=fact(); + + else r/=fact(); + + } + + return r; + +} + + + +int fact() + +{ + + int r=0; + + if(s[k]=='(') + + { + + ++k; + + r=eval(); + + ++k; + + } + + + + while(s[k]>='0'&&s[k]<='9') + + { + + r=r*10+s[k]-'0'; + + ++k; + + } + + + + return r; + +} + + + +int main() + +{ + + f.getline(s+1,nmax-1); + + k=1; + + g< +#include +#include + + +using namespace std; + + + +const int N=100001; + + + +char s[N]; + + + +int p; + +int termen(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while('0'<=s[p] && s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + ifstream in("evaluare.in"); + + ofstream out("evaluare.out"); + + in>>s; + + out< +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +char s[100005]; + +int cautare(int st, int dr, char s1, char s2) { + int nivel = 0; + for (int i = dr; i >= st; --i) { + if (s[i] == ')') + nivel++; + else if (s[i] == '(') + nivel--; + else if (nivel == 0 && (s[i] == s1 || s[i] == s2)) + return i; + } + return -1; +} + +int evaluare(int st, int dr) { + int poz = cautare(st, dr, '+', '-'); + if (poz != -1) { + if (s[poz] == '+') + return evaluare(st, poz - 1) + evaluare(poz + 1, dr); + return evaluare(st, poz - 1) - evaluare(poz + 1, dr); + } + poz = cautare(st, dr, '/', '*'); + if (poz != -1) { + if (s[poz] == '*') + return evaluare(st, poz - 1) * evaluare(poz + 1, dr); + return evaluare(st, poz - 1) / evaluare(poz + 1, dr); + } + if (s[st] == '(') + return evaluare(st + 1, dr - 1); + int nr = 0; + for (int i = st; i <= dr; ++i) + nr = nr * 10 + (s[i] - '0'); + return nr; +} + +int main() { + f.getline(s, 100005); + g << evaluare(0, strlen(s) - 1); + return 0; +} diff --git a/eval/recursive/7fb995a5-d4e3-48bb-ba1c-3d42b5adadb4.cpp b/eval/recursive/7fb995a5-d4e3-48bb-ba1c-3d42b5adadb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..893351fcff036e9850bc21d7cf3ccb02775a73a4 --- /dev/null +++ b/eval/recursive/7fb995a5-d4e3-48bb-ba1c-3d42b5adadb4.cpp @@ -0,0 +1,72 @@ +#include +char s[100001],*p=s,r[100001],o; + +inline void S(int x) + +{ + + if(x<0) + + x=-x,r[o++]='-'; + + int i,d=x>999999999?10:x>99999999?9:x>9999999?8:x>999999?7:x>99999?6:x>9999?5:x>999?4:x>99?3:x>9?2:1; + + for(i=d-1;i>=0;x/=10,i--) + + r[o+i]=x%10+48; + + o+=d; + +} + +inline int E(int l) + +{ + + int x,y; + + if(l==2) + + if(*p=='(') + + p++,x=E(0),p++; + + else + + for(x=0;*p>='0'&&*p<='9';p++) + + x=x*10+*p-'0'; + + else + + for(x=E(l+1);(!l&&(*p=='+'||*p=='-'))||(l&&(*p=='*'||*p=='/'));x=y) + + if(!l) + + if(*p=='+') + + p++,y=x+E(l+1); + + else + + p++,y=x-E(l+1); + + else if(*p=='*') + + p++,y=x*E(l+1); + + else + + p++,y=x/E(l+1); + + return x; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin),freopen("evaluare.out","w",stdout),fread(s,1,100001,stdin),S(E(0)),fwrite(r,1,o,stdout); + +} diff --git a/eval/recursive/7ffd60c5-a2a1-4fba-8826-d09f6fd6b038.cpp b/eval/recursive/7ffd60c5-a2a1-4fba-8826-d09f6fd6b038.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7d556120abc0e9f9108241506c289daeffd7fa3 --- /dev/null +++ b/eval/recursive/7ffd60c5-a2a1-4fba-8826-d09f6fd6b038.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100005],*p; + +int eval(); + +int termen(); + +int factor(); + + + + + +int main() + +{ + + f.getline(s,100005); + + p=s; + + int rez=eval(); + + g<='0'&&*p<='9') + + { + + r=r*10+(*p-'0'); + + p++; + + } + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + } + + return r; + +} + +int eval() + +{ + + int r=termen(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + } + + return r; + +} diff --git a/eval/recursive/803d1c45-eb8f-4b83-b63e-fbb126dfec2b.cpp b/eval/recursive/803d1c45-eb8f-4b83-b63e-fbb126dfec2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11295e072a5c26bbb0504a68443a47cd494fa27f --- /dev/null +++ b/eval/recursive/803d1c45-eb8f-4b83-b63e-fbb126dfec2b.cpp @@ -0,0 +1,233 @@ + #include + #include +using namespace std; + ifstream in("evaluare" ".in"); + + ofstream out("evaluare" ".out"); +int col; + +char expr[100000]; + + + + + + + +inline bool numeric_chr(char chr) {return '0' <= chr && chr <= '9';} + +inline bool paran_chr(char chr) {return chr == '(';} + +inline bool operator_de_grad_1_chr(char chr) {return chr == '*' || chr == '/';} + +inline bool operator_de_grad_0_chr(char chr) {return chr == '+' || chr == '-';} + + + + + + + +int numar() { + + + + int rt = 0; + + + + while (numeric_chr(expr[col])) { + + + + rt *= 10; + + rt += expr[col] - '0'; + + + + col++; + + + + } + + + + return rt; + + + +} + + + + + + + +int expresie(); + + + +int paran_sau_nr() { + + + + if (paran_chr(expr[col])) { + + + + col++; + + + + int rt = expresie(); + + + + col++; + + + + return rt; + + + + } else { + + + + return numar(); + + + + } + + + +} + + + + + + + +int grad_1() { + + + + int rt = paran_sau_nr(); + + + + while (operator_de_grad_1_chr(expr[col])) { + + + + col++; + + + + if (expr[col - 1] == '*') { + + + + rt *= paran_sau_nr(); + + + + } else { + + + + rt /= paran_sau_nr(); + + + + } + + + + } + + + + return rt; + + + +} + + + + + + + +int expresie() { + + + + int rt = grad_1(); + + + + while (operator_de_grad_0_chr(expr[col])) { + + + + col++; + + + + if (expr[col - 1] == '+') { + + + + rt += grad_1(); + + + + } else { + + + + rt -= grad_1(); + + + + } + + + + } + + + + return rt; + + + +} + + + + + + + +int main() { + + + + in >> expr; + + + + out << expresie(); + + + +} diff --git a/eval/recursive/807c794d-ec44-4526-8f10-de17684d6c77.cpp b/eval/recursive/807c794d-ec44-4526-8f10-de17684d6c77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f935f3b51d0515dd6dc3c6dbd94632a142eba62 --- /dev/null +++ b/eval/recursive/807c794d-ec44-4526-8f10-de17684d6c77.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int termen(int&); + +int expresie(int&); + +int factor(int&); + +int numar(int&); + + + +int expresie(int &p) + +{ + + int r=termen(p); + + while(s[p]=='+' or s[p]=='-') + + { + + p++; + + if(s[p-1]=='+') r+=termen(p); + + else r-=termen(p); + + } + + return r; + +} + + + +int termen(int &p) + +{ + + int r=factor(p); + + while(s[p]=='/' or s[p]=='*') + + { + + p++; + + if(s[p-1]=='*') r*=factor(p); + + else r/=factor(p); + + } + + return r; + +} + + + +int factor(int &p) + +{ + + int r=0; + + if(s[p]=='(') + + { + + p++; + + r=expresie(p); + + if(s[p]==')') p++; + + } + + else r=numar(p); + + return r; + +} + + + +int numar(int &p) + +{ + + int nr=0; + + while(s[p]>='0' and s[p]<='9') + + { + + nr*=10; + + nr+=(s[p]-'0'); + + p++; + + } + + return nr; + +} + + + +int main() + +{ + + fin >> s; + + int p=0; + + fout << expresie(p); + + return 0; + +} diff --git a/eval/recursive/80f657cc-c8ac-4720-861d-a5db36a88f76.cpp b/eval/recursive/80f657cc-c8ac-4720-861d-a5db36a88f76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c3b4c73911ed210a6f126fa6229bac92fbddd16 --- /dev/null +++ b/eval/recursive/80f657cc-c8ac-4720-861d-a5db36a88f76.cpp @@ -0,0 +1,171 @@ +#include +#include + + +std::ifstream in("evaluare.in"); + +std::ofstream out("evaluare.out"); + + + +const int N=100000; + +static char s[N+1]; + +int p=0; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else if(s[p]=='-') + + { + + p++; + + sum-=termen(); + + } + + } + + + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else if(s[p]=='/') + + { + + p++; + + prod/=factor(); + + } + + } + + + + return prod; + + + +} + + + +int factor() + +{ + + int semn=1; + + int val=0; + + + + if(s[p]=='-') + + { + + semn*=-1; + + p++; + + } + + + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + + + while(isdigit(s[p])) + + { + + val=val*10+(s[p++]-'0'); + + } + + return semn*val; + +} + + + +int main() + +{ + + in.getline(s, N+1); + + out< +using namespace std; + + +string s; +int act; + +int Expresie(); + +int Factor() +{ + return Expresie(); +} + +int Termen() +{ + int ans = Factor(); + while (s[act] == '*' || s[act] == '/') { + char operatie = s[act]; + act++; + int val = Factor(); + if (operatie == '*') + ans *= val; + else if (operatie == '/') + ans /= val; + else + assert(false); + } + return ans; +} + +int Expresie() +{ + if (s[act] >= '0' && s[act] <= '9') { + int n = 0; + while (s[act] >= '0' && s[act] <= '9') { + n = 10 * n + s[act] - '0'; + act++; + } + return n; + } + assert(s[act] == '('); + act++; + + int ans = Termen(); + while (s[act] != ')') { + char operatie = s[act]; + act++; + int val = Termen(); + if (operatie == '+') + ans += val; + else if (operatie == '-') + ans -= val; + else + assert(false); + } + assert(s[act] == ')'); + act++; + return ans; +} + +int main() +{ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + cin >> s; + s = "(" + s + ")"; + int ans = Expresie(); + cout << ans << '\n'; + return 0; +} diff --git a/eval/recursive/81a8a614-498f-45c4-a716-5420531e2bee.cpp b/eval/recursive/81a8a614-498f-45c4-a716-5420531e2bee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3dfc32658f02fe77b0f6695d51d611a22cffe0e --- /dev/null +++ b/eval/recursive/81a8a614-498f-45c4-a716-5420531e2bee.cpp @@ -0,0 +1,83 @@ +#include +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N = 100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum = termen(); + while(s[p] == '+' || s[p] == '-') + { + if(s[p] == '+') + { + p++; + sum += termen(); + } + else + { + p++; + sum -= termen(); + } + } + return sum; +} +int termen() +{ + int prod = factor(); + while(s[p] == '*' || s[p] == '/') + { + if(s[p] == '*') + { + p++; + prod *= factor(); + } + else + { + p++; + prod /= factor(); + } + } + return prod; +} +int factor() +{ + int semn = 1, val = 0; + while(s[p] == '-') + { + p++; + semn = -semn; + } + if(s[p] == '(') + { + p++; + val = expresie(); + p++; + return semn*val; + } + while(s[p] >= '0' && s[p] <= '9') + { + val = val * 10 + (s[p] - '0'); + p++; + } + return semn*val; +} + + +int main() +{ + in>>s; + out< +#include +#include +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +int ind; + +char s[100005]; + +int f3(); + +int f2(); + +int f1() + +{ + + int result = f2(); + + while(s[ind] == '+' or s[ind] == '-') + + { + + if(s[ind] == '+') + + { + + ++ind; + + result+=f2(); + + } + + else + + { + + ++ind; + + result-=f2(); + + } + + } + + return result; + +} + +int f2() + +{ + + int raspuns = f3(); + + while(s[ind] == '*' or s[ind] == '/') + + { + + if(s[ind] == '*') + + { + + ++ind; + + raspuns*=f3(); + + } + + else { + + ++ind; + + raspuns/=f3(); + + } + + } + + return raspuns; + +} + +int f3() + +{ + + int raspuns=0; + + if(s[ind] == '(') + + { + + ++ind; + + raspuns = f1(); + + ++ind; + + } + + else + + { + + while(isdigit(s[ind])) + + { + + raspuns = raspuns * 10 + (s[ind]-'0'); + + ++ind; + + } + + } + + return raspuns; + +} + +int main() + +{ + + f.getline(s,100005); + + g< +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int SumDif(int &pos); + +int ProdCat(int &pos); + +int BracketVal(int &pos); + + + +string S; + + + +int SumDif(int &pos) + +{ + + int ans = ProdCat(pos); + + + + while(pos < (int)S.size() && (S[pos] == '+' || S[pos] == '-')) + + { + + pos++; + + + + if(S[pos - 1] == '+') + + ans += ProdCat(pos); + + else + + ans -= ProdCat(pos); + + } + + + + return ans; + +} + + + +int ProdCat(int &pos) + +{ + + int ans = BracketVal(pos); + + + + while(pos < (int)S.size() && (S[pos] == '*' || S[pos] == '/')) + + { + + pos++; + + + + if(S[pos - 1] == '*') + + ans *= BracketVal(pos); + + else + + ans /= BracketVal(pos); + + } + + + + return ans; + +} + + + +int BracketVal(int &pos) + +{ + + if(S[pos] == '(') + + { + + pos++; + + int res = SumDif(pos); + + pos++; + + + + return res; + + } + + + + int nr = 0; + + + + while(isdigit(S[pos]) && pos < (int)S.size()) + + { + + nr = nr * 10 + (S[pos] - '0'); + + pos++; + + } + + + + return nr; + +} + + + +int main() + +{ + + fin >> S; + + + + int pos = 0; + + fout << SumDif(pos) << '\n'; + + + + return 0; + +} diff --git a/eval/recursive/83c38ae1-3f98-40a9-ba73-88df00a1aaaf.cpp b/eval/recursive/83c38ae1-3f98-40a9-ba73-88df00a1aaaf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c9696a037d50729911cd77394b63a73324b5902 --- /dev/null +++ b/eval/recursive/83c38ae1-3f98-40a9-ba73-88df00a1aaaf.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +const int N = 100010; + +char E[N],*p; + +int suma(),produs(),factor(),paranteza(),numar(); + +int main() + +{ + + f>>E; + + p=E; + + g< +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int step = 0; + +string expresie; + + + +long eval(); + + + +long factor(){ + + int r = 0; + + + + if(expresie[step]=='('){ + + step++; + + r = eval(); + + step++; + + } + + else{ + + while(expresie[step]<='9' && expresie[step]>='0'){ + + r = r*10 + expresie[step] - '0'; + + step++; + + } + + } + + + + return r; + +} + + + +long termen(){ + + int r = factor(); + + + + while(expresie[step]=='*' || expresie[step]=='/'){ + + switch(expresie[step]){ + + case '*': + + step++; + + r *= factor(); + + break; + + case '/': + + step++; + + r /= factor(); + + + + } + + } + + + + return r; + +} + + + +long eval(){ + + int r = termen(); + + + + while(expresie[step]=='+' || expresie[step]=='-'){ + + switch(expresie[step]){ + + case '+': + + step++; + + r+=termen(); + + break; + + + + case '-': + + step++; + + r-=termen(); + + break; + + } + + } + + + + return r; + + + +} + +int main() + +{ + + + + getline(fin, expresie, '\n'); + + + + fout< +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N=100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+' || s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*' || s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0, semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0' && s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} + +int main() +{ + in.get(s, N); + out< +#include + + +using namespace std; + +const int NMAX = 100005; + +char s[NMAX]; + +char *p = s; + +int termen(); + +int factor(); + +int eval() { + + int r = termen(); + + while(*p == '+' || *p == '-') { + + switch(*p) { + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -= termen(); + + break; + + } + + } + + return r; + +} + +int termen() { + + int r = factor(); + + while(*p == '*' || *p == '/') { + + switch(*p) { + + case '*': + + ++p; + + r *= factor(); + + break; + + case '/': + + ++p; + + r /= factor(); + + break; + + } + + } + + return r; + +} + +int factor() { + + int r = 0; + + if(*p == '(') { + + ++p; + + r = eval(); + + ++p; + + } else { + + while('0' <= *p && *p <= '9') { + + r = r * 10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + + + +int main() + +{ + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + int k = -1; + + char ch; + + while(scanf("%c", &ch) != -1 && ch != '\n') s[++k] = ch; + + printf("%d\n", eval()); + + return 0; + +} diff --git a/eval/recursive/86d24b09-9147-44d3-87b1-dca8366a37e4.cpp b/eval/recursive/86d24b09-9147-44d3-87b1-dca8366a37e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d49268ab44a0abdc6335a6acd6b97e361c3ba644 --- /dev/null +++ b/eval/recursive/86d24b09-9147-44d3-87b1-dca8366a37e4.cpp @@ -0,0 +1,166 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + in >> s; + + in.close(); + + out << expresie(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/885af141-74b1-486c-8a2f-7a39ab8fcc3c.cpp b/eval/recursive/885af141-74b1-486c-8a2f-7a39ab8fcc3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73f3b12333b9788abddbf78830201000e17cc7e5 --- /dev/null +++ b/eval/recursive/885af141-74b1-486c-8a2f-7a39ab8fcc3c.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include + + +using namespace std; +ifstream fi("evaluare.in"); +ofstream fo("evaluare.out"); + + +char exp[100005], *p=exp; +map OP; + +void initOP(); +bool op(char c); +int ordin(char c); +int efectueaza(int a, int b, char op); +int eval(); +int termen(); +int factor(); + + + +int main() +{ + fi>>exp; + initOP(); + fo<='0' && *p<='9' ) + { + res = res*10 + (*p - '0'); + p++; + } + } + return res; +} diff --git a/eval/recursive/890075b6-6d41-4fea-a59d-413fa82f2a1c.cpp b/eval/recursive/890075b6-6d41-4fea-a59d-413fa82f2a1c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1564b0cc1cacb75a8e722a2c393c6280c1dd5e2 --- /dev/null +++ b/eval/recursive/890075b6-6d41-4fea-a59d-413fa82f2a1c.cpp @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +const int LMAX = 2; +char op[4][4]={"+-","*/","^",""}; + +const int NMAX = 100005; + +char s[NMAX]; +char *p=s; + +int eval(int a,int b, char sign){ + if(sign=='+') + return a+b; + if(sign=='-') + return a-b; + if(sign=='*') + return a*b; + if(sign=='/') + return a/b; +} + + +int expr(int lvl){ + int x; + if(lvl==LMAX) + if(*p=='('){ + ++p; + x=expr(0); + ++p; + return x; + }else{ + for(x=0;*p>='0' && *p<='9';++p) + x=x*10+*p-'0'; + return x; + } + else{ + int y=0; + for(x=expr(lvl+1);strchr(op[lvl],*p);x=y) + y=eval(x,expr(lvl+1),*p++); + + return x; + } +} + +int main(){ + freopen("evaluare.in","r",stdin); + freopen("evaluare.out","w",stdout); + scanf("%s",s); + + int n=strlen(s); + s[n]=1; + + printf("%d\n",expr(0)); +} diff --git a/eval/recursive/8942e49b-70fc-4f39-ad35-a7dad52e8359.cpp b/eval/recursive/8942e49b-70fc-4f39-ad35-a7dad52e8359.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f0f7ed9b02b45cdfd4d0d9363b0d55bb02f9f55 --- /dev/null +++ b/eval/recursive/8942e49b-70fc-4f39-ad35-a7dad52e8359.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int i; + +char s[100100]; + +int eval(); + +int termen(); + +int factor(); + + + +int eval() + +{ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-') + + { if(s[i] == '+') + + { i++; + + r = r + termen(); + + } + + else + + { i++; + + r = r - termen(); + + } + + } + + return r; + +} + + + +int termen() + +{ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/') + + { if(s[i] == '*') + + { i++; + + r = r * factor(); + + } + + else + + { i++; + + r = r / factor(); + + } + + } + + return r; + +} + + + +int factor() + +{ + + int r = 0; + + if(s[i] == '(') + + { i++; + + r = eval(); + + i++; + + } + + else + + { while(s[i] >= '0' && s[i] <= '9') + + { r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} + + + +int main() + +{ + + f >> s; + + g << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/89a37a3b-713e-4b43-ac98-2e54786a1133.cpp b/eval/recursive/89a37a3b-713e-4b43-ac98-2e54786a1133.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a13b60d36fccd8f0d6e8bc51cb83b120951807f9 --- /dev/null +++ b/eval/recursive/89a37a3b-713e-4b43-ac98-2e54786a1133.cpp @@ -0,0 +1,176 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + +char c; + +int poz; + + + +int Sum(); + + + +int termen(); + + + +int Prod(); + + + +int termen() + +{ + + int sum = 0; + + if(c == '(') + + { + + poz ++; + + c = s[poz]; + + sum = Sum(); + + poz ++; + + c = s[poz]; + + } + + else + + while(c <= '9' && c >= '0') + + { + + sum = sum * 10 + c - '0'; + + poz ++; + + c = s[poz]; + + } + + return sum; + +} + + + +int Prod() + +{ + + int rez = termen(); + + while(c == '*' || c == '/') + + { + + if(c == '*') + + { + + poz++; + + c=s[poz]; + + rez*=termen(); + + } + + else + + { + + poz++; + + c=s[poz]; + + rez/=termen(); + + } + + } + + return rez; + + + +} + + + +int Sum() + +{ + + int rez = Prod(); + + while(c == '+' || c == '-') + + { + + if(c == '+') + + { + + poz++; + + c=s[poz]; + + rez += Prod(); + + } + + else + + { + + poz++; + + c=s[poz]; + + rez -= Prod(); + + } + + } + + return rez; + +} + + + +int main() + +{ + + fin >> s; + + c = s[0]; + + + + fout << Sum(); + + + + return 0; + +} diff --git a/eval/recursive/8a519f22-77a3-46b2-b580-8525172d5860.cpp b/eval/recursive/8a519f22-77a3-46b2-b580-8525172d5860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea587d999ffe4b1462bb72c16e5198f9d3c1ffd0 --- /dev/null +++ b/eval/recursive/8a519f22-77a3-46b2-b580-8525172d5860.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +char s[100005], *p; + + + +int termen(); + +int factor(); + + + +int eval() + +{ + + int rez=termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p=='+') + + p++,rez+=termen(); + + else + + p++,rez-=termen(); + + + + } + + return rez; + +} + + + +int termen() + +{ + + int rez=factor(); + + while(*p=='*' || *p=='/') + + { + + if(*p=='*') + + p++, rez*=factor(); + + else + + p++,rez/=factor(); + + } + + return rez; + +} + + + +int factor() + +{ + + int rez=0; + + + + if(*p=='(') + + p++,rez= eval(),p++; + + else + + { + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + } + + return rez; + + + +} + +int main() + +{ + + cin >> s; + + p=s; + + cout< +const long MAX = 100010; + + + +char s[MAX]; + +int i = 0; + + + + + + + +long termen(); + + + +long factor(); +long eval() + +{ + + + + long rezultat = factor(); + + + + while ( s[i] =='+' || s[i]=='-' ) + + { + + + + switch ( s[i] ) + + { + + + + case '+': + + + + ++i; + + + + rezultat += factor(); + + + + break; + + + + case '-': + + + + ++i; + + + + rezultat -= factor(); + + + + break; + + + + } + + + + } + + + + return rezultat; + + + +} +long termen() + +{ + + long nr=0; + + while(s[i]>='0' && s[i]<='9') + + nr=nr*10+s[i++]-'0'; + + if(s[i]=='(') + + { + + i++; + + nr=eval(); + + i++; + + } + + return nr; + +} +long factor() + +{ + + long rezultat=termen(); + + while ( s[i] =='*' || s[i]=='/' ) + + { + + + + switch ( s[i] ) + + { + + + + case '*': + + + + ++i; + + + + rezultat *= termen(); + + + + break; + + + + case '/': + + + + ++i; + + + + rezultat /= termen(); + + + + break; + + + + } + + + + } + + return rezultat; + +} + + + + + + + +int main() { + + + + fgets(s, MAX, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + + + return 0; + + + +} diff --git a/eval/recursive/8ac51e35-7426-4c68-8b56-cc9bbb59e069.cpp b/eval/recursive/8ac51e35-7426-4c68-8b56-cc9bbb59e069.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3af20f5c1bae0173ab3edd7555c4a6cf425cb9e --- /dev/null +++ b/eval/recursive/8ac51e35-7426-4c68-8b56-cc9bbb59e069.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + + + +const long MAX = 100005; + +char S[MAX], *p = S; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +long termen(); + +long factor(); + + + +long eval() { + + long r = termen(); + + while ( *p == '+' || *p == '-' ) { + + switch ( *p ) { + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -= termen(); + + break; + + } + + } + + return r; + +} + + + +long termen() { + + long r = factor(); + + while ( *p == '*' || *p == '/' ) { + + switch ( *p ) { + + case '*' : + + ++p; + + r *= factor(); + + break; + + case '/': + + ++p; + + r /= factor(); + + break; + + } + + } + + return r; + +} + + + +long factor() { + + long r = 0; + + if ( *p == '(' ) { + + ++p; + + r = eval(); + + ++p; + + } else { + + while ( *p>='0' && *p<='9' ) { + + r = r*10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + + + +int main() { + + f.getline(S, MAX); + + g << eval(); + + f.close(); + + g.close(); + + return 0; + +} diff --git a/eval/recursive/8b77d509-c9d5-484b-9ce0-71c49f00c122.cpp b/eval/recursive/8b77d509-c9d5-484b-9ce0-71c49f00c122.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdf6bb4dbab255052d01b404a8890b30765bc46a --- /dev/null +++ b/eval/recursive/8b77d509-c9d5-484b-9ce0-71c49f00c122.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char *p,s[100005]; + +int nr() + +{ + + int rez=0; + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + return rez; + +} + +int eval(); + +int termen(); + +int factor(); + +int main() + +{ + + f.getline(s,100000); + + p=s; + + g< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + +int x; + + + +bool op1(int ind) + +{ + + if(s[ind]=='+' || s[ind]=='-') + + return 1; + + else + + return 0; + +} + + + +bool op2(int ind) + +{ + + if(s[ind]=='*' || s[ind]=='/') + + return 1; + + else + + return 0; + +} + + + +int factor(int &ind) + +{ + + int eval(int &a); + + int nr=0; + + if(s[ind]=='(') + + { + + int nr=eval(++ind); + + ind++; + + return nr; + + } + + else + + while(ind<=s.size() && isdigit(s[ind])) + + { + + nr=nr*10+s[ind]-'0'; + + ind++; + + } + + return nr; + +} + + + +int termen(int &ind) + +{ + + int p=factor(ind); + + while(ind<=s.size() && op2(ind)) + + { + + if(s[ind]=='*') + + p*=factor(++ind); + + else + + p/=factor(++ind); + + } + + return p; + +} + + + +int eval(int &ind) + +{ + + int a=termen(ind); + + while(ind<=s.size() && op1(ind)) + + { + + if(s[ind]=='+') + + a+=termen(++ind); + + else + + a-=termen(++ind); + + } + + return a; + +} + + + +int main() + +{ + + fin>>s; + + fout< +#include + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char s[100005]; + +int cautare (int st, int dr, char s1, char s2) + +{ + + int nivel=0; + + for(int i=dr; i>=st; i--) + + { + + if(s[i]=='(') nivel++; + + else if(s[i]==')') nivel--; + + else if(nivel==0 && (s[i]==s1 || s[i]==s2)) + + return i; + + } + + return -1; + +} + +int evaluare (int st, int dr) + +{ + + int poz=cautare(st, dr, '+', '-'); + + if(poz!=-1) + + { + + if(s[poz]=='+') + + return evaluare(st, poz-1)+evaluare(poz+1, dr); + + else + + return evaluare(st, poz-1)-evaluare(poz+1, dr); + + } + + poz=cautare(st, dr, '*', '/'); + + if(poz!=-1) + + { + + if(s[poz]=='*') + + return evaluare(st, poz-1)*evaluare(poz+1, dr); + + else + + return evaluare(st, poz-1)/evaluare(poz+1, dr); + + } + + if(s[st]=='(') + + return evaluare(st+1, dr-1); + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+(s[i]-'0'); + + return nr; + +} + +int main() + +{ + + fin.getline(s, 100005); + + fout< +#include +#include + +const int LMAX = 100005; + +char input[LMAX]; +char *p = input; + +int eval_add(); +int eval_mul(); +int eval_par(); + +int eval_add() { + int result = eval_mul(); + + while (strchr("+-", *p)) + if (*p == '+') + p++, result += eval_mul(); + else + p++, result -= eval_mul(); + + return result; +} + +int eval_mul() { + int result = eval_par(); + + while (strchr("*/", *p)) + if (*p == '*') + p++, result *= eval_par(); + else + p++, result /= eval_par(); + + return result; +} + +int eval_par() { + int result = 0; + + if (*p == '(') + p++, result = eval_add(), p++; + else + while (isdigit(*p)) + result = result * 10 + (*p - '0'), p++; + + return result; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + fgets(input, LMAX, stdin); + printf("%d\n", eval_add()); + + return 0; +} diff --git a/eval/recursive/8d624646-5502-4802-b03a-74af7c6e9c9a.cpp b/eval/recursive/8d624646-5502-4802-b03a-74af7c6e9c9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a80fc34634ac21a517509e812f7646617052d97 --- /dev/null +++ b/eval/recursive/8d624646-5502-4802-b03a-74af7c6e9c9a.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + + + +using namespace std; + + + +FILE *fin=freopen("evaluare.in","r",stdin); + +FILE *fout=freopen("evaluare.out","w",stdout); + + + +char s[100005]; + +int i=0; + + + +int num(); + +int mult(); + + + +int solve() + +{ + + long t=mult(); + + while(s[i]=='+' || s[i]=='-') + + { + + if(s[i]=='+') + + { + + i++; + + t+=mult(); + + } + + else + + { + + i++; + + t-=mult(); + + } + + } + + return t; + +} + + + +int mult() + +{ + + long t=num(); + + while(s[i]=='*' || s[i]=='/') + + { + + if(s[i]=='*') + + { + + i++; + + t*=num(); + + } + + else + + { + + i++; + + t/=num(); + + } + + } + + return t; + +} + + + +int num() + +{ + + int t=0; + + if(s[i]=='(') + + { + + i++; + + t=solve(); + + i++; + + } + + else + + while(isdigit(s[i])) + + { + + t=t*10+s[i++]-'0'; + + } + + return t; + +} + + + +void citire() + +{ + + fgets(s,100005,stdin); + +} + + + +int main() + +{ + + citire(); + + printf("%d\n",solve()); + + return 0; + +} diff --git a/eval/recursive/8dd2cc2b-daa2-4456-8031-3a13500f754c.cpp b/eval/recursive/8dd2cc2b-daa2-4456-8031-3a13500f754c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56d981d60b45bf6249c7d9488acac32ad08596a1 --- /dev/null +++ b/eval/recursive/8dd2cc2b-daa2-4456-8031-3a13500f754c.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005], *p; + + + + + +int factor(); + +int elem(); + + + +int termen(){ + + int rez=factor(); + + while(*p=='+' || *p=='-'){ + + if(*p=='+'){ + + p++; + + rez=rez+factor(); + + } + + else{ + + p++; + + rez=rez-factor(); + + } + + } + + return rez; + +} + +int factor(){ + + + + int rez=elem(); + + while(*p=='*' || *p=='/'){ + + if(*p=='*'){ + + p++; + + rez=rez*elem(); + + } + + else{ + + p++; + + rez=rez/elem(); + + } + + } + + return rez; + +} + +int elem(){ + + int rez=0; + + if(*p=='('){ + + p++; + + rez=termen(); + + p++; + + return rez; + + } + + while(isdigit(*p)){ + + rez=rez*10+*p-'0'; + + p++; + + } + + return rez; + +} + +int main() + +{ + + f>>s; + + p=s; + + g< + + + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],*p; + +int eval(); + +int termen(); + +int factor(); + +int numar() + +{ int nr=0; + + while(*p>='0' && *p<='9') + + { nr=nr*10+*p-48; + + p++; + + } + + return nr; + +} + +int main() + +{ f>>s; + + p=s; + + g< +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100005]; + +int pozitie(int ss, int dd, char c1, char c2) + +{ + + int ct=0; + + for(int i=dd; i>=ss; i--) + + { + + if(s[i]=='(') + + ct++; + + if(s[i]==')') + + ct--; + + if(ct==0&&(s[i]==c1||s[i]==c2)) + + return i; + + } + + return -1; + +} + + + +int eval(int st, int dr) + +{ + + int p=pozitie(st, dr, '+', '-'); + + if(p==-1) + + p=pozitie(st, dr, '/', '*'); + + if(p==-1) + + if(s[st]=='(') + + return eval(st+1, dr-1); + + if(p==-1) + + { + + int nr=0; + + for(int i=st; i<=dr; i++) + + nr=nr*10+(s[i]-'0'); + + return nr; + + } + + switch(s[p]) + + { + + case '+': + + return (eval(st, p-1)+eval(p+1, dr)); + + break; + + case '-': + + return (eval(st, p-1)-eval(p+1, dr)); + + break; + + case '*': + + return (eval(st, p-1)*eval(p+1, dr)); + + break; + + case '/': + + return (eval(st, p-1)/eval(p+1, dr)); + + break; + + } + +} + + + +int main() + +{ + + f.getline(s, 100005); + + int lgs=strlen(s); + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005], *p = s; + + + +long termen(); + +long factor(); + + + +long eval() + +{ + + long r = termen(); + + while (*p == '+' || *p == '-') + + { + + if (*p == '+') + + { + + p++; + + r += termen(); + + } + + else + + { + + p++; + + r -= termen(); + + } + + } + + return r; + +} + + + + long termen() + + { + + long r = factor(); + + while (*p == '*' || *p == '/') + + { + + if (*p == '*') + + { + + p++; + + r *= factor(); + + } + + else + + { + + p++; + + r /= factor(); + + } + + } + + return r; + + } + + + +long factor() + +{ + + long r = 0; + + if (*p == '(') + + { + + p++; + + r = eval(); + + p++; + + } + + else + + { + + while (isdigit(*p)) + + { + + r = r * 10 + (*p - '0'); + + p++; + + } + + } + + return r; + +} + + + +int main() + +{ + + fin >> s; + + fout << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/8f35e977-a195-40a6-9dfa-2ac62b87fc5a.cpp b/eval/recursive/8f35e977-a195-40a6-9dfa-2ac62b87fc5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7d431226891677230afda682ca300a45e2ec838 --- /dev/null +++ b/eval/recursive/8f35e977-a195-40a6-9dfa-2ac62b87fc5a.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream f("evaluare.in"); ofstream g("evaluare.out"); + +int MAX; + +char S[100010], *p=S; + +int termen(); + +int factor(); + +int eval() + +{ int r = termen(); + + while ( *p=='+' || *p=='-' ) + + switch ( *p ) + + {case '+' : ++p; + + r += termen(); break; + + case '-' : ++p; + + r -= termen(); break; + + } + + return r; + +} + +int termen() + +{ int r = factor(); + + while ( *p=='*' || *p=='/' ) + + switch ( *p ) + + {case '*' : ++p; r *= factor(); break; + + case '/' : ++p; r /= factor(); break; + + } + + return r; + +} + +int factor() + +{ int r=0; + + if (*p =='(') { + + ++p; + + r = eval(); + + ++p; + + } + + else { + + while ( *p>='0' && *p<='9' ) {r = r*10 + *p - '0'; ++p;} + + } + + return r; + +} + +int main() + +{f>>S; MAX=strlen(S); g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],*p=s; + + + +int e2(); + +int e1() + +{ + + int r=e2(); + + while(*p=='+' or *p=='-') + + if(*p=='+') + + p++, r+=e2(); + + else + + p++, r-=e2(); + + return r; + +} + +int e3(); + +int e2() + +{ + + int r=e3(); + + while(*p=='*' or *p=='/') + + if(*p=='*') + + p++, r*=e3(); + + else + + p++, r/=e3(); + + return r; + +} + +int e3() + +{ + + int r=0; + + if(*p=='(') + + p++, r=e1(), p++; + + else + + while(*p>='0' and *p<='9') + + r=r*10+*p-'0', p++; + + return r; + +} + +int main() + +{ + + + + fin >> s; + + fout << e1(); + + + + return 0; + + + +} diff --git a/eval/recursive/9055b148-bf7d-4561-9dfa-fc30f2d1fbc5.cpp b/eval/recursive/9055b148-bf7d-4561-9dfa-fc30f2d1fbc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79de099f34aa8f6b056f62d7a44522de9ff63a08 --- /dev/null +++ b/eval/recursive/9055b148-bf7d-4561-9dfa-fc30f2d1fbc5.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100001],*p; + +int r; + +int eval(); + +int termene(); + +int factor(); + +int eval() + +{ + + int r=termene(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') p++,r+=termene(); + + else p++,r-=termene(); + + } + + return r; + +} + +int termene() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') p++,r*=factor(); + + else p++,r/=factor(); + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='(') + + p++,r=eval(),p++; + + else while('0'<=*p&&'9'>=*p) + + r=r*10+*p-'0',p++; + + return r; + +}int main() + +{ + + f.getline(s,100001); + + p=s; + + r=eval(); + + g< +const int LMAX = 100000; +char s[LMAX + 1]; +int expresie(); +int termen(); +int factor(); +int p = 0; +int expresie() +{ + int sum = termen(); + while(s[p] == '+' || s[p] == '-') + { + if(s[p] == '+') + { + p ++; + sum += termen(); + } + else + { + p ++; + sum -= termen(); + } + } + return sum; +} +int termen() +{ + int prod = factor(); + while(s[p] == '*' || s[p] == '/') + { + if(s[p] == '*') + { + ++p; + prod *= factor(); + } + else + { + ++p; + prod /= factor(); + } + + } + return prod; +} +int factor() +{ + int r = 0; + if(s[p] == '(') + { + ++p; + r = expresie(); + ++p; + } + else + while(s[p] >='0' && s[p] <= '9') + { + r = r * 10 + s[p] - '0'; + p ++; + } + + return r; +} +int main() +{ + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + scanf("%s", s); + printf("%d", expresie()); +} diff --git a/eval/recursive/928215ee-bc2e-4701-98ee-2c28ecc34b55.cpp b/eval/recursive/928215ee-bc2e-4701-98ee-2c28ecc34b55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a719f90ee7875c1c85d65a979f9bdb279ec5e18 --- /dev/null +++ b/eval/recursive/928215ee-bc2e-4701-98ee-2c28ecc34b55.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string expression; + +int expIndex; + + + +int terms(); + +int calculateInteger(); + + + +int calculateExpression() { + + int answer = terms(); + + + + while(expression[expIndex] == '+' || expression[expIndex] == '-') { + + if(expression[expIndex] == '+') { + + ++expIndex; + + answer += terms(); + + } else { + + ++expIndex; + + answer -= terms(); + + } + + } + + ++expIndex; + + + + return answer; + +} + + + +int terms() { + + int makeProduct = calculateInteger(); + + while(expression[expIndex] == '*' || expression[expIndex] == '/') { + + if(expression[expIndex] == '/') { + + ++expIndex; + + makeProduct /= calculateInteger(); + + } else { + + ++expIndex; + + makeProduct *= calculateInteger(); + + } + + } + + + + return makeProduct; + +} + + + +int calculateInteger() { + + int next_integer = 0; + + + + if(expression[expIndex] == '(') { + + ++expIndex; + + return calculateExpression(); + + } + + while(expression[expIndex] >= '0' && expression[expIndex] <= '9' && expIndex < expression.length()) { + + next_integer = next_integer * 10 + (expression[expIndex] - '0'); + + ++expIndex; + + } + + return next_integer; + +} + + + +int main() + +{ + + in >> expression; + + out << calculateExpression(); + + return 0; + +} diff --git a/eval/recursive/93d14614-0bdd-43b6-babb-2737c169134a.cpp b/eval/recursive/93d14614-0bdd-43b6-babb-2737c169134a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..272f6dbbe41d1eeb045e4ecbd24d7f298ad11976 --- /dev/null +++ b/eval/recursive/93d14614-0bdd-43b6-babb-2737c169134a.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int p=0; + +string s; + +int termen(); + +int factor(); + +int evaluare(){ + + long long int r=termen(); + + while (s[p]=='+' || s[p]=='-'){ + + switch (s[p]){ + + case '+': + + p++; + + r+=termen(); + + break; + + case '-': + + p++; + + r-=termen(); + + break; + + } + + } + + return r; + +} + +int termen(){ + + long long int r=factor(); + + while (s[p]=='*' || s[p]=='/'){ + + switch (s[p]){ + + case '*': + + p++; + + r*=factor(); + + break; + + case '/': + + p++; + + r/=factor(); + + break; + + } + + } + + return r; + +} + +int factor(){ + + long long int r=0; + + if (s[p]=='('){ + + ++p; + + r=evaluare(); + + p++; + + } + + else + + { + + while (s[p]>='0' && s[p]<='9') + + { + + r=r*10+s[p]-'0'; + + p++; + + } + + } + + return r; + +} + +int main() + +{ + + fin>>s; + + fout< + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[1000010]; + +int n,i,nr; + +int eval(); + +int factor(); + +int termen(); + +int eval() + +{ + + int r=termen(); + + while(s[nr]=='+' || s[nr]=='-') + + { + + if(s[nr]=='+') + + { + + nr++; + + r+=termen(); + + } + + else + + { + + nr++; + + r-=termen(); + + } + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(s[nr]=='*' || s[nr]=='/') + + { + + if(s[nr]=='*') + + { + + nr++; + + r*=factor(); + + } + + else + + { + + nr++; + + r/=factor(); + + } + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(s[nr]=='(') + + { + + nr++; + + r=eval(); + + nr++; + + } + + while(s[nr]>='0' && s[nr]<='9') + + { + + r=r*10+(s[nr]-'0'); + + nr++; + + } + + return r; + +} + +int main() + +{ + + f.getline(s,1000010); + + nr=0; + + g< + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N=100001; +char s[N]; +int p; + +int evaluare(); +int termen(); +int factor(); + +int expresie () +{ + int sum=termen(); + while (s[p]=='+' || s[p]=='-') + { + if (s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod=factor(); + while (s[p]=='*' || s[p]=='/') + { + if (s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0, semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if (s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while (s[p]>='0' && s[p]<='9') + { + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} + +int main() +{ + in>>s; + out< +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +char a[100001]; + +int x; + +int expresie(); + +int termen(); + +int evaluare(); + +int main() + +{ in>>a; + + out<='0' && a[x]<='9') + + k=k*10+a[x]-'0',x++; + + } + + return k; + +} diff --git a/eval/recursive/970800d6-7ae8-46ae-bfcf-b1fdb76f429c.cpp b/eval/recursive/970800d6-7ae8-46ae-bfcf-b1fdb76f429c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c8bd2bf6490f21c91fe040bf82b5e6010bd6432 --- /dev/null +++ b/eval/recursive/970800d6-7ae8-46ae-bfcf-b1fdb76f429c.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +ifstream fin ("evaluare.in"); +ofstream fout("evaluare.out"); + +const int NMAX=100004; + +char E[NMAX]; +int I; + +int factor(),eval(),produs(); +int eval(){ + + int sum=produs(); + for(;E[I]=='+' or E[I]=='-';){ + + if(E[I]=='+') + ++I,sum+=produs(); + else + ++I,sum-=produs(); + } + return sum; +} + +int produs(){ + int prod=factor(); + for(;E[I]=='*' or E[I]=='/';) + { + if(E[I]=='*') + ++I,prod*=factor(); + if(E[I]=='/') + ++I,prod/=factor(); + } + return prod; +} + +int factor(){ + + if(E[I]=='('){ + ++I; + int r=eval(); + ++I; + return r; + } + + int nr=0; + for(;E[I]<='9' and E[I]>='0';) + nr=nr*10+E[I]-'0',++I; + + return nr; +} + + +int main(){ + fin>>E; + fout< +#include + + + + + + +char op[4][4] = { "+-", "*/", "^", "" }; + + + + + +char S[100010], * p = S; + + + +int eval(int a, int b, char o) { + + switch (o) { + + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + } + +} + +int expr(int lev) { + + int x, y; + + + + if (lev == 2) + + if (*p == '(') + + ++p, x = expr(0), ++p; + + else + + for (x = 0; *p >= '0' && *p <= '9'; ++p) + + x = x * 10 + *p - '0'; + + else + + for (x = expr(lev + 1); strchr(op[lev], *p); x = y) + + y = eval(x, expr(lev + 1), *p++); + + return x; + +} + + + +int main() { + + fgets(S, 100010, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%d\n", expr(0)); + + return 0; + +} diff --git a/eval/recursive/98853744-5986-4e87-82ae-7b2e032cbed6.cpp b/eval/recursive/98853744-5986-4e87-82ae-7b2e032cbed6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28ad50d5f5f7052a37b03609ba3c1ce527d50021 --- /dev/null +++ b/eval/recursive/98853744-5986-4e87-82ae-7b2e032cbed6.cpp @@ -0,0 +1,83 @@ +#include +#include +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100001]; + +int i=0; + + + +int expr(); + +int termen(); + +int factor(); + + + +int main() + +{ + + f>>s; + + g<='0' && s[i]<='9') r=r*10+s[i]-'0', i++; + + return r; + +} diff --git a/eval/recursive/98ab9fc1-1e39-4b41-9ecb-e51e44ea9b64.cpp b/eval/recursive/98ab9fc1-1e39-4b41-9ecb-e51e44ea9b64.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c70d9eea50f8d61437f8517f961f8bddf5bf52e7 --- /dev/null +++ b/eval/recursive/98ab9fc1-1e39-4b41-9ecb-e51e44ea9b64.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +typedef long long ll; + + + +char s[100010], *p=s; + +ll expr(); + +ll subexpr(); + +ll eval() { + + ll r = expr(); + + while ( *p=='+' || *p=='-' ) { + + switch ( *p ) { + + case '+': + + ++p; + + r += expr(); + + break; + + case '-': + + ++p; + + r -= expr(); + + break; + + } + + } + + return r; + +} + + + +ll expr(){ + + ll r = subexpr(); + + while ( *p=='*' || *p=='/' ) { + + switch ( *p ) { + + case '*' : + + ++p; + + r *= subexpr(); + + break; + + case '/': + + ++p; + + r /= subexpr(); + + break; + + } + + } + + return r; + + + +} + +ll subexpr(){ + + ll r = 0; + + if ( *p == '(' ) { + + ++p; + + r = eval(); + + ++p; + + } + + else { + + while ( *p>='0' && *p<='9' ) { + + r = r*10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + +int main(){ + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + fgets(s, 100010, stdin); + + printf("%lld", eval()); + + return 0; + +} diff --git a/eval/recursive/98e825b2-295d-4bbb-bee9-b3568d11c830.cpp b/eval/recursive/98e825b2-295d-4bbb-bee9-b3568d11c830.cpp new file mode 100644 index 0000000000000000000000000000000000000000..710feeae16bea1c2650433c7c12d6a1cbeeacd79 --- /dev/null +++ b/eval/recursive/98e825b2-295d-4bbb-bee9-b3568d11c830.cpp @@ -0,0 +1,153 @@ +#include +#include + + +using namespace std; + + + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + + + +char s[1000010]; + +char *p=s; + + + +long int termen(); + +long int factor(); + + + +long int eval() + +{ + + long int r=termen(); + + + + while(*p=='+'||*p=='-') + + switch(*p){ + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -= termen(); + + break; + + } + + + + return r; + +} + + + +long int termen() + +{ + + long int r=factor(); + + + + while(*p=='*'||*p=='/') + + switch(*p){ + + case '*': + + ++p; + + r *= factor(); + + break; + + case '/': + + ++p; + + r /= factor(); + + break; + + } + + + + return r; + +} + + + +long int factor() + +{ + + long int r=0; + + + + if( *p=='(' ) + + {++p; + + r = eval(); + + ++p;} + + else + + { + + while(*p>='0'&&*p<='9') + + {r = r*10 + *p - '0'; + + ++p;} + + } + + + + return r; + +} + + + +int main() + +{ + + fi>>s; + + + + fo< + + +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +char S[100100], *p=S; + + + +long long eval(); + + + +long long termen(); + + + +long long factor(); + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + cin>>S; + + cout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100001]; + + + +int evaluate(char*& expr); + +int operand(char*& expr); + +int value(char*& expr); + +int plus_minus(char*& expr); + +int ori_div(char*& expr); + + + +int main(){ + + fin.get(s, 100001 -1); + + char* expr = s; + + fout< +#include +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +char c[100005]; + +int ind; + +int fact(),exp(),pol(); + +int exp() + +{ + + int t=fact(); + + while(c[ind]=='+'||c[ind]=='-') + + { ++ind; + + if(c[ind-1]=='+') + + t+=fact(); + + else + + t-=fact(); + + } + + return t; + +} + +int fact() + +{ + + int t=pol(); + + while(c[ind]=='*'||c[ind]=='/') + + { + + ind++; + + if(c[ind-1]=='*') t*=pol(); + + else + + t/=pol(); + + } + + return t; + +} + +int pol() + +{ int t=0; + + if(c[ind]=='(') + + { ++ind; + + t=exp(); + + ++ind; + + return t; + + } + + else + + { + + while(isdigit(c[ind])) + + { + + t=t*10+c[ind]-'0'; + + ++ind; + + } + + } + + return t; + +} + +int main() + +{ + + cin>>(c+1); + + int x=strlen(c)-1; + + ind=1; + + cout< +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char s[100001],*p=s; + +int adunare (); + +int inmultire (); + +int paranr (); + +int adunare () + +{ + + int r; + + r=inmultire(); + + while (*p=='+' || *p=='-') + + if (*p=='+') + + { + + p++; + + r+=inmultire(); + + } + + else + + if (*p=='-') + + { + + p++; + + r-=inmultire(); + + } + + return r; + +} + +int inmultire () + +{ + + int r; + + r=paranr(); + + while (*p=='*' || *p=='/') + + if (*p=='*') + + { + + p++; + + r*=paranr(); + + } + + else + + if (*p=='/') + + { + + p++; + + r/=paranr(); + + } + + return r; + +} + +int paranr () + +{ + + int r=0; + + if (*p=='(') + + { + + p++; + + r=adunare(); + + p++; + + } + + else + + while (*p>='0' && *p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main () + +{ + + fin.get(s,100001); + + fout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +const int MAXN = 1e6 + 5; + +char sir[MAXN], *p = sir; + +long long termen(); + +long long factor(); + +long long eval(){ + + long long r = termen(); + + while(*p == '+' or *p == '-'){ + + if(*p == '+'){ + + ++p; + + r += termen(); + + } + + else if(*p == '-'){ + + ++p; + + r -= termen(); + + } + + } + + return r; + +} + +long long termen(){ + + long long r = factor(); + + while(*p == '*' or *p == '/'){ + + if(*p == '*'){ + + ++p; + + r *= factor(); + + } + + else if(*p == '/'){ + + ++p; + + r /= factor(); + + } + + } + + return r; + +} + +long long factor(){ + + long long r = 0; + + if(*p == '(') { + + ++p; + + r = eval(); + + ++p; + + } + + else{ + + while(*p >= '0' and *p <= '9') + + r = r * 10 + *p - '0', ++p; + + } + + return r; + +} + +int main(){ + + fin.getline(sir, MAXN); + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/9f6eb477-54c1-46e4-b356-342bcdceab21.cpp b/eval/recursive/9f6eb477-54c1-46e4-b356-342bcdceab21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e21ba4cdc453150204bd5e2f2026f74a06af808 --- /dev/null +++ b/eval/recursive/9f6eb477-54c1-46e4-b356-342bcdceab21.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +const int Nmax = 1e5 + 5; + +char *p,S[Nmax]; + +int eval(); + +int termen(); + +int factor(); + + + +int factor() + +{ + + int r = 0; + + if(*p == '(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else while(isdigit(*p)) + + { + + r = r*10 + *p - '0'; + + ++p; + + } + + return r; + +} + +int termen() + +{ + + int r = factor(); + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + { + + ++p; + + r *= factor(); + + } + + else + + { + + ++p; + + r /= factor(); + + } + + } + + return r; + +} + +int eval() + +{ + + int r = termen(); + + while(*p == '+' || *p == '-') + + { + + if(*p == '+') + + { + + ++p; + + r += termen(); + + } + + else + + { + + ++p; + + r -= termen(); + + } + + } + + return r; + +} + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + + + cin.getline(S,sizeof(S)); + + p = S; + + cout << eval(); + + return 0; + +} diff --git a/eval/recursive/9f9ef112-c539-4610-8c0b-dcef2e4c1a91.cpp b/eval/recursive/9f9ef112-c539-4610-8c0b-dcef2e4c1a91.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c602a063e067d47d39d6a4d6f52064897679a07e --- /dev/null +++ b/eval/recursive/9f9ef112-c539-4610-8c0b-dcef2e4c1a91.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +int sum_diff(); + +int prod_imp(); + +int term(); + +string s; + +int ind; + +int sum_diff() + +{ + + int k=prod_imp(); + + while(ind>s; + + out< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/a09361b8-ee8b-49a3-83e7-0a6cfb6cbfcd.cpp b/eval/recursive/a09361b8-ee8b-49a3-83e7-0a6cfb6cbfcd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ad164c593197a3bbd8f57d4a0aac495d6dacb2a --- /dev/null +++ b/eval/recursive/a09361b8-ee8b-49a3-83e7-0a6cfb6cbfcd.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int l; + +char op[4][4] = { "+-", "*/", "^", "" }; + +char c[100010],*p=c; + +int eval (int a,int b,char o) + +{ + + switch ( o ) + + { + + case '+': return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/': return a/b; + + } + +} + +int expr (int niv) + +{ + + int x,y; + + if (niv==2) + + { + + if (*p=='(') + + { + + ++p; + + x=expr(0); + + ++p; + + } + + else + + { + + for (x=0; *p>='0' && *p<='9'; ++p) + + { + + x=x*10+*p-'0'; + + } + + } + + } + + else + + { + + for (x=expr(niv+1); strchr(op[niv],*p); x=y) + + { + + y=eval(x,expr(niv+1),*p++); + + } + + } + + return x; + +} + +int main() + +{ + + f.getline(c,100001); + + l=strlen(c); + + c[l]='_'; + + g< + +using namespace std; + +ifstream fin ("evaluare.in"); +ofstream fout ("evaluare.out"); + +const int Dim = 1e6 +5; +char s[Dim]; +int p = 1; +int factor(); +int exp(); +int termen(); + +int exp() { + + int sum = termen(); + while ( s[p] == '+' or s[p] == '-') { + + if ( s[p] == '+') { + + ++p; + sum += termen(); + } + else + if ( s[p] == '-') { + + ++p; + sum -= termen(); + } + } + return sum; +} + +int termen() { + + int prod = factor(); + while ( s[p] == '*' or s[p] == '/' ) { + + if ( s[p] == '*') { + + ++p; + prod *= factor(); + } + else if ( s[p] == '/' ) { + ++p; + prod /= factor(); + } + } + return prod; +} + +int factor() { + + int val = 0, semn = 1; + while ( s[p] == '-') { + + ++p; + semn *= -1; + } + if ( s[p] == '(') { + + p++; + val = exp(); + p++; + return semn * val; + } + while ( isdigit(s[p]) ) { + val = val * 10 + s[p] - '0'; + ++p; + } + return semn * val; +} + +int main() { + + fin >> (s + 1); + fout << exp(); + +} diff --git a/eval/recursive/a0a8a370-3a80-4c12-a60d-06c022b224e1.cpp b/eval/recursive/a0a8a370-3a80-4c12-a60d-06c022b224e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef3bcf5b118bda3cec2c8971c0c813ad1e882044 --- /dev/null +++ b/eval/recursive/a0a8a370-3a80-4c12-a60d-06c022b224e1.cpp @@ -0,0 +1,161 @@ +#include +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string s; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while (s[p]=='+' || s[p]=='-') + + { + + if (s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while (s[p]=='*' || s[p]=='/') + + { + + if (s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0, semn=1; + + while (s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if (s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while (s[p]>='0' && s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + cin >> s; + + cout << expresie(); + + return 0; + +} diff --git a/eval/recursive/a1bfe720-52b9-4a75-9b4b-9432cd6c8b9c.cpp b/eval/recursive/a1bfe720-52b9-4a75-9b4b-9432cd6c8b9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b16cdb3ca3fa6b910236d8adb4d1e4fd9e859da4 --- /dev/null +++ b/eval/recursive/a1bfe720-52b9-4a75-9b4b-9432cd6c8b9c.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string s; + +long long p; + + + +long long termen(); + +long long factor(); + + + +long long eval() { + + long long r = termen(); + + while (s[p] == '+' || s[p] == '-') { + + if (s[p] == '+') { + + ++p; + + r += termen(); + + } + + else if (s[p] == '-') { + + ++p; + + r -= termen(); + + } + + } + + return r; + +} + + + +long long termen() { + + long long r = factor(); + + while (s[p] == '*' || s[p] == '/') { + + if (s[p] == '*') { + + ++p; + + r *= factor(); + + } + + else if (s[p] == '/') { + + ++p; + + r /= factor(); + + } + + } + + return r; + +} + + + +long long factor() { + + long long r = 0; + + if (s[p] == '(' || s[p] == ')') { + + ++p; + + r = eval(); + + ++p; + + } + + else + + while (s[p] >= '0' && s[p] <= '9') { + + r = r * 10 + (s[p] - '0'); + + ++p; + + } + + return r; + +} + + + +int main() { + + getline(fin, s); + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/a1f2ad99-34ad-4ef1-8f2a-7c3fadc7b3ad.cpp b/eval/recursive/a1f2ad99-34ad-4ef1-8f2a-7c3fadc7b3ad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07812bd18f78819b55af142419fbb4b46c45f563 --- /dev/null +++ b/eval/recursive/a1f2ad99-34ad-4ef1-8f2a-7c3fadc7b3ad.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int Expresie(); + +int Termen(); + +int Factor(); + +char S[100007]; + +int indexxx; + + + +int Expresie() + +{ + + int r=Termen(); + + while(S[indexxx]=='+'||S[indexxx]=='-') + + { + + indexxx++; + + if(S[indexxx-1]=='+') r+=Termen(); + + else r-=Termen(); + + } + + return r; + +} + + + +int Termen() + +{ + + int r=Factor(); + + while(S[indexxx]=='/'||S[indexxx]=='*') + + { + + indexxx++; + + if(S[indexxx-1]=='/') r/=Factor(); + + else r*=Factor(); + + } + + return r; + +} + + + +int Factor() + +{ + + int sgn=1; + + while(S[indexxx]=='-') sgn=-sgn,indexxx++; + + + + if(S[indexxx]=='(') + + { + + indexxx++; + + int r=Expresie(); + + indexxx++; + + return r*sgn; + + } + + int r=0; + + + + while(S[indexxx]>='0'&&S[indexxx]<='9') + + r=r*10+(S[indexxx]-'0'),indexxx++; + + + + return r; + +} + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); freopen("evaluare.out","w",stdout); + + cin.getline(S,100007); + + g< + + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +const int dim = 100005; + + + +int current; + +char a[dim]; + + + +int evaluare(); + +int termen(); + +int factor(); + + + +bool isOp1(char c) + +{ + + return (c == '+' || c == '-'); + +} + + + + + +bool isOp2(char c) + +{ + + return (c == '*' || c == '/'); + +} + + + +bool isDigit(char c) + +{ + + if(c >= '0' && c <='9') + + return true; + + return false; + +} + + + +int getSign(char c) + +{ + + if(c == '-') + + return -1; + + else if(c == '+') + + return 1; + + return 0; + +} + + + +int getDigit(char c) + +{ + + return c - '0'; + +} + + + +int evaluare() + +{ + + int rez = termen(), sign = 0; + + while((sign = getSign(a[current])) != 0) + + { + + current++; + + rez += sign * termen(); + + } + + return rez; + +} + + + +int termen() + +{ + + int rez = factor(); + + while(isOp2(a[current])) + + { + + if(a[current] == '*') + + current++, rez *= factor(); + + else if(a[current] == '/') + + current++, rez /= factor(); + + } + + return rez; + +} + + + +int factor() + +{ + + int rez = 0, sign = 1; + + while(a[current] == '-') + + { + + current++; + + sign *= -1; + + } + + + + if(a[current] == '(') + + { + + current++; + + rez = evaluare(); + + current++; + + return rez * sign; + + } + + + + while(isDigit(a[current])) + + { + + rez = rez * 10 + getDigit(a[current]); + + current++; + + } + + + + return rez * sign; + +} + + + +int main() + +{ + + cin>>a; + + int rez = evaluare(); + + + + cout< +#include +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char s[100005], *p; + + + +int eval(); + +int termen(); + +int factor(); + + + +int eval() + +{ + + int r = termen(); + + while (*p == '+' || *p == '-') + + if (*p == '+') + + { + + ++p; + + r += termen(); + + } + + else + + { + + ++p; + + r -= termen(); + + } + + return r; + +} + + + +int termen() + +{ + + int r = factor(); + + while (*p == '*' || *p == '/') + + if (*p == '*') + + { + + ++p; + + r *= factor(); + + } + + else + + { + + ++p; + + r /= factor(); + + } + + return r; + +} + + + +int factor() + +{ + + int r = 0; + + if (*p == '(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else + + { + + while (*p >= '0' && *p <= '9') + + { + + r = r * 10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + + + +int main() + +{ + + f >> s; + + p = s; + + g << eval(); + + return 0; + +} diff --git a/eval/recursive/a28f5108-ec55-4fe2-9bc7-6aeb5b67c58d.cpp b/eval/recursive/a28f5108-ec55-4fe2-9bc7-6aeb5b67c58d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c61331f91e2812c63fa8c70d70f6125afea7b1be --- /dev/null +++ b/eval/recursive/a28f5108-ec55-4fe2-9bc7-6aeb5b67c58d.cpp @@ -0,0 +1,84 @@ +#include + + +std::string s; + +int curr; + + + +int solve(); + + + +int factor() { + + int ans = 0; + + if (s[curr] == '(') { + + curr++, ans += solve(), curr++; + + return ans; + + } + + while (curr < s.size() and isdigit(s[curr])) ans = ans * 10 + (int)(s[curr++] - '0'); + + return ans; + +} + + + +int termen() { + + int ans = factor(); + + while (curr < s.size()) { + + if (s[curr] == '*') curr++, ans *= factor(); + + else if (s[curr] == '/') curr++, ans /= factor(); + + else break; + + } + + return ans; + +} + + + +int solve() { + + int ans = termen(); + + while (curr < s.size()) { + + if (s[curr] == '+') curr++, ans += termen(); + + else if (s[curr] == '-') curr++, ans -= termen(); + + else break; + + } + + return ans; + +} + + + +int main() { + + std::ifstream fin("evaluare.in"); + + std::ofstream fout("evaluare.out"); + + fin >> s; + + fout< + +using namespace std; + +int where = 0; +char s[100002]; + +int solveMultiplicationDivision(); +int solveBracketsNumbers(); + +int solveAdditionSubtraction() { + int value = solveMultiplicationDivision(); + while (s[where] == '+' or s[where] == '-') { + if (s[where] == '+') { + where += 1; + value += solveMultiplicationDivision(); + } else { + where += 1; + value -= solveMultiplicationDivision(); + } + } + return value; +} + +int solveMultiplicationDivision() { + int value = solveBracketsNumbers(); + while (s[where] == '*' or s[where] == '/') { + if (s[where] == '*') { + where += 1; + value *= solveBracketsNumbers(); + } else { + where += 1; + value /= solveBracketsNumbers(); + } + } + return value; +} + +int solveBracketsNumbers() { + int result = 0; + if (s[where] == '(') { + where += 1; + result = solveAdditionSubtraction(); + where += 1; + } else { + while ('0' <= s[where] and s[where] <= '9') { + result = result * 10 + (s[where] - '0'); + where += 1; + } + } + return result; +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + fin.getline(s, 100002); + fout << solveAdditionSubtraction(); + return 0; +} diff --git a/eval/recursive/a42bb402-a552-4693-92f1-29fe3da8b87d.cpp b/eval/recursive/a42bb402-a552-4693-92f1-29fe3da8b87d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c06849e5a55e0f2fb522ef751a74cc890444d89 --- /dev/null +++ b/eval/recursive/a42bb402-a552-4693-92f1-29fe3da8b87d.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100001; + + + +char s[N]; + + + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie(){ + + int sum = termen(); + + while( s[p] == '+' || s[p] == '-'){ + + if(s[p]=='+'){ + + p++; + + sum += termen(); + + }else{ + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen(){ + + int prod=factor(); + + while(s[p] == '*' || s[p] == '/'){ + + if(s[p] == '*'){ + + p++; + + prod*=factor(); + + }else{ + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor(){ + + int val=0,semn=1; + + while(s[p] == '-'){ + + p++; + + semn = -semn; + + } + + if(s[p] == '('){ + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while('0' <= s[p] && s[p] <= '9'){ + + val = val*10 + (s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/a4891206-2714-432f-9b18-ae39d34f5623.cpp b/eval/recursive/a4891206-2714-432f-9b18-ae39d34f5623.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09f0c3cc9d17f1dab28322e35d07a2f8d2c1aa40 --- /dev/null +++ b/eval/recursive/a4891206-2714-432f-9b18-ae39d34f5623.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int expresie(), termen(), factor(); + +int i; + +char s[100010]; + + + +int expresie(){ + + int r = termen(); + + while ( s[i] == '+' || s[i] == '-' ) { + + if ( s[i] == '+' ){ + + i++; + + r += termen(); + + } else { + + i++; + + r -= termen(); + + } + + } + + return r; + +} + + + +int termen(){ + + int r = factor(); + + while ( s[i] == '*' || s[i] == '/' ){ + + if ( s[i] == '*' ){ + + i++; + + r *= factor(); + + } else { + + i++; + + r /= factor(); + + } + + } + + return r; + +} + + + +int factor(){ + + int r; + + if ( s[i] == '(' ){ + + i++; + + r = expresie(); + + i++; + + } else { + + r = 0; + + while ( s[i] >= '0' && s[i] <= '9' ) + + r = r*10+s[i++]-'0'; + + } + + return r; + +} + + + +int main() + +{ + + f>>s; i = 0; + + g< +using namespace std; + + + +const int NMAX=100010; + +char s[NMAX]; + +int ind; + + + +int evaluare(); + +int termen(); + +int factor(); + + + +int expresie(){ + + int ans=termen(); + + while(s[ind]=='+' || s[ind]=='-'){ + + int semn=1; + + if(s[ind++]=='-') + + semn=-1; + + ans=ans+semn*termen(); + + } + + return ans; + +} + + + +int termen(){ + + int ans=factor(); + + while(s[ind]=='*' || s[ind]=='/'){ + + if(s[ind]=='*'){ + + ind++; + + ans*=factor(); + + } + + else{ + + ind++; + + ans/=factor(); + + } + + } + + return ans; + +} + + + +int factor(){ + + if(s[ind]=='\n' || s[ind]==EOF) + + return 0; + + if(s[ind]=='('){ + + ind++; + + int ans=expresie(); + + ind++; + + return ans; + + } + + int nr=0; + + while(isdigit(s[ind])){ + + nr=nr*10+(s[ind]-'0'); + + ind++; + + } + + return nr; + +} + + + +int main(){ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + cin>>s; + + printf("%d", expresie()); + + return 0; + +} diff --git a/eval/recursive/a51843e6-6937-42a6-9a98-9dea6528a8f7.cpp b/eval/recursive/a51843e6-6937-42a6-9a98-9dea6528a8f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a2948f6dc04c0325262809b74e00ef590c5d58a --- /dev/null +++ b/eval/recursive/a51843e6-6937-42a6-9a98-9dea6528a8f7.cpp @@ -0,0 +1,66 @@ +#include +#include + + + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +int n,ind; +char s[100000 +1]; + +int eval(); +int inm_sau_imp(); +int termen(); + +int eval(){ + int rez=inm_sau_imp(); + while(s[ind]=='+'||s[ind]=='-'){ + if(s[ind]=='+'){ + ind++; + rez=rez+inm_sau_imp(); + }else{ + ind++; + rez=rez-inm_sau_imp(); + } + } + return rez; +} + +int inm_sau_imp(){ + int rez=termen(); + while(s[ind]=='*'||s[ind]=='/'){ + if(s[ind]=='*'){ + ind++; + rez=rez*termen(); + }else{ + ind++; + rez=rez/termen(); + } + } + return rez; +} + +int termen(){ + int rez=0; + if(s[ind]=='('){ + ind++; + rez=eval(); + ind++; + }else{ + while(s[ind]<='9'&&s[ind]>='0'){ + rez=rez*10+s[ind]-'0'; + ind++; + } + } + return rez; +} + +int main(){ + f>>s; + g< +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + + + +char s[100010], *p=s; + + + +int factor(); + +int numar(); + + + +int solve(){ + + int r=factor(); + + while(*p=='+'||*p=='-') + + switch(*p){ + + case '+': + + p++; + + r+=factor(); + + break; + + case '-': + + p++; + + r-=factor(); + + break; + + } + + return r; + +} + + + +int factor(){ + + int r=numar(); + + while(*p=='*'||*p=='/') + + switch(*p){ + + case '*': + + p++; + + r*=numar(); + + break; + + case '/': + + p++; + + r/=numar(); + + break; + + } + + return r; + +} + + + +int numar(){ + + int r=0; + + if ( *p == '(' ) { + + ++p; + + r = solve(); + + ++p; + + }else + + while(*p>='0'&&*p<='9'){ + + r=r*10+(*p-'0'); + + p++; + + } + + return r; + +} + + + +int main() + +{ + + cin.getline(s,100010); + + cout< +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); + + + +string s; + +int i; + + + +int f1(); + +int f2(); + +int f3(); + + + +int f1() + +{ + + int ans = f2(); + + while (s[i] == '+' || s[i] == '-') + + { + + if (s[i] == '+') + + { + + i++; + + ans = ans + f2(); + + } + + else + + { + + i++; + + ans = ans - f2(); + + } + + } + + return ans; + +} + + + +int f2() + +{ + + int ans = f3(); + + while (s[i] == '*' || s[i] == '/') + + { + + if (s[i] == '*') + + { + + i++; + + ans = ans * f3(); + + } + + else + + { + + i++; + + ans = ans / f3(); + + } + + } + + return ans; + +} + + + +int f3() + +{ + + int ans = 0; + + if (s[i] == '(') + + { + + i++; + + ans = f1(); + + i++; + + } + + else + + { + + while (i < s.size() && s[i] >= '0' && s[i] <= '9') + + { + + ans = ans * 10 + (s[i] - '0'); + + i++; + + } + + } + + return ans; + +} + + + + + +int main() + +{ + + cin >> s; + + cout << f1(); + + return 0; + +} diff --git a/eval/recursive/a7d07adb-27b5-4d6b-bca4-0bfa149950e4.cpp b/eval/recursive/a7d07adb-27b5-4d6b-bca4-0bfa149950e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d02467750360cfa323dff9e92b7be7edc50acb6 --- /dev/null +++ b/eval/recursive/a7d07adb-27b5-4d6b-bca4-0bfa149950e4.cpp @@ -0,0 +1,115 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int i; + +string s; + + + +int eval(); + +int termen(); + +int factor(); + + + +int eval() + +{ int val = termen(); + + while(s[i] == '+' || s[i] == '-') + + { if(s[i] == '+') + + i++, val = val + termen(); + + else + + i++, val = val - termen(); + + } + + return val; + +} + + + +int termen() + +{ int val = factor(); + + while(s[i] == '*' || s[i] == '/') + + { if(s[i] == '*') + + i++, val = val * factor(); + + if(s[i] == '/') + + i++, val = val / factor(); + + } + + return val; + +} + + + +int factor() + +{ int val = 0; + + if(s[i] == '(') + + { i++; + + val = eval(); + + i++; + + } + + else + + { while(s[i] >= '0' && s[i] <= '9') + + { val = val * 10 + s[i] - '0'; + + i++; + + } + + } + + return val; + +} + + + +int main() + +{ + + f >> s; + + g << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/a8d31d2a-4d6d-4446-a53a-fc103970e758.cpp b/eval/recursive/a8d31d2a-4d6d-4446-a53a-fc103970e758.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10a12b690490e858572fb9569dcb674d33dfaea6 --- /dev/null +++ b/eval/recursive/a8d31d2a-4d6d-4446-a53a-fc103970e758.cpp @@ -0,0 +1,89 @@ +#include + +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +string s; + +int eval(); +int termen(); +int factor(); + +int pos = 0; +int n; + +int eval() +{ + int r = termen(); + + while(pos < n && (s[pos] == '+' || s[pos] == '-')) + { + if(s[pos] == '+') + { + ++pos; + r += termen(); + } + else + { + ++pos; + r -= termen(); + } + } + + return r; +} + +int termen() +{ + int f = factor(); + + while(pos < n && (s[pos] == '*' || s[pos] == '/')) + { + if(s[pos] == '*') + { + ++pos; + f *= factor(); + } + else + { + ++pos; + f /= factor(); + } + } + + return f; +} + +int factor() +{ + int e = 0; + + if(s[pos] == '(') + { + ++pos; + e = eval(); + ++pos; + + return e; + } + else + { + while(s[pos] >= '0' && s[pos] <= '9') + { + e = e * 10 + s[pos] - '0'; + ++pos; + } + + return e; + } +} + +main() +{ + fin >> s; + n = s.size(); + fout << eval(); + +} diff --git a/eval/recursive/a97980d7-0111-4794-a52d-1c9912e5bae2.cpp b/eval/recursive/a97980d7-0111-4794-a52d-1c9912e5bae2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69dc36af101067616cd52bdb9157379ef786fe04 --- /dev/null +++ b/eval/recursive/a97980d7-0111-4794-a52d-1c9912e5bae2.cpp @@ -0,0 +1,200 @@ +#include + + +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +int n,ind; + +string s; + +int eval(); + +int numar(); + +int main() + +{ + + cin>>s; + + n=s.size(); + + ind=0; + + cout<='0' and ind='0'){ + + prod=numar(); + + while(s[ind]=='*' or s[ind]=='/'){ + + if(s[ind]=='/'){ + + ind++; + + if(s[ind]=='('){ + + ind++; + + prod/=eval(); + + } + + else{ + + prod/=numar(); + + } + + } + + else{ + + ind++; + + if(s[ind]=='('){ + + ind++; + + prod*=eval(); + + } + + else + + prod*=numar(); + + } + + } + + sum+=semn*prod; + + semn=1; + + continue; + + } + + if(s[ind]==')'){ + + ind++; + + return sum; + + } + + if(s[ind]=='('){ + + ind++; + + prod=eval(); + + while(s[ind]=='*' or s[ind]=='/'){ + + if(s[ind]=='/'){ + + ind++; + + if(s[ind]=='('){ + + ind++; + + prod/=eval(); + + } + + else + + prod/=numar(); + + } + + else{ + + ind++; + + if(s[ind]=='('){ + + ind++; + + prod*=eval(); + + } + + else + + prod*=numar(); + + } + + } + + sum+=semn*prod; + + semn=1; + + } + + } + + return sum; + +} diff --git a/eval/recursive/aa1b2dc1-b5b3-4801-8b82-1d5c760336bd.cpp b/eval/recursive/aa1b2dc1-b5b3-4801-8b82-1d5c760336bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9722779b01f3c657d87b2f1d8f7bb168f36613d --- /dev/null +++ b/eval/recursive/aa1b2dc1-b5b3-4801-8b82-1d5c760336bd.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +string s; + +int p; + + + +int termen(); + +int fact(); + + + +int eval() { + + int nr=termen(); + + while (p='0' && s[p]<='9') { + + nr=nr*10+s[p]-'0'; + + ++p; + + } + + } + + return nr; + +} + + + +int main() { + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + cin>>s; + + cout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],*p=s; + + + +long e1(); + +long e2(); + +long e() + +{ + + long r=e1(); + + while(*p=='+' or *p=='-') + + if(*p=='+') + + ++p, r+=e1(); + + else + + ++p, r-=e1(); + + return r; + +} + +long e1() + +{ + + long r=e2(); + + while(*p=='*' or *p=='/') + + if(*p=='*') + + ++p, r*=e2(); + + else + + ++p, r/=e2(); + + return r; + +} + +long e2() + +{ + + long r=0; + + if(*p=='(') + + ++p, r=e(), ++p; + + else + + while(*p>='0' and *p<='9') + + r=r*10+*p-'0', ++p; + + return r; + +} + + + +int main() + +{ + + + + fin >> s; + + fout << e(); + + + + return 0; + + + +} diff --git a/eval/recursive/abd5a8bf-21a0-4c1a-b2bf-010ebd14c018.cpp b/eval/recursive/abd5a8bf-21a0-4c1a-b2bf-010ebd14c018.cpp new file mode 100644 index 0000000000000000000000000000000000000000..353ef82cb1ed753d91fc7445f586086551f591f8 --- /dev/null +++ b/eval/recursive/abd5a8bf-21a0-4c1a-b2bf-010ebd14c018.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +const int N = 100010; + +char s[N],*p; + +int suma(),produs(),factor(); + +int main() + +{ + + f>>s; + + p=s; + + g< +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string sir; + +int idx; + + + +long long eval(); + +long long term(); + +long long factor(); + + + +int main() + +{ + + cin>>sir; + + + + cout<='0' && sir[idx]<='9') + + { + + rsp=rsp*10+sir[idx]-'0'; + + ++idx; + + } + + } + + return rsp; + +} diff --git a/eval/recursive/ad7fc4b4-2a23-4b18-a2c9-cf26d6deed57.cpp b/eval/recursive/ad7fc4b4-2a23-4b18-a2c9-cf26d6deed57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff3a43ab4a69203ad0945e9c3daaf064e66822de --- /dev/null +++ b/eval/recursive/ad7fc4b4-2a23-4b18-a2c9-cf26d6deed57.cpp @@ -0,0 +1,118 @@ +#include +char s[100005]; + +char *p=s; + +int factor(); + +int termen(); + + + +int eval(){ + + int r=factor(); + + while(*p=='+'||*p=='-'){ + + if(*p=='+'){ + + ++p; + + r=r+factor(); + + } + + else{ + + ++p; + + r=r-factor(); + + } + + } + + return r; + +} + + + +int factor(){ + + int r=termen(); + + while(*p=='*'||*p=='/'){ + + if(*p=='*'){ + + ++p; + + r=r*termen(); + + } + + else{ + + ++p; + + r=r/termen(); + + } + + } + + return r; + +} + + + +int termen(){ + + int r; + + if(*p=='('){ + + p++; + + r=eval(); + + p++; + + } + + else{ + + r=0; + + while(*p<='9'&&*p>='0'){ + + r=r*10+*p-'0'; + + p++; + + } + + } + + return r; + +} + + + + + +int main(){ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + scanf("%s",&s); + + printf("%d",eval()); + +} diff --git a/eval/recursive/b1490854-df01-49fa-a804-d440605f2fda.cpp b/eval/recursive/b1490854-df01-49fa-a804-d440605f2fda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b97b7ce470c46420bc9a0e92773324ac87bd66cb --- /dev/null +++ b/eval/recursive/b1490854-df01-49fa-a804-d440605f2fda.cpp @@ -0,0 +1,262 @@ +#include +#include +#include + + +using namespace std; + + + + + +string const inFile = "evaluare.in"; + +string const outFile = "evaluare.out"; + + + + + +ifstream Read(inFile); + +ofstream Write(outFile); + + + + + +int GetNumber(string const &expr, unsigned &index) { + + int number = 0; + + + + for (; expr[index] >= '0' && expr[index] <= '9'; ++index) { + + number = number * 10 + expr[index] - '0'; + + } + + + + --index; + + + + return number; + +} + + + + + +int EvalPriority(vector const &operands, string const &operators, unsigned &index) { + + int result = operands[index]; + + + + for (; index < operators.size(); ++index) { + + if (operators[index] == '*') { + + result *= operands[index + 1]; + + } + + else if (operators[index] == '/') { + + result /= operands[index + 1]; + + } + + else { + + break; + + } + + } + + + + return result; + +} + + + + + +int EvalSimple(vector const &operands, string const &operators) { + + vector _operands; + + string _operators; + + unsigned index = 0; + + + + if (operators[0] == '*' || operators[0] == '/') { + + _operands.push_back(EvalPriority(operands, operators, index)); + + } + + else { + + _operands.push_back(operands[0]); + + } + + + + for (; index < operators.size(); ++index) { + + if (operators[index] == '*' || operators[index] == '/') { + + _operands.push_back(EvalPriority(operands, operators, index)); + + --index; + + } + + else { + + if (index == operators.size() - 1) { + + _operands.push_back(operands[index + 1]); + + } + + else if (operators[index + 1] == '+' || operators[index + 1] == '-') { + + _operands.push_back(operands[index + 1]); + + } + + + + _operators.push_back(operators[index]); + + } + + } + + + + int result = _operands[0]; + + + + for (unsigned i = 0; i < _operators.size(); ++i) { + + if (_operators[i] == '+') { + + result += _operands[i + 1]; + + } + + else if (_operators[i] == '-') { + + result -= _operands[i + 1]; + + } + + } + + + + return result; + +} + + + + + +int Eval(string const &expr, unsigned &index) { + + vector operands; + + string operators; + + + + for (; index < expr.size(); ++index) { + + if (expr[index] == '(') { + + operands.push_back(Eval(expr, ++index)); + + } + + else if (expr[index] == ')') { + + break; + + } + + else if (expr[index] == '-') { + + operators.push_back('-'); + + } + + else if (expr[index] == '+') { + + operators.push_back('+'); + + } + + else if (expr[index] == '*') { + + operators.push_back('*'); + + } + + else if (expr[index] == '/') { + + operators.push_back('/'); + + } + + else if (expr[index] >= '0' && expr[index] <= '9') { + + operands.push_back(GetNumber(expr, index)); + + } + + } + + + + return EvalSimple(operands, operators); + +} + + + + + +int main() { + + string expr; + + unsigned index = 0; + + + + Read >> expr; + + + + Write << Eval(expr, index); + + + + return 0; + +} diff --git a/eval/recursive/b167ce30-f461-4e9d-8fe2-18a004bfd42d.cpp b/eval/recursive/b167ce30-f461-4e9d-8fe2-18a004bfd42d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba562433559637951a8d9a2bf2a8803432a8768e --- /dev/null +++ b/eval/recursive/b167ce30-f461-4e9d-8fe2-18a004bfd42d.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int i; + +string s; + + + +int eval(); + +int termen(); + +int factor(); + + + +int eval() + +{ int r = termen(); + + while(s[i] == '+' || s[i] == '-') + + { if(s[i] == '+') + + { i++; + + r = r + termen(); + + } + + else + + { i++; + + r = r - termen(); + + } + + } + + return r; + +} + + + +int termen() + +{ int r = factor(); + + while(s[i] == '*' || s[i] == '/') + + { if(s[i] == '*') + + { i++; + + r = r * factor(); + + } + + else + + { i++; + + r = r / factor(); + + } + + } + + return r; + +} + + + +int factor() + +{ int r = 0; + + if(s[i] == '(') + + { i++; + + r = eval(); + + i++; + + } + + else + + { while(s[i] >= '0' && s[i] <= '9') + + { r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} + + + +int main() + +{ + + f >> s; + + g << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/b17dbf3c-65a1-48bc-8131-602483d72fdd.cpp b/eval/recursive/b17dbf3c-65a1-48bc-8131-602483d72fdd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2618f37fd653b97ca9eae39a01bf28435c2575 --- /dev/null +++ b/eval/recursive/b17dbf3c-65a1-48bc-8131-602483d72fdd.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/b414141c-781e-467d-a3b8-9aa7c65b5eef.cpp b/eval/recursive/b414141c-781e-467d-a3b8-9aa7c65b5eef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bd728e52e7ec99e3348ea849a3e7d68c39ba29d --- /dev/null +++ b/eval/recursive/b414141c-781e-467d-a3b8-9aa7c65b5eef.cpp @@ -0,0 +1,56 @@ +#include + + +char s[100001],*p=s; + +inline int E(int l) + +{ + + int x,y; + + if(l==2) + + if(*p=='(') + + p++,x=E(0),p++; + + else + + for(x=0;*p>='0'&&*p<='9';p++) + + x=x*10+*p-'0'; + + else + + for(x=E(l+1);(!l&&(*p=='+'||*p=='-'))||(l&&(*p=='*'||*p=='/'));x=y) + + if(!l) + + if(*p=='+') + + p++,y=x+E(l+1); + + else + + p++,y=x-E(l+1); + + else if(*p=='*') + + p++,y=x*E(l+1); + + else + + p++,y=x/E(l+1); + + return x; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin),freopen("evaluare.out","w",stdout),fgets(s,100001,stdin),printf("%d",E(0)); + +} diff --git a/eval/recursive/b434b123-5215-4a51-87dc-583de0c0066c.cpp b/eval/recursive/b434b123-5215-4a51-87dc-583de0c0066c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a83529192aa97c2d11a58f19d843ebea530d24d --- /dev/null +++ b/eval/recursive/b434b123-5215-4a51-87dc-583de0c0066c.cpp @@ -0,0 +1,133 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int i,n; + +char c[100005]; + + + +int expresie(); + +int factor(); + +int termen(); + + + +int main() + +{ + + fin >> c; + + n=strlen(c); + + i=0; + + fout << expresie(); + + + + return 0; + +} + + + +int expresie() + +{ + + int s=factor(); + + while (c[i]=='+' || c[i]=='-') + + { + + i++; + + if (c[i-1]=='+') s+=factor(); + + else s-=factor(); + + } + + return s; + +} + + + +int factor() + +{ + + int s=termen(); + + while (c[i]=='*' || c[i]=='/') + + { + + i++; + + if (c[i-1]=='*') s*=termen(); + + else s/=termen(); + + } + + return s; + +} + + + +int termen() + +{ + + int s=0; + + if (c[i]=='(') + + { + + i++; + + s=expresie(); + + i++; + + } + + else + + { + + while (c[i]>='0' && c[i]<='9') + + { + + s=s*10+(c[i]-'0'); + + i++; + + } + + } + + return s; + +} diff --git a/eval/recursive/b43623a9-d59b-4406-801a-7268b717898c.cpp b/eval/recursive/b43623a9-d59b-4406-801a-7268b717898c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd6fb4935f6de89519ce5ee6073a36f55778187a --- /dev/null +++ b/eval/recursive/b43623a9-d59b-4406-801a-7268b717898c.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int i; + +char s[100010]; + +int expresie(),termen(),factor(); + +int main() { + + fin>>s; + + i=0; + + fout<='0'&&s[i]<='9') + + r=r*10+s[i++]-'0'; + + } + + return r; + +} diff --git a/eval/recursive/b4e0f74e-fe2f-4147-8276-34ca21015052.cpp b/eval/recursive/b4e0f74e-fe2f-4147-8276-34ca21015052.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a745f91222a4aa5ae266f2d6ce3e69a0ecf3a811 --- /dev/null +++ b/eval/recursive/b4e0f74e-fe2f-4147-8276-34ca21015052.cpp @@ -0,0 +1,70 @@ +#include +#include + +std::ifstream f("evaluare.in"); +std::ofstream g("evaluare.out"); + +int i,expresie(),termen(),factor(); +std::string s; + +int expresie(){ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + i++; + r += termen(); + }else{ + i++; + r -= termen(); + } + } + + return r; +} + +int termen(){ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + i++; + r *= factor(); + }else{ + i++; + r /= factor(); + } + } + + return r; +} + +int factor(){ + + int r = 0; + + if(s[i] == '('){ + i++; + r = expresie(); + i++; + }else{ + + while(s[i] >= '0' && s[i] <= '9') + r = r * 10 + (s[i++] - '0'); + } + + return r; +} + +int main(){ + + f >> s; + + g << expresie(); + + return 0; +} diff --git a/eval/recursive/b5544482-1b4c-4916-8058-ee273881c58c.cpp b/eval/recursive/b5544482-1b4c-4916-8058-ee273881c58c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6e1b526288e44b137dd894c8e832c412cdb9006 --- /dev/null +++ b/eval/recursive/b5544482-1b4c-4916-8058-ee273881c58c.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N=100001; +int p; +char s[N]; + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum=termen(); + while(s[p]=='+' || s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*' || s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + semn=-semn; + p++; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0' && s[p]<='9') + { + val=val*10+(s[p++]-'0'); + } + return semn*val; +} + +int main() +{ + in>>s; + out< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int p; + +string s; + +int factor(); + +int termen(); + +int evaluare() + +{ + + int r=factor(); + + while(s[p]=='+' || s[p]=='-') + + { + + p++; + + if(s[p-1]=='+') r+=factor(); + + else r-=factor(); + + } + + return r; + +} + + + +int factor() + +{ + + int r=termen(); + + while(s[p]=='*' || s[p]=='/') + + { + + p++; + + if(s[p-1]=='*') r*=termen(); + + else r/=termen(); + + } + + return r; + +} + +int termen() + +{ + + int r=0; + + if(s[p]=='(') + + { + + p++; + + r=evaluare(); + + ++p; + + } + + else while(s[p]>='0'&& s[p]<='9') {r=r*10+(s[p]-'0'); p++; + + } + + return r; + +} + +int main() + +{ + + fin>>s; + + s+='#'; + + fout< + + +using namespace std; + +int i; + +char a[100005], *p=a; + +long long termen(); + +long long factor(); + +long long eval() + +{ + + long long r=termen(); + + while(*p=='+' || *p=='-') + + { + + switch(*p) + + { + + case('+'): + + p++; + + r=r+termen(); + + break; + + case('-'): + + p++; + + r=r-termen(); + + break; + + } + + } + + return r; + +} + +long long termen() + +{ + + long long r=factor(); + + while (*p=='*' || *p=='/') + + { + + switch(*p) + + { + + case'*': + + p++; + + r= r* factor(); + + break; + + case'/': + + p++; + + r = r/factor(); + + break; + + } + + } + + return r; + +} + +long long factor() + +{ + + long long r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + { + + while(*p>='0' && *p<='9') + + { + + r = r*10+*p-'0'; + + p++; + + } + + } + + return r; + + + +} + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> a; + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/b67d4291-7167-479f-aa99-b84adc4e67a6.cpp b/eval/recursive/b67d4291-7167-479f-aa99-b84adc4e67a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e34a77823671aebada73a3ddcf1d377d5aba774f --- /dev/null +++ b/eval/recursive/b67d4291-7167-479f-aa99-b84adc4e67a6.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int lg,i; + +char e[100005]; + + + +int formareNr(); + +int Expresie(); + +int Termen(); + +int Factor(); + + + +int FormareNr() + +{ int nr=0; + + while(e[i]>='0' && e[i]<='9' && i='0'&&e[i]<='9') + + { + + nr=nr*10+e[i]-'0'; + + i++; + + } + + return nr; + + } + +} + + + +int Termen() + +{ + + int f=Factor(); + + + + while(i>e; + + lg=strlen(e); + + fout< + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/b755db95-1daf-4eff-9b80-522022af4703.cpp b/eval/recursive/b755db95-1daf-4eff-9b80-522022af4703.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49656c4312712d78942351cf7998117c355ddc0b --- /dev/null +++ b/eval/recursive/b755db95-1daf-4eff-9b80-522022af4703.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +const int N = 100010; + +char s[N],*p; + +int suma(),termen(),factor(),numar(); + + + +int main() + +{ + + f>>s; + + p=s; + + g<='0'&&*p<='9') + + { + + ret=10*ret+*p-'0'; + + p++; + + } + + return ret; + +} diff --git a/eval/recursive/b94a223e-8db7-4ef1-b508-8cdca1059f95.cpp b/eval/recursive/b94a223e-8db7-4ef1-b508-8cdca1059f95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4ea9287e18b9782b6d6db16babe11dad07f06d0 --- /dev/null +++ b/eval/recursive/b94a223e-8db7-4ef1-b508-8cdca1059f95.cpp @@ -0,0 +1,154 @@ +#include +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +char s[100005]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + semn=-semn; + + p++; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10+s[p++]-'0'; + + } + + return semn*val; + +} + + + +int main() + +{ + + in>>s; + + out< +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +const int N = 100010; + +int p; + +char s[N]; + +int suma(),produs(),factor(),numar(); + +int main() + +{ + + f>>s; + + g< +#include + +using namespace std; + +ifstream in ("evaluare.in"); +ofstream out ("evaluare.out"); + +const int NM = 100001; +char s[NM] ; +int p ; + +int expresie(); +int factor(); +int termen(); + +int expresie() +{ + int sum = termen() ; + while ( s[p] == '+' || s[p] == '-' ) + { + if ( s[p] == '+' ) + { + p++ ; + sum += termen () ; + } + else + { + p++ ; + sum -= termen() ; + } + } + return sum ; +} + +int termen () +{ + int prod = factor () ; + while ( s[p] == '*' || s[p] == '/' ) + { + if ( s[p] == '*' ) + { + p++ ; + prod *= factor() ; + } + else + { + p++ ; + prod /= factor() ; + } + } + return prod ; +} + +int factor() +{ + int val = 0, semn = 1 ; + while ( s[p] == '-' ) + { + p++ ; + semn = -semn ; + } + if ( s[p] == '(' ) + { + p++ ; + val = expresie() ; + p++ ; + return semn * val ; + } + while ( s[p] >= '0' && s[p] <= '9' ) + { + val = val * 10 + (s[p] - '0' ) ; + p++ ; + } + return semn * val ; +} + +int main() +{ + in >> s; + out << expresie() ; + return 0; +} diff --git a/eval/recursive/bb6e3095-9a9c-42c0-8084-5af69b73c3db.cpp b/eval/recursive/bb6e3095-9a9c-42c0-8084-5af69b73c3db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9791decbf23bbdbae25615fdd5f8056f727ecaed --- /dev/null +++ b/eval/recursive/bb6e3095-9a9c-42c0-8084-5af69b73c3db.cpp @@ -0,0 +1,149 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100001]; + + + +long termen(); + +long factor(); + +int i; +long eval() + +{ + + long r; + + r= termen(); + + while (s[i]=='+' || s[i]=='-') + + + + switch(s[i]) + + { + + case '+': + + i++; + + r=r+termen(); + + break; + + case '-': + + i++; + + r=r-termen(); + + break; + + } + + return r; + +} +long termen() + +{ + + long r; + + r=factor(); + + while (s[i]=='*' || s[i]=='/') + + switch(s[i]) + + { + + case '*':i++; + + r=r*factor(); + + break; + + + + case '/': i++; + + r=r/factor(); + + break; + + } + + return r; + +} +long factor() + +{ + + long r; + + r=0; + + if(s[i]=='(') + + { + + i++; + + r=eval(); + + i++; + + } + + else + + while(s[i]>='0' && s[i]<='9') + + { + + r=r*10+s[i]-'0'; + + i++; + + } + +return r; + +} + + int main() + + { + + char x; + + i=0; + + while (fin>>x) + + { + + i++; + + s[i]=x; + + } + + + + i=1; + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +typedef long long ll; + + + +string ex; + +int idx; + + + +ll eval(); + +ll termen(); + +ll factor(); + + + +ll eval(){ + + ll ans = termen(); + + + + while(ex[idx] == '+' || ex[idx] == '-'){ + + if(ex[idx] == '+'){ + + idx++; + + ans += termen(); + + } else if(ex[idx] == '-'){ + + idx++; + + ans -= termen(); + + } + + } + + + + return ans; + +} + + + +ll termen(){ + + ll ans = factor(); + + + + while(ex[idx] == '*' || ex[idx] == '/'){ + + if(ex[idx] == '*'){ + + idx++; + + ans *= factor(); + + } else if(ex[idx] == '/'){ + + idx++; + + ans /= factor(); + + } + + } + + + + return ans; + +} + + + +ll factor(){ + + ll ans = 0; + + + + if(ex[idx] == '('){ + + idx++; + + ans = eval(); + + idx++; + + } else { + + while(ex[idx] >= '0' && ex[idx] <= '9'){ + + ans = ans * 10 + (ex[idx] - '0'); + + idx++; + + } + + } + + + + return ans; + +} + + + +int main() + +{ + + fin >> ex; + + fout << eval(); + + + + return 0; + +} diff --git a/eval/recursive/bba2291c-a708-4d0a-b714-7693c89bab06.cpp b/eval/recursive/bba2291c-a708-4d0a-b714-7693c89bab06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5fad21ce35b815f3a91095368ffd40918ef7417 --- /dev/null +++ b/eval/recursive/bba2291c-a708-4d0a-b714-7693c89bab06.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +char sir[100001]; + +int lungime; + +int compune_nr(int indice) + +{ + + int nr = 0; + + while(indice=st;i--) + + { + + if(sir[i] == ')') + + paranteze++; + + else + + if(sir[i] == '(') + + paranteze--; + + else + + if(sir[i] == '+' && paranteze == 0) + + return eval_expr(st,i-1) + eval_expr(i+1,dr); + + if(sir[i] == '-' && paranteze == 0) + + return eval_expr(st,i-1) - eval_expr(i+1,dr); + + } + + paranteze = 0; + + + + for(int i = dr;i>=st;i--) + + { + + if(sir[i] == ')') + + paranteze++; + + if(sir[i] == '(') + + paranteze--; + + if(sir[i] == '*' && paranteze == 0) + + return eval_expr(st,i-1) * eval_expr(i+1,dr); + + if(sir[i] == '/' && paranteze == 0) + + return eval_expr(st,i-1) / eval_expr(i+1,dr); + + } + + + + if(sir[st] == '(' && sir[dr] == ')') + + return eval_expr(st+1,dr-1); + + + + return compune_nr(st); + +} + +int main() + +{ + + f.getline(sir,100001); + + lungime = strlen(sir); + + g << eval_expr(0,lungime-1); + +} diff --git a/eval/recursive/be022d50-6da9-4a7a-9341-28587076f4b2.cpp b/eval/recursive/be022d50-6da9-4a7a-9341-28587076f4b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f38bc218871d548c2a47bfc89f100a7a68eca258 --- /dev/null +++ b/eval/recursive/be022d50-6da9-4a7a-9341-28587076f4b2.cpp @@ -0,0 +1,77 @@ +#include +#include +FILE *fin=fopen("evaluare.in", "r"); + + FILE *fout=fopen("evaluare.out", "w"); + + + +char op[4][4] = { "+-", "*/", "^", "" }; + + + + + +char S[100010], *p = S; + + + +int eval( int a, int b, char o ) { + + switch( o ) { + + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + } + +} + + + + + +int expr( int lev ) { + + int x, y; + + + + if( lev == 2 ) + + if( *p == '(' ) + + ++p, x = expr(0), ++p; + + else + + for( x = 0; *p >= '0' && *p <= '9'; ++p ) + + x = x * 10 + *p - '0'; + + else + + for( x = expr(lev+1); strchr(op[lev], *p); x = y ) + + y = eval( x, expr(lev+1), *p++ ); + + return x; + +} + + + +int main() { + + fgets(S, 100010, fin); + + fprintf( fout, "%d\n", expr(0) ); + + return 0; + +} diff --git a/eval/recursive/be6a567b-9b11-4697-b513-968efe7a8615.cpp b/eval/recursive/be6a567b-9b11-4697-b513-968efe7a8615.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2077a99b56de516e0189ba7838d65d82527425bb --- /dev/null +++ b/eval/recursive/be6a567b-9b11-4697-b513-968efe7a8615.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +using namespace std; + +ifstream fin( "evaluare.in" ); +ofstream fout( "evaluare.out" ); + +const int MaxL = 100001; + +char exp[MaxL + 1]; +int i; + +int eval(); +int term(); +int fact(); + +int eval() { + int res; + + res = term(); + while ( exp[i] == '+' || exp[i] == '-' ) { + ++i; + if ( exp[i - 1] == '+' ) { + res += term(); + } else { + res -= term(); + } + } + return res; +} +int term() { + int res; + + res = fact(); + while ( exp[i] == '*' || exp[i] == '/' ) { + ++i; + if ( exp[i - 1] == '*' ) { + res *= fact(); + } else { + res /= fact(); + } + } + return res; +} +int fact() { + int res = 0; + + if ( exp[i] == '(' ) { + ++i; + res = eval(); + ++i; + } else { + while ( isdigit( exp[i] ) ) { + res = res * 10 + exp[i] - '0'; + ++i; + } + } + return res; +} + +int main() { + fin >> exp; + fout << eval(); + fin.close(); + fout.close(); + return 0; +} diff --git a/eval/recursive/be8b628d-ce4f-4bf0-adfc-dbfffe229588.cpp b/eval/recursive/be8b628d-ce4f-4bf0-adfc-dbfffe229588.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82e97fb775b0681c223dee5dafeadbaa751463b5 --- /dev/null +++ b/eval/recursive/be8b628d-ce4f-4bf0-adfc-dbfffe229588.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +const int N = 1e5; + +char s[5 + N]; +int p; + +int expresie(); +int termen(); +int factor(); + +int expresie() { + int sum = termen(); + while(s[p] == '+' || s[p] == '-') { + if(s[p] == '+') { + p++; + sum += termen(); + } else { + p++; + sum -= termen(); + } + } + return sum; +} + +int termen(){ + int prod = factor(); + while(s[p] == '*' || s[p] == '/'){ + if(s[p] == '*'){ + p++; + prod *= factor(); + } else { + p++; + prod /= factor(); + } + } + return prod; +} + +int factor(){ + int val(0), semn(1); + while(s[p] == '-'){ + semn = -semn; + p++; + } + if(s[p] == '('){ + p++; + val = expresie(); + p++; + return semn * val; + } + while(s[p] >= '0' && s[p] <= '9') + val = val * 10 + (s[p++] - '0'); + return semn * val; +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + fgets(s, 3 + N, stdin); + printf("%d\n", expresie()); + return 0; +} diff --git a/eval/recursive/bf51b21a-e18a-45e4-9a8f-de0543dd42d0.cpp b/eval/recursive/bf51b21a-e18a-45e4-9a8f-de0543dd42d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67bd18fe403c93acee734cce5f6c7d263061db85 --- /dev/null +++ b/eval/recursive/bf51b21a-e18a-45e4-9a8f-de0543dd42d0.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +char last; + +int paranteza(); + +int inmultire(); + +int adunare(); + + + +int getint () { + + int ans = 0; + + while (isdigit(last)) { + + ans = (ans * 10 + last - '0'); + + fin.get(last); + + } + + return ans; + +} + + + +int paranteza () { + + int ans; + + if (last == '(') { + + fin.get(last); + + ans = adunare(); + + fin.get(last); + + } else ans = getint(); + + return ans; + +} + + + +int inmultire () { + + int ans = paranteza(); + + while (last == '*' || last == '/') { + + if (last == '*') { + + fin.get(last); + + ans *= paranteza(); + + } + + else { + + fin.get(last); + + ans /= paranteza(); + + } + + } + + return ans; + +} + + + +int adunare () { + + int ans = inmultire(); + + while (last == '+' || last == '-') { + + if (last == '+') { + + fin.get(last); + + ans += inmultire(); + + } else { + + fin.get(last); + + ans -= inmultire(); + + } + + } + + return ans; + +} + + + +int main () { + + fin.get(last); + + fout << adunare(); + + fin.close(); fout.close(); + + return 0; + +} diff --git a/eval/recursive/bf8791e6-1cf5-4c20-94cd-75c7e54ed0e8.cpp b/eval/recursive/bf8791e6-1cf5-4c20-94cd-75c7e54ed0e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..817b5cdafc35ec757d72b207f798e0203c16df23 --- /dev/null +++ b/eval/recursive/bf8791e6-1cf5-4c20-94cd-75c7e54ed0e8.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int i; + +char s[100100]; + +int eval(); + +int termen(); + +int factor(); + +int eval() + +{ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-') + + { if(s[i] == '+') + + { i++; + + r = r + termen(); + + } + + else + + { i++; + + r = r - termen(); + + } + + } + + return r; + +} + +int termen() + +{ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/') + + { if(s[i] == '*') + + { i++; + + r = r * factor(); + + } + + else + + { i++; + + r = r / factor(); + + } + + } + + return r; + +} + +int factor() + +{ + + int r = 0; + + if(s[i] == '(') + + { i++; + + r = eval(); + + i++; + + } + + else + + { while(s[i] >= '0' && s[i] <= '9') + + { r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} + +int main() + +{ + + f >> s; + + g << eval() << '\n'; + + return 0; + +} diff --git a/eval/recursive/c16590c4-173d-4f4c-8404-d7456b6e9601.cpp b/eval/recursive/c16590c4-173d-4f4c-8404-d7456b6e9601.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0f582f863e0759f36689e6aff93f525132e6dab --- /dev/null +++ b/eval/recursive/c16590c4-173d-4f4c-8404-d7456b6e9601.cpp @@ -0,0 +1,193 @@ +#include +#include + + + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +int n, pointer; + +char line[100005], curchar; + + + +int suma(int,char); + +int factor(int); + +int produs(int,char); + + + +void readnextchar() + +{ + + curchar = line[pointer]; + + pointer++; + +} + + + +int suma(int answer, char op) + +{ + + int r = produs(1,'*'); + + if(op == '+') + + answer+=r; + + else + + answer-=r; + + + + if(curchar == '+') + + { + + readnextchar(); + + return suma(answer, '+'); + + } + + else if(curchar == '-') + + { + + readnextchar(); + + return suma(answer, '-'); + + } + + else + + return answer; + +} + + + +int factor(int result) + +{ + + if(curchar == '(') + + { + + readnextchar(); + + result = suma(0,'+'); + + readnextchar(); + + return result; + + } + + else if('0' <= curchar && curchar <= '9') + + { + + result = result * 10 + (curchar - '0'); + + readnextchar(); + + return factor(result); + + } + + else + + return result; + +} + + + +int produs(int answer, char op) + +{ + + int r = factor(0); + + if(op == '*') + + answer *= r; + + else + + answer /= r; + + + + if(curchar == '*') + + { + + readnextchar(); + + return produs(answer, '*'); + + } + + else if(curchar == '/') + + { + + readnextchar(); + + return produs(answer, '/'); + + } + + else + + return answer; + +} + + + +int main() + +{ + + in.getline(line, 100005); + + + + readnextchar(); + + + + out << suma(0,'+'); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/eval/recursive/c17973a4-83a3-4707-b62d-dd2486305cf5.cpp b/eval/recursive/c17973a4-83a3-4707-b62d-dd2486305cf5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63286411845df8cd88abd595edf4c1408aef4c7e --- /dev/null +++ b/eval/recursive/c17973a4-83a3-4707-b62d-dd2486305cf5.cpp @@ -0,0 +1,74 @@ +#include + +std::ifstream f("evaluare.in"); +std::ofstream g("evaluare.out"); + +std::string s; +int i,expresie(),termen(),factor(); + +int expresie(){ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + i++; + r += termen(); + }else{ + i++; + r -= termen(); + } + } + + return r; +} + +int termen(){ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + i++; + r *= factor(); + }else{ + i++; + r /= factor(); + } + } + + return r; +} + +int factor(){ + + int r = 0; + + if(s[i] == '('){ + + i++; + r = expresie(); + i++; + + return r; + }else{ + + while(s[i] >= '0' && s[i] <= '9') + r = r * 10 + (s[i++] - '0'); + + return r; + } + + return r; +} + +int main(){ + + f >> s; + + g << expresie(); + + return 0; +} diff --git a/eval/recursive/c1e16170-80e6-44d7-86b6-745c1ef2ddfc.cpp b/eval/recursive/c1e16170-80e6-44d7-86b6-745c1ef2ddfc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3836ec0e794ae4bdd2f5fc4fcc0ed3471a43f56d --- /dev/null +++ b/eval/recursive/c1e16170-80e6-44d7-86b6-745c1ef2ddfc.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char *p, s[100005]; + +int eval(); + +int termen(); + +int factor(); + +int eval() + +{ + + int r=termen(); + + while(*p=='+' || *p=='-') + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(*p=='*' || *p=='/') + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0' && *p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} + +int main() + +{ + + f.getline(s,100000); + + p=s; + + int rez=eval(); + + g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int N = 100000; + + + +char s[N+1]; + + + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val = 0, semn = 1; + + while(s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if(s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while(isdigit(s[p])) + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + fin.getline(s,100001); + + fout << expresie(); + +} diff --git a/eval/recursive/c3b61db4-645d-44e1-8abb-bec5b5c3497d.cpp b/eval/recursive/c3b61db4-645d-44e1-8abb-bec5b5c3497d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07109db3bd1c9ed30d5f2d12140825d92a646946 --- /dev/null +++ b/eval/recursive/c3b61db4-645d-44e1-8abb-bec5b5c3497d.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; +ifstream fin ("evaluare.in"); +ofstream fout ("evaluare.out"); +char sir[100009];int n,p; +int termen () ; +int factor (); + +int eval () { + int r=termen(); + while(sir[p]=='+' || sir[p]=='-') { + if(sir[p]=='+') + ++p,r+=termen(); + else + ++p,r-=termen(); + } + return r; +} +int termen () { + int r=factor(); + while(sir[p]=='*' || sir[p]=='/') { + if(sir[p]=='*') + ++p,r*=factor(); + else + ++p,r/=factor(); + } + return r; +} +int factor () { + int r=0; + if(sir[p]=='(') { + ++p;r=eval(); + ++p; + } + else { + while(sir[p]>='0' && sir[p]<='9') + r=r*10+sir[p]-'0',++p; + } + return r; +} + +int main () { + fin.getline(sir+1,100005);p=1;n=strlen(sir+1); + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005]; + + + +int eval(char*& expr); + +int operand(char*& expr); + +int value(char*& expr); + +int pm(char*& expr); + +int od(char*& expr); + + + +int eval(char*& expr){ + + return pm(expr); + +} + + + +int pm(char*& expr){ + + int result = od(expr); + + while(expr[0]=='-' || expr[0]=='+'){ + + switch (expr[0]){ + + case '-': + + result-=od(++expr); + + break; + + case '+': + + result+=od(++expr); + + break; + + default: + + break; + + } + + } + + return result; + +} + + + +int od(char*& expr){ + + int result = operand(expr); + + while(expr[0]=='/' || expr[0]=='*'){ + + switch (expr[0]){ + + case '/': + + result/=operand(++expr); + + break; + + case '*': + + result*=operand(++expr); + + break; + + default: + + break; + + } + + } + + return result; + +} + + + +int value(char*& expr){ + + int n = 0; + + while(isdigit(expr[0])){ + + n = n*10 + (expr[0]-'0'); + + expr++; + + } + + return n; + +} + + + +int operand(char*& expr){ + + int result; + + if(expr[0]=='('){ + + result = eval(++expr); + + expr++; + + }else{ + + result = value(expr); + + } + + return result; + +} + + + +int main(){ + + fin.get(s, 100005); + + char* expr = s; + + fout< + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005]; + +int poz,n; + +int eval(); + +int factor(); + +int termen(); + +int eval() + +{ + + int rez=termen(); + + while (poz='0' && s[poz]<='9') + + { + + rez=rez*10+(s[poz]-'0'); + + poz++; + + } + + return rez; + +} + +int main() + +{ + + f.getline(s,100005); + + n=strlen(s); + + poz=0; + + g< +#include + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + + + +char s[N+1]; + + + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie () + +{ + + int sum = termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + +int termen () + +{ + + int prod = factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod *=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int semn = 1, val=0; + + while(s[p]=='-') + + { + + p++; + + semn = -semn; + + } + + if(s[p]=='(') + + { + + p++; + + val = expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val = val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in>>s; + + out< +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +string s; + +int i = 0; + +long long produs(); + +long long term(); + + + +long long eval() { + + long long r = produs(); + + while (s[i] == '+' || s[i] == '-') { + + if (s[i] == '+') { + + i++; + + r += produs(); + + } + + else if (s[i] == '-') { + + i++; + + r -= produs(); + + } + + } + + return r; + +} + +long long produs() { + + long long r = term(); + + while (s[i] == '*' || s[i] == '/') { + + if (s[i] == '*') { + + i++; + + r *= term(); + + } + + else if (s[i] == '/') { + + i++; + + r /= term(); + + } + + } + + return r; + +} + +long long term() { + + long long r = 0; + + if (s[i] == '(') { + + i++; + + r = eval(); + + i++; + + } + + else { + + while (s[i] >= '0' && s[i] <= '9') { + + r = r * 10 + (s[i] - '0'); + + i++; + + } + + } + + return r; + +} + +int main() { + + in >> s; + + out << eval(); + +} diff --git a/eval/recursive/cba28b67-6cd3-4b03-b8af-c3b226a5adf3.cpp b/eval/recursive/cba28b67-6cd3-4b03-b8af-c3b226a5adf3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dab74866cd24bc4483b67f3a601c074da9c9f9d6 --- /dev/null +++ b/eval/recursive/cba28b67-6cd3-4b03-b8af-c3b226a5adf3.cpp @@ -0,0 +1,127 @@ +#include +using namespace std; + +char s[100010]; + +int i; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int main() { + + ifstream fin ("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + fin>>s; + + i = 0; + + fout<= '0' && s[i] <= '9') { + + r = r*10 + s[i]-'0'; + + i++; + + } + + return r; + + } + +} diff --git a/eval/recursive/cbbd4602-7133-44d1-adec-19f3141d32ec.cpp b/eval/recursive/cbbd4602-7133-44d1-adec-19f3141d32ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ed96640b580296a3341e7f6cc7295e4f34b51a2 --- /dev/null +++ b/eval/recursive/cbbd4602-7133-44d1-adec-19f3141d32ec.cpp @@ -0,0 +1,129 @@ +#include +#include + + +using namespace std; + +const int NMAX = 100005; + +char s[NMAX]; + +char *p = s; + +int termen(); + +int factor(); + +int eval() { + + int r = termen(); + + while(*p == '+' || *p == '-') { + + switch(*p) { + + case '+': + + ++p; + + r += termen(); + + break; + + case '-': + + ++p; + + r -= termen(); + + break; + + } + + } + + return r; + +} + +int termen() { + + int r = factor(); + + while(*p == '*' || *p == '/') { + + switch(*p) { + + case '*': + + ++p; + + r *= factor(); + + break; + + case '/': + + ++p; + + r /= factor(); + + break; + + } + + } + + return r; + +} + +int factor() { + + int r = 0; + + if(*p == '(') { + + ++p; + + r = eval(); + + ++p; + + } else { + + while((*p ^ '0') < 10) { + + r = r * 10 + *p - '0'; + + ++p; + + } + + } + + return r; + +} + + + +int main() + +{ + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + int k = -1; + + char ch; + + while(scanf("%c", &ch) != -1 && ch != '\n') s[++k] = ch; + + printf("%d\n", eval()); + + return 0; + +} diff --git a/eval/recursive/cc2f4060-437e-4854-ba64-bdd136704474.cpp b/eval/recursive/cc2f4060-437e-4854-ba64-bdd136704474.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7daf176efbf858956c9499cda0eb8a0987fb1be --- /dev/null +++ b/eval/recursive/cc2f4060-437e-4854-ba64-bdd136704474.cpp @@ -0,0 +1,189 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int evaluate(char* &expr); + +int termen(char* &expr) ; + +int factor(char* &expr) ; + +int number(char* &expr) ; + + + +char s[100001]; + + + +int main() + +{ + + char *expr; + + + + fin >> s; + + expr = s; + + fout << evaluate(expr); + + return 0; + +} + + + +int evaluate(char* &expr) + +{ + + auto result = termen(expr); + + + + while (expr[0] == '+' || expr[0] == '-') + + { + + switch (expr[0]) + + { + + case '+': + + result += termen(++expr); + + break; + + case '-': + + result -= termen(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int termen(char* &expr) + +{ + + auto result = factor(expr); + + + + while (expr[0] == '*' || expr[0] == '/') + + { + + switch (expr[0]) + + { + + case '*': + + result *= factor(++expr); + + break; + + case '/': + + result /= factor(++expr); + + break; + + default: + + break; + + } + + } + + + + return result; + +} + + + +int factor(char* &expr) + +{ + + if (expr[0] == '(') + + { + + auto result = evaluate(++expr); + + expr++; + + return result; + + } + + else + + { + + return number(expr); + + } + +} + + + +int number(char* &expr) + +{ + + int nr = 0; + + + + while (isdigit(expr[0])) + + { + + nr = nr * 10 + (expr[0] - '0'); + + expr++; + + } + + + + return nr; + +} diff --git a/eval/recursive/cd07453e-95d8-4975-9fa4-eba77b938d60.cpp b/eval/recursive/cd07453e-95d8-4975-9fa4-eba77b938d60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bd728e52e7ec99e3348ea849a3e7d68c39ba29d --- /dev/null +++ b/eval/recursive/cd07453e-95d8-4975-9fa4-eba77b938d60.cpp @@ -0,0 +1,56 @@ +#include + + +char s[100001],*p=s; + +inline int E(int l) + +{ + + int x,y; + + if(l==2) + + if(*p=='(') + + p++,x=E(0),p++; + + else + + for(x=0;*p>='0'&&*p<='9';p++) + + x=x*10+*p-'0'; + + else + + for(x=E(l+1);(!l&&(*p=='+'||*p=='-'))||(l&&(*p=='*'||*p=='/'));x=y) + + if(!l) + + if(*p=='+') + + p++,y=x+E(l+1); + + else + + p++,y=x-E(l+1); + + else if(*p=='*') + + p++,y=x*E(l+1); + + else + + p++,y=x/E(l+1); + + return x; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin),freopen("evaluare.out","w",stdout),fgets(s,100001,stdin),printf("%d",E(0)); + +} diff --git a/eval/recursive/cd2e7d68-872a-40a1-af96-b43c1d913064.cpp b/eval/recursive/cd2e7d68-872a-40a1-af96-b43c1d913064.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e58cd49b29b7359955d4a930dd8b6105ab155b3 --- /dev/null +++ b/eval/recursive/cd2e7d68-872a-40a1-af96-b43c1d913064.cpp @@ -0,0 +1,122 @@ +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int E(),T(),F(),N();char c; + +int main() + +{ + + g<>c; + + int R=T(); + + while(c=='+'||c=='-') + + if(c=='+') + + { + + f>>c; + + R+=T(); + + } + + else + + { + + f>>c; + + R-=T(); + + } + + return R; + +} + +int T() + +{ + + int R=F(); + + while(c=='*'||c=='/') + + if(c=='*') + + { + + f>>c; + + R*=F(); + + } + + else + + { + + f>>c; + + R/=F(); + + } + + return R; + +} + +int F() + +{ + + if(isdigit(c)) + + return N(); + + int R=E(); + + f>>c; + + return R; + +} + +int N() + +{ + + int R=0; + + while(isdigit(c)) + + { + + R=10*R+c-'0'; + + if(!(f>>c))c=0;; + + } + + return R; + +} diff --git a/eval/recursive/cda670f0-83b6-4a87-920b-a6a0c6c24297.cpp b/eval/recursive/cda670f0-83b6-4a87-920b-a6a0c6c24297.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ffe8768dddb9e50330a5853cbc5d75acc3248f6 --- /dev/null +++ b/eval/recursive/cda670f0-83b6-4a87-920b-a6a0c6c24297.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +const int N = 100000; + + + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie(){ + + int sum=termen(); + + while(s[p]=='+' || s[p]=='-'){ + + if(s[p]=='+'){ + + p++; + + sum+=termen(); + + }else{ + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen(){ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/'){ + + if(s[p]=='*'){ + + p++; + + prod*=factor(); + + }else{ + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor(){ + + int semn=1, val=0; + + while(s[p]=='-'){ + + p++; + + semn=-semn; + + } + + if(s[p]=='('){ + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while (s[p]>='0' && s[p]<='9'){ + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + cin>>s; + + cout< +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],c; + +int poz; + + + +int adun(); + + + +int termen() + +{ + + int rez=0; + + if(c=='(') + + { + + poz++; + + c=s[poz]; + + rez=adun(); + + poz++; + + c=s[poz]; + + } + + else + + while(c>='0'&&c<='9') + + { + + rez=rez*10+(c-'0'); + + poz++; + + c=s[poz]; + + } + + return rez; + +} + + + +int prod() + +{ + + int rez=termen(); + + while(c=='*'||c=='/') + + { + + if(c=='*') + + { + + poz++; + + c=s[poz]; + + rez*=termen(); + + } + + else + + { + + poz++; + + c=s[poz]; + + rez/=termen(); + + } + + } + + return rez; + +} + + + +int adun() + +{ + + int rez=prod(); + + while(c=='+'||c=='-') + + { + + if(c=='+') + + { + + poz++; + + c=s[poz]; + + rez+=prod(); + + } + + else + + { + + poz++; + + c=s[poz]; + + rez-=prod(); + + } + + } + + return rez; + +} + + + +int main() + +{ + + fin.getline(s,100000); + + c=s[poz]; + + fout< +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char c[100001]; + +int i; + +int solve(); + +int mult(); + +int num(); + + + +int solve() + +{ + + int t=mult(); + + while(c[i]=='+' || c[i]=='-') + + { + + if(c[i]=='+') + + { + + i++; + + t+=mult(); + + } + + else + + { + + i++; + + t-=mult(); + + } + + } + + return t; + +} + +int mult() + +{ + + int t=num(); + + while(c[i]=='*'|| c[i]=='/') + + { + + if(c[i]=='*') + + { + + i++; + + t*=num(); + + } + + else + + { + + i++; + + t/=num(); + + } + + } + + return t; + +} + +int num() + +{ + + int t=0; + + if(c[i]=='(') + + { + + i++; + + t=solve(); + + i++; + + } + + else + + while(isdigit(c[i])) + + { + + t=t*10+(int)c[i++]-'0'; + + } + + return t; + +} + +int main() + +{ + + fin.getline(c,100001); + + fout< + + +using namespace std; + + + +int poz,l; + +char sir[100010]; + + + +int number() + +{ + + int s=0; + + for(;sir[poz]>='0' && sir[poz]<='9';poz++) s=s*10+sir[poz]-'0'; + + return s; + +} + + + +int solve() + +{ + + int ans=0,p=1; + + if(sir[poz]=='-') {p=-1;poz++;} + + while(poz +#include +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char x[100001]; +int i, lg; + +int factor(); +int produs(); +int suma(); + +int factor() +{ + int r; + if (x[i] == '(') + { + i++; + r = suma(); + i++; + } + else + { + + r = 0; + while (i> x; + lg = strlen(x); + fout << suma(); + return 0; +} diff --git a/eval/recursive/cf0a9ada-edc0-444b-a896-ce84743cba0f.cpp b/eval/recursive/cf0a9ada-edc0-444b-a896-ce84743cba0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..846311d755f2b433b2baeba561b587e2784292f9 --- /dev/null +++ b/eval/recursive/cf0a9ada-edc0-444b-a896-ce84743cba0f.cpp @@ -0,0 +1,141 @@ +#include +#include +using namespace std; + + + +char S[100010],*p=S; + +int eval2(); + +int calcul(); + + + +int eval1() { + + int r = eval2(); + + while (*p=='+'||*p=='-') + + { + + if (*p=='+') + + { + + p++; + + r+=eval2(); + + } + + else if (*p=='-') + + { + + p++; + + r-=eval2(); + + } + + } + + return r; + +} + + + +int eval2() + +{ + + int r = calcul(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + r*=calcul(); + + } + + else if (*p=='/') + + { + + p++; + + r/=calcul(); + + } + + } + + return r; + +} + + + +int calcul() { + + int r=0; + + if (*p=='(') + + { + + p++; + + r=eval1(); + + p++; + + } + + else + + { + + while (*p>='0'&&*p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + + } + + return r; + +} + + + +int main() + +{ + + ifstream f("evaluare.in"); + + ofstream fout("evaluare.out"); + + f.getline(S,100010); + + fout< +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +char s[100002]; + + + +char *t=s; + +int getnr() + +{ + + + + int nr=0; + + while(*t>='0' && *t<='9') + + { + + nr*=10; + + nr+=*t-'0'; + + ++t; + + } + + + + return nr; + +} + +int prod(); + +int parant(); + +int sum() + +{ + + int r=prod(); + + + + while(*t=='+' || *t=='-') + + { + + if(*t=='+') + + { + + ++t; + + r+=prod(); + + } + + + + else + + { + + ++t; + + r-=prod(); + + } + + + + } + + + + return r; + +} + +int prod() + +{ + + int r=parant(); + + + + while(*t=='*' || *t=='/') + + { + + if(*t=='*') + + { + + + + ++t; + + r*=parant(); + + } + + + + else { + + ++t; + + r/=parant(); + + } + + + + } + + + + return r; + +} + +int parant() + +{ + + int r=0; + + + + if(*t=='(') + + { + + ++t; + + r= sum(); + + ++t; + + } + + + + else r= getnr(); + + + + + + return r; + +} + + + +int main() + +{ + + f>>s; + + g< +#include + +using namespace std; + +string sir; + +int poz; + +int eval(); +int termen(); +int factor(); + +int expr() +{ + int val = termen(); + + while (poz < sir.size()) + { + if (sir[poz] == '+') + { + ++poz; + val += termen(); + } + else if (sir[poz] == '-') + { + ++poz; + val -= termen(); + } + else + break; + } + + return val; +} + +int termen() +{ + int val = factor(); + + while (poz < sir.size()) + { + if (sir[poz] == '*') + { + ++poz; + val *= factor(); + } + else if (sir[poz] == '/') + { + ++poz; + val /= factor(); + } + else + break; + } + + return val; +} + +int factor() +{ + int val = 0; + + if (sir[poz] == '(') + { + ++poz; + val = expr(); + ++poz; + } + else + { + while (poz < sir.size() && '0' <= sir[poz] && sir[poz] <= '9') + { + val = val * 10 + (sir[poz] - '0'); + ++poz; + } + } + + return val; +} + +int main() +{ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + + cin >> sir; + + cout << expr() << '\n'; + + return 0; +} diff --git a/eval/recursive/d1fe869e-9314-4468-a91e-b3c83c78cfc9.cpp b/eval/recursive/d1fe869e-9314-4468-a91e-b3c83c78cfc9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73a05f738bac6bd5d67ec71ca7bf17a75757bd40 --- /dev/null +++ b/eval/recursive/d1fe869e-9314-4468-a91e-b3c83c78cfc9.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char e[100001]; + +int i,lg,lgp; + +int Expresie(); + +int Termen(); + +int Factor(); + +int main() + +{ + + fin.getline(e,100001); + + fout<='0'&&e[i]<='9') + + val=val*10+(e[i++]-'0'); + + return semn*val; + +} diff --git a/eval/recursive/d2a03f1e-08d3-4152-90d8-478c90ad72de.cpp b/eval/recursive/d2a03f1e-08d3-4152-90d8-478c90ad72de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..108ce8c4d7f4629749ce91fabc08b3c01181758c --- /dev/null +++ b/eval/recursive/d2a03f1e-08d3-4152-90d8-478c90ad72de.cpp @@ -0,0 +1,65 @@ +#include + + +char expr[100000], *p = expr; + +long termen(); +long factor(); + +long eval() { + long r = termen(); + while (*p == '+' || *p == '-') { + switch (*p) { + case '+': + ++p; + r += termen(); + break; + case '-': + ++p; + r -= termen(); + break; + } + } + + return r; +} + +long termen() { + long r = factor(); + while (*p == '*' || *p == '/') { + switch (*p) { + case '*': + ++p; + r *= factor(); + break; + case '/': + ++p; + r /= factor(); + break; + } + } + + return r; +} + +long factor() { + long r = 0; + if (*p == '(') { + ++p; + r = eval(); + ++p; + } else { + while ('0' <= *p && *p <= '9') { + r = (r << 3) + (r << 1) + *p - '0'; + ++p; + } + } + + return r; +} + +int main() { + fgets(expr, 100000, fopen("evaluare.in", "r")); + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + return 0; +} diff --git a/eval/recursive/d2eff854-bdb0-4192-8f90-d09264f8aae1.cpp b/eval/recursive/d2eff854-bdb0-4192-8f90-d09264f8aae1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..094646b4f8cd57d9ecf3b2ecc285e2c1293e22ed --- /dev/null +++ b/eval/recursive/d2eff854-bdb0-4192-8f90-d09264f8aae1.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +int i; +string s; + +int op1(); +int op2(); +int op3(); + +int op1() { + int ans = op2(); + while (s[i] == '+' || s[i] == '-') { + if (s[i] == '+') { + i++; + ans += op2(); + } + else { + i++; + ans -= op2(); + } + } + return ans; +} + +int op2() { + int ans = op3(); + while (s[i] == '*' || s[i] == '/') { + if (s[i] == '*') { + i++; + ans *= op3(); + } else { + i++; + ans /= op3(); + } + } + return ans; +} + +int op3() { + int ans = 0; + if (s[i] == '(') { + i++; + ans = op1(); + i++; + } else { + while (isdigit(s[i]) && i > s; + cout << op1() << '\n'; + return 0; +} diff --git a/eval/recursive/d31236f4-c53a-4f04-8f71-4815d7cb769a.cpp b/eval/recursive/d31236f4-c53a-4f04-8f71-4815d7cb769a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a745f91222a4aa5ae266f2d6ce3e69a0ecf3a811 --- /dev/null +++ b/eval/recursive/d31236f4-c53a-4f04-8f71-4815d7cb769a.cpp @@ -0,0 +1,70 @@ +#include +#include + +std::ifstream f("evaluare.in"); +std::ofstream g("evaluare.out"); + +int i,expresie(),termen(),factor(); +std::string s; + +int expresie(){ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + i++; + r += termen(); + }else{ + i++; + r -= termen(); + } + } + + return r; +} + +int termen(){ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + i++; + r *= factor(); + }else{ + i++; + r /= factor(); + } + } + + return r; +} + +int factor(){ + + int r = 0; + + if(s[i] == '('){ + i++; + r = expresie(); + i++; + }else{ + + while(s[i] >= '0' && s[i] <= '9') + r = r * 10 + (s[i++] - '0'); + } + + return r; +} + +int main(){ + + f >> s; + + g << expresie(); + + return 0; +} diff --git a/eval/recursive/d3dc51f1-0e7e-4fd1-b38f-40c89566a1dc.cpp b/eval/recursive/d3dc51f1-0e7e-4fd1-b38f-40c89566a1dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68b7490d73ea4750dd6918d17b906332c45368b8 --- /dev/null +++ b/eval/recursive/d3dc51f1-0e7e-4fd1-b38f-40c89566a1dc.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); + + + + + + + +string s; + +int i; + + + +int f1(); + +int f2(); + +int f3(); + + + +int f1() { + + int ans = f2(); + + while (s[i] == '+' || s[i] == '-') { + + if (s[i] == '+') { + + i++; + + ans += f2(); + + } + + else { + + i++; + + ans -= f2(); + + } + + } + + return ans; + +} + + + +int f2() { + + int ans = f3(); + + while (s[i] == '*' || s[i] == '/') { + + if (s[i] == '*') { + + i++; + + ans *= f3(); + + } + + else { + + i++; + + ans /= f3(); + + } + + } + + return ans; + +} + + + +int f3() { + + int ans = 0; + + if (s[i] == '(') { + + i++; + + ans = f1(); + + i++; + + } + + else { + + while (i < s.size() && '0' <= s[i] && s[i] <= '9') { + + ans *= 10; + + ans += s[i] - '0'; + + i++; + + } + + } + + return ans; + +} + + + + + +int main() { + + + + cin >> s; + + cout << f1() << '\n'; + + + + return 0; + +} diff --git a/eval/recursive/d546b333-7313-42e1-817c-8be9da6c716f.cpp b/eval/recursive/d546b333-7313-42e1-817c-8be9da6c716f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..313cd6c768d156853e12c95ea7159f716757a389 --- /dev/null +++ b/eval/recursive/d546b333-7313-42e1-817c-8be9da6c716f.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + +string s; + +int p = 0; + +int eval(), factor(), nr(), termen(); + +int nr () + +{ + + int rez = 0; + + while (isdigit(s[p])) + + rez = rez * 10 + s[p++]-'0'; + + return rez; + +} + + + +int factor () + +{ + + if (s[p]=='(') + + { + + p++; + + int k = eval(); + + p++; + + return k; + + } + + else + + return nr(); + +} + + + +int termen () + +{ + + int r = factor (); + + while (s[p]=='*' || s[p]=='/') + + { + + if (s[p]=='*') + + p++,r *= factor(); + + else + + p++,r /= factor(); + + } + + return r; + +} + + + +int eval () + +{ + + int r = termen(); + + while (s[p]=='+' || s[p]=='-') + + { + + if (s[p]=='+') + + p++, r += termen(); + + else + + p++, r -= termen(); + + } + + return r; + +} + +int main () + +{ + + in>>s; + + out< + +using namespace std; + +ifstream fin( "evaluare.in" ); +ofstream fout( "evaluare.out" ); + +const int N = 100000; +int p; +char s[N + 1]; + +int expresie(); +int termen(); +int factor(); +int expresie(){ + int sum = termen(); + while( s[p] == '+' || s[p] == '-' ){ + if( s[p] == '+'){ + ++p; + sum += termen(); + } + else{ + ++p; + sum -= termen(); + } + } + return sum; +} + +int termen(){ + int prod = factor(); + while( s[p] == '*' || s[p] == '/' ){ + if( s[p] == '*' ){ + ++p; + prod *= factor(); + } + else{ + ++p; + prod /= factor(); + } + } + return prod; +} + +int factor(){ + int val = 0, semn = 1; + while( s[p] == '-' ){ + semn = -semn; + ++p; + } + if( s[p] == '(' ){ + ++p; + val = expresie(); + ++p; + return semn * val; + } + while( s[p] >= '0' && s[p] <= '9') + val = val * 10 + (s[p++] - '0'); + return semn * val; +} + +int main() { + fin.getline(s, N); + fout << expresie(); + return 0; +} diff --git a/eval/recursive/d8628877-508f-46b5-ac45-2e9cf853528c.cpp b/eval/recursive/d8628877-508f-46b5-ac45-2e9cf853528c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d3c23219985f86c9049a084a19d9ec9e0c64f96 --- /dev/null +++ b/eval/recursive/d8628877-508f-46b5-ac45-2e9cf853528c.cpp @@ -0,0 +1,87 @@ +#include + + +using namespace std; +string s; + + + +int eval2(int &i); + +int eval1(int &i); + +int eval(int &i) { + + int nr = eval1(i); + + while(s[i] == '+' or s[i] == '-') + + nr += s[i] == '+' ? eval1(++i) : -eval1(++i); + + return nr; + +} + +int eval1(int &i) { + + int nr = eval2(i); + + while(s[i] == '*' or s[i] == '/') + + nr = s[i] == '*' ? nr * eval2(++i) : nr / eval2(++i); + + return nr; + +} + +int eval2(int &i) { + + int nr = 0; + + if (s[i] == '(') { + + nr = eval(++i); + + i++; + + } + + else + + while(s[i] and strchr("0123456789", s[i])) + + nr = nr * 10 + s[i++] - '0'; + + return nr; + +} + +int main() { + + + + freopen("evaluare.in", "r", stdin ); + + freopen("evaluare.out", "w", stdout); + + + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + + + cin >> s; + + int k = 0; + + cout << eval(k); + + + + return 0; + + + +} diff --git a/eval/recursive/d9132922-3bc1-48e3-bb2f-841658cb1860.cpp b/eval/recursive/d9132922-3bc1-48e3-bb2f-841658cb1860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5b251a504c078de94348cca98a3ef356207b93e --- /dev/null +++ b/eval/recursive/d9132922-3bc1-48e3-bb2f-841658cb1860.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const long MAX = 100010; +char S[MAX], *p=S; + +long termen(); +long factor(); + +long eval(){ + long rez = termen(); + while( *p == '+' || *p == '-'){ + switch( *p ){ + case '+': + ++p; + rez += termen(); + break; + case '-': + ++p; + rez -= termen(); + break; + } + } + return rez; +} + +long factor(){ + long rez = 0; + if( *p == '('){ + ++p; + rez = eval(); + ++p; + }else{ + while(isdigit(*p)){ + rez = rez * 10 + *p - '0'; + ++p; + } + } + return rez; +} + +long termen(){ + long rez = factor(); + while( *p == '*' || *p == '/'){ + switch( *p ){ + case '*': + ++p; + rez *= factor(); + break; + case '/': + ++p; + rez /= factor(); + break; + } + } + return rez; +} + + +int main(){ + in >> S; + out << eval(); + return 0; +} diff --git a/eval/recursive/d99f6425-da30-41df-b3d7-485e3e7316cd.cpp b/eval/recursive/d99f6425-da30-41df-b3d7-485e3e7316cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9beb52db7ed273af784826986f0d02322e706e4c --- /dev/null +++ b/eval/recursive/d99f6425-da30-41df-b3d7-485e3e7316cd.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +char a[100001]; + +int x; + +int expresie(); + +int termen(); + +int evaluare(); + +int main() + +{ in>>a; + + out<='0' && a[x]<='9') + + k=k*10+a[x]-'0',x++; + + } + + return k; + +} diff --git a/eval/recursive/d9d831a0-456c-4017-bde8-3aa79b01ddd1.cpp b/eval/recursive/d9d831a0-456c-4017-bde8-3aa79b01ddd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5a00aff25e2de4243171ec33ffcb27030a7e6bc --- /dev/null +++ b/eval/recursive/d9d831a0-456c-4017-bde8-3aa79b01ddd1.cpp @@ -0,0 +1,70 @@ +#include +#include + +using namespace std; + +int DfsAddSub(const string &str, size_t &pos); + +int Number(const string &str, size_t &pos) +{ + if (str[pos] == '(') { + pos += 1; + auto num = DfsAddSub(str, pos); + pos += 1; + return num; + } + + int num = 0; + while (pos < str.size() && isdigit(str[pos])) { + num = num * 10 + (str[pos] - '0'); + pos += 1; + } + return num; +} + +int DfsMultDiv(const string &str, size_t &pos) +{ + int res = Number(str, pos); + while (pos < str.size() && (str[pos] == '*' || str[pos] == '/')) { + char sign = str[pos]; + pos += 1; + + if (sign == '*') { + res *= Number(str, pos); + } else { + res /= Number(str, pos); + } + } + return res; +} + +int DfsAddSub(const string &str, size_t &pos) +{ + int res = DfsMultDiv(str, pos); + while (pos < str.size() && (str[pos] == '+' || str[pos] == '-')) { + int sign = (str[pos] == '+' ? 1 : -1); + pos += 1; + res += sign * DfsMultDiv(str, pos); + } + return res; +} + +int Evaluate(const string &str) +{ + size_t pos = 0; + return DfsAddSub(str, pos); +} + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string str; + getline(fin, str); + + auto res = Evaluate(str); + fout << res << "\n"; + + return 0; +} diff --git a/eval/recursive/dae26679-b28a-4548-a32d-134a9ced00d6.cpp b/eval/recursive/dae26679-b28a-4548-a32d-134a9ced00d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7416562f49f6961fc21b2d7ae417017a7e891d7 --- /dev/null +++ b/eval/recursive/dae26679-b28a-4548-a32d-134a9ced00d6.cpp @@ -0,0 +1,78 @@ +#include +#include + +using namespace std; +const int N=100001; +char s[N]; +int p; +int expresie(); +int termen(); +int factor(); +ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); +int expresie() +{ + int sum=termen(); + while(s[p]=='+' || s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod=factor(); + while(s[p]=='*' || s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(s[p]>='0' && s[p]<='9') + { + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} + +int main() +{ + cin.getline(s,N); + cout< + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char s[100010]; + +int n,i; + +int expresie(); + +int termen(); + +int factor(); + +int main (){ + + + + fin>>s+1; + + n = strlen (s+1); + + i = 1; + + fout<= '0' && s[i] <= '9'){ + + r = r*10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} diff --git a/eval/recursive/dd3212de-b447-47a2-aeb1-69b87b70e476.cpp b/eval/recursive/dd3212de-b447-47a2-aeb1-69b87b70e476.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d5a99333461007d3c9f820f47650460e646b28a --- /dev/null +++ b/eval/recursive/dd3212de-b447-47a2-aeb1-69b87b70e476.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +char expr[100001]; + + + +int evaluate(char *& expr); + +int ori_div(char *& expr); + +int plus_minus(char *& expr); + +int operand(char *& expr); + +int value(char *& expr); + + + +int main() + +{ + + in.get(expr, 1000001); + + char *p = expr; + + out << evaluate(p); + + return 0; + +} + + + +int evaluate(char *& expr) + +{ + + return + + plus_minus(expr); + +} + + + +int ori_div(char *& expr) + +{ + + int result = operand(expr); + + while(expr[0] == '/' || expr[0] == '*') + + { + + switch(expr[0]) + + { + + case '*': + + result = result * operand(++ expr); + + break; + + case '/': + + result = result / operand(++ expr); + + break; + + } + + } + + return + + result; + +} + + + +int plus_minus(char *& expr) + +{ + + int result = ori_div(expr);; + + while(expr[0] == '+' || expr[0] == '-') + + { + + switch(expr[0]) + + { + + case '+': + + result += ori_div(++ expr); + + break; + + case '-': + + result -= ori_div(++ expr); + + break; + + } + + } + + return + + result; + +} + + + +int operand(char *& expr) + +{ + + int result; + + if(expr[0] == '(') + + { + + result = evaluate(++ expr); + + ++ expr; + + } + + else + + result = value(expr); + + return + + result; + +} + + + +int value(char *& expr) + +{ + + int nr = 0; + + while(isdigit(expr[0])) + + { + + nr = (expr[0] - '0') + nr * 10; + + ++ expr; + + } + + return nr; + +} diff --git a/eval/recursive/de4d4699-c05f-4580-9ee6-beffc383a04c.cpp b/eval/recursive/de4d4699-c05f-4580-9ee6-beffc383a04c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35687ca1dc2747e6db07415f42b3fec1716b4fd8 --- /dev/null +++ b/eval/recursive/de4d4699-c05f-4580-9ee6-beffc383a04c.cpp @@ -0,0 +1,160 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100010]; + +int i; + + + +int expresie(); + +int termen(); + +int factor(); + + + + + +int expresie() + +{ + + int r = termen(); + + while(s[i] == '+' || s[i] == '-') + + if(s[i] == '+') + + { + + i++; + + r += termen(); + + } + + else + + { + + i++; + + r -= termen(); + + } + + + + return r; + +} + + + + + +int termen() + +{ + + int r = factor(); + + while(s[i] == '*' || s[i] == '/') + + if(s[i] == '*') + + { + + i++; + + r *= factor(); + + } + + else + + { + + i++; + + r /= factor(); + + } + + + + return r; + +} +int factor() + +{ + + int r; + + if(s[i]=='(') + + { + + i++; + + r=expresie(); + + i++; + + } + + else + + { + + r=0; + + while(s[i]>='0' && s[i]<='9') + + { + + r=r*10+s[i]-'0'; + + i++; + + } + + + + } + + return r; + +} + + + + + + + +int main() + +{ + + fin >> s; + + fout << expresie(); + + return 0; + +} diff --git a/eval/recursive/ded51cf5-dbdd-404a-8727-0e7a259cb31b.cpp b/eval/recursive/ded51cf5-dbdd-404a-8727-0e7a259cb31b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c8cf2a842926f981c424c20b66f48afc1b8e22c --- /dev/null +++ b/eval/recursive/ded51cf5-dbdd-404a-8727-0e7a259cb31b.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],*p=s; + + + +int eval2(); + +int eval1(); + +int eval() + +{ + + int r=eval1(); + + while(*p=='+' or *p=='-') + + if(*p=='+') + + p++, r+=eval1(); + + else + + p++, r-=eval1(); + + return r; + +} + +int eval1() + +{ + + int r=eval2(); + + while(*p=='*' or *p=='/') + + if(*p=='*') + + p++, r*=eval2(); + + else + + p++, r/=eval2(); + + return r; + +} + +int eval2() + +{ + + int r=0; + + if(*p=='(') + + p++, r=eval(), p++; + + else + + while('0'<=*p and *p<='9') + + r=r*10+*p-'0', p++; + + return r; + +} + +int main() + +{ + + + + fin >> s; + + fout << eval(); + + + + return 0; + + + +} diff --git a/eval/recursive/e03d42d0-c3c4-451c-b39e-2ce7f4f76404.cpp b/eval/recursive/e03d42d0-c3c4-451c-b39e-2ce7f4f76404.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d66cc9ca4c45ca409810576bb277be11249af3ef --- /dev/null +++ b/eval/recursive/e03d42d0-c3c4-451c-b39e-2ce7f4f76404.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +const int N=200001; +char s[N]; +int p = 0; + +ifstream fin ("evaluare.in"); +ofstream fout ("evaluare.out"); + +int expresie(); +int termen(); +int factor(); + +int expresie() +{ + int sum = termen(); + while( s[p] == '+' || s[p] == '-' ) + { + if( s[p] == '+') + { + p++; + sum+=termen(); + } + else + { + p++; + sum-=termen(); + } + } + return sum; +} + +int termen() +{ + int prod = factor(); + while( s[p] == '*' || s[p] == '/') + { + if( s[p] == '*') + { + p++; + prod*=factor(); + } + else + { + p++; + prod/=factor(); + } + } + return prod; +} + +int factor() +{ + int val=0,semn=1; + while( s[p] == '-' ) + { + p++; + semn=-semn; + } + if( s[p] == '(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while( s[p]>='0' && s[p]<='9') + { + val=val*10+s[p++]-'0'; + } + return semn*val; +} + +int main() +{ + fin.getline(s,200001); + fout< +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +const int NMax = 100001; + +char x[NMax], *p = x; + + + +long termen(); + +long factor(); + + + +long eval() + +{ + + long r = termen(); + + while(*p == '+' || *p == '-') + + { + + switch(*p) + + { + + case '+': + + ++p; + + r+=termen(); + + break; + + case '-': + + ++p; + + r-=termen(); + + break; + + + + } + + } + + return r; + +} + + + +long termen() + +{ + + long r = factor(); + + while(*p =='*' || *p =='/') + + { + + switch(*p) + + { + + case '*': + + ++p; + + r*=factor(); + + break; + + case '/': + + ++p; + + r/=factor(); + + break; + + } + + } + + return r; + +} + + + +long factor() + +{ + + long r = 0; + + if(*p =='(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else + + { + + while(*p >= '0' && *p <= '9') + + { + + r = r * 10 + *p - '0'; + + ++p; + + } + + } + +return r; + +} + + + +int main() + +{ + + in >> x; + + out << eval(); + + return 0; + +} diff --git a/eval/recursive/e23692a5-df8a-4dd2-8313-b479e55d54cc.cpp b/eval/recursive/e23692a5-df8a-4dd2-8313-b479e55d54cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a62bc9e1cb7923e26b252220d664795fe150f3c --- /dev/null +++ b/eval/recursive/e23692a5-df8a-4dd2-8313-b479e55d54cc.cpp @@ -0,0 +1,161 @@ +#include +#include + + +using namespace std; + + + + + +const int N=100000; + + + +char s[N+1]; + + + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+' || s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*' || s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int semn=1,val=0;; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn * val; + + } + + while(s[p]>='0' && s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn * val; + +} + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin>>s; + + fout< +#include + +using namespace std; + +string s; +int i; + +long termen(); +long factor(); + +long eval() { + long r = termen(); + while(s[i] == '+' || s[i] == '-') { + if(s[i] == '+') { + i++; + r += termen(); + } + else if(s[i] == '-') { + i++; + r -= termen(); + } + } + return r; +} + +long termen() { + long r = factor(); + while(s[i] == '*' || s[i] == '/') { + if(s[i] == '*') { + i++; + r *= factor(); + } + else if(s[i] == '/') { + i++; + r /= factor(); + } + } + return r; +} + +long factor() { + long r = 0; + if(s[i] == '(') { + i++; + r = eval(); + i++; + } + else + while(isdigit(s[i])) + r = r * 10 + (s[i++] - '0'); + return r; +} + +int main() { + ifstream f("evaluare.in"); + ofstream g("evaluare.out"); + getline(f, s); + + g << eval() << '\n'; + f.close(); + g.close(); + return 0; +} diff --git a/eval/recursive/e2c410b7-c517-4fae-b4b5-7730672e6114.cpp b/eval/recursive/e2c410b7-c517-4fae-b4b5-7730672e6114.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72f6686e62a1aa66ae313340bfbbf4fcbe37ccf9 --- /dev/null +++ b/eval/recursive/e2c410b7-c517-4fae-b4b5-7730672e6114.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int n,i; + +char s[100005]; + +long fact(); + +long termen(); + +long eval() + +{ + + long r=termen(); + + while(s[i]=='+'||s[i]=='-') + + { + + + + if(s[i]=='+') {i++;r+=termen();} + + else {i++;r-=termen();} + + } + + return r; + +} + +long termen() + +{ + + long r=fact(); + + while(s[i]=='*'||s[i]=='/') + + { + + if(s[i]=='*') {i++;r=r*fact();} + + else {i++;r=r/fact();} + + } + + return r; + +} + +long fact() + +{ + + long r=0; + + if(s[i]=='(') {i++;r=eval();i++;} + + else + + { + + while(s[i]>='0'&&s[i]<='9') + + { + + r=r*10+s[i]-'0'; + + i++; + + } + + } + + return r; + +} + +int main() + +{ + + f>>s; + + g< +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N=100001; + + + +char s[N]; + + + +int p; + +int expresie(); + +int termen(); + +int factor(); + + + +int expresie() + +{ + + int sum=termen(); + + while (s[p] == '+' || s[p] == '-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + } + + + + else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + + + +int termen() + +{ + + int prod=factor(); + + while(s[p]== '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + + + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + p++; + + semn=-semn; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while('0'<=s[p]&&s[p]<='9') + + { + + val=val*10+(s[p]-'0'); + + p++; + + } + + return semn*val; + +} + + + +int main() + +{ + + in>>s; + + out< +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005]; + +int i; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int main() + +{ + + fin.getline(s, 100005); + + fout<='0' && s[i]<='9') { + + r = r*10 + (s[i] - '0'); + + i++; + + } + + } + + + + return r; + +} diff --git a/eval/recursive/e3f034ea-910e-4237-aac5-b52b4025f9e9.cpp b/eval/recursive/e3f034ea-910e-4237-aac5-b52b4025f9e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89fac44d382c7b73d3d2a4a90e9f30f764a64407 --- /dev/null +++ b/eval/recursive/e3f034ea-910e-4237-aac5-b52b4025f9e9.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int NMax = 1e5; + + + +char S[NMax + 5], *p = S; + + + +int Eval(); + + + +int Factor() + +{ + + int Sol = 0; + + + + if(*p == '(') + + ++p, Sol = Eval(), p++; + + else + + { + + while(isdigit(*p)) + + Sol = Sol * 10 + *p - '0', p++; + + } + + return Sol; + +} + + + +int Termen() + +{ + + int Sol = Factor(); + + + + while(*p == '*' || *p == '/') + + { + + if(*p == '*') + + p++, Sol *= Factor(); + + else + + p++, Sol /= Factor(); + + } + + return Sol; + +} + + + +int Eval() + +{ + + int Sol = Termen(); + + + + while(*p == '+' || *p == '-') + + { + + if(*p == '+') + + p++, Sol += Termen(); + + else + + p++, Sol -= Termen(); + + } + + return Sol; + +} + + + +int main() + +{ + + fin >> S; + + + + fout << Eval() << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/eval/recursive/e3f875b2-b0d9-484c-95bb-b188fb4b2640.cpp b/eval/recursive/e3f875b2-b0d9-484c-95bb-b188fb4b2640.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20f46f324c7297ef12b7aa8f6334af2b3c6c6706 --- /dev/null +++ b/eval/recursive/e3f875b2-b0d9-484c-95bb-b188fb4b2640.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +const int N=100001; +char s[N]; +int p; + +int expresie(); +int termen(); +int factor(); + +int expresie() { + int sum=termen(); + while(s[p]=='+'||s[p]=='-') + { + if(s[p]=='+') + { + p++; + sum+=termen(); + } + else { + p++; + sum-=termen(); + } + } + return sum; +} +int termen() +{ + int prod=factor(); + while(s[p]=='*'||s[p]=='/') + { + if(s[p]=='*') + { + p++; + prod*=factor(); + } + else { + p++; + prod/=factor(); + } + } + return prod; +} +int factor() +{ + int val=0,semn=1; + while(s[p]=='-') + { + p++; + semn=-semn; + } + if(s[p]=='(') + { + p++; + val=expresie(); + p++; + return semn*val; + } + while(isdigit(s[p])) + { + val=val*10+(s[p]-'0'); + p++; + } + return semn*val; +} + +int main() +{ + in>>s; + out< +#include + + + + +using namespace std; + + + +char expr[1000100], *p = expr; + + + +long expresie(); + +long termen(); + + + +long eval() + +{ + + long r = termen(); + + + + while (*p == '+' || *p == '-') + + { + + + + switch (*p) + + { + + case '+': ++p; + + r = r + termen(); + + break; + + + + case '-': ++p; + + r = r - termen(); + + break; + + + + } + + + + + + } + + return r; + +} + + + +long termen() + +{ + + + + long r = expresie(); + + + + while (*p == '*' || *p == '/') + + { + + switch (*p) + + { + + case '*': ++p; + + r = r * expresie(); + + break; + + + + case '/': ++p; + + r = r / expresie(); + + break; + + } + + + + + + } + + + + return r; + + + +} + + + +long expresie() + +{ + + + + long r = 0; + + + + if (*p == '(') + + { + + ++p; + + r = eval(); + + ++p; + + } + + else + + { + + while (*p >= '0' && *p <= '9') + + { + + r = r * 10 + *p - '0'; + + p++; + + + + } + + + + } + + + + return r; + + + + + +} +void citire() + +{ + + ifstream in("evaluare.in"); + + + + in.getline(expr, 1000100); + + + + + + + + in.close(); + + + +} + + + +int main() + +{ + + citire(); + + ofstream out("evaluare.out"); + + + + out << eval(); + + + + + + + + out.close(); + + + + return 0; + +} diff --git a/eval/recursive/e56ca531-baf4-4d46-907a-8678ae54f3cf.cpp b/eval/recursive/e56ca531-baf4-4d46-907a-8678ae54f3cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82d19f21a970f0cd66ab478c84ff942fc3cb02ec --- /dev/null +++ b/eval/recursive/e56ca531-baf4-4d46-907a-8678ae54f3cf.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + + + +int termen(); + +int factor(); + +int expresie(); + + + +const int N=(int)1e5+7; + +char s[N]; + +int j; + + + +int expresie() + +{ + + int r=termen(); + + while(s[j]=='+' || s[j]=='-') + + { + + j++; + + if(s[j-1]=='-') r-=termen(); else r+=termen(); + + } + + return r; + +} + + + +int termen() + +{ + + int r=factor(); + + while(s[j]=='/' || s[j]=='*') + + { + + j++; + + if(s[j-1]=='*') r*=factor(); else r/=factor(); + + } + + return r; + +} + + + +int factor() + +{ + + int sng=1; + + while(s[j]=='-') sng=-sng, j++; + + if(s[j]=='(') + + { + + j++; + + int r=expresie(); + + j++; + + return r*sng; + + } + + int r=0; + + while('0'<=s[j] && s[j]<='9') r=10*r+(s[j]-'0'), j++; + + return r*sng; + +} + + + +int main() + +{ + + freopen("evaluare.in","r",stdin); freopen("evaluare.out","w",stdout); + + cin.getline(s,N); cout< +#include +#include + + +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +constexpr int NX {100010}; +char expresie[NX]; + +int prioritate(char c) +{ + if(c=='(' || c==')') return 0; + if(c=='+' || c=='-') return 1; + if(c=='*' || c=='/') return 2; +} +int semn(int st, int dr) +{ + int np{}; + for(int i=dr; i>=st; --i) + { + if(expresie[i]==')') + np++; + else if(expresie[i]=='(') + np--; + if(np==0 && (expresie[i]=='+' || expresie[i]=='-')) + return i; + } + for(int i=dr; i>=st; --i) + { + if(expresie[i]==')') + np++; + else if(expresie[i]=='(') + np--; + if(np==0 && (expresie[i]=='/' || expresie[i]=='*')) + return i; + } + return -1; +} +int getNum(int st) +{ + int nr{}; + while(isdigit(expresie[st])) + { + nr=nr*10+(expresie[st]-'0'); + st++; + } + return nr; +} +int solve(int st, int dr) +{ + int poz{semn(st, dr)}; + if(expresie[poz]=='+') + return solve(st, poz-1)+solve(poz+1, dr); + else if(expresie[poz]=='-') + return solve(st, poz-1)-solve(poz+1, dr); + else if(expresie[poz]=='*') + return solve(st, poz-1)*solve(poz+1, dr); + else if(expresie[poz]=='/') + return solve(st, poz-1)/solve(poz+1, dr); + else if(expresie[st]=='(' && expresie[dr]==')') + return solve(st+1, dr-1); + else return getNum(st); +} +int main() +{ + fin.getline(expresie, NX); + fout< +#include + +std::ifstream fin("evaluare.in"); +std::ofstream fout("evaluare.out"); + +class ecuatie { +public: + ecuatie() { + this->rezultat = 0; + this->pozitie = 0; + this->este_rezolvat = 0; + this->str_ecuatie.clear(); + } + + ecuatie(const char* str) { + this->rezultat = 0; + this->pozitie = 0; + this->este_rezolvat = 0; + this->str_ecuatie.clear(); + this->str_ecuatie.append(str); + } + + ecuatie(std::string str) { + this->rezultat = 0; + this->pozitie = 0; + this->este_rezolvat = 0; + this->str_ecuatie.clear(); + this->str_ecuatie = str; + } + + void schimba_ecuatie(const char* str) { + this->rezultat = 0; + this->pozitie = 0; + this->este_rezolvat = 0; + this->str_ecuatie.clear(); + this->str_ecuatie.append(str); + } + + void schimba_ecuatie(std::string str) { + this->rezultat = 0; + this->pozitie = 0; + this->este_rezolvat = 0; + this->str_ecuatie.clear(); + this->str_ecuatie = str; + } + + long long rezolva() { + if (this->este_rezolvat == 0) { + this->este_rezolvat = 1; + this->pozitie = 0; + this->rezultat = eval(); + } + + return this->rezultat; + } + +private: + std::size_t pozitie; + std::string str_ecuatie; + bool este_rezolvat; + long long rezultat; + + long long eval() { + long long r = termen(); + + while (this->str_ecuatie[this->pozitie] == '+' || this->str_ecuatie[this->pozitie] == '-') { + switch (this->str_ecuatie[this->pozitie]) { + case '+': + this->pozitie++; + r = r + termen(); + break; + case '-': + this->pozitie++; + r = r - termen(); + break; + } + } + + return r; + } + + long long termen() + { + long long r = factor(); + + while (this->str_ecuatie[this->pozitie] == '*' || this->str_ecuatie[this->pozitie] == '/') { + switch (this->str_ecuatie[this->pozitie]) { + case '*': + this->pozitie++; + r = r * factor(); + break; + + case '/': + this->pozitie++; + r = r / factor(); + break; + } + } + + return r; + } + + long long factor() + { + long long r = 0; + + if (this->str_ecuatie[this->pozitie] == '(') + { + this->pozitie++; + r = eval(); + this->pozitie++; + } + else { + while (this->str_ecuatie[this->pozitie] >= '0' && this->str_ecuatie[this->pozitie] <= '9') + { + r = r * 10 + (long long)(this->str_ecuatie[this->pozitie]) - (long long)('0'); + this->pozitie++; + } + } + + return r; + } + +}; + +std::string str; + +int main() { + std::getline(fin, str); + fout << ecuatie(str).rezolva() << '\n'; + return 0; +} diff --git a/eval/recursive/e707db79-20df-42ec-8f32-b551ca2d463e.cpp b/eval/recursive/e707db79-20df-42ec-8f32-b551ca2d463e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefa4d428506e54db46191569e5beaeb1c5793c5 --- /dev/null +++ b/eval/recursive/e707db79-20df-42ec-8f32-b551ca2d463e.cpp @@ -0,0 +1,129 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100005; + +int n, p; + +char s[NMAX]; + +int expr(), termen(), factor(); + + + +int expr() + +{ + + int x = termen(); + + while (p <= n && (s[p] == '+' || s[p] == '-')) + + { + + ++p; + + if (s[p - 1] == '+') + + x += termen(); + + else + + x -= termen(); + + } + + return x; + +} + + + +int termen() + +{ + + int x = factor(); + + while (p <= n && (s[p] == '*' || s[p] == '/')) + + { + + ++p; + + if (s[p - 1] == '*') + + x *= factor(); + + else + + x /= factor(); + + } + + return x; + +} + + + +int factor() + +{ + + int x = 0; + + if (s[p] == '(') + + { + + ++p; + + x = expr(); + + ++p; + + } + + else + + { + + while (p <= n && ('0' <= s[p] && s[p] <= '9')) + + x = x * 10 + (s[p++] - '0'); + + } + + return x; + +} + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> (s + 1); + + n = strlen(s + 1); + + p = 1; + + fout << expr() << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/recursive/e7dfa70f-8e54-4c5e-a5bd-713b91a1fadb.cpp b/eval/recursive/e7dfa70f-8e54-4c5e-a5bd-713b91a1fadb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3322b0b41294c53ab42326ce82014467e56a8de3 --- /dev/null +++ b/eval/recursive/e7dfa70f-8e54-4c5e-a5bd-713b91a1fadb.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +int n,i; + +char s[100100]; + + + + + +int termen(); + +int factor(); + +int eval(); + + + + + +int termen() + +{ + + int sol=factor(); + + while(i>s; + + n=strlen(s); + + i=0; + + g< +const long MAX = 100010; + + + +char s[MAX]; + +int i = 0; + + + + + + + +long termen(); + + + +long factor(); +long eval() + +{ + + long rezultat = termen(); + + while ( s[i] =='+' || s[i]=='-' ) + + { + + switch ( s[i] ) + + { + + case '+': + + ++i; + + rezultat += termen(); + + break; + + case '-': + + ++i; + + rezultat -= termen(); + + break; + + } + + } + + return rezultat; + +} +long termen() + +{ + + long rezultat=factor(); + + while ( s[i] =='*' || s[i]=='/' ) + + { + + switch ( s[i] ) + + { + + case '*': + + ++i; + + rezultat *= factor(); + + break; + + case '/': + + ++i; + + rezultat /= factor(); + + break; + + } + + } + + return rezultat; + +} +long factor() + +{ + + long f=0; + + if(s[i]=='(') + + { + + i++; + + f=eval(); + + i++; + + } + + while(s[i]>='0' && s[i]<='9') + + { + + f*=10; + + f+=s[i]-'0'; + + i++; + + } + + return f; + +} + + + + + + + +int main() + +{ + + + + fgets(s, MAX, fopen("evaluare.in", "r")); + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + return 0; + +} diff --git a/eval/recursive/e7f0cb32-2fd3-42ed-b8a2-01adb52a6a9f.cpp b/eval/recursive/e7f0cb32-2fd3-42ed-b8a2-01adb52a6a9f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12fb5ba7aa3f91743caed8f5cb605bbfa737c52f --- /dev/null +++ b/eval/recursive/e7f0cb32-2fd3-42ed-b8a2-01adb52a6a9f.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in") ; + +ofstream fout("evaluare.out") ; + + + +char sir[100005] ; + +int i; + + + +int termen() ; + +int factor() ; + + + +int eval() + +{ + + int r = termen() ; + + while ( sir[i] == '+' || sir[i] == '-' ) + + { + + if ( sir[i] == '+' ) + + { + + i++ ; + + r = r + termen() ; + + } + + else + + { + + i++ ; + + r = r-termen() ; + + } + + } + + return r; + +} + + + +int termen() + +{ + + int r = factor() ; + + while ( sir[i] == '*' || sir[i] == '/' ) + + { + + if ( sir[i] == '*' ) + + { + + i++ ; + + r = r*factor() ; + + } + + else + + { + + i++ ; + + r = r/factor() ; + + } + + } + + return r; + +} + + + +int factor() + +{ + + int r = 0 ; + + if ( sir[i] == '(' ) + + { + + i++ ; + + r = eval() ; + + i++ ; + + } + + else + + { + + while ( sir[i] >='0' && sir[i] <= '9' ) + + { + + r = r*10 + sir[i]-'0' ; + + i++ ; + + } + + } + + return r; + +} + + + +int main() + +{ + + fin >> sir ; + + i = 0 ; + + fout << eval() ; + +} diff --git a/eval/recursive/e86c2398-9eda-4c6a-bc69-bb06cddac3d7.cpp b/eval/recursive/e86c2398-9eda-4c6a-bc69-bb06cddac3d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7873fbaa424b76a76317e8f3910ee370aca01a74 --- /dev/null +++ b/eval/recursive/e86c2398-9eda-4c6a-bc69-bb06cddac3d7.cpp @@ -0,0 +1,164 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +const int N = 100000; + + + +char s[N+1]; + +int p; + + + +int expresie(); + +int termen(); + +int factor(); + + + +int factor() + +{ + + int semn = 1, val = 0; + + while(s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if(s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while(s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*' || s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int expresie() + +{ + + int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + else + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int main() + +{ + + fin>>s; + + fout < +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string sir; + + + +int poz; + + + +int termen(); + +int factor(); + + + +int expr() { + + int val = termen(); + + + + while (poz < sir.size()) { + + if (sir[poz] == '+') { + + poz++; + + val += termen(); + + } + + else if (sir[poz] == '-') { + + poz++; + + val -= termen(); + + } + + else break; + + } + + return val; + +} + + + +int termen() { + + int val = factor(); + + + + while (poz < sir.size()) { + + if (sir[poz] == '*') { + + poz++; + + val *= factor(); + + } + + else if (sir[poz] == '/') { + + poz++; + + val /= factor(); + + } + + else break; + + } + + return val; + +} + + + +int factor() { + + int val = 0; + + + + if (sir[poz] == '(') { + + ++poz; + + val = expr(); + + ++poz; + + } + + else { + + while (poz < sir.size() && '0' <= sir[poz] && sir[poz] <= '9') { + + val = val * 10 + int(sir[poz] - '0'); + + ++poz; + + } + + } + + return val; + +} + + + +int main() { + + + + fin >> sir; + + + + fout << expr(); + + + + return 0; + +} diff --git a/eval/recursive/eac7e0f6-6418-4290-a695-3b27484d39b4.cpp b/eval/recursive/eac7e0f6-6418-4290-a695-3b27484d39b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f935f3b51d0515dd6dc3c6dbd94632a142eba62 --- /dev/null +++ b/eval/recursive/eac7e0f6-6418-4290-a695-3b27484d39b4.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int termen(int&); + +int expresie(int&); + +int factor(int&); + +int numar(int&); + + + +int expresie(int &p) + +{ + + int r=termen(p); + + while(s[p]=='+' or s[p]=='-') + + { + + p++; + + if(s[p-1]=='+') r+=termen(p); + + else r-=termen(p); + + } + + return r; + +} + + + +int termen(int &p) + +{ + + int r=factor(p); + + while(s[p]=='/' or s[p]=='*') + + { + + p++; + + if(s[p-1]=='*') r*=factor(p); + + else r/=factor(p); + + } + + return r; + +} + + + +int factor(int &p) + +{ + + int r=0; + + if(s[p]=='(') + + { + + p++; + + r=expresie(p); + + if(s[p]==')') p++; + + } + + else r=numar(p); + + return r; + +} + + + +int numar(int &p) + +{ + + int nr=0; + + while(s[p]>='0' and s[p]<='9') + + { + + nr*=10; + + nr+=(s[p]-'0'); + + p++; + + } + + return nr; + +} + + + +int main() + +{ + + fin >> s; + + int p=0; + + fout << expresie(p); + + return 0; + +} diff --git a/eval/recursive/eb0fee98-ce38-4b81-af2e-355b7df729c5.cpp b/eval/recursive/eb0fee98-ce38-4b81-af2e-355b7df729c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2ce0353e81471c67cee216499bda76675f50d99 --- /dev/null +++ b/eval/recursive/eb0fee98-ce38-4b81-af2e-355b7df729c5.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +char a[100010]; + +int i; + + + +int expresie(); + +int factor(); + +int termen(); + + + +int main() { + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin>>a; + + fout< +#include + +using namespace std; + +string sir; + +int poz; + +int eval(); +int termen(); +int factor(); + +int expr() +{ + int val = termen(); + + while (poz < sir.size()) + { + if (sir[poz] == '+') + { + ++poz; + val += termen(); + } + else if (sir[poz] == '-') + { + ++poz; + val -= termen(); + } + else + break; + } + + return val; +} + +int termen() +{ + int val = factor(); + + while (poz < sir.size()) + { + if (sir[poz] == '*') + { + ++poz; + val *= factor(); + } + else if (sir[poz] == '/') + { + ++poz; + val /= factor(); + } + else + break; + } + + return val; +} + +int factor() +{ + int val = 0; + + if (sir[poz] == '(') + { + ++poz; + val = expr(); + ++poz; + } + else + { + while (poz < sir.size() && '0' <= sir[poz] && sir[poz] <= '9') + { + val = val * 10 + (sir[poz] - '0'); + ++poz; + } + } + + return val; +} + +int main() +{ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + + cin >> sir; + + cout << expr() << '\n'; + + return 0; +} diff --git a/eval/recursive/eca70ab2-5252-4ca9-9de9-2fcd5f7cf4d7.cpp b/eval/recursive/eca70ab2-5252-4ca9-9de9-2fcd5f7cf4d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76836e290fab42121dd14dccc39b6a734725ec40 --- /dev/null +++ b/eval/recursive/eca70ab2-5252-4ca9-9de9-2fcd5f7cf4d7.cpp @@ -0,0 +1,138 @@ +#include + + +const int MV = 1e5 ; + + + +class Expression { + +private: + + char *exp ; + + int point ; + +public : + + Expression(char S[]) { + + exp = S ; + + point = 0 ; + + } + + char current() { return exp[point] ; } + + __attribute__ ( (always_inline)) void advance() { point ++ ; } + + + + int evaluate() { + + int currVal = termen() ; + + for ( ; current() == '+' || current() == '-' ; ) { + + if (current() == '+') { + + advance() ; + + currVal += termen() ; + + } else { + + advance() ; + + currVal -= termen() ; + + } + + } + + return currVal ; + + } + + + + int termen() { + + int currVal = factor() ; + + for ( ; current() == '*' || current() == '/' ; ) { + + if (current() == '*') { + + advance() ; + + currVal *= factor() ; + + } else { + + advance() ; + + currVal /= factor() ; + + } + + } + + return currVal ; + + } + + + + int factor() { + + int currVal(0) ; + + if (current() == '(') { + + advance() ; + + currVal += evaluate() ; + + advance() ; + + } else { + + for ( ; isdigit(current()) ; advance()) { + + currVal = currVal * ((1 << 3) + (1 << 1)) + (current() - '0') ; + + } + + } + + return currVal ; + + } + + + +}; + + + +std::fstream in ("evaluare.in", std::ios::in) ; + +std::fstream out ("evaluare.out", std::ios::out) ; + + + +int main() { + + char S[MV + 5] ; + + in >> S ; + + Expression expresie(S) ; + + int answer = expresie.evaluate() ; + + out << answer ; + +} diff --git a/eval/recursive/ecdffea7-751f-4b33-8c87-55797dfa58ed.cpp b/eval/recursive/ecdffea7-751f-4b33-8c87-55797dfa58ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cc46f0984c8becea92c175570cdc5750f4e20e4 --- /dev/null +++ b/eval/recursive/ecdffea7-751f-4b33-8c87-55797dfa58ed.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005],*p; + +int factor(); + +int elem(); + +int termen() + +{ + + int rez=factor(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') + + { + + p++; + + rez=rez+factor(); + + } + + else + + { + + p++; + + rez=rez-factor(); + + } + + } + + return rez; + +} + +int factor() + +{ + + int rez=elem(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + rez=rez*elem(); + + } + + else + + { + + p++; + + rez=rez/elem(); + + } + + } + + return rez; + +} + +int elem() + +{ int rez=0; + + if(*p=='(') + + { + + p++; + + rez=termen(); + + p++; + + return rez; + + } + + + + while(isdigit(*p)) + + { + + rez=rez*10+*p-'0'; + + p++; + + } + + return rez; + +} + +int main() + +{ + + fin>>s; + + p=s; + + fout< +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +char s[100010]; + +int i = 1; + + + +int eval(); + +int factor(); + + + +int numar() + +{ + + int r = 0; + + if(s[i] == '(') + + { + + i++; + + r = eval(); + + i++; + + } + + else + + { + + while(isdigit(s[i])) + + { + + r = r * 10 + s[i] - '0'; + + i++; + + } + + } + + return r; + +} + + + +int factor() + +{ + + int r = numar(); + + while(s[i] == '*' || s[i] == '/') + + { + + if(s[i] == '*') + + { + + i++; + + r = r * numar(); + + } + + else + + { + + i++; + + r = r / numar(); + + } + + } + + return r; + +} + + + +int eval() + +{ + + int r = factor(); + + while(s[i] == '-' || s[i] == '+') + + { + + int semn = 1; + + if(s[i] == '-') + + semn = -1; + + i++; + + r = r + semn * factor(); + + } + + return r; + +} + + + +int main() + +{ + + in.getline(s + 1, 100005); + + out << eval(); + + return 0; + +} diff --git a/eval/recursive/ef3ad713-282a-42c7-a182-efd23e2bb219.cpp b/eval/recursive/ef3ad713-282a-42c7-a182-efd23e2bb219.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef23c166a4f3bafc26600e0e1c297d8c0b242afa --- /dev/null +++ b/eval/recursive/ef3ad713-282a-42c7-a182-efd23e2bb219.cpp @@ -0,0 +1,90 @@ +#include +char s[100100]; + +int poz; + +int eval(); + +int produs(); + +int termen(){ + + int aux=0; + + if(s[poz]=='('){ + + ++poz; + + aux=eval(); + + ++poz; + + return aux; + + } + + while('0'<=s[poz] && s[poz]<='9') + + aux=aux*10+(s[poz++]-'0'); + + return aux; + +} + +int produs(){ + + int aux=termen(); + + while(s[poz]=='*' || s[poz]=='/'){ + + if(s[poz++]=='*') + + aux*=termen(); + + else + + aux/=termen(); + + } + + return aux; + +} + +int eval(){ + + int aux=produs(); + + while(s[poz]=='+' || s[poz]=='-'){ + + if(s[poz++]=='+') + + aux+=produs(); + + else + + aux-=produs(); + + } + + return aux; + +} + +int main(){ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + scanf("%s",s); + + printf("%d\n",eval()); + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/eval/recursive/efaed8c2-0a90-46ef-b6f0-9fa5baed2d49.cpp b/eval/recursive/efaed8c2-0a90-46ef-b6f0-9fa5baed2d49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feb7d915aa6a3734d2dcfc3c8d1c103522353e5e --- /dev/null +++ b/eval/recursive/efaed8c2-0a90-46ef-b6f0-9fa5baed2d49.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval() + +{ + + int r=termen(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else + + { + + p++; + + r-=termen(); + + } + + } + + return r; + +} + +int termen() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + } + + return r; + +} + +int factor() + +{ + + int r=0; + + if(*p=='('){ + + p++;r=eval();p++; + + } + + else while(*p>='0' && *p<='9'){ + + r=r*10+*p-'0';p++; + + } + + return r; + +} + +int main() + +{ + + f.getline(s,100005); + + p=s; + + g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char s[100005],*p=s; + + + +int e2(); + +int e1() + +{ + + int r=e2(); + + while(*p=='+' or *p=='-') + + if(*p=='+') + + p++, r+=e2(); + + else + + p++, r-=e2(); + + return r; + +} + +int e3(); + +int e2() + +{ + + int r=e3(); + + while(*p=='*' or *p=='/') + + if(*p=='*') + + p++, r*=e3(); + + else + + p++, r/=e3(); + + return r; + +} + +int e3() + +{ + + int r=0; + + if(*p=='(') + + p++, r=e1(), p++; + + else + + while(*p>='0' and *p<='9') + + r=r*10+*p-'0', p++; + + return r; + +} + +int main() + +{ + + + + fin >> s; + + fout << e1(); + + + + return 0; + + + +} diff --git a/eval/recursive/f04336a9-b5af-4874-a6e9-4bef52ec1886.cpp b/eval/recursive/f04336a9-b5af-4874-a6e9-4bef52ec1886.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b14d248124c17b5e9e6dc1b420f38ef200ed2f6 --- /dev/null +++ b/eval/recursive/f04336a9-b5af-4874-a6e9-4bef52ec1886.cpp @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +const int DIM = 1e5; + +int l; +char s[DIM + 5]; + +long long SumaDif(int &p); +long long ProdCat(int &p); +long long ParantezeVal(int &p); + +long long SumaDif(int &p) +{ + long long res = ProdCat(p); + + while(p <= l && (s[p] == '+' || s[p] == '-')) { + + if(s[p] == '+') + p++, res += ProdCat(p); + else + p++, res -= ProdCat(p); + + } + + return res; +} + +long long ProdCat(int &p) +{ + long long res = ParantezeVal(p); + + while(p <= l && (s[p] == '*' || s[p] == '/')) { + + if(s[p] == '*') + p++, res *= ParantezeVal(p); + else + p++, res /= ParantezeVal(p); + + } + + return res; +} + +long long ParantezeVal(int &p) +{ + if(s[p] == '(') + { + p++; + long long res = SumaDif(p); + p++; + + return res; + } + + long long nr = 0; + + while(isdigit(s[p])) { + + nr = nr * 10LL + (s[p] - '0'); + p++; + + } + + return nr; +} + +int main() +{ + fin >> s; l = strlen(s) - 1; + + int p = 0; + fout << SumaDif(p) << '\n'; + + return 0; +} diff --git a/eval/recursive/f08401bd-0273-4a4a-ba9c-b8d386c481d0.cpp b/eval/recursive/f08401bd-0273-4a4a-ba9c-b8d386c481d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da1a00647f7e893de854cd728fa5439568f1763c --- /dev/null +++ b/eval/recursive/f08401bd-0273-4a4a-ba9c-b8d386c481d0.cpp @@ -0,0 +1,76 @@ +#include + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +const string file = "evaluare"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 2147483647; + +int p; +string s; + +int factor(); +int termen(); + +int eval() +{ + int x = factor(); + while(s[p] == '+' || s[p] == '-'){ + if(s[p] == '+'){ + ++p; + x += factor(); + }else{ + ++p; + x -= factor(); + } + } + return x; +} + +int factor() +{ + int x = termen(); + while(s[p] == '*' || s[p] == '/'){ + if(s[p] == '*'){ + ++p; + x *= termen(); + }else{ + ++p; + x /= termen(); + } + } + return x; +} + +int termen() +{ + if(s[p] == '('){ + ++p; + int x = eval(); + ++p; + return x; + } + int x = 0; + while(s[p] >= '0' && s[p] <= '9'){ + x = x*10+s[p]-'0'; + ++p; + } + return x; +} + +int main() +{ + ifstream fin (file+".in"); + ofstream fout (file+".out"); + fin >> s; + s += "#"; + fout << eval() << "\n"; + return 0; +} diff --git a/eval/recursive/f08e848c-be73-4422-ae52-9fcd9895ac33.cpp b/eval/recursive/f08e848c-be73-4422-ae52-9fcd9895ac33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7868ffe0bbcd8817db397758008a3fa09bfa51a1 --- /dev/null +++ b/eval/recursive/f08e848c-be73-4422-ae52-9fcd9895ac33.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char *p,e[1<<17]; + + + +int eval(),termen(),factor(); + + + +int main() + +{ f.get(e,1<<17); + + p=e; + + g<='0' && *p<='9'; p++) + + nr=nr*10+(*p-'0'); + + r=nr; + + } + + return r; + +} diff --git a/eval/recursive/f096c854-f52e-444f-a577-26d41f7497b7.cpp b/eval/recursive/f096c854-f52e-444f-a577-26d41f7497b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2618f37fd653b97ca9eae39a01bf28435c2575 --- /dev/null +++ b/eval/recursive/f096c854-f52e-444f-a577-26d41f7497b7.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + + + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int p; + + + +int expresie (); + +int termen (); + +int factor (); + + + +int expresie () + +{ + + int sum = termen (); + + while (s[p] == '+' || s[p] == '-') + + { + + if (s[p] == '+') + + { + + p++; + + sum += termen(); + + } + + if (s[p] == '-') + + { + + p++; + + sum -= termen(); + + } + + } + + return sum; + +} + + + +int termen () + +{ + + int prod = factor (); + + while (s[p] == '*' || s[p] == '/') + + { + + if (s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + if (s[p] == '/') + + { + + p++; + + prod /= factor(); + + } + + } + + return prod; + +} + + + +int factor () + +{ + + int semn = 1, val = 0; + + while (s[p] == '-') + + { + + p++; + + semn = -semn; + + } + + if (s[p] == '(') + + { + + p++; + + val = expresie(); + + p++; + + return semn * val; + + } + + while (s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p++; + + } + + return semn * val; + +} + + + +int main() + +{ + + p = 0; + + in >> s; + + out << expresie (); + + in.close(); + + out.close(); + + return 0; + +} diff --git a/eval/recursive/f0995b3c-ec62-4e72-990e-a62ef321cf60.cpp b/eval/recursive/f0995b3c-ec62-4e72-990e-a62ef321cf60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a157351afd58a4e9ecbd0b11f645e0ae6ba862c7 --- /dev/null +++ b/eval/recursive/f0995b3c-ec62-4e72-990e-a62ef321cf60.cpp @@ -0,0 +1,217 @@ +#include +const long MAX = 100010; + + + +char S[MAX], *p=S; + + + + + + + +long termen(); + + + +long factor(); +long eval() { + + + + long r = termen(); + + + + while ( *p=='+' || *p=='-' ) { + + + + switch ( *p ) { + + + + case '+': + + + + ++p; + + + + r += termen(); + + + + break; + + + + case '-': + + + + ++p; + + + + r -= termen(); + + + + break; + + + + } + + + + } + + + + return r; + + + +} +long termen() { + + + + long r = factor(); + + + + while ( *p=='*' || *p=='/' ) { + + + + switch ( *p ) { + + + + case '*' : + + + + ++p; + + + + r *= factor(); + + + + break; + + + + case '/': + + + + ++p; + + + + r /= factor(); + + + + break; + + + + } + + + + } + + + + return r; + + + +} +long factor() { + + + + long r=0; + + + + if ( *p == '(' ) { + + + + ++p; + + + + r = eval(); + + + + ++p; + + + + } else { + + + + while ( *p>='0' && *p<='9' ) { + + + + r = r*10 + *p - '0'; + + + + ++p; + + + + } + + + + } + + + + return r; + + + +} + + + + + + + +int main() { + + + + fgets(S, MAX, fopen("evaluare.in", "r")); + + + + fprintf(fopen("evaluare.out", "w"), "%ld\n", eval()); + + + + return 0; + + + +} diff --git a/eval/recursive/f1e37bd1-c0c1-42b3-96d0-d690974337fb.cpp b/eval/recursive/f1e37bd1-c0c1-42b3-96d0-d690974337fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45027e573e36fa512ee47bfba7ecd68d0a82f31b --- /dev/null +++ b/eval/recursive/f1e37bd1-c0c1-42b3-96d0-d690974337fb.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +string s; + +int i; + + + +int f1(); + +int f2(); + +int f3(); + + + + + +int f1(){ + + int raspuns = f2(); + + while(s[i] == '+' || s[i] == '-'){ + + if(s[i] == '+'){ + + i++; + + raspuns += f2(); + + }else{ + + i++; + + raspuns -= f2(); + + } + + } + + return raspuns; + +} + +int f2(){ + + int raspuns = f3(); + + while(s[i] == '*' || s[i] == '/'){ + + if(s[i] == '*'){ + + i++; + + raspuns *= f3(); + + }else{ + + i++; + + raspuns /= f3(); + + } + + } + + return raspuns; + +} + + + +int f3(){ + + int raspuns = 0; + + if(s[i] == '('){ + + i++; + + raspuns = f1(); + + i++; + + }else{ + + while(isdigit(s[i])){ + + raspuns = raspuns * 10 + s[i] - '0'; + + i++; + + } + + } + + return raspuns; + +} + + + +int main() + +{ + + in>>s; + + out< +#include +#include +#include + +std::string sir; +int pos; + +void read() { + std::ifstream in("evaluare.in"); + + in >> sir; + + in.close(); +} + +int expresie(); +int termen(); +int factor(); + +int expresie() { + int val = termen(); + + while (pos < sir.size()) { + if (sir[pos] == '+') { + ++pos; + val += termen(); + } else if (sir[pos] == '-') { + ++pos; + val -= termen(); + } else { + break; + } + } + + return val; +} + +int termen() { + int val = factor(); + + while (pos < sir.size()) { + if (sir[pos] == '*') { + ++pos; + val *= factor(); + } else if (sir[pos] == '/') { + ++pos; + val /= factor(); + } else { + break; + } + } + + return val; +} + +int factor() { + int val = 0; + + if (sir[pos] == '(') { + ++pos; + val = expresie(); + ++pos; + } else { + while (isdigit(sir[pos])) { + val = 10 * val + (sir[pos] - '0'); + ++pos; + } + } + + return val; +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << expresie(); + + return 0; +} diff --git a/eval/recursive/f21a94b2-98e1-4985-a4d9-a553b53924fd.cpp b/eval/recursive/f21a94b2-98e1-4985-a4d9-a553b53924fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57c8ebe72068a40d8dd03a1486176708aca78648 --- /dev/null +++ b/eval/recursive/f21a94b2-98e1-4985-a4d9-a553b53924fd.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +using namespace std; +string expresion; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); +char is_digit(char a) +{ + return a>='0'&&a<='9'; +} +int get_number(int&to_start) +{ + int f=0; + for(; is_digit(expresion[to_start]); to_start++) + { + f=f*10+(expresion[to_start]-'0'); + } + to_start--; + return f; + +} + +void encapsulate(string&a) +{ + a='('+a+')'; +} +bool is_operator(char a) +{ + return a=='+'||a=='-'; +} +bool is_operator2(char a) +{ + return a=='*'||a=='/'; +} +void evaluate(int&last,int now,char oper) +{ + switch(oper) + { + case '+': + last+=now; + break; + case '-': + last-=now; + break; + case '/': + last/=now; + break; + case '*': + last*=now; + break; + + } + +} +int get_factor(int&to_start); +int solve_paranthesis(int&to_start); +int get_element(int&to_start) +{ + if(is_digit(expresion[to_start])) + return get_number(to_start); + else if (expresion[to_start]=='(') + { + to_start++; + int to_return=solve_paranthesis(to_start); + return to_return; + } + return 0; + + +} +int get_factor(int&to_start) +{ + int first,num; + first=get_element(to_start); + to_start++; + char last; + while(is_operator2(expresion[to_start])) + { + last=expresion[to_start]; + to_start++; + num=get_element(to_start); + evaluate(first,num,last); + to_start++; + } + to_start--; + return first; + + +} +int solve_paranthesis(int&to_start) +{ + char last_operator='+'; + int last_result=0; + for(; expresion[to_start]!=')'; to_start++) + { + if(!is_operator(expresion[to_start])) + { + int p=get_factor(to_start); + evaluate(last_result,p,last_operator); + } + else + { + last_operator=expresion[to_start]; + } + } + return last_result; + + + +} +int main() +{ + in>>expresion; + encapsulate(expresion); + int cursor=1; + out< + + + + +using namespace std; + + + +char s[100000], *p = s; + + + +int eval(); + + + +int factor( ) { + + int nr = 0; + + if( *p == '(' ) { + + p ++; + + nr = eval(); + + p ++; + + return nr; + + } + + else { + + while( *p >= '0' && *p <= '9' ) { + + nr = nr * 10 + (*p - '0'); + + p ++; + + } + + return nr; + + } + +} + + + +int termen( ) { + + int r = factor(); + + while( *p == '*' || *p == '/' ) { + + if( *p == '*' ) + + p ++, r = r * factor(); + + else + + p ++, r = r / factor(); + + } + + return r; + +} + + + +int eval( ) { + + int r = termen(); + + while( *p == '+' || *p == '-' ) { + + if( *p == '+' ) + + p ++, r += termen(); + + else + + p ++, r -= termen(); + + } + + return r; + +} + + + +int main() { + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin>>s; + + fout< +#include + + +using namespace std; + + + +const int N = 100001; + +int p; + +char s[N]; + + + +int e(); + +int t(); + +int f(); + + + +int e() { + + int x = t(); + + bool ok; + + while (s[p] == '+' || s[p] == '-') { + + ok = (s[p] == '+'); + + p++; + + if (ok) + + x += t(); + + else + + x -= t(); + + } + + return x; + +} + + + +int t() { + + int x = f(); + + bool ok; + + while (s[p] == '*' || s[p] == '/') { + + ok = (s[p] == '*'); + + p++; + + if (ok) + + x *= f(); + + else + + x /= f(); + + } + + return x; + +} + + + +int f() { + + int sgn = 1; + + while (s[p] == '-') { + + p++; + + sgn *= -1; + + } + + if (s[p] == '(') { + + p++; + + int x = e(); + + p++; + + return x * sgn; + + } + + int x = 0; + + while ('0' <= s[p] && s[p] <= '9') { + + x = 10 * x + s[p] - '0'; + + p++; + + } + + return x * sgn; + +} + + + +int main() { + + freopen ("evaluare.in","r",stdin); + + freopen ("evaluare.out","w",stdout); + + + + scanf("%s", &s); + + printf("%d\n", e()); + + + + + + return 0; + +} diff --git a/eval/recursive/f3789149-2828-4d19-bb53-6f76d6e6f579.cpp b/eval/recursive/f3789149-2828-4d19-bb53-6f76d6e6f579.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cb16a450112e10f2ab806dc8a98ef7ec86712a2 --- /dev/null +++ b/eval/recursive/f3789149-2828-4d19-bb53-6f76d6e6f579.cpp @@ -0,0 +1,152 @@ +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int N = 100000; + +char s[N + 1]; + +int expresie(); + +int factor(); + +int termen(); + +int p; + + + +int expresie() + +{ + +int sum = termen(); + + while(s[p] == '+' || s[p] == '-') + + { + + if(s[p] == '+') + + { + + p ++; + + sum += termen(); + + } + + else + + { + + p ++; + + sum -= termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod = factor(); + + while(s[p] == '*'||s[p] == '/') + + { + + if(s[p] == '*') + + { + + p++; + + prod *= factor(); + + } + + else + + { + + p ++; + + prod /= factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int sign = 1,val = 0; + + while(s[p] == '-') + + { + + p ++; + + sign = -sign; + + } + + if(s[p] == '(') + + { + + p ++; + + val = expresie(); + + p ++; + + return sign * val; + + } + + else while(s[p] >= '0' && s[p] <= '9') + + { + + val = val * 10 + (s[p] - '0'); + + p ++; + + } + + return sign * val; + +} + + + +int main() + +{ + + in >> s; + + out << expresie(); + + return 0; + +} diff --git a/eval/recursive/f4a24a67-31a4-485c-a848-53fdbf8a8e42.cpp b/eval/recursive/f4a24a67-31a4-485c-a848-53fdbf8a8e42.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3122deb359212cb03b37d508b344df20321663ed --- /dev/null +++ b/eval/recursive/f4a24a67-31a4-485c-a848-53fdbf8a8e42.cpp @@ -0,0 +1,72 @@ +#include + + +std::ifstream in("evaluare" ".in"); + +std::ofstream out("evaluare" ".out"); + +char S[100001]; int i, zero(); + +int doi() + +{ + + if (S[i] == '(') + + return i++, zero(); + + int n = 0; + + while ('0' <= S[i] and S[i] <= '9') + + (n *= 10) += S[i++] - '0'; + + return n; + +} + +int unu() + +{ + + int p = doi(); + + for (;;) + + switch (S[i++]) + + { + + case '*': p *= doi(); break; + + case '/': p /= doi(); break; + + default: i--; return p; + + } + +} + +int zero() + +{ + + int s = 0, semn = 1; + + while (s += semn * unu(), S[i] and S[i] != ')') + + semn = (S[i++] == '+'? 1: -1); + + i++; return s; + +} + +int main() + +{ + + in >> S; + + out << zero(); + +} diff --git a/eval/recursive/f4cdb839-e50c-49d9-be18-820c6cac10ee.cpp b/eval/recursive/f4cdb839-e50c-49d9-be18-820c6cac10ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e3f954255d499f0b1950d294156c994f951072e --- /dev/null +++ b/eval/recursive/f4cdb839-e50c-49d9-be18-820c6cac10ee.cpp @@ -0,0 +1,137 @@ +#include +#include +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +int i; + +string A; + +int produs(); + +int expresie(); + +int aduna() + +{ + + int rez=produs(); + + while(A[i]=='+' or A[i]=='-') + + { + + if(A[i]=='+') + + { + + i++; + + rez=rez+produs(); + + } + + else + + { + + i++; + + rez=rez-produs(); + + } + + } + + return rez; + +} + +int produs() + +{ + + int rez=expresie(); + + while(A[i]=='*' || A[i]=='/') + + { + + if(A[i]=='*') + + { + + i++; + + rez=rez*expresie(); + + } + + else + + { + + i++; + + rez=rez/expresie(); + + } + + } + + return rez; + +} + +int expresie() + +{ + + int rez=0; + + if(A[i]=='(') + + { + + i++; + + rez=aduna(); + + i++; + + } + + else + + { + + while(A[i]>='0' && A[i]<='9') + + { + + rez=rez*10+(A[i]-'0'); + + i++; + + } + + } + + return rez; + +} + +int main() + +{ + + fi>>A; + + fo< +#include + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +string s; + +int i; + +long termen(); + +long factor(); + +long eval() + +{ + + int x=termen(); + + while(s[i]=='+'||s[i]=='-') + + { + + if(s[i]=='+') + + { + + i++; + + x+=termen(); + + } + + else if(s[i]=='-') + + { + + i++; + + x-=termen(); + + + + } + + } + + return x; + +} + +long termen() + +{ + + long x=factor(); + + while(s[i]=='*'||s[i]=='/') + + { + + if(s[i]=='*') + + { + + i++; + + x=x*factor(); + + } + + else if(s[i]=='/') + + { + + i++; + + x=x/factor(); + + } + + } + + return x; + +} + +long factor() + +{long x=0; + + if(s[i]=='(') + + { + + i++; + + x=eval(); + + i++; + + } + + else + + { + + while(s[i]>='0'&&s[i]<='9') + + { + + x=x*10+s[i]-'0'; + + i++; + + } + + } + + return x; + +} + + + +int main() { + + + +f>>s; + +g< + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; + +const string file = "evaluare"; +const ll INF = 9223372036854775807ll; +const int inf = 2147483647; + +int p; +string s; + +int factor(), termen(); + +int eval() +{ + int r = factor(); + while(s[p] == '+' || s[p] == '-') + if(s[p] == '+'){ + ++p; + r += factor(); + }else{ + ++p; + r -= factor(); + } + return r; +} + +int factor() +{ + int r = termen(); + while(s[p] == '*' || s[p] == '/') + if(s[p] == '*'){ + ++p; + r *= termen(); + }else{ + ++p; + r /= termen(); + } + return r; +} + +int termen() +{ + int r = 0; + if(s[p] == '('){ + ++p; + r = eval(); + ++p; + }else + while(s[p] >= '0' && s[p] <= '9'){ + r = r*10 + s[p]-'0'; + ++p; + } + return r; +} + +int main() +{ + ifstream fin (file+".in"); + ofstream fout (file+".out"); + fin >> s; s += "#"; + fout << eval(); + return 0; +} diff --git a/eval/recursive/f9fed51a-5ac0-4b4c-975a-1bfdc424523a.cpp b/eval/recursive/f9fed51a-5ac0-4b4c-975a-1bfdc424523a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..175f21a417e62eed891da3ee52c21644c7290b49 --- /dev/null +++ b/eval/recursive/f9fed51a-5ac0-4b4c-975a-1bfdc424523a.cpp @@ -0,0 +1,103 @@ +#include +#include + +using namespace std; +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100000],*p; + +int eval(); + +int termen(); + +int factor(); + +int eval() +{ + int r=termen(); + + while(*p=='+' || *p=='-') + if(*p=='+') + { + p++; + + r+=termen(); + + } + else if(*p=='-') + { + p++; + + r-=termen(); + + } + + return r; + + +} + +int termen() +{ + int r=factor(); + + while(*p=='*' || *p=='/') + if(*p=='*') + { + p++; + + r*=factor(); + + } + else if(*p=='/') + { + p++; + + r/=factor(); + + } + + return r; + +} + +int factor() +{ + int r=0; + + if(*p=='(') + { + p++; + + r=eval(); + + p++; + + } + else + while('0'<=*p && *p<='9') + { + r=r*10+*p-'0'; + + p++; + + } + + return r; + +} +int main() +{ + f.getline(s,100000); + + p=s; + + int rez=eval(); + + g< + + +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],*p; + +int eval(); + +int termen(); + +int factor(); + +int factor() + +{ + + int r=0; + + if(*p=='(') + + { + + p++; + + r=eval(); + + p++; + + } + + else + + while(*p>='0'&&*p<='9') + + { + + r=r*10+*p-'0'; + + p++; + + } + +return r; + +} + +int termen () + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + if(*p=='*') + + { + + p++; + + r*=factor(); + + } + + else + + { + + p++; + + r/=factor(); + + } + + return r; + +} + +int eval() + +{ + + int r=termen(); + + while(*p=='+'||*p=='-') + + if(*p=='+') + + { + + p++; + + r+=termen(); + + } + + else { p++; r-=termen(); } + + return r; + +} + +int main() + +{ + + f.getline(s,100005); + + p=s; + + g< +#include +using namespace std; + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100001],*p; + +int r; + +int eval(); + +int termene(); + +int factor(); + +int eval() + +{ + + int r=termene(); + + while(*p=='+'||*p=='-') + + { + + if(*p=='+') p++,r+=termene(); + + else p++,r-=termene(); + + } + + return r; + +} + + + + + +int termene() + +{ + + int r=factor(); + + while(*p=='*'||*p=='/') + + { + + if(*p=='*') p++,r*=factor(); + + else p++,r/=factor(); + + } + + return r; + +} + + + + + +int factor() + +{ + + int r=0; + + if(*p=='(') + + p++,r=eval(),p++; + + else while('0'<=*p&&'9'>=*p) + + r=r*10+*p-'0',p++; + + return r; + +}int main() + +{ + + f.getline(s,100001); + + p=s; + + r=eval(); + + g< + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + + + + + +char expresie[100005]; + +int n; + +int poz; + + + +int eval(); + +int factor(); + +int find_nr(); + +bool cifra(char c); + +int rez(int p1 , char oper , int p2); + + + + + +int main() + +{ + + while (!fin.eof()) + + { + + fin >> expresie[n]; + + n++; + + } + + n--; + + fout << eval(); + + + + return 0; + +} + + + +int eval() + +{ + + int nr1 = 0, nr2; + + char oper; + + while (poz < n && expresie[poz] != ')') + + { + + nr1 += factor(); + + if (poz >= n || expresie[poz] == ')') + + return nr1; + + + + oper = expresie[poz]; + + + + poz++; + + nr2 = factor(); + + nr1 = rez(nr1 , oper , nr2); + + } + + return nr1; + +} + + + +int factor() + +{ + + int nr1 = 0 , nr2; + + char oper; + + while (poz < n && expresie[poz] != ')') + + { + + nr1 += find_nr(); + + if (poz >= n || expresie[poz] == ')') + + return nr1; + + + + oper = expresie[poz]; + + if (oper == '-' || oper == '+') + + return nr1; + + + + poz++; + + nr2 = find_nr(); + + nr1 = rez(nr1 , oper , nr2); + + } + + return nr1; + +} + + + +int find_nr() + +{ + + int nr = 0; + + if (cifra(expresie[poz]) == true) + + { + + while (cifra(expresie[poz])) + + { + + nr = nr * 10 + int(expresie[poz]) - 48; + + poz++; + + } + + } + + else if (expresie[poz] == '(') + + { + + poz++; + + nr = eval(); + + poz++; + + } + + return nr; + +} + + + +bool cifra(char c) + +{ + + if (c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int rez(int p1 , char oper , int p2) + +{ + + if (oper == '-') + + return p1 - p2; + + if (oper == '+') + + return p1 + p2; + + if (oper == '/') + + return p1 / p2; + + if (oper == '*') + + return p1 * p2; + +} diff --git a/eval/recursive/fe00c219-658e-4386-a203-cbfeb525cad9.cpp b/eval/recursive/fe00c219-658e-4386-a203-cbfeb525cad9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c8eb6ca4e36bf3add3f049629ba82280899fa3d --- /dev/null +++ b/eval/recursive/fe00c219-658e-4386-a203-cbfeb525cad9.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +const int MAXN = 1 * 1e5 + 5; + +char sir[MAXN], *p = sir; + +long long termen(); + +long long factor(); + +long long eval(){ + + long long r = termen(); + + while(*p == '+' || *p == '-'){ + + if(*p == '+'){ + + ++p; + + r += termen(); + + } + + else if(*p == '-'){ + + ++p; + + r -= termen(); + + } + + } + + return r; + +} + +long long termen(){ + + long long r = factor(); + + while(*p == '*' || *p == '/'){ + + if(*p == '*'){ + + ++p; + + r *= factor(); + + } + + else if(*p == '/'){ + + ++p; + + r /= factor(); + + } + + } + + return r; + +} + +long long factor(){ + + long long r = 0; + + if(*p == '('){ + + ++p; + + r = eval(); + + ++p; + + } + + else{ + + while(*p <= '9' and *p >= '0') + + r = r * 10 + *p - '0', ++p; + + } + + return r; + +} + +int main() + +{ + + fin.getline(sir, MAXN); + + fout << eval(); + + return 0; + +} diff --git a/eval/recursive/fe4f6778-d4d5-4ae6-8cd3-5ea1889d162b.cpp b/eval/recursive/fe4f6778-d4d5-4ae6-8cd3-5ea1889d162b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b8447d01de9e4c64c2a82e446e4c82368dbf644 --- /dev/null +++ b/eval/recursive/fe4f6778-d4d5-4ae6-8cd3-5ea1889d162b.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; +string expresion; +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); +char is_digit(char a) +{ + return a>='0'&&a<='9'; +} +int get_number(int&to_start) +{ + int f=0; + for(; is_digit(expresion[to_start]); to_start++) + { + f=f*10+(expresion[to_start]-'0'); + } + to_start--; + return f; + +} + +void encapsulate(string&a) +{ + a='('+a+')'; +} +bool is_operator(char a) +{ + return a=='+'||a=='-'; +} +bool is_operator2(char a) +{ + return a=='*'||a=='/'; +} +void evaluate(int&last,int now,char oper) +{ + switch(oper) + { + case '+': + last+=now; + break; + case '-': + last-=now; + break; + case '/': + last/=now; + break; + case '*': + last*=now; + break; + + } + +} +int get_factor(int&to_start); +int solve_paranthesis(int&to_start); +int get_element(int&to_start) +{ + if(is_digit(expresion[to_start])) + return get_number(to_start); + else if (expresion[to_start]=='(') + { + to_start++; + int to_return=solve_paranthesis(to_start); + return to_return; + } + + +} +int get_factor(int&to_start) +{ + int first,num; + first=get_element(to_start); + to_start++; + char last; + while(is_operator2(expresion[to_start])) + { + last=expresion[to_start]; + to_start++; + num=get_element(to_start); + evaluate(first,num,last); + to_start++; + } + to_start--; + return first; + + +} +int solve_paranthesis(int&to_start) +{ + char last_operator='+'; + int last_result=0; + for(; expresion[to_start]!=')'; to_start++) + { + if(!is_operator(expresion[to_start])) + { + int p=get_factor(to_start); + evaluate(last_result,p,last_operator); + } + else + { + last_operator=expresion[to_start]; + } + } + return last_result; + + + +} +int main() +{ + in>>expresion; + encapsulate(expresion); + int cursor=1; + out< +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100001],*p=s; + +long x; + +long numar(); + +long fact(); + + + +long adsc() + +{ + + long x=numar(); + + while(*p=='+' or *p=='-') + + { + + switch (*p) + + { + + case '+': + + p++; + + x=x+numar(); + + break; + + case '-': + + p++; + + x=x-numar(); + + break; + + } + + } + + return x; + +} + + + + + +long numar() + +{ + + long x=fact(); + + while(*p=='*' or *p=='/') + + { + + { + + switch (*p) + + { + + case '*': + + p++; + + x=x*fact(); + + break; + + case '/': + + p++; + + x=x/fact(); + + break; + + } + + } + + + + + + + + } + + return x; + +} + + + + + +long fact() + +{ + + x=0; + + if(*p=='(') + + { + + p++; + + x=adsc(); + + p++; + + } + + else + + { + + while(*p<='9' and *p>='0') + + { + + x=x*10+(*p-'0'); + + p++; + + } + + + + } + + return x; + +} + + + +int main() + +{ + + f.get(s,100001); + + g< +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +char s[100001]; + +int p; + +int expresie(); + +int termen(); + +int factor(); + +int expresie() + +{ + + int sum=termen(); + + while(s[p]=='+'||s[p]=='-') + + { + + if(s[p]=='+') + + { + + p++; + + sum+=termen(); + + }else + + { + + p++; + + sum-=termen(); + + } + + } + + return sum; + +} + +int termen() + +{ + + int prod=factor(); + + while(s[p]=='*'||s[p]=='/') + + { + + if(s[p]=='*') + + { + + p++; + + prod*=factor(); + + } + + else + + { + + p++; + + prod/=factor(); + + } + + } + + return prod; + +} + +int factor() + +{ + + int val=0,semn=1; + + while(s[p]=='-') + + { + + semn=-semn; + + p++; + + } + + if(s[p]=='(') + + { + + p++; + + val=expresie(); + + p++; + + return semn*val; + + } + + while(s[p]>='0'&&s[p]<='9') + + { + + val=val*10+s[p++]-'0'; + + } + + return semn*val; + +} + +int main() + +{ + + p=0; + + while(in.get(s[p])) + + { + + p++; + + } + + p=0; + + out< +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int exp(), term(), fact(); + + + +int i; + +char s[100010]; + + + +int exp(){ + + int r=term(); + + + + while(s[i]=='+' || s[i]=='-'){ + + i++; + + if(s[i-1]=='+') + + r+=term(); + + else + + r-=term(); + + } + + + + return r; + +} + + + +int term(){ + + int r=fact(); + + + + while(s[i]=='*' || s[i]=='/'){ + + i++; + + if(s[i-1]=='*') + + r*=fact(); + + else + + r/=fact(); + + } + + + + return r; + +} + + + +int fact(){ + + int semn=1; + + + + if(s[i]=='-') + + semn=-1,i++; + + + + int r; + + + + if(s[i]=='('){ + + i++; + + r=exp(); + + i++; + + }else{ + + r=0; + + while(s[i]>='0' && s[i]<='9') + + r=r*10+s[i++]-'0'; + + } + + + + return r*semn; + +} + + + +int main(){ + + fin>>s; + + fout< + + +using namespace std; + + + +ifstream fin( "evaluare.in" ); + +ofstream fout( "evaluare.out" ); + + + +string s; + + + +deque var; + +deque op; + + + +void Read() { + + fin >> s; + + fin.close(); + +} + + + +bool is_operator( char c ) { + + return strchr( "+-*/", c ); + +} + + + +int Operate( int a, int b, char op ) { + + if( op == '+' ) return a + b; + + if( op == '-' ) return a - b; + + if( op == '*' ) return a * b; + + if( op == '/' ) return a / b; + +} + + + +int Priority( char ch ) { + + if( ch == '(' ) return 0; + + if( ch == '+' || ch == '-' ) return 1; + + if( ch == '/' || ch == '*' ) return 2; + +} + + + +void Do() { + + for( int i = 0; i < s.size(); ++i ) { + + if( s[i] == '(' ) { op.push_back( '(' ); continue; } + + if( isdigit( s[i] ) ) { + + int aux = 0; + + + + while( i < s.size() && isdigit( s[i] ) ) { + + aux = aux * 10 + s[i] - '0'; + + ++i; + + } + + --i; + + + + var.push_back( aux ); + + continue; + + } + + if( is_operator( s[i] ) ) { + + while( !op.empty() && Priority( op.back() ) >= Priority( s[i] ) ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.push_back( s[i] ); + + continue; + + } + + if( s[i] == ')' ) { + + while( op.back() != '(' ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.pop_back(); + + } + + + + } + + + + while( !op.empty() ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + + + fout << var.back() << '\n'; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/eval/stack/017359bd-e832-4044-8f55-772e79a51b73.cpp b/eval/stack/017359bd-e832-4044-8f55-772e79a51b73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcd6856b88d5f1b9021bd81623015e6dd2165bbf --- /dev/null +++ b/eval/stack/017359bd-e832-4044-8f55-772e79a51b73.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char x[100001]; +int i, lg; + +int nr(); +int prod(); +int suma(); + +int main() +{ + fin >> x; + lg = strlen(x); + fout << suma(); + return 0; +} + +int nr() +{ + int r = 0; + if (x[i] == '(') + { + i++; + r = suma(); + i++; + } + else + { + while (isdigit(x[i])) + { + r = r * 10 + x[i] - 48; + i++; + } + } + return r; +} + +int produs() +{ + int r = nr(); + while (i +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +struct type + +{ + + union + + { + + int i; + + char c; + + }; + + char t; + +}; + + + +queue v; + +stack op; + + + +int main() + +{ + + char c[100000]; + + f>>c; + + char *p=c; + + type e; + + while(*p!='\00') + + { + + if(*p>='0' && *p<='9') + + { + + int x=0; + + while(*p>='0' && *p<='9') + + x=x*10+*p-'0',++p; + + e.t='i'; + + e.i=x; + + v.push(e); + + } + + else + + { + + if(op.empty()) + + op.push(*p); + + else + + switch(*p) + + { + + case '*': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '/': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '+': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '-': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '(': + + op.push(*p); + + break; + + case ')': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.pop(); + + break; + + } + + ++p; + + } + + } + + while(!op.empty()) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + stack st; + + queue aux=v; + + while(!aux.empty()) + + { + + if(aux.front().t=='i') + + cout< +using namespace std; + + +int precedence(char op) +{ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + return 0; +} + +int applyOp(int a, int b, char op) +{ + switch(op) + { + case '+': + return a + b; + case '-': + return a - b; + case '*': + return a * b; + case '/': + return a / b; + } + return 0; +} + +bool is_operator(char to_check) +{ + return to_check=='+' || to_check=='-' ||to_check=='*' ||to_check=='/'; + +} + + +bool is_digit(char x) +{ + return x>='0' && x<='9'; + +} + +int get_next_number(const string&x,unsigned&cursor) +{ + int res=0; + + while(is_digit(x[cursor]) && cursor values; +stack ops; + +void apply_last_operation() +{ + int a=values.top(); + values.pop(); + int b=values.top(); + values.pop(); + + char op=ops.top(); + + int c=applyOp(b,a,op); + + + values.push(c); + + ops.pop(); +} + + + +void eval() +{ + + while(ops.top()!='(') + { + apply_last_operation(); + } + ops.pop(); + +} + + + +int evaluate(string tokens) +{ + for(unsigned i=0; i=precedence(op)) + { + apply_last_operation(); + } + ops.push(op); + } + + + + } + + while(!ops.empty()) + { + apply_last_operation(); + } + return values.top(); + +} + +int main() +{ + ifstream in("evaluare.in"); + string a; + in>>a; + ofstream out("evaluare.out"); + out< +#include +using namespace std; + + + +const int NMAX = 100005; + +int n; + +char s[NMAX]; + +int st[NMAX], top; + + + +inline bool isDigit(char ch) + +{ + + return '0' <= ch && ch <= '9'; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> (s + 1); + + n = strlen(s + 1); + + s[0] = '('; + + s[n + 1] = ')'; + + ++n; + + for (int i = 0;i <= n;++i) + + { + + if (s[i] == '+') + + continue; + + else if (s[i] == '-') + + st[++top] = 1000000002; + + else if (s[i] == '*') + + st[++top] = 1000000003; + + else if (s[i] == '/') + + st[++top] = 1000000004; + + else if (s[i] == '(') + + st[++top] = 1000000001; + + else if (s[i] == ')') + + { + + int x = 0; + + while (top > 0 && st[top] != 1000000001) + + x += st[top--]; + + --top; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + else + + { + + int x = 0; + + while (i <= n && isDigit(s[i])) + + x = x * 10 + (s[i++] - '0'); + + --i; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + } + + fout << st[1] << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/054d031f-df3f-4dc3-a69c-b6ffb09a6439.cpp b/eval/stack/054d031f-df3f-4dc3-a69c-b6ffb09a6439.cpp new file mode 100644 index 0000000000000000000000000000000000000000..284eb99f3dbb3b2e818b427d472488484b4ced02 --- /dev/null +++ b/eval/stack/054d031f-df3f-4dc3-a69c-b6ffb09a6439.cpp @@ -0,0 +1,403 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +class StackException + +{ + +private: + + std::string m_error; + + + +public: + + StackException(std::string error) + + : m_error(error) + + { + + } + + + + const char* getError() + + { + + return m_error.c_str(); + + } + +}; + +template + +struct Element + +{ + + Element* next; + + T data; + +}; + +template + +class Stack + +{ + +private: + + Element *top; + +public: + + Stack(); + + Stack(vector newElements); + + Stack(const Stack &S); + + void Push(T newElement); + + T Pop(); + + T Top() const; + + bool isEmpty() const; + + void printStack(); + +}; + +template + +Stack::Stack() + +{ + + top=NULL; + +} + +template + +Stack::Stack(vector newElements) + +{ + + top=NULL; + + for(auto ¤tElement:newElements) + + Push(currentElement); + +} + +template + +Stack::Stack(const Stack &S) + +{ + + top=NULL; + + vector stackElements; + + int numberStackElements=0; + + Element *currentElement=S.top; + + while(currentElement!=NULL) + + { + + stackElements.push_back(currentElement->data); + + currentElement=currentElement->next; + + numberStackElements++; + + } + + for(int i=numberStackElements-1;i>=0;--i) + + Push(stackElements[i]); + +} + +template + +void Stack::Push(T newElement) + +{ + + Element *newTop=new Element; + + if(!newTop) + + throw StackException("Stack Overflow"); + + newTop->data=newElement; + + newTop->next=top; + + top=newTop; + +} + +template + +T Stack::Top() const + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + return top->data; + +} + +template + +T Stack::Pop() + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + Element *temp=top; + + T topData=top->data; + + top=top->next; + + temp->next=NULL; + + free(temp); + + return topData; + +} + +template + +bool Stack::isEmpty() const + +{ + + return (top==NULL); + +} + +template + +void Stack::printStack() + +{ + + if(top==NULL) + + throw StackException("Stack has no elements"); + + Element *currentElement=top; + + while(currentElement!=NULL) + + { + + cout<data<<" "; + + currentElement=currentElement->next; + + } + +} + +int applyOperator(int x, int y, char Operator) + +{ + + switch(Operator) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int priority(char Operator) + +{ + + switch(Operator) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + } + + return 0; + +} + +int DijkstraAlgorithm(string expression) + +{ + + Stack Values; + + Stack Operators; + + for(std::string::iterator it=expression.begin(); it!=expression.end(); ++it) + + { + + char currentCharacter=*it; + + if(currentCharacter==' ') + + continue; + + if(isdigit(currentCharacter)) + + { + + int value=0; + + for(;it!=expression.end()&&isdigit(*it); ++it) + + value=value*10+(*it-'0'); + + --it; + + Values.Push(value); + + continue; + + } + + if(currentCharacter=='(') + + { + + Operators.Push(currentCharacter); + + continue; + + } + + if(currentCharacter==')') + + { + + while(!Operators.isEmpty()&&Operators.Top()!='(') + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + if(!Operators.isEmpty()) + + Operators.Pop(); + + continue; + + } + + int Operator=*it; + + while(!Operators.isEmpty()&&priority(Operator)<=priority(Operators.Top())) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + Operators.Push(Operator); + + } + + while(!Operators.isEmpty()) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + return Values.Top(); + +} + +void evaluateExpression() + +{ + + string expression; + + getline(fin,expression); + + fout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include + + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +char op[100005],s[100005]; +int nr[100005],nr_top,n,op_top; + +int hai(int a, int b, char c) +{ + if(c=='+') + return a+b; + else if(c=='*') + return a*b; + else if(c=='-') + return a-b; + else if(c=='/') + return a/b; + else + return -1; +} + +bool semn(char c) +{ + if(c=='+' || c=='-' || c=='*' || c=='/') + return true; + return false; +} + +bool cifra(char c) +{ + if(c<='9' && c>='0') + return true; + return false; +} + +int prioritate(char c) +{ + if(c=='+' || c=='-') + return 0; + if(c=='*' || c=='/') + return 1; + return -1; +} + +int main() { + cin.getline(s,100005); + n=(int)strlen(s); + op[0]='('; + s[n]=')'; + s[n+1]='\0'; + for(int i=0;i<=n;i++) + if(cifra(s[i])) + { + int x=0; + do + { + x=x*10+(int)(s[i]-'0'); + i++; + } + while (cifra(s[i])); + i--; + nr[++nr_top]=x; + } + else if(s[i]=='(') + op[++op_top]=s[i]; + else if(s[i]==')') + { + while(op[op_top]!='(') + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr_top--; + nr[nr_top]=x; + op_top--; + } + op_top--; + } + else if(semn(s[i])) + { + while(op_top>0 && prioritate(s[i])<=prioritate(op[op_top])) + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr[--nr_top]=x; + op_top--; + } + op[++op_top]=s[i]; + } + cout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string tokens; + +int n; + +stack ops; + +stack nr; + +int prioritate(char c) + +{ + + if (c == '-'||c == '+') + + return 1; + + if (c == '*'||c == '/') + + return 2; + + return 0; + +} + +int culcal(int a,int b,char c) + +{ + + switch(c){ + + case '+': return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/': return a/b; + + } + +} + +int evalexp() + +{ + + int i = 0; + + int val1,val2,val; + + n = tokens.size(); + + while (i != n) + + { + + if (tokens[i] == ' ') + + continue; + + if (tokens[i] == '(') ops.push(tokens[i]); + + else if (isdigit(tokens[i])) + + { + + val = 0; + + while(tokens[i] != '\n' && isdigit(tokens[i])) + + { + + val = val * 10 + (tokens[i] - '0'); + + i ++; + + } + + i --; + + nr.push(val); + + } + + else if (tokens[i] == ')') + + { + + while(!ops.empty() && ops.top()!='(') + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + } + + if (!ops.empty()) + + ops.pop(); + + } + + else{ + + char op,op1; + + op = tokens[i]; + + while (!ops.empty() && prioritate(op) <= prioritate(ops.top())) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + op1 = ops.top(); + + ops.pop(); + + nr.push(culcal(val1,val2,op1)); + + + + } + + ops.push(op); + + } + + i++; + + } + + while(!ops.empty()) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + if (!ops.empty()) + + if (ops.top() == '(') ops.pop(); + + } + + return nr.top(); + +} + +int main() + +{ + + getline(fin,tokens); + + fout << evalexp() << '\n'; + + return 0; + +} diff --git a/eval/stack/07410e30-ca38-4099-b9de-dd89c825a010.cpp b/eval/stack/07410e30-ca38-4099-b9de-dd89c825a010.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5dfb4aa5c59fe883032dc56380b9c5918c244977 --- /dev/null +++ b/eval/stack/07410e30-ca38-4099-b9de-dd89c825a010.cpp @@ -0,0 +1,91 @@ +#include + + + + +using namespace std; +typedef long long ll; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +int n, m, i, j, dg; +char s[200005]; +stack val; +stack op; +int fa(int x, char semn, int y) +{ + if(semn=='+') + return x+y; + else if(semn=='-') + return y-x; + else if(semn=='*') + return x*y; + else return y/x; +} +int pred(char semn) +{ + if(semn=='*'||semn=='/') + return 2; + else if(semn=='+'||semn=='-')return 1; + return 0; +} +int main() +{ + fin>>s; + n=strlen(s); + for(i=0;i='0'&&s[i]<='9') + { + if(dg==-1) + dg=0; + dg=dg*10+s[i]-'0'; + continue; + } + if(dg>-1) + { + val.push(dg); + dg=-1; + } + if(s[i]=='(') + op.push('('); + else if(s[i]==')') + { + while(op.top()!='(') + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.pop(); + } + else + { + while(!op.empty()&&pred(op.top())>=pred(s[i])) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.push(s[i]); + } + } + if(dg>-1) + val.push(dg); + while(!op.empty()) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + cout< +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +struct my_timer{ + chrono::time_point start; + my_timer(): + start(chrono::high_resolution_clock::now()){} + ~my_timer(){ + std::chrono::duration diff = chrono::high_resolution_clock::now() - start; + clog< val; + stack op; + int c=getchar(); + while(c!=EOF && c != '\n'){ + if(isdigit(c)){ + int num = c&15; + while(isdigit(c=getchar())){ + num = (num << 3) + (num << 1) + (c & 15); + } + val.emplace(num); + if(c==EOF || c=='\n') + break; + } + if(c==')'){ + while(op.top() != '('){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.pop(); + } + else{ + while(!op.empty() && op.top() != '(' && ord[op.top()] >= ord[c]){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.emplace(c); + } + c=getchar(); + } + while(!op.empty()){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + while(!val.empty()){ + if(val.front() & (1ll << 62)){ + ll a = op.top(); + op.pop(); + switch(val.front() & (~(1ll<<62))){ + case '+': + op.top() += a; + break; + case '-': + op.top() -= a; + break; + case '*': + op.top() *= a; + break; + case '/': + op.top() /= a; + break; + default : + return 1; + } + }else{ + op.emplace(val.front()); + } + val.pop(); + } + printf("%lld",op.top()); + } +} diff --git a/eval/stack/080ab5c6-a17c-4983-ad26-e5fbd7d0bb11.cpp b/eval/stack/080ab5c6-a17c-4983-ad26-e5fbd7d0bb11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c893198b0a3867618d8031d86267c27f0146051 --- /dev/null +++ b/eval/stack/080ab5c6-a17c-4983-ad26-e5fbd7d0bb11.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +std::string expr; + +int pos = 0; + +bool issign(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +int eval() { + std::vector stack; + + std::string number; + + char prev_sign = '+'; + int value; + + if (expr[pos] == '-') { + ++pos; + prev_sign = '-'; + } + + for (; pos < expr.size(); ++pos) { + if (isdigit(expr[pos])) { + number += expr[pos]; + } else if (issign(expr[pos])) { + if (!number.empty()) + value = std::stoi(number); + + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + prev_sign = expr[pos]; + } else if (expr[pos] == '(') { + ++pos; + value = eval(); + } else if (expr[pos] == ')') { + + if (!number.empty()) + value = std::stoi(number); + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + break; + } + } + + int ans = 0; + for (int el : stack) + ans += el; + + return ans; +} + +void read() { + std::ifstream in("evaluare.in"); + + std::getline(in, expr); + expr += '+'; + + in.close(); +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << eval() << '\n'; + + out.close(); + return 0; +} diff --git a/eval/stack/09842b3d-d5e1-48ab-ad88-0b43feab5bf4.cpp b/eval/stack/09842b3d-d5e1-48ab-ad88-0b43feab5bf4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..625b4745d81b413173ebad2491aec5d721d8c410 --- /dev/null +++ b/eval/stack/09842b3d-d5e1-48ab-ad88-0b43feab5bf4.cpp @@ -0,0 +1,226 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include +#include + +using namespace std; + +inline int app_op ( int x , int y , char op ) +{ + if ( op == '+' ) return x + y ; + if ( op == '-' ) return y - x ; + if ( op == '*' ) return x * y ; + if ( op == '/' ) return y / x ; + return 0 ; +} + +inline int priority ( char op ) +{ + if ( op == '+' || op == '-' ) return 1 ; + if ( op == '*' || op == '/' ) return 2 ; + return 0 ; +} + +void solve ( void ) +{ + stack < char > operatori ; + stack < int > operanzi ; + char ch , op ; + int nr = 0 , x1 , x2 , ok = 0 ; + + while ( scanf ( "%c" , & ch ) != EOF ) + { + if ( isdigit ( ch ) ) + nr = nr * 10 + ( int ) ( ch - '0' ) , ok = 1 ; + else + { + if ( ok ) + { + operanzi.push ( nr ) ; + nr = 0 ; + ok = 0 ; + } + + if ( ch == '(' ) + operatori.push ( '(' ) ; + else + if ( ch == ')' ) + { + while ( operatori.top () != '(' ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + operatori.pop () ; + } + else + if ( ch == '+' || ch == '-' || ch == '*' || ch == '/' ) + { + while ( ! operatori.empty () && priority ( ch ) <= priority ( operatori.top () ) ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + operatori.push ( ch ) ; + } + } + } + + while ( ! operatori.empty () ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + printf ( "%d" , operanzi.top () ) ; +} + +int main() +{ + freopen ( "evaluare.in" , "r" , stdin ) ; + freopen ( "evaluare.out" , "w" , stdout ) ; + + solve () ; + + return 0; +} diff --git a/eval/stack/0b40fa01-8987-4485-b6e4-48ecdf7a4c75.cpp b/eval/stack/0b40fa01-8987-4485-b6e4-48ecdf7a4c75.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbe2fd8102e2cafe537a2357ab74c86678e76382 --- /dev/null +++ b/eval/stack/0b40fa01-8987-4485-b6e4-48ecdf7a4c75.cpp @@ -0,0 +1,313 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< + + + + +using namespace std; +typedef long long ll; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +int n, m, i, j, dg; +char s[200005]; +stack val; +stack op; +int fa(int x, char semn, int y) +{ + if(semn=='+') + return x+y; + else if(semn=='-') + return y-x; + else if(semn=='*') + return x*y; + else return y/x; +} +int pred(char semn) +{ + if(semn=='*'||semn=='/') + return 2; + else if(semn=='+'||semn=='-')return 1; + return 0; +} +int main() +{ + fin>>s; + n=strlen(s); + for(i=0;i='0'&&s[i]<='9') + { + if(dg==-1) + dg=0; + dg=dg*10+s[i]-'0'; + continue; + } + if(dg>-1) + { + val.push(dg); + dg=-1; + } + if(s[i]=='(') + op.push('('); + else if(s[i]==')') + { + while(op.top()!='(') + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.pop(); + } + else + { + while(!op.empty()&&pred(op.top())>=pred(s[i])) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.push(s[i]); + } + } + if(dg>-1) + val.push(dg); + while(!op.empty()) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + cout< +#include + + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +char op[100005],s[100005]; +int nr[100005],nr_top,n,op_top; + +int hai(int a, int b, char c) +{ + if(c=='+') + return a+b; + else if(c=='*') + return a*b; + else if(c=='-') + return a-b; + else if(c=='/') + return a/b; + else + return -1; +} + +bool semn(char c) +{ + if(c=='+' || c=='-' || c=='*' || c=='/') + return true; + return false; +} + +bool cifra(char c) +{ + if(c<='9' && c>='0') + return true; + return false; +} + +int prioritate(char c) +{ + if(c=='+' || c=='-') + return 0; + if(c=='*' || c=='/') + return 1; + return -1; +} + +int main() { + cin.getline(s,100005); + n=(int)strlen(s); + op[0]='('; + s[n]=')'; + s[n+1]='\0'; + for(int i=0;i<=n;i++) + if(cifra(s[i])) + { + int x=0; + do + { + x=x*10+(int)(s[i]-'0'); + i++; + } + while (cifra(s[i])); + i--; + nr[++nr_top]=x; + } + else if(s[i]=='(') + op[++op_top]=s[i]; + else if(s[i]==')') + { + while(op[op_top]!='(') + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr_top--; + nr[nr_top]=x; + op_top--; + } + op_top--; + } + else if(semn(s[i])) + { + while(op_top>0 && prioritate(s[i])<=prioritate(op[op_top])) + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr[--nr_top]=x; + op_top--; + } + op[++op_top]=s[i]; + } + cout< +#include +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char x[100001]; +int i, lg; + +int nr(); +int prod(); +int suma(); + +int main() +{ + fin >> x; + lg = strlen(x); + fout << suma(); + return 0; +} + +int nr() +{ + int r = 0; + if (x[i] == '(') + { + i++; + r = suma(); + i++; + } + else + { + while (isdigit(x[i])) + { + r = r * 10 + x[i] - 48; + i++; + } + } + return r; +} + +int produs() +{ + int r = nr(); + while (i +#include + + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + +char op[100005],s[100005]; +int nr[100005],nr_top,n,op_top; + +int hai(int a, int b, char c) +{ + if(c=='+') + return a+b; + else if(c=='*') + return a*b; + else if(c=='-') + return a-b; + else if(c=='/') + return a/b; + else + return -1; +} + +bool semn(char c) +{ + if(c=='+' || c=='-' || c=='*' || c=='/') + return true; + return false; +} + +bool cifra(char c) +{ + if(c<='9' && c>='0') + return true; + return false; +} + +int prioritate(char c) +{ + if(c=='+' || c=='-') + return 0; + if(c=='*' || c=='/') + return 1; + return -1; +} + +int main() { + cin.getline(s,100005); + n=(int)strlen(s); + op[0]='('; + s[n]=')'; + s[n+1]='\0'; + for(int i=0;i<=n;i++) + if(cifra(s[i])) + { + int x=0; + do + { + x=x*10+(int)(s[i]-'0'); + i++; + } + while (cifra(s[i])); + i--; + nr[++nr_top]=x; + } + else if(s[i]=='(') + op[++op_top]=s[i]; + else if(s[i]==')') + { + while(op[op_top]!='(') + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr_top--; + nr[nr_top]=x; + op_top--; + } + op_top--; + } + else if(semn(s[i])) + { + while(op_top>0 && prioritate(s[i])<=prioritate(op[op_top])) + { + int x=hai(nr[nr_top-1], nr[nr_top],op[op_top]); + nr[--nr_top]=x; + op_top--; + } + op[++op_top]=s[i]; + } + cout< + + +using namespace std; + + + +ifstream si("evaluare.in"); + +ofstream so("evaluare.out"); + + + +long long op[100001],q[100001],n,i,nr,nr2,x; + +char s[100001]; + + + +void calc(int a,int b) + +{ + + if(op[b]=='+') + + { + + q[a-1]+=q[a]; + + } + + if(op[b]=='-') + + { + + q[a-1]-=q[a]; + + } + + if(op[b]=='*') + + { + + q[a-1]*=q[a]; + + } + + if(op[b]=='/') + + { + + q[a-1]/=q[a]; + + } + + return; + + + +} + + + +int gr(char c) + +{ + + if(c=='+' or c=='-') + + { + + return 1; + + } + + if(c=='*' or c=='/') + + { + + return 2; + + } + + return 0; + +} + + + +int main() + +{ + + si.getline(s,100001); + + n=strlen(s); + + for(i=0; i + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char infix[100000 + 1]; + + + +int evaluate_basic(int A, int B, char c) + +{ + + if(c == '*') return A * B; + + if(c == '/') return A / B; + + if(c == '+') return A + B; + + if(c == '-') return A - B; + + + + return -1; + +} + + + + + +int operator_priority(char c) + +{ + + if(c == '+' || c == '-') return 1; + + if(c == '*' || c == '/') return 2; + + + + return -1; + +} + + + +int get_operand(int &i) + +{ + + int operand = 0; + + + + while(infix[i] >= '0' && infix[i] <= '9' && infix[i] != '\0') + + { + + operand = operand * 10 + (infix[i] - '0'); + + ++i; + + } + + + + i--; + + + + + + return operand; + +} + + + +int evaluate() + +{ + + stack operators; + + vector operands; + + + + + + + + for(int i = 0; infix[i] != '\0'; i++) + + { + + if(infix[i] >= '0' && infix[i] <= '9') operands.push_back(get_operand(i)); + + else if(infix[i] == '(') operators.push(infix[i]); + + else + + { + + while(!operators.empty() + + && operators.top() != '(' + + && operator_priority(infix[i]) <= operator_priority(operators.top())) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + + + + if(infix[i]==')') operators.pop(); + + else operators.push(infix[i]); + + } + + + + + + } + + + + while(!operators.empty()) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + return operands[0]; + +} + + + + + +int main() + +{ + + fin.getline(infix, 100000 + 1, '\n'); + + fout << evaluate(); + + + +} diff --git a/eval/stack/0fc7ee77-cce9-49b9-b961-dab58acc7944.cpp b/eval/stack/0fc7ee77-cce9-49b9-b961-dab58acc7944.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46711f87da1ce58b82386c22eecf9e6c92e422a2 --- /dev/null +++ b/eval/stack/0fc7ee77-cce9-49b9-b961-dab58acc7944.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +stack ev; + +vector v; + +char c[100005], t; + +int i, n, nr, p; + + + + + +int putere(char c) + +{ + + switch(c) + + { + + case '+': return 1; + + case '-': return 1; + + + + case '*': return 2; + + case '/': return 2; + + + + case '(': return 0; + + } + +} + +int tip(char c) + +{ + + switch(c) + + { + + case '-': return -1; + + case '+': return -2; + + case '*': return -3; + + case '/': return -4; + + } + +} + +int numar() + +{ + + int nr=0; + + while('0'<=c[i]&&c[i]<='9') + + { + + nr=nr*10+c[i]-'0'; + + i++; + + } + + return nr; + +} + +int main() + +{ + + fin.getline(c, 100005); + + n=strlen(c); + + for(i=0;i=p) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + s.push(c[i]); + + } + + } + + } + + + + while(!s.empty()) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + + + for(i=0;i +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +stack sign; + +stack num; + + + +char s[100001], t[150001]; + + + +inline int Precedence(char ch) + +{ if (ch=='*' || ch=='/') + + return 2; + + return 1; + +} + + + +int main() + +{ int j, i, x, y; + + cin>>s; + + cin.close(); + + j=-1; + + for (i=0;s[i];++i) + + if (isdigit(s[i])) + + { while (isdigit(s[i])) + + { t[++j]=s[i]; + + ++i; + + } + + t[++j]=' '; + + --i; + + } + + else if (s[i]=='(') + + sign.push('('); + + else if (s[i]==')') + + { while (sign.top()!='(') + + { t[++j]=sign.top(); + + sign.pop(); + + } + + sign.pop(); + + } + + else + + { while (!sign.empty() && sign.top()!='(' && Precedence(s[i])<=Precedence(sign.top())) + + { t[++j]=sign.top(); + + sign.pop(); + + } + + sign.push(s[i]); + + } + + while (!sign.empty()) + + { t[++j]=sign.top(); + + sign.pop(); + + } + + + + for (i=0;t[i];++i) + + if (isdigit(t[i])) + + { x=0; + + while (isdigit(t[i])) + + { x=x*10+t[i]-'0'; + + ++i; + + } + + num.push(x); + + --i; + + } + + else if (t[i]!=' ') + + { x=num.top(); + + num.pop(); + + y=num.top(); + + num.pop(); + + if (t[i]=='*') + + num.push(x*y); + + else if (t[i]=='/') + + num.push(y/x); + + else if (t[i]=='+') + + num.push(x+y); + + else + + num.push(y-x); + + } + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + +long doOperation(char op, long left, long right) { + + switch( op ) { + + case '+': return left+right; + case '-': return left-right; + case '*': return left*right; + case '/': return (left/right); + + default: { + return 0; + } + + } +} + + +void computeNextInStack(stack & numberStack, stack & operatorStack) { + + char op = operatorStack.top(); + operatorStack.pop(); + + long right = numberStack.top(); + numberStack.pop(); + + long left = numberStack.top(); + numberStack.pop(); + + long result = doOperation(op, left, right); + + numberStack.push( result ); +} + +unsigned int priority(char op) { + + switch( op ) { + + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + default: + return 0; + } +} + + +long parseExpression(string const expression) { + + stack numberStack; + stack operatorStack; + + + string::const_iterator pos = expression.begin(); + + + while(pos != expression.end()) { + + char current = *pos; + + switch(current) { + + case '+': + case '-': + case '*': + case '/': + + { + + while( + !operatorStack.empty() && + + priority(operatorStack.top()) >= priority(current) + ) + + { + computeNextInStack(numberStack, operatorStack); + } + + operatorStack.push(current); + + break; + } + + + + case '(': { + + operatorStack.push(current); + + break; + } + + + case ')': { + + if(operatorStack.empty()) { + + + + return 0; + } + + while(operatorStack.top() != '(') { + + computeNextInStack(numberStack, operatorStack); + + + if(operatorStack.empty()) { + + + + return 0; + } + } + + operatorStack.pop(); + break; + } + + + + default: { + + if(isdigit(current)) { + + + long number = 0; + + while( pos != expression.end() && isdigit(*pos)) { + + number *= 10; + number += (*pos)-'0'; + pos++; + + } + + + numberStack.push(static_cast(number)); + + pos--; + + } else { + + } + + break; + } + } + + pos++; + } + + while(!operatorStack.empty()) { + + computeNextInStack(numberStack, operatorStack); + } + + return numberStack.top(); +} + + +int main(int argc, char const *argv[]) +{ + + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string expression; + + getline(fin, expression); + + long result = parseExpression(expression); + + fout< +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +short Imp(char op) + +{ + + if (op == '+' || op == '-') + + return 1; + + if (op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int Operatie(int a, int b, char op) + +{ + + if (op == '+')return a + b; + + if (op == '-')return a - b; + + if (op == '*')return a * b; + + if (op == '/')return a / b; + +} + + + +int eval(string s) + +{ + + stack t; + + stackop; + + for (int i = 0; i < s.size(); i++) + + { + + + + if (s[i] == '(') + + op.push(s[i]); + + else if (isdigit(s[i])) + + { + + int val = 0; + + while (i < s.size() && isdigit(s[i])) { + + val = val * 10 + s[i] - '0'; + + i++; + + } + + t.push(val); + + i--; + + } + + else if (s[i] == ')') + + { + + while (!op.empty() && op.top() != '(') + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + if (!op.empty())op.pop(); + + } + + else + + { + + + + while (!op.empty() && Imp(op.top()) >= Imp(s[i])) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + op.push(s[i]); + + } + + } + + + + while (!op.empty()) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + + + } + + return t.top(); + +} + + + +int main() + +{ + + string s; + + fin >> s; + + fout << eval(s); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/17e4233c-30f3-430c-9a56-26b393d41618.cpp b/eval/stack/17e4233c-30f3-430c-9a56-26b393d41618.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6c92c49dd5e4e21feaa7a8492c034984b7ccec7 --- /dev/null +++ b/eval/stack/17e4233c-30f3-430c-9a56-26b393d41618.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],semne[100005],semne1[100005]; + +int h,stiva[100005],sol[100005],k,h1; + +int main() + +{ + + f>>s; + + int nr=0; + + for(int i=0; s[i]; i++) + + { + + if(s[i]=='('||s[i]==')'||s[i]=='*'||s[i]=='+'||s[i]=='-'||s[i]=='/') + + { + + if(s[i-1]!=')'&&s[i]!='(') + + stiva[++h]=nr; + + nr=0; + + semne1[++k]=s[i]; + + if(s[i]=='+'||s[i]=='-') + + while(semne1[k-1]!='('&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]=='*'||s[i]=='/') + + while(semne1[k-1]!='('&&semne1[k-1]!='+'&&semne1[k-1]!='-'&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]==')') + + { + + while(semne1[k-1]!='(') + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + k-=2; + + } + + } + + else + + nr=nr*10+s[i]-'0'; + + } + + if(s[strlen(s)-2]!=')'&&s[strlen(s)-1]!=')') + + stiva[++h]=nr; + + while(semne1[k]) + + semne[++h]=semne1[k],k--; + + for(int i=1; i<=h; i++) + + { + + if(semne[i]) + + { + + switch(semne[i]) + + { + + case '+': + + sol[h1-1]=sol[h1-1]+sol[h1]; + + break; + + case '-': + + sol[h1-1]=sol[h1-1]-sol[h1]; + + break; + + case '*': + + sol[h1-1]=sol[h1-1]*sol[h1]; + + break; + + case '/': + + sol[h1-1]=sol[h1-1]/sol[h1]; + + break; + + + + } + + h1--; + + } + + else + + sol[++h1]=stiva[i]; + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string s; + + + +inline int Precedence(char ch) + +{ + + if (ch == '^') + + return 3; + + if (ch == '*' || ch == '/') + + return 2; + + return 1; + +} + + + +inline void InfixToPostfix() + +{ + + int i; + + string t; + + stack st; + + for (i = 0; s[i]; ++i) + + if (isdigit(s[i]) || (s[i]=='-' && (!i || s[i-1]=='(') && isdigit(s[i+1]))) + + { + + if (s[i] == '-') + + { + + t.push_back('-'); + + ++i; + + } + + while (isdigit(s[i])) + + { + + t.push_back(s[i]); + + ++i; + + } + + t.push_back(' '); + + --i; + + } + + else if (s[i] == '(') + + st.push('('); + + else if (s[i] == ')') + + { + + while (st.top() != '(') + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + st.pop(); + + } + + else + + { + + while (!st.empty() && st.top() != '(' && Precedence(s[i]) <= Precedence(st.top())) + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + st.push(s[i]); + + } + + while (!st.empty()) + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + s = t; + +} + + + +inline int Power(int b, int e) + +{ + + int ans; + + ans = 1; + + while (e) + + { + + if (e % 2) + + ans = ans * b; + + b = b * b; + + e /= 2; + + } + + return ans; + +} + + + +inline int Calc(int a, int b, char op) + +{ + + if (op == '^') + + return Power(a, b); + + if (op == '*') + + return a * b; + + if (op == '/') + + return a / b; + + if (op == '+') + + return a + b; + + return a - b; + +} + + + +inline int Evaluate() + +{ + + int i, sign, x; + + vector st; + + for (i = 0; s[i]; ++i) + + if (isdigit(s[i]) || (s[i]=='-' && isdigit(s[i+1]))) + + { + + if (s[i] == '-') + + { + + sign = -1; + + ++i; + + } + + else + + sign = 1; + + x = 0; + + while (isdigit(s[i])) + + { + + x = x * 10 + s[i] - '0'; + + ++i; + + } + + st.push_back(sign * x); + + --i; + + } + + else if (s[i] != '(' && s[i] != ')' && s[i]!=' ') + + + + { + + x = Calc(st[st.size() - 2], st[st.size() - 1], s[i]); + + st.pop_back(), st.pop_back(); + + st.push_back(x); + + } + + return st.back(); + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(nullptr), cout.tie(nullptr); + + cin >> s; + + cin.close(); + + InfixToPostfix(); + + cout << Evaluate(); + + cout.close(); + + + + return 0; + +} diff --git a/eval/stack/1a2c05f5-794f-4d6c-bf62-711fa52272ec.cpp b/eval/stack/1a2c05f5-794f-4d6c-bf62-711fa52272ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eae227f1fd351ea43328327c54328aa485767dea --- /dev/null +++ b/eval/stack/1a2c05f5-794f-4d6c-bf62-711fa52272ec.cpp @@ -0,0 +1,194 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack nums; + +stack ops; + +int pri[260]; + + + +void setpri(); + +int getNum(char*&s); + +void eval(char* s); + +void exec(char &op); + + + +int main(){ + + setpri(); + + char s[100005]; + + fin>>s; + + eval(s); + + fout<=pri[op]){ + + exec(ops.top()); + + ops.pop(); + + } + + ops.push(op); + + s++; + + } + + } + + while(!ops.empty()){ + + exec(ops.top()); + + ops.pop(); + + } + +} + + + +void exec(char &op){ + + int rez = 0; + + int x1 = nums.top(); + + nums.pop(); + + int x2 = nums.top(); + + nums.pop(); + + switch (op){ + + case '+': + + rez = x1+x2; + + break; + + case '-': + + rez = x2-x1; + + break; + + case '*': + + rez = x1*x2; + + break; + + case '/': + + rez = x2/x1; + + break; + + case '^': + + rez = pow(x2,x1); + + break; + + default: + + break; + + } + + nums.push(rez); + +} diff --git a/eval/stack/1c8e0ffd-1bfd-402c-bf69-ea0c60439aa6.cpp b/eval/stack/1c8e0ffd-1bfd-402c-bf69-ea0c60439aa6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99f4d669dc57a436ae3aaf2fda234fffb840bd99 --- /dev/null +++ b/eval/stack/1c8e0ffd-1bfd-402c-bf69-ea0c60439aa6.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char e[100005]; +int n; + +int priority(char o){ + if(o == '+' || o == '-') + return 1; + if(o == '*' || o == '/') + return 2; + return 0; +} + +int compute(int a, int b, int o){ + if(o == '+') + return a + b; + if(o == '-') + return a - b; + if(o == '*') + return a * b; + if(o == '/') + return a / b; +} + +int eval(){ + int i = 0; + stack op; + stack val; + while(i < n){ + if(e[i] == '('){ + op.push(e[i]); + } + else if(e[i] == ')'){ + while(!op.empty() && op.top() != '('){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.pop(); + } + else if(isdigit(e[i])){ + int v = 0; + while(i < n && isdigit(e[i])){ + v = (v * 10) + (e[i] - '0'); + ++i; + } + --i; + val.push(v); + } + else{ + while(!op.empty() && priority(op.top()) >= priority(e[i])){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.push(e[i]); + } + ++i; + } + while(!op.empty()){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + return val.top(); +} + +int main(){ + + fin.getline(e, 100005); + n = strlen(e); + fout << eval(); + fin.close(); + fout.close(); + return 0; +} diff --git a/eval/stack/1d2edfaf-09d0-47dd-b2b6-a39ad3d34ecc.cpp b/eval/stack/1d2edfaf-09d0-47dd-b2b6-a39ad3d34ecc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..759040ad11428f228a0d7cbc58448baae634decd --- /dev/null +++ b/eval/stack/1d2edfaf-09d0-47dd-b2b6-a39ad3d34ecc.cpp @@ -0,0 +1,282 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string mes; + +string post; + +int i; + +stack nr; + +stack op; + + + +long long calc(long long nr1,long long nr2,char operatie) + +{ + + + + switch(operatie){ + + case '+': + + return nr1+nr2; + + case '-': + + return nr1-nr2; + + case '*': + + return nr1*nr2; + + case '/': + + return nr1/nr2; + + case '^': + + return pow(nr1,nr2); + + } + + return 0; + +} + + + +int prec(char ch) + +{ + + switch(ch) + + { + + case '+': + + case '-': + + return 1; + + case '*': + + case '/': + + return 2; + + case '^': + + return 3; + + case '(': + + return 0; + + } + + return 0; + +} + + + +int main() + +{ + + fin>>mes; + + + + while(i= prec(mes[i])) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + op.push(mes[i]); + + } + + } + + + + i++; + + } + + + + while(!op.empty() ) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + + + + + i = 0; + + long long sol = 0; + + while(i < post.size()) + + { + + if(isdigit(post[i])) + + { + + long long numar = 0; + + while(isdigit(post[i])) + + { + + numar = numar * 10 + (post[i]-'0'); + + i++; + + } + + nr.push(numar); + + + + } + + else if(post[i] == '+' || post[i] == '*' || post[i] == '-' || post[i] == '^' || post[i] == '/') + + { + + + + long long nr1 = nr.top(); + + nr.pop(); + + long long nr2 = nr.top(); + + nr.pop(); + + long long res = calc(nr2,nr1,post[i]); + + nr.push(res); + + + + } + + + + i++; + + } + + + + fout< +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char expr[100001]; + +map priority; + + + +struct elts { + + int val; + + int type; + +}; + + + +bool isDigit(char c) { + + return (c >= '0' && c <= '9'); + +} + + + +int getNumber(int& i) { + + int ret = 0; + + while (expr[i] != '\0' && isDigit(expr[i])) + + ret = ret * 10 + expr[i] - '0', ++i; + + --i; + + return ret; + +} + + + +bool isOperator(char c) { + + return (c == '+' || c == '-' || c == '*' || c == '/'); + +} + + + +int getRes(int a, int b, char op) { + + if (op == '+') + + return a + b; + + if (op == '-') + + return a - b; + + if (op == '*') + + return a * b; + + if (op == '/') + + return a / b; + +} + + + +int compute() { + + stack op; + + vector val; + + for (int i = 0;expr[i] != '\0';++i) { + + if (isDigit(expr[i])) + + val.push_back({ getNumber(i),0 }); + + else if (isOperator(expr[i])) { + + while (!op.empty() && op.top() != '(' && priority[op.top()] >= priority[expr[i]]) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.push(expr[i]); + + } + + else if (expr[i] == '(') + + op.push('('); + + else { + + while (op.top() != '(') { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.pop(); + + } + + + + } + + while (!op.empty()) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + + + stack res; + + + + for (int i = 0;i < val.size();++i) { + + if (val[i].type == 0) + + res.push(val[i].val); + + else { + + int a = res.top(); + + res.pop(); + + int b = res.top(); + + res.pop(); + + res.push(getRes(b, a, val[i].val)); + + } + + } + + return res.top(); + +} + + + +int main() + +{ + + fin >> expr; + + priority['+'] = 0; + + priority['-'] = 0; + + priority['*'] = 1; + + priority['/'] = 1; + + fout << compute(); + + + + return 0; + +} diff --git a/eval/stack/1f795ce7-8da9-4148-bcab-59f2124b2ba2.cpp b/eval/stack/1f795ce7-8da9-4148-bcab-59f2124b2ba2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5c3922abb522f09a1bf484e5e65e2693d140539 --- /dev/null +++ b/eval/stack/1f795ce7-8da9-4148-bcab-59f2124b2ba2.cpp @@ -0,0 +1,259 @@ +#include +#include +#include + + +std::ifstream in("evaluare.in"); + +std::ofstream out("evaluare.out"); + + + + + +int Priority(char x) + +{ + + if (x == '+' || x == '-') + + return 1; + + if (x == '*' || x == '/') + + return 2; + + + + return 0; + +} + + + + + + + +int ApplyOperator(int a, int b, char op) + +{ + + switch (op) + + { + + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + } + +} + + + +int Evaluate(std::string& Stream) + +{ + + int i; + + + + + + std::stack Valori; + + + + + + std::stack Operatori; + + + + int n = Stream.length(); + + + + for (i = 0; i < n; i++) + + { + + if (Stream[i] == '(') + + { + + Operatori.push(Stream[i]); + + } + + + + + + else if (isdigit(Stream[i])) + + { + + int numar = 0; + + + + while (i < Stream.length() && isdigit(Stream[i])) + + { + + numar = (numar * 10) + (Stream[i] - '0'); + + i++; + + } + + i--; + + + + Valori.push(numar); + + } + + + + + + else if (Stream[i] == ')') + + { + + + + while(!Operatori.empty() && Operatori.top() != '(') + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + + + Operatori.pop(); + + } + + + + else + + { + while (!Operatori.empty() && Priority(Operatori.top()) >= Priority(Stream[i])) + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + Operatori.push(Stream[i]); + + } + + + + } + + + + while (!Operatori.empty()) + + { + + + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + return Valori.top(); + +} + + + +int main() + +{ + + std::string Stream; + + in >> Stream; + + out << Evaluate(Stream); + +} diff --git a/eval/stack/200adaf7-cdd8-4dac-a75e-2f723f3032fb.cpp b/eval/stack/200adaf7-cdd8-4dac-a75e-2f723f3032fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad25faddab6e77e25df08fad3cb1cfca19814e34 --- /dev/null +++ b/eval/stack/200adaf7-cdd8-4dac-a75e-2f723f3032fb.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in ("evaluare.in"); +ofstream out ("evaluare.out"); + +stack s; +deque < pair > fin; +stack r; + + + + + + + +int main() +{ + char c,aux='a'; + long long cnt=0,nr=0,x1,x2,rez; + while (in.get(c)&&c!='\n') + { + if (isdigit(c)&&isdigit(aux)) + nr=nr*10+c-48; + else if (isdigit(c)&&!isdigit(aux)) + nr=c-48; + else if (!isdigit(c)&&isdigit(aux)) + fin.push_back({nr,0}); + if (!isdigit(c)) + { + if (c=='+'||c=='-') + { + while (!s.empty()&&s.top()) + fin.push_back({s.top(),1}),s.pop(); + if (c=='+') + s.push(2); + else + s.push(1); + } + else if (c=='*'||c=='/') + { + while (!s.empty()&&s.top()>=3) + fin.push_back({s.top(),1}),s.pop(); + if (c=='*') + s.push(3); + else + s.push(4); + } + else if (c=='(') + s.push(0); + else + { + while (!s.empty()&&s.top()!=0) + fin.push_back({s.top(),1}),s.pop(); + s.pop(); + } + } + aux=c; + } + if (isdigit(aux)) + fin.push_back({nr,0}); + while (!s.empty()) + fin.push_back({s.top(),1}),s.pop(); + for (int i=0;i +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +string s; + +int i,steve[100001],cnt,signsteve[100001],cnt1,steva[100001],contor; + +int formnr() + +{ + + int rez=0; + + for(; i='0'&&s[i]<='9'; i++) + + rez=rez*10+s[i]-'0'; + + i--; + + steve[++cnt]=rez; + +} + +void codif() + +{ + + for(i=0; i='0'&&s[i]<='9') + + formnr(); + + else if(s[i]=='+'||s[i]=='-') + + { + + while(cnt1&&signsteve[cnt1]<=-2) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + if(s[i]=='+') + + signsteve[++cnt1]=-2; + + else + + signsteve[++cnt1]=-3; + + } + + else if(s[i]=='/'||s[i]=='*') + + { + + while(cnt1&&signsteve[cnt1]<=-4) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + if(s[i]=='*') + + signsteve[++cnt1]=-4; + + else + + signsteve[++cnt1]=-5; + + } + + else + + { + + if(s[i]=='(') + + signsteve[++cnt1]=-1; + + else + + { + + while(cnt1&&signsteve[cnt1]!=-1) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + cnt1--; + + } + + } + + } + + while(cnt1) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + +} + +int main() + +{ + + in>>s; + + codif(); + + for(i=1; i<=cnt; i++) + + { + + if(steve[i]<=-1) + + { + + if(steve[i]==-2) + + { + + steva[contor-1]+=steva[contor]; + + contor--; + + } + + else if(steve[i]==-3) + + { + + steva[contor-1]-=steva[contor]; + + contor--; + + } + + else if(steve[i]==-4) + + { + + steva[contor-1]*=steva[contor]; + + contor--; + + } + + else if(steve[i]==-5) + + { + + steva[contor-1]/=steva[contor]; + + contor--; + + } + + } + + else + + steva[++contor]=steve[i]; + + } + + out< +#include +#include +using namespace std; + + + +const int N = 100005; + + + +string a; + +int st[N], top; + + + +void Read () + +{ + + ifstream fin ("evaluare.in"); + + fin >> a; + + fin.close(); + +} + + + +void Solve () + +{ + + int i; + + for (i = 0; a[i];) + + { + + if (a[i] == '+') + + i++; + + else if (a[i] == '(') + + { + + st[++top] = 1e9 + 1; + + i++; + + } + + else if (a[i] == '*') + + { + + st[++top] = 1e9 + 2; + + i++; + + } + + else if (a[i] == '/') + + { + + st[++top] = 1e9 + 3; + + i++; + + } + + else if (a[i] == '-') + + { + + st[++top] = 1e9 + 4; + + i++; + + } + + else if ('0' <= a[i] && a[i] <= '9') + + { + + int x = 0; + + while ('0' <= a[i] && a[i] <= '9') + + { + + x = x * 10 + a[i] - '0'; + + i++; + + } + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + else + + { + + i++; + + int x = 0; + + while (st[top] != 1e9 + 1) + + { + + x += st[top]; + + top--; + + } + + top--; + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + } + + int ans = 0; + + for (int i = 1; i <= top; i++) + + ans += st[i]; + + ofstream fout ("evaluare.out"); + + fout << ans << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + return 0; + +} diff --git a/eval/stack/21ee4f9c-2ab0-44a1-96f4-23a3e052a10e.cpp b/eval/stack/21ee4f9c-2ab0-44a1-96f4-23a3e052a10e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae800ddfa6148c9e7b0698bac33e138cd6b80caf --- /dev/null +++ b/eval/stack/21ee4f9c-2ab0-44a1-96f4-23a3e052a10e.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +const int NMAX = 100004; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack < int > S; + +stack < char > Op; + + + +char C[NMAX]; + + + +int pi = 2000000000, x; + +int pl = pi+1; + +int m = pi-1; + +int sup = pi-2; + +int ori = pi+2; + +int N; + + + +int NR( int &i ) + +{ + + int nr = 0; + + while( C[i] >= '0' && C[i] <= '9' ) + + { + + nr = nr*10 + C[i]-'0'; + + i++; + + } + + i--; + + return nr; + +} + + + +int Priority( char op ) + +{ + + if( op == '*' || op == '/' ) + + return 2; + + if( op == '+' || op == '-' ) + + return 1; + + return 0; + +} + + + +int ApplyOp( int x, int y, char op ) + +{ + + switch( op ) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +void Solve() + +{ + + fin >> C; + + N = strlen( C ); + + + + for( int i = 0; C[i]; ++i ) + + { + + + + if( C[i] == '(' ) + + { + + Op.push( C[i] ); + + continue; + + } + + if( C[i] >= '0' && C[i] <= '9' ) + + S.push( NR( i ) ); + + + + else + + if( C[i] == ')' ) + + { + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + + + } + + else + + { + + while( Op.size() && Priority( Op.top() ) >= Priority( C[i] ) ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + + + Op.pop(); + + } + + Op.push( C[i] ); + + } + + } + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + fout << S.top(); + +} + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/eval/stack/22478885-ee30-4583-af36-7dcec7515f4d.cpp b/eval/stack/22478885-ee30-4583-af36-7dcec7515f4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5158bedf4e9510b5d386adff51475952279d3d54 --- /dev/null +++ b/eval/stack/22478885-ee30-4583-af36-7dcec7515f4d.cpp @@ -0,0 +1,187 @@ +#include +#include +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +const int N = 300001; + +char c[N]; + +int stiva [100001], vf; + +int prio(char x) + +{ + + if(x == '(' or x == ')') + + return 0; + + if(x == '+' or x == '-') + + return 1; + + return 2; + +} + +int operatie( int x, int y , char semn) + +{ + + switch(semn) + + { + + case '+': return x + y; + + case '-': return x - y; + + case '*': return x * y; + + case '/': return x / y; + + } + +} + +int eval(char []); + +void postfix(char[]); + +int main() + +{ + + cin >> c; + + postfix(c); + + cout << eval(c); + + return 0; + +} + +void postfix(char x[]) + +{ + + int poz = 0; + + int n = strlen(x); + + char aux[N]= {0}; + + for( int i =0 ;i < n ;i ++) + + { + + if(x[i] >='0' and x[i]<='9') + + aux[poz++]=x[i]; + + else + + { + + if(i > 0 and x[i-1]>='0' and x[i -1]<='9') + + aux[poz++]= ' '; + + if(x[i] == '(') + + stiva[vf++]= x[i]; + + else + + if(x[i] == ')') + + { + + while(stiva[vf - 1] !='(') + + aux[poz++] = stiva[--vf]; + + vf--; + + } + + else + + { + + while(vf != 0 and prio(x[i]) <= prio(stiva[vf- 1])) + + aux[poz ++]= stiva[--vf]; + + stiva[vf++] = x[i]; + + } + + + + } + + } + + if(aux[poz - 1] >='0' and aux[poz - 1] <='9') + + aux[poz++] = ' '; + + while(vf != 0) + + { + + + + aux[poz++] = stiva[--vf]; + + } + + strcpy(x, aux); + +} + +int eval(char x[]) + +{ + + int n = strlen(x); + + int numar = 0; + + for( int i =0 ; i < n ;i++) + + if(x[i] >='0' and x[i]<='9') + + numar = numar * 10 +x[i] -'0'; + + else + + if(x[i] == ' ') + + { + + stiva[vf++] = numar; + + numar = 0; + + } + + else + + { + + int nr2 = stiva[--vf], nr1= stiva[--vf]; + + stiva[vf++] = operatie(nr1,nr2, x[i]); + + } + + return stiva[vf - 1]; + +} diff --git a/eval/stack/22da37fe-9eb5-482b-8853-6e4a60bc7e57.cpp b/eval/stack/22da37fe-9eb5-482b-8853-6e4a60bc7e57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e75e53198c5b4628ee2c3e13c5baa9cca5f7ad5 --- /dev/null +++ b/eval/stack/22da37fe-9eb5-482b-8853-6e4a60bc7e57.cpp @@ -0,0 +1,146 @@ +#include +using namespace std; + + +int precedence(char op) +{ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + return 0; +} + +int applyOp(int a, int b, char op) +{ + switch(op) + { + case '+': + return a + b; + case '-': + return a - b; + case '*': + return a * b; + case '/': + return a / b; + } + return 0; +} + +bool is_operator(char to_check) +{ + return to_check=='+' || to_check=='-' ||to_check=='*' ||to_check=='/'; + +} + + +bool is_digit(char x) +{ + return x>='0' && x<='9'; + +} + +int get_next_number(const string&x,unsigned&cursor) +{ + int res=0; + + while(is_digit(x[cursor]) && cursor values; +stack ops; + +void apply_last_operation() +{ + int a=values.top(); + values.pop(); + int b=values.top(); + values.pop(); + + char op=ops.top(); + + int c=applyOp(b,a,op); + + + values.push(c); + + ops.pop(); +} + + + +void eval() +{ + + while(ops.top()!='(') + { + apply_last_operation(); + } + ops.pop(); + +} + + + +int evaluate(string tokens) +{ + for(unsigned i=0; i=precedence(op)) + { + apply_last_operation(); + } + ops.push(op); + } + + + + } + + while(!ops.empty()) + { + apply_last_operation(); + } + return values.top(); + +} + +int main() +{ + ifstream in("evaluare.in"); + string a; + in>>a; + ofstream out("evaluare.out"); + out< +#include +#include +using namespace std; + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + +string s; + +int num[100007],st[100007]; + +int pd=-1000000001,pi=-1000000002,plu=-1000000003,minu=-1000000004,inm=-1000000005,imp=-1000000006; + +int main() + +{ + + in>>s; + + int n=s.length(); + + int i=0,f=0,k=0; + + int nr=0; + + while(i=0) + + st[++k]=num[i]; + + else + + { + + if(num[i]==inm) + + { + + if(k<2) + + st[k]=0; + + else + + { + + st[--k]*=st[k+1]; + + } + + } + + else if(num[i]==imp) + + { + + st[--k]/=st[k+1]; + + } + + else if(num[i]==plu) + + { + + st[--k]+=st[k+1]; + + } + + else if(num[i]==minu) + + { + + st[--k]-=st[k+1]; + + } + + } + + } + + out< +#include +#include +#include +#include +using namespace std; + +const int NMAX=100005; + +int op[NMAX],polo[NMAX]; + +int top_op,top_polo; + +int pr(char ch) + +{ + + switch(ch) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + default: return 0; + + } + +} + +void eval() + +{ + + if(op[top_op]=='+') + + polo[top_polo-1]=polo[top_polo-1]+polo[top_polo]; + + if(op[top_op]=='-') + + polo[top_polo-1]=polo[top_polo-1]-polo[top_polo]; + + if(op[top_op]=='*') + + polo[top_polo-1]=polo[top_polo-1]*polo[top_polo]; + + if(op[top_op]=='/') + + polo[top_polo-1]=polo[top_polo-1]/polo[top_polo]; + + top_op--; + + top_polo--; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + int nr; + + char ch; + + scanf("%c",&ch); + + while(isdigit(ch) || ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='(' || ch==')') + + { + + if(isdigit(ch)) + + { + + nr=0; + + while(isdigit(ch)) + + { + + nr=nr*10+(ch-'0'); + + scanf("%c",&ch); + + } + + polo[++top_polo]=nr; + + } + + else + + { + + if(top_op==0 || ch=='(' || pr(op[top_op])=pr(ch)) + + eval(); + + op[++top_op]=ch; + + } + + } + + scanf("%c",&ch); + + } + + } + + while(top_op) + + eval(); + + printf("%d",polo[1]); + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/eval/stack/2474192e-7fb8-406e-b846-0af4f3fd1687.cpp b/eval/stack/2474192e-7fb8-406e-b846-0af4f3fd1687.cpp new file mode 100644 index 0000000000000000000000000000000000000000..290546dfda09deeccc71d43e8ad1da7740401401 --- /dev/null +++ b/eval/stack/2474192e-7fb8-406e-b846-0af4f3fd1687.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +using namespace std; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, string cur) { + if (cur == "+") + return a + b; + if (cur == "-") + return a - b; + if (cur == "*") + return a * b; + if (cur == "/") + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + + +int evaluare_polandeza(vector &output) { + stack eval; + for (int i = 0; i < (int) output.size(); i++) { + string cur = output[i]; + if (!is_operator(cur[0])) { + eval.push(stoi(cur)); + } else { + int a = eval.top(); + eval.pop(); + int b = eval.top(); + eval.pop(); + int r = result(b, a, cur); + eval.push(r); + } + } + return eval.top(); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + int res = evaluare_polandeza(output); + fout << res << "\n"; + + return 0; +} diff --git a/eval/stack/252fe25b-43cc-419e-a49f-e179132344cd.cpp b/eval/stack/252fe25b-43cc-419e-a49f-e179132344cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbf31c336072827ea478cb509c30c3589d449424 --- /dev/null +++ b/eval/stack/252fe25b-43cc-419e-a49f-e179132344cd.cpp @@ -0,0 +1,216 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +stack stac; + +bool isPriority(char a,char b) + +{ + + if((b=='+' || b=='-') && (a=='*' || a=='/')) + + return true; + + if((b=='+' || b=='-') && (a=='+' || a=='-')) + + return true; + + if((b=='*' || b=='/') && (a=='*' || a=='/')) + + return true; + + return false; + +} + +bool isDigit(char a) + +{ + + if(a>='0'&& a<='9') + + return true; + + return false; + +} + +string infixToPostFix(string a) + +{ + + string res; + + for(int i = 0; i< a.size(); i++) + + { + + if(isDigit(a[i])) + + { + + res+=a[i]; + + if(i!=a.size()-1) + + { + + if(!isDigit(a[i+1])) + + res+=" "; + + } + + else + + { + + res+=" "; + + } + + } + + else + + { + + if(a[i]==')') + + { + + while(stac.size() && stac.top()!='(') + + res+=stac.top(),stac.pop(); + + if(stac.size()) + + stac.pop(); + + } + + else + + { + + while(stac.size() && isPriority(stac.top(),a[i])) + + { + + res+=stac.top(); + + stac.pop(); + + } + + stac.push(a[i]); + + } + + } + + } + + while(stac.size()) + + res+=stac.top(),stac.pop(); + + return res; + +} + +stack numere; + +long long solvePostFix(string a) + +{ + + long long b=0; + + for(int i = 0;i>a; + + fout< +using namespace std; + +char s[100001]; + +int nr, k1, k2, rez, i, pr[300]; + +char st1[100001]; + +int st2[100001]; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +int operatie(int a, int b, char sgn) + +{ + + if (sgn == '+') { + + return a + b; + + } else if (sgn == '*') { + + return a * b; + + } else if (sgn == '/') { + + return a / b; + + } else if (sgn == '-') { + + return a - b; + + } + + + + return 0; + +} + + + +int main() { + + fin >> s; + + pr['+'] = 1; + + pr['-'] = 1; + + pr['('] = 0; + + pr[')'] = 0; + + pr['*'] = 2; + + pr['/'] = 2; + + + + for (int i = 0; s[i] != 0; i++) { + + if (s[i] >= '0' && s[i] <= '9') { + + nr = 0; + + while (s[i] >= '0' && s[i] <= '9') { + + nr = nr * 10 + s[i] - '0'; + + i++; + + } + + i--; + + st2[++k2] = nr; + + } else if (s[i] == '(') { + + st1[++k1] = '('; + + } else if (s[i] == ')') { + + while (st1[k1] != '(') { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + k1--; + + } else { + + while (k1 > 0 && pr[st1[k1]] >= pr[s[i]]) { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + st1[++k1] = s[i]; + + } + + } + + + + while (k1 > 0) { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + fout << st2[1]; + + return 0; + +} diff --git a/eval/stack/25b190eb-8b64-480c-ac5c-519691f99c97.cpp b/eval/stack/25b190eb-8b64-480c-ac5c-519691f99c97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c172cb1c5954047269c9c928e935cc52932c3416 --- /dev/null +++ b/eval/stack/25b190eb-8b64-480c-ac5c-519691f99c97.cpp @@ -0,0 +1,273 @@ +#include +#include + + + + +using namespace std; + + + +struct solComp + +{ + + int num; + + char c; + +}; + + + +int solve(string s) + +{ + + vector sol; + + vector stiva; + + + + int n = s.length(), m, i; + + + + for(i = 0; i < n; i++) + + { + + if(s[i] == '(')stiva.push_back(s[i]); + + else if(s[i] == '*' || s[i] == '/') + + { + + m = stiva.size() - 1; + + while(m >= 0 && (stiva[m] == '*' || stiva[m] == '/') && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == '+' || s[i] == '-') + + { + + m = stiva.size() - 1; + + while(m >= 0 && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == ')') + + { + + m = stiva.size() - 1; + + while(stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + stiva.pop_back(); + + } + + else + + { + + int number = s[i] - '0'; + + + + for(i = i + 1; i < n && '0' <= s[i] && s[i] <= '9'; i++)number = number * 10 + s[i] - '0'; + + + + i--; + + solComp aux; + + + + aux.num = number; + + aux.c = 0; + + + + sol.push_back(aux); + + } + + } + + + + m = stiva.size() - 1; + + + + while(m >= 0) + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + + + m--; + + } + + + + n = sol.size(); + + + + for(i = 0; i < n; i++) + + if(sol[i].num == -1) + + { + + if(sol[i].c == '/')stiva[m - 1] = stiva[m - 1] / stiva[m]; + + else if(sol[i].c == '*')stiva[m - 1] = stiva[m - 1] * stiva[m]; + + else if(sol[i].c == '+')stiva[m - 1] = stiva[m - 1] + stiva[m]; + + else stiva[m - 1] = stiva[m - 1] - stiva[m]; + + + + m--; + + + + } + + else + + { + + m++; + + stiva[m] = sol[i].num; + + } + + + + return stiva[0]; + +} + + + +int main() + +{ + + string s; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + cin >> s; + + + + cout << solve(s); + + + + return 0; + +} diff --git a/eval/stack/25dfadfe-824a-410a-baaf-63ee3d944c69.cpp b/eval/stack/25dfadfe-824a-410a-baaf-63ee3d944c69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae800ddfa6148c9e7b0698bac33e138cd6b80caf --- /dev/null +++ b/eval/stack/25dfadfe-824a-410a-baaf-63ee3d944c69.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +const int NMAX = 100004; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack < int > S; + +stack < char > Op; + + + +char C[NMAX]; + + + +int pi = 2000000000, x; + +int pl = pi+1; + +int m = pi-1; + +int sup = pi-2; + +int ori = pi+2; + +int N; + + + +int NR( int &i ) + +{ + + int nr = 0; + + while( C[i] >= '0' && C[i] <= '9' ) + + { + + nr = nr*10 + C[i]-'0'; + + i++; + + } + + i--; + + return nr; + +} + + + +int Priority( char op ) + +{ + + if( op == '*' || op == '/' ) + + return 2; + + if( op == '+' || op == '-' ) + + return 1; + + return 0; + +} + + + +int ApplyOp( int x, int y, char op ) + +{ + + switch( op ) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +void Solve() + +{ + + fin >> C; + + N = strlen( C ); + + + + for( int i = 0; C[i]; ++i ) + + { + + + + if( C[i] == '(' ) + + { + + Op.push( C[i] ); + + continue; + + } + + if( C[i] >= '0' && C[i] <= '9' ) + + S.push( NR( i ) ); + + + + else + + if( C[i] == ')' ) + + { + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + + + } + + else + + { + + while( Op.size() && Priority( Op.top() ) >= Priority( C[i] ) ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + + + Op.pop(); + + } + + Op.push( C[i] ); + + } + + } + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + fout << S.top(); + +} + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/eval/stack/25e357a5-f40e-4393-a95e-b19af3f3a58e.cpp b/eval/stack/25e357a5-f40e-4393-a95e-b19af3f3a58e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2382eb802feb85f27b368ae6be8e1e481a41ee67 --- /dev/null +++ b/eval/stack/25e357a5-f40e-4393-a95e-b19af3f3a58e.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +using namespace std; +ifstream f ("evaluare.in"); +ofstream g ("evaluare.out"); +char buff [5 + (int)1e6]; +short x [300]; +stack st , ans; +vector eval; +void solve (int semn){ + int b = ans . top (); + ans . pop (); + int a = ans . top (); + ans . pop (); + if (semn == -3) + ans . push (a * b); + if (semn == -4) + ans . push (a / b); + if (semn == -5) + ans . push (a + b); + if (semn == -6) + ans . push (a - b); + return; + +} +bool check (int x , int y){ + if (x >= y) + return true; + if (x == -6 && y == -5) + return true; + if (x == -4 && y == -3) + return true; + return false; +} +int main() +{ + x ['+'] = -5 , x ['-'] = -6 , x ['*'] = -3 , x ['/'] = -4 , x ['('] = -1 , x [')'] = -2; + f >> buff; + int a; + for(int i = 0 ; buff [i] ; i){ + if (isdigit (buff [i])){ + a = 0; + while (isdigit (buff [i])) + a = a * 10 + (buff [i ++] - '0'); + eval . push_back (a); + } + else{ + int val = x [buff [i]]; + if (val == -1) + st . push (val); + else + if (val == -2){ + while (0 < st . size () && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + if (0 < st . size () && st . top () == -1) + st . pop (); + } + else{ + while (0 < st . size () && check (st . top () , val) && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + st . push (val); + } + ++ i; + } + } + while (0 < st . size ()) + eval . push_back (st . top ()) , st . pop (); + for(auto x : eval){ + if (x < 0) + solve (x); + else + ans . push (x); + } + g << ans . top (); + return 0; +} diff --git a/eval/stack/26557572-8d0e-4aaa-9510-ce37cb3c7d98.cpp b/eval/stack/26557572-8d0e-4aaa-9510-ce37cb3c7d98.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c05684508a59adc9d73f3de900c4d731906fed6 --- /dev/null +++ b/eval/stack/26557572-8d0e-4aaa-9510-ce37cb3c7d98.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char ss[100001]; + + + +int main(){ + + + + f >> ss; + + stack st, ops; + + + + for(int i = 0;ss[i];){ + + + + if(isdigit(ss[i])){ + + int Nr = 0; + + while(isdigit(ss[i])) + + Nr = Nr * 10 + (ss[i] - '0'), i++; + + st.push(Nr); + + i--; + + } + + + + if(ss[i] == '+') {i++; ops.push(1); continue;} + + if(ss[i] == '-') {i++; ops.push(1); st.push(-1); ops.push(3); continue;} + + if(ss[i] == '*') {i++; ops.push(3); continue;} + + if(ss[i] == '/') {i++; ops.push(4); continue;} + + if(ss[i] == '(') {i++; ops.push(5); continue;} + + + + if(ss[i] == ')') { + + while(ops.top() != 5){ + + int Nr = st.top(); + + st.pop(); + + st.top() += Nr; + + ops.pop(); + + } + + ops.pop(); + + } + + + + + + while(!ops.empty() && (ops.top() == 3 || ops.top() == 4)){ + + + + int Nr = st.top(); + + st.pop(); + + if(ops.top() == 3) st.top() *= Nr; + + if(ops.top() == 4) st.top() /= Nr; + + ops.pop(); + + } + + + + i++; + + } + + + + long long answ = 0; + + while(!st.empty()){ + + answ += st.top(); + + st.pop(); + + ops.pop(); + + } + + + + g << answ; + + + +} diff --git a/eval/stack/26ab7b21-d32a-4c78-9983-27ad1ad28fcd.cpp b/eval/stack/26ab7b21-d32a-4c78-9983-27ad1ad28fcd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46711f87da1ce58b82386c22eecf9e6c92e422a2 --- /dev/null +++ b/eval/stack/26ab7b21-d32a-4c78-9983-27ad1ad28fcd.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +stack ev; + +vector v; + +char c[100005], t; + +int i, n, nr, p; + + + + + +int putere(char c) + +{ + + switch(c) + + { + + case '+': return 1; + + case '-': return 1; + + + + case '*': return 2; + + case '/': return 2; + + + + case '(': return 0; + + } + +} + +int tip(char c) + +{ + + switch(c) + + { + + case '-': return -1; + + case '+': return -2; + + case '*': return -3; + + case '/': return -4; + + } + +} + +int numar() + +{ + + int nr=0; + + while('0'<=c[i]&&c[i]<='9') + + { + + nr=nr*10+c[i]-'0'; + + i++; + + } + + return nr; + +} + +int main() + +{ + + fin.getline(c, 100005); + + n=strlen(c); + + for(i=0;i=p) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + s.push(c[i]); + + } + + } + + } + + + + while(!s.empty()) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + + + for(i=0;i + + +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int NMAX=100000; + +char c,semn[NMAX],st[NMAX]; + +int i,n,k,nr,gasitnr,fp[NMAX]; + +long long steval[100]; + + + +int main() + +{ + + while(in>>c){ + + if(c>='0'&&c<='9'){ + + gasitnr=1; + + nr=nr*10+(c-48); + + } + + else{ + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + if(c=='('||c=='+'||c=='-'||c=='*'||c=='/') + + { + + k++; + + st[k]=c; + + if(st[k]=='+'||st[k]=='-'){ + + while(st[k-1]=='+'||st[k-1]=='-'||st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + else + + if(st[k]=='*'||st[k]=='/'){ + + while(st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + } + + else + + if(c==')'){ + + while(st[k]!='('){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k--; + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + while(k){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k=0; + + for(i=1;i<=n;i++){ + + if(semn[i]!='+'&&semn[i]!='-'&&semn[i]!='*'&&semn[i]!='/'){ + + k++; + + steval[k]=fp[i]; + + } + + else + + { + + if(semn[i]=='+'){ + + steval[k-1]=steval[k-1]+steval[k]; + + } + + if(semn[i]=='-'){ + + steval[k-1]=steval[k-1]-steval[k]; + + } + + if(semn[i]=='*'){ + + steval[k-1]=steval[k-1]*steval[k]; + + } + + if(semn[i]=='/'){ + + steval[k-1]=steval[k-1]/steval[k]; + + } + + k--; + + } + + } + + out< +typedef unsigned long long ul; + +typedef long long ll; + +using namespace std; + + + +bool is_op(char c){ + + return (c == '+' or c == '-' or c == '*' or c == '/'); + +} + + + +int priority(char c){ + + if (c == '+' or c == '-') + + return 1; + + if (c == '*' or c == '/') + + return 2; + + return -1; + +} + + + +void process(stack &stk, char op){ + + int r = stk.top(); + + stk.pop(); + + int l = stk.top(); + + stk.pop(); + + + + if (op == '+') + + stk.push(l + r); + + else if (op == '-') + + stk.push(l - r); + + else if (op == '*') + + stk.push(l * r); + + else if (op == '/') + + stk.push(l / r); + +} + + + +int evaluare(string s){ + + stack val; + + stack op; + + + + for (int i = 0; i < s.length(); i++){ + + if (s[i] == '(') + + op.push('('); + + + + else if (s[i] == ')'){ + + while (op.top() != '('){ + + process(val, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + + + else if (is_op(s[i])){ + + char curr = s[i]; + + while(!op.empty() and priority(op.top()) >= priority(curr)){ + + process(val, op.top()); + + op.pop(); + + } + + op.push(curr); + + } + + + + else { + + int num = 0; + + while (i < s.length() and isdigit(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + val.push(num); + + } + + } + + + + while(!op.empty()){ + + process(val, op.top()); + + op.pop(); + + } + + + + int ans = val.top(); + + val.pop(); + + return ans; + +} + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + + + string s; + + cin >> s; + + cout << evaluare(s); + + + + return 0; + +} diff --git a/eval/stack/29a9b95b-6983-4568-9e3f-3e9482be75a3.cpp b/eval/stack/29a9b95b-6983-4568-9e3f-3e9482be75a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..190424efced0547d0fd375b137627f904d3b6ccd --- /dev/null +++ b/eval/stack/29a9b95b-6983-4568-9e3f-3e9482be75a3.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int precedenta(char op) + +{ + + if(op == '+' || op == '-') + + return 1; + + if(op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int operatie(char op, int x, int y) + +{ + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + + + } + +} + + + +int eval(string sir) + +{ + + int i; + + stack val; + + stack ops; + + + + for(i=0; i= precedenta(sir[i])) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + ops.push(sir[i]); + + } + + } + + while(!ops.empty()) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + return val.top(); + +} + + + +int main() + +{ + + string sir; + + fin>>sir; + + fout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string tokens; + +int n; + +stack ops; + +stack nr; + +int prioritate(char c) + +{ + + if (c == '-'||c == '+') + + return 1; + + if (c == '*'||c == '/') + + return 2; + + return 0; + +} + +int culcal(int a,int b,char c) + +{ + + switch(c){ + + case '+': return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/': return a/b; + + } + +} + +int evalexp() + +{ + + int i = 0; + + int val1,val2,val; + + n = tokens.size(); + + while (i != n) + + { + + if (tokens[i] == ' ') + + continue; + + if (tokens[i] == '(') ops.push(tokens[i]); + + else if (isdigit(tokens[i])) + + { + + val = 0; + + while(tokens[i] != '\n' && isdigit(tokens[i])) + + { + + val = val * 10 + (tokens[i] - '0'); + + i ++; + + } + + i --; + + nr.push(val); + + } + + else if (tokens[i] == ')') + + { + + while(!ops.empty() && ops.top()!='(') + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + } + + if (!ops.empty()) + + ops.pop(); + + } + + else{ + + char op,op1; + + op = tokens[i]; + + while (!ops.empty() && prioritate(op) <= prioritate(ops.top())) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + op1 = ops.top(); + + ops.pop(); + + nr.push(culcal(val1,val2,op1)); + + + + } + + ops.push(op); + + } + + i++; + + } + + while(!ops.empty()) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + if (!ops.empty()) + + if (ops.top() == '(') ops.pop(); + + } + + return nr.top(); + +} + +int main() + +{ + + getline(fin,tokens); + + fout << evalexp() << '\n'; + + return 0; + +} diff --git a/eval/stack/2a0215c5-9aad-4984-921f-2162ff72fced.cpp b/eval/stack/2a0215c5-9aad-4984-921f-2162ff72fced.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c172cb1c5954047269c9c928e935cc52932c3416 --- /dev/null +++ b/eval/stack/2a0215c5-9aad-4984-921f-2162ff72fced.cpp @@ -0,0 +1,273 @@ +#include +#include + + + + +using namespace std; + + + +struct solComp + +{ + + int num; + + char c; + +}; + + + +int solve(string s) + +{ + + vector sol; + + vector stiva; + + + + int n = s.length(), m, i; + + + + for(i = 0; i < n; i++) + + { + + if(s[i] == '(')stiva.push_back(s[i]); + + else if(s[i] == '*' || s[i] == '/') + + { + + m = stiva.size() - 1; + + while(m >= 0 && (stiva[m] == '*' || stiva[m] == '/') && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == '+' || s[i] == '-') + + { + + m = stiva.size() - 1; + + while(m >= 0 && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == ')') + + { + + m = stiva.size() - 1; + + while(stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + stiva.pop_back(); + + } + + else + + { + + int number = s[i] - '0'; + + + + for(i = i + 1; i < n && '0' <= s[i] && s[i] <= '9'; i++)number = number * 10 + s[i] - '0'; + + + + i--; + + solComp aux; + + + + aux.num = number; + + aux.c = 0; + + + + sol.push_back(aux); + + } + + } + + + + m = stiva.size() - 1; + + + + while(m >= 0) + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + + + m--; + + } + + + + n = sol.size(); + + + + for(i = 0; i < n; i++) + + if(sol[i].num == -1) + + { + + if(sol[i].c == '/')stiva[m - 1] = stiva[m - 1] / stiva[m]; + + else if(sol[i].c == '*')stiva[m - 1] = stiva[m - 1] * stiva[m]; + + else if(sol[i].c == '+')stiva[m - 1] = stiva[m - 1] + stiva[m]; + + else stiva[m - 1] = stiva[m - 1] - stiva[m]; + + + + m--; + + + + } + + else + + { + + m++; + + stiva[m] = sol[i].num; + + } + + + + return stiva[0]; + +} + + + +int main() + +{ + + string s; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + cin >> s; + + + + cout << solve(s); + + + + return 0; + +} diff --git a/eval/stack/2b338088-968f-402c-8a59-e6e64635bf51.cpp b/eval/stack/2b338088-968f-402c-8a59-e6e64635bf51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5329efabcddfb36323c99922ee2c75854dfb13f --- /dev/null +++ b/eval/stack/2b338088-968f-402c-8a59-e6e64635bf51.cpp @@ -0,0 +1,238 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int l, nr, i; + +string s; + +stack obj; + +stack numb; + + + +void calc_nr(); + +void test_nr(); + + + +int main() + +{ + + + + fin >> s; + + l = s.length(); + + + + for(i = 0; i < l; ++i){ + + if(s[i] >= '0' && s[i] <= '9'){ + + calc_nr(); + + test_nr(); + + } + + else if(s[i] == ')'){ + + nr = numb.top(); + + numb.pop(); + + while(obj.top() != '('){ + + nr += numb.top(); numb.pop(); + + obj.pop(); + + } + + obj.pop(); + + + + test_nr(); + + } + + else obj.push(s[i]); + + } + + + + nr = numb.top(); numb.pop(); + + while(obj.size()){ + + nr += numb.top(); numb.pop(); + + obj.pop(); + + } + + + + numb.push(nr); + + + + fout << numb.top(); + + + + return 0; + +} + + + +void calc_nr(){ + + nr = 0; + + while(i < l && s[i] >= '0' && s[i] <= '9'){ + + nr = nr * 10 + (s[i] - '0'); + + ++i; + + } + + + + --i; + +} + + + +void test_nr(){ + + + + if(obj.size()){ + + if(obj.top() == '*'){ + + int other = numb.top(); numb.pop(); + + obj.pop(); + + numb.push(nr * other); + + } + + else if(obj.top() == '/'){ + + int other = numb.top(); numb.pop(); + + obj.pop(); + + numb.push(other / nr); + + } + + else if(obj.top() == '-'){ + + obj.pop(); obj.push('+'); + + numb.push(-nr); + + } + + else numb.push(nr); + + } + + else numb.push(nr); + + + +} diff --git a/eval/stack/302215bf-9a02-4a9e-aa28-c4fbcfe231c2.cpp b/eval/stack/302215bf-9a02-4a9e-aa28-c4fbcfe231c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffe681f6a23698eabef00a07ebb3f055fb702d25 --- /dev/null +++ b/eval/stack/302215bf-9a02-4a9e-aa28-c4fbcfe231c2.cpp @@ -0,0 +1,183 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else if (nr) + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/31b79758-7c33-49ef-b175-856bfe2348d6.cpp b/eval/stack/31b79758-7c33-49ef-b175-856bfe2348d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c7d0d1a3cf54c8451088be51376942007a37eff --- /dev/null +++ b/eval/stack/31b79758-7c33-49ef-b175-856bfe2348d6.cpp @@ -0,0 +1,219 @@ +#include + + +using namespace std; + + + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + + + +long long operatie[100001],q[100001],n,numIndex,opIndex,number; + +char s[100001]; +inline void readInput() + +{ + + f.getline(s, 100001); + + n = strlen(s); + +} + + + +int grad(char c) + +{ + + if(c == '+' || c == '-') return 1; + + if(c == '*' || c == '/') return 2; + + return 0; + +} + + + +void calculeaza(int a,int b) + +{ + + if(operatie[b]=='+') + + { + + q[a-1]+=q[a]; + + } + + if(operatie[b]=='-') + + { + + q[a-1]-=q[a]; + + } + + if(operatie[b]=='*') + + { + + q[a-1]*=q[a]; + + } + + if(operatie[b]=='/') + + { + + q[a-1]/=q[a]; + + } + + return; + +} + + + +inline void Solution() + +{ + + for(int i = 0 ; i < n ; ++ i) + + { + + if(isdigit(s[i])) + + { + + number = 0; + + while(isdigit(s[i])) + + { + + number *= 10; + + number += (s[i] - '0'); + + i++; + + } + + i--; + + q[++numIndex] = number; + + } + + else + + { + + if(opIndex == 0 || s[i] == '(' || grad(operatie[opIndex]) < grad(s[i])) + + { + + operatie[++ opIndex] = s[i]; + + } + + else if(s[i] == ')') + + { + + while(operatie[opIndex] != '(') + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + + opIndex --; + + } + + else + + { + + while(grad(s[i]) <= grad(operatie[opIndex])) + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + + operatie[++ opIndex] = s[i]; + + } + + } + + + + } + + while(opIndex) + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + +} + + + +inline void Output() + +{ + + g << q[1]; + +} + + + +int main() + +{ + + readInput(); + + Solution(); + + Output(); + + return 0; + +} diff --git a/eval/stack/325445e8-5fe2-4db9-8992-42e6cd3670a5.cpp b/eval/stack/325445e8-5fe2-4db9-8992-42e6cd3670a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8af849d314ce0b1cf5493bc99e02c8dae42f154 --- /dev/null +++ b/eval/stack/325445e8-5fe2-4db9-8992-42e6cd3670a5.cpp @@ -0,0 +1,235 @@ +#include +#include + + + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +int precedence(char op) + +{ + + if(op == '+'||op == '-') + + return 1; + + if(op == '*'||op == '/') + + return 2; + + return 0; + +} + + + +int opera(int a, int b, char op) + +{ + + if(op == '+') + + return a + b; + + if(op == '-' ) + + return a - b; + + if(op == '*') + + return a * b; + + if(op == '/') + + return a / b; + +} + + + +int eval(string token) + +{ + + int i; + + stack values; + + + + stack ops; + + + + for(i = 0 ; i < token.length() ; i++) + + { + + if(token[i] == '(') + + ops.push(token[i]); + + + + else if(isdigit(token[i])) + + { + + int val = 0; + + + + while(i < token.length() && + + isdigit(token[i])) + + { + + val = (val*10) + (token[i]-'0'); + + i++; + + } + + i--; + + values.push(val); + + } + + + + else if(token[i] == ')') + + { + + while(!ops.empty() && ops.top() != '(') + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + } + + ops.pop(); + + } + + + + + + else + + { + + while(!ops.empty() && precedence(ops.top()) >= precedence(token[i])) + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + } + + ops.push(token[i]); + + } + + } + + + + while(!ops.empty()) + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + + + } + + + + return values.top(); + +} + + + +int main() + +{ + + string token; + + getline(cin, token); + + cout << eval(token) ; + + return 0; + +} diff --git a/eval/stack/33ebf2ae-31ee-44f2-b0c7-f415d9bcff9d.cpp b/eval/stack/33ebf2ae-31ee-44f2-b0c7-f415d9bcff9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..609fc0220e98ddca2c214ed0e1d8b60019eafed8 --- /dev/null +++ b/eval/stack/33ebf2ae-31ee-44f2-b0c7-f415d9bcff9d.cpp @@ -0,0 +1,198 @@ +#include + + +using namespace std; + + + +ifstream si("evaluare.in"); + +ofstream so("evaluare.out"); + + + +long long op[100001],q[100001],n,i,nr,nr2,x; + +char s[100001]; + + + +void calc(int a,int b) + +{ + + if(op[b]=='+') + + { + + q[a-1]+=q[a]; + + } + + if(op[b]=='-') + + { + + q[a-1]-=q[a]; + + } + + if(op[b]=='*') + + { + + q[a-1]*=q[a]; + + } + + if(op[b]=='/') + + { + + q[a-1]/=q[a]; + + } + + return; + + + +} + + + +int gr(char c) + +{ + + if(c=='+' or c=='-') + + { + + return 1; + + } + + if(c=='*' or c=='/') + + { + + return 2; + + } + + return 0; + +} + + + +int main() + +{ + + si.getline(s,100001); + + n=strlen(s); + + for(i=0; i +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string s; + +int a[1001]; + +char o[1001]; + +int preced(char op) + +{ + + if(op=='+' or op=='-') + + return 1; + + if(op=='*' or op=='/') + + return 2; + + return 0; + +} + +int Calcul(int a,int b,char op) + +{ + + switch(op){case '+':return a+b; + + case '-':return a-b; + + case '*':return a*b; + + case '/':return a/b;} + +} + +int eval(string tokens){ + +int i;char op; + +stack values; + +stack ops; + +for(i=0;i=preced(tokens[i])) + + { + + int val2=values.top(); + + values.pop(); + + int val1=values.top(); + + values.pop(); + + char op=ops.top(); + + ops.pop(); + + values.push(Calcul(val1,val2,op)); + + } + + ops.push(tokens[i]); + + } + +} + +while(!ops.empty()) + +{ + + int val2=values.top(); + + values.pop(); + + int val1=values.top(); + + values.pop(); + + char op=ops.top(); + + ops.pop(); + + values.push(Calcul(val1,val2,op)); + +} + +return values.top(); + +} + +int main() + +{ + + getline(fin,s); + + fout << eval(s) << '\n'; + + return 0; + +} diff --git a/eval/stack/34792aba-12d7-4125-84ba-09547744f551.cpp b/eval/stack/34792aba-12d7-4125-84ba-09547744f551.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c396acf43ce83f0affad7c03104fc9ad26b83f --- /dev/null +++ b/eval/stack/34792aba-12d7-4125-84ba-09547744f551.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< + + + + + +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +void citire(); +int vf = 0; +int prio(char x) +{ + if( x=='(' || x == ')') + return 0; + if( x=='+' || x=='-') + return 1; + + return 2; +} +int operatie ( int a, int b, char c ) +{ + + switch (c) + { + case '+': return a+b; + case '-': return a-b; + case '*': return a*b; + case '/': return a/b; + + } +} +int eval(char []); +char x[300001]; +int s[100001]; +void postfix(char []); +void rez(); +void afis(); +int main() +{ + fin >> x; + postfix(x); + + + fout<< eval(x); +} +void postfix(char x[]) +{ + int poz = 0; + int N =strlen (x); + char aux[300001] = {0}; + for ( int i = 0 ; i < N ; i++) + { + if(isdigit(x[i])) + { + aux[poz++] = x[i] ; + } + else + { + if(i>0 && isdigit(x[i-1])) + aux[poz++]=' '; + if(x[i]=='(') + s[vf++]=x[i]; + else + if(x[i]==')') + { + while(s[vf-1]!='(') + { + aux[poz++]=s[--vf]; + } + vf--; + } + else + { + while(vf != 0 && prio(x[i]) <= prio(s[vf-1])) + { + aux[poz++]=s[--vf]; + } + s[vf++] = x[i]; + } + } + + + } + if(isdigit(aux[poz-1])) aux[poz++]=' '; + while(vf!=0) + { + aux[poz++] = s[--vf]; + } + + strcpy(x,aux); + +} +int eval( char x[]) +{ + int N =strlen(x); + int numar =0; + for( int i=0 ;i < N ;i ++) + { + if(isdigit(x[i])) numar = numar*10 +(x[i]-'0'); + else + if (x[i]== ' ') { + s[vf++]=numar; + numar=0; + } + else + { + int nr2=s[--vf], nr1= s[--vf]; + s[vf++] = operatie ( nr1 ,nr2 , x[i]); + } + } + return s[vf-1]; +} diff --git a/eval/stack/356eefa8-b9d0-4019-8cde-5510f7e754b1.cpp b/eval/stack/356eefa8-b9d0-4019-8cde-5510f7e754b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5329efabcddfb36323c99922ee2c75854dfb13f --- /dev/null +++ b/eval/stack/356eefa8-b9d0-4019-8cde-5510f7e754b1.cpp @@ -0,0 +1,238 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +stack operations; + +stack numbers; + +string expression; + + + +bool isOperator(char s) { + + if(s == '+' || s == '-' || s == '*' || s == '/') { + + return 1; + + } + + return 0; + +} + + + +int priority(char s) { + + if(s == '+' || s == '-') { + + return 0; + + } else if(s == '*' || s == '/') { + + return 1; + + } + + return -1; + +} + + + +int calculateOperation() { + + int a = numbers.top(); numbers.pop(); + + int b = numbers.top(); numbers.pop(); + + + + char op = operations.top(); operations.pop(); + + if(op == '+') { + + return a + b; + + } else if(op == '-') { + + return b - a; + + } else if(op == '/') { + + return b / a; + + } + + return a * b; + +} + + + +void prepare() { + + int i = 0, next_value; + + while(i < expression.length()) { + + if(isOperator(expression[i])) { + + while(operations.size() && priority(expression[i]) <= priority(operations.top())) { + + int next_value = calculateOperation(); + + numbers.push(next_value); + + } + + operations.push(expression[i]); + + i++; + + } else if(isdigit(expression[i])) { + + int integer = 0; + + while(expression[i] >= '0' && expression[i] <= '9' && i < expression.length()) { + + integer = integer * 10 + (expression[i] - '0'); + + i++; + + } + + numbers.push(integer); + + } else if(expression[i] == '(') { + + operations.push(expression[i]); + + i++; + + } else if(expression[i] == ')') { + + while(operations.top() != '(') { + + next_value = calculateOperation(); + + numbers.push(next_value); + + } + + i++; + + operations.pop(); + + } + + } + +} + + + +int calculateExpression() { + + while(operations.size()) { + + int next_value = calculateOperation(); + + numbers.push(next_value); + + } + + return numbers.top(); + +} + + + +int main() + +{ + + in >> expression; + + prepare(); + + out << calculateExpression(); + + return 0; + +} diff --git a/eval/stack/365bc3e1-44da-4746-86e7-6415a590fafe.cpp b/eval/stack/365bc3e1-44da-4746-86e7-6415a590fafe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36903463d9028724f41bfa9cba929034acc5b80f --- /dev/null +++ b/eval/stack/365bc3e1-44da-4746-86e7-6415a590fafe.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 1e5; + + + +char sti[5 + N]; + +int sir[5 + N]; + +int v[5 + N]; + + + +bool is_operator(char ch) { + + return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); + +} + + + +int rang(char ch) { + + if(ch == '+' || ch == '-') return 1; + + return 2; + +} + + + +int operator_to_int(char ch) { + + if(ch == '+') return -1; + + if(ch == '-') return -2; + + if(ch == '*') return -3; + + if(ch == '/') return -4; + +} + + + +int main() { + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + char ch(' '), chprev(' '); + + int vf(0), nr(0); + + while(ch != '\n') { + + ch = getchar(); + + if(ch != '\n') { + + if(ch == '(') sti[++vf] = '('; + + else if(ch == ')') { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(') { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + vf--; + + nr = 0; + + } else if(is_operator(ch)) { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(' && rang(sti[vf]) >= rang(ch)) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + sti[++vf] = ch; + + nr = 0; + + } else + + nr = nr * 10 + ch - '0'; + + chprev = ch; + + } + + } + + if(nr > 0) sir[++sir[0]] = nr; + + nr = 0; + + while(vf > 0) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + + + vf = 0; + + for(int i = 1; i <= sir[0]; i++){ + + if(sir[i] < 0){ + + if(sir[i] == -1) + + v[vf - 1] += v[vf]; + + else if(sir[i] == -2) + + v[vf - 1] -= v[vf]; + + else if(sir[i] == -3) + + v[vf - 1] *= v[vf]; + + else if(sir[i] == -4) + + v[vf - 1] /= v[vf]; + + vf--; + + } + + else v[++vf] = sir[i]; + + } + + printf("%d\n", v[1]); + + return 0; + +} diff --git a/eval/stack/376b0a22-07ab-47e1-921d-049d3bd31d53.cpp b/eval/stack/376b0a22-07ab-47e1-921d-049d3bd31d53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d5553860a73d18077ad6ef60a7de5aaeee0d9ff --- /dev/null +++ b/eval/stack/376b0a22-07ab-47e1-921d-049d3bd31d53.cpp @@ -0,0 +1,199 @@ +#include +#include + + + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int n, i; + + + +int stiv_num[100005]; + +char stiv_sign[100005], x[100005]; + +int numbers = 0, signs = 0; + + + +int getNr() + +{ + + int rez = 0; + + for(i = i; i < n && (x[i] >= '0' && x[i] <= '9'); i++) + + rez = rez * 10 + (x[i] - '0'); + + + + i--; + + return rez; + +} + +void solveParanthese() + +{ + + int rez = 0; + + while(stiv_sign[signs] != '(') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; + + + + if(stiv_sign[signs] == '+') + + rez += nrNow; + + if(stiv_sign[signs] == '-') + + rez -= nrNow; + + signs--; + + } + + signs--; + + + + stiv_num[numbers] += rez; + + if(stiv_sign[signs] == '*') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] *= nrNow; + + } + + if(stiv_sign[signs] == '/') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] /= nrNow; + + } + +} + + + +int main() + +{ + + f.getline(x, 100005); + + n = strlen(x); + + + + stiv_sign[++signs] = '('; + + for(i = 0; i < n; i++) + + { + + if(x[i] == '(') + + stiv_sign[++signs] = '('; + + + + if(x[i] >= '0' && x[i] <= '9') + + { + + int nrNow = getNr(); + + + + if(stiv_sign[signs] == '*') + + { + + stiv_num[numbers] *= nrNow; + + signs--; + + continue; + + } + + if(stiv_sign[signs] == '/') + + { + + stiv_num[numbers] /= nrNow; + + signs--; + + continue; + + } + + + + stiv_num[++numbers] = nrNow; + + continue; + + } + + + + if(x[i] == '*' || x[i] == '/' || x[i] == '+' || x[i] == '-') + + stiv_sign[++signs] = x[i]; + + + + if(x[i] == ')') + + solveParanthese(); + + } + + solveParanthese(); + + + + g << stiv_num[1]; + + return 0; + +} diff --git a/eval/stack/378aca6d-0919-4288-ade5-091ae36e3370.cpp b/eval/stack/378aca6d-0919-4288-ade5-091ae36e3370.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9251864dab7eb013410fd9f3ebc05d7209f968e --- /dev/null +++ b/eval/stack/378aca6d-0919-4288-ade5-091ae36e3370.cpp @@ -0,0 +1,250 @@ +#include + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + +struct Node + +{ + + string val; + + Node *left, *right; + +} *root; + +bool priority (Node* n1, Node* n2) + +{ + + string val1 = n1 -> val; + + string val2 = n2 -> val; + + if (val2 == "(" || val1 == "(") + + return true; + + if ((val1 == "+" || val1 == "-") && (val2 == "*" || val2 == "/")) + + return true; + + return false; + +} + +void adjust (stack &op, stack &num) + +{ + + Node* n2 = op.top (); + + Node *nr1, *nr2; + + nr1 = num.top (); + + num.pop (); + + nr2 = num.top (); + + num.pop (); + + n2 -> left = nr1; + + n2 -> right = nr2; + + num.push (n2); + + op.pop (); + +} + +Node* postfix_tree (string s) + +{ + + stack op; + + stack num; + + int i = 0; + + int length = s.size (); + + while (i < length) + + { + + if (s[i] == ' ' || s[i] == '\t') + + i++; + + if (isdigit (s[i])) + + { + + string number; + + while (isdigit (s[i]) && i < length) + + { + + number += s[i]; + + i++; + + } + + Node* n1 = new Node; + + n1 -> val = number; + + n1 -> left = NULL; + + n1 -> right = NULL; + + num.push (n1); + + } + + else + + { + + Node* n1 = new Node; + + n1 -> val += s[i]; + + n1 -> left = NULL; + + n1 -> right = NULL; + + if (op.empty ()) + + op.push (n1); + + else + + { + + if (s[i] == ')') + + { + + while (op.top () -> val != "(") + + adjust (op, num); + + op.pop (); + + } + + else if (priority (op.top (), n1)) + + op.push (n1); + + else + + { + + while ((!op.empty ()) ? (!priority(op.top (), n1)) : 0) + + adjust (op, num); + + op.push (n1); + + } + + } + + i++; + + } + + } + + while (!op.empty ()) + + adjust (op, num); + + return num.top (); + +} + +int evaluate (Node* root) + +{ + + if (root) + + if (root -> val == "+") + + return evaluate (root -> right) + evaluate (root -> left); + + else if (root -> val == "-") + + return evaluate (root -> right) - evaluate (root -> left); + + else if (root -> val == "*") + + return evaluate (root -> right) * evaluate (root -> left); + + else if (root -> val == "/") + + return evaluate (root -> right) / evaluate (root -> left); + + else + + { + + int nr = 0; + + int n = root -> val.size (); + + int i = 0; + + for (int i = 0; i < n; i++) + + nr = nr * 10 + root -> val[i] - '0'; + + return nr; + + } + +} + +void print (Node* root) + +{ + + if (root) + + { + + cout << root -> val << endl; + + print (root -> left); + + print (root -> right); + + } + +} + +int main () + +{ + + getline (fin, s); + + root = postfix_tree (s); + + fout << evaluate (root); + +} diff --git a/eval/stack/37be226a-6768-4c15-8313-930b36f5670c.cpp b/eval/stack/37be226a-6768-4c15-8313-930b36f5670c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f8f2bfefab9a36d0bfea0e1ec8d62ea8ba1d2ce --- /dev/null +++ b/eval/stack/37be226a-6768-4c15-8313-930b36f5670c.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +stack semne; + +stack numere; + + + +char s[100050]; + + + +int prop(char c) + +{ + + if(c=='(')return 0; + + if(c=='+'||c=='-')return 1; + + if(c=='*'||c=='/')return 2; + + if(c=='~')return 3; + + return -1; + +} + + + + + +int main() + +{ + + char ll; + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.getline(s+1,100050); + + s[0]='('; + + int n=strlen(s); + + s[n]=')';s[++n]=0; + + int i=0,j,b,a; + + char numars[100]; + + while(i=0) + + + + {i++;semne.push('~');} + + else + + if(s[i]==')') + + { + + while(semne.top()!='(') + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + i++; + + semne.pop(); + + } + + else + + if(prop(s[i])>0) + + { + + while(prop(s[i])<=prop(ll=semne.top())) + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + + + semne.push(s[i]); + + i++; + + } + + else + + { + + j=i; + + while(isdigit(s[j]))j++; + + strncpy(numars,s+i,j-i); + + numars[j-i]=0; + + numere.push(atoi(numars)); + + i=j; + + } + + } + + fout< +#include +#include +#include +#include +#include +using namespace std; + +string expression; + +bool isOperator(char ch) { + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; +} + +int getPriority(char ch) { + if (ch == '+' || ch == '-') { + return 1; + } else if (ch == '*' || ch == '/') { + return 2; + } + return -1; +} + +void applyOperator(stack& numbers, char op) { + int secondNo = numbers.top(); + numbers.pop(); + int firstNo = numbers.top(); + numbers.pop(); + + switch (op) { + case '+': { + numbers.push(firstNo + secondNo); + break; + } + case '-': { + numbers.push(firstNo - secondNo); + break; + } + case '*': { + numbers.push(firstNo * secondNo); + break; + } + case '/': { + numbers.push(firstNo / secondNo); + break; + } + } +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + cin >> expression; + stack numbers; + stack operators; + + for (size_t i = 0; i < expression.size(); i++) { + if (expression[i] == '(') { + operators.push('('); + } else if (expression[i] == ')') { + while (operators.top() != '(') { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.pop(); + } else if (isOperator(expression[i])) { + while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.push(expression[i]); + } else { + assert(isdigit(expression[i])); + int number = 0; + while (i < expression.size() && isdigit(expression[i])) { + number = number * 10 + expression[i++] - '0'; + } + i--; + numbers.push(number); + } + } + + while (!operators.empty()) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + + cout << numbers.top() << "\n"; + return 0; +} diff --git a/eval/stack/39363780-9db8-4516-9f6c-012e4fce558f.cpp b/eval/stack/39363780-9db8-4516-9f6c-012e4fce558f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9a8ef322718b5bbc2c0304101fb83a5dc1a312 --- /dev/null +++ b/eval/stack/39363780-9db8-4516-9f6c-012e4fce558f.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +int opPriority(char c) +{ + if (c == '(' || c == ')') return -1; + if (c == '+' || c == '-') return 1; + if (c == '*' || c == '/') return 2; + return 3; +} + +bool isDigit(char c) +{ + return '0' <= c && c <= '9'; +} + +class ExpressionEvaluator{ + public: + int Evaluate(string expr) + { + for (int i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (c == '(') { + op_.push(c); + priority_.push(opPriority(c)); + continue; + } + if (isDigit(c)) { + int number = c - '0'; + while (i+1 < expr.size() && isDigit(expr[i + 1])) { + ++i; + number = number * 10 + (expr[i] - '0'); + } + numbers_.push(number); + continue; + } + Pop_(opPriority(c)); + if (c != ')') { + priority_.push(opPriority(c)); + op_.push(c); + } + } + int result = numbers_.top(); + numbers_.pop(); + return result; + } + private: + void Pop_(int p) + { + while (!priority_.empty() && priority_.top() >= p) { + char op = op_.top(); + op_.pop(); + priority_.pop(); + + if (op == '(') + return; + + int b = numbers_.top(); + numbers_.pop(); + int a = numbers_.top(); + numbers_.pop(); + + if (op == '+') numbers_.push(a + b); + else if (op == '-') numbers_.push(a - b); + else if (op == '*') numbers_.push(a * b); + else if (op == '/') numbers_.push(a / b); + } + } + + stack numbers_; + stack priority_; + stack op_; +}; + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string s; + fin >> s; + + ExpressionEvaluator exprEval; + fout << exprEval.Evaluate("(" + s + ")"); + + return 0; +} diff --git a/eval/stack/39cbb3aa-6128-4c3b-a85f-1b0797cb6fc7.cpp b/eval/stack/39cbb3aa-6128-4c3b-a85f-1b0797cb6fc7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5a30851967d3f478f833d18f2947a0f50492f04 --- /dev/null +++ b/eval/stack/39cbb3aa-6128-4c3b-a85f-1b0797cb6fc7.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); +string s; + + + + + + + +bool isOp(char c) { + + return c == '+' || c == '-' || c == '/' || c == '*'; + +} + + + +int priority(char op) { + + if (op == '/' || op == '*') return 2; + + if (op == '-' || op == '+') return 1; + + return -1; + +} + + + +void process_op(stack & st, char op) { + + int r = st.top(); st.pop(); + + int l = st.top(); st.pop(); + + + + switch (op) { + + case '+': st.push(l + r); break; + + case '-': st.push(l - r); break; + + case '/': st.push(l / r); break; + + case '*': st.push(l * r); break; + + } + +} + + + +int evaluate(string& s) { + + stack st; + + stack op; + + + + for (int i = 0; i < (int)s.size(); i++) { + + + + if (s[i] == '(') { + + op.push('('); + + } + + else if (s[i] == ')') { + + while (op.top() != '(') { + + process_op(st, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + else if (isOp(s[i])) { + + char curr_op = s[i]; + + while (!op.empty() && priority(op.top()) >= priority(curr_op)) { + + process_op(st, op.top()); + + op.pop(); + + } + + op.push(curr_op); + + } + + else { + + + + int num = 0; + + while (i < (int)s.size() && isalnum(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + st.push(num); + + } + + } + + + + while (!op.empty()) { + + process_op(st, op.top()); + + op.pop(); + + } + + return st.top(); + +} + + + + + + + +int main() { + + + + cin >> s; + + cout << evaluate(s); + + + + return 0; + +} diff --git a/eval/stack/3b0c602f-cb87-4ed8-8738-74d7f30203ce.cpp b/eval/stack/3b0c602f-cb87-4ed8-8738-74d7f30203ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99274564a2c1624ec9afe9997bdea1d0e478fd38 --- /dev/null +++ b/eval/stack/3b0c602f-cb87-4ed8-8738-74d7f30203ce.cpp @@ -0,0 +1,182 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int i,v1,v2,rez; + +string str; + +stack val; + +stack s; + +int calc(int v1,int v2,char op) + +{ + + if(op=='+') return v2+v1; + + else if(op=='-') return v1-v2; + + else if(op=='*') return v1*v2; + + else return v1/v2; + +} + +int preced(char op) + +{ + + if(op=='-'||op=='+') return 1; + + if(op=='*'||op=='/') return 2; + + return 0; + +} + +int ev() + +{ + + + + char op; + +getline(fin,str); + + for(i=0; i=preced(str[i])) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + } + + s.push(str[i]); + + } + + } + + while(!s.empty()) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + + + } + + return val.top(); + + + +} + +int main() + +{fout< +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +struct my_timer{ + chrono::time_point start; + my_timer(): + start(chrono::high_resolution_clock::now()){} + ~my_timer(){ + std::chrono::duration diff = chrono::high_resolution_clock::now() - start; + clog< val; + stack op; + int c=getchar(); + while(c!=EOF && c != '\n'){ + if(isdigit(c)){ + int num = c&15; + while(isdigit(c=getchar())){ + num = (num << 3) + (num << 1) + (c & 15); + } + val.emplace(num); + if(c==EOF || c=='\n') + break; + } + if(c==')'){ + while(op.top() != '('){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.pop(); + } + else{ + while(!op.empty() && op.top() != '(' && ord[op.top()] >= ord[c]){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.emplace(c); + } + c=getchar(); + } + while(!op.empty()){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + while(!val.empty()){ + if(val.front() & (1ll << 62)){ + ll a = op.top(); + op.pop(); + switch(val.front() & (~(1ll<<62))){ + case '+': + op.top() += a; + break; + case '-': + op.top() -= a; + break; + case '*': + op.top() *= a; + break; + case '/': + op.top() /= a; + break; + default : + return 1; + } + }else{ + op.emplace(val.front()); + } + val.pop(); + } + printf("%lld",op.top()); + } +} diff --git a/eval/stack/3bfef42a-9e01-4aae-81ad-dd87cba34705.cpp b/eval/stack/3bfef42a-9e01-4aae-81ad-dd87cba34705.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af6e5223ea62954f44dd72ba26a26f7274e53692 --- /dev/null +++ b/eval/stack/3bfef42a-9e01-4aae-81ad-dd87cba34705.cpp @@ -0,0 +1,224 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include +using namespace std; + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + +const int N = 300001; + +char c[N]; + +int stiva [100001], vf; + +int prio(char x) + +{ + + if(x == '(' or x == ')') + + return 0; + + if(x == '+' or x == '-') + + return 1; + + return 2; + +} + +int operatie( int x, int y , char semn) + +{ + + switch(semn) + + { + + case '+': return x + y; + + case '-': return x - y; + + case '*': return x * y; + + case '/': return x / y; + + } + +} + +int eval(char []); + +void postfix(char[]); + +int main() + +{ + + cin >> c; + + postfix(c); + + cout << eval(c); + + return 0; + +} + +void postfix(char x[]) + +{ + + int poz = 0; + + int n = strlen(x); + + char aux[N]= {0}; + + for( int i =0 ;i < n ;i ++) + + { + + if(x[i] >='0' and x[i]<='9') + + aux[poz++]=x[i]; + + else + + { + + if(i > 0 and x[i-1]>='0' and x[i -1]<='9') + + aux[poz++]= ' '; + + if(x[i] == '(') + + stiva[vf++]= x[i]; + + else + + if(x[i] == ')') + + { + + while(stiva[vf - 1] !='(') + + aux[poz++] = stiva[--vf]; + + vf--; + + } + + else + + { + + while(vf != 0 and prio(x[i]) <= prio(stiva[vf- 1])) + + aux[poz ++]= stiva[--vf]; + + stiva[vf++] = x[i]; + + } + + + + } + + } + + if(aux[poz - 1] >='0' and aux[poz - 1] <='9') + + aux[poz++] = ' '; + + while(vf != 0) + + { + + + + aux[poz++] = stiva[--vf]; + + } + + strcpy(x, aux); + +} + +int eval(char x[]) + +{ + + int n = strlen(x); + + int numar = 0; + + for( int i =0 ; i < n ;i++) + + if(x[i] >='0' and x[i]<='9') + + numar = numar * 10 +x[i] -'0'; + + else + + if(x[i] == ' ') + + { + + stiva[vf++] = numar; + + numar = 0; + + } + + else + + { + + int nr2 = stiva[--vf], nr1= stiva[--vf]; + + stiva[vf++] = operatie(nr1,nr2, x[i]); + + } + + return stiva[vf - 1]; + +} diff --git a/eval/stack/3d68fa5e-a70a-4445-bdc0-b0714d5e7348.cpp b/eval/stack/3d68fa5e-a70a-4445-bdc0-b0714d5e7348.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f8f2bfefab9a36d0bfea0e1ec8d62ea8ba1d2ce --- /dev/null +++ b/eval/stack/3d68fa5e-a70a-4445-bdc0-b0714d5e7348.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +stack semne; + +stack numere; + + + +char s[100050]; + + + +int prop(char c) + +{ + + if(c=='(')return 0; + + if(c=='+'||c=='-')return 1; + + if(c=='*'||c=='/')return 2; + + if(c=='~')return 3; + + return -1; + +} + + + + + +int main() + +{ + + char ll; + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.getline(s+1,100050); + + s[0]='('; + + int n=strlen(s); + + s[n]=')';s[++n]=0; + + int i=0,j,b,a; + + char numars[100]; + + while(i=0) + + + + {i++;semne.push('~');} + + else + + if(s[i]==')') + + { + + while(semne.top()!='(') + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + i++; + + semne.pop(); + + } + + else + + if(prop(s[i])>0) + + { + + while(prop(s[i])<=prop(ll=semne.top())) + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + + + semne.push(s[i]); + + i++; + + } + + else + + { + + j=i; + + while(isdigit(s[j]))j++; + + strncpy(numars,s+i,j-i); + + numars[j-i]=0; + + numere.push(atoi(numars)); + + i=j; + + } + + } + + fout< +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char op) { + + switch(op) { + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + } + +} + + + +int main() { + + char s[100005], operation; + + int i, l, n1, n2; + + stack nr; + + stack op; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + l = strlen(s); + + + + for(i = 0; i < l; ++i) + + if(priority(s[i])) { + + while(!op.empty() && priority(s[i]) <= priority(op.top())) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.push(s[i]); + + } + + else if(isDigit(s[i])) { + + n1 = 0; + + while(isDigit(s[i]) && i < l) + + n1 = 10*n1 + s[i++] - '0'; + + --i; + + nr.push(n1); + + } + + else if(s[i] == '(') + + op.push('('); + + else if(s[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.pop(); + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + + + printf("%d", nr.top()); + +} diff --git a/eval/stack/3e261a3d-2d6c-4ca5-a718-4ec384aaf2bf.cpp b/eval/stack/3e261a3d-2d6c-4ca5-a718-4ec384aaf2bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c396acf43ce83f0affad7c03104fc9ad26b83f --- /dev/null +++ b/eval/stack/3e261a3d-2d6c-4ca5-a718-4ec384aaf2bf.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string mes; + +string post; + +int i; + +stack nr; + +stack op; + + + +long long calc(long long nr1,long long nr2,char operatie) + +{ + + + + switch(operatie){ + + case '+': + + return nr1+nr2; + + case '-': + + return nr1-nr2; + + case '*': + + return nr1*nr2; + + case '/': + + return nr1/nr2; + + case '^': + + return pow(nr1,nr2); + + } + + return 0; + +} + + + +int prec(char ch) + +{ + + switch(ch) + + { + + case '+': + + case '-': + + return 1; + + case '*': + + case '/': + + return 2; + + case '^': + + return 3; + + case '(': + + return 0; + + } + + return 0; + +} + + + +int main() + +{ + + fin>>mes; + + + + while(i= prec(mes[i])) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + op.push(mes[i]); + + } + + } + + + + i++; + + } + + + + while(!op.empty() ) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + + + + + i = 0; + + long long sol = 0; + + while(i < post.size()) + + { + + if(isdigit(post[i])) + + { + + long long numar = 0; + + while(isdigit(post[i])) + + { + + numar = numar * 10 + (post[i]-'0'); + + i++; + + } + + nr.push(numar); + + + + } + + else if(post[i] == '+' || post[i] == '*' || post[i] == '-' || post[i] == '^' || post[i] == '/') + + { + + + + long long nr1 = nr.top(); + + nr.pop(); + + long long nr2 = nr.top(); + + nr.pop(); + + long long res = calc(nr2,nr1,post[i]); + + nr.push(res); + + + + } + + + + i++; + + } + + + + fout< +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char vp[42949678],s[42949678],exp[42949678]; + +stack st; + +int n,kexp; + + + + + +int prioritate(int c) + +{ + + if (s[c]=='(' || s[c]==')') + + return 0; + + if (s[c]=='*' || s[c]=='/') + + return 2; + + return 1; + +} + + + +void formare_nr(int &k) + +{ + + while (s[k]>='0'&& s[k]<='9') + + { + + exp[++kexp]=s[k]; + + + + k++; + + } + + k--; + + exp[++kexp]=' '; + +} + + + + + +void forma_poloneza() + +{ + + for (int i=0;i<=n;i++) + + { + + if (s[i]>='0' && s[i]<='9') + + formare_nr(i); + + else if (prioritate(i)!=0) + + { + + if (st.empty()) + + st.push(i); + + else + + { + + int a=prioritate(i); + + while (!st.empty() && prioritate(st.top())>=a) + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + st.push(i); + + } + + } + + else + + { + + if (s[i]=='(') + + st.push(i); + + else + + { + + while (!st.empty()&& s[st.top()]!='(') + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + exp[++kexp]=' '; + + if (!st.empty()&& s[st.top()]=='(') + + st.pop(); + + } + + } + + + + } + + while (!st.empty()) exp[++kexp]= s[st.top()], st.pop(); + + + +} + + + +int transf_int (int &k) + +{ int nr=0; + + while (exp[k]>='0' && exp[k]<='9') + + { + + nr =nr*10 + (exp[k]-'0'); + + k++; + + } + + + + return nr; + + + + + +} + + + +int calcul(int i, int a,int b) + +{ + + + + if (exp[i]=='-') + + return a-b; + + if (exp[i]=='+') + + return a+b; + + if (exp[i]=='*') + + return a*b; + + return a/b; + + + +} + + + +int evaluare(int k) + +{ + + while (!st.empty()) + + st.pop(); + + for (int i=1;i<=k;i++) + + {if (exp[i]>='0' && exp[i]<='9') + + st.push(transf_int(i)); + + if (st.size()>=2 && exp[i]!=' ') + + { + + int a,b; + + a=st.top(); + + st.pop(); + + b=st.top(); + + st.pop(); + + + + st.push(calcul(i,b,a)); + + } + + } + + return st.top(); + + + +} + + + +int main() + +{ + + f>>s; + + n=strlen(s)-1; + + forma_poloneza(); + + + +g< +#include +#include +using namespace std; + + + +struct operation{ + + char operation; + + int priority; + +}; + + + +int main(){ + + + + iostream::sync_with_stdio(false); + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + int result = 0, currentOperandPriority, currentExpressionPriority = 0, currentNumber = 0; + + operation currentOp; + + string input; + + + + getline(fin, input); + + + + stack stackNumbers; + + stack stackDigits; + + stack stackOperators; + + + + bool newNumber; + + + + for(int i = input.length() - 1; i >= 0; i--) + + + + if(input[i] <= '9' && input[i] >= '0') + + stackDigits.push(input[i]); + + + + else{ + + + + currentNumber = 0; + + newNumber = false; + + + + while(!stackDigits.empty()){ + + newNumber = true; + + currentNumber = currentNumber*10 + stackDigits.top() - '0'; + + stackDigits.pop(); + + } + + if(newNumber) + + stackNumbers.push(currentNumber); + + + + + + if(input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '+'){ + + + + if(input[i] == '*' || input[i] == '/') + + currentOperandPriority = currentExpressionPriority + 1; + + else + + currentOperandPriority = currentExpressionPriority; + + + + if(stackOperators.empty() || stackOperators.top().priority <= currentOperandPriority){ + + + + currentOp.operation = input[i]; + + currentOp.priority = currentOperandPriority; + + stackOperators.push(currentOp); + + + + } else { + + while(!stackOperators.empty() && (currentOp = stackOperators.top()).priority > currentOperandPriority) { + + + + stackOperators.pop(); + + + + int operand1 = stackNumbers.top(); + + stackNumbers.pop(); + + int operand2 = stackNumbers.top(); + + stackNumbers.pop(); + + + + int res; + + + + if(currentOp.operation == '+') + + res = operand1 + operand2; + + else if (currentOp.operation == '-') + + res = operand1 - operand2; + + else if (currentOp.operation == '/') + + res = operand1 / operand2; + + else if (currentOp.operation == '*') + + res = operand1 * operand2; + + + + stackNumbers.push(res); + + } + + + + currentOp.operation = input[i]; + + currentOp.priority = currentOperandPriority; + + stackOperators.push(currentOp); + + } + + } + + + + else if(input[i] == '('){ + + currentExpressionPriority -= 2; + + } + + + + else if(input[i] == ')'){ + + currentExpressionPriority += 2; + + } + + + + } + + + + + + currentNumber = 0; + + newNumber = false; + + + + while(!stackDigits.empty()){ + + newNumber = true; + + currentNumber = currentNumber*10 + stackDigits.top() - '0'; + + stackDigits.pop(); + + } + + if(newNumber) + + stackNumbers.push(currentNumber); + + + + while(!stackOperators.empty()){ + + currentOp = stackOperators.top(); + + stackOperators.pop(); + + int operand1 = stackNumbers.top(); + + stackNumbers.pop(); + + int operand2 = stackNumbers.top(); + + stackNumbers.pop(); + + + + int res; + + + + if(currentOp.operation == '+') + + res = operand1 + operand2; + + else if (currentOp.operation == '-') + + res = operand1 - operand2; + + else if (currentOp.operation == '/') + + res = operand1 / operand2; + + else if (currentOp.operation == '*') + + res = operand1 * operand2; + + + + stackNumbers.push(res); + + } + + + + fout< +using namespace std; + + +int precedence(char op) +{ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + return 0; +} + +int applyOp(int a, int b, char op) +{ + switch(op) + { + case '+': + return a + b; + case '-': + return a - b; + case '*': + return a * b; + case '/': + return a / b; + } + return 0; +} + +bool is_operator(char to_check) +{ + return to_check=='+' || to_check=='-' ||to_check=='*' ||to_check=='/'; + +} + + +bool is_digit(char x) +{ + return x>='0' && x<='9'; + +} + +int get_next_number(const string&x,unsigned&cursor) +{ + int res=0; + + while(is_digit(x[cursor]) && cursor values; +stack ops; + +void apply_last_operation() +{ + int a=values.top(); + values.pop(); + int b=values.top(); + values.pop(); + + char op=ops.top(); + + int c=applyOp(b,a,op); + + + values.push(c); + + ops.pop(); +} + + + +void eval() +{ + + while(ops.top()!='(') + { + apply_last_operation(); + } + ops.pop(); + +} + + + +int evaluate(string tokens) +{ + for(unsigned i=0; i=precedence(op)) + { + apply_last_operation(); + } + ops.push(op); + } + + + + } + + while(!ops.empty()) + { + apply_last_operation(); + } + return values.top(); + +} + +int main() +{ + ifstream in("evaluare.in"); + string a; + in>>a; + ofstream out("evaluare.out"); + out< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char infix[100000 + 1]; + + + +int evaluate_basic(int A, int B, char c) + +{ + + if(c == '*') return A * B; + + if(c == '/') return A / B; + + if(c == '+') return A + B; + + if(c == '-') return A - B; + + + + return -1; + +} + + + + + +int operator_priority(char c) + +{ + + if(c == '+' || c == '-') return 1; + + if(c == '*' || c == '/') return 2; + + + + return -1; + +} + + + +int get_operand(int &i) + +{ + + int operand = 0; + + + + while(infix[i] >= '0' && infix[i] <= '9' && infix[i] != '\0') + + { + + operand = operand * 10 + (infix[i] - '0'); + + ++i; + + } + + + + i--; + + + + + + return operand; + +} + + + +int evaluate() + +{ + + stack operators; + + vector operands; + + + + + + + + for(int i = 0; infix[i] != '\0'; i++) + + { + + if(infix[i] >= '0' && infix[i] <= '9') operands.push_back(get_operand(i)); + + else if(infix[i] == '(') operators.push(infix[i]); + + else + + { + + while(!operators.empty() + + && operators.top() != '(' + + && operator_priority(infix[i]) <= operator_priority(operators.top())) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + + + + if(infix[i]==')') operators.pop(); + + else operators.push(infix[i]); + + } + + + + + + } + + + + while(!operators.empty()) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + return operands[0]; + +} + + + + + +int main() + +{ + + fin.getline(infix, 100000 + 1, '\n'); + + fout << evaluate(); + + + +} diff --git a/eval/stack/442adf96-f6cb-41d7-94fa-548b830d44d3.cpp b/eval/stack/442adf96-f6cb-41d7-94fa-548b830d44d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d4783e4c2bc1e038041edd56ae395d67eec1d99 --- /dev/null +++ b/eval/stack/442adf96-f6cb-41d7-94fa-548b830d44d3.cpp @@ -0,0 +1,255 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct stackchar + +{ + + char val[100000]; + + int varf; + +}; + + + +struct stackint + +{ + + int val[100000]; + + int varf; + +}; + + + +int priority(char op) + +{ + + if(op=='*' || op=='/') + + return 2; + + if(op=='+' || op=='-') + + return 1; + + return 0; + +} + + + +void postfix(char *exp, char *post) + +{ + + stackchar stiva; + + stiva.varf=-1; + + + + int j=0; + + + + for(int i=0;exp[i]!=0;) + + { + + if(exp[i]>='0' && exp[i]<='9') + + { + + while(exp[i]>='0' && exp[i]<='9') + + post[j++]=exp[i++]; + + post[j++]=' '; + + } + + + + if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') + + { + + if(stiva.varf==-1) + + stiva.val[++stiva.varf]=exp[i++]; + + else + + { + + while(priority(exp[i])<=priority(stiva.val[stiva.varf])) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[++stiva.varf]=exp[i++]; + + } + + } + + + + if(exp[i]=='(') + + { + + stiva.val[++stiva.varf]=exp[i++]; + + } + + if(exp[i]==')') + + { + + while(stiva.val[stiva.varf]!='(') + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[stiva.varf--]; + + ++i; + + } + + } + + + + while(stiva.varf!=-1) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + post[j]=0; + +} + + + +int evaluare(char *exp) + +{ + + stackint stiva; + + stiva.varf=-1; + + int nr; + + for(int j=0;exp[j]!=0;) + + { + + if(exp[j]==' ') + + ++j; + + if(exp[j]>='0' && exp[j]<='9') + + { + + nr=0; + + while(exp[j]>='0' && exp[j]<='9') + + nr=nr*10+(exp[j++]-'0'); + + stiva.val[++stiva.varf]=nr; + + } + + + + if(exp[j]=='+' || exp[j]=='/' || exp[j]=='*' || exp[j]=='-') + + { + + nr=stiva.val[stiva.varf--]; + + if(exp[j]=='+') + + stiva.val[stiva.varf]+=nr; + + if(exp[j]=='-') + + stiva.val[stiva.varf]-=nr; + + if(exp[j]=='*') + + stiva.val[stiva.varf]*=nr; + + if(exp[j]=='/') + + stiva.val[stiva.varf]/=nr; + + j++; + + } + + } + + return stiva.val[stiva.varf]; + +} + + + +char exp[100001], post[300001]; + +int rez; + + + +int main() + +{ + + fin.getline(exp, 100000); + + postfix(exp, post); + + rez=evaluare(post); + + fout< +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int precedenta(char op) + +{ + + if(op == '+' || op == '-') + + return 1; + + if(op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int operatie(char op, int x, int y) + +{ + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + + + } + +} + + + +int eval(string sir) + +{ + + int i; + + stack val; + + stack ops; + + + + for(i=0; i= precedenta(sir[i])) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + ops.push(sir[i]); + + } + + } + + while(!ops.empty()) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + return val.top(); + +} + + + +int main() + +{ + + string sir; + + fin>>sir; + + fout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string s; + +stacksol; + +stackrez; + +queueq; + +unordered_mapmp; + +int me(char a,char b) + +{ + + return mp[a]>=mp[b]; + +} + +int conv(string s) + +{ + + int ret=0; + + for(int i=0;i>s; + + s='('+s+')'; + + for(int i=0;i +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char c) { + + switch(c) { + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + } + +} + + + +int main() { + + char expresie[100005], sign; + + int l, i, temp, n1, n2; + + stack nr; + + stack op; + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s", expresie); + + l = strlen(expresie); + + + + for(i = 0; i < l; ++i) { + + if(priority(expresie[i])) { + + while(!op.empty() && priority(expresie[i]) <= priority(op.top())) { + + sign = op.top(); + + op.pop(); + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.push(expresie[i]); + + + + } + + else if(isDigit(expresie[i])) { + + temp = 0; + + while(isDigit(expresie[i]) && i < l) + + temp = 10*temp + expresie[i++] - '0'; + + nr.push(temp); + + i--; + + } + + else if(expresie[i] == '(') + + op.push('('); + + else if(expresie[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.pop(); + + } + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + printf("%d", nr.top()); + +} diff --git a/eval/stack/4622f824-2ed7-414b-bea8-008532fb4ab9.cpp b/eval/stack/4622f824-2ed7-414b-bea8-008532fb4ab9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..625b4745d81b413173ebad2491aec5d721d8c410 --- /dev/null +++ b/eval/stack/4622f824-2ed7-414b-bea8-008532fb4ab9.cpp @@ -0,0 +1,226 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include +using namespace std; + +ifstream cin ("evaluare.in"); + +ofstream cout ("evaluare.out"); + +char vc[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + int n,i,nr,cnt=0,k=0,ok=0; + + cin>>vc+1; + + n=strlen(vc+1); + + i=1; + + while (i<=n) + + { + + if (vc[i]>='0'&&vc[i]<='9') + + { + + nr=0; + + while (vc[i]>='0'&&vc[i]<='9') + + { + + nr=nr*10+vc[i]-'0'; + + i++; + + } + + v[++cnt]=nr; + + i--; + + } + + else + + { + + if (vc[i]=='(') + + st[++k]=-1; + + else + + if (vc[i]==')') + + { + + while (st[k]!=-1) + + { + + v[++cnt]=st[k]; + + k--; + + } + + k--; + + } + + else + + if (vc[i]=='+') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-3; + + } + + else + + if (vc[i]=='-') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-4; + + } + + else + + if (vc[i]=='*') + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-5; + + } + + else + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-6; + + } + + } + + i++; + + } + + while (k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + + + for (i=1;i<=cnt;i++) + + { + + if (v[i]>=0) + + st[++k]=v[i]; + + else + + { + + if (v[i]==-5) + + { + + + + if (k<=1) + + st[k]=0; + + else + + { + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if (v[i]==-6) + + { + + + + k--; + + st[k]/=(st[k+1]); + + } + + else + + if (v[i]==-3) + + { + + k--; + + st[k]+=st[k+1]; + + } + + else + + { + + k--; + + st[k]-=st[k+1]; + + } + + } + + } + + cout< +#include +#include +#include +#include +using namespace std; + +const int NMAX=100005; + +int op[NMAX],polo[NMAX]; + +int top_op,top_polo; + +int pr(char ch) + +{ + + switch(ch) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + default: return 0; + + } + +} + +void eval() + +{ + + if(op[top_op]=='+') + + polo[top_polo-1]=polo[top_polo-1]+polo[top_polo]; + + if(op[top_op]=='-') + + polo[top_polo-1]=polo[top_polo-1]-polo[top_polo]; + + if(op[top_op]=='*') + + polo[top_polo-1]=polo[top_polo-1]*polo[top_polo]; + + if(op[top_op]=='/') + + polo[top_polo-1]=polo[top_polo-1]/polo[top_polo]; + + top_op--; + + top_polo--; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + int nr; + + char ch; + + scanf("%c",&ch); + + while(isdigit(ch) || ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='(' || ch==')') + + { + + if(isdigit(ch)) + + { + + nr=0; + + while(isdigit(ch)) + + { + + nr=nr*10+(ch-'0'); + + scanf("%c",&ch); + + } + + polo[++top_polo]=nr; + + } + + else + + { + + if(top_op==0 || ch=='(' || pr(op[top_op])=pr(ch)) + + eval(); + + op[++top_op]=ch; + + } + + } + + scanf("%c",&ch); + + } + + } + + while(top_op) + + eval(); + + printf("%d",polo[1]); + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/eval/stack/46f7532e-9b79-4215-892b-0473a00d034e.cpp b/eval/stack/46f7532e-9b79-4215-892b-0473a00d034e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffe681f6a23698eabef00a07ebb3f055fb702d25 --- /dev/null +++ b/eval/stack/46f7532e-9b79-4215-892b-0473a00d034e.cpp @@ -0,0 +1,183 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else if (nr) + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/4701d294-daf2-4457-af2f-c78eea421716.cpp b/eval/stack/4701d294-daf2-4457-af2f-c78eea421716.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ff44097b3d1ea571a57dce3eda08414fc823ae9 --- /dev/null +++ b/eval/stack/4701d294-daf2-4457-af2f-c78eea421716.cpp @@ -0,0 +1,267 @@ +#include +#include +#include +#include + + + + +std::ifstream f("evaluare.in"); + +std::ofstream g("evaluare.out"); + + + +std::string s; + + + +long long order(const char& c){ + + if(c == '+' || c == '-') + + return 1; + + else if(c == '*' || c == '/') + + return 2; + + return 0; + +} + + + +int main(){ + + f >> s; + + + + std::stackval; + + std::stackops; + + + + for(long long i = 0;i < s.length();i++){ + + + + if(s[i] == ' ') + + continue; + + + + if(i == 0 && !isdigit(s[i])){ + + char sign = s[0]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(sign == '-') + + val.push(-1 * value); + + else val.push(value); + + }else if(isdigit(s[i])){ + + long long value{}; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + val.push(value); + + }else if(s[i] == '('){ + + ops.push(s[i]); + + if(s[i + 1] == '-' || s[i + 1] == '+'){ + + i++; + + char op = s[i]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(op == '+') + + val.push(value); + + else if(op == '-') + + val.push(-value); + + } + + }else if(s[i] == ')'){ + + + + while (!ops.empty() && val.size() >= 2 && ops.top() != '(') { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + if(!ops.empty()) + + ops.pop(); + + + + }else{ + + while (!ops.empty() && order(ops.top()) >= order(s[i])&& val.size() >= 2) { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + ops.push(s[i]); + + } + + } + + + + while (!ops.empty() && val.size() >= 2) { + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + g << val.top(); + +} diff --git a/eval/stack/47b02141-795f-4c5b-88aa-1d8cc204f395.cpp b/eval/stack/47b02141-795f-4c5b-88aa-1d8cc204f395.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c893198b0a3867618d8031d86267c27f0146051 --- /dev/null +++ b/eval/stack/47b02141-795f-4c5b-88aa-1d8cc204f395.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +std::string expr; + +int pos = 0; + +bool issign(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +int eval() { + std::vector stack; + + std::string number; + + char prev_sign = '+'; + int value; + + if (expr[pos] == '-') { + ++pos; + prev_sign = '-'; + } + + for (; pos < expr.size(); ++pos) { + if (isdigit(expr[pos])) { + number += expr[pos]; + } else if (issign(expr[pos])) { + if (!number.empty()) + value = std::stoi(number); + + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + prev_sign = expr[pos]; + } else if (expr[pos] == '(') { + ++pos; + value = eval(); + } else if (expr[pos] == ')') { + + if (!number.empty()) + value = std::stoi(number); + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + break; + } + } + + int ans = 0; + for (int el : stack) + ans += el; + + return ans; +} + +void read() { + std::ifstream in("evaluare.in"); + + std::getline(in, expr); + expr += '+'; + + in.close(); +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << eval() << '\n'; + + out.close(); + return 0; +} diff --git a/eval/stack/47e2938a-3c2a-4fd0-87e5-87a189c2b59b.cpp b/eval/stack/47e2938a-3c2a-4fd0-87e5-87a189c2b59b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3491bdf413873837b8cf85b06060ac79070688f --- /dev/null +++ b/eval/stack/47e2938a-3c2a-4fd0-87e5-87a189c2b59b.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +string s; + +int i = 0; + +int res = 0; + +stack seq; + + + +int open_bracket = 1000000000 + int('('); + +int closed_bracket = 1000000000 + int(')'); + +int plus_sign = 1000000000 + int('+'); + +int minus_sign = 1000000000 + int('-'); + +int multiplication_sign = 1000000000 + int('*'); + +int division_sign = 1000000000 + int('/'); + + + +int sum_to_closed_bracket() + +{ + + int sum = 0; + + int nr; + + while (seq.top() != open_bracket) + + { + + nr = seq.top(); + + seq.pop(); + + if (seq.top() == plus_sign) + + { + + seq.pop(); + + sum += nr; + + } + + if (seq.top() == minus_sign) + + { + + seq.pop(); + + sum -= nr; + + nr = 0; + + } + + } + + return sum + nr; + +} + + + +void close_bracket() + +{ + + int sum = sum_to_closed_bracket(); + + + + seq.pop(); + + + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int multiplier = seq.top(); + + seq.pop(); + + sum = multiplier * sum; + + } + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int dividend = seq.top(); + + seq.pop(); + + sum = dividend / sum; + + } + + seq.push(sum); + +} + + + +void readNumber() + +{ + + int nr = 0; + + while (i < s.length() && s[i] >= '0' && s[i] <= '9') + + nr = nr * 10 + int(s[i++] - '0'); + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous * nr); + + } + + else + + { + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous / nr); + + } + + else + + seq.push(nr); + + } + + + + --i; + +} + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + cin >> s; + + seq.push(open_bracket); + + + + while (i < s.length()) + + { + + switch (s[i]) + + { + + case ')': + + close_bracket(); + + break; + + case '(': + + seq.push(open_bracket); + + break; + + case '+': + + seq.push(plus_sign); + + break; + + case '-': + + seq.push(minus_sign); + + break; + + case '*': + + seq.push(multiplication_sign); + + break; + + case '/': + + seq.push(division_sign); + + break; + + default: + + readNumber(); + + break; + + } + + ++i; + + } + + + + cout << sum_to_closed_bracket(); + + + + return 0; + +} diff --git a/eval/stack/47e30e0a-5e6c-4303-86e7-e8b81a2676fc.cpp b/eval/stack/47e30e0a-5e6c-4303-86e7-e8b81a2676fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4535a49bd7c550445cc9bf00bece9719dec82768 --- /dev/null +++ b/eval/stack/47e30e0a-5e6c-4303-86e7-e8b81a2676fc.cpp @@ -0,0 +1,200 @@ +#include + + +using namespace std; + + + +ifstream fin( "evaluare.in" ); + +ofstream fout( "evaluare.out" ); + + + +string s; + + + +deque var; + +deque op; + + + +void Read() { + + fin >> s; + + fin.close(); + +} + + + +bool is_operator( char c ) { + + return strchr( "+-*/", c ); + +} + + + +int Operate( int a, int b, char op ) { + + if( op == '+' ) return a + b; + + if( op == '-' ) return a - b; + + if( op == '*' ) return a * b; + + if( op == '/' ) return a / b; + +} + + + +int Priority( char ch ) { + + if( ch == '(' ) return 0; + + if( ch == '+' || ch == '-' ) return 1; + + if( ch == '/' || ch == '*' ) return 2; + +} + + + +void Do() { + + for( int i = 0; i < s.size(); ++i ) { + + if( s[i] == '(' ) { op.push_back( '(' ); continue; } + + if( isdigit( s[i] ) ) { + + int aux = 0; + + + + while( i < s.size() && isdigit( s[i] ) ) { + + aux = aux * 10 + s[i] - '0'; + + ++i; + + } + + --i; + + + + var.push_back( aux ); + + continue; + + } + + if( is_operator( s[i] ) ) { + + while( !op.empty() && Priority( op.back() ) >= Priority( s[i] ) ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.push_back( s[i] ); + + continue; + + } + + if( s[i] == ')' ) { + + while( op.back() != '(' ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.pop_back(); + + } + + + + } + + + + while( !op.empty() ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + + + fout << var.back() << '\n'; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/eval/stack/47e6bd85-8adc-4bcf-9dee-2abb091e4312.cpp b/eval/stack/47e6bd85-8adc-4bcf-9dee-2abb091e4312.cpp new file mode 100644 index 0000000000000000000000000000000000000000..936fa3a3e858aacd0e69fe3124fdb8518f8a75ea --- /dev/null +++ b/eval/stack/47e6bd85-8adc-4bcf-9dee-2abb091e4312.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char op) { + + switch(op) { + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + } + +} + + + +int main() { + + char s[100005], operation; + + int i, l, n1, n2; + + stack nr; + + stack op; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + l = strlen(s); + + + + for(i = 0; i < l; ++i) + + if(priority(s[i])) { + + while(!op.empty() && priority(s[i]) <= priority(op.top())) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.push(s[i]); + + } + + else if(isDigit(s[i])) { + + n1 = 0; + + while(isDigit(s[i]) && i < l) + + n1 = 10*n1 + s[i++] - '0'; + + --i; + + nr.push(n1); + + } + + else if(s[i] == '(') + + op.push('('); + + else if(s[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.pop(); + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + + + printf("%d", nr.top()); + +} diff --git a/eval/stack/49660a83-d50b-4c1b-94db-da86b19f2a36.cpp b/eval/stack/49660a83-d50b-4c1b-94db-da86b19f2a36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d4783e4c2bc1e038041edd56ae395d67eec1d99 --- /dev/null +++ b/eval/stack/49660a83-d50b-4c1b-94db-da86b19f2a36.cpp @@ -0,0 +1,255 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct stackchar + +{ + + char val[100000]; + + int varf; + +}; + + + +struct stackint + +{ + + int val[100000]; + + int varf; + +}; + + + +int priority(char op) + +{ + + if(op=='*' || op=='/') + + return 2; + + if(op=='+' || op=='-') + + return 1; + + return 0; + +} + + + +void postfix(char *exp, char *post) + +{ + + stackchar stiva; + + stiva.varf=-1; + + + + int j=0; + + + + for(int i=0;exp[i]!=0;) + + { + + if(exp[i]>='0' && exp[i]<='9') + + { + + while(exp[i]>='0' && exp[i]<='9') + + post[j++]=exp[i++]; + + post[j++]=' '; + + } + + + + if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') + + { + + if(stiva.varf==-1) + + stiva.val[++stiva.varf]=exp[i++]; + + else + + { + + while(priority(exp[i])<=priority(stiva.val[stiva.varf])) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[++stiva.varf]=exp[i++]; + + } + + } + + + + if(exp[i]=='(') + + { + + stiva.val[++stiva.varf]=exp[i++]; + + } + + if(exp[i]==')') + + { + + while(stiva.val[stiva.varf]!='(') + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[stiva.varf--]; + + ++i; + + } + + } + + + + while(stiva.varf!=-1) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + post[j]=0; + +} + + + +int evaluare(char *exp) + +{ + + stackint stiva; + + stiva.varf=-1; + + int nr; + + for(int j=0;exp[j]!=0;) + + { + + if(exp[j]==' ') + + ++j; + + if(exp[j]>='0' && exp[j]<='9') + + { + + nr=0; + + while(exp[j]>='0' && exp[j]<='9') + + nr=nr*10+(exp[j++]-'0'); + + stiva.val[++stiva.varf]=nr; + + } + + + + if(exp[j]=='+' || exp[j]=='/' || exp[j]=='*' || exp[j]=='-') + + { + + nr=stiva.val[stiva.varf--]; + + if(exp[j]=='+') + + stiva.val[stiva.varf]+=nr; + + if(exp[j]=='-') + + stiva.val[stiva.varf]-=nr; + + if(exp[j]=='*') + + stiva.val[stiva.varf]*=nr; + + if(exp[j]=='/') + + stiva.val[stiva.varf]/=nr; + + j++; + + } + + } + + return stiva.val[stiva.varf]; + +} + + + +char exp[100001], post[300001]; + +int rez; + + + +int main() + +{ + + fin.getline(exp, 100000); + + postfix(exp, post); + + rez=evaluare(post); + + fout< +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int precedenta(char op) + +{ + + if(op == '+' || op == '-') + + return 1; + + if(op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int operatie(char op, int x, int y) + +{ + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + + + } + +} + + + +int eval(string sir) + +{ + + int i; + + stack val; + + stack ops; + + + + for(i=0; i= precedenta(sir[i])) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + ops.push(sir[i]); + + } + + } + + while(!ops.empty()) + + { + + int val1, val2, op; + + op = ops.top(); + + ops.pop(); + + + + val2 = val.top(); + + val.pop(); + + + + val1 = val.top(); + + val.pop(); + + + + int rez = operatie(op, val1, val2); + + val.push(rez); + + } + + return val.top(); + +} + + + +int main() + +{ + + string sir; + + fin>>sir; + + fout< +#include + + + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int n, i; + + + +int stiv_num[100005]; + +char stiv_sign[100005], x[100005]; + +int numbers = 0, signs = 0; + + + +int getNr() + +{ + + int rez = 0; + + for(i = i; i < n && (x[i] >= '0' && x[i] <= '9'); i++) + + rez = rez * 10 + (x[i] - '0'); + + + + i--; + + return rez; + +} + +void solveParanthese() + +{ + + int rez = 0; + + while(stiv_sign[signs] != '(') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; + + + + if(stiv_sign[signs] == '+') + + rez += nrNow; + + if(stiv_sign[signs] == '-') + + rez -= nrNow; + + signs--; + + } + + signs--; + + + + stiv_num[numbers] += rez; + + if(stiv_sign[signs] == '*') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] *= nrNow; + + } + + if(stiv_sign[signs] == '/') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] /= nrNow; + + } + +} + + + +int main() + +{ + + f.getline(x, 100005); + + n = strlen(x); + + + + stiv_sign[++signs] = '('; + + for(i = 0; i < n; i++) + + { + + if(x[i] == '(') + + stiv_sign[++signs] = '('; + + + + if(x[i] >= '0' && x[i] <= '9') + + { + + int nrNow = getNr(); + + + + if(stiv_sign[signs] == '*') + + { + + stiv_num[numbers] *= nrNow; + + signs--; + + continue; + + } + + if(stiv_sign[signs] == '/') + + { + + stiv_num[numbers] /= nrNow; + + signs--; + + continue; + + } + + + + stiv_num[++numbers] = nrNow; + + continue; + + } + + + + if(x[i] == '*' || x[i] == '/' || x[i] == '+' || x[i] == '-') + + stiv_sign[++signs] = x[i]; + + + + if(x[i] == ')') + + solveParanthese(); + + } + + solveParanthese(); + + + + g << stiv_num[1]; + + return 0; + +} diff --git a/eval/stack/4b4663ab-e5e6-4438-8070-b8a1cb4905da.cpp b/eval/stack/4b4663ab-e5e6-4438-8070-b8a1cb4905da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d2042881084b2f120fd7e6a097975bbc6eeaae3 --- /dev/null +++ b/eval/stack/4b4663ab-e5e6-4438-8070-b8a1cb4905da.cpp @@ -0,0 +1,171 @@ +#include +#include +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c,semn[100000],st[100000]; + +int i,n,k,nr,gasitnr,fp[100000]; + +long long steval[1000]; + + + +int main(){ + + while(f >> c){ + + if(c >= '0' && c <= '9'){ + + gasitnr = 1; + + nr *= 10; + + nr += c-'0'; + + }else{ + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + if(c == '(' || c == '+' || c == '-' || c == '*' || c == '/'){ + + k++; + + st[k] = c; + + if(st[k] == '+' || st[k] == '-'){ + + while(st[k-1] == '+' || st[k-1] == '-' || st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + + } + + }else{ + + if(st[k] == '*' || st[k] == '/'){ + + while(st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + +; } + + } + + } + + }else{ + + if(c == ')'){ + + while(st[k] != '('){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k--; + + } + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + while(k){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k = 0; + + for(int i = 1; i <= n; i++){ + + if(semn[i] != '+' && semn[i] != '-' && semn[i] != '*' && semn[i] != '/'){ + + k++; + + steval[k] = fp[i]; + + }else{ + + switch(semn[i]){ + + case '+': steval[k-1] = steval[k-1]+steval[k]; break; + + case '-': steval[k-1] = steval[k-1]-steval[k]; break; + + case '*': steval[k-1] = steval[k-1]*steval[k]; break; + + case '/': steval[k-1] = steval[k-1]/steval[k]; break; + + default: + + break; + + } + + k--; + + } + + } + + g << steval[1]; + + return 0; + +} diff --git a/eval/stack/4cc70508-be2a-4950-a497-52617c64ddcf.cpp b/eval/stack/4cc70508-be2a-4950-a497-52617c64ddcf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9a8ef322718b5bbc2c0304101fb83a5dc1a312 --- /dev/null +++ b/eval/stack/4cc70508-be2a-4950-a497-52617c64ddcf.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +int opPriority(char c) +{ + if (c == '(' || c == ')') return -1; + if (c == '+' || c == '-') return 1; + if (c == '*' || c == '/') return 2; + return 3; +} + +bool isDigit(char c) +{ + return '0' <= c && c <= '9'; +} + +class ExpressionEvaluator{ + public: + int Evaluate(string expr) + { + for (int i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (c == '(') { + op_.push(c); + priority_.push(opPriority(c)); + continue; + } + if (isDigit(c)) { + int number = c - '0'; + while (i+1 < expr.size() && isDigit(expr[i + 1])) { + ++i; + number = number * 10 + (expr[i] - '0'); + } + numbers_.push(number); + continue; + } + Pop_(opPriority(c)); + if (c != ')') { + priority_.push(opPriority(c)); + op_.push(c); + } + } + int result = numbers_.top(); + numbers_.pop(); + return result; + } + private: + void Pop_(int p) + { + while (!priority_.empty() && priority_.top() >= p) { + char op = op_.top(); + op_.pop(); + priority_.pop(); + + if (op == '(') + return; + + int b = numbers_.top(); + numbers_.pop(); + int a = numbers_.top(); + numbers_.pop(); + + if (op == '+') numbers_.push(a + b); + else if (op == '-') numbers_.push(a - b); + else if (op == '*') numbers_.push(a * b); + else if (op == '/') numbers_.push(a / b); + } + } + + stack numbers_; + stack priority_; + stack op_; +}; + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string s; + fin >> s; + + ExpressionEvaluator exprEval; + fout << exprEval.Evaluate("(" + s + ")"); + + return 0; +} diff --git a/eval/stack/4d410815-d975-4826-8fa2-b021b0c60a9b.cpp b/eval/stack/4d410815-d975-4826-8fa2-b021b0c60a9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1ee849344d71c328e9fb218d65ce22dcc53996a --- /dev/null +++ b/eval/stack/4d410815-d975-4826-8fa2-b021b0c60a9b.cpp @@ -0,0 +1,93 @@ +#include +#include + + + +using namespace std; + +stack postForm; +stack opStack; + +int priorities[256]; + +void setPriorities() { + priorities['+'] = 1; + priorities['-'] = 1; + priorities['*'] = 2; + priorities['/'] = 2; + priorities['('] = 0; +} + +int getNum(char *&s) { + int num = 0; + while (isdigit(*s)) { + num = num * 10 + *s - '0'; + s++; + } + return num; +} + +int eval(int x, int y, char op) { + switch (op) { + case '+': + return x + y; + case '-': + return x - y; + case '*': + return x * y; + case '/': + return x / y; + default: + return 0; + } +} + +void evaluateLast() { + char op = opStack.top(); + opStack.pop(); + int o2 = postForm.top(); + postForm.pop(); + int o1 = postForm.top(); + postForm.pop(); + postForm.push(eval(o1, o2, op)); +} + +void convert(char *s) { + while (*s) { + if (isdigit(*s)) { + postForm.push(getNum(s)); + } else { + if (*s == '(') { + opStack.push(*s); + s++; + } else if (*s == ')') { + while (opStack.top() != '(') { + evaluateLast(); + } + opStack.pop(); + s++; + } else { + char op = *s; + s++; + while (!opStack.empty() && priorities[opStack.top()] >= priorities[op]) { + evaluateLast(); + } + opStack.push(op); + } + } + } + while (!opStack.empty()) { + evaluateLast(); + } +} + +int main() { + ifstream f("evaluare.in"); + ofstream g("evaluare.out"); + setPriorities(); + char S[100005], *s = S; + f >> S; + convert(s); + g << postForm.top() << '\n'; + return 0; +} diff --git a/eval/stack/4dfdfc81-0894-4c9e-b61a-2febe8883390.cpp b/eval/stack/4dfdfc81-0894-4c9e-b61a-2febe8883390.cpp new file mode 100644 index 0000000000000000000000000000000000000000..284eb99f3dbb3b2e818b427d472488484b4ced02 --- /dev/null +++ b/eval/stack/4dfdfc81-0894-4c9e-b61a-2febe8883390.cpp @@ -0,0 +1,403 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +class StackException + +{ + +private: + + std::string m_error; + + + +public: + + StackException(std::string error) + + : m_error(error) + + { + + } + + + + const char* getError() + + { + + return m_error.c_str(); + + } + +}; + +template + +struct Element + +{ + + Element* next; + + T data; + +}; + +template + +class Stack + +{ + +private: + + Element *top; + +public: + + Stack(); + + Stack(vector newElements); + + Stack(const Stack &S); + + void Push(T newElement); + + T Pop(); + + T Top() const; + + bool isEmpty() const; + + void printStack(); + +}; + +template + +Stack::Stack() + +{ + + top=NULL; + +} + +template + +Stack::Stack(vector newElements) + +{ + + top=NULL; + + for(auto ¤tElement:newElements) + + Push(currentElement); + +} + +template + +Stack::Stack(const Stack &S) + +{ + + top=NULL; + + vector stackElements; + + int numberStackElements=0; + + Element *currentElement=S.top; + + while(currentElement!=NULL) + + { + + stackElements.push_back(currentElement->data); + + currentElement=currentElement->next; + + numberStackElements++; + + } + + for(int i=numberStackElements-1;i>=0;--i) + + Push(stackElements[i]); + +} + +template + +void Stack::Push(T newElement) + +{ + + Element *newTop=new Element; + + if(!newTop) + + throw StackException("Stack Overflow"); + + newTop->data=newElement; + + newTop->next=top; + + top=newTop; + +} + +template + +T Stack::Top() const + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + return top->data; + +} + +template + +T Stack::Pop() + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + Element *temp=top; + + T topData=top->data; + + top=top->next; + + temp->next=NULL; + + free(temp); + + return topData; + +} + +template + +bool Stack::isEmpty() const + +{ + + return (top==NULL); + +} + +template + +void Stack::printStack() + +{ + + if(top==NULL) + + throw StackException("Stack has no elements"); + + Element *currentElement=top; + + while(currentElement!=NULL) + + { + + cout<data<<" "; + + currentElement=currentElement->next; + + } + +} + +int applyOperator(int x, int y, char Operator) + +{ + + switch(Operator) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int priority(char Operator) + +{ + + switch(Operator) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + } + + return 0; + +} + +int DijkstraAlgorithm(string expression) + +{ + + Stack Values; + + Stack Operators; + + for(std::string::iterator it=expression.begin(); it!=expression.end(); ++it) + + { + + char currentCharacter=*it; + + if(currentCharacter==' ') + + continue; + + if(isdigit(currentCharacter)) + + { + + int value=0; + + for(;it!=expression.end()&&isdigit(*it); ++it) + + value=value*10+(*it-'0'); + + --it; + + Values.Push(value); + + continue; + + } + + if(currentCharacter=='(') + + { + + Operators.Push(currentCharacter); + + continue; + + } + + if(currentCharacter==')') + + { + + while(!Operators.isEmpty()&&Operators.Top()!='(') + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + if(!Operators.isEmpty()) + + Operators.Pop(); + + continue; + + } + + int Operator=*it; + + while(!Operators.isEmpty()&&priority(Operator)<=priority(Operators.Top())) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + Operators.Push(Operator); + + } + + while(!Operators.isEmpty()) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + return Values.Top(); + +} + +void evaluateExpression() + +{ + + string expression; + + getline(fin,expression); + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< + + +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int NMAX=100000; + +char c,semn[NMAX],st[NMAX]; + +int i,n,k,nr,gasitnr,fp[NMAX]; + +long long steval[100]; + + + +int main() + +{ + + while(in>>c){ + + if(c>='0'&&c<='9'){ + + gasitnr=1; + + nr=nr*10+(c-48); + + } + + else{ + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + if(c=='('||c=='+'||c=='-'||c=='*'||c=='/') + + { + + k++; + + st[k]=c; + + if(st[k]=='+'||st[k]=='-'){ + + while(st[k-1]=='+'||st[k-1]=='-'||st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + else + + if(st[k]=='*'||st[k]=='/'){ + + while(st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + } + + else + + if(c==')'){ + + while(st[k]!='('){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k--; + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + while(k){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k=0; + + for(i=1;i<=n;i++){ + + if(semn[i]!='+'&&semn[i]!='-'&&semn[i]!='*'&&semn[i]!='/'){ + + k++; + + steval[k]=fp[i]; + + } + + else + + { + + if(semn[i]=='+'){ + + steval[k-1]=steval[k-1]+steval[k]; + + } + + if(semn[i]=='-'){ + + steval[k-1]=steval[k-1]-steval[k]; + + } + + if(semn[i]=='*'){ + + steval[k-1]=steval[k-1]*steval[k]; + + } + + if(semn[i]=='/'){ + + steval[k-1]=steval[k-1]/steval[k]; + + } + + k--; + + } + + } + + out< +#include +#include +#include +using namespace std; + + + +string name = "evaluare"; + +ifstream fin(name + ".in"); + +ofstream fout(name + ".out"); + + + +string expression; + + + +bool isOperator(char ch) + +{ + + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; + +} + + + +int getPriority(char ch) + +{ + + if (ch == '+' || ch == '-') + + { + + return 1; + + } + + else if (ch == '*' || ch == '/') + + { + + return 2; + + } + + return -1; + +} + + + +void applyOperator(stack& numbers, char op) + +{ + + int secondNo = numbers.top(); + + numbers.pop(); + + int firstNo = numbers.top(); + + numbers.pop(); + + switch (op) + + { + + case '+': + + { + + numbers.push(firstNo + secondNo); + + break; + + } + + case '-': + + { + + numbers.push(firstNo - secondNo); + + break; + + } + + case '*': + + { + + numbers.push(firstNo * secondNo); + + break; + + } + + case '/': + + { + + numbers.push(firstNo / secondNo); + + break; + + } + + } + +} + + + +int main() + +{ + + fin >> expression; + + stack numbers; + + stack operators; + + for (size_t i = 0; i < expression.size(); i++) + + { + + if (expression[i] == '(') + + { + + operators.push('('); + + } + + else if (expression[i] == ')') + + { + + while (operators.top() != '(') + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + operators.pop(); + + } + + else if (isOperator(expression[i])) + + { + + while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + + + operators.push(expression[i]); + + } + + else + + { + + assert(isdigit(expression[i])); + + int number = 0; + + while (i < expression.size() && isdigit(expression[i])) + + { + + number = number * 10 + expression[i++] - '0'; + + } + + i--; + + numbers.push(number); + + } + + } + + + + while (!operators.empty()) + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + + + fout << numbers.top() << "\n"; + + return 0; + +} diff --git a/eval/stack/57a73aad-742e-4f62-a3f4-837bec960ad4.cpp b/eval/stack/57a73aad-742e-4f62-a3f4-837bec960ad4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3491bdf413873837b8cf85b06060ac79070688f --- /dev/null +++ b/eval/stack/57a73aad-742e-4f62-a3f4-837bec960ad4.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +string s; + +int i = 0; + +int res = 0; + +stack seq; + + + +int open_bracket = 1000000000 + int('('); + +int closed_bracket = 1000000000 + int(')'); + +int plus_sign = 1000000000 + int('+'); + +int minus_sign = 1000000000 + int('-'); + +int multiplication_sign = 1000000000 + int('*'); + +int division_sign = 1000000000 + int('/'); + + + +int sum_to_closed_bracket() + +{ + + int sum = 0; + + int nr; + + while (seq.top() != open_bracket) + + { + + nr = seq.top(); + + seq.pop(); + + if (seq.top() == plus_sign) + + { + + seq.pop(); + + sum += nr; + + } + + if (seq.top() == minus_sign) + + { + + seq.pop(); + + sum -= nr; + + nr = 0; + + } + + } + + return sum + nr; + +} + + + +void close_bracket() + +{ + + int sum = sum_to_closed_bracket(); + + + + seq.pop(); + + + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int multiplier = seq.top(); + + seq.pop(); + + sum = multiplier * sum; + + } + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int dividend = seq.top(); + + seq.pop(); + + sum = dividend / sum; + + } + + seq.push(sum); + +} + + + +void readNumber() + +{ + + int nr = 0; + + while (i < s.length() && s[i] >= '0' && s[i] <= '9') + + nr = nr * 10 + int(s[i++] - '0'); + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous * nr); + + } + + else + + { + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous / nr); + + } + + else + + seq.push(nr); + + } + + + + --i; + +} + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + cin >> s; + + seq.push(open_bracket); + + + + while (i < s.length()) + + { + + switch (s[i]) + + { + + case ')': + + close_bracket(); + + break; + + case '(': + + seq.push(open_bracket); + + break; + + case '+': + + seq.push(plus_sign); + + break; + + case '-': + + seq.push(minus_sign); + + break; + + case '*': + + seq.push(multiplication_sign); + + break; + + case '/': + + seq.push(division_sign); + + break; + + default: + + readNumber(); + + break; + + } + + ++i; + + } + + + + cout << sum_to_closed_bracket(); + + + + return 0; + +} diff --git a/eval/stack/57cdeffd-c57f-4607-954d-13bd973321ab.cpp b/eval/stack/57cdeffd-c57f-4607-954d-13bd973321ab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5329efabcddfb36323c99922ee2c75854dfb13f --- /dev/null +++ b/eval/stack/57cdeffd-c57f-4607-954d-13bd973321ab.cpp @@ -0,0 +1,238 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + + +string to_eval; + +bool is_operator(char x) +{ + + return x=='+'||x=='-'||x=='*'||x=='/'; + +} + + + +bool is_digit(char a) +{ + + return a>='0' &&a<='9'; +} + +int apply(int a,int b,char op) +{ + + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + default: + return -1; + } + + +} + + +int get_int_at(string&r,int&index) +{ + int result=0; + + while(is_digit(r[index])) + { + result=result*10+(r[index]-'0'); + index++; + } + index--; + return result; +} + + + +int operator_order(char x) +{ + switch(x) + { + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + default: + return -1; + } + +} + +string get_polish_form(string expression) +{ + stack st; + string to_return; + + for(int i=0; i=operator_order(expression[i])) + { + to_return+=st.top(); + st.pop(); + } + st.push(expression[i]); + } + else if(expression[i]==')') + { + while(st.size() && st.top()!='(') + { + to_return+=st.top(); + st.pop(); + } + if(st.top()=='(') + st.pop(); + } + + } + while(st.size()) + { + to_return+=st.top(); + st.pop(); + } + + + return to_return; +} + +int evaluate_polish_form(string polish) +{ + stackprocessed; + for(int i=0; i>to_eval; + string polish=get_polish_form(to_eval); + out< +#include +#include +#include +#include + + +using namespace std; + + + +class ExprEvaluator { + +private: + + + + vector OPERATORS; + + bool isOperator(char); + + int opPriority(char); + + int evaluateSubExpr(int, int, char); + + + +public: + + + + ExprEvaluator(); + + int evaluateExpr(const string &); + +}; + + + +bool ExprEvaluator::isOperator(char chr) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(chr) != string::npos) + + return true; + + } + + return false; + +} + + + +int ExprEvaluator::opPriority(char oper) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(oper) != string::npos) + + return i; + + } + + return -1; + +} + + + +int ExprEvaluator::evaluateSubExpr(int operand1, int operand2, char oper) { + + if (oper == '/') { + + return operand1 / operand2; + + } else if (oper == '*') { + + return operand1 * operand2; + + } else if (oper == '-') { + + return operand1 - operand2; + + } else { + + return operand1 + operand2; + + } + +} + + + +ExprEvaluator::ExprEvaluator() { + + + + this->OPERATORS.push_back("/*"); + + this->OPERATORS.push_back("-+"); + +} + + + +int ExprEvaluator::evaluateExpr(const string & expr) { + + stack operands_stack; + + stack operators_stack; + + int expr_pos = 0; + + + + while (expr_pos < expr.size()) { + + + + if (isdigit(expr[expr_pos])) { + + + + int number = expr[expr_pos] - '0'; + + expr_pos += 1; + + + + while (expr_pos < expr.size() && isdigit(expr[expr_pos])) { + + number = number * 10 + expr[expr_pos] - '0'; + + expr_pos += 1; + + } + + + + operands_stack.push(number); + + + + } else if (this->isOperator(expr[expr_pos])) { + + + + if (operators_stack.empty() or operators_stack.top() == '(' or + + this->opPriority(expr[expr_pos]) < this->opPriority(operators_stack.top())) { + + + + operators_stack.push(expr[expr_pos]); + + + + } else { + + int priority = this->opPriority(expr[expr_pos]); + + + + while (!operators_stack.empty() && operators_stack.top() != '(' && + + priority >= this->opPriority(operators_stack.top())) { + + + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + operators_stack.push(expr[expr_pos]); + + } + + + + expr_pos += 1; + + + + } else if (expr[expr_pos] == '(') { + + + + operators_stack.push('('); + + expr_pos += 1; + + + + } else { + + + + while (operators_stack.top() != '(') { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + operators_stack.pop(); + + expr_pos += 1; + + + + } + + } + + + + while (!operators_stack.empty()) { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + return operands_stack.top(); + +} + + + +int main() { + + string expr; + + + + ifstream inputFile("evaluare.in"); + + inputFile >> expr; + + inputFile.close(); + + + + ExprEvaluator evaluator; + + int ans = evaluator.evaluateExpr(expr); + + + + ofstream outputFile("evaluare.out"); + + outputFile << ans; + + outputFile.close(); + + + + return 0; + +} diff --git a/eval/stack/59048cf5-434e-44d0-82e4-075122c220d7.cpp b/eval/stack/59048cf5-434e-44d0-82e4-075122c220d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37dd5ea2ca80b512e5a11c41fafb755e90e6fa3d --- /dev/null +++ b/eval/stack/59048cf5-434e-44d0-82e4-075122c220d7.cpp @@ -0,0 +1,232 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +char semne[100005]; + +int nre[100005]; + +int top_semne, top_nre; + +int l; + + + +bool isDigit(char a) + +{ + + if(a >= '0' && a <= '9') + + return 1; + + return 0; + +} + + + +void push_semne(char semn) + +{ + + semne[++top_semne] = semn; + +} + + + +void push_nre(int nr) + +{ + + nre[++top_nre] = nr; + +} + + + +void formnr(int &i) + +{ + + int nr = 0; + + while(isDigit(a[i])) + + { + + nr = nr * 10 + (a[i] - '0'); + + ++i; + + } + + --i; + + push_nre(nr); + +} + + + +void calcul(char c) + +{ + + int x = nre[top_nre]; + + nre[top_nre --] = 0; + + int y = nre[top_nre]; + + nre[top_nre --] = 0; + + int rez; + + + + switch(c) + + { + + case '+': + + rez = y + x; + + break; + + case '-': + + rez = y - x; + + break; + + case '*': + + rez = y * x; + + break; + + case '/': + + rez = y / x; + + break; + + } + + push_nre(rez); + + semne[top_semne--] = 0; + + + +} +int grad(char c) + +{ + + if(c == '+' || c == '-' || c == ')') + + return 1; + + if(c == '*' || c == '/') + + return 2; + + return 0; + +} +void eliber(char forWho) + +{ + + int grad_actual = grad(forWho); + + + + while(top_semne != 0 && grad_actual <= grad(semne[top_semne])) + + calcul(semne[top_semne]); + +} +void solve() + +{ + + for(int i = 0; i < l; ++i) + + { + + if(isDigit(a[i])) + + formnr(i); + + else if(a[i] == '(') + + push_semne(a[i]); + + else if(a[i] == ')') + + { + + eliber(')'); + + semne[top_semne --] = 0; + + } + + else{ + + if(top_semne != 0) + + eliber(a[i]); + + push_semne(a[i]); + + } + + + + } + + while(top_semne != 0) + + calcul(semne[top_semne]); + +} + + + + + +int main() + +{ + + f>>a; + + l = strlen(a); + + solve(); + + g< +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int st,qu,x,y; + +string s,nr; + +string q[100005]; + +char a[100005]; + +int aa[100005]; + + + +bool comp(char A,char B) + +{ + + if((A=='+' || A=='-') && B!='(') return true; + + if((A=='*' || A=='/') && (B=='*' || B=='/')) return true; + + return false; + +} + + + +int main() + +{ + + getline(f,s); + + for(int i=0;i='0' && s[i]<='9') + + { + + nr+=s[i]; + + ++i; + + } + + --i; + + q[++qu]=nr; + + } + + } + + while(st) + + { + + q[++qu]=a[st]; + + --st; + + } + + st=0; + + for(int i=1;i<=qu;++i) + + { + + if(q[i][0]>='0' && q[i][0]<='9') + + { + + x=atoi(q[i].c_str()); + + aa[++st]=x; + + } + + else + + { + + y=aa[st]; --st; + + x=aa[st]; --st; + + if(q[i][0]=='+') x+=y; + + if(q[i][0]=='-') x-=y; + + if(q[i][0]=='*') x*=y; + + if(q[i][0]=='/') x/=y; + + aa[++st]=x; + + } + + } + + g< +char ch[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("evaluare.in","r"); + + fout=fopen("evaluare.out","w"); + + int n,i,q=0,nr,k=0; + + n=1; + + while(fscanf(fin,"%c",&ch[n])!=EOF) + + n++; + + i=1; + + while(i<=n){ + + if(ch[i]>='0' && ch[i]<='9'){ + + nr=0; + + while(ch[i]>='0' && ch[i]<='9'){ + + nr=nr*10+ch[i]-'0'; + + i++; + + } + + q++; + + v[q]=nr; + + i--; + + } + + else{ + + if(ch[i]=='('){ + + k++; + + st[k]=-1; + + } + + if(ch[i]==')'){ + + while(st[k]!=-1){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k--; + + } + + if(ch[i]=='*'){ + + while (st[k]==-5 || st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-5; + + } + + if(ch[i]=='/'){ + + while (st[k]==-5||st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-6; + + } + + if(ch[i]=='+'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-3; + + } + + if(ch[i]=='-'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-4; + + } + + } + + i++; + + } + + while(k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + for(i=1;i<=q;i++){ + + if(v[i]>=0){ + + k++; + + st[k]=v[i]; + + } + + else{ + + if(v[i]==-5){ + + if(k<=1) + + st[k]=0; + + else{ + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if(v[i]==-6){ + + k--; + + st[k]=st[k]/st[k+1]; + + } + + else + + if(v[i]==-3){ + + k--; + + st[k]=st[k]+st[k+1]; + + } + + else{ + + k--; + + st[k]=st[k]-st[k+1]; + + } + + } + + } + + fprintf(fout,"%d ",st[1]); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/eval/stack/5b272a3a-6076-402a-8290-de49806c0d1a.cpp b/eval/stack/5b272a3a-6076-402a-8290-de49806c0d1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..284eb99f3dbb3b2e818b427d472488484b4ced02 --- /dev/null +++ b/eval/stack/5b272a3a-6076-402a-8290-de49806c0d1a.cpp @@ -0,0 +1,403 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +class StackException + +{ + +private: + + std::string m_error; + + + +public: + + StackException(std::string error) + + : m_error(error) + + { + + } + + + + const char* getError() + + { + + return m_error.c_str(); + + } + +}; + +template + +struct Element + +{ + + Element* next; + + T data; + +}; + +template + +class Stack + +{ + +private: + + Element *top; + +public: + + Stack(); + + Stack(vector newElements); + + Stack(const Stack &S); + + void Push(T newElement); + + T Pop(); + + T Top() const; + + bool isEmpty() const; + + void printStack(); + +}; + +template + +Stack::Stack() + +{ + + top=NULL; + +} + +template + +Stack::Stack(vector newElements) + +{ + + top=NULL; + + for(auto ¤tElement:newElements) + + Push(currentElement); + +} + +template + +Stack::Stack(const Stack &S) + +{ + + top=NULL; + + vector stackElements; + + int numberStackElements=0; + + Element *currentElement=S.top; + + while(currentElement!=NULL) + + { + + stackElements.push_back(currentElement->data); + + currentElement=currentElement->next; + + numberStackElements++; + + } + + for(int i=numberStackElements-1;i>=0;--i) + + Push(stackElements[i]); + +} + +template + +void Stack::Push(T newElement) + +{ + + Element *newTop=new Element; + + if(!newTop) + + throw StackException("Stack Overflow"); + + newTop->data=newElement; + + newTop->next=top; + + top=newTop; + +} + +template + +T Stack::Top() const + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + return top->data; + +} + +template + +T Stack::Pop() + +{ + + if(isEmpty()) + + throw StackException("Stack has no elements"); + + Element *temp=top; + + T topData=top->data; + + top=top->next; + + temp->next=NULL; + + free(temp); + + return topData; + +} + +template + +bool Stack::isEmpty() const + +{ + + return (top==NULL); + +} + +template + +void Stack::printStack() + +{ + + if(top==NULL) + + throw StackException("Stack has no elements"); + + Element *currentElement=top; + + while(currentElement!=NULL) + + { + + cout<data<<" "; + + currentElement=currentElement->next; + + } + +} + +int applyOperator(int x, int y, char Operator) + +{ + + switch(Operator) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +int priority(char Operator) + +{ + + switch(Operator) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + } + + return 0; + +} + +int DijkstraAlgorithm(string expression) + +{ + + Stack Values; + + Stack Operators; + + for(std::string::iterator it=expression.begin(); it!=expression.end(); ++it) + + { + + char currentCharacter=*it; + + if(currentCharacter==' ') + + continue; + + if(isdigit(currentCharacter)) + + { + + int value=0; + + for(;it!=expression.end()&&isdigit(*it); ++it) + + value=value*10+(*it-'0'); + + --it; + + Values.Push(value); + + continue; + + } + + if(currentCharacter=='(') + + { + + Operators.Push(currentCharacter); + + continue; + + } + + if(currentCharacter==')') + + { + + while(!Operators.isEmpty()&&Operators.Top()!='(') + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + if(!Operators.isEmpty()) + + Operators.Pop(); + + continue; + + } + + int Operator=*it; + + while(!Operators.isEmpty()&&priority(Operator)<=priority(Operators.Top())) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + Operators.Push(Operator); + + } + + while(!Operators.isEmpty()) + + { + + char currentOperator=Operators.Pop(); + + int operand1=Values.Pop(), operand2=Values.Pop(); + + Values.Push(applyOperator(operand2,operand1,currentOperator)); + + } + + return Values.Top(); + +} + +void evaluateExpression() + +{ + + string expression; + + getline(fin,expression); + + fout< +#include +#include + +using namespace std; + +bool is_operator(char to_check) +{ + return to_check =='+'||to_check=='-'||to_check=='/'||to_check=='*'||to_check=='^'; +} + +int apply_operator(int a,int b,char op) +{ + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + case '^': + return a^b; + + + } + + +} + + +int get_precedence(char to_get) +{ + + + switch(to_get) + { + case '^': + return 3; + case '*': + return 2; + case '/': + return 2; + case '+': + return 1; + case '-': + return 1; + } + + return 0; +} + +bool is_character(char to_check) +{ + + return to_check>='0' &&to_check<='9'; + +} + +int get_next_number(const std::string&s,unsigned&cursor) +{ + int to_return=0; + + + while(is_character(s[cursor])) + { + to_return=to_return*10+(s[cursor++]-'0'); + } + cursor--; + + return to_return; +} + + +string get_infix(const std::string&postfix) +{ + stack operators; + string to_return; + + for(unsigned i=0; i values; + + for(unsigned i=0; i>to_eval; + + string infix=get_infix(to_eval); + + out< +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + +struct Node + +{ + + string val; + + Node *left, *right; + +} *root; + +bool priority (Node* n1, Node* n2) + +{ + + string val1 = n1 -> val; + + string val2 = n2 -> val; + + if (val2 == "(" || val1 == "(") + + return true; + + if ((val1 == "+" || val1 == "-") && (val2 == "*" || val2 == "/")) + + return true; + + return false; + +} + +void adjust (stack &op, stack &num) + +{ + + Node* n2 = op.top (); + + Node *nr1, *nr2; + + nr1 = num.top (); + + num.pop (); + + nr2 = num.top (); + + num.pop (); + + n2 -> left = nr1; + + n2 -> right = nr2; + + num.push (n2); + + op.pop (); + +} + +Node* postfix_tree (string s) + +{ + + stack op; + + stack num; + + int i = 0; + + int length = s.size (); + + while (i < length) + + { + + if (s[i] == ' ' || s[i] == '\t') + + i++; + + if (isdigit (s[i])) + + { + + string number; + + while (isdigit (s[i]) && i < length) + + { + + number += s[i]; + + i++; + + } + + Node* n1 = new Node; + + n1 -> val = number; + + n1 -> left = NULL; + + n1 -> right = NULL; + + num.push (n1); + + } + + else + + { + + Node* n1 = new Node; + + n1 -> val += s[i]; + + n1 -> left = NULL; + + n1 -> right = NULL; + + if (op.empty ()) + + op.push (n1); + + else + + { + + if (s[i] == ')') + + { + + while (op.top () -> val != "(") + + adjust (op, num); + + op.pop (); + + } + + else if (priority (op.top (), n1)) + + op.push (n1); + + else + + { + + while ((!op.empty ()) ? (!priority(op.top (), n1)) : 0) + + adjust (op, num); + + op.push (n1); + + } + + } + + i++; + + } + + } + + while (!op.empty ()) + + adjust (op, num); + + return num.top (); + +} + +int evaluate (Node* root) + +{ + + if (root) + + if (root -> val == "+") + + return evaluate (root -> right) + evaluate (root -> left); + + else if (root -> val == "-") + + return evaluate (root -> right) - evaluate (root -> left); + + else if (root -> val == "*") + + return evaluate (root -> right) * evaluate (root -> left); + + else if (root -> val == "/") + + return evaluate (root -> right) / evaluate (root -> left); + + else + + { + + int nr = 0; + + int n = root -> val.size (); + + int i = 0; + + for (int i = 0; i < n; i++) + + nr = nr * 10 + root -> val[i] - '0'; + + return nr; + + } + +} + +void print (Node* root) + +{ + + if (root) + + { + + cout << root -> val << endl; + + print (root -> left); + + print (root -> right); + + } + +} + +int main () + +{ + + getline (fin, s); + + root = postfix_tree (s); + + fout << evaluate (root); + +} diff --git a/eval/stack/5edf38c9-bc37-4aeb-a5c6-538b9b2ce055.cpp b/eval/stack/5edf38c9-bc37-4aeb-a5c6-538b9b2ce055.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7490a6a2bb2e38c9bb2806af761c42a84f40426f --- /dev/null +++ b/eval/stack/5edf38c9-bc37-4aeb-a5c6-538b9b2ce055.cpp @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char expr[100001]; + +map priority; + + + +struct elts { + + int val; + + int type; + +}; + + + +bool isDigit(char c) { + + return (c >= '0' && c <= '9'); + +} + + + +int getNumber(int& i) { + + int ret = 0; + + while (expr[i] != '\0' && isDigit(expr[i])) + + ret = ret * 10 + expr[i] - '0', ++i; + + --i; + + return ret; + +} + + + +bool isOperator(char c) { + + return (c == '+' || c == '-' || c == '*' || c == '/'); + +} + + + +int getRes(int a, int b, char op) { + + if (op == '+') + + return a + b; + + if (op == '-') + + return a - b; + + if (op == '*') + + return a * b; + + if (op == '/') + + return a / b; + +} + + + +int compute() { + + stack op; + + vector val; + + for (int i = 0;expr[i] != '\0';++i) { + + if (isDigit(expr[i])) + + val.push_back({ getNumber(i),0 }); + + else if (isOperator(expr[i])) { + + while (!op.empty() && op.top() != '(' && priority[op.top()] >= priority[expr[i]]) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.push(expr[i]); + + } + + else if (expr[i] == '(') + + op.push('('); + + else { + + while (op.top() != '(') { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.pop(); + + } + + + + } + + while (!op.empty()) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + + + stack res; + + + + for (int i = 0;i < val.size();++i) { + + if (val[i].type == 0) + + res.push(val[i].val); + + else { + + int a = res.top(); + + res.pop(); + + int b = res.top(); + + res.pop(); + + res.push(getRes(b, a, val[i].val)); + + } + + } + + return res.top(); + +} + + + +int main() + +{ + + fin >> expr; + + priority['+'] = 0; + + priority['-'] = 0; + + priority['*'] = 1; + + priority['/'] = 1; + + fout << compute(); + + + + return 0; + +} diff --git a/eval/stack/5f1b9f76-c536-40e4-8f5f-cb5b4b7710e1.cpp b/eval/stack/5f1b9f76-c536-40e4-8f5f-cb5b4b7710e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea694b1f490d2d5676d31bc94b94e0e68a1eccb8 --- /dev/null +++ b/eval/stack/5f1b9f76-c536-40e4-8f5f-cb5b4b7710e1.cpp @@ -0,0 +1,183 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/5f4eae41-e1bc-43a2-b55e-9c153a88f729.cpp b/eval/stack/5f4eae41-e1bc-43a2-b55e-9c153a88f729.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c0ba010b74eab0b2272cd4316323b66c6091804 --- /dev/null +++ b/eval/stack/5f4eae41-e1bc-43a2-b55e-9c153a88f729.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string exp, pol; + + + +int priority(char c) { + + if (c == '(') + + return 0; + + if (c == '-' || c == '+') + + return 1; + + return 2; + +} + + + +void genFormaPoloneza(string s) { + + stack oper; + + int poz = 0; + + + + while (poz < s.length()) { + + if (isdigit(s[poz])) { + + pol += s[poz]; + + while (poz + 1 < s.length() && isdigit(s[poz + 1])) + + pol += exp[++poz]; + + pol += '.'; + + } else if (s[poz] == '(') { + + oper.push('('); + + } else if (s[poz] == ')') { + + while (oper.top() != '(') + + pol += oper.top(), oper.pop(); + + oper.pop(); + + } else { + + while (!oper.empty() && priority(oper.top()) >= priority(s[poz])) + + pol += oper.top(), oper.pop(); + + oper.push(s[poz]); + + } + + poz++; + + } + + + + while (!oper.empty()) + + pol += oper.top(), oper.pop(); + +} + + + +int evalPol(string s) { + + stack num; + + int poz = -1; + + + + while (++poz < s.length()) { + + if (isdigit(s[poz])) { + + int nr = 0; + + do { + + nr = nr = nr * 10 + s[poz] - '0'; + + } while (poz < s.length() && isdigit(s[++poz])); + + + + num.push(nr); + + } else { + + int nr = num.top(); + + num.pop(); + + + + if (s[poz] == '+') + + num.top() += nr; + + else if (s[poz] == '-') + + num.top() -= nr; + + else if (s[poz] == '*') + + num.top() *= nr; + + else if (s[poz] == '/') + + num.top() /= nr; + + } + + } + + + + return num.top(); + +} + + + +int main() { + + fin >> exp; + + genFormaPoloneza(exp); + + fout << evalPol(pol); + + return 0; + +} diff --git a/eval/stack/606c4c7f-b8e6-42b4-ae02-c1490a2c9acd.cpp b/eval/stack/606c4c7f-b8e6-42b4-ae02-c1490a2c9acd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b18ce340e06a149fc460d07698e5b6b38069527 --- /dev/null +++ b/eval/stack/606c4c7f-b8e6-42b4-ae02-c1490a2c9acd.cpp @@ -0,0 +1,171 @@ +#include +#include + + +using namespace std; + + + +stack polo; + +stack op; + + + +int priority(char ch) + +{ + + if (ch=='+'||ch=='-') + + return 1; + + if (ch=='*'||ch=='/') + + return 2; + + return 0; + +} + + + +void move_op() + +{ + + int x,y; + + char semn; + + semn=op.top(); + + op.pop(); + + y=polo.top(); + + polo.pop(); + + x=polo.top(); + + polo.pop(); + + if (semn=='+') + + polo.push(x+y); + + else if (semn=='-') + + polo.push(x-y); + + else if (semn=='*') + + polo.push(x*y); + + else + + polo.push(x/y); + +} + + + +int main() + +{ + + ifstream fin ("evaluare.in"); + + ofstream fout ("evaluare.out"); + + int nr; + + char ch; + + bool v=0; + + while (fin>>ch) + + { + + if ('0'<=ch&&ch<='9') + + if (v) + + nr=nr*10+(ch-'0'); + + else + + { + + v=1; + + nr=ch-'0'; + + } + + else + + { + + if (v) + + { + + polo.push(nr); + + v=0; + + nr=0; + + } + + if (ch=='(') + + op.push(ch); + + else if (ch==')') + + { + + while (op.top()!='(') + + move_op(); + + op.pop(); + + } + + else + + { + + while (!op.empty()&&priority(ch)<=priority(op.top())) + + move_op(); + + op.push(ch); + + } + + } + + } + + if (v) + + polo.push(nr); + + while (!op.empty()) + + move_op(); + + fout< +#include +#include +using namespace std; + + + +const int N = 100005; + + + +string a; + +int st[N], top; + + + +void Read () + +{ + + ifstream fin ("evaluare.in"); + + fin >> a; + + fin.close(); + +} + + + +void Solve () + +{ + + int i; + + for (i = 0; a[i];) + + { + + if (a[i] == '+') + + i++; + + else if (a[i] == '(') + + { + + st[++top] = 1e9 + 1; + + i++; + + } + + else if (a[i] == '*') + + { + + st[++top] = 1e9 + 2; + + i++; + + } + + else if (a[i] == '/') + + { + + st[++top] = 1e9 + 3; + + i++; + + } + + else if (a[i] == '-') + + { + + st[++top] = 1e9 + 4; + + i++; + + } + + else if ('0' <= a[i] && a[i] <= '9') + + { + + int x = 0; + + while ('0' <= a[i] && a[i] <= '9') + + { + + x = x * 10 + a[i] - '0'; + + i++; + + } + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + else + + { + + i++; + + int x = 0; + + while (st[top] != 1e9 + 1) + + { + + x += st[top]; + + top--; + + } + + top--; + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + } + + int ans = 0; + + for (int i = 1; i <= top; i++) + + ans += st[i]; + + ofstream fout ("evaluare.out"); + + fout << ans << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + return 0; + +} diff --git a/eval/stack/61a1e010-dc36-4b7c-9ffe-cf771aac0468.cpp b/eval/stack/61a1e010-dc36-4b7c-9ffe-cf771aac0468.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09fdaf6af6204768e35d4ff7f2681d95095ce54a --- /dev/null +++ b/eval/stack/61a1e010-dc36-4b7c-9ffe-cf771aac0468.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +char s[100005]; +stack operatori; +stack operanzi; + +void evaluare_expresie() { + while (!operatori.empty() && operatori.top() != '(') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + char op = operatori.top(); + operatori.pop(); + switch (op) { + case '+': + operanzi.push(a + b); + break; + case '-': + operanzi.push(a - b); + break; + case '*': + operanzi.push(a * b); + break; + case '/': + operanzi.push(a / b); + break; + } + } +} + +int evaluare() { + int nrl = strlen(s); + for (int i = 0; i < nrl; ++i) { + if (s[i] == '(') + operatori.push('('); + else if (s[i] == ')') { + evaluare_expresie(); + operatori.pop(); + } else if (s[i] == '+' || s[i] == '-') { + evaluare_expresie(); + operatori.push(s[i]); + } else if (s[i] == '*' || s[i] == '/') { + if (!operatori.empty()) { + char inainte = operatori.top(); + if (inainte == '*' || inainte == '/') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + operatori.pop(); + if (inainte == '*') + operanzi.push(a * b); + else + operanzi.push(a / b); + } + } + operatori.push(s[i]); + } else { + int nr = 0, j; + for (j = i; j < nrl && isdigit(s[j]); ++j) + nr = nr * 10 + s[j] - '0'; + operanzi.push(nr); + i = j - 1; + } + } + evaluare_expresie(); + return operanzi.top(); +} + +int main() { + f.getline(s, 100005); + g << evaluare(); + return 0; +} diff --git a/eval/stack/6250950d-3c32-4365-9433-ae80ec3ee60b.cpp b/eval/stack/6250950d-3c32-4365-9433-ae80ec3ee60b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f10fdbc55340f40857285d54c87a915220c504b7 --- /dev/null +++ b/eval/stack/6250950d-3c32-4365-9433-ae80ec3ee60b.cpp @@ -0,0 +1,180 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int prec(char c) + +{ + + if(c=='^')return 3; + + if(c=='*' || c=='/')return 2; + + if(c=='+' || c=='-') return 1; + + return -1; + +} + +string infixToPostfix(string a) + +{ + + string temp=""; + + stacks; + + int i,n=a.size(); + + for(i=0; i>c) + + { + + if(c<'0' || c>'9')a+=c; + + else + + { + + a+='a'; + + int nr = c-'0'; + + while(fin>>c) + + if(c<'0' || c>'9') + + { + + a+=c; + + break; + + } + + else{ + + int cifra=c-'0'; + + nr=nr*10+cifra; + + } + + x[k]=nr; + + ++k; + + } + + } + + k=1; + + a=infixToPostfix(a); + + int n= a.size(); + + stacks; + + for(i=0;i +#include +#include + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include + + + + +using namespace std; + + + +struct solComp + +{ + + int num; + + char c; + +}; + + + +int solve(string s) + +{ + + vector sol; + + vector stiva; + + + + int n = s.length(), m, i; + + + + for(i = 0; i < n; i++) + + { + + if(s[i] == '(')stiva.push_back(s[i]); + + else if(s[i] == '*' || s[i] == '/') + + { + + m = stiva.size() - 1; + + while(m >= 0 && (stiva[m] == '*' || stiva[m] == '/') && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == '+' || s[i] == '-') + + { + + m = stiva.size() - 1; + + while(m >= 0 && stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + + + stiva.push_back(s[i]); + + } + + else if(s[i] == ')') + + { + + m = stiva.size() - 1; + + while(stiva[m] != '(') + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + m--; + + } + + stiva.pop_back(); + + } + + else + + { + + int number = s[i] - '0'; + + + + for(i = i + 1; i < n && '0' <= s[i] && s[i] <= '9'; i++)number = number * 10 + s[i] - '0'; + + + + i--; + + solComp aux; + + + + aux.num = number; + + aux.c = 0; + + + + sol.push_back(aux); + + } + + } + + + + m = stiva.size() - 1; + + + + while(m >= 0) + + { + + solComp aux; + + + + aux.num = -1; + + aux.c = stiva[m]; + + + + sol.push_back(aux); + + stiva.pop_back(); + + + + m--; + + } + + + + n = sol.size(); + + + + for(i = 0; i < n; i++) + + if(sol[i].num == -1) + + { + + if(sol[i].c == '/')stiva[m - 1] = stiva[m - 1] / stiva[m]; + + else if(sol[i].c == '*')stiva[m - 1] = stiva[m - 1] * stiva[m]; + + else if(sol[i].c == '+')stiva[m - 1] = stiva[m - 1] + stiva[m]; + + else stiva[m - 1] = stiva[m - 1] - stiva[m]; + + + + m--; + + + + } + + else + + { + + m++; + + stiva[m] = sol[i].num; + + } + + + + return stiva[0]; + +} + + + +int main() + +{ + + string s; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + cin >> s; + + + + cout << solve(s); + + + + return 0; + +} diff --git a/eval/stack/6637b65b-73e5-4bb3-bb8a-90b211200798.cpp b/eval/stack/6637b65b-73e5-4bb3-bb8a-90b211200798.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ff44097b3d1ea571a57dce3eda08414fc823ae9 --- /dev/null +++ b/eval/stack/6637b65b-73e5-4bb3-bb8a-90b211200798.cpp @@ -0,0 +1,267 @@ +#include +#include +#include +#include + + + + +std::ifstream f("evaluare.in"); + +std::ofstream g("evaluare.out"); + + + +std::string s; + + + +long long order(const char& c){ + + if(c == '+' || c == '-') + + return 1; + + else if(c == '*' || c == '/') + + return 2; + + return 0; + +} + + + +int main(){ + + f >> s; + + + + std::stackval; + + std::stackops; + + + + for(long long i = 0;i < s.length();i++){ + + + + if(s[i] == ' ') + + continue; + + + + if(i == 0 && !isdigit(s[i])){ + + char sign = s[0]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(sign == '-') + + val.push(-1 * value); + + else val.push(value); + + }else if(isdigit(s[i])){ + + long long value{}; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + val.push(value); + + }else if(s[i] == '('){ + + ops.push(s[i]); + + if(s[i + 1] == '-' || s[i + 1] == '+'){ + + i++; + + char op = s[i]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(op == '+') + + val.push(value); + + else if(op == '-') + + val.push(-value); + + } + + }else if(s[i] == ')'){ + + + + while (!ops.empty() && val.size() >= 2 && ops.top() != '(') { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + if(!ops.empty()) + + ops.pop(); + + + + }else{ + + while (!ops.empty() && order(ops.top()) >= order(s[i])&& val.size() >= 2) { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + ops.push(s[i]); + + } + + } + + + + while (!ops.empty() && val.size() >= 2) { + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + g << val.top(); + +} diff --git a/eval/stack/664939e3-d79e-4c8d-b65d-4d235a085e87.cpp b/eval/stack/664939e3-d79e-4c8d-b65d-4d235a085e87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d8b30a066d2379604836bd54aaa506e5cde7f92 --- /dev/null +++ b/eval/stack/664939e3-d79e-4c8d-b65d-4d235a085e87.cpp @@ -0,0 +1,100 @@ +#include + +typedef unsigned long long ull; +typedef long long ll; +using namespace std; +ll ceildiv(ll one, ll two) { + if (one % two == 0) {return one / two;} + else {return one / two + 1;} +} ll power(ll n, ll pow, ll m) { + if (pow == 0) return 1; + if (pow % 2 == 0) { + ll x = power(n, pow / 2, m); + return (x * x) % m; + } + else return (power(n, pow - 1, m) * n) % m; +} ll gcd(ll a, ll b) { + if (!b)return a; + return gcd(b, a % b); +} ll factorial(ll n, ll mod) { + if (n > 1) + return (n * factorial(n - 1, mod)) % mod; + else + return 1; +} ll lcm(ll a, ll b) { + return (a * b) / gcd(a, b); +} vector read(ll n) {vector a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x);} return a;}vector> adj;void init(ll n) {for (ll i = 0; i <= n; i++) { vector a; adj.push_back(a);}} + +bool delim(char c) { + return c == ' '; +} + +bool is_op(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +ll priority (char op) { + if (op == '+' || op == '-') + return 1; + if (op == '*' || op == '/') + return 2; + return -1; +} + +void process_op(stack& st, char op) { + ll r = st.top(); st.pop(); + ll l = st.top(); st.pop(); + switch (op) { + case '+': st.push(l + r); break; + case '-': st.push(l - r); break; + case '*': st.push(l * r); break; + case '/': st.push(l / r); break; + } +} + +ll evaluate(string& s) { + stack st; + stack op; + for (ll i = 0; i < (ll)s.size(); i++) { + if (delim(s[i])) + continue; + + if (s[i] == '(') { + op.push('('); + } else if (s[i] == ')') { + while (op.top() != '(') { + process_op(st, op.top()); + op.pop(); + } + op.pop(); + } else if (is_op(s[i])) { + char cur_op = s[i]; + while (!op.empty() && priority(op.top()) >= priority(cur_op)) { + process_op(st, op.top()); + op.pop(); + } + op.push(cur_op); + } else { + ll number = 0; + while (i < (ll)s.size() && isalnum(s[i])) + number = number * 10 + s[i++] - '0'; + --i; + st.push(number); + } + } + + while (!op.empty()) { + process_op(st, op.top()); + op.pop(); + } + return st.top(); +} + +int32_t main() { + ios_base::sync_with_stdio(false);cin.tie(0);; + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + string s; + cin >> s; + cout << evaluate(s) << '\n'; +} diff --git a/eval/stack/684ea1a9-1335-4366-be97-469e828627b3.cpp b/eval/stack/684ea1a9-1335-4366-be97-469e828627b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eef6042e3d6956441f1ba5078d5d5a627749d430 --- /dev/null +++ b/eval/stack/684ea1a9-1335-4366-be97-469e828627b3.cpp @@ -0,0 +1,249 @@ +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 100005; + +int fp[NMAX], st[NMAX], s[NMAX]; + +char sh[NMAX]; + + + +int main() { + + + + freopen ("evaluare.in", "r", stdin); + + freopen ("evaluare.out", "w", stdout); + + + + int n, nfp, nst, x, i, nr, dei; + + + + scanf ("%s", sh); + + + + n = strlen(sh); + + for (i = 0; i < n; i++) { + + if (sh[i] == '*') + + s[i] = -1; + + if (sh[i] == '/') + + s[i] = -2; + + if (sh[i] == '+') + + s[i] = -3; + + if (sh[i] == '-') + + s[i] = -4; + + if (sh[i] == '(') + + s[i] = -5; + + if (sh[i] == ')') + + s[i] = -6; + + if (sh[i] >= '0' && sh[i] <= '9') + + s[i] = sh[i] - '0'; + + } + + nfp = 0; + + nst = 0; + + i = 0; + + while (i < n) { + + x = s[i]; + + if (x >= 0) { + + nr = 0; + + while (i < n && s[i] >= 0) { + + nr = nr * 10 + s[i]; + + i++; + + } + + nfp++; + + fp[nfp] = nr; + + } + + else { + + while (i < n && s[i] < 0) { + + x = s[i]; + + if (x == -5) { + + nst++; + + st[nst] = x; + + } + + else if (x >= -2) { + + while (nst > 0 && st[nst] >= -2) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst++; + + st[nst] = x; + + } + + else if (x >= -4) { + + while (nst > 0 && st[nst] >= -4) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst++; + + st[nst] = x; + + } + + else { + + while (nst > 0 && st[nst] != -5) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst--; + + } + + i++; + + } + + } + + } + + while (nst > 0) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + + + + + + + nst = 0; + + for (i = 1; i < NMAX; i++) + + st[i] = 0; + + nfp++; + + fp[nfp] = -3; + for (i = 0; i <= nfp; i++) { + + if (fp[i] >= 0) { + + nst++; + + st[nst] = fp[i]; + + dei = 1; + + } + + else { + + nst--; + + if (fp[i] == -1) + + st[nst] *= st[nst + 1]; + + if (fp[i] == -2) + + st[nst] /= st[nst + 1]; + + if (fp[i] == -3) + + st[nst] += st[nst + 1]; + + if (fp[i] == -4) + + st[nst] -= st[nst + 1]; + + } + + } + + if (st[0] != 0) + + printf ("%d", st[0]); + + else + + printf ("%d", st[1]); + + + + + + + + return 0; + +} diff --git a/eval/stack/6a055f72-ec48-4e1f-a51d-6c31992fb3d8.cpp b/eval/stack/6a055f72-ec48-4e1f-a51d-6c31992fb3d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dccdf306a9b7e997ebb21bff0a5ebf947d641ac --- /dev/null +++ b/eval/stack/6a055f72-ec48-4e1f-a51d-6c31992fb3d8.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include + + +using namespace std; + + + +char exp[100002]; + + + +vector postfix_st; + +stack op_st; + + + + + +int get_priority(char op) { + + if (op == '*' || op == '/') + + return 1; + + if (op == '+' || op == '-') + + return 2; + +} + + + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s\n", exp); + + int n = strlen(exp); + + if (exp[n-1] == '\n') { + + exp[n-1] = 0; + + --n; + + } + + + + + + int current_number = 0, res, a, b; + + for(int i=0; i= '0' && exp[i] <='9') { + + current_number = 0; + + while(exp[i] >= '0' && exp[i] <='9' && i = get_priority(op_st.top())) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + op_st.push(exp[i]); + + + + } + + + + while(op_st.size()) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + + + for(int i=0; i= '0') + + op_st.push(postfix_st[i] - '0'); + + else { + + a = op_st.top(); + + op_st.pop(); + + b = op_st.top(); + + op_st.pop(); + + + + switch(postfix_st[i]) { + + case '+': + + res = a + b; + + break; + + case '-': + + res = b-a; + + break; + + case '/': + + res = b/a; + + break; + + case '*': + + res = a*b; + + break; + + } + + + + op_st.push(res); + + } + + } + + + + printf("%d\n", op_st.top()); + + return 0; + +} diff --git a/eval/stack/6a149c77-7565-405a-a87f-8df80b4b70ae.cpp b/eval/stack/6a149c77-7565-405a-a87f-8df80b4b70ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcd6856b88d5f1b9021bd81623015e6dd2165bbf --- /dev/null +++ b/eval/stack/6a149c77-7565-405a-a87f-8df80b4b70ae.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char x[100001]; +int i, lg; + +int nr(); +int prod(); +int suma(); + +int main() +{ + fin >> x; + lg = strlen(x); + fout << suma(); + return 0; +} + +int nr() +{ + int r = 0; + if (x[i] == '(') + { + i++; + r = suma(); + i++; + } + else + { + while (isdigit(x[i])) + { + r = r * 10 + x[i] - 48; + i++; + } + } + return r; +} + +int produs() +{ + int r = nr(); + while (i +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + +long doOperation(char op, long left, long right) { + + switch( op ) { + + case '+': return left+right; + case '-': return left-right; + case '*': return left*right; + case '/': return (left/right); + + default: { + return 0; + } + + } +} + + +void computeNextInStack(stack & numberStack, stack & operatorStack) { + + char op = operatorStack.top(); + operatorStack.pop(); + + long right = numberStack.top(); + numberStack.pop(); + + long left = numberStack.top(); + numberStack.pop(); + + long result = doOperation(op, left, right); + + numberStack.push( result ); +} + +unsigned int priority(char op) { + + switch( op ) { + + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + default: + return 0; + } +} + + +long parseExpression(string const expression) { + + stack numberStack; + stack operatorStack; + + + string::const_iterator pos = expression.begin(); + + + while(pos != expression.end()) { + + char current = *pos; + + switch(current) { + + case '+': + case '-': + case '*': + case '/': + + { + + while( + !operatorStack.empty() && + + priority(operatorStack.top()) >= priority(current) + ) + + { + computeNextInStack(numberStack, operatorStack); + } + + operatorStack.push(current); + + break; + } + + + + case '(': { + + operatorStack.push(current); + + break; + } + + + case ')': { + + if(operatorStack.empty()) { + + + + return 0; + } + + while(operatorStack.top() != '(') { + + computeNextInStack(numberStack, operatorStack); + + + if(operatorStack.empty()) { + + + + return 0; + } + } + + operatorStack.pop(); + break; + } + + + + default: { + + if(isdigit(current)) { + + + long number = 0; + + while( pos != expression.end() && isdigit(*pos)) { + + number *= 10; + number += (*pos)-'0'; + pos++; + + } + + + numberStack.push(static_cast(number)); + + pos--; + + } else { + + } + + break; + } + } + + pos++; + } + + while(!operatorStack.empty()) { + + computeNextInStack(numberStack, operatorStack); + } + + return numberStack.top(); +} + + +int main(int argc, char const *argv[]) +{ + + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string expression; + + getline(fin, expression); + + long result = parseExpression(expression); + + fout< +#include +char c, semn[100001], st[10001]; + + + +int fp[100001]; + +long long steval[101]; + + + +using namespace std; + + + +int main() { + + int nr, gasitnr, n, i, k; + + FILE *fin, *fout; + + + + fin=fopen("evaluare.in", "r"); + + fout=fopen("evaluare.out", "w"); + + c=fgetc( fin ); + + nr=0; + + n=0; + + k=0; + + gasitnr=0; + + while(c!=EOF) { + + if(c>='0' && c<='9') { + + gasitnr=1; + + nr=nr*10+(c-48); + + } + + else { + + if(gasitnr) { + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + + + } + + if(c=='(' || c=='-' || c=='+' || c=='/' || c=='*' ) { + + k++; + + st[k]=c; + + if(st[k]=='+' || st[k]=='-') { + + while(st[k-1]=='+' || st[k-1]=='-' || st[k-1]=='*' || st[k-1]=='/') { + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + else if(st[k]=='*' || st[k]=='/') { + + while(st[k-1]=='*' || st[k-1]=='/') { + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + } + + else if(c==')') { + + while(st[k]!='(') { + + n++; + + semn[n]=st[k]; + + k--; + + } + + k--; + + } + + } + + c=fgetc( fin ); + + } + + + + if(gasitnr) { + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + while(k) { + + n++; + + semn[n]=st[k]; + + k--; + + } + + k=0; + + for(i=1; i<=n; i++) { + + if(semn[i]!='+' && semn[i]!='-' && semn[i]!='*' && semn[i]!='/') { + + k++; + + steval[k]=fp[i]; + + } + + else { + + if(semn[i]=='+') { + + steval[k-1]=steval[k-1]+steval[k]; + + } + + if(semn[i]=='-') { + + steval[k-1]=steval[k-1]-steval[k]; + + } + + if(semn[i]=='*') { + + steval[k-1]=steval[k-1]*steval[k]; + + } + + if(semn[i]=='/') { + + steval[k-1]=steval[k-1]/steval[k]; + + } + + k--; + + } + + } + + fprintf( fout, "%d", steval[1]); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/eval/stack/6f6391c1-2969-4867-8d01-9a74c65771ca.cpp b/eval/stack/6f6391c1-2969-4867-8d01-9a74c65771ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed03f55dd6df1b7a47227ccce53727797705fb9 --- /dev/null +++ b/eval/stack/6f6391c1-2969-4867-8d01-9a74c65771ca.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +stack nr; + +stack operatori; + +int lungime; + +char sir[100001]; + +int putere(char semn) + +{ + + if(semn == '+' || semn == '-') + + return 1; + + if(semn == '*' || semn == '/') + + return 2; + + return 0; + +} + +int compune_nr(int &indice) + +{ + + int nr = 0; + + while(indice +#include +#include + +using namespace std; + +inline int app_op ( int x , int y , char op ) +{ + if ( op == '+' ) return x + y ; + if ( op == '-' ) return y - x ; + if ( op == '*' ) return x * y ; + if ( op == '/' ) return y / x ; + return 0 ; +} + +inline int priority ( char op ) +{ + if ( op == '+' || op == '-' ) return 1 ; + if ( op == '*' || op == '/' ) return 2 ; + return 0 ; +} + +void solve ( void ) +{ + stack < char > operatori ; + stack < int > operanzi ; + char ch , op ; + int nr = 0 , x1 , x2 , ok = 0 ; + + while ( scanf ( "%c" , & ch ) != EOF ) + { + if ( isdigit ( ch ) ) + nr = nr * 10 + ( int ) ( ch - '0' ) , ok = 1 ; + else + { + if ( ok ) + { + operanzi.push ( nr ) ; + nr = 0 ; + ok = 0 ; + } + + if ( ch == '(' ) + operatori.push ( '(' ) ; + else + if ( ch == ')' ) + { + while ( operatori.top () != '(' ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + operatori.pop () ; + } + else + if ( ch == '+' || ch == '-' || ch == '*' || ch == '/' ) + { + while ( ! operatori.empty () && priority ( ch ) <= priority ( operatori.top () ) ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + operatori.push ( ch ) ; + } + } + } + + while ( ! operatori.empty () ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + printf ( "%d" , operanzi.top () ) ; +} + +int main() +{ + freopen ( "evaluare.in" , "r" , stdin ) ; + freopen ( "evaluare.out" , "w" , stdout ) ; + + solve () ; + + return 0; +} diff --git a/eval/stack/700231d3-4e69-4fc8-be39-d1cb1bd4cc04.cpp b/eval/stack/700231d3-4e69-4fc8-be39-d1cb1bd4cc04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ffa00f8170d440dd6f2775d3feb83e9beeb0a93 --- /dev/null +++ b/eval/stack/700231d3-4e69-4fc8-be39-d1cb1bd4cc04.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include +#include +#include + +using namespace std; +ifstream f ("evaluare.in"); +ofstream g ("evaluare.out"); +char buff [5 + (int)1e6]; +short x [300]; +stack st , ans; +vector eval; +void solve (int semn){ + int b = ans . top (); + ans . pop (); + int a = ans . top (); + ans . pop (); + if (semn == -3) + ans . push (a * b); + if (semn == -4) + ans . push (a / b); + if (semn == -5) + ans . push (a + b); + if (semn == -6) + ans . push (a - b); + return; + +} +bool check (int x , int y){ + if (x >= y) + return true; + if (x == -6 && y == -5) + return true; + if (x == -4 && y == -3) + return true; + return false; +} +int main() +{ + x ['+'] = -5 , x ['-'] = -6 , x ['*'] = -3 , x ['/'] = -4 , x ['('] = -1 , x [')'] = -2; + f >> buff; + int a; + for(int i = 0 ; buff [i] ; i){ + if (isdigit (buff [i])){ + a = 0; + while (isdigit (buff [i])) + a = a * 10 + (buff [i ++] - '0'); + eval . push_back (a); + } + else{ + int val = x [buff [i]]; + if (val == -1) + st . push (val); + else + if (val == -2){ + while (0 < st . size () && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + if (0 < st . size () && st . top () == -1) + st . pop (); + } + else{ + while (0 < st . size () && check (st . top () , val) && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + st . push (val); + } + ++ i; + } + } + while (0 < st . size ()) + eval . push_back (st . top ()) , st . pop (); + for(auto x : eval){ + if (x < 0) + solve (x); + else + ans . push (x); + } + g << ans . top (); + return 0; +} diff --git a/eval/stack/70899e8e-a29d-4788-b2f7-7890a892ace8.cpp b/eval/stack/70899e8e-a29d-4788-b2f7-7890a892ace8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dccdf306a9b7e997ebb21bff0a5ebf947d641ac --- /dev/null +++ b/eval/stack/70899e8e-a29d-4788-b2f7-7890a892ace8.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include + + +using namespace std; + + + +char exp[100002]; + + + +vector postfix_st; + +stack op_st; + + + + + +int get_priority(char op) { + + if (op == '*' || op == '/') + + return 1; + + if (op == '+' || op == '-') + + return 2; + +} + + + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s\n", exp); + + int n = strlen(exp); + + if (exp[n-1] == '\n') { + + exp[n-1] = 0; + + --n; + + } + + + + + + int current_number = 0, res, a, b; + + for(int i=0; i= '0' && exp[i] <='9') { + + current_number = 0; + + while(exp[i] >= '0' && exp[i] <='9' && i = get_priority(op_st.top())) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + op_st.push(exp[i]); + + + + } + + + + while(op_st.size()) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + + + for(int i=0; i= '0') + + op_st.push(postfix_st[i] - '0'); + + else { + + a = op_st.top(); + + op_st.pop(); + + b = op_st.top(); + + op_st.pop(); + + + + switch(postfix_st[i]) { + + case '+': + + res = a + b; + + break; + + case '-': + + res = b-a; + + break; + + case '/': + + res = b/a; + + break; + + case '*': + + res = a*b; + + break; + + } + + + + op_st.push(res); + + } + + } + + + + printf("%d\n", op_st.top()); + + return 0; + +} diff --git a/eval/stack/70aa64e7-aba7-4c45-a286-0977ed1b3778.cpp b/eval/stack/70aa64e7-aba7-4c45-a286-0977ed1b3778.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5c3922abb522f09a1bf484e5e65e2693d140539 --- /dev/null +++ b/eval/stack/70aa64e7-aba7-4c45-a286-0977ed1b3778.cpp @@ -0,0 +1,259 @@ +#include +#include +#include + + +std::ifstream in("evaluare.in"); + +std::ofstream out("evaluare.out"); + + + + + +int Priority(char x) + +{ + + if (x == '+' || x == '-') + + return 1; + + if (x == '*' || x == '/') + + return 2; + + + + return 0; + +} + + + + + + + +int ApplyOperator(int a, int b, char op) + +{ + + switch (op) + + { + + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + } + +} + + + +int Evaluate(std::string& Stream) + +{ + + int i; + + + + + + std::stack Valori; + + + + + + std::stack Operatori; + + + + int n = Stream.length(); + + + + for (i = 0; i < n; i++) + + { + + if (Stream[i] == '(') + + { + + Operatori.push(Stream[i]); + + } + + + + + + else if (isdigit(Stream[i])) + + { + + int numar = 0; + + + + while (i < Stream.length() && isdigit(Stream[i])) + + { + + numar = (numar * 10) + (Stream[i] - '0'); + + i++; + + } + + i--; + + + + Valori.push(numar); + + } + + + + + + else if (Stream[i] == ')') + + { + + + + while(!Operatori.empty() && Operatori.top() != '(') + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + + + Operatori.pop(); + + } + + + + else + + { + while (!Operatori.empty() && Priority(Operatori.top()) >= Priority(Stream[i])) + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + Operatori.push(Stream[i]); + + } + + + + } + + + + while (!Operatori.empty()) + + { + + + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + return Valori.top(); + +} + + + +int main() + +{ + + std::string Stream; + + in >> Stream; + + out << Evaluate(Stream); + +} diff --git a/eval/stack/718ba524-09b4-4416-942e-8c87970eb609.cpp b/eval/stack/718ba524-09b4-4416-942e-8c87970eb609.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d2042881084b2f120fd7e6a097975bbc6eeaae3 --- /dev/null +++ b/eval/stack/718ba524-09b4-4416-942e-8c87970eb609.cpp @@ -0,0 +1,171 @@ +#include +#include +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c,semn[100000],st[100000]; + +int i,n,k,nr,gasitnr,fp[100000]; + +long long steval[1000]; + + + +int main(){ + + while(f >> c){ + + if(c >= '0' && c <= '9'){ + + gasitnr = 1; + + nr *= 10; + + nr += c-'0'; + + }else{ + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + if(c == '(' || c == '+' || c == '-' || c == '*' || c == '/'){ + + k++; + + st[k] = c; + + if(st[k] == '+' || st[k] == '-'){ + + while(st[k-1] == '+' || st[k-1] == '-' || st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + + } + + }else{ + + if(st[k] == '*' || st[k] == '/'){ + + while(st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + +; } + + } + + } + + }else{ + + if(c == ')'){ + + while(st[k] != '('){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k--; + + } + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + while(k){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k = 0; + + for(int i = 1; i <= n; i++){ + + if(semn[i] != '+' && semn[i] != '-' && semn[i] != '*' && semn[i] != '/'){ + + k++; + + steval[k] = fp[i]; + + }else{ + + switch(semn[i]){ + + case '+': steval[k-1] = steval[k-1]+steval[k]; break; + + case '-': steval[k-1] = steval[k-1]-steval[k]; break; + + case '*': steval[k-1] = steval[k-1]*steval[k]; break; + + case '/': steval[k-1] = steval[k-1]/steval[k]; break; + + default: + + break; + + } + + k--; + + } + + } + + g << steval[1]; + + return 0; + +} diff --git a/eval/stack/72050c62-a457-494c-ac2f-caa5537fac4e.cpp b/eval/stack/72050c62-a457-494c-ac2f-caa5537fac4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c2695bb04d9906e519792e152c42dd6ae2aab4 --- /dev/null +++ b/eval/stack/72050c62-a457-494c-ac2f-caa5537fac4e.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +char s[100005]; +stack operatori; +stack operanzi; + +void evaluare_expresie() { + while (!operatori.empty() && operatori.top() != '(') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + char op = operatori.top(); + operatori.pop(); + switch (op) { + case '+': + operanzi.push(a + b); + break; + case '-': + operanzi.push(a - b); + break; + case '*': + operanzi.push(a * b); + break; + case '/': + operanzi.push(a / b); + break; + } + } +} + +int evaluare() { + int nrl = strlen(s); + for (int i = 0; i < nrl; ++i) { + if (s[i] == '(') + operatori.push('('); + else if (s[i] == ')') { + evaluare_expresie(); + operatori.pop(); + } else if (s[i] == '+' || s[i] == '-') { + evaluare_expresie(); + operatori.push(s[i]); + } else if (s[i] == '*' || s[i] == '/') { + if (!operatori.empty()) { + char inainte = operatori.top(); + if (inainte == '*' || inainte == '/') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + operatori.pop(); + if (inainte == '*') + operanzi.push(a * b); + else + operanzi.push(a / b); + } + } + operatori.push(s[i]); + } else { + int nr = 0, j; + for (j = i; j < nrl && isdigit(s[j]); ++j) + nr = nr * 10 + s[j] - '0'; + operanzi.push(nr); + i = j - 1; + } + } + evaluare_expresie(); + return operanzi.top(); +} + +int main() { + f.getline(s, 100005); + g << evaluare(); + return 0; +} diff --git a/eval/stack/72e089e3-8829-494e-b909-dd3392e44240.cpp b/eval/stack/72e089e3-8829-494e-b909-dd3392e44240.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d5553860a73d18077ad6ef60a7de5aaeee0d9ff --- /dev/null +++ b/eval/stack/72e089e3-8829-494e-b909-dd3392e44240.cpp @@ -0,0 +1,199 @@ +#include +#include + + + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int n, i; + + + +int stiv_num[100005]; + +char stiv_sign[100005], x[100005]; + +int numbers = 0, signs = 0; + + + +int getNr() + +{ + + int rez = 0; + + for(i = i; i < n && (x[i] >= '0' && x[i] <= '9'); i++) + + rez = rez * 10 + (x[i] - '0'); + + + + i--; + + return rez; + +} + +void solveParanthese() + +{ + + int rez = 0; + + while(stiv_sign[signs] != '(') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; + + + + if(stiv_sign[signs] == '+') + + rez += nrNow; + + if(stiv_sign[signs] == '-') + + rez -= nrNow; + + signs--; + + } + + signs--; + + + + stiv_num[numbers] += rez; + + if(stiv_sign[signs] == '*') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] *= nrNow; + + } + + if(stiv_sign[signs] == '/') + + { + + int nrNow = stiv_num[numbers]; + + numbers--; signs--; + + + + stiv_num[numbers] /= nrNow; + + } + +} + + + +int main() + +{ + + f.getline(x, 100005); + + n = strlen(x); + + + + stiv_sign[++signs] = '('; + + for(i = 0; i < n; i++) + + { + + if(x[i] == '(') + + stiv_sign[++signs] = '('; + + + + if(x[i] >= '0' && x[i] <= '9') + + { + + int nrNow = getNr(); + + + + if(stiv_sign[signs] == '*') + + { + + stiv_num[numbers] *= nrNow; + + signs--; + + continue; + + } + + if(stiv_sign[signs] == '/') + + { + + stiv_num[numbers] /= nrNow; + + signs--; + + continue; + + } + + + + stiv_num[++numbers] = nrNow; + + continue; + + } + + + + if(x[i] == '*' || x[i] == '/' || x[i] == '+' || x[i] == '-') + + stiv_sign[++signs] = x[i]; + + + + if(x[i] == ')') + + solveParanthese(); + + } + + solveParanthese(); + + + + g << stiv_num[1]; + + return 0; + +} diff --git a/eval/stack/73552950-0e71-45c8-9eaf-b8fe90848324.cpp b/eval/stack/73552950-0e71-45c8-9eaf-b8fe90848324.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c448ee2b92d6abc5460290f5745452cd1177474c --- /dev/null +++ b/eval/stack/73552950-0e71-45c8-9eaf-b8fe90848324.cpp @@ -0,0 +1,234 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +char semne[100005]; + +int nre[100005]; + +int top_semne, top_nre; + +int l; + + + +bool isDigit(char a) + +{ + + if(a >= '0' && a <= '9') + + return 1; + + return 0; + +} + + + +void push_semne(char semn) + +{ + + semne[++top_semne] = semn; + +} + + + +void push_nre(int nr) + +{ + + nre[++top_nre] = nr; + +} + + + +void formnr(int &i) + +{ + + int nr = 0; + + while(isDigit(a[i])) + + { + + nr = nr * 10 + (a[i] - '0'); + + ++i; + + } + + --i; + + push_nre(nr); + +} + + + +void calcul(char c) + +{ + + int x = nre[top_nre]; + + nre[top_nre --] = 0; + + int y = nre[top_nre]; + + nre[top_nre --] = 0; + + int rez; + + + + switch(c) + + { + + case '+': + + rez = y + x; + + break; + + case '-': + + rez = y - x; + + break; + + case '*': + + rez = y * x; + + break; + + case '/': + + rez = y / x; + + break; + + } + + push_nre(rez); + + semne[top_semne--] = 0; + + + +} +int grad(char c) + +{ + + if(c == '+' || c == '-' || c == ')') + + return 1; + + if(c == '*' || c == '/') + + return 2; + + return 0; + +} +void eliber(char forWho) + +{ + + int grad_actual = grad(forWho); + + + + while(top_semne != 0 && grad_actual <= grad(semne[top_semne])) + + calcul(semne[top_semne]); + +} +void solve() + +{ + + for(int i = 0; i < l; ++i) + + { + + if(isDigit(a[i])) + + formnr(i); + + else if(a[i] == '(') + + push_semne(a[i]); + + else if(a[i] == ')') + + { + + if(top_semne != 0) + + eliber(')'); + + semne[top_semne --] = 0; + + } + + else{ + + if(top_semne != 0) + + eliber(a[i]); + + push_semne(a[i]); + + } + + + + } + + while(top_semne != 0) + + calcul(semne[top_semne]); + +} + + + + + +int main() + +{ + + f>>a; + + l = strlen(a); + + solve(); + + g< +#include + + + + +int evaluate(int op1, int op2, char op) + +{ + + auto res = 0; + + switch(op) + + { + + case '+': + + res = op1 + op2; + + break; + + case '-': + + res = op1 - op2; + + break; + + case '*': + + res = op1 * op2; + + break; + + case '/': + + res = op1 / op2; + + break; + + } + + + + return res; + +} + + + +void evaluate(std::stack& st, std::stack& op) + +{ + + auto op2 = st.top(); + + st.pop(); + + auto op1 = st.top(); + + st.pop(); + + + + st.push(evaluate(op1, op2, op.top())); + + op.pop(); + +} + + + +int extractValue(const std::string& input, unsigned long long& i) + +{ + + auto value = 0; + + while((input[i] >= '0' && input[i] <= '9') && i < input.size()) + + { + + value = value*10 + input[i] - '0'; + + ++i; + + } + + --i; + + + + return value; + +} +int main() + +{ + + std::ifstream fin("evaluare.in"); + + std::ofstream fout("evaluare.out"); + + std::string input; + + fin>>input; + + + + std::stack st; + + std::stack op; + + for(auto i=0ULL; i < input.size(); ++i) + + { + + auto ch = input[i]; + + if(ch == '(' || ch == '*' || ch == '/') + + { + + op.push(ch); + + } + + if(ch >= '0' && ch <= '9') + + { + + st.push(extractValue(input, i)); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + if(ch == '+' || ch == '-') + + { + + if(!op.empty() && op.top() != '(') + + { + + evaluate(st, op); + + } + + op.push(ch); + + } + + if(ch == ')') + + { + + while(op.top() != '(') + + { + + evaluate(st, op); + + } + + op.pop(); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + + + } + + + + while(!op.empty()) + + { + + evaluate(st, op); + + + + } + + + + fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char infix[100000 + 1]; + + + +int evaluate_basic(int A, int B, char c) + +{ + + if(c == '*') return A * B; + + if(c == '/') return A / B; + + if(c == '+') return A + B; + + if(c == '-') return A - B; + + + + return -1; + +} + + + + + +int operator_priority(char c) + +{ + + if(c == '+' || c == '-') return 1; + + if(c == '*' || c == '/') return 2; + + + + return -1; + +} + + + +int get_operand(int &i) + +{ + + int operand = 0; + + + + while(infix[i] >= '0' && infix[i] <= '9' && infix[i] != '\0') + + { + + operand = operand * 10 + (infix[i] - '0'); + + ++i; + + } + + + + i--; + + + + + + return operand; + +} + + + +int evaluate() + +{ + + stack operators; + + vector operands; + + + + + + + + for(int i = 0; infix[i] != '\0'; i++) + + { + + if(infix[i] >= '0' && infix[i] <= '9') operands.push_back(get_operand(i)); + + else if(infix[i] == '(') operators.push(infix[i]); + + else + + { + + while(!operators.empty() + + && operators.top() != '(' + + && operator_priority(infix[i]) <= operator_priority(operators.top())) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + + + + if(infix[i]==')') operators.pop(); + + else operators.push(infix[i]); + + } + + + + + + } + + + + while(!operators.empty()) + + { + + size_t len = operands.size(); + + + + operands[len - 2] = evaluate_basic(operands[len - 2], operands[len - 1], operators.top()); + + + + operands.pop_back(); + + + + operators.pop(); + + } + return operands[0]; + +} + + + + + +int main() + +{ + + fin.getline(infix, 100000 + 1, '\n'); + + fout << evaluate(); + + + +} diff --git a/eval/stack/77b30117-bdfe-45a2-b045-2ad20f267f48.cpp b/eval/stack/77b30117-bdfe-45a2-b045-2ad20f267f48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbe4630d0945b7b6d20914e6410dd443648a11a2 --- /dev/null +++ b/eval/stack/77b30117-bdfe-45a2-b045-2ad20f267f48.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +string nume_problema = "evaluare"; +ifstream fin(nume_problema + ".in"); +ofstream fout(nume_problema + ".out"); + + + +enum operand +{ + ADD = '+', + SUB = '-', + MUL = '*', + DIV = '/', + LPR = '(', + RPR = ')', + VAL = '0' +}; +struct expr +{ + vector> ops; + vector> tmp; + expr() + { + } + void grad2() + { + bool lastmult = false; + for (int i = 1; i < ops.size(); i += 2) + { + switch (ops[i].first) + { + case MUL: + case DIV: + { + int prod = ops[i - 1].second; + while (i < ops.size() && (ops[i].first == MUL || ops[i].first == DIV)) + { + if (ops[i].first == MUL) + prod *= ops[i + 1].second; + if (ops[i].first == DIV) + prod /= ops[i + 1].second; + i += 2; + } + if (i >= ops.size()) + lastmult = true; + ops[i - 1].second = prod; + } + default: + tmp.push_back(ops[i - 1]); + if (i < ops.size()) + tmp.push_back(ops[i]); + break; + } + } + if (!lastmult) + tmp.push_back(ops.back()); + } + int grad1() + { + int semn = 1, total = 0; + for (int i = 0; i < tmp.size(); i++) + { + if (tmp[i].first == ADD) + semn = 1; + else if (tmp[i].first == SUB) + semn = -1; + else + total += tmp[i].second * semn; + } + return total; + } + int execute() + { + grad2(); + return grad1(); + } + void dbg() + { + for (const auto &i : ops) + cerr << "OPS:" << char(i.first) << " " << i.second << endl; + for (const auto &i : tmp) + cerr << "TMP:" << i.first << " " << i.second << endl; + } +}; +stack st; + +int main() +{ + expr blank; + st.push(blank); + int val = 0; + for (char c; fin >> c;) + { + if (c == '(') + { + expr blank; + st.push(blank); + } + else if (c == ')') + { + st.top().ops.push_back(make_pair(VAL, val)); + val = st.top().execute(); + st.pop(); + } + else if (isdigit(c)) + val = val * 10 + (c - '0'); + else + { + st.top().ops.push_back(make_pair(VAL, val)); + val = 0; + if (c == ADD) + st.top().ops.push_back(make_pair(ADD, -1)); + if (c == SUB) + st.top().ops.push_back(make_pair(SUB, -1)); + if (c == MUL) + st.top().ops.push_back(make_pair(MUL, -1)); + if (c == DIV) + st.top().ops.push_back(make_pair(DIV, -1)); + } + } + st.top().ops.push_back(make_pair(VAL, val)); + + + fout << st.top().execute(); + return 0; +} diff --git a/eval/stack/7870677e-2df0-4c5c-9261-0450ecb16fab.cpp b/eval/stack/7870677e-2df0-4c5c-9261-0450ecb16fab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d4c246c8dd23b371fa62532d6e48fd3ea8a4256 --- /dev/null +++ b/eval/stack/7870677e-2df0-4c5c-9261-0450ecb16fab.cpp @@ -0,0 +1,202 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +short Imp(char op) + +{ + + if (op == '+' || op == '-') + + return 1; + + if (op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int Operatie(int a, int b, char op) + +{ + + if (op == '+')return a + b; + + if (op == '-')return a - b; + + if (op == '*')return a * b; + + if (op == '/')return a / b; + +} + + + +int eval(string s) + +{ + + stack t; + + stackop; + + for (int i = 0; i < s.size(); i++) + + { + + + + if (s[i] == '(') + + op.push(s[i]); + + else if (isdigit(s[i])) + + { + + int val = 0; + + while (i < s.size() && isdigit(s[i])) { + + val = val * 10 + s[i] - '0'; + + i++; + + } + + t.push(val); + + i--; + + } + + else if (s[i] == ')') + + { + + while (!op.empty() && op.top() != '(') + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + if (!op.empty())op.pop(); + + } + + else + + { + + + + while (!op.empty() && Imp(op.top()) >= Imp(s[i])) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + op.push(s[i]); + + } + + } + + + + while (!op.empty()) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + + + } + + return t.top(); + +} + + + +int main() + +{ + + string s; + + fin >> s; + + fout << eval(s); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/78bbfd98-10ea-414b-953e-3e0fda4081b5.cpp b/eval/stack/78bbfd98-10ea-414b-953e-3e0fda4081b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a4908d27f3b1e17b07abd3451088deb41b777cd --- /dev/null +++ b/eval/stack/78bbfd98-10ea-414b-953e-3e0fda4081b5.cpp @@ -0,0 +1,253 @@ +#include +#include +using namespace std; + +ifstream cin ("evaluare.in"); + +ofstream cout ("evaluare.out"); + +char vc[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + int n,i,nr,cnt=0,k=0,ok=0; + + cin>>vc+1; + + n=strlen(vc+1); + + i=1; + + while (i<=n) + + { + + if (vc[i]>='0'&&vc[i]<='9') + + { + + nr=0; + + while (vc[i]>='0'&&vc[i]<='9') + + { + + nr=nr*10+vc[i]-'0'; + + i++; + + } + + v[++cnt]=nr; + + i--; + + } + + else + + { + + if (vc[i]=='(') + + st[++k]=-1; + + else + + if (vc[i]==')') + + { + + while (st[k]!=-1) + + { + + v[++cnt]=st[k]; + + k--; + + } + + k--; + + } + + else + + if (vc[i]=='+') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-3; + + } + + else + + if (vc[i]=='-') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-4; + + } + + else + + if (vc[i]=='*') + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-5; + + } + + else + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-6; + + } + + } + + i++; + + } + + while (k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + + + for (i=1;i<=cnt;i++) + + { + + if (v[i]>=0) + + st[++k]=v[i]; + + else + + { + + if (v[i]==-5) + + { + + + + if (k<=1) + + st[k]=0; + + else + + { + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if (v[i]==-6) + + { + + + + k--; + + st[k]/=(st[k+1]); + + } + + else + + if (v[i]==-3) + + { + + k--; + + st[k]+=st[k+1]; + + } + + else + + { + + k--; + + st[k]-=st[k+1]; + + } + + } + + } + + cout< +char ch[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("evaluare.in","r"); + + fout=fopen("evaluare.out","w"); + + int n,i,q=0,nr,k=0; + + n=1; + + while(fscanf(fin,"%c",&ch[n])!=EOF) + + n++; + + i=1; + + while(i<=n){ + + if(ch[i]>='0' && ch[i]<='9'){ + + nr=0; + + while(ch[i]>='0' && ch[i]<='9'){ + + nr=nr*10+ch[i]-'0'; + + i++; + + } + + q++; + + v[q]=nr; + + i--; + + } + + else{ + + if(ch[i]=='('){ + + k++; + + st[k]=-1; + + } + + if(ch[i]==')'){ + + while(st[k]!=-1){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k--; + + } + + if(ch[i]=='*'){ + + while (st[k]==-5 || st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-5; + + } + + if(ch[i]=='/'){ + + while (st[k]==-5||st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-6; + + } + + if(ch[i]=='+'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-3; + + } + + if(ch[i]=='-'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-4; + + } + + } + + i++; + + } + + while(k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + for(i=1;i<=q;i++){ + + if(v[i]>=0){ + + k++; + + st[k]=v[i]; + + } + + else{ + + if(v[i]==-5){ + + if(k<=1) + + st[k]=0; + + else{ + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if(v[i]==-6){ + + k--; + + st[k]=st[k]/st[k+1]; + + } + + else + + if(v[i]==-3){ + + k--; + + st[k]=st[k]+st[k+1]; + + } + + else{ + + k--; + + st[k]=st[k]-st[k+1]; + + } + + } + + } + + fprintf(fout,"%d ",st[1]); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/eval/stack/79b8b86d-7274-41e9-8d76-d0d9e838f4ac.cpp b/eval/stack/79b8b86d-7274-41e9-8d76-d0d9e838f4ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b82b721483551445ef7a7748e8903fc4acf6eda --- /dev/null +++ b/eval/stack/79b8b86d-7274-41e9-8d76-d0d9e838f4ac.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + +using namespace std; + +bool is_operator(char to_check) +{ + return to_check =='+'||to_check=='-'||to_check=='/'||to_check=='*'||to_check=='^'; +} + +int apply_operator(int a,int b,char op) +{ + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + case '^': + return a^b; + + + } + + +} + + +int get_precedence(char to_get) +{ + + + switch(to_get) + { + case '^': + return 3; + case '*': + return 2; + case '/': + return 2; + case '+': + return 1; + case '-': + return 1; + } + + return 0; +} + +bool is_character(char to_check) +{ + + return to_check>='0' &&to_check<='9'; + +} + +int get_next_number(const std::string&s,unsigned&cursor) +{ + int to_return=0; + + + while(is_character(s[cursor])) + { + to_return=to_return*10+(s[cursor++]-'0'); + } + cursor--; + + return to_return; +} + + +string get_infix(const std::string&postfix) +{ + stack operators; + string to_return; + + for(unsigned i=0; i values; + + for(unsigned i=0; i>to_eval; + + string infix=get_infix(to_eval); + + out< +#include +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char vc[100005]; + +int v[100005], st[100005]; + +int main() + +{ + + int n, i, nr, cnt=0, k=0, ok=0; + + fin>>vc+1; + + n=strlen(vc+1); + + i=1; + + while (i<=n) + + { + + if (vc[i]>='0'&&vc[i]<='9') + + { + + nr=0; + + while (vc[i]>='0'&&vc[i]<='9') + + { + + nr=nr*10+vc[i]-'0'; + + i++; + + } + + v[++cnt]=nr; + + i--; + + } + + else + + { + + if (vc[i]=='(') + + st[++k]=-1; + + else + + if (vc[i]==')') + + { + + while (st[k]!=-1) + + { + + v[++cnt]=st[k]; + + k--; + + } + + k--; + + } + + else + + if (vc[i]=='+') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-3; + + } + + else + + if (vc[i]=='-') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-4; + + } + + else + + if (vc[i]=='*') + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-5; + + } + + else + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-6; + + } + + } + + i++; + + } + + while (k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + for (i=1;i<=cnt;i++) + + { + + if (v[i]>=0) + + st[++k]=v[i]; + + else + + { + + if (v[i]==-5) + + { + + if (k<=1) + + st[k]=0; + + else + + { + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if (v[i]==-6) + + { + + k--; + + st[k]/=(st[k+1]); + + } + + else + + if (v[i]==-3) + + { + + k--; + + st[k]+=st[k+1]; + + } + + else + + { + + k--; + + st[k]-=st[k+1]; + + } + + } + + } + + fout< +#include +#include + + +std::ifstream in("evaluare.in"); + +std::ofstream out("evaluare.out"); + + + + + +int Priority(char x) + +{ + + if (x == '+' || x == '-') + + return 1; + + if (x == '*' || x == '/') + + return 2; + + + + return 0; + +} + + + + + + + +int ApplyOperator(int a, int b, char op) + +{ + + switch (op) + + { + + case '+': return a + b; + + case '-': return a - b; + + case '*': return a * b; + + case '/': return a / b; + + } + +} + + + +int Evaluate(std::string& Stream) + +{ + + int i; + + + + + + std::stack Valori; + + + + + + std::stack Operatori; + + + + int n = Stream.length(); + + + + for (i = 0; i < n; i++) + + { + + if (Stream[i] == '(') + + { + + Operatori.push(Stream[i]); + + } + + + + + + else if (isdigit(Stream[i])) + + { + + int numar = 0; + + + + while (i < Stream.length() && isdigit(Stream[i])) + + { + + numar = (numar * 10) + (Stream[i] - '0'); + + i++; + + } + + i--; + + + + Valori.push(numar); + + } + + + + + + else if (Stream[i] == ')') + + { + + + + while(!Operatori.empty() && Operatori.top() != '(') + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + + + Operatori.pop(); + + } + + + + else + + { + while (!Operatori.empty() && Priority(Operatori.top()) >= Priority(Stream[i])) + + { + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + Operatori.push(Stream[i]); + + } + + + + } + + + + while (!Operatori.empty()) + + { + + + + int val2 = Valori.top(); + + Valori.pop(); + + + + int val1 = Valori.top(); + + Valori.pop(); + + + + char op = Operatori.top(); + + Operatori.pop(); + + + + Valori.push(ApplyOperator(val1, val2, op)); + + } + + + + return Valori.top(); + +} + + + +int main() + +{ + + std::string Stream; + + in >> Stream; + + out << Evaluate(Stream); + +} diff --git a/eval/stack/79fda8fd-652d-41ce-bc73-d29a73298b05.cpp b/eval/stack/79fda8fd-652d-41ce-bc73-d29a73298b05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c2695bb04d9906e519792e152c42dd6ae2aab4 --- /dev/null +++ b/eval/stack/79fda8fd-652d-41ce-bc73-d29a73298b05.cpp @@ -0,0 +1,120 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include + + + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +int precedence(char op) + +{ + + if(op == '+'||op == '-') + + return 1; + + if(op == '*'||op == '/') + + return 2; + + return 0; + +} + + + +int opera(int a, int b, char op) + +{ + + if(op == '+') + + return a + b; + + if(op == '-' ) + + return a - b; + + if(op == '*') + + return a * b; + + if(op == '/') + + return a / b; + +} + + + +int eval(string token) + +{ + + int i; + + stack values; + + + + stack ops; + + + + for(i = 0 ; i < token.length() ; i++) + + { + + if(token[i] == '(') + + ops.push(token[i]); + + + + else if(isdigit(token[i])) + + { + + int val = 0; + + + + while(i < token.length() && + + isdigit(token[i])) + + { + + val = (val*10) + (token[i]-'0'); + + i++; + + } + + i--; + + values.push(val); + + } + + + + else if(token[i] == ')') + + { + + while(!ops.empty() && ops.top() != '(') + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + } + + ops.pop(); + + } + + + + + + else + + { + + while(!ops.empty() && precedence(ops.top()) >= precedence(token[i])) + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + } + + ops.push(token[i]); + + } + + } + + + + while(!ops.empty()) + + { + + int val2 = values.top(); + + values.pop(); + + + + int val1 = values.top(); + + values.pop(); + + + + char oper = ops.top(); + + ops.pop(); + + + + values.push(opera(val1, val2, oper) ); + + + + } + + + + return values.top(); + +} + + + +int main() + +{ + + string token; + + getline(cin, token); + + cout << eval(token) ; + + return 0; + +} diff --git a/eval/stack/7b5d8109-868b-443b-867d-7c5bb290ba61.cpp b/eval/stack/7b5d8109-868b-443b-867d-7c5bb290ba61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf03c73b22e7114c40f0abd4807a773794ca4916 --- /dev/null +++ b/eval/stack/7b5d8109-868b-443b-867d-7c5bb290ba61.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + + + +const int NMAX = 100005; + +int n; + +char s[NMAX]; + +int st[NMAX], top; + + + +inline bool isDigit(char ch) + +{ + + return '0' <= ch && ch <= '9'; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> (s + 1); + + n = strlen(s + 1); + + s[0] = '('; + + s[n + 1] = ')'; + + ++n; + + for (int i = 0;i <= n;++i) + + { + + if (s[i] == '+') + + continue; + + else if (s[i] == '-') + + st[++top] = 1000000002; + + else if (s[i] == '*') + + st[++top] = 1000000003; + + else if (s[i] == '/') + + st[++top] = 1000000004; + + else if (s[i] == '(') + + st[++top] = 1000000001; + + else if (s[i] == ')') + + { + + int x = 0; + + while (top > 0 && st[top] != 1000000001) + + x += st[top--]; + + --top; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + else + + { + + int x = 0; + + while (i <= n && isDigit(s[i])) + + x = x * 10 + (s[i++] - '0'); + + --i; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + } + + fout << st[1] << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/7bc65ecc-3abf-4b62-aa97-c154c50edd91.cpp b/eval/stack/7bc65ecc-3abf-4b62-aa97-c154c50edd91.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e10d01c6fb13ed4d6568ea97629642408ec00e1 --- /dev/null +++ b/eval/stack/7bc65ecc-3abf-4b62-aa97-c154c50edd91.cpp @@ -0,0 +1,180 @@ +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +string str, expr; + + + +void opp (int & idx) + +{ + + while (str[idx] >= '0' && str[idx] <= '9') { + + expr += str[idx]; + + idx ++; + + } + + expr += ' '; + + idx --; + +} + + + +int priority (int i) + +{ + + if (str[i] == '*' || str[i] == '/') return 2; + + if (str[i] == '+' || str[i] == '-') return 1; + + return 0; + +} + + + +void formaPoloneza (int n) + +{ + + for (int i = 0; i < n; i ++) { + + if (str[i] >= '0' && str[i] <= '9') opp(i); + + else if (priority(i) != 0) { + + if (s.empty()) s.push(i); + + else { + + while (!s.empty() && priority(s.top()) >= priority(i)) { + + expr += str[s.top()], s.pop(); + + } + + s.push(i); + + } + + } + + else { + + if (str[i] == '(') s.push(i); + + else { + + while (!s.empty() && str[s.top()] != '(') expr += str[s.top()], s.pop(); + + expr += ' '; + + if (!s.empty() && str[s.top()] == '(') s.pop(); + + } + + } + + } + + while (!s.empty()) expr += str[s.top()], s.pop(); + +} + + + +int strToInt (int & idx) + +{ + + int nr = 0; + + while (expr[idx] <= '9' && expr[idx] >= '0') nr = nr * 10 + (expr[idx] - '0'), idx ++; + + idx --; + + return nr; + +} + + + +int expresie (int i, int o1, int o2) + +{ + + if (expr[i] == '+') return o1 + o2; + + else if (expr[i] == '-') return o1 - o2; + + else if (expr[i] == '/') return o1 / o2; + + else return o1 * o2; + +} + + + +int evaluare (int n) + +{ + + int o1, o2; + + while (!s.empty()) s.pop(); + + for (int i = 0; i < n; i ++) { + + if (expr[i] >= '0' && str[i] <= '9') s.push(strToInt(i)); + + else if (expr[i] != ' ' && s.size() >= 2) { + + o1 = s.top(), s.pop(); + + o2 = s.top(), s.pop(); + + s.push(expresie(i, o2, o1)); + + } + + } + + return s.top(); + +} + + + +int main() + +{ + + getline(fin, str); + + int n = str.size(); + + formaPoloneza(n); + + fout << evaluare(expr.size()); + + return 0; + +} diff --git a/eval/stack/7c47588d-2aa1-4dab-acd3-c1167266e928.cpp b/eval/stack/7c47588d-2aa1-4dab-acd3-c1167266e928.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c448ee2b92d6abc5460290f5745452cd1177474c --- /dev/null +++ b/eval/stack/7c47588d-2aa1-4dab-acd3-c1167266e928.cpp @@ -0,0 +1,234 @@ +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +char semne[100005]; + +int nre[100005]; + +int top_semne, top_nre; + +int l; + + + +bool isDigit(char a) + +{ + + if(a >= '0' && a <= '9') + + return 1; + + return 0; + +} + + + +void push_semne(char semn) + +{ + + semne[++top_semne] = semn; + +} + + + +void push_nre(int nr) + +{ + + nre[++top_nre] = nr; + +} + + + +void formnr(int &i) + +{ + + int nr = 0; + + while(isDigit(a[i])) + + { + + nr = nr * 10 + (a[i] - '0'); + + ++i; + + } + + --i; + + push_nre(nr); + +} + + + +void calcul(char c) + +{ + + int x = nre[top_nre]; + + nre[top_nre --] = 0; + + int y = nre[top_nre]; + + nre[top_nre --] = 0; + + int rez; + + + + switch(c) + + { + + case '+': + + rez = y + x; + + break; + + case '-': + + rez = y - x; + + break; + + case '*': + + rez = y * x; + + break; + + case '/': + + rez = y / x; + + break; + + } + + push_nre(rez); + + semne[top_semne--] = 0; + + + +} +int grad(char c) + +{ + + if(c == '+' || c == '-' || c == ')') + + return 1; + + if(c == '*' || c == '/') + + return 2; + + return 0; + +} +void eliber(char forWho) + +{ + + int grad_actual = grad(forWho); + + + + while(top_semne != 0 && grad_actual <= grad(semne[top_semne])) + + calcul(semne[top_semne]); + +} +void solve() + +{ + + for(int i = 0; i < l; ++i) + + { + + if(isDigit(a[i])) + + formnr(i); + + else if(a[i] == '(') + + push_semne(a[i]); + + else if(a[i] == ')') + + { + + if(top_semne != 0) + + eliber(')'); + + semne[top_semne --] = 0; + + } + + else{ + + if(top_semne != 0) + + eliber(a[i]); + + push_semne(a[i]); + + } + + + + } + + while(top_semne != 0) + + calcul(semne[top_semne]); + +} + + + + + +int main() + +{ + + f>>a; + + l = strlen(a); + + solve(); + + g< +#include +#include +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +bool isOperator(char op){ + return op=='+' || op=='-' || op=='*' || op=='/'; +} +bool isDigit(char ch){ + return ch>='0' && ch<='9'; +} + +int precedence(char op){ + switch(op){ + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + } + return 0; +} + +int applyOperator(char op,int a,int b){ + switch(op){ + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + } + return 0; +} + +int peek_integer(const string&expression,int&cursor){ + int result=0; + while(cursor ops; + + + while(cursor= prec){ + + result+=ops.top(); + ops.pop(); + } + + ops.push(here); + } + cursor++; + } + + + while(ops.size()){ + result+=ops.top(); + ops.pop(); + } + return result; +} + +int evaluate_postfix(const std::string&postfix){ + stack values; + int cursor=0; + + while(cursor>eval; + + out< +#include +using namespace std; + +ifstream cin ("evaluare.in"); + +ofstream cout ("evaluare.out"); + +char vc[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + int n,i,nr,cnt=0,k=0,ok=0; + + cin>>vc+1; + + n=strlen(vc+1); + + i=1; + + while (i<=n) + + { + + if (vc[i]>='0'&&vc[i]<='9') + + { + + nr=0; + + while (vc[i]>='0'&&vc[i]<='9') + + { + + nr=nr*10+vc[i]-'0'; + + i++; + + } + + v[++cnt]=nr; + + i--; + + } + + else + + { + + if (vc[i]=='(') + + st[++k]=-1; + + else + + if (vc[i]==')') + + { + + while (st[k]!=-1) + + { + + v[++cnt]=st[k]; + + k--; + + } + + k--; + + } + + else + + if (vc[i]=='+') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-3; + + } + + else + + if (vc[i]=='-') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-4; + + } + + else + + if (vc[i]=='*') + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-5; + + } + + else + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-6; + + } + + } + + i++; + + } + + while (k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + + + for (i=1;i<=cnt;i++) + + { + + if (v[i]>=0) + + st[++k]=v[i]; + + else + + { + + if (v[i]==-5) + + { + + + + if (k<=1) + + st[k]=0; + + else + + { + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if (v[i]==-6) + + { + + + + k--; + + st[k]/=(st[k+1]); + + } + + else + + if (v[i]==-3) + + { + + k--; + + st[k]+=st[k+1]; + + } + + else + + { + + k--; + + st[k]-=st[k+1]; + + } + + } + + } + + cout< +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< +#include +#include +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +bool isOperator(char op){ + return op=='+' || op=='-' || op=='*' || op=='/'; +} +bool isDigit(char ch){ + return ch>='0' && ch<='9'; +} + +int precedence(char op){ + switch(op){ + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + } + return 0; +} + +int applyOperator(char op,int a,int b){ + switch(op){ + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + } + return 0; +} + +int peek_integer(const string&expression,int&cursor){ + int result=0; + while(cursor ops; + + + while(cursor= prec){ + + result+=ops.top(); + ops.pop(); + } + + ops.push(here); + } + cursor++; + } + + + while(ops.size()){ + result+=ops.top(); + ops.pop(); + } + return result; +} + +int evaluate_postfix(const std::string&postfix){ + stack values; + int cursor=0; + + while(cursor>eval; + + out< +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/883c32db-bb9b-46e2-b62a-ea6da36b84d2.cpp b/eval/stack/883c32db-bb9b-46e2-b62a-ea6da36b84d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99274564a2c1624ec9afe9997bdea1d0e478fd38 --- /dev/null +++ b/eval/stack/883c32db-bb9b-46e2-b62a-ea6da36b84d2.cpp @@ -0,0 +1,182 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int i,v1,v2,rez; + +string str; + +stack val; + +stack s; + +int calc(int v1,int v2,char op) + +{ + + if(op=='+') return v2+v1; + + else if(op=='-') return v1-v2; + + else if(op=='*') return v1*v2; + + else return v1/v2; + +} + +int preced(char op) + +{ + + if(op=='-'||op=='+') return 1; + + if(op=='*'||op=='/') return 2; + + return 0; + +} + +int ev() + +{ + + + + char op; + +getline(fin,str); + + for(i=0; i=preced(str[i])) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + } + + s.push(str[i]); + + } + + } + + while(!s.empty()) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + + + } + + return val.top(); + + + +} + +int main() + +{fout< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string mes; + +string post; + +int i; + +stack nr; + +stack op; + + + +long long calc(long long nr1,long long nr2,char operatie) + +{ + + + + switch(operatie){ + + case '+': + + return nr1+nr2; + + case '-': + + return nr1-nr2; + + case '*': + + return nr1*nr2; + + case '/': + + return nr1/nr2; + + case '^': + + return pow(nr1,nr2); + + } + + return 0; + +} + + + +int prec(char ch) + +{ + + switch(ch) + + { + + case '+': + + case '-': + + return 1; + + case '*': + + case '/': + + return 2; + + case '^': + + return 3; + + case '(': + + return 0; + + } + + return 0; + +} + + + +int main() + +{ + + fin>>mes; + + + + while(i= prec(mes[i])) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + op.push(mes[i]); + + } + + } + + + + i++; + + } + + + + while(!op.empty() ) + + { + + post+= op.top(); + + post+= " "; + + op.pop(); + + } + + + + + + i = 0; + + long long sol = 0; + + while(i < post.size()) + + { + + if(isdigit(post[i])) + + { + + long long numar = 0; + + while(isdigit(post[i])) + + { + + numar = numar * 10 + (post[i]-'0'); + + i++; + + } + + nr.push(numar); + + + + } + + else if(post[i] == '+' || post[i] == '*' || post[i] == '-' || post[i] == '^' || post[i] == '/') + + { + + + + long long nr1 = nr.top(); + + nr.pop(); + + long long nr2 = nr.top(); + + nr.pop(); + + long long res = calc(nr2,nr1,post[i]); + + nr.push(res); + + + + } + + + + i++; + + } + + + + fout< + + + + + +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +void citire(); +int vf = 0; +int prio(char x) +{ + if( x=='(' || x == ')') + return 0; + if( x=='+' || x=='-') + return 1; + + return 2; +} +int operatie ( int a, int b, char c ) +{ + + switch (c) + { + case '+': return a+b; + case '-': return a-b; + case '*': return a*b; + case '/': return a/b; + + } +} +int eval(char []); +char x[300001]; +int s[100001]; +void postfix(char []); +void rez(); +void afis(); +int main() +{ + fin >> x; + postfix(x); + + + fout<< eval(x); +} +void postfix(char x[]) +{ + int poz = 0; + int N =strlen (x); + char aux[300001] = {0}; + for ( int i = 0 ; i < N ; i++) + { + if(isdigit(x[i])) + { + aux[poz++] = x[i] ; + } + else + { + if(i>0 && isdigit(x[i-1])) + aux[poz++]=' '; + if(x[i]=='(') + s[vf++]=x[i]; + else + if(x[i]==')') + { + while(s[vf-1]!='(') + { + aux[poz++]=s[--vf]; + } + vf--; + } + else + { + while(vf != 0 && prio(x[i]) <= prio(s[vf-1])) + { + aux[poz++]=s[--vf]; + } + s[vf++] = x[i]; + } + } + + + } + if(isdigit(aux[poz-1])) aux[poz++]=' '; + while(vf!=0) + { + aux[poz++] = s[--vf]; + } + + strcpy(x,aux); + +} +int eval( char x[]) +{ + int N =strlen(x); + int numar =0; + for( int i=0 ;i < N ;i ++) + { + if(isdigit(x[i])) numar = numar*10 +(x[i]-'0'); + else + if (x[i]== ' ') { + s[vf++]=numar; + numar=0; + } + else + { + int nr2=s[--vf], nr1= s[--vf]; + s[vf++] = operatie ( nr1 ,nr2 , x[i]); + } + } + return s[vf-1]; +} diff --git a/eval/stack/8b345308-5d80-4660-91e7-8192e5e83701.cpp b/eval/stack/8b345308-5d80-4660-91e7-8192e5e83701.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89df9616ec163ac82d2e197fef4647b9a9121a9e --- /dev/null +++ b/eval/stack/8b345308-5d80-4660-91e7-8192e5e83701.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string tokens; + +int n; + +stack ops; + +stack nr; + +int prioritate(char c) + +{ + + if (c == '-'||c == '+') + + return 1; + + if (c == '*'||c == '/') + + return 2; + + return 0; + +} + +int culcal(int a,int b,char c) + +{ + + switch(c){ + + case '+': return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/': return a/b; + + } + +} + +int evalexp() + +{ + + int i = 0; + + int val1,val2,val; + + n = tokens.size(); + + while (i != n) + + { + + if (tokens[i] == ' ') + + continue; + + if (tokens[i] == '(') ops.push(tokens[i]); + + else if (isdigit(tokens[i])) + + { + + val = 0; + + while(tokens[i] != '\n' && isdigit(tokens[i])) + + { + + val = val * 10 + (tokens[i] - '0'); + + i ++; + + } + + i --; + + nr.push(val); + + } + + else if (tokens[i] == ')') + + { + + while(!ops.empty() && ops.top()!='(') + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + } + + if (!ops.empty()) + + ops.pop(); + + } + + else{ + + char op,op1; + + op = tokens[i]; + + while (!ops.empty() && prioritate(op) <= prioritate(ops.top())) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + op1 = ops.top(); + + ops.pop(); + + nr.push(culcal(val1,val2,op1)); + + + + } + + ops.push(op); + + } + + i++; + + } + + while(!ops.empty()) + + { + + val2 = nr.top(); + + nr.pop(); + + val1 = nr.top(); + + nr.pop(); + + nr.push(culcal(val1,val2,ops.top())); + + + + ops.pop(); + + if (!ops.empty()) + + if (ops.top() == '(') ops.pop(); + + } + + return nr.top(); + +} + +int main() + +{ + + getline(fin,tokens); + + fout << evalexp() << '\n'; + + return 0; + +} diff --git a/eval/stack/8bc6ab57-6b17-4a58-9d6d-7c86532c06d4.cpp b/eval/stack/8bc6ab57-6b17-4a58-9d6d-7c86532c06d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eae227f1fd351ea43328327c54328aa485767dea --- /dev/null +++ b/eval/stack/8bc6ab57-6b17-4a58-9d6d-7c86532c06d4.cpp @@ -0,0 +1,194 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack nums; + +stack ops; + +int pri[260]; + + + +void setpri(); + +int getNum(char*&s); + +void eval(char* s); + +void exec(char &op); + + + +int main(){ + + setpri(); + + char s[100005]; + + fin>>s; + + eval(s); + + fout<=pri[op]){ + + exec(ops.top()); + + ops.pop(); + + } + + ops.push(op); + + s++; + + } + + } + + while(!ops.empty()){ + + exec(ops.top()); + + ops.pop(); + + } + +} + + + +void exec(char &op){ + + int rez = 0; + + int x1 = nums.top(); + + nums.pop(); + + int x2 = nums.top(); + + nums.pop(); + + switch (op){ + + case '+': + + rez = x1+x2; + + break; + + case '-': + + rez = x2-x1; + + break; + + case '*': + + rez = x1*x2; + + break; + + case '/': + + rez = x2/x1; + + break; + + case '^': + + rez = pow(x2,x1); + + break; + + default: + + break; + + } + + nums.push(rez); + +} diff --git a/eval/stack/8c94e893-5646-474f-a4a9-c79e3cda3984.cpp b/eval/stack/8c94e893-5646-474f-a4a9-c79e3cda3984.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99f4d669dc57a436ae3aaf2fda234fffb840bd99 --- /dev/null +++ b/eval/stack/8c94e893-5646-474f-a4a9-c79e3cda3984.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char e[100005]; +int n; + +int priority(char o){ + if(o == '+' || o == '-') + return 1; + if(o == '*' || o == '/') + return 2; + return 0; +} + +int compute(int a, int b, int o){ + if(o == '+') + return a + b; + if(o == '-') + return a - b; + if(o == '*') + return a * b; + if(o == '/') + return a / b; +} + +int eval(){ + int i = 0; + stack op; + stack val; + while(i < n){ + if(e[i] == '('){ + op.push(e[i]); + } + else if(e[i] == ')'){ + while(!op.empty() && op.top() != '('){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.pop(); + } + else if(isdigit(e[i])){ + int v = 0; + while(i < n && isdigit(e[i])){ + v = (v * 10) + (e[i] - '0'); + ++i; + } + --i; + val.push(v); + } + else{ + while(!op.empty() && priority(op.top()) >= priority(e[i])){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.push(e[i]); + } + ++i; + } + while(!op.empty()){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + return val.top(); +} + +int main(){ + + fin.getline(e, 100005); + n = strlen(e); + fout << eval(); + fin.close(); + fout.close(); + return 0; +} diff --git a/eval/stack/8e4d2b22-0f05-44ae-a6ba-08adc0c85322.cpp b/eval/stack/8e4d2b22-0f05-44ae-a6ba-08adc0c85322.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff36a17d166db552e1a427a5588a911eca8592a9 --- /dev/null +++ b/eval/stack/8e4d2b22-0f05-44ae-a6ba-08adc0c85322.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + + + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +vector < int > val(256); + +void getPol (string &pol) { + + string s; + getline(cin, s); + + stack < char > stk; + + int ind = 0; + while (ind < (int)(s).size()) { + + bool digit = false; + while (ind < (int)(s).size() and isdigit(s[ind])) { + pol += s[ind ++]; + digit = true; + } + + if (digit) pol += ' '; + + if (ind == (int)(s).size()) break; + + if (s[ind] == '(') { + stk.push(s[ind ++]); + continue; + } + + if (s[ind] == ')') { + while (not stk.empty() and stk.top() != '(') { + pol += stk.top(); + stk.pop(); + } + stk.pop(); + ++ ind; + continue; + } + + while (not stk.empty() and val[stk.top()] >= val[s[ind]]) { + if (stk.top() == '(') break; + pol += stk.top(); + stk.pop(); + } + stk.push(s[ind ++]); + } + + while (not stk.empty()) { + pol += stk.top(); + stk.pop(); + } +} + +int main() { + + + + + + + ios::sync_with_stdio(false); + cin.tie(0);cout.tie(0); + + val['('] = 20; + val[')'] = 20; + val['*'] = 6; + val['/'] = 6; + val['+'] = 3; + val['-'] = 3; + + string pol; + getPol(pol); + + int ind = 0; + stack < int > stk; + + while (ind < (int)(pol).size()) { + + int nr = 0; + while (ind < (int)(pol).size() and isdigit(pol[ind])) { + nr = (nr << 1) + (nr << 3) + pol[ind ++] - '0'; + } + + stk.push(nr); + + while (ind < (int)(pol).size() and not isdigit(pol[ind])) { + + if (pol[ind] == ' ') { + ++ ind; + continue; + } + + int b = stk.top(); + stk.pop(); + int a = stk.top(); + stk.pop(); + + if (pol[ind] == '+') { + stk.push(a + b); + } + else if (pol[ind] == '-') { + stk.push(a - b); + } + else if (pol[ind] == '/') { + stk.push(a / b); + } + else if (pol[ind] == '*') { + stk.push(a * b); + } + + ++ ind; + } + } + + cout << stk.top() << '\n'; +} diff --git a/eval/stack/8ec6e3f1-da0e-4399-b8aa-717386a8e6e8.cpp b/eval/stack/8ec6e3f1-da0e-4399-b8aa-717386a8e6e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5a30851967d3f478f833d18f2947a0f50492f04 --- /dev/null +++ b/eval/stack/8ec6e3f1-da0e-4399-b8aa-717386a8e6e8.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); +string s; + + + + + + + +bool isOp(char c) { + + return c == '+' || c == '-' || c == '/' || c == '*'; + +} + + + +int priority(char op) { + + if (op == '/' || op == '*') return 2; + + if (op == '-' || op == '+') return 1; + + return -1; + +} + + + +void process_op(stack & st, char op) { + + int r = st.top(); st.pop(); + + int l = st.top(); st.pop(); + + + + switch (op) { + + case '+': st.push(l + r); break; + + case '-': st.push(l - r); break; + + case '/': st.push(l / r); break; + + case '*': st.push(l * r); break; + + } + +} + + + +int evaluate(string& s) { + + stack st; + + stack op; + + + + for (int i = 0; i < (int)s.size(); i++) { + + + + if (s[i] == '(') { + + op.push('('); + + } + + else if (s[i] == ')') { + + while (op.top() != '(') { + + process_op(st, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + else if (isOp(s[i])) { + + char curr_op = s[i]; + + while (!op.empty() && priority(op.top()) >= priority(curr_op)) { + + process_op(st, op.top()); + + op.pop(); + + } + + op.push(curr_op); + + } + + else { + + + + int num = 0; + + while (i < (int)s.size() && isalnum(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + st.push(num); + + } + + } + + + + while (!op.empty()) { + + process_op(st, op.top()); + + op.pop(); + + } + + return st.top(); + +} + + + + + + + +int main() { + + + + cin >> s; + + cout << evaluate(s); + + + + return 0; + +} diff --git a/eval/stack/8edac17b-b54f-4b0d-af5d-155ccde203a8.cpp b/eval/stack/8edac17b-b54f-4b0d-af5d-155ccde203a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d8b30a066d2379604836bd54aaa506e5cde7f92 --- /dev/null +++ b/eval/stack/8edac17b-b54f-4b0d-af5d-155ccde203a8.cpp @@ -0,0 +1,100 @@ +#include + +typedef unsigned long long ull; +typedef long long ll; +using namespace std; +ll ceildiv(ll one, ll two) { + if (one % two == 0) {return one / two;} + else {return one / two + 1;} +} ll power(ll n, ll pow, ll m) { + if (pow == 0) return 1; + if (pow % 2 == 0) { + ll x = power(n, pow / 2, m); + return (x * x) % m; + } + else return (power(n, pow - 1, m) * n) % m; +} ll gcd(ll a, ll b) { + if (!b)return a; + return gcd(b, a % b); +} ll factorial(ll n, ll mod) { + if (n > 1) + return (n * factorial(n - 1, mod)) % mod; + else + return 1; +} ll lcm(ll a, ll b) { + return (a * b) / gcd(a, b); +} vector read(ll n) {vector a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x);} return a;}vector> adj;void init(ll n) {for (ll i = 0; i <= n; i++) { vector a; adj.push_back(a);}} + +bool delim(char c) { + return c == ' '; +} + +bool is_op(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +ll priority (char op) { + if (op == '+' || op == '-') + return 1; + if (op == '*' || op == '/') + return 2; + return -1; +} + +void process_op(stack& st, char op) { + ll r = st.top(); st.pop(); + ll l = st.top(); st.pop(); + switch (op) { + case '+': st.push(l + r); break; + case '-': st.push(l - r); break; + case '*': st.push(l * r); break; + case '/': st.push(l / r); break; + } +} + +ll evaluate(string& s) { + stack st; + stack op; + for (ll i = 0; i < (ll)s.size(); i++) { + if (delim(s[i])) + continue; + + if (s[i] == '(') { + op.push('('); + } else if (s[i] == ')') { + while (op.top() != '(') { + process_op(st, op.top()); + op.pop(); + } + op.pop(); + } else if (is_op(s[i])) { + char cur_op = s[i]; + while (!op.empty() && priority(op.top()) >= priority(cur_op)) { + process_op(st, op.top()); + op.pop(); + } + op.push(cur_op); + } else { + ll number = 0; + while (i < (ll)s.size() && isalnum(s[i])) + number = number * 10 + s[i++] - '0'; + --i; + st.push(number); + } + } + + while (!op.empty()) { + process_op(st, op.top()); + op.pop(); + } + return st.top(); +} + +int32_t main() { + ios_base::sync_with_stdio(false);cin.tie(0);; + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + string s; + cin >> s; + cout << evaluate(s) << '\n'; +} diff --git a/eval/stack/9166196c-a3be-4be7-965c-dd10cda9031d.cpp b/eval/stack/9166196c-a3be-4be7-965c-dd10cda9031d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3e39ce44094afbf9ba392cb5b4b03457ff719ba --- /dev/null +++ b/eval/stack/9166196c-a3be-4be7-965c-dd10cda9031d.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +using namespace std; + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + +string s; + +int num[100007],st[100007]; + +int pd=-1000000001,pi=-1000000002,plu=-1000000003,minu=-1000000004,inm=-1000000005,imp=-1000000006; + +int main() + +{ + + in>>s; + + int n=s.length(); + + int i=0,f=0,k=0; + + int nr=0; + + while(i=0) + + st[++k]=num[i]; + + else + + { + + if(num[i]==inm) + + { + + if(k<2) + + st[k]=0; + + else + + { + + st[--k]*=st[k+1]; + + } + + } + + else if(num[i]==imp) + + { + + st[--k]/=st[k+1]; + + } + + else if(num[i]==plu) + + { + + st[--k]+=st[k+1]; + + } + + else if(num[i]==minu) + + { + + st[--k]-=st[k+1]; + + } + + } + + } + + out< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + + +string to_eval; + +bool is_operator(char x) +{ + + return x=='+'||x=='-'||x=='*'||x=='/'; + +} + + + +bool is_digit(char a) +{ + + return a>='0' &&a<='9'; +} + +int apply(int a,int b,char op) +{ + + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + default: + return -1; + } + + +} + + +int get_int_at(string&r,int&index) +{ + int result=0; + + while(is_digit(r[index])) + { + result=result*10+(r[index]-'0'); + index++; + } + index--; + return result; +} + + + +int operator_order(char x) +{ + switch(x) + { + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + default: + return -1; + } + +} + +string get_polish_form(string expression) +{ + stack st; + string to_return; + + for(int i=0; i=operator_order(expression[i])) + { + to_return+=st.top(); + st.pop(); + } + st.push(expression[i]); + } + else if(expression[i]==')') + { + while(st.size() && st.top()!='(') + { + to_return+=st.top(); + st.pop(); + } + if(st.top()=='(') + st.pop(); + } + + } + while(st.size()) + { + to_return+=st.top(); + st.pop(); + } + + + return to_return; +} + +int evaluate_polish_form(string polish) +{ + stackprocessed; + for(int i=0; i>to_eval; + string polish=get_polish_form(to_eval); + out< +#include +#include +#include + + +using namespace std; + + + +char exp[100002]; + + + +vector postfix_st; + +stack op_st; + + + + + +int get_priority(char op) { + + if (op == '*' || op == '/') + + return 1; + + if (op == '+' || op == '-') + + return 2; + +} + + + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s\n", exp); + + int n = strlen(exp); + + if (exp[n-1] == '\n') { + + exp[n-1] = 0; + + --n; + + } + + + + + + int current_number = 0, res, a, b; + + for(int i=0; i= '0' && exp[i] <='9') { + + current_number = 0; + + while(exp[i] >= '0' && exp[i] <='9' && i = get_priority(op_st.top())) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + op_st.push(exp[i]); + + + + } + + + + while(op_st.size()) { + + postfix_st.push_back(op_st.top()); + + op_st.pop(); + + } + + + + for(int i=0; i= '0') + + op_st.push(postfix_st[i] - '0'); + + else { + + a = op_st.top(); + + op_st.pop(); + + b = op_st.top(); + + op_st.pop(); + + + + switch(postfix_st[i]) { + + case '+': + + res = a + b; + + break; + + case '-': + + res = b-a; + + break; + + case '/': + + res = b/a; + + break; + + case '*': + + res = a*b; + + break; + + } + + + + op_st.push(res); + + } + + } + + + + printf("%d\n", op_st.top()); + + return 0; + +} diff --git a/eval/stack/93a699a8-1727-46bd-b394-271ab65c891b.cpp b/eval/stack/93a699a8-1727-46bd-b394-271ab65c891b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..609fc0220e98ddca2c214ed0e1d8b60019eafed8 --- /dev/null +++ b/eval/stack/93a699a8-1727-46bd-b394-271ab65c891b.cpp @@ -0,0 +1,198 @@ +#include + + +using namespace std; + + + +ifstream si("evaluare.in"); + +ofstream so("evaluare.out"); + + + +long long op[100001],q[100001],n,i,nr,nr2,x; + +char s[100001]; + + + +void calc(int a,int b) + +{ + + if(op[b]=='+') + + { + + q[a-1]+=q[a]; + + } + + if(op[b]=='-') + + { + + q[a-1]-=q[a]; + + } + + if(op[b]=='*') + + { + + q[a-1]*=q[a]; + + } + + if(op[b]=='/') + + { + + q[a-1]/=q[a]; + + } + + return; + + + +} + + + +int gr(char c) + +{ + + if(c=='+' or c=='-') + + { + + return 1; + + } + + if(c=='*' or c=='/') + + { + + return 2; + + } + + return 0; + +} + + + +int main() + +{ + + si.getline(s,100001); + + n=strlen(s); + + for(i=0; i +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char vp[42949678],s[42949678],exp[42949678]; + +stack st; + +int n,kexp; + + + + + +int prioritate(int c) + +{ + + if (s[c]=='(' || s[c]==')') + + return 0; + + if (s[c]=='*' || s[c]=='/') + + return 2; + + return 1; + +} + + + +void formare_nr(int &k) + +{ + + while (s[k]>='0'&& s[k]<='9') + + { + + exp[++kexp]=s[k]; + + + + k++; + + } + + k--; + + exp[++kexp]=' '; + +} + + + + + +void forma_poloneza() + +{ + + for (int i=0;i<=n;i++) + + { + + if (s[i]>='0' && s[i]<='9') + + formare_nr(i); + + else if (prioritate(i)!=0) + + { + + if (st.empty()) + + st.push(i); + + else + + { + + int a=prioritate(i); + + while (!st.empty() && prioritate(st.top())>=a) + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + st.push(i); + + } + + } + + else + + { + + if (s[i]=='(') + + st.push(i); + + else + + { + + while (!st.empty()&& s[st.top()]!='(') + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + exp[++kexp]=' '; + + if (!st.empty()&& s[st.top()]=='(') + + st.pop(); + + } + + } + + + + } + + while (!st.empty()) exp[++kexp]= s[st.top()], st.pop(); + + + +} + + + +int transf_int (int &k) + +{ int nr=0; + + while (exp[k]>='0' && exp[k]<='9') + + { + + nr =nr*10 + (exp[k]-'0'); + + k++; + + } + + + + return nr; + + + + + +} + + + +int calcul(int i, int a,int b) + +{ + + + + if (exp[i]=='-') + + return a-b; + + if (exp[i]=='+') + + return a+b; + + if (exp[i]=='*') + + return a*b; + + return a/b; + + + +} + + + +int evaluare(int k) + +{ + + while (!st.empty()) + + st.pop(); + + for (int i=1;i<=k;i++) + + {if (exp[i]>='0' && exp[i]<='9') + + st.push(transf_int(i)); + + if (st.size()>=2 && exp[i]!=' ') + + { + + int a,b; + + a=st.top(); + + st.pop(); + + b=st.top(); + + st.pop(); + + + + st.push(calcul(i,b,a)); + + } + + } + + return st.top(); + + + +} + + + +int main() + +{ + + f>>s; + + n=strlen(s)-1; + + forma_poloneza(); + + + +g< + + + + +using namespace std; +typedef long long ll; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +int n, m, i, j, dg; +char s[200005]; +stack val; +stack op; +int fa(int x, char semn, int y) +{ + if(semn=='+') + return x+y; + else if(semn=='-') + return y-x; + else if(semn=='*') + return x*y; + else return y/x; +} +int pred(char semn) +{ + if(semn=='*'||semn=='/') + return 2; + else if(semn=='+'||semn=='-')return 1; + return 0; +} +int main() +{ + fin>>s; + n=strlen(s); + for(i=0;i='0'&&s[i]<='9') + { + if(dg==-1) + dg=0; + dg=dg*10+s[i]-'0'; + continue; + } + if(dg>-1) + { + val.push(dg); + dg=-1; + } + if(s[i]=='(') + op.push('('); + else if(s[i]==')') + { + while(op.top()!='(') + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.pop(); + } + else + { + while(!op.empty()&&pred(op.top())>=pred(s[i])) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + val.push(fa(x,op.top(),y)); + op.pop(); + } + op.push(s[i]); + } + } + if(dg>-1) + val.push(dg); + while(!op.empty()) + { + int x=val.top(); + val.pop(); + int y=val.top(); + val.pop(); + cout< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/9b480b14-56dd-49fa-bc8e-75551c90adda.cpp b/eval/stack/9b480b14-56dd-49fa-bc8e-75551c90adda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dfea93c24cae726e840cd151b32c8dedbfc16249 --- /dev/null +++ b/eval/stack/9b480b14-56dd-49fa-bc8e-75551c90adda.cpp @@ -0,0 +1,230 @@ +#include +char ch[100005]; + +int v[100005],st[100005]; + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("evaluare.in","r"); + + fout=fopen("evaluare.out","w"); + + int n,i,q=0,nr,k=0; + + n=1; + + while(fscanf(fin,"%c",&ch[n])!=EOF) + + n++; + + i=1; + + while(i<=n){ + + if(ch[i]>='0' && ch[i]<='9'){ + + nr=0; + + while(ch[i]>='0' && ch[i]<='9'){ + + nr=nr*10+ch[i]-'0'; + + i++; + + } + + q++; + + v[q]=nr; + + i--; + + } + + else{ + + if(ch[i]=='('){ + + k++; + + st[k]=-1; + + } + + if(ch[i]==')'){ + + while(st[k]!=-1){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k--; + + } + + if(ch[i]=='*'){ + + while (st[k]==-5 || st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-5; + + } + + if(ch[i]=='/'){ + + while (st[k]==-5||st[k]==-6){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-6; + + } + + if(ch[i]=='+'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-3; + + } + + if(ch[i]=='-'){ + + while(st[k]!=-1 && k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + k++; + + st[k]=-4; + + } + + } + + i++; + + } + + while(k!=0){ + + q++; + + v[q]=st[k]; + + k--; + + } + + for(i=1;i<=q;i++){ + + if(v[i]>=0){ + + k++; + + st[k]=v[i]; + + } + + else{ + + if(v[i]==-5){ + + if(k<=1) + + st[k]=0; + + else{ + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if(v[i]==-6){ + + k--; + + st[k]=st[k]/st[k+1]; + + } + + else + + if(v[i]==-3){ + + k--; + + st[k]=st[k]+st[k+1]; + + } + + else{ + + k--; + + st[k]=st[k]-st[k+1]; + + } + + } + + } + + fprintf(fout,"%d ",st[1]); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/eval/stack/9b530353-a0f8-47b3-b804-c1d2d28ffcf8.cpp b/eval/stack/9b530353-a0f8-47b3-b804-c1d2d28ffcf8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc7457df6dcc58a4bece055aa4e6a4c0b20fef44 --- /dev/null +++ b/eval/stack/9b530353-a0f8-47b3-b804-c1d2d28ffcf8.cpp @@ -0,0 +1,143 @@ +#include +#include +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + + + +int precedence(char op){ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + return 0; +} + + +int applyOp(int a, int b, char op){ + switch(op){ + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': return a / b; + } +} + + + +int evaluate(string tokens){ + int i; + + + stack values; + + + stack ops; + + for(i = 0; i < tokens.length(); i++){ + + + + if(tokens[i] == ' ') + continue; + + + + else if(tokens[i] == '('){ + ops.push(tokens[i]); + } + + + + else if(isdigit(tokens[i])){ + int val = 0; + + + + while(i < tokens.length() && + isdigit(tokens[i])) + { + val = (val*10) + (tokens[i]-'0'); + i++; + } + + values.push(val); + i--; + } + + + + else if(tokens[i] == ')') + { + while(!ops.empty() && ops.top() != '(') + { + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + if(!ops.empty()) + ops.pop(); + } + + + else + { + + + + + while(!ops.empty() && precedence(ops.top()) + >= precedence(tokens[i])){ + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + ops.push(tokens[i]); + } + } + + + + + while(!ops.empty()){ + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + return values.top(); +} + +int main() { + string s; + fin >> s; + fout << evaluate(s); + return 0; +} diff --git a/eval/stack/9c4480a7-54d0-4f6b-97f0-5ac17c8d02a9.cpp b/eval/stack/9c4480a7-54d0-4f6b-97f0-5ac17c8d02a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..600f7bf8af998bc120bd2a8707fc08a0c3f05140 --- /dev/null +++ b/eval/stack/9c4480a7-54d0-4f6b-97f0-5ac17c8d02a9.cpp @@ -0,0 +1,109 @@ +#include +using namespace std; + +vector get_precedence() +{ + vector ans(256, 0); + ans['-'] = ans['+'] = 1; + ans['*'] = ans['/'] = 2; + + return ans; +} + +vector transform_to_postfix(string S) +{ + vector precedence = get_precedence(); + vector postfix; + vector operators; + for (int i = 0; i < S.size(); i++) + { + if (S[i] >= '0' && S[i] <= '9') + { + int j = i; + while (i + 1 < S.size() && S[i + 1] >= '0' && S[i + 1] <= '9') + i++; + postfix.push_back(S.substr(j, i - j + 1)); + continue; + } + if (S[i] == ')') + { + while (!operators.empty() && operators.back() != '(') + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + operators.pop_back(); + continue; + } + + if (S[i] == '(' || operators.empty() || precedence[S[i]] > precedence[operators.back()]) + operators.push_back(S[i]); + else + { + while (!operators.empty() && operators.back() != '(' && precedence[operators.back()] >= precedence[S[i]]) + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + operators.push_back(S[i]); + } + } + + + while (!operators.empty()) + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + + return postfix; +} + +int convert_string_to_number(string S) +{ + int ans = 0; + for (int i = 0; i < S.size(); i++) + ans = ans * 10 + S[i] - '0'; + + return ans; +} + +int evaluate(vector postfix) +{ + vector operands; + for (auto elem : postfix) + if (elem[0] >= '0' && elem[0] <= '9') + operands.push_back(convert_string_to_number(elem)); + else + { + int y = operands.back(); + operands.pop_back(); + int x = operands.back(); + operands.pop_back(); + int res; + if (elem[0] == '+') + res = x + y; + if (elem[0] == '-') + res = x - y; + if (elem[0] == '*') + res = x * y; + if (elem[0] == '/') + res = x / y; + operands.push_back(res); + } + + return operands.back(); +} + +int main() +{ + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + cin.sync_with_stdio(false); + + string S; + cin >> S; + vector postfix = transform_to_postfix(S); + cout << evaluate(postfix) << "\n"; + return 0; +} diff --git a/eval/stack/9d53f71d-529f-4a4c-b9c3-84543ae60754.cpp b/eval/stack/9d53f71d-529f-4a4c-b9c3-84543ae60754.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64b723c5c4b60e29341440bc5a8e6500486b6b10 --- /dev/null +++ b/eval/stack/9d53f71d-529f-4a4c-b9c3-84543ae60754.cpp @@ -0,0 +1,174 @@ +#include +typedef unsigned long long ul; + +typedef long long ll; + +using namespace std; + + + +bool is_op(char c){ + + return (c == '+' or c == '-' or c == '*' or c == '/'); + +} + + + +int priority(char c){ + + if (c == '+' or c == '-') + + return 1; + + if (c == '*' or c == '/') + + return 2; + + return -1; + +} + + + +void process(stack &stk, char op){ + + int r = stk.top(); + + stk.pop(); + + int l = stk.top(); + + stk.pop(); + + + + if (op == '+') + + stk.push(l + r); + + else if (op == '-') + + stk.push(l - r); + + else if (op == '*') + + stk.push(l * r); + + else if (op == '/') + + stk.push(l / r); + +} + + + +int evaluare(string s){ + + stack val; + + stack op; + + + + for (int i = 0; i < s.length(); i++){ + + if (s[i] == '(') + + op.push('('); + + + + else if (s[i] == ')'){ + + while (op.top() != '('){ + + process(val, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + + + else if (is_op(s[i])){ + + char curr = s[i]; + + while(!op.empty() and priority(op.top()) >= priority(curr)){ + + process(val, op.top()); + + op.pop(); + + } + + op.push(curr); + + } + + + + else { + + int num = 0; + + while (i < s.length() and isdigit(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + val.push(num); + + } + + } + + + + while(!op.empty()){ + + process(val, op.top()); + + op.pop(); + + } + + + + int ans = val.top(); + + val.pop(); + + return ans; + +} + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + + + string s; + + cin >> s; + + cout << evaluare(s); + + + + return 0; + +} diff --git a/eval/stack/9d5f0e1b-65a5-4721-9cab-a4739d0eb09e.cpp b/eval/stack/9d5f0e1b-65a5-4721-9cab-a4739d0eb09e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3e39ce44094afbf9ba392cb5b4b03457ff719ba --- /dev/null +++ b/eval/stack/9d5f0e1b-65a5-4721-9cab-a4739d0eb09e.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +using namespace std; + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + +string s; + +int num[100007],st[100007]; + +int pd=-1000000001,pi=-1000000002,plu=-1000000003,minu=-1000000004,inm=-1000000005,imp=-1000000006; + +int main() + +{ + + in>>s; + + int n=s.length(); + + int i=0,f=0,k=0; + + int nr=0; + + while(i=0) + + st[++k]=num[i]; + + else + + { + + if(num[i]==inm) + + { + + if(k<2) + + st[k]=0; + + else + + { + + st[--k]*=st[k+1]; + + } + + } + + else if(num[i]==imp) + + { + + st[--k]/=st[k+1]; + + } + + else if(num[i]==plu) + + { + + st[--k]+=st[k+1]; + + } + + else if(num[i]==minu) + + { + + st[--k]-=st[k+1]; + + } + + } + + } + + out< + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +string str, expr; + + + +void opp (int & idx) + +{ + + while (str[idx] >= '0' && str[idx] <= '9') { + + expr += str[idx]; + + idx ++; + + } + + expr += ' '; + + idx --; + +} + + + +int priority (int i) + +{ + + if (str[i] == '*' || str[i] == '/') return 2; + + if (str[i] == '+' || str[i] == '-') return 1; + + return 0; + +} + + + +void formaPoloneza (int n) + +{ + + for (int i = 0; i < n; i ++) { + + if (str[i] >= '0' && str[i] <= '9') opp(i); + + else if (priority(i) != 0) { + + if (s.empty()) s.push(i); + + else { + + while (!s.empty() && priority(s.top()) >= priority(i)) { + + expr += str[s.top()], s.pop(); + + } + + s.push(i); + + } + + } + + else { + + if (str[i] == '(') s.push(i); + + else { + + while (!s.empty() && str[s.top()] != '(') expr += str[s.top()], s.pop(); + + expr += ' '; + + if (!s.empty() && str[s.top()] == '(') s.pop(); + + } + + } + + } + + while (!s.empty()) expr += str[s.top()], s.pop(); + +} + + + +int strToInt (int & idx) + +{ + + int nr = 0; + + while (expr[idx] <= '9' && expr[idx] >= '0') nr = nr * 10 + (expr[idx] - '0'), idx ++; + + idx --; + + return nr; + +} + + + +int expresie (int i, int o1, int o2) + +{ + + if (expr[i] == '+') return o1 + o2; + + else if (expr[i] == '-') return o1 - o2; + + else if (expr[i] == '/') return o1 / o2; + + else return o1 * o2; + +} + + + +int evaluare (int n) + +{ + + int o1, o2; + + while (!s.empty()) s.pop(); + + for (int i = 0; i < n; i ++) { + + if (expr[i] >= '0' && str[i] <= '9') s.push(strToInt(i)); + + else if (expr[i] != ' ' && s.size() >= 2) { + + o1 = s.top(), s.pop(); + + o2 = s.top(), s.pop(); + + s.push(expresie(i, o2, o1)); + + } + + } + + return s.top(); + +} + + + +int main() + +{ + + getline(fin, str); + + int n = str.size(); + + formaPoloneza(n); + + fout << evaluare(expr.size()); + + return 0; + +} diff --git a/eval/stack/9e10efe1-fb6a-4ff0-9fcc-bd22ce89e434.cpp b/eval/stack/9e10efe1-fb6a-4ff0-9fcc-bd22ce89e434.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6c92c49dd5e4e21feaa7a8492c034984b7ccec7 --- /dev/null +++ b/eval/stack/9e10efe1-fb6a-4ff0-9fcc-bd22ce89e434.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],semne[100005],semne1[100005]; + +int h,stiva[100005],sol[100005],k,h1; + +int main() + +{ + + f>>s; + + int nr=0; + + for(int i=0; s[i]; i++) + + { + + if(s[i]=='('||s[i]==')'||s[i]=='*'||s[i]=='+'||s[i]=='-'||s[i]=='/') + + { + + if(s[i-1]!=')'&&s[i]!='(') + + stiva[++h]=nr; + + nr=0; + + semne1[++k]=s[i]; + + if(s[i]=='+'||s[i]=='-') + + while(semne1[k-1]!='('&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]=='*'||s[i]=='/') + + while(semne1[k-1]!='('&&semne1[k-1]!='+'&&semne1[k-1]!='-'&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]==')') + + { + + while(semne1[k-1]!='(') + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + k-=2; + + } + + } + + else + + nr=nr*10+s[i]-'0'; + + } + + if(s[strlen(s)-2]!=')'&&s[strlen(s)-1]!=')') + + stiva[++h]=nr; + + while(semne1[k]) + + semne[++h]=semne1[k],k--; + + for(int i=1; i<=h; i++) + + { + + if(semne[i]) + + { + + switch(semne[i]) + + { + + case '+': + + sol[h1-1]=sol[h1-1]+sol[h1]; + + break; + + case '-': + + sol[h1-1]=sol[h1-1]-sol[h1]; + + break; + + case '*': + + sol[h1-1]=sol[h1-1]*sol[h1]; + + break; + + case '/': + + sol[h1-1]=sol[h1-1]/sol[h1]; + + break; + + + + } + + h1--; + + } + + else + + sol[++h1]=stiva[i]; + + } + + g< +#include +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char A[100001]; + +int nl,a,b; + +queue ex; + +stack >semn; + +stack evaluare; + + + +int prioritate(char c) + +{ + + if(c=='+') + + return 1; + + if(c=='-') + + return 1; + + if(c=='*') + + return 2; + + if(c=='/') + + return 2; + + return 0; + +} + +int conv(pair c) + +{ + + if(c.first=='+') + + return -1; + + if(c.first=='-') + + return -2; + + if(c.first=='*') + + return -3; + + if(c.first=='/') + + return -4; + + return 0; + +} + +int nr(int &r) + +{ + + int d=0; + + while('0'<=A[r]&&A[r]<='9') + + { + + d=d*10+A[r]-'0'; + + r++; + + } + + return d; + +} + +int main() + +{ + + fin>>A; + + nl=strlen(A); + + int k=0; + + for(int i=0;i0) + + { + + while(!semn.empty()&&(prioritate(A[i])+k<=semn.top().second)) + + { + + ex.push(conv(semn.top())); + + semn.pop(); + + } + + semn.push({A[i],prioritate(A[i])+k}); + + } + + if(A[i]=='(') + + k+=2; + + if(A[i]==')') + + k-=2; + + } + + while(!semn.empty()) + + { + + ex.push(conv(semn.top())); + + semn.pop(); + + } + + while(!ex.empty()) + + { + + if(ex.front()>=0) + + { + + evaluare.push(ex.front()); + + + + ex.pop(); + + } + + else + + { + + a=evaluare.top(); + + evaluare.pop(); + + b=evaluare.top(); + + evaluare.pop(); + + if(ex.front()==-1) + + evaluare.push(b+a); + + if(ex.front()==-2) + + evaluare.push(b-a); + + if(ex.front()==-3) + + evaluare.push(b*a); + + if(ex.front()==-4) + + evaluare.push(b/a); + + + + ex.pop(); + + } + + } + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("evaluare.in"); ofstream cout("evaluare.out"); +string s; + + + + + + + +bool isOp(char c) { + + return c == '+' || c == '-' || c == '/' || c == '*'; + +} + + + +int priority(char op) { + + if (op == '/' || op == '*') return 2; + + if (op == '-' || op == '+') return 1; + + return -1; + +} + + + +void process_op(stack & st, char op) { + + int r = st.top(); st.pop(); + + int l = st.top(); st.pop(); + + + + switch (op) { + + case '+': st.push(l + r); break; + + case '-': st.push(l - r); break; + + case '/': st.push(l / r); break; + + case '*': st.push(l * r); break; + + } + +} + + + +int evaluate(string& s) { + + stack st; + + stack op; + + + + for (int i = 0; i < (int)s.size(); i++) { + + + + if (s[i] == '(') { + + op.push('('); + + } + + else if (s[i] == ')') { + + while (op.top() != '(') { + + process_op(st, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + else if (isOp(s[i])) { + + char curr_op = s[i]; + + while (!op.empty() && priority(op.top()) >= priority(curr_op)) { + + process_op(st, op.top()); + + op.pop(); + + } + + op.push(curr_op); + + } + + else { + + + + int num = 0; + + while (i < (int)s.size() && isalnum(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + st.push(num); + + } + + } + + + + while (!op.empty()) { + + process_op(st, op.top()); + + op.pop(); + + } + + return st.top(); + +} + + + + + + + +int main() { + + + + cin >> s; + + cout << evaluate(s); + + + + return 0; + +} diff --git a/eval/stack/a0d8efcf-c7f5-468e-a98e-b644926d6f7d.cpp b/eval/stack/a0d8efcf-c7f5-468e-a98e-b644926d6f7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfa4689a93d0fac006daa7caf6ac706de36e32d4 --- /dev/null +++ b/eval/stack/a0d8efcf-c7f5-468e-a98e-b644926d6f7d.cpp @@ -0,0 +1,198 @@ +#include +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +string s; + +int i,steve[100001],cnt,signsteve[100001],cnt1,steva[100001],contor; + +int formnr() + +{ + + int rez=0; + + for(; i='0'&&s[i]<='9'; i++) + + rez=rez*10+s[i]-'0'; + + i--; + + steve[++cnt]=rez; + +} + +void codif() + +{ + + for(i=0; i='0'&&s[i]<='9') + + formnr(); + + else if(s[i]=='+'||s[i]=='-') + + { + + while(cnt1&&signsteve[cnt1]<=-2) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + if(s[i]=='+') + + signsteve[++cnt1]=-2; + + else + + signsteve[++cnt1]=-3; + + } + + else if(s[i]=='/'||s[i]=='*') + + { + + while(cnt1&&signsteve[cnt1]<=-4) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + if(s[i]=='*') + + signsteve[++cnt1]=-4; + + else + + signsteve[++cnt1]=-5; + + } + + else + + { + + if(s[i]=='(') + + signsteve[++cnt1]=-1; + + else + + { + + while(cnt1&&signsteve[cnt1]!=-1) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + + cnt1--; + + } + + } + + } + + while(cnt1) + + { + + steve[++cnt]=signsteve[cnt1]; + + cnt1--; + + } + +} + +int main() + +{ + + in>>s; + + codif(); + + for(i=1; i<=cnt; i++) + + { + + if(steve[i]<=-1) + + { + + if(steve[i]==-2) + + { + + steva[contor-1]+=steva[contor]; + + contor--; + + } + + else if(steve[i]==-3) + + { + + steva[contor-1]-=steva[contor]; + + contor--; + + } + + else if(steve[i]==-4) + + { + + steva[contor-1]*=steva[contor]; + + contor--; + + } + + else if(steve[i]==-5) + + { + + steva[contor-1]/=steva[contor]; + + contor--; + + } + + } + + else + + steva[++contor]=steve[i]; + + } + + out< + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +stack ev; + +vector v; + +char c[100005], t; + +int i, n, nr, p; + + + + + +int putere(char c) + +{ + + switch(c) + + { + + case '+': return 1; + + case '-': return 1; + + + + case '*': return 2; + + case '/': return 2; + + + + case '(': return 0; + + } + +} + +int tip(char c) + +{ + + switch(c) + + { + + case '-': return -1; + + case '+': return -2; + + case '*': return -3; + + case '/': return -4; + + } + +} + +int numar() + +{ + + int nr=0; + + while('0'<=c[i]&&c[i]<='9') + + { + + nr=nr*10+c[i]-'0'; + + i++; + + } + + return nr; + +} + +int main() + +{ + + fin.getline(c, 100005); + + n=strlen(c); + + for(i=0;i=p) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + s.push(c[i]); + + } + + } + + } + + + + while(!s.empty()) + + { + + v.push_back(tip(s.top())); + + s.pop(); + + } + + + + for(i=0;i +#include +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +char s[100005]; + +int aux[100005]; + + + +void Precalc () + +{ + + stack < int > stiva; + + for (int i=0; s[i]; i++) + + { + + if (s[i] == '(') + + stiva.push(i); + + + + if (s[i] == ')') + + { + + aux[stiva.top()] = i; + + stiva.pop(); + + } + + } + +} + + + +int Solve (int st, int dr) + +{ + + int numar, k, j, v[100005], rez = 0; + + char semn = '+'; + + numar = k = 0; + + for (int i=st; i<=dr; i++) + + { + + if (s[i] == '+' || s[i] == '-' || s[i] == '/' || s[i] == '*') + + semn = s[i]; + + + + if (s[i] >= '0' && s[i] <= '9') + + { + + numar = 0, j = i; + + while (s[j] >= '0' && s[j] <= '9') + + { + + numar = numar * 10 + s[j] - '0'; + + j ++; + + } + + i = j-1; + + + + if (semn == '+') + + k ++, v[k] = numar; + + else if (semn == '-') + + k ++, v[k] = -numar; + + else if (semn == '*') + + v[k] = v[k] * numar; + + else if (semn == '/') + + v[k] = v[k] / numar; + + } + + + + if (s[i] == '(') + + { + + numar = Solve(i+1, aux[i]-1); + + if (semn == '+') + + k ++, v[k] = numar; + + else if (semn == '-') + + k ++, v[k] = -numar; + + else if (semn == '*') + + v[k] = v[k] * numar; + + else if (semn == '/') + + v[k] = v[k] / numar; + + + + i = aux[i]; + + } + + } + + for (int i=1; i<=k; i++) + + rez += v[i]; + + return rez; + +} + + + +int main() + +{ + + f >> s; + + Precalc(); + + g << Solve(0, strlen(s)-1); + + return 0; + +} diff --git a/eval/stack/a3607c18-2a11-41fd-80ed-74282fba3ad5.cpp b/eval/stack/a3607c18-2a11-41fd-80ed-74282fba3ad5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9a8ef322718b5bbc2c0304101fb83a5dc1a312 --- /dev/null +++ b/eval/stack/a3607c18-2a11-41fd-80ed-74282fba3ad5.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +int opPriority(char c) +{ + if (c == '(' || c == ')') return -1; + if (c == '+' || c == '-') return 1; + if (c == '*' || c == '/') return 2; + return 3; +} + +bool isDigit(char c) +{ + return '0' <= c && c <= '9'; +} + +class ExpressionEvaluator{ + public: + int Evaluate(string expr) + { + for (int i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (c == '(') { + op_.push(c); + priority_.push(opPriority(c)); + continue; + } + if (isDigit(c)) { + int number = c - '0'; + while (i+1 < expr.size() && isDigit(expr[i + 1])) { + ++i; + number = number * 10 + (expr[i] - '0'); + } + numbers_.push(number); + continue; + } + Pop_(opPriority(c)); + if (c != ')') { + priority_.push(opPriority(c)); + op_.push(c); + } + } + int result = numbers_.top(); + numbers_.pop(); + return result; + } + private: + void Pop_(int p) + { + while (!priority_.empty() && priority_.top() >= p) { + char op = op_.top(); + op_.pop(); + priority_.pop(); + + if (op == '(') + return; + + int b = numbers_.top(); + numbers_.pop(); + int a = numbers_.top(); + numbers_.pop(); + + if (op == '+') numbers_.push(a + b); + else if (op == '-') numbers_.push(a - b); + else if (op == '*') numbers_.push(a * b); + else if (op == '/') numbers_.push(a / b); + } + } + + stack numbers_; + stack priority_; + stack op_; +}; + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string s; + fin >> s; + + ExpressionEvaluator exprEval; + fout << exprEval.Evaluate("(" + s + ")"); + + return 0; +} diff --git a/eval/stack/a3b8fbe9-6ee8-4985-a5b3-872eb218255b.cpp b/eval/stack/a3b8fbe9-6ee8-4985-a5b3-872eb218255b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eef6042e3d6956441f1ba5078d5d5a627749d430 --- /dev/null +++ b/eval/stack/a3b8fbe9-6ee8-4985-a5b3-872eb218255b.cpp @@ -0,0 +1,249 @@ +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 100005; + +int fp[NMAX], st[NMAX], s[NMAX]; + +char sh[NMAX]; + + + +int main() { + + + + freopen ("evaluare.in", "r", stdin); + + freopen ("evaluare.out", "w", stdout); + + + + int n, nfp, nst, x, i, nr, dei; + + + + scanf ("%s", sh); + + + + n = strlen(sh); + + for (i = 0; i < n; i++) { + + if (sh[i] == '*') + + s[i] = -1; + + if (sh[i] == '/') + + s[i] = -2; + + if (sh[i] == '+') + + s[i] = -3; + + if (sh[i] == '-') + + s[i] = -4; + + if (sh[i] == '(') + + s[i] = -5; + + if (sh[i] == ')') + + s[i] = -6; + + if (sh[i] >= '0' && sh[i] <= '9') + + s[i] = sh[i] - '0'; + + } + + nfp = 0; + + nst = 0; + + i = 0; + + while (i < n) { + + x = s[i]; + + if (x >= 0) { + + nr = 0; + + while (i < n && s[i] >= 0) { + + nr = nr * 10 + s[i]; + + i++; + + } + + nfp++; + + fp[nfp] = nr; + + } + + else { + + while (i < n && s[i] < 0) { + + x = s[i]; + + if (x == -5) { + + nst++; + + st[nst] = x; + + } + + else if (x >= -2) { + + while (nst > 0 && st[nst] >= -2) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst++; + + st[nst] = x; + + } + + else if (x >= -4) { + + while (nst > 0 && st[nst] >= -4) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst++; + + st[nst] = x; + + } + + else { + + while (nst > 0 && st[nst] != -5) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + nst--; + + } + + i++; + + } + + } + + } + + while (nst > 0) { + + nfp++; + + fp[nfp] = st[nst]; + + nst--; + + } + + + + + + + + nst = 0; + + for (i = 1; i < NMAX; i++) + + st[i] = 0; + + nfp++; + + fp[nfp] = -3; + for (i = 0; i <= nfp; i++) { + + if (fp[i] >= 0) { + + nst++; + + st[nst] = fp[i]; + + dei = 1; + + } + + else { + + nst--; + + if (fp[i] == -1) + + st[nst] *= st[nst + 1]; + + if (fp[i] == -2) + + st[nst] /= st[nst + 1]; + + if (fp[i] == -3) + + st[nst] += st[nst + 1]; + + if (fp[i] == -4) + + st[nst] -= st[nst + 1]; + + } + + } + + if (st[0] != 0) + + printf ("%d", st[0]); + + else + + printf ("%d", st[1]); + + + + + + + + return 0; + +} diff --git a/eval/stack/a4b0eaf7-c2de-4e0f-b992-8b9cc2c0799b.cpp b/eval/stack/a4b0eaf7-c2de-4e0f-b992-8b9cc2c0799b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae800ddfa6148c9e7b0698bac33e138cd6b80caf --- /dev/null +++ b/eval/stack/a4b0eaf7-c2de-4e0f-b992-8b9cc2c0799b.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + + + +const int NMAX = 100004; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack < int > S; + +stack < char > Op; + + + +char C[NMAX]; + + + +int pi = 2000000000, x; + +int pl = pi+1; + +int m = pi-1; + +int sup = pi-2; + +int ori = pi+2; + +int N; + + + +int NR( int &i ) + +{ + + int nr = 0; + + while( C[i] >= '0' && C[i] <= '9' ) + + { + + nr = nr*10 + C[i]-'0'; + + i++; + + } + + i--; + + return nr; + +} + + + +int Priority( char op ) + +{ + + if( op == '*' || op == '/' ) + + return 2; + + if( op == '+' || op == '-' ) + + return 1; + + return 0; + +} + + + +int ApplyOp( int x, int y, char op ) + +{ + + switch( op ) + + { + + case '+': return x+y; + + case '-': return x-y; + + case '*': return x*y; + + case '/': return x/y; + + } + +} + +void Solve() + +{ + + fin >> C; + + N = strlen( C ); + + + + for( int i = 0; C[i]; ++i ) + + { + + + + if( C[i] == '(' ) + + { + + Op.push( C[i] ); + + continue; + + } + + if( C[i] >= '0' && C[i] <= '9' ) + + S.push( NR( i ) ); + + + + else + + if( C[i] == ')' ) + + { + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + + + } + + else + + { + + while( Op.size() && Priority( Op.top() ) >= Priority( C[i] ) ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + + + Op.pop(); + + } + + Op.push( C[i] ); + + } + + } + + while( Op.size() && Op.top() != '(' ) + + { + + int val1 = S.top();S.pop(); + + int val2 = S.top();S.pop(); + + + + S.push( ApplyOp( val2, val1, Op.top() ) ); + + Op.pop(); + + } + + if( Op.size() ) Op.pop(); + + fout << S.top(); + +} + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/eval/stack/a538c8de-9188-4a13-b822-c134d9bc7c37.cpp b/eval/stack/a538c8de-9188-4a13-b822-c134d9bc7c37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..664a290750859b2d36e353d8500ccf41eab33bf7 --- /dev/null +++ b/eval/stack/a538c8de-9188-4a13-b822-c134d9bc7c37.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +string expression; + +bool isOperator(char ch) { + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; +} + +int getPriority(char ch) { + if (ch == '+' || ch == '-') { + return 1; + } else if (ch == '*' || ch == '/') { + return 2; + } + return -1; +} + +void applyOperator(stack& numbers, char op) { + int secondNo = numbers.top(); + numbers.pop(); + int firstNo = numbers.top(); + numbers.pop(); + + switch (op) { + case '+': { + numbers.push(firstNo + secondNo); + break; + } + case '-': { + numbers.push(firstNo - secondNo); + break; + } + case '*': { + numbers.push(firstNo * secondNo); + break; + } + case '/': { + numbers.push(firstNo / secondNo); + break; + } + } +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + cin >> expression; + stack numbers; + stack operators; + + for (size_t i = 0; i < expression.size(); i++) { + if (expression[i] == '(') { + operators.push('('); + } else if (expression[i] == ')') { + while (operators.top() != '(') { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.pop(); + } else if (isOperator(expression[i])) { + while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.push(expression[i]); + } else { + assert(isdigit(expression[i])); + int number = 0; + while (i < expression.size() && isdigit(expression[i])) { + number = number * 10 + expression[i++] - '0'; + } + i--; + numbers.push(number); + } + } + + while (!operators.empty()) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + + cout << numbers.top() << "\n"; + return 0; +} diff --git a/eval/stack/a5d58fe0-2e4c-434f-b07e-0ea4635dcc04.cpp b/eval/stack/a5d58fe0-2e4c-434f-b07e-0ea4635dcc04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d18d1e3eb177aa4ec5ee742dc5bd9ff1ab3a776c --- /dev/null +++ b/eval/stack/a5d58fe0-2e4c-434f-b07e-0ea4635dcc04.cpp @@ -0,0 +1,174 @@ +#include + + +using namespace std; + + + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +stack operations; + +stack numbers; + +string expression; + + + +bool isOperator(char s) { + + if(s == '+' || s == '-' || s == '*' || s == '/') { + + return 1; + + } + + return 0; + +} + + + +int priority(char s) { + + if(s == '+' || s == '-') { + + return 0; + + } else if(s == '*' || s == '/') { + + return 1; + + } + + return -1; + +} + + + +int calculateOperation() { + + int a = numbers.top(); numbers.pop(); + + int b = numbers.top(); numbers.pop(); + + + + char op = operations.top(); operations.pop(); + + if(op == '+') { + + return a + b; + + } else if(op == '-') { + + return b - a; + + } else if(op == '/') { + + return b / a; + + } + + return a * b; + +} + + + +void prepare() { + + int i = 0, next_value; + + while(i < expression.length()) { + + if(isOperator(expression[i])) { + + while(operations.size() && priority(expression[i]) <= priority(operations.top())) { + + int next_value = calculateOperation(); + + numbers.push(next_value); + + } + + operations.push(expression[i]); + + i++; + + } else if(isdigit(expression[i])) { + + int integer = 0; + + while(expression[i] >= '0' && expression[i] <= '9' && i < expression.length()) { + + integer = integer * 10 + (expression[i] - '0'); + + i++; + + } + + numbers.push(integer); + + } else if(expression[i] == '(') { + + operations.push(expression[i]); + + i++; + + } else if(expression[i] == ')') { + + while(operations.top() != '(') { + + next_value = calculateOperation(); + + numbers.push(next_value); + + } + + i++; + + operations.pop(); + + } + + } + +} + + + +int calculateExpression() { + + while(operations.size()) { + + int next_value = calculateOperation(); + + numbers.push(next_value); + + } + + return numbers.top(); + +} + + + +int main() + +{ + + in >> expression; + + prepare(); + + out << calculateExpression(); + + return 0; + +} diff --git a/eval/stack/a7f24edd-7836-4fc8-bfa0-f2dc390bd30d.cpp b/eval/stack/a7f24edd-7836-4fc8-bfa0-f2dc390bd30d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af6e5223ea62954f44dd72ba26a26f7274e53692 --- /dev/null +++ b/eval/stack/a7f24edd-7836-4fc8-bfa0-f2dc390bd30d.cpp @@ -0,0 +1,224 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +vector op; + +vector numbers; + +string fin; + +void postfix() + +{ + + int n = strlen(a); + + for (int i = 0; i < n; ++i) + + { + + int nr = 0; + + bool number = false; + + while (i < n && a[i] >= '0' && a[i] <= '9') + + { + + number = true; + + nr = nr * 10 + a[i] - '0'; + + i++; + + } + + if (number) + + { + + fin += to_string(nr) + ' '; + + i--; + + continue; + + } + + if (a[i] == '*' || a[i] == '/') + + { + + while (op.size() != 0 && op[op.size() - 1] != '(' && op[op.size() - 1] != '+' && op[op.size() - 1] != '-') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + op.push_back(a[i]); + + } + + else if (a[i] == '+' || a[i] == '-') + + { + + while (op.size() != 0 && op[op.size() - 1] != '(') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + op.push_back(a[i]); + + } + + else if (a[i] == '(') + + { + + op.push_back(a[i]); + + } + + else + + { + + while (op.size() != 0 && op[op.size() - 1] != '(') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + if(op.size() != 0) + + op.pop_back(); + + } + + } + + while (op.size() != 0) + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + +} + + + +int calculate(int a, int b, char op) + +{ + + switch (op) + + { + + case '+': + + return a + b; + + break; + + case '-': + + return a - b; + + break; + + case '*': + + return a * b; + + break; + + case '/': + + return a / b; + + break; + + } + +} + + + +int eval() + +{ + + for (int i = 0; i < fin.size(); ++i) + + { + + int nr = 0; + + bool number = false; + + while (fin[i] >= '0' && fin[i] <= '9') + + { + + nr = nr * 10 + fin[i] - '0'; + + i++; + + number = true; + + } + + if (number) + + { + + numbers.push_back(nr); + + } + + else if (fin[i] != ' ') + + { + + int val = calculate(numbers[numbers.size() - 2], numbers[numbers.size() - 1], fin[i]); + + numbers.pop_back(); + + numbers.pop_back(); + + numbers.push_back(val); + + } + + } + + return numbers[0]; + +} + + + +int main() + +{ + + f.getline(a, 100005); + + postfix(); + + g< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char A[100001]; + +int nl,a,b; + +queue ex; + +stack >semn; + +stack evaluare; + + + +int prioritate(char c) + +{ + + if(c=='+') + + return 1; + + if(c=='-') + + return 1; + + if(c=='*') + + return 2; + + if(c=='/') + + return 2; + + return 0; + +} + +int conv(pair c) + +{ + + if(c.first=='+') + + return -1; + + if(c.first=='-') + + return -2; + + if(c.first=='*') + + return -3; + + if(c.first=='/') + + return -4; + + return 0; + +} + +int nr(int &r) + +{ + + int d=0; + + while('0'<=A[r]&&A[r]<='9') + + { + + d=d*10+A[r]-'0'; + + r++; + + } + + return d; + +} + +int main() + +{ + + fin>>A; + + nl=strlen(A); + + int k=0; + + for(int i=0;i0) + + { + + while(!semn.empty()&&(prioritate(A[i])+k<=semn.top().second)) + + { + + ex.push(conv(semn.top())); + + semn.pop(); + + } + + semn.push({A[i],prioritate(A[i])+k}); + + } + + if(A[i]=='(') + + k+=2; + + if(A[i]==')') + + k-=2; + + } + + while(!semn.empty()) + + { + + ex.push(conv(semn.top())); + + semn.pop(); + + } + + while(!ex.empty()) + + { + + if(ex.front()>=0) + + { + + evaluare.push(ex.front()); + + + + ex.pop(); + + } + + else + + { + + a=evaluare.top(); + + evaluare.pop(); + + b=evaluare.top(); + + evaluare.pop(); + + if(ex.front()==-1) + + evaluare.push(b+a); + + if(ex.front()==-2) + + evaluare.push(b-a); + + if(ex.front()==-3) + + evaluare.push(b*a); + + if(ex.front()==-4) + + evaluare.push(b/a); + + + + ex.pop(); + + } + + } + + fout< +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string exp, pol; + + + +int priority(char c) { + + if (c == '(') + + return 0; + + if (c == '-' || c == '+') + + return 1; + + return 2; + +} + + + +void genFormaPoloneza(string s) { + + stack oper; + + int poz = 0; + + + + while (poz < s.length()) { + + if (isdigit(s[poz])) { + + pol += s[poz]; + + while (poz + 1 < s.length() && isdigit(s[poz + 1])) + + pol += exp[++poz]; + + pol += '.'; + + } else if (s[poz] == '(') { + + oper.push('('); + + } else if (s[poz] == ')') { + + while (oper.top() != '(') + + pol += oper.top(), oper.pop(); + + oper.pop(); + + } else { + + while (!oper.empty() && priority(oper.top()) >= priority(s[poz])) + + pol += oper.top(), oper.pop(); + + oper.push(s[poz]); + + } + + poz++; + + } + + + + while (!oper.empty()) + + pol += oper.top(), oper.pop(); + +} + + + +int evalPol(string s) { + + stack num; + + int poz = -1; + + + + while (++poz < s.length()) { + + if (isdigit(s[poz])) { + + int nr = 0; + + do { + + nr = nr = nr * 10 + s[poz] - '0'; + + } while (poz < s.length() && isdigit(s[++poz])); + + + + num.push(nr); + + } else { + + int nr = num.top(); + + num.pop(); + + + + if (s[poz] == '+') + + num.top() += nr; + + else if (s[poz] == '-') + + num.top() -= nr; + + else if (s[poz] == '*') + + num.top() *= nr; + + else if (s[poz] == '/') + + num.top() /= nr; + + } + + } + + + + return num.top(); + +} + + + +int main() { + + fin >> exp; + + genFormaPoloneza(exp); + + fout << evalPol(pol); + + return 0; + +} diff --git a/eval/stack/a95b812e-9f43-4752-bddb-73177f3449dc.cpp b/eval/stack/a95b812e-9f43-4752-bddb-73177f3449dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff36a17d166db552e1a427a5588a911eca8592a9 --- /dev/null +++ b/eval/stack/a95b812e-9f43-4752-bddb-73177f3449dc.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +ifstream cin("evaluare.in"); +ofstream cout("evaluare.out"); + + + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +vector < int > val(256); + +void getPol (string &pol) { + + string s; + getline(cin, s); + + stack < char > stk; + + int ind = 0; + while (ind < (int)(s).size()) { + + bool digit = false; + while (ind < (int)(s).size() and isdigit(s[ind])) { + pol += s[ind ++]; + digit = true; + } + + if (digit) pol += ' '; + + if (ind == (int)(s).size()) break; + + if (s[ind] == '(') { + stk.push(s[ind ++]); + continue; + } + + if (s[ind] == ')') { + while (not stk.empty() and stk.top() != '(') { + pol += stk.top(); + stk.pop(); + } + stk.pop(); + ++ ind; + continue; + } + + while (not stk.empty() and val[stk.top()] >= val[s[ind]]) { + if (stk.top() == '(') break; + pol += stk.top(); + stk.pop(); + } + stk.push(s[ind ++]); + } + + while (not stk.empty()) { + pol += stk.top(); + stk.pop(); + } +} + +int main() { + + + + + + + ios::sync_with_stdio(false); + cin.tie(0);cout.tie(0); + + val['('] = 20; + val[')'] = 20; + val['*'] = 6; + val['/'] = 6; + val['+'] = 3; + val['-'] = 3; + + string pol; + getPol(pol); + + int ind = 0; + stack < int > stk; + + while (ind < (int)(pol).size()) { + + int nr = 0; + while (ind < (int)(pol).size() and isdigit(pol[ind])) { + nr = (nr << 1) + (nr << 3) + pol[ind ++] - '0'; + } + + stk.push(nr); + + while (ind < (int)(pol).size() and not isdigit(pol[ind])) { + + if (pol[ind] == ' ') { + ++ ind; + continue; + } + + int b = stk.top(); + stk.pop(); + int a = stk.top(); + stk.pop(); + + if (pol[ind] == '+') { + stk.push(a + b); + } + else if (pol[ind] == '-') { + stk.push(a - b); + } + else if (pol[ind] == '/') { + stk.push(a / b); + } + else if (pol[ind] == '*') { + stk.push(a * b); + } + + ++ ind; + } + } + + cout << stk.top() << '\n'; +} diff --git a/eval/stack/ada575bc-76b7-4b9e-803c-a61015ed696f.cpp b/eval/stack/ada575bc-76b7-4b9e-803c-a61015ed696f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84c3f22b637468433ed909f3ff43ee20fa4dfd32 --- /dev/null +++ b/eval/stack/ada575bc-76b7-4b9e-803c-a61015ed696f.cpp @@ -0,0 +1,180 @@ +#include + + + + +using namespace std; + +typedef long long ll; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +const int N = 100005; + +char c[N]; + +string s; + +stack < int > nums; + +stack < char > op; + +bool is_op(char ch) { + + if (ch == '+' || ch == '-' || ch == '/' || ch == '*') { + + return true; + + } + + return false; + +} + +int val(char ch) { + + if (ch == '+' || ch == '-') { + + return 1; + + } + + if (ch == '*' || ch == '/') { + + return 2; + + } + + return -1; + +} + +int do_op(char op) { + + int a = nums.top(); + + nums.pop(); + + int b = nums.top(); + + nums.pop(); + + if (op == '+') { + + b += a; + + } + + if (op == '-') { + + b -= a; + + } + + if (op == '*') { + + b *= a; + + } + + if (op == '/') { + + b /= a; + + } + + nums.push(b); + +} + +int main() { + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + in >> s; + + for (int i = 0; i < s.size(); i++) { + + c[i + 1] = s[i]; + + } + + int n = s.size(); + + for (int i = 1; i <= n; i++) { + + if (c[i] == '(') { + + op.push('('); + + } + + else if (c[i] == ')') { + + while(op.top() != '(') { + + char oper = op.top(); + + op.pop(); + + do_op(oper); + + } + + op.pop(); + + } + + else if (is_op(c[i])) { + + while(op.size() && val(c[i]) <= val(op.top())) { + + do_op(op.top()); + + op.pop(); + + } + + op.push(c[i]); + + } + + else { + + int nr = 0; + + int j = i; + + while(c[j] >= '0' && c[j] <= '9') { + + nr = nr * 10 + (c[j] - '0'); + + j++; + + } + + nums.push(nr); + + i = j - 1; + + } + + } + + while(op.size()) { + + do_op(op.top()); + + op.pop(); + + } + + out << nums.top() << '\n'; + + return 0; + +} diff --git a/eval/stack/ade60529-0910-4144-9651-156488b31e67.cpp b/eval/stack/ade60529-0910-4144-9651-156488b31e67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad25faddab6e77e25df08fad3cb1cfca19814e34 --- /dev/null +++ b/eval/stack/ade60529-0910-4144-9651-156488b31e67.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in ("evaluare.in"); +ofstream out ("evaluare.out"); + +stack s; +deque < pair > fin; +stack r; + + + + + + + +int main() +{ + char c,aux='a'; + long long cnt=0,nr=0,x1,x2,rez; + while (in.get(c)&&c!='\n') + { + if (isdigit(c)&&isdigit(aux)) + nr=nr*10+c-48; + else if (isdigit(c)&&!isdigit(aux)) + nr=c-48; + else if (!isdigit(c)&&isdigit(aux)) + fin.push_back({nr,0}); + if (!isdigit(c)) + { + if (c=='+'||c=='-') + { + while (!s.empty()&&s.top()) + fin.push_back({s.top(),1}),s.pop(); + if (c=='+') + s.push(2); + else + s.push(1); + } + else if (c=='*'||c=='/') + { + while (!s.empty()&&s.top()>=3) + fin.push_back({s.top(),1}),s.pop(); + if (c=='*') + s.push(3); + else + s.push(4); + } + else if (c=='(') + s.push(0); + else + { + while (!s.empty()&&s.top()!=0) + fin.push_back({s.top(),1}),s.pop(); + s.pop(); + } + } + aux=c; + } + if (isdigit(aux)) + fin.push_back({nr,0}); + while (!s.empty()) + fin.push_back({s.top(),1}),s.pop(); + for (int i=0;i +#include +#include +#include +std::string expr; + +int pos = 0; + +bool issign(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +int eval() { + + std::vector stack; + + std::string number; + + char prev_sign = '+'; + int value; + + if (expr[pos] == '-') { + ++pos; + prev_sign = '-'; + } + + for (; pos < expr.size(); ++pos) { + + if (isdigit(expr[pos])) { + number += expr[pos]; + + } else if (issign(expr[pos])) { + + if (!number.empty()) + value = std::stoi(number); + + number.clear(); + + if (prev_sign == '*') { + + stack.back() *= value; + + } else if (prev_sign == '/') { + + stack.back() /= value; + + } else if (prev_sign == '+') { + stack.push_back(value); + + } else if (prev_sign == '-') { + stack.push_back(-value); + + } + + prev_sign = expr[pos]; + } else if (expr[pos] == '(') { + + ++pos; + value = eval(); + + } else if (expr[pos] == ')') { + + + if (!number.empty()) + value = std::stoi(number); + number.clear(); + + if (prev_sign == '*') { + + stack.back() *= value; + + } else if (prev_sign == '/') { + + stack.back() /= value; + + } else if (prev_sign == '+') { + stack.push_back(value); + + } else if (prev_sign == '-') { + stack.push_back(-value); + + } + + break; + } + } + + int ans = 0; + for (int el : stack) + ans += el; + + + return ans; +} + +void read() { + std::ifstream in("evaluare.in"); + + std::getline(in, expr); + expr += '+'; + + in.close(); +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << eval() << '\n'; + + out.close(); + return 0; +} diff --git a/eval/stack/ae2eac0a-69b9-4014-a88f-992370e49550.cpp b/eval/stack/ae2eac0a-69b9-4014-a88f-992370e49550.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27572942cf58f770fa2a883561c0d8d95d3aa6eb --- /dev/null +++ b/eval/stack/ae2eac0a-69b9-4014-a88f-992370e49550.cpp @@ -0,0 +1,127 @@ +#include +#include +#include + +using namespace std; +stack ch; +queue st; +int pr[300],hashh[300],f[10],v[100005]; +int main() +{ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + string s; + cin>>s; + int n=s.size(); + pr['+']=1; + pr['-']=1; + pr['/']=2; + pr['*']=2; + hashh['+']=1000000001; + hashh['-']=1000000002; + hashh['/']=1000000003; + hashh['*']=1000000004; + hashh['(']=1000000006; + hashh[')']=1000000007; + f[1]='+'; + f[2]='-'; + f[3]='/'; + f[4]='*'; + f[6]='('; + f[7]=')'; + + int i; + + + for(i=0;i='0' and i=pr[s[i]] and ch.top()!='('){ + st.push(hashh[ch.top()]); + ch.pop(); + if(ch.empty()==true) + break; + } + ch.push(s[i]); + } + + } + + } + + + } + + int a; + while(ch.empty()==false){ + st.push(hashh[ch.top()]); + ch.pop(); + } + int cnt=0; + while(st.empty()==false){ + a=st.front(); + st.pop(); + if(a>=1000000000){ + + if(f[a-1000000000]=='+'){ + cnt--; + v[cnt]=v[cnt]+v[cnt+1]; + } + if(f[a-1000000000]=='-'){ + cnt--; + v[cnt]=v[cnt]-v[cnt+1]; + } + if(f[a-1000000000]=='*'){ + cnt--; + v[cnt]=v[cnt]*v[cnt+1]; + } + if(f[a-1000000000]=='/'){ + cnt--; + v[cnt]=v[cnt]/v[cnt+1]; + } + } + else{ + + v[++cnt]=a; + } + } + + cout< + + +using namespace std; + + + +ifstream fin( "evaluare.in" ); + +ofstream fout( "evaluare.out" ); + + + +string s; + + + +deque var; + +deque op; + + + +void Read() { + + fin >> s; + + fin.close(); + +} + + + +bool is_operator( char c ) { + + return strchr( "+-*/", c ); + +} + + + +int Operate( int a, int b, char op ) { + + if( op == '+' ) return a + b; + + if( op == '-' ) return a - b; + + if( op == '*' ) return a * b; + + if( op == '/' ) return a / b; + +} + + + +int Priority( char ch ) { + + if( ch == '(' ) return 0; + + if( ch == '+' || ch == '-' ) return 1; + + if( ch == '/' || ch == '*' ) return 2; + +} + + + +void Do() { + + for( int i = 0; i < s.size(); ++i ) { + + if( s[i] == '(' ) { op.push_back( '(' ); continue; } + + if( isdigit( s[i] ) ) { + + int aux = 0; + + + + while( i < s.size() && isdigit( s[i] ) ) { + + aux = aux * 10 + s[i] - '0'; + + ++i; + + } + + --i; + + + + var.push_back( aux ); + + continue; + + } + + if( is_operator( s[i] ) ) { + + while( !op.empty() && Priority( op.back() ) >= Priority( s[i] ) ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.push_back( s[i] ); + + continue; + + } + + if( s[i] == ')' ) { + + while( op.back() != '(' ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + op.pop_back(); + + } + + + + } + + + + while( !op.empty() ) { + + int a, b; + + char Op = op.back(); + + + + op.pop_back(); + + b = var.back(); var.pop_back(); + + a = var.back(); var.pop_back(); + + + + var.push_back( Operate( a, b, Op ) ); + + } + + + + fout << var.back() << '\n'; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/eval/stack/aff8956e-1920-48cf-a493-5a540a78707e.cpp b/eval/stack/aff8956e-1920-48cf-a493-5a540a78707e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb8f3e02e36c9f5f72e5cd58a25f65de004d29fc --- /dev/null +++ b/eval/stack/aff8956e-1920-48cf-a493-5a540a78707e.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using std::vector; +using std::string; + +std::ifstream fin("evaluare.in"); +std::ofstream fout("evaluare.out"); + +int main() { + string str; + fin >> str; str += '?'; + + char prev = '?'; + vector stVal; + vector stOpr; + + auto pop = [&]() { + int nr = stVal.back(); + stVal.pop_back(); + if (stOpr.back() == '+') stVal.back() += nr; else + if (stOpr.back() == '-') stVal.back() -= nr; else + if (stOpr.back() == '*') stVal.back() *= nr; else + if (stOpr.back() == '/') stVal.back() /= nr; + stOpr.pop_back(); + }; + + for (char chr : str) { + if ('0' <= chr && chr <= '9') { + if ('0' <= prev && prev <= '9') + stVal.back() = stVal.back() * 10 + chr - '0'; + else + stVal.push_back(chr - '0'); + } + else { + if ((('0' <= prev && prev <= '9') || prev == ')') && !stOpr.empty()) { + if (stOpr.back() == '*' || stOpr.back() == '/') + pop(); + if ((stOpr.back() == '+' || stOpr.back() == '-') && chr != '*' && chr != '/') + pop(); + } + if (chr == ')') + stOpr.pop_back(); + else + stOpr.push_back(chr); + } + prev = chr; + } + fout << stVal[0] << '\n'; + + fout.close(); + return 0; +} diff --git a/eval/stack/b0653c62-54ad-4c9e-91d0-698ac9c36beb.cpp b/eval/stack/b0653c62-54ad-4c9e-91d0-698ac9c36beb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bfdcb026a97d851f6c3a61a4ae65b878469d7f4 --- /dev/null +++ b/eval/stack/b0653c62-54ad-4c9e-91d0-698ac9c36beb.cpp @@ -0,0 +1,284 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +string s; + + + +inline int Precedence(char ch) + +{ + + if (ch == '^') + + return 3; + + if (ch == '*' || ch == '/') + + return 2; + + return 1; + +} + + + +inline void InfixToPostfix() + +{ + + int i; + + string t; + + stack st; + + for (i = 0; s[i]; ++i) + + if (isdigit(s[i]) || (s[i]=='-' && (!i || s[i-1]=='(') && isdigit(s[i+1]))) + + { + + if (s[i] == '-') + + { + + t.push_back('-'); + + ++i; + + } + + while (isdigit(s[i])) + + { + + t.push_back(s[i]); + + ++i; + + } + + t.push_back(' '); + + --i; + + } + + else if (s[i] == '(') + + st.push('('); + + else if (s[i] == ')') + + { + + while (st.top() != '(') + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + st.pop(); + + } + + else + + { + + while (!st.empty() && st.top() != '(' && Precedence(s[i]) <= Precedence(st.top())) + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + st.push(s[i]); + + } + + while (!st.empty()) + + { + + t.push_back(st.top()); + + t.push_back(' '); + + st.pop(); + + } + + s = t; + +} + + + +inline int Power(int b, int e) + +{ + + int ans; + + ans = 1; + + while (e) + + { + + if (e % 2) + + ans = ans * b; + + b = b * b; + + e /= 2; + + } + + return ans; + +} + + + +inline int Calc(int a, int b, char op) + +{ + + if (op == '^') + + return Power(a, b); + + if (op == '*') + + return a * b; + + if (op == '/') + + return a / b; + + if (op == '+') + + return a + b; + + return a - b; + +} + + + +inline int Evaluate() + +{ + + int i, sign, x; + + vector st; + + for (i = 0; s[i]; ++i) + + if (isdigit(s[i]) || (s[i]=='-' && isdigit(s[i+1]))) + + { + + if (s[i] == '-') + + { + + sign = -1; + + ++i; + + } + + else + + sign = 1; + + x = 0; + + while (isdigit(s[i])) + + { + + x = x * 10 + s[i] - '0'; + + ++i; + + } + + st.push_back(sign * x); + + --i; + + } + + else if (s[i] != '(' && s[i] != ')' && s[i]!=' ') + + + + { + + x = Calc(st[st.size() - 2], st[st.size() - 1], s[i]); + + st.pop_back(), st.pop_back(); + + st.push_back(x); + + } + + return st.back(); + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(nullptr), cout.tie(nullptr); + + cin >> s; + + cin.close(); + + InfixToPostfix(); + + cout << Evaluate(); + + cout.close(); + + + + return 0; + +} diff --git a/eval/stack/b07ca4dc-fbc4-45ea-b56d-e3e619e7a07f.cpp b/eval/stack/b07ca4dc-fbc4-45ea-b56d-e3e619e7a07f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..814d8b54aecb4ee62e54ff768213c8bb90aade08 --- /dev/null +++ b/eval/stack/b07ca4dc-fbc4-45ea-b56d-e3e619e7a07f.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +char a[100005]; + +stack op; + +stack val; + + + +int valoare (char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 0; + + + +} + +int operatie (int x, int y, char op) + +{ + + if(op=='+') + + return x+y; + + if(op=='-') + + return x-y; + + if(op=='*') + + return x*y; + + if(op=='/') + + return x/y; + + + + + +} + + + +int evaluare (int lung ) + +{ + + + + for(int i=0; i=valoare(a[i])) + + { + + int val2 = val.top(); + + val.pop(); + + + + int val1 = val.top(); + + val.pop(); + + + + char ops = op.top(); + + op.pop(); + + + + val.push(operatie(val1, val2, ops)); + + } + + op.push(a[i]); + + } + + + + } + + while(!op.empty()) + + { + + int doi=val.top(); + + val.pop(); + + + + int prim=val.top(); + + val.pop(); + + + + char operation=op.top(); + + op.pop(); + + + + val.push(operatie(prim,doi,operation)); + + } + + + + return val.top(); + + + +} + +int main() + +{ + + f.getline(a,100001); + + int x=strlen(a); + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + +long doOperation(char op, long left, long right) { + + switch( op ) { + + case '+': return left+right; + case '-': return left-right; + case '*': return left*right; + case '/': return (left/right); + + default: { + return 0; + } + + } +} + + +void computeNextInStack(stack & numberStack, stack & operatorStack) { + + char op = operatorStack.top(); + operatorStack.pop(); + + long right = numberStack.top(); + numberStack.pop(); + + long left = numberStack.top(); + numberStack.pop(); + + long result = doOperation(op, left, right); + + numberStack.push( result ); +} + +unsigned int priority(char op) { + + switch( op ) { + + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + default: + return 0; + } +} + + +long parseExpression(string const expression) { + + stack numberStack; + stack operatorStack; + + + string::const_iterator pos = expression.begin(); + + + while(pos != expression.end()) { + + char current = *pos; + + switch(current) { + + case '+': + case '-': + case '*': + case '/': + + { + + while( + !operatorStack.empty() && + + priority(operatorStack.top()) >= priority(current) + ) + + { + computeNextInStack(numberStack, operatorStack); + } + + operatorStack.push(current); + + break; + } + + + + case '(': { + + operatorStack.push(current); + + break; + } + + + case ')': { + + if(operatorStack.empty()) { + + + + return 0; + } + + while(operatorStack.top() != '(') { + + computeNextInStack(numberStack, operatorStack); + + + if(operatorStack.empty()) { + + + + return 0; + } + } + + operatorStack.pop(); + break; + } + + + + default: { + + if(isdigit(current)) { + + + long number = 0; + + while( pos != expression.end() && isdigit(*pos)) { + + number *= 10; + number += (*pos)-'0'; + pos++; + + } + + + numberStack.push(static_cast(number)); + + pos--; + + } else { + + } + + break; + } + } + + pos++; + } + + while(!operatorStack.empty()) { + + computeNextInStack(numberStack, operatorStack); + } + + return numberStack.top(); +} + + +int main(int argc, char const *argv[]) +{ + + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string expression; + + getline(fin, expression); + + long result = parseExpression(expression); + + fout< +using namespace std; +typedef unsigned long long ull; +typedef long long ll; +struct my_timer{ + chrono::time_point start; + my_timer(): + start(chrono::high_resolution_clock::now()){} + ~my_timer(){ + std::chrono::duration diff = chrono::high_resolution_clock::now() - start; + clog< val; + stack op; + int c=getchar(); + while(c!=EOF && c != '\n'){ + if(isdigit(c)){ + int num = c&15; + while(isdigit(c=getchar())){ + num = (num << 3) + (num << 1) + (c & 15); + } + val.emplace(num); + if(c==EOF || c=='\n') + break; + } + if(c==')'){ + while(op.top() != '('){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.pop(); + } + else{ + while(!op.empty() && op.top() != '(' && ord[op.top()] >= ord[c]){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + op.emplace(c); + } + c=getchar(); + } + while(!op.empty()){ + val.emplace((1ll<<62) | op.top()); + op.pop(); + } + while(!val.empty()){ + if(val.front() & (1ll << 62)){ + ll a = op.top(); + op.pop(); + switch(val.front() & (~(1ll<<62))){ + case '+': + op.top() += a; + break; + case '-': + op.top() -= a; + break; + case '*': + op.top() *= a; + break; + case '/': + op.top() /= a; + break; + default : + return 1; + } + }else{ + op.emplace(val.front()); + } + val.pop(); + } + printf("%lld",op.top()); + } +} diff --git a/eval/stack/b10616b9-a205-417c-9592-d7ea189e0bcc.cpp b/eval/stack/b10616b9-a205-417c-9592-d7ea189e0bcc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9a8ef322718b5bbc2c0304101fb83a5dc1a312 --- /dev/null +++ b/eval/stack/b10616b9-a205-417c-9592-d7ea189e0bcc.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +int opPriority(char c) +{ + if (c == '(' || c == ')') return -1; + if (c == '+' || c == '-') return 1; + if (c == '*' || c == '/') return 2; + return 3; +} + +bool isDigit(char c) +{ + return '0' <= c && c <= '9'; +} + +class ExpressionEvaluator{ + public: + int Evaluate(string expr) + { + for (int i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (c == '(') { + op_.push(c); + priority_.push(opPriority(c)); + continue; + } + if (isDigit(c)) { + int number = c - '0'; + while (i+1 < expr.size() && isDigit(expr[i + 1])) { + ++i; + number = number * 10 + (expr[i] - '0'); + } + numbers_.push(number); + continue; + } + Pop_(opPriority(c)); + if (c != ')') { + priority_.push(opPriority(c)); + op_.push(c); + } + } + int result = numbers_.top(); + numbers_.pop(); + return result; + } + private: + void Pop_(int p) + { + while (!priority_.empty() && priority_.top() >= p) { + char op = op_.top(); + op_.pop(); + priority_.pop(); + + if (op == '(') + return; + + int b = numbers_.top(); + numbers_.pop(); + int a = numbers_.top(); + numbers_.pop(); + + if (op == '+') numbers_.push(a + b); + else if (op == '-') numbers_.push(a - b); + else if (op == '*') numbers_.push(a * b); + else if (op == '/') numbers_.push(a / b); + } + } + + stack numbers_; + stack priority_; + stack op_; +}; + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string s; + fin >> s; + + ExpressionEvaluator exprEval; + fout << exprEval.Evaluate("(" + s + ")"); + + return 0; +} diff --git a/eval/stack/b113a343-d2a3-417c-84f3-aba169f727c9.cpp b/eval/stack/b113a343-d2a3-417c-84f3-aba169f727c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6c92c49dd5e4e21feaa7a8492c034984b7ccec7 --- /dev/null +++ b/eval/stack/b113a343-d2a3-417c-84f3-aba169f727c9.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char s[100005],semne[100005],semne1[100005]; + +int h,stiva[100005],sol[100005],k,h1; + +int main() + +{ + + f>>s; + + int nr=0; + + for(int i=0; s[i]; i++) + + { + + if(s[i]=='('||s[i]==')'||s[i]=='*'||s[i]=='+'||s[i]=='-'||s[i]=='/') + + { + + if(s[i-1]!=')'&&s[i]!='(') + + stiva[++h]=nr; + + nr=0; + + semne1[++k]=s[i]; + + if(s[i]=='+'||s[i]=='-') + + while(semne1[k-1]!='('&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]=='*'||s[i]=='/') + + while(semne1[k-1]!='('&&semne1[k-1]!='+'&&semne1[k-1]!='-'&&k>1) + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + else if(s[i]==')') + + { + + while(semne1[k-1]!='(') + + { + + semne[++h]=semne1[k-1]; + + semne1[k-1]=semne1[k]; + + k--; + + } + + k-=2; + + } + + } + + else + + nr=nr*10+s[i]-'0'; + + } + + if(s[strlen(s)-2]!=')'&&s[strlen(s)-1]!=')') + + stiva[++h]=nr; + + while(semne1[k]) + + semne[++h]=semne1[k],k--; + + for(int i=1; i<=h; i++) + + { + + if(semne[i]) + + { + + switch(semne[i]) + + { + + case '+': + + sol[h1-1]=sol[h1-1]+sol[h1]; + + break; + + case '-': + + sol[h1-1]=sol[h1-1]-sol[h1]; + + break; + + case '*': + + sol[h1-1]=sol[h1-1]*sol[h1]; + + break; + + case '/': + + sol[h1-1]=sol[h1-1]/sol[h1]; + + break; + + + + } + + h1--; + + } + + else + + sol[++h1]=stiva[i]; + + } + + g< +#include +#include +#include + + + + + + +using namespace std; + + + +string s; + +int i = 0; + +int res = 0; + +stack seq; + + + +int open_bracket = 1000000000 + int('('); + +int closed_bracket = 1000000000 + int(')'); + +int plus_sign = 1000000000 + int('+'); + +int minus_sign = 1000000000 + int('-'); + +int multiplication_sign = 1000000000 + int('*'); + +int division_sign = 1000000000 + int('/'); + + + +int sum_to_closed_bracket() + +{ + + int sum = 0; + + int nr; + + while (seq.top() != open_bracket) + + { + + nr = seq.top(); + + seq.pop(); + + if (seq.top() == plus_sign) + + { + + seq.pop(); + + sum += nr; + + } + + if (seq.top() == minus_sign) + + { + + seq.pop(); + + sum -= nr; + + nr = 0; + + } + + } + + return sum + nr; + +} + + + +void close_bracket() + +{ + + int sum = sum_to_closed_bracket(); + + + + seq.pop(); + + + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int multiplier = seq.top(); + + seq.pop(); + + sum = multiplier * sum; + + } + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int dividend = seq.top(); + + seq.pop(); + + sum = dividend / sum; + + } + + seq.push(sum); + +} + + + +void readNumber() + +{ + + int nr = 0; + + while (i < s.length() && s[i] >= '0' && s[i] <= '9') + + nr = nr * 10 + int(s[i++] - '0'); + + if (seq.top() == multiplication_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous * nr); + + } + + else + + { + + if (seq.top() == division_sign) + + { + + seq.pop(); + + int previous = seq.top(); + + seq.pop(); + + seq.push(previous / nr); + + } + + else + + seq.push(nr); + + } + + + + --i; + +} + + + +int main() { + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + cin >> s; + + seq.push(open_bracket); + + + + while (i < s.length()) + + { + + switch (s[i]) + + { + + case ')': + + close_bracket(); + + break; + + case '(': + + seq.push(open_bracket); + + break; + + case '+': + + seq.push(plus_sign); + + break; + + case '-': + + seq.push(minus_sign); + + break; + + case '*': + + seq.push(multiplication_sign); + + break; + + case '/': + + seq.push(division_sign); + + break; + + default: + + readNumber(); + + break; + + } + + ++i; + + } + + + + cout << sum_to_closed_bracket(); + + + + return 0; + +} diff --git a/eval/stack/b300b75b-c6e3-48c8-afe5-20c596a9941d.cpp b/eval/stack/b300b75b-c6e3-48c8-afe5-20c596a9941d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..290e57a054a4af160253b31da9d2ec30115d0e19 --- /dev/null +++ b/eval/stack/b300b75b-c6e3-48c8-afe5-20c596a9941d.cpp @@ -0,0 +1,275 @@ +#include +#include + + +using namespace std; + + + +int stNum[100000 +3],vfOp=0,vfNum=0; + +char v[100000 +3],op[100000 +3]; + + + +FILE *fin,*fout; + + + +int calc(int a,int b,char oper) + +{ + + if(oper=='+') + + { + + return a+b; + + } + + else if(oper=='-') + + { + + return a-b; + + } + + else if(oper=='*') + + { + + return a*b; + + } + + else if(oper=='/') + + { + + return a/b; + + } + +} + + + +int oper(char x) + +{ + + if(x=='+') + + { + + return 1; + + } + + else if(x=='-') + + { + + return 1; + + } + + else if(x=='*') + + { + + return 2; + + } + + else if(x=='/') + + { + + return 2; + + } + + return 0; + +} + + + +void afis() + +{ + + for(int i=1; i<=vfOp; i++) + + { + + fprintf(fout,"%c",op[i]); + + } + + fprintf(fout,"\n"); + + for(int i=1; i<=vfNum; i++) + + { + + fprintf(fout,"%d ",stNum[i]); + + } + + fprintf(fout,"\n\n"); + +} + + + +int main() + +{ + + fin=fopen("evaluare.in","r"); + + fout=fopen("evaluare.out","w"); + + + + fgets(v,100000 +3,fin); + + int n=strlen(v); + + n--; + + + + for(int i=0; i='0' && ch<='9') + + { + + + + int num=0; + + while(i='0' && v[i]<='9') + + { + + num=num*10+v[i]-'0'; + + i++; + + } + + i--; + + vfNum++; + + stNum[vfNum]=num; + + } + + else + + { + + + + + + while(vfOp>0 && oper(ch)<=oper(op[vfOp])) + + { + + int rez=calc(stNum[vfNum-1],stNum[vfNum],op[vfOp]); + + vfOp--; + + vfNum--; + + stNum[vfNum]=rez; + + } + + vfOp++; + + op[vfOp]=ch; + + } + + } + + + + } + + + + while(vfOp>0) + + { + + int rez=calc(stNum[vfNum-1],stNum[vfNum],op[vfOp]); + + vfOp--; + + vfNum--; + + stNum[vfNum]=rez; + + } + + fprintf(fout,"%d",stNum[1]); + + return 0; + +} diff --git a/eval/stack/b3ee1d65-8a70-4809-893e-7ee8b69eb921.cpp b/eval/stack/b3ee1d65-8a70-4809-893e-7ee8b69eb921.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7f19e4a1a2bc51e0a93ff86e733789c382e6c18 --- /dev/null +++ b/eval/stack/b3ee1d65-8a70-4809-893e-7ee8b69eb921.cpp @@ -0,0 +1,246 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +vector op; + +vector numbers; + +string fin; + +void postfix() + +{ + + int n = strlen(a); + + for (int i = 0; i < n; ++i) + + { + + int nr = 0; + + bool number = false; + + while (i < n && a[i] >= '0' && a[i] <= '9') + + { + + number = true; + + nr = nr * 10 + a[i] - '0'; + + i++; + + } + + if (number) + + { + + fin += to_string(nr) + ' '; + + i--; + + continue; + + } + + if (a[i] == '*' || a[i] == '/') + + { + + while (op.size() != 0 && op[op.size() - 1] != '(' && op[op.size() - 1] != '+' && op[op.size() - 1] != '-') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + op.push_back(a[i]); + + } + + else if (a[i] == '+' || a[i] == '-') + + { + + while (op.size() != 0 && op[op.size() - 1] != '(') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + op.push_back(a[i]); + + } + + else if (a[i] == '(') + + { + + op.push_back(a[i]); + + } + + else + + { + + while (op.size() != 0 && op[op.size() - 1] != '(') + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + + if(op.size() != 0) + + op.pop_back(); + + } + + } + + while (op.size() != 0) + + { + + fin += op[op.size() - 1]; + + op.pop_back(); + + } + +} + + + +int calculate(int a, int b, char op) + +{ + + switch (op) + + { + + case '+': + + return a + b; + + break; + + case '-': + + return a - b; + + break; + + case '*': + + return a * b; + + break; + + case '/': + + return a / b; + + break; + + } + +} + + + +int eval() + +{ + + for (int i = 0; i < fin.size(); ++i) + + { + + int nr = 0; + + bool number = false; + + while (fin[i] >= '0' && fin[i] <= '9') + + { + + nr = nr * 10 + fin[i] - '0'; + + i++; + + number = true; + + } + + if (number) + + { + + numbers.push_back(nr); + + } + + else if (fin[i] != ' ') + + { + + int val = calculate(numbers[numbers.size() - 2], numbers[numbers.size() - 1], fin[i]); + + numbers.pop_back(); + + numbers.pop_back(); + + numbers.push_back(val); + + } + + } + + return numbers[0]; + +} + + + +int main() + +{ + + f.getline(a, 100005); + + postfix(); + + g< +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char op) { + + switch(op) { + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + } + +} + + + +int main() { + + char s[100005], operation; + + int i, l, n1, n2; + + stack nr; + + stack op; + + + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + scanf("%s", s); + + l = strlen(s); + + + + for(i = 0; i < l; ++i) + + if(priority(s[i])) { + + while(!op.empty() && priority(s[i]) <= priority(op.top())) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.push(s[i]); + + } + + else if(isDigit(s[i])) { + + n1 = 0; + + while(isDigit(s[i]) && i < l) + + n1 = 10*n1 + s[i++] - '0'; + + --i; + + nr.push(n1); + + } + + else if(s[i] == '(') + + op.push('('); + + else if(s[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + op.pop(); + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + operation = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, operation)); + + } + + + + printf("%d", nr.top()); + +} diff --git a/eval/stack/b57cc704-0611-4400-af5d-aedfcd53ee41.cpp b/eval/stack/b57cc704-0611-4400-af5d-aedfcd53ee41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbf31c336072827ea478cb509c30c3589d449424 --- /dev/null +++ b/eval/stack/b57cc704-0611-4400-af5d-aedfcd53ee41.cpp @@ -0,0 +1,216 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +stack stac; + +bool isPriority(char a,char b) + +{ + + if((b=='+' || b=='-') && (a=='*' || a=='/')) + + return true; + + if((b=='+' || b=='-') && (a=='+' || a=='-')) + + return true; + + if((b=='*' || b=='/') && (a=='*' || a=='/')) + + return true; + + return false; + +} + +bool isDigit(char a) + +{ + + if(a>='0'&& a<='9') + + return true; + + return false; + +} + +string infixToPostFix(string a) + +{ + + string res; + + for(int i = 0; i< a.size(); i++) + + { + + if(isDigit(a[i])) + + { + + res+=a[i]; + + if(i!=a.size()-1) + + { + + if(!isDigit(a[i+1])) + + res+=" "; + + } + + else + + { + + res+=" "; + + } + + } + + else + + { + + if(a[i]==')') + + { + + while(stac.size() && stac.top()!='(') + + res+=stac.top(),stac.pop(); + + if(stac.size()) + + stac.pop(); + + } + + else + + { + + while(stac.size() && isPriority(stac.top(),a[i])) + + { + + res+=stac.top(); + + stac.pop(); + + } + + stac.push(a[i]); + + } + + } + + } + + while(stac.size()) + + res+=stac.top(),stac.pop(); + + return res; + +} + +stack numere; + +long long solvePostFix(string a) + +{ + + long long b=0; + + for(int i = 0;i>a; + + fout< + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +char s[100005]; +char op[50005]; +int nr[50005],optop,nrtop,pr[200]; + + +int calculate(int a,int b,char op) +{ + if(op=='+') return a+b; + else if(op=='-') return a-b; + else if(op=='*') return a*b; + else if(op=='/') return a/b; +} +int main() +{ + in>>s; + pr['+']=1; + pr['-']=1; + pr['*']=2; + pr['/']=2; + int lung=strlen(s); + for(int i=0;i='0' && s[i]<='9') + { + int number=0; + while(s[i]>='0' && s[i]<='9') + { + number=number*10+s[i]-'0'; + i++; + } + i--; + nr[++nrtop]=number; + } + else + { + while(optop>0 && pr[op[optop]]>=pr[s[i]]) + { + int ans=calculate(nr[nrtop-1],nr[nrtop],op[optop]); + optop--; + nrtop--; + nr[nrtop]=ans; + } + op[++optop]=s[i]; + } + } + while(optop>0) + { + int ans=calculate(nr[nrtop-1],nr[nrtop],op[optop]); + nrtop--; + optop--; + nr[nrtop]=ans; + } + out< +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char a[100005]; + +char semne[100005]; + +int nre[100005]; + +int top_semne, top_nre; + +int l; + + + +bool isDigit(char a) + +{ + + if(a >= '0' && a <= '9') + + return 1; + + return 0; + +} + + + +void push_semne(char semn) + +{ + + semne[++top_semne] = semn; + +} + + + +void push_nre(int nr) + +{ + + nre[++top_nre] = nr; + +} + + + +void formnr(int &i) + +{ + + int nr = 0; + + while(isDigit(a[i])) + + { + + nr = nr * 10 + (a[i] - '0'); + + ++i; + + } + + --i; + + push_nre(nr); + +} + + + +void calcul(char c) + +{ + + int x = nre[top_nre]; + + nre[top_nre --] = 0; + + int y = nre[top_nre]; + + nre[top_nre --] = 0; + + int rez; + + + + switch(c) + + { + + case '+': + + rez = y + x; + + break; + + case '-': + + rez = y - x; + + break; + + case '*': + + rez = y * x; + + break; + + case '/': + + rez = y / x; + + break; + + } + + push_nre(rez); + + semne[top_semne--] = 0; + + + +} +int grad(char c) + +{ + + if(c == '+' || c == '-' || c == ')') + + return 1; + + if(c == '*' || c == '/') + + return 2; + + return 0; + +} +void eliber(char forWho) + +{ + + int grad_actual = grad(forWho); + + + + while(top_semne != 0 && grad_actual <= grad(semne[top_semne])) + + calcul(semne[top_semne]); + +} +void solve() + +{ + + for(int i = 0; i < l; ++i) + + { + + if(isDigit(a[i])) + + formnr(i); + + else if(a[i] == '(') + + push_semne(a[i]); + + else if(a[i] == ')') + + { + + eliber(')'); + + semne[top_semne --] = 0; + + } + + else{ + + if(top_semne != 0) + + eliber(a[i]); + + push_semne(a[i]); + + } + + + + } + + while(top_semne != 0) + + calcul(semne[top_semne]); + +} + + + + + +int main() + +{ + + f>>a; + + l = strlen(a); + + solve(); + + g< +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string s; + +int a[1001]; + +char o[1001]; + +int preced(char op) + +{ + + if(op=='+' or op=='-') + + return 1; + + if(op=='*' or op=='/') + + return 2; + + return 0; + +} + +int Calcul(int a,int b,char op) + +{ + + switch(op){case '+':return a+b; + + case '-':return a-b; + + case '*':return a*b; + + case '/':return a/b;} + +} + +int eval(string tokens){ + +int i;char op; + +stack values; + +stack ops; + +for(i=0;i=preced(tokens[i])) + + { + + int val2=values.top(); + + values.pop(); + + int val1=values.top(); + + values.pop(); + + char op=ops.top(); + + ops.pop(); + + values.push(Calcul(val1,val2,op)); + + } + + ops.push(tokens[i]); + + } + +} + +while(!ops.empty()) + +{ + + int val2=values.top(); + + values.pop(); + + int val1=values.top(); + + values.pop(); + + char op=ops.top(); + + ops.pop(); + + values.push(Calcul(val1,val2,op)); + +} + +return values.top(); + +} + +int main() + +{ + + getline(fin,s); + + fout << eval(s) << '\n'; + + return 0; + +} diff --git a/eval/stack/b7f2196d-e04e-4d8a-be63-f088610258f7.cpp b/eval/stack/b7f2196d-e04e-4d8a-be63-f088610258f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbe2fd8102e2cafe537a2357ab74c86678e76382 --- /dev/null +++ b/eval/stack/b7f2196d-e04e-4d8a-be63-f088610258f7.cpp @@ -0,0 +1,313 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 1e5; + + + +char sti[5 + N]; + +int sir[5 + N]; + +int v[5 + N]; + + + +bool is_operator(char ch) { + + return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); + +} + + + +int rang(char ch) { + + if(ch == '+' || ch == '-') return 1; + + return 2; + +} + + + +int operator_to_int(char ch) { + + if(ch == '+') return -1; + + if(ch == '-') return -2; + + if(ch == '*') return -3; + + if(ch == '/') return -4; + +} + + + +int main() { + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + char ch(' '), chprev(' '); + + int vf(0), nr(0); + + while(ch != '\n') { + + ch = getchar(); + + if(ch != '\n') { + + if(ch == '(') sti[++vf] = '('; + + else if(ch == ')') { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(') { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + vf--; + + nr = 0; + + } else if(is_operator(ch)) { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(' && rang(sti[vf]) >= rang(ch)) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + sti[++vf] = ch; + + nr = 0; + + } else + + nr = nr * 10 + ch - '0'; + + chprev = ch; + + } + + } + + if(nr > 0) sir[++sir[0]] = nr; + + nr = 0; + + while(vf > 0) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + + + vf = 0; + + for(int i = 1; i <= sir[0]; i++){ + + if(sir[i] < 0){ + + if(sir[i] == -1) + + v[vf - 1] += v[vf]; + + else if(sir[i] == -2) + + v[vf - 1] -= v[vf]; + + else if(sir[i] == -3) + + v[vf - 1] *= v[vf]; + + else if(sir[i] == -4) + + v[vf - 1] /= v[vf]; + + vf--; + + } + + else v[++vf] = sir[i]; + + } + + printf("%d\n", v[1]); + + return 0; + +} diff --git a/eval/stack/b946a0b3-605c-43cd-9572-a713a26a8e01.cpp b/eval/stack/b946a0b3-605c-43cd-9572-a713a26a8e01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed03f55dd6df1b7a47227ccce53727797705fb9 --- /dev/null +++ b/eval/stack/b946a0b3-605c-43cd-9572-a713a26a8e01.cpp @@ -0,0 +1,185 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + +stack nr; + +stack operatori; + +int lungime; + +char sir[100001]; + +int putere(char semn) + +{ + + if(semn == '+' || semn == '-') + + return 1; + + if(semn == '*' || semn == '/') + + return 2; + + return 0; + +} + +int compune_nr(int &indice) + +{ + + int nr = 0; + + while(indice +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +short Imp(char op) + +{ + + if (op == '+' || op == '-') + + return 1; + + if (op == '*' || op == '/') + + return 2; + + return 0; + +} + + + +int Operatie(int a, int b, char op) + +{ + + if (op == '+')return a + b; + + if (op == '-')return a - b; + + if (op == '*')return a * b; + + if (op == '/')return a / b; + +} + + + +int eval(string s) + +{ + + stack t; + + stackop; + + for (int i = 0; i < s.size(); i++) + + { + + + + if (s[i] == '(') + + op.push(s[i]); + + else if (isdigit(s[i])) + + { + + int val = 0; + + while (i < s.size() && isdigit(s[i])) { + + val = val * 10 + s[i] - '0'; + + i++; + + } + + t.push(val); + + i--; + + } + + else if (s[i] == ')') + + { + + while (!op.empty() && op.top() != '(') + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + if (!op.empty())op.pop(); + + } + + else + + { + + + + while (!op.empty() && Imp(op.top()) >= Imp(s[i])) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + } + + op.push(s[i]); + + } + + } + + + + while (!op.empty()) + + { + + int val1, val2; + + char ope; + + val2 = t.top(); + + t.pop(); + + val1 = t.top(); + + t.pop(); + + ope = op.top(); + + op.pop(); + + t.push(Operatie(val1, val2, ope)); + + + + } + + return t.top(); + +} + + + +int main() + +{ + + string s; + + fin >> s; + + fout << eval(s); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/b9e725ca-e1f5-45e4-a925-a955429cfee5.cpp b/eval/stack/b9e725ca-e1f5-45e4-a925-a955429cfee5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf03c73b22e7114c40f0abd4807a773794ca4916 --- /dev/null +++ b/eval/stack/b9e725ca-e1f5-45e4-a925-a955429cfee5.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + + + +const int NMAX = 100005; + +int n; + +char s[NMAX]; + +int st[NMAX], top; + + + +inline bool isDigit(char ch) + +{ + + return '0' <= ch && ch <= '9'; + +} + + + +int main() + +{ + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin >> (s + 1); + + n = strlen(s + 1); + + s[0] = '('; + + s[n + 1] = ')'; + + ++n; + + for (int i = 0;i <= n;++i) + + { + + if (s[i] == '+') + + continue; + + else if (s[i] == '-') + + st[++top] = 1000000002; + + else if (s[i] == '*') + + st[++top] = 1000000003; + + else if (s[i] == '/') + + st[++top] = 1000000004; + + else if (s[i] == '(') + + st[++top] = 1000000001; + + else if (s[i] == ')') + + { + + int x = 0; + + while (top > 0 && st[top] != 1000000001) + + x += st[top--]; + + --top; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + else + + { + + int x = 0; + + while (i <= n && isDigit(s[i])) + + x = x * 10 + (s[i++] - '0'); + + --i; + + if (top == 0) + + st[++top] = x; + + else if (st[top] == 1000000003) + + --top, st[top] *= x; + + else if (st[top] == 1000000004) + + --top, st[top] /= x; + + else if (st[top] == 1000000002) + + st[top] = -x; + + else + + st[++top] = x; + + } + + } + + fout << st[1] << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/eval/stack/baf6b7b0-3b5b-4eca-999f-f56b7d5e4217.cpp b/eval/stack/baf6b7b0-3b5b-4eca-999f-f56b7d5e4217.cpp new file mode 100644 index 0000000000000000000000000000000000000000..290e57a054a4af160253b31da9d2ec30115d0e19 --- /dev/null +++ b/eval/stack/baf6b7b0-3b5b-4eca-999f-f56b7d5e4217.cpp @@ -0,0 +1,275 @@ +#include +#include + + +using namespace std; + + + +int stNum[100000 +3],vfOp=0,vfNum=0; + +char v[100000 +3],op[100000 +3]; + + + +FILE *fin,*fout; + + + +int calc(int a,int b,char oper) + +{ + + if(oper=='+') + + { + + return a+b; + + } + + else if(oper=='-') + + { + + return a-b; + + } + + else if(oper=='*') + + { + + return a*b; + + } + + else if(oper=='/') + + { + + return a/b; + + } + +} + + + +int oper(char x) + +{ + + if(x=='+') + + { + + return 1; + + } + + else if(x=='-') + + { + + return 1; + + } + + else if(x=='*') + + { + + return 2; + + } + + else if(x=='/') + + { + + return 2; + + } + + return 0; + +} + + + +void afis() + +{ + + for(int i=1; i<=vfOp; i++) + + { + + fprintf(fout,"%c",op[i]); + + } + + fprintf(fout,"\n"); + + for(int i=1; i<=vfNum; i++) + + { + + fprintf(fout,"%d ",stNum[i]); + + } + + fprintf(fout,"\n\n"); + +} + + + +int main() + +{ + + fin=fopen("evaluare.in","r"); + + fout=fopen("evaluare.out","w"); + + + + fgets(v,100000 +3,fin); + + int n=strlen(v); + + n--; + + + + for(int i=0; i='0' && ch<='9') + + { + + + + int num=0; + + while(i='0' && v[i]<='9') + + { + + num=num*10+v[i]-'0'; + + i++; + + } + + i--; + + vfNum++; + + stNum[vfNum]=num; + + } + + else + + { + + + + + + while(vfOp>0 && oper(ch)<=oper(op[vfOp])) + + { + + int rez=calc(stNum[vfNum-1],stNum[vfNum],op[vfOp]); + + vfOp--; + + vfNum--; + + stNum[vfNum]=rez; + + } + + vfOp++; + + op[vfOp]=ch; + + } + + } + + + + } + + + + while(vfOp>0) + + { + + int rez=calc(stNum[vfNum-1],stNum[vfNum],op[vfOp]); + + vfOp--; + + vfNum--; + + stNum[vfNum]=rez; + + } + + fprintf(fout,"%d",stNum[1]); + + return 0; + +} diff --git a/eval/stack/bb129ebb-c62a-4a8e-a13f-e8dd97e579b2.cpp b/eval/stack/bb129ebb-c62a-4a8e-a13f-e8dd97e579b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..364122854339ddb348b8cc02148eed9c850acdba --- /dev/null +++ b/eval/stack/bb129ebb-c62a-4a8e-a13f-e8dd97e579b2.cpp @@ -0,0 +1,163 @@ +#include +#include + + +using namespace std; + + + +ifstream cin("evaluare.in"); + +ofstream cout("evaluare.out"); + + + +stack sign; + +stack num; + + + +char s[100001], t[150001]; + + + +inline int Precedence(char ch) + +{ if (ch=='*' || ch=='/') + + return 2; + + return 1; + +} + + + +int main() + +{ int j, i, x, y; + + cin>>s; + + cin.close(); + + j=-1; + + for (i=0;s[i];++i) + + if (isdigit(s[i])) + + { while (isdigit(s[i])) + + { t[++j]=s[i]; + + ++i; + + } + + t[++j]=' '; + + --i; + + } + + else if (s[i]=='(') + + sign.push('('); + + else if (s[i]==')') + + { while (sign.top()!='(') + + { t[++j]=sign.top(); + + sign.pop(); + + } + + sign.pop(); + + } + + else + + { while (!sign.empty() && sign.top()!='(' && Precedence(s[i])<=Precedence(sign.top())) + + { t[++j]=sign.top(); + + sign.pop(); + + } + + sign.push(s[i]); + + } + + while (!sign.empty()) + + { t[++j]=sign.top(); + + sign.pop(); + + } + + + + for (i=0;t[i];++i) + + if (isdigit(t[i])) + + { x=0; + + while (isdigit(t[i])) + + { x=x*10+t[i]-'0'; + + ++i; + + } + + num.push(x); + + --i; + + } + + else if (t[i]!=' ') + + { x=num.top(); + + num.pop(); + + y=num.top(); + + num.pop(); + + if (t[i]=='*') + + num.push(x*y); + + else if (t[i]=='/') + + num.push(y/x); + + else if (t[i]=='+') + + num.push(x+y); + + else + + num.push(y-x); + + } + + cout< +#include +#include + +using namespace std; +stack ch; +queue st; +int pr[300],hashh[300],f[10],v[100005]; +int main() +{ + ifstream cin("evaluare.in"); + ofstream cout("evaluare.out"); + string s; + cin>>s; + int n=s.size(); + pr['+']=1; + pr['-']=1; + pr['/']=2; + pr['*']=2; + hashh['+']=1000000001; + hashh['-']=1000000002; + hashh['/']=1000000003; + hashh['*']=1000000004; + hashh['(']=1000000006; + hashh[')']=1000000007; + f[1]='+'; + f[2]='-'; + f[3]='/'; + f[4]='*'; + f[6]='('; + f[7]=')'; + + int i; + + + for(i=0;i='0' and i=pr[s[i]] and ch.top()!='('){ + st.push(hashh[ch.top()]); + ch.pop(); + if(ch.empty()==true) + break; + } + ch.push(s[i]); + } + + } + + } + + + } + + int a; + while(ch.empty()==false){ + st.push(hashh[ch.top()]); + ch.pop(); + } + int cnt=0; + while(st.empty()==false){ + a=st.front(); + st.pop(); + if(a>=1000000000){ + + if(f[a-1000000000]=='+'){ + cnt--; + v[cnt]=v[cnt]+v[cnt+1]; + } + if(f[a-1000000000]=='-'){ + cnt--; + v[cnt]=v[cnt]-v[cnt+1]; + } + if(f[a-1000000000]=='*'){ + cnt--; + v[cnt]=v[cnt]*v[cnt+1]; + } + if(f[a-1000000000]=='/'){ + cnt--; + v[cnt]=v[cnt]/v[cnt+1]; + } + } + else{ + + v[++cnt]=a; + } + } + + cout< +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +struct stackchar + +{ + + char val[100000]; + + int varf; + +}; + + + +struct stackint + +{ + + int val[100000]; + + int varf; + +}; + + + +int priority(char op) + +{ + + if(op=='*' || op=='/') + + return 2; + + if(op=='+' || op=='-') + + return 1; + + return 0; + +} + + + +void postfix(char *exp, char *post) + +{ + + stackchar stiva; + + stiva.varf=-1; + + + + int j=0; + + + + for(int i=0;exp[i]!=0;) + + { + + if(exp[i]>='0' && exp[i]<='9') + + { + + while(exp[i]>='0' && exp[i]<='9') + + post[j++]=exp[i++]; + + post[j++]=' '; + + } + + + + if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') + + { + + if(stiva.varf==-1) + + stiva.val[++stiva.varf]=exp[i++]; + + else + + { + + while(priority(exp[i])<=priority(stiva.val[stiva.varf])) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[++stiva.varf]=exp[i++]; + + } + + } + + + + if(exp[i]=='(') + + { + + stiva.val[++stiva.varf]=exp[i++]; + + } + + if(exp[i]==')') + + { + + while(stiva.val[stiva.varf]!='(') + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + stiva.val[stiva.varf--]; + + ++i; + + } + + } + + + + while(stiva.varf!=-1) + + { + + post[j++]=stiva.val[stiva.varf--]; + + post[j++]=' '; + + } + + post[j]=0; + +} + + + +int evaluare(char *exp) + +{ + + stackint stiva; + + stiva.varf=-1; + + int nr; + + for(int j=0;exp[j]!=0;) + + { + + if(exp[j]==' ') + + ++j; + + if(exp[j]>='0' && exp[j]<='9') + + { + + nr=0; + + while(exp[j]>='0' && exp[j]<='9') + + nr=nr*10+(exp[j++]-'0'); + + stiva.val[++stiva.varf]=nr; + + } + + + + if(exp[j]=='+' || exp[j]=='/' || exp[j]=='*' || exp[j]=='-') + + { + + nr=stiva.val[stiva.varf--]; + + if(exp[j]=='+') + + stiva.val[stiva.varf]+=nr; + + if(exp[j]=='-') + + stiva.val[stiva.varf]-=nr; + + if(exp[j]=='*') + + stiva.val[stiva.varf]*=nr; + + if(exp[j]=='/') + + stiva.val[stiva.varf]/=nr; + + j++; + + } + + } + + return stiva.val[stiva.varf]; + +} + + + +char exp[100001], post[300001]; + +int rez; + + + +int main() + +{ + + fin.getline(exp, 100000); + + postfix(exp, post); + + rez=evaluare(post); + + fout< +#include +#include +#include +#include +using namespace std; + +const int NMAX=100005; + +int op[NMAX],polo[NMAX]; + +int top_op,top_polo; + +int pr(char ch) + +{ + + switch(ch) + + { + + case '+': return 1; + + case '-': return 1; + + case '*': return 2; + + case '/': return 2; + + default: return 0; + + } + +} + +void eval() + +{ + + if(op[top_op]=='+') + + polo[top_polo-1]=polo[top_polo-1]+polo[top_polo]; + + if(op[top_op]=='-') + + polo[top_polo-1]=polo[top_polo-1]-polo[top_polo]; + + if(op[top_op]=='*') + + polo[top_polo-1]=polo[top_polo-1]*polo[top_polo]; + + if(op[top_op]=='/') + + polo[top_polo-1]=polo[top_polo-1]/polo[top_polo]; + + top_op--; + + top_polo--; + +} + +int main() + +{ + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + int nr; + + char ch; + + scanf("%c",&ch); + + while(isdigit(ch) || ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='(' || ch==')') + + { + + if(isdigit(ch)) + + { + + nr=0; + + while(isdigit(ch)) + + { + + nr=nr*10+(ch-'0'); + + scanf("%c",&ch); + + } + + polo[++top_polo]=nr; + + } + + else + + { + + if(top_op==0 || ch=='(' || pr(op[top_op])=pr(ch)) + + eval(); + + op[++top_op]=ch; + + } + + } + + scanf("%c",&ch); + + } + + } + + while(top_op) + + eval(); + + printf("%d",polo[1]); + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/eval/stack/bede9556-b268-4e73-9e12-9d395ee3bf8e.cpp b/eval/stack/bede9556-b268-4e73-9e12-9d395ee3bf8e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb8f3e02e36c9f5f72e5cd58a25f65de004d29fc --- /dev/null +++ b/eval/stack/bede9556-b268-4e73-9e12-9d395ee3bf8e.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +using std::vector; +using std::string; + +std::ifstream fin("evaluare.in"); +std::ofstream fout("evaluare.out"); + +int main() { + string str; + fin >> str; str += '?'; + + char prev = '?'; + vector stVal; + vector stOpr; + + auto pop = [&]() { + int nr = stVal.back(); + stVal.pop_back(); + if (stOpr.back() == '+') stVal.back() += nr; else + if (stOpr.back() == '-') stVal.back() -= nr; else + if (stOpr.back() == '*') stVal.back() *= nr; else + if (stOpr.back() == '/') stVal.back() /= nr; + stOpr.pop_back(); + }; + + for (char chr : str) { + if ('0' <= chr && chr <= '9') { + if ('0' <= prev && prev <= '9') + stVal.back() = stVal.back() * 10 + chr - '0'; + else + stVal.push_back(chr - '0'); + } + else { + if ((('0' <= prev && prev <= '9') || prev == ')') && !stOpr.empty()) { + if (stOpr.back() == '*' || stOpr.back() == '/') + pop(); + if ((stOpr.back() == '+' || stOpr.back() == '-') && chr != '*' && chr != '/') + pop(); + } + if (chr == ')') + stOpr.pop_back(); + else + stOpr.push_back(chr); + } + prev = chr; + } + fout << stVal[0] << '\n'; + + fout.close(); + return 0; +} diff --git a/eval/stack/bf315b71-3c00-4f84-b93b-153867280491.cpp b/eval/stack/bf315b71-3c00-4f84-b93b-153867280491.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64b723c5c4b60e29341440bc5a8e6500486b6b10 --- /dev/null +++ b/eval/stack/bf315b71-3c00-4f84-b93b-153867280491.cpp @@ -0,0 +1,174 @@ +#include +typedef unsigned long long ul; + +typedef long long ll; + +using namespace std; + + + +bool is_op(char c){ + + return (c == '+' or c == '-' or c == '*' or c == '/'); + +} + + + +int priority(char c){ + + if (c == '+' or c == '-') + + return 1; + + if (c == '*' or c == '/') + + return 2; + + return -1; + +} + + + +void process(stack &stk, char op){ + + int r = stk.top(); + + stk.pop(); + + int l = stk.top(); + + stk.pop(); + + + + if (op == '+') + + stk.push(l + r); + + else if (op == '-') + + stk.push(l - r); + + else if (op == '*') + + stk.push(l * r); + + else if (op == '/') + + stk.push(l / r); + +} + + + +int evaluare(string s){ + + stack val; + + stack op; + + + + for (int i = 0; i < s.length(); i++){ + + if (s[i] == '(') + + op.push('('); + + + + else if (s[i] == ')'){ + + while (op.top() != '('){ + + process(val, op.top()); + + op.pop(); + + } + + op.pop(); + + } + + + + else if (is_op(s[i])){ + + char curr = s[i]; + + while(!op.empty() and priority(op.top()) >= priority(curr)){ + + process(val, op.top()); + + op.pop(); + + } + + op.push(curr); + + } + + + + else { + + int num = 0; + + while (i < s.length() and isdigit(s[i])) + + num = num * 10 + s[i++] - '0'; + + --i; + + val.push(num); + + } + + } + + + + while(!op.empty()){ + + process(val, op.top()); + + op.pop(); + + } + + + + int ans = val.top(); + + val.pop(); + + return ans; + +} + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + + + string s; + + cin >> s; + + cout << evaluare(s); + + + + return 0; + +} diff --git a/eval/stack/bf3ab856-2009-49ac-9f42-03316bd4df74.cpp b/eval/stack/bf3ab856-2009-49ac-9f42-03316bd4df74.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b73e364e81f9c1ad0fb6c8f1f886edfa923d79da --- /dev/null +++ b/eval/stack/bf3ab856-2009-49ac-9f42-03316bd4df74.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +using namespace std; + + + +string x; + +stack a; + +stack b; + +char pr[256]; + + + +int proces(int a,int b,char semn) + +{ + + if(semn=='+') return a+b; + + if(semn=='-') return a-b; + + if(semn=='*') return a*b; + + if(semn=='/') return a/b; + + return 0; + +} + + + +void f() + +{ + + char semn=a.top(); + + a.pop(); + + if(semn=='(') return; + + int c=b.top(); + + b.pop(); + + int d=b.top(); + + b.pop(); + + b.push(proces(d,c,semn)); + + return; + +} + +int main() + +{ + + pr['+']=pr['-']=0; + + pr['*']=pr['/']=1; + + pr['(']=pr[')']=3; + + freopen("evaluare.in","r", stdin); + + freopen("evaluare.out","w", stdout); + + getline(cin,x); + + for(unsigned int i=0;i0 && pr[x[i]]<=pr[a.top()] && a.top()!='(') + + { + + f(); + + } + + a.push(x[i]); + + + + } + + } + + } + + while(a.size()) f(); + + printf("%d",b.top()); + + + + return 0; + +} diff --git a/eval/stack/bf4c652d-625b-432c-92e2-8c75358c7cda.cpp b/eval/stack/bf4c652d-625b-432c-92e2-8c75358c7cda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fee79e8c1531b186ed0157173191765bc1f50a2 --- /dev/null +++ b/eval/stack/bf4c652d-625b-432c-92e2-8c75358c7cda.cpp @@ -0,0 +1,268 @@ +#include +#include +#include +using namespace std; + + + +struct operation{ + + char operation; + + int priority; + +}; + + + +int main(){ + + + + iostream::sync_with_stdio(false); + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + + + int result = 0, currentOperandPriority, currentExpressionPriority = 0, currentNumber = 0; + + operation currentOp; + + string input; + + + + getline(fin, input); + + + + stack stackNumbers; + + stack stackDigits; + + stack stackOperators; + + + + bool newNumber; + + + + for(int i = input.length() - 1; i >= 0; i--) + + + + if(input[i] <= '9' && input[i] >= '0') + + stackDigits.push(input[i]); + + + + else{ + + + + currentNumber = 0; + + newNumber = false; + + + + while(!stackDigits.empty()){ + + newNumber = true; + + currentNumber = currentNumber*10 + stackDigits.top() - '0'; + + stackDigits.pop(); + + } + + if(newNumber) + + stackNumbers.push(currentNumber); + + + + + + if(input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '+'){ + + + + if(input[i] == '*' || input[i] == '/') + + currentOperandPriority = currentExpressionPriority + 1; + + else + + currentOperandPriority = currentExpressionPriority; + + + + if(stackOperators.empty() || stackOperators.top().priority <= currentOperandPriority){ + + + + currentOp.operation = input[i]; + + currentOp.priority = currentOperandPriority; + + stackOperators.push(currentOp); + + + + } else { + + while(!stackOperators.empty() && (currentOp = stackOperators.top()).priority > currentOperandPriority) { + + + + stackOperators.pop(); + + + + int operand1 = stackNumbers.top(); + + stackNumbers.pop(); + + int operand2 = stackNumbers.top(); + + stackNumbers.pop(); + + + + int res; + + + + if(currentOp.operation == '+') + + res = operand1 + operand2; + + else if (currentOp.operation == '-') + + res = operand1 - operand2; + + else if (currentOp.operation == '/') + + res = operand1 / operand2; + + else if (currentOp.operation == '*') + + res = operand1 * operand2; + + + + stackNumbers.push(res); + + } + + + + currentOp.operation = input[i]; + + currentOp.priority = currentOperandPriority; + + stackOperators.push(currentOp); + + } + + } + + + + else if(input[i] == '('){ + + currentExpressionPriority -= 2; + + } + + + + else if(input[i] == ')'){ + + currentExpressionPriority += 2; + + } + + + + } + + + + + + currentNumber = 0; + + newNumber = false; + + + + while(!stackDigits.empty()){ + + newNumber = true; + + currentNumber = currentNumber*10 + stackDigits.top() - '0'; + + stackDigits.pop(); + + } + + if(newNumber) + + stackNumbers.push(currentNumber); + + + + while(!stackOperators.empty()){ + + currentOp = stackOperators.top(); + + stackOperators.pop(); + + int operand1 = stackNumbers.top(); + + stackNumbers.pop(); + + int operand2 = stackNumbers.top(); + + stackNumbers.pop(); + + + + int res; + + + + if(currentOp.operation == '+') + + res = operand1 + operand2; + + else if (currentOp.operation == '-') + + res = operand1 - operand2; + + else if (currentOp.operation == '/') + + res = operand1 / operand2; + + else if (currentOp.operation == '*') + + res = operand1 * operand2; + + + + stackNumbers.push(res); + + } + + + + fout< +#include +#include +#include +using namespace std; + + + +string x; + +stack a; + +stack b; + +char pr[256]; + + + +int proces(int a,int b,char semn) + +{ + + if(semn=='+') return a+b; + + if(semn=='-') return a-b; + + if(semn=='*') return a*b; + + if(semn=='/') return a/b; + + return 0; + +} + + + +void f() + +{ + + char semn=a.top(); + + a.pop(); + + if(semn=='(') return; + + int c=b.top(); + + b.pop(); + + int d=b.top(); + + b.pop(); + + b.push(proces(d,c,semn)); + + return; + +} + +int main() + +{ + + pr['+']=pr['-']=0; + + pr['*']=pr['/']=1; + + pr['(']=pr[')']=3; + + freopen("evaluare.in","r", stdin); + + freopen("evaluare.out","w", stdout); + + getline(cin,x); + + for(unsigned int i=0;i0 && pr[x[i]]<=pr[a.top()] && a.top()!='(') + + { + + f(); + + } + + a.push(x[i]); + + + + } + + } + + } + + while(a.size()) f(); + + printf("%d",b.top()); + + + + return 0; + +} diff --git a/eval/stack/c2061fc4-15fd-4d10-82e9-f8404ab8409d.cpp b/eval/stack/c2061fc4-15fd-4d10-82e9-f8404ab8409d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7118867fbf4f6eb9fcef016092c8da15497b7919 --- /dev/null +++ b/eval/stack/c2061fc4-15fd-4d10-82e9-f8404ab8409d.cpp @@ -0,0 +1,249 @@ +#include +#include +#include +#include +using namespace std; + +stack ch; + +queue st; + +int pr[300],hashh[300],f[10],v[100005]; + +int main() + +{ + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + string s; + + cin>>s; + + int n=s.size(); + + pr['+']=1; + + pr['-']=1; + + pr['/']=2; + + pr['*']=2; + + hashh['+']=1000000001; + + hashh['-']=1000000002; + + hashh['/']=1000000003; + + hashh['*']=1000000004; + + hashh['(']=1000000006; + + hashh[')']=1000000007; + + f[1]='+'; + + f[2]='-'; + + f[3]='/'; + + f[4]='*'; + + f[6]='('; + + f[7]=')'; + + + + int i; + + + + + + for(i=0;i='0' and i=pr[s[i]] and ch.top()!='('){ + + st.push(hashh[ch.top()]); + + ch.pop(); + + if(ch.empty()==true) + + break; + + } + + ch.push(s[i]); + + } + + + + } + + + + } + + + + + + } + + + + int a; + + while(ch.empty()==false){ + + st.push(hashh[ch.top()]); + + ch.pop(); + + } + + int cnt=0; + + while(st.empty()==false){ + + a=st.front(); + + st.pop(); + + if(a>=1000000000){ + + + + if(f[a-1000000000]=='+'){ + + cnt--; + + v[cnt]=v[cnt]+v[cnt+1]; + + } + + if(f[a-1000000000]=='-'){ + + cnt--; + + v[cnt]=v[cnt]-v[cnt+1]; + + } + + if(f[a-1000000000]=='*'){ + + cnt--; + + v[cnt]=v[cnt]*v[cnt+1]; + + } + + if(f[a-1000000000]=='/'){ + + cnt--; + + v[cnt]=v[cnt]/v[cnt+1]; + + } + + } + + else{ + + + + v[++cnt]=a; + + } + + } + + + + cout< + + +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +string s; + +struct Node + +{ + + string val; + + Node *left, *right; + +} *root; + +bool priority (Node* n1, Node* n2) + +{ + + string val1 = n1 -> val; + + string val2 = n2 -> val; + + if (val2 == "(" || val1 == "(") + + return true; + + if ((val1 == "+" || val1 == "-") && (val2 == "*" || val2 == "/")) + + return true; + + return false; + +} + +void adjust (stack &op, stack &num) + +{ + + Node* n2 = op.top (); + + Node *nr1, *nr2; + + nr1 = num.top (); + + num.pop (); + + nr2 = num.top (); + + num.pop (); + + n2 -> left = nr1; + + n2 -> right = nr2; + + num.push (n2); + + op.pop (); + +} + +Node* postfix_tree (string s) + +{ + + stack op; + + stack num; + + int i = 0; + + int length = s.size (); + + while (i < length) + + { + + if (s[i] == ' ' || s[i] == '\t') + + i++; + + if (isdigit (s[i])) + + { + + string number; + + while (isdigit (s[i]) && i < length) + + { + + number += s[i]; + + i++; + + } + + Node* n1 = new Node; + + n1 -> val = number; + + n1 -> left = NULL; + + n1 -> right = NULL; + + num.push (n1); + + } + + else + + { + + Node* n1 = new Node; + + n1 -> val += s[i]; + + n1 -> left = NULL; + + n1 -> right = NULL; + + if (op.empty ()) + + op.push (n1); + + else + + { + + if (s[i] == ')') + + { + + while (op.top () -> val != "(") + + adjust (op, num); + + op.pop (); + + } + + else if (priority (op.top (), n1)) + + op.push (n1); + + else + + { + + while ((!op.empty ()) ? (!priority(op.top (), n1)) : 0) + + adjust (op, num); + + op.push (n1); + + } + + } + + i++; + + } + + } + + while (!op.empty ()) + + adjust (op, num); + + return num.top (); + +} + +int evaluate (Node* root) + +{ + + if (root) + + if (root -> val == "+") + + return evaluate (root -> right) + evaluate (root -> left); + + else if (root -> val == "-") + + return evaluate (root -> right) - evaluate (root -> left); + + else if (root -> val == "*") + + return evaluate (root -> right) * evaluate (root -> left); + + else if (root -> val == "/") + + return evaluate (root -> right) / evaluate (root -> left); + + else + + { + + int nr = 0; + + int n = root -> val.size (); + + int i = 0; + + for (int i = 0; i < n; i++) + + nr = nr * 10 + root -> val[i] - '0'; + + return nr; + + } + +} + +void print (Node* root) + +{ + + if (root) + + { + + cout << root -> val << endl; + + print (root -> left); + + print (root -> right); + + } + +} + +int main () + +{ + + getline (fin, s); + + root = postfix_tree (s); + + fout << evaluate (root); + +} diff --git a/eval/stack/c2ebf5f9-a7cf-481e-948b-8298d1bf9cc0.cpp b/eval/stack/c2ebf5f9-a7cf-481e-948b-8298d1bf9cc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b82b721483551445ef7a7748e8903fc4acf6eda --- /dev/null +++ b/eval/stack/c2ebf5f9-a7cf-481e-948b-8298d1bf9cc0.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + +using namespace std; + +bool is_operator(char to_check) +{ + return to_check =='+'||to_check=='-'||to_check=='/'||to_check=='*'||to_check=='^'; +} + +int apply_operator(int a,int b,char op) +{ + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + case '^': + return a^b; + + + } + + +} + + +int get_precedence(char to_get) +{ + + + switch(to_get) + { + case '^': + return 3; + case '*': + return 2; + case '/': + return 2; + case '+': + return 1; + case '-': + return 1; + } + + return 0; +} + +bool is_character(char to_check) +{ + + return to_check>='0' &&to_check<='9'; + +} + +int get_next_number(const std::string&s,unsigned&cursor) +{ + int to_return=0; + + + while(is_character(s[cursor])) + { + to_return=to_return*10+(s[cursor++]-'0'); + } + cursor--; + + return to_return; +} + + +string get_infix(const std::string&postfix) +{ + stack operators; + string to_return; + + for(unsigned i=0; i values; + + for(unsigned i=0; i>to_eval; + + string infix=get_infix(to_eval); + + out< +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 1e5; + + + +char sti[5 + N]; + +int sir[5 + N]; + +int v[5 + N]; + + + +bool is_operator(char ch) { + + return (ch == '+' || ch == '-' || ch == '*' || ch == '/'); + +} + + + +int rang(char ch) { + + if(ch == '+' || ch == '-') return 1; + + return 2; + +} + + + +int operator_to_int(char ch) { + + if(ch == '+') return -1; + + if(ch == '-') return -2; + + if(ch == '*') return -3; + + if(ch == '/') return -4; + +} + + + +int main() { + + freopen("evaluare.in","r",stdin); + + freopen("evaluare.out","w",stdout); + + char ch(' '), chprev(' '); + + int vf(0), nr(0); + + while(ch != '\n') { + + ch = getchar(); + + if(ch != '\n') { + + if(ch == '(') sti[++vf] = '('; + + else if(ch == ')') { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(') { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + vf--; + + nr = 0; + + } else if(is_operator(ch)) { + + if(chprev >= '0' && chprev <= '9') sir[++sir[0]] = nr; + + while(vf > 0 && sti[vf] != '(' && rang(sti[vf]) >= rang(ch)) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + sti[++vf] = ch; + + nr = 0; + + } else + + nr = nr * 10 + ch - '0'; + + chprev = ch; + + } + + } + + if(nr > 0) sir[++sir[0]] = nr; + + nr = 0; + + while(vf > 0) { + + sir[++sir[0]] = operator_to_int(sti[vf]); + + vf--; + + } + + + + vf = 0; + + for(int i = 1; i <= sir[0]; i++){ + + if(sir[i] < 0){ + + if(sir[i] == -1) + + v[vf - 1] += v[vf]; + + else if(sir[i] == -2) + + v[vf - 1] -= v[vf]; + + else if(sir[i] == -3) + + v[vf - 1] *= v[vf]; + + else if(sir[i] == -4) + + v[vf - 1] /= v[vf]; + + vf--; + + } + + else v[++vf] = sir[i]; + + } + + printf("%d\n", v[1]); + + return 0; + +} diff --git a/eval/stack/c5ef47a0-1571-43e9-969f-f8257f9ca7a4.cpp b/eval/stack/c5ef47a0-1571-43e9-969f-f8257f9ca7a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3e39ce44094afbf9ba392cb5b4b03457ff719ba --- /dev/null +++ b/eval/stack/c5ef47a0-1571-43e9-969f-f8257f9ca7a4.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +using namespace std; + +ifstream in ("evaluare.in"); + +ofstream out ("evaluare.out"); + +string s; + +int num[100007],st[100007]; + +int pd=-1000000001,pi=-1000000002,plu=-1000000003,minu=-1000000004,inm=-1000000005,imp=-1000000006; + +int main() + +{ + + in>>s; + + int n=s.length(); + + int i=0,f=0,k=0; + + int nr=0; + + while(i=0) + + st[++k]=num[i]; + + else + + { + + if(num[i]==inm) + + { + + if(k<2) + + st[k]=0; + + else + + { + + st[--k]*=st[k+1]; + + } + + } + + else if(num[i]==imp) + + { + + st[--k]/=st[k+1]; + + } + + else if(num[i]==plu) + + { + + st[--k]+=st[k+1]; + + } + + else if(num[i]==minu) + + { + + st[--k]-=st[k+1]; + + } + + } + + } + + out< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< +#include +#include + + +using namespace std; + + + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +char s[100005]; + +int aux[100005]; + + + +void Precalc () + +{ + + stack < int > stiva; + + for (int i=0; s[i]; i++) + + { + + if (s[i] == '(') + + stiva.push(i); + + + + if (s[i] == ')') + + { + + aux[stiva.top()] = i; + + stiva.pop(); + + } + + } + +} + + + +int Solve (int st, int dr) + +{ + + int numar, k, j, v[100005], rez = 0; + + char semn = '+'; + + numar = k = 0; + + for (int i=st; i<=dr; i++) + + { + + if (s[i] == '+' || s[i] == '-' || s[i] == '/' || s[i] == '*') + + semn = s[i]; + + + + if (s[i] >= '0' && s[i] <= '9') + + { + + numar = 0, j = i; + + while (s[j] >= '0' && s[j] <= '9') + + { + + numar = numar * 10 + s[j] - '0'; + + j ++; + + } + + i = j-1; + + + + if (semn == '+') + + k ++, v[k] = numar; + + else if (semn == '-') + + k ++, v[k] = -numar; + + else if (semn == '*') + + v[k] = v[k] * numar; + + else if (semn == '/') + + v[k] = v[k] / numar; + + } + + + + if (s[i] == '(') + + { + + numar = Solve(i+1, aux[i]-1); + + if (semn == '+') + + k ++, v[k] = numar; + + else if (semn == '-') + + k ++, v[k] = -numar; + + else if (semn == '*') + + v[k] = v[k] * numar; + + else if (semn == '/') + + v[k] = v[k] / numar; + + + + i = aux[i]; + + } + + } + + for (int i=1; i<=k; i++) + + rez += v[i]; + + return rez; + +} + + + +int main() + +{ + + f >> s; + + Precalc(); + + g << Solve(0, strlen(s)-1); + + return 0; + +} diff --git a/eval/stack/c860c50a-21de-4390-8966-bca8fb02eb1e.cpp b/eval/stack/c860c50a-21de-4390-8966-bca8fb02eb1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7118867fbf4f6eb9fcef016092c8da15497b7919 --- /dev/null +++ b/eval/stack/c860c50a-21de-4390-8966-bca8fb02eb1e.cpp @@ -0,0 +1,249 @@ +#include +#include +#include +#include +using namespace std; + +stack ch; + +queue st; + +int pr[300],hashh[300],f[10],v[100005]; + +int main() + +{ + + ifstream cin("evaluare.in"); + + ofstream cout("evaluare.out"); + + string s; + + cin>>s; + + int n=s.size(); + + pr['+']=1; + + pr['-']=1; + + pr['/']=2; + + pr['*']=2; + + hashh['+']=1000000001; + + hashh['-']=1000000002; + + hashh['/']=1000000003; + + hashh['*']=1000000004; + + hashh['(']=1000000006; + + hashh[')']=1000000007; + + f[1]='+'; + + f[2]='-'; + + f[3]='/'; + + f[4]='*'; + + f[6]='('; + + f[7]=')'; + + + + int i; + + + + + + for(i=0;i='0' and i=pr[s[i]] and ch.top()!='('){ + + st.push(hashh[ch.top()]); + + ch.pop(); + + if(ch.empty()==true) + + break; + + } + + ch.push(s[i]); + + } + + + + } + + + + } + + + + + + } + + + + int a; + + while(ch.empty()==false){ + + st.push(hashh[ch.top()]); + + ch.pop(); + + } + + int cnt=0; + + while(st.empty()==false){ + + a=st.front(); + + st.pop(); + + if(a>=1000000000){ + + + + if(f[a-1000000000]=='+'){ + + cnt--; + + v[cnt]=v[cnt]+v[cnt+1]; + + } + + if(f[a-1000000000]=='-'){ + + cnt--; + + v[cnt]=v[cnt]-v[cnt+1]; + + } + + if(f[a-1000000000]=='*'){ + + cnt--; + + v[cnt]=v[cnt]*v[cnt+1]; + + } + + if(f[a-1000000000]=='/'){ + + cnt--; + + v[cnt]=v[cnt]/v[cnt+1]; + + } + + } + + else{ + + + + v[++cnt]=a; + + } + + } + + + + cout< +#include +#include +#include +std::string expr; + +int pos = 0; + +bool issign(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +int eval() { + + std::vector stack; + + std::string number; + + char prev_sign = '+'; + int value; + + if (expr[pos] == '-') { + ++pos; + prev_sign = '-'; + } + + for (; pos < expr.size(); ++pos) { + + if (isdigit(expr[pos])) { + number += expr[pos]; + + } else if (issign(expr[pos])) { + + if (!number.empty()) + value = std::stoi(number); + + number.clear(); + + if (prev_sign == '*') { + + stack.back() *= value; + + } else if (prev_sign == '/') { + + stack.back() /= value; + + } else if (prev_sign == '+') { + stack.push_back(value); + + } else if (prev_sign == '-') { + stack.push_back(-value); + + } + + prev_sign = expr[pos]; + } else if (expr[pos] == '(') { + + ++pos; + value = eval(); + + } else if (expr[pos] == ')') { + + + if (!number.empty()) + value = std::stoi(number); + number.clear(); + + if (prev_sign == '*') { + + stack.back() *= value; + + } else if (prev_sign == '/') { + + stack.back() /= value; + + } else if (prev_sign == '+') { + stack.push_back(value); + + } else if (prev_sign == '-') { + stack.push_back(-value); + + } + + break; + } + } + + int ans = 0; + for (int el : stack) + ans += el; + + + return ans; +} + +void read() { + std::ifstream in("evaluare.in"); + + std::getline(in, expr); + expr += '+'; + + in.close(); +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << eval() << '\n'; + + out.close(); + return 0; +} diff --git a/eval/stack/cb25d268-7787-4a73-94a7-634aac9eda1d.cpp b/eval/stack/cb25d268-7787-4a73-94a7-634aac9eda1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebeb445dbbaf29acfb34460bfc7302851ace0638 --- /dev/null +++ b/eval/stack/cb25d268-7787-4a73-94a7-634aac9eda1d.cpp @@ -0,0 +1,163 @@ +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + + +string to_eval; + +bool is_operator(char x) +{ + + return x=='+'||x=='-'||x=='*'||x=='/'; + +} + + + +bool is_digit(char a) +{ + + return a>='0' &&a<='9'; +} + +int apply(int a,int b,char op) +{ + + switch(op) + { + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + default: + return -1; + } + + +} + + +int get_int_at(string&r,int&index) +{ + int result=0; + + while(is_digit(r[index])) + { + result=result*10+(r[index]-'0'); + index++; + } + index--; + return result; +} + + + +int operator_order(char x) +{ + switch(x) + { + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + default: + return -1; + } + +} + +string get_polish_form(string expression) +{ + stack st; + string to_return; + + for(int i=0; i=operator_order(expression[i])) + { + to_return+=st.top(); + st.pop(); + } + st.push(expression[i]); + } + else if(expression[i]==')') + { + while(st.size() && st.top()!='(') + { + to_return+=st.top(); + st.pop(); + } + if(st.top()=='(') + st.pop(); + } + + } + while(st.size()) + { + to_return+=st.top(); + st.pop(); + } + + + return to_return; +} + +int evaluate_polish_form(string polish) +{ + stackprocessed; + for(int i=0; i>to_eval; + string polish=get_polish_form(to_eval); + out< +#include + + +using namespace std; + + + +stack polo; + +stack op; + + + +int priority(char ch) + +{ + + if (ch=='+'||ch=='-') + + return 1; + + if (ch=='*'||ch=='/') + + return 2; + + return 0; + +} + + + +void move_op() + +{ + + int x,y; + + char semn; + + semn=op.top(); + + op.pop(); + + y=polo.top(); + + polo.pop(); + + x=polo.top(); + + polo.pop(); + + if (semn=='+') + + polo.push(x+y); + + else if (semn=='-') + + polo.push(x-y); + + else if (semn=='*') + + polo.push(x*y); + + else + + polo.push(x/y); + +} + + + +int main() + +{ + + ifstream fin ("evaluare.in"); + + ofstream fout ("evaluare.out"); + + int nr; + + char ch; + + bool v=0; + + while (fin>>ch) + + { + + if ('0'<=ch&&ch<='9') + + if (v) + + nr=nr*10+(ch-'0'); + + else + + { + + v=1; + + nr=ch-'0'; + + } + + else + + { + + if (v) + + { + + polo.push(nr); + + v=0; + + nr=0; + + } + + if (ch=='(') + + op.push(ch); + + else if (ch==')') + + { + + while (op.top()!='(') + + move_op(); + + op.pop(); + + } + + else + + { + + while (!op.empty()&&priority(ch)<=priority(op.top())) + + move_op(); + + op.push(ch); + + } + + } + + } + + if (v) + + polo.push(nr); + + while (!op.empty()) + + move_op(); + + fout< +#include +#include +#include + +using namespace std; +ifstream f ("evaluare.in"); +ofstream g ("evaluare.out"); +char buff [5 + (int)1e6]; +short x [300]; +stack st , ans; +vector eval; +void solve (int semn){ + int b = ans . top (); + ans . pop (); + int a = ans . top (); + ans . pop (); + if (semn == -3) + ans . push (a * b); + if (semn == -4) + ans . push (a / b); + if (semn == -5) + ans . push (a + b); + if (semn == -6) + ans . push (a - b); + return; + +} +bool check (int x , int y){ + if (x >= y) + return true; + if (x == -6 && y == -5) + return true; + if (x == -4 && y == -3) + return true; + return false; +} +int main() +{ + x ['+'] = -5 , x ['-'] = -6 , x ['*'] = -3 , x ['/'] = -4 , x ['('] = -1 , x [')'] = -2; + f >> buff; + int a; + for(int i = 0 ; buff [i] ; i){ + if (isdigit (buff [i])){ + a = 0; + while (isdigit (buff [i])) + a = a * 10 + (buff [i ++] - '0'); + eval . push_back (a); + } + else{ + int val = x [buff [i]]; + if (val == -1) + st . push (val); + else + if (val == -2){ + while (0 < st . size () && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + if (0 < st . size () && st . top () == -1) + st . pop (); + } + else{ + while (0 < st . size () && check (st . top () , val) && st . top () != -1){ + eval . push_back (st . top ()); + st . pop (); + } + st . push (val); + } + ++ i; + } + } + while (0 < st . size ()) + eval . push_back (st . top ()) , st . pop (); + for(auto x : eval){ + if (x < 0) + solve (x); + else + ans . push (x); + } + g << ans . top (); + return 0; +} diff --git a/eval/stack/cf1af1c5-59af-4e21-abc4-1bbb157c3b61.cpp b/eval/stack/cf1af1c5-59af-4e21-abc4-1bbb157c3b61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..878a1ed593c5623dbc60af33776d75e7ea5c0e6d --- /dev/null +++ b/eval/stack/cf1af1c5-59af-4e21-abc4-1bbb157c3b61.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +void update(vector&num,char c) +{ + int b=num.back(); num.pop_back(); + int a=num.back(); num.pop_back(); + if(c=='+') num.push_back(a+b); + if(c=='-') num.push_back(a-b); + if(c=='*') num.push_back(a*b); + if(c=='/') num.push_back(a/b); +} + +int pr(char c) +{ + if(c=='(') return 0; else + if(c=='+' || c=='-') return 1; else + if(c=='*' || c=='/') return 2; +} + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + freopen("evaluare.in","r",stdin); + freopen("evaluare.out","w",stdout); + + vectornum,op; + seth={'+','-','/','*'}; + setnr={'0','1','2','3','4','5','6','7','8','9'}; + string s; + cin>>s; + for(int i=0;i=pr(s[i])) + update(num,op.back()),op.pop_back(); + op.push_back(s[i]); + } else + { + int val=0; + while(i +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char c) { + + switch(c) { + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + } + +} + + + +int main() { + + char expresie[100005], sign; + + int l, i, temp, n1, n2; + + stack nr; + + stack op; + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s", expresie); + + l = strlen(expresie); + + + + for(i = 0; i < l; ++i) { + + if(priority(expresie[i])) { + + while(!op.empty() && priority(expresie[i]) <= priority(op.top())) { + + sign = op.top(); + + op.pop(); + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.push(expresie[i]); + + + + } + + else if(isDigit(expresie[i])) { + + temp = 0; + + while(isDigit(expresie[i]) && i < l) + + temp = 10*temp + expresie[i++] - '0'; + + nr.push(temp); + + i--; + + } + + else if(expresie[i] == '(') + + op.push('('); + + else if(expresie[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.pop(); + + } + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + printf("%d", nr.top()); + +} diff --git a/eval/stack/d009e376-9001-4c21-9750-b67d3b393a33.cpp b/eval/stack/d009e376-9001-4c21-9750-b67d3b393a33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..814d8b54aecb4ee62e54ff768213c8bb90aade08 --- /dev/null +++ b/eval/stack/d009e376-9001-4c21-9750-b67d3b393a33.cpp @@ -0,0 +1,224 @@ +#include + + +using namespace std; + +ifstream f ("evaluare.in"); + +ofstream g ("evaluare.out"); + + + +char a[100005]; + +stack op; + +stack val; + + + +int valoare (char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 0; + + + +} + +int operatie (int x, int y, char op) + +{ + + if(op=='+') + + return x+y; + + if(op=='-') + + return x-y; + + if(op=='*') + + return x*y; + + if(op=='/') + + return x/y; + + + + + +} + + + +int evaluare (int lung ) + +{ + + + + for(int i=0; i=valoare(a[i])) + + { + + int val2 = val.top(); + + val.pop(); + + + + int val1 = val.top(); + + val.pop(); + + + + char ops = op.top(); + + op.pop(); + + + + val.push(operatie(val1, val2, ops)); + + } + + op.push(a[i]); + + } + + + + } + + while(!op.empty()) + + { + + int doi=val.top(); + + val.pop(); + + + + int prim=val.top(); + + val.pop(); + + + + char operation=op.top(); + + op.pop(); + + + + val.push(operatie(prim,doi,operation)); + + } + + + + return val.top(); + + + +} + +int main() + +{ + + f.getline(a,100001); + + int x=strlen(a); + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +struct type + +{ + + union + + { + + int i; + + char c; + + }; + + char t; + +}; + + + +queue v; + +stack op; + + + +int main() + +{ + + char c[100000]; + + f>>c; + + char *p=c; + + type e; + + while(*p!='\00') + + { + + if(*p>='0' && *p<='9') + + { + + int x=0; + + while(*p>='0' && *p<='9') + + x=x*10+*p-'0',++p; + + e.t='i'; + + e.i=x; + + v.push(e); + + } + + else + + { + + if(op.empty()) + + op.push(*p); + + else + + switch(*p) + + { + + case '*': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '/': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '+': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '-': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '(': + + op.push(*p); + + break; + + case ')': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.pop(); + + break; + + } + + ++p; + + } + + } + + while(!op.empty()) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + stack st; + + queue aux=v; + + while(!aux.empty()) + + { + + if(aux.front().t=='i') + + cout< + + + + + +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); +void citire(); +int vf = 0; +int prio(char x) +{ + if( x=='(' || x == ')') + return 0; + if( x=='+' || x=='-') + return 1; + + return 2; +} +int operatie ( int a, int b, char c ) +{ + + switch (c) + { + case '+': return a+b; + case '-': return a-b; + case '*': return a*b; + case '/': return a/b; + + } +} +int eval(char []); +char x[300001]; +int s[100001]; +void postfix(char []); +void rez(); +void afis(); +int main() +{ + fin >> x; + postfix(x); + + + fout<< eval(x); +} +void postfix(char x[]) +{ + int poz = 0; + int N =strlen (x); + char aux[300001] = {0}; + for ( int i = 0 ; i < N ; i++) + { + if(isdigit(x[i])) + { + aux[poz++] = x[i] ; + } + else + { + if(i>0 && isdigit(x[i-1])) + aux[poz++]=' '; + if(x[i]=='(') + s[vf++]=x[i]; + else + if(x[i]==')') + { + while(s[vf-1]!='(') + { + aux[poz++]=s[--vf]; + } + vf--; + } + else + { + while(vf != 0 && prio(x[i]) <= prio(s[vf-1])) + { + aux[poz++]=s[--vf]; + } + s[vf++] = x[i]; + } + } + + + } + if(isdigit(aux[poz-1])) aux[poz++]=' '; + while(vf!=0) + { + aux[poz++] = s[--vf]; + } + + strcpy(x,aux); + +} +int eval( char x[]) +{ + int N =strlen(x); + int numar =0; + for( int i=0 ;i < N ;i ++) + { + if(isdigit(x[i])) numar = numar*10 +(x[i]-'0'); + else + if (x[i]== ' ') { + s[vf++]=numar; + numar=0; + } + else + { + int nr2=s[--vf], nr1= s[--vf]; + s[vf++] = operatie ( nr1 ,nr2 , x[i]); + } + } + return s[vf-1]; +} diff --git a/eval/stack/d17aa0ed-e9f6-4368-be22-16a7431d1ef4.cpp b/eval/stack/d17aa0ed-e9f6-4368-be22-16a7431d1ef4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ffa00f8170d440dd6f2775d3feb83e9beeb0a93 --- /dev/null +++ b/eval/stack/d17aa0ed-e9f6-4368-be22-16a7431d1ef4.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include +#include +#include +#include +#include +using namespace std; + +string expression; + +bool isOperator(char ch) { + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; +} + +int getPriority(char ch) { + if (ch == '+' || ch == '-') { + return 1; + } else if (ch == '*' || ch == '/') { + return 2; + } + return -1; +} + +void applyOperator(stack& numbers, char op) { + int secondNo = numbers.top(); + numbers.pop(); + int firstNo = numbers.top(); + numbers.pop(); + + switch (op) { + case '+': { + numbers.push(firstNo + secondNo); + break; + } + case '-': { + numbers.push(firstNo - secondNo); + break; + } + case '*': { + numbers.push(firstNo * secondNo); + break; + } + case '/': { + numbers.push(firstNo / secondNo); + break; + } + } +} + +int main() { + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + + cin >> expression; + stack numbers; + stack operators; + + for (size_t i = 0; i < expression.size(); i++) { + if (expression[i] == '(') { + operators.push('('); + } else if (expression[i] == ')') { + while (operators.top() != '(') { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.pop(); + } else if (isOperator(expression[i])) { + while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + operators.push(expression[i]); + } else { + assert(isdigit(expression[i])); + int number = 0; + while (i < expression.size() && isdigit(expression[i])) { + number = number * 10 + expression[i++] - '0'; + } + i--; + numbers.push(number); + } + } + + while (!operators.empty()) { + applyOperator(numbers, operators.top()); + operators.pop(); + } + + cout << numbers.top() << "\n"; + return 0; +} diff --git a/eval/stack/d3437cab-5b69-4c1a-b870-f1f3868b361a.cpp b/eval/stack/d3437cab-5b69-4c1a-b870-f1f3868b361a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e10d01c6fb13ed4d6568ea97629642408ec00e1 --- /dev/null +++ b/eval/stack/d3437cab-5b69-4c1a-b870-f1f3868b361a.cpp @@ -0,0 +1,180 @@ +#include + + +using namespace std; + + + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +stack s; + +string str, expr; + + + +void opp (int & idx) + +{ + + while (str[idx] >= '0' && str[idx] <= '9') { + + expr += str[idx]; + + idx ++; + + } + + expr += ' '; + + idx --; + +} + + + +int priority (int i) + +{ + + if (str[i] == '*' || str[i] == '/') return 2; + + if (str[i] == '+' || str[i] == '-') return 1; + + return 0; + +} + + + +void formaPoloneza (int n) + +{ + + for (int i = 0; i < n; i ++) { + + if (str[i] >= '0' && str[i] <= '9') opp(i); + + else if (priority(i) != 0) { + + if (s.empty()) s.push(i); + + else { + + while (!s.empty() && priority(s.top()) >= priority(i)) { + + expr += str[s.top()], s.pop(); + + } + + s.push(i); + + } + + } + + else { + + if (str[i] == '(') s.push(i); + + else { + + while (!s.empty() && str[s.top()] != '(') expr += str[s.top()], s.pop(); + + expr += ' '; + + if (!s.empty() && str[s.top()] == '(') s.pop(); + + } + + } + + } + + while (!s.empty()) expr += str[s.top()], s.pop(); + +} + + + +int strToInt (int & idx) + +{ + + int nr = 0; + + while (expr[idx] <= '9' && expr[idx] >= '0') nr = nr * 10 + (expr[idx] - '0'), idx ++; + + idx --; + + return nr; + +} + + + +int expresie (int i, int o1, int o2) + +{ + + if (expr[i] == '+') return o1 + o2; + + else if (expr[i] == '-') return o1 - o2; + + else if (expr[i] == '/') return o1 / o2; + + else return o1 * o2; + +} + + + +int evaluare (int n) + +{ + + int o1, o2; + + while (!s.empty()) s.pop(); + + for (int i = 0; i < n; i ++) { + + if (expr[i] >= '0' && str[i] <= '9') s.push(strToInt(i)); + + else if (expr[i] != ' ' && s.size() >= 2) { + + o1 = s.top(), s.pop(); + + o2 = s.top(), s.pop(); + + s.push(expresie(i, o2, o1)); + + } + + } + + return s.top(); + +} + + + +int main() + +{ + + getline(fin, str); + + int n = str.size(); + + formaPoloneza(n); + + fout << evaluare(expr.size()); + + return 0; + +} diff --git a/eval/stack/d3eb4dbd-10dd-4adb-bb8e-254660139a6b.cpp b/eval/stack/d3eb4dbd-10dd-4adb-bb8e-254660139a6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10ab4220ecfa1cdb3273817eacdc54cf4590a02c --- /dev/null +++ b/eval/stack/d3eb4dbd-10dd-4adb-bb8e-254660139a6b.cpp @@ -0,0 +1,203 @@ +#include +#include +char c, semn[100001], st[10001]; + + + +int fp[100001]; + +long long steval[101]; + + + +using namespace std; + + + +int main() { + + int nr, gasitnr, n, i, k; + + FILE *fin, *fout; + + + + fin=fopen("evaluare.in", "r"); + + fout=fopen("evaluare.out", "w"); + + c=fgetc( fin ); + + nr=0; + + n=0; + + k=0; + + gasitnr=0; + + while(c!=EOF) { + + if(c>='0' && c<='9') { + + gasitnr=1; + + nr=nr*10+(c-48); + + } + + else { + + if(gasitnr) { + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + + + } + + if(c=='(' || c=='-' || c=='+' || c=='/' || c=='*' ) { + + k++; + + st[k]=c; + + if(st[k]=='+' || st[k]=='-') { + + while(st[k-1]=='+' || st[k-1]=='-' || st[k-1]=='*' || st[k-1]=='/') { + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + else if(st[k]=='*' || st[k]=='/') { + + while(st[k-1]=='*' || st[k-1]=='/') { + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + } + + else if(c==')') { + + while(st[k]!='(') { + + n++; + + semn[n]=st[k]; + + k--; + + } + + k--; + + } + + } + + c=fgetc( fin ); + + } + + + + if(gasitnr) { + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + while(k) { + + n++; + + semn[n]=st[k]; + + k--; + + } + + k=0; + + for(i=1; i<=n; i++) { + + if(semn[i]!='+' && semn[i]!='-' && semn[i]!='*' && semn[i]!='/') { + + k++; + + steval[k]=fp[i]; + + } + + else { + + if(semn[i]=='+') { + + steval[k-1]=steval[k-1]+steval[k]; + + } + + if(semn[i]=='-') { + + steval[k-1]=steval[k-1]-steval[k]; + + } + + if(semn[i]=='*') { + + steval[k-1]=steval[k-1]*steval[k]; + + } + + if(semn[i]=='/') { + + steval[k-1]=steval[k-1]/steval[k]; + + } + + k--; + + } + + } + + fprintf( fout, "%d", steval[1]); + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/eval/stack/d4ff5009-a9a9-4f19-94a4-f5c6e554e1bc.cpp b/eval/stack/d4ff5009-a9a9-4f19-94a4-f5c6e554e1bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04be66a707c08c47cb5e8dd6edb944a9caff8ce4 --- /dev/null +++ b/eval/stack/d4ff5009-a9a9-4f19-94a4-f5c6e554e1bc.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +int l, nr, i; + +string s; + +stack obj; + +stack numb; + + + +void calc_nr(); + +void test_nr(); + + + +int main() + +{ + + + + fin >> s; + + l = s.length(); + + + + for(i = 0; i < l; ++i){ + + if(s[i] >= '0' && s[i] <= '9'){ + + calc_nr(); + + test_nr(); + + } + + else if(s[i] == ')'){ + + nr = numb.top(); + + numb.pop(); + + while(obj.top() != '('){ + + nr += numb.top(); numb.pop(); + + obj.pop(); + + } + + obj.pop(); + + + + test_nr(); + + } + + else obj.push(s[i]); + + } + + + + nr = numb.top(); numb.pop(); + + while(obj.size()){ + + nr += numb.top(); numb.pop(); + + obj.pop(); + + } + + + + numb.push(nr); + + + + fout << numb.top(); + + + + return 0; + +} + + + +void calc_nr(){ + + nr = 0; + + while(i < l && s[i] >= '0' && s[i] <= '9'){ + + nr = nr * 10 + (s[i] - '0'); + + ++i; + + } + + + + --i; + +} + + + +void test_nr(){ + + + + if(obj.size()){ + + if(obj.top() == '*'){ + + int other = numb.top(); numb.pop(); + + obj.pop(); + + numb.push(nr * other); + + } + + else if(obj.top() == '/'){ + + int other = numb.top(); numb.pop(); + + obj.pop(); + + numb.push(other / nr); + + } + + else if(obj.top() == '-'){ + + obj.pop(); obj.push('+'); + + numb.push(-nr); + + } + + else numb.push(nr); + + } + + else numb.push(nr); + + + +} diff --git a/eval/stack/d5a6990c-28a0-4418-9c63-7dd2d4d1c7b5.cpp b/eval/stack/d5a6990c-28a0-4418-9c63-7dd2d4d1c7b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ffa00f8170d440dd6f2775d3feb83e9beeb0a93 --- /dev/null +++ b/eval/stack/d5a6990c-28a0-4418-9c63-7dd2d4d1c7b5.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +char s[100005], st1[50001]; + +int i,st2[50001],vf1,vf2, pr[256]; + +int calcul(int a, int b, char op){ + + switch(op){ + + case '+':return a+b; + + case '-': return a-b; + + case '*': return a*b; + + case '/':return a/b; + + } + +} + +int main() + +{ + + fin.getline(s,100001); + + pr['+']=pr['-']=1; + + pr['*']=pr['/']=2; + + for(i=0;s[i]!=0;i++) + + if(s[i]=='(') + + st1[++vf1]=s[i]; + + else + + if(s[i]==')'){ + + while(st1[vf1]!='('){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + vf1--; + + } + + else + + if(s[i]>='0'&&s[i]<='9'){ + + int nr=0; + + while(s[i]>='0'&&s[i]<='9'){ + + nr=nr*10+s[i]-'0'; + + i++; + + } + + i--; + + st2[++vf2]=nr; + + } + + else + + { + + while(vf1>0&&pr[st1[vf1]]>=pr[s[i]]){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + st1[++vf1]=s[i]; + + } + + while(vf1>0){ + + int rez=calcul(st2[vf2-1],st2[vf2],st1[vf1]); + + vf1--; + + vf2--; + + st2[vf2]=rez; + + } + + fout< +#include +#include + +using namespace std; + +ifstream f("evaluare.in"); +ofstream g("evaluare.out"); + +char s[100005]; +stack operatori; +stack operanzi; + +void evaluare_expresie() { + while (!operatori.empty() && operatori.top() != '(') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + char op = operatori.top(); + operatori.pop(); + switch (op) { + case '+': + operanzi.push(a + b); + break; + case '-': + operanzi.push(a - b); + break; + case '*': + operanzi.push(a * b); + break; + case '/': + operanzi.push(a / b); + break; + } + } +} + +int evaluare() { + int nrl = strlen(s); + for (int i = 0; i < nrl; ++i) { + if (s[i] == '(') + operatori.push('('); + else if (s[i] == ')') { + evaluare_expresie(); + operatori.pop(); + } else if (s[i] == '+' || s[i] == '-') { + evaluare_expresie(); + operatori.push(s[i]); + } else if (s[i] == '*' || s[i] == '/') { + if (!operatori.empty()) { + char inainte = operatori.top(); + if (inainte == '*' || inainte == '/') { + int b = operanzi.top(); + operanzi.pop(); + int a = operanzi.top(); + operanzi.pop(); + operatori.pop(); + if (inainte == '*') + operanzi.push(a * b); + else + operanzi.push(a / b); + } + } + operatori.push(s[i]); + } else { + int nr = 0, j; + for (j = i; j < nrl && isdigit(s[j]); ++j) + nr = nr * 10 + s[j] - '0'; + operanzi.push(nr); + i = j - 1; + } + } + evaluare_expresie(); + return operanzi.top(); +} + +int main() { + f.getline(s, 100005); + g << evaluare(); + return 0; +} diff --git a/eval/stack/d5e812d9-b43d-49e9-952b-c28b78678959.cpp b/eval/stack/d5e812d9-b43d-49e9-952b-c28b78678959.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e9a8ef322718b5bbc2c0304101fb83a5dc1a312 --- /dev/null +++ b/eval/stack/d5e812d9-b43d-49e9-952b-c28b78678959.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +int opPriority(char c) +{ + if (c == '(' || c == ')') return -1; + if (c == '+' || c == '-') return 1; + if (c == '*' || c == '/') return 2; + return 3; +} + +bool isDigit(char c) +{ + return '0' <= c && c <= '9'; +} + +class ExpressionEvaluator{ + public: + int Evaluate(string expr) + { + for (int i = 0; i < expr.size(); ++i) { + char c = expr[i]; + if (c == '(') { + op_.push(c); + priority_.push(opPriority(c)); + continue; + } + if (isDigit(c)) { + int number = c - '0'; + while (i+1 < expr.size() && isDigit(expr[i + 1])) { + ++i; + number = number * 10 + (expr[i] - '0'); + } + numbers_.push(number); + continue; + } + Pop_(opPriority(c)); + if (c != ')') { + priority_.push(opPriority(c)); + op_.push(c); + } + } + int result = numbers_.top(); + numbers_.pop(); + return result; + } + private: + void Pop_(int p) + { + while (!priority_.empty() && priority_.top() >= p) { + char op = op_.top(); + op_.pop(); + priority_.pop(); + + if (op == '(') + return; + + int b = numbers_.top(); + numbers_.pop(); + int a = numbers_.top(); + numbers_.pop(); + + if (op == '+') numbers_.push(a + b); + else if (op == '-') numbers_.push(a - b); + else if (op == '*') numbers_.push(a * b); + else if (op == '/') numbers_.push(a / b); + } + } + + stack numbers_; + stack priority_; + stack op_; +}; + +int main() +{ + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string s; + fin >> s; + + ExpressionEvaluator exprEval; + fout << exprEval.Evaluate("(" + s + ")"); + + return 0; +} diff --git a/eval/stack/d61dbe2c-49db-4d3e-98a4-5c3eee0d311c.cpp b/eval/stack/d61dbe2c-49db-4d3e-98a4-5c3eee0d311c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad25faddab6e77e25df08fad3cb1cfca19814e34 --- /dev/null +++ b/eval/stack/d61dbe2c-49db-4d3e-98a4-5c3eee0d311c.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in ("evaluare.in"); +ofstream out ("evaluare.out"); + +stack s; +deque < pair > fin; +stack r; + + + + + + + +int main() +{ + char c,aux='a'; + long long cnt=0,nr=0,x1,x2,rez; + while (in.get(c)&&c!='\n') + { + if (isdigit(c)&&isdigit(aux)) + nr=nr*10+c-48; + else if (isdigit(c)&&!isdigit(aux)) + nr=c-48; + else if (!isdigit(c)&&isdigit(aux)) + fin.push_back({nr,0}); + if (!isdigit(c)) + { + if (c=='+'||c=='-') + { + while (!s.empty()&&s.top()) + fin.push_back({s.top(),1}),s.pop(); + if (c=='+') + s.push(2); + else + s.push(1); + } + else if (c=='*'||c=='/') + { + while (!s.empty()&&s.top()>=3) + fin.push_back({s.top(),1}),s.pop(); + if (c=='*') + s.push(3); + else + s.push(4); + } + else if (c=='(') + s.push(0); + else + { + while (!s.empty()&&s.top()!=0) + fin.push_back({s.top(),1}),s.pop(); + s.pop(); + } + } + aux=c; + } + if (isdigit(aux)) + fin.push_back({nr,0}); + while (!s.empty()) + fin.push_back({s.top(),1}),s.pop(); + for (int i=0;i +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +string s; + +stacksol; + +stackrez; + +queueq; + +unordered_mapmp; + +int me(char a,char b) + +{ + + return mp[a]>=mp[b]; + +} + +int conv(string s) + +{ + + int ret=0; + + for(int i=0;i>s; + + s='('+s+')'; + + for(int i=0;i +#include +using namespace std; +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + + + +int precedence(char op){ + if(op == '+'||op == '-') + return 1; + if(op == '*'||op == '/') + return 2; + return 0; +} + + +int applyOp(int a, int b, char op){ + switch(op){ + case '+': return a + b; + case '-': return a - b; + case '*': return a * b; + case '/': return a / b; + } +} + + + +int evaluate(string tokens){ + int i; + + + stack values; + + + stack ops; + + for(i = 0; i < tokens.length(); i++){ + + + + if(tokens[i] == ' ') + continue; + + + + else if(tokens[i] == '('){ + ops.push(tokens[i]); + } + + + + else if(isdigit(tokens[i])){ + int val = 0; + + + + while(i < tokens.length() && + isdigit(tokens[i])) + { + val = (val*10) + (tokens[i]-'0'); + i++; + } + + values.push(val); + i--; + } + + + + else if(tokens[i] == ')') + { + while(!ops.empty() && ops.top() != '(') + { + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + if(!ops.empty()) + ops.pop(); + } + + + else + { + + + + + while(!ops.empty() && precedence(ops.top()) + >= precedence(tokens[i])){ + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + ops.push(tokens[i]); + } + } + + + + + while(!ops.empty()){ + int val2 = values.top(); + values.pop(); + + int val1 = values.top(); + values.pop(); + + char op = ops.top(); + ops.pop(); + + values.push(applyOp(val1, val2, op)); + } + + + return values.top(); +} + +int main() { + string s; + fin >> s; + fout << evaluate(s); + return 0; +} diff --git a/eval/stack/dc680579-e04f-4ea5-beff-f03c93065710.cpp b/eval/stack/dc680579-e04f-4ea5-beff-f03c93065710.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0c4c42b96fdec10c160326286a0e8dce519d07f --- /dev/null +++ b/eval/stack/dc680579-e04f-4ea5-beff-f03c93065710.cpp @@ -0,0 +1,221 @@ +#include +#include +#include +#include +using namespace std; + + + +string name = "evaluare"; + +ifstream fin(name + ".in"); + +ofstream fout(name + ".out"); + + + +string expression; + + + +bool isOperator(char ch) + +{ + + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; + +} + + + +int getPriority(char ch) + +{ + + if (ch == '+' || ch == '-') + + { + + return 1; + + } + + else if (ch == '*' || ch == '/') + + { + + return 2; + + } + + return -1; + +} + + + +void applyOperator(stack& numbers, char op) + +{ + + int secondNo = numbers.top(); + + numbers.pop(); + + int firstNo = numbers.top(); + + numbers.pop(); + + switch (op) + + { + + case '+': + + { + + numbers.push(firstNo + secondNo); + + break; + + } + + case '-': + + { + + numbers.push(firstNo - secondNo); + + break; + + } + + case '*': + + { + + numbers.push(firstNo * secondNo); + + break; + + } + + case '/': + + { + + numbers.push(firstNo / secondNo); + + break; + + } + + } + +} + + + +int main() + +{ + + fin >> expression; + + stack numbers; + + stack operators; + + for (size_t i = 0; i < expression.size(); i++) + + { + + if (expression[i] == '(') + + { + + operators.push('('); + + } + + else if (expression[i] == ')') + + { + + while (operators.top() != '(') + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + operators.pop(); + + } + + else if (isOperator(expression[i])) + + { + + while (!operators.empty() && getPriority(operators.top()) >= getPriority(expression[i])) + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + + + operators.push(expression[i]); + + } + + else + + { + + assert(isdigit(expression[i])); + + int number = 0; + + while (i < expression.size() && isdigit(expression[i])) + + { + + number = number * 10 + expression[i++] - '0'; + + } + + i--; + + numbers.push(number); + + } + + } + + + + while (!operators.empty()) + + { + + applyOperator(numbers, operators.top()); + + operators.pop(); + + } + + + + fout << numbers.top() << "\n"; + + return 0; + +} diff --git a/eval/stack/dd74a005-a671-46ab-9311-f5594abe86bf.cpp b/eval/stack/dd74a005-a671-46ab-9311-f5594abe86bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f03e38230be30e9086bde3aaa8b78a259c033189 --- /dev/null +++ b/eval/stack/dd74a005-a671-46ab-9311-f5594abe86bf.cpp @@ -0,0 +1,247 @@ +#include +#include +using namespace std; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + +char vc[100005]; + +int v[100005], st[100005]; + +int main() + +{ + + int n, i, nr, cnt=0, k=0, ok=0; + + fin>>vc+1; + + n=strlen(vc+1); + + i=1; + + while (i<=n) + + { + + if (vc[i]>='0'&&vc[i]<='9') + + { + + nr=0; + + while (vc[i]>='0'&&vc[i]<='9') + + { + + nr=nr*10+vc[i]-'0'; + + i++; + + } + + v[++cnt]=nr; + + i--; + + } + + else + + { + + if (vc[i]=='(') + + st[++k]=-1; + + else + + if (vc[i]==')') + + { + + while (st[k]!=-1) + + { + + v[++cnt]=st[k]; + + k--; + + } + + k--; + + } + + else + + if (vc[i]=='+') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-3; + + } + + else + + if (vc[i]=='-') + + { + + while (st[k]!=-1&&k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-4; + + } + + else + + if (vc[i]=='*') + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-5; + + } + + else + + { + + while (st[k]==-5||st[k]==-6) + + { + + v[++cnt]=st[k]; + + k--; + + } + + st[++k]=-6; + + } + + } + + i++; + + } + + while (k) + + { + + v[++cnt]=st[k]; + + k--; + + } + + for (i=1;i<=cnt;i++) + + { + + if (v[i]>=0) + + st[++k]=v[i]; + + else + + { + + if (v[i]==-5) + + { + + if (k<=1) + + st[k]=0; + + else + + { + + k--; + + st[k]=st[k]*st[k+1]; + + } + + } + + else + + if (v[i]==-6) + + { + + k--; + + st[k]/=(st[k+1]); + + } + + else + + if (v[i]==-3) + + { + + k--; + + st[k]+=st[k+1]; + + } + + else + + { + + k--; + + st[k]-=st[k+1]; + + } + + } + + } + + fout< +#include +#include +#include +#include +using namespace std; + +bool is_number(char a) { + return a >= '0' && a <= '9'; +} + +bool is_operator(char a) { + return a == '+' || a == '-' || a == '*' || a == '/'; +} + +int result(int a, int b, string cur) { + if (cur == "+") + return a + b; + if (cur == "-") + return a - b; + if (cur == "*") + return a * b; + if (cur == "/") + return a / b; + return 1; +} + +vector expresie_polandeza(string &exp) { + int n = exp.size(); + + map prec; + prec['+'] = prec['-'] = 0; + prec['*'] = prec['/'] = 1; + + stack st; + st.push('e'); + + vector output; + int i = 0; + while (i < n) { + if (is_number(exp[i])) { + string number = ""; + while (is_number(exp[i])) { + number += exp[i]; + i++; + } + output.push_back(number); + } else { + if (exp[i] == '(') { + st.push(exp[i]); + } else if (exp[i] == ')') { + while (st.top() != '(') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + } else { + while (is_operator(st.top()) && prec[st.top()] >= prec[exp[i]]) { + output.push_back(string(1, st.top())); + st.pop(); + } + st.push(exp[i]); + } + i++; + } + } + + while (st.top() != 'e') { + output.push_back(string(1, st.top())); + st.pop(); + } + st.pop(); + + return output; +} + + +int evaluare_polandeza(vector &output) { + stack eval; + for (int i = 0; i < (int) output.size(); i++) { + string cur = output[i]; + if (!is_operator(cur[0])) { + eval.push(stoi(cur)); + } else { + int a = eval.top(); + eval.pop(); + int b = eval.top(); + eval.pop(); + int r = result(b, a, cur); + eval.push(r); + } + } + return eval.top(); +} + +int main() { + ifstream fin("evaluare.in"); + ofstream fout("evaluare.out"); + + string exp; fin >> exp; + + vector output = expresie_polandeza(exp); + + int res = evaluare_polandeza(output); + fout << res << "\n"; + + return 0; +} diff --git a/eval/stack/dfda66b2-e4d4-44ee-8fec-75b6ae39d2fa.cpp b/eval/stack/dfda66b2-e4d4-44ee-8fec-75b6ae39d2fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c7d0d1a3cf54c8451088be51376942007a37eff --- /dev/null +++ b/eval/stack/dfda66b2-e4d4-44ee-8fec-75b6ae39d2fa.cpp @@ -0,0 +1,219 @@ +#include + + +using namespace std; + + + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + + + +long long operatie[100001],q[100001],n,numIndex,opIndex,number; + +char s[100001]; +inline void readInput() + +{ + + f.getline(s, 100001); + + n = strlen(s); + +} + + + +int grad(char c) + +{ + + if(c == '+' || c == '-') return 1; + + if(c == '*' || c == '/') return 2; + + return 0; + +} + + + +void calculeaza(int a,int b) + +{ + + if(operatie[b]=='+') + + { + + q[a-1]+=q[a]; + + } + + if(operatie[b]=='-') + + { + + q[a-1]-=q[a]; + + } + + if(operatie[b]=='*') + + { + + q[a-1]*=q[a]; + + } + + if(operatie[b]=='/') + + { + + q[a-1]/=q[a]; + + } + + return; + +} + + + +inline void Solution() + +{ + + for(int i = 0 ; i < n ; ++ i) + + { + + if(isdigit(s[i])) + + { + + number = 0; + + while(isdigit(s[i])) + + { + + number *= 10; + + number += (s[i] - '0'); + + i++; + + } + + i--; + + q[++numIndex] = number; + + } + + else + + { + + if(opIndex == 0 || s[i] == '(' || grad(operatie[opIndex]) < grad(s[i])) + + { + + operatie[++ opIndex] = s[i]; + + } + + else if(s[i] == ')') + + { + + while(operatie[opIndex] != '(') + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + + opIndex --; + + } + + else + + { + + while(grad(s[i]) <= grad(operatie[opIndex])) + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + + operatie[++ opIndex] = s[i]; + + } + + } + + + + } + + while(opIndex) + + { + + calculeaza(numIndex, opIndex); + + numIndex --; + + opIndex --; + + } + +} + + + +inline void Output() + +{ + + g << q[1]; + +} + + + +int main() + +{ + + readInput(); + + Solution(); + + Output(); + + return 0; + +} diff --git a/eval/stack/e0f9cd8f-5a27-42dc-8aaa-c75ca2c97791.cpp b/eval/stack/e0f9cd8f-5a27-42dc-8aaa-c75ca2c97791.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbe4630d0945b7b6d20914e6410dd443648a11a2 --- /dev/null +++ b/eval/stack/e0f9cd8f-5a27-42dc-8aaa-c75ca2c97791.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +string nume_problema = "evaluare"; +ifstream fin(nume_problema + ".in"); +ofstream fout(nume_problema + ".out"); + + + +enum operand +{ + ADD = '+', + SUB = '-', + MUL = '*', + DIV = '/', + LPR = '(', + RPR = ')', + VAL = '0' +}; +struct expr +{ + vector> ops; + vector> tmp; + expr() + { + } + void grad2() + { + bool lastmult = false; + for (int i = 1; i < ops.size(); i += 2) + { + switch (ops[i].first) + { + case MUL: + case DIV: + { + int prod = ops[i - 1].second; + while (i < ops.size() && (ops[i].first == MUL || ops[i].first == DIV)) + { + if (ops[i].first == MUL) + prod *= ops[i + 1].second; + if (ops[i].first == DIV) + prod /= ops[i + 1].second; + i += 2; + } + if (i >= ops.size()) + lastmult = true; + ops[i - 1].second = prod; + } + default: + tmp.push_back(ops[i - 1]); + if (i < ops.size()) + tmp.push_back(ops[i]); + break; + } + } + if (!lastmult) + tmp.push_back(ops.back()); + } + int grad1() + { + int semn = 1, total = 0; + for (int i = 0; i < tmp.size(); i++) + { + if (tmp[i].first == ADD) + semn = 1; + else if (tmp[i].first == SUB) + semn = -1; + else + total += tmp[i].second * semn; + } + return total; + } + int execute() + { + grad2(); + return grad1(); + } + void dbg() + { + for (const auto &i : ops) + cerr << "OPS:" << char(i.first) << " " << i.second << endl; + for (const auto &i : tmp) + cerr << "TMP:" << i.first << " " << i.second << endl; + } +}; +stack st; + +int main() +{ + expr blank; + st.push(blank); + int val = 0; + for (char c; fin >> c;) + { + if (c == '(') + { + expr blank; + st.push(blank); + } + else if (c == ')') + { + st.top().ops.push_back(make_pair(VAL, val)); + val = st.top().execute(); + st.pop(); + } + else if (isdigit(c)) + val = val * 10 + (c - '0'); + else + { + st.top().ops.push_back(make_pair(VAL, val)); + val = 0; + if (c == ADD) + st.top().ops.push_back(make_pair(ADD, -1)); + if (c == SUB) + st.top().ops.push_back(make_pair(SUB, -1)); + if (c == MUL) + st.top().ops.push_back(make_pair(MUL, -1)); + if (c == DIV) + st.top().ops.push_back(make_pair(DIV, -1)); + } + } + st.top().ops.push_back(make_pair(VAL, val)); + + + fout << st.top().execute(); + return 0; +} diff --git a/eval/stack/e26d863e-4adb-460e-b89d-c57c01937845.cpp b/eval/stack/e26d863e-4adb-460e-b89d-c57c01937845.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ff44097b3d1ea571a57dce3eda08414fc823ae9 --- /dev/null +++ b/eval/stack/e26d863e-4adb-460e-b89d-c57c01937845.cpp @@ -0,0 +1,267 @@ +#include +#include +#include +#include + + + + +std::ifstream f("evaluare.in"); + +std::ofstream g("evaluare.out"); + + + +std::string s; + + + +long long order(const char& c){ + + if(c == '+' || c == '-') + + return 1; + + else if(c == '*' || c == '/') + + return 2; + + return 0; + +} + + + +int main(){ + + f >> s; + + + + std::stackval; + + std::stackops; + + + + for(long long i = 0;i < s.length();i++){ + + + + if(s[i] == ' ') + + continue; + + + + if(i == 0 && !isdigit(s[i])){ + + char sign = s[0]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(sign == '-') + + val.push(-1 * value); + + else val.push(value); + + }else if(isdigit(s[i])){ + + long long value{}; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + val.push(value); + + }else if(s[i] == '('){ + + ops.push(s[i]); + + if(s[i + 1] == '-' || s[i + 1] == '+'){ + + i++; + + char op = s[i]; + + long long value{}; + + i++; + + while(isdigit(s[i])){ + + value = value * 10 + s[i] - '0'; + + i++; + + } + + i--; + + if(op == '+') + + val.push(value); + + else if(op == '-') + + val.push(-value); + + } + + }else if(s[i] == ')'){ + + + + while (!ops.empty() && val.size() >= 2 && ops.top() != '(') { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + if(!ops.empty()) + + ops.pop(); + + + + }else{ + + while (!ops.empty() && order(ops.top()) >= order(s[i])&& val.size() >= 2) { + + + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + ops.push(s[i]); + + } + + } + + + + while (!ops.empty() && val.size() >= 2) { + + long long a = val.top(); + + val.pop(); + + long long b = val.top(); + + val.pop(); + + char op = ops.top(); + + ops.pop(); + + std::swap(a,b); + + + + if(op == '+') + + val.push(a + b); + + else if(op == '-') + + val.push(a - b); + + else if(op == '*') + + val.push(a * b); + + else val.push(a / b); + + } + + + + g << val.top(); + +} diff --git a/eval/stack/e28296f1-1ff9-429f-97c3-f5dcfb529d63.cpp b/eval/stack/e28296f1-1ff9-429f-97c3-f5dcfb529d63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa90f28021d22e015c18ffa53728d068cbaa35cd --- /dev/null +++ b/eval/stack/e28296f1-1ff9-429f-97c3-f5dcfb529d63.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +char s[100005]; +char op[50005]; +int nr[50005],optop,nrtop,pr[200]; + + +int calculate(int a,int b,char op) +{ + if(op=='+') return a+b; + else if(op=='-') return a-b; + else if(op=='*') return a*b; + else if(op=='/') return a/b; +} +int main() +{ + in>>s; + pr['+']=1; + pr['-']=1; + pr['*']=2; + pr['/']=2; + int lung=strlen(s); + for(int i=0;i='0' && s[i]<='9') + { + int number=0; + while(s[i]>='0' && s[i]<='9') + { + number=number*10+s[i]-'0'; + i++; + } + i--; + nr[++nrtop]=number; + } + else + { + while(optop>0 && pr[op[optop]]>=pr[s[i]]) + { + int ans=calculate(nr[nrtop-1],nr[nrtop],op[optop]); + optop--; + nrtop--; + nr[nrtop]=ans; + } + op[++optop]=s[i]; + } + } + while(optop>0) + { + int ans=calculate(nr[nrtop-1],nr[nrtop],op[optop]); + nrtop--; + optop--; + nr[nrtop]=ans; + } + out< + + + + +using namespace std; + +typedef long long ll; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + +const int N = 100005; + +char c[N]; + +string s; + +stack < int > nums; + +stack < char > op; + +bool is_op(char ch) { + + if (ch == '+' || ch == '-' || ch == '/' || ch == '*') { + + return true; + + } + + return false; + +} + +int val(char ch) { + + if (ch == '+' || ch == '-') { + + return 1; + + } + + if (ch == '*' || ch == '/') { + + return 2; + + } + + return -1; + +} + +int do_op(char op) { + + int a = nums.top(); + + nums.pop(); + + int b = nums.top(); + + nums.pop(); + + if (op == '+') { + + b += a; + + } + + if (op == '-') { + + b -= a; + + } + + if (op == '*') { + + b *= a; + + } + + if (op == '/') { + + b /= a; + + } + + nums.push(b); + +} + +int main() { + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + in >> s; + + for (int i = 0; i < s.size(); i++) { + + c[i + 1] = s[i]; + + } + + int n = s.size(); + + for (int i = 1; i <= n; i++) { + + if (c[i] == '(') { + + op.push('('); + + } + + else if (c[i] == ')') { + + while(op.top() != '(') { + + char oper = op.top(); + + op.pop(); + + do_op(oper); + + } + + op.pop(); + + } + + else if (is_op(c[i])) { + + while(op.size() && val(c[i]) <= val(op.top())) { + + do_op(op.top()); + + op.pop(); + + } + + op.push(c[i]); + + } + + else { + + int nr = 0; + + int j = i; + + while(c[j] >= '0' && c[j] <= '9') { + + nr = nr * 10 + (c[j] - '0'); + + j++; + + } + + nums.push(nr); + + i = j - 1; + + } + + } + + while(op.size()) { + + do_op(op.top()); + + op.pop(); + + } + + out << nums.top() << '\n'; + + return 0; + +} diff --git a/eval/stack/e69b5495-45f7-45e3-9c8d-06ee255d5ea9.cpp b/eval/stack/e69b5495-45f7-45e3-9c8d-06ee255d5ea9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c406c67a67aeac4a4411c21494c074ead794f6f --- /dev/null +++ b/eval/stack/e69b5495-45f7-45e3-9c8d-06ee255d5ea9.cpp @@ -0,0 +1,266 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + +char vp[42949678],s[42949678],exp[42949678]; + +stack st; + +int n,kexp; + + + + + +int prioritate(int c) + +{ + + if (s[c]=='(' || s[c]==')') + + return 0; + + if (s[c]=='*' || s[c]=='/') + + return 2; + + return 1; + +} + + + +void formare_nr(int &k) + +{ + + while (s[k]>='0'&& s[k]<='9') + + { + + exp[++kexp]=s[k]; + + + + k++; + + } + + k--; + + exp[++kexp]=' '; + +} + + + + + +void forma_poloneza() + +{ + + for (int i=0;i<=n;i++) + + { + + if (s[i]>='0' && s[i]<='9') + + formare_nr(i); + + else if (prioritate(i)!=0) + + { + + if (st.empty()) + + st.push(i); + + else + + { + + int a=prioritate(i); + + while (!st.empty() && prioritate(st.top())>=a) + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + st.push(i); + + } + + } + + else + + { + + if (s[i]=='(') + + st.push(i); + + else + + { + + while (!st.empty()&& s[st.top()]!='(') + + { + + exp[++kexp]=s[st.top()]; + + st.pop(); + + } + + exp[++kexp]=' '; + + if (!st.empty()&& s[st.top()]=='(') + + st.pop(); + + } + + } + + + + } + + while (!st.empty()) exp[++kexp]= s[st.top()], st.pop(); + + + +} + + + +int transf_int (int &k) + +{ int nr=0; + + while (exp[k]>='0' && exp[k]<='9') + + { + + nr =nr*10 + (exp[k]-'0'); + + k++; + + } + + + + return nr; + + + + + +} + + + +int calcul(int i, int a,int b) + +{ + + + + if (exp[i]=='-') + + return a-b; + + if (exp[i]=='+') + + return a+b; + + if (exp[i]=='*') + + return a*b; + + return a/b; + + + +} + + + +int evaluare(int k) + +{ + + while (!st.empty()) + + st.pop(); + + for (int i=1;i<=k;i++) + + {if (exp[i]>='0' && exp[i]<='9') + + st.push(transf_int(i)); + + if (st.size()>=2 && exp[i]!=' ') + + { + + int a,b; + + a=st.top(); + + st.pop(); + + b=st.top(); + + st.pop(); + + + + st.push(calcul(i,b,a)); + + } + + } + + return st.top(); + + + +} + + + +int main() + +{ + + f>>s; + + n=strlen(s)-1; + + forma_poloneza(); + + + +g< + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< +#include +#include + + + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include +#include + +using namespace std; + +inline int app_op ( int x , int y , char op ) +{ + if ( op == '+' ) return x + y ; + if ( op == '-' ) return y - x ; + if ( op == '*' ) return x * y ; + if ( op == '/' ) return y / x ; + return 0 ; +} + +inline int priority ( char op ) +{ + if ( op == '+' || op == '-' ) return 1 ; + if ( op == '*' || op == '/' ) return 2 ; + return 0 ; +} + +void solve ( void ) +{ + stack < char > operatori ; + stack < int > operanzi ; + char ch , op ; + int nr = 0 , x1 , x2 , ok = 0 ; + + while ( scanf ( "%c" , & ch ) != EOF ) + { + if ( isdigit ( ch ) ) + nr = nr * 10 + ( int ) ( ch - '0' ) , ok = 1 ; + else + { + if ( ok ) + { + operanzi.push ( nr ) ; + nr = 0 ; + ok = 0 ; + } + + if ( ch == '(' ) + operatori.push ( '(' ) ; + else + if ( ch == ')' ) + { + while ( operatori.top () != '(' ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + operatori.pop () ; + } + else + if ( ch == '+' || ch == '-' || ch == '*' || ch == '/' ) + { + while ( ! operatori.empty () && priority ( ch ) <= priority ( operatori.top () ) ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + operatori.push ( ch ) ; + } + } + } + + while ( ! operatori.empty () ) + { + op = operatori.top () ; operatori.pop () ; + x1 = operanzi.top () ; operanzi.pop () ; + x2 = operanzi.top () ; operanzi.pop () ; + + operanzi.push ( app_op ( x1 , x2 , op ) ) ; + } + + printf ( "%d" , operanzi.top () ) ; +} + +int main() +{ + freopen ( "evaluare.in" , "r" , stdin ) ; + freopen ( "evaluare.out" , "w" , stdout ) ; + + solve () ; + + return 0; +} diff --git a/eval/stack/ec2fcd7d-a533-406e-9da0-5bd76b04d596.cpp b/eval/stack/ec2fcd7d-a533-406e-9da0-5bd76b04d596.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eae227f1fd351ea43328327c54328aa485767dea --- /dev/null +++ b/eval/stack/ec2fcd7d-a533-406e-9da0-5bd76b04d596.cpp @@ -0,0 +1,194 @@ +#include +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +stack nums; + +stack ops; + +int pri[260]; + + + +void setpri(); + +int getNum(char*&s); + +void eval(char* s); + +void exec(char &op); + + + +int main(){ + + setpri(); + + char s[100005]; + + fin>>s; + + eval(s); + + fout<=pri[op]){ + + exec(ops.top()); + + ops.pop(); + + } + + ops.push(op); + + s++; + + } + + } + + while(!ops.empty()){ + + exec(ops.top()); + + ops.pop(); + + } + +} + + + +void exec(char &op){ + + int rez = 0; + + int x1 = nums.top(); + + nums.pop(); + + int x2 = nums.top(); + + nums.pop(); + + switch (op){ + + case '+': + + rez = x1+x2; + + break; + + case '-': + + rez = x2-x1; + + break; + + case '*': + + rez = x1*x2; + + break; + + case '/': + + rez = x2/x1; + + break; + + case '^': + + rez = pow(x2,x1); + + break; + + default: + + break; + + } + + nums.push(rez); + +} diff --git a/eval/stack/ec3441e7-70be-4d23-b253-5952b81b93c7.cpp b/eval/stack/ec3441e7-70be-4d23-b253-5952b81b93c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1ee849344d71c328e9fb218d65ce22dcc53996a --- /dev/null +++ b/eval/stack/ec3441e7-70be-4d23-b253-5952b81b93c7.cpp @@ -0,0 +1,93 @@ +#include +#include + + + +using namespace std; + +stack postForm; +stack opStack; + +int priorities[256]; + +void setPriorities() { + priorities['+'] = 1; + priorities['-'] = 1; + priorities['*'] = 2; + priorities['/'] = 2; + priorities['('] = 0; +} + +int getNum(char *&s) { + int num = 0; + while (isdigit(*s)) { + num = num * 10 + *s - '0'; + s++; + } + return num; +} + +int eval(int x, int y, char op) { + switch (op) { + case '+': + return x + y; + case '-': + return x - y; + case '*': + return x * y; + case '/': + return x / y; + default: + return 0; + } +} + +void evaluateLast() { + char op = opStack.top(); + opStack.pop(); + int o2 = postForm.top(); + postForm.pop(); + int o1 = postForm.top(); + postForm.pop(); + postForm.push(eval(o1, o2, op)); +} + +void convert(char *s) { + while (*s) { + if (isdigit(*s)) { + postForm.push(getNum(s)); + } else { + if (*s == '(') { + opStack.push(*s); + s++; + } else if (*s == ')') { + while (opStack.top() != '(') { + evaluateLast(); + } + opStack.pop(); + s++; + } else { + char op = *s; + s++; + while (!opStack.empty() && priorities[opStack.top()] >= priorities[op]) { + evaluateLast(); + } + opStack.push(op); + } + } + } + while (!opStack.empty()) { + evaluateLast(); + } +} + +int main() { + ifstream f("evaluare.in"); + ofstream g("evaluare.out"); + setPriorities(); + char S[100005], *s = S; + f >> S; + convert(s); + g << postForm.top() << '\n'; + return 0; +} diff --git a/eval/stack/ec73009d-f78c-459e-a47e-1e907e4e6685.cpp b/eval/stack/ec73009d-f78c-459e-a47e-1e907e4e6685.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99f4d669dc57a436ae3aaf2fda234fffb840bd99 --- /dev/null +++ b/eval/stack/ec73009d-f78c-459e-a47e-1e907e4e6685.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include + +using namespace std; + + +ifstream fin("evaluare.in"); +ofstream fout("evaluare.out"); + +char e[100005]; +int n; + +int priority(char o){ + if(o == '+' || o == '-') + return 1; + if(o == '*' || o == '/') + return 2; + return 0; +} + +int compute(int a, int b, int o){ + if(o == '+') + return a + b; + if(o == '-') + return a - b; + if(o == '*') + return a * b; + if(o == '/') + return a / b; +} + +int eval(){ + int i = 0; + stack op; + stack val; + while(i < n){ + if(e[i] == '('){ + op.push(e[i]); + } + else if(e[i] == ')'){ + while(!op.empty() && op.top() != '('){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.pop(); + } + else if(isdigit(e[i])){ + int v = 0; + while(i < n && isdigit(e[i])){ + v = (v * 10) + (e[i] - '0'); + ++i; + } + --i; + val.push(v); + } + else{ + while(!op.empty() && priority(op.top()) >= priority(e[i])){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + op.push(e[i]); + } + ++i; + } + while(!op.empty()){ + int val2 = val.top(); + val.pop(); + int val1 = val.top(); + val.pop(); + char o = op.top(); + op.pop(); + val.push(compute(val1, val2, o)); + } + return val.top(); +} + +int main(){ + + fin.getline(e, 100005); + n = strlen(e); + fout << eval(); + fin.close(); + fout.close(); + return 0; +} diff --git a/eval/stack/ed1814d5-ac98-466f-b826-c7cda7e7a03a.cpp b/eval/stack/ed1814d5-ac98-466f-b826-c7cda7e7a03a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..716d50caed55acc98b8c251b71a66113f3349693 --- /dev/null +++ b/eval/stack/ed1814d5-ac98-466f-b826-c7cda7e7a03a.cpp @@ -0,0 +1,162 @@ +#include +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +int st,qu,x,y; + +string s,nr; + +string q[100005]; + +char a[100005]; + +int aa[100005]; + + + +bool comp(char A,char B) + +{ + + if((A=='+' || A=='-') && B!='(') return true; + + if((A=='*' || A=='/') && (B=='*' || B=='/')) return true; + + return false; + +} + + + +int main() + +{ + + getline(f,s); + + for(int i=0;i='0' && s[i]<='9') + + { + + nr+=s[i]; + + ++i; + + } + + --i; + + q[++qu]=nr; + + } + + } + + while(st) + + { + + q[++qu]=a[st]; + + --st; + + } + + st=0; + + for(int i=1;i<=qu;++i) + + { + + if(q[i][0]>='0' && q[i][0]<='9') + + { + + x=atoi(q[i].c_str()); + + aa[++st]=x; + + } + + else + + { + + y=aa[st]; --st; + + x=aa[st]; --st; + + if(q[i][0]=='+') x+=y; + + if(q[i][0]=='-') x-=y; + + if(q[i][0]=='*') x*=y; + + if(q[i][0]=='/') x/=y; + + aa[++st]=x; + + } + + } + + g< +using namespace std; + +vector get_precedence() +{ + vector ans(256, 0); + ans['-'] = ans['+'] = 1; + ans['*'] = ans['/'] = 2; + + return ans; +} + +vector transform_to_postfix(string S) +{ + vector precedence = get_precedence(); + vector postfix; + vector operators; + for (int i = 0; i < S.size(); i++) + { + if (S[i] >= '0' && S[i] <= '9') + { + int j = i; + while (i + 1 < S.size() && S[i + 1] >= '0' && S[i + 1] <= '9') + i++; + postfix.push_back(S.substr(j, i - j + 1)); + continue; + } + if (S[i] == ')') + { + while (!operators.empty() && operators.back() != '(') + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + operators.pop_back(); + continue; + } + + if (S[i] == '(' || operators.empty() || precedence[S[i]] > precedence[operators.back()]) + operators.push_back(S[i]); + else + { + while (!operators.empty() && operators.back() != '(' && precedence[operators.back()] >= precedence[S[i]]) + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + operators.push_back(S[i]); + } + } + + + while (!operators.empty()) + { + postfix.push_back(string(1, operators.back())); + operators.pop_back(); + } + + return postfix; +} + +int convert_string_to_number(string S) +{ + int ans = 0; + for (int i = 0; i < S.size(); i++) + ans = ans * 10 + S[i] - '0'; + + return ans; +} + +int evaluate(vector postfix) +{ + vector operands; + for (auto elem : postfix) + if (elem[0] >= '0' && elem[0] <= '9') + operands.push_back(convert_string_to_number(elem)); + else + { + int y = operands.back(); + operands.pop_back(); + int x = operands.back(); + operands.pop_back(); + int res; + if (elem[0] == '+') + res = x + y; + if (elem[0] == '-') + res = x - y; + if (elem[0] == '*') + res = x * y; + if (elem[0] == '/') + res = x / y; + operands.push_back(res); + } + + return operands.back(); +} + +int main() +{ + freopen("evaluare.in", "r", stdin); + freopen("evaluare.out", "w", stdout); + cin.sync_with_stdio(false); + + string S; + cin >> S; + vector postfix = transform_to_postfix(S); + cout << evaluate(postfix) << "\n"; + return 0; +} diff --git a/eval/stack/ef0a7502-8f00-463a-908d-efdd66a0afc1.cpp b/eval/stack/ef0a7502-8f00-463a-908d-efdd66a0afc1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f10fdbc55340f40857285d54c87a915220c504b7 --- /dev/null +++ b/eval/stack/ef0a7502-8f00-463a-908d-efdd66a0afc1.cpp @@ -0,0 +1,180 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int prec(char c) + +{ + + if(c=='^')return 3; + + if(c=='*' || c=='/')return 2; + + if(c=='+' || c=='-') return 1; + + return -1; + +} + +string infixToPostfix(string a) + +{ + + string temp=""; + + stacks; + + int i,n=a.size(); + + for(i=0; i>c) + + { + + if(c<'0' || c>'9')a+=c; + + else + + { + + a+='a'; + + int nr = c-'0'; + + while(fin>>c) + + if(c<'0' || c>'9') + + { + + a+=c; + + break; + + } + + else{ + + int cifra=c-'0'; + + nr=nr*10+cifra; + + } + + x[k]=nr; + + ++k; + + } + + } + + k=1; + + a=infixToPostfix(a); + + int n= a.size(); + + stacks; + + for(i=0;i +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c[100001]; + +int p[100001]; + +pair < int, char > v[100001], stiva[100001]; + +int nrv, nrs; + + + +int rezolva(char c[]) + +{ + + int rez = 0; + + char semn = '+'; + + for (int i = 0; c[i]; i++) + + { + + if (c[i] == '+' || c[i] == '-' || c[i] == '*' || c[i] == '/') + + semn = c[i]; + + else if (c[i] >= '0' && c[i] <= '9') + + { + + int nr = 0; + + while (c[i] >= '0' && c[i] <= '9') + + { + + nr = nr * 10 + c[i] - '0'; + + i++; + + } + + i--; + + + + if (semn == '+' || semn == '-') + + { + + nrv++; + + v[nrv].first = nr; + + v[nrv].second = semn; + + } + + else + + { + + if (semn == '*') + + v[nrv].first *= nr; + + else if (nr) + + v[nrv].first /= nr; + + } + + } + + else if (c[i] == '(') + + { + + nrs++; + + stiva[nrs].first = nrv + 1; + + stiva[nrs].second = semn; + + semn = '+'; + + } + + else if (c[i] == ')') + + { + + for (int j = stiva[nrs].first + 1; j <= nrv; j++) + + if (v[j].second == '+') + + v[stiva[nrs].first].first += v[j].first; + + else + + v[stiva[nrs].first].first -= v[j].first; + + nrv = stiva[nrs].first; + + v[nrv].second = stiva[nrs].second; + + nrs--; + + if (stiva[nrs].first != nrv) + + { + + if (v[nrv].second == '*') + + { + + v[nrv - 1].first *= v[nrv].first; + + nrv--; + + } + + else if (v[nrv].second == '/') + + { + + v[nrv - 1].first /= v[nrv].first; + + nrv--; + + } + + } + + } + + } + + + + for (int i = 2; i <= nrv; i++) + + if (v[i].second == '+') + + v[1].first += v[i].first; + + else + + v[1].first -= v[i].first; + + + + g << v[1].first; + +} + + + +int main() + +{ + + f >> c; + + rezolva(c); + + return 0; + +} diff --git a/eval/stack/efdd93ce-546a-4650-add0-c20a32d7fba0.cpp b/eval/stack/efdd93ce-546a-4650-add0-c20a32d7fba0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5329efabcddfb36323c99922ee2c75854dfb13f --- /dev/null +++ b/eval/stack/efdd93ce-546a-4650-add0-c20a32d7fba0.cpp @@ -0,0 +1,238 @@ +#include + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +string s; + + + +int apl(int X, int Y, char Z) { + + if(Z == '+') return X + Y; + + if(Z == '-') return X - Y; + + if(Z == '*') return X * Y; + + return X / Y; + +} + + + +int prec(const char X) { + + if(X == '+' || X == '-') return 1; + + if(X == '*' || X == '/') return 2; + + return 0; + +} + + + +int evexp1(string X) { + + stack nb; + + stack op; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') op.push(X[i]); + + else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nb.push(nr); + + } else if(X[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + + + if(!op.empty()) op.pop(); + + } else { + + while(!op.empty() && prec(op.top()) >= prec(X[i])) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + op.push(X[i]); + + } + + } + + while(!op.empty()) { + + int nr1 = nb.top(); + + nb.pop(); + + int nr2 = nb.top(); + + nb.pop(); + + char ope = op.top(); + + op.pop(); + + nb.push(apl(nr2, nr1, ope)); + + } + + return nb.top(); + +} + + + +int evexp(string X) { + + stack nbs; + + stack ops; + + for(int i = 0; i < X.size(); ++i) { + + if(X[i] == '(') ops.push(X[i]); + + else if(X[i] == ')') { + + while(!ops.empty() && ops.top() != '(') { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + if(!ops.empty()) ops.pop(); + + } else if(isdigit(X[i])) { + + int nr = 0; + + while(i < X.size() && isdigit(X[i])) + + nr = nr * 10 + X[i] - '0', ++i; + + --i, nbs.push(nr); + + } else { + + while(!ops.empty() && prec(ops.top()) >= prec(X[i])) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + ops.push(X[i]); + + } + + } + + while(!ops.empty()) { + + int nr1 = nbs.top(); + + nbs.pop(); + + int nr2 = nbs.top(); + + nbs.pop(); + + char ope = ops.top(); + + ops.pop(); + + nbs.push(apl(nr2, nr1, ope)); + + } + + return nbs.top(); + +} + + + +int main() + +{ + + getline(fin, s), fout< +#include +#include +#include +#include + + +using namespace std; + + + +class ExprEvaluator { + +private: + + + + vector OPERATORS; + + bool isOperator(char); + + int opPriority(char); + + int evaluateSubExpr(int, int, char); + + + +public: + + + + ExprEvaluator(); + + int evaluateExpr(const string &); + +}; + + + +bool ExprEvaluator::isOperator(char chr) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(chr) != string::npos) + + return true; + + } + + return false; + +} + + + +int ExprEvaluator::opPriority(char oper) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(oper) != string::npos) + + return i; + + } + + return -1; + +} + + + +int ExprEvaluator::evaluateSubExpr(int operand1, int operand2, char oper) { + + if (oper == '/') { + + return operand1 / operand2; + + } else if (oper == '*') { + + return operand1 * operand2; + + } else if (oper == '-') { + + return operand1 - operand2; + + } else { + + return operand1 + operand2; + + } + +} + + + +ExprEvaluator::ExprEvaluator() { + + + + this->OPERATORS.push_back("/*"); + + this->OPERATORS.push_back("-+"); + +} + + + +int ExprEvaluator::evaluateExpr(const string & expr) { + + stack operands_stack; + + stack operators_stack; + + int expr_pos = 0; + + + + while (expr_pos < expr.size()) { + + + + if (isdigit(expr[expr_pos])) { + + + + int number = expr[expr_pos] - '0'; + + expr_pos += 1; + + + + while (expr_pos < expr.size() && isdigit(expr[expr_pos])) { + + number = number * 10 + expr[expr_pos] - '0'; + + expr_pos += 1; + + } + + + + operands_stack.push(number); + + + + } else if (this->isOperator(expr[expr_pos])) { + + + + if (operators_stack.empty() or operators_stack.top() == '(' or + + this->opPriority(expr[expr_pos]) < this->opPriority(operators_stack.top())) { + + + + operators_stack.push(expr[expr_pos]); + + + + } else { + + int priority = this->opPriority(expr[expr_pos]); + + + + while (!operators_stack.empty() && operators_stack.top() != '(' && + + priority >= this->opPriority(operators_stack.top())) { + + + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + operators_stack.push(expr[expr_pos]); + + } + + + + expr_pos += 1; + + + + } else if (expr[expr_pos] == '(') { + + + + operators_stack.push('('); + + expr_pos += 1; + + + + } else { + + + + while (operators_stack.top() != '(') { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + operators_stack.pop(); + + expr_pos += 1; + + + + } + + } + + + + while (!operators_stack.empty()) { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + return operands_stack.top(); + +} + + + +int main() { + + string expr; + + + + ifstream inputFile("evaluare.in"); + + inputFile >> expr; + + inputFile.close(); + + + + ExprEvaluator evaluator; + + int ans = evaluator.evaluateExpr(expr); + + + + ofstream outputFile("evaluare.out"); + + outputFile << ans; + + outputFile.close(); + + + + return 0; + +} diff --git a/eval/stack/f13a456b-a4f4-40cc-b0b6-23731a37c908.cpp b/eval/stack/f13a456b-a4f4-40cc-b0b6-23731a37c908.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35233635b7f8f7eaa64b7c8200c9e88defbf636b --- /dev/null +++ b/eval/stack/f13a456b-a4f4-40cc-b0b6-23731a37c908.cpp @@ -0,0 +1,208 @@ +#include +#include + + + + +int evaluate(int op1, int op2, char op) + +{ + + auto res = 0; + + switch(op) + + { + + case '+': + + res = op1 + op2; + + break; + + case '-': + + res = op1 - op2; + + break; + + case '*': + + res = op1 * op2; + + break; + + case '/': + + res = op1 / op2; + + break; + + } + + + + return res; + +} + + + +void evaluate(std::stack& st, std::stack& op) + +{ + + auto op2 = st.top(); + + st.pop(); + + auto op1 = st.top(); + + st.pop(); + + + + st.push(evaluate(op1, op2, op.top())); + + op.pop(); + +} + + + +int extractValue(const std::string& input, unsigned long long& i) + +{ + + auto value = 0; + + while((input[i] >= '0' && input[i] <= '9') && i < input.size()) + + { + + value = value*10 + input[i] - '0'; + + ++i; + + } + + --i; + + + + return value; + +} +int main() + +{ + + std::ifstream fin("evaluare.in"); + + std::ofstream fout("evaluare.out"); + + std::string input; + + fin>>input; + + + + std::stack st; + + std::stack op; + + for(auto i=0ULL; i < input.size(); ++i) + + { + + auto ch = input[i]; + + if(ch == '(' || ch == '*' || ch == '/') + + { + + op.push(ch); + + } + + if(ch >= '0' && ch <= '9') + + { + + st.push(extractValue(input, i)); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + if(ch == '+' || ch == '-') + + { + + if(!op.empty() && op.top() != '(') + + { + + evaluate(st, op); + + } + + op.push(ch); + + } + + if(ch == ')') + + { + + while(op.top() != '(') + + { + + evaluate(st, op); + + } + + op.pop(); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + + + } + + + + while(!op.empty()) + + { + + evaluate(st, op); + + + + } + + + + fout< +#include +#include +using namespace std; + + + +bool isDigit(char c) { + + if(c >= '0' && c <= '9') + + return true; + + return false; + +} + + + +int priority(char c) { + + if(c == '*' || c == '/') + + return 2; + + if(c == '+' || c == '-') + + return 1; + + return 0; + +} + + + +int operate(int n1, int n2, char c) { + + switch(c) { + + case '+': + + return n1 + n2; + + case '-': + + return n1 - n2; + + case '*': + + return n1 * n2; + + case '/': + + return n1 / n2; + + } + +} + + + +int main() { + + char expresie[100005], sign; + + int l, i, temp, n1, n2; + + stack nr; + + stack op; + + freopen("evaluare.in", "r", stdin); + + freopen("evaluare.out", "w", stdout); + + + + scanf("%s", expresie); + + l = strlen(expresie); + + + + for(i = 0; i < l; ++i) { + + if(priority(expresie[i])) { + + while(!op.empty() && priority(expresie[i]) <= priority(op.top())) { + + sign = op.top(); + + op.pop(); + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.push(expresie[i]); + + + + } + + else if(isDigit(expresie[i])) { + + temp = 0; + + while(isDigit(expresie[i]) && i < l) + + temp = 10*temp + expresie[i++] - '0'; + + nr.push(temp); + + i--; + + } + + else if(expresie[i] == '(') + + op.push('('); + + else if(expresie[i] == ')') { + + while(!op.empty() && op.top() != '(') { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + op.pop(); + + } + + } + + + + while(!op.empty()) { + + n2 = nr.top(); + + nr.pop(); + + n1 = nr.top(); + + nr.pop(); + + sign = op.top(); + + op.pop(); + + nr.push(operate(n1, n2, sign)); + + } + + printf("%d", nr.top()); + +} diff --git a/eval/stack/f2564f11-3736-48bf-b192-24fd543edab2.cpp b/eval/stack/f2564f11-3736-48bf-b192-24fd543edab2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f8f2bfefab9a36d0bfea0e1ec8d62ea8ba1d2ce --- /dev/null +++ b/eval/stack/f2564f11-3736-48bf-b192-24fd543edab2.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +stack semne; + +stack numere; + + + +char s[100050]; + + + +int prop(char c) + +{ + + if(c=='(')return 0; + + if(c=='+'||c=='-')return 1; + + if(c=='*'||c=='/')return 2; + + if(c=='~')return 3; + + return -1; + +} + + + + + +int main() + +{ + + char ll; + + ifstream fin("evaluare.in"); + + ofstream fout("evaluare.out"); + + fin.getline(s+1,100050); + + s[0]='('; + + int n=strlen(s); + + s[n]=')';s[++n]=0; + + int i=0,j,b,a; + + char numars[100]; + + while(i=0) + + + + {i++;semne.push('~');} + + else + + if(s[i]==')') + + { + + while(semne.top()!='(') + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + i++; + + semne.pop(); + + } + + else + + if(prop(s[i])>0) + + { + + while(prop(s[i])<=prop(ll=semne.top())) + + { + + if(semne.top()=='~') + + {semne.pop();numere.top()*=-1;} + + else if(semne.top()=='*') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b*a); + + } + + else if(semne.top()=='/') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b/a); + + } + + else if(semne.top()=='+') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b+a); + + } + + else if(semne.top()=='-') + + { + + a=numere.top();numere.pop(); + + b=numere.top();numere.pop(); + + semne.pop();numere.push(b-a); + + } + + } + + + + semne.push(s[i]); + + i++; + + } + + else + + { + + j=i; + + while(isdigit(s[j]))j++; + + strncpy(numars,s+i,j-i); + + numars[j-i]=0; + + numere.push(atoi(numars)); + + i=j; + + } + + } + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +class ExpresionEvaluator + +{ + +private: + + const int INF=1e9; + + int res; + + stack s; + + queue > q; + + + + int priorities[255]={0}; + + + + int getRes(int x,int y,char op) + + { + + switch(op) + + { + + case '+': + + return x+y; + + case '-': + + return x-y; + + case '*': + + return x*y; + + case '/': + + return x/y; + + } + + } + + + + bool isNumber(char c) + + { + + return (c<='9' && c>='0'); + + } + + + + int getNumber(char *&p) + + { + + int nr=0; + + while(isNumber(*p)) + + { + + nr=nr*10+(*p-'0'); + + ++p; + + } + + + + return nr; + + } + + + + void buildPostfix(char *v) + + { + + char *p=v; + + while(*p!='\0') + + { + + if(isNumber(*p)) + + q.push(make_pair(0,getNumber(p))); + + else + + { + + if(*p=='(') + + s.push(*p); + + else if(*p==')') + + { + + while(s.top()!='(') + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.pop(); + + + + } + + else + + { + + while(!s.empty() && priorities[s.top()]>=priorities[*p]) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + s.push(*p); + + } + + + + ++p; + + } + + } + + while(!s.empty()) + + { + + q.push(make_pair(1,s.top())); + + s.pop(); + + } + + } + + + +public: + + + + int evaluate() + + { + + if(res!=INF) + + return res; + + while(!q.empty()) + + { + + if(q.front().first==0) + + s.push(q.front().second); + + else + + { + + int y=s.top(); + + s.pop(); + + int x=s.top(); + + s.pop(); + + s.push(getRes(x,y,(char)q.front().second)); + + } + + + + q.pop(); + + } + + res=s.top(); + + s.pop(); + + return res; + + } + + + + void printPostfix() + + { + + queue >e=q; + + while(!e.empty()) + + { + + if(e.front().first) + + cout<<(char)e.front().second<<' '; + + else + + cout<>v; + + + + ExpresionEvaluator exp(v); + + g< + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char ss[100001]; + + + +int main(){ + + + + f >> ss; + + stack st, ops; + + + + for(int i = 0;ss[i];){ + + + + if(isdigit(ss[i])){ + + int Nr = 0; + + while(isdigit(ss[i])) + + Nr = Nr * 10 + (ss[i] - '0'), i++; + + st.push(Nr); + + i--; + + } + + + + if(ss[i] == '+') {i++; ops.push(1); continue;} + + if(ss[i] == '-') {i++; ops.push(1); st.push(-1); ops.push(3); continue;} + + if(ss[i] == '*') {i++; ops.push(3); continue;} + + if(ss[i] == '/') {i++; ops.push(4); continue;} + + if(ss[i] == '(') {i++; ops.push(5); continue;} + + + + if(ss[i] == ')') { + + while(ops.top() != 5){ + + int Nr = st.top(); + + st.pop(); + + st.top() += Nr; + + ops.pop(); + + } + + ops.pop(); + + } + + + + + + while(!ops.empty() && (ops.top() == 3 || ops.top() == 4)){ + + + + int Nr = st.top(); + + st.pop(); + + if(ops.top() == 3) st.top() *= Nr; + + if(ops.top() == 4) st.top() /= Nr; + + ops.pop(); + + } + + + + i++; + + } + + + + long long answ = 0; + + while(!st.empty()){ + + answ += st.top(); + + st.pop(); + + ops.pop(); + + } + + + + g << answ; + + + +} diff --git a/eval/stack/f48dbdc5-9714-4237-862a-bb2316567772.cpp b/eval/stack/f48dbdc5-9714-4237-862a-bb2316567772.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0efeb9a3b4f792420d5a6ba120e3b75d54bdc2a4 --- /dev/null +++ b/eval/stack/f48dbdc5-9714-4237-862a-bb2316567772.cpp @@ -0,0 +1,294 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +class ExprEvaluator { + +private: + + + + vector OPERATORS; + + bool isOperator(char); + + int opPriority(char); + + int evaluateSubExpr(int, int, char); + + + +public: + + + + ExprEvaluator(); + + int evaluateExpr(const string &); + +}; + + + +bool ExprEvaluator::isOperator(char chr) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(chr) != string::npos) + + return true; + + } + + return false; + +} + + + +int ExprEvaluator::opPriority(char oper) { + + for (int i = 0; i < this->OPERATORS.size(); i++) { + + if (this->OPERATORS[i].find(oper) != string::npos) + + return i; + + } + + return -1; + +} + + + +int ExprEvaluator::evaluateSubExpr(int operand1, int operand2, char oper) { + + if (oper == '/') { + + return operand1 / operand2; + + } else if (oper == '*') { + + return operand1 * operand2; + + } else if (oper == '-') { + + return operand1 - operand2; + + } else { + + return operand1 + operand2; + + } + +} + + + +ExprEvaluator::ExprEvaluator() { + + + + this->OPERATORS.push_back("/*"); + + this->OPERATORS.push_back("-+"); + +} + + + +int ExprEvaluator::evaluateExpr(const string & expr) { + + stack operands_stack; + + stack operators_stack; + + int expr_pos = 0; + + + + while (expr_pos < expr.size()) { + + + + if (isdigit(expr[expr_pos])) { + + + + int number = expr[expr_pos] - '0'; + + expr_pos += 1; + + + + while (expr_pos < expr.size() && isdigit(expr[expr_pos])) { + + number = number * 10 + expr[expr_pos] - '0'; + + expr_pos += 1; + + } + + + + operands_stack.push(number); + + + + } else if (this->isOperator(expr[expr_pos])) { + + + + if (operators_stack.empty() or operators_stack.top() == '(' or + + this->opPriority(expr[expr_pos]) < this->opPriority(operators_stack.top())) { + + + + operators_stack.push(expr[expr_pos]); + + + + } else { + + int priority = this->opPriority(expr[expr_pos]); + + + + while (!operators_stack.empty() && operators_stack.top() != '(' && + + priority >= this->opPriority(operators_stack.top())) { + + + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + operators_stack.push(expr[expr_pos]); + + } + + + + expr_pos += 1; + + + + } else if (expr[expr_pos] == '(') { + + + + operators_stack.push('('); + + expr_pos += 1; + + + + } else { + + + + while (operators_stack.top() != '(') { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + operators_stack.pop(); + + expr_pos += 1; + + + + } + + } + + + + while (!operators_stack.empty()) { + + char oper = operators_stack.top(); operators_stack.pop(); + + int operand2 = operands_stack.top(); operands_stack.pop(); + + int operand1 = operands_stack.top(); operands_stack.pop(); + + operands_stack.push( + + this->evaluateSubExpr(operand1, operand2, oper) + + ); + + } + + + + return operands_stack.top(); + +} + + + +int main() { + + string expr; + + + + ifstream inputFile("evaluare.in"); + + inputFile >> expr; + + inputFile.close(); + + + + ExprEvaluator evaluator; + + int ans = evaluator.evaluateExpr(expr); + + + + ofstream outputFile("evaluare.out"); + + outputFile << ans; + + outputFile.close(); + + + + return 0; + +} diff --git a/eval/stack/f4e58ef7-c6b5-4104-9090-9e6eaacaeb7c.cpp b/eval/stack/f4e58ef7-c6b5-4104-9090-9e6eaacaeb7c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..650b2d2b43301c9b326c4fcc75ab7b499bf18242 --- /dev/null +++ b/eval/stack/f4e58ef7-c6b5-4104-9090-9e6eaacaeb7c.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + +char s[100001]; + +int nr, k1, k2, rez, i, pr[300]; + +char st1[100001]; + +int st2[100001]; + +ifstream fin ("evaluare.in"); + +ofstream fout ("evaluare.out"); + + + +int operatie(int a, int b, char sgn) + +{ + + if (sgn == '+') { + + return a + b; + + } else if (sgn == '*') { + + return a * b; + + } else if (sgn == '/') { + + return a / b; + + } else if (sgn == '-') { + + return a - b; + + } + + + + return 0; + +} + + + +int main() { + + fin >> s; + + pr['+'] = 1; + + pr['-'] = 1; + + pr['('] = 0; + + pr[')'] = 0; + + pr['*'] = 2; + + pr['/'] = 2; + + + + for (int i = 0; s[i] != 0; i++) { + + if (s[i] >= '0' && s[i] <= '9') { + + nr = 0; + + while (s[i] >= '0' && s[i] <= '9') { + + nr = nr * 10 + s[i] - '0'; + + i++; + + } + + i--; + + st2[++k2] = nr; + + } else if (s[i] == '(') { + + st1[++k1] = '('; + + } else if (s[i] == ')') { + + while (st1[k1] != '(') { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + k1--; + + } else { + + while (k1 > 0 && pr[st1[k1]] >= pr[s[i]]) { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + st1[++k1] = s[i]; + + } + + } + + + + while (k1 > 0) { + + rez = operatie(st2[k2 - 1], st2[k2], st1[k1]); + + k1--; k2--; + + st2[k2] = rez; + + } + + fout << st2[1]; + + return 0; + +} diff --git a/eval/stack/f5558268-07fa-401b-934d-a97ee1eacc06.cpp b/eval/stack/f5558268-07fa-401b-934d-a97ee1eacc06.cpp new file mode 100644 index 0000000000000000000000000000000000000000..878a1ed593c5623dbc60af33776d75e7ea5c0e6d --- /dev/null +++ b/eval/stack/f5558268-07fa-401b-934d-a97ee1eacc06.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; + +void update(vector&num,char c) +{ + int b=num.back(); num.pop_back(); + int a=num.back(); num.pop_back(); + if(c=='+') num.push_back(a+b); + if(c=='-') num.push_back(a-b); + if(c=='*') num.push_back(a*b); + if(c=='/') num.push_back(a/b); +} + +int pr(char c) +{ + if(c=='(') return 0; else + if(c=='+' || c=='-') return 1; else + if(c=='*' || c=='/') return 2; +} + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + freopen("evaluare.in","r",stdin); + freopen("evaluare.out","w",stdout); + + vectornum,op; + seth={'+','-','/','*'}; + setnr={'0','1','2','3','4','5','6','7','8','9'}; + string s; + cin>>s; + for(int i=0;i=pr(s[i])) + update(num,op.back()),op.pop_back(); + op.push_back(s[i]); + } else + { + int val=0; + while(i +#include +#include + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< +#include + + + + +int evaluate(int op1, int op2, char op) + +{ + + auto res = 0; + + switch(op) + + { + + case '+': + + res = op1 + op2; + + break; + + case '-': + + res = op1 - op2; + + break; + + case '*': + + res = op1 * op2; + + break; + + case '/': + + res = op1 / op2; + + break; + + } + + + + return res; + +} + + + +void evaluate(std::stack& st, std::stack& op) + +{ + + auto op2 = st.top(); + + st.pop(); + + auto op1 = st.top(); + + st.pop(); + + + + st.push(evaluate(op1, op2, op.top())); + + op.pop(); + +} + + + +int extractValue(const std::string& input, unsigned long long& i) + +{ + + auto value = 0; + + while((input[i] >= '0' && input[i] <= '9') && i < input.size()) + + { + + value = value*10 + input[i] - '0'; + + ++i; + + } + + --i; + + + + return value; + +} +int main() + +{ + + std::ifstream fin("evaluare.in"); + + std::ofstream fout("evaluare.out"); + + std::string input; + + fin>>input; + + + + std::stack st; + + std::stack op; + + for(auto i=0ULL; i < input.size(); ++i) + + { + + auto ch = input[i]; + + if(ch == '(' || ch == '*' || ch == '/') + + { + + op.push(ch); + + } + + if(ch >= '0' && ch <= '9') + + { + + st.push(extractValue(input, i)); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + if(ch == '+' || ch == '-') + + { + + if(!op.empty() && op.top() != '(') + + { + + evaluate(st, op); + + } + + op.push(ch); + + } + + if(ch == ')') + + { + + while(op.top() != '(') + + { + + evaluate(st, op); + + } + + op.pop(); + + if(!op.empty() && (op.top() == '*' || op.top() =='/')) + + { + + evaluate(st, op); + + } + + } + + + + } + + + + while(!op.empty()) + + { + + evaluate(st, op); + + + + } + + + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +struct type + +{ + + union + + { + + int i; + + char c; + + }; + + char t; + +}; + + + +queue v; + +stack op; + + + +int main() + +{ + + char c[100000]; + + f>>c; + + char *p=c; + + type e; + + while(*p!='\00') + + { + + if(*p>='0' && *p<='9') + + { + + int x=0; + + while(*p>='0' && *p<='9') + + x=x*10+*p-'0',++p; + + e.t='i'; + + e.i=x; + + v.push(e); + + } + + else + + { + + if(op.empty()) + + op.push(*p); + + else + + switch(*p) + + { + + case '*': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '/': + + while(!op.empty() && (op.top()=='*' || op.top()=='/')) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '+': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '-': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.push(*p); + + break; + + case '(': + + op.push(*p); + + break; + + case ')': + + while(!op.empty() && op.top()!='(') + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + op.pop(); + + break; + + } + + ++p; + + } + + } + + while(!op.empty()) + + e.t='c',e.c=op.top(),v.push(e),op.pop(); + + stack st; + + queue aux=v; + + while(!aux.empty()) + + { + + if(aux.front().t=='i') + + cout< +#include +#include +#include + +std::string expr; + +int pos = 0; + +bool issign(char c) { + return c == '+' || c == '-' || c == '*' || c == '/'; +} + +int eval() { + std::vector stack; + + std::string number; + + char prev_sign = '+'; + int value; + + if (expr[pos] == '-') { + ++pos; + prev_sign = '-'; + } + + for (; pos < expr.size(); ++pos) { + if (isdigit(expr[pos])) { + number += expr[pos]; + } else if (issign(expr[pos])) { + if (!number.empty()) + value = std::stoi(number); + + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + prev_sign = expr[pos]; + } else if (expr[pos] == '(') { + ++pos; + value = eval(); + } else if (expr[pos] == ')') { + + if (!number.empty()) + value = std::stoi(number); + number.clear(); + + if (prev_sign == '*') { + stack.back() *= value; + } else if (prev_sign == '/') { + stack.back() /= value; + } else if (prev_sign == '+') { + stack.push_back(value); + } else if (prev_sign == '-') { + stack.push_back(-value); + } + + break; + } + } + + int ans = 0; + for (int el : stack) + ans += el; + + return ans; +} + +void read() { + std::ifstream in("evaluare.in"); + + std::getline(in, expr); + expr += '+'; + + in.close(); +} + +int main() { + read(); + + std::ofstream out("evaluare.out"); + out << eval() << '\n'; + + out.close(); + return 0; +} diff --git a/eval/stack/f931fcd3-aa3a-4ae2-a3e3-7839e563082a.cpp b/eval/stack/f931fcd3-aa3a-4ae2-a3e3-7839e563082a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a7f23a8b330a6129906b6ca953f8f3fa004898b --- /dev/null +++ b/eval/stack/f931fcd3-aa3a-4ae2-a3e3-7839e563082a.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +using namespace std; + + + +const int N = 100005; + + + +string a; + +int st[N], top; + + + +void Read () + +{ + + ifstream fin ("evaluare.in"); + + fin >> a; + + fin.close(); + +} + + + +void Solve () + +{ + + int i; + + for (i = 0; a[i];) + + { + + if (a[i] == '+') + + i++; + + else if (a[i] == '(') + + { + + st[++top] = 1e9 + 1; + + i++; + + } + + else if (a[i] == '*') + + { + + st[++top] = 1e9 + 2; + + i++; + + } + + else if (a[i] == '/') + + { + + st[++top] = 1e9 + 3; + + i++; + + } + + else if (a[i] == '-') + + { + + st[++top] = 1e9 + 4; + + i++; + + } + + else if ('0' <= a[i] && a[i] <= '9') + + { + + int x = 0; + + while ('0' <= a[i] && a[i] <= '9') + + { + + x = x * 10 + a[i] - '0'; + + i++; + + } + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + else + + { + + i++; + + int x = 0; + + while (st[top] != 1e9 + 1) + + { + + x += st[top]; + + top--; + + } + + top--; + + if (st[top] == 1e9 + 2) + + st[--top] *= x; + + else if (st[top] == 1e9 + 3) + + st[--top] /= x; + + else if (st[top] == 1e9 + 4) + + st[top] = -x; + + else st[++top] = x; + + } + + } + + int ans = 0; + + for (int i = 1; i <= top; i++) + + ans += st[i]; + + ofstream fout ("evaluare.out"); + + fout << ans << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + return 0; + +} diff --git a/eval/stack/f97e4cd4-f268-46c7-9d9d-0c062749fb32.cpp b/eval/stack/f97e4cd4-f268-46c7-9d9d-0c062749fb32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..538b31dc2fcefc2bd51b3558c674aa03b9f5cdd0 --- /dev/null +++ b/eval/stack/f97e4cd4-f268-46c7-9d9d-0c062749fb32.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +using namespace std; + +ifstream in("evaluare.in"); +ofstream out("evaluare.out"); + +bool isOperator(char op){ + return op=='+' || op=='-' || op=='*' || op=='/'; +} +bool isDigit(char ch){ + return ch>='0' && ch<='9'; +} + +int precedence(char op){ + switch(op){ + case '+': + return 1; + case '-': + return 1; + case '*': + return 2; + case '/': + return 2; + } + return 0; +} + +int applyOperator(char op,int a,int b){ + switch(op){ + case '+': + return a+b; + case '-': + return a-b; + case '*': + return a*b; + case '/': + return a/b; + } + return 0; +} + +int peek_integer(const string&expression,int&cursor){ + int result=0; + while(cursor ops; + + + while(cursor= prec){ + + result+=ops.top(); + ops.pop(); + } + + ops.push(here); + } + cursor++; + } + + + while(ops.size()){ + result+=ops.top(); + ops.pop(); + } + return result; +} + +int evaluate_postfix(const std::string&postfix){ + stack values; + int cursor=0; + + while(cursor>eval; + + out< +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + + + +char expr[100001]; + +map priority; + + + +struct elts { + + int val; + + int type; + +}; + + + +bool isDigit(char c) { + + return (c >= '0' && c <= '9'); + +} + + + +int getNumber(int& i) { + + int ret = 0; + + while (expr[i] != '\0' && isDigit(expr[i])) + + ret = ret * 10 + expr[i] - '0', ++i; + + --i; + + return ret; + +} + + + +bool isOperator(char c) { + + return (c == '+' || c == '-' || c == '*' || c == '/'); + +} + + + +int getRes(int a, int b, char op) { + + if (op == '+') + + return a + b; + + if (op == '-') + + return a - b; + + if (op == '*') + + return a * b; + + if (op == '/') + + return a / b; + +} + + + +int compute() { + + stack op; + + vector val; + + for (int i = 0;expr[i] != '\0';++i) { + + if (isDigit(expr[i])) + + val.push_back({ getNumber(i),0 }); + + else if (isOperator(expr[i])) { + + while (!op.empty() && op.top() != '(' && priority[op.top()] >= priority[expr[i]]) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.push(expr[i]); + + } + + else if (expr[i] == '(') + + op.push('('); + + else { + + while (op.top() != '(') { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + op.pop(); + + } + + + + } + + while (!op.empty()) { + + val.push_back({ op.top(),1 }); + + op.pop(); + + } + + + + stack res; + + + + for (int i = 0;i < val.size();++i) { + + if (val[i].type == 0) + + res.push(val[i].val); + + else { + + int a = res.top(); + + res.pop(); + + int b = res.top(); + + res.pop(); + + res.push(getRes(b, a, val[i].val)); + + } + + } + + return res.top(); + +} + + + +int main() + +{ + + fin >> expr; + + priority['+'] = 0; + + priority['-'] = 0; + + priority['*'] = 1; + + priority['/'] = 1; + + fout << compute(); + + + + return 0; + +} diff --git a/eval/stack/fb8b9338-8d66-4471-8e90-cedcbd544ce0.cpp b/eval/stack/fb8b9338-8d66-4471-8e90-cedcbd544ce0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d2042881084b2f120fd7e6a097975bbc6eeaae3 --- /dev/null +++ b/eval/stack/fb8b9338-8d66-4471-8e90-cedcbd544ce0.cpp @@ -0,0 +1,171 @@ +#include +#include +using namespace std; + + + +ifstream f("evaluare.in"); + +ofstream g("evaluare.out"); + + + +char c,semn[100000],st[100000]; + +int i,n,k,nr,gasitnr,fp[100000]; + +long long steval[1000]; + + + +int main(){ + + while(f >> c){ + + if(c >= '0' && c <= '9'){ + + gasitnr = 1; + + nr *= 10; + + nr += c-'0'; + + }else{ + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + if(c == '(' || c == '+' || c == '-' || c == '*' || c == '/'){ + + k++; + + st[k] = c; + + if(st[k] == '+' || st[k] == '-'){ + + while(st[k-1] == '+' || st[k-1] == '-' || st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + + } + + }else{ + + if(st[k] == '*' || st[k] == '/'){ + + while(st[k-1] == '*' || st[k-1] == '/'){ + + n++; + + semn[n] = st[k-1]; + + st[k-1] = st[k]; + + k--; + +; } + + } + + } + + }else{ + + if(c == ')'){ + + while(st[k] != '('){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k--; + + } + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n] = nr; + + nr = 0; + + gasitnr = 0; + + } + + while(k){ + + n++; + + semn[n] = st[k]; + + k--; + + } + + k = 0; + + for(int i = 1; i <= n; i++){ + + if(semn[i] != '+' && semn[i] != '-' && semn[i] != '*' && semn[i] != '/'){ + + k++; + + steval[k] = fp[i]; + + }else{ + + switch(semn[i]){ + + case '+': steval[k-1] = steval[k-1]+steval[k]; break; + + case '-': steval[k-1] = steval[k-1]-steval[k]; break; + + case '*': steval[k-1] = steval[k-1]*steval[k]; break; + + case '/': steval[k-1] = steval[k-1]/steval[k]; break; + + default: + + break; + + } + + k--; + + } + + } + + g << steval[1]; + + return 0; + +} diff --git a/eval/stack/fd9ba839-04cf-47fd-8d83-0b0bcef23710.cpp b/eval/stack/fd9ba839-04cf-47fd-8d83-0b0bcef23710.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1ee849344d71c328e9fb218d65ce22dcc53996a --- /dev/null +++ b/eval/stack/fd9ba839-04cf-47fd-8d83-0b0bcef23710.cpp @@ -0,0 +1,93 @@ +#include +#include + + + +using namespace std; + +stack postForm; +stack opStack; + +int priorities[256]; + +void setPriorities() { + priorities['+'] = 1; + priorities['-'] = 1; + priorities['*'] = 2; + priorities['/'] = 2; + priorities['('] = 0; +} + +int getNum(char *&s) { + int num = 0; + while (isdigit(*s)) { + num = num * 10 + *s - '0'; + s++; + } + return num; +} + +int eval(int x, int y, char op) { + switch (op) { + case '+': + return x + y; + case '-': + return x - y; + case '*': + return x * y; + case '/': + return x / y; + default: + return 0; + } +} + +void evaluateLast() { + char op = opStack.top(); + opStack.pop(); + int o2 = postForm.top(); + postForm.pop(); + int o1 = postForm.top(); + postForm.pop(); + postForm.push(eval(o1, o2, op)); +} + +void convert(char *s) { + while (*s) { + if (isdigit(*s)) { + postForm.push(getNum(s)); + } else { + if (*s == '(') { + opStack.push(*s); + s++; + } else if (*s == ')') { + while (opStack.top() != '(') { + evaluateLast(); + } + opStack.pop(); + s++; + } else { + char op = *s; + s++; + while (!opStack.empty() && priorities[opStack.top()] >= priorities[op]) { + evaluateLast(); + } + opStack.push(op); + } + } + } + while (!opStack.empty()) { + evaluateLast(); + } +} + +int main() { + ifstream f("evaluare.in"); + ofstream g("evaluare.out"); + setPriorities(); + char S[100005], *s = S; + f >> S; + convert(s); + g << postForm.top() << '\n'; + return 0; +} diff --git a/eval/stack/fde3a328-f149-4b54-ac0b-fbe8dca9bf7e.cpp b/eval/stack/fde3a328-f149-4b54-ac0b-fbe8dca9bf7e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af6e5223ea62954f44dd72ba26a26f7274e53692 --- /dev/null +++ b/eval/stack/fde3a328-f149-4b54-ac0b-fbe8dca9bf7e.cpp @@ -0,0 +1,224 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("evaluare.in"); + +ofstream fo("evaluare.out"); + +const int lmax=1e5; + +char C[lmax+5]; + +int n, nr; + +stack ops; + +stack val; + + + +bool eCifra(char c) + +{ + + return (c>='0' && c<='9'); + +} + + + +bool eSemn(char c) + +{ + + return (c=='+' || c=='-' || c=='*' || c=='/'); + +} + + + +int operatie(int a, int b, int op) + +{ + + switch (op) + + { + + case '+': + + return a+b; + + break; + + case '-': + + return a-b; + + break; + + case '*': + + return a*b; + + break; + + case '/': + + return a/b; + + } + +} + + + +int ordin(char c) + +{ + + if(c=='+' || c=='-') + + return 1; + + if(c=='*' || c=='/') + + return 2; + + return 3; + +} + + + +int main() + +{ + + C[0]='('; + + fi>>C+1; + + n=strlen(C+1); + + C[n+1]=')'; + + + + for(int i=0; i<=n+1; i++) + + { + + if(eCifra(C[i])) + + { + + nr=nr*10+(C[i]-'0'); + + if(!eCifra(C[i+1])) + + { + + val.push(nr); + + nr=0; + + } + + } + + else + + { + + if(C[i]=='(') + + ops.push(C[i]); + + else + + { + + if(C[i]==')') + + { + + while(ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.pop(); + + } + + else if(eSemn(C[i])) + + { + + if(C[i-1]=='(') + + { + + val.push(0); + + } + + while(ordin(ops.top())>=ordin(C[i]) && ops.top()!='(') + + { + + int b=val.top(); + + val.pop(); + + int a=val.top(); + + val.pop(); + + val.push(operatie(a, b, ops.top())); + + ops.pop(); + + } + + ops.push(C[i]); + + } + + } + + } + + } + + + + fo< + + +using namespace std; + +ifstream in("evaluare.in"); + +ofstream out("evaluare.out"); + + + +const int NMAX=100000; + +char c,semn[NMAX],st[NMAX]; + +int i,n,k,nr,gasitnr,fp[NMAX]; + +long long steval[100]; + + + +int main() + +{ + + while(in>>c){ + + if(c>='0'&&c<='9'){ + + gasitnr=1; + + nr=nr*10+(c-48); + + } + + else{ + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + if(c=='('||c=='+'||c=='-'||c=='*'||c=='/') + + { + + k++; + + st[k]=c; + + if(st[k]=='+'||st[k]=='-'){ + + while(st[k-1]=='+'||st[k-1]=='-'||st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + else + + if(st[k]=='*'||st[k]=='/'){ + + while(st[k-1]=='*'||st[k-1]=='/'){ + + n++; + + semn[n]=st[k-1]; + + st[k-1]=st[k]; + + k--; + + } + + } + + } + + else + + if(c==')'){ + + while(st[k]!='('){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k--; + + } + + } + + } + + if(gasitnr){ + + n++; + + fp[n]=nr; + + nr=0; + + gasitnr=0; + + } + + while(k){ + + n++; + + semn[n]=st[k]; + + k--; + + } + + k=0; + + for(i=1;i<=n;i++){ + + if(semn[i]!='+'&&semn[i]!='-'&&semn[i]!='*'&&semn[i]!='/'){ + + k++; + + steval[k]=fp[i]; + + } + + else + + { + + if(semn[i]=='+'){ + + steval[k-1]=steval[k-1]+steval[k]; + + } + + if(semn[i]=='-'){ + + steval[k-1]=steval[k-1]-steval[k]; + + } + + if(semn[i]=='*'){ + + steval[k-1]=steval[k-1]*steval[k]; + + } + + if(semn[i]=='/'){ + + steval[k-1]=steval[k-1]/steval[k]; + + } + + k--; + + } + + } + + out< + + +using namespace std; + +ifstream fin("evaluare.in"); + +ofstream fout("evaluare.out"); + +int i,v1,v2,rez; + +string str; + +stack val; + +stack s; + +int calc(int v1,int v2,char op) + +{ + + if(op=='+') return v2+v1; + + else if(op=='-') return v1-v2; + + else if(op=='*') return v1*v2; + + else return v1/v2; + +} + +int preced(char op) + +{ + + if(op=='-'||op=='+') return 1; + + if(op=='*'||op=='/') return 2; + + return 0; + +} + +int ev() + +{ + + + + char op; + +getline(fin,str); + + for(i=0; i=preced(str[i])) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + } + + s.push(str[i]); + + } + + } + + while(!s.empty()) + + { + + v2=val.top(); + + val.pop(); + + v1=val.top(); + + val.pop(); + + op=s.top(); + + s.pop(); + + val.push(calc(v1,v2,op)); + + + + } + + return val.top(); + + + +} + +int main() + +{fout< +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc (int a,int b) + +{ + + int r; + + if (a==0&&b==0) + + return -1; + + if (a==0||b==0) + + return max (a,b); + + r=a%b; + + while (r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + fin>>a>>b; + + if(cmmdc(a,b)!=1) + + fout< +using namespace std; + +class MyClass + +{ + +private: + + int a , b; + +public: + + MyClass(int x , int y) + + { + + a = x; + + b = y; + + } + + void cmmdc() + + { + + int r; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + } + + int geta() + + { + + if(a == 1) + + return 0; + + return a; + + } + +}; + +int main() + +{ + + freopen("cmmdc.in" , "r" , stdin); + + freopen("cmmdc.out" , "w" , stdout); + + int x , y; + + scanf("%d%d" , &x , &y); + + MyClass d(x , y); + + d.cmmdc(); + + printf("%d\n" , d.geta()); + + return 0; + +} diff --git a/gcd/Euclid-division/038eee90-456b-4926-90be-b5ac7de652f5.cpp b/gcd/Euclid-division/038eee90-456b-4926-90be-b5ac7de652f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b5e14a7236f13a51eb101494688596a94522ced --- /dev/null +++ b/gcd/Euclid-division/038eee90-456b-4926-90be-b5ac7de652f5.cpp @@ -0,0 +1,47 @@ +#include +#include + + +using namespace std; + + + +int main() + +{ + + fstream fin("cmmdc.in", ios::in); + + fstream fout("cmmdc.out", ios::out); + + int a, b, rest; + + fin >> a; + + fin >> b; + + while (b > 0) + + { + + rest = a % b; + + a = b; + + if (rest==0) + + if (b == 1) + + fout << 0; + + else + + fout << b; + + b = rest; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/0403ad22-5f8e-42a3-8a7d-c972a39f3c34.cpp b/gcd/Euclid-division/0403ad22-5f8e-42a3-8a7d-c972a39f3c34.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe85332899239c014b31c9344a5bed5805f20191 --- /dev/null +++ b/gcd/Euclid-division/0403ad22-5f8e-42a3-8a7d-c972a39f3c34.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + int r; + + while(a % b) { + + r = a % b; + + a = b; + + b = r; + + } + + if(b > 1) + + fout << b; + + else + + fout << 0; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/gcd/Euclid-division/071e7ef9-c2e0-4b4a-8aa4-e6b3d05c47bc.cpp b/gcd/Euclid-division/071e7ef9-c2e0-4b4a-8aa4-e6b3d05c47bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ad2e628ee2822edd70f7af80c3d027c718e0d1b --- /dev/null +++ b/gcd/Euclid-division/071e7ef9-c2e0-4b4a-8aa4-e6b3d05c47bc.cpp @@ -0,0 +1,42 @@ +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,r; + +int main() + +{ + + f>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) + + + + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + long long a, b, r; + + fin >> a >> b; + + while(b != 0) + + { + + r = a % b; + + a = b; + + b = r; + + } + + if(a == 1) fout << 0; + + else fout << a; + +} diff --git a/gcd/Euclid-division/07908e6b-40e8-412f-9ca1-ea9c4277c847.cpp b/gcd/Euclid-division/07908e6b-40e8-412f-9ca1-ea9c4277c847.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c14bced05f063397c92685594767695b11b3e263 --- /dev/null +++ b/gcd/Euclid-division/07908e6b-40e8-412f-9ca1-ea9c4277c847.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) + + fout< +using namespace std; + +typedef long long ll; + +ll const mod=1000000007; + +ll const md=998244353; + +ll mypowr(ll a,ll b) {ll res=1;if(b<0)b=0;a%=mod; assert(b>=0); + +for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} + +ll mypow(ll a,ll b) {ll res=1;if(b<0)b=0;assert(b>=0); + +for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); +int gcd(int a,int b){ + +if(b!=0){ + + return gcd(b,a%b); + +} + +if(a==1)a=0; + +return a; + + + +} + + + + + +int main() + +{ + + ios_base::sync_with_stdio(0); + + in.tie(0);out.tie(0); + + out<>a>>b; + + out< +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + +int cmmdc(int a,int b){ + +if (b==0) return a; + +return cmmdc(b,a%b); + +} + + + +int main() + +{int a,b; + +f>>a>>b; + +if(cmmdc(a,b)==1) g<<0; + +else g< +#include + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + if (b == 0) return a; + + else return cmmdc(b, a%b); + + + +} + + + + + +int main(){ + +ifstream fisier("cmmdc.in"); ofstream ofisier("cmmdc.out"); + +int x, y; + + fisier >> x; + + fisier >> y; + + if (cmmdc(x, y) == 1) ofisier << 0; + + else ofisier << cmmdc(x, y); + + return 0; + +} diff --git a/gcd/Euclid-division/0d7a78fe-99d7-4da3-a003-0a927c5a1b2a.cpp b/gcd/Euclid-division/0d7a78fe-99d7-4da3-a003-0a927c5a1b2a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1aa0331e1a75085e5c3809198eaeeb62f8b2b433 --- /dev/null +++ b/gcd/Euclid-division/0d7a78fe-99d7-4da3-a003-0a927c5a1b2a.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + + + +int cmmdc(int a, int b){ + + int r; + + + + while(b>0){ + + r=a%b; + + a=b; + + b=r; + + } + + + + return a; + +} + + + +int solve(int n){ + + int r; + + + + if(n==1) + + r=0; + + else + + r=n; + + + + return r; + +} + +int main(){ + + FILE *fin, *fout; + + + + fin=fopen("cmmdc.in", "r"); + + fout=fopen("cmmdc.out", "w"); + + + + int a, b; + + + + fscanf(fin, "%d%d", &a, &b); + + + + fprintf(fout, "%d\n", solve(cmmdc(a, b))); + + + + fclose(fin); + + fclose(fout); + + + + return 0; + +} diff --git a/gcd/Euclid-division/0f325b6b-40a9-4d37-983e-59511ea49939.cpp b/gcd/Euclid-division/0f325b6b-40a9-4d37-983e-59511ea49939.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2b7b0f3ee22ac849e33439bbfe84e6ef5468e1 --- /dev/null +++ b/gcd/Euclid-division/0f325b6b-40a9-4d37-983e-59511ea49939.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if(a==0) + + return b; + + return cmmdc(b%a,a); + +} + + + +int main() + +{ + + + + int a,b; + + fin>>a>>b; + + if(cmmdc(a,b)==1){ + + fout<<0; + + }else{ + + fout< + + +std::ifstream fin ("cmmdc.in"); + +std::ofstream fout ("cmmdc.out"); + + + +int main() + +{ + + + + int a{}; + + int b{}; + + + + fin >> a >> b; + + + + int r{}; + + + + while (b) + + { + + r = a%b; + + a = b; + + b=r; + + } + + + + if (a>1) + + fout << a; + + + + else + + fout << b; + + + + + + return 0; + + + +} diff --git a/gcd/Euclid-division/1297dd3e-7483-47ae-acb1-cc5f985a3e13.cpp b/gcd/Euclid-division/1297dd3e-7483-47ae-acb1-cc5f985a3e13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b29439a59833b3eb8518498bf8c09bed20a3ca60 --- /dev/null +++ b/gcd/Euclid-division/1297dd3e-7483-47ae-acb1-cc5f985a3e13.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a,b,r; + + f>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + g<<(a==1? 0 : a); + + f.close(); + + g.close(); + + return 0; +} diff --git a/gcd/Euclid-division/14371ce7-408e-4b6d-8679-86a7e598ae72.cpp b/gcd/Euclid-division/14371ce7-408e-4b6d-8679-86a7e598ae72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68c8457bc3050414d4151b011c75468dd270d552 --- /dev/null +++ b/gcd/Euclid-division/14371ce7-408e-4b6d-8679-86a7e598ae72.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int cmmdc(int a, int b){ + +while (b){ + + int r=a%b; + + a=b; + + b=r; + +} + +if (a==1) return 0; + +return a; + + + +} + +int main() + +{ + + int a, b; + + in>>a>>b; + +out< + + +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream ge("cmmdc.out"); + + + +bool primalitate(int x, int y) + +{ + + int r; + + while(y) + + { + + r=x%y; + + x=y; + + y=r; + + } + + if(x==1) return true; + + else return false; + + + +} + + + +int a,b; + +int main() + +{ + + + + fi>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(primalitate(a,b)==true) ge<<0; + + else ge< +#include +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int b, a, r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int main() + +{ + + int a, b; + + in >> a >> b; + + while (a %= b) + + std::swap(a, b); + + if (b == 1) + + b = 0; + + out << b; + +} diff --git a/gcd/Euclid-division/14e8a96e-137d-11eb-8761-309c234925a0.cpp b/gcd/Euclid-division/14e8a96e-137d-11eb-8761-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bc999cddd37fef7e77808b894a0a11703064556 --- /dev/null +++ b/gcd/Euclid-division/14e8a96e-137d-11eb-8761-309c234925a0.cpp @@ -0,0 +1,30 @@ +#include + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int rasp() + +{ + + int a, b; + + in >> a >> b; + + if (!b) + + return a; + + while (a %= b) + + std::swap(a, b); + + return b == 1? 0: b; + +} + +int main() + +{out << rasp();} diff --git a/gcd/Euclid-division/14f860ca-137d-11eb-ad21-309c234925a0.cpp b/gcd/Euclid-division/14f860ca-137d-11eb-ad21-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a82689279c747e96d8ef4ca085ce6ca93382839 --- /dev/null +++ b/gcd/Euclid-division/14f860ca-137d-11eb-ad21-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a,b,r; + +int main() + +{ + + + +fin>>a>>b; + +while(b!=0) + +{ + + r=a%b; + + a=b; + + b=r; + +} + +if(a==1) + + fout<<0; + +else fout< + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int rasp() + +{ + + int a, b; + + in >> a >> b; + + while (a %= b) + + std::swap(a, b); + + return b == 1? 0: b; + +} + +int main() + +{out << rasp();} diff --git a/gcd/Euclid-division/15347d18-137d-11eb-9cb6-309c234925a0.cpp b/gcd/Euclid-division/15347d18-137d-11eb-9cb6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8df28ea5bfde7609d730a11d3d1360c4cc00e68b --- /dev/null +++ b/gcd/Euclid-division/15347d18-137d-11eb-9cb6-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + fin>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else + + fout< +using namespace std; + + + +string problem = "cmmdc"; + +ifstream fin (problem + ".in"); + +ofstream fout(problem + ".out"); + + + + + + + +int gcd(int a,int b) + +{ + + if(!b) + + return a; + + return gcd(b,a % b); + +} + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + if(gcd(a,b) == 1) + + { + + fout << 0 << "\n"; + + return 0; + + } + + fout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/15da6814-137d-11eb-9de5-309c234925a0.cpp b/gcd/Euclid-division/15da6814-137d-11eb-9de5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23de0ee401a21f1d556060d5eacde11b86ae583f --- /dev/null +++ b/gcd/Euclid-division/15da6814-137d-11eb-9de5-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int t, x, y; + + + +int cmmdc(int x, int y) + +{ + + int r; + + while(y) + + { + + r = x % y; + + x = y; + + y = r; + + } + + return x; + +} + + + +int main() + +{ + + f >> x >> y; + + int t = cmmdc(x, y); + + + + if(t == 1) g << 0; + + else g << t; + + return 0; + +} diff --git a/gcd/Euclid-division/161c30ae-137d-11eb-a263-309c234925a0.cpp b/gcd/Euclid-division/161c30ae-137d-11eb-a263-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b73aeacfa5bb89c62b61db46136013916829342a --- /dev/null +++ b/gcd/Euclid-division/161c30ae-137d-11eb-a263-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int a,b; + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + else + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + fin>>a; + + fin>>b; + + int c=cmmdc(a,b); + + if(c==1) + + fout<<0; + + else + + fout< +using namespace std; + + + +string problem = "cmmdc"; + +ifstream fin (problem + ".in"); + +ofstream fout(problem + ".out"); + + + + + + + +int gcd(int a,int b) + +{ + + if(!b) + + return a; + + return gcd(b,a % b); + +} + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + if(gcd(a,b) == 1) + + { + + fout << 0 << "\n"; + + return 0; + + } + + fout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/165dc1d2-137d-11eb-8658-309c234925a0.cpp b/gcd/Euclid-division/165dc1d2-137d-11eb-8658-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7cdd6d14e169bfc3bb5c5944fccb6c46a733d1e --- /dev/null +++ b/gcd/Euclid-division/165dc1d2-137d-11eb-8658-309c234925a0.cpp @@ -0,0 +1,44 @@ +#include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + r = a % b; + + while(r > 0) + + { + + a = b; + + b = r; + + r = a % b; + + } + + if(b != 1) + + fout << b; + + else + + fout << 0; + + return 0; + +} diff --git a/gcd/Euclid-division/166bf2a2-137d-11eb-85b4-309c234925a0.cpp b/gcd/Euclid-division/166bf2a2-137d-11eb-85b4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebe62206f09dc0bac86afb54556cc75061188ae4 --- /dev/null +++ b/gcd/Euclid-division/166bf2a2-137d-11eb-85b4-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ long long a, b, r=0; + + fin >> a >> b; + + + + while(b) + + { r = a%b; + + a = b; + + b = r; + + + + } + + + + if( a == 1 ) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/16a9bc36-137d-11eb-af3e-309c234925a0.cpp b/gcd/Euclid-division/16a9bc36-137d-11eb-af3e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b12f76ee3b42e31d1704a049f3f6b073faface6 --- /dev/null +++ b/gcd/Euclid-division/16a9bc36-137d-11eb-af3e-309c234925a0.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int a,b; + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + else + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + fin>>a; + + fin>>b; + + int c=cmmdc(a,b); + + if(c==1) + + fout<<0; + + else + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + +vector Read(string path) + +{ + + std::ifstream file; + + file.open(path, std::ios::in); + + vector read; + + int ti; + + while (file >> ti) + + read.push_back(ti); + + file.close(); + + return read; + +} + +void Write(string path, vector data) + +{ + + std::ofstream file; + + file.open(path); + + for (int i = 0; i < data.size(); i++) + + { + + file << data[i] << " "; + + } + + file.close(); + +} + +void Write(string path, int data) + +{ + + std::ofstream file; + + file.open(path); + + file << data; + + file.close(); + +} + + + +void Swap(int* a, int* b) + +{ + + int ti = *a; + + *a = *b; + + *b = ti; + +} + + + +int GCD(int a, int b) + +{ + + while (b != 0) + + { + + int t = b; + + b = a % b; + + a = t; + + } + + return a; + +} + + + + + +int main() + +{ + + vector input = Read("cmmdc.in"); + + int gcd = GCD(input[0], input[1]); + + if (gcd == 1) + + gcd = 0; + + + + Write("cmmdc.out", gcd); + +} diff --git a/gcd/Euclid-division/16c7c0dc-137d-11eb-b771-309c234925a0.cpp b/gcd/Euclid-division/16c7c0dc-137d-11eb-b771-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94068bab0943aa366ebf5ba42a109ccba0771267 --- /dev/null +++ b/gcd/Euclid-division/16c7c0dc-137d-11eb-b771-309c234925a0.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +ifstream fin ("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int x,y; + + fin>>x>>y; + + while(y) + + { + + int r=x%y; + + x=y; + + y=r; + + } + + if(x!=1) + + fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + r = a % b; + + while(r) + + { + + a = b; + + b = r; + + r = a % b; + + } + + if(b == 1) + + fout << 0; + + else + + fout << b; + + return 0; + +} diff --git a/gcd/Euclid-division/174554f6-137d-11eb-915f-309c234925a0.cpp b/gcd/Euclid-division/174554f6-137d-11eb-915f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..523660cd1931d1311c8279112d30f5b83018ad52 --- /dev/null +++ b/gcd/Euclid-division/174554f6-137d-11eb-915f-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include + + +std::ifstream fin ("cmmdc.in"); + +std::ofstream fout ("cmmdc.out"); + + + +int main() + +{ + + + + int a{}; + + int b{}; + + + + fin >> a >> b; + + + + int r{}; + + + + while (b) + + { + + r = a%b; + + a = b; + + b=r; + + } + + + + if (a>1) + + fout << a; + + + + else + + fout << b; + + + + + + return 0; + + + +} diff --git a/gcd/Euclid-division/1766991a-137d-11eb-b628-309c234925a0.cpp b/gcd/Euclid-division/1766991a-137d-11eb-b628-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..831d2adc562ecebe7aece0c22c7c636dec5b175c --- /dev/null +++ b/gcd/Euclid-division/1766991a-137d-11eb-b628-309c234925a0.cpp @@ -0,0 +1,42 @@ +#include + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + if(b==1) + + return 0; + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + int a,b; + + f>>a>>b; + + g< + +using namespace std; +typedef long long ll; +typedef vector vi; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int a, b, r; + fin >> a >> b; + while(b!=0) { + r = a%b; + a=b; + b=r; + } + if(a==1) { + fout << 0; + return 0; + } + fout << a; + return 0; +} diff --git a/gcd/Euclid-division/17c88862-137d-11eb-b1a7-309c234925a0.cpp b/gcd/Euclid-division/17c88862-137d-11eb-b1a7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84c039ad1da542b93cbc62229030d00c00bc748 --- /dev/null +++ b/gcd/Euclid-division/17c88862-137d-11eb-b1a7-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int GCD(int a , int b){ + + if (!b) + + return a; + + return GCD(b,a%b); + +} + +int main() { + + int a, b; + + bool prime = false; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin >> a >> b; + + if ( GCD(a, b) != 1 ) { + + fout << GCD(a, b); + + prime = true; + + } + + if ( prime == false ) { + + fout << 0; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/18197636-137d-11eb-be4e-309c234925a0.cpp b/gcd/Euclid-division/18197636-137d-11eb-be4e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0deb8b28118075974fc3f90fb9aa39d4c7c677de --- /dev/null +++ b/gcd/Euclid-division/18197636-137d-11eb-be4e-309c234925a0.cpp @@ -0,0 +1,55 @@ +#include +using namespace std; + +int cmmdc(int a,int b) + +{ + + int r; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + return a; + +} + +int main() + +{ + freopen("cmmdc.in","r",stdin); + + freopen("cmmdc.out","w",stdout); + + + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + cout.tie(NULL); + + int a,b; + + cin>>a>>b; + + if(cmmdc(a,b)!=1) + + cout< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + + + int cmmdc_a, cmmdc_b, a, b,max; + + + + fin >> a >> b; + + + + + + if (a > b) { + + max = a; + + } + + else { + + max = b; + + } + + + + do { + + + + cmmdc_a = a % max; + + cmmdc_b = b % max; + + + + max--; + + + + } while (cmmdc_a!=0 || cmmdc_b!=0); + + + + + + max++; + + + + if (max == 1) { + + max--; + + } + + + + fout << max; + + + + cin.get(); + + return 0; + +} diff --git a/gcd/Euclid-division/185a97ae-137d-11eb-958b-309c234925a0.cpp b/gcd/Euclid-division/185a97ae-137d-11eb-958b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11f25844c97b68b2e4a7ab46d52a0a522ec61952 --- /dev/null +++ b/gcd/Euclid-division/185a97ae-137d-11eb-958b-309c234925a0.cpp @@ -0,0 +1,45 @@ +#include +#include + + +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a,b,r; + + in >> a >> b ; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a>1) out << a ; + + else out << 0 ; + + in.close(); + + out.close(); + + return 0; + +} diff --git a/gcd/Euclid-division/18677c86-b022-4b8e-bde6-998530fb08a8.cpp b/gcd/Euclid-division/18677c86-b022-4b8e-bde6-998530fb08a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac7dee30a35808f61fde5bcb6802ebedee8c3859 --- /dev/null +++ b/gcd/Euclid-division/18677c86-b022-4b8e-bde6-998530fb08a8.cpp @@ -0,0 +1,65 @@ +#include +#include +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + int t; + + while (b != 0) + + { + + t = b; + + b = a % b; + + a = t; + + } + + return a; + +} + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(a==b) + + out< +using namespace std; + +int a, b; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + f >> a >> b; + + while (b) { int r = a % b; a = b; b = r; } + + if (a == 1) g << 0; + + else g << a; + + f.close(); g.close(); + +} diff --git a/gcd/Euclid-division/1884ee8a-137d-11eb-96c4-309c234925a0.cpp b/gcd/Euclid-division/1884ee8a-137d-11eb-96c4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b164f316885a102d1a2c8bc3765120e03ec3248a --- /dev/null +++ b/gcd/Euclid-division/1884ee8a-137d-11eb-96c4-309c234925a0.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +FILE *f = fopen("cmmdc.in","r"); +FILE *g = fopen("cmmdc.out","w"); + +int gcd(int a,int b){ + if(!b) return a; + return gcd(b,a % b); +} + +int main(){ + + int a,b; + + fscanf(f,"%d %d",&a,&b); + + fprintf(g,"%d\n",gcd(a,b) == 1 ? 0:gcd(a,b)); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/gcd/Euclid-division/1893d09a-137d-11eb-bbea-309c234925a0.cpp b/gcd/Euclid-division/1893d09a-137d-11eb-bbea-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7c0c42180d54596a5c056393e216f400ba24a2b --- /dev/null +++ b/gcd/Euclid-division/1893d09a-137d-11eb-bbea-309c234925a0.cpp @@ -0,0 +1,26 @@ +#include "stdio.h" + +int cmmdc(int a, int b) +{ + if(b==0) + if(a==1) + return 0; + else + return a; + else + return cmmdc(b, a%b); +} + +int main() +{ + int a, b; + + FILE* in = fopen("cmmdc.in", "r"); + FILE* out = fopen("cmmdc.out", "w"); + + fscanf(in, "%d %d", &a, &b); + fprintf(out, "%d", cmmdc(a, b)); + + fclose(in); + fclose(out); +} diff --git a/gcd/Euclid-division/18a31348-137d-11eb-bb05-309c234925a0.cpp b/gcd/Euclid-division/18a31348-137d-11eb-bb05-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39707c35512985e9c788a9c81bf1fb0d7bc17387 --- /dev/null +++ b/gcd/Euclid-division/18a31348-137d-11eb-bb05-309c234925a0.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +typedef long long ll; + +ll const mod=1000000007; + +ll const md=998244353; + +ll mypowr(ll a,ll b) {ll res=1;if(b<0)b=0;a%=mod; assert(b>=0); + +for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} + +ll mypow(ll a,ll b) {ll res=1;if(b<0)b=0;assert(b>=0); + +for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); +int gcd(int a,int b){ + +if(b!=0){ + + return gcd(b,a%b); + +} + +if(a==1)a=0; + +return a; + + + +} + + + + + +int main() + +{ + + ios_base::sync_with_stdio(0); + + in.tie(0);out.tie(0); + + out<>a>>b; + + out< +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + + + +int cmmdc(int a, int b) { + + if (a == 0 || b == 0) return a; + + if (a % b == 0) return b; + + if (a != 0 && b != 0) { + + a = a % b; + + return cmmdc(b, a); + + } + + return 0; + +} + + + +int main() + +{ + + int a, b, r; + + in >> a; + + in >> b; + + + + if (a > b) + + r = cmmdc(a, b); + + else + + r = cmmdc(b, a); + + + + if (r == 1) out << 0; + + else out << r; + + return 0; + +} diff --git a/gcd/Euclid-division/18d4a6c2-137d-11eb-b6ce-309c234925a0.cpp b/gcd/Euclid-division/18d4a6c2-137d-11eb-b6ce-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81498c0191739f6726f4f81d2ac40ba9a53012c7 --- /dev/null +++ b/gcd/Euclid-division/18d4a6c2-137d-11eb-b6ce-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int euclid(int x,int y) + +{ + + if(y==0) + + return x; + + else + + return euclid(y,x%y); + + + +} + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,c; + + fin>>a>>b; + + c=euclid(a,b); + + if(c!=1) + + fout< +#include +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + int t; + + while (b != 0) + + { + + t = b; + + b = a % b; + + a = t; + + } + + return a; + +} + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(a==b) + + out< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,c; + +int main() + +{ + + f >> a >> b; + + c = a % b; + + while(c != 0) + + { + + a = b; + + b = c; + + c = a % b; + + } + + if(b == 1) + + b = 0; + + g << b; + + return 0; + +} diff --git a/gcd/Euclid-division/1b283c4c-137d-11eb-abd4-309c234925a0.cpp b/gcd/Euclid-division/1b283c4c-137d-11eb-abd4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85842e8dd51210e99c6b5176e27e3275950dea79 --- /dev/null +++ b/gcd/Euclid-division/1b283c4c-137d-11eb-abd4-309c234925a0.cpp @@ -0,0 +1,37 @@ +#include +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b, c; + + f >> a >> b; + + while(b) + + { + + c = a % b; + + a = b; + + b = c; + + } + + if(a == 1)g << 0; + + else g << a; + +} diff --git a/gcd/Euclid-division/1b49e692-137d-11eb-b25f-309c234925a0.cpp b/gcd/Euclid-division/1b49e692-137d-11eb-b25f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52bd508b941e3dc6e5327b1494e4d8c0b0becf3d --- /dev/null +++ b/gcd/Euclid-division/1b49e692-137d-11eb-b25f-309c234925a0.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r = a % b; + + while (r != 0) + + { + + a = b; + + b = r; + + r = a % b; + + } + + return b; + +} + + + +int main() + +{ + + int a, b; + + fin >> a >> b; + + if (cmmdc(a,b) == 1) + + fout << '0'; + + else + + fout << cmmdc(a, b); + + return 0; + +} diff --git a/gcd/Euclid-division/1b66964c-137d-11eb-99a4-309c234925a0.cpp b/gcd/Euclid-division/1b66964c-137d-11eb-99a4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebf75dac84feda3f8d9d218331a90027fe23f07f --- /dev/null +++ b/gcd/Euclid-division/1b66964c-137d-11eb-99a4-309c234925a0.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + if(a==0 && b!=0) + + { + + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a,b,c; + + fin>>a>>b; + + while(b) + + { + + c=a%b; + + a=b; + + b=c; + + } + + if(a==1) fout<<0; + + else fout< +using namespace std; +int cmmdc(int a, int b) +{ + if(b == 0) + return a; + else + return cmmdc(b, a%b); +} +int main() +{ + freopen("cmmdc.in", "r", stdin); + freopen("cmmdc.out", "w", stdout); + int a, b; + scanf("%d%d", &a, &b); + int c = cmmdc(a, b); + if(c == 1) + c = 0; + printf("%d", c); + return 0; +} diff --git a/gcd/Euclid-division/1ba424ac-137d-11eb-bd2e-309c234925a0.cpp b/gcd/Euclid-division/1ba424ac-137d-11eb-bd2e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4b5d72ca3a7fbff3f38fa23b5bc3c75b0f78e49 --- /dev/null +++ b/gcd/Euclid-division/1ba424ac-137d-11eb-bd2e-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include "bits/stdc++.h" +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a, b; + + cin>>a>>b; + + while(a!=0 && b!=0) + + { + + if(a>b) + + { + + a%=b; + + } + + else b%=a; + + } + + if (a+b==1) + + { + + cout <<0; + + } + + else cout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int64_t a, b, cmmdc(int64_t, int64_t); + +int main() + +{ + + f >> a >> b; + + if(cmmdc(a, b) == 1) + + g << 0; + + else + + g << cmmdc(a, b); + + return 0; + +} + +int64_t cmmdc(int64_t x, int64_t y) + +{ + + if(x % y == 0) + + return y; + + return cmmdc(y, x % y); + +} diff --git a/gcd/Euclid-division/1c15ee36-137d-11eb-9145-309c234925a0.cpp b/gcd/Euclid-division/1c15ee36-137d-11eb-9145-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90ed4413605d6dc36f0f4cf8ad7f114b0de6c839 --- /dev/null +++ b/gcd/Euclid-division/1c15ee36-137d-11eb-9145-309c234925a0.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fon("cmmdc.out"); + +int main(){ + +int a,b,r; + +fin>>a>>b; + +while(b!=0){ + + r = a % b; + + a = b; + + b = r;} + +if(a!=1) + +fon< +#include +using namespace std; + + + + + +int main() + +{ + + ifstream fin("cmmdc.in", ios::in); + + ofstream fout("cmmdc.out", ios::out); + + long a, b,r; + + fin >> a >> b; + + r = a % b; + + while (r != 0) + + { + + + + a = b; + + b = r; + + r = a % b; + + } + + if (b == 1) + + b = 0; + + fout << b; + +} diff --git a/gcd/Euclid-division/1c415b50-137d-11eb-870c-309c234925a0.cpp b/gcd/Euclid-division/1c415b50-137d-11eb-870c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55dda3620f9c1e4400ea498128b7658a367113b1 --- /dev/null +++ b/gcd/Euclid-division/1c415b50-137d-11eb-870c-309c234925a0.cpp @@ -0,0 +1,56 @@ +#include + + + +typedef long long ll; + + + +const ll mod=1e9+7; + + + +const int dx[] = {0, 1, 0, -1}; + +const int dy[] = {1, 0, -1, 0}; +using namespace std; + + + +ll t; + + + +int euclid(int a, int b) { + + if (!b)return a; + + else return euclid(b,a%b); + +} + + + +int main() { + +ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + ll a,b; + + cin >> a >> b; + + if (euclid(a,b)==1) { + + cout << "0\n"; + + } + + else cout << euclid(a,b)<< '\n'; + + + +} diff --git a/gcd/Euclid-division/1c60299c-137d-11eb-bc56-309c234925a0.cpp b/gcd/Euclid-division/1c60299c-137d-11eb-bc56-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c14bced05f063397c92685594767695b11b3e263 --- /dev/null +++ b/gcd/Euclid-division/1c60299c-137d-11eb-bc56-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) + + fout< + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if (b == 0) + + return (a == 1) ? 0 : a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + int a, b; + + fin >> a >> b; + + fout << cmmdc(a, b); + + + + return 0; + +} diff --git a/gcd/Euclid-division/1c7da2de-137d-11eb-90f2-309c234925a0.cpp b/gcd/Euclid-division/1c7da2de-137d-11eb-90f2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a71c6eb8b4e4976b3cb90c9ef23c8a44c5f0f65 --- /dev/null +++ b/gcd/Euclid-division/1c7da2de-137d-11eb-90f2-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int main() + +{ + + freopen("cmmdc.in","r",stdin); + + freopen("cmmdc.out","w",stdout); + + int n,m; + + scanf("%d%d",&n,&m); + + while(m != 0) + + { + + int r = n % m; + + n = m; + + m = r; + + } + + if(n!=1) + + printf("%d",n); + + else + + printf("0"); + + return 0; + +} diff --git a/gcd/Euclid-division/1c9afcc0-137d-11eb-a057-309c234925a0.cpp b/gcd/Euclid-division/1c9afcc0-137d-11eb-a057-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7174d98f622e557ec637c42cbb096758318879c --- /dev/null +++ b/gcd/Euclid-division/1c9afcc0-137d-11eb-a057-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int gcd(int a, int b) + +{ + + if (a % b == 0) + + return b; + + gcd(b, a % b); + +} + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + int r = gcd(a, b); + + cout << r; + + g << ((r == 1) ? 0 : r); + + return 0; + +} diff --git a/gcd/Euclid-division/1caa69e8-137d-11eb-8e19-309c234925a0.cpp b/gcd/Euclid-division/1caa69e8-137d-11eb-8e19-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b29439a59833b3eb8518498bf8c09bed20a3ca60 --- /dev/null +++ b/gcd/Euclid-division/1caa69e8-137d-11eb-8e19-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a,b,r; + + f>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + g<<(a==1? 0 : a); + + f.close(); + + g.close(); + + return 0; +} diff --git a/gcd/Euclid-division/1ce0f86c-137d-11eb-8e00-309c234925a0.cpp b/gcd/Euclid-division/1ce0f86c-137d-11eb-8e00-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34ab992f5b72baf54b9152a0497071457d34620f --- /dev/null +++ b/gcd/Euclid-division/1ce0f86c-137d-11eb-8e00-309c234925a0.cpp @@ -0,0 +1,44 @@ +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + + + +int main(){ + + long long a,b; + + in >> a >> b; + + while (b != 0) + + { + + int rest = a % b; + + a = b; + + b = rest; + + } + + if(a==1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-division/1cf36ef4-137d-11eb-8736-309c234925a0.cpp b/gcd/Euclid-division/1cf36ef4-137d-11eb-8736-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2de92048bca03b3fe25c20faa0d6c83e0bd3a3bc --- /dev/null +++ b/gcd/Euclid-division/1cf36ef4-137d-11eb-8736-309c234925a0.cpp @@ -0,0 +1,45 @@ +#include +#include + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ int r; + + while (b!=0) + + { r=a%b; + + a=b; + + b=r; + + } + + return a; + +} + + + +int main() + +{ int a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f >> a >> b; + + if (cmmdc(a,b)==1) g << "0"; + + else g << cmmdc(a,b); + + + +} diff --git a/gcd/Euclid-division/1d04d400-137d-11eb-b7be-309c234925a0.cpp b/gcd/Euclid-division/1d04d400-137d-11eb-b7be-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e34460e076f2c5390a259c3e7b5f3ac1dd4d5cd --- /dev/null +++ b/gcd/Euclid-division/1d04d400-137d-11eb-b7be-309c234925a0.cpp @@ -0,0 +1,50 @@ +#include + + +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a,b,rest; + + cin>>a>>b; + + while(b>0) + + { + + rest=a%b; + + a=b; + + b=rest; + + } + + if(a==1) + + { + + cout<<"0"; + + } + + else + + { + + cout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + int a, b; + + f>>a>>b; + + while(b!=0) + + { + + int r=a%b; + + a=b; + + b=r; + + + + } + + if(a!=1) + + g< +#include + + +using namespace std; + +ifstream i("cmmdc.in"); + +ofstream o("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + i>> a >> b; + + while(b > 0) + + { + + int rest = a%b; + + a = b; + + b = rest; + + + + } + + if (a==1) + + { + + o<< 0; + + } else + + { + + o<< a; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/1d9bce87-607e-495c-a29a-9abe699eead0.cpp b/gcd/Euclid-division/1d9bce87-607e-495c-a29a-9abe699eead0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a82689279c747e96d8ef4ca085ce6ca93382839 --- /dev/null +++ b/gcd/Euclid-division/1d9bce87-607e-495c-a29a-9abe699eead0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a,b,r; + +int main() + +{ + + + +fin>>a>>b; + +while(b!=0) + +{ + + r=a%b; + + a=b; + + b=r; + +} + +if(a==1) + + fout<<0; + +else fout< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + + + } + + if(a==1) + + a=0; + + out< +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) + + out<<0; + + else + + out< + + +using namespace std; + + + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + while(b) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if(a == 1) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/1ddd0b68-137d-11eb-8140-309c234925a0.cpp b/gcd/Euclid-division/1ddd0b68-137d-11eb-8140-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12f19b2ad6a73f6f14ab17593c637a7684692b43 --- /dev/null +++ b/gcd/Euclid-division/1ddd0b68-137d-11eb-8140-309c234925a0.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int x,int y){ + + if(x>n>>n2; + + int sol=cmmdc(n,n2); + + if(sol==1){ + + sol=0; + + } + + fout< +#include +using namespace std; + +ifstream f ("cmmdc.in"); + +ofstream g ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + f>>a>>b; + + if(a>b) + + { + + int aux; + + aux=a; + + a=b; + + b=aux; + + } + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + g<<0; + + else + + g< + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + a=0; + + out< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<"0"; + + else out< + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + if(b==1) + + return 0; + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + int a,b; + + f>>a>>b; + + g< +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int aux; + + while(b) + + { + + aux=b; + + b=a%b; + + a=aux; + + } + + if(a==1) return 0; + + return a; + +} + + + +int main() + +{ + + int a, b; + + in>>a>>b; + + out< +#include + + +int cmmdc(int a, int b) { + + if (b == 0) + + if (a == 1) + + return 0; + + else + + return a; + + else + + return cmmdc(b, a%b); + +} + + + +int main() + +{ + + std::ofstream out("cmmdc.out"); + + std::ifstream in("cmmdc.in"); + + int a, b; + + in >> a >> b; + + out << cmmdc(a, b); + + + +} diff --git a/gcd/Euclid-division/1ec3525f-d242-4330-95f2-d750251487c9.cpp b/gcd/Euclid-division/1ec3525f-d242-4330-95f2-d750251487c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c3dd735ce37a5a4f70e2e5680f508ea316b04c8 --- /dev/null +++ b/gcd/Euclid-division/1ec3525f-d242-4330-95f2-d750251487c9.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + out<<0; + + else out< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main(){ + + unsigned int a,b,r; + + fin>>a>>b; + + r=a%b; + + while(r){ + + r = a % b; + + a = b; + + b = r;} + + if(a==1)fout<<0; + + else fout< +#include + + + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a,b,x,y,r,i; + + f>>a>>b; + + x=a; + + y=b; + + r=x%y; + + while(r!=0) + + { + + x=y; + + y=r; + + r=x%y; + + } + + if(y==1) g<<"0"; + + else + + { + + for(i=a;i>=2;i--) + + { + + if(a%i==0) + + { + + if(b%i==0) + + { + + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a,b,c; + + fin>>a>>b; + + while(b) + + { + + c=a%b; + + a=b; + + b=c; + + } + + if(a==1) fout<<0; + + else fout< +using namespace std; + + + +unsigned gcd(unsigned a, unsigned b){ + + if (!b) return a; + + return gcd(b, a%b); + +} + + + +int main() { + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + + + unsigned a, b; + + cin >> a >> b; + + if (gcd(a,b)==1) + + cout << 0; + + else + + cout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/21239530-137d-11eb-ae2c-309c234925a0.cpp b/gcd/Euclid-division/21239530-137d-11eb-ae2c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deae47de5731ffb9966c47c7181d4af376003abb --- /dev/null +++ b/gcd/Euclid-division/21239530-137d-11eb-ae2c-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + + + +int main() + +{ + + ifstream xin("cmmdc.in"); + + ofstream xout("cmmdc.out"); + + int a,b,rest; + + xin>>a>>b; + + while(b){ + + rest=a%b; + + a=b; + + b=rest; + + } + + if(a==1){ + + xout<<0; + + }else{ + + xout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ int a,b,r; + + f>>a>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + +if(a!=1) + + g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ int a,b,r; + + f>>a>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) g< + + +using namespace std; + + + +int cmmdc(int a, int b){ + + int r; + + + + while(b>0){ + + r=a%b; + + a=b; + + b=r; + + } + + + + return a; + +} + + + +int solve(int n){ + + int r; + + + + if(n==1) + + r=0; + + else + + r=n; + + + + return r; + +} + +int main(){ + + FILE *fin, *fout; + + + + fin=fopen("cmmdc.in", "r"); + + fout=fopen("cmmdc.out", "w"); + + + + int a, b; + + + + fscanf(fin, "%d%d", &a, &b); + + + + fprintf(fout, "%d\n", solve(cmmdc(a, b))); + + + + fclose(fin); + + fclose(fout); + + + + return 0; + +} diff --git a/gcd/Euclid-division/21b613ba-137d-11eb-af41-309c234925a0.cpp b/gcd/Euclid-division/21b613ba-137d-11eb-af41-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac4c30b146a2cda348954e53cabb96a8a224474d --- /dev/null +++ b/gcd/Euclid-division/21b613ba-137d-11eb-af41-309c234925a0.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; + + + +unsigned gcd(unsigned a, unsigned b){ + + if (!b) return a; + + return gcd(b, a%b); + +} + + + +int main() { + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + + + unsigned a, b; + + cin >> a >> b; + + if (gcd(a,b)==1) + + cout << 0; + + else + + cout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/21c57d0a-137d-11eb-a37e-309c234925a0.cpp b/gcd/Euclid-division/21c57d0a-137d-11eb-a37e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5603695779221630bd47bc17eb8c7d3f4f09d239 --- /dev/null +++ b/gcd/Euclid-division/21c57d0a-137d-11eb-a37e-309c234925a0.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() +{ + int a , b ; + fin >> a >> b ; + while ( b != 0 ) + { + int rest = a % b ; + a = b ; + b = rest ; + } + if ( a == 1 ) fout << "0" ; + else fout << a ; + return 0; +} diff --git a/gcd/Euclid-division/21c6d863-d1a3-4a92-9729-c006743ae463.cpp b/gcd/Euclid-division/21c6d863-d1a3-4a92-9729-c006743ae463.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3776d44bb01c5b3086b46d6a386b72f2d6c3f8bb --- /dev/null +++ b/gcd/Euclid-division/21c6d863-d1a3-4a92-9729-c006743ae463.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + +long long cmmdc(long long a, long long b){ + + if(b==0) return a; + + else cmmdc(b,a%b); + +} + + + +int main() + +{ + + long long a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) out<<0; + + else out< + +using namespace std; + +int cmmdc(int a, int b) +{ + if (!b) + return a; + return cmmdc(b, a%b); +} + +int main() +{ + freopen("cmmdc.in","r",stdin); + freopen("cmmdc.out","w",stdout); + int a,b; + cin >> a >> b; + if (cmmdc(a,b)==1) + cout << "0"; + else cout << cmmdc(a,b); + return 0; +} diff --git a/gcd/Euclid-division/21f10058-137d-11eb-a268-309c234925a0.cpp b/gcd/Euclid-division/21f10058-137d-11eb-a268-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc515f7aedb9eda288eaa0899d15968df3a898ab --- /dev/null +++ b/gcd/Euclid-division/21f10058-137d-11eb-a268-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<"0"; + + else out< +#include + + +using namespace std; + + + +int cmmdc(int a, int b) { + + int r; + + + + while (b != 0) { + + r = a % b; + + a = b; + + b = r; + + } + + + + return a; + +} + + + +int main() { + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + unsigned int a, b; + + + + fin >> a >> b; + + + + int d = cmmdc(a, b); + + + + if (d != 1) + + fout << d; + + else + + fout << 0; + + + + return 0; + +} diff --git a/gcd/Euclid-division/22013f7a-a50e-4a08-a095-61df93fb6b54.cpp b/gcd/Euclid-division/22013f7a-a50e-4a08-a095-61df93fb6b54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69e2dd9635f3ea70b8980aa9a88e5467221e16b2 --- /dev/null +++ b/gcd/Euclid-division/22013f7a-a50e-4a08-a095-61df93fb6b54.cpp @@ -0,0 +1,27 @@ +#include +#include + +int gcd(int a, int b) +{ + int m; + while (b != 0) + { + m = b; + b = a % b; + a = m; + } + return a; +} + +int main() +{ + std::ifstream fin("cmmdc.in"); + std::ofstream fout("cmmdc.out"); + int a, b; + fin >> a >> b; + a = gcd(a, b); + if (a == 1) + fout << 0; + else + fout << a; +} diff --git a/gcd/Euclid-division/2236e466-137d-11eb-bbe1-309c234925a0.cpp b/gcd/Euclid-division/2236e466-137d-11eb-bbe1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d5c570036825bbb62e037b8841c71c76e973094 --- /dev/null +++ b/gcd/Euclid-division/2236e466-137d-11eb-bbe1-309c234925a0.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + + + +long int a, b, rest; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() { + + + + f >> a >> b; + + while (b != 0) { + + rest = a % b; + + a = b; + + b = rest; + + } + + if (a == 1) { + + g << 0; + + } + + else { + + g << a; + + } + + + + return 0; + +} diff --git a/gcd/Euclid-division/225b5c46-137d-11eb-b0f7-309c234925a0.cpp b/gcd/Euclid-division/225b5c46-137d-11eb-b0f7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eeb7d3052318d81a4867d64a53ed759ba1081357 --- /dev/null +++ b/gcd/Euclid-division/225b5c46-137d-11eb-b0f7-309c234925a0.cpp @@ -0,0 +1,67 @@ +#include +#include + + +int main() + +{ + + std::ifstream fisier_in("cmmdc.in"); + + std::ofstream fisier_out("cmmdc.out"); + + long a,b, cmmdc; + + fisier_in>>a; + + fisier_in>>b; + + fisier_in.close(); + + if(b > a) + + { + + while(a!=0) + + { + + int temp = a; + + a = b % a; + + b = temp; + + } + + if(b == 1) cmmdc = 0; + + else cmmdc = b; + + } + + else{ + + while(b != 0){ + + int temp = b; + + b = a % b; + + a = temp; + + } + + if(a == 1) cmmdc = 0; + + else cmmdc = a; + + } + + fisier_out< + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + +int a, b; + +int main() + +{ + + cin>>a>>b; + + if(b==0) + + { + + cout<<"0"; + + } + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + { + + cout<<"0"; + + } + + else + + { + + cout< +#include + + +using namespace std; + + + +long int a, b, rest; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() { + + + + f >> a >> b; + + while (b != 0) { + + rest = a % b; + + a = b; + + b = rest; + + } + + if (a == 1) { + + g << 0; + + } + + else { + + g << a; + + } + + + + return 0; + +} diff --git a/gcd/Euclid-division/22795158-137d-11eb-b985-309c234925a0.cpp b/gcd/Euclid-division/22795158-137d-11eb-b985-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10ff978966c4240f5c73405737b3ce95053cea25 --- /dev/null +++ b/gcd/Euclid-division/22795158-137d-11eb-b985-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,x; + + in>>a; + + in>>b; + + while(b!=0) + + { + + x=a%b; + + a=b; + + b=x; + + } + + if(a!=1) + + out< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin>>a>>b; + + r=a%b; + + while (r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if (b==1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +typedef unsigned long long ULL; + + + +ULL cmmdc(ULL a, ULL b) { + + unsigned long long rest; + + while(b) { + + rest = a % b; + + a = b; + + b = rest; + + } + + return a; + +} + + + +int main() { + + ifstream in("cmmdc.in"); + + + + ULL a, b; + + in >> a; + + in >> b; + + + + in.close(); + + + + ofstream out("cmmdc.out"); + + + + ULL result = cmmdc(a, b); + + if (result != 1) { + + out << result; + + } else { + + out << 0; + + } + + + + + + out.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-division/22b3fb9c-137d-11eb-95a7-309c234925a0.cpp b/gcd/Euclid-division/22b3fb9c-137d-11eb-95a7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5e889b5b7c4387781934555b4df941288faa475 --- /dev/null +++ b/gcd/Euclid-division/22b3fb9c-137d-11eb-95a7-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int main() + +{ + + unsigned long long a, b, r; + + cin >> a; + + cin >> b; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + if(a!=1)cout << a; + + else cout<<0; + + return 0; + +} diff --git a/gcd/Euclid-division/22c1f7a4-137d-11eb-ba21-309c234925a0.cpp b/gcd/Euclid-division/22c1f7a4-137d-11eb-ba21-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a74764f7130337b3776b11c166b31dacdb175f86 --- /dev/null +++ b/gcd/Euclid-division/22c1f7a4-137d-11eb-ba21-309c234925a0.cpp @@ -0,0 +1,47 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + +unsigned int a,b,r; + +int main() + +{fin>>a>>b; + +while(b) + +{r=a%b; + + a=b; + + b=r; + +} + +if(a!=1) + +{fout< +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + +int cmmdc(int a,int b){ + +if (b==0) return a; + +return cmmdc(b,a%b); + +} + + + +int main() + +{int a,b; + +f>>a>>b; + +if(cmmdc(a,b)==1) g<<0; + +else g< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,r; + +int main() + +{ + + f>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) + + + + g< +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + +int cmmdc(int a,int b){ + +if (b==0) return a; + +return cmmdc(b,a%b); + +} + + + +int main() + +{int a,b; + +f>>a>>b; + +if(cmmdc(a,b)==1) g<<0; + +else g< +#include + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + if (b == 0) return a; + + else return cmmdc(b, a%b); + + + +} + + + + + +int main(){ + +ifstream fisier("cmmdc.in"); ofstream ofisier("cmmdc.out"); + +int x, y; + + fisier >> x; + + fisier >> y; + + if (cmmdc(x, y) == 1) ofisier << 0; + + else ofisier << cmmdc(x, y); + + return 0; + +} diff --git a/gcd/Euclid-division/236d4d88-137d-11eb-97d4-309c234925a0.cpp b/gcd/Euclid-division/236d4d88-137d-11eb-97d4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2b7b0f3ee22ac849e33439bbfe84e6ef5468e1 --- /dev/null +++ b/gcd/Euclid-division/236d4d88-137d-11eb-97d4-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if(a==0) + + return b; + + return cmmdc(b%a,a); + +} + + + +int main() + +{ + + + + int a,b; + + fin>>a>>b; + + if(cmmdc(a,b)==1){ + + fout<<0; + + }else{ + + fout< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + r = a % b; + + while(r > 0) + + { + + a = b; + + b = r; + + r = a % b; + + } + + if(b != 1) + + fout << b; + + else + + fout << 0; + + return 0; + +} diff --git a/gcd/Euclid-division/23a1921a-137d-11eb-b11c-309c234925a0.cpp b/gcd/Euclid-division/23a1921a-137d-11eb-b11c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c56de739c47ba6449005055cf386d0078ec79ab6 --- /dev/null +++ b/gcd/Euclid-division/23a1921a-137d-11eb-b11c-309c234925a0.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; + +int main() +{ + freopen ("cmmdc.in","r",stdin); + freopen ("cmmdc.out","w",stdout); + int a, b, rest; + cin >> a >> b; + while(b != 0){ + rest = a % b ; + a = b; + b = rest; + } + if (a == 1) + a = 0; + cout << a; + return 0; +} diff --git a/gcd/Euclid-division/23c193ee-e3db-4376-9b22-3a409bc8a9e8.cpp b/gcd/Euclid-division/23c193ee-e3db-4376-9b22-3a409bc8a9e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..520dd203602b96663dfe45b4e01612b2360b9f97 --- /dev/null +++ b/gcd/Euclid-division/23c193ee-e3db-4376-9b22-3a409bc8a9e8.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int d,i,r; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>d>>i; + + while(i) + + { + + r=d%i; + + d=i; + + i=r; + + } + + if(d==1) + + g<<"0"; + + else + + g< +using namespace std; +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int main() { + int a,b; + f>>a>>b; + while(b!=0){ + int c=a%b; + a=b; + b=c; + } + if(a==1) + g<<0; + else g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + f>>a>>b; + + while (b!=0) + + { + + int r=a%b; + + a=b; + + b=r; + + } + + if (a==1) + + g<<0; + + else + + g< + + +int cmmdc(int a, int b) { + + if(b == 0) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + int a, b; + + freopen("cmmdc.in", "r", stdin); + + freopen("cmmdc.out", "w", stdout); + + scanf("%d%d", &a, &b); + + if(cmmdc(a, b) > 1) + + printf("%d", cmmdc(a, b)); + + else + + printf("0"); + +} diff --git a/gcd/Euclid-division/241a6a52-137d-11eb-9478-309c234925a0.cpp b/gcd/Euclid-division/241a6a52-137d-11eb-9478-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..520dd203602b96663dfe45b4e01612b2360b9f97 --- /dev/null +++ b/gcd/Euclid-division/241a6a52-137d-11eb-9478-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int d,i,r; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>d>>i; + + while(i) + + { + + r=d%i; + + d=i; + + i=r; + + } + + if(d==1) + + g<<"0"; + + else + + g< +#include +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + +long long cmmdc(long long a, long long b){ + + if(b==0) return a; + + else cmmdc(b,a%b); + +} + + + +int main() + +{ + + long long a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) out<<0; + + else out< +#include +using namespace std; + +int a,b; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{int rest; + + f>>a>>b; + + while(b!=0) + + {rest=a%b; + + a=b;b=rest; + + } + + if(a==1)g<<0; + + else g< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,p; + +int main() + +{ + + f>>a>>b; + + while(a%b!=0){p=a%b; + + a=b; + + b=p; + + } + + if(p!=1)g< +#include + + +using namespace std; + + + +int main() + +{ + + fstream fin("cmmdc.in", ios::in); + + fstream fout("cmmdc.out", ios::out); + + int a, b, rest; + + fin >> a; + + fin >> b; + + while (b > 0) + + { + + rest = a % b; + + a = b; + + if (rest==0) + + if (b == 1) + + fout << 0; + + else + + fout << b; + + b = rest; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/249a9068-137d-11eb-9dce-309c234925a0.cpp b/gcd/Euclid-division/249a9068-137d-11eb-9dce-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33d71f1d6cf664b8c5370a50f502265520e1d6c3 --- /dev/null +++ b/gcd/Euclid-division/249a9068-137d-11eb-9dce-309c234925a0.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + + + +int main() + +{ + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + int x, y, r; + + + + fin>>x>>y; + + + + while(y != 0) + + { + + r=x % y; + + x = y; + + y = r; + + } + + + + if(x != 1) + + fout< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a, b, c; + + + +int cmmdc(int a, int b) + +{ + + if(!b) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() + +{ + + f>>a>>b; + + c = cmmdc(a, b); + + if(c == 1) + + g<<0; + + else + + g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main () + +{ + + int a,b,r; + + fin>>a>>b; + + while(b>0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + { + + fout<<0; + + } + + else + + { + + fout< + + +inline int cmmdc (int a, int b) { + + if (!b) return a; + + return cmmdc(b, a%b); + +} + + + +int main () { + + FILE *fin=fopen ("cmmdc.in", "r"), + + *fout=fopen ("cmmdc.out", "w"); + + int a, b; + + fscanf (fin, "%d%d", &a, &b); + + a=cmmdc(a, b); + + fprintf (fout, "%d", a==1?0:a); + + return 0; + +} diff --git a/gcd/Euclid-division/25bb0a12-137d-11eb-8beb-309c234925a0.cpp b/gcd/Euclid-division/25bb0a12-137d-11eb-8beb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68c8457bc3050414d4151b011c75468dd270d552 --- /dev/null +++ b/gcd/Euclid-division/25bb0a12-137d-11eb-8beb-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int cmmdc(int a, int b){ + +while (b){ + + int r=a%b; + + a=b; + + b=r; + +} + +if (a==1) return 0; + +return a; + + + +} + +int main() + +{ + + int a, b; + + in>>a>>b; + +out< +#include + + +using namespace std; + + + +int main() + +{ + + int a,b,r; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + + + while(b!=0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if(a==1) + + g<<0; + + else + + g< +using namespace std; + +class MyClass + +{ + +private: + + int a , b; + +public: + + MyClass(int x , int y) + + { + + a = x; + + b = y; + + } + + void cmmdc() + + { + + int r; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + } + + int geta() + + { + + if(a == 1) + + return 0; + + return a; + + } + +}; + +int main() + +{ + + freopen("cmmdc.in" , "r" , stdin); + + freopen("cmmdc.out" , "w" , stdout); + + int x , y; + + scanf("%d%d" , &x , &y); + + MyClass d(x , y); + + d.cmmdc(); + + printf("%d\n" , d.geta()); + + return 0; + +} diff --git a/gcd/Euclid-division/2616830c-137d-11eb-a36a-309c234925a0.cpp b/gcd/Euclid-division/2616830c-137d-11eb-a36a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c44b85ae09beab303a2c7db785f54d4a0a7a8c7a --- /dev/null +++ b/gcd/Euclid-division/2616830c-137d-11eb-a36a-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +long long a, b; + + + +int main() + +{ + + long long r; + + fin >> a >> b; + + while(b != 0) + + { + + r = a % b; + + a = b; + + b = r; + + } + + + + if(a == 1) fout << 0; + + else fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/26263a7e-137d-11eb-bc7d-309c234925a0.cpp b/gcd/Euclid-division/26263a7e-137d-11eb-bc7d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9923d425e8552cfdeaa16360b862a8ae80c902d --- /dev/null +++ b/gcd/Euclid-division/26263a7e-137d-11eb-bc7d-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int main(){ + + FILE *fin, *fout; + + int a, b, r; + + fin=fopen("cmmdc.in", "r"); + + fout=fopen("cmmdc.out", "w"); + + fscanf(fin, "%d%d", &a, &b); + + while(b>0){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + a=0; + + fprintf(fout, "%d\n", a); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/gcd/Euclid-division/2634ca46-137d-11eb-8900-309c234925a0.cpp b/gcd/Euclid-division/2634ca46-137d-11eb-8900-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ced7d1cb443cac59da931e7a259b7cc9dab922b --- /dev/null +++ b/gcd/Euclid-division/2634ca46-137d-11eb-8900-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int euclid(int a, int b) + +{ + + if (!b) + + return a; + + return euclid(b, a % b); + +} + + + +int main() + +{ + + int x, y; + + in >> x >> y; + + if (euclid(x, y) == 1) + + out << 0; + + else + + out << euclid(x, y); + + + + return 0; + + + +} diff --git a/gcd/Euclid-division/263bdb8a-38dc-4e0f-8d84-81fb0933db4a.cpp b/gcd/Euclid-division/263bdb8a-38dc-4e0f-8d84-81fb0933db4a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21ad43a838fa19fa53f9c7564983a6033741b55c --- /dev/null +++ b/gcd/Euclid-division/263bdb8a-38dc-4e0f-8d84-81fb0933db4a.cpp @@ -0,0 +1,38 @@ +#include + + +std::ifstream cin("cmmdc.in"); + +std::ofstream cout("cmmdc.out"); + + + +long long cmmdc(long long a, long long b){ + + if(b == 0) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() + +{ + + long long a{}, b{}; + + cin >> a >> b; + + long long c = cmmdc(a, b); + + if(c == 1) + + cout << 0; + + else cout << c; + +} diff --git a/gcd/Euclid-division/264481b6-137d-11eb-8a4c-309c234925a0.cpp b/gcd/Euclid-division/264481b6-137d-11eb-8a4c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a82502224364288cb008f1f5cb8fa95b4a6b2fd --- /dev/null +++ b/gcd/Euclid-division/264481b6-137d-11eb-8a4c-309c234925a0.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +class MyClass + +{ + +private: + + int a , b; + +public: + + MyClass(int x , int y) + + { + + a = x; + + b = y; + + } + + void cmmdc() + + { + + int r; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + } + + int geta() + + { + + if(a == 1) + + return 0; + + return a; + + } + +}; + +int main() + +{ + + freopen("cmmdc.in" , "r" , stdin); + + freopen("cmmdc.out" , "w" , stdout); + + int a , b; + + scanf("%d%d" , &a , &b); + + MyClass d(a , b); + + d.cmmdc(); + + printf("%d\n" , d.geta()); + + return 0; + +} diff --git a/gcd/Euclid-division/26975cbe-137d-11eb-8fd0-309c234925a0.cpp b/gcd/Euclid-division/26975cbe-137d-11eb-8fd0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb203a95a5a32478033cdc4cbd3787bd35277c2 --- /dev/null +++ b/gcd/Euclid-division/26975cbe-137d-11eb-8fd0-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) + + out<<0; + + else + + out< +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fi>>a>>b; + + while (b != 0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + fo<<0; + + else + + fo< + +using namespace std; + +ifstream in("cmmdc.in"); +ofstream out("cmmdc.out"); + +int a, b, r; + +int main() +{ + in>>a>>b; + while(b) + { + r = a % b; + a = b; + b = r; + } + if(a == 1) + out<<0; + else + out< +#include +using namespace std; +int cmmdc(int a, int b){ + if(!b) return a; + return cmmdc(b, a%b); +} +int main() +{ + ifstream in("cmmdc.in"); + ofstream out("cmmdc.out"); + int a, b; + in>>a>>b; + int gcd = cmmdc(a, b); + if(gcd==1) + out<<0; + else + out< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int euclid(int a,int b) + +{ + + if (b==0) + + return a; + + return euclid(b,a%b); + +} + +int main() + +{ + + int a,b,x; + + fin>>a>>b; + + x=euclid(a,b); + + if (x==1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if(!b) + + return a; + + return cmmdc(b, a%b); + +} + + + +int main() { + + int a, b; + + f>>a>>b; + + + + if(cmmdc(a, b) == 1) + + fo << 0; + + else + + fo << cmmdc(a, b); + +} diff --git a/gcd/Euclid-division/270bc950-137d-11eb-af89-309c234925a0.cpp b/gcd/Euclid-division/270bc950-137d-11eb-af89-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb203a95a5a32478033cdc4cbd3787bd35277c2 --- /dev/null +++ b/gcd/Euclid-division/270bc950-137d-11eb-af89-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) + + out<<0; + + else + + out< +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fi>>a>>b; + + while (b != 0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + fo<<0; + + else + + fo< +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int CMMDC(int a, int b) + +{ + + int c; + + while (b) + + { + + c = a % b; + + a = b; + + b = c; + + } + + return a; + +} + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + if (CMMDC(a, b) == 1) + + g << 0; + + else + + g << CMMDC(a, b); + + return 0; + +} diff --git a/gcd/Euclid-division/2737eba6-137d-11eb-8278-309c234925a0.cpp b/gcd/Euclid-division/2737eba6-137d-11eb-8278-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..953f303003d591595a7115b9f9f28834dc4a1f3d --- /dev/null +++ b/gcd/Euclid-division/2737eba6-137d-11eb-8278-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + + + +ifstream cin ("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if (b == 0) + + return a; + + else + + return cmmdc(b, a%b); + +} + + + +int main() + +{ + + int a, b; + + cin >> a >> b; + + if (cmmdc(a, b) == 1) + + cout << "0"; + + else + + cout << cmmdc(a, b); + + + +} diff --git a/gcd/Euclid-division/281fe40a-137d-11eb-8e19-309c234925a0.cpp b/gcd/Euclid-division/281fe40a-137d-11eb-8e19-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..922459243fd2d165e182a31599abd94ff577761a --- /dev/null +++ b/gcd/Euclid-division/281fe40a-137d-11eb-8e19-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + long long a, b, r; + + fin >> a >> b; + + while(b != 0) + + { + + r = a % b; + + a = b; + + b = r; + + } + + if(a == 1) fout << 0; + + else fout << a; + +} diff --git a/gcd/Euclid-division/2830acb8-137d-11eb-8649-309c234925a0.cpp b/gcd/Euclid-division/2830acb8-137d-11eb-8649-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39615441fe6cda21ed4d7136ad68ef7a1f62a76a --- /dev/null +++ b/gcd/Euclid-division/2830acb8-137d-11eb-8649-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + r=a%b; + + while(r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + out<<0; + + else + + out< + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int a, b; + + + +int cmmdc(int a, int b) { + + if (b == 0) return a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + in >> a >> b; + + int ans = (cmmdc(a, b) == 1) ? 0 : cmmdc(a, b); + + out << ans; + + return 0; + +} diff --git a/gcd/Euclid-division/285db036-137d-11eb-8b89-309c234925a0.cpp b/gcd/Euclid-division/285db036-137d-11eb-8b89-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dde9ddd7901647c3ddce21e6922a1ccb37595465 --- /dev/null +++ b/gcd/Euclid-division/285db036-137d-11eb-8b89-309c234925a0.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int a, b, r; + +int main() { + + in >> a >> b; + + while (b != 0) { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-division/286d196e-137d-11eb-85d6-309c234925a0.cpp b/gcd/Euclid-division/286d196e-137d-11eb-85d6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10d73688871fd9128a090de38bccc7fce9553f71 --- /dev/null +++ b/gcd/Euclid-division/286d196e-137d-11eb-85d6-309c234925a0.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main () + +{int a,b,r; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +f>>a; + +f>>b; + +do{r=a%b; + +a=b; + +b=r;}while (b>0); + +if(a==1) g<<0; + +else g< using namespace std; int a,b,r; int main() { ifstream in("cmmdc.in"); ofstream out("cmmdc.out"); in>>a>>b; while(b) { r=a%b; a=b; b=r; } if(a==1) out<<0; else out< +using namespace std; + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + out<<0; + + else out< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a, b, r, ca, cb; + +int main() + +{ + + fin>>a>>b; + + ca=a; + + cb=b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(ca==1 || cb==1) + + fout<<1; + + else + + { + + if(a==1) + + fout<<0; + + else + + fout< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,c; + + in>>a>>b; + + while (b) + + { + + c=a%b; + + a=b; + + b=c; + + } + + if (a==1) + + out <<0; + + else + + out < + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if (b == 0) + + return a; + + return cmmdc(b, a % b); + + + +} + +int main() { + + + + int a, b; + + fin >> a >> b; + + + + int CMMDC = cmmdc(a, b); + + if (CMMDC == 1) + + fout << '0'; + + else + + fout << CMMDC; + + + + return 0; + +} diff --git a/gcd/Euclid-division/291c3100-137d-11eb-9c7e-309c234925a0.cpp b/gcd/Euclid-division/291c3100-137d-11eb-9c7e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd40e6f534cebe1b45a163c290313412422ee64a --- /dev/null +++ b/gcd/Euclid-division/291c3100-137d-11eb-9c7e-309c234925a0.cpp @@ -0,0 +1,103 @@ +#include +#include + + + + +using namespace std; + + + +long long int a,b,r,aux; + + + +int main() + + + +{ + + + + ifstream fin("cmmdc.in"); + + + + ofstream fout("cmmdc.out"); + + + + fin>>a; + + + + fin>>b; + + + + if (b>a) + + + + { + + + + aux=b; + + + + b=a; + + + + a=aux; + + + + } + + + + r=a%b; + + + + while (r) + + + + { + + + + a=b; + + + + b=r; + + + + r=a%b; + + + + } + + if (b==1) + + fout<<"0"; + + else + + fout< +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + int r; + + while(a % b) { + + r = a % b; + + a = b; + + b = r; + + } + + if(b > 1) + + fout << b; + + else + + fout << 0; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/gcd/Euclid-division/294822f8-137d-11eb-baf5-309c234925a0.cpp b/gcd/Euclid-division/294822f8-137d-11eb-baf5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ea8da55875e189f5a63be74385d95be427938ec --- /dev/null +++ b/gcd/Euclid-division/294822f8-137d-11eb-baf5-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc (int a,int b) + +{ + + int r; + + if (a==0&&b==0) + + return -1; + + if (a==0||b==0) + + return max (a,b); + + r=a%b; + + while (r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + fin>>a>>b; + + if(cmmdc(a,b)!=1) + + fout< +#include + +int gcd(int a, int b) +{ + int m; + while (b != 0) + { + m = b; + b = a % b; + a = m; + } + return a; +} + +int main() +{ + std::ifstream fin("cmmdc.in"); + std::ofstream fout("cmmdc.out"); + int a, b; + fin >> a >> b; + a = gcd(a, b); + if (a == 1) + fout << 0; + else + fout << a; +} diff --git a/gcd/Euclid-division/29b2c14a-137d-11eb-b8f9-309c234925a0.cpp b/gcd/Euclid-division/29b2c14a-137d-11eb-b8f9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21ad43a838fa19fa53f9c7564983a6033741b55c --- /dev/null +++ b/gcd/Euclid-division/29b2c14a-137d-11eb-b8f9-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include + + +std::ifstream cin("cmmdc.in"); + +std::ofstream cout("cmmdc.out"); + + + +long long cmmdc(long long a, long long b){ + + if(b == 0) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() + +{ + + long long a{}, b{}; + + cin >> a >> b; + + long long c = cmmdc(a, b); + + if(c == 1) + + cout << 0; + + else cout << c; + +} diff --git a/gcd/Euclid-division/29c0a400-137d-11eb-bf62-309c234925a0.cpp b/gcd/Euclid-division/29c0a400-137d-11eb-bf62-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b4961ddd4f831e43680c16b87d84de250801bfe --- /dev/null +++ b/gcd/Euclid-division/29c0a400-137d-11eb-bf62-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +f>>a>>b; + +while(b!=0) + +{ + +int r=a%b; + +a=b; + +b=r; + +} + +if(a==1) + +g<<0; + +else + +g< +using namespace std; + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +f>>a>>b; + +while(b!=0) + +{ + +int r=a%b; + +a=b; + +b=r; + +} + +if(a==1) + +g<<0; + +else + +g< +#include +#include +#include + + + + + + +using namespace std; + + + +vector Read(string path) + +{ + + std::ifstream file; + + file.open(path, std::ios::in); + + vector read; + + int ti; + + while (file >> ti) + + read.push_back(ti); + + file.close(); + + return read; + +} + +void Write(string path, vector data) + +{ + + std::ofstream file; + + file.open(path); + + for (int i = 0; i < data.size(); i++) + + { + + file << data[i] << " "; + + } + + file.close(); + +} + +void Write(string path, int data) + +{ + + std::ofstream file; + + file.open(path); + + file << data; + + file.close(); + +} + + + +void Swap(int* a, int* b) + +{ + + int ti = *a; + + *a = *b; + + *b = ti; + +} + + + +int GCD(int a, int b) + +{ + + while (b != 0) + + { + + int t = b; + + b = a % b; + + a = t; + + } + + return a; + +} + + + + + +int main() + +{ + + vector input = Read("cmmdc.in"); + + int gcd = GCD(input[0], input[1]); + + if (gcd == 1) + + gcd = 0; + + + + Write("cmmdc.out", gcd); + +} diff --git a/gcd/Euclid-division/2a27bffe-137d-11eb-a960-309c234925a0.cpp b/gcd/Euclid-division/2a27bffe-137d-11eb-a960-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7c0ff6d39b629610801a6a4de3b080c78c07011 --- /dev/null +++ b/gcd/Euclid-division/2a27bffe-137d-11eb-a960-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include + + + + +using namespace std; + + + +int CMMDC(int a, int b) { + + if (b == 0) { + + if (a == 1) + + return 0; + + return a; + + } + + return CMMDC(b, a % b); + +} + + + +int main(void) { + + ifstream in; + + ofstream out; + + in.open ("cmmdc.in"); + + out.open ("cmmdc.out"); + + in.is_open(); + + int a, b; + + in>>a>>b; + + out< +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int rest, nr1, nr2; + + + +void cmmddc(int &a,int &b) { + + rest=1; + + while (rest!=0) { + + rest=a%b; + + a=b; + + b=rest; + + } + +} + +int main() + +{ + + f >> nr1 >> nr2; + + cmmddc(nr1,nr2); + + if (nr1==1) { + + g << 0; + + } + + else { + + g << nr1; + + } + + + + return 0; + +} diff --git a/gcd/Euclid-division/2a46e0ca-137d-11eb-ad19-309c234925a0.cpp b/gcd/Euclid-division/2a46e0ca-137d-11eb-ad19-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33473002a4283098a6beead6d35e4c0817e7aafe --- /dev/null +++ b/gcd/Euclid-division/2a46e0ca-137d-11eb-ad19-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + + + +int main() + +{ + + int r,a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + g<<0; + + else + + g< +#include +using namespace std; + + + +int GCD(int A, int B) + +{ + + if(!B) + + return A; + + return GCD(B, A%B); + +} + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b; + + in>>a>>b; + + if(GCD(a, b) == 1) out<<"0"; + + else { + + while(a!=b){ + + if(a>b) a-=b; + + else b-=a; + + } + + out< + + +inline int cmmdc (int a, int b) { + + if (!b) return a; + + return cmmdc(b, a%b); + +} + + + +int main () { + + FILE *fin=fopen ("cmmdc.in", "r"), + + *fout=fopen ("cmmdc.out", "w"); + + int a, b; + + fscanf (fin, "%d%d", &a, &b); + + a=cmmdc(a, b); + + fprintf (fout, "%d", a==1?0:a); + + return 0; + +} diff --git a/gcd/Euclid-division/2a942e58-137d-11eb-9d92-309c234925a0.cpp b/gcd/Euclid-division/2a942e58-137d-11eb-9d92-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5e325e4e28c445410743e193c1c125a0f491d80 --- /dev/null +++ b/gcd/Euclid-division/2a942e58-137d-11eb-9d92-309c234925a0.cpp @@ -0,0 +1,48 @@ +#include + + +using namespace std; + +ifstream cin ("cmmdc.in"); + +ofstream cout ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + cin >> a >> b; + + while (b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if (a==1) + + { + + cout << 0; + + } + + else + + { + + cout << a; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/2afee8e6-b478-4320-8065-04a6987db397.cpp b/gcd/Euclid-division/2afee8e6-b478-4320-8065-04a6987db397.cpp new file mode 100644 index 0000000000000000000000000000000000000000..339b9c024f27b557ccfd7ae71af8fc8691ccc7f7 --- /dev/null +++ b/gcd/Euclid-division/2afee8e6-b478-4320-8065-04a6987db397.cpp @@ -0,0 +1,43 @@ +#include +#include + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if(!b) + + return a; + + return cmmdc(b, a%b); + +} + + + +int main() { + + int a, b; + + f>>a>>b; + + + + if(cmmdc(a, b) == 1) + + fo << 0; + + else + + fo << cmmdc(a, b); + +} diff --git a/gcd/Euclid-division/2c0fefb9-1cc1-499c-8486-8cb677ff725d.cpp b/gcd/Euclid-division/2c0fefb9-1cc1-499c-8486-8cb677ff725d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7c0ff6d39b629610801a6a4de3b080c78c07011 --- /dev/null +++ b/gcd/Euclid-division/2c0fefb9-1cc1-499c-8486-8cb677ff725d.cpp @@ -0,0 +1,54 @@ +#include + + + + +using namespace std; + + + +int CMMDC(int a, int b) { + + if (b == 0) { + + if (a == 1) + + return 0; + + return a; + + } + + return CMMDC(b, a % b); + +} + + + +int main(void) { + + ifstream in; + + ofstream out; + + in.open ("cmmdc.in"); + + out.open ("cmmdc.out"); + + in.is_open(); + + int a, b; + + in>>a>>b; + + out< +using namespace std; + +int main(){ + + FILE *fin, *fout; + + int a, b, r; + + fin=fopen("cmmdc.in", "r"); + + fout=fopen("cmmdc.out", "w"); + + fscanf(fin, "%d%d", &a, &b); + + while(b>0){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + a=0; + + fprintf(fout, "%d\n", a); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/gcd/Euclid-division/31868d90-c1d8-4b58-9161-e950e9ab71f8.cpp b/gcd/Euclid-division/31868d90-c1d8-4b58-9161-e950e9ab71f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55fe159cf1f28ccc44a8516dc1144cc94dd8a447 --- /dev/null +++ b/gcd/Euclid-division/31868d90-c1d8-4b58-9161-e950e9ab71f8.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int b, a, r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + +int a, b; + +int main() + +{ + + cin>>a>>b; + + if(b==0) + + { + + cout<<"0"; + + } + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + { + + cout<<"0"; + + } + + else + + { + + cout< + +using namespace std; + +int main() +{ + freopen ("cmmdc.in","r",stdin); + freopen ("cmmdc.out","w",stdout); + int a, b, rest; + cin >> a >> b; + while(b != 0){ + rest = a % b ; + a = b; + b = rest; + } + if (a == 1) + a = 0; + cout << a; + return 0; +} diff --git a/gcd/Euclid-division/3c4fba31-5f4d-4dd4-b352-fe080cfa8e1b.cpp b/gcd/Euclid-division/3c4fba31-5f4d-4dd4-b352-fe080cfa8e1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5dc1e11057eb604c3da875cd09ec9377b364c1f --- /dev/null +++ b/gcd/Euclid-division/3c4fba31-5f4d-4dd4-b352-fe080cfa8e1b.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fi>>a>>b; + + while (b != 0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + fo<<0; + + else + + fo< +#include +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int rasp() + +{ + + int a, b; + + in >> a >> b; + + if (!b) + + return a; + + while (a %= b) + + std::swap(a, b); + + return b == 1? 0: b; + +} + +int main() + +{out << rasp();} diff --git a/gcd/Euclid-division/47c70bd3-3e3d-4e30-987c-cf0dfe8c61f7.cpp b/gcd/Euclid-division/47c70bd3-3e3d-4e30-987c-cf0dfe8c61f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f5f5088ad199b87814d492e6715e7da864037b4 --- /dev/null +++ b/gcd/Euclid-division/47c70bd3-3e3d-4e30-987c-cf0dfe8c61f7.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,c; + +int main() + +{ + + f >> a >> b; + + c = a % b; + + while(c != 0) + + { + + a = b; + + b = c; + + c = a % b; + + } + + if(b == 1) + + b = 0; + + g << b; + + return 0; + +} diff --git a/gcd/Euclid-division/4b509872-7baf-4b6a-8a37-bd98dfc1a92e.cpp b/gcd/Euclid-division/4b509872-7baf-4b6a-8a37-bd98dfc1a92e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90ed4413605d6dc36f0f4cf8ad7f114b0de6c839 --- /dev/null +++ b/gcd/Euclid-division/4b509872-7baf-4b6a-8a37-bd98dfc1a92e.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fon("cmmdc.out"); + +int main(){ + +int a,b,r; + +fin>>a>>b; + +while(b!=0){ + + r = a % b; + + a = b; + + b = r;} + +if(a!=1) + +fon< +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream fo("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fi>>a>>b; + + while (b != 0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + fo<<0; + + else + + fo< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +long long a, b; + + + +int main() + +{ + + long long r; + + fin >> a >> b; + + while(b != 0) + + { + + r = a % b; + + a = b; + + b = r; + + } + + + + if(a == 1) fout << 0; + + else fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/542d84fc-6dee-4f3a-962b-91b3cbd93b70.cpp b/gcd/Euclid-division/542d84fc-6dee-4f3a-962b-91b3cbd93b70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68fa2e1f18a0b01405ced3a6367a339c87e0dfc9 --- /dev/null +++ b/gcd/Euclid-division/542d84fc-6dee-4f3a-962b-91b3cbd93b70.cpp @@ -0,0 +1,79 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a,b,x,y,r,i; + + f>>a>>b; + + x=a; + + y=b; + + r=x%y; + + while(r!=0) + + { + + x=y; + + y=r; + + r=x%y; + + } + + if(y==1) g<<"0"; + + else + + { + + for(i=a;i>=2;i--) + + { + + if(a%i==0) + + { + + if(b%i==0) + + { + + g< +using namespace std; + +class MyClass + +{ + +private: + + int a , b; + +public: + + MyClass(int x , int y) + + { + + a = x; + + b = y; + + } + + void cmmdc() + + { + + int r; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + } + + int geta() + + { + + if(a == 1) + + return 0; + + return a; + + } + +}; + +int main() + +{ + + freopen("cmmdc.in" , "r" , stdin); + + freopen("cmmdc.out" , "w" , stdout); + + int a , b; + + scanf("%d%d" , &a , &b); + + MyClass d(a , b); + + d.cmmdc(); + + printf("%d\n" , d.geta()); + + return 0; + +} diff --git a/gcd/Euclid-division/5a853c94-0782-4f2b-be80-5a2340e26204.cpp b/gcd/Euclid-division/5a853c94-0782-4f2b-be80-5a2340e26204.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b12f76ee3b42e31d1704a049f3f6b073faface6 --- /dev/null +++ b/gcd/Euclid-division/5a853c94-0782-4f2b-be80-5a2340e26204.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int a,b; + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + else + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + fin>>a; + + fin>>b; + + int c=cmmdc(a,b); + + if(c==1) + + fout<<0; + + else + + fout< +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int rest, nr1, nr2; + + + +void cmmddc(int &a,int &b) { + + rest=1; + + while (rest!=0) { + + rest=a%b; + + a=b; + + b=rest; + + } + +} + +int main() + +{ + + f >> nr1 >> nr2; + + cmmddc(nr1,nr2); + + if (nr1==1) { + + g << 0; + + } + + else { + + g << nr1; + + } + + + + return 0; + +} diff --git a/gcd/Euclid-division/5cb7bf4a-67e5-4053-960c-133d51be3282.cpp b/gcd/Euclid-division/5cb7bf4a-67e5-4053-960c-133d51be3282.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55dda3620f9c1e4400ea498128b7658a367113b1 --- /dev/null +++ b/gcd/Euclid-division/5cb7bf4a-67e5-4053-960c-133d51be3282.cpp @@ -0,0 +1,56 @@ +#include + + + +typedef long long ll; + + + +const ll mod=1e9+7; + + + +const int dx[] = {0, 1, 0, -1}; + +const int dy[] = {1, 0, -1, 0}; +using namespace std; + + + +ll t; + + + +int euclid(int a, int b) { + + if (!b)return a; + + else return euclid(b,a%b); + +} + + + +int main() { + +ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + ll a,b; + + cin >> a >> b; + + if (euclid(a,b)==1) { + + cout << "0\n"; + + } + + else cout << euclid(a,b)<< '\n'; + + + +} diff --git a/gcd/Euclid-division/5eda2355-9926-4ed3-a4b1-43b912e9af14.cpp b/gcd/Euclid-division/5eda2355-9926-4ed3-a4b1-43b912e9af14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5e325e4e28c445410743e193c1c125a0f491d80 --- /dev/null +++ b/gcd/Euclid-division/5eda2355-9926-4ed3-a4b1-43b912e9af14.cpp @@ -0,0 +1,48 @@ +#include + + +using namespace std; + +ifstream cin ("cmmdc.in"); + +ofstream cout ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + cin >> a >> b; + + while (b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if (a==1) + + { + + cout << 0; + + } + + else + + { + + cout << a; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/61f3fb72-234e-4112-afa1-3f048c93c90f.cpp b/gcd/Euclid-division/61f3fb72-234e-4112-afa1-3f048c93c90f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b73aeacfa5bb89c62b61db46136013916829342a --- /dev/null +++ b/gcd/Euclid-division/61f3fb72-234e-4112-afa1-3f048c93c90f.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int a,b; + + + +int cmmdc(int a, int b) + +{ + + if(b==0) + + return a; + + else + + return cmmdc(b,a%b); + +} + + + +int main() + +{ + + fin>>a; + + fin>>b; + + int c=cmmdc(a,b); + + if(c==1) + + fout<<0; + + else + + fout< +#include +using namespace std; +int cmmdc(int a, int b){ + if(!b) return a; + return cmmdc(b, a%b); +} +int main() +{ + ifstream in("cmmdc.in"); + ofstream out("cmmdc.out"); + int a, b; + in>>a>>b; + int gcd = cmmdc(a, b); + if(gcd==1) + out<<0; + else + out< +#include + + +using namespace std; + + + +typedef unsigned long long ULL; + + + +ULL cmmdc(ULL a, ULL b) { + + unsigned long long rest; + + while(b) { + + rest = a % b; + + a = b; + + b = rest; + + } + + return a; + +} + + + +int main() { + + ifstream in("cmmdc.in"); + + + + ULL a, b; + + in >> a; + + in >> b; + + + + in.close(); + + + + ofstream out("cmmdc.out"); + + + + ULL result = cmmdc(a, b); + + if (result != 1) { + + out << result; + + } else { + + out << 0; + + } + + + + + + out.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-division/6693a83a-1d06-481b-b1d1-d11d9677436f.cpp b/gcd/Euclid-division/6693a83a-1d06-481b-b1d1-d11d9677436f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3eaad392c018e348b496b0d97fc97b7369edf55c --- /dev/null +++ b/gcd/Euclid-division/6693a83a-1d06-481b-b1d1-d11d9677436f.cpp @@ -0,0 +1,26 @@ +#include + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int rasp() + +{ + + int a, b; + + in >> a >> b; + + while (a %= b) + + std::swap(a, b); + + return b == 1? 0: b; + +} + +int main() + +{out << rasp();} diff --git a/gcd/Euclid-division/66f8e4ef-eadb-4f07-a3bb-00352f1b5719.cpp b/gcd/Euclid-division/66f8e4ef-eadb-4f07-a3bb-00352f1b5719.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ced7d1cb443cac59da931e7a259b7cc9dab922b --- /dev/null +++ b/gcd/Euclid-division/66f8e4ef-eadb-4f07-a3bb-00352f1b5719.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int euclid(int a, int b) + +{ + + if (!b) + + return a; + + return euclid(b, a % b); + +} + + + +int main() + +{ + + int x, y; + + in >> x >> y; + + if (euclid(x, y) == 1) + + out << 0; + + else + + out << euclid(x, y); + + + + return 0; + + + +} diff --git a/gcd/Euclid-division/68bdd6b7-1dae-4d76-8f96-1aff60514871.cpp b/gcd/Euclid-division/68bdd6b7-1dae-4d76-8f96-1aff60514871.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34ab992f5b72baf54b9152a0497071457d34620f --- /dev/null +++ b/gcd/Euclid-division/68bdd6b7-1dae-4d76-8f96-1aff60514871.cpp @@ -0,0 +1,44 @@ +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + + + +int main(){ + + long long a,b; + + in >> a >> b; + + while (b != 0) + + { + + int rest = a % b; + + a = b; + + b = rest; + + } + + if(a==1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-division/69e656b4-c25f-4d56-84bc-acc17111eedd.cpp b/gcd/Euclid-division/69e656b4-c25f-4d56-84bc-acc17111eedd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0610460a7c517ea0e373765b62293c60559d9462 --- /dev/null +++ b/gcd/Euclid-division/69e656b4-c25f-4d56-84bc-acc17111eedd.cpp @@ -0,0 +1,65 @@ +#include +#include + + +using namespace std; + + + +int cmmdc(int a, int b) { + + int r; + + + + while (b != 0) { + + r = a % b; + + a = b; + + b = r; + + } + + + + return a; + +} + + + +int main() { + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + unsigned int a, b; + + + + fin >> a >> b; + + + + int d = cmmdc(a, b); + + + + if (d != 1) + + fout << d; + + else + + fout << 0; + + + + return 0; + +} diff --git a/gcd/Euclid-division/6a4e4f14-7d7c-4764-b0bb-fa92ddb5e073.cpp b/gcd/Euclid-division/6a4e4f14-7d7c-4764-b0bb-fa92ddb5e073.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebe62206f09dc0bac86afb54556cc75061188ae4 --- /dev/null +++ b/gcd/Euclid-division/6a4e4f14-7d7c-4764-b0bb-fa92ddb5e073.cpp @@ -0,0 +1,46 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ long long a, b, r=0; + + fin >> a >> b; + + + + while(b) + + { r = a%b; + + a = b; + + b = r; + + + + } + + + + if( a == 1 ) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/6b6c20a4-beff-4ed5-a325-953f9c33a041.cpp b/gcd/Euclid-division/6b6c20a4-beff-4ed5-a325-953f9c33a041.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23de0ee401a21f1d556060d5eacde11b86ae583f --- /dev/null +++ b/gcd/Euclid-division/6b6c20a4-beff-4ed5-a325-953f9c33a041.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int t, x, y; + + + +int cmmdc(int x, int y) + +{ + + int r; + + while(y) + + { + + r = x % y; + + x = y; + + y = r; + + } + + return x; + +} + + + +int main() + +{ + + f >> x >> y; + + int t = cmmdc(x, y); + + + + if(t == 1) g << 0; + + else g << t; + + return 0; + +} diff --git a/gcd/Euclid-division/720abc6c-a65b-400c-b0d9-a0e01d69fddf.cpp b/gcd/Euclid-division/720abc6c-a65b-400c-b0d9-a0e01d69fddf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7d36ffc2a73f1c032b414a6b799dc28d6fc4469 --- /dev/null +++ b/gcd/Euclid-division/720abc6c-a65b-400c-b0d9-a0e01d69fddf.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +ifstream f ("cmmdc.in"); + +ofstream g ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + f>>a>>b; + + if(a>b) + + { + + int aux; + + aux=a; + + a=b; + + b=aux; + + } + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + g<<0; + + else + + g< + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if (b == 0) + + return (a == 1) ? 0 : a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + int a, b; + + fin >> a >> b; + + fout << cmmdc(a, b); + + + + return 0; + +} diff --git a/gcd/Euclid-division/73f53489-1a51-444e-b39b-144ae59df013.cpp b/gcd/Euclid-division/73f53489-1a51-444e-b39b-144ae59df013.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8b949f5554788c903ed07a191adcdf7b5dcb40f --- /dev/null +++ b/gcd/Euclid-division/73f53489-1a51-444e-b39b-144ae59df013.cpp @@ -0,0 +1,41 @@ +#include +#include + + +int cmmdc(int a, int b) { + + if (b == 0) + + if (a == 1) + + return 0; + + else + + return a; + + else + + return cmmdc(b, a%b); + +} + + + +int main() + +{ + + std::ofstream out("cmmdc.out"); + + std::ifstream in("cmmdc.in"); + + int a, b; + + in >> a >> b; + + out << cmmdc(a, b); + + + +} diff --git a/gcd/Euclid-division/7616191c-1a98-436f-9084-b4c7fad9051f.cpp b/gcd/Euclid-division/7616191c-1a98-436f-9084-b4c7fad9051f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b164f316885a102d1a2c8bc3765120e03ec3248a --- /dev/null +++ b/gcd/Euclid-division/7616191c-1a98-436f-9084-b4c7fad9051f.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; + +FILE *f = fopen("cmmdc.in","r"); +FILE *g = fopen("cmmdc.out","w"); + +int gcd(int a,int b){ + if(!b) return a; + return gcd(b,a % b); +} + +int main(){ + + int a,b; + + fscanf(f,"%d %d",&a,&b); + + fprintf(g,"%d\n",gcd(a,b) == 1 ? 0:gcd(a,b)); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/gcd/Euclid-division/7677f1c5-fa2c-454f-aee8-03cf6f028860.cpp b/gcd/Euclid-division/7677f1c5-fa2c-454f-aee8-03cf6f028860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff8aad8738e49e474ab0c89c09f1e08e9da40a98 --- /dev/null +++ b/gcd/Euclid-division/7677f1c5-fa2c-454f-aee8-03cf6f028860.cpp @@ -0,0 +1,53 @@ +#include +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int aux; + + while(b) + + { + + aux=b; + + b=a%b; + + a=aux; + + } + + if(a==1) return 0; + + return a; + +} + + + +int main() + +{ + + int a, b; + + in>>a>>b; + + out< +using namespace std; +int cmmdc(int a, int b) +{ + if(b == 0) + return a; + else + return cmmdc(b, a%b); +} +int main() +{ + freopen("cmmdc.in", "r", stdin); + freopen("cmmdc.out", "w", stdout); + int a, b; + scanf("%d%d", &a, &b); + int c = cmmdc(a, b); + if(c == 1) + c = 0; + printf("%d", c); + return 0; +} diff --git a/gcd/Euclid-division/7894eea1-e269-42b1-919c-6900241fee31.cpp b/gcd/Euclid-division/7894eea1-e269-42b1-919c-6900241fee31.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc515f7aedb9eda288eaa0899d15968df3a898ab --- /dev/null +++ b/gcd/Euclid-division/7894eea1-e269-42b1-919c-6900241fee31.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<"0"; + + else out< +#include +using namespace std; + + + +int GCD(int A, int B) + +{ + + if(!B) + + return A; + + return GCD(B, A%B); + +} + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b; + + in>>a>>b; + + if(GCD(a, b) == 1) out<<"0"; + + else { + + while(a!=b){ + + if(a>b) a-=b; + + else b-=a; + + } + + out< + +using namespace std; + +int cmmdc(int a, int b) +{ + if (!b) + return a; + return cmmdc(b, a%b); +} + +int main() +{ + freopen("cmmdc.in","r",stdin); + freopen("cmmdc.out","w",stdout); + int a,b; + cin >> a >> b; + if (cmmdc(a,b)==1) + cout << "0"; + else cout << cmmdc(a,b); + return 0; +} diff --git a/gcd/Euclid-division/7a7e7eb1-aca4-4d85-8a96-a9bd16c819b4.cpp b/gcd/Euclid-division/7a7e7eb1-aca4-4d85-8a96-a9bd16c819b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c09650f9a1fbb11f5c0b019cb9391852a6532a43 --- /dev/null +++ b/gcd/Euclid-division/7a7e7eb1-aca4-4d85-8a96-a9bd16c819b4.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main(){ + + unsigned int a,b,r; + + fin>>a>>b; + + r=a%b; + + while(r){ + + r = a % b; + + a = b; + + b = r;} + + if(a==1)fout<<0; + + else fout< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int euclid(int a,int b) + +{ + + if (b==0) + + return a; + + return euclid(b,a%b); + +} + +int main() + +{ + + int a,b,x; + + fin>>a>>b; + + x=euclid(a,b); + + if (x==1) + + fout<<0; + + else + + fout< + + +std::ifstream in("cmmdc" ".in"); + +std::ofstream out("cmmdc" ".out"); + +int main() + +{ + + int a, b; + + in >> a >> b; + + while (a %= b) + + std::swap(a, b); + + if (b == 1) + + b = 0; + + out << b; + +} diff --git a/gcd/Euclid-division/7f9c64dd-9b0f-40a4-a9ee-4f371bdccd1e.cpp b/gcd/Euclid-division/7f9c64dd-9b0f-40a4-a9ee-4f371bdccd1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33c6d30dc09ff84ebfe3890ec74c6de6d10cba5e --- /dev/null +++ b/gcd/Euclid-division/7f9c64dd-9b0f-40a4-a9ee-4f371bdccd1e.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + a=0; + + out< +using namespace std; + +int main() + +{ + + freopen("cmmdc.in","r",stdin); + + freopen("cmmdc.out","w",stdout); + + int n,m; + + scanf("%d%d",&n,&m); + + while(m != 0) + + { + + int r = n % m; + + n = m; + + m = r; + + } + + if(n!=1) + + printf("%d",n); + + else + + printf("0"); + + return 0; + +} diff --git a/gcd/Euclid-division/813c5bcd-2b12-410c-b971-968cc8119d36.cpp b/gcd/Euclid-division/813c5bcd-2b12-410c-b971-968cc8119d36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eeb7d3052318d81a4867d64a53ed759ba1081357 --- /dev/null +++ b/gcd/Euclid-division/813c5bcd-2b12-410c-b971-968cc8119d36.cpp @@ -0,0 +1,67 @@ +#include +#include + + +int main() + +{ + + std::ifstream fisier_in("cmmdc.in"); + + std::ofstream fisier_out("cmmdc.out"); + + long a,b, cmmdc; + + fisier_in>>a; + + fisier_in>>b; + + fisier_in.close(); + + if(b > a) + + { + + while(a!=0) + + { + + int temp = a; + + a = b % a; + + b = temp; + + } + + if(b == 1) cmmdc = 0; + + else cmmdc = b; + + } + + else{ + + while(b != 0){ + + int temp = b; + + b = a % b; + + a = temp; + + } + + if(a == 1) cmmdc = 0; + + else cmmdc = a; + + } + + fisier_out< +using namespace std; + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +f>>a>>b; + +while(b!=0) + +{ + +int r=a%b; + +a=b; + +b=r; + +} + +if(a==1) + +g<<0; + +else + +g< + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) { + + if (b == 0) + + return a; + + return cmmdc(b, a % b); + + + +} + +int main() { + + + + int a, b; + + fin >> a >> b; + + + + int CMMDC = cmmdc(a, b); + + if (CMMDC == 1) + + fout << '0'; + + else + + fout << CMMDC; + + + + return 0; + +} diff --git a/gcd/Euclid-division/8639fd9d-f4e9-4a17-8046-c11182c2704f.cpp b/gcd/Euclid-division/8639fd9d-f4e9-4a17-8046-c11182c2704f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33d71f1d6cf664b8c5370a50f502265520e1d6c3 --- /dev/null +++ b/gcd/Euclid-division/8639fd9d-f4e9-4a17-8046-c11182c2704f.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + + + +int main() + +{ + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + int x, y, r; + + + + fin>>x>>y; + + + + while(y != 0) + + { + + r=x % y; + + x = y; + + y = r; + + } + + + + if(x != 1) + + fout< + + +using namespace std; + +ifstream fi("cmmdc.in"); + +ofstream ge("cmmdc.out"); + + + +bool primalitate(int x, int y) + +{ + + int r; + + while(y) + + { + + r=x%y; + + x=y; + + y=r; + + } + + if(x==1) return true; + + else return false; + + + +} + + + +int a,b; + +int main() + +{ + + + + fi>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(primalitate(a,b)==true) ge<<0; + + else ge< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else + + fout< +#include + + + + +using namespace std; + + + +long long int a,b,r,aux; + + + +int main() + + + +{ + + + + ifstream fin("cmmdc.in"); + + + + ofstream fout("cmmdc.out"); + + + + fin>>a; + + + + fin>>b; + + + + if (b>a) + + + + { + + + + aux=b; + + + + b=a; + + + + a=aux; + + + + } + + + + r=a%b; + + + + while (r) + + + + { + + + + a=b; + + + + b=r; + + + + r=a%b; + + + + } + + if (b==1) + + fout<<"0"; + + else + + fout< +#include +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int gcd(int a, int b) + +{ + + if (a % b == 0) + + return b; + + gcd(b, a % b); + +} + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + int r = gcd(a, b); + + cout << r; + + g << ((r == 1) ? 0 : r); + + return 0; + +} diff --git a/gcd/Euclid-division/8c6754d1-cb15-450f-81a7-b137829b6f4c.cpp b/gcd/Euclid-division/8c6754d1-cb15-450f-81a7-b137829b6f4c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cbed90df0f375a7efd48a92948abc450cc5b9ee --- /dev/null +++ b/gcd/Euclid-division/8c6754d1-cb15-450f-81a7-b137829b6f4c.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +int a,b; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{int rest; + + f>>a>>b; + + while(b!=0) + + {rest=a%b; + + a=b;b=rest; + + } + + if(a==1)g<<0; + + else g< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a, b, c; + + + +int cmmdc(int a, int b) + +{ + + if(!b) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() + +{ + + f>>a>>b; + + c = cmmdc(a, b); + + if(c == 1) + + g<<0; + + else + + g< +#include +using namespace std; + + + +int main() + +{ + + ifstream xin("cmmdc.in"); + + ofstream xout("cmmdc.out"); + + int a,b,rest; + + xin>>a>>b; + + while(b){ + + rest=a%b; + + a=b; + + b=rest; + + } + + if(a==1){ + + xout<<0; + + }else{ + + xout< + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int main() + +{ + + unsigned long long a, b, r; + + cin >> a; + + cin >> b; + + while(b) + + { + + r = a % b; + + a = b; + + b = r; + + } + + if(a!=1)cout << a; + + else cout<<0; + + return 0; + +} diff --git a/gcd/Euclid-division/9225a624-ff24-48ef-ba4d-50adaad0460c.cpp b/gcd/Euclid-division/9225a624-ff24-48ef-ba4d-50adaad0460c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10ff978966c4240f5c73405737b3ce95053cea25 --- /dev/null +++ b/gcd/Euclid-division/9225a624-ff24-48ef-ba4d-50adaad0460c.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,x; + + in>>a; + + in>>b; + + while(b!=0) + + { + + x=a%b; + + a=b; + + b=x; + + } + + if(a!=1) + + out< + + +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + r=a%b; + + while(r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if(b==1) + + out<<0; + + else + + out< + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int a, b; + + + +int cmmdc(int a, int b) { + + if (b == 0) return a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + in >> a >> b; + + int ans = (cmmdc(a, b) == 1) ? 0 : cmmdc(a, b); + + out << ans; + + return 0; + +} diff --git a/gcd/Euclid-division/998898c2-0bff-4397-b520-16b4630e1162.cpp b/gcd/Euclid-division/998898c2-0bff-4397-b520-16b4630e1162.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52bd508b941e3dc6e5327b1494e4d8c0b0becf3d --- /dev/null +++ b/gcd/Euclid-division/998898c2-0bff-4397-b520-16b4630e1162.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r = a % b; + + while (r != 0) + + { + + a = b; + + b = r; + + r = a % b; + + } + + return b; + +} + + + +int main() + +{ + + int a, b; + + fin >> a >> b; + + if (cmmdc(a,b) == 1) + + fout << '0'; + + else + + fout << cmmdc(a, b); + + return 0; + +} diff --git a/gcd/Euclid-division/9abdd88e-8935-4c3e-99c8-3f8f7c22e390.cpp b/gcd/Euclid-division/9abdd88e-8935-4c3e-99c8-3f8f7c22e390.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d84c039ad1da542b93cbc62229030d00c00bc748 --- /dev/null +++ b/gcd/Euclid-division/9abdd88e-8935-4c3e-99c8-3f8f7c22e390.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int GCD(int a , int b){ + + if (!b) + + return a; + + return GCD(b,a%b); + +} + +int main() { + + int a, b; + + bool prime = false; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin >> a >> b; + + if ( GCD(a, b) != 1 ) { + + fout << GCD(a, b); + + prime = true; + + } + + if ( prime == false ) { + + fout << 0; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/9c6f09f8-dde7-40ce-bae6-f66b3ab73547.cpp b/gcd/Euclid-division/9c6f09f8-dde7-40ce-bae6-f66b3ab73547.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb203a95a5a32478033cdc4cbd3787bd35277c2 --- /dev/null +++ b/gcd/Euclid-division/9c6f09f8-dde7-40ce-bae6-f66b3ab73547.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + int r=a%b; + + while(r) + + { + + a=b; + + b=r; + + r=a%b; + + } + + return b; + +} + + + +int main() + +{ + + int a,b; + + in>>a>>b; + + if(cmmdc(a,b)==1) + + out<<0; + + else + + out< +#include +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + int a,b,r; + + in>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<"0"; + + else out< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + + + int cmmdc_a, cmmdc_b, a, b,max; + + + + fin >> a >> b; + + + + + + if (a > b) { + + max = a; + + } + + else { + + max = b; + + } + + + + do { + + + + cmmdc_a = a % max; + + cmmdc_b = b % max; + + + + max--; + + + + } while (cmmdc_a!=0 || cmmdc_b!=0); + + + + + + max++; + + + + if (max == 1) { + + max--; + + } + + + + fout << max; + + + + cin.get(); + + return 0; + +} diff --git a/gcd/Euclid-division/9fe40dae-2890-4060-b005-fafa8f484276.cpp b/gcd/Euclid-division/9fe40dae-2890-4060-b005-fafa8f484276.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62dbc746ca786ba9686ca11e20d94021b8c1770c --- /dev/null +++ b/gcd/Euclid-division/9fe40dae-2890-4060-b005-fafa8f484276.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + +int cmmdc(int a,int b){ + +if (b==0) return a; + +return cmmdc(b,a%b); + +} + + + +int main() + +{int a,b; + +f>>a>>b; + +if(cmmdc(a,b)==1) g<<0; + +else g< using namespace std; int a,b,r; int main() { ifstream in("cmmdc.in"); ofstream out("cmmdc.out"); in>>a>>b; while(b) { r=a%b; a=b; b=r; } if(a==1) out<<0; else out< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a, b, r, ca, cb; + +int main() + +{ + + fin>>a>>b; + + ca=a; + + cb=b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(ca==1 || cb==1) + + fout<<1; + + else + + { + + if(a==1) + + fout<<0; + + else + + fout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ int a,b,r; + + f>>a>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a!=1) g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ int a,b,r; + + f>>a>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + +if(a!=1) + + g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + r = a % b; + + while(r) + + { + + a = b; + + b = r; + + r = a % b; + + } + + if(b == 1) + + fout << 0; + + else + + fout << b; + + return 0; + +} diff --git a/gcd/Euclid-division/a4ee4dc7-651c-40c5-acd2-83a52d6adea8.cpp b/gcd/Euclid-division/a4ee4dc7-651c-40c5-acd2-83a52d6adea8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..953f303003d591595a7115b9f9f28834dc4a1f3d --- /dev/null +++ b/gcd/Euclid-division/a4ee4dc7-651c-40c5-acd2-83a52d6adea8.cpp @@ -0,0 +1,46 @@ +#include +using namespace std; + + + +ifstream cin ("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + if (b == 0) + + return a; + + else + + return cmmdc(b, a%b); + +} + + + +int main() + +{ + + int a, b; + + cin >> a >> b; + + if (cmmdc(a, b) == 1) + + cout << "0"; + + else + + cout << cmmdc(a, b); + + + +} diff --git a/gcd/Euclid-division/aa665d71-a0e3-453f-ae67-adbdbc54646f.cpp b/gcd/Euclid-division/aa665d71-a0e3-453f-ae67-adbdbc54646f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4b5d72ca3a7fbff3f38fa23b5bc3c75b0f78e49 --- /dev/null +++ b/gcd/Euclid-division/aa665d71-a0e3-453f-ae67-adbdbc54646f.cpp @@ -0,0 +1,46 @@ +#include "bits/stdc++.h" +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a, b; + + cin>>a>>b; + + while(a!=0 && b!=0) + + { + + if(a>b) + + { + + a%=b; + + } + + else b%=a; + + } + + if (a+b==1) + + { + + cout <<0; + + } + + else cout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + +unsigned int a,b,r; + +int main() + +{fin>>a>>b; + +while(b) + +{r=a%b; + + a=b; + + b=r; + +} + +if(a!=1) + +{fout< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + out<<0; + + else + + out< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + in>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + + + } + + if(a==1) + + a=0; + + out< +using namespace std; + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +f>>a>>b; + +while(b!=0) + +{ + +int r=a%b; + +a=b; + +b=r; + +} + +if(a==1) + +g<<0; + +else + +g< +using namespace std; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + if(a==0 && b!=0) + + { + + g< +#include + + +using namespace std; + + + +int main() + +{ + + int a,b,r; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + + + while(b!=0) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if(a==1) + + g<<0; + + else + + g< +using namespace std; + + + +string problem = "cmmdc"; + +ifstream fin (problem + ".in"); + +ofstream fout(problem + ".out"); + + + + + + + +int gcd(int a,int b) + +{ + + if(!b) + + return a; + + return gcd(b,a % b); + +} + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + if(gcd(a,b) == 1) + + { + + fout << 0 << "\n"; + + return 0; + + } + + fout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/b7384646-ae5a-47d8-8257-3babcbde32a6.cpp b/gcd/Euclid-division/b7384646-ae5a-47d8-8257-3babcbde32a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d247ef9aeb4231c8aba31f9fcda67f5854076ba --- /dev/null +++ b/gcd/Euclid-division/b7384646-ae5a-47d8-8257-3babcbde32a6.cpp @@ -0,0 +1,34 @@ +#include + + +int cmmdc(int a, int b) { + + if(b == 0) + + return a; + + return cmmdc(b, a % b); + +} + + + +int main() { + + int a, b; + + freopen("cmmdc.in", "r", stdin); + + freopen("cmmdc.out", "w", stdout); + + scanf("%d%d", &a, &b); + + if(cmmdc(a, b) > 1) + + printf("%d", cmmdc(a, b)); + + else + + printf("0"); + +} diff --git a/gcd/Euclid-division/b8b9ea6b-861a-4d93-83e3-4b09589bf6f0.cpp b/gcd/Euclid-division/b8b9ea6b-861a-4d93-83e3-4b09589bf6f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b57debdb1cedbbd611f47552d72ed86cec68b2e2 --- /dev/null +++ b/gcd/Euclid-division/b8b9ea6b-861a-4d93-83e3-4b09589bf6f0.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int64_t a, b, cmmdc(int64_t, int64_t); + +int main() + +{ + + f >> a >> b; + + if(cmmdc(a, b) == 1) + + g << 0; + + else + + g << cmmdc(a, b); + + return 0; + +} + +int64_t cmmdc(int64_t x, int64_t y) + +{ + + if(x % y == 0) + + return y; + + return cmmdc(y, x % y); + +} diff --git a/gcd/Euclid-division/b92eb0fc-4661-436f-9581-6ee26b622a66.cpp b/gcd/Euclid-division/b92eb0fc-4661-436f-9581-6ee26b622a66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68d0e11828881240da8116b5817a02dfa415d055 --- /dev/null +++ b/gcd/Euclid-division/b92eb0fc-4661-436f-9581-6ee26b622a66.cpp @@ -0,0 +1,24 @@ +#include + +using namespace std; + +ifstream in("cmmdc.in"); +ofstream out("cmmdc.out"); + +int a, b, r; + +int main() +{ + in>>a>>b; + while(b) + { + r = a % b; + a = b; + b = r; + } + if(a == 1) + out<<0; + else + out< + + +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a,b,rest; + + cin>>a>>b; + + while(b>0) + + { + + rest=a%b; + + a=b; + + b=rest; + + } + + if(a==1) + + { + + cout<<"0"; + + } + + else + + { + + cout< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin>>a>>b; + + r=a%b; + + while (r!=0) + + { + + a=b; + + b=r; + + r=a%b; + + } + + if (b==1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +int main() + +{ + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a,b,r; + + in >> a >> b ; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a>1) out << a ; + + else out << 0 ; + + in.close(); + + out.close(); + + return 0; + +} diff --git a/gcd/Euclid-division/d6d24676-2f30-4edb-a146-9614797a76e4.cpp b/gcd/Euclid-division/d6d24676-2f30-4edb-a146-9614797a76e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5cc894a41025dd717d94a7a4a189765e0961ec8 --- /dev/null +++ b/gcd/Euclid-division/d6d24676-2f30-4edb-a146-9614797a76e4.cpp @@ -0,0 +1,26 @@ +#include + +using namespace std; +typedef long long ll; +typedef vector vi; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int a, b, r; + fin >> a >> b; + while(b!=0) { + r = a%b; + a=b; + b=r; + } + if(a==1) { + fout << 0; + return 0; + } + fout << a; + return 0; +} diff --git a/gcd/Euclid-division/d734829f-c918-4e4e-95da-f441d7b8bb0f.cpp b/gcd/Euclid-division/d734829f-c918-4e4e-95da-f441d7b8bb0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10d73688871fd9128a090de38bccc7fce9553f71 --- /dev/null +++ b/gcd/Euclid-division/d734829f-c918-4e4e-95da-f441d7b8bb0f.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +int main () + +{int a,b,r; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +f>>a; + +f>>b; + +do{r=a%b; + +a=b; + +b=r;}while (b>0); + +if(a==1) g<<0; + +else g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + f>>a>>b; + + while (b!=0) + + { + + int r=a%b; + + a=b; + + b=r; + + } + + if (a==1) + + g<<0; + + else + + g< +using namespace std; + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + +int a, b, r; + +int main() + +{ + + fin>>a>>b; + + while(b!=0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else fout< +#include + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ int r; + + while (b!=0) + + { r=a%b; + + a=b; + + b=r; + + } + + return a; + +} + + + +int main() + +{ int a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f >> a >> b; + + if (cmmdc(a,b)==1) g << "0"; + + else g << cmmdc(a,b); + + + +} diff --git a/gcd/Euclid-division/dbf5f7e1-84c0-4810-8a77-3396334a7bc5.cpp b/gcd/Euclid-division/dbf5f7e1-84c0-4810-8a77-3396334a7bc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5603695779221630bd47bc17eb8c7d3f4f09d239 --- /dev/null +++ b/gcd/Euclid-division/dbf5f7e1-84c0-4810-8a77-3396334a7bc5.cpp @@ -0,0 +1,20 @@ +#include + +using namespace std; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() +{ + int a , b ; + fin >> a >> b ; + while ( b != 0 ) + { + int rest = a % b ; + a = b ; + b = rest ; + } + if ( a == 1 ) fout << "0" ; + else fout << a ; + return 0; +} diff --git a/gcd/Euclid-division/dc1aa55f-a255-46be-9bc1-b4ee01cd3ad0.cpp b/gcd/Euclid-division/dc1aa55f-a255-46be-9bc1-b4ee01cd3ad0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d65a9da3dd0dfd46ca3fd2ca96c3d492d5f1028 --- /dev/null +++ b/gcd/Euclid-division/dc1aa55f-a255-46be-9bc1-b4ee01cd3ad0.cpp @@ -0,0 +1,44 @@ +#include + + +using namespace std; + + + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + + + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + while(b) + + { + + r = a%b; + + a = b; + + b = r; + + } + + if(a == 1) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-division/de395590-6d95-49ff-a7dd-1e58cb37d98f.cpp b/gcd/Euclid-division/de395590-6d95-49ff-a7dd-1e58cb37d98f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68ce78ee6cba9210d95c9113395524a3c654962f --- /dev/null +++ b/gcd/Euclid-division/de395590-6d95-49ff-a7dd-1e58cb37d98f.cpp @@ -0,0 +1,65 @@ +#include +#include + + +using namespace std; + + + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + + + +int cmmdc(int a, int b) { + + if (a == 0 || b == 0) return a; + + if (a % b == 0) return b; + + if (a != 0 && b != 0) { + + a = a % b; + + return cmmdc(b, a); + + } + + return 0; + +} + + + +int main() + +{ + + int a, b, r; + + in >> a; + + in >> b; + + + + if (a > b) + + r = cmmdc(a, b); + + else + + r = cmmdc(b, a); + + + + if (r == 1) out << 0; + + else out << r; + + return 0; + +} diff --git a/gcd/Euclid-division/e258375e-09b9-4d67-8df4-889e65bc4ba8.cpp b/gcd/Euclid-division/e258375e-09b9-4d67-8df4-889e65bc4ba8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81498c0191739f6726f4f81d2ac40ba9a53012c7 --- /dev/null +++ b/gcd/Euclid-division/e258375e-09b9-4d67-8df4-889e65bc4ba8.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +int euclid(int x,int y) + +{ + + if(y==0) + + return x; + + else + + return euclid(y,x%y); + + + +} + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,c; + + fin>>a>>b; + + c=euclid(a,b); + + if(c!=1) + + fout< +#include +using namespace std; + + + + + +int main() + +{ + + ifstream fin("cmmdc.in", ios::in); + + ofstream fout("cmmdc.out", ios::out); + + long a, b,r; + + fin >> a >> b; + + r = a % b; + + while (r != 0) + + { + + + + a = b; + + b = r; + + r = a % b; + + } + + if (b == 1) + + b = 0; + + fout << b; + +} diff --git a/gcd/Euclid-division/e40482f6-9ea6-4358-9423-af68af079d2b.cpp b/gcd/Euclid-division/e40482f6-9ea6-4358-9423-af68af079d2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12f19b2ad6a73f6f14ab17593c637a7684692b43 --- /dev/null +++ b/gcd/Euclid-division/e40482f6-9ea6-4358-9423-af68af079d2b.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int x,int y){ + + if(x>n>>n2; + + int sol=cmmdc(n,n2); + + if(sol==1){ + + sol=0; + + } + + fout< +using namespace std; + +int a, b; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + f >> a >> b; + + while (b) { int r = a % b; a = b; b = r; } + + if (a == 1) g << 0; + + else g << a; + + f.close(); g.close(); + +} diff --git a/gcd/Euclid-division/e66e65b6-a777-4cb3-8353-60a972bda4bc.cpp b/gcd/Euclid-division/e66e65b6-a777-4cb3-8353-60a972bda4bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc45446d22addfc05569e5701fb7461dbea06c99 --- /dev/null +++ b/gcd/Euclid-division/e66e65b6-a777-4cb3-8353-60a972bda4bc.cpp @@ -0,0 +1,32 @@ +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,p; + +int main() + +{ + + f>>a>>b; + + while(a%b!=0){p=a%b; + + a=b; + + b=p; + + } + + if(p!=1)g< +using namespace std; + +int cmmdc(int a,int b) + +{ + + int r; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + return a; + +} + +int main() + +{ + freopen("cmmdc.in","r",stdin); + + freopen("cmmdc.out","w",stdout); + + + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + cout.tie(NULL); + + int a,b; + + cin>>a>>b; + + if(cmmdc(a,b)!=1) + + cout< +#include +using namespace std; + + + +int main() + +{ + + int r,a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while(b){ + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + g<<0; + + else + + g< + + +using namespace std; + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + +int main() + +{ + + int a,b,c; + + in>>a>>b; + + while (b) + + { + + c=a%b; + + a=b; + + b=c; + + } + + if (a==1) + + out <<0; + + else + + out < +using namespace std; + + + +string problem = "cmmdc"; + +ifstream fin (problem + ".in"); + +ofstream fout(problem + ".out"); + + + + + + + +int gcd(int a,int b) + +{ + + if(!b) + + return a; + + return gcd(b,a % b); + +} + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + if(gcd(a,b) == 1) + + { + + fout << 0 << "\n"; + + return 0; + + } + + fout << gcd(a,b); + +} diff --git a/gcd/Euclid-division/ed49493b-b175-4c7f-a5fb-6e99bf2b4713.cpp b/gcd/Euclid-division/ed49493b-b175-4c7f-a5fb-6e99bf2b4713.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dde9ddd7901647c3ddce21e6922a1ccb37595465 --- /dev/null +++ b/gcd/Euclid-division/ed49493b-b175-4c7f-a5fb-6e99bf2b4713.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int a, b, r; + +int main() { + + in >> a >> b; + + while (b != 0) { + + r = a%b; + + a = b; + + b = r; + + } + + if (a == 1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-division/edc271b1-5b35-43db-9da0-1d075ef144c1.cpp b/gcd/Euclid-division/edc271b1-5b35-43db-9da0-1d075ef144c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1257e34ddec292a1661409b59d9435275ecbc19 --- /dev/null +++ b/gcd/Euclid-division/edc271b1-5b35-43db-9da0-1d075ef144c1.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main () + +{ + + int a,b,r; + + fin>>a>>b; + + while(b>0) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + { + + fout<<0; + + } + + else + + { + + fout< +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int CMMDC(int a, int b) + +{ + + int c; + + while (b) + + { + + c = a % b; + + a = b; + + b = c; + + } + + return a; + +} + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + if (CMMDC(a, b) == 1) + + g << 0; + + else + + g << CMMDC(a, b); + + return 0; + +} diff --git a/gcd/Euclid-division/fa015731-e52d-4ae7-b984-aa6d451539e9.cpp b/gcd/Euclid-division/fa015731-e52d-4ae7-b984-aa6d451539e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8341f78d8240b6c8d49869a2e9f8fe3739c0dec --- /dev/null +++ b/gcd/Euclid-division/fa015731-e52d-4ae7-b984-aa6d451539e9.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + + int a, b; + + f>>a>>b; + + while(b!=0) + + { + + int r=a%b; + + a=b; + + b=r; + + + + } + + if(a!=1) + + g< +using namespace std; + +ifstream fin ("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int x,y; + + fin>>x>>y; + + while(y) + + { + + int r=x%y; + + x=y; + + y=r; + + } + + if(x!=1) + + fout< +#include + + +using namespace std; + +ifstream i("cmmdc.in"); + +ofstream o("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + i>> a >> b; + + while(b > 0) + + { + + int rest = a%b; + + a = b; + + b = rest; + + + + } + + if (a==1) + + { + + o<< 0; + + } else + + { + + o<< a; + + } + + return 0; + +} diff --git a/gcd/Euclid-division/fc984c82-085e-433d-ac8f-9186fad65b7d.cpp b/gcd/Euclid-division/fc984c82-085e-433d-ac8f-9186fad65b7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29f751894aea30f6e6bff6ce025184ec9c8baada --- /dev/null +++ b/gcd/Euclid-division/fc984c82-085e-433d-ac8f-9186fad65b7d.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int main() { + int a,b; + f>>a>>b; + while(b!=0){ + int c=a%b; + a=b; + b=c; + } + if(a==1) + g<<0; + else g< +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b, c; + + f >> a >> b; + + while(b) + + { + + c = a % b; + + a = b; + + b = c; + + } + + if(a == 1)g << 0; + + else g << a; + +} diff --git a/gcd/Euclid-substraction/0138a617-5a1b-4942-86cd-d6fc71741c41.cpp b/gcd/Euclid-substraction/0138a617-5a1b-4942-86cd-d6fc71741c41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69b35185c9783409dc77d6e11bf23a13fab82351 --- /dev/null +++ b/gcd/Euclid-substraction/0138a617-5a1b-4942-86cd-d6fc71741c41.cpp @@ -0,0 +1,43 @@ +#include +#include + + +using namespace std; + +int main() { + + fstream citirein("cmmdc.in",ios::in); + + ofstream output("cmmdc.out",ios::out); + + int a,b; + + citirein>>a>>b; + + citirein.close(); + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + a=0; + + output< +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/06687b41-a967-444e-8a22-5699458fc132.cpp b/gcd/Euclid-substraction/06687b41-a967-444e-8a22-5699458fc132.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dbc8f8a19f11a251f3141e4dee9d8a0837e4620 --- /dev/null +++ b/gcd/Euclid-substraction/06687b41-a967-444e-8a22-5699458fc132.cpp @@ -0,0 +1,61 @@ +#include +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include + + +std::ifstream f("cmmdc.in"); + +std::ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while (a != b && a>0 && b>0) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + g << 0; + + else + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/090152b9-dab2-4cf9-967a-84edb6e2c80c.cpp b/gcd/Euclid-substraction/090152b9-dab2-4cf9-967a-84edb6e2c80c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1c898f13cfb6d650a26d1df1cbcd23f835782f1 --- /dev/null +++ b/gcd/Euclid-substraction/090152b9-dab2-4cf9-967a-84edb6e2c80c.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + if(a==0 && b!=0) + + { + + g<b) + + { + + a=a-b; + + } + + else + + { + + b=b-a; + + } + + } + + if(a==1) + + { + + g<<0; + + } + + else + + { + + g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main () { + + int a, b; + + f >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } else { + + b = b - a; + + } + + } + + if (a == 1) a = 0; + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/0be21d21-8783-4c01-9943-e0a30161d38f.cpp b/gcd/Euclid-substraction/0be21d21-8783-4c01-9943-e0a30161d38f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23d3dfbf60e2fcc3f09fd93c83f3dbcd092c6ad0 --- /dev/null +++ b/gcd/Euclid-substraction/0be21d21-8783-4c01-9943-e0a30161d38f.cpp @@ -0,0 +1,53 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + + + +int main() + +{ + + unsigned int a,b; + + in >> a >> b; + + while(a!=b){ + + if(a > b){ + + a = a - b; + + } + + else if(b > a){ + + b = b - a; + + } + + } + + if(a == 1){ + + out << 0; + + } + + else{ + + out << a; + + } + +} diff --git a/gcd/Euclid-substraction/0d78c1a1-a5b4-4427-ab85-d0ad72829415.cpp b/gcd/Euclid-substraction/0d78c1a1-a5b4-4427-ab85-d0ad72829415.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c31ab30bb715583d3a98a97cf7ced8f10cffebc7 --- /dev/null +++ b/gcd/Euclid-substraction/0d78c1a1-a5b4-4427-ab85-d0ad72829415.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a > 1) + + fout << a; + + else + + fout << 0; + +} diff --git a/gcd/Euclid-substraction/0d88cf3c-aea9-4be2-9485-d42f06a37351.cpp b/gcd/Euclid-substraction/0d88cf3c-aea9-4be2-9485-d42f06a37351.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed4bf9a2dc22f08b1a6eaccfd068e57508383e66 --- /dev/null +++ b/gcd/Euclid-substraction/0d88cf3c-aea9-4be2-9485-d42f06a37351.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,diva=0,divb=0; + + in >> a; + + in >> b; + while(a!=b) + + { + + if(a > b) + + { + + a = a - b; + + } + + else + + { + + b = b - a; + + } + + } + + if(a==1) out << "0"; + + else out << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/0d897bb8-8042-4804-9ade-5cf6c7539f62.cpp b/gcd/Euclid-substraction/0d897bb8-8042-4804-9ade-5cf6c7539f62.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc485aece85551b25def2ad3a30e756d33eb1c83 --- /dev/null +++ b/gcd/Euclid-substraction/0d897bb8-8042-4804-9ade-5cf6c7539f62.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + +int a,b; + +f>>a>>b; + +while(a!=b){ + + if(a>b)a=a-b; + + else b=b-a; + +} + +if (a==1)g<<0; + +else g< + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int a,b; + +int main() +{ + f>>a>>b; + + while (a!=b) + { + if(a>b) + { + a=a-b; + } + else + { + b=b-a; + } + + } + + if(a==1) + { + g<<0; + } + else + g< + + +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a,b; + + cin >> a >> b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + cout << "0"; + + else + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/10251c98-c8f2-4064-b81b-0d2d7f8f5ab5.cpp b/gcd/Euclid-substraction/10251c98-c8f2-4064-b81b-0d2d7f8f5ab5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5031b0ac61f7f708e17d15cd4c2da9973e8b384 --- /dev/null +++ b/gcd/Euclid-substraction/10251c98-c8f2-4064-b81b-0d2d7f8f5ab5.cpp @@ -0,0 +1,63 @@ +#include +#include + + +using namespace std; + + + +int main() + +{ double a,b; + + + + std::ifstream file1; + + std::ofstream file2; + + file1.open("cmmdc.in"); + + file1>>a; + + file1>>b; + + + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + + + + + file2.open("cmmdc.out"); + + if (a==1) + + file2<<0; + + else + + file2< + + +using namespace std; + + + +int main() + +{ + + int a, b; + + + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + fin >> a >> b; + + + + while (a != b) + + { + + if (a > b) + + { + + a -= b; + + } + + else + + { + + b -= a; + + } + + } + + if (a == 1) + + { + + fout << 0; + + } + + else + + { + + fout << a; + + } + + + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/11c372db-87b8-4a82-8945-d25c97608f11.cpp b/gcd/Euclid-substraction/11c372db-87b8-4a82-8945-d25c97608f11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37464e4b69956534cd4819fd2530ca3c60e9cf87 --- /dev/null +++ b/gcd/Euclid-substraction/11c372db-87b8-4a82-8945-d25c97608f11.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + + + +int main() + +{ + + long long a,b; + + + + f>>a>>b; + + + + while (a!=b){ + + if (a>b){ + + a=a-b; + + }else b=b-a; + + } + + if (a==1 && b==1) + + g<<"0"; + + else { + + if (a>b) + + g< +#include + + +using namespace std; + + + +int main() + +{ + + ifstream in; + + ofstream out; + + int a, b; + + + + in.open("cmmdc.in"); + + out.open("cmmdc.out"); + + + + in >> a >> b; + + while (a != b){ + + if (a > b) + + a -= b; + + if (b > a) + + b -= a; + + } + + + + if (a == 1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/1425a0c9-7f3b-4dc0-b402-5456b76abd2c.cpp b/gcd/Euclid-substraction/1425a0c9-7f3b-4dc0-b402-5456b76abd2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb0049aa9900f0dad53873d68a302e7e28a7d7a5 --- /dev/null +++ b/gcd/Euclid-substraction/1425a0c9-7f3b-4dc0-b402-5456b76abd2c.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int main() + +{ + + long a; + + long b; + + in >> a; + + in >> b; + + while(a != b) + + { + + if(a > b) + + a = a - b; + + else b = b - a; + + } + + if(a != 1) + + { + + out << a; + + } + + else out << 0; + + out.close(); + + in.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/148a6b68-137d-11eb-a1fb-309c234925a0.cpp b/gcd/Euclid-substraction/148a6b68-137d-11eb-a1fb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ee9a3b11a57ce5de90fb5d94de7b16c0e9109ac --- /dev/null +++ b/gcd/Euclid-substraction/148a6b68-137d-11eb-a1fb-309c234925a0.cpp @@ -0,0 +1,44 @@ +#include + + +int main() + +{ + + int a,b; + + FILE* p,*q; + + p = fopen("cmmdc.in", "r"); + + q = fopen("cmmdc.out", "w"); + + fscanf(p,"%d%d", &a,&b); + + + + while (a != b) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + fprintf(q, "%c", '0'); + + else + + fprintf(q, "%d", a); + + fclose(p); + + fclose(q); + + return 0; + +} diff --git a/gcd/Euclid-substraction/1507abc0-137d-11eb-94d8-309c234925a0.cpp b/gcd/Euclid-substraction/1507abc0-137d-11eb-94d8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc485aece85551b25def2ad3a30e756d33eb1c83 --- /dev/null +++ b/gcd/Euclid-substraction/1507abc0-137d-11eb-94d8-309c234925a0.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + +int a,b; + +f>>a>>b; + +while(a!=b){ + + if(a>b)a=a-b; + + else b=b-a; + +} + +if (a==1)g<<0; + +else g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a,b; + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + fout<<"0"; + + else + + fout< +#include +using namespace std; + + + + unsigned int a, b; + +ifstream citire("cmmdc.in"); + +ofstream afisare("cmmdc.out"); + +int main() + +{ + + while (!citire.eof()) + + citire >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + { + + a = 0; + + } + + + + afisare << a; + + afisare.close(); + + system("pause"); + + return 0; + +} diff --git a/gcd/Euclid-substraction/15bd1c2e-137d-11eb-91de-309c234925a0.cpp b/gcd/Euclid-substraction/15bd1c2e-137d-11eb-91de-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7de850e21e35057ea695bd56d0f24cd3dd320a5 --- /dev/null +++ b/gcd/Euclid-substraction/15bd1c2e-137d-11eb-91de-309c234925a0.cpp @@ -0,0 +1,45 @@ +#include +#include + + +using namespace std; + + + +int main() { + + + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else b = b - a; + + } + + + + if (a == 1) { + + out << 0; + + } + + else out << a; + +} diff --git a/gcd/Euclid-substraction/15f913b4-137d-11eb-a5d3-309c234925a0.cpp b/gcd/Euclid-substraction/15f913b4-137d-11eb-a5d3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9378c645b70bab0228ef6f23e493f8b0370011d4 --- /dev/null +++ b/gcd/Euclid-substraction/15f913b4-137d-11eb-a5d3-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else + + fout< +using namespace std; + + + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int main () { + + int a, b; + + cin >> a >> b; + + while (a != b) { + + if (a > b) a -= b; + + else if (a < b) b -= a; + + } + + if (a == 1) { + + cout << 0; + + return 0; + + } + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/163d6558-de66-4de4-a374-6d9301328105.cpp b/gcd/Euclid-substraction/163d6558-de66-4de4-a374-6d9301328105.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29bc017a1cd5a9625c4544d93af2e6f3d8a67a9f --- /dev/null +++ b/gcd/Euclid-substraction/163d6558-de66-4de4-a374-6d9301328105.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std ; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + g<<0; + + else + + g< +#include +using namespace std; + + + +int main() { + + int a, b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f >> a; + + f >> b; + + + + + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) g << 0; + + else g << a; + + + +} diff --git a/gcd/Euclid-substraction/169bd8ca-137d-11eb-94bf-309c234925a0.cpp b/gcd/Euclid-substraction/169bd8ca-137d-11eb-94bf-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b568bd4dd788c2da43c11e5d1ce8ef0682dda2ce --- /dev/null +++ b/gcd/Euclid-substraction/169bd8ca-137d-11eb-94bf-309c234925a0.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int a,b; + +int main() +{ + f>>a>>b; + + while (a!=b) + { + if(a>b) + { + a=a-b; + } + else + { + b=b-a; + } + + } + + if(a==1) + { + g<<0; + } + else + g< +#include + + + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + if (a < b) + + swap(a, b); + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + + + } + + return a; + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + int a, b; + + f >> a >> b; + + int cmmdcNR = cmmdc(a, b); + + + + + + + + ofstream g("cmmdc.out"); + + if (cmmdcNR == 1) + + g << 0; + + else + + g << cmmdcNR; + +} diff --git a/gcd/Euclid-substraction/171722d2-137d-11eb-a7af-309c234925a0.cpp b/gcd/Euclid-substraction/171722d2-137d-11eb-a7af-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e40154f383061eab663b6d1e3b9bfd85e372aa41 --- /dev/null +++ b/gcd/Euclid-substraction/171722d2-137d-11eb-a7af-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +int main() + +{f>>a>>b; + +while(a!=b) + +{ + + if(a>b) + + a=a-b; + + else + + b=b-a; + +} + +if(a!=1) + + g < +#include + + +std::ifstream f("cmmdc.in"); + +std::ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while (a != b && a>0 && b>0) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + g << 0; + + else + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/1755a928-137d-11eb-9a41-309c234925a0.cpp b/gcd/Euclid-substraction/1755a928-137d-11eb-9a41-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c31ab30bb715583d3a98a97cf7ced8f10cffebc7 --- /dev/null +++ b/gcd/Euclid-substraction/1755a928-137d-11eb-9a41-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a > 1) + + fout << a; + + else + + fout << 0; + +} diff --git a/gcd/Euclid-substraction/18094258-137d-11eb-a9bd-309c234925a0.cpp b/gcd/Euclid-substraction/18094258-137d-11eb-a9bd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95b327ded18509a3382612fc81c8ea7ea1d2b57 --- /dev/null +++ b/gcd/Euclid-substraction/18094258-137d-11eb-a9bd-309c234925a0.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; +typedef long long ll; +typedef vector vi; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int a, b, r; + fin >> a >> b; + r = a%b; + while(r!=0) { + a=b; + b=r; + r=a%b; + } + if(b==1) { + fout << 0; + return 0; + } + while(a!=b) { + if(a>b) + a-=b; + else + b-=a; + } + fout << a; + return 0; +} diff --git a/gcd/Euclid-substraction/1876f42e-137d-11eb-85b4-309c234925a0.cpp b/gcd/Euclid-substraction/1876f42e-137d-11eb-85b4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb0049aa9900f0dad53873d68a302e7e28a7d7a5 --- /dev/null +++ b/gcd/Euclid-substraction/1876f42e-137d-11eb-85b4-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int main() + +{ + + long a; + + long b; + + in >> a; + + in >> b; + + while(a != b) + + { + + if(a > b) + + a = a - b; + + else b = b - a; + + } + + if(a != 1) + + { + + out << a; + + } + + else out << 0; + + out.close(); + + in.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/18b2c678-137d-11eb-b485-309c234925a0.cpp b/gcd/Euclid-substraction/18b2c678-137d-11eb-b485-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dbc8f8a19f11a251f3141e4dee9d8a0837e4620 --- /dev/null +++ b/gcd/Euclid-substraction/18b2c678-137d-11eb-b485-309c234925a0.cpp @@ -0,0 +1,61 @@ +#include +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< + + +using namespace std; + + + +int main() + +{int a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while(a!=b) + + {if(a>b) a=a-b; + + else b=b-a;} + + if(a==b && a!=1) g< +#include + + +using namespace std; + +ifstream f ("cmmdc.in"); + +ofstream g ("cmmdc.out"); + + + +unsigned long int cmmdc (unsigned long int a , unsigned long int b) + +{ + + while(a!=b) + + if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + return 0; + + return a; + +} + + + +unsigned int a,b ; + +int main() + +{ + + f>>a>>b; + + g< +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + + + +int cmmdc ( int a , int b) + +{ + + while(a!=b) + + { + + if(a > b) + + a-=b; + + else + + b-=a; + + } + + if(a != 1) + + return a; + + else + + return 0; + +} + + + +int main() + +{ + + fstream cit ("cmmdc.in"); + + ofstream afis ("cmmdc.out"); + + + + int a, b; + + + + cit >> a >> b; + + + + afis< +#include +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + +int main() +{ + short a,b; + ifstream in("cmmdc.in"); + in >>a >>b; + in.close(); + while(a>0 and b>0) + if(a>b) + a-=b; + else + b-=a; + ofstream out("cmmdc.out"); + if(a<=0) + if(b<=1) + out <<0; + else + out < + + +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a,b; + + cin >> a >> b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + cout << "0"; + + else + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/1b868728-137d-11eb-97d5-309c234925a0.cpp b/gcd/Euclid-substraction/1b868728-137d-11eb-97d5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e48165fcb4aaba68f29c7196d7081cc80a2030ee --- /dev/null +++ b/gcd/Euclid-substraction/1b868728-137d-11eb-97d5-309c234925a0.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +int a,b; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ in>>a>>b; + + while(a!=b) if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b; + + fin>>a>>b; + + while(a>b || b>a) + + { + + if(a>b)a=a-b; + + if(a +using namespace std; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + if(a==0 && b!=0) + + { + + g<b) + + { + + a=a-b; + + } + + else + + { + + b=b-a; + + } + + } + + if(a==1) + + { + + g<<0; + + } + + else + + { + + g< + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + if (b > a) + + b = b - a; + + } + + if (a == 1) + + fout << 0; + + else fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/1c5022fa-137d-11eb-8ad9-309c234925a0.cpp b/gcd/Euclid-substraction/1c5022fa-137d-11eb-8ad9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d70a2537564aab604602a4fe707b32a0d71fd14 --- /dev/null +++ b/gcd/Euclid-substraction/1c5022fa-137d-11eb-8ad9-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + + + +ifstream intrare("cmmdc.in"); + +ofstream iesire("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + intrare >> a; + + intrare >> b; + + while(a != b) + + if(a > b) + + a = a - b; + + else + + b = b -a; + + int cmmdc = a; + + if(cmmdc == 1) + + iesire << 0; + + else + + iesire << cmmdc; + + + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/1d5787e6-137d-11eb-974d-309c234925a0.cpp b/gcd/Euclid-substraction/1d5787e6-137d-11eb-974d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd4ddfefa094186548ebf98d0e88289c6c85bd53 --- /dev/null +++ b/gcd/Euclid-substraction/1d5787e6-137d-11eb-974d-309c234925a0.cpp @@ -0,0 +1,26 @@ +#include +std::ifstream in ("cmmdc.in"); std::ofstream out ("cmmdc.out"); + + + +int main (){ + + std::ios::sync_with_stdio(false); + + int a,b; + + in>>a>>b; + + while(a!=b){ + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,cmmdc; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a== 1) + + fout<<0; + + else + + fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + + + } + + if(b==1) + + b=0; + + fout< + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int n,int n2){ + + while(n!=n2){ + + if(n>n2){ + + n=n-n2; + + }else{ + + n2=n2-n; + + } + + } + + return n; + +} +int main(){ + + int n,n2; + + fin>>n>>n2; + + int sol=cmmdc(n,n2); + + if(sol==1){ + + sol=0; + + } + + fout< + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + while(a!=b) + + { + + if (a>b) + + a-=b; + + else + + b-=a; + + } + + if (a==1 && b==1) + + return 0; + + return a; + +} + +int main() + +{ + + int a, b; + + cin >> a >> b; + + cout << cmmdc(a,b); + + return 0; + +} diff --git a/gcd/Euclid-substraction/1e55bde8-137d-11eb-94a7-309c234925a0.cpp b/gcd/Euclid-substraction/1e55bde8-137d-11eb-94a7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1663c5a790c368c3a05b70455d518885279919f --- /dev/null +++ b/gcd/Euclid-substraction/1e55bde8-137d-11eb-94a7-309c234925a0.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +ifstream in("cmmdc.in"); +ofstream out("cmmdc.out"); + +int main() +{ int a,b,aux=0; +in>>a>>b; + while(b!=a){ + if(a>b) + a-=b; + if(b>a) + b-=a; + } + + if(a==1){ + out<<"0"; + }else{ + out< +#include +int main() +{ + int a, b; + std::cin>>a>>b; + std:: ofstream myfile; + myfile.open ("cmmdc.in"); + myfile<< a << "\n" << b; + myfile.close(); + while (a != b) + { + if(a>b) + a -= b; + else if(a + +using namespace std; +typedef long long ll; +typedef vector vi; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int a, b, r; + fin >> a >> b; + r = a%b; + while(r!=0) { + a=b; + b=r; + r=a%b; + } + if(b==1) { + fout << 0; + return 0; + } + while(a!=b) { + if(a>b) + a-=b; + else + b-=a; + } + fout << a; + return 0; +} diff --git a/gcd/Euclid-substraction/1f02b2e4-137d-11eb-93fe-309c234925a0.cpp b/gcd/Euclid-substraction/1f02b2e4-137d-11eb-93fe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29bc017a1cd5a9625c4544d93af2e6f3d8a67a9f --- /dev/null +++ b/gcd/Euclid-substraction/1f02b2e4-137d-11eb-93fe-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std ; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + g<<0; + + else + + g< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main(void) + +{ + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1 || b==1) + + g<<0; + + else + + g< +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/1f517d74-137d-11eb-8939-309c234925a0.cpp b/gcd/Euclid-substraction/1f517d74-137d-11eb-8939-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/1f517d74-137d-11eb-8939-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/1f7224de-137d-11eb-bdd4-309c234925a0.cpp b/gcd/Euclid-substraction/1f7224de-137d-11eb-bdd4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1a92b2a89090a865b2d836adf840093337591ce --- /dev/null +++ b/gcd/Euclid-substraction/1f7224de-137d-11eb-bdd4-309c234925a0.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main () { + + int a, b; + + f >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } else { + + b = b - a; + + } + + } + + if (a == 1) a = 0; + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/1f8f7a36-137d-11eb-b7df-309c234925a0.cpp b/gcd/Euclid-substraction/1f8f7a36-137d-11eb-b7df-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32f6398b4a7a5d629bfa07fdef137827e215a0a7 --- /dev/null +++ b/gcd/Euclid-substraction/1f8f7a36-137d-11eb-b7df-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + + { + + int a,b; + + fin >> a >> b; + + + + while (a!=b) + + { + + if (a>b) + + a-=b; + + + + else + + b-=a; + + } + + + + if (a<=1 || b<=1) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/1f9e02de-137d-11eb-b0f4-309c234925a0.cpp b/gcd/Euclid-substraction/1f9e02de-137d-11eb-b0f4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/1f9e02de-137d-11eb-b0f4-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/209f0cc3-1416-4fd8-a1d7-15d937ad25cb.cpp b/gcd/Euclid-substraction/209f0cc3-1416-4fd8-a1d7-15d937ad25cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/209f0cc3-1416-4fd8-a1d7-15d937ad25cb.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/2106be62-137d-11eb-9ff8-309c234925a0.cpp b/gcd/Euclid-substraction/2106be62-137d-11eb-9ff8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/2106be62-137d-11eb-9ff8-309c234925a0.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/21158b6e-137d-11eb-8d18-309c234925a0.cpp b/gcd/Euclid-substraction/21158b6e-137d-11eb-8d18-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8d9aeb601dab3f17f73b03770ccc3f38ca9acd --- /dev/null +++ b/gcd/Euclid-substraction/21158b6e-137d-11eb-8d18-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + fin >> a; + + fin >> b; + + + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else { + + b = b - a; + + } + + } + + if (a == 1 || b == 1) { + + fout << 0; + + } + + else { + + fout << a; + + } + +} diff --git a/gcd/Euclid-substraction/2178bd40-137d-11eb-85a6-309c234925a0.cpp b/gcd/Euclid-substraction/2178bd40-137d-11eb-85a6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebf6d811c460d729d016b277709c0c48c165f5a4 --- /dev/null +++ b/gcd/Euclid-substraction/2178bd40-137d-11eb-85a6-309c234925a0.cpp @@ -0,0 +1,49 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while(a != b) + + { + + if(a>b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + g << "0"; + + else + + g << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/21891152-137d-11eb-8316-309c234925a0.cpp b/gcd/Euclid-substraction/21891152-137d-11eb-8316-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b72c39d1a6dc5ec4b251cd83fae8d01a2c9e4ac9 --- /dev/null +++ b/gcd/Euclid-substraction/21891152-137d-11eb-8316-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + + + +int main() + +{ + + int a, b; + + + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + fin >> a >> b; + + + + while (a != b) + + { + + if (a > b) + + { + + a -= b; + + } + + else + + { + + b -= a; + + } + + } + + if (a == 1) + + { + + fout << 0; + + } + + else + + { + + fout << a; + + } + + + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/2198056e-137d-11eb-a144-309c234925a0.cpp b/gcd/Euclid-substraction/2198056e-137d-11eb-a144-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc045f20b8c6fc43e6564ca10cd8e533a5d6a418 --- /dev/null +++ b/gcd/Euclid-substraction/2198056e-137d-11eb-a144-309c234925a0.cpp @@ -0,0 +1,63 @@ +#include +#include + + + + +using namespace std; + + + +int main() + +{ + + int a,b,cmmdc; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if(a==1){ + + cmmdc=0; + + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, rest; + + fin>>a>>b; + + while (a!=b){ + + if (a>b) a=a-b; + + else b=b-a; + + }if (a==1) fout << "0"; + + else fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/22490ce2-137d-11eb-9f73-309c234925a0.cpp b/gcd/Euclid-substraction/22490ce2-137d-11eb-9f73-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519a979ab60071f8c220314a17ec283514279e3e --- /dev/null +++ b/gcd/Euclid-substraction/22490ce2-137d-11eb-9f73-309c234925a0.cpp @@ -0,0 +1,30 @@ +#include +# include + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int main() +{ + int a,b; + f>>a>>b; + + while(a!=b) + { + if(b1) + g< +#include + + +using namespace std; + + + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + if (b > a) { + + b = b - a; + + } + + } + + if (a == 1) { + + fout << 0; + + } else { + + fout << a; + + } + + return 0; + +} diff --git a/gcd/Euclid-substraction/2311ec40-137d-11eb-a906-309c234925a0.cpp b/gcd/Euclid-substraction/2311ec40-137d-11eb-a906-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47f33a7e0ad0565efa174972cdb9de52d5359f66 --- /dev/null +++ b/gcd/Euclid-substraction/2311ec40-137d-11eb-a906-309c234925a0.cpp @@ -0,0 +1,40 @@ +#include + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< +#include +using namespace std; + + + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,A,B; + +f>>a>>b; + +A=a; + +B=b; + +while(A!=B){ + +if(A>B) A=A-B; + +else B=B-A; + +} + +if(A>1) g< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,cmmdc; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a== 1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +int main() + +{ double a,b; + + + + std::ifstream file1; + + std::ofstream file2; + + file1.open("cmmdc.in"); + + file1>>a; + + file1>>b; + + + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + + + + + file2.open("cmmdc.out"); + + if (a==1) + + file2<<0; + + else + + file2< +#include +using namespace std; + + + +int cmmdc ( int a , int b) + +{ + + while(a!=b) + + { + + if(a > b) + + a-=b; + + else + + b-=a; + + } + + if(a != 1) + + return a; + + else + + return 0; + +} + + + +int main() + +{ + + fstream cit ("cmmdc.in"); + + ofstream afis ("cmmdc.out"); + + + + int a, b; + + + + cit >> a >> b; + + + + afis< +#include +using namespace std; + + + +int main() { + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + a = 0; + + out << a; + + return 0; + + + +} diff --git a/gcd/Euclid-substraction/253b1798-137d-11eb-85b1-309c234925a0.cpp b/gcd/Euclid-substraction/253b1798-137d-11eb-85b1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37464e4b69956534cd4819fd2530ca3c60e9cf87 --- /dev/null +++ b/gcd/Euclid-substraction/253b1798-137d-11eb-85b1-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + + + +int main() + +{ + + long long a,b; + + + + f>>a>>b; + + + + while (a!=b){ + + if (a>b){ + + a=a-b; + + }else b=b-a; + + } + + if (a==1 && b==1) + + g<<"0"; + + else { + + if (a>b) + + g< +#include + + +using namespace std; + + + +ifstream intrare("cmmdc.in"); + +ofstream iesire("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + intrare >> a; + + intrare >> b; + + while(a != b) + + if(a > b) + + a = a - b; + + else + + b = b -a; + + int cmmdc = a; + + if(cmmdc == 1) + + iesire << 0; + + else + + iesire << cmmdc; + + + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/25655bc6-137d-11eb-b42e-309c234925a0.cpp b/gcd/Euclid-substraction/25655bc6-137d-11eb-b42e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6814fc10901ed1f686fdd67eb01de092b1417cf1 --- /dev/null +++ b/gcd/Euclid-substraction/25655bc6-137d-11eb-b42e-309c234925a0.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int cmmdc(int a, int b) + +{ + + + + while (a != b ) + + { + + if (a > b) a = a - b; + + else b = b - a; + + + + } + + return a; + +} + +int main() + +{ + + int a, b; + + f >> a >> b; + + if (cmmdc(a, b) == 1) g << 0; + + else g << cmmdc(a, b); + + + + f.close(); + + g.close(); + +} diff --git a/gcd/Euclid-substraction/25c996da-137d-11eb-9b6f-309c234925a0.cpp b/gcd/Euclid-substraction/25c996da-137d-11eb-9b6f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a50a6c9650180551ccbb75734c08572663179fd7 --- /dev/null +++ b/gcd/Euclid-substraction/25c996da-137d-11eb-9b6f-309c234925a0.cpp @@ -0,0 +1,45 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + while (a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + if (a==1) + + fout << 0; + + else fout << a; + +} diff --git a/gcd/Euclid-substraction/25f47c28-137d-11eb-bf2b-309c234925a0.cpp b/gcd/Euclid-substraction/25f47c28-137d-11eb-bf2b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69b35185c9783409dc77d6e11bf23a13fab82351 --- /dev/null +++ b/gcd/Euclid-substraction/25f47c28-137d-11eb-bf2b-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include + + +using namespace std; + +int main() { + + fstream citirein("cmmdc.in",ios::in); + + ofstream output("cmmdc.out",ios::out); + + int a,b; + + citirein>>a>>b; + + citirein.close(); + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + a=0; + + output< +#include +using namespace std; + + + +int main () + +{ + + ifstream f("cmmdc.in"); + + long a,b; + + f>>a>>b; + + ofstream o("cmmdc.out"); + + while(a!=b) + + if (a>b) + + a=a-b; + + else + + b=b-a; + + + + if(a==1) + + a=0; + + o< + + +using namespace std; + + + +int main() + +{ + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int s,a,b; + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + { + + fout<<0; + + } + + else + + fout< +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,diva=0,divb=0; + + in >> a; + + in >> b; + while(a!=b) + + { + + if(a > b) + + { + + a = a - b; + + } + + else + + { + + b = b - a; + + } + + } + + if(a==1) out << "0"; + + else out << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/27989118-137d-11eb-b2de-309c234925a0.cpp b/gcd/Euclid-substraction/27989118-137d-11eb-b2de-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8039e05f12dad3bf3d2ffc66cad6fcacde2e11 --- /dev/null +++ b/gcd/Euclid-substraction/27989118-137d-11eb-b2de-309c234925a0.cpp @@ -0,0 +1,40 @@ + #include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +long long a,b; + +int main() + +{ + + f>>a>>b; + + while (a!=b) + + { + + if (a +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a, b; + +int cmmdc (int a, int b) + +{ + + if (a==b) + + if (a==1) + + return 0; + + else + + return a; + + else + + if (a>b) + + return cmmdc(a-b,b); + + else + + return cmmdc(a,b-a); + +} + +int main() + +{ + + f >> a >> b; + + g << cmmdc(a,b); + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/27c76982-137d-11eb-95f2-309c234925a0.cpp b/gcd/Euclid-substraction/27c76982-137d-11eb-95f2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8039e05f12dad3bf3d2ffc66cad6fcacde2e11 --- /dev/null +++ b/gcd/Euclid-substraction/27c76982-137d-11eb-95f2-309c234925a0.cpp @@ -0,0 +1,40 @@ + #include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< + + +using namespace std; + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + +f>>a>>b; + +while(a!=b) + +{ if(a>b) + +{a=a-b; + +} + +else b=b-a; + +} + +if(a!=1) + +g< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< +#include + + +using namespace std; + + + +int main() + +{ + + ifstream in; + + ofstream out; + + int a, b; + + + + in.open("cmmdc.in"); + + out.open("cmmdc.out"); + + + + in >> a >> b; + + while (a != b){ + + if (a > b) + + a -= b; + + if (b > a) + + b -= a; + + } + + + + if (a == 1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/2939a40c-137d-11eb-a1be-309c234925a0.cpp b/gcd/Euclid-substraction/2939a40c-137d-11eb-a1be-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58e4a2252b442d542536e66494a4e0bd05bcc384 --- /dev/null +++ b/gcd/Euclid-substraction/2939a40c-137d-11eb-a1be-309c234925a0.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +int main() + +{ + + int a,b; + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + in>>a>>b; + + while(a!=b) { + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< +#include +using namespace std; + +int cmmd(int a, int b) { + while (a != b) { + if (a > b) { + a -= b; + } else { + b -= a; + } + } + if (a == 1) { + return 0; + } + return a; +} +int main() { + ifstream f("cmmdc.in"); + ofstream g("cmmdc.out"); + int a, b; + f >> a, f >> b; + g << cmmd(a, b); +} diff --git a/gcd/Euclid-substraction/2a56230c-137d-11eb-a8af-309c234925a0.cpp b/gcd/Euclid-substraction/2a56230c-137d-11eb-a8af-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23d3dfbf60e2fcc3f09fd93c83f3dbcd092c6ad0 --- /dev/null +++ b/gcd/Euclid-substraction/2a56230c-137d-11eb-a8af-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + + + +int main() + +{ + + unsigned int a,b; + + in >> a >> b; + + while(a!=b){ + + if(a > b){ + + a = a - b; + + } + + else if(b > a){ + + b = b - a; + + } + + } + + if(a == 1){ + + out << 0; + + } + + else{ + + out << a; + + } + +} diff --git a/gcd/Euclid-substraction/2a86248a-137d-11eb-a5cb-309c234925a0.cpp b/gcd/Euclid-substraction/2a86248a-137d-11eb-a5cb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4fc45015409b5d6d8c11cd8afea31e1f10b4a13 --- /dev/null +++ b/gcd/Euclid-substraction/2a86248a-137d-11eb-a5cb-309c234925a0.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int prime, a,b; + + + +int main() + +{ + + in>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + +int main() +{ + short a,b; + ifstream in("cmmdc.in"); + in >>a >>b; + in.close(); + while(a>0 and b>0) + if(a>b) + a-=b; + else + b-=a; + ofstream out("cmmdc.out"); + if(a<=0) + if(b<=1) + out <<0; + else + out < + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +long long a,b; + +int main() + +{ + + f>>a>>b; + + while (a!=b) + + { + + if (a +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int cmmdc(int a, int b) + +{ + + + + while (a != b ) + + { + + if (a > b) a = a - b; + + else b = b - a; + + + + } + + return a; + +} + +int main() + +{ + + int a, b; + + f >> a >> b; + + if (cmmdc(a, b) == 1) g << 0; + + else g << cmmdc(a, b); + + + + f.close(); + + g.close(); + +} diff --git a/gcd/Euclid-substraction/2e0ef70e-6575-4beb-855b-31d4b43d42f7.cpp b/gcd/Euclid-substraction/2e0ef70e-6575-4beb-855b-31d4b43d42f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e169e64856ca760e9896f211758102664c175394 --- /dev/null +++ b/gcd/Euclid-substraction/2e0ef70e-6575-4beb-855b-31d4b43d42f7.cpp @@ -0,0 +1,40 @@ +#include + + +using namespace std; + + + +int main() + +{ + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int n,int n2){ + + while(n!=n2){ + + if(n>n2){ + + n=n-n2; + + }else{ + + n2=n2-n; + + } + + } + + return n; + +} +int main(){ + + int n,n2; + + fin>>n>>n2; + + int sol=cmmdc(n,n2); + + if(sol==1){ + + sol=0; + + } + + fout< +std::ifstream in ("cmmdc.in"); std::ofstream out ("cmmdc.out"); + + + +int main (){ + + std::ios::sync_with_stdio(false); + + int a,b; + + in>>a>>b; + + while(a!=b){ + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< +#include + + +using namespace std; + + + +ifstream in ("cmmdc.in"); + +ofstream out ("cmmdc.out"); + + + + + +int main() + +{ + + unsigned int a,b; + + in >> a >> b; + + while(a!=b){ + + if(a > b){ + + a = a - b; + + } + + else if(b > a){ + + b = b - a; + + } + + } + + if(a == 1){ + + out << 0; + + } + + else{ + + out << a; + + } + +} diff --git a/gcd/Euclid-substraction/3eba8a59-2c3a-4e25-bbc4-c4e7d05f6aa6.cpp b/gcd/Euclid-substraction/3eba8a59-2c3a-4e25-bbc4-c4e7d05f6aa6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1663c5a790c368c3a05b70455d518885279919f --- /dev/null +++ b/gcd/Euclid-substraction/3eba8a59-2c3a-4e25-bbc4-c4e7d05f6aa6.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +ifstream in("cmmdc.in"); +ofstream out("cmmdc.out"); + +int main() +{ int a,b,aux=0; +in>>a>>b; + while(b!=a){ + if(a>b) + a-=b; + if(b>a) + b-=a; + } + + if(a==1){ + out<<"0"; + }else{ + out< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main(void) + +{ + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1 || b==1) + + g<<0; + + else + + g< +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ + + int a,b,diva=0,divb=0; + + in >> a; + + in >> b; + while(a!=b) + + { + + if(a > b) + + { + + a = a - b; + + } + + else + + { + + b = b - a; + + } + + } + + if(a==1) out << "0"; + + else out << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/44d1cd53-02e9-4b53-82b5-99f9ad3dc7c9.cpp b/gcd/Euclid-substraction/44d1cd53-02e9-4b53-82b5-99f9ad3dc7c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d373c3d3ed8d56cb15f7c5c0c95c87ae13e6403f --- /dev/null +++ b/gcd/Euclid-substraction/44d1cd53-02e9-4b53-82b5-99f9ad3dc7c9.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + + + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,A,B; + +f>>a>>b; + +A=a; + +B=b; + +while(A!=B){ + +if(A>B) A=A-B; + +else B=B-A; + +} + +if(A>1) g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + + + } + + if(b==1) + + b=0; + + fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a,b; + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + fout<<"0"; + + else + + fout< +#include +int main() +{ + int a, b; + std::cin>>a>>b; + std:: ofstream myfile; + myfile.open ("cmmdc.in"); + myfile<< a << "\n" << b; + myfile.close(); + while (a != b) + { + if(a>b) + a -= b; + else if(a +#include + + +using namespace std; + + + +int main() { + + + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else b = b - a; + + } + + + + if (a == 1) { + + out << 0; + + } + + else out << a; + +} diff --git a/gcd/Euclid-substraction/4dce11ad-f1ac-4d9b-8990-0d5bd9ada9c7.cpp b/gcd/Euclid-substraction/4dce11ad-f1ac-4d9b-8990-0d5bd9ada9c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eb61ce1174a63caa9b67cdad7e8af0e0947d22f --- /dev/null +++ b/gcd/Euclid-substraction/4dce11ad-f1ac-4d9b-8990-0d5bd9ada9c7.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + + unsigned int a, b; + +ifstream citire("cmmdc.in"); + +ofstream afisare("cmmdc.out"); + +int main() + +{ + + while (!citire.eof()) + + citire >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + { + + a = 0; + + } + + + + afisare << a; + + afisare.close(); + + system("pause"); + + return 0; + +} diff --git a/gcd/Euclid-substraction/4f10a4ce-738d-4615-ac67-8aea7b260d58.cpp b/gcd/Euclid-substraction/4f10a4ce-738d-4615-ac67-8aea7b260d58.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a50a6c9650180551ccbb75734c08572663179fd7 --- /dev/null +++ b/gcd/Euclid-substraction/4f10a4ce-738d-4615-ac67-8aea7b260d58.cpp @@ -0,0 +1,45 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + while (a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + if (a==1) + + fout << 0; + + else fout << a; + +} diff --git a/gcd/Euclid-substraction/513a7953-8203-49ed-bae2-15de86370724.cpp b/gcd/Euclid-substraction/513a7953-8203-49ed-bae2-15de86370724.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e03ec642b82539d16dc7f47f18ae8a730c753f78 --- /dev/null +++ b/gcd/Euclid-substraction/513a7953-8203-49ed-bae2-15de86370724.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + + + +int main () + +{ + + ifstream f("cmmdc.in"); + + long a,b; + + f>>a>>b; + + ofstream o("cmmdc.out"); + + while(a!=b) + + if (a>b) + + a=a-b; + + else + + b=b-a; + + + + if(a==1) + + a=0; + + o< + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int a,b; + +int main() +{ + f>>a>>b; + + while (a!=b) + { + if(a>b) + { + a=a-b; + } + else + { + b=b-a; + } + + } + + if(a==1) + { + g<<0; + } + else + g< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int s,a,b; + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + { + + fout<<0; + + } + + else + + fout< +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/5332c08b-c31d-49b9-acd3-6401ff95162b.cpp b/gcd/Euclid-substraction/5332c08b-c31d-49b9-acd3-6401ff95162b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85992ec63c7ba1a5c5b0cdbe22cade7245265989 --- /dev/null +++ b/gcd/Euclid-substraction/5332c08b-c31d-49b9-acd3-6401ff95162b.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + if (b > a) + + b = b - a; + + } + + if (a == 1) + + fout << 0; + + else fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/54cae2a7-d4cd-4ce1-b4e4-0b1f02eba115.cpp b/gcd/Euclid-substraction/54cae2a7-d4cd-4ce1-b4e4-0b1f02eba115.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29bc017a1cd5a9625c4544d93af2e6f3d8a67a9f --- /dev/null +++ b/gcd/Euclid-substraction/54cae2a7-d4cd-4ce1-b4e4-0b1f02eba115.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std ; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + g<<0; + + else + + g< + + +using namespace std; + + + +int main() + +{int a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while(a!=b) + + {if(a>b) a=a-b; + + else b=b-a;} + + if(a==b && a!=1) g< + + +using namespace std; + + + +int main() + +{ + + ifstream cin("cmmdc.in"); + + ofstream cout("cmmdc.out"); + + int a,b; + + cin >> a >> b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + cout << "0"; + + else + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/587f5467-1cf4-4ece-be2d-ae20e70c1e9d.cpp b/gcd/Euclid-substraction/587f5467-1cf4-4ece-be2d-ae20e70c1e9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4fc45015409b5d6d8c11cd8afea31e1f10b4a13 --- /dev/null +++ b/gcd/Euclid-substraction/587f5467-1cf4-4ece-be2d-ae20e70c1e9d.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int prime, a,b; + + + +int main() + +{ + + in>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1) + + out<<0; + + else + + out< + + +using namespace std; + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + +f>>a>>b; + +while(a!=b) + +{ if(a>b) + +{a=a-b; + +} + +else b=b-a; + +} + +if(a!=1) + +g< +#include +using namespace std; + + + +int main() { + + int a, b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f >> a; + + f >> b; + + + + + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) g << 0; + + else g << a; + + + +} diff --git a/gcd/Euclid-substraction/5f726f23-5132-44a2-9085-4a2c6fc82f0d.cpp b/gcd/Euclid-substraction/5f726f23-5132-44a2-9085-4a2c6fc82f0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95b327ded18509a3382612fc81c8ea7ea1d2b57 --- /dev/null +++ b/gcd/Euclid-substraction/5f726f23-5132-44a2-9085-4a2c6fc82f0d.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; +typedef long long ll; +typedef vector vi; +ifstream fin("cmmdc.in"); +ofstream fout("cmmdc.out"); + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + int a, b, r; + fin >> a >> b; + r = a%b; + while(r!=0) { + a=b; + b=r; + r=a%b; + } + if(b==1) { + fout << 0; + return 0; + } + while(a!=b) { + if(a>b) + a-=b; + else + b-=a; + } + fout << a; + return 0; +} diff --git a/gcd/Euclid-substraction/5fe8e30c-02a4-4818-a58a-72cc02c53d07.cpp b/gcd/Euclid-substraction/5fe8e30c-02a4-4818-a58a-72cc02c53d07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8039e05f12dad3bf3d2ffc66cad6fcacde2e11 --- /dev/null +++ b/gcd/Euclid-substraction/5fe8e30c-02a4-4818-a58a-72cc02c53d07.cpp @@ -0,0 +1,40 @@ + #include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< + + +int main() + +{ + + int a,b; + + FILE* p,*q; + + p = fopen("cmmdc.in", "r"); + + q = fopen("cmmdc.out", "w"); + + fscanf(p,"%d%d", &a,&b); + + + + while (a != b) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + fprintf(q, "%c", '0'); + + else + + fprintf(q, "%d", a); + + fclose(p); + + fclose(q); + + return 0; + +} diff --git a/gcd/Euclid-substraction/60be3f2a-8a11-4405-bcfc-040ca2f011a1.cpp b/gcd/Euclid-substraction/60be3f2a-8a11-4405-bcfc-040ca2f011a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..793d55f86e13e948dc735e10f6cc090571044efa --- /dev/null +++ b/gcd/Euclid-substraction/60be3f2a-8a11-4405-bcfc-040ca2f011a1.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + + + +int main() { + + int a, b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f >> a; + + f >> b; + + + + + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) g << 0; + + else g << a; + + + +} diff --git a/gcd/Euclid-substraction/610b857c-49bd-42ae-a9de-ef61e8984705.cpp b/gcd/Euclid-substraction/610b857c-49bd-42ae-a9de-ef61e8984705.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84a1aabf9cb35d2a0a35a0f2947f10a56c42087f --- /dev/null +++ b/gcd/Euclid-substraction/610b857c-49bd-42ae-a9de-ef61e8984705.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + + + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int main () { + + int a, b; + + cin >> a >> b; + + while (a != b) { + + if (a > b) a -= b; + + else if (a < b) b -= a; + + } + + if (a == 1) { + + cout << 0; + + return 0; + + } + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/63ef033b-61cc-4237-99d2-8eaeb8d7d0e2.cpp b/gcd/Euclid-substraction/63ef033b-61cc-4237-99d2-8eaeb8d7d0e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6e7087f50e09600b6aa5e52c36181d947590099 --- /dev/null +++ b/gcd/Euclid-substraction/63ef033b-61cc-4237-99d2-8eaeb8d7d0e2.cpp @@ -0,0 +1,50 @@ +#include + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + while(a!=b) + + { + + if (a>b) + + a-=b; + + else + + b-=a; + + } + + if (a==1 && b==1) + + return 0; + + return a; + +} + +int main() + +{ + + int a, b; + + cin >> a >> b; + + cout << cmmdc(a,b); + + return 0; + +} diff --git a/gcd/Euclid-substraction/65052514-c3a8-42d4-a767-eb373d1a1021.cpp b/gcd/Euclid-substraction/65052514-c3a8-42d4-a767-eb373d1a1021.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb004a6b8e7898314ee2d8aea2e932e4f523b4e9 --- /dev/null +++ b/gcd/Euclid-substraction/65052514-c3a8-42d4-a767-eb373d1a1021.cpp @@ -0,0 +1,51 @@ +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int cmmdc(int n,int n2){ + + while(n!=n2){ + + if(n>n2){ + + n=n-n2; + + }else{ + + n2=n2-n; + + } + + } + + return n; + +} +int main(){ + + int n,n2; + + fin>>n>>n2; + + int sol=cmmdc(n,n2); + + if(sol==1){ + + sol=0; + + } + + fout< +#include + + + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + if (a < b) + + swap(a, b); + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + + + } + + return a; + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + int a, b; + + f >> a >> b; + + int cmmdcNR = cmmdc(a, b); + + + + + + + + ofstream g("cmmdc.out"); + + if (cmmdcNR == 1) + + g << 0; + + else + + g << cmmdcNR; + +} diff --git a/gcd/Euclid-substraction/68f0f70f-647b-42d2-90f5-ce936c123bd3.cpp b/gcd/Euclid-substraction/68f0f70f-647b-42d2-90f5-ce936c123bd3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a10575e545b38c8df0d619bc2abc35c9775be85 --- /dev/null +++ b/gcd/Euclid-substraction/68f0f70f-647b-42d2-90f5-ce936c123bd3.cpp @@ -0,0 +1,43 @@ +#include +#include + + +std::ifstream f("cmmdc.in"); + +std::ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while (a != b && a>0 && b>0) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + g << 0; + + else + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/6e2d2c9d-4a34-4bd5-b1d0-9b1c06029870.cpp b/gcd/Euclid-substraction/6e2d2c9d-4a34-4bd5-b1d0-9b1c06029870.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc485aece85551b25def2ad3a30e756d33eb1c83 --- /dev/null +++ b/gcd/Euclid-substraction/6e2d2c9d-4a34-4bd5-b1d0-9b1c06029870.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main() + +{ + +int a,b; + +f>>a>>b; + +while(a!=b){ + + if(a>b)a=a-b; + + else b=b-a; + +} + +if (a==1)g<<0; + +else g< +#include +using namespace std; + + + + unsigned int a, b; + +ifstream citire("cmmdc.in"); + +ofstream afisare("cmmdc.out"); + +int main() + +{ + + while (!citire.eof()) + + citire >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + { + + a = 0; + + } + + + + afisare << a; + + afisare.close(); + + system("pause"); + + return 0; + +} diff --git a/gcd/Euclid-substraction/7119162f-7da4-4c9c-bc79-22abcd4fa258.cpp b/gcd/Euclid-substraction/7119162f-7da4-4c9c-bc79-22abcd4fa258.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8d9aeb601dab3f17f73b03770ccc3f38ca9acd --- /dev/null +++ b/gcd/Euclid-substraction/7119162f-7da4-4c9c-bc79-22abcd4fa258.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + fin >> a; + + fin >> b; + + + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else { + + b = b - a; + + } + + } + + if (a == 1 || b == 1) { + + fout << 0; + + } + + else { + + fout << a; + + } + +} diff --git a/gcd/Euclid-substraction/753832c3-048c-440b-90a8-d788e14429cd.cpp b/gcd/Euclid-substraction/753832c3-048c-440b-90a8-d788e14429cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dbc8f8a19f11a251f3141e4dee9d8a0837e4620 --- /dev/null +++ b/gcd/Euclid-substraction/753832c3-048c-440b-90a8-d788e14429cd.cpp @@ -0,0 +1,61 @@ +#include +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + + + +int main() { + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + a = 0; + + out << a; + + return 0; + + + +} diff --git a/gcd/Euclid-substraction/77647361-b597-48ce-a879-7071e43d5995.cpp b/gcd/Euclid-substraction/77647361-b597-48ce-a879-7071e43d5995.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47f33a7e0ad0565efa174972cdb9de52d5359f66 --- /dev/null +++ b/gcd/Euclid-substraction/77647361-b597-48ce-a879-7071e43d5995.cpp @@ -0,0 +1,40 @@ +#include + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,cmmdc; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a== 1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a,b; + + fin >> a >> b; + + while (a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + if (a==1) + + fout << 0; + + else fout << a; + +} diff --git a/gcd/Euclid-substraction/7d703e3b-08a0-42da-bf21-1ce8f9b71bd8.cpp b/gcd/Euclid-substraction/7d703e3b-08a0-42da-bf21-1ce8f9b71bd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55b02e5fee771ab82177f7df79bc1dd6028cfc77 --- /dev/null +++ b/gcd/Euclid-substraction/7d703e3b-08a0-42da-bf21-1ce8f9b71bd8.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + +ifstream f ("cmmdc.in"); + +ofstream g ("cmmdc.out"); + + + +unsigned long int cmmdc (unsigned long int a , unsigned long int b) + +{ + + while(a!=b) + + if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + return 0; + + return a; + +} + + + +unsigned int a,b ; + +int main() + +{ + + f>>a>>b; + + g< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a, b; + +int cmmdc (int a, int b) + +{ + + if (a==b) + + if (a==1) + + return 0; + + else + + return a; + + else + + if (a>b) + + return cmmdc(a-b,b); + + else + + return cmmdc(a,b-a); + +} + +int main() + +{ + + f >> a >> b; + + g << cmmdc(a,b); + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/8513a890-e077-40fc-93e3-59fa9e787629.cpp b/gcd/Euclid-substraction/8513a890-e077-40fc-93e3-59fa9e787629.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/8513a890-e077-40fc-93e3-59fa9e787629.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/871a4bd3-4d4b-4f5d-8536-df0abed0ca2d.cpp b/gcd/Euclid-substraction/871a4bd3-4d4b-4f5d-8536-df0abed0ca2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc045f20b8c6fc43e6564ca10cd8e533a5d6a418 --- /dev/null +++ b/gcd/Euclid-substraction/871a4bd3-4d4b-4f5d-8536-df0abed0ca2d.cpp @@ -0,0 +1,63 @@ +#include +#include + + + + +using namespace std; + + + +int main() + +{ + + int a,b,cmmdc; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if(a==1){ + + cmmdc=0; + + g< +#include +using namespace std; + +int cmmd(int a, int b) { + while (a != b) { + if (a > b) { + a -= b; + } else { + b -= a; + } + } + if (a == 1) { + return 0; + } + return a; +} +int main() { + ifstream f("cmmdc.in"); + ofstream g("cmmdc.out"); + int a, b; + f >> a, f >> b; + g << cmmd(a, b); +} diff --git a/gcd/Euclid-substraction/8d311bd7-d898-411c-8def-ac5962e0ab6e.cpp b/gcd/Euclid-substraction/8d311bd7-d898-411c-8def-ac5962e0ab6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/8d311bd7-d898-411c-8def-ac5962e0ab6e.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/8d83e027-d18e-415d-9db0-29c78dd68ec2.cpp b/gcd/Euclid-substraction/8d83e027-d18e-415d-9db0-29c78dd68ec2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82da5466beaef626252955778d18b6b92c06e594 --- /dev/null +++ b/gcd/Euclid-substraction/8d83e027-d18e-415d-9db0-29c78dd68ec2.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + if (b > a) { + + b = b - a; + + } + + } + + if (a == 1) { + + fout << 0; + + } else { + + fout << a; + + } + + return 0; + +} diff --git a/gcd/Euclid-substraction/8e0914e6-8529-4241-90da-23886df382d7.cpp b/gcd/Euclid-substraction/8e0914e6-8529-4241-90da-23886df382d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69b35185c9783409dc77d6e11bf23a13fab82351 --- /dev/null +++ b/gcd/Euclid-substraction/8e0914e6-8529-4241-90da-23886df382d7.cpp @@ -0,0 +1,43 @@ +#include +#include + + +using namespace std; + +int main() { + + fstream citirein("cmmdc.in",ios::in); + + ofstream output("cmmdc.out",ios::out); + + int a,b; + + citirein>>a>>b; + + citirein.close(); + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + a=0; + + output< +# include + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int main() +{ + int a,b; + f>>a>>b; + + while(a!=b) + { + if(b1) + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, rest; + + fin>>a>>b; + + while (a!=b){ + + if (a>b) a=a-b; + + else b=b-a; + + }if (a==1) fout << "0"; + + else fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/917db930-6705-42ab-b09f-5973154225b9.cpp b/gcd/Euclid-substraction/917db930-6705-42ab-b09f-5973154225b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..839323913cee06369d2af3ab93d3633db2529856 --- /dev/null +++ b/gcd/Euclid-substraction/917db930-6705-42ab-b09f-5973154225b9.cpp @@ -0,0 +1,47 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int a,b; + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if (a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + fout<<"0"; + + else + + fout< +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/91bc7043-0829-4e23-aece-0a6f43ccaa46.cpp b/gcd/Euclid-substraction/91bc7043-0829-4e23-aece-0a6f43ccaa46.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58e4a2252b442d542536e66494a4e0bd05bcc384 --- /dev/null +++ b/gcd/Euclid-substraction/91bc7043-0829-4e23-aece-0a6f43ccaa46.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +int main() + +{ + + int a,b; + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + in>>a>>b; + + while(a!=b) { + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< +#include + + +using namespace std; + + + +int main() + +{ + + ifstream in; + + ofstream out; + + int a, b; + + + + in.open("cmmdc.in"); + + out.open("cmmdc.out"); + + + + in >> a >> b; + + while (a != b){ + + if (a > b) + + a -= b; + + if (b > a) + + b -= a; + + } + + + + if (a == 1) + + out << 0; + + else + + out << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/9336f27a-6198-4876-b2c9-1ac2f22fe894.cpp b/gcd/Euclid-substraction/9336f27a-6198-4876-b2c9-1ac2f22fe894.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51573b4bf108b9cd40cc4b7da70b358acd9acf47 --- /dev/null +++ b/gcd/Euclid-substraction/9336f27a-6198-4876-b2c9-1ac2f22fe894.cpp @@ -0,0 +1,36 @@ +#include + + +using namespace std; + + + +int main() + +{int a,b; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while(a!=b) + + {if(a>b) a=a-b; + + else b=b-a;} + + if(a==b && a!=1) g< +#include + + + + +using namespace std; + + + +int cmmdc(int a, int b) + +{ + + if (a < b) + + swap(a, b); + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + + + } + + return a; + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + int a, b; + + f >> a >> b; + + int cmmdcNR = cmmdc(a, b); + + + + + + + + ofstream g("cmmdc.out"); + + if (cmmdcNR == 1) + + g << 0; + + else + + g << cmmdcNR; + +} diff --git a/gcd/Euclid-substraction/9642354c-58db-474a-9e88-f00d42660193.cpp b/gcd/Euclid-substraction/9642354c-58db-474a-9e88-f00d42660193.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d70a2537564aab604602a4fe707b32a0d71fd14 --- /dev/null +++ b/gcd/Euclid-substraction/9642354c-58db-474a-9e88-f00d42660193.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + + + +ifstream intrare("cmmdc.in"); + +ofstream iesire("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + intrare >> a; + + intrare >> b; + + while(a != b) + + if(a > b) + + a = a - b; + + else + + b = b -a; + + int cmmdc = a; + + if(cmmdc == 1) + + iesire << 0; + + else + + iesire << cmmdc; + + + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/97e8bb36-dbd4-4f69-92ac-403d30bdfbfb.cpp b/gcd/Euclid-substraction/97e8bb36-dbd4-4f69-92ac-403d30bdfbfb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1a92b2a89090a865b2d836adf840093337591ce --- /dev/null +++ b/gcd/Euclid-substraction/97e8bb36-dbd4-4f69-92ac-403d30bdfbfb.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main () { + + int a, b; + + f >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } else { + + b = b - a; + + } + + } + + if (a == 1) a = 0; + + g << a; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/97ef46a1-bb5f-4870-9873-6d7e370e1512.cpp b/gcd/Euclid-substraction/97ef46a1-bb5f-4870-9873-6d7e370e1512.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519a979ab60071f8c220314a17ec283514279e3e --- /dev/null +++ b/gcd/Euclid-substraction/97ef46a1-bb5f-4870-9873-6d7e370e1512.cpp @@ -0,0 +1,30 @@ +#include +# include + +using namespace std; + +ifstream f("cmmdc.in"); +ofstream g("cmmdc.out"); + +int main() +{ + int a,b; + f>>a>>b; + + while(a!=b) + { + if(b1) + g< +#include +using namespace std; + +int main() +{ + short a,b; + ifstream in("cmmdc.in"); + in >>a >>b; + in.close(); + while(a>0 and b>0) + if(a>b) + a-=b; + else + b-=a; + ofstream out("cmmdc.out"); + if(a<=0) + if(b<=1) + out <<0; + else + out < +#include +using namespace std; + +int a,b; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ in>>a>>b; + + while(a!=b) if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + + + + + +int main() + +{ + + long long a,b; + + + + f>>a>>b; + + + + while (a!=b){ + + if (a>b){ + + a=a-b; + + }else b=b-a; + + } + + if (a==1 && b==1) + + g<<"0"; + + else { + + if (a>b) + + g< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b; + + fin>>a>>b; + + while(a>b || b>a) + + { + + if(a>b)a=a-b; + + if(a +#include +using namespace std; + + + +int main() { + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + a = 0; + + out << a; + + return 0; + + + +} diff --git a/gcd/Euclid-substraction/a52fbd66-5399-4437-99f3-eb5d33788e07.cpp b/gcd/Euclid-substraction/a52fbd66-5399-4437-99f3-eb5d33788e07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd4ddfefa094186548ebf98d0e88289c6c85bd53 --- /dev/null +++ b/gcd/Euclid-substraction/a52fbd66-5399-4437-99f3-eb5d33788e07.cpp @@ -0,0 +1,26 @@ +#include +std::ifstream in ("cmmdc.in"); std::ofstream out ("cmmdc.out"); + + + +int main (){ + + std::ios::sync_with_stdio(false); + + int a,b; + + in>>a>>b; + + while(a!=b){ + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a > 1) + + fout << a; + + else + + fout << 0; + +} diff --git a/gcd/Euclid-substraction/a984d1d4-b3ea-407b-bd05-790665ceaed8.cpp b/gcd/Euclid-substraction/a984d1d4-b3ea-407b-bd05-790665ceaed8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6814fc10901ed1f686fdd67eb01de092b1417cf1 --- /dev/null +++ b/gcd/Euclid-substraction/a984d1d4-b3ea-407b-bd05-790665ceaed8.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int cmmdc(int a, int b) + +{ + + + + while (a != b ) + + { + + if (a > b) a = a - b; + + else b = b - a; + + + + } + + return a; + +} + +int main() + +{ + + int a, b; + + f >> a >> b; + + if (cmmdc(a, b) == 1) g << 0; + + else g << cmmdc(a, b); + + + + f.close(); + + g.close(); + +} diff --git a/gcd/Euclid-substraction/ab83865d-f790-4a72-903b-94dd76ba1c4f.cpp b/gcd/Euclid-substraction/ab83865d-f790-4a72-903b-94dd76ba1c4f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e03ec642b82539d16dc7f47f18ae8a730c753f78 --- /dev/null +++ b/gcd/Euclid-substraction/ab83865d-f790-4a72-903b-94dd76ba1c4f.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + + + +int main () + +{ + + ifstream f("cmmdc.in"); + + long a,b; + + f>>a>>b; + + ofstream o("cmmdc.out"); + + while(a!=b) + + if (a>b) + + a=a-b; + + else + + b=b-a; + + + + if(a==1) + + a=0; + + o< + + + + +using namespace std; + + + +int a, b; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + if(a>b) a=a-b; + + else b=b-a; + + if(a!=1) fout< +#include +int main() +{ + int a, b; + std::cin>>a>>b; + std:: ofstream myfile; + myfile.open ("cmmdc.in"); + myfile<< a << "\n" << b; + myfile.close(); + while (a != b) + { + if(a>b) + a -= b; + else if(a + + +using namespace std; + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int cmmdc(int a, int b) + +{ + + while(a!=b) + + { + + if (a>b) + + a-=b; + + else + + b-=a; + + } + + if (a==1 && b==1) + + return 0; + + return a; + +} + +int main() + +{ + + int a, b; + + cin >> a >> b; + + cout << cmmdc(a,b); + + return 0; + +} diff --git a/gcd/Euclid-substraction/c239b7e9-a593-4637-9cbe-b39cbdca15c2.cpp b/gcd/Euclid-substraction/c239b7e9-a593-4637-9cbe-b39cbdca15c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8d9aeb601dab3f17f73b03770ccc3f38ca9acd --- /dev/null +++ b/gcd/Euclid-substraction/c239b7e9-a593-4637-9cbe-b39cbdca15c2.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + fin >> a; + + fin >> b; + + + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else { + + b = b - a; + + } + + } + + if (a == 1 || b == 1) { + + fout << 0; + + } + + else { + + fout << a; + + } + +} diff --git a/gcd/Euclid-substraction/c23e6347-3b0c-418d-aa78-37e3e83b26db.cpp b/gcd/Euclid-substraction/c23e6347-3b0c-418d-aa78-37e3e83b26db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a3e0f3b664fab0326f9b080f02cd80ad6d118da --- /dev/null +++ b/gcd/Euclid-substraction/c23e6347-3b0c-418d-aa78-37e3e83b26db.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int main(void) + +{ + + int a,b; + + f>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1 || b==1) + + g<<0; + + else + + g< +#include + + +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + +{ + + int a, b, rest; + + fin>>a>>b; + + while (a!=b){ + + if (a>b) a=a-b; + + else b=b-a; + + }if (a==1) fout << "0"; + + else fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/c5dde0ee-0c16-413a-9367-a62d933efa5f.cpp b/gcd/Euclid-substraction/c5dde0ee-0c16-413a-9367-a62d933efa5f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4fc45015409b5d6d8c11cd8afea31e1f10b4a13 --- /dev/null +++ b/gcd/Euclid-substraction/c5dde0ee-0c16-413a-9367-a62d933efa5f.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int prime, a,b; + + + +int main() + +{ + + in>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a-=b; + + else + + b-=a; + + } + + if(a==1) + + out<<0; + + else + + out< +using namespace std; + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + + f>>a>>b; + + if(a==0 && b!=0) + + { + + g<b) + + { + + a=a-b; + + } + + else + + { + + b=b-a; + + } + + } + + if(a==1) + + { + + g<<0; + + } + + else + + { + + g< +#include + + +using namespace std; + + + +ifstream fin ("cmmdc.in"); + +ofstream fout ("cmmdc.out"); + + + +int main() { + + int a, b; + + fin >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + if (b > a) { + + b = b - a; + + } + + } + + if (a == 1) { + + fout << 0; + + } else { + + fout << a; + + } + + return 0; + +} diff --git a/gcd/Euclid-substraction/cadf67f9-86c3-4921-ad16-8579e771e192.cpp b/gcd/Euclid-substraction/cadf67f9-86c3-4921-ad16-8579e771e192.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32f6398b4a7a5d629bfa07fdef137827e215a0a7 --- /dev/null +++ b/gcd/Euclid-substraction/cadf67f9-86c3-4921-ad16-8579e771e192.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + + { + + int a,b; + + fin >> a >> b; + + + + while (a!=b) + + { + + if (a>b) + + a-=b; + + + + else + + b-=a; + + } + + + + if (a<=1 || b<=1) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/cfa15cc2-a34e-4976-abcf-3afb8e1fe0e9.cpp b/gcd/Euclid-substraction/cfa15cc2-a34e-4976-abcf-3afb8e1fe0e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9378c645b70bab0228ef6f23e493f8b0370011d4 --- /dev/null +++ b/gcd/Euclid-substraction/cfa15cc2-a34e-4976-abcf-3afb8e1fe0e9.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else + + fout< +#include + + +using namespace std; + + + +int main() { + + + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + + + int a, b; + + in >> a >> b; + + while (a != b) { + + if (a > b) { + + a = a - b; + + } + + else b = b - a; + + } + + + + if (a == 1) { + + out << 0; + + } + + else out << a; + +} diff --git a/gcd/Euclid-substraction/d1402a23-b101-49d8-b6de-e2d9a8d17efa.cpp b/gcd/Euclid-substraction/d1402a23-b101-49d8-b6de-e2d9a8d17efa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1663c5a790c368c3a05b70455d518885279919f --- /dev/null +++ b/gcd/Euclid-substraction/d1402a23-b101-49d8-b6de-e2d9a8d17efa.cpp @@ -0,0 +1,23 @@ +#include +#include +using namespace std; +ifstream in("cmmdc.in"); +ofstream out("cmmdc.out"); + +int main() +{ int a,b,aux=0; +in>>a>>b; + while(b!=a){ + if(a>b) + a-=b; + if(b>a) + b-=a; + } + + if(a==1){ + out<<"0"; + }else{ + out< + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int s,a,b; + +int main() + +{ + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + if(a==1) + + { + + fout<<0; + + } + + else + + fout< +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +int main() + +{f>>a>>b; + +while(a!=b) + +{ + + if(a>b) + + a=a-b; + + else + + b=b-a; + +} + +if(a!=1) + + g < +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/d55049f8-b1b1-46c6-9606-e533835f69ca.cpp b/gcd/Euclid-substraction/d55049f8-b1b1-46c6-9606-e533835f69ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e40154f383061eab663b6d1e3b9bfd85e372aa41 --- /dev/null +++ b/gcd/Euclid-substraction/d55049f8-b1b1-46c6-9606-e533835f69ca.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b; + +int main() + +{f>>a>>b; + +while(a!=b) + +{ + + if(a>b) + + a=a-b; + + else + + b=b-a; + +} + +if(a!=1) + + g < + + +int main() + +{ + + int a,b; + + FILE* p,*q; + + p = fopen("cmmdc.in", "r"); + + q = fopen("cmmdc.out", "w"); + + fscanf(p,"%d%d", &a,&b); + + + + while (a != b) + + if (a > b) + + a = a - b; + + else + + b = b - a; + + if (a == 1) + + fprintf(q, "%c", '0'); + + else + + fprintf(q, "%d", a); + + fclose(p); + + fclose(q); + + return 0; + +} diff --git a/gcd/Euclid-substraction/da5a2897-374b-4597-a70d-6fbf8b580e95.cpp b/gcd/Euclid-substraction/da5a2897-374b-4597-a70d-6fbf8b580e95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebf6d811c460d729d016b277709c0c48c165f5a4 --- /dev/null +++ b/gcd/Euclid-substraction/da5a2897-374b-4597-a70d-6fbf8b580e95.cpp @@ -0,0 +1,49 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while(a != b) + + { + + if(a>b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + g << "0"; + + else + + g << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/dc0f8069-3397-416e-b925-ff1c4a716f48.cpp b/gcd/Euclid-substraction/dc0f8069-3397-416e-b925-ff1c4a716f48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8039e05f12dad3bf3d2ffc66cad6fcacde2e11 --- /dev/null +++ b/gcd/Euclid-substraction/dc0f8069-3397-416e-b925-ff1c4a716f48.cpp @@ -0,0 +1,40 @@ + #include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< +#include +using namespace std; + + + +int main() + +{ + + int a,b; + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) a-=b; + + else b-=a; + + } + + if (a==1) fout<<0; + + else fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + + + } + + if(b==1) + + b=0; + + fout< + + +using namespace std; + + + +int main() + +{ + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + int a,b; + + fin>>a>>b; + + while(a!=b) + + { + + if(a +using namespace std; + + + +ifstream cin("cmmdc.in"); + +ofstream cout("cmmdc.out"); + + + +int main () { + + int a, b; + + cin >> a >> b; + + while (a != b) { + + if (a > b) a -= b; + + else if (a < b) b -= a; + + } + + if (a == 1) { + + cout << 0; + + return 0; + + } + + cout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/e1a431a4-50a3-453e-882b-35fb7404354e.cpp b/gcd/Euclid-substraction/e1a431a4-50a3-453e-882b-35fb7404354e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f29ea5f92144ae5a6e67724554a2bf895afec394 --- /dev/null +++ b/gcd/Euclid-substraction/e1a431a4-50a3-453e-882b-35fb7404354e.cpp @@ -0,0 +1,24 @@ +#include +#include +using namespace std; + +int cmmd(int a, int b) { + while (a != b) { + if (a > b) { + a -= b; + } else { + b -= a; + } + } + if (a == 1) { + return 0; + } + return a; +} +int main() { + ifstream f("cmmdc.in"); + ofstream g("cmmdc.out"); + int a, b; + f >> a, f >> b; + g << cmmd(a, b); +} diff --git a/gcd/Euclid-substraction/e3eb8a09-5081-4bed-85e7-4e3307830b25.cpp b/gcd/Euclid-substraction/e3eb8a09-5081-4bed-85e7-4e3307830b25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb0049aa9900f0dad53873d68a302e7e28a7d7a5 --- /dev/null +++ b/gcd/Euclid-substraction/e3eb8a09-5081-4bed-85e7-4e3307830b25.cpp @@ -0,0 +1,51 @@ +#include +#include +using namespace std; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + + + +int main() + +{ + + long a; + + long b; + + in >> a; + + in >> b; + + while(a != b) + + { + + if(a > b) + + a = a - b; + + else b = b - a; + + } + + if(a != 1) + + { + + out << a; + + } + + else out << 0; + + out.close(); + + in.close(); + + return 0; + +} diff --git a/gcd/Euclid-substraction/e5967744-af59-4496-8b30-5543d24779a4.cpp b/gcd/Euclid-substraction/e5967744-af59-4496-8b30-5543d24779a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85992ec63c7ba1a5c5b0cdbe22cade7245265989 --- /dev/null +++ b/gcd/Euclid-substraction/e5967744-af59-4496-8b30-5543d24779a4.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b; + + fin>>a>>b; + + while(a>b || b>a) + + { + + if(a>b)a=a-b; + + if(a +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a, b, r; + + fin >> a >> b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + if (b > a) + + b = b - a; + + } + + if (a == 1) + + fout << 0; + + else fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/e8e44ca6-ef4d-42b0-b946-aa1f7f90ff32.cpp b/gcd/Euclid-substraction/e8e44ca6-ef4d-42b0-b946-aa1f7f90ff32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8039e05f12dad3bf3d2ffc66cad6fcacde2e11 --- /dev/null +++ b/gcd/Euclid-substraction/e8e44ca6-ef4d-42b0-b946-aa1f7f90ff32.cpp @@ -0,0 +1,40 @@ + #include + + +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + +unsigned int a,b; + +fin>>a>>b; + +while(a!=b) + +{if(a>b) + +a=a-b; + +else + +b=b-a; + +} + +if(a==1) + +fout<<"0"; + + else + +fout< +#include +using namespace std; + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + +int main() + +{ + + int a,b,r; + + fin>>a>>b; + + while(b) + + { + + r=a%b; + + a=b; + + b=r; + + } + + if(a==1) + + fout<<0; + + else + + fout< +#include +using namespace std; + + + +int cmmdc ( int a , int b) + +{ + + while(a!=b) + + { + + if(a > b) + + a-=b; + + else + + b-=a; + + } + + if(a != 1) + + return a; + + else + + return 0; + +} + + + +int main() + +{ + + fstream cit ("cmmdc.in"); + + ofstream afis ("cmmdc.out"); + + + + int a, b; + + + + cit >> a >> b; + + + + afis< +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< +#include +using namespace std; + +int a,b; + +ifstream in("cmmdc.in"); + +ofstream out("cmmdc.out"); + +int main() + +{ in>>a>>b; + + while(a!=b) if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + out<<0; + + else + + out< +#include +using namespace std; + + + +ifstream fin("cmmdc.in"); + +ofstream fout("cmmdc.out"); + + + +int main() + + { + + int a,b; + + fin >> a >> b; + + + + while (a!=b) + + { + + if (a>b) + + a-=b; + + + + else + + b-=a; + + } + + + + if (a<=1 || b<=1) + + fout << 0; + + else + + fout << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/f133872c-ffc8-4eb7-b30b-a3fc09177e0a.cpp b/gcd/Euclid-substraction/f133872c-ffc8-4eb7-b30b-a3fc09177e0a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d373c3d3ed8d56cb15f7c5c0c95c87ae13e6403f --- /dev/null +++ b/gcd/Euclid-substraction/f133872c-ffc8-4eb7-b30b-a3fc09177e0a.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + + + +int main() + +{ + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a,b,A,B; + +f>>a>>b; + +A=a; + +B=b; + +while(A!=B){ + +if(A>B) A=A-B; + +else B=B-A; + +} + +if(A>1) g< + + +using namespace std; + + + +int main() + +{ + + int a, b; + + + + ifstream fin("cmmdc.in"); + + ofstream fout("cmmdc.out"); + + + + fin >> a >> b; + + + + while (a != b) + + { + + if (a > b) + + { + + a -= b; + + } + + else + + { + + b -= a; + + } + + } + + if (a == 1) + + { + + fout << 0; + + } + + else + + { + + fout << a; + + } + + + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/f56c476d-fc53-48a3-8652-23ad79accfab.cpp b/gcd/Euclid-substraction/f56c476d-fc53-48a3-8652-23ad79accfab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebf6d811c460d729d016b277709c0c48c165f5a4 --- /dev/null +++ b/gcd/Euclid-substraction/f56c476d-fc53-48a3-8652-23ad79accfab.cpp @@ -0,0 +1,49 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + + + +int main() + +{ + + int a, b; + + f >> a >> b; + + while(a != b) + + { + + if(a>b) + + a = a - b; + + else + + b = b - a; + + } + + if (a == 1) + + g << "0"; + + else + + g << a; + + return 0; + +} diff --git a/gcd/Euclid-substraction/f63acc8f-c256-4965-85a2-fa4cc7bf5e1e.cpp b/gcd/Euclid-substraction/f63acc8f-c256-4965-85a2-fa4cc7bf5e1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f53da0e919dcc0c1797ed854f6f2bda448be90 --- /dev/null +++ b/gcd/Euclid-substraction/f63acc8f-c256-4965-85a2-fa4cc7bf5e1e.cpp @@ -0,0 +1,41 @@ +#include +#include + + +std::ifstream fin("cmmdc.in"); + +std::ofstream fout("cmmdc.out"); + +int main() { + + int a, b; + + fin >> a >> b; + + + + while (a != b) { + + if (a > b) + + a -= b; + + else + + b -= a; + + } + + if (a == 1 || b == 1) + + fout << 0; + + else + + fout << a; + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/f6b8be98-4757-49db-937b-e9f2c32cfe76.cpp b/gcd/Euclid-substraction/f6b8be98-4757-49db-937b-e9f2c32cfe76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dbc8f8a19f11a251f3141e4dee9d8a0837e4620 --- /dev/null +++ b/gcd/Euclid-substraction/f6b8be98-4757-49db-937b-e9f2c32cfe76.cpp @@ -0,0 +1,61 @@ +#include +#include + + +using namespace std; + +int a,b,cmmdc; + + + +int CMMDC(int a, int b){ + + + + while(a!=b){ + + if(a>b){ + + a=a-b; + + } + + if(b>a){ + + b=b-a; + + } + + } + + if(a == 1 ){ + + return 0; + + }else{ + + return a; + + } + +} + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a>>b; + + g< + + +using namespace std; + + + +int main() + +{ + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + int a,b; + +f>>a>>b; + +while(a!=b) + +{ if(a>b) + +{a=a-b; + +} + +else b=b-a; + +} + +if(a!=1) + +g< +#include + + +using namespace std; + + + +int main() + +{ double a,b; + + + + std::ifstream file1; + + std::ofstream file2; + + file1.open("cmmdc.in"); + + file1>>a; + + file1>>b; + + + + while(a!=b) + + { + + if(a>b) + + a=a-b; + + else + + b=b-a; + + } + + + + + + + + file2.open("cmmdc.out"); + + if (a==1) + + file2<<0; + + else + + file2< +#include + + + + +using namespace std; + + + +int main() + +{ + + int a,b,cmmdc; + + ifstream f("cmmdc.in"); + + ofstream g("cmmdc.out"); + + f>>a; + + f>>b; + + while (a != b) + + { + + if (a > b) + + a = a - b; + + else + + b = b - a; + + } + + if(a==1){ + + cmmdc=0; + + g< + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +long long a,b; + +int main() + +{ + + f>>a>>b; + + while (a!=b) + + { + + if (a +#include + + +using namespace std; + +ifstream f ("cmmdc.in"); + +ofstream g ("cmmdc.out"); + + + +unsigned long int cmmdc (unsigned long int a , unsigned long int b) + +{ + + while(a!=b) + + if(a>b) + + a=a-b; + + else + + b=b-a; + + if(a==1) + + return 0; + + return a; + +} + + + +unsigned int a,b ; + +int main() + +{ + + f>>a>>b; + + g< +#include + + +using namespace std; + +ifstream f("cmmdc.in"); + +ofstream g("cmmdc.out"); + +int a, b; + +int cmmdc (int a, int b) + +{ + + if (a==b) + + if (a==1) + + return 0; + + else + + return a; + + else + + if (a>b) + + return cmmdc(a-b,b); + + else + + return cmmdc(a,b-a); + +} + +int main() + +{ + + f >> a >> b; + + g << cmmdc(a,b); + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/gcd/Euclid-substraction/ffd679a9-f814-49e9-8373-73dc36d887f2.cpp b/gcd/Euclid-substraction/ffd679a9-f814-49e9-8373-73dc36d887f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58e4a2252b442d542536e66494a4e0bd05bcc384 --- /dev/null +++ b/gcd/Euclid-substraction/ffd679a9-f814-49e9-8373-73dc36d887f2.cpp @@ -0,0 +1,31 @@ +#include +#include +using namespace std; + +int main() + +{ + + int a,b; + + ifstream in("cmmdc.in"); + + ofstream out("cmmdc.out"); + + in>>a>>b; + + while(a!=b) { + + if(a>b) a=a-b; + + else b=b-a; + + } + + if(a==1) out<<0; + + else out< +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +long long n,v[100010],a[100010],k=1,aux[100010]; + +long long query(long long x){ + + int r=0; + + while(x){ + + r+=aux[x]; + + x-=(x&-x); + + } + + return r; + +} + +void update(long long x){ + + while(x<=k){ + + aux[x]++; + + x+=(x&-x); + + } + +} + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1,a+n+1); + + for(int i=2;i<=n;i++) + + if(a[i]!=a[k]) + + a[++k]=a[i]; + + + + for(int i=1;i<=n;i++){ + + int st=1; + + int dr=k; + + while(st<=dr){ + + int mid=(st+dr)/2; + + if(v[i]==a[mid]){ + + v[i]=mid; + + break; + + } + + else + + if(v[i]>a[mid]) + + st=mid+1; + + else + + dr=mid-1; + + + + } + + + + } + + int suma=0; + + + + + + for(int i=1;i<=n;i++){ + + suma+=(query(k)-query(v[i]));suma%=9917; + + update(v[i]); + + } + + fout< +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +using PII = pair; + +using VP = vector; + +const int mod = 9917; + +int aib[110011], s, n, x; + + + +inline void Update(int poz) + +{ + + for (int i = poz; i <= n; i += i & -i) + + ++aib[i]; + +} + + + +inline int Query(int poz) + +{ + + int sum = 0; + + for (int i = poz; i > 0; i -= i & -i) + + sum += aib[i]; + + return sum; + +} + + + +int main() + +{ + + fin >> n; + + VP v(n + 1); + + for (int i = 1; i <= n; ++i) + + { + + fin >> x; + + v[i] = {x, i}; + + } + + sort(v.begin() + 1, v.end(), greater()); + + for (int i = 1; i <= n; ++i) + + { + + s = (s + Query(v[i].second)) % mod; + + Update(v[i].second); + + } + + fout << s; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/054286a2-3183-478e-87da-6b9ac706312e.cpp b/invCount/aib/054286a2-3183-478e-87da-6b9ac706312e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73db369a0007edf5a3fd6ad132ec80dcb75660de --- /dev/null +++ b/invCount/aib/054286a2-3183-478e-87da-6b9ac706312e.cpp @@ -0,0 +1,65 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + + + +static const int MOD = 9917; + +static const int NMAX = 1e5+5; + +int aib[NMAX]; +int ind[NMAX]; +int v[NMAX]; +int n; + +int query(int position) +{ + int sum = 0; + for(int i = position; i>= 1; i-=(i&(-i))) + sum+=aib[i]; + return sum; +} + +void update(int position) +{ + for(int i = position; i<=n; i+=(i&(-i))) + aib[i]++; +} + +bool cmp(const int &a, const int &b) +{ + return v[a] < v[b]; +} + +int main() +{ + fin >> n; + + for(int i =1; i<=n; ++i) + { + fin >> v[i]; + ind[i] = i; + } + stable_sort(ind+1,ind+n+1,cmp); + + int sol = 0; + for(int i = 1; i<=n; ++i) + { + int pos = ind[i]; + + sol = (sol + pos - query(pos) -1) % MOD; + update(pos); + } + + fout << sol; + + + return 0; +} diff --git a/invCount/aib/0a835620-ee2f-4786-8f2e-ad8276a039e4.cpp b/invCount/aib/0a835620-ee2f-4786-8f2e-ad8276a039e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec6a05dceb13d0a330e50baeb715b8f39e1a4d62 --- /dev/null +++ b/invCount/aib/0a835620-ee2f-4786-8f2e-ad8276a039e4.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef long long ll; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int MOD = 9917; + +const int NMAX = 100005; + + + +int aib[NMAX]; + + + +void update(int pos, int n) { + + for(; pos <= n; pos += pos & (-pos)) + + aib[pos] ++; + +} + + + +int query(int pos) { + + int ans = 0; + + for(; pos; pos -= pos & (-pos)) + + ans += aib[pos]; + + return ans; + +} + + + +int main() { + + int n; + + in >> n; + + vector > v(n + 1, {0, 0}); + + for(int i = 1; i <= n; i ++) { + + in >> v[i].first; + + v[i].second = i; + + } + + sort(v.begin() + 1, v.end()); + + + + vector tonorm(n + 1, 0); + + for(int i = 1; i <= n; i ++) + + tonorm[v[i].second] = i; + + + + int ans = 0; + + for(int i = 1; i <= n; i ++) { + + ans = (ans + query(n) - query(tonorm[i])) % MOD; + + update(tonorm[i], n); + + } + + out << ans; + + return 0; + +} diff --git a/invCount/aib/0a8ce047-6b35-4a90-a805-7913346d14e4.cpp b/invCount/aib/0a8ce047-6b35-4a90-a805-7913346d14e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..131b32d7d413e74e27adf213b7729c1241578d15 --- /dev/null +++ b/invCount/aib/0a8ce047-6b35-4a90-a805-7913346d14e4.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +struct xd + +{ + + long long x,poz; + +}a[100009]; + +long long n,i,v[100009],aib[100009],s; + +long long compare (xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x=1;j-=j^(j&(j-1))) + + s+=aib[j]; + + return s; + +} + +int main() + +{ + + fin.sync_with_stdio(false); + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i],a[i].x=v[i],a[i].poz=i; + + sort(a+1,a+n+1,compare); + + for(i=1;i<=n;i++) + + v[a[i].poz]=i; + + s=n*(n-1)/2; + + for(i=1;i<=n;i++) + + { + + s-=1LL*query(v[i]); + + update(v[i],1); + + } + + fout< + + +using namespace std; + +int s[100001],n,z[100001]; + +pair v[100001]; + + + +int zeros(int i) + +{ + + return (i^(i-1))&i; + +} + +void add(int p,int x) + +{ + + for(int i=p; i<=n; i+=zeros(i)) + + s[i]+=x; + +} + +long long sum(int p) + +{ + + long long rez=0LL; + + for(int i=p; i>0; i-=zeros(i)) + + rez+=s[i]; + + return rez; + +} + +int main() + +{ + + int i,x; + + long long rez=0LL; + + freopen("inv.in","r",stdin); + + freopen("inv.out","w",stdout); + + scanf("%d",&n); + + for(i=1; i<=n; i++) + + { + + scanf("%d",&z[i]); + + v[i].first=z[i]; + + v[i].second=i; + + } + + sort(v+1,v+1+n); + + for(i=1; i<=n; i++) + + z[v[i].second]=i; + + for(i=1; i<=n; i++) + + { + + x=z[i]; + + rez+=i-1-sum(x-1); + + add(x,1); + + } + + printf("%d",rez%9917); + + return 0; + + + +} diff --git a/invCount/aib/0d9b51c9-a036-47e8-bd10-a1874e545c40.cpp b/invCount/aib/0d9b51c9-a036-47e8-bd10-a1874e545c40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6557b5239bfaf438d0cb292f1b9b9505b30c4c27 --- /dev/null +++ b/invCount/aib/0d9b51c9-a036-47e8-bd10-a1874e545c40.cpp @@ -0,0 +1,156 @@ +#include + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +struct sir + +{ + + long long val,poz; + +}v[100100]; + + + + + +long long compare1 (sir A,sir B) + +{ + + if(A.val==B.val)return(A.poz=1;i-=(i^(i-1)&i)) + + s+=AIB[i]; + + return s; + +} + + + + + +long long i,NR,k; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare1); + + for(i=1;i<=n;i++) + + { + + v[i].val=i; + + } + + sort(v+1,v+n+1,compare2); + + + + + + + + for(k=1;k<=n;k++) + + { + + NR+=suma(v[k].val); + + adaug(v[k].val,1); + + } + + + + + + g<<(n*(n-1)/2-NR)%9917; + + + + + + + + return 0; + +} diff --git a/invCount/aib/0db5d432-1adb-4097-bdaa-495cbe9aa5ce.cpp b/invCount/aib/0db5d432-1adb-4097-bdaa-495cbe9aa5ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b130925c6497d5e8d316c23c10055b80a13ed3 --- /dev/null +++ b/invCount/aib/0db5d432-1adb-4097-bdaa-495cbe9aa5ce.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int MOD = 9917; + + + +int tree[100001]; + +pair v[100001]; + +int n; + + + +void update(int poz) + +{ + + for(int i = poz; i <= n; i += i&-i) + + tree[i]++; + +} + + + +int64_t query(int poz) + +{ + + int s = 0; + + + + for(int i = poz; i; i -= i&-i) + + s += tree[i]%MOD; + + + + return s%MOD; + +} + + + +int main() + +{ + + in >> n; + + + + for(int i = 1; i <= n; i++) + + { + + in >> v[i].first; + + v[i].second = i; + + } + + + + sort(v+1, v+n+1); + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << v[i].first << ' ' << v[i].second << '\n'; + + } + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + + + + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + cout << query(2) << ' ' << query(4); + + } + + + + int64_t rez = 0; + + for(int i = n; i; i--) + + { + + if(!1) + + { + + if(query(v[i].second)) + + cout << query(v[i].second) << ' ' << v[i].first << ' ' << v[i].second << '\n'; + + } + + rez += query(v[i].second)%MOD; + + update(v[i].second); + + + + } + + out << rez%MOD; + + + + return 0; + +} diff --git a/invCount/aib/0e915a3e-53cc-4d25-8a66-31e322943411.cpp b/invCount/aib/0e915a3e-53cc-4d25-8a66-31e322943411.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b253e4c162b1b7beff057835dd3cff9baa33608 --- /dev/null +++ b/invCount/aib/0e915a3e-53cc-4d25-8a66-31e322943411.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + + +const int MOD = 9917; +const int INF = INT_MAX; +int t, n, swaps, bit[100005]; + + + +void update(int i){ + + while (i <= n){ + + ++bit[i]; + + i += (-i & i); + + } + +} + + + +int query(int i){ + + int ret = 0; + + while (i > 0){ + + ret += bit[i]; + + i -= (-i & i); + + } + + return ret % MOD; + +} + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + + cin >> n; + + vector > a(n + 1); + + for (int i = 1; i <= n; i++){ + + cin >> a[i].first; + + a[i].second = i; + + } + + sort(1 + (a).begin(),(a).end()); + + for (int i = n; i > 0; i--){ + + update(a[i].second); + + swaps += query(a[i].second - 1); + + swaps %= MOD; + + } + cout << swaps; + return 0; +} diff --git a/invCount/aib/0fadb88d-5146-4a2e-baa7-4c2b7d3fc1d5.cpp b/invCount/aib/0fadb88d-5146-4a2e-baa7-4c2b7d3fc1d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db3cf167c9c83c9208cc3aee4b7dcdee059beee9 --- /dev/null +++ b/invCount/aib/0fadb88d-5146-4a2e-baa7-4c2b7d3fc1d5.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef pair pii; + + + +const int N = 100010; +int n, bit[N], ans; +pii a[N]; + +void update(int idx){ + for (;idx <= n; idx += (idx & -idx)) + bit[idx]++; +} + +int query(int idx){ + int rs = 0; + for(;idx;idx -= (idx & -idx)) + rs += bit[idx]; + return rs; +} + +int main(){ + freopen("inv.in", "r", stdin); + freopen("inv.out", "w", stdout); + scanf("%d", &n); + for (int i=1; i<=n; i++) scanf("%d", &a[i].first), a[i].second = i; + sort(a+1, a+1+n); + for(int i=1; i<=n; i++){ + update(a[i].second); + ans = (ans + query(n) - query(a[i].second))%9917; + } + printf("%d", ans); + return 0; +} diff --git a/invCount/aib/103cf325-2948-4c04-87ed-3ff1d258edfa.cpp b/invCount/aib/103cf325-2948-4c04-87ed-3ff1d258edfa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9bceebb61454c2c48787aab1c051532f185bea9 --- /dev/null +++ b/invCount/aib/103cf325-2948-4c04-87ed-3ff1d258edfa.cpp @@ -0,0 +1,155 @@ +#include +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aint[3*100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p9917) + + aint[nod]-=9917; + +} + + + +void query(int nod, int st, int dr){ + + if(a<=st && dr<=b){ + + nr+=aint[nod]; + + if(nr>=9917) + + nr-=9917; + + return; + + } + + else{ + + int mid=(st+dr)/2; + + if(a<=mid) + + query(2*nod,st,mid); + + if(b>mid) + + query(2*nod+1,mid+1,dr); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + a=v[i]+1; b=k; + + nr=0; + + query(1,1,k); + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + poz=v[i]; + + update(1,1,k); + + } + + fout< +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,k,v[100001],a[100001],ord[100001]; + +long long sol; + + + +inline int cb(int x){ + + int st=1; + + int dr=k; + + int mid; + + + + while(st<=dr){ + + mid=(st+dr)/2; + + + + if(x==ord[mid]) + + return mid; + + + + if(x>n; + + for(i=1;i<=n;i++) + + fin>>a[i],ord[i]=a[i]; + + sort(ord+1,ord+n+1); + + + + ord[0]=-1; + + for(i=1;i<=n;i++) + + if(ord[i]!=ord[k]) + + ord[++k]=ord[i]; + + + + for(i=1;i<=n;i++) + + a[i]=cb(a[i]); + + + + for(i=1;i<=n;i++){ + + sol=(sol+i-1-query(a[i]))%9917; + + upd(a[i]); + + } + + + + fout< +#include +#include +using namespace std; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +int n,i,S[100010],A[100010],sol,dbl; + +pair v[100010]; + +int q(int p){ + + int nr=0; + + for(;p;p-=(p&-p)) + + nr+=A[p]; + + return nr; + +} + +void up(int p){ + + for(;p<=n;p+=(p&-p)) + + A[p]++; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + +} + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++){ + + if(v[i].first==v[i-1].first){ + + S[v[i].second]=S[v[i-1].second]; + + dbl++; + + } + + else + + S[v[i].second]=i-dbl; + + } + + for(i=1;i<=n;i++){ + + sol+=q(n-dbl)-q(S[i]); + + sol%=9917; + + up(S[i]); + + } + + fout< + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long n, q, x; + +unsigned long long r; + +pair v[100005]; + +long long stree[400050]; + + + +long long query(long long x, long long y, long long st, long long dr, long long node) + +{ + + if(y=dr) return stree[node]; + + long long mid = (st+dr)/2, s1=0, s2=0; + + if(x<=mid) s1 = query(x, y, st, mid, node*2); + + if(y>mid) s2 = query(x, y, mid+1, dr, node*2+1); + + return s1+s2; + +} + + + + + +void update(long long pos, long long st, long long dr, long long node) + +{ + + if(st==dr) + + stree[node]=1; + + else{ + + long long mid = (st+dr)/2; + + if(pos<=mid) update(pos, st, mid, node*2); + + else update(pos, mid+1, dr, node*2+1); + + stree[node] = stree[node*2]+stree[node*2+1]; + + } + +} + + + +int main() + +{ + + fin>>n; + + r = 0; + + for(long long i=1; i<=n; i++) + + { + + fin>>v[i].first; + + v[i].second = i; + + } + + sort(v+1, v+n+1); + + + + for(long long i=1; i<=n; i++) + + { + + r+=query(v[i].second+1, n, 1, n, 1); + + r=r%9917; + + + + update(v[i].second, 1, n, 1); + + } + + fout< + + + + + + +template class Normalize { + +private: + + int value[MAXN]; + + + +public: + + Normalize(int N, int V[]) { + + std::vector normSet; + + for (int i=0; i=1; X-=(X&(-X))) + + ans += AIB[X], ans %= 9917; + + return ans; + +} + + + +std::ifstream input ("inv.in"); + +std::ofstream output("inv.out"); + + + +void readInput() { + + input >> N; + + for (int i=1; i<=N; ++i) + + input >> V[i]; + +} + + + +void solveInput() { + + Normalize <100005> normVal(N, V+1); + + int ans = 0; + + for (int i=0; i + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + return 0; + +} diff --git a/invCount/aib/17f60b48-e319-4f10-a8d0-f62cc109e7a9.cpp b/invCount/aib/17f60b48-e319-4f10-a8d0-f62cc109e7a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..212976711c9d996e79e958cff3461bea7d2b5400 --- /dev/null +++ b/invCount/aib/17f60b48-e319-4f10-a8d0-f62cc109e7a9.cpp @@ -0,0 +1,142 @@ +#include + + + + +using namespace std; + + + +int getSum(int BITree[], int index) + +{ + + int sum = 0; + + while (index > 0) + + { + + sum += BITree[index]; + + index -= index & (-index); + + } + + return sum; + +} + + + + + +void updateBIT(int BITree[], int n, int index, int val) + +{ + + while (index <= n) + + { + + BITree[index] += val; + + index += index & (-index); + + } + +} + + + +void convert(int arr[],int n) + +{ + + int lst[n]; + + for(int i=1;i<=n;i++) + + lst[i]=arr[i]; + + sort(lst+1,lst+n+1); + + for(int i=1;i<=n;i++) + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + +} + + + +int getInvCount(int arr[], int n) + +{ + + int invcount = 0; + + convert(arr,n); + + int maxElement = 0; + + for (int i=1; i<=n; i++) + + if (maxElement < arr[i]) + + maxElement = arr[i]; + + + + int BIT[maxElement+1]; + + for (int i=1; i<=maxElement; i++) + + BIT[i] = 0; + + + + for (int i=n; i>0; i--) + + { + + invcount += getSum(BIT, arr[i]-1); + + invcount%=9917; + + updateBIT(BIT, maxElement, arr[i], 1); + + } + + + + return invcount; + +} + + + +int main() + +{ + + int arr[100000],n; + + ifstream fin("inv.in"); + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>arr[i]; + + fin.close(); + + ofstream fout("inv.out"); + + fout< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +static inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], N; + + + +int AIB[NMAX]; + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + + + + return; + +} + + + +static inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +int B[NMAX], C[NMAX]; + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + + + + return; + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + C[++C[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + C[++C[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=(lower_bound(C+1, C+C[0]+1, A[i])-C); + + + + return; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + ans+=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans%=MOD; + + } + + + + g< +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int vn[100001],aint[300001]; + +struct vec + +{ + + int nr, poz; + +}v[100001]; + +bool cmp(vec x, vec y) + +{ + + if(x.nr==y.nr) + + return x.pozmid) + + query(2*nod+1,mid+1,dr,x,y); + +} + +void update(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]++; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=mid) + + update(2*nod,st,mid); + + else + + update(2*nod+1,mid+1,dr); + + aint[nod]=(aint[2*nod]+aint[2*nod+1])%9917; + +} + +int main () + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i].nr,v[i].poz=i; + + normalizare(); + + for(int i=1;i<=n;i++) + + { + + poz=vn[i]; + + query(1,1,k,poz+1,k); + + update(1,1,k); + + } + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + + private: + + int a[5 + N]; + + + + int lsb(int i) { + + return (i & (-i)); + + } + + + + public: + + void Update(int pos) { + + for(int i = pos; i <= n; i += lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos; i >= 1; i -= lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b) { + + if(a.first == b.first) return a.second < b.second; + + return a.first < b.first; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++) { + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + sort(v + 1, v + n + 1); + + + + for(int i = n; i >= 1; i--) { + + inv += Fenw.Query(v[i].second); + + Fenw.Update(v[i].second); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/1fa53d8a-8ce5-4117-aca2-33b79095f5b9.cpp b/invCount/aib/1fa53d8a-8ce5-4117-aca2-33b79095f5b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73db369a0007edf5a3fd6ad132ec80dcb75660de --- /dev/null +++ b/invCount/aib/1fa53d8a-8ce5-4117-aca2-33b79095f5b9.cpp @@ -0,0 +1,65 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + + + +static const int MOD = 9917; + +static const int NMAX = 1e5+5; + +int aib[NMAX]; +int ind[NMAX]; +int v[NMAX]; +int n; + +int query(int position) +{ + int sum = 0; + for(int i = position; i>= 1; i-=(i&(-i))) + sum+=aib[i]; + return sum; +} + +void update(int position) +{ + for(int i = position; i<=n; i+=(i&(-i))) + aib[i]++; +} + +bool cmp(const int &a, const int &b) +{ + return v[a] < v[b]; +} + +int main() +{ + fin >> n; + + for(int i =1; i<=n; ++i) + { + fin >> v[i]; + ind[i] = i; + } + stable_sort(ind+1,ind+n+1,cmp); + + int sol = 0; + for(int i = 1; i<=n; ++i) + { + int pos = ind[i]; + + sol = (sol + pos - query(pos) -1) % MOD; + update(pos); + } + + fout << sol; + + + return 0; +} diff --git a/invCount/aib/2049244a-33b9-48b7-a48a-1cd43953ec9f.cpp b/invCount/aib/2049244a-33b9-48b7-a48a-1cd43953ec9f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..242ab97aa4be16b806793fe1fe0d77694c5f27e7 --- /dev/null +++ b/invCount/aib/2049244a-33b9-48b7-a48a-1cd43953ec9f.cpp @@ -0,0 +1,133 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + long long nrInv = 1LL * N * (N - 1) / 2; + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv -= sum(x); + + add(x); + + } + + g << nrInv % MOD; + + return 0; + +} diff --git a/invCount/aib/20875b00-c5a3-4c25-ab4c-6304e3f9c084.cpp b/invCount/aib/20875b00-c5a3-4c25-ab4c-6304e3f9c084.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c3ac6b7db3479750d1cb8e38b2f69d38ef53c8 --- /dev/null +++ b/invCount/aib/20875b00-c5a3-4c25-ab4c-6304e3f9c084.cpp @@ -0,0 +1,199 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/2274e730-b36e-4d9e-a1fd-629ff545e3a4.cpp b/invCount/aib/2274e730-b36e-4d9e-a1fd-629ff545e3a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0976ef353a5c0fba4935ad3230cff0e9ec66d4e --- /dev/null +++ b/invCount/aib/2274e730-b36e-4d9e-a1fd-629ff545e3a4.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + + + +using namespace std; + +ifstream cin("inv.in"); +ofstream cout("inv.out"); + +int AIB[100000 +1]; +vector> v; + +void update(int x,int n){ + for(;x<=n;x+=x&(-x)) + AIB[x]++; +} + +int query(int x){ + int ans=0; + + for(;x>0;x-=x&(-x)) + ans+=AIB[x]; + + return ans; +} + +int main(){ + int n; + cin>>n; + + int ans=0; + for(int i=1;i<=n;i++){ + int x; + cin>>x; + + v.push_back({x,i}); + } + + sort(v.begin(),v.end()); + + for(int i=0;i + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/246837e4-edb0-4c1d-ac43-1a6591fc750e.cpp b/invCount/aib/246837e4-edb0-4c1d-ac43-1a6591fc750e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/246837e4-edb0-4c1d-ac43-1a6591fc750e.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/24bc732a-a67b-44a1-b302-b407d1386fdb.cpp b/invCount/aib/24bc732a-a67b-44a1-b302-b407d1386fdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..537013d2e420b2846a2374f47bd1d89da1f9670d --- /dev/null +++ b/invCount/aib/24bc732a-a67b-44a1-b302-b407d1386fdb.cpp @@ -0,0 +1,112 @@ +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long n, q, x; + +unsigned long long r; + +pair v[100005]; + +long long stree[400050]; + + + +long long query(long long x, long long y, long long st, long long dr, long long node) + +{ + + if(y=dr) return stree[node]; + + long long mid = (st+dr)/2, s1=0, s2=0; + + if(x<=mid) s1 = query(x, y, st, mid, node*2); + + if(y>mid) s2 = query(x, y, mid+1, dr, node*2+1); + + return s1+s2; + +} + + + + + +void update(long long pos, long long st, long long dr, long long node) + +{ + + if(st==dr) + + stree[node]=1; + + else{ + + long long mid = (st+dr)/2; + + if(pos<=mid) update(pos, st, mid, node*2); + + else update(pos, mid+1, dr, node*2+1); + + stree[node] = stree[node*2]+stree[node*2+1]; + + } + +} + + + +int main() + +{ + + fin>>n; + + r = 0; + + for(long long i=1; i<=n; i++) + + { + + fin>>v[i].first; + + v[i].second = i; + + } + + sort(v+1, v+n+1); + + + + for(long long i=1; i<=n; i++) + + { + + r+=query(v[i].second+1, n, 1, n, 1); + + r=r%9917; + + + + update(v[i].second, 1, n, 1); + + } + + fout< +#include +#include +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +const int mod = 9917; + +int aib[5 + 100000]; + +int n; + +vector> v; + + + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] = (aib[i] + val) % mod; + +} + + + +int Query(int p) + +{ + + int suma = 0; + + for (int i = p; i >= 1; i -= i & -i) + + suma = (suma + aib[i]) % mod; + + + + return suma; + +} + + + +int main() + +{ + + fin >> n; + + v = vector>(n); + + int val; + + for (int i = 0; i < n; ++i) + + { + + fin >> val; + + v[i] = {val, i + 1}; + + } + + + + sort(v.begin(), v.end(), greater>()); + + + + int res = 0; + + for (auto& p : v) + + { + + res += Query(p.second); + + if (res >= mod) + + res -= mod; + + Update(p.second, 1); + + } + + + + fout << res; + + + + return 0; + +} diff --git a/invCount/aib/259dae39-5361-442a-9f80-c02b7f68ef73.cpp b/invCount/aib/259dae39-5361-442a-9f80-c02b7f68ef73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73ee02c72fb60d28226aefbe9fe78ac2ba9e4022 --- /dev/null +++ b/invCount/aib/259dae39-5361-442a-9f80-c02b7f68ef73.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[200005], aib[200005], q, tip, a[200005], p; + +long long ans; + +unordered_mapmp; + +void update(int nod) + +{ + + for(int i=nod; i<=n; i+=(i&(-i))) + + aib[i]++; + +} + +int query(int nod) + +{ + + int sum=0; + + for(int i=nod; i; i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1, a+n+1); + + for(int i=1; i<=n; i++) + + if(mp.find(a[i])==mp.end()) mp[a[i]]=++p; + + for(int i=n; i>=1; i--) + + { + + ans=(ans+query(mp[v[i]]-1))%9917; + + update(mp[v[i]]); + + } + + g< + + + + +using namespace std; + +typedef long long ll; + + + +const int MOD = 9917; + + + +int aib[100005],n; + + + +int sum(int x){ + + int s = 0; + + for(int i = x;i > 0;i -= i&(-i)) + + s += aib[i]; + + return s; + +} + + + +void update(int x){ + + for(int i = x;i <= n;i += i&(-i)) + + aib[i]++; + +} + +vector > v; + +int main() + +{ + + int i; + + ll rez = 0; + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin >> n; + + for(i = 1;i <= n;i++){ + + int x; + + cin >> x; + + v.push_back({x,i}); + + } + + sort(v.begin(),v.end()); + + i = 1; + + for(auto x : v){ + + rez += 1LL * (i - 1 - sum(x.second - 1)); + + rez %= MOD; + + update(x.second); + + i++; + + } + + cout << rez; + + return 0; + +} diff --git a/invCount/aib/274017d8-9947-4515-a523-337c50589ffb.cpp b/invCount/aib/274017d8-9947-4515-a523-337c50589ffb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20c1528a71b747b997054956277f3446346bf0a0 --- /dev/null +++ b/invCount/aib/274017d8-9947-4515-a523-337c50589ffb.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + return 0; + +} diff --git a/invCount/aib/28766c80-0900-46a6-bbd1-f1ad847c9bfd.cpp b/invCount/aib/28766c80-0900-46a6-bbd1-f1ad847c9bfd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f87b7149ad126e7a1450f8d6e85eec5d5decf5d9 --- /dev/null +++ b/invCount/aib/28766c80-0900-46a6-bbd1-f1ad847c9bfd.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const long long N=200009; + +struct xd + +{ + + long long x,poz; + +}a[N]; + +long long n; + +long long aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(long long i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + +const int MOD = 9917; + + + +struct nume{ + + int node,ind; + +}; + + + +vector vec; + + + +int V[DMAX]; + +int tree[4*DMAX]; + + + +int n,cate; + +ll ans; + + + +inline bool operator<(nume x,nume y){ + + return x.node>n; + + for(i=1;i<=n;i++){ + + fin>>V[i]; + + vec.push_back({V[i],i}); + + } + + sort(vec.begin(),vec.end()); + + int prec=-1e9; + + for(auto& it:vec){ + + if(it.node == prec){ + + V[it.ind]=dif; + + continue; + + } + + dif++; + + prec=it.node; + + V[it.ind]=dif; + + } + + for(i=n;i>=1;i--){ + + if(V[i] > 1) + + ans+=query(1,1,n,V[i]-1); + + update(1,1,n,V[i]); + + } + + fout<= pos) + + ans+=query(2*node,st,mij,pos); + + return ans; + +} diff --git a/invCount/aib/28abc4dc-92d7-4f36-88b6-24707ae6e4ec.cpp b/invCount/aib/28abc4dc-92d7-4f36-88b6-24707ae6e4ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2feb106d2b89afa7804d2ec1106385ffafff6582 --- /dev/null +++ b/invCount/aib/28abc4dc-92d7-4f36-88b6-24707ae6e4ec.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(;p;p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(;p<=k;p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + for(i=1; i<=n; i++){ + + if(w[i].first!=w[i-1].first) k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++){ + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< +#include +#include + + +using namespace std; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + + + +const int MAXN = 100000; + +pair vp[1 + MAXN]; + +bool sortVal (pair pair1, pair pair2) + +{ + + return pair1.second < pair2.second; + +} + +bool sortInd (pair pair1, pair pair2) + +{ + + return pair1.first < pair2.first; + +} + + + +int arb[4 * MAXN]; + +int l, r; + +int query (int p, int st, int dr) + +{ + + if (l <= st && dr <= r) + + return arb[p]; + + else + + { + + int ans = 0; + + int m = (st + dr) / 2; + + if(l <= m) + + ans += query (2 * p, st, m); + + if(r >= m + 1) + + ans += query (2 * p + 1, m + 1, dr); + + return ans; + + } + +} + + + +int poz; + +void update (int p, int st, int dr) + +{ + + if (st == dr) + + arb[p]++; + + else + + { + + int m = (st + dr) / 2; + + if(poz <= m) + + update (2 * p, st, m); + + else + + update (2 * p + 1, m + 1, dr); + + arb[p] = arb[2 * p] + arb[2 * p + 1]; + + } + +} + + + +int main() + +{ + + int n; + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> vp[i].second; + + vp[i].first = i; + + } + + + + sort (vp + 1, vp + n + 1, sortVal); + + + + int ant = vp[1].second; + + vp[1].second = 1; + + for (int i = 2; i <= n; i++) + + { + + if (vp[i].second == ant) + + { + + vp[i].second = vp[i-1].second; + + } + + else + + { + + ant = vp[i].second; + + vp[i].second = vp[i-1].second + 1; + + } + + } + + + + sort (vp + 1, vp + n + 1, sortInd); + + + + int rez = 0; + + for (int i = 1; i <= n; i++) + + { + + l = vp[i].second + 1; + + r = n; + + if(l <= r) + + rez += query(1, 1, n); + + rez %= 9917; + + + + poz = vp[i].second; + + update(1, 1, n); + + } + + out< + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +pair v[100005]; +int n; +int aib[100005]; + +void Read() +{ + f>>n; + for(int i = 1;i <= n;++i) + { + f>>v[i].first; + v[i].second = i; + } + sort(v + 1, v + n + 1); +} + +int query(int x) +{ + int sum = 0; + for(int i = x;i;i -= (( i ^ (i - 1)) & i)) + sum += aib[i]; + return sum; +} + +void update(int x) +{ + for(int i = x;i <= n;i += (( i ^ (i - 1)) & i)) + aib[i]++; +} + +void Solve() +{ + int res = 0; + for(int i = 1;i <= n;++i) + { + update(v[i].second); + res = (res + query(n) - query(v[i].second)) % 9917; + } + g< +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long long i,n,m,a[100005],b[100005],v[100005],aib[100005],st,dr,poz,mij,nr; + +int ub(int x) + +{ + + return(x&(-x)); + +} + +long long sum(int x) + +{ + + long long s=0; + + for(int i=x; i>=1; i-=ub(i)) + + { + + s+=aib[i]; + + s%=9917; + + } + + return s; + +} + +void Add(int x) + +{ + + for(int i=x; i<=m; i+=ub(i)) + + { + + aib[i]++; + + aib[i]%=9917; + + } + +} + +int main() + +{ + + f>>n; + + for(i=1; i<=n; ++i) + + { + + f>>a[i]; + + b[i]=a[i]; + + } + + sort(b+1,b+n+1); + + v[++m]=b[1]; + + for(i=2; i<=n; ++i) + + if(b[i]!=b[i-1]) + + v[++m]=b[i]; + + for(i=1; i<=n; ++i) + + { + + st=1; + + dr=m; + + poz=0; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + if(v[mij]<=a[i]) + + { + + poz=mij; + + st=mij+1; + + } + + else + + dr=mij-1; + + } + + a[i]=poz; + + } + + for(i=n; i>=1; --i) + + { + + nr+=sum(a[i]-1); + + nr%=9917; + + Add(a[i]); + + } + + g< +#include +#include + + +using namespace std; + + + +int aib[100005], n, k; + + + +const int mod = 9917; + + + +struct Element + +{ + + int val, pos, new_val; + +} v[100005]; + + + +bool ByValue (Element a, Element b) + +{ + + return a.val < b.val; + +} + + + +bool ByIndex (Element a, Element b) + +{ + + return a.pos < b.pos; + +} + + + +void Read_Normalize () + +{ + + ifstream fin ("inv.in"); + + fin >> n; + + int i, x; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + v[i].val = x; + + v[i].pos = i; + + } + + sort (v + 1, v + n + 1, ByValue); + + v[1].new_val = 1; + + k = 1; + + for (i = 2; i <= n; i++) + + if (v[i].val == v[i - 1].val) + + v[i].new_val = k; + + else k++, v[i].new_val = k; + + sort (v + 1, v + n + 1, ByIndex); + +} + + + + + +void Update (int p, int x) + +{ + + for (; p <= n; p += (p & - p)) + + aib[p] += x; + +} + + + +int Sum (int p) + +{ + + int s; + + for (s = 0; p > 0; p -= (p & - p)) + + s += aib[p]; + + return s; + +} + + + +void Solve () + +{ + + int ans = 0, i; + + Update(v[n].new_val, 1); + + for (i = n - 1; i >= 1; i--) + + { + + ans = (ans + Sum(v[i].new_val - 1)) % mod; + + Update(v[i].new_val, 1); + + } + + ofstream fout ("inv.out"); + + fout << ans << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read_Normalize(); + + Solve(); + + return 0; + +} diff --git a/invCount/aib/2f07c2aa-43ff-4d90-a6b4-19bd2311177f.cpp b/invCount/aib/2f07c2aa-43ff-4d90-a6b4-19bd2311177f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59c6541ee39eed39deba17722d50b0e7c9f94a2f --- /dev/null +++ b/invCount/aib/2f07c2aa-43ff-4d90-a6b4-19bd2311177f.cpp @@ -0,0 +1,106 @@ +#include + + + + +using namespace std; + + + +const int mxn = 100 * 1000 + 10, md = 9917; + + + +int arb[ 4 * mxn ]; + + + +pair< int, int > sir[ mxn ]; + +int sol[ mxn ]; + + + +int n; + + + +void update(int nod, int st, int sf, int poz){ + + arb[ nod ]++; + + arb[ nod ] %= md; + + if(st < sf){ + + int mid = (st + sf) / 2; + + if(mid >= poz) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, sf, poz); + + } + +} + + + +int query(int nod, int st, int sf, int poz){ + + if(poz <= st) + + return arb[ nod ]; + + else if(sf < poz) + + return 0; + + int mid = (st + sf)/2; + + return (query(2 * nod, st, mid, poz) + query(2 * nod + 1, mid + 1, sf, poz)) % md; + +} + + + +int main() + +{ + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin>> n; + + for(int i = 0, x; i < n; i++){ + + cin>> x; + + sir[ i ] = make_pair(x, i); + + } + + sort(sir, sir + n); + + int s = 0; + + for(int i = 0; i < n; i++){ + + s += query(1, 0, n - 1, sir[ i ].second); + + s %= md; + + update(1, 0, n - 1, sir[ i ].second); + + } + + cout<< s; + + return 0; + +} diff --git a/invCount/aib/2fe7a270-bc63-4e91-abba-2fb82a69e762.cpp b/invCount/aib/2fe7a270-bc63-4e91-abba-2fb82a69e762.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f47732e537ec9573bbb79c5e172717a7cddbf300 --- /dev/null +++ b/invCount/aib/2fe7a270-bc63-4e91-abba-2fb82a69e762.cpp @@ -0,0 +1,150 @@ +#include + + + + +using namespace std; + + + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + + + +struct normalizare + +{ + + int val,poz; + +}v[100100]; + + + + + +int compare (normalizare A,normalizare B) + +{ + + if(A.val == B.val)return (A.poz=1;i-=(i&(-i))) + + S+=AIB[i]; + + return S; + +} + + + +int i,x,total,numere_mai_mari,sol[100100]; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x; + + v[i].val=x; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare); + + + + for(i=1;i<=n;i++) + + { + + sol[v[i].poz]=i; + + } + + + + + + for(i=1;i<=n;i++) + + { + + x=sol[i]; + + + + numere_mai_mari=suma(n)-suma(x); + + total = (total+numere_mai_mari) % 9917; + + + + adaug(x,1); + + + + } + + + + g< +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[200005], aib[200005], q, tip, a[200005], p; + +long long ans; + +unordered_mapmp; + +void update(int nod) + +{ + + for(int i=nod; i<=n; i+=(i&(-i))) + + aib[i]++; + +} + +int query(int nod) + +{ + + int sum=0; + + for(int i=nod; i; i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1, a+n+1); + + for(int i=1; i<=n; i++) + + if(mp.find(a[i])==mp.end()) mp[a[i]]=++p; + + for(int i=n; i>=1; i--) + + { + + ans=(ans+query(mp[v[i]]-1))%9917; + + update(mp[v[i]]); + + } + + g< +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/33570024-137e-11eb-80e9-309c234925a0.cpp b/invCount/aib/33570024-137e-11eb-80e9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9058a876922b66a404fd3bc8db680b39b3b517fc --- /dev/null +++ b/invCount/aib/33570024-137e-11eb-80e9-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + + + +int ans,aint[400005],v[100005],n,t,nr,st,dr,s; + +vector b; + + + +void query(int nod,int l,int r, int st,int dr){ + + if(st<=l && dr>=r){ + + s+=aint[nod]; + + return; + + } + + int mij=(l+r)>>1; + + if(st<=mij){ + + query(nod*2,l,mij,st,dr); + + } + + if(dr>mij){ + + query(nod*2+1,mij+1,r,st,dr); + + } + +} + + + +void update(int nod,int l,int r,int poz,int val){ + + if(l==poz && r==poz){ + + aint[nod]+=val; + + return; + + } + + int mij; + + mij=(l+r)>>1; + + if(poz<=mij){ + + update(nod*2,l,mij,poz,val); + + } + + if(poz>mij){ + + update(nod*2+1,mij+1,r,poz,val); + + } + + aint[nod]=aint[nod*2]+aint[nod*2+1]; + + + +} + + + +int main() + +{ + + cin>>n; + + for (int i=1;i<=n;i++) { + + cin>>v[i]; + + b.push_back(v[i]); + + } + + sort(b.begin(),b.end()); + + b.erase(unique(b.begin(),b.end()),b.end()); + + for (int i=1;i<=n;i++){ + + v[i]=lower_bound(b.begin(),b.end(),v[i])-b.begin()+1; + + } + + for(int i=1;i<=n;i++){ + + s=0; + + query(1,1,n,v[i]+1,n); + + ans+=s; + + ans%=9917; + + update(1,1,n,v[i],1); + + } + + cout< + + + +using namespace std; + +struct Fenwick { + int n; + vector t; + Fenwick(int n_) : n(n_), t(n + 1) {} + void Update(int pos, int val) { + for (++pos; pos <= n; pos += pos & -pos) t[pos] += val; + } + int Query(int pos) { + int ans = 0; + for (++pos; pos; pos -= pos & -pos) ans += t[pos]; + return ans; + } +}; + +int main() { + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + auto all = v; + all.emplace_back(-(int)1e9); + sort(all.begin(), all.end()); + all.erase(unique(all.begin(), all.end()), all.end()); + for (int i = 0; i < n; ++i) { + v[i] = lower_bound(all.begin(), all.end(), v[i]) - all.begin(); + } + + Fenwick fw(n); + int64_t ans = 0; + for (int i = n - 1; i >= 0; --i) { + ans += fw.Query(v[i] - 1); + fw.Update(v[i], 1); + } + cout << ans % 9917 << endl; +} diff --git a/invCount/aib/33850a52-137e-11eb-87fc-309c234925a0.cpp b/invCount/aib/33850a52-137e-11eb-87fc-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..430c1e27be7f3ff748893fd82e7fc1fe4161277a --- /dev/null +++ b/invCount/aib/33850a52-137e-11eb-87fc-309c234925a0.cpp @@ -0,0 +1 @@ +#include using namespace std; ifstream f("inv.in"); ofstream g("inv.out"); #define ub(x) (x&(-x)) #define MOD 9917 int n,i,p,nr,AIB[100005],x; long long inv; vector >v; long long sol; void add(int x) { for(int j=x; j<=n; j+=ub(j)) AIB[j]++; } long long sum(int x) { long long s=0; for(int j=x; j>0; j-=ub(j)) s+=AIB[j]; return s; } int main() { f>>n; for(i=0;i>x; v.push_back(make_pair(x,i+1)); } sort(v.begin(),v.end()); inv=1LL*n*(n-1)/2; for(i=0; i + + +using namespace std; + +void __print(int x) {cerr << x;} +void __print(long x) {cerr << x;} +void __print(long long x) {cerr << x;} +void __print(unsigned x) {cerr << x;} +void __print(unsigned long x) {cerr << x;} +void __print(unsigned long long x) {cerr << x;} +void __print(float x) {cerr << x;} +void __print(double x) {cerr << x;} +void __print(long double x) {cerr << x;} +void __print(char x) {cerr << '\'' << x << '\'';} +void __print(const char *x) {cerr << '\"' << x << '\"';} +void __print(const string &x) {cerr << '\"' << x << '\"';} +void __print(bool x) {cerr << (x ? "true" : "false");} + +template +void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} +template +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} +void _print() {cerr << "]\n";} +template +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} + + + + + + + +struct fenwick_tree{ + vector tr; + int n; + void build(vector &arr){ + for(int i=1;i<=n;i++){ + tr[i]+=arr[i-1]; + if(i+(i&-i)<=n){ + tr[i+(i&-i)]+=tr[i]; + } + } + + } + void update(int pos,int val){ + while(pos<=n){ + tr[pos]+=val; + pos=pos+(pos&-pos); + } + + } + int query(int l,int r){ + int s1=0; + while(l>0){ + s1+=tr[l]; + l=l-(l&-l); + } + int s2=0; + while(r>0){ + s2+=tr[r]; + r=r-(r&-r); + } + + return s2-s1; + } +}; + +int main(){ + ifstream in("inv.in"); + ofstream out("inv.out"); + int n; + in>>n; + vector a(n); + for(auto &x:a)in>>x; + int mod=9917; + int ans=0; + auto temp=a; + temp.push_back((int)-1e9); + sort(temp.begin(),temp.end()); + temp.erase(unique(temp.begin(),temp.end()),temp.end()); + for(int i=0;i=0;i--){ + ans=ans+ft.query(0,a[i]-1); + ft.update(a[i],1); + ans%=mod; + } + out< +# include +using namespace std; + +int val,poz; + +struct inv + +{ + + int val,poz; + +}; + +inv v[100001]; + +int a[100001],i,x,n; + +long long r; + +bool cmp(inv a, inv b) + +{ + + if (a.val==b.val) return(a.poz=1; i-=(i&(-i))) + + s+=a[i]; + + return s; + +} + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + f>>n; + + r=n-1; + + r*=n; + + r/=2; + + for(i=1; i<=n; ++i) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(i=1; i<=n; ++i) + + { + + r=r-query(v[i].poz); + + update(v[i].poz); + + } + + r%=9917; + + g<< r; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/340bd0b0-3af4-4717-a281-98d6c802d821.cpp b/invCount/aib/340bd0b0-3af4-4717-a281-98d6c802d821.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3af75edd8e264ed7f58470f70dd2f13cba0d2f15 --- /dev/null +++ b/invCount/aib/340bd0b0-3af4-4717-a281-98d6c802d821.cpp @@ -0,0 +1,185 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX = 1e5 + 5, MOD = 9917; + + + +long long i, n, m, a[NMAX], b[NMAX], v[NMAX], aib[NMAX], st, dr, poz, mij, nr; + + + +int ub (int x) + +{ + + return (x & (-x)); + +} + + + +long long sum (int x) + +{ + + long long s = 0; + + + + for(int i = x; i >= 1; i -= ub(i)) + + { + + s += aib[i]; + + + + s %= MOD; + + } + + + + return s; + +} + +void Add (int x) + +{ + + for(int i = x; i <= m; i += ub(i)) + + { + + aib[i]++; + + + + aib[i] %= MOD; + + } + +} + + + +int main() + +{ + + f>>n; + + + + for(i=1; i<=n; i++) + + { + + f>>a[i]; + + + + b[i]=a[i]; + + } + + + + sort(b+1,b+n+1); + + + + v[++m]=b[1]; + + + + for(i=2; i<=n; i++) + + if(b[i]!=b[i-1]) + + v[++m]=b[i]; + + + + for(i=1; i<=n; i++) + + { + + st=1; + + dr=m; + + + + poz=0; + + + + while(st<=dr) + + { + + mij=(st+dr)/2; + + + + if(v[mij] <= a[i]) + + { + + poz=mij; + + st=mij+1; + + } + + else + + dr=mij-1; + + } + + a[i]=poz; + + } + + + + for(i = n; i >= 1; i--) + + { + + nr += sum(a[i] - 1); + + + + nr %= 9917; + + + + Add(a[i]); + + } + + + + g< + + + + + + +using namespace std; + + + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); +int n; + +int aib[100010]; + +int a[100010]; + +int b[100010]; + + + +int cb(int val, int left, int right) { + + if(left > right) { + + return -1; + + } + + int mijl = (left+right) / 2; + + if(b[mijl] == val && b[mijl -1] < val) { + + return mijl; + + } + + if(b[mijl] < val) { + + return cb(val, mijl + 1, right); + + } else { + + return cb(val, left, mijl); + + } + +} + + + +void adauga(int x) { + + for(int i = x; i <= n; i += i & (-i)) { + + aib[i]++; + + } + +} + + + +int scoate(int x) { + + int sol = 0; + + for(int i=x; i >= 1; i -= i & (-i)) { + + sol += aib[i]; + + } + + return sol; + +} + + + +void solve() { + + fin >> n; + + + + for(int i=1; i <=n; i++) { + + fin >> a[i]; + + b[i] = a[i]; + + } + + sort(b + 1, b + n + 1); + + + + int sol = 0; + + + + for(int i=1; i <= n; i++) { + + int x =cb(a[i], 1, n); + + sol = (sol + i - 1 - scoate(x)) % 9917; + + adauga(x); + + } + + fout << sol; + +} + + + + + +int main() { + + ios_base::sync_with_stdio(0); + + fin .tie(0); + + fout.tie(0); + + + + int testCases = 1; + + + + while(testCases--) { + + solve(); + + } + +} diff --git a/invCount/aib/34309c48-7d07-422d-9a24-d6edeb25dad5.cpp b/invCount/aib/34309c48-7d07-422d-9a24-d6edeb25dad5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9058a876922b66a404fd3bc8db680b39b3b517fc --- /dev/null +++ b/invCount/aib/34309c48-7d07-422d-9a24-d6edeb25dad5.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + + + +int ans,aint[400005],v[100005],n,t,nr,st,dr,s; + +vector b; + + + +void query(int nod,int l,int r, int st,int dr){ + + if(st<=l && dr>=r){ + + s+=aint[nod]; + + return; + + } + + int mij=(l+r)>>1; + + if(st<=mij){ + + query(nod*2,l,mij,st,dr); + + } + + if(dr>mij){ + + query(nod*2+1,mij+1,r,st,dr); + + } + +} + + + +void update(int nod,int l,int r,int poz,int val){ + + if(l==poz && r==poz){ + + aint[nod]+=val; + + return; + + } + + int mij; + + mij=(l+r)>>1; + + if(poz<=mij){ + + update(nod*2,l,mij,poz,val); + + } + + if(poz>mij){ + + update(nod*2+1,mij+1,r,poz,val); + + } + + aint[nod]=aint[nod*2]+aint[nod*2+1]; + + + +} + + + +int main() + +{ + + cin>>n; + + for (int i=1;i<=n;i++) { + + cin>>v[i]; + + b.push_back(v[i]); + + } + + sort(b.begin(),b.end()); + + b.erase(unique(b.begin(),b.end()),b.end()); + + for (int i=1;i<=n;i++){ + + v[i]=lower_bound(b.begin(),b.end(),v[i])-b.begin()+1; + + } + + for(int i=1;i<=n;i++){ + + s=0; + + query(1,1,n,v[i]+1,n); + + ans+=s; + + ans%=9917; + + update(1,1,n,v[i],1); + + } + + cout< +#include + + + + +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct numar { + + int id, poz; + +}; + + + +int n, i, x, sol, cnt; + +int A[100005], f[100005]; + + + +numar v[100005]; + + + +inline bool cmp (numar a, numar b){ + + return a.id < b.id; + +} + + + +inline void update (int i){ + + while (i <= n){ + + A[i]++; + + i += (i&-i); + + } + +} + + + +inline int query (int i){ + + int sol = 0; + + while (i){ + + sol += A[i]; + + i -= (i&-i); + + } + + return sol; + +} + + + +int main(){ + + fin >> n; + + for (i=1; i<=n; i++){ + + fin >> x; + + v[i] = {x, i}; + + } + + sort (v + 1, v + n + 1, cmp); + + f[v[1].poz] = 1; + + for (i=2; i<=n; i++){ + + if (v[i].id == v[i-1].id){ + + cnt++; + + f[v[i].poz] = f[v[i-1].poz]; + + } + + else{ + + f[v[i].poz] = i - cnt; + + } + + } + + cnt = n - cnt; + + for (i=1; i<=n; i++){ + + sol += (query(cnt) - query(f[i])); + + sol = sol%9917; + + update (f[i]); + + } + + fout << sol; + + return 0; + +} diff --git a/invCount/aib/343e1bc2-137e-11eb-ad4c-309c234925a0.cpp b/invCount/aib/343e1bc2-137e-11eb-ad4c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7cc72f6343b4dfad026eb6cd78db26fcb0510f3 --- /dev/null +++ b/invCount/aib/343e1bc2-137e-11eb-ad4c-309c234925a0.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +using namespace std; + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + +const int lim=1e5+3; + +const int mod=9917; + +int v[lim],f[lim]; + +int tree[lim]; + +int n; + +vector upd; + +void sum(int &a,int b) + +{ + + a+=b; + + if(a>=mod) + + a-=mod; + +} + +void add(int k,int x) + +{ + + while(k<=n) + + { + + sum(tree[k],x); + + k+=k&-k; + + } + +} + +int cnt(int k) + +{ + + int rez=0; + + while(k>0) + + { + + sum(rez,tree[k]); + + k-=k&-k; + + } + + return rez; + +} + +bool ordine(int a,int b) + +{return f[a]>f[b];} + +int main() + +{ + + cin>>n; + + for(int i=1;i<=n;++i) + + { + + cin>>f[i]; + + v[i]=i; + + } + + sort(v+1,v+n+1,ordine); + + int ans=0; + + sum(ans,cnt(v[1])); + + upd.push_back(v[1]); + + for(int i=2;i<=n;++i) + + { + + if(f[v[i]]!=f[v[i-1]]) + + { + + for(int j=0;j + + + + +using namespace std; + + + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + + + +struct normalizare + +{ + + int val,poz; + +}v[100100]; + + + + + +int compare (normalizare A,normalizare B) + +{ + + if(A.val == B.val)return (A.poz=1;i-=(i&(-i))) + + S+=AIB[i]; + + return S; + +} + + + +int i,x,total,numere_mai_mari,sol[100100]; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x; + + v[i].val=x; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare); + + + + for(i=1;i<=n;i++) + + { + + sol[v[i].poz]=i; + + } + + + + + + for(i=1;i<=n;i++) + + { + + x=sol[i]; + + + + numere_mai_mari=suma(n)-suma(x); + + total = (total+numere_mai_mari) % 9917; + + + + adaug(x,1); + + + + } + + + + g< +#include +using namespace std; + + + +FILE *fin = fopen("inv.in", "r"); + +FILE *fout = fopen("inv.out", "w"); + + + +struct tablou {int x; int poz;} a[100005]; + + + +int n,i,aib[100005]; + +long long total; + + + +bool comparare(tablou a, tablou b) + +{ + + if(a.x!=b.x) return (a.x < b.x); + + return(a.poz=1; i-=i&(-i)) + + ans += aib[i]; + + + + return ans; + +} + + + +int main() + +{ + + fscanf(fin, "%d", &n); + + for(i=1; i<=n; ++i) + + { + + fscanf(fin, "%d", &a[i].x); + + a[i].poz = i; + + } + + + + sort(a+1, a+n+1, comparare); + + + + for(i=1; i<=n; ++i) + + { + + total += 1LL*sum(a[i].poz); + + update(a[i].poz); + + } + + + + fprintf(fout, "%d", (1LL*n*(n-1)/2 - total)%9917); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invCount/aib/3666cb5d-5f16-49f2-8a5e-1e7ad21fe95d.cpp b/invCount/aib/3666cb5d-5f16-49f2-8a5e-1e7ad21fe95d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11b7670f2691ab2aab255b52e9b0d532c88d363f --- /dev/null +++ b/invCount/aib/3666cb5d-5f16-49f2-8a5e-1e7ad21fe95d.cpp @@ -0,0 +1,104 @@ +#include +int N, M; + +int AIB[100005]; + + + +int Ind[100005]; + +std::pair V[100005]; + + + +void Update(int X, int Value) { + + for (X; X<=N; X += X&(-X)) + + AIB[X] += Value, + + AIB[X] %= 9917; + +} + + + +int Query(int X) { + + int Sum = 0; + + for (X; X; X -= X&(-X)) + + Sum += AIB[X], + + Sum %= 9917; + + return Sum; + +} + + + +std::ifstream In("inv.in"); + +std::ofstream Out("inv.out"); + + + +void Citire() { + + In >> N; + + for (int i=0; i> V[i+1].first, + + V[i+1].second = i+1; + +} + + + +void Rezolvare() { + + std::sort (V+1, V+N+1); + + for (int i=1; i<=N; ++i) + + Ind[V[i].second] = i; + + + + int NInv = 0; + + + + for (int i=0, X; i +#include +#include + + +using namespace std; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + + + +const int MAXN = 100000; + +pair vp[1 + MAXN]; + +bool sortVal (pair pair1, pair pair2) + +{ + + return pair1.second < pair2.second; + +} + +bool sortInd (pair pair1, pair pair2) + +{ + + return pair1.first < pair2.first; + +} + + + +int arb[4 * MAXN]; + +int l, r; + +int query (int p, int st, int dr) + +{ + + if (l <= st && dr <= r) + + return arb[p]; + + else + + { + + int ans = 0; + + int m = (st + dr) / 2; + + if(l <= m) + + ans += query (2 * p, st, m); + + if(r >= m + 1) + + ans += query (2 * p + 1, m + 1, dr); + + return ans; + + } + +} + + + +int poz; + +void update (int p, int st, int dr) + +{ + + if (st == dr) + + arb[p]++; + + else + + { + + int m = (st + dr) / 2; + + if(poz <= m) + + update (2 * p, st, m); + + else + + update (2 * p + 1, m + 1, dr); + + arb[p] = arb[2 * p] + arb[2 * p + 1]; + + } + +} + + + +int main() + +{ + + int n; + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> vp[i].second; + + vp[i].first = i; + + } + + + + sort (vp + 1, vp + n + 1, sortVal); + + + + int ant = vp[1].second; + + vp[1].second = 1; + + for (int i = 2; i <= n; i++) + + { + + if (vp[i].second == ant) + + { + + vp[i].second = vp[i-1].second; + + } + + else + + { + + ant = vp[i].second; + + vp[i].second = vp[i-1].second + 1; + + } + + } + + + + sort (vp + 1, vp + n + 1, sortInd); + + + + int rez = 0; + + for (int i = 1; i <= n; i++) + + { + + l = vp[i].second + 1; + + r = n; + + if(l <= r) + + rez += query(1, 1, n); + + rez %= 9917; + + + + poz = vp[i].second; + + update(1, 1, n); + + } + + out< + + + + + + +template class Normalize { + +private: + + int value[MAXN]; + + + +public: + + Normalize(int N, int V[]) { + + std::vector normSet; + + for (int i=0; i=1; X-=(X&(-X))) + + ans += AIB[X], ans %= 9917; + + return ans; + +} + + + +std::ifstream input ("inv.in"); + +std::ofstream output("inv.out"); + + + +void readInput() { + + input >> N; + + for (int i=1; i<=N; ++i) + + input >> V[i]; + +} + + + +void solveInput() { + + Normalize <100005> normVal(N, V+1); + + int ans = 0; + + for (int i=0; i + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +static inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], N; + + + +int AIB[NMAX]; + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + + + + return; + +} + + + +static inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +int B[NMAX], C[NMAX]; + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + + + + return; + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + C[++C[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + C[++C[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=(lower_bound(C+1, C+C[0]+1, A[i])-C); + + + + return; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + ans+=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans%=MOD; + + } + + + + g< +#include +using namespace std; + + + + + + + +int getSum(int BITree[], int index) + + + +{ + + + + int sum = 0; + + + + while (index > 0) + + + + { + + + + sum += BITree[index]; + + + + index -= index & (-index); + + + + } + + + + return sum; + + + +} +void updateBIT(int BITree[], int n, int index, int val) + + + +{ + + + + while (index <= n) + + + + { + + + + BITree[index] += val; + + + + index += index & (-index); + + + + } + + + +} + + + + + + + +void convert(int arr[],int n) + + + +{ + + + + int lst[n]; + + + + for(int i=1;i<=n;i++) + + + + lst[i]=arr[i]; + + + + sort(lst+1,lst+n+1); + + + + for(int i=1;i<=n;i++) + + + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + + + +} + + + + + + + +int getInvCount(int arr[], int n) + + + +{ + + + + int invcount = 0; + + + + convert(arr,n); + + + + int maxElement = 0; + + + + for (int i=1; i<=n; i++) + + + + if (maxElement < arr[i]) + + + + maxElement = arr[i]; + + + + + + + + int BIT[maxElement+1]; + + + + for (int i=1; i<=maxElement; i++) + + + + BIT[i] = 0; + + + + + + + + for (int i=n; i>0; i--) + + + + { + + + + invcount += getSum(BIT, arr[i]-1); + + + + invcount%=9917; + + + + updateBIT(BIT, maxElement, arr[i], 1); + + + + } + + + + + + + + return invcount; + + + +} + + + + + + + +int main() + + + +{ + + + + int arr[100000],n; + + + + ifstream fin("inv.in"); + + + + fin>>n; + + + + for(int i=1;i<=n;i++) + + + + fin>>arr[i]; + + + + fin.close(); + + + + ofstream fout("inv.out"); + + + + fout< +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long A[100002], Srt[100002]; + +int N, lg; + +int aib[100002]; + +int i; + +int sol; + + + +void update(int X, int val) + +{ + + for(; X <= lg; X += ((X ^ (X - 1)) & X)) + + aib[X] += val; + +} + + + +int query(int X) + +{ + + int ret = 0; + + for(; X >= 1; X -= ((X ^ (X - 1)) & X)) + + ret += aib[X]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for(i = 1; i <= N; i++) + + fin >> A[i], Srt[i] = A[i]; + + sort(Srt + 1, Srt + N + 1); + + lg = 1; + + for(i = 2; i <= N; i++) + + if(Srt[i] != Srt[lg]) + + Srt[++lg] = Srt[i]; + + for(i = 1; i <= N; i++) + + A[i] = lower_bound(Srt + 1, Srt + lg + 1, A[i]) - Srt; + + for(i = N; i >= 1; i--) + + { + + sol = (sol + query(A[i] - 1)) % 9917; + + update(A[i], 1); + + } + + fout << sol << "\n"; + + return 0; + +} diff --git a/invCount/aib/382014e8-137e-11eb-a264-309c234925a0.cpp b/invCount/aib/382014e8-137e-11eb-a264-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b68bf837bf8167740732961e84da439b546e8d6c --- /dev/null +++ b/invCount/aib/382014e8-137e-11eb-a264-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +pair v[100005]; +int n; +int aib[100005]; + +void Read() +{ + f>>n; + for(int i = 1;i <= n;++i) + { + f>>v[i].first; + v[i].second = i; + } + sort(v + 1, v + n + 1); +} + +int query(int x) +{ + int sum = 0; + for(int i = x;i;i -= (( i ^ (i - 1)) & i)) + sum += aib[i]; + return sum; +} + +void update(int x) +{ + for(int i = x;i <= n;i += (( i ^ (i - 1)) & i)) + aib[i]++; +} + +void Solve() +{ + int res = 0; + for(int i = 1;i <= n;++i) + { + update(v[i].second); + res = (res + query(n) - query(v[i].second)) % 9917; + } + g< +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + + +const int MOD = 9917; +const int INF = INT_MAX; +int t, n, swaps, bit[100005]; + + + +void update(int i){ + + while (i <= n){ + + ++bit[i]; + + i += (-i & i); + + } + +} + + + +int query(int i){ + + int ret = 0; + + while (i > 0){ + + ret += bit[i]; + + i -= (-i & i); + + } + + return ret % MOD; + +} + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + + cin >> n; + + vector > a(n + 1); + + for (int i = 1; i <= n; i++){ + + cin >> a[i].first; + + a[i].second = i; + + } + + sort(1 + (a).begin(),(a).end()); + + for (int i = n; i > 0; i--){ + + update(a[i].second); + + swaps += query(a[i].second - 1); + + swaps %= MOD; + + } + cout << swaps; + return 0; +} diff --git a/invCount/aib/394458c6-137e-11eb-b0ed-309c234925a0.cpp b/invCount/aib/394458c6-137e-11eb-b0ed-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20467cce7bd1f5ded78398b6a665b595fdea0f52 --- /dev/null +++ b/invCount/aib/394458c6-137e-11eb-b0ed-309c234925a0.cpp @@ -0,0 +1,105 @@ +#include + + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int v[100001]; + +pair ss[100001]; + +int AIB[100001]; + +int n; + +void update(int i, int val) + +{ + + while(i<=n) + + { + + AIB[i]+=val; + + i+=(i&(-i)); + + } + +} + +int query(int i) + +{ + + int s=0; + + while(i>0) + + { + + s+=AIB[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + ios_base::sync_with_stdio(false); + + int i; + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>ss[i].first; + + ss[i].second=i; + + } + + sort(ss+1,ss+n+1); + + for(i=1;i<=n;i++) + + v[ss[i].second]=i; + + for(i=1;i<=n;i++) + + update(v[i],1); + + int ans=0; + + for(i=1;i<=n;i++) + + { + + ans=(ans+query(v[i])-1+9917)%9917; + + update(v[i],-1); + + } + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + + private: + + int a[5 + N]; + + + + int lsb(int i) { + + return (i & (-i)); + + } + + + + public: + + void Update(int pos) { + + for(int i = pos; i <= n; i += lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos; i >= 1; i -= lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b) { + + if(a.first == b.first) return a.second < b.second; + + return a.first < b.first; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++) { + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + sort(v + 1, v + n + 1); + + + + for(int i = n; i >= 1; i--) { + + inv += Fenw.Query(v[i].second); + + Fenw.Update(v[i].second); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/395f1552-b311-4616-9161-5bdac0c2b89e.cpp b/invCount/aib/395f1552-b311-4616-9161-5bdac0c2b89e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0019c51b1110b7ec9e893c181c32a3ff49d3cbb --- /dev/null +++ b/invCount/aib/395f1552-b311-4616-9161-5bdac0c2b89e.cpp @@ -0,0 +1,77 @@ +#include + + + +using namespace std; + +ifstream fin ("inv.in"); +ofstream fout ("inv.out"); + +int aib[100005], x, n, sol; + +struct nr +{ + int val, poz; +}; + +nr v[100005]; + +void update (int poz, int val) +{ + for (int i = poz; i <= n; i += (i&(-i))) + { + aib[i] += val; + } +} + +int suma (int poz) +{ + int s = 0; + for (int i = poz; i > 0; i -= (i&(-i))) + { + s = (s + aib[i]) % 9917; + } + + return s; +} + +bool cmp1 (nr a, nr b) +{ + if (a.val > b.val) return false; + + if (a.val == b.val && a.poz > b.poz) return false; + + return true; +} + +bool cmp2 (nr a, nr b) +{ + if (a.poz > b.poz) return false; + + return true; +} +int main() +{ + fin >> n; + + for (int i = 1; i <= n; i++) + { + fin >> v[i].val; + v[i].poz = i; + } + + sort(v + 1, v + n + 1, cmp1); + for (int i = 1; i <= n; i++) + v[i].val = i; + + sort(v + 1, v + n + 1, cmp2); + + for (int i = 1; i <= n; i++) + { + sol = (sol + suma(v[i].val)) % 9917; + update(v[i].val, 1); + } + + fout << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + return 0; +} diff --git a/invCount/aib/396686b0-137e-11eb-9a10-309c234925a0.cpp b/invCount/aib/396686b0-137e-11eb-9a10-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db3cf167c9c83c9208cc3aee4b7dcdee059beee9 --- /dev/null +++ b/invCount/aib/396686b0-137e-11eb-9a10-309c234925a0.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef pair pii; + + + +const int N = 100010; +int n, bit[N], ans; +pii a[N]; + +void update(int idx){ + for (;idx <= n; idx += (idx & -idx)) + bit[idx]++; +} + +int query(int idx){ + int rs = 0; + for(;idx;idx -= (idx & -idx)) + rs += bit[idx]; + return rs; +} + +int main(){ + freopen("inv.in", "r", stdin); + freopen("inv.out", "w", stdout); + scanf("%d", &n); + for (int i=1; i<=n; i++) scanf("%d", &a[i].first), a[i].second = i; + sort(a+1, a+1+n); + for(int i=1; i<=n; i++){ + update(a[i].second); + ans = (ans + query(n) - query(a[i].second))%9917; + } + printf("%d", ans); + return 0; +} diff --git a/invCount/aib/399585fa-137e-11eb-8db2-309c234925a0.cpp b/invCount/aib/399585fa-137e-11eb-8db2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7afb5c945211663b59a5f9dba0bc1423df2c07a6 --- /dev/null +++ b/invCount/aib/399585fa-137e-11eb-8db2-309c234925a0.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int mod=9917; + +const int nmax=100005; + + + +int n,aib[nmax]; + + + +struct arr + +{ + + int val,poz; + +}v[nmax]; + + + +bool cmp(arr a,arr b) + +{ + + return (a.val>b.val || (a.val==b.val && a.poz>b.poz)); + +} + + + +void read() + +{ + + fin>>n; + + + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,cmp); + +} + + + +void update(int poz) + +{ + + for(;poz<=n;poz+= poz & -poz) + + aib[poz]++; + +} + + + +int query(int poz) + +{ + + int ans=0; + + + + for(;poz;poz-= poz & -poz) + + ans+=aib[poz]; + + + + return ans; + +} + + + +void solve() + +{ + + int ans=0; + + + + for(int i=1;i<=n;i++) + + { + + ans=ans+query(v[i].poz-1); + + ans%=mod; + + + + update(v[i].poz); + + } + + + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + +private: + + int a[5 + N]; + + + + int lsb(int i){ + + return (i & (-i)); + + } + + + +public: + + void Update(int pos) { + + for(int i = pos; i >= 1; i -= lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos + 1; i <= n; i += lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b){ + + return a.second < b.second; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++){ + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + + + sort(v + 1, v + n + 1); + + for(int i = 1; i <= n; i++) { + + int j = i; + + while(v[j].first == v[j + 1].first && j <= n) j++; + + v[i].first = v[i - 1].first + 1; + + i++; + + + + while(i <= j) v[i].first = v[i - 1].first, i++; + + i--; + + } + + + + sort(v + 1, v + n + 1, cmp); + + + + for(int i = 1; i <= n; i++){ + + inv += Fenw.Query(v[i].first); + + Fenw.Update(v[i].first); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/39d7ec18-137e-11eb-b395-309c234925a0.cpp b/invCount/aib/39d7ec18-137e-11eb-b395-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d19d524cadb2537f66dda5320f861862907cf146 --- /dev/null +++ b/invCount/aib/39d7ec18-137e-11eb-b395-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include + + + + +using namespace std; + +typedef long long ll; + + + +const int MOD = 9917; + + + +int aib[100005],n; + + + +int sum(int x){ + + int s = 0; + + for(int i = x;i > 0;i -= i&(-i)) + + s += aib[i]; + + return s; + +} + + + +void update(int x){ + + for(int i = x;i <= n;i += i&(-i)) + + aib[i]++; + +} + +vector > v; + +int main() + +{ + + int i; + + ll rez = 0; + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin >> n; + + for(i = 1;i <= n;i++){ + + int x; + + cin >> x; + + v.push_back({x,i}); + + } + + sort(v.begin(),v.end()); + + i = 1; + + for(auto x : v){ + + rez += 1LL * (i - 1 - sum(x.second - 1)); + + rez %= MOD; + + update(x.second); + + i++; + + } + + cout << rez; + + return 0; + +} diff --git a/invCount/aib/39e77c80-137e-11eb-94eb-309c234925a0.cpp b/invCount/aib/39e77c80-137e-11eb-94eb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93e0254a42f46fd3740cd38a83f3d933a613fd9a --- /dev/null +++ b/invCount/aib/39e77c80-137e-11eb-94eb-309c234925a0.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int MOD = 9917; + +const int N = 100000; + + + +int aib[N + 1]; + +struct nr{ + + int val, index; + +} v[N + 1]; + + + +int index[N + 1]; + + + +bool cmp(nr a, nr b){ + + if(a.val > b.val) + + return true; + + else if(a.val == b.val && a.index > b.index) + + return true; + + return false; + +} + + + +int sum(int x){ + + int ans = 0; + + while(x){ + + ans += aib[x]; + + x -= (x & (-x)); + + } + + return ans; + +} + + + +int n; + + + +void add(int x, int val){ + + while(x <= n){ + + aib[x] += val; + + x += (x & (-x)); + + } + +} + + + +int main() + +{ + + int i,ans; + + fin >> n; + + for(i=1; i <= n; i++){ + + fin >> v[i].val; + + v[i].index = i; + + } + + sort(v + 1, v + n + 1, cmp); + + ans = 0; + + for(i=1; i<=n; i++){ + + ans = (ans + sum(v[i].index - 1)) % MOD; + + add(v[i].index, 1); + + } + + fout << ans << "\n"; + + return 0; + +} diff --git a/invCount/aib/3a3b8a22-137e-11eb-ba67-309c234925a0.cpp b/invCount/aib/3a3b8a22-137e-11eb-ba67-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0af88a60d227af5e132697075c830778b38cc30 --- /dev/null +++ b/invCount/aib/3a3b8a22-137e-11eb-ba67-309c234925a0.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int vn[100001],aint[300001]; + +struct vec + +{ + + int nr, poz; + +}v[100001]; + +bool cmp(vec x, vec y) + +{ + + if(x.nr==y.nr) + + return x.pozmid) + + query(2*nod+1,mid+1,dr,x,y); + +} + +void update(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]++; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=mid) + + update(2*nod,st,mid); + + else + + update(2*nod+1,mid+1,dr); + + aint[nod]=(aint[2*nod]+aint[2*nod+1])%9917; + +} + +int main () + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i].nr,v[i].poz=i; + + normalizare(); + + for(int i=1;i<=n;i++) + + { + + poz=vn[i]; + + query(1,1,k,poz+1,k); + + update(1,1,k); + + } + + fout< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + +pair a[100005]; + + + +inline void update(int pos) + +{ + + for (; pos <= n; pos += (pos & -pos)) + + aib[pos]++; + +} + + + +inline int Querry(int pos) + +{ + + int s = 0; + + for (; pos; pos -= (pos & -pos)) + + s += aib[pos], s %= 9917; + + return s; + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i].first, a[i].second = i; + + sort(a + 1, a + n + 1); + + int ans = 0; + + for (int i = n; i; i--) + + { + + ans += Querry(a[i].second); + + update(a[i].second); + + } + + fout << ans % 9917; + + return 0; + +} diff --git a/invCount/aib/3a827922-137e-11eb-9bb2-309c234925a0.cpp b/invCount/aib/3a827922-137e-11eb-9bb2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..140f7fcc467bd657953df1f33fc67e386b6728f3 --- /dev/null +++ b/invCount/aib/3a827922-137e-11eb-9bb2-309c234925a0.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; +ifstream cin("inv.in"); +ofstream cout("inv.out"); +bool ms(pair a,pair b){ + return a.second=1;a-=(a&(-a))){ + sol+=aib[a]; + } + return sol; +} +pair v[100005]; +int main() +{ + int a,sum=0; + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i].first; + v[i].second=i; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++) + v[i].first=i; + sort(v+1,v+n+1,ms); + for(int i=1;i<=n;i++){ + a=v[i].first; + sum+=i-que(a)-1; + sum%=9917; + upd(a); + + } + cout< + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +long long n, v[100005], aib[100005]; + +pair vRead[100005]; + + + +long long sum(int x) + +{ + + int s = 0; + + while (x) { + + s += aib[x]; + + x -= x & (-x); + + } + + return s; + +} + + + +void add(int x, int val) + +{ + + while (x <= n) { + + aib[x] = (aib[x] + val) % 9917; + + x += x & (-x); + + } + +} + + + +void normalise() + +{ + + long long x; + + for (int i = 1; i <= n; ++i) { + + f >> x; + + vRead[i] = {x, i}; + + } + + sort (vRead + 1, vRead + n + 1); + + for (int i = 1; i <= n; ++i) { + + v[i] = vRead[i].second; + + } + +} + + + +int main() + +{ + + f >> n; + + + + normalise(); + + + + + + long long noOfInversions = 0; + + for (int i = n; i >= 1; --i) { + + noOfInversions = (noOfInversions + sum(v[i])) % 9917; + + add(v[i], 1); + + } + + g << noOfInversions; + + return 0; + +} diff --git a/invCount/aib/3acbc4ac-137e-11eb-819d-309c234925a0.cpp b/invCount/aib/3acbc4ac-137e-11eb-819d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ed8afa0e8a066cdb3488897951b0e3fea145d2e --- /dev/null +++ b/invCount/aib/3acbc4ac-137e-11eb-819d-309c234925a0.cpp @@ -0,0 +1,183 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + +const int MOD = 9917; + + + +struct nume{ + + int node,ind; + +}; + + + +vector vec; + + + +int V[DMAX]; + +int tree[4*DMAX]; + + + +int n,cate; + +ll ans; + + + +inline bool operator<(nume x,nume y){ + + return x.node>n; + + for(i=1;i<=n;i++){ + + fin>>V[i]; + + vec.push_back({V[i],i}); + + } + + sort(vec.begin(),vec.end()); + + int prec=-1e9; + + for(auto& it:vec){ + + if(it.node == prec){ + + V[it.ind]=dif; + + continue; + + } + + dif++; + + prec=it.node; + + V[it.ind]=dif; + + } + + for(i=n;i>=1;i--){ + + if(V[i] > 1) + + ans+=query(1,1,n,V[i]-1); + + update(1,1,n,V[i]); + + } + + fout<= pos) + + ans+=query(2*node,st,mij,pos); + + return ans; + +} diff --git a/invCount/aib/3b0dd99c-137e-11eb-a6a5-309c234925a0.cpp b/invCount/aib/3b0dd99c-137e-11eb-a6a5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6876dcdb6da74b376a7f0d970eb0f0843c6e637c --- /dev/null +++ b/invCount/aib/3b0dd99c-137e-11eb-a6a5-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + +pair a[100005]; + + + +inline void update(int pos) + +{ + + for (; pos <= n; pos += (pos & -pos)) + + aib[pos]++; + +} + + + +inline int Querry(int pos) + +{ + + int s = 0; + + for (; pos; pos -= (pos & -pos)) + + s += aib[pos], s %= 9917; + + return s; + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i].first, a[i].second = i; + + sort(a + 1, a + n + 1); + + int ans = 0; + + for (int i = n; i; i--) + + { + + ans += Querry(a[i].second); + + update(a[i].second); + + } + + fout << ans % 9917; + + return 0; + +} diff --git a/invCount/aib/3b1ddf2c-137e-11eb-b884-309c234925a0.cpp b/invCount/aib/3b1ddf2c-137e-11eb-b884-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..537013d2e420b2846a2374f47bd1d89da1f9670d --- /dev/null +++ b/invCount/aib/3b1ddf2c-137e-11eb-b884-309c234925a0.cpp @@ -0,0 +1,112 @@ +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long n, q, x; + +unsigned long long r; + +pair v[100005]; + +long long stree[400050]; + + + +long long query(long long x, long long y, long long st, long long dr, long long node) + +{ + + if(y=dr) return stree[node]; + + long long mid = (st+dr)/2, s1=0, s2=0; + + if(x<=mid) s1 = query(x, y, st, mid, node*2); + + if(y>mid) s2 = query(x, y, mid+1, dr, node*2+1); + + return s1+s2; + +} + + + + + +void update(long long pos, long long st, long long dr, long long node) + +{ + + if(st==dr) + + stree[node]=1; + + else{ + + long long mid = (st+dr)/2; + + if(pos<=mid) update(pos, st, mid, node*2); + + else update(pos, mid+1, dr, node*2+1); + + stree[node] = stree[node*2]+stree[node*2+1]; + + } + +} + + + +int main() + +{ + + fin>>n; + + r = 0; + + for(long long i=1; i<=n; i++) + + { + + fin>>v[i].first; + + v[i].second = i; + + } + + sort(v+1, v+n+1); + + + + for(long long i=1; i<=n; i++) + + { + + r+=query(v[i].second+1, n, 1, n, 1); + + r=r%9917; + + + + update(v[i].second, 1, n, 1); + + } + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + + +string name = "inv"; + + +ifstream fin(name + ".in"); +ofstream fout(name + ".out"); + + + + +int n; +vector a; + +int f[100005]; + +inline int nrz(int x) { + return x & -x; +} + +int query(int x) { + int s = 0; + + while (x > 0) { + s += f[x]; + x -= nrz(x); + } + + return s; +} + +int query(int a, int b) { + return query(b) - query(a - 1); +} + +void update(int x) { + while (x <= n) { + f[x]++; + x += nrz(x); + } +} + +struct Item { + int v; + int i; +}; + +int main() { + + fin >> n; + + auto compare = [](Item lhs, Item rhs) { + return lhs.v > rhs.v; + }; + + priority_queue, decltype(compare)> pq(compare); + + for (int i = 0; i > x; + a.push_back(x); + pq.push({x, i}); + } + + int v = 0; + int last = -0x3f3f3f3f; + + while (!pq.empty()) { + auto x = pq.top(); + pq.pop(); + + if (x.v != last) { + last = x.v; + v++; + } + + a[x.i] = v; + } + + int result = 0; + + for (int i = 0; i < n; ++i) { + int x = a[i]; + + if (x < n) { + result += query(x + 1, n); + result %= 9917; + } + + update(x); + } + + fout << result; + + return 0; +} diff --git a/invCount/aib/3b750be8-137e-11eb-8eee-309c234925a0.cpp b/invCount/aib/3b750be8-137e-11eb-8eee-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df47bba76a132249128f31250695f97f92a0e7bf --- /dev/null +++ b/invCount/aib/3b750be8-137e-11eb-8eee-309c234925a0.cpp @@ -0,0 +1,136 @@ +#include + + + + +using namespace std; + + + +void solve() { + + int n; + + cin >> n; + + + + vector< int > v(n); + + + + for(int i = 0; i < n; ++i) cin >> v[i]; + + + + + + vector< int > actual = v; + + sort(actual.begin(), actual.end()); + + + + actual.erase(unique(actual.begin(), actual.end()), actual.end()); + + + + for(auto &x : v) { + + int l = 0, r = actual.size() - 1; + + int idx = 0; + + while(l <= r) { + + int m = l + r >> 1; + + if(actual[m] == x) { + + idx = m; + + break; + + } + + if(actual[m] < x) { + + l = m + 1; + + } else r = m - 1; + + } + + + + + + x = idx + 1; + + } + + + + vector< int > aib(actual.size() + 1); + + + + auto update = [&](int p) { + + for(; p < aib.size(); p += p&-p) ++aib[p]; + + }; + + + + auto query = [&](int p) { + + int ret = 0; + + for(;p;p-=p&-p) ret += aib[p]; + + return ret; + + }; + + + + long long ans = 0; + + + + for(int i = 0; i < n; ++i) { + + ans += i - query(v[i]); + + update(v[i]); + + } + + + + cout << ans % 9917<< '\n'; + +} + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + ios::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + + + + + solve(); + +} diff --git a/invCount/aib/3ba52fd2-137e-11eb-8c5c-309c234925a0.cpp b/invCount/aib/3ba52fd2-137e-11eb-8c5c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89c1f2afda83418ed8651f4d59c61fef0dc2c7cb --- /dev/null +++ b/invCount/aib/3ba52fd2-137e-11eb-8c5c-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + +class FenTree { + private: + int n; + vector bit; + + public: + FenTree(int n) : + n(n), bit(n + 1) { } + + void update(int pos, int val) { + for (int i = pos; i <= n; i += i & -i) + bit[i] += val; + } + + int query(int pos) { + int sum = 0; + for (int i = pos; i >= 1; i -= i & -i) + sum += bit[i]; + return sum; + } +}; + +int main() { + int n; fin >> n; + vector> val(n); + + for (int i = 0; i < n; i++) { + fin >> val[i].first; + val[i].second = i; + } + sort(val.begin(), val.end()); + + int cnt = 0; + vector v(n); + for (int i = 0; i < n; i++) { + if (!i || val[i - 1].first != val[i].first) + cnt++; + v[val[i].second] = cnt; + } + + int sol = 0; + const int MOD = 9917; + + FenTree bit(cnt); + for (int i = n - 1; i >= 0; i--) { + sol = (sol + bit.query(v[i] - 1)) % MOD; + bit.update(v[i], +1); + } + fout << sol << '\n'; + + fout.close(); + return 0; +} diff --git a/invCount/aib/3bc30bf7-75fc-4e9e-aa2b-075bd33b1d00.cpp b/invCount/aib/3bc30bf7-75fc-4e9e-aa2b-075bd33b1d00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89c1f2afda83418ed8651f4d59c61fef0dc2c7cb --- /dev/null +++ b/invCount/aib/3bc30bf7-75fc-4e9e-aa2b-075bd33b1d00.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + +class FenTree { + private: + int n; + vector bit; + + public: + FenTree(int n) : + n(n), bit(n + 1) { } + + void update(int pos, int val) { + for (int i = pos; i <= n; i += i & -i) + bit[i] += val; + } + + int query(int pos) { + int sum = 0; + for (int i = pos; i >= 1; i -= i & -i) + sum += bit[i]; + return sum; + } +}; + +int main() { + int n; fin >> n; + vector> val(n); + + for (int i = 0; i < n; i++) { + fin >> val[i].first; + val[i].second = i; + } + sort(val.begin(), val.end()); + + int cnt = 0; + vector v(n); + for (int i = 0; i < n; i++) { + if (!i || val[i - 1].first != val[i].first) + cnt++; + v[val[i].second] = cnt; + } + + int sol = 0; + const int MOD = 9917; + + FenTree bit(cnt); + for (int i = n - 1; i >= 0; i--) { + sol = (sol + bit.query(v[i] - 1)) % MOD; + bit.update(v[i], +1); + } + fout << sol << '\n'; + + fout.close(); + return 0; +} diff --git a/invCount/aib/3bd61620-244b-4bbf-82f4-94657676f3bc.cpp b/invCount/aib/3bd61620-244b-4bbf-82f4-94657676f3bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59a0e20adbb77806cddc005af3bc32a78cd67bf --- /dev/null +++ b/invCount/aib/3bd61620-244b-4bbf-82f4-94657676f3bc.cpp @@ -0,0 +1,194 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/3be32f68-137e-11eb-b951-309c234925a0.cpp b/invCount/aib/3be32f68-137e-11eb-b951-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d01efb1e666867590cb00fab8b09f5cf2e1b842e --- /dev/null +++ b/invCount/aib/3be32f68-137e-11eb-b951-309c234925a0.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3c6c2a88-137e-11eb-910e-309c234925a0.cpp b/invCount/aib/3c6c2a88-137e-11eb-910e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb9763a5200ff483b2265c822313004d84b20ef0 --- /dev/null +++ b/invCount/aib/3c6c2a88-137e-11eb-910e-309c234925a0.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int MOD = 9917; + + + +int tree[100001]; + +pair v[100001]; + +int n; + + + +void update(int poz) + +{ + + for(int i = poz; i <= n; i += i&-i) + + tree[i]++; + +} + + + +int64_t query(int poz) + +{ + + int s = 0; + + + + for(int i = poz; i; i -= i&-i) + + s += tree[i]%MOD; + + + + return s%MOD; + +} + + + +int main() + +{ + + in >> n; + + + + for(int i = 1; i <= n; i++) + + { + + in >> v[i].first; + + v[i].second = i; + + } + + + + sort(v+1, v+n+1); + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << v[i].first << ' ' << v[i].second << '\n'; + + } + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + + + + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + cout << query(2) << ' ' << query(4); + + } + + + + int64_t rez = 0; + + for(int i = n; i; i--) + + { + + if(!1) + + { + + if(query(v[i].second)) + + cout << query(v[i].second) << ' ' << v[i].first << ' ' << v[i].second << '\n'; + + } + + rez += query(v[i].second)%MOD; + + update(v[i].second); + + + + } + + out << rez%MOD; + + + + return 0; + +} diff --git a/invCount/aib/3c8a8998-137e-11eb-b55b-309c234925a0.cpp b/invCount/aib/3c8a8998-137e-11eb-b55b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb9763a5200ff483b2265c822313004d84b20ef0 --- /dev/null +++ b/invCount/aib/3c8a8998-137e-11eb-b55b-309c234925a0.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i +#include +#include + +using namespace std; +ifstream fin("inv.in"); +ofstream fout("inv.out"); +struct numar { + int poz, val; +}; +int n; +numar v[100001]; +int a[100005]; +void citire() { + fin >> n; + int va; + for (int i = 0; i < n; ++i) { + fin >> va; + v[i].val = va; + v[i].poz = i + 1; + } +} +bool comp(numar a, numar b) { + if (a.val == b.val) { + return a.poz > b.poz; + } + return a.val > b.val; +} +void update(int poz, int val) { + for (int i = poz; i <= n; i += i & -i) { + a[i] = (a[i] + val) % 9917; + } +} +long query(int poz) { + long s = 0; + for (int i = poz; i >= 1; i -= i & -i) { + s += a[i] % 9917; + } + return s % 9917; +} +int main() +{ + long inv = 0; + citire(); + sort(v, v + n, comp); + for (int i = 0; i < n; ++i) { + inv = (inv + query(v[i].poz)) % 9917; + update(v[i].poz, 1); + } + fout << inv << "\n"; + return 0; +} diff --git a/invCount/aib/3cdca2a4-137e-11eb-a26f-309c234925a0.cpp b/invCount/aib/3cdca2a4-137e-11eb-a26f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c79dd9ce0712388da99eefd77a776c7268af071 --- /dev/null +++ b/invCount/aib/3cdca2a4-137e-11eb-a26f-309c234925a0.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n,i,x,aib[100005],v[100005],a[100005],b[100005]; + +long long s; + +int ub(int x) + +{ + + return x & (-x); + +} + +void update(int x) + +{ + + for(int i=x;i<=n;i+=ub(i)) + + aib[i]++; + +} + +int query(int x) + +{ + + long long s=0; + + for(int i=x;i>=1;i-=ub(i)) + + s+=aib[i]; + + return s; + +} + +void normalizare() + +{ + + for(int i=1;i<=n;i++) + + b[i]=a[i]; + + sort(b+1,b+n+1); + + for(int i=1;i<=n;i++) + + { + + int st=1,p=0; + + int dr=n; + + int x=a[i]; + + while(st<=dr) + + { + + int mij=(st+dr)/2; + + if(x<=b[mij]) + + { + + p=mij; + + dr=mij-1; + + } + + else st=mij+1; + + } + + v[i]=p; + + } + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + f>>a[i]; + + normalizare(); + + for(i=n;i>=1;i--) + + { + + x=v[i]; + + update(x); + + s+=query(x-1); + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector < pair > v; + + + +int n, aib[100001]; + +const int MOD = 9917; + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + { + + aib[i] += val; + + if (aib[i] > MOD) aib[i] -= MOD; + + } + +} + + + +int64_t Query(int p) + +{ + + int64_t suma = 0; + + + + for (int i = p; i >= 1; i -= i & -i) + + { + + suma += aib[i]; + + if (suma > MOD) suma -= MOD; + + } + + return suma; + +} + + + +int main() + +{ + + int a; + + fin >> n; + + + + v.resize(n); + + + + for (int i = 0; i < n; ++i) + + { + + fin >> a; + + v[i] = {a, i + 1}; + + } + + sort(v.begin(), v.end(), greater > ()); + + + + int64_t cnt = 0; + + + + for (int i = 0; i < n; ++i) + + { + + cnt += Query(v[i].second); + + if (cnt > MOD) cnt -= MOD; + + Update(v[i].second, 1); + + } + + + + fout << cnt << "\n"; + + + + return 0; + +} diff --git a/invCount/aib/3cf9dcb6-137e-11eb-a4a6-309c234925a0.cpp b/invCount/aib/3cf9dcb6-137e-11eb-a4a6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5072c18912b458803b49505611cb354d65e45e59 --- /dev/null +++ b/invCount/aib/3cf9dcb6-137e-11eb-a4a6-309c234925a0.cpp @@ -0,0 +1,196 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/3d09bbb8-137e-11eb-b546-309c234925a0.cpp b/invCount/aib/3d09bbb8-137e-11eb-b546-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c3ac6b7db3479750d1cb8e38b2f69d38ef53c8 --- /dev/null +++ b/invCount/aib/3d09bbb8-137e-11eb-b546-309c234925a0.cpp @@ -0,0 +1,199 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/3d499376-137e-11eb-9ac0-309c234925a0.cpp b/invCount/aib/3d499376-137e-11eb-9ac0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41378807eb2aa0f94c3ea1d6483c97afa2c8ae83 --- /dev/null +++ b/invCount/aib/3d499376-137e-11eb-9ac0-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + +using namespace std; + + + +int aib[100005], n, k; + + + +const int mod = 9917; + + + +struct Element + +{ + + int val, pos, new_val; + +} v[100005]; + + + +bool ByValue (Element a, Element b) + +{ + + return a.val < b.val; + +} + + + +bool ByIndex (Element a, Element b) + +{ + + return a.pos < b.pos; + +} + + + +void Read_Normalize () + +{ + + ifstream fin ("inv.in"); + + fin >> n; + + int i, x; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + v[i].val = x; + + v[i].pos = i; + + } + + sort (v + 1, v + n + 1, ByValue); + + v[1].new_val = 1; + + k = 1; + + for (i = 2; i <= n; i++) + + if (v[i].val == v[i - 1].val) + + v[i].new_val = k; + + else k++, v[i].new_val = k; + + sort (v + 1, v + n + 1, ByIndex); + +} + + + + + +void Update (int p, int x) + +{ + + for (; p <= n; p += (p & - p)) + + aib[p] += x; + +} + + + +int Sum (int p) + +{ + + int s; + + for (s = 0; p > 0; p -= (p & - p)) + + s += aib[p]; + + return s; + +} + + + +void Solve () + +{ + + int ans = 0, i; + + Update(v[n].new_val, 1); + + for (i = n - 1; i >= 1; i--) + + { + + ans = (ans + Sum(v[i].new_val - 1)) % mod; + + Update(v[i].new_val, 1); + + } + + ofstream fout ("inv.out"); + + fout << ans << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read_Normalize(); + + Solve(); + + return 0; + +} diff --git a/invCount/aib/3d58879e-137e-11eb-9655-309c234925a0.cpp b/invCount/aib/3d58879e-137e-11eb-9655-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1d5281f34c1ab5c56319cc38d8caba51fff312 --- /dev/null +++ b/invCount/aib/3d58879e-137e-11eb-9655-309c234925a0.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3d6754ac-137e-11eb-972b-309c234925a0.cpp b/invCount/aib/3d6754ac-137e-11eb-972b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d01efb1e666867590cb00fab8b09f5cf2e1b842e --- /dev/null +++ b/invCount/aib/3d6754ac-137e-11eb-972b-309c234925a0.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3d9bf934-137e-11eb-88fb-309c234925a0.cpp b/invCount/aib/3d9bf934-137e-11eb-88fb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf071b88c7856244f0fd2a0df2912a7c8e9aa9ff --- /dev/null +++ b/invCount/aib/3d9bf934-137e-11eb-88fb-309c234925a0.cpp @@ -0,0 +1,160 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool CMP1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool CMP2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, CMP1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, CMP2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3dac25e8-137e-11eb-a8a9-309c234925a0.cpp b/invCount/aib/3dac25e8-137e-11eb-a8a9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1d5281f34c1ab5c56319cc38d8caba51fff312 --- /dev/null +++ b/invCount/aib/3dac25e8-137e-11eb-a8a9-309c234925a0.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3dbc0458-137e-11eb-b40c-309c234925a0.cpp b/invCount/aib/3dbc0458-137e-11eb-b40c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59a0e20adbb77806cddc005af3bc32a78cd67bf --- /dev/null +++ b/invCount/aib/3dbc0458-137e-11eb-b40c-309c234925a0.cpp @@ -0,0 +1,194 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/3dca9b7e-137e-11eb-9e3b-309c234925a0.cpp b/invCount/aib/3dca9b7e-137e-11eb-9e3b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20c1528a71b747b997054956277f3446346bf0a0 --- /dev/null +++ b/invCount/aib/3dca9b7e-137e-11eb-9e3b-309c234925a0.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + return 0; + +} diff --git a/invCount/aib/3dd94454-137e-11eb-b6e8-309c234925a0.cpp b/invCount/aib/3dd94454-137e-11eb-b6e8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59a0e20adbb77806cddc005af3bc32a78cd67bf --- /dev/null +++ b/invCount/aib/3dd94454-137e-11eb-b6e8-309c234925a0.cpp @@ -0,0 +1,194 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/3de9e63a-137e-11eb-a1ea-309c234925a0.cpp b/invCount/aib/3de9e63a-137e-11eb-a1ea-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/3de9e63a-137e-11eb-a1ea-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3df8c0a8-137e-11eb-b4ab-309c234925a0.cpp b/invCount/aib/3df8c0a8-137e-11eb-b4ab-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c3ac6b7db3479750d1cb8e38b2f69d38ef53c8 --- /dev/null +++ b/invCount/aib/3df8c0a8-137e-11eb-b4ab-309c234925a0.cpp @@ -0,0 +1,199 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/3e0852a8-137e-11eb-bc6c-309c234925a0.cpp b/invCount/aib/3e0852a8-137e-11eb-bc6c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/3e0852a8-137e-11eb-bc6c-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3e3c25d8-137e-11eb-a1de-309c234925a0.cpp b/invCount/aib/3e3c25d8-137e-11eb-a1de-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80bf9a939857a5a24c7002545146a01a5f0caaf1 --- /dev/null +++ b/invCount/aib/3e3c25d8-137e-11eb-a1de-309c234925a0.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +const int mod = 9917; + +int aib[5 + 100000]; + +int n; + +vector> v; + + + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] = (aib[i] + val) % mod; + +} + + + +int Query(int p) + +{ + + int suma = 0; + + for (int i = p; i >= 1; i -= i & -i) + + suma = (suma + aib[i]) % mod; + + + + return suma; + +} + + + +int main() + +{ + + fin >> n; + + v = vector>(n); + + int val; + + for (int i = 0; i < n; ++i) + + { + + fin >> val; + + v[i] = {val, i + 1}; + + } + + + + sort(v.begin(), v.end(), greater>()); + + + + int res = 0; + + for (auto& p : v) + + { + + res += Query(p.second); + + if (res >= mod) + + res -= mod; + + Update(p.second, 1); + + } + + + + fout << res; + + + + return 0; + +} diff --git a/invCount/aib/3e4ab4e4-137e-11eb-aad8-309c234925a0.cpp b/invCount/aib/3e4ab4e4-137e-11eb-aad8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e1e7f32272284b03afa01da03600caa656bc691 --- /dev/null +++ b/invCount/aib/3e4ab4e4-137e-11eb-aad8-309c234925a0.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +using PII = pair; + +using VP = vector; + +const int mod = 9917; + +int aib[110011], s, n, x; + + + +inline void Update(int poz) + +{ + + for (int i = poz; i <= n; i += i & -i) + + ++aib[i]; + +} + + + +inline int Query(int poz) + +{ + + int sum = 0; + + for (int i = poz; i > 0; i -= i & -i) + + sum += aib[i]; + + return sum; + +} + + + +int main() + +{ + + fin >> n; + + VP v(n + 1); + + for (int i = 1; i <= n; ++i) + + { + + fin >> x; + + v[i] = {x, i}; + + } + + sort(v.begin() + 1, v.end(), greater()); + + for (int i = 1; i <= n; ++i) + + { + + s = (s + Query(v[i].second)) % mod; + + Update(v[i].second); + + } + + fout << s; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/3e78bb0a-137e-11eb-a04e-309c234925a0.cpp b/invCount/aib/3e78bb0a-137e-11eb-a04e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73db369a0007edf5a3fd6ad132ec80dcb75660de --- /dev/null +++ b/invCount/aib/3e78bb0a-137e-11eb-a04e-309c234925a0.cpp @@ -0,0 +1,65 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + + + +static const int MOD = 9917; + +static const int NMAX = 1e5+5; + +int aib[NMAX]; +int ind[NMAX]; +int v[NMAX]; +int n; + +int query(int position) +{ + int sum = 0; + for(int i = position; i>= 1; i-=(i&(-i))) + sum+=aib[i]; + return sum; +} + +void update(int position) +{ + for(int i = position; i<=n; i+=(i&(-i))) + aib[i]++; +} + +bool cmp(const int &a, const int &b) +{ + return v[a] < v[b]; +} + +int main() +{ + fin >> n; + + for(int i =1; i<=n; ++i) + { + fin >> v[i]; + ind[i] = i; + } + stable_sort(ind+1,ind+n+1,cmp); + + int sol = 0; + for(int i = 1; i<=n; ++i) + { + int pos = ind[i]; + + sol = (sol + pos - query(pos) -1) % MOD; + update(pos); + } + + fout << sol; + + + return 0; +} diff --git a/invCount/aib/3e887c42-137e-11eb-88c4-309c234925a0.cpp b/invCount/aib/3e887c42-137e-11eb-88c4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ed0da6e19a835e1267a185fd470989ef03b5bd5 --- /dev/null +++ b/invCount/aib/3e887c42-137e-11eb-88c4-309c234925a0.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i, nrInv = 0; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv = (nrInv + x - sum(x) - 1) % MOD; + + add(x); + + } + + g << nrInv; + + return 0; + +} diff --git a/invCount/aib/3e98f70a-137e-11eb-b1c2-309c234925a0.cpp b/invCount/aib/3e98f70a-137e-11eb-b1c2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f778ddab2d48cc27b572165735bebe796ca92469 --- /dev/null +++ b/invCount/aib/3e98f70a-137e-11eb-b1c2-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int NMAX = 100000, + + MOD = 9917; + + + +struct elem { + + int poz, val; + +}; + +int N, AIB[NMAX+1]; + +elem V[NMAX+1]; + + + +bool comp(const elem& a, const elem& b) { + + if(a.val == b.val) return a.poz < b.poz; + + else return a.val < b.val; + +} + + + +void update(int p) { + + while(p <= N) { + + AIB[p]++; + + p += p & (-p); + + } + +} + + + +int query(int p) { + + int s = 0; + + while(p > 0) { + + s += AIB[p]; + + p -= p & (-p); + + } + + return s; + +} + + + +int main() + +{ + + int x, nrInv = 0; + + in >> N; + + for(int i = 1; i <= N; i++) { + + in >> x; + + V[i] = {i, x}; + + } + + sort(V+1, V+N+1, comp); + + + + for(int i = 1; i <= N; i++) { + + x = V[i].poz; + + nrInv = (nrInv + x - query(x) - 1) % MOD; + + update(x); + + } + + + + out << nrInv; + + return 0; + +} diff --git a/invCount/aib/3ea7da9c-137e-11eb-88ae-309c234925a0.cpp b/invCount/aib/3ea7da9c-137e-11eb-88ae-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..242ab97aa4be16b806793fe1fe0d77694c5f27e7 --- /dev/null +++ b/invCount/aib/3ea7da9c-137e-11eb-88ae-309c234925a0.cpp @@ -0,0 +1,133 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + long long nrInv = 1LL * N * (N - 1) / 2; + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv -= sum(x); + + add(x); + + } + + g << nrInv % MOD; + + return 0; + +} diff --git a/invCount/aib/3f2584ae-137e-11eb-b921-309c234925a0.cpp b/invCount/aib/3f2584ae-137e-11eb-b921-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f28e078f23cae8da6cee526df90b0688e135ad80 --- /dev/null +++ b/invCount/aib/3f2584ae-137e-11eb-b921-309c234925a0.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[100010],v2[100010], aib[100010],ans; + +unordered_map M; + +void update(int nr) + +{ + + for(int i = nr; i <= n; i += (i & (-i))) + + { + + aib[i] ++; + + } + + + +} + +int suma(int nod) + +{ + + int sum = 0; + + for(int i = nod; i; i -= (i & (-i)) ) + + { + + sum += aib[i]; + + sum %= 9917; + + } + + return sum; + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> v[i], v2[i] = v[i]; + + sort(v + 1, v + n + 1); + + int p = 0; + + v[0] = -1; + + for(int i = 1; i <= n; ++i) + + { + + if(v[i] != v[i-1]) + + p++; + + M[v[i]] = p; + + } + + for(int i = n; i >= 1; --i) + + { + + ans += suma(M[v2[i]] - 1); + + ans %= 9917; + + update(M[v2[i]]); + + } + + g << ans; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/3f5340ba-137e-11eb-bcc0-309c234925a0.cpp b/invCount/aib/3f5340ba-137e-11eb-bcc0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..212976711c9d996e79e958cff3461bea7d2b5400 --- /dev/null +++ b/invCount/aib/3f5340ba-137e-11eb-bcc0-309c234925a0.cpp @@ -0,0 +1,142 @@ +#include + + + + +using namespace std; + + + +int getSum(int BITree[], int index) + +{ + + int sum = 0; + + while (index > 0) + + { + + sum += BITree[index]; + + index -= index & (-index); + + } + + return sum; + +} + + + + + +void updateBIT(int BITree[], int n, int index, int val) + +{ + + while (index <= n) + + { + + BITree[index] += val; + + index += index & (-index); + + } + +} + + + +void convert(int arr[],int n) + +{ + + int lst[n]; + + for(int i=1;i<=n;i++) + + lst[i]=arr[i]; + + sort(lst+1,lst+n+1); + + for(int i=1;i<=n;i++) + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + +} + + + +int getInvCount(int arr[], int n) + +{ + + int invcount = 0; + + convert(arr,n); + + int maxElement = 0; + + for (int i=1; i<=n; i++) + + if (maxElement < arr[i]) + + maxElement = arr[i]; + + + + int BIT[maxElement+1]; + + for (int i=1; i<=maxElement; i++) + + BIT[i] = 0; + + + + for (int i=n; i>0; i--) + + { + + invcount += getSum(BIT, arr[i]-1); + + invcount%=9917; + + updateBIT(BIT, maxElement, arr[i], 1); + + } + + + + return invcount; + +} + + + +int main() + +{ + + int arr[100000],n; + + ifstream fin("inv.in"); + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>arr[i]; + + fin.close(); + + ofstream fout("inv.out"); + + fout< +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[200005], aib[200005], q, tip, a[200005], p; + +long long ans; + +unordered_mapmp; + +void update(int nod) + +{ + + for(int i=nod; i<=n; i+=(i&(-i))) + + aib[i]++; + +} + +int query(int nod) + +{ + + int sum=0; + + for(int i=nod; i; i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + f>>n; + + for(int i=1; i<=n; i++) + + { + + f>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1, a+n+1); + + for(int i=1; i<=n; i++) + + if(mp.find(a[i])==mp.end()) mp[a[i]]=++p; + + for(int i=n; i>=1; i--) + + { + + ans=(ans+query(mp[v[i]]-1))%9917; + + update(mp[v[i]]); + + } + + g< +#include + + +using namespace std; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +int n,i,m,a[100005],b[100005],v[100005],aib[100005]; + +int ub (int x) + +{ + + return(x&(-x)); + +} + + + +void add( int x ) + +{ + + for(int i=x; i<=n; i+=ub(i)) + + { + + aib[i]++; + + + + } + + + +} + +int sum (int x) + +{ + + int s=0; + + for(int i=x; i>=1; i-=ub(i)) + + { + + s=s+aib[i]; + + + + } + + return s; + +} +int main() + +{ + +in>>n; + + + +for(i=1;i<=n;i++) + +{in>>a[i]; + + b[i]=a[i]; + +} + +sort(b+1,b+n+1); + +m=0; + +v[++m]=b[1]; + +for(i=2;i<=n;i++) + +{if(b[i]!=b[i-1])v[++m]=b[i]; + + + +} +for(i=1;i<=n;i++){a[i]=lower_bound(v+1,v+m+1,a[i])-v; } +int nr=0; + +for(i=n;i>=1;i--) + +{nr=nr+sum(a[i]-1); + + + + nr=nr%9917; + + add(a[i]); + + + +} + + + + out< +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aib[100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + + + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p0){ + + s=(s+aib[x])%9917; + + x-=(x&(-x)); + + } + + return s; + +} + + + +void update(int x){ + + while(x<=k){ + + aib[x]++; + + if(aib[x]>=9917) + + aib[x]-=9917; + + x+=(x&(-x)); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + nr=query(k)-query(v[i]); + + if(nr<0) + + nr+=9917; + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + update(v[i]); + + } + + fout< +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long long i,n,m,a[100005],b[100005],v[100005],aib[100005],st,dr,poz,mij,nr; + +int ub(int x) + +{ + + return(x&(-x)); + +} + +long long sum(int x) + +{ + + long long s=0; + + for(int i=x; i>=1; i-=ub(i)) + + { + + s+=aib[i]; + + s%=9917; + + } + + return s; + +} + +void Add(int x) + +{ + + for(int i=x; i<=m; i+=ub(i)) + + { + + aib[i]++; + + aib[i]%=9917; + + } + +} + +int main() + +{ + + f>>n; + + for(i=1; i<=n; ++i) + + { + + f>>a[i]; + + b[i]=a[i]; + + } + + sort(b+1,b+n+1); + + v[++m]=b[1]; + + for(i=2; i<=n; ++i) + + if(b[i]!=b[i-1]) + + v[++m]=b[i]; + + for(i=1; i<=n; ++i) + + { + + st=1; + + dr=m; + + poz=0; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + if(v[mij]<=a[i]) + + { + + poz=mij; + + st=mij+1; + + } + + else + + dr=mij-1; + + } + + a[i]=poz; + + } + + for(i=n; i>=1; --i) + + { + + nr+=sum(a[i]-1); + + nr%=9917; + + Add(a[i]); + + } + + g< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX = 1e5 + 5, MOD = 9917; + + + +long long i, n, m, a[NMAX], b[NMAX], v[NMAX], aib[NMAX], st, dr, poz, mij, nr; + + + +int ub (int x) + +{ + + return (x & (-x)); + +} + + + +long long sum (int x) + +{ + + long long s = 0; + + + + for(int i = x; i >= 1; i -= ub(i)) + + { + + s += aib[i]; + + + + s %= MOD; + + } + + + + return s; + +} + +void Add (int x) + +{ + + for(int i = x; i <= m; i += ub(i)) + + { + + aib[i]++; + + + + aib[i] %= MOD; + + } + +} + + + +int main() + +{ + + f>>n; + + + + for(i=1; i<=n; i++) + + { + + f>>a[i]; + + + + b[i]=a[i]; + + } + + + + sort(b+1,b+n+1); + + + + v[++m]=b[1]; + + + + for(i=2; i<=n; i++) + + if(b[i]!=b[i-1]) + + v[++m]=b[i]; + + + + for(i=1; i<=n; i++) + + { + + st=1; + + dr=m; + + + + poz=0; + + + + while(st<=dr) + + { + + mij=(st+dr)/2; + + + + if(v[mij] <= a[i]) + + { + + poz=mij; + + st=mij+1; + + } + + else + + dr=mij-1; + + } + + a[i]=poz; + + } + + + + for(i = n; i >= 1; i--) + + { + + nr += sum(a[i] - 1); + + + + nr %= 9917; + + + + Add(a[i]); + + } + + + + g< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int t[100001],aib[300001]; + +int k,n,sol,a,b,poz,nr,MOD=9917; + +struct norm + +{ + + int poz,val; + +}v[100001]; + +int cmp(norm a, norm b) + +{ + + if(a.val==b.val) + + return a.poz0) + + { + + s=(s+aib[x])%MOD; + + x=(x-(x&(-x))); + + } + + return s; + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(int i=1;i<=n;i++) + + { + + if(v[i].val!=v[i-1].val) + + k++; + + t[v[i].poz]=k; + + } + + + + for(int i=1;i<=n;i++) + + { + + int val=query(k)-query(t[i]); + + if(val<0) + + val+=MOD; + + sol=(sol+val)%MOD; + + update(t[i]); + + } + + fout< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int t[100001],aib[300001]; + +int k,n,sol,a,b,poz,nr,MOD=9917; + +struct norm + +{ + + int poz,val; + +}v[100001]; + +int cmp(norm a, norm b) + +{ + + if(a.val==b.val) + + return a.poz0) + + { + + s=(s+aib[x])%MOD; + + x=(x-(x&(-x))); + + } + + return s; + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(int i=1;i<=n;i++) + + { + + if(v[i].val!=v[i-1].val) + + k++; + + t[v[i].poz]=k; + + } + + + + for(int i=1;i<=n;i++) + + { + + int val=query(k)-query(t[i]); + + if(val<0) + + val+=MOD; + + sol=(sol+val)%MOD; + + update(t[i]); + + } + + fout< +#include + + + + +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct numar { + + int id, poz; + +}; + + + +int n, i, x, sol, cnt; + +int A[100005], f[100005]; + + + +numar v[100005]; + + + +inline bool cmp (numar a, numar b){ + + return a.id < b.id; + +} + + + +inline void update (int i){ + + while (i <= n){ + + A[i]++; + + i += (i&-i); + + } + +} + + + +inline int query (int i){ + + int sol = 0; + + while (i){ + + sol += A[i]; + + i -= (i&-i); + + } + + return sol; + +} + + + +int main(){ + + fin >> n; + + for (i=1; i<=n; i++){ + + fin >> x; + + v[i] = {x, i}; + + } + + sort (v + 1, v + n + 1, cmp); + + f[v[1].poz] = 1; + + for (i=2; i<=n; i++){ + + if (v[i].id == v[i-1].id){ + + cnt++; + + f[v[i].poz] = f[v[i-1].poz]; + + } + + else{ + + f[v[i].poz] = i - cnt; + + } + + } + + cnt = n - cnt; + + for (i=1; i<=n; i++){ + + sol += (query(cnt) - query(f[i])); + + sol = sol%9917; + + update (f[i]); + + } + + fout << sol; + + return 0; + +} diff --git a/invCount/aib/403cc486-137e-11eb-88f4-309c234925a0.cpp b/invCount/aib/403cc486-137e-11eb-88f4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e934433746268405b289daa3ab2dbd0a9a16f44c --- /dev/null +++ b/invCount/aib/403cc486-137e-11eb-88f4-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aib[100001], sol; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int x){ + + while(x <= k){ + + aib[x] = (aib[x] + 1) % 9917; + + x = x + (x & (-x)); + + } + +} + +int quary(int x){ + + int s = 0; + + while(x >= 1){ + + s = (s + aib[x]) % 9917; + + x = x - (x & (-x)); + + } + + return s; + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + for(i = 1; i <= n; i++){ + + int value = quary(k) - quary(x[i]); + + if(value < 0) + + value += 9917; + + sol = (sol + value) % 9917; + + update(x[i]); + + } + + g << sol; + + return 0; + +} diff --git a/invCount/aib/404cca10-137e-11eb-9308-309c234925a0.cpp b/invCount/aib/404cca10-137e-11eb-9308-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..376f70ee35545a28bb7ea34a351214d889ca94f8 --- /dev/null +++ b/invCount/aib/404cca10-137e-11eb-9308-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int t[100001],aint[300001]; + +int k,n,sol,a,b,poz,nr,MOD=9917; + +struct norm + +{ + + int poz,val; + +}v[100001]; + +int cmp(norm a, norm b) + +{ + + if(a.val==b.val) + + return a.poz=dr) + + { + + nr+=aint[nod]; + + if(nr==MOD) + + nr=0; + + } + + else + + { + + int mid=(st+dr)/2; + + if(a<=mid) + + query(2*nod,st,mid); + + if(b>mid) + + query(2*nod+1,mid+1,dr); + + } + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(int i=1;i<=n;i++) + + { + + if(v[i].val!=v[i-1].val) + + k++; + + t[v[i].poz]=k; + + } + + for(int i=1;i<=n;i++) + + { + + a=t[i]+1;b=k;nr=0; + + query(1,1,k); + + sol=(sol+nr)%MOD; + + poz=t[i]; + + update(1,1,k); + + } + + fout< +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aint[300001], s, a, b, pos; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int node, int st, int dr){ + + if(st == dr){ + + aint[node] = (aint[node] + 1) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid < pos) + + update(2 * node + 1, mid + 1, dr); + + else + + update(2 * node, st, mid); + + aint[node] = (aint[node] + 1) % 9917; + +} + +void quary(int node, int st, int dr){ + + if(st >= a && dr <= b){ + + s = (s + aint[node]) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid + 1 <= b) + + quary(2 * node + 1, mid + 1, dr); + + if(mid >= a) + + quary(2 * node, st, mid); + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + b = k; + + for(i = 1; i <= n; i++){ + + if(x[i] + 1 <= n){ + + a = x[i] + 1; + + quary(1, 1, k); + + } + + pos = x[i]; + + update(1, 1, k); + + } + + g << s; + + return 0; + +} diff --git a/invCount/aib/4068db04-96e2-4796-b6cf-b8feb206fb02.cpp b/invCount/aib/4068db04-96e2-4796-b6cf-b8feb206fb02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4275deb47915ef1578ca023a5abae6d8d1cbb5a0 --- /dev/null +++ b/invCount/aib/4068db04-96e2-4796-b6cf-b8feb206fb02.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long long aib[100005],n; + +int bit (int i) + +{ + + return i^(i&(i-1)); + +} + +void update (int i,int val) + +{ + + int j; + + for (j=i;j<=n;j+=bit(j)) + + { + + aib[j]+=val; + + } + +} + +long long query (int i) + +{ + + int j;long long s=0; + + for (j=i;j>=1;j-=bit(j)) + + { + + s+=aib[j]; + + } + + return s; + +} + +long long v[100005],v1[100005]; + +struct wow + +{ + + long long nr,poz; + +}a[100005]; + +int compare (wow a,wow b) + +{ + + return a.nr>n; + + for (i=1;i<=n;i++) + + { + + f>>v[i]; + + a[i].nr=v[i]; + + a[i].poz=i; + + } + + sort (a+1,a+n+1,compare); + + for (i=1;i<=n;i++) + + { + + v1[a[i].poz]=i; + + } + + s=(n*(n-1))/2; + + for (i=1;i<=n;i++) + + { + + s-=1LL*query(v1[i]); + + update(v1[i],1); + + } + + g< +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct Element + +{ + + int value; + + int index; + + bool operator < (const Element &a) + + { + + return value < a.value || (value == a.value && index < a.index); + + } + +}; + + + +int N; + + + +Element A[100000 + 5]; + +int AIB[100000 + 5]; + + + +static inline void update (int poz, int value); + +static inline int query (int poz); + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; ++i) + + fin >> A[i].value, A[i].index = i; + + sort (A + 1, A + N + 1); + + long long sum = 0; + + for (int i = 1; i <= N; ++i) + + { + + sum += 1LL * (query (A[i].index)); + + update (A[i].index, 1); + + } + + long long ans = (1LL * N * (N - 1) / 2) - sum; + + fout << ans % 9917; + + return 0; + +} + + + +static inline void update (int poz, int value) + +{ + + while (poz <= N) + + AIB[poz] += value, poz += (poz&(-poz)); + +} + + + +static inline int query (int poz) + +{ + + int ans = 0; + + while (poz) + + ans = (ans + AIB[poz]) % 9917, poz -= (poz&(-poz)); + + return ans; + + + +} diff --git a/invCount/aib/407a1ba4-137e-11eb-bdee-309c234925a0.cpp b/invCount/aib/407a1ba4-137e-11eb-bdee-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f47732e537ec9573bbb79c5e172717a7cddbf300 --- /dev/null +++ b/invCount/aib/407a1ba4-137e-11eb-bdee-309c234925a0.cpp @@ -0,0 +1,150 @@ +#include + + + + +using namespace std; + + + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + + + +struct normalizare + +{ + + int val,poz; + +}v[100100]; + + + + + +int compare (normalizare A,normalizare B) + +{ + + if(A.val == B.val)return (A.poz=1;i-=(i&(-i))) + + S+=AIB[i]; + + return S; + +} + + + +int i,x,total,numere_mai_mari,sol[100100]; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>x; + + v[i].val=x; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare); + + + + for(i=1;i<=n;i++) + + { + + sol[v[i].poz]=i; + + } + + + + + + for(i=1;i<=n;i++) + + { + + x=sol[i]; + + + + numere_mai_mari=suma(n)-suma(x); + + total = (total+numere_mai_mari) % 9917; + + + + adaug(x,1); + + + + } + + + + g< +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aint[3*100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p9917) + + aint[nod]-=9917; + +} + + + +void query(int nod, int st, int dr){ + + if(a<=st && dr<=b){ + + nr+=aint[nod]; + + if(nr>=9917) + + nr-=9917; + + return; + + } + + else{ + + int mid=(st+dr)/2; + + if(a<=mid) + + query(2*nod,st,mid); + + if(b>mid) + + query(2*nod+1,mid+1,dr); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + a=v[i]+1; b=k; + + nr=0; + + query(1,1,k); + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + poz=v[i]; + + update(1,1,k); + + } + + fout< +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int N, answer; + +pair v[100000 + 5]; + + + +int bit[100000 + 5]; + +vector pos; + + + +void update(int x) + +{ + + for (int i = x; i <= N; i += (((i - 1) ^ i) & i)) + + bit[i]++; + +} + + + +int query(int x) + +{ + + int ret = 0; + + for (int i = x; i >= 1; i -= (((i - 1) ^ i) & i)) + + ret += bit[i]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; i++) + + { + + fin >> v[i].first; + + v[i].second = i; + + } + + sort(v + 1, v + N + 1); + + reverse(v + 1, v + N + 1); + + for (int i = 1; i <= N; i++) + + { + + int j = i; + + while (j <= N && v[j].first == v[i].first) + + { + + pos.push_back(j); + + answer = (answer + query(v[j].second)) % 9917; + + j++; + + } + + for (auto &it: pos) + + update(v[it].second); + + pos.clear(); + + if (j > i) + + i = j - 1; + + } + + fout << answer << "\n"; + + return 0; + +} diff --git a/invCount/aib/40aecf68-137e-11eb-96e0-309c234925a0.cpp b/invCount/aib/40aecf68-137e-11eb-96e0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfdc76184b01a0d1dc438bbc9742767ad2cd15b4 --- /dev/null +++ b/invCount/aib/40aecf68-137e-11eb-96e0-309c234925a0.cpp @@ -0,0 +1,111 @@ +#include +#include +using namespace std; + + + +FILE *fin = fopen("inv.in", "r"); + +FILE *fout = fopen("inv.out", "w"); + + + +struct tablou {int x; int poz;} a[100005]; + + + +int n,i,aib[100005]; + +long long total; + + + +bool comparare(tablou a, tablou b) + +{ + + if(a.x!=b.x) return (a.x < b.x); + + return(a.poz=1; i-=i&(-i)) + + ans += aib[i]; + + + + return ans; + +} + + + +int main() + +{ + + fscanf(fin, "%d", &n); + + for(i=1; i<=n; ++i) + + { + + fscanf(fin, "%d", &a[i].x); + + a[i].poz = i; + + } + + + + sort(a+1, a+n+1, comparare); + + + + for(i=1; i<=n; ++i) + + { + + total += 1LL*sum(a[i].poz); + + update(a[i].poz); + + } + + + + fprintf(fout, "%d", (1LL*n*(n-1)/2 - total)%9917); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invCount/aib/40bdea9e-137e-11eb-bb1d-309c234925a0.cpp b/invCount/aib/40bdea9e-137e-11eb-bb1d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1dac29f0fc070c35d068a087b767133b4afeae8 --- /dev/null +++ b/invCount/aib/40bdea9e-137e-11eb-bb1d-309c234925a0.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream f ("inv.in") ; + +ofstream g ("inv.out") ; + +int N , x , a , b , inversiune , sol[100005]; + +int AIB[100005]; + +int solutie; + +struct normalize + +{ + + int val; + + int poz; + +}v[100005]; + +bool cmp (normalize a , normalize b) + +{ + + if (a.val == b.val) return (a.poz < b.poz); + + return (a.val < b.val) ; + +} + +void Update (int poz , int val) + +{ + + for (int i = poz ; i <= N ; i += i & (-i)) + + AIB[i] += val; + + + + return; + +} + +int Query (int poz) + +{ + + int sum = 0 ; + + for (int i = poz ; i >= 1 ; i -= i & (-i)) + + sum += AIB[i] ; + + return sum ; + +} + +int main() + +{ + + f >> N ; + + for (int i = 1 ; i <= N ; ++i) + + { + + f >> v[i].val; + + v[i].poz = i ; + + } + + sort (v + 1 , v + N + 1 , cmp) ; + + + + for (int i = 1 ; i <= N ; ++i) + + sol[v[i].poz] = i ; + + + + + + + + for (int i = 1 ; i <= N ; ++i) + + { + + x = sol[i] ; + + inversiune = Query(N) - Query(x) ; + + solutie = (solutie + inversiune) % 9917; + + + + Update(x,1) ; + + } + + g << solutie ; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/410294be-137e-11eb-8717-309c234925a0.cpp b/invCount/aib/410294be-137e-11eb-8717-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82ae7f6014558a52e866de9a3476ba0f32bf778e --- /dev/null +++ b/invCount/aib/410294be-137e-11eb-8717-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include + + + + + + +template class Normalize { + +private: + + int value[MAXN]; + + + +public: + + Normalize(int N, int V[]) { + + std::vector normSet; + + for (int i=0; i=1; X-=(X&(-X))) + + ans += AIB[X], ans %= 9917; + + return ans; + +} + + + +std::ifstream input ("inv.in"); + +std::ofstream output("inv.out"); + + + +void readInput() { + + input >> N; + + for (int i=1; i<=N; ++i) + + input >> V[i]; + +} + + + +void solveInput() { + + Normalize <100005> normVal(N, V+1); + + int ans = 0; + + for (int i=0; i +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(;p;p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(;p<=k;p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + for(i=1; i<=n; i++){ + + if(w[i].first!=w[i-1].first) k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++){ + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< +#include +#include +using namespace std; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +int n,i,S[100010],A[100010],sol,dbl; + +pair v[100010]; + +int q(int p){ + + int nr=0; + + for(;p;p-=(p&-p)) + + nr+=A[p]; + + return nr; + +} + +void up(int p){ + + for(;p<=n;p+=(p&-p)) + + A[p]++; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + +} + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++){ + + if(v[i].first==v[i-1].first){ + + S[v[i].second]=S[v[i-1].second]; + + dbl++; + + } + + else + + S[v[i].second]=i-dbl; + + } + + for(i=1;i<=n;i++){ + + sol+=q(n-dbl)-q(S[i]); + + sol%=9917; + + up(S[i]); + + } + + fout< + + + +using namespace std; + +ifstream fin ("inv.in"); +ofstream fout ("inv.out"); + +int aib[100005], x, n, sol; + +struct nr +{ + int val, poz; +}; + +nr v[100005]; + +void update (int poz, int val) +{ + for (int i = poz; i <= n; i += (i&(-i))) + { + aib[i] += val; + } +} + +int suma (int poz) +{ + int s = 0; + for (int i = poz; i > 0; i -= (i&(-i))) + { + s = (s + aib[i]) % 9917; + } + + return s; +} + +bool cmp1 (nr a, nr b) +{ + if (a.val > b.val) return false; + + if (a.val == b.val && a.poz > b.poz) return false; + + return true; +} + +bool cmp2 (nr a, nr b) +{ + if (a.poz > b.poz) return false; + + return true; +} +int main() +{ + fin >> n; + + for (int i = 1; i <= n; i++) + { + fin >> v[i].val; + v[i].poz = i; + } + + sort(v + 1, v + n + 1, cmp1); + for (int i = 1; i <= n; i++) + v[i].val = i; + + sort(v + 1, v + n + 1, cmp2); + + for (int i = 1; i <= n; i++) + { + sol = (sol + suma(v[i].val)) % 9917; + update(v[i].val, 1); + } + + fout << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + return 0; +} diff --git a/invCount/aib/41401ca8-137e-11eb-9832-309c234925a0.cpp b/invCount/aib/41401ca8-137e-11eb-9832-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf6ccaeee15de381778fcc8f0553fcb2d608c697 --- /dev/null +++ b/invCount/aib/41401ca8-137e-11eb-9832-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +long long n,v[100010],a[100010],k=1,aux[100010]; + +long long query(long long x){ + + int r=0; + + while(x){ + + r+=aux[x]; + + x-=(x&-x); + + } + + return r; + +} + +void update(long long x){ + + while(x<=k){ + + aux[x]++; + + x+=(x&-x); + + } + +} + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1,a+n+1); + + for(int i=2;i<=n;i++) + + if(a[i]!=a[k]) + + a[++k]=a[i]; + + + + for(int i=1;i<=n;i++){ + + int st=1; + + int dr=k; + + while(st<=dr){ + + int mid=(st+dr)/2; + + if(v[i]==a[mid]){ + + v[i]=mid; + + break; + + } + + else + + if(v[i]>a[mid]) + + st=mid+1; + + else + + dr=mid-1; + + + + } + + + + } + + int suma=0; + + + + + + for(int i=1;i<=n;i++){ + + suma+=(query(k)-query(v[i]));suma%=9917; + + update(v[i]); + + } + + fout< +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(; p>=1; p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(; p<=k; p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + + + for(i=1; i<=n; i++) + + { + + if(w[i].first!=w[i-1].first) + + k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++) + + { + + + + + + + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,f[100010],w[100010],cnt,sol; + +pair v[100010]; + +void update(int x) { + + for (;x<=n;x+=(x&-x)) + + f[x]++; + +} + +int query(int x) { + + int val=0; + + for (;x;x-=(x&-x)) + + val+=f[x]; + + return val; + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) { + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for (i=1;i<=n;i++) { + + if (v[i].first==v[i-1].first) { + + w[v[i].second]=w[v[i-1].second]; + + cnt++; + + } + + else + + w[v[i].second]=i-cnt; + + } + + for (i=1;i<=n;i++) { + + sol=(sol+query(n-cnt)-query(w[i]))%9917; + + update(w[i]); + + } + + fout< +#include + + + + +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct numar { + + int id, poz; + +}; + + + +int n, i, x, sol, cnt; + +int A[100005], f[100005]; + + + +numar v[100005]; + + + +inline bool cmp (numar a, numar b){ + + return a.id < b.id; + +} + + + +inline void update (int i){ + + while (i <= n){ + + A[i]++; + + i += (i&-i); + + } + +} + + + +inline int query (int i){ + + int sol = 0; + + while (i){ + + sol += A[i]; + + i -= (i&-i); + + } + + return sol; + +} + + + +int main(){ + + fin >> n; + + for (i=1; i<=n; i++){ + + fin >> x; + + v[i] = {x, i}; + + } + + sort (v + 1, v + n + 1, cmp); + + f[v[1].poz] = 1; + + for (i=2; i<=n; i++){ + + if (v[i].id == v[i-1].id){ + + cnt++; + + f[v[i].poz] = f[v[i-1].poz]; + + } + + else{ + + f[v[i].poz] = i - cnt; + + } + + } + + cnt = n - cnt; + + for (i=1; i<=n; i++){ + + sol += (query(cnt) - query(f[i])); + + sol = sol%9917; + + update (f[i]); + + } + + fout << sol; + + return 0; + +} diff --git a/invCount/aib/41abaf00-137e-11eb-a55a-309c234925a0.cpp b/invCount/aib/41abaf00-137e-11eb-a55a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fc5032aef800cc83913bd69d2e33d8092d8e800 --- /dev/null +++ b/invCount/aib/41abaf00-137e-11eb-a55a-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,k,v[100001],a[100001],ord[100001]; + +long long sol; + + + +inline int cb(int x){ + + int st=1; + + int dr=k; + + int mid; + + + + while(st<=dr){ + + mid=(st+dr)/2; + + + + if(x==ord[mid]) + + return mid; + + + + if(x>n; + + for(i=1;i<=n;i++) + + fin>>a[i],ord[i]=a[i]; + + sort(ord+1,ord+n+1); + + + + ord[0]=-1; + + for(i=1;i<=n;i++) + + if(ord[i]!=ord[k]) + + ord[++k]=ord[i]; + + + + for(i=1;i<=n;i++) + + a[i]=cb(a[i]); + + + + for(i=1;i<=n;i++){ + + sol=(sol+i-1-query(a[i]))%9917; + + upd(a[i]); + + } + + + + fout< +#include +#include + +using namespace std; + +const int NMAX = 100010; +int aib[NMAX]; +int n; + +void update(int poz) +{ + while (poz <= n) + aib[poz]++, poz += ((poz) & -(poz)); +} +int query(int poz) +{ + int ans = 0; + while (poz) + ans += aib[poz], poz -= ((poz) & -(poz)); + return ans; +} + +int v[NMAX]; + +int main() +{ + FILE * in = fopen("inv.in", "r"); + FILE * out = fopen("inv.out", "w"); + + vector nrz; + + fscanf(in, "%d", &n); + + for (int i = 1; i <= n; i++) { + fscanf(in, "%d", v + i); + nrz.push_back(v[i]); + } + + sort(nrz.begin(), nrz.end()); + nrz.resize(unique(nrz.begin(), nrz.end()) - nrz.begin()); + + for (int i = 1; i <= n; i++) + v[i] = lower_bound(nrz.begin(), nrz.end(), v[i]) - nrz.begin() + 1; + + long long ans = 0; + + for (int i = 1; i <= n; i++) { + update(v[i]); + ans += i - query(v[i]); + } + + fprintf(out, "%lld\n", ans % 9917); + + return 0; +} diff --git a/invCount/aib/41c9e9d0-137e-11eb-ac59-309c234925a0.cpp b/invCount/aib/41c9e9d0-137e-11eb-ac59-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6888f53aed67fbe71f3ab6068b8e050c03abd0b --- /dev/null +++ b/invCount/aib/41c9e9d0-137e-11eb-ac59-309c234925a0.cpp @@ -0,0 +1,109 @@ +#include +#include + + +using namespace std; + +int sum,b,p,i,x,n,aib[101*1001]; + + + +int sol(int x){ + +int i; + +int rez=0; + +for(i=x;i>0;i-=(i&(-i))) + + rez+=aib[i]; + +return rez; + + + +} + + + +void add(int x,int n){ + +int i; + +for(i=x;i<=n;i+=(i&(-i))) aib[i]++; + + + +} + +int ok=0; + +struct mch{ + +int val,ord; + +} a[101*1001]; + +int compare( mch a,mch b){ + +if(a.val==b.val) return a.ord +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,nr,k,sol; + +pair v[100010]; + +int w[100010],f[100010]; + +void update(int p){ + + for(;p<=k;p+=(p&-p)) + + f[p]++; + +} + +int query(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=f[p]; + + return sol; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++) + + if(v[i].first!=v[i-1].first) + + w[v[i].second]=i-nr; + + else{ + + nr++; + + w[v[i].second]=w[v[i-1].second]; + + } + + k=n-nr; + + for(i=1;i<=n;i++){ + + sol+=(query(k)-query(w[i])); + + sol%=9917; + + update(w[i]); + + } + + fout< using namespace std; ifstream f("inv.in"); ofstream g("inv.out"); #define ub(x) (x&(-x)) #define MOD 9917 int n,i,p,nr,AIB[100005],x; long long inv; vector >v; long long sol; void add(int x) { for(int j=x; j<=n; j+=ub(j)) AIB[j]++; } long long sum(int x) { long long s=0; for(int j=x; j>0; j-=ub(j)) s+=AIB[j]; return s; } int main() { f>>n; for(i=0;i>x; v.push_back(make_pair(x,i+1)); } sort(v.begin(),v.end()); inv=1LL*n*(n-1)/2; for(i=0; i +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + int x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +#include +#include + +using namespace std; +ifstream fin("inv.in"); +ofstream fout("inv.out"); +struct numar { + int poz, val; +}; +int n; +numar v[100001]; +int a[100005]; +void citire() { + fin >> n; + int va; + for (int i = 0; i < n; ++i) { + fin >> va; + v[i].val = va; + v[i].poz = i + 1; + } +} +bool comp(numar a, numar b) { + if (a.val == b.val) { + return a.poz > b.poz; + } + return a.val > b.val; +} +void update(int poz, int val) { + for (int i = poz; i <= n; i += i & -i) { + a[i] = (a[i] + val) % 9917; + } +} +long query(int poz) { + long s = 0; + for (int i = poz; i >= 1; i -= i & -i) { + s += a[i] % 9917; + } + return s % 9917; +} +int main() +{ + long inv = 0; + citire(); + sort(v, v + n, comp); + for (int i = 0; i < n; ++i) { + inv = (inv + query(v[i].poz)) % 9917; + update(v[i].poz, 1); + } + fout << inv << "\n"; + return 0; +} diff --git a/invCount/aib/4278e376-137e-11eb-93b7-309c234925a0.cpp b/invCount/aib/4278e376-137e-11eb-93b7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4efbfff339215450637925a9927b957b15454c92 --- /dev/null +++ b/invCount/aib/4278e376-137e-11eb-93b7-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const long long N=200009; + +struct xd + +{ + + long long x,poz; + +}a[N]; + +long long n; + +long long aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(long long i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +static inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], N; + + + +int AIB[NMAX]; + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + + + + return; + +} + + + +static inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +int B[NMAX], C[NMAX]; + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + + + + return; + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + C[++C[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + C[++C[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=(lower_bound(C+1, C+C[0]+1, A[i])-C); + + + + return; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + ans+=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans%=MOD; + + } + + + + g< + + + + +using namespace std; + + + +const int mxn = 100 * 1000 + 10, md = 9917; + + + +int arb[ 4 * mxn ]; + + + +pair< int, int > sir[ mxn ]; + +int sol[ mxn ]; + + + +int n; + + + +void update(int nod, int st, int sf, int poz){ + + arb[ nod ]++; + + arb[ nod ] %= md; + + if(st < sf){ + + int mid = (st + sf) / 2; + + if(mid >= poz) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, sf, poz); + + } + +} + + + +int query(int nod, int st, int sf, int poz){ + + if(poz <= st) + + return arb[ nod ]; + + else if(sf < poz) + + return 0; + + int mid = (st + sf)/2; + + return (query(2 * nod, st, mid, poz) + query(2 * nod + 1, mid + 1, sf, poz)) % md; + +} + + + +int main() + +{ + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin>> n; + + for(int i = 0, x; i < n; i++){ + + cin>> x; + + sir[ i ] = make_pair(x, i); + + } + + sort(sir, sir + n); + + int s = 0; + + for(int i = 0; i < n; i++){ + + s += query(1, 0, n - 1, sir[ i ].second); + + s %= md; + + update(1, 0, n - 1, sir[ i ].second); + + } + + cout<< s; + + return 0; + +} diff --git a/invCount/aib/43acbff2-287b-4bc8-80ef-bf9e8e1f9574.cpp b/invCount/aib/43acbff2-287b-4bc8-80ef-bf9e8e1f9574.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4275deb47915ef1578ca023a5abae6d8d1cbb5a0 --- /dev/null +++ b/invCount/aib/43acbff2-287b-4bc8-80ef-bf9e8e1f9574.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long long aib[100005],n; + +int bit (int i) + +{ + + return i^(i&(i-1)); + +} + +void update (int i,int val) + +{ + + int j; + + for (j=i;j<=n;j+=bit(j)) + + { + + aib[j]+=val; + + } + +} + +long long query (int i) + +{ + + int j;long long s=0; + + for (j=i;j>=1;j-=bit(j)) + + { + + s+=aib[j]; + + } + + return s; + +} + +long long v[100005],v1[100005]; + +struct wow + +{ + + long long nr,poz; + +}a[100005]; + +int compare (wow a,wow b) + +{ + + return a.nr>n; + + for (i=1;i<=n;i++) + + { + + f>>v[i]; + + a[i].nr=v[i]; + + a[i].poz=i; + + } + + sort (a+1,a+n+1,compare); + + for (i=1;i<=n;i++) + + { + + v1[a[i].poz]=i; + + } + + s=(n*(n-1))/2; + + for (i=1;i<=n;i++) + + { + + s-=1LL*query(v1[i]); + + update(v1[i],1); + + } + + g< + + + + +using namespace std; + + + +void solve() { + + int n; + + cin >> n; + + + + vector< int > v(n); + + + + for(int i = 0; i < n; ++i) cin >> v[i]; + + + + + + vector< int > actual = v; + + sort(actual.begin(), actual.end()); + + + + actual.erase(unique(actual.begin(), actual.end()), actual.end()); + + + + for(auto &x : v) { + + int l = 0, r = actual.size() - 1; + + int idx = 0; + + while(l <= r) { + + int m = l + r >> 1; + + if(actual[m] == x) { + + idx = m; + + break; + + } + + if(actual[m] < x) { + + l = m + 1; + + } else r = m - 1; + + } + + + + + + x = idx + 1; + + } + + + + vector< int > aib(actual.size() + 1); + + + + auto update = [&](int p) { + + for(; p < aib.size(); p += p&-p) ++aib[p]; + + }; + + + + auto query = [&](int p) { + + int ret = 0; + + for(;p;p-=p&-p) ret += aib[p]; + + return ret; + + }; + + + + long long ans = 0; + + + + for(int i = 0; i < n; ++i) { + + ans += i - query(v[i]); + + update(v[i]); + + } + + + + cout << ans % 9917<< '\n'; + +} + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + ios::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + + + + + solve(); + +} diff --git a/invCount/aib/444f4f34-137e-11eb-83b5-309c234925a0.cpp b/invCount/aib/444f4f34-137e-11eb-83b5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cffedec6f8dd580ee4626052c632b0162fc9c400 --- /dev/null +++ b/invCount/aib/444f4f34-137e-11eb-83b5-309c234925a0.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + int X=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=1; i<=N; ++i) + + { + + int X=i-1-Query(A[i]); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long long aib[100005],n; + +int bit (int i) + +{ + + return i^(i&(i-1)); + +} + +void update (int i,int val) + +{ + + int j; + + for (j=i;j<=n;j+=bit(j)) + + { + + aib[j]+=val; + + } + +} + +long long query (int i) + +{ + + int j;long long s=0; + + for (j=i;j>=1;j-=bit(j)) + + { + + s+=aib[j]; + + } + + return s; + +} + +long long v[100005],v1[100005]; + +struct wow + +{ + + long long nr,poz; + +}a[100005]; + +int compare (wow a,wow b) + +{ + + return a.nr>n; + + for (i=1;i<=n;i++) + + { + + f>>v[i]; + + a[i].nr=v[i]; + + a[i].poz=i; + + } + + sort (a+1,a+n+1,compare); + + for (i=1;i<=n;i++) + + { + + v1[a[i].poz]=i; + + } + + s=(n*(n-1))/2; + + for (i=1;i<=n;i++) + + { + + s-=1LL*query(v1[i]); + + update(v1[i],1); + + } + + g< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/453c801c-334e-4777-8e7f-ab04f65d3ad6.cpp b/invCount/aib/453c801c-334e-4777-8e7f-ab04f65d3ad6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..201170659be487d96daa49d726cde2faeb341735 --- /dev/null +++ b/invCount/aib/453c801c-334e-4777-8e7f-ab04f65d3ad6.cpp @@ -0,0 +1,264 @@ +#include +#include +using namespace std; + + + + + + + +int getSum(int BITree[], int index) + + + +{ + + + + int sum = 0; + + + + while (index > 0) + + + + { + + + + sum += BITree[index]; + + + + index -= index & (-index); + + + + } + + + + return sum; + + + +} +void updateBIT(int BITree[], int n, int index, int val) + + + +{ + + + + while (index <= n) + + + + { + + + + BITree[index] += val; + + + + index += index & (-index); + + + + } + + + +} + + + + + + + +void convert(int arr[],int n) + + + +{ + + + + int lst[n]; + + + + for(int i=1;i<=n;i++) + + + + lst[i]=arr[i]; + + + + sort(lst+1,lst+n+1); + + + + for(int i=1;i<=n;i++) + + + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + + + +} + + + + + + + +int getInvCount(int arr[], int n) + + + +{ + + + + int invcount = 0; + + + + convert(arr,n); + + + + int maxElement = 0; + + + + for (int i=1; i<=n; i++) + + + + if (maxElement < arr[i]) + + + + maxElement = arr[i]; + + + + + + + + int BIT[maxElement+1]; + + + + for (int i=1; i<=maxElement; i++) + + + + BIT[i] = 0; + + + + + + + + for (int i=n; i>0; i--) + + + + { + + + + invcount += getSum(BIT, arr[i]-1); + + + + invcount%=9917; + + + + updateBIT(BIT, maxElement, arr[i], 1); + + + + } + + + + + + + + return invcount; + + + +} + + + + + + + +int main() + + + +{ + + + + int arr[100000],n; + + + + ifstream fin("inv.in"); + + + + fin>>n; + + + + for(int i=1;i<=n;i++) + + + + fin>>arr[i]; + + + + fin.close(); + + + + ofstream fout("inv.out"); + + + + fout< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +struct xd + +{ + + long long x,poz; + +}a[100009]; + +long long n,i,v[100009],aib[100009],s; + +long long compare (xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x=1;j-=j^(j&(j-1))) + + s+=aib[j]; + + return s; + +} + +int main() + +{ + + fin.sync_with_stdio(false); + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i],a[i].x=v[i],a[i].poz=i; + + sort(a+1,a+n+1,compare); + + for(i=1;i<=n;i++) + + v[a[i].poz]=i; + + s=n*(n-1)/2; + + for(i=1;i<=n;i++) + + { + + s-=1LL*query(v[i]); + + update(v[i],1); + + } + + fout< +int N, M; + +int AIB[100005]; + + + +int Ind[100005]; + +std::pair V[100005]; + + + +void Update(int X, int Value) { + + for (X; X<=N; X += X&(-X)) + + AIB[X] += Value, + + AIB[X] %= 9917; + +} + + + +int Query(int X) { + + int Sum = 0; + + for (X; X; X -= X&(-X)) + + Sum += AIB[X], + + Sum %= 9917; + + return Sum; + +} + + + +std::ifstream In("inv.in"); + +std::ofstream Out("inv.out"); + + + +void Citire() { + + In >> N; + + for (int i=0; i> V[i+1].first, + + V[i+1].second = i+1; + +} + + + +void Rezolvare() { + + std::sort (V+1, V+N+1); + + for (int i=1; i<=N; ++i) + + Ind[V[i].second] = i; + + + + int NInv = 0; + + + + for (int i=0, X; i + + + + + + +using namespace std; + + + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + + + +int aib[100005], x, n, sol; + + + +struct nr + +{ + + int val, poz; + +}; + + + +nr v[100005]; + + + +void update (int poz, int val) + +{ + + for (int i = poz; i <= n; i += (i&(-i))) + + { + + aib[i] += val; + + } + +} + + + +int suma (int poz) + +{ + + int s = 0; + + for (int i = poz; i > 0; i -= (i&(-i))) + + { + + s = (s + aib[i]) % 9917; + + } + + + + return s; + +} + + + +bool cmp1 (nr a, nr b) + +{ + + if (a.val > b.val) return false; + + + + if (a.val == b.val && a.poz > b.poz) return false; + + + + return true; + +} + + + +bool cmp2 (nr a, nr b) + +{ + + if (a.poz > b.poz) return false; + + + + return true; + +} + +int main() + +{ + + f >> n; + + + + for (int i = 1; i <= n; i++) + + { + + f >> v[i].val; + + v[i].poz = i; + + } + + + + sort(v + 1, v + n + 1, cmp1); + + for (int i = 1; i <= n; i++) + + v[i].val = i; + + + + sort(v + 1, v + n + 1, cmp2); + + + + for (int i = 1; i <= n; i++) + + { + + sol = (sol + suma(v[i].val)) % 9917; + + update(v[i].val, 1); + + } + + + + g << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + + return 0; + +} diff --git a/invCount/aib/46202ea4-137e-11eb-8144-309c234925a0.cpp b/invCount/aib/46202ea4-137e-11eb-8144-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6557b5239bfaf438d0cb292f1b9b9505b30c4c27 --- /dev/null +++ b/invCount/aib/46202ea4-137e-11eb-8144-309c234925a0.cpp @@ -0,0 +1,156 @@ +#include + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +struct sir + +{ + + long long val,poz; + +}v[100100]; + + + + + +long long compare1 (sir A,sir B) + +{ + + if(A.val==B.val)return(A.poz=1;i-=(i^(i-1)&i)) + + s+=AIB[i]; + + return s; + +} + + + + + +long long i,NR,k; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare1); + + for(i=1;i<=n;i++) + + { + + v[i].val=i; + + } + + sort(v+1,v+n+1,compare2); + + + + + + + + for(k=1;k<=n;k++) + + { + + NR+=suma(v[k].val); + + adaug(v[k].val,1); + + } + + + + + + g<<(n*(n-1)/2-NR)%9917; + + + + + + + + return 0; + +} diff --git a/invCount/aib/4640624a-137e-11eb-ba4b-309c234925a0.cpp b/invCount/aib/4640624a-137e-11eb-ba4b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5bbd7a44315e0d991e6b0863d3f7b0408cb81d2f --- /dev/null +++ b/invCount/aib/4640624a-137e-11eb-ba4b-309c234925a0.cpp @@ -0,0 +1,153 @@ +#include + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); +struct sir + +{ + + long long val,poz; + +}v[100100]; + + + + + +long long compare1 (sir A,sir B) + +{ + + if(A.val==B.val)return(A.poz=1;i-=(i^(i-1)&i)) + + s+=AIB[i]; + + return s; + +} + + + + + +long long i,NR,k; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare1); + + for(i=1;i<=n;i++) + + { + + v[i].val=i; + + } + + sort(v+1,v+n+1,compare2); + + + + + + + + for(k=1;k<=n;k++) + + { + + NR+=suma(v[k].val); + + adaug(v[k].val,1); + + } + + + + + + g<<(n*(n-1)/2-NR)%9917; + + + + + + + + return 0; + +} diff --git a/invCount/aib/464fa4c6-137e-11eb-ac9d-309c234925a0.cpp b/invCount/aib/464fa4c6-137e-11eb-ac9d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec6a05dceb13d0a330e50baeb715b8f39e1a4d62 --- /dev/null +++ b/invCount/aib/464fa4c6-137e-11eb-ac9d-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +typedef long long ll; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int MOD = 9917; + +const int NMAX = 100005; + + + +int aib[NMAX]; + + + +void update(int pos, int n) { + + for(; pos <= n; pos += pos & (-pos)) + + aib[pos] ++; + +} + + + +int query(int pos) { + + int ans = 0; + + for(; pos; pos -= pos & (-pos)) + + ans += aib[pos]; + + return ans; + +} + + + +int main() { + + int n; + + in >> n; + + vector > v(n + 1, {0, 0}); + + for(int i = 1; i <= n; i ++) { + + in >> v[i].first; + + v[i].second = i; + + } + + sort(v.begin() + 1, v.end()); + + + + vector tonorm(n + 1, 0); + + for(int i = 1; i <= n; i ++) + + tonorm[v[i].second] = i; + + + + int ans = 0; + + for(int i = 1; i <= n; i ++) { + + ans = (ans + query(n) - query(tonorm[i])) % MOD; + + update(tonorm[i], n); + + } + + out << ans; + + return 0; + +} diff --git a/invCount/aib/465ff880-137e-11eb-8248-309c234925a0.cpp b/invCount/aib/465ff880-137e-11eb-8248-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..657ed50618c001155e213d79334d49f223f643f1 --- /dev/null +++ b/invCount/aib/465ff880-137e-11eb-8248-309c234925a0.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + +int s[100001],n,z[100001]; + +pair v[100001]; + + + +int zeros(int i) + +{ + + return (i^(i-1))&i; + +} + +void add(int p,int x) + +{ + + for(int i=p; i<=n; i+=zeros(i)) + + s[i]+=x; + +} + +long long sum(int p) + +{ + + long long rez=0LL; + + for(int i=p; i>0; i-=zeros(i)) + + rez+=s[i]; + + return rez; + +} + +int main() + +{ + + int i,x; + + long long rez=0LL; + + freopen("inv.in","r",stdin); + + freopen("inv.out","w",stdout); + + scanf("%d",&n); + + for(i=1; i<=n; i++) + + { + + scanf("%d",&z[i]); + + v[i].first=z[i]; + + v[i].second=i; + + } + + sort(v+1,v+1+n); + + for(i=1; i<=n; i++) + + z[v[i].second]=i; + + for(i=1; i<=n; i++) + + { + + x=z[i]; + + rez+=i-1-sum(x-1); + + add(x,1); + + } + + printf("%d",rez%9917); + + return 0; + + + +} diff --git a/invCount/aib/46bca3a2-137e-11eb-85ef-309c234925a0.cpp b/invCount/aib/46bca3a2-137e-11eb-85ef-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e26ce7f1b6861ded1b58e9f47e451b485e61f40 --- /dev/null +++ b/invCount/aib/46bca3a2-137e-11eb-85ef-309c234925a0.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +const int lim = 1e5; + +int arbint[4 * lim], v[lim], w[lim]; + +vector normie; + + + +inline int to_normie(int x){ + + int ans = 0; + + for(int pas = 1 << 17; pas > 0; pas /= 2) + + if(ans + pas < normie.size() && normie[ans + pas] <= x) + + ans += pas; + + return ans; + +} + + + +void update(int nod, int st, int dr, int x){ + + if(st == dr){ + + arbint[nod]++; + + return; + + } + + if(x <= (st + dr) / 2) + + update(2 * nod, st, (st + dr) / 2, x); + + else + + update(2 * nod + 1, (st + dr) / 2 + 1, dr, x); + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int query(int nod, int st, int dr, int l, int r){ + + if(l <= st && dr <= r) + + return arbint[nod]; + + int ans = 0; + + if(l <= (st + dr) / 2) + + ans += query(2 * nod, st, (st + dr) / 2, l, r); + + if(r > (st + dr) / 2) + + ans += query(2 * nod + 1, (st + dr) / 2 + 1, dr, l, r); + + return ans; + +} + + + +int main() + +{ + + int n; + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + fin >> n; + + for(int i = 0; i < n; i++){ + + fin >> v[i]; + + w[i] = v[i]; + + } + + sort(w, w + n); + + normie.push_back(w[0]); + + for(int i = 1; i < n; i++) + + if(w[i] != w[i - 1]) + + normie.push_back(w[i]); + + for(int i = 0; i < n; i++) + + v[i] = to_normie(v[i]); + + long long ans = 0; + + for(int i = 0; i < n; i++){ + + if(v[i] < n - 1) + + ans = ans + query(1, 0, n - 1, v[i] + 1, n - 1); + + update(1, 0, n - 1, v[i]); + + } + + fout << ans % 9917; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/473e09fa-137e-11eb-818c-309c234925a0.cpp b/invCount/aib/473e09fa-137e-11eb-818c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afa75814293d1e36a2f2990b0d76fa9f75e0024f --- /dev/null +++ b/invCount/aib/473e09fa-137e-11eb-818c-309c234925a0.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int bit[100009],v[100009],v1[100009]; + +int n; + +int upt(int i,int add) { + + for(; i<=n; i+=(i&-i)) + + bit[i]+=add; + +} + +int see(int i) { + + int ans=0; + + for(; i>0; i-=(i&-i)) + + ans+=bit[i]; + + return ans; + +} + +int main() { + + int i,c=0; + + in>>n; + + for(i=1; i<=n; i++) + + in>>v[i],v1[i]=v[i]; + + sort(v1+1,v1+n+1); + + for(i=1; i<=n; i++) + + v[i]=lower_bound(v1,v1+n,v[i])-v1+1; + + for (i=n; i>=1; i--) { + + c+=see(v[i]-1); + + c%=9917; + + upt(v[i],1); + + } + + out< +#include +#include + + + +using namespace std; + +ifstream cin("inv.in"); +ofstream cout("inv.out"); + +int AIB[100000 +1]; +vector> v; + +void update(int x,int n){ + for(;x<=n;x+=x&(-x)) + AIB[x]++; +} + +int query(int x){ + int ans=0; + + for(;x>0;x-=x&(-x)) + ans+=AIB[x]; + + return ans; +} + +int main(){ + int n; + cin>>n; + + int ans=0; + for(int i=1;i<=n;i++){ + int x; + cin>>x; + + v.push_back({x,i}); + } + + sort(v.begin(),v.end()); + + for(int i=0;i +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< + + + + +using namespace std; + + + +int getSum(int BITree[], int index) + +{ + + int sum = 0; + + while (index > 0) + + { + + sum += BITree[index]; + + index -= index & (-index); + + } + + return sum; + +} + + + + + +void updateBIT(int BITree[], int n, int index, int val) + +{ + + while (index <= n) + + { + + BITree[index] += val; + + index += index & (-index); + + } + +} + + + +void convert(int arr[],int n) + +{ + + int lst[n]; + + for(int i=1;i<=n;i++) + + lst[i]=arr[i]; + + sort(lst+1,lst+n+1); + + for(int i=1;i<=n;i++) + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + +} + + + +int getInvCount(int arr[], int n) + +{ + + int invcount = 0; + + convert(arr,n); + + int maxElement = 0; + + for (int i=1; i<=n; i++) + + if (maxElement < arr[i]) + + maxElement = arr[i]; + + + + int BIT[maxElement+1]; + + for (int i=1; i<=maxElement; i++) + + BIT[i] = 0; + + + + for (int i=n; i>0; i--) + + { + + invcount += getSum(BIT, arr[i]-1); + + invcount%=9917; + + updateBIT(BIT, maxElement, arr[i], 1); + + } + + + + return invcount; + +} + + + +int main() + +{ + + int arr[100000],n; + + ifstream fin("inv.in"); + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>arr[i]; + + fin.close(); + + ofstream fout("inv.out"); + + fout< +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i +#include +#include +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +const int mod = 9917; + +int aib[5 + 100000]; + +int n; + +vector> v; + + + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] = (aib[i] + val) % mod; + +} + + + +int Query(int p) + +{ + + int suma = 0; + + for (int i = p; i >= 1; i -= i & -i) + + suma = (suma + aib[i]) % mod; + + + + return suma; + +} + + + +int main() + +{ + + fin >> n; + + v = vector>(n); + + int val; + + for (int i = 0; i < n; ++i) + + { + + fin >> val; + + v[i] = {val, i + 1}; + + } + + + + sort(v.begin(), v.end(), greater>()); + + + + int res = 0; + + for (auto& p : v) + + { + + res += Query(p.second); + + if (res >= mod) + + res -= mod; + + Update(p.second, 1); + + } + + + + fout << res; + + + + return 0; + +} diff --git a/invCount/aib/4cef2d3f-0d46-4942-bbcc-8fa888202ba8.cpp b/invCount/aib/4cef2d3f-0d46-4942-bbcc-8fa888202ba8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7afb5c945211663b59a5f9dba0bc1423df2c07a6 --- /dev/null +++ b/invCount/aib/4cef2d3f-0d46-4942-bbcc-8fa888202ba8.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int mod=9917; + +const int nmax=100005; + + + +int n,aib[nmax]; + + + +struct arr + +{ + + int val,poz; + +}v[nmax]; + + + +bool cmp(arr a,arr b) + +{ + + return (a.val>b.val || (a.val==b.val && a.poz>b.poz)); + +} + + + +void read() + +{ + + fin>>n; + + + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,cmp); + +} + + + +void update(int poz) + +{ + + for(;poz<=n;poz+= poz & -poz) + + aib[poz]++; + +} + + + +int query(int poz) + +{ + + int ans=0; + + + + for(;poz;poz-= poz & -poz) + + ans+=aib[poz]; + + + + return ans; + +} + + + +void solve() + +{ + + int ans=0; + + + + for(int i=1;i<=n;i++) + + { + + ans=ans+query(v[i].poz-1); + + ans%=mod; + + + + update(v[i].poz); + + } + + + + fout< +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int bit[100009],v[100009],v1[100009]; + +int n; + +int upt(int i,int add) { + + for(; i<=n; i+=(i&-i)) + + bit[i]+=add; + +} + +int see(int i) { + + int ans=0; + + for(; i>0; i-=(i&-i)) + + ans+=bit[i]; + + return ans; + +} + +int main() { + + int i,c=0; + + in>>n; + + for(i=1; i<=n; i++) + + in>>v[i],v1[i]=v[i]; + + sort(v1+1,v1+n+1); + + for(i=1; i<=n; i++) + + v[i]=lower_bound(v1,v1+n,v[i])-v1+1; + + for (i=n; i>=1; i--) { + + c+=see(v[i]-1); + + c%=9917; + + upt(v[i],1); + + } + + out< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int t[100001],aint[300001]; + +int k,n,sol,a,b,poz,nr,MOD=9917; + +struct norm + +{ + + int poz,val; + +}v[100001]; + +int cmp(norm a, norm b) + +{ + + if(a.val==b.val) + + return a.poz=dr) + + { + + nr+=aint[nod]; + + if(nr==MOD) + + nr=0; + + } + + else + + { + + int mid=(st+dr)/2; + + if(a<=mid) + + query(2*nod,st,mid); + + if(b>mid) + + query(2*nod+1,mid+1,dr); + + } + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(int i=1;i<=n;i++) + + { + + if(v[i].val!=v[i-1].val) + + k++; + + t[v[i].poz]=k; + + } + + for(int i=1;i<=n;i++) + + { + + a=t[i]+1;b=k;nr=0; + + query(1,1,k); + + sol=(sol+nr)%MOD; + + poz=t[i]; + + update(1,1,k); + + } + + fout< + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); +struct sir + +{ + + long long val,poz; + +}v[100100]; + + + + + +long long compare1 (sir A,sir B) + +{ + + if(A.val==B.val)return(A.poz=1;i-=(i^(i-1)&i)) + + s+=AIB[i]; + + return s; + +} + + + + + +long long i,NR,k; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare1); + + for(i=1;i<=n;i++) + + { + + v[i].val=i; + + } + + sort(v+1,v+n+1,compare2); + + + + + + + + for(k=1;k<=n;k++) + + { + + NR+=suma(v[k].val); + + adaug(v[k].val,1); + + } + + + + + + g<<(n*(n-1)/2-NR)%9917; + + + + + + + + return 0; + +} diff --git a/invCount/aib/572e6e04-7df0-4590-a992-14c1437de669.cpp b/invCount/aib/572e6e04-7df0-4590-a992-14c1437de669.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1d5281f34c1ab5c56319cc38d8caba51fff312 --- /dev/null +++ b/invCount/aib/572e6e04-7df0-4590-a992-14c1437de669.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/5759a7a1-ebc2-49e9-8037-8530c9c259cc.cpp b/invCount/aib/5759a7a1-ebc2-49e9-8037-8530c9c259cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce2bb323fa14ae1aad17541001d39be265e92f2c --- /dev/null +++ b/invCount/aib/5759a7a1-ebc2-49e9-8037-8530c9c259cc.cpp @@ -0,0 +1,83 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,nr,k,sol; + +pair v[100010]; + +int w[100010],f[100010]; + +void update(int p){ + + for(;p<=k;p+=(p&-p)) + + f[p]++; + +} + +int query(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=f[p]; + + return sol; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++) + + if(v[i].first!=v[i-1].first) + + w[v[i].second]=i-nr; + + else{ + + nr++; + + w[v[i].second]=w[v[i-1].second]; + + } + + k=n-nr; + + for(i=1;i<=n;i++){ + + sol+=(query(k)-query(w[i])); + + sol%=9917; + + update(w[i]); + + } + + fout< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/5c1af209-6e54-4874-89a7-ed52d3c4a2bf.cpp b/invCount/aib/5c1af209-6e54-4874-89a7-ed52d3c4a2bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb9763a5200ff483b2265c822313004d84b20ef0 --- /dev/null +++ b/invCount/aib/5c1af209-6e54-4874-89a7-ed52d3c4a2bf.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int MOD = 9917; + +const int N = 100000; + + + +int aib[N + 1]; + +struct nr{ + + int val, index; + +} v[N + 1]; + + + +int index[N + 1]; + + + +bool cmp(nr a, nr b){ + + if(a.val > b.val) + + return true; + + else if(a.val == b.val && a.index > b.index) + + return true; + + return false; + +} + + + +int sum(int x){ + + int ans = 0; + + while(x){ + + ans += aib[x]; + + x -= (x & (-x)); + + } + + return ans; + +} + + + +int n; + + + +void add(int x, int val){ + + while(x <= n){ + + aib[x] += val; + + x += (x & (-x)); + + } + +} + + + +int main() + +{ + + int i,ans; + + fin >> n; + + for(i=1; i <= n; i++){ + + fin >> v[i].val; + + v[i].index = i; + + } + + sort(v + 1, v + n + 1, cmp); + + ans = 0; + + for(i=1; i<=n; i++){ + + ans = (ans + sum(v[i].index - 1)) % MOD; + + add(v[i].index, 1); + + } + + fout << ans << "\n"; + + return 0; + +} diff --git a/invCount/aib/60da1411-050c-4b9f-beca-135f4afc74cc.cpp b/invCount/aib/60da1411-050c-4b9f-beca-135f4afc74cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51fcabd01da0e6db15017eaaec3370f9d65189a8 --- /dev/null +++ b/invCount/aib/60da1411-050c-4b9f-beca-135f4afc74cc.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aint[300001], s, a, b, pos; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int node, int st, int dr){ + + if(st == dr){ + + aint[node] = (aint[node] + 1) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid < pos) + + update(2 * node + 1, mid + 1, dr); + + else + + update(2 * node, st, mid); + + aint[node] = (aint[node] + 1) % 9917; + +} + +void quary(int node, int st, int dr){ + + if(st >= a && dr <= b){ + + s = (s + aint[node]) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid + 1 <= b) + + quary(2 * node + 1, mid + 1, dr); + + if(mid >= a) + + quary(2 * node, st, mid); + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + b = k; + + for(i = 1; i <= n; i++){ + + if(x[i] + 1 <= n){ + + a = x[i] + 1; + + quary(1, 1, k); + + } + + pos = x[i]; + + update(1, 1, k); + + } + + g << s; + + return 0; + +} diff --git a/invCount/aib/6464d968-7124-4ca5-8098-9705966da27e.cpp b/invCount/aib/6464d968-7124-4ca5-8098-9705966da27e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1d5281f34c1ab5c56319cc38d8caba51fff312 --- /dev/null +++ b/invCount/aib/6464d968-7124-4ca5-8098-9705966da27e.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/65fae472-ed3f-4a75-8015-34a16820b0e8.cpp b/invCount/aib/65fae472-ed3f-4a75-8015-34a16820b0e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b72b520e69187ab04f29c4799928bd23f80009db --- /dev/null +++ b/invCount/aib/65fae472-ed3f-4a75-8015-34a16820b0e8.cpp @@ -0,0 +1,95 @@ +#include + + +using namespace std; + +void __print(int x) {cerr << x;} +void __print(long x) {cerr << x;} +void __print(long long x) {cerr << x;} +void __print(unsigned x) {cerr << x;} +void __print(unsigned long x) {cerr << x;} +void __print(unsigned long long x) {cerr << x;} +void __print(float x) {cerr << x;} +void __print(double x) {cerr << x;} +void __print(long double x) {cerr << x;} +void __print(char x) {cerr << '\'' << x << '\'';} +void __print(const char *x) {cerr << '\"' << x << '\"';} +void __print(const string &x) {cerr << '\"' << x << '\"';} +void __print(bool x) {cerr << (x ? "true" : "false");} + +template +void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} +template +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} +void _print() {cerr << "]\n";} +template +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} + + + + + + + +struct fenwick_tree{ + vector tr; + int n; + void build(vector &arr){ + for(int i=1;i<=n;i++){ + tr[i]+=arr[i-1]; + if(i+(i&-i)<=n){ + tr[i+(i&-i)]+=tr[i]; + } + } + + } + void update(int pos,int val){ + while(pos<=n){ + tr[pos]+=val; + pos=pos+(pos&-pos); + } + + } + int query(int l,int r){ + int s1=0; + while(l>0){ + s1+=tr[l]; + l=l-(l&-l); + } + int s2=0; + while(r>0){ + s2+=tr[r]; + r=r-(r&-r); + } + + return s2-s1; + } +}; + +int main(){ + ifstream in("inv.in"); + ofstream out("inv.out"); + int n; + in>>n; + vector a(n); + for(auto &x:a)in>>x; + int mod=9917; + int ans=0; + auto temp=a; + temp.push_back((int)-1e9); + sort(temp.begin(),temp.end()); + temp.erase(unique(temp.begin(),temp.end()),temp.end()); + for(int i=0;i=0;i--){ + ans=ans+ft.query(0,a[i]-1); + ft.update(a[i],1); + ans%=mod; + } + out< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + int x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +#include +#include +using namespace std; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +int n,i,S[100010],A[100010],sol,dbl; + +pair v[100010]; + +int q(int p){ + + int nr=0; + + for(;p;p-=(p&-p)) + + nr+=A[p]; + + return nr; + +} + +void up(int p){ + + for(;p<=n;p+=(p&-p)) + + A[p]++; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + +} + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++){ + + if(v[i].first==v[i-1].first){ + + S[v[i].second]=S[v[i-1].second]; + + dbl++; + + } + + else + + S[v[i].second]=i-dbl; + + } + + for(i=1;i<=n;i++){ + + sol+=q(n-dbl)-q(S[i]); + + sol%=9917; + + up(S[i]); + + } + + fout< + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +long long n, v[100005], aib[100005]; + +pair vRead[100005]; + + + +long long sum(int x) + +{ + + int s = 0; + + while (x) { + + s += aib[x]; + + x -= x & (-x); + + } + + return s; + +} + + + +void add(int x, int val) + +{ + + while (x <= n) { + + aib[x] = (aib[x] + val) % 9917; + + x += x & (-x); + + } + +} + + + +void normalise() + +{ + + long long x; + + for (int i = 1; i <= n; ++i) { + + f >> x; + + vRead[i] = {x, i}; + + } + + sort (vRead + 1, vRead + n + 1); + + for (int i = 1; i <= n; ++i) { + + v[i] = vRead[i].second; + + } + +} + + + +int main() + +{ + + f >> n; + + + + normalise(); + + + + + + long long noOfInversions = 0; + + for (int i = n; i >= 1; --i) { + + noOfInversions = (noOfInversions + sum(v[i])) % 9917; + + add(v[i], 1); + + } + + g << noOfInversions; + + return 0; + +} diff --git a/invCount/aib/681e8747-6afa-4282-b520-9ff04346066e.cpp b/invCount/aib/681e8747-6afa-4282-b520-9ff04346066e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5650ede2c2d3a631e5abdeabdc3577119b2ba8b --- /dev/null +++ b/invCount/aib/681e8747-6afa-4282-b520-9ff04346066e.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + + +string name = "inv"; + + +ifstream fin(name + ".in"); +ofstream fout(name + ".out"); + + + + +int n; +vector a; + +int f[100005]; + +inline int nrz(int x) { + return x & -x; +} + +int query(int x) { + int s = 0; + + while (x > 0) { + s += f[x]; + x -= nrz(x); + } + + return s; +} + +int query(int a, int b) { + return query(b) - query(a - 1); +} + +void update(int x) { + while (x <= n) { + f[x]++; + x += nrz(x); + } +} + +struct Item { + int v; + int i; +}; + +int main() { + + fin >> n; + + auto compare = [](Item lhs, Item rhs) { + return lhs.v > rhs.v; + }; + + priority_queue, decltype(compare)> pq(compare); + + for (int i = 0; i > x; + a.push_back(x); + pq.push({x, i}); + } + + int v = 0; + int last = -0x3f3f3f3f; + + while (!pq.empty()) { + auto x = pq.top(); + pq.pop(); + + if (x.v != last) { + last = x.v; + v++; + } + + a[x.i] = v; + } + + int result = 0; + + for (int i = 0; i < n; ++i) { + int x = a[i]; + + if (x < n) { + result += query(x + 1, n); + result %= 9917; + } + + update(x); + } + + fout << result; + + return 0; +} diff --git a/invCount/aib/6933ec82-f078-4b5e-b61b-30c120e80862.cpp b/invCount/aib/6933ec82-f078-4b5e-b61b-30c120e80862.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6888f53aed67fbe71f3ab6068b8e050c03abd0b --- /dev/null +++ b/invCount/aib/6933ec82-f078-4b5e-b61b-30c120e80862.cpp @@ -0,0 +1,109 @@ +#include +#include + + +using namespace std; + +int sum,b,p,i,x,n,aib[101*1001]; + + + +int sol(int x){ + +int i; + +int rez=0; + +for(i=x;i>0;i-=(i&(-i))) + + rez+=aib[i]; + +return rez; + + + +} + + + +void add(int x,int n){ + +int i; + +for(i=x;i<=n;i+=(i&(-i))) aib[i]++; + + + +} + +int ok=0; + +struct mch{ + +int val,ord; + +} a[101*1001]; + +int compare( mch a,mch b){ + +if(a.val==b.val) return a.ord +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(; p>=1; p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(; p<=k; p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + + + for(i=1; i<=n; i++) + + { + + if(w[i].first!=w[i-1].first) + + k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++) + + { + + + + + + + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); +struct sir + +{ + + long long val,poz; + +}v[100100]; + + + + + +long long compare1 (sir A,sir B) + +{ + + if(A.val==B.val)return(A.poz=1;i-=(i^(i-1)&i)) + + s+=AIB[i]; + + return s; + +} + + + + + +long long i,NR,k; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + + + sort(v+1,v+n+1,compare1); + + for(i=1;i<=n;i++) + + { + + v[i].val=i; + + } + + sort(v+1,v+n+1,compare2); + + + + + + + + for(k=1;k<=n;k++) + + { + + NR+=suma(v[k].val); + + adaug(v[k].val,1); + + } + + + + + + g<<(n*(n-1)/2-NR)%9917; + + + + + + + + return 0; + +} diff --git a/invCount/aib/7099568d-512f-4613-894b-f20105095641.cpp b/invCount/aib/7099568d-512f-4613-894b-f20105095641.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7474dfc66abddbf3636b57a6d8411406cfa96e25 --- /dev/null +++ b/invCount/aib/7099568d-512f-4613-894b-f20105095641.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + +private: + + int a[5 + N]; + + + + int lsb(int i){ + + return (i & (-i)); + + } + + + +public: + + void Update(int pos) { + + for(int i = pos; i >= 1; i -= lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos + 1; i <= n; i += lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b){ + + return a.second < b.second; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++){ + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + + + sort(v + 1, v + n + 1); + + for(int i = 1; i <= n; i++) { + + int j = i; + + while(v[j].first == v[j + 1].first && j <= n) j++; + + v[i].first = v[i - 1].first + 1; + + i++; + + + + while(i <= j) v[i].first = v[i - 1].first, i++; + + i--; + + } + + + + sort(v + 1, v + n + 1, cmp); + + + + for(int i = 1; i <= n; i++){ + + inv += Fenw.Query(v[i].first); + + Fenw.Update(v[i].first); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/70a0b2fa-6081-4fe5-92d7-dc51e2bb690c.cpp b/invCount/aib/70a0b2fa-6081-4fe5-92d7-dc51e2bb690c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f28e078f23cae8da6cee526df90b0688e135ad80 --- /dev/null +++ b/invCount/aib/70a0b2fa-6081-4fe5-92d7-dc51e2bb690c.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +using PII = pair; + +using VP = vector; + +const int mod = 9917; + +int aib[110011], s, n, x; + + + +inline void Update(int poz) + +{ + + for (int i = poz; i <= n; i += i & -i) + + ++aib[i]; + +} + + + +inline int Query(int poz) + +{ + + int sum = 0; + + for (int i = poz; i > 0; i -= i & -i) + + sum += aib[i]; + + return sum; + +} + + + +int main() + +{ + + fin >> n; + + VP v(n + 1); + + for (int i = 1; i <= n; ++i) + + { + + fin >> x; + + v[i] = {x, i}; + + } + + sort(v.begin() + 1, v.end(), greater()); + + for (int i = 1; i <= n; ++i) + + { + + s = (s + Query(v[i].second)) % mod; + + Update(v[i].second); + + } + + fout << s; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/71356626-343d-4a36-b9e8-a65a40bd6d07.cpp b/invCount/aib/71356626-343d-4a36-b9e8-a65a40bd6d07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9058a876922b66a404fd3bc8db680b39b3b517fc --- /dev/null +++ b/invCount/aib/71356626-343d-4a36-b9e8-a65a40bd6d07.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + + + +int ans,aint[400005],v[100005],n,t,nr,st,dr,s; + +vector b; + + + +void query(int nod,int l,int r, int st,int dr){ + + if(st<=l && dr>=r){ + + s+=aint[nod]; + + return; + + } + + int mij=(l+r)>>1; + + if(st<=mij){ + + query(nod*2,l,mij,st,dr); + + } + + if(dr>mij){ + + query(nod*2+1,mij+1,r,st,dr); + + } + +} + + + +void update(int nod,int l,int r,int poz,int val){ + + if(l==poz && r==poz){ + + aint[nod]+=val; + + return; + + } + + int mij; + + mij=(l+r)>>1; + + if(poz<=mij){ + + update(nod*2,l,mij,poz,val); + + } + + if(poz>mij){ + + update(nod*2+1,mij+1,r,poz,val); + + } + + aint[nod]=aint[nod*2]+aint[nod*2+1]; + + + +} + + + +int main() + +{ + + cin>>n; + + for (int i=1;i<=n;i++) { + + cin>>v[i]; + + b.push_back(v[i]); + + } + + sort(b.begin(),b.end()); + + b.erase(unique(b.begin(),b.end()),b.end()); + + for (int i=1;i<=n;i++){ + + v[i]=lower_bound(b.begin(),b.end(),v[i])-b.begin()+1; + + } + + for(int i=1;i<=n;i++){ + + s=0; + + query(1,1,n,v[i]+1,n); + + ans+=s; + + ans%=9917; + + update(1,1,n,v[i],1); + + } + + cout< +# include +using namespace std; + +int val,poz; + +struct inv + +{ + + int val,poz; + +}; + +inv v[100001]; + +int a[100001],i,x,n; + +long long r; + +bool cmp(inv a, inv b) + +{ + + if (a.val==b.val) return(a.poz=1; i-=(i&(-i))) + + s+=a[i]; + + return s; + +} + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + f>>n; + + r=n-1; + + r*=n; + + r/=2; + + for(i=1; i<=n; ++i) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(i=1; i<=n; ++i) + + { + + r=r-query(v[i].poz); + + update(v[i].poz); + + } + + r%=9917; + + g<< r; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/739eb4ce-4b89-4b1d-8e69-20736f1b009f.cpp b/invCount/aib/739eb4ce-4b89-4b1d-8e69-20736f1b009f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0976ef353a5c0fba4935ad3230cff0e9ec66d4e --- /dev/null +++ b/invCount/aib/739eb4ce-4b89-4b1d-8e69-20736f1b009f.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + + + +using namespace std; + +ifstream cin("inv.in"); +ofstream cout("inv.out"); + +int AIB[100000 +1]; +vector> v; + +void update(int x,int n){ + for(;x<=n;x+=x&(-x)) + AIB[x]++; +} + +int query(int x){ + int ans=0; + + for(;x>0;x-=x&(-x)) + ans+=AIB[x]; + + return ans; +} + +int main(){ + int n; + cin>>n; + + int ans=0; + for(int i=1;i<=n;i++){ + int x; + cin>>x; + + v.push_back({x,i}); + } + + sort(v.begin(),v.end()); + + for(int i=0;i + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/78bd1799-8ee2-4cc9-a7d2-38a432272d85.cpp b/invCount/aib/78bd1799-8ee2-4cc9-a7d2-38a432272d85.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f87b7149ad126e7a1450f8d6e85eec5d5decf5d9 --- /dev/null +++ b/invCount/aib/78bd1799-8ee2-4cc9-a7d2-38a432272d85.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const long long N=200009; + +struct xd + +{ + + long long x,poz; + +}a[N]; + +long long n; + +long long aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(long long i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +int N, M; + +int AIB[100005]; + + + +int Ind[100005]; + +std::pair V[100005]; + + + +void Update(int X, int Value) { + + for (X; X<=N; X += X&(-X)) + + AIB[X] += Value, + + AIB[X] %= 9917; + +} + + + +int Query(int X) { + + int Sum = 0; + + for (X; X; X -= X&(-X)) + + Sum += AIB[X], + + Sum %= 9917; + + return Sum; + +} + + + +std::ifstream In("inv.in"); + +std::ofstream Out("inv.out"); + + + +void Citire() { + + In >> N; + + for (int i=0; i> V[i+1].first, + + V[i+1].second = i+1; + +} + + + +void Rezolvare() { + + std::sort (V+1, V+N+1); + + for (int i=1; i<=N; ++i) + + Ind[V[i].second] = i; + + + + int NInv = 0; + + + + for (int i=0, X; i +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aint[3*100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p9917) + + aint[nod]-=9917; + +} + + + +void query(int nod, int st, int dr){ + + if(a<=st && dr<=b){ + + nr+=aint[nod]; + + if(nr>=9917) + + nr-=9917; + + return; + + } + + else{ + + int mid=(st+dr)/2; + + if(a<=mid) + + query(2*nod,st,mid); + + if(b>mid) + + query(2*nod+1,mid+1,dr); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + a=v[i]+1; b=k; + + nr=0; + + query(1,1,k); + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + poz=v[i]; + + update(1,1,k); + + } + + fout< + + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int v[100001]; + +pair ss[100001]; + +int AIB[100001]; + +int n; + +void update(int i, int val) + +{ + + while(i<=n) + + { + + AIB[i]+=val; + + i+=(i&(-i)); + + } + +} + +int query(int i) + +{ + + int s=0; + + while(i>0) + + { + + s+=AIB[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + ios_base::sync_with_stdio(false); + + int i; + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>ss[i].first; + + ss[i].second=i; + + } + + sort(ss+1,ss+n+1); + + for(i=1;i<=n;i++) + + v[ss[i].second]=i; + + for(i=1;i<=n;i++) + + update(v[i],1); + + int ans=0; + + for(i=1;i<=n;i++) + + { + + ans=(ans+query(v[i])-1+9917)%9917; + + update(v[i],-1); + + } + + g< +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +long long n,v[100010],a[100010],k=1,aux[100010]; + +long long query(long long x){ + + int r=0; + + while(x){ + + r+=aux[x]; + + x-=(x&-x); + + } + + return r; + +} + +void update(long long x){ + + while(x<=k){ + + aux[x]++; + + x+=(x&-x); + + } + +} + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>v[i]; + + a[i]=v[i]; + + } + + sort(a+1,a+n+1); + + for(int i=2;i<=n;i++) + + if(a[i]!=a[k]) + + a[++k]=a[i]; + + + + for(int i=1;i<=n;i++){ + + int st=1; + + int dr=k; + + while(st<=dr){ + + int mid=(st+dr)/2; + + if(v[i]==a[mid]){ + + v[i]=mid; + + break; + + } + + else + + if(v[i]>a[mid]) + + st=mid+1; + + else + + dr=mid-1; + + + + } + + + + } + + int suma=0; + + + + + + for(int i=1;i<=n;i++){ + + suma+=(query(k)-query(v[i]));suma%=9917; + + update(v[i]); + + } + + fout< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + int X=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< +# include +using namespace std; + +int val,poz; + +struct inv + +{ + + int val,poz; + +}; + +inv v[100001]; + +int a[100001],i,x,n; + +long long r; + +bool cmp(inv a, inv b) + +{ + + if (a.val==b.val) return(a.poz=1; i-=(i&(-i))) + + s+=a[i]; + + return s; + +} + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + f>>n; + + r=n-1; + + r*=n; + + r/=2; + + for(i=1; i<=n; ++i) + + { + + f>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(i=1; i<=n; ++i) + + { + + r=r-query(v[i].poz); + + update(v[i].poz); + + } + + r%=9917; + + g<< r; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/7f6c9bae-9026-44e1-b1aa-b61bc701bdf7.cpp b/invCount/aib/7f6c9bae-9026-44e1-b1aa-b61bc701bdf7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51fcabd01da0e6db15017eaaec3370f9d65189a8 --- /dev/null +++ b/invCount/aib/7f6c9bae-9026-44e1-b1aa-b61bc701bdf7.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aint[300001], s, a, b, pos; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int node, int st, int dr){ + + if(st == dr){ + + aint[node] = (aint[node] + 1) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid < pos) + + update(2 * node + 1, mid + 1, dr); + + else + + update(2 * node, st, mid); + + aint[node] = (aint[node] + 1) % 9917; + +} + +void quary(int node, int st, int dr){ + + if(st >= a && dr <= b){ + + s = (s + aint[node]) % 9917; + + return; + + } + + int mid = (st + dr) / 2; + + if(mid + 1 <= b) + + quary(2 * node + 1, mid + 1, dr); + + if(mid >= a) + + quary(2 * node, st, mid); + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + b = k; + + for(i = 1; i <= n; i++){ + + if(x[i] + 1 <= n){ + + a = x[i] + 1; + + quary(1, 1, k); + + } + + pos = x[i]; + + update(1, 1, k); + + } + + g << s; + + return 0; + +} diff --git a/invCount/aib/8034a065-558b-4077-9418-febeb93aac5c.cpp b/invCount/aib/8034a065-558b-4077-9418-febeb93aac5c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1d5281f34c1ab5c56319cc38d8caba51fff312 --- /dev/null +++ b/invCount/aib/8034a065-558b-4077-9418-febeb93aac5c.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + +int main() + +{ + + int i; + + int x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i-1].val) + + a[i].nou = a[i-1].nou; + + else a[i].nou = a[i-1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + for(i = n; i >= 1; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/809d2e0d-f069-47cf-ab54-f8b44cbd9ce4.cpp b/invCount/aib/809d2e0d-f069-47cf-ab54-f8b44cbd9ce4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7479d7eb9a6cb6e1a651e91f174e55be565f5031 --- /dev/null +++ b/invCount/aib/809d2e0d-f069-47cf-ab54-f8b44cbd9ce4.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + + private: + + int a[5 + N]; + + + + int lsb(int i) { + + return (i & (-i)); + + } + + + + public: + + void Update(int pos) { + + for(int i = pos; i <= n; i += lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos; i >= 1; i -= lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b) { + + if(a.first == b.first) return a.second < b.second; + + return a.first < b.first; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++) { + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + sort(v + 1, v + n + 1); + + + + for(int i = n; i >= 1; i--) { + + inv += Fenw.Query(v[i].second); + + Fenw.Update(v[i].second); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/810bc336-1dc4-4216-9e3d-808605212e39.cpp b/invCount/aib/810bc336-1dc4-4216-9e3d-808605212e39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4efbfff339215450637925a9927b957b15454c92 --- /dev/null +++ b/invCount/aib/810bc336-1dc4-4216-9e3d-808605212e39.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long A[100002], Srt[100002]; + +int N, lg; + +int aib[100002]; + +int i; + +int sol; + + + +void update(int X, int val) + +{ + + for(; X <= lg; X += ((X ^ (X - 1)) & X)) + + aib[X] += val; + +} + + + +int query(int X) + +{ + + int ret = 0; + + for(; X >= 1; X -= ((X ^ (X - 1)) & X)) + + ret += aib[X]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for(i = 1; i <= N; i++) + + fin >> A[i], Srt[i] = A[i]; + + sort(Srt + 1, Srt + N + 1); + + lg = 1; + + for(i = 2; i <= N; i++) + + if(Srt[i] != Srt[lg]) + + Srt[++lg] = Srt[i]; + + for(i = 1; i <= N; i++) + + A[i] = lower_bound(Srt + 1, Srt + lg + 1, A[i]) - Srt; + + for(i = N; i >= 1; i--) + + { + + sol = (sol + query(A[i] - 1)) % 9917; + + update(A[i], 1); + + } + + fout << sol << "\n"; + + return 0; + +} diff --git a/invCount/aib/82a7f9f2-be9e-44e0-98b8-7a926c540b41.cpp b/invCount/aib/82a7f9f2-be9e-44e0-98b8-7a926c540b41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f778ddab2d48cc27b572165735bebe796ca92469 --- /dev/null +++ b/invCount/aib/82a7f9f2-be9e-44e0-98b8-7a926c540b41.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int NMAX = 100000, + + MOD = 9917; + + + +struct elem { + + int poz, val; + +}; + +int N, AIB[NMAX+1]; + +elem V[NMAX+1]; + + + +bool comp(const elem& a, const elem& b) { + + if(a.val == b.val) return a.poz < b.poz; + + else return a.val < b.val; + +} + + + +void update(int p) { + + while(p <= N) { + + AIB[p]++; + + p += p & (-p); + + } + +} + + + +int query(int p) { + + int s = 0; + + while(p > 0) { + + s += AIB[p]; + + p -= p & (-p); + + } + + return s; + +} + + + +int main() + +{ + + int x, nrInv = 0; + + in >> N; + + for(int i = 1; i <= N; i++) { + + in >> x; + + V[i] = {i, x}; + + } + + sort(V+1, V+N+1, comp); + + + + for(int i = 1; i <= N; i++) { + + x = V[i].poz; + + nrInv = (nrInv + x - query(x) - 1) % MOD; + + update(x); + + } + + + + out << nrInv; + + return 0; + +} diff --git a/invCount/aib/82f5742d-d02f-4445-be74-5c1f1833edba.cpp b/invCount/aib/82f5742d-d02f-4445-be74-5c1f1833edba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..140f7fcc467bd657953df1f33fc67e386b6728f3 --- /dev/null +++ b/invCount/aib/82f5742d-d02f-4445-be74-5c1f1833edba.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; +ifstream cin("inv.in"); +ofstream cout("inv.out"); +bool ms(pair a,pair b){ + return a.second=1;a-=(a&(-a))){ + sol+=aib[a]; + } + return sol; +} +pair v[100005]; +int main() +{ + int a,sum=0; + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i].first; + v[i].second=i; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++) + v[i].first=i; + sort(v+1,v+n+1,ms); + for(int i=1;i<=n;i++){ + a=v[i].first; + sum+=i-que(a)-1; + sum%=9917; + upd(a); + + } + cout< + + +using namespace std; + +const int lim = 1e5; + +int arbint[4 * lim], v[lim], w[lim]; + +vector normie; + + + +inline int to_normie(int x){ + + int ans = 0; + + for(int pas = 1 << 17; pas > 0; pas /= 2) + + if(ans + pas < normie.size() && normie[ans + pas] <= x) + + ans += pas; + + return ans; + +} + + + +void update(int nod, int st, int dr, int x){ + + if(st == dr){ + + arbint[nod]++; + + return; + + } + + if(x <= (st + dr) / 2) + + update(2 * nod, st, (st + dr) / 2, x); + + else + + update(2 * nod + 1, (st + dr) / 2 + 1, dr, x); + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int query(int nod, int st, int dr, int l, int r){ + + if(l <= st && dr <= r) + + return arbint[nod]; + + int ans = 0; + + if(l <= (st + dr) / 2) + + ans += query(2 * nod, st, (st + dr) / 2, l, r); + + if(r > (st + dr) / 2) + + ans += query(2 * nod + 1, (st + dr) / 2 + 1, dr, l, r); + + return ans; + +} + + + +int main() + +{ + + int n; + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + fin >> n; + + for(int i = 0; i < n; i++){ + + fin >> v[i]; + + w[i] = v[i]; + + } + + sort(w, w + n); + + normie.push_back(w[0]); + + for(int i = 1; i < n; i++) + + if(w[i] != w[i - 1]) + + normie.push_back(w[i]); + + for(int i = 0; i < n; i++) + + v[i] = to_normie(v[i]); + + long long ans = 0; + + for(int i = 0; i < n; i++){ + + if(v[i] < n - 1) + + ans = ans + query(1, 0, n - 1, v[i] + 1, n - 1); + + update(1, 0, n - 1, v[i]); + + } + + fout << ans % 9917; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/85278f04-7d97-4961-bbd8-f4092558b95d.cpp b/invCount/aib/85278f04-7d97-4961-bbd8-f4092558b95d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a59a0e20adbb77806cddc005af3bc32a78cd67bf --- /dev/null +++ b/invCount/aib/85278f04-7d97-4961-bbd8-f4092558b95d.cpp @@ -0,0 +1,194 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/8629cc7e-cdd8-4815-a6b2-ebceea63eccb.cpp b/invCount/aib/8629cc7e-cdd8-4815-a6b2-ebceea63eccb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4adf13f229ba239da8555fd11f8e88d72ba97d82 --- /dev/null +++ b/invCount/aib/8629cc7e-cdd8-4815-a6b2-ebceea63eccb.cpp @@ -0,0 +1,112 @@ +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +long long n, v[100005], aib[100005]; + +pair vRead[100005]; + + + +long long sum(int x) + +{ + + int s = 0; + + while (x) { + + s += aib[x]; + + x -= x & (-x); + + } + + return s; + +} + + + +void add(int x, int val) + +{ + + while (x <= n) { + + aib[x] = (aib[x] + val) % 9917; + + x += x & (-x); + + } + +} + + + +void normalise() + +{ + + long long x; + + for (int i = 1; i <= n; ++i) { + + f >> x; + + vRead[i] = {x, i}; + + } + + sort (vRead + 1, vRead + n + 1); + + for (int i = 1; i <= n; ++i) { + + v[i] = vRead[i].second; + + } + +} + + + +int main() + +{ + + f >> n; + + + + normalise(); + + + + + + long long noOfInversions = 0; + + for (int i = n; i >= 1; --i) { + + noOfInversions = (noOfInversions + sum(v[i])) % 9917; + + add(v[i], 1); + + } + + g << noOfInversions; + + return 0; + +} diff --git a/invCount/aib/8c6f77f4-c5fc-473c-ac93-023c5fa4af6c.cpp b/invCount/aib/8c6f77f4-c5fc-473c-ac93-023c5fa4af6c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3990d41e89ad618ab5fa7fc4ac4171fdef21aa1d --- /dev/null +++ b/invCount/aib/8c6f77f4-c5fc-473c-ac93-023c5fa4af6c.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int t[100001],aib[300001]; + +int k,n,sol,a,b,poz,nr,MOD=9917; + +struct norm + +{ + + int poz,val; + +}v[100001]; + +int cmp(norm a, norm b) + +{ + + if(a.val==b.val) + + return a.poz0) + + { + + s=(s+aib[x])%MOD; + + x=(x-(x&(-x))); + + } + + return s; + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i].val; + + v[i].poz=i; + + } + + sort(v+1,v+n+1,cmp); + + for(int i=1;i<=n;i++) + + { + + if(v[i].val!=v[i-1].val) + + k++; + + t[v[i].poz]=k; + + } + + + + for(int i=1;i<=n;i++) + + { + + int val=query(k)-query(t[i]); + + if(val<0) + + val+=MOD; + + sol=(sol+val)%MOD; + + update(t[i]); + + } + + fout< +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + long long s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aib[100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + + + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p0){ + + s=(s+aib[x])%9917; + + x-=(x&(-x)); + + } + + return s; + +} + + + +void update(int x){ + + while(x<=k){ + + aib[x]++; + + if(aib[x]>=9917) + + aib[x]-=9917; + + x+=(x&(-x)); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + nr=query(k)-query(v[i]); + + if(nr<0) + + nr+=9917; + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + update(v[i]); + + } + + fout< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/9282e767-25d8-4700-b126-b83e5856a514.cpp b/invCount/aib/9282e767-25d8-4700-b126-b83e5856a514.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b72b520e69187ab04f29c4799928bd23f80009db --- /dev/null +++ b/invCount/aib/9282e767-25d8-4700-b126-b83e5856a514.cpp @@ -0,0 +1,95 @@ +#include + + +using namespace std; + +void __print(int x) {cerr << x;} +void __print(long x) {cerr << x;} +void __print(long long x) {cerr << x;} +void __print(unsigned x) {cerr << x;} +void __print(unsigned long x) {cerr << x;} +void __print(unsigned long long x) {cerr << x;} +void __print(float x) {cerr << x;} +void __print(double x) {cerr << x;} +void __print(long double x) {cerr << x;} +void __print(char x) {cerr << '\'' << x << '\'';} +void __print(const char *x) {cerr << '\"' << x << '\"';} +void __print(const string &x) {cerr << '\"' << x << '\"';} +void __print(bool x) {cerr << (x ? "true" : "false");} + +template +void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} +template +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} +void _print() {cerr << "]\n";} +template +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} + + + + + + + +struct fenwick_tree{ + vector tr; + int n; + void build(vector &arr){ + for(int i=1;i<=n;i++){ + tr[i]+=arr[i-1]; + if(i+(i&-i)<=n){ + tr[i+(i&-i)]+=tr[i]; + } + } + + } + void update(int pos,int val){ + while(pos<=n){ + tr[pos]+=val; + pos=pos+(pos&-pos); + } + + } + int query(int l,int r){ + int s1=0; + while(l>0){ + s1+=tr[l]; + l=l-(l&-l); + } + int s2=0; + while(r>0){ + s2+=tr[r]; + r=r-(r&-r); + } + + return s2-s1; + } +}; + +int main(){ + ifstream in("inv.in"); + ofstream out("inv.out"); + int n; + in>>n; + vector a(n); + for(auto &x:a)in>>x; + int mod=9917; + int ans=0; + auto temp=a; + temp.push_back((int)-1e9); + sort(temp.begin(),temp.end()); + temp.erase(unique(temp.begin(),temp.end()),temp.end()); + for(int i=0;i=0;i--){ + ans=ans+ft.query(0,a[i]-1); + ft.update(a[i],1); + ans%=mod; + } + out< + + + +using namespace std; + +ifstream fin ("inv.in"); +ofstream fout ("inv.out"); + +int aib[100005], x, n, sol; + +struct nr +{ + int val, poz; +}; + +nr v[100005]; + +void update (int poz, int val) +{ + for (int i = poz; i <= n; i += (i&(-i))) + { + aib[i] += val; + } +} + +int suma (int poz) +{ + int s = 0; + for (int i = poz; i > 0; i -= (i&(-i))) + { + s = (s + aib[i]) % 9917; + } + + return s; +} + +bool cmp1 (nr a, nr b) +{ + if (a.val > b.val) return false; + + if (a.val == b.val && a.poz > b.poz) return false; + + return true; +} + +bool cmp2 (nr a, nr b) +{ + if (a.poz > b.poz) return false; + + return true; +} +int main() +{ + fin >> n; + + for (int i = 1; i <= n; i++) + { + fin >> v[i].val; + v[i].poz = i; + } + + sort(v + 1, v + n + 1, cmp1); + for (int i = 1; i <= n; i++) + v[i].val = i; + + sort(v + 1, v + n + 1, cmp2); + + for (int i = 1; i <= n; i++) + { + sol = (sol + suma(v[i].val)) % 9917; + update(v[i].val, 1); + } + + fout << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + return 0; +} diff --git a/invCount/aib/9718aab7-8297-40b9-89fa-c73f16a47a70.cpp b/invCount/aib/9718aab7-8297-40b9-89fa-c73f16a47a70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8468e7fdfa2c891cc6d44dcc55a268c4da71a54 --- /dev/null +++ b/invCount/aib/9718aab7-8297-40b9-89fa-c73f16a47a70.cpp @@ -0,0 +1,43 @@ +#include + + + +using namespace std; + +struct Fenwick { + int n; + vector t; + Fenwick(int n_) : n(n_), t(n + 1) {} + void Update(int pos, int val) { + for (++pos; pos <= n; pos += pos & -pos) t[pos] += val; + } + int Query(int pos) { + int ans = 0; + for (++pos; pos; pos -= pos & -pos) ans += t[pos]; + return ans; + } +}; + +int main() { + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + auto all = v; + all.emplace_back(-(int)1e9); + sort(all.begin(), all.end()); + all.erase(unique(all.begin(), all.end()), all.end()); + for (int i = 0; i < n; ++i) { + v[i] = lower_bound(all.begin(), all.end(), v[i]) - all.begin(); + } + + Fenwick fw(n); + int64_t ans = 0; + for (int i = n - 1; i >= 0; --i) { + ans += fw.Query(v[i] - 1); + fw.Update(v[i], 1); + } + cout << ans % 9917 << endl; +} diff --git a/invCount/aib/99ecba73-cbcf-41fa-9ddb-e3a15cec8e1a.cpp b/invCount/aib/99ecba73-cbcf-41fa-9ddb-e3a15cec8e1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ed0da6e19a835e1267a185fd470989ef03b5bd5 --- /dev/null +++ b/invCount/aib/99ecba73-cbcf-41fa-9ddb-e3a15cec8e1a.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i, nrInv = 0; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv = (nrInv + x - sum(x) - 1) % MOD; + + add(x); + + } + + g << nrInv; + + return 0; + +} diff --git a/invCount/aib/9b399622-8a6e-472c-a9ef-ba1665f051ca.cpp b/invCount/aib/9b399622-8a6e-472c-a9ef-ba1665f051ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89c1f2afda83418ed8651f4d59c61fef0dc2c7cb --- /dev/null +++ b/invCount/aib/9b399622-8a6e-472c-a9ef-ba1665f051ca.cpp @@ -0,0 +1,59 @@ +#include +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + +class FenTree { + private: + int n; + vector bit; + + public: + FenTree(int n) : + n(n), bit(n + 1) { } + + void update(int pos, int val) { + for (int i = pos; i <= n; i += i & -i) + bit[i] += val; + } + + int query(int pos) { + int sum = 0; + for (int i = pos; i >= 1; i -= i & -i) + sum += bit[i]; + return sum; + } +}; + +int main() { + int n; fin >> n; + vector> val(n); + + for (int i = 0; i < n; i++) { + fin >> val[i].first; + val[i].second = i; + } + sort(val.begin(), val.end()); + + int cnt = 0; + vector v(n); + for (int i = 0; i < n; i++) { + if (!i || val[i - 1].first != val[i].first) + cnt++; + v[val[i].second] = cnt; + } + + int sol = 0; + const int MOD = 9917; + + FenTree bit(cnt); + for (int i = n - 1; i >= 0; i--) { + sol = (sol + bit.query(v[i] - 1)) % MOD; + bit.update(v[i], +1); + } + fout << sol << '\n'; + + fout.close(); + return 0; +} diff --git a/invCount/aib/9babfec4-25c7-4708-a7fa-23e4d174fcb2.cpp b/invCount/aib/9babfec4-25c7-4708-a7fa-23e4d174fcb2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3af75edd8e264ed7f58470f70dd2f13cba0d2f15 --- /dev/null +++ b/invCount/aib/9babfec4-25c7-4708-a7fa-23e4d174fcb2.cpp @@ -0,0 +1,185 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX = 1e5 + 5, MOD = 9917; + + + +long long i, n, m, a[NMAX], b[NMAX], v[NMAX], aib[NMAX], st, dr, poz, mij, nr; + + + +int ub (int x) + +{ + + return (x & (-x)); + +} + + + +long long sum (int x) + +{ + + long long s = 0; + + + + for(int i = x; i >= 1; i -= ub(i)) + + { + + s += aib[i]; + + + + s %= MOD; + + } + + + + return s; + +} + +void Add (int x) + +{ + + for(int i = x; i <= m; i += ub(i)) + + { + + aib[i]++; + + + + aib[i] %= MOD; + + } + +} + + + +int main() + +{ + + f>>n; + + + + for(i=1; i<=n; i++) + + { + + f>>a[i]; + + + + b[i]=a[i]; + + } + + + + sort(b+1,b+n+1); + + + + v[++m]=b[1]; + + + + for(i=2; i<=n; i++) + + if(b[i]!=b[i-1]) + + v[++m]=b[i]; + + + + for(i=1; i<=n; i++) + + { + + st=1; + + dr=m; + + + + poz=0; + + + + while(st<=dr) + + { + + mij=(st+dr)/2; + + + + if(v[mij] <= a[i]) + + { + + poz=mij; + + st=mij+1; + + } + + else + + dr=mij-1; + + } + + a[i]=poz; + + } + + + + for(i = n; i >= 1; i--) + + { + + nr += sum(a[i] - 1); + + + + nr %= 9917; + + + + Add(a[i]); + + } + + + + g< +#include + + +using namespace std; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +int n,i,m,a[100005],b[100005],v[100005],aib[100005]; + +int ub (int x) + +{ + + return(x&(-x)); + +} + + + +void add( int x ) + +{ + + for(int i=x; i<=n; i+=ub(i)) + + { + + aib[i]++; + + + + } + + + +} + +int sum (int x) + +{ + + int s=0; + + for(int i=x; i>=1; i-=ub(i)) + + { + + s=s+aib[i]; + + + + } + + return s; + +} +int main() + +{ + +in>>n; + + + +for(i=1;i<=n;i++) + +{in>>a[i]; + + b[i]=a[i]; + +} + +sort(b+1,b+n+1); + +m=0; + +v[++m]=b[1]; + +for(i=2;i<=n;i++) + +{if(b[i]!=b[i-1])v[++m]=b[i]; + + + +} +for(i=1;i<=n;i++){a[i]=lower_bound(v+1,v+m+1,a[i])-v; } +int nr=0; + +for(i=n;i>=1;i--) + +{nr=nr+sum(a[i]-1); + + + + nr=nr%9917; + + add(a[i]); + + + +} + + + + out< +#include + +using namespace std; +ifstream cin("inv.in"); +ofstream cout("inv.out"); +bool ms(pair a,pair b){ + return a.second=1;a-=(a&(-a))){ + sol+=aib[a]; + } + return sol; +} +pair v[100005]; +int main() +{ + int a,sum=0; + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i].first; + v[i].second=i; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++) + v[i].first=i; + sort(v+1,v+n+1,ms); + for(int i=1;i<=n;i++){ + a=v[i].first; + sum+=i-que(a)-1; + sum%=9917; + upd(a); + + } + cout< +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long A[100002], Srt[100002]; + +int N, lg; + +int aib[100002]; + +int i; + +int sol; + + + +void update(int X, int val) + +{ + + for(; X <= lg; X += ((X ^ (X - 1)) & X)) + + aib[X] += val; + +} + + + +int query(int X) + +{ + + int ret = 0; + + for(; X >= 1; X -= ((X ^ (X - 1)) & X)) + + ret += aib[X]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for(i = 1; i <= N; i++) + + fin >> A[i], Srt[i] = A[i]; + + sort(Srt + 1, Srt + N + 1); + + lg = 1; + + for(i = 2; i <= N; i++) + + if(Srt[i] != Srt[lg]) + + Srt[++lg] = Srt[i]; + + for(i = 1; i <= N; i++) + + A[i] = lower_bound(Srt + 1, Srt + lg + 1, A[i]) - Srt; + + for(i = N; i >= 1; i--) + + { + + sol = (sol + query(A[i] - 1)) % 9917; + + update(A[i], 1); + + } + + fout << sol << "\n"; + + return 0; + +} diff --git a/invCount/aib/9ebe1bbb-24dd-4b92-8253-923b18ac1ce7.cpp b/invCount/aib/9ebe1bbb-24dd-4b92-8253-923b18ac1ce7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f9a5cd60a87b1e3f9e7391e9bfcf4ae9dff0d72 --- /dev/null +++ b/invCount/aib/9ebe1bbb-24dd-4b92-8253-923b18ac1ce7.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +const int NMAX = 100010; +int aib[NMAX]; +int n; + +void update(int poz) +{ + while (poz <= n) + aib[poz]++, poz += ((poz) & -(poz)); +} +int query(int poz) +{ + int ans = 0; + while (poz) + ans += aib[poz], poz -= ((poz) & -(poz)); + return ans; +} + +int v[NMAX]; + +int main() +{ + FILE * in = fopen("inv.in", "r"); + FILE * out = fopen("inv.out", "w"); + + vector nrz; + + fscanf(in, "%d", &n); + + for (int i = 1; i <= n; i++) { + fscanf(in, "%d", v + i); + nrz.push_back(v[i]); + } + + sort(nrz.begin(), nrz.end()); + nrz.resize(unique(nrz.begin(), nrz.end()) - nrz.begin()); + + for (int i = 1; i <= n; i++) + v[i] = lower_bound(nrz.begin(), nrz.end(), v[i]) - nrz.begin() + 1; + + long long ans = 0; + + for (int i = 1; i <= n; i++) { + update(v[i]); + ans += i - query(v[i]); + } + + fprintf(out, "%lld\n", ans % 9917); + + return 0; +} diff --git a/invCount/aib/9ed59fa0-a9ba-4841-b6a5-bd88d5f4ea5a.cpp b/invCount/aib/9ed59fa0-a9ba-4841-b6a5-bd88d5f4ea5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c79dd9ce0712388da99eefd77a776c7268af071 --- /dev/null +++ b/invCount/aib/9ed59fa0-a9ba-4841-b6a5-bd88d5f4ea5a.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n,i,x,aib[100005],v[100005],a[100005],b[100005]; + +long long s; + +int ub(int x) + +{ + + return x & (-x); + +} + +void update(int x) + +{ + + for(int i=x;i<=n;i+=ub(i)) + + aib[i]++; + +} + +int query(int x) + +{ + + long long s=0; + + for(int i=x;i>=1;i-=ub(i)) + + s+=aib[i]; + + return s; + +} + +void normalizare() + +{ + + for(int i=1;i<=n;i++) + + b[i]=a[i]; + + sort(b+1,b+n+1); + + for(int i=1;i<=n;i++) + + { + + int st=1,p=0; + + int dr=n; + + int x=a[i]; + + while(st<=dr) + + { + + int mij=(st+dr)/2; + + if(x<=b[mij]) + + { + + p=mij; + + dr=mij-1; + + } + + else st=mij+1; + + } + + v[i]=p; + + } + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + f>>a[i]; + + normalizare(); + + for(i=n;i>=1;i--) + + { + + x=v[i]; + + update(x); + + s+=query(x-1); + + } + + g< +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i +#include +#include + + + + + + +using namespace std; + +vector < pair< int,int > > V; + +int A[100001]; + +int N,i,x,k; + +long long sol; + +void add(int x) + +{ + +int i=0; + +for (i=x;i<=N;i+=(i&(-i))) A[i]++; + +} + +int Sum(int x) + +{ + +int i=0,S=0; + +for (i=x;i>0;i-=(i&(-i))) S+=A[i]; + +return S; + +} + +int main() + +{ + +freopen("inv.in","r",stdin); + +freopen("inv.out","w",stdout); + +scanf("%d",&N); + +for (i=0;i + + + + +using namespace std; + +typedef long long ll; + + + +const int MOD = 9917; + + + +int aib[100005],n; + + + +int sum(int x){ + + int s = 0; + + for(int i = x;i > 0;i -= i&(-i)) + + s += aib[i]; + + return s; + +} + + + +void update(int x){ + + for(int i = x;i <= n;i += i&(-i)) + + aib[i]++; + +} + +vector > v; + +int main() + +{ + + int i; + + ll rez = 0; + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin >> n; + + for(i = 1;i <= n;i++){ + + int x; + + cin >> x; + + v.push_back({x,i}); + + } + + sort(v.begin(),v.end()); + + i = 1; + + for(auto x : v){ + + rez += 1LL * (i - 1 - sum(x.second - 1)); + + rez %= MOD; + + update(x.second); + + i++; + + } + + cout << rez; + + return 0; + +} diff --git a/invCount/aib/a1545ccd-749d-4bfc-8d17-233c78e24896.cpp b/invCount/aib/a1545ccd-749d-4bfc-8d17-233c78e24896.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/a1545ccd-749d-4bfc-8d17-233c78e24896.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/a2103b89-a446-4e6c-b8e3-3e169cf7bba9.cpp b/invCount/aib/a2103b89-a446-4e6c-b8e3-3e169cf7bba9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4968b40fed2bb587bcf010baa57411cb773b9374 --- /dev/null +++ b/invCount/aib/a2103b89-a446-4e6c-b8e3-3e169cf7bba9.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector < pair > v; + + + +int n, aib[100001]; + +const int MOD = 9917; + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + { + + aib[i] += val; + + if (aib[i] > MOD) aib[i] -= MOD; + + } + +} + + + +int64_t Query(int p) + +{ + + int64_t suma = 0; + + + + for (int i = p; i >= 1; i -= i & -i) + + { + + suma += aib[i]; + + if (suma > MOD) suma -= MOD; + + } + + return suma; + +} + + + +int main() + +{ + + int a; + + fin >> n; + + + + v.resize(n); + + + + for (int i = 0; i < n; ++i) + + { + + fin >> a; + + v[i] = {a, i + 1}; + + } + + sort(v.begin(), v.end(), greater > ()); + + + + int64_t cnt = 0; + + + + for (int i = 0; i < n; ++i) + + { + + cnt += Query(v[i].second); + + if (cnt > MOD) cnt -= MOD; + + Update(v[i].second, 1); + + } + + + + fout << cnt << "\n"; + + + + return 0; + +} diff --git a/invCount/aib/a22237c2-8d7b-44d9-85f7-0788cc16f76d.cpp b/invCount/aib/a22237c2-8d7b-44d9-85f7-0788cc16f76d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e73c0f7e40ee502d1e73bfe9162a3e503b8e642f --- /dev/null +++ b/invCount/aib/a22237c2-8d7b-44d9-85f7-0788cc16f76d.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + int x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct Element + +{ + + int value; + + int index; + + bool operator < (const Element &a) + + { + + return value < a.value || (value == a.value && index < a.index); + + } + +}; + + + +int N; + + + +Element A[100000 + 5]; + +int AIB[100000 + 5]; + + + +static inline void update (int poz, int value); + +static inline int query (int poz); + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; ++i) + + fin >> A[i].value, A[i].index = i; + + sort (A + 1, A + N + 1); + + long long sum = 0; + + for (int i = 1; i <= N; ++i) + + { + + sum += 1LL * (query (A[i].index)); + + update (A[i].index, 1); + + } + + long long ans = (1LL * N * (N - 1) / 2) - sum; + + fout << ans % 9917; + + return 0; + +} + + + +static inline void update (int poz, int value) + +{ + + while (poz <= N) + + AIB[poz] += value, poz += (poz&(-poz)); + +} + + + +static inline int query (int poz) + +{ + + int ans = 0; + + while (poz) + + ans = (ans + AIB[poz]) % 9917, poz -= (poz&(-poz)); + + return ans; + + + +} diff --git a/invCount/aib/a5b5b7ff-2d6c-436b-8a5e-b15e7811b0aa.cpp b/invCount/aib/a5b5b7ff-2d6c-436b-8a5e-b15e7811b0aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfdc76184b01a0d1dc438bbc9742767ad2cd15b4 --- /dev/null +++ b/invCount/aib/a5b5b7ff-2d6c-436b-8a5e-b15e7811b0aa.cpp @@ -0,0 +1,111 @@ +#include +#include +using namespace std; + + + +FILE *fin = fopen("inv.in", "r"); + +FILE *fout = fopen("inv.out", "w"); + + + +struct tablou {int x; int poz;} a[100005]; + + + +int n,i,aib[100005]; + +long long total; + + + +bool comparare(tablou a, tablou b) + +{ + + if(a.x!=b.x) return (a.x < b.x); + + return(a.poz=1; i-=i&(-i)) + + ans += aib[i]; + + + + return ans; + +} + + + +int main() + +{ + + fscanf(fin, "%d", &n); + + for(i=1; i<=n; ++i) + + { + + fscanf(fin, "%d", &a[i].x); + + a[i].poz = i; + + } + + + + sort(a+1, a+n+1, comparare); + + + + for(i=1; i<=n; ++i) + + { + + total += 1LL*sum(a[i].poz); + + update(a[i].poz); + + } + + + + fprintf(fout, "%d", (1LL*n*(n-1)/2 - total)%9917); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invCount/aib/a729c11d-182b-4925-b17a-945c931b9379.cpp b/invCount/aib/a729c11d-182b-4925-b17a-945c931b9379.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4968b40fed2bb587bcf010baa57411cb773b9374 --- /dev/null +++ b/invCount/aib/a729c11d-182b-4925-b17a-945c931b9379.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector < pair > v; + + + +int n, aib[100001]; + +const int MOD = 9917; + + + +void Update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + { + + aib[i] += val; + + if (aib[i] > MOD) aib[i] -= MOD; + + } + +} + + + +int64_t Query(int p) + +{ + + int64_t suma = 0; + + + + for (int i = p; i >= 1; i -= i & -i) + + { + + suma += aib[i]; + + if (suma > MOD) suma -= MOD; + + } + + return suma; + +} + + + +int main() + +{ + + int a; + + fin >> n; + + + + v.resize(n); + + + + for (int i = 0; i < n; ++i) + + { + + fin >> a; + + v[i] = {a, i + 1}; + + } + + sort(v.begin(), v.end(), greater > ()); + + + + int64_t cnt = 0; + + + + for (int i = 0; i < n; ++i) + + { + + cnt += Query(v[i].second); + + if (cnt > MOD) cnt -= MOD; + + Update(v[i].second, 1); + + } + + + + fout << cnt << "\n"; + + + + return 0; + +} diff --git a/invCount/aib/a867aa84-0398-4aa7-8cc6-9403f96193ea.cpp b/invCount/aib/a867aa84-0398-4aa7-8cc6-9403f96193ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42f8a9c972ece180291d4af45a53ec55d170aadc --- /dev/null +++ b/invCount/aib/a867aa84-0398-4aa7-8cc6-9403f96193ea.cpp @@ -0,0 +1,145 @@ +#include + + + + + + +using namespace std; + + + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); +int n; + +int aib[100010]; + +int a[100010]; + +int b[100010]; + + + +int cb(int val, int left, int right) { + + if(left > right) { + + return -1; + + } + + int mijl = (left+right) / 2; + + if(b[mijl] == val && b[mijl -1] < val) { + + return mijl; + + } + + if(b[mijl] < val) { + + return cb(val, mijl + 1, right); + + } else { + + return cb(val, left, mijl); + + } + +} + + + +void adauga(int x) { + + for(int i = x; i <= n; i += i & (-i)) { + + aib[i]++; + + } + +} + + + +int scoate(int x) { + + int sol = 0; + + for(int i=x; i >= 1; i -= i & (-i)) { + + sol += aib[i]; + + } + + return sol; + +} + + + +void solve() { + + fin >> n; + + + + for(int i=1; i <=n; i++) { + + fin >> a[i]; + + b[i] = a[i]; + + } + + sort(b + 1, b + n + 1); + + + + int sol = 0; + + + + for(int i=1; i <= n; i++) { + + int x =cb(a[i], 1, n); + + sol = (sol + i - 1 - scoate(x)) % 9917; + + adauga(x); + + } + + fout << sol; + +} + + + + + +int main() { + + ios_base::sync_with_stdio(0); + + fin .tie(0); + + fout.tie(0); + + + + int testCases = 1; + + + + while(testCases--) { + + solve(); + + } + +} diff --git a/invCount/aib/a8b9523f-3d3f-405b-a6d7-dee67c6ca7b7.cpp b/invCount/aib/a8b9523f-3d3f-405b-a6d7-dee67c6ca7b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07f05906556295b41bd697a7313fad82d937d94a --- /dev/null +++ b/invCount/aib/a8b9523f-3d3f-405b-a6d7-dee67c6ca7b7.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=1; i<=N; ++i) + + { + + int X=i-1-Query(A[i]); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i, nrInv = 0; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv = (nrInv + x - sum(x) - 1) % MOD; + + add(x); + + } + + g << nrInv; + + return 0; + +} diff --git a/invCount/aib/aab85571-9dca-4cfc-82e1-697adffc579c.cpp b/invCount/aib/aab85571-9dca-4cfc-82e1-697adffc579c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5650ede2c2d3a631e5abdeabdc3577119b2ba8b --- /dev/null +++ b/invCount/aib/aab85571-9dca-4cfc-82e1-697adffc579c.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + + +string name = "inv"; + + +ifstream fin(name + ".in"); +ofstream fout(name + ".out"); + + + + +int n; +vector a; + +int f[100005]; + +inline int nrz(int x) { + return x & -x; +} + +int query(int x) { + int s = 0; + + while (x > 0) { + s += f[x]; + x -= nrz(x); + } + + return s; +} + +int query(int a, int b) { + return query(b) - query(a - 1); +} + +void update(int x) { + while (x <= n) { + f[x]++; + x += nrz(x); + } +} + +struct Item { + int v; + int i; +}; + +int main() { + + fin >> n; + + auto compare = [](Item lhs, Item rhs) { + return lhs.v > rhs.v; + }; + + priority_queue, decltype(compare)> pq(compare); + + for (int i = 0; i > x; + a.push_back(x); + pq.push({x, i}); + } + + int v = 0; + int last = -0x3f3f3f3f; + + while (!pq.empty()) { + auto x = pq.top(); + pq.pop(); + + if (x.v != last) { + last = x.v; + v++; + } + + a[x.i] = v; + } + + int result = 0; + + for (int i = 0; i < n; ++i) { + int x = a[i]; + + if (x < n) { + result += query(x + 1, n); + result %= 9917; + } + + update(x); + } + + fout << result; + + return 0; +} diff --git a/invCount/aib/ac24d18e-d43f-4fb2-ac04-eab0de302637.cpp b/invCount/aib/ac24d18e-d43f-4fb2-ac04-eab0de302637.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25ef47fe707d5f4a54149c23060ab3b845d5661f --- /dev/null +++ b/invCount/aib/ac24d18e-d43f-4fb2-ac04-eab0de302637.cpp @@ -0,0 +1,104 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[100010],v2[100010], aib[100010],ans; + +unordered_map M; + +void update(int nr) + +{ + + for(int i = nr; i <= n; i += (i & (-i))) + + { + + aib[i] ++; + + } + + + +} + +int suma(int nod) + +{ + + int sum = 0; + + for(int i = nod; i; i -= (i & (-i)) ) + + { + + sum += aib[i]; + + sum %= 9917; + + } + + return sum; + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> v[i], v2[i] = v[i]; + + sort(v + 1, v + n + 1); + + int p = 0; + + v[0] = -1; + + for(int i = 1; i <= n; ++i) + + { + + if(v[i] != v[i-1]) + + p++; + + M[v[i]] = p; + + } + + for(int i = n; i >= 1; --i) + + { + + ans += suma(M[v2[i]] - 1); + + ans %= 9917; + + update(M[v2[i]]); + + } + + g << ans; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/ae1f7e0b-52bd-403f-b191-455af99c02fb.cpp b/invCount/aib/ae1f7e0b-52bd-403f-b191-455af99c02fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f778ddab2d48cc27b572165735bebe796ca92469 --- /dev/null +++ b/invCount/aib/ae1f7e0b-52bd-403f-b191-455af99c02fb.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int NMAX = 100000, + + MOD = 9917; + + + +struct elem { + + int poz, val; + +}; + +int N, AIB[NMAX+1]; + +elem V[NMAX+1]; + + + +bool comp(const elem& a, const elem& b) { + + if(a.val == b.val) return a.poz < b.poz; + + else return a.val < b.val; + +} + + + +void update(int p) { + + while(p <= N) { + + AIB[p]++; + + p += p & (-p); + + } + +} + + + +int query(int p) { + + int s = 0; + + while(p > 0) { + + s += AIB[p]; + + p -= p & (-p); + + } + + return s; + +} + + + +int main() + +{ + + int x, nrInv = 0; + + in >> N; + + for(int i = 1; i <= N; i++) { + + in >> x; + + V[i] = {i, x}; + + } + + sort(V+1, V+N+1, comp); + + + + for(int i = 1; i <= N; i++) { + + x = V[i].poz; + + nrInv = (nrInv + x - query(x) - 1) % MOD; + + update(x); + + } + + + + out << nrInv; + + return 0; + +} diff --git a/invCount/aib/afe4a79e-8d29-428d-b87c-f47a14c051cc.cpp b/invCount/aib/afe4a79e-8d29-428d-b87c-f47a14c051cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9e8e92c0ba9842cb35c0b4ae0c88151ce8bad12 --- /dev/null +++ b/invCount/aib/afe4a79e-8d29-428d-b87c-f47a14c051cc.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(; p>=1; p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(; p<=k; p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + + + for(i=1; i<=n; i++) + + { + + if(w[i].first!=w[i-1].first) + + k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++) + + { + + + + + + + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< +#include +#include + +using namespace std; + +const int NMAX = 100010; +int aib[NMAX]; +int n; + +void update(int poz) +{ + while (poz <= n) + aib[poz]++, poz += ((poz) & -(poz)); +} +int query(int poz) +{ + int ans = 0; + while (poz) + ans += aib[poz], poz -= ((poz) & -(poz)); + return ans; +} + +int v[NMAX]; + +int main() +{ + FILE * in = fopen("inv.in", "r"); + FILE * out = fopen("inv.out", "w"); + + vector nrz; + + fscanf(in, "%d", &n); + + for (int i = 1; i <= n; i++) { + fscanf(in, "%d", v + i); + nrz.push_back(v[i]); + } + + sort(nrz.begin(), nrz.end()); + nrz.resize(unique(nrz.begin(), nrz.end()) - nrz.begin()); + + for (int i = 1; i <= n; i++) + v[i] = lower_bound(nrz.begin(), nrz.end(), v[i]) - nrz.begin() + 1; + + long long ans = 0; + + for (int i = 1; i <= n; i++) { + update(v[i]); + ans += i - query(v[i]); + } + + fprintf(out, "%lld\n", ans % 9917); + + return 0; +} diff --git a/invCount/aib/b2387fcf-c4e3-4234-be8a-fb1408a4726d.cpp b/invCount/aib/b2387fcf-c4e3-4234-be8a-fb1408a4726d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..430c1e27be7f3ff748893fd82e7fc1fe4161277a --- /dev/null +++ b/invCount/aib/b2387fcf-c4e3-4234-be8a-fb1408a4726d.cpp @@ -0,0 +1 @@ +#include using namespace std; ifstream f("inv.in"); ofstream g("inv.out"); #define ub(x) (x&(-x)) #define MOD 9917 int n,i,p,nr,AIB[100005],x; long long inv; vector >v; long long sol; void add(int x) { for(int j=x; j<=n; j+=ub(j)) AIB[j]++; } long long sum(int x) { long long s=0; for(int j=x; j>0; j-=ub(j)) s+=AIB[j]; return s; } int main() { f>>n; for(i=0;i>x; v.push_back(make_pair(x,i+1)); } sort(v.begin(),v.end()); inv=1LL*n*(n-1)/2; for(i=0; i + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int N=200009; + +struct xd + +{ + + long long x; + + int poz; + +}a[N]; + +int n; + +int aib[N],v[N]; + +int compare(xd a,xd b) + +{ + + if(a.x!=b.x) + + return a.x>n; + + for(int i=1;i<=n;i++) + + fin>>a[i].x,a[i].poz=i; + +} + +void solve() + +{ + + sort(a+1,a+n+1,compare); + + for(int i=1;i<=n;i++) + + v[a[i].poz]=i; + + int s=0; + + for(int i=1;i<=n;i++) + + { + + s+=query(n)-query(v[i]); + + s%=9917; + + update(v[i],1); + + } + + fout< +#include + + +using namespace std; + +int sum,b,p,i,x,n,aib[101*1001]; + + + +int sol(int x){ + +int i; + +int rez=0; + +for(i=x;i>0;i-=(i&(-i))) + + rez+=aib[i]; + +return rez; + + + +} + + + +void add(int x,int n){ + +int i; + +for(i=x;i<=n;i+=(i&(-i))) aib[i]++; + + + +} + +int ok=0; + +struct mch{ + +int val,ord; + +} a[101*1001]; + +int compare( mch a,mch b){ + +if(a.val==b.val) return a.ord + + + + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int v[100001]; + +pair ss[100001]; + +int AIB[100001]; + +int n; + +void update(int i, int val) + +{ + + while(i<=n) + + { + + AIB[i]+=val; + + i+=(i&(-i)); + + } + +} + +int query(int i) + +{ + + int s=0; + + while(i>0) + + { + + s+=AIB[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + ios_base::sync_with_stdio(false); + + int i; + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>ss[i].first; + + ss[i].second=i; + + } + + sort(ss+1,ss+n+1); + + for(i=1;i<=n;i++) + + v[ss[i].second]=i; + + for(i=1;i<=n;i++) + + update(v[i],1); + + int ans=0; + + for(i=1;i<=n;i++) + + { + + ans=(ans+query(v[i])-1+9917)%9917; + + update(v[i],-1); + + } + + g< +#include + + + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,a,b,k,v[100005],aib[100005],nr,sol,poz; + +struct numar{ + + int x,p; + +}aa[100005]; + + + +int cmp(numar a, numar b){ + + if(a.x==b.x) + + return a.p0){ + + s=(s+aib[x])%9917; + + x-=(x&(-x)); + + } + + return s; + +} + + + +void update(int x){ + + while(x<=k){ + + aib[x]++; + + if(aib[x]>=9917) + + aib[x]-=9917; + + x+=(x&(-x)); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>aa[i].x; + + aa[i].p=i; + + } + + sort(aa+1,aa+n+1,cmp); + + k=1; + + for(i=1;i<=n;i++){ + + if(aa[i].x!=aa[i-1].x) + + k++; + + v[aa[i].p]=k; + + } + + for(i=1;i<=n;i++){ + + nr=query(k)-query(v[i]); + + if(nr<0) + + nr+=9917; + + sol+=nr; + + if(sol>=9917) + + sol-=9917; + + update(v[i]); + + } + + fout< +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n,i,x,aib[100005],v[100005],a[100005],b[100005]; + +long long s; + +int ub(int x) + +{ + + return x & (-x); + +} + +void update(int x) + +{ + + for(int i=x;i<=n;i+=ub(i)) + + aib[i]++; + +} + +int query(int x) + +{ + + long long s=0; + + for(int i=x;i>=1;i-=ub(i)) + + s+=aib[i]; + + return s; + +} + +void normalizare() + +{ + + for(int i=1;i<=n;i++) + + b[i]=a[i]; + + sort(b+1,b+n+1); + + for(int i=1;i<=n;i++) + + { + + int st=1,p=0; + + int dr=n; + + int x=a[i]; + + while(st<=dr) + + { + + int mij=(st+dr)/2; + + if(x<=b[mij]) + + { + + p=mij; + + dr=mij-1; + + } + + else st=mij+1; + + } + + v[i]=p; + + } + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + f>>a[i]; + + normalizare(); + + for(i=n;i>=1;i--) + + { + + x=v[i]; + + update(x); + + s+=query(x-1); + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n, v[100010],v2[100010], aib[100010],ans; + +unordered_map M; + +void update(int nr) + +{ + + for(int i = nr; i <= n; i += (i & (-i))) + + { + + aib[i] ++; + + } + + + +} + +int suma(int nod) + +{ + + int sum = 0; + + for(int i = nod; i; i -= (i & (-i)) ) + + { + + sum += aib[i]; + + sum %= 9917; + + } + + return sum; + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; ++i) + + f >> v[i], v2[i] = v[i]; + + sort(v + 1, v + n + 1); + + int p = 0; + + v[0] = -1; + + for(int i = 1; i <= n; ++i) + + { + + if(v[i] != v[i-1]) + + p++; + + M[v[i]] = p; + + } + + for(int i = n; i >= 1; --i) + + { + + ans += suma(M[v2[i]] - 1); + + ans %= 9917; + + update(M[v2[i]]); + + } + + g << ans; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/b7c33e0f-2e79-4c8c-81e7-f8c029d5e3b2.cpp b/invCount/aib/b7c33e0f-2e79-4c8c-81e7-f8c029d5e3b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15d083cea4848836f021c8b833b07cad03c55427 --- /dev/null +++ b/invCount/aib/b7c33e0f-2e79-4c8c-81e7-f8c029d5e3b2.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,f[100010],w[100010],cnt,sol; + +pair v[100010]; + +void update(int x) { + + for (;x<=n;x+=(x&-x)) + + f[x]++; + +} + +int query(int x) { + + int val=0; + + for (;x;x-=(x&-x)) + + val+=f[x]; + + return val; + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) { + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for (i=1;i<=n;i++) { + + if (v[i].first==v[i-1].first) { + + w[v[i].second]=w[v[i-1].second]; + + cnt++; + + } + + else + + w[v[i].second]=i-cnt; + + } + + for (i=1;i<=n;i++) { + + sol=(sol+query(n-cnt)-query(w[i]))%9917; + + update(w[i]); + + } + + fout< +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int MOD = 9917; + +const int N = 100000; + + + +int aib[N + 1]; + +struct nr{ + + int val, index; + +} v[N + 1]; + + + +int index[N + 1]; + + + +bool cmp(nr a, nr b){ + + if(a.val > b.val) + + return true; + + else if(a.val == b.val && a.index > b.index) + + return true; + + return false; + +} + + + +int sum(int x){ + + int ans = 0; + + while(x){ + + ans += aib[x]; + + x -= (x & (-x)); + + } + + return ans; + +} + + + +int n; + + + +void add(int x, int val){ + + while(x <= n){ + + aib[x] += val; + + x += (x & (-x)); + + } + +} + + + +int main() + +{ + + int i,ans; + + fin >> n; + + for(i=1; i <= n; i++){ + + fin >> v[i].val; + + v[i].index = i; + + } + + sort(v + 1, v + n + 1, cmp); + + ans = 0; + + for(i=1; i<=n; i++){ + + ans = (ans + sum(v[i].index - 1)) % MOD; + + add(v[i].index, 1); + + } + + fout << ans << "\n"; + + return 0; + +} diff --git a/invCount/aib/b9fb832e-bf25-4a8b-a454-ec7d389b7590.cpp b/invCount/aib/b9fb832e-bf25-4a8b-a454-ec7d389b7590.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d01efb1e666867590cb00fab8b09f5cf2e1b842e --- /dev/null +++ b/invCount/aib/b9fb832e-bf25-4a8b-a454-ec7d389b7590.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/ba5cdef6-498f-49c3-bfca-0edb2d29a5cd.cpp b/invCount/aib/ba5cdef6-498f-49c3-bfca-0edb2d29a5cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59c6541ee39eed39deba17722d50b0e7c9f94a2f --- /dev/null +++ b/invCount/aib/ba5cdef6-498f-49c3-bfca-0edb2d29a5cd.cpp @@ -0,0 +1,106 @@ +#include + + + + +using namespace std; + + + +const int mxn = 100 * 1000 + 10, md = 9917; + + + +int arb[ 4 * mxn ]; + + + +pair< int, int > sir[ mxn ]; + +int sol[ mxn ]; + + + +int n; + + + +void update(int nod, int st, int sf, int poz){ + + arb[ nod ]++; + + arb[ nod ] %= md; + + if(st < sf){ + + int mid = (st + sf) / 2; + + if(mid >= poz) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, sf, poz); + + } + +} + + + +int query(int nod, int st, int sf, int poz){ + + if(poz <= st) + + return arb[ nod ]; + + else if(sf < poz) + + return 0; + + int mid = (st + sf)/2; + + return (query(2 * nod, st, mid, poz) + query(2 * nod + 1, mid + 1, sf, poz)) % md; + +} + + + +int main() + +{ + + ifstream cin("inv.in"); + + ofstream cout("inv.out"); + + cin>> n; + + for(int i = 0, x; i < n; i++){ + + cin>> x; + + sir[ i ] = make_pair(x, i); + + } + + sort(sir, sir + n); + + int s = 0; + + for(int i = 0; i < n; i++){ + + s += query(1, 0, n - 1, sir[ i ].second); + + s %= md; + + update(1, 0, n - 1, sir[ i ].second); + + } + + cout<< s; + + return 0; + +} diff --git a/invCount/aib/bb32fc80-096d-4a31-81ef-b674a30d879f.cpp b/invCount/aib/bb32fc80-096d-4a31-81ef-b674a30d879f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a271a8935a9107b533eca8ae071efea6a93fa04a --- /dev/null +++ b/invCount/aib/bb32fc80-096d-4a31-81ef-b674a30d879f.cpp @@ -0,0 +1,152 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + + + +int aib[100005], x, n, sol; + + + +struct nr + +{ + + int val, poz; + +}; + + + +nr v[100005]; + + + +void update (int poz, int val) + +{ + + for (int i = poz; i <= n; i += (i&(-i))) + + { + + aib[i] += val; + + } + +} + + + +int suma (int poz) + +{ + + int s = 0; + + for (int i = poz; i > 0; i -= (i&(-i))) + + { + + s = (s + aib[i]) % 9917; + + } + + + + return s; + +} + + + +bool cmp1 (nr a, nr b) + +{ + + if (a.val > b.val) return false; + + + + if (a.val == b.val && a.poz > b.poz) return false; + + + + return true; + +} + + + +bool cmp2 (nr a, nr b) + +{ + + if (a.poz > b.poz) return false; + + + + return true; + +} + +int main() + +{ + + f >> n; + + + + for (int i = 1; i <= n; i++) + + { + + f >> v[i].val; + + v[i].poz = i; + + } + + + + sort(v + 1, v + n + 1, cmp1); + + for (int i = 1; i <= n; i++) + + v[i].val = i; + + + + sort(v + 1, v + n + 1, cmp2); + + + + for (int i = 1; i <= n; i++) + + { + + sol = (sol + suma(v[i].val)) % 9917; + + update(v[i].val, 1); + + } + + + + g << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + + return 0; + +} diff --git a/invCount/aib/bc27fa31-7f37-4526-9ca4-f9657e017c26.cpp b/invCount/aib/bc27fa31-7f37-4526-9ca4-f9657e017c26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b130925c6497d5e8d316c23c10055b80a13ed3 --- /dev/null +++ b/invCount/aib/bc27fa31-7f37-4526-9ca4-f9657e017c26.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int MOD = 9917; + + + +int tree[100001]; + +pair v[100001]; + +int n; + + + +void update(int poz) + +{ + + for(int i = poz; i <= n; i += i&-i) + + tree[i]++; + +} + + + +int64_t query(int poz) + +{ + + int s = 0; + + + + for(int i = poz; i; i -= i&-i) + + s += tree[i]%MOD; + + + + return s%MOD; + +} + + + +int main() + +{ + + in >> n; + + + + for(int i = 1; i <= n; i++) + + { + + in >> v[i].first; + + v[i].second = i; + + } + + + + sort(v+1, v+n+1); + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << v[i].first << ' ' << v[i].second << '\n'; + + } + + + + if(!1) + + { + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + + + + + for(int i = 1; i <= n; i++) + + cout << i << ' ' << tree[i] << '\n'; + + cout << '\n'; + + cout << query(2) << ' ' << query(4); + + } + + + + int64_t rez = 0; + + for(int i = n; i; i--) + + { + + if(!1) + + { + + if(query(v[i].second)) + + cout << query(v[i].second) << ' ' << v[i].first << ' ' << v[i].second << '\n'; + + } + + rez += query(v[i].second)%MOD; + + update(v[i].second); + + + + } + + out << rez%MOD; + + + + return 0; + +} diff --git a/invCount/aib/be7899ab-9a39-4f8a-9732-141fecea45f5.cpp b/invCount/aib/be7899ab-9a39-4f8a-9732-141fecea45f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df47bba76a132249128f31250695f97f92a0e7bf --- /dev/null +++ b/invCount/aib/be7899ab-9a39-4f8a-9732-141fecea45f5.cpp @@ -0,0 +1,136 @@ +#include + + + + +using namespace std; + + + +void solve() { + + int n; + + cin >> n; + + + + vector< int > v(n); + + + + for(int i = 0; i < n; ++i) cin >> v[i]; + + + + + + vector< int > actual = v; + + sort(actual.begin(), actual.end()); + + + + actual.erase(unique(actual.begin(), actual.end()), actual.end()); + + + + for(auto &x : v) { + + int l = 0, r = actual.size() - 1; + + int idx = 0; + + while(l <= r) { + + int m = l + r >> 1; + + if(actual[m] == x) { + + idx = m; + + break; + + } + + if(actual[m] < x) { + + l = m + 1; + + } else r = m - 1; + + } + + + + + + x = idx + 1; + + } + + + + vector< int > aib(actual.size() + 1); + + + + auto update = [&](int p) { + + for(; p < aib.size(); p += p&-p) ++aib[p]; + + }; + + + + auto query = [&](int p) { + + int ret = 0; + + for(;p;p-=p&-p) ret += aib[p]; + + return ret; + + }; + + + + long long ans = 0; + + + + for(int i = 0; i < n; ++i) { + + ans += i - query(v[i]); + + update(v[i]); + + } + + + + cout << ans % 9917<< '\n'; + +} + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + ios::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + + + + + solve(); + +} diff --git a/invCount/aib/bf60785e-1f29-4ef5-abab-51a6f49bfd7b.cpp b/invCount/aib/bf60785e-1f29-4ef5-abab-51a6f49bfd7b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8468e7fdfa2c891cc6d44dcc55a268c4da71a54 --- /dev/null +++ b/invCount/aib/bf60785e-1f29-4ef5-abab-51a6f49bfd7b.cpp @@ -0,0 +1,43 @@ +#include + + + +using namespace std; + +struct Fenwick { + int n; + vector t; + Fenwick(int n_) : n(n_), t(n + 1) {} + void Update(int pos, int val) { + for (++pos; pos <= n; pos += pos & -pos) t[pos] += val; + } + int Query(int pos) { + int ans = 0; + for (++pos; pos; pos -= pos & -pos) ans += t[pos]; + return ans; + } +}; + +int main() { + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + int n; cin >> n; + vector v(n); + for (int i = 0; i < n; ++i) cin >> v[i]; + auto all = v; + all.emplace_back(-(int)1e9); + sort(all.begin(), all.end()); + all.erase(unique(all.begin(), all.end()), all.end()); + for (int i = 0; i < n; ++i) { + v[i] = lower_bound(all.begin(), all.end(), v[i]) - all.begin(); + } + + Fenwick fw(n); + int64_t ans = 0; + for (int i = n - 1; i >= 0; --i) { + ans += fw.Query(v[i] - 1); + fw.Update(v[i], 1); + } + cout << ans % 9917 << endl; +} diff --git a/invCount/aib/c08f9450-c600-4204-a001-56f91257adce.cpp b/invCount/aib/c08f9450-c600-4204-a001-56f91257adce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c3ac6b7db3479750d1cb8e38b2f69d38ef53c8 --- /dev/null +++ b/invCount/aib/c08f9450-c600-4204-a001-56f91257adce.cpp @@ -0,0 +1,199 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + +Trei a[100005]; + + + +int n, aib[100005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort (a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + sort (a + 1, a + n + 1, Compara2); + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/aib/c4d4ebcd-7cef-473a-86d3-8c0ff277e4b4.cpp b/invCount/aib/c4d4ebcd-7cef-473a-86d3-8c0ff277e4b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/c4d4ebcd-7cef-473a-86d3-8c0ff277e4b4.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/c533b4e5-7da6-4009-9b14-ebb3564544aa.cpp b/invCount/aib/c533b4e5-7da6-4009-9b14-ebb3564544aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ed8afa0e8a066cdb3488897951b0e3fea145d2e --- /dev/null +++ b/invCount/aib/c533b4e5-7da6-4009-9b14-ebb3564544aa.cpp @@ -0,0 +1,183 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + +const int MOD = 9917; + + + +struct nume{ + + int node,ind; + +}; + + + +vector vec; + + + +int V[DMAX]; + +int tree[4*DMAX]; + + + +int n,cate; + +ll ans; + + + +inline bool operator<(nume x,nume y){ + + return x.node>n; + + for(i=1;i<=n;i++){ + + fin>>V[i]; + + vec.push_back({V[i],i}); + + } + + sort(vec.begin(),vec.end()); + + int prec=-1e9; + + for(auto& it:vec){ + + if(it.node == prec){ + + V[it.ind]=dif; + + continue; + + } + + dif++; + + prec=it.node; + + V[it.ind]=dif; + + } + + for(i=n;i>=1;i--){ + + if(V[i] > 1) + + ans+=query(1,1,n,V[i]-1); + + update(1,1,n,V[i]); + + } + + fout<= pos) + + ans+=query(2*node,st,mij,pos); + + return ans; + +} diff --git a/invCount/aib/c7c83130-91d6-48ad-a702-da0fbdf4c5d9.cpp b/invCount/aib/c7c83130-91d6-48ad-a702-da0fbdf4c5d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fc5032aef800cc83913bd69d2e33d8092d8e800 --- /dev/null +++ b/invCount/aib/c7c83130-91d6-48ad-a702-da0fbdf4c5d9.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n,i,k,v[100001],a[100001],ord[100001]; + +long long sol; + + + +inline int cb(int x){ + + int st=1; + + int dr=k; + + int mid; + + + + while(st<=dr){ + + mid=(st+dr)/2; + + + + if(x==ord[mid]) + + return mid; + + + + if(x>n; + + for(i=1;i<=n;i++) + + fin>>a[i],ord[i]=a[i]; + + sort(ord+1,ord+n+1); + + + + ord[0]=-1; + + for(i=1;i<=n;i++) + + if(ord[i]!=ord[k]) + + ord[++k]=ord[i]; + + + + for(i=1;i<=n;i++) + + a[i]=cb(a[i]); + + + + for(i=1;i<=n;i++){ + + sol=(sol+i-1-query(a[i]))%9917; + + upd(a[i]); + + } + + + + fout< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,f[100010],w[100010],cnt,sol; + +pair v[100010]; + +void update(int x) { + + for (;x<=n;x+=(x&-x)) + + f[x]++; + +} + +int query(int x) { + + int val=0; + + for (;x;x-=(x&-x)) + + val+=f[x]; + + return val; + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) { + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for (i=1;i<=n;i++) { + + if (v[i].first==v[i-1].first) { + + w[v[i].second]=w[v[i-1].second]; + + cnt++; + + } + + else + + w[v[i].second]=i-cnt; + + } + + for (i=1;i<=n;i++) { + + sol=(sol+query(n-cnt)-query(w[i]))%9917; + + update(w[i]); + + } + + fout< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=1; i<=N; ++i) + + { + + int X=i-1-Query(A[i]); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< + + +using namespace std; + +ifstream f ("inv.in") ; + +ofstream g ("inv.out") ; + +int N , x , a , b , inversiune , sol[100005]; + +int AIB[100005]; + +int solutie; + +struct normalize + +{ + + int val; + + int poz; + +}v[100005]; + +bool cmp (normalize a , normalize b) + +{ + + if (a.val == b.val) return (a.poz < b.poz); + + return (a.val < b.val) ; + +} + +void Update (int poz , int val) + +{ + + for (int i = poz ; i <= N ; i += i & (-i)) + + AIB[i] += val; + + + + return; + +} + +int Query (int poz) + +{ + + int sum = 0 ; + + for (int i = poz ; i >= 1 ; i -= i & (-i)) + + sum += AIB[i] ; + + return sum ; + +} + +int main() + +{ + + f >> N ; + + for (int i = 1 ; i <= N ; ++i) + + { + + f >> v[i].val; + + v[i].poz = i ; + + } + + sort (v + 1 , v + N + 1 , cmp) ; + + + + for (int i = 1 ; i <= N ; ++i) + + sol[v[i].poz] = i ; + + + + + + + + for (int i = 1 ; i <= N ; ++i) + + { + + x = sol[i] ; + + inversiune = Query(N) - Query(x) ; + + solutie = (solutie + inversiune) % 9917; + + + + Update(x,1) ; + + } + + g << solutie ; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/d1085a7f-7bee-41d9-9e2c-2041205be2ba.cpp b/invCount/aib/d1085a7f-7bee-41d9-9e2c-2041205be2ba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cffedec6f8dd580ee4626052c632b0162fc9c400 --- /dev/null +++ b/invCount/aib/d1085a7f-7bee-41d9-9e2c-2041205be2ba.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX=1e5+5, MOD=9917; + + + +int A[NMAX], AIB[NMAX], N, B[NMAX], Sol[NMAX]; + + + +inline int UB (int X) + +{ + + return (X&(-X)); + +} + + + +inline void Update (int poz, int val) + +{ + + for(int i=poz; i<=N; i+=UB(i)) + + AIB[i]+=val; + +} + + + +inline int Query (int poz) + +{ + + int ans=0; + + + + for(int i=poz; i>=1; i-=UB(i)) + + ans+=AIB[i]; + + + + return ans; + +} + + + +inline void Read () + +{ + + f.tie(NULL); + + + + f>>N; + + + + for(int i=1; i<=N; ++i) + + { + + f>>A[i]; + + + + B[i]=A[i]; + + } + +} + + + +inline void Normalize () + +{ + + sort(B+1, B+N+1); + + + + Sol[++Sol[0]]=B[1]; + + + + for(int i=2; i<=N; ++i) + + if(B[i] != B[i-1]) + + Sol[++Sol[0]]=B[i]; + + + + for(int i=1; i<=N; ++i) + + A[i]=lower_bound(Sol+1, Sol+Sol[0]+1, A[i])-Sol; + +} + + + +inline void Solve () + +{ + + int ans=0; + + + + for(int i=N; i>=1; --i) + + { + + int X=Query(A[i]-1); + + + + Update(A[i], 1); + + + + ans+=X; + + + + ans%=MOD; + + } + + + + g< +#include + + +using namespace std; + + + +const int NMAX = 100001, + + MOD = 9917; + + + +struct numar + +{ + + int poz, val; + +}; + + + +int N, AiB[NMAX]; + +numar v[NMAX]; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +bool comp(const numar &a, const numar &b) + +{ + + if(a.val == b.val) + + return a.poz < b.poz; + + else + + return a.val < b.val; + +} + + + +void add(int poz) + +{ + + while(poz <= N) + + { + + AiB[poz]++; + + poz += poz & (-poz); + + } + +} + + + +int sum(int poz) + +{ + + int s = 0; + + while(poz > 0) + + { + + s += AiB[poz]; + + poz -= poz & (-poz); + + } + + return s; + +} + + + +int main() + +{ + + int x, i; + + f >> N; + + for(i = 1; i <= N; i++) + + { + + f >> x; + + v[i].poz = i; + + v[i].val = x; + + } + + sort(v + 1, v + N + 1, comp); + + long long nrInv = 1LL * N * (N - 1) / 2; + + for(i = 1; i <= N; i++) + + { + + x = v[i].poz; + + nrInv -= sum(x); + + add(x); + + } + + g << nrInv % MOD; + + return 0; + +} diff --git a/invCount/aib/d379e9d3-d083-42f2-abf7-ea2a8c286445.cpp b/invCount/aib/d379e9d3-d083-42f2-abf7-ea2a8c286445.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7cc72f6343b4dfad026eb6cd78db26fcb0510f3 --- /dev/null +++ b/invCount/aib/d379e9d3-d083-42f2-abf7-ea2a8c286445.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +using namespace std; + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + +const int lim=1e5+3; + +const int mod=9917; + +int v[lim],f[lim]; + +int tree[lim]; + +int n; + +vector upd; + +void sum(int &a,int b) + +{ + + a+=b; + + if(a>=mod) + + a-=mod; + +} + +void add(int k,int x) + +{ + + while(k<=n) + + { + + sum(tree[k],x); + + k+=k&-k; + + } + +} + +int cnt(int k) + +{ + + int rez=0; + + while(k>0) + + { + + sum(rez,tree[k]); + + k-=k&-k; + + } + + return rez; + +} + +bool ordine(int a,int b) + +{return f[a]>f[b];} + +int main() + +{ + + cin>>n; + + for(int i=1;i<=n;++i) + + { + + cin>>f[i]; + + v[i]=i; + + } + + sort(v+1,v+n+1,ordine); + + int ans=0; + + sum(ans,cnt(v[1])); + + upd.push_back(v[1]); + + for(int i=2;i<=n;++i) + + { + + if(f[v[i]]!=f[v[i-1]]) + + { + + for(int j=0;j +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int N, answer; + +pair v[100000 + 5]; + + + +int bit[100000 + 5]; + +vector pos; + + + +void update(int x) + +{ + + for (int i = x; i <= N; i += (((i - 1) ^ i) & i)) + + bit[i]++; + +} + + + +int query(int x) + +{ + + int ret = 0; + + for (int i = x; i >= 1; i -= (((i - 1) ^ i) & i)) + + ret += bit[i]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; i++) + + { + + fin >> v[i].first; + + v[i].second = i; + + } + + sort(v + 1, v + N + 1); + + reverse(v + 1, v + N + 1); + + for (int i = 1; i <= N; i++) + + { + + int j = i; + + while (j <= N && v[j].first == v[i].first) + + { + + pos.push_back(j); + + answer = (answer + query(v[j].second)) % 9917; + + j++; + + } + + for (auto &it: pos) + + update(v[it].second); + + pos.clear(); + + if (j > i) + + i = j - 1; + + } + + fout << answer << "\n"; + + return 0; + +} diff --git a/invCount/aib/d7a955ae-4367-457a-a5a0-999aa2ea1b30.cpp b/invCount/aib/d7a955ae-4367-457a-a5a0-999aa2ea1b30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7474dfc66abddbf3636b57a6d8411406cfa96e25 --- /dev/null +++ b/invCount/aib/d7a955ae-4367-457a-a5a0-999aa2ea1b30.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 1e5; + +const int MOD = 9917; + + + +int n; + + + +class AIB { + +private: + + int a[5 + N]; + + + + int lsb(int i){ + + return (i & (-i)); + + } + + + +public: + + void Update(int pos) { + + for(int i = pos; i >= 1; i -= lsb(i)) + + a[i]++; + + } + + + + int Query(int pos) { + + int s(0); + + + + for(int i = pos + 1; i <= n; i += lsb(i)) + + s += a[i]; + + return s; + + } + +}; + +AIB Fenw; + +pair v[5 + N]; + + + +bool cmp(pair a, pair b){ + + return a.second < b.second; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + int inv(0), nr; + + scanf("%d", &n); + + for(int i = 1; i <= n; i++){ + + scanf("%d", &nr); + + v[i] = make_pair(nr, i); + + } + + + + sort(v + 1, v + n + 1); + + for(int i = 1; i <= n; i++) { + + int j = i; + + while(v[j].first == v[j + 1].first && j <= n) j++; + + v[i].first = v[i - 1].first + 1; + + i++; + + + + while(i <= j) v[i].first = v[i - 1].first, i++; + + i--; + + } + + + + sort(v + 1, v + n + 1, cmp); + + + + for(int i = 1; i <= n; i++){ + + inv += Fenw.Query(v[i].first); + + Fenw.Update(v[i].first); + + + + inv %= MOD; + + } + + + + printf("%d\n", inv); + + return 0; + +} diff --git a/invCount/aib/d8fead5e-b7fc-4151-b8f7-1103565dcea4.cpp b/invCount/aib/d8fead5e-b7fc-4151-b8f7-1103565dcea4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce2bb323fa14ae1aad17541001d39be265e92f2c --- /dev/null +++ b/invCount/aib/d8fead5e-b7fc-4151-b8f7-1103565dcea4.cpp @@ -0,0 +1,83 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,i,nr,k,sol; + +pair v[100010]; + +int w[100010],f[100010]; + +void update(int p){ + + for(;p<=k;p+=(p&-p)) + + f[p]++; + +} + +int query(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=f[p]; + + return sol; + +} + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>v[i].first; + + v[i].second=i; + + } + + sort(v+1,v+n+1); + + for(i=1;i<=n;i++) + + if(v[i].first!=v[i-1].first) + + w[v[i].second]=i-nr; + + else{ + + nr++; + + w[v[i].second]=w[v[i-1].second]; + + } + + k=n-nr; + + for(i=1;i<=n;i++){ + + sol+=(query(k)-query(w[i])); + + sol%=9917; + + update(w[i]); + + } + + fout< +#include +#include +using namespace std; + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + +const int lim=1e5+3; + +const int mod=9917; + +int v[lim],f[lim]; + +int tree[lim]; + +int n; + +vector upd; + +void sum(int &a,int b) + +{ + + a+=b; + + if(a>=mod) + + a-=mod; + +} + +void add(int k,int x) + +{ + + while(k<=n) + + { + + sum(tree[k],x); + + k+=k&-k; + + } + +} + +int cnt(int k) + +{ + + int rez=0; + + while(k>0) + + { + + sum(rez,tree[k]); + + k-=k&-k; + + } + + return rez; + +} + +bool ordine(int a,int b) + +{return f[a]>f[b];} + +int main() + +{ + + cin>>n; + + for(int i=1;i<=n;++i) + + { + + cin>>f[i]; + + v[i]=i; + + } + + sort(v+1,v+n+1,ordine); + + int ans=0; + + sum(ans,cnt(v[1])); + + upd.push_back(v[1]); + + for(int i=2;i<=n;++i) + + { + + if(f[v[i]]!=f[v[i-1]]) + + { + + for(int j=0;j +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aib[100001], sol; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int x){ + + while(x <= k){ + + aib[x] = (aib[x] + 1) % 9917; + + x = x + (x & (-x)); + + } + +} + +int quary(int x){ + + int s = 0; + + while(x >= 1){ + + s = (s + aib[x]) % 9917; + + x = x - (x & (-x)); + + } + + return s; + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + for(i = 1; i <= n; i++){ + + int value = quary(k) - quary(x[i]); + + if(value < 0) + + value += 9917; + + sol = (sol + value) % 9917; + + update(x[i]); + + } + + g << sol; + + return 0; + +} diff --git a/invCount/aib/de0830dc-9d87-4b19-98bf-3495f93b32d8.cpp b/invCount/aib/de0830dc-9d87-4b19-98bf-3495f93b32d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..943b9534730bcbe9a6ea3a379e1da850c7de53ce --- /dev/null +++ b/invCount/aib/de0830dc-9d87-4b19-98bf-3495f93b32d8.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int N, answer; + +pair v[100000 + 5]; + + + +int bit[100000 + 5]; + +vector pos; + + + +void update(int x) + +{ + + for (int i = x; i <= N; i += (((i - 1) ^ i) & i)) + + bit[i]++; + +} + + + +int query(int x) + +{ + + int ret = 0; + + for (int i = x; i >= 1; i -= (((i - 1) ^ i) & i)) + + ret += bit[i]; + + return ret; + +} + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; i++) + + { + + fin >> v[i].first; + + v[i].second = i; + + } + + sort(v + 1, v + N + 1); + + reverse(v + 1, v + N + 1); + + for (int i = 1; i <= N; i++) + + { + + int j = i; + + while (j <= N && v[j].first == v[i].first) + + { + + pos.push_back(j); + + answer = (answer + query(v[j].second)) % 9917; + + j++; + + } + + for (auto &it: pos) + + update(v[it].second); + + pos.clear(); + + if (j > i) + + i = j - 1; + + } + + fout << answer << "\n"; + + return 0; + +} diff --git a/invCount/aib/df0c2db9-067d-465c-8a75-367b4eb1c681.cpp b/invCount/aib/df0c2db9-067d-465c-8a75-367b4eb1c681.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf071b88c7856244f0fd2a0df2912a7c8e9aa9ff --- /dev/null +++ b/invCount/aib/df0c2db9-067d-465c-8a75-367b4eb1c681.cpp @@ -0,0 +1,160 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool CMP1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool CMP2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, CMP1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, CMP2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/df87f996-7489-401c-8a06-f459094df0b5.cpp b/invCount/aib/df87f996-7489-401c-8a06-f459094df0b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd685737b744c10dc38674f05a8ceae1e1d93fc4 --- /dev/null +++ b/invCount/aib/df87f996-7489-401c-8a06-f459094df0b5.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +struct Element + +{ + + int value; + + int index; + + bool operator < (const Element &a) + + { + + return value < a.value || (value == a.value && index < a.index); + + } + +}; + + + +int N; + + + +Element A[100000 + 5]; + +int AIB[100000 + 5]; + + + +static inline void update (int poz, int value); + +static inline int query (int poz); + + + +int main() + +{ + + fin >> N; + + for (int i = 1; i <= N; ++i) + + fin >> A[i].value, A[i].index = i; + + sort (A + 1, A + N + 1); + + long long sum = 0; + + for (int i = 1; i <= N; ++i) + + { + + sum += 1LL * (query (A[i].index)); + + update (A[i].index, 1); + + } + + long long ans = (1LL * N * (N - 1) / 2) - sum; + + fout << ans % 9917; + + return 0; + +} + + + +static inline void update (int poz, int value) + +{ + + while (poz <= N) + + AIB[poz] += value, poz += (poz&(-poz)); + +} + + + +static inline int query (int poz) + +{ + + int ans = 0; + + while (poz) + + ans = (ans + AIB[poz]) % 9917, poz -= (poz&(-poz)); + + return ans; + + + +} diff --git a/invCount/aib/e1f53ac9-d5bd-4b7d-8b98-820696432c70.cpp b/invCount/aib/e1f53ac9-d5bd-4b7d-8b98-820696432c70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8535d565146429eb17324f09882e34caa2490be7 --- /dev/null +++ b/invCount/aib/e1f53ac9-d5bd-4b7d-8b98-820696432c70.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, Compara2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/e32484f1-93f2-4475-a376-032c9a15fdb4.cpp b/invCount/aib/e32484f1-93f2-4475-a376-032c9a15fdb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42f8a9c972ece180291d4af45a53ec55d170aadc --- /dev/null +++ b/invCount/aib/e32484f1-93f2-4475-a376-032c9a15fdb4.cpp @@ -0,0 +1,145 @@ +#include + + + + + + +using namespace std; + + + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); +int n; + +int aib[100010]; + +int a[100010]; + +int b[100010]; + + + +int cb(int val, int left, int right) { + + if(left > right) { + + return -1; + + } + + int mijl = (left+right) / 2; + + if(b[mijl] == val && b[mijl -1] < val) { + + return mijl; + + } + + if(b[mijl] < val) { + + return cb(val, mijl + 1, right); + + } else { + + return cb(val, left, mijl); + + } + +} + + + +void adauga(int x) { + + for(int i = x; i <= n; i += i & (-i)) { + + aib[i]++; + + } + +} + + + +int scoate(int x) { + + int sol = 0; + + for(int i=x; i >= 1; i -= i & (-i)) { + + sol += aib[i]; + + } + + return sol; + +} + + + +void solve() { + + fin >> n; + + + + for(int i=1; i <=n; i++) { + + fin >> a[i]; + + b[i] = a[i]; + + } + + sort(b + 1, b + n + 1); + + + + int sol = 0; + + + + for(int i=1; i <= n; i++) { + + int x =cb(a[i], 1, n); + + sol = (sol + i - 1 - scoate(x)) % 9917; + + adauga(x); + + } + + fout << sol; + +} + + + + + +int main() { + + ios_base::sync_with_stdio(0); + + fin .tie(0); + + fout.tie(0); + + + + int testCases = 1; + + + + while(testCases--) { + + solve(); + + } + +} diff --git a/invCount/aib/e7bddfde-8362-4f62-990d-5da2cf7a8767.cpp b/invCount/aib/e7bddfde-8362-4f62-990d-5da2cf7a8767.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a271a8935a9107b533eca8ae071efea6a93fa04a --- /dev/null +++ b/invCount/aib/e7bddfde-8362-4f62-990d-5da2cf7a8767.cpp @@ -0,0 +1,152 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + + + +int aib[100005], x, n, sol; + + + +struct nr + +{ + + int val, poz; + +}; + + + +nr v[100005]; + + + +void update (int poz, int val) + +{ + + for (int i = poz; i <= n; i += (i&(-i))) + + { + + aib[i] += val; + + } + +} + + + +int suma (int poz) + +{ + + int s = 0; + + for (int i = poz; i > 0; i -= (i&(-i))) + + { + + s = (s + aib[i]) % 9917; + + } + + + + return s; + +} + + + +bool cmp1 (nr a, nr b) + +{ + + if (a.val > b.val) return false; + + + + if (a.val == b.val && a.poz > b.poz) return false; + + + + return true; + +} + + + +bool cmp2 (nr a, nr b) + +{ + + if (a.poz > b.poz) return false; + + + + return true; + +} + +int main() + +{ + + f >> n; + + + + for (int i = 1; i <= n; i++) + + { + + f >> v[i].val; + + v[i].poz = i; + + } + + + + sort(v + 1, v + n + 1, cmp1); + + for (int i = 1; i <= n; i++) + + v[i].val = i; + + + + sort(v + 1, v + n + 1, cmp2); + + + + for (int i = 1; i <= n; i++) + + { + + sol = (sol + suma(v[i].val)) % 9917; + + update(v[i].val, 1); + + } + + + + g << ((1LL * n * (n - 1) / 2) % 9917 - sol + 9917) % 9917 << '\n'; + + return 0; + +} diff --git a/invCount/aib/ed496c9f-0acb-48ac-bea3-b59b5a376692.cpp b/invCount/aib/ed496c9f-0acb-48ac-bea3-b59b5a376692.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2feb106d2b89afa7804d2ec1106385ffafff6582 --- /dev/null +++ b/invCount/aib/ed496c9f-0acb-48ac-bea3-b59b5a376692.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +pairw[100002]; + +int n,i,v[100002],k,a[100002]; + +unsigned long long sol; + +int query(int p) + +{ + + int s=0; + + for(;p;p-=p&(-p)) + + { + + s+=a[p]; + + } + + return s; + +} + + + +void update(int p) + +{ + + for(;p<=k;p+=p&(-p)) + + a[p]++; + + return; + +} + +int main() + +{ + + fin>>n; + + for(i=1; i<=n; i++) + + { + + fin>>w[i].first; + + v[i]=w[i].first; + + w[i].second=i; + + } + + sort(w+1,w+n+1); + + w[0].first=2000000000; + + for(i=1; i<=n; i++){ + + if(w[i].first!=w[i-1].first) k++; + + v[w[i].second]=k; + + } + + + + for(i=1; i<=n; i++){ + + sol+=(query(k)-query(v[i])); + + update(v[i]); + + } + + fout< +#include + + +using namespace std; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +int n,i,m,a[100005],b[100005],v[100005],aib[100005]; + +int ub (int x) + +{ + + return(x&(-x)); + +} + + + +void add( int x ) + +{ + + for(int i=x; i<=n; i+=ub(i)) + + { + + aib[i]++; + + + + } + + + +} + +int sum (int x) + +{ + + int s=0; + + for(int i=x; i>=1; i-=ub(i)) + + { + + s=s+aib[i]; + + + + } + + return s; + +} +int main() + +{ + +in>>n; + + + +for(i=1;i<=n;i++) + +{in>>a[i]; + + b[i]=a[i]; + +} + +sort(b+1,b+n+1); + +m=0; + +v[++m]=b[1]; + +for(i=2;i<=n;i++) + +{if(b[i]!=b[i-1])v[++m]=b[i]; + + + +} +for(i=1;i<=n;i++){a[i]=lower_bound(v+1,v+m+1,a[i])-v; } +int nr=0; + +for(i=n;i>=1;i--) + +{nr=nr+sum(a[i]-1); + + + + nr=nr%9917; + + add(a[i]); + + + +} + + + + out< +#include +using namespace std; + + + + + + + +int getSum(int BITree[], int index) + + + +{ + + + + int sum = 0; + + + + while (index > 0) + + + + { + + + + sum += BITree[index]; + + + + index -= index & (-index); + + + + } + + + + return sum; + + + +} +void updateBIT(int BITree[], int n, int index, int val) + + + +{ + + + + while (index <= n) + + + + { + + + + BITree[index] += val; + + + + index += index & (-index); + + + + } + + + +} + + + + + + + +void convert(int arr[],int n) + + + +{ + + + + int lst[n]; + + + + for(int i=1;i<=n;i++) + + + + lst[i]=arr[i]; + + + + sort(lst+1,lst+n+1); + + + + for(int i=1;i<=n;i++) + + + + arr[i]=lower_bound(lst+1,lst+n+1,arr[i])-lst; + + + +} + + + + + + + +int getInvCount(int arr[], int n) + + + +{ + + + + int invcount = 0; + + + + convert(arr,n); + + + + int maxElement = 0; + + + + for (int i=1; i<=n; i++) + + + + if (maxElement < arr[i]) + + + + maxElement = arr[i]; + + + + + + + + int BIT[maxElement+1]; + + + + for (int i=1; i<=maxElement; i++) + + + + BIT[i] = 0; + + + + + + + + for (int i=n; i>0; i--) + + + + { + + + + invcount += getSum(BIT, arr[i]-1); + + + + invcount%=9917; + + + + updateBIT(BIT, maxElement, arr[i], 1); + + + + } + + + + + + + + return invcount; + + + +} + + + + + + + +int main() + + + +{ + + + + int arr[100000],n; + + + + ifstream fin("inv.in"); + + + + fin>>n; + + + + for(int i=1;i<=n;i++) + + + + fin>>arr[i]; + + + + fin.close(); + + + + ofstream fout("inv.out"); + + + + fout< +#include +#include + +using namespace std; +ifstream fin("inv.in"); +ofstream fout("inv.out"); +struct numar { + int poz, val; +}; +int n; +numar v[100001]; +int a[100005]; +void citire() { + fin >> n; + int va; + for (int i = 0; i < n; ++i) { + fin >> va; + v[i].val = va; + v[i].poz = i + 1; + } +} +bool comp(numar a, numar b) { + if (a.val == b.val) { + return a.poz > b.poz; + } + return a.val > b.val; +} +void update(int poz, int val) { + for (int i = poz; i <= n; i += i & -i) { + a[i] = (a[i] + val) % 9917; + } +} +long query(int poz) { + long s = 0; + for (int i = poz; i >= 1; i -= i & -i) { + s += a[i] % 9917; + } + return s % 9917; +} +int main() +{ + long inv = 0; + citire(); + sort(v, v + n, comp); + for (int i = 0; i < n; ++i) { + inv = (inv + query(v[i].poz)) % 9917; + update(v[i].poz, 1); + } + fout << inv << "\n"; + return 0; +} diff --git a/invCount/aib/ef96b126-f484-41d2-b9b9-5984dd7e2cb0.cpp b/invCount/aib/ef96b126-f484-41d2-b9b9-5984dd7e2cb0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1dac29f0fc070c35d068a087b767133b4afeae8 --- /dev/null +++ b/invCount/aib/ef96b126-f484-41d2-b9b9-5984dd7e2cb0.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream f ("inv.in") ; + +ofstream g ("inv.out") ; + +int N , x , a , b , inversiune , sol[100005]; + +int AIB[100005]; + +int solutie; + +struct normalize + +{ + + int val; + + int poz; + +}v[100005]; + +bool cmp (normalize a , normalize b) + +{ + + if (a.val == b.val) return (a.poz < b.poz); + + return (a.val < b.val) ; + +} + +void Update (int poz , int val) + +{ + + for (int i = poz ; i <= N ; i += i & (-i)) + + AIB[i] += val; + + + + return; + +} + +int Query (int poz) + +{ + + int sum = 0 ; + + for (int i = poz ; i >= 1 ; i -= i & (-i)) + + sum += AIB[i] ; + + return sum ; + +} + +int main() + +{ + + f >> N ; + + for (int i = 1 ; i <= N ; ++i) + + { + + f >> v[i].val; + + v[i].poz = i ; + + } + + sort (v + 1 , v + N + 1 , cmp) ; + + + + for (int i = 1 ; i <= N ; ++i) + + sol[v[i].poz] = i ; + + + + + + + + for (int i = 1 ; i <= N ; ++i) + + { + + x = sol[i] ; + + inversiune = Query(N) - Query(x) ; + + solutie = (solutie + inversiune) % 9917; + + + + Update(x,1) ; + + } + + g << solutie ; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/aib/f29177a8-c229-4b93-b4a3-c83dd7b81ce3.cpp b/invCount/aib/f29177a8-c229-4b93-b4a3-c83dd7b81ce3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e26ce7f1b6861ded1b58e9f47e451b485e61f40 --- /dev/null +++ b/invCount/aib/f29177a8-c229-4b93-b4a3-c83dd7b81ce3.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +const int lim = 1e5; + +int arbint[4 * lim], v[lim], w[lim]; + +vector normie; + + + +inline int to_normie(int x){ + + int ans = 0; + + for(int pas = 1 << 17; pas > 0; pas /= 2) + + if(ans + pas < normie.size() && normie[ans + pas] <= x) + + ans += pas; + + return ans; + +} + + + +void update(int nod, int st, int dr, int x){ + + if(st == dr){ + + arbint[nod]++; + + return; + + } + + if(x <= (st + dr) / 2) + + update(2 * nod, st, (st + dr) / 2, x); + + else + + update(2 * nod + 1, (st + dr) / 2 + 1, dr, x); + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int query(int nod, int st, int dr, int l, int r){ + + if(l <= st && dr <= r) + + return arbint[nod]; + + int ans = 0; + + if(l <= (st + dr) / 2) + + ans += query(2 * nod, st, (st + dr) / 2, l, r); + + if(r > (st + dr) / 2) + + ans += query(2 * nod + 1, (st + dr) / 2 + 1, dr, l, r); + + return ans; + +} + + + +int main() + +{ + + int n; + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + fin >> n; + + for(int i = 0; i < n; i++){ + + fin >> v[i]; + + w[i] = v[i]; + + } + + sort(w, w + n); + + normie.push_back(w[0]); + + for(int i = 1; i < n; i++) + + if(w[i] != w[i - 1]) + + normie.push_back(w[i]); + + for(int i = 0; i < n; i++) + + v[i] = to_normie(v[i]); + + long long ans = 0; + + for(int i = 0; i < n; i++){ + + if(v[i] < n - 1) + + ans = ans + query(1, 0, n - 1, v[i] + 1, n - 1); + + update(1, 0, n - 1, v[i]); + + } + + fout << ans % 9917; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/f72fc6da-33f4-481a-bada-33160fa764d6.cpp b/invCount/aib/f72fc6da-33f4-481a-bada-33160fa764d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f28e078f23cae8da6cee526df90b0688e135ad80 --- /dev/null +++ b/invCount/aib/f72fc6da-33f4-481a-bada-33160fa764d6.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +int v[150001],v2[150001],aib[150001],n; + +unordered_map mymap; + +ifstream in ("inv.in"); + +ofstream out ("inv.out"); + +void update(int value) + +{ + + for(int i=value;i<=n;i+=(i&(-i))) + + aib[i]++; + +} + +int query (int nod) + +{ + + int sum=0; + + for (int i=nod;i;i-=(i&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + in>>n; + + for (int i=1;i<=n;++i) + + in>>v[i],v2[i]=v[i]; + + sort(v+1,v+n+1); + + int sum1=0,p=0; + + v[0]=-1; + + for (int i=1;i<=n;++i) + + { + + if (v[i]!=v[i-1]) + + p++; + + mymap[v[i]]=p; + + } + + for (int i=n;i>=1;--i) + + { + + sum1+=query(mymap[v2[i]]-1); + + sum1%=9917; + + update(mymap[v2[i]]); + + } + + out< + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + +int n, aib[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for(i = 2; i <= n; i++) + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + + + for(i = n; i > 0; i--) + + { + + cnt += Suma(a[i].nou - 1); + + Update(a[i].nou, 1); + + cnt %= 9917; + + } + + fout << cnt; + + return 0; + +} diff --git a/invCount/aib/f850981b-30c2-4163-bc63-f4c4dccfbe89.cpp b/invCount/aib/f850981b-30c2-4163-bc63-f4c4dccfbe89.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d01efb1e666867590cb00fab8b09f5cf2e1b842e --- /dev/null +++ b/invCount/aib/f850981b-30c2-4163-bc63-f4c4dccfbe89.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/fa5d11f6-4ee6-4a01-877f-697fc8e32699.cpp b/invCount/aib/fa5d11f6-4ee6-4a01-877f-697fc8e32699.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d01efb1e666867590cb00fab8b09f5cf2e1b842e --- /dev/null +++ b/invCount/aib/fa5d11f6-4ee6-4a01-877f-697fc8e32699.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +int n, aib[100005]; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +void Update(int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + +int Suma(int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool Compara1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool Compara2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt = 0; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + + + sort(a + 1, a + n + 1, Compara1); + + a[1].nou = 1; + + for (i = 2; i <= n; i++) + + if (a[i].val == a[i-1].val) + + a[i].nou = a[i - 1].nou; + + else a[i].nou = a[i - 1].nou + 1; + + sort(a + 1, a + n + 1, Compara2); + + + + for (i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt %= 9917; + + fout << cnt << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/fad07183-b90c-4bc3-b2a1-5b705dee3e32.cpp b/invCount/aib/fad07183-b90c-4bc3-b2a1-5b705dee3e32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf071b88c7856244f0fd2a0df2912a7c8e9aa9ff --- /dev/null +++ b/invCount/aib/fad07183-b90c-4bc3-b2a1-5b705dee3e32.cpp @@ -0,0 +1,160 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, aib[100005]; + + + +struct Trei + +{ + + int val, ind, nou; + +}; + + + +Trei a[100005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +inline bool CMP1(Trei A, Trei B) + +{ + + return A.val < B.val; + +} + + + +inline bool CMP2(Trei A, Trei B) + +{ + + return A.ind < B.ind; + +} + + + +int main() + +{ + + int i, x; + + long long cnt; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> x; + + a[i].val = x; + + a[i].ind = i; + + } + + sort(a + 1, a + n + 1, CMP1); + + a[1].nou = 1; + + for(int i = 2; i <= n; i++) + + { + + if(a[i].val == a[i - 1].val) + + a[i].nou = a[i - 1].nou; + + else + + a[i].nou = a[i - 1].nou + 1; + + } + + sort(a + 1, a + n + 1, CMP2); + + cnt = 0; + + for(i = n; i >= 1; i--) + + { + + x = a[i].nou; + + cnt += Suma(x - 1); + + Update(x, 1); + + } + + cnt = cnt % 9917; + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/aib/fd8fd479-f129-41db-8cd9-7622ef2cc6d7.cpp b/invCount/aib/fd8fd479-f129-41db-8cd9-7622ef2cc6d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e934433746268405b289daa3ab2dbd0a9a16f44c --- /dev/null +++ b/invCount/aib/fd8fd479-f129-41db-8cd9-7622ef2cc6d7.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i, n, m, k, x[100001], aib[100001], sol; + +struct meme{ + + int value, pos; + +}v[100001]; + +int cmp(meme a, meme b){ + + return a.value < b.value; + +} + +void update(int x){ + + while(x <= k){ + + aib[x] = (aib[x] + 1) % 9917; + + x = x + (x & (-x)); + + } + +} + +int quary(int x){ + + int s = 0; + + while(x >= 1){ + + s = (s + aib[x]) % 9917; + + x = x - (x & (-x)); + + } + + return s; + +} + +int main() + +{ f >> n; + + for(i = 1; i <= n; i++){ + + f >> v[i].value; + + v[i].pos = i; + + } + + sort(v + 1, v + n + 1, cmp); + + k = 1; + + x[v[1].pos] = 1; + + for(i = 2; i <= n; i++){ + + if(v[i].value != v[i - 1].value) + + k++; + + x[v[i].pos] = k; + + } + + for(i = 1; i <= n; i++){ + + int value = quary(k) - quary(x[i]); + + if(value < 0) + + value += 9917; + + sol = (sol + value) % 9917; + + update(x[i]); + + } + + g << sol; + + return 0; + +} diff --git a/invCount/merge/003cfe88-619e-406b-8d6b-eb590966bdbb.cpp b/invCount/merge/003cfe88-619e-406b-8d6b-eb590966bdbb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d134d95bf222aa331c587e0d6e7dd7f0adfeec --- /dev/null +++ b/invCount/merge/003cfe88-619e-406b-8d6b-eb590966bdbb.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; + +const int N=100001; +const int M=9917; +int aux[N], v[N]; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int merge_v(int v[], int st, int dr){ + int m=(st+dr)/2; + int i, j, k, r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr){ + if(v[i]<=v[j]){ + aux[k++]=v[i++]; + } else { + aux[k++]=v[j++]; + r+=m-i+1; + r%=M; + } + } + while(i<=m){ + aux[k++]=v[i++]; + } + while(j<=dr){ + aux[k++]=v[j++]; + } + for(k=st; k<=dr; k++){ + v[k]=aux[k]; + } + return r; +} + +int ms(int v[], int st, int dr){ + int r=0; + if(st==dr){ + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r%=M; + r+=ms(v, m+1, dr); + r%=M; + r+=merge_v(v, st, dr); + r%=M; + return r; +} + +int main(){ + int n; + in>>n; + for(int i=1; i<=n; i++){ + in>>v[i]; + } + out< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int v[], int l, int r) { + + if (l < r) { + + int mid = (l + r) / 2; + + MergeSort(v, l, mid); + + MergeSort(v, mid + 1, r); + + int i = l, j = mid + 1, k = 0; + + while (i <= mid && j <= r) + + if (v[i] <= v[j]) + + temp[++k] = v[i++]; + + else { + + temp[++k] = v[j++]; + + nr_inv = (nr_inv + mid - i + 1) % 9917; + + } + + while (i <= mid) + + temp[++k] = v[i++]; + + while (j <= r) + + temp[++k] = v[j++]; + + for (i = l, j = 1; i <= r; i++, j++) + + v[i] = temp[j]; + + } + +} + + + +int main() { + + int n, v[NR]; + + + + f >> n; + + for (int i = 1; i <= n; i++) + + f >> v[i]; + + + + MergeSort(v, 1, n); + + + + g << nr_inv; + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/merge/057a525c-0bba-4784-8f3a-b583202ca93c.cpp b/invCount/merge/057a525c-0bba-4784-8f3a-b583202ca93c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c8898c4d21668bc5011964ac523c5600c113d5d --- /dev/null +++ b/invCount/merge/057a525c-0bba-4784-8f3a-b583202ca93c.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + + +using namespace std; + + + + + +int inversiuni(vector &inversat,int stanga,int dreapta) + +{ + + if(stanga==dreapta) + + return 0; + + int mijloc=(stanga+dreapta)/2; + + + + int inv1=inversiuni(inversat,stanga,mijloc); + + int inv2=inversiuni(inversat,mijloc+1,dreapta); + + + + int inv3=0; + + vectorprimul; + + vectoral_doilea; + + for(int i=stanga; i<=mijloc; i++) + + { + + primul.push_back(inversat[i]); + + + + } + + for(int i=mijloc+1; i<=dreapta; i++) + + { + + al_doilea.push_back(inversat[i]); + + + + } + + + + int x=stanga; + + int x1=0; + + int x2=0; + + while(x1!=primul.size()&&x2!=al_doilea.size()) + + { + + + + if(al_doilea[x2]>nr_elemente; + + vector elemente; + + for(int i=0; i>aux; + + elemente.push_back(aux); + + } + + + + out< + + +using namespace std; + + + +ifstream fin("inv.in") ; + +ofstream fout("inv.out") ; + + + +int t[100005] , v[100005] , nrpm ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft >= rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( v[x] <= v[y] ) + + t[i++] = v[x++] ; + + else + + { + + nrpm = (nrpm+mij-x+1)%9917 ; ; + + t[i++] = v[y++] ; + + } + + } + + while ( x <= mij ) + + t[i++] = v[x++] ; + + while ( y <= rght ) + + t[i++] = v[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + v[i] = t[i] ; + +} + + + +int main() + +{ + + int n , i ; + + fin >> n ; + + for ( i = 1 ; i <= n ; i++ ) + + fin >> v[i] ; + + nrpm = 0 ; + + mergemerge(1,n) ; + + fout << nrpm ; + +} diff --git a/invCount/merge/07aeaf26-c20b-4315-9845-6207c9fd9315.cpp b/invCount/merge/07aeaf26-c20b-4315-9845-6207c9fd9315.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bee76654be72df6703585dd637f60a5a44710070 --- /dev/null +++ b/invCount/merge/07aeaf26-c20b-4315-9845-6207c9fd9315.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + + +int n; + +long long nr_permutari; + +const int MOD = 9917; + + + +void afisare(int a[], int size_array){ + + for(int i = 0 ; i < size_array ; i++){ + + g<>n; + +int a[n]; + +for(int i = 0 ; i < n ; i++) + + f>>a[i]; + + + +mergesort(a, 0, n-1); + + + +g< + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int a[100001],n; + +int inv; + + + +void InterCls(int v[],int l,int m, int r) + +{ int i,j,k=0; + + int aux[100001]; + + i=l; + + j=m+1; + + while(i<=m && j<=r) + + if(v[i]<=v[j]) + + aux[k++]=v[i++]; + + else + + {aux[k++]=v[j++]; + + inv+=(m-i+1); + + inv%=9917; + + } + + while(i<=m) + + aux[k++]=v[i++]; + + while(j<=r) + + aux[k++]=v[j++]; + + int ct=0; + + for(i=l; i<=r; i++) + + v[i]=aux[ct++]; + +} + + + +void MergeSort(int v[],int l,int r) + +{ if(l>n; + + int i; + + for(i=1; i<=n; i++) + + fin>>a[i]; + + MergeSort(a,1,n); + + fout< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, v[100002], inter[100002], inv; + + + +void Interclasare(int v[], int st, int dr) + +{ + + int j, k, i, mij; + + mij = (st + dr) / 2; + + j = mij + 1; + + i = st; + + k = 0; + + while(i <= mij && j <= dr) + + { + + if(v[i] <= v[j]) + + inter[++k] = v[i++]; + + else + + { + + inv += mij - i + 1; + + inv = inv % 9917; + + inter[++k] = v[j++]; + + } + + } + + + + while(i <= mij) + + inter[++k] = v[i++]; + + while(j <= dr) + + inter[++k] = v[j++]; + + for(i = 1; i <= k; i++) + + v[st + i - 1] = inter[i]; + +} + + + +void Mergesort(int v[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = (st + dr) / 2; + + Mergesort(v, st, mij); + + Mergesort(v, mij + 1, dr); + + Interclasare(v, st, dr); + + } + +} + + + +int main() + +{ + + int i, j; + + fin >> n; + + for(i = 1; i <= n; i++) + + fin >> v[i]; + + Mergesort(v, 1, n); + + fout << inv; + + return 0; + +} diff --git a/invCount/merge/1226d2b2-ccca-4b6a-8bc9-9149ea059ae8.cpp b/invCount/merge/1226d2b2-ccca-4b6a-8bc9-9149ea059ae8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83d6f3cfca12c84c54c87b124947cc8881a5d378 --- /dev/null +++ b/invCount/merge/1226d2b2-ccca-4b6a-8bc9-9149ea059ae8.cpp @@ -0,0 +1,149 @@ +#include +#include + + +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(i=0; i>v[i]; + + mergeSort(v, 0, n-1); + + g << nr; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/merge/14b6b640-bf36-48a8-8dc0-7fc26d4c77fd.cpp b/invCount/merge/14b6b640-bf36-48a8-8dc0-7fc26d4c77fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6803a5a1c1373e162d138183d326a5eaeb85c9f6 --- /dev/null +++ b/invCount/merge/14b6b640-bf36-48a8-8dc0-7fc26d4c77fd.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +int n; + +int a[100003], b[100003]; + + + +void Citire() + +{ + + int i; + + ifstream fin("inv.in"); + + fin>>n; + + for(i=1;i<=n;++i) + + fin>>a[i]; + + fin.close(); + +} + + + +long long NrInv(int stg, int drp) + +{ + + if(stg==drp) + + return 0; + + if(drp==stg+1) + + { + + if(a[stg] <= a[drp]) + + return 0; + + swap(a[stg],a[drp]); + + return 1; + + } + + int mijl, i, j, indb; + + long long solstg, soldrp, solmijl; + + mijl = (stg+drp)/2; + + solstg = NrInv(stg, mijl); + + soldrp = NrInv(mijl+1, drp); + + + + solmijl=0; + + i=stg; + + j=mijl+1; + + indb=stg-1; + + while(i<=mijl && j<=drp) + + { + + if(a[i] <= a[j]) + + { + + b[++indb] = a[i]; + + i++; + + } + + else + + { + + b[++indb] = a[j]; + + solmijl += 1LL*(mijl-i+1); + + j++; + + + + } + + } + + while(i<=mijl) + + { + + b[++indb] = a[i]; + + i++; + + } + + while(j<=drp) + + { + + b[++indb] = a[j]; + + j++; + + } + + for(i=stg;i<=drp;++i) + + a[i] = b[i]; + + return (solstg + solmijl + soldrp)%9917; + +} + + + +int main() + +{ + + Citire(); + + ofstream fout("inv.out"); + + fout< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int nmax = 100005; + +const int mod = 9917; + +int n, v[nmax], L[nmax], R[nmax], temp[nmax], ans; + + + +void solve(int st, int dr) + +{ + + if(st < dr) + + { + + int mid = (st + dr) / 2; + + solve(st, mid); + + solve(mid + 1, dr); + + int l1 = 0, l2 = 0; + + for(int i = st; i <= mid; ++i) + + L[++l1] = v[i]; + + for(int i = mid + 1; i <= dr; ++i) + + R[++l2] = v[i]; + + int a = 1, b = 1, l = 0; + + while(a <= l1 && b <= l2) + + { + + if(L[a] > R[b]) + + { + + ans = (ans + l2 - b + 1) % mod; + + temp[++l] = L[a++]; + + } + + else + + temp[++l] = R[b++]; + + } + + while(a <= l1) + + temp[++l] = L[a++]; + + while(b <= l2) + + temp[++l] = R[b++]; + + for(int i = st, j = 1; i <= dr; ++i, ++j) + + v[i] = temp[j]; + + } + +} + +int main() + +{ + + fin >> n; + + for(int i = 1; i <= n; ++i) + + fin >> v[i]; + + solve(1, n); + + fout << ans << "\n"; + + return 0; + +} diff --git a/invCount/merge/18e82e1b-3300-4117-a586-dfac00e0e24f.cpp b/invCount/merge/18e82e1b-3300-4117-a586-dfac00e0e24f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec16ac7503450bad0bbea936e555a2d13e2a3e5d --- /dev/null +++ b/invCount/merge/18e82e1b-3300-4117-a586-dfac00e0e24f.cpp @@ -0,0 +1,176 @@ +#include +#include + + +std::ifstream f; + +long merge(long arr[], long left, long mid, long right) + +{ + + long count = 0; + + long i, j, k; + + + + long n1 = mid - left + 1; + + long n2 = right - mid; + + + + long* L = new long[n1]; + + long* R = new long[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = arr[left + i]; + + + + for (j = 0; j < n2; j++) + + R[j] = arr[mid + 1 + j]; + k = left; + + i = 0; + + j = 0; + + + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) { + + arr[k] = L[i]; + + i++; + + + + } + + else { + + arr[k] = R[j]; + + j++; + + count += n1 - i; + + } + + k++; + + } + + + + while (i < n1) + + { + + arr[k++] = L[i++]; + + } + + + + while (j < n2) + + { + + arr[k++] = R[j++]; + + } + + + + delete[] L; + + delete[] R; + + + + return count; + +} + +long mergeSort(long arr[], long left, long right) + +{ + + if (left == right) + + return 0; + + long inv_count = 0; + + if (left < right) + + { + + long mid = (left + right) / 2; + + + + inv_count += mergeSort(arr, left, mid); + + inv_count += mergeSort(arr, mid + 1, right); + + + + inv_count += merge(arr, left, mid, right); + + } + + return inv_count; + +} + + + +int main() + +{ + + long n, a[100010]; + + + + f.open("inv.in"); + + + + f >> n; + + for (long i = 0; i < n; i++) + + f >> a[i]; + + f.close(); + + + + std::ofstream g("inv.out"); + + long solutie = mergeSort(a, 0, n - 1); + + g << solutie % 9917; + + + + g.close(); + + + + return 0; + +} diff --git a/invCount/merge/1da6bd6b-d96d-4bcb-8045-588b14101081.cpp b/invCount/merge/1da6bd6b-d96d-4bcb-8045-588b14101081.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34599df479cb8e703b7f6295396f9561dfc76a3c --- /dev/null +++ b/invCount/merge/1da6bd6b-d96d-4bcb-8045-588b14101081.cpp @@ -0,0 +1,145 @@ +#include +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g ( "inv.out"); + +int nr; + + + +void interclasare(int a[], int l, int r){ + + + + + + + +int mid= (l + r) / 2; + + + +int i = l; + +int j = mid + 1; + +int aux = 0; + + + +int size = r - l + 1; + +int v[size]; + + + +while ( i <= mid and j <= r) { + +if (a[i] <= a[j]) { + +v[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mid - i + 1; + +nr = nr % 9917; + +v[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mid) { + +v[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= r) { + + v[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int i= 0; i < aux; ++i) { + +a[l+i] = v[i]; + +} + + + +} + + + +void mergesort(int a[], int l, int r){ + +if (l == r) + +return; + +int mid = (l + r) / 2; + +mergesort(a, l, mid); + +mergesort(a, mid + 1, r); + +interclasare(a, l, r); + + + +} + + + +int main() { + + + + + +int n; + +int vt[100005]; + +f >> n; + +for (int i = 0; i < n; ++i) { + +f>> vt[i]; + +} + +mergesort(vt, 0, n-1); + +g<< nr; + +return 0; + +} diff --git a/invCount/merge/1db93e1e-c6fe-4845-967f-0f22a5b3aa89.cpp b/invCount/merge/1db93e1e-c6fe-4845-967f-0f22a5b3aa89.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3b8201bb5cd5559d4e0f1914c145eeae6c13e76 --- /dev/null +++ b/invCount/merge/1db93e1e-c6fe-4845-967f-0f22a5b3aa89.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +int N, List[100005], VA, VB, A[100005], B[100005], i; + +long long int Inv=0LL; + +void MSort(int left, int right); + +void IClass(int left); + +int main() + +{ + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + scanf("%d", &N); + + for(i=1; i<=N; ++i)scanf("%d", &List[i]); + + MSort(1, N); + + printf("%lld", Inv%9917); + + return 0; + +} + +void MSort(int left, int right){ + + if(left==right) return; + + int mid=(left+right)/2; + + MSort(left, mid); + + MSort(mid+1, right); + + int i; + + VA=VB=0; + + for(i=left; i<=mid; ++i) A[++VA]=List[i]; + + for(i=mid+1; i<=right; ++i) B[++VB]=List[i]; + + IClass(left); + + return; + +} + +void IClass(int left){ + + int i, j; + + i=j=1; --left; + + while(i<=VA && j<=VB){ + + if(A[i]>B[j]){ + + Inv+=(VA-i+1); + + List[++left]=B[j++]; + + } + + else List[++left]=A[i++]; + + } + + while(i<=VA)List[++left]=A[i++]; + + while(j<=VB)List[++left]=B[j++]; + + return; + +} diff --git a/invCount/merge/1e25175e-b772-44c5-981f-45e3f22f8efa.cpp b/invCount/merge/1e25175e-b772-44c5-981f-45e3f22f8efa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21c5797b15eea49cbccd61bb1640322fc61da681 --- /dev/null +++ b/invCount/merge/1e25175e-b772-44c5-981f-45e3f22f8efa.cpp @@ -0,0 +1,173 @@ +#include +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n, v[100001]; + + + +int interclaseaza(int st, int m, int dr) + +{ + + int aux[dr-st+2]; + + int i,j,k,nr; + + + + i=st; + + j=m+1; + + k=0; + + nr=0; + + + + while(i<=m && j<=dr) + + { + + + + if (v[i]<=v[j]) + + { + + aux[k]=v[i++]; + + } + + else + + { + + + + + + + + aux[k]=v[j++]; + + nr=(nr+(m-i+1))%9917; + + } + + + + k++; + + } + + + + + + while(i<=m) + + { + + aux[k]=v[i++]; + + k++; + + } + + + + + + while(j<=dr) + + { + + aux[k]=v[j++]; + + k++; + + } + + + + for(i=st; i<=dr; i++) + + + + v[i]=aux[i-st]; + + return nr; + +} + + + +int nrInv(int st, int dr) + +{ + + + + if (st==dr) + + return 0; + + + + else + + { + + + + int m=(st+dr)/2; + + + + int a=nrInv(st,m); + + + + int b=nrInv(m+1,dr); + + + + int c=a+b+interclaseaza(st,m,dr); + + return c; + + } + +} + +int main() + +{ + + int i; + + + + f>>n; + + for (i=0; i>v[i]; + + + + g< + + + + +using namespace std; + +int v[100001],aux[100001],sol; + +void intercls(int st,int dr,int mij) { + + int i=st,j=mij+1,k=st-1; + + while (i<=mij || j<=dr) + + if ((v[i]>v[j] && j<=dr) || i>mij) + + aux[++k]=v[j],sol=(sol+mij-i+1)%9917,++j; + + else aux[++k]=v[i],++i; + + + + for (i=st;i<=dr;++i) + + v[i]=aux[i],aux[i]=0; + +} + +void msort(int st,int dr) { + + if (st>n; + + for (i=1;i<=n;++i) + + f>>v[i]; + + msort(1,n); + + g< +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n,k,i,st,dr,mid,pos,v[100001],in[100001]; + +long long S; + +void cnt(int a,int m,int b){ + + int c=0,i=a,j=m+1; + + while(i<=m&&j<=b){ + + if(v[i]<=v[j]){ + + in[++c]=v[i]; + + i++; + + } + + else if(v[j]0) + + S=(S+j-(c+a-1))%9917; + + j++; + + } + + } + + while(i<=m){ + + in[++c]=v[i]; + + i++; + + } + + while(j<=b){ + + in[++c]=v[j]; + + j++; + + } + + for(i=a;i<=b;i++) + + v[i]=in[i-a+1]; + +} + +void divi(int a,int b){ + + if(b-a>0){ + + int m=(a+b)/2; + + divi(a,m); + + divi(m+1,b); + + cnt(a,m,b); + + } + +} + +int main() + +{ f>>n; + + for(i=1;i<=n;i++) + + f>>v[i]; + + divi(1,n); + + g< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int inv = 0; + + + +int aux[100001]; + + + +void interclasare(int v[], int st, int dr) { + + int mij, i, j, k; + + mij = (st + dr) / 2; + + k = 0; + + i = st; + + j = mij + 1; + + while (i <= mij && j <= dr) { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else { + + aux[++k] = v[j++]; + + inv = inv + mij - i + 1; + + inv = inv % 9917; + + } + + } + + while (i <= mij) + + aux[++k] = v[i++]; + + + + while (j <= dr) + + aux[++k] = v[j++]; + + + + for (i = st, j = 1; i <= dr; i++, j++) + + v[i] = aux[j]; + +} + + + + + +void mergeSort(int v[], int st, int dr) { + + if (st < dr) { + + int mij = (st + dr) / 2; + + mergeSort(v, st, mij); + + mergeSort(v, mij + 1, dr); + + interclasare(v, st, dr); + + } + +} + + + +int main() { + + + + int v[100001]; + + int n, i; + + f>> n; + + + + for (i = 1; i <= n; ++i) + + f>> v[i]; + + + + mergeSort(v, 1, n); + + + + g << inv % 9917; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/invCount/merge/27d5d759-d45a-4fdc-b4c2-c5e40559f677.cpp b/invCount/merge/27d5d759-d45a-4fdc-b4c2-c5e40559f677.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dfaff468a96a1dd20f739fbff402825dcabbaf8 --- /dev/null +++ b/invCount/merge/27d5d759-d45a-4fdc-b4c2-c5e40559f677.cpp @@ -0,0 +1,145 @@ +#include +#include + + + + +int nr=0; + + + +void interclasare(int a[], int l, int r){ + + + + + + + +int mid= (l + r) / 2; + + + +int i = l; + +int j = mid + 1; + +int aux = 0; + + + +int size = r - l + 1; + +int v[size]; + + + +while ( i <= mid and j <= r) { + +if (a[i] <= a[j]) { + +v[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mid - i + 1; + +nr = nr % 9917; + +v[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mid) { + +v[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= r) { + + v[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int i= 0; i < aux; ++i) { + +a[l+i] = v[i]; + +} + + + +} + + + +void mergesort(int a[], int l, int r){ + +if (l == r) + +return; + +int mid = (l + r) / 2; + +mergesort(a, l, mid); + +mergesort(a, mid + 1, r); + +interclasare(a, l, r); + + + +} + + + +int main() { + + + +std::ifstream f("inv.in"); + +std::ofstream g ( "inv.out"); + +int n; + +int vt[100005]; + +f >> n; + +for (int i = 0; i < n; ++i) { + +f>> vt[i]; + +} + +mergesort(vt, 0, n-1); + +g<< nr; + +return 0; + +} diff --git a/invCount/merge/27f409cf-0730-4fd5-b258-b6a6eecf0c45.cpp b/invCount/merge/27f409cf-0730-4fd5-b258-b6a6eecf0c45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac717b96a16152d3f3862f5294978b8a85b2e695 --- /dev/null +++ b/invCount/merge/27f409cf-0730-4fd5-b258-b6a6eecf0c45.cpp @@ -0,0 +1,175 @@ +#include +#include +using namespace std; + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + +int a[100005]; + +void merging(int p[100005], int st, int mij, int dr, long long& nr_swaps) + +{ + + int l[mij-st+2]; + + int r[dr-mij+1]; + + for(int i=1; i<=mij-st+1; i++) + + { + + l[i]=p[st+i-1]; + + } + + for(int i=1; i<=dr-mij; i++) + + { + + r[i]=p[mij+i]; + + } + + int i=1, j=1; + + int t=st; + + int pate=0; + + while(i<=mij-st+1 && j<=dr-mij) + + { + + if(r[j]>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i]; + + } + + long long rez=0; + + merge_sort(a, 1, n, rez); + + g< +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int NMAX = 100005; + +const int MOD = 9917; + +int a[NMAX]; + +int b[NMAX]; + +int rasp; + + + +void msort(int st,int dr) + +{ + + if(st==dr) return; + + int mij=(st+dr)/2; + + msort(st,mij); + + msort(mij+1,dr); + + int i=st; + + int j=mij+1; + + int k=0; + + while(i<=mij and j<=dr) + + { + + if(a[i]>a[j]) + + { + + rasp+=mij-i+1; + + rasp%=MOD; + + b[++k]=a[j]; + + j++; + + } + + else + + { + + b[++k]=a[i]; + + i++; + + } + + } + + while(i<=mij) + + { + + b[++k]=a[i]; + + i++; + + } + + while(j<=dr) + + { + + b[++k]=a[j]; + + j++; + + } + + k=1; + + for(int i=st;i<=dr;i++) + + { + + a[i]=b[k]; + + k++; + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i=1;i<=n;i++) + + { + + fin >> a[i]; + + } + + msort(1,n); + + fout << rasp << '\n'; + + return 0; + +} diff --git a/invCount/merge/29597376-8eff-42c1-8ed3-3a140bde6bb7.cpp b/invCount/merge/29597376-8eff-42c1-8ed3-3a140bde6bb7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b28764af884c0e9d22a5a42bb376d652945d6228 --- /dev/null +++ b/invCount/merge/29597376-8eff-42c1-8ed3-3a140bde6bb7.cpp @@ -0,0 +1,69 @@ +#include + + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +int n; +int v[100005]; + +int Merge(int v[], int l, int m, int r) +{ + int i, j, k, res; + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + + for(i = 0;i < n1;++i) + L[i] = v[l + i]; + for(int j = 0;j < n2;++j) + R[j] = v[j + m + 1]; + i = j = res = 0; + k = l; + while(i < n1 && j < n2) + { + if(L[i] <= R[j]) + v[k++] = L[i++]; + else + { + v[k++] = R[j++]; + res += n1 - i; + res %= 9917; + } + } + while(i < n1) + v[k++] = L[i++]; + while(j < n2) + v[k++] = R[j++]; + return res; +} + +int MergeSort(int v[], int l, int r) +{ + if(l < r) + { + int res = 0; + int m = (l + r) >> 1; + res = (res + MergeSort(v, l, m)) % 9917; + res = (res + MergeSort(v, m + 1, r)) % 9917; + res = (res + Merge(v, l, m, r)) % 9917; + return res; + } + else + return 0; +} + +void Read() +{ + f>>n; + for(int i = 0;i < n;++i) + f>>v[i]; +} + +int main() +{ + Read(); + g< +#include +const int MOD = 9917; + + + +using namespace std; + + + +void merge_sort(int *v, int stg, int dr, int &sol) { + + if (stg < dr) { + + int mid = (stg + dr) / 2; + + merge_sort(v, stg, mid, sol); + + merge_sort(v, mid + 1, dr, sol); + + int stg_index = stg; + + int dr_index = mid + 1; + + int *aux = new int[dr - stg + 1]; + + int aux_index = 0; + + while (stg_index <= mid && dr_index <= dr) { + + if (v[stg_index] <= v[dr_index]) { + + aux[aux_index++] = v[stg_index]; + + stg_index++; + + } else { + + sol += mid - stg_index + 1; + + sol %= MOD; + + aux[aux_index++] = v[dr_index]; + + dr_index++; + + } + + } + + while (stg_index <= mid) { + + aux[aux_index++] = v[stg_index]; + + stg_index++; + + } + + while (dr_index <= dr) { + + aux[aux_index++] = v[dr_index]; + + dr_index++; + + } + + for (int i = stg; i <= dr; ++i) { + + v[i] = aux[i - stg]; + + } + + + + + + + + delete[] aux; + + } + +} + + + +int main() { + + int n; + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + fin >> n; + + int *v = new int[n]; + + for (int i = 0; i < n; ++i) { + + fin >> v[i]; + + } + + int sol = 0; + + merge_sort(v, 0, n - 1, sol); + + fout << sol; + + + + return 0; + +} diff --git a/invCount/merge/2a7c991d-0f3a-4e5f-932b-c4ce87479dac.cpp b/invCount/merge/2a7c991d-0f3a-4e5f-932b-c4ce87479dac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5160b448a93a60fa60fc80d0327ee2fae51b404 --- /dev/null +++ b/invCount/merge/2a7c991d-0f3a-4e5f-932b-c4ce87479dac.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int v[1000001], v_nou[100001], i, inv, n; + +void sortare(int v[], int st, int dr) + +{ + + if ( st < dr ) + + {int m; + + m = (st + dr)/2; + + sortare(v,st,m); + + sortare(v,m+1,dr); + + int i,j,nr; + + i = st; j = m+1; + + nr = 0; + + while ( i <=m && j<=dr) + + if (v[i] <= v[j]) + + {nr++; + + v_nou[nr] = v[i]; + + i++;} + + else + + {nr++; + + v_nou[nr] = v[j]; + + j++; + + inv = (inv + m - i + 1)%9917; + + } + + while (i <= m) + + v_nou[++nr] = v[i++]; + + while(j <= dr) + + v_nou[++nr] = v[j++]; + + i = 1; + + for (j=st; j<=dr ; j++) + + { v[j] = v_nou[i]; + + i++; + + } + + } + +} + +int main() + +{ f>>n; + + for(i=1; i<=n; i++) + + f>>v[i]; + + sortare(v,1,n); + + g< +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917 ; + +int n ,v[100005] , tmp[100005]; + +long long sol = 0 ; + + + +void MergeSort(int v[] , int st , int dr){ + + if( st < dr ){ + + int m = (st + dr) / 2 ; + + MergeSort( v , m+1 , dr ) ; + + MergeSort( v , st , m ) ; + + + + int i = st , j = m + 1 , k = 0 ; + + long long nr = 0 , rasp = 0 ; + + while( i<=m && j<=dr ) + + if( v[i] > v[j] ){ + + tmp[++k] = v[j++] ; + + nr++ ; + + } + + else{ + + tmp[++k] = v[i++] ; + + rasp+=nr ; + + } + + while(i <= m){ + + rasp+=nr ; + + tmp[++k] = v[i++] ; + + } + + while(j <= dr) + + tmp[++k] = v[j++] ; + + + + sol=(sol+rasp)%MOD ; + + + + for(i = st , j = 1 ; i <= dr ; i ++ , j ++) + + v[i] = tmp[j]; + + } + +} + +int main() + +{ + +f>>n; + +for(int i=1 ; i<=n ; i++){ + + f>>v[i] ; + +} + +MergeSort( v , 1 , n ) ; + + + +g< + + + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int aux; + +int v[100001], u[100001]; + + + +void interclasare(int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + u[k] = v[i]; + + i++; + + k++; + + } + + else { + + aux = (aux + mij - i + 1) % 9917; + + u[k] = v[j]; + + k++; + + j++; + + + + } + + } + + + + while (i <= mij) + + { + + u[k] = v[i]; + + i++; + + k++; + + } + + + + while (j <= mij) + + { + + u[k] = v[j]; + + j++; + + k++; + + } + + + + for (int i = 0; i < k; i++) + + v[i + st] = u[i]; + +} + + + + + +void mergesort(int st, int dr) + +{ + + if (st < dr) + + { + + int mij = (st + dr) / 2; + + mergesort(st, mij); + + mergesort(mij + 1, dr); + + interclasare(st, dr); + + } + +} + + + +int main() + +{ + + int n; + + + + f >> n; + + + + for(int i = 0; i < n; i++) + + f >> v[i]; + + + + mergesort(0, n - 1); + + + + g << aux << '\n'; + + return 0; + +} diff --git a/invCount/merge/2d90118d-ad5f-4d5f-8124-e44c9cc4305e.cpp b/invCount/merge/2d90118d-ad5f-4d5f-8124-e44c9cc4305e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d276a68ee67e23e14563ffb92664ef95a9dfd7b6 --- /dev/null +++ b/invCount/merge/2d90118d-ad5f-4d5f-8124-e44c9cc4305e.cpp @@ -0,0 +1,109 @@ +#include +#include +using namespace std; + +int v1[100001], v2[100001], nr; + + + +void intercl(int left, int mid, int right) + +{ + + int i = left, j = mid + 1, k = 0; + + + + while(i <= mid and j <= right) + + { + + if(v1[i] <= v1[j]) + + v2[k ++] = v1[i ++]; + + else + + { + + nr += mid - i + 1; + + nr = nr % 9917; + + v2[k ++] = v1[j ++]; + + } + + } + + + + while(i <= mid) v2[k ++] = v1[i ++]; + + while(j <= right) v2[k ++] = v1[j ++]; + + + + for(k = left; k <= right; k ++) + + v1[k] = v2[k - left]; + +} + + + +void mergesort(int left, int right) + +{ + + int mid; + + + + if(left < right) + + { + + mid = (left + right) / 2; + + + + mergesort(left, mid); + + mergesort(mid + 1, right); + + + + intercl(left, mid, right); + + } + +} + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + int n, i; + + f>> n; + + for (i=0;i>v1[i]; + +mergesort(0,n-1); + + + + + +g< +#include +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int nr = 0; + +void interclasare(int v[], int aux[], int st, int m, int dr) { + + int i, j, k; + + i = st; + + j = m; + + k = st; + + while ((i<=m-1)&&(j<=dr)) { + + if (v[i]<=v[j]) + + aux[k++]=v[i++]; + + else{ + + aux[k++] = v[j++]; + + nr += (m-i); + + nr = nr%9917; + + } + + } + + while (i<=m-1) + + aux[k++] = v[i++]; + + while (j<=dr) + + aux[k++] = v[j++]; + + for (i=st;i<=dr;i++) + + v[i] = aux[i]; + +} + + + +void mergeSort(int v[], int aux[], int st, int dr) { + + int m; + + if (dr>st){ + + m = (dr+st)/2; + + mergeSort(v, aux, st, m); + + mergeSort(v, aux, m+1, dr); + + interclasare(v, aux, st, m+1, dr); + + } + +} + + + + + +int main() { + + int v[100005],n,aux[100005]; + + in>>n; + + for(int i=0;i>v[i]; + + mergeSort(v, aux, 0, n-1); + + out< + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long v[100000]; + +int n,poz[100000],dreapta; + +long long solutie; + +void MergeSort(long a[],int limInf, int limSup) + +{ + + if(limInf>n; + + for(int i=0;i>v[i]; + + MergeSort(v,0,n-1); + + fout< +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int N = 100000; + +int v[N], aux[N]; + +long long inv=0; + + + +void MergeSort(int v[], int st, int dr); + +void Merge(int v[], int st, int dr); + + + +int main() + +{ + + int n; + + in >> n; + + for(int i=0; i> v[i]; + + + + MergeSort(v, 0, n-1); + + + + out << inv%9917; + + return 0; + +} + + + +void MergeSort(int v[], int st, int dr) + +{ + + if(st==dr) return; + + int m=(st+dr)/2; + + MergeSort(v, st, m); + + MergeSort(v, m+1, dr); + + Merge(v, st, dr); + +} + + + +void Merge(int v[], int st, int dr) + +{ + + int m=(st+dr)/2; + + int i=st, k=st, j=m+1; + + while(i<=m && j<=dr) + + { + + if(v[j] +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long inv = 0; + + + +void interclasare(int s[], int st, int mij, int dr) + +{ + + int i,j,k,rasp = 0,nr = 0; + + int n1 = mij-st+1; + + int n2 = dr - mij; + + + + int S[n1],D[n2]; + + + + for(i = 0; i>n; + + + + int s[100001]; + + + + for(i=1; i<=n; i++) + + { + + fin>>s[i]; + + } + + + + mergesort(s,1,n); + + + + fout< +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + +void interclasare(int arr[], int s, int d); + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + + + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/33a45228-137e-11eb-8edb-309c234925a0.cpp b/invCount/merge/33a45228-137e-11eb-8edb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3be8c107859257c295e440160bf04556b8bc72b2 --- /dev/null +++ b/invCount/merge/33a45228-137e-11eb-8edb-309c234925a0.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int v[], int l, int r) { + + if (l < r) { + + int mid = (l + r) / 2; + + MergeSort(v, l, mid); + + MergeSort(v, mid + 1, r); + + int i = l, j = mid + 1, k = 0; + + while (i <= mid && j <= r) + + if (v[i] <= v[j]) + + temp[++k] = v[i++]; + + else { + + temp[++k] = v[j++]; + + nr_inv = (nr_inv + mid - i + 1) % 9917; + + } + + while (i <= mid) + + temp[++k] = v[i++]; + + while (j <= r) + + temp[++k] = v[j++]; + + for (i = l, j = 1; i <= r; i++, j++) + + v[i] = temp[j]; + + } + +} + + + +int main() { + + int n, v[NR]; + + + + f >> n; + + for (int i = 1; i <= n; i++) + + f >> v[i]; + + + + MergeSort(v, 1, n); + + + + g << nr_inv; + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/merge/33b31068-137e-11eb-bd6c-309c234925a0.cpp b/invCount/merge/33b31068-137e-11eb-bd6c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f07182048b49c7d92dfd701d5ddb9f4b657fdddd --- /dev/null +++ b/invCount/merge/33b31068-137e-11eb-bd6c-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include +#include +using namespace std; + + + +int n,i; + +int v[100003]; + +int a[100003]; + + + +int interclasare(int st,int dr){ + + int mid=(st+dr)/2,i=st,j=mid+1,nr=0,s=0; + + + + while(i<=mid && j<=dr){ + + if(v[i]<=v[j]) + + a[++nr]=v[i++]; + + else + + a[++nr]=v[j++],s=s+mid-i+1; + + } + + + + while(i<=mid) + + a[++nr]=v[i++]; + + + + while(j<=dr) + + a[++nr]=v[j++]; + + + + for(i=1;i<=nr;i++) + + v[st+i-1]=a[i]; + + + + return s; + + + +} + + + +int mergesort(int st,int dr){ + + int mid=(st+dr)/2,s=0; + + + + if(st +using namespace std; + +const int maxN=1e5+1; +const int MOD=9917; + +int n,sol; +int v[maxN]; +int aux[maxN]; + +void mergeSort(int st,int dr){ + if(st>=dr) + return; + + int mij=(st+dr)/2; + mergeSort(st,mij); + mergeSort(mij+1,dr); + + int leftIdx=st; + int rightIdx=mij+1; + int curr=0; + + while(leftIdx<=mij && rightIdx<=dr) + if(v[leftIdx]<=v[rightIdx]){ + aux[++curr]=v[leftIdx]; + leftIdx++; + }else{ + aux[++curr]=v[rightIdx]; + rightIdx++; + + sol+=mij-leftIdx+1; + while(sol>=MOD) + sol-=MOD; + } + + while(leftIdx<=mij){ + aux[++curr]=v[leftIdx]; + leftIdx++; + } + + while(rightIdx<=dr){ + aux[++curr]=v[rightIdx]; + rightIdx++; + } + + for(int i=st;i<=dr;i++) + v[i]=aux[i-st+1]; +} + +int main() { + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + cin>>n; + for(int i=1;i<=n;i++) + cin>>v[i]; + + mergeSort(1,n); + + cout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int vectorr[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mijloc = (stanga + dreapta) / 2; + + MergeSort(vectorr, stanga , mijloc); + + MergeSort(vectorr, mijloc + 1 , dreapta); + + int i = stanga, j = mijloc + 1, k = 0; + + + + while( i <= mijloc && j <= dreapta ) + + if( vectorr[i] <= vectorr[j]) + + temp[++k] = vectorr[i++]; + + else + + { + + temp[++k] = vectorr[j++]; + + nr_inv = (nr_inv + mijloc - i + 1)%9917; + + } + + + + while(i <= mijloc) + + temp[++k] = vectorr[i++]; + + + + while(j <= dreapta) + + temp[++k] = vectorr[j++]; + + + + for(i = stanga , j = 1 ; i <= dreapta ; i ++ , j ++) + + vectorr[i] = temp[j]; + + } + +} + + + +int main() + +{ + + + + int n, v[NR]; + + f>>n; + + + + for(int i = 1; i <= n; i++) + + f>>v[i]; + + + + MergeSort(v,1,n); + + + + g< + + + + +const int MV = 1e5 + 5 ; + + + +int n, inv, k ; + +int v[MV], L[MV >> 1], R[MV >> 1] ; + + + +void merge(int st, int dr) { + + int mij = (dr + st) >> 1, k(st - 1), c(0) ; + + int i(1), j(1) ; + + for (i = 1 ; i <= mij - st + 1 ; ++ i) { + + L[i] = v[i + st - 1] ; + + } + + for (i = 1 ; i <= dr - mij ; ++ i) { + + R[i] = v[mij + i] ; + + } + + i = 1 ; + + while (i <= mij - st + 1 && j <= dr - mij) { + + if (L[i] <= R[j]) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } else { + + c = j ; + + v[++ k] = R[j ++] ; + + } + + } + + while (i <= mij - st + 1) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } + + while (j <= dr - mij) { + + v[++ k] = R[j ++] ; + + } + +} + + + +void merge_sort(int st, int dr) { + + if (st >= dr) + + return ; + + merge_sort(st, (st + dr) >> 1) ; + + merge_sort(((st + dr) >> 1) + 1, dr) ; + + merge(st, dr) ; + +} + + + +int main() { + + freopen("inv.in", "r", stdin) ; + + freopen("inv.out", "w", stdout) ; + + int i ; + + scanf("%d", &n) ; + + for (i = 1 ; i <= n ; ++ i) { + + scanf("%d", v + i) ; + + } + + merge_sort(1, n) ; + + printf("%d", inv) ; + + return 0 ; } diff --git a/invCount/merge/33f46da2-137e-11eb-90f0-309c234925a0.cpp b/invCount/merge/33f46da2-137e-11eb-90f0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aa4ae30b607c8da6ef9a4a8c788436947bc1ff1 --- /dev/null +++ b/invCount/merge/33f46da2-137e-11eb-90f0-309c234925a0.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, v[100002], inter[100002], inv; + + + +void Interclasare(int v[], int st, int dr) + +{ + + int j, k, i, mij; + + mij = (st + dr) / 2; + + j = mij + 1; + + i = st; + + k = 0; + + while(i <= mij && j <= dr) + + { + + if(v[i] <= v[j]) + + inter[++k] = v[i++]; + + else + + { + + inv += mij - i + 1; + + inv = inv % 9917; + + inter[++k] = v[j++]; + + } + + } + + + + while(i <= mij) + + inter[++k] = v[i++]; + + while(j <= dr) + + inter[++k] = v[j++]; + + for(i = 1; i <= k; i++) + + v[st + i - 1] = inter[i]; + +} + + + +void Mergesort(int v[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = (st + dr) / 2; + + Mergesort(v, st, mij); + + Mergesort(v, mij + 1, dr); + + Interclasare(v, st, dr); + + } + +} + + + +int main() + +{ + + int i, j; + + fin >> n; + + for(i = 1; i <= n; i++) + + fin >> v[i]; + + Mergesort(v, 1, n); + + fout << inv; + + return 0; + +} diff --git a/invCount/merge/34127dd2-137e-11eb-bb3e-309c234925a0.cpp b/invCount/merge/34127dd2-137e-11eb-bb3e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72765edcc4bd10480b46dc2f2402eade110127fd --- /dev/null +++ b/invCount/merge/34127dd2-137e-11eb-bb3e-309c234925a0.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(int i=0; i>v[i]; + + f.close(); + + mergeSort(v, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/3420819e-137e-11eb-ac66-309c234925a0.cpp b/invCount/merge/3420819e-137e-11eb-ac66-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4803c8a41cf8d11e8d11013264771bfab9b96e2 --- /dev/null +++ b/invCount/merge/3420819e-137e-11eb-ac66-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + + + +using namespace std; + +ifstream r("inv.in"); + +ofstream w("inv.out"); + +int v[500003], sol=0; + +const int mod=9917; + +void unire(int a, int mij, int b){ + + int n1 = mij - a + 1, n2 = b - mij, g[n1], h[n2], nr=0, rasp=0; + + for(int i=0;i>n; + + for(int i=0; i>v[i]; + + } + + mergesort(0, n-1); + + w< +using namespace std; + + + + + +int a[100001], n, b[100001]; + + + +long long merge(int a[], int b[], int low, int mid, int high); + + + +long long mergesort(int a[], int b[], int low, int high) { + + long long mid, cnt = 0; + + if (low < high) { + + mid = (low + high)/2; + + cnt = (cnt + mergesort(a, b, low, mid) + mergesort(a, b, mid + 1, high) + merge(a, b, low, mid, high)) % 9917; + + } + + return cnt; + +} + +long long merge(int a[], int b[], int low, int mid, int high) { + + int h, i, j, k; + + long long cnt = 0; + + h = low; + + i = low; + + j = mid + 1; + + + + while ((h <= mid) && (j <= high)) { + + if (a[h] <= a[j]) { + + b[i] = a[h]; + + h++; + + } + + else { + + b[i] = a[j]; + + j++; + + cnt = (cnt + mid - h + 1) % 9917; + + } + + i++; + + } + + if (h > mid) { + + for (k = j; k <= high; k++) { + + b[i] = a[k]; + + i++; + + } + + } + + else { + + for (k = h; k <= mid; k++) { + + b[i] = a[k]; + + i++; + + } + + } + + for (k = low; k <= high; k++) { + + a[k] = b[k]; + + } + + return cnt; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + scanf("%d", &n); + + for(int i = 0; i < n; i++) { + + scanf("%d", &a[i]); + + } + + + + printf("%lld", mergesort(a, b, 0, n - 1)); + + return 0; + +} diff --git a/invCount/merge/345d3c82-137e-11eb-9809-309c234925a0.cpp b/invCount/merge/345d3c82-137e-11eb-9809-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d80acedf60000ba9096028b69f37483585c7014 --- /dev/null +++ b/invCount/merge/345d3c82-137e-11eb-9809-309c234925a0.cpp @@ -0,0 +1,109 @@ +#include +#include +using namespace std; + + + + + +int interclasare(int a[], int i, int j,int m) + +{ + + int nr=0; + + int aux[i+j-1]; + + int p=i; + + int q=m+1; + + int k=0; + + while(p<=m && q<=j) + + { + + if(a[p]<=a[q]) + + aux[k++]=a[p++]; + + else + + {aux[k++]=a[q++];nr+=m-p+1;} + + } + + while(p<=m) + + aux[k++]=a[p++]; + + while(q<=j) + + aux[k++]=a[q++]; + + + + for(int l=i; l<=j; l++) + + a[l]=aux[l-i]; + + + + return nr; + + + +} + + + +int mergesort(int a[],int i,int j) + +{ + + int nrinv=0; + + if(i>N; + + int a[100010]; + + for(int i=0;i>a[i]; + + out< +#include + +using namespace std; +int nr = 0; + +void interclasare(int v[], int st, int dr) { + int mij = (st + dr) / 2; + int i = st; + int j = mij + 1; + int k = 0; + int n = dr - st + 1; + int c[n]; + while (i <= mij && j <= dr) { + if (v[i] <= v[j]) { + c[k] = v[i]; + k++; + i++; + } else { + nr += mij - i + 1; + nr = nr % 9917; + c[k] = v[j]; + k++; + j++; + } + } + while (i <= mij) { + c[k] = v[i]; + k++; + i++; + } + while (j <= dr) { + c[k] = v[j]; + k++; + j++; + + } + for (i = 0; i < k; i++) { + v[st + i] = c[i]; + } +} + +void mergeSort(int v[], int st, int dr) { + int mij; + if (st < dr) { + mij = (st + dr) / 2; + mergeSort(v, st, mij); + mergeSort(v, mij + 1, dr); + interclasare(v, st, dr); + } +} + +int main() { + ifstream f("inv.in"); + ofstream g("inv.out"); + int n, v[100001]; + f >> n; + for (int i = 0; i < n; ++i) + f >> v[i]; + f.close(); + mergeSort(v, 0, n - 1); + g << nr; + g.close(); + return 0; +} diff --git a/invCount/merge/34a319a8-137e-11eb-9817-309c234925a0.cpp b/invCount/merge/34a319a8-137e-11eb-9817-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cca42315cdb6b7335c2142578356232ae68e4f4 --- /dev/null +++ b/invCount/merge/34a319a8-137e-11eb-9817-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include +#include +using namespace std; + +int nr; + +void merges(long long int v[],int l,int m,int r) + +{ + + int i, j, k; + + int n1 = m - l + 1; + + int n2 = r - m; + + int L[n1], R[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = v[l + i]; + + for (j = 0; j < n2; j++) + + R[j] = v[m + 1+ j]; + + i = 0; + + j = 0; + + k = l; + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) + + { + + v[k] = L[i]; + + i++; + + } + + else + + {nr += n1-i; + + nr = nr % 9917; + + v[k] = R[j]; + + j++; + + } + + k++; + + } + + while (i < n1) + + { + + v[k] = L[i]; + + i++; + + k++; + + } + + + + while (j < n2) + + { + + v[k] = R[j]; + + j++; + + k++; + + } + + + +} + + + + + +void mergesort(long long int v[],int l,int r) + +{ + + if(l> n; + + for (i=1;i<=n;i++) + + f>>v[i]; + +mergesort(v,1,n); + + + + + +g< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int inv = 0; + + + +int aux[100001]; + + + +void interclasare(int v[], int st, int dr) { + + int mij, i, j, k; + + mij = (st + dr) / 2; + + k = 0; + + i = st; + + j = mij + 1; + + while (i <= mij && j <= dr) { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else { + + aux[++k] = v[j++]; + + inv = inv + mij - i + 1; + + inv = inv % 9917; + + } + + } + + while (i <= mij) + + aux[++k] = v[i++]; + + + + while (j <= dr) + + aux[++k] = v[j++]; + + + + for (i = st, j = 1; i <= dr; i++, j++) + + v[i] = aux[j]; + +} + + + + + +void mergeSort(int v[], int st, int dr) { + + if (st < dr) { + + int mij = (st + dr) / 2; + + mergeSort(v, st, mij); + + mergeSort(v, mij + 1, dr); + + interclasare(v, st, dr); + + } + +} + + + +int main() { + + + + int v[100001]; + + int n, i; + + f>> n; + + + + for (i = 1; i <= n; ++i) + + f>> v[i]; + + + + mergeSort(v, 1, n); + + + + g << inv % 9917; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/invCount/merge/34c2d7ca-137e-11eb-9e86-309c234925a0.cpp b/invCount/merge/34c2d7ca-137e-11eb-9e86-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..235b51538e7077cb88ebaa0b67c99f8bfa6b7ba4 --- /dev/null +++ b/invCount/merge/34c2d7ca-137e-11eb-9e86-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long v[100000]; + +int n,poz[100000],dreapta; + +long long solutie; + +void MergeSort(long a[],int st, int dr) + +{ + + if(st>n; + + for(int i=0; i>v[i]; + + MergeSort(v,0,n-1); + + fout< +#include +using namespace std; + +int v1[100001], v2[100001], nr; + + + +void intercl(int left, int mid, int right) + +{ + + int i = left, j = mid + 1, k = 0; + + + + while(i <= mid and j <= right) + + { + + if(v1[i] <= v1[j]) + + v2[k ++] = v1[i ++]; + + else + + { + + nr += mid - i + 1; + + nr = nr % 9917; + + v2[k ++] = v1[j ++]; + + } + + } + + + + while(i <= mid) v2[k ++] = v1[i ++]; + + while(j <= right) v2[k ++] = v1[j ++]; + + + + for(k = left; k <= right; k ++) + + v1[k] = v2[k - left]; + +} + + + +void mergesort(int left, int right) + +{ + + int mid; + + + + if(left < right) + + { + + mid = (left + right) / 2; + + + + mergesort(left, mid); + + mergesort(mid + 1, right); + + + + intercl(left, mid, right); + + } + +} + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + int n, i; + + f>> n; + + for (i=0;i>v1[i]; + +mergesort(0,n-1); + + + + + +g< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int v1[100001], v2[100001], nr; + + + +void intercl(int left, int mid, int right) + +{ + + int i = left, j = mid + 1, k = 0; + + + + while(i <= mid and j <= right) + + { + + if(v1[i] <= v1[j]) + + v2[k ++] = v1[i ++]; + + else + + { + + nr += mid - i + 1; + + nr = nr % 9917; + + v2[k ++] = v1[j ++]; + + } + + } + + + + while(i <= mid) v2[k ++] = v1[i ++]; + + while(j <= right) v2[k ++] = v1[j ++]; + + + + for(k = left; k <= right; k ++) + + v1[k] = v2[k - left]; + +} + + + +void mergesort(int left, int right) + +{ + + int mid; + + + + if(left < right) + + { + + mid = (left + right) / 2; + + + + mergesort(left, mid); + + mergesort(mid + 1, right); + + + + intercl(left, mid, right); + + } + +} + + + +int main() + +{ + + int n; + + f >> n; + + for(int i = 0; i < n; i ++) + + f >> v1[i]; + + + + mergesort(0, n - 1); + + g << nr; + + return 0; + +} diff --git a/invCount/merge/34f1c734-137e-11eb-b8b2-309c234925a0.cpp b/invCount/merge/34f1c734-137e-11eb-b8b2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..beb9bac7a3310c9eda5acc4a4992baa89aaab234 --- /dev/null +++ b/invCount/merge/34f1c734-137e-11eb-b8b2-309c234925a0.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +int n, a[100007], raspuns; + + + +void Citire() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + +} + + + +void Interclasare(int st, int dr) + +{ + + int mij, i, j, k = 0, aux[dr - st + 7]; + + mij = (st + dr) / 2; + + i = st; + + j = mij + 1; + + + + while (i <= mij && j <= dr) + + { + + if (a[i] <= a[j]) + + aux[++k] = a[i++]; + + else + + { + + raspuns += mij - i + 1; + + raspuns %= 9917; + + aux[++k] = a[j++]; + + } + + } + + + + while(i <= mij) + + aux[++k] = a[i++]; + + + + while(j <= dr) + + aux[++k] = a[j++]; + + + + for (i = 1; i <= k; i++) + + a[st + i - 1] = aux[i]; + +} + + + +void MergeSort(int st, int dr) + +{ + + int mij; + + if (st < dr) + + { + + mij = (st + dr) / 2; + + MergeSort(st, mij); + + MergeSort(mij + 1, dr); + + Interclasare(st, dr); + + } + +} + + + +int main() + +{ + + Citire(); + + MergeSort(1, n); + + fout << raspuns << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/352189c6-137e-11eb-98e9-309c234925a0.cpp b/invCount/merge/352189c6-137e-11eb-98e9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c8ed7943d1e18f629a9de14076ccfa642198358 --- /dev/null +++ b/invCount/merge/352189c6-137e-11eb-98e9-309c234925a0.cpp @@ -0,0 +1,143 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + +long a[100001]; + +int n,nrinv; + +long rez; + + + +void Merge(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + Merge(a,st,mij); + + Merge(a,mij+1,dr); + + + + int k=0; + + int i=st; + + int j=mij+1; + + int b[100001]; + + int nrinv=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + else + + { + + b[k]=a[j]; + + k++; + + j++; + + nrinv++; + + } + + + + while(i<=mij) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + while(j<=dr) + + { + + b[k]=a[j]; + + k++; + + j++; + + } + + for(i=st; i<=dr; i++) + + { + + a[i]=b[i-st]; + + } + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>a[i]; + + } + + Merge(a,0,n-1); + + g< +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + + + +long a[100001]; + +int n,inv_count; + +long Ifinal; + + + +void MergeSort(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + + + MergeSort(a,st,mij); + + MergeSort(a,mij+1,dr); + + + + + + int k=0; + + int i=st; + + int j=mij+1; + + + + int temp[100001]; + + int inv_count=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + temp[k++]=a[i++]; + + Ifinal+=inv_count; + + + + } + + else + + { + + temp[k++]=a[j++]; + + inv_count++; + + + + } + + + + while(i<=mij) + + { + + temp[k++]=a[i++]; + + Ifinal+=inv_count; + + } + + while(j<=dr) + + temp[k++]=a[j++]; + + + + + + for(i=st; i<=dr; i++) + + a[i]=temp[i-st]; + + } + +} + + + +long Inv(long a[],int n) + +{ + + MergeSort(a,0,n-1); + + return Ifinal; + +} + + + + + +int main() + +{ + + in>>n; + + for(int i=0; i>a[i]; + + + + out< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + +long a[100001]; + +int n,nrinv; + +long rez; + + + +void Merge(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + Merge(a,st,mij); + + Merge(a,mij+1,dr); + + + + int k=0; + + int i=st; + + int j=mij+1; + + int b[100001]; + + int nrinv=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + else + + { + + b[k]=a[j]; + + k++; + + j++; + + nrinv++; + + } + + + + while(i<=mij) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + while(j<=dr) + + { + + b[k]=a[j]; + + k++; + + j++; + + } + + for(i=st; i<=dr; i++) + + { + + a[i]=b[i-st]; + + } + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>a[i]; + + } + + Merge(a,0,n-1); + + g< +#include + +using namespace std; + +int inv = 0; + +int w[100001]; + +void interclasare(int v[], int st, int dr) { + int m, i, j, k; + m = (st + dr) / 2; + k = 0; + i = st; + j = m + 1; + while (i <= m && j <= dr) { + if (v[i] <= v[j]) w[++k] = v[i++]; + else { + w[++k] = v[j++]; + inv = inv + m - i + 1; + inv = inv % 9917; + } + } + while (i <= m) w[++k] = v[i++]; + while (j <= dr) w[++k] = v[j++]; + for (i = st, j = 1; i <= dr; i++, j++) v[i] = w[j]; +} + + +void mergeSort(int v[], int st, int dr) { + if (st < dr) { + int m = (st + dr) / 2; + mergeSort(v, st, m); + mergeSort(v, m + 1, dr); + interclasare(v, st, dr); + } +} + +int main() { + ifstream fin("inv.in"); + ofstream fout("inv.out"); + int v[100001]; + int n, i; + fin >> n; + for (i = 1; i <= n; ++i) + fin >> v[i]; + mergeSort(v, 1, n); + fout << inv % 9917; + return 0; +} diff --git a/invCount/merge/35604076-137e-11eb-999b-309c234925a0.cpp b/invCount/merge/35604076-137e-11eb-999b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c6c179cd95893076bc8f49824bb26bb12151cf4 --- /dev/null +++ b/invCount/merge/35604076-137e-11eb-999b-309c234925a0.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int a[100005]; + +long long inv; + + + +void Interclasare(int s,int mij,int d) + +{ int b[100005],i,j,k=0; + + i=s; j=mij+1; + + while(i<=mij && j<=d) + + if(a[i]<=a[j]) b[++k]=a[i++]; + + else {b[++k]=a[j++]; inv += (mij-i+1)%9917;} + + while(i<=mij) b[++k]=a[i++]; + + while(j<=d) b[++k]=a[j++]; + + + + for(i=s,j=1;j<=k;i++,j++) + + a[i]=b[j]; + +} + + + +void MS(int s,int d) + +{ if(s>n; + + for(i=1;i<=n;i++) + + fin>>a[i]; + + MS(1,n); + + fout<< inv%9917; + + return 0; + +} diff --git a/invCount/merge/358ecf6c-137e-11eb-acb9-309c234925a0.cpp b/invCount/merge/358ecf6c-137e-11eb-acb9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..798769b1536f8dc3215872c52dcc227eeda494e0 --- /dev/null +++ b/invCount/merge/358ecf6c-137e-11eb-acb9-309c234925a0.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + +long merge(long arr[], long left, long mid, long right) + +{ + + long count = 0; + + long i, j, k; + + + + long temp1 = mid - left + 1; + + long temp2 = right - mid; + + + + long* L = new long[temp1]; + + long* R = new long[temp2]; + + + + for (i = 0; i < temp1; i++) + + L[i] = arr[left + i]; + + + + for (j = 0; j < temp2; j++) + + R[j] = arr[mid + 1 + j]; + + + + + + + + k = left; + + i = 0; + + j = 0; + + + + while (i < temp1 && j < temp2) + + { + + if (L[i] <= R[j]) { + + arr[k] = L[i]; + + i++; + + } + + else { + + arr[k] = R[j]; + + j++; + + count += temp1 - i; + + } + + k++; + + } + + while (i < temp1) + + arr[k++] = L[i++]; + + while (j < temp2) + + arr[k++] = R[j++]; + + + + delete[] L; + + delete[] R; + + + + return count; + +} + +long mergeSort(long arr[], long left, long right) + +{ + + if (left == right) + + return 0; + + long nr_inv = 0; + + if (left < right) + + { + + long mij = (left + right) / 2; + + nr_inv += mergeSort(arr, left, mij); + + nr_inv += mergeSort(arr, mij + 1, right); + + nr_inv += merge(arr, left, mij, right); + + } + + return nr_inv; + +} + + + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + + + long n, S[100001]; + + f >> n; + + for (long i = 0; i < n; i++) + + f >> S[i]; + + long solutie = mergeSort(S, 0, n - 1); + + g << solutie % 9917; + + + + return 0; + +} diff --git a/invCount/merge/358fa746-6eed-4f7c-a070-c5bfca455cb1.cpp b/invCount/merge/358fa746-6eed-4f7c-a070-c5bfca455cb1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9291e1023cae85c917058795f09c034d88bb860 --- /dev/null +++ b/invCount/merge/358fa746-6eed-4f7c-a070-c5bfca455cb1.cpp @@ -0,0 +1,128 @@ +#include + + + + +using namespace std; + +int v[100005]; + +int interclaseaza(int st,int dr) + +{ + + int mij=(st+dr)/2,i=st,j=mij+1,n3=0,nU=0; + + int *u=new int[dr-st+2]; + + while (i<=mij && j<=dr) + + { + + if (v[i]<=v[j]) + + { + + u[++nU]=v[i]; + + i++; + + } + + else + + { + + u[++nU]=v[j]; + + n3=(n3+(mij-i+1))%9917; + + j++; + + } + + } + + while (i<=mij) + + { + + u[++nU]=v[i]; + + i++; + + } + + while (j<=dr) + + { + + u[++nU]=v[j]; + + j++; + + } + + for (i=st;i<=dr;i++) + + { + + v[i]=u[i-st+1]; + + } + + delete []u; + + return n3; + + + +} + +int inversiuni(int st, int dr) + +{ + + if (st==dr) + + return 0; + + int mij=(st+dr)/2; + + int n1=inversiuni(st,mij); + + int n2=inversiuni(mij+1,dr); + + return (n1+n2+interclaseaza(st,dr))%9917; + +} + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n,i; + + fin>>n; + + for (i=1;i<=n;i++) + + { + + fin>>v[i]; + + } + + fout< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int v[], int l, int r) + +{ + + if(l < r) + + { + + int mid = (l + r) / 2; + + MergeSort(v, l , mid); + + MergeSort(v, mid + 1 , r); + + int i = l, j = mid + 1, k = 0; + + while( i <= mid && j <= r ) + + if( v[i] <= v[j]) + + temp[++k] = v[i++]; + + else + + { + + temp[++k] = v[j++]; + + nr_inv = (nr_inv + mid - i + 1)%9917; + + } + + while(i <= mid) + + temp[++k] = v[i++]; + + while(j <= r) + + temp[++k] = v[j++]; + + for(i = l , j = 1 ; i <= r ; i ++ , j ++) + + v[i] = temp[j]; + + } + +} + + + +int main() + +{ + + + + int n, v[NR]; + + + + f>>n; + + for(int i = 1; i <= n; i++) + + f>>v[i]; + + + + MergeSort(v,1,n); + + + + g< +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX = 100001; + +long long nr_inv; + + + +void interclasare(int v[], int p, int u, int m) + +{ + + int a[NMAX]; + + int i = p, j = m + 1, k = 1, mij = (p + u)/2; + + while(i <= m && j <= u) + + if(v[i] <= v[j]) + + a[k++] = v[i++]; + + else + + { + + a[k++] = v[j++]; + + nr_inv += (mij - i + 1)%9917; + + } + + while(i <= m) a[k++] = v[i++]; + + while(j <= u) a[k++] = v[j++]; + + for(i = p, k = 1; i <= u; i++, k++) + + v[i] = a[k]; + +} + + + +void merge_sort(int v[], int p, int u) + +{ + + if(p < u) + + { + + int m = (p + u)/2; + + merge_sort(v, p, m); + + merge_sort(v, m + 1, u); + + interclasare(v, p, u, m); + + } + +} + + + +int main() + +{ + + int s[NMAX], n; + + f >> n; + + for(int i = 1; i <= n; i++) + + f >> s[i]; + + + + merge_sort(s, 1, n); + + g << nr_inv % 9917; + + return 0; + +} diff --git a/invCount/merge/35dc2738-137e-11eb-b7a2-309c234925a0.cpp b/invCount/merge/35dc2738-137e-11eb-b7a2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9861b0e71a9acee880fc65611ebfd8208c2480ce --- /dev/null +++ b/invCount/merge/35dc2738-137e-11eb-b7a2-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + + + +vector nums; + +int temp[100000]; + + + +long long merge(vector &arr, int left, int right) { + + int mid = (right + left) / 2; + + int i = left, j = mid + 1, k = left; + + long long inv_count = 0; + + + + while(i <= mid && j <= right) { + + if(arr[i] <= arr[j]) { + + temp[k++] = arr[i++]; + + } else { + + temp[k++] = arr[j++]; + + inv_count += (mid - i + 1) % 9917; + + } + + } + + + + while(i <= mid) + + temp[k++] = arr[i++]; + + + + while(j <= right) + + temp[k++] = arr[j++]; + + + + for(i = left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count % 9917; + +} + + + +long long mergeSort(vector &arr, int left, int right) { + + long long inv_count = 0; + + int mid; + + + + if(right > left) { + + mid = (right + left) / 2; + + + + inv_count += mergeSort(arr, left, mid); + + inv_count += mergeSort(arr, mid + 1, right); + + + + inv_count += merge(arr, left, right); + + } + + + + return inv_count % 9917; + +} + + + +int main() { + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + int n; + + + + fin >> n; + + for(int i = 0; i < n; i++) { + + int x; + + fin >> x; + + nums.push_back(x); + + } + + + + fout << mergeSort(nums, 0, n - 1); + +} diff --git a/invCount/merge/35ecf012-137e-11eb-b9fe-309c234925a0.cpp b/invCount/merge/35ecf012-137e-11eb-b9fe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d983de84b060c98045c3b4d9780efe5657757258 --- /dev/null +++ b/invCount/merge/35ecf012-137e-11eb-b9fe-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int v[100001],w[100001],n,nr=0; + +void inversiune(int v[],int st,int dr){ + + if(st>n; + + for(int i=1;i<=n;i++) + + in>>v[i]; + + inversiune(v,1,n); + + out< +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long X[100001],N,cnt,secv,nr,tmp[100001]; + + + +void MergeSort(long A[], int st, int dr) + +{ + + if(st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort(A, st , m); + + MergeSort(A, m + 1 , dr); + + int i = st; + + int j = m + 1; + + int k = 0; + + while( i <= m && j <= dr ) + + if( A[i] <= A[j]) + + tmp[++k] = A[i++]; + + else + + { + + tmp[++k] = A[j++]; + + nr = (nr+m-i+1)%9917; + + } + + while(i <= m) + + tmp[++k] = A[i++]; + + while(j <= dr) + + tmp[++k] = A[j++]; + + j=1; + + for(i = st; i<=dr ; i++ , j++) + + A[i] = tmp[j]; + + } + +} + +int main() + +{ + + f >> N; + + for ( int i=1;i<=N;i++ ) + + f >> X[i]; + + MergeSort(X,1,N); + + g << nr; + + return 0; + +} diff --git a/invCount/merge/360bc2b6-137e-11eb-b02b-309c234925a0.cpp b/invCount/merge/360bc2b6-137e-11eb-b02b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5160b448a93a60fa60fc80d0327ee2fae51b404 --- /dev/null +++ b/invCount/merge/360bc2b6-137e-11eb-b02b-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int v[1000001], v_nou[100001], i, inv, n; + +void sortare(int v[], int st, int dr) + +{ + + if ( st < dr ) + + {int m; + + m = (st + dr)/2; + + sortare(v,st,m); + + sortare(v,m+1,dr); + + int i,j,nr; + + i = st; j = m+1; + + nr = 0; + + while ( i <=m && j<=dr) + + if (v[i] <= v[j]) + + {nr++; + + v_nou[nr] = v[i]; + + i++;} + + else + + {nr++; + + v_nou[nr] = v[j]; + + j++; + + inv = (inv + m - i + 1)%9917; + + } + + while (i <= m) + + v_nou[++nr] = v[i++]; + + while(j <= dr) + + v_nou[++nr] = v[j++]; + + i = 1; + + for (j=st; j<=dr ; j++) + + { v[j] = v_nou[i]; + + i++; + + } + + } + +} + +int main() + +{ f>>n; + + for(i=1; i<=n; i++) + + f>>v[i]; + + sortare(v,1,n); + + g< + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long v[100000]; + +int n,poz[100000],dreapta; + +long long solutie; + +void MergeSort(long a[],int limInf, int limSup) + +{ + + if(limInf>n; + + for(int i=0;i>v[i]; + + MergeSort(v,0,n-1); + + fout< +#include +using namespace std; + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + +long long v[100003]; + +int nr=0,n; + +void merge(long long *v,int st, int dr,int mijloc) + +{ + + int temp[dr-st+1]; + + int i=st, j = mijloc+1,k=0; + + while(i<= mijloc and j<=dr) + + { + + if(v[i]<=v[j]) + + { + + temp[k++] = v[i]; + + i++; + + } + + else + + { + + nr += mijloc - i + 1; + + nr = nr % 9917; + + temp[k++] = v[j]; + + j++; + + } + + + + } + + while(i<=mijloc) + + { + + temp[k++] = v[i]; + + i++; + + } + + while(j<=dr) + + { + + temp[k++] = v[j]; + + j++; + + } + + for(i=st; i<=dr; i++) + + v[i] = temp[i-st]; + + + +} + +void mergeSort(long long v[],int st,int dr) + +{ + + if(st>n; + + for(int i=0; i>v[i]; + + + + mergeSort(v,0,n-1); + + + + + + g< +using namespace std; + +int merge(int v[], int temp[], int st, int mij, int dr){ + int i = st; + int j = mij; + int k = st; + int count = 0; + while((i <= mij - 1) && (j <= dr)){ + if(v[i] <= v[j]) + temp[k++] = v[i++]; + else { + temp[k++] = v[j++]; + count += (mij - i); + count = count % 9917; + } + } + while(i <= mij - 1) + temp[k++] = v[i++]; + while (j <= dr) + temp[k++] = v[j++]; + for(int l = st; l <= dr; l++) + v[l] = temp[l]; + return count; +} + +int nr(int v[], int temp[], int st, int dr){ + int mij = 0; + int count = 0; + if (dr > st){ + mij = (dr+st)/2; + count += nr(v, temp, st, mij); + count += nr(v, temp, mij + 1, dr); + count += merge(v, temp, st, mij + 1, dr); + } + return count; +} + +int mergesort(int v[], int n) { + int temp[100001]; + return nr(v, temp, 0, n-1); +} + + +int main() { + freopen("inv.in", "r", stdin); + freopen("inv.out", "w", stdout); + ios::sync_with_stdio(0); + cin.tie(0); + int n; + int S[100001]; + cin >> n; + for (int i = 0; i < n; i++) + cin >> S[i]; + int raspuns = mergesort(S,n); + cout << raspuns % 9917; + return 0; +} diff --git a/invCount/merge/3685ac80-137e-11eb-aac1-309c234925a0.cpp b/invCount/merge/3685ac80-137e-11eb-aac1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b510f5042f36be69114e90115adcfc25a0b0d38 --- /dev/null +++ b/invCount/merge/3685ac80-137e-11eb-aac1-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +int aux[100001]; +int interclasare(int v[], int l, int m, int r, int &nrinv){ + + int i = l; + int j = m+1; + int h=0; + + while(i <= m && j <= r){ + if(v[i] <= v[j]){ + aux[h] = v[i]; + h++; + i++; + }else{ + nrinv += m-i+1; + nrinv = nrinv % 9917; + aux[h] = v[j]; + h++; + j++; + } + } + + while(i <= m){ + aux[h] = v[i]; + h++; + i++; + } + + + while(j <= r) { + aux[h] = v[j]; + h++; + j++; + } + + for(i = l; i <= r;i++) + v[i] = aux[i-l]; + +} + +void mergeSort(int v[], int l, int r, int &nrInv){ + + if(l < r){ + int m = (l+r)/2; + + mergeSort(v, l, m, nrInv); + mergeSort(v, m+1, r, nrInv); + + interclasare(v, l, m, r, nrInv); + } +} + +int main() { + ifstream f("inv.in"); + ofstream g("inv.out"); + + int n; + f >> n; + + int v[n]; + for(int i = 0; i < n; i++) + f >> v[i]; + + int nr = 0; + mergeSort(v, 0, n-1, nr); + g << nr; + return 0; +} diff --git a/invCount/merge/3694a092-137e-11eb-89f6-309c234925a0.cpp b/invCount/merge/3694a092-137e-11eb-89f6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f1a9384a447ce6ffea4013c944d7d1f48669a7f --- /dev/null +++ b/invCount/merge/3694a092-137e-11eb-89f6-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n,v[100005],aux[100005],nr; + + + + + +void mergesort(int l, int r) + +{ + + if(l >= r) + + return; + + int i, j, loc = l; + + int m = (l + r) / 2; + + mergesort (l, m); + + mergesort (m+1, r); + + i = l; + + j = m + 1; + + while (i <= m && j <= r) + + { + + if (v[i] <= v[j]) + + { + + aux[loc++] = v[i++]; + + } + + else + + { + + nr = (nr + m - i + 1) % 9917; + + aux[loc++] = v[j++]; + + } + + } + + while( i <= m) + + { + + aux[loc++] = v[i++]; + + } + + while(j <= r) + + { + + aux[loc++] = v[j++]; + + } + + for(int i = l; i <= r; i++) + + { + + v[i] = aux[i]; + + } + +} + + + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; i++) + + { + + f >> v[i]; + + } + + mergesort(1, n); + + g << nr; + + f.close(); + + g.close(); + +} diff --git a/invCount/merge/36a36da4-137e-11eb-acdf-309c234925a0.cpp b/invCount/merge/36a36da4-137e-11eb-acdf-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83d6f3cfca12c84c54c87b124947cc8881a5d378 --- /dev/null +++ b/invCount/merge/36a36da4-137e-11eb-acdf-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include +#include + + +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(i=0; i>v[i]; + + mergeSort(v, 0, n-1); + + g << nr; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invCount/merge/36b19e74-137e-11eb-84f5-309c234925a0.cpp b/invCount/merge/36b19e74-137e-11eb-84f5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72765edcc4bd10480b46dc2f2402eade110127fd --- /dev/null +++ b/invCount/merge/36b19e74-137e-11eb-84f5-309c234925a0.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(int i=0; i>v[i]; + + f.close(); + + mergeSort(v, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/36e139f6-137e-11eb-8fba-309c234925a0.cpp b/invCount/merge/36e139f6-137e-11eb-8fba-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d516b055f6d5f56fcf20282d3330b8942ac3534c --- /dev/null +++ b/invCount/merge/36e139f6-137e-11eb-8fba-309c234925a0.cpp @@ -0,0 +1,165 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + +void interclasare(int arr[], int s, int d); + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + + + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/36f0ca50-137e-11eb-81ae-309c234925a0.cpp b/invCount/merge/36f0ca50-137e-11eb-81ae-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b78332ce4b1a2d5e04f06c1685a6b6189349af4d --- /dev/null +++ b/invCount/merge/36f0ca50-137e-11eb-81ae-309c234925a0.cpp @@ -0,0 +1,161 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + + + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/36ffbe70-137e-11eb-8cab-309c234925a0.cpp b/invCount/merge/36ffbe70-137e-11eb-8cab-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ea242d9aa73807cfcbf1d8ca3612ba49441baa7 --- /dev/null +++ b/invCount/merge/36ffbe70-137e-11eb-8cab-309c234925a0.cpp @@ -0,0 +1,165 @@ +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n, arr[100005]; + +long long cnt; + + + +void interclasare(int st1, int dr1, int st2, int dr2){ + + + + int i = st1; + + int j = st2; + + + + int k = 0; + + + + int * auxarr = new int[dr1 - st1 + dr2 - st2 + 5]; + + + + while(i <= dr1 && j <= dr2){ + + + + if(arr[i] <= arr[j]){ + + + + auxarr[k] = arr[i]; + + i += 1; + + } + + else{ + + + + cnt+= dr1 - i + 1; + + cnt %= 9917; + + + + auxarr[k] = arr[j]; + + j += 1; + + } + + k += 1; + + } + + + + while(i <= dr1){ + + + + auxarr[k] = arr[i]; + + i += 1; + + k += 1; + + } + + + + while(j <= dr2){ + + + + auxarr[k] = arr[j]; + + j += 1; + + k += 1; + + } + + + + for(int i = 0; i < k; i++) + + arr[st1 + i] = auxarr[i]; + + + + delete[] auxarr; + +} + + + +void mergesort(int st, int dr){ + + + + if(st >= dr) + + return; + + + + int m = (st + dr) / 2; + + + + mergesort(st, m); + + mergesort(m + 1, dr); + + + + interclasare(st, m, m + 1, dr); + +} + + + + + +int main(){ + + + + f>>n; + + + + for(int i = 0; i < n; i++) + + f>>arr[i]; + + + + mergesort(0, n - 1); + + + + g< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int merge(int arr[], int l, int m, int r) + +{ + + int i, j, k; + + int n1 = m - l + 1; + + int n2 = r - m; + + int inv = 0; + + int L[n1], R[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = arr[l + i]; + + for (j = 0; j < n2; j++) + + R[j] = arr[m + 1+ j]; + + + + i = 0; + + j = 0; + + k = l; + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) + + { + + arr[k] = L[i]; + + i++; + + inv += j; + + inv %= 9917; + + } + + else + + { + + arr[k] = R[j]; + + j++; + + } + + k++; + + } + + + + while (i < n1) + + { + + arr[k] = L[i]; + + i++; + + k++; + + inv += j; + + inv %= 9917; + + } + + + + while (j < n2) + + { + + arr[k] = R[j]; + + j++; + + k++; + + } + + inv %= 9917; + + return inv; + +} + + + +int mergeSort(int arr[], int l, int r) + +{ + + if (l < r) + + { + + int m = l+(r-l)/2; + + int inv = 0; + + + + inv += mergeSort(arr, l, m); + + inv %= 9917; + + inv += mergeSort(arr, m+1, r); + + inv %= 9917; + + + + inv += merge(arr, l, m, r); + + inv %= 9917; + + return inv; + + } + + return 0; + +} + + + +int v[100000]; + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i = 0; i < n; i++) + + fin >> v[i]; + + int inv = mergeSort(v, 0, n-1); + + inv %= 9917; + + fout << inv; + + return 0; + +} diff --git a/invCount/merge/371f5468-137e-11eb-bb37-309c234925a0.cpp b/invCount/merge/371f5468-137e-11eb-bb37-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11fa87b29fb9d25dde04a9cf5e7305aa873d0de3 --- /dev/null +++ b/invCount/merge/371f5468-137e-11eb-bb37-309c234925a0.cpp @@ -0,0 +1,165 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + +void interclasare(int arr[], int s, int d); + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + + + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/374e75d8-137e-11eb-ab44-309c234925a0.cpp b/invCount/merge/374e75d8-137e-11eb-ab44-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edff99bd59b36c6ee47b3c7969f6703e5f49f9f9 --- /dev/null +++ b/invCount/merge/374e75d8-137e-11eb-ab44-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include + +std::ifstream fin("inv.in"); +std::ofstream fout("inv.out"); + +int n, inversions, v[100001]; + +void merge(int v[], int left, int right) +{ + int mid = (left + right) / 2; + int i = left, j = mid + 1; + int aux[right - left + 1], index = 0; + while(i <= mid && j <= right) + if(v[i] <= v[j]) + aux[index++] = v[i++]; + else + { + aux[index++] = v[j++]; + inversions += (mid - i + 1); + inversions = inversions % 9917; + } + + while(i <= mid) + aux[index++] = v[i++]; + while(j <= right) + aux[index++] = v[j++]; + + for(i = 0; i < index; i++) + v[left + i] = aux[i]; + +} + +int mergeSort(int v[], int left, int right) +{ + if(left < right) + { + mergeSort(v, left, (left + right) / 2); + mergeSort(v, (left + right) / 2 + 1, right); + merge(v, left, right); + } + + return inversions; +} + + +int main() +{ + fin >> n; + for(int i = 0; i < n; i++) + fin >> v[i]; + fout << mergeSort(v, 0, n - 1); + + return 0; +} diff --git a/invCount/merge/375fcb5a-137e-11eb-ab32-309c234925a0.cpp b/invCount/merge/375fcb5a-137e-11eb-ab32-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8581b71f37dc4b494c1c6ef9c561d1e68021c57 --- /dev/null +++ b/invCount/merge/375fcb5a-137e-11eb-ab32-309c234925a0.cpp @@ -0,0 +1,156 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int aux; + +int v[100001], u[100001]; + + + +void interclasare(int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + u[k] = v[i]; + + i++; + + k++; + + } + + else { + + aux = (aux + mij - i + 1) % 9917; + + u[k] = v[j]; + + k++; + + j++; + + + + } + + } + + + + while (i <= mij) + + { + + u[k] = v[i]; + + i++; + + k++; + + } + + + + while (j <= mij) + + { + + u[k] = v[j]; + + j++; + + k++; + + } + + + + for (int i = 0; i < k; i++) + + v[i + st] = u[i]; + +} + + + + + +void mergesort(int st, int dr) + +{ + + if (st < dr) + + { + + int mij = (st + dr) / 2; + + mergesort(st, mij); + + mergesort(mij + 1, dr); + + interclasare(st, dr); + + } + +} + + + +int main() + +{ + + int n; + + + + f >> n; + + + + for(int i = 0; i < n; i++) + + f >> v[i]; + + + + mergesort(0, n - 1); + + + + g << aux << '\n'; + + return 0; + +} diff --git a/invCount/merge/37706d34-137e-11eb-9fbd-309c234925a0.cpp b/invCount/merge/37706d34-137e-11eb-9fbd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34599df479cb8e703b7f6295396f9561dfc76a3c --- /dev/null +++ b/invCount/merge/37706d34-137e-11eb-9fbd-309c234925a0.cpp @@ -0,0 +1,145 @@ +#include +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g ( "inv.out"); + +int nr; + + + +void interclasare(int a[], int l, int r){ + + + + + + + +int mid= (l + r) / 2; + + + +int i = l; + +int j = mid + 1; + +int aux = 0; + + + +int size = r - l + 1; + +int v[size]; + + + +while ( i <= mid and j <= r) { + +if (a[i] <= a[j]) { + +v[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mid - i + 1; + +nr = nr % 9917; + +v[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mid) { + +v[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= r) { + + v[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int i= 0; i < aux; ++i) { + +a[l+i] = v[i]; + +} + + + +} + + + +void mergesort(int a[], int l, int r){ + +if (l == r) + +return; + +int mid = (l + r) / 2; + +mergesort(a, l, mid); + +mergesort(a, mid + 1, r); + +interclasare(a, l, r); + + + +} + + + +int main() { + + + + + +int n; + +int vt[100005]; + +f >> n; + +for (int i = 0; i < n; ++i) { + +f>> vt[i]; + +} + +mergesort(vt, 0, n-1); + +g<< nr; + +return 0; + +} diff --git a/invCount/merge/37802498-137e-11eb-93c9-309c234925a0.cpp b/invCount/merge/37802498-137e-11eb-93c9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cbef36614ca9d9e3f2c6e37ad35a9d8c9ddbfc9 --- /dev/null +++ b/invCount/merge/37802498-137e-11eb-93c9-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, v[100005]; + + + +int Merge(int st, int dr) + +{ + + int nrInv = 0; + + int mij = (st+dr)/2, i = st, j = mij+1, k = 0; + + int aux[dr-st+1]; + + while(i <= mij && j <= dr) + + if(v[i] <= v[j]) aux[k++] = v[i++]; + + else + + { + + aux[k++] = v[j++]; + + nrInv += (mij-i+1); + + nrInv %= 9917; + + } + + while(i <= mij) aux[k++] = v[i++]; + + while(j <= dr) aux[k++] = v[j++]; + + for(int i = 0; i < k; ++i) v[st+i] = aux[i]; + + return nrInv; + +} + + + +int countInv(int st, int dr) + +{ + + int nrInv = 0; + + if(st < dr) + + { + + nrInv += countInv(st, (st+dr)/2); + + nrInv += countInv((st+dr)/2 + 1, dr); + + nrInv += Merge(st, dr); + + nrInv %= 9917; + + } + + return nrInv; + +} + + + +int main() + +{ + + fin >> n; + + for(int i = 1; i <= n; ++i) fin >> v[i]; + + fin.close(); + + fout << countInv(1, n); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/37909f7e-137e-11eb-8a96-309c234925a0.cpp b/invCount/merge/37909f7e-137e-11eb-8a96-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec16ac7503450bad0bbea936e555a2d13e2a3e5d --- /dev/null +++ b/invCount/merge/37909f7e-137e-11eb-8a96-309c234925a0.cpp @@ -0,0 +1,176 @@ +#include +#include + + +std::ifstream f; + +long merge(long arr[], long left, long mid, long right) + +{ + + long count = 0; + + long i, j, k; + + + + long n1 = mid - left + 1; + + long n2 = right - mid; + + + + long* L = new long[n1]; + + long* R = new long[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = arr[left + i]; + + + + for (j = 0; j < n2; j++) + + R[j] = arr[mid + 1 + j]; + k = left; + + i = 0; + + j = 0; + + + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) { + + arr[k] = L[i]; + + i++; + + + + } + + else { + + arr[k] = R[j]; + + j++; + + count += n1 - i; + + } + + k++; + + } + + + + while (i < n1) + + { + + arr[k++] = L[i++]; + + } + + + + while (j < n2) + + { + + arr[k++] = R[j++]; + + } + + + + delete[] L; + + delete[] R; + + + + return count; + +} + +long mergeSort(long arr[], long left, long right) + +{ + + if (left == right) + + return 0; + + long inv_count = 0; + + if (left < right) + + { + + long mid = (left + right) / 2; + + + + inv_count += mergeSort(arr, left, mid); + + inv_count += mergeSort(arr, mid + 1, right); + + + + inv_count += merge(arr, left, mid, right); + + } + + return inv_count; + +} + + + +int main() + +{ + + long n, a[100010]; + + + + f.open("inv.in"); + + + + f >> n; + + for (long i = 0; i < n; i++) + + f >> a[i]; + + f.close(); + + + + std::ofstream g("inv.out"); + + long solutie = mergeSort(a, 0, n - 1); + + g << solutie % 9917; + + + + g.close(); + + + + return 0; + +} diff --git a/invCount/merge/37a1b8be-137e-11eb-be96-309c234925a0.cpp b/invCount/merge/37a1b8be-137e-11eb-be96-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dfaff468a96a1dd20f739fbff402825dcabbaf8 --- /dev/null +++ b/invCount/merge/37a1b8be-137e-11eb-be96-309c234925a0.cpp @@ -0,0 +1,145 @@ +#include +#include + + + + +int nr=0; + + + +void interclasare(int a[], int l, int r){ + + + + + + + +int mid= (l + r) / 2; + + + +int i = l; + +int j = mid + 1; + +int aux = 0; + + + +int size = r - l + 1; + +int v[size]; + + + +while ( i <= mid and j <= r) { + +if (a[i] <= a[j]) { + +v[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mid - i + 1; + +nr = nr % 9917; + +v[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mid) { + +v[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= r) { + + v[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int i= 0; i < aux; ++i) { + +a[l+i] = v[i]; + +} + + + +} + + + +void mergesort(int a[], int l, int r){ + +if (l == r) + +return; + +int mid = (l + r) / 2; + +mergesort(a, l, mid); + +mergesort(a, mid + 1, r); + +interclasare(a, l, r); + + + +} + + + +int main() { + + + +std::ifstream f("inv.in"); + +std::ofstream g ( "inv.out"); + +int n; + +int vt[100005]; + +f >> n; + +for (int i = 0; i < n; ++i) { + +f>> vt[i]; + +} + +mergesort(vt, 0, n-1); + +g<< nr; + +return 0; + +} diff --git a/invCount/merge/37d37714-137e-11eb-ac55-309c234925a0.cpp b/invCount/merge/37d37714-137e-11eb-ac55-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20192d64b32824b1456055f8afa44de876c6c8c9 --- /dev/null +++ b/invCount/merge/37d37714-137e-11eb-ac55-309c234925a0.cpp @@ -0,0 +1,139 @@ +#include +#include + + +int nr = 0; + + + +void interclasare(int a[], int st, int dr){ + + + + int mijloc = (st + dr) / 2; + + + + int i = st; + + int j = mijloc + 1; + + int aux = 0; + + + + int size = dr - st + 1; + + int arr[size]; + + + + while ( i <= mijloc and j <= dr) { + + if (a[i] <= a[j]) { + + arr[aux] = a [i]; + + aux++; + + i++; + + } else { + + nr += mijloc - i + 1; + + nr = nr % 9917; + + arr[aux] = a[j]; + + aux++; + + j++; + + } + + } + + + + while (i <= mijloc) { + + arr[aux] = a[i]; + + aux++; + + i++; + + } + + while (j <= dr) { + + arr[aux] = a[j]; + + aux++; + + j++; + + } + + + + for (int k = 0; k < aux; ++k) { + + a[st+k] = arr[k]; + + } + + + +} + + + +void mergesort(int a[], int st, int dr){ + + if (st == dr) + + return; + + int mijloc = (st + dr) / 2; + + mergesort(a, st, mijloc); + + mergesort(a, mijloc + 1, dr); + + interclasare(a, st, dr); + + + +} + + + +int main() { + + std :: ifstream fin("inv.in"); + + std :: ofstream fout ( "inv.out"); + + + + int n; + + int vt[100005]; + + fin >> n; + + for (int i = 0; i < n; ++i) { + + fin >> vt[i]; + + } + + mergesort(vt, 0, n-1); + + fout << nr; + + return 0; + +} diff --git a/invCount/merge/37e1fa7e-137e-11eb-9adf-309c234925a0.cpp b/invCount/merge/37e1fa7e-137e-11eb-9adf-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..181f9b12a962e7b5d05d9d5518f921fd95a29748 --- /dev/null +++ b/invCount/merge/37e1fa7e-137e-11eb-9adf-309c234925a0.cpp @@ -0,0 +1,91 @@ +#include +#include +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int nr = 0; + +void interclasare(int v[], int aux[], int st, int m, int dr) { + + int i, j, k; + + i = st; + + j = m; + + k = st; + + while ((i<=m-1)&&(j<=dr)) { + + if (v[i]<=v[j]) + + aux[k++]=v[i++]; + + else{ + + aux[k++] = v[j++]; + + nr += (m-i); + + nr = nr%9917; + + } + + } + + while (i<=m-1) + + aux[k++] = v[i++]; + + while (j<=dr) + + aux[k++] = v[j++]; + + for (i=st;i<=dr;i++) + + v[i] = aux[i]; + +} + + + +void mergeSort(int v[], int aux[], int st, int dr) { + + int m; + + if (dr>st){ + + m = (dr+st)/2; + + mergeSort(v, aux, st, m); + + mergeSort(v, aux, m+1, dr); + + interclasare(v, aux, st, m+1, dr); + + } + +} + + + + + +int main() { + + int v[100005],n,aux[100005]; + + in>>n; + + for(int i=0;i>v[i]; + + mergeSort(v, aux, 0, n-1); + + out< +#include + + +using namespace std; + + + +const int maxN = 100001, + + MOD = 9917; + + + +int a[maxN], b[maxN]; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int interClas(int p, int q, int m) { + + int i = p, + + j = m+1, + + k = 0, + + inv = 0; + + while(i <= m && j <= q) + + if(a[i] <= a[j]) + + b[++k] = a[i++]; + + else { + + b[++k] = a[j++]; + + inv = (inv+m-i+1)%MOD; + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= q) + + b[++k] = a[j++]; + + k = 1; + + for(int i = p; i <= q; i++) + + a[i] = b[k++]; + + return inv; + +} + + + +int mergeSort(int p, int q) { + + int inv = 0; + + if(p < q) { + + int m = (p+q)/2; + + inv = mergeSort(p, m)%MOD; + + inv = (inv+mergeSort(m+1, q))%MOD; + + inv = (inv+interClas(p, q, m))%MOD; + + } + + return inv%MOD; + +} + + + +int main() + +{ + + int n; + + in >> n; + + for(int i = 1; i <= n; i++) + + in >> a[i]; + + + + out << mergeSort(1, n); + + return 0; + +} diff --git a/invCount/merge/38116eee-137e-11eb-ae7a-309c234925a0.cpp b/invCount/merge/38116eee-137e-11eb-ae7a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b28764af884c0e9d22a5a42bb376d652945d6228 --- /dev/null +++ b/invCount/merge/38116eee-137e-11eb-ae7a-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include + + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +int n; +int v[100005]; + +int Merge(int v[], int l, int m, int r) +{ + int i, j, k, res; + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + + for(i = 0;i < n1;++i) + L[i] = v[l + i]; + for(int j = 0;j < n2;++j) + R[j] = v[j + m + 1]; + i = j = res = 0; + k = l; + while(i < n1 && j < n2) + { + if(L[i] <= R[j]) + v[k++] = L[i++]; + else + { + v[k++] = R[j++]; + res += n1 - i; + res %= 9917; + } + } + while(i < n1) + v[k++] = L[i++]; + while(j < n2) + v[k++] = R[j++]; + return res; +} + +int MergeSort(int v[], int l, int r) +{ + if(l < r) + { + int res = 0; + int m = (l + r) >> 1; + res = (res + MergeSort(v, l, m)) % 9917; + res = (res + MergeSort(v, m + 1, r)) % 9917; + res = (res + Merge(v, l, m, r)) % 9917; + return res; + } + else + return 0; +} + +void Read() +{ + f>>n; + for(int i = 0;i < n;++i) + f>>v[i]; +} + +int main() +{ + Read(); + g< +#include + + +int nr = 0; + + + +void interclasare(int a[], int st, int dr){ + + + +int mijloc = (st + dr) / 2; + + + +int i = st; + +int j = mijloc + 1; + +int aux = 0; + + + +int size = dr - st + 1; + +int arr[size]; + + + +while ( i <= mijloc and j <= dr) { + +if (a[i] <= a[j]) { + +arr[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mijloc - i + 1; + +nr = nr % 9917; + +arr[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mijloc) { + +arr[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= dr) { + +arr[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int k = 0; k < aux; ++k) { + +a[st+k] = arr[k]; + +} + + + +} + + + +void mergesort(int a[], int st, int dr){ + +if (st == dr) + +return; + +int mijloc = (st + dr) / 2; + +mergesort(a, st, mijloc); + +mergesort(a, mijloc + 1, dr); + +interclasare(a, st, dr); + + + +} + + + +int main() { + +std :: ifstream fin("inv.in"); + +std :: ofstream fout ( "inv.out"); + + + +int n; + +int vt[100005]; + +fin >> n; + +for (int i = 0; i < n; ++i) { + +fin >> vt[i]; + +} + +mergesort(vt, 0, n-1); + +fout << nr; + +return 0; + +} diff --git a/invCount/merge/38411b4c-137e-11eb-b733-309c234925a0.cpp b/invCount/merge/38411b4c-137e-11eb-b733-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..477c812e669c6f97c5e2a71f16fb692c606bbfd1 --- /dev/null +++ b/invCount/merge/38411b4c-137e-11eb-b733-309c234925a0.cpp @@ -0,0 +1,71 @@ +#include +#include + +int nr = 0; + +void interclasare(int a[], int st, int dr){ + + int mijloc = (st + dr) / 2; + + int i = st; + int j = mijloc + 1; + int aux = 0; + + int size = dr - st + 1; + int arr[size]; + + while ( i <= mijloc and j <= dr) { + if (a[i] <= a[j]) { + arr[aux] = a [i]; + aux++; + i++; + } else { + nr += mijloc - i + 1; + nr = nr % 9917; + arr[aux] = a[j]; + aux++; + j++; + } + } + + while (i <= mijloc) { + arr[aux] = a[i]; + aux++; + i++; + } + while (j <= dr) { + arr[aux] = a[j]; + aux++; + j++; + } + + for (int k = 0; k < aux; ++k) { + a[st+k] = arr[k]; + } + +} + +void mergesort(int a[], int st, int dr){ + if (st == dr) + return; + int mijloc = (st + dr) / 2; + mergesort(a, st, mijloc); + mergesort(a, mijloc + 1, dr); + interclasare(a, st, dr); + +} + +int main() { + std :: ifstream fin("inv.in"); + std :: ofstream fout ( "inv.out"); + + int n; + int vt[100005]; + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> vt[i]; + } + mergesort(vt, 0, n-1); + fout << nr; + return 0; +} diff --git a/invCount/merge/3853b8f6-137e-11eb-ac28-309c234925a0.cpp b/invCount/merge/3853b8f6-137e-11eb-ac28-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63eda1b90b114c54aa59a621e2a78378d3cbb2e9 --- /dev/null +++ b/invCount/merge/3853b8f6-137e-11eb-ac28-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +const int MOD = 9917; +int v[100000], n; + +long long Merge(int arr[], int aux[], int left, int mid, int right) { + int i, j, k = 0; + long long inv_count = 0; + i = left, j = mid, k = left; + while(i <= mid-1 && j <= right) { + if(arr[i] <= arr[j]) + aux[k++] = arr[i++]; + else { + aux[k++] = arr[j++]; + inv_count += (mid - i); + } + } + while(i <= mid-1) + aux[k++] = arr[i++]; + while(j <= right) + aux[k++] = arr[j++]; + for(i=left; i<=right; i++) + arr[i] = aux[i]; + return inv_count; +} + +long long mergeSort(int arr[], int aux[], int left, int right) { + long long inv_count = 0; + if(left < right) { + int mid = (left + right) >> 1; + inv_count = mergeSort(arr,aux,left,mid); + inv_count += mergeSort(arr,aux,mid+1,right); + inv_count += Merge(arr,aux,left,mid+1,right); + } + return inv_count; +} + +int main() { + long long ans; + int aux[100000]; + FILE *fin, *fout; + fin = fopen("inv.in","r"); + fout = fopen("inv.out","w"); + fscanf(fin,"%d",&n); + for(int i=0; i +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int max_size = 100000 + 1; + + + +int ans; + +vector v(max_size), temp(max_size); + + + +void interclasare(int s, int m, int d) { + + + + int i = s, j = m + 1, k = 0; + + + + while (i <= m && j <= d) { + + + + if (v[i] <= v[j]) + + temp[k++] = v[i++]; + + + + else { + + ans = (ans + m - i + 1) % 9917; + + temp[k++] = v[j++]; + + } + + + + } + + + + while (i <= m) + + temp[k++] = v[i++]; + + + + while (j <= d) + + temp[k++] = v[j++]; + + + + for (k = s; k <= d; ++k) + + v[k] = temp[k - s]; + +} + + + +void merge_sort(int s, int d) { + + if (s < d) { + + int m = (s + d) / 2; + + merge_sort(s, m); + + merge_sort(m + 1, d); + + interclasare(s, m, d); + + } + +} + + + +int main() { + + int N; + + in >> N; + + + + for (int i = 0; i < N; ++i) + + in >> v.at(i); + + + + merge_sort(0, N - 1); + + + + out << ans; + + + + return 0; + +} diff --git a/invCount/merge/38917b18-137e-11eb-8ec9-309c234925a0.cpp b/invCount/merge/38917b18-137e-11eb-8ec9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac717b96a16152d3f3862f5294978b8a85b2e695 --- /dev/null +++ b/invCount/merge/38917b18-137e-11eb-8ec9-309c234925a0.cpp @@ -0,0 +1,175 @@ +#include +#include +using namespace std; + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + +int a[100005]; + +void merging(int p[100005], int st, int mij, int dr, long long& nr_swaps) + +{ + + int l[mij-st+2]; + + int r[dr-mij+1]; + + for(int i=1; i<=mij-st+1; i++) + + { + + l[i]=p[st+i-1]; + + } + + for(int i=1; i<=dr-mij; i++) + + { + + r[i]=p[mij+i]; + + } + + int i=1, j=1; + + int t=st; + + int pate=0; + + while(i<=mij-st+1 && j<=dr-mij) + + { + + if(r[j]>n; + + for(int i=1; i<=n; i++) + + { + + f>>a[i]; + + } + + long long rez=0; + + merge_sort(a, 1, n, rez); + + g< +#include +using namespace std; + + + +int v[100001], c[100001]; + +int n; + +long long inv; + + + +void interclasare(int st,int mij,int dr) + +{ + + int k=1,i=st,j=mij+1; + + while(i<=mij&&j<=dr) + + if(v[i]<=v[j]) + + c[k++]=v[i++]; + + else + + { + + inv += mij - i + 1 ; + + c[k++]=v[j++]; + + + + } + + while(i<=mij) + + c[k++]=v[i++]; + + while(j<=dr) + + c[k++]=v[j++]; + + k--; + + for(i=1;i<=k;i++) + + v[st+i-1]=c[i]; + + + +} + + + +void divide(int st,int dr) + +{ + + if(st>n; + + for(i=1;i<=n;i++) + + fin>>v[i]; + + + + divide(1,n); + + + + fout << inv % 9917; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/38b0c4d8-137e-11eb-886f-309c234925a0.cpp b/invCount/merge/38b0c4d8-137e-11eb-886f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a8138f477b6e90984dfdf998900739eb76789a5 --- /dev/null +++ b/invCount/merge/38b0c4d8-137e-11eb-886f-309c234925a0.cpp @@ -0,0 +1,160 @@ +#include + + +const int MOD = 9917; + + + +using namespace std; + + + +int Merge(int* v, int* aux, int left, int middle, int right) + +{ + + int index = left; + + int i1 = left; + + int i2 = middle + 1; + + + + int r = 0; + + while (i1 <= middle && i2 <= right) + + { + + if (v[i1] <= v[i2]) + + aux[index++] = v[i1++]; + + else + + { + + aux[index++] = v[i2++]; + + r += middle - i1 + 1; + + r %= MOD; + + } + + } + + + + while (i1 <= middle) + + aux[index++] = v[i1++]; + + + + while (i2 <= right) + + aux[index++] = v[i2++]; + + + + for (int i = left; i <= right; i++) + + v[i] = aux[i]; + + + + return r; + +} + + + +int MergeSort(int* v, int* aux, int left, int right) + +{ + + if (left >= right) + + return 0; + + + + int result = 0; + + int middle = (left + right) / 2; + + + + result += MergeSort(v, aux, left, middle); + + result %= MOD; + + result += MergeSort(v, aux, middle + 1, right); + + result %= MOD; + + result += Merge(v, aux, left, middle, right); + + result %= MOD; + + + + return result; + +} + + + +int main() + +{ + + int n; + + int* v; + + int* aux; + + ifstream fin("inv.in"); + + fin >> n; + + + + v = new int[n]; + + aux = new int[n]; + + + + for (int i = 0; i < n; i++) + + fin >> v[i]; + + fin.close(); + + + + ofstream fout("inv.out"); + + fout << MergeSort(v, aux, 0, n - 1); + + fout.close(); + + + + delete[] aux; + + aux = NULL; + + delete[] v; + + v = NULL; + + + + return 0; + +} diff --git a/invCount/merge/38bed646-137e-11eb-825a-309c234925a0.cpp b/invCount/merge/38bed646-137e-11eb-825a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79a14d449d1d482bfb1ed3b51dd912ba0ff18bca --- /dev/null +++ b/invCount/merge/38bed646-137e-11eb-825a-309c234925a0.cpp @@ -0,0 +1,143 @@ +#include +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int aux[100001]; + +int n; + +void afisare(int v[]) + +{ + + int i; + + for(i=1; i<=n; i++) + + out<>n; + + for(i=1; i<=n; i++) + + in>>v[i]; + + out << sortare(v,1,n)%9917; + + return 0; + +} diff --git a/invCount/merge/38e12a1c-137e-11eb-9177-309c234925a0.cpp b/invCount/merge/38e12a1c-137e-11eb-9177-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7216c415fd957a554996d53ea54c6780022866f6 --- /dev/null +++ b/invCount/merge/38e12a1c-137e-11eb-9177-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int nr=0, v[100001], nou[100001]; + +void interclasare(int left, int mid, int right) + +{ + + int i=left; + + int j=mid+1; + + int k=0; + + while(i<=mid && j<=right) + + { + + if(v[i]<=v[j]) + + nou[k++]=v[i++]; + + else + + { + + nr+=mid-i+1; + + nr=nr%9917; + + nou[k++]=v[j++]; + + } + + } + + + + while(i<=mid) + + nou[k++]=v[i++]; + + while(j<=right) + + nou[k++]=v[j++]; + + for(k=left;k<=right;k++) + + v[k]=nou[k-left]; + +} + + + +void mergesort(int left, int right) + +{ + + if(left>n; + +for(int i=0;i>v[i]; + +mergesort(0,n-1); + +g< +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + +int w[100001]; + +int Interclasare(int v[], int left, int right); +int MergeSort(int v[], int left, int right); + +int main() +{ + int n; + int i; + fin >> n; + int v[n]; + for (i = 0; i < n; i++) + fin >> v[i]; + fout << MergeSort(v, 0, n - 1) % 9917; + return 0; +} + +int MergeSort(int v[], int left, int right) +{ + if (left == right) + { + return 0; + } + int m = (left + right) / 2; + int inversiune = 0; + inversiune += MergeSort(v, left, m); + inversiune += MergeSort(v, m + 1, right); + inversiune += Interclasare(v, left, right); + return inversiune % 9917; +} +int Interclasare(int v[], int left, int right) +{ + int i, j, k; + int m = (left + right) / 2; + i = left; + k = left; + j = m + 1; + int inversiune = 0; + while (i <= m && j <= right) + { + if (v[i] <= v[j]) + { + w[k] = v[i]; + i++; + k++; + } + else + { + w[k] = v[j]; + inversiune += m - i + 1; + j++; + k++; + } + } + while (i <= m) + { + w[k] = v[i]; + k++; + i++; + } + while (j <= right) + { + w[k] = v[j]; + k++; + j++; + } + for (k = left; k <= right; k++) + { + v[k] = w[k]; + } + return inversiune % 9917; +} diff --git a/invCount/merge/39026dba-137e-11eb-85e5-309c234925a0.cpp b/invCount/merge/39026dba-137e-11eb-85e5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbabfe8d695e54b5810d6d92099e75643b06cd7d --- /dev/null +++ b/invCount/merge/39026dba-137e-11eb-85e5-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include +#include + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100001]; + +long long merge(int v[], int st, int dr) +{ + int stanga, dreapta, k, m = (st + dr) / 2; + stanga = k = st; + dreapta = m + 1; + long long r = 0; + while (stanga <= m && dreapta <= dr) + { + if (v[stanga] <= v[dreapta]) + { + aux[k++] = v[stanga++]; + } + else + { + aux[k++] = v[dreapta++]; + r += m - stanga + 1; + } + } + while (stanga <= m) + { + aux[k++] = v[stanga++]; + } + while (dreapta <= dr) + { + aux[k++] = v[dreapta++]; + } + for (k = st; k <= dr; k++) + { + v[k] = aux[k]; + } + return r; +} + +long long mergesort(int v[], int st, int dr) +{ + if (st == dr) + { + return 0; + } + int m = (st + dr) / 2; + long long r = 0; + r += mergesort(v, st, m); + r += mergesort(v, m + 1, dr); + r += merge(v, st, dr); + return r; +} + +int main() +{ + int n; + in>>n; + int v[n + 1]; + for(int i=1; i<=n; i++) + in>>v[i]; + out << mergesort(v,1,n)%9917; + in.close(); + out.close(); + return 0; +} diff --git a/invCount/merge/3913d2e6-137e-11eb-b099-309c234925a0.cpp b/invCount/merge/3913d2e6-137e-11eb-b099-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..120775fc4a26b3cbd0dc994e9cc7034b89b4ee97 --- /dev/null +++ b/invCount/merge/3913d2e6-137e-11eb-b099-309c234925a0.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + + +const ll MOD = 9917; +const int INF = INT_MAX; +const int maxN = 100005; + +ll n, a[maxN], poz[maxN]; + +ll swaps = 0; + + + +void interclasare(ll a[], ll left, ll mid, ll right){ + + ll i = left; + + ll j = mid + 1; + + ll index = left; + + + + while (i <= mid && j <= right){ + + if (a[i] <= a[j]){ + + poz[index] = a[i]; + + index++; i++; + + } + + else{ + + poz[index] = a[j]; + + index++; j++; + + swaps = (swaps + (mid - i + 1)) % MOD; + + } + + } + + + + while(i <= mid){ + + poz[index] = a[i]; + + index++; i++; + + } + + + + while(j <= right){ + + poz[index] = a[j]; + + index++; j++; + + } + + + + for (ll i = left; i <= right; i++) + + a[i] = poz[i]; + +} + + + +void mergesort(ll a[], ll left, ll right){ + + if (left < right){ + + ll mid = left + (right - left) / 2; + + mergesort(a, left, mid); + + mergesort(a, mid + 1, right); + + interclasare(a, left, mid, right); + + } + +} + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + + cin >> n; + + for (ll i = 0; i < n; i++){ + + cin >> a[i]; + + } + + + + mergesort(a, 0LL, n - 1); + + cout << swaps % MOD; + + return 0; +} diff --git a/invCount/merge/39786106-137e-11eb-9581-309c234925a0.cpp b/invCount/merge/39786106-137e-11eb-9581-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39fdb8596c092eff87deaf21837a0cfef719b9ed --- /dev/null +++ b/invCount/merge/39786106-137e-11eb-9581-309c234925a0.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + + + + + + + +typedef pair < int, int > pii; + +typedef long long ll; + +typedef long double ld; + +typedef unsigned int ui; + +typedef unsigned long long ull; + + + +int sol = 0; + +const int MOD = 9917; + + + +void merge(int st, int mid, int dr, vector < int > &v) { + + + + int i = st, j = mid + 1; + + vector < int > aux(dr - st + 1); + + + + int cnt = 0, ind = 0; + + while (i <= mid and j <= dr) { + + if (v[i] <= v[j]) { + + aux[ind ++] = v[i ++]; + + } + + else { + + aux[ind ++] = v[j ++]; + + cnt += mid - i + 1; + + } + + } + + + + while (i <= mid) aux[ind ++] = v[i ++]; + + while (j <= dr) aux[ind ++] = v[j ++]; + + + + for (int i = st; i <= dr; ++ i) { + + v[i] = aux[i - st]; + + } + + + + sol += cnt; + + sol %= MOD; + +} + + + +void mergeSort(int st, int dr, vector < int > &v) { + + + + if (st == dr) return; + + + + int mid = (st + dr) >> 1; + + + + mergeSort(st, mid, v); + + mergeSort(mid + 1, dr, v); + + + + merge(st, mid, dr, v); + +} + + + +int main() { + ios::sync_with_stdio(false); + + cin.tie(0);cout.tie(0); + + + + int n; + + cin >> n; + + + + vector < int > v(n); + + for (auto &x : v) cin >> x; + + + + mergeSort(0, n - 1, v); + + cout << sol << '\n'; + +} diff --git a/invCount/merge/3986b8da-137e-11eb-9c33-309c234925a0.cpp b/invCount/merge/3986b8da-137e-11eb-9c33-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d20ab698c29415d2900251c0764bfe2b56ad303 --- /dev/null +++ b/invCount/merge/3986b8da-137e-11eb-9c33-309c234925a0.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + +ifstream r("inv.in"); + +ofstream w("inv.out"); + +int v[500003], sol=0; + +const int mod=9917; + +void unire(int a, int mij, int b){ + + int n1 = mij - a + 1, n2 = b - mij, g[n1], h[n2], nr=0, rasp=0; + + for(int i=0;i>n; + + for(int i=0; i>v[i]; + + } + + mergesort(0, n-1); + + w< +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + + +const ll MOD = 9917; +const int INF = INT_MAX; +const int maxN = 100005; + +ll n, a[maxN], poz[maxN]; + +ll swaps = 0; + + + +void interclasare(ll a[], ll left, ll mid, ll right){ + + ll i = left; + + ll j = mid + 1; + + ll index = left; + + + + while (i <= mid && j <= right){ + + if (a[i] <= a[j]){ + + poz[index] = a[i]; + + index++; i++; + + } + + else{ + + poz[index] = a[j]; + + index++; j++; + + swaps = (swaps + (mid - i + 1)) % MOD; + + } + + } + + + + while(i <= mid){ + + poz[index] = a[i]; + + index++; i++; + + } + + + + while(j <= right){ + + poz[index] = a[j]; + + index++; j++; + + } + + + + for (ll i = left; i <= right; i++) + + a[i] = poz[i]; + +} + + + +void mergesort(ll a[], ll left, ll right){ + + if (left < right){ + + ll mid = left + (right - left) / 2; + + mergesort(a, left, mid); + + mergesort(a, mid + 1, right); + + interclasare(a, left, mid, right); + + } + +} + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + + cin >> n; + + for (ll i = 0; i < n; i++){ + + cin >> a[i]; + + } + + + + mergesort(a, 0LL, n - 1); + + cout << swaps % MOD; + + return 0; +} diff --git a/invCount/merge/39f956c8-137e-11eb-bd2b-309c234925a0.cpp b/invCount/merge/39f956c8-137e-11eb-bd2b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e2a8f75cedde2c3bd73eadd8f5f40fbbe953b7a --- /dev/null +++ b/invCount/merge/39f956c8-137e-11eb-bd2b-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int rasp=0,a[200005],aux[200005],sol=0,mod=9917,n; + +void interclaseaza(int pozst,int mij,int dr) + +{ + + int i=pozst; + + int j=mij+1; + + int nr=0,cnt=0,rasp=0; + + + + while(i<=mij&&j<=dr) + + { + + if(a[i]>a[j]) + + { + + nr++; + + aux[++cnt]=a[j]; + + j++; + + + + } + + else + + { + + rasp+=nr; + + aux[++cnt]=a[i]; + + i++; + + } + + } + + while(i<=mij) + + { + + rasp+=nr; + + aux[++cnt]=a[i]; + + i++; + + } + + while(j<=dr) + + { + + aux[++cnt]=a[j]; + + j++; + + } + + sol=(sol+rasp)%mod; + + for(int i=pozst;i<=dr;i++) + + { + + a[i]=aux[i-pozst+1]; + + } + +} + +void mergesort(int st,int dr) + +{ + + if(st>n; + +for(int i=1;i<=n;i++) + +{ + + f>>a[i]; + +} + +mergesort(1,n); + +g< +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917 ; + +int n ,v[100005] , tmp[100005]; + +long long sol = 0 ; + + + +void MergeSort(int v[] , int st , int dr){ + + if( st < dr ){ + + int m = (st + dr) / 2 ; + + MergeSort( v , m+1 , dr ) ; + + MergeSort( v , st , m ) ; + + + + int i = st , j = m + 1 , k = 0 ; + + long long nr = 0 , rasp = 0 ; + + while( i<=m && j<=dr ) + + if( v[i] > v[j] ){ + + tmp[++k] = v[j++] ; + + nr++ ; + + } + + else{ + + tmp[++k] = v[i++] ; + + rasp+=nr ; + + } + + while(i <= m){ + + rasp+=nr ; + + tmp[++k] = v[i++] ; + + } + + while(j <= dr) + + tmp[++k] = v[j++] ; + + + + sol=(sol+rasp)%MOD ; + + + + for(i = st , j = 1 ; i <= dr ; i ++ , j ++) + + v[i] = tmp[j]; + + } + +} + +int main() + +{ + +f>>n; + +for(int i=1 ; i<=n ; i++){ + + f>>v[i] ; + +} + +MergeSort( v , 1 , n ) ; + + + +g< +#include + + + + + + +int v [ 100000 + 1 ] ; + +int aux[ 100000 + 1 ] ; + + + +long long merge (int v[], int st, int dr ) { + + int m , i, j, k ; + + long long r = 0 ; + + m = (st+dr) / 2 ; + + i = k = st ; + + j = m + 1 ; + + while( i <= m && j <= dr ){ + + if(v[i] <= v[j]){ + + aux[k++] = v[i++] ; + + } + + else{ + + aux[k++] = v[j++] ; + + r += m - i + 1 ; + + } + + } + + while(i <= m){ + + aux[k++] = v[i++] ; + + } + + while(j <= dr) { + + aux[k++] = v[j++] ; + + } + + for(k = st; k <= dr ; k++ ){ + + v[k] = aux[k] ; + + } + + return r ; + +} + + + +long long mergesort(int v[],int st,int dr){ + + if(st == dr) + + return 0 ; + + int m = ( st + dr) / 2 ; + + long long r = 0 ; + + r += mergesort (v,st,m) ; + + r += mergesort (v,m+1,dr) ; + + r += merge (v,st,dr) ; + + return r; + +} + + + +int main() { + + FILE *fin, *fout ; + + fin = fopen("inv.in","r"); + + fout = fopen("inv.out","w"); + + int n, i ; + + fscanf(fin, "%d", &n ) ; + + for(i = 0 ; i < n ; i++ ) + + fscanf(fin, "%d", &v[i] ) ; + + fprintf (fout,"%lld ",mergesort(v, 0, n-1) % 9917 ) ; + + fclose(fin) ; + + fclose(fout) ; + + return 0; + +} diff --git a/invCount/merge/3a2b0f64-137e-11eb-8bf1-309c234925a0.cpp b/invCount/merge/3a2b0f64-137e-11eb-8bf1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..694da7e4aab529a1ab9c3a89ee9afe5c2ea8d159 --- /dev/null +++ b/invCount/merge/3a2b0f64-137e-11eb-8bf1-309c234925a0.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917 ; + +int n ,v[100005] , tmp[100005]; + +long long sol = 0 ; + + + +void MergeSort(int v[] , int st , int dr){ + + if( st < dr ){ + + int m = (st + dr) / 2 ; + + MergeSort( v , m+1 , dr ) ; + + MergeSort( v , st , m ) ; + + + + int i = st , j = m + 1 , k = 0 ; + + long long nr = 0 , rasp = 0 ; + + while( i<=m && j<=dr ) + + if( v[i] > v[j] ){ + + tmp[++k] = v[j++] ; + + nr++ ; + + } + + else{ + + tmp[++k] = v[i++] ; + + rasp+=nr ; + + } + + while(i <= m){ + + rasp+=nr ; + + tmp[++k] = v[i++] ; + + } + + while(j <= dr) + + tmp[++k] = v[j++] ; + + + + sol=(sol+rasp)%MOD ; + + + + for(i = st , j = 1 ; i <= dr ; i ++ , j ++) + + v[i] = tmp[j]; + + } + +} + +int main() + +{ + +f>>n; + +for(int i=1 ; i<=n ; i++){ + + f>>v[i] ; + +} + +MergeSort( v , 1 , n ) ; + + + +g< +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int Mod = 9917; + + + +int inv; + + + +void merge(vector &Arr, int lft, int mid, int rgt) { + + + + int i = lft; + + int j = mid + 1; + + + + vector mergedArr; + + mergedArr.reserve(rgt - lft + 1); + + + + while (i <= mid and j <= rgt) + + if (Arr[i] <= Arr[j]) + + mergedArr.push_back(Arr[i++]); + + else { + + + + inv += mid - i + 1; + + inv %= Mod; + + + + mergedArr.push_back(Arr[j++]); + + } + + + + while (i <= mid) mergedArr.push_back(Arr[i++]); + + while (j <= rgt) mergedArr.push_back(Arr[j++]); + + + + for (int idx = lft, mergedArrIdx = 0; idx <= rgt; ++idx, ++mergedArrIdx) + + Arr[idx] = mergedArr[mergedArrIdx]; + +} + + + +void mergeSort(vector &Arr, int lft, int rgt) { + + + + if (rgt == lft) + + return; + + else { + + + + int mid = (lft + rgt) / 2; + + + + mergeSort(Arr, lft, mid); + + mergeSort(Arr, mid + 1, rgt); + + merge(Arr, lft, mid, rgt); + + } + +} + + + +int main() { + + + + int n; + + fin >> n; + + + + vector Arr(n); + + for (int &itm : Arr) + + fin >> itm; + + + + mergeSort(Arr, 0, n - 1); + + + + fout << inv; + +} diff --git a/invCount/merge/3aec931e-137e-11eb-902d-309c234925a0.cpp b/invCount/merge/3aec931e-137e-11eb-902d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7955e059c67e8ecd08dc7fb5ee51601759dd198d --- /dev/null +++ b/invCount/merge/3aec931e-137e-11eb-902d-309c234925a0.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int MAXN = 1e5 + 4; + +const int MOD = 9917; + +int rasp, v[MAXN], x[MAXN]; + +void interclasare(int st1 , int dr1, int st2, int dr2){ + + int i = st1 , j = st2, indice = st1 - 1; + + while(i <= dr1 and j <= dr2){ + + if(v[i] <= v[j]){ + + x[++indice] = v[i++]; + + } + + else{ + + x[++indice] = v[j++]; + + rasp += (dr1 - i + 1); + + rasp %= MOD; + + } + + } + + for(i; i <= dr1; ++i) x[++indice] = v[i]; + + for(j; j <= dr2; ++j) x[++indice] = v[j]; + + for(int k = st1; k <= dr2; ++k) + + v[k] = x[k], x[k] = 0; + +} + +void dei(int st, int dr){ + + int m = (st + dr ) / 2; + + if(dr - st <= 0) return; + + dei(st, m); + + dei(m + 1, dr); + + interclasare(st, m, m + 1, dr); + +} + +int main(){ + + ios::sync_with_stdio(false); + + fin.tie(0);fout.tie(0); + + int n ; fin >> n; + + for(int i = 1; i <= n; ++i){ + + fin >> v[i]; + + } + + dei(1, n); + + fout << rasp; + + return 0; + +} diff --git a/invCount/merge/3afe4940-137e-11eb-aef8-309c234925a0.cpp b/invCount/merge/3afe4940-137e-11eb-aef8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb446c123e1b0307896fad70ae9ccbda7fb4b86f --- /dev/null +++ b/invCount/merge/3afe4940-137e-11eb-aef8-309c234925a0.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int nmax = 100005; + +const int mod = 9917; + +int n, v[nmax], L[nmax], R[nmax], temp[nmax], ans; + + + +void solve(int st, int dr) + +{ + + if(st < dr) + + { + + int mid = (st + dr) / 2; + + solve(st, mid); + + solve(mid + 1, dr); + + int l1 = 0, l2 = 0; + + for(int i = st; i <= mid; ++i) + + L[++l1] = v[i]; + + for(int i = mid + 1; i <= dr; ++i) + + R[++l2] = v[i]; + + int a = 1, b = 1, l = 0; + + while(a <= l1 && b <= l2) + + { + + if(L[a] > R[b]) + + { + + ans = (ans + l2 - b + 1) % mod; + + temp[++l] = L[a++]; + + } + + else + + temp[++l] = R[b++]; + + } + + while(a <= l1) + + temp[++l] = L[a++]; + + while(b <= l2) + + temp[++l] = R[b++]; + + for(int i = st, j = 1; i <= dr; ++i, ++j) + + v[i] = temp[j]; + + } + +} + +int main() + +{ + + fin >> n; + + for(int i = 1; i <= n; ++i) + + fin >> v[i]; + + solve(1, n); + + fout << ans << "\n"; + + return 0; + +} diff --git a/invCount/merge/3b3cb1cc-137e-11eb-ae34-309c234925a0.cpp b/invCount/merge/3b3cb1cc-137e-11eb-ae34-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cabfe64f49c468ed65eaf65f783e74d3d7e701a --- /dev/null +++ b/invCount/merge/3b3cb1cc-137e-11eb-ae34-309c234925a0.cpp @@ -0,0 +1,107 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n; + +int a[100005],b[100005]; + +int ct; + + + +void inv(int s,int d) + +{ + + int mij=s+(d-s)/2,i,j,k; + + if(s==d)return ; + + if(s!=d) + + { + + inv(s,mij); + + inv(mij+1,d); + + + + i=s;j=mij+1;k=s-1; + + while(i<=mij&&j<=d) + + if(a[i]>a[j]) + + b[++k]=a[j++]; + + + + else + + { + + ct=(ct+j-mij-1)%9917; + + b[++k]=a[i++]; + + } + + while(i<=mij) + + b[++k]=a[i++],ct=(ct+d-mij)%9917; + + + + while(j<=d) + + b[++k]=a[j++]; + + + + for(i=s;i<=d;++i) + + a[i]=b[i]; + + } + + + + + +} + + + +int main() + +{ + + int i; + + fin>>n; + + for(i=1;i<=n;++i) + + fin>>a[i]; + + inv(1,n); + + fout< +#include + + + + + + +using namespace std; + + + +int v[100001], n; + + + +int sol; + + + +void Merge(int mat[], int st, int mij, int dr) + +{ + + int L[100001], D[100001]; + + int i, j, k; + + int n1 = mij - st + 1; + + int n2 = dr - mij; + + + + for(i = 0; i < n1; i++) + + { + + L[i] = mat[st + i]; + + + + + + } + + + + for(i = 0; i < n2; i++) + + D[i] = mat[mij + 1 + i]; + + + + i = j = 0; + + k = st; + + int contor = 0; + + + + while(i < n1 && j < n2) + + { + + if(L[i] > D[j]) + + { + + mat[k] = D[j]; + + + + sol += j - i + n1 - contor; + + j++; + + contor++; + + sol %= 9917; + + } + + else + + { + + mat[k] = L[i]; + + + + i++; + + } + + + + k++; + + } + + + + while(i < n1) + + { + + mat[k] = L[i]; + + + + i++; + + k++; + + } + + + + while(j < n2) + + { + + mat[k] = D[j]; + + + + j++; + + k++; + + } + +} + + + +void MergeSort(int mat[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = st + (dr - st) / 2; + + MergeSort(mat, st, mij); + + MergeSort(mat, mij + 1, dr); + + + + Merge(mat, st, mij, dr); + + } + + + +} + + + +int main() + +{ + + int i, j; + + + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + fin >> n; + + + + for(i = 1; i <= n; i++)fin >> v[i]; + + + + MergeSort(v, 1, n); + + + + fout << sol; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/merge/3b95391e-137e-11eb-b770-309c234925a0.cpp b/invCount/merge/3b95391e-137e-11eb-b770-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8835a5f40a10e312c9272fbbedd35dcce76ffb90 --- /dev/null +++ b/invCount/merge/3b95391e-137e-11eb-b770-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int nmax = 100000, mod = 9917; + + + +int n; + +long long v[nmax + 5], L[nmax + 5], R[nmax + 5], temp[nmax + 5], ans; + + + +void solve(int st, int dr) + +{ + + if (st < dr) + + { + + int mid = (st + dr) / 2; + + solve(st, mid); + + solve(mid + 1, dr); + + int l1 = 0, l2 = 0, l = 0, a = 1, b = 1; + + for (int i = st; i <= mid; ++i) + + L[++l1] = v[i]; + + for (int i = mid + 1; i <= dr; ++i) + + R[++l2] = v[i]; + + while (a <= l1 && b <= l2) + + { + + if (L[a] > R[b]) + + { + + ans = (ans + l2 - b + 1) % mod; + + temp[++l] = L[a++]; + + } + + else + + { + + temp[++l] = R[b++]; + + } + + } + + while (a <= l1) + + temp[++l] = L[a++]; + + while (b <= l2) + + temp[++l] = R[b++]; + + for (int i = st, j = 1; i <= dr; ++i, ++j) + + v[i] = temp[j]; + + } + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i]; + + solve(1, n); + + fout << ans; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/3bb5dddc-137e-11eb-8f7e-309c234925a0.cpp b/invCount/merge/3bb5dddc-137e-11eb-8f7e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d660a3ece29d85cc91e4e882fd52a7dae1591065 --- /dev/null +++ b/invCount/merge/3bb5dddc-137e-11eb-8f7e-309c234925a0.cpp @@ -0,0 +1,113 @@ +#include +#include + + +const int MAXN = 1e5 + 1,MOD = 9917; + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n,v[MAXN],a[MAXN],b[MAXN]; + +long long inv = 0; + + + +void combina(int l,int r){ + + int mid = (l + r) / 2; + + for(int i = l; i <= mid; i++) + + a[i] = v[i]; + + for(int i = mid + 1; i <= r; i++) + + b[i] = v[i]; + + int i = l; + + int j = mid + 1; + + int index = l; + + while(i <= mid && j <= r){ + + if(a[i] <= b[j]){ + + v[index++] = a[i]; + + i++; + + }else{ + + inv += 1ll * (mid - i + 1); + + inv %= MOD; + + v[index++] = b[j]; + + j++; + + } + + } + + while(i <= mid) + + v[index++] = a[i++]; + + while(j <= mid) + + v[index++] = b[j++]; + + + +} + +void divide(int l = 1,int r = n){ + + int mid = (l + r) / 2; + + if(l == r) + + return; + + divide(l,mid); + + divide(mid + 1,r); + + combina(l,r); + +} + + + + + +int main() + +{ + + in>>n; + + for(int i = 1; i <= n; i++) + + in>>v[i]; + + divide(); + + out< + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +int n,a[100005],sol,temp[100005]; +const int MOD=9917; +void interclasare(int st,int dr) +{ + int k=0; + int i=st; + int mid=(st+dr)/2; + int j=mid+1; + while(i<=mid && j<=dr) + { + if(a[i]<=a[j]) + temp[++k]=a[i++]; + else + { + sol+=(mid-i+1); + sol%=MOD; + temp[++k]=a[j++]; + } + } + while(i<=mid) + temp[++k]=a[i++]; + while(j<=dr) + temp[++k]=a[j++]; + for(i=st;i<=dr;i++) + a[i]=temp[i-st+1]; +} +void divide(int st,int dr) +{ + if(st>n; + for(int i=1;i<=n;i++) + f>>a[i]; + divide(1,n); + g< +#include + + + + + + +using namespace std; + + + +int v[100001], n; + + + +int sol; + + + +void Merge(int mat[], int st, int mij, int dr) + +{ + + int L[100001], D[100001]; + + int i, j, k; + + int n1 = mij - st + 1; + + int n2 = dr - mij; + + + + for(i = 0; i < n1; i++) + + { + + L[i] = mat[st + i]; + + + + + + } + + + + for(i = 0; i < n2; i++) + + D[i] = mat[mij + 1 + i]; + + + + i = j = 0; + + k = st; + + int contor = 0; + + + + while(i < n1 && j < n2) + + { + + if(L[i] > D[j]) + + { + + mat[k] = D[j]; + + + + j++; + + sol += j - i + n1 - 1 - contor; + + contor++; + + sol %= 9917; + + } + + else + + { + + mat[k] = L[i]; + + + + i++; + + } + + + + k++; + + } + + + + while(i < n1) + + { + + mat[k] = L[i]; + + + + i++; + + k++; + + } + + + + while(j < n2) + + { + + mat[k] = D[j]; + + + + j++; + + k++; + + } + +} + + + +void MergeSort(int mat[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = st + (dr - st) / 2; + + MergeSort(mat, st, mij); + + MergeSort(mat, mij + 1, dr); + + + + Merge(mat, st, mij, dr); + + + + + + } + + + +} + + + +int main() + +{ + + int i, j; + + + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + fin >> n; + + + + for(i = 1; i <= n; i++)fin >> v[i]; + + + + MergeSort(v, 1, n); + + + + + + + + fout << sol; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/merge/3c140364-137e-11eb-97eb-309c234925a0.cpp b/invCount/merge/3c140364-137e-11eb-97eb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..373030d05cc83a767a4c5769b000c3fe8d251087 --- /dev/null +++ b/invCount/merge/3c140364-137e-11eb-97eb-309c234925a0.cpp @@ -0,0 +1,118 @@ +#include + + + + +using namespace std; + + + +int nr_inversiuni; + + + +void sortare_interclasare(int *v, int *a, int *b, int n) { + + int i = 0, j = 0, mid = n / 2; + + for (int k = 0; k < n; k++) { + + if (i == mid) { + + v[k] = b[j]; + + j++; + + continue; + + } + + if (j == mid + n % 2) { + + v[k] = a[i]; + + i++; + + continue; + + } + + if (a[i] > b[j]) { + + v[k] = b[j]; + + nr_inversiuni = (nr_inversiuni + mid - i) % 9917; + + j++; + + } + + else { + + v[k] = a[i]; + + i++; + + } + + } + +} + + + +void merge_sort(int *v, int n) { + + if (n > 1) { + + int mid = n / 2, k = 0; + + int a[mid + 1], b[mid + 1]; + + for (int i = 0; i < mid; i++) + + a[i] = v[i]; + + for (int i = mid; i < n; i++) + + b[k++] = v[i]; + + merge_sort(a, mid); + + merge_sort(b, mid + n % 2); + + sortare_interclasare(v, a, b, n); + + } + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + int n, numar; + + scanf("%d", &n); + + int numere[n]; + + for (int i = 0; i < n; i++) { + + scanf("%d", &numar); + + numere[i] = numar; + + } + + merge_sort(numere, n); + + printf("%d ", nr_inversiuni); + + return 0; + +} diff --git a/invCount/merge/3c240968-137e-11eb-a505-309c234925a0.cpp b/invCount/merge/3c240968-137e-11eb-a505-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..460d41e68b582d69dd633c58c42578ca41d73824 --- /dev/null +++ b/invCount/merge/3c240968-137e-11eb-a505-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n, a[100100], aux[100100], vf; + + + +int rec(int st, int dr) { + + if (st == dr) + + return 0; + + + + int mid = (st + dr) / 2; + + int cnt = rec(st, mid) + rec(mid + 1, dr); + + cnt %= 9917; + + + + int p1 = st; + + int p2 = mid + 1; + + + + vf = 0; + + for (int i = st; i <= dr; i++) { + + if (p1 > mid) { + + aux[++vf] = a[p2++]; + + if (a[p1] > a[dr]) { + + cnt += dr - mid; + + cnt %= 9917; + + } + + continue; + + } + + if (p2 > dr) { + + aux[++vf] = a[p1++]; + + continue; + + } + + if (a[p1] <= a[p2]) + + aux[++vf] = a[p1++]; + + else { + + aux[++vf] = a[p2++]; + + cnt += mid - p1 + 1; + + cnt %= 9917; + + } + + } + + for (int i = st; i <= dr; i++) + + a[i] = aux[i - st + 1]; + + + + return cnt; + +} + + + +int main() { + + in >> n; + + for (int i = 1; i <= n; i++) + + in >> a[i]; + + + + out << rec(1, n); + + + + return 0; + +} diff --git a/invCount/merge/3c383908-137e-11eb-9e32-309c234925a0.cpp b/invCount/merge/3c383908-137e-11eb-9e32-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1593fbcec247f20720127f55b7557ee89caf744f --- /dev/null +++ b/invCount/merge/3c383908-137e-11eb-9e32-309c234925a0.cpp @@ -0,0 +1,115 @@ +#include +#include +const int MOD = 9917; + + + +using namespace std; + + + +void merge_sort(int *v, int stg, int dr, int &sol) { + + if (stg < dr) { + + int mid = (stg + dr) / 2; + + merge_sort(v, stg, mid, sol); + + merge_sort(v, mid + 1, dr, sol); + + int stg_index = stg; + + int dr_index = mid + 1; + + int *aux = new int[dr - stg + 1]; + + int aux_index = 0; + + while (stg_index <= mid && dr_index <= dr) { + + if (v[stg_index] <= v[dr_index]) { + + aux[aux_index++] = v[stg_index]; + + stg_index++; + + } else { + + sol += mid - stg_index + 1; + + sol %= MOD; + + aux[aux_index++] = v[dr_index]; + + dr_index++; + + } + + } + + while (stg_index <= mid) { + + aux[aux_index++] = v[stg_index]; + + stg_index++; + + } + + while (dr_index <= dr) { + + aux[aux_index++] = v[dr_index]; + + dr_index++; + + } + + for (int i = stg; i <= dr; ++i) { + + v[i] = aux[i - stg]; + + } + + + + + + + + delete[] aux; + + } + +} + + + +int main() { + + int n; + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + fin >> n; + + int *v = new int[n]; + + for (int i = 0; i < n; ++i) { + + fin >> v[i]; + + } + + int sol = 0; + + merge_sort(v, 0, n - 1, sol); + + fout << sol; + + + + return 0; + +} diff --git a/invCount/merge/3c9892ee-137e-11eb-80c5-309c234925a0.cpp b/invCount/merge/3c9892ee-137e-11eb-80c5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6fa9fa468f9d2a7dc83a8402f91576329b54a29a --- /dev/null +++ b/invCount/merge/3c9892ee-137e-11eb-80c5-309c234925a0.cpp @@ -0,0 +1,111 @@ +#include +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, a[100001], b[100001]; + + + +void Read() + +{ + + fin >> n; + + for(int i=1; i<=n; i++) + + fin >> a[i]; + +} + + + +long long Inv(int st, int dr) + +{ + + int mij; + + mij=(st+dr)/2; + + if(st==dr) + + return 0; + + long long inv1, inv2; + + inv1=Inv(st,mij)%9917; + + inv2=Inv(mij+1,dr)%9917; + + int i=st; + + int j=mij+1; + + int k=st-1; + + long long inv3=0; + + while(i<=mij && j<=dr) + + if(a[j] + +using namespace std; +typedef long long ll; +typedef pair< ll , ll > PII; + +ll n, ans; +vector < int > a; + +void divide(int st, int dr){ + if (st >= dr) return ; + + int mid = (st + dr) >> 1; + + divide(st, mid); divide(mid + 1, dr); + + vector < int > c; + int p1 = st, p2 = mid + 1; + + while (p1 <= mid && p2 <= dr){ + if (a[p1] <= a[p2]){ + c.push_back(a[p1]); + ans = (ans + p2 - mid - 1) % 9917; + p1++; + }else{ + c.push_back(a[p2]); + p2++; + } + } + + while (p1 <= mid){ + c.push_back(a[p1++]); + ans = (ans + p2 - mid - 1) % 9917; + } + + while (p2 <= dr) c.push_back(a[p2++]); + + int pos = 0; + for (int i = st; i <= dr; i++){ + a[i] = c[pos++]; + } +} + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + cin >> n; + a.push_back(0); + + for (int i = 1, x; i <= n; i++){ + cin >> x; + a.push_back(x); + } + + divide(1, n); + + cout << ans; + + return 0; +} diff --git a/invCount/merge/3d1abb4a-137e-11eb-a278-309c234925a0.cpp b/invCount/merge/3d1abb4a-137e-11eb-a278-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a08c8e3901c12d3e52e1462cee4bc02d08ff17d --- /dev/null +++ b/invCount/merge/3d1abb4a-137e-11eb-a278-309c234925a0.cpp @@ -0,0 +1,87 @@ +#include +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +const int N = 100001; + +int n, v[N]; + +long long inv; + +void intercls (int v[], int st, int mij, int dr){ + + int i = st, j = mij + 1, k = 0; + + int v2[N]; + + while(i <= mij && j <= dr){ + + if(v[i] <= v[j]) { + + v2[k ++] = v[i ++]; + + } + + else { + + v2[k ++] = v[j ++]; + + inv=(inv + mij-i+1)%9917; + + } + + } + + while(i <= mij) + + v2[k ++] = v[i ++]; + + while(j <= dr) + + v2[k ++] = v[j ++]; + + int c=0; + + for(int i=st; i<=dr; i++) + + v[i]=v2[c++]; + +} + +void mergesort (int v[], int st, int dr){ + + if(st < dr){ + + int mij = (st + dr) / 2; + + mergesort (v, st, mij); + + mergesort (v, mij + 1, dr); + + intercls (v, st, mij, dr); + + } + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; i ++) + + f >> v[i]; + + mergesort (v, 1, n); + + g << inv; + + return 0; + +} diff --git a/invCount/merge/3d2a99dc-137e-11eb-8565-309c234925a0.cpp b/invCount/merge/3d2a99dc-137e-11eb-8565-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4728d1a6c58ce1595ad43640d2da4a44a50397fc --- /dev/null +++ b/invCount/merge/3d2a99dc-137e-11eb-8565-309c234925a0.cpp @@ -0,0 +1,63 @@ +#include +#include +using namespace std; +ifstream fin("inv.in"); ofstream fout("inv.out"); + +int merge(int v[] , int left , int mid , int right) +{ + int i = left , j = mid , k = left , temp[200200]; + int count = 0; + while (i<=mid-1 && j<=right) + { + if(v[i] <= v[j]) + { + temp[k] = v[i]; + i++; + k++; + } + else + { + temp[k] = v[j]; + j++; k++; + count += (mid - i); + } + } + while(i<=mid-1) + { + temp[k] = v[i]; + i++; k++; + } + while(j<=right) + { + temp[k] = v[j]; + j++; k++; + } + for(i=left ; i<=right ; i++) + v[i] = temp[i]; + return count; +} + +int mergesort(int v[], int left, int right) +{ + int mid, count = 0; + + if(right > left) + { + mid = (right + left)/2; + count = mergesort(v , left , mid) % 9917; + count += mergesort(v , mid+1 , right) % 9917; + count += merge(v , left , mid+1 , right) % 9917; + } + return count; +} + +int main() +{ + int n , v[200200]; + fin >> n; + int temp[n]; + for(int i=0 ; i> v[i]; + int ans = mergesort(v , 0 , n-1) % 9917; + fout << ans; +} diff --git a/invCount/merge/3d39b4f6-137e-11eb-9d11-309c234925a0.cpp b/invCount/merge/3d39b4f6-137e-11eb-9d11-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f23707ffe511c8a94a3dc13fd84c59bde9579919 --- /dev/null +++ b/invCount/merge/3d39b4f6-137e-11eb-9d11-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include + + + +using namespace std; + +int rs = 0; +const int MAXN = 100010; +const int_fast64_t MODULO = 9917; +int n; +int arr[MAXN]; + +int_fast64_t merge(int left, int mid, int right, int arr[]) { + queue q1,q2; + for (int i = left; i <= mid; i++) q1.push(arr[i]); + for (int i = mid + 1; i <= right; i++) q2.push(arr[i]); + + int index = left; + int_fast64_t inv = 0; + int i = left; + while (!q1.empty() && !q2.empty()) { + if (q1.front() <= q2.front()) { + arr[index] = q1.front(); + q1.pop(); + + } else { + arr[index] = q2.front(); + q2.pop(); + inv += q1.size(); + } + index++; + } + + while (!q1.empty()) { + arr[index] = q1.front(); + q1.pop(); + index++; + } + + while (!q2.empty()) { + arr[index] = q2.front(); + q2.pop(); + index++; + } + return inv; +} + +int_fast64_t mergesort(int left, int right, int arr[]) { + if (left >= right) return 0; + + int mid = (left + right) / 2; + + int_fast64_t inv = mergesort(left, mid, arr); + inv += mergesort(mid + 1, right, arr); + + inv += merge(left, mid, right, arr); + + return inv; +} + +int main() { + + ifstream fin("inv.in"); + ofstream fout("inv.out"); + + ios_base::sync_with_stdio(0); + cin.tie(NULL); + + fin >> n; + for (int i = 0; i < n; i++) fin >> arr[i]; + + fout << mergesort(0, n - 1, arr) % MODULO; + + + return 0; +} diff --git a/invCount/merge/3d43a2fe-5033-4700-a9c2-41c099352dd8.cpp b/invCount/merge/3d43a2fe-5033-4700-a9c2-41c099352dd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c8ed7943d1e18f629a9de14076ccfa642198358 --- /dev/null +++ b/invCount/merge/3d43a2fe-5033-4700-a9c2-41c099352dd8.cpp @@ -0,0 +1,143 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + +long a[100001]; + +int n,nrinv; + +long rez; + + + +void Merge(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + Merge(a,st,mij); + + Merge(a,mij+1,dr); + + + + int k=0; + + int i=st; + + int j=mij+1; + + int b[100001]; + + int nrinv=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + else + + { + + b[k]=a[j]; + + k++; + + j++; + + nrinv++; + + } + + + + while(i<=mij) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + while(j<=dr) + + { + + b[k]=a[j]; + + k++; + + j++; + + } + + for(i=st; i<=dr; i++) + + { + + a[i]=b[i-st]; + + } + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>a[i]; + + } + + Merge(a,0,n-1); + + g< + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +long long interclasare(long long v[], long long a, long long m, long long b) + +{ + + long long i = a, j = m + 1, k = 0, r[100000], t, nr = 0; + + while(i <= m && j <= b) + + { + + if(v[i] <= v[j]) + + r[++k] = v[i++]; + + else if(v[j] < v[i]) + + { + + r[++k] = v[j++]; + + nr += m - i + 1; + + } + + } + + for(t = i; t <= m; t++) + + r[++k] = v[i++]; + + for(t = j; t <= b; t++) + + r[++k] = v[j++]; + + k = 1; + + for(t = a; t <= b; t++) + + v[t] = r[k++]; + + return nr; + +} + + + +long long divide_et_impera(long long v[], long long st, long long dr) + +{ + + long long m = (dr + st) / 2, nr = 0; + + if(st < dr) + + { + + nr += divide_et_impera(v, st, m); + + nr += divide_et_impera(v, m + 1, dr); + + nr += interclasare(v, st, m, dr); + + } + + return nr; + +} + + + +int main() + +{ + + long long n, i; + + f >> n; + + long long v[100000]; + + for(i = 1; i <= n; i++) + + f >> v[i]; + + g << divide_et_impera(v, 1, n) % 9917; + + return 0; + +} diff --git a/invCount/merge/3e699fd4-137e-11eb-81ec-309c234925a0.cpp b/invCount/merge/3e699fd4-137e-11eb-81ec-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37b1622f65e08fe44371445028ed0ba7332d0878 --- /dev/null +++ b/invCount/merge/3e699fd4-137e-11eb-81ec-309c234925a0.cpp @@ -0,0 +1,51 @@ +#include +#include + + + + +using namespace std; + +ifstream f("inv.in"); +ofstream g("inv.out"); + +int n,sir[100000 +5],aux[100000 +5],rez; + +void mergesort(int l,int r){ + if(l>=r){ + return; + } + int i,j,m=(l+r)/2,k=l; + mergesort(l,m); + mergesort(m+1,r); + i=l; + j=m+1; + while(i<=m&&j<=r){ + if(sir[i]<=sir[j]){ + aux[k++]=sir[i++]; + }else{ + rez=(rez+m-i+1+9917)%9917; + aux[k++]=sir[j++]; + } + } + while(i<=m){ + aux[k++]=sir[i++]; + } + while(j<=r){ + aux[k++]=sir[j++]; + } + for(int i=l;i<=r;i++){ + sir[i]=aux[i]; + } +} + +int main(){ + f>>n; + for(int i=1;i<=n;i++){ + f>>sir[i]; + } + mergesort(1,n); + g< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917; + +int N, S[100001], T[100001], nrInv; + + + +void Interclasare(int p, int u, int m) + +{ + + int i = p, j = m + 1, k = 0; + + while(i <= m && j <= u) + + if(S[i] <= S[j]) + + T[++k] = S[i++]; + + else + + { + + T[++k] = S[j++]; + + nrInv = (nrInv + m - i + 1) % MOD; + + } + + while(i <= m) + + T[++k] = S[i++]; + + while(j <= u) + + T[++k] = S[j++]; + + for(int i = p, k = 1; i <= u; i++, k++) + + S[i] = T[k]; + +} + + + +void Mergesort(int p, int u) + +{ + + if(p < u) + + { + + int m = p + (u - p) / 2; + + Mergesort(p, m); + + Mergesort(m + 1, u); + + Interclasare(p, u, m); + + } + +} + + + +int main() + +{ + + f>>N; + + for(int i=1;i<=N;i++) + + f>>S[i]; + + Mergesort(1,N); + + g< +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +const int MOD = 9917; + +int N, S[100001], T[100001], nrinv; + +void interclasare(int p, int u, int m) + +{ + + int i = p, j = m + 1, k = 0; + + while(i <= m && j <= u) + + if(S[i] <= S[j]) + + T[++k] = S[i++]; + + else + + { + + T[++k] = S[j++]; + + nrinv = (nrinv + m - i + 1) % MOD; + + } + + while(i <= m) + + T[++k] = S[i++]; + + while(j <= u) + + T[++k] = S[j++]; + + for(i = p, k = 1; i <= u; i++, k++) + + S[i] = T[k]; + +} + +void MergeSort(int p, int u) + +{ + + if(p < u) + + { + + int m = p + (u - p) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, u); + + interclasare(p, u, m); + + } + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> S[i]; + + MergeSort(1,N); + + g << nrinv; + + return 0; + +} diff --git a/invCount/merge/3fb1da40-137e-11eb-baea-309c234925a0.cpp b/invCount/merge/3fb1da40-137e-11eb-baea-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a104805b5eb2d3077f3ee437426d34e6012679d6 --- /dev/null +++ b/invCount/merge/3fb1da40-137e-11eb-baea-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int kMax = 100005; + +const int kMod = 9917; + +int v[kMax], rez[kMax]; + + + +int merge_halves(int a[], int b[], int left, int right, int m) { + + int size_a = m - left + 1; + + int size_b = right - m; + int idxa = 0, idxb = 0, nrInv = 0, idxRez = 0; + + + + for(idxa = 0; idxa < size_a && idxb < size_b; idxRez++) { + + if(a[idxa] > b[idxb]) { + + rez[idxRez] = b[idxb++]; + + nrInv += size_a - idxa; + + } else { + + rez[idxRez] = a[idxa++]; + + } + + } + + + + for(; idxa < size_a; idxRez++, idxa++) { + + rez[idxRez] = a[idxa]; + + } + + + + for(; idxb < size_b; idxRez++, idxb++) { + + rez[idxRez] = b[idxb]; + + } + for(int i = 0; i < size_b + size_a; i++) { + + a[i] = rez[i]; + + } + + + + return nrInv; + +} + + + +int merge_sort(int left, int right) { + + if(left == right) + + return 0; + + + + int middle = (left + right) / 2; + + int inv_left = merge_sort(left, middle); + + int inv_right = merge_sort(middle + 1, right); + + + + int crt_inv = merge_halves(v + left, v + middle + 1, left, right, middle); + + return (crt_inv + inv_right + inv_left) % kMod; + +} + + + +int main() { + + int n; + + f >> n; + + for(int i = 0; i < n; i++) { + + f >> v[i]; + + } + + + + g << merge_sort(0, n - 1); + + return 0; + +} diff --git a/invCount/merge/3ff6987a-137e-11eb-8d9a-309c234925a0.cpp b/invCount/merge/3ff6987a-137e-11eb-8d9a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a104805b5eb2d3077f3ee437426d34e6012679d6 --- /dev/null +++ b/invCount/merge/3ff6987a-137e-11eb-8d9a-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int kMax = 100005; + +const int kMod = 9917; + +int v[kMax], rez[kMax]; + + + +int merge_halves(int a[], int b[], int left, int right, int m) { + + int size_a = m - left + 1; + + int size_b = right - m; + int idxa = 0, idxb = 0, nrInv = 0, idxRez = 0; + + + + for(idxa = 0; idxa < size_a && idxb < size_b; idxRez++) { + + if(a[idxa] > b[idxb]) { + + rez[idxRez] = b[idxb++]; + + nrInv += size_a - idxa; + + } else { + + rez[idxRez] = a[idxa++]; + + } + + } + + + + for(; idxa < size_a; idxRez++, idxa++) { + + rez[idxRez] = a[idxa]; + + } + + + + for(; idxb < size_b; idxRez++, idxb++) { + + rez[idxRez] = b[idxb]; + + } + for(int i = 0; i < size_b + size_a; i++) { + + a[i] = rez[i]; + + } + + + + return nrInv; + +} + + + +int merge_sort(int left, int right) { + + if(left == right) + + return 0; + + + + int middle = (left + right) / 2; + + int inv_left = merge_sort(left, middle); + + int inv_right = merge_sort(middle + 1, right); + + + + int crt_inv = merge_halves(v + left, v + middle + 1, left, right, middle); + + return (crt_inv + inv_right + inv_left) % kMod; + +} + + + +int main() { + + int n; + + f >> n; + + for(int i = 0; i < n; i++) { + + f >> v[i]; + + } + + + + g << merge_sort(0, n - 1); + + return 0; + +} diff --git a/invCount/merge/40f156be-137e-11eb-8074-309c234925a0.cpp b/invCount/merge/40f156be-137e-11eb-8074-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6338d97960e7ff6ebbd6a5c0c571ac4692c6204 --- /dev/null +++ b/invCount/merge/40f156be-137e-11eb-8074-309c234925a0.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int n,k,i,st,dr,mid,pos,v[100001],in[100001]; + +long long S; + +void cnt(int a,int m,int b){ + + int c=0,i=a,j=m+1; + + while(i<=m&&j<=b){ + + if(v[i]<=v[j]){ + + in[++c]=v[i]; + + i++; + + } + + else if(v[j]0) + + S=(S+j-(c+a-1))%9917; + + j++; + + } + + } + + while(i<=m){ + + in[++c]=v[i]; + + i++; + + } + + while(j<=b){ + + in[++c]=v[j]; + + j++; + + } + + for(i=a;i<=b;i++) + + v[i]=in[i-a+1]; + +} + +void divi(int a,int b){ + + if(b-a>0){ + + int m=(a+b)/2; + + divi(a,m); + + divi(m+1,b); + + cnt(a,m,b); + + } + +} + +int main() + +{ f>>n; + + for(i=1;i<=n;i++) + + f>>v[i]; + + divi(1,n); + + g< +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + + + +int v[100000 +2]; + + + +int cnt = 0; + + + +void Merge(int st, int mij, int dr) + +{ + + int aux[100000 +2]; + + int i = st, j = mij+1, k = 0; + + + + while(i <= mij && j <= dr) + + { + + if(v[i] <= v[j]) + + { + + aux[k++] = v[i++]; + + } + + else + + { + + aux[k++] = v[j++]; + + cnt += (mij - i + 1); + + cnt%=9917; + + } + + } + + + + while(i <= mij) + + { + + aux[k++] = v[i++]; + + } + + + + while(j <= dr) + + { + + aux[k++] = v[j++]; + + } + + + + for(i=st; i<=dr; i++) + + { + + v[i] = aux[i-st]; + + } + +} + + + + + +void MergeSort(int st, int dr) + +{ + + if(st == dr) + + { + + return; + + } + + int mij = (st+dr)/2; + + MergeSort(st, mij); + + MergeSort(mij+1, dr); + + Merge(st, mij, dr); + + + +} + + + +int main() + +{ + + int n; + + fin>>n; + + for(int i=1; i<=n; i++) + + { + + fin>>v[i]; + + } + + + + MergeSort(1, n); + + fout< + +using namespace std; + +ifstream in ("inv.in"); +ofstream out ("inv.out"); + +const int N=100005, M=9917; + +int v[N], aux[N]; + +int mergeV (int v[], int st, int dr) +{ + int m=(st+dr)/2, i, j, k, r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr) + { + if(v[i]<=v[j]) + aux[k++]=v[i++]; + else + { + aux[k++]=v[j++]; + r+=m-i+1; + r%=M; + } + } + while(i<=m) + { + aux[k++]=v[i++]; + } + while(j<=dr) + aux[k++]=v[j++]; + for(k=st; k<=dr; k++) + v[k]=aux[k]; + return r; +} + +int ms(int v[], int st, int dr) +{ + int r=0; + if(st==dr) + { + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r%=M; + r+=ms(v, m+1, dr); + r%=M; + r+=mergeV(v, st, dr); + r%=M; + return r; +} + +int main() +{ + int n, i; + in>>n; + for(i=0; i>v[i]; + } + out< +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int N = 100000; + +int v[N], aux[N]; + +long long inv=0; + + + +void MergeSort(int v[], int st, int dr); + +void Merge(int v[], int st, int dr); + + + +int main() + +{ + + int n; + + in >> n; + + for(int i=0; i> v[i]; + + + + MergeSort(v, 0, n-1); + + + + out << inv%9917; + + return 0; + +} + + + +void MergeSort(int v[], int st, int dr) + +{ + + if(st==dr) return; + + int m=(st+dr)/2; + + MergeSort(v, st, m); + + MergeSort(v, m+1, dr); + + Merge(v, st, dr); + +} + + + +void Merge(int v[], int st, int dr) + +{ + + int m=(st+dr)/2; + + int i=st, k=st, j=m+1; + + while(i<=m && j<=dr) + + { + + if(v[j] +#include + + +using namespace std; + + + +const int maxN = 100001, + + MOD = 9917; + + + +int a[maxN], b[maxN]; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int interClas(int p, int q, int m) { + + int i = p, + + j = m+1, + + k = 0, + + inv = 0; + + while(i <= m && j <= q) + + if(a[i] <= a[j]) + + b[++k] = a[i++]; + + else { + + b[++k] = a[j++]; + + inv = (inv+m-i+1)%MOD; + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= q) + + b[++k] = a[j++]; + + k = 1; + + for(int i = p; i <= q; i++) + + a[i] = b[k++]; + + return inv; + +} + + + +int mergeSort(int p, int q) { + + int inv = 0; + + if(p < q) { + + int m = (p+q)/2; + + inv = mergeSort(p, m)%MOD; + + inv = (inv+mergeSort(m+1, q))%MOD; + + inv = (inv+interClas(p, q, m))%MOD; + + } + + return inv%MOD; + +} + + + +int main() + +{ + + int n; + + in >> n; + + for(int i = 1; i <= n; i++) + + in >> a[i]; + + + + out << mergeSort(1, n); + + return 0; + +} diff --git a/invCount/merge/4298524c-137e-11eb-bf11-309c234925a0.cpp b/invCount/merge/4298524c-137e-11eb-bf11-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d134d95bf222aa331c587e0d6e7dd7f0adfeec --- /dev/null +++ b/invCount/merge/4298524c-137e-11eb-bf11-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; + +const int N=100001; +const int M=9917; +int aux[N], v[N]; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int merge_v(int v[], int st, int dr){ + int m=(st+dr)/2; + int i, j, k, r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr){ + if(v[i]<=v[j]){ + aux[k++]=v[i++]; + } else { + aux[k++]=v[j++]; + r+=m-i+1; + r%=M; + } + } + while(i<=m){ + aux[k++]=v[i++]; + } + while(j<=dr){ + aux[k++]=v[j++]; + } + for(k=st; k<=dr; k++){ + v[k]=aux[k]; + } + return r; +} + +int ms(int v[], int st, int dr){ + int r=0; + if(st==dr){ + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r%=M; + r+=ms(v, m+1, dr); + r%=M; + r+=merge_v(v, st, dr); + r%=M; + return r; +} + +int main(){ + int n; + in>>n; + for(int i=1; i<=n; i++){ + in>>v[i]; + } + out< + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100000]; + +long long merge_v(int v[], int st, int dr){ + int m=(st+dr)/2, i, j, k; + long long r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr){ + if(v[i]<=v[j]){ + aux[k++]=v[i++]; + }else{ + aux[k++]=v[j++]; + r+=m-i+1; + } + } + while(i<=m){ aux[k++]=v[i++]; } + while(j<=dr){ aux[k++]=v[j++]; } + for(k=st; k<=dr; k++){ + v[k]=aux[k]; + } + return r; +} + +long long ms(int v[], int st, int dr){ + long long r=0; + if(st==dr){ + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r+=ms(v, m+1, dr); + r+=merge_v(v, st, dr); + return r; +} + +int main() +{ + int v[100000], n; + in>>n; + for(int i=0; i>v[i]; + } + out< + + +using namespace std; + + + +int n; + +int x[100005]; + +int s[100007]; + +int d[100006]; + + + +int merge_bine(int st,int mid,int dr) + +{ + + int i,j; + + long long sol=0; + + int nrs=mid-st+1; + + int nrd=dr-mid; + + for(i=1;i<=nrs;i++) s[i]=x[st+i-1]; + + for(i=1;i<=nrd;i++) d[i]=x[mid+i]; + + s[nrs+1]=0x7fffffff; + + d[nrd+1]=0x7fffffff; + + i=1; j=1; + + for(int k=st;k<=dr;k++) + + { + + if(s[i]<=d[j]) x[k]=s[i++]; + + else + + { + + x[k]=d[j++]; + + if(i!=nrs+1) + + { + + sol+=nrs+1-i; + + if(sol>9917) sol-=9917; + + } + + } + + } + + return sol; + +} + + + +int inversions(int st,int dr) + +{ + + if(st==dr) return 0; + + int mid=st+(dr-st)/2; + + long long sol=0; + + sol+=inversions(st,mid); + + sol+=inversions(mid+1,dr); + + sol+=merge_bine(st,mid,dr); + + sol%=9917; + + return sol; + +} + + + +int main() + +{ + + ifstream t1("inv.in"); + + ofstream t2("inv.out"); + + t1>>n; + + for(int i=1;i<=n;i++) t1>>x[i]; + + t2< + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int v[100001],aux[100001]; + +int mmerge(int st,int dr,int v[]) +{ + int i,j,k,m=(st+dr)/2,c=0; + i=k=st; + j=m+1; + while(i<=m&&j<=dr) + { + if(v[i]<=v[j]) + aux[k++]=v[i++]; + else + aux[k++]=v[j++],c+=m-i+1,c%=9917; + } + while(i<=m) + aux[k++]=v[i++]; + while(j<=dr) + aux[k++]=v[j++]; + for(k=st; k<=dr; k++) + v[k]=aux[k]; + return c; +} + +int msort(int st,int dr,int v[]) +{ + int c = 0; + if(st==dr) + return 0; + int m=(st+dr)/2; + c += msort(st,m,v); + c += msort(m+1,dr,v); + c += mmerge(st,dr,v); + c %= 9917; + return c; +} + +int main() +{ + int n,c=0; + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + + out< +#include + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100005],v[100005],n,cnt = 0; + +int mmerge(int st,int dr,int v[]) +{ + int i,j,k,m = (st+dr)/2,nr = 0; + i = k = st; + j = m+1; + while (i <= m && j <= dr) + { + if (v[i] <= v[j]) + { + aux[k++] = v[i++]; + } + else + { + aux[k++] = v[j++]; + nr += m-i+1; + nr %= 9917; + } + } + while (i <= m) + { + aux[k++] = v[i++]; + } + while (j <= dr) + { + aux[k++] = v[j++]; + } + for (k=st; k<=dr; k++) + { + v[k] = aux[k]; + } + return nr; +} + +int msort(int st,int dr,int v[]) +{ + int cnt = 0; + if (st == dr) + { + return 0; + } + int m = (st+dr)/2; + cnt += msort(st,m,v); + cnt %= 9917; + cnt += msort(m+1,dr,v); + cnt %= 9917; + cnt += mmerge(st,dr,v); + cnt %= 9917; + return cnt; +} + +int main() +{ + int i; + in >> n; + for (i=1; i<=n; i++) + { + in >> v[i]; + } + cnt = msort(1,n,v); + out << cnt; + return 0; +} diff --git a/invCount/merge/43292492-137e-11eb-88fe-309c234925a0.cpp b/invCount/merge/43292492-137e-11eb-88fe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98a7c80f37398d5ed6a61a476098d8d0b1a7ead1 --- /dev/null +++ b/invCount/merge/43292492-137e-11eb-88fe-309c234925a0.cpp @@ -0,0 +1,47 @@ +#include +#include +using std::cin; +using std::cout; + + + std::ifstream fin("inv.in"); + std::ofstream fout("inv.out"); + + + + +int s[100000], aux[100000]; + +int merge (int st, int dr, int v[]) { + int i, j, k, m = (st + dr) >> 1, nr; + i = k = st; + j = m + 1; + nr = 0; + while (i <= m && j <= dr) + if (v[i] <= v[j]) aux[k++] = v[i++]; + else { + aux[k++] = v[j++]; + nr += m - i + 1; + } + + while (i <= m) aux[k++] = v[i++]; + while (j <= dr) aux[k++] = v[j++]; + + for (k = st ; k <= dr ; k++) v[k] = aux[k]; + + return nr; +} + +int msort (int st, int dr, int v[]) { + if (st == dr) return 0; + int m = (st + dr) >> 1; + return (msort(st, m, v) + msort(m + 1, dr, v) + merge(st, dr, v)) % 9917; +} + +int main (int argc, const char * argv[]) { + int n; + fin >> n; + for (auto i = 0 ; i < n ; i++) fin >> s[i]; + fout << msort (0, n - 1, s); + return 0; +} diff --git a/invCount/merge/4345da2e-137e-11eb-a3e2-309c234925a0.cpp b/invCount/merge/4345da2e-137e-11eb-a3e2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1231bbf46a8ac8081cc5b25ff869463324f13d7c --- /dev/null +++ b/invCount/merge/4345da2e-137e-11eb-a3e2-309c234925a0.cpp @@ -0,0 +1,101 @@ +#include +#include + + +const int MAXN = 1e5 + 5,MOD = 9917; + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n,v[MAXN],aux[MAXN],ans = 0; + +pair nume[MAXN]; + + + +void combina(int left,int mid,int right){ + + int i = left,j = mid + 1,index = left - 1; + + while(i <= mid and j <= right){ + + while(v[i] > v[j] and j <= right){ + + aux[++index] = v[j]; + + j++; + + ans += mid - i + 1; + + } + + while(v[j] >= v[i] and i <= mid){ + + aux[++index] = v[i]; + + i++; + + } + + } + + while(i <= mid) + + aux[++index] = v[i++]; + + while(j <= right) + + aux[++index] = v[j++]; + + for(int i = left; i <= right; i++) + + v[i] = aux[i]; + + ans %= MOD; + +} + +void divide(int left = 1,int right = n){ + + if(left == right) + + return; + + int mid = (left + right) / 2; + + divide(left,mid); + + divide(mid + 1,right); + + combina(left,mid,right); + +} + + + +int main() + +{ + + in>>n; + + for(int i = 1; i <= n; i++) + + in>>v[i]; + + divide(); + + out< + + + +using namespace std; + +int aux[100005], v[100005]; + +int mmerge( int st, int dr, int v[] ) { + int i,j,k,m; + m = ( st + dr ) / 2; + i = k = st; + j = m + 1; + int nr = 0; + while( i <= m && j <= dr ) { + if( v[i] <= v[j] ) + aux[k++] = v[i++]; + else + aux[k++] = v[j++], nr += ( m - i + 1 ) % 9917; + } + while( i <= m ) + aux[k++] = v[i++]; + while( j <= dr ) + aux[k++] = v[j++]; + for( k = st; k <= dr; k ++ ) + v[k] = aux[k]; + return nr; +} + +int msort( int st, int dr, int v[] ) { + int i, j; + if( st == dr ) + return 0; + int m = ( st + dr ) / 2; + i = msort(st,m,v); + j = msort(m+1,dr,v); + return (i + j + mmerge(st,dr,v)) % 9917; +} + +int main() { + freopen("inv.in","r",stdin); + freopen("inv.out","w",stdout); + int i, n, nr; + scanf("%d",&n); + for( i = 1; i <= n; i ++ ) + scanf("%d",&v[i]); + nr = msort(1,n,v) % 9917; + printf("%d",nr); + return 0; +} diff --git a/invCount/merge/437548c0-137e-11eb-bde8-309c234925a0.cpp b/invCount/merge/437548c0-137e-11eb-bde8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61452b5f3a61a48deb15e95077039bb6a7fe9760 --- /dev/null +++ b/invCount/merge/437548c0-137e-11eb-bde8-309c234925a0.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + +int aux[100005], v[100005]; + +int mmerge(int st, int dr) +{ + int i, j, k, m = (st+dr) / 2; + long long nr = 0; + + i = k = st; j = m+1; + + while(i <= m && j <= dr) + { + if(v[i] <= v[j]) + aux[k++] = v[i++]; + else + { + aux[k++] = v[j++]; + nr += m - i + 1; + } + } + + while(i <= m) aux[k++] = v[i++]; + while(j <= dr) aux[k++] = v[j++]; + + for(k = st; k <= dr; ++k) v[k] = aux[k]; + + return nr % 9917; +} + +int msort(int st, int dr) +{ + if(st == dr) return 0; + int m = (st+dr) / 2; + + int a = msort(st, m); + int b = msort(m+1, dr); + return (a + b + mmerge(st, dr))%9917; +} + +int main() +{ + ifstream fin("inv.in"); + ofstream fout("inv.out"); + + int n, i; + fin >> n; + for(i = 0; i < n; ++i) fin >> v[i]; + + fout << msort(0, n-1); +} diff --git a/invCount/merge/43a8e04a-137e-11eb-b03b-309c234925a0.cpp b/invCount/merge/43a8e04a-137e-11eb-b03b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91041baa5e99a8ec5e0104a71cb488378c4d0014 --- /dev/null +++ b/invCount/merge/43a8e04a-137e-11eb-b03b-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n ,cnt; + +int a[100005]; + +int b[100005]; + +void intercl(int st,int m,int dr) + +{ + + int i , j , k ; + + i = st; + + j = m + 1; + + k = 0 ; + + while(i <= m && j <= dr) + + if(a[i] <= a[j]) + + b[++k] = a[i++]; + + else { + + cnt = (cnt + (m - i + 1)) % 9917; + + b[++k] = a[j++]; + + } + + + + + + while( i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for( i = st; i <= dr; i ++) + + a[i] = b[j++]; + +} + +void MSort(int st , int dr) + +{ + + int m ; + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + {swap(a[dr],a[st]); + + cnt++; + + cnt %= 9917; + + } + + } + + else + + { + + m = (st + dr) / 2; + + MSort(st,m); + + MSort(m + 1,dr); + + intercl(st,m,dr); + + } + + + + + +} + +int main() + +{ + + int i ; + + fin>>n; + + for(i = 1; i <= n ; i ++) + + fin>> a[i]; + + MSort(1,n); + + + + fout< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, a[100005], b[100005]; + +long long cnt; + + + +void Intercl(int st, int m, int dr) + +{ + + int i, j, k = 0; + + i = st; + + j = m + 1; + + while(i <= m && j <= dr) + + { + + if(a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + cnt = cnt + (m - i + 1); + + cnt %= 9917; + + b[++k] = a[j++]; + + } + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for(i = st; i <= dr; i++) + + a[i] = b[j++]; + +} + + + +void MSort(int st, int dr) + +{ + + int m; + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + { + + swap(a[st], a[dr]); + + cnt++; + + } + + } + + else + + { + + m = (st + dr) / 2; + + MSort(st, m); + + MSort(m + 1, dr); + + Intercl(st, m, dr); + + } + +} + + + +int main() + +{ + + cnt = 0; + + fin >> n; + + for(int i = 1; i <= n; i++) + + fin >> a[i]; + + MSort(1, n); + + cnt %= 9917; + + fout << cnt << "\n"; + + return 0; + +} diff --git a/invCount/merge/43c99b1e-137e-11eb-bbd7-309c234925a0.cpp b/invCount/merge/43c99b1e-137e-11eb-bbd7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aea747e81a1f0f033112bad72e85b14053f4355 --- /dev/null +++ b/invCount/merge/43c99b1e-137e-11eb-bbd7-309c234925a0.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +using namespace std; + +const int MOD = 9917; + +int a[100005], b[100005], sol; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +void Merge (int left, int right, int m) + +{ + + int i, j, k = 0; + + i = left; + + j = m + 1; + + while (i <= m && j <= right) + + if (a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + sol = (sol + (m - i + 1)) % MOD; + + b[++k] = a[j++]; + + } + + while (i <= m) + + b[++k] = a[i++]; + + while (j <= right) + + b[++k] = a[j++]; + + j = 1; + + for (i = left; i <= right; i++) + + a[i] = b[j++]; + +} + + + +void MergeSort (int left, int right) + +{ + + if (right - left <= 1) + + { + + if (a[right] < a[left]) + + { + + swap (a[left], a[right]); + + sol++; + + sol %= MOD; + + } + + } + + else + + { + + int m = (left + right) / 2; + + MergeSort(left, m); + + MergeSort(m + 1, right); + + Merge(left, right, m); + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + + MergeSort(1, n); + + fout << sol << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/43d7f302-137e-11eb-8f24-309c234925a0.cpp b/invCount/merge/43d7f302-137e-11eb-8f24-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62c07d7a9fbf8c151623ff8fbc6f3859ddc5cd36 --- /dev/null +++ b/invCount/merge/43d7f302-137e-11eb-8f24-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int a[100002]; + +int b[100002]; + +int n,ct; + +void interclasare(int st,int m, int dr) + +{ + + int i=st,j=m+1,k=0; + + while(i<=m && j<=dr) + + if(a[i]<=a[j]) b[++k]=a[i++]; + + else + + { + + b[++k]=a[j++]; + + ct+=(m-i+1); + + ct=ct%9917; + + } + + while(i<=m) + + b[++k]=a[i++]; + + while(j<=dr) + + b[++k]=a[j++]; + + j=1; + + for(i=st;i<=dr;i++) + + a[i]=b[j++]; + +} + +void MergeSort(int st, int dr) + +{ + + int m; + + if(dr-st<=1) + + { + + if(a[st]>a[dr]) + + { + + swap(a[dr],a[st]); + + ct++; + + } + + } + + else + + { + + m=(st+dr)/2; + + MergeSort(st,m); + + MergeSort(m+1,dr); + + interclasare(st,m,dr); + + } + +} + +int main() + +{ + + fin>>n; + + for(int i=1; i<=n; i++) + + fin>>a[i]; + + MergeSort(1,n); + + fout< + + +using namespace std; + + + +int a[100005], b[100005], n, cnt; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int Int(int st, int m, int dr) + +{ + + int i, j, k; + + i = st; j = m + 1; k = 0; + + while(i <= m && j <= dr) + + if(a[i] <= a[j])b[++k] = a[i++]; + + else + + { + + cnt += (m - i + 1); + + cnt = cnt % 9917; + + b[++k] = a[j++]; + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for(i = st; i <= dr; i++) + + a[i] = b[j++]; + + + +} + + + +void MSort(int st, int dr) + +{ + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + { + + cnt++; + + if(cnt == 9917)cnt = 0; + + swap(a[st], a[dr]); + + } + + } + + else + + { + + int m = (st + dr) / 2; + + MSort(st, m); + + MSort(m + 1, dr); + + Int(st, m, dr); + + } + +} + + + +int main() + +{ + + int i; + + fin >> n; + + for(i = 1; i <= n; i++) + + fin >> a[i]; + + MSort(1, n); + + fout << cnt; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/43f739f0-137e-11eb-b6bd-309c234925a0.cpp b/invCount/merge/43f739f0-137e-11eb-b6bd-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aea747e81a1f0f033112bad72e85b14053f4355 --- /dev/null +++ b/invCount/merge/43f739f0-137e-11eb-b6bd-309c234925a0.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +using namespace std; + +const int MOD = 9917; + +int a[100005], b[100005], sol; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +void Merge (int left, int right, int m) + +{ + + int i, j, k = 0; + + i = left; + + j = m + 1; + + while (i <= m && j <= right) + + if (a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + sol = (sol + (m - i + 1)) % MOD; + + b[++k] = a[j++]; + + } + + while (i <= m) + + b[++k] = a[i++]; + + while (j <= right) + + b[++k] = a[j++]; + + j = 1; + + for (i = left; i <= right; i++) + + a[i] = b[j++]; + +} + + + +void MergeSort (int left, int right) + +{ + + if (right - left <= 1) + + { + + if (a[right] < a[left]) + + { + + swap (a[left], a[right]); + + sol++; + + sol %= MOD; + + } + + } + + else + + { + + int m = (left + right) / 2; + + MergeSort(left, m); + + MergeSort(m + 1, right); + + Merge(left, right, m); + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + + MergeSort(1, n); + + fout << sol << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/442fa108-137e-11eb-8e86-309c234925a0.cpp b/invCount/merge/442fa108-137e-11eb-8e86-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7882ececc7440e662cd24f69ad81082c42c5229 --- /dev/null +++ b/invCount/merge/442fa108-137e-11eb-8e86-309c234925a0.cpp @@ -0,0 +1,55 @@ +#include + + + +using namespace std; + +int n, v[100000], aux[100000]; + +int countInv(int left, int right) { + if (left >= right) { + return 0; + } + + int mid = (left + right) / 2; + int currCount = (countInv(left, mid) + countInv(mid + 1, right)) % 9917; + int i, j, k; + + for (k = left, i = left, j = mid + 1; i <= mid && j <= right; ++k) { + if (v[i] > v[j]) { + currCount = (currCount + mid - i + 1) % 9917; + aux[k] = v[j++]; + } else { + aux[k] = v[i++]; + } + } + + for (; i <= mid; ++i, ++k) { + aux[k] = v[i]; + } + for (; j <= right; ++j, ++k) { + aux[k] = v[j]; + } + + for (i = left; i < k; ++i) { + v[i] = aux[i]; + } + + return currCount; +} + +int main() { + ifstream fIn("inv.in"); + ofstream fOut("inv.out"); + + fIn >> n; + for (int i = 0; i < n; ++i) { + fIn >> v[i]; + } + + fOut << countInv(0, n - 1); + + fIn.close(); + fOut.close(); + return 0; +} diff --git a/invCount/merge/4440f776-137e-11eb-9bcb-309c234925a0.cpp b/invCount/merge/4440f776-137e-11eb-9bcb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f5f19763f2e5b95e884f4820cb2cd1549b0fc81 --- /dev/null +++ b/invCount/merge/4440f776-137e-11eb-9bcb-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +const int MAXN = 1000005; + + + +int v[MAXN], aux[MAXN]; + + + +long long ans; + + + +int pos1, pos2, k; + + + +void mergesort(int st, int dr){ + + if(st == dr) + + return; + + int mij = (st + dr) / 2; + + mergesort(st, mij); + + mergesort(mij + 1, dr); + + pos1 = st; + + pos2 = mij + 1; + + k = st; + + while(pos1 <= mij && pos2 <= dr){ + + if(v[pos1] <= v[pos2]) + + aux[k++] = v[pos1++]; + + else{ + + aux[k++] = v[pos2++]; + + ans += mij - pos1 + 1; + + ans %= 9917; + + } + + } + + while(pos1 <= mij) + + aux[k++] = v[pos1++]; + + while(pos2 <= dr) + + aux[k++] = v[pos2++]; + + for(int i = st; i <= dr; ++i) + + v[i] = aux[i]; + +} + + + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) + + fin >> v[i]; + + mergesort(1, n); + + fout << ans; + + return 0; + +} diff --git a/invCount/merge/445938db-6e7c-430f-9994-fb0bcd349ace.cpp b/invCount/merge/445938db-6e7c-430f-9994-fb0bcd349ace.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d15090dbcba5a80306d9a8175a3dfc73bf623eb --- /dev/null +++ b/invCount/merge/445938db-6e7c-430f-9994-fb0bcd349ace.cpp @@ -0,0 +1,120 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int v[100010],n; + + + +long long inver(int left, int right) + +{ + + if (left == right) + + return 0; + + int mid = (left + right)/2; + + long long r1 = inver(left,mid); + + long long r2 = inver(mid+1, right); + + long long p1 = left, p2 = mid + 1, r3 = 0,x = 0; + + vector l; + + while (p1 <= mid && p2 <= right) + + { + + if (v[p1] <= v[p2]) + + { + + l.push_back(v[p1]); + + p1++; + + } + + else + + { + + r3 += p2 - p1 -x; + + l.push_back(v[p2]); + + p2++; x++; + + + + } + + } + + while (p1 <= mid) + + { + + l.push_back(v[p1]); + + p1++; + + } + + while (p2 <=right) + + { + + l.push_back(v[p2]); + + p2++; + + } + + + + for (int i = left,j = 0; i <= right; i++, j++) + + v[i] = l[j]; + + + + return r1+r2+r3; + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> v[i]; + + fout << (inver(1,n)%9917); + + return 0; + +} diff --git a/invCount/merge/44624976-137e-11eb-b7fe-309c234925a0.cpp b/invCount/merge/44624976-137e-11eb-b7fe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55cc2f3857b469bcac6fe48ffa15677eaea30d56 --- /dev/null +++ b/invCount/merge/44624976-137e-11eb-b7fe-309c234925a0.cpp @@ -0,0 +1,108 @@ +#include + + + + +const int MV = 1e5 + 5 ; + + + +int n, inv, k ; + +int v[MV], L[MV >> 1], R[MV >> 1] ; + + + +void merge(int st, int dr) { + + int mij = (dr + st) >> 1, k(st - 1), c(0) ; + + int i(1), j(1) ; + + for (i = 1 ; i <= mij - st + 1 ; ++ i) { + + L[i] = v[i + st - 1] ; + + } + + for (i = 1 ; i <= dr - mij ; ++ i) { + + R[i] = v[mij + i] ; + + } + + i = 1 ; + + while (i <= mij - st + 1 && j <= dr - mij) { + + if (L[i] <= R[j]) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } else { + + c = j ; + + v[++ k] = R[j ++] ; + + } + + } + + while (i <= mij - st + 1) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } + + while (j <= dr - mij) { + + v[++ k] = R[j ++] ; + + } + +} + + + +void merge_sort(int st, int dr) { + + if (st >= dr) + + return ; + + merge_sort(st, (st + dr) >> 1) ; + + merge_sort(((st + dr) >> 1) + 1, dr) ; + + merge(st, dr) ; + +} + + + +int main() { + + freopen("inv.in", "r", stdin) ; + + freopen("inv.out", "w", stdout) ; + + int i ; + + scanf("%d", &n) ; + + for (i = 1 ; i <= n ; ++ i) { + + scanf("%d", v + i) ; + + } + + merge_sort(1, n) ; + + printf("%d", inv) ; + + return 0 ; } diff --git a/invCount/merge/4471b2ca-137e-11eb-8bc6-309c234925a0.cpp b/invCount/merge/4471b2ca-137e-11eb-8bc6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..774b63c184545178e16f0e2eea782f28b2496517 --- /dev/null +++ b/invCount/merge/4471b2ca-137e-11eb-8bc6-309c234925a0.cpp @@ -0,0 +1,133 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int NMAX = 100005; + +const int MOD = 9917; + +int a[NMAX]; + +int b[NMAX]; + +int rasp; + + + +void msort(int st,int dr) + +{ + + if(st==dr) return; + + int mij=(st+dr)/2; + + msort(st,mij); + + msort(mij+1,dr); + + int i=st; + + int j=mij+1; + + int k=0; + + while(i<=mij and j<=dr) + + { + + if(a[i]>a[j]) + + { + + rasp+=mij-i+1; + + rasp%=MOD; + + b[++k]=a[j]; + + j++; + + } + + else + + { + + b[++k]=a[i]; + + i++; + + } + + } + + while(i<=mij) + + { + + b[++k]=a[i]; + + i++; + + } + + while(j<=dr) + + { + + b[++k]=a[j]; + + j++; + + } + + k=1; + + for(int i=st;i<=dr;i++) + + { + + a[i]=b[k]; + + k++; + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i=1;i<=n;i++) + + { + + fin >> a[i]; + + } + + msort(1,n); + + fout << rasp << '\n'; + + return 0; + +} diff --git a/invCount/merge/4492f66c-137e-11eb-bd54-309c234925a0.cpp b/invCount/merge/4492f66c-137e-11eb-bd54-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e0c82418b0954ebf82b6ab24fef1c7f40772274 --- /dev/null +++ b/invCount/merge/4492f66c-137e-11eb-bd54-309c234925a0.cpp @@ -0,0 +1,66 @@ +#include + + + + +using namespace std; + +int v[100001],aux[100001],sol; + +void intercls(int st,int dr,int mij) { + + int i=st,j=mij+1,k=st-1; + + while (i<=mij || j<=dr) + + if ((v[i]>v[j] && j<=dr) || i>mij) + + aux[++k]=v[j],sol=(sol+mij-i+1)%9917,++j; + + else aux[++k]=v[i],++i; + + + + for (i=st;i<=dr;++i) + + v[i]=aux[i],aux[i]=0; + +} + +void msort(int st,int dr) { + + if (st>n; + + for (i=1;i<=n;++i) + + f>>v[i]; + + msort(1,n); + + g< + + + + +const int MV = 1e5 + 5 ; + + + +int n, inv, k ; + +int v[MV], L[MV >> 1], R[MV >> 1] ; + + + +void merge(int st, int dr) { + + int mij = (dr + st) >> 1, k(st - 1), c(0) ; + + int i(1), j(1) ; + + for (i = 1 ; i <= mij - st + 1 ; ++ i) { + + L[i] = v[i + st - 1] ; + + } + + for (i = 1 ; i <= dr - mij ; ++ i) { + + R[i] = v[mij + i] ; + + } + + i = 1 ; + + while (i <= mij - st + 1 && j <= dr - mij) { + + if (L[i] <= R[j]) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } else { + + c = j ; + + v[++ k] = R[j ++] ; + + } + + } + + while (i <= mij - st + 1) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } + + while (j <= dr - mij) { + + v[++ k] = R[j ++] ; + + } + +} + + + +void merge_sort(int st, int dr) { + + if (st >= dr) + + return ; + + merge_sort(st, (st + dr) >> 1) ; + + merge_sort(((st + dr) >> 1) + 1, dr) ; + + merge(st, dr) ; + +} + + + +int main() { + + freopen("inv.in", "r", stdin) ; + + freopen("inv.out", "w", stdout) ; + + int i ; + + scanf("%d", &n) ; + + for (i = 1 ; i <= n ; ++ i) { + + scanf("%d", v + i) ; + + } + + merge_sort(1, n) ; + + printf("%d", inv) ; + + return 0 ; } diff --git a/invCount/merge/44aee2e2-137e-11eb-990b-309c234925a0.cpp b/invCount/merge/44aee2e2-137e-11eb-990b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6803a5a1c1373e162d138183d326a5eaeb85c9f6 --- /dev/null +++ b/invCount/merge/44aee2e2-137e-11eb-990b-309c234925a0.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +int n; + +int a[100003], b[100003]; + + + +void Citire() + +{ + + int i; + + ifstream fin("inv.in"); + + fin>>n; + + for(i=1;i<=n;++i) + + fin>>a[i]; + + fin.close(); + +} + + + +long long NrInv(int stg, int drp) + +{ + + if(stg==drp) + + return 0; + + if(drp==stg+1) + + { + + if(a[stg] <= a[drp]) + + return 0; + + swap(a[stg],a[drp]); + + return 1; + + } + + int mijl, i, j, indb; + + long long solstg, soldrp, solmijl; + + mijl = (stg+drp)/2; + + solstg = NrInv(stg, mijl); + + soldrp = NrInv(mijl+1, drp); + + + + solmijl=0; + + i=stg; + + j=mijl+1; + + indb=stg-1; + + while(i<=mijl && j<=drp) + + { + + if(a[i] <= a[j]) + + { + + b[++indb] = a[i]; + + i++; + + } + + else + + { + + b[++indb] = a[j]; + + solmijl += 1LL*(mijl-i+1); + + j++; + + + + } + + } + + while(i<=mijl) + + { + + b[++indb] = a[i]; + + i++; + + } + + while(j<=drp) + + { + + b[++indb] = a[j]; + + j++; + + } + + for(i=stg;i<=drp;++i) + + a[i] = b[i]; + + return (solstg + solmijl + soldrp)%9917; + +} + + + +int main() + +{ + + Citire(); + + ofstream fout("inv.out"); + + fout< + +using namespace std; +ifstream fin("inv.in"); +ofstream fout("inv.out"); +int cnt; +int v[100001]; +void dei(int st,int dr) +{ + if(st==dr) + { + return; + } + int mij=(st+dr)/2; + dei(st,mij); + dei(mij+1,dr); + int B[dr-st+2]; + int i=st,j=mij+1,pas=0; + while(i<=mij && j<=dr) + { + if(v[i]>v[j]) + { + cnt+=(mij-i+1); + cnt%=9917; + B[pas++]=v[j]; + j++; + } + else + { + B[pas++]=v[i]; + i++; + } + } + while(i<=mij) {B[pas++]=v[i];i++;}; + while(j<=dr) {B[pas++]=v[j];j++;}; + for(i=st,pas=0;i<=dr;i++,pas++) v[i]=B[pas]; +} +int main() +{ + int n,i; + fin>>n; + for(i=1;i<=n;i++) fin>>v[i]; + dei(1,n); + fout< +#include + +std::ifstream fin("inv.in"); +std::ofstream fout("inv.out"); + +const int kMod = 9917; +const int kMaxN = 100005; + +int n; +int v[kMaxN], res[kMaxN]; + +int merge(int a[], int b[], int a_sz, int b_sz) { + int inv = 0; + int a_pos = 0; + int b_pos = 0; + int pos = 0; + + for (a_pos = 0; a_pos < a_sz && b_pos < b_sz; ++pos) { + if (b[b_pos] < a[a_pos]) { + inv += a_sz - a_pos; + res[pos] = b[b_pos]; + ++b_pos; + } + else { + res[pos] = a[a_pos]; + ++a_pos; + } + } + + for (; b_pos < b_sz; ++b_pos, ++pos) { + res[pos] = b[b_pos]; + } + + for (; a_pos < a_sz; ++a_pos, ++pos) { + res[pos] = a[a_pos]; + } + + for (int i = 0; i < a_sz + b_sz; ++i) { + a[i] = res[i]; + } + + return inv; +} + +int merge_sort(int lf = 0, int rg = n - 1) { + if (rg == lf) { + return 0; + } + + int med = (lf + rg) / 2; + + int lf_inv = merge_sort(lf, med); + int rg_inv = merge_sort(med + 1, rg); + + int curr_inv = merge(v + lf, v + med + 1, med - lf + 1, rg - med); + return (lf_inv + rg_inv + curr_inv) % kMod; +} + +int main() { + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> v[i]; + } + + fout << merge_sort() << '\n'; + return 0; +} diff --git a/invCount/merge/4510641e-137e-11eb-afac-309c234925a0.cpp b/invCount/merge/4510641e-137e-11eb-afac-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1f301df0b6d3cf99fa397074de5be4edfbaa559 --- /dev/null +++ b/invCount/merge/4510641e-137e-11eb-afac-309c234925a0.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; + +const int NMAX = 100000, MD = 9917; +int sir[NMAX + 1]; +int mergeSort(int l,int r) +{ + int i, j, k, mid , counter, tmp[NMAX + 1]; + mid = (l + r) / 2; + i = l; + j = mid +1; + k = 0; + counter = 0; + while(i <= mid && j <= r) + { + if(sir[i] <= sir[j]) + { + tmp[k] = sir[i++]; + } + else + { + tmp[k] = sir[j++]; + counter += mid -i+1; + counter %= MD; + } + k++; + } + while(i <= mid) + { + tmp[k] = sir[i++]; + k++; + } + while(j <= r) + { + tmp[k] = sir[j++]; + k++; + } + for(i = l; i <= r; i++) + { + sir[i] = tmp[i-l]; + } + return counter; +} +int inversiuni(int st, int dr) +{ + if (st >= dr) + { + return 0; + } + int mij, leftInv, rightInv, totalInv; + mij = (st+dr)/2; + leftInv = inversiuni(st,mij); + rightInv = inversiuni(mij+1,dr); + totalInv = leftInv+rightInv+mergeSort(st,dr); + return totalInv; +} +int main() +{ + ifstream fin("inv.in"); + ofstream fout("inv.out"); + int n,i; + fin >> n; + for(i = 1; i <= n; i++) + { + fin >> sir[i]; + } + fout << inversiuni(1,n) % MD; + fin.close(); + fout.close(); + return 0; +} diff --git a/invCount/merge/4522175c-137e-11eb-84d5-309c234925a0.cpp b/invCount/merge/4522175c-137e-11eb-84d5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7999ff61cd39269f225aa91789481710657ecf --- /dev/null +++ b/invCount/merge/4522175c-137e-11eb-84d5-309c234925a0.cpp @@ -0,0 +1,139 @@ +#include +#include +using namespace std; + + + +int v[100001]; + +int mergeSort(int l,int r) + +{ + + int i,j,k,mij,counter,t[100001]; + + mij=(l+r)/2; + + i=l; + + j=mij+1; + + k=0; + + counter=0; + + while(i<=mij && j<=r) + + { + + if(v[i]<=v[j]) + + { + + t[k]=v[i++]; + + } + + else + + { + + t[k]=v[j++]; + + counter+=mij-i+1; + + counter%=9917; + + } + + k++; + + } + + while(i<=mij) + + { + + t[k]=v[i++]; + + k++; + + } + + while(j<=r) + + { + + t[k]=v[j++]; + + k++; + + } + + for(i=l;i<=r;i++) + + { + + v[i]=t[i-l]; + + } + + return counter; + +} + +int inversiuni(int st, int dr) + +{ + + if (st>=dr) + + { + + return 0; + + } + + int mij,x,y,z; + + mij=(st+dr)/2; + + x=inversiuni(st,mij); + + y=inversiuni(mij+1,dr); + + z=x+y+mergeSort(st,dr); + + return z; + +} + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n,i,counter=0; + + fin>>n; + + for(i=1;i<=n;i++) + + { + + fin>>v[i]; + + } + + fout< + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int NMAX=1e5, M=9917; + + + +int a[NMAX],N; + + + +int v_merge(int a[],int l, int r, int m) + +{ + + int i=l; + + int j=m+1; + + int p=l; + + int c[NMAX]; + + int ans=0; + + while(i<=m && j<=r) + + { + + if(a[i]<=a[j]) + + { + + c[p++]=a[i++]; + + } + + else + + { + + c[p++]=a[j++]; + + ans+=m-i+1; + + ans%=M; + + } + + } + + for(i;i<=m;++i) + + { + + c[p++]=a[i]; + + } + + for(j;j<=r;++j) + + { + + c[p++]=a[j]; + + } + + for(int i=l;i=r) + + { + + return 0; + + } + + { + + int m=(l+r)/2; + + int ileft=ninv(a,l,m); + + int iright=ninv(a,m+1,r); + + return (ileft+iright+v_merge(a,l,r,m))%M; + + } + +} + + + +void read() + +{ + + in>>N; + + for(int i=0;i>a[i]; + + } + +} + + + +void solve() + +{ + + read(); + + int ans=ninv(a,0,N-1); + + out< +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector v; + + + + + + + +int interclasare(int left1, int right1, int left2, int right2) + +{ + + int nr_inversiuni = 0; + + vector interclasat; + + int i=left1, j= left2; + + while(i<= right1 && j<= right2) + + { + + if(v[i] > v[j]) + + { + + interclasat.push_back(v[j]); + + nr_inversiuni += right1 - i +1; + + j++; + + } + + else + + { + + interclasat.push_back(v[i]); + + i++; + + } + + } + + while(i<=right1) + + { + + interclasat.push_back(v[i]); + + i++; + + } + + while(j<=right2) + + { + + interclasat.push_back(v[j]); + + j++; + + } + + int k = left1; + + for(i=0; i>n; + + int i; + + for(i=1; i<=n; i++) + + { + + int x; + + fin>>x; + + v.push_back(x); + + } + + fout< + + +using namespace std; + + + +ifstream cin ("inv.in"); + +ofstream cout ("inv.out"); + + + +int n; + +long long nr_permutari; + +const int MOD = 9917; + + + +void afisare (int a[], int size_array) + +{ + + for (int i = 0; i < size_array; ++i) + + cout << a[i] << ' '; + + cout << '\n'; + +} + + + +void merge_function (int a[], int low, int mid, int high) + +{ + + int len1 = mid - low + 1; + + int len2 = high - mid; + + int L[len1], R[len2]; + + int i, j; + + for (i = 0; i < len1; ++i) + + L[i] = a[low + i]; + + for (i = 0; i < len2; ++i) + + R[i] = a[mid + i + 1]; + + i = j = 0; + + int k = low; + + while (i < len1 && j < len2) + + { + + if (L[i] <= R[j]) + + a[k++] = L[i++]; + + else + + { + + a[k++] = R[j++]; + + nr_permutari += len1 - i; + + nr_permutari %= MOD; + + } + + } + + while (i < len1) + + a[k++] = L[i++]; + + while (j < len2) + + a[k++] = R[j++]; + +} + + + +void mergesort (int a[], int low, int high) + +{ + + int mid = low + (high - low) / 2; + + if (low < high) + + { + + mergesort(a, low, mid); + + mergesort(a, mid + 1, high); + + merge_function(a, low, mid, high); + + } + +} + + + +int main() + +{ + + cin >> n; + + int a[n]; + + for (int i = 0; i < n; ++i) + + cin >> a[i]; + + mergesort(a, 0, n - 1); + + cout << nr_permutari % MOD; + + return 0; + +} diff --git a/invCount/merge/4584f7de-137e-11eb-9495-309c234925a0.cpp b/invCount/merge/4584f7de-137e-11eb-9495-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ec07c978b98e4594d46fc95791b0f8ed7ce5b45 --- /dev/null +++ b/invCount/merge/4584f7de-137e-11eb-9495-309c234925a0.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in") ; + +ofstream fout("inv.out") ; + + + +int t[100005] , v[100005] , nrpm ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft >= rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( v[x] <= v[y] ) + + t[i++] = v[x++] ; + + else + + { + + nrpm = (nrpm+mij-x+1)%9917 ; ; + + t[i++] = v[y++] ; + + } + + } + + while ( x <= mij ) + + t[i++] = v[x++] ; + + while ( y <= rght ) + + t[i++] = v[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + v[i] = t[i] ; + +} + + + +int main() + +{ + + int n , i ; + + fin >> n ; + + for ( i = 1 ; i <= n ; i++ ) + + fin >> v[i] ; + + nrpm = 0 ; + + mergemerge(1,n) ; + + fout << nrpm ; + +} diff --git a/invCount/merge/45860c0d-73b6-4d8e-b227-23b9df65a302.cpp b/invCount/merge/45860c0d-73b6-4d8e-b227-23b9df65a302.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe4adffa5607a6b31390d3de47f233985f5ebbb2 --- /dev/null +++ b/invCount/merge/45860c0d-73b6-4d8e-b227-23b9df65a302.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector v; + + + + + + + +int interclasare(int left1, int right1, int left2, int right2) + +{ + + int nr_inversiuni = 0; + + vector interclasat; + + int i=left1, j= left2; + + while(i<= right1 && j<= right2) + + { + + if(v[i] > v[j]) + + { + + interclasat.push_back(v[j]); + + nr_inversiuni += right1 - i +1; + + j++; + + } + + else + + { + + interclasat.push_back(v[i]); + + i++; + + } + + } + + while(i<=right1) + + { + + interclasat.push_back(v[i]); + + i++; + + } + + while(j<=right2) + + { + + interclasat.push_back(v[j]); + + j++; + + } + + int k = left1; + + for(i=0; i>n; + + int i; + + for(i=1; i<=n; i++) + + { + + int x; + + fin>>x; + + v.push_back(x); + + } + + fout< +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n, v[100001]; + + + +int interclaseaza(int st, int m, int dr) + +{ + + int aux[dr-st+2]; + + int i,j,k,nr; + + + + i=st; + + j=m+1; + + k=0; + + nr=0; + + + + while(i<=m && j<=dr) + + { + + + + if (v[i]<=v[j]) + + { + + aux[k]=v[i++]; + + } + + else + + { + + + + + + + + aux[k]=v[j++]; + + nr=(nr+(m-i+1))%9917; + + } + + + + k++; + + } + + + + + + while(i<=m) + + { + + aux[k]=v[i++]; + + k++; + + } + + + + + + while(j<=dr) + + { + + aux[k]=v[j++]; + + k++; + + } + + + + for(i=st; i<=dr; i++) + + + + v[i]=aux[i-st]; + + return nr; + +} + + + +int nrInv(int st, int dr) + +{ + + + + if (st==dr) + + return 0; + + + + else + + { + + + + int m=(st+dr)/2; + + + + int a=nrInv(st,m); + + + + int b=nrInv(m+1,dr); + + + + int c=a+b+interclaseaza(st,m,dr); + + return c; + + } + +} + +int main() + +{ + + int i; + + + + f>>n; + + for (i=0; i>v[i]; + + + + g< +using namespace std; + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + + +int n; + +long long nr_permutari; + +const int MOD = 9917; + + + +void afisare(int a[], int size_array){ + + for(int i = 0 ; i < size_array ; i++){ + + g<>n; + +int a[n]; + +for(int i = 0 ; i < n ; i++) + + f>>a[i]; + + + +mergesort(a, 0, n-1); + + + +g< +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int v[100005]; + + + +long long interclasare (int l1,int r1,int l2,int r2) + +{ + + vector c; + + long long rezultat=0; + + int c_l1=l1,c_l2=l2; + + while (c_l1<=r1&&c_l2<=r2) + + { + + if (v[c_l1]>v[c_l2]) + + { + + rezultat+=r1-c_l1+1; + + c.push_back(v[c_l2]); + + c_l2++; + + } + + else + + { + + c.push_back(v[c_l1]); + + c_l1++; + + } + + } + + while (c_l1<=r1) + + { + + c.push_back(v[c_l1]); + + c_l1++; + + } + + while (c_l2<=r2) + + { + + c.push_back(v[c_l2]); + + c_l2++; + + } + + int contor=l1; + + for (int aux : c) + + {v[contor]=aux; + + contor++;} + + return rezultat; + +} + + + +long long calcul (int left,int right) + +{ int mid=(left+right)/2; + + if (left==right) return 0; + + + + long long r1=calcul (left,mid); + + long long r2=calcul (mid+1,right); + + + + long long r3=interclasare(left,mid,mid+1,right); + + + + return r1+r2+r3; + + + + + +} + + + +int main() + +{ + + int n; + + fin>>n; + + for (int i=1;i<=n;i++) + + fin>>v[i]; + + fout< + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int a[100001],n; + +int inv; + + + +void InterCls(int v[],int l,int m, int r) + +{ int i,j,k=0; + + int aux[100001]; + + i=l; + + j=m+1; + + while(i<=m && j<=r) + + if(v[i]<=v[j]) + + aux[k++]=v[i++]; + + else + + {aux[k++]=v[j++]; + + inv+=(m-i+1); + + inv%=9917; + + } + + while(i<=m) + + aux[k++]=v[i++]; + + while(j<=r) + + aux[k++]=v[j++]; + + int ct=0; + + for(i=l; i<=r; i++) + + v[i]=aux[ct++]; + +} + + + +void MergeSort(int v[],int l,int r) + +{ if(l>n; + + int i; + + for(i=1; i<=n; i++) + + fin>>a[i]; + + MergeSort(a,1,n); + + fout< +#include + + + + + + +using namespace std; + + + +int v[100001], n; + + + +int sol; + + + +void Merge(int mat[], int st, int mij, int dr) + +{ + + int L[100001], D[100001]; + + int i, j, k; + + int n1 = mij - st + 1; + + int n2 = dr - mij; + + + + for(i = 0; i < n1; i++) + + { + + L[i] = mat[st + i]; + + + + + + } + + + + for(i = 0; i < n2; i++) + + D[i] = mat[mij + 1 + i]; + + + + i = j = 0; + + k = st; + + int contor = 0; + + + + while(i < n1 && j < n2) + + { + + if(L[i] > D[j]) + + { + + mat[k] = D[j]; + + + + sol += j - i + n1 - contor; + + j++; + + contor++; + + sol %= 9917; + + } + + else + + { + + mat[k] = L[i]; + + + + i++; + + } + + + + k++; + + } + + + + while(i < n1) + + { + + mat[k] = L[i]; + + + + i++; + + k++; + + } + + + + while(j < n2) + + { + + mat[k] = D[j]; + + + + j++; + + k++; + + } + +} + + + +void MergeSort(int mat[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = st + (dr - st) / 2; + + MergeSort(mat, st, mij); + + MergeSort(mat, mij + 1, dr); + + + + Merge(mat, st, mij, dr); + + } + + + +} + + + +int main() + +{ + + int i, j; + + + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + fin >> n; + + + + for(i = 1; i <= n; i++)fin >> v[i]; + + + + MergeSort(v, 1, n); + + + + fout << sol; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/merge/46316e28-137e-11eb-9814-309c234925a0.cpp b/invCount/merge/46316e28-137e-11eb-9814-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aebc1ff4f5f0144bf05d4cc2ee0a9c11825133c --- /dev/null +++ b/invCount/merge/46316e28-137e-11eb-9814-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include + +const std :: string programName = "inv"; +std :: ifstream f(programName + ".in"); +std :: ofstream g(programName + ".out"); + +const int MAX = 100005, MOD = 9917; + +int N, nr, v[MAX], b[MAX]; + +void read(void); +void merge(int, int, int); +void mergeSort(int, int); + +int main(void) { + read(); + mergeSort(0, N - 1); + g << nr; + return 0x0; +} + +void read(void) { + f >> N; + for (int i = 0; i < N; ++i) + f >> v[i]; +} + +void merge(int l, int r, int mid) { + int i = l, j = mid + 1, k = 1; + while (i <= mid and j <= r){ + if (v[i] <= v[j]) + b[k++] = v[i++]; + else { + b[k++] = v[j++]; + nr = (nr + mid - i + 1) % MOD; + } + } + while (i <= mid) + b[k++] = v[i++]; + while (j <= r) + b[k++] = v[j++]; + for (i = l, k = 1; i <= r; i++, k++) + v[i] = b[k]; +} + +void mergeSort(int l, int r) { + if (l < r) { + int mid = (l + r) / 2; + mergeSort(l, mid); + mergeSort(mid + 1, r); + merge(l, r, mid); + } +} diff --git a/invCount/merge/469dc4d0-137e-11eb-89b8-309c234925a0.cpp b/invCount/merge/469dc4d0-137e-11eb-89b8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09abff7ad37a2205e5b7f646e0adf62afc29d74d --- /dev/null +++ b/invCount/merge/469dc4d0-137e-11eb-89b8-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i,a[100005],aux[100005],n,k; + +void interclasare(int a[],int aux[],int p,int q,int r,int &inv) + +{ + + int i,j,k,c; + + i=p; + + j=q+1; + + k=p; + + c=0; + + while(i<=q&&j<=r) + + { + + if(a[i]<=a[j]) + + { + + aux[k]=a[i]; + + inv=(inv+c)%9917; + + i++; + + k++; + + } + + else + + { + + aux[k]=a[j]; + + c=j-q; + + j++; + + k++; + + } + + } + + while(i<=q) + + { + + aux[k]=a[i]; + + inv=(inv+c)%9917; + + i++; + + k++; + + } + + while(j<=r) + + { + + aux[k]=a[j]; + + j++; + + k++; + + } + + for(i=p;i<=r;i++){a[i]=aux[i];} + +} + +void mgsort(int a[],int aux[],int p,int r,int &inv) + +{ + + int q; + + if(p>n; + + for(i=1;i<=n;i++) + + f>>a[i]; + + mgsort(a,aux,1,n,k); + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector v; + + + + + + + +int interclasare(int left1, int right1, int left2, int right2) + +{ + + int nr_inversiuni = 0; + + vector interclasat; + + int i=left1, j= left2; + + while(i<= right1 && j<= right2) + + { + + if(v[i] > v[j]) + + { + + interclasat.push_back(v[j]); + + nr_inversiuni += right1 - i +1; + + j++; + + } + + else + + { + + interclasat.push_back(v[i]); + + i++; + + } + + } + + while(i<=right1) + + { + + interclasat.push_back(v[i]); + + i++; + + } + + while(j<=right2) + + { + + interclasat.push_back(v[j]); + + j++; + + } + + int k = left1; + + for(i=0; i>n; + + int i; + + for(i=1; i<=n; i++) + + { + + int x; + + fin>>x; + + v.push_back(x); + + } + + fout< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int a[100005], b[100005],n,i,inv; + +void interclasare(int a[], int aux[], int p, int q, int r, int &inv) + +{ int i,j,k,c; + + i=p; j=q+1; k=p; c=0; + + while(i<=q && j<=r) + + { + + if(a[i]<=a[j]) { inv=(inv+c)%9917; aux[k]=a[i]; i++; k++;} + + else {c=j-q; aux[k]=a[j]; j++; k++;} + + } + + while(i<=q){ inv=(inv+c)%9917; aux[k]=a[i]; i++; k++;} + + while(j<=r){aux[k]=a[j]; j++; k++;} + + for(i=p;i<=r;i++){a[i]=aux[i];} + +} + +void mergesort(int a[], int aux[], int p, int r, int &inv) + +{ if(p>n; + + for(i=1;i<=n;i++)fin>>a[i]; + + inv=0; + + mergesort(a,b,1,n,inv); + + fout< +#include +#include + + +using namespace std; + + + + + +int inversiuni(vector &inversat,int stanga,int dreapta) + +{ + + if(stanga==dreapta) + + return 0; + + int mijloc=(stanga+dreapta)/2; + + + + int inv1=inversiuni(inversat,stanga,mijloc); + + int inv2=inversiuni(inversat,mijloc+1,dreapta); + + + + int inv3=0; + + vectorprimul; + + vectoral_doilea; + + for(int i=stanga; i<=mijloc; i++) + + { + + primul.push_back(inversat[i]); + + + + } + + for(int i=mijloc+1; i<=dreapta; i++) + + { + + al_doilea.push_back(inversat[i]); + + + + } + + + + int x=stanga; + + int x1=0; + + int x2=0; + + while(x1!=primul.size()&&x2!=al_doilea.size()) + + { + + + + if(al_doilea[x2]>nr_elemente; + + vector elemente; + + for(int i=0; i>aux; + + elemente.push_back(aux); + + } + + + + out< + + + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int v[100010],n; + + + +long long inver(int left, int right) + +{ + + if (left == right) + + return 0; + + int mid = (left + right)/2; + + long long r1 = inver(left,mid); + + long long r2 = inver(mid+1, right); + + long long p1 = left, p2 = mid + 1, r3 = 0,x = 0; + + vector l; + + while (p1 <= mid && p2 <= right) + + { + + if (v[p1] <= v[p2]) + + { + + l.push_back(v[p1]); + + p1++; + + } + + else + + { + + r3 += p2 - p1 -x; + + l.push_back(v[p2]); + + p2++; x++; + + + + } + + } + + while (p1 <= mid) + + { + + l.push_back(v[p1]); + + p1++; + + } + + while (p2 <=right) + + { + + l.push_back(v[p2]); + + p2++; + + } + + + + for (int i = left,j = 0; i <= right; i++, j++) + + v[i] = l[j]; + + + + return r1+r2+r3; + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> v[i]; + + fout << (inver(1,n)%9917); + + return 0; + +} diff --git a/invCount/merge/472021c0-137e-11eb-abb4-309c234925a0.cpp b/invCount/merge/472021c0-137e-11eb-abb4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6b6037ee2a6e81b95e18a764e5396bffd273d5 --- /dev/null +++ b/invCount/merge/472021c0-137e-11eb-abb4-309c234925a0.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n; + +vector v, M; + +long long int inv; + +void citire() + +{ + + int i; + + f >> n; + + v.resize(n + 3); + + M.resize(n + 3); + + for (i = 1; i <= n; i++) + + { + + f >> v[i]; + + } + + f.close(); + +} + +void merge(int l1, int r1, int l2, int r2) + +{ + + int i, j, k; + + i = l1; j = l2; k = 1; + + while (i <= r1 && j <= r2) + + { + + if (v[j] < v[i]) + + { + + M[k] = v[j]; + + k++; j++; + + inv += (r1 - i + 1); + + } + + else + + { + + M[k] = v[i]; + + i++; k++; + + } + + } + + while (i <= r1) + + { + + M[k] = v[i]; + + k++; i++; + + } + + while (j <= r2) + + { + + M[k] = v[j]; + + k++; j++; + + } + + for (i = l1, j = 1; i <= r2; j++, i++) + + { + + v[i] = M[j]; + + M[j] = 0; + + } + +} + +void divide(int left, int right) + +{ + + if (right == left) + + { + + return; + + } + + int mid = (left + right) / 2; + + divide(left, mid); + + divide(mid + 1, right); + + merge(left, mid, mid + 1, right); + +} + +int main() + +{ + + citire(); + + divide(1, n); + + g << inv % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/472f8b0c-137e-11eb-9fdc-309c234925a0.cpp b/invCount/merge/472f8b0c-137e-11eb-9fdc-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ebe2c421d2f2f32f5f65360493681a29cd29678 --- /dev/null +++ b/invCount/merge/472f8b0c-137e-11eb-9fdc-309c234925a0.cpp @@ -0,0 +1,103 @@ +#include +#include + + +using namespace std; + + + +int interclasare(int v[], int t[], int left, int mid, int right) + +{ + + int i=left, j=mid, k=left, nrInversiuni=0; + + while (i <= mid - 1 && j <= right) + + { + + if (v[i] <= v[j]) + + t[k++] = v[i++]; + + else + + { + + t[k++] = v[j++]; + + nrInversiuni += mid - i; + + } + + } + + while (i <= mid - 1) + + t[k++] = v[i++]; + + while (j <= right) + + t[k++] = v[j++]; + + for (i = left; i <= right; i++) + + v[i] = t[i]; + + return nrInversiuni%9917; + +} + + + +int inversiuni(int v[], int t[], int left, int right) + +{ + + int nrInversiuni{0}; + + if (right > left) + + { + + int mid = (right + left) / 2; + + nrInversiuni = inversiuni(v, t, left, mid); + + nrInversiuni += inversiuni(v, t, mid + 1, right); + + nrInversiuni += interclasare(v, t, left, mid + 1, right); + + } + + return nrInversiuni%9917; + +} + + + +int main() + +{ + + ifstream fin("inv.in"); + + int n; + + fin >> n; + + int v[n]; + + for(int i=0; i> v[i]; + + int t[n]; + + ofstream fout("inv.out"); + + fout << inversiuni(v, t, 0, n-1); + + return 0; + +} diff --git a/invCount/merge/475143cc-137e-11eb-bf90-309c234925a0.cpp b/invCount/merge/475143cc-137e-11eb-bf90-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9291e1023cae85c917058795f09c034d88bb860 --- /dev/null +++ b/invCount/merge/475143cc-137e-11eb-bf90-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include + + + + +using namespace std; + +int v[100005]; + +int interclaseaza(int st,int dr) + +{ + + int mij=(st+dr)/2,i=st,j=mij+1,n3=0,nU=0; + + int *u=new int[dr-st+2]; + + while (i<=mij && j<=dr) + + { + + if (v[i]<=v[j]) + + { + + u[++nU]=v[i]; + + i++; + + } + + else + + { + + u[++nU]=v[j]; + + n3=(n3+(mij-i+1))%9917; + + j++; + + } + + } + + while (i<=mij) + + { + + u[++nU]=v[i]; + + i++; + + } + + while (j<=dr) + + { + + u[++nU]=v[j]; + + j++; + + } + + for (i=st;i<=dr;i++) + + { + + v[i]=u[i-st+1]; + + } + + delete []u; + + return n3; + + + +} + +int inversiuni(int st, int dr) + +{ + + if (st==dr) + + return 0; + + int mij=(st+dr)/2; + + int n1=inversiuni(st,mij); + + int n2=inversiuni(mij+1,dr); + + return (n1+n2+interclaseaza(st,dr))%9917; + +} + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n,i; + + fin>>n; + + for (i=1;i<=n;i++) + + { + + fin>>v[i]; + + } + + fout< + + + + +using namespace std; + + + +int nr_inversiuni; + + + +void sortare_interclasare(int *v, int *a, int *b, int n) { + + int i = 0, j = 0, mid = n / 2; + + for (int k = 0; k < n; k++) { + + if (i == mid) { + + v[k] = b[j]; + + j++; + + continue; + + } + + if (j == mid + n % 2) { + + v[k] = a[i]; + + i++; + + continue; + + } + + if (a[i] > b[j]) { + + v[k] = b[j]; + + nr_inversiuni = (nr_inversiuni + mid - i) % 9917; + + j++; + + } + + else { + + v[k] = a[i]; + + i++; + + } + + } + +} + + + +void merge_sort(int *v, int n) { + + if (n > 1) { + + int mid = n / 2, k = 0; + + int a[mid + 1], b[mid + 1]; + + for (int i = 0; i < mid; i++) + + a[i] = v[i]; + + for (int i = mid; i < n; i++) + + b[k++] = v[i]; + + merge_sort(a, mid); + + merge_sort(b, mid + n % 2); + + sortare_interclasare(v, a, b, n); + + } + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + int n, numar; + + scanf("%d", &n); + + int numere[n]; + + for (int i = 0; i < n; i++) { + + scanf("%d", &numar); + + numere[i] = numar; + + } + + merge_sort(numere, n); + + printf("%d ", nr_inversiuni); + + return 0; + +} diff --git a/invCount/merge/47bc2840-137e-11eb-9050-309c234925a0.cpp b/invCount/merge/47bc2840-137e-11eb-9050-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..746942cbcca6ae4e0440db44a8b288f173b79458 --- /dev/null +++ b/invCount/merge/47bc2840-137e-11eb-9050-309c234925a0.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; +typedef long long ll; +typedef pair< ll , ll > PII; + +ll n, ans; +vector < int > a; + +void divide(int st, int dr){ + if (st >= dr) return ; + + int mid = (st + dr) >> 1; + + divide(st, mid); divide(mid + 1, dr); + + vector < int > c; + int p1 = st, p2 = mid + 1; + + while (p1 <= mid && p2 <= dr){ + if (a[p1] <= a[p2]){ + c.push_back(a[p1]); + ans = (ans + p2 - mid - 1) % 9917; + p1++; + }else{ + c.push_back(a[p2]); + p2++; + } + } + + while (p1 <= mid){ + c.push_back(a[p1++]); + ans = (ans + p2 - mid - 1) % 9917; + } + + while (p2 <= dr) c.push_back(a[p2++]); + + int pos = 0; + for (int i = st; i <= dr; i++){ + a[i] = c[pos++]; + } +} + +int main(){ + ios_base::sync_with_stdio(0); + cin.tie(0); cout.tie(0); + + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + cin >> n; + a.push_back(0); + + for (int i = 1, x; i <= n; i++){ + cin >> x; + a.push_back(x); + } + + divide(1, n); + + cout << ans; + + return 0; +} diff --git a/invCount/merge/47cc6efa-137e-11eb-b237-309c234925a0.cpp b/invCount/merge/47cc6efa-137e-11eb-b237-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58fb38c15433d52103627eb6506bee55511ed89b --- /dev/null +++ b/invCount/merge/47cc6efa-137e-11eb-b237-309c234925a0.cpp @@ -0,0 +1,133 @@ +#include +#include +using namespace std; + +ifstream f("inv.in") ; + +ofstream g("inv.out") ; + +long long x[100001] , n , i , nrinv ; + + + +void interclaseaza(int st, int mij, int dr) + +{ + + long long i, j, k = 0, y[100001] ; + + i = st; + + j = mij + 1; + + + + + + while (i <= mij && j <= dr) + + { + + if (x[i] <= x[j]) + + { + + k++; + + y[k] = x[i]; + + i++; + + } + + else + + { + + k++; + + y[k] = x[j]; + + j++; + + nrinv = ( nrinv + mij - i + 1) % 9917; + + } + + } + + while (i <= mij) + + { + + k++; + + y[k] = x[i]; + + ++i; + + } + + while (j <= dr) + + { + + k++; + + y[k] = x[j]; + + ++j; + + } + + + + k=1; + + for (i = st; i <= dr; ++i) + + { + + x[i] = y[k]; + + k++; + + } + +} + + + + void mergesort( int st , int dr) + + { + + if( st < dr ) + + { + + mergesort( st , ( st + dr ) / 2 ) ; + + mergesort( ( st + dr ) / 2 + 1 , dr ) ; + + interclaseaza( st , ( st + dr ) / 2 , dr ) ; } + + } + +int main() + +{ + + f >> n ; + + for( i = 1 ; i <= n ; i ++ ) + + f >> x[i] ; + + mergesort( 1 , n ) ; + + g << nrinv ; + + return 0 ; + +} diff --git a/invCount/merge/47dd37e4-137e-11eb-9681-309c234925a0.cpp b/invCount/merge/47dd37e4-137e-11eb-9681-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8a9f6b989bb145fad5ffe1aee53aebdd2353e6 --- /dev/null +++ b/invCount/merge/47dd37e4-137e-11eb-9681-309c234925a0.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int v[100005]; + + + +long long interclasare (int l1,int r1,int l2,int r2) + +{ + + vector c; + + long long rezultat=0; + + int c_l1=l1,c_l2=l2; + + while (c_l1<=r1&&c_l2<=r2) + + { + + if (v[c_l1]>v[c_l2]) + + { + + rezultat+=r1-c_l1+1; + + c.push_back(v[c_l2]); + + c_l2++; + + } + + else + + { + + c.push_back(v[c_l1]); + + c_l1++; + + } + + } + + while (c_l1<=r1) + + { + + c.push_back(v[c_l1]); + + c_l1++; + + } + + while (c_l2<=r2) + + { + + c.push_back(v[c_l2]); + + c_l2++; + + } + + int contor=l1; + + for (int aux : c) + + {v[contor]=aux; + + contor++;} + + return rezultat; + +} + + + +long long calcul (int left,int right) + +{ int mid=(left+right)/2; + + if (left==right) return 0; + + + + long long r1=calcul (left,mid); + + long long r2=calcul (mid+1,right); + + + + long long r3=interclasare(left,mid,mid+1,right); + + + + return r1+r2+r3; + + + + + +} + + + +int main() + +{ + + int n; + + fin>>n; + + for (int i=1;i<=n;i++) + + fin>>v[i]; + + fout< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,a[100001],b[100001]; + +int inv=0; + +void Divide(int left, int right) + +{ + + if(left==right) return; + + + + int mid=(left+right)/2; + + Divide(left,mid); + + Divide(mid+1,right); + + + + int x=left,y=mid+1,dim=left-1; + + while(x<=mid && y<=right) + + { + + if(a[x]<=a[y]) {dim++;b[dim]=a[x];x++;} + + else {dim++;b[dim]=a[y];inv=(inv+(mid-x+1)) % 9917;y++;} + + } + + while(y<=right) {dim++;b[dim]=a[y];y++;} + + while(x<=mid) {dim++;b[dim]=a[x];x++;} + + + + for(int i=left;i<=right;i++) + + a[i]=b[i]; + +} + +int main() + +{ + + fin>>n; + + for(int i=1; i<=n; i++) + + fin>>a[i]; + + + + Divide(1,n); + +fout< +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +const int mod = 9917; + +vector< int > v; +int ans; + +void nrInv(int left, int mid, int right) { + vector< int > rez; + + int i = left, j = mid + 1; + while(i <= mid && j <= right) { + if(v[i] <= v[j]) { + ans += j - mid - 1; + rez.push_back(v[i]); + ++i; + } else { + rez.push_back(v[j]); + ++j; + } + } + + while(j <= right) { + rez.push_back(v[j]); + ++j; + } + while(i <= mid) { + rez.push_back(v[i]); + ++i; + ans += right - mid; + } + + ans %= mod; + + for(int i = 0; i < (int)rez.size(); ++i) { + v[left + i] = rez[i]; + } +} + +void merge(int left, int right) { + if(left == right) { + return; + } + + int mid = (left + right) / 2; + merge(left, mid); + merge(mid + 1, right); + nrInv(left, mid, right); +} + +int main() { + ios::sync_with_stdio(false); in.tie(0); out.tie(0); + + int n; in >> n; + + v.resize(n); + for(auto &x: v) in >> x; + + merge(0, n - 1); + + out << ans % mod << "\n"; + + in.close(); out.close(); + + return 0; +} diff --git a/invCount/merge/480cd362-137e-11eb-a5df-309c234925a0.cpp b/invCount/merge/480cd362-137e-11eb-a5df-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34c319f30b1d475b2fdaf9b50f6c446724bb28a4 --- /dev/null +++ b/invCount/merge/480cd362-137e-11eb-a5df-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +vectorv; + +int n; + +long long ans; + +void Citire() + +{ + + int i,x; + + fin>>n; + + v.push_back(0); + + for(i=1;i<=n;++i) + + { + + fin>>x; + + v.push_back(x); + + } + +} + +void Divide(int left,int right) + +{ + + if(left a; + + int pos1=left,pos2=mid+1; + + while(pos1<=mid&&pos2<=right) + + { + + if(v[pos1]<=v[pos2]) + + { + + ans=(ans+pos2-mid-1)%9917; + + a.push_back(v[pos1]); + + pos1++; + + } + + else { + + a.push_back(v[pos2]); + + pos2++; + + } + + } + + while(pos1<=mid) + + { + + ans=(ans+pos2-mid-1)%9917; + + a.push_back(v[pos1++]); + + } + + while(pos2<=right)a.push_back(v[pos2++]); + + for(int i=left;i<=right;++i)v[i]=a[i-left]; + + } + +} + +int main() + +{ + + Citire(); + + Divide(1,n); + + fout< +using namespace std; + +const int maxN=1e5+1; +const int MOD=9917; + +int n,sol; +int v[maxN]; +int aux[maxN]; + +void mergeSort(int st,int dr){ + if(st>=dr) + return; + + int mij=(st+dr)/2; + mergeSort(st,mij); + mergeSort(mij+1,dr); + + int leftIdx=st; + int rightIdx=mij+1; + int curr=0; + + while(leftIdx<=mij && rightIdx<=dr) + if(v[leftIdx]<=v[rightIdx]){ + aux[++curr]=v[leftIdx]; + leftIdx++; + }else{ + aux[++curr]=v[rightIdx]; + rightIdx++; + + sol+=mij-leftIdx+1; + while(sol>=MOD) + sol-=MOD; + } + + while(leftIdx<=mij){ + aux[++curr]=v[leftIdx]; + leftIdx++; + } + + while(rightIdx<=dr){ + aux[++curr]=v[rightIdx]; + rightIdx++; + } + + for(int i=st;i<=dr;i++) + v[i]=aux[i-st+1]; +} + +int main() { + ifstream cin("inv.in"); + ofstream cout("inv.out"); + + cin>>n; + for(int i=1;i<=n;i++) + cin>>v[i]; + + mergeSort(1,n); + + cout< +#include + + +using namespace std; + + + +int nr ; + +const int N = 100005; + +const int M = 9917; + + + +void interclasare (int s[], int st, int m, int dr) + +{ + + int i,l, j, k = 0,aux[N]; + i = st; + + j = m+1; + + while ( i <= m && j <= dr) + + { + + if ( s[i] <= s[j]) + + aux[k++] = s[i++]; + + else + + { + + aux[k++] = s[j++]; + + nr += m-i+1; + + nr = nr%M; + + } + + } + + while (i <= m) + + aux[k++] = s[i++]; + + while ( j <= dr) + + aux[k++] = s[j++]; + + for (i = st,k = 0; i <= dr ; i++,k++) + + s[i] = aux[k]; + +} + + + +void MergeSort (int s[], int st, int dr) + +{ + + if ( st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort (s, st, m); + + MergeSort (s, m+1, dr); + + interclasare (s, st, m, dr); + + } + +} + + + +int main() + +{ + + int n, s[N], i, j; + + + + ifstream in ("inv.in"); + + in >> n; + + + + for (i = 0; i < n; i++) + + in >> s[i]; + + in.close(); + + + + MergeSort (s, 0, n-1); + + + + ofstream out("inv.out"); + + out << nr%M; + + out.close(); + + + + return 0; + +} diff --git a/invCount/merge/48652d0c-137e-11eb-9ebb-309c234925a0.cpp b/invCount/merge/48652d0c-137e-11eb-9ebb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3b8201bb5cd5559d4e0f1914c145eeae6c13e76 --- /dev/null +++ b/invCount/merge/48652d0c-137e-11eb-9ebb-309c234925a0.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +int N, List[100005], VA, VB, A[100005], B[100005], i; + +long long int Inv=0LL; + +void MSort(int left, int right); + +void IClass(int left); + +int main() + +{ + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + scanf("%d", &N); + + for(i=1; i<=N; ++i)scanf("%d", &List[i]); + + MSort(1, N); + + printf("%lld", Inv%9917); + + return 0; + +} + +void MSort(int left, int right){ + + if(left==right) return; + + int mid=(left+right)/2; + + MSort(left, mid); + + MSort(mid+1, right); + + int i; + + VA=VB=0; + + for(i=left; i<=mid; ++i) A[++VA]=List[i]; + + for(i=mid+1; i<=right; ++i) B[++VB]=List[i]; + + IClass(left); + + return; + +} + +void IClass(int left){ + + int i, j; + + i=j=1; --left; + + while(i<=VA && j<=VB){ + + if(A[i]>B[j]){ + + Inv+=(VA-i+1); + + List[++left]=B[j++]; + + } + + else List[++left]=A[i++]; + + } + + while(i<=VA)List[++left]=A[i++]; + + while(j<=VB)List[++left]=B[j++]; + + return; + +} diff --git a/invCount/merge/486e1ffd-787d-473a-a646-008a9c38811e.cpp b/invCount/merge/486e1ffd-787d-473a-a646-008a9c38811e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aea747e81a1f0f033112bad72e85b14053f4355 --- /dev/null +++ b/invCount/merge/486e1ffd-787d-473a-a646-008a9c38811e.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +using namespace std; + +const int MOD = 9917; + +int a[100005], b[100005], sol; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +void Merge (int left, int right, int m) + +{ + + int i, j, k = 0; + + i = left; + + j = m + 1; + + while (i <= m && j <= right) + + if (a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + sol = (sol + (m - i + 1)) % MOD; + + b[++k] = a[j++]; + + } + + while (i <= m) + + b[++k] = a[i++]; + + while (j <= right) + + b[++k] = a[j++]; + + j = 1; + + for (i = left; i <= right; i++) + + a[i] = b[j++]; + +} + + + +void MergeSort (int left, int right) + +{ + + if (right - left <= 1) + + { + + if (a[right] < a[left]) + + { + + swap (a[left], a[right]); + + sol++; + + sol %= MOD; + + } + + } + + else + + { + + int m = (left + right) / 2; + + MergeSort(left, m); + + MergeSort(m + 1, right); + + Merge(left, right, m); + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + + MergeSort(1, n); + + fout << sol << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/48761d00-137e-11eb-bbe0-309c234925a0.cpp b/invCount/merge/48761d00-137e-11eb-bbe0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad14c2cefd609991436ce107262abf0b8ce4a56c --- /dev/null +++ b/invCount/merge/48761d00-137e-11eb-bbe0-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include +#include + + +using namespace std; + + + +int nr ; + +const int N = 100005; + +const int M = 9917; + + + +void interclasare (int s[], int st, int m, int dr) + +{ + + int i,l, j, k = 0,aux[N]; + i = st; + + j = m+1; + + while ( i <= m && j <= dr) + + { + + if ( s[i] <= s[j]) + + aux[k++] = s[i++]; + + else + + { + + aux[k++] = s[j++]; + + nr += m-i+1; + + nr = nr%M; + + } + + } + + while (i <= m) + + aux[k++] = s[i++]; + + while ( j <= dr) + + aux[k++] = s[j++]; + + for (i = st,k = 0; i <= dr ; i++,k++) + + s[i] = aux[k]; + +} + + + +void MergeSort (int s[], int st, int dr) + +{ + + if ( st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort (s, st, m); + + MergeSort (s, m+1, dr); + + interclasare (s, st, m, dr); + + } + +} + + + +int main() + +{ + + int n, s[N], i, j; + + + + ifstream in ("inv.in"); + + in >> n; + + + + for (i = 0; i < n; i++) + + in >> s[i]; + + in.close(); + + + + MergeSort (s, 0, n-1); + + + + ofstream out("inv.out"); + + out << nr%M; + + out.close(); + + + + return 0; + +} diff --git a/invCount/merge/4884c308-137e-11eb-aea1-309c234925a0.cpp b/invCount/merge/4884c308-137e-11eb-aea1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5d0be57028aa93f507070f73b701548e75494bb --- /dev/null +++ b/invCount/merge/4884c308-137e-11eb-aea1-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + +int n,v[100005],aux[100005]; + +long long cnt; + +void mergesort(int s,int d){ + + if(s==d) return; + + int m=(s+d)/2; + + mergesort(s,m); + + mergesort(m+1,d); + + int i=s,j=m+1,k=0; + + while(i<=m && j<=d) + + if(v[i]<=v[j]) aux[++k]=v[i++]; + + else { + + aux[++k]=v[j++]; + + cnt+=m-i+1; + + } + + while(i<=m) aux[++k]=v[i++]; + + while(j<=d) aux[++k]=v[j++]; + + for(i=s,k=1;i<=d;i++,k++) + + v[i]=aux[k]; + +} + +int main(){ + + cin>>n; + + for(int i=1;i<=n;i++) + + cin>>v[i]; + + mergesort(1,n); + + cout< + + + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, v[100005]; + + + +int Merge(int st, int dr) + +{ + + int nrInv = 0; + + int mij = (st+dr)/2, i = st, j = mij+1, k = 0; + + int aux[dr-st+1]; + + while(i <= mij && j <= dr) + + if(v[i] <= v[j]) aux[k++] = v[i++]; + + else + + { + + aux[k++] = v[j++]; + + nrInv += (mij-i+1); + + nrInv %= 9917; + + } + + while(i <= mij) aux[k++] = v[i++]; + + while(j <= dr) aux[k++] = v[j++]; + + for(int i = 0; i < k; ++i) v[st+i] = aux[i]; + + return nrInv; + +} + + + +int countInv(int st, int dr) + +{ + + int nrInv = 0; + + if(st < dr) + + { + + nrInv += countInv(st, (st+dr)/2); + + nrInv += countInv((st+dr)/2 + 1, dr); + + nrInv += Merge(st, dr); + + nrInv %= 9917; + + } + + return nrInv; + +} + + + +int main() + +{ + + fin >> n; + + for(int i = 1; i <= n; ++i) fin >> v[i]; + + fin.close(); + + fout << countInv(1, n); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/4c1dcbc6-cfe4-4d05-b18e-df8f92c63eee.cpp b/invCount/merge/4c1dcbc6-cfe4-4d05-b18e-df8f92c63eee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc30e176f0d9250f42dc1bddb0cc4e91078ab623 --- /dev/null +++ b/invCount/merge/4c1dcbc6-cfe4-4d05-b18e-df8f92c63eee.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +long X[100001],N,cnt,secv,nr,tmp[100001]; + + + +void MergeSort(long A[], int st, int dr) + +{ + + if(st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort(A, st , m); + + MergeSort(A, m + 1 , dr); + + int i = st; + + int j = m + 1; + + int k = 0; + + while( i <= m && j <= dr ) + + if( A[i] <= A[j]) + + tmp[++k] = A[i++]; + + else + + { + + tmp[++k] = A[j++]; + + nr = (nr+m-i+1)%9917; + + } + + while(i <= m) + + tmp[++k] = A[i++]; + + while(j <= dr) + + tmp[++k] = A[j++]; + + j=1; + + for(i = st; i<=dr ; i++ , j++) + + A[i] = tmp[j]; + + } + +} + +int main() + +{ + + f >> N; + + for ( int i=1;i<=N;i++ ) + + f >> X[i]; + + MergeSort(X,1,N); + + g << nr; + + return 0; + +} diff --git a/invCount/merge/4c6fa286-fdf6-4971-8526-55560cc70925.cpp b/invCount/merge/4c6fa286-fdf6-4971-8526-55560cc70925.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6852fbc3f1f4909b622effb3cb02578f1af96dd --- /dev/null +++ b/invCount/merge/4c6fa286-fdf6-4971-8526-55560cc70925.cpp @@ -0,0 +1,68 @@ +#include +#include + +std::ifstream fin("inv.in"); +std::ofstream fout("inv.out"); + +const int kMod = 9917; +const int kMaxN = 100005; + +int n; +int v[kMaxN], res[kMaxN]; + +int merge(int a[], int b[], int a_sz, int b_sz) { + int inv = 0; + int a_pos = 0; + int b_pos = 0; + int pos = 0; + + for (a_pos = 0; a_pos < a_sz && b_pos < b_sz; ++pos) { + if (b[b_pos] < a[a_pos]) { + inv += a_sz - a_pos; + res[pos] = b[b_pos]; + ++b_pos; + } + else { + res[pos] = a[a_pos]; + ++a_pos; + } + } + + for (; b_pos < b_sz; ++b_pos, ++pos) { + res[pos] = b[b_pos]; + } + + for (; a_pos < a_sz; ++a_pos, ++pos) { + res[pos] = a[a_pos]; + } + + for (int i = 0; i < a_sz + b_sz; ++i) { + a[i] = res[i]; + } + + return inv; +} + +int merge_sort(int lf = 0, int rg = n - 1) { + if (rg == lf) { + return 0; + } + + int med = (lf + rg) / 2; + + int lf_inv = merge_sort(lf, med); + int rg_inv = merge_sort(med + 1, rg); + + int curr_inv = merge(v + lf, v + med + 1, med - lf + 1, rg - med); + return (lf_inv + rg_inv + curr_inv) % kMod; +} + +int main() { + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> v[i]; + } + + fout << merge_sort() << '\n'; + return 0; +} diff --git a/invCount/merge/4d6bda82-00b2-4ee4-a935-3ac33fff882d.cpp b/invCount/merge/4d6bda82-00b2-4ee4-a935-3ac33fff882d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aebc1ff4f5f0144bf05d4cc2ee0a9c11825133c --- /dev/null +++ b/invCount/merge/4d6bda82-00b2-4ee4-a935-3ac33fff882d.cpp @@ -0,0 +1,53 @@ +#include + +const std :: string programName = "inv"; +std :: ifstream f(programName + ".in"); +std :: ofstream g(programName + ".out"); + +const int MAX = 100005, MOD = 9917; + +int N, nr, v[MAX], b[MAX]; + +void read(void); +void merge(int, int, int); +void mergeSort(int, int); + +int main(void) { + read(); + mergeSort(0, N - 1); + g << nr; + return 0x0; +} + +void read(void) { + f >> N; + for (int i = 0; i < N; ++i) + f >> v[i]; +} + +void merge(int l, int r, int mid) { + int i = l, j = mid + 1, k = 1; + while (i <= mid and j <= r){ + if (v[i] <= v[j]) + b[k++] = v[i++]; + else { + b[k++] = v[j++]; + nr = (nr + mid - i + 1) % MOD; + } + } + while (i <= mid) + b[k++] = v[i++]; + while (j <= r) + b[k++] = v[j++]; + for (i = l, k = 1; i <= r; i++, k++) + v[i] = b[k]; +} + +void mergeSort(int l, int r) { + if (l < r) { + int mid = (l + r) / 2; + mergeSort(l, mid); + mergeSort(mid + 1, r); + merge(l, r, mid); + } +} diff --git a/invCount/merge/4f549385-d20c-4fea-afc6-da5b97c72d80.cpp b/invCount/merge/4f549385-d20c-4fea-afc6-da5b97c72d80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20192d64b32824b1456055f8afa44de876c6c8c9 --- /dev/null +++ b/invCount/merge/4f549385-d20c-4fea-afc6-da5b97c72d80.cpp @@ -0,0 +1,139 @@ +#include +#include + + +int nr = 0; + + + +void interclasare(int a[], int st, int dr){ + + + + int mijloc = (st + dr) / 2; + + + + int i = st; + + int j = mijloc + 1; + + int aux = 0; + + + + int size = dr - st + 1; + + int arr[size]; + + + + while ( i <= mijloc and j <= dr) { + + if (a[i] <= a[j]) { + + arr[aux] = a [i]; + + aux++; + + i++; + + } else { + + nr += mijloc - i + 1; + + nr = nr % 9917; + + arr[aux] = a[j]; + + aux++; + + j++; + + } + + } + + + + while (i <= mijloc) { + + arr[aux] = a[i]; + + aux++; + + i++; + + } + + while (j <= dr) { + + arr[aux] = a[j]; + + aux++; + + j++; + + } + + + + for (int k = 0; k < aux; ++k) { + + a[st+k] = arr[k]; + + } + + + +} + + + +void mergesort(int a[], int st, int dr){ + + if (st == dr) + + return; + + int mijloc = (st + dr) / 2; + + mergesort(a, st, mijloc); + + mergesort(a, mijloc + 1, dr); + + interclasare(a, st, dr); + + + +} + + + +int main() { + + std :: ifstream fin("inv.in"); + + std :: ofstream fout ( "inv.out"); + + + + int n; + + int vt[100005]; + + fin >> n; + + for (int i = 0; i < n; ++i) { + + fin >> vt[i]; + + } + + mergesort(vt, 0, n-1); + + fout << nr; + + return 0; + +} diff --git a/invCount/merge/4fc767fe-eddf-4dac-b096-2907366cdfca.cpp b/invCount/merge/4fc767fe-eddf-4dac-b096-2907366cdfca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b99c12afa900c45a51f7d5c8d143c5756667e38d --- /dev/null +++ b/invCount/merge/4fc767fe-eddf-4dac-b096-2907366cdfca.cpp @@ -0,0 +1,139 @@ +#include +#include + + +int nr = 0; + + + +void interclasare(int a[], int st, int dr){ + + + +int mijloc = (st + dr) / 2; + + + +int i = st; + +int j = mijloc + 1; + +int aux = 0; + + + +int size = dr - st + 1; + +int arr[size]; + + + +while ( i <= mijloc and j <= dr) { + +if (a[i] <= a[j]) { + +arr[aux] = a [i]; + +aux++; + +i++; + +} else { + +nr += mijloc - i + 1; + +nr = nr % 9917; + +arr[aux] = a[j]; + +aux++; + +j++; + +} + +} + + + +while (i <= mijloc) { + +arr[aux] = a[i]; + +aux++; + +i++; + +} + +while (j <= dr) { + +arr[aux] = a[j]; + +aux++; + +j++; + +} + + + +for (int k = 0; k < aux; ++k) { + +a[st+k] = arr[k]; + +} + + + +} + + + +void mergesort(int a[], int st, int dr){ + +if (st == dr) + +return; + +int mijloc = (st + dr) / 2; + +mergesort(a, st, mijloc); + +mergesort(a, mijloc + 1, dr); + +interclasare(a, st, dr); + + + +} + + + +int main() { + +std :: ifstream fin("inv.in"); + +std :: ofstream fout ( "inv.out"); + + + +int n; + +int vt[100005]; + +fin >> n; + +for (int i = 0; i < n; ++i) { + +fin >> vt[i]; + +} + +mergesort(vt, 0, n-1); + +fout << nr; + +return 0; + +} diff --git a/invCount/merge/50c4390e-ac91-4212-a6e9-f5dafafc08aa.cpp b/invCount/merge/50c4390e-ac91-4212-a6e9-f5dafafc08aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ebe2c421d2f2f32f5f65360493681a29cd29678 --- /dev/null +++ b/invCount/merge/50c4390e-ac91-4212-a6e9-f5dafafc08aa.cpp @@ -0,0 +1,103 @@ +#include +#include + + +using namespace std; + + + +int interclasare(int v[], int t[], int left, int mid, int right) + +{ + + int i=left, j=mid, k=left, nrInversiuni=0; + + while (i <= mid - 1 && j <= right) + + { + + if (v[i] <= v[j]) + + t[k++] = v[i++]; + + else + + { + + t[k++] = v[j++]; + + nrInversiuni += mid - i; + + } + + } + + while (i <= mid - 1) + + t[k++] = v[i++]; + + while (j <= right) + + t[k++] = v[j++]; + + for (i = left; i <= right; i++) + + v[i] = t[i]; + + return nrInversiuni%9917; + +} + + + +int inversiuni(int v[], int t[], int left, int right) + +{ + + int nrInversiuni{0}; + + if (right > left) + + { + + int mid = (right + left) / 2; + + nrInversiuni = inversiuni(v, t, left, mid); + + nrInversiuni += inversiuni(v, t, mid + 1, right); + + nrInversiuni += interclasare(v, t, left, mid + 1, right); + + } + + return nrInversiuni%9917; + +} + + + +int main() + +{ + + ifstream fin("inv.in"); + + int n; + + fin >> n; + + int v[n]; + + for(int i=0; i> v[i]; + + int t[n]; + + ofstream fout("inv.out"); + + fout << inversiuni(v, t, 0, n-1); + + return 0; + +} diff --git a/invCount/merge/55468f06-473e-4c5c-bf78-65df0b8ec43a.cpp b/invCount/merge/55468f06-473e-4c5c-bf78-65df0b8ec43a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98a7c80f37398d5ed6a61a476098d8d0b1a7ead1 --- /dev/null +++ b/invCount/merge/55468f06-473e-4c5c-bf78-65df0b8ec43a.cpp @@ -0,0 +1,47 @@ +#include +#include +using std::cin; +using std::cout; + + + std::ifstream fin("inv.in"); + std::ofstream fout("inv.out"); + + + + +int s[100000], aux[100000]; + +int merge (int st, int dr, int v[]) { + int i, j, k, m = (st + dr) >> 1, nr; + i = k = st; + j = m + 1; + nr = 0; + while (i <= m && j <= dr) + if (v[i] <= v[j]) aux[k++] = v[i++]; + else { + aux[k++] = v[j++]; + nr += m - i + 1; + } + + while (i <= m) aux[k++] = v[i++]; + while (j <= dr) aux[k++] = v[j++]; + + for (k = st ; k <= dr ; k++) v[k] = aux[k]; + + return nr; +} + +int msort (int st, int dr, int v[]) { + if (st == dr) return 0; + int m = (st + dr) >> 1; + return (msort(st, m, v) + msort(m + 1, dr, v) + merge(st, dr, v)) % 9917; +} + +int main (int argc, const char * argv[]) { + int n; + fin >> n; + for (auto i = 0 ; i < n ; i++) fin >> s[i]; + fout << msort (0, n - 1, s); + return 0; +} diff --git a/invCount/merge/5621c9d4-15d7-4165-8809-cfeba05d532c.cpp b/invCount/merge/5621c9d4-15d7-4165-8809-cfeba05d532c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ea242d9aa73807cfcbf1d8ca3612ba49441baa7 --- /dev/null +++ b/invCount/merge/5621c9d4-15d7-4165-8809-cfeba05d532c.cpp @@ -0,0 +1,165 @@ +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n, arr[100005]; + +long long cnt; + + + +void interclasare(int st1, int dr1, int st2, int dr2){ + + + + int i = st1; + + int j = st2; + + + + int k = 0; + + + + int * auxarr = new int[dr1 - st1 + dr2 - st2 + 5]; + + + + while(i <= dr1 && j <= dr2){ + + + + if(arr[i] <= arr[j]){ + + + + auxarr[k] = arr[i]; + + i += 1; + + } + + else{ + + + + cnt+= dr1 - i + 1; + + cnt %= 9917; + + + + auxarr[k] = arr[j]; + + j += 1; + + } + + k += 1; + + } + + + + while(i <= dr1){ + + + + auxarr[k] = arr[i]; + + i += 1; + + k += 1; + + } + + + + while(j <= dr2){ + + + + auxarr[k] = arr[j]; + + j += 1; + + k += 1; + + } + + + + for(int i = 0; i < k; i++) + + arr[st1 + i] = auxarr[i]; + + + + delete[] auxarr; + +} + + + +void mergesort(int st, int dr){ + + + + if(st >= dr) + + return; + + + + int m = (st + dr) / 2; + + + + mergesort(st, m); + + mergesort(m + 1, dr); + + + + interclasare(st, m, m + 1, dr); + +} + + + + + +int main(){ + + + + f>>n; + + + + for(int i = 0; i < n; i++) + + f>>arr[i]; + + + + mergesort(0, n - 1); + + + + g< + + +using namespace std; + + + +int n; + +int x[100005]; + +int s[100007]; + +int d[100006]; + + + +int merge_bine(int st,int mid,int dr) + +{ + + int i,j; + + long long sol=0; + + int nrs=mid-st+1; + + int nrd=dr-mid; + + for(i=1;i<=nrs;i++) s[i]=x[st+i-1]; + + for(i=1;i<=nrd;i++) d[i]=x[mid+i]; + + s[nrs+1]=0x7fffffff; + + d[nrd+1]=0x7fffffff; + + i=1; j=1; + + for(int k=st;k<=dr;k++) + + { + + if(s[i]<=d[j]) x[k]=s[i++]; + + else + + { + + x[k]=d[j++]; + + if(i!=nrs+1) + + { + + sol+=nrs+1-i; + + if(sol>9917) sol-=9917; + + } + + } + + } + + return sol; + +} + + + +int inversions(int st,int dr) + +{ + + if(st==dr) return 0; + + int mid=st+(dr-st)/2; + + long long sol=0; + + sol+=inversions(st,mid); + + sol+=inversions(mid+1,dr); + + sol+=merge_bine(st,mid,dr); + + sol%=9917; + + return sol; + +} + + + +int main() + +{ + + ifstream t1("inv.in"); + + ofstream t2("inv.out"); + + t1>>n; + + for(int i=1;i<=n;i++) t1>>x[i]; + + t2< +#include + + +using namespace std; + +ifstream r("inv.in"); + +ofstream w("inv.out"); + +int v[500003], sol=0; + +const int mod=9917; + +void unire(int a, int mij, int b){ + + int n1 = mij - a + 1, n2 = b - mij, g[n1], h[n2], nr=0, rasp=0; + + for(int i=0;i>n; + + for(int i=0; i>v[i]; + + } + + mergesort(0, n-1); + + w< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int v[], int l, int r) + +{ + + if(l < r) + + { + + int mid = (l + r) / 2; + + MergeSort(v, l , mid); + + MergeSort(v, mid + 1 , r); + + int i = l, j = mid + 1, k = 0; + + while( i <= mid && j <= r ) + + if( v[i] <= v[j]) + + temp[++k] = v[i++]; + + else + + { + + temp[++k] = v[j++]; + + nr_inv = (nr_inv + mid - i + 1)%9917; + + } + + while(i <= mid) + + temp[++k] = v[i++]; + + while(j <= r) + + temp[++k] = v[j++]; + + for(i = l , j = 1 ; i <= r ; i ++ , j ++) + + v[i] = temp[j]; + + } + +} + + + +int main() + +{ + + + + int n, v[NR]; + + + + f>>n; + + for(int i = 1; i <= n; i++) + + f>>v[i]; + + + + MergeSort(v,1,n); + + + + g< +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +const int MOD = 9917; + +int N, S[100001], T[100001], nrinv; + +void interclasare(int p, int u, int m) + +{ + + int i = p, j = m + 1, k = 0; + + while(i <= m && j <= u) + + if(S[i] <= S[j]) + + T[++k] = S[i++]; + + else + + { + + T[++k] = S[j++]; + + nrinv = (nrinv + m - i + 1) % MOD; + + } + + while(i <= m) + + T[++k] = S[i++]; + + while(j <= u) + + T[++k] = S[j++]; + + for(i = p, k = 1; i <= u; i++, k++) + + S[i] = T[k]; + +} + +void MergeSort(int p, int u) + +{ + + if(p < u) + + { + + int m = p + (u - p) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, u); + + interclasare(p, u, m); + + } + +} + +int main() + +{ + + f >> N; + + for(int i = 1; i <= N; i++) + + f >> S[i]; + + MergeSort(1,N); + + g << nrinv; + + return 0; + +} diff --git a/invCount/merge/6009222a-ddfd-4526-9570-9b040d5927b2.cpp b/invCount/merge/6009222a-ddfd-4526-9570-9b040d5927b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a8138f477b6e90984dfdf998900739eb76789a5 --- /dev/null +++ b/invCount/merge/6009222a-ddfd-4526-9570-9b040d5927b2.cpp @@ -0,0 +1,160 @@ +#include + + +const int MOD = 9917; + + + +using namespace std; + + + +int Merge(int* v, int* aux, int left, int middle, int right) + +{ + + int index = left; + + int i1 = left; + + int i2 = middle + 1; + + + + int r = 0; + + while (i1 <= middle && i2 <= right) + + { + + if (v[i1] <= v[i2]) + + aux[index++] = v[i1++]; + + else + + { + + aux[index++] = v[i2++]; + + r += middle - i1 + 1; + + r %= MOD; + + } + + } + + + + while (i1 <= middle) + + aux[index++] = v[i1++]; + + + + while (i2 <= right) + + aux[index++] = v[i2++]; + + + + for (int i = left; i <= right; i++) + + v[i] = aux[i]; + + + + return r; + +} + + + +int MergeSort(int* v, int* aux, int left, int right) + +{ + + if (left >= right) + + return 0; + + + + int result = 0; + + int middle = (left + right) / 2; + + + + result += MergeSort(v, aux, left, middle); + + result %= MOD; + + result += MergeSort(v, aux, middle + 1, right); + + result %= MOD; + + result += Merge(v, aux, left, middle, right); + + result %= MOD; + + + + return result; + +} + + + +int main() + +{ + + int n; + + int* v; + + int* aux; + + ifstream fin("inv.in"); + + fin >> n; + + + + v = new int[n]; + + aux = new int[n]; + + + + for (int i = 0; i < n; i++) + + fin >> v[i]; + + fin.close(); + + + + ofstream fout("inv.out"); + + fout << MergeSort(v, aux, 0, n - 1); + + fout.close(); + + + + delete[] aux; + + aux = NULL; + + delete[] v; + + v = NULL; + + + + return 0; + +} diff --git a/invCount/merge/670d7559-88a6-4751-90ae-6362cabcb92e.cpp b/invCount/merge/670d7559-88a6-4751-90ae-6362cabcb92e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbabfe8d695e54b5810d6d92099e75643b06cd7d --- /dev/null +++ b/invCount/merge/670d7559-88a6-4751-90ae-6362cabcb92e.cpp @@ -0,0 +1,69 @@ +#include +#include + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100001]; + +long long merge(int v[], int st, int dr) +{ + int stanga, dreapta, k, m = (st + dr) / 2; + stanga = k = st; + dreapta = m + 1; + long long r = 0; + while (stanga <= m && dreapta <= dr) + { + if (v[stanga] <= v[dreapta]) + { + aux[k++] = v[stanga++]; + } + else + { + aux[k++] = v[dreapta++]; + r += m - stanga + 1; + } + } + while (stanga <= m) + { + aux[k++] = v[stanga++]; + } + while (dreapta <= dr) + { + aux[k++] = v[dreapta++]; + } + for (k = st; k <= dr; k++) + { + v[k] = aux[k]; + } + return r; +} + +long long mergesort(int v[], int st, int dr) +{ + if (st == dr) + { + return 0; + } + int m = (st + dr) / 2; + long long r = 0; + r += mergesort(v, st, m); + r += mergesort(v, m + 1, dr); + r += merge(v, st, dr); + return r; +} + +int main() +{ + int n; + in>>n; + int v[n + 1]; + for(int i=1; i<=n; i++) + in>>v[i]; + out << mergesort(v,1,n)%9917; + in.close(); + out.close(); + return 0; +} diff --git a/invCount/merge/68862451-2385-4885-ac31-2e273c1cc343.cpp b/invCount/merge/68862451-2385-4885-ac31-2e273c1cc343.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c3222f273f169cdfdb2351a3359221c56f9d8af --- /dev/null +++ b/invCount/merge/68862451-2385-4885-ac31-2e273c1cc343.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +const int mod = 9917; + +vector< int > v; +int ans; + +void nrInv(int left, int mid, int right) { + vector< int > rez; + + int i = left, j = mid + 1; + while(i <= mid && j <= right) { + if(v[i] <= v[j]) { + ans += j - mid - 1; + rez.push_back(v[i]); + ++i; + } else { + rez.push_back(v[j]); + ++j; + } + } + + while(j <= right) { + rez.push_back(v[j]); + ++j; + } + while(i <= mid) { + rez.push_back(v[i]); + ++i; + ans += right - mid; + } + + ans %= mod; + + for(int i = 0; i < (int)rez.size(); ++i) { + v[left + i] = rez[i]; + } +} + +void merge(int left, int right) { + if(left == right) { + return; + } + + int mid = (left + right) / 2; + merge(left, mid); + merge(mid + 1, right); + nrInv(left, mid, right); +} + +int main() { + ios::sync_with_stdio(false); in.tie(0); out.tie(0); + + int n; in >> n; + + v.resize(n); + for(auto &x: v) in >> x; + + merge(0, n - 1); + + out << ans % mod << "\n"; + + in.close(); out.close(); + + return 0; +} diff --git a/invCount/merge/68c96537-31bc-43b6-a6a6-3b9c93a418dd.cpp b/invCount/merge/68c96537-31bc-43b6-a6a6-3b9c93a418dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b764ea54112fac096e67c6300fd86a208ba1f918 --- /dev/null +++ b/invCount/merge/68c96537-31bc-43b6-a6a6-3b9c93a418dd.cpp @@ -0,0 +1,211 @@ +#include +#include + + + + + + +using namespace std; + + + +int v[100001], n; + + + +int sol; + + + +void Merge(int mat[], int st, int mij, int dr) + +{ + + int L[100001], D[100001]; + + int i, j, k; + + int n1 = mij - st + 1; + + int n2 = dr - mij; + + + + for(i = 0; i < n1; i++) + + { + + L[i] = mat[st + i]; + + + + + + } + + + + for(i = 0; i < n2; i++) + + D[i] = mat[mij + 1 + i]; + + + + i = j = 0; + + k = st; + + int contor = 0; + + + + while(i < n1 && j < n2) + + { + + if(L[i] > D[j]) + + { + + mat[k] = D[j]; + + + + j++; + + sol += j - i + n1 - 1 - contor; + + contor++; + + sol %= 9917; + + } + + else + + { + + mat[k] = L[i]; + + + + i++; + + } + + + + k++; + + } + + + + while(i < n1) + + { + + mat[k] = L[i]; + + + + i++; + + k++; + + } + + + + while(j < n2) + + { + + mat[k] = D[j]; + + + + j++; + + k++; + + } + +} + + + +void MergeSort(int mat[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij = st + (dr - st) / 2; + + MergeSort(mat, st, mij); + + MergeSort(mat, mij + 1, dr); + + + + Merge(mat, st, mij, dr); + + + + + + } + + + +} + + + +int main() + +{ + + int i, j; + + + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + fin >> n; + + + + for(i = 1; i <= n; i++)fin >> v[i]; + + + + MergeSort(v, 1, n); + + + + + + + + fout << sol; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invCount/merge/694504ec-084b-4a38-bc41-bd24321dbd8d.cpp b/invCount/merge/694504ec-084b-4a38-bc41-bd24321dbd8d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..235b51538e7077cb88ebaa0b67c99f8bfa6b7ba4 --- /dev/null +++ b/invCount/merge/694504ec-084b-4a38-bc41-bd24321dbd8d.cpp @@ -0,0 +1,102 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long v[100000]; + +int n,poz[100000],dreapta; + +long long solutie; + +void MergeSort(long a[],int st, int dr) + +{ + + if(st>n; + + for(int i=0; i>v[i]; + + MergeSort(v,0,n-1); + + fout< + + + +using namespace std; + +int aux[100005], v[100005]; + +int mmerge( int st, int dr, int v[] ) { + int i,j,k,m; + m = ( st + dr ) / 2; + i = k = st; + j = m + 1; + int nr = 0; + while( i <= m && j <= dr ) { + if( v[i] <= v[j] ) + aux[k++] = v[i++]; + else + aux[k++] = v[j++], nr += ( m - i + 1 ) % 9917; + } + while( i <= m ) + aux[k++] = v[i++]; + while( j <= dr ) + aux[k++] = v[j++]; + for( k = st; k <= dr; k ++ ) + v[k] = aux[k]; + return nr; +} + +int msort( int st, int dr, int v[] ) { + int i, j; + if( st == dr ) + return 0; + int m = ( st + dr ) / 2; + i = msort(st,m,v); + j = msort(m+1,dr,v); + return (i + j + mmerge(st,dr,v)) % 9917; +} + +int main() { + freopen("inv.in","r",stdin); + freopen("inv.out","w",stdout); + int i, n, nr; + scanf("%d",&n); + for( i = 1; i <= n; i ++ ) + scanf("%d",&v[i]); + nr = msort(1,n,v) % 9917; + printf("%d",nr); + return 0; +} diff --git a/invCount/merge/6c5d276f-9064-40c6-9528-bb9111023084.cpp b/invCount/merge/6c5d276f-9064-40c6-9528-bb9111023084.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7882ececc7440e662cd24f69ad81082c42c5229 --- /dev/null +++ b/invCount/merge/6c5d276f-9064-40c6-9528-bb9111023084.cpp @@ -0,0 +1,55 @@ +#include + + + +using namespace std; + +int n, v[100000], aux[100000]; + +int countInv(int left, int right) { + if (left >= right) { + return 0; + } + + int mid = (left + right) / 2; + int currCount = (countInv(left, mid) + countInv(mid + 1, right)) % 9917; + int i, j, k; + + for (k = left, i = left, j = mid + 1; i <= mid && j <= right; ++k) { + if (v[i] > v[j]) { + currCount = (currCount + mid - i + 1) % 9917; + aux[k] = v[j++]; + } else { + aux[k] = v[i++]; + } + } + + for (; i <= mid; ++i, ++k) { + aux[k] = v[i]; + } + for (; j <= right; ++j, ++k) { + aux[k] = v[j]; + } + + for (i = left; i < k; ++i) { + v[i] = aux[i]; + } + + return currCount; +} + +int main() { + ifstream fIn("inv.in"); + ofstream fOut("inv.out"); + + fIn >> n; + for (int i = 0; i < n; ++i) { + fIn >> v[i]; + } + + fOut << countInv(0, n - 1); + + fIn.close(); + fOut.close(); + return 0; +} diff --git a/invCount/merge/6ed2fe5a-b885-4313-81ae-ec414c1b05fa.cpp b/invCount/merge/6ed2fe5a-b885-4313-81ae-ec414c1b05fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82fe5c50132d2c6cb8ab3172571cc1f832c21593 --- /dev/null +++ b/invCount/merge/6ed2fe5a-b885-4313-81ae-ec414c1b05fa.cpp @@ -0,0 +1,115 @@ +#include +#include + + + + + + +int v [ 100000 + 1 ] ; + +int aux[ 100000 + 1 ] ; + + + +long long merge (int v[], int st, int dr ) { + + int m , i, j, k ; + + long long r = 0 ; + + m = (st+dr) / 2 ; + + i = k = st ; + + j = m + 1 ; + + while( i <= m && j <= dr ){ + + if(v[i] <= v[j]){ + + aux[k++] = v[i++] ; + + } + + else{ + + aux[k++] = v[j++] ; + + r += m - i + 1 ; + + } + + } + + while(i <= m){ + + aux[k++] = v[i++] ; + + } + + while(j <= dr) { + + aux[k++] = v[j++] ; + + } + + for(k = st; k <= dr ; k++ ){ + + v[k] = aux[k] ; + + } + + return r ; + +} + + + +long long mergesort(int v[],int st,int dr){ + + if(st == dr) + + return 0 ; + + int m = ( st + dr) / 2 ; + + long long r = 0 ; + + r += mergesort (v,st,m) ; + + r += mergesort (v,m+1,dr) ; + + r += merge (v,st,dr) ; + + return r; + +} + + + +int main() { + + FILE *fin, *fout ; + + fin = fopen("inv.in","r"); + + fout = fopen("inv.out","w"); + + int n, i ; + + fscanf(fin, "%d", &n ) ; + + for(i = 0 ; i < n ; i++ ) + + fscanf(fin, "%d", &v[i] ) ; + + fprintf (fout,"%lld ",mergesort(v, 0, n-1) % 9917 ) ; + + fclose(fin) ; + + fclose(fout) ; + + return 0; + +} diff --git a/invCount/merge/705ef72b-2040-4b42-83b8-216a23cc5dfa.cpp b/invCount/merge/705ef72b-2040-4b42-83b8-216a23cc5dfa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37b1622f65e08fe44371445028ed0ba7332d0878 --- /dev/null +++ b/invCount/merge/705ef72b-2040-4b42-83b8-216a23cc5dfa.cpp @@ -0,0 +1,51 @@ +#include +#include + + + + +using namespace std; + +ifstream f("inv.in"); +ofstream g("inv.out"); + +int n,sir[100000 +5],aux[100000 +5],rez; + +void mergesort(int l,int r){ + if(l>=r){ + return; + } + int i,j,m=(l+r)/2,k=l; + mergesort(l,m); + mergesort(m+1,r); + i=l; + j=m+1; + while(i<=m&&j<=r){ + if(sir[i]<=sir[j]){ + aux[k++]=sir[i++]; + }else{ + rez=(rez+m-i+1+9917)%9917; + aux[k++]=sir[j++]; + } + } + while(i<=m){ + aux[k++]=sir[i++]; + } + while(j<=r){ + aux[k++]=sir[j++]; + } + for(int i=l;i<=r;i++){ + sir[i]=aux[i]; + } +} + +int main(){ + f>>n; + for(int i=1;i<=n;i++){ + f>>sir[i]; + } + mergesort(1,n); + g< +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +long long inv = 0; + + + +void interclasare(int s[], int st, int mij, int dr) + +{ + + int i,j,k,rasp = 0,nr = 0; + + int n1 = mij-st+1; + + int n2 = dr - mij; + + + + int S[n1],D[n2]; + + + + for(i = 0; i>n; + + + + int s[100001]; + + + + for(i=1; i<=n; i++) + + { + + fin>>s[i]; + + } + + + + mergesort(s,1,n); + + + + fout< +#include +using namespace std; + +ifstream f("inv.in") ; + +ofstream g("inv.out") ; + +long long x[100001] , n , i , nrinv ; + + + +void interclaseaza(int st, int mij, int dr) + +{ + + long long i, j, k = 0, y[100001] ; + + i = st; + + j = mij + 1; + + + + + + while (i <= mij && j <= dr) + + { + + if (x[i] <= x[j]) + + { + + k++; + + y[k] = x[i]; + + i++; + + } + + else + + { + + k++; + + y[k] = x[j]; + + j++; + + nrinv = ( nrinv + mij - i + 1) % 9917; + + } + + } + + while (i <= mij) + + { + + k++; + + y[k] = x[i]; + + ++i; + + } + + while (j <= dr) + + { + + k++; + + y[k] = x[j]; + + ++j; + + } + + + + k=1; + + for (i = st; i <= dr; ++i) + + { + + x[i] = y[k]; + + k++; + + } + +} + + + + void mergesort( int st , int dr) + + { + + if( st < dr ) + + { + + mergesort( st , ( st + dr ) / 2 ) ; + + mergesort( ( st + dr ) / 2 + 1 , dr ) ; + + interclaseaza( st , ( st + dr ) / 2 , dr ) ; } + + } + +int main() + +{ + + f >> n ; + + for( i = 1 ; i <= n ; i ++ ) + + f >> x[i] ; + + mergesort( 1 , n ) ; + + g << nrinv ; + + return 0 ; + +} diff --git a/invCount/merge/78f0afd6-57a3-460c-b95d-7b306c196ab7.cpp b/invCount/merge/78f0afd6-57a3-460c-b95d-7b306c196ab7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39fdb8596c092eff87deaf21837a0cfef719b9ed --- /dev/null +++ b/invCount/merge/78f0afd6-57a3-460c-b95d-7b306c196ab7.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + + + + + + + +typedef pair < int, int > pii; + +typedef long long ll; + +typedef long double ld; + +typedef unsigned int ui; + +typedef unsigned long long ull; + + + +int sol = 0; + +const int MOD = 9917; + + + +void merge(int st, int mid, int dr, vector < int > &v) { + + + + int i = st, j = mid + 1; + + vector < int > aux(dr - st + 1); + + + + int cnt = 0, ind = 0; + + while (i <= mid and j <= dr) { + + if (v[i] <= v[j]) { + + aux[ind ++] = v[i ++]; + + } + + else { + + aux[ind ++] = v[j ++]; + + cnt += mid - i + 1; + + } + + } + + + + while (i <= mid) aux[ind ++] = v[i ++]; + + while (j <= dr) aux[ind ++] = v[j ++]; + + + + for (int i = st; i <= dr; ++ i) { + + v[i] = aux[i - st]; + + } + + + + sol += cnt; + + sol %= MOD; + +} + + + +void mergeSort(int st, int dr, vector < int > &v) { + + + + if (st == dr) return; + + + + int mid = (st + dr) >> 1; + + + + mergeSort(st, mid, v); + + mergeSort(mid + 1, dr, v); + + + + merge(st, mid, dr, v); + +} + + + +int main() { + ios::sync_with_stdio(false); + + cin.tie(0);cout.tie(0); + + + + int n; + + cin >> n; + + + + vector < int > v(n); + + for (auto &x : v) cin >> x; + + + + mergeSort(0, n - 1, v); + + cout << sol << '\n'; + +} diff --git a/invCount/merge/7ac90ca7-1c8e-4690-aaeb-f1e410402b25.cpp b/invCount/merge/7ac90ca7-1c8e-4690-aaeb-f1e410402b25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a104805b5eb2d3077f3ee437426d34e6012679d6 --- /dev/null +++ b/invCount/merge/7ac90ca7-1c8e-4690-aaeb-f1e410402b25.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int kMax = 100005; + +const int kMod = 9917; + +int v[kMax], rez[kMax]; + + + +int merge_halves(int a[], int b[], int left, int right, int m) { + + int size_a = m - left + 1; + + int size_b = right - m; + int idxa = 0, idxb = 0, nrInv = 0, idxRez = 0; + + + + for(idxa = 0; idxa < size_a && idxb < size_b; idxRez++) { + + if(a[idxa] > b[idxb]) { + + rez[idxRez] = b[idxb++]; + + nrInv += size_a - idxa; + + } else { + + rez[idxRez] = a[idxa++]; + + } + + } + + + + for(; idxa < size_a; idxRez++, idxa++) { + + rez[idxRez] = a[idxa]; + + } + + + + for(; idxb < size_b; idxRez++, idxb++) { + + rez[idxRez] = b[idxb]; + + } + for(int i = 0; i < size_b + size_a; i++) { + + a[i] = rez[i]; + + } + + + + return nrInv; + +} + + + +int merge_sort(int left, int right) { + + if(left == right) + + return 0; + + + + int middle = (left + right) / 2; + + int inv_left = merge_sort(left, middle); + + int inv_right = merge_sort(middle + 1, right); + + + + int crt_inv = merge_halves(v + left, v + middle + 1, left, right, middle); + + return (crt_inv + inv_right + inv_left) % kMod; + +} + + + +int main() { + + int n; + + f >> n; + + for(int i = 0; i < n; i++) { + + f >> v[i]; + + } + + + + g << merge_sort(0, n - 1); + + return 0; + +} diff --git a/invCount/merge/7acd97f6-9ee5-45b2-a6c6-795dd061cc11.cpp b/invCount/merge/7acd97f6-9ee5-45b2-a6c6-795dd061cc11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f1a9384a447ce6ffea4013c944d7d1f48669a7f --- /dev/null +++ b/invCount/merge/7acd97f6-9ee5-45b2-a6c6-795dd061cc11.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n,v[100005],aux[100005],nr; + + + + + +void mergesort(int l, int r) + +{ + + if(l >= r) + + return; + + int i, j, loc = l; + + int m = (l + r) / 2; + + mergesort (l, m); + + mergesort (m+1, r); + + i = l; + + j = m + 1; + + while (i <= m && j <= r) + + { + + if (v[i] <= v[j]) + + { + + aux[loc++] = v[i++]; + + } + + else + + { + + nr = (nr + m - i + 1) % 9917; + + aux[loc++] = v[j++]; + + } + + } + + while( i <= m) + + { + + aux[loc++] = v[i++]; + + } + + while(j <= r) + + { + + aux[loc++] = v[j++]; + + } + + for(int i = l; i <= r; i++) + + { + + v[i] = aux[i]; + + } + +} + + + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; i++) + + { + + f >> v[i]; + + } + + mergesort(1, n); + + g << nr; + + f.close(); + + g.close(); + +} diff --git a/invCount/merge/7bbf17f3-fcb8-4121-86d9-fbe082bbf131.cpp b/invCount/merge/7bbf17f3-fcb8-4121-86d9-fbe082bbf131.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad14c2cefd609991436ce107262abf0b8ce4a56c --- /dev/null +++ b/invCount/merge/7bbf17f3-fcb8-4121-86d9-fbe082bbf131.cpp @@ -0,0 +1,122 @@ +#include +#include + + +using namespace std; + + + +int nr ; + +const int N = 100005; + +const int M = 9917; + + + +void interclasare (int s[], int st, int m, int dr) + +{ + + int i,l, j, k = 0,aux[N]; + i = st; + + j = m+1; + + while ( i <= m && j <= dr) + + { + + if ( s[i] <= s[j]) + + aux[k++] = s[i++]; + + else + + { + + aux[k++] = s[j++]; + + nr += m-i+1; + + nr = nr%M; + + } + + } + + while (i <= m) + + aux[k++] = s[i++]; + + while ( j <= dr) + + aux[k++] = s[j++]; + + for (i = st,k = 0; i <= dr ; i++,k++) + + s[i] = aux[k]; + +} + + + +void MergeSort (int s[], int st, int dr) + +{ + + if ( st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort (s, st, m); + + MergeSort (s, m+1, dr); + + interclasare (s, st, m, dr); + + } + +} + + + +int main() + +{ + + int n, s[N], i, j; + + + + ifstream in ("inv.in"); + + in >> n; + + + + for (i = 0; i < n; i++) + + in >> s[i]; + + in.close(); + + + + MergeSort (s, 0, n-1); + + + + ofstream out("inv.out"); + + out << nr%M; + + out.close(); + + + + return 0; + +} diff --git a/invCount/merge/7cdb0b1e-6b32-4725-a31c-bc26db9f0af0.cpp b/invCount/merge/7cdb0b1e-6b32-4725-a31c-bc26db9f0af0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55cc2f3857b469bcac6fe48ffa15677eaea30d56 --- /dev/null +++ b/invCount/merge/7cdb0b1e-6b32-4725-a31c-bc26db9f0af0.cpp @@ -0,0 +1,108 @@ +#include + + + + +const int MV = 1e5 + 5 ; + + + +int n, inv, k ; + +int v[MV], L[MV >> 1], R[MV >> 1] ; + + + +void merge(int st, int dr) { + + int mij = (dr + st) >> 1, k(st - 1), c(0) ; + + int i(1), j(1) ; + + for (i = 1 ; i <= mij - st + 1 ; ++ i) { + + L[i] = v[i + st - 1] ; + + } + + for (i = 1 ; i <= dr - mij ; ++ i) { + + R[i] = v[mij + i] ; + + } + + i = 1 ; + + while (i <= mij - st + 1 && j <= dr - mij) { + + if (L[i] <= R[j]) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } else { + + c = j ; + + v[++ k] = R[j ++] ; + + } + + } + + while (i <= mij - st + 1) { + + v[++ k] = L[i ++] ; + + inv = (inv + c) % 9917 ; + + } + + while (j <= dr - mij) { + + v[++ k] = R[j ++] ; + + } + +} + + + +void merge_sort(int st, int dr) { + + if (st >= dr) + + return ; + + merge_sort(st, (st + dr) >> 1) ; + + merge_sort(((st + dr) >> 1) + 1, dr) ; + + merge(st, dr) ; + +} + + + +int main() { + + freopen("inv.in", "r", stdin) ; + + freopen("inv.out", "w", stdout) ; + + int i ; + + scanf("%d", &n) ; + + for (i = 1 ; i <= n ; ++ i) { + + scanf("%d", v + i) ; + + } + + merge_sort(1, n) ; + + printf("%d", inv) ; + + return 0 ; } diff --git a/invCount/merge/7ce24dc7-9165-42f8-93f0-b4e5033bdb64.cpp b/invCount/merge/7ce24dc7-9165-42f8-93f0-b4e5033bdb64.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6b6037ee2a6e81b95e18a764e5396bffd273d5 --- /dev/null +++ b/invCount/merge/7ce24dc7-9165-42f8-93f0-b4e5033bdb64.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int n; + +vector v, M; + +long long int inv; + +void citire() + +{ + + int i; + + f >> n; + + v.resize(n + 3); + + M.resize(n + 3); + + for (i = 1; i <= n; i++) + + { + + f >> v[i]; + + } + + f.close(); + +} + +void merge(int l1, int r1, int l2, int r2) + +{ + + int i, j, k; + + i = l1; j = l2; k = 1; + + while (i <= r1 && j <= r2) + + { + + if (v[j] < v[i]) + + { + + M[k] = v[j]; + + k++; j++; + + inv += (r1 - i + 1); + + } + + else + + { + + M[k] = v[i]; + + i++; k++; + + } + + } + + while (i <= r1) + + { + + M[k] = v[i]; + + k++; i++; + + } + + while (j <= r2) + + { + + M[k] = v[j]; + + k++; j++; + + } + + for (i = l1, j = 1; i <= r2; j++, i++) + + { + + v[i] = M[j]; + + M[j] = 0; + + } + +} + +void divide(int left, int right) + +{ + + if (right == left) + + { + + return; + + } + + int mid = (left + right) / 2; + + divide(left, mid); + + divide(mid + 1, right); + + merge(left, mid, mid + 1, right); + +} + +int main() + +{ + + citire(); + + divide(1, n); + + g << inv % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/7d56d01b-f4e3-4036-86a5-d3a6fc965418.cpp b/invCount/merge/7d56d01b-f4e3-4036-86a5-d3a6fc965418.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf5389d5218a61842469876b25382f582f8cd017 --- /dev/null +++ b/invCount/merge/7d56d01b-f4e3-4036-86a5-d3a6fc965418.cpp @@ -0,0 +1,72 @@ +#include +#include + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100005],v[100005],n,cnt = 0; + +int mmerge(int st,int dr,int v[]) +{ + int i,j,k,m = (st+dr)/2,nr = 0; + i = k = st; + j = m+1; + while (i <= m && j <= dr) + { + if (v[i] <= v[j]) + { + aux[k++] = v[i++]; + } + else + { + aux[k++] = v[j++]; + nr += m-i+1; + nr %= 9917; + } + } + while (i <= m) + { + aux[k++] = v[i++]; + } + while (j <= dr) + { + aux[k++] = v[j++]; + } + for (k=st; k<=dr; k++) + { + v[k] = aux[k]; + } + return nr; +} + +int msort(int st,int dr,int v[]) +{ + int cnt = 0; + if (st == dr) + { + return 0; + } + int m = (st+dr)/2; + cnt += msort(st,m,v); + cnt %= 9917; + cnt += msort(m+1,dr,v); + cnt %= 9917; + cnt += mmerge(st,dr,v); + cnt %= 9917; + return cnt; +} + +int main() +{ + int i; + in >> n; + for (i=1; i<=n; i++) + { + in >> v[i]; + } + cnt = msort(1,n,v); + out << cnt; + return 0; +} diff --git a/invCount/merge/7d952f85-4684-424a-9c9c-f3076d33e3fd.cpp b/invCount/merge/7d952f85-4684-424a-9c9c-f3076d33e3fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..beb9bac7a3310c9eda5acc4a4992baa89aaab234 --- /dev/null +++ b/invCount/merge/7d952f85-4684-424a-9c9c-f3076d33e3fd.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + + + +int n, a[100007], raspuns; + + + +void Citire() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + +} + + + +void Interclasare(int st, int dr) + +{ + + int mij, i, j, k = 0, aux[dr - st + 7]; + + mij = (st + dr) / 2; + + i = st; + + j = mij + 1; + + + + while (i <= mij && j <= dr) + + { + + if (a[i] <= a[j]) + + aux[++k] = a[i++]; + + else + + { + + raspuns += mij - i + 1; + + raspuns %= 9917; + + aux[++k] = a[j++]; + + } + + } + + + + while(i <= mij) + + aux[++k] = a[i++]; + + + + while(j <= dr) + + aux[++k] = a[j++]; + + + + for (i = 1; i <= k; i++) + + a[st + i - 1] = aux[i]; + +} + + + +void MergeSort(int st, int dr) + +{ + + int mij; + + if (st < dr) + + { + + mij = (st + dr) / 2; + + MergeSort(st, mij); + + MergeSort(mij + 1, dr); + + Interclasare(st, dr); + + } + +} + + + +int main() + +{ + + Citire(); + + MergeSort(1, n); + + fout << raspuns << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/7dc86148-847c-4584-9565-6d4caefeb649.cpp b/invCount/merge/7dc86148-847c-4584-9565-6d4caefeb649.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91041baa5e99a8ec5e0104a71cb488378c4d0014 --- /dev/null +++ b/invCount/merge/7dc86148-847c-4584-9565-6d4caefeb649.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n ,cnt; + +int a[100005]; + +int b[100005]; + +void intercl(int st,int m,int dr) + +{ + + int i , j , k ; + + i = st; + + j = m + 1; + + k = 0 ; + + while(i <= m && j <= dr) + + if(a[i] <= a[j]) + + b[++k] = a[i++]; + + else { + + cnt = (cnt + (m - i + 1)) % 9917; + + b[++k] = a[j++]; + + } + + + + + + while( i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for( i = st; i <= dr; i ++) + + a[i] = b[j++]; + +} + +void MSort(int st , int dr) + +{ + + int m ; + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + {swap(a[dr],a[st]); + + cnt++; + + cnt %= 9917; + + } + + } + + else + + { + + m = (st + dr) / 2; + + MSort(st,m); + + MSort(m + 1,dr); + + intercl(st,m,dr); + + } + + + + + +} + +int main() + +{ + + int i ; + + fin>>n; + + for(i = 1; i <= n ; i ++) + + fin>> a[i]; + + MSort(1,n); + + + + fout< +#include +using namespace std; + + + +int n,i; + +int v[100003]; + +int a[100003]; + + + +int interclasare(int st,int dr){ + + int mid=(st+dr)/2,i=st,j=mid+1,nr=0,s=0; + + + + while(i<=mid && j<=dr){ + + if(v[i]<=v[j]) + + a[++nr]=v[i++]; + + else + + a[++nr]=v[j++],s=s+mid-i+1; + + } + + + + while(i<=mid) + + a[++nr]=v[i++]; + + + + while(j<=dr) + + a[++nr]=v[j++]; + + + + for(i=1;i<=nr;i++) + + v[st+i-1]=a[i]; + + + + return s; + + + +} + + + +int mergesort(int st,int dr){ + + int mid=(st+dr)/2,s=0; + + + + if(st + +using namespace std; + +ifstream in ("inv.in"); +ofstream out ("inv.out"); + +const int N=100005, M=9917; + +int v[N], aux[N]; + +int mergeV (int v[], int st, int dr) +{ + int m=(st+dr)/2, i, j, k, r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr) + { + if(v[i]<=v[j]) + aux[k++]=v[i++]; + else + { + aux[k++]=v[j++]; + r+=m-i+1; + r%=M; + } + } + while(i<=m) + { + aux[k++]=v[i++]; + } + while(j<=dr) + aux[k++]=v[j++]; + for(k=st; k<=dr; k++) + v[k]=aux[k]; + return r; +} + +int ms(int v[], int st, int dr) +{ + int r=0; + if(st==dr) + { + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r%=M; + r+=ms(v, m+1, dr); + r%=M; + r+=mergeV(v, st, dr); + r%=M; + return r; +} + +int main() +{ + int n, i; + in>>n; + for(i=0; i>v[i]; + } + out< + +std::ifstream fin("inv.in"); +std::ofstream fout("inv.out"); + +int n, inversions, v[100001]; + +void merge(int v[], int left, int right) +{ + int mid = (left + right) / 2; + int i = left, j = mid + 1; + int aux[right - left + 1], index = 0; + while(i <= mid && j <= right) + if(v[i] <= v[j]) + aux[index++] = v[i++]; + else + { + aux[index++] = v[j++]; + inversions += (mid - i + 1); + inversions = inversions % 9917; + } + + while(i <= mid) + aux[index++] = v[i++]; + while(j <= right) + aux[index++] = v[j++]; + + for(i = 0; i < index; i++) + v[left + i] = aux[i]; + +} + +int mergeSort(int v[], int left, int right) +{ + if(left < right) + { + mergeSort(v, left, (left + right) / 2); + mergeSort(v, (left + right) / 2 + 1, right); + merge(v, left, right); + } + + return inversions; +} + + +int main() +{ + fin >> n; + for(int i = 0; i < n; i++) + fin >> v[i]; + fout << mergeSort(v, 0, n - 1); + + return 0; +} diff --git a/invCount/merge/82557e5d-96a4-4c65-b586-d2b5a4e5d731.cpp b/invCount/merge/82557e5d-96a4-4c65-b586-d2b5a4e5d731.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e2a8f75cedde2c3bd73eadd8f5f40fbbe953b7a --- /dev/null +++ b/invCount/merge/82557e5d-96a4-4c65-b586-d2b5a4e5d731.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int rasp=0,a[200005],aux[200005],sol=0,mod=9917,n; + +void interclaseaza(int pozst,int mij,int dr) + +{ + + int i=pozst; + + int j=mij+1; + + int nr=0,cnt=0,rasp=0; + + + + while(i<=mij&&j<=dr) + + { + + if(a[i]>a[j]) + + { + + nr++; + + aux[++cnt]=a[j]; + + j++; + + + + } + + else + + { + + rasp+=nr; + + aux[++cnt]=a[i]; + + i++; + + } + + } + + while(i<=mij) + + { + + rasp+=nr; + + aux[++cnt]=a[i]; + + i++; + + } + + while(j<=dr) + + { + + aux[++cnt]=a[j]; + + j++; + + } + + sol=(sol+rasp)%mod; + + for(int i=pozst;i<=dr;i++) + + { + + a[i]=aux[i-pozst+1]; + + } + +} + +void mergesort(int st,int dr) + +{ + + if(st>n; + +for(int i=1;i<=n;i++) + +{ + + f>>a[i]; + +} + +mergesort(1,n); + +g< + + + + +using namespace std; + + + +int nr_inversiuni; + + + +void sortare_interclasare(int *v, int *a, int *b, int n) { + + int i = 0, j = 0, mid = n / 2; + + for (int k = 0; k < n; k++) { + + if (i == mid) { + + v[k] = b[j]; + + j++; + + continue; + + } + + if (j == mid + n % 2) { + + v[k] = a[i]; + + i++; + + continue; + + } + + if (a[i] > b[j]) { + + v[k] = b[j]; + + nr_inversiuni = (nr_inversiuni + mid - i) % 9917; + + j++; + + } + + else { + + v[k] = a[i]; + + i++; + + } + + } + +} + + + +void merge_sort(int *v, int n) { + + if (n > 1) { + + int mid = n / 2, k = 0; + + int a[mid + 1], b[mid + 1]; + + for (int i = 0; i < mid; i++) + + a[i] = v[i]; + + for (int i = mid; i < n; i++) + + b[k++] = v[i]; + + merge_sort(a, mid); + + merge_sort(b, mid + n % 2); + + sortare_interclasare(v, a, b, n); + + } + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + int n, numar; + + scanf("%d", &n); + + int numere[n]; + + for (int i = 0; i < n; i++) { + + scanf("%d", &numar); + + numere[i] = numar; + + } + + merge_sort(numere, n); + + printf("%d ", nr_inversiuni); + + return 0; + +} diff --git a/invCount/merge/84760b8c-10b1-49a9-a37b-977e9005a70a.cpp b/invCount/merge/84760b8c-10b1-49a9-a37b-977e9005a70a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad14c2cefd609991436ce107262abf0b8ce4a56c --- /dev/null +++ b/invCount/merge/84760b8c-10b1-49a9-a37b-977e9005a70a.cpp @@ -0,0 +1,122 @@ +#include +#include + + +using namespace std; + + + +int nr ; + +const int N = 100005; + +const int M = 9917; + + + +void interclasare (int s[], int st, int m, int dr) + +{ + + int i,l, j, k = 0,aux[N]; + i = st; + + j = m+1; + + while ( i <= m && j <= dr) + + { + + if ( s[i] <= s[j]) + + aux[k++] = s[i++]; + + else + + { + + aux[k++] = s[j++]; + + nr += m-i+1; + + nr = nr%M; + + } + + } + + while (i <= m) + + aux[k++] = s[i++]; + + while ( j <= dr) + + aux[k++] = s[j++]; + + for (i = st,k = 0; i <= dr ; i++,k++) + + s[i] = aux[k]; + +} + + + +void MergeSort (int s[], int st, int dr) + +{ + + if ( st < dr) + + { + + int m = (st + dr) / 2; + + MergeSort (s, st, m); + + MergeSort (s, m+1, dr); + + interclasare (s, st, m, dr); + + } + +} + + + +int main() + +{ + + int n, s[N], i, j; + + + + ifstream in ("inv.in"); + + in >> n; + + + + for (i = 0; i < n; i++) + + in >> s[i]; + + in.close(); + + + + MergeSort (s, 0, n-1); + + + + ofstream out("inv.out"); + + out << nr%M; + + out.close(); + + + + return 0; + +} diff --git a/invCount/merge/85086578-1d32-44c2-961d-aa2b2b435a7d.cpp b/invCount/merge/85086578-1d32-44c2-961d-aa2b2b435a7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b510f5042f36be69114e90115adcfc25a0b0d38 --- /dev/null +++ b/invCount/merge/85086578-1d32-44c2-961d-aa2b2b435a7d.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +int aux[100001]; +int interclasare(int v[], int l, int m, int r, int &nrinv){ + + int i = l; + int j = m+1; + int h=0; + + while(i <= m && j <= r){ + if(v[i] <= v[j]){ + aux[h] = v[i]; + h++; + i++; + }else{ + nrinv += m-i+1; + nrinv = nrinv % 9917; + aux[h] = v[j]; + h++; + j++; + } + } + + while(i <= m){ + aux[h] = v[i]; + h++; + i++; + } + + + while(j <= r) { + aux[h] = v[j]; + h++; + j++; + } + + for(i = l; i <= r;i++) + v[i] = aux[i-l]; + +} + +void mergeSort(int v[], int l, int r, int &nrInv){ + + if(l < r){ + int m = (l+r)/2; + + mergeSort(v, l, m, nrInv); + mergeSort(v, m+1, r, nrInv); + + interclasare(v, l, m, r, nrInv); + } +} + +int main() { + ifstream f("inv.in"); + ofstream g("inv.out"); + + int n; + f >> n; + + int v[n]; + for(int i = 0; i < n; i++) + f >> v[i]; + + int nr = 0; + mergeSort(v, 0, n-1, nr); + g << nr; + return 0; +} diff --git a/invCount/merge/865e1066-bf67-4ac3-b1a0-66afe9c57ec7.cpp b/invCount/merge/865e1066-bf67-4ac3-b1a0-66afe9c57ec7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cabfe64f49c468ed65eaf65f783e74d3d7e701a --- /dev/null +++ b/invCount/merge/865e1066-bf67-4ac3-b1a0-66afe9c57ec7.cpp @@ -0,0 +1,107 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n; + +int a[100005],b[100005]; + +int ct; + + + +void inv(int s,int d) + +{ + + int mij=s+(d-s)/2,i,j,k; + + if(s==d)return ; + + if(s!=d) + + { + + inv(s,mij); + + inv(mij+1,d); + + + + i=s;j=mij+1;k=s-1; + + while(i<=mij&&j<=d) + + if(a[i]>a[j]) + + b[++k]=a[j++]; + + + + else + + { + + ct=(ct+j-mij-1)%9917; + + b[++k]=a[i++]; + + } + + while(i<=mij) + + b[++k]=a[i++],ct=(ct+d-mij)%9917; + + + + while(j<=d) + + b[++k]=a[j++]; + + + + for(i=s;i<=d;++i) + + a[i]=b[i]; + + } + + + + + +} + + + +int main() + +{ + + int i; + + fin>>n; + + for(i=1;i<=n;++i) + + fin>>a[i]; + + inv(1,n); + + fout< + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int aux[100000]; + +long long merge_v(int v[], int st, int dr){ + int m=(st+dr)/2, i, j, k; + long long r=0; + i=k=st; + j=m+1; + while(i<=m && j<=dr){ + if(v[i]<=v[j]){ + aux[k++]=v[i++]; + }else{ + aux[k++]=v[j++]; + r+=m-i+1; + } + } + while(i<=m){ aux[k++]=v[i++]; } + while(j<=dr){ aux[k++]=v[j++]; } + for(k=st; k<=dr; k++){ + v[k]=aux[k]; + } + return r; +} + +long long ms(int v[], int st, int dr){ + long long r=0; + if(st==dr){ + return 0; + } + int m=(st+dr)/2; + r+=ms(v, st, m); + r+=ms(v, m+1, dr); + r+=merge_v(v, st, dr); + return r; +} + +int main() +{ + int v[100000], n; + in>>n; + for(int i=0; i>v[i]; + } + out< +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int i,a[100005],aux[100005],n,k; + +void interclasare(int a[],int aux[],int p,int q,int r,int &inv) + +{ + + int i,j,k,c; + + i=p; + + j=q+1; + + k=p; + + c=0; + + while(i<=q&&j<=r) + + { + + if(a[i]<=a[j]) + + { + + aux[k]=a[i]; + + inv=(inv+c)%9917; + + i++; + + k++; + + } + + else + + { + + aux[k]=a[j]; + + c=j-q; + + j++; + + k++; + + } + + } + + while(i<=q) + + { + + aux[k]=a[i]; + + inv=(inv+c)%9917; + + i++; + + k++; + + } + + while(j<=r) + + { + + aux[k]=a[j]; + + j++; + + k++; + + } + + for(i=p;i<=r;i++){a[i]=aux[i];} + +} + +void mgsort(int a[],int aux[],int p,int r,int &inv) + +{ + + int q; + + if(p>n; + + for(i=1;i<=n;i++) + + f>>a[i]; + + mgsort(a,aux,1,n,k); + + g< +#include + + +using namespace std; + +long merge(long arr[], long left, long mid, long right) + +{ + + long count = 0; + + long i, j, k; + + + + long temp1 = mid - left + 1; + + long temp2 = right - mid; + + + + long* L = new long[temp1]; + + long* R = new long[temp2]; + + + + for (i = 0; i < temp1; i++) + + L[i] = arr[left + i]; + + + + for (j = 0; j < temp2; j++) + + R[j] = arr[mid + 1 + j]; + + + + + + + + k = left; + + i = 0; + + j = 0; + + + + while (i < temp1 && j < temp2) + + { + + if (L[i] <= R[j]) { + + arr[k] = L[i]; + + i++; + + } + + else { + + arr[k] = R[j]; + + j++; + + count += temp1 - i; + + } + + k++; + + } + + while (i < temp1) + + arr[k++] = L[i++]; + + while (j < temp2) + + arr[k++] = R[j++]; + + + + delete[] L; + + delete[] R; + + + + return count; + +} + +long mergeSort(long arr[], long left, long right) + +{ + + if (left == right) + + return 0; + + long nr_inv = 0; + + if (left < right) + + { + + long mij = (left + right) / 2; + + nr_inv += mergeSort(arr, left, mij); + + nr_inv += mergeSort(arr, mij + 1, right); + + nr_inv += merge(arr, left, mij, right); + + } + + return nr_inv; + +} + + + +int main() + +{ + + ifstream f("inv.in"); + + ofstream g("inv.out"); + + + + long n, S[100001]; + + f >> n; + + for (long i = 0; i < n; i++) + + f >> S[i]; + + long solutie = mergeSort(S, 0, n - 1); + + g << solutie % 9917; + + + + return 0; + +} diff --git a/invCount/merge/9074dfc6-4f77-4410-b4ff-795975e61e47.cpp b/invCount/merge/9074dfc6-4f77-4410-b4ff-795975e61e47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f23707ffe511c8a94a3dc13fd84c59bde9579919 --- /dev/null +++ b/invCount/merge/9074dfc6-4f77-4410-b4ff-795975e61e47.cpp @@ -0,0 +1,76 @@ +#include + + + +using namespace std; + +int rs = 0; +const int MAXN = 100010; +const int_fast64_t MODULO = 9917; +int n; +int arr[MAXN]; + +int_fast64_t merge(int left, int mid, int right, int arr[]) { + queue q1,q2; + for (int i = left; i <= mid; i++) q1.push(arr[i]); + for (int i = mid + 1; i <= right; i++) q2.push(arr[i]); + + int index = left; + int_fast64_t inv = 0; + int i = left; + while (!q1.empty() && !q2.empty()) { + if (q1.front() <= q2.front()) { + arr[index] = q1.front(); + q1.pop(); + + } else { + arr[index] = q2.front(); + q2.pop(); + inv += q1.size(); + } + index++; + } + + while (!q1.empty()) { + arr[index] = q1.front(); + q1.pop(); + index++; + } + + while (!q2.empty()) { + arr[index] = q2.front(); + q2.pop(); + index++; + } + return inv; +} + +int_fast64_t mergesort(int left, int right, int arr[]) { + if (left >= right) return 0; + + int mid = (left + right) / 2; + + int_fast64_t inv = mergesort(left, mid, arr); + inv += mergesort(mid + 1, right, arr); + + inv += merge(left, mid, right, arr); + + return inv; +} + +int main() { + + ifstream fin("inv.in"); + ofstream fout("inv.out"); + + ios_base::sync_with_stdio(0); + cin.tie(NULL); + + fin >> n; + for (int i = 0; i < n; i++) fin >> arr[i]; + + fout << mergesort(0, n - 1, arr) % MODULO; + + + return 0; +} diff --git a/invCount/merge/90979d15-5ee2-4848-807e-437e217a17ce.cpp b/invCount/merge/90979d15-5ee2-4848-807e-437e217a17ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef00b3c1692aa10a93d5918724840a81dd0d9513 --- /dev/null +++ b/invCount/merge/90979d15-5ee2-4848-807e-437e217a17ce.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream fin("inv.in"); +ofstream fout("inv.out"); + +int w[100001]; + +int Interclasare(int v[], int left, int right); +int MergeSort(int v[], int left, int right); + +int main() +{ + int n; + int i; + fin >> n; + int v[n]; + for (i = 0; i < n; i++) + fin >> v[i]; + fout << MergeSort(v, 0, n - 1) % 9917; + return 0; +} + +int MergeSort(int v[], int left, int right) +{ + if (left == right) + { + return 0; + } + int m = (left + right) / 2; + int inversiune = 0; + inversiune += MergeSort(v, left, m); + inversiune += MergeSort(v, m + 1, right); + inversiune += Interclasare(v, left, right); + return inversiune % 9917; +} +int Interclasare(int v[], int left, int right) +{ + int i, j, k; + int m = (left + right) / 2; + i = left; + k = left; + j = m + 1; + int inversiune = 0; + while (i <= m && j <= right) + { + if (v[i] <= v[j]) + { + w[k] = v[i]; + i++; + k++; + } + else + { + w[k] = v[j]; + inversiune += m - i + 1; + j++; + k++; + } + } + while (i <= m) + { + w[k] = v[i]; + k++; + i++; + } + while (j <= right) + { + w[k] = v[j]; + k++; + j++; + } + for (k = left; k <= right; k++) + { + v[k] = w[k]; + } + return inversiune % 9917; +} diff --git a/invCount/merge/9175d44d-0fcd-417d-87e0-e8f7216a228b.cpp b/invCount/merge/9175d44d-0fcd-417d-87e0-e8f7216a228b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4728d1a6c58ce1595ad43640d2da4a44a50397fc --- /dev/null +++ b/invCount/merge/9175d44d-0fcd-417d-87e0-e8f7216a228b.cpp @@ -0,0 +1,63 @@ +#include +#include +using namespace std; +ifstream fin("inv.in"); ofstream fout("inv.out"); + +int merge(int v[] , int left , int mid , int right) +{ + int i = left , j = mid , k = left , temp[200200]; + int count = 0; + while (i<=mid-1 && j<=right) + { + if(v[i] <= v[j]) + { + temp[k] = v[i]; + i++; + k++; + } + else + { + temp[k] = v[j]; + j++; k++; + count += (mid - i); + } + } + while(i<=mid-1) + { + temp[k] = v[i]; + i++; k++; + } + while(j<=right) + { + temp[k] = v[j]; + j++; k++; + } + for(i=left ; i<=right ; i++) + v[i] = temp[i]; + return count; +} + +int mergesort(int v[], int left, int right) +{ + int mid, count = 0; + + if(right > left) + { + mid = (right + left)/2; + count = mergesort(v , left , mid) % 9917; + count += mergesort(v , mid+1 , right) % 9917; + count += merge(v , left , mid+1 , right) % 9917; + } + return count; +} + +int main() +{ + int n , v[200200]; + fin >> n; + int temp[n]; + for(int i=0 ; i> v[i]; + int ans = mergesort(v , 0 , n-1) % 9917; + fout << ans; +} diff --git a/invCount/merge/92613f03-2ad9-476c-b4af-d3af4730bcba.cpp b/invCount/merge/92613f03-2ad9-476c-b4af-d3af4730bcba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6379a6b3f3d9be57b5dadbd07f583548ed28818 --- /dev/null +++ b/invCount/merge/92613f03-2ad9-476c-b4af-d3af4730bcba.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +ifstream in("inv.in"); +ofstream out("inv.out"); + +int v[100001],aux[100001]; + +int mmerge(int st,int dr,int v[]) +{ + int i,j,k,m=(st+dr)/2,c=0; + i=k=st; + j=m+1; + while(i<=m&&j<=dr) + { + if(v[i]<=v[j]) + aux[k++]=v[i++]; + else + aux[k++]=v[j++],c+=m-i+1,c%=9917; + } + while(i<=m) + aux[k++]=v[i++]; + while(j<=dr) + aux[k++]=v[j++]; + for(k=st; k<=dr; k++) + v[k]=aux[k]; + return c; +} + +int msort(int st,int dr,int v[]) +{ + int c = 0; + if(st==dr) + return 0; + int m=(st+dr)/2; + c += msort(st,m,v); + c += msort(m+1,dr,v); + c += mmerge(st,dr,v); + c %= 9917; + return c; +} + +int main() +{ + int n,c=0; + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + + out< +#include + +using namespace std; + +int inv = 0; + +int w[100001]; + +void interclasare(int v[], int st, int dr) { + int m, i, j, k; + m = (st + dr) / 2; + k = 0; + i = st; + j = m + 1; + while (i <= m && j <= dr) { + if (v[i] <= v[j]) w[++k] = v[i++]; + else { + w[++k] = v[j++]; + inv = inv + m - i + 1; + inv = inv % 9917; + } + } + while (i <= m) w[++k] = v[i++]; + while (j <= dr) w[++k] = v[j++]; + for (i = st, j = 1; i <= dr; i++, j++) v[i] = w[j]; +} + + +void mergeSort(int v[], int st, int dr) { + if (st < dr) { + int m = (st + dr) / 2; + mergeSort(v, st, m); + mergeSort(v, m + 1, dr); + interclasare(v, st, dr); + } +} + +int main() { + ifstream fin("inv.in"); + ofstream fout("inv.out"); + int v[100001]; + int n, i; + fin >> n; + for (i = 1; i <= n; ++i) + fin >> v[i]; + mergeSort(v, 1, n); + fout << inv % 9917; + return 0; +} diff --git a/invCount/merge/9a40e770-1d75-43c2-9cdb-c09cd1b8c420.cpp b/invCount/merge/9a40e770-1d75-43c2-9cdb-c09cd1b8c420.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11fa87b29fb9d25dde04a9cf5e7305aa873d0de3 --- /dev/null +++ b/invCount/merge/9a40e770-1d75-43c2-9cdb-c09cd1b8c420.cpp @@ -0,0 +1,165 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + +void interclasare(int arr[], int s, int d); + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + + + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/9be7ac84-8a44-4e91-994d-1b09ec41d2e6.cpp b/invCount/merge/9be7ac84-8a44-4e91-994d-1b09ec41d2e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d660a3ece29d85cc91e4e882fd52a7dae1591065 --- /dev/null +++ b/invCount/merge/9be7ac84-8a44-4e91-994d-1b09ec41d2e6.cpp @@ -0,0 +1,113 @@ +#include +#include + + +const int MAXN = 1e5 + 1,MOD = 9917; + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n,v[MAXN],a[MAXN],b[MAXN]; + +long long inv = 0; + + + +void combina(int l,int r){ + + int mid = (l + r) / 2; + + for(int i = l; i <= mid; i++) + + a[i] = v[i]; + + for(int i = mid + 1; i <= r; i++) + + b[i] = v[i]; + + int i = l; + + int j = mid + 1; + + int index = l; + + while(i <= mid && j <= r){ + + if(a[i] <= b[j]){ + + v[index++] = a[i]; + + i++; + + }else{ + + inv += 1ll * (mid - i + 1); + + inv %= MOD; + + v[index++] = b[j]; + + j++; + + } + + } + + while(i <= mid) + + v[index++] = a[i++]; + + while(j <= mid) + + v[index++] = b[j++]; + + + +} + +void divide(int l = 1,int r = n){ + + int mid = (l + r) / 2; + + if(l == r) + + return; + + divide(l,mid); + + divide(mid + 1,r); + + combina(l,r); + +} + + + + + +int main() + +{ + + in>>n; + + for(int i = 1; i <= n; i++) + + in>>v[i]; + + divide(); + + out< +#include +using namespace std; + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int v[100001],w[100001],n,nr=0; + +void inversiune(int v[],int st,int dr){ + + if(st>n; + + for(int i=1;i<=n;i++) + + in>>v[i]; + + inversiune(v,1,n); + + out< + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int n,a[100001],b[100001]; + +int inv=0; + +void Divide(int left, int right) + +{ + + if(left==right) return; + + + + int mid=(left+right)/2; + + Divide(left,mid); + + Divide(mid+1,right); + + + + int x=left,y=mid+1,dim=left-1; + + while(x<=mid && y<=right) + + { + + if(a[x]<=a[y]) {dim++;b[dim]=a[x];x++;} + + else {dim++;b[dim]=a[y];inv=(inv+(mid-x+1)) % 9917;y++;} + + } + + while(y<=right) {dim++;b[dim]=a[y];y++;} + + while(x<=mid) {dim++;b[dim]=a[x];x++;} + + + + for(int i=left;i<=right;i++) + + a[i]=b[i]; + +} + +int main() + +{ + + fin>>n; + + for(int i=1; i<=n; i++) + + fin>>a[i]; + + + + Divide(1,n); + +fout< +#include + +using namespace std; +int nr = 0; + +void interclasare(int v[], int st, int dr) { + int mij = (st + dr) / 2; + int i = st; + int j = mij + 1; + int k = 0; + int n = dr - st + 1; + int c[n]; + while (i <= mij && j <= dr) { + if (v[i] <= v[j]) { + c[k] = v[i]; + k++; + i++; + } else { + nr += mij - i + 1; + nr = nr % 9917; + c[k] = v[j]; + k++; + j++; + } + } + while (i <= mij) { + c[k] = v[i]; + k++; + i++; + } + while (j <= dr) { + c[k] = v[j]; + k++; + j++; + + } + for (i = 0; i < k; i++) { + v[st + i] = c[i]; + } +} + +void mergeSort(int v[], int st, int dr) { + int mij; + if (st < dr) { + mij = (st + dr) / 2; + mergeSort(v, st, mij); + mergeSort(v, mij + 1, dr); + interclasare(v, st, dr); + } +} + +int main() { + ifstream f("inv.in"); + ofstream g("inv.out"); + int n, v[100001]; + f >> n; + for (int i = 0; i < n; ++i) + f >> v[i]; + f.close(); + mergeSort(v, 0, n - 1); + g << nr; + g.close(); + return 0; +} diff --git a/invCount/merge/a793ead6-e453-4dc9-a157-1fb299b97fb6.cpp b/invCount/merge/a793ead6-e453-4dc9-a157-1fb299b97fb6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc4def2e9265da9ab36401a622170499e4c7d64e --- /dev/null +++ b/invCount/merge/a793ead6-e453-4dc9-a157-1fb299b97fb6.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + + + +int a[100001], n, b[100001]; + + + +long long merge(int a[], int b[], int low, int mid, int high); + + + +long long mergesort(int a[], int b[], int low, int high) { + + long long mid, cnt = 0; + + if (low < high) { + + mid = (low + high)/2; + + cnt = (cnt + mergesort(a, b, low, mid) + mergesort(a, b, mid + 1, high) + merge(a, b, low, mid, high)) % 9917; + + } + + return cnt; + +} + +long long merge(int a[], int b[], int low, int mid, int high) { + + int h, i, j, k; + + long long cnt = 0; + + h = low; + + i = low; + + j = mid + 1; + + + + while ((h <= mid) && (j <= high)) { + + if (a[h] <= a[j]) { + + b[i] = a[h]; + + h++; + + } + + else { + + b[i] = a[j]; + + j++; + + cnt = (cnt + mid - h + 1) % 9917; + + } + + i++; + + } + + if (h > mid) { + + for (k = j; k <= high; k++) { + + b[i] = a[k]; + + i++; + + } + + } + + else { + + for (k = h; k <= mid; k++) { + + b[i] = a[k]; + + i++; + + } + + } + + for (k = low; k <= high; k++) { + + a[k] = b[k]; + + } + + return cnt; + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + + + scanf("%d", &n); + + for(int i = 0; i < n; i++) { + + scanf("%d", &a[i]); + + } + + + + printf("%lld", mergesort(a, b, 0, n - 1)); + + return 0; + +} diff --git a/invCount/merge/ab8db424-6852-4173-bf42-b032070a08b3.cpp b/invCount/merge/ab8db424-6852-4173-bf42-b032070a08b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cca42315cdb6b7335c2142578356232ae68e4f4 --- /dev/null +++ b/invCount/merge/ab8db424-6852-4173-bf42-b032070a08b3.cpp @@ -0,0 +1,149 @@ +#include +#include +using namespace std; + +int nr; + +void merges(long long int v[],int l,int m,int r) + +{ + + int i, j, k; + + int n1 = m - l + 1; + + int n2 = r - m; + + int L[n1], R[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = v[l + i]; + + for (j = 0; j < n2; j++) + + R[j] = v[m + 1+ j]; + + i = 0; + + j = 0; + + k = l; + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) + + { + + v[k] = L[i]; + + i++; + + } + + else + + {nr += n1-i; + + nr = nr % 9917; + + v[k] = R[j]; + + j++; + + } + + k++; + + } + + while (i < n1) + + { + + v[k] = L[i]; + + i++; + + k++; + + } + + + + while (j < n2) + + { + + v[k] = R[j]; + + j++; + + k++; + + } + + + +} + + + + + +void mergesort(long long int v[],int l,int r) + +{ + + if(l> n; + + for (i=1;i<=n;i++) + + f>>v[i]; + +mergesort(v,1,n); + + + + + +g< +#include +using namespace std; + +ifstream f ("inv.in"); + +ofstream g ("inv.out"); + +long long v[100003]; + +int nr=0,n; + +void merge(long long *v,int st, int dr,int mijloc) + +{ + + int temp[dr-st+1]; + + int i=st, j = mijloc+1,k=0; + + while(i<= mijloc and j<=dr) + + { + + if(v[i]<=v[j]) + + { + + temp[k++] = v[i]; + + i++; + + } + + else + + { + + nr += mijloc - i + 1; + + nr = nr % 9917; + + temp[k++] = v[j]; + + j++; + + } + + + + } + + while(i<=mijloc) + + { + + temp[k++] = v[i]; + + i++; + + } + + while(j<=dr) + + { + + temp[k++] = v[j]; + + j++; + + } + + for(i=st; i<=dr; i++) + + v[i] = temp[i-st]; + + + +} + +void mergeSort(long long v[],int st,int dr) + +{ + + if(st>n; + + for(int i=0; i>v[i]; + + + + mergeSort(v,0,n-1); + + + + + + g< + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +vectorv; + +int n; + +long long ans; + +void Citire() + +{ + + int i,x; + + fin>>n; + + v.push_back(0); + + for(i=1;i<=n;++i) + + { + + fin>>x; + + v.push_back(x); + + } + +} + +void Divide(int left,int right) + +{ + + if(left a; + + int pos1=left,pos2=mid+1; + + while(pos1<=mid&&pos2<=right) + + { + + if(v[pos1]<=v[pos2]) + + { + + ans=(ans+pos2-mid-1)%9917; + + a.push_back(v[pos1]); + + pos1++; + + } + + else { + + a.push_back(v[pos2]); + + pos2++; + + } + + } + + while(pos1<=mid) + + { + + ans=(ans+pos2-mid-1)%9917; + + a.push_back(v[pos1++]); + + } + + while(pos2<=right)a.push_back(v[pos2++]); + + for(int i=left;i<=right;++i)v[i]=a[i-left]; + + } + +} + +int main() + +{ + + Citire(); + + Divide(1,n); + + fout< + + + + +using namespace std; + +ifstream r("inv.in"); + +ofstream w("inv.out"); + +int v[500003], sol=0; + +const int mod=9917; + +void unire(int a, int mij, int b){ + + int n1 = mij - a + 1, n2 = b - mij, g[n1], h[n2], nr=0, rasp=0; + + for(int i=0;i>n; + + for(int i=0; i>v[i]; + + } + + mergesort(0, n-1); + + w< + +using namespace std; +ifstream fin("inv.in"); +ofstream fout("inv.out"); +int cnt; +int v[100001]; +void dei(int st,int dr) +{ + if(st==dr) + { + return; + } + int mij=(st+dr)/2; + dei(st,mij); + dei(mij+1,dr); + int B[dr-st+2]; + int i=st,j=mij+1,pas=0; + while(i<=mij && j<=dr) + { + if(v[i]>v[j]) + { + cnt+=(mij-i+1); + cnt%=9917; + B[pas++]=v[j]; + j++; + } + else + { + B[pas++]=v[i]; + i++; + } + } + while(i<=mij) {B[pas++]=v[i];i++;}; + while(j<=dr) {B[pas++]=v[j];j++;}; + for(i=st,pas=0;i<=dr;i++,pas++) v[i]=B[pas]; +} +int main() +{ + int n,i; + fin>>n; + for(i=1;i<=n;i++) fin>>v[i]; + dei(1,n); + fout< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int v1[100001], v2[100001], nr; + + + +void intercl(int left, int mid, int right) + +{ + + int i = left, j = mid + 1, k = 0; + + + + while(i <= mid and j <= right) + + { + + if(v1[i] <= v1[j]) + + v2[k ++] = v1[i ++]; + + else + + { + + nr += mid - i + 1; + + nr = nr % 9917; + + v2[k ++] = v1[j ++]; + + } + + } + + + + while(i <= mid) v2[k ++] = v1[i ++]; + + while(j <= right) v2[k ++] = v1[j ++]; + + + + for(k = left; k <= right; k ++) + + v1[k] = v2[k - left]; + +} + + + +void mergesort(int left, int right) + +{ + + int mid; + + + + if(left < right) + + { + + mid = (left + right) / 2; + + + + mergesort(left, mid); + + mergesort(mid + 1, right); + + + + intercl(left, mid, right); + + } + +} + + + +int main() + +{ + + int n; + + f >> n; + + for(int i = 0; i < n; i ++) + + f >> v1[i]; + + + + mergesort(0, n - 1); + + g << nr; + + return 0; + +} diff --git a/invCount/merge/b8082a2f-66b3-4d9c-986a-030ab818f01a.cpp b/invCount/merge/b8082a2f-66b3-4d9c-986a-030ab818f01a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c8ed7943d1e18f629a9de14076ccfa642198358 --- /dev/null +++ b/invCount/merge/b8082a2f-66b3-4d9c-986a-030ab818f01a.cpp @@ -0,0 +1,143 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + + + +long a[100001]; + +int n,nrinv; + +long rez; + + + +void Merge(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + Merge(a,st,mij); + + Merge(a,mij+1,dr); + + + + int k=0; + + int i=st; + + int j=mij+1; + + int b[100001]; + + int nrinv=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + else + + { + + b[k]=a[j]; + + k++; + + j++; + + nrinv++; + + } + + + + while(i<=mij) + + { + + b[k]=a[i]; + + k++; + + i++; + + rez+=nrinv; + + } + + while(j<=dr) + + { + + b[k]=a[j]; + + k++; + + j++; + + } + + for(i=st; i<=dr; i++) + + { + + a[i]=b[i-st]; + + } + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>a[i]; + + } + + Merge(a,0,n-1); + + g< +using namespace std; + +int merge(int v[], int temp[], int st, int mij, int dr){ + int i = st; + int j = mij; + int k = st; + int count = 0; + while((i <= mij - 1) && (j <= dr)){ + if(v[i] <= v[j]) + temp[k++] = v[i++]; + else { + temp[k++] = v[j++]; + count += (mij - i); + count = count % 9917; + } + } + while(i <= mij - 1) + temp[k++] = v[i++]; + while (j <= dr) + temp[k++] = v[j++]; + for(int l = st; l <= dr; l++) + v[l] = temp[l]; + return count; +} + +int nr(int v[], int temp[], int st, int dr){ + int mij = 0; + int count = 0; + if (dr > st){ + mij = (dr+st)/2; + count += nr(v, temp, st, mij); + count += nr(v, temp, mij + 1, dr); + count += merge(v, temp, st, mij + 1, dr); + } + return count; +} + +int mergesort(int v[], int n) { + int temp[100001]; + return nr(v, temp, 0, n-1); +} + + +int main() { + freopen("inv.in", "r", stdin); + freopen("inv.out", "w", stdout); + ios::sync_with_stdio(0); + cin.tie(0); + int n; + int S[100001]; + cin >> n; + for (int i = 0; i < n; i++) + cin >> S[i]; + int raspuns = mergesort(S,n); + cout << raspuns % 9917; + return 0; +} diff --git a/invCount/merge/b9df18cf-6be3-4453-a1b0-1e2834a97c77.cpp b/invCount/merge/b9df18cf-6be3-4453-a1b0-1e2834a97c77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..373030d05cc83a767a4c5769b000c3fe8d251087 --- /dev/null +++ b/invCount/merge/b9df18cf-6be3-4453-a1b0-1e2834a97c77.cpp @@ -0,0 +1,118 @@ +#include + + + + +using namespace std; + + + +int nr_inversiuni; + + + +void sortare_interclasare(int *v, int *a, int *b, int n) { + + int i = 0, j = 0, mid = n / 2; + + for (int k = 0; k < n; k++) { + + if (i == mid) { + + v[k] = b[j]; + + j++; + + continue; + + } + + if (j == mid + n % 2) { + + v[k] = a[i]; + + i++; + + continue; + + } + + if (a[i] > b[j]) { + + v[k] = b[j]; + + nr_inversiuni = (nr_inversiuni + mid - i) % 9917; + + j++; + + } + + else { + + v[k] = a[i]; + + i++; + + } + + } + +} + + + +void merge_sort(int *v, int n) { + + if (n > 1) { + + int mid = n / 2, k = 0; + + int a[mid + 1], b[mid + 1]; + + for (int i = 0; i < mid; i++) + + a[i] = v[i]; + + for (int i = mid; i < n; i++) + + b[k++] = v[i]; + + merge_sort(a, mid); + + merge_sort(b, mid + n % 2); + + sortare_interclasare(v, a, b, n); + + } + +} + + + +int main() { + + freopen("inv.in", "r", stdin); + + freopen("inv.out", "w", stdout); + + int n, numar; + + scanf("%d", &n); + + int numere[n]; + + for (int i = 0; i < n; i++) { + + scanf("%d", &numar); + + numere[i] = numar; + + } + + merge_sort(numere, n); + + printf("%d ", nr_inversiuni); + + return 0; + +} diff --git a/invCount/merge/bc327b53-192e-4d27-b6b8-36ea70b77b2f.cpp b/invCount/merge/bc327b53-192e-4d27-b6b8-36ea70b77b2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe4adffa5607a6b31390d3de47f233985f5ebbb2 --- /dev/null +++ b/invCount/merge/bc327b53-192e-4d27-b6b8-36ea70b77b2f.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +vector v; + + + + + + + +int interclasare(int left1, int right1, int left2, int right2) + +{ + + int nr_inversiuni = 0; + + vector interclasat; + + int i=left1, j= left2; + + while(i<= right1 && j<= right2) + + { + + if(v[i] > v[j]) + + { + + interclasat.push_back(v[j]); + + nr_inversiuni += right1 - i +1; + + j++; + + } + + else + + { + + interclasat.push_back(v[i]); + + i++; + + } + + } + + while(i<=right1) + + { + + interclasat.push_back(v[i]); + + i++; + + } + + while(j<=right2) + + { + + interclasat.push_back(v[j]); + + j++; + + } + + int k = left1; + + for(i=0; i>n; + + int i; + + for(i=1; i<=n; i++) + + { + + int x; + + fin>>x; + + v.push_back(x); + + } + + fout< +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n, a[100100], aux[100100], vf; + + + +int rec(int st, int dr) { + + if (st == dr) + + return 0; + + + + int mid = (st + dr) / 2; + + int cnt = rec(st, mid) + rec(mid + 1, dr); + + cnt %= 9917; + + + + int p1 = st; + + int p2 = mid + 1; + + + + vf = 0; + + for (int i = st; i <= dr; i++) { + + if (p1 > mid) { + + aux[++vf] = a[p2++]; + + if (a[p1] > a[dr]) { + + cnt += dr - mid; + + cnt %= 9917; + + } + + continue; + + } + + if (p2 > dr) { + + aux[++vf] = a[p1++]; + + continue; + + } + + if (a[p1] <= a[p2]) + + aux[++vf] = a[p1++]; + + else { + + aux[++vf] = a[p2++]; + + cnt += mid - p1 + 1; + + cnt %= 9917; + + } + + } + + for (int i = st; i <= dr; i++) + + a[i] = aux[i - st + 1]; + + + + return cnt; + +} + + + +int main() { + + in >> n; + + for (int i = 1; i <= n; i++) + + in >> a[i]; + + + + out << rec(1, n); + + + + return 0; + +} diff --git a/invCount/merge/c35c0576-570e-4c23-93af-ed726a8cfc1e.cpp b/invCount/merge/c35c0576-570e-4c23-93af-ed726a8cfc1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5d0be57028aa93f507070f73b701548e75494bb --- /dev/null +++ b/invCount/merge/c35c0576-570e-4c23-93af-ed726a8cfc1e.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +ifstream cin("inv.in"); + +ofstream cout("inv.out"); + +int n,v[100005],aux[100005]; + +long long cnt; + +void mergesort(int s,int d){ + + if(s==d) return; + + int m=(s+d)/2; + + mergesort(s,m); + + mergesort(m+1,d); + + int i=s,j=m+1,k=0; + + while(i<=m && j<=d) + + if(v[i]<=v[j]) aux[++k]=v[i++]; + + else { + + aux[++k]=v[j++]; + + cnt+=m-i+1; + + } + + while(i<=m) aux[++k]=v[i++]; + + while(j<=d) aux[++k]=v[j++]; + + for(i=s,k=1;i<=d;i++,k++) + + v[i]=aux[k]; + +} + +int main(){ + + cin>>n; + + for(int i=1;i<=n;i++) + + cin>>v[i]; + + mergesort(1,n); + + cout< +#include + + + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +int nr=0,n,S[100005]; + + + + + + + +void interclasare(int arr[], int s, int d) + +{ + + int mijl = (s + d) / 2; + + + + int i = s; + + int j = mijl + 1; + + int k = 0; + + + + + + int size = d - s + 1; + + int c[size]; + + + + while (i <= mijl && j <= d) { + + if (arr[i] <= arr[j]) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + else { + + + + nr += mijl - i + 1; + + nr = nr % 9917; + + c[k] = arr[j]; + + k++; + + j++; + + } + + } + + while (i <= mijl) { + + c[k] = arr[i]; + + k++; + + i++; + + } + + while (j <= d) { + + c[k] = arr[j]; + + k++; + + j++; + + } + + for (i = 0; i < k; i++) { + + arr[s+i] = c[i]; + + } + + + +} + + + +void merge_sort(int arr[], int s, int d) + +{ + + int mijl; + + if (s < d){ + + + + mijl = (s+d) / 2; + + merge_sort(arr,s,mijl); + + merge_sort(arr,mijl+1,d); + + + + interclasare(arr,s,d); + + } + +} + + + +int main() + +{ + + f>>n; + + for(int i=0; i>S[i]; + + } + + f.close(); + + merge_sort(S, 0, n-1); + + g << nr % 9917; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/c39a6aac-4775-440e-8aeb-e7ac38f48711.cpp b/invCount/merge/c39a6aac-4775-440e-8aeb-e7ac38f48711.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9861b0e71a9acee880fc65611ebfd8208c2480ce --- /dev/null +++ b/invCount/merge/c39a6aac-4775-440e-8aeb-e7ac38f48711.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + + + +vector nums; + +int temp[100000]; + + + +long long merge(vector &arr, int left, int right) { + + int mid = (right + left) / 2; + + int i = left, j = mid + 1, k = left; + + long long inv_count = 0; + + + + while(i <= mid && j <= right) { + + if(arr[i] <= arr[j]) { + + temp[k++] = arr[i++]; + + } else { + + temp[k++] = arr[j++]; + + inv_count += (mid - i + 1) % 9917; + + } + + } + + + + while(i <= mid) + + temp[k++] = arr[i++]; + + + + while(j <= right) + + temp[k++] = arr[j++]; + + + + for(i = left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count % 9917; + +} + + + +long long mergeSort(vector &arr, int left, int right) { + + long long inv_count = 0; + + int mid; + + + + if(right > left) { + + mid = (right + left) / 2; + + + + inv_count += mergeSort(arr, left, mid); + + inv_count += mergeSort(arr, mid + 1, right); + + + + inv_count += merge(arr, left, right); + + } + + + + return inv_count % 9917; + +} + + + +int main() { + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + + + int n; + + + + fin >> n; + + for(int i = 0; i < n; i++) { + + int x; + + fin >> x; + + nums.push_back(x); + + } + + + + fout << mergeSort(nums, 0, n - 1); + +} diff --git a/invCount/merge/cc22e03d-de31-4cc1-ab6f-03a60a1b6464.cpp b/invCount/merge/cc22e03d-de31-4cc1-ab6f-03a60a1b6464.cpp new file mode 100644 index 0000000000000000000000000000000000000000..694da7e4aab529a1ab9c3a89ee9afe5c2ea8d159 --- /dev/null +++ b/invCount/merge/cc22e03d-de31-4cc1-ab6f-03a60a1b6464.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917 ; + +int n ,v[100005] , tmp[100005]; + +long long sol = 0 ; + + + +void MergeSort(int v[] , int st , int dr){ + + if( st < dr ){ + + int m = (st + dr) / 2 ; + + MergeSort( v , m+1 , dr ) ; + + MergeSort( v , st , m ) ; + + + + int i = st , j = m + 1 , k = 0 ; + + long long nr = 0 , rasp = 0 ; + + while( i<=m && j<=dr ) + + if( v[i] > v[j] ){ + + tmp[++k] = v[j++] ; + + nr++ ; + + } + + else{ + + tmp[++k] = v[i++] ; + + rasp+=nr ; + + } + + while(i <= m){ + + rasp+=nr ; + + tmp[++k] = v[i++] ; + + } + + while(j <= dr) + + tmp[++k] = v[j++] ; + + + + sol=(sol+rasp)%MOD ; + + + + for(i = st , j = 1 ; i <= dr ; i ++ , j ++) + + v[i] = tmp[j]; + + } + +} + +int main() + +{ + +f>>n; + +for(int i=1 ; i<=n ; i++){ + + f>>v[i] ; + +} + +MergeSort( v , 1 , n ) ; + + + +g< +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +const int N = 100001; + +int n, v[N]; + +long long inv; + +void intercls (int v[], int st, int mij, int dr){ + + int i = st, j = mij + 1, k = 0; + + int v2[N]; + + while(i <= mij && j <= dr){ + + if(v[i] <= v[j]) { + + v2[k ++] = v[i ++]; + + } + + else { + + v2[k ++] = v[j ++]; + + inv=(inv + mij-i+1)%9917; + + } + + } + + while(i <= mij) + + v2[k ++] = v[i ++]; + + while(j <= dr) + + v2[k ++] = v[j ++]; + + int c=0; + + for(int i=st; i<=dr; i++) + + v[i]=v2[c++]; + +} + +void mergesort (int v[], int st, int dr){ + + if(st < dr){ + + int mij = (st + dr) / 2; + + mergesort (v, st, mij); + + mergesort (v, mij + 1, dr); + + intercls (v, st, mij, dr); + + } + +} + +int main() + +{ + + f >> n; + + for(int i = 1; i <= n; i ++) + + f >> v[i]; + + mergesort (v, 1, n); + + g << inv; + + return 0; + +} diff --git a/invCount/merge/cd5e64cd-918c-4a1c-b257-bda8933bd574.cpp b/invCount/merge/cd5e64cd-918c-4a1c-b257-bda8933bd574.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7999ff61cd39269f225aa91789481710657ecf --- /dev/null +++ b/invCount/merge/cd5e64cd-918c-4a1c-b257-bda8933bd574.cpp @@ -0,0 +1,139 @@ +#include +#include +using namespace std; + + + +int v[100001]; + +int mergeSort(int l,int r) + +{ + + int i,j,k,mij,counter,t[100001]; + + mij=(l+r)/2; + + i=l; + + j=mij+1; + + k=0; + + counter=0; + + while(i<=mij && j<=r) + + { + + if(v[i]<=v[j]) + + { + + t[k]=v[i++]; + + } + + else + + { + + t[k]=v[j++]; + + counter+=mij-i+1; + + counter%=9917; + + } + + k++; + + } + + while(i<=mij) + + { + + t[k]=v[i++]; + + k++; + + } + + while(j<=r) + + { + + t[k]=v[j++]; + + k++; + + } + + for(i=l;i<=r;i++) + + { + + v[i]=t[i-l]; + + } + + return counter; + +} + +int inversiuni(int st, int dr) + +{ + + if (st>=dr) + + { + + return 0; + + } + + int mij,x,y,z; + + mij=(st+dr)/2; + + x=inversiuni(st,mij); + + y=inversiuni(mij+1,dr); + + z=x+y+mergeSort(st,dr); + + return z; + +} + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n,i,counter=0; + + fin>>n; + + for(i=1;i<=n;i++) + + { + + fin>>v[i]; + + } + + fout< +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int MOD = 9917; + +int N, S[100001], T[100001], nrInv; + + + +void Interclasare(int p, int u, int m) + +{ + + int i = p, j = m + 1, k = 0; + + while(i <= m && j <= u) + + if(S[i] <= S[j]) + + T[++k] = S[i++]; + + else + + { + + T[++k] = S[j++]; + + nrInv = (nrInv + m - i + 1) % MOD; + + } + + while(i <= m) + + T[++k] = S[i++]; + + while(j <= u) + + T[++k] = S[j++]; + + for(int i = p, k = 1; i <= u; i++, k++) + + S[i] = T[k]; + +} + + + +void Mergesort(int p, int u) + +{ + + if(p < u) + + { + + int m = p + (u - p) / 2; + + Mergesort(p, m); + + Mergesort(m + 1, u); + + Interclasare(p, u, m); + + } + +} + + + +int main() + +{ + + f>>N; + + for(int i=1;i<=N;i++) + + f>>S[i]; + + Mergesort(1,N); + + g< + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int a[100005]; + +long long inv; + + + +void Interclasare(int s,int mij,int d) + +{ int b[100005],i,j,k=0; + + i=s; j=mij+1; + + while(i<=mij && j<=d) + + if(a[i]<=a[j]) b[++k]=a[i++]; + + else {b[++k]=a[j++]; inv += (mij-i+1)%9917;} + + while(i<=mij) b[++k]=a[i++]; + + while(j<=d) b[++k]=a[j++]; + + + + for(i=s,j=1;j<=k;i++,j++) + + a[i]=b[j]; + +} + + + +void MS(int s,int d) + +{ if(s>n; + + for(i=1;i<=n;i++) + + fin>>a[i]; + + MS(1,n); + + fout<< inv%9917; + + return 0; + +} diff --git a/invCount/merge/cfeb7fc0-481d-447d-afac-639488130142.cpp b/invCount/merge/cfeb7fc0-481d-447d-afac-639488130142.cpp new file mode 100644 index 0000000000000000000000000000000000000000..996faf8ef94efc66d372445508ccf9d31ce16508 --- /dev/null +++ b/invCount/merge/cfeb7fc0-481d-447d-afac-639488130142.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; +ifstream f("inv.in"); +ofstream g("inv.out"); +int n,a[100005],sol,temp[100005]; +const int MOD=9917; +void interclasare(int st,int dr) +{ + int k=0; + int i=st; + int mid=(st+dr)/2; + int j=mid+1; + while(i<=mid && j<=dr) + { + if(a[i]<=a[j]) + temp[++k]=a[i++]; + else + { + sol+=(mid-i+1); + sol%=MOD; + temp[++k]=a[j++]; + } + } + while(i<=mid) + temp[++k]=a[i++]; + while(j<=dr) + temp[++k]=a[j++]; + for(i=st;i<=dr;i++) + a[i]=temp[i-st+1]; +} +void divide(int st,int dr) +{ + if(st>n; + for(int i=1;i<=n;i++) + f>>a[i]; + divide(1,n); + g< +#include + +int nr = 0; + +void interclasare(int a[], int st, int dr){ + + int mijloc = (st + dr) / 2; + + int i = st; + int j = mijloc + 1; + int aux = 0; + + int size = dr - st + 1; + int arr[size]; + + while ( i <= mijloc and j <= dr) { + if (a[i] <= a[j]) { + arr[aux] = a [i]; + aux++; + i++; + } else { + nr += mijloc - i + 1; + nr = nr % 9917; + arr[aux] = a[j]; + aux++; + j++; + } + } + + while (i <= mijloc) { + arr[aux] = a[i]; + aux++; + i++; + } + while (j <= dr) { + arr[aux] = a[j]; + aux++; + j++; + } + + for (int k = 0; k < aux; ++k) { + a[st+k] = arr[k]; + } + +} + +void mergesort(int a[], int st, int dr){ + if (st == dr) + return; + int mijloc = (st + dr) / 2; + mergesort(a, st, mijloc); + mergesort(a, mijloc + 1, dr); + interclasare(a, st, dr); + +} + +int main() { + std :: ifstream fin("inv.in"); + std :: ofstream fout ( "inv.out"); + + int n; + int vt[100005]; + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> vt[i]; + } + mergesort(vt, 0, n-1); + fout << nr; + return 0; +} diff --git a/invCount/merge/d198000b-6219-4fd8-b4a8-f51e5700075d.cpp b/invCount/merge/d198000b-6219-4fd8-b4a8-f51e5700075d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72765edcc4bd10480b46dc2f2402eade110127fd --- /dev/null +++ b/invCount/merge/d198000b-6219-4fd8-b4a8-f51e5700075d.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(int i=0; i>v[i]; + + f.close(); + + mergeSort(v, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/d410b7af-d636-452d-af9d-9700c667fdf6.cpp b/invCount/merge/d410b7af-d636-452d-af9d-9700c667fdf6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de947c612bbb2a7b92c903404614197a05789f5b --- /dev/null +++ b/invCount/merge/d410b7af-d636-452d-af9d-9700c667fdf6.cpp @@ -0,0 +1,113 @@ +#include +#include +using namespace std; + + + +int v[100001], c[100001]; + +int n; + +long long inv; + + + +void interclasare(int st,int mij,int dr) + +{ + + int k=1,i=st,j=mij+1; + + while(i<=mij&&j<=dr) + + if(v[i]<=v[j]) + + c[k++]=v[i++]; + + else + + { + + inv += mij - i + 1 ; + + c[k++]=v[j++]; + + + + } + + while(i<=mij) + + c[k++]=v[i++]; + + while(j<=dr) + + c[k++]=v[j++]; + + k--; + + for(i=1;i<=k;i++) + + v[st+i-1]=c[i]; + + + +} + + + +void divide(int st,int dr) + +{ + + if(st>n; + + for(i=1;i<=n;i++) + + fin>>v[i]; + + + + divide(1,n); + + + + fout << inv % 9917; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/d5f57090-bdc1-4370-9695-ffd0a54e6c63.cpp b/invCount/merge/d5f57090-bdc1-4370-9695-ffd0a54e6c63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a041e20ef6bde9a91626ce3864726f03b47972f3 --- /dev/null +++ b/invCount/merge/d5f57090-bdc1-4370-9695-ffd0a54e6c63.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, a[100005], b[100005]; + +long long cnt; + + + +void Intercl(int st, int m, int dr) + +{ + + int i, j, k = 0; + + i = st; + + j = m + 1; + + while(i <= m && j <= dr) + + { + + if(a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + cnt = cnt + (m - i + 1); + + cnt %= 9917; + + b[++k] = a[j++]; + + } + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for(i = st; i <= dr; i++) + + a[i] = b[j++]; + +} + + + +void MSort(int st, int dr) + +{ + + int m; + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + { + + swap(a[st], a[dr]); + + cnt++; + + } + + } + + else + + { + + m = (st + dr) / 2; + + MSort(st, m); + + MSort(m + 1, dr); + + Intercl(st, m, dr); + + } + +} + + + +int main() + +{ + + cnt = 0; + + fin >> n; + + for(int i = 1; i <= n; i++) + + fin >> a[i]; + + MSort(1, n); + + cnt %= 9917; + + fout << cnt << "\n"; + + return 0; + +} diff --git a/invCount/merge/d674f240-12af-49b9-a061-1960039457de.cpp b/invCount/merge/d674f240-12af-49b9-a061-1960039457de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1f301df0b6d3cf99fa397074de5be4edfbaa559 --- /dev/null +++ b/invCount/merge/d674f240-12af-49b9-a061-1960039457de.cpp @@ -0,0 +1,72 @@ +#include +#include +using namespace std; + +const int NMAX = 100000, MD = 9917; +int sir[NMAX + 1]; +int mergeSort(int l,int r) +{ + int i, j, k, mid , counter, tmp[NMAX + 1]; + mid = (l + r) / 2; + i = l; + j = mid +1; + k = 0; + counter = 0; + while(i <= mid && j <= r) + { + if(sir[i] <= sir[j]) + { + tmp[k] = sir[i++]; + } + else + { + tmp[k] = sir[j++]; + counter += mid -i+1; + counter %= MD; + } + k++; + } + while(i <= mid) + { + tmp[k] = sir[i++]; + k++; + } + while(j <= r) + { + tmp[k] = sir[j++]; + k++; + } + for(i = l; i <= r; i++) + { + sir[i] = tmp[i-l]; + } + return counter; +} +int inversiuni(int st, int dr) +{ + if (st >= dr) + { + return 0; + } + int mij, leftInv, rightInv, totalInv; + mij = (st+dr)/2; + leftInv = inversiuni(st,mij); + rightInv = inversiuni(mij+1,dr); + totalInv = leftInv+rightInv+mergeSort(st,dr); + return totalInv; +} +int main() +{ + ifstream fin("inv.in"); + ofstream fout("inv.out"); + int n,i; + fin >> n; + for(i = 1; i <= n; i++) + { + fin >> sir[i]; + } + fout << inversiuni(1,n) % MD; + fin.close(); + fout.close(); + return 0; +} diff --git a/invCount/merge/d7e1a562-b737-457f-832c-3130f9199316.cpp b/invCount/merge/d7e1a562-b737-457f-832c-3130f9199316.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e765fa5d42056c571453d7e284fe334c451e0ba --- /dev/null +++ b/invCount/merge/d7e1a562-b737-457f-832c-3130f9199316.cpp @@ -0,0 +1,155 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + + + +int v[100000 +2]; + + + +int cnt = 0; + + + +void Merge(int st, int mij, int dr) + +{ + + int aux[100000 +2]; + + int i = st, j = mij+1, k = 0; + + + + while(i <= mij && j <= dr) + + { + + if(v[i] <= v[j]) + + { + + aux[k++] = v[i++]; + + } + + else + + { + + aux[k++] = v[j++]; + + cnt += (mij - i + 1); + + cnt%=9917; + + } + + } + + + + while(i <= mij) + + { + + aux[k++] = v[i++]; + + } + + + + while(j <= dr) + + { + + aux[k++] = v[j++]; + + } + + + + for(i=st; i<=dr; i++) + + { + + v[i] = aux[i-st]; + + } + +} + + + + + +void MergeSort(int st, int dr) + +{ + + if(st == dr) + + { + + return; + + } + + int mij = (st+dr)/2; + + MergeSort(st, mij); + + MergeSort(mij+1, dr); + + Merge(st, mij, dr); + + + +} + + + +int main() + +{ + + int n; + + fin>>n; + + for(int i=1; i<=n; i++) + + { + + fin>>v[i]; + + } + + + + MergeSort(1, n); + + fout< +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int Mod = 9917; + + + +int inv; + + + +void merge(vector &Arr, int lft, int mid, int rgt) { + + + + int i = lft; + + int j = mid + 1; + + + + vector mergedArr; + + mergedArr.reserve(rgt - lft + 1); + + + + while (i <= mid and j <= rgt) + + if (Arr[i] <= Arr[j]) + + mergedArr.push_back(Arr[i++]); + + else { + + + + inv += mid - i + 1; + + inv %= Mod; + + + + mergedArr.push_back(Arr[j++]); + + } + + + + while (i <= mid) mergedArr.push_back(Arr[i++]); + + while (j <= rgt) mergedArr.push_back(Arr[j++]); + + + + for (int idx = lft, mergedArrIdx = 0; idx <= rgt; ++idx, ++mergedArrIdx) + + Arr[idx] = mergedArr[mergedArrIdx]; + +} + + + +void mergeSort(vector &Arr, int lft, int rgt) { + + + + if (rgt == lft) + + return; + + else { + + + + int mid = (lft + rgt) / 2; + + + + mergeSort(Arr, lft, mid); + + mergeSort(Arr, mid + 1, rgt); + + merge(Arr, lft, mid, rgt); + + } + +} + + + +int main() { + + + + int n; + + fin >> n; + + + + vector Arr(n); + + for (int &itm : Arr) + + fin >> itm; + + + + mergeSort(Arr, 0, n - 1); + + + + fout << inv; + +} diff --git a/invCount/merge/dcc552a4-eab8-477f-bbcc-4fcb2f18a778.cpp b/invCount/merge/dcc552a4-eab8-477f-bbcc-4fcb2f18a778.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c5033840691b5b47049e2647f65ad33fe44ed3a --- /dev/null +++ b/invCount/merge/dcc552a4-eab8-477f-bbcc-4fcb2f18a778.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +long long interclasare(long long v[], long long a, long long m, long long b) + +{ + + long long i = a, j = m + 1, k = 0, r[100000], t, nr = 0; + + while(i <= m && j <= b) + + { + + if(v[i] <= v[j]) + + r[++k] = v[i++]; + + else if(v[j] < v[i]) + + { + + r[++k] = v[j++]; + + nr += m - i + 1; + + } + + } + + for(t = i; t <= m; t++) + + r[++k] = v[i++]; + + for(t = j; t <= b; t++) + + r[++k] = v[j++]; + + k = 1; + + for(t = a; t <= b; t++) + + v[t] = r[k++]; + + return nr; + +} + + + +long long divide_et_impera(long long v[], long long st, long long dr) + +{ + + long long m = (dr + st) / 2, nr = 0; + + if(st < dr) + + { + + nr += divide_et_impera(v, st, m); + + nr += divide_et_impera(v, m + 1, dr); + + nr += interclasare(v, st, m, dr); + + } + + return nr; + +} + + + +int main() + +{ + + long long n, i; + + f >> n; + + long long v[100000]; + + for(i = 1; i <= n; i++) + + f >> v[i]; + + g << divide_et_impera(v, 1, n) % 9917; + + return 0; + +} diff --git a/invCount/merge/dfc1071d-09a7-420d-8fde-8a73c9c06cb1.cpp b/invCount/merge/dfc1071d-09a7-420d-8fde-8a73c9c06cb1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bf5325c0e986b63a4d7fbdef8a788bcc5c9baba --- /dev/null +++ b/invCount/merge/dfc1071d-09a7-420d-8fde-8a73c9c06cb1.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +ifstream cin ("inv.in"); + +ofstream cout ("inv.out"); + + + +int n; + +long long nr_permutari; + +const int MOD = 9917; + + + +void afisare (int a[], int size_array) + +{ + + for (int i = 0; i < size_array; ++i) + + cout << a[i] << ' '; + + cout << '\n'; + +} + + + +void merge_function (int a[], int low, int mid, int high) + +{ + + int len1 = mid - low + 1; + + int len2 = high - mid; + + int L[len1], R[len2]; + + int i, j; + + for (i = 0; i < len1; ++i) + + L[i] = a[low + i]; + + for (i = 0; i < len2; ++i) + + R[i] = a[mid + i + 1]; + + i = j = 0; + + int k = low; + + while (i < len1 && j < len2) + + { + + if (L[i] <= R[j]) + + a[k++] = L[i++]; + + else + + { + + a[k++] = R[j++]; + + nr_permutari += len1 - i; + + nr_permutari %= MOD; + + } + + } + + while (i < len1) + + a[k++] = L[i++]; + + while (j < len2) + + a[k++] = R[j++]; + +} + + + +void mergesort (int a[], int low, int high) + +{ + + int mid = low + (high - low) / 2; + + if (low < high) + + { + + mergesort(a, low, mid); + + mergesort(a, mid + 1, high); + + merge_function(a, low, mid, high); + + } + +} + + + +int main() + +{ + + cin >> n; + + int a[n]; + + for (int i = 0; i < n; ++i) + + cin >> a[i]; + + mergesort(a, 0, n - 1); + + cout << nr_permutari % MOD; + + return 0; + +} diff --git a/invCount/merge/dfecfa56-39a7-4fec-b658-466c6c0bf7a4.cpp b/invCount/merge/dfecfa56-39a7-4fec-b658-466c6c0bf7a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63eda1b90b114c54aa59a621e2a78378d3cbb2e9 --- /dev/null +++ b/invCount/merge/dfecfa56-39a7-4fec-b658-466c6c0bf7a4.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + +const int MOD = 9917; +int v[100000], n; + +long long Merge(int arr[], int aux[], int left, int mid, int right) { + int i, j, k = 0; + long long inv_count = 0; + i = left, j = mid, k = left; + while(i <= mid-1 && j <= right) { + if(arr[i] <= arr[j]) + aux[k++] = arr[i++]; + else { + aux[k++] = arr[j++]; + inv_count += (mid - i); + } + } + while(i <= mid-1) + aux[k++] = arr[i++]; + while(j <= right) + aux[k++] = arr[j++]; + for(i=left; i<=right; i++) + arr[i] = aux[i]; + return inv_count; +} + +long long mergeSort(int arr[], int aux[], int left, int right) { + long long inv_count = 0; + if(left < right) { + int mid = (left + right) >> 1; + inv_count = mergeSort(arr,aux,left,mid); + inv_count += mergeSort(arr,aux,mid+1,right); + inv_count += Merge(arr,aux,left,mid+1,right); + } + return inv_count; +} + +int main() { + long long ans; + int aux[100000]; + FILE *fin, *fout; + fin = fopen("inv.in","r"); + fout = fopen("inv.out","w"); + fscanf(fin,"%d",&n); + for(int i=0; i + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +const int nmax = 100000, mod = 9917; + + + +int n; + +long long v[nmax + 5], L[nmax + 5], R[nmax + 5], temp[nmax + 5], ans; + + + +void solve(int st, int dr) + +{ + + if (st < dr) + + { + + int mid = (st + dr) / 2; + + solve(st, mid); + + solve(mid + 1, dr); + + int l1 = 0, l2 = 0, l = 0, a = 1, b = 1; + + for (int i = st; i <= mid; ++i) + + L[++l1] = v[i]; + + for (int i = mid + 1; i <= dr; ++i) + + R[++l2] = v[i]; + + while (a <= l1 && b <= l2) + + { + + if (L[a] > R[b]) + + { + + ans = (ans + l2 - b + 1) % mod; + + temp[++l] = L[a++]; + + } + + else + + { + + temp[++l] = R[b++]; + + } + + } + + while (a <= l1) + + temp[++l] = L[a++]; + + while (b <= l2) + + temp[++l] = R[b++]; + + for (int i = st, j = 1; i <= dr; ++i, ++j) + + v[i] = temp[j]; + + } + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i]; + + solve(1, n); + + fout << ans; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/e8c2ea9d-6434-486d-bfae-b70e97f04b65.cpp b/invCount/merge/e8c2ea9d-6434-486d-bfae-b70e97f04b65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3dfe7b1272568869b26d598f0a27813d73e1429 --- /dev/null +++ b/invCount/merge/e8c2ea9d-6434-486d-bfae-b70e97f04b65.cpp @@ -0,0 +1,162 @@ +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int NMAX=1e5, M=9917; + + + +int a[NMAX],N; + + + +int v_merge(int a[],int l, int r, int m) + +{ + + int i=l; + + int j=m+1; + + int p=l; + + int c[NMAX]; + + int ans=0; + + while(i<=m && j<=r) + + { + + if(a[i]<=a[j]) + + { + + c[p++]=a[i++]; + + } + + else + + { + + c[p++]=a[j++]; + + ans+=m-i+1; + + ans%=M; + + } + + } + + for(i;i<=m;++i) + + { + + c[p++]=a[i]; + + } + + for(j;j<=r;++j) + + { + + c[p++]=a[j]; + + } + + for(int i=l;i=r) + + { + + return 0; + + } + + { + + int m=(l+r)/2; + + int ileft=ninv(a,l,m); + + int iright=ninv(a,m+1,r); + + return (ileft+iright+v_merge(a,l,r,m))%M; + + } + +} + + + +void read() + +{ + + in>>N; + + for(int i=0;i>a[i]; + + } + +} + + + +void solve() + +{ + + read(); + + int ans=ninv(a,0,N-1); + + out< +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + +int aux[100001]; + +int n; + +void afisare(int v[]) + +{ + + int i; + + for(i=1; i<=n; i++) + + out<>n; + + for(i=1; i<=n; i++) + + in>>v[i]; + + out << sortare(v,1,n)%9917; + + return 0; + +} diff --git a/invCount/merge/e98b2303-321d-4760-8954-df6685bc8df6.cpp b/invCount/merge/e98b2303-321d-4760-8954-df6685bc8df6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1231bbf46a8ac8081cc5b25ff869463324f13d7c --- /dev/null +++ b/invCount/merge/e98b2303-321d-4760-8954-df6685bc8df6.cpp @@ -0,0 +1,101 @@ +#include +#include + + +const int MAXN = 1e5 + 5,MOD = 9917; + + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +int n,v[MAXN],aux[MAXN],ans = 0; + +pair nume[MAXN]; + + + +void combina(int left,int mid,int right){ + + int i = left,j = mid + 1,index = left - 1; + + while(i <= mid and j <= right){ + + while(v[i] > v[j] and j <= right){ + + aux[++index] = v[j]; + + j++; + + ans += mid - i + 1; + + } + + while(v[j] >= v[i] and i <= mid){ + + aux[++index] = v[i]; + + i++; + + } + + } + + while(i <= mid) + + aux[++index] = v[i++]; + + while(j <= right) + + aux[++index] = v[j++]; + + for(int i = left; i <= right; i++) + + v[i] = aux[i]; + + ans %= MOD; + +} + +void divide(int left = 1,int right = n){ + + if(left == right) + + return; + + int mid = (left + right) / 2; + + divide(left,mid); + + divide(mid + 1,right); + + combina(left,mid,right); + +} + + + +int main() + +{ + + in>>n; + + for(int i = 1; i <= n; i++) + + in>>v[i]; + + divide(); + + out< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int a[100005], b[100005],n,i,inv; + +void interclasare(int a[], int aux[], int p, int q, int r, int &inv) + +{ int i,j,k,c; + + i=p; j=q+1; k=p; c=0; + + while(i<=q && j<=r) + + { + + if(a[i]<=a[j]) { inv=(inv+c)%9917; aux[k]=a[i]; i++; k++;} + + else {c=j-q; aux[k]=a[j]; j++; k++;} + + } + + while(i<=q){ inv=(inv+c)%9917; aux[k]=a[i]; i++; k++;} + + while(j<=r){aux[k]=a[j]; j++; k++;} + + for(i=p;i<=r;i++){a[i]=aux[i];} + +} + +void mergesort(int a[], int aux[], int p, int r, int &inv) + +{ if(p>n; + + for(i=1;i<=n;i++)fin>>a[i]; + + inv=0; + + mergesort(a,b,1,n,inv); + + fout< +#include +using namespace std; + + + +int nr=0; + + + +void interclasare(int v[], int st, int dr) + +{ + + int mij = (st + dr) / 2; + + int i = st; + + int j = mij + 1; + + int k = 0; + + int n = dr - st + 1; + + int c[n]; + + while (i <= mij && j <= dr) + + { + + if (v[i] <= v[j]) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + else + + { + + nr += mij - i + 1; + + nr = nr % 9917; + + c[k] = v[j]; + + k++; + + j++; + + } + + } + + while (i <= mij) + + { + + c[k] = v[i]; + + k++; + + i++; + + } + + while (j <= dr) + + { + + c[k] = v[j]; + + k++; + + j++; + + } + + for (i=0; i>n; + + for(int i=0; i>v[i]; + + f.close(); + + mergeSort(v, 0, n-1); + + g << nr; + + g.close(); + + return 0; + +} diff --git a/invCount/merge/ec952715-2828-4829-a968-025bf2d15ac1.cpp b/invCount/merge/ec952715-2828-4829-a968-025bf2d15ac1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f5f19763f2e5b95e884f4820cb2cd1549b0fc81 --- /dev/null +++ b/invCount/merge/ec952715-2828-4829-a968-025bf2d15ac1.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +const int MAXN = 1000005; + + + +int v[MAXN], aux[MAXN]; + + + +long long ans; + + + +int pos1, pos2, k; + + + +void mergesort(int st, int dr){ + + if(st == dr) + + return; + + int mij = (st + dr) / 2; + + mergesort(st, mij); + + mergesort(mij + 1, dr); + + pos1 = st; + + pos2 = mij + 1; + + k = st; + + while(pos1 <= mij && pos2 <= dr){ + + if(v[pos1] <= v[pos2]) + + aux[k++] = v[pos1++]; + + else{ + + aux[k++] = v[pos2++]; + + ans += mij - pos1 + 1; + + ans %= 9917; + + } + + } + + while(pos1 <= mij) + + aux[k++] = v[pos1++]; + + while(pos2 <= dr) + + aux[k++] = v[pos2++]; + + for(int i = st; i <= dr; ++i) + + v[i] = aux[i]; + +} + + + +int main() + +{ + + ifstream fin("inv.in"); + + ofstream fout("inv.out"); + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) + + fin >> v[i]; + + mergesort(1, n); + + fout << ans; + + return 0; + +} diff --git a/invCount/merge/efff3503-1284-4c09-a262-b78f2bb3508e.cpp b/invCount/merge/efff3503-1284-4c09-a262-b78f2bb3508e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ca1a97d5f2569f7f80314760f8e8cfdee09b4fd --- /dev/null +++ b/invCount/merge/efff3503-1284-4c09-a262-b78f2bb3508e.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NR = 100001; + +int temp[NR], nr_inv = 0; + + + +void MergeSort(int vectorr[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mijloc = (stanga + dreapta) / 2; + + MergeSort(vectorr, stanga , mijloc); + + MergeSort(vectorr, mijloc + 1 , dreapta); + + int i = stanga, j = mijloc + 1, k = 0; + + + + while( i <= mijloc && j <= dreapta ) + + if( vectorr[i] <= vectorr[j]) + + temp[++k] = vectorr[i++]; + + else + + { + + temp[++k] = vectorr[j++]; + + nr_inv = (nr_inv + mijloc - i + 1)%9917; + + } + + + + while(i <= mijloc) + + temp[++k] = vectorr[i++]; + + + + while(j <= dreapta) + + temp[++k] = vectorr[j++]; + + + + for(i = stanga , j = 1 ; i <= dreapta ; i ++ , j ++) + + vectorr[i] = temp[j]; + + } + +} + + + +int main() + +{ + + + + int n, v[NR]; + + f>>n; + + + + for(int i = 1; i <= n; i++) + + f>>v[i]; + + + + MergeSort(v,1,n); + + + + g< +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +const int MAXN = 1e5 + 4; + +const int MOD = 9917; + +int rasp, v[MAXN], x[MAXN]; + +void interclasare(int st1 , int dr1, int st2, int dr2){ + + int i = st1 , j = st2, indice = st1 - 1; + + while(i <= dr1 and j <= dr2){ + + if(v[i] <= v[j]){ + + x[++indice] = v[i++]; + + } + + else{ + + x[++indice] = v[j++]; + + rasp += (dr1 - i + 1); + + rasp %= MOD; + + } + + } + + for(i; i <= dr1; ++i) x[++indice] = v[i]; + + for(j; j <= dr2; ++j) x[++indice] = v[j]; + + for(int k = st1; k <= dr2; ++k) + + v[k] = x[k], x[k] = 0; + +} + +void dei(int st, int dr){ + + int m = (st + dr ) / 2; + + if(dr - st <= 0) return; + + dei(st, m); + + dei(m + 1, dr); + + interclasare(st, m, m + 1, dr); + +} + +int main(){ + + ios::sync_with_stdio(false); + + fin.tie(0);fout.tie(0); + + int n ; fin >> n; + + for(int i = 1; i <= n; ++i){ + + fin >> v[i]; + + } + + dei(1, n); + + fout << rasp; + + return 0; + +} diff --git a/invCount/merge/f3e95f53-d41d-4c0f-9c78-ac3f4bb2d0d6.cpp b/invCount/merge/f3e95f53-d41d-4c0f-9c78-ac3f4bb2d0d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aa9845aa19bd65fa0456234a28bf0e4471d3539 --- /dev/null +++ b/invCount/merge/f3e95f53-d41d-4c0f-9c78-ac3f4bb2d0d6.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + +const int max_size = 100000 + 1; + + + +int ans; + +vector v(max_size), temp(max_size); + + + +void interclasare(int s, int m, int d) { + + + + int i = s, j = m + 1, k = 0; + + + + while (i <= m && j <= d) { + + + + if (v[i] <= v[j]) + + temp[k++] = v[i++]; + + + + else { + + ans = (ans + m - i + 1) % 9917; + + temp[k++] = v[j++]; + + } + + + + } + + + + while (i <= m) + + temp[k++] = v[i++]; + + + + while (j <= d) + + temp[k++] = v[j++]; + + + + for (k = s; k <= d; ++k) + + v[k] = temp[k - s]; + +} + + + +void merge_sort(int s, int d) { + + if (s < d) { + + int m = (s + d) / 2; + + merge_sort(s, m); + + merge_sort(m + 1, d); + + interclasare(s, m, d); + + } + +} + + + +int main() { + + int N; + + in >> N; + + + + for (int i = 0; i < N; ++i) + + in >> v.at(i); + + + + merge_sort(0, N - 1); + + + + out << ans; + + + + return 0; + +} diff --git a/invCount/merge/f6d1ddab-0b52-4735-9a4b-6350c89d0b43.cpp b/invCount/merge/f6d1ddab-0b52-4735-9a4b-6350c89d0b43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43452c8913bfc20b8e06eab6ea9bd09bbbdf7002 --- /dev/null +++ b/invCount/merge/f6d1ddab-0b52-4735-9a4b-6350c89d0b43.cpp @@ -0,0 +1,95 @@ +#include +#include +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int NMAX = 100001; + +long long nr_inv; + + + +void interclasare(int v[], int p, int u, int m) + +{ + + int a[NMAX]; + + int i = p, j = m + 1, k = 1, mij = (p + u)/2; + + while(i <= m && j <= u) + + if(v[i] <= v[j]) + + a[k++] = v[i++]; + + else + + { + + a[k++] = v[j++]; + + nr_inv += (mij - i + 1)%9917; + + } + + while(i <= m) a[k++] = v[i++]; + + while(j <= u) a[k++] = v[j++]; + + for(i = p, k = 1; i <= u; i++, k++) + + v[i] = a[k]; + +} + + + +void merge_sort(int v[], int p, int u) + +{ + + if(p < u) + + { + + int m = (p + u)/2; + + merge_sort(v, p, m); + + merge_sort(v, m + 1, u); + + interclasare(v, p, u, m); + + } + +} + + + +int main() + +{ + + int s[NMAX], n; + + f >> n; + + for(int i = 1; i <= n; i++) + + f >> s[i]; + + + + merge_sort(s, 1, n); + + g << nr_inv % 9917; + + return 0; + +} diff --git a/invCount/merge/f739cd5f-77d9-4786-8703-bb4300892e74.cpp b/invCount/merge/f739cd5f-77d9-4786-8703-bb4300892e74.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7216c415fd957a554996d53ea54c6780022866f6 --- /dev/null +++ b/invCount/merge/f739cd5f-77d9-4786-8703-bb4300892e74.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + +ifstream f("inv.in"); + +ofstream g("inv.out"); + +int nr=0, v[100001], nou[100001]; + +void interclasare(int left, int mid, int right) + +{ + + int i=left; + + int j=mid+1; + + int k=0; + + while(i<=mid && j<=right) + + { + + if(v[i]<=v[j]) + + nou[k++]=v[i++]; + + else + + { + + nr+=mid-i+1; + + nr=nr%9917; + + nou[k++]=v[j++]; + + } + + } + + + + while(i<=mid) + + nou[k++]=v[i++]; + + while(j<=right) + + nou[k++]=v[j++]; + + for(k=left;k<=right;k++) + + v[k]=nou[k-left]; + +} + + + +void mergesort(int left, int right) + +{ + + if(left>n; + +for(int i=0;i>v[i]; + +mergesort(0,n-1); + +g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("inv.in"); + +ofstream g("inv.out"); + + + +const int kMax = 100005; + +const int kMod = 9917; + +int v[kMax], rez[kMax]; + + + +int merge_halves(int a[], int b[], int left, int right, int m) { + + int size_a = m - left + 1; + + int size_b = right - m; + int idxa = 0, idxb = 0, nrInv = 0, idxRez = 0; + + + + for(idxa = 0; idxa < size_a && idxb < size_b; idxRez++) { + + if(a[idxa] > b[idxb]) { + + rez[idxRez] = b[idxb++]; + + nrInv += size_a - idxa; + + } else { + + rez[idxRez] = a[idxa++]; + + } + + } + + + + for(; idxa < size_a; idxRez++, idxa++) { + + rez[idxRez] = a[idxa]; + + } + + + + for(; idxb < size_b; idxRez++, idxb++) { + + rez[idxRez] = b[idxb]; + + } + for(int i = 0; i < size_b + size_a; i++) { + + a[i] = rez[i]; + + } + + + + return nrInv; + +} + + + +int merge_sort(int left, int right) { + + if(left == right) + + return 0; + + + + int middle = (left + right) / 2; + + int inv_left = merge_sort(left, middle); + + int inv_right = merge_sort(middle + 1, right); + + + + int crt_inv = merge_halves(v + left, v + middle + 1, left, right, middle); + + return (crt_inv + inv_right + inv_left) % kMod; + +} + + + +int main() { + + int n; + + f >> n; + + for(int i = 0; i < n; i++) { + + f >> v[i]; + + } + + + + g << merge_sort(0, n - 1); + + return 0; + +} diff --git a/invCount/merge/f8be80e7-b45e-4959-a19b-36dd9009879c.cpp b/invCount/merge/f8be80e7-b45e-4959-a19b-36dd9009879c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8eee8a91baf2bd2522d7f2ff624045c4cb50924 --- /dev/null +++ b/invCount/merge/f8be80e7-b45e-4959-a19b-36dd9009879c.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +int a[100005], b[100005], n, cnt; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int Int(int st, int m, int dr) + +{ + + int i, j, k; + + i = st; j = m + 1; k = 0; + + while(i <= m && j <= dr) + + if(a[i] <= a[j])b[++k] = a[i++]; + + else + + { + + cnt += (m - i + 1); + + cnt = cnt % 9917; + + b[++k] = a[j++]; + + } + + while(i <= m) + + b[++k] = a[i++]; + + while(j <= dr) + + b[++k] = a[j++]; + + j = 1; + + for(i = st; i <= dr; i++) + + a[i] = b[j++]; + + + +} + + + +void MSort(int st, int dr) + +{ + + if(dr - st <= 1) + + { + + if(a[st] > a[dr]) + + { + + cnt++; + + if(cnt == 9917)cnt = 0; + + swap(a[st], a[dr]); + + } + + } + + else + + { + + int m = (st + dr) / 2; + + MSort(st, m); + + MSort(m + 1, dr); + + Int(st, m, dr); + + } + +} + + + +int main() + +{ + + int i; + + fin >> n; + + for(i = 1; i <= n; i++) + + fin >> a[i]; + + MSort(1, n); + + fout << cnt; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/f95bf555-6779-4a5b-99be-e19c4b821855.cpp b/invCount/merge/f95bf555-6779-4a5b-99be-e19c4b821855.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8aea747e81a1f0f033112bad72e85b14053f4355 --- /dev/null +++ b/invCount/merge/f95bf555-6779-4a5b-99be-e19c4b821855.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +using namespace std; + +const int MOD = 9917; + +int a[100005], b[100005], sol; + +ifstream fin ("inv.in"); + +ofstream fout ("inv.out"); + +void Merge (int left, int right, int m) + +{ + + int i, j, k = 0; + + i = left; + + j = m + 1; + + while (i <= m && j <= right) + + if (a[i] <= a[j]) b[++k] = a[i++]; + + else + + { + + sol = (sol + (m - i + 1)) % MOD; + + b[++k] = a[j++]; + + } + + while (i <= m) + + b[++k] = a[i++]; + + while (j <= right) + + b[++k] = a[j++]; + + j = 1; + + for (i = left; i <= right; i++) + + a[i] = b[j++]; + +} + + + +void MergeSort (int left, int right) + +{ + + if (right - left <= 1) + + { + + if (a[right] < a[left]) + + { + + swap (a[left], a[right]); + + sol++; + + sol %= MOD; + + } + + } + + else + + { + + int m = (left + right) / 2; + + MergeSort(left, m); + + MergeSort(m + 1, right); + + Merge(left, right, m); + + } + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for (int i = 1; i <= n; i++) + + fin >> a[i]; + + MergeSort(1, n); + + fout << sol << "\n"; + + fout.close(); + + return 0; + +} diff --git a/invCount/merge/f96917b5-9acf-47db-a7b0-21119b8fb22c.cpp b/invCount/merge/f96917b5-9acf-47db-a7b0-21119b8fb22c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62c07d7a9fbf8c151623ff8fbc6f3859ddc5cd36 --- /dev/null +++ b/invCount/merge/f96917b5-9acf-47db-a7b0-21119b8fb22c.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + +int a[100002]; + +int b[100002]; + +int n,ct; + +void interclasare(int st,int m, int dr) + +{ + + int i=st,j=m+1,k=0; + + while(i<=m && j<=dr) + + if(a[i]<=a[j]) b[++k]=a[i++]; + + else + + { + + b[++k]=a[j++]; + + ct+=(m-i+1); + + ct=ct%9917; + + } + + while(i<=m) + + b[++k]=a[i++]; + + while(j<=dr) + + b[++k]=a[j++]; + + j=1; + + for(i=st;i<=dr;i++) + + a[i]=b[j++]; + +} + +void MergeSort(int st, int dr) + +{ + + int m; + + if(dr-st<=1) + + { + + if(a[st]>a[dr]) + + { + + swap(a[dr],a[st]); + + ct++; + + } + + } + + else + + { + + m=(st+dr)/2; + + MergeSort(st,m); + + MergeSort(m+1,dr); + + interclasare(st,m,dr); + + } + +} + +int main() + +{ + + fin>>n; + + for(int i=1; i<=n; i++) + + fin>>a[i]; + + MergeSort(1,n); + + fout< +#include +using namespace std; + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int n, a[100001], b[100001]; + + + +void Read() + +{ + + fin >> n; + + for(int i=1; i<=n; i++) + + fin >> a[i]; + +} + + + +long long Inv(int st, int dr) + +{ + + int mij; + + mij=(st+dr)/2; + + if(st==dr) + + return 0; + + long long inv1, inv2; + + inv1=Inv(st,mij)%9917; + + inv2=Inv(mij+1,dr)%9917; + + int i=st; + + int j=mij+1; + + int k=st-1; + + long long inv3=0; + + while(i<=mij && j<=dr) + + if(a[j] + + +using namespace std; + +int aux[100005], v[100005]; + +int mmerge(int st, int dr) +{ + int i, j, k, m = (st+dr) / 2; + long long nr = 0; + + i = k = st; j = m+1; + + while(i <= m && j <= dr) + { + if(v[i] <= v[j]) + aux[k++] = v[i++]; + else + { + aux[k++] = v[j++]; + nr += m - i + 1; + } + } + + while(i <= m) aux[k++] = v[i++]; + while(j <= dr) aux[k++] = v[j++]; + + for(k = st; k <= dr; ++k) v[k] = aux[k]; + + return nr % 9917; +} + +int msort(int st, int dr) +{ + if(st == dr) return 0; + int m = (st+dr) / 2; + + int a = msort(st, m); + int b = msort(m+1, dr); + return (a + b + mmerge(st, dr))%9917; +} + +int main() +{ + ifstream fin("inv.in"); + ofstream fout("inv.out"); + + int n, i; + fin >> n; + for(i = 0; i < n; ++i) fin >> v[i]; + + fout << msort(0, n-1); +} diff --git a/invCount/merge/fb7e2e04-6776-4fb1-826a-9627652ee06e.cpp b/invCount/merge/fb7e2e04-6776-4fb1-826a-9627652ee06e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98145e68703dd8f60f5147ad2b094d09e446fbaf --- /dev/null +++ b/invCount/merge/fb7e2e04-6776-4fb1-826a-9627652ee06e.cpp @@ -0,0 +1,180 @@ +#include + + +using namespace std; + + + +ifstream fin("inv.in"); + +ofstream fout("inv.out"); + + + +int merge(int arr[], int l, int m, int r) + +{ + + int i, j, k; + + int n1 = m - l + 1; + + int n2 = r - m; + + int inv = 0; + + int L[n1], R[n2]; + + + + for (i = 0; i < n1; i++) + + L[i] = arr[l + i]; + + for (j = 0; j < n2; j++) + + R[j] = arr[m + 1+ j]; + + + + i = 0; + + j = 0; + + k = l; + + while (i < n1 && j < n2) + + { + + if (L[i] <= R[j]) + + { + + arr[k] = L[i]; + + i++; + + inv += j; + + inv %= 9917; + + } + + else + + { + + arr[k] = R[j]; + + j++; + + } + + k++; + + } + + + + while (i < n1) + + { + + arr[k] = L[i]; + + i++; + + k++; + + inv += j; + + inv %= 9917; + + } + + + + while (j < n2) + + { + + arr[k] = R[j]; + + j++; + + k++; + + } + + inv %= 9917; + + return inv; + +} + + + +int mergeSort(int arr[], int l, int r) + +{ + + if (l < r) + + { + + int m = l+(r-l)/2; + + int inv = 0; + + + + inv += mergeSort(arr, l, m); + + inv %= 9917; + + inv += mergeSort(arr, m+1, r); + + inv %= 9917; + + + + inv += merge(arr, l, m, r); + + inv %= 9917; + + return inv; + + } + + return 0; + +} + + + +int v[100000]; + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i = 0; i < n; i++) + + fin >> v[i]; + + int inv = mergeSort(v, 0, n-1); + + inv %= 9917; + + fout << inv; + + return 0; + +} diff --git a/invCount/merge/fb9667b4-2001-41bc-9e5d-a7163181905a.cpp b/invCount/merge/fb9667b4-2001-41bc-9e5d-a7163181905a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d80acedf60000ba9096028b69f37483585c7014 --- /dev/null +++ b/invCount/merge/fb9667b4-2001-41bc-9e5d-a7163181905a.cpp @@ -0,0 +1,109 @@ +#include +#include +using namespace std; + + + + + +int interclasare(int a[], int i, int j,int m) + +{ + + int nr=0; + + int aux[i+j-1]; + + int p=i; + + int q=m+1; + + int k=0; + + while(p<=m && q<=j) + + { + + if(a[p]<=a[q]) + + aux[k++]=a[p++]; + + else + + {aux[k++]=a[q++];nr+=m-p+1;} + + } + + while(p<=m) + + aux[k++]=a[p++]; + + while(q<=j) + + aux[k++]=a[q++]; + + + + for(int l=i; l<=j; l++) + + a[l]=aux[l-i]; + + + + return nr; + + + +} + + + +int mergesort(int a[],int i,int j) + +{ + + int nrinv=0; + + if(i>N; + + int a[100010]; + + for(int i=0;i>a[i]; + + out< +#include + + +using namespace std; + + + +ifstream in("inv.in"); + +ofstream out("inv.out"); + + + + + +long a[100001]; + +int n,inv_count; + +long Ifinal; + + + +void MergeSort(long a[], int st, int dr) + +{ + + if(st < dr) + + { + + int mij =(st + dr)/2; + + + + MergeSort(a,st,mij); + + MergeSort(a,mij+1,dr); + + + + + + int k=0; + + int i=st; + + int j=mij+1; + + + + int temp[100001]; + + int inv_count=0; + + + + while (i <= mij && j<=dr) + + if(a[i]<=a[j]) + + { + + temp[k++]=a[i++]; + + Ifinal+=inv_count; + + + + } + + else + + { + + temp[k++]=a[j++]; + + inv_count++; + + + + } + + + + while(i<=mij) + + { + + temp[k++]=a[i++]; + + Ifinal+=inv_count; + + } + + while(j<=dr) + + temp[k++]=a[j++]; + + + + + + for(i=st; i<=dr; i++) + + a[i]=temp[i-st]; + + } + +} + + + +long Inv(long a[],int n) + +{ + + MergeSort(a,0,n-1); + + return Ifinal; + +} + + + + + +int main() + +{ + + in>>n; + + for(int i=0; i>a[i]; + + + + out< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int LogP(int a, int n, int P) + +{ + + int rez = 1; + + while(n > 0) + + { + + if (n % 2 == 1) + + rez = 1LL * rez * a % P; + + n /= 2; + + a = 1LL * a * a % P; + + } + + return rez; + +} + + + +int Phi(int n) + +{ + + int f = 2, e, phi = n; + + while(n > 1) + + { + + e = 0; + + while(n % f == 0) + + { + + e++; + + n/=f; + + } + + if(e > 0) phi = phi / f * (f - 1); + + if(f * f < n) f++; + + else f = n; + + } + + + + return phi; + +} + + + +int main() + +{ + + long long a, P; + + fin >> a >> P; + + fout << LogP(a, Phi(P) - 1, P); + + + + return 0; + +} diff --git a/invmod/euler-toutient/02c933eb-d85d-4b33-8268-4faced1b2f7f.cpp b/invmod/euler-toutient/02c933eb-d85d-4b33-8268-4faced1b2f7f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc985e38140cb0a4309a95781a079cc78a64c0e7 --- /dev/null +++ b/invmod/euler-toutient/02c933eb-d85d-4b33-8268-4faced1b2f7f.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n,a,x; + +int putere(int b,int e) + +{ + + if(e==0)return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1)r=1LL*r*b%n; + + return r; + +} + +int euler(int m) + +{ + + int phi=1; + + if(m%2==0){m/=2;while(m%2==0){m/=2;phi*=2;}} + + for(int d=3;d*d<=m;d+=2) + + if(m%d==0){m/=d;phi*=d-1;while(m%d==0){m/=d;phi*=d;}} + + if(m>1)phi*=m-1; + + return phi; + +} + +int main() + +{ + + f>>a>>n; + + g< + + +using namespace std; + + + + + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol,p; + + sol = n; + + p = 2; + + for( p = 2; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p *(p-1); + + while(n%p == 0) + + n/=p; + + } + + } + + if(n > 1) + + sol = sol / n *(n-1); + + return sol; + +} + + + +int exp(int a, int n,int mod) + +{ + + int sol=1; + + while(n>0) + + { + + if(n % 2 == 1) + + sol = 1LL*sol*a%mod; + + n/=2; + + a=1LL*a*a%mod; + + + + } + + return sol; + + + +} + + + +int main() + +{ + + int a , n; + + fin >> a >> n; + + int b; + + b=exp(a,Phi(n)-1,n); + + fout< + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a, n; +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + fin >> a >> n; + + fout << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/04ac9293-4f45-4085-9fa8-a193c57567c2.cpp b/invmod/euler-toutient/04ac9293-4f45-4085-9fa8-a193c57567c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c32a218b92b883c8a8ccddbb673761f3bb00d14 --- /dev/null +++ b/invmod/euler-toutient/04ac9293-4f45-4085-9fa8-a193c57567c2.cpp @@ -0,0 +1,101 @@ +#include +#include + + + + +using namespace std; + + + +ifstream x("inversmodular.in"); + +ofstream y("inversmodular.out"); + + + +long long a,n; + + + +long long get(long long M) + +{ + + long long cr=M; + + for(long long i=2;i*i<=M;++i) + + { + + if(M%i==0) + + { + + while(M%i==0) + + { + + M/=i; + + } + + cr=(cr/i)*(i-1); + + } + + } + + if(M!=1) + + { + + cr=cr/M*(M-1); + + } + + return cr; + +} + + + +long long inv(long long N, long long M) + +{ + + long long nr=N,cr=1,put=get(M)-1; + + for(long long i=1;i<=put;i<<=1) + + { + + if(i&put) + + cr=(cr*nr)%M; + + nr=(nr*nr)%M; + + } + + return cr; + +} + + + +int main() + +{ + + x>>a>>n; + + y< + + +using namespace std; + + + +int n; + + + +int rid_put(int a,int b) + +{ + + int sol=1; + + for(long long i=1;i<=b;i<<=1) + + { + + if(b&i) sol=(1LL*sol*a)%n; + + a=(1LL*a*a)%n; + + } + + return sol; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a; + + scanf("%d%d",&a,&n); + + int fi=n,nr=n; + + for(int i=2;i*i<=nr;i++) + + if(nr%i==0) + + { + + while(nr%i==0) nr/=i; + + fi=(fi/i)*(i-1); + + } + + if(nr>1) fi=(fi/nr)*(nr-1); + + printf("%d",rid_put(a,fi-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/07e93f74-cb78-4df0-b47a-c04ec82e110c.cpp b/invmod/euler-toutient/07e93f74-cb78-4df0-b47a-c04ec82e110c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..571da34648d04b7bc30dc43f197a5c7d4596b297 --- /dev/null +++ b/invmod/euler-toutient/07e93f74-cb78-4df0-b47a-c04ec82e110c.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +const int NMax = 1e5; + +vector PrimeList; + + + +int A, N, Nprim, Prime[NMax], Div[NMax], NrDivs; + +long long Sol = 1; + + + +int LogPow(int X, int P,int MOD) { + + long long ans = 1; + + while (P) { + + if (P % 2 == 1) + + ans = (1LL * ans * X) % MOD; + + X = (1LL * X * X) % MOD; + + P /= 2; + + } + + return ans; + +} + + + +void Sieve() + +{ + + for(int i = 2; i <= NMax; i++) + + if(Prime[i] == 0) + + { + + PrimeList.push_back(i); + + + + for(int j = 2 * i; j <= NMax; j += i) + + Prime[j] = 1; + + } + +} + + + +void Decompose() + +{ + + Nprim = N; + + for(auto x : PrimeList) + + { + + if(x * x > N) break; + + if(N % x) continue; + + + + while(N % x == 0) + + N /= x; + + + + Div[++NrDivs] = x; + + } + + if(N > 1) Div[++NrDivs] = N; + +} + + + +void Solve() { + + Sol = Nprim; + + for (int i = 1; i <= NrDivs; i++) { + + Sol *= (Div[i] - 1); + + Sol /= Div[i]; + + } + +} + + + +int main() { + + in >> A >> N; + + Sieve(); + + Decompose(); + + Solve(); + + out << LogPow(A,Sol-1,Nprim); + +} diff --git a/invmod/euler-toutient/0888b19b-ce85-4d7f-b6b4-11ce750be193.cpp b/invmod/euler-toutient/0888b19b-ce85-4d7f-b6b4-11ce750be193.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc1277738a8e1a6f185f7a143ed6167792c3039b --- /dev/null +++ b/invmod/euler-toutient/0888b19b-ce85-4d7f-b6b4-11ce750be193.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int main() + +{ + + long long a = 0; + + int n = 0, nn; + + in >> a >> n; + + nn = n; + + int d = 2; + + long long p = 1; + + int fi = n; + + while(d*d<=n){ + + if(n%d==0){ + + fi = fi/d*(d-1); + + while(n%d==0){ + + n/=d; + + } + + } + + d++; + + } + + if(n > 1){ + + fi = fi/n*(n-1); + + } + + + + n = nn; + + fi--; + + p = 1; + + while(fi!=0){ + + if(fi%2!=0){ + + p = p * a % n; + + } + + fi/=2; + + a = a * a % n; + + } + + out << p; + +} diff --git a/invmod/euler-toutient/0c5da6e4-a963-4d84-ae67-fc4f65af4152.cpp b/invmod/euler-toutient/0c5da6e4-a963-4d84-ae67-fc4f65af4152.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b47a7019ad1fb1fc5fca36db9c1a58caf89d76a3 --- /dev/null +++ b/invmod/euler-toutient/0c5da6e4-a963-4d84-ae67-fc4f65af4152.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + + + +int main() { + + freopen ("inversmodular.in", "r", stdin); + + freopen ("inversmodular.out", "w", stdout); + + + + auto get_phi = [&] (int num) { + + int phi = num; + + for (int d = 2; d * d <= num; d++) { + + bool is = 0; + + while (num % d == 0) { + + num /= d; + + is = 1; + + } + + if (is) { + + phi /= d; + + phi *= (d - 1); + + } + + } + + if (num > 1) { + + phi /= num; + + phi *= (num - 1); + + } + + return phi; + + }; + + + + int a, mod; + + cin >> a >> mod; + + int b = get_phi(mod) - 1; + + int sol = 1; + + + + while (b) { + + if (b & 1) { + + sol = sol * (long long) a % mod; + + } + + a = a * (long long) a % mod; + + b /= 2; + + } + + cout << sol << "\n"; + + + + + + return 0; + +} diff --git a/invmod/euler-toutient/0cc9122e-59ce-4fbf-94b6-04489f2ee1ef.cpp b/invmod/euler-toutient/0cc9122e-59ce-4fbf-94b6-04489f2ee1ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4ca226d54bd1f53691fe51bd315e443f5cba541 --- /dev/null +++ b/invmod/euler-toutient/0cc9122e-59ce-4fbf-94b6-04489f2ee1ef.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int main() { + + long long n, a, phi, cn, ans, d; + + cin >> a >> n; + + cn = n; + + phi = n; + + d = 2; + + while ( d * d <= n ) { + + if ( n % d == 0 ) { + + phi = phi / d * ( d - 1 ); + + while ( n % d == 0 ) + + n /= d; + + } + + d++; + + } + + if ( n > 1 ) + + phi = phi / n * ( n - 1 ); + + phi--; + + ans = 1; + + while ( phi > 0 ) { + + if ( phi % 2 != 0 ) + + ans = ans * a % cn; + + phi /= 2; + + a = a * a % cn; + + } + + cout << ans; + + return 0; + +} diff --git a/invmod/euler-toutient/0cd73a62-bf73-4916-88e5-dd0da4d0a344.cpp b/invmod/euler-toutient/0cd73a62-bf73-4916-88e5-dd0da4d0a344.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f09acdefd363d5e2c59c9b9f328e9fbaafaa1567 --- /dev/null +++ b/invmod/euler-toutient/0cd73a62-bf73-4916-88e5-dd0da4d0a344.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long A,N,EulerTotient; + +long long Phi(long long L) + +{ + + long long Prod=1; + + for(int i=2; i*i<=L; i++) + + if(L%i==0) + + { + + Prod=Prod*(i-1); + + L/=i; + + while(L%i==0) + + { + + Prod*=i; + + L/=i; + + } + + } + + + + if(L!=1) + + Prod*=(L-1); + + + + return Prod; + +} + +long long logPow() + +{ + + long long rez=1; + + while(EulerTotient) + + { + + if(EulerTotient%2) + + rez=(rez*A)%N; + + A=(A*A)%N; + + EulerTotient/=2; + + } + + return rez%N; + +} + +int main() + +{ + + f>>A>>N; + + EulerTotient=Phi(N)-1; + + g< + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long A, N, mod; + +long long phi(long long n){ + + long long ans=n; + +for(long long i=2;i*i<=n;i++){ + + if(n%i==0) + + { + + ans=ans/i*(i-1); + + while(n%i==0) n/=i; + + } + +} + +if(n!=1) + + ans=ans/n*(n-1); + +return ans; + +} + +long long put(long long n, long long p){ + +long long ans=1; + +while(p>0){ + + if(p%2==1) + + ans=(ans*n)%mod; + + n=(n*n)%mod; + + p/=2; + +} + +return ans; + +} + +int main() + +{ + + fin>>A>>N; + + mod=N; + + long long p=phi(N)-1; + + long long sol=1; + + + + + + fout< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol,p; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int PLog(int a,int n,int MOD) + +{ + + int sol=1; + + while(n>0) + + { + + if(n%2==1) + + sol=1LL*sol*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return sol; + +} + + + +int main() + +{ + + int b,n,a; + + fin>>a>>n; + + b=PLog(a,Phi(n)-1,n); + + fout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a, n; + + fin >> a >> n; + + fout << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/1293957a-9479-4781-ac13-5b48280fa87c.cpp b/invmod/euler-toutient/1293957a-9479-4781-ac13-5b48280fa87c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3161b9699aeac465340c0c5c12e4c8c9f427ec0 --- /dev/null +++ b/invmod/euler-toutient/1293957a-9479-4781-ac13-5b48280fa87c.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n; + + + +int Phi(int n) + +{ + + int sol, p; + + sol = n; + + for(p = 2 ; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + + + +} + + + +int LogP(int a, int n, int MOD) + +{ + + int sol = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + sol = 1LL * sol * a % MOD; + + n /= 2; + + a = 1LL * a * a % MOD; + + } + + return sol; + +} + + + +int main() + +{ + + int b; + + fin >> a >> n; + + b = LogP(a, Phi(n) - 1, n); + + fout << b; + + return 0; + +} diff --git a/invmod/euler-toutient/13322533-a0c8-480f-a44a-5b4d0f345702.cpp b/invmod/euler-toutient/13322533-a0c8-480f-a44a-5b4d0f345702.cpp new file mode 100644 index 0000000000000000000000000000000000000000..516c26d3a9f87fb1808be9a2ece5bc9945c61aeb --- /dev/null +++ b/invmod/euler-toutient/13322533-a0c8-480f-a44a-5b4d0f345702.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +int v[1000005]; + +int euler(int x){ + + int nr=x; + + int d=2; + + while(d*d<=x){ + + int ex=0; + + while(x%d==0){ + + x/=d; + + ++ex; + + } + + if(ex>0) + + nr=1LL*nr*(d-1)/d; + + ++d; + + } + + if(x>1) + + nr=1LL*nr*(x-1)/x; + + return nr; + +} + +int lg_pow(long long a,long long b,int MOD){ + + long long ans=1,p=a; + + for(long long i=1;i<=b;i<<=1){ + + if(i&b) + + ans=ans*p%MOD; + + p=p*p%MOD; + + } + + return ans; + +} + +int main(){ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a,n; + + scanf("%d %d",&a,&n); + + printf("%d\n",lg_pow(a,euler(n)-1,n)); + + return 0; + +} diff --git a/invmod/euler-toutient/147e753a-4ddb-45a4-abb3-c7cf66bccb14.cpp b/invmod/euler-toutient/147e753a-4ddb-45a4-abb3-c7cf66bccb14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c42fb810ab0b4209e0b358f626ecc563e9274964 --- /dev/null +++ b/invmod/euler-toutient/147e753a-4ddb-45a4-abb3-c7cf66bccb14.cpp @@ -0,0 +1,86 @@ +#include + + + + +using namespace std; + + + +long long N,M; + + + +long long phi(long long n) { + + long long result = n; + + + + for (long long p = 2; p * p <= n; ++p) { + + if (n % p == 0) { + + while (n % p == 0) + + n /= p; + + result *= (1.0 - (1.0 / (float)p)); + + } + + } + + if (n > 1) + + result *= (1.0 - (1.0 / (float)n)); + + + + return result; + +} + + + +int main(){ + + + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + + + scanf("%lld %lld\n",&N,&M); + + + + long long put = phi(M) - 1; + + long long nr = N; + + long long crt = 1; + + + + for(long long p = 1; p <= put; p <<= 1){ + + if (p & put) + + crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + + + printf("%lld",crt); + + + + return 0; + +} diff --git a/invmod/euler-toutient/154692e0-eefb-4d9c-92d0-d4301202ee53.cpp b/invmod/euler-toutient/154692e0-eefb-4d9c-92d0-d4301202ee53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcde2e1bdf57f9c46b2b4c5d5d6dc23d0eaf9fc0 --- /dev/null +++ b/invmod/euler-toutient/154692e0-eefb-4d9c-92d0-d4301202ee53.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long phi(long long n) + +{ + + long long cur=n; + + for (int i=2;i*i<=n;i++) + + { + + if (n%i==0) + + { + + while (n%i==0) + + { + + n/=i; + + } + + cur=cur*(i-1)/i; + + } + + } + + if (n!=1) cur=cur*(n-1)/n; + + return cur; + +} + +int main() + +{ + + long long a,n,x,b,p; + + fin >> a >> n; + + b=phi(n)-1; + + p=1; + + while (b) + + { + + if (b%2) p=(p*a)%n; + + a=(a*a)%n; + + b/=2; + + } + + x=p%n; + + fout << x; + +} diff --git a/invmod/euler-toutient/15809fe8-770f-40f3-9848-ca952152aef8.cpp b/invmod/euler-toutient/15809fe8-770f-40f3-9848-ca952152aef8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38c8d872547c9e06474cb97ab8d5d8f614fcb4a8 --- /dev/null +++ b/invmod/euler-toutient/15809fe8-770f-40f3-9848-ca952152aef8.cpp @@ -0,0 +1,101 @@ +#include +#include + + +using namespace std; + + + +int prod; + + + +void rid_put(int x, int put, int mod) { + + if (put == 0) + + return; + + if (put % 2 == 1) + + prod = (1ll * prod * x) % mod; + + x = (1ll * x * x) % mod; + + rid_put(x, put / 2, mod); + +} + + + +int phi(int x) { + + int d, sol; + + + + sol = x; + + for (d = 2; d * d <= x; d++) + + if (x % d == 0) { + + while (x % d == 0) + + x /= d; + + sol = sol / d * (d - 1); + + } + + + + if (x > 1) + + sol = sol / x * (x - 1); + + return sol; + +} + + + +int inv_mod(int a, int n) { + + int nr = phi(n); + + prod = 1; + + rid_put(a, nr - 1, n); + + return prod; + +} + + + +int main() { + + FILE *fin, *fout; + + int a, n, nr; + + + + fin = fopen("inversmodular.in", "r"); + + fscanf(fin, "%d%d", &a, &n); + + fclose( fin ); + + + + fout = fopen("inversmodular.out", "w"); + + fprintf(fout, "%d", inv_mod(a, n)); + + fclose( fout ); + + return 0; + +} diff --git a/invmod/euler-toutient/1667feb3-4073-4c2f-a8c0-9fe1c8bea612.cpp b/invmod/euler-toutient/1667feb3-4073-4c2f-a8c0-9fe1c8bea612.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3620b96e6b3ae65b63cd50107a897662a8ca2c9 --- /dev/null +++ b/invmod/euler-toutient/1667feb3-4073-4c2f-a8c0-9fe1c8bea612.cpp @@ -0,0 +1,58 @@ +#include + + +int n, m; + + + +int prod(int a, int b) { return (1ll * a * b) % m; } + + + +int putlog(int n, int k) { + + if (k == 0) return 1; + + int x = putlog(n, k / 2); + + if (k % 2 == 0) return prod(x, x); + + return prod(prod(x, x), n); + +} + + + +int phi(int x) { + + int ph = x; + + for(int i=2;i*i<=x;i++) + + if (!(x % i)) { + + while (!(x % i)) x /= i; + + ph = ph / i * (i - 1); + + } + + if (x - 1) ph = ph / x * (x - 1); + + return ph; + +} + + + +int main() { + + std::ifstream fin("inversmodular.in"); + + std::ofstream fout("inversmodular.out"); + + fin >> n >> m; + + fout << putlog(n, phi(m) - 1); + +} diff --git a/invmod/euler-toutient/16ef66f7-4849-40c2-999a-43297607a96a.cpp b/invmod/euler-toutient/16ef66f7-4849-40c2-999a-43297607a96a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ad2238b752a72eb40ee1cbb03ee245a1437c24 --- /dev/null +++ b/invmod/euler-toutient/16ef66f7-4849-40c2-999a-43297607a96a.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int GetPhi(int nr) + +{ + + int phi = nr; + + + + for(int i = 2; i * i <= nr; i++) + + if(nr % i == 0) + + { + + phi /= i; + + phi *= (i - 1); + + + + while(nr % i == 0) + + nr /= i; + + } + + + + if(nr != 1) + + { + + phi /= nr; + + phi *= (nr - 1); + + } + + + + return phi; + +} + + + +int lgput(int base, int exp, int N) + +{ + + int ans = 1; + + int aux = base; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = (1LL * ans * aux) % N; + + + + aux = (1LL * aux * aux) % N; + + } + + + + return ans; + +} + + + +int main() + +{ + + int A, N; fin >> A >> N; + + + + int phi = GetPhi(N); + + + + fout << lgput(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/17958399-7c2c-4741-ba70-7a0f7466460a.cpp b/invmod/euler-toutient/17958399-7c2c-4741-ba70-7a0f7466460a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4276ad0f3eba4f43daa0df549369012b0966e7fe --- /dev/null +++ b/invmod/euler-toutient/17958399-7c2c-4741-ba70-7a0f7466460a.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + + + +long long n,a; + + + +long long get_phi(long long n) + +{ + + int i=2; + + int phi=n; + + for (i=2;i<=sqrt(n);++i) + + { + + if (n%i==0) + + { + + while(n%i==0) n/=i; + + phi=(phi/i)*(i-1); + + } + + } + + if (n>1) phi=(phi/n)*(n-1); + + return phi; + +} + + + +long long power(long long baza, long long exp) + +{ + + long long aux=1; + + while(exp) + + { + + if (exp%2==0) + + { + + baza=(baza*baza)%n; + + exp/=2; + + } + + if (exp%2==1) + + { + + aux=(aux*baza)%n; + + --exp; + + } + + } + + return aux; + +} + + + +int main(void) + +{ + + ifstream cin("inversmodular.in"); + + ofstream cout("inversmodular.out"); + + cin>>a>>n; + + cout<<1LL*power(a%n,get_phi(n)-1); + + return 0; + +} diff --git a/invmod/euler-toutient/183f9e72-7a40-4e6c-bdd2-c8ff0e9e6ef2.cpp b/invmod/euler-toutient/183f9e72-7a40-4e6c-bdd2-c8ff0e9e6ef2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d42a8de526a44b047a413c3180a899a690582ce7 --- /dev/null +++ b/invmod/euler-toutient/183f9e72-7a40-4e6c-bdd2-c8ff0e9e6ef2.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +int phi(int x) +{ + int ans = x; + + for(int i = 2; i * i <= x; ++i) + if(x % i == 0) + { + while(x % i == 0) + x /= i; + + ans = ans / i * (i - 1); + } + + if(x > 1) + ans = ans / x * (x - 1); + + return ans; +} + +main() +{ + int x, y; + fin >> x >> y; + + int p = phi(y) - 1; + int ans = 1; + + for(; p; p >>= 1, x = (x * 1LL * x) % y) + if(p & 1) + ans = (ans * 1LL * x) % y; + + fout << ans << '\n'; +} diff --git a/invmod/euler-toutient/18e6e838-9ea6-4a0a-b81f-a9085004820f.cpp b/invmod/euler-toutient/18e6e838-9ea6-4a0a-b81f-a9085004820f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5bf6c3e57ae452159762ab2bffcd388b97468d0 --- /dev/null +++ b/invmod/euler-toutient/18e6e838-9ea6-4a0a-b81f-a9085004820f.cpp @@ -0,0 +1,83 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long lg_put(long long base, long long exp, long long mod) + +{ + + long long p =1; + + while(exp>0) + + { + + if(exp&1) + + p = (p*(base%mod))%mod; + + base = ((base%mod) * (base%mod))%mod; + + exp = exp >>1; + + } + + return p; + +} +long long phi(long long n) + +{ + + long long phi = n, d =2, e; + + while(d*d<=n and d >1) + + { + + e =0; + + while(n%d==0) + + { + + n = n/d; + + e++; + + } + + if(e>0) + + phi = phi/d*(d-1); + + d++; + + } + + if(n>1) + + phi = phi/n*(n-1); + + return phi; + +} + +int main() + +{ + + long long n, m; + + fin>>n>>m; + + fout< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +unsigned long long a,n,MOD; + +unsigned long long lgnp(unsigned long long n, unsigned long long p) + +{ + + n=n%MOD; + + unsigned long long r=1; + + while(p) + + { + + if(p%2==1) r=(1LL*r*n)%MOD; + + n=(n*n*1LL)%MOD; + + p/=2; + + } + + return r%MOD; + +} + +unsigned long long phi(unsigned long long n) + +{ + + unsigned long long d=2,fm,nr=1; + + do + + { + + fm=0; + + while(n%d==0) + + { + + n/=d; + + fm++; + + } + + if(fm>0) nr*=(d-1)*lgnp(d,fm-1); + + d++; + + if(d*d>n&&n>1) + + { + + nr=nr*(n-1); + + n=1; + + } + + } + + while(n>1); + + return nr; + +} + +int main() + +{ + + fin>>a>>n; + + MOD=n; + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + + + +int phi(int x) + +{ + + int p=x; + + for (int i=2;i*i<=x;i++) + + if (x%i==0) + + { + + while (x%i==0) + + x/=i; + + p=(p/i)*(i-1); + + } + + if (x!=1) + + p=(p/x)*(x-1); + + return p; + +} + + + +int inv_mod(int x, int mod) + +{ + + int p = phi(mod) - 1; + + int rez = 1; + + while (p) + + { + + if (p%2) + + rez = (1LL * rez * x) % mod; + + x = (1LL * x * x) % mod; + + p = p / 2; + + } + + return rez; + +} + + + +int main() + +{ + + f>>a>>n; + + g< + + +using namespace std; + +void euclid_e(int a, int b, int &x, int &y, int &d){ + + if ( b == 0 ){ + + d = a; + + x = 1; + + y = 0; + + return; + + } + + int xx, yy, q = a/b; + + euclid_e(b, a%b, xx, yy, d); + + x = yy; + + y = xx - yy*q; + +} + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int a, n, x, y, d, div, exp, nrd, r; + + fin >> a >> n; + + euclid_e(a, n, x, y, d); + + x %= n; + + if ( x < 0 ){ + + x += n; + + } + + fout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/1baa0ebc-da54-4ee4-ab84-8c4ff23f3e21.cpp b/invmod/euler-toutient/1baa0ebc-da54-4ee4-ab84-8c4ff23f3e21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2aee425c760ee4ce795eb0f497072228922b256 --- /dev/null +++ b/invmod/euler-toutient/1baa0ebc-da54-4ee4-ab84-8c4ff23f3e21.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +typedef long long ll; + +ll fast_exp(ll a, ll b, ll MOD){ + + if(b==0) return 1; + + if(b==1) return a% MOD; + + if(b%2==1) return a*fast_exp(a, b-1, MOD) % MOD; + + if(b%2==0) return fast_exp(a*a% MOD, b/2, MOD)% MOD; + +} + +ll phi(ll a){ + + ll cur = a; + + for(ll i = 2; i*i<=a; i++){ + + if(a%i==0){ + + while(a%i==0) a/=i; + + cur = (cur/i)*(i-1); + + } + + } + + if(a>1) cur = cur / a * (a-1); + + return cur; + +} + +int main(){ + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + ll n, m; + + scanf("%lld %lld", &n, &m); + + ll x = phi(m)-1, nr = n, ct = 1; + + ct = fast_exp(n, x, m); + + printf("%lld", ct); + +} diff --git a/invmod/euler-toutient/1c317c8a-6876-492b-8f5c-5fd3ca0a79b7.cpp b/invmod/euler-toutient/1c317c8a-6876-492b-8f5c-5fd3ca0a79b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d08a9dee526480bb3b84c4840500b1fe0de2ca64 --- /dev/null +++ b/invmod/euler-toutient/1c317c8a-6876-492b-8f5c-5fd3ca0a79b7.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int phi(long long m) + +{ + + long long k=m,i; + + for(i=2;i*i<=m;i++) + + if(m%i==0) + + { + + while(m%i==0)m/=i; + + k=k/i*(i-1); + + } + + if(m!=1) + + k=k/m*(m-1); + + return k; + +} + +int main() + +{ + + long long a,n,p,rez; + + f>>a>>n; + + p=phi(n)-1; + + rez=1; + + while(p) + + { + + if(p&1) rez=rez*a%n; + + a=a*a%n; + + p/=2; + + } + + g< +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int phi(long long m) + +{ + + long long k=m,i; + + for(i=2;i*i<=m;i++) + + if(m%i==0) + + { + + while(m%i==0)m/=i; + + k=k/i*(i-1); + + } + + if(m!=1) + + k=k/m*(m-1); + + return k; + +} + +int main() + +{ + + long long a,n,p,rez; + + f>>a>>n; + + p=phi(n)-1; + + rez=1; + + while(p) + + { + + if(p&1) rez=rez*a%n; + + a=a*a%n; + + p/=2; + + } + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/1e17ffdd-42ee-470a-b8df-7a346c6032d2.cpp b/invmod/euler-toutient/1e17ffdd-42ee-470a-b8df-7a346c6032d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c500369c6f64ef3a6d1dfec5f8496fa5b5d745 --- /dev/null +++ b/invmod/euler-toutient/1e17ffdd-42ee-470a-b8df-7a346c6032d2.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , i; + + for (i = 2 ; i * i <= n && n > 1 ; i++) + + { + + if (n % i == 0) + + sol = sol / i * (i - 1); + + while (n % i == 0) + + n /= i; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +long long log(int a , int n , int mod) + +{ + + long long p = 1; + + while (n > 0) + + { + + if (n % 2 == 1) p = 1LL*p * a % mod; + + n /= 2; + + a = 1LL*a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + cout << Phi(12); + + int a , n; + + fin >> a >> n; + + fout << log(a , Phi(n) - 1 , n); + + return 0; + +} diff --git a/invmod/euler-toutient/1e2a4681-ac90-46bd-a3c6-217684982794.cpp b/invmod/euler-toutient/1e2a4681-ac90-46bd-a3c6-217684982794.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09260257487118bc6bcc0231c14b17211587b34d --- /dev/null +++ b/invmod/euler-toutient/1e2a4681-ac90-46bd-a3c6-217684982794.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long po(long long nr,long long g){ + + long long t=1; + + for(long long i=1;i<=g;i++){ + + t*=nr; + + } + + return t; + +} + +long long phi(long long nr){ + +long long phi=nr; + +for(long long i=2;i*i<=nr;i++){ + + if(nr%i==0){ + + while(nr%i==0)nr/=i; + + phi=phi/i*(i-1); + + } + +} + +if(nr!=1)phi=phi/nr*(nr-1); + +return phi; + +} + +int main() + +{long long n,m; + + long long phinr,s=1; + + f>>n>>m; + + phinr=phi(m)-1; + + while(phinr){ + + if(phinr&1)s=(s*n)%m; + + n=(n*n)%m; + + phinr>>=1; + + } + + g< + + +using namespace std; + + + +ifstream in ("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long n, mod; + + + +long long GetPhi(long long n){ + + long long prod = n; + + for(int d = 2; d * d <= n; ++ d){ + + if(n % d == 0){ + + prod = prod / d; + + prod = prod * (d - 1); + + } + + while(n % d == 0) + + n /= d; + + } + + if(n > 1){ + + prod = prod / (n); + + prod = prod * (n - 1); + + } + + + + return prod; + +} + + + +long long LogPow(long long n, long long pow){ + + long long aux = 1; + + while(pow){ + + if(pow % 2){ + + aux = (aux * n) % mod; + + -- pow; + + } + + else{ + + n = (n * n) % mod; + + pow /= 2; + + } + + } + + return aux; + +} + + + +int main() + +{ + + in>>n>>mod; + + long long pow = GetPhi(mod) - 1; + + long long answer = LogPow(n, pow); + + out< + + +long long phi(long long n){ + + long long d=2,r=n; + + + + while(d*d<=n){ + + if(n%d==0){ + + while(n%d==0) + + n/=d; + + r=r/d*(d-1); + + } + + d++; + + } + + if(n>1) + + r=r/n*(n-1); + + return r; + +} + + + +long long exp(long long x, long long y, long long MOD){ + + long long r=1; + + + + while(y){ + + if(y%2) + + r=(r*x)%MOD; + + x=(x*x)%MOD; + + y/=2; + + } + + return r; + +} + + + +int main(){ + + FILE *fin=fopen("inversmodular.in","r"); + + FILE *fout=fopen("inversmodular.out","w"); + + long long a,n; + + + + fscanf(fin,"%lld%lld",&a,&n); + + fprintf(fout,"%lld\n",exp(a,phi(n)-1,n)); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invmod/euler-toutient/1f2b0f81-1238-4572-9e23-55dbe2a1683b.cpp b/invmod/euler-toutient/1f2b0f81-1238-4572-9e23-55dbe2a1683b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a6600eaea04677999e3ab60f6994fc0ef352445 --- /dev/null +++ b/invmod/euler-toutient/1f2b0f81-1238-4572-9e23-55dbe2a1683b.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long A,N; + +int fi(int n) + +{ + + int rez=n,d=2; + + while(d*d<=n) + + { + + if(n%d==0) + + { + + while(n%d==0) + + n/=d; + + rez=rez/d*(d-1); + + } + + d++; + + } + + if(n!=1) + + rez=rez/n*(n-1); + + return rez; + +} + +int lgput(int a,int b) + +{ + + long long rez=1; + + while(b) + + { + + if(b%2) + + rez=(1LL*rez*a)%N; + + a=(1LL*a*a)%N; + + b/=2; + + } + + return rez; + +} + +int main() + +{ + + in>>A>>N; + + out< + + +using namespace std; + +long long c,a,p,n,copie,nr,i; +long long euler(int n) + +{ + + p=n;copie=n; + + + + if(n%2==0) + + { + + p=p/2; + + while(n%2==0) n=n/2; + + } + + for(i=3; i*i<=n; i+=2) + + { + + if(n%i==0) + + { + + p=p/i; + + p*=(i-1); + + while(n%i==0) n=n/i; + + + + } + + } + + if(n>1) + + { + + p=p/n; + + p*=(n-1); + + } + + nr=p-1; + + p=a; + + a=1; + + while(nr) + + { + + if(nr%2==1) + + { + + a=a*p; + + a=a%copie; + + } + + p=p*p; + + p=p%copie; + + nr=nr/2; + + + + } + + + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld%lld",&a,&n); + + euler(n); + + printf("%lld",a%n); + + return 0; + +} diff --git a/invmod/euler-toutient/2140d528-f7d0-43ea-8abc-5aadc69bb235.cpp b/invmod/euler-toutient/2140d528-f7d0-43ea-8abc-5aadc69bb235.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1630fcbe2a4c1362b4e9966ca9df3aeab0557457 --- /dev/null +++ b/invmod/euler-toutient/2140d528-f7d0-43ea-8abc-5aadc69bb235.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long a,n; + +long long putere(long long a,long long e) + +{ long long p=1; + + while(e>0) + + { if(e%2==0) + + a*=a,a%=n,e/=2; + + else + + p*=a,p%=n,e--; + + } + + return p; + +} + +long long phi(long long x) + +{ long long p=1; + + for(long long i=2;i*i<=x;i++) + + { if(x%i!=0) + + continue; + + long long e=0,k=1; + + while(x%i==0) + + x/=i,e++,k*=i; + + k/=i; + + p=(i-1)*k; + + } + + if(x!=1) + + p*=(x-1); + + return p; + +} + +int main() + +{ in>>a>>n; + + out< + + +using namespace std; + + + + + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long sigma(long long n) + +{ + + long long rez = n; + + + + if(n % 2 == 0) + + { + + rez /= 2; + + + + while(n % 2 == 0) + + n /= 2; + + } + + + + for(long long i = 3; i * i <= n; i += 2) + + { + + if(n % i == 0) + + { + + rez /= i; + + rez *= (i - 1); + + + + while(n % i == 0) + + n /= i; + + } + + } + + + + if(n != 1) + + { + + rez /= n; + + rez *= (n - 1); + + } + + + + return rez; + +} + + + + + +long long lgput(long long n,long long p, long long b) + +{ + + long long rez = 1; + + long long x = n; + + + + while(p > 0) + + { + + if(p & 1) + + { + + rez = (rez * x) % b; + + } + + x = (x * x) % b; + + p >>= 1; + + } + + + + return rez; + +} + + + + main() + +{ + + long long a, b; + + in >> a >> b; + + + + long long p = sigma(b) - 1; + + + + out << lgput(a,p,b); + + + + return 0; + +} diff --git a/invmod/euler-toutient/22fc207b-10d5-4fda-8f0d-4464a7c3e3f8.cpp b/invmod/euler-toutient/22fc207b-10d5-4fda-8f0d-4464a7c3e3f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e135c8e8be1adc6e143fa8c8c13c3eb24d6715 --- /dev/null +++ b/invmod/euler-toutient/22fc207b-10d5-4fda-8f0d-4464a7c3e3f8.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int euler(int n){ + + long long d = 2, p, prod = n; + + while ( d * d <= n ) { + + p = 0; + + while ( n % d == 0 ) { + + n /= d; + + p ++; + + } + + if ( p ) + + prod = prod / d * ( d - 1 ); + + d ++; + + } + + if ( n > 1 ) + + prod = prod / n * ( n - 1 ); + + return prod; + +} + +int putere(int a,int n,int m){ + + int prod = 1; + + do{ + + if(n%2 != 0){ + + prod = (long long)prod * a % m; + + } + + a = 1LL * a * a % m; + + n /= 2; + + }while(n != 0); + + return prod; + +} + +int main() + +{ + + int a,n,x; + + cin >> a >> n; + + x = putere(a,euler(n) - 1,n); + + cout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/2690f28b-8a10-472b-a6f2-304749b2f9f6.cpp b/invmod/euler-toutient/2690f28b-8a10-472b-a6f2-304749b2f9f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12595ebe5467d5598e675c819e7ecc08520b9f17 --- /dev/null +++ b/invmod/euler-toutient/2690f28b-8a10-472b-a6f2-304749b2f9f6.cpp @@ -0,0 +1,61 @@ +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +int a,n; + +int phi(int n) +{ + int p = n; + int i,j,cnt = 0; + int sus = 1,jos = 1; + for (i=2; i*i<=n; i++) + { + if (n%i == 0) + { + while (n%i == 0) + { + n /= i; + } + sus *= (i-1); + jos *= i; + } + } + if (n != 1) + { + sus *= (n-1); + jos *= n; + } + return p/jos*sus; +} + +int ridica(int a,int p) +{ + int rasp = 1; + while (p != 0) + { + if (p%2 == 0) + { + a = (long long)a*a%n; + p /= 2; + } + else + { + rasp = (long long)rasp*a%n; + p--; + } + } + return rasp; +} + +int main() +{ + in >> a >> n; + + out << ridica(a,phi(n)-1); + return 0; +} diff --git a/invmod/euler-toutient/26d89849-1b05-4406-ae59-f10bf5bec331.cpp b/invmod/euler-toutient/26d89849-1b05-4406-ae59-f10bf5bec331.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c32a218b92b883c8a8ccddbb673761f3bb00d14 --- /dev/null +++ b/invmod/euler-toutient/26d89849-1b05-4406-ae59-f10bf5bec331.cpp @@ -0,0 +1,101 @@ +#include +#include + + + + +using namespace std; + + + +ifstream x("inversmodular.in"); + +ofstream y("inversmodular.out"); + + + +long long a,n; + + + +long long get(long long M) + +{ + + long long cr=M; + + for(long long i=2;i*i<=M;++i) + + { + + if(M%i==0) + + { + + while(M%i==0) + + { + + M/=i; + + } + + cr=(cr/i)*(i-1); + + } + + } + + if(M!=1) + + { + + cr=cr/M*(M-1); + + } + + return cr; + +} + + + +long long inv(long long N, long long M) + +{ + + long long nr=N,cr=1,put=get(M)-1; + + for(long long i=1;i<=put;i<<=1) + + { + + if(i&put) + + cr=(cr*nr)%M; + + nr=(nr*nr)%M; + + } + + return cr; + +} + + + +int main() + +{ + + x>>a>>n; + + y< + + +using namespace std; + +ifstream fi("inversmodular.in"); + +ofstream fo("inversmodular.out"); + +long long a,n,p; + +long long phi_n(long long n) + +{ + + long long sol=n,d; + + for(d=2;d*d<=n;d++) + + if (n%d==0) + + { + + while(n%d==0)n/=d; + + sol= (sol/d)*(d-1); + + } + + if (n!=1) sol=sol/n*(n-1); + + return sol; + +} +long long putere(long long n,long long p,long long MOD) + +{ + +long long r=1; + +while(p){ + + if(p%2)r=(1LL*r*n)%MOD; + + n=(1LL*n*n)%MOD; + + p/=2; + + } + +return r; + +} + +int main() + +{ + +fi>>a>>n; + +p=phi_n(n)-1; + +fo< + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int a,n; + + + +int logpow2(int a,int p) + +{ + + int rez=1,inm=a; + + + + while(p) + + { + + if(p%2) + + rez*=inm; + + + + p/=2; + + inm=inm*inm; + + } + + + + return rez; + +} + + + +int coprime(int nr) + +{ + + int fact[101],nrFact[101],t=0; + + int sum=1; + + + + int radNr=sqrt(nr); + + for(int d=2;d<=radNr && nr!=1;d++) + + if(nr%d==0) + + { + + fact[++t]=d; + + nrFact[t]=0; + + + + while(nr%d==0) + + { + + nrFact[t]++; + + nr/=d; + + } + + } + + + + if(nr!=1) + + { + + fact[++t]=nr; + + nrFact[t]=1; + + } + + + + for(int i=1;i<=t;i++) + + sum*=logpow2(fact[i],nrFact[i]-1)*(fact[i]-1); + + + + return sum; + +} + + + +int logpow(int a,int p) + +{ + + long long rez=1; + + long long inm=a; + + + + while(p) + + { + + if(p%2) + + rez=(rez*inm)%n; + + + + p/=2; + + inm=(inm*inm)%n; + + } + + + + return rez; + +} + + + +int main() + +{ + + in>>a>>n; + + + + int p=coprime(n); + + int rez=logpow(a,p-1); + + + + out< +#include +using namespace std; + + + + + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} +int Phi(int n) + +{ + + int sol=n,p; + + for(p=2;p*p<=n and n>1;p++) + + { + + if(n%p==0) + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + if(n>1) + + sol=sol/n*(n-1); + + return sol; + +} + + + +int main() + +{ + + int a,phi,n; + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin>>a>>n; + + phi=Phi(n); + + fout< +using namespace std; + +long long rid_put(int nr,int exp,int mod) + +{ + + long long rez=1,val=nr; + + while(exp) + + { + + if(exp&1)rez=rez*val; + + val=val*val; + + rez%=mod; + + val%=mod; + + exp=exp>>1; + + } + + return rez; + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int n,k; + + scanf("%d%d",&n,&k); + + int d=2,e=0; + + int prim=k; + + int l=k; + + while(d*d<=k) + + { + + e=0; + + while(k%d==0) + + { + + k/=d; + + e++; + + } + + if(e) + + prim/=d,prim*=d-1; + + d++; + + } + + if(k>1)prim/=k,prim*=k-1; + + + + cout< +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int n, a; + +long long x; + + + +int prim(int n) + +{ + + if(n<2) + + return 0; + + if(n%2==0 && n>2) + + return 0; + + for(int d=3;d*d<=n;d+=2) + + if(n%d==0) + + return 0; + + return 1; + +} + + + +long long P(int a, int n, int mod) + +{ + + long long p=1; + + while(n) + + { + + if(n%2==1) + + p=((p%mod)*(a%mod))%mod; + + a=(1LL*(a%mod)*(a%mod))%mod; + + n/=2; + + } + + return p; + +} + + + +int Phi(int n) + +{ + + int r=n, d=2; + + while(n>1) + + { + + if(n%d==0) + + { + + r=r/d*(d-1); + + while(n%d==0) + + n/=d; + + } + + d++; + + if(d*d>n) + + d=n; + + } + + return r; + +} + + + +int main() + +{ + + fin >> a >> n; + + x = P(a,Phi(n)-1,n); + + fout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/2bdf9272-b679-4f6e-9ca0-74a2e961fc85.cpp b/invmod/euler-toutient/2bdf9272-b679-4f6e-9ca0-74a2e961fc85.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779d8e7928b2cac705cfc57683ef2015013f4ef1 --- /dev/null +++ b/invmod/euler-toutient/2bdf9272-b679-4f6e-9ca0-74a2e961fc85.cpp @@ -0,0 +1,81 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +int Phi(int n) + +{ + + int p,sol; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + { + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} + + + +int main() + +{ + + int a,n; + + fin>>a>>n; + + fout< +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/2ceb57dc-6c92-4f89-9ba7-e47459dbdcb8.cpp b/invmod/euler-toutient/2ceb57dc-6c92-4f89-9ba7-e47459dbdcb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2829269396b96d15ad163a14fd70f2735d60b9a7 --- /dev/null +++ b/invmod/euler-toutient/2ceb57dc-6c92-4f89-9ba7-e47459dbdcb8.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int euclid( int a, int b, int &x, int &y ){ + + if( b == 0 ){ + + x = 1; + + y = 0; + + return a; + + }else{ + + int x0, y0; + + int cmmdc = euclid( b, a % b, x0, y0 ); + + x = y0; + + y = x0 - y0 * (a / b); + + return cmmdc; + + } + +} + +int A, N; + +int main(){ + + fin >> A >> N; + + int invmod, aux; + + int d = euclid( A, N, invmod, aux ); + + while( invmod < 0 ) + + invmod += N; + + fout << invmod << endl; + + return 0; + +} diff --git a/invmod/euler-toutient/2cfd4c8b-ed77-4902-9ad1-0347cbbcd257.cpp b/invmod/euler-toutient/2cfd4c8b-ed77-4902-9ad1-0347cbbcd257.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a6cd71f08a19c113af495b648dd802eaf6c9e0c --- /dev/null +++ b/invmod/euler-toutient/2cfd4c8b-ed77-4902-9ad1-0347cbbcd257.cpp @@ -0,0 +1,83 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a, n; +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + fin >> a >> n; + + fout << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/2d479b3a-075e-4f05-9bba-1b09a88ec757.cpp b/invmod/euler-toutient/2d479b3a-075e-4f05-9bba-1b09a88ec757.cpp new file mode 100644 index 0000000000000000000000000000000000000000..955b0888dce5c48a73c6f19574ef304d5f3fc3a8 --- /dev/null +++ b/invmod/euler-toutient/2d479b3a-075e-4f05-9bba-1b09a88ec757.cpp @@ -0,0 +1,79 @@ +#include +#include + + +using namespace std; + +long long phi(long long x) { + + long long answer = x; + + for (int i = 2; i * i <= x; ++i) { + + if (x % i == 0) { + + answer = answer / i * (i - 1); + + while (x % i == 0) { + + x = x / i; + + } + + } + + } + + if (x > 1) { + + answer = answer / x * (x - 1); + + } + + return answer; + +} + + + +long long power(long long a, long long b, long long mod) { + + if (b == 0) { + + return 1; + + } + + if (b % 2 == 0) { + + return power((1LL * a * a) % mod, b / 2, mod); + + } else { + + return ((1LL * a * power(a, b - 1, mod)) % mod) ; + + } + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + long long n, mod; + + fin >> n >> mod; + + fout << power(n, phi(mod) - 1, mod); + + + + return 0; + +} diff --git a/invmod/euler-toutient/2ef383a7-f7d4-4a42-935c-7cc02c22ef52.cpp b/invmod/euler-toutient/2ef383a7-f7d4-4a42-935c-7cc02c22ef52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72bed6bd95521ed193cb5cb982750404e8985c7e --- /dev/null +++ b/invmod/euler-toutient/2ef383a7-f7d4-4a42-935c-7cc02c22ef52.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +unsigned long long a, MOD, x, d, fi, fm, i; + +unsigned long long rez; + +unsigned long long paw(int a, int b) + +{ + + unsigned long long r = 1, A = a; + + if(b == 0)return 1; + + while(b > 1) + + { + + if(b % 2 == 1)r = (r * A) % MOD; + + A = (A * A) % MOD; + + b = b / 2; + + } + + return (r * A) % MOD; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> MOD; + + + + x = MOD; + + d = 2; + + fi = 1; + + do + + { + + fm = 0; + + while(x % d == 0) + + { + + x = x / d; + + fm ++; + + } + + if(fm > 0)fi = (fi * paw(d, fm - 1) * (d - 1)) % MOD; + + d ++; + + if(d * d > x && x != 1)fi = (fi * (x - 1)) % MOD, x = 1; + + }while(x != 1); + + g << paw(a, fi - 1) % MOD << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/2f0775ab-b2c6-4a7b-af00-35ad50291474.cpp b/invmod/euler-toutient/2f0775ab-b2c6-4a7b-af00-35ad50291474.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2517648b95973534456b9c3c4c62559dd579ff5 --- /dev/null +++ b/invmod/euler-toutient/2f0775ab-b2c6-4a7b-af00-35ad50291474.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long phi,n; + + + +void descomp(long long n) + +{ + + for(long long i=2;i*i<=n;i++) + + if(n%i==0) + + { + + while(n%i==0) + + n/=i; + + phi/=i; + + phi*=(i-1); + + } + + if(n!=1) + + phi/=n,phi*=(n-1); + +} + + + + + +long long ridica (long long baza,long long exp) + +{ + + long long sol=1; + + while (exp!=0) + + { + + if (exp%2==1) + + { + + sol=sol*baza; + + sol%=n; + + } + + baza=baza*baza; + + baza%=n; + + exp/=2; + + } + + return sol; + +} + + + +int main() + +{ + + long long a; + + fin>>a>>n; + + phi=n; + + descomp (n); + + fout< +long long N,A; + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long Phi(long long n) + +{ + + long long c=n; + + for(long long i=2;i*i<=n;++i) + + { + + if (n%i==0) + + { + + while(n%i==0) n/=i; + + c=(c/i)*(i-1); + + } + + } + + if(n!=1) c=c/n*(n-1); + + return c; + +} + + + +int main() + +{ + + fin>>A>>N; + + long long p=Phi(N)-1; + + long long n=A; + + long long c=1; + + for(long long i=1;i<=p;i<<=1) + + { + + if(i&p)c=(c*n)%N; + + n=(n*n)%N; + + } + + fout< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,a,phi; + +long long phi_euler(long long n) + +{ + + long long nn = n, ans = n; + + for(long long i = 2; i * i <= n; i ++) + + { + + if(nn % i == 0) + + { + + ans *= (i-1); + + ans /= i; + + while(nn % i == 0) + + nn /= i; + + } + + } + + if(nn != 1) + + { + + ans *= (nn-1); + + ans /= nn; + + } + + return ans; + +} + +long long lgput(long long base,long long exp) + +{ + + long long i; + + long long aux=base,ans=1; + + for(i=1; i<=exp; i*=2) + + { + + if(i&exp) + + ans=(1LL*ans*aux)%n; + + aux=(1LL*aux*aux)%n; + + } + + return ans; + +} + + + +int32_t main() + +{ + + fin>>a>>n; + + + + phi=phi_euler(n); + + + + fout< +#include + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,m; + +long long phi(long long nr){ + + long long rez=nr; + + for(int i=2;i*i<=nr;i++){ + + if(nr%i==0){ + + while(nr%i==0) nr/=i; + + rez=(rez/i)*(i-1); + + } + + } + + if(nr!=1) rez=rez/nr*(nr-1); + + return rez; + +} + +int main(){ + + fin>>n>>m; + + long long nr=n,crt=1,put=phi(m)-1; + + for(long long p=1;p<=put;p<<=1){ + + if(p&put) crt=(crt*nr)%m; + + nr=(nr*nr)%m; + + } + + fout< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +struct A + +{ + + int nr, val; + +}; + + + +vector < A > v; + +int MOD; + + + +long long n_la_puterea_k ( long long n, long long k ); + +long long indicator_Euler ( long long x ); + + + +int main() + +{ + + long long x; + + + + fin >> x >> MOD; + + fout << n_la_puterea_k ( x, indicator_Euler ( MOD ) - 1 ); + + + + return 0; + +} + + + +long long n_la_puterea_k ( long long n, long long k ) + +{ + + if ( k == 0 ) return 1; + + else if ( k == 1 ) return n % MOD; + + else if ( k % 2 == 0 ) return n_la_puterea_k ( ( n * n ) % MOD, k / 2 ); + + else return ( ( n % MOD ) * n_la_puterea_k ( ( n * n ) % MOD, k / 2 ) ) % MOD; + +} + + + +long long indicator_Euler ( long long x ) + +{ + + long long i, nr, val, r = 1; + + + + if ( x % 2 == 0 ) + + { + + nr = 2, val = 0; + + while ( x % 2 == 0 ) val++, x /= 2; + + v.push_back ( { nr, val } ); + + } + + + + for ( i = 3 ; i * i <= x ; i += 2 ) + + if ( x % i == 0 ) + + { + + nr = i, val = 0; + + while ( x % i == 0 ) val++, x /= i; + + v.push_back ( { nr, val } ); + + } + + + + if ( x != 1 ) v.push_back ( { x, 1 } ); + + for ( i = 0 ; i < v.size() ; i++ ) r *= ( v[i].nr - 1 ) * n_la_puterea_k ( v[i].nr, v[i].val - 1 ); + + + + return r; + +} diff --git a/invmod/euler-toutient/3223a5c4-900f-4906-a2df-09c5c0ed6884.cpp b/invmod/euler-toutient/3223a5c4-900f-4906-a2df-09c5c0ed6884.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d10802ee71142cb7f3030c67d07002899ee3a65f --- /dev/null +++ b/invmod/euler-toutient/3223a5c4-900f-4906-a2df-09c5c0ed6884.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +int lgput(int a, int b, int mod) { + int ret = 1; + while(b) { + if(b&1) ret = 1ll*ret*a%mod; + a = 1ll*a*a%mod; + b>>=1; + } + + return ret; +} +int main() { + + + + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + int a, n; + cin >> a >> n; + + int phi = n; + int cop = n; + if(cop % 2 == 0){ + phi /= 2; + while(cop % 2 == 0) cop >>= 1; + } + + int d = 3; + while(d*d <= cop) { + if(cop % d == 0) { + phi /= d; + phi *= d-1; + } + while(cop % d == 0) { + cop /= d; + } + d += 2; + } + + if(cop != 1) { + phi /= cop; + phi *= cop-1; + } + + cout << lgput(a, phi - 1, n) << '\n'; + return 0; +} diff --git a/invmod/euler-toutient/3285f0a1-6096-4864-a316-e98d2c76aee3.cpp b/invmod/euler-toutient/3285f0a1-6096-4864-a316-e98d2c76aee3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f51924d3fd15a705628a7bfc2995b2e3138a806 --- /dev/null +++ b/invmod/euler-toutient/3285f0a1-6096-4864-a316-e98d2c76aee3.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +ifstream in ("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long n, mod; + + + +long long GetPhi(long long n){ + + long long prod = n; + + for(int d = 2; d * d <= n; ++ d){ + + if(n % d == 0){ + + prod = prod / d; + + prod = prod * (d - 1); + + } + + while(n % d == 0) + + n /= d; + + } + + if(n > 1){ + + prod = prod / (n); + + prod = prod * (n - 1); + + } + + + + return prod; + +} + + + +long long LogPow(long long n, long long pow){ + + long long aux = 1; + + while(pow){ + + if(pow % 2){ + + aux = (aux * n) % mod; + + -- pow; + + } + + else{ + + n = (n * n) % mod; + + pow /= 2; + + } + + } + + return aux; + +} + + + +int main() + +{ + + in>>n>>mod; + + long long pow = GetPhi(mod) - 1; + + long long answer = LogPow(n, pow); + + out< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +int Fact(int n, int P) + +{ + + int rez = 1; + + for(int i = 2;i <= n;i++) + + rez = rez * i % P; + + return rez; + +} + + + +int LogP(int a, int n, int P) + +{ + + int rez = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + rez = 1LL * rez * a % P; + + n /= 2; + + a = 1LL * a * a % P; + + } + + return rez; + +} + + + +int Phi(int n) + +{ + + int f,exp,phi = n; + + f = 2; + + while(n > 1) + + { + + exp = 0; + + while(n % f == 0) + + { + + n /= f; + + exp++; + + } + + if(exp > 0) phi = phi / f * (f - 1); + + if(f * f < n) f++; + + else f = n; + + } + + return phi; + +} + + + +int main() + +{ + + int a, N; + + fin >> a >> N; + + fout << LogP(a, Phi(N) - 1, N); + + return 0; + +} diff --git a/invmod/euler-toutient/3536665a-4e26-4c31-9888-dde0402b5fca.cpp b/invmod/euler-toutient/3536665a-4e26-4c31-9888-dde0402b5fca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12791929f1cc6a2909bec2e33e03586723fd9d69 --- /dev/null +++ b/invmod/euler-toutient/3536665a-4e26-4c31-9888-dde0402b5fca.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int a , n; + + + +inline int Lgput(int x , int p) + +{ + + int s = 1; + + while(p) + + { + + if(p & 1) + + s = 1LL * s * x % n; + + p /= 2; + + x = 1LL * x * x % n; + + } + + return s; + +} + + + +inline int Fi(int x) + +{ + + int fi = n , d = 2; + + while(d * d <= x && x > 1) + + { + + if(x % d == 0) + + { + + fi = fi / d * (d - 1); + + while(x % d == 0) + + x /= d; + + } + + d++; + + } + + if(x > 1) + + fi = fi / x * (x - 1); + + return fi; + +} + + + +int main() + +{ + + int p; + + fin >> a >> n; + + p = Fi(n); + + fout << Lgput(a , p - 1) << "\n"; + + fin.close(); + + fout.close(); + +} diff --git a/invmod/euler-toutient/35ae18bc-e40d-42b0-b6ac-60b87bbf3839.cpp b/invmod/euler-toutient/35ae18bc-e40d-42b0-b6ac-60b87bbf3839.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5be941a294834f5a7e5aafbf9cf79cc9ec664917 --- /dev/null +++ b/invmod/euler-toutient/35ae18bc-e40d-42b0-b6ac-60b87bbf3839.cpp @@ -0,0 +1,75 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int LogP(int a, int n, int mod) + +{ + + int p=1; + + while (n) + + { + + if (n%2==1) + + p=1ll*p*a%mod; + + a=1ll*a*a%mod; + + n/=2; + + } + + return p; + +} + +int phi(int n) + +{ + + int sol=n,p; + + for (p=2; n>1 and p*p<=n; p++) + + { + + if (n%p==0) + + sol=sol/p*(p-1); + + while (n%p==0) + + n/=p; + + + + } + + if (n>1) + + sol=sol/n*(n-1); + + return sol; + +} + +int main() + +{ + + int a,n; + + fin >> a >> n; + + fout << (LogP(a,(phi(n)-1),n)); + return 0; + +} diff --git a/invmod/euler-toutient/35b18dfd-dc6b-4325-a82f-d66a44d91448.cpp b/invmod/euler-toutient/35b18dfd-dc6b-4325-a82f-d66a44d91448.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be6138ea7358d615250d454187fe93938a820728 --- /dev/null +++ b/invmod/euler-toutient/35b18dfd-dc6b-4325-a82f-d66a44d91448.cpp @@ -0,0 +1,79 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long MOD; + + + +long long pow_log(long long a,long long k) + +{ + + long long sol=1; + + while(k!=0) + + { + + if(k%2==1) + + sol*=a,sol%=MOD;; + + a*=a; + + a%=MOD; + + k/=2; + + } + + return sol; + +} + + + +int main() + +{ + + long long a,n,sol,i; + + f>>a>>n; + + sol=n; + + MOD=n; + + for(i=2;i*i<=n;i++) + + if(n%i==0) + + { + + while(n%i==0) + + n/=i; + + sol/=i; + + sol*=(i-1); + + } + + if(n!=1) + + sol/=n,sol*=(n-1); + + sol=pow_log(a,sol-1); + + g< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int GetPhi(int x) + +{ + + int phi = x; + + + + for(long long i = 2; 1LL * i * i <= x; i++) + + if(x % i == 0) + + { + + phi /= i; + + phi *= (i - 1); + + + + while(x % i == 0) + + x /= i; + + } + + + + if(x != 1) + + { + + phi /= x; + + phi *= (x - 1); + + } + + + + return phi; + +} + + + +long long RidPut(int base, int exp, int mod) + +{ + + long long ans = 1; + + long long aux = base % mod; + + + + for(long long i = 1; 1LL * i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = (1LL * ans * aux) % mod; + + + + aux = (1LL * aux * aux) % mod; + + } + + + + return ans % mod; + +} + + + +int main() + +{ + + int A, N; + + + + fin >> A >> N; + + + + int phi = GetPhi(N); + + + + fout << RidPut(A, phi - 1, N) << '\n'; + + + + return 0; + +} diff --git a/invmod/euler-toutient/385466ac-ee87-4bd3-84d5-c1abe66d51c5.cpp b/invmod/euler-toutient/385466ac-ee87-4bd3-84d5-c1abe66d51c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b5d20b7c35bdc539a3152404d6122bd00ec01cd --- /dev/null +++ b/invmod/euler-toutient/385466ac-ee87-4bd3-84d5-c1abe66d51c5.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long A, N; + + + +void read() + +{ + + f >> A >> N; + +} + + + +long long expLg(long long x, long long p) + +{ + + long long y = 1; + + while (p > 1) { + + if (p & 1) + + y = (x * y) % N; + + x = (x * x) % N; + + p >>= 1; + + } + + return (x * y) % N; + +} + + + +long long phi(long long x) + +{ + + long long res = x; + + for (int i = 2; i * i <= x; ++i) + + if (x % i == 0) { + + while (x % i == 0) + + x /= i; + + res = (res / i) * (i - 1); + + } + + if (x > 1) + + res = (res / x) * (x - 1); + + return res; + +} + + + +void show() + +{ + + g << expLg(A, phi(N) - 1); + +} + + + +int main() + +{ + + read(); + + show(); + + return 0; + +} diff --git a/invmod/euler-toutient/38b98584-ec39-4b44-af32-c0e81c9bf80a.cpp b/invmod/euler-toutient/38b98584-ec39-4b44-af32-c0e81c9bf80a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72bed6bd95521ed193cb5cb982750404e8985c7e --- /dev/null +++ b/invmod/euler-toutient/38b98584-ec39-4b44-af32-c0e81c9bf80a.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +unsigned long long a, MOD, x, d, fi, fm, i; + +unsigned long long rez; + +unsigned long long paw(int a, int b) + +{ + + unsigned long long r = 1, A = a; + + if(b == 0)return 1; + + while(b > 1) + + { + + if(b % 2 == 1)r = (r * A) % MOD; + + A = (A * A) % MOD; + + b = b / 2; + + } + + return (r * A) % MOD; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> MOD; + + + + x = MOD; + + d = 2; + + fi = 1; + + do + + { + + fm = 0; + + while(x % d == 0) + + { + + x = x / d; + + fm ++; + + } + + if(fm > 0)fi = (fi * paw(d, fm - 1) * (d - 1)) % MOD; + + d ++; + + if(d * d > x && x != 1)fi = (fi * (x - 1)) % MOD, x = 1; + + }while(x != 1); + + g << paw(a, fi - 1) % MOD << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/38c73b43-cff0-4aaf-8c5a-24211df9bc30.cpp b/invmod/euler-toutient/38c73b43-cff0-4aaf-8c5a-24211df9bc30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e985fc17799759b6fe636a3acb2eebc42b5f6d99 --- /dev/null +++ b/invmod/euler-toutient/38c73b43-cff0-4aaf-8c5a-24211df9bc30.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +int a,n; + +int euler(int x) + +{ + + int e=1; + + if(x%2==0) + + { + + x/=2; + + while(x%2==0) + + { + + x/=2; + + e*=2; + + } + + } + + for(int d=3;d*d<=x;d+=2) + + if(x%d==0) + + { + + x/=d; + + e*=d-1; + + while(x%d==0) + + { + + x/=d; + + e*=d; + + } + + } + + if(x>1) + + e*=x-1; + + return e; + +} + +int putere(int b,int e) + +{ + + if(e==0) + + return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1) + + r=1LL*r*b%n; + + return r; + + + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%d%d",&a,&n); + + printf("%d",putere(a,euler(n)-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/39065b66-f3c4-4099-a22a-c897a4bf5ce6.cpp b/invmod/euler-toutient/39065b66-f3c4-4099-a22a-c897a4bf5ce6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99427e89d367f0478d86c958f0d64a1d7091611e --- /dev/null +++ b/invmod/euler-toutient/39065b66-f3c4-4099-a22a-c897a4bf5ce6.cpp @@ -0,0 +1,91 @@ +#include +#include + + +using namespace std; + + + +int phi( int n ) { + + int d = 2, val, rez = 1; + + while ( d * d <= n ) { + + val = 1; + + while ( n % d == 0 ) { + + val *= d; + + n /= d; + + } + + if ( val > 1 ) + + val = val / d * ( d - 1 ); + + rez *= val; + + d ++; + + } + + if ( n > 1 ) + + rez *= n - 1; + + return rez; + +} + + + +int exponentiere( long long b, int e, int mod ) { + + long long produs = 1; + + while ( e > 0 ) { + + if ( e % 2 == 0 ) { + + b = b * b % mod; + + e /= 2; + + } else { + + produs = produs * b % mod; + + e --; + + } + + } + + return produs; + +} + +int main() { + + ifstream fin( "inversmodular.in" ); + + ofstream fout( "inversmodular.out" ); + + int n, mod, e, i; + + fin >> n >> mod; + + e = phi( mod ) - 1; + + fout << exponentiere( n, e, mod ) << endl; + + fout.close(); + + fin.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/393d2a83-41c1-459f-b180-2c266dfcfab4.cpp b/invmod/euler-toutient/393d2a83-41c1-459f-b180-2c266dfcfab4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f97ddcf3168dd96a0f8b9c79a1340dc6a8b4207b --- /dev/null +++ b/invmod/euler-toutient/393d2a83-41c1-459f-b180-2c266dfcfab4.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + + + +const long long mod=1e9+7; + + + +long long a,n; + + + +long long phi(long long x){ + + long long rs=x; + + for(long long i=2LL;i*i<=x;i++){ + + if(x%i==0LL){ + + rs=((rs/i)*(i-1LL)); + + while(x%i==0LL) x/=i; + + } + + } + + if(x>=2LL) rs=((rs/x)*(x-1LL)); + + return rs; + +} + + + +long long lgp(long long a,long long b){ + + if(b==0LL) return 1LL; + + else if(b==1LL) return (a%n); + + else{ + + long long cc=(lgp(a,b/2LL)%n); + + if(b%2LL==1LL) return ((((cc%n)*(cc%n))%n)*(a%n))%n; + + else return ((cc%n)*(cc%n))%n; + + } + +} + + + +int32_t main(){ + + + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin >> a >> n; + + fout << lgp(a,phi(n)-1LL)%n; + +} diff --git a/invmod/euler-toutient/3999e403-eb7c-4c40-a3e4-10af4cd6f5ac.cpp b/invmod/euler-toutient/3999e403-eb7c-4c40-a3e4-10af4cd6f5ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0452400855f95b2d051a20a9e4e8721f084541df --- /dev/null +++ b/invmod/euler-toutient/3999e403-eb7c-4c40-a3e4-10af4cd6f5ac.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long a, n, i, diviz; + +long long explog(long long x1, long long n1) + +{ + + long long p=1; + + long long x=x1; + + while(n1>0) + + { + + if(n1&1) + + { + + p=(p*x)%n; + + + + n1--; + + } + + x=(x*x)%n; + + n1>>=1; + + } + + return p; + +} + +long long ptphi(long long x) + +{ + + long long temp=x; + + for(i=2; i*i<=x; i++) + + { + + if(x%i==0) + + { + + while(x%i==0) {x/=i;} + + temp=temp/i*(i-1); + + } + + } + + if(x>1) temp=temp/x*(x-1); + + return temp; + +} + +int main() + +{ + + in>>a>>n; + + out< + + +using namespace std; + + + +int N, A; + + + +int GetPhi(int x) + +{ + + int phi = x; + + for (int i = 2;i * i <= x;++i) + + { + + if (x % i == 0) + + { + + while (x % i == 0) + + x /= i; + + phi = phi / i * (i - 1); + + } + + } + + if (x != 1) + + phi = phi / x * (x - 1); + + return phi; + +} + + + +int lgput(int base, int exp) + +{ + + int res = 1; + + while (exp > 0) + + { + + if (exp % 2) + + res = (1LL * res * base) % N; + + base = (1LL * base * base) % N; + + exp /= 2; + + } + + return res; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin >> A >> N; + + fout << lgput(A, GetPhi(N) - 1) << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/3a3099cb-394b-4704-81da-234ca540a597.cpp b/invmod/euler-toutient/3a3099cb-394b-4704-81da-234ca540a597.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60ece56c063e07d7057c6a1e777bfbc301c7de5f --- /dev/null +++ b/invmod/euler-toutient/3a3099cb-394b-4704-81da-234ca540a597.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +typedef long long ll; + +ll a,n; + +ll phi(ll nr) + +{ + + ll ans=nr; + + for(int d=2;d*d<=nr;d++) + + { + + if(nr%d==0) + + { + + while(nr%d==0) + + nr/=d; + + ans-=ans/d; + + } + + } + + if(nr>1)ans-=ans/nr; + + return ans-1; + +} + +ll exp(ll a, ll p) + +{ + + ll rez=1; + + while(p) + + { + + if(p%2==0) + + { + + a=(a*a)%n; + + p/=2; + + } + + else + + { + + rez=(rez*a)%n; + + p--; + + } + + } + + return rez; + +} + +int main() + +{ + + fin>>a>>n; + + fout< +#include +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +const int NMax = 1e5; + +vector PrimeList; + + + +int A, N, Nprim, Prime[NMax], Div[NMax], NrDivs; + +long long Sol = 1; + + + +int LogPow(int X, int P,int MOD) { + + long long ans = 1; + + while (P) { + + if (P % 2 == 1) + + ans = (1LL * ans * X) % MOD; + + X = (1LL * X * X) % MOD; + + P /= 2; + + } + + return ans; + +} + + + +void Sieve() + +{ + + for(int i = 2; i <= NMax; i++) + + if(Prime[i] == 0) + + { + + PrimeList.push_back(i); + + + + for(int j = 2 * i; j <= NMax; j += i) + + Prime[j] = 1; + + } + +} + + + +void Decompose() + +{ + + Nprim = N; + + for(auto x : PrimeList) + + { + + if(x * x > N) break; + + if(N % x) continue; + + + + while(N % x == 0) + + N /= x; + + + + Div[++NrDivs] = x; + + } + + if(N > 1) Div[++NrDivs] = N; + +} + + + +void Solve() { + + Sol = Nprim; + + for (int i = 1; i <= NrDivs; i++) { + + Sol *= (Div[i] - 1); + + Sol /= Div[i]; + + } + +} + + + +int main() { + + in >> A >> N; + + Sieve(); + + Decompose(); + + Solve(); + + out << LogPow(A,Sol-1,Nprim); + +} diff --git a/invmod/euler-toutient/3a7a110d-2072-4a61-9ec3-b36fdc050ece.cpp b/invmod/euler-toutient/3a7a110d-2072-4a61-9ec3-b36fdc050ece.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f3f1e2c20a7d9e2f39379df308f0218959e7d0e --- /dev/null +++ b/invmod/euler-toutient/3a7a110d-2072-4a61-9ec3-b36fdc050ece.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + + + +long long a, n, nr; + + + +void descomp(long long x) + +{ + + if(x%2 == 0) + + { + + while(x%2 == 0) + + { + + x/=2; + + } + + nr=nr/2; + + } + + for(int i=3; i*i<=x; i+=2) + + { + + if(x%i == 0) + + { + + while(x%i == 0) + + { + + x/=i; + + } + + nr=nr/i*(i-1); + + } + + } + + if(x != 1) + + { + + nr=nr/x*(x-1); + + } + +} + + + +long long putere(long long x, long long p) + +{ + + long long rez=1; + + while(p) + + { + + if(p&1) + + { + + p--; + + rez=(rez*x)%n; + + } + + p>>=1; + + x=(x*x)%n; + + } + + return rez%n; + + + +} + + + + + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%lld %lld", &a, &n); + + nr=n; + + descomp(n); + + printf("%lld", putere(a, nr-1)%n); + + + + return 0; + +} diff --git a/invmod/euler-toutient/3bbfa3a2-1034-4bdb-a96a-e9969530d199.cpp b/invmod/euler-toutient/3bbfa3a2-1034-4bdb-a96a-e9969530d199.cpp new file mode 100644 index 0000000000000000000000000000000000000000..307224a1219082857cf6539707ae348562bdd1ef --- /dev/null +++ b/invmod/euler-toutient/3bbfa3a2-1034-4bdb-a96a-e9969530d199.cpp @@ -0,0 +1,75 @@ +#include +typedef unsigned long long ul; +typedef long long ll; +using namespace std; + + +ll a, n, x; + + + +ll phiCalc(ll n) + +{ + + ll phi = n; + + for (ll p = 2; p * p <= n; p++) + + { + + if (n % p == 0) + + { + + while (n % p == 0) n /= p; + + phi -= phi / p; + + + + } + + } + + if (n > 1) phi -= phi / n; + + return phi; + +} + + + +ll expon(ll b, ll p) + +{ + + if (p == 0) return 1; + + else if (p == 1) return b % n; + + else if (p % 2 == 0) return expon((b * b) % n, p / 2) % n; + + else if (p % 2 == 1) return (b % n) * (expon((b * b) % n, (p - 1) / 2) % n); + +} + + +int main(){ +ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); +ifstream cin("inversmodular.in"); +ofstream cout("inversmodular.out"); + + +cin >> a >> n; + + + + +cout << expon(a, phiCalc(n) - 1) % n << "\n"; + + + + +return 0; +} diff --git a/invmod/euler-toutient/3c0596fd-38a0-4e28-a013-ba3fb47f7eea.cpp b/invmod/euler-toutient/3c0596fd-38a0-4e28-a013-ba3fb47f7eea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..955b0888dce5c48a73c6f19574ef304d5f3fc3a8 --- /dev/null +++ b/invmod/euler-toutient/3c0596fd-38a0-4e28-a013-ba3fb47f7eea.cpp @@ -0,0 +1,79 @@ +#include +#include + + +using namespace std; + +long long phi(long long x) { + + long long answer = x; + + for (int i = 2; i * i <= x; ++i) { + + if (x % i == 0) { + + answer = answer / i * (i - 1); + + while (x % i == 0) { + + x = x / i; + + } + + } + + } + + if (x > 1) { + + answer = answer / x * (x - 1); + + } + + return answer; + +} + + + +long long power(long long a, long long b, long long mod) { + + if (b == 0) { + + return 1; + + } + + if (b % 2 == 0) { + + return power((1LL * a * a) % mod, b / 2, mod); + + } else { + + return ((1LL * a * power(a, b - 1, mod)) % mod) ; + + } + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + long long n, mod; + + fin >> n >> mod; + + fout << power(n, phi(mod) - 1, mod); + + + + return 0; + +} diff --git a/invmod/euler-toutient/3f3620f1-3828-4316-b95c-745a98522fca.cpp b/invmod/euler-toutient/3f3620f1-3828-4316-b95c-745a98522fca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fd12114de1ed7eb226738adbaccab3f1a160798 --- /dev/null +++ b/invmod/euler-toutient/3f3620f1-3828-4316-b95c-745a98522fca.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int InvMod(int a, int b) + +{ + + long long x1 = 1, x2 = 0, aux, q, auxb = b; + + + + while(b) + + { + + q = a / b; + + aux = b; + + b = a - q * b; + + a = aux; + + aux = x2; + + x2 = x1 - q * x2; + + x1 = aux; + + } + + + + if(x1 < 0) + + { + + x1 += auxb; + + } + + + + return x1; + +} + + + +int main() + +{ + + int a, b; + + + + fin >> a >> b; + + + + fout << InvMod(a, b); + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/3f403373-e67b-43e3-8d05-a189b0caee8e.cpp b/invmod/euler-toutient/3f403373-e67b-43e3-8d05-a189b0caee8e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11cf5bd292e2ee41ba9f827d4721516421da23f3 --- /dev/null +++ b/invmod/euler-toutient/3f403373-e67b-43e3-8d05-a189b0caee8e.cpp @@ -0,0 +1,101 @@ +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +unsigned long long lgput(unsigned long long a, unsigned long long b, unsigned long long mod) + +{ + + unsigned long long r = 1; + + while(b) + + { + + if(b % 2 == 1) + + r = ( r % mod * a % mod ) % mod; + + a = ( a % mod * a % mod ) % mod; + + b /= 2; + + } + + return r; + +} + +unsigned long long phi(int n) + +{ + + unsigned long long p = 1, d = 2, fm, prod = 1; + + do + + { + + fm = 0; + + prod = 1; + + while(n % d == 0) + + { + + n /= d; + + prod = prod * d; + + fm ++; + + } + + if(fm > 0) + + { + + p = p * ( d - 1) * ( prod / d ); + + } + + d ++; + + if(d * d > n && n > 1) + + { + + p = p * (n - 1); + + n = 1; + + } + + }while(n > 1); + + return p; + +} + +int main() + +{ + + int a, n; + + f >> a >> n; + + g << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/3fa96d47-7995-4a11-bfc3-0383502246a0.cpp b/invmod/euler-toutient/3fa96d47-7995-4a11-bfc3-0383502246a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fff8b8e571bc35c0a5dc90afcb48b8d7148894d3 --- /dev/null +++ b/invmod/euler-toutient/3fa96d47-7995-4a11-bfc3-0383502246a0.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/40e26ba1-27d0-434e-a015-c6ff8e546919.cpp b/invmod/euler-toutient/40e26ba1-27d0-434e-a015-c6ff8e546919.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0452400855f95b2d051a20a9e4e8721f084541df --- /dev/null +++ b/invmod/euler-toutient/40e26ba1-27d0-434e-a015-c6ff8e546919.cpp @@ -0,0 +1,86 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long a, n, i, diviz; + +long long explog(long long x1, long long n1) + +{ + + long long p=1; + + long long x=x1; + + while(n1>0) + + { + + if(n1&1) + + { + + p=(p*x)%n; + + + + n1--; + + } + + x=(x*x)%n; + + n1>>=1; + + } + + return p; + +} + +long long ptphi(long long x) + +{ + + long long temp=x; + + for(i=2; i*i<=x; i++) + + { + + if(x%i==0) + + { + + while(x%i==0) {x/=i;} + + temp=temp/i*(i-1); + + } + + } + + if(x>1) temp=temp/x*(x-1); + + return temp; + +} + +int main() + +{ + + in>>a>>n; + + out< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long A,n,rez,P = 1,MOD; + + + +int main(){ + + int i; + + fin>>A>>n; + + rez = n; + + MOD = n; + + if(n%2 == 0){ + + while(n%2 == 0){ + + n/=2; + + } + + rez = rez/2; + + } + + for(i = 3; i*i <= n; i+=2){ + + if(n%i == 0){ + + while(n%i == 0){ + + n/=i; + + } + + rez = rez - rez/i; + + } + + } + + if(n > 1){ + + rez = rez-rez/n; + + } + + rez--; + + while(rez != 0){ + + if(rez%2 == 1){ + + P = ((P%MOD)*(A%MOD))%MOD; + + } + + A = ((A%MOD)*(A%MOD))%MOD; + + rez /= 2; + + } + + fout< + + + + + + + +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/43d4aaa7-4d22-4c8d-be16-292b575a1be9.cpp b/invmod/euler-toutient/43d4aaa7-4d22-4c8d-be16-292b575a1be9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18d66f25f4874aca9849c5448bf04cdd5eca6a6d --- /dev/null +++ b/invmod/euler-toutient/43d4aaa7-4d22-4c8d-be16-292b575a1be9.cpp @@ -0,0 +1,66 @@ +#include + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int main() { + + long long n, a, phi, cn, ans, d; + + cin >> a >> n; + + cn = n; + + phi = n; + + d = 2; + + while ( d * d <= n ) { + + if ( n % d == 0 ) { + + phi = phi / d * ( d - 1 ); + + while ( n % d == 0 ) + + n /= d; + + } + + d++; + + } + + if ( n > 1 ) + + phi = phi / n * ( n - 1 ); + + phi--; + + ans = 1; + + while ( phi > 0 ) { + + if ( phi % 2 != 0 ) + + ans = ans * a % cn; + + phi /= 2; + + a = a * a % cn; + + } + + cout << ans; + + return 0; + +} diff --git a/invmod/euler-toutient/44d0434c-790c-423d-ae13-dff7ff5d3e6f.cpp b/invmod/euler-toutient/44d0434c-790c-423d-ae13-dff7ff5d3e6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af2752b36c407ae7870a21e34faac382c39cb521 --- /dev/null +++ b/invmod/euler-toutient/44d0434c-790c-423d-ae13-dff7ff5d3e6f.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +int buildPhi(int n) { + int answer = n; + for (int i = 2; i * i <= n; ++i) { + if (n % i == 0) { + answer = answer / i * (i - 1); + while (n % i == 0) { + n /= i; + } + } + } + if (n > 1) { + answer = answer / n * (n - 1); + } + return answer; +} + +int power(int a, int b, int MOD) { + if (b == 0) return 1; + if (b % 2 == 0) return power(static_cast(1LL * a * a % MOD), b / 2, MOD); + return static_cast(1LL * a * power(a, b - 1, MOD) % MOD); +} + +int main() { + ifstream f("inversmodular.in"); + ofstream g("inversmodular.out"); + int n, MOD; + f >> n >> MOD; + g << power(n, buildPhi(MOD) - 1, MOD); + return 0; +} diff --git a/invmod/euler-toutient/44dd5858-1b8b-47ab-b291-8637a032d316.cpp b/invmod/euler-toutient/44dd5858-1b8b-47ab-b291-8637a032d316.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b704dac55d6b847689175db39aa9039383b6f74a --- /dev/null +++ b/invmod/euler-toutient/44dd5858-1b8b-47ab-b291-8637a032d316.cpp @@ -0,0 +1,130 @@ +#include +long long N,M; + + + + + + + +long long getphi(long long nr) + + + +{ + + + + long long cur = nr; + + + + for(long long i = 2;i * i <= nr; ++i) + + + + { + + + + if (nr % i == 0) + + + + { + + + + while(nr % i == 0)nr /= i; + + + + cur = (cur / i) * (i - 1); + + + + } + + + + } + + + + if (nr != 1) cur = cur / nr * (nr - 1); + + + + return cur; + + + +} + + + + + + + +int main() + + + +{ + + + + freopen("inversmodular.in","r",stdin); + + + + freopen("inversmodular.out","w",stdout); + + + + scanf("%lld %lld\n",&N,&M); + + + + long long put = getphi(M) - 1; + + + + long long nr = N; + + + + long long crt = 1; + + + + for(long long p = 1;p <= put;p <<= 1) + + + + { + + + + if (p & put) crt = (crt * nr) % M; + + + + nr = (nr * nr) % M; + + + + } + + + + printf("%lld\n",crt); + + + + return 0; + + + +} diff --git a/invmod/euler-toutient/47ff7dd8-4a19-422a-8983-b2c39229755a.cpp b/invmod/euler-toutient/47ff7dd8-4a19-422a-8983-b2c39229755a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/47ff7dd8-4a19-422a-8983-b2c39229755a.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/482a14ca-9008-42de-996c-f5de203ed31b.cpp b/invmod/euler-toutient/482a14ca-9008-42de-996c-f5de203ed31b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fcb4db404c637f7e98b7096ef44e0e66b660f1a --- /dev/null +++ b/invmod/euler-toutient/482a14ca-9008-42de-996c-f5de203ed31b.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; + + + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long n, m, k, l, euler, auxm; + + + +void solve_euler() + +{ + + euler=m; + + if (m%2==0) + + { + + euler/=2; + + while (m%2==0) + + { + + m/=2; + + } + + } + + int aux=m; + + for (int d=3; d*d<=m; d+=2) + + { + + if (m%d==0) + + { + + euler*=(d-1); + + euler/=d; + + while (m%d==0) + + m/=d; + + } + + } + + if (m>1) + + { + + euler*=(m-1); + + euler/=m; + + } + + + +} + + + +long long ridicare_putere(long long a, long long p) + +{ + + long long x=a, y=1; + + while (p) + + { + + if (p%2==1) + + { + + y*=x; + + --p; + + y%=auxm; + + } + + else + + { + + x*=x; + + x%=auxm; + + p/=2; + + } + + } + + return y; + +} + + + +int main() + +{ + + f >> n >> m; + + auxm=m; + + solve_euler(); + + g << ridicare_putere(n,euler-1); + + return 0; + +} diff --git a/invmod/euler-toutient/48dbfb30-24d7-4a71-bcbe-58b85e8d1592.cpp b/invmod/euler-toutient/48dbfb30-24d7-4a71-bcbe-58b85e8d1592.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f379f80c60ae65ec44a65fa5f45b3f4c7ae0e57c --- /dev/null +++ b/invmod/euler-toutient/48dbfb30-24d7-4a71-bcbe-58b85e8d1592.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; +ifstream fin("inversmodular.in"); ofstream fout("inversmodular.out"); +int phi(int x){ + +int res=x; + +int n=x; + + + +for(int i=2; i*i<=x; i++){ + + if(n%i==0){ + + while(n%i==0){ + + n/=i; + + } + + res-=res/i; + + } + +} + + + +if(n>1){ + + res-=res/n; + +} + +return res; + +} + + + + + +long long n, a; +long long exp(long long x, long long e){ + + + + + +if(e==1){return x;} + + + +if(e%2==0){return exp( (x*x)%n, e/2 )%n; } + +else{return (x*exp( (x*x)%n, e/2 ))%n; } + + + + + + + +} +int32_t main(){ + +ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +fin>>a>>n; + + + +fout< + + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int a, n; + + + +int exp(int N, int p) { + + int r = 1; + + while (p) { + + if (p % 2 == 1)r = (1LL * N * r) % n; + + N =(1LL * N * N) % n; + + p /= 2; + + } + + return r; + +} + + + +int phi(int n) { + + int result = n; + + + + for (int p = 2; p * p <= n; ++p) { + + + + + + if (n % p == 0) { + + while (n % p == 0) + + n /= p; + + result -= result / p; + + } + + } + + if (n > 1) + + result -= result / n; + + return result; + +} + + + +int main() { + + ios_base::sync_with_stdio(false); + + in.tie(NULL), out.tie(NULL); + + in >> a >> n; + + out << exp(a, phi(n) - 1) % n; + + + +} diff --git a/invmod/euler-toutient/4a0b60fc-39f1-4eb6-9eef-54df87a312a2.cpp b/invmod/euler-toutient/4a0b60fc-39f1-4eb6-9eef-54df87a312a2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a6b3cb3e06a6770b17c11323c628c8777603aae --- /dev/null +++ b/invmod/euler-toutient/4a0b60fc-39f1-4eb6-9eef-54df87a312a2.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long int n,pr; + +int MOD,invers,mod; + + + +void exp(long long int nr,long long int ad,long long int x) + + { + + if(nr!=pr) + + { + + if(nr+ad<=pr) { + + invers=(invers*x)%MOD; + + x=(x*x)%MOD; + + nr+=ad; + + ad*=2; + + exp(nr,ad,x); + + } + + else exp(nr,1,n); + + } + + } + +int main() + +{ + +f>>n>>mod; + +MOD=mod; + +pr=mod; + +int d=3; + +if(mod%2==0) + + { + + pr=pr/2; + + while(mod%2==0) + + mod/=2; + + } + +while(d*d<=mod&&mod!=1) + + { + + if(mod%d!=0) d+=2; + + else + + { + + pr=pr/d*(d-1); + + while(mod%d==0) + + mod/=d; + + } + + } + +if(mod!=1) pr=pr/mod*(mod-1); + +pr--; + +invers=n; + +if(pr==0) g<<"1"; + +else + +{ + +exp(1,1,n); + +g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n,a,x; + +int putere(int b,int e) + +{ + + if(e==0)return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1)r=1LL*r*b%n; + + return r; + +} + +int euler(int m) + +{ + + int phi=1; + + if(m%2==0){m/=2;while(m%2==0){m/=2;phi*=2;}} + + for(int d=3;d*d<=m;d+=2) + + if(m%d==0){m/=d;phi*=d-1;while(m%d==0){m/=d;phi*=d;}} + + if(m>1)phi*=m-1; + + return phi; + +} + +int main() + +{ + + f>>a>>n; + + g< +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long int n,pr; + +int MOD,invers,mod; + + + +void exp(long long int nr,long long int ad,long long int x) + + { + + if(nr!=pr) + + { + + if(nr+ad<=pr) { + + invers=(invers*x)%MOD; + + x=(x*x)%MOD; + + nr+=ad; + + ad*=2; + + exp(nr,ad,x); + + } + + else exp(nr,1,n); + + } + + } + +int main() + +{ + +f>>n>>mod; + +MOD=mod; + +pr=mod; + +int d=3; + +if(mod%2==0) + + { + + pr=pr/2; + + while(mod%2==0) + + mod/=2; + + } + +while(d*d<=mod&&mod!=1) + + { + + if(mod%d!=0) d+=2; + + else + + { + + pr=pr/d*(d-1); + + while(mod%d==0) + + mod/=d; + + } + + } + +if(mod!=1) pr=pr/mod*(mod-1); + +pr--; + +invers=n; + +if(pr==0) g<<"1"; + +else + +{ + +exp(1,1,n); + +g< +#include + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,m; + +long long phi(long long nr){ + + long long rez=nr; + + for(int i=2;i*i<=nr;i++){ + + if(nr%i==0){ + + while(nr%i==0) nr/=i; + + rez=(rez/i)*(i-1); + + } + + } + + if(nr!=1) rez=rez/nr*(nr-1); + + return rez; + +} + +int main(){ + + fin>>n>>m; + + long long nr=n,crt=1,put=phi(m)-1; + + for(long long p=1;p<=put;p<<=1){ + + if(p&put) crt=(crt*nr)%m; + + nr=(nr*nr)%m; + + } + + fout< + + +using namespace std; + + + +int n; + + + +int rid_put(int a,int b) + +{ + + int sol=1; + + for(long long i=1;i<=b;i<<=1) + + { + + if(b&i) sol=(1LL*sol*a)%n; + + a=(1LL*a*a)%n; + + } + + return sol; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a; + + scanf("%d%d",&a,&n); + + int fi=n,nr=n; + + for(int i=2;i*i<=nr;i++) + + if(nr%i==0) + + { + + while(nr%i==0) nr/=i; + + fi=(fi/i)*(i-1); + + } + + if(nr>1) fi=(fi/nr)*(nr-1); + + printf("%d",rid_put(a,fi-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/4df88d26-7f51-42ff-b1fc-0ce0c14faf87.cpp b/invmod/euler-toutient/4df88d26-7f51-42ff-b1fc-0ce0c14faf87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fff8b8e571bc35c0a5dc90afcb48b8d7148894d3 --- /dev/null +++ b/invmod/euler-toutient/4df88d26-7f51-42ff-b1fc-0ce0c14faf87.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/4dfb8eb6-0ba0-4ea7-aea2-70bd9052d5e4.cpp b/invmod/euler-toutient/4dfb8eb6-0ba0-4ea7-aea2-70bd9052d5e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11cf5bd292e2ee41ba9f827d4721516421da23f3 --- /dev/null +++ b/invmod/euler-toutient/4dfb8eb6-0ba0-4ea7-aea2-70bd9052d5e4.cpp @@ -0,0 +1,101 @@ +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +unsigned long long lgput(unsigned long long a, unsigned long long b, unsigned long long mod) + +{ + + unsigned long long r = 1; + + while(b) + + { + + if(b % 2 == 1) + + r = ( r % mod * a % mod ) % mod; + + a = ( a % mod * a % mod ) % mod; + + b /= 2; + + } + + return r; + +} + +unsigned long long phi(int n) + +{ + + unsigned long long p = 1, d = 2, fm, prod = 1; + + do + + { + + fm = 0; + + prod = 1; + + while(n % d == 0) + + { + + n /= d; + + prod = prod * d; + + fm ++; + + } + + if(fm > 0) + + { + + p = p * ( d - 1) * ( prod / d ); + + } + + d ++; + + if(d * d > n && n > 1) + + { + + p = p * (n - 1); + + n = 1; + + } + + }while(n > 1); + + return p; + +} + +int main() + +{ + + int a, n; + + f >> a >> n; + + g << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/501a982a-c073-401e-83cc-45d7451863c0.cpp b/invmod/euler-toutient/501a982a-c073-401e-83cc-45d7451863c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ac9c8f5f1250c31e5eb0321669e6668ce914ad0 --- /dev/null +++ b/invmod/euler-toutient/501a982a-c073-401e-83cc-45d7451863c0.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +int phi(int n) { + + int rez = n; + + + + for (int i = 2;i * i <= n;i ++) { + + if (n % i == 0) { + + + + while (n % i == 0) { + + n /= i; + + } + + + + rez = rez / i * (i - 1); + + } + + } + + + + if (n != 1) + + rez = rez / n * (n - 1); + + return rez; + +} + + + +long long ridicare_la_putere(int x, int n, int exp) { + + if (exp == 0) + + return 1; + + if (exp % 2 == 0) + + return ridicare_la_putere(1LL * x * x % n, n, exp / 2) % n; + + return 1LL * x * ridicare_la_putere(1LL * x * x % n, n, (exp - 1) / 2) % n; + +} + + + +int main() { + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, n; + + fin >> A >> n; + + + + int exponent = phi(n); + + + + + + fout << ridicare_la_putere(A, n, exponent - 1); + + + + return 0; + +} diff --git a/invmod/euler-toutient/50529e3f-2c3a-498c-8ecb-725a9354f74f.cpp b/invmod/euler-toutient/50529e3f-2c3a-498c-8ecb-725a9354f74f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92cd5d3fb986d93fd65df3b4b76f72b1bdba99aa --- /dev/null +++ b/invmod/euler-toutient/50529e3f-2c3a-498c-8ecb-725a9354f74f.cpp @@ -0,0 +1,85 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long eulerTotient(long long n) { + + long long sol = n; + + if (n % 2 == 0) { + + sol = sol / 2; + + while (n % 2 == 0) + + n /= 2; + + } + + for (int d = 3; d * d <= n; d += 2) + + if (n % d == 0) { + + sol = sol / d * (d - 1); + + while (n % d == 0) + + n /= d; + + } + + if (n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + + + +long long raise(long long base, long long exp, long long n) { + + long long result = 1; + + while (exp) { + + if (exp & 1) + + result = (result * base) % n; + + base = (base * base) % n; + + exp >>= 1; + + } + + return result; + +} + + + +int main() { + + long long a, n; + + fin >> a >> n; + + fout << raise(a, eulerTotient(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/5155dff7-67a5-4116-b405-da7ec146c138.cpp b/invmod/euler-toutient/5155dff7-67a5-4116-b405-da7ec146c138.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdbb9d406dd21be886077eb86e6b4a2d99444726 --- /dev/null +++ b/invmod/euler-toutient/5155dff7-67a5-4116-b405-da7ec146c138.cpp @@ -0,0 +1,115 @@ +#include +#include + + +using namespace std; + + + +long long a,n,rez,p; + + + +long long indicator() + +{ + + rez=n; + + if(n%2==0) + + { + + while(n%2==0) + + n/=2; + + rez/=2; + + } + + for(long long i=3;i*i<=n;i+=2) + + if(n%i==0) + + { + + rez=rez/i*(i-1); + + while(n%i==0) + + n/=i; + + } + + if(n!=1) + + rez=rez/n*(n-1); + +} + + + +void putere() + +{ + + ofstream fout("inversmodular.out"); + + long long sol=1; + + while(rez) + + { + + if(rez%2) + + { + + rez--; + + sol=(sol*a)%p; + + } + + else + + { + + rez/=2; + + a=(a*a)%p; + + } + + } + + while(sol<0) + + sol=(sol+n)%p; + + fout<>a>>n; + + p=n; + + indicator(); + + rez--; + + putere(); + + return 0; + +} diff --git a/invmod/euler-toutient/517e4f1b-f2b8-4ed9-8021-4033bc220b4d.cpp b/invmod/euler-toutient/517e4f1b-f2b8-4ed9-8021-4033bc220b4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f43d930a459097c3df7650bd252f3de9973c79d4 --- /dev/null +++ b/invmod/euler-toutient/517e4f1b-f2b8-4ed9-8021-4033bc220b4d.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +typedef long long LL; + + + +LL ridicare(LL baza, LL exp, LL n) + +{ + + LL ans = 1; + + + + while(exp) + + { + + if(exp % 2) + + ans = ans * baza % n; + + + + baza = baza * baza % n; + + exp /= 2; + + } + + + + return ans; + +} + + + +LL phi(LL n) + +{ + + LL ans = n; + + + + LL div = 2; + + + + while(div * div <= n) + + { + + if(n % div == 0) + + { + + while(n % div == 0) + + n /= div; + + + + ans = ans / div * (div - 1); + + } + + + + div++; + + } + + + + if(n > 1) + + ans = ans / n * (n - 1); + + + + return ans; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + LL a, n; + + + + fin >> a >> n; + + cout << phi(n); + + + + fout << ridicare(a, phi(n) - 1, n); + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/invmod/euler-toutient/52ba817b-a262-45a2-b025-668416ebddba.cpp b/invmod/euler-toutient/52ba817b-a262-45a2-b025-668416ebddba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fff8b8e571bc35c0a5dc90afcb48b8d7148894d3 --- /dev/null +++ b/invmod/euler-toutient/52ba817b-a262-45a2-b025-668416ebddba.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/54e3ae65-035b-4739-a2a3-3fafbbeba414.cpp b/invmod/euler-toutient/54e3ae65-035b-4739-a2a3-3fafbbeba414.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45bb7449d952297f5ed9cf2985aaaefc96021637 --- /dev/null +++ b/invmod/euler-toutient/54e3ae65-035b-4739-a2a3-3fafbbeba414.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int a,n; + + + +int logpow2(int a,int p) + +{ + + int rez=1,inm=a; + + + + while(p) + + { + + if(p%2) + + rez*=inm; + + + + p/=2; + + inm=inm*inm; + + } + + + + return rez; + +} + + + +int coprime(int nr) + +{ + + int fact[101],nrFact[101],t=0; + + int sum=1; + + + + int radNr=sqrt(nr); + + for(int d=2;d<=radNr && nr!=1;d++) + + if(nr%d==0) + + { + + fact[++t]=d; + + nrFact[t]=0; + + + + while(nr%d==0) + + { + + nrFact[t]++; + + nr/=d; + + } + + } + + + + if(nr!=1) + + { + + fact[++t]=nr; + + nrFact[t]=1; + + } + + + + for(int i=1;i<=t;i++) + + sum*=logpow2(fact[i],nrFact[i]-1)*(fact[i]-1); + + + + return sum; + +} + + + +int logpow(int a,int p) + +{ + + long long rez=1; + + long long inm=a; + + + + while(p) + + { + + if(p%2) + + rez=(rez*inm)%n; + + + + p/=2; + + inm=(inm*inm)%n; + + } + + + + return rez; + +} + + + +int main() + +{ + + in>>a>>n; + + + + int p=coprime(n); + + int rez=logpow(a,p-1); + + + + out< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int LogP(int a, int n, int mod) + +{ + + int p=1; + + while (n) + + { + + if (n%2==1) + + p=1ll*p*a%mod; + + a=1ll*a*a%mod; + + n/=2; + + } + + return p; + +} + +int phi(int n) + +{ + + int sol=n,p; + + for (p=2; n>1 and p*p<=n; p++) + + { + + if (n%p==0) + + sol=sol/p*(p-1); + + while (n%p==0) + + n/=p; + + + + } + + if (n>1) + + sol=sol/n*(n-1); + + return sol; + +} + +int main() + +{ + + int a,n; + + fin >> a >> n; + + fout << (LogP(a,(phi(n)-1),n)); + return 0; + +} diff --git a/invmod/euler-toutient/568c66a2-04fa-4330-a836-f10195fce935.cpp b/invmod/euler-toutient/568c66a2-04fa-4330-a836-f10195fce935.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/568c66a2-04fa-4330-a836-f10195fce935.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/56b378b1-800d-46ff-a5d3-3e9755beefe5.cpp b/invmod/euler-toutient/56b378b1-800d-46ff-a5d3-3e9755beefe5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a3c7f24d9a734844d53058d2622c86a9bae3af7 --- /dev/null +++ b/invmod/euler-toutient/56b378b1-800d-46ff-a5d3-3e9755beefe5.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +unsigned long long a,n,MOD; + +unsigned long long lgnp(unsigned long long n, unsigned long long p) + +{ + + n=n%MOD; + + unsigned long long r=1; + + while(p) + + { + + if(p%2==1) r=(1LL*r*n)%MOD; + + n=(n*n*1LL)%MOD; + + p/=2; + + } + + return r%MOD; + +} + +unsigned long long phi(unsigned long long n) + +{ + + unsigned long long d=2,fm,nr=1; + + do + + { + + fm=0; + + while(n%d==0) + + { + + n/=d; + + fm++; + + } + + if(fm>0) nr*=(d-1)*lgnp(d,fm-1); + + d++; + + if(d*d>n&&n>1) + + { + + nr=nr*(n-1); + + n=1; + + } + + } + + while(n>1); + + return nr; + +} + +int main() + +{ + + fin>>a>>n; + + MOD=n; + + fout< +using namespace std; + +unsigned long long a, MOD, x, d, fi, fm, i; + +unsigned long long rez; + +unsigned long long paw(int a, int b) + +{ + + unsigned long long r = 1, A = a; + + if(b == 0)return 1; + + while(b > 1) + + { + + if(b % 2 == 1)r = (r * A) % MOD; + + A = (A * A) % MOD; + + b = b / 2; + + } + + return (r * A) % MOD; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> MOD; + + + + x = MOD; + + d = 2; + + fi = 1; + + do + + { + + fm = 0; + + while(x % d == 0) + + { + + x = x / d; + + fm ++; + + } + + if(fm > 0)fi = (fi * paw(d, fm - 1) * (d - 1)) % MOD; + + d ++; + + if(d * d > x && x != 1)fi = (fi * (x - 1)) % MOD, x = 1; + + }while(x != 1); + + g << paw(a, fi - 1) % MOD << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/5baca1c3-a1f1-42e3-a7a9-964efc8e5296.cpp b/invmod/euler-toutient/5baca1c3-a1f1-42e3-a7a9-964efc8e5296.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae4369e31cad674d3f0901e2890379afe58e4b1e --- /dev/null +++ b/invmod/euler-toutient/5baca1c3-a1f1-42e3-a7a9-964efc8e5296.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long lgput(long long a, long long b, long long MOD) { + + long long ret = 1LL; + + while (b) { + + if (b & 1LL) ret = (ret * a) % MOD; + + a = (a * a) % MOD; + + b >>= 1LL; + + } + + + + return (ret % MOD); + +} + + + +long long phi(long long nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1){ + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + + + +long long inv_mod(long long x, long long MOD) { + + return lgput(x, phi(MOD) - 1, MOD); + +} + + + +int main() { + + + + long long a, b; + + cin >> a >> b; + + cout << inv_mod(a, b); + + + + return 0; + +} diff --git a/invmod/euler-toutient/5bd06201-e932-4a51-963d-07d39b881bd8.cpp b/invmod/euler-toutient/5bd06201-e932-4a51-963d-07d39b881bd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26777bd676ad52cf623894071a1462079f4ff0ff --- /dev/null +++ b/invmod/euler-toutient/5bd06201-e932-4a51-963d-07d39b881bd8.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long n; + + + +long long ridic(long long x, long long p) + +{ + + if (p == 1) + + return (x %n); + + long long aux = ridic(x, p / 2); + + + + if (p % 2 == 0) + + return ((aux*aux) % n); + + else + + return ((((aux * aux) % n) *x) % n); + +} + + + +long long phi(long long n) + +{ + + long long rez = n; + + + + for (long long i = 2; i*i <= n; ++i) + + { + + if (n %i == 0) + + { + + while (n %i == 0) + + n /= i; + + rez -= (rez / i); + + } + + } + + + + if (n > 1) + + rez -= (rez / n); + + + + return rez; + +} + + + + + +int main() + +{ + + long long a; + + f >> a >> n; + + + + long long ph = phi(n); + + + + long long inv_m = ridic(a, ph - 1); + + + + g << inv_m << "\n"; + + + + return 0; + +} diff --git a/invmod/euler-toutient/5da5bcf3-aba6-4db0-9165-5238c36e2d00.cpp b/invmod/euler-toutient/5da5bcf3-aba6-4db0-9165-5238c36e2d00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ad2238b752a72eb40ee1cbb03ee245a1437c24 --- /dev/null +++ b/invmod/euler-toutient/5da5bcf3-aba6-4db0-9165-5238c36e2d00.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int GetPhi(int nr) + +{ + + int phi = nr; + + + + for(int i = 2; i * i <= nr; i++) + + if(nr % i == 0) + + { + + phi /= i; + + phi *= (i - 1); + + + + while(nr % i == 0) + + nr /= i; + + } + + + + if(nr != 1) + + { + + phi /= nr; + + phi *= (nr - 1); + + } + + + + return phi; + +} + + + +int lgput(int base, int exp, int N) + +{ + + int ans = 1; + + int aux = base; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = (1LL * ans * aux) % N; + + + + aux = (1LL * aux * aux) % N; + + } + + + + return ans; + +} + + + +int main() + +{ + + int A, N; fin >> A >> N; + + + + int phi = GetPhi(N); + + + + fout << lgput(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/5fd1bac7-4a90-4a06-ad39-a39f1f275d55.cpp b/invmod/euler-toutient/5fd1bac7-4a90-4a06-ad39-a39f1f275d55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a6cd71f08a19c113af495b648dd802eaf6c9e0c --- /dev/null +++ b/invmod/euler-toutient/5fd1bac7-4a90-4a06-ad39-a39f1f275d55.cpp @@ -0,0 +1,83 @@ +#include + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a, n; +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + fin >> a >> n; + + fout << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/5fdb1cc3-c542-4466-83a7-97b0d9417daa.cpp b/invmod/euler-toutient/5fdb1cc3-c542-4466-83a7-97b0d9417daa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12a00a9ecfd081748c6d1c031aa80267c598671b --- /dev/null +++ b/invmod/euler-toutient/5fdb1cc3-c542-4466-83a7-97b0d9417daa.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +long long unsigned phi(long long unsigned n) +{ + long long unsigned nr = n; + + for(long long unsigned i = 2; i * i <= n; i++) + { + if(n % i == 0) + { + while(n % i == 0) + { + n /= i; + } + + nr = (nr / i) * (i - 1); + cout<>n>>m; + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/6279f838-d966-4140-876b-ea32eff0e5ee.cpp b/invmod/euler-toutient/6279f838-d966-4140-876b-ea32eff0e5ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..511bbdef033e3dd3eeb1b9b550ff204adb9d11a0 --- /dev/null +++ b/invmod/euler-toutient/6279f838-d966-4140-876b-ea32eff0e5ee.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void invers_mod(long long a,long long b,long long &x,long long &y) + +{ + + if(b==0) + + { + + x=1,y=0; + + } + + else + + { + + invers_mod(b,a%b,x,y); + + int aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + +} + + + + + +int main() + +{ + + long long n,m,x,y; + + fin>>n>>m; + + invers_mod(n,m,x,y); + + if(x<=0) + + x=m+x; + + fout< +#include + + +using namespace std; + + + +ifstream in ("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +namespace Maths{ + + typedef long long LL; + + using Maths::LL; + + + + const LL mod=998244353; + void modularInverseUtil(LL & cmmdc, LL &x, LL &y, LL a, LL b){ + + if(b==0){ + + cmmdc=a; + + x=1; y=0; + + return; + + } + + modularInverseUtil(cmmdc, x, y, b, a%b); + + LL x0=x; + + x=y; + + y=x0-y*(a/b); + + } + + inline LL expLog(LL x, LL put){ + + LL prod=1; + + while(put){ + + if(put&1) + + prod*=x, prod%=mod; + + put>>=1, x=(x*x)%mod; + + } + + return prod; + + } + + inline LL invMod(LL a, LL b=mod){ + + LL x, y, cmdc; + + modularInverseUtil(cmdc, x, y, a, b); + + + + return (x<0) ? b+x%b : x%b; + + } +}; + +long long x, y; + +int main() + +{ + + in>>x>>y; + + out< + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int N, A; + + + +int LogPow(int x, int n) + +{ + + if (n == 0) return 1; + + else if (n % 2 == 0) return LogPow(1LL * x * x % N, n / 2); + + else return 1LL * x * LogPow(1LL * x * x % N, n / 2) % N; + +} + + + +int phi(int N) + +{ + + int p = N; + + for (int i = 2; i * i <= N; i += 1 + i % 2) + + if (N % i == 0) + + { + + while (N % i == 0) + + N /= i; + + p = p / i * (i - 1); + + } + + if (N > 1) + + p = p / N * (N - 1); + + return p; + +} + + + +int main() + +{ + + fin >> A >> N; + + fout << LogPow(A, phi(N) - 1); + + return 0; + +} diff --git a/invmod/euler-toutient/6823aa22-7b37-4653-8641-e1168e92cd75.cpp b/invmod/euler-toutient/6823aa22-7b37-4653-8641-e1168e92cd75.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f3f1e2c20a7d9e2f39379df308f0218959e7d0e --- /dev/null +++ b/invmod/euler-toutient/6823aa22-7b37-4653-8641-e1168e92cd75.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + + + +long long a, n, nr; + + + +void descomp(long long x) + +{ + + if(x%2 == 0) + + { + + while(x%2 == 0) + + { + + x/=2; + + } + + nr=nr/2; + + } + + for(int i=3; i*i<=x; i+=2) + + { + + if(x%i == 0) + + { + + while(x%i == 0) + + { + + x/=i; + + } + + nr=nr/i*(i-1); + + } + + } + + if(x != 1) + + { + + nr=nr/x*(x-1); + + } + +} + + + +long long putere(long long x, long long p) + +{ + + long long rez=1; + + while(p) + + { + + if(p&1) + + { + + p--; + + rez=(rez*x)%n; + + } + + p>>=1; + + x=(x*x)%n; + + } + + return rez%n; + + + +} + + + + + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%lld %lld", &a, &n); + + nr=n; + + descomp(n); + + printf("%lld", putere(a, nr-1)%n); + + + + return 0; + +} diff --git a/invmod/euler-toutient/6840a7c6-9ed6-4a5a-a956-e40322839ee8.cpp b/invmod/euler-toutient/6840a7c6-9ed6-4a5a-a956-e40322839ee8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2471dbd4e1cce91c056a41d8e8199da31065303 --- /dev/null +++ b/invmod/euler-toutient/6840a7c6-9ed6-4a5a-a956-e40322839ee8.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int a,n; + +int phi(int nr) + +{ + + int ret=1; + + if(nr%2==0) + + ret/=2; + + while(nr%2==0) + + { + + nr/=2; + + ret*=2; + + } + + for(int d=3; d*d<=nr; d+=2) + + if(nr%d==0) + + { + + nr/=d; + + ret*=(d-1); + + while(nr%d==0) + + { + + nr/=d; + + ret*=d; + + } + + } + + if(nr>1) + + ret*=nr-1; + + return ret; + +} + +int putere(int b,int e) + +{ + + if(e==0) + + return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2) + + r=1ll*r*b%n; + + return r; + +} + +int main(void) + +{ + + f>>a>>n; + + g< +#include + + +using namespace std; + + + +long long getphi(int n) { + + long long rez = n; + + int i = 2; + + while (i * i <= n) { + + if (n % i == 0) { + + rez = rez * (i - 1) / i; + + while (n % i == 0) + + n /= i; + + } + + i++; + + } + + if (n != 1) + + rez = rez * (n - 1) / n; + + return rez; + +} + + + + + +long long lgput (long long n, long long p, long long mod) { + + long long rez = 1; + + while (p > 0) { + + if (p % 2 == 0) { + + n = (n % mod) * n % mod; + + p = p / 2; + + } + + else { + + rez = rez * (n % mod) % mod; + + p--; + + } + + } + + return rez; + +} + + + +int main() { + + + + freopen ("inversmodular.in", "r", stdin); + + freopen ("inversmodular.out", "w", stdout); + + + + long long A, N, put; + + + + scanf ("%lld%lld", &A, &N); + + + + put = getphi(N) - 1; + + printf ("%lld", lgput(A, put, N)); + + + + + + return 0; + +} diff --git a/invmod/euler-toutient/699cf908-4900-4ab4-892c-4c95b350f797.cpp b/invmod/euler-toutient/699cf908-4900-4ab4-892c-4c95b350f797.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c799cc8fbd94106e5692b76a96c2e82786625fc --- /dev/null +++ b/invmod/euler-toutient/699cf908-4900-4ab4-892c-4c95b350f797.cpp @@ -0,0 +1,68 @@ +#include +long long N,A; + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long Phi(long long n) + +{ + + long long c=n; + + for(long long i=2;i*i<=n;++i) + + { + + if (n%i==0) + + { + + while(n%i==0) n/=i; + + c=(c/i)*(i-1); + + } + + } + + if(n!=1) c=c/n*(n-1); + + return c; + +} + + + +int main() + +{ + + fin>>A>>N; + + long long p=Phi(N)-1; + + long long n=A; + + long long c=1; + + for(long long i=1;i<=p;i<<=1) + + { + + if(i&p)c=(c*n)%N; + + n=(n*n)%N; + + } + + fout< +#include + +using namespace std; + +ifstream fin; +ofstream fout; + +long long mod, a; + +long long log_exp (long long base, long long power){ + long long to_return; + + if (power == 1ll) return (base%mod); + + long long aux = power % 2; + switch (aux){ + case 0ll: + to_return = log_exp((base%mod) * (base%mod), power/2) % mod; + break; + + case 1ll: + to_return = (base%mod * ((log_exp((base%mod) * (base%mod), power/2)) %mod)); + break; + } + + return to_return; +} + +int getphi(int n) +{ + int result = n; + + + + if (n % 2 == 0){ + while (n % 2 == 0) + n /= 2; + result -= result / 2; + } + + for (int p = 3; p * p <= n; p+=2) { + + + if (n % p == 0) { + + + while (n % p == 0) + n /= p; + result -= result / p; + } + } + + + + if (n > 1) + result -= result / n; + return result; +} + + +int main(void){ + fin.open("inversmodular.in"); + fin>>a>>mod; + fin.close(); + + long long result, exponent = getphi(mod); + + result = log_exp(a, (exponent-1)); + + fout.open("inversmodular.out"); + fout<<(result%mod)<<"\n"; + fout.close(); + + return 0; +} diff --git a/invmod/euler-toutient/6ad15e0c-6500-4d47-bc46-ceb1f0cc0357.cpp b/invmod/euler-toutient/6ad15e0c-6500-4d47-bc46-ceb1f0cc0357.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41afc59d5d5a00966c7637e0d0232cca21511d8b --- /dev/null +++ b/invmod/euler-toutient/6ad15e0c-6500-4d47-bc46-ceb1f0cc0357.cpp @@ -0,0 +1,119 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int n, a; + +long long x; + + + +int prim(int n) + +{ + + if(n<2) + + return 0; + + if(n%2==0 && n>2) + + return 0; + + for(int d=3;d*d<=n;d+=2) + + if(n%d==0) + + return 0; + + return 1; + +} + + + +long long P(int a, int n, int mod) + +{ + + long long p=1; + + while(n) + + { + + if(n%2==1) + + p=((p%mod)*(a%mod))%mod; + + a=(1LL*(a%mod)*(a%mod))%mod; + + n/=2; + + } + + return p; + +} + + + +int Phi(int n) + +{ + + int r=n, d=2; + + while(n>1) + + { + + if(n%d==0) + + { + + r=r/d*(d-1); + + while(n%d==0) + + n/=d; + + } + + d++; + + if(d*d>n) + + d=n; + + } + + return r; + +} + + + +int main() + +{ + + fin >> a >> n; + + x = P(a,Phi(n)-1,n); + + fout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/6c7e60f7-75f7-4a57-80e0-9f29461c76f1.cpp b/invmod/euler-toutient/6c7e60f7-75f7-4a57-80e0-9f29461c76f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3161b9699aeac465340c0c5c12e4c8c9f427ec0 --- /dev/null +++ b/invmod/euler-toutient/6c7e60f7-75f7-4a57-80e0-9f29461c76f1.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n; + + + +int Phi(int n) + +{ + + int sol, p; + + sol = n; + + for(p = 2 ; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + + + +} + + + +int LogP(int a, int n, int MOD) + +{ + + int sol = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + sol = 1LL * sol * a % MOD; + + n /= 2; + + a = 1LL * a * a % MOD; + + } + + return sol; + +} + + + +int main() + +{ + + int b; + + fin >> a >> n; + + b = LogP(a, Phi(n) - 1, n); + + fout << b; + + return 0; + +} diff --git a/invmod/euler-toutient/6cc5d465-60f6-4ea5-a19c-2bf228add081.cpp b/invmod/euler-toutient/6cc5d465-60f6-4ea5-a19c-2bf228add081.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c56637de8874f884b0200434d02944d51f102e3a --- /dev/null +++ b/invmod/euler-toutient/6cc5d465-60f6-4ea5-a19c-2bf228add081.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + + + +int phi(int x) + +{ + + int p=x; + + for (int i=2;i*i<=x;i++) + + if (x%i==0) + + { + + while (x%i==0) + + x/=i; + + p=(p/i)*(i-1); + + } + + if (x!=1) + + p=(p/x)*(x-1); + + return p; + +} + + + +int inv_mod(int x, int mod) + +{ + + int p = phi(mod) - 1; + + int rez = 1; + + while (p) + + { + + if (p%2) + + rez = (1LL * rez * x) % mod; + + x = (1LL * x * x) % mod; + + p = p / 2; + + } + + return rez; + +} + + + +int main() + +{ + + f>>a>>n; + + g< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a , n; + + + +int phi(int n) + +{ + + int sol = n, p; + + for(p = 2; n > 1 && p * p <= n; p++) + + if(n % p == 0) + + { + + sol = sol/p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lgput(int a, int n, int modulo) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % modulo; + + n /= 2; + + a = 1LL * a * a % modulo; + + } + + return p; + + + +} + + + + + +int main() + +{ + + int b; + + fin >> a >> n; + + b = lgput(a, phi(n) - 1, n); + + fout << b; + + return 0; + +} diff --git a/invmod/euler-toutient/6f2a63a7-9eae-4e1c-bd8e-6f04589d7536.cpp b/invmod/euler-toutient/6f2a63a7-9eae-4e1c-bd8e-6f04589d7536.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c500369c6f64ef3a6d1dfec5f8496fa5b5d745 --- /dev/null +++ b/invmod/euler-toutient/6f2a63a7-9eae-4e1c-bd8e-6f04589d7536.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , i; + + for (i = 2 ; i * i <= n && n > 1 ; i++) + + { + + if (n % i == 0) + + sol = sol / i * (i - 1); + + while (n % i == 0) + + n /= i; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +long long log(int a , int n , int mod) + +{ + + long long p = 1; + + while (n > 0) + + { + + if (n % 2 == 1) p = 1LL*p * a % mod; + + n /= 2; + + a = 1LL*a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + cout << Phi(12); + + int a , n; + + fin >> a >> n; + + fout << log(a , Phi(n) - 1 , n); + + return 0; + +} diff --git a/invmod/euler-toutient/70ec2f39-5d89-4ed5-916e-ab5cadffa677.cpp b/invmod/euler-toutient/70ec2f39-5d89-4ed5-916e-ab5cadffa677.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ad2238b752a72eb40ee1cbb03ee245a1437c24 --- /dev/null +++ b/invmod/euler-toutient/70ec2f39-5d89-4ed5-916e-ab5cadffa677.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int GetPhi(int nr) + +{ + + int phi = nr; + + + + for(int i = 2; i * i <= nr; i++) + + if(nr % i == 0) + + { + + phi /= i; + + phi *= (i - 1); + + + + while(nr % i == 0) + + nr /= i; + + } + + + + if(nr != 1) + + { + + phi /= nr; + + phi *= (nr - 1); + + } + + + + return phi; + +} + + + +int lgput(int base, int exp, int N) + +{ + + int ans = 1; + + int aux = base; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = (1LL * ans * aux) % N; + + + + aux = (1LL * aux * aux) % N; + + } + + + + return ans; + +} + + + +int main() + +{ + + int A, N; fin >> A >> N; + + + + int phi = GetPhi(N); + + + + fout << lgput(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/7198219d-9308-4aa0-81fc-a58af5062265.cpp b/invmod/euler-toutient/7198219d-9308-4aa0-81fc-a58af5062265.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f97ddcf3168dd96a0f8b9c79a1340dc6a8b4207b --- /dev/null +++ b/invmod/euler-toutient/7198219d-9308-4aa0-81fc-a58af5062265.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + + + +const long long mod=1e9+7; + + + +long long a,n; + + + +long long phi(long long x){ + + long long rs=x; + + for(long long i=2LL;i*i<=x;i++){ + + if(x%i==0LL){ + + rs=((rs/i)*(i-1LL)); + + while(x%i==0LL) x/=i; + + } + + } + + if(x>=2LL) rs=((rs/x)*(x-1LL)); + + return rs; + +} + + + +long long lgp(long long a,long long b){ + + if(b==0LL) return 1LL; + + else if(b==1LL) return (a%n); + + else{ + + long long cc=(lgp(a,b/2LL)%n); + + if(b%2LL==1LL) return ((((cc%n)*(cc%n))%n)*(a%n))%n; + + else return ((cc%n)*(cc%n))%n; + + } + +} + + + +int32_t main(){ + + + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin >> a >> n; + + fout << lgp(a,phi(n)-1LL)%n; + +} diff --git a/invmod/euler-toutient/7226e949-f6cf-40e4-900a-3f27abd18650.cpp b/invmod/euler-toutient/7226e949-f6cf-40e4-900a-3f27abd18650.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b67f4c23991b08061ac436856fd11921ca4aef2 --- /dev/null +++ b/invmod/euler-toutient/7226e949-f6cf-40e4-900a-3f27abd18650.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long y,n; + +void phi(long long a) + +{ + + long long p=a,s=a,x; + + for(long long i=2;i*i<=a;++i) + + { + + if(a%i==0) + + { + + while(a%i==0)a/=i; + + p=p/i*(i-1); + + } + + } + + if(a>1)p=p/a*(a-1); + + x=p-1; + + p=y; + + y=1; + + while(x) + + { + + if(x%2)y*=p,y%=s; + + p=(p*p)%s; + + x/=2; + + } + +} + +int main() + +{ + + fin>>y>>n; + + phi(n); + + fout < +#include + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long eulerTotient(long long n) { + + long long sol = n; + + if (n % 2 == 0) { + + sol = sol / 2; + + while (n % 2 == 0) + + n /= 2; + + } + + for (int d = 3; d * d <= n; d += 2) + + if (n % d == 0) { + + sol = sol / d * (d - 1); + + while (n % d == 0) + + n /= d; + + } + + if (n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + + + +long long raise(long long base, long long exp, long long n) { + + long long result = 1; + + while (exp) { + + if (exp & 1) + + result = (result * base) % n; + + base = (base * base) % n; + + exp >>= 1; + + } + + return result; + +} + + + +int main() { + + long long a, n; + + fin >> a >> n; + + fout << raise(a, eulerTotient(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/732e5dd1-b522-4a9a-8696-05d9d58d0ccd.cpp b/invmod/euler-toutient/732e5dd1-b522-4a9a-8696-05d9d58d0ccd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a6b3cb3e06a6770b17c11323c628c8777603aae --- /dev/null +++ b/invmod/euler-toutient/732e5dd1-b522-4a9a-8696-05d9d58d0ccd.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long int n,pr; + +int MOD,invers,mod; + + + +void exp(long long int nr,long long int ad,long long int x) + + { + + if(nr!=pr) + + { + + if(nr+ad<=pr) { + + invers=(invers*x)%MOD; + + x=(x*x)%MOD; + + nr+=ad; + + ad*=2; + + exp(nr,ad,x); + + } + + else exp(nr,1,n); + + } + + } + +int main() + +{ + +f>>n>>mod; + +MOD=mod; + +pr=mod; + +int d=3; + +if(mod%2==0) + + { + + pr=pr/2; + + while(mod%2==0) + + mod/=2; + + } + +while(d*d<=mod&&mod!=1) + + { + + if(mod%d!=0) d+=2; + + else + + { + + pr=pr/d*(d-1); + + while(mod%d==0) + + mod/=d; + + } + + } + +if(mod!=1) pr=pr/mod*(mod-1); + +pr--; + +invers=n; + +if(pr==0) g<<"1"; + +else + +{ + +exp(1,1,n); + +g< + + +std::ifstream fin ("inversmodular.in"); + +std::ofstream fout ("inversmodular.out"); + + + +int64_t Pow(int64_t a, int64_t p, int64_t m) + +{ + + int64_t res = 1; + + while (p > 0) + + { + + if (p % 2 == 1) + + { + + res = (res * a) % m; + + } + + a = (a * a) % m; + + p /= 2; + + } + + return res; + +} + + + +int64_t Phi(int64_t n) + +{ + + int64_t r = 1, d = 2, p = 0; + + while (n > 1) + + { + + p = 0; + + while (n % d == 0) + + { + + n /= d; + + ++p; + + } + + if (p != 0) + + { + + r *= (d - 1) * Pow(d, p - 1, LONG_LONG_MAX); + + } + + ++d; + + if(n > 1 && d * d > n) + + { + + d = n; + + } + + } + + return r; + +} + + + +int main() + +{ + + int64_t a, n, p; + + + + fin >> a >> n; + + + + fout << Pow(a, Phi(n) - 1, n); + + + + return 0; + +} diff --git a/invmod/euler-toutient/73c57779-bca4-4cf5-9e1a-ee6e2d3f1b5e.cpp b/invmod/euler-toutient/73c57779-bca4-4cf5-9e1a-ee6e2d3f1b5e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7acce6a788b5cf1c7391439c21056b37fdd98cbf --- /dev/null +++ b/invmod/euler-toutient/73c57779-bca4-4cf5-9e1a-ee6e2d3f1b5e.cpp @@ -0,0 +1,72 @@ +#include + + + + + + +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/73deb00f-0277-41f4-ab7a-d13df01d4d3c.cpp b/invmod/euler-toutient/73deb00f-0277-41f4-ab7a-d13df01d4d3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee5e59696a86ee4e4c8e159206633bba0999196d --- /dev/null +++ b/invmod/euler-toutient/73deb00f-0277-41f4-ab7a-d13df01d4d3c.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + + + +long long A,N,E,Sol; + + + +void EulerTotient(long long n, long long &ans) + +{ + + ans=1; + + long long d=2,p=0; + + while(n>1) + + { + + p=0; + + while(n%d==0) + + { + + n/=d; + + p++; + + } + + if(p) + + { + + ans=ans*(d-1); + + for(int i=1;i1 && d*d>n) + + d=n; + + } + +} + + + +void Pow(long long a, long long power) + +{ + + if(power<=1) + + { + + if(power==1) + + Sol=a; + + else + + Sol=0; + + return; + + } + + else + + { + + if(power%2==0) + + { + + Pow(a,power/2); + + Sol=(Sol*Sol)%N; + + } + + else + + { + + Pow(a,power-1); + + Sol=(Sol*a)%N; + + } + + } + +} + + + +int main() + +{ + + fin>>A>>N; + + EulerTotient(N,E); + + Pow(A,E-1); + + fout< + + +using namespace std; + + + + + +int main() { + + freopen ("inversmodular.in", "r", stdin); + + freopen ("inversmodular.out", "w", stdout); + + + + auto get_phi = [&] (int num) { + + int phi = num; + + for (int d = 2; d * d <= num; d++) { + + bool is = 0; + + while (num % d == 0) { + + num /= d; + + is = 1; + + } + + if (is) { + + phi /= d; + + phi *= (d - 1); + + } + + } + + if (num > 1) { + + phi /= num; + + phi *= (num - 1); + + } + + return phi; + + }; + + + + int a, mod; + + cin >> a >> mod; + + int b = get_phi(mod) - 1; + + int sol = 1; + + + + while (b) { + + if (b & 1) { + + sol = sol * (long long) a % mod; + + } + + a = a * (long long) a % mod; + + b /= 2; + + } + + cout << sol << "\n"; + + + + + + return 0; + +} diff --git a/invmod/euler-toutient/75212b70-e433-432b-90fc-7b8a96abf491.cpp b/invmod/euler-toutient/75212b70-e433-432b-90fc-7b8a96abf491.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72bed6bd95521ed193cb5cb982750404e8985c7e --- /dev/null +++ b/invmod/euler-toutient/75212b70-e433-432b-90fc-7b8a96abf491.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +unsigned long long a, MOD, x, d, fi, fm, i; + +unsigned long long rez; + +unsigned long long paw(int a, int b) + +{ + + unsigned long long r = 1, A = a; + + if(b == 0)return 1; + + while(b > 1) + + { + + if(b % 2 == 1)r = (r * A) % MOD; + + A = (A * A) % MOD; + + b = b / 2; + + } + + return (r * A) % MOD; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> MOD; + + + + x = MOD; + + d = 2; + + fi = 1; + + do + + { + + fm = 0; + + while(x % d == 0) + + { + + x = x / d; + + fm ++; + + } + + if(fm > 0)fi = (fi * paw(d, fm - 1) * (d - 1)) % MOD; + + d ++; + + if(d * d > x && x != 1)fi = (fi * (x - 1)) % MOD, x = 1; + + }while(x != 1); + + g << paw(a, fi - 1) % MOD << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/77145975-1ad1-45af-b097-0044e993a3d8.cpp b/invmod/euler-toutient/77145975-1ad1-45af-b097-0044e993a3d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bbe5638400e106f768e97362c943ab0fdc02428e --- /dev/null +++ b/invmod/euler-toutient/77145975-1ad1-45af-b097-0044e993a3d8.cpp @@ -0,0 +1,78 @@ +#include + + +using namespace std; + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int n,ct; + +int fi(int n) + +{ + + int d=2; + + long long ans=n; + + while(n>1 and d*d<=n) + + { + + int p=0; + + while(n%d==0) + + n/=d, p=1; + + if(p) + + ans=1LL*ans/d*(d-1)%ct; + + d++; + + } + + if(n!=1) + + ans=1LL*ans*(n-1)/n; + + return ans; + +} + +long long put(int a, int b) + +{ + + a%=ct; + + if(b==0) + + return 1; + + if(b%2) + + return 1LL*a*put(a,b-1)%ct; + + long long p=put(a,b/2); + + return 1LL*p*p%ct; + +} + +int main() + +{ + + int a; + + cin>>a>>n; + + ct=n; + + cout<<1LL*put(a,fi(n)-1)%ct; + +} diff --git a/invmod/euler-toutient/775ed291-bae7-4d55-883e-997fdda6d5af.cpp b/invmod/euler-toutient/775ed291-bae7-4d55-883e-997fdda6d5af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/775ed291-bae7-4d55-883e-997fdda6d5af.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/7769cebe-b429-4230-b11b-f5f0d5c8ecbc.cpp b/invmod/euler-toutient/7769cebe-b429-4230-b11b-f5f0d5c8ecbc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca9e2d74b7c43cc6742d0d1df416fd4fd4d7c146 --- /dev/null +++ b/invmod/euler-toutient/7769cebe-b429-4230-b11b-f5f0d5c8ecbc.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a, n; + + fin >> a >> n; + + fout << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/786741f6-f7e5-4ce3-a6ee-c3e916f740bc.cpp b/invmod/euler-toutient/786741f6-f7e5-4ce3-a6ee-c3e916f740bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e135c8e8be1adc6e143fa8c8c13c3eb24d6715 --- /dev/null +++ b/invmod/euler-toutient/786741f6-f7e5-4ce3-a6ee-c3e916f740bc.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int euler(int n){ + + long long d = 2, p, prod = n; + + while ( d * d <= n ) { + + p = 0; + + while ( n % d == 0 ) { + + n /= d; + + p ++; + + } + + if ( p ) + + prod = prod / d * ( d - 1 ); + + d ++; + + } + + if ( n > 1 ) + + prod = prod / n * ( n - 1 ); + + return prod; + +} + +int putere(int a,int n,int m){ + + int prod = 1; + + do{ + + if(n%2 != 0){ + + prod = (long long)prod * a % m; + + } + + a = 1LL * a * a % m; + + n /= 2; + + }while(n != 0); + + return prod; + +} + +int main() + +{ + + int a,n,x; + + cin >> a >> n; + + x = putere(a,euler(n) - 1,n); + + cout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/78ab6c15-2139-4150-9c8f-e709d5a41ca0.cpp b/invmod/euler-toutient/78ab6c15-2139-4150-9c8f-e709d5a41ca0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28d6de0037cbe6142907d974d08308605ad16663 --- /dev/null +++ b/invmod/euler-toutient/78ab6c15-2139-4150-9c8f-e709d5a41ca0.cpp @@ -0,0 +1,104 @@ +#include + + +using namespace std; + + + +int GetPhi(int x) + +{ + + int phi = x; + + + + for(int i = 2; i * i <= x; i++) + + if(x % i == 0) + + { + + phi = (phi / i) * (i - 1); + + + + while(x % i == 0) + + x /= i; + + } + + + + if(x != 1) + + phi = (phi / x) * (x - 1); + + + + return phi; + +} + + + +int RidPut(int base, int exp, int mod) + +{ + + int sol = 1; + + int aux = base % mod; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + sol = 1LL * sol * aux % mod; + + + + aux = 1LL * aux * aux % mod; + + } + + + + return sol; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, N; + + + + fin >> A >> N; + + + + int phi = GetPhi(N); + + fout << RidPut(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/78d9e540-1487-44bb-a27d-ab61f3537b76.cpp b/invmod/euler-toutient/78d9e540-1487-44bb-a27d-ab61f3537b76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92d72c5d1a148b50ff73f42cc1108ad7bf0ea7c3 --- /dev/null +++ b/invmod/euler-toutient/78d9e540-1487-44bb-a27d-ab61f3537b76.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long n,a,x,tot,aa,d=2,k,i,sol=1,nr; + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin >> a >> n; + + aa=n; + + tot=n; + + while(d*d<=aa) + + { + + if(aa%d==0) + + { + + while(aa%d==0) + + aa/=d; + + tot/=d; + + tot*=d-1; + + } + + d++; + + } + + if(aa>1) + + tot/=aa, + + tot*=aa-1; + + tot--; + + nr=a; + + for(long long p=1;p<=tot;p<<=1) + + { + + if(p&tot)sol=(sol*nr)%n; + + nr=(nr*nr)%n; + + } + + fout << sol; + + return 0; + +} diff --git a/invmod/euler-toutient/7a983e89-e197-4add-8c0e-599fef41dcdd.cpp b/invmod/euler-toutient/7a983e89-e197-4add-8c0e-599fef41dcdd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7b3025548b478c32caff0dfbcc4ed822058bced --- /dev/null +++ b/invmod/euler-toutient/7a983e89-e197-4add-8c0e-599fef41dcdd.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +int euler(int a) + +{ + + int phi = a , d , e; + + d = 2; + + while(1LL * d * d <= a && a > 1) + + { + + e = 0; + + while(a % d == 0) + + { + + a = a / d; + + e ++; + + } + + if(e > 0) + + phi = phi / d * (d - 1); + + d ++; + + } + + if(a > 1) + + phi = phi / a * (a - 1); + + return phi; + +} + +long long fast_pow(int a , int b , int MOD) + +{ + + long long aa = a , p; + + for(p = 1 ; b ; b = b >> 1) + + { + + if(b & 1) + + p = (p * aa) % MOD; + + aa = (aa * aa) % MOD; + + } + + return p; + +} + +int main() + +{ + + freopen("inversmodular.in" , "r" , stdin); + + freopen("inversmodular.out" , "w" , stdout); + + int a , n; + + scanf("%d%d" , &a , &n); + + printf("%lld\n" , fast_pow(a , euler(n) - 1 , n)); + + return 0; + +} diff --git a/invmod/euler-toutient/7d081910-7a7d-47d1-83ee-5f98e97a3281.cpp b/invmod/euler-toutient/7d081910-7a7d-47d1-83ee-5f98e97a3281.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adf36736389f83aa4c3df08512ebf4b9b24ebb70 --- /dev/null +++ b/invmod/euler-toutient/7d081910-7a7d-47d1-83ee-5f98e97a3281.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long eul(long long n) + +{ + + long long r = n, d = 2; + + while (n > 1) + + { + + if (n % d == 0) + + { + + r /= d; + + r *= (d - 1); + + while (n % d == 0) + + n /= d; + + } + + ++d; + + if (d * d > n) + + d = n; + + } + + return r; + +} + + + +long long mod; + + + +long long lgput(long long a, long long b) + +{ + + long long p = 1; + + while (b) + + { + + if (b & 1) + + { + + p *= a; + + p %= mod; + + b--; + + } + + else + + { + + a *= a; + + a %= mod; + + b = b >> 1; + + } + + } + + return p; + +} + + + +int main() + +{ + + long long a, n; + + fin >> a >> n; + + long long phi = eul(n) - 1; + + mod = n; + + fout << lgput(a, phi) << "\n"; + + return 0; + + + +} diff --git a/invmod/euler-toutient/7ecc4c54-1831-4ab7-810a-d7de9684dee7.cpp b/invmod/euler-toutient/7ecc4c54-1831-4ab7-810a-d7de9684dee7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e7af4c7edebee5f338ef1eea2ba90924eef67b6 --- /dev/null +++ b/invmod/euler-toutient/7ecc4c54-1831-4ab7-810a-d7de9684dee7.cpp @@ -0,0 +1,43 @@ +#include + +int A, N; + +int totient(int N) { + int result = N; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + result = result / i * (i - 1); + while (N % i == 0) { + N /= i; + } + } + } + + if (N != 1) { + result = result / N * (N - 1); + } + + return result; +} + +int lgput(int base, int exponent, int MOD) { + int result = 1, basePow2Value = base; + for (int powIdx = 0; (1LL << powIdx) <= exponent; powIdx++) { + if (exponent & (1 << powIdx)) { + result = (1LL * result * basePow2Value) % MOD; + } + + basePow2Value = (1LL * basePow2Value * basePow2Value) % MOD; + } + + return result; +} + +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d%d", &A, &N); + printf("%d\n", lgput(A, totient(N) - 1, N)); + return 0; +} diff --git a/invmod/euler-toutient/7f378971-aa99-4ebb-945a-1566dc2c278b.cpp b/invmod/euler-toutient/7f378971-aa99-4ebb-945a-1566dc2c278b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7affe01690ced680cd6821984f7f2bdff648264 --- /dev/null +++ b/invmod/euler-toutient/7f378971-aa99-4ebb-945a-1566dc2c278b.cpp @@ -0,0 +1,88 @@ +#include + + + + +using namespace std; + + + +long long N,M; + + + +long long getphi(long long nr){ + + long long cur = nr; + + + + for(long long i = 2; i * i <= nr; ++i){ + + if (nr % i == 0){ + + while(nr % i == 0) + + nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + + + if (nr != 1) + + cur = cur / nr * (nr - 1); + + + + return cur; + +} + + + +int main(){ + + + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + + + scanf("%lld %lld\n",&N,&M); + + + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + + + for(long long p = 1; p <= put; p <<= 1){ + + if (p & put) + + crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + + + printf("%lld\n",crt); + + + + return 0; + +} diff --git a/invmod/euler-toutient/7fca8a11-bff8-4447-a923-54112fb76861.cpp b/invmod/euler-toutient/7fca8a11-bff8-4447-a923-54112fb76861.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e88a0c2d51177af5ac6514abe881ddac8bf0499f --- /dev/null +++ b/invmod/euler-toutient/7fca8a11-bff8-4447-a923-54112fb76861.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n, mod, phi=1, ex(int, int); + +void desc(int); + +int main() + +{ + + f >> n >> mod; + + desc(mod); + + g << ex(n, phi - 1); + + return 0; + +} + +void desc(int N) + +{ + + int d = 3; + + if(N % 2 == 0) + + { + + N /= 2; + + while(N % 2 == 0) + + N /= 2, phi *= 2; + + } + + while(d * d <= N) + + { + + if(N % d == 0) + + { + + N /= d; + + phi *= (d - 1); + + while(N % d == 0) + + N /=d, phi *= d; + + } + + d += 2; + + } + + if(N > 1) + + phi *= N - 1; + +} + +int ex(int b, int exponent) + +{ + + if(!exponent) + + return 1; + + int r = ex(b, exponent / 2); + + r = 1LL * r * r % mod; + + if(exponent % 2) + + r = 1LL * r * b % mod; + + return r; + +} diff --git a/invmod/euler-toutient/809d707e-6910-4ee2-b842-bb0259d03db7.cpp b/invmod/euler-toutient/809d707e-6910-4ee2-b842-bb0259d03db7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12a00a9ecfd081748c6d1c031aa80267c598671b --- /dev/null +++ b/invmod/euler-toutient/809d707e-6910-4ee2-b842-bb0259d03db7.cpp @@ -0,0 +1,54 @@ +#include + +using namespace std; + + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +long long unsigned phi(long long unsigned n) +{ + long long unsigned nr = n; + + for(long long unsigned i = 2; i * i <= n; i++) + { + if(n % i == 0) + { + while(n % i == 0) + { + n /= i; + } + + nr = (nr / i) * (i - 1); + cout<>n>>m; + + fout< + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int A, N; + + + +int Fi(int n) + +{ + + int d; + + int fi; + + fi = n; + + d = 2; + + while (n > 1 && d * d <= n) + + { + + if (n % d == 0) + + { + + fi = (fi / d) * (d - 1); + + while (n % d == 0) + + n /= d; + + } + + d++; + + } + + if (n > 1) + + fi = fi / n * (n - 1); + + + + return fi; + +} + + + +int PutLog(int a, int n, int modulo) + +{ + + long long rez = 1; + + while (n != 0) + + { + + if (n % 2 == 1) + + rez = (1LL * rez * a) % modulo; + + n /= 2; + + a = (1LL * a * a) % modulo; + + } + + return rez; + +} + + + +int main() + +{ + + int fi, prod; + + fin >> A >> N; + + + + fi = Fi(N) - 1; + + + + prod = PutLog(A, fi, N); + + + + fout << prod << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/83268957-e77d-4768-a71f-38941c6feb0e.cpp b/invmod/euler-toutient/83268957-e77d-4768-a71f-38941c6feb0e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..637a7ad19fc4be33c38ec4b0dae9b10777adf7ba --- /dev/null +++ b/invmod/euler-toutient/83268957-e77d-4768-a71f-38941c6feb0e.cpp @@ -0,0 +1,204 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + + + +ofstream fout("inversmodular.out"); + + + +long long eul(long long n) + + + +{ + + + +long long r = n, d = 2; + + + +while (n > 1) + + + +{ + + + +if (n % d == 0) + + + +{ + + + +r /= d; + + + +r *= (d - 1); + + + +while (n % d == 0) + + + +n /= d; + + + +} + + + +++d; + + + +if (d * d > n) + + + +d = n; + + + +} + + + +return r; + + + +} + + + +long long mod; + + + +long long lgput(long long a, long long b) + + + +{ + + + +long long p = 1; + + + +while (b) + + + +{ + + + +if (b & 1) + + + +{ + + + +p *= a; + + + +p %= mod; + + + +b--; + + + +} + + + +else + + + +{ + + + +a *= a; + + + +a %= mod; + + + +b = b >> 1; + + + +} + + + +} + + + +return p; + + + +} + + + +int main() + + + +{ + + + +long long a, n; + + + +fin >> a >> n; + + + +long long phi = eul(n) - 1; + + + +mod = n; + + + +fout << lgput(a, phi) << "\n"; + + + +return 0; + + + +} diff --git a/invmod/euler-toutient/83770dd8-f0a6-4e17-9e2a-0a9fefacb504.cpp b/invmod/euler-toutient/83770dd8-f0a6-4e17-9e2a-0a9fefacb504.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21e76271363d2673816c5353f5edf8e54e19a7eb --- /dev/null +++ b/invmod/euler-toutient/83770dd8-f0a6-4e17-9e2a-0a9fefacb504.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int GetPhi(int x) + +{ + + int phi = x; + + + + for(long long i = 2; 1LL * i * i <= x; i++) + + if(x % i == 0) + + { + + phi /= i; + + phi *= (i - 1); + + + + while(x % i == 0) + + x /= i; + + } + + + + if(x != 1) + + { + + phi /= x; + + phi *= (x - 1); + + } + + + + return phi; + +} + + + +long long RidPut(int base, int exp, int mod) + +{ + + long long ans = 1; + + long long aux = base % mod; + + + + for(long long i = 1; 1LL * i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = (1LL * ans * aux) % mod; + + + + aux = (1LL * aux * aux) % mod; + + } + + + + return ans % mod; + +} + + + +int main() + +{ + + int A, N; + + + + fin >> A >> N; + + + + int phi = GetPhi(N); + + + + fout << RidPut(A, phi - 1, N) << '\n'; + + + + return 0; + +} diff --git a/invmod/euler-toutient/840cd047-6e9d-40f8-b109-cd20cf94e35f.cpp b/invmod/euler-toutient/840cd047-6e9d-40f8-b109-cd20cf94e35f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b1a775f9092ed6f4dced18e0c15f01e8fd1c544 --- /dev/null +++ b/invmod/euler-toutient/840cd047-6e9d-40f8-b109-cd20cf94e35f.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + + + +ifstream ci ("inversmodular.in"); + +ofstream co ("inversmodular.out"); + + + +long long putere (long long a,long long n,long long m) + +{ + + long long p=1; + + while (n) + + { + + if (n%2!=0) + + { + + p=p*a%m; + + } + + n/=2; + + a=a*a%m; + + } + + return p; + +} + + + +int main() + +{ + + long long a,n,b,d=2, nn; + + ci >> a >> n; + + nn = n; + + b=n; + + while (d*d<=n) + + { + + if(n%d==0) + + { + + b=b/d*(d-1); + + while (n%d==0) + + { + + n/=d; + + } + + } + + d++; + + } + + if(n>1) + + { + + b=b/n*(n-1); + + } + + co << putere(a,b-1,nn); + + return 0; + +} diff --git a/invmod/euler-toutient/84799e0e-216d-48cb-873f-2db9d0a783d2.cpp b/invmod/euler-toutient/84799e0e-216d-48cb-873f-2db9d0a783d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779d8e7928b2cac705cfc57683ef2015013f4ef1 --- /dev/null +++ b/invmod/euler-toutient/84799e0e-216d-48cb-873f-2db9d0a783d2.cpp @@ -0,0 +1,81 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +int Phi(int n) + +{ + + int p,sol; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + { + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} + + + +int main() + +{ + + int a,n; + + fin>>a>>n; + + fout< + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int A, N; + + + +int Fi(int n) + +{ + + int d; + + int fi; + + fi = n; + + d = 2; + + while (n > 1 && d * d <= n) + + { + + if (n % d == 0) + + { + + fi = (fi / d) * (d - 1); + + while (n % d == 0) + + n /= d; + + } + + d++; + + } + + if (n > 1) + + fi = fi / n * (n - 1); + + + + return fi; + +} + + + +int PutLog(int a, int n, int modulo) + +{ + + long long rez = 1; + + while (n != 0) + + { + + if (n % 2 == 1) + + rez = (1LL * rez * a) % modulo; + + n /= 2; + + a = (1LL * a * a) % modulo; + + } + + return rez; + +} + + + +int main() + +{ + + int fi, prod; + + fin >> A >> N; + + + + fi = Fi(N) - 1; + + + + prod = PutLog(A, fi, N); + + + + fout << prod << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/87de5d21-4468-4a73-a0f3-24c958a37c1e.cpp b/invmod/euler-toutient/87de5d21-4468-4a73-a0f3-24c958a37c1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcabc970d45a0502fa662abd61deca26be005b28 --- /dev/null +++ b/invmod/euler-toutient/87de5d21-4468-4a73-a0f3-24c958a37c1e.cpp @@ -0,0 +1,78 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long int n,P; + +long long int InversModular(long long int x,long long int k) + +{ + + long long int rez=1; + + while(k) + + { + + if(k&1) + + rez=(rez*x)%P; + + x=(x*x)%P; + + k>>=1; + + } + + return rez; + +} + +int main() + +{ + + f>>n>>P; + + long long int copie=P,phi=P,d=2; + + while(copie>1) + + { + + if(!(copie%d)) + + { + + phi*=(d-1),phi/=d; + + while(!(copie%d)) + + copie/=d; + + } + + d++; + + if(d*d>copie&&copie>1) + + d=copie; + + } + + g< + + + +long long N,M; + +long long getphi(long long nr) +{ + long long cur = nr; + for(long long i = 2;i * i <= nr; ++i) + { + if (nr % i == 0) + { + while(nr % i == 0)nr /= i; + cur = (cur / i) * (i - 1); + } + } + if (nr != 1) cur = cur / nr * (nr - 1); + return cur; +} + +int main() +{ + freopen("inversmodular.in","r",stdin); + freopen("inversmodular.out","w",stdout); + scanf("%lld %lld\n",&N,&M); + long long put = getphi(M) - 1; + long long nr = N; + long long crt = 1; + for(long long p = 1;p <= put;p <<= 1) + { + if (p & put) crt = (crt * nr) % M; + nr = (nr * nr) % M; + } + printf("%lld\n",crt); + return 0; +} diff --git a/invmod/euler-toutient/8d40ab6e-e71d-4f2a-b78d-c376ce802c39.cpp b/invmod/euler-toutient/8d40ab6e-e71d-4f2a-b78d-c376ce802c39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779d8e7928b2cac705cfc57683ef2015013f4ef1 --- /dev/null +++ b/invmod/euler-toutient/8d40ab6e-e71d-4f2a-b78d-c376ce802c39.cpp @@ -0,0 +1,81 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +int Phi(int n) + +{ + + int p,sol; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + { + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} + + + +int main() + +{ + + int a,n; + + fin>>a>>n; + + fout< + + +using namespace std; + + + +ifstream fin( "inversmodular.in" ); + +ofstream fout( "inversmodular.out" ); + + + +int phi_gen( int x ){ + + int d = 2, cx = x, e; + + while( d * d <= x ){ + + e = 0; + + while( !(x % d) ){ + + e = 1; + + x /= d; + + } + + if( e == 1 ){ + + cx /= d; + + cx *= (d - 1); + + } + + ++d; + + } + + if( x > 1 ){ + + cx /= x; + + cx *= (x - 1); + + } + + return cx; + +} + + + +int lgput( int x, int y, int n ){ + + int p = 1; + + while( y ){ + + if( y & 1 ) + + p = (1LL * p * x) % n; + + x = (1LL * x * x) % n; + + y >>= 1; + + } + + return p; + +} + + + +int main() { + + int a, n, y; + + fin >> a >> n; + + y = phi_gen(n); + + fout << lgput(a, y - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/8da46503-a650-4b06-8d26-69ed2b886cdd.cpp b/invmod/euler-toutient/8da46503-a650-4b06-8d26-69ed2b886cdd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10f5e3919ba8266c82a7c51bc5d9bc3e25ef9967 --- /dev/null +++ b/invmod/euler-toutient/8da46503-a650-4b06-8d26-69ed2b886cdd.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a, mod; + +long long phi(long long); + +long long lgput(long long, long long); + +int main() + +{ + + f >> a >> mod; + + g << lgput(a, phi(mod) - 1); + + return 0; + +} + +long long lgput(long long a, long long b) + +{ + + long long rez = 1; + + for (long long i = 1; b; i <<= 1) + + { + + if (i & b) + + b ^= i, rez *= a, rez %= mod; + + a *= a; + + a %= mod; + + } + + return rez; + +} + +long long phi(long long x) + +{ + + long long rez = x; + + bool ok = 0; + + while (x % 2 == 0) + + x /= 2, ok = 1; + + if (ok) + + rez /= 2; + + ok = 0; + + for (long long d = 3; d * d <= x; d += 2) + + { + + while (x % d == 0) + + x /= d, ok = 1; + + if (ok) + + rez /= d, rez *= d - 1, ok = 0; + + } + + if (x != 1) + + rez /= x, rez *= x - 1; + + return rez; + +} diff --git a/invmod/euler-toutient/8f441c2c-a01b-4573-9555-2031ea19b5aa.cpp b/invmod/euler-toutient/8f441c2c-a01b-4573-9555-2031ea19b5aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db32e70d02df252372d5d36ab43ab3abcc433dcd --- /dev/null +++ b/invmod/euler-toutient/8f441c2c-a01b-4573-9555-2031ea19b5aa.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol,p; + + sol = n; + + p = 2; + + for( p = 2; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p *(p-1); + + while(n%p == 0) + + n/=p; + + } + + } + + if(n > 1) + + sol = sol / n *(n-1); + + return sol; + +} + + + +int exp(int a, int n,int mod) + +{ + + int sol=1; + + while(n>0) + + { + + if(n % 2 == 1) + + sol = 1LL*sol*a%mod; + + n/=2; + + a=1LL*a*a%mod; + + + + } + + return sol; + + + +} + + + +int main() + +{ + + int a , n; + + fin >> a >> n; + + int b; + + b=exp(a,Phi(n)-1,n); + + fout< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int n; + + + +bool IsPrime (int x) + +{ + + if (x <= 1) return false; + + if (x != 2 && !(x & 1)) return false; + + for (int i = 3; i * i <= x; i += 2) + + if (x % i == 0) + + return false; + + return true; + + + +} + + + +int Phi (int x) + +{ + + int f, phi; + + if (IsPrime(x)) + + return x - 1; + + phi = x; + + f = 2; + + while (f * f <= x && x > 1) + + { + + if (!(x % f)) + + { + + while (!(x % f)) + + x /= f; + + phi /= f; + + phi *= (f - 1); + + } + + f++; + + } + + if (x > 1) + + { + + phi /= x; + + phi *= (x - 1); + + } + + return phi; + +} + + + +int Fap (int a, int b) + +{ + + int p = 1; + + while (b) + + { + + if (b & 1) + + p = 1LL * p * a % n; + + b >>= 1; + + a = 1LL * a * a % n; + + } + + return p; + +} + + + +void Solve () + +{ + + int a; + + fin >> a >> n; + + fout << Fap(a, Phi(n) - 1); + + fout.close(); + +} + + + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/invmod/euler-toutient/8fc2983f-3e98-4539-8838-74d4cb33d1dc.cpp b/invmod/euler-toutient/8fc2983f-3e98-4539-8838-74d4cb33d1dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1605386209a77d08324db41ac3cb75159550e49 --- /dev/null +++ b/invmod/euler-toutient/8fc2983f-3e98-4539-8838-74d4cb33d1dc.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + + + +long long MOD; + + + +long long fastPow(long long nr, long long p){ + + long long aux=1; + + while(p){ + + if(p%2) + + aux=(aux*nr)%MOD; + + nr=(nr*nr)%MOD; + + p/=2; + + } + + return aux%MOD; + +} + + + +long long getPhi(long long n){ + + long long sol=n,d=2; + + while(d*d<=n){ + + if(n%d==0){ + + while(n%d==0) + + n/=d; + + sol=(sol/d)*(d-1); + + } + + d++; + + } + + if(n>1) + + sol=(sol/n)*(n-1); + + return sol; + +} + + + +int main(){ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + long long n; + + scanf("%lld%lld", &n, &MOD); + + printf("%lld", fastPow(n, getPhi(MOD)-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/903677ed-c820-427b-8496-23493e80c608.cpp b/invmod/euler-toutient/903677ed-c820-427b-8496-23493e80c608.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51b87a8edcf9646c6fa610486f8ef1bf777ba422 --- /dev/null +++ b/invmod/euler-toutient/903677ed-c820-427b-8496-23493e80c608.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + +int a, n, ind; + +int putere(long long a, long long b) + +{ + + long long p = 1; + + while(b) + + { + + if(b%2==1) + + p=(p*a)%n; + + a=(a*a)%n; + + b/=2; + + } + + return p; + +} + +int indicator(int n) + +{ + + int f = 2, p, rez = 1; + + while(f*f<=n) + + { + + if(n%f == 0) + + { + + p = 0; + + while(n%f==0) + + { + + p++; + + n/=f; + + } + + rez*=putere(f, p-1)*(f-1); + + } + + f++; + + } + + if(n > 1) + + rez*=(n-1); + + return rez; + +} + +int main() + +{ + + fin >> a >> n; + + ind = indicator(n); + + a = putere(a, ind-1); + + fout << a; + + return 0; + +} diff --git a/invmod/euler-toutient/921ea6b3-5d58-4d4b-9a13-4f761c01e565.cpp b/invmod/euler-toutient/921ea6b3-5d58-4d4b-9a13-4f761c01e565.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe31671a3eeb3aa1d052f100c3aa4f1af676cf12 --- /dev/null +++ b/invmod/euler-toutient/921ea6b3-5d58-4d4b-9a13-4f761c01e565.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long lgput(long long a, long long p, long long mod) + +{ + + long long res = 1; + + while(p > 0) + + { + + if(p % 2 == 1) + + res = (1LL * res * a) % mod; + + a = (1LL * a * a) % mod; + + p /= 2; + + } + + return res; + +} + +long long f(long long n) + +{ + + long long r = 1, d = 2; + + while(n > 1) + + { + + long long put = 0; + + while(n % d == 0) + + { + + n /= d; + + put ++; + + } + + if(put != 0) + + r *= 1LL * (d - 1) * lgput(d, put - 1, 20000000000000); + + d ++; + + if(n > 1 && d * d > n) + + d = n; + + } + + return r; + +} + +int32_t main() + +{ + + long long a, n; + + in >> a >> n; + + out << lgput(a, f(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/93fecc8e-4727-4f52-8679-031b5ee0926d.cpp b/invmod/euler-toutient/93fecc8e-4727-4f52-8679-031b5ee0926d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..511bbdef033e3dd3eeb1b9b550ff204adb9d11a0 --- /dev/null +++ b/invmod/euler-toutient/93fecc8e-4727-4f52-8679-031b5ee0926d.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void invers_mod(long long a,long long b,long long &x,long long &y) + +{ + + if(b==0) + + { + + x=1,y=0; + + } + + else + + { + + invers_mod(b,a%b,x,y); + + int aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + +} + + + + + +int main() + +{ + + long long n,m,x,y; + + fin>>n>>m; + + invers_mod(n,m,x,y); + + if(x<=0) + + x=m+x; + + fout< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int n; + + + +bool IsPrime (int x) + +{ + + if (x <= 1) return false; + + if (x != 2 && !(x & 1)) return false; + + for (int i = 3; i * i <= x; i += 2) + + if (x % i == 0) + + return false; + + return true; + + + +} + + + +int Phi (int x) + +{ + + int f, phi; + + if (IsPrime(x)) + + return x - 1; + + phi = x; + + f = 2; + + while (f * f <= x && x > 1) + + { + + if (!(x % f)) + + { + + while (!(x % f)) + + x /= f; + + phi /= f; + + phi *= (f - 1); + + } + + f++; + + } + + if (x > 1) + + { + + phi /= x; + + phi *= (x - 1); + + } + + return phi; + +} + + + +int Fap (int a, int b) + +{ + + int p = 1; + + while (b) + + { + + if (b & 1) + + p = 1LL * p * a % n; + + b >>= 1; + + a = 1LL * a * a % n; + + } + + return p; + +} + + + +void Solve () + +{ + + int a; + + fin >> a >> n; + + fout << Fap(a, Phi(n) - 1); + + fout.close(); + +} + + + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/invmod/euler-toutient/940b7acd-e4b0-40d6-9da1-abe1e86ac8ff.cpp b/invmod/euler-toutient/940b7acd-e4b0-40d6-9da1-abe1e86ac8ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2829269396b96d15ad163a14fd70f2735d60b9a7 --- /dev/null +++ b/invmod/euler-toutient/940b7acd-e4b0-40d6-9da1-abe1e86ac8ff.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int euclid( int a, int b, int &x, int &y ){ + + if( b == 0 ){ + + x = 1; + + y = 0; + + return a; + + }else{ + + int x0, y0; + + int cmmdc = euclid( b, a % b, x0, y0 ); + + x = y0; + + y = x0 - y0 * (a / b); + + return cmmdc; + + } + +} + +int A, N; + +int main(){ + + fin >> A >> N; + + int invmod, aux; + + int d = euclid( A, N, invmod, aux ); + + while( invmod < 0 ) + + invmod += N; + + fout << invmod << endl; + + return 0; + +} diff --git a/invmod/euler-toutient/943e07d9-3572-4175-b8ae-a35e066217f5.cpp b/invmod/euler-toutient/943e07d9-3572-4175-b8ae-a35e066217f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2829269396b96d15ad163a14fd70f2735d60b9a7 --- /dev/null +++ b/invmod/euler-toutient/943e07d9-3572-4175-b8ae-a35e066217f5.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int euclid( int a, int b, int &x, int &y ){ + + if( b == 0 ){ + + x = 1; + + y = 0; + + return a; + + }else{ + + int x0, y0; + + int cmmdc = euclid( b, a % b, x0, y0 ); + + x = y0; + + y = x0 - y0 * (a / b); + + return cmmdc; + + } + +} + +int A, N; + +int main(){ + + fin >> A >> N; + + int invmod, aux; + + int d = euclid( A, N, invmod, aux ); + + while( invmod < 0 ) + + invmod += N; + + fout << invmod << endl; + + return 0; + +} diff --git a/invmod/euler-toutient/961f85f9-8cf8-4dc6-b00d-11cbe177d055.cpp b/invmod/euler-toutient/961f85f9-8cf8-4dc6-b00d-11cbe177d055.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34efce924cd56e4d9fdbfb016dbcf383d712c14a --- /dev/null +++ b/invmod/euler-toutient/961f85f9-8cf8-4dc6-b00d-11cbe177d055.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + +long long a, putere, nr, r; + +long long n; + +long long fi(long n) + +{ + + long long aux=n,prod=1; + + long long d=2, nr=1; + + while(d*d<=aux) + + { + + if(aux%d==0) + + { + + while(aux%d==0) + + aux=aux/d; + + prod=prod*d; + + nr=nr*(d-1); + + } + + d++; + + } + + if(aux!=1) + + nr*=(aux-1); + + return (long long)n/prod*nr; + +} + +int main() + +{ + + fin >> a >> n; + + putere=fi(n)-1; + + nr=a; + + r=1; + + while(putere!=0) + + { + + if(putere%2==1) + + r=(r*nr)%n; + + nr=(nr*nr)%n; + + putere=putere/2; + + } + + fout << r; + + return 0; + +} diff --git a/invmod/euler-toutient/96522de1-6383-4e79-9c75-b76812180687.cpp b/invmod/euler-toutient/96522de1-6383-4e79-9c75-b76812180687.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7acce6a788b5cf1c7391439c21056b37fdd98cbf --- /dev/null +++ b/invmod/euler-toutient/96522de1-6383-4e79-9c75-b76812180687.cpp @@ -0,0 +1,72 @@ +#include + + + + + + +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/96d0cdde-f13a-401d-ae3b-cf0b38252a14.cpp b/invmod/euler-toutient/96d0cdde-f13a-401d-ae3b-cf0b38252a14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4ca226d54bd1f53691fe51bd315e443f5cba541 --- /dev/null +++ b/invmod/euler-toutient/96d0cdde-f13a-401d-ae3b-cf0b38252a14.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int main() { + + long long n, a, phi, cn, ans, d; + + cin >> a >> n; + + cn = n; + + phi = n; + + d = 2; + + while ( d * d <= n ) { + + if ( n % d == 0 ) { + + phi = phi / d * ( d - 1 ); + + while ( n % d == 0 ) + + n /= d; + + } + + d++; + + } + + if ( n > 1 ) + + phi = phi / n * ( n - 1 ); + + phi--; + + ans = 1; + + while ( phi > 0 ) { + + if ( phi % 2 != 0 ) + + ans = ans * a % cn; + + phi /= 2; + + a = a * a % cn; + + } + + cout << ans; + + return 0; + +} diff --git a/invmod/euler-toutient/97d8e60c-0469-422d-81c0-23b1c6c17205.cpp b/invmod/euler-toutient/97d8e60c-0469-422d-81c0-23b1c6c17205.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e016ff373c61182e12c2d0708e074895a7a9081e --- /dev/null +++ b/invmod/euler-toutient/97d8e60c-0469-422d-81c0-23b1c6c17205.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +int GetPhi(int X) + +{ + + int phi = X; + + + + for(int i = 2; i * i <= X; i++) + + if(X % i == 0) + + { + + phi = (phi / i) * (i - 1); + + + + while(X % i == 0) + + X /= i; + + } + + + + if(X != 1) + + phi = (phi / X) * (X - 1); + + + + return phi; + +} + + + +int RidPut(int base, int exp, int mod) + +{ + + int ans = 1; + + int aux = base % mod; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = 1LL * ans * aux % mod; + + + + aux = 1LL * aux * aux % mod; + + } + + + + return ans; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, N; + + fin >> A >> N; + + + + int phi = GetPhi(N); + + fout << RidPut(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/987ed25c-98bb-4ecf-ac8d-bfc409859eaa.cpp b/invmod/euler-toutient/987ed25c-98bb-4ecf-ac8d-bfc409859eaa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0f5cd976d89e8a9158d7896c0968f908e05afc8 --- /dev/null +++ b/invmod/euler-toutient/987ed25c-98bb-4ecf-ac8d-bfc409859eaa.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , p; + + for (p = 2 ; n > 1 && p * p <= n ; p++) + + if (n % p == 0) + + { + + sol = sol / p * (p - 1); + + while (n % p == 0) + + n /= p; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lg(int a , int b , int mod) + +{ + + int p = 1; + + while (b > 0) + + { + + if (b % 2 == 1) + + p = 1LL * p * a % mod; + + b /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a , n , rez; + + fin >> a >> n; + + rez = lg(a , Phi(n) - 1 , n); + + fout << rez; + + return 0; + +} diff --git a/invmod/euler-toutient/98dee52a-0b1c-452b-86f5-050b385ac125.cpp b/invmod/euler-toutient/98dee52a-0b1c-452b-86f5-050b385ac125.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c253a5476f703b568b935d45d83d9b3afe00925 --- /dev/null +++ b/invmod/euler-toutient/98dee52a-0b1c-452b-86f5-050b385ac125.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + + + + +typedef unsigned long long mare; + +int phi(int n) +{ + int rez = n; + int d; + for (d = 2; d*d<=n; d++) + if (n%d == 0) + { + rez = rez / d * (d-1); + while (n%d == 0) + n = n/d; + } + if (n > 1) + rez = rez / n * (n-1); + return rez; +} + +int invmod(int a, int n) +{ + + int e = phi(n)-1; + mare b = a, rez = 1; + while (e > 0) + { + if (e&1) + rez = rez * b % n; + b = b*b % n; + e >>= 1; + } + return rez; +} + +int main() +{ + int a, n; + fin >> a >> n; + fout << invmod(a, n); + return 0; +} diff --git a/invmod/euler-toutient/99ce57e1-7415-46e0-8513-d4abdd369a92.cpp b/invmod/euler-toutient/99ce57e1-7415-46e0-8513-d4abdd369a92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f042c621ed356240df2f215526dfdead8d7ffa0f --- /dev/null +++ b/invmod/euler-toutient/99ce57e1-7415-46e0-8513-d4abdd369a92.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int phi(int n) + +{ + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a, n; + + f >> a >> n; + + g << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/9b275d22-bf2c-43e9-8df5-0c4054927851.cpp b/invmod/euler-toutient/9b275d22-bf2c-43e9-8df5-0c4054927851.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0f5cd976d89e8a9158d7896c0968f908e05afc8 --- /dev/null +++ b/invmod/euler-toutient/9b275d22-bf2c-43e9-8df5-0c4054927851.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , p; + + for (p = 2 ; n > 1 && p * p <= n ; p++) + + if (n % p == 0) + + { + + sol = sol / p * (p - 1); + + while (n % p == 0) + + n /= p; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lg(int a , int b , int mod) + +{ + + int p = 1; + + while (b > 0) + + { + + if (b % 2 == 1) + + p = 1LL * p * a % mod; + + b /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a , n , rez; + + fin >> a >> n; + + rez = lg(a , Phi(n) - 1 , n); + + fout << rez; + + return 0; + +} diff --git a/invmod/euler-toutient/9c0bcb9c-25a2-4aa2-84c4-b5699524cf26.cpp b/invmod/euler-toutient/9c0bcb9c-25a2-4aa2-84c4-b5699524cf26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c0266fca6fda85519bd2d446d471f31ad042ad --- /dev/null +++ b/invmod/euler-toutient/9c0bcb9c-25a2-4aa2-84c4-b5699524cf26.cpp @@ -0,0 +1,73 @@ +#include //Andrei Alexandru a.k.a Sho10 + + + +using namespace std; + +long long a,n,inv=0; + +long long pw(long long a,long long b){ + + if(b==0) return 1; + + long long x=pw(a,b/2); + + if(b%2==1){ + + return (((x*x)%n)*a)%n; + + }else return (x*x)%n; + +} + +long long phi(long long x){ + +long long ans=x; + +for(long long i=2;i*i<=n;i++) + +{ + + if(x%i==0){ + + ans=(ans/i); + + ans=ans*(i-1LL); + + while(x%i==0){ + + x=x/i; + + } + + } + +} + +if(x>1){ + + ans=ans/x; + + ans=ans*(x-1LL); + +} + +return ans; + +} + +int32_t main(){ + +ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);; + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +cin>>a>>n; + +phi(n); + +cout< +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + +long long N, M, ans = 1, Phii; + +long long Phi (long long A){ + + long long ret = A; + + for (long long i = 2; i * i <= A; i ++){ + + if (!(A % i)){ + + while (A % i == 0)A /= i; + + ret = (ret / i) * (i - 1); + + } + + } + + if (A != 1)ret = ret / A * (A - 1); + + return ret; + +} + +int main (){ + + fin >> N >> M; + + Phii = Phi (M) - 1; + + for (long long pw = 1; pw <= Phii; pw <<= 1){ + + if (pw & Phii)ans = (ans * N) % M; + + N = (N * N) % M; + + } + + fout << ans; + + return 0; + +} diff --git a/invmod/euler-toutient/9e0c1824-ac32-4645-85ea-4706ff2ac464.cpp b/invmod/euler-toutient/9e0c1824-ac32-4645-85ea-4706ff2ac464.cpp new file mode 100644 index 0000000000000000000000000000000000000000..516c26d3a9f87fb1808be9a2ece5bc9945c61aeb --- /dev/null +++ b/invmod/euler-toutient/9e0c1824-ac32-4645-85ea-4706ff2ac464.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +int v[1000005]; + +int euler(int x){ + + int nr=x; + + int d=2; + + while(d*d<=x){ + + int ex=0; + + while(x%d==0){ + + x/=d; + + ++ex; + + } + + if(ex>0) + + nr=1LL*nr*(d-1)/d; + + ++d; + + } + + if(x>1) + + nr=1LL*nr*(x-1)/x; + + return nr; + +} + +int lg_pow(long long a,long long b,int MOD){ + + long long ans=1,p=a; + + for(long long i=1;i<=b;i<<=1){ + + if(i&b) + + ans=ans*p%MOD; + + p=p*p%MOD; + + } + + return ans; + +} + +int main(){ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a,n; + + scanf("%d %d",&a,&n); + + printf("%d\n",lg_pow(a,euler(n)-1,n)); + + return 0; + +} diff --git a/invmod/euler-toutient/9e3cb477-7d05-4ec4-88d3-0f57edbf33f4.cpp b/invmod/euler-toutient/9e3cb477-7d05-4ec4-88d3-0f57edbf33f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd5ea5c53a2bbf0077cd264febc83b8734d4d21f --- /dev/null +++ b/invmod/euler-toutient/9e3cb477-7d05-4ec4-88d3-0f57edbf33f4.cpp @@ -0,0 +1,116 @@ +#include + + +std::ifstream fin ("inversmodular.in"); + +std::ofstream fout ("inversmodular.out"); + + + +int64_t Pow(int64_t a, int64_t p, int64_t m) + +{ + + int64_t res = 1; + + while (p > 0) + + { + + if (p % 2 == 1) + + { + + res = (res * a) % m; + + } + + a = (a * a) % m; + + p /= 2; + + } + + return res; + +} + + + +int64_t Phi(int64_t n) + +{ + + int64_t r = 1, d = 2, p = 0; + + while (n > 1) + + { + + p = 0; + + while (n % d == 0) + + { + + n /= d; + + ++p; + + } + + if (p != 0) + + { + + r *= (d - 1) * Pow(d, p - 1, LONG_LONG_MAX); + + } + + ++d; + + if(n > 1 && d * d > n) + + { + + d = n; + + } + + } + + return r; + +} + + + +int64_t InversModular(const int64_t & a, const int64_t & n) + +{ + + return Pow(a, Phi(n) - 1, n); + +} + + + +int main() + +{ + + int64_t a, n; + + + + fin >> a >> n; + + + + fout << InversModular(a, n); + + + + return 0; + +} diff --git a/invmod/euler-toutient/9e3dccf5-8dd6-4ef7-b356-a8fb143c6d33.cpp b/invmod/euler-toutient/9e3dccf5-8dd6-4ef7-b356-a8fb143c6d33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f876857d964912900b62ae0b182a3da3821c3ef8 --- /dev/null +++ b/invmod/euler-toutient/9e3dccf5-8dd6-4ef7-b356-a8fb143c6d33.cpp @@ -0,0 +1,88 @@ +#include +#include +using namespace std; + + + + + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} +int Phi(int n) + +{ + + int sol=n,p; + + for(p=2;p*p<=n and n>1;p++) + + { + + if(n%p==0) + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + if(n>1) + + sol=sol/n*(n-1); + + return sol; + +} + + + +int main() + +{ + + int a,phi,n; + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin>>a>>n; + + phi=Phi(n); + + fout< + +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + + +long long Phi(long long n) { + long long sol = n; + + for (long long i = 2; i * i <= n; i++) + if (n % i == 0) { + while (n % i == 0) + n /= i; + + sol = (sol / i) * (i - 1); + } + + if (n != 1) + sol = (sol / n) * (n - 1); + + return sol; +} + +long long lgput(int a, int p, int MOD) { + int r = 1; + while(p) { + if (p % 2) + r = 1LL * a * r % MOD; + + p /= 2; + + a = 1LL * a * a % MOD; + } + + return r; +} + +int main() { + long long a, n; + fin >> a >> n; + + long long p = Phi(n) - 1; + + fout << lgput(a, p, n) << '\n';; + return 0; +} diff --git a/invmod/euler-toutient/9f481790-c36e-4e9b-8ac3-78f5888d9855.cpp b/invmod/euler-toutient/9f481790-c36e-4e9b-8ac3-78f5888d9855.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b36b4194523594bbc26d91498955de116f3cad00 --- /dev/null +++ b/invmod/euler-toutient/9f481790-c36e-4e9b-8ac3-78f5888d9855.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + +int n,a,phi; + + + +int phi_euler(int nr) + +{ + + int i,phii=nr; + + for(i=2; i*i<=nr; i++) + + { + + if(nr%i==0) + + { + + phii/=i; + + phii*=(i-1); + + while(nr%i==0) + + nr=nr/i; + + } + + } + + if(nr!=1) + + { + + phii/=nr; + + phii*=(nr-1); + + } + + return phii; + +} + + + +int lgput(int base,int exp) + +{ + + long long i; + + int aux=base,ans=1; + + for(i=1; i<=exp; i*=2) + + { + + if(i&exp) + + ans=(1LL*ans*aux)%n; + + aux=(1LL*aux*aux)%n; + + } + + return ans; + +} + + + +int main() + +{ + + fin>>a>>n; + + phi=phi_euler(n); + + fout< +using namespace std; + +unsigned long long a, MOD, x, d, fi, fm, i; + +unsigned long long rez; + +unsigned long long paw(int a, int b) + +{ + + unsigned long long r = 1, A = a; + + if(b == 0)return 1; + + while(b > 1) + + { + + if(b % 2 == 1)r = (r * A) % MOD; + + A = (A * A) % MOD; + + b = b / 2; + + } + + return (r * A) % MOD; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> MOD; + + + + x = MOD; + + d = 2; + + fi = 1; + + do + + { + + fm = 0; + + while(x % d == 0) + + { + + x = x / d; + + fm ++; + + } + + if(fm > 0)fi = (fi * paw(d, fm - 1) * (d - 1)) % MOD; + + d ++; + + if(d * d > x && x != 1)fi = (fi * (x - 1)) % MOD, x = 1; + + }while(x != 1); + + g << paw(a, fi - 1) % MOD << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/a2ad51aa-c93f-4b54-b1c1-9d4060faf3c7.cpp b/invmod/euler-toutient/a2ad51aa-c93f-4b54-b1c1-9d4060faf3c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87e51e73ee7254a5922f95611c3bb3387c75babf --- /dev/null +++ b/invmod/euler-toutient/a2ad51aa-c93f-4b54-b1c1-9d4060faf3c7.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long a, n; + + + +inline long long lgput(long long x, long long y) + +{ + + long long sol = 1; + + while (y) + + { + + if (y & 1) + + sol = (sol * x) % n; + + x = ((x % n) * (x % n)) % n; + + y >>= 1; + + } + + return sol; + +} + + + +inline long long phi(int n) + +{ + + long long rez = n; + + if (n % 2 == 0) + + { + + rez >>= 1; + + while (n % 2 == 0) + + n >>= 1; + + } + + for (long long i = 3; i * i <= n; i += 2) + + { + + if (n % i == 0) + + { + + rez = rez / i * (i - 1); + + while (n % i == 0) + + n /= i; + + } + + } + + if (n != 1) + + rez = rez / n * (n - 1); + + return rez; + +} + + + +int main() + +{ + + in >> a >> n; + + return out << lgput(a, phi(n) - 1), 0; + +} diff --git a/invmod/euler-toutient/a2e95cff-7425-4351-aa0e-173643207a6b.cpp b/invmod/euler-toutient/a2e95cff-7425-4351-aa0e-173643207a6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc985e38140cb0a4309a95781a079cc78a64c0e7 --- /dev/null +++ b/invmod/euler-toutient/a2e95cff-7425-4351-aa0e-173643207a6b.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n,a,x; + +int putere(int b,int e) + +{ + + if(e==0)return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1)r=1LL*r*b%n; + + return r; + +} + +int euler(int m) + +{ + + int phi=1; + + if(m%2==0){m/=2;while(m%2==0){m/=2;phi*=2;}} + + for(int d=3;d*d<=m;d+=2) + + if(m%d==0){m/=d;phi*=d-1;while(m%d==0){m/=d;phi*=d;}} + + if(m>1)phi*=m-1; + + return phi; + +} + +int main() + +{ + + f>>a>>n; + + g< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol,p; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int PLog(int a,int n,int MOD) + +{ + + int sol=1; + + while(n>0) + + { + + if(n%2==1) + + sol=1LL*sol*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return sol; + +} + + + +int main() + +{ + + int b,n,a; + + fin>>a>>n; + + b=PLog(a,Phi(n)-1,n); + + fout< +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/a6a5c144-5d64-474c-b8f5-9704e9d4109f.cpp b/invmod/euler-toutient/a6a5c144-5d64-474c-b8f5-9704e9d4109f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a6600eaea04677999e3ab60f6994fc0ef352445 --- /dev/null +++ b/invmod/euler-toutient/a6a5c144-5d64-474c-b8f5-9704e9d4109f.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long A,N; + +int fi(int n) + +{ + + int rez=n,d=2; + + while(d*d<=n) + + { + + if(n%d==0) + + { + + while(n%d==0) + + n/=d; + + rez=rez/d*(d-1); + + } + + d++; + + } + + if(n!=1) + + rez=rez/n*(n-1); + + return rez; + +} + +int lgput(int a,int b) + +{ + + long long rez=1; + + while(b) + + { + + if(b%2) + + rez=(1LL*rez*a)%N; + + a=(1LL*a*a)%N; + + b/=2; + + } + + return rez; + +} + +int main() + +{ + + in>>A>>N; + + out< + + +using namespace std; + + + + + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol,p; + + sol = n; + + p = 2; + + for( p = 2; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p *(p-1); + + while(n%p == 0) + + n/=p; + + } + + } + + if(n > 1) + + sol = sol / n *(n-1); + + return sol; + +} + + + +int exp(int a, int n,int mod) + +{ + + int sol=1; + + while(n>0) + + { + + if(n % 2 == 1) + + sol = 1LL*sol*a%mod; + + n/=2; + + a=1LL*a*a%mod; + + + + } + + return sol; + + + +} + + + +int main() + +{ + + int a , n; + + fin >> a >> n; + + int b; + + b=exp(a,Phi(n)-1,n); + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/a84a431a-de6c-4b46-9a90-784a23fee721.cpp b/invmod/euler-toutient/a84a431a-de6c-4b46-9a90-784a23fee721.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2aee425c760ee4ce795eb0f497072228922b256 --- /dev/null +++ b/invmod/euler-toutient/a84a431a-de6c-4b46-9a90-784a23fee721.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + +typedef long long ll; + +ll fast_exp(ll a, ll b, ll MOD){ + + if(b==0) return 1; + + if(b==1) return a% MOD; + + if(b%2==1) return a*fast_exp(a, b-1, MOD) % MOD; + + if(b%2==0) return fast_exp(a*a% MOD, b/2, MOD)% MOD; + +} + +ll phi(ll a){ + + ll cur = a; + + for(ll i = 2; i*i<=a; i++){ + + if(a%i==0){ + + while(a%i==0) a/=i; + + cur = (cur/i)*(i-1); + + } + + } + + if(a>1) cur = cur / a * (a-1); + + return cur; + +} + +int main(){ + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + ll n, m; + + scanf("%lld %lld", &n, &m); + + ll x = phi(m)-1, nr = n, ct = 1; + + ct = fast_exp(n, x, m); + + printf("%lld", ct); + +} diff --git a/invmod/euler-toutient/a8bbacfa-aa2e-4c0a-a82a-8c472804456d.cpp b/invmod/euler-toutient/a8bbacfa-aa2e-4c0a-a82a-8c472804456d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2517648b95973534456b9c3c4c62559dd579ff5 --- /dev/null +++ b/invmod/euler-toutient/a8bbacfa-aa2e-4c0a-a82a-8c472804456d.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long phi,n; + + + +void descomp(long long n) + +{ + + for(long long i=2;i*i<=n;i++) + + if(n%i==0) + + { + + while(n%i==0) + + n/=i; + + phi/=i; + + phi*=(i-1); + + } + + if(n!=1) + + phi/=n,phi*=(n-1); + +} + + + + + +long long ridica (long long baza,long long exp) + +{ + + long long sol=1; + + while (exp!=0) + + { + + if (exp%2==1) + + { + + sol=sol*baza; + + sol%=n; + + } + + baza=baza*baza; + + baza%=n; + + exp/=2; + + } + + return sol; + +} + + + +int main() + +{ + + long long a; + + fin>>a>>n; + + phi=n; + + descomp (n); + + fout< +using namespace std; + +typedef long long ll; + +ll fast_exp(ll a, ll b, ll MOD){ + + if(b==0) return 1; + + if(b==1) return a% MOD; + + if(b%2==1) return a*fast_exp(a, b-1, MOD) % MOD; + + if(b%2==0) return fast_exp(a*a% MOD, b/2, MOD)% MOD; + +} + +ll phi(ll a){ + + ll cur = a; + + for(ll i = 2; i*i<=a; i++){ + + if(a%i==0){ + + while(a%i==0) a/=i; + + cur = (cur/i)*(i-1); + + } + + } + + if(a>1) cur = cur / a * (a-1); + + return cur; + +} + +int main(){ + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + ll n, m; + + scanf("%lld %lld", &n, &m); + + ll x = phi(m)-1, nr = n, ct = 1; + + ct = fast_exp(n, x, m); + + printf("%lld", ct); + +} diff --git a/invmod/euler-toutient/a969257a-b216-4b7c-a88b-8a8ea7649c01.cpp b/invmod/euler-toutient/a969257a-b216-4b7c-a88b-8a8ea7649c01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f042c621ed356240df2f215526dfdead8d7ffa0f --- /dev/null +++ b/invmod/euler-toutient/a969257a-b216-4b7c-a88b-8a8ea7649c01.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int phi(int n) + +{ + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a, n; + + f >> a >> n; + + g << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/ab24831f-bde5-4789-962e-4e7e53ffda53.cpp b/invmod/euler-toutient/ab24831f-bde5-4789-962e-4e7e53ffda53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f09acdefd363d5e2c59c9b9f328e9fbaafaa1567 --- /dev/null +++ b/invmod/euler-toutient/ab24831f-bde5-4789-962e-4e7e53ffda53.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long A,N,EulerTotient; + +long long Phi(long long L) + +{ + + long long Prod=1; + + for(int i=2; i*i<=L; i++) + + if(L%i==0) + + { + + Prod=Prod*(i-1); + + L/=i; + + while(L%i==0) + + { + + Prod*=i; + + L/=i; + + } + + } + + + + if(L!=1) + + Prod*=(L-1); + + + + return Prod; + +} + +long long logPow() + +{ + + long long rez=1; + + while(EulerTotient) + + { + + if(EulerTotient%2) + + rez=(rez*A)%N; + + A=(A*A)%N; + + EulerTotient/=2; + + } + + return rez%N; + +} + +int main() + +{ + + f>>A>>N; + + EulerTotient=Phi(N)-1; + + g< + + +long long phi(long long n){ + + long long d=2,r=n; + + + + while(d*d<=n){ + + if(n%d==0){ + + while(n%d==0) + + n/=d; + + r=r/d*(d-1); + + } + + d++; + + } + + if(n>1) + + r=r/n*(n-1); + + return r; + +} + + + +long long exp(long long x, long long y, long long MOD){ + + long long r=1; + + + + while(y){ + + if(y%2) + + r=(r*x)%MOD; + + x=(x*x)%MOD; + + y/=2; + + } + + return r; + +} + + + +int main(){ + + FILE *fin=fopen("inversmodular.in","r"); + + FILE *fout=fopen("inversmodular.out","w"); + + long long a,n; + + + + fscanf(fin,"%lld%lld",&a,&n); + + fprintf(fout,"%lld\n",exp(a,phi(n)-1,n)); + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invmod/euler-toutient/ac0bc4ba-78ab-41c8-854f-1975eb74a725.cpp b/invmod/euler-toutient/ac0bc4ba-78ab-41c8-854f-1975eb74a725.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9003a256d94a0a0f9b8954fbbef841a45492791b --- /dev/null +++ b/invmod/euler-toutient/ac0bc4ba-78ab-41c8-854f-1975eb74a725.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +struct A + +{ + + long long nr_prim, exponent; + +}; + + + +A v[15]; + +long long MOD; + + + +long long n_la_puterea_k ( long long n, long long k ); + +long long indicator_Euler ( long long x ); + + + +int main() + +{ + + long long a; + + + + fin >> a >> MOD; + + fout << ( n_la_puterea_k ( a, indicator_Euler ( MOD ) - 1 ) ) % MOD; + + + + return 0; + +} + + + +long long n_la_puterea_k ( long long n, long long k ) + +{ + + if ( k == 0 ) return 1; + + else if ( k == 1 ) return n % MOD; + + else if ( k % 2 == 0 ) return ( n_la_puterea_k ( ( n * n ) % MOD, k / 2 ) ) % MOD; + + else return ( n * n_la_puterea_k ( ( n * n ) % MOD, k / 2 ) ) % MOD; + +} + + + +long long indicator_Euler ( long long x ) + +{ + + long long i, rez = 1; + + + + if ( x % 2 == 0 ) + + { + + v[++v[0].exponent].nr_prim = 2; + + while ( x % 2 == 0 ) + + { + + x /= 2; + + v[v[0].exponent].exponent++; + + } + + } + + + + for ( i = 3 ; i * i <= x ; i += 2 ) + + if ( x % i == 0 ) + + { + + v[++v[0].exponent].nr_prim = i; + + while ( x % i == 0 ) + + { + + x /= i; + + v[v[0].exponent].exponent++; + + } + + } + + + + if ( x != 1 ) v[++v[0].exponent].nr_prim = x, v[v[0].exponent].exponent = 1; + + + + for ( i = 1 ; i <= v[0].exponent ; i++ ) rez *= ( v[i].nr_prim - 1 ) * n_la_puterea_k ( v[i].nr_prim, v[i].exponent - 1 ); + + + + return rez; + +} diff --git a/invmod/euler-toutient/acb8f61f-a4c5-4b2d-9867-96d3419cc7dc.cpp b/invmod/euler-toutient/acb8f61f-a4c5-4b2d-9867-96d3419cc7dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..957073104d967eeb2714976a6714058ef04610aa --- /dev/null +++ b/invmod/euler-toutient/acb8f61f-a4c5-4b2d-9867-96d3419cc7dc.cpp @@ -0,0 +1,149 @@ +#include +#include + + +using namespace std; + + + +const int N = 50005; + + + +int a, n; + + + +bool IsPrime (int x) + +{ + + if (x <= 1) return false; + + if (!(x & 1) && x != 2) return false; + + for (int i = 3; i * i <= x; i += 2) + + if (!(x % i)) return false; + + return true; + +} + + + +int Euler_Totient (int x) + +{ + + if (IsPrime(x)) return x - 1; + + int f, phi; + + phi = x; + + f = 2; + + while (f * f <= x && x > 1) + + { + + if (x % f == 0) + + { + + while (x % f == 0) + + x /= f; + + phi /= f; + + phi *= (f - 1); + + } + + f++; + + } + + if (x > 1) + + { + + phi /= x; + + phi *= (x - 1); + + } + + return phi; + +} + + + +int Power (int a, int b, int mod) + +{ + + int p = 1; + + while (b) + + { + + if (b & 1) + + p = 1LL * p * a % mod; + + b >>= 1; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +void Read () + +{ + + ifstream fin ("inversmodular.in"); + + fin >> a >> n; + + fin.close(); + +} + + + +void Solve () + +{ + + ofstream fout ("inversmodular.out"); + + fout << Power(a, Euler_Totient(n) - 1, n) << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + return 0; + +} diff --git a/invmod/euler-toutient/accf398e-0348-4e9a-991b-99ac05c00c3f.cpp b/invmod/euler-toutient/accf398e-0348-4e9a-991b-99ac05c00c3f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..739a35ea1c45de001db90cd2e884d7bd085a0563 --- /dev/null +++ b/invmod/euler-toutient/accf398e-0348-4e9a-991b-99ac05c00c3f.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +long long int number, modulo, power = 1, inversModular, mask; + + + +bool prime(long long int checkNumber){ + + if ( checkNumber == 0 or checkNumber == 1 ) + + return false; + + if ( checkNumber == 2 ) + + return true; + + for ( int index = 2; index*index <= checkNumber; index++ ) + + if ( checkNumber%index == 0 ) + + return false; + + return true; + +} + + + +long long int risingPower(long long int base, long long int exponent){ + + long long int product = 1; + + for ( ; exponent; exponent >>= 1 ){ + + if ( exponent % 2 ) + + product = (product * base) % mask; + + base = (base*base) % mask; + + } + + return product; + +} + + + +int main(){ + + fin >> number >> modulo; + + mask = modulo; + + if ( prime(modulo) == 1 ) + + inversModular = risingPower(number, modulo-2); + + else{ + + for ( int index = 2; index*index <= modulo; index++ ){ + + if ( modulo % index == 0 ){ + + power *= (index-1); + + while ( modulo % index == 0 ){ + + power *= index; + + modulo /= index; + + } + + power /= index; + + } + + } + + if ( modulo > 0 and modulo != 1 ) + + power *= (modulo-1); + + inversModular = risingPower(number, power-1); + + } + + fout << inversModular; + +} diff --git a/invmod/euler-toutient/ae1ec03f-6e91-4454-80e3-2b7834d07dc5.cpp b/invmod/euler-toutient/ae1ec03f-6e91-4454-80e3-2b7834d07dc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd316ca16e85d3355156528a430dbac7d98b31b --- /dev/null +++ b/invmod/euler-toutient/ae1ec03f-6e91-4454-80e3-2b7834d07dc5.cpp @@ -0,0 +1,66 @@ +#include +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/ae31a8c7-9ef8-4410-9b1b-058d84733e40.cpp b/invmod/euler-toutient/ae31a8c7-9ef8-4410-9b1b-058d84733e40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50466e8458451209c58838014ec16852bf0b6790 --- /dev/null +++ b/invmod/euler-toutient/ae31a8c7-9ef8-4410-9b1b-058d84733e40.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a,n,MOD; + +long long lgnp(long long n, long long p) + +{ + + n=n%MOD; + + long long r=1; + + while(p) + + { + + if(p%2==1) r=(1LL*r*n)%MOD; + + n=(n*n*1LL)%MOD; + + p/=2; + + } + + return r%MOD; + +} + +long long phi(long long n) + +{ + + int d=2,fm; + + long long nr=1; + + do + + { + + fm=0; + + while(n%d==0) + + { + + n/=d; + + fm++; + + } + + if(fm>0) nr*=(d-1)*lgnp(d,fm-1); + + d++; + + if(d*d>n&&n>1) + + { + + nr=nr*(n-1); + + n=1; + + } + + } + + while(n>1); + + return nr; + +} + +int main() + +{ + + fin>>a>>n; + + MOD=n; + + fout< + +using namespace std; + +int lgput(int a, int b, int mod) { + int ret = 1; + while(b) { + if(b&1) ret = 1ll*ret*a%mod; + a = 1ll*a*a%mod; + b>>=1; + } + + return ret; +} +int main() { + + + + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + + ios::sync_with_stdio(false); + cin.tie(0); + cout.tie(0); + + int a, n; + cin >> a >> n; + + int phi = n; + int cop = n; + if(cop % 2 == 0){ + phi /= 2; + while(cop % 2 == 0) cop >>= 1; + } + + int d = 3; + while(d*d <= cop) { + if(cop % d == 0) { + phi /= d; + phi *= d-1; + } + while(cop % d == 0) { + cop /= d; + } + d += 2; + } + + if(cop != 1) { + phi /= cop; + phi *= cop-1; + } + + cout << lgput(a, phi - 1, n) << '\n'; + return 0; +} diff --git a/invmod/euler-toutient/aeca329b-d4e9-4dd7-bfff-5898f2435961.cpp b/invmod/euler-toutient/aeca329b-d4e9-4dd7-bfff-5898f2435961.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c349c7339a69208cfa7936e0df3132d4b5e385 --- /dev/null +++ b/invmod/euler-toutient/aeca329b-d4e9-4dd7-bfff-5898f2435961.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +int n; +long long indicator, invers, a; +void desc(int nr) +{ + indicator = nr; + for (int i=2;i*i<=nr;i++){ + if (nr%i==0){ + while(nr%i==0){ + nr/=i; + } + indicator /= i; + indicator *= i-1; + } + } + if (nr>1){ + indicator /= nr; + indicator *= nr-1; + } +} +long long euler() +{ + long long inv = 1; + indicator--; + while(indicator > 0){ + if (indicator%2==1){ + inv = (inv * a)%n; + } + a = (a * a)%n; + indicator /=2; + } + return inv; +} +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%lld%d", &a, &n); + desc(n); + invers = euler(); + printf("%lld", invers%n); + return 0; +} diff --git a/invmod/euler-toutient/b136fcc6-6c0d-4d11-b7cb-5144d6b42fe4.cpp b/invmod/euler-toutient/b136fcc6-6c0d-4d11-b7cb-5144d6b42fe4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0879aab66b2b5a351f6c5bf140ce969b621b1d8c --- /dev/null +++ b/invmod/euler-toutient/b136fcc6-6c0d-4d11-b7cb-5144d6b42fe4.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol, p; + + sol = n; + + for(p = 2;n > 1 && p * p <= n;p++) + + { + + if(n % p == 0) + + { + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + } + + if(n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + +int invmod(int a, int n, int MOD) + +{ + + int sol = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + sol = 1LL * sol * a % MOD; + + n /= 2; + + a = 1LL * a * a % MOD; + + } + + return sol; + +} + +int a, n; + + + +int main() + +{ + + fin >> a >> n; + + fout << invmod(a, Phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/b15c8acd-db6d-4116-a2d1-7fed4cd16834.cpp b/invmod/euler-toutient/b15c8acd-db6d-4116-a2d1-7fed4cd16834.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e135c8e8be1adc6e143fa8c8c13c3eb24d6715 --- /dev/null +++ b/invmod/euler-toutient/b15c8acd-db6d-4116-a2d1-7fed4cd16834.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int euler(int n){ + + long long d = 2, p, prod = n; + + while ( d * d <= n ) { + + p = 0; + + while ( n % d == 0 ) { + + n /= d; + + p ++; + + } + + if ( p ) + + prod = prod / d * ( d - 1 ); + + d ++; + + } + + if ( n > 1 ) + + prod = prod / n * ( n - 1 ); + + return prod; + +} + +int putere(int a,int n,int m){ + + int prod = 1; + + do{ + + if(n%2 != 0){ + + prod = (long long)prod * a % m; + + } + + a = 1LL * a * a % m; + + n /= 2; + + }while(n != 0); + + return prod; + +} + +int main() + +{ + + int a,n,x; + + cin >> a >> n; + + x = putere(a,euler(n) - 1,n); + + cout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/b21a60a3-f22f-49a7-ac60-e90c2e6aabf0.cpp b/invmod/euler-toutient/b21a60a3-f22f-49a7-ac60-e90c2e6aabf0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d968fc11f386ce697d0de975b9f26371265823ed --- /dev/null +++ b/invmod/euler-toutient/b21a60a3-f22f-49a7-ac60-e90c2e6aabf0.cpp @@ -0,0 +1,55 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,m; + +long long phi(long long nr){ + + long long rez=nr; + + for(int i=2;i*i<=nr;i++){ + + if(nr%i==0){ + + while(nr%i==0) nr/=i; + + rez=(rez/i)*(i-1); + + } + + } + + if(nr!=1) rez=rez/nr*(nr-1); + + return rez; + +} + +int main(){ + + fin>>n>>m; + + long long nr=n,crt=1,put=phi(m)-1; + + for(long long p=1;p<=put;p<<=1){ + + if(p&put) crt=(crt*nr)%m; + + nr=(nr*nr)%m; + + } + + fout< +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int n,pr; + +int MOD,invers,mod; + + + +void exp(long long int nr,long long int ad,long long int x) + + { + + if(nr!=pr) + + { + + if(nr+ad<=pr) { + + invers=(invers*x)%MOD; + + x=(x*x)%MOD; + + nr+=ad; + + ad*=2; + + exp(nr,ad,x); + + } + + else exp(nr,1,n); + + } + + } + +int main() + +{ + +f>>n>>mod; + +MOD=mod; + +pr=mod; + +int d=3; + +if(mod%2==0) + + { + + pr=pr/2; + + while(mod%2==0) + + mod/=2; + + } + +while(d*d<=mod&&mod!=1) + + { + + if(mod%d!=0) d+=2; + + else + + { + + pr=pr/d*(d-1); + + while(mod%d==0) + + mod/=d; + + } + + } + +if(mod!=1) pr=pr/mod*(mod-1); + +pr--; + +invers=n; + +if(pr==0) g<<"1"; + +else + +{ + +exp(1,1,n); + +g< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +long long n; + + + +long long ridic(long long x, long long p) + +{ + + if (p == 1) + + return (x %n); + + long long aux = ridic(x, p / 2); + + + + if (p % 2 == 0) + + return ((aux*aux) % n); + + else + + return ((((aux * aux) % n) *x) % n); + +} + + + +long long phi(long long n) + +{ + + long long rez = n; + + + + for (long long i = 2; i*i <= n; ++i) + + { + + if (n %i == 0) + + { + + while (n %i == 0) + + n /= i; + + rez -= (rez / i); + + } + + } + + + + if (n > 1) + + rez -= (rez / n); + + + + return rez; + +} + + + + + +int main() + +{ + + long long a; + + f >> a >> n; + + + + long long ph = phi(n); + + + + long long inv_m = ridic(a, ph - 1); + + + + g << inv_m << "\n"; + + + + return 0; + +} diff --git a/invmod/euler-toutient/b4144db9-6f73-4b22-bc10-a606253ea0c4.cpp b/invmod/euler-toutient/b4144db9-6f73-4b22-bc10-a606253ea0c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3e0d18c4116c98a34e84073679f855c504413c0 --- /dev/null +++ b/invmod/euler-toutient/b4144db9-6f73-4b22-bc10-a606253ea0c4.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + + + +int n; + + + +int rid_put(int a,int b) + +{ + + int sol=1; + + for(long long i=1;i<=b;i<<=1) + + { + + if(b&i) sol=(1LL*sol*a)%n; + + a=(1LL*a*a)%n; + + } + + return sol; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a; + + scanf("%d%d",&a,&n); + + int c=n,fi=n; + + for(int i=2;i*i<=c;i++) + + if(c%i==0) + + { + + while(c%i==0) c/=i; + + fi=fi/i*(i-1); + + } + + if(c>1) fi=fi/c*(c-1); + + printf("%d",rid_put(a,fi-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/b4561da2-2736-4e0d-8e30-0a1abdeae805.cpp b/invmod/euler-toutient/b4561da2-2736-4e0d-8e30-0a1abdeae805.cpp new file mode 100644 index 0000000000000000000000000000000000000000..307224a1219082857cf6539707ae348562bdd1ef --- /dev/null +++ b/invmod/euler-toutient/b4561da2-2736-4e0d-8e30-0a1abdeae805.cpp @@ -0,0 +1,75 @@ +#include +typedef unsigned long long ul; +typedef long long ll; +using namespace std; + + +ll a, n, x; + + + +ll phiCalc(ll n) + +{ + + ll phi = n; + + for (ll p = 2; p * p <= n; p++) + + { + + if (n % p == 0) + + { + + while (n % p == 0) n /= p; + + phi -= phi / p; + + + + } + + } + + if (n > 1) phi -= phi / n; + + return phi; + +} + + + +ll expon(ll b, ll p) + +{ + + if (p == 0) return 1; + + else if (p == 1) return b % n; + + else if (p % 2 == 0) return expon((b * b) % n, p / 2) % n; + + else if (p % 2 == 1) return (b % n) * (expon((b * b) % n, (p - 1) / 2) % n); + +} + + +int main(){ +ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); +ifstream cin("inversmodular.in"); +ofstream cout("inversmodular.out"); + + +cin >> a >> n; + + + + +cout << expon(a, phiCalc(n) - 1) % n << "\n"; + + + + +return 0; +} diff --git a/invmod/euler-toutient/b4e5a7f6-d7f6-460d-8f9f-62f4f299af0d.cpp b/invmod/euler-toutient/b4e5a7f6-d7f6-460d-8f9f-62f4f299af0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c55382196e4b1b45e93d5462a8e04285b5e9eb50 --- /dev/null +++ b/invmod/euler-toutient/b4e5a7f6-d7f6-460d-8f9f-62f4f299af0d.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long Put(int a, int n, int mod) + +{ + + long long p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + +int Phi(int n) + +{ + + int sol = n, i; + + for(i = 2;i * i <= n && n > 1;i++) + + { + + if(n % i == 0) + + { + + sol = sol / i * (i - 1); + + while(n % i == 0) + + n /= i; + + } + + } + + if(n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + +int main() + +{ + + int a, n; + + fin >> a >> n; + + fout << Put(a, Phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/b548fa6b-7dfb-43cc-bb37-fd3af5edfbd3.cpp b/invmod/euler-toutient/b548fa6b-7dfb-43cc-bb37-fd3af5edfbd3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e88a0c2d51177af5ac6514abe881ddac8bf0499f --- /dev/null +++ b/invmod/euler-toutient/b548fa6b-7dfb-43cc-bb37-fd3af5edfbd3.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n, mod, phi=1, ex(int, int); + +void desc(int); + +int main() + +{ + + f >> n >> mod; + + desc(mod); + + g << ex(n, phi - 1); + + return 0; + +} + +void desc(int N) + +{ + + int d = 3; + + if(N % 2 == 0) + + { + + N /= 2; + + while(N % 2 == 0) + + N /= 2, phi *= 2; + + } + + while(d * d <= N) + + { + + if(N % d == 0) + + { + + N /= d; + + phi *= (d - 1); + + while(N % d == 0) + + N /=d, phi *= d; + + } + + d += 2; + + } + + if(N > 1) + + phi *= N - 1; + +} + +int ex(int b, int exponent) + +{ + + if(!exponent) + + return 1; + + int r = ex(b, exponent / 2); + + r = 1LL * r * r % mod; + + if(exponent % 2) + + r = 1LL * r * b % mod; + + return r; + +} diff --git a/invmod/euler-toutient/b5604d98-606f-4064-8bbf-a178256f0795.cpp b/invmod/euler-toutient/b5604d98-606f-4064-8bbf-a178256f0795.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce059bdd1437ea1181b44ee8b19a1db36d4ae4e8 --- /dev/null +++ b/invmod/euler-toutient/b5604d98-606f-4064-8bbf-a178256f0795.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +typedef long long ll; + + + +ll a, b; + + + +ll phi(ll n) { + + ll ans, d; + + ans = n; + + d = 2; + + while (d * d <= n) { + + if (n % d == 0) { + + while (n % d == 0) { + + n /= d; + + } + + ans = ans / d * (d - 1); + + } + + d += 1 + (d % 2); + + } + + if (n != 1) { + + ans = ans / n * (n - 1); + + } + + return ans; + +} + + + +ll lgpow(ll base, ll exp) { + + ll ans; + + ans = 1; + + while (exp > 0) { + + if (exp % 2 > 0) { + + ans = (1LL * ans * base) % b; + + } + + base = (1LL * base * base) % b; + + exp >>= 1; + + } + + return ans; + +} + + + +int main() { + + fin >> a >> b; + + fout << lgpow(a, phi(b) - 1); + + return 0; + +} diff --git a/invmod/euler-toutient/b5b98ff7-60e9-489d-978a-dd8b2b2ee476.cpp b/invmod/euler-toutient/b5b98ff7-60e9-489d-978a-dd8b2b2ee476.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41f1da147b9f0e39f8bfa6feafbdedddb7e7b23c --- /dev/null +++ b/invmod/euler-toutient/b5b98ff7-60e9-489d-978a-dd8b2b2ee476.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +struct A + +{ + + int nr, val; + +}; + + + +vector < A > v; + +int MOD; + + + +long long n_la_puterea_k ( long long n, long long k ); + +long long indicator_Euler ( long long x ); + + + +int main() + +{ + + long long x; + + + + fin >> x >> MOD; + + fout << n_la_puterea_k ( x, indicator_Euler ( MOD ) - 1 ); + + + + return 0; + +} + + + +long long n_la_puterea_k ( long long n, long long k ) + +{ + + if ( k == 0 ) return 1; + + else if ( k == 1 ) return n % MOD; + + else if ( k % 2 == 0 ) return n_la_puterea_k ( ( n * n ) % MOD, k / 2 ); + + else return ( ( n % MOD ) * n_la_puterea_k ( ( n * n ) % MOD, k / 2 ) ) % MOD; + +} + + + +long long indicator_Euler ( long long x ) + +{ + + long long i, nr, val, r = 1; + + + + if ( x % 2 == 0 ) + + { + + nr = 2, val = 0; + + while ( x % 2 == 0 ) val++, x /= 2; + + v.push_back ( { nr, val } ); + + } + + + + for ( i = 3 ; i * i <= x ; i += 2 ) + + { + + if ( x % i == 0 ) + + { + + nr = i, val = 0; + + while ( x % i == 0 ) val++, x /= i; + + v.push_back ( { nr, val } ); + + } + + } + + + + if ( x != 1 ) v.push_back ( { x, 1 } ); + + for ( i = 0 ; i < v.size() ; i++ ) r *= ( v[i].nr - 1 ) * n_la_puterea_k ( v[i].nr, v[i].val - 1 ); + + + + return r; + +} diff --git a/invmod/euler-toutient/b7101aae-62e9-4c2d-8770-1177ca61493d.cpp b/invmod/euler-toutient/b7101aae-62e9-4c2d-8770-1177ca61493d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1615fd263f7a664db96b670616136a33d4888ee8 --- /dev/null +++ b/invmod/euler-toutient/b7101aae-62e9-4c2d-8770-1177ca61493d.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +ifstream cin ("inversmodular.in"); + +ofstream cout ("inversmodular.out"); + + + +long long mod; + +long long ridput(long long b, long long e) + +{ + + long long r = 1; + + while (e) + + { + + if (e % 2) + + r = (r * b) % mod; + + b = (b * b) % mod; + + e /= 2; + + } + + return r; + +} + +long long phi(long long n) + +{ + + long long r = n, f = 2; + + while (f * f <= n && n != 1) + + { + + if (!(n % f)) + + { + + r = r * (f - 1) / f; + + while (!(n % f)) + + n /= f; + + } + + f++; + + } + + if (n != 1) + + r = r * (n - 1) / n; + + return r; + +} + +int main() + +{ + + long long a, b; + + cin >> a >> b; + + mod = b; + + cout << ridput(a, phi(b) - 1); + + return 0; + +} diff --git a/invmod/euler-toutient/b7c6a2f8-4df5-4bda-b25f-0f6b21d4b2f2.cpp b/invmod/euler-toutient/b7c6a2f8-4df5-4bda-b25f-0f6b21d4b2f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc985e38140cb0a4309a95781a079cc78a64c0e7 --- /dev/null +++ b/invmod/euler-toutient/b7c6a2f8-4df5-4bda-b25f-0f6b21d4b2f2.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n,a,x; + +int putere(int b,int e) + +{ + + if(e==0)return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1)r=1LL*r*b%n; + + return r; + +} + +int euler(int m) + +{ + + int phi=1; + + if(m%2==0){m/=2;while(m%2==0){m/=2;phi*=2;}} + + for(int d=3;d*d<=m;d+=2) + + if(m%d==0){m/=d;phi*=d-1;while(m%d==0){m/=d;phi*=d;}} + + if(m>1)phi*=m-1; + + return phi; + +} + +int main() + +{ + + f>>a>>n; + + g< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +int Phi(int n) + +{ + + int p,sol; + + sol=n; + + for(p=2;n>1 && p*p<=n;p++) + + { + + if(n%p==0) + + { + + sol=sol/p*(p-1); + + while(n%p==0) + + n/=p; + + } + + } + + if(n>1) sol=sol/n*(n-1); + + return sol; + +} + + + +int LogP(int a,int n,int MOD) + +{ + + int p=1; + + while(n>0) + + { + + if(n%2==1) p=1LL*p*a%MOD; + + n/=2; + + a=1LL*a*a%MOD; + + } + + return p; + +} + + + +int main() + +{ + + int a,n; + + fin>>a>>n; + + fout< +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int n,pr; + +int MOD,invers,mod; + + + +void exp(long long int nr,long long int ad,long long int x) + + { + + if(nr!=pr) + + { + + if(nr+ad<=pr) { + + invers=(invers*x)%MOD; + + x=(x*x)%MOD; + + nr+=ad; + + ad*=2; + + exp(nr,ad,x); + + } + + else exp(nr,1,n); + + } + + } + +int main() + +{ + +f>>n>>mod; + +MOD=mod; + +pr=mod; + +int d=3; + +if(mod%2==0) + + { + + pr=pr/2; + + while(mod%2==0) + + mod/=2; + + } + +while(d*d<=mod&&mod!=1) + + { + + if(mod%d!=0) d+=2; + + else + + { + + pr=pr/d*(d-1); + + while(mod%d==0) + + mod/=d; + + } + + } + +if(mod!=1) pr=pr/mod*(mod-1); + +pr--; + +invers=n; + +if(pr==0) g<<"1"; + +else + +{ + +exp(1,1,n); + +g< +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long indicatoreuler(long long N) + +{ + + long long S = N; + + + + if(N % 2 == 0) + + { + + S /= 2; + + while(N % 2 == 0) + + N /= 2; + + } + + for(long long i = 3; i * i <= N; i++) + + { + + if(N % i == 0) + + { + + S /= i; + + S *= i - 1; + + while(N % i == 0) + + N /= i; + + } + + } + + if(N!=1) + + { + + S /= N; + + S *= N-1; + + } + + return S; + +} + + + +long long lgput(long long A, long long S, long long N) + +{ + + long long rez = 1; + + while(S > 0) + + { + + if(S & 1) + + { + + rez *= A; + + rez %= N; + + S--; + + } + + A *=A; + + A %=N; + + S>>=1; + + } + + return rez; + +} + +int main() + +{ + + long long A,N; + + in >> A >> N; + + long long S = indicatoreuler(N); + + S--; + + out << lgput(A, S, N); + + return 0; + +} diff --git a/invmod/euler-toutient/bba380fc-9a7f-4cf9-b9e9-c2883436e476.cpp b/invmod/euler-toutient/bba380fc-9a7f-4cf9-b9e9-c2883436e476.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/bba380fc-9a7f-4cf9-b9e9-c2883436e476.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/bbc6f716-b6b8-4f98-8c58-dc83551cc1fc.cpp b/invmod/euler-toutient/bbc6f716-b6b8-4f98-8c58-dc83551cc1fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24f000a9d71905e0b789652992ac48f9fc0eb8b8 --- /dev/null +++ b/invmod/euler-toutient/bbc6f716-b6b8-4f98-8c58-dc83551cc1fc.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + + + +int main() { + + ifstream cin ("inversmodular.in"); + + ofstream cout ("inversmodular.out"); + + long long a, n, i, cn, nr, put; + + cin >> a >> n; + + cn = nr = n; + + i = 2; + + while (i * i <= cn) { + + if (cn % i == 0) { + + nr = nr / i * (i - 1); + + while (cn % i == 0) + + cn /= i; + + } + + i++; + + } + + if (cn > 1) + + nr = nr / cn * (cn - 1); + + nr--; + + put = 1; + + while (nr) { + + if (nr % 2) + + put = (put * a) % n; + + nr /= 2; + + a = (a * a) % n; + + } + + cout << put; + + return 0; + +} diff --git a/invmod/euler-toutient/bddb29fb-11c0-4b10-9c8f-618da7b17500.cpp b/invmod/euler-toutient/bddb29fb-11c0-4b10-9c8f-618da7b17500.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c06826e13e0bcd09e4dbd487249b464527972542 --- /dev/null +++ b/invmod/euler-toutient/bddb29fb-11c0-4b10-9c8f-618da7b17500.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + + + +long long invMod(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + invMod(x, y, b, a%b); + + long long aux = x; + + x = y; + + y = aux - y*(a/b); + + } + +} + +long long x, y; + +int a, b; + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + cin>>a>>b; + + invMod(x,y,a,b); + + if (x < 0) x = b + x; + + cout< +#include +#include +#include +#include + + +using namespace std; + + +using namespace std; + +class InvMod { +private: + + int A, N; + long long p[25]; + + long long phi(int x) { + int c=x; + long long d=x; + for (int i = 2; i * i <= c; i++) + if (x % i == 0) { + while(c%i==0)c/=i; + d=(d/i)*(i-1); + } + if (c != 1) d = d / c * (c - 1); + return d; + } + + void lgpowx(long long px) { + long long q = log2(px) + 1; + + p[0] = A; + for (int i = 1; i <= q; i++) + p[i] = (p[i - 1] * p[i - 1]) % N; + } + + long long lgpow(int n, long long px) { + long long ans = 1; + int k = 0; + lgpowx(px); + while (px) { + if (px & 1) + ans = ans * p[k] % N; + ++k; + px = px >> 1; + } + return ans; + } + + +public: + void invMod_main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d %d", &A, &N); + + printf("%lld", lgpow(A, phi(N) - 1)); + + } + + +} invMod; + +int main() { + invMod.invMod_main(); + return 0; +} diff --git a/invmod/euler-toutient/bee5448a-81dc-4b5b-96cd-0beeb474a607.cpp b/invmod/euler-toutient/bee5448a-81dc-4b5b-96cd-0beeb474a607.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e135c8e8be1adc6e143fa8c8c13c3eb24d6715 --- /dev/null +++ b/invmod/euler-toutient/bee5448a-81dc-4b5b-96cd-0beeb474a607.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int euler(int n){ + + long long d = 2, p, prod = n; + + while ( d * d <= n ) { + + p = 0; + + while ( n % d == 0 ) { + + n /= d; + + p ++; + + } + + if ( p ) + + prod = prod / d * ( d - 1 ); + + d ++; + + } + + if ( n > 1 ) + + prod = prod / n * ( n - 1 ); + + return prod; + +} + +int putere(int a,int n,int m){ + + int prod = 1; + + do{ + + if(n%2 != 0){ + + prod = (long long)prod * a % m; + + } + + a = 1LL * a * a % m; + + n /= 2; + + }while(n != 0); + + return prod; + +} + +int main() + +{ + + int a,n,x; + + cin >> a >> n; + + x = putere(a,euler(n) - 1,n); + + cout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/bf234921-8689-4496-bd5f-8d7229e514cb.cpp b/invmod/euler-toutient/bf234921-8689-4496-bd5f-8d7229e514cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc985e38140cb0a4309a95781a079cc78a64c0e7 --- /dev/null +++ b/invmod/euler-toutient/bf234921-8689-4496-bd5f-8d7229e514cb.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n,a,x; + +int putere(int b,int e) + +{ + + if(e==0)return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1)r=1LL*r*b%n; + + return r; + +} + +int euler(int m) + +{ + + int phi=1; + + if(m%2==0){m/=2;while(m%2==0){m/=2;phi*=2;}} + + for(int d=3;d*d<=m;d+=2) + + if(m%d==0){m/=d;phi*=d-1;while(m%d==0){m/=d;phi*=d;}} + + if(m>1)phi*=m-1; + + return phi; + +} + +int main() + +{ + + f>>a>>n; + + g< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +typedef long long ll; + +ll a,n; + +ll phi(ll nr) + +{ + + ll ans=nr; + + for(int d=2;d*d<=nr;d++) + + { + + if(nr%d==0) + + { + + while(nr%d==0) + + nr/=d; + + ans-=ans/d; + + } + + } + + if(nr>1)ans-=ans/nr; + + return ans-1; + +} + +ll exp(ll a, ll p) + +{ + + ll rez=1; + + while(p) + + { + + if(p%2==0) + + { + + a=(a*a)%n; + + p/=2; + + } + + else + + { + + rez=(rez*a)%n; + + p--; + + } + + } + + return rez; + +} + +int main() + +{ + + fin>>a>>n; + + fout< + + +using namespace std; + + + +ifstream f ("inversmodular.in"); + +ofstream g ("inversmodular.out"); + + + +int a, n; + + + +int phi (int x) + +{ + + int rez = x; + + for (int i=2; i*i<=x; i++) + + { + + if (x % i == 0) + + { + + while (x % i == 0) + + x /= i; + + + + rez = (rez / i) * (i-1); + + } + + } + + + + if (x > 1) + + rez = (rez/x) * (x-1); + + return rez; + +} + + + +int Inv_Mod (int x, int mod) + +{ + + int p = phi(mod) - 1; + + int rez = 1; + + + + while (p) + + { + + if (p % 2 == 1) + + rez = (1LL * rez * x) % mod; + + x = (1LL * x * x) % mod; + + p /= 2; + + } + + + + return rez; + +} + + + +int main() + +{ + + f >> a >> n; + + g << Inv_Mod(a, n); + + return 0; + +} diff --git a/invmod/euler-toutient/c050b448-b456-416e-8629-8c54290cebf6.cpp b/invmod/euler-toutient/c050b448-b456-416e-8629-8c54290cebf6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e85451e6ba439ed3a7c75dc1a01aba62782ec73a --- /dev/null +++ b/invmod/euler-toutient/c050b448-b456-416e-8629-8c54290cebf6.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; +ifstream fin ("inversmodular.in"); +ofstream fout ("inversmodular.out"); + +long long putere_log (long long a,long long n,long long modulol) +{ + if(n==0) + return 1; + else + { + if(n%2==1) + return ((a%modulol)*((putere_log((a%modulol)*(a%modulol)%modulol,n/2,modulol)%modulol)))%modulol; + else + return (putere_log(((a%modulol)*(a%modulol))%modulol,n/2,modulol))%modulol; + } +} +long long phi_de_n (long long n) +{ + long long phin=1,d=2,contorcrt; + while(n>1) + { + if(n%d==0) + { + contorcrt=0; + while(n%d==0) + n/=d,contorcrt++; + phin=phin*(d-1)*putere_log(d,contorcrt-1,1000000007); + } + d++; + if(d*d>n) + d=n; + } + return phin; +} +long long invers_modular(long long a,long long n) +{ + + return putere_log(a,phi_de_n(n)-1,n); + +} +int main () +{ + long long a,n; + fin>>a>>n; + fout< + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol,p; + + sol = n; + + p = 2; + + for( p = 2; n > 1 && p * p <= n; p++) + + { + + if(n % p == 0) + + { + + sol = sol / p *(p-1); + + while(n%p == 0) + + n/=p; + + } + + } + + if(n > 1) + + sol = sol / n *(n-1); + + return sol; + +} + + + +int exp(int a, int n,int mod) + +{ + + int sol=1; + + while(n>0) + + { + + if(n % 2 == 1) + + sol = 1LL*sol*a%mod; + + n/=2; + + a=1LL*a*a%mod; + + + + } + + return sol; + + + +} + + + +int main() + +{ + + int a , n; + + fin >> a >> n; + + int b; + + b=exp(a,Phi(n)-1,n); + + fout< +#include + +using namespace std; + +ifstream fin; +ofstream fout; + +long long mod, a; + +long long log_exp (long long base, long long power){ + long long to_return; + + if (power == 1ll) return (base%mod); + + long long aux = power % 2; + switch (aux){ + case 0ll: + to_return = log_exp((base%mod) * (base%mod), power/2) % mod; + break; + + case 1ll: + to_return = (base%mod * ((log_exp((base%mod) * (base%mod), power/2)) %mod)); + break; + } + + return to_return; +} + +int getphi(int n) +{ + int result = n; + + + + for (int p = 2; p * p <= n; ++p) { + + + if (n % p == 0) { + + + while (n % p == 0) + n /= p; + result -= result / p; + } + } + + + + if (n > 1) + result -= result / n; + return result; +} + + +int main(void){ + fin.open("inversmodular.in"); + fin>>a>>mod; + fin.close(); + + long long result, exponent = getphi(mod); + + result = log_exp(a, (exponent-1)); + + fout.open("inversmodular.out"); + fout<<(result%mod)<<"\n"; + fout.close(); + + return 0; +} diff --git a/invmod/euler-toutient/c3a04b4b-b51f-4601-81fd-5cc29c9355d7.cpp b/invmod/euler-toutient/c3a04b4b-b51f-4601-81fd-5cc29c9355d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..679ce1bb05847f938784fc287768a7a64e9a2dd8 --- /dev/null +++ b/invmod/euler-toutient/c3a04b4b-b51f-4601-81fd-5cc29c9355d7.cpp @@ -0,0 +1,42 @@ +#include +#include + + + +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +long long n,m; + + +long long PHI(long long nr) { + long long phi = nr; + for (long long i = 2; i * i <= nr; ++i) { + if (nr % i == 0) { + while (nr % i == 0) + nr /= i; + phi = (phi / i) * (i - 1); + } + } + if (nr != 1) + phi = phi / nr * (nr - 1); + return phi; +} + +int main() { + fin >> n >> m; + long long exponent = PHI(m) - 1; + long long inverse = 1; + + + for (long long pow = 1; pow <= exponent; pow <<= 1) { + if (pow & exponent) + inverse = (inverse * n) % m; + n = (n * n) % m; + } + fout << inverse; + + return 0; +} diff --git a/invmod/euler-toutient/c3a7d4ea-ba49-4f57-9fb7-cd90db6991f5.cpp b/invmod/euler-toutient/c3a7d4ea-ba49-4f57-9fb7-cd90db6991f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87e51e73ee7254a5922f95611c3bb3387c75babf --- /dev/null +++ b/invmod/euler-toutient/c3a7d4ea-ba49-4f57-9fb7-cd90db6991f5.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long a, n; + + + +inline long long lgput(long long x, long long y) + +{ + + long long sol = 1; + + while (y) + + { + + if (y & 1) + + sol = (sol * x) % n; + + x = ((x % n) * (x % n)) % n; + + y >>= 1; + + } + + return sol; + +} + + + +inline long long phi(int n) + +{ + + long long rez = n; + + if (n % 2 == 0) + + { + + rez >>= 1; + + while (n % 2 == 0) + + n >>= 1; + + } + + for (long long i = 3; i * i <= n; i += 2) + + { + + if (n % i == 0) + + { + + rez = rez / i * (i - 1); + + while (n % i == 0) + + n /= i; + + } + + } + + if (n != 1) + + rez = rez / n * (n - 1); + + return rez; + +} + + + +int main() + +{ + + in >> a >> n; + + return out << lgput(a, phi(n) - 1), 0; + +} diff --git a/invmod/euler-toutient/c3a8cd5c-bb9e-4230-a842-d7cfafbc5196.cpp b/invmod/euler-toutient/c3a8cd5c-bb9e-4230-a842-d7cfafbc5196.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09260257487118bc6bcc0231c14b17211587b34d --- /dev/null +++ b/invmod/euler-toutient/c3a8cd5c-bb9e-4230-a842-d7cfafbc5196.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long po(long long nr,long long g){ + + long long t=1; + + for(long long i=1;i<=g;i++){ + + t*=nr; + + } + + return t; + +} + +long long phi(long long nr){ + +long long phi=nr; + +for(long long i=2;i*i<=nr;i++){ + + if(nr%i==0){ + + while(nr%i==0)nr/=i; + + phi=phi/i*(i-1); + + } + +} + +if(nr!=1)phi=phi/nr*(nr-1); + +return phi; + +} + +int main() + +{long long n,m; + + long long phinr,s=1; + + f>>n>>m; + + phinr=phi(m)-1; + + while(phinr){ + + if(phinr&1)s=(s*n)%m; + + n=(n*n)%m; + + phinr>>=1; + + } + + g< + +using namespace std; +ifstream fin ("inversmodular.in"); +ofstream fout ("inversmodular.out"); + +long long putere_log (long long a,long long n,long long modulol) +{ + if(n==0) + return 1; + else + { + if(n%2==1) + return ((a%modulol)*((putere_log((a%modulol)*(a%modulol)%modulol,n/2,modulol)%modulol)))%modulol; + else + return (putere_log(((a%modulol)*(a%modulol))%modulol,n/2,modulol))%modulol; + } +} +long long phi_de_n (long long n) +{ + long long phin=1,d=2,contorcrt; + while(n>1) + { + if(n%d==0) + { + contorcrt=0; + while(n%d==0) + n/=d,contorcrt++; + phin=phin*(d-1)*putere_log(d,contorcrt-1,1000000007); + } + d++; + if(d*d>n) + d=n; + } + return phin; +} +long long invers_modular(long long a,long long n) +{ + + return putere_log(a,phi_de_n(n)-1,n); + +} +int main () +{ + long long a,n; + fin>>a>>n; + fout< +#include + + +using namespace std; + +ifstream fin ( "inversmodular.in" ); +ofstream fout ( "inversmodular.out" ); + + +int phi ( int n ){ + + int euler = 1; + int e = 0; + + while ( n % 2 == 0 ){ + n /= 2; + euler *= 2; + e ++; + } + + if ( e ) + euler /= 2; + + int d = 3; + while ( d * d <= n ){ + + e = 0; + while ( n % d == 0 ){ + e ++; + n /= d; + euler *= d; + } + + if ( e ) + euler = euler / d * ( d - 1 ); + + d += 2; + } + + if ( n > 1 ) + euler *= ( n - 1 ); + return euler; +} + +int log_exp ( long long x, int p, int mod ) { + + long long rez = 1; + + while ( p > 0 ){ + + if ( p % 2 == 0 ) + x = x * x % mod, p /= 2; + else + rez = rez * x % mod, p --; + + } + return rez; +} + +int main() + +{ + + + int a, n; + + + + fin >> a >> n; + + fout << log_exp ( a, phi ( n ) - 1, n ); + + return 0; + +} diff --git a/invmod/euler-toutient/c7dde18a-0a38-4fd4-99c5-a3e08ecc6905.cpp b/invmod/euler-toutient/c7dde18a-0a38-4fd4-99c5-a3e08ecc6905.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae4369e31cad674d3f0901e2890379afe58e4b1e --- /dev/null +++ b/invmod/euler-toutient/c7dde18a-0a38-4fd4-99c5-a3e08ecc6905.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long lgput(long long a, long long b, long long MOD) { + + long long ret = 1LL; + + while (b) { + + if (b & 1LL) ret = (ret * a) % MOD; + + a = (a * a) % MOD; + + b >>= 1LL; + + } + + + + return (ret % MOD); + +} + + + +long long phi(long long nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1){ + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + + + +long long inv_mod(long long x, long long MOD) { + + return lgput(x, phi(MOD) - 1, MOD); + +} + + + +int main() { + + + + long long a, b; + + cin >> a >> b; + + cout << inv_mod(a, b); + + + + return 0; + +} diff --git a/invmod/euler-toutient/c8419485-39ef-4248-b47e-64b737934f71.cpp b/invmod/euler-toutient/c8419485-39ef-4248-b47e-64b737934f71.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12791929f1cc6a2909bec2e33e03586723fd9d69 --- /dev/null +++ b/invmod/euler-toutient/c8419485-39ef-4248-b47e-64b737934f71.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int a , n; + + + +inline int Lgput(int x , int p) + +{ + + int s = 1; + + while(p) + + { + + if(p & 1) + + s = 1LL * s * x % n; + + p /= 2; + + x = 1LL * x * x % n; + + } + + return s; + +} + + + +inline int Fi(int x) + +{ + + int fi = n , d = 2; + + while(d * d <= x && x > 1) + + { + + if(x % d == 0) + + { + + fi = fi / d * (d - 1); + + while(x % d == 0) + + x /= d; + + } + + d++; + + } + + if(x > 1) + + fi = fi / x * (x - 1); + + return fi; + +} + + + +int main() + +{ + + int p; + + fin >> a >> n; + + p = Fi(n); + + fout << Lgput(a , p - 1) << "\n"; + + fin.close(); + + fout.close(); + +} diff --git a/invmod/euler-toutient/c85c6429-1673-45d6-b620-c89963df485d.cpp b/invmod/euler-toutient/c85c6429-1673-45d6-b620-c89963df485d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fd316ca16e85d3355156528a430dbac7d98b31b --- /dev/null +++ b/invmod/euler-toutient/c85c6429-1673-45d6-b620-c89963df485d.cpp @@ -0,0 +1,66 @@ +#include +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/c8b669ce-2677-4364-b850-d2d198b34e8c.cpp b/invmod/euler-toutient/c8b669ce-2677-4364-b850-d2d198b34e8c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fff8b8e571bc35c0a5dc90afcb48b8d7148894d3 --- /dev/null +++ b/invmod/euler-toutient/c8b669ce-2677-4364-b850-d2d198b34e8c.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +int phi(int n) + +{ + + + + + + int sol = n, p; + + for(p = 2; p * p <= n && n > 1; p++) + + { + + if(n % p == 0) + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + + + return sol; + +} + + + +int lgput(int a, int n, int mod) + +{ + + int p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int a, n; + + + +int main() + +{ + + in >> a >> n; + + out << (lgput(a, (phi(n) - 1), n)); + + return 0; + +} diff --git a/invmod/euler-toutient/c8e9fdd4-6180-48fb-b7dd-f1d40ab98059.cpp b/invmod/euler-toutient/c8e9fdd4-6180-48fb-b7dd-f1d40ab98059.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/c8e9fdd4-6180-48fb-b7dd-f1d40ab98059.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/c9549c2d-d02f-401e-b263-73ef72f76b1c.cpp b/invmod/euler-toutient/c9549c2d-d02f-401e-b263-73ef72f76b1c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c3ab2638f1ddec4baef9716b5f991baf883682 --- /dev/null +++ b/invmod/euler-toutient/c9549c2d-d02f-401e-b263-73ef72f76b1c.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a , n; + + + +int phi(int n) + +{ + + int sol = n, p; + + for(p = 2; n > 1 && p * p <= n; p++) + + if(n % p == 0) + + { + + sol = sol/p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + if(n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lgput(int a, int n, int modulo) + +{ + + int p = 1; + + while(n) + + { + + if(n % 2 == 1) p = 1LL * p * a % modulo; + + n /= 2; + + a = 1LL * a * a % modulo; + + } + + return p; + + + +} + + + + + +int main() + +{ + + int b; + + fin >> a >> n; + + b = lgput(a, phi(n) - 1, n); + + fout << b; + + return 0; + +} diff --git a/invmod/euler-toutient/ca16d9f9-791d-43d8-98eb-787f9153205c.cpp b/invmod/euler-toutient/ca16d9f9-791d-43d8-98eb-787f9153205c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..637a7ad19fc4be33c38ec4b0dae9b10777adf7ba --- /dev/null +++ b/invmod/euler-toutient/ca16d9f9-791d-43d8-98eb-787f9153205c.cpp @@ -0,0 +1,204 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + + + +ofstream fout("inversmodular.out"); + + + +long long eul(long long n) + + + +{ + + + +long long r = n, d = 2; + + + +while (n > 1) + + + +{ + + + +if (n % d == 0) + + + +{ + + + +r /= d; + + + +r *= (d - 1); + + + +while (n % d == 0) + + + +n /= d; + + + +} + + + +++d; + + + +if (d * d > n) + + + +d = n; + + + +} + + + +return r; + + + +} + + + +long long mod; + + + +long long lgput(long long a, long long b) + + + +{ + + + +long long p = 1; + + + +while (b) + + + +{ + + + +if (b & 1) + + + +{ + + + +p *= a; + + + +p %= mod; + + + +b--; + + + +} + + + +else + + + +{ + + + +a *= a; + + + +a %= mod; + + + +b = b >> 1; + + + +} + + + +} + + + +return p; + + + +} + + + +int main() + + + +{ + + + +long long a, n; + + + +fin >> a >> n; + + + +long long phi = eul(n) - 1; + + + +mod = n; + + + +fout << lgput(a, phi) << "\n"; + + + +return 0; + + + +} diff --git a/invmod/euler-toutient/cb7511cf-9498-4841-8ecc-8e71569d3b7f.cpp b/invmod/euler-toutient/cb7511cf-9498-4841-8ecc-8e71569d3b7f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35d61f7b9837a2143c621907d21c08c23e9bdeae --- /dev/null +++ b/invmod/euler-toutient/cb7511cf-9498-4841-8ecc-8e71569d3b7f.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +long long rid_put(int nr,int exp,int mod) + +{ + + long long rez=1,val=nr; + + while(exp) + + { + + if(exp&1)rez=rez*val; + + val=val*val; + + rez%=mod; + + val%=mod; + + exp=exp>>1; + + } + + return rez; + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int n,k; + + scanf("%d%d",&n,&k); + + int d=2,e=0; + + int prim=k; + + int l=k; + + while(d*d<=k) + + { + + e=0; + + while(k%d==0) + + { + + k/=d; + + e++; + + } + + if(e) + + prim/=d,prim*=d-1; + + d++; + + } + + if(k>1)prim/=k,prim*=k-1; + + + + cout< + + +using namespace std; + + + +int GetPhi(int x) + +{ + + int phi = x; + + + + for(int i = 2; i * i <= x; i++) + + if(x % i == 0) + + { + + phi = (phi / i) * (i - 1); + + + + while(x % i == 0) + + x /= i; + + } + + + + if(x != 1) + + phi = (phi / x) * (x - 1); + + + + return phi; + +} + + + +int RidPut(int base, int exp, int mod) + +{ + + int sol = 1; + + int aux = base % mod; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + sol = 1LL * sol * aux % mod; + + + + aux = 1LL * aux * aux % mod; + + } + + + + return sol; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, N; + + + + fin >> A >> N; + + + + int phi = GetPhi(N); + + fout << RidPut(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/cec1f1d2-e28a-4c67-8e22-87b6134c36d8.cpp b/invmod/euler-toutient/cec1f1d2-e28a-4c67-8e22-87b6134c36d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa63b53f85fbca1e6359e5ceaff833fa763cf64b --- /dev/null +++ b/invmod/euler-toutient/cec1f1d2-e28a-4c67-8e22-87b6134c36d8.cpp @@ -0,0 +1,87 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); +long long phi_de_n(long long n) + +{ + + int phin; + + phin = n; + + for(int i = 2; i * i <= n; ++i) + + { + + if(n % i == 0) + + { + + phin /= i; + + phin *= (i - 1); + + while(n % i == 0) + + { + + n /= i; + + } + + } + + } + + if(n != 1) + + { + + phin /= n; + + phin *= (n - 1); + + } + + phin -= 1; + + return phin; + +} + + + +int main() + +{ + + long long a,n; + + fin >> a >> n; + + long long doru = 1; + + long long phin = phi_de_n(n); + + for(long long i = 1; i <= phin; i <<= 1) + + { + + if(i & phin)doru = (doru * a) % n; + + a = (a * a) % n; + + } + + fout << doru; + + return 0; + +} diff --git a/invmod/euler-toutient/d0f59f72-88eb-4161-b733-5a341207dd47.cpp b/invmod/euler-toutient/d0f59f72-88eb-4161-b733-5a341207dd47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..351e704da1276bd0832e80e338f6ed32408ba285 --- /dev/null +++ b/invmod/euler-toutient/d0f59f72-88eb-4161-b733-5a341207dd47.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long eul(long long n) + +{ + + long long r = n, d = 2; + + while (n > 1) + + { + + if (n % d == 0) + + { + + r /= d; + + r *= (d - 1); + + while (n % d == 0) + + n /= d; + + } + + + + ++d; + + + + if (d * d > n) + + d = n; + + + + } + + return r; + +} + + + +long long mod; + + + +long long lgput(long long a, long long b) + +{ + + long long p = 1; + + while (b) + + { + + if (b & 1) + + { + + p *= a; + + p %= mod; + + b--; + + } + + else + + { + + a *= a; + + a %= mod; + + b = b >> 1; + + } + + } + + return p; + +} + + + +int main() + +{ + + long long a, n; + + fin >> a >> n; + + long long phi = eul(n) - 1; + + mod = n; + + fout << lgput(a, phi) << "\n"; + + return 0; + +} diff --git a/invmod/euler-toutient/d1ef34df-10c8-48e0-b0ee-64a6395aa1cf.cpp b/invmod/euler-toutient/d1ef34df-10c8-48e0-b0ee-64a6395aa1cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c06826e13e0bcd09e4dbd487249b464527972542 --- /dev/null +++ b/invmod/euler-toutient/d1ef34df-10c8-48e0-b0ee-64a6395aa1cf.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + + + +long long invMod(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + invMod(x, y, b, a%b); + + long long aux = x; + + x = y; + + y = aux - y*(a/b); + + } + +} + +long long x, y; + +int a, b; + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + cin>>a>>b; + + invMod(x,y,a,b); + + if (x < 0) x = b + x; + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long lgput(long long a, long long b, long long MOD) { + + long long ret = 1LL; + + while (b) { + + if (b & 1LL) ret = (ret * a) % MOD; + + a = (a * a) % MOD; + + b >>= 1LL; + + } + + + + return (ret % MOD); + +} + + + +long long phi(long long nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1){ + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + + + +long long inv_mod(long long x, long long MOD) { + + return lgput(x, phi(MOD) - 1, MOD); + +} + + + +int main() { + + + + long long a, b; + + cin >> a >> b; + + cout << inv_mod(a, b); + + + + return 0; + +} diff --git a/invmod/euler-toutient/d31284c5-ecf7-4574-a777-30cd1f55f81b.cpp b/invmod/euler-toutient/d31284c5-ecf7-4574-a777-30cd1f55f81b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e590b42bda66fd783341174b6839f6558342b1b --- /dev/null +++ b/invmod/euler-toutient/d31284c5-ecf7-4574-a777-30cd1f55f81b.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int main(){ + + long long int cn,a,fi,d,nr,n; + + + + fin>>a>>n; + + cn=n; + + fi=n; + + d=2; + + while(d*d<=n){ + + if(n%d==0){ + + fi=fi/d*(d-1); + + while(n%d==0) + + n/=d; + + } + + d++; + + } + + if(n>1) + + fi=fi/n*(n-1); + + fi--; + + nr=1; + + while(fi!=0){ + + if(fi%2!=0) + + nr=(nr*a)%cn; + + fi/=2; + + a=a*a%cn; + + } + + fout< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,a,phi; + +long long phi_euler(long long n) + +{ + + long long nn = n, ans = n; + + for(long long i = 2; i * i <= n; i ++) + + { + + if(nn % i == 0) + + { + + ans *= (i-1); + + ans /= i; + + while(nn % i == 0) + + nn /= i; + + } + + } + + if(nn != 1) + + { + + ans *= (nn-1); + + ans /= nn; + + } + + return ans; + +} + +long long lgput(long long base,long long exp) + +{ + + long long i; + + long long aux=base,ans=1; + + for(i=1; i<=exp; i*=2) + + { + + if(i&exp) + + ans=(1LL*ans*aux)%n; + + aux=(1LL*aux*aux)%n; + + } + + return ans; + +} + + + +int32_t main() + +{ + + fin>>a>>n; + + + + phi=phi_euler(n); + + + + fout< +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long A, N; + + + +long long phi(int x) + +{ + + long long result = x * 1LL; + + for (int i = 2; i * i <= x; ++i) + + { + + if (x % i == 0) + + { + + while (x % i == 0) + + x = x / i; + + result *= (i - 1); + + result /= i; + + } + + } + + if (x > 1) + + { + + result *= (x - 1); + + result /= x; + + } + + return result; + +} + + + +long long log2pow(long long n, long long p) + +{ + + if (p == 0) + + return 1; + + long long x = log2pow(n, p / 2); + + if (p % 2 == 0) + + return x * x % N; + + return (n * x % N * x) % N; + +} + + + +int main() + +{ + + fin >> A >> N; + + fout << log2pow(A, phi(N) - 1); + + return 0; + +} diff --git a/invmod/euler-toutient/d6cea177-cc35-42f7-9782-e2b0eefde3fc.cpp b/invmod/euler-toutient/d6cea177-cc35-42f7-9782-e2b0eefde3fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fd12114de1ed7eb226738adbaccab3f1a160798 --- /dev/null +++ b/invmod/euler-toutient/d6cea177-cc35-42f7-9782-e2b0eefde3fc.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int InvMod(int a, int b) + +{ + + long long x1 = 1, x2 = 0, aux, q, auxb = b; + + + + while(b) + + { + + q = a / b; + + aux = b; + + b = a - q * b; + + a = aux; + + aux = x2; + + x2 = x1 - q * x2; + + x1 = aux; + + } + + + + if(x1 < 0) + + { + + x1 += auxb; + + } + + + + return x1; + +} + + + +int main() + +{ + + int a, b; + + + + fin >> a >> b; + + + + fout << InvMod(a, b); + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/d74191a1-465a-4b27-bcba-4f52497a125a.cpp b/invmod/euler-toutient/d74191a1-465a-4b27-bcba-4f52497a125a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12595ebe5467d5598e675c819e7ecc08520b9f17 --- /dev/null +++ b/invmod/euler-toutient/d74191a1-465a-4b27-bcba-4f52497a125a.cpp @@ -0,0 +1,61 @@ +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +int a,n; + +int phi(int n) +{ + int p = n; + int i,j,cnt = 0; + int sus = 1,jos = 1; + for (i=2; i*i<=n; i++) + { + if (n%i == 0) + { + while (n%i == 0) + { + n /= i; + } + sus *= (i-1); + jos *= i; + } + } + if (n != 1) + { + sus *= (n-1); + jos *= n; + } + return p/jos*sus; +} + +int ridica(int a,int p) +{ + int rasp = 1; + while (p != 0) + { + if (p%2 == 0) + { + a = (long long)a*a%n; + p /= 2; + } + else + { + rasp = (long long)rasp*a%n; + p--; + } + } + return rasp; +} + +int main() +{ + in >> a >> n; + + out << ridica(a,phi(n)-1); + return 0; +} diff --git a/invmod/euler-toutient/d8e18428-0a04-4c2e-a197-1a69bf2727b7.cpp b/invmod/euler-toutient/d8e18428-0a04-4c2e-a197-1a69bf2727b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c09e044fa24fec86c8c9d18d313d9a8cf1703144 --- /dev/null +++ b/invmod/euler-toutient/d8e18428-0a04-4c2e-a197-1a69bf2727b7.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long A, N; + + + +long long phi(int x) + +{ + + long long result = x * 1LL; + + for (int i = 2; i * i <= x; ++i) + + { + + if (x % i == 0) + + { + + while (x % i == 0) + + x = x / i; + + result *= (i - 1); + + result /= i; + + } + + } + + if (x > 1) + + { + + result *= (x - 1); + + result /= x; + + } + + return result; + +} + + + +long long log2pow(long long n, long long p) + +{ + + if (p == 0) + + return 1; + + long long x = log2pow(n, p / 2); + + if (p % 2 == 0) + + return x * x % N; + + return (n * x % N * x) % N; + +} + + + +int main() + +{ + + fin >> A >> N; + + fout << log2pow(A, phi(N) - 1); + + return 0; + +} diff --git a/invmod/euler-toutient/d974dd62-5fd4-40a8-a0ac-65a565947f40.cpp b/invmod/euler-toutient/d974dd62-5fd4-40a8-a0ac-65a565947f40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c55382196e4b1b45e93d5462a8e04285b5e9eb50 --- /dev/null +++ b/invmod/euler-toutient/d974dd62-5fd4-40a8-a0ac-65a565947f40.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long Put(int a, int n, int mod) + +{ + + long long p = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + p = 1LL * p * a % mod; + + n /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + +int Phi(int n) + +{ + + int sol = n, i; + + for(i = 2;i * i <= n && n > 1;i++) + + { + + if(n % i == 0) + + { + + sol = sol / i * (i - 1); + + while(n % i == 0) + + n /= i; + + } + + } + + if(n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + +int main() + +{ + + int a, n; + + fin >> a >> n; + + fout << Put(a, Phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/d9e12ff1-b336-4430-b8ef-a934f53f56d7.cpp b/invmod/euler-toutient/d9e12ff1-b336-4430-b8ef-a934f53f56d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38c49a0d3b7adafb76c46d0f47a04b12fd07f3c3 --- /dev/null +++ b/invmod/euler-toutient/d9e12ff1-b336-4430-b8ef-a934f53f56d7.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + + + +int mod(unsigned long long int a, unsigned long long int b, unsigned long long int modulo ) +{ + return ((a%modulo)*(b%modulo))%modulo; + +} + +int totient(unsigned long long int n) +{ + int tot =n; + for(unsigned long long int i =2 ; i*i <= n ; i++ ) + { + if(n%i==0) + tot -= tot/i; + while(n%i == 0) + n /=i; + + } + + if (n > 1) + tot -=tot/n; + + return tot; + +} + +int main ( ) +{ + unsigned long long int a,n,modulo,base=a,res=1 ; + + in>>base>>n; + modulo = n; + unsigned long long int powertot = totient(n)-1; + + while(powertot) + { + if(powertot&1) + res= mod(res,base, modulo); + + base = mod(base,base, modulo); + powertot >>=1; + } + out< + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long A, N, mod; + +long long phi(long long n){ + + long long ans=n; + +for(long long i=2;i*i<=n;i++){ + + if(n%i==0) + + { + + ans=ans/i*(i-1); + + while(n%i==0) n/=i; + + } + +} + +if(n!=1) + + ans=ans/n*(n-1); + +return ans; + +} + +long long put(long long n, long long p){ + +long long ans=1; + +while(p>0){ + + if(p%2==1) + + ans=(ans*n)%mod; + + n=(n*n)%mod; + + p/=2; + +} + +return ans; + +} + +int main() + +{ + + fin>>A>>N; + + mod=N; + + long long p=phi(N)-1; + + long long sol=1; + + + + + + fout< +#include + + +using namespace std; + + + +int phi( int n ) { + + int d = 2, val, rez = 1; + + while ( d * d <= n ) { + + val = 1; + + while ( n % d == 0 ) { + + val *= d; + + n /= d; + + } + + if ( val > 1 ) + + val = val / d * ( d - 1 ); + + rez *= val; + + d ++; + + } + + if ( n > 1 ) + + rez *= n - 1; + + return rez; + +} + + + +int exponentiere( long long b, int e, int mod ) { + + long long produs = 1; + + while ( e > 0 ) { + + if ( e % 2 == 0 ) { + + b = b * b % mod; + + e /= 2; + + } else { + + produs = produs * b % mod; + + e --; + + } + + } + + return produs; + +} + +int main() { + + ifstream fin( "inversmodular.in" ); + + ofstream fout( "inversmodular.out" ); + + int n, mod, e, i; + + fin >> n >> mod; + + e = phi( mod ) - 1; + + fout << exponentiere( n, e, mod ) << endl; + + fout.close(); + + fin.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/da821b00-2627-4d38-b540-09be5d142002.cpp b/invmod/euler-toutient/da821b00-2627-4d38-b540-09be5d142002.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9325d58a81a023c7d95751ec4203a8ae8ff21d1 --- /dev/null +++ b/invmod/euler-toutient/da821b00-2627-4d38-b540-09be5d142002.cpp @@ -0,0 +1,67 @@ +#include + + +using namespace std; + +ifstream fi("inversmodular.in"); + +ofstream fo("inversmodular.out"); + +long long a,n,p; + +long long phi_n(long long n) + +{ + +long long sol=n,d; + +for(d=2;d*d<=n;d++) + +if (n%d==0) + +{ + +while(n%d==0)n/=d; + +sol= (sol/d)*(d-1); + +} + +if (n!=1) sol=sol/n*(n-1); + +return sol; + +} +long long putere(long long n,long long p,long long MOD) + +{ + +long long r=1; + +while(p){ + + if(p%2)r=(1LL*r*n)%MOD; + + n=(1LL*n*n)%MOD; + + p/=2; + + } + +return r; + +} + +int main() + +{ + +fi>>a>>n; + +p=phi_n(n)-1; + +fo< +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +long long indicatoreuler(long long N) + +{ + + long long S = N; + + + + if(N % 2 == 0) + + { + + S /= 2; + + while(N % 2 == 0) + + N /= 2; + + } + + for(long long i = 3; i * i <= N; i++) + + { + + if(N % i == 0) + + { + + S /= i; + + S *= i - 1; + + while(N % i == 0) + + N /= i; + + } + + } + + if(N!=1) + + { + + S /= N; + + S *= N-1; + + } + + return S; + +} + + + +long long lgput(long long A, long long S, long long N) + +{ + + long long rez = 1; + + while(S > 0) + + { + + if(S & 1) + + { + + rez *= A; + + rez %= N; + + S--; + + } + + A *=A; + + A %=N; + + S>>=1; + + } + + return rez; + +} + +int main() + +{ + + long long A,N; + + in >> A >> N; + + long long S = indicatoreuler(N); + + S--; + + out << lgput(A, S, N); + + return 0; + +} diff --git a/invmod/euler-toutient/dcafc0a8-71a7-4124-8239-88dd6563e70b.cpp b/invmod/euler-toutient/dcafc0a8-71a7-4124-8239-88dd6563e70b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94a56cf5af68ee7ec660cba9a56e92c56bc0ac96 --- /dev/null +++ b/invmod/euler-toutient/dcafc0a8-71a7-4124-8239-88dd6563e70b.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + + + +long long MOD; + + + +long long phi(long long a) + +{ + + long long ca = a; + + int phi = a; + + if(a % 2 == 0) + + phi /= 2 , phi *= 1; + + while(ca % 2 == 0) + + ca /= 2; + + for(int i = 3; i * i <= a; i+=2) + + { + + if(ca % i == 0) + + phi /= i , phi *= (i - 1); + + while(ca % i == 0) + + ca /= i; + + } + + if(ca > 1) + + phi /= ca , phi *= (ca - 1); + + return phi; + + + + + +} + + + +long long lgput(long long a , long long b) + +{ + + long long r = 1; + + while(b > 1) + + { + + if(b % 2) + + r = (r * a) % MOD , b--; + + a = (a * a) % MOD , b/=2; + + } + + a = (a * r) % MOD; + + return a; + +} + + + +long long im(long long a) + +{ + + return lgput(a , phi(MOD) - 1) % MOD; + +} + + + +int main() + +{ + + long long a; + + cin >> a >> MOD; + + cout << im(a); + + return 0; + +} diff --git a/invmod/euler-toutient/de4b29fa-764a-4fc8-8b80-58ab0b6325d8.cpp b/invmod/euler-toutient/de4b29fa-764a-4fc8-8b80-58ab0b6325d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0879aab66b2b5a351f6c5bf140ce969b621b1d8c --- /dev/null +++ b/invmod/euler-toutient/de4b29fa-764a-4fc8-8b80-58ab0b6325d8.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Phi(int n) + +{ + + int sol, p; + + sol = n; + + for(p = 2;n > 1 && p * p <= n;p++) + + { + + if(n % p == 0) + + { + + sol = sol / p * (p - 1); + + while(n % p == 0) + + n /= p; + + } + + } + + if(n > 1) + + sol = sol / n * (n - 1); + + return sol; + +} + +int invmod(int a, int n, int MOD) + +{ + + int sol = 1; + + while(n > 0) + + { + + if(n % 2 == 1) + + sol = 1LL * sol * a % MOD; + + n /= 2; + + a = 1LL * a * a % MOD; + + } + + return sol; + +} + +int a, n; + + + +int main() + +{ + + fin >> a >> n; + + fout << invmod(a, Phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/dfd67ef2-b72d-4082-9811-d3d75a8e65bf.cpp b/invmod/euler-toutient/dfd67ef2-b72d-4082-9811-d3d75a8e65bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3e0d18c4116c98a34e84073679f855c504413c0 --- /dev/null +++ b/invmod/euler-toutient/dfd67ef2-b72d-4082-9811-d3d75a8e65bf.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + + + +int n; + + + +int rid_put(int a,int b) + +{ + + int sol=1; + + for(long long i=1;i<=b;i<<=1) + + { + + if(b&i) sol=(1LL*sol*a)%n; + + a=(1LL*a*a)%n; + + } + + return sol; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a; + + scanf("%d%d",&a,&n); + + int c=n,fi=n; + + for(int i=2;i*i<=c;i++) + + if(c%i==0) + + { + + while(c%i==0) c/=i; + + fi=fi/i*(i-1); + + } + + if(c>1) fi=fi/c*(c-1); + + printf("%d",rid_put(a,fi-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/e209a5ce-4bf5-4131-a9da-7d809296d73d.cpp b/invmod/euler-toutient/e209a5ce-4bf5-4131-a9da-7d809296d73d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74deb279c811f2929a45e0f984da0d70ae951337 --- /dev/null +++ b/invmod/euler-toutient/e209a5ce-4bf5-4131-a9da-7d809296d73d.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n, mod; + + + +int Get_phi(int x) + +{ + + int phi = x; + + + + if(x % 2 == 0) + + { + + phi = phi / 2; + + while(x % 2 == 0) + + x /= 2; + + } + + + + int d = 3; + + + + while(x > 1 && d * d <= x) + + { + + if(x % d == 0) + + { + + phi = phi / d * (d - 1); + + while(x % d == 0) + + x /= d; + + } + + d += 2; + + } + + + + if(x > 1) phi = phi / x * (x - 1); + + + + return phi; + +} + + + +int Lgput(int b, int e) + +{ + + int p = 1; + + while(e) + + { + + if(e % 2) p = 1LL * p * b % mod; + + b = 1LL * b * b % mod; + + e /= 2; + + } + + + + return p; + +} + + + +int main() + +{ + + fin >> a >> n; + + + + mod = n; + + n = Get_phi(n); + + fout << Lgput(a, n - 1) << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/e20fe040-da7b-4d5f-a836-4c3713501f3b.cpp b/invmod/euler-toutient/e20fe040-da7b-4d5f-a836-4c3713501f3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13c349c7339a69208cfa7936e0df3132d4b5e385 --- /dev/null +++ b/invmod/euler-toutient/e20fe040-da7b-4d5f-a836-4c3713501f3b.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +int n; +long long indicator, invers, a; +void desc(int nr) +{ + indicator = nr; + for (int i=2;i*i<=nr;i++){ + if (nr%i==0){ + while(nr%i==0){ + nr/=i; + } + indicator /= i; + indicator *= i-1; + } + } + if (nr>1){ + indicator /= nr; + indicator *= nr-1; + } +} +long long euler() +{ + long long inv = 1; + indicator--; + while(indicator > 0){ + if (indicator%2==1){ + inv = (inv * a)%n; + } + a = (a * a)%n; + indicator /=2; + } + return inv; +} +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%lld%d", &a, &n); + desc(n); + invers = euler(); + printf("%lld", invers%n); + return 0; +} diff --git a/invmod/euler-toutient/e38ddb81-54d6-4746-a938-5e264e934057.cpp b/invmod/euler-toutient/e38ddb81-54d6-4746-a938-5e264e934057.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e016ff373c61182e12c2d0708e074895a7a9081e --- /dev/null +++ b/invmod/euler-toutient/e38ddb81-54d6-4746-a938-5e264e934057.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +int GetPhi(int X) + +{ + + int phi = X; + + + + for(int i = 2; i * i <= X; i++) + + if(X % i == 0) + + { + + phi = (phi / i) * (i - 1); + + + + while(X % i == 0) + + X /= i; + + } + + + + if(X != 1) + + phi = (phi / X) * (X - 1); + + + + return phi; + +} + + + +int RidPut(int base, int exp, int mod) + +{ + + int ans = 1; + + int aux = base % mod; + + + + for(long long i = 1; i <= exp; i <<= 1) + + { + + if(i & exp) + + ans = 1LL * ans * aux % mod; + + + + aux = 1LL * aux * aux % mod; + + } + + + + return ans; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, N; + + fin >> A >> N; + + + + int phi = GetPhi(N); + + fout << RidPut(A, phi - 1, N); + + + + return 0; + +} diff --git a/invmod/euler-toutient/e411c871-fb16-41d9-aa1e-ed5e834f3953.cpp b/invmod/euler-toutient/e411c871-fb16-41d9-aa1e-ed5e834f3953.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73da4e98279e1df2f6c369e5910a00f9bb9fd9b6 --- /dev/null +++ b/invmod/euler-toutient/e411c871-fb16-41d9-aa1e-ed5e834f3953.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + + + +int n; + + + +int rid_put(int a,int b) + +{ + + int sol=1; + + for(long long i=1;i<=b;i<<=1) + + { + + if(b&i) sol=(1LL*sol*a)%n; + + a=(1LL*a*a)%n; + + } + + return sol; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a; + + scanf("%d%d",&a,&n); + + int fi=n,nr=n; + + for(int i=2;i*i<=nr;i++) + + if(nr%i==0) + + { + + while(nr%i==0) nr/=i; + + fi=(fi/i)*(i-1); + + } + + if(nr>1) fi=(fi/nr)*(nr-1); + + printf("%d",rid_put(a,fi-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/e43f3355-f2f4-4ea4-9350-ab7b6fd2772d.cpp b/invmod/euler-toutient/e43f3355-f2f4-4ea4-9350-ab7b6fd2772d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fd5ca11cec2341df966529b59050610f547d7eb --- /dev/null +++ b/invmod/euler-toutient/e43f3355-f2f4-4ea4-9350-ab7b6fd2772d.cpp @@ -0,0 +1,76 @@ +#include +#include + + +using namespace std; + +ifstream fin ( "inversmodular.in" ); +ofstream fout ( "inversmodular.out" ); + + +int phi ( int n ){ + + int euler = 1; + int e = 0; + + while ( n % 2 == 0 ){ + n /= 2; + euler *= 2; + e ++; + } + + if ( e ) + euler /= 2; + + int d = 3; + while ( d * d <= n ){ + + e = 0; + while ( n % d == 0 ){ + e ++; + n /= d; + euler *= d; + } + + if ( e ) + euler = euler / d * ( d - 1 ); + + d += 2; + } + + if ( n > 1 ) + euler *= ( n - 1 ); + return euler; +} + +int log_exp ( long long x, int p, int mod ) { + + long long rez = 1; + + while ( p > 0 ){ + + if ( p % 2 == 0 ) + x = x * x % mod, p /= 2; + else + rez = rez * x % mod, p --; + + } + return rez; +} + +int main() + +{ + + + int a, n; + + + + fin >> a >> n; + + fout << log_exp ( a, phi ( n ) - 1, n ); + + return 0; + +} diff --git a/invmod/euler-toutient/e464c7a2-94fa-468c-98b4-74770179af16.cpp b/invmod/euler-toutient/e464c7a2-94fa-468c-98b4-74770179af16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02e135c8e8be1adc6e143fa8c8c13c3eb24d6715 --- /dev/null +++ b/invmod/euler-toutient/e464c7a2-94fa-468c-98b4-74770179af16.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int euler(int n){ + + long long d = 2, p, prod = n; + + while ( d * d <= n ) { + + p = 0; + + while ( n % d == 0 ) { + + n /= d; + + p ++; + + } + + if ( p ) + + prod = prod / d * ( d - 1 ); + + d ++; + + } + + if ( n > 1 ) + + prod = prod / n * ( n - 1 ); + + return prod; + +} + +int putere(int a,int n,int m){ + + int prod = 1; + + do{ + + if(n%2 != 0){ + + prod = (long long)prod * a % m; + + } + + a = 1LL * a * a % m; + + n /= 2; + + }while(n != 0); + + return prod; + +} + +int main() + +{ + + int a,n,x; + + cin >> a >> n; + + x = putere(a,euler(n) - 1,n); + + cout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/e6eb7584-becb-41d3-956f-1f0748a56bff.cpp b/invmod/euler-toutient/e6eb7584-becb-41d3-956f-1f0748a56bff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a28133bc579031bdf5a19a949e52ed9cdce3745c --- /dev/null +++ b/invmod/euler-toutient/e6eb7584-becb-41d3-956f-1f0748a56bff.cpp @@ -0,0 +1,105 @@ +#include +#include + + +using namespace std; + +long long n,rez,a,x=1,p; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long putere() + +{ + + while(rez) + + { + + if(rez%2==1) + + { + + x=(x*a)%p; + + rez--; + + } + + else + + { + + rez/=2; + + a=(a*a)%p; + + } + + } + + return x%n; + +} + +void desc(int n) + +{ + + rez=n; + + if(n%2==0) + + rez=rez/2; + + while(n%2==0) + + { + + n/=2; + + } + + for(long long d=3; d*d<=n; d+=2) + + { + + if(n%d==0) + + rez=(rez/d)*(d-1); + + while(n%d==0) + + { + + n/=d; + + } + + } + + if(n!=1) + + rez=(rez/n)*(n-1); + +} + +int main() + +{ + + fin>>a>>n; + + p=n; + + desc(n); + + rez--; + + fout< + + +std::ifstream fin ("inversmodular.in"); + +std::ofstream fout ("inversmodular.out"); + + + +int64_t Pow(int64_t a, int64_t p, int64_t m) + +{ + + int64_t res = 1; + + while (p > 0) + + { + + if (p % 2 == 1) + + { + + res = (res * a) % m; + + } + + a = (a * a) % m; + + p /= 2; + + } + + return res; + +} + + + +int64_t Phi(int64_t n) + +{ + + int64_t r = 1, d = 2, p = 0; + + while (n > 1) + + { + + p = 0; + + while (n % d == 0) + + { + + n /= d; + + ++p; + + } + + if (p != 0) + + { + + r *= (d - 1) * Pow(d, p - 1, LONG_LONG_MAX); + + } + + ++d; + + if(n > 1 && d * d > n) + + { + + d = n; + + } + + } + + return r; + +} + + + +int64_t InversModular(const int64_t & a, const int64_t & n) + +{ + + return Pow(a, Phi(n) - 1, n); + +} + + + +int main() + +{ + + int64_t a, n; + + + + fin >> a >> n; + + + + fout << InversModular(a, n); + + + + return 0; + +} diff --git a/invmod/euler-toutient/e7b00c2f-979c-4765-acbb-2b4e88f3a90a.cpp b/invmod/euler-toutient/e7b00c2f-979c-4765-acbb-2b4e88f3a90a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a28133bc579031bdf5a19a949e52ed9cdce3745c --- /dev/null +++ b/invmod/euler-toutient/e7b00c2f-979c-4765-acbb-2b4e88f3a90a.cpp @@ -0,0 +1,105 @@ +#include +#include + + +using namespace std; + +long long n,rez,a,x=1,p; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long putere() + +{ + + while(rez) + + { + + if(rez%2==1) + + { + + x=(x*a)%p; + + rez--; + + } + + else + + { + + rez/=2; + + a=(a*a)%p; + + } + + } + + return x%n; + +} + +void desc(int n) + +{ + + rez=n; + + if(n%2==0) + + rez=rez/2; + + while(n%2==0) + + { + + n/=2; + + } + + for(long long d=3; d*d<=n; d+=2) + + { + + if(n%d==0) + + rez=(rez/d)*(d-1); + + while(n%d==0) + + { + + n/=d; + + } + + } + + if(n!=1) + + rez=(rez/n)*(n-1); + +} + +int main() + +{ + + fin>>a>>n; + + p=n; + + desc(n); + + rez--; + + fout< +using namespace std; + +int v[1000005]; + +int euler(int x){ + + int nr=x; + + int d=2; + + while(d*d<=x){ + + int ex=0; + + while(x%d==0){ + + x/=d; + + ++ex; + + } + + if(ex>0) + + nr=1LL*nr*(d-1)/d; + + ++d; + + } + + if(x>1) + + nr=1LL*nr*(x-1)/x; + + return nr; + +} + +int lg_pow(long long a,long long b,int MOD){ + + long long ans=1,p=a; + + for(long long i=1;i<=b;i<<=1){ + + if(i&b) + + ans=ans*p%MOD; + + p=p*p%MOD; + + } + + return ans; + +} + +int main(){ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int a,n; + + scanf("%d %d",&a,&n); + + printf("%d\n",lg_pow(a,euler(n)-1,n)); + + return 0; + +} diff --git a/invmod/euler-toutient/e8217605-4623-4607-8dd8-2bc229d315af.cpp b/invmod/euler-toutient/e8217605-4623-4607-8dd8-2bc229d315af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45bb7449d952297f5ed9cf2985aaaefc96021637 --- /dev/null +++ b/invmod/euler-toutient/e8217605-4623-4607-8dd8-2bc229d315af.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int a,n; + + + +int logpow2(int a,int p) + +{ + + int rez=1,inm=a; + + + + while(p) + + { + + if(p%2) + + rez*=inm; + + + + p/=2; + + inm=inm*inm; + + } + + + + return rez; + +} + + + +int coprime(int nr) + +{ + + int fact[101],nrFact[101],t=0; + + int sum=1; + + + + int radNr=sqrt(nr); + + for(int d=2;d<=radNr && nr!=1;d++) + + if(nr%d==0) + + { + + fact[++t]=d; + + nrFact[t]=0; + + + + while(nr%d==0) + + { + + nrFact[t]++; + + nr/=d; + + } + + } + + + + if(nr!=1) + + { + + fact[++t]=nr; + + nrFact[t]=1; + + } + + + + for(int i=1;i<=t;i++) + + sum*=logpow2(fact[i],nrFact[i]-1)*(fact[i]-1); + + + + return sum; + +} + + + +int logpow(int a,int p) + +{ + + long long rez=1; + + long long inm=a; + + + + while(p) + + { + + if(p%2) + + rez=(rez*inm)%n; + + + + p/=2; + + inm=(inm*inm)%n; + + } + + + + return rez; + +} + + + +int main() + +{ + + in>>a>>n; + + + + int p=coprime(n); + + int rez=logpow(a,p-1); + + + + out< +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , p; + + for (p = 2 ; n > 1 && p * p <= n ; p++) + + if (n % p == 0) + + { + + sol = sol / p * (p - 1); + + while (n % p == 0) + + n /= p; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +int lg(int a , int b , int mod) + +{ + + int p = 1; + + while (b > 0) + + { + + if (b % 2 == 1) + + p = 1LL * p * a % mod; + + b /= 2; + + a = 1LL * a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + int a , n , rez; + + fin >> a >> n; + + rez = lg(a , Phi(n) - 1 , n); + + fout << rez; + + return 0; + +} diff --git a/invmod/euler-toutient/e8633e1c-ae63-457c-8d42-e1920dee7e9a.cpp b/invmod/euler-toutient/e8633e1c-ae63-457c-8d42-e1920dee7e9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d08a9dee526480bb3b84c4840500b1fe0de2ca64 --- /dev/null +++ b/invmod/euler-toutient/e8633e1c-ae63-457c-8d42-e1920dee7e9a.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int phi(long long m) + +{ + + long long k=m,i; + + for(i=2;i*i<=m;i++) + + if(m%i==0) + + { + + while(m%i==0)m/=i; + + k=k/i*(i-1); + + } + + if(m!=1) + + k=k/m*(m-1); + + return k; + +} + +int main() + +{ + + long long a,n,p,rez; + + f>>a>>n; + + p=phi(n)-1; + + rez=1; + + while(p) + + { + + if(p&1) rez=rez*a%n; + + a=a*a%n; + + p/=2; + + } + + g< + + +using namespace std; + +int a,n; + +int euler(int x) + +{ + + int e=1; + + if(x%2==0) + + { + + x/=2; + + while(x%2==0) + + { + + x/=2; + + e*=2; + + } + + } + + for(int d=3;d*d<=x;d+=2) + + if(x%d==0) + + { + + x/=d; + + e*=d-1; + + while(x%d==0) + + { + + x/=d; + + e*=d; + + } + + } + + if(x>1) + + e*=x-1; + + return e; + +} + +int putere(int b,int e) + +{ + + if(e==0) + + return 1; + + int r=putere(b,e/2); + + r=1LL*r*r%n; + + if(e%2==1) + + r=1LL*r*b%n; + + return r; + + + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%d%d",&a,&n); + + printf("%d",putere(a,euler(n)-1)); + + return 0; + +} diff --git a/invmod/euler-toutient/ea490032-59ca-4051-b465-ff5ae0fa9522.cpp b/invmod/euler-toutient/ea490032-59ca-4051-b465-ff5ae0fa9522.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e7af4c7edebee5f338ef1eea2ba90924eef67b6 --- /dev/null +++ b/invmod/euler-toutient/ea490032-59ca-4051-b465-ff5ae0fa9522.cpp @@ -0,0 +1,43 @@ +#include + +int A, N; + +int totient(int N) { + int result = N; + for (int i = 2; i * i <= N; i++) { + if (N % i == 0) { + result = result / i * (i - 1); + while (N % i == 0) { + N /= i; + } + } + } + + if (N != 1) { + result = result / N * (N - 1); + } + + return result; +} + +int lgput(int base, int exponent, int MOD) { + int result = 1, basePow2Value = base; + for (int powIdx = 0; (1LL << powIdx) <= exponent; powIdx++) { + if (exponent & (1 << powIdx)) { + result = (1LL * result * basePow2Value) % MOD; + } + + basePow2Value = (1LL * basePow2Value * basePow2Value) % MOD; + } + + return result; +} + +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d%d", &A, &N); + printf("%d\n", lgput(A, totient(N) - 1, N)); + return 0; +} diff --git a/invmod/euler-toutient/eba79f7f-138f-42f0-805c-1e7c48cee5cb.cpp b/invmod/euler-toutient/eba79f7f-138f-42f0-805c-1e7c48cee5cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38c49a0d3b7adafb76c46d0f47a04b12fd07f3c3 --- /dev/null +++ b/invmod/euler-toutient/eba79f7f-138f-42f0-805c-1e7c48cee5cb.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + + + +int mod(unsigned long long int a, unsigned long long int b, unsigned long long int modulo ) +{ + return ((a%modulo)*(b%modulo))%modulo; + +} + +int totient(unsigned long long int n) +{ + int tot =n; + for(unsigned long long int i =2 ; i*i <= n ; i++ ) + { + if(n%i==0) + tot -= tot/i; + while(n%i == 0) + n /=i; + + } + + if (n > 1) + tot -=tot/n; + + return tot; + +} + +int main ( ) +{ + unsigned long long int a,n,modulo,base=a,res=1 ; + + in>>base>>n; + modulo = n; + unsigned long long int powertot = totient(n)-1; + + while(powertot) + { + if(powertot&1) + res= mod(res,base, modulo); + + base = mod(base,base, modulo); + powertot >>=1; + } + out< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a,n,MOD; + +long long lgnp(long long n, long long p) + +{ + + n=n%MOD; + + long long r=1; + + while(p) + + { + + if(p%2==1) r=(1LL*r*n)%MOD; + + n=(n*n*1LL)%MOD; + + p/=2; + + } + + return r%MOD; + +} + +long long phi(long long n) + +{ + + int d=2,fm; + + long long nr=1; + + do + + { + + fm=0; + + while(n%d==0) + + { + + n/=d; + + fm++; + + } + + if(fm>0) nr*=(d-1)*lgnp(d,fm-1); + + d++; + + if(d*d>n&&n>1) + + { + + nr=nr*(n-1); + + n=1; + + } + + } + + while(n>1); + + return nr; + +} + +int main() + +{ + + fin>>a>>n; + + MOD=n; + + fout< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n, mod; + + + +int Get_phi(int x) + +{ + + int phi = x; + + + + if(x % 2 == 0) + + { + + phi = phi / 2; + + while(x % 2 == 0) + + x /= 2; + + } + + + + int d = 3; + + + + while(x > 1 && d * d <= x) + + { + + if(x % d == 0) + + { + + phi = phi / d * (d - 1); + + while(x % d == 0) + + x /= d; + + } + + d += 2; + + } + + + + if(x > 1) phi = phi / x * (x - 1); + + + + return phi; + +} + + + +int Lgput(int b, int e) + +{ + + int p = 1; + + while(e) + + { + + if(e % 2) p = 1LL * p * b % mod; + + b = 1LL * b * b % mod; + + e /= 2; + + } + + + + return p; + +} + + + +int main() + +{ + + fin >> a >> n; + + + + mod = n; + + n = Get_phi(n); + + fout << Lgput(a, n - 1) << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/ef50515f-a179-48c4-addb-5a5c08ce9211.cpp b/invmod/euler-toutient/ef50515f-a179-48c4-addb-5a5c08ce9211.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12791929f1cc6a2909bec2e33e03586723fd9d69 --- /dev/null +++ b/invmod/euler-toutient/ef50515f-a179-48c4-addb-5a5c08ce9211.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int a , n; + + + +inline int Lgput(int x , int p) + +{ + + int s = 1; + + while(p) + + { + + if(p & 1) + + s = 1LL * s * x % n; + + p /= 2; + + x = 1LL * x * x % n; + + } + + return s; + +} + + + +inline int Fi(int x) + +{ + + int fi = n , d = 2; + + while(d * d <= x && x > 1) + + { + + if(x % d == 0) + + { + + fi = fi / d * (d - 1); + + while(x % d == 0) + + x /= d; + + } + + d++; + + } + + if(x > 1) + + fi = fi / x * (x - 1); + + return fi; + +} + + + +int main() + +{ + + int p; + + fin >> a >> n; + + p = Fi(n); + + fout << Lgput(a , p - 1) << "\n"; + + fin.close(); + + fout.close(); + +} diff --git a/invmod/euler-toutient/f01e6b18-6c59-4113-a759-b9af388700be.cpp b/invmod/euler-toutient/f01e6b18-6c59-4113-a759-b9af388700be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bf7581aa11e82e5d087502f947b86bcd1c90807 --- /dev/null +++ b/invmod/euler-toutient/f01e6b18-6c59-4113-a759-b9af388700be.cpp @@ -0,0 +1,85 @@ +#include + + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int a, n; + + + +int exp(int N, int p) { + + int r = 1; + + while (p) { + + if (p % 2 == 1)r = (1LL * N * r) % n; + + N =(1LL * N * N) % n; + + p /= 2; + + } + + return r; + +} + + + +int phi(int n) { + + int result = n; + + + + for (int p = 2; p * p <= n; ++p) { + + + + + + if (n % p == 0) { + + while (n % p == 0) + + n /= p; + + result -= result / p; + + } + + } + + if (n > 1) + + result -= result / n; + + return result; + +} + + + +int main() { + + ios_base::sync_with_stdio(false); + + in.tie(NULL), out.tie(NULL); + + in >> a >> n; + + out << exp(a, phi(n) - 1) % n; + + + +} diff --git a/invmod/euler-toutient/f0acc67f-438c-4fad-887f-447d8b4885ca.cpp b/invmod/euler-toutient/f0acc67f-438c-4fad-887f-447d8b4885ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db986d1928b2d079410932f2a4b9840822c592e7 --- /dev/null +++ b/invmod/euler-toutient/f0acc67f-438c-4fad-887f-447d8b4885ca.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +void euclid_e(int a, int b, int &x, int &y, int &d){ + + if ( b == 0 ){ + + d = a; + + x = 1; + + y = 0; + + return; + + } + + int xx, yy, q = a/b; + + euclid_e(b, a%b, xx, yy, d); + + x = yy; + + y = xx - yy*q; + +} + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int a, n, x, y, d, div, exp, nrd, r; + + fin >> a >> n; + + euclid_e(a, n, x, y, d); + + x %= n; + + if ( x < 0 ){ + + x += n; + + } + + fout << x; + + return 0; + +} diff --git a/invmod/euler-toutient/f1942ee5-3a29-47ac-a5cd-b5ccb50021e7.cpp b/invmod/euler-toutient/f1942ee5-3a29-47ac-a5cd-b5ccb50021e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7acce6a788b5cf1c7391439c21056b37fdd98cbf --- /dev/null +++ b/invmod/euler-toutient/f1942ee5-3a29-47ac-a5cd-b5ccb50021e7.cpp @@ -0,0 +1,72 @@ +#include + + + + + + +long long N,M; + + + +long long getphi(long long nr) + +{ + + long long cur = nr; + + for(long long i = 2;i * i <= nr; ++i) + + { + + if (nr % i == 0) + + { + + while(nr % i == 0)nr /= i; + + cur = (cur / i) * (i - 1); + + } + + } + + if (nr != 1) cur = cur / nr * (nr - 1); + + return cur; + +} + + + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%lld %lld\n",&N,&M); + + long long put = getphi(M) - 1; + + long long nr = N; + + long long crt = 1; + + for(long long p = 1;p <= put;p <<= 1) + + { + + if (p & put) crt = (crt * nr) % M; + + nr = (nr * nr) % M; + + } + + printf("%lld\n",crt); + + return 0; + +} diff --git a/invmod/euler-toutient/f27dcb4b-9da9-4dc4-8786-0663cab91f78.cpp b/invmod/euler-toutient/f27dcb4b-9da9-4dc4-8786-0663cab91f78.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a17d45cb53e639c6958f26b3a754d62d0841b34 --- /dev/null +++ b/invmod/euler-toutient/f27dcb4b-9da9-4dc4-8786-0663cab91f78.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + +int N, A; + + + +int GetPhi(int x) + +{ + + int phi = x; + + for (int i = 2;i * i <= x;++i) + + { + + if (x % i == 0) + + { + + while (x % i == 0) + + x /= i; + + phi = phi / i * (i - 1); + + } + + } + + if (x != 1) + + phi = phi / x * (x - 1); + + return phi; + +} + + + +int lgput(int base, int exp) + +{ + + int res = 1; + + while (exp > 0) + + { + + if (exp % 2) + + res = (1LL * res * base) % N; + + base = (1LL * base * base) % N; + + exp /= 2; + + } + + return res; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin >> A >> N; + + fout << lgput(A, GetPhi(N) - 1) << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/euler-toutient/f439cb90-cc8e-40d7-8ae7-bc3a787c26dd.cpp b/invmod/euler-toutient/f439cb90-cc8e-40d7-8ae7-bc3a787c26dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35d61f7b9837a2143c621907d21c08c23e9bdeae --- /dev/null +++ b/invmod/euler-toutient/f439cb90-cc8e-40d7-8ae7-bc3a787c26dd.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +long long rid_put(int nr,int exp,int mod) + +{ + + long long rez=1,val=nr; + + while(exp) + + { + + if(exp&1)rez=rez*val; + + val=val*val; + + rez%=mod; + + val%=mod; + + exp=exp>>1; + + } + + return rez; + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int n,k; + + scanf("%d%d",&n,&k); + + int d=2,e=0; + + int prim=k; + + int l=k; + + while(d*d<=k) + + { + + e=0; + + while(k%d==0) + + { + + k/=d; + + e++; + + } + + if(e) + + prim/=d,prim*=d-1; + + d++; + + } + + if(k>1)prim/=k,prim*=k-1; + + + + cout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long phi(long long n) + +{ + + long long cur=n; + + for (int i=2;i*i<=n;i++) + + { + + if (n%i==0) + + { + + while (n%i==0) + + { + + n/=i; + + } + + cur=cur*(i-1)/i; + + } + + } + + if (n!=1) cur=cur*(n-1)/n; + + return cur; + +} + +int main() + +{ + + long long a,n,x,b,p; + + fin >> a >> n; + + b=phi(n)-1; + + p=1; + + while (b) + + { + + if (b%2) p=(p*a)%n; + + a=(a*a)%n; + + b/=2; + + } + + x=p%n; + + fout << x; + +} diff --git a/invmod/euler-toutient/f5d900a6-eeef-48fc-a93e-1f20192606e2.cpp b/invmod/euler-toutient/f5d900a6-eeef-48fc-a93e-1f20192606e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38d13d8df3415fea53c777b9520da5f7c8667178 --- /dev/null +++ b/invmod/euler-toutient/f5d900a6-eeef-48fc-a93e-1f20192606e2.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +long long phi(int nr) { + + long long ans = nr; + + for (long long i = 2; i * i <= nr; i++) { + + if (nr % i) { + + continue; + + } + + while (nr % i == 0) { + + nr /= i; + + } + + ans /= i; + + ans *= (i - 1LL); + + } + + if (nr > 1) { + + ans /= nr; + + ans *= (nr - 1LL); + + } + + return ans; + +} + + + +long long lgput(long long n, long long p, long long mod) { + + long long ans = 1; + + while (p) { + + if (p % 2) { + + ans *= n; + + ans = ans % mod; + + } + + n *= n; + + n = n % mod; + + p /= 2; + + } + + return (ans % mod); + +} + + + +int main() { + + + + long long a, n, ans; + + cin >> a >> n; + + cout << lgput(a, phi(n) - 1, n); + + return 0; + +} diff --git a/invmod/euler-toutient/f6276777-2989-443f-9335-e601f42bc8c1.cpp b/invmod/euler-toutient/f6276777-2989-443f-9335-e601f42bc8c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65c500369c6f64ef3a6d1dfec5f8496fa5b5d745 --- /dev/null +++ b/invmod/euler-toutient/f6276777-2989-443f-9335-e601f42bc8c1.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Phi(int n) + +{ + + int sol = n , i; + + for (i = 2 ; i * i <= n && n > 1 ; i++) + + { + + if (n % i == 0) + + sol = sol / i * (i - 1); + + while (n % i == 0) + + n /= i; + + } + + if (n > 1) sol = sol / n * (n - 1); + + return sol; + +} + + + +long long log(int a , int n , int mod) + +{ + + long long p = 1; + + while (n > 0) + + { + + if (n % 2 == 1) p = 1LL*p * a % mod; + + n /= 2; + + a = 1LL*a * a % mod; + + } + + return p; + +} + + + +int main() + +{ + + cout << Phi(12); + + int a , n; + + fin >> a >> n; + + fout << log(a , Phi(n) - 1 , n); + + return 0; + +} diff --git a/invmod/euler-toutient/fb79794d-4a6f-4cb6-8b0a-3cacb742444e.cpp b/invmod/euler-toutient/fb79794d-4a6f-4cb6-8b0a-3cacb742444e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25d822e3c870fad4bc4afb1378daa0d821adc73a --- /dev/null +++ b/invmod/euler-toutient/fb79794d-4a6f-4cb6-8b0a-3cacb742444e.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,a,phi; + +long long phi_euler(long long n) + +{ + + long long nn = n, ans = n; + + for(long long i = 2; i * i <= n; i ++) + + { + + if(nn % i == 0) + + { + + ans *= (i-1); + + ans /= i; + + while(nn % i == 0) + + nn /= i; + + } + + } + + if(nn != 1) + + { + + ans *= (nn-1); + + ans /= nn; + + } + + return ans; + +} + +long long lgput(long long base,long long exp) + +{ + + long long i; + + long long aux=base,ans=1; + + for(i=1; i<=exp; i*=2) + + { + + if(i&exp) + + ans=(1LL*ans*aux)%n; + + aux=(1LL*aux*aux)%n; + + } + + return ans; + +} + + + +int32_t main() + +{ + + fin>>a>>n; + + + + phi=phi_euler(n); + + + + fout< +using namespace std; + +int euler(int a) + +{ + + int phi = a , d , e; + + d = 2; + + while(1LL * d * d <= a && a > 1) + + { + + e = 0; + + while(a % d == 0) + + { + + a = a / d; + + e ++; + + } + + if(e > 0) + + phi = phi / d * (d - 1); + + d ++; + + } + + if(a > 1) + + phi = phi / a * (a - 1); + + return phi; + +} + +long long fast_pow(int a , int b , int MOD) + +{ + + long long aa = a , p; + + for(p = 1 ; b ; b = b >> 1) + + { + + if(b & 1) + + p = (p * aa) % MOD; + + aa = (aa * aa) % MOD; + + } + + return p; + +} + +int main() + +{ + + freopen("inversmodular.in" , "r" , stdin); + + freopen("inversmodular.out" , "w" , stdout); + + int a , n; + + scanf("%d%d" , &a , &n); + + printf("%lld\n" , fast_pow(a , euler(n) - 1 , n)); + + return 0; + +} diff --git a/invmod/euler-toutient/fe2ba6a1-bf9a-4825-9d8e-56bb7a766d43.cpp b/invmod/euler-toutient/fe2ba6a1-bf9a-4825-9d8e-56bb7a766d43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a6600eaea04677999e3ab60f6994fc0ef352445 --- /dev/null +++ b/invmod/euler-toutient/fe2ba6a1-bf9a-4825-9d8e-56bb7a766d43.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long A,N; + +int fi(int n) + +{ + + int rez=n,d=2; + + while(d*d<=n) + + { + + if(n%d==0) + + { + + while(n%d==0) + + n/=d; + + rez=rez/d*(d-1); + + } + + d++; + + } + + if(n!=1) + + rez=rez/n*(n-1); + + return rez; + +} + +int lgput(int a,int b) + +{ + + long long rez=1; + + while(b) + + { + + if(b%2) + + rez=(1LL*rez*a)%N; + + a=(1LL*a*a)%N; + + b/=2; + + } + + return rez; + +} + +int main() + +{ + + in>>A>>N; + + out< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +void exte(int a, int b, int& m, int& n){ + + if(!b){ + + m=1; + + n=0; + + return; + + } + + int M,N; + + exte(b,a%b,M,N); + + m=N; + + n=M-N*(a/b); + + + +} + + + +int main() + +{ + + int a,n,x,y; + + fin>>a>>n; + + exte(a,n,x,y); + + while(x<0) + + x+=n; + + fout< +#include +#include +#include + +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +int A, B, X, Y; + +void Modular_Inverse(int &X, int &Y, int A, int B) +{ + if (B==0) + X=1, Y=0; + else + { + Modular_Inverse(X, Y, B, A % B); + int AUX=X; + X=Y; + Y=AUX-Y*(A / B); + } +} + +int main() +{ + fin >> A >> B; + Modular_Inverse(X, Y, A, B); + X%=B; + if (X<0) + X+=B; + fout << X << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/invmod/extended-euclid-algo/00dfdd1b-cc0a-41c7-80ec-31b66fa986fa.cpp b/invmod/extended-euclid-algo/00dfdd1b-cc0a-41c7-80ec-31b66fa986fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e379058f74d38c8f913501ec93322e481aec3a1 --- /dev/null +++ b/invmod/extended-euclid-algo/00dfdd1b-cc0a-41c7-80ec-31b66fa986fa.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +void euclid(int a, int b, int &d, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + d = a; + + } + + else + + { + + int x0, y0; + + euclid(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int invmod(int a, int n) + +{ + + int x, y, d; + + euclid(a, n, d, x, y); + + x %= n; + + if(x < 0) + + x += n; + + return x; + +} + +int main() + +{ + + int a, n; + + f >> a >> n; + + g << invmod(a, n); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/0209ead4-be86-4dc2-a9b7-96242bb938d3.cpp b/invmod/extended-euclid-algo/0209ead4-be86-4dc2-a9b7-96242bb938d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ef460aa202cc794633e85c77115265f673d3196 --- /dev/null +++ b/invmod/extended-euclid-algo/0209ead4-be86-4dc2-a9b7-96242bb938d3.cpp @@ -0,0 +1,66 @@ +#include + + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + +int main() + +{ + + FILE *f = fopen("inversmodular.in", "r"); + + FILE *g = fopen("inversmodular.out", "w"); + + int t, a, b, c, x, y; + + fscanf(f, "%d%d", &a,&b); + + eulcid_eXt(a, b, 1, x, y); + + if(x<0) x=(b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/021d2bcd-9ee6-486e-93c1-18cb9f78b194.cpp b/invmod/extended-euclid-algo/021d2bcd-9ee6-486e-93c1-18cb9f78b194.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b953054a5a277a776f906892da2eaae1506aa859 --- /dev/null +++ b/invmod/extended-euclid-algo/021d2bcd-9ee6-486e-93c1-18cb9f78b194.cpp @@ -0,0 +1,54 @@ +#include + + +using namespace std; + +ifstream f ( "inversmodular.in" ); + +ofstream g ( "inversmodular.out" ); + +void cmmdc(long long a,long long b,long long &x,long long &y) + +{ if(!b) + + { x=1; + + y=0; + + } + + else + + { long long xi,yi; + + cmmdc(b,a%b,xi,yi); + + x=yi; + + y=xi-(a/b)*yi; + + } + +} + +int main() + +{ + + long long a,b; + + f>>a>>b; + + long long y,sol; + + cmmdc(a,b,sol,y); + + if(sol<=0) sol += b; + + + + g< +#include + + + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void gcdExtended(long long a, long long b, long long &x, long long &y) { + + if (!b) { + + x = 1; + + y = 0; + + return; + + } + + gcdExtended(b, a % b, x, y); + + long long aux = x; + + x = y; + + y = aux - (a / b) * y; + +} + + + +int main() { + + long long a, n, x, y; + + fin >> a >> n; + + gcdExtended(a, n, x, y); + + if (x < 0) + + x += n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/04a077df-5611-48bc-a9bd-352e22d23c01.cpp b/invmod/extended-euclid-algo/04a077df-5611-48bc-a9bd-352e22d23c01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e9b95ae5a73da5a4f1f3d18db5a4dfe137d8295 --- /dev/null +++ b/invmod/extended-euclid-algo/04a077df-5611-48bc-a9bd-352e22d23c01.cpp @@ -0,0 +1,48 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long inv_mod(long long a, long long b, long long &x, long long &y){ + + if (b==0){ + + x=1; + + y=0; + + return a; + + } + + long long val = inv_mod(b,a%b,x,y); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + +} + +long long a,n,x,y; + +int main() + +{ + + fin >> a >> n; + + inv_mod(a,n,x,y); + + fout << (x+n)%n; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/04a26a2f-047e-429c-ac0d-de7af689982e.cpp b/invmod/extended-euclid-algo/04a26a2f-047e-429c-ac0d-de7af689982e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..404bdd760193d8928b5dd9259bf34e948af2e901 --- /dev/null +++ b/invmod/extended-euclid-algo/04a26a2f-047e-429c-ac0d-de7af689982e.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +typedef long long ll; + +ll A,N; + + + +void Euler(ll a,ll b,ll &x,ll &y) + +{ + + if( !b ) + + { + + x=1; + + y=0; + + return; + + } + + ll x0,y0; + + Euler(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + +} + + + +int main() + +{ + + f>>A>>N; + + + + + + ll x,y; + + Euler(A,N,x,y); + + x%=N; + + if(x<0) x=N+x; + + + + + + g< + + + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long x,y,a,n; + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + f>>a>>n; + + gcd(x,y,a,n); + + if(x<=0) + + x=n+x%n; + + g< +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int A, N; + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int main() + +{ + + long long inv = 0, ins; + + f >> A >> N; + + gcd(inv, ins, A, N); + + if (inv <= 0) + + inv = N + inv % N; + + g << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/0817c707-c613-42f7-9d68-3f135f5bd28f.cpp b/invmod/extended-euclid-algo/0817c707-c613-42f7-9d68-3f135f5bd28f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81a9cd5f7a1fb0c7478fb1a980cc93dff8a17ae6 --- /dev/null +++ b/invmod/extended-euclid-algo/0817c707-c613-42f7-9d68-3f135f5bd28f.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +void gcd(long long &x,long long &y,int a,int b){ + if(b==0){ + x=1; + y=0; + return; + } + gcd(x,y,b,a%b); + long long aux = x; + x = y; + y = aux - y*(a/b); +} + +int main() +{ + int a,n; + long long inv = 0,ins; + ifstream f("inversmodular.in"); + ofstream g("inversmodular.out"); + f>>a>>n; + gcd(inv,ins,a,n); + if(inv<=0) + inv = n + inv%n; + g< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invmod(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + x=1,y=0; + + else + + { + + invmod(x,y,b,a%b); + + long long c=x; + + x=y; + + y=c-y*(a/b); + + } + +} + +int main() + +{ + + long long a,b,x=0,y=0; + + fin>>a>>b; + + invmod(x,y,a,b); + + if(x<=0) + + { + + x=b+x%b; + + } + + fout< + + +using namespace std; + +ifstream in ("inversmodular.in"); + +ofstream out ("inversmodular.out"); + + + +long long int gcd(long long int a, long long int b, long long int &x, long long int &y) { + + if (a == 0) { + + x = 0; + + y = 1; + + return b; + + } + + + + long long int x1, y1, d; + + d = gcd(b % a, a, x1, y1); + + + + x = y1 - (b / a) * x1; + + y = x1; + + + + return d; + +} + + + +int main() + +{ + + long long int x, y, a, n, invmod; + + + + in>> a >> n ; + + + + invmod = gcd(a, n, x, y); + + + + while(x < 0) + + x += n; + + out<< x ; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/0bfc6aec-254d-47cc-9875-ecb3f36fd262.cpp b/invmod/extended-euclid-algo/0bfc6aec-254d-47cc-9875-ecb3f36fd262.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0de7e2279e619705bdae78793c026fc3b13bc5a4 --- /dev/null +++ b/invmod/extended-euclid-algo/0bfc6aec-254d-47cc-9875-ecb3f36fd262.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a,b,x,y; + + + +long long euclid(long long a,long long b,long long &x,long long &y){ + + if(b==0){ + + x=1; + + y=0; + + return a; + + } + + else{ + + long long X,Y,r=euclid(b,a%b,X,Y); + + x=Y; + + y=X-Y*(a/b); + + return r; + + } + +} + + + +int main() { + + fin>>a>>b; + + euclid(a,b,x,y); + + fout<<(b+x%b)%b; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/0ce5154c-6f95-471f-848e-5a9084af371d.cpp b/invmod/extended-euclid-algo/0ce5154c-6f95-471f-848e-5a9084af371d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd997f1476f12c8a5872f6b4284ad09733d97738 --- /dev/null +++ b/invmod/extended-euclid-algo/0ce5154c-6f95-471f-848e-5a9084af371d.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void euclid(int a, int b, int* d, int* x, int* y); + + + +int main() + +{ + + int a, n; + + + + fin >> a >> n; + + + + int x, y, d; + + + + euclid(a, n, &d, &x, &y); + + + + if (x < 0) + + { + + x = (n + x) % n; + + } + + + + fout << x; + + + + fin.close(); + + fout.close(); + + return 0; + +} + + + +void euclid(int a, int b, int* d, int* x, int* y) + +{ + + if (b == 0) + + { + + *d = a; + + *x = 1; + + *y = 0; + + } + + else + + { + + int x0, y0; + + + + euclid(b, a % b, d, &x0, &y0); + + + + *x = y0; + + *y = x0 - (a / b) * y0; + + } + +} diff --git a/invmod/extended-euclid-algo/0e963eee-2119-4279-9709-35c0e48d8e6a.cpp b/invmod/extended-euclid-algo/0e963eee-2119-4279-9709-35c0e48d8e6a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8eb2c2a8451c198f1c97125b81cc46a23f477282 --- /dev/null +++ b/invmod/extended-euclid-algo/0e963eee-2119-4279-9709-35c0e48d8e6a.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +void euclid(int a,int b,int &x,int &y) +{ + if (b == 0) { + x = 1; + y = 0; + return; + } + int x0, y0; + euclid(b, a%b, x0, y0); + x = y0; + y = x0 - (a / b) * y0; +} + +int main() +{ + int n; + int a, b; + + fin >> a >> b; + + int x, y; + euclid(a, b, x, y); + + if (x < 0) + x += b; + + fout << x << "\n"; +} diff --git a/invmod/extended-euclid-algo/0f259a95-16a1-4274-8057-9a9191fcfa5e.cpp b/invmod/extended-euclid-algo/0f259a95-16a1-4274-8057-9a9191fcfa5e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7aba29676c66e23c7aa1e31f8e649dcfb2d119b --- /dev/null +++ b/invmod/extended-euclid-algo/0f259a95-16a1-4274-8057-9a9191fcfa5e.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace std; + + + +int a, n; + + + +pair euclid(int a, int b) + +{ + + if(b == 0) + + return {1, 0}; + + auto p=euclid(b, a%b); + + return {p.second, p.first-(a/b)*p.second}; + +} + + + + + +int main() + +{ + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + + + scanf("%d %d", &a, &n); + + auto invers=euclid(a, n); + + int inv=invers.first; + + while(inv<0) + + inv+=n; + + printf("%d", inv); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/10ffeb6b-71b9-473a-a623-c24d80e6a075.cpp b/invmod/extended-euclid-algo/10ffeb6b-71b9-473a-a623-c24d80e6a075.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f75b325dd52803518afe8044c704789c42579ab5 --- /dev/null +++ b/invmod/extended-euclid-algo/10ffeb6b-71b9-473a-a623-c24d80e6a075.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long inv = 0,ins,a,n; + +void inv_m(long long &x,long long &y, long long a, long long b) + +{ + + if(!b) + + { + + x = 1; + + y = 0; + + } + + else + + { + + inv_m(x,y,b,a%b); + + long long aux = x; + + x = y; + + y = aux-y*(a/b); + + } + +} + +int main() + +{ + + fin >> a >> n; + + inv_m(inv,ins,a,n); + + if(inv <= 0) + + inv = n+inv%n; + + fout << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/11999684-70bb-481c-bcc9-e3b61fa4ba18.cpp b/invmod/extended-euclid-algo/11999684-70bb-481c-bcc9-e3b61fa4ba18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..893eb2e37372434b0bf6d6ab49f7d42fc7c4db18 --- /dev/null +++ b/invmod/extended-euclid-algo/11999684-70bb-481c-bcc9-e3b61fa4ba18.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a,n; + +long long x,y; + +void gcd(int a,int b,long long &x,long long &y); + +int main() { + + fin>>a>>n; + + gcd(a,n,x,y); + + if (x<=0){ + + x=n+x%n; + + } + + fout< +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a,n; + +void invers(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else{ + + invers(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + fin>>a>>n; + + long long inv=0,ins; + + invers(inv,ins,a,n); + + if(inv<=0) + + inv=n+inv%n; + + fout< + + +using namespace std; + +ifstream in ("inversmodular.in"); + +ofstream out ("inversmodular.out"); + +void euclid(int a,int b,long long &x,long long &y) + +{ + + if(b==0) + + { + + y=0; + + x=1; + + } + + else + + { + + euclid(b,a%b,x,y); + + long long d=x; + + x=y; + + y=d-y*(a/b); + + } + +} + +int main() + +{ + + int a,n; + + in>>a>>n; + + long long x,y; + + euclid(a,n,x,y); + + if(x<0) + + x=n+x%n; + + out< + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +void euclid(long long a,long long b,long long &x,long long &y) +{ + if(b==0) + { + x=1; + y=0; + } + else + { + euclid(b,a%b,x,y); + long long aux=x; + x=y; + y=aux-(a/b)*y; + } +} + +int main() +{ + long long a,n,ans=0,ins; + in>>a>>n; + euclid(a,n,ans,ins); + if(ans<=0) + ans=n+(ans%n); + out< + + + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +inline int gcdExtended(int a, int b, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + return a; + + } + + int x0, y0, d = gcdExtended(b, a%b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + return d; + +} + + + +int main() + +{ + + int a, n, x, y; + + fin >> a >> n; + + int d = gcdExtended(a, n, x, y); + + if(1 % d == 0) + + { + + x = x * (1 / d); + + if(x < 0) + + fout << x + n; + + else + + fout << x; + + } + + else + + fout << "0\n"; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/1374bfe0-9666-4564-9a0a-7d53c73a0294.cpp b/invmod/extended-euclid-algo/1374bfe0-9666-4564-9a0a-7d53c73a0294.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aae4d772a275055078f3b399107e178c42accbd --- /dev/null +++ b/invmod/extended-euclid-algo/1374bfe0-9666-4564-9a0a-7d53c73a0294.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int x,y; + +int a,n; + + + +void euclid_extins(int a, int b) { + + if (b!=0) { + + euclid_extins(b,a%b); + + } + + if (b==0) { + + x = 1; + + y = 0; + + } + + else { + + int aux = x; + + x = y; + + y = aux - (a/b)*y; + + } + +} + + + +int main() + +{ + + f >> a >> n; + + euclid_extins(a,n); + + if (x<1) { + + int need = x-1; + + need = -need; + + if (need%n==0) { + + x += need; + + } + + else { + + need /= n; + + need++; + + x += need*n; + + } + + } + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/13a6ddd0-479c-4e36-9ee6-ce6bd0f54d4e.cpp b/invmod/extended-euclid-algo/13a6ddd0-479c-4e36-9ee6-ce6bd0f54d4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37d2e264dcf8359cd12c21c908a80778d3d47f1f --- /dev/null +++ b/invmod/extended-euclid-algo/13a6ddd0-479c-4e36-9ee6-ce6bd0f54d4e.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + long long x=0,y; + + int a,n; + + f>>a>>n; + + gcd(x,y,a,n); + + if(x<=0) + + x=n+x%n; + + g< +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +void inv_mod(long long &inv, long long &ins, int a, int b) { + + if(b == 0) { + + inv = 1; + + ins = 0; + + } else { + + inv_mod(inv, ins, b, a % b); + + swap(inv, ins); + + + + ins -= inv * (a / b); + + } + +} + + + +int main() + +{ + + int a, n; + + long long inv, ins; + + + + in >> a >> n; + + + + inv_mod(inv, ins, a, n); + + + + inv = (inv + n) % n; + + + + out << inv << '\n'; + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/1406937b-0269-40bc-bf9e-032e2826dabd.cpp b/invmod/extended-euclid-algo/1406937b-0269-40bc-bf9e-032e2826dabd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c5b06030c7a23c13224d56d1a3e9e57c545e038 --- /dev/null +++ b/invmod/extended-euclid-algo/1406937b-0269-40bc-bf9e-032e2826dabd.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n,yy0,yy1,r,x,c,aux; + + + +int main() + +{ + + f>>a>>n; + + yy0=0; yy1=1; + + aux=n; + + while (a!=0) + + { + + r=n%a; + + c=n/a; + + n=a; + + a=r; + + int y = yy0-c*yy1; + + yy0=yy1; + + yy1=y; + + } + + while (yy0<0) yy0+=aux; + + + + g< +using namespace std; +long long w, z; +int p, n; +void cmmdc(int a,int b,long long &x,long long &y) +{ + if(b == 0){ + x = 1; + y = 0; + }else{ + long long x0, y0; + cmmdc(b, a%b, x0, y0); + x = y0; + y = x0 - a/b*y0; + } +} +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d%d", &p, &n); + cmmdc(p, n, w, z); + + if (w <= 0) + w = n + w%n; + + printf("%lld", w); + return 0; +} diff --git a/invmod/extended-euclid-algo/14c73d0f-14ad-496a-947c-a260acb05eb7.cpp b/invmod/extended-euclid-algo/14c73d0f-14ad-496a-947c-a260acb05eb7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..849c6ddf71557e87565e7d16acfc037712b38f23 --- /dev/null +++ b/invmod/extended-euclid-algo/14c73d0f-14ad-496a-947c-a260acb05eb7.cpp @@ -0,0 +1,79 @@ +#include + + + + +using namespace std; + + + + + +void gcdExtended(int a, int b, long long *x, long long *y) { + + + + if (a == 0) { + + *x = 0; + + *y = 1; + + return; + + } + + + + long long x1, y1; + + gcdExtended(b%a, a, &x1, &y1); + + + + + + *x = y1 - (b/a) * x1; + + *y = x1; + +} + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + long long x, y; + + int a, b; + + + + scanf("%d %d", &a, &b); + gcdExtended(a, b, &x, &y); + + + + + + + + if (x <= 0) + + x = b + x % b; + + + + printf("%d\n", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/1658ecf5-2c64-47c8-9d7e-468e50c834de.cpp b/invmod/extended-euclid-algo/1658ecf5-2c64-47c8-9d7e-468e50c834de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..917138c17a0eafa65615ed1ff38f932a2663fa8d --- /dev/null +++ b/invmod/extended-euclid-algo/1658ecf5-2c64-47c8-9d7e-468e50c834de.cpp @@ -0,0 +1,89 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +struct valori + +{ + + int d; + + int x0; + + int y0; + +}; + + + +int a, x, b, y, c; + + + +valori f(int a, int b) + +{ + + if (b==0) + + { + + return {a,1,0}; + + } + + valori d=f(b, a%b); + + valori fun; + + fun.x0=d.y0; + + fun.y0=d.x0-(a/b)*d.y0; + + fun.d=d.d; + + return fun; + +} + + + + + + + +int main() + +{ + + + + fin >> a >> b; + + valori r=f(a,b); + + int af=r.x0; + + if (af>b) + + af%=b; + + else while (af<0) + + af+=b; + + fout << af; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/168d3031-ca9c-4c71-a5a8-e709524a8de2.cpp b/invmod/extended-euclid-algo/168d3031-ca9c-4c71-a5a8-e709524a8de2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a163e6e4e4a065d3438923c69691f2a4727ecfd --- /dev/null +++ b/invmod/extended-euclid-algo/168d3031-ca9c-4c71-a5a8-e709524a8de2.cpp @@ -0,0 +1,70 @@ +#include + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/170954a0-314d-4869-a8db-c1f97fbf8263.cpp b/invmod/extended-euclid-algo/170954a0-314d-4869-a8db-c1f97fbf8263.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1379d955ef9fa48e2df2a6bb2804a0d680d30b4 --- /dev/null +++ b/invmod/extended-euclid-algo/170954a0-314d-4869-a8db-c1f97fbf8263.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a, n; + +int x, y; + + + +void ee(int a, int n, int &x, int &y) { + + if(!n) { + + x=1; + + y=0; + + return; + + } + + int xp, yp; + + ee(n, a%n, xp, yp); + + x=yp; + + y=xp-(a/n)*yp; + +} + + + +int main() { + + f>>a>>n; + + + + ee(a, n, x, y); + + + + while(x<0) + + x+=n; + + + + g< +using namespace std; + +ifstream f("inversmodular.in"); ofstream g("inversmodular.out"); + + + +int A, N; + +void gcd(long long &x, long long &y, int a, int b) + +{ if (!b){ x = 1; y = 0; } + + else + + { gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int main() + +{ long long invers=0,ins; + + f>>A>>N; + + gcd(invers,ins,A,N); + + if (invers <= 0) invers = N + invers % N; + + g< +#include + + + + +using namespace std; + + + + + +char const in_file[] = "inversmodular.in"; + +char const out_file[] = "inversmodular.out"; + + + + + +ifstream Read(in_file); + +ofstream Write(out_file); + + + + + +void GCD( + + uint32_t const first, + + uint32_t const second, + + int64_t &x, + + int64_t &y + +) { + + if (second == 0) { + + x = 1; + + y = 0; + + return; + + } + + + + int64_t x0; + + int64_t y0; + + GCD(second, first % second, x0, y0); + + + + x = y0; + + y = x0 - (first / second) * y0; + +} + + + + + +int main() { + + uint32_t a; + + uint32_t n; + + + + Read >> a; + + Read >> n; + + + + int64_t x; + + int64_t y; + + GCD(a, n, x, y); + + + + x = x % n + (x < 0 ? n : 0); + + + + Write << x; + + + + Read.close(); + + Write.close(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/1b0011b4-d8de-4dc0-9917-09d766622f6b.cpp b/invmod/extended-euclid-algo/1b0011b4-d8de-4dc0-9917-09d766622f6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5995972e07fcdcc18e6a953d0ad6e202e68f75f --- /dev/null +++ b/invmod/extended-euclid-algo/1b0011b4-d8de-4dc0-9917-09d766622f6b.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; + +void invMod(int a, int b, int &x, int &y) { + if (!b) + x = 1, y = 0; + else { + int x0, y0; + invMod(b, a % b, x0, y0); + x = y0; + y = x0 - (a / b) * y0; + } +} + +int main() { + ifstream cin ("inversmodular.in"); + ofstream cout ("inversmodular.out"); + int a, p; + cin >> a >> p; + int x, y; + invMod(a, p, x, y); + if (x <= 0) + x += p; + cout << x; + return 0; +} diff --git a/invmod/extended-euclid-algo/1c65dc8d-d6ff-46eb-b456-6dce0383de74.cpp b/invmod/extended-euclid-algo/1c65dc8d-d6ff-46eb-b456-6dce0383de74.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ce67b043b25900906369e53c13a469618a320f7 --- /dev/null +++ b/invmod/extended-euclid-algo/1c65dc8d-d6ff-46eb-b456-6dce0383de74.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a, n, x, y; + +pair p; + + + +pair euclid(int x, int y){ + + if(y==0) + + return {1, 0}; + + p=euclid(y,x%y); + + return {p.second, p.first - (x/y)*p.second }; + +} + + + +int main() + +{ + + f>>a>>n; + + + + p=euclid(a,n); + + + + while(p.first<0) + + p.first+=n; + + + + g< +#include + + +using namespace std; + + + +pair euclid(int x, int y) + +{ + + if(y==0) + + return {1, 0}; + + auto p=euclid(y, x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + + + +int x, n; + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + scanf("%d %d", &x, &n); + + auto invers=euclid(x, n); + + int y=invers.first; + + while(y<0) + + y+=n; + + printf("%d", y); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/1de17b33-2229-4add-ab38-ecacc6d8bfef.cpp b/invmod/extended-euclid-algo/1de17b33-2229-4add-ab38-ecacc6d8bfef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edcdec69594b1d88aafaa5345eb817d4a55ff633 --- /dev/null +++ b/invmod/extended-euclid-algo/1de17b33-2229-4add-ab38-ecacc6d8bfef.cpp @@ -0,0 +1,91 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int euclid (int a, int b, int &x, int &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + + + else + + { + + int x1, y1; + + + + euclid(b, a%b, x1, y1); + + x=y1; + + y=x1-a/b*y1; + + } + +} + + + + + +int main() + +{ + + int a, n, x, y; + + + + fin>>a>>n; + + + + euclid (a, n, x, y); + + + + while(x>0) + + { + + x=x-n; + + } + + + + while(x<0) + + { + + x=x+n; + + } + + + + fout< +int a,n,x,y; +inline void E(int &x,int &y,int a,int b) +{ + int z; + if(!b) + x=1,y=0; + else + E(x,y,b,a%b),z=x,x=y,y=z-y*(a/b); +} +int main() +{ + freopen("inversmodular.in","r",stdin),freopen("inversmodular.out","w",stdout),scanf("%d%d",&a,&n),E(x,y,a,n),printf("%d",x<=0?(n+x%n):x); +} diff --git a/invmod/extended-euclid-algo/1ebb7fbb-268a-4bbd-877e-62e156dc6898.cpp b/invmod/extended-euclid-algo/1ebb7fbb-268a-4bbd-877e-62e156dc6898.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf888597fbde4930242503679f955d71eaf16c93 --- /dev/null +++ b/invmod/extended-euclid-algo/1ebb7fbb-268a-4bbd-877e-62e156dc6898.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +int a, n, k, L; + + + +void euclid(int a, int n, int &k, int &L) + +{ + + if (n==0){ + + k=0; + + L=1; + + return; + + } + + int k1, l1; + + euclid(n, a%n, k1, l1); + + L = k1; + + k = l1 - (a/n) * k1; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin>>a>>n; + + euclid(a, n, k, L); + + while (L < 0){ + + L+=n; + + } + + fout< + + + + + + +using namespace std; + + + +int a , n; + + + +int euclid(int A , int B , int &X , int &Y) + +{ + + if(B == 0) + + { + + X = 1; + + Y = 0; + + return A; + + } + + + + int X0 , Y0; + + int D = euclid(B , A % B , X0 , Y0); + + + + X = Y0; + + Y = X0 - A / B * Y0; + + + + return D; + +} + + + +int invmod() + +{ + + int X , Y; + + euclid(a , n , X , Y); + + + + if(X < 0) + + return X + n; + + + + return X; + +} + + + +signed main() + +{ + + + + ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) , freopen("inversmodular.in" , "r" , stdin) , freopen("inversmodular.out" , "w" , stdout); + + + + + + cin >> a >> n; + + cout << invmod(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/20f98de0-3a2c-46db-8adf-2446fe78f497.cpp b/invmod/extended-euclid-algo/20f98de0-3a2c-46db-8adf-2446fe78f497.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f67d21a36e622e51f86649e65eed8c9e061209c8 --- /dev/null +++ b/invmod/extended-euclid-algo/20f98de0-3a2c-46db-8adf-2446fe78f497.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +typedef long long ll; + +ll eucl(ll A,ll B, ll &X, ll &Y) + +{ + + if(B==0) + + { + + X=1; + + Y=0; + + return A; + + } + + ll X0,Y0,D; + + D=eucl(B,A%B,X0,Y0); + + X=Y0; + + Y=X0-(A/B)*Y0; + + return D; + +} + +int main() + +{ + + ll x,y,A,N; + + ll inv; + + fin>>A>>N; + + inv=eucl(A,N,x,y); + + while(x<1) x+=N; + + while(x>N-1) x-=N; + + fout< +#include +#include +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + + + +long long int egcd(long long int a, long long int b, long long int *x, long long int *y) +{ + if(b ==0) + { + *x=1; + *y=0; + return a; + } + long long int x0,y0,d; + d = egcd(b,a%b,&x0,&y0); + *x = y0; + *y = x0 - (a/b)*y0; + + return d; + +} + +int main ( ) +{ + long long int a,n,x,y,d; + + in>>a>>n; + d = egcd(a,n,&x,&y); + + while(x<0) x+=n; + out< +using namespace std; + +ifstream cin ("inversmodular.in"); + +ofstream cout("inversmodular.out"); + +int a, b, c, d, x, y; + +void gcd(int a, int b, int &x, int &y) { + + if (!b) { + + x = 1; + + y = 0; + + } + + else { + + int x0, y0; + + gcd(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int main() { + + int a, n; + + cin >> a >> n; + + int inv, ins; + + gcd(a, n, inv, ins); + + if (inv <= 0) inv = n + inv % n; + + cout << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/22e95005-e455-4cd2-a206-25ef3793d112.cpp b/invmod/extended-euclid-algo/22e95005-e455-4cd2-a206-25ef3793d112.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2d8947527ecefd4db5a8329b20826ba25002e3c --- /dev/null +++ b/invmod/extended-euclid-algo/22e95005-e455-4cd2-a206-25ef3793d112.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +void gcd(int &x, int &y, int a, int b) { + + if(b == 0) x = 1, y = 0; + + else{ + + gcd(x, y, b, a % b); + + int aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() { + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int inv, ins, A, N; + + scanf("%d%d", &A, &N); + + inv = 0; + + gcd(inv, ins, A, N); + + if(inv <= 0) inv = N + inv % N; + + printf("%d\n", inv); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/23928f84-bfd6-492a-820a-5926fa067d43.cpp b/invmod/extended-euclid-algo/23928f84-bfd6-492a-820a-5926fa067d43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f19eb52bcf210c103687ffdc09de98b4bb3d25e6 --- /dev/null +++ b/invmod/extended-euclid-algo/23928f84-bfd6-492a-820a-5926fa067d43.cpp @@ -0,0 +1,47 @@ +#include +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a,mod; + +void euclid(int a,int b,int &x,int &y){ + +if(b==0){ + + x=1; + + y=0; + + return ; + +} + +int x0,y0; + +euclid(b,a%b,x0,y0); + +x=y0; + +y=x0-y0*(a/b); + +return; + +} + +int main() + +{ + + fin>>a>>mod; + + int x,y; + + euclid(a,mod,x,y); + + fout<<(x+mod)%mod; + +} diff --git a/invmod/extended-euclid-algo/239d3850-b00c-4696-a910-bc8627cf012c.cpp b/invmod/extended-euclid-algo/239d3850-b00c-4696-a910-bc8627cf012c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d634d13a112186512474702bfb549e785ac2053 --- /dev/null +++ b/invmod/extended-euclid-algo/239d3850-b00c-4696-a910-bc8627cf012c.cpp @@ -0,0 +1,61 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +struct solu{ + + int k, l, d; + +}; + + + +solu nepitagora(int a, int b){ + + if(b == 0){ + + return {1, 0, a}; + + } + + solu idk = nepitagora(b, a%b); + + solu kdi = {idk.l, idk.k-(a/b)*idk.l, idk.d}; + + return kdi; + +} + + + +int main() + +{ + + int a, b; + + fin >> a >> b; + + solu ba = nepitagora(a, b); + + while(ba.k < 0){ + + ba.k += b; + + } + + fout << ba.k; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/24cb83bc-79b5-41b6-bc5d-3cf2b99bb959.cpp b/invmod/extended-euclid-algo/24cb83bc-79b5-41b6-bc5d-3cf2b99bb959.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdd6dccb557b104feefad543ff81eb869e539a8e --- /dev/null +++ b/invmod/extended-euclid-algo/24cb83bc-79b5-41b6-bc5d-3cf2b99bb959.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int gcd(int a, int b, int &x, int &y) + +{ + + if(b == 0) + + x = 1, y = 0; + + else + + { + + gcd(b, a % b, x, y); + + int aux = y; + + y = x - (a / b) * y; + + x = aux; + + } + +} + + + +int a, n, inv, ins; + + + +int main() + +{ + + f >> a >> n; + + gcd(a, n, inv, ins); + + if(inv <= 0) + + inv = n + inv % n; + + g << inv << "\n"; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/28f31c4c-36de-448d-a6ab-4b7a505a7648.cpp b/invmod/extended-euclid-algo/28f31c4c-36de-448d-a6ab-4b7a505a7648.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4290c7e96c51da81b118522ab0f8dbf3c8a88782 --- /dev/null +++ b/invmod/extended-euclid-algo/28f31c4c-36de-448d-a6ab-4b7a505a7648.cpp @@ -0,0 +1,57 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +typedef long long lint; + +typedef pair gcdres; + +gcdres gcd(lint x, lint y){ + + if(y == 0){ + + return {1, 0}; + + }else{ + + gcdres res = gcd(y, x%y); + + return {res.second, res.first - res.second*(x/y)}; + + } + +} + + + +lint invmod(lint a, lint n){ + + return (n*100+gcd(a, n).first) % n; + +} + + + +lint a, n; + + + +int main(){ + + fin >> a >> n; + + fout << invmod(a, n); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/2a351a18-fad8-4dc6-a7c8-9553a2603823.cpp b/invmod/extended-euclid-algo/2a351a18-fad8-4dc6-a7c8-9553a2603823.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d40d79b4ff8bd0ec4337efad496c01c056a08d53 --- /dev/null +++ b/invmod/extended-euclid-algo/2a351a18-fad8-4dc6-a7c8-9553a2603823.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; + +void find(long long a, long long b, long long *x, long long *y){ + if(b == 0){ + *x = 1; + *y = 0; + + } else { + long long x0, y0; + find(b, a%b, &x0, &y0); + *x = y0; + *y = x0 - (a/b)*y0; + } + +} + +int main(){ + ifstream fin("inversmodular.in"); + ofstream fout("inversmodular.out"); + long long A, N; + fin >> A >> N; + long long x = 0, y; + find(A, N, &x, &y); + if(x<=0){ + x = N + x%N; + } + + fout << x <<'\n'; + return 0; +} diff --git a/invmod/extended-euclid-algo/2ad5f03c-4ee0-4e1c-b843-08da88301854.cpp b/invmod/extended-euclid-algo/2ad5f03c-4ee0-4e1c-b843-08da88301854.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e0d9bd4fe5ffc0a714b7729fe61030e0e953078 --- /dev/null +++ b/invmod/extended-euclid-algo/2ad5f03c-4ee0-4e1c-b843-08da88301854.cpp @@ -0,0 +1,80 @@ +#include + + +void euclid_eXt(int a, int b, int c, int &x, int &y) + +{ + + + + int q[100], x0, y0, r, p=0; + + + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) + + { + + x=y=0; + + return; + + } + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + + + +int main() + +{ + + FILE *f =fopen("inversmodular.in", "r"); + + FILE *g =fopen("inversmodular.out", "w"); + + int a, b,x, y; + + fscanf(f, "%d%d", &a, &b); + + euclid_eXt(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/2bc308b6-04d4-40a5-9e9d-be74fb644e4d.cpp b/invmod/extended-euclid-algo/2bc308b6-04d4-40a5-9e9d-be74fb644e4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1ded06983b20c63ae0c83b2a67d69d447ae350b --- /dev/null +++ b/invmod/extended-euclid-algo/2bc308b6-04d4-40a5-9e9d-be74fb644e4d.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int a,n; + +void Euclid(int a,int b,long long& x,long long& y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + long long x0,y0; + + Euclid(b,a%b,x0,y0); + + x=y0; + + y=(x0-a/b*y0); + + } + +} + +int main() +{ + + long long x,y; + + f>>a>>n; + + f.close(); + + Euclid(a,n,x,y); + + x=((x%n)+n)%n; + + g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int a,n; + + + +void Read() + +{ + + f>>a>>n; + + f.close(); + +} + + + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + { + + x = 1; + + y = 0; + + } + + else + + { + + gcd(x, y, b,a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + Read(); + + gcd(inv, ins, a, n); + + + + while(inv <= 0) + + inv = n + inv % n; + + g< + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + +typedef long long ll; + + + +ll gcd(ll &x, ll &y, ll a, ll b) + +{ + + if(!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + ll aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + ll a, n, inv = 0, ins; + + + + cin >> a >> n; + + + + gcd(inv, ins, a, n); + + + + if(inv <= 0) + + inv = n + inv % n; + + + + cout << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/2f02ccbf-5937-486b-b73e-b16eed4f1123.cpp b/invmod/extended-euclid-algo/2f02ccbf-5937-486b-b73e-b16eed4f1123.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7827a039dcf542368e4f16ff7ddb46ad98009250 --- /dev/null +++ b/invmod/extended-euclid-algo/2f02ccbf-5937-486b-b73e-b16eed4f1123.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + +ifstream r("inversmodular.in"); + +ofstream w("inversmodular.out"); + +int A, N; + +void euclid(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + euclid(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int main() + +{ + + long long inv=0, ins; + + r>>A>>N; + + euclid(inv, ins, A, N); + + if (inv <= 0) + + inv = N + inv % N; + + w< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + +long long inv,ins; + + + +void gcd(int u,int n) + +{ + + if(n==0) inv=1,ins=0; + + else + + { + + gcd(n,u%n); + + long long aux=inv; + + inv=ins; + + ins=aux-ins*(u/n); + + } + +} + + + +int main() + +{ + + + + f>>a>>n; + + inv=0; + + gcd(a,n); + + if(inv<=0) inv=n+inv%n; + + g< +using namespace std; + +int x, X1, X2, cat, rest, rest1, rest2, modulo; + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> rest1 >> rest2; + + modulo = rest2; + + X1 = 1; + + X2 = 0; + + while(rest1 % rest2 != 0) + + { + + cat = rest1 / rest2; + + rest = rest1 % rest2; + + rest1 = rest2; + + rest2 = rest; + + x = X1 - X2 * cat; + + X1 = X2; + + X2 = x; + + } + + while(x < 0)x = x + modulo; + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/30b68b1a-298d-4f16-bc0c-c0f17a4ab50b.cpp b/invmod/extended-euclid-algo/30b68b1a-298d-4f16-bc0c-c0f17a4ab50b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51c7460f87d658983d7ed7c6f3c10b4ff0cc6d5e --- /dev/null +++ b/invmod/extended-euclid-algo/30b68b1a-298d-4f16-bc0c-c0f17a4ab50b.cpp @@ -0,0 +1,74 @@ +#include + + +std::ifstream fin("inversmodular.in"); + +std::ofstream fout("inversmodular.out"); + + + +typedef long long ll; + + + +ll gcd(ll a, ll b, ll &x, ll &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + return a; + + } + + else + + { + + ll x0,y0,d; + + d = gcd(b,a%b,x0,y0); + + x = y0; + + y = x0 - (a/b)*y0; + + } + +} + + + +int main() + +{ + + ll a, n; + + ll inv = 0, ins; + + fin >> a >> n; + + gcd(a,n,inv,ins); + + + + if(inv < 0) + + { + + inv = n + inv % n; + + } + + + + fout << inv; + +} diff --git a/invmod/extended-euclid-algo/31d1a286-23cb-4209-a596-27fb27429678.cpp b/invmod/extended-euclid-algo/31d1a286-23cb-4209-a596-27fb27429678.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d4460a522b6bbfcede8deb9f4dd9bf51360d61e --- /dev/null +++ b/invmod/extended-euclid-algo/31d1a286-23cb-4209-a596-27fb27429678.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long x,y,a,n; + +void euclid(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + { + + x=1; + + y=0; + + } + + else + + { + + euclid(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{f>>a>>n; + +euclid(x,y,a,n); + +if(x<=0) + + x=n+x%n; + +g< +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void euclid(int a, int b, int* x, int* y) + +{ + + if (b == 0) + + { + + *x = 1; + + *y = 0; + + } + + else + + { + + int x0, y0; + + euclid(b, a % b, &x0, &y0); + + * x = y0; + + *y = x0 - a / b * y0; + + } + + + + + + + +} + + + + + +int main() + +{ + + int a, b, d, x, y; + + + + f >> a >> b; + + d = 0; + + euclid(a, b, &x, &y); + + if (x < 0) + + { + + int aux = -x/b; + + x = x + aux * b; + + if (x < 0) x += b; + + } + + g << x; + return 0; + + + +} diff --git a/invmod/extended-euclid-algo/33670987-4f6b-4f28-9ef4-62233b603b79.cpp b/invmod/extended-euclid-algo/33670987-4f6b-4f28-9ef4-62233b603b79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e05c56855bdcc1142903d1b75c4b7b871825ea1 --- /dev/null +++ b/invmod/extended-euclid-algo/33670987-4f6b-4f28-9ef4-62233b603b79.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +long long a, b, x, y; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void gcd_extended(long long a, long long b, long long &x, long long &y) { + + if (!b) { + + x = 1; + + y = 0; + + } + + else { + + long long x0, y0; + + gcd_extended(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + + + +int main() { + + f >> a >> b; + + gcd_extended(a, b, x, y); + + while (x < 0) + + x += b; + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/33cc1d0a-1793-4e44-ace0-269cd4c0e541.cpp b/invmod/extended-euclid-algo/33cc1d0a-1793-4e44-ace0-269cd4c0e541.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d2aed95debc88992331c9ccd2d540a6c1e553e3 --- /dev/null +++ b/invmod/extended-euclid-algo/33cc1d0a-1793-4e44-ace0-269cd4c0e541.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +void gcd(long long &x,long long &y,int a,int b){ + + if(b==0){ + + x=1; + y=0; + return; + } + gcd(x,y,b,a%b); + long long aux = x; + x = y; + y = aux - y*(a/b); +} + +int main() +{ + int a,n; + long long inv = 0,ins; + ifstream f("inversmodular.in"); + ofstream g("inversmodular.out"); + f>>a>>n; + gcd(inv,ins,a,n); + if(inv<=0) + inv = n + inv%n; + g< +#include +#include +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + + + +long long int egcd(long long int a, long long int b, long long int *x, long long int *y) +{ + if(b ==0) + { + *x=1; + *y=0; + return a; + } + long long int x0,y0,d; + d = egcd(b,a%b,&x0,&y0); + *x = y0; + *y = x0 - (a/b)*y0; + + return d; + +} + +int main ( ) +{ + long long int a,n,x,y,d; + + in>>a>>n; + d = egcd(a,n,&x,&y); + + while(x<0) x+=n; + out< +#include + +using namespace std; +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); +typedef long long ll; +void euclid (ll a, ll b, ll &x, ll &y) +{ + if(b==0) + { + x=1; + y=0; + } + else + { + euclid(b, a%b, x, y); + ll aux=x; + x=y; + y=aux-(a/b)*y; + } +} +int main() +{ + int A, N; + fin>>A>>N; + ll sol1{}, sol2{}; + + euclid(A, N, sol1, sol2); + + if(sol1<=0) + sol1 = N + sol1%N; + + fout< + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + + + +void euclid_e( int a, int b, int& x, int& y, int& d ) { + + if ( b == 0 ) + + { + + d = a; + + x = 1; + + y = 0; + + return; + + } + + int xx, yy, q = a / b; + + euclid_e( b, a % b, xx, yy, d ); + + x = yy; + + y = xx - yy * q; + +} + + + +int main() + +{ + + int a, n, x, y, d; + + in >> a >> n; + + euclid_e(a, n, x, y, d); + + x %= n; + + if (x < 0) + + { + + x += n; + + } + + out << x; + + in.close(); + + out.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/366d153b-212e-4013-822a-4ac5760aee69.cpp b/invmod/extended-euclid-algo/366d153b-212e-4013-822a-4ac5760aee69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6d06eb402523563539587581acc27fd1ff66a2a --- /dev/null +++ b/invmod/extended-euclid-algo/366d153b-212e-4013-822a-4ac5760aee69.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void euclid( long long a, long long b, long long &d, long long &x, long long &y) + +{ + + if(!b) + + { + + d = a; + + x = 1; + + y = 0; + + } + + else + + { + + long long x_u, y_u; + + euclid(b,a%b,d, x_u, y_u); + + x=y_u; + + y=x_u- a/b*y_u; + + } + +} + +int main() + +{ + + long long n; + + + + long long a, b, c; + + fin >> a >> b; + + long long x,y,d; + + euclid(a, b, d, x, y); + + if(x<=0)x=b+x%b; + + fout< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void inv(long long a, long long b, long long &x, long long &y) + +{ + + if(!b) + + x=1,y=0; + + else + + { + + inv(b,a%b,x,y); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + long long a,mod,x,y; + + f>>a>>mod; + + inv(a,mod,x,y); + + while(x<=0) + + x=mod+x%mod; + + g< +#include + + +using namespace std; + +ifstream r("inversmodular.in"); + +ofstream w("inversmodular.out"); + +int A, N; + +void euclid(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + { + + x=1, y=0; + + } + + else + + { + + euclid(x, y, b, a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + long long inv=0, ins; + + r>>A>>N; + + euclid(inv, ins, A, N); + + if (inv<=0) + + { + + inv = N + inv % N; + + } + + w< + + +using namespace std; + + + +long long power(long long n, long long p, long long mod) { + + long long aux = n; + + n = 1; + + for(long long i=1; i<=p; i = i<<1) { + + if (p & i) + + n = aux * n % mod; + + aux = aux * aux % mod; + + } + + return n; + +} + + + + + +long long phi(int n) { + + long long res = n; + + + + for(long long i=2; i*i <= n; ++i) + + if (n%i == 0) { + + while (n%i == 0) + + n = n / i; + + res = res / i * (i-1); + + } + + if (n != 1) + + res = res / n * (n-1); + + + + return res; + +} + + + + + +int main() { + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + long long a, n; + + scanf("%lld%lld", &a, &n); + + + + long long res = phi(n) - 1; + + + + printf("%lld\n", power(a, res, n)); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/38fc38d5-14e6-4ad2-a3b5-e769546a1afb.cpp b/invmod/extended-euclid-algo/38fc38d5-14e6-4ad2-a3b5-e769546a1afb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c460f285874a81d4f90514a8d910bcfc9e99401 --- /dev/null +++ b/invmod/extended-euclid-algo/38fc38d5-14e6-4ad2-a3b5-e769546a1afb.cpp @@ -0,0 +1,58 @@ +#include + + +int inv_mod(int x,int b) + +{ + + int r,q[100],nr=0,bz=b; + + while(b) + + { + + q[++nr]=x/b; + + r=x%b; + + x=b;b=r; + + } + + int a=1,a1,b1; + + b=0; + + while(nr) + + { + + a1=b; + + b1=a-q[nr--]*b; + + a=a1;b=b1; + + } + + return ((long long)bz+a%bz)%bz; + +} + + + +int main() + +{ + + int a,b;FILE *f=fopen("inversmodular.in","r"); + + fscanf(f,"%d%d",&a,&b); + + f=fopen("inversmodular.out","w"); + + fprintf(f,"%d",inv_mod(a,b)); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/39b79688-bb7b-4efa-9240-f8c0e5e6dfa2.cpp b/invmod/extended-euclid-algo/39b79688-bb7b-4efa-9240-f8c0e5e6dfa2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab2691dc5671d89eab06744ea6427475f275eca5 --- /dev/null +++ b/invmod/extended-euclid-algo/39b79688-bb7b-4efa-9240-f8c0e5e6dfa2.cpp @@ -0,0 +1,40 @@ +#include + +int A, N; + +void extendedEuclid(int A, int B, int& resultX, int& resultY) { + if (B == 0) { + resultX = 1; + resultY = 0; + return; + } + + int prevResultX, prevResultY; + extendedEuclid(B, A % B, prevResultX, prevResultY); + + resultX = prevResultY; + resultY = prevResultX - (A / B) * prevResultY; +} + +inline int positiveMod(int x, int MOD) { + x %= MOD; + if (x < 0) { + x += MOD; + } + + return x; +} + +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d%d", &A, &N); + + int x, y; + extendedEuclid(A, N, x, y); + + printf("%d\n", positiveMod(x, N)); + + return 0; +} diff --git a/invmod/extended-euclid-algo/3aa3f80e-6144-4a55-83da-4f48712e7f82.cpp b/invmod/extended-euclid-algo/3aa3f80e-6144-4a55-83da-4f48712e7f82.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2a117e78418d8f8393044f51323491383037ca3 --- /dev/null +++ b/invmod/extended-euclid-algo/3aa3f80e-6144-4a55-83da-4f48712e7f82.cpp @@ -0,0 +1,65 @@ +#include +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void euclid(int a, int b, int &c, int &x, int &y) + +{ + + if(b == 0){ + + c = a; + + x = 1; + + y = 0; + + } + + else{ + + int x0, y0; + + euclid(b, a%b, c, x0, y0); + + x = y0; + + y = x0 - (a/b)*y0; + + } + +} + + + +int main() + +{ + + int a, n, c, x, y; + + fin >> a >> n; + + euclid(a, n, c, x, y); + + + + while(x < 0) x += n; + + + + fout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/3c8e5a73-fac8-44fd-a3d1-c33c9d5aeba1.cpp b/invmod/extended-euclid-algo/3c8e5a73-fac8-44fd-a3d1-c33c9d5aeba1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8642fd009196178156a916b705e8ca7e7598d086 --- /dev/null +++ b/invmod/extended-euclid-algo/3c8e5a73-fac8-44fd-a3d1-c33c9d5aeba1.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int a,n,d; + +pair eucl_extins(int x,int y) + +{ + + if(y==0) + + { + + d=x; + + return {1,0}; + + } + + auto p=eucl_extins(y,x%y); + + return {p.second,p.first-(x/y)*p.second}; + + + +} + +void citire() + +{ + + f>>a>>n; + + auto p=eucl_extins(a,n); + + if(p.first<0) + + g< +int A, N; + + + + + + + +void gcd(long long &x, long long &y, int a, int b) + + + +{ + + + + if (!b) + + + + x = 1, y = 0; + + + + else + + + + { + + + + gcd(x, y, b, a % b); + + + + long long aux = x; + + + + x = y; + + + + y = aux - y * (a / b); + + + + } + + + +} + + + + + + + +int main() + + + +{ + + + + long long inv = 0, ins; + + + + + + + + freopen("inversmodular.in", "r", stdin); + + + + freopen("inversmodular.out", "w", stdout); + + + + + + + + scanf("%d %d", &A, &N); + + + + gcd(inv, ins, A, N); + + + + + + + + if (inv <= 0) + + + + inv = N + inv % N; + + + + + + + + printf("%lld\n", inv); + + + + + + + + return 0; + + + +} diff --git a/invmod/extended-euclid-algo/3f58f71c-b19d-4c1e-8dc5-91cdbbc770fd.cpp b/invmod/extended-euclid-algo/3f58f71c-b19d-4c1e-8dc5-91cdbbc770fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b5afef42ef8ab550c23cad4532bbae121c08fce --- /dev/null +++ b/invmod/extended-euclid-algo/3f58f71c-b19d-4c1e-8dc5-91cdbbc770fd.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int A,N; + + + + + +void euclid(int a,int b,int &x,int &y) + +{ + + if(b==0) + + { + + x=1,y=1; + + } + + else + + { + + int x1,y1; + + euclid(b,a%b,x1,y1); + + x=y1; + + y=x1-a/b*y1; + + } + +} + + + +int main() + +{ + + int x,y; + + fin>>A>>N; + + euclid(A,N,x,y); + + while(x<0) x+=N; + + fout< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +long long ext_euclid(long long a, long long b, long long &x, long long &y) + +{ + + if(b == 0) { + + x = 1; + + y = 0; + + return a; + + } + + long long x0, y0; + + long long d = ext_euclid(b, a % b, x0, y0); + + x = y0; + + y = x0 - y0 * (a / b); + + return d; + +} + + + +int main() + +{ + + long long a, b, x, y; + + + + fin >> a >> b; + + ext_euclid(a, b, x, y); + + x = (x + b) % b; + + fout << x << "\n"; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/412af916-2693-43cb-bd80-a3cbe233dac2.cpp b/invmod/extended-euclid-algo/412af916-2693-43cb-bd80-a3cbe233dac2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17ec38b3e1a1500453a00f2d3984ed8f8d8ccf26 --- /dev/null +++ b/invmod/extended-euclid-algo/412af916-2693-43cb-bd80-a3cbe233dac2.cpp @@ -0,0 +1,40 @@ +#include +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +typedef long long ll; + +int a, n; +ll inv, ins; + +void inv_mod(ll &inv, ll &ins, int a, int b) { + if(b == 0) { + inv = 1; + ins = 0; + } else { + inv_mod(inv, ins, b, a % b); + swap(inv, ins); + + ins -= inv * (a / b); + } +} + +int main() +{ + in >> a >> n; + + inv_mod(inv, ins, a, n); + + inv = (inv + n) % n; + + out << inv << '\n'; + + in.close(); + out.close(); + + return 0; +} diff --git a/invmod/extended-euclid-algo/416c997a-6b9c-48b5-b161-4921e2055c95.cpp b/invmod/extended-euclid-algo/416c997a-6b9c-48b5-b161-4921e2055c95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4747045822f35ee8116d99b4602ec0f084c69c2c --- /dev/null +++ b/invmod/extended-euclid-algo/416c997a-6b9c-48b5-b161-4921e2055c95.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int b,n; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void invers(int a,int b,int &x,int &y) + +{ + + if(!b) + + { + + x = 1; + + y = 0; + + } + + else + + { + + int x0,y0; + + invers(b,a%b,x0,y0); + + { + + x = y0; + + y = x0 - a/b * y0; + + } + + } + +} + + + +int main() + +{ + + fin>>b>>n; + + int x,y; + + + + invers(b,n,x,y); + + while(x<0) + + x+=n; + + fout< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a,b,d,x,y; + +long long gcd (long long a,long long b,long long &x,long long &y) + +{ + + if (b==0) + + { + + x=1; + + y=0; + + return a; + + } + + long long x0,y0,d; + + d=gcd(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + return d; + +} + +int main() + +{ + + f>>a>>b; + + d=gcd(a,b,x,y); + + if (x<=0) + + { + + x=(x+b)%b; + + } + + g< + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + +int a,n; + + + +void invmod(long long &x,long long &y,int a,int b){ + + if (!b){ + + x=1; y=0; + + } + + else{ + + invmod(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + + + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false);; + + cin.tie(0); + + cout.tie(0); + + long long invm,aj; + + cin>>a>>n; + + invmod (invm,aj,a,n); + + if (invm<0) invm=n+invm%n; + + cout< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +typedef long long llu; + + + +void euclid(llu a,llu b,llu &x,llu &y) + +{ + + if(!b) + + { + + x=1; + + y=0; + + }else{ + + llu aux; + + euclid(b,a%b,x,y); + + aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + +} + + + +int main() + +{ + + llu a,n,x,y; + + fin>>a>>n; + + euclid(a,n,x,y); + + if(x<=0)x=n+x%n; + + fout< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long int a,n; + +long long int invmod(long long int a,long long int b,long long int &x,long long int &y) + +{ + + if (b==0) + + { + + x=1; + + y=0; + + return a; + + } + + long long int aux,d; + + d=invmod(b,a%b,x,y); + + aux=x; + + x=y; + + y=aux-(a/b)*y; + + return d; + +} + +int main() + +{ + + fin>>a>>n; + + long long int x,y,d; + + d=invmod(a,n,x,y); + + x=x%n; + + if (x<0) + + x+=n; + + fout< + + +using namespace std; + + + +void extins(int a, int b, int &d, int &x, int &y){ + + if(b == 0){ + + d = a; + + x = 1; + + y = 0; + + } + + else{ + + int x0 = 0, y0 = 0; + + extins(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - y0 * (a / b); + + } + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int n, a; + + fin >> a >> n; + + int x = 0, y = 0, d = 0; + + extins(a, n, d, x, y); + + if(x < 0) + + x = (x + n) % n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/44089b6f-73c1-4ea3-bf20-6276d2a76a81.cpp b/invmod/extended-euclid-algo/44089b6f-73c1-4ea3-bf20-6276d2a76a81.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8276220cbd84b5ecb47e4ad708dc18a0732c78d0 --- /dev/null +++ b/invmod/extended-euclid-algo/44089b6f-73c1-4ea3-bf20-6276d2a76a81.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int T,x,y,A,N; + +long long a,b,L,inv,p; + +void cmmdc(int a,int b,long long &x,long long &y) + +{ + + if(!b) + + { + + x=1; + + y=0; + + } + + else + + { + + cmmdc(b,a%b,x,y); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + fin>>A>>N; + + cmmdc(A,N,inv,p); + + if(inv<=0) + + inv=N+inv%N; + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void EE(int a, int b, int &d, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + d = a; + + } + + else + + { + + int x0, y0; + + EE(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + + + +int inversModular(int A, int N) + +{ + + int X, Y, d; + + EE(A, N, d, X, Y); + + X %= N; + + if(X < 0) + + X += N; + + return X; + +} + + + +int main() + +{ + + int A, N; + + f >> A >> N; + + g << inversModular(A, N); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/462f5f41-39d9-4ad2-9c8f-617fde0c98bb.cpp b/invmod/extended-euclid-algo/462f5f41-39d9-4ad2-9c8f-617fde0c98bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..daad3a3194d4ba1fb42e0a071d386c3c0bf7084f --- /dev/null +++ b/invmod/extended-euclid-algo/462f5f41-39d9-4ad2-9c8f-617fde0c98bb.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long x,y,a,n; + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + f>>a>>n; + + gcd(x,y,a,n); + + if(x<=0) + + x=n+x%n; + + g< +#include + + + + + + +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +void invers_modular(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + invers_modular(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int a,n; + +int main() + +{ + + long long inv=0,ins; + + in>>a>>n; + + invers_modular(inv,ins,a,n); + + if(inv <= 0) + + inv=n+inv%n; + + out< + + + +using namespace std; + +void cmmdc(long long &x, long long &y, int a, int b) +{ + if (!b) + { + x = 1; + y = 0; + } + else + { + cmmdc(x, y, b, a % b); + long long aux = x; + x = y; + y = aux - y * (a / b); + } +} + +int a, n, x, y; +long long inv, ins; +int main() +{ + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + scanf("%d %d", &a, &n); + cmmdc(inv, ins, a, n); + + if (inv <= 0) + { + inv = n + inv % n; + } + + printf("%d", inv); + + fclose(stdin); + fclose(stdout); + return 0; +} diff --git a/invmod/extended-euclid-algo/4781b4b1-e461-454d-bcd0-7164cf2db06c.cpp b/invmod/extended-euclid-algo/4781b4b1-e461-454d-bcd0-7164cf2db06c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16e4b9488486094934b1b65921294c30fae7596c --- /dev/null +++ b/invmod/extended-euclid-algo/4781b4b1-e461-454d-bcd0-7164cf2db06c.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +typedef long long ull; + +ull mod,a,x,y,d; + + + +int euclid_e(ull a, ull b, ull &x, ull &y, ull &d){ + + if(b==0){ + + x=1; + + d=a; + + y=0; + + } + + else{ + + ull x0, y0; + + euclid_e(b,a%b,x0,y0,d); + + x=y0; + + y=x0-a/b*y0; + + } + +} + + + +int main() + +{ + + in>>a>>mod; + + euclid_e(mod,a,x,y,d); + + while(y<1) y+=mod; + + out< +#include + + +using namespace std; + + + +void euclid(int a, int b, int c, int &x, int &y) + +{ + + + + int q[50],r,x0,y0,pas=0; + + while(b) + + { + + r=a%b; + + q[++pas]=a/b; + + a=b; + + b=r; + + } + + if(c%a) + + { + + x=y=0; + + return; + + } + + x=x0=c/a; + + y=y0=0; + + while(pas) + + { + + x=y0; + + y=x0-q[pas--]*y0; + + x0=x; + + y0=y; + + } + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + + +ofstream fout("inversmodular.out"); + + int a,x,y,b; + + fin>>a>>b; + + euclid(a,b,1,x,y); + + x=((long long)b+x%b)%b; + + fout< + + +using namespace std; + + + +string file="inversmodular"; + +ifstream fin(file+".in"); + +ofstream fout(file+".out"); + + + +typedef long long ll; + + + +int N,A; + +ll inv,ins; + + + +void gcd(ll &x,ll &y,ll a,ll b) + +{ + + if(!b){ + + x=1; y=0; + + } + + else{ + + gcd(x,y,b,a%b); + + ll aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + fin>>A>>N; + + gcd(inv,ins,A,N); + + if(inv<=0){ + + inv=N+inv%N; + + } + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a, n; + + + + + +pair euclid_ext(int x, int y) + +{ + + if(y == 0) + + { + + return {1, 0}; + + } + + pair p = euclid_ext(y, x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + + + + + +int main() + +{ + + f>>a>>n; + + pair rez = euclid_ext(a, n); + + while(rez.first < 0) + + rez.first += n; + + g< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + +long long a,n,x,y; + + + +void read() + +{ + + fin>>a>>n; + +} + + + +void invmod(long long u,long long v,long long &x,long long &y) + +{ + + if(!v) + + { + + x=1; + + y=0; + + return; + + } + + + + long long X,Y; + + invmod(v,u%v,X,Y); + + x=Y; + + y=X-(u/v)*Y; + + + +} + + + +void solve() + +{ + + invmod(a,n,x,y); + + + + while(x<0) + + x+=n; + + + + fout< +#include + +using namespace std; + +template +pair gcd_ext(T a, T b, T c) { + if (b == 0) { + if (c % a == 0) + return make_pair(c/a, 0); + else + return make_pair(0, 0); + } + + auto p = gcd_ext(b, a % b, c); + return make_pair(p.second, p.first - (a/b) * p.second); +} + +int main(int argc, char const *argv[]) +{ + ifstream fin("inversmodular.in"); + ofstream fout("inversmodular.out"); + + long int A, N; fin >> A >> N; + + auto p = gcd_ext(A, N, 1); + long int x = p.first; + x %= N; + if (x < 0) x += N; + + fout << x << endl; + + return 0; +} diff --git a/invmod/extended-euclid-algo/4904c9f1-0918-4032-aaad-22fa1557856f.cpp b/invmod/extended-euclid-algo/4904c9f1-0918-4032-aaad-22fa1557856f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9efdd0b2fe203e40c87e4e8c2e1eccc508177704 --- /dev/null +++ b/invmod/extended-euclid-algo/4904c9f1-0918-4032-aaad-22fa1557856f.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + +int a,n; + + + +void invmod(long long &x,long long &y,int a,int b){ + + if (!b){ + + x=1; y=0; + + } + + else{ + + invmod(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + + + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false);; + + cin.tie(0); + + cout.tie(0); + + long long invm,aj; + + cin>>a>>n; + + invmod (invm,aj,a,n); + + if (invm<0) invm=n+invm%n; + + cout< +#include + +using namespace std; + +ifstream fin ("inversmodular.in"); +ofstream fout("inversmodular.out"); + +int x, MOD, temp, ans; + +int gcd(int a, int b, int &x, int &y){ + if(b == 0){ + x = 1; + y = 0; + return a; + } + int x0, y0; + int d = gcd(b, a%b, x0, y0); + x = y0; + y = x0 - (a/b) * y0; + return d; +} + +int main(){ + fin >> x >> MOD; + gcd(x, MOD, ans, temp); + while(ans < 0) + ans += MOD; + fout << ans; + + return 0; +} diff --git a/invmod/extended-euclid-algo/4c05bdf0-07f1-49a7-adbd-5dc6b00b9c11.cpp b/invmod/extended-euclid-algo/4c05bdf0-07f1-49a7-adbd-5dc6b00b9c11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0acfa14df46e0af95c78366428ea7a047871c8b6 --- /dev/null +++ b/invmod/extended-euclid-algo/4c05bdf0-07f1-49a7-adbd-5dc6b00b9c11.cpp @@ -0,0 +1,70 @@ +#include + + +void euclid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + + + +int main() + +{ + + FILE *f =fopen("inversmodular.in", "r"); + + FILE *g =fopen("inversmodular.out", "w"); + + int t, a, b,c ,x, y; + + + + fscanf(f, "%d%d", &a, &b); + + euclid_eXt(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/4c7ce038-5dff-4ce5-97db-5f38a95a7a65.cpp b/invmod/extended-euclid-algo/4c7ce038-5dff-4ce5-97db-5f38a95a7a65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ce787a34e4729641b71490fe9e3bdf296fda568 --- /dev/null +++ b/invmod/extended-euclid-algo/4c7ce038-5dff-4ce5-97db-5f38a95a7a65.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int a, p; + + + +void Euclid (int a, int b, int&d, int &x, int &y) + +{ + + if (b==0) d=a, x=1, y=0; + + else + + { + + int x0, y0; + + Euclid(b, a%b, d, x0, y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + + + +int InversMod(int a, int p) + +{ + + int d, x, y; + + Euclid(a, p, d, x, y); + + if (x<0) x+=p; + + return x; + +} + + + +int main() + +{ + + fin>>a>>p; + + fin.close(); + + fout< +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +typedef long long ll; + + + +ll modulo; + + + +ll cmmdc(ll a, ll b, ll &x, ll &y){ + + if(b == 0){ + + x = 1; + + y = 0; + + return a; + + }else{ + + ll ans = cmmdc(b, a % b, x, y); + + ll xPrim = x, yPrim = y; + + x = yPrim % modulo; + + y = (xPrim - (a / b) * yPrim) % modulo; + + return ans; + + } + +} + + + +int main() + +{ + + ll n, m, x, y, ans; + + in >> n >> m; + + modulo = m; + + ans = cmmdc(n, m, x, y); + + x = x % m; + + if(x < 0) + + x += m; + + out << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/4f2b15bc-3339-4a78-88df-cee305187893.cpp b/invmod/extended-euclid-algo/4f2b15bc-3339-4a78-88df-cee305187893.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adb842f12b5879524afd2e1cf7dea2b3dde6e093 --- /dev/null +++ b/invmod/extended-euclid-algo/4f2b15bc-3339-4a78-88df-cee305187893.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int a,n; + +long long inv,ins; + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + x=1,y=0; + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + f>>a>>n; + + gcd(inv,ins,a,n); + + if(inv<=0) + + inv=(n+inv)%n; + + g< +#include + + + +void euclid_extins(int a, int b, long long &x, long long &y) { + if (b == 0) { + x = 1; + y = 0; + } else { + euclid_extins(b, a % b, x, y); + long long temp = x; + x = y; + y = temp - (a / b) * y; + } +} + +int main() { + + + + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + int a, n; + long long inv = 0, y; + + + scanf("%d%d", &a, &n); + euclid_extins(a, n, inv, y); + + if (inv <= 0) { + inv = n + inv % n; + } + + printf("%lld\n", inv); + + + return 0; +} diff --git a/invmod/extended-euclid-algo/5022f270-09df-44bb-8819-a9ceb31d3abb.cpp b/invmod/extended-euclid-algo/5022f270-09df-44bb-8819-a9ceb31d3abb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..113036bf9daa17631b4ed5e9341b111f9cff538e --- /dev/null +++ b/invmod/extended-euclid-algo/5022f270-09df-44bb-8819-a9ceb31d3abb.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +pair euclid(int x, int y) { + if (y == 0) return {1, 0}; + auto p = euclid(y, x % y); + return {p.second, p.first - 1ll * (x / y) * p.second}; +} + +int main() { + int a, n; + + in >> a >> n; + + auto q = euclid(a, n); + while (q.first < 0) + q.first += n; + + out << q.first; + + return 0; +} diff --git a/invmod/extended-euclid-algo/5036b294-8832-4267-9cc7-6abeafe64a00.cpp b/invmod/extended-euclid-algo/5036b294-8832-4267-9cc7-6abeafe64a00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54381ac37601cd357b8edcd94c5d3e2ce1575abe --- /dev/null +++ b/invmod/extended-euclid-algo/5036b294-8832-4267-9cc7-6abeafe64a00.cpp @@ -0,0 +1,59 @@ +#include +#include + + +using namespace std; + + + +int a,n; + + + +void euclid_extins(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + x = 1,y = 0; + + else + + { + + euclid_extins( x, y, b, a%b); + + long long aux = x; + + x = y; + + y = aux - y *(a / b); + + } + +} + +int main() + +{ + + ifstream cin("inversmodular.in"); + + ofstream cout("inversmodular.out"); + + long long inv = 0,ins; + + cin>>a>>n; + + euclid_extins(inv,ins,a,n); + + if(inv <= 0) + + inv = n +inv % n; + + cout< + + + + + + + +using namespace std; + + + +ifstream f("inversmodular" ".in"); + +ofstream g("inversmodular" ".out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef pair pi; + +typedef pair llp; + +typedef pair pct; + + + +const ll inf = 1LL << 60; + +const ll mod = 1e9 + 7; + +const ld eps = 1e-9; + + + + + +void add(ll &a , ll b) + +{ + + a += b; + + a %= mod; + +} + + + +void sub(ll &a, ll b) + +{ + + a = (a - b + mod) % mod; + +} + +ll a, b, x,y; + + + +ll gcd(ll a, ll b, ll &x , ll &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + return a; + + } + + ll v = gcd(b, a % b, x, y); + + ll aux = y; + + y = x - (a / b) * y; + + x = aux; + + return v; + +} + +int main() + +{ + + f >> a >> b; + + gcd(a,b,x,y); + + g << (x % b + b) % b; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/50f4f5eb-0dc9-4655-9f71-5ec824bbc942.cpp b/invmod/extended-euclid-algo/50f4f5eb-0dc9-4655-9f71-5ec824bbc942.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a163e6e4e4a065d3438923c69691f2a4727ecfd --- /dev/null +++ b/invmod/extended-euclid-algo/50f4f5eb-0dc9-4655-9f71-5ec824bbc942.cpp @@ -0,0 +1,70 @@ +#include + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/521cf3ba-4145-40d2-b6db-eddf40afb924.cpp b/invmod/extended-euclid-algo/521cf3ba-4145-40d2-b6db-eddf40afb924.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bcaa649832b21c4fd1c571f15070cf07c9748d0 --- /dev/null +++ b/invmod/extended-euclid-algo/521cf3ba-4145-40d2-b6db-eddf40afb924.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void inversmod(long long &x, long long &y, int a, int b) + +{ + + if (b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + inversmod(x, y, b, a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + long long x, y; + + int a, n; + + fin>>a>>n; + + inversmod(x, y, a, n); + + while(x<=0) + + x=x+n; + + fout< +#include + + +using namespace std; + + + +ifstream x("inversmodular.in"); + +ofstream y("inversmodular.out"); + + + +int a,n,yy,sol; + + + +void e_e(int &xx, int &yy, int a, int b) + +{ + + if(!b) + + xx=1,yy=0; + + else + + { + + e_e(xx,yy,b,a%b); + + int aux=xx; + + xx=yy; + + yy=aux-yy*(a/b); + + } + +} + + + +int main() + +{ + + x>>a>>n; + + e_e(sol,yy,a,n); + + if(sol<=0) + + sol=n+sol%n; + + y< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +inline int gcd (int A, int B, long long &X, long long &Y ) { + + if (B == 0) { + + X = 1; + + Y = 0; + + return A; + + } + + int D; + + long long X0, Y0; + + D = gcd (B, A % B, X0, Y0); + + X = Y0; + + Y = X0 - (A / B) * Y0; + + return D; + +} + + + +int main() { + + long long inv = 0, ins; + + int A, N; + + fin >> A >> N; + + gcd (A, N, inv, ins); + + if (inv <= 0) inv = N + inv % N; + + fout << inv; + + fin.close (); + + fout.close (); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/5284cc63-6e7e-4112-bef2-2d0bf013dcc3.cpp b/invmod/extended-euclid-algo/5284cc63-6e7e-4112-bef2-2d0bf013dcc3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..016e9768bc61d622c193f1b2972e52711ff36383 --- /dev/null +++ b/invmod/extended-euclid-algo/5284cc63-6e7e-4112-bef2-2d0bf013dcc3.cpp @@ -0,0 +1,78 @@ +#include + + + + + + +namespace maths { + + void euclid(long long A, long long B, long long &X, long long &Y) { + + if (B == 0) { + + Y = 0, X = 1; + + return; + + } euclid(B, A%B, X, Y); + + long long aux = Y; Y = X - (A/B)*Y; X = aux; + + } + + + + long long invmod(long long A, long long MOD) { + + long long temp, inv; + + euclid(A, MOD, inv, temp); + + return (inv+MOD)%MOD; + + } + +} + + + +long long A, N; + + + +std::ifstream input ("inversmodular.in"); + +std::ofstream output("inversmodular.out"); + + + +void readInput() { + + input >> A >> N; + +} + + + +void solveInput() { + + output << maths::invmod(A, N); + +} + + + +int main() + +{ + + readInput(); + + solveInput(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/53d01ac8-0a36-49f2-865c-110ce5d03731.cpp b/invmod/extended-euclid-algo/53d01ac8-0a36-49f2-865c-110ce5d03731.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2235eb467d725732f520711edc3119157aaf096 --- /dev/null +++ b/invmod/extended-euclid-algo/53d01ac8-0a36-49f2-865c-110ce5d03731.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +typedef unsigned long long ull; +typedef signed long long ll; +typedef unsigned int uint; +typedef unsigned short ushort; +typedef unsigned char uchar; + +void gcd_extended(int a, int b, int &d, int &x, int &y) { + if (b == 0) { + x = 1; + y = 0; + d = a; + + } else { + int x0, y0; + gcd_extended(b, a % b, d, x0, y0); + x = y0; + y = x0 - (a / b) * y0; + } +} + +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + int A, N; + scanf("%d %d", &A, &N); + + int d, x, y; + gcd_extended(A, N, d, x, y); + while (x <= 0) { + x += N; + x %= N; + } + printf("%d\n", x); + + return 0; +} diff --git a/invmod/extended-euclid-algo/53f49e55-27ca-49e1-9215-598a48c6e5d4.cpp b/invmod/extended-euclid-algo/53f49e55-27ca-49e1-9215-598a48c6e5d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa8056f599891e7be5a93b9bb98198d6acea9e3f --- /dev/null +++ b/invmod/extended-euclid-algo/53f49e55-27ca-49e1-9215-598a48c6e5d4.cpp @@ -0,0 +1,73 @@ +#include + + + + +typedef long long ll; + +typedef long double ld; + + + +const int nmax=1e5+3; + +const int mod=998244353; + +const ll inf=0x3f3f3f3f3f3f3f3f; +using namespace std; + + + +int gcdex(int a,int b,int &x,int &y) { + + if(!b) { + + x=1; + + y=0; + + return a; + + } + + else { + + int d,x1,y1; + + d=gcdex(b,a%b,x1,y1); + + x=y1; + + y=x1-(a/b)*y1; + + return d; + + } + +} + + + +int a,b,x,y,d; + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + ios_base::sync_with_stdio(0); cin.tie(0); + + cin>>a>>b; + + d=gcdex(a,b,x,y); + + if(x<=0) + + x=b+x%b; + + cout< +using namespace std; + +ifstream f ("inversmodular.in"); + +ofstream g ("inversmodular.out"); + +void euclid(int a , int b ,int & x ,int & y) + +{ + + if(b == 0) + + { + + x = 1, y = 1; + + } + + else + + { + + int x1 , y1; + + euclid(b , a % b , x1 , y1); + + x = y1; + + y = x1 - a / b * y1; + + } + +} + + + +int main() + +{ + + int A,N; + + f>>A>>N; + + int X , Y; + + euclid(A, N , X ,Y); + + while(X < 0) + + X += N; + + g << X; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/5480c985-3dbb-40fb-bc1e-6ec31d51721b.cpp b/invmod/extended-euclid-algo/5480c985-3dbb-40fb-bc1e-6ec31d51721b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2053d60d5d31c9762d7425720497d249460237a9 --- /dev/null +++ b/invmod/extended-euclid-algo/5480c985-3dbb-40fb-bc1e-6ec31d51721b.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, b, c, x, y, d, n; + +int cmmdc(int a, int b, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1, y = 0; + + return a; + + } + + int x0, y0, d; + + d = cmmdc(b, a%b, x0, y0); + + + + x = y0; + + y = x0-(a/b)*y0; + + return d; + +} + + + +int main() + +{ + + fin >> a >> b; + + d = cmmdc(a, b, x, y); + + + + while(x < 0) + + x += b; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/55072c89-48f1-42ea-9189-faf4e1f432c9.cpp b/invmod/extended-euclid-algo/55072c89-48f1-42ea-9189-faf4e1f432c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ae2eab3490ac813471339e43844f1ab4dbcb0ba --- /dev/null +++ b/invmod/extended-euclid-algo/55072c89-48f1-42ea-9189-faf4e1f432c9.cpp @@ -0,0 +1,46 @@ +#include + + +using namespace std; + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +long long plmplmplmpl(long long a, long long b, long long &x, long long &y){ + + if(!b){ + + x=1; + + y=0; + + return a; + + } + + long long t=plmplmplmpl(b, a%b, x, y); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + return t; + +} + +int main(){ + + long long a, n,x,y; + + in>>a>>n; + + plmplmplmpl(a, n, x, y); + + out<<(x+n)%n; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/55ea21b7-5890-4d75-af21-c442c57bab43.cpp b/invmod/extended-euclid-algo/55ea21b7-5890-4d75-af21-c442c57bab43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75aa5fb0a3cc66dc5eed0495eb09e4915ecfc1f7 --- /dev/null +++ b/invmod/extended-euclid-algo/55ea21b7-5890-4d75-af21-c442c57bab43.cpp @@ -0,0 +1,36 @@ +#include +#include + +std::ifstream fin("inversmodular.in"); +std::ofstream fout("inversmodular.out"); + +int a,n; + +void euclid(int a, int b, long long &x, long long &y, long long &d) +{ + if(b==0) + { + d=a; + x=1; + y=0; + } + else + { + long long x0,y0; + euclid(b,a%b,x0,y0,d); + y=x0-(a/b)*y0; + x=y0; + } +} + +int main() +{ + fin>>a>>n; + long long x,y,d; + euclid(a,n,x,y,d); + if(x<0) + x+=((-x)/n)*n; + if(x<0) + x+=n; + fout< +#include + + +using namespace std; + + + +typedef pair pr; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + + + +pr ee(int a, int b) { + + + + if(b == 0) { + + return {1, 0}; + + } + + pr r = ee(b, a % b); + + return {r.second, r.first - (a / b) * r.second}; + +} + + + + + +int im(int x, int n) + +{ + + int r = ee(x, n).first; + + if(r < 0) { + + r = n + (r % n); + + } + + return r; + +} + + + +int main() + +{ + + int x = 0, n = 0; + + + + f >> x >> n; + + + + g << im(x, n); + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/586a0b5d-bfd5-4b8b-8cb9-cc39d6edc1d6.cpp b/invmod/extended-euclid-algo/586a0b5d-bfd5-4b8b-8cb9-cc39d6edc1d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9974d26e91b2dc507d96237ab3d6edcb653d3081 --- /dev/null +++ b/invmod/extended-euclid-algo/586a0b5d-bfd5-4b8b-8cb9-cc39d6edc1d6.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +void euclid(int a, int b, int *x, int *y) { + if (b == 0) { + *x = 1; + *y = 0; + } else { + int x0, y0; + euclid(b, a % b, &x0, &y0); + *x = y0; + *y = x0 - (a / b) * y0; + } +} +ifstream fin( "inversmodular.in" ); +ofstream fout( "inversmodular.out" ); +int main() { + int a, n, x, y; + fin >> a >> n; + euclid( a, n, &x, &y ); + if ( x <= 0 ) + x = n + x % n; + fout << x; + + return 0; +} diff --git a/invmod/extended-euclid-algo/5908dd17-5bd0-4e94-ae72-f82b9f319490.cpp b/invmod/extended-euclid-algo/5908dd17-5bd0-4e94-ae72-f82b9f319490.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8344d77498fa78252287359deffb7dabc8e1f4bc --- /dev/null +++ b/invmod/extended-euclid-algo/5908dd17-5bd0-4e94-ae72-f82b9f319490.cpp @@ -0,0 +1,72 @@ +#include + + +FILE* fin, * fout; + + + +int a, n; + + + +int invers(int a, int n) { + + int t = n; + + int v[2], v1[2] = { 1,0 }, v2[2] = { 0,1 }; + + while (n) { + + int q, r; + + q = a / n; + + r = a % n; + + v[0] = v1[0], v[1] = v1[1]; + + v1[0] = v2[0], v1[1] = v2[1]; + + v2[0] = v[0] - q * v2[0], v2[1] = v[1] = q * v2[1]; + + + + a = n; + + n = r; + + } + + + + int alfa = v1[0] % t; + + if (alfa < 0) alfa += t; + + + + return alfa; + +} + + + +int main() { + + fin = fopen("inversmodular.in", "r"); + + fout = fopen("inversmodular.out", "w"); + + + + fscanf(fin, "%i %i", &a, &n); + + + + fprintf(fout, "%i", invers(a, n)); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/590c62cf-81e8-4aa5-964d-c1aca6960abc.cpp b/invmod/extended-euclid-algo/590c62cf-81e8-4aa5-964d-c1aca6960abc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a214ea6fbfe29cfbba26c99fbf7d68ce31d86dd --- /dev/null +++ b/invmod/extended-euclid-algo/590c62cf-81e8-4aa5-964d-c1aca6960abc.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +void EE(int a, int b, int &d, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + d = a; + + } + + else + + { + + int x0, y0; + + EE(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int inversModular(int A, int N) + +{ + + int X, Y, d; + + EE(A, N, d, X, Y); + + X %= N; + + if(X < 0) + + X += N; + + return X; + +} + +int main() + +{ + + int A, N; + + f >> A >> N; + + g << inversModular(A, N); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/599c443b-fade-455d-ab43-aafea44b6102.cpp b/invmod/extended-euclid-algo/599c443b-fade-455d-ab43-aafea44b6102.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44f5f4cb5f1c972e415dddf2be04ff96d11aedb4 --- /dev/null +++ b/invmod/extended-euclid-algo/599c443b-fade-455d-ab43-aafea44b6102.cpp @@ -0,0 +1,72 @@ +#include + + +using namespace std; + +typedef long long ll; + + + +int A, N; + + + +void gcd(ll &x, ll &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + ll aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + ll inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/59aad860-9360-46c4-87f1-da5fc13a8104.cpp b/invmod/extended-euclid-algo/59aad860-9360-46c4-87f1-da5fc13a8104.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fec345e7b5f6cfd6f3d0278c9fcd77f25957bcf --- /dev/null +++ b/invmod/extended-euclid-algo/59aad860-9360-46c4-87f1-da5fc13a8104.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void invrs(long long int &x, long long int &y, int a, int b){ + + if(!b) + + x=1,y=0; + + else{ + + invrs(x,y,b,a%b); + + swap(x,y); + + y=y-x*(a/b); + + } + + + +} + + + +int main(){ + + long long int inv=0,ins; + + int a,b; + + f>>a>>b; + + invrs(inv,ins,a,b); + + + + if(inv<=0) + + inv = b+inv%b; + + + + g< + + +using namespace std; + + + + + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Quick(int a, int b, int mod) + +{ + + int p=1; + + while (b>0) + + { + + if (b%2==1) p=1ll*p*a%mod; + + b/=2; + + a=1ll*a*a%mod; + + } + + return p; + +} + +int Phi(int n) + +{ + + int sol,p; + + sol=n; + + for (p=2; n>1 and p*p<=n; p++) + + { + + if (n%p==0) + + { + + sol=sol/p*(p-1); + + while (n%p==0) + + n/=p; + + } + + } + + if (n>1) sol=sol/n*(n-1); + + return sol; + +} + + + + + +int main() + +{ + + int a,n; + + fin >> a >> n; + + fout << (Quick(a,(Phi(n)-1),n)); + return 0; + +} diff --git a/invmod/extended-euclid-algo/5a39d9b1-4f43-41b4-bbb8-55b94b8f2c90.cpp b/invmod/extended-euclid-algo/5a39d9b1-4f43-41b4-bbb8-55b94b8f2c90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f0c84700f7bf44fa63339e64c3a6939a41daab8 --- /dev/null +++ b/invmod/extended-euclid-algo/5a39d9b1-4f43-41b4-bbb8-55b94b8f2c90.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int main() + +{ + +long long int a, n, aux, r, c, y, y0, y1; + +y0 = 0; + +y1 = 1; + +fin >> a >> n; + +aux = n; + +while(a!=0) + +{ + + r = n%a; + + c = n/a; + + n = a; + + a = r; + + y = y0 - c * y1; + + y0 = y1; + + y1 = y; + +} + +if(y0 < 0) + + y0 = aux + y0%aux; + +fout << y0; + +return 0; + +} diff --git a/invmod/extended-euclid-algo/5a7f9a7d-03d3-4c96-af6e-db813b8e859d.cpp b/invmod/extended-euclid-algo/5a7f9a7d-03d3-4c96-af6e-db813b8e859d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca253bc85bfa2ba2aa571c720946dba590c310a3 --- /dev/null +++ b/invmod/extended-euclid-algo/5a7f9a7d-03d3-4c96-af6e-db813b8e859d.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + + + + + +pair euclid(int x, int y){ + + if(y==0) + + return {1, 0}; + + auto p=euclid(y,x%y); + + return {p.second, p.first - (x/y)*p.second }; + +} + +int a, n, x, y; + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + + + scanf("%d %d", &a, &n); + + auto p=euclid(a,n); + + while(p.first<0) + + p.first+=n; + + printf("%d",p.first); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/5ac08bb4-84eb-4aaa-8393-cf11ecdff65e.cpp b/invmod/extended-euclid-algo/5ac08bb4-84eb-4aaa-8393-cf11ecdff65e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f660e8525ca914d64183986abe791da05c5732e6 --- /dev/null +++ b/invmod/extended-euclid-algo/5ac08bb4-84eb-4aaa-8393-cf11ecdff65e.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +ifstream f ("inversmodular.in"); + +ofstream g ("inversmodular.out"); + +void euclid_extins(long long a,long long b,long long &x,long long &y) + +{ + + if (b == 0) + + { + + x = 1; + + y = 0; + + return; + + } + + long long fost_x,fost_y; + + euclid_extins(b,a%b,fost_x,fost_y); + + x = fost_y; + + y = fost_x + (-a/b)*fost_y; + + return; + +} + +int main() + +{ + + long long nr,mod,x,y; + + f >> nr>> mod; + + euclid_extins(nr,mod,x,y); + + long long invers_modular = (x + mod) % mod; + + g << invers_modular; + + + + + +} diff --git a/invmod/extended-euclid-algo/5c77c103-d0ae-4d0f-925a-2148e300fffb.cpp b/invmod/extended-euclid-algo/5c77c103-d0ae-4d0f-925a-2148e300fffb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46663ab873d344b20233f95263023e99c88dae03 --- /dev/null +++ b/invmod/extended-euclid-algo/5c77c103-d0ae-4d0f-925a-2148e300fffb.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void Euclid(int a,int b, int &d,int &x,int &y) + +{ + + if(b==0) {d=a,x=1,y=0;} + + else { + + int x0,y0; + + Euclid(b,a%b,d,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + + + +int InversModular(int A,int N) + +{ + + int d,x,y; + + Euclid(A,N,d,x,y); + + if(x<0) x+=N; + + return x; + +} + + + +int main() + +{ + + int A,N; + + fin>>A>>N; + + fout< + + +using namespace std; + +FILE *f,*g; + + + +void inv(long long a,long long b,long long &x,long long &y) + +{ + + if(!b) + + x=1,y=0; + + else + + { + + inv(b,a%b,x,y); + + int aux=x; + + x=y; + + y=aux-a/b*y; + + } + +} + +int main() + +{ + + f=fopen("inversmodular.in","r"); + + g=fopen("inversmodular.out","w"); + + long long nr,mod,x,y; + + fscanf(f,"%lld %lld",&nr,&mod); + + inv(nr,mod,x,y); + + if(x<=0) + + x=x+mod; + + fprintf(g,"%lld\n",x); + + fclose(f); + + fclose(g); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/5d47601d-e6bb-4f32-ba53-cdb3a324c9bb.cpp b/invmod/extended-euclid-algo/5d47601d-e6bb-4f32-ba53-cdb3a324c9bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79269b1cde552fbbaf7c2add3b8338e71bf6364d --- /dev/null +++ b/invmod/extended-euclid-algo/5d47601d-e6bb-4f32-ba53-cdb3a324c9bb.cpp @@ -0,0 +1,114 @@ +#include + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + + + +{ + + + + if (!b) + + + + x = 1, y = 0; + + + + else + + + + { + + + + gcd(x, y, b, a % b); + + + + long long aux = x; + + + + x = y; + + + + y = aux - y * (a / b); + + + + } + + + +} + + + +int main() + + + +{ + + + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + + + gcd(inv, ins, A, N); + + + + + + + + if (inv <= 0) + + + + inv = N + inv % N; + + + + + + + + printf("%lld\n", inv); + + + + + + + + return 0; + + + +} diff --git a/invmod/extended-euclid-algo/5d7af645-1456-4455-affb-b24275e53d6f.cpp b/invmod/extended-euclid-algo/5d7af645-1456-4455-affb-b24275e53d6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3635d53b984eb7ed1ae48da9870611d5990d482d --- /dev/null +++ b/invmod/extended-euclid-algo/5d7af645-1456-4455-affb-b24275e53d6f.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void cmmdc(int a, int b, long long &x, long long &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + } + + else + + { + + long long x0,y0; + + cmmdc(b, a % b, x0, y0); + + x = y0; + + y = x0 - a / b * y0; + + } + +} + + + +int main() + +{ + + int a,n; + + long long x,y; + + fin >> a >> n; + + cmmdc(a,n,x,y); + + if(x <= 0)x = n + x % n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/5eb3aa78-e29e-4060-8737-78d3371488fb.cpp b/invmod/extended-euclid-algo/5eb3aa78-e29e-4060-8737-78d3371488fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edd551c84d82342f6291ce0fd393d366b91b52fa --- /dev/null +++ b/invmod/extended-euclid-algo/5eb3aa78-e29e-4060-8737-78d3371488fb.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +ifstream fin ( "inversmodular.in" ); + +ofstream fout ( "inversmodular.out" ); + + + +void invers_mod ( int a, int b, int &x, int &y ); + + + +int main ( ) + +{ + + int a, mod; + + + + fin >> a >> mod; + + + + int x, y; + + + + invers_mod( a, mod, x, y); + + + + while ( x < 0 ) + + x += mod; + + fout << x << '\n' ; + +} + + + + + +void invers_mod ( int a, int b, int &x, int &y ) + +{ + + if ( b == 0 ) + + { + + x = 1; + + y = 0; + + + + return ; + + } + + + + int c = a/b; + + + + invers_mod ( b, a%b, x, y ); + + + + int x1 = y; + + int y1 = x - y*c; + + + + y = y1; + + x = x1; + +} diff --git a/invmod/extended-euclid-algo/5ef943ea-0f9f-4d79-a812-18c0ef779e3d.cpp b/invmod/extended-euclid-algo/5ef943ea-0f9f-4d79-a812-18c0ef779e3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6dc68d825a0c1a04031884b54900d1e191ca74dd --- /dev/null +++ b/invmod/extended-euclid-algo/5ef943ea-0f9f-4d79-a812-18c0ef779e3d.cpp @@ -0,0 +1,77 @@ +#include +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int modInv(int a, int b) + +{ + + long long r, x1=1, x2=0, q, aux, auxb = b; + + + + while(b > 0) + + { + + q = a/b; + + aux = a; + + a = b; + + b = aux - q * b; + + aux = x1; + + x1 = x2; + + x2 = aux - q * x2; + + } + + if(x1 < 0) + + { + + x1 += auxb; + + } + + return x1; + +} + + + +int main() + +{ + + int a, n; + + + + fin>>a>>n; + + + + fout< +long long A, N, modular_inverse; + + + + + +long long ext_euclid(long long a, long long b) + +{ + + a %= b; + + if (a < 0) + + { + + a += b; + + } + + + + long long old_r = a, r = b; + + long long old_s = 1, s = 0; + + long long old_t = 0, t = 1; + + long long q, new_r, new_s, new_t; + + while (r != 0) + + { + + q = old_r / r; + + new_s = old_s - s * q; + + new_t = old_t - t * q; + + new_r = old_r % r; + + old_r = r, r = new_r; + + old_s = s, s = new_s; + + old_t = t, t = new_t; + + } + + return old_s; + +} + + + + + +int main() + +{ + + std::ifstream fisierIN("inversmodular.in"); + + fisierIN >> A >> N; + + fisierIN.close(); + + modular_inverse = ext_euclid(A, N); + + if (modular_inverse < 0) + + { + + do + + { + + modular_inverse += N; + + } while (modular_inverse < 0); + + } + + std::ofstream fisierOUT("inversmodular.out"); + + fisierOUT << modular_inverse; + + fisierOUT.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/60ac6cbe-ec83-4c3f-8ef9-5a4d63efc9ca.cpp b/invmod/extended-euclid-algo/60ac6cbe-ec83-4c3f-8ef9-5a4d63efc9ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93042ebbd5b23da651e34d46f22371a8d17df3c6 --- /dev/null +++ b/invmod/extended-euclid-algo/60ac6cbe-ec83-4c3f-8ef9-5a4d63efc9ca.cpp @@ -0,0 +1,55 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,a; + +void euclid(int a,int b,long long &x,long long &y){ + + if(b==0){ + + x=1,y=0; + + } + + else{ + + euclid(b,a%b,x,y); + + long long auxx=x; + + x=y; + + y=auxx-y*(a/b); + + } + +} + +int main(){ + + long long x=0,y=0; + + fin>>a>>n; + + euclid(a,n,x,y); + + if(x<=0){ + + x=n+x%n; + + } + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + + + +void euclid_e(long long &x, long long &y, int a, int b) + +{ + + if(!b) x=1,y=0; + + else { + + euclid_e(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + f>>a>>n; + + long long inv=0, ins; + + euclid_e(inv,ins,a,n); + + if(inv<=0) inv=n+inv%n; + + g< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int Euclid(long long a, long long b, long long &x, long long &y){ + + if (b == 0){ + + x = 1; + + y = 0; + + return a; + + } + + else{ + + long long x0, y0; + + long long d = Euclid(b, a % b, x0, y0); + + x = y0; + + y = x0 - 1LL * (a / b) * y0; + + return d; + + } + +} + + + +int main(){ + + long long a, n; + + fin >> a >> n; + + long long x, y, d; + + d = Euclid(a, n, x, y); + + if (x < 0) x = n + x % n; + + fout << x; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/621b7ed7-b322-452a-9c67-8ff12844d9fc.cpp b/invmod/extended-euclid-algo/621b7ed7-b322-452a-9c67-8ff12844d9fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fd5e3e967ae4289b7b918a8ae64b6e4df6d2955 --- /dev/null +++ b/invmod/extended-euclid-algo/621b7ed7-b322-452a-9c67-8ff12844d9fc.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invmod(long long &invers,long long &ins,int a,int minune) + +{ + + if(!minune) + + { + + invers=1; + + ins=0; + + } + + else + + { + + invmod(invers,ins,minune,a%minune); + + long long aux=invers; + + invers=ins; + + ins=aux-ins*(a/minune); + + } + +} + +int main() + +{ + + long long invers=0,ins; + + int a,minune; + + fin>>a>>minune; + + invmod(invers,ins,a,minune); + + if(invers<=0) + + invers=minune+invers%minune; + + fout< + +using namespace std; +long long a,b,x,y; + +ifstream cin("inversmodular.in"); +ofstream cout("inversmodular.out"); + +void gcd_extended(long long a, long long b, long long&x, long long&y){ + if(!b) + x=1, y=0; + else{ + long long x0, y0; + gcd_extended(b, a%b, x0, y0); + x=y0; + y=x0-(a/b)*y0; + } +} + +int main(){ +cin>>a>>b; +gcd_extended(a,b,x,y); +while(x<0) + x+=b; +cout< +#include + + +using namespace std; + + + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + + + +void euclidext( long long a ,long long b , long long &x,long long &y) + +{ + + + + if(!b) + + { + + x=1; + + y=0; + + return; + + } + + euclidext(b,a%b,x,y); + + long long aux=x; + + x=y; + + y=aux -(a/b)*y; + +} + + + + + +int main() + +{ + + long long a,b,x,y; + + f>>a>>b; + + euclidext(a,b,x,y); + + if(x<0) + + g< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void Euclid(int a,int b,int&d,int&x,int&y) + +{ + + if(b==0) {d=a;x=1;y=0;} + + else + + { + + int x0,y0; + + Euclid(b,a%b,d,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + +int InversModular(int A,int N) + +{ + + int x,y,d; + + Euclid(A,N,d,x,y); + + if(x<0) x+=N; + + return x; + +} + +int main() + +{ + + int A,N; + + fin>>A>>N; + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a,b,x,y,z=1,r,c,in; + +int main() + +{ + + f >> a >> b;in=b; + + while(a) + + { + + r=b%a; + + c=b/a; + + b=a; + + a=r; + + x=y-c*z; + + y=z; + + z=x; + + } + + while(y<0) + + y+=in; + + g << y ; + + + +} diff --git a/invmod/extended-euclid-algo/63f70c3d-7f1c-4d09-92fb-6b5e4178819d.cpp b/invmod/extended-euclid-algo/63f70c3d-7f1c-4d09-92fb-6b5e4178819d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a199f9e55dbe570118097bcfb4ff61fa2a45d612 --- /dev/null +++ b/invmod/extended-euclid-algo/63f70c3d-7f1c-4d09-92fb-6b5e4178819d.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +ifstream cin("inversmodular.in"); +ofstream cout("inversmodular.out"); +long gcd(long a, long b, long &x, long &y){ + if (!b){ + x=1; + y=0; + return a; + } + else{ + long d, x0, y0; + d=gcd(b, a%b, x0, y0); + x=y0; + y=x0-(a/b)*y0; + return d; + } +} +int main() +{ + long a, n, x, y; + cin>>a>>n; + gcd(a, n, x, y); + while (x<0){ + x=x+n; + } + cout< + + +long long a, n; + +void cmmdc (long long a, long long b, long long &x, long long &y) { + + long long x0, y0; + + if (b == 0) + + { x = 1, y = 0; } + + else { + + cmmdc (b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int32_t main() { + + long long x, y; + + freopen ("inversmodular.in", "r", stdin); + + freopen ("inversmodular.out", "w", stdout); + + scanf ("%d %d", &a, &n); + + cmmdc (a, n, x, y); + + printf ("%d", ( (x % n) + n) % n); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/65bf34ea-479c-48d1-992f-004e092914cc.cpp b/invmod/extended-euclid-algo/65bf34ea-479c-48d1-992f-004e092914cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f7ea6c6cff899e35a82a2cf52ccf699e1139487 --- /dev/null +++ b/invmod/extended-euclid-algo/65bf34ea-479c-48d1-992f-004e092914cc.cpp @@ -0,0 +1,48 @@ +#include + + +long long a,n; + +void cmmdc(long long a,long long b,long long *x,long long *y) + +{ + + long long x0,y0; + + if (b==0) + + *x=1,*y=0; + + else + + { + + cmmdc(b,a%b,&x0,&y0); + + *x = y0; + + *y = x0 - (a / b) * y0; + + } + +} + +int32_t main() + +{ + + long long x,y; + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%d %d",&a,&n); + + cmmdc(a,n,&x,&y); + + printf("%d",((x%n)+n)%n); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/65fcb93e-0a1d-4229-b3e6-eb2102c100bd.cpp b/invmod/extended-euclid-algo/65fcb93e-0a1d-4229-b3e6-eb2102c100bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbe0418b7828becb5cb386262f897015ccf34f55 --- /dev/null +++ b/invmod/extended-euclid-algo/65fcb93e-0a1d-4229-b3e6-eb2102c100bd.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long a,b; + + + +void euclid_extins(long long a,long long b,long long &x,long long &y) + +{ + + if(b==0) + + x=1,y=0; + + else + + { + + euclid_extins(b,a%b,x,y); + + long long aux=y; + + y=x-(a/b)*y; + + x=aux; + + } + +} + + + +int main() + +{ + + long long m,n,x,y; + + fin>>n>>m; + + euclid_extins(n,m,x,y); + + if(x<=0) + + x=m+x%m; + + fout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invmod(long long &x,long long &y,int a,int b){ + + if(b==0) + + x=1,y=0; + + else{ + + invmod(x,y,b,a%b); + + long long c=x; + + x=y; + + y=c-y*(a/b); + + } + +} + +int main(){ + + long long a,b,x=0,y=0; + + fin>>a>>b; + + invmod(x,y,a,b); + + if(x<=0){ + + x=b+x%b; + + } + + fout< +#include + + +using namespace std; + + + +void euclid(long long &x, long long &y, long long a, long long b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + euclid(x,y,b,a%b); + + int aux=x; + + x=y; + + y=aux -y*(a/b); + + + + } + +} + + + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + + + long long k=0,l=0,a,n; + + + + f>>a>>n; + + euclid(k,l,a,n); + + if(k<=0) + + k=n+k%n; + + g< +long long A,N; + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void P(long long &x,long long &y, int a, int b) + +{ + + if(b==0) x=1,y=0; + + else + + { + + P(x,y,b,a%b); + + long long o=x; + + x=y; + + y=o-y*(a/b); + + } + +} + + + +int main() + +{ + + long long i=0,j; + + fin>>A>>N; + + P(i,j,A,N); + + if (i<=0) + + i=N+i%N; + + fout< +using namespace std; + + + +void euclid(int a, int b, long long &x, long long &y){ + + if(b == 0){ + + x = 1; y = 0; + + return ; + + } + + + + long long x0, y0; + + euclid(b, a % b, x0, y0); + + x = y0; + + y = x0 - 1LL * (a / b) * y0; + +} + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + int a, MOD; + + scanf("%d%d", &a, &MOD); + + + + long long inv_mod, aux; + + euclid(a, MOD, inv_mod, aux); + + + + inv_mod = (inv_mod % MOD + MOD) % MOD; + + printf("%d", inv_mod); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/684538a6-c6a5-4a56-886c-0a30ce1685ee.cpp b/invmod/extended-euclid-algo/684538a6-c6a5-4a56-886c-0a30ce1685ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6dc51aff6571076038bea29c464e4f80a11bfed --- /dev/null +++ b/invmod/extended-euclid-algo/684538a6-c6a5-4a56-886c-0a30ce1685ee.cpp @@ -0,0 +1,14 @@ +#include +int a,n,x,y; +inline void E(int &x,int &y,int a,int b) +{ + int z; + if(!b) + x=1,y=0; + else + E(x,y,b,a%b),z=x,x=y,y=z-y*(a/b); +} +int main() +{ + freopen("inversmodular.in","r",stdin),freopen("inversmodular.out","w",stdout),scanf("%d%d",&a,&n),E(x,y,a,n),printf("%d",x<=0?(n+x%n):x); +} diff --git a/invmod/extended-euclid-algo/6a9a654c-a5b2-43f7-8c7b-2d324c998d22.cpp b/invmod/extended-euclid-algo/6a9a654c-a5b2-43f7-8c7b-2d324c998d22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e89049398a6d685b4d90c5605af6e8f5a6e4b7bb --- /dev/null +++ b/invmod/extended-euclid-algo/6a9a654c-a5b2-43f7-8c7b-2d324c998d22.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int A,N,x,y; + + + +void cmmdc(int a, int b, int &x, int &y) { + + if (b==0){ + + x=1; + + y=0; + + } + + else{ + + int x0,y0; + + cmmdc(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + + + +int main() { + + fin>>A>>N; + + cmmdc(A,N,x,y); + + x%=N; + + if (x<0) + + x+=N; + + fout< + + +using namespace std; + + + +void gcd(int a, int b, int& x, int& y) { + + if (!b) { + + x = 1; + + y = 0; + + } else { + + int x0, y0; + + gcd(b, a % b, x0, y0); + + x = y0; + + y = x0 - a / b * y0; + + } + +} + + + +int main() { + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + int A, N; + + + + fin >> A >> N; + + + + int inv, y; + + + + gcd(A, N, inv, y); + + + + while (inv < 0) + + inv += N; + + + + fout << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/6dad3266-957c-4a20-97ef-668b891000ab.cpp b/invmod/extended-euclid-algo/6dad3266-957c-4a20-97ef-668b891000ab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd3a6dea13ca002e07495aec18eff2e4a5a2960d --- /dev/null +++ b/invmod/extended-euclid-algo/6dad3266-957c-4a20-97ef-668b891000ab.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +void euclid (int a, int b, long long &x, long long &y) +{ + if (b == 0) + { + x = 1; + y = 0; + } + else + { + long long x1, y1; + euclid(b, a%b, x1, y1); + x = y1; + y = x1 - y1 * (a/b); + } +} + +int main() +{ + + + int a, n; + fin >> a >> n; + long long r, g; + euclid(a, n, r, g); + if (r < 0) + r = (r+n)%n; + fout << r%n; + return 0; +} diff --git a/invmod/extended-euclid-algo/6f1f1b79-cd31-4101-a647-457629458c40.cpp b/invmod/extended-euclid-algo/6f1f1b79-cd31-4101-a647-457629458c40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50b760e85d3c62a1ad318479fbc7c755559c0e6b --- /dev/null +++ b/invmod/extended-euclid-algo/6f1f1b79-cd31-4101-a647-457629458c40.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + + + +ifstream f ("inversmodular.in"); + +ofstream g ("inversmodular.out"); + + + +long long A, N, inv, ins; + + + +void gcd (long long a, long long b, long long &x, long long &y) { + + if (!b) { + + x = 1, y = 0; + + } + + + + else { + + gcd (b, a % b, x, y); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() { + + f >> A >> N; + + + + gcd (A, N, inv, ins); + + if (inv < 0) { + + inv = inv % N + N; + + } + + + + inv %= N; + + g << inv << '\n'; + +} diff --git a/invmod/extended-euclid-algo/6f224d1d-ec4e-46a8-9fe6-d5f25da01bf6.cpp b/invmod/extended-euclid-algo/6f224d1d-ec4e-46a8-9fe6-d5f25da01bf6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..602273a040b0ad95d4fd66964e8962398ea60293 --- /dev/null +++ b/invmod/extended-euclid-algo/6f224d1d-ec4e-46a8-9fe6-d5f25da01bf6.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void Euclid(int a, int b, int &d, int &x, int &y) + +{if(b==0) + + {d=a; x=1; y=0;} + + else {int x0,y0; + + Euclid(b,a%b,d,x0,y0); + + x=y0; y=x0-a/b*y0; + + } + +} + + + + + +int main() + +{int A,N,d,x,y; + + fin>>A>>N; + + Euclid(A,N,d,x,y); + + if(x<0) x+=N; + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + + + +void gcd(int a, int b, long long& x, long long& y) { + + if (a == 0) { + + x = 0; + + y = 1; + + return; + + } + + + + long long x1, y1; + + + + gcd(b % a, a, x1, y1); + + + + x = y1 - (b / a) * x1; + + y = x1; + +} + + + +int main() { + + int A, N; + + + + fin >> A >> N; + + + + long long inv = 0, ins; + + + + gcd(A, N, inv, ins); + + + + if (inv < 0) + + inv = N + inv % N; + + + + fout << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/70314b28-e317-4e34-a1a5-e54a52f628d3.cpp b/invmod/extended-euclid-algo/70314b28-e317-4e34-a1a5-e54a52f628d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99bba0d34bfd14035202d8003314fa9b51515fd5 --- /dev/null +++ b/invmod/extended-euclid-algo/70314b28-e317-4e34-a1a5-e54a52f628d3.cpp @@ -0,0 +1,66 @@ +#include + + +using namespace std; + +int euclid(int a, int b ,long long &x,long long &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + return a; + + } + + long long x1,y1; + + int na; + + na= euclid(b,a%b,x1,y1); + + x=y1; + + y=x1-(a/b)*y1; + + return na; + +} + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + int t; + + int a,b,c; + + c=1; + + scanf("%d%d",&a,&b); + + long long x=0,y; + + int na; + + na= euclid(a,b,x,y); + + if(x<=0) + + x=b+x%b; + + printf("%lld",x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/71c97686-3398-4542-974c-b099cef83db0.cpp b/invmod/extended-euclid-algo/71c97686-3398-4542-974c-b099cef83db0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65ddb65929e04fd535837f5a027873227684a931 --- /dev/null +++ b/invmod/extended-euclid-algo/71c97686-3398-4542-974c-b099cef83db0.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +void euclid_ext(long long a, long long b, long long& d, long long& x, long long& y) { + + if (b == 0) { + + d = a; + + x = 1; y = 0; + + } + + else { + + long long x0, y0; + + euclid_ext(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + + + +int main() { + + + + long long a, b, d, x, y; + + cin >> a >> b; + + euclid_ext(a, b, d, x, y); + + x = (x % b + b) % b; + + cout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/725b4209-aa12-4998-9944-2b17f1f7b905.cpp b/invmod/extended-euclid-algo/725b4209-aa12-4998-9944-2b17f1f7b905.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2a117e78418d8f8393044f51323491383037ca3 --- /dev/null +++ b/invmod/extended-euclid-algo/725b4209-aa12-4998-9944-2b17f1f7b905.cpp @@ -0,0 +1,65 @@ +#include +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void euclid(int a, int b, int &c, int &x, int &y) + +{ + + if(b == 0){ + + c = a; + + x = 1; + + y = 0; + + } + + else{ + + int x0, y0; + + euclid(b, a%b, c, x0, y0); + + x = y0; + + y = x0 - (a/b)*y0; + + } + +} + + + +int main() + +{ + + int a, n, c, x, y; + + fin >> a >> n; + + euclid(a, n, c, x, y); + + + + while(x < 0) x += n; + + + + fout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7583071e-9a26-4ff5-a421-c71d3256539e.cpp b/invmod/extended-euclid-algo/7583071e-9a26-4ff5-a421-c71d3256539e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3ddc7e1fbc20c05997695d8eb3fa42844884807 --- /dev/null +++ b/invmod/extended-euclid-algo/7583071e-9a26-4ff5-a421-c71d3256539e.cpp @@ -0,0 +1,48 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long int n,a,x,y,d; + +void cmmdc(long long int,long long int,long long int&,long long int&); + +int main() + +{ + + fin>>a>>n; + + long long int x,y; + + cmmdc(a,n,x,y); + + x=x%n; + + if(x<0)x+=n; + + fout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a,n,x,y,d; + +int cmmdc(int a,int b,int &x,int &y) { + + if (b==0) { + + x=1, y=0; + + return a; + + } + + else { + + int x0,y0; + + int d=cmmdc(b,a%b,x0,y0); + + x=y0, y=x0-(a/b)*y0; + + return d; + + } + +} + +int main() { + + fin>>a>>n; + + d=cmmdc(a,n,x,y); + + if (x<0) + + x+=n*(x/n+1); + + fout< +#include +#include +using namespace std; + + + +void euclid_e(int a,int b,int& x,int& y,int& d){ + + if(b==0) { + + d=a; + + x=1; + + y=0; + + return; + + } + + int xx,yy,q=a/b; + + euclid_e(b,a%b,xx,yy,d); + + x=yy; + + y=xx-yy*q; + +} + + + +int main(){ + + int a,n,x,y,d; + + FILE *fin,*fout; + + fin=fopen("inversmodular.in","r"); + + fout=fopen("inversmodular.out","w"); + + fscanf(fin,"%d%d",&a,&n); + + + + euclid_e(a,n,x,y,d); + + x%=n; + + if(x<0){ + + x+=n; + + } + + + + fprintf(fout,"%d",x); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/79285f1f-6cbb-4ab2-a848-74a4e1cda30b.cpp b/invmod/extended-euclid-algo/79285f1f-6cbb-4ab2-a848-74a4e1cda30b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19d9f7a88407c8927d6fe0edca9bf1476a42856f --- /dev/null +++ b/invmod/extended-euclid-algo/79285f1f-6cbb-4ab2-a848-74a4e1cda30b.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + +void cmmdc(int a,int b,int &x,int &y) + +{ + + if(!b) + + { + + x=1; + + y=0; + + return ; + + } + + int x0,y0; + + cmmdc(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + +} + +int main() + +{ + + int a,n,x,y; + + ifstream in("inversmodular.in"); + + ofstream out("inversmodular.out"); + + in>>a>>n; + + cmmdc(a,n,x,y); + + if(x<0) + + x=n-abs(x)%n; + + out< +#include + + +using namespace std; + +ifstream f("inversmodular.in"); +ofstream g("inversmodular.out"); + +int a,n,d,x,y; + +void euclid(int a,int b,int& d,int& x,int& y){ + if(b==0){ + d=a; + x=1; + y=0; + return; + }else{ + int x0,y0; + euclid(b,a%b,d,x0,y0); + x=y0; + y=x0-(a/b)*y0; + } +} + +int main(){ + f>>a>>n; + euclid(a,n,d,x,y); + while(x<0){ + x+=n; + } + x=x%n; + g< + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7aa61ab7-c223-4520-bf93-58f55241779c.cpp b/invmod/extended-euclid-algo/7aa61ab7-c223-4520-bf93-58f55241779c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79247126c6dd5a666e4c3f38203634a1dc29465f --- /dev/null +++ b/invmod/extended-euclid-algo/7aa61ab7-c223-4520-bf93-58f55241779c.cpp @@ -0,0 +1,69 @@ +#include +#include + + +using namespace std; + + + +void gcd(long long &x, long long &y, long long a, long long b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + int aux=x; + + x=y; + + y=aux -y*(a/b); + + + + } + +} + + + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + + + long long inv=0,ins=0,a,n; + + + + f>>a>>n; + + gcd(inv,ins,a,n); + + if(inv<=0) + + inv=n+inv%n; + + g< + + + + + + +long long A, N, modular_inverse; + + + + + + + +long long ext_euclid(long long a, long long b) + +{ + + long long old_r = a, r = b; + + long long old_s = 1, s = 0; + + long long old_t = 0, t = 1; + + long long q, new_r, new_s, new_t; + + while (r != 0) + + { + + q = old_r / r; + + new_s = old_s - s * q; + + new_t = old_t - t * q; + + new_r = old_r % r; + + old_r = r, r = new_r; + + old_s = s, s = new_s; + + old_t = t, t = new_t; + + } + + return old_s; + +} + + + + + +int main() + +{ + + std::ifstream fisierIN("inversmodular.in"); + + fisierIN >> A >> N; + + fisierIN.close(); + + modular_inverse = ext_euclid(A, N); + + if (modular_inverse < 0) + + { + + do + + { + + modular_inverse += N; + + } while (modular_inverse < 0); + + } + + std::ofstream fisierOUT("inversmodular.out"); + + fisierOUT << modular_inverse; + + fisierOUT.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7b13d8b8-2bf4-43d0-883c-80c3659eb619.cpp b/invmod/extended-euclid-algo/7b13d8b8-2bf4-43d0-883c-80c3659eb619.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7344626f71dfaed0057df2e8f58c15f7e33db585 --- /dev/null +++ b/invmod/extended-euclid-algo/7b13d8b8-2bf4-43d0-883c-80c3659eb619.cpp @@ -0,0 +1,67 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void euclid_e(int a, int b, long long &x, long long &y) + +{ + + if (b == 0) + + { + + x = 1; + + y = 0; + + return; + + } + + long long xx, yy, q = a / b; + + euclid_e(b, a % b, xx, yy); + + x = yy; + + y = xx - yy * q; + +} + + + + + +int main() + +{ + + int a, n; + + long long x = 0, y; + + fin >> a >> n; + + euclid_e(a, n, x, y); + + x %= n; + + if (x < 0) + + x += n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7b89818a-2c9c-4c9b-8fa3-a41c82c21cb5.cpp b/invmod/extended-euclid-algo/7b89818a-2c9c-4c9b-8fa3-a41c82c21cb5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1cf49ac887f0f518f037c21c5c13aeac5d5ca0c0 --- /dev/null +++ b/invmod/extended-euclid-algo/7b89818a-2c9c-4c9b-8fa3-a41c82c21cb5.cpp @@ -0,0 +1,63 @@ +#include +#include +using namespace std; + + + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int n, m, k, l; + + + +void invers_mod(int a, int b, int &k, int &l) + +{ + + if (!b) + + { + + k=1; + + l=0; + + return; + + } + + int kp, lp; + + invers_mod(b,a%b,kp,lp); + + k=lp; + + l=kp-lp*(a/b); + +} + + + +int main() + +{ + + f >> n >> m; + + invers_mod(n,m,k, l); + + while (k<0) + + k+=m; + + g << k; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7c542281-97d0-4080-8589-33ab734bac6c.cpp b/invmod/extended-euclid-algo/7c542281-97d0-4080-8589-33ab734bac6c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26aebf119fe1b53058c66bc3608ca49b53ed4897 --- /dev/null +++ b/invmod/extended-euclid-algo/7c542281-97d0-4080-8589-33ab734bac6c.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a,b,x,y; + +long long nsh(long long a,long long b,long long&x,long long&y) + +{ + + if(b==0) + + { + + x=1;y=0;return a; + + } + + long long x0,y0,d; + + d=nsh(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + return d; + +} + +int main() + +{ + + f>>a>>b; + + nsh(a,b,x,y); + + if(x<0)x=b+x%b; + + g< + + +using namespace std; + + + +struct EuclidExtins { + + int x; + + int y; + +}; + + + +EuclidExtins euclid_extins(int a, int b) { + + if (b == 0) + + return {1, 0}; + + + + EuclidExtins lst; + + lst = euclid_extins(b, a % b); + + + + return {lst.y, lst.x - (a / b) * lst.y}; + +} + + + +int main() { + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + int a, n; + + scanf("%d%d", &a, &n); + + + + + + EuclidExtins e; + + e = euclid_extins(a, n); + + + + int ans; + + ans = e.x; + + while (ans < 0) + + ans += n; + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/7cf6e45b-d46c-4e8f-8310-357c8096d731.cpp b/invmod/extended-euclid-algo/7cf6e45b-d46c-4e8f-8310-357c8096d731.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36eb438046d637f671f1a939bebf3103ad340e77 --- /dev/null +++ b/invmod/extended-euclid-algo/7cf6e45b-d46c-4e8f-8310-357c8096d731.cpp @@ -0,0 +1,46 @@ +#include +std::ifstream input (std::string("inversmodular")+".in"); + +std::ofstream output(std::string("inversmodular")+".out"); + + + +int gcd(long long A, long long B, long long &X, long long &Y) { + + if (B == 0) { + + X = 1; + + Y = 0; + + return A; + + } int v = gcd(B, A%B, X, Y); + + long long aux = Y; + + Y = X - (A/B)*Y; + + X = aux; + + return v; + +} + + + +int main() + +{ + + long long A, B; input >> A >> B; + + long long X, Y; gcd(A, B, X, Y); + + output << (X%B+B)%B; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/812a891c-ffd0-4be3-b4ce-a77f0e284293.cpp b/invmod/extended-euclid-algo/812a891c-ffd0-4be3-b4ce-a77f0e284293.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbba804fbf0e7d13c5141bc8cab8e586ac9f12d9 --- /dev/null +++ b/invmod/extended-euclid-algo/812a891c-ffd0-4be3-b4ce-a77f0e284293.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a){x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + +int main() + +{ ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + int n, a, b, c, x, y; + + fin>>a>>n; + + eulcid_eXt(a, n, 1, x, y); + + x=((long long)n+x%n)%n; + + fout< + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +long long ext_gcd(long long a, long long b, long long & x, long long & y) + +{ + + if(b == 0) { + + x = 1; + + y = 0; + + return a; + + } + + long long x1, y1; + + long long d = ext_gcd(b, a % b, x1, y1); + + y = x1 - (a / b) * y1; + + x = y1; + + return d; + +} + + + +int main() + +{ + + usain_bolt(); + + + + long long a, n; + + + + fin >> a >> n; + + + + long long x, y; + + ext_gcd(a, n, x, y); + + x = (x + n) % n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/82cde410-4071-495b-8e1a-d89d6a3fa03c.cpp b/invmod/extended-euclid-algo/82cde410-4071-495b-8e1a-d89d6a3fa03c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e076a19a11e12fc0c214109e2ddf951f12f8c35f --- /dev/null +++ b/invmod/extended-euclid-algo/82cde410-4071-495b-8e1a-d89d6a3fa03c.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a,n; + +void invmd(long long &inv,long long &ins,int a,int n) + +{ + + if(n==0) + + { + + inv=1; + + ins=0; + + } + + else{ + + invmd(inv,ins,n,a%n); + + long long aux=inv; + + inv=ins; + + ins=aux-ins*(a/n); + + } + +} + +int main() + +{ + + fin>>a>>n; + + long long inv=0,ins; + + invmd(inv,ins,a,n); + + if(inv<=0) + + inv=n+inv%n; + + fout< +using namespace std; + + + +int n, m; + +int inv(int a, int b){ + + return (a > 1 ? b - 1LL*inv(b%a, a)*b/a : 1); + +} + + + +int main(){ + + ifstream cin ("inversmodular.in"); + + ofstream cout ("inversmodular.out"); + + cin >> n >> m; + + cout << inv(n, m); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/8469ad75-1bb3-4529-864e-54a7e185e3c2.cpp b/invmod/extended-euclid-algo/8469ad75-1bb3-4529-864e-54a7e185e3c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..480b7f25b48442247b4e403bb37229b13e654c77 --- /dev/null +++ b/invmod/extended-euclid-algo/8469ad75-1bb3-4529-864e-54a7e185e3c2.cpp @@ -0,0 +1,75 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ( "inversmodular.in" ); + +ofstream fout ( "inversmodular.out" ); + + + +void euclid_extins( int a, int b, int& x, int& y, int& d ) { + + if ( b == 0 ) { + + d = a; + + x = 1; + + y = 0; + + return; + + } + + int xx, yy, q = a / b; + + euclid_extins( b, a % b, xx, yy, d ); + + x = yy; + + y = xx - yy * q; + +} + + + +int cmmdc( int a, int b ) { + + if ( b == 0 ) + + return a; + + return cmmdc( b, a % b ); + +} + + + +int main() { + + int a, n, x, y, d; + + fin >> a >> n; + + d = cmmdc( a, n ); + + euclid_extins( a, n, x, y, d ); + + x %= n; + + if ( x < 0 ) { + + x += n; + + } + + fout << x << "\n"; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/84ff6461-f9e0-4eb0-8907-3779b8ef89da.cpp b/invmod/extended-euclid-algo/84ff6461-f9e0-4eb0-8907-3779b8ef89da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d82670527d275418716b6e26a35af121e7162dd --- /dev/null +++ b/invmod/extended-euclid-algo/84ff6461-f9e0-4eb0-8907-3779b8ef89da.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +long long ext_gcd(long long a, long long b, long long & x, long long & y) + +{ + + if(b == 0) { + + x = 1; + + y = 0; + + return a; + + } + + long long x1, y1; + + long long d = ext_gcd(b, a % b, x1, y1); + + y = x1 - (a / b) * y1; + + x = y1; + + return d; + +} + + + +int main() + +{ + + usain_bolt(); + + + + long long a, n; + + + + fin >> a >> n; + + + + long long x = 0, y = 0; + + ext_gcd(a, n, x, y); + + x = (x + n) % n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/87ecf1b2-05c3-416d-8000-e0eee4c87264.cpp b/invmod/extended-euclid-algo/87ecf1b2-05c3-416d-8000-e0eee4c87264.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3e1a2699188ede5b344bace745cb9fac0d12f5f --- /dev/null +++ b/invmod/extended-euclid-algo/87ecf1b2-05c3-416d-8000-e0eee4c87264.cpp @@ -0,0 +1,73 @@ +#include +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int modinv(int a, int b) + +{ + + long long auxb = b, q, x1 = 1, x2 = 0, aux; + + + + while(b) + + { + + q = a/b; + + aux = a; + + a = b; + + b = aux - q * b; + + aux = x1; + + x1 = x2; + + x2 = aux - q * x2; + + } + + if(x1 < 0) + + { + + x1 += auxb; + + } + + return x1; + +} + + + + + +int main() + +{ + + int x, y; + + fin>>x>>y; + + fout< + +using namespace std; +long long int a,n; +ifstream in ("inversmodular.in"); +ofstream out ("inversmodular.out"); +void euclid(long long &x, long long &y, int a, int b) +{ + if (!b){ + x = 1; + y = 0; + }else + { + euclid(x, y, b, a % b); + long long cx = x; + x = y; + y = cx - y * (a / b); + } +} +int main() +{ + long long int x0,y0; + in>>a>>n; + euclid(x0,y0,a ,n); + if(x0<=0){ + x0=n+x0%n; + } + out< +#include + +using namespace std; + +ifstream in("inversmodular.in"); +ofstream out("inversmodular.out"); + +typedef long long ll; + +int a, mod; + +void inv_mod(ll &inv, ll &ins, int a, int b) { + if(b == 0) { + inv = 1; + ins = 0; + } else { + inv_mod(inv, ins, b, a % b); + swap(inv, ins); + ins -= inv * (a / b); + } +} + +int main() +{ + in >> a >> mod; + + ll inv = 0, ins; + inv_mod(inv, ins, a, mod); + + if(inv <= 0) + inv = (inv + mod) % mod; + + out << inv << '\n'; + + in.close(); + out.close(); + return 0; +} diff --git a/invmod/extended-euclid-algo/886ba17e-ff1c-4746-9e14-81d2151ff0fb.cpp b/invmod/extended-euclid-algo/886ba17e-ff1c-4746-9e14-81d2151ff0fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5446baeaafdeae13303fc7bd4e19f6677c9d44a8 --- /dev/null +++ b/invmod/extended-euclid-algo/886ba17e-ff1c-4746-9e14-81d2151ff0fb.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + + + +ifstream fin ( "inversmodular.in" ); + +ofstream fout ( "inversmodular.out" ); + + + +void gcda ( int a, int b, pair &xy ) + +{ + + if ( b == 0 ) { xy = {1, 0}; return; } + + gcda (b, a%b, xy); + + xy = {xy.second, xy.first - (a/b)*xy.second}; + +} + + + +int main () + +{ + + int a, n; fin >> a >> n; + + pair xy; + + gcda (a, n, xy); + + if (xy.first < 0) xy.first += ((-xy.first)/n+1)*n; + + fout << xy.first % n; + + fin.close (); fout.close (); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/8a189554-4d51-4cd4-952e-f5cd067fde19.cpp b/invmod/extended-euclid-algo/8a189554-4d51-4cd4-952e-f5cd067fde19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdc86bf746e1769a59fa0f25a4439672920d4e3a --- /dev/null +++ b/invmod/extended-euclid-algo/8a189554-4d51-4cd4-952e-f5cd067fde19.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + + + +ifstream inf("inversmodular.in"); + +ofstream outf("inversmodular.out"); + + + +int modinv(int a, int b) { + + long long x1 = 1, x2 = 0, q, aux, auxb = b; + + while(b) { + + q = a / b; + + aux = b; + + b = a - q * b; + + a = aux; + + aux = x2; + + x2 = x1 - q * x2; + + x1 = aux; + + } + + if(x1 < 0) { + + x1 += auxb; + + } + + return x1; + +} + + + +int main() { + + int a, b; + + inf >> a >> b; + + outf << modinv(a, b); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/8a8485ef-4f2e-4190-a91d-16e785c95a21.cpp b/invmod/extended-euclid-algo/8a8485ef-4f2e-4190-a91d-16e785c95a21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09ffd92ed673a3c45d154c2bef759d11d22a97b9 --- /dev/null +++ b/invmod/extended-euclid-algo/8a8485ef-4f2e-4190-a91d-16e785c95a21.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +void exte(int a, int b, int& m, int& n){ + + if(!b){ + + m=1; + + n=0; + + return; + + } + + int M,N; + + exte(b,a%b,M,N); + + m=N; + + n=M-N*(a/b); + + + +} + + + +int main() + +{ + + int a,n,x,y; + + fin>>a>>n; + + exte(a,n,x,y); + + while(x<0) + + x+=n; + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); +void euclid(long long a, long long b, long long& d, long long& x, long long& y) { + + if (b == 0) { + + d = a; + + x = 1; y = 0; + + return; + + } + + + + long long x0, y0; + + euclid(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + +} + + + + + + + +int main() { + + + + long long a, n, d, x, y; + + cin >> a >> n; + + + + euclid(a, n, d, x, y); + + + + x = (x % n + n) % n; + + cout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/8b5407a4-d88d-4793-9b49-4a2b0bcf9811.cpp b/invmod/extended-euclid-algo/8b5407a4-d88d-4793-9b49-4a2b0bcf9811.cpp new file mode 100644 index 0000000000000000000000000000000000000000..287318d5078c9186f38cb3d55f6d5246a665bf65 --- /dev/null +++ b/invmod/extended-euclid-algo/8b5407a4-d88d-4793-9b49-4a2b0bcf9811.cpp @@ -0,0 +1,69 @@ +#include +#include + + +using namespace std; + + + +void euclid(long long &x, long long &y, long long a, long long b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + euclid(x,y,b,a%b); + + int aux=x; + + x=y; + + y=aux -y*(a/b); + + + + } + +} + + + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + + + long long k=0,l=0,a,n; + + + + f>>a>>n; + + euclid(k,l,a,n); + + if(k<=0) + + k=n+k%n; + + g< + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/8ce2299e-2db6-4beb-b5c2-f6ce3876bf88.cpp b/invmod/extended-euclid-algo/8ce2299e-2db6-4beb-b5c2-f6ce3876bf88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf45e19f9eec06d8af26f1923a23f93e0fc440da --- /dev/null +++ b/invmod/extended-euclid-algo/8ce2299e-2db6-4beb-b5c2-f6ce3876bf88.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +struct str{ + +int d; + +int x0; + +int y0; + +}; + + + +str euclid(int a, int b){ + + if(b==0) + + return {a, 1, 0}; + + str d=euclid(b, a%b); + + return {d.d, d.y0, d.x0-(a/b) * d.y0}; + +} + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int main() + +{ + + int a, n; + + f>>a>>n; + + str rez=euclid(a,n); + + rez.x0 %=n; + + while(rez.x0<0){ + + rez.x0+=n;} + + g< +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + ios::sync_with_stdio(false); + + long long inv = 0, ins; + + fin>>A>>N; + + fin.close(); + + gcd(inv, ins, A, N); + + if (inv <= 0) + + inv = N + inv % N; + + fout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a,b,c,n,t,x,y,d; + +int euextins(int a, int b, int &x, int &y) { + + if (b == 0) { + + x = 1; + + y = 0; + + return a; + + } + + else { + + int xa, ya; + + int r = euextins(b, a%b, xa, ya); + + x = ya; + + y = xa - ya*(a/b); + + return r; + + + + } + + + +} + + + +int main() { + + fin>>a>>n; + + c=euextins(a,n,x,y); + + x%=n; + + if(x<0){ + + x+=n; + + + + } + + + + + + fout< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a,n,x,y; + + + +void euclid_ext(int a,int n,int &x,int &y) + +{ + + int x0,y0; + + if(n==0) + + { + + x=1; + + y=0; + + return; + + } + + euclid_ext(n,a%n,x0,y0); + + x=y0; + + y=x0-(a/n)*y0; + +} + +int main() + +{ + + fin>>a>>n; + + euclid_ext(a,n,x,y); + + while(x<0) + + x+=n; + + fout << x << endl; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/9070352f-d893-4850-9124-e5c4abcf0102.cpp b/invmod/extended-euclid-algo/9070352f-d893-4850-9124-e5c4abcf0102.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbe8ba3cbaa9c364471f9b31d861327464e3ab28 --- /dev/null +++ b/invmod/extended-euclid-algo/9070352f-d893-4850-9124-e5c4abcf0102.cpp @@ -0,0 +1,72 @@ +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +int Euclid(long long a, long long b, long long &x, long long &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + return a; + + } + + else + + { + + long long x0,y0; + + long long d=Euclid(b,a%b,x0,y0); + + x=y0; + + y=x0-1LL*(a/b)*y0; + + return d; + + } + +} + + + +int main() + +{ + + long long a,n; + + in>>a>>n; + + long long x,y,d; + + d=Euclid(a,n,x,y); + + if(x<0) x=n+x%n; + + out< +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int N,A,x,y; + + + +void Euclid(int a,int b,int &x,int &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + return; + + } + + else + + { + + int x0,y0; + + Euclid(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + + + +int main() + +{ + + f>>A>>N; + + Euclid(A,N,x,y); + + if(x<0) g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int A, N; + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + f >> A >> N; + + gcd(inv, ins, A, N); + + if (inv <= 0) + + inv = N + inv % N; + + g << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/91090262-36c7-4d91-bc45-f3128bf24395.cpp b/invmod/extended-euclid-algo/91090262-36c7-4d91-bc45-f3128bf24395.cpp new file mode 100644 index 0000000000000000000000000000000000000000..975a9edd9aaa7f19184c0c110ea102892ecf88ed --- /dev/null +++ b/invmod/extended-euclid-algo/91090262-36c7-4d91-bc45-f3128bf24395.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + + + +void gcd(int a, int b, long long& x, long long& y) { + + if (a == 0) { + + x = 0; + + y = 1; + + return; + + } + + + + long long x1, y1; + + + + gcd(b % a, a, x1, y1); + + + + x = y1 - (b / a) * x1; + + y = x1; + +} + + + +int main() { + + int A, N; + + + + fin >> A >> N; + + + + long long inv, ins; + + + + gcd(A, N, inv, ins); + + + + if (inv < 0) + + inv = N + inv % N; + + + + fout << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/9188a6ae-7628-463a-9489-1f932e484c65.cpp b/invmod/extended-euclid-algo/9188a6ae-7628-463a-9489-1f932e484c65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c92e591b4a60fdf8519840d4c9dd898cde8c2b48 --- /dev/null +++ b/invmod/extended-euclid-algo/9188a6ae-7628-463a-9489-1f932e484c65.cpp @@ -0,0 +1,79 @@ +#include +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + +long long A,N,x,y; + + + +void invmod(long long a,long long b,long long &x,long long &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + return; + + } + + long long X,Y; + + invmod(b,a%b,X,Y); + + x=Y; + + y=X-(a/b)*Y; + + + +} + + + + + +int main() + +{ + + + + fin>>A>>N; + + + + invmod(A,N,x,y); + + + + while(x<0) + + x+=N; + + + + fout< +using namespace std; + +void euclid_e ( int a, int b, int &x, int &y, int &d ) { + + if ( b == 0 ) { + + d = a, x = 1, y = 0; + + return; + + } + + int x1, y1, q = a / b; + + euclid_e ( b, a % b, x1, y1, d ); + + x = y1, y = x1 - y1 * q; + +} + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int a, n, x, y, d; + + fin >> a >> n; + + euclid_e( a, n, x, y, d ); + + x %= n; + + if( x < 0 ) + + x+= n; + + fout << x; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/92707a38-118d-4c18-8479-eb2dd6988e46.cpp b/invmod/extended-euclid-algo/92707a38-118d-4c18-8479-eb2dd6988e46.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ec3f1fb9c0bba68903bc9279d0e03c5a58fcbd3 --- /dev/null +++ b/invmod/extended-euclid-algo/92707a38-118d-4c18-8479-eb2dd6988e46.cpp @@ -0,0 +1,66 @@ +#include + + + + +void euclidExtins(long long a, long long b, long long& d, long long& x, long long& y) + +{ + + if (!b) + + { + + x = 1; + + y = 0; + + d = a; + + } + + else + + { + + euclidExtins(b, a % b, d, x, y); + + long long x0 = x; + + long long y0 = y; + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int main() + +{ + + FILE* fin = fopen("inversmodular.in", "r"); + + FILE* fout = fopen("inversmodular.out", "w"); + + + + long long a, b, d=0; + + fscanf(fin, "%lld%lld", &a, &b); + + long long x = 0, y = 0; + + euclidExtins(a, b, d, x, y); + + if(x>=0) + + fprintf(fout, "%lld",x); + + else + + fprintf(fout, "%lld", b+x%b); + +} diff --git a/invmod/extended-euclid-algo/9303955b-29da-4dd3-8c22-ac2043fa1ab8.cpp b/invmod/extended-euclid-algo/9303955b-29da-4dd3-8c22-ac2043fa1ab8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d90ca6371718e9e6ad18f99c6400a69af531e1f --- /dev/null +++ b/invmod/extended-euclid-algo/9303955b-29da-4dd3-8c22-ac2043fa1ab8.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; + + + +int t, a, n, c, x, y; + + + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + + + +int main() + +{ + + FILE *f = fopen("inversmodular.in", "r"); + + FILE *g = fopen("inversmodular.out", "w"); + + fscanf(f, "%d%d", &a,&n); + + eulcid_eXt(a, n, 1, x, y); + + x=((long long)n+x%n)%n; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/937c6948-0961-474b-9319-82b5ecd7d65a.cpp b/invmod/extended-euclid-algo/937c6948-0961-474b-9319-82b5ecd7d65a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c30857ab8cbc731503216ffbb1aefda8256059d0 --- /dev/null +++ b/invmod/extended-euclid-algo/937c6948-0961-474b-9319-82b5ecd7d65a.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +long long n; + +void euclid (int a, int b, long long &x, long long &y) +{ + if (b == 0) + { + x = 1; + y = 0; + } + else + { + long long x1, y1; + euclid(b, a%b, x1, y1); + x = y1; + y = x1 - y1 * (a/b); + x = (x+n)%n; + y = (y+n)%n; + } +} + +int main() +{ + + + int a; + fin >> a >> n; + long long r, g; + euclid(a, n, r, g); + fout << r; + return 0; +} diff --git a/invmod/extended-euclid-algo/95602ce7-9836-4630-9fb0-6816cccf9cfe.cpp b/invmod/extended-euclid-algo/95602ce7-9836-4630-9fb0-6816cccf9cfe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..226203f19e4319fc38ff4ff7d905df4d99347254 --- /dev/null +++ b/invmod/extended-euclid-algo/95602ce7-9836-4630-9fb0-6816cccf9cfe.cpp @@ -0,0 +1,85 @@ +#include + + +using namespace std; + + + + + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int Quick(int a, int b, int mod) + +{ + + int p=1; + + while (b>0) + + { + + if (b%2==1) p=1ll*p*a%mod; + + b/=2; + + a=1ll*a*a%mod; + + } + + return p; + +} + +int Phi(int n) + +{ + + int sol,p; + + sol=n; + + for (p=2; n>1 and p*p<=n; p++) + + { + + if (n%p==0) + + { + + sol=sol/p*(p-1); + + while (n%p==0) + + n/=p; + + } + + } + + if (n>1) sol=sol/n*(n-1); + + return sol; + +} + + + + + +int main() + +{ + + int a,n; + + fin >> a >> n; + + fout << (Quick(a,(Phi(n)-1),n)); + return 0; + +} diff --git a/invmod/extended-euclid-algo/95f4954f-f244-4c79-a374-0045dd5f82ee.cpp b/invmod/extended-euclid-algo/95f4954f-f244-4c79-a374-0045dd5f82ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cf1afc1e8b0eb94105597e3cbc994cc245745b7 --- /dev/null +++ b/invmod/extended-euclid-algo/95f4954f-f244-4c79-a374-0045dd5f82ee.cpp @@ -0,0 +1,66 @@ +#include + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/9756d4c5-fc55-43ba-91f6-6c0592f083e0.cpp b/invmod/extended-euclid-algo/9756d4c5-fc55-43ba-91f6-6c0592f083e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7379dfe2221b4a5e6b20b2129ad977a68a50812 --- /dev/null +++ b/invmod/extended-euclid-algo/9756d4c5-fc55-43ba-91f6-6c0592f083e0.cpp @@ -0,0 +1,34 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in") ; + +ofstream fout("inversmodular.out") ; + + + +long long inv(int a,int b) + +{ + + return 1 < a ? b-inv(b%a,a)*b/a : 1 ; + +} + + + +int main() + +{ + + int a , b ; + + fin >> a >> b; + + fout << inv(a,b) ; + +} diff --git a/invmod/extended-euclid-algo/9a6ccfe6-da55-48ae-9a28-8bd607626241.cpp b/invmod/extended-euclid-algo/9a6ccfe6-da55-48ae-9a28-8bd607626241.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c3db3304edc67dff3b20ed3693d6d674ad8455d --- /dev/null +++ b/invmod/extended-euclid-algo/9a6ccfe6-da55-48ae-9a28-8bd607626241.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + +FILE *f,*g; + + + +void im(long long a, long long b, long long &x, long long &y) + +{ + + if(!b) + + x=1,y=0; + + else + + { + + im(b,a%b,x,y); + + int cop=x; + + x=y; + + y=cop-a/b*y; + + } + +} + +int main() + +{ + + f=fopen("inversmodular.in","r"); + + g=fopen("inversmodular.out","w"); + + long long a,mod,x,y; + + fscanf(f,"%lld %lld",&a,&mod); + + im(a,mod,x,y); + + if(x<=0) + + x=mod+x%mod; + + fprintf(g,"%lld",x); + + fclose(f); + + fclose(g); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/9c4be10d-2197-4dd7-8f66-85a7958eefa6.cpp b/invmod/extended-euclid-algo/9c4be10d-2197-4dd7-8f66-85a7958eefa6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98b39a4e3efb67eadf1b68aaf34f71d2a2344417 --- /dev/null +++ b/invmod/extended-euclid-algo/9c4be10d-2197-4dd7-8f66-85a7958eefa6.cpp @@ -0,0 +1,66 @@ +#include + + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + +int main() + +{ + + FILE *f = fopen("inversmodular.in", "r"); + + FILE *g = fopen("inversmodular.out", "w"); + + int t, a, b, c, x, y; + + fscanf(f, "%d%d", &a,&b); + + eulcid_eXt(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/9c71ecf6-a639-4454-83e3-19f7eea6f1d8.cpp b/invmod/extended-euclid-algo/9c71ecf6-a639-4454-83e3-19f7eea6f1d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6b34c4a984f7bda49ff3e5140d4ee1cb67f0282 --- /dev/null +++ b/invmod/extended-euclid-algo/9c71ecf6-a639-4454-83e3-19f7eea6f1d8.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +ifstream f("inversmodular.in"); +ofstream g("inversmodular.out"); +typedef pair p; +p euclid(int a , int b, int c) +{ + if(b == 0) + { + if(c%a)return make_pair(0, 0); + else return make_pair(c/a, 0); + } + else + { + p rez = euclid(b, a%b, c); + return make_pair(rez.second, rez.first - a/b * rez.second); + } +} +int main() { + int a , b; + f>>a>>b; + auto rez = euclid(a, b, 1); + int r = rez.first; + if(r<0)r+=b; + g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int n, k; + +int inv, ins; + + + +void invers_modular(int a, int b, int &x, int &y) + +{ + + if(!b) + + { + + x = 1; + + y = 0; + + } + + else + + { + + invers_modular(b, a % b, x, y); + + int aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + f>>n>>k; + + invers_modular(n, k, inv, ins); + + while(inv <= 0) + + inv = k + inv % k; + + g< +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("inversmodular.in"); +ofstream fout ("inversmodular.out"); + +int ext_gcd(int a, int b, int &x, int &y) { + if (b == 0) { + x = 1; y = 0; + return a; + } + int x1, y1; + int d = ext_gcd(b, a % b, x1, y1); + x = y1; + y = x1 - a/b * y1; + + return d; +} + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + int a, n; + fin >> a >> n; + int x, y; + ext_gcd(n, a, x, y); + + y %= n; if (y < 0) { y += n; } + fout << y << endl; + + + return 0; +} diff --git a/invmod/extended-euclid-algo/9ec075ed-1e43-475c-975b-7775f1e33855.cpp b/invmod/extended-euclid-algo/9ec075ed-1e43-475c-975b-7775f1e33855.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9502c18c254651743c68cb5c3d30a1d19e1b051a --- /dev/null +++ b/invmod/extended-euclid-algo/9ec075ed-1e43-475c-975b-7775f1e33855.cpp @@ -0,0 +1,58 @@ +#include + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a,M; + +void euclid_extins(int &x,int &y,int a,int b) + +{ + +if(!b) + +{ + +x=1;y=0; + +} + +else + +{ + +euclid_extins(x,y,b,a%b); + +int aux=x; + +x=y; + +y=aux-y*(a/b); + +} + + + +} + + + +int main() + +{int inv=0,ins; + +f>>a>>M; + +euclid_extins(inv,ins,a,M); + +if(inv<=0) inv=M+inv%M; + +g< + +using namespace std; + +void invMod(long long a, long long b, long long &x, long long &y) { + if (!b) + x = 1, y = 0; + else { + long long x0, y0; + + invMod(b, a % b, x0, y0); + + x = y0; + y = x0 - (a / b) * y0; + } +} + +int main() { + ifstream cin("inversmodular.in"); + ofstream cout("inversmodular.out"); + + long long a, n, inv, y; + cin >> a >> n; + + invMod(a, n, inv, y); + if (inv < 0) inv = n + inv % n; + + cout << inv; + return 0; +} diff --git a/invmod/extended-euclid-algo/9f90a7a4-a0c9-4724-8ef1-ee4911ba5da2.cpp b/invmod/extended-euclid-algo/9f90a7a4-a0c9-4724-8ef1-ee4911ba5da2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d76070ea53218f905253b236466d390354f34518 --- /dev/null +++ b/invmod/extended-euclid-algo/9f90a7a4-a0c9-4724-8ef1-ee4911ba5da2.cpp @@ -0,0 +1,23 @@ +#include + +using namespace std; + +ifstream f("inversmodular.in"); +ofstream g("inversmodular.out"); + +long long a, MOD; + +pair cmmdc(long a, long b) +{ + if(b == 0) + return {1, 0}; + auto p = cmmdc(b, a%b); + return {p.second, (p.first - ((a/b) * p.second)%MOD + MOD)%MOD}; +} + +int main() { + f>>a>>MOD; + auto p = cmmdc(a, MOD); + g< +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + { + + x = 1; + + y = 0; + + } + + else + + { + + gcd(x,y,b,a%b); + + int aux = x; + + x = y; + + y = aux - (a/b)*y; + + } + +} + +int main() + +{ + + long long x,y; + + int a,n; + + fin>>a>>n; + + gcd(x,y,a,n); + + if(x<=0) + + x = n + x%n; + + fout< +int n; +int mod(long long a, int m) +{ + return (a + m) % m; + +} +void euclid_extins(int a, int b, int &d, long long &x, long long &y) +{ + if(b == 0) + { + d = a; + x = 1; + y = 0; + return; + } + long long xprim = 0, yprim = 0, q = a / b; + euclid_extins(b, a % b, d, xprim, yprim); + x = yprim , n; + y = xprim - 1LL * yprim * q , n; +} + + +int main() +{ + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + int tests, a, b, c, d; + long long rez1 , rez2; + rez1 = rez2 = 0; + scanf("%d%d", &a,&n); + euclid_extins(a, n, d, rez1, rez2); + printf("%d" , mod(rez1 , n)); +return 0; +} diff --git a/invmod/extended-euclid-algo/a39ce628-1ef2-4ffe-854f-322075450933.cpp b/invmod/extended-euclid-algo/a39ce628-1ef2-4ffe-854f-322075450933.cpp new file mode 100644 index 0000000000000000000000000000000000000000..621fe261360545a5792c1877d2927db86a34fd6f --- /dev/null +++ b/invmod/extended-euclid-algo/a39ce628-1ef2-4ffe-854f-322075450933.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a,n,cmmdc; + + + +pair euclid_extins(int x, int y) + +{ + + if(y==0) + + { + + cmmdc=x; + + return {1,0}; + + } + + auto p=euclid_extins(y,x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + +void read() + +{ + + fin>>a>>n; + + auto p=euclid_extins(a,n); + + fout<<((p.first<0)?(p.first+n):p.first); + +} + +int main() + +{ + + read(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a3dcd693-203e-456d-ae4f-4ad01e71de21.cpp b/invmod/extended-euclid-algo/a3dcd693-203e-456d-ae4f-4ad01e71de21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6c7ea13aab93da92a2d3106336f9b4890fcc047 --- /dev/null +++ b/invmod/extended-euclid-algo/a3dcd693-203e-456d-ae4f-4ad01e71de21.cpp @@ -0,0 +1,80 @@ +#include + + + + + + +long long A, N; + + + +void Euclid(long long A, long long B, long long &X, long long &Y) { + + if (B == 0) { + + Y = 0; + + X = 1; + + return; + + } Euclid(B, A%B, X, Y); + + long long Aux = Y; + + Y = X - (A/B)*Y; + + X = Aux; + +} + + + +long long InvMod(long long A, long long MOD) { + + long long Temp, Inv; + + Euclid(A, MOD, Inv, Temp); + + return (Inv + MOD) % MOD; + +} + + + +std::ifstream In ("inversmodular.in"); + +std::ofstream Out("inversmodular.out"); + + + +void Citire() { + + In >> A >> N; + +} + + + +void Rezolvare() { + + Out << InvMod(A, N); + +} + + + +int main() + +{ + + Citire(); + + Rezolvare(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a3fdc66a-16e8-4c0b-9d9f-abe369c2115b.cpp b/invmod/extended-euclid-algo/a3fdc66a-16e8-4c0b-9d9f-abe369c2115b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43eb841ac5d387da65aa6cc4d9e223412aeec822 --- /dev/null +++ b/invmod/extended-euclid-algo/a3fdc66a-16e8-4c0b-9d9f-abe369c2115b.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n,m,x,y; + +long long euclid(long long a, long long b, long long &x, long long &y){ + + if(b==0){ + + x=1; + + y=0; + + return a; + + } + + long long x1,y1; + + long long d=euclid(b,a%b,x1,y1); + + x=y1; + + y=x1-(a/b)*y1; + + return d; + +} + +int main(){ + + fin>>n>>m; + + euclid(n,m,x,y); + + x%=m; + + if(x<0) + + x+=m; + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + + + +void gcd(int a, int b, long long& x, long long& y) { + + if (a == 0) { + + x = 0; + + y = 1; + + return; + + } + + + + long long x1, y1; + + + + gcd(b % a, a, x1, y1); + + + + x = y1 - (b / a) * x1; + + y = x1; + +} + + + +int main() { + + int A, N; + + + + fin >> A >> N; + + + + long long inv, ins; + + + + gcd(A, N, inv, ins); + + + + if (inv < 0) + + inv = N + inv % N; + + + + fout << inv; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a5676d46-bd2b-4550-8d8d-853a65f51f4b.cpp b/invmod/extended-euclid-algo/a5676d46-bd2b-4550-8d8d-853a65f51f4b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a22876835e05b7369d86d88af1c9534208d9aac --- /dev/null +++ b/invmod/extended-euclid-algo/a5676d46-bd2b-4550-8d8d-853a65f51f4b.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a, b, c, d, x, y,t; + +long long euclid(long long a, long long b, long long &x, long long &y) + +{ + + if(b==0) + + { + + x=1,y=0; + + return a; + + } + + long long d,xa,ya; + + d=euclid(b, a%b, xa, ya); + + x=ya,y=xa-a/b*ya; + + return d; + +} + + + + + +int main() + +{ + + fin>>a>>b; + + d=euclid(a, b, x, y); + + if(x<=0) x+=b; + + fout< +typedef long long ll; + + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void gcd(ll &x, ll &y, int a, int b) + +{ + + if(!b) + + x=1, y=0; + + else + + { + + gcd(x, y, b, a%b); + + ll aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + int a, n; + + ll inv=0, y=0; + + fin >> a >> n; + + gcd(inv, y, a, n); + + if(inv<=0) + + inv=n+inv%n; + + fout << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a6d194e8-79fb-4048-8ff4-3dbd93a91b4c.cpp b/invmod/extended-euclid-algo/a6d194e8-79fb-4048-8ff4-3dbd93a91b4c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6635ce4e2e9cadc5561e8fd05f8b2ff6460651f --- /dev/null +++ b/invmod/extended-euclid-algo/a6d194e8-79fb-4048-8ff4-3dbd93a91b4c.cpp @@ -0,0 +1,80 @@ +#include + + +FILE* FIN=freopen("inversmodular.in","r",stdin); + +FILE* FOUT=freopen("inversmodular.out","w",stdout); + + + +long long n,m; + + + +void cit() + +{ + + scanf("%lld%lld",&n,&m); + +} + +void invmod(long long &x,long long &y,long long z,long long t) + +{ + + if(!t) + + { + + x=1; + + y=0; + + } + + else + + { + + int b; + + invmod(x,y,t,z%t); + + b=x; + + x=y; + + y=b-y*(z/t); + + } + +} + +void af(long long p) + +{ + + p%=m; + + p+=(p<0)*m; + + printf("%lld",p); + +} + +int main() + +{ + + long long p=0,k; + + cit(); + + invmod(p,k,n,m); + + af(p); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a6ee9450-5337-462f-aaf5-d5f64ff52850.cpp b/invmod/extended-euclid-algo/a6ee9450-5337-462f-aaf5-d5f64ff52850.cpp new file mode 100644 index 0000000000000000000000000000000000000000..636fe3bb9df9017cc337a5e5099f6dc734bee248 --- /dev/null +++ b/invmod/extended-euclid-algo/a6ee9450-5337-462f-aaf5-d5f64ff52850.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void cmmdc(int a,int b,int &k,int &l) + +{ int kp,lp; + + if(b==0) + + { + + l=0; + + k=1; + + return; + + } + + cmmdc(b,a%b,kp,lp); + + k=lp; + + l=kp-lp*(a/b); + + + +} + +int main() + +{ int a,x,y,n; + + fin>>a>>n; + + cmmdc(n,a,x,y); + + while(y<0) + + y+=n; + + fout< + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int euclext(int a, int b, int c, int &x, int &y) + +{ + + int q[50], x0, y0, r, pos = 0; + + while(b) + + { + + q[++pos] = a / b; + + r = a % b; + + a = b; + + b = r; + + } + + if(c % a) {x=y=0;return 0;} + + x0 = c / a; + + y0 = 0; + + while(pos) + + { + + x = y0; + + y = x0 - q[pos--] * y0; + + x0 = x; + + y0 = y; + + } + + return 1; + +} + + + +int main() + +{ + + int a, b, x, y; + + fin >> a >> b; + + euclext(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a7bede9a-d6e8-4777-bcd4-498a852f9268.cpp b/invmod/extended-euclid-algo/a7bede9a-d6e8-4777-bcd4-498a852f9268.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c202fafc9e8654ac9a69e6869143607373e9fd17 --- /dev/null +++ b/invmod/extended-euclid-algo/a7bede9a-d6e8-4777-bcd4-498a852f9268.cpp @@ -0,0 +1,36 @@ +#include +#include + + + +void euclid_extins(int a, int b, long long &x, long long &y) { + if (b == 0) { + x = 1; + y = 0; + } else { + euclid_extins(b, a % b, x, y); + long long temp = x; + x = y; + y = temp - (a / b) * y; + } +} + +int main() { + std::ifstream cin("inversmodular.in"); + std::ofstream cout("inversmodular.out"); + std::ios::sync_with_stdio(false); + + int a, n; + long long inv = 0, y; + + cin >> a >> n; + euclid_extins(a, n, inv, y); + + if (inv <= 0) { + inv = n + inv % n; + } + + cout << (long long)inv << '\n'; + + return 0; +} diff --git a/invmod/extended-euclid-algo/a807c805-6c47-46ae-81ca-00b4b4fc2ef1.cpp b/invmod/extended-euclid-algo/a807c805-6c47-46ae-81ca-00b4b4fc2ef1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f51250c327ea14e8975ed153b49632315061e8a8 --- /dev/null +++ b/invmod/extended-euclid-algo/a807c805-6c47-46ae-81ca-00b4b4fc2ef1.cpp @@ -0,0 +1,85 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +class Elem + +{ + +public: + + int d, x, y; + + Elem(){}; + + Elem(int dd, int xx, int yy) + + { + + d = dd; + + x = xx; + + y = yy; + + } + +}; + + + +Elem euclid(int a, int b) + +{ + + if(b == 0) + + { + + Elem e = Elem(a, 1, 0); + + return e; + + } + + Elem d = euclid(b, a%b); + + Elem rez = Elem(d.d, d.y, d.x - (a/b)*d.y); + + return rez; + +} + + + +int main() + +{ + + int a, n; + + fin >> a >> n; + + Elem rez = euclid(a, n); + + rez.x %= n; + + while(rez.x < 0) + + rez.x += n; + + fout << rez.x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a8ec1c0d-b708-4c51-a211-44cbcb62b792.cpp b/invmod/extended-euclid-algo/a8ec1c0d-b708-4c51-a211-44cbcb62b792.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d74d136a9ac8de5df7b8b44f294df42ed222c64b --- /dev/null +++ b/invmod/extended-euclid-algo/a8ec1c0d-b708-4c51-a211-44cbcb62b792.cpp @@ -0,0 +1,49 @@ +#include +#include + + +using namespace std; + + + + + +pair euclid(int x, int y){ + + if(y == 0) + + return {1, 0}; + + auto p = euclid(y, x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + + + +int a, n, x, y; + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + scanf("%d %d", &a, &n); + + auto Q = euclid(a, n); + + while(Q.first<0) + + Q.first += n; + + printf("%d", Q.first); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/a9b898a6-fac3-48bf-b6ab-c85f3fe9961b.cpp b/invmod/extended-euclid-algo/a9b898a6-fac3-48bf-b6ab-c85f3fe9961b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa6c4651159e8e37563dc39f8c7627974470592 --- /dev/null +++ b/invmod/extended-euclid-algo/a9b898a6-fac3-48bf-b6ab-c85f3fe9961b.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + + + +void euclid_e(long long &x, long long &y, int a, int b) + +{ + + if(!b) x=1,y=0; + + else { + + euclid_e(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + f>>a>>n; + + long long inv=0, ins; + + euclid_e(inv,ins,a,n); + + if(inv<=0) inv=n+inv%n; + + g< + + +using namespace std; + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + +int a, n; + + + +void citire() + +{ + + cin >> a >> n; + +} + + + +void invers(long long &x, long long &y, int a, int b) + +{ + + if(!b) + + x = 1, y = 0; + + else + + { + + invers(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +void print(long long x) + +{ + + if (x <= 0) + + x = n + x % n; + + cout << x; + +} + + + +int main() + +{ + + long long x = 0, y; + + citire(); + + invers(x,y,a,n); + + print(x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/aa8be08c-ba01-4071-8f55-05a7cf118f2b.cpp b/invmod/extended-euclid-algo/aa8be08c-ba01-4071-8f55-05a7cf118f2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6f486f36ceabdbf65511beff67662ec8e49a55a --- /dev/null +++ b/invmod/extended-euclid-algo/aa8be08c-ba01-4071-8f55-05a7cf118f2b.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + +long long t; + +long long a,b,c; + +long long gcd(long long a,long long b,long long &X,long long &Y){ + + if(b==0){ + + X=1; + + Y=0; + + return a; + + } + + else{ + + long long X0=0,Y0=0,D=0; + + D=gcd(b,a%b,X0,Y0); + + long long tz=X0; + + X=Y0; + + Y=(X0-(a/b)*Y0); + + return D; + + } + +} + +int32_t main() { + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + + + fin>>a>>b; + + long long x=0,y=0; + + long long w=gcd(a,b,x,y); + + + + long long ans=x; + + ans=ans%b; + + while(ans<0) ans+=b; + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +void euclid(long long a, long long b, long long& d, long long& x, long long& y) { + + if (b == 0) { + + d = a; + + x = 1; y = 0; + + + + + + } + + else { + + long long x0, y0; + + euclid(b, a % b, d, x0, y0); + x = y0; + + y = x0 - (a / b) * y0; + + + + + + } + +} + + + +int main() { + + + + long long a, b, d, x, y; + + cin >> a >> b; + + euclid(a, b, d, x, y); + + x = (x%b + b) % b; + + cout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/b05686ae-ea20-48d1-a9b2-21a25484853a.cpp b/invmod/extended-euclid-algo/b05686ae-ea20-48d1-a9b2-21a25484853a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b355e1ff33a441ed9250f5145274954f8c5f144 --- /dev/null +++ b/invmod/extended-euclid-algo/b05686ae-ea20-48d1-a9b2-21a25484853a.cpp @@ -0,0 +1,52 @@ +#include +using namespace std; + +int a, b; + +long long rez1, rez2; + +void invers_modular(int a, int b, long long & alfa, long long & beta) + +{ + + if(b == 0) + + { + + alfa = 1; + + beta = 0; + + return; + + } + + long long fost_alfa, fost_beta; + + invers_modular(b, a % b, fost_alfa, fost_beta); + + alfa = fost_beta; + + beta = fost_alfa + (-a / b) * fost_beta; + +} + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> a >> b; + + invers_modular(a, b, rez1, rez2); + + if(rez1 > 0)g << rez1; + + else g << rez1 + b; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/b0778efd-de1e-4be8-80be-ff246e986276.cpp b/invmod/extended-euclid-algo/b0778efd-de1e-4be8-80be-ff246e986276.cpp new file mode 100644 index 0000000000000000000000000000000000000000..556cd7e3fd4d6d4f4ce2dd9eff7309da96e6abab --- /dev/null +++ b/invmod/extended-euclid-algo/b0778efd-de1e-4be8-80be-ff246e986276.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long a,n,x; + +void euclid_extins(long long &x,long long &y,long long a,long long b) + +{ + + if (!b) + + { + + x=1;y=0; + + } + + else + + { + + euclid_extins(x,y,b,a%b); + + int aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + fin>>a>>n; + + long long ins=0; + + euclid_extins(x,ins,a,n); + + if(x<=0) + + x=n+x%n; + + fout< +#include + + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +ifstream fin ("inversmodular.in"); +ofstream fout ("inversmodular.out"); + +void ext_gcd(int a, int b, int &x, int &y) { + if (!b) { + x = 1, y = 0; + return; + } + int x0, y0; + ext_gcd(b, a % b, x0, y0); + x = y0; + y = x0 - (a / b) * y0; +} + +int main(void) { + int a,n; + fin >> a >> n; + int x, y; + ext_gcd(a, n, x, y); + x = x % n; + if (x < 0) { + x += n; + } + fout << x << endl; + + return 0; +} diff --git a/invmod/extended-euclid-algo/b35d6a94-6824-44c8-a80f-71b40538fc60.cpp b/invmod/extended-euclid-algo/b35d6a94-6824-44c8-a80f-71b40538fc60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1149ef64cbfaaddb73cf4b1e16387fccab74490e --- /dev/null +++ b/invmod/extended-euclid-algo/b35d6a94-6824-44c8-a80f-71b40538fc60.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + + + +int invers_modular(int a, int m) + +{ + + int y, y0 = 0, y1 = 1; + + int aux = m, r, c; + + while(a!=0) + + { + + r = m%a; + + c = m/a; + + m = a; + + a = r; + + y = y0 - c*y1; + + y0 = y1; + + y1 = y; + + } + + if(y0<0) y0 = aux + y0%aux; + + return y0; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int a, b; + + fin>>a>>b; + + fout< + + +using namespace std; + + + +long long aux; + + + +void euclid(long long &x, long long &y, long long a, long long b) + +{ + + if(b == 0) { + + x = 1; + + y = 0; + + } + + else { + + euclid(x, y, b, a % b); + + aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + long long a,n,inv = 0,ins; + + fin >> a >> n; + + euclid(inv, ins, a, n); + + if(inv <= 0) + + inv = n + inv % n; + + fout << inv << '\n'; + +} diff --git a/invmod/extended-euclid-algo/b558639b-43bd-420d-83bc-18dff3784a5f.cpp b/invmod/extended-euclid-algo/b558639b-43bd-420d-83bc-18dff3784a5f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b437658b1bf09001639c571f0d585b67b47626ae --- /dev/null +++ b/invmod/extended-euclid-algo/b558639b-43bd-420d-83bc-18dff3784a5f.cpp @@ -0,0 +1,66 @@ +#include + + +std::ifstream fin("inversmodular.in"); + +std::ofstream fout("inversmodular.out"); + + + +int gcd(int a, int b, int& x, int& y) { + + if (!b) { + + x = 1; + + y = 0; + + return a; + + } + + + + int x0, y0; + + int d = gcd(b, a % b, x0, y0); + + + + x = y0; + + y = x0 - a / b * y0; + + return d; + +} + + + +int main() { + + int a, n; + + fin >> a >> n; + + + + int x, y; + + gcd(a, n, x, y); + + + + if (x < 0) + + x += n; + + fout << x << '\n'; + + + + fout.close(); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/b5f5eb53-383f-4b0d-9a3e-4fd4d635aca5.cpp b/invmod/extended-euclid-algo/b5f5eb53-383f-4b0d-9a3e-4fd4d635aca5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0beeccd264f9e77e5f310783919142995171ff7 --- /dev/null +++ b/invmod/extended-euclid-algo/b5f5eb53-383f-4b0d-9a3e-4fd4d635aca5.cpp @@ -0,0 +1,54 @@ +#include +using namespace std; + + + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + +void eclext(int a, int b, int *d, int *x, int *y) { + + if (b == 0) { + + *d = a; + + *x = 1; + + *y = 0; + + } else { + + int x0, y0; + + eclext(b, a % b, d, &x0, &y0); + + *x = y0; + + *y = x0 - (a / b) * y0; + + } + +} + +int main() { + + int a, n, d, x, y; + + in >> a >> n; + + eclext(a, n, &d, &x, &y); + + if(x > 0) + + out << x; + + else + + out << n + x % n; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/b86a46e3-7cab-4e6b-b4ab-c3319a275340.cpp b/invmod/extended-euclid-algo/b86a46e3-7cab-4e6b-b4ab-c3319a275340.cpp new file mode 100644 index 0000000000000000000000000000000000000000..903f1a072d1dde4bb4b6b8b5d9888720e800355d --- /dev/null +++ b/invmod/extended-euclid-algo/b86a46e3-7cab-4e6b-b4ab-c3319a275340.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; + +pair euclid_extins(int a, int b) { + if (!b) + return {1, 0}; + pair rez = euclid_extins(b, a % b); + return {rez.second, rez.first - (a / b) * rez.second}; +} + +int main() { + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + int a, n; + scanf("%d %d", &a, &n); + int rez = euclid_extins(a, n).first % n; + while (rez < 0) + rez += n; + printf("%d", rez); + return 0; +} diff --git a/invmod/extended-euclid-algo/b872c065-8be0-435c-b407-bf3bd5033b79.cpp b/invmod/extended-euclid-algo/b872c065-8be0-435c-b407-bf3bd5033b79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ed63cf79b4e39f1f51c45fbf03e39491e09fe0b --- /dev/null +++ b/invmod/extended-euclid-algo/b872c065-8be0-435c-b407-bf3bd5033b79.cpp @@ -0,0 +1,52 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +void EuclidE(int a,int b,int &x,int &y,int &d) + +{ + + if(b==0){x=1;y=0;d=a;} + + else{int x0,y0; + + EuclidE(b,a%b,x0,y0,d); + + x=y0; y=x0-a/b*y0; + + } + +} + + + +int a,b,c,d,x,y,t,k,n; + +int main() + +{ + + fin>>a>>n; + + EuclidE(n,a,x,y,d); + + while(y<1)y+=n; + + fout< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long n, p; + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x,y,b,a%b); + + long long aux = x; + + x = y; + + y = aux - y*(a/b); + + } + +} + +int main() + +{ + + long long v1, v2; + + fin>>n>>p; + + gcd(v1, v2, n, p); + + while (v1<0)v1+=p; + + fout< + + +using namespace std; + + + +ifstream f ("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a, n; + + + +void euc(int a, int b, int &x, int &y) + +{ + + if (!b) + + { + + x=1; + + y=0; + + return; + + } + + euc(b,a%b,x,y); + + int y0=y; + + y=x-(a/b)*y; + + x=y0; + +} + + + + + +int main() + +{ + + f>>a>>n; + + int x, y; + + euc(a,n,x,y); + + if (x<0) + + x+=n; + + g< + + +using namespace std; + + + +int a, n, k, L; + + + +void euclid(int a, int n, int &k, int &L) + +{ + + if (n==0){ + + k=0; + + L=1; + + return; + + } + + int k1, l1; + + euclid(n, a%n, k1, l1); + + L = k1; + + k = l1 - (a/n) * k1; + +} + + + +int main() + +{ + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin>>a>>n; + + euclid(a, n, k, L); + + while (L < 0){ + + L+=n; + + } + + fout< + + + + +using ll = long long; + +std::ifstream fin("inversmodular.in"); +std::ofstream fout("inversmodular.out"); + +int a, n; + +void ecl(ll &x, ll &y, int a, int b) { + if (!b) { + x = 1, y = 0; + } else { + ecl(x, y, b, a % b); + ll aux = x; + x = y; + y = aux - y * (a / b); + } +} + +int main() { + ll inv = 0, ins; + fin >> a >> n; + ecl(inv, ins, a, n); + if (inv <= 0) { + inv = n + inv % n; + } + fout << inv; + return 0; +} diff --git a/invmod/extended-euclid-algo/bc51acf4-42a0-46f8-8579-2b9d2cbdf4cc.cpp b/invmod/extended-euclid-algo/bc51acf4-42a0-46f8-8579-2b9d2cbdf4cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77c58fb2cbc0760801a3ecd95f1616267b0e8353 --- /dev/null +++ b/invmod/extended-euclid-algo/bc51acf4-42a0-46f8-8579-2b9d2cbdf4cc.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + + + + + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux - y*(a/b); + + } + +} + + + +int main() + +{ + + long long x=0,y; + + + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + + + int a,n; + + + + scanf("%d %d",&a,&n); + + + + gcd(x,y,a,n); + + + + if(x<=0) + + x=n+x%n; + + + + printf("%lld",x); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/bc7570ab-442d-4f09-8067-34a7ba767f1d.cpp b/invmod/extended-euclid-algo/bc7570ab-442d-4f09-8067-34a7ba767f1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd3aa18c03324d0b99ed69f19b426c7b77d9955b --- /dev/null +++ b/invmod/extended-euclid-algo/bc7570ab-442d-4f09-8067-34a7ba767f1d.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int a, b, c, x, y, k; + + + +int euclid (int a, int b, int &x, int &y){ + + int xa, ya, d; + + if (b == 0){ + + y = 0, x = 1; + + return a; + + } + + else{ + + d = euclid(b, a%b, xa, ya); + + x = ya; + + y = xa - a/b*ya; + + return d; + + } + +} + + + +int main(){ + + fin>>a>>b; + + k = euclid(a, b, x, y); + + x%=b; + + if(x<0){ + + x+=b; + + } + + fout< +#include + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +void euclid(int a, int b, int *d, int *x, int *y){ + + if( b == 0 ){ + + *d = a; + + *x = 1; + + *y = 0; + + } + + else { + + int x0, y0; + + euclid( b, a % b, d, &x0, &y0 ); + + *x = y0; + + *y = x0 - ( a / b ) * y0; + + } + +} + + + +int main() + +{ + + int n, x, y, a, d; + + in>>a>>n; + + euclid( a, n, &d, &x, &y ); + + if( x < 0 ) + + x = n + x % n; + + out< + + +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); +void invers_modular(long long int a , long long int b ,long long int & x ,long long int & y) + +{ + + if(b == 0) + + { + + x = 1, y = 1; + + } + + else + + { + + long long int x1 , y1; + + invers_modular(b , a % b , x1 , y1); + + x = y1; + + y = x1 - a / b * y1; + + } + +} + + + +int main() + +{ + + long long int a , n , x , y; + + fin >> a >> n; + + invers_modular(a, n , x ,y); + + while(x < 0) + + x += n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/bea8e942-8d6f-47a6-9116-24f22a4c99c6.cpp b/invmod/extended-euclid-algo/bea8e942-8d6f-47a6-9116-24f22a4c99c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd906e662888910c6b1eafe9c72926d7cb4cd00b --- /dev/null +++ b/invmod/extended-euclid-algo/bea8e942-8d6f-47a6-9116-24f22a4c99c6.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int n, a, inv, y; + + + +void euclid_extins(int a, int b, int &x, int &y) + +{ + + int x0, y0; + + + + if(!b) + + { + + x = 1; + + y = 0; + + } + + else + + { + + euclid_extins(b, a % b, x0, y0); + + x = y0; + + y = x0 - a / b * y0; + + } + + + +} + + + +int main() + +{ + + f >> a >> n; + + f.close(); + + + + euclid_extins(a, n, inv, y); + + + + if(inv < 0) + + inv += n; + + inv %= n; + + + + g << inv << '\n'; + + g.close(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/c172ad6d-d465-4aa8-b9f4-2ce6b9cd287b.cpp b/invmod/extended-euclid-algo/c172ad6d-d465-4aa8-b9f4-2ce6b9cd287b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd747f4c9e80574b72e416e8239fca00df65de7b --- /dev/null +++ b/invmod/extended-euclid-algo/c172ad6d-d465-4aa8-b9f4-2ce6b9cd287b.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void Euclid(int a,int b,int &x,int &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + return; + + } + + int x0,y0; + + Euclid(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + +} + + + +int main() + +{ + + int N,A,X,Y; + + f>>A>>N; + + + + Euclid(A,N,X,Y); + + X=X%N; + + if(X<0) X=N+X; + + g< + + +struct EuclidExtins { + + int x; + + int y; + +}; + + + +EuclidExtins euclid_extins(int a, int b, int d) { + + if (b == 0) + + return {d / a, 0}; + + else { + + EuclidExtins sol = euclid_extins(b, a % b, d); + + int c = a / b; + + return {sol.y, sol.x - c * sol.y}; + + } + +} + + + +int main() { + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + int a, n; + + scanf("%d%d", &a, &n); + + + + EuclidExtins sol = euclid_extins(a, n, 1); + + int x = sol.x; + + while (x <= 0) + + x += n; + + printf("%d\n", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/c299452c-7318-4d90-bf0d-acc33db4191e.cpp b/invmod/extended-euclid-algo/c299452c-7318-4d90-bf0d-acc33db4191e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a98a1cd04f15e7390e2695f9d0a846013a959ee --- /dev/null +++ b/invmod/extended-euclid-algo/c299452c-7318-4d90-bf0d-acc33db4191e.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a, n; + +void Read() + +{ + + fin >> a >> n; + + fin.close(); + +} + +void EuclidExtins(int a, int b, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + } + + else + + { + + int x0, y0; + + EuclidExtins(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int main() + +{ + + Read(); + + int x, y; + + EuclidExtins(a, n, x, y); + + int inversmodular = x; + + while(inversmodular < 0) inversmodular += n; + + fout << inversmodular; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/c327f0f7-0abc-4dc0-ab7b-149348c8101b.cpp b/invmod/extended-euclid-algo/c327f0f7-0abc-4dc0-ab7b-149348c8101b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6ac53a258ee8739edbfba1d530b3b53b0ea6c22 --- /dev/null +++ b/invmod/extended-euclid-algo/c327f0f7-0abc-4dc0-ab7b-149348c8101b.cpp @@ -0,0 +1,65 @@ +#include +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int euclid(int a, int b, int &x, int &y) + +{ + + if (b == 0) + + { + + x = 1; + + y = 0; + + return a; + + } + + int x0, y0; + + int rez = euclid(b, a%b, x0, y0); + + x = y0; + + y = x0 - (a/b) * y0; + + return rez; + +} + + + +int main() + +{ + + int a, n; + + int x, y; + + f >> a >> n; + + euclid(a, n, x, y); + + while (x < 0) + + x += n; + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/c46de8bb-0760-4383-be3a-2d031361028d.cpp b/invmod/extended-euclid-algo/c46de8bb-0760-4383-be3a-2d031361028d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa6c4651159e8e37563dc39f8c7627974470592 --- /dev/null +++ b/invmod/extended-euclid-algo/c46de8bb-0760-4383-be3a-2d031361028d.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +int a,n; + + + +void euclid_e(long long &x, long long &y, int a, int b) + +{ + + if(!b) x=1,y=0; + + else { + + euclid_e(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + f>>a>>n; + + long long inv=0, ins; + + euclid_e(inv,ins,a,n); + + if(inv<=0) inv=n+inv%n; + + g< +using namespace std ; + +ifstream in ("inversmodular.in") ; + +ofstream out ("inversmodular.out") ; + +int euclid( int a , int b , int & x , int & y ) + +{ + + if ( !b ) + + { + + x = 1 ; + + y = 0 ; + + return a ; + + } + + { + + int x0 , y0 ; + + int u = euclid ( b , a % b , x0 , y0 ) ; + + x = y0 ; + + y = x0 - y0 * ( a / b ) ; + + return u ; + + } + + + +} + +int main () + +{ + + int a , b , gcd , x , y ; + + in >> a >> b ; + + gcd = euclid( a , b , x , y ) ; + + while ( x < 0 ) x += b ; + + out << x ; + +} diff --git a/invmod/extended-euclid-algo/c67f56fc-2673-4a41-9a77-c1e5ee5c174f.cpp b/invmod/extended-euclid-algo/c67f56fc-2673-4a41-9a77-c1e5ee5c174f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..205ee639903a05dde11fa7c96286acf07bf08540 --- /dev/null +++ b/invmod/extended-euclid-algo/c67f56fc-2673-4a41-9a77-c1e5ee5c174f.cpp @@ -0,0 +1,51 @@ +#include +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long x,y,r,r2,aux; + +void mad(long long &x, long long &y,long long a,long long b){ + + if(!b){ + + x=1; + + y=0; + + } + + else{ + + mad(x,y,b,a%b); + + aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main(){ + + fin>>x>>y; + + mad(r,r2,x,y); + + if(r<=0) + + r=y+(r%y); + + fout< +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void euclid(int a, int b, int &x, int &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + int x1,y1; + + euclid(b,a%b,x1,y1); + + x=y1; + + y=x1-a/b*y1; + + } + +} + +int32_t main() + +{ + + int a,b,x=0,y=0; + + fin>>a>>b; + + euclid(a,b,x,y); + + while(x<0) + + x+=b; + + fout< + + + + +using namespace std; + + + + + +void gcdExtended(int a, int b, long long *x, long long *y) { + + + + if (a == 0) { + + *x = 0; + + *y = 1; + + return; + + } + + + + long long x1, y1; + + gcdExtended(b%a, a, &x1, &y1); + + + + + + *x = y1 - (b/a) * x1; + + *y = x1; + +} + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + long long x, y; + + int a, b, c = 1; + + + + scanf("%d %d", &a, &b); + gcdExtended(a, b, &x, &y); + + + + + + + + if (x <= 0) + + x = b + x % b; + + printf("%d\n", x); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/c7aa11e2-b767-462c-adfb-18f83eacf798.cpp b/invmod/extended-euclid-algo/c7aa11e2-b767-462c-adfb-18f83eacf798.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ba91c6b6770ba168581583b7db0a44aac055c34 --- /dev/null +++ b/invmod/extended-euclid-algo/c7aa11e2-b767-462c-adfb-18f83eacf798.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; + + + +const long long mod=1e9+7; + + + +long long a,n; + + + +void invers(long long a,long long b,long long &X,long long &Y){ + + if(b==0){ X=1;Y=0;} + + else{ + + invers(b,a%b,X,Y); + + long long tz=X; + + X=Y; + + Y=tz-(a/b)*Y; + + } + +} + + + +int32_t main(){ + + + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream fin("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + fin >> a >> n; + + long long X0=0,Y0=0; + + invers(a,n,X0,Y0); + + X0=X0%n; + + if(X0<0) X0+=n; + + fout << X0 << '\n'; + +} diff --git a/invmod/extended-euclid-algo/c8c25a1b-df19-4ed2-9e0c-f4bcc3993507.cpp b/invmod/extended-euclid-algo/c8c25a1b-df19-4ed2-9e0c-f4bcc3993507.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b64c8857f1c600699709c5a752ad8f8e0143081 --- /dev/null +++ b/invmod/extended-euclid-algo/c8c25a1b-df19-4ed2-9e0c-f4bcc3993507.cpp @@ -0,0 +1,60 @@ +#include + + + + +using namespace std; + + + + + + + +long long euclid(long long a, long long b, long long &x, long long &y){ + + if(b==0){ + + x=1; + + y=0; + + return a; + + }else{ + + long long x0,y0,s=euclid(b,a%b,x0,y0); + + x=y0; + + y=x0-y0*(a/b); + + return s; + + } + +} + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int main(){ + + long long a,n,x,y; + + fin>>a>>n; + + euclid(n,a,x,y); + + while(y<0)y+=n; + + fout< +using namespace std; + + + +int a, b, x, y; + + + +void euclid(int a, int b, int &x, int &y) { + + if(!b) { + + x = 1, y = 0; + + return; + + } + + int x1, y1; + + euclid(b, a % b, x1, y1); + + x = y1; + + y = x1 - y1 * (a / b); + +} + + + +int main() { + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d%d", &a, &b); + + euclid(a, b, x, y); + + + + if(x < 0) { x += b; } + + printf("%d", x % b); + +} diff --git a/invmod/extended-euclid-algo/ca8f7296-fcac-4f33-948a-b28c774df23a.cpp b/invmod/extended-euclid-algo/ca8f7296-fcac-4f33-948a-b28c774df23a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48652666147df4910354c00c84aa3049d6c8b417 --- /dev/null +++ b/invmod/extended-euclid-algo/ca8f7296-fcac-4f33-948a-b28c774df23a.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + + + +ifstream f ("inversmodular.in"); + +ofstream g ("inversmodular.out"); + + + +int a,n,x,y; + + + +void Euclid(int a, int b, int &x, int&y) + +{ + + if(b==0){x=1;y=0;} + + else{ + + int x0,y0; + + Euclid(b,a%b,x0,y0); + + x=y0; y=x0-a/b*y0; + + } + +} + + + +int main() + +{ + + f>>a>>n; + + Euclid(n,a,x,y); + + while(y<0)y+=n; + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); +void euclid(long long a, long long b, long long& x, long long& y) { + + if (b == 0) { + + x = 1; y = 0; + + return; + + } + + + + long long x0, y0; + + euclid(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + +} + + + + + + + +int main() { + + + + long long a, n, x, y; + + cin >> a >> n; + + + + euclid(a, n, x, y); + + + + x = (x % n + n) % n; + + cout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/cab814bf-fd65-4111-a98f-b0baffa58de6.cpp b/invmod/extended-euclid-algo/cab814bf-fd65-4111-a98f-b0baffa58de6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c0ade88f7d788bd721a93e67bc7d6c1fed97c2a --- /dev/null +++ b/invmod/extended-euclid-algo/cab814bf-fd65-4111-a98f-b0baffa58de6.cpp @@ -0,0 +1,76 @@ +#include + + +using namespace std; + + + +int eucl(int a,int b,int c,int &x, int &y) + +{ + + int q[100],x0,y0,r,pas=0; + + while (b) + + { + + q[++pas]=a/b; + + r=a%b; + + a=b; + + b=r; + + } + + if (c%a) {x=y=0;return 0;} + + x=x0=c/a; + + y=y0=0; + + while(pas) + + { + + x=y0; + + y=x0-q[pas--]*y0; + + x0=x; + + y0=y; + + } + + return 1; + +} + + + + + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream fout("inversmodular.out"); + + int a,b,x,y; + + f>>a>>b; + + eucl(a,b,1,x,y); + + x=((long long)b+x%b)%b; + + fout< + + + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + long long x=0,y; + + int a,n; + + f>>a>>n; + + + + gcd(x,y,a,n); + + + + if(x<=0) + + x=n+x%n; + + + + g< + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +void euclid(int a, int b, int & x, int & y){ + + if(!b){ + + x = y = 1; + + } + + else{ + + int x1, y1; + + euclid(b, a % b, x1, y1); + + x = y1; + + y = x1 - a / b * y1; + + } + +} + + + +int main(){ + + int a, n, x, y; + + in>>a>>n; + + euclid(a, n, x, y); + + while(x < 0) + + x += n; + + out< +using namespace std; + +pair euclid(int a, int b) { + if (b == 0) { + return make_pair(1, 0); + } + + auto res = euclid(b, a % b); + int64_t q = (a / b); + + auto ans = make_pair(res.second, res.first - 1LL * q * res.second); + return ans; +} + +int main() { + ifstream cin("inversmodular.in"); + ofstream cout("inversmodular.out"); + + int a, n; cin >> a >> n; + int b = n; + + auto res = euclid(a, b); + int64_t inv = res.first; + + if (inv < 0) { + inv = (b + inv % b); + } + + cout << inv << "\n"; +} diff --git a/invmod/extended-euclid-algo/cc08c310-705a-41d0-9010-41134c64afc5.cpp b/invmod/extended-euclid-algo/cc08c310-705a-41d0-9010-41134c64afc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa57bbbf8ce6120313d6cd736fc09ab228ea5fd3 --- /dev/null +++ b/invmod/extended-euclid-algo/cc08c310-705a-41d0-9010-41134c64afc5.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +int a, n; + +void euclidextins(int a, int b, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1, y = 1; + + } + + else + + { + + int x0, y0; + + euclidextins(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int main() + +{ + + fin >> a >> n; + + int x, y; + + euclidextins(a, n, x, y); + + while(x < 0) x += n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/cd89eeaa-29c2-4e8b-9540-cbf3e83bbc73.cpp b/invmod/extended-euclid-algo/cd89eeaa-29c2-4e8b-9540-cbf3e83bbc73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30f15e035ea98acd2c188318d37d2fc146eeeaf2 --- /dev/null +++ b/invmod/extended-euclid-algo/cd89eeaa-29c2-4e8b-9540-cbf3e83bbc73.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +int x, X1, X2, cat, rest, rest1, rest2, modulo; + +int main() + +{ + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f >> rest1 >> rest2; + + modulo = rest2; + + X1 = 1; + + X2 = 0; + + while(rest1 % rest2 != 0) + + { + + cat = rest1 / rest2; + + rest = rest1 % rest2; + + rest1 = rest2; + + rest2 = rest; + + x = X1 - X2 * cat; + + X1 = X2; + + X2 = x; + + } + + while(x < 0)x = x + modulo; + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/cda08305-d6cb-439e-8999-bb6060ee2c4d.cpp b/invmod/extended-euclid-algo/cda08305-d6cb-439e-8999-bb6060ee2c4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11a87e6303fa7644e132be60b2cfedebdc35335b --- /dev/null +++ b/invmod/extended-euclid-algo/cda08305-d6cb-439e-8999-bb6060ee2c4d.cpp @@ -0,0 +1,55 @@ +#include +#include + + +using namespace std; + + + +int a, n; + + + +pair eu_ex(int x, int y) + +{ + + if(y == 0) + + return {1, 0}; + + auto p=eu_ex(y, x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &a, &n); + + + + int inv=eu_ex(a, n).first; + + if(inv<0) + + inv+=n; + + printf("%d", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/cef9c9dd-99de-4c9c-a049-75c9847eadcd.cpp b/invmod/extended-euclid-algo/cef9c9dd-99de-4c9c-a049-75c9847eadcd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..674c064944491a90a3928bd306884dfb0bc2a305 --- /dev/null +++ b/invmod/extended-euclid-algo/cef9c9dd-99de-4c9c-a049-75c9847eadcd.cpp @@ -0,0 +1,69 @@ +#include +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +void EuclidExtins(int a, int b, int &d, int &x, int &y) + +{ + + if(b == 0) + + { + + x = 1; + + y = 0; + + d = a; + + } + + else + + { + + int x0, y0; + + EuclidExtins(b, a % b, d, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + +int InversModular(int A, int N) + +{ + + int X, Y, d; + + EuclidExtins(A, N, d, X, Y); + + X %= N; + + if(X < 0) + + X += N; + + return X; + +} + +int main() + +{ int a,b; + + f>>a>>b; + + g< + + +int inv_mod(int x,int b) + +{ + + int r,q[100],nr=0,bz=b; + + while(b) + + { + + q[++nr]=x/b; + + r=x%b; + + x=b;b=r; + + } + + int a=1,a1,b1; + + b=0; + + while(nr) + + { + + a1=b; + + b1=a-q[nr--]*b; + + a=a1;b=b1; + + } + + return ((long long)bz+a1%bz)%bz; + +} + + + +int main() + +{ + + int a,b;FILE *f=fopen("inversmodular.in","r"); + + fscanf(f,"%d%d",&a,&b); + + f=fopen("inversmodular.out","w"); + + fprintf(f,"%d",inv_mod(a,b)); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/cfac5672-82a9-41fd-853b-3f0acd19ace9.cpp b/invmod/extended-euclid-algo/cfac5672-82a9-41fd-853b-3f0acd19ace9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce4304bde69728fff4e20e399783e1a0b1cc6260 --- /dev/null +++ b/invmod/extended-euclid-algo/cfac5672-82a9-41fd-853b-3f0acd19ace9.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + + + + + +typedef vector vi; +typedef vector vvi; +typedef pair ii; +typedef vector vii; +typedef long long ll; +typedef vector vll; +typedef vector vvll; +typedef double ld; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + +ll a, n, inv, ins; + +void euclid(ll &x, ll &y, ll a, ll b) { + if (b == 0) { + x = 1; + y = 0; + return; + } + + euclid(x, y, b, a % b); + ll aux = x; + x = y; + y = aux - y * (a / b); +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + + fin >> a >> n; + + euclid(inv, ins, a, n); + + if (inv < 0) { + inv = inv + n; + } + fout << inv; + + return 0; +} diff --git a/invmod/extended-euclid-algo/d0b167df-387b-411b-8a1a-0c60bdbcec4b.cpp b/invmod/extended-euclid-algo/d0b167df-387b-411b-8a1a-0c60bdbcec4b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a027dbb28068a0a72e3ecfd70eeb9db62ea7bb7f --- /dev/null +++ b/invmod/extended-euclid-algo/d0b167df-387b-411b-8a1a-0c60bdbcec4b.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int t,a,n,inv,inv2; + +void euclid(int &inv, int &inv2, int a, int n) + +{ + + if(n==0) + + { + + inv=1; + + inv2=0; + + } + + else + + { + + int inv0, inv20; + + euclid(inv,inv2,n,a%n); + + inv0=inv; + + inv20=inv2; + + inv=inv20; + + inv2=inv0-(a/n)*inv20; + + } + +} + +int main() + +{ + + f>>a>>n; + + euclid(inv,inv2,a,n); + + while(inv<0) + + inv=inv+n; + + g< + + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + +int main() + +{ + + FILE *f = fopen("inversmodular.in", "r"); + + FILE *g = fopen("inversmodular.out", "w"); + + int t, a, b, c, x, y; + + fscanf(f, "%d%d", &a,&b); + + eulcid_eXt(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/d1212e70-b28e-458d-8c77-16ecb52dbfc4.cpp b/invmod/extended-euclid-algo/d1212e70-b28e-458d-8c77-16ecb52dbfc4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98b39a4e3efb67eadf1b68aaf34f71d2a2344417 --- /dev/null +++ b/invmod/extended-euclid-algo/d1212e70-b28e-458d-8c77-16ecb52dbfc4.cpp @@ -0,0 +1,66 @@ +#include + + +void eulcid_eXt(int a, int b, int c, int &x, int &y) + +{ + + int q[100], x0, y0, r, p=0; + + while(b != 0) + + { + + r = a%b; + + q[++p] = a/b; + + a=b; + + b=r; + + } + + if(c%a) {x=y=0;return;} + + x=x0 = c/a; + + y=y0 = 0; + + while(p) + + { + + x= y0; + + y = x0 - (q[p--]*y0); + + x0 = x; + + y0 = y; + + } + +} + +int main() + +{ + + FILE *f = fopen("inversmodular.in", "r"); + + FILE *g = fopen("inversmodular.out", "w"); + + int t, a, b, c, x, y; + + fscanf(f, "%d%d", &a,&b); + + eulcid_eXt(a, b, 1, x, y); + + x=((long long)b+x%b)%b; + + fprintf(g, "%d", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/d1c258b5-086a-4257-8edc-d95222ca0367.cpp b/invmod/extended-euclid-algo/d1c258b5-086a-4257-8edc-d95222ca0367.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ce839e4a1665b140d98e99b7d738218ff432ec1 --- /dev/null +++ b/invmod/extended-euclid-algo/d1c258b5-086a-4257-8edc-d95222ca0367.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +pair euclidex(int a, int b){ + if(b==0) + return {1ll,0}; + auto p = euclidex(b,a%b); + return {p.second, p.first-(a/b)*p.second}; +} + +int main() +{ + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + int a,n; + scanf("%d%d", &a, &n); + auto p = euclidex(n,a); + + while(p.second < 0) + p.second += n; + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; +ifstream cin("inversmodular.in"); ofstream cout("inversmodular.out"); + + + +void euclid(long long a, long long b, long long& d, long long& x, long long& y) { + + if (b == 0) { + + d = a; + + x = 1; y = 0; + + + + + + } + + else { + + long long x0, y0; + + euclid(b, a % b, d, x0, y0); + x = y0; + + y = x0 - (a / b) * y0; + + + + + + } + +} + + + +int main() { + + + + long long a, b, d, x , y; + + cin >> a >> b; + + euclid(a, b, d, x, y); + + x = (x + b) % b; + + cout << x; + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/d4f6c9a1-c853-4633-93e8-04d68c79436a.cpp b/invmod/extended-euclid-algo/d4f6c9a1-c853-4633-93e8-04d68c79436a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03f472b175171e7cd893c1f664ceb51e53bb7c3c --- /dev/null +++ b/invmod/extended-euclid-algo/d4f6c9a1-c853-4633-93e8-04d68c79436a.cpp @@ -0,0 +1,67 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n; + +void euclid_extins(int a, int b, int &x, int &y) + +{ + + if( b == 0) + + { + + x = 1; + + y = 0; + + } + + else + + { + + int x0, y0; + + euclid_extins(b, a% b, x0, y0); + + x = y0; + + y = x0 - (a/b)*y0; + + } + +} + +int main() + +{ + + int x, y; + + fin>>a>>n; + + euclid_extins(a, n, x, y); + + + + while(x < 0) + + x += n; + + fout< +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int A,N; + + + +void ee(long long &x, long long &y, int a, int b) + +{ + + if(!b) + + x=1,y=0; + + else + + { + + ee(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + + + +int main() + +{ + + long long inv=0,ins; + + + + + + + + f>>A>>N; + + ee(inv,ins,A,N); + + if (inv<=0) + + inv=N+inv%N; + + + + g< + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/da7526a5-44a5-4b8e-9f7d-934c8a833a89.cpp b/invmod/extended-euclid-algo/da7526a5-44a5-4b8e-9f7d-934c8a833a89.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5574a88ca833cb2e35550d032cd253b42b0b987b --- /dev/null +++ b/invmod/extended-euclid-algo/da7526a5-44a5-4b8e-9f7d-934c8a833a89.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +long long gcd(long long a, long long b, long long & x, long long & y) { + + if(b == 0) { + + x = 1; + + y = 0; + + return a; + + } + + long long x1, y1; + + long long d = gcd(b, a % b, x1, y1); + + y = x1 - (a / b) * y1; + + x = y1; + + return d; + +} + + + +int main() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + + + + long long a, n, x, y; + + fin >> a >> n; + + gcd(a, n, x, y); + + x = (x + n) % n; + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/dadda2cb-ef25-47a2-b814-949fa8f380ec.cpp b/invmod/extended-euclid-algo/dadda2cb-ef25-47a2-b814-949fa8f380ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0db99857c3b7f3cde3aa3d705abed4c82e802d5 --- /dev/null +++ b/invmod/extended-euclid-algo/dadda2cb-ef25-47a2-b814-949fa8f380ec.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +ifstream in; +ofstream out; + + +void euclid(int a, int b, int &x, int &y) +{ + if (b == 0) + { + x = 1; + y = 0; + } + else + { + int x0, y0; + euclid(b, a%b, x0, y0); + x = y0; + y = x0 - (a/b) * y0; + } +} + +int n,p; + +int main() { + + in.open("inversmodular.in"); + out.open("inversmodular.out"); + + in>>n>>p; + int x,y; + euclid(n, p, x , y); + if(x>0) + out< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long int n,a,x,y,d; + +void cmmdc(long long int,long long int,long long int&,long long int&); + +int main() + +{ + + fin>>a>>n; + + long long int x,y; + + cmmdc(a,n,x,y); + + x=x%n; + + if(x<0)x+=n; + + fout< + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void invrs(long long int &x, long long int &y, int a, int b){ + + if(!b) + + x=1,y=0; + + else{ + + invrs(x,y,b,a%b); + + swap(x,y); + + y=y-x*(a/b); + + } + + + +} + + + +int main(){ + + long long int inv=0,ins; + + int a,b; + + f>>a>>b; + + invrs(inv,ins,a,b); + + + + if(inv<=0) + + inv = b+inv%b; + + + + g< +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +int A,N; + +void gcd(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + { + + x=1; + + y=0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ + + long long inv=0,ins; + + f>>A>>N; + + gcd(inv,ins,A,N); + + if(inv<0) + + inv=N+inv%N; + + + + g< + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invmod(long long &invers,long long &ins,int a,int minune) + +{ + + if(!minune) + + { + + invers=1; + + ins=0; + + } + + else + + { + + invmod(invers,ins,minune,a%minune); + + long long aux=invers; + + invers=ins; + + ins=aux-ins*(a/minune); + + } + +} + +int main() + +{ + + long long invers=0,ins; + + int a,minune; + + fin>>a>>minune; + + invmod(invers,ins,a,minune); + + while(invers<=0) + + invers+=minune; + + fout< + + + + +using namespace std; + +const string problem("inversmodular"); + +ifstream fin(problem + ".in"); + +ofstream fout(problem + ".out"); + +inline int Euclid(int a, int b, int& x, int& y) { + + if (!b) { + + x = 1, y = 0; + + return a; + + } + + int x0, y0, d = Euclid(b, a % b, x0, y0); + + x = y0, y = x0 - (a / b) * y0; + + return d; + +} + +int a, n, x, y; + +int main() { + + ios_base::sync_with_stdio(false); fin.tie(0); fout.tie(0); + + fin >> a >> n; + + Euclid(a, n, x, y); + + while (x > n) + + x -= n; + + while (x < 0) + + x += n; + + fout << x; + + fin.close(); fout.close(); return 0; + +} diff --git a/invmod/extended-euclid-algo/de70e30a-06a7-4fa2-a983-bed9ca2c9e0e.cpp b/invmod/extended-euclid-algo/de70e30a-06a7-4fa2-a983-bed9ca2c9e0e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fec345e7b5f6cfd6f3d0278c9fcd77f25957bcf --- /dev/null +++ b/invmod/extended-euclid-algo/de70e30a-06a7-4fa2-a983-bed9ca2c9e0e.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void invrs(long long int &x, long long int &y, int a, int b){ + + if(!b) + + x=1,y=0; + + else{ + + invrs(x,y,b,a%b); + + swap(x,y); + + y=y-x*(a/b); + + } + + + +} + + + +int main(){ + + long long int inv=0,ins; + + int a,b; + + f>>a>>b; + + invrs(inv,ins,a,b); + + + + if(inv<=0) + + inv = b+inv%b; + + + + g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +void gcd(long long & x,long long & y, int a, int b) + +{if(b==0) + +{ x=1; + + y=0; + +} + +else + +{ gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + +} + +} + +int main() + +{ long long x=0,y; + + int a,n; + + f>>a>>n; + + gcd(x,y,a,n); + + if(x<=0) + + x=n+x%n; + + g< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long a,n; + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if(!b) + + x = 1, y = 0; + + else + + { + + gcd(x,y,b,a%b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + +int main() + +{ + + long long inv = 0, ins; + + f >> a >> n; + + gcd(inv,ins,a,n); + + if(inv <= 0) + + inv = n + inv % n; + + g << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/e1556026-5bb3-4849-b613-1f5ca76c3248.cpp b/invmod/extended-euclid-algo/e1556026-5bb3-4849-b613-1f5ca76c3248.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fab338902c01b33596ae02ec898089f172db5c05 --- /dev/null +++ b/invmod/extended-euclid-algo/e1556026-5bb3-4849-b613-1f5ca76c3248.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + + + +void gcd (long a, long b, long *d, long *x, long *y) { + + if (b == 0) { + + *d = a; + + *x = 1; + + *y = 0; + + } + + else { + + long x0, y0; + + gcd (b, a%b, d, &x0, &y0); + + *x = y0; + + *y = x0 - (a/b) * y0; + + } + +} + +int main() + +{ + + ifstream f ("inversmodular.in"); + + ofstream g ("inversmodular.out"); + + long a, n, d, x, y; + + f >> a >> n; + + gcd (a, n, &d, &x, &y); + + while (x < 0) + + x += n; + + g << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/e20cec4f-f591-41f3-9a15-9c4be1548e45.cpp b/invmod/extended-euclid-algo/e20cec4f-f591-41f3-9a15-9c4be1548e45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b546b7f2fc98cb445bf6c0096b8d97c6b3951ae --- /dev/null +++ b/invmod/extended-euclid-algo/e20cec4f-f591-41f3-9a15-9c4be1548e45.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +void euclid(long long a, long long b, long long &X, long long &Y) +{ + if (b == 0) { + X = 1; + Y = 0; + return; + } + long long X_, Y_; + euclid(b, a % b, X_, Y_); + + + + X = Y_; + Y = X_ - a/b * Y_; +} + +int main() +{ + freopen("inversmodular.in", "r", stdin); + freopen("inversmodular.out", "w", stdout); + + long long a, MOD, X, Y; + cin >> a >> MOD; + euclid(a, MOD, X, Y); + cout << (X % MOD + MOD) % MOD << "\n"; + return 0; +} diff --git a/invmod/extended-euclid-algo/e2d000bb-1e29-431c-91e4-86d742820195.cpp b/invmod/extended-euclid-algo/e2d000bb-1e29-431c-91e4-86d742820195.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbe214cf070044356acd5422f5449923c75c9d0a --- /dev/null +++ b/invmod/extended-euclid-algo/e2d000bb-1e29-431c-91e4-86d742820195.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; +ifstream fin ("inversmodular.in"); +ofstream fout ("inversmodular.out"); +int gcdextins (int a,int b,int &x,int &y) +{ + if(b==0) + { + x=1;y=0; + return a; + } + else + { + int x0,y0,d; + d=gcdextins(b,a%b,x0,y0); + x=y0; + y=x0-(a/b)*y0; + return d; + } +} +int main () +{ + int a,b,x,y,d; + fin>>a>>b; + d=gcdextins(a,b,x,y); + while(x<0) + x+=b; + fout< +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void modularInverse(int a, int n, int &inverse, int &x) { + + if (!n) { + + inverse = 1; + + x = 0; + + return; + + } + + modularInverse(n, a % n, inverse, x); + + int aux = inverse; + + inverse = x; + + x = aux - x * (a / n); + +} + + + +int main() { + + int a, n, inverse, x; + + fin >> a >> n; + + modularInverse(a, n, inverse, x); + + if (inverse < 0) + + inverse += n; + + fout << inverse; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/e36e9084-26a9-46b7-a397-d883e9e2f930.cpp b/invmod/extended-euclid-algo/e36e9084-26a9-46b7-a397-d883e9e2f930.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1de2360248e8bb992e0039eb2d7f6ad7e806a619 --- /dev/null +++ b/invmod/extended-euclid-algo/e36e9084-26a9-46b7-a397-d883e9e2f930.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); +ofstream fout("inversmodular.out"); + + + +void euclid(int a, int b, int &x, int &y) +{ + if (b == 0) + { + x = 1; + y = 0; + } + else + { + int x0, y0; + euclid(b, a%b, x0, y0); + x = y0; + y = x0 - (a/b) * y0; + } +} + +int main() +{ + int a, n; + fin >> a >> n; + int rez, gunoi; + + euclid(a, n, rez, gunoi); + if (rez > 0) + fout << rez; + else + fout << rez + n; + return 0; +} diff --git a/invmod/extended-euclid-algo/e50456a6-5279-4d4a-9c0a-730f9a24c383.cpp b/invmod/extended-euclid-algo/e50456a6-5279-4d4a-9c0a-730f9a24c383.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2d9d78ba198847ea3fd6603ce2686b85d53a27 --- /dev/null +++ b/invmod/extended-euclid-algo/e50456a6-5279-4d4a-9c0a-730f9a24c383.cpp @@ -0,0 +1,60 @@ +#include + + +using namespace std; + + + +int A, N; + + + +void modularinverse(long long &x, long long &y, int a, int b) + +{ + + if (b == 0) + + { + + x = 1; + + y = 0; + + return; + + } + + modularinverse(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + +} + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + scanf("%d%d", &A, &N); + + long long x = 0, y; + + modularinverse(x, y, A, N); + + if (x <= 0) + + x = N + x % N; + + printf("%lld", x); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/e53e1689-b156-4588-a1be-255a5d1a674f.cpp b/invmod/extended-euclid-algo/e53e1689-b156-4588-a1be-255a5d1a674f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f05e7f05532edf0b1373439f33501085f04847a --- /dev/null +++ b/invmod/extended-euclid-algo/e53e1689-b156-4588-a1be-255a5d1a674f.cpp @@ -0,0 +1,52 @@ +#include + + +using namespace std; + + + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + + + +void EuclidExtins(int a, int b, int d, int &x, int &y){ + + if(b == 0){ + + x = 1; + + y = 0; + + d = a; + + } + + else{ + + int x0, y0; + + EuclidExtins(b, a%b, d, x0, y0); + + x = y0; + + y = x0 - y0 * (a/b); + + } + +} + + + +int main(){ + + int A,N,d,x,y; f>>A>>N; + + EuclidExtins(A,N,d,x,y); + + while(x < 0) x += N; + + g< + + +using namespace std; + +void gcd(long long &x,long long &y,int a,int b) { + + if (b==0) x=1,y=0; + + else { + + gcd(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{ long long x=0,y;int a,n; + + ifstream f("inversmodular.in"); + + ofstream g("inversmodular.out"); + + f>>a>>n; + + gcd(x,y,a,n); + + if (x<=0) x=n+x%n; + + g< +#include + + +using namespace std; + + + +pair euclid(int x, int y) + +{ + + if(y==0) + + return {1, 0}; + + auto p=euclid(y, x%y); + + return {p.second, p.first-(x/y)*p.second}; + +} + + + +int x, n; + + + +int main() + +{ + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + scanf("%d %d", &x, &n); + + auto invers=euclid(x, n); + + int y=invers.first; + + while(y<0) + + y+=n; + + printf("%d", y); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/e82a399d-0517-4232-885b-066e7aa85563.cpp b/invmod/extended-euclid-algo/e82a399d-0517-4232-885b-066e7aa85563.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3324ec44d1bc06b1d0f8a1ad9b0be806a628852 --- /dev/null +++ b/invmod/extended-euclid-algo/e82a399d-0517-4232-885b-066e7aa85563.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void euclidex(int a, int b,int &x, int &y){ + + if(b==0){ + + y=0; + + x=1; + + } + + else{ + + int xx,yy; + + euclidex(b,a%b,xx,yy); + + x=yy; + + y=xx-(a/b)*yy; + + } + +} + +int a,b,x,y; + +int main(){ + + fin>>a>>b; + + euclidex(b,a,x,y); + + while(y<0) + + y+=b; + + + + fout< +#include + + + + +using namespace std; + + + + + +char const in_file[] = "inversmodular.in"; + +char const out_file[] = "inversmodular.out"; + + + + + +ifstream Read(in_file); + +ofstream Write(out_file); + + + + + +void GCD( + + uint32_t const first, + + uint32_t const second, + + int64_t &x, + + int64_t &y + +) { + + if (second == 0) { + + x = 1; + + y = 0; + + return; + + } + + + + int64_t x0; + + int64_t y0; + + GCD(second, first % second, x0, y0); + + + + x = y0; + + y = x0 - (first / second) * y0; + +} + + + + + +int main() { + + uint32_t a; + + uint32_t n; + + + + Read >> a; + + Read >> n; + + + + int64_t x; + + int64_t y; + + GCD(a, n, x, y); + + + + x = x % n + (x < 0 ? n : 0); + + + + Write << x; + + + + Read.close(); + + Write.close(); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/eaa0bb2a-7fe8-45c3-b105-5896dd3ec190.cpp b/invmod/extended-euclid-algo/eaa0bb2a-7fe8-45c3-b105-5896dd3ec190.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80a02fd92182aca8161b19c0e482041d7a253e78 --- /dev/null +++ b/invmod/extended-euclid-algo/eaa0bb2a-7fe8-45c3-b105-5896dd3ec190.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n; + +int x, y, x00, y00; + + + +void cmmdc(int a, int b); + + + +int main() + +{ + + + + fin >> a >> n; + + + + cmmdc(n, a); + + + + while(y < 0) y += n; + + + + fout << y; + + + + return 0; + +} + + + +void cmmdc(int a, int b){ + + if(!b){ + + x = 1; + + y = 0; + + return; + + } + + else{ + + cmmdc(b, a % b); + + x00 = y; + + y00 = x - y * (a / b); + + x = x00; y = y00; + + } + +} diff --git a/invmod/extended-euclid-algo/eaefec17-e737-4c73-927a-2b763c7f3733.cpp b/invmod/extended-euclid-algo/eaefec17-e737-4c73-927a-2b763c7f3733.cpp new file mode 100644 index 0000000000000000000000000000000000000000..360a5a24ab2edfdca8e0503c1fa70103973801bb --- /dev/null +++ b/invmod/extended-euclid-algo/eaefec17-e737-4c73-927a-2b763c7f3733.cpp @@ -0,0 +1,96 @@ +#include +#include + + +std::ifstream f ("inversmodular.in"); + +std::ofstream g ("inversmodular.out"); + + + +using namespace std; + + + +void extins(int a, int b, int& x, int& y) { + + + + + + int r; + + int z, t; + + + + + + + + if (b != 0) { + + + + extins(b, a%b, x, y); + + } + + if (b == 0) { + + x = 1; + + y = 0; + + } + + else { + + z = y; + + t = x - (a/b)*y; + + + + x = z; + + y = t; + + } + +} + + + + + +int main() + +{ + + + + int a, b; + + + + int x, y; + + f >> a >> b; + + + + extins(a, b, x, y); + if (x < 0) { + + g << x + b << "\n"; + + } else { + + g << x << "\n"; + + } + + return 0; + +} diff --git a/invmod/extended-euclid-algo/ec60c231-cfe4-47d9-b55a-b59b68e9461e.cpp b/invmod/extended-euclid-algo/ec60c231-cfe4-47d9-b55a-b59b68e9461e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e586df7270d95582697cea96741f7cfa621224d --- /dev/null +++ b/invmod/extended-euclid-algo/ec60c231-cfe4-47d9-b55a-b59b68e9461e.cpp @@ -0,0 +1,56 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invers(long long &x,long long &y,long long a,long long b) + +{ + + if(b==0) + + y=0,x=1; + + else + + { + + invers(x,y,b,a%b); + + long long aux; + + aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + +} + +int main() + +{ + + long long n,minune,a,b; + + fin>>n>>minune; + + a=1; + + invers(a,b,n,minune); + + if(a<=0) + + a=minune+a%minune; + + fout< + + +using namespace std; + +ifstream f("inversmodular.in"); + +ofstream g("inversmodular.out"); + +long long x,y,a,n; + +void euclid(long long &x,long long &y,int a,int b) + +{ + + if(!b) + + { + + x=1; + + y=0; + + } + + else + + { + + euclid(x,y,b,a%b); + + long long aux=x; + + x=y; + + y=aux-y*(a/b); + + } + +} + +int main() + +{f>>a>>n; + +euclid(x,y,a,n); + +if(x<=0) + + x=n+x%n; + +g< +#include +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +long long A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if(b == 0){ + + x = 1; + + y = 0; + + } + + else + + { + + gcd(x,y,b,a%b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv, ins; + + fin >> A >> N; + + gcd(inv,ins,A,N); + + if(inv <= 0){ + + inv = N + inv % N; + + } + + fout << inv << "\n"; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/f073a11c-6f49-4118-9b41-b57b7c8102ff.cpp b/invmod/extended-euclid-algo/f073a11c-6f49-4118-9b41-b57b7c8102ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db59cd6216523426329eb846c53e649f97ee3e98 --- /dev/null +++ b/invmod/extended-euclid-algo/f073a11c-6f49-4118-9b41-b57b7c8102ff.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +void euclid(int a, int b, int& x, int& y) + +{ + + if (b == 0) + + { + + x = 1, y = 1; + + } + + else + + { + + int x1, y1; + + euclid(b, a%b, x1, y1); + + x = y1; + + y = x1 - a / b * y1; + + } + +} + + + +int main() + +{ + + int A, N; + + fin >> A >> N; + + int x, y; + + euclid(A, N, x, y); + + while (x < 0) + + x += N; + + fout << x; + + fin.close(), fout.close(); + + return 0; + + + +} diff --git a/invmod/extended-euclid-algo/f1f23765-5367-485f-8fdc-3e6ce0716060.cpp b/invmod/extended-euclid-algo/f1f23765-5367-485f-8fdc-3e6ce0716060.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a163e6e4e4a065d3438923c69691f2a4727ecfd --- /dev/null +++ b/invmod/extended-euclid-algo/f1f23765-5367-485f-8fdc-3e6ce0716060.cpp @@ -0,0 +1,70 @@ +#include + + + + + + +int A, N; + + + +void gcd(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + gcd(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long inv = 0, ins; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &N); + + gcd(inv, ins, A, N); + + + + if (inv <= 0) + + inv = N + inv % N; + + + + printf("%lld\n", inv); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/f34ad2a6-a769-495b-9999-8802013d0622.cpp b/invmod/extended-euclid-algo/f34ad2a6-a769-495b-9999-8802013d0622.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22de0846a6bb15515f035489f98ac24e715b91e1 --- /dev/null +++ b/invmod/extended-euclid-algo/f34ad2a6-a769-495b-9999-8802013d0622.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +long long x1,w; + +int p,n; + +void cmmdc(int a,int b,long long &x,long long &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + + + else + + { + + long long x0; + + long long y0; + + cmmdc(b,a%b,x0,y0); + + x=y0; + + y=x0-(a/b)*y0; + + } + +} + +int main() + +{ + + fin>>p>>n; + + cmmdc(p,n,x1,w); + + if(x1<=0)x1=n+x1%n; + + fout < + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + +int a, n, x, y; + + + +void euclid(int a, int b, int &x, int &y) + +{ + + if(b==0) + + { + + x=1; y=0; + + return; + + } + + int x0, y0; + + euclid(b, a%b, x0, y0); + + x=y0; + + y=x0-(a/b)*y0; + +} + + + +int main() + +{ + + fin>>a>>n; + + euclid(a, n, x, y); + + if(x<=0) x=n+x%n; + + fout< +#include + + +int n; + +int A; + + + +void fcc(long long &x, long long &y, int a, int b) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + fcc(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + long long sol = 0, s; + + + + freopen("inversmodular.in", "r", stdin); + + freopen("inversmodular.out", "w", stdout); + + + + scanf("%d %d", &A, &n); + + fcc(sol, s, A, n); + + + + if (sol <= 0) + + sol = n + sol % n; + + + + printf("%lld\n", sol); + + + + return 0; + +} diff --git a/invmod/extended-euclid-algo/f61df7bc-e375-4b44-b0d2-15db658ed946.cpp b/invmod/extended-euclid-algo/f61df7bc-e375-4b44-b0d2-15db658ed946.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6789cae0a2ea5272a17999c3c00278c7809b4f95 --- /dev/null +++ b/invmod/extended-euclid-algo/f61df7bc-e375-4b44-b0d2-15db658ed946.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +int main() + +{ + + ifstream cin("inversmodular.in"); + + ofstream cout("inversmodular.out"); + + int y0,y,y1,aux,b,a,c,r; + + y0=0;y1=1;cin>>a>>b; + + aux=b; + + while(a!=0){ + + r=b%a; + + c=b/a; + + b=a; + + a=r; + + y=y0-c*y1; + + y0=y1; + + y1=y; + + } + + while(y0<0) + + y0+=aux; + + cout< +#include + + +using namespace std; + + + +typedef long long ll; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +ll a, b, d, yy, xx; + + + +ll gcd(ll a, ll b, ll &x, ll &y) + +{ + + if(!b) + + { + + x=1, y=0; + + return a; + + } + + ll rez, y1, x1; + + rez=gcd(b, a%b, x1, y1); + + x=y1; + + y=x1-(a/b)*y1; + + return rez; + +} + + + +int main() + +{ + + in>>a>>b; + + d=gcd(a, b, xx, yy); + + if(xx<=0) xx=b+xx%b; + + out< + + + + +using namespace std; + + + +ifstream in("inversmodular.in"); + +ofstream out("inversmodular.out"); + + + +inline void gcd(long long a, long long b, long long &x, long long &y) + +{ + + if (!b) + + x = 1, y = 0; + + else + + { + + long long x0, y0; + + gcd(b, a % b, x0, y0); + + x = y0; + + y = x0 - (a / b) * y0; + + } + +} + + + +int main() + +{ + + long long a, n, inv, y; + + in >> a >> n; + + gcd(a, n, inv, y); + + if (inv < 0) + + inv = n + inv % n; + + out << inv; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/f958040b-410a-45d1-8a57-e728a741e2d4.cpp b/invmod/extended-euclid-algo/f958040b-410a-45d1-8a57-e728a741e2d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cde89fc0433e5279890338ab2c2ab01841087bff --- /dev/null +++ b/invmod/extended-euclid-algo/f958040b-410a-45d1-8a57-e728a741e2d4.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + +int A, n; + +struct elem + +{ + + int div; + + int x0; + + int y0; + +}; + +elem euclid_extins (int a, int b) + +{ + + if(b==0) + + { + + elem ultim; + + ultim.div=a; ultim.x0=1; ultim.y0=0; + + return ultim; + + } + + elem d=euclid_extins(b, a%b); + + + + elem nou; + + nou.x0=d.y0; + + nou.y0=d.x0-(a/b)*d.y0; + + nou.div=d.div; + + return nou; + +} + +int main() + +{ + + fin>>A>>n; + + elem rez=euclid_extins(A, n); + + int afis=rez.x0; + + if(afis>n) afis%=n; + + else while(afis<0) afis+=n; + + fout< +using namespace std; + +ifstream fin( "inversmodular.in" ); + +ofstream fout( "inversmodular.out" ); + +void fast() + +{ + + cin.tie( 0 ); + + ios_base::sync_with_stdio( 0 ); + +} + +int a,b; + +void invers( int a, int b, int &x, int &y ) + +{ + + if( !b ) + + { + + x = 1; + + y = 0; + + return; + + } + + + + int x0, y0; + + invers( b, a % b, x0, y0 ); + + x = y0; + + y = x0 - y0 * ( a / b ); + +} + +int main() + +{ + + fast(); + + fin >> a >> b; + + int x, y; + + invers( a, b, x, y ); + + + + while( x < 0 ) + + x = ( x + b ) % b; + + + + fout << x; + + return 0; + +} diff --git a/invmod/extended-euclid-algo/fb7d058a-17e6-4b1c-8f75-c1ee691f3fdd.cpp b/invmod/extended-euclid-algo/fb7d058a-17e6-4b1c-8f75-c1ee691f3fdd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..696f3ddcb56a93dae87b86594268ea435cf9f855 --- /dev/null +++ b/invmod/extended-euclid-algo/fb7d058a-17e6-4b1c-8f75-c1ee691f3fdd.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + + + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + + + + + + + +int A, N; + + + +void Euclid(long long &x, long long &y, int a, int b) + +{ + + if(b == 0) { + + x = 1; + + y = 0; + + } + + else { + + Euclid(x, y, b, a % b); + + long long aux = x; + + x = y; + + y = aux - y * (a / b); + + } + +} + + + +int main() + +{ + + fin.tie(NULL); + + fin >> A >> N; + + + + long long inv = 0, ins; + + Euclid(inv, ins, A, N); + + + + if(inv <= 0) + + inv = N + inv % N; + + + + fout << inv << "\n"; + +} diff --git a/invmod/extended-euclid-algo/fc0adbd6-fdce-42d8-8164-709a07e3751c.cpp b/invmod/extended-euclid-algo/fc0adbd6-fdce-42d8-8164-709a07e3751c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9732e1cf73db31d31a26dccd60187dcc33a8b890 --- /dev/null +++ b/invmod/extended-euclid-algo/fc0adbd6-fdce-42d8-8164-709a07e3751c.cpp @@ -0,0 +1,54 @@ +#include + + + + +long long a,n,aux,inv,ins; + +using namespace std; + +ifstream fin("inversmodular.in"); + +ofstream fout("inversmodular.out"); + +void invers(long long &x,long long &y,int a,int b) + +{ + + if(b==0) + + x=1, y=0; + + else + + { + + invers(x,y,b,a%b); + + aux=x; + + x=y; + + y=aux-(a/b)*y; + + } + +} + +int main() + +{ + + fin>>a>>n; + + invers(inv,ins,a,n); + + if(inv<=0) + + inv=n+inv%n; + + fout< +using namespace std; + +typedef long long ll; +const long long dx[] = {0, 1, 0, -1}; + +const long long dy[] = {1, 0, -1, 0}; + + + +const ll inf = 0x3f3f3f3f3f3f3f; + + + +const long long N = 2e5 + 11; + + + +long long a,b; + + + +long long gcd(long long a,long long b,long long &x, long long &y){ + + + + if(!b){ + + x = 1; + + y = 0; + + return b; + + } + + + + long long x0,y0,d; + + + + d = gcd(b, a%b, x0, y0); + + + + x = y0; + + y = x0 - y0*(a/b); + + + + return d; + +} + + + + + +int32_t main(){ + +ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0); + + + +ifstream cin("inversmodular.in"); + +ofstream cout("inversmodular.out"); + + + + cin >> a >> b; + + + + long long x,y; + + long long dd = gcd(a,b,x,y); + + + + while(x<0)x+=b; + + + + return cout< + + +using namespace std; + +FILE *f,*g; + + + +void inv(long long a,long long b,long long &x,long long &y) + +{ + + if(!b) + + x=1,y=0; + + else + + { + + inv(b,a%b,x,y); + + int aux=x; + + x=y; + + y=aux-a/b*y; + + } + +} + +int main() + +{ + + f=fopen("inversmodular.in","r"); + + g=fopen("inversmodular.out","w"); + + long long nr,mod,x,y; + + fscanf(f,"%lld %lld",&nr,&mod); + + inv(nr,mod,x,y); + + if(x<=0) + + x=mod+x%mod; + + fprintf(g,"%lld\n",x); + + fclose(f); + + fclose(g); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/fd5e9817-13f2-4888-95e0-decc571214d0.cpp b/invmod/extended-euclid-algo/fd5e9817-13f2-4888-95e0-decc571214d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7479f9b66f3e5ee9571723b6b7c8fa8b49954f33 --- /dev/null +++ b/invmod/extended-euclid-algo/fd5e9817-13f2-4888-95e0-decc571214d0.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + + + +pair euclid(int x, int y){ + + if(y==0) + + return {1, 0}; + + auto p=euclid(y,x%y); + + return {p.second, p.first - (x/y)*p.second }; + +} + +int a, n, x, y; + +int main() + +{ + + freopen("inversmodular.in","r",stdin); + + freopen("inversmodular.out","w",stdout); + + scanf("%d %d", &a, &n); + + auto Q=euclid(a,n); + + while(Q.first<0) + + Q.first+=n; + + printf("%d",Q.first); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/ff0f9ac5-3f7a-419d-931d-27a415d2fd5a.cpp b/invmod/extended-euclid-algo/ff0f9ac5-3f7a-419d-931d-27a415d2fd5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f40f222d9a952bc26b9af19ba9ee50bc386ad0f4 --- /dev/null +++ b/invmod/extended-euclid-algo/ff0f9ac5-3f7a-419d-931d-27a415d2fd5a.cpp @@ -0,0 +1,56 @@ +#include + + +void calculez( long long a, long long b, long long *x, long long *y ) + +{ + + if( b == 0 ){ + + *x = 1; + + *y = 0; + + } else { + + long long x0, y0; + + calculez( b, a % b, &x0, &y0 ); + + *x = y0; + + *y = x0 - ( a / b ) * y0; + + } + +} + + + +int main() + +{ + + long long a, n, x, y; + + FILE *fin = fopen( "inversmodular.in", "r" ); + + fscanf( fin, "%lld %lld", &a, &n ); + + fclose( fin ); + + calculez( a, n, &x, &y ); + + if( x < 1 ) + + x = (x + n) % n; + + FILE *fout = fopen( "inversmodular.out", "w" ); + + fprintf( fout, "%lld", x ); + + fclose( fout ); + + return 0; + +} diff --git a/invmod/extended-euclid-algo/fff15d70-6f05-4801-a6f8-4643fa175063.cpp b/invmod/extended-euclid-algo/fff15d70-6f05-4801-a6f8-4643fa175063.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42f8a4bc4d6d267d25114fed40c74cd8f6cd2c6e --- /dev/null +++ b/invmod/extended-euclid-algo/fff15d70-6f05-4801-a6f8-4643fa175063.cpp @@ -0,0 +1,78 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("inversmodular.in"); + +ofstream fout ("inversmodular.out"); + + + +int euclid (int a, int b, int &x, int &y) + +{ + + if(b==0) + + { + + x=1; + + y=0; + + } + + + + else + + { + + int x1, y1; + + + + euclid(b, a%b, x1, y1); + + x=y1; + + y=x1-a/b*y1; + + } + +} + + + + + +int main() + +{ + + int a, n, x, y; + + + + fin>>a>>n; + + + + euclid (a, n, x, y); + while(x<0) + + { + + x=x+n; + + } + + + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n,m,sol=0,tata[200005]; + +vector< pair >vecini[200005]; + +bool fost[200005]; + + + +set< pair > >perechi; + + + +void adauga(int nod) + +{ + + for(int i=0; ifirst; + + int x=perechi.begin()->second.first; + + int plecare=perechi.begin()->second.second; + + perechi.erase(perechi.begin()); + + + + if(!fost[x]) + + { + + tata[x]=plecare; + + sol+=cost; + + dfs(x); + + } + + } + +} + + + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y,z; + + f>>x>>y>>z; + + vecini[x].push_back(make_pair(y,z)); + + vecini[y].push_back(make_pair(x,z)); + + } + + dfs(1); + + g< + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + +int const NMAX = 2e5; +int const MMAX = 4e5; + +struct Edge { + int from; + int to; + int cost; + + bool operator<(Edge other) const { + return (cost < other.cost); + } +}; + +int n, m; +Edge edges[1 + MMAX]; +int boss[1 + NMAX]; +int sz[1 + NMAX]; + +int sol; +vector treeEdges; + +int getBoss(int i) { + if (boss[i] != i) { + boss[i] = getBoss(boss[i]); + } + return boss[i]; +} + +void doUnion(int bossFrom, int bossTo) { + if (sz[bossFrom] <= sz[bossTo]) { + boss[bossFrom] = bossTo; + sz[bossTo] += sz[bossFrom]; + } else { + boss[bossTo] = bossFrom; + sz[bossFrom] += sz[bossTo]; + } +} + +int main() { + in >> n >> m; + for (int i = 1; i <= m; i++) { + in >> edges[i].from >> edges[i].to >> edges[i].cost; + } + sort(edges + 1, edges + m + 1); + for (int i = 1; i <= n; i++) { + boss[i] = i; + sz[i] = 1; + } + for (int i = 1; i <= m; i++) { + int bossFrom = getBoss(edges[i].from); + int bossTo = getBoss(edges[i].to); + if (bossFrom != bossTo) { + sol += edges[i].cost; + treeEdges.push_back(i); + doUnion(bossFrom, bossTo); + } + } + out << sol << "\n" << treeEdges.size() << "\n"; + for (int i = 0; i < treeEdges.size(); i++) { + out << edges[treeEdges[i]].from << " " << edges[treeEdges[i]].to << "\n"; + } + return 0; +} diff --git a/mst/kruskal/01ead525-b3b2-49cc-a932-b5883540a722.cpp b/mst/kruskal/01ead525-b3b2-49cc-a932-b5883540a722.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fca35119a4b45eff0bdadf18ca82bd276dd52140 --- /dev/null +++ b/mst/kruskal/01ead525-b3b2-49cc-a932-b5883540a722.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie { + + int operator< (const muchie &b) const { + + return c < b.c; + + } + + int a, b, c; + +}; + + + +muchie m; + +int n, x, y, c, T[200005], cost, mn; + +vector v; + +vector< pair > rez; + + + +int rad(int x) { + + while (T[x] > 0) + + x = T[x]; + + return x; + +} + + + +void process(muchie m) { + + int ra = rad(m.a); + + int rb = rad(m.b); + + + + if (ra != rb){ + + cost += m.c; + + + + rez.push_back(make_pair(m.a, m.b)); + + + + if (T[ra] < T[rb]) { + + T[ra] += T[rb]; + + T[rb] = ra; + + } else { + + T[rb] += T[ra]; + + T[ra] = rb; + + } + + } + +} + + + +void afisare(pair pr) { + + fout << pr.first << " " << pr.second << "\n"; + +} + + + +int main() { + + fin >> n >> mn; + + + + while (fin >> m.a >> m.b >> m.c) + + v.push_back(m); + + + + for (int i=1;i<=n;++i) + + T[i] = -1; + + + + sort(v.begin(), v.end()); + + + + for_each(v.begin(), v.end(), process); + + + + fout << cost << "\n" << n-1 << "\n"; + + + + for_each(rez.begin(), rez.end(), afisare); + +} diff --git a/mst/kruskal/02422afa-121c-4086-9183-e4d8000405ef.cpp b/mst/kruskal/02422afa-121c-4086-9183-e4d8000405ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7e251a5fae485d9bba8ded17b6d02f680210697 --- /dev/null +++ b/mst/kruskal/02422afa-121c-4086-9183-e4d8000405ef.cpp @@ -0,0 +1,104 @@ +#include + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int MAXN = 2e5 + 5, MAXM = 4e5 + 5; + +int n, m, tree[MAXN]; + +struct edge { + + int node1, node2, cost; + +}; + + + +bool compareByCost(edge i, edge j) { + + return (i.cost < j.cost); + +} + + + +int findRoot(int node) { + + if (node == tree[node]) + + return node; + + tree[node] = findRoot(tree[node]); + + return tree[node]; + +} + + + +int main() { + + fin.tie(0); + + fout.tie(0); + + ios::sync_with_stdio(0); + + fin >> n >> m; + + vector graph(m); + + for (int i = 0; i < m; i++) + + fin >> graph[i].node1 >> graph[i].node2 >> graph[i].cost; + + sort(graph.begin(), graph.end(), compareByCost); + + for (int i = 1; i <= n; i++) + + tree[i] = i; + + + + long long totalCost = 0; + + vector > minTree; + + for (int i = 0; i < m; i++) { + + int rootA = findRoot(graph[i].node1); + + int rootB = findRoot(graph[i].node2); + + if (rootA != rootB) { + + minTree.push_back(make_pair(graph[i].node1, graph[i].node2)); + + totalCost += graph[i].cost; + + tree[min(rootA, rootB)] = tree[max(rootA, rootB)]; + + + + } + + } + + + + fout << totalCost << "\n" << n - 1 << "\n"; + + for (int i = 0; i < n - 1; i++) + + fout << minTree[i].first << " " << minTree[i].second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/027db3d2-cccf-4f11-89fa-a74b2b3ac9c9.cpp b/mst/kruskal/027db3d2-cccf-4f11-89fa-a74b2b3ac9c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5558cdbec045befb95dae0776eb215176fda451 --- /dev/null +++ b/mst/kruskal/027db3d2-cccf-4f11-89fa-a74b2b3ac9c9.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +pair > input[400005]; + +int root[400005]; + + + +int top(int a) { + + if (root[a] == a) + + return a; + + return root[a] = top(root[a]); + +} + + + +bool comp(pair > a, pair > b) { + + return a.second.second < b.second.second; + +} + + + +void connect(int a, int b) { + + if (root[a] == a) + + return; + + connect(root[a], b); + + root[a] = b; + +} + + + +int main() { + + int n, m; + + + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> input[i].first >> input[i].second.first >> input[i].second.second; + + + + for (int i = 1; i <= n; i++) + + root[i] = i; + + + + sort(input + 1, input + m + 1, comp); + + + + long long result = 0; + + vector > edges; + + for (int i = 1; i <= m; i++) + + if (root[top(input[i].first)] != root[top(input[i].second.first)]) { + + result += input[i].second.second; + + + + root[top(input[i].second.first)] = top(input[i].first); + + edges.push_back(make_pair(input[i].first, input[i].second.first)); + + } + + + + + + fout << result << '\n' << edges.size() << '\n'; + + for (pair edge : edges) + + fout << edge.first << ' ' << edge.second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/03479ec5-c4da-4076-8a62-a1e144b887b7.cpp b/mst/kruskal/03479ec5-c4da-4076-8a62-a1e144b887b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..347643d65dbd470e999bce11854530fbb067ae7b --- /dev/null +++ b/mst/kruskal/03479ec5-c4da-4076-8a62-a1e144b887b7.cpp @@ -0,0 +1,142 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int N_MAX = 400010; + +int n, m, i, TOTAL = 0, TOP = 0, T[N_MAX], RG[N_MAX]; + +pair SOL[N_MAX]; + +struct pct + +{ + + int x, y, cost; + +} V[N_MAX]; + + + +bool Compara(pct a, pct b) + +{ + + return a.cost < b.cost; + +} + + + +int Find(int nod) + +{ + + while (nod != T[nod]) + + nod = T[nod]; + + return nod; + +} + + + +void Unire(int x, int y) + +{ + + if (RG[x] < RG[y]) + + T[x] = y; + + else + + if (RG[x] > RG[y]) + + T[y] = x; + + else + + if (RG[x] == RG[y]) + + { + + T[x] = y; + + ++RG[y]; + + } + +} + + + +void Rezolvare() + +{ + + for (int i = 1; i <= m; ++i) + + { + + int tatal_x = Find(V[i].x); + + int tatal_y = Find(V[i].y); + + if (tatal_x != tatal_y) + + { + + Unire(tatal_x, tatal_y); + + SOL[++TOP].first = V[i].x; + + SOL[TOP].second = V[i].y; + + TOTAL += V[i].cost; + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (i = 1; i <= m; ++i) + + f >> V[i].x >> V[i].y >> V[i].cost; + + sort(V + 1, V + m + 1, Compara); + + for (i = 1; i <= n; ++i) + + T[i] = i; + + Rezolvare(); + + g << TOTAL << '\n' << TOP << '\n'; + + for (i = 1; i <= TOP; ++i) + + g << SOL[i].first << " " << SOL[i].second << '\n'; + + return 0; + +} diff --git a/mst/kruskal/038aa130-9a04-4b28-82d3-1e21eadb24f8.cpp b/mst/kruskal/038aa130-9a04-4b28-82d3-1e21eadb24f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d81a2d6c1a62f0d75c46e42826d4f09c18bfce42 --- /dev/null +++ b/mst/kruskal/038aa130-9a04-4b28-82d3-1e21eadb24f8.cpp @@ -0,0 +1,104 @@ +#include + +using namespace std; + +struct DS { + int n; + vector color, cnt; + vector> adj; + + DS(int n) : n(n), color(n), cnt(n, 1), adj(n) { + for (int i = 0; i < n; ++i) + color[i] = i; + } + + bool Connected(int a, int b) { + + return color[a] == color[b]; + } + + void dfs(int node, int col) { + color[node] = col; + cnt[col]++; + + for (auto vec : adj[node]) { + if (color[vec] != col) { + dfs(vec, col); + } + } + } + + void Link(int a, int b) { + + + if (cnt[color[a]] < cnt[color[b]]) { + cnt[color[a]] = 0; + dfs(a, color[b]); + } else { + cnt[color[b]] = 0; + dfs(b, color[a]); + } + + adj[a].push_back(b); + adj[b].push_back(a); + } +}; + +struct Graph { + struct Edge { + int a, b, c; + Edge(int a, int b, int c) : a(a), b(b), c(c) {} + + bool operator<(const Edge& oth) const { + return c < oth.c; + } + }; + + int n; + vector edges; + vector mst_edges; + + Graph(int n) : n(n) {} + + void AddEdge(int a, int b, int c) { + edges.emplace_back(a, b, c); + } + + long long ComputeMST() { + + sort(edges.begin(), edges.end()); + + + DS ds(n); + long long answer = 0; + for (auto e : edges) { + + + if (!ds.Connected(e.a, e.b)) { + answer += e.c; + + ds.Link(e.a, e.b); + mst_edges.push_back(e); + } + } + return answer; + } +}; + +int main() { + ifstream cin("apm.in"); + ofstream cout("apm.out"); + + int n, m; cin >> n >> m; + Graph G(n); + for (int i = 0; i < m; ++i) { + int a, b, c; cin >> a >> b >> c; --a; --b; + G.AddEdge(a, b, c); + } + cout << G.ComputeMST() << endl; + cout << G.mst_edges.size() << endl; + for (auto e : G.mst_edges) { + cout << e.a + 1 << " " << e.b + 1 << '\n'; + } + return 0; +} diff --git a/mst/kruskal/0412dff5-3797-40d8-b5c5-16592b01c5c3.cpp b/mst/kruskal/0412dff5-3797-40d8-b5c5-16592b01c5c3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c35e021ede28d0584dda6c27868eee8942283de --- /dev/null +++ b/mst/kruskal/0412dff5-3797-40d8-b5c5-16592b01c5c3.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +pair fin[400001]; + + + +int n, m, T[200001], index; + + + +struct Edge + +{ + + int i, j, cost; + + + +}E[400001]; + + + +void read() + +{ + + f >> n >> m; + + + + for (int i = 1; i <= m; i++) + + + + f >> E[i].i >> E[i].j >> E[i].cost; + +} + + + +bool way(Edge a , Edge b) + +{ + + return a.cost < b.cost; + +} + + + +int find(int x) + +{ + + if (T[x] == x)return x; + + + + else + + + + { + + T[x] = find(T[x]); + + + + return T[x]; + + } + +} + + + +void Union(int x, int y) + +{ + + T[x] = T[y]; + +} + + + +void solve() + +{ + + sort(E + 1, E + m + 1, way); + + + + long s = 0; + + + + for (int i = 1; i <= n; i++)T[i] = i; + + + + for (int i = 1; i <= m; i++) + + { + + int x = find(E[i].i); + + int y = find(E[i].j); + + + + if (x != y) + + { + + Union(T[x], T[y]); + + + + fin[++index]=(make_pair(E[i].i, E[i].j)); + + + + s += E[i].cost; + + } + + } + + g << s << '\n' << index << '\n'; + + + + for (int i = 1; i <= index; i++) + + + + g << fin[i].first << " " << fin[i].second << '\n'; + +} + +int main() + +{ + + read(); + + solve(); + +} diff --git a/mst/kruskal/04a97224-7831-4cc6-ab5e-a9d79965d399.cpp b/mst/kruskal/04a97224-7831-4cc6-ab5e-a9d79965d399.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365f19c5e07464f5bbef766c25dbe5869f148e91 --- /dev/null +++ b/mst/kruskal/04a97224-7831-4cc6-ab5e-a9d79965d399.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< pair >apm; + + + +int n, m, t[400001], s; + + + +struct muchii + +{ + + int x, y, cost; + +}; + + + +muchii lm[400001]; + + + +bool cmp(muchii a, muchii b) + +{ + + return a.cost> n >> m; + + + + for(int i = 0; i < m; i++) + + f >> lm[i].x >> lm[i].y >> lm[i].cost; + + + + sort(lm, lm + m, cmp); + + + + for(int i = 1; i <= n; i++) + + t[i] = i; + + + + for(int i = 0; i < m; i++) + + if(rep(lm[i].x) != rep(lm[i].y)) + + { + + t[rep(lm[i].y)] = lm[i].x; + + s += lm[i].cost; + + apm.push_back ( make_pair(lm[i].x,lm[i].y)); + + } + + + + g< +#include + + +using namespace std; + + + +ifstream in ("apm.in"); + +ofstream out ("apm.out"); + + + +struct ura + +{ + + int x, y; + + int cost; + +} muchii[400137]; + + + +bool cmp ( ura a, ura b ); + + + +int findee ( int nod ); + + + +void gluee ( int nod1, int nod2 ); + + + +int n, m; + + + +int h[400137]; + + + +int r[400137]; + + + +int t[400137]; + + + +int nr; + + + +int sorin; + + + +int main() + +{ + + in >> n >> m; + + for ( register int i = 1 ; i <= m ; ++i ) + + in >> muchii[i].x >> muchii[i].y >> muchii[i].cost; + + sort ( muchii + 1, muchii + m + 1, cmp ); + + for ( register int i = 1 ; i <= n ; ++i ) + + h[i] = 1; + + int i = 1; + + while ( nr < n - 1 ) + + { + + int nod1 = muchii[i].x; + + int nod2 = muchii[i].y; + + int t1 = findee (nod1); + + int t2 = findee (nod2); + + if ( t1 != t2 ) + + { + + ++nr; + + r[++r[0]] = nod2; + + r[++r[0]] = nod1; + + sorin += muchii[i].cost; + + gluee ( t1, t2 ); + + } + + ++i; + + } + + out << sorin << '\n'; + + out << nr << '\n'; + + i = 1; + + while ( i <= r[0] ) + + { + + out << r[i] << " " << r[i + 1] << '\n'; + + i += 2; + + } + + return 0; + +} + + + +bool cmp ( ura a, ura b ) + +{ + + return a.cost < b.cost; + +} + + + +int findee ( int nod ) + +{ + + while ( t[nod] != 0 ) + + nod = t[nod]; + + return nod; + +} + + + +void gluee ( int nod1, int nod2 ) + +{ + + if ( h[nod1] > h[nod2] ) + + t[nod2] = nod1; + + else + + { + + t[nod1] = nod2; + + if ( h[nod1] == h[nod2] ) + + ++h[nod2]; + + } + +} diff --git a/mst/kruskal/05d933f4-d00e-4675-94ef-5916494760e9.cpp b/mst/kruskal/05d933f4-d00e-4675-94ef-5916494760e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cfc038eb0451d4475205894af6de6a014ee7bbb --- /dev/null +++ b/mst/kruskal/05d933f4-d00e-4675-94ef-5916494760e9.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int father[100005], group[100005], n, m, answer; + +vector< pair > > edges; + +vector< pair > apm; + + + +int findFather(int node) { + + if(node == father[node]) + + return node; + + int new_father = findFather(father[node]); + + father[node] = new_father; + + return new_father; + +} + + + +int main () { + + int a, b, cost; + + + + f>>n>>m; + + for(int i = 1; i <= m; i++) { + + f>>a>>b>>cost; + + edges.push_back(make_pair(cost, make_pair(a, b))); + + } + + sort(edges.begin(), edges.end()); + + + + for(int i = 1; i <= n; i++) { + + father[i] = i; + + group[i] = 1; + + } + + + + for(auto edge : edges) { + + int a = edge.second.first; + + int b = edge.second.second; + + + + int fa = findFather(a); + + int fb = findFather(b); + + + + if(fa == fb) { + + continue; + + } + + + + apm.push_back(edge.second); + + answer += edge.first; + + + + if(group[fa] > group[fb]) { + + father[fb] = fa; + + group[fa] += group[fb]; + + } + + else { + + father[fa] = fb; + + group[fb] += group[fa]; + + } + + } + + + + g< +#include +#include +using namespace std; +ifstream fin("apm.in"); +ofstream fout("apm.out"); +int h[200005],t[200005],n,m,x,y,c,i,j,aux,rad,s; +struct muchii +{ + int a; + int b; + int cost; +}; +muchii e[200005],v[200005]; +int find(int k) +{ + rad=k; + while(t[rad]) + rad=t[rad]; + return rad; +} +void uniune(int a, int b) +{ + if(h[a]>h[b]) + t[b]=a; + else + if(h[a]>n>>m; + for(i=1;i<=m;i++) + { + fin>>x>>y>>c; + e[i].a=x; + e[i].b=y; + e[i].cost=c; + } + s=0; + sort(e+1,e+m+1,cmp1); + int dim=0; + for(i=1;i<=m;i++) + { + if(find(e[i].a)!=find(e[i].b)) + { + s=s+e[i].cost; + dim++; + v[dim]=e[i]; + uniune(find(e[i].a),find(e[i].b)); + } + } + fout< + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int tata[200009]; + +int h[200009]; + +struct tip{int x; int y;int c;}; + +vector v; + +long long int n,m,cost; + +vector sol; + +void citire(); + +bool compar( tip a, tip b) + +{ + + return a.ch[y]) + + {tata[y]=x;return;} + + tata[x]=y; + + if(h[x]==h[y]) + + h[y]++; + +} + + + +void afis(); + +int main() + +{citire(); + + afis(); + + return 0; + +} + +void citire() + +{int x,y,c,i; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + v.push_back({x,y,c}); + + + + } + + sort(v.begin(),v.end(),compar); + +int nrsel=0; + +for(i=0;i +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct muchie{ + + int x,y,c; + +}v[400005]; + +pair p[400005]; + +int t[200005], nr[200005]; + +int radacina(int x){ + + if(t[x]==0){ + + return x; + + } + + t[x]=radacina(t[x]); + + return t[x]; + + + +} + +void reuniune(int x, int y){ + + int rx=radacina(x); + + int ry=radacina(y); + + if(nr[rx]>n>>m; + + + + for(int i=1;i<=m;i++){ + + cin>>v[i].x>>v[i].y>>v[i].c; + + } + + sort(v+1,v+m+1,cmp); + + int k=0; + + for(int i=1;i<=m;i++){ + + if(radacina(v[i].x)!=radacina(v[i].y)){ + + reuniune(v[i].x,v[i].y); + + p[++k].first=v[i].x; + + p[k].second=v[i].y; + + total+=v[i].c; + + } + + } + + cout< +#include +using namespace std; +struct muchie +{ + int x,y,c; +}; +int n,m,i,t[200001],h[200001],k,cost,sol[200000]; +muchie a[400001]; +int compare(muchie x,muchie y) +{ + return x.ch[r2]) + { + t[r2]=r1; + c=r1; + } + else + { + t[r1]=r2; + c=r2; + } + if (h[r1]==h[r2]) h[c]++; + return 1; +} +int main() +{ + FILE *f1 = fopen("apm.in","r"); + FILE *f2 = fopen("apm.out","w"); + fscanf(f1,"%d%d",&n,&m); + for (i=1; i<=m; i++) + { + fscanf(f1,"%d%d%d",&a[i].x,&a[i].y,&a[i].c); + } + sort(a+1,a+m+1,compare); + for (i=1; i<=n; i++) + { + t[i]=i; + h[i]=1; + } + k=0; + i=1; + while (k +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m,a[400004],s,m1,v1[400004]; + +struct graf + +{ + + int x,y,c; + +}; + +bool cmp(graf x, graf y) + +{ + + if(x.c!=y.c) + + return x.c>n>>m; + + for(int i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int rx=arad(v[i].x); + + int ry=arad(v[i].y); + + if(rx==ry) + + continue; + + s+=v[i].c; + + v1[++m1]=i; + + if(rx +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + + + +struct muchii + +{ + + int x,y,cost; + +}v[400005]; + + + +int n,m,t[200005],rang[200005]; + +void citire() + +{ + + in>>n>>m; + + for(int i=1; i<=m; ++i) + + in>>v[i].x>>v[i].y>>v[i].cost; + + for(int i=1; i<=n; ++i) + + t[i]=i,rang[i]=1; + +} + +int radacina(int x) + +{ + + if(x==t[x]) return x; + + t[x]=radacina(t[x]); + + return t[x]; + +} + + + +void unire(int x, int y) + +{ + + if(rang[x]>rang[y]) + + t[y]=x; + + else t[x]=y; + + if(rang[x]==rang[y]) + + rang[y]++; + +} + +bool cmp(muchii x, muchii y) + +{ + + return x.costv[i+1].cost) + + {muchii aux; + + aux=v[i]; + + v[i]=v[i+1]; + + v[i+1]=aux; + + ok=1; + + } + + } + +} + +int selectat[200005]; + +int verif(int x, int y) + +{ + + if(radacina(x)==radacina(y)) return 1; + + return 0; + +} + +int main() + +{ + + citire(); + + sort(v+1,v+m+1,cmp); + + int suma=0,nr=0; + + for(int i=1; i<=m; ++i) + + if(verif(v[i].x,v[i].y)==0) + + { + + unire(radacina(v[i].x),radacina(v[i].y)); + + suma+=v[i].cost; + + selectat[i]=1; + + nr++; + + } + + out< + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +pair > input[400005]; + +int n, m, root[400005]; + + + +int top(int a) { + + if (root[a] == a) + + return a; + + return root[a] = top(root[a]); + +} + + + +bool comp(pair > a, pair > b) { + + return a.second.second < b.second.second; + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> input[i].first >> input[i].second.first >> input[i].second.second; + + + + for (int i = 1; i <= n; i++) + + root[i] = i; + + + + sort(input + 1, input + m + 1, comp); + + + + long long result = 0; + + vector > edges; + + for (int i = 1; i <= m; i++) + + if (root[top(input[i].first)] != root[top(input[i].second.first)]) { + + result += input[i].second.second; + + root[top(input[i].second.first)] = top(input[i].first); + + edges.push_back(make_pair(input[i].first, input[i].second.first)); + + } + + + + fout << result << '\n' << edges.size() << '\n'; + + for (pair edge : edges) + + fout << edge.first << ' ' << edge.second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/0b88c1b9-52b2-4491-8e4e-7207a390e43c.cpp b/mst/kruskal/0b88c1b9-52b2-4491-8e4e-7207a390e43c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..597d1903d1e344aa61436f402c90efca36ccd99d --- /dev/null +++ b/mst/kruskal/0b88c1b9-52b2-4491-8e4e-7207a390e43c.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n,m,a,b,c,conexe,suma,cnt,rasp[500005]; + +int tata[500005],conex[500005]; + + + +int grupa(int i) + +{ + + if(conex[i]==i)return i; + + conex[i]=grupa(conex[i]); + + return conex[i]; + +} + +void reuniune(int i,int j) + +{ + + conex[grupa(i)]=grupa(j); + +} + + + +struct triplet + +{ + + int x,y,cost; + +}; + + + +triplet muchie[500005]; + +bool cmp(triplet i,triplet j) + +{ + + return i.cost>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>muchie[i].x>>muchie[i].y>>muchie[i].cost; + + tata[i]=i; + + conex[i]=i; + + } + + sort(muchie+1,muchie+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int a=muchie[tata[i]].x; + + int b=muchie[tata[i]].y; + + if(grupa(a)!=grupa(b)) + + { + + suma+=muchie[i].cost; + + cnt++; + + rasp[cnt]=i; + + reuniune(a,b); + + } + + } + + fout< +using namespace std; + + + +vector > v; + +vector> ans; + +vector ind; + +int n, m, cost; + + + +int root(int x) { + + if(ind[x] == x) { + + return x; + + } + + return (ind[x] = root(ind[x])); + +} + + + +void fabricaDeUnire(int x, int y) { + + ind[ind[y]] = ind[x]; + +} + + + +bool cmp(const tuple & x,const tuple & b) { + + return (get<2>(x) < get<2>(b)); + +} + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + scanf("%d%d", &n, &m); + + for (int i = 0; i < m; ++i) { + + int x, y, cost; + + scanf("%d%d%d", &x, &y, &cost); + + v.emplace_back(x - 1, y - 1, cost); + + } + + sort(v.begin(), v.end(), cmp); + + + + for(int i = 0; i < n; ++i) { + + ind.push_back(i); + + } + + + + for(int i = 0; i < m; i++) { + + int x = get<0>(v[i]); + + int y = get<1>(v[i]); + + if(root(x) != root(y)) { + + cost += get<2>(v[i]); + + fabricaDeUnire(x, y); + + ans.push_back({x, y}); + + } + + } + + + + printf("%d\n%d\n", cost, n - 1); + + for (int i = 0; i < ans.size(); ++i) { + + printf("%d %d\n", ans[i].first + 1, ans[i].second + 1); + + } + + return 0; + +} diff --git a/mst/kruskal/0dfee47d-4639-418e-98ed-91aaab0bf4b7.cpp b/mst/kruskal/0dfee47d-4639-418e-98ed-91aaab0bf4b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cd982d6bd7d40a2d9ecb55ad65a33e85943468a --- /dev/null +++ b/mst/kruskal/0dfee47d-4639-418e-98ed-91aaab0bf4b7.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie + +{ + + int x,y,val; + +}; + +int tata[200001], h[200001]; + +int Reprez(int u) + +{ + + while(tata[u]!=0) + + u=tata[u]; + + return u; + +} + + + +void Reun(int ru, int rv) + +{ + + + + if(h[ru]>h[rv]) + + tata[rv]=ru; + + else + + { + + tata[ru]=rv; + + if(h[ru]==h[rv]) + + h[rv]+=1; + + } + +} + + + + + +int main() + +{ + + + + int n, m; + + fin>>n>>m; + + + + vector muchii; + + + + while(m--) + + { + + muchie m; + + fin>>m.x>>m.y>>m.val; + + muchii.push_back(m); + + } + + sort(muchii.begin(),muchii.end(),[](muchie a, muchie b) + + { + + return a.val rez; + + int cost=0; + + for(auto m : muchii) + + { + + int rx=Reprez(m.x),ry=Reprez(m.y); + + if((rx==0&&ry==0)||(rx!=ry)) + + { + + Reun(rx,ry); + + + + cost += m.val; + + rez.push_back(m); + + } + + } + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int maxn = 2e5+5, maxm = 4e5+5; + + + +struct muchie { + + int x, y, cost; + +}; + +muchie mc[maxm], mcans[maxn]; + + + +int n, m, x, y, cost, ans, k; + + + +vector nod[maxn]; + + + +int cmp(muchie a, muchie b) { + + return a.cost < b.cost; + +} + + + +int tt[maxn], rg[maxn]; + + + +int rad(int x) { + + int r, y; + + for(r = x; tt[r] != r; r = tt[r]); + + while(tt[x] != x) { + + y = tt[x]; + + tt[x] = r; + + x = y; + + } + + return r; + +} + + + +void unite(int x, int y) { + + if(rg[x] > rg[y]) { + + tt[y] = x; + + } else { + + tt[x] = y; + + } + + if(rg[x] == rg[y]) { rg[y] ++; } + +} + + + +int main() + +{ + + int i; + + f >> n >> m; + + for(i = 1; i <= n; i ++) { + + tt[i] = i; + + rg[i] = 1; + + } + + for(i = 1; i <= m; i ++) { + + f >> x >> y >> cost; + + nod[x].push_back(y); + + nod[y].push_back(x); + + mc[i] = {x, y, cost}; + + } + + sort(mc + 1, mc + m + 1, cmp); + + + + for(i = 1; i <= m; i ++) { + + x = mc[i].x; + + y = mc[i].y; + + cost = mc[i].cost; + + if((rad(x) != rad(y))) { + + unite(rad(x), rad(y)); + + ans += cost; + + mcans[++k] = {x, y, 0}; + + } + + } + + + + g << ans << '\n'; + + g << k << '\n'; + + for(i = 1; i <= k; i ++) { + + g << mcans[i].x << ' ' << mcans[i].y << '\n'; + + } + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/mst/kruskal/103440e1-6fac-488b-9702-6ac6417c19df.cpp b/mst/kruskal/103440e1-6fac-488b-9702-6ac6417c19df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9d972f82257bc38f0c2ac61d7da5e124078f177 --- /dev/null +++ b/mst/kruskal/103440e1-6fac-488b-9702-6ac6417c19df.cpp @@ -0,0 +1,245 @@ + #include + #include +#include +#include + std::ifstream in("apm" ".in"); + + std::ofstream out("apm" ".out"); +const int + +N = 100000, + +M = 400000, + +COST_MIN = -1000, COST_MAX = 1000; + + + +int n, m; + + + +struct Muchie + +{ + + std::pair incidente; + + int cost; + + + + inline bool operator <= (Muchie& alta) + + {return cost <= alta.cost;} + +} + +*muchii; + + + + + + + +void sorteaza(int start, int end) + +{ + + if (start == end) + + return; + + + + int piv = start; + + for (int itr = start; itr < end; itr++) + + if (muchii[itr] <= muchii[end - 1]) + + std::swap(muchii[piv++], muchii[itr]); + + + + sorteaza(start, piv - 1); + + sorteaza(piv, end); + +} + + + + + + + +namespace arbore_partial + +{ + + int + + *set, + + nr_muchii, + + cost; + + + + Muchie* muchii; + + + + int radacina(int rad) + + { + + std::vector drum; + + while (set[rad]) + + { + + drum.push_back(rad); + + rad = set[rad] - 1; + + } + + for (int varf: drum) + + set[varf] = rad + 1; + + return rad; + + } + + + + void gestioneaza(Muchie& muchie) + + { + + int + + &a = muchie.incidente.first, + + &b = muchie.incidente.second, + + rad_a = radacina(a), + + rad_b = radacina(b); + + + + if (rad_a != rad_b) + + { + + set[rad_a] = rad_b + 1; + + muchii[nr_muchii++] = muchie; + + cost += muchie.cost; + + } + + } + +} + + + +using arbore_partial::gestioneaza; + +using arbore_partial::radacina; + + + + + + + +int main() + +{ + + std::ios_base::sync_with_stdio(false); + in >> n >> m; + + muchii = new Muchie[m]; + + + + for (int i = 0; i < m; i++) + + { + + int a, b, cost; + + in >> a >> b >> cost; + + a--, b--; + + muchii[i] = {{a, b}, cost}; + + } + + + + sorteaza(0, m); + + arbore_partial::set = new int[n](); + + arbore_partial::muchii = new Muchie[n - 1]; + + + + for (int i = 0; i < m; i++) + + gestioneaza(muchii[i]); + + + + delete[] muchii; + + delete[] arbore_partial::set; + + + + out + + << arbore_partial::cost << '\n' + + << n - 1 << '\n'; + + + + for (int i = 0; i < n - 1; i++) + + { + + auto& incidente = arbore_partial::muchii[i].incidente; + + + + int + + &a = incidente.first, + + &b = incidente.second; + + + + out << a + 1 << ' ' << b + 1 << '\n'; + + } + + + + delete[] arbore_partial::muchii; + +} diff --git a/mst/kruskal/10f8ed62-4a06-4d88-b99f-bc0a514d2363.cpp b/mst/kruskal/10f8ed62-4a06-4d88-b99f-bc0a514d2363.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e06ed521422bf85e05944643b9c5a8920d1e90e6 --- /dev/null +++ b/mst/kruskal/10f8ed62-4a06-4d88-b99f-bc0a514d2363.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int lim = 400005; + +int n,m,t[lim],rang[lim],total,k; + +pair < int, int >P[lim]; + + + +struct Edge + +{ + + int x,y,c; + +} v[lim]; + + + +bool comp(Edge a, Edge b) + +{ + + return a.c < b.c; + +} + + + +void citire() + +{ + + fin>>n>>m; + + for(int i=1 ; i<=m ; i++) + + fin>> v[i].x >> v[i].y >> v[i].c; + + for(int i=1 ; i<=n ; i++) + + t[i]=i, rang[i]=1; + + sort(v+1, v+m+1, comp); + +} + + + +int Root(int x) + +{ + + if(t[x] == x) return x; + + else return t[x]=Root(t[x]); + +} + + + +void unit(int a, int b) + +{ + + if(rang[a] > rang[b]) t[b]=a; + + else if(rang[a] < rang[b]) t[a]=b; + + else + + { + + t[a]=b; + + rang[a]++; + + } + +} + + + +void Kruskal() + +{ + + for(int i=1 ; i<=m ; i++) + + { + + int r1=Root(v[i].x); + + int r2=Root(v[i].y); + + if(r1 != r2) + + { + + unit(r1,r2); + + total += v[i].c; + + k++; + + P[k].first = v[i].y; + + P[k].second = v[i].x; + + } + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + fout< +#include +#include +using namespace std; +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); +const int nmax=200005,mmax=400005; +int n,m,s,cnt,tt[nmax],rg[nmax]; +pair rez[nmax]; +struct muchie +{ + int x,y,c; +}v[mmax]; +bool cmp(muchie a,muchie b) +{ + return a.c> n >> m; + for (int i=1;i<=m;i++) + { + fin >> v[i].x >> v[i].y >> v[i].c; + } + sort (v+1,v+m+1,cmp); + for (int i=1;i<=n;i++) + { + tt[i]=i; + rg[i]=1; + } + apm(); + fout << s << '\n'; + fout << cnt << '\n'; + for (int i=1;i<=cnt;i++) + { + fout << rez[i].first << " " << rez[i].second << '\n'; + } +} diff --git a/mst/kruskal/151e69e3-6ff4-4188-8a20-f1b71b68c85d.cpp b/mst/kruskal/151e69e3-6ff4-4188-8a20-f1b71b68c85d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd496e2c69fb5104b4cd4a0265997e7a2c702105 --- /dev/null +++ b/mst/kruskal/151e69e3-6ff4-4188-8a20-f1b71b68c85d.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n,m,grd[200005],tata[200005],muchii[2][200005],sol=0; + + + +struct pct + +{ + + int x,y,z; + +} v[400005]; + + + +bool cmp(pct a,pct b) + +{ + + return a.zgrd[b]){ + + tata[b]=a; + + } + + if(grd[a]==grd[b]){ + + tata[a]=b; + + grd[b]++; + + } + +} + + + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + f>>v[i].x>>v[i].y>>v[i].z; + + } + + sort(v+1,v+m+1,cmp); + + int ct=0; + + for(int i=1; i<=m; i++) + + { + + int a=v[i].x,b=v[i].y,cost=v[i].z; + + int aa=reprezentant(a); + + int bb=reprezentant(b); + + + + if(aa!=bb){ + + sol+=cost; + + muchii[0][++ct]=a; + + muchii[1][ct]=b; + + leaga(aa,bb); + + } + + } + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n, m, cost; + +struct muchie + +{ + + int sursa, dest, cost; + +}; + +vector v; + +vector P; + +vector < pair > sol; + + + +bool comp(muchie A, muchie B) + +{ + + return A.cost>n>>m; + + int x, y, z; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>z; + + v.push_back({x,y,z}); + + } + + + + sort(v.begin(),v.end(),comp); + + + + for(int i=0;i<=n;i++) + + P.push_back(i); + + fin.close(); + +} + + + +int radacina(int nod) + +{ + + int rad=nod, aux; + + while(P[rad]!=rad) + + rad=P[rad]; + + while(nod!=rad) + + { + + aux=P[nod]; + + P[nod]=rad; + + nod=aux; + + } + + return rad; + +} + + + +void unire(int nod1, int nod2) + +{ + + P[P[nod2]]=P[nod1]; + +} + + + +int Kruskal() + +{ + + int x, y, z; + + for(int i=0;i +#include + + +typedef struct _edge { + + int source, dest, cost; + +}edge; + + + +int Find(int x, int *c) { + + int y = x, t; + + while(y != c[y]) + + y = c[y]; + + + + while(x != y) { + + t = c[x]; + + c[x] = y; + + x = t; + + } + + + + return x; + +} + + + +void Unite(int x, int y, int *c) { + + x = Find(x, c); + + y = Find(y, c); + + c[y] = x; + +} + + + +int cmp_edges(const void *pa, const void *pb) { + + edge a = *(edge *)pa; + + edge b = *(edge *)pb; + + + + return a.cost-b.cost; + +} + + + +int APM_Kruskal(int n, int *c, int m, edge *e, edge *out) { + + int cost = 0, sz = 0; + + + + for(int i = 0; i < m; i++) { + + int x = e[i].source, y = e[i].dest, z = e[i].cost; + + if(Find(x, c) != Find(y, c)) { + + Unite(x, y, c); + + cost+=z; + + out[sz++] = e[i]; + + } + + } + + + + return cost; + +} + + + +int main() { + + + + FILE *input = fopen("apm.in", "r"); + + FILE *output = fopen("apm.out", "w"); + + int n, m, *c; + + edge *e, *out; + + + + fscanf(input, "%d%d", &n, &m); + + + + c = (int *) calloc(n + 1, sizeof(int)); + + e = (edge *) calloc(m, sizeof(edge)); + + out = (edge *) calloc(n - 1, sizeof(edge)); + + + + for(int i = 1; i <= n; i++) + + c[i] = i; + + + + for(int i = 0; i < m; i++) + + fscanf(input, "%d %d %d", &e[i].source, &e[i].dest, &e[i].cost); + + + + qsort(e, m, sizeof(edge), cmp_edges); + + + + int apm_cost = APM_Kruskal(n, c, m, e, out); + + fprintf(output, "%d\n", apm_cost); + + + + fprintf(output, "%d\n", n - 1); + + + + for(int i = 0; i < n - 1; i++) { + + fprintf(output, "%d %d\n", out[i].source, out[i].dest); + + } + + + + free(c); + + free(e); + + free(out); + + + + fclose(input); + + fclose(output); + + + + return 0; + +} diff --git a/mst/kruskal/17ae58b4-bf32-4c58-968e-9f1782012501.cpp b/mst/kruskal/17ae58b4-bf32-4c58-968e-9f1782012501.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9456f1846780dd2990cca35e73b97553740cbeb --- /dev/null +++ b/mst/kruskal/17ae58b4-bf32-4c58-968e-9f1782012501.cpp @@ -0,0 +1,146 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct Muchie + +{ + + int Cost, x, y; + +}; + +Muchie V[400001]; + +pair Pr[400001]; + +long long Sum, k; + +int Tt[400001], Rg[400001], N, M; + +void Citire() + +{ + + f >> N >> M; + + for (int i = 1; i <= M; i++ ) + + f >> V[i].x >> V[i].y >> V[i].Cost; + +} + +int Cmp ( Muchie a1, Muchie a2 ) + +{ + + return a1.Cost < a2.Cost; + +} + +int VerificareTata(int nod) + +{ + + while ( Tt[nod] != nod) + + nod = Tt[nod]; + + return nod; + +} + +int Unire( int a1, int a2) + +{ + + if( Rg[a1] > Rg[a2] ) + + Tt[a2] = a1; + + if( Rg[a1] < Rg[a2] ) + + Tt[a1] = a2; + + if( Rg[a1] == Rg[a2] ) + + { + + Tt[a1] = a2; + + Rg[a2] ++; + + } + +} + +void Afisare() + +{ + + g << Sum << endl << k << endl; + + for (int i = 1 ; i<=k ; i++ ) + + g << Pr[i].first << " " << Pr[i].second << endl; + +} + +int main() + +{ + + Citire(); + + sort( V+1, V+M+1, Cmp); + + for ( int i = 1; i <= N; i++ ) + + { + + Tt[i] = i; + + Rg[i] = 1; + + } + + for ( int i = 1; i <= M ; i++ ) + + { + + int tatax = VerificareTata(V[i].x); + + int tatay = VerificareTata(V[i].y); + + if ( tatax != tatay ) + + { + + Unire(tatax, tatay); + + Pr[++k].first = V[i].x; + + Pr[k].second = V[i].y; + + Sum = Sum + V[i].Cost; + + } + + } + + Afisare(); + + return 0; + +} diff --git a/mst/kruskal/17d47820-6831-42bb-b41b-2886bfaeb5ae.cpp b/mst/kruskal/17d47820-6831-42bb-b41b-2886bfaeb5ae.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47547fe350372a2d8dc675e80035c03d02a01a87 --- /dev/null +++ b/mst/kruskal/17d47820-6831-42bb-b41b-2886bfaeb5ae.cpp @@ -0,0 +1,73 @@ +#include + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + +int a[400010],b[400010],c[400010],grupa[400010],ind[400010]; +int sze[400010]; +int ans; +vector ANS; + + +int Grupa(int i) +{ + if(grupa[i]==i) return i; + grupa[i]=Grupa(grupa[i]); + return grupa[i]; +} + +void unite(int i,int j) +{ + i=Grupa(i); + j=Grupa(j); + if(sze[i]>n>>m; + for(int i=1;i<=m;i++) + { + in>>a[i]>>b[i]>>c[i]; + ind[i]=i; + } + for(int i=1;i<=n;i++) + { + grupa[i]=i; + sze[i]=1; + } + sort(ind+1,ind+m+1,ok); + for(int i=1;i<=m;i++) + { + if(Grupa(a[ind[i]])!=Grupa(b[ind[i]])) + { + ans+=c[ind[i]]; + unite(a[ind[i]],b[ind[i]]); + ANS.push_back(ind[i]); + } + } + out< +#include + + +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +int sef[200005]; + + + +struct ura{ + + int x,y,cost; + +}v[400005]; + + + +struct ura2{ + + int x,y; + +}v2[200005]; + + + +bool cmp(ura a,ura b){ + + if(a.cost>n>>m; + + for(i=1;i<=n;i++) + + sef[i]=i; + + + + for(int i=1;i<=m;i++) + + cin>>v[i].x>>v[i].y>>v[i].cost; + + + + + + sort(v+1,v+m+1,cmp); + + + + + + for(int i=1;add +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int MAX = 400005; + + + +struct muchie { + + int x, y, c; + +} muchii[MAX]; + + + +int n, m, total, TT[MAX], RG[MAX], k; + + + +bool compare(muchie a, muchie b) { + + return a.c < b.c; + +} + + + +pair P[MAX]; + + + +void citire() { + + in >> n >> m; + + int x, y, c; + + for(int i = 1; i <= m; i++) { + + in >> x >> y >> c; + + muchii[i].x = x; + + muchii[i].y = y; + + muchii[i].c = c; + + } + + sort(muchii + 1, muchii + m + 1, compare); + +} + + + +int Find(int nod) { + + while(TT[nod] != nod) + + nod = TT[nod]; + + return nod; + +} + + + +void Unite(int x, int y) { + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[y] < RG[x]) + + TT[y] = x; + + if(RG[x] == RG[y]) { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() { + + for(int i = 1; i <= m; i++) { + + int tata_x = Find(muchii[i].x); + + int tata_y = Find(muchii[i].y); + + if(tata_x != tata_y) { + + Unite(tata_x, tata_y); + + P[++k].first = muchii[i].x; + + P[k].second = muchii[i].y; + + total += muchii[i].c; + + } + + } + +} + + + +int main() { + + citire(); + + + + for(int i = 1; i <= m; i++) { + + TT[i] = i; + + RG[i] = 1; + + } + + + + Solve(); + + + + out << total << "\n"; + + out << n - 1 << "\n"; + + + + for(int i = 1; i <= k; i++) { + + out << P[i].first << " " << P[i].second << " " << "\n"; + + } + + return 0; + +} diff --git a/mst/kruskal/19a94f2f-c64c-4dcd-b3db-cdabe2b7ae50.cpp b/mst/kruskal/19a94f2f-c64c-4dcd-b3db-cdabe2b7ae50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..749a97c1bfdb106095adeec42ffa8421c2221600 --- /dev/null +++ b/mst/kruskal/19a94f2f-c64c-4dcd-b3db-cdabe2b7ae50.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +set>>S; + +vector>G; + +int n,m,t,T[100001]; + +int Radacina(int x) + +{ + + if(T[x]==x)return x; + + return T[x]=Radacina(T[x]); + +} + +void Citire() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y,w; + + fin>>x>>y>>w; + + S.insert({w,{x,y}}); + + } + +} + +void Kruskal() + +{ + + for(int i=1;i<=n;i++) + + T[i]=i; + + for(auto p : S) + + { + + int w=p.first; + + auto p2=p.second; + + int x=p2.first; + + int y=p2.second; + + int rx=Radacina(x); + + int ry=Radacina(y); + + if(rx!=ry) + + { + + T[ry]=rx; + + t=t+w; + + G.push_back({x,y}); + + } + + } + +} + +void Afisare() + +{ + + fout< +#include +#include +using namespace std; +ifstream cin("apm.in"); +ofstream cout("apm.out"); +struct chestie +{ + int a,b ,cost; +}v[2000001]; +vector > sol; +int costmin; +bool cond(chestie x, chestie y) +{ + return x.cost > n >> m; + for(int i =1 ;i <=m;i ++) + cin >> v[i].a >> v[i].b>>v[i] .cost; + sort(v + 1, v + m + 1, cond); + int contor = 0; + for( int i =1 ;i <=m; i ++) + { + int x = FIND(v[i].a); + int y = FIND(v[i].b); + if(x != y) + { + Union(x, y); + costmin += v[i].cost; + sol.push_back({v[i].a,v[i].b}); + if(sol.size() == n -1) + break; + } + } + cout << costmin<<'\n'< height[y]) + { + tata[y] =x; + } else + if(height[x] + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int vf, muchii, ct; + +long long Suma; + +int Tata[200005], Dimensiune[200005]; + +pair < int, int > Perechi[400005]; + +struct elemente + +{ + + int x, y, cost; + + + +} V[400005]; + +inline bool cmp( elemente a, elemente b ) + +{ + + return a.cost < b.cost; + +} + +inline int Find( int Nod ) + +{ + + if( Nod != Tata[Nod] ) + + Tata[Nod] = Find(Tata[Nod]); + + return Tata[Nod]; + +} + +void Unire( int x, int y ) + +{ + + x = Find(x); + + y = Find(y); + + + + if( Dimensiune[x] < Dimensiune[y] ) + + { + + Dimensiune[y] += Dimensiune[x]; + + Tata[x] = y; + + } + + else + + { + + Dimensiune[x] += Dimensiune[y]; + + Tata[y] = x; + + } + +} + +void Kruskal() + +{ + + for( int i = 1; i <= muchii; ++ i ) + + { + + int tata_x = Find(V[i].x); + + int tata_y = Find(V[i].y); + + + + if( tata_x != tata_y ) + + { + + Unire( tata_x, tata_y ); + + + + Suma += V[i].cost; + + Perechi[++ ct].first = V[i].x; + + Perechi[ct].second = V[i].y; + + } + + } + +} + +int main() + +{ + + fin >> vf >> muchii; + + for( int i = 1; i <= muchii; ++ i ) + + fin >> V[i].x >> V[i].y >> V[i].cost; + + + + sort( V + 1, V + muchii + 1, cmp ); + + + + for(int i = 1; i <= vf; ++ i) + + Tata[i] = i, Dimensiune[i] = 1; + + + + Kruskal(); + + + + fout << Suma << "\n" << ct << "\n"; + + for(int i = 1; i <= ct; ++ i) + + fout << Perechi[i].second << " " << Perechi[i].first << "\n"; + + return 0; + +} diff --git a/mst/kruskal/1b2b1558-7558-49a2-94ab-30de8d127330.cpp b/mst/kruskal/1b2b1558-7558-49a2-94ab-30de8d127330.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2806c537ee3011aa00974d133d508be0c013b3ca --- /dev/null +++ b/mst/kruskal/1b2b1558-7558-49a2-94ab-30de8d127330.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +struct muchie + +{ + + int x,y,cost; + +} a[400005]; + +struct muchie2 + +{ + + int x,y; + +} sol[400005]; + +int n,m,i,r1,r2,muchii,costmin,t[200005]; + + + +bool cmp(muchie a, muchie b) + +{ + + return a.cost>n>>m; + + for (i=1; i<=m; i++) + + fin>>a[i].x>>a[i].y>>a[i].cost; + + sort(a+1,a+m+1,cmp); + + for (i=1; i<=n; i++) + + t[i]=i; + + for (i=1; i<=m&&muchii +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m,a[400004],s,m1; + +struct graf + +{ + + int x,y,c; + +}; + +bool cmp(graf x, graf y) + +{ + + if(x.c!=y.c) + + return x.c>n>>m; + + for(int i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int x=v[i].x; + + int y=v[i].y; + + int rx=arad(x); + + int ry=arad(y); + + if(rx==ry) + + continue; + + s+=v[i].c; + + if(rx +#include +#include +#include + +using namespace std; + +ifstream f("apm.in"); +ofstream g("apm.out"); + +struct edge { int x,y,cost; }; + +int n,m; +vectoredges; +vectorsol; + +class DisjointSet { + +private: + + vector parent; + + + void compress(int node,const int& root) { + while(node != root) { + int aux = parent[node]; + parent[node] = root; + node = aux; + } + } + +public: + + DisjointSet(const int& size) { + parent.resize(size + 1,-1); + } + + + int _find( const int& node ) { + + int root = node; + + while(parent[root] > 0) + root = parent[root]; + + compress(node,root); + + return root; + } + + + void _union(int X,int Y) { + if(parent[X] >= parent[Y]) { + parent[X] += parent[Y]; + parent[Y] = X; + }else { + parent[Y] += parent[X]; + parent[X] = Y; + } + } +}; + +void kruskal() { + + DisjointSet disjoint(n); + + + sort(edges.begin(),edges.end(),[](const edge& a,const edge& b) { + return a.cost < b.cost; + }); + + int cnt{}, cost{}; + + for(int i = 0;i < m;++i) { + + int rootX = disjoint._find(edges[i].x); + int rootY = disjoint._find(edges[i].y); + + + if(rootX != rootY) { + cnt++; + disjoint._union(rootX,rootY); + sol.push_back(i); + cost += edges[i].cost; + } + + if(cnt == n - 1) + break; + } + + g << cost << '\n' << n - 1 << '\n'; + + for(int it : sol) + g << edges[it].x << ' ' << edges[it].y << '\n'; +} + +int main() { + + f >> n >> m; + + edges.resize(m); + + for(int i = 0;i < m;++i) + f >> edges[i].x >> edges[i].y >> edges[i].cost; + + kruskal(); + + return 0; +} diff --git a/mst/kruskal/1d714b75-de8b-43ee-b8e8-421448b77110.cpp b/mst/kruskal/1d714b75-de8b-43ee-b8e8-421448b77110.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c34f509af34c01fee0bd407adac8f91d748216b9 --- /dev/null +++ b/mst/kruskal/1d714b75-de8b-43ee-b8e8-421448b77110.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int maxn = 400100; + + + +int GR[maxn], X[maxn], Y[maxn], C[maxn]; + +int N, M, ANS, IND[maxn]; + +vector> add; + +int grupa(int i) { + + if (GR[i] == i) return i; + + GR[i] = grupa(GR[i]); + + return GR[i]; + +} + + + +void reuniune(int i, int j) { + + GR[grupa(i)] = grupa(j); + +} + + + +bool cmpf(int i, int j) { + + return(C[i] < C[j]); + +} + + + +int main() { + + fin >> N >> M; + + for (int i = 1; i <= M; ++i) { + + fin >> X[i] >> Y[i] >> C[i]; + + IND[i] = i; + + } + + for (int i = 1; i <= N; ++i) GR[i] = i; + + sort(IND + 1, IND + M + 1, cmpf); + + + + for (int i = 1; i <= M; ++i) { + + + + if (grupa(X[IND[i]]) != grupa(Y[IND[i]])) { + + ANS += C[IND[i]]; + + add.push_back({ X[IND[i]], Y[IND[i]] }); + + reuniune(X[IND[i]], Y[IND[i]]); + + } + + } + + fout << ANS << "\n"; + + fout << add.size() << "\n"; + + for (int i = 0; i < add.size(); ++i) + + fout << add[i].first << " " << add[i].second << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/1e1d651f-5e62-4f65-a738-25a10ed070ea.cpp b/mst/kruskal/1e1d651f-5e62-4f65-a738-25a10ed070ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bd16fb54904934000997693907d6d38955a5728 --- /dev/null +++ b/mst/kruskal/1e1d651f-5e62-4f65-a738-25a10ed070ea.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +struct muchie{ + +int x,y,cost;}; + +struct soll{ + +int x,y;}; + +vectorsol; + +int h[200100],tata[200100]; + +void Union(int x,int y); + +int Find(int x); + +void kruskal(); + +int costmin,nrsel,n,m; + +inline bool operator<(muchie a,muchie b) + +{ + + return a.cost>b.cost; + +} + +priority_queueheap; + +int main() + +{ + + int i,x,y,z; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>z; + + heap.push({x,y,z}); + + } + + kruskal(); + + fout<h[y]) + + tata[y]=x; + + else{ + + tata[x]=y; + + if(h[x]==h[y]) + + h[y]++; + + } + +} + +void kruskal() + +{ + + muchie a; + + int nrsel=0,rx,ry; + + while(nrsel +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); +struct edge{ + + int x; + + int y; + + int cost; + +}; + +bool comparator(const edge& lhs, const edge& rhs) + +{ + + return lhs.cost < rhs.cost; + +} + + + +vector graph; + +vector> solution; + + + +int n, m, total = 0; + +int t[200005], rang[200005]; + + + +void read() + +{ + + in >> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int x, y, c; + + in >> x >> y >> c; + + graph.push_back({ x,y,c }); + + + + + + t[i] = i; + + rang[i] = 1; + + } + + + + sort(graph.begin(), graph.end(), &comparator); + + + +} + + + +int find(int nod) + +{ + + while (t[nod] != nod) + + nod = t[nod]; + + return nod; + +} + + + +void Union(int x, int y) + +{ + + if (rang[x] < rang[y]) + + t[x] = y; + + + + if (rang[x] > rang[y]) + + t[y] = x; + + + + if (rang[x] == rang[y]) + + { + + t[x] = y; + + rang[y]++; + + } + + + +} + + + +void solve() + +{ + + for (const auto& e : graph) + + { + + int tatal_x = find(e.x); + + int tatal_y = find(e.y); + + + + if (tatal_x != tatal_y) + + { + + Union(tatal_x,tatal_y); + + solution.push_back({ e.x,e.y }); + + total += e.cost; + + } + + } + +} + + + +void print() + +{ + + + + out << total << '\n'; + + out << solution.size()<<'\n'; + + + + for (const auto& p : solution) + + { + + out << p.first << ' ' << p.second << '\n'; + + } + +} + +int main() + +{ + + read(); + + solve(); + + print(); + +} diff --git a/mst/kruskal/1e749e2a-c7e0-48c4-8963-1020fd1cbbe9.cpp b/mst/kruskal/1e749e2a-c7e0-48c4-8963-1020fd1cbbe9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ae76dae29c343688ce7f0dc14166462e58d2a81 --- /dev/null +++ b/mst/kruskal/1e749e2a-c7e0-48c4-8963-1020fd1cbbe9.cpp @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include + +constexpr int g_maxn = 200'001; + +class edge { + private: + int m_a = 0; + int m_b = 0; + int m_cost = 0; + + using ref = edge &; + + public: + edge() noexcept = default; + edge(edge const &) noexcept = default; + edge(edge &&) noexcept = default; + ~edge() noexcept = default; + + edge(int const a, int const b, int const cost) + : m_a{a}, m_b{b}, m_cost{cost} {} + + auto operator=(edge const &) noexcept -> ref = default; + auto operator=(edge &&) noexcept -> ref = default; + + auto first() const noexcept -> int { return m_a; } + auto second() const noexcept -> int { return m_b; } + auto cost() const noexcept -> int { return m_cost; } +}; + +std::vector g_edges; +std::vector g_answer; +std::array g_groups; + +auto get_group(int const i) noexcept -> int { + if (g_groups[i] == i) { + return i; + } + + g_groups[i] = get_group(g_groups[i]); + + return g_groups[i]; +} + +auto unite(int const i, int const j) noexcept -> void { + g_groups[get_group(i)] = get_group(j); +} + +auto main(int, char *[]) noexcept -> int { + std::ifstream f{"apm.in"}; + std::ofstream g{"apm.out"}; + + int n = 0; + int m = 0; + + f >> n >> m; + g_edges.reserve(m); + g_answer.reserve(m); + + for (int i = 0; i < m; ++i) { + int a = 0; + int b = 0; + int cost = 0; + + f >> a >> b >> cost; + g_edges.emplace_back(a, b, cost); + g_groups[i] = i; + } + + std::sort(g_edges.begin(), g_edges.end(), + [](edge const &a, edge const &b) { return a.cost() < b.cost(); }); + + std::int64_t total_cost = 0; + for (auto const &e : g_edges) { + if (get_group(e.first()) != get_group(e.second())) { + total_cost += e.cost(); + unite(e.first(), e.second()); + g_answer.push_back(e); + } + } + + g << total_cost << std::endl; + g << g_answer.size() << std::endl; + + for (auto const &e : g_answer) { + g << e.first() << ' ' << e.second() << std::endl; + } +} diff --git a/mst/kruskal/20a38ec3-c2a7-4454-b722-27aa7d495e4c.cpp b/mst/kruskal/20a38ec3-c2a7-4454-b722-27aa7d495e4c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7da27f3a485f1a50041e45e549723786a18c3793 --- /dev/null +++ b/mst/kruskal/20a38ec3-c2a7-4454-b722-27aa7d495e4c.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; + + + +vector t; + +int _find (int x) { + + if (t[x]==x) + + return x; + + return t[x]=_find(t[x]); + +} + + + +void _union (int x, int y) { + + int p1=t[x], + + p2=t[y]; + + t[p2]=p1; + +} + + + +int main () { + + ifstream fin ("apm.in"); + + ofstream fout ("apm.out"); + + int n, m, i, j, c, k; + + fin >> n >> m; + + vector > muchii; + + for (k=0; k> i >> j >> c; + + muchii.push_back(make_tuple(i, j, c)); + + } + + fin.close(); + + + + sort (muchii.begin(), muchii.end(), [] (const tuple &a, const tuple &b) -> bool { + + return (get<2>(a))<(get<2>(b)); + + }); + + t.assign(n+1, 0); + + iota(t.begin(), t.end(), 0); + + + + fout << " \n" + + << n-1 << '\n'; + + int s=0, ct=0; + + auto it=muchii.begin(); + + while (it!=muchii.end() && ct(*it)) != _find(get<1>(*it))) { + + _union(get<0>(*it), get<1>(*it)); + + s+=get<2>(*it); + + ct++; + + fout << get<0>(*it) << ' ' << get<1>(*it) << '\n'; + + } + + ++it; + + } + + + + fout.seekp(0); + + fout << s; + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/20f1899c-f649-4dbb-af37-910da05e800f.cpp b/mst/kruskal/20f1899c-f649-4dbb-af37-910da05e800f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d19f62e7f293d287f2480a2676c187746e8ecf38 --- /dev/null +++ b/mst/kruskal/20f1899c-f649-4dbb-af37-910da05e800f.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; + +struct graf{ + + int x,y,cost; + +}; + + + +vectorgr(200003,0); + +vectorv(200003); + +vectorind(200003); + +bool cmp(int i,int j){ + + return (v[i].costsol; + + for(int i=1;i<=m;i++){ + + if(grupa(v[ind[i]].x)!=grupa(v[ind[i]].y)){ + + s=s+v[ind[i]].cost; + + reuniune(v[ind[i]].x,v[ind[i]].y); + + sol.push_back(ind[i]); + + } + + + + } + + + + printf("%d\n%d\n",s,n-1); + + for(auto i:sol) + + printf("%d %d\n",v[i].x,v[i].y); + + + + + + return 0; + +} diff --git a/mst/kruskal/2175711c-2b56-49f1-a435-282353d10d6f.cpp b/mst/kruskal/2175711c-2b56-49f1-a435-282353d10d6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f056b94a4a2082cf8e575d51d7e75a5a494c328 --- /dev/null +++ b/mst/kruskal/2175711c-2b56-49f1-a435-282353d10d6f.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct muchie + +{ + + int cost,a,b; + +}; + +bool cmp(const muchie &x,const muchie &y) + +{ + + return x.cost ans[400005]; + +int main() + +{ + + int n,m; + + cin>>n>>m; + + for(int i=1; i<=n; ++i) + + { + + t[i]=i; + + h[i]=1; + + } + + for(int i=1; i<=m; ++i) + + { + + int xx; + + cin>>xx; + + d[i].a=xx; + + cin>>xx; + + d[i].b=xx; + + cin>>xx; + + d[i].cost=xx; + + } + + sort(d+1,d+m+1,cmp); + + int costt=0,nrm=0; + + for(int i=1; i<=m; ++i) + + { + + int r1=findr(d[i].a),r2=findr(d[i].b); + + if(r1!=r2) + + unite(r1,r2),costt+=d[i].cost,ans[++nrm].first=d[i].a,ans[nrm].second=d[i].b; + + } + + cout< +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< pair >apm; + + + +int n, m, t[200001], s; + + + +struct muchii + +{ + + int x, y, cost; + +}; + + + +muchii lm[400001]; + + + +bool cmp(muchii a, muchii b) + +{ + + return a.cost> n >> m; + + + + for(int i = 0; i < m; i++) + + f >> lm[i].x >> lm[i].y >> lm[i].cost; + + + + sort(lm, lm + m, cmp); + + + + for(int i = 1; i <= n; i++) + + t[i] = i; + + + + for(int i = 0; i < m; i++) + + if(rep(lm[i].x) != rep(lm[i].y)) + + { + + t[rep(lm[i].y)] = lm[i].x; + + s += lm[i].cost; + + apm.push_back ( make_pair(lm[i].x,lm[i].y)); + + } + + + + g< + + +using namespace std; + + + +const int NMAX=2e5+5,MMAX=4e5+5; + +int n,m; + +vector> tree; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct nod + +{ + + int parent; + + int depth; + + int cost; + +} graph[NMAX]; + + + +struct edge + +{ + + int x,y,cost; + + bool operator<(edge a) + + { + + if(a.cost==cost) + + return x>n>>m; + + for(int i=0; i>edges[i].x>>edges[i].y>>edges[i].cost; + + sort(edges,edges+m); + +} + + + +void reunion(int r1,int r2,int i) + +{ + + if(graph[r1].depth==graph[r2].depth) + + { + + graph[r1].cost+=graph[r2].cost+edges[i].cost; + + graph[r2].parent=r1; + + graph[r1].depth++; + + } + + else if(graph[r1].depth>graph[r2].depth) + + { + + graph[r1].cost+=graph[r2].cost+edges[i].cost; + + graph[r2].parent=r1; + + } + + else + + { + + graph[r2].cost+=graph[r1].cost+edges[i].cost; + + graph[r1].parent=r2; + + } + +} + + + +void Kruskal() + +{ + + for(int i=0; i +#include +#include + + +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +pair < int , pair < int , int > > pr[200005 * 2]; + +vector < pair < int , int > > Answer; + +int ds[200005], N, M, minCost; + + + +int root(int x) + +{ + + while(ds[x] != x){ + + ds[x] = ds[ds[x]]; + + x = ds[x]; + + } + + return x; + +} + + + +void UnionFind(int x, int y) + +{ + + int px = root(x); + + int py = root(y); + + ds[px] = ds[py]; + +} + + + +int Kruskal() + +{ + + int x, y, cost; + + for(int i = 1; i <= M; i++){ + + x = pr[i].second.first; + + y = pr[i].second.second; + + cost = pr[i].first; + + if(root(x) != root(y)){ + + UnionFind(x,y); + + Answer.push_back(make_pair(x,y)); + + minCost += cost; + + } + + } + + return minCost; + +} + + + +int main() + +{ + + + + int x, y, c; + + cin >> N >> M; + + for(int i = 1; i <= M; i++){ + + cin >> x >> y >> c; + + pr[i] = make_pair(c,make_pair(x,y)); + + } + + sort(pr + 1, pr + M + 1); + + for(int i = 1; i <= N; i++){ + + ds[i] = i; + + } + + Kruskal(); + + cout << minCost << "\n" << Answer.size() << "\n"; + + for(int i = 0; i < Answer.size(); i++){ + + cout << Answer[i].first << " " << Answer[i].second << "\n"; + + } + + return 0; + +} diff --git a/mst/kruskal/22a7bb56-8e4f-48a4-bac8-9e1c55beb78c.cpp b/mst/kruskal/22a7bb56-8e4f-48a4-bac8-9e1c55beb78c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..819ab04ba851045ccca82ef394da521c026abc14 --- /dev/null +++ b/mst/kruskal/22a7bb56-8e4f-48a4-bac8-9e1c55beb78c.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + + +ifstream in("apm.in"); +ofstream out("apm.out"); + +int num_nodes,num_edges; + + +struct edge{ + int a,b,cost; +} edges[400000]; + +bool cmp(edge&a,edge&b){ + return a.cost>num_nodes>>num_edges; + for(int i=0;i>edges[i].a>>edges[i].b>>edges[i].cost; + sort(edges,edges+num_edges,cmp); +} + +void unite(int a,int b){ + if(depth[a]==depth[b]){ + father[b]=a; + depth[a]++; + } + else if(depth[a]> results; + for(int i=0;i + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n,m,a,b,c,conexe,suma,cnt,rasp[500005]; + +int tata[500005],conex[500005]; + + + +int grupa(int i) + +{ + + if(conex[i]==i)return i; + + conex[i]=grupa(conex[i]); + + return conex[i]; + +} + +void reuniune(int i,int j) + +{ + + conex[grupa(i)]=grupa(j); + +} + + + +struct triplet + +{ + + int x,y,cost; + +}; + + + +triplet muchie[500005]; + +bool cmp(triplet i,triplet j) + +{ + + return i.cost>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>muchie[i].x>>muchie[i].y>>muchie[i].cost; + + tata[i]=i; + + conex[i]=i; + + } + + sort(muchie+1,muchie+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int a=muchie[tata[i]].x; + + int b=muchie[tata[i]].y; + + if(grupa(a)!=grupa(b)) + + { + + suma+=muchie[i].cost; + + cnt++; + + rasp[cnt]=i; + + reuniune(a,b); + + } + + } + + fout< + + +using namespace std; + + + +struct edge + +{ + + int e1, e2, cost; + +}; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +int n, m, x, y, cost, total; + +int TT[200005]; + +int RG[200005]; + +vector edges; + +vector> rez; + + + +int find(int x) + +{ + + int R, y; + + + + for(R = x;R != TT[R]; R = TT[R]); + + + + while(x != TT[x]) + + { + + y = TT[x]; + + TT[x] = R; + + x = y; + + } + + + + return R; + +} + + + +void unite(int x, int y) + +{ + + if(RG[x] > RG[y]) + + TT[y] = x; + + else + + TT[x] = y; + + + + if(RG[x] == RG[y]) ++RG[y]; + + + +} + + + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= m;++i) + + { + + f>>x>>y>>cost; + + edges.push_back({x, y, cost}); + + } + +} + + + +bool cmp(edge m1, edge m2) + +{ + + return m1.cost < m2.cost; + +} + + + +void Solve() + +{ + + for(int i = 1;i <= n;++i) + + { + + TT[i] = i; + + RG[i] = 1; + + } + + sort(edges.begin(), edges.end(), cmp); + + for(vector::iterator it = edges.begin();it != edges.end();++it) + + if(find((*it).e1) != find((*it).e2)) + + { + + total += (*it).cost; + + unite(find((*it).e1), find((*it).e2)); + + rez.push_back(make_pair((*it).e1, (*it).e2)); + + } + +} + + + +void Print() + +{ + + g<>::iterator it = rez.begin();it != rez.end();++it) + + g<<(*it).first<<" "<<(*it).second<<'\n'; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/mst/kruskal/23c25776-7efc-476f-ba63-3b6f9476a209.cpp b/mst/kruskal/23c25776-7efc-476f-ba63-3b6f9476a209.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f7fa3078c3190bfdabfb98527d2e300eaabfe0c --- /dev/null +++ b/mst/kruskal/23c25776-7efc-476f-ba63-3b6f9476a209.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +vector > v; + +int n, m; + +struct nod + +{ + + int p; + + int d; + +} graf[200005]; + +struct muchie + +{ + + int x,y; + + int cost; + +} nodes[400005]; + +bool cmp(muchie a, muchie b) + +{ + + if(a.cost < b.cost) + + return true; + + else if( a. cost == b. cost) + + if(a.x < b.x) + + return true; + + if(a.x == b.x) + + if(a.y < b.y) + + return true; + + return false; + +} + +int getRoot(int node) + +{ + + if(graf[node].p == 0) + + return node; + + int p = getRoot(graf[node].p); + + graf[node].d = graf[p].d; + + graf[node].p = p; + + return p; + +} + +void citire() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + fin >> nodes[i].x >> nodes[i].y >> nodes[i].cost; + + sort(nodes, nodes + m, cmp); + +} + +void solve() + +{ + + int c = 0; + + int k = 0; + + for(int i = 0; i < m; i++) + + { + + int r1 = getRoot(nodes[i].x); + + int r2 = getRoot(nodes[i].y); + + if(r1 != r2) + + { + + if(graf[r1].d == graf[r2].d) + + { + + graf[r2].p = r1; + + graf[r1].d++; + + } + + else if(graf[r1].d > graf[r2].d) + + { + + graf[r2].p = r1; + + } + + else if(graf[r1].d < graf[r2].d) + + { + + graf[r1].p = r2; + + } + + c =c + nodes[i].cost; + + v.push_back({nodes[i].x, nodes[i].y}); + + } + + } + + fout << c << "\n"; + + fout << n - 1 << "\n"; + + for(auto &n : v) + + fout << n.first <<" "<< n.second <<"\n"; + +} + +int main() + +{ + + citire(); + + solve(); + + return 0; + +} diff --git a/mst/kruskal/23f290d3-07c2-48c7-b6d7-de3be7d8fea5.cpp b/mst/kruskal/23f290d3-07c2-48c7-b6d7-de3be7d8fea5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e675fc49a70090ad7cb96949d2039800fa54fe95 --- /dev/null +++ b/mst/kruskal/23f290d3-07c2-48c7-b6d7-de3be7d8fea5.cpp @@ -0,0 +1,159 @@ +#include +using namespace std; + + + +ifstream ci("apm.in"); + +ofstream cou("apm.out"); + + + +struct date{ + + int x,y,cost; + +}; + + + +int n,m; + +int t[200005]; + +int cn; + +int k; + +date sol[400005]; + +date v[400005]; + + + +int Find(int x){ + + int r=x,p,r1; + + while(t[r]){ + + r=t[r]; + + } + + p=r; + + r=x; + + while(t[r]){ + + r1=t[r]; + + t[r]=p; + + r=r1; + + } + + return p; + +} + +void Union(int x,int y){ + + t[y]=x; + +} + + + +bool cmp(date a,date b){ + + return a.cost>n>>m; + + for(i=1;i<=m;i++){ + + ci>>v[i].x>>v[i].y>>v[i].cost; + + } + + sort(v+1,v+m+1,cmp); + +} + + + +void rez(){ + + int i,x,y,s=0; + + for(i=1;i<=m;i++){ + + x=v[i].x; + + y=v[i].y; + + + + if(Find(x)!=Find(y)){ + + if(Find(x)!=x){ + + cn++; + + } + + if(Find(y)!=y){ + + cn++; + + } + + s+=v[i].cost; + + Union(Find(x),Find(y)); + + sol[++k].x=x; + + sol[k].y=y; + + + + } + } + + cou< +#include +#include +#include + + +using namespace std; + + + +ifstream fi("apm.in"); + +ofstream fo("apm.out"); + + + +struct muchie + +{ + + int v1, v2, c; + +}; + + + +int n,m; + +muchie e[400000]; + +int p[200001]; + +int c[200001]; + + + +int cmp(muchie a, muchie b) + +{ + + return a.c > s; + + + +int main() + +{ + + fi>>n>>m; + + + + for(int i=1;i<=n;i++) + + { + + p[i]=i; + + c[i]=1; + + } + + + + for(int i=0;i>e[i].v1>>e[i].v2>>e[i].c; + + } + + + + sort(e,e+m,cmp); + + + + int nrm=1; + + int rez=e[0].c; + + + + p[e[0].v2]=e[0].v1; + + c[e[0].v2]=2; + + + + s.push({e[0].v1,e[0].v2}); + + + + int p1,p2; + + for(int i=1;ic[p2]) + + { + + c[p1]+=c[p2]; + + p[p2]=p1; + + } + + else + + { + + c[p2]+=c[p1]; + + p[p1]=p2; + + } + + } + + } + + else + + break; + + } + + + + fo< +#include +#include +#include + + +using namespace std; + + + +int N, M, t[200001], rang[2000001], cost_total; + +vector > sol; + + + +struct muchie{int x, y, cost; }L[400005]; + + + +bool cmp(muchie a, muchie b) + +{ + + return a.cost < b.cost; + +} + + + +int cauta(int nod) + +{ + + while (t[nod] != nod) + + { + + nod = t[nod]; + + } + + return nod; + +} + + + +void unire(int a, int b) + +{ + + if (rang[a] < rang[b]) + + t[a] = b; + + if (rang[a] > rang[b]) + + t[b] = a; + + if (rang[a] == rang[b]) + + { + + t[a] = b; + + rang[b]++; + + } + +} + + + +int main() + +{ + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> N >> M; + + + + for(int i = 1; i <= M; i++) + + fin >> L[i].x >> L[i].y >> L[i].cost; + + + + for (int i = 1; i <= N; i++) + + { + + t[i] = i; + + rang[i] = 1; + + } + + + + sort(L+1, L+M+1, cmp); + + + + for (int i = 1; i <= M; i++) + + { + + int tx = cauta(L[i].x); + + int ty = cauta(L[i].y); + + + + if (tx != ty) + + { + + unire(tx, ty); + + sol.push_back({L[i].x, L[i].y}); + + cost_total = cost_total + L[i].cost; + + } + + } + + + + fout << cost_total << "\n"; + + fout << sol.size() << "\n"; + + for (int i = 0; i < sol.size(); i++) + + fout << sol[i].first << " " << sol[i].second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/27215de1-89a7-408d-9773-33a175eac9cd.cpp b/mst/kruskal/27215de1-89a7-408d-9773-33a175eac9cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..878433369952c2a746aa344217ea35c40021c975 --- /dev/null +++ b/mst/kruskal/27215de1-89a7-408d-9773-33a175eac9cd.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +fstream f("apm.in"); + +ofstream o("apm.out"); + +int n, m; + + + +class node + +{ + +public: + + int source, dest, cost; + + + + node() + + { + + + + } + + + +}; + + + +node g[200005]; + +int parinte[200005]; + +node out[200005]; + +int sz; + + + +int Find(int x) + +{ + + int y = x, t; + + while (parinte[y] != y) + + { + + y = parinte[y]; + + } + + + + while (x != y) + + { + + t = parinte[x]; + + parinte[x] = y; + + x = t; + + } + + return x; + + + +} + + + + + +void Union(int x, int y) + +{ + + x = Find(x); + + y = Find(y); + + + + if (x != y) + + { + + parinte[y] = x; + + } + +} + + + +int apm_kruskal() + +{ + + int cost = 0; + + + + for (size_t i = 0; i < m; i++) + + { + + if (Find(g[i].source) != Find(g[i].dest)) + + { + + Union(g[i].source, g[i].dest); + + cost += g[i].cost; + + out[sz++] = g[i]; + + } + + } + + return cost; + +} + + + +int main() + +{ + + f >> n >> m; + + for (size_t i = 0; i < m; i++) + + { + + int x, y, cost; + + f >> x >> y >> cost; + + g[i].source = x; + + g[i].dest = y; + + g[i].cost = cost; + + } + + + + for (size_t i = 1; i <= n; i++) + + { + + parinte[i] = i; + + } + + + + sort(g, g + m, [](node cost1, node cost2) + + { + + return cost1.cost < cost2.cost; + + }); + + + + o << apm_kruskal() << "\n"; + + o << sz << "\n"; + + for (size_t i = 0; i < sz; i++) + + { + + o << out[i].dest << " " << out[i].source << "\n"; + + } + + + +} diff --git a/mst/kruskal/2755c21d-f8f0-4bdc-b807-c2d6bcdf669e.cpp b/mst/kruskal/2755c21d-f8f0-4bdc-b807-c2d6bcdf669e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20dad7984f8d5617a23c0956d652aa57ef6c34d8 --- /dev/null +++ b/mst/kruskal/2755c21d-f8f0-4bdc-b807-c2d6bcdf669e.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +int t[200010], S[200010]; + +int a, b, ra, rb, n, m, i, sol, alese; + + + +int rad(int x) { + + while(t[x] > 0) + + x = t[x]; + + return x; + +} + + + +pair< int, pair > v[400010]; + + + +int main (){ + + fin>>n>>m; + + for (i=1;i<=m;i++) + + fin>>v[i].second.first>>v[i].second.second>>v[i].first; + + sort(v+1, v+m+1); + + + + for (i=1;i<=n;i++) + + t[i] = -1; + + + + for (i=1;i<=m;i++) { + + a = v[i].second.first; + + b = v[i].second.second; + + + + ra = rad(a); + + rb = rad(b); + if (ra != rb) { + + if (-t[ra] > -t[rb]) { + + + + t[ra] += t[rb]; + + t[rb] = ra; + + } else { + + t[rb] += t[ra]; + + t[ra] = rb; + + } + + sol += v[i].first; + + + + + + alese++; + + S[alese] = i; + + if (alese == n-1) + + break; + + } + + } + + fout< +#include +#include +using namespace std; + +using pii = pair; + +int N, M; + +vector parent, tree_size; + +vector> adj; + + + + + +void make_set(const int &vertex) + +{ + + parent[vertex] = vertex; + + tree_size[vertex] = 1; + +} + + + + + +int find_leader(const int &vertex) + +{ + + if (parent[vertex] == vertex) + + { + + return vertex; + + } + + + + return parent[vertex] = find_leader(parent[vertex]); + +} + + + + + +void unite_sets(int &set1, int &set2) + +{ + + set1 = find_leader(set1); + + set2 = find_leader(set2); + + + + if (set1 != set2) + + { + + if (tree_size[set1] < tree_size[set2]) + + { + + swap(set1, set2); + + } + + + + tree_size[set1] += tree_size[set2]; + + parent[set2] = set1; + + } + +} + + + + + +void Kruskal() + +{ + + vector MST_edges; + + int total_weight = 0; + + + + for (int node = 1; node <= N; ++node) + + { + + make_set(node); + + } + + + + sort(begin(adj), end(adj)); + + + + for (const auto &edge : adj) + + { + + int CC1 = edge.second.first, CC2 = edge.second.second; + + int leader1 = find_leader(CC1), leader2 = find_leader(CC2); + + + + if (leader1 != leader2) + + { + + total_weight += edge.first; + + MST_edges.push_back({CC1, CC2}); + + unite_sets(leader1, leader2); + + } + + } + + + + ofstream fout("apm.out"); + + + + fout << total_weight << "\n" << N - 1 << "\n"; + + for (const pii &MST_edge : MST_edges) + + { + + fout << MST_edge.first << " " << MST_edge.second << "\n"; + + } + +} + + + + + +int main() + +{ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + parent.resize(N + 1); + + tree_size.resize(N + 1); + + + + while (M--) + + { + + int i, j, weight; + + fin >> i >> j >> weight; + + + + adj.push_back({weight, {i, j}}); + + adj.push_back({weight, {j, i}}); + + } + + + + Kruskal(); + +} diff --git a/mst/kruskal/2a3777eb-c741-424d-b1b8-9f6ef248bd4e.cpp b/mst/kruskal/2a3777eb-c741-424d-b1b8-9f6ef248bd4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e054af7c5a1037c88fc5963dce1e8860147a2ca4 --- /dev/null +++ b/mst/kruskal/2a3777eb-c741-424d-b1b8-9f6ef248bd4e.cpp @@ -0,0 +1,198 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +struct muchie + +{ + + int a, b, c; + +}v[400001]; + + + +int comparare (muchie x, muchie y) + +{ + + return x.c>n>>m; + + + + for(i=1; i<=m; i++) + + { + + fin>>v[i].a>>v[i].b>>v[i].c; + + } + + + + sort(v+1, v+m+1, comparare); + + + + for(i=1; i<=n; i++) + + { + + tt[i]=i; + + rg[i]=1; + + } + + + + ct=0; + + k=0; + + i=1; + + while(k +#include + + + +using namespace std; +ifstream in ( "apm.in" ); +ofstream out( "apm.out" ); +struct potae{ + int x, y, cost; +} v[200200], sol[200200]; +int n, m, i, t[200200], much, rx, ry, total; + +bool cmp ( potae a, potae b ){ + return a.cost < b.cost; +} + +int rad ( int a ){ + while ( t[a] > 0 ) + a = t[a]; + return a; +} + +int main() { + in>>n>>m; + for ( i=1; i <= m; i++ ) + in>>v[i].x>>v[i].y>>v[i].cost; + + sort( v+1, v+m+1, cmp ); + + for ( i=1; i <= n; i++ ) + t[i] = -1; + for ( i=1; i <= m && much < n; i++ ){ + rx = rad( v[i].x ); + ry = rad( v[i].y ); + if ( rx == ry ) + continue; + if ( t[rx] < t[ry] ){ + t[rx] += t[ry]; + t[ry] = rx; + } else { + t[ry] += t[rx]; + t[rx] = ry; + } + sol[++much] = { v[i].x, v[i].y }; + total += v[i].cost; + } + out< +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int MAX = 400005; + + + +struct muchie { + + int x, y, c; + +} muchii[MAX]; + + + +int n, m, total, TT[MAX], RG[MAX], k; + + + +bool compare(muchie a, muchie b) { + + return a.c < b.c; + +} + + + +pair P[MAX]; + + + +void citire() { + + in >> n >> m; + + int x, y, c; + + for(int i = 1; i <= m; i++) { + + in >> x >> y >> c; + + muchii[i].x = x; + + muchii[i].y = y; + + muchii[i].c = c; + + } + + sort(muchii + 1, muchii + m + 1, compare); + +} + + + +int Find(int nod) { + + while(TT[nod] != nod) + + nod = TT[nod]; + + return nod; + +} + + + +void Unite(int x, int y) { + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[y] < RG[x]) + + TT[y] = x; + + if(RG[x] == RG[y]) { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() { + + for(int i = 1; i <= m; i++) { + + int tata_x = Find(muchii[i].x); + + int tata_y = Find(muchii[i].y); + + if(tata_x != tata_y) { + + Unite(tata_x, tata_y); + + P[++k].first = muchii[i].x; + + P[k].second = muchii[i].y; + + total += muchii[i].c; + + } + + } + +} + + + +int main() { + + citire(); + + + + for(int i = 1; i <= m; i++) { + + TT[i] = i; + + RG[i] = 1; + + } + + + + Solve(); + + + + out << total << "\n"; + + out << n - 1 << "\n"; + + + + for(int i = 1; i <= k; i++) { + + out << P[i].first << " " << P[i].second << " " << "\n"; + + } + + return 0; + +} diff --git a/mst/kruskal/2dced3cf-e98e-49de-8d30-21dfba2924ed.cpp b/mst/kruskal/2dced3cf-e98e-49de-8d30-21dfba2924ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c56e9bdf037a61cef5a2c5038bf3612f279b6d8 --- /dev/null +++ b/mst/kruskal/2dced3cf-e98e-49de-8d30-21dfba2924ed.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int c, x, y; + +}; + + + +typedef pair pereche; + + + +int tata[200005], rang[200005]; + + + +bool comp(muchie i, muchie j) + +{ + + return i.c < j.c; + +} + + + +int Find(int x) + +{ + + while (tata[x] != x) + + x = tata[tata[x]]; + + return x; + +} +bool Union(int x, int y) + +{ + + x = Find(x); + + y = Find(y); + + + + if (x != y) + + { + + if (rang[x] <= rang[y]) + + { + + tata[x] = y; + + rang[y] += rang[x]; + + } + + else + + { + + tata[y] = x; + + rang[x] += rang[y]; + + } + + + + return true; + + } + + + + return false; + +} + + + +int main() + +{ + + int n, m, x, y, c; + + int cost = 0; + + vector arbore; + + vector muchii; + + f >> n >> m; + + muchii.resize(m); + + for (int i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + muchii.push_back({c,x,y}); + + } + + sort(muchii.begin(), muchii.end(), comp); + + + + for (int i = 1; i <= n; i++) + + { + + tata[i] = i; + + rang[i] = 1; + + } + + + + for (muchie m : muchii) + + { + + x = m.x; + + y = m.y; + + if (Union(x, y)) + + { + + arbore.push_back(make_pair(x,y)); + + cost += m.c; + + } + + } + + g << cost << '\n' << arbore.size() << '\n'; + + for (size_t i = 0; i < arbore.size(); i++) + + g << arbore[i].first << " " << arbore[i].second << '\n'; + + return 0; + +} diff --git a/mst/kruskal/2ecd0160-db31-4905-bc0d-b49b55da2c1b.cpp b/mst/kruskal/2ecd0160-db31-4905-bc0d-b49b55da2c1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78a1bcb922967bcd0f74d5377a693e2125a6c952 --- /dev/null +++ b/mst/kruskal/2ecd0160-db31-4905-bc0d-b49b55da2c1b.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int Nmax=400005; + + + +int n, m, Total, T[Nmax], cnt; + + + +struct Muchie + +{ + + int x, y, cost; + +}G[Nmax]; + + + +Muchie A[Nmax]; + + + +bool compare(Muchie a, Muchie b) + +{ + + return a.cost < b.cost; + +} + + + +void citire() + +{ + + fin>>n>>m; + + for(int i=1; i<=m; i++) + + { + + fin>>G[i].x>>G[i].y>>G[i].cost; + + } + + for(int i=1; i<=n; i++) + + T[i]=i; + + sort(G+1, G+m+1, compare); + +} + + + +int leaga(int a, int b) + +{ + + T[a]=T[b]; + +} + + + +int radacina(int a) + +{ + + if(a==T[a]) return a; + + else return T[a]=radacina(T[a]); + +} + + + +void Kruskal() + +{ + + int r1, r2; + + for(int i=1; i<=m; i++) + + { + + r1= radacina(G[i].x); + + r2= radacina(G[i].y); + + if(r1!=r2) + + { + + Total+=G[i].cost; + + A[++cnt]=G[i]; + + leaga(r1, r2); + + } + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + fout< + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n,m,total,k; + +int tata[400005],rang[400005]; + +pair P[400005]; + + + +struct Muchie + +{ + + int x,y,cost; + +}v[400005]; + + + +bool Compare(Muchie a,Muchie b) + +{ + + return a.cost < b.cost; + +} + + + +void Read() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>v[i].x>>v[i].y>>v[i].cost; + + } + + + + sort(v+1,v+m+1,Compare); + + + + for(int i=1;i<=n;i++) + + { + + tata[i]=i; + + rang[i]=1; + + } + + f.close(); + +} + + + +int Find(int nod) + +{ + + while(tata[nod] != nod) + + nod = tata[nod]; + + return nod; + +} + + + +void Uneste(int x,int y) + +{ + + if(rang[x] < rang[y]) + + tata[x] = y; + + if(rang[x] > rang[y]) + + tata[y] = x; + + if(rang[x] == rang[y]) + + { + + tata[x]=y; + + rang[y]++; + + } + +} + + + +void Solve() + +{ + + for(int i=1;i<=m;i++) + + { + + int tata_x = Find(v[i].x); + + int tata_y = Find(v[i].y); + + + + if(tata_x != tata_y) + + { + + Uneste(tata_x,tata_y); + + P[++k].first = v[i].x; + + P[k].second = v[i].y; + + total += v[i].cost; + + } + + } + +} + + + +void Afisare() + +{ + + g< + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + +class UnionFind +{ + int size; + + + int *sz; + + + int *id; + +public: + UnionFind(int size) + { + size = size; + + sz = new int[size]; + id = new int[size]; + + for (int i = 0; i < size; i++) + { + + id[i] = i; + sz[i] = 1; + } + } + + + int find(int p) + { + int root = p; + while (id[root] != root) + { + root = id[root]; + } + + + while (p != root) + { + int next = id[p]; + id[p] = root; + p = next; + } + + return root; + } + + bool connected(int p, int q) + { + + return find(p) == find(q); + } + + void unify(int p, int q) + { + int root1 = find(p); + int root2 = find(q); + + + if (root1 == root2) + return; + + + if (sz[root1] > sz[root2]) + { + id[root2] = root1; + sz[root1] += sz[root2]; + } + else + { + id[root1] = root2; + sz[root2] += sz[root1]; + } + } +}; + +template +struct triplet +{ + T from, to, cost; +}; + +template +triplet make_triplet(const T &m1, const T &m2, const T &m3) +{ + triplet ans; + ans.from = m1; + ans.to = m2; + ans.cost = m3; + return ans; +} + +const int VMAX = 200001; +vector> edges; +int v, e; + + +pair rez[VMAX - 1]; +int mstCost = 0; + +bool cmp(triplet a, triplet b) +{ + return a.cost < b.cost; +} + +int main() +{ + in >> v >> e; + for (int i = 0; i < e; i++) + { + int from, to, cost; + in >> from >> to >> cost; + + edges.push_back(make_triplet(from, to, cost)); + } + + sort(edges.begin(), edges.end(), cmp); + + UnionFind u(v + 1); + int edge_count = 0; + for (auto edge : edges) + { + if (edge_count == v - 1) + break; + if (!u.connected(edge.from, edge.to)) + { + u.unify(edge.from, edge.to); + rez[edge_count++] = make_pair(edge.from, edge.to); + mstCost += edge.cost; + } + } + + out << mstCost << '\n'; + out << v - 1 << '\n'; + for (int i = 0; i < v - 1; i++) + { + out << rez[i].first << ' ' << rez[i].second << '\n'; + } + return 0; +} diff --git a/mst/kruskal/31e2bd14-f9b4-4e60-a1f4-58235d1b0c6e.cpp b/mst/kruskal/31e2bd14-f9b4-4e60-a1f4-58235d1b0c6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a09b5458b1c12ca2ed63968089820d2ee8b0c5c9 --- /dev/null +++ b/mst/kruskal/31e2bd14-f9b4-4e60-a1f4-58235d1b0c6e.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + + + + +int father[200000 +1],depth[200000 +1]; +int num_nodes,num_edges; + +struct edge{ + int a,b,cost; +} edges[400000]; + +bool compare(const edge&a,const edge&b){ + return a.costresult; + for(int i=0;i>num_nodes>>num_edges; + for(int i=0;i>edges[i].a>>edges[i].b>>edges[i].cost; + } +} + + +int main(){ + read(); + solve(); + return 0; + +} diff --git a/mst/kruskal/3283dc71-5d50-4ad4-ba81-6c3e06ba41d9.cpp b/mst/kruskal/3283dc71-5d50-4ad4-ba81-6c3e06ba41d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c487c8a3f2df70527452c77a7196b5add668a085 --- /dev/null +++ b/mst/kruskal/3283dc71-5d50-4ad4-ba81-6c3e06ba41d9.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 200005; + +int n, m; +vector>> graph; +vector>> tree; +int color[NMAX]; +int parent[NMAX]; + +void initialize(int node) { + parent[node] = color[node] = 0; +} + +int get_repr(int node) { + while (parent[node] != 0) + node = parent[node]; + return node; +} + +void reunion(int i, int j) { + int repr_i = get_repr(i); + int repr_j = get_repr(j); + + if (color[repr_i] > color[repr_j]) { + parent[repr_j] = repr_i; + } + else { + parent[repr_i] = repr_j; + + if (color[repr_i] == color[repr_j]) { + color[repr_j]++; + } + } +} + +int main() { + ifstream fin("apm.in"); + ofstream fout("apm.out"); + + fin >> n >> m; + int x, y, w; + + for (int i = 0; i < m; i++) { + fin >> x >> y >> w; + graph.emplace_back(w, make_pair(x, y)); + } + + sort(graph.begin(), graph.end()); + + for (int node = 1; node <= n; node++) { + initialize(node); + } + + int nr = 0; + int cost = 0; + + for (int i = 0; i < m; i++) { + auto edge = graph[i].second; + w = graph[i].first; + x = edge.first; + y = edge.second; + if (get_repr(x) != get_repr(y)) { + tree.emplace_back(w, make_pair(x, y)); + cost += w; + + reunion(x, y); + nr++; + + if (nr == n - 1) { + break; + } + } + } + + fout << cost << "\n" << nr << "\n"; + for (auto edge : tree) { + x = edge.second.first; + y = edge.second.second; + fout << x << " " << y << "\n"; + } + + fin.close(); + fout.close(); + return 0; +} diff --git a/mst/kruskal/33188144-800c-4906-90c0-8430fcc820bc.cpp b/mst/kruskal/33188144-800c-4906-90c0-8430fcc820bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..124bbc88a5c9e3c44211feba101dbdb4b003f979 --- /dev/null +++ b/mst/kruskal/33188144-800c-4906-90c0-8430fcc820bc.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream f ("apm.in"); + +ofstream g ("apm.out"); + + + +const int nmax = 2e5 + 1; + + + +int rin[nmax]; + + + +struct Graf + +{ + + int x, y, cost; + +}; + +Graf v[nmax], rasp[nmax]; + + + +bool Compara(Graf a, Graf b) + +{ + + return a.cost < b.cost; + +} + + + +int Serif(int i) + +{ + + if(rin[i] == i) + + return i; + + + + return rin[i] = Serif(rin[i]); + +} + + + +int main() + +{ + + int n, m, cnt = 0, k = 0; + + + + f >> n >> m; + + + + for(int i = 1; i <= m; ++i) + + f >> v[i].x >> v[i].y >> v[i].cost; + + + + for(int i = 1; i <= n; ++i) + + rin[i]=i; + + + + sort(v + 1, v + m + 1, Compara); + + + + for(int i = 1; i <= m; ++i) + + { + + if(Serif(v[i].x) != Serif(v[i].y)) + + { + + rin[Serif(v[i].y)] = v[i].x; + + cnt += v[i].cost; + + rasp[++k].x = v[i].x; + + rasp[k].y = v[i].y; + + } + + } + + + + g< +#include +#include +#include +#include +#include + +using namespace std; +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +int find(vector& parent, int a) { + int parenta = a; + while(parenta != parent[parenta]) { + parenta = parent[parenta]; + } + + while(a != parent[a]) { + int tmp = parent[a]; + parent[a] = parenta; + a = tmp; + } + + return parenta; +} + + +void union_find(vector& rank, vector& parent, int a, int b) { + int parenta = find(parent, a); + int parentb = find(parent, b); + if (rank[parenta] >= rank[parentb]) { + parent[parentb] = parenta; + } else { + parent[parenta] = parentb; + } + + if (rank[parenta] == rank[parentb]) { + ++rank[parenta]; + } +} + +int main() +{ + int n, m, a, b, c; + fin>>n>>m; + vector parent(n+1); + vector rank(n+1); + for (int i = 0; i < n; ++i) { + parent[i] = i; + rank[i] = 1; + } + priority_queue, vector >, greater > > pq; + for (int i = 0; i < m; ++i) { + fin>>a>>b>>c; + pq.emplace(c, a, b); + } + + int final_cost = 0; + vector > ret; + while (pq.size()) { + auto tp = pq.top(); pq.pop(); + int cost = get<0>(tp); int node_a = get<1>(tp); int node_b = get<2>(tp); + + if (find(parent, node_a) == find(parent, node_b)) { + continue; + } + + union_find(rank, parent, node_a, node_b); + ret.push_back(make_pair(node_a, node_b)); + final_cost += cost; + } + + fout< + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct ex{ + + int x , y, cost; + +}g[200005], sol[200005]; + + + +int n, m, rang[200005], z, t[200005]; + +long long int sum; + + + +bool cmp(ex a, ex b){ + + return a.cost < b.cost; + +} + + + +int radacina(int k){ + + if(!t[k]) + + return k; + + else + + return t[k] = radacina(t[k]); + +} + + + +void unire(int rp, int rk){ + + if(rang[rk] > rang[rp]) + + t[rp] = rk; + + else{ + + t[rk] = rp; + + if(rang[rk] == rang[rp]) + + rang[rp]++; + + } + +} + + + +int main(){ + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + in>>g[i].x>>g[i].y>>g[i].cost; + + sort(g + 1, g + m + 1, cmp); + + + + for(int i = 1; i <= m; i++){ + + int rk = radacina(g[i].x), rp = radacina(g[i].y); + + if(rk != rp){ + + unire(rp, rk); + + sol[++z].x = g[i].x, sol[z].y = g[i].y, sol[z].cost = g[i].cost; + + sum += g[i].cost; + + } + + } + + out< +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int root(vi &par, int x) { + int rx = x; + while(rx != par[rx]) { rx = par[rx]; } + while(x != rx) { + int i = par[x]; + par[x] = rx; + x = i; + } + return rx; +} + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + int N, M; + fin >> N >> M; + vi par(N); + vector muchii(M); + queue used; + + for(int i = 0; i < (int)(N); i++) { par[i] = i; } + + priority_queue, greater > pq; + int a, b, c; + for(int i = 0; i < (int)(M); i++) { + fin >> a >> b >> c; + --a; --b; + muchii[i] = {a, b}; + pq.push({c, i}); + } + int cost = 0; + while(!pq.empty()) { + auto X = pq.top(); pq.pop(); + int c = X.first; + pii much = muchii[X.second]; + int a = much.first; + int b = much.second; + + int ra = root(par, a); + int rb = root(par, b); + if (ra == rb) { + continue; + } else { + cost += c; + used.push(X.second); + par[ra] = par[rb] = min(ra, rb); + } + } + fout << cost << '\n' << N-1 << '\n'; + while(!used.empty()) { + pii much = muchii[used.front()]; used.pop(); + fout << much.first+1 << " " << much.second + 1 << '\n'; + } + + return 0; +} diff --git a/mst/kruskal/34db2a17-7f35-4d23-84ac-71ca1d8b2f72.cpp b/mst/kruskal/34db2a17-7f35-4d23-84ac-71ca1d8b2f72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa756584fd91b25f5c3eee71f92be3b054d4d8d7 --- /dev/null +++ b/mst/kruskal/34db2a17-7f35-4d23-84ac-71ca1d8b2f72.cpp @@ -0,0 +1,218 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct edge + +{ + + int node1, node2; + + int cost; + +}; + + + +bool cmp(edge a, edge b) + +{ + + return a.cost < b.cost; + +} + + + +int n, m; + + + +vector < edge > e; + +vector < int > sol; + +vector < int > parent, h; + + + +void uneste(int x, int y) + +{ + + while (parent[x]) + + x = parent[x]; + + + + while (parent[y]) + + y = parent[y]; + + + + if (x == y) + + return; + + + + if (h[x] <= h[y]) + + { + + parent[x] = y; + + h[y] = max(h[y], h[x] + 1); + + } + + else + + { + + parent[y] = x; + + h[x] = max(h[x], h[y] + 1); + + } + +} + + + + + +bool verif(int x, int y) + +{ + + int m1 = x, m2 = y; + + + + while (parent[m1]) + + m1 = parent[m1]; + + + + while (parent[m2]) + + m2 = parent[m2]; + + + + int aux; + + + + while (parent[x]) + + { + + aux = x; + + x = parent[x]; + + parent[aux] = m1; + + } + + + + while (parent[y]) + + { + + aux = y; + + y = parent[y]; + + parent[aux] = m2; + + } + + + + return m1 != m2; + +} + + + +int main() + +{ + + f >> n >> m; + + + + e.resize(m); + + h.resize(n + 1); + + parent.resize(n + 1); + + + + for (int i=0; i> e[i].node1 >> e[i].node2 >> e[i].cost; + + + + sort(e.begin(), e.end(), cmp); + + + + int costTotal = 0; + + for (int i=0; i +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct MUCHIE + +{ + + int x, y, c; + + bool v; + +}; + + + +vector v(1); + +vector t(1), h(1); + +MUCHIE temp; + +int n, m, x, y, c, vfx, vfy, nrm, cost, i; + + + +bool cmp(MUCHIE a, MUCHIE b) + +{ + + return a.c < b.c; + +} + + + +int varf(int x) + +{ + + while (x != t[x]) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if (h[x] < h[y]) + + t[x] = y; + + else if (h[y] < h[x]) + + t[y] = x; + + else + + { + + t[y] = x; + + ++h[x]; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (i = 1; i <= n; ++i) + + { + + t.push_back(i); + + h.push_back(1); + + } + + for (i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + temp.x = x; + + temp.y = y; + + temp.c = c; + + v.push_back(temp); + + } + + sort(v.begin() + 1, v.end(), cmp); + + for (i = 1; i <= m; ++i) + + { + + vfx = varf(v[i].x); + + vfy = varf(v[i].y); + + if (vfx != vfy) + + { + + unire(vfx, vfy); + + v[i].v = true; + + ++nrm; + + cost += v[i].c; + + } + + } + + fout << cost << '\n' << nrm << '\n'; + + for (i = 1; i <= m; ++i) + + if (v[i].v) + + fout << v[i].x << ' ' << v[i].y << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/35e92328-226b-40fd-b64f-89a3347072de.cpp b/mst/kruskal/35e92328-226b-40fd-b64f-89a3347072de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e789af995a9a3d72bee6de9e83ded57814e532e --- /dev/null +++ b/mst/kruskal/35e92328-226b-40fd-b64f-89a3347072de.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +int t[200010], S[200010]; + +int a, b, ra, rb, n, m, i, sol, alese; + + + +int rad(int x) { + + + + while(t[x] > 0) + + x = t[x]; + + return x; + +} + + + +pair< int, pair > v[400010]; + + + +int main (){ + + fin>>n>>m; + + for (i=1;i<=m;i++) + + fin>>v[i].second.first>>v[i].second.second>>v[i].first; + + sort(v+1, v+m+1); + + + + for (i=1;i<=n;i++) + + t[i] = -1; + + + + for (i=1;i<=m;i++) { + + a = v[i].second.first; + + b = v[i].second.second; + + + + ra = rad(a); + + rb = rad(b); + + + + if (ra != rb) { + + if (-t[ra] > -t[rb]) { + + + + t[ra] += t[rb]; + + t[rb] = ra; + + } else { + + t[rb] += t[ra]; + + t[ra] = rb; + + } + + sol += v[i].first; + + + + alese++; + + S[alese] = i; + + if (alese == n-1) + + break; + + } + + } + + fout< +#include +#include +using namespace std; + + + +const int NMAX = 400000; + + + +struct muchie + +{ + + int u, v, c, pus; + +}; + + + +muchie x[NMAX + 5]; + +int t[200005], h[200005]; + + + +int varf(int x) + +{ + + while(t[x] != x) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if(h[x] > h[y]) + + t[y] = x; + + else + + { + + if(h[x] < h[y]) + + t[x] = y; + + else + + { + + t[y] = x; + + ++h[x]; + + } + + } + +} + + + +bool cmp(muchie a, muchie b) + +{ + + return a.c>n>>m; + + for(i = 1; i <= n; ++i) + + t[i] = i, h[i] = 1; + + for(i = 1; i <= m; ++i) + + fin>>x[i].u >> x[i].v>>x[i].c; + + cost = 0; + + sort(x + 1, x + m + 1, cmp); + + int rx, ry, ms; + + for(ms = 0, i = 1; ms < n-1 && i <= m; ++i) + + { + + rx = varf(x[i].u); + + ry = varf(x[i].v); + + if(rx != ry) + + { + + unire(rx, ry); + + x[i].pus = 1; + + cost = cost + x[i].c; + + ++ms; + + } + + } + + fout< +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m,sum=0; + +int tree[200005]; + + + +struct muchie{ + + int x, y, cost; + + bool operator<(const muchie &other) const { + + if(cost == other.cost) + + { + + if(x == other.x) + + return y>n>>m; + + for(int i=1; i<=n; i++) + + tree[i] = i; + + for(int i=1; i<=m; i++){ + + f >> muchii[i].x >> muchii[i].y >> muchii[i].cost; + + } + +} + +int main() + +{ + + citire(); + + sort(muchii + 1, muchii + m + 1); + + rezolvare(); + + g< +#include + + +using namespace std; + +ifstream cin ("apm.in"); + +ofstream cout ("apm.out"); + +struct ura{ + + int x, y, c, ok; + +}; + +ura v[400005]; + +int h[200005]; + +bool cmp(ura a, ura b) + +{ + + return a.c < b.c; + +} + +int daddy[200005]; + +int nr, cost; + +int bigdaddy(int x) + +{ + + if (daddy[x] == x) + + return x; + + int d = bigdaddy(daddy[x]); + + daddy[x] = d; + + return d; + +} + +int unire(int i) + +{ + + int x = v[i].x, y = v[i].y; + + int d1 = bigdaddy(x), d2 = bigdaddy(y); + + if (d1 != d2) + + { + + nr++; + + v[i].ok = 1; + + cost += v[i].c; + + if (h[d1] > h[d2]) + + daddy[d2] = d1; + + else + + { + + daddy[d1] = d2; + + if (h[d1] == h[d2]) + + h[d2]++; + + } + + } + +} + +int main() + +{ + + int n, m, i; + + cin >> n >> m; + + for (i = 1; i <= m; i++) + + cin >> v[i].x >> v[i].y >> v[i].c; + + for (i = 1; i <= n; i++) + + daddy[i] = i; + + sort (v + 1, v + m + 1, cmp); + + nr = cost = 0; + + for (i = 1; i <= m; i++) + + unire(i); + + cout << cost << '\n' << nr; + + for (i = 1; i <= m; i++) + + if (v[i].ok == 1) + + cout << '\n' << v[i].x << " " << v[i].y; + + return 0; + +} diff --git a/mst/kruskal/39ef3567-7e9e-4535-b1de-72619540f93a.cpp b/mst/kruskal/39ef3567-7e9e-4535-b1de-72619540f93a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83a93311f855bb7367804e9c24438303ca77b04d --- /dev/null +++ b/mst/kruskal/39ef3567-7e9e-4535-b1de-72619540f93a.cpp @@ -0,0 +1,145 @@ +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int i, j, c; + +}; + + + +int n, m, cost; + +muchie M[400001]; + +int CC[200001]; + +int NrM[200001]; + + + +void citire() + +{ + + f >> n >> m; + + for(int i = 1; i <= m; i++) + + f >> M[i].i >> M[i].j >> M[i].c; + +} + + + +void afis() + +{ + + g << cost << '\n'; + + g << n - 1 << '\n'; + + for(int i = 1; i < n; i++) + + { + + int k = NrM[i]; + + g << M[k].i << ' ' << M[k].j << '\n'; + + } + +} + + + +bool comp(const muchie &a, const muchie &b) + +{ + + return a.c < b.c; + +} + + + +int grupa(int i) + +{ + + if(CC[i] == 0) + + return i; + + return CC[i] = grupa(CC[i]); + +} + + + +void Kruskal() + +{ + + sort(M + 1, M + m + 1, comp); + + int nm = 0; + + for(int i = 1; i <= m; i++) + + { + + int ci = grupa(M[i].i); + + int cj = grupa(M[i].j); + + if(ci != cj) + + { + + cost += M[i].c; + + CC[ci] = cj; + + NrM[++nm] = i; + + } + + if(nm == n - 1) + + break; + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + afis(); + + return 0; + +} diff --git a/mst/kruskal/3ad53bb5-7f2f-4d41-8b97-21717bcdbb7a.cpp b/mst/kruskal/3ad53bb5-7f2f-4d41-8b97-21717bcdbb7a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..238eec9fbd1fa12b90a2569fd3874a2701516951 --- /dev/null +++ b/mst/kruskal/3ad53bb5-7f2f-4d41-8b97-21717bcdbb7a.cpp @@ -0,0 +1,207 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +typedef long long ll; + +typedef pair pi; + +const int dim=4e5+2; + +ll t,T,n,m,a,b,c,tata[dim],h[dim],R[dim],rmq[dim][20]; + +ll ans; + +bitset < dim > there,viz; + + + +pi father[dim]; + + + +struct go + +{ + + ll v1,c,idx; + +}; + + + +struct no + +{ + + ll v1,v2,c,idx; + +}B[dim]; + +vector < pi > OP; + +vector < go > V[dim]; + +vector < go > A(dim); + + + +bool cmp(no X,no Y) + +{ + + if(X.ch[y]) tata[y]=x; + + else + + { + + tata[x]=y; + + if(h[x]==h[y]) h[y]++; + + } + +} + + + +int Find(int v) + +{ + + int r=v; + + while(tata[r]!=r) r=tata[r]; + + int y=v; + + while(y!=r) + + { + + t=tata[y]; + + tata[y]=r; + + y=t; + + } + + return r; + +} + + + +void Built() + +{ + + for(int i=1;i<=n;i++) + + { + + tata[i]=i; + + h[i]=0; + + } + + sort(B+1,B+m+1,cmp); + + + + for(int i=1;i<=m;i++) + + { + + a=B[i].v1; b=B[i].v2; c=B[i].c; + + if(Find(a)!=Find(b)) + + { + + Union(Find(a),Find(b)); + + OP.push_back({a,b}); + + there[B[i].idx]=1; + + ans+=c; + + } + + } + + father[1]={0,0}; + +} +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c,i}); + + V[b].push_back({a,c,i}); + + A[i]={a,b,c}; + + B[i]={a,b,c,i}; + + } + + Built(); + + + + g< +#include +std::ifstream f("apm.in"); + +std::ofstream g("apm.out"); + + + + + +const int LIM = 500000; + +int N, M,Sum; + +int tata[LIM], rang[LIM]; + +int nr; + +struct muchie + +{ + + int first,second,cost; + + int einarbore; + +}m[LIM]; + + + + + + + +bool cnd(muchie a, muchie b) + +{ + + return (a.cost < b.cost); + +} + + + + + +void citire() + +{ + + f >> N >> M; + + + + for (int i = 1; i <= M; i++) + + f >> m[i].first >> m[i].second >> m[i].cost; + + + + std::sort(m + 1, m + M + 1, cnd); +} + + + +int T(int x) + +{ + + if (tata[x] == 0) + + return x; + + else + + { + + int y = T(tata[x]); + + tata[x] = y; + + return y; + + } + +} + + + +void unire(muchie &x) + +{ + + int tf = T(x.first); + + int ts = T(x.second); + + if (tf != ts) + + { + + if (rang[tf] > rang[ts]) + + tata[ts] = tf; + + else + + { + + tata[tf] = ts; + + if (rang[tf] == rang[ts]) + + rang[ts]++; + + } + + Sum += x.cost; + + nr++; + + x.einarbore = 1; + + + + + + } + + + +} + + + +void Kruskal() + +{ + + + + + + for (int i = 1; i <= M; i++) + + if (tata[m[i].first] != tata[m[i].second] || (!tata[m[i].first] && !tata[m[i].second])) + + unire(m[i]); + + + + + + + + g << Sum; + + g << '\n' << nr << '\n'; + + for (int i = 1; i <= M; i++) + + if(m[i].einarbore) + + g << m[i].first << " " << m[i].second << '\n'; + + + +} + + + + + +int main() + +{ + + citire(); + + Kruskal(); + + f.close(); + + return 0; + +} diff --git a/mst/kruskal/3c03a63f-4173-487a-9fbf-783e0c2bb59c.cpp b/mst/kruskal/3c03a63f-4173-487a-9fbf-783e0c2bb59c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4262eb9462aee465f3878a02131c32cfceff8999 --- /dev/null +++ b/mst/kruskal/3c03a63f-4173-487a-9fbf-783e0c2bb59c.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream cin("apm.in"); + + + +ofstream cout("apm.out"); + + + +struct Op{int x;int y;int c;}; + + + +vector edge,sol; + + + +bool mycmp(Op s,Op t) + + + +{return s.c>n>>m; + + + +for(int i=0;i>x>>y>>c,edge.push_back({x,y,c}); + + + +sort(edge.begin(),edge.end(),mycmp); + + + +for(int i=1;i<=n;++i)link[i]=i,dim[i]=1; + + + +for(auto t:edge)if(tata(t.x)!=tata(t.y)) + + + +unite(t.x,t.y),sol.push_back(t),s+=t.c; + + + +cout< +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchii + +{ + + int st, dr, cost; + +}; + + + +const int NMAX = 400001; + + + +int n, m, T[NMAX], rad; + +muchii M[NMAX], sol[NMAX]; + + + +bool cmp(muchii a, muchii b) + +{ + + return a.cost> n >> m; + + for(int i = 0; i < m; i++) + + f >> M[i].st >> M[i].dr >> M[i].cost; + +} + + + +int Rad(int i) + +{ + + if(T[i]==i) + + return i; + + return T[i] = Rad(T[i]); + +} + + + +int main() + +{ + + citire(); + + + + sort(M, M + m, cmp); + + + + for(int i = 1; i <= n; i++) + + T[i] = i; + + + + int S = 0, nr = 0; + + for(int i = 0; i < m; i++) + + if(Rad(M[i].st) != Rad(M[i].dr)) + + { + + T[Rad(M[i].dr)] = M[i].st; + + S += M[i].cost; + + + + sol[++nr].st = M[i].st; + + sol[nr].dr = M[i].dr; + + } + + + + g << S << '\n' << nr << '\n'; + + for(int i = 1; i <= nr; i++) + + g << sol[i].st << ' ' << sol[i].dr << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/3d16e852-169d-4c15-a46e-92b862bdb930.cpp b/mst/kruskal/3d16e852-169d-4c15-a46e-92b862bdb930.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e9470ff5bcb7ea5401cb54881b1b773cf58c07a --- /dev/null +++ b/mst/kruskal/3d16e852-169d-4c15-a46e-92b862bdb930.cpp @@ -0,0 +1,114 @@ +#include + + + + +using namespace std; + +const int DN=1e6+5; + +int n,m,pr[DN]; + +pair >mc[DN]; + +priority_queue >pq; + +vector >sol; + + + +long long sum; + + + +int getpr(int nod) + +{ + + if(pr[nod]==nod) + + return nod; + + pr[nod]=getpr(pr[nod]); + + return pr[nod]; + +} + + + + + +void unesc(int f,int g,int cost) + +{ + + int p1=getpr(f); + + int p2=getpr(g); + + cout< +#include +#include +using namespace std; + +using pii = pair; + +int N, M; + +vector parent, tree_size; + + + + + +struct comp + +{ + + inline bool operator()(const pair &edge1, const pair &edge2) + + { + + return edge1.first > edge2.first; + + } + +}; + +priority_queue, vector>, comp> adj; + + + + + +void make_set(const int &vertex) + +{ + + parent[vertex] = vertex; + + tree_size[vertex] = 1; + +} + + + + + +int find_leader(const int &vertex) + +{ + + if (parent[vertex] == vertex) + + { + + return vertex; + + } + + + + return parent[vertex] = find_leader(parent[vertex]); + +} + + + + + +void unite_sets(int &set1, int &set2) + +{ + + set1 = find_leader(set1); + + set2 = find_leader(set2); + + + + if (set1 != set2) + + { + + if (tree_size[set1] < tree_size[set2]) + + { + + swap(set1, set2); + + } + + + + tree_size[set1] += tree_size[set2]; + + parent[set2] = set1; + + } + +} + + + + + +void Kruskal() + +{ + + vector MST_edges; + + int total_weight = 0; + + + + for (int node = 1; node <= N; ++node) + + { + + make_set(node); + + } + + + + for (int i = 1; i < N; ++i) + + { + + int CC1, CC2, leader1, leader2; + + + + do + + { + + CC1 = adj.top().second.first; + + CC2 = adj.top().second.second; + + leader1 = find_leader(CC1); + + leader2 = find_leader(CC2); + + adj.pop(); + + } while (leader1 == leader2); + + + + total_weight += adj.top().first; + + MST_edges.push_back({CC1, CC2}); + + unite_sets(leader1, leader2); + + } + + + + ofstream fout("apm.out"); + + + + fout << total_weight << "\n" << N - 1 << "\n"; + + for (const pii &MST_edge : MST_edges) + + { + + fout << MST_edge.first << " " << MST_edge.second << "\n"; + + } + +} + + + + + +int main() + +{ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + parent.resize(N + 1); + + tree_size.resize(N + 1); + + + + while (M--) + + { + + int i, j, weight; + + fin >> i >> j >> weight; + + + + adj.push({weight, {i, j}}); + + adj.push({weight, {j, i}}); + + } + + + + Kruskal(); + +} diff --git a/mst/kruskal/3df34d2f-a5e9-4204-9694-37d7592113a8.cpp b/mst/kruskal/3df34d2f-a5e9-4204-9694-37d7592113a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c1c65744542209aa6da1c23b23d7f5b8e905888 --- /dev/null +++ b/mst/kruskal/3df34d2f-a5e9-4204-9694-37d7592113a8.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchii + +{ + + int st, dr, cost; + +}; + + + +const int NMAX = 400001; + + + +int n, m, T[NMAX], rad; + +muchii M[NMAX], sol[NMAX]; + + + +bool cmp(muchii a, muchii b) + +{ + + return a.cost> n >> m; + + for(int i = 0; i < m; i++) + + f >> M[i].st >> M[i].dr >> M[i].cost; + +} + + + +int Rad(int i) + +{ + + if(T[i]==i) + + return i; + + return T[i] = Rad(T[i]); + +} + + + +int main() + +{ + + citire(); + + + + sort(M, M + m, cmp); + + + + for(int i = 1; i <= n; i++) + + T[i] = i; + + + + int S = 0, nr = 0; + + for(int i = 0; i < m; i++) + + if(Rad(M[i].st) != Rad(M[i].dr)) + + { + + T[Rad(M[i].dr)] = M[i].st; + + S += M[i].cost; + + + + sol[++nr].st = M[i].st; + + sol[nr].dr = M[i].dr; + + } + + + + g << S << '\n' << nr << '\n'; + + for(int i = 1; i <= nr; i++) + + g << sol[i].st << ' ' << sol[i].dr << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/3e5559e0-d211-46b5-ba2d-bac89bf7b4cf.cpp b/mst/kruskal/3e5559e0-d211-46b5-ba2d-bac89bf7b4cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..507f5efb2e97bcbe86f5faa9197bef837f4a1ee9 --- /dev/null +++ b/mst/kruskal/3e5559e0-d211-46b5-ba2d-bac89bf7b4cf.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie{ + + int x,y,c; + +}; + + + +int n,m,T[200005],rang[200005]; + +vector v; + +vector l; + + + +bool compare(muchie a, muchie b){ + + return a.c < b.c; + +} + + + +void read(int &n, int &m, vector &v){ + + f>>n>>m; + + muchie var; + + for(int i=1; i<=m; i++){ + + f>>var.x>>var.y>>var.c; + + v.push_back(var); + + } + + sort(v.begin(), v.end(), compare); + +} + + + +int Leader(int nod){ + + + + if(T[nod] == nod) + + return nod; + + int x = Leader(T[nod]); + + T[nod] = x; + + return x; + +} + + + +void Unire(int x, int y){ + + + + if(rang[x] > rang[y]) + + T[y] = x; + + else{ + + T[x] = y; + + if(rang[x] == rang[y]) + + rang[y]++; + + } + +} + + + +void Kruskal(){ + + int cost = 0, contor = 0; + + for(int i=0; i + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector > >muchii; + +int n,m,t[200001],h[200001]; + +vector >sol; + + + +bool muchie(int x,int y) + +{ + + + + int x1,y1,r1=x,r2=y,c; + + + + while(r1!=t[r1]) + + r1=t[r1]; + + + + while(r2!=t[r2]) + + r2=t[r2]; + + + + while(r1!=t[x]) + + { + + x1=t[x]; + + t[x]=t[x1]; + + x=x1; + + } + + + + while(r2!=t[y]) + + { + + y1=t[y]; + + t[y]=t[y1]; + + y=y1; + + } + + + + if(r1==r2)return false; + + + + if(h[r1]>h[r2]) + + { + + t[r2]=r1; + + c=r1; + + } + + else + + { + + t[r1]=r2; + + c=r2; + + } + + + + if(h[r1]==h[r2]) + + h[c]++; + + + + return true; + + + +} + + + +int main() + +{ + + f>>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y,c; + + f>>x>>y>>c; + + muchii.push_back(make_pair(c,make_pair(x,y))); + + } + + sort(muchii.begin(),muchii.end()); + + + + for(int i=1;i<=n;++i) + + { + + t[i]=i; + + h[i]=1; + + } + + + + n--; + + int sol2=n,cost=0; + + int i=0; + + while(n) + + { + + + + if(muchie(muchii[i].second.first,muchii[i].second.second)) + + { + + n--; + + cost+=muchii[i].first; + + sol.push_back(make_pair(muchii[i].second.first,muchii[i].second.second)); + + } + + i++; + + + + } + + g< + +std::ifstream fin("apm.in"); +std::ofstream fout("apm.out"); + +std::pair > edges[400001]; +int d[200001], sol[200001]; +int n, m; + +int get_root(int node){ + while(d[node] > 0) + node = d[node]; + return node; +} + +int main() +{ + int i, x, y, cost, total_cost = 0, found = 0; + fin >> n >> m; + for(i = 0; i < n; i++) + d[i] = -1; + for(i = 0; i < m; i++){ + fin >> x >> y >> cost; + edges[i].first = cost; + edges[i].second = std::make_pair(x, y); + } + sort(edges, edges + m); + + for(i = 0; i < m && found != n; i++){ + int node_1 = edges[i].second.first, node_2 = edges[i].second.second; + int r1 = get_root(node_1), r2 = get_root(node_2); + cost = edges[i].first; + + if(r1 != r2){ + if(d[r1] < d[r2]){ + d[r1] += d[r2]; + d[r2] = r1; + } + else{ + d[r2] += d[r1]; + d[r1] = r2; + } + + sol[found++] = i; + total_cost += cost; + } + } + + fout << total_cost << std::endl << found << std::endl; + for(i = 0; i < found; i++) + fout << edges[sol[i]].second.first << " " << edges[sol[i]].second.second << std::endl; + + + + + + return 0; +} diff --git a/mst/kruskal/40d400f9-ecc0-451e-b335-e04b9bd82e14.cpp b/mst/kruskal/40d400f9-ecc0-451e-b335-e04b9bd82e14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f81ecb86aa716df4fdf136f8182d8361445f593 --- /dev/null +++ b/mst/kruskal/40d400f9-ecc0-451e-b335-e04b9bd82e14.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int Nmax = 4e5 + 5; + +struct Muchie{ + + int x, y, c; + +}M[Nmax]; + +struct cmp{ + + bool operator()(Muchie x, Muchie y) + + { + + return x.c < y.c; + + } + +}; + +int n, m, k, Total; + +int TT[Nmax], RG[Nmax]; + +pair P[Nmax]; + + + +void Read() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + in>>M[i].x>>M[i].y>>M[i].c; + + sort(M + 1, M + m + 1, cmp()); + + for(int i = 1; i <= n; i++) + + { + + TT[i] = i; + + RG[i] = 1; + + } + + + +} + + + +int Find(int Nod) + +{ + + while(TT[Nod] != Nod) + + Nod = TT[Nod]; + + return Nod; + +} + + + +void Unire(int x, int y) + +{ + + if(RG[x] < RG[y]) + + TT[x] = y; + + else if(RG[x] > RG[y]) + + TT[y] = x; + + else if(RG[x] == RG[y]) + + { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() + +{ + + for(int i = 1; i <= m; i++) + + { + + int tatal_x = Find(M[i].x); + + int tatal_y = Find(M[i].y); + + if(tatal_x != tatal_y) + + { + + Unire(tatal_x, tatal_y); + + P[++k].first = M[i].x; + + P[k].second = M[i].y; + + Total += M[i].c; + + } + + } + +} + + + +void Print() + +{ + + out< + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +pair < int , int > P[400005]; + +int n, m, total, TT[400005], k; + + + +struct Edge { + + int x, y, c; + +}V[400005]; + + + +bool fcmp (Edge a, Edge b) { + + return a.c < b.c; + +} + + + +void Read () { + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) fin >> V[i].x >> V[i].y >> V[i].c; + + sort (V + 1, V + m + 1, fcmp); + +} + + + +int Find (int i) { + + if (TT[i] == i) + + return i; + + return TT[i] = Find(TT[i]); + +} + + + +void Unite (int i, int j) { + + int ti, tj; + + ti = Find(i); + + tj = Find(j); + + TT[tj] = ti; + +} + + + +void Solve () { + + for (int i = 1; i <= m; ++i) + + TT[i] = i; + + for (int i = 1; i <= m; ++i) { + + int xx = Find (V[i].x), yy = Find (V[i].y); + + if (xx != yy) { + + Unite (xx, yy); + + P[++k] = {V[i].x, V[i].y}; + + total += V[i].c; + + } + + } + +} + + + +void Print () { + + fout << total << '\n' << n - 1 << '\n'; + + for (int i = 1; i <= k; ++i) fout << P[i].first << ' ' << P[i].second << ' ' << '\n'; + +} + + + +int main() { + + Read (); + + Solve (); + + Print (); + + return 0; + +} diff --git a/mst/kruskal/411667b6-51bb-4cb4-93f7-c37072867c4a.cpp b/mst/kruskal/411667b6-51bb-4cb4-93f7-c37072867c4a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e625315a55b13b5d7688e33e8375e48d954c635b --- /dev/null +++ b/mst/kruskal/411667b6-51bb-4cb4-93f7-c37072867c4a.cpp @@ -0,0 +1,91 @@ +#include +#include +using namespace std; + +int v[200001]; + +struct eu{ + + int x,y,cn; + +}; + +eu d[400001],sol[200001]; + +bool cmp(eu a,eu b){ + + return a.cn +#include + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct muchie { + + int x; + + int y; + + int c; + +}; + +muchie v[2*200001], sol[200001]; + +int n, m, t[200001], i, S, dim; + +bool cmp (muchie i, muchie j) + +{ + + return i.c0) + + nod=t[nod]; + + return nod; + +} + +int main() + +{ + + fin>>n>>m; + + for (i=1; i<=m; i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + sort (v+1, v+m+1, cmp); + + for (i=1; i<=n; i++) + + t[i]=-1; + + + + for (i=1; i<=m; i++) + + { + + int rx=rad(v[i].x); + + int ry=rad(v[i].y); + + if (rx!=ry) + + { + + if (-t[rx]>-t[ry]) + + { + + t[rx]+=t[ry]; + + t[ry]=rx; + + } + + else + + { + + t[ry]+=t[rx]; + + t[rx]=ry; + + } + + S+=v[i].c; + + sol[++dim].x=v[i].x; + + sol[dim].y=v[i].y; + + } + + if (dim==n-1) + + break; + + } + + fout< + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie { + + int x, y, c, OK; + +}v[400001]; + + + +int t[200001]; + +int n, m, ct, nr; + + + +int comp(muchie u,muchie v) + +{ + + return u.c> n >> m; + + for(i = 1; i <= m; i++) + + fin >> v[i].x >> v[i].y >> v[i].c; + + + + + + for(i=1; i <= n; i++) + + t[i] = 0; + + + + + + sort(v+1,v+m+1,comp); + + + + + + ct = 0; nr = 0; i = 1; + + while(nr < n-1) + + { + + x = v[i].x; y = v[i].y; + + rx=radacina(x); + + ry=radacina(y); + + + + if(rx != ry) + + { + + ct=ct+v[i].c; + + t[ry]=rx; + + v[i].OK = 1; + + nr++; + + } + + i++; + + } + + + + + + + + fout << ct << '\n'< +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie + +{ + + int x,y,c; + +}; + + + +muchie v[400005],w[400005]; + +int t[400005]; + + + +bool cmp(muchie a, muchie b) + +{ + + if (a.c> n >> m; + + for (i=1;i<=m;i++) fin >> v[i].x >> v[i].y >> v[i].c; + + sort (v+1,v+m+1,cmp); + + for (i=1;i<=m;i++) + + { + + x=v[i].x; + + y=v[i].y; + + rx=rad(x); + + ry=rad(y); + + if (rxry) + + { + + s+=v[i].c; + + t[rx]=ry; + + nr++; + + w[nr].x=x; + + w[nr].y=y; + + } + + } + + fout << s << "\n" << nr << "\n"; + + for (i=1;i<=nr;i++) fout << w[i].y << " " << w[i].x << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/44b4e485-030e-41e3-9c7c-ffc0c05d6b15.cpp b/mst/kruskal/44b4e485-030e-41e3-9c7c-ffc0c05d6b15.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ada4a4ed3019b60d1fbe21769a56135e68832c37 --- /dev/null +++ b/mst/kruskal/44b4e485-030e-41e3-9c7c-ffc0c05d6b15.cpp @@ -0,0 +1,85 @@ +#include +#include +#include + +using namespace std; + +ifstream cin("apm.in"); +ofstream cout("apm.out"); + + + +int n, m; +int father[200005], rang[200005]; +vector ans[200005]; +int cost, k; + +struct vertex{ + int x, y, c; +} graph[200005]; + +bool compare(vertex a, vertex b) +{ + return a.c < b.c; +} + +int find(int nod) +{ + while(father[nod] != nod) + nod = father[nod]; + return nod; +} + +void kruskal() +{ + for(int i = 1;i <= m;i++) + { + + int x = find(graph[i].x), y = find(graph[i].y); + if(x != y) + { + if(rang[x] < rang[y]) + father[x] = y; + + if(rang[y] < rang[x]) + father[y] = x; + + if(rang[x] == rang[y]) { + father[x] = y; + rang[y]++; + } + k++; + ans[k].push_back(graph[i].x); + ans[k].push_back(graph[i].y); + cost += graph[i].c; + } + } + +} + +int main() +{ + cin >> n >> m; + + for(int i = 1; i <= m; i++) + cin >> graph[i].x >> graph[i].y >> graph[i].c; + + sort(graph+1, graph+m+1, compare); + + for(int i = 1; i <= m; i++) + { + father[i] = i; + rang[i] = 1; + } + + kruskal(); + + cout << cost << "\n"; + cout << k << "\n"; + + for(int i = 1; i <= k; i++) + { + cout << ans[i][0] << " " << ans[i][1] << "\n"; + } + +} diff --git a/mst/kruskal/45be22a4-8a0d-4ae3-bf04-8b000406bf8b.cpp b/mst/kruskal/45be22a4-8a0d-4ae3-bf04-8b000406bf8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42564acaf1a4b60584452e28cc9f7a53f915a519 --- /dev/null +++ b/mst/kruskal/45be22a4-8a0d-4ae3-bf04-8b000406bf8b.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector > > muchii; + +int n, m, t[200005], cost; + +vector > apm; + + + +int root(int x) { + + if(!t[x]) return x; + + t[x] = root(t[x]); + + return t[x]; + +} + + + +int Union(int x, int y) { + + int rx = root(x); + + int ry = root(y); + + if(rx == ry) return 0; + + + + t[rx] = ry; + + return 1; + +} + + + +void citire() { + + fin >> n >> m; + + while(m--) { + + int x, y, c; + + fin >> x >> y >> c; + + muchii.push_back({c, {x, y}}); + + } + +} + + + +void solve() { + + sort(muchii.begin(), muchii.end()); + + for(auto muc: muchii) { + + int x = muc.second.first; + + int y = muc.second.second; + + int c = muc.first; + + + + if(Union(x, y)) { + + apm.push_back({x, y}); + + cost += c; + + } + + } + +} + + + +void afis() { + + fout << cost << '\n' << apm.size() << '\n'; + + for(auto x: apm) fout << x.first << ' ' << x.second << '\n'; + +} + + + +int main() { + + citire(); + + solve(); + + afis(); + +} diff --git a/mst/kruskal/45fb6fb1-e5a9-43f3-a003-ff9513ce6851.cpp b/mst/kruskal/45fb6fb1-e5a9-43f3-a003-ff9513ce6851.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8ff3ee67e2c4896fcb48936d2f373e6e39b7d3b --- /dev/null +++ b/mst/kruskal/45fb6fb1-e5a9-43f3-a003-ff9513ce6851.cpp @@ -0,0 +1,213 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200005; + + + +int tt[N], rnk[N]; + + + +int n, m; + + + +struct muchie + +{ + + int x, y, c; + +}; + + + +vector v; + +vector > rez; + + + + + +bool comp(muchie a, muchie b) + +{ + + return a.c < b.c; + +} + + + +int root(int x) + +{ + + int R, y; + + for(R=x; R!=tt[R]; R=tt[R]); + + + + while(x != tt[x]) + + { + + y = tt[x]; + + tt[x] = R; + + x = y; + + } + + return R; + +} + + + +void unite(int x, int y) + +{ + + if(rnk[x] > rnk[y]) + + { + + tt[y] = x; + + } + + else + + { + + tt[x] = y; + + } + + if(rnk[x] == rnk[y]) + + { + + rnk[y]++; + + } + +} + + + +int kruskal() + +{ + + int cmin = 0; + + sort(v.begin(), v.end(), comp); + + int nr = 0, i = 0; + + + + while(nr < n-1) + + { + + int n1 = v[i].x, n2 = v[i].y, cst = v[i].c; + + int rx = root(n1), ry = root(n2); + + if(rx != ry) + + { + + unite(rx, ry); + + cmin += cst; + + nr++; + + rez.push_back({n1, n2}); + + } + + i++; + + } + + + + return cmin; + + + +} + + + + + +int main() + +{ + + int i, j, a, b, C; + + fin>>n>>m; + + for(i=1; i<=m; i++) + + { + + fin>>a>>b>>C; + + v.push_back({a, b, C}); + + } + + + + for(i=1; i<=n; i++) + + { + + tt[i] = i; + + rnk[i] = 1; + + } + + fout< +#include +#include +#include + + + + + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int findNode(int node, vector& T) + +{ + + if(node != T[node]) + + return T[node] = findNode(T[node], T); + + + + return node; + +} + + + +void uneste(int node1, int node2, vector& T) + +{ + + int m1 = findNode(node1, T); + + int m2 = findNode(node2, T); + + + + T[m1] = m2; + +} + + + +bool cmp(const pair, int> x,const pair, int> y) + +{ + + return x.second < y.second; + +} + + + +void kruskal(vector, int> >& V, vector& T) + +{ + + int cost = 0; + + vector > sol; + + + + for(int i = 0; i < V.size(); ++i) + + { + + int x = V[i].first.first; + + int y = V[i].first.second; + + int c = V[i].second; + + + + if(findNode(x, T) != findNode(y, T)) + + { + + uneste(x, y, T); + + cost += c; + + sol.push_back(make_pair(x,y)); + + } + + } + + + + out << cost << '\n'; + + out << sol.size() << '\n'; + + for(int i = 0; i < sol.size(); ++i) + + { + + out << sol[i].first << " " << sol[i].second << '\n'; + + } + +} + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + + + vector, int> > V; + + vector T( n + 2); + + + + for(int i = 1; i<=n; ++i) + + T[i] = i; + + + + for(int x, y, cost; m; --m) + + { + + in >> x >> y >> cost; + + V.push_back(make_pair(make_pair(x, y), cost)); + + } + + + + sort(V.begin(), V.end(), cmp); + + kruskal(V, T); + + return 0; + +} diff --git a/mst/kruskal/46042818-8866-470f-98ba-56b9b764c07a.cpp b/mst/kruskal/46042818-8866-470f-98ba-56b9b764c07a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8214c39fe3077588b1f53fed86ee85682e3184e2 --- /dev/null +++ b/mst/kruskal/46042818-8866-470f-98ba-56b9b764c07a.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int NMax=400005; + +pair P[NMax]; + +int n,m,total,TT[200005],Rang[200005],cnt; + +struct Muchie + +{ + + int x,y,cost; + +} V[NMax]; + +bool compare(Muchie a,Muchie b) + +{ + + return a.costRang[J]) + + TT[J]=I; + + + + if(Rang[I]==Rang[J]) + + { + + TT[I]=J; + + Rang[J]++; + + } + +} + +int main() + +{ + + f>>n>>m; + + + + for(int i=1; i<=n; i++) + + { + + TT[i]=i; + + Rang[i]=1; + + } + + + + for(int i=1; i<=m; i++) + + f>>V[i].x>>V[i].y>>V[i].cost; + + + + sort(V+1,V+m+1,compare); + + + + for(int i=1; i<=m; i++) + + { + + int Tata_x=Find(V[i].x); + + int Tata_y=Find(V[i].y); + + + + if(Tata_x!=Tata_y) + + { + + Unire(Tata_x,Tata_y); + + P[++cnt].first=V[i].x; + + P[cnt].second=V[i].y; + + total+=V[i].cost; + + + + } + + + + } + + + + g< +#include +#include +#include + +using namespace std; + +struct edge { + int x, y, c; +}; + +pair> readGraph() { + ifstream fin("apm.in"); + int n, m; + vector edges; + + fin >> n >> m; + int x, y, c; + while (m--) { + fin >> x >> y >> c; + edges.push_back((edge) {x, y, c}); + } + + return make_pair(n, edges); +} + +int dsuRoot(int node, const vector &parent) { + while (parent[node]) + node = parent[node]; + return node; +} + +pair> kruskal(int n, vector &edges) { + int cost = 0; + vector selEdges; + vector parent(n + 1, 0); + vector height(n + 1, 0); + + sort(edges.begin(), edges.end(), [](edge &a, edge &b) { + return a.c < b.c; + }); + + int last = -1, root1, root2; + + for (int i = 1; i < n; i++) { + do { + last++; + root1 = dsuRoot(edges[last].x, parent); + root2 = dsuRoot(edges[last].y, parent); + } while (root1 == root2); + + selEdges.push_back(last); + cost += edges[last].c; + + + if (height[root1] < height[root2]) + parent[root1] = root2; + else if (height[root1] > height[root2]) + parent[root2] = root1; + else { + parent[root2] = root1; + height[root1]++; + } + } + + return make_pair(cost, selEdges); +} + +int main() { + ofstream fout("apm.out"); + + int n; + vector edges; + tie(n, edges) = readGraph(); + + int cost; + vector selectedEdges; + tie(cost, selectedEdges) = kruskal(n, edges); + + fout << cost << '\n' << n - 1; + for (const int e : selectedEdges) + fout << '\n' << edges[e].x << " " << edges[e].y; + return 0; +} diff --git a/mst/kruskal/4a60195f-7b86-4a8b-b1a8-677703ee4af4.cpp b/mst/kruskal/4a60195f-7b86-4a8b-b1a8-677703ee4af4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66a5f69b9b101aafd16816b7348f1e834a5abddb --- /dev/null +++ b/mst/kruskal/4a60195f-7b86-4a8b-b1a8-677703ee4af4.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +pair < int , int > P[400005]; +int n, m, total, TT[400005], k; + +struct Edge { + int x, y, c; +}V[400005]; + +bool fcmp (Edge a, Edge b) { + return a.c < b.c; +} + +void Read () { + fin >> n >> m; + for (int i = 1; i <= m; ++i) fin >> V[i].x >> V[i].y >> V[i].c; + sort (V + 1, V + m + 1, fcmp); +} + +int Find (int i) { + if (TT[i] == i) + return i; + return TT[i] = Find(TT[i]); +} + +void Unite (int i, int j) { + int ti, tj; + ti = Find(i); + tj = Find(j); + TT[tj] = ti; +} + +void Solve () { + for (int i = 1; i <= m; ++i) + TT[i] = i; + for (int i = 1; i <= m; ++i) { + int xx = Find (V[i].x), yy = Find (V[i].y); + if (xx != yy) { + Unite (xx, yy); + P[++k] = {V[i].x, V[i].y}; + total += V[i].c; + } + } +} + +void Print () { + fout << total << '\n' << n - 1 << '\n'; + for (int i = 1; i <= k; ++i) fout << P[i].first << ' ' << P[i].second << ' ' << '\n'; +} + +int main() { + Read (); + Solve (); + Print (); + return 0; +} diff --git a/mst/kruskal/4b8df485-e79e-42a3-99be-4aac3010df7c.cpp b/mst/kruskal/4b8df485-e79e-42a3-99be-4aac3010df7c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a73356a1c8bf37615fdaf41057355f317275a63 --- /dev/null +++ b/mst/kruskal/4b8df485-e79e-42a3-99be-4aac3010df7c.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +struct ura{ + +int x, y, c; + +}v[400001], sol[200001]; + + + +using namespace std; + +bool cmp(ura a, ura b) { + + return a.c>n>>m; + + for(i=1;i<=m;i++) { + + fin>>v[i].x>>v[i].y>>v[i].c; + + } + + for(i=1;i<=n;i++) { + + sef[i]=i; + + } + + sort(v+1, v+m+1, cmp); + + s=0; + + k=1; + + for(i=1;i<=m && k<=n-1;i++) { + + if(sefsup(v[i].x)!=sefsup(v[i].y)) { + + sol[k]=v[i]; + + unire(v[i].x, v[i].y); + + s=s+v[i].c; + + k++; + + } + + } + + fout< + + +using namespace std; + +ifstream f("apm.in"); +ofstream g("apm.out"); + + +class DisjointSets { +private: + vector parent; + vector rank; + +public: + DisjointSets(int n) { + for (int i = 0; i < n; i++) + make_set(i); + } + + ~DisjointSets() { + parent.clear(); + rank.clear(); + } + + void make_set(int x) { + parent.push_back(x); + rank.push_back(0); + } + + int find_set(int x) { + vector queue; + while (parent[x] != x) { + queue.push_back(x); + x = parent[x]; + } + + for (int it: queue) + parent[it] = x; + + queue.clear(); + + return x; + } + + void union_set(int x, int y) { + int rx = find_set(x); + int ry = find_set(y); + + if (rank[rx] <= rank[ry]) { + parent[rx] = ry; + rank[rx] == rank[ry] ? rank[ry]++ : false; + } + else + parent[ry] = rx; + } + + void show() { + for (int i = 0; i < (int)parent.size(); i++) + cout << i << " " << parent[i] << " " << rank[i] << endl; + + } +}; + + +struct edge { + int u, v, eticheta; +}; + +vector edges; + +bool comparator(edge x, edge y) { + return x.eticheta < y.eticheta; +} + +void read(vector &edges, int &n, int &m) { + f >> n >> m; + for (int i = 0; i < m; i++) { + edge e; + f >> e.u >> e.v >> e.eticheta; + e.u--; + e.v--; + edges.push_back(e); + } +} + +void showEdges(vector edges) { + for (auto it: edges) + cout << it.u << " " << it.v << " " << it.eticheta << "\n"; +} + +int main() { + int n, m; + vector edges; + + read(edges, n, m); + + sort(edges.begin(), edges.end(), comparator); + + DisjointSets sets(n); + + long long min = 0; + vector> answer; + + for (auto it: edges) { + if (sets.find_set(it.u) != sets.find_set(it.v)) { + min += it.eticheta; + sets.union_set(it.u, it.v); + answer.push_back({it.u, it.v}); + } + } + + g << min << "\n"; + + g << answer.size() << "\n"; + for (auto it: answer) + g << it.second + 1 << " " << it.first + 1 << "\n"; + + return 0; +} diff --git a/mst/kruskal/4bc70eef-50bd-4a6a-917c-86779f33312f.cpp b/mst/kruskal/4bc70eef-50bd-4a6a-917c-86779f33312f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d4553fa88b2154004c5ef3c834505d5a3c71121 --- /dev/null +++ b/mst/kruskal/4bc70eef-50bd-4a6a-917c-86779f33312f.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +struct muchie{int n1, n2, cost;}; + + + +const int NMAX = 200005; + +const int MMAX = 400005; + + + +int sef[NMAX]; + +muchie vm[MMAX], sol[NMAX]; + + + +bool cmp(muchie a, muchie b) { + + return a.cost < b.cost; + +} + + + +int sefsuprem (int x) { + + if (sef[x] == x) + + return x; + + else + + return sef[x] = sefsuprem(sef[x]); + +} + + + +void unire(int x, int y) { + + int sefx = sefsuprem(x); + + int sefy = sefsuprem(y); + + sef[sefx] = sefy; + +} + + + +int main() { + + + + freopen ("apm.in", "r", stdin); + + freopen ("apm.out", "w", stdout); + + + + int n, m, i, adaug, sum; + + + + scanf ("%d%d", &n, &m); + + + + for (i = 1; i <= m; i++) + + scanf ("%d%d%d", &vm[i].n1, &vm[i].n2, &vm[i].cost); + + + + sort(vm + 1, vm + m + 1, cmp); + + + + for (i = 1; i <= n; i++) + + sef[i] = i; + + + + adaug = 0; + + i = 1; + + sum = 0; + + while (adaug < n - 1) { + + if (sefsuprem(vm[i].n1) != sefsuprem(vm[i].n2)) { + + + + unire(vm[i].n1, vm[i].n2); + + sum += vm[i].cost; + + adaug++; + + sol[adaug].n1 = vm[i].n1; + + sol[adaug].n2 = vm[i].n2; + + } + + i++; + + } + + printf ("%d\n%d\n", sum, adaug); + + for (i = 1; i <= adaug; i++) + + printf ("%d %d\n", sol[i].n1, sol[i].n2); + + + + + + + + return 0; + +} diff --git a/mst/kruskal/4c1baaa6-7411-459b-bdf3-7e0c59fc4409.cpp b/mst/kruskal/4c1baaa6-7411-459b-bdf3-7e0c59fc4409.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e33f844c92faf639665056ee70c4c9d0f0228cb --- /dev/null +++ b/mst/kruskal/4c1baaa6-7411-459b-bdf3-7e0c59fc4409.cpp @@ -0,0 +1,129 @@ +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n , m; + + + +int parent[200005]; + +int previous[200005]; + + + +struct redge + +{ + + int a , b; + +}; + +vectorrez; + + + +struct edge + +{ + + int a , b , d; + +}; + + + +bool xort(edge a , edge b) + +{ + + if(a.d < b.d) + + return true; + + return false; + +} + + + +vectoredges; + + + +int Find(int node) + +{ + + if(node == parent[node]) + + return node; + + return parent[node] = Find(parent[node]); + +} + + + +int main() + +{ + + int a , b , d , price = 0; + + in >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + in >> a >> b >> d; + + edges.push_back({a , b , d}); + + } + + sort(edges.begin(),edges.end(),xort); + + for(int i = 1; i <= n; ++i) + + parent[i] = i; + + for(auto &i : edges) + + { + + if(Find(i.a) != Find(i.b)) + + { + + parent[Find(i.b)] = i.a; + + price += i.d; + + rez.push_back({i.a ,i.b}); + + } + + } + + out << price << '\n' << n - 1 << '\n'; + + for(auto &i : rez) + + out << i.a << " " << i.b << '\n'; + + + +} diff --git a/mst/kruskal/50880e6a-0284-4db3-8b02-8df7df16fce7.cpp b/mst/kruskal/50880e6a-0284-4db3-8b02-8df7df16fce7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c67f1a04b96abf455b6036eb0447aca0aeced4a1 --- /dev/null +++ b/mst/kruskal/50880e6a-0284-4db3-8b02-8df7df16fce7.cpp @@ -0,0 +1,206 @@ +#include +#include +#include +using namespace std; + +using pii = pair; + +int N, M; + +vector parent, tree_size; + +vector> adj; + + + + + +void make_set(const int &vertex) + +{ + + parent[vertex] = vertex; + + tree_size[vertex] = 1; + +} + + + + + +int find_leader(const int &vertex) + +{ + + if (parent[vertex] == vertex) + + { + + return vertex; + + } + + + + return parent[vertex] = find_leader(parent[vertex]); + +} + + + + + +void unite_sets(int &set1, int &set2) + +{ + + set1 = find_leader(set1); + + set2 = find_leader(set2); + + + + if (set1 != set2) + + { + + if (tree_size[set1] < tree_size[set2]) + + { + + swap(set1, set2); + + } + + + + tree_size[set1] += tree_size[set2]; + + parent[set2] = set1; + + } + +} + + + + + +void Kruskal() + +{ + + vector MST_edges; + + int total_weight = 0; + + + + for (int node = 1; node <= N; ++node) + + { + + make_set(node); + + } + + + + sort(begin(adj), end(adj), greater>()); + + + + for (int i = 1; i < N; ++i) + + { + + int CC1, CC2, leader1, leader2; + + + + do + + { + + CC1 = adj.back().second.first; + + CC2 = adj.back().second.second; + + leader1 = find_leader(CC1); + + leader2 = find_leader(CC2); + + adj.pop_back(); + + } while (leader1 == leader2); + + + + total_weight += adj.back().first; + + MST_edges.push_back({CC1, CC2}); + + unite_sets(leader1, leader2); + + } + + + + ofstream fout("apm.out"); + + + + fout << total_weight << "\n" << N - 1 << "\n"; + + for (const pii &MST_edge : MST_edges) + + { + + fout << MST_edge.first << " " << MST_edge.second << "\n"; + + } + +} + + + + + +int main() + +{ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + parent.resize(N + 1); + + tree_size.resize(N + 1); + + + + while (M--) + + { + + int i, j, weight; + + fin >> i >> j >> weight; + + + + adj.push_back({weight, {i, j}}); + + adj.push_back({weight, {j, i}}); + + } + + + + Kruskal(); + +} diff --git a/mst/kruskal/511dd1c0-fd64-45f1-ba9e-fa180ac82ab4.cpp b/mst/kruskal/511dd1c0-fd64-45f1-ba9e-fa180ac82ab4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..643a9370b28c07b103c3ba2bebf7d4ff252b7f23 --- /dev/null +++ b/mst/kruskal/511dd1c0-fd64-45f1-ba9e-fa180ac82ab4.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +using namespace std; + + + +struct MUCHIE + +{ + + int u,v,c,sel; + +}; + + + +vectorv; + +vectort,h; + + + +int FindSet(int x) + +{ + + while(x!=t[x]) + + x=t[x]; + + return x; + +} + + + +void UnionSet(int x,int y) + +{ + + if(h[x]==h[y]) + + { + + t[y]=x; + + h[x]++; + + } + + else if(h[x]>h[y]) + + t[y]=x; + + else + + t[x]=y; + +} + + + +bool cmp(MUCHIE a, MUCHIE b) + +{ + + return a.c +#include +using namespace std; + + + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +struct much +{ + int a, b, c; +} v[400001]; + +bool comp(much a, much b) +{ + return a.c < b.c; +} + +bool ok[400001]; + +int h[200001], t[200001]; + +void comb(int x, int y) +{ + if (h[x] < h[y]) + t[x] = y; + else + { + t[y] = x; + if (h[x] == h[y]) + h[x]++; + } +} + +int find(int x) +{ + int r, y; + r = x; + while (t[r]) + r = t[r]; + while (x != r) + { + y = t[x]; + t[x] = r; + x = y; + } + return r; +} + +int main() +{ + int n, m, i, x, y; + int nrmuch = 0; + int cost = 0; + fin >> n >> m; + for (i = 1; i<=m; i++) + fin >> v[i].a >> v[i].b >> v[i].c; + sort(v+1, v+m+1, comp); + for (i = 1; nrmuch < n-1; i++) + { + x = find(v[i].a); + y = find(v[i].b); + if (x != y) + { + ok[i] = 1; + cost += v[i].c; + nrmuch++; + comb(x, y); + } + } + fout << cost << '\n' << n-1 << '\n'; + for (i = 1; i<=m; i++) + if (ok[i]) + fout << v[i].a << ' ' << v[i].b << '\n'; + return 0; +} diff --git a/mst/kruskal/51f7921d-cf1a-4235-a701-4c572673b16a.cpp b/mst/kruskal/51f7921d-cf1a-4235-a701-4c572673b16a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7fbef8b3677ad8575817fad03680adb83ad5f6c --- /dev/null +++ b/mst/kruskal/51f7921d-cf1a-4235-a701-4c572673b16a.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector t; + + + +int find(int x) + +{ + + if (t[x] == 0) + + return x; + + t[x] = find(t[x]); + + return t[x]; + +} + + + +void unite(int x, int y) + +{ + + int tx = find(x); + + int ty = find(y); + + t[tx] = ty; + +} + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + + + vector< pair> > muchii; + + muchii.resize(m); + + t.resize(n + 1); + + for (int i = 0; i < m; i++) + + { + + int c, x, y; + + fin >> x >> y >> c; + + muchii[i] = { c, {x, y} }; + + } + + + + sort(muchii.begin(), muchii.end()); + + + + int costArb = 0; + + vector> arbore; + + for (auto muchie : muchii) + + { + + int x = muchie.second.first; + + int y = muchie.second.second; + + int cost = muchie.first; + + + + if (find(x) != find(y)) + + { + + arbore.push_back({ x, y }); + + costArb += cost; + + + + unite(x, y); + + } + + } + + + + fout << costArb << '\n' << arbore.size() << '\n'; + + for (auto muchie : arbore) + + fout << muchie.first << ' ' << muchie.second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/523ff693-64f1-4c39-aae4-b53aecd20da7.cpp b/mst/kruskal/523ff693-64f1-4c39-aae4-b53aecd20da7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..05f2d178c89ce7d9db3bc7faf9195701a3ff0b12 --- /dev/null +++ b/mst/kruskal/523ff693-64f1-4c39-aae4-b53aecd20da7.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int Dim = 4e5 + 5; + + + +struct edge { + + int x, y, cost; + + bool operator <(const edge &other) { + + return cost < other.cost; + + } + +}; + + + +int n, m, costTotal; + +int tata[Dim], w[Dim]; + + + +edge e[Dim]; + + + +vector > ans; + + + +int findTata(int node) { + + int cn = node; + + while(tata[cn] != cn) { + + cn = tata[cn]; + + } + + + + while(node != cn) { + + int aux = node; + + tata[node] = cn; + + node = tata[aux]; + + } + + return node; + +} + + + +void uneste(int node1, int node2) { + + int tNode1 = findTata(node1); + + int tNode2 = findTata(node2); + + if(w[tNode1] < w[tNode2]) { + + tata[tNode1] = tNode2; + + w[tNode2] += w[tNode1]; + + } else { + + tata[tNode2] = tNode1; + + w[tNode1] += w[tNode2]; + + } + +} + + + +int main() { + + f >> n >> m; + + for(int i = 1; i <= m; ++i) { + + f >> e[i].x >> e[i].y >> e[i].cost; + + } + + + + sort(e + 1, e + m + 1); + + for(int i = 1; i <= n; ++i) { + + w[i] = 1; + + tata[i] = i; + + } + + + + for(int i = 1; i <= m; ++i) { + + if(findTata(e[i].x) != findTata(e[i].y)) { + + uneste(e[i].x, e[i].y); + + ans.push_back({e[i].x, e[i].y}); + + costTotal += e[i].cost; + + } + + } + + + + g << costTotal << '\n' << n - 1 << '\n'; + + for(auto it = ans.begin(); it != ans.end(); ++it) { + + g << it -> first << " " << it -> second << '\n'; + + } + + return 0; + +} diff --git a/mst/kruskal/524ea52f-135a-46f6-8c06-e6d26f829e4f.cpp b/mst/kruskal/524ea52f-135a-46f6-8c06-e6d26f829e4f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8d08e8e502e7326110ae3cc2ec7dc5cf5c5f66 --- /dev/null +++ b/mst/kruskal/524ea52f-135a-46f6-8c06-e6d26f829e4f.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout("apm.out"); + + + + + +typedef pair iPair; + + + +struct Graph { + + int V, E; + + vector< pair> edges; + + vector> kruskalPath; + + + + void addEdge(int u, int v, int w) { + + edges.push_back({ w, {u, v} }); + + } + + int kruskalMST(); + + void readKruskal(); + +}; + + + + + +void Graph::readKruskal() { + + + + fin >> V >> E; + + + + int edge1, edge2, cost; + + for (int i = 0; i < E; i++) { + + fin >> edge1>> edge2>>cost; + + addEdge(edge1, edge2, cost); + + } + +} + + + + + +struct DisjointSets { + + int* parent, * rnk; + + int n; + + DisjointSets(int n) { + + this->n = n; + + parent = new int[n + 1]; + + rnk = new int[n + 1]; + + for (int i = 0; i <= n; i++) { + + rnk[i] = 0; + + parent[i] = i; + + } + + } + + int find(int u) { + + if (u != parent[u]) + + parent[u] = find(parent[u]); + + return parent[u]; + + } + + void merge(int x, int y) { + + x = find(x), y = find(y); + + if (rnk[x] > rnk[y]) + + parent[y] = x; + + else + + parent[x] = y; + + if (rnk[x] == rnk[y]) + + rnk[y]++; + + } + +}; + +int Graph::kruskalMST() { + + int mst_wt = 0; + + + + sort(edges.begin(), edges.end()); + + + + DisjointSets ds(V); + + + + vector< pair >::iterator it; + + for (it = edges.begin(); it != edges.end(); it++) { + + + + int u = it->second.first; + + + + int v = it->second.second; + + + + int set_u = ds.find(u); + + int set_v = ds.find(v); + + + + if (set_u != set_v) { + + kruskalPath.push_back({ u,v }); + + mst_wt += it->first; + + ds.merge(set_u, set_v); + + } + + } + + return mst_wt; + +} + +int main() { + + Graph g; + + g.readKruskal(); + + + + int mst_wt = g.kruskalMST(); + + fout << mst_wt << "\n" << g.kruskalPath.size() << "\n"; + + for (vector>::iterator it = g.kruskalPath.begin(); it != g.kruskalPath.end(); it++) { + + fout << it->first << " " << it->second << "\n"; + + } + + + + return 0; + +} diff --git a/mst/kruskal/5277a15f-c1d9-4e0f-926b-5893f6b28be2.cpp b/mst/kruskal/5277a15f-c1d9-4e0f-926b-5893f6b28be2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6be992ec6d767eb79b8b7fc5f8c2861d8dd95bb --- /dev/null +++ b/mst/kruskal/5277a15f-c1d9-4e0f-926b-5893f6b28be2.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int MMAX = 4e5 + 5; + +const int NMAX = 1e5 + 5; + + + +struct edge { + + int x, y, c; + +}; + +edge e[MMAX]; + +int h[NMAX], dad[NMAX], cnt, ans[NMAX], sum; + + + +bool cmp(const edge &a, const edge &b) { + + return a.c < b.c; + +} + + + +void Union(int x, int y) { + + if(h[x] > h[y]) { + + dad[y] = x; + + } else { + + dad[x] = y; + + if(h[x] == h[y]) { + + h[y]++; + + } + + } + +} + + + +int Find(int x) { + + int root = x; + + while(dad[root] != root) { + + root = dad[root]; + + } + + int y = x; + + while(y != root) { + + int t = dad[y]; + + dad[y] = root; + + y = t; + + } + + return root; + +} + + + +int main() + +{ + + int n, m; + + f >> n >> m; + + for(int i = 1; i <= m; i++) { + + f >> e[i].x >> e[i].y >> e[i].c; + + } + + sort(e + 1, e + m + 1, cmp); + + for(int i = 1; i <= n; i++) { + + dad[i] = i; + + h[i] = 1; + + } + + for(int i = 1; i <= m && cnt < n - 1; i++) { + + int p = Find(e[i].x); + + int q = Find(e[i].y); + + if(p != q) { + + cnt++; + + sum += e[i].c; + + ans[cnt] = i; + + Union(p, q); + + } + + } + + g << sum << '\n'; + + g << n - 1 << '\n'; + + for(int i = 1; i <= n - 1; i++) { + + g << e[ans[i]].x << ' ' << e[ans[i]].y << '\n'; + + } + + return 0; + +} diff --git a/mst/kruskal/52a581b5-11f1-4e98-8479-84bcfc35ef40.cpp b/mst/kruskal/52a581b5-11f1-4e98-8479-84bcfc35ef40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f4715a1ed760299b1239ba041b55e58eb9f4fc6 --- /dev/null +++ b/mst/kruskal/52a581b5-11f1-4e98-8479-84bcfc35ef40.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; +struct muchii { + int x,y,cost; + bool operator < (const muchii &a) const { + return cost sol[200003]; +int find_root (int x) { + if(x==tata[x]) + return x; + else { + tata[x]=find_root(tata[x]); + return tata[x]; + } +} +void apeme () { + for(int i=1;i<=n;++i) + tata[i]=i; + for(int i=1;i<=m;++i) { + nod=find_root(v[i].x);nod1=find_root(v[i].y); + if(nod!=nod1) + suma+=v[i].cost,sol[++kontor]={v[i].x,v[i].y},tata[nod]=nod1; + } +} +int main () { + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + scanf("%d%d", &n, &m); + for(int i=1;i<=m;++i) + scanf("%d%d%d", &v[i].x, &v[i].y, &v[i].cost); + sort(v+1,v+m+1);apeme(); + printf("%d\n%d\n", suma, kontor); + for(int i=1;i<=kontor;++i) + printf("%d %d\n", sol[i].first, sol[i].second); + return 0; +} diff --git a/mst/kruskal/532dc270-9da8-41b1-a98d-45d99d960121.cpp b/mst/kruskal/532dc270-9da8-41b1-a98d-45d99d960121.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a73ec684c635a35173ac761fe14defa514dc1f2a --- /dev/null +++ b/mst/kruskal/532dc270-9da8-41b1-a98d-45d99d960121.cpp @@ -0,0 +1,254 @@ +#include + + + + + + + +using namespace std; + ifstream fffin("apm.in"); + + ofstream fffout("apm.out"); + + + + + + + +const bool MULTIPLE_CASES = 0; + +const bool PRINT_CASE_MSG = 0; + + + +const int DOUBLE_PRECISION = 6; + + + + + +using i64 = long long; + +using pii = pair; + +using Graph = vector>; + + + + + +const int INF = (1 << 30); + +const int NMAX = 1e5 + 3; + + + +struct CostGraph + +{ + + using Edge = pair; + + using Graph = vector>; + + + + int N; + + Graph mGraph; + + vector> mEdges; + + + + CostGraph(int n) : N(n), mGraph(n + 1) + + { } + + + + void insertEdge(int from, Edge to) + + { + + assert(1 <= from && from <= N); + + assert(1 <= to.first && to.first <= N); + + + + mGraph[from].push_back(to); + + mEdges.push_back({from, to}); + + } + + + + int getAPM(vector>& actualAPM) + + { + + actualAPM.clear(); + + vector tt(N + 1), gr(N + 1, 1); + + + + function ROOT = [&](int nod) { + + if (tt[nod] == nod) + + return nod; + + return tt[nod] = ROOT(tt[nod]); + + }; + + + + function UNION = [&](int x, int y) { + + x = ROOT(x), y = ROOT(y); + + if ( x == y ) + + return false; + + if (gr[x] < gr[y]) + + swap(x, y); + + + + gr[x] += gr[y]; + + tt[y] = x; + + return true; + + }; + + + + iota(tt.begin(), tt.end(), 0); + + sort(mEdges.begin(), mEdges.end(), [](const pair& a, const pair& b){ + + return a.second.second < b.second.second; + + }); + + + + int apmCost = 0; + + for (auto mED : mEdges) { + + int from = mED.first; + + int to = mED.second.first; + + int cost = mED.second.second; + + if (UNION(from, to)) + + apmCost += cost, + + actualAPM.push_back({from, {to, cost}}); + + } + + return apmCost; + + } + +}; + + + +void solve() + +{ + + int N, M; + + cin >> N >> M; + + CostGraph graph(N); + + for (int i = 1; i <= M; ++i) { + + int x,y,c; + + cin >> x >> y >> c; + + graph.insertEdge(x, {y, c}); + + } + + vector> edges; + + cout << graph.getAPM(edges) << '\n'; + + cout << edges.size() << '\n'; + + for (auto x : edges) { + + cout << x.first << ' ' << x.second.first << '\n'; + + } + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(0); + + cin.tie(0); + + cout.tie(0); + + + + + + cin.rdbuf(fffin.rdbuf()); + + cout.rdbuf(fffout.rdbuf()); + if (DOUBLE_PRECISION > 0) + + cout << setprecision(DOUBLE_PRECISION) << fixed; + + + + auto testMessage = [](int id) { + + return "Case #" + to_string(id) + ": "; + + }; + + + + int T = 1; + + if (MULTIPLE_CASES) + + cin >> T; + + + + for (int t = 1; t <= T; ++t) { + + cout << (PRINT_CASE_MSG ? testMessage(t) : ""); + + solve(); + + } +} diff --git a/mst/kruskal/5384c9e2-a8e0-4906-ab9f-6da4f32e1601.cpp b/mst/kruskal/5384c9e2-a8e0-4906-ab9f-6da4f32e1601.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a2c4ad6d800c7ca281e2c4e3d0314c090fbdd53 --- /dev/null +++ b/mst/kruskal/5384c9e2-a8e0-4906-ab9f-6da4f32e1601.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int NMax = 400005; + +pair P[NMax]; + +int N, M, Total, TT[NMax], k, RG[NMax]; + +struct Edge + +{ + + int x,y,c; + +}V[NMax]; + +bool Compare(Edge a, Edge b) + +{ + + return a.c < b.c; + +} + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + fin >> V[i].x >> V[i].y >> V[i].c; + + sort(V+1, V+M+1, Compare); + + for(int i = 1; i <= M; i++) + + cout << V[i].x << " " << V[i].y << " " << V[i].c << "\n"; + +} + +int Find(int nod) + +{ + + while(TT[nod] != nod) + + nod = TT[nod]; + + return nod; + +} + +void Unite(int x, int y) + +{ + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[y] < RG[x]) + + TT[y] = x; + + if(RG[x] == RG[y]) + + { + + TT[x]=y; + + RG[y]++; + + } + +} + +void Solve() + +{ + + for(int i=1;i<=M;i++) + + { + + + + if(Find(V[i].x) != Find(V[i].y)) + + { + + + + Unite(Find(V[i].x), Find(V[i].y)); + + P[++k].first = V[i].x; + + P[k].second = V[i].y; + + Total += V[i].c; + + } + + } + +} + +int main() + +{ + + Read(); + + for(int i = 1; i <= M; i++) + + { + + TT[i]=i; + + RG[i]=1; + + } + + Solve(); + + fout << Total << "\n"; + + fout << N-1 <<"\n"; + + for(int i = 1; i <= k; i++) + + fout << P[i].first << " " << P[i].second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/5450ccf2-3609-4086-8358-f185fb46d10a.cpp b/mst/kruskal/5450ccf2-3609-4086-8358-f185fb46d10a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd35f056f7abd5699edf52222f58cd2a9e3f53d2 --- /dev/null +++ b/mst/kruskal/5450ccf2-3609-4086-8358-f185fb46d10a.cpp @@ -0,0 +1,123 @@ +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +struct elem + +{ + + int x,y,c; + +}; + +elem v[400002],sol[200002]; + + + +inline bool cmp(const elem &a,const elem &b) + +{ + + return a.c>n>>m; + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for(i=1;i<=n;i++) + + tata[i]=i,dim[i]=1; + + for(i=1;i<=m&&k + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +const int MAX=200001; + +int n,m,tata[MAX],cnt,cost; + +bool sel[MAX]; + +struct muchie + +{ + + int x,y,c; + +}v[MAX]; + + + +bool cmp(muchie a, muchie b) + +{ + + return a.c>n>>m; + + for(int i=1;i<=n;i++) tata[i]=i; + + for(int i=1;i<=m;i++) in>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + + + for(int i=1;i<=m;i++) + + { + + if(radacina(v[i].x)!=radacina(v[i].y)) + + { + + sel[i]=true; + + cnt++; + + cost+=v[i].c; + + tata[radacina(v[i].x)]=radacina(v[i].y); + + + + } + + } + + + + out< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +pair P[400005]; + + + +int N, M, k, RG[400005], TT[400005], total; + + + +struct Edge { + + int x, y, c; + +}V[400005]; + + + +bool cmp(Edge a, Edge b) { + + return a.c < b.c; + +} + + + +void Read() { + + fin >> N >> M; + + + + for (int i = 1; i <= M; ++i) + + fin >> V[i].x >> V[i].y >> V[i].c; + + + + sort(V + 1, V + M + 1, cmp); + +} + + + +int Find(int nod) { + + while (TT[nod] != nod) + + nod = TT[nod]; + + + + return nod; + +} + + + +void Unite(int x, int y) { + + if (RG[x] < RG[y]) + + TT[x] = y; + + if (RG[y] < RG[x]) + + TT[y] = x; + + if (RG[x] == RG[y]) { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() { + + for (int i = 1; i <= M; ++i) { + + if (Find(V[i].x) != Find(V[i].y)) { + + Unite(Find(V[i].x), Find(V[i].y)); + + P[++k].first = V[i].x; + + P[k].second = V[i].y; + + total += V[i].c; + + } + + } + +} + + + +int main() { + + + + Read(); + + + + for (int i = 1; i <= M; i++) + + { + + TT[i] = i; + + RG[i] = 1; + + } + + + + Solve(); + + + + fout << total << "\n"; + + fout << N - 1 << "\n"; + + for (int i = 1; i <= k; i++) + + fout << P[i].first << " " << P[i].second << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/566d1401-ab48-4b95-a218-1845df882d39.cpp b/mst/kruskal/566d1401-ab48-4b95-a218-1845df882d39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..562f8c5bc3946aef022b7efc15fb3b8ba40d2238 --- /dev/null +++ b/mst/kruskal/566d1401-ab48-4b95-a218-1845df882d39.cpp @@ -0,0 +1,166 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int NMax = 400005; + +int n, m, t[NMax], rang[NMax], k, total; + + + +pair muchii_ramase[NMax]; + + + +struct Muchie{ + + int x, y, cost; + +}muchii[NMax]; + + + +bool compara(Muchie a, Muchie b) + +{ + + return a.cost < b.cost; + +} + + + +void citire() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + fin >> muchii[i].x >> muchii[i].y >> muchii[i].cost; + +} + + + +int radacina(int k) + +{ + + if(t[k] == 0) + + return k; + + else + + { + + int x = radacina(t[k]); + + t[k] = x; + + return x; + + } + +} + + + +void unire(int x, int y) + +{ + + if(rang[x] < rang[y]) + + t[x] = y; + + else + + { + + t[y] = x; + + if(rang[x] == rang[y]) + + rang[x]++; + + } + +} + + + +void kruskal() + +{ + + for(int i = 1; i <= m; i++) + + { + + int tata_x = radacina(muchii[i].x); + + int tata_y = radacina(muchii[i].y); + + if(tata_x != tata_y) + + { + + unire(tata_x, tata_y); + + + + muchii_ramase[++k].first = muchii[i].x; + + muchii_ramase[k].second = muchii[i].y; + + total += muchii[i].cost; + + } + + } + +} + + + +void afisare() + +{ + + fout << total << '\n' << n - 1 << '\n'; + + for(int i = 1; i <= k; i++) + + fout << muchii_ramase[i].first << ' ' << muchii_ramase[i].second << '\n'; + +} + + + +int main() + +{ + + citire(); + + sort(muchii + 1, muchii + m + 1, compara); + + kruskal(); + + afisare(); + +} diff --git a/mst/kruskal/5710c393-ed0b-4cde-a218-d1d244ee5df0.cpp b/mst/kruskal/5710c393-ed0b-4cde-a218-d1d244ee5df0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67ef47dd500ee38a0f94ed54a3ea352407905db1 --- /dev/null +++ b/mst/kruskal/5710c393-ed0b-4cde-a218-d1d244ee5df0.cpp @@ -0,0 +1,76 @@ +#include + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +const int MAX_N = 200000; + +struct Edge { + int c, u, v; +}; + +int n, m; +int tata[MAX_N + 5], cnt[MAX_N + 5]; + +vector edges; +vector > ans; + +int sum; + +inline bool cmp(const Edge &a, const Edge &b) { + return a.c < b.c; +} + +int root(int x) { + if (tata[x] == x) + return x; + return tata[x] = root(tata[x]); +} + +void join(int x, int y){ + x = root(x); + y = root(y); + + if(cnt[x] > cnt[y]) + swap(x, y); + + cnt[y] += cnt[x]; + tata[x] =y; +} + +int main() { + int n, m; + fin >> n >> m; + for (int i = 1; i <= m; i++) { + int a, b, c; + fin >> a >> b >> c; + edges.push_back({c, a, b}); + } + + sort(edges.begin(), edges.end(), cmp); + + for (int i = 1; i <= n; i++) { + tata[i] = i; + cnt[i] = 1; + } + + for (int i = 0; i < m; i++) { + int a = edges[i].u; + int b = edges[i].v; + int c = edges[i].c; + + if (root(a) == root(b)) + continue; + + join(a, b); + sum += c; + ans.push_back({a, b}); + } + + fout << sum << '\n' << ans.size() << '\n'; + for (auto i : ans) + fout << i.first << ' ' << i.second << '\n'; + return 0; +} diff --git a/mst/kruskal/576ffc6b-ffc2-425b-8c15-6b60a30d99dd.cpp b/mst/kruskal/576ffc6b-ffc2-425b-8c15-6b60a30d99dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b22e537d4d41500e2c5c7d325fe9834d1f2d1f3f --- /dev/null +++ b/mst/kruskal/576ffc6b-ffc2-425b-8c15-6b60a30d99dd.cpp @@ -0,0 +1,237 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, cost; + + + +class Edge + +{ + +public: + + int source, destination, weight; + +}edges[400010]; + + + + + +class Subset + +{ + +public: + + int parent, rank; + +}; + + + + + +bool cmp(Edge x, Edge y) + +{ + + return x.weight < y.weight; + +} + + + + + +void Read() + +{ + + int i; + + f>>n>>m; + + for(i = 0; i < m; i++) + + { + + int x,y,z; + + f>>x>>y>>z; + + edges[i].source = x; + + edges[i].destination = y; + + edges[i].weight = z; + + } + +} +int Find(Subset subsets[], int i) + +{ + + if(subsets[i].parent != i) + + subsets[i].parent = Find(subsets, subsets[i].parent); + + + + return subsets[i].parent; + +} + + + +void Union(Subset subsets[], int x, int y) + +{ + + int xSet = Find(subsets, x); + + int ySet = Find(subsets, y); + + + + if(subsets[xSet].rank < subsets[ySet].rank) + + subsets[xSet].parent = ySet; + + else if (subsets[xSet].rank > subsets[ySet].rank) + + subsets[ySet].parent = xSet; + + else + + { + + subsets[ySet].parent = xSet; + + subsets[xSet].rank++; + + } + +} + + + +void Kruskal(Edge edges[]) + +{ + + int count = 0, nr = 0; + + Edge result[400010]; + + Subset subsets[400010]; + + + + sort(edges, edges + m, cmp); + + for(int i = 0; i < n; i++) + + { + + subsets[i].parent = i; + + subsets[i].rank = 0; + + } + + + + while(count < n - 1 && nr < m) + + { + + Edge next = edges[nr++]; + + int x = Find(subsets, next.source); + + int y = Find(subsets, next.destination); + + + + if(x != y) + + { + + result[count++] = next; + + Union(subsets, x, y); + + } + + } + + + + + + + + for(int i = 0; i < count; i++) + + { + + + + cost += result[i].weight; + + } + + g< + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +vector>> e; + +vector > rez; + +int n,m,cost,t[200005],rang[200005]; + +int reprezentant(int x) + +{ + + if(t[x]!=x) + + { + + return reprezentant(t[x]); + + } + + return t[x]; + +} + +bool uneste(int x, int y) + +{ + + x=reprezentant(x); + + y=reprezentant(y); + + if(x==y) + + { + + return false; + + } + + if(rang[x]>rang[y]) + + { + + t[y]=x; + + rang[x]+=rang[y]; + + rang[y]=0; + + return true; + + } + + t[x]=y; + + rang[y]+=rang[x]; + + rang[x]=0; + + return true; + +} + +void apm() + +{ + + for(auto it : e) + + { + + int x=it.second.first; + + int y=it.second.second; + + if(t[x]!=t[y]) + + { + + if(uneste(x,y)) + + { + + cost+=it.first; + + rez.push_back(it.second); + + } + + } + + } + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y,c; + + f>>x>>y>>c; + + e.push_back({c,{x,y}}); + + } + + sort(e.begin(),e.end()); + + for(int i=1;i<=n;i++) + + { + + t[i]=i; + + rang[i]=1; + + } + + apm(); + + g< +#include +#include +using namespace std; + +ifstream cin("apm.in"); +ofstream cout("apm.out"); + + + +int n, m, k; +struct vertex{ + int x, y, c; +}graph[200005]; + +bool comp(vertex a, vertex b) +{ + return a.c < b.c; +} + +int cost; +int rang[200005], father[200005]; +vector ans[200005]; + + +int find(int nod) +{ + while(nod != father[nod]) + nod = father[nod]; + + return nod; +} + +void kruskal(){ + for(int i = 1; i <= m; i++) + { + int x = find(graph[i].x), y = find(graph[i].y); + if(x != y) + { + if(rang[x] < rang[y]) + father[x] = y; + if(rang[y] < rang[x]) + father[y] = x; + if(rang[y] == rang[x]) + { + father[x] = y; + rang[y]++; + } + k++; + ans[k].push_back(graph[i].x); + ans[k].push_back(graph[i].y); + cost += graph[i].c; + } + } + +} + +int main() { + + cin >> n >> m; + + for(int i = 1; i <= m; i++) + cin >> graph[i].x >> graph[i].y >> graph[i].c; + + sort(graph+1, graph+m+1, comp); + + for(int i = 1; i <= m; i++) + { + rang[i] = 1; + father[i] = i; + } + + kruskal(); + + cout << cost << "\n"; + cout << k << "\n"; + + for(int i = 1; i <= k; i++) + cout << ans[i][0] << " " << ans[i][1] << "\n"; + +} diff --git a/mst/kruskal/58997786-438f-4963-a2b1-71f9a51d6653.cpp b/mst/kruskal/58997786-438f-4963-a2b1-71f9a51d6653.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b911fc0bfd04598a4936985e2593dd813641e85c --- /dev/null +++ b/mst/kruskal/58997786-438f-4963-a2b1-71f9a51d6653.cpp @@ -0,0 +1,164 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int Nmax = 4e5 + 5; + +struct Muchie{ + + int x, y, c; + +}M[Nmax]; + +struct cmp{ + + bool operator()(Muchie x, Muchie y) + + { + + return x.c < y.c; + + } + +}; + +int n, m, k, Total; + +int TT[Nmax], RG[Nmax]; + +pair P[Nmax]; + + + +void Read() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + in>>M[i].x>>M[i].y>>M[i].c; + + sort(M + 1, M + m + 1, cmp()); + + for(int i = 1; i <= n; i++) + + { + + TT[i] = i; + + RG[i] = 1; + + } + + + +} + + + +int Find(int Nod) + +{ + + while(TT[Nod] != Nod) + + Nod = TT[Nod]; + + return Nod; + +} + + + +void Unire(int x, int y) + +{ + + if(RG[x] < RG[y]) + + TT[x] = y; + + else if(RG[x] > RG[y]) + + TT[y] = x; + + else if(RG[x] == RG[y]) + + { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() + +{ + + for(int i = 1; i <= m; i++) + + { + + if(Find(M[i].x) != Find(M[i].y)) + + { + + Unire(Find(M[i].x), Find(M[i].y)); + + P[++k].first = M[i].x; + + P[k].second = M[i].y; + + Total += M[i].c; + + } + + } + +} + + + +void Print() + +{ + + out< +#include +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 100000, + +M = 4*N; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +std::ostream& operator << (std::ostream& stream, Muchie& muchie) + +{return stream << muchie.a+1 << ' ' << muchie.b+1 << '\n';} + + + +int n; + + + +namespace disjunct + +{ + + int set[N]; + + + + void reset() + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int rad_a, int rad_b) + + { + + if (set[rad_a] > set[rad_b]) + + std::swap(rad_a, rad_b); + + set[rad_a] += set[rad_b]; + + set[rad_b] = rad_a; + + } + +} + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], suma; + + std::vector muchii; + + + + struct CompMuchie + + {inline bool operator () (Muchie& a, Muchie& b) {return a.cost > b.cost;}}; + + + + void reset() + + {for (int i = 0; i < n; i++) {tata[i].b = -1; rank[i] = 0;}} + + + + void eval(std::vector* vecini, int radacina) + + { + + std::priority_queue + + , CompMuchie> inventar; + + + + auto push_vecini = [vecini, &inventar](Muchie muchie) + + {for (auto arc: vecini[muchie.b]) if (arc.b != muchie.a) inventar.push({muchie.b, arc.b, arc.cost});}; + + + + push_vecini({-1, radacina, -1}); + + + + + + while (!inventar.empty()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + + + int + + rad_a = disjunct::rad(muchie.a), + + rad_b = disjunct::rad(muchie.b); + + + + if (rad_a != rad_b) + + { + + disjunct::uneste(rad_a, rad_b); + + push_vecini(muchie); + + muchii.push_back(muchie); + + suma += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + } + + } + + } + +} +std::vector vecini[N]; + + + +int main() + +{ + + int m; + + in >> n >> m; + + + + while (m--) + + { + + int a, b, cost; + + in >> a >> b >> cost; + + a--, b--; + + vecini[a].push_back({b, cost}); + + vecini[b].push_back({a, cost}); + + } + + + + disjunct::reset(); + + apm::reset(); + + + + apm::eval(vecini, 0); + + + + out << apm::suma << '\n' << n - 1 << '\n'; + + for (auto muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/kruskal/59e207e7-9af7-4281-a6c1-dc587ff934de.cpp b/mst/kruskal/59e207e7-9af7-4281-a6c1-dc587ff934de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e45db31c77108a715dacd7fcde1d32ff966faa4 --- /dev/null +++ b/mst/kruskal/59e207e7-9af7-4281-a6c1-dc587ff934de.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +struct DS { + int n; + vector link, cnt; + + DS(int n) : n(n), cnt(n, 1), link(n, -1) {} + + int root(int a) { + while (link[a] != -1) + a = link[a]; + return a; + } + + bool Connected(int a, int b) { + + return root(a) == root(b); + } + + void Link(int a, int b) { + a = root(a); b = root(b); + if (cnt[a] > cnt[b]) + swap(a, b); + + + link[a] = b; + cnt[b] += cnt[a]; + } +}; + +struct Graph { + struct Edge { + int a, b, c; + Edge(int a, int b, int c) : a(a), b(b), c(c) {} + + bool operator<(const Edge& oth) const { + return c < oth.c; + } + }; + + int n; + vector edges; + vector mst_edges; + + Graph(int n) : n(n) {} + + void AddEdge(int a, int b, int c) { + edges.emplace_back(a, b, c); + } + + long long ComputeMST() { + + sort(edges.begin(), edges.end()); + + + DS ds(n); + long long answer = 0; + for (auto e : edges) { + + + if (!ds.Connected(e.a, e.b)) { + answer += e.c; + + ds.Link(e.a, e.b); + mst_edges.push_back(e); + } + } + return answer; + } +}; + +int main() { + ifstream cin("apm.in"); + ofstream cout("apm.out"); + + int n, m; cin >> n >> m; + Graph G(n); + for (int i = 0; i < m; ++i) { + int a, b, c; cin >> a >> b >> c; --a; --b; + G.AddEdge(a, b, c); + } + cout << G.ComputeMST() << endl; + cout << G.mst_edges.size() << endl; + for (auto e : G.mst_edges) { + cout << e.a + 1 << " " << e.b + 1 << '\n'; + } + return 0; +} diff --git a/mst/kruskal/59eb1dd6-d784-4f4b-858a-75f5cf291a66.cpp b/mst/kruskal/59eb1dd6-d784-4f4b-858a-75f5cf291a66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fae7d4539258ece35c05a5dddd9d9157be41a5c --- /dev/null +++ b/mst/kruskal/59eb1dd6-d784-4f4b-858a-75f5cf291a66.cpp @@ -0,0 +1,232 @@ +#include +#include +#include + + + + +using namespace std; + + + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + + + +struct Edge { + + uint32_t node1; + + uint32_t node2; + + int16_t cost; + +}; + + + + + +void Read( + + uint32_t &n, + + vector &edges + +) { + + in >> n; + + + + uint32_t m; + + int32_t node1; + + int32_t node2; + + int16_t cost; + + + + for (in >> m; m; --m) { + + in >> node1; + + in >> node2; + + in >> cost; + + + + edges.push_back({node1 - 1, node2 - 1, cost}); + + } + +} + + + + + +inline bool Compare( + + Edge const &edge1, + + Edge const &edge2 + +) { + + return edge1.cost < edge2.cost; + +} + + + + + +inline uint32_t Find( + + vector &parent, + + uint32_t const node + +) { + + if (parent[node] == node) { + + return node; + + } + + + + parent[node] = Find(parent, parent[node]); + + + + return parent[node]; + +} + + + + + +inline void Union( + + vector &parent, + + uint32_t const node1, + + uint32_t const node2 + +) { + + parent[Find(parent, node1)] = Find(parent, node2); + +} + + + + + +void Solve( + + uint32_t const n, + + vector &edges + +) { + + sort(edges.begin(), edges.end(), Compare); + + + + vector parent(n); + + + + for (uint32_t i = 0; i < n; ++i) { + + parent[i] = i; + + } + + + + int32_t sum = 0; + + vector indices; + + + + for (uint32_t i = 0; i < edges.size(); ++i) { + + if (Find(parent, edges[i].node1) != Find(parent, edges[i].node2)) { + + sum += edges[i].cost; + + indices.push_back(i); + + + + Union(parent, edges[i].node1, edges[i].node2); + + } + + } + + + + out << sum << '\n'; + + out << n - 1 << '\n'; + + + + for (uint32_t i = 0; i < indices.size(); ++i) { + + out << edges[indices[i]].node1 + 1 << ' '; + + out << edges[indices[i]].node2 + 1 << '\n'; + + } + +} + + + + + +int main() { + + uint32_t n; + + vector edges; + + + + Read(n, edges); + + + + Solve(n, edges); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/mst/kruskal/5a811260-d715-4eec-b50b-1cc029d7c828.cpp b/mst/kruskal/5a811260-d715-4eec-b50b-1cc029d7c828.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f87dcc8396ebb9f424abec58006f2658e418eb7 --- /dev/null +++ b/mst/kruskal/5a811260-d715-4eec-b50b-1cc029d7c828.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie { + + int x; + + int y; + + int val; + +}; + + + +vector > solution; + +int cycle[400005], cost_min; + + + +struct less_than_key + +{ + + inline bool operator() (const muchie& struct1, const muchie& struct2) + + { + + return (struct1.val < struct2.val); + + } + +}; + + + +void Union(int u, int v, int s[]) + +{ + + if (s[u] < s[v]) + + { + + s[u] += s[v]; + + s[v] = u; + + } + + else + + { + + s[v] += s[u]; + + s[u] = v; + + } + +} + + + +int Find(int u, int s[]) + +{ + + int x = u; + + int v = 0; + + while (s[x] > 0) + + { + + x = s[x]; + + } + + while (u != x) + + { + + v = s[u]; + + s[u] = x; + + u = v; + + } + + return x; + +} + + + +void Kruskal(int n, int m, vector g) + +{ + + for(int i = 0; i <= m; i++) + + cycle[i] = -1; + + int i = 0; + + while (i < m) + + { + + int u = g[i].x; + + int v = g[i].y; + + int cost = g[i].val; + + if (Find(u, cycle) != Find(v, cycle)) + + { + + cost_min += cost; + + solution.push_back(make_pair(u,v)); + + Union(Find(u, cycle), Find(v, cycle), cycle); + + } + + i++; + + } + + + +} + + + +int main() + +{ + + int n, m; + + f>>n>>m; + + vector graph; + + for(int i = 0; i < m; i++) + + { + + muchie m; + + f>>m.x>>m.y>>m.val; + + graph.push_back(m); + + } + + sort(graph.begin(), graph.end(), less_than_key()); + + Kruskal(n, m, graph); + + g< +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + +int tata[200005], c, ct, n, m; + + + +struct nod + +{ + + int x, y, cost; + +} v[400005], w[400005]; + + + +bool cmp(nod a, nod b) + +{ + + return a.cost < b.cost; + +} + + + +int rad(int x) + +{ + + if(tata[x] != x) + + tata[x] = rad(tata[x]); + + return tata[x]; + +} + + + +void unire(int x, int y) + +{ + + x = rad(x); + + y = rad(y); + + if(x != y) + + if(x < y) + + tata[y] = x; + + else + + tata[x] = y; + +} + + + +int main() + +{ + + f >> n >> m; + + + + for(int i = 1; i <= n; i++) + + tata[i] = i; + + + + for(int i = 1; i <= m; i++) + + f >> v[i].x >> v[i].y >> v[i].cost; + + + + sort(v + 1, v + m + 1, cmp); + + + + for(int i = 1; i <= m; i++) + + if(rad(v[i].x) != rad(v[i].y)) + + { + + unire(v[i].x, v[i].y); + + c += v[i].cost; + + w[++ct].x = v[i].x, w[ct].y = v[i].y; + + } + + + + g << c << '\n' << ct << '\n'; + + + + for(int i = 1; i <= ct; i++) + + g << w[i].x << ' ' << w[i].y << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/5b03d936-8c2b-46ba-8694-d637b288b87d.cpp b/mst/kruskal/5b03d936-8c2b-46ba-8694-d637b288b87d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65794cd5513bffcb3dbcf6b24b9e62ed8db7e43f --- /dev/null +++ b/mst/kruskal/5b03d936-8c2b-46ba-8694-d637b288b87d.cpp @@ -0,0 +1,102 @@ +#include + + + + +using namespace std; + + + +ifstream fin( "apm.in" ); + +ofstream fout( "apm.out" ); + + + +const int NMAX = 2e5; + +const int MMAX = 4e5; + +struct lol { + + int a, b, c; + + bool operator < ( const lol &shit ) const { + + return c < shit.c; + + } + +}; + +lol v[MMAX + 1]; + +int sef[NMAX + 1]; + +vector > sol; + + + +int myfind( int x ){ + + if( sef[x] == x ) + + return x; + + return sef[x] = myfind(sef[x]); + +} + + + +void myunite( int x, int y ){ + + int sefx = myfind(x), sefy = myfind(y); + + if( sefx != sefy ) + + sef[sefy] = sefx; + +} + + + +int main() { + + int n, m, i, sum; + + fin >> n >> m; + + for( i = 0; i < m; ++i ) + + fin >> v[i].a >> v[i].b >> v[i].c; + + for( i = 1; i <= n; ++i ) + + sef[i] = i; + + sort( v, v + m ); + + sum = 0; + + for( i = 0; i < m; ++i ) + + if( myfind(v[i].a) != myfind(v[i].b) ){ + + sum += v[i].c; + + sol.push_back({v[i].a, v[i].b}); + + myunite(v[i].a, v[i].b); + + } + + fout << sum << "\n" << sol.size() << "\n"; + + for( auto it : sol ) + + fout << it.first << " " << it.second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/5cb0841c-8927-4d56-b714-ea69abd99d29.cpp b/mst/kruskal/5cb0841c-8927-4d56-b714-ea69abd99d29.cpp new file mode 100644 index 0000000000000000000000000000000000000000..416f8299a17947e5523e4aae6e29b6fb89c08346 --- /dev/null +++ b/mst/kruskal/5cb0841c-8927-4d56-b714-ea69abd99d29.cpp @@ -0,0 +1,142 @@ +#include + + + + + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct muchie + +{ + + int x,y,c,ok; + +}v[400001]; + + + +int n,m,i,t[200001]; + + + +int tata(int x) + +{ + + int r, y, aux; + + r=x; + + while(t[r]!=0) + + r=t[r]; + + + + y=x; + + while(t[y]!=0) + + { + + aux=t[y]; + + t[y]=r; + + y=aux; + + } + + return r; + +} + + + +bool comp(muchie u, muchie v) + +{ + + if(u.c>n>>m; + + for(i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + int j; + + sort(v+1, v+m+1, comp); + + for(i=1;i<=n;i++) + + t[i]=0; + + int ct=0; + + int nr=0; + + i=1; + + int x,y,tx,ty; + + while(nr + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +pair < int , int > P[400005]; + +int n, m, total, TT[400005], k, RG[400005]; + + + +struct Edge { + + int x, y, c; + +}V[400005]; + + + +bool fcmp (Edge a, Edge b) { + + return a.c < b.c; + +} + + + +void Read () { + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) fin >> V[i].x >> V[i].y >> V[i].c; + + sort (V + 1, V + m + 1, fcmp); + +} + + + +int Find (int x) { + + int R, y; + + for (R = x; TT[R] != R; R = TT[R]); + + for ( ; TT[x] != x;) { + + y = TT[x]; + + TT[x] = R; + + x = y; + + } + + return R; + +} + + + +void Unite (int x, int y) { + + if (RG[x] > RG[y]) TT[y] = x; + + else TT[x] = y; + + if (RG[x] == RG[y]) RG[y] ++; + +} + + + +void Solve () { + + for (int i = 1; i <= m; ++i) { + + RG[i] = 1; + + TT[i] = i; + + } + + for (int i = 1; i <= m; ++i) { + + int xx = Find (V[i].x), yy = Find (V[i].y); + + if (xx != yy) { + + Unite (xx, yy); + + P[++k] = {V[i].x, V[i].y}; + + total += V[i].c; + + } + + } + +} + + + +void Print () { + + fout << total << '\n' << n - 1 << '\n'; + + for (int i = 1; i <= k; ++i) fout << P[i].first << ' ' << P[i].second << ' ' << '\n'; + +} + + + +void Close () { + + fin.close(); + + fout.close(); + +} + + + +int main() { + + Read (); + + Solve (); + + Print (); + + Close (); + + return 0; + +} diff --git a/mst/kruskal/5d5fbbe8-f33e-4b47-a899-34eb231216d7.cpp b/mst/kruskal/5d5fbbe8-f33e-4b47-a899-34eb231216d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0790755741e8424c7e21236ad774652dc63e9016 --- /dev/null +++ b/mst/kruskal/5d5fbbe8-f33e-4b47-a899-34eb231216d7.cpp @@ -0,0 +1,116 @@ +#include + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct graf{ + + int plecare, destinatie, cost; + +}; + +vector edges; + +vector sols; + +int t[200005], height[200005], n, m, cost_total, nr_muchii_adaugate; + + + +bool compara(graf a, graf b) + +{ + + return (a.cost < b.cost); + +} + + + +int get_root(int k) + +{ + + while(t[k] != k) + + k = t[k]; + + return k; + +} + + + +void unire(int a, int b) + +{ + + if(height[a] > height[b]) + + t[b] = a; + + else if(height[b] > height[a]) + + t[a] = b; + + else + + t[b] = a, height[a]++; + +} + + + +int main() + +{ + + int i, x, y; + + graf variabila; + + fin >> n >> m; + + for(i = 1; i <= n; i++) + + t[i] = i; + + for(i = 0; i < m; i++) + + fin >> variabila.plecare >> variabila.destinatie >> variabila.cost, edges.push_back(variabila); + + sort(edges.begin(), edges.end(), compara); + + for(i = 0; i < m; i++) + + { + + x = get_root(edges[i].plecare), y = get_root(edges[i].destinatie); + + if(x != y) + + cost_total+=edges[i].cost, unire(x, y), sols.push_back(edges[i]), nr_muchii_adaugate++; + + } + + fout << cost_total << '\n'; + + fout << nr_muchii_adaugate << '\n'; + + for(i = 0; i < nr_muchii_adaugate; i++) + + fout << sols[i].destinatie << ' ' << sols[i].plecare << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/5dee03a7-f3a7-4ed8-adff-af40b8517812.cpp b/mst/kruskal/5dee03a7-f3a7-4ed8-adff-af40b8517812.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44c3a89fcf7957ab7747d40c12d494f338053a9c --- /dev/null +++ b/mst/kruskal/5dee03a7-f3a7-4ed8-adff-af40b8517812.cpp @@ -0,0 +1,252 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +typedef struct PAIR{ + + + + int muchieX, muchieY, costMuchie; + + + +}PAIR; + + + +PAIR P; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int nrNoduri, nrMuchii, costArborePartial, nrMuchiiArborePartial, vectorTati[500000], rang[500000]; + +vector muchii; + +bool vizitatMuchii[500000]; + + + +int Radacina(int nod){ + + + + int radacinaNod = nod; + + + + while ( vectorTati[radacinaNod] != radacinaNod ) + + radacinaNod = vectorTati[radacinaNod]; + + + + while ( vectorTati[nod] != nod ){ + + int aux = vectorTati[nod]; + + vectorTati[nod] = radacinaNod; + + nod = aux; + + } + + + + return radacinaNod; + + + +} + + + +void Unire(int radacinaX, int radacinaY){ + + + + if ( radacinaX != radacinaY ){ + + + + if(rang[radacinaX] > rang[radacinaY]) + + vectorTati[radacinaY] = radacinaX; + + + + else + + vectorTati[radacinaX] = radacinaY; + + + + + + if( rang[radacinaX] == rang[radacinaY] ) + + rang[radacinaY]++; + + } + + + +} + + + +int cmp(PAIR X, PAIR Y){ + + + + return X.costMuchie < Y.costMuchie; + + + +} + + + +void kruskal(){ + + + + int nrComponenteConexe = nrNoduri; + + + + for ( int i = 1; i <= nrNoduri; i++){ + + vectorTati[i] = i; + + rang[i] = 1; + + } + + + + costArborePartial = 0 , nrMuchiiArborePartial = 0; + + + + int lim = muchii.size(); + + + + for(int i = 0; i < lim && nrComponenteConexe > 1 ; i++){ + + + + int radacinaX = Radacina(muchii[i].muchieX) , radacinaY = Radacina(muchii[i].muchieY); + + + + if ( radacinaX != radacinaY ){ + + + + vizitatMuchii[i] = true; + + + + costArborePartial += muchii[i].costMuchie; + + nrMuchiiArborePartial++; + + + + Unire(radacinaX,radacinaY); + + + + nrComponenteConexe--; + + } + + } + +} + + + +int main(){ + + + + if ( !fin ){ + + cout << "\nEroare la deschiderea fisierului !\n"; + + exit(-1); + + } + + + + fin >> nrNoduri >> nrMuchii; + + + + for (int i = 0; i < nrMuchii; i++) { + + + + fin >> P.muchieX >> P.muchieY >> P.costMuchie; + + + + muchii.push_back(P); + + + + vizitatMuchii[i] = false; + + + + } + + + + sort(muchii.begin(),muchii.end(),cmp); + + + + kruskal(); + + + + fout << costArborePartial << "\n" << nrMuchiiArborePartial << "\n"; + + + + for ( int i = 0; i < nrMuchii; i++) + + if ( vizitatMuchii[i] ) + + fout << muchii[i].muchieX << " " << muchii[i].muchieY <<"\n"; + + + + + + return 0; + + + +} diff --git a/mst/kruskal/5df64e9c-e8f3-42fb-a89f-9dc52f93d8cd.cpp b/mst/kruskal/5df64e9c-e8f3-42fb-a89f-9dc52f93d8cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1294ef52ea3077cc7f52b70d548fd078c36fd31 --- /dev/null +++ b/mst/kruskal/5df64e9c-e8f3-42fb-a89f-9dc52f93d8cd.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +set>>S; + +vector>G; + +int T[200005]; + +int n,m,t; + +void Citire() + +{ + + fin>>n>>m; + + for(int i=1;i<=n;i++) + + T[i]=i; + + for(int i=1;i<=m;i++) + + { + + int x,y,w; + + fin>>x>>y>>w; + + S.insert({w,{x,y}}); + + } + +} + +int Radacina(int x) + +{ + + if(T[x]==x)return x; + + return T[x]=Radacina(T[x]); + +} + +int main() + +{ + + Citire(); + + for(auto p : S) + + { + + int w=p.first; + + auto p2=p.second; + + int x=p2.first; + + int y=p2.second; + + int rx=Radacina(x); + + int ry=Radacina(y); + + if(rx!=ry) + + { + + T[ry]=rx; + + t=t+w; + + G.push_back({x,y}); + + } + + } + + fout< +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct elem + +{ + + int dest, node, cost; + +}v[400005]; + + + +int dads[200005]; + +int rnk[200005]; + + + +int cost, k; + + + +int sol[200005][2]; + + + + int n, m; + + + + + +inline bool cmp(const elem &a, const elem &b) + +{ + + return a.cost < b.cost; + +} + + + +int root(int node) + +{ + + int father = dads[node]; + + + + while(father != dads[father]) + + father = dads[father]; + + + + dads[node] = father; + + return father; + +} + + + +void join(int p, int q) + +{ + + p = root(p); + + q = root(q); + + + + if(rnk[p] < rnk[q]) + + dads[p] = q; + + else + + if(rnk[p] > rnk[q]) + + dads[q] = p; + + else + + rnk[q]++, dads[p] = q; + +} + + + +void kruskal() + +{ + + for(int i = 1; i <= m; i++) + + if(root(v[i].node) != root(v[i].dest)) + + { + + join(v[i].node,v[i].dest); + + cost += v[i].cost; + + sol[++k][0] = v[i].node; + + sol[k][1] = v[i].dest; + + } + + fout << cost << '\n'; + + fout << k << '\n'; + + for(int i = 1; i <= k; i++) + + fout << sol[i][0] << " " << sol[i][1] << '\n'; + +} + + + +int main() + +{ + + fin >> n >> m; + + + + for(int i = 1; i <= m; i ++) + + { + + int x, y, z; + + + + fin >> x >> y >> z; + + + + v[i] = {x,y,z}; + + } + + + + for(int i = 1; i <= n; i++) + + dads[i] = i; + + + + sort(v + 1,v + m + 1,cmp); + + + + kruskal(); + + return 0; + +} diff --git a/mst/kruskal/5f55869a-2cb8-4d5b-9d41-ad2e257bdd2c.cpp b/mst/kruskal/5f55869a-2cb8-4d5b-9d41-ad2e257bdd2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c813effa992f826603266f984b29379856b131 --- /dev/null +++ b/mst/kruskal/5f55869a-2cb8-4d5b-9d41-ad2e257bdd2c.cpp @@ -0,0 +1,205 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie + +{ + + int x, y, cost; + +}; + + + +int tata[400005], c, ct, n, m; + +muchie v[400005], w[400005]; + + + +bool Compara(muchie a, muchie b) + +{ + + return a.cost < b.cost; + +} + + + +void Citire() + +{ + + int i; + + fin >> n >> m; + + for (i = 1; i <= n; i++) + + tata[i] = i; + + for (int i = 1; i <= m; i++) + + fin >> v[i].x >> v[i].y >> v[i].cost; + +} + + + +int Rad(int x) + +{ + + if (tata[x] != x) + + tata[x] = Rad(tata[x]); + + return tata[x]; + +} + + + +void Unire(int x, int y) + +{ + + x = Rad(x); + + y = Rad(y); + + if (x != y) + + if (x < y) + + tata[y] = x; + + else + + tata[x] = y; + +} + + + +int Pivotare(muchie a[],int s, int d) + +{ + + int i = s, j = d, pasi = 0, pasj = 1; + + muchie aux; + + while (i < j) + + { + + if (a[i].cost > a[j].cost) + + { + + aux = a[i]; + + a[i] = a[j]; + + a[j] = aux; + + pasi = 1 - pasi; + + pasj = 1 - pasj; + + } + + i = i + pasi; + + j = j + pasj; + + } + + return i; + +} + + + + + +void QS(muchie a[], int s, int d) + +{ + + if (s < d) + + { + + int p = Pivotare(a, s, d); + + QS(a, s, p - 1); + + QS(a, p + 1, d); + + } + +} + + + + + +int main() + +{ + + int i; + + Citire(); + + + + sort(v + 1, v + m + 1, Compara); + + for (i = 1; i <= m; i++) + + if (Rad(v[i].x) != Rad(v[i].y)) + + { + + Unire(v[i].x, v[i].y); + + c += v[i].cost; + + w[++ct].x = v[i].x, + + w[ct].y = v[i].y; + + } + + + + fout << c << '\n' << ct << '\n'; + + + + for (int i = 1; i <= ct; i++) + + fout << w[i].x << ' ' << w[i].y << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/5f92f726-cbbf-4b6e-b2b4-aea715d8014b.cpp b/mst/kruskal/5f92f726-cbbf-4b6e-b2b4-aea715d8014b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79bdd610844084748ae4b2efc6c9c0dd56bf43ab --- /dev/null +++ b/mst/kruskal/5f92f726-cbbf-4b6e-b2b4-aea715d8014b.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int Nmax = 2e5 + 5; + +int n, m, cnt; + +vector>G[Nmax]; + +vector>G_min; + +int T[Nmax]; + +struct muchie{ + + int x, y, c; + +}V[2*Nmax]; + +struct cmp{ + + bool operator()(muchie x, muchie y) + + { + + return x.c < y.c; + + } + +}; + + + +void read() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + in>>V[i].x>>V[i].y>>V[i].c; + + G[V[i].x].push_back({V[i].y, V[i].c}); + + G[V[i].y].push_back({V[i].x, V[i].c}); + + } + +} + + + +void unire(int x, int y) + +{ + + T[x] = y; + + + +} + + + +int radacina(int nod) + +{ + + while(T[nod] != nod) + + nod = T[nod]; + + return nod; + +} + + + +void compresie(int x, int rad) + +{ + + int y = T[x]; + + while(T[x] != rad) + + { + + unire(x, rad); + + x = y; + + } + +} + + + +void solve() + +{ + + int cost_min = 0; + + sort(V + 1, V + m + 1, cmp()); + + for(int i = 1; i <= n; i++) + + T[i] = i; + + for(int i = 1; i <= m; i++) + + { + + int x = V[i].x, + + y = V[i].y, + + c = V[i].c; + + int X = radacina(x); + + int Y = radacina(y); + + compresie(x, X); + + compresie(y, Y); + + if(X != Y) + + unire(X, Y), cost_min += c, cnt++, G_min.push_back({x,y}); + + } + + out< + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m; + +int cost; + +int parent[200005]; + +int card[200005]; + +vector >ans; + +priority_queue >,vector > >,greater > > >pq; + +int find_parent(int pos) + +{ + + + + if (parent[pos]!=pos) + + { + + parent[pos]=find_parent(parent[pos]); + + return parent[pos]; + + } + + return pos; + +} + +void reuniune(int x,int y) + +{ + + + + + + + + x=find_parent(x); + + y=find_parent(y); + + + + if (card[x]>n>>m; + + int c,x,y; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + pq.push({c,{x,y}}); + + } + + for(int i=1;i<=n;i++) + + { + + + + parent[i]=i; + + card[i]=i; + + } + + compute_answer(); + + + + fout< + + + + + +using namespace std; + +void __print(int x) {cerr << x;} +void __print(long x) {cerr << x;} +void __print(long long x) {cerr << x;} +void __print(unsigned x) {cerr << x;} +void __print(unsigned long x) {cerr << x;} +void __print(unsigned long long x) {cerr << x;} +void __print(float x) {cerr << x;} +void __print(double x) {cerr << x;} +void __print(long double x) {cerr << x;} +void __print(char x) {cerr << '\'' << x << '\'';} +void __print(const char *x) {cerr << '\"' << x << '\"';} +void __print(const string &x) {cerr << '\"' << x << '\"';} +void __print(bool x) {cerr << (x ? "true" : "false");} + +template +void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} +template +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} +void _print() {cerr << "]\n";} +template +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} + + + + + + + +const int nax=1e5+10; + +vector>> edge; +vector p(nax,-1); + +int get_parent(int v){ + if(p[v]==-1)return v; + return p[v]=get_parent(p[v]); +} + +int main(){ + ifstream in("apm.in"); + ofstream out("apm.out"); + int n,m; + in>>n>>m; + int u,v,w; + for(int i=0;i>u>>v>>w; + u--,v--; + edge.push_back(make_pair(w,make_pair(u,v))); + } + sort(edge.begin(),edge.end()); + int p1,p2; + int c=0; + vector> ans; + for(int i=0;i +#include + + +using namespace std; + +ifstream in ("apm.in"); + +ofstream out("apm.out"); + + + +bool fr[200001]; + +int t[200005], h[200005]; + + + +int varf(int x) + +{ + + while (x != t[x]) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if (h[x] < h[y]) + + t[x] = y; + + else if (h[y] < h[x]) + + t[y] = x; + + else + + { + + t[x] = y; + + ++h[y]; + + } + +} + + + +struct MUCHIE + +{ + + int x,y,c; + +}temp,temp2; + + + +struct mycomp + +{ + + bool operator()(MUCHIE &a,MUCHIE &b) + + { + + if(a.c>b.c) + + return 1; + + else + + return 0; + + } + +}; + + + +int n,m; + +priority_queue,mycomp> pq; + +queue q; + + + +int main() + +{ + + int nr,cost=0,i; + + in>>n>>m; + + + + nr=n-1; + + + + for (i = 1; i <= n; ++i) + + t[i] = i, h[i] = 1; + + + + for(i = 0; i < m; i++) + + { + + in>>temp.x>>temp.y>>temp.c; + + pq.push(temp); + + } + + + + while(nr && !pq.empty()) + + { + + temp=pq.top(); + + temp2=temp; + + temp.x=varf(temp.x); + + temp.y=varf(temp.y); + + pq.pop(); + + + + if (temp.x != temp.y) + + { + + unire(temp.x, temp.y); + + q.push(temp2); + + cost += temp.c; + + nr--; + + } + + } + + + + out< +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 2e5; + +const int MMAX = 4e5; + + + +struct edge { + + int a; + + int b; + + int cost; + +}; + +vector edges; + +vector ans; + +int father[NMAX + 1]; + + + +void init() { + + for ( int i = 1; i <= NMAX; i ++ ) + + father[i] = i; + +} + +int root( int i ) { + + if ( father[i] == i ) + + return i; + + return ( father[i] = root( father[i]) ); + +} + +bool cmp( edge a, edge b ) { + + return a.cost < b.cost; + +} + +int main() { + + ifstream fin( "apm.in" ); + + ofstream fout( "apm.out" ); + + int n, m, i, a, b, cost, total = 0, k; + + fin >> n >> m; + + for ( i = 0; i < m; i ++ ) { + + fin >> a >> b >> cost; + + edges.push_back( { a, b, cost } ); + + } + + init(); + + sort( edges.begin(), edges.end(), cmp ); + + k = i = 0; + + while ( k < n - 1 ) { + + a = edges[i].a; + + b = edges[i].b; + + if ( root( a ) != root( b ) ) { + + total += edges[i].cost; + + father[root( a )] = root( b ); + + ans.push_back( { a, b, cost } ); + + k ++; + + } + + i ++; + + } + + fout << total << '\n' << n - 1 << '\n'; + + for ( auto& x : ans ) + + fout << x.a << ' ' << x.b << '\n'; + + return 0; + +} diff --git a/mst/kruskal/658139d2-a707-413e-835a-2105b729b2d7.cpp b/mst/kruskal/658139d2-a707-413e-835a-2105b729b2d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecacc36cd808bc493a4e0ef51e6dd89ace263712 --- /dev/null +++ b/mst/kruskal/658139d2-a707-413e-835a-2105b729b2d7.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int nlim=200005; + +int n,m,tt[nlim],rg[nlim],total=0,z=0; + +pair P[nlim]; + + + +struct muchie + +{ + + int a,b,cost; + +}v[nlim]; + + + +int comp(muchie a, muchie b) + +{ + + return a.costrg[y]) + + tt[y]=x; + + if(rg[x]==rg[y]) + + { + + tt[x]=y; + + rg[y]++; + + } + +} + + + +void kruskal() + +{ + + for(int i=1;i<=m;i++) + + { + + int tatal_x=tatal(v[i].a); + + int tatal_y=tatal(v[i].b); + + int costul=v[i].cost; + + if(tatal_x!=tatal_y) + + { + + unire(tatal_x,tatal_y); + + total=total+costul; + + z++; + + P[z].first=v[i].a; + + P[z].second=v[i].b; + + } + + } + +} + + + +void citire() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + f>>v[i].a>>v[i].b>>v[i].cost; + + sort(v+1,v+m+1,comp); + +} + + + +int main() + +{ + + citire(); + + kruskal(); + + g< +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int vf, muchii, ct; + +long long Suma; + +int Tata[200005], Dimensiune[200005]; + + + +pair < int, int > Perechi[400005]; + + + +struct elemente + +{ + + int x, y, cost; + + + +} V[400005]; + + + +inline bool cmp( elemente a, elemente b ) + +{ + + return a.cost < b.cost; + +} + + + +inline int Find( int Nod ) + +{ + + if( Nod != Tata[Nod] ) + + Tata[Nod] = Find(Tata[Nod]); + + return Tata[Nod]; + +} + + + +void Unire( int x, int y ) + +{ + + x = Find(x); + + y = Find(y); + + + + if( Dimensiune[x] < Dimensiune[y] ) + + { + + Dimensiune[y] += Dimensiune[x]; + + Tata[x] = y; + + } + + else + + { + + Dimensiune[x] += Dimensiune[y]; + + Tata[y] = x; + + } + + + +} + + + +void Kruskal() + +{ + + + + for( int i = 1; i <= muchii; ++ i ) + + { + + int tata_x = Find(V[i].x); + + int tata_y = Find(V[i].y); + + + + if( tata_x != tata_y ) + + { + + Unire( tata_x, tata_y ); + + + + Suma += V[i].cost; + + Perechi[++ ct].first = V[i].x; + + Perechi[ct].second = V[i].y; + + } + + } + + + +} + + + +int main() + +{ + + fin >> vf >> muchii; + + for( int i = 1; i <= muchii; ++ i ) + + fin >> V[i].x >> V[i].y >> V[i].cost; + + + + sort( V + 1, V + muchii + 1, cmp ); + + + + for(int i = 1; i <= vf; ++ i) + + Tata[i] = i, Dimensiune[i] = 1; + + + + Kruskal(); + + + + fout << Suma << "\n" << ct << "\n"; + + for(int i = 1; i <= ct; ++ i) + + fout << Perechi[i].second << " " << Perechi[i].first << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/663ace64-52f0-48c3-b770-1a95cead5047.cpp b/mst/kruskal/663ace64-52f0-48c3-b770-1a95cead5047.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33a831c4640237ecedbc6a912d13f8feb276ba2e --- /dev/null +++ b/mst/kruskal/663ace64-52f0-48c3-b770-1a95cead5047.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +ifstream r("apm.in"); + +ofstream w("apm.out"); + +vector>cost, g, rez; + +int v[200002], len[200002]; + +int root(int a) + +{ + + if(v[a]==a) + + { + + return a; + + } + + return v[a]=root(v[a]); + +} + +int main() + +{ + + int n, m; + + r>>n>>m; + + for(int i=0; i>x>>y>>c; + + g.push_back(make_pair(x, y)); + + cost.push_back(make_pair(c, i)); + + } + + sort(cost.begin(), cost.end()); + + for(int i=1; i<=n; i++) + + { + + v[i]=i; + + len[i]=1; + + } + + long long sum=0; + + for(int i=0;ib){ + + swap(a, b); + + } + + if(len[a]==len[b]){ + + len[a]++; + + } + + v[b]=a; + + } + + w< +namespace FastRead + +{ + + char buff[5000];int lg = 0 , p = 0; + + char nc() + + { + + if(lg == p){lg = fread(buff , 1 , 5000 , stdin);p = 0;if(!lg) return EOF;} + + return buff[p++]; + + } + + templatevoid read(T&x) + + { + + T sgn = 1; char c;while(!isdigit(c = nc()))if(c == '-')sgn = -1; + + x = c - '0';while(isdigit(c = nc()))x = x * 10 + c - '0';x *= sgn; + + } + +} + + + +using namespace FastRead; + +using namespace std; + + + +const int N = 4e5 + 10; + +const int M = 1e9 + 7; + +const long double PI = acos(-1); + +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + + +int n , m , x , y , cost; + +int root[N] , height[N]; + +vector < pair < int , int > > answer; + + + +struct ceva + +{ + + int cost , x , y; + +}a[N]; + + + +bool inline cmp(ceva a , ceva b) + +{ + + return a.cost < b.cost; + +} + + + +int find(int x) + +{ + + int R; + + + + for(R = x ; R != root[R] ; R = root[R]); + + + + for( ; root[x] != x ; ) + + { + + int aux = root[x]; + + root[x] = R; + + x = aux; + + } + + + + return R; + +} + + + +void unite(int rx , int ry) + +{ + + if(height[rx] < height[ry]) + + root[rx] = ry; + + else if(height[rx] > height[ry]) + + root[ry] = rx; + + else + + { + + ++height[ry]; + + root[rx] = ry; + + } + +} + + + +signed main() + +{ + + + + ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) , freopen("apm.in" , "r" , stdin) , freopen("apm.out" , "w" , stdout); + + + + + + int i; + + + + cin >> n >> m; + + + + for(i = 1 ; i <= m ; i++) + + { + + cin >> x >> y >> cost; + + a[i] = {cost , x , y}; + + } + + + + sort(a + 1 , a + m + 1 , cmp); + + + + for(i = 1 ; i <= n ; i++) + + { + + root[i] = i; + + height[i] = 1; + + } + + + + int sum = 0; + + + + for(i = 1 ; i <= m ; i++) + + { + + int rx = find(a[i].x); + + int ry = find(a[i].y); + + + + if(rx != ry) + + { + + sum += a[i].cost; + + answer.push_back({a[i].x , a[i].y}); + + unite(rx , ry); + + } + + } + + + + cout << sum << '\n'; + + cout << answer.size() << '\n'; + + + + for(auto i : answer) + + cout << i.first << ' ' << i.second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/667dc026-af46-47c7-a290-20f8950fe966.cpp b/mst/kruskal/667dc026-af46-47c7-a290-20f8950fe966.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56dc09f17b45e943dc4cff198121e85d8f1fd8ab --- /dev/null +++ b/mst/kruskal/667dc026-af46-47c7-a290-20f8950fe966.cpp @@ -0,0 +1,170 @@ +#include + + + + +using namespace std; + +ifstream f ("apm.in") ; + +ofstream g ("apm.out") ; + +int T[200005] , h[200005] ; + +struct solutie + +{ + + int st; + + int dr; + +}v[200005]; + +int N , M , x ,y ,cost = 0; + +int cnt = 0 , k = 0 ; + +struct didi + +{ + + int a; + + int b; + + int c; + +}mch[400005]; + +bool cmp (didi a , didi b) + +{ + + return (a.c < b.c) ; + +} + +int TATA (int x) + +{ + + if (x != T[x]) return TATA(T[x]) ; + + return x; + +} + +int muchie (int x , int y) + +{ + + int r1 = TATA(x) ; + + int r2 = TATA(y) ; + + + + if (r1 == r2) return 0 ; + + else + + { + + if (h[r1] < h[r2]) T[r1] = r2; + + else if (h[r1] > h[r2]) T[r2] = r1; + + else + + { + + T[r2] = r1; + + h[r1] ++ ; + + } + + + + return 1; + + } + +} + +void KRUSKAL() + +{ + + k = 1; + + while (cnt < N - 1 && k <= M) + + { + + x = mch[k].a; + + y = mch[k].b; + + if (muchie(x,y)) + + { + + cost += mch[k].c ; + + cnt ++ ; + + v[cnt].st = x; + + v[cnt].dr = y; + + } + + + + k ++ ; + + } + +} + +int main() + +{ + + f >> N >> M ; + + for (int i = 1 ; i <= M ; ++i) + + f >> mch[i].a >> mch[i].b >> mch[i].c; + + + + sort(mch+1,mch+M+1,cmp) ; + + for (int i = 1; i <= N ; ++i) T[i] = i , h[i] = 1; + + + + KRUSKAL(); + + + + g << cost << '\n' ; + + g << N - 1 << '\n' ; + + for (int i = 1 ; i <= cnt ; ++i) + + g << v[i].st << ' ' << v[i].dr << '\n' ; + + + + f.close(); + + g.close(); + + return 0 ; + +} diff --git a/mst/kruskal/66c4d66d-bcfe-4631-bf4e-df35d3fc8b62.cpp b/mst/kruskal/66c4d66d-bcfe-4631-bf4e-df35d3fc8b62.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7ef632103388c5a483ea843fd2df4fe6741cbdf --- /dev/null +++ b/mst/kruskal/66c4d66d-bcfe-4631-bf4e-df35d3fc8b62.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + +using namespace std; + + + +struct MUCHIE{ + + int x, y, ct, sol; + +}v[400005]; + + + +int t[200005], h[200005]; + + + +bool cmp(MUCHIE a, MUCHIE b){ + + return a.ct < b.ct; + +} + + + +int FindSet(int x){ + + while(x != t[x]){ + + x = t[x]; + + } + + return x; + +} + + + +void UnionSet(int x, int y){ + + if(h[x] <= h[y]){ + + if(h[x] == h[y]) + + h[y]++; + + t[x] = y; + + } + + else + + t[y] = x; + +} + + + +int main() + +{ + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int n, m, x, y, ct, s, ma, tx, ty, i; + + scanf("%d%d", &n, &m); + + for(i = 1;i <= m;i++){ + + scanf("%d%d%d", &x, &y, &ct); + + v[i].x = x; + + v[i].y = y; + + v[i].ct = ct; + + } + + sort(v + 1, v + m + 1, cmp); + + for(i = 1;i < n;i++){ + + h[i] = 1; + + t[i] = i; + + } + + s = ma = 0; + + i = 1; + + while(i <= m && ma < n - 1){ + + tx = FindSet(v[i].x); + + ty = FindSet(v[i].y); + + if(tx != ty){ + + ma++; + + s += v[i].ct; + + UnionSet(tx, ty); + + v[i].sol = 1; + + } + + i++; + + } + + printf("%d\n", s); + + printf("%d\n", ma); + + for(i = 1;i <= m;i++){ + + if(v[i].sol == 1) + + printf("%d %d\n", v[i].x, v[i].y); + + } + + return 0; + +} diff --git a/mst/kruskal/674bf091-8d46-4dc6-9992-fe90fc4be004.cpp b/mst/kruskal/674bf091-8d46-4dc6-9992-fe90fc4be004.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36a8313a414aaf5ac60d001e6cc3cc49053ee782 --- /dev/null +++ b/mst/kruskal/674bf091-8d46-4dc6-9992-fe90fc4be004.cpp @@ -0,0 +1,142 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct edge + +{ + + int x, y, c; + +}; + +vector E; + +vector S; + + + +int T[200002]; + +int L[200002]; + +int N, M; + +int i; + + + +int find(int node) + +{ + + if(node == T[node]) + + return node; + + return T[node] = find(T[node]); + +} + + + +void unite(int a, int b) + +{ + + a = find(a); + + b = find(b); + + if(L[a] < L[b]) + + swap(a, b); + + L[a] += L[b]; + + T[b] = a; + +} + + + +bool qx(edge a, edge b) + +{ + + return a.c < b.c; + +} + + + +int main() + +{ + + fin >> N >> M; + + for(i = 1; i <= M; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + E.push_back({x, y, c}); + + } + + for(i = 1; i <= N; i++) + + { + + T[i] = i; + + L[i] = 1; + + } + + int sol = 0; + + sort(E.begin(), E.end(), qx); + + for(i = 0; i < M; i++) + + if(find(E[i].x) != find(E[i].y)) + + { + + sol += E[i].c; + + unite(E[i].x, E[i].y); + + S.push_back(E[i]); + + } + + fout << sol << "\n"; + + fout << S.size() << "\n"; + + for(i = 0; i < S.size(); i++) + + fout << S[i].x << " " << S[i].y << "\n"; + + return 0; + +} diff --git a/mst/kruskal/6825a6b8-1d4b-459b-b9ad-213b60a999bb.cpp b/mst/kruskal/6825a6b8-1d4b-459b-b9ad-213b60a999bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8586ab2013081276c589f9cc4570c96ad519bae --- /dev/null +++ b/mst/kruskal/6825a6b8-1d4b-459b-b9ad-213b60a999bb.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + + +using namespace std; + +const int N = 2e5; +const int M = 4e5; +int sef[N]; + +struct edge { + int from, to, cost; + bool operator < ( const edge &other )const { + return cost < other.cost; + } +}; +vector graf; +vector> result; +int n, m, final_cost; + +int sef_suprem ( int node ) { + if ( sef[node] == node ) + return node; + return sef[node] = sef_suprem ( sef[node] ); +} + +bool cmp_sef ( int node_x, int node_y ) { + int sef_x, sef_y; + sef_x = sef_suprem ( node_x ); + sef_y = sef_suprem ( node_y ); + + if ( sef_x == sef_y ) + return true; + return false; +} + +void union_node ( int node_x, int node_y ) { + int sef_x, sef_y; + sef_x = sef_suprem ( node_x ); + sef_y = sef_suprem ( node_y ); + + sef[sef_y] = sef_x; +} + + +ifstream fin ( "apm.in" ); +ofstream fout ( "apm.out" ); + + +int main() + +{ + + fin >> n >> m; + for ( int i = 0; i < m; i ++ ) { + int x, y, pay; + fin >> x >> y >> pay; + graf.push_back ( {x, y, pay} ); + } + sort ( graf.begin (), graf.end () ); + + for ( int i = 1; i <= N; i ++ ) + sef[i] = i; + + int count_good = 0; + int e = 0; + while ( count_good < n - 1 ) { + if ( cmp_sef ( graf[e].from, graf[e].to ) == false ) { + result.push_back ( {graf[e].from, graf[e].to} ); + union_node ( graf[e].from, graf[e].to ); + final_cost += graf[e].cost; + count_good ++; + } + e ++; + } + + fout << final_cost << '\n'; + fout << n - 1 << '\n'; + for ( int i = 0; i < (int) result.size (); i ++ ) { + fout << result[i].first << ' ' << result[i].second; + fout << '\n'; + } + + return 0; + +} diff --git a/mst/kruskal/698f5666-52da-489e-8357-536901b04b8e.cpp b/mst/kruskal/698f5666-52da-489e-8357-536901b04b8e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8c051841b780f97dfde921d39a9412c89457c69 --- /dev/null +++ b/mst/kruskal/698f5666-52da-489e-8357-536901b04b8e.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct ura{ + + int x,y,c; + +}v[400001],a[200001]; + +int sef[200001]; + + + +bool cmp(ura a,ura b){ + + return a.c>n>>m; + + for(i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + sef[i]=i; + + sort(v+1,v+m+1,cmp); + + for(i=1;i<=m && k<=n-1;i++) + + if(sefsuprem(v[i].x)!=sefsuprem(v[i].y)){ + + a[k]=v[i]; + + sef[sefsuprem(v[i].y)]=sefsuprem(v[i].x); + + s=s+v[i].c; + + k++; + + } + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct Edge { + + int src, dest, weight; + +}; + + + +int n, m; + +Edge graph[400010]; + +int reps[400010]; + +vector sol; + +int sizes[400010]; + + + +int findRep(int x) { + + if(x != reps[x]) + + reps[x] = findRep(reps[x]); + + return reps[x]; + +} + + + +void unite(int x, int y) { + + int smaller = findRep(x); + + int larger = findRep(y); + + + + if(smaller == larger) { + + return; + + } + + + + if(sizes[smaller] > sizes[larger]) { + + swap(smaller, larger); + + } + + + + reps[smaller] = larger; + + sizes[larger] += sizes[smaller]; + +} + + + +int main() { + + int totalWeight = 0; + + + + fin >> n >> m; + + for(int i = 1; i <= m; i++) { + + int x, y, weight; + + fin >> x >> y >> weight; + + graph[i] = {x, y, weight}; + + } + + + + for(int i = 1; i <= n; i++) { + + reps[i] = i; + + sizes[i] = 1; + + } + + + + sort(graph + 1, graph + m + 1, [](Edge a, Edge b) { return a.weight < b.weight; }); + + + + for(int i = 1; i <= m && sol.size() < n - 1; i++) { + + if(findRep(graph[i].src) != findRep(graph[i].dest)) { + + unite(graph[i].src, graph[i].dest); + + totalWeight += graph[i].weight; + + sol.push_back(i); + + } + + } + + + + fout << totalWeight << '\n' << sol.size() << '\n'; + + for(auto vertex : sol) { + + fout << graph[vertex].dest << ' ' << graph[vertex].src << '\n'; + + } + +} diff --git a/mst/kruskal/6ab8eef0-f7a3-4f11-b223-62153a9d8140.cpp b/mst/kruskal/6ab8eef0-f7a3-4f11-b223-62153a9d8140.cpp new file mode 100644 index 0000000000000000000000000000000000000000..565ef5be82cc8290023b2515faadff732adc4c5e --- /dev/null +++ b/mst/kruskal/6ab8eef0-f7a3-4f11-b223-62153a9d8140.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +ifstream fin("apm.in"); ofstream fout("apm.out"); + + + + + +struct node { + + int x, y, dist; + +} mat [400005]; + + + +bool cmp(node a, node b) { + + return a.dist < b.dist; + +} + + + +int dad[200005]; + + + +vector anss; + + + +int ans = 0; + + + +int finddad(int nod) { + + if(dad[nod] != nod) dad[nod] = finddad(dad[nod]); + + return dad[nod]; + +} + + + +void unite(int a, int b) { + + finddad(a); + + finddad(b); + + dad[dad[b]] = dad[a]; + +} +int main() { + + int n, m; + + fin >> n >> m; + + + + for (int i = 1; i <= m; i++) { + + int x, y, dist; + + fin >> mat[i].x >> mat[i].y >> mat[i].dist; + + } + + + + for (int i = 1; i <= n; i++) { + + dad[i] = i; + + } + + + + sort(mat + 1, mat + m + 1, cmp); + + for (int i = 1; i <= m; i++) { + + node now = mat[i]; + + + + int x, y; + + + + x = finddad(now.x); + + y = finddad(now.y); + + + + if (x != y) { + + unite(x, y); + + anss.push_back(now); + + ans += now.dist; + + } + + } + + + + fout << ans << '\n' << anss.size() << '\n'; + + for (auto& x : anss) { + + fout << x.x << " " << x.y << '\n'; + + } + + + + return 0; + +} diff --git a/mst/kruskal/6afdb52a-253f-4a32-988d-4e12982fd33e.cpp b/mst/kruskal/6afdb52a-253f-4a32-988d-4e12982fd33e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70963238bb305e0057589eb2fd1c652900eb90cb --- /dev/null +++ b/mst/kruskal/6afdb52a-253f-4a32-988d-4e12982fd33e.cpp @@ -0,0 +1,146 @@ +#include +#include +#include + + +using namespace std; + + + +struct Arc { + + int x, y, cost; + + Arc(int x, int y, int cost) { + + this->x = x; + + this->y = y; + + this->cost = cost; + + } + +}; + + + +vector selectedArcs; + +vector arcs; + +vector forest; + + + +bool comp(Arc a, Arc b) { + + return a.cost < b.cost; + +} + + + + + +int get(int i) { + + if (forest[i] == i) + + return i; + + forest[i] = get(forest[i]); + + return forest[i]; + +} + + + + + +void add (int i, int j) { + + forest[get(i)] = get(j); + +} + + + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + int n, m, a, b, c; + + scanf("%d%d", &n, &m); + + + + selectedArcs.resize(m+1, 0); + + forest.resize(n+1); + + + + for(int i=0; i +#include +#include + + +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +int n, m, v[400005], rnk[400005], cost, nrsel, sel[400005]; + +vector < pair > > mm; + + + +int getPar(int x) + +{ + + if(v[x] == x) + + return x; + + + + v[x] = getPar(v[x]); + + return v[x]; + +} + + + +void unitePar(int x, int y) + +{ + + int px = getPar(x); + + int py = getPar(y); + + + + if(rnk[px] < rnk[py]) + + { + + v[px] = py; + + rnk[py] = max(rnk[py], rnk[px] + 1); + + } + + else + + { + + v[py] = px; + + rnk[px] = max(rnk[px], rnk[py] + 1); + + } + +} + + + +int main() + +{ + + cin >> n >> m; + + + + for(int i = 1; i <= n; ++i) + + { + + v[i]=i; + + } + + for(int i = 1; i <= m; ++i) + + { + + int a, b, c; + + cin >> a >> b >> c; + + mm.push_back({c, {a, b}}); + + } + + + + sort(mm.begin(),mm.end()); + + + + for(auto it = mm.begin(); it != mm.end(); ++it) + + { + + if(getPar(it->second.first) != getPar(it->second.second)) + + { + + unitePar(it->second.first, it->second.second); + + cost += it->first; + + sel[++nrsel] = distance(mm.begin(), it); + + } + + } + + + + cout << cost << '\n'; + + cout << nrsel << '\n'; + + + + for(int i = 1; i <= nrsel; ++i) + + { + + cout << next(mm.begin(), sel[i])->second.first << ' ' << next(mm.begin(), sel[i])->second.second << '\n'; + + } + + + + return 0; + +} diff --git a/mst/kruskal/6bdd7c33-11df-47b3-b06e-92025abc4d32.cpp b/mst/kruskal/6bdd7c33-11df-47b3-b06e-92025abc4d32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70a7f65c82f4ac68fd5040952c8df6d31fc5ce01 --- /dev/null +++ b/mst/kruskal/6bdd7c33-11df-47b3-b06e-92025abc4d32.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int nMax = 200005; + +const int mMax = 400005; + +int n, m; + +int t[nMax]; + + + +struct triplet + +{ + + int x, y, cost, viz; + +}; + +triplet a[mMax]; + + + +bool cmp(const triplet A, const triplet B) + +{ + + return A.cost < B.cost; + +} + + + + + + + +void Citire() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> a[i].x >> a[i].y >> a[i].cost; + + sort(a + 1, a + m + 1, cmp); + +} + + + +void Union(int x, int y) + +{ + + t[y] = x; + +} + + + +int Find(int x) + +{ + + int aux, rad; + + rad = x; + + while (t[rad]) + + { + + rad = t[rad]; + + } + + while (t[x]) + + { + + aux = t[x]; + + t[x] = rad; + + x = aux; + + } + + return rad; + +} + + + +void Rezolvare() + +{ + + int cost, v, w, nrc = n,costt = 0; + + for (int i = 1; i <= m and nrc > 1; i++) + + { + + v = a[i].x; + + w = a[i].y; + + v = Find(v); + + w = Find(w); + + if (v != w) + + { + + Union(v, w); + + a[i].viz = 1; + + costt += a[i].cost; + + nrc--; + + } + + } + + fout << costt << "\n"; + + fout << n - 1 << "\n"; + + for (int i = 1; i <= m; i++) + + if (a[i].viz == 1) + + fout << a[i].x << " " << a[i].y << "\n"; + +} + + + +int main() + +{ + + Citire(); + + Rezolvare(); + + return 0; + +} diff --git a/mst/kruskal/6c036ff9-e818-431a-8afb-f44b7e67f2ad.cpp b/mst/kruskal/6c036ff9-e818-431a-8afb-f44b7e67f2ad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00ba4a7bcc0d9e63b1212219fc848f1a391e2a39 --- /dev/null +++ b/mst/kruskal/6c036ff9-e818-431a-8afb-f44b7e67f2ad.cpp @@ -0,0 +1,97 @@ +#include +#include + + +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +int tata[200005],sz[200005],cost=0,cnt=0; + +pair >edge[400005]; + +pair sol[200005]; + +int daddy(int a){ + + if(a==tata[a]) + + return a; + + tata[a]=daddy(tata[a]); + + return tata[a]; + +} + +void unite(int a,int b,int c){ + + int aa=a; + + int bb=b; + + a=daddy(a); + + b=daddy(b); + + if(a==b) + + return; + + if(sz[a]>sz[b]) + + swap(a,b); + + tata[a]=b; + + sz[b]+=sz[a]; + + cost+=c; + + sol[++cnt]=make_pair(aa,bb); + +} + +int main() + +{ + + int n,m,a,b,c; + + cin>>n>>m; + + for(int i=1;i<=n;i++){ + + tata[i]=i; + + sz[i]=1; + + } + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>c; + + edge[i]=make_pair(c,make_pair(a,b)); + + } + + sort(edge+1,edge+m+1); + + for(int i=1;i<=m;i++){ + + unite(edge[i].second.first,edge[i].second.second,edge[i].first); + + } + + cout< + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int DIM = 200000 + 5; + + + +int Size[DIM], parent[DIM]; + + + +vector < pair < int, int > > sol; + + + +void make_set(int k) + +{ + + Size[k] = 1; + + parent[k] = k; + +} + + + +int find_set(int k) + +{ + + if(k == parent[k]) return k; + + return parent[k] = find_set(parent[k]); + +} + + + +void union_sets(int a, int b) + +{ + + a = find_set(a); + + b = find_set(b); + + if(a != b) { + + if(Size[b] > Size[a]) { + + swap(a, b); + + } + + Size[a] += Size[b]; + + parent[b] = a; + + } + +} + + + +struct solve + +{ + + int x, y, w; + + + + bool operator < (const solve & nxt) const + + { + + return w < nxt.w; + + } + +}; + + + +vector < solve > edges; + + + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y, w; + + + + fin >> x >> y >> w; + + edges.push_back({x, y, w}); + + } + + int ans = 0; + + for(int i = 1; i <= n; ++i) make_set(i); + + sort(edges.begin(), edges.end()); + + for(auto v : edges) { + + if(find_set(v.x) != find_set(v.y)) { + + union_sets(v.x, v.y); + + ans += v.w; + + sol.push_back({v.x, v.y}); + + } + + } + + fout << ans << "\n"; + + fout << sol.size() << "\n"; + + for(auto v : sol) { + + fout << v.first << " " << v.second << "\n"; + + } + + return 0; + +} diff --git a/mst/kruskal/6c93fd8d-d762-43d3-9f24-afd2473288fb.cpp b/mst/kruskal/6c93fd8d-d762-43d3-9f24-afd2473288fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a63066b7f7a56aa6e739ba9272c3e3284ef6a3c4 --- /dev/null +++ b/mst/kruskal/6c93fd8d-d762-43d3-9f24-afd2473288fb.cpp @@ -0,0 +1,102 @@ +#include +#include +#include + + +using namespace std; + +pair > c[400010]; + +pair sol[400010]; + +int t[400010]; + +int i,n,m,u,rx,ry,cost,x,y; + + + +int aflareRadacina(int i) { + + while (t[i]>0) { + + i=t[i]; + + } + + return i; + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>c[i].second.first>>c[i].second.second; + + fin>>c[i].first; + + } + + sort(c+1,c+m+1); + + for (i=1;i<=n;i++) { + + t[i]=-1; + + } + + for (i=1;i<=m;i++) { + + x=c[i].second.first; + + y=c[i].second.second; + + rx=aflareRadacina(x); + + ry=aflareRadacina(y); + + if (rx!=ry) { + + if (t[rx]>t[ry]) { + + t[ry]+=t[rx]; + + t[rx]=ry; + + } + + else { + + t[rx]+=t[ry]; + + t[ry]=rx; + + } + + sol[++u]={x,y}; + + cost+=c[i].first; + + } + + } + + fout< + + + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct muchie + +{ + + int x, y, c; + +}; + +muchie m[2*200005]; + +int viz[2*200005], n, e, t[200005], sz[200005], s; + +bool criteriu(muchie a, muchie b) + +{ + + return a.csz[y]) + + { + + t[y]=x; + + sz[x]+=sz[y]; + + } + + else + + { + + t[x]=y; + + sz[y]+=sz[x]; + + } + +} + +int main() + +{ + + fin >> n >> e; + + for(int i=1;i<=e;i++) + + fin >> m[i].x >> m[i].y >> m[i].c; + + + + sort(m+1, m+e+1, criteriu); + + for(int i=1;i<=n;i++) + + { + + t[i]=i; + + sz[i]=1; + + } + + for(int i=1;i<=e;i++) + + { + + int x, y; + + x=tata(m[i].x); + + y=tata(m[i].y); + + if(x!=y) + + { + + viz[i]=1; + + s+=m[i].c; + + uneste(x, y); + + } + + } + + fout << s << '\n' << n-1 << '\n'; + + for(int i=1;i<=e;i++) + + if(viz[i]) + + fout << m[i].x << ' ' << m[i].y << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/6d106f89-6420-46c7-8255-5744f59cdfb5.cpp b/mst/kruskal/6d106f89-6420-46c7-8255-5744f59cdfb5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d928d2b1fe35b545c13c68715bf352ef81329a4 --- /dev/null +++ b/mst/kruskal/6d106f89-6420-46c7-8255-5744f59cdfb5.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + + + +vector t; + +int _find (int x) { + + if (t[x]==x) + + return x; + + return t[x]=_find(t[x]); + +} + + + +void _union (int x, int y) { + + int p1=t[x], + + p2=t[y]; + + t[p2]=p1; + +} + + + +int main () { + + ifstream fin ("apm.in"); + + ofstream fout ("apm.out"); + + ios::sync_with_stdio(false); + + + + int n, m, i, j, c, k; + + fin >> n >> m; + + vector > muchii; + + for (k=0; k> i >> j >> c; + + muchii.push_back(make_tuple(i, j, c)); + + } + + fin.close(); + + + + sort (muchii.begin(), muchii.end(), [] (const tuple &a, const tuple &b) -> bool { + + return (get<2>(a))<(get<2>(b)); + + }); + + t.assign(n+1, 0); + + iota(t.begin(), t.end(), 0); + + + + fout << " \n" + + << n-1 << '\n'; + + int s=0, ct=0; + + auto it=muchii.begin(); + + while (it!=muchii.end() && ct(*it)) != _find(get<1>(*it))) { + + _union(get<0>(*it), get<1>(*it)); + + s+=get<2>(*it); + + ct++; + + fout << get<0>(*it) << ' ' << get<1>(*it) << '\n'; + + } + + ++it; + + } + + + + fout.seekp(0); + + fout << s; + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/6d1e4d4d-286e-4d1a-9633-848614aad063.cpp b/mst/kruskal/6d1e4d4d-286e-4d1a-9633-848614aad063.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3002eed3584dc6327ddd1d01a01691062704c3ff --- /dev/null +++ b/mst/kruskal/6d1e4d4d-286e-4d1a-9633-848614aad063.cpp @@ -0,0 +1,157 @@ +#include +#include +using namespace std; + + + +FILE *fin = fopen("apm.in", "r"); + +FILE *fout = fopen("apm.out", "w"); + + + +struct muchie {int x; int y; int cost; int ok;} muchii[400005]; + + + +int n,m,nr,costmin,i,daddy[200005],h[200005]; + + + +bool cmp(muchie a, muchie b) + +{ + + return a.cost < b.cost; + +} + + + +int bigDaddy(int x) + +{ + + int d; + + + + if(daddy[x] == x) + + return x; + + + + d = bigDaddy(daddy[x]); + + daddy[x] = d; + + return d; + +} + + + +void unire(int much) + +{ + + int x,y,d1,d2; + + + + x = muchii[much].x; + + y = muchii[much].y; + + + + d1 = bigDaddy(x); + + d2 = bigDaddy(y); + + + + if(d1 != d2) + + { + + muchii[much].ok = true; + + nr++; + + costmin += muchii[much].cost; + + + + if(h[d1] > h[d2]) + + daddy[d2] = d1; + + else + + { + + daddy[d1] = d2; + + if(h[d1] == h[d2]) + + h[d2]++; + + } + + } + +} + + + +int main() + +{ + + fscanf(fin, "%d%d", &n,&m); + + for(i=1; i<=m; ++i) + + fscanf(fin, "%d%d%d", &muchii[i].x,&muchii[i].y,&muchii[i].cost); + + + + for(i=1; i<=n; ++i) + + daddy[i] = i; + + + + stable_sort(muchii+1, muchii+m+1, cmp); + + + + nr = costmin = 0; + + + + for(i=1; i<=m; ++i) + + unire(i); + + + + fprintf(fout, "%d\n%d\n", costmin,nr); + + for(i=1; i<=m; ++i) + + if(muchii[i].ok) + + fprintf(fout, "%d %d\n", muchii[i].x,muchii[i].y); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/mst/kruskal/6d45a5d6-f987-4279-831a-4f15645c7f1b.cpp b/mst/kruskal/6d45a5d6-f987-4279-831a-4f15645c7f1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..879261a04d39bdd3e6f7803af78fbc7851b6e632 --- /dev/null +++ b/mst/kruskal/6d45a5d6-f987-4279-831a-4f15645c7f1b.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int sef[200001]; + +struct ura + +{ + + int x,y,cost; + +}; + +ura v[200001],ans[200001]; + +bool cmp(ura a,ura b) + +{ + + return a.cost>n>>m; + + for(i=1; i<=m; i++) + + { + + in>>v[i].x>>v[i].y>>v[i].cost; + + } + + for(i=1; i<=n; i++) + + { + + sef[i]=i; + + } + + sort(v+1,v+m+1,cmp); + + int k=0; + + for(i=1; i<=m; i++) + + { + + if(sefsuprem(v[i].x)!=sefsuprem(v[i].y)) + + { + + sef[sefsuprem(v[i].y)]=v[i].x; + + cnt+=v[i].cost; + + ans[++k].x=v[i].x; + + ans[k].y=v[i].y; + + } + + } + + out< + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct edge { + + int x, y, c, OK; + +}v[400001]; + + + +int t[200001]; + +int N, M, ct, nr; + +int rx,ry,x,y; + + + +bool cmp(edge u,edge v){ + + return u.c> N >> M; + + for(int i = 1; i <= M; i++) + + fin >> v[i].x >> v[i].y >> v[i].c; + + + + for(int i=1; i <= N; i++) + + t[i] = 0; + + + + sort(v+1,v+M+1,cmp); + + + + ct = 0; + + nr = 0; + + int i = 1; + + + + while(nr < N-1) { + + x = v[i].x; y = v[i].y; + + rx=radacina(x); + + ry=radacina(y); + + if(rx != ry) { + + ct=ct+v[i].c; + + t[ry]=rx; + + v[i].OK = 1; + + nr++; + + } + + i++; + + } + + + + fout << ct << '\n'< +using namespace std; + + + +int n, m, i, k, s; + +struct elem + +{ + + int x, y, c; + +}; + +elem v[400002], sol[200002]; + + + +inline bool cmp(const elem &a, const elem &b) + +{ + + return a.c < b.c; + +} + +int tata[200002], dim[200002]; + + + +int tata_multime(int x) + +{ + + if(x != tata[x]) + + tata[x] = tata_multime(tata[x]); + + return tata[x]; + +} + + + +void unire(int x, int y) + +{ + + x = tata_multime(x); + + y = tata_multime(y); + + if(dim[x] < dim[y]) + + { + + dim[y]= dim[y] + dim[x]; + + tata[x] = y; + + } + + else + + { + + dim[x] = dim[x] + dim[y]; + + tata[y] = x; + + } + +} + + + +int main() + +{ + + ifstream f("apm.in"); + + ofstream g("apm.out"); + + + + f >> n >> m; + + for(i = 1; i <= m; i ++) + + f >> v[i].x >> v[i].y >> v[i].c; + + sort(v + 1, v + m + 1, cmp); + + for(i = 1; i <= n; i ++) + + tata[i] = i, dim[i] = 1; + + for(i = 1; i <= m && k < n - 1; i ++) + + { + + if(tata_multime(v[i].x) != tata_multime(v[i].y)) + + { + + unire(v[i].x, v[i].y); + + s = s + v[i].c; + + sol[++ k] = v[i]; + + } + + } + + g << s << "\n" << n - 1 << "\n"; + + for(i = 1; i <= k; i ++) + + g << sol[i].x << " " << sol[i].y << "\n"; + + return 0; + +} diff --git a/mst/kruskal/700bd88f-14fa-4a51-88f2-c8285d4f8baf.cpp b/mst/kruskal/700bd88f-14fa-4a51-88f2-c8285d4f8baf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eca3f0396bbc642ea03430ddf20162e0f167c806 --- /dev/null +++ b/mst/kruskal/700bd88f-14fa-4a51-88f2-c8285d4f8baf.cpp @@ -0,0 +1,114 @@ +#include + + + + + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +int n, m, dad[400001], sz[400001], sum; + +vector>ans; + +struct mch + +{ + + int x, y, cost; + +}v[400001]; + +bool cmp(mch a, mch b) + +{ + + return a.cost>n>>m; + + for(int i=1; i<=m; i++) + + f>>v[i].x>>v[i].y>>v[i].cost; + + for(int i=1; i<=n; i++) + + dad[i]=i, sz[i]=1; + + sort(v+1, v+m+1, cmp); + + for(int i=1; i<=m; i++) + + { + + if(Find(v[i].x)!=Find(v[i].y)) + + { + + Union(Find(v[i].x), Find(v[i].y)); + + ans.push_back({v[i].x, v[i].y}); + + sum+=v[i].cost; + + } + + } + + g< +#include +using namespace std; + +class muc { +public: + int x, y, c; + bool taken; + friend istream & operator>>(istream &in, muc &tmp) { + in >> tmp.x >> tmp.y >> tmp.c; + return in; + } +}; + +int n, m; +int cmin, nedges; +int Parent[200005], rang[200005]; +muc edge[400005]; + + +inline void read(); +inline void print(); +inline void solve(); + +inline void init(); +inline void Kruskal(); + +inline int Find(int); +inline void Union(int, int); + +bool cmp(muc &, muc &); + +int main(int argc, const char * argv[]) { + + read(); + solve(); + print(); + return 0; +} + +inline void read() { + ifstream f("apm.in"); + f >> n >> m; + for (int i = 1; i <= m; ++i) + f >> edge[i]; + f.close(); +} + +inline void solve() { + init(); + Kruskal(); +} + +inline void print() { + ofstream g("apm.out"); + g << cmin << '\n' << nedges << '\n'; + for (int i = 1; i <= m; ++i) { + if (edge[i].taken == 1) + g << edge[i].x << ' ' << edge[i].y << '\n'; + } + g.close(); +} + +inline void init() { + for (int i = 1; i <= n; ++i) + Parent[i] = i; + sort(edge + 1, edge + m + 1, cmp); +} + +inline void Kruskal() { + for (int i = 1; i <= m; ++i) + if (Find(edge[i].x) != Find(edge[i].y)) { + Union(edge[i].x, edge[i].y); + cmin += edge[i].c; + edge[i].taken = 1; + ++nedges; + } +} + +inline int Find(int node) { + if (Parent[node] != node) + Parent[node] = Find(Parent[node]); + return Parent[node]; +} + +inline void Union(int x, int y) { + int px = Find(x); + int py = Find(y); + + if (px == py) + return; + if (rang[px] < rang[py]) + Parent[px] = py; + else if (rang[px] > rang[py]) + Parent[py] = px; + else { + Parent[px] = py; + ++rang[py]; + } + +} + +bool cmp(muc & a, muc & b) { + return a.c < b.c; +} diff --git a/mst/kruskal/72a9ba55-0f50-4b44-b1d6-d1b8ae2a44e4.cpp b/mst/kruskal/72a9ba55-0f50-4b44-b1d6-d1b8ae2a44e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8be45118a3dad3df4821f34fe22bbe0620fa99e8 --- /dev/null +++ b/mst/kruskal/72a9ba55-0f50-4b44-b1d6-d1b8ae2a44e4.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); +ofstream g("apm.out"); + +const int NMAX = 200'001; + +struct Edge { + int x, y, cost; +}; + +template < int SIZE > +class Disjoint { + +private: + + vector < int > tree; + + void collapse(const int& root, int node) { + while(node != root) { + int aux = tree[node]; + tree[node] = root; + node = aux; + } + } + +public: + + Disjoint() { + tree.resize(SIZE + 1, -1); + } + + int Find(const int& node) { + int root = node; + + while(tree[root] > 0) + root = tree[root]; + + collapse(root, node); + + return root; + } + + void Union(int rootX, int rootY) { + if(-tree[rootX] < -tree[rootY]) + swap(rootX, rootY); + + tree[rootX] = tree[rootX] + tree[rootY]; + tree[rootY] = rootX; + } + +}; + +int n, m; +vector < pair < int, int > > G[NMAX]; +vector < Edge > edges, sol; + +int main() { + + f >> n >> m; + + while(m--) { + int x, y, c; + f >> x >> y >> c; + + G[x].push_back( { y, c } ); + G[y].push_back( { x, c } ); + edges.push_back({ x, y, c }); + } + + sort(edges.begin(), edges.end(),[](const Edge& a, const Edge& b) { + return a.cost < b.cost; + }); + + Disjoint < NMAX > disjoint; + int cnt = 0, cost = 0; + + for(Edge e : edges) { + + const int rootX = disjoint.Find(e.x); + const int rootY = disjoint.Find(e.y); + + if(rootX != rootY) { + sol.push_back(e); + disjoint.Union(rootX, rootY); + cnt++; + cost += e.cost; + } + + if(cnt == n - 1) + break; + } + + g << cost << '\n'; + g << (int) sol.size() << '\n'; + + for(auto it : sol) + g << it.x << ' ' << it.y << '\n'; + + return 0; +} diff --git a/mst/kruskal/736ec05a-390c-45a1-a9b4-82703fee0fd6.cpp b/mst/kruskal/736ec05a-390c-45a1-a9b4-82703fee0fd6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d8159896acceef847ab20d527f5a5b8213f19f06 --- /dev/null +++ b/mst/kruskal/736ec05a-390c-45a1-a9b4-82703fee0fd6.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream o("apm.out"); + + + +struct muchie{ + + int x,y,c; + +} g[400001]; + + + +int k,n,m,x,y,c,rang[200001],t[200001],rez,s[400001],nr; + + + +inline bool cmp(const muchie a,const muchie b){ + + return a.c>n>>m; + + for(i=1;i<=m;++i) + + f>>g[i].x>>g[i].y>>g[i].c; + + + + sort(g+1,g+m+1,cmp); + + + + apm(); + + o << rez << '\n' << n-1 << '\n'; + + for(i=1;i<=nr;++i){ + + k=s[i]; + + o << g[k].x << " " << g[k].y << '\n'; + + } + + return 0; + +} diff --git a/mst/kruskal/73e9c0e2-5aeb-4ceb-8daf-c4f21e8b666f.cpp b/mst/kruskal/73e9c0e2-5aeb-4ceb-8daf-c4f21e8b666f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..662202810596e2f357c9ef925348f9e793f7e95d --- /dev/null +++ b/mst/kruskal/73e9c0e2-5aeb-4ceb-8daf-c4f21e8b666f.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct ex{ + + int x, y, c; + +}g[400005], sol[400005]; + + + +int n, m, t[200005], rang[200005], z; + + int sum; + + + +bool cmp(ex a, ex b){ + + return a.c < b.c; + +} + + + +int radacina(int k){ + + if(!t[k]) + + return k; + + else{ + + int x = radacina(t[k]); + + t[k] = x; + + return x; + + } + +} + + + +void unire(int rk, int rp){ + + if(rang[rk] > rang[rp]) + + t[rp] = rk; + + else{ + + t[rk] = rp; + + if(rang[rp] == rang[rk]) + + rang[rp]++; + + } + +} + + + +int main(){ + + in>>n>>m; + + + + for(int i = 1; i <= m; i++) + + in>>g[i].x>>g[i].y>>g[i].c; + + + + sort(g + 1, g + m + 1, cmp); + + + + for(int i = 1; i <= m; i++){ + + int rk = radacina(g[i].x), rp = radacina(g[i].y); + + if(rk != rp){ + + unire(rk, rp); + + sol[++z].x = g[i].y, sol[z].y = g[i].x; + + sol[z].c = g[i].c; + + sum += g[i].c; + + } + + } + + out< + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int lim = 400005; + +int n,m,t[lim],rang[lim],total,k; + +pair < int , int >P[lim]; + + + +struct Edge + +{ + + int x,y,c; + +}v[lim]; + + + +bool comp(Edge a, Edge b) + +{ + + return a.c < b.c; + +} + + + +void citire() + +{ + + fin>>n>>m; + + for(int i=1 ; i<=m ; i++) + + fin>> v[i].x >> v[i].y >> v[i].c; + + for(int i=1 ; i<=n ; i++) + + t[i]=i , rang[i]=1; + + sort(v+1 , v+m+1 , comp); + +} + + + +int Root(int x) + +{ + + if(t[x] == x) return x; + + else return t[x]=Root(t[x]); + +} + + + +void Unite(int a, int b) + +{ + + if(rang[a] > rang[b]) t[b]=a; + + else if(rang[a] < rang[b]) t[a]=b; + + else + + { + + t[a]=b; + + rang[a]++; + + } + +} + + + +void Kruskal() + +{ + + for(int i=1 ; i<=m ; i++) + + { + + int r1=Root(v[i].x); + + int r2=Root(v[i].y); + + if(r1 != r2) + + { + + Unite(r1,r2); + + total += v[i].c; + + k++; + + P[k].first = v[i].y; + + P[k].second = v[i].x; + + } + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + fout< +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int Tata[200005], Rank[400005]; + + + +pair < int, int > Perechi[400005]; + + + +struct elemente + +{ + + int x, y, cost; + + + +}V[400005]; + + + +bool Crescator(elemente a, elemente b) + +{ + + return a.cost < b.cost; + +} + + + +int Gasit( int Nod ) + +{ + + while( Nod != Tata[Nod] ) + + Nod = Tata[Nod]; + + + + return Nod; + +} + + + +void Unire( int a, int b ) + +{ + + if( Rank[a] < Rank[b] ) + + Tata[a] = b; + + else if( Rank[a] > Rank[b] ) + + Tata[b] = a; + + else if( Rank[a] == Rank[b] ) + + { + + Tata[a] = b; + + Rank[b] ++; + + } + +} + + + +int main() + +{ + + long long Suma = 0; + + int k = 0; + + int vf, muchii; + + fin >> vf >> muchii; + + for(int i = 1; i <= muchii; i ++) + + { + + fin >> V[i].x >> V[i].y >> V[i].cost; + + } + + for(int i = 1; i <= vf; i ++) + + { + + Tata[i] = i; + + Rank[i] = 1; + + } + + + + sort( V + 1, V + muchii + 1, Crescator ); + + + + for(int i = 1; i <= muchii; i ++) + + { + + int tata_x = Gasit(V[i].x); + + int tata_y = Gasit(V[i].y); + + + + if( Tata[tata_x] != Tata[tata_y] ) + + { + + Unire( tata_x, tata_y ); + + + + Suma += V[i].cost; + + Perechi[++ k].first = V[i].x; + + Perechi[k].second= V[i].y; + + } + + } + + + + fout << Suma << "\n"; + + + + fout << vf - 1 << "\n"; + + + + for(int i = 1; i < vf; i ++) + + { + + fout << Perechi[i].second << " " << Perechi[i].first << "\n"; + + } + + + + return 0; + +} diff --git a/mst/kruskal/75d1b51b-39bd-42f7-adf2-2a5c54a4f5b8.cpp b/mst/kruskal/75d1b51b-39bd-42f7-adf2-2a5c54a4f5b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83079b7d759ead5d4e37303502ccfc594f4e26dd --- /dev/null +++ b/mst/kruskal/75d1b51b-39bd-42f7-adf2-2a5c54a4f5b8.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +const int len = 400005; + +int m, n, parent[len], level[len], cost; + +vector > sol; + + + +struct edge { + + int x, y, c; + +} e[len]; + + + +bool cmp(edge a, edge b) { + + return a.c < b.c; + +} + + + +void make_set(int k) { + + parent[k] = k; + + level[k] = 1; + +} + + + +int find_set(int k) { + + if (parent[k] == k) + + return k; + + return parent[k] = find_set(parent[k]); + +} + + + +void union_sets(int x, int y) { + + x = find_set(x); + + y = find_set(y); + + if (x != y) { + + if (level[x] < level[y]) + + swap(x, y); + + parent[y] = x; + + if (level[x] == level[y]) + + level[x]++; + + } + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + cin.tie(0); + + cout.tie(0); + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + cin >> n >> m; + + for (int i = 0; i < m; i++) + + cin >> e[i].x >> e[i].y >> e[i].c; + + sort(e, e + m, cmp); + + for (int i = 0; i < m; i++) + + make_set(i); + + + + for (int i = 0; i < m && !sol.empty() < n; i++) + + if (find_set(e[i].x) != find_set(e[i].y)) { + + cost += e[i].c; + + union_sets(e[i].x, e[i].y); + + sol.push_back({e[i].x, e[i].y}); + + } + + + + cout << cost << "\n" << n - 1 << "\n"; + + for (auto& it : sol) + + cout << it.first << " " << it.second << "\n"; + +} diff --git a/mst/kruskal/76149b38-d776-46df-82f2-0cebdee3f2b2.cpp b/mst/kruskal/76149b38-d776-46df-82f2-0cebdee3f2b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7774e4205c719ef360db895092bb3eb278079960 --- /dev/null +++ b/mst/kruskal/76149b38-d776-46df-82f2-0cebdee3f2b2.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int NMAX=200005,MMAX=400005; + +int n,m,answer,status[NMAX],parent[NMAX]; + +vectorsolution; + +struct edge + +{ + + int x,y,c; + +} v[MMAX]; + +bool cmp(edge A,edge B) + +{ + + return A.c>n>>m; + + for(int i=1; i<=n; i++) + + { + + parent[i]=i; + + status[i]=1; + + } + + for(int i=1; i<=m; i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for(int j=1; j<=m; j++) + + if(findRoot(v[j].x)!=findRoot(v[j].y)) + + { + + answer+=v[j].c; + + solution.push_back(j); + + unionSets(v[j].x,v[j].y); + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +struct edge{ + + int nod1, nod2, cost; + +}v[400005]; + + + +int cmp(edge a, edge b){ + + return a.cost < b.cost; + +} + + + +int n, m, r1, r2, suma; + +int t[200005], soll; + +pair sol[200005]; + + + +int r(int nod){ + + while(t[nod] > 0) + + nod=t[nod]; + + return nod; + +} + + + +int main (){ + + fin>>n>>m; + + for(int i=1; i<=m; i++) + + fin>>v[i].nod1>>v[i].nod2>>v[i].cost; + + sort(v+1, v+m+1, cmp); + + + + for(int i=1; i<=n; i++) + + t[i]=-1; + + + + for(int i=1; i<=m; i++){ + + r1=r(v[i].nod1); + + r2=r(v[i].nod2); + + + + if(r1 != r2){ + + + + if(-t[r1] > -t[r2]){ + + t[r1]+=t[r2]; + + t[r2]=r1; + + }else{ + + t[r2]+=t[r1]; + + t[r1]=r2; + + } + + + + suma+=v[i].cost; + + sol[++soll]=make_pair(v[i].nod1, v[i].nod2); + + if(soll == n-1) + + break; + + } + + } + + + + fout< + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct tip{int x; int c;}; + +struct muc{int x; int y;int c;} mc[400009]; + +vector g[200009]; + +int n,m,cost,tata[200009], h[200009]; + +vector sol; + +bool compar( muc a, muc b) + +{ + + return a.ch[y]) + + tata[y]=x; + + else + + {if(h[y]>=h[x]) + + tata[x]=y; + + if(h[x]==h[y]) + + h[y]++; + + } + +} + +void citire(); + +void sel(); + +void afis(); + +int main() + +{ + + + + citire(); + + sel(); + + afis(); + + return 0; + +} + + + +void citire() + +{ + + int i; + + int x,y,c; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + g[x].push_back({y,c}); + + g[y].push_back({y,c}); + + mc[i]={x,y,c}; + + } + + sort(mc+1,mc+m+1,compar); + +} + +void sel() + +{ + + int nrs=0; + + for(int i=1;i<=m && nrs!=n-1;i++) + + { + + int a=mc[i].x; + + int b=mc[i].y; + + int ca=Find(a),cb=Find(b); + + if(ca!=cb) + + { + + nrs++; + + Unite(ca,cb); + + sol.push_back(i); + + cost+= mc[i].c; + + } + + } + +} + +void afis() + +{ + + int i; + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +int ds[200005]; + +pair > pr[200005]; + +vector < pair < int , int > > ans; + +int N, M, minCost; + + + +void init() + +{ + + for(int i = 1; i <= N; i++){ + + ds[i] = i; + + } + +} + + + +int root(int x) + +{ + + while(ds[x] != x){ + + ds[x] = ds[ds[x]]; + + x = ds[x]; + + } + + return x; + +} + + + +void Union(int x, int y) + +{ + + int px = root(x); + + int py = root(y); + + ds[px] = ds[py]; + +} + + + +int Kursk() + +{ + + int cost, x, y; + + for(int i = 1; i <= M; i++){ + + x = pr[i].second.first; + + y = pr[i].second.second; + + + + cost = pr[i].first; + + if(root(x) != root(y)){ + + + + minCost += cost; + + Union(x,y); + + ans.push_back(make_pair(x,y)); + + } + + } + + + + return minCost; + +} + + + +int main() + +{ + + int x, y, c; + + cin >> N >> M; + + for(int i = 1; i <= M; i++){ + + cin >> x >> y >> c; + + pr[i] = make_pair(c,make_pair(x,y)); + + } + + sort (pr + 1, pr + M + 1); + + init(); + + Kursk(); + + cout << minCost << "\n" << ans.size() << "\n"; + + for(int i = 0; i < ans.size(); i++){ + + cout << ans[i].first << " " << ans[i].second << "\n"; + + } + + return 0; + +} diff --git a/mst/kruskal/7934f785-a58b-40d0-9de4-3202130b6846.cpp b/mst/kruskal/7934f785-a58b-40d0-9de4-3202130b6846.cpp new file mode 100644 index 0000000000000000000000000000000000000000..734b8676e6166a0db50c955f22b272a5c7f901aa --- /dev/null +++ b/mst/kruskal/7934f785-a58b-40d0-9de4-3202130b6846.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +typedef pair iPair; + +struct DS +{ + int nodeCnt; + vector father, connected; + + explicit DS(int N) : nodeCnt(N), connected(N + 1, 1), father(N + 1, -1) {} + + int root(int child) + { + while (father[child] != -1) child = father[child]; + + return child; + } + + bool Connected(int a, int b) + { + return root(a) == root(b); + } + + void Link(int st, int dr) + { + int rootSt = root(st); + int rootDr = root(dr); + if (connected[rootSt] > connected[rootDr]) + swap(rootSt, rootDr); + father[rootSt] = rootDr; + connected[rootDr] += connected[rootSt]; + } +}; + + +struct Edge { + int src, dest, cost; + + Edge(int src, int dest, int cost) : src(src), dest(dest), cost(cost) {} + + bool operator<(const Edge &oth) const + { + return cost < oth.cost; + } +}; + +struct Graph { + int nodeCnt, edgeCnt; + DS *ds = nullptr; + vector edges, apmEdges; + + Graph(int V, int E) + { + this->nodeCnt = V; + this->edgeCnt = E; + edges.reserve(edgeCnt); + + for(int i = 0, a, b, c; i < edgeCnt; ++i) + { + fin >> a >> b >> c; + edges.emplace_back(a, b, c); + } + + this->ds = new DS(nodeCnt); + } + + void displayAPM() { + fout << APM() << '\n'; + fout << apmEdges.size() << '\n'; + for (auto e : apmEdges) + { + fout << e.src << " " << e.dest << '\n'; + } + } + + long long APM() + { + sort(edges.begin(), edges.end()); + + long long TotalCostAPM = 0, edgesAdded = 0; + for (auto e : edges) + { + if(edgesAdded == nodeCnt - 1) break; + if (!ds->Connected(e.src, e.dest)) + { + ds->Link(e.src, e.dest); + TotalCostAPM += e.cost; + apmEdges.emplace_back(e); + edgesAdded++; + } + } + + return TotalCostAPM; + } +}; + +int main() +{ + int n, m; + fin >> n >> m; + + Graph graph(n, m); + + graph.displayAPM(); +} diff --git a/mst/kruskal/7986ad51-8211-4279-a29a-686830a9e873.cpp b/mst/kruskal/7986ad51-8211-4279-a29a-686830a9e873.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac2c2bf13265d06327a075fa31a98c3717c0fa2f --- /dev/null +++ b/mst/kruskal/7986ad51-8211-4279-a29a-686830a9e873.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + + + +vector t; + +int _find (int x) { + + if (t[x]==x) + + return x; + + return t[x]=_find(t[x]); + +} + + + +void _union (int x, int y) { + + t[y]=x; + +} + + + +int main () { + + ifstream fin ("apm.in"); + + ofstream fout ("apm.out"); + + int n, m, i, j, c, k; + + fin >> n >> m; + + vector > muchii; + + for (k=0; k> i >> j >> c; + + muchii.push_back(make_tuple(i, j, c)); + + } + + fin.close(); + + + + sort (muchii.begin(), muchii.end(), [] (const tuple &a, const tuple &b) -> bool { + + return (get<2>(a))<(get<2>(b)); + + }); + + t.assign(n+1, 0); + + iota(t.begin(), t.end(), 0); + + + + fout << " \n" << n-1 << '\n'; + + int s=0, ct=0; + + auto it=muchii.begin(); + + while (it!=muchii.end() && ct(*it)) != _find(get<1>(*it))) { + + _union(t[get<0>(*it)], t[get<1>(*it)]); + + s+=get<2>(*it); + + ct++; + + fout << get<0>(*it) << ' ' << get<1>(*it) << '\n'; + + } + + ++it; + + } + + fout.seekp(0); + + fout << s; + + return 0; + +} diff --git a/mst/kruskal/7a394b59-f6f4-4986-a7da-05a48365ddb6.cpp b/mst/kruskal/7a394b59-f6f4-4986-a7da-05a48365ddb6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..887cdac8145cd695555cf1a221fb87c2610b44df --- /dev/null +++ b/mst/kruskal/7a394b59-f6f4-4986-a7da-05a48365ddb6.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + +const int Nmax = 2e5 + 5; +int n, m, cnt; +vector>G[Nmax]; +vector>G_min; +int T[Nmax]; +struct muchie{ + int x, y, c; +}V[2*Nmax]; +struct cmp{ + bool operator()(muchie x, muchie y) + { + return x.c < y.c; + } +}; + +void read() +{ + in>>n>>m; + for(int i = 1; i <= m; i++) + { + in>>V[i].x>>V[i].y>>V[i].c; + G[V[i].x].push_back({V[i].y, V[i].c}); + G[V[i].y].push_back({V[i].x, V[i].c}); + } +} + +void unire(int x, int y) +{ + T[x] = y; + +} + +int radacina(int nod) +{ + while(T[nod] != nod) + nod = T[nod]; + return nod; +} + +void compresie(int x, int rad) +{ + int y = T[x]; + while(T[x] != rad) + { + unire(x, rad); + x = y; + } +} + +void solve() +{ + int cost_min = 0; + sort(V + 1, V + m + 1, cmp()); + for(int i = 1; i <= n; i++) + T[i] = i; + for(int i = 1; i <= m; i++) + { + int x = V[i].x, + y = V[i].y, + c = V[i].c; + int X = radacina(x); + int Y = radacina(y); + compresie(x, X); + compresie(y, Y); + if(X != Y) + unire(X, Y), cost_min += c, cnt++, G_min.push_back({x,y}); + } + out< +#include +using namespace std; +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); +const int nmax=200005,mmax=400005; +int n,m,tt[nmax],rg[nmax],cnt,s; +pair rez[mmax]; +struct muchie +{ + int x,y,c; +}v[mmax]; +bool cmp (muchie a,muchie b) +{ + return a.c> n >> m; + for (int i=1;i<=m;i++) + { + fin >> v[i].x >> v[i].y >> v[i].c; + } + sort (v+1,v+m+1,cmp); + for (int i=1;i<=n;i++) + { + tt[i]=i; + rg[i]=1; + } + apm(); + fout << s << '\n'; + fout << cnt << '\n'; + for (int i=1;i<=cnt;i++) + { + fout << rez[i].first << " " << rez[i].second << '\n'; + } +} diff --git a/mst/kruskal/7bd2584a-57ea-4f6c-aa3d-2d482e86ca28.cpp b/mst/kruskal/7bd2584a-57ea-4f6c-aa3d-2d482e86ca28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d47581f8f01ae4c43dbfe957892997947acb779 --- /dev/null +++ b/mst/kruskal/7bd2584a-57ea-4f6c-aa3d-2d482e86ca28.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream o("apm.out"); + + + +int n,m,cost=0,t[200010],r[200010]; bool viz[200010]; + +struct muchie{int x,y,c;}; + +vector v; + +vector > p; + + + +bool conditie(muchie a,muchie b){ + + return a.c>n>>m; int i,x,y,c; + + for(i=1;i<=m;i++){ + + f>>x>>y>>c; + + v.push_back({x,y,c}); + + } + + sort(v.begin(),v.end(),conditie); + + for(i=1;i<=n;i++) + + t[i]=i,r[i]=1; + +} + +int tata(int nod){ + + while(t[nod]!=nod) + + nod=t[nod]; + + return nod; + +} + +void unire(int t1,int t2){ + + if(r[t1] +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream o("apm.out"); + + + +int n,m,rang[200001],t[200001],s[400001],sol,nr; + +pair > g[400001]; + + + +void read(){ + + int i; + + f >> n >> m; + + for(i=1;i<=m;++i) + + f >> g[i].second.first >> g[i].second.second >> g[i].first; + + sort(g+1,g+1+m); + +} + + + +int multime(int nod){ + + if(t[nod]!=nod) + + t[nod]=multime(t[nod]); + + return t[nod]; + +} + + + +void join(int i,int j){ + + i=multime(i); + + j=multime(j); + + if(i==j) return; + + + + if(rang[i] + + +using namespace std; + + + +int father[200005]; + +int depth[200005]; + + + +FILE *in = fopen("apm.in", "r"), *out = fopen("apm.out", "w"); + + + +int get_father(int x) + +{ + + if(x != father[x]) + + father[x] = get_father(father[x]); + + return father[x]; + +} + + + +int nr_unions; + + + +struct pereche + +{ + + int x, y; + +} pairs[200005]; + +int pr_size = 0; + + + +int total_cost; + + + +void unite(int x, int y, int c) + +{ + + int cpy_x = x, cpy_y = y; + + x = get_father(x); + + y = get_father(y); + + if(x != y) + + { + + total_cost += c; + + pairs[pr_size].x = cpy_x; + + pairs[pr_size].y = cpy_y; + + pr_size++; + + + + nr_unions--; + + if(depth[y] > depth[x]) swap(x, y); + + + + father[y] = x; + + depth[x]++; + + } + +} + + + +struct edge + +{ + + int x, y, c; + +} edges[400005]; + + + +bool cmp(const edge &a, const edge &b) + +{ + + return a.c < b.c; + +} + + + +int main() + +{ + + int n, m; + + fscanf(in, "%d%d", &n, &m); + + + + for(int i = 0; i < m; i++) + + { + + fscanf(in, "%d%d%d", &edges[i].x, &edges[i].y, &edges[i].c); + + } + + + + for(int i = 0; i <= n; i++) + + { + + father[i] = i; + + depth[i] = 1; + + } + + + + sort(edges, edges + m, cmp); + + + + nr_unions = n - 1; + + for(int i = 0; i < m && nr_unions > 0; i++) + + { + + unite(edges[i].x, edges[i].y, edges[i].c); + + } + + + + fprintf(out, "%d\n", total_cost); + + fprintf(out, "%d\n", pr_size); + + + + for(int i = 0; i < pr_size; i++) + + { + + fprintf(out, "%d %d\n", pairs[i].x, pairs[i].y); + + } + +} diff --git a/mst/kruskal/7d7b7b82-c1cd-405d-b0ac-94dab1049ba8.cpp b/mst/kruskal/7d7b7b82-c1cd-405d-b0ac-94dab1049ba8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8afa5316444a1787d8c16b5b710cc2b1b852d53e --- /dev/null +++ b/mst/kruskal/7d7b7b82-c1cd-405d-b0ac-94dab1049ba8.cpp @@ -0,0 +1,103 @@ +#include +using namespace std; + + +using edge = std::tuple; + + + +class DisjointSet { + std::vector parent; + + public: + DisjointSet(int n) : parent (n + 1) { + std::iota(parent.begin(), parent.end(), 0); + } + + int find_root(int &node) { + if (node != parent[node]) { + parent[node] = find_root(parent[node]); + } + + return parent[node]; + + } + + void reunion(int &root1, int &root2) { + parent[find_root(root1)] = find_root(root2); + } + +}; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m, cost_apm; + + + std::vector edges; + std::vector apm; + + void read_input() { + std::ifstream fin("apm.in"); + fin >> n >> m; + for (int i = 0; i < m; i++) { + int src, dst, cost; + fin >> src >> dst >> cost; + edges.push_back(std::make_tuple(src,dst,cost)); + } + fin.close(); + return ; + } + + void Kruskal() { + + cost_apm = 0; + DisjointSet set(n + 1); + apm.clear(); + + for (auto e : edges) { + int x = std::get<0>(e); + int y = std::get<1>(e); + int c = std::get<2>(e); + if (set.find_root(x) != set.find_root(y)) { + cost_apm = cost_apm + c; + apm.push_back(e); + set.reunion(x, y); + } + } + } + + void get_result() { + std::sort(edges.begin(), edges.end(), [](auto const &t1, auto const &t2) { + return std::get<2>(t1) < std::get<2>(t2); + }); + + Kruskal(); + } + + void print_output() { + std::ofstream fout("apm.out"); + fout<(e) <<" " << std::get<1>(e) << "\n"; + } + fout.close(); + } +}; + +int main() { + + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/mst/kruskal/7dbcc214-c78b-4aa7-8524-29ad1d5ace2f.cpp b/mst/kruskal/7dbcc214-c78b-4aa7-8524-29ad1d5ace2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6fdb51575eca203c7b621f8e66d67fe9a17d33d --- /dev/null +++ b/mst/kruskal/7dbcc214-c78b-4aa7-8524-29ad1d5ace2f.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include + + + +class DisjointSet { +private: + int n; + std::vector p = std::vector(200000 + 1); + std::vector h = std::vector(200000 + 1); +public: + DisjointSet(int nr) { + n = nr; + for(int i = 1; i <= n; ++i) { + p[i] = i; + h[i] = 1; + } + } + + int Find(int x) { + int r, aux; + r = x; + while(p[r] != r) + r = p[r]; + while(p[x] != r) { + aux = p[x]; + p[x] = r; + x = aux; + } + return r; + } + + bool Union(int x, int y) { + int rx, ry; + rx = Find(x); + ry = Find(y); + if(rx != ry) { + if(h[rx] < h[ry]) + p[rx] = ry; + else if(h[rx] > h[ry]) + p[ry] = rx; + else { + p[rx] = ry; + ++h[ry]; + } + return true; + } else return false; + } + + ~DisjointSet() { + p.clear(); + h.clear(); + p.shrink_to_fit(); + h.shrink_to_fit(); + } +}; + +class Edge { +private: + int x; + int y; + int cost; +public: + int getX() { + return x; + } + + int getY() { + return y; + } + + int getCost() { + return cost; + } + + void setX(int X) { + x = X; + } + + void setY(int Y) { + y = Y; + + } + + void setCost(int Cost) { + cost = Cost; + } +}; + +class CompareCost { +public: + bool operator() (Edge& a, Edge& b) { + return a.getCost() < b.getCost(); + } +}; + +class Graph { +private: + int V; + int E; + std::vectorg; +public: + void readGraph(std::string name_fin) { + Edge p; + int x, y, c; + std::ifstream fin(name_fin); + fin >> V >> E; + for(int i = 0; i < E; ++i) { + fin >> x >> y >> c; + p.setX(x); + p.setY(y); + p.setCost(c); + g.push_back(p); + } + } + + void sortEdges() { + sort(g.begin(), g.end(), CompareCost()); + } + + void Kruskal() { + std::ofstream fout("apm.out"); + DisjointSet ds(this->V); + sortEdges(); + int i, nrEdges, apmCost; + std::vector>apm; + i = nrEdges = apmCost = 0; + while(i < this->E && nrEdges < V - 1) { + if(ds.Union(g[i].getX(), g[i].getY())) { + apm.push_back(std::make_pair(g[i].getX(), g[i].getY())); + apmCost += g[i].getCost(); + ++nrEdges; + } + ++i; + } + fout << apmCost << '\n' << nrEdges << '\n'; + for(auto i : apm) + fout << i.first << " " << i.second << "\n"; + fout.close(); + } + + ~Graph() { + g.clear(); + g.shrink_to_fit(); + } +}; + +int main() { + Graph G; + G.readGraph("apm.in"); + G.Kruskal(); + return 0; +} diff --git a/mst/kruskal/7f241d3c-cd0c-4825-bae4-41246c207da3.cpp b/mst/kruskal/7f241d3c-cd0c-4825-bae4-41246c207da3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4822a7edbea738efea3744ca3084046cd1fc2352 --- /dev/null +++ b/mst/kruskal/7f241d3c-cd0c-4825-bae4-41246c207da3.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n, m, cost; + +struct muchie + +{ + + int sursa, dest, cost; + +}; + +vector v; + +vector P; + +vector < pair > sol; + + + +bool comp(muchie A, muchie B) + +{ + + return A.cost>n>>m; + + int x, y, z; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y>>z; + + v.push_back({x,y,z}); + + } + + + + sort(v.begin(),v.end(),comp); + + + + for(int i=0;i<=n;i++) + + P.push_back(i); + + + +} + + + +int radacina(int nod) + +{ + + int rad=nod, aux; + + while(P[rad]!=rad) + + rad=P[rad]; + + while(nod!=rad) + + { + + aux=P[nod]; + + P[nod]=rad; + + nod=aux; + + } + + return rad; + +} + + + +void unire(int nod1, int nod2) + +{ + + P[P[nod2]]=P[nod1]; + +} + + + +int Kruskal() + +{ + + int x, y, z; + + for(int i=0;i +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int vt[400005],r[400005],ind[400005]; + +int n,m,s,k; + +struct muchie + +{ + + int x,y,c; + +}e[400005]; + +bool compara(muchie a, muchie b) + +{ + + return (a.c>n>>m; + + for(int i=1; i<=m; i++) + + { + + fin>>e[i].x>>e[i].y>>e[i].c; + + } + + sort(e+1,e+m+1,compara); + +} + +int radacina(int x) + +{ + + while(vt[x]!=x) + + x=vt[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if(r[x]r[y]) + + vt[y]=x; + + if(r[x]==r[y]) + + { + + vt[y]=x; + + r[x]++; + + } + +} + +void kruskal() + +{ + + for(int i=1;i<=n;i++) + + vt[i]=i; + + for(int i=1;i<=m;i++) + + { + + int a=radacina(e[i].x); + + int b=radacina(e[i].y); + + if(a!=b) + + { + + unire(a,b); + + s=s+e[i].c; + + k++; + + ind[k]=i; + + } + + } + +} + +void afisare() + +{ + + fout< +#include +#include +#include +#include +#include + + +using namespace std; +int father[100005], group[100005], n, m, answer; + +vector< pair > > edges; + +vector< pair > apm; + + + +int findFather(int node) { + + if(node == father[node]) + + return node; + + int new_father = findFather(father[node]); + + father[node] = new_father; + + return new_father; + +} + + + +int main () { + + int a, b, cost; + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d%d", &a, &b, &cost); + + edges.push_back(make_pair(cost, make_pair(a, b))); + + } + + sort(edges.begin(), edges.end()); + + + + for(int i = 1; i <= n; i++) { + + father[i] = i; + + group[i] = 1; + + } + + + + for(auto edge : edges) { + + int a = edge.second.first; + + int b = edge.second.second; + + + + int fa = findFather(a); + + int fb = findFather(b); + + + + if(fa == fb) { + + continue; + + } + + + + apm.push_back(edge.second); + + answer += edge.first; + + + + if(group[fa] > group[fb]) { + + father[fb] = fa; + + group[fa] += group[fb]; + + } + + else { + + father[fa] = fb; + + group[fb] += group[fa]; + + } + + } + + + + printf("%d\n%d\n", answer, n - 1); + + for(auto edge : apm) { + + printf("%d %d\n", edge.first, edge.second); + + } + + + + return 0; + +} diff --git a/mst/kruskal/823b735c-3fe4-47c6-9cd4-a5cf0f134c2d.cpp b/mst/kruskal/823b735c-3fe4-47c6-9cd4-a5cf0f134c2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2838de1a0f3f660c6b9eec8af7d43b3c551f5c2b --- /dev/null +++ b/mst/kruskal/823b735c-3fe4-47c6-9cd4-a5cf0f134c2d.cpp @@ -0,0 +1,61 @@ +#include +#include +const int N=200001,M=400001; + +int d[N],x[M],y[M],c[M],n,m,e,p[M],l[N],i,j; + +int E(int x) + +{ + + if(d[x]==x) + + return x; + + d[x]=E(d[x]); + + return d[x]; + +} + +void R(int x,int y) + +{ + + d[E(x)]=E(y); + +} + +int F(const void *a,const void *b) + +{ + + return c[*(int*)a]-c[*(int*)b]; + +} + +int main() + +{ + + freopen("apm.in","r",stdin),freopen("apm.out","w",stdout),scanf("%d%d",&n,&m); + + for(i=0;i + + + + +using namespace std; + +ifstream f ("apm.in") ; + +ofstream g ("apm.out") ; + +int T[200005] , h[200005] ; + +struct solutie + +{ + + int st; + + int dr; + +}v[200005]; + +int N , M , x ,y ,cost = 0; + +int cnt = 0 , k = 0 ; + +struct didi + +{ + + int a; + + int b; + + int c; + +}mch[400005]; + +bool cmp (didi a , didi b) + +{ + + return (a.c < b.c) ; + +} + +int TATA (int x) + +{ + + if (x != T[x]) return TATA(T[x]) ; + + return x; + +} + +int muchie (int x , int y) + +{ + + int r1 = TATA(x) ; + + int r2 = TATA(y) ; + + + + if (r1 == r2) return 0 ; + + else + + { + + if (h[r1] < h[r2]) T[r1] = r2; + + else if (h[r1] > h[r2]) T[r2] = r1; + + else + + { + + T[r2] = r1; + + h[r1] ++ ; + + } + + + + return 1; + + } + +} + +void KRUSKAL() + +{ + + k = 1; + + while (cnt < N - 1 && k <= M) + + { + + x = mch[k].a; + + y = mch[k].b; + + if (muchie(x,y)) + + { + + cost += mch[k].c ; + + cnt ++ ; + + v[cnt].st = x; + + v[cnt].dr = y; + + } + + + + k ++ ; + + } + +} + +int main() + +{ + + f >> N >> M ; + + for (int i = 1 ; i <= M ; ++i) + + f >> mch[i].a >> mch[i].b >> mch[i].c; + + + + sort(mch+1,mch+M+1,cmp) ; + + for (int i = 1; i <= N ; ++i) T[i] = i , h[i] = 1; + + + + KRUSKAL(); + + + + g << cost << '\n' ; + + g << N - 1 << '\n' ; + + for (int i = 1 ; i <= cnt ; ++i) + + g << v[i].st << ' ' << v[i].dr << '\n' ; + + + + f.close(); + + g.close(); + + return 0 ; + +} diff --git a/mst/kruskal/83c55246-bc05-42bc-90de-9f5f3ea11e48.cpp b/mst/kruskal/83c55246-bc05-42bc-90de-9f5f3ea11e48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3620bd4a13afe502345d210c1cf47aee6a5cc15f --- /dev/null +++ b/mst/kruskal/83c55246-bc05-42bc-90de-9f5f3ea11e48.cpp @@ -0,0 +1,164 @@ +#include +using namespace std; + + + +ifstream ci("apm.in"); + +ofstream cou("apm.out"); + + + +struct date{ + + int x,y,cost; + +}; + + + +int n,m; + +int t[200005]; + +int cn; + +int k; + +date sol[400005]; + +date v[400005]; + + + +int Find(int x){ + + int r=x,p,r1; + + while(t[r]){ + + r=t[r]; + + } + + p=r; + + r=x; + + while(t[r]){ + + r1=t[r]; + + t[r]=p; + + r=r1; + + } + + return p; + +} + +void Union(int x,int y){ + + t[y]=x; + +} + + + +bool cmp(date a,date b){ + + return a.cost>n>>m; + + for(i=1;i<=m;i++){ + + ci>>v[i].x>>v[i].y>>v[i].cost; + + } + + sort(v+1,v+m+1,cmp); + +} + + + +void rez(){ + + int i,x,y,s=0; + + for(i=1;i<=m;i++){ + + x=v[i].x; + + y=v[i].y; + + if(Find(x)!=Find(y)){ + + if(Find(x)!=x){ + + cn++; + + } + + if(Find(y)!=y){ + + cn++; + + } + + s+=v[i].cost; + + Union(Find(x),Find(y)); + + sol[++k].x=x; + + sol[k].y=y; + + + + } + + if(cn==n){ + + break; + + } + + } + + cou< + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct muchie + +{ + + int x, y, c; + +}; + +struct cmp + +{ + + bool operator()(const muchie& a, const muchie& b) + + { + + return a.c > b.c; + + } + +}; + +priority_queue, cmp> q; + +vector> sol; + +void read(); + +int n, m, rez, t[200005]; + +int rad(int x) + +{ + + if(t[x]!=x) + + return (t[x]=rad(t[x])); + + return t[x]; + +} + +void unire(int a, int b, int c) + +{ + + int r1 = rad(a), r2 = rad(b); + + if(r1!=r2) + + { + + if(rand()%2) + + t[r1] = r2; + + else t[r2] = r1; + + rez+=c; + + sol.push_back({a, b}); + + } + +} + +int main() + +{ + + read(); + + for(int i=1; i<=n; i++) + + t[i] = i; + + while(!q.empty()) + + { + + muchie cur = q.top(); + + q.pop(); + + unire(cur.x, cur.y, cur.c); + + } + + fout << rez << '\n'; + + fout << n-1 << '\n'; + + for(auto it:sol) + + fout << it.first << ' ' << it.second << '\n'; + + return 0; + +} + +void read() + +{ + + fin >> n >> m; + + for(int i=1; i<=m; i++) + + { + + muchie w; + + fin >> w.x >> w.y >> w.c; + + q.push(w); + + } + +} diff --git a/mst/kruskal/84174aaf-75e9-47a7-96d7-2220b4032263.cpp b/mst/kruskal/84174aaf-75e9-47a7-96d7-2220b4032263.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a90c48b2e954cb9fff38af8e303b8efdc93dafde --- /dev/null +++ b/mst/kruskal/84174aaf-75e9-47a7-96d7-2220b4032263.cpp @@ -0,0 +1,184 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f ("apm.in"); + +ofstream g ("apm.out"); + + + +int n, m; + + + +int nrm, sum; + + + +int multime[200005]; + + + +pair < int, int > sol[200005]; + + + +bool used[200005]; + + + +struct muchie { + + int x, y, cost; + +}v[400005]; + + + +vector < int > padure[200005]; + + + +bool Cmp (muchie a, muchie b) + +{ + + if (a.cost < b.cost) + + return true; + + return false; + +} + + + +void Uneste (int x, int y) + +{ + + if (padure[y].size() <= padure[x].size()) + + { + + for (int i=0; i> n >> m; + + for (int i=1; i<=m; i++) + + { + + f >> v[i].x >> v[i].y; + + f >> v[i].cost; + + } + + + + for (int i=1; i<=n; i++) + + { + + padure[i].push_back(i); + + multime[i] = i; + + } + + + + sort(v+1, v+m+1, Cmp); + + + + for (int i=1; i<=m && nrm < (n-1); i++) + + { + + int a = v[i].x; + + int b = v[i].y; + + + + if (multime[a] != multime[b]) + + { + + sum += v[i].cost; + + Uneste(multime[a], multime[b]); + + nrm ++; + + sol[nrm].first = b; + + sol[nrm].second = a; + + } + + } + + + + g << sum << "\n" << nrm << "\n"; + + for (int i=1; i<=nrm; i++) + + g << sol[i].first << " " << sol[i].second << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/842b14aa-cde0-443f-adfe-697d1934c0e2.cpp b/mst/kruskal/842b14aa-cde0-443f-adfe-697d1934c0e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b50a5906c15862ec74487a5e71a7998d4d6dddf --- /dev/null +++ b/mst/kruskal/842b14aa-cde0-443f-adfe-697d1934c0e2.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +ifstream fin( "apm.in" ); + +ofstream fout( "apm.out" ); + + + +const int NMAX = 200005; + + + +int N, M; + +struct edg + +{ + + int a, b; + + int d; + + + + bool operator < ( const edg & E ) { + + return d < E.d; + + } + +} A[2 * NMAX]; + + + +int m[NMAX]; + +int sz[NMAX]; + + + +vector > sol; + + + +int Find( int a ) { + + int ra = a; + + + + while( m[ra] != ra ) + + ra = m[ra]; + + + + while( a != m[a] ) { + + int aux = m[a]; + + m[a] = ra; + + a = aux; + + } + + return ra; + +} + + + +void Union( int a, int b ) { + + int ra = Find( a ); + + int rb = Find( b ); + + + + if( sz[ra] < sz[rb] ) swap( ra, rb ); + + + + sz[ra] += sz[rb]; + + m[rb] = ra; + +} + + + +void Read() { + + fin >> N >> M; + + + + int a, b, d; + + for( int i = 1; i <= M; ++i ) { + + fin >> a >> b >> d; + + + + A[i].a = a; + + A[i].b = b; + + A[i].d = d; + + } + +} + + + +void Do() + +{ + + sort( &A[1], &A[M + 1] ); + + + + for( int i = 1; i <= N; ++i ) + + sz[i] = 1, m[i] = i; + + + + int added = 0; + + int cost = 0; + + + + for( int i = 1; i <= M && added < N - 1; ++i ) { + + int fa = Find( A[i].a ), fb = Find( A[i].b ); + + + + if( fa != fb ) { + + ++added; + + Union( fa, fb ); + + cost += A[i].d; + + sol.push_back( { A[i].a, A[i].b } ); + + } + + } + + + + fout << cost << '\n' << added << '\n'; + + for( int i = 0; i < sol.size(); ++i ) + + fout << sol[i].first << ' ' << sol[i].second << '\n'; + + + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/mst/kruskal/844460bd-450b-4dc5-8e1d-b2a911e88969.cpp b/mst/kruskal/844460bd-450b-4dc5-8e1d-b2a911e88969.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff718e6c2fca90e648b0a3590729aa52656c0d1f --- /dev/null +++ b/mst/kruskal/844460bd-450b-4dc5-8e1d-b2a911e88969.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct Op{int x;int y;int c;}; + +vector edge,sol; + +bool mycmp(Op s,Op t) + +{return s.c>n>>m; + +for(int i=0;i>x>>y>>c,edge.push_back({x,y,c}); + +sort(edge.begin(),edge.end(),mycmp); + +for(int i=1;i<=n;++i)link[i]=i,dim[i]=1; + +for(auto t:edge)if(tata(t.x)!=tata(t.y)) + +unite(t.x,t.y),sol.push_back(t),s+=t.c; + +cout< +#include +#include +using namespace std; + +struct Edge { + int x, y, w; + + bool operator < (const Edge& e) const + { + return w < e.w; + } +}; + +using VI = vector; +using VE = vector; + +int n, m, cost; +VE G, apm; +VI t, h; + +void Read(); +void Kruskal(); +int Find(int x); +void Union(int x, int y); +void Write(); + +int main() +{ + Read(); + Kruskal(); + Write(); +} + +void Kruskal() +{ + t = h = VI(n + 1); + for (int i = 1; i <= n; i++) + t[i] = i; + + sort(G.begin(), G.end()); + int c1, c2, cnt{0}; + for (Edge e : G) + { + c1 = Find(e.x); + c2 = Find(e.y); + if (c1 == c2) + continue; + Union(c1, c2); + apm.emplace_back(e); + cost += e.w; + cnt++; + if (cnt == n - 1) + break; + } +} + +int Find(int x) +{ + if (t[x] == x) return x; + return t[x] = Find(t[x]); +} + +void Union(int x, int y) +{ + if (h[x] > h[y]) + t[y] = x; + else + { + t[x] = y; + if (h[x] == h[y]) + h[y]++; + } +} + +void Write() +{ + ofstream fout("apm.out"); + fout << cost << '\n' << n - 1 << '\n'; + for (const Edge& e : apm) + fout << e.x << ' ' << e.y << '\n'; +} + +void Read() +{ + ifstream fin("apm.in"); + fin >> n >> m; + + int x, y, w; + for (int i = 0; i < m; i++) + { + fin >> x >> y >> w; + G.push_back({x, y, w}); + } +} diff --git a/mst/kruskal/857c05bf-ef87-490c-9590-4746e2a12793.cpp b/mst/kruskal/857c05bf-ef87-490c-9590-4746e2a12793.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6f6c9e2944d826fe12d57e3e505daf4f9caa213 --- /dev/null +++ b/mst/kruskal/857c05bf-ef87-490c-9590-4746e2a12793.cpp @@ -0,0 +1,111 @@ +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct chestii{ + + int x,y,c; + +}; + +int sef[200002]; + +bool cmp(chestii a,chestii b) + +{ + + if(a.c>n>>m; + + for(i=1;i<=n;i++) + + sef[i]=i; + + for(i=1;i<=m;i++) + + cin>>a[i].x>>a[i].y>>a[i].c; + + sort(a+1,a+m+1,cmp); + + i=1; + + while(n1!=n-1 && i<=m) + + { + + if(sefsuprem(a[i].x)!=sefsuprem(a[i].y)) + + { + + unire(a[i].x,a[i].y); + + n1++; + + rez[n1]=a[i]; + + nrsol+=a[i].c; + + } + + i++; + + } + + cout< +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + +int tata[200005], c, ct, n, m; + + + +struct nod + +{ + + int x, y, cost; + +} v[400005], w[400005]; + + + +bool cmp(nod a, nod b) + +{ + + return a.cost < b.cost; + +} + + + +int rad(int x) + +{ + + if(tata[x] != x) + + tata[x] = rad(tata[x]); + + return tata[x]; + +} + + + +void unire(int x, int y) + +{ + + x = rad(x); + + y = rad(y); + + if(x != y) + + if(x < y) + + tata[y] = x; + + else + + tata[x] = y; + +} + + + +int main() + +{ + + f >> n >> m; + + + + for(int i = 1; i <= n; i++) + + tata[i] = i; + + + + for(int i = 1; i <= m; i++) + + f >> v[i].x >> v[i].y >> v[i].cost; + + + + sort(v + 1, v + m + 1, cmp); + + + + for(int i = 1; i <= m; i++) + + if(rad(v[i].x) != rad(v[i].y)) + + { + + unire(v[i].x, v[i].y); + + c += v[i].cost; + + w[++ct].x = v[i].x, w[ct].y = v[i].y; + + } + + + + g << c << '\n' << ct << '\n'; + + + + for(int i = 1; i <= ct; i++) + + g << w[i].x << ' ' << w[i].y << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/86843695-4989-42e7-902f-49bb35ece55f.cpp b/mst/kruskal/86843695-4989-42e7-902f-49bb35ece55f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2afd9ea28867b5ec9139946266ad3a21c06edaee --- /dev/null +++ b/mst/kruskal/86843695-4989-42e7-902f-49bb35ece55f.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +const int N_MAX = 2e5 + 5; + +struct Edges { + int x, y, c; + + bool operator < (const Edges other) const { + return c < other.c; + } +}; + +int N, M; +int dad[N_MAX], rnk[N_MAX]; + +vector edges, answer; + +int root(int node) { + if(node == dad[node]) + return node; + return dad[node] = root(dad[node]); +} + +void join(int x, int y) { + x = root(x); + y = root(y); + + if(rnk[x] == rnk[y]) { + rnk[x] ++; + dad[y] = x; + } + else + if(rnk[x] > rnk[y]) + dad[y] = x; + else + dad[x] = y; +} + + +int main() { + fin >> N >> M; + + for(int i = 1; i <= N; i++) + dad[i] = i; + + for(int i = 1; i <= M; i++) { + int x, y, c; + fin >> x >> y >> c; + edges.push_back({x, y, c}); + } + + sort(edges.begin(), edges.end()); + + long long APM = 0; + + for(auto it : edges) { + if(root(it.x) != root(it.y)) { + join(it.x, it.y); + APM += it.c; + answer.push_back(it); + } + } + + fout << APM << '\n' << N - 1 << '\n'; + for(auto it : answer) + fout << it.x << ' ' << it.y << '\n'; + + return 0; +} diff --git a/mst/kruskal/88efb75c-2613-432a-b90a-1cebb04bada3.cpp b/mst/kruskal/88efb75c-2613-432a-b90a-1cebb04bada3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c87cffd8f3b5df421900e8f4f14ad164c9aa2a --- /dev/null +++ b/mst/kruskal/88efb75c-2613-432a-b90a-1cebb04bada3.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + + + +const int len = 400005; + +int m, n, parent[len], level[len], cost; + +vector > sol; + + + +struct Edge { + + int x, y, c; + +}edge[len]; + + + +bool compare(Edge x, Edge y) { + + return x.c < y.c; + +} + + + +void make_set(int k) { + + parent[k] = k; + + level[k] = 0; + +} + + + +int find_set(int k) { + + if (parent[k] == k) + + return k; + + return parent[k] = find_set(parent[k]); + +} + + + +int union_sets(int x, int y) { + + x = find_set(x); + + y = find_set(y); + + if (x != y) { + + if (level[x] < level[y]) + + swap(x, y); + + parent[x] = y; + + if (level[x] == level[y]) + + level[x]++; + + } + +} + + + +int main() { + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + cin >> n >> m; + + for (int i = 0; i < m; i++) + + cin >> edge[i].x >> edge[i].y >> edge[i].c; + + sort(edge, edge + m, compare); + + for (int i = 0; i < n; i++) + + make_set(i); + + + + for (int i = 0; i < m && sol.size() < n; i++) + + if (find_set(edge[i].x) != find_set(edge[i].y)) { + + cost += edge[i].c; + + union_sets(edge[i].x, edge[i].y); + + sol.push_back({edge[i].x, edge[i].y}); + + } + + + + cout << cost << "\n" << n - 1 << "\n"; + + for (int i = 0; i < n - 1; i++) + + cout << sol[i].first << " " << sol[i].second << "\n"; + +} diff --git a/mst/kruskal/890eab34-5ae4-4bcf-950f-2a281eb65ae1.cpp b/mst/kruskal/890eab34-5ae4-4bcf-950f-2a281eb65ae1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bcd63e8e02e846bddbe8addc6807fbc3701fa523 --- /dev/null +++ b/mst/kruskal/890eab34-5ae4-4bcf-950f-2a281eb65ae1.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + + + +FILE* fin, * fout; + + + +struct edge { + + int x, y, c; + +}; + + + +int n, m; + +std::vector e; + +std::vector apm; + +int d[200005]; + + + +void compress(int x, int r) { + + while (x != r) { + + int t = d[x]; + + d[x] = r; + + x = t; + + } + +} + + + +int root(int x) { + + int r = x; + + for (;r != d[r];r = d[r]); + + + + compress(x, r); + + + + return r; + +} + + + +void combine(int x, int y) { + + int r1 = root(x), r2 = root(y); + + + + d[r1] = r2; + +} + + + +void buildAPM() { + + for (int i = 1;i <= n;++i) + + d[i] = i; + + + + auto cmp = [](edge& e, edge& f) -> bool {return e.c < f.c;}; + + + + sort(e.begin(), e.end(), cmp); + + + + for (auto a : e) { + + if (root(a.x) != root(a.y)) { + + apm.push_back(a); + + combine(a.x, a.y); + + } + + } + + + +} + + + +int main() { + + fin = fopen("apm.in", "r"); + + fout = fopen("apm.out", "w"); + + + + fscanf(fin, "%i %i", &n, &m); + + + + while (m--) { + + int x, y, c; + + fscanf(fin, "%i %i %i", &x, &y, &c); + + e.push_back({ x,y,c }); + + } + + + + buildAPM(); + + + + int c = 0; + + + + for (auto a : apm) + + c += a.c; + + + + fprintf(fout,"%i\n%i\n", c, apm.size()); + + + + for (auto a : apm) + + fprintf(fout,"%i %i\n", a.x, a.y); + + + + return 0; + +} diff --git a/mst/kruskal/895ec992-1c1e-4c1a-8a18-35de2a44a572.cpp b/mst/kruskal/895ec992-1c1e-4c1a-8a18-35de2a44a572.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49d73d85bb2cc12f49fb798fd368ae4416c06645 --- /dev/null +++ b/mst/kruskal/895ec992-1c1e-4c1a-8a18-35de2a44a572.cpp @@ -0,0 +1,196 @@ +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int DIM = 2e5 + 7; + + + +int p[DIM]; + + + +vector < pair > rez; + + + +vector < pair > > v; + + + +struct + +{ + + bool operator()(pair > x,pair > y) + + { + + return x.first < y.first; + + } + +}cmp; + + + +int findp(int x) + +{ + + if(p[x] < 0) + + return x; + + + + return findp(p[x]); + +} + + + +void unionp(int x,int y) + +{ + + if(p[x] < p[y]) + + { + + p[x] = p[x] + p[y]; + + p[y] = x; + + } + + else + + { + + p[y] = p[y] + p[x]; + + p[x] = y; + + } + +} + + + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + + + for(int i = 1; i <= n; i++) + + p[i] = -1; + + + + while(m--) + + { + + int x, y, c; + + in >> x >> y >> c; + + + + v.push_back(make_pair(c,make_pair(x, y))); + + } + + + + int sum = 0; + + + + sort(v.begin(), v.end(), cmp); + + + + int cnt = 0; + + + + for(int i = 0; i < v.size(); i++) + + { + + int nod1 = v[i].second.first; + + int nod2 = v[i].second.second; + + + + int p1 = findp(nod1); + + int p2 = findp(nod2); + + + + if(p1 != p2) + + { + + unionp(p1, p2); + + if(p[p1] < 0) + + { + + rez.push_back({nod1,nod2}); + + } + + else + + rez.push_back({nod2,nod1}); + + sum += v[i].first; + + cnt++; + + if(cnt == n - 1) + + break; + + } + + } + + + + out << sum <<'\n'<< n - 1 <<'\n'; + + + + for(int i = 0; i < rez.size(); i++) + + out << rez[i].first <<" "<< rez[i].second <<'\n'; + + + + return 0; + +} diff --git a/mst/kruskal/8ab230c8-2327-497b-94df-91163378dd63.cpp b/mst/kruskal/8ab230c8-2327-497b-94df-91163378dd63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..027f2f677f61f11cdef0473f6bcd5f3fd5e8b1c3 --- /dev/null +++ b/mst/kruskal/8ab230c8-2327-497b-94df-91163378dd63.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct elem + +{ + + int sursa,destinatie,cost; + +} v[400001]; + + + +elem ans[200002]; + +int dim[200002],tata[400002],n,m,costapm,k; + + + +inline bool cmp(const elem &a,const elem &b) + +{ + + return a.cost>n>>m; + + for(i=1; i<=m; i++) + + fin>>v[i].sursa>>v[i].destinatie>>v[i].cost; + + for(i=1; i<=n; i++) + + { + + dim[i]=1; + + tata[i]=i; + + } + + sort(v+1,v+m+1,cmp); + + for(i=1; i<=m&&k +#include +#include +#include +const int nmax = 2e5 + 5; + + + +int tata[nmax], sz[nmax]; + +std::vector >ans_pairs; + + + +int extreme(int node) { + + int t = node; + + while (tata[t] != t) t = tata[t]; + + tata[node] = t; + + while (tata[node] != node) node = tata[node], tata[node] = t; + + return t; + +} + + + +void connect(int u, int v) { + + int su = extreme(u), sv = extreme(v); + + if (sz[su] < sz[sv]) std::swap(su, sv); + + tata[sv] = su, sz[su] += sz[sv]; + +} + + + +bool is_connected(int u, int v) { + + return extreme(u) == extreme(v); + +} + + + +int main() { + + std::ifstream fin("apm.in"); + + std::ofstream fout("apm.out"); + + int n, m, ans = 0; + + fin >> n >> m; + + std::vector > >l(m); + + for (int i = 0; i < m; i++) fin >> l[i].second.first >> l[i].second.second >> l[i].first; + + for (int i = 1; i <= n; i++) sz[i] = 1, tata[i] = i; + + std::sort(l.begin(), l.end()); + + for(auto a:l) + + if (!is_connected(a.second.first, a.second.second)) { + + ans += a.first; + + connect(a.second.first, a.second.second); + + ans_pairs.push_back(a.second); + + } + + fout << ans << "\n"; + + fout << ans_pairs.size() << "\n"; + + for (auto a : ans_pairs) fout << a.first << " " << a.second << "\n"; + +} diff --git a/mst/kruskal/9125cd6a-87bb-4be0-b7d9-1e263632c5cb.cpp b/mst/kruskal/9125cd6a-87bb-4be0-b7d9-1e263632c5cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53ff757251ca91b457c1936e8cb8cb5c0ee4f8aa --- /dev/null +++ b/mst/kruskal/9125cd6a-87bb-4be0-b7d9-1e263632c5cb.cpp @@ -0,0 +1,182 @@ +#include + + + + +using namespace std; + + + +struct edge { + + int x; + + int y; + + int c; + +}; + + + +bool cmp(const struct edge& edge1, const struct edge& edge2) { + + return edge1.c < edge2.c; + +} + + + +int n, m, cost_apm; + +int root[200010]; + +vector edges; + +vector apm; + + + +void read_input() { + + ifstream fin("apm.in"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= m; ++i) { + + int x, y, c; + + fin >> x >> y >> c; + + + + struct edge to_be_added; + + to_be_added.x = x; + + to_be_added.y = y; + + to_be_added.c = c; + + + + edges.push_back(to_be_added); + + } + + + + fin.close(); + +} + + + +int get_set(int node) { + + if (root[node] != node) { + + root[node] = get_set(root[node]); + + } + + + + return root[node]; + +} + + + +void reunion_set(int x, int y) { + + root[get_set(x)] = get_set(y); + +} + + + +void Kruskal() { + + sort(edges.begin(), edges.end(), cmp); + + + + for (int i = 1; i <= n; ++i) { + + root[i] = i; + + } + + + + cost_apm = 0; + + + + for (auto &edge : edges) { + + if (get_set(edge.x) != get_set(edge.y)) { + + cost_apm += edge.c; + + apm.push_back(edge); + + reunion_set(edge.x, edge.y); + + } + + } + +} + + + +void print_output() { + + ofstream fout("apm.out"); + + + + fout << cost_apm << "\n"; + + fout << apm.size() << "\n"; + + + + for (auto &edge : apm) { + + fout << edge.x << " " << edge.y << "\n"; + + } + + + + fout.close(); + +} + + + + + + + +int main() { + + read_input(); + + Kruskal(); + + print_output(); + + + + return 0; + +} diff --git a/mst/kruskal/930d4991-58ec-4a83-8136-fe7b0f79277c.cpp b/mst/kruskal/930d4991-58ec-4a83-8136-fe7b0f79277c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2632d34e5738d1d65e12bd2343bc101ee289d4aa --- /dev/null +++ b/mst/kruskal/930d4991-58ec-4a83-8136-fe7b0f79277c.cpp @@ -0,0 +1,94 @@ +#include + + + + + + +using namespace std; + + + +int n , m, cost; + +vector> edges, apm; + +int root[200010]; + + + +int get_set(int x) { + + if (x != root[x]) + + root[x] = get_set(root[x]); + + return root[x]; + +} + + + +int main() + +{ + + cost = 0; + + ifstream fin("apm.in"); + + fin >> n >> m; + + for (int i = 1, x, y, c; i <= m; ++i) { + + fin >> x >> y >> c; + + root[i] = i; + + edges.push_back(make_tuple(c, x, y)); + + } + + fin.close(); + + + + sort(edges.begin(), edges.end()); + + for (auto &edge : edges) { + + int c = get<0>(edge), x = get<1>(edge), y = get<2>(edge); + + if (get_set(x) != get_set(y)) { + + cost += c; + + apm.push_back(edge); + + if (apm.size() == n - 1) + + break; + + root[get_set(x)] = get_set(y); + + } + + } + + + + ofstream fout("apm.out"); + + fout << cost << endl << n - 1 << endl; + + for (auto &edge : apm) { + + fout << get<1>(edge) << ' ' << get<2>(edge) << endl; + + } + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/93365372-12d8-4e95-9745-76b6dd5a7f26.cpp b/mst/kruskal/93365372-12d8-4e95-9745-76b6dd5a7f26.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77c5c566242f4f2358f5d7061b76a8affc9f1574 --- /dev/null +++ b/mst/kruskal/93365372-12d8-4e95-9745-76b6dd5a7f26.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector t; + + + +int find(int x) + +{ + + if(t[x] == 0) + + return x; + + t[x] = find(t[x]); + + return t[x]; + +} + + + +void unite(int x, int y) + +{ + + int tx = find(x); + + int ty = find(y); + + t[tx] = ty; + +} + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + + + vector< pair> > muchii; + + muchii.resize(m); + + t.resize(n+1); + + for(int i = 0; i < m; i++) + + { + + int c, x, y; + + fin >> x >> y >> c; + + muchii[i] = {c, {x, y}}; + + } + + + + sort(muchii.begin(), muchii.end()); + + + + int costArb = 0; + + vector> arbore; + + for(auto muchie : muchii) + + { + + int x = muchie.second.first; + + int y = muchie.second.second; + + int cost = muchie.first; + + + + if(find(x) != find(y)) + + { + + arbore.push_back({x, y}); + + costArb += cost; + + + + unite(x, y); + + } + + } + + + + fout << costArb << '\n' << arbore.size() << '\n'; + + for(auto muchie : arbore) + + fout << muchie.first << ' ' << muchie.second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/93ffc702-c2ce-41e7-80ed-302c5f86c521.cpp b/mst/kruskal/93ffc702-c2ce-41e7-80ed-302c5f86c521.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f44012addd3fff409fbf864ce16ddfdc40021fea --- /dev/null +++ b/mst/kruskal/93ffc702-c2ce-41e7-80ed-302c5f86c521.cpp @@ -0,0 +1,223 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +class Edge{ + + + +public: + + + + int first; + + int second; + + + + int price; + +}; + + + +int n, m; + +Edge * lm; + + + +int * t; + + + +vector selected; + +int price_min; + + + +bool comp(Edge i, Edge j){ + + + + if(i.price < j.price) + + return 1; + + + + return 0; + +} + + + +int get_root(int node){ + + + + if(t[node] > 0){ + + + + int root = get_root(t[node]); + + t[node] = root; + + + + return root; + + } + + else + + return node; + +} + + + +bool check(int i){ + + + + int root_first = get_root(lm[i].first); + + int root_second = get_root(lm[i].second); + + + + if(root_first == root_second) + + return 0; + + + + if(t[root_first] <= t[root_second]){ + + + + t[root_first] += t[root_second]; + + t[root_second] = root_first; + + } + + else{ + + + + t[root_second] += t[root_first]; + + t[root_first] = root_second; + + } + + + + return 1; + +} + + + +void Kruskal(){ + + + + sort(lm, lm + m, comp); + + + + for(int i = 0; i < m; i++) + + + + if(check(i)){ + + + + price_min += lm[i].price; + + selected.push_back(lm[i]); + + } + +} + + + +int main(){ + + + + f >> n >> m; + + + + lm = new Edge[m]; + + t = new int[n + 1]; + + + + for(int i = 0; i <= n; i++) + + t[i] = -1; + + + + int x, y, c; + + for(int i = 0; i < m; i++){ + + + + f >> x >> y >> c; + + + + lm[i].first = x; + + lm[i].second = y; + + lm[i].price = c; + + } + + + + Kruskal(); + + + + g << price_min << '\n'; + + g << selected.size() << '\n'; + + + + for(int i = 0; i < selected.size(); i++) + + + + g << selected[i].first << " " << selected[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/94655019-91d4-408d-8e48-8d5afb6eca56.cpp b/mst/kruskal/94655019-91d4-408d-8e48-8d5afb6eca56.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d11c09ac1e22efd8f32988f454a011cfb09ce9a --- /dev/null +++ b/mst/kruskal/94655019-91d4-408d-8e48-8d5afb6eca56.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n, m; + +vector < pair > > edge; + +vector ans; + +int t[200001]; + + + +int Find(int x) + +{ + + int rx = x, y; + + while (t[rx] != 0)rx = t[rx]; + + while (x != rx) + + { + + y = t[x]; + + t[x] = rx; + + x = y; + + } + + return rx; + +} + +void Union(int x, int y) + +{ + + t[y] = x; + +} + +int main() + +{ + + int x, y, c; + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> c; + + edge.push_back({ c, {x, y} }); + + } + + + + sort(edge.begin(), edge.end()); + + int cost = 0; + + for (unsigned int i = 0; i < edge.size(); i++) + + { + + int x = Find(edge[i].second.first); + + int y = Find(edge[i].second.second); + + if (x != y) + + { + + Union(x, y); + + cost += edge[i].first; + + ans.push_back(i); + + } + + } + + fout << cost << "\n"; + + fout << ans.size() << "\n"; + + for (unsigned int i = 0; i < ans.size(); i++) + + fout << edge[ans[i]].second.first << " " << edge[ans[i]].second.second << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/94ed7756-6ef6-4169-9565-ae52c38996c4.cpp b/mst/kruskal/94ed7756-6ef6-4169-9565-ae52c38996c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75d1bdcfbcc2a5edb963abb41180cb33e0501241 --- /dev/null +++ b/mst/kruskal/94ed7756-6ef6-4169-9565-ae52c38996c4.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int NMAX = 2e5 + 3; + +int n, m, ans; + +class Link + +{ + +public: + + int x, y, cost; + + bool operator < (const Link &other) const + + { + + return this->cost < other.cost; + + } + +}; + +Link e[NMAX * 2]; + +int dad[NMAX]; + +bool vis[NMAX * 2]; + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> e[i].x >> e[i].y >> e[i].cost; + +} + + + +int findDad(int node) + +{ + + int ndad = node; + + while (ndad != dad[ndad]) + + ndad = dad[ndad]; + + + + while (node != ndad) + + { + + int aux = dad[node]; + + dad[node] = ndad; + + node = aux; + + } + + return ndad; + +} + + + +void kruskal() + +{ + + sort(e + 1, e + m + 1); + + iota(dad + 1, dad + n + 1, 1); + + for (int i = 1; i <= m; i++) + + { + + int x = findDad(e[i].x); + + int y = findDad(e[i].y); + + if (x != y) + + { + + ans += e[i].cost; + + dad[x] = y; + + vis[i] = true; + + } + + } + + + + fout << ans << '\n' << n - 1 << '\n'; + + for (int i = 1; i <= m; i++) + + if (vis[i]) + + fout << e[i].x << ' ' << e[i].y << '\n'; + +} + + + +int main() + +{ + + read(); + + kruskal(); + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/95227ccc-4722-4dba-b490-2b6389751e71.cpp b/mst/kruskal/95227ccc-4722-4dba-b490-2b6389751e71.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c06a7542784284702db3a6cf76f1faf8f07eb8a --- /dev/null +++ b/mst/kruskal/95227ccc-4722-4dba-b490-2b6389751e71.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int MaxN = 200002; + +const int MaxM = 400004; + + + +struct Muchie{ + + int x, y, cost; + +}mu[MaxM], rez[MaxN]; + +int n, m; + +int Tata[MaxN]; + + + +void Citire(); + +bool Compara(const Muchie &a, const Muchie &b); + +int Radacina(int nod); + +bool Unire(int x, int y); + + + +int main() + +{ + + Citire(); + + + + int total = 0, k = 0; + + for (int i = 1; i <= m; ++i) + + { + + if (Unire(mu[i].x, mu[i].y)) + + { + + rez[++k].x = mu[i].x; + + rez[k].y = mu[i].y; + + total += mu[i].cost; + + } + + } + + + + fout << total << '\n' << k << '\n'; + + for (int i = 1; i <= k; ++i) + + { + + fout << rez[i].x << ' ' << rez[i].y << '\n'; + + } + +} + + + +bool Unire(int x, int y) + +{ + + int rx = Radacina(x), ry = Radacina(y); + + + + if (rx != ry) + + { + + Tata[ry] = rx; + + return true; + + } + + + + return false; + +} + + + +int Radacina(int nod) + +{ + + if (Tata[nod] == 0) return nod; + + else + + { + + int r = Radacina(Tata[nod]); + + Tata[nod] = r; + + return r; + + } + +} + + + +bool Compara(const Muchie &a, const Muchie &b) + +{ + + return (a.cost < b.cost); + +} + + + +void Citire() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> mu[i].x >> mu[i].y >> mu[i].cost; + + } + + + + sort(mu + 1, mu + m + 1, Compara); + +} diff --git a/mst/kruskal/972aee0b-cba8-4c85-b63c-c0ced5401251.cpp b/mst/kruskal/972aee0b-cba8-4c85-b63c-c0ced5401251.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c78023d4590d66285e99b4ee3c49c164aa9f2f --- /dev/null +++ b/mst/kruskal/972aee0b-cba8-4c85-b63c-c0ced5401251.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +ifstream fin( "apm.in" ); +ofstream fout( "apm.out" ); + +const int MaxM = 400002; +const int MaxN = 200002; + +struct graph { + int cost, x, y; +}; + +int M[MaxN], ind[MaxM]; +graph inG[MaxM]; +graph G[MaxN]; +int edges[MaxM][2]; + +int root( int x ) { + if ( M[x] == x ) { + return x; + } + return M[x] = root( M[x] ); +} +void join( int x, int y ) { + M[root( x )] = root( y ); +} + +int cmp( int a, int b ) { + return inG[a].cost < inG[b].cost; +} + +int main() { + int n, m, i, c; + + fin >> n >> m; + for ( i = 0; i < m; ++i ) { + fin >> inG[i].x >> inG[i].y >> inG[i].cost; + ind[i] = i; + M[inG[i].x] = inG[i].x; + M[inG[i].y] = inG[i].y; + } + sort( ind, ind + m, cmp ); + for ( i = 0; i < m; ++i ) { + G[i].cost = inG[ind[i]].cost; + G[i].x = inG[ind[i]].x; + G[i].y = inG[ind[i]].y; + } + c = 0; + int k = 0; + for ( i = 0; i < m; ++i ) { + if ( root( G[i].x ) != root( G[i].y ) ) { + c += G[i].cost; + edges[k][0] = G[i].x; + edges[k++][1] = G[i].y; + join( G[i].x, G[i].y ); + } + } + fout << c << "\n" << k << "\n"; + for ( i = 0; i < k; ++i ) { + fout << edges[i][0] << " " << edges[i][1] << "\n"; + } + fin.close(); + fout.close(); + return 0; +} diff --git a/mst/kruskal/97efa29d-ec58-483d-81a6-4fe3c1059e69.cpp b/mst/kruskal/97efa29d-ec58-483d-81a6-4fe3c1059e69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a164580ede142bd6ef013911224913622d11b8ee --- /dev/null +++ b/mst/kruskal/97efa29d-ec58-483d-81a6-4fe3c1059e69.cpp @@ -0,0 +1,83 @@ +#include +#include +using namespace std; +ifstream f("apm.in"); +ofstream g("apm.out"); +int n,m,costt; +struct vladut +{ + int st,dr,cost; +}mch[400005],sol[200005]; +bool compare(vladut a,vladut b) +{ + return (a.costH[r2]) + { + T[r2]=r1; + } + else + { + T[r1]=r2; + H[r2]++; + } + return 1; + } +} +void kruskal() +{ + int cnt=0,k=1; + while(cnt>n>>m; + for(int i=1;i<=m;i++) + { + f>>mch[i].st>>mch[i].dr>>mch[i].cost; + } + sort(mch+1,mch+m+1,compare); + for(int i=1;i<=n;i++) + { + T[i]=i; + H[i]=1; + } + kruskal(); + g< +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct nod + +{ + + int parent; + + int depth; + + int cost; + +} graf[200005]; + +int n, m; + + + +struct muchie + +{ + + int x, y, c; + + bool operator<(muchie other) + + { + + if(other.c == c) + + return x < other.x; + + return c < other.c; + + } + +} muchii[400005]; + + + +vector > sol; + + + +int getRoot(int node) + +{ + + if(graf[node].parent == 0) + + return node; + + int parent = getRoot(graf[node].parent); + + graf[node].parent = parent; + + return parent; + +} + + + +void read() + +{ + + f>>n>>m; + + int c, x, y; + + for(int i = 0; i>muchii[i].x>>muchii[i].y>>muchii[i].c; + + } + + sort(muchii, muchii+m); + +} + + + +void solve() + +{ + + for(int i = 0; i graf[r2].depth) + + { + + graf[r1].cost += graf[r2].cost + muchii[i].c; + + graf[r2].parent = r1; + + } + + else if(graf[r1].depth < graf[r2].depth) + + { + + graf[r2].cost += graf[r1].cost + muchii[i].c; + + graf[r1].parent = r2; + + } + + sol.push_back({muchii[i].x, muchii[i].y}); + + } + +} + + + +void afisare() + +{ + + g< + + +using namespace std; + + + +const int NMAX = 400005; + +int N, M, TT[NMAX], RG[NMAX], total, k; + +pair P[NMAX]; + + + +struct Edge{ + + int x, y, cost; + + + +}V[NMAX]; + + + +bool cmp(Edge a, Edge b) { + + return a.cost < b.cost; + +} + + + +int Find(int v) { + + if(v == TT[v]) + + return v; + + return TT[v] = Find(TT[v]); + +} + + + +int Unite(int x, int y) { + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[x] > RG[y]) + + TT[y] = x; + + + + if(RG[x] == RG[y]) { + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void Solve() { + + for(int i = 1; i <= M; ++i) + + if(Find(V[i].x) != Find(V[i].y)) { + + Unite(Find(V[i].x), Find(V[i].y)); + + P[++k].first = V[i].x; + + P[k].second = V[i].y; + + total += V[i].cost; + + } + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL), cout.tie(NULL); + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + cin >> N >> M; + + for(int i = 1; i <= M; ++i) + + cin >> V[i].x >> V[i].y >> V[i].cost; + + + + sort(V + 1, V + M + 1, cmp); + + + + for(int i = 1; i <= M; ++i) { + + TT[i] = i; + + RG[i] = 1; + + } + + + + Solve(); + + + + cout << total << "\n" + + << N-1 << "\n"; + + for(int i = 1; i <= k; ++i) + + cout << P[i].first << " " << P[i].second << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/99bf838c-b0cb-4813-853c-abc934cc8860.cpp b/mst/kruskal/99bf838c-b0cb-4813-853c-abc934cc8860.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d2ae570ba6a82dacbe84a10927898e8c20d54de --- /dev/null +++ b/mst/kruskal/99bf838c-b0cb-4813-853c-abc934cc8860.cpp @@ -0,0 +1,162 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int x, y, c; + + + +}v[400005]; + + + +struct elem + +{ + + int x, y; + + + +}a[400005]; + + + +bool cmp(muchie i, muchie j) + +{ + + return i.c < j.c; + +} + + + +int tata[200005], rang[200005], n, m, i, x, y, k, s; + + + +int tata_multime( int nod ) + +{ + + if( tata[nod] != nod ) + + tata[nod] = tata_multime( tata[nod] ); + + return tata[nod]; + +} + + + +void Union( int px, int py ) + +{ + + px = tata_multime( px ); + + py = tata_multime( py ); + + if( px != py ) + + { + + if( rang[px] < rang[py] ) + + { + + rang[py] += rang[px]; + + tata[px] = py; + + } + + else + + { + + rang[px] += rang[py]; + + tata[py] = px; + + } + + } + +} + +int main() + +{ + + f >> n >> m; + + for( i = 1; i <= n; ++ i ) + + { + + tata[i] = i; + + rang[i] = 1; + + } + + for( i = 1 ; i <= m ; ++ i ) + + f >> v[i].x >> v[i].y >> v[i].c; + + sort(v + 1, v + m + 1, cmp); + + for( i = 1; i <= m; ++ i ) + + { + + x = tata_multime( v[i].x ); + + y = tata_multime( v[i].y ); + + if( x != y ) + + { + + Union( x, y ); + + s = s + v[i].c; + + a[++ k].x = v[i].x; + + a[k].y = v[i].y; + + } + + } + + g << s << "\n" << k << "\n"; + + for( i = 1; i <= k; ++ i ) + + g << a[i].x << " " << a[i].y << "\n"; + + return 0; + +} diff --git a/mst/kruskal/9a66f7af-11df-4448-a701-9ceb725c8a77.cpp b/mst/kruskal/9a66f7af-11df-4448-a701-9ceb725c8a77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a5fa3c88f5e073d04b6092869ff47d38acb5a6b --- /dev/null +++ b/mst/kruskal/9a66f7af-11df-4448-a701-9ceb725c8a77.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct edges + +{ + + int x,y; + + short int c; + +}; + + + + + + + +int n,m; + +edges v[400010]; + +int ind[400010],t[200010],gr[200010]; + +int cost,rez[200010]; + + + +void read() + +{ + + fin>>n>>m; + + for(int i=0; i>v[i].x>>v[i].y>>v[i].c; + + ind[i]=i; + + } + +} + + + +int find(int x) + +{ + + int r=x; + + for(; t[r]!=r; r=t[r]) + + ; + + while(t[x]!=x) + + { + + int aux=t[x]; + + t[x]=r; + + x=aux; + + } + + return r; + +} + + + +void reuniune(int x,int y) + +{ + + if(gr[x] + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n, m, cost, p[400005]; + +vector> sol; + + + +struct muchie { + + int a, b, d; + +} v[400005]; + + + +bool cmp(muchie x, muchie y) + +{ + + return x.d < y.d; + +} + + + +int Find(int x) + +{ + + int r=x; + + while(r!=p[r]) + + r=p[r]; + + while(p[x]!=r) + + { + + int tmp=p[x]; + + p[x]=r; + + x=tmp; + + } + + return r; + +} + + + +void Union(int x, int y) + +{ + + int rx=Find(x); + + int ry=Find(y); + + if(rx!=ry) + + p[rx]=ry; + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i=1; i<=m; i++) + + fin >> v[i].a >> v[i].b >> v[i].d; + + sort(v+1, v+m+1, cmp); + + for(int i=1; i<=n; i++) + + p[i]=i; + + for(int i=1; i<=m && sol.size()<(n-1); i++) + + if(Find(v[i].a)!=Find(v[i].b)) + + { + + cost+=v[i].d; + + Union(v[i].a, v[i].b); + + sol.push_back({v[i].a, v[i].b}); + + } + + fout << cost << "\n" << n-1 << "\n"; + + for(int i=0; i + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int n,m,p[400005],h[400005]; + +vector>ans; + +struct muchie + +{ + + int x,y,c; + +}v[400005]; + +bool cmp(muchie a,muchie b) + +{ + + return a.ch[ry]) p[ry]=rx; + + else if(h[rx]>n>>m; + + for(int i=1;i<=m;i++) + + in>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + out< +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int MMAX = 400000; + +const int NMAX = 200000; + + + +struct Edge + +{ + + int x, y, z; + +}; + + + +struct Parents + +{ + + int root, siz; + +}p[NMAX + 4]; + + + +inline bool cmp(const Edge &a, const Edge &b) + +{ + + if(a.z == b.z) + + return a.x < b.x; + + return a.z < b.z; + +} + + + +vector v; + +Edge a[NMAX + 5]; + + + +struct Padure + +{ + + int Find(int x) + + { + + if(p[x].root == x) + + return x; + + p[x].root = Find(p[p[x].root].root); + + return p[x].root; + + } + + + + void Unite(int x, int y) + + { + + y = Find(y); + + x = Find(x); + + if(p[x].siz > p[y].siz) + + p[x].root = y; + + else if (p[x].siz < p[y].siz) + + p[y].root = x; + + else + + { + + p[x].root = y; + + p[x].siz++; + + } + + } + + + +}Pad; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + int x, y, z; + + fin >> x >> y >> z; + + v.push_back({x, y, z}); + + } + + sort(v.begin(), v.end(), cmp); + + + + for(int i = 1; i <= n; i++) + + { + + p[i].root = i; + + p[i].siz = 1; + + } + + + + int r = 0; + + for(int i = 0; i < m; i++) + + if(Pad.Find(v[i].x) != Pad.Find(v[i].y)) + + { + + Pad.Unite(v[i].x, v[i].y); + + a[++r] = v[i]; + + } + + + + int cost = 0; + + + + for(int i = 1; i <= r; i++) + + { + + cost += a[i].z; + + } + + + + fout << cost << '\n' << r << '\n'; + + + + for(int i = 1; i <= r; i++) + + fout << a[i].y << ' ' << a[i].x << '\n'; + + return 0; + +} diff --git a/mst/kruskal/9cbb3372-5936-4044-8b61-fa2b8eeddb34.cpp b/mst/kruskal/9cbb3372-5936-4044-8b61-fa2b8eeddb34.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3de2847c04fa9d7c306e461c009c5bb0263dadca --- /dev/null +++ b/mst/kruskal/9cbb3372-5936-4044-8b61-fa2b8eeddb34.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct muchie + +{ + + int x, y, c; + +}; + +struct cmp + +{ + + bool operator()(const muchie& a, const muchie& b) + + { + + return a.c > b.c; + + } + +}; + +priority_queue, cmp> q; + +vector> sol; + +void read(); + +int n, m, rez, t[200005]; + +int rad(int x) + +{ + + if(t[x]!=x) + + return (t[x]=rad(t[x])); + + return t[x]; + +} + +void unire(int a, int b, int c) + +{ + + int r1 = rad(a), r2 = rad(b); + + if(r1!=r2) + + { + + if(rand()%2) + + t[r1] = r2; + + else t[r2] = r1; + + rez+=c; + + sol.push_back({a, b}); + + } + +} + +int main() + +{ + + read(); + + srand(time(NULL)); + + for(int i=1; i<=n; i++) + + t[i] = i; + + while(!q.empty()) + + { + + muchie cur = q.top(); + + q.pop(); + + unire(cur.x, cur.y, cur.c); + + } + + fout << rez << '\n'; + + fout << n-1 << '\n'; + + for(auto it:sol) + + fout << it.first << ' ' << it.second << '\n'; + + return 0; + +} + +void read() + +{ + + fin >> n >> m; + + for(int i=1; i<=m; i++) + + { + + muchie w; + + fin >> w.x >> w.y >> w.c; + + q.push(w); + + } + +} diff --git a/mst/kruskal/9eb0e2fa-9d6a-4cad-9c82-24b0db3e81b6.cpp b/mst/kruskal/9eb0e2fa-9d6a-4cad-9c82-24b0db3e81b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6147763fb22ff4560eaffb2735a3b1d0900e7003 --- /dev/null +++ b/mst/kruskal/9eb0e2fa-9d6a-4cad-9c82-24b0db3e81b6.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int DIM = 200005; + + + +int n,m,t[DIM],rg[DIM]; + + + +long long ans=0; + + + +struct Tuple + +{ + + int x,y,c; + +}P[DIM]; + + + +vector > M; + + + +bool Compare(Tuple a, Tuple b) + +{ + + return (a.c>n>>m; + + for(int i=1;i<=m;i++) + + fin>>P[i].x>>P[i].y>>P[i].c; + + for(int i=1;i<=n;i++) + + { + + t[i]=i; + + rg[i]=1; + + } + + sort(P+1,P+1+m,Compare); + +} + + + +void Solve() + +{ + + for(int i=1;i<=m;i++) + + { + + int a=P[i].x; + + int b=P[i].y; + + if(Find(a)!=Find(b)) + + { + + ans+=P[i].c; + + M.push_back(make_pair(a,b)); + + Union(Find(a),Find(b)); + + } + + } + +} + + + +void Print() + +{ + + fout< +#include +#include + + +using namespace std; + + + + + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct graph + +{ + + int x,y; + + short int c; + +}; + + + + + + + +int n,m; + +graph v[400010]; + +int ind[400010],t[200010],gr[200010]; + +int cost,rez[200010]; + + + +void read() + +{ + + fin>>n>>m; + + for(int i=0; i>v[i].x>>v[i].y>>v[i].c; + + ind[i]=i; + + } + +} + +int find(int x) + +{ + + int r=x; + + for(; t[r]!=r; r=t[r]) + + ; + + while(t[x]!=x) + + { + + int aux=t[x]; + + t[x]=r; + + x=aux; + + } + + return r; + +} + +void reuniune(int x,int y) + +{ + + if(gr[x] + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int Nmax = 2e5 + 5; + +const int Mmax = 4e5 + 5; + + + +int n, m; + +int t[Nmax]; + + + +pair > muchii[Mmax]; + +pair sol[Mmax]; + + + +void Union(int x, int y) + +{ + + t[y] = x; + +} + + + +int Find(int x) + +{ + + int y, rad = x; + + + + while(t[rad]) + + rad = t[rad]; + + + + while(x != rad) + + { + + y = t[x]; + + t[x] = rad; + + x = y; + + } + + + + return rad; + +} + + + +int main() + +{ + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) + + fin >> muchii[i].second.first >> muchii[i].second.second >> muchii[i].first; + + + + sort(muchii + 1, muchii + m + 1); + + int x, y, lg, cost; + + + + cost = lg = 0; + + for(int i = 1; i <= m; i++) + + { + + x = Find(muchii[i].second.first); + + y = Find(muchii[i].second.second); + + + + if(x != y) + + { + + cost += muchii[i].first; + + lg++; + + sol[lg] = muchii[i].second; + + + + Union(x, y); + + } + + } + + + + fout << cost << "\n" << lg << "\n"; + + + + for(int i = 1; i <= lg; i++) + + fout << sol[i].first << " " << sol[i].second << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/a092033b-9f2b-44fa-aa22-f41c9386962f.cpp b/mst/kruskal/a092033b-9f2b-44fa-aa22-f41c9386962f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbc7089a69f368f8ca4b44c4aad03faeb0a176cc --- /dev/null +++ b/mst/kruskal/a092033b-9f2b-44fa-aa22-f41c9386962f.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +typedef long long ll; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int NMAX = 4e5 + 5; + +int t[NMAX], r[NMAX], total; + +vector> er; + + + +struct edge{ + + int x, y, p; + +}v[NMAX]; + + + +bool srt(edge a, edge b){return a.p < b.p;} + + + +int FindT(int nod) + +{ + + while(t[nod] != nod) + + nod = t[nod]; + + return nod; + +} + + + +void UN(int nx, int ny) + +{ + + if(r[nx] < r[ny]) t[nx] = ny; + + else if(r[nx] > r[ny]) t[ny] = nx; + + else t[nx] = ny, ++r[ny]; + +} + + + +int main() + +{ + + ios::sync_with_stdio(false); + + fin.tie(); + + + + int n, m; + + + + fin >> n >> m; + + + + for(int i = 1; i <= m; ++i) + + fin >> v[i].x >> v[i].y >> v[i].p; + + for (int i = 1; i <= m; ++i) + + t[i] = i, r[i] = 1; + + + + sort(v + 1, v + m + 1, srt); + + + + for(int i = 1; i <= m; ++i) + + { + + int auxf1 = FindT(v[i].x), auxf2 = FindT(v[i].y); + + if(auxf1 != auxf2) + + { + + UN(auxf1, auxf2); + + er.push_back({v[i].x, v[i].y}); + + total += v[i].p; + + } + + } + + + + fout << total << "\n" << er.size() << "\n"; + + + + for (auto i : er) + + fout << i.second << " " << i.first << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/a1280547-737d-42ae-95a5-0ecf98cf3a14.cpp b/mst/kruskal/a1280547-737d-42ae-95a5-0ecf98cf3a14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..801d2f1ec12db840d04dcf992e08a11f43c73b6e --- /dev/null +++ b/mst/kruskal/a1280547-737d-42ae-95a5-0ecf98cf3a14.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +typedef tuple T; + +typedef pair P; + + + +void read(int& n, int& m, vector& utak,vector

& fa) { + + ifstream fin("apm.in"); + + fin >> n >> m; + + utak.resize(m + 1); + + fa.resize(m + 1); + + for (int i = 1; i <= m; i++) { + + int a, b, c; + + fin >> a >> b >> c; + + utak[i] = make_tuple(c, a, b); + + } + +} + + + +void init(int n, vector

& fa) { + + for (int i = 1; i <= n; i++) { + + fa[i].first = i; + + fa[i].second = 1; + + } + +} + + + +int keres(int k, vector

& fa) { + + if (fa[k].first == k) + + return k; + + fa[k].first = keres(fa[k].first, fa); + + return fa[k].first; + +} + + + +void kiir(int sum, vector eredmeny, vector utak) { + + ofstream fout("apm.out"); + + fout << sum << "\n"; + + fout << eredmeny.size() << "\n"; + + for (auto& i : eredmeny) { + + fout << get<1>(utak[i]) << " " << get<2>(utak[i]) << "\n"; + + } + +} + + + +int main() { + + int n, m; + + vector utak; + + vector

fa; + + read(n, m, utak, fa); + + sort(utak.begin()+1, utak.end()); + + init(n, fa); + + vector eredmeny; + + int sum = 0; + + for (int i = 1; i <= m; i++) { + + int csp1 = keres(get<1>(utak[i]), fa); + + int csp2 = keres(get<2>(utak[i]), fa); + + if (csp1 != csp2) { + + eredmeny.push_back(i); + + sum += get<0>(utak[i]); + + if (fa[csp2].second < fa[csp1].second) { + + fa[csp2].first = csp1; + + fa[csp1].second += fa[csp2].second; + + } + + else { + + fa[csp1].first = csp2; + + fa[csp2].second += fa[csp1].second; + + } + + } + + } + + kiir(sum, eredmeny, utak); + +} diff --git a/mst/kruskal/a14cfab0-74d1-48db-aada-173ca1e99dc2.cpp b/mst/kruskal/a14cfab0-74d1-48db-aada-173ca1e99dc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cd427e34b70731055bad0e8ab591a5cd058b40b --- /dev/null +++ b/mst/kruskal/a14cfab0-74d1-48db-aada-173ca1e99dc2.cpp @@ -0,0 +1,232 @@ +#include +#include +#include + + + + +using namespace std; + + + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + + + +struct Edge { + + uint32_t node1; + + uint32_t node2; + + int16_t cost; + +}; + + + + + +void Read( + + uint32_t &n, + + vector &edges + +) { + + in >> n; + + + + uint32_t m; + + uint32_t node1; + + uint32_t node2; + + int16_t cost; + + + + for (in >> m; m; --m) { + + in >> node1; + + in >> node2; + + in >> cost; + + + + edges.push_back({node1 - 1, node2 - 1, cost}); + + } + +} + + + + + +inline bool Compare( + + Edge const &edge1, + + Edge const &edge2 + +) { + + return edge1.cost < edge2.cost; + +} + + + + + +inline uint32_t Find( + + vector &parent, + + uint32_t const node + +) { + + if (parent[node] == node) { + + return node; + + } + + + + parent[node] = Find(parent, parent[node]); + + + + return parent[node]; + +} + + + + + +inline void Union( + + vector &parent, + + uint32_t const node1, + + uint32_t const node2 + +) { + + parent[Find(parent, node1)] = Find(parent, node2); + +} + + + + + +void Solve( + + uint32_t const n, + + vector &edges + +) { + + sort(edges.begin(), edges.end(), Compare); + + + + vector parent(n); + + + + for (uint32_t i = 0; i < n; ++i) { + + parent[i] = i; + + } + + + + int32_t sum = 0; + + vector indices; + + + + for (uint32_t i = 0; i < edges.size(); ++i) { + + if (Find(parent, edges[i].node1) != Find(parent, edges[i].node2)) { + + sum += edges[i].cost; + + indices.push_back(i); + + + + Union(parent, edges[i].node1, edges[i].node2); + + } + + } + + + + out << sum << '\n'; + + out << n - 1 << '\n'; + + + + for (uint32_t i = 0; i < indices.size(); ++i) { + + out << edges[indices[i]].node1 + 1 << ' '; + + out << edges[indices[i]].node2 + 1 << '\n'; + + } + +} + + + + + +int main() { + + uint32_t n; + + vector edges; + + + + Read(n, edges); + + + + Solve(n, edges); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/mst/kruskal/a1c85320-ddd2-4f05-907a-46b24ee5c9f3.cpp b/mst/kruskal/a1c85320-ddd2-4f05-907a-46b24ee5c9f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..210b6493fbce8943248bd1e00e352ff08a0ab95a --- /dev/null +++ b/mst/kruskal/a1c85320-ddd2-4f05-907a-46b24ee5c9f3.cpp @@ -0,0 +1,133 @@ +#include +#include + + + + +using namespace std; + + + +struct edge + +{ + + int x, y, cost; + +}; + + + +pair < int, int > sol[200003]; + +int t[200003], n, m, k; + +edge a[2 * 200003]; + + + +void Union(int x, int y) + +{ + + t[x] = y; + +} + + + +int Find(int x) + +{ + + int root, y; + + root = x; + + while(t[root]) + + root = t[root]; + + while(x != root) + + { + + y = t[x]; + + t[x] = root; + + x = y; + + } + + return root; + +} + + + +inline bool Compare(edge A, edge B) + +{ + + return A.cost < B.cost; + +} + + + +int main() + +{ + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + int p, q, cnt, minimum = 0; + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + fin >> a[i].x >> a[i].y >> a[i].cost; + + fin.close(); + + sort(a + 1, a + m + 1, Compare); + + for(int i = 1; i <= m; i++) + + { + + p = Find(a[i].x); + + q = Find(a[i].y); + + if(p != q) + + { + + minimum += a[i].cost; + + sol[++k].first = a[i].x; + + sol[k].second = a[i].y; + + Union(p, q); + + } + + } + + fout << minimum << "\n" << k << "\n"; + + for(int i = 1; i <= k; i++) + + fout << sol[i].first << " " << sol[i].second << "\n"; + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/a2540c47-b472-4beb-ae47-6332f32bd76b.cpp b/mst/kruskal/a2540c47-b472-4beb-ae47-6332f32bd76b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea1ee1b6dc64e5748768edffcf445abf621be974 --- /dev/null +++ b/mst/kruskal/a2540c47-b472-4beb-ae47-6332f32bd76b.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int NMAX = 200005; + +const int MMAX = 400005; + + + +int N,M,Total; + +int TT[NMAX],RG[NMAX]; + + + +pair < int, int > PP[MMAX]; + +int k; + + + +struct Muchie{ + + int x,y,Cost; + +}V[MMAX]; + + + +bool Compare(Muchie a, Muchie b){ + + return a.Cost < b.Cost; + +} + + + +void Read(){ + + f >> N >> M; + + for(int i = 1; i <= M;i++) + + f >> V[i].x >> V[i].y >> V[i].Cost; + + + + sort (V + 1, V + M + 1, Compare); + + + + for(int i = 1; i <= N;i++){ + + TT[i] = i; + + RG[i]= 1; + + } + + + +} + + + +int Find(int Nod){ + + while(TT[Nod] != Nod) + + Nod = TT[Nod]; + + return Nod; + +} + + + +void Unire(int x, int y){ + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[y] < RG[x]) + + TT[y] = x; + + if(RG[x] == RG[y]){ + + TT[x] = y; + + RG[y]++; + + } + +} + + + +void apm(){ + + for(int i = 1; i <= M;i++){ + + int Tx = Find(V[i].x); + + int Ty = Find(V[i].y); + + if(Tx!=Ty){ + + Unire(Tx,Ty); + + PP[++k].first = V[i].x; + + PP[k].second = V[i].y; + + Total += V[i].Cost; + + } + + } + +} + + + +void afis(){ + + g << Total << "\n"; + + g << k << "\n"; + + for(int i = 1; i<= k;i++) + + g << PP[i].first <<" " << PP[i].second << "\n"; + +} + + + +int main() + +{ + + Read(); + + apm(); + + afis(); + + return 0; + +} diff --git a/mst/kruskal/a2a38ea9-3284-4d66-bfd8-1a06f5744eb5.cpp b/mst/kruskal/a2a38ea9-3284-4d66-bfd8-1a06f5744eb5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70cd8ec8082ef8c003419e4b8f70d423b835a05b --- /dev/null +++ b/mst/kruskal/a2a38ea9-3284-4d66-bfd8-1a06f5744eb5.cpp @@ -0,0 +1,102 @@ +#include +#include +#include + +using namespace std; + +struct Edge +{ + int x, y, w; +}; + +bool compEdge(Edge a, Edge b) +{ + return a.w < b.w; +} + +pair> readGraph(const string& inputPath) +{ + ifstream fin(inputPath); + int n, m; + fin >> n >> m; + vector edges(m); + for (int i = 0; i < m; i++) + { + fin >> edges[i].x >> edges[i].y >> edges[i].w; + edges[i].x--; + edges[i].y--; + } + fin.close(); + return make_pair(n, edges); +} + +int getRoot(const vector& parents, int node) +{ + while (parents[node] != node) + node = parents[node]; + return node; +} + +void setRoot(vector& parents, int node, int root) +{ + int aux; + while (parents[node] != node) + { + aux = parents[node]; + parents[node] = root; + node = aux; + } + parents[node] = root; +} + +bool join(vector& parents, int x, int y) +{ + int rootX, rootY; + rootX = getRoot(parents, x); + rootY = getRoot(parents, y); + bool needsJoin = rootX != rootY; + if (needsJoin) + { + parents[rootY] = rootX; + } + setRoot(parents, x, rootX); + setRoot(parents, y, rootX); + return needsJoin; +} + +vector computeMST(vector& edges, int n) +{ + vector parents(n); + for (int i = 0; i < n; i++) + parents[i] = i; + sort(edges.begin(), edges.end(), compEdge); + vector mst; + for (auto& edge: edges) + { + if (join(parents, edge.x, edge.y)) + { + mst.push_back(edge); + } + } + return mst; +} + +void printMST(const string& outputPath, const vector& edges) +{ + ofstream fout(outputPath); + int cost = 0; + for (auto& edge: edges) + cost += edge.w; + fout << cost << '\n' << edges.size() << '\n'; + for (auto& edge: edges) + fout << edge.x + 1 << ' ' << edge.y + 1 << '\n'; + fout.close(); +} + +int main() +{ + pair> graphData = readGraph("apm.in"); + vector mst = computeMST(graphData.second, graphData.first); + printMST("apm.out", mst); + return 0; +} diff --git a/mst/kruskal/a3c620ff-3801-4c0c-accc-2879177f1ed3.cpp b/mst/kruskal/a3c620ff-3801-4c0c-accc-2879177f1ed3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83bb10d86b8470cead729c9a12fd9a05e06650c1 --- /dev/null +++ b/mst/kruskal/a3c620ff-3801-4c0c-accc-2879177f1ed3.cpp @@ -0,0 +1,312 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int nmax = 200069; + +int n , m ; + +int par[nmax] , nr[nmax]; + +long long r; + +int cont; + +int x[nmax] , y[nmax]; + +struct muchie + +{ + + int x; + + int y; + + int c; + +}; + + + +muchie v[nmax]; + + + + + + + +inline void readInput() + +{ + + f>>n>>m; + + for(int i=1 ; i <= m ; i++) + + { + + f>>v[i].x>>v[i].y>>v[i].c; + + } + +} + + + +bool comp(muchie a , muchie b) + +{ + + if(a.c < b.c) + + { + + return true; + + } + + else + + { + + if(a.c == b.c) + + { + + if(a.x < b.x) return true; + + else if( a.x == b.x) + + { + + if(a.y< b.y) return true; + + } + + } + + } + + return false; + +} + + + +void uneste(int x, int y) + +{ + + int x2=x; + + int y2=y; + + while(x2!=par[x2]) + + { + + x2=par[x2]; + + } + + while(y2!=par[y2]) + + { + + y2=par[y2]; + + } + + if(nr[x2] +#include +#include + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +const int NMAX = 200000; + +struct Edge +{ + int x, y, c; + + bool operator < (const Edge other) const + { + return c < other.c; + } +}; + +int N, M; +int dad[NMAX + 5], rnk[NMAX + 5]; + +vector < Edge > edges; +vector < Edge > apmEdges; + +int root(int node) +{ + if(node == dad[node]) + return node; + + return dad[node] = root(dad[node]); +} + +void join(int p, int q) +{ + p = root(p); + q = root(q); + + if(rnk[p] == rnk[q]) + rnk[p]++, dad[q] = p; + else if(rnk[p] > rnk[q]) + dad[q] = p; + else + dad[p] = q; +} + +int main() +{ + fin >> N >> M; + + for(int i = 1; i <= N; i++) + dad[i] = i; + + for(int i = 1; i <= M; i++) + { + int from, to, cost; fin >> from >> to >> cost; + edges.push_back({from, to, cost}); + } + + sort(edges.begin(), edges.end()); + + long long APM = 0; + + for(auto it : edges) + if(root(it.x) != root(it.y)) + { + join(it.x, it.y); + APM += it.c; + apmEdges.push_back(it); + } + + fout << APM << '\n' << N - 1 << '\n'; + + for(auto it : apmEdges) + fout << it.x << ' ' << it.y << '\n'; + + return 0; +} diff --git a/mst/kruskal/a5994cc7-da51-4193-93c6-5133556e1ee3.cpp b/mst/kruskal/a5994cc7-da51-4193-93c6-5133556e1ee3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44c3a89fcf7957ab7747d40c12d494f338053a9c --- /dev/null +++ b/mst/kruskal/a5994cc7-da51-4193-93c6-5133556e1ee3.cpp @@ -0,0 +1,252 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +typedef struct PAIR{ + + + + int muchieX, muchieY, costMuchie; + + + +}PAIR; + + + +PAIR P; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int nrNoduri, nrMuchii, costArborePartial, nrMuchiiArborePartial, vectorTati[500000], rang[500000]; + +vector muchii; + +bool vizitatMuchii[500000]; + + + +int Radacina(int nod){ + + + + int radacinaNod = nod; + + + + while ( vectorTati[radacinaNod] != radacinaNod ) + + radacinaNod = vectorTati[radacinaNod]; + + + + while ( vectorTati[nod] != nod ){ + + int aux = vectorTati[nod]; + + vectorTati[nod] = radacinaNod; + + nod = aux; + + } + + + + return radacinaNod; + + + +} + + + +void Unire(int radacinaX, int radacinaY){ + + + + if ( radacinaX != radacinaY ){ + + + + if(rang[radacinaX] > rang[radacinaY]) + + vectorTati[radacinaY] = radacinaX; + + + + else + + vectorTati[radacinaX] = radacinaY; + + + + + + if( rang[radacinaX] == rang[radacinaY] ) + + rang[radacinaY]++; + + } + + + +} + + + +int cmp(PAIR X, PAIR Y){ + + + + return X.costMuchie < Y.costMuchie; + + + +} + + + +void kruskal(){ + + + + int nrComponenteConexe = nrNoduri; + + + + for ( int i = 1; i <= nrNoduri; i++){ + + vectorTati[i] = i; + + rang[i] = 1; + + } + + + + costArborePartial = 0 , nrMuchiiArborePartial = 0; + + + + int lim = muchii.size(); + + + + for(int i = 0; i < lim && nrComponenteConexe > 1 ; i++){ + + + + int radacinaX = Radacina(muchii[i].muchieX) , radacinaY = Radacina(muchii[i].muchieY); + + + + if ( radacinaX != radacinaY ){ + + + + vizitatMuchii[i] = true; + + + + costArborePartial += muchii[i].costMuchie; + + nrMuchiiArborePartial++; + + + + Unire(radacinaX,radacinaY); + + + + nrComponenteConexe--; + + } + + } + +} + + + +int main(){ + + + + if ( !fin ){ + + cout << "\nEroare la deschiderea fisierului !\n"; + + exit(-1); + + } + + + + fin >> nrNoduri >> nrMuchii; + + + + for (int i = 0; i < nrMuchii; i++) { + + + + fin >> P.muchieX >> P.muchieY >> P.costMuchie; + + + + muchii.push_back(P); + + + + vizitatMuchii[i] = false; + + + + } + + + + sort(muchii.begin(),muchii.end(),cmp); + + + + kruskal(); + + + + fout << costArborePartial << "\n" << nrMuchiiArborePartial << "\n"; + + + + for ( int i = 0; i < nrMuchii; i++) + + if ( vizitatMuchii[i] ) + + fout << muchii[i].muchieX << " " << muchii[i].muchieY <<"\n"; + + + + + + return 0; + + + +} diff --git a/mst/kruskal/a70be145-1870-4738-8cc4-435e0d1471a8.cpp b/mst/kruskal/a70be145-1870-4738-8cc4-435e0d1471a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..584caae3290f228bff83545b98d520ab80987147 --- /dev/null +++ b/mst/kruskal/a70be145-1870-4738-8cc4-435e0d1471a8.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< pair >apm; + + + +int n, m, t[200001], s; + + + +struct muchii + +{ + + int x, y, cost; + +}; + + + +muchii lm[400001]; + + + +bool cmp(muchii a, muchii b) + +{ + + return a.cost> n >> m; + + + + for(int i = 0; i < m; i++) + + f >> lm[i].x >> lm[i].y >> lm[i].cost; + + + + sort(lm, lm + m, cmp); + + + + for(int i = 1; i <= n; i++) + + t[i] = i; + + + + for(int i = 0; i < m; i++) + + if(rep(lm[i].x) != rep(lm[i].y)) + + { + + t[rep(lm[i].y)] = lm[i].x; + + s += lm[i].cost; + + apm.push_back ( make_pair(lm[i].x,lm[i].y)); + + } + + + + g< +using namespace std; +struct DS +{ + int n; + vector link, cnt; + DS(int n) : n(n), cnt(n, 1), link(n, -1) {} + int root(int a) + { + while (link[a] != -1) + a = link[a]; + return a; + } + + bool Connected(int a, int b) + { + return root(a) == root(b); + } + + void Link(int a, int b) + { + a = root(a); + b = root(b); + if (cnt[a] > cnt[b]) + swap(a, b); + link[a] = b; + cnt[b] += cnt[a]; + } +}; + +struct Graph +{ + struct Edge + { + int a, b, c; + Edge(int a, int b, int c) : a(a), b(b), c(c) {} + bool operator<(const Edge &oth) const + { + return c < oth.c; + } + }; + + int n; + vector edges; + vector mst_edges; + + Graph(int n) : n(n) {} + + void AddEdge(int a, int b, int c) + { + edges.emplace_back(a, b, c); + } + + long long ComputeMST() + { + + sort(edges.begin(), edges.end()); + + + DS ds(n); + long long answer = 0; + for (auto e : edges) + { + + + if (!ds.Connected(e.a, e.b)) + { + answer += e.c; + + ds.Link(e.a, e.b); + mst_edges.push_back(e); + } + } + return answer; + } +}; + +int main() +{ + ifstream cin("apm.in"); + ofstream cout("apm.out"); + + int n, m; + cin >> n >> m; + Graph G(n); + for (int i = 0; i < m; ++i) + { + int a, b, c; + cin >> a >> b >> c; + --a; + --b; + G.AddEdge(a, b, c); + } + cout << G.ComputeMST() << endl; + cout << G.mst_edges.size() << endl; + for (auto e : G.mst_edges) + { + cout << e.a + 1 << " " << e.b + 1 << '\n'; + } + return 0; +} diff --git a/mst/kruskal/a78baa11-7a52-40d8-8e4f-885295726544.cpp b/mst/kruskal/a78baa11-7a52-40d8-8e4f-885295726544.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f98588bcac9706b4d3ddfb090643e6fe2d7ebc5a --- /dev/null +++ b/mst/kruskal/a78baa11-7a52-40d8-8e4f-885295726544.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; +int father[100005], group[100005], n, m, answer; + +vector< pair > > edges; + +vector< pair > apm; + + + +int findFather(int node) { + + if(node == father[node]) + + return node; + + int new_father = findFather(father[node]); + + father[node] = new_father; + + return new_father; + +} + + + +int main () { + + int a, b, cost; + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d%d", &a, &b, &cost); + + edges.push_back(make_pair(cost, make_pair(a, b))); + + } + + sort(edges.begin(), edges.end()); + + + + for(int i = 1; i <= n; i++) { + + father[i] = i; + + group[i] = 1; + + } + + + + for(auto edge : edges) { + + int a = edge.second.first; + + int b = edge.second.second; + + + + int fa = findFather(a); + + int fb = findFather(b); + + + + if(fa == fb) { + + continue; + + } + + + + apm.push_back(edge.second); + + answer += edge.first; + + + + if(group[fa] > group[fb]) { + + father[fb] = fa; + + group[fa] += group[fb]; + + } + + else { + + father[fa] = fb; + + group[fb] += group[fa]; + + } + + } + + + + printf("%d\n%d\n", answer, n - 1); + + for(auto edge : apm) { + + printf("%d %d\n", edge.first, edge.second); + + } + + + + return 0; + +} diff --git a/mst/kruskal/a79f933d-19a4-48ac-bbf1-3f04c19cffa4.cpp b/mst/kruskal/a79f933d-19a4-48ac-bbf1-3f04c19cffa4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a641bc78cc1ee080a05dbd6260b9203756cd33c2 --- /dev/null +++ b/mst/kruskal/a79f933d-19a4-48ac-bbf1-3f04c19cffa4.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + + +using namespace std; + + + +struct ura { + + int st, dr, cost; + + bool corect; + +}; + + + +ura v[400005], sol[400005]; + +int sef[200005]; + + + +bool cmp(ura a, ura b) { + + if(a.cost +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N_MAX = 2e5 + 5; + +struct ura { + + int x, y, c; + + bool operator<(const ura &other)const { + + return cv, r; + + + +int tata(int nodd) { + + if(nodd==t[nodd]) + + return nodd; + + return t[nodd]=tata(t[nodd]); + +} + + + +void tacpac(int x, int y) { + + x=tata(x); + + y=tata(y); + + if(p[x]==p[y]) { + + p[x]++; + + t[y]=x; + + } + + else + + if(p[x]>p[y]) + + t[y]=x; + + else + + t[x]=y; + +} + +int main() { + + fin>>n>>m; + + for(int i=1; i<=n; i++) { + + t[i]=i; + + } + + for(int i=1; i<=m; i++) { + + int x, y, c; + + fin>>x>>y>>c; + + v.push_back({x, y, c}); + + } + + + + sort(v.begin(), v.end()); + + long long coost=0; + + for(auto it : v) { + + if(tata(it.x)!=tata(it.y)) { + + tacpac(it.x, it.y); + + coost+=it.c; + + r.push_back(it); + + } + + } + + fout< +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct ura{ + + int x,y,c; + +}v[400001],a[200001]; + +int sef[200001]; + + + +bool cmp(ura a,ura b){ + + return a.c>n>>m; + + for(i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + sef[i]=i; + + sort(v+1,v+m+1,cmp); + + for(i=1;i<=m && k<=n-1;i++) + + if(sefsuprem(v[i].x)!=sefsuprem(v[i].y)){ + + a[k]=v[i]; + + sef[sefsuprem(v[i].y)]=sefsuprem(v[i].x); + + s=s+v[i].c; + + k++; + + } + + fout< + + +using namespace std; + +ifstream r("apm.in"); + +ofstream w("apm.out"); + +vector>cost, g, rez; + +int v[200002], len[200002]; + +int root(int a) + +{ + + if(v[a]==a) + + { + + return a; + + } + + return v[a]=root(v[a]); + +} + +int main() + +{ + + int n, m; + + r>>n>>m; + + for(int i=0; i>x>>y>>c; + + g.push_back(make_pair(x, y)); + + cost.push_back(make_pair(c, i)); + + } + + sort(cost.begin(), cost.end()); + + for(int i=1; i<=n; i++) + + { + + v[i]=i; + + len[i]=1; + + } + + long long sum=0; + + for(int i=0;ib){ + + swap(a, b); + + } + + if(len[a]==len[b]){ + + len[a]++; + + } + + v[b]=a; + + } + + w< +#include +#include +using namespace std; + + + +const int NMAX = 400000; + + + +struct muchie + +{ + + int u, v, c, pus; + +}; + + + +muchie x[NMAX + 5]; + +int t[200005], h[200005]; + + + +int varf(int x) + +{ + + while(t[x] != x) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if(h[x] > h[y]) + + t[y] = x; + + else + + { + + if(h[x] < h[y]) + + t[x] = y; + + else + + { + + t[y] = x; + + ++h[x]; + + } + + } + +} + + + +bool cmp(muchie a, muchie b) + +{ + + if(a.c==b.c) + + { + + if(a.u == b.u) + + return a.v>n>>m; + + for(i = 1; i <= n; ++i) + + t[i] = i, h[i] = 1; + + for(i = 1; i <= m; ++i) + + { + + fin>>a>>b>>cost; + + x[i].u = a; + + x[i].v = b; + + x[i].c = cost; + + } + + cost = 0; + + sort(x + 1, x + m + 1, cmp); + + int rx, ry, ms; + + for(ms = 0, i = 1; ms < n-1 && i <= m; ++i) + + { + + rx = varf(x[i].u); + + ry = varf(x[i].v); + + if(rx != ry) + + { + + unire(rx, ry); + + x[i].pus = 1; + + cost = cost + x[i].c; + + ++ms; + + } + + } + + fout< +#include + + + + + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +int nod,n,i,j,t[200000],aux,r[200000],m,x,y,c,xx,yy,sum; + +struct muchie{ + +int x,y,c;}; + +vector v; + +vector > sol; + +bool comp( const muchie a, const muchie b ) + +{return a.cr[b]) + + t[b]=a; + + if(r[a]>n>>m; + + + + for(i=1;i<=n;i++) + + { + + t[i]=i; + + r[i]=1; + + } + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>c; + + v.push_back({x,y,c}); + + } + + sort(v.begin(),v.end(),comp); + + + + for(i=0;i +#include +#include + + +using namespace std; + + + +int n, m, tata[400005]; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +struct Muchii{ + + int x, y, cost, p; + +}; + +Muchii a[400005]; + + + +inline bool Compar (const Muchii A, const Muchii B){ + + return A.cost < B.cost; + +} + + + +void Read(){ + + fin >> n >> m; + + + + for (int i = 1; i <= m; i++){ + + fin >> a[i].x >> a[i].y >> a[i].cost; + + a[i].p = 0; + + } + + + + sort (a + 1, a + m + 1, Compar); + +} + + + +int Find(int x) + +{ + + int rad, y; + + rad = x; + + + + while(tata[rad] != 0) + + { + + rad = tata[rad]; + + } + + + + while (x != rad){ + + y = tata[x]; + + tata[x] = rad; + + x = y; + + } + + return rad; + +} + + + +void Union (int x, int y){ + + tata[x] = y; + +} + + + +void APM (){ + + int x, y, sumacosturi = 0, numardemuchii; + + for (int i = 1; i <= m; i++){ + + x = Find (a[i].x); + + y = Find (a[i].y); + + + + if (x != y){ + + sumacosturi += a[i].cost; + + Union(x, y); + + a[i].p = 1; + + numardemuchii++; + + } + + } + + + + fout << sumacosturi << "\n" << n - 1 << "\n"; + + + + for (int i = 1; i <= m; i++){ + + if (a[i].p == 1){ + + fout << a[i].y << " " << a[i].x << "\n"; + + } + + } + +} + +int main() + +{ + + Read (); + + APM (); + + return 0; + +} diff --git a/mst/kruskal/af7d7412-a238-4a55-8cb6-4e8e34207b23.cpp b/mst/kruskal/af7d7412-a238-4a55-8cb6-4e8e34207b23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..844813cdc473accbf49bb9c0135ec23e9fcc632b --- /dev/null +++ b/mst/kruskal/af7d7412-a238-4a55-8cb6-4e8e34207b23.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); +int father[100005], group[100005], n, m, answer; + +vector< pair > > edges; + +vector< pair > apm; + + + +int findFather(int node) + +{ + + if(node == father[node]) + + { + + return node; + + } + + int new_father = findFather(father[node]); + + father[node] = new_father; + + return new_father; + +} + + + +int main () + +{ + + int a, b, cost; + + f >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + f >> a >> b >> cost; + + edges.push_back(make_pair(cost, make_pair(a, b))); + + } + + sort(edges.begin(), edges.end()); + + + + for(int i = 1; i <= n; i++) + + { + + father[i] = i; + + group[i] = 1; + + } + + + + for(auto edge : edges) + + { + + int a = edge.second.first; + + int b = edge.second.second; + + + + int fa = findFather(a); + + int fb = findFather(b); + + + + if(fa == fb) + + { + + continue; + + } + + + + apm.push_back(edge.second); + + answer += edge.first; + + + + if(group[fa] > group[fb]) + + { + + father[fb] = fa; + + group[fa] += group[fb]; + + } + + else + + { + + father[fa] = fb; + + group[fb] += group[fa]; + + } + + } + + + + g << answer << "\n" << n - 1 << "\n"; + + for(auto edge : apm) + + { + + g << edge.first << " " << edge.second << "\n"; + + } + + + + return 0; + +} diff --git a/mst/kruskal/afa90eef-271d-44fb-ae56-2d2c4746f0b1.cpp b/mst/kruskal/afa90eef-271d-44fb-ae56-2d2c4746f0b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c7b7ca5c9655135a020d99805617f232fa05060 --- /dev/null +++ b/mst/kruskal/afa90eef-271d-44fb-ae56-2d2c4746f0b1.cpp @@ -0,0 +1,117 @@ +#include +#include + + +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +pair > muchie[400005]; + +int t[200005],sz[200005],sum=0,sol[200005]; + +bool mysort(pair > a,pair > b){ + + return a.second.second>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>muchie[i].first>>muchie[i].second.first>>muchie[i].second.second; + + } + + sort(muchie+1,muchie+m+1,mysort); + + for(int i=1;i<=n;i++){ + + sz[i]=1; + + t[i]=i; + + } + + int cnt=0; + + for(int i=1;i<=m;i++){ + + if(same(muchie[i].first,muchie[i].second.first)==true) + + continue; + + sum+=muchie[i].second.second; + + unite(muchie[i].first,muchie[i].second.first); + + sol[++cnt]=i; + + } + + cout< +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< pair >ar; + +int n, m, v[400001], s; + + + +struct muchii{ + + int x, y, cost; + + + +}lm[400001]; + + + +bool compara(muchii a, muchii b){ + + return a.cost> n >> m; + + + + for(int i = 0; i < m; i++) + + f >> lm[i].x >> lm[i].y >> lm[i].cost; + + + + sort(lm, lm + m, compara); + + + + for(int i = 1; i <= n; i++) + + v[i] = i; + + + + for(int i = 0; i < m; i++) + + if(radacina(lm[i].x) != radacina(lm[i].y)){ + + v[radacina(lm[i].y)] = lm[i].x; + + s += lm[i].cost; + + ar.push_back ( make_pair(lm[i].x,lm[i].y)); + + } + + + + g< +#include +#include +const int NMAX = 200000; + +struct edge{ + +int from , to; + +int cost; + +bool operator <(const edge &other){ + + return cost < other.cost; + +} + +}; + +struct dublu{ + +int x , y; + +}; + +dublu Sol[NMAX + 1]; + +std::vector Edges; + +int father[NMAX + 1]; + +int getFather(int x){ + + if(father[x] == x) + + return x; + + father[x] = getFather(father[x]); + + return father[x]; + +} + +void join(int x , int y){ + + father[getFather(x)] = getFather(y); + +} + +int main() + +{ + + freopen("apm.in" ,"r",stdin); + + freopen("apm.out" ,"w",stdout); + + int n , m; + + scanf("%d%d" , &n , &m); + + for(int i = 1; i <= m ; i ++) + + { + + int x, y, cost; + + scanf("%d%d%d", &x, &y, &cost); + + Edges.push_back({x, y, cost}); + + + + } + + std:: sort(Edges.begin(), Edges.end()); + + int k = 0; + + for(int i = 1; i <= n ; i ++) + + { + + father[i] = i; + + } + + int totalCost = 0; + + for(int i = 0; i < m ; i ++){ + + int x = Edges[i].from; + + int y = Edges[i].to; + + + + if(getFather(x) != getFather(y)){ + + join(x , y); + + totalCost += Edges[i].cost; + + Sol[k ++ ] = {x , y}; + + } + + } + + printf("%d\n" , totalCost); + + printf("%d\n" , k); + + for(auto x : Sol){ + + printf("%d %d\n", x.x, x.y); + + } + +} diff --git a/mst/kruskal/b2f05f51-91b4-4154-bf84-b24ed0f2696b.cpp b/mst/kruskal/b2f05f51-91b4-4154-bf84-b24ed0f2696b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3901c34350892cfbbbdb6cd08f7540e029f5ec23 --- /dev/null +++ b/mst/kruskal/b2f05f51-91b4-4154-bf84-b24ed0f2696b.cpp @@ -0,0 +1,268 @@ + #include + #include +#include +#include + std::ifstream in("apm" ".in"); + + std::ofstream out("apm" ".out"); +const int + +N = 100000, + +M = 400000, + +C_MIN = -1000, C_MAX = 1000; + + + +int n, m; + + + +struct Muchie + +{ + + std::pair incidente; + + int cost; + + + + inline bool operator <= (Muchie& alta) + + {return cost <= alta.cost;} + + + + inline bool operator < (int& alt_cost) + + {return cost < alt_cost;} + +} + +*muchii; + + + + + + + +int pivot(int start, int end, Muchie& val) + +{ + + int piv = start; + + for (; start < end; start++) + + if (muchii[start] <= val) + + std::swap(muchii[piv++], muchii[start]); + + return piv; + +} + + + +void sorteaza(int start, int end) + +{ + + if (start == end) + + return; + + + + int piv = pivot(start, end, muchii[end - 1]); + + + + sorteaza(start, piv - 1); + + sorteaza(piv, end); + +} + + + + + + + +namespace arbore_partial + +{ + + int + + *set, + + nr_muchii, + + cost; + + + + Muchie* muchii; + + + + int radacina(int rad) + + { + + std::vector drum; + + while (set[rad]) + + { + + drum.push_back(rad); + + rad = set[rad] - 1; + + } + + for (int varf: drum) + + { + + set[varf] = rad + 1; + + } + + return rad; + + } + + + + void gestioneaza(Muchie& muchie) + + { + + int + + &a = muchie.incidente.first, + + &b = muchie.incidente.second, + + + + rad_a = radacina(a), + + rad_b = radacina(b); + + + + if (rad_a != rad_b) + + { + + set[rad_a] = rad_b + 1; + + muchii[nr_muchii++] = muchie; + + cost += muchie.cost; + + } + + } + +} + + + +using arbore_partial::gestioneaza; + +using arbore_partial::radacina; + + + + + + + +int main() + +{ + + in >> n >> m; + + muchii = new Muchie[m]; + + + + for (int i = 0; i < m; i++) + + { + + int a, b, cost; + + in >> a >> b >> cost; + + a--, b--; + + muchii[i] = {{a, b}, cost}; + + } + + + + sorteaza(0, m); + + arbore_partial::set = new int[n](); + + arbore_partial::muchii = new Muchie[n - 1]; + + + + for (int i = 0; i < m; i++) + + gestioneaza(muchii[i]); + + + + delete[] muchii; + + delete[] arbore_partial::set; + + + + out + + << arbore_partial::cost << '\n' + + << n - 1 << '\n'; + + + + for (int i = 0; i < n - 1; i++) + + { + + auto& incidente = arbore_partial::muchii[i].incidente; + + + + int + + &a = incidente.first, + + &b = incidente.second; + + + + out << a + 1 << ' ' << b + 1 << '\n'; + + } + + + + delete[] arbore_partial::muchii; + +} diff --git a/mst/kruskal/b5c062b8-c9b6-41f0-910b-b3f6b73b558e.cpp b/mst/kruskal/b5c062b8-c9b6-41f0-910b-b3f6b73b558e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb1e2e24a9bfc09109c1913b69f2937051ecd4c6 --- /dev/null +++ b/mst/kruskal/b5c062b8-c9b6-41f0-910b-b3f6b73b558e.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int x[400005], y[400005], cost[400006], ind[400005]; + +vector> rez; + +int TT[200005]; + +int RG[200005]; + +int n, m, total; + + + +bool cmp(int i, int j) + +{ + + return cost[i] < cost[j]; + +} + + + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= n;++i) + + { + + RG[i] = 1; + + TT[i] = i; + + } + + for(int i = 1;i <= m;++i) + + { + + f>>x[i]>>y[i]>>cost[i]; + + ind[i] = i; + + } + + sort(ind + 1, ind + m + 1, cmp); + +} + + + +int find(int x) + +{ + + int R, y; + + + + for(R = x;R != TT[R];R = TT[R]); + + + + while(x != TT[x]) + + { + + y = TT[x]; + + TT[x] = R; + + x = y; + + } + + return R; + +} + + + +void unite(int x,int y) + +{ + + if(RG[x] > RG[y]) + + TT[y] = x; + + else + + TT[x] = y; + + + + if(RG[x] == RG[y]) ++RG[y]; + +} + + + +void Solve() + +{ + + for(int i = 1;i <= m;++i) + + if(find(x[ind[i]]) != find(y[ind[i]])) + + { + + total += cost[ind[i]]; + + unite(find(x[ind[i]]), find(y[ind[i]])); + + rez.push_back({x[ind[i]], y[ind[i]]}); + + } + +} + + + +void Print() + +{ + + g< +using namespace std; + + +ifstream fin( "apm.in"); +ofstream fout( "apm.out"); + +typedef vector vi; + +vector> edges; + +int n, m; +vi link(n+1,0); + +int find(int x){ + if(link[x]==x) return x; + int p=find(link[x]); + link[x]=p; + return p; +} + +void unite(int x, int y){ + x=find(x), y=find(y); + link[x]=y; +} + +void kruscal(){ + sort(edges.begin(),edges.end()); + int cost=0; + for(int i=1;i<=n;++i) + link[i]=i; + vector> ans; + for(auto &t:edges){ + int x,y,c; + tie(c,x,y)=t; + if(find(x)!=find(y)) + unite(x, y), cost+=c, ans.push_back({x,y}); + } + sort(ans.begin(),ans.end()); + fout<>n>>m; + link.resize(n+1,-1); + for(int i=m;i--;){ + int x,y,c; + fin>>x>>y>>c; + + + edges.push_back(make_tuple(c,x,y)); + } + kruscal(); +} diff --git a/mst/kruskal/b8c6a505-8ffd-470f-982e-1d8e9e1a4bcd.cpp b/mst/kruskal/b8c6a505-8ffd-470f-982e-1d8e9e1a4bcd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d5c8158e0a7f012b0645b0c430358289b641288 --- /dev/null +++ b/mst/kruskal/b8c6a505-8ffd-470f-982e-1d8e9e1a4bcd.cpp @@ -0,0 +1,117 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n,m,i,sol[200005],tata[200005]; + + + +struct muchie{ + + int x; + + int y; + + int cost; + +} v[400005]; + + + +bool cmp(muchie a, muchie b) + +{ + + return a.cost < b.cost; + +} + + + +int rad(int nod) + +{ + + while (tata[nod] > 0) + + nod = tata[nod]; + + return nod; + +} + + + +int main() + +{ + + fin >> n >> m; + + for (i=1; i<=m; i++) + + fin >> v[i].x >> v[i].y >> v[i].cost; + + sort(v+1, v+m+1, cmp); + + for (i=1; i<=n; i++) + + tata[i] = -1; + + int costmin = 0; int k = 0; + + for (i=1; i<=m; i++) + + { + + int rx = rad(v[i].x); int ry = rad(v[i].y); + + if (rx != ry) + + { + + sol[++k] = i; costmin += v[i].cost; + + if (tata[rx] < tata[ry]) + + { + + tata[rx] += tata[ry]; + + tata[ry] = rx; + + } + + else + + { + + tata[ry] += tata[rx]; + + tata[rx] = ry; + + } + + } + + } + + fout << costmin << "\n" << n-1 << "\n"; + + for (i=1; i +#include +#include +#include + + +using namespace std; + +const int MAXN = 2e5 + 5; + +const int MAXM = 4e5 + 5; + +struct muchie{ + + int x, y, cost; + +}; + +muchie M[MAXM]; + + + +int tip,x,y; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int t[MAXN],n,m,h[MAXN]; + +vector < pair < int ,int > > ans; + +bool cmp(muchie x, muchie y) { + + return x.cost < y.cost; + +} + + + +int findroot(int nod) { + + if(t[nod] == nod) + + return nod; + + return t[nod] = findroot(t[nod]); + +} + + + +void Union(int rad1, int rad2) { + + if(h[rad1] > h[rad2]) { + + t[rad2] = rad1; + +} + +else + + if(h[rad1] < h[rad2]) { + + t[rad1] = rad2; + +} + +else + +{ + + t[rad1] = rad2; + + h[rad2]++; + +} + + + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 1; i <= n; ++i) { + + t[i] = i; + + h[i] = 1; + +} + + for ( int i = 1; i <= m; ++i){ + + fin >> M[i].x >> M[i].y >> M[i].cost; + +} + + sort(M+1,M+m+1,cmp); + + long long s = 0; + + for ( int i = 1; i <= m; ++i) { + + if(findroot(M[i].x) != findroot(M[i].y)) { + + Union(findroot(M[i].x),findroot(M[i].y)); + + s+=M[i].cost; + + ans.push_back({M[i].x,M[i].y}); + +} + +} + + fout << s <<"\n"<< ans.size() <<"\n"; + + for ( auto idk : ans) + + fout << idk.second <<" "<< idk.first <<"\n"; + +} diff --git a/mst/kruskal/b9637aa1-23e3-49c3-ac93-77de72527a3e.cpp b/mst/kruskal/b9637aa1-23e3-49c3-ac93-77de72527a3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b76225e99f63dbd8e1a545d0a5ade68983b498bb --- /dev/null +++ b/mst/kruskal/b9637aa1-23e3-49c3-ac93-77de72527a3e.cpp @@ -0,0 +1,158 @@ +#include + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie { + + int x, y, c, OK; + +}v[400001]; + + + +int t[200001]; + +int n, m, ct, nr; + + + +int comp(muchie u,muchie v) + +{ + + return u.c> n >> m; + + for(i = 1; i <= m; i++) + + fin >> v[i].x >> v[i].y >> v[i].c; + + + + + + for(i=1; i <= n; i++) + + t[i] = 0; + + + + + + sort(v+1,v+m+1,comp); + + + + + + ct = 0; nr = 0; i = 1; + + while(nr < n-1) + + { + + x = v[i].x; y = v[i].y; + + rx=radacina(x); + + ry=radacina(y); + + + + if(rx != ry) + + { + + ct=ct+v[i].c; + + t[ry]=rx; + + v[i].OK = 1; + + nr++; + + } + + i++; + + } + + + + + + + + fout << ct << '\n'< + + + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +struct Muchie + +{ + + int x, y, cost; + +}; + + + +struct A + +{ + + int x, y; + +}; + + + +vector < A > R; + +Muchie v[400005]; + +int t[200005], lg[200005]; + + + +int radacina ( int x ); + +void uneste ( int x, int y ); + +bool crt ( Muchie a, Muchie b ); + + + +int main() + +{ + + int n, m, i, x, y, r = 0; + + + + fin >> n >> m; + + for ( i = 1 ; i <= m ; i++ ) fin >> v[i].x >> v[i].y >> v[i].cost; + + + + sort ( v + 1, v + m + 1, crt ); + + for ( i = 1 ; i <= n ; i++ ) t[i] = i, lg[i] = 1; + + + + for ( i = 1 ; i <= m ; i++ ) + + { + + x = radacina ( v[i].x ); + + y = radacina ( v[i].y ); + + + + if ( x != y ) + + { + + uneste ( x, y ); + + r += v[i].cost; + + R.push_back ( { v[i].x, v[i].y } ); + + } + + } + + + + fout << r << '\n' << n - 1 << '\n'; + + for ( i = 0 ; i < R.size() ; i++ ) fout << R[i].x << ' ' << R[i].y << '\n'; + + + + return 0; + +} + + + +bool crt ( Muchie a, Muchie b ) + +{ + + if ( a.cost < b.cost ) return 1; + + + + return 0; + +} + + + +int radacina ( int x ) + +{ + + if ( x != t[x] ) t[x] = radacina ( t[x] ); + + + + return t[x]; + +} + + + +void uneste ( int x, int y ) + +{ + + if ( lg[x] < lg[y] ) lg[y] += lg[x], t[x] = y; + + else lg[x] += lg[y], t[y] = x; + +} diff --git a/mst/kruskal/bb6b5ded-c3b1-4694-9bf7-d8aca37ac7dd.cpp b/mst/kruskal/bb6b5ded-c3b1-4694-9bf7-d8aca37ac7dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20630777dfa8142d64c48b1a04b9d6de97cb8947 --- /dev/null +++ b/mst/kruskal/bb6b5ded-c3b1-4694-9bf7-d8aca37ac7dd.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n, m, nRasp, costRasp, tati[400001], rang[400001]; + +pair rasp[400001]; + + + +struct muchie { + + int x, y, cost; + +} muchii[400001]; + + + +bool compara(muchie a, muchie b) { + + return a.cost < b.cost; + +} + + + +int verTati(int Nod) { + + while (tati[Nod] != Nod) + + Nod = tati[Nod]; + + + + return Nod; + +} + + + +void uneste(int x, int y) { + + if (rang[x] < rang[y]) + + tati[x] = y; + + else if (rang[y] < rang[x]) + + tati[y] = x; + + else { + + tati[y] = x; + + ++rang[x]; + + } + +} + + + +int main() { + + in >> n >> m; + + for (int i = 1; i <= n; ++i) { + + tati[i] = i; + + rang[i] = 1; + + } + + + + for (int i = 1; i <= m; ++i) + + in >> muchii[i].x >> muchii[i].y >> muchii[i].cost; + + sort(muchii + 1, muchii + m + 1, compara); + + + + for (int i = 1; i <= m; ++i) { + + int a = verTati(muchii[i].x); + + int b = verTati(muchii[i].y); + + + + if (a != b) { + + uneste(a, b); + + rasp[++nRasp].first = muchii[i].x; + + rasp[nRasp].second = muchii[i].y; + + costRasp += muchii[i].cost; + + } + + } + + + + out << costRasp << '\n' << n - 1 << '\n'; + + for (int i = 1; i <= nRasp; ++i) + + out << rasp[i].first << ' ' << rasp[i].second << '\n'; + + return 0; + +} diff --git a/mst/kruskal/bd347b93-07ce-4011-ac1d-74eba9d4c6a4.cpp b/mst/kruskal/bd347b93-07ce-4011-ac1d-74eba9d4c6a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b49f5f30f0a3725a23b2f04baf9dfe6c5392e7c --- /dev/null +++ b/mst/kruskal/bd347b93-07ce-4011-ac1d-74eba9d4c6a4.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int maxn = 200005; + + + +int UF_array[maxn], x[maxn], y[maxn], c[maxn]; + +int n, m, rez, ind[maxn]; + +vector v_rez; + + + +void afisare_UF_array(){ + + fout<<"\n------------------------------------------------------------\n"; + + for (int i=1; i<=n; i++) + + fout<>n>>m; + + for(i=1; i<=m; i++){ + + fin>>x[i]>>y[i]>>c[i]; + + ind[i] = i; + + } + + for(i=1; i<=n; i++) + + UF_array[i] = i; + + + + sort(ind+1, ind+m+1, my_cmp); + + + + for(i=1; i<=m; i++){ + + + + + + + + if (findd(x[ind[i]]) != findd(y[ind[i]])){ + + rez += c[ind[i]]; + + reuniune(x[ind[i]],y[ind[i]]); + + v_rez.push_back(ind[i]); + + } + + } + + fout< + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct edge { + + int x, y, cost; + + + + bool operator <(const edge &other) { + + return cost < other.cost; + + } + +}; + + + +const int Dim = 4e5 + 5; + + + +int tata[Dim], w[Dim]; + +int n, m, costTotal; + + + +edge e[Dim]; + + + +vector > ans; + + + +int findTata(int node) { + + int cn = node; + + while(tata[node] != node) { + + node = tata[node]; + + } + + + + while(cn != node) { + + int aux = tata[cn]; + + cn = aux; + + tata[cn] = node; + + } + + return node; + +} + + + +void uneste(int node1, int node2) { + + int t1 = findTata(node1); + + int t2 = findTata(node2); + + + + if(w[t1] < w[t2]) { + + tata[t1] = t2; + + w[t2] += w[t1]; + + } else { + + tata[t2] = t1; + + w[t1] += w[t2]; + + } + +} + + + +int main() { + + f >> n >> m; + + for(int i = 1; i <= m; ++i) { + + f >> e[i].x >> e[i].y >> e[i].cost; + + } + + + + for(int i = 1; i <= n; ++i) { + + tata[i] = i; + + w[i] = 1; + + } + + + + sort(e + 1, e + m + 1); + + for(int i = 1; i <= m; ++i) { + + if(findTata(e[i].x) != findTata(e[i].y)) { + + uneste(e[i].x, e[i].y); + + costTotal += e[i].cost; + + ans.push_back({e[i].x, e[i].y}); + + } + + } + + + + g << costTotal << '\n' << n - 1 << '\n'; + + for(auto it = ans.begin(); it != ans.end(); ++it) { + + g << it -> first << " " << it -> second << '\n'; + + } + + return 0; + +} diff --git a/mst/kruskal/bf3df567-d9e6-40d4-8d7d-43ac392a1072.cpp b/mst/kruskal/bf3df567-d9e6-40d4-8d7d-43ac392a1072.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c0ed81f47ea1efde79f7f0934c52d3cc7194715 --- /dev/null +++ b/mst/kruskal/bf3df567-d9e6-40d4-8d7d-43ac392a1072.cpp @@ -0,0 +1,178 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int MMax = 400005; + + + +pair P[MMax]; + +int k; + + + +int N, M, Total, TT[MMax], RG[MMax]; + + + +struct Muchie{ + + int x, y, cost; + +}V[MMax]; + + + +int Compare(Muchie a, Muchie b) + +{ + + return a.cost < b.cost; + +} + + + +void Citire() + +{ + + in >> N >> M; + + for(int i = 1; i <= M; ++ i) + + in >> V[i].x >> V[i].y >> V[i].cost; + + sort(V + 1, V + M + 1, Compare); + + for(int i = 1; i <= N; ++ i) + + { + + TT[i] = i; + + RG[i] = 1; + + } + +} + + + +int Find(int Nod) + +{ + + while(TT[Nod] != Nod) + + Nod = TT[Nod]; + + return Nod; + +} + + + +void Unire(int x, int y) + +{ + + + + + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[x] > RG[y]) + + TT[y] = x; + + if(RG[x] == RG[y]) + + { + + TT[x] = y; + + ++ RG[y]; + + } + +} + + + +void Rezolva() + +{ + + for(int i = 1; i <= M; ++ i) + + { + + int tata_x = Find(V[i].x); + + int tata_y = Find(V[i].y); + + if(tata_x != tata_y) + + { + + Unire(tata_x, tata_y); + + P[++ k].first = V[i].x; + + P[k].second = V[i].y; + + Total += V[i].cost; + + } + + } + +} + + + +void Afisare() + +{ + + out << Total << '\n'; + + out << N - 1 << '\n'; + + for(int i = 1; i <= k; ++ i) + + out << P[i].second << " " << P[i].first << '\n'; + +} + + + +int main() + +{ + + Citire(); + + Rezolva(); + + Afisare(); + + return 0; + +} diff --git a/mst/kruskal/c17faebb-9034-4b84-86d9-0393e59b2256.cpp b/mst/kruskal/c17faebb-9034-4b84-86d9-0393e59b2256.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f69f33f3e4487c78aa284421535cedbfbb100b6 --- /dev/null +++ b/mst/kruskal/c17faebb-9034-4b84-86d9-0393e59b2256.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + + + +ifstream cin ("apm.in"); + +ofstream cout ("apm.out"); + + + +struct ura{ + + int x,y,c,ok; + +}; + +ura v[400005]; + +int h[200005]; + +bool cmp(ura a,ura b) + +{ + + return a.ch[d2]) + + d[d2]=d1; + + else + + { + + d[d1]=d2; + + if(h[d1]==h[d2]) + + h[d2]++; + + } + + } + +} + +int main() + +{ + + int n,m,i; + + cin>>n>>m; + + for (i=1;i<=m;i++) + + cin>>v[i].x>>v[i].y>>v[i].c; + + for (i=1;i<=n;i++) + + d[i]=i; + + sort(v+1,v+m+1,cmp); + + nr=cost=0; + + for(i=1;i<=m;i++) + + unire(i); + + cout< + + + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +int n, m, rang[200020], dad[200200], sum; + +vector>ans; + +struct mch + +{ + + int x, y, cost; + +}v[500201]; + +bool cmp(mch a, mch b) + +{ + + return a.cost>n>>m; + + for(int i=1; i<=n; i++) + + { + + rang[i]=1; + + dad[i]=i; + + } + + for(int i=1; i<=m; i++) + + f>>v[i].x>>v[i].y>>v[i].cost; + + sort(v+1, v+m+1, cmp); + + for(int i=1; i<=m; i++) + + { + + if(Find(v[i].x)!=Find(v[i].y)) + + { + + Union(Find(v[i].x), Find(v[i].y)); + + ans.push_back({v[i].x, v[i].y}); + + sum+=v[i].cost; + + } + + } + + g< + + + + +using namespace std; + + + + + + + +struct muchie{ + + int nod1, nod2, cost; + +}; + + + +vector w, wsol; + +int rad[400005]; + + + +bool comp(muchie A, muchie B){ + + return A.cost < B.cost; + +} + + + +int radacina(int nod){ + + + + if(rad[nod] == nod) return nod; + + + + return rad[nod] = radacina(rad[nod]); + +} + + + +void unesc(int nod1, int nod2){ + + rad[radacina(nod1)] = radacina(nod2); + +} + + + +int main() + +{ + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + int n, m, i, j; + + int sol = 0; + + muchie var; + + fin >> n >> m; + + for(i = 1; i <= m; ++i){ + + fin >> var.nod1 >> var.nod2 >> var.cost; + + w.push_back(var); + + } + + + + for(i = 1; i <= n; ++i) rad[i] = i; + + + + sort(w.begin(), w.end(), comp); + + + + + + for(i = 0; i < m; ++i){ + + + + if(radacina(w[i].nod1) != radacina(w[i].nod2)){ + + unesc(w[i].nod1, w[i].nod2); + + sol += w[i].cost; + + wsol.push_back(w[i]); + + } + + + + } + + + + int help = wsol.size(); + + fout << sol << '\n' << help << '\n'; + + for(i = 0; i < help; ++i){ + + fout << wsol[i].nod1 << ' ' << wsol[i].nod2 << '\n'; + + } + + + + return 0; + +} diff --git a/mst/kruskal/c368f49b-8d28-4ebe-a491-d6097a267cd5.cpp b/mst/kruskal/c368f49b-8d28-4ebe-a491-d6097a267cd5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9bd9ac4b1a3bc59b22a6f17084e87d8cbca962d --- /dev/null +++ b/mst/kruskal/c368f49b-8d28-4ebe-a491-d6097a267cd5.cpp @@ -0,0 +1,223 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + +struct muchie{ + + int x, y, c; + + + + bool operator<(const muchie &other) const { + + if(c != other.c) + + return c < other.c; + + if(y != other.y) + + return y < other.y; + + return x < other.x; + + } + + + + + +}; + + + + + +struct graph{ + + int t; + + int d; + +}gr[200005]; +int n, m, cmin; + +int tati[200005]; + +muchie ms[400005], sol[200005]; + + + + + +int rad_act; + +void rad(int x) + +{ + + if(gr[x].t == x) + + { + + rad_act = x; + + return; + + } + + rad(gr[x].t); + + gr[x].t = rad_act; + +} +bool findSameR(int x, int y) + +{ + + rad(x); + + int radOfX = rad_act; + + rad(y); + + int radOfY = rad_act; + + if(radOfX == radOfY) + + return 0; + + else return 1; + +} +void read() + +{ + + f>>n>>m; + + for(int i=0; i>ms[i].x>>ms[i].y>>ms[i].c; + + sort(ms, ms+m); + +} + + + + + + + +void initTati() + +{ + + for(int i=1; i<=n; ++i) + + gr[i].t = i; + +} + + + +void unionOf(int x, int y) + +{ + + rad(x); + + int aux = rad_act; + + rad(y); + + + + if(gr[aux].d == gr[rad_act].d) + + { + + gr[aux].t = rad_act; + + gr[aux].d++; + + } + + else if(gr[aux].d < gr[rad_act].d) + + gr[rad_act].t = aux; + + else{ + + gr[aux].t = rad_act; + + } + +} + + + +void solve() + +{ + + initTati(); + + int chosen = 0; + + for(int i=0; i +#include +#include +#include + +using namespace std; +int tata[200001], h[200001], n, m; +vector, int>> graf, apcm; +ifstream f("apm.in"); +ofstream g("apm.out"); + +bool cmp (pair, int> a, pair, int> b) +{ + if (a.second < b.second) + return 1; + else + return 0; + +} + +void initializare (int u) +{ + tata[u] = 0; h[u] = 0; +} + +int reprezentare (int u) +{ + while(tata[u] != 0) + u = tata[u]; + return u; +} + +void reuneste(int u, int v) +{ + int ru = reprezentare(u); + int rv = reprezentare(v); + if (h[ru] > h[rv]) + tata[rv] = ru; + else + { + tata[ru] = rv; + if(h[ru] == h[rv]) + h[rv]++; + } +} + +int kruskal() +{ + sort(graf.begin(), graf.end(), cmp); + for (int v = 1; v <= n; v++) + initializare(v); + int nr = 0; + int cost = 0; + for (auto x : graf) + if(reprezentare(x.first.first) != reprezentare(x.first.second)) + { + apcm.push_back(x); + cost += x.second; + reuneste(x.first.first, x.first.second); + nr++; + if(nr == n - 1) + break; + } + return cost; +} + +int main() +{ + f >> n >> m; + for(int i = 0; i < m; i++) + { + int x, y, cost; + f >> x >> y >> cost; + graf.push_back(make_pair(make_pair(x, y), cost)); + } + int cost_final = kruskal(); + g << cost_final << '\n' << n - 1 << '\n'; + for(auto x : apcm) + { + g << x.first.first << " " << x.first.second << '\n'; + } + return 0; +} diff --git a/mst/kruskal/c52e5a19-7fbb-479b-a770-1af88704b492.cpp b/mst/kruskal/c52e5a19-7fbb-479b-a770-1af88704b492.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab17189e010546496accf875b32a2123a3b6d0dc --- /dev/null +++ b/mst/kruskal/c52e5a19-7fbb-479b-a770-1af88704b492.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int lim = 400005; + +int n,m,t[lim],rang[lim],total,k; + +pair < int , int >P[lim]; + + + +struct Edge + +{ + + int x,y,c; + +}v[lim]; + + + +bool comp(Edge a, Edge b) + +{ + + return a.c < b.c; + +} + + + +void citire() + +{ + + fin>>n>>m; + + for(int i=1 ; i<=m ; i++) + + fin>> v[i].x >> v[i].y >> v[i].c; + + for(int i=1 ; i<=n ; i++) + + t[i]=i , rang[i]=1; + + sort(v+1 , v+m+1 , comp); + +} + + + +int Root(int x) + +{ + + if(t[x] == x) return x; + + else return t[x]=Root(t[x]); + +} + + + +void Unite(int a, int b) + +{ + + if(rang[a] > rang[b]) t[b]=a; + + else if(rang[a] < rang[b]) t[a]=b; + + else + + { + + t[a]=b; + + rang[a]++; + + } + +} + + + +void Kruskal() + +{ + + for(int i=1 ; i<=m ; i++) + + { + + int r1=Root(v[i].x); + + int r2=Root(v[i].y); + + if(r1 != r2) + + { + + Unite(r1,r2); + + total += v[i].c; + + k++; + + P[k].first = v[i].y; + + P[k].second = v[i].x; + + } + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +typedef struct PAIR{ + + + + int muchieX, muchieY, costMuchie; + + + +}PAIR; + + + +PAIR P; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int nrNoduri, nrMuchii, costArborePartial, nrMuchiiArborePartial, *vectorTati, *rang; + +vector muchii; + +bool *vizitatMuchii; + + + +int Radacina(int nod){ + + + + int radacinaNod = nod; + + + + while ( vectorTati[radacinaNod] != radacinaNod ) + + radacinaNod = vectorTati[radacinaNod]; + + + + while ( vectorTati[nod] != nod ){ + + int aux = vectorTati[nod]; + + vectorTati[nod] = radacinaNod; + + nod = aux; + + } + + + + return radacinaNod; + + + +} + + + +void Unire(int radacinaX, int radacinaY){ + + + + if ( radacinaX != radacinaY ){ + + + + if(rang[radacinaX] > rang[radacinaY]) + + vectorTati[radacinaY] = radacinaX; + + + + else + + vectorTati[radacinaX] = radacinaY; + + + + + + if( rang[radacinaX] == rang[radacinaY] ) + + rang[radacinaY]++; + + } + + + +} + + + +int cmp(PAIR X, PAIR Y){ + + + + return X.costMuchie < Y.costMuchie; + + + +} + + + +void kruskal(){ + + + + int nrComponenteConexe = nrNoduri; + + + + for ( int i = 1; i <= nrNoduri; i++){ + + vectorTati[i] = i; + + rang[i] = 1; + + } + + + + costArborePartial = 0 , nrMuchiiArborePartial = 0; + + + + int lim = muchii.size(); + + + + for(int i = 0; i < lim && nrComponenteConexe > 1 ; i++){ + + + + int radacinaX = Radacina(muchii[i].muchieX) , radacinaY = Radacina(muchii[i].muchieY); + + + + if ( radacinaX != radacinaY ){ + + + + vizitatMuchii[i] = true; + + + + costArborePartial += muchii[i].costMuchie; + + nrMuchiiArborePartial++; + + + + Unire(radacinaX,radacinaY); + + + + nrComponenteConexe--; + + } + + } + +} + + + +int main(){ + + + + if ( !fin ){ + + cout << "\nEroare la deschiderea fisierului !\n"; + + exit(-1); + + } + + + + fin >> nrNoduri; + + + + vectorTati = NULL; + + + + vectorTati = new int[nrNoduri + 5]; + + rang = new int[nrNoduri + 5]; + + + + if (!vectorTati || !rang){ + + cout << "\nEroare la alocarea dinamica !\n"; + + exit(-1); + + } + + + + fin >> nrMuchii; + + + + vizitatMuchii = new bool[nrMuchii + 5]; + + + + if (!vizitatMuchii){ + + cout << "\nEroare la alocarea dinamica !\n"; + + exit(-1); + + } + + + + for (int i = 0; i < nrMuchii; i++) { + + + + fin >> P.muchieX >> P.muchieY >> P.costMuchie; + + + + muchii.push_back(P); + + + + vizitatMuchii[i] = false; + + + + } + + + + sort(muchii.begin(),muchii.end(),cmp); + + + + kruskal(); + + + + fout << costArborePartial << "\n" << nrMuchiiArborePartial << "\n"; + + + + for ( int i = 0; i < nrMuchii; i++) + + if ( vizitatMuchii[i] ) + + fout << muchii[i].muchieX << " " << muchii[i].muchieY <<"\n"; + + + + + + return 0; + + + +} diff --git a/mst/kruskal/c79b27cd-cb4b-4a63-9300-a2586237f1f8.cpp b/mst/kruskal/c79b27cd-cb4b-4a63-9300-a2586237f1f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd35f056f7abd5699edf52222f58cd2a9e3f53d2 --- /dev/null +++ b/mst/kruskal/c79b27cd-cb4b-4a63-9300-a2586237f1f8.cpp @@ -0,0 +1,123 @@ +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +struct elem + +{ + + int x,y,c; + +}; + +elem v[400002],sol[200002]; + + + +inline bool cmp(const elem &a,const elem &b) + +{ + + return a.c>n>>m; + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for(i=1;i<=n;i++) + + tata[i]=i,dim[i]=1; + + for(i=1;i<=m&&k +#include +#include +using namespace std; + +ifstream f ( "apm.in" ); + +ofstream g ( "apm.out" ); + +const int NMAX = 200000,MMAX=400000; + +struct muchie + +{ + + int i, j, c; + +}; + +int n, m, cost; + +muchie M[MMAX+1]; + +int CC[NMAX + 1]; + +int NrM[NMAX]; + +void citire() + +{ + + f >> n >> m; + + + + for ( int i = 1; i <= m; i++ ) + + f >> M[i].i >> M[i].j >> M[i].c; + +} + +void afis() + +{ + + g << cost << '\n'< +#include +using namespace std; +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); +const int nmax=200005,mmax=400005; +int n,m,tt[nmax],rg[nmax],cnt,s; +pair rez[mmax]; +struct muchie +{ + int x,y,c; +}v[mmax]; +bool cmp (muchie a,muchie b) +{ + return a.c> n >> m; + for (int i=1;i<=m;i++) + { + fin >> v[i].x >> v[i].y >> v[i].c; + } + sort (v+1,v+m+1,cmp); + for (int i=1;i<=n;i++) + { + tt[i]=i; + rg[i]=1; + } + apm(); + fout << s << '\n'; + fout << cnt << '\n'; + for (int i=1;i<=cnt;i++) + { + fout << rez[i].first << " " << rez[i].second << '\n'; + } +} diff --git a/mst/kruskal/c95aadd3-0f19-4be1-abcb-003496bfcbbc.cpp b/mst/kruskal/c95aadd3-0f19-4be1-abcb-003496bfcbbc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5de03d15a8e9148535d1d613fe7dbc1fac850589 --- /dev/null +++ b/mst/kruskal/c95aadd3-0f19-4be1-abcb-003496bfcbbc.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int N,M,cT; + +int T[200005],R[200005]; + +struct Muchie{int x,y,c;}V[400005]; + +vector< pair >mArb; + + + +bool comp(Muchie a, Muchie b) + +{ + + return a.c>N>>M; + + for(int i=1;i<=M;i++) + + fin>>V[i].x>>V[i].y>>V[i].c; + + sort(V+1,V+M+1,comp); + +} + + + +int parinte(int vf) + +{ + + if(!T[vf]) + + return vf; + + int p=parinte(T[vf]); + + T[vf]=p; + + return p; + +} + + + +void unire(int pA, int pB) + +{ + + if(R[pA] +#include +using namespace std; +ifstream cin ("apm.in"); +ofstream cout ("apm.out"); +int sef[200010]; +struct ura +{ + int x,y,cost; +}; +ura v[400010]; +struct urica +{ + int x,y; +}; +urica rez[400010]; +bool cmp(ura a,ura b) +{ + if (a.cost>n>>m; + for (i=1; i<=m; i++) + cin>>v[i].x>>v[i].y>>v[i].cost; + sort (v+1,v+m+1,cmp); + for (i=1; i<=n; i++) + sef[i]=i; + i=1; + ct=0; + cost_total=0; + while (ct +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchii + +{ + + bool use; + + int a,b,cost; + +} w[400001]; + + + +int v[400001]; + +int m,n; + + + +bool compara(muchii vs, muchii vd) + +{ + + return vs.cost>n>>m; + + make(); + + for(int i=1; i<=m; i++) + + { + + fin>>w[i].a>>w[i].b>>w[i].cost; + + w[i].use=true; + + } + + sort(w+1,w+1+m, compara); + + + +} + + + +int main() + +{ + + read(); + + solve(); + + return 0; + +} diff --git a/mst/kruskal/ca920748-a152-422e-99c3-278aa9d5d95e.cpp b/mst/kruskal/ca920748-a152-422e-99c3-278aa9d5d95e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5e4dd2a482698a826cf0fec5189671a0d8013ce --- /dev/null +++ b/mst/kruskal/ca920748-a152-422e-99c3-278aa9d5d95e.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int muchii_puse,CT,n,tata[200100],h[200100]; + + + +struct MUCHIE + +{ + + int x,y,cost; + +}mch[400100],sol[400100]; + + + +int compare(MUCHIE A, MUCHIE B) + +{ + + return (A.cost>n>>m; + + for(i=1;i<=n;i++)tata[i]=i,h[i]=1; + + for(i=1;i<=m;i++) + + { + + f>>mch[i].x>>mch[i].y>>mch[i].cost; + + } + + + + sort(mch+1,mch+m+1,compare); + + kruskal(); + + + + g< +#include +#include +struct s + +{ + + int n1, n2, muchii; + +} v[400005]; + + + +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + + + +vector < s > sol; + +vector < vector < pair < int, int > > > G; + +int stramos[200005], raspuns; + +int n, m; + + + +bool cmp(s a, s b) + +{ + + return a.muchii < b.muchii ; + +} + + + +int tata(int x) + +{ + + if (stramos[x] == x) + + return x; + + stramos[x] = tata(stramos[x]); + + + + + + return stramos[x]; + +} + + + +void reuniune(int x, int y) + +{ + + stramos[tata(x)] = tata(y); + +} + + + +void citire() + +{ + + cin >> n >> m; + + + + G.resize(n + 5); + + for(int i = 1 ; i <= m ; i++) + + cin >> v[i].n1 >> v[i].n2 >> v[i].muchii, + + G[v[i].n1].push_back(make_pair(v[i].n2, v[i].muchii)), + + G[v[i].n2].push_back(make_pair(v[i].n1, v[i].muchii)); + + + + + +} + + + +void prep() + +{ + + sort(v + 1, v + m + 1, cmp); + + + + for(int i = 1 ; i <= n ; i++) + + stramos[i] = i; + +} + + + +void rez() + +{ + + int x, y, aux; + + + + for(int i = 1 ; i <= m ; i++) + + if (tata(v[i].n1) != tata(v[i].n2)) + + { + + raspuns += v[i].muchii, reuniune(v[i].n1, v[i].n2), + + sol.push_back(v[i]); + + } + +} + + + +void afis() + +{ + + cout << raspuns << '\n' << sol.size() << '\n'; + + + + for(int i = 0 ; i < sol.size() ; i++) + + cout << sol[i].n1 << ' ' << sol[i].n2 << '\n'; + +} + + + +int main() + +{ + + citire(); + + prep(); + + rez(); + + afis(); + + return 0; + +} diff --git a/mst/kruskal/cafe29c4-a1b9-46f6-b931-310fcb099e3d.cpp b/mst/kruskal/cafe29c4-a1b9-46f6-b931-310fcb099e3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3b064657bc20843c7e68e34e316d4bb2a108ba9 --- /dev/null +++ b/mst/kruskal/cafe29c4-a1b9-46f6-b931-310fcb099e3d.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +struct ura{ + + int nod1,nod2,cost; + +}; + +ura v[400001],ans[400001]; + +bool ord(ura a,ura b){ + + if(a.cost!=b.cost) + + return a.cost>n>>m; + + for(i=1;i<=n;++i) + + sef[i]=i; + + for(i=1;i<=m;++i) + + cin>>v[i].nod1>>v[i].nod2>>v[i].cost; + + sort(v+1,v+m+1,ord); + + nr_muchii=0; + + costmin=0; + + for(i=1;i<=m && nr_muchii +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200041; + + + +struct edg{ + + int a, b, v; + + bool operator<(const edg &rhs){ + + return v < rhs.v; + + } + +}; + + + +int n, m; + +vector eg; + +vector nice; + + + +int tre[N]; + +int dad(int a){ + + if(a != tre[a]){ + + tre[a] = dad(tre[a]); + + } + + return tre[a]; + +} + + + +void makeuni(int a, int b){ + + tre[dad(a)] = dad(b); + +} + + + +bool chekuni(int a, int b){ + + return dad(a) == dad(b); + +} + + + +int main(){ + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + edg e; + + fin >> e.a >> e.b >> e.v; + + eg.push_back(e); + + } + + + + for(int i = 1; i <= n; ++i){ + + tre[i] = i; + + } + + + + int cost = 0; + + sort(eg.begin(), eg.end()); + + for(auto e : eg){ + + if(!chekuni(e.a, e.b)){ + + makeuni(e.a, e.b); + + cost += e.v; + + nice.push_back(e); + + } + + } + + + + fout << cost << "\n"; + + fout << nice.size() << "\n"; + + for(auto e : nice){ + + fout << e.a << " " << e.b << "\n"; + + } + + + + return 0; + +} diff --git a/mst/kruskal/cc05d243-365d-41c3-91a6-20c70caa3745.cpp b/mst/kruskal/cc05d243-365d-41c3-91a6-20c70caa3745.cpp new file mode 100644 index 0000000000000000000000000000000000000000..883dd4e62d1afc84575a3877fa046d0cc9942616 --- /dev/null +++ b/mst/kruskal/cc05d243-365d-41c3-91a6-20c70caa3745.cpp @@ -0,0 +1,174 @@ +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N_MAX = 2e5 + 1; + + + +int id[N_MAX], sz[N_MAX]; + + + +int dsu_find(int node) + +{ + + int root = node; + + + + while(id[root] != 0) + + root = id[root]; + + + + int aux = node; + + + + while(node != root) + + { + + aux = id[node]; + + id[node] = root; + + node = aux; + + } + + + + return root; + +} + + + + + +bool dsu_union(int x, int y) + +{ + + int root_x = dsu_find(x); + + int root_y = dsu_find(y); + + + + if(root_x == root_y) return false; + + + + if(sz[root_x] < sz[root_y]) swap(root_x, root_y); + + + + sz[root_x] += sz[root_y]; + + id[root_y] = root_x; + + + + return true; + +} + + + + + +int N, M; + + + +struct Muchie{ + + int x, y, c; + +}; + + + +vector graph; + + + +vector> answer; + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1; i <= M; ++i) + + { + + Muchie m; + + fin >> m.x >> m.y >> m.c; + + + + graph.push_back(m); + + } + + + + sort(graph.begin(), graph.end(), [](Muchie& A, Muchie& B){return A.c < B.c;}); + + + + long long min_cost = 0; + + + + for(size_t i = 0; i < graph.size() && answer.size() != (size_t)N - 1; ++i) + + { + + if(dsu_union(graph[i].x, graph[i].y) == true) + + { + + min_cost += graph[i].c; + + answer.push_back({graph[i].x, graph[i].y}); + + } + + } + + + + + + fout << min_cost << '\n'; + + fout << N - 1 << '\n'; + + + + for(auto& muchie : answer) + + fout << muchie.first << ' ' << muchie.second << '\n'; + +} diff --git a/mst/kruskal/cc3334b1-9bcb-4cbf-9abc-1e28622fa36b.cpp b/mst/kruskal/cc3334b1-9bcb-4cbf-9abc-1e28622fa36b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e37e2aa822dfba07a027d718c6a4d4ec7c72d665 --- /dev/null +++ b/mst/kruskal/cc3334b1-9bcb-4cbf-9abc-1e28622fa36b.cpp @@ -0,0 +1,148 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +struct Muchie + +{ + + int x, y, cost; + +}; + + + +struct A + +{ + + int x, y; + +}; + + + +vector < A > R; + +Muchie v[400005]; + +int t[200005], lg[200005]; + + + +int radacina ( int x ); + +void uneste ( int x, int y ); + +bool crt ( Muchie a, Muchie b ); + + + +int main() + +{ + + int n, m, i, x, y, r = 0; + + + + fin >> n >> m; + + for ( i = 1 ; i <= m ; i++ ) fin >> v[i].x >> v[i].y >> v[i].cost; + + + + sort ( v + 1, v + m + 1, crt ); + + for ( i = 1 ; i <= n ; i++ ) t[i] = i, lg[i] = 1; + + + + for ( i = 1 ; i <= m ; i++ ) + + { + + x = radacina ( v[i].x ); + + y = radacina ( v[i].y ); + + + + if ( x != y ) + + { + + uneste ( x, y ); + + r += v[i].cost; + + R.push_back ( { v[i].x, v[i].y } ); + + } + + } + + + + fout << r << '\n' << n - 1 << '\n'; + + for ( i = 0 ; i < R.size() ; i++ ) fout << R[i].x << ' ' << R[i].y << '\n'; + + + + return 0; + +} + + + +bool crt ( Muchie a, Muchie b ) + +{ + + if ( a.cost < b.cost ) return 1; + + + + return 0; + +} + + + +int radacina ( int x ) + +{ + + if ( x != t[x] ) t[x] = radacina ( t[x] ); + + + + return t[x]; + +} + + + +void uneste ( int x, int y ) + +{ + + if ( lg[x] < lg[y] ) lg[y] += lg[x], t[x] = y; + + else lg[x] += lg[y], t[y] = x; + +} diff --git a/mst/kruskal/ccca20d3-7688-40d9-b709-e3cc6c7624c4.cpp b/mst/kruskal/ccca20d3-7688-40d9-b709-e3cc6c7624c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b0d8523e3ea46a7c2caa75d21d7896c7c91e296e --- /dev/null +++ b/mst/kruskal/ccca20d3-7688-40d9-b709-e3cc6c7624c4.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int mmax = 400005; + +const int nmax = 200005; + +int n, m; + +int rang[nmax], tt[nmax]; + + + +struct ed + +{ + + int x, y, c; + +}edge[mmax]; + + + +inline void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> edge[i].x >> edge[i].y >> edge[i].c; + +} + + + +bool cmp(ed a, ed b) + +{ + + return a.c < b.c; + +} + + + +vector >sol; + + + +inline int findset(int x) + +{ + + if (x != tt[x]) + + tt[x] = findset(tt[x]); + + return tt[x]; + +} + + + +inline void unite(int a, int b) + +{ + + if (rang[a] < rang[b]) + + tt[a] = b; + + else if (rang[a] > rang[b]) + + tt[b] = a; + + else + + tt[a] = b, rang[a]++; + +} + + + +inline void apm() + +{ + + sort(edge + 1, edge + m + 1, cmp); + + for (int i = 1; i <= n; i++) + + rang[i] = 1, tt[i] = i; + + int ans = 0; + + for (int i = 1; i <= m; i++) + + { + + int a = findset(edge[i].x); + + int b = findset(edge[i].y); + + if (a == b) + + continue; + + ans += edge[i].c; + + unite(a, b); + + sol.push_back({edge[i].x, edge[i].y}); + + } + + fout << ans << "\n" << n-1 << "\n"; + + for (int i = 1; i < n; i++) + + fout << sol[i-1].first << " " << sol[i-1].second << "\n"; + +} + + + +int main() + +{ + + read(); + + apm(); + + return 0; + +} diff --git a/mst/kruskal/cd5a24c4-0c6f-49e1-b963-8135bc5ad054.cpp b/mst/kruskal/cd5a24c4-0c6f-49e1-b963-8135bc5ad054.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02b45ec83d873867e95d5fed57e28ff181117d28 --- /dev/null +++ b/mst/kruskal/cd5a24c4-0c6f-49e1-b963-8135bc5ad054.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + + + +using namespace std; + + + +long long tata[200001], nrmuchii, s; + + + +struct mama + +{ + + int x, y, cost; + +}v[400001]; + + + +int findtata(int x) + +{ + + if(x == tata[x]) + + return x; + + else + + return tata[x] = findtata(tata[x]); + +} + + + +void uniune(int x, int y) + +{ + + int sefx, sefy; + + sefx = findtata(x); + + sefy = findtata(y); + + tata[sefx] = sefy; + +} + + + +bool cmp(mama a, mama b) + +{ + + return a.cost < b.cost; + +} + +int main() + +{ + + + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + int i, n, m; + + + + fin >> n >> m; + + + + for(i = 1; i <= n; i ++) + + tata[i] = i; + + + + for(i = 1; i <= m; i ++) + + fin >> v[i].x >> v[i].y >> v[i].cost; + + + + sort(v + 1, v + m + 1, cmp); + + + + for(i = 1; i <= m; i ++) + + { + + if(findtata(v[i].x) != findtata(v[i].y)) + + { + + uniune(v[i].x, v[i].y); + + nrmuchii ++; + + s += v[i].cost; + + v[i].cost = 1001; + + } + + } + + fout << s << '\n'; + + fout << nrmuchii << '\n'; + + for(i = 1; i <= m; i ++) + + if(v[i].cost == 1001) + + fout << v[i].x << " " << v[i].y << '\n'; + + + + return 0; + + + +} diff --git a/mst/kruskal/ce278e77-946d-4ec9-b9e3-796b809be75f.cpp b/mst/kruskal/ce278e77-946d-4ec9-b9e3-796b809be75f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f573a6dcfdf66f5654ee8d29c8907392208b492e --- /dev/null +++ b/mst/kruskal/ce278e77-946d-4ec9-b9e3-796b809be75f.cpp @@ -0,0 +1,102 @@ +#include +#include + + + + + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct muchie + +{ + + int x, y, c; + + bool operator < (const muchie& other) const + + { + + return c < other.c; + + } + +}v[400000 +5]; + + + +int sef[200000 +5], n, m, sol[200000 +5]; + + + +int sefsuprem(int x) + +{ + + if(sef[x]==x) return x; + + sef[x] = sefsuprem(sef[x]); + + return sef[x]; + +} +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=m; i++) in>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1, v+m+1); + + + + for(int i=1; i<=n; i++) sef[i]=i; + + + + int cnt = 0, total = 0; + + for(int i=1; i<=m && cnt + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +struct tip{int x; int y; int c;}; + +bool operator <(tip a, tip b) + +{ + + return a.c>b.c; + +} + +priority_queueH; + +vectorsol; + +int n,m; + +int tata[200009]; + +int h[200009]; + +void citire(); + +void solve(); + + + +int aflare(int x); + + + +void unire(int x, int y); + +int main() + +{citire(); + + solve(); + + return 0; + +} + +void citire() + +{ tip val; + + int i; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>val.x>>val.y>>val.c; + + H.push(val); + + } + +} + +void solve() + +{int cx,cy,i; + + int cost=0; + + int nrsel=0; + + while(nrsel!=n-1 && !H.empty()) + + { + + tip act=H.top();H.pop(); + + int cx=aflare(act.x); + + int cy=aflare(act.y); + + if(cx!=cy) + + {unire(cx,cy);nrsel++;cost+=act.c;sol.push_back(act);} + + } + + fout<h[y]) + + tata[y]=x; + + else + + { + + if(h[y]>h[x]) + + tata[x]=y; + + else + + {h[y]++;tata[x]=y;} + + } + +} diff --git a/mst/kruskal/d305338d-6ddb-461b-bbc4-c216aff6ac0c.cpp b/mst/kruskal/d305338d-6ddb-461b-bbc4-c216aff6ac0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0fe58f9ca7ea5c551268599ef4176d4ecdf7b39 --- /dev/null +++ b/mst/kruskal/d305338d-6ddb-461b-bbc4-c216aff6ac0c.cpp @@ -0,0 +1,173 @@ +#include +#include + + +typedef struct _edge { + + int source, dest, cost; + +}edge; + + + +int Find(int x, int *c) { + + int y = x, t; + + while(y != c[y]) + + y = c[y]; + + + + while(x != y) { + + t = c[x]; + + c[x] = y; + + x = t; + + } + + + + return x; + +} + + + +void Unite(int x, int y, int *c) { + + x = Find(x, c); + + y = Find(y, c); + + c[y] = x; + +} + + + +int cmp_edges(const void *pa, const void *pb) { + + edge a = *(edge *)pa; + + edge b = *(edge *)pb; + + + + return a.cost-b.cost; + +} + + + +int APM_Kruskal(int n, int *c, int m, edge *e, edge *out) { + + int cost = 0, sz = 0; + + + + for(int i = 0; i < m; i++) { + + int x = e[i].source, y = e[i].dest, z = e[i].cost; + + if(Find(x, c) != Find(y, c)) { + + Unite(x, y, c); + + cost+=z; + + out[sz++] = e[i]; + + } + + } + + + + return cost; + +} + + + +int main() { + + + + FILE *input = fopen("apm.in", "r"); + + FILE *output = fopen("apm.out", "w"); + + int n, m, *c; + + edge *e, *out; + + + + fscanf(input, "%d%d", &n, &m); + + + + c = (int *) calloc(n + 1, sizeof(int)); + + e = (edge *) calloc(m, sizeof(edge)); + + out = (edge *) calloc(n - 1, sizeof(edge)); + + + + for(int i = 1; i <= n; i++) + + c[i] = i; + + + + for(int i = 0; i < m; i++) + + fscanf(input, "%d %d %d", &e[i].source, &e[i].dest, &e[i].cost); + + + + qsort(e, m, sizeof(edge), cmp_edges); + + + + int apm_cost = APM_Kruskal(n, c, m, e, out); + + fprintf(output, "%d\n", apm_cost); + + + + fprintf(output, "%d\n", n - 1); + + + + for(int i = 0; i < n - 1; i++) { + + fprintf(output, "%d %d\n", out[i].source, out[i].dest); + + } + + + + free(c); + + free(e); + + free(out); + + + + fclose(input); + + fclose(output); + + + + return 0; + +} diff --git a/mst/kruskal/d449e05e-f753-48dc-a471-c5c2c38448ef.cpp b/mst/kruskal/d449e05e-f753-48dc-a471-c5c2c38448ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7ec36e3c3e37ae7f5630b7e320a96cad4713e2b --- /dev/null +++ b/mst/kruskal/d449e05e-f753-48dc-a471-c5c2c38448ef.cpp @@ -0,0 +1,107 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("apm.in"); + +ofstream out ("apm.out"); + + + +struct ura{ + + int x,y,c,ok; + +}; + +ura v[400005]; + +bool cmp(ura a,ura b) + +{ + + return a.c>n>>m; + + for (i=1;i<=m;i++) + + in>>v[i].x>>v[i].y>>v[i].c; + + for (i=1;i<=n;i++) + + d[i]=i; + + sort(v+1,v+m+1,cmp); + + nr=cost=0; + + for(i=1;i<=m;i++) + + if(stramos(v[i].x)!=stramos(v[i].y)){ + + unire(i); + + cost+=v[i].c; + + v[i].ok=1; + + nr++; + + } + + out< + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int N = 2e5 + 5; + +int TT[N]; + +int RG[N]; + +long long ans_cost = 0; + +int n, m; + + + +struct edge + +{ + + int x, y, c; + +}; + +vector graph; + +vector >ans; + + + +bool cmp(edge A, edge B) + +{ + + return A.c < B.c; + +} + + + +int find_root(int node) + +{ + + int root = node, aux = node; + + + + while(TT[root] != 0) + + root = TT[root]; + + + + while(root != node) + + { + + aux = TT[node]; + + TT[node] = root; + + node = aux; + + } + + + + return root; + +} + + + +int unite(int x, int y) + +{ + + x = find_root(x); + + y = find_root(y); + + + + if(x == y) return 0; + + + + if(RG[x] < RG[y]) + + swap(x, y); + + + + TT[y] = x; + + RG[x] += RG[y]; + + + + return 1; + +} + + + +main() + +{ + + ios::sync_with_stdio(false); + + in.tie(0); + + + + in >> n >> m; + + + + while(m--) + + { + + edge M; + + in >> M.x >> M.y >> M.c; + + graph.push_back(M); + + } + + + + sort(graph.begin(), graph.end(), cmp); + + + + for(size_t i = 0; i < graph.size() && ans.size() < n - 1; i++) + + if(unite(graph[i].x,graph[i].y) != 0) + + { + + ans_cost += graph[i].c; + + ans.push_back({graph[i].x, graph[i].y}); + + } + + + + out << ans_cost << '\n' << n - 1 << '\n'; + + + + for(size_t i = 0; i < ans.size(); i++) + + out << ans[i].first << ' ' << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/d5172d8b-9296-4cb4-aca8-c0f6e8b7d3c2.cpp b/mst/kruskal/d5172d8b-9296-4cb4-aca8-c0f6e8b7d3c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c214d18d9a03650e81867cc1e3910af801b3fd29 --- /dev/null +++ b/mst/kruskal/d5172d8b-9296-4cb4-aca8-c0f6e8b7d3c2.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include + + +const int NMAX=200001; + + + +struct Muchie{ + + int x,y,cost; + +}aux; + + + +std::ofstream out("apm.out"); + +std::ifstream in("apm.in"); + + + +std::vectormuchii,sol; + +int tata[NMAX],h[NMAX]; + + + +int getRoot(int x){ + + while(x!=tata[x]) + + x=tata[x]; + + return x; + +} + + + +void unionSet(int x,int y){ + + if(h[x]h[y]) + + tata[y]=x; + + else tata[y]=x,h[x]++; + +} + + + +int main(){ + + int n,m,suma=0; + + in>>n>>m; + + for(int i=1;i<=n;i++) + + tata[i]=i,h[i]=1; + + for(int i=1;i<=m;i++) + + in>>aux.x>>aux.y>>aux.cost,muchii.push_back(aux); + + std::sort(muchii.begin(),muchii.end(), + + [](const Muchie & a,const Muchie &b){ + + return a.cost +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct Muchii{ + + int x, y, cost; + +}muchii[400005]; + + + +struct{ + + int x, y; + +}bune[400005]; + + + +int n, m, t[200005], s, k, rang[200005]; + + + +bool compare(Muchii a, Muchii b) + +{ + + return a.cost < b.cost; + +} + + + +void citire() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + fin >> muchii[i].x >> muchii[i].y >> muchii[i].cost; + + sort(muchii + 1, muchii + m + 1, compare); + + for(int i = 1; i <= n; i++) + + rang[i] = 1; + +} + + + +int radacina(int k) + +{ + + if(t[k] == 0) + + return k; + + else + + { + + int x = radacina(t[k]); + + t[k] = x; + + return x; + + } + +} + + + +void unire(int x, int y) + +{ + + if(rang[x] > rang[y]) + + t[y] = x; + + else + + { + + t[x] = y; + + if(rang[x] == rang[y]) + + rang[y]++; + + } + +} + + + + + + + +int main() + +{ + + citire(); + + for(int i = 1; i <= m; i++) + + { + + int tatal_x = radacina(muchii[i].x); + + int tatal_y = radacina(muchii[i].y); + + if(tatal_x != tatal_y) + + { + + unire(tatal_x, tatal_y); + + bune[++k].x = muchii[i].x; + + bune[k].y = muchii[i].y; + + s += muchii[i].cost; + + } + + } + + fout << s << '\n' << n - 1 << '\n'; + + for(int i = 1; i <= k; i++) + + fout << bune[i].x << ' ' << bune[i].y << '\n'; + +} diff --git a/mst/kruskal/d6937c6f-ab8a-487d-b7b1-925aba4ad502.cpp b/mst/kruskal/d6937c6f-ab8a-487d-b7b1-925aba4ad502.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e477d165dce0ac379b568bd3d81bef6e2f42fe5a --- /dev/null +++ b/mst/kruskal/d6937c6f-ab8a-487d-b7b1-925aba4ad502.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef tuple edge; +int n, m, cost, p[200009]; + +vector edges; + +vector apm; + + + +int get_set(int node) { + + if (p[node] != node) { + + p[node] = get_set(p[node]); + + } + + + + return p[node]; + +} + + + +void reunion_set(int x, int y) { + + p[get_set(x)] = get_set(y); + +} + + + +void Kruskal() { + + for (int i = 1; i <= n; ++i) { + + p[i] = i; + + } + + + + cost = 0; + + for (auto it = edges.begin(); it != edges.end(); ++it) { + + int x = std::get<1>(*it), y = std::get<2>(*it), c = std::get<0>(*it); + + + + if (get_set(x) != get_set(y)) { + + cost += c; + + apm.push_back(*it); + + reunion_set(x, y); + + } + + } + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + fin >> n >> m; + + + + for (int i = 0; i <= m; ++i) { + + int x, y, c; + + fin >> x >> y >> c; + + + + edges.push_back(edge(c, x, y)); + + } + + + + sort(edges.begin(), edges.end()); + + + + Kruskal(); + + + + fout << cost << "\n"; + + fout << (n - 1) << "\n"; + + for (auto it = apm.begin(); it != apm.end(); ++it) { + + fout << std::get<1>(*it) << " " << std::get<2>(*it) << "\n"; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/kruskal/d6bc5e9a-f137-4f06-a0b1-cef3d4b9fb25.cpp b/mst/kruskal/d6bc5e9a-f137-4f06-a0b1-cef3d4b9fb25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9407afb5d6be3075d22f257334af982af77ccfac --- /dev/null +++ b/mst/kruskal/d6bc5e9a-f137-4f06-a0b1-cef3d4b9fb25.cpp @@ -0,0 +1,162 @@ +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct muchie + +{ + + int x, y, cost; + +}; + + + +int pivot(int p, int q, muchie v[]) + +{ + + muchie x = v[p]; + + while(p= x.cost)q--; + + v[p]=v[q]; + + while(p> n>> m; + + int t[n+1], h[n+1]; + + muchie v[m+1], w[n]; + + for(int i = 1; i <= n; ++i)t[i]=h[i]=0; + + for(int i = 1; i <= m; ++i) + + { + + fin>> v[i].x >> v[i].y >> v[i].cost; + + } + + + + QS(1, m, v); + k=0; + + s=0; + + for(int i = 1; i<=m && k < n-1 ; ++i) + + { + + p = v[i].x; + + q = v[i].y; + + c = v[i].cost; + + rp = radacina(p, t); + + rq = radacina(q, t); + + if( rp != rq ) + + { + + s+=v[i].cost; + + w[++k] = v[i]; + + if(h[rp] > h[rq]) t[rq] = rp; + + else + + { + + t[rp] = rq; + + if(h[rp] == h[rq]) h[rq] ++; + + } + + } + + } + + + + fout << s<<'\n'< +#include +#include +struct s + +{ + + int n1, n2, muchii; + +} v[400005]; + + + +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + + + +vector < s > sol; + +vector < vector < pair < int, int > > > G; + +int stramos[200005], raspuns; + +int n, m; + + + +bool cmp(s a, s b) + +{ + + return a.muchii < b.muchii ; + +} + + + +int tata(int x) + +{ + + if (stramos[x] == x) + + return x; + + stramos[x] = tata(stramos[x]); + + + + + + return stramos[x]; + +} + + + +void reuniune(int x, int y) + +{ + + stramos[tata(x)] = tata(y); + +} + + + +void citire() + +{ + + cin >> n >> m; + + + + G.resize(n + 5); + + for(int i = 1 ; i <= m ; i++) + + cin >> v[i].n1 >> v[i].n2 >> v[i].muchii, + + G[v[i].n1].push_back(make_pair(v[i].n2, v[i].muchii)), + + G[v[i].n2].push_back(make_pair(v[i].n1, v[i].muchii)); + + + + + +} + + + +void prep() + +{ + + sort(v + 1, v + m + 1, cmp); + + + + for(int i = 1 ; i <= n ; i++) + + stramos[i] = i; + +} + + + +void rez() + +{ + + int x, y, aux; + + + + for(int i = 1 ; i <= m ; i++) + + if (tata(v[i].n1) != tata(v[i].n2)) + + { + + raspuns += v[i].muchii, reuniune(v[i].n1, v[i].n2), + + sol.push_back(v[i]); + + } + +} + + + +void afis() + +{ + + cout << raspuns << '\n' << sol.size() << '\n'; + + + + for(int i = 0 ; i < sol.size() ; i++) + + cout << sol[i].n1 << ' ' << sol[i].n2 << '\n'; + +} + + + +int main() + +{ + + citire(); + + prep(); + + rez(); + + afis(); + + return 0; + +} diff --git a/mst/kruskal/d7604993-cc1a-4df6-93a7-a4d903373321.cpp b/mst/kruskal/d7604993-cc1a-4df6-93a7-a4d903373321.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d7f5cef8e55d62c94d3217fb686f743857c42dc --- /dev/null +++ b/mst/kruskal/d7604993-cc1a-4df6-93a7-a4d903373321.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +std::ifstream in("apm.in"); + +std::ofstream out("apm.out"); + +struct graf + +{ + + int u,v,c,sel; + + bool operator <(const graf&other)const + + { + + return c < other.c; + + } + +}; + +graf v[400005]; + +int t[200005],h[200005]; + +int fs(int x) + +{ + + while(t[x] != x) + + x = t[x]; + + return x; + +} + +void us(int tx,int ty) + +{ + + if(h[tx] == h[ty]) + + t[ty] = tx,h[tx]++; + + else if(h[tx] > h[ty]) + + t[ty] = tx; + + else + + t[tx] = ty; + +} + +void init(int n) + +{ + + for(int i = 1;i <= n;i++) + + t[i] = i,h[i] = 1; + +} + +int main() + +{ + + int n,m; + + in>>n>>m; + + init(n); + + for(int i = 1;i <= m;i++) + + in>>v[i].u>>v[i].v>>v[i].c; + + std::sort(v + 1,v + m + 1); + + int ct = 0,nr = 0,tx,ty; + + for(int i = 1;i <= m && nr < n - 1;i++) + + { + + tx = fs(v[i].u); + + ty = fs(v[i].v); + + if(tx != ty) + + { + + us(tx,ty); + + ct += v[i].c; + + v[i].sel = 1; + + nr++; + + } + + } + + out< //Andrei Alexandru a.k.a Sho10 + + + + + +using namespace std; + +long long n,m,p[200005],second[200005],sum=0; + +pair>g[200005]; + +vector>ans; + +long long caut(long long x){ + +if(x==p[x]){ + + return x; + +}else return p[x]=caut(p[x]); + +} + +void uni(long long x,long long y){ + +x=caut(x); + +y=caut(y); + +if(x!=y){ + + if(second[x]>n>>m; + +for(long long i=1;i<=n;i++) + +{ + + p[i]=i; + +} + +for(long long i=1;i<=m;i++) + +{ + + cin>>g[i].second.first>>g[i].second.second>>g[i].first; + +} + +sort(g+1,g+m+1); + +for(long long i=1;i<=m;i++) + +{ + + long long x=g[i].second.first,y=g[i].second.second; + + if(caut(x)!=caut(y)){ + + uni(x,y); + + sum+=g[i].first; + + ans.push_back(make_pair(x,y)); + + } + +} + +cout< +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct ura + +{ + + int nr1; + + int nr2; + + int c; + +}; + +int sef[200001]; + +ura v[400001]; + +pair raspuns[200000]; + +int sefsuprem(int nod) + +{ + + if(sef[nod]==nod) + + return nod; + + else + + return sef[nod]=sefsuprem(sef[nod]); + +} + +void unire(int nod1, int nod2) + +{ + + int sef1, sef2; + + sef1=sefsuprem(nod1); + + sef2=sefsuprem(nod2); + + sef[sef2]=sef1; + +} + +bool cmp(ura a, ura b) + +{ + + return a.c < b.c; + +} + +int main() + +{ + + int n, m, sef1, sef2, k, s; + + cin>>n>>m; + + for(int i=1; i<=n; i++) + + sef[i]=i; + + for(int i=1; i<=m; i++) + + { + + cin>>v[i].nr1>>v[i].nr2>>v[i].c; + + } + + sort(v+1, v+m+1, cmp); + + k=n; + + s=0; + + for(int i=1; i<=m && k>0; i++) + + { + + sef1=sefsuprem(v[i].nr1); + + sef2=sefsuprem(v[i].nr2); + + if(sef1 != sef2) + + { + + k--; + + s+=v[i].c; + + unire(v[i].nr1, v[i].nr2); + + raspuns[n-k].first=v[i].nr1; + + raspuns[n-k].second=v[i].nr2; + + } + + } + + cout< +#include + + + + + + +using namespace std; + + + +struct muchie{int x,y,c;}; + +int n,m,i,t[200001],h[200001],k,cost,sol[200000]; + +muchie a[400001]; + + + +int compare(muchie x,muchie y) + +{ + + return x.ch[r2]) t[r2]=r1; + +else { t[r1]=r2; + + if (h[r1]==h[r2]) h[r2]++; + + } + + + +return 1; + +} + + + +int main() + +{FILE *f1 = fopen("apm.in","r"); + +FILE *f2 = fopen("apm.out","w"); + +fscanf(f1,"%d%d",&n,&m); + +for (i=1;i<=m;i++) {fscanf(f1,"%d%d%d",&a[i].x,&a[i].y,&a[i].c);} + +sort(a+1,a+m+1,compare); + +for (i=1;i<=n;i++) {t[i]=i;h[i]=1;} + +k=0; + + + +i=1; + +while (k +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +int T[200010], S[200010]; + +int a,b,Ra,Rb,n,m,i,sol,nr; + +int R(int x) + +{ + + while(T[x]>0) + + x=T[x]; + + + + return x; + +} + + + +pair< int, pair > V[400010]; + + + +int main () + +{ + + fin>>n>>m; + + for(i=1;i<=m;i++) + + fin>>V[i].second.first>>V[i].second.second>>V[i].first; + + sort(V+1,V+m+1); + + + + for(i=1;i<=n;i++) + + T[i]=-1; + + for(i=1;i<=m;i++) + + { + + a=V[i].second.first; + + b=V[i].second.second; + + + + Ra=R(a); + + Rb=R(b); + + + + if(Ra!=Rb) + + { + + if(-T[Ra]>-T[Rb]) + + { + + T[Ra]=T[Ra]+T[Rb]; + + T[Rb]=Ra; + + } + + else + + { + + T[Rb]=T[Rb]+T[Ra]; + + T[Ra]=Rb; + + } + + sol=sol+V[i].first; + + + + nr++; + + S[nr]=i; + + if(nr==n-1) + + break; + + } + + } + + fout< +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; +const int Nmax = 2e5+7; + +int TT[Nmax]; +struct edge { + int a,b,c; +} e[Nmax]; + +inline bool cmp(const edge &u, const edge &v) { return u.c < v.c; } + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int root(int a) { + if (a == TT[a]) { return a; } + return TT[a] = root(TT[a]); +} + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + int n, m; + fin >> n >> m; + for(int i = 0; i < (int)(m); i++) { + fin >> e[i].a >> e[i].b >> e[i].c; + } + sort(e, e+m, cmp); + for(int i = 0; i < (int)(n+1); i++) { TT[i] = i;} + + int cost = 0; + vi res; + for(int i = 0; i < (int)(m); i++) { + if (root(e[i].a) != root(e[i].b)) { + TT[root(e[i].b)] = root(e[i].a); + cost += e[i].c; + res.push_back(i); + } + } + fout << cost << '\n'; + fout << res.size() << '\n'; + for(auto i: res) { + fout << e[i].a << " " << e[i].b << '\n'; + } + + return 0; +} diff --git a/mst/kruskal/db0eb252-098c-4e59-b6d9-f9cc120e277b.cpp b/mst/kruskal/db0eb252-098c-4e59-b6d9-f9cc120e277b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1dd7065fb725fba96948237bfb0abe0e5c0afc5b --- /dev/null +++ b/mst/kruskal/db0eb252-098c-4e59-b6d9-f9cc120e277b.cpp @@ -0,0 +1,119 @@ +#include +#include + + +using namespace std; + + + +struct muchie { + + int cost; + + int a, b; + + bool operator<(const muchie& alt) const { + + return this->cost < alt.cost; + + } + +}; + + + +const int N = 2e5 + 1, M = 4e5; + + + +int sef[N]; + +muchie mc[M], rez[N]; + + + +int sefsuprem(int nod) { + + if (sef[nod] == nod) + + return nod; + + return sef[nod] = sefsuprem(sef[nod]); + +} + + + +bool unire(int a, int b) { + + int sef_a, sef_b; + + sef_a = sefsuprem(a); + + sef_b = sefsuprem(b); + + if (sef_a == sef_b) + + return false; + + sef[sef_a] = sef_b; + + return true; + +} + + + +int main() { + + ifstream in("apm.in"); + + ofstream out("apm.out"); + + + + int n, m; + + in >> n >> m; + + for (int i = 1; i <= n; ++i) + + sef[i] = i; + + for (int i = 0; i < m; ++i) + + in >> mc[i].a >> mc[i].b >> mc[i].cost; + + sort(mc, mc + m); + + int poz = 0, cost = 0; + + for (int i = 0; i < n - 1; ++i) { + + while (!unire(mc[poz].a, mc[poz].b)) + + ++poz; + + cost += mc[poz].cost; + + rez[i] = mc[poz]; + + ++poz; + + } + + out << cost << '\n' << n - 1 << '\n'; + + for (int i = 0; i < n - 1; ++i) + + out << rez[i].a << ' ' << rez[i].b << '\n'; + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/mst/kruskal/db1b2562-577b-4489-900d-28cac1b297f9.cpp b/mst/kruskal/db1b2562-577b-4489-900d-28cac1b297f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59303293f9341038f93f34d43b780d2a44f5f469 --- /dev/null +++ b/mst/kruskal/db1b2562-577b-4489-900d-28cac1b297f9.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +pair< int, pair > G[400010]; + +int n, m,a,b,ra,rb,sol,alese; + +int t[200010], S[200010]; + + + +int rad(int x) { + + + + while (t[x] > 0) + + x = t[x]; + + return x; + +} + + + +int main() + +{ + + + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + { + + fin >> G[i].second.first; + + fin >> G[i].second.second; + + fin >> G[i].first; + + } + + sort(G + 1, G + m + 1); + + for (int i = 1; i <= n; i++) + + t[i] = -1; + + for (int i = 1; i <= m; i++) { + + a = G[i].second.first; + + b = G[i].second.second; + + ra = rad(a); + + rb = rad(b); + + if (ra != rb) { + + if (-t[ra] > -t[rb]) { + + + + t[ra] += t[rb]; + + t[rb] = ra; + + } + + else { + + t[rb] += t[ra]; + + t[ra] = rb; + + } + + sol += G[i].first; + + + + alese++; + + S[alese] = i; + + if (alese == n - 1) + + break; + + } + + } + + + + fout << sol << "\n" << n - 1 << "\n"; + + for (int i = 1; i <= alese; i++) + + fout << G[S[i]].second.first << " " << G[S[i]].second.second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/dc2c0d05-33f5-4f48-8e3b-c48df709e3c0.cpp b/mst/kruskal/dc2c0d05-33f5-4f48-8e3b-c48df709e3c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63a6ad77ab9dbb2f3c346b5acba455ef67bead11 --- /dev/null +++ b/mst/kruskal/dc2c0d05-33f5-4f48-8e3b-c48df709e3c0.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + +vector daddy; +vector height; + +class Edge{ + public: + Edge(int x, int y, int c):x_(x), y_(y), cost_(c){} + int from() const {return x_;} + int to() const {return y_;} + int cost() const {return cost_;} + bool operator<(const Edge& other) {return cost() < other.cost();} + private: + int x_, y_, cost_; +}; + +int find_parent(int k) +{ + if (daddy[k] != k) + daddy[k] = find_parent(daddy[k]); + return daddy[k]; +} + +int Union(int x, int y) +{ + x = find_parent(x); + y = find_parent(y); + + if (height[x] > height[y]) + swap(x,y); + + if (height[x] == height[y]) + ++height[y]; + + daddy[x] = y; +} + + +int main() +{ + freopen("apm.in", "r", stdin); + freopen("apm.out", "w", stdout); + + vector edges, selected; + + int N, M; + scanf("%d%d", &N, &M); + daddy.resize(N); + height.resize(N); + for (int i = 0; i < N; ++i) + daddy[i] = i; + + for (int i = 0; i < M; ++i) { + int a, b, c; + scanf("%d%d%d", &a, &b, &c); + --a; --b; + edges.emplace_back(a, b, c); + } + + sort(edges.begin(), edges.end()); + long long answer = 0; + for (auto e : edges) { + if (find_parent(e.from()) != find_parent(e.to())) { + selected.emplace_back(e); + Union(e.from(), e.to()); + answer += e.cost(); + } + } + printf("%lld\n%d\n", answer, (int)selected.size()); + for (auto e : selected) + printf("%d %d\n", e.from() + 1, e.to() + 1); + return 0; +} diff --git a/mst/kruskal/dca1be66-52b9-45d6-b6e6-7dacff1fd5ec.cpp b/mst/kruskal/dca1be66-52b9-45d6-b6e6-7dacff1fd5ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb08a66528ecc310f3757bc546a53a1c5faad489 --- /dev/null +++ b/mst/kruskal/dca1be66-52b9-45d6-b6e6-7dacff1fd5ec.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +struct muchie + +{ + + int c,nod1,nod2; + +}; + +muchie edge[400005]; + +int dad[200005],grad[200005],n,m; + +vector > sol; + +int cost=0; + +bool cmp(muchie a,muchie b) + +{ + + return a.cgrad[y]) + + { + + dad[y]=x; + + } + + else + + { + + dad[x]=y; + + if(grad[y]==grad[x]) + + grad[y]++; + + } + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + f>>edge[i].nod1>>edge[i].nod2>>edge[i].c; + + } + + + + sort(edge+1,edge+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int x=root(edge[i].nod1); + + int y=root(edge[i].nod2); + + if(x!=y) + + { + + unite(x,y); + + sol.push_back({edge[i].nod1,edge[i].nod2}); + + cost=cost+edge[i].c; + + } + + + + } + + g< + + + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, cost; + +int parent[200005], card[200005]; + +vector < pair< int, int > > ans; + +priority_queue < pair < int, pair< int, int > >, vector < pair < int, pair< int, int > > >, greater < pair < int, pair< int, int > > > > q; + + + +int find_parent(int nod){ + + if(parent[nod] != nod){ + + parent[nod] = find_parent(parent[nod]); + + return parent[nod]; + + } + + return nod; + +} + + + +void reunion(int x, int y){ + + x = find_parent(x); + + y = find_parent(y); + + if(card[x] < card[y]) + + swap(x, y); + + card[x] += card[y]; + + parent[y] = x; + +} + + + +void answer(){ + + while(ans.size() != n-1){ + + int c = q.top().first; + + int x = q.top().second.first; + + int y = q.top().second.second; + + q.pop(); + + if(find_parent(x) == find_parent(y)) + + continue; + + ans.push_back({x, y}); + + cost += c; + + reunion(x, y); + + } + +} + + + +void read(){ + + f>>n>>m; + + for(int i = 1; i <= n; ++i){ + + parent[i] = i; + + card[i] = 1; + + } + + int c, x, y; + + for(int i = 1; i <= m; ++i){ + + f>>x>>y>>c; + + q.push({c, {x, y}}); + + } + + answer(); + + g< +#include +#include +#include +using namespace std; + + + +struct muchie { + + int nod1, nod2, cost; + + + + muchie(int n1 = 0, int n2 = 0, int c = 0) { + + nod1 = n1, nod2 = n2, cost = c; + + } + +}; + + + +int n, m, capm, nm; + +int repr[200000 + 5], apm[200000 + 5]; + +vector v[200000 + 5]; + +muchie muchii[400000 + 5]; + + + +bool cmp(muchie m1, muchie m2) { + + return m1.cost < m2.cost; + +} + + + +int get_repr(int nod) { + + while(nod != repr[nod]) + + nod = repr[nod]; + + return nod; + +} + + + +void union_repr(int nod, int rnod) { + + while(nod != repr[nod]) { + + int aux = nod; + + nod = repr[nod]; + + repr[aux] = rnod; + + } + + repr[nod] = rnod; + +} + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int x, y, z; + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= n; i++) + + repr[i] = i; + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &z); + + muchii[i] = muchie(x, y, z); + + } + + + + sort(muchii + 1, muchii + m + 1, cmp); + + nm = 0; + + for(int i = 1; i <= m && nm < n - 1; i++) { + + int n1 = muchii[i].nod1, n2 = muchii[i].nod2, c = muchii[i].cost; + + int r1 = get_repr(n1), r2 = get_repr(n2); + + + + if(r1 == r2) + + continue; + + + + capm += c; + + apm[++nm] = i; + + union_repr(n1, r2); + + } + + + + printf("%d\n%d\n", capm, nm); + + for(int i = 1; i <= nm; i++) + + printf("%d %d\n", muchii[apm[i]].nod1, muchii[apm[i]].nod2); + + + + return 0; + +} diff --git a/mst/kruskal/dd2ccb1f-3cd6-4dbc-b6f9-9e84788ee335.cpp b/mst/kruskal/dd2ccb1f-3cd6-4dbc-b6f9-9e84788ee335.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0bd7f53e3c5aeae6df3918b7455d935fe6866191 --- /dev/null +++ b/mst/kruskal/dd2ccb1f-3cd6-4dbc-b6f9-9e84788ee335.cpp @@ -0,0 +1,142 @@ +#include + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m; + +int cost; + +int parent[200005]; + +int card[200005]; + +vector >ans; + +priority_queue >,vector > >,greater > > >pq; + +int find_parent(int pos) + +{ + + + + if (parent[pos]!=pos) + + { + + parent[pos]=find_parent(parent[pos]); + + return parent[pos]; + + } + + return pos; + +} + +void reuniune(int x,int y) + +{ + + + + + + + + x=find_parent(x); + + y=find_parent(y); + + + + if (card[x]>n>>m; + + int c,x,y; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + pq.push({c,{x,y}}); + + } + + for(int i=1;i<=n;i++) + + { + + + + parent[i]=i; + + card[i]=i; + + } + + compute_answer(); + + fout< + +using namespace std; + +struct muchie { + int a; + int b; + int cost; +} graf[400005]; +vector rasp; +int cost; +int t[200005]; +int h[200005]; +int n,m; +ifstream f("apm.in"); +ofstream g("apm.out"); + +int find_tata(int x) { + if(x==t[x]) + return x; + return find_tata(t[x]); +} + +void uneste(int x, int y){ + x = find_tata(x); + y = find_tata(y); + if(h[x] == h[y]) { + t[x] = y; + h[y] ++; + } + else if (h[x] < h[y]) { + t[x]=y; + } + else { + t[y]=x; + } + +} +bool verifica(int x, int y) { + return find_tata(x) == find_tata(y); +} + +int main() { + f>>n>>m; + for(int i=1;i<=m;i++) + { + int x,y,c; + f>>graf[i].a>>graf[i].b>>graf[i].cost; + } + sort(graf+1,graf+m+1,[](muchie a, muchie b){return a.cost +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int x, y, w; + + inline bool operator<(muchie m) + + {return (w v; + +vector t, r; + +int n, m; + + + +void rd() + +{ + + f>>n>>m; + + v.resize(m); + + t.resize(n+1); + + for(int i=0; i>v[i].x>>v[i].y>>v[i].w; + + for(int i=1; i<=n; i++) + + t[i]=i; + +} + + + +int tata(int x) + +{ + + if(t[x]!=x) + + t[x]=tata(t[x]); + + return t[x]; + +} + + + +inline void unite(int x, int y) + +{ + + t[tata(x)]=tata(y); + +} + + + +inline bool q(int x, int y) + +{ + + return (tata(x)==tata(y)); + +} + + + +inline int Kruskal() + +{ + + int cost=0, i=0; + + sort(v.begin(), v.end()); + + while(r.size()!=n-1) + + { + + while(q(v[i].x, v[i].y)) + + i++; + + r.push_back(i); + + cost+=v[i].w; + + unite(v[i].x, v[i].y); + + i++; + + } + + return cost; + +} + + + +int main() + +{ + + rd(); + + + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream cin("apm.in"); +ofstream cout("apm.out"); + +const int MAXM = 4e5 + 100; +const int MAXN = 2e5 + 100; + +struct muchie{ + int x , y , c; +} mat [MAXM] ; + +bool cmp(muchie a , muchie b){ + return a.c < b.c; +} + +int n, m, cost; +int tata [MAXN]; +vector ans; + +void read() { + cin >> n >> m; + for (int i = 1; i <= m; i++) + cin >> mat[i].x >> mat[i].y >> mat[i].c; + for (int i = 1; i <= n; i++) + tata[i] = i; +} + +int cureCancerFunc (int node){ + if (node != tata[node]) + tata[node] = cureCancerFunc(tata[node]); + return tata[node]; +} + +void unite (int a , int b){ + cureCancerFunc(b); + cureCancerFunc(a); + tata[tata[b]] = tata[a]; +} + +void solve() { + read(); + sort(mat + 1, mat + 1 + m, cmp); + for (int i = 1; i <= m; i++) { + muchie now = mat[i]; + int x, y; + x = cureCancerFunc(now.x); + y = cureCancerFunc(now.y); + if (x != y) { + unite(x, y); + ans.push_back(now); + cost += now.c; + } + } + cout << cost << '\n' << ans.size() << '\n'; + for (auto& x : ans) { + cout << x.x << " " << x.y << '\n'; + } +} + +int main() { + + solve(); + + return 0; +} diff --git a/mst/kruskal/e26ca5bd-3ee7-4907-a0db-1582826a7bf2.cpp b/mst/kruskal/e26ca5bd-3ee7-4907-a0db-1582826a7bf2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb6aee55354a29120dd939375bdc4df50fd40705 --- /dev/null +++ b/mst/kruskal/e26ca5bd-3ee7-4907-a0db-1582826a7bf2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int i, j, c; + +}; + +int n, m, + + cost; + + + +muchie M[400001]; + +int CC[200001]; + +int NrM[200000]; + + + +void citire() + +{ + + f >> n >> m; + + for(int i = 1; i <= m; i++) + + f >> M[i].i >> M[i].j >> M[i].c; + +} + + + +void afis() + +{ + + g << cost << '\n'; + + g << n - 1 << '\n'; + + for(int i = 1; i < n; i++) + + g << M[NrM[i]].i << ' ' << M[NrM[i]].j << '\n'; + +} + + + +bool comp(const muchie &a, const muchie &b) + +{ + + return a.c < b.c; + +} + + + +int Find(int i) + +{ + + if(CC[i] == 0) + + return i; + + return CC[i] = Find(CC[i]); + +} + + + +void Kruskal() + +{ + + sort(M + 1, M + m + 1, comp); + + int nm = 0; + + for(int i = 1; i <= m; i++) + + { + + int ci = Find(M[i].i); + + int cj = Find(M[i].j); + + if(ci != cj) + + { + + cost += M[i].c; + + CC[ci] = cj; + + NrM[++nm] = i; + + if(nm == n - 1) + + break; + + } + + } + +} + + + +int main() + +{ + + citire(); + + Kruskal(); + + afis(); + + return 0; + +} diff --git a/mst/kruskal/e353a715-7a18-4422-b6c3-4ee2dc3c4a87.cpp b/mst/kruskal/e353a715-7a18-4422-b6c3-4ee2dc3c4a87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f331915adf2929f7983813f59596ce9cd3f5c0c --- /dev/null +++ b/mst/kruskal/e353a715-7a18-4422-b6c3-4ee2dc3c4a87.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + +int h[200005],t[200005]; + +struct aj + +{ + + int x,y,c; + +}; + +vectorv; + +bool cmp(aj a, aj b) + +{ + + if(a.c==b.c) + + { + + if(a.x==b.x) return a.yh[yy]) t[yy]=xx; + + else if(h[yy]>h[xx]) t[xx]=yy; + + else t[yy]=xx,h[xx]++; + +} + +struct ans + +{ + + int x,y; + +}; + +vectoran; + +int main() + +{ + + aj ha; + + freopen("apm.in","r",stdin); + + freopen("apm.out","w",stdout); + + int n,m,x,x1,x2,cnt=0; + + scanf("%d%d",&n,&m); + + for(register int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&x1,&x2); + + ha.x=x; + + ha.y=x1; + + ha.c=x2; + + v.push_back(ha); + + } + + sort(v.begin(),v.end(),cmp); + + for(register int i=1;i<=n;i++) + + { + + t[i]=i; + + h[i]=1; + + } + + int cost=0; + + for(register int i=0;i +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m,t[200001],d[200001],ct; + +bool bag[400001]; + +vectorv[200001]; + +pair>mu[400001]; + +int rep(int x) + +{ + + if(t[x]==0) + + return x; + + t[x]=rep(t[x]); + + return t[x]; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>mu[i].second.first>>mu[i].second.second>>mu[i].first; + + v[mu[i].second.first].push_back(mu[i].second.second); + + v[mu[i].second.second].push_back(mu[i].second.first); + + } + + sort(mu+1,mu+m+1); + + for(int i=1;i<=m;i++) + + { + + int x=rep(mu[i].second.second); + + int y=rep(mu[i].second.first); + + if(x!=y) + + { + + if(d[x]>d[y]) + + { + + t[y]=x; + + d[y]++; + + } + + else if(d[x] +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct muchie + +{ + + int x, y; + + short c; + +}m[400005]; + + + +int N, M, cost, ct; + +int tata[200005], id[200005]; + + + +bool comp(muchie a, muchie b) + +{ + + return a.c < b.c; + +} + + + +void citire() + +{ + + in >> N >> M; + + for(int i = 1; i <= M; i++) + + in >> m[i].x >> m[i].y >> m[i].c; + + + + sort(m + 1, m + M + 1, comp); + +} + + + +int gasire(int x) + +{ + + if(tata[x] != x) + + tata[x] = gasire(tata[x]); + + return tata[x]; + +} + + + +void unire(int a, int b) + +{ + + if(a != b) + + if(a < b) + + tata[b] = a; + + else + + tata[a] = b; + +} + + + +void kruskal() + +{ + + for(int i = 1; i <= M; i++) + + { + + int a = gasire(m[i].x); + + int b = gasire(m[i].y); + + if(a != b) + + { + + unire(a, b); + + cost += m[i].c; + + id[++ct] = i; + + } + + } + +} + + + +int main() + +{ + + citire(); + + for(int i = 1; i <= N; i++) + + tata[i] = i; + + + + kruskal(); + + + + out << cost << '\n' << ct << '\n'; + + for(int i = 1; i <= N - 1; i++) + + out << m[id[i]].x << ' ' << m[id[i]].y << '\n'; + +} diff --git a/mst/kruskal/e5a2062c-c125-4a38-ba31-69fa8faf28f0.cpp b/mst/kruskal/e5a2062c-c125-4a38-ba31-69fa8faf28f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b658f8f0490091d8895914c3425a3832ef1c699c --- /dev/null +++ b/mst/kruskal/e5a2062c-c125-4a38-ba31-69fa8faf28f0.cpp @@ -0,0 +1,174 @@ +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int N = 2e5 + 5; + +int TT[N]; + +int RG[N]; + +long long ans_cost = 0; + +int n, m; + + + +struct edge + +{ + + int x, y, c; + +}; + +vector graph; + +vector >ans; + + + +bool cmp(edge A, edge B) + +{ + + return A.c < B.c; + +} + + + +int find_root(int node) + +{ + + int root = node, aux = node; + + + + while(TT[root] != 0) + + root = TT[root]; + + + + while(root != node) + + { + + aux = TT[node]; + + TT[node] = root; + + node = aux; + + } + + + + return root; + +} + + + +int unite(int x, int y) + +{ + + x = find_root(x); + + y = find_root(y); + + + + if(x == y) return 0; + + + + if(RG[x] < RG[y]) + + swap(x, y); + + + + TT[y] = x; + + RG[x] += RG[y]; + + + + return 1; + +} + + + +main() + +{ + + ios::sync_with_stdio(false); + + in.tie(0); + + + + in >> n >> m; + + + + while(m--) + + { + + edge M; + + in >> M.x >> M.y >> M.c; + + graph.push_back(M); + + } + + + + sort(graph.begin(), graph.end(), cmp); + + + + for(size_t i = 0; i < graph.size() && ans.size() < n - 1; i++) + + if(unite(graph[i].x,graph[i].y) != 0) + + { + + ans_cost += graph[i].c; + + ans.push_back({graph[i].x, graph[i].y}); + + } + + + + out << ans_cost << '\n' << n - 1 << '\n'; + + + + for(size_t i = 0; i < ans.size(); i++) + + out << ans[i].first << ' ' << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/kruskal/e5fcbcd3-b4e9-48f6-9a80-657bc65c47a6.cpp b/mst/kruskal/e5fcbcd3-b4e9-48f6-9a80-657bc65c47a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cfd2109ab431de42d34f1315b16509ceff615bc --- /dev/null +++ b/mst/kruskal/e5fcbcd3-b4e9-48f6-9a80-657bc65c47a6.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +int n, m, r[200001], t[200001], ct, poz[200000], k; + +pair < int, pair > mu[400001]; + + + +int cauta (int a) { + + int x = a, y; + + while (x != t[x]) + + x = t[x]; + + while (a != t[a]) { + + y = t[a]; + + t[a] = x; + + a = y; + + } + + return x; + +} + + + +void uneste (int a, int b) { + + int x = cauta (a); + + int y = cauta (b); + + if (r[x] < r[y]) + + t[x] = y; + + else + + t[y] = x; + + if (r[x] == r[y]) + + r[x]++; + +} + + + +void kruskal () { + + sort (mu + 1, mu + m + 1); + + for (int i = 1; i <= m; i++) + + if (cauta (mu[i].second.first) != cauta (mu[i].second.second)) { + + poz[++k] = i; + + uneste (mu[i].second.first, mu[i].second.second); + + ct += mu[i].first; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= n; i++) { + + r[i] = 1; t[i] = i; + + } + + int x, y, c; + + for (int i = 1; i <= m; i++) { + + fin >> x >> y >> c; + + mu[i].first = c; mu[i].second = {x, y}; + + } + + kruskal (); + + fout << ct << '\n'; + + fout << k << '\n'; + + for (int i = 1; i <= k; i++) + + fout << mu[poz[i]].second.first << ' ' << mu[poz[i]].second.second << '\n'; + + return 0; + +} diff --git a/mst/kruskal/e6323a87-21e0-4103-b1e3-95c96d18f454.cpp b/mst/kruskal/e6323a87-21e0-4103-b1e3-95c96d18f454.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7423268595f55e67cfc8395bd7398cd05f2020e --- /dev/null +++ b/mst/kruskal/e6323a87-21e0-4103-b1e3-95c96d18f454.cpp @@ -0,0 +1,227 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + + + + + +int n,m,x,y,c; + + + + + +vector > Muchii; + +vector < pair > muchii_sol; + +bool cmp(tuple < int ,int , int > st, tuple < int ,int , int > dr) + +{ + + if(get<2>(st) < get<2>(dr)) + + return 1; + + return 0; + +} + + + + void Citire() + + { + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + Muchii.push_back(make_tuple(x,y,c)); + + } + + + + + + + + sort(Muchii.begin(),Muchii.end(),cmp); + + } + + + +int sol1=0; + +int tata[200001],h[200001]; + + + + + +int Find(int x) + +{ + + int r=x,y; + + while(tata[r]) + + { + + r=tata[r]; + + + + } + + + + while(tata[x]) + + { + + y=tata[x]; + + tata[x]=r; + + x=y; + + } + + + + + + return r; + + + +} + + + +void Union(int x,int y) + +{ + + if(h[x]>h[y]) + + { + + tata[y]=x; + + + + } + + else + + { + + tata[x]=y; + + if(h[x]==h[y]) + + h[y]++; + + + + } + + + + + +} +void Kruskal() + +{ + + for(int i=0;i(Muchii[i])); + + int B=Find(get<1>(Muchii[i])); + + + + if(A!=B) + + {Union(A,B); + + sol1+=get<2>(Muchii[i]); + + muchii_sol.push_back(make_pair(get<1>(Muchii[i]),get<0>(Muchii[i]))); + + } + + } + + + + + +} + +void Afisare() + +{ + + + + fout< +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int NMax = 400005; + +pair P[NMax]; + +int N, M, Total, TT[NMax], k, RG[NMax]; + +struct Edge + +{ + + int x, y, c; + + + +}V[NMax]; + +bool Compare(Edge a, Edge b) + +{ + + return a.c < b.c; + +} + +void Read() + +{ + + fin >> N >> M; + + for (int i = 1; i <= M; i++) + + fin >> V[i].x >> V[i].y >> V[i].c; + + sort(V + 1, V + M + 1, Compare); + + for (int i = 1; i <= M; i++) + + cout << V[i].x << " " << V[i].y << " " << V[i].c << "\n"; + +} + +int Find(int nod) + +{ + + while (TT[nod] != nod) + + nod = TT[nod]; + + return nod; + +} + +void Unite(int x, int y) + +{ + + if (RG[x] < RG[y]) + + TT[x] = y; + + if (RG[y] < RG[x]) + + TT[y] = x; + + if (RG[x] == RG[y]) + + { + + TT[x] = y; + + RG[y]++; + + } + +} + +void Solve() + +{ + + for (int i = 1; i <= M; i++) + + { + + + + if (Find(V[i].x) != Find(V[i].y)) + + { + + + + Unite(Find(V[i].x), Find(V[i].y)); + + P[++k].first = V[i].x; + + P[k].second = V[i].y; + + Total += V[i].c; + + } + + } + +} + +int main() + +{ + + Read(); + + for (int i = 1; i <= M; i++) + + { + + TT[i] = i; + + RG[i] = 1; + + } + + Solve(); + + fout << Total << "\n"; + + fout << N - 1 << "\n"; + + for (int i = 1; i <= k; i++) + + fout << P[i].first << " " << P[i].second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/e69ca977-7958-472d-9cae-0106ab938b66.cpp b/mst/kruskal/e69ca977-7958-472d-9cae-0106ab938b66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a13072b114a10fe6a0d0cce7deb90e0ce6446292 --- /dev/null +++ b/mst/kruskal/e69ca977-7958-472d-9cae-0106ab938b66.cpp @@ -0,0 +1,209 @@ +#include +#include +using namespace std; + + + +const int NMAX = 2 * 1e5 + 2; + + + +std::ifstream fin("apm.in"); + +std::ofstream fout("apm.out"); + + + +typedef struct nod + +{ + + int rank; + + int key; + + nod* p; + +} + +NodeT; + + + +nod *a[NMAX]; + + + +struct muchie + +{ + + int c1; + + int c2; + + int cost; + +}; + + + +struct arbore + +{ + + int x, y; + +}; + + + +muchie g[2 * NMAX]; + +arbore v[NMAX]; + + + +int t[NMAX]; + + + +void MAKE_SET(int x) + +{ + + a[x] = (nod*)malloc(sizeof a[x]); + + a[x]->key = x; + + a[x]->rank = 0; + + a[x]->p = a[x]; + +} + + + +NodeT* &FIND_SET(NodeT* &x) + +{ + + if (x == x->p) + + return x; + + return x->p = FIND_SET(x->p); + +} + + + +void LINK(NodeT* &x, NodeT* &y) + +{ + + if (x->rank > y->rank) y->p = x; + + else + + { + + x->p = y; + + if (x->rank == y->rank) y->rank++; + + } + +} + + + +void UNION(int x, int y) + +{ + + LINK(FIND_SET(a[x]), FIND_SET(a[y])); + +} + + + +int n, m; + + + +int cmp(const void *a, const void *b) + +{ + + muchie m1 = *(const muchie*)a; + + muchie m2 = *(const muchie*)b; + + return m1.cost - m2.cost; + +} + + + +int main() + +{ + + fin >> n >> m; + + + + for (int i = 0; i < m; ++i) + + fin >> g[i].c1 >> g[i].c2 >> g[i].cost; + + + + for (int i = 1; i <= n; ++i) + + MAKE_SET(i); + + + + qsort(g, m, sizeof *g, cmp); + + + + int total(0); + + + + int ind = 0; + + + + for (int i = 0; i < m; ++i) + + { + + if (FIND_SET(a[g[i].c1]) != FIND_SET(a[g[i].c2])) + + { + + UNION(g[i].c1, g[i].c2); + + total += g[i].cost; + + v[ind].x = g[i].c1; + + v[ind++].y = g[i].c2; + + } + + } + + fout << total << "\n" << n - 1 << "\n"; + + for (int i = 0; i < ind; ++i) + + fout << v[i].x << " " << v[i].y << '\n'; + + return 0; + +} diff --git a/mst/kruskal/e6b8ed00-3d04-4ed1-85c0-922a1a0f7c18.cpp b/mst/kruskal/e6b8ed00-3d04-4ed1-85c0-922a1a0f7c18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5fa8d3ef64d33189c05cb56156926ecb4d7092d --- /dev/null +++ b/mst/kruskal/e6b8ed00-3d04-4ed1-85c0-922a1a0f7c18.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +struct Edge { + int x, y, z; + Edge(int x = 0, int y = 0, int z = 0) : + x(x), y(y), z(z) { } + inline bool operator<(Edge e) const { + return z > e.z; + } +}; + +class Forest { + private: + vector height; + vector father; + + public: + Forest(int n) : + height(n + 1), father(n + 1) { } + + int find(int x) { + int root = x; + while (father[root]) + root = father[root]; + + while (father[x]) { + int aux = father[x]; + father[x] = root; + x = aux; + } + return root; + } + + void unite(int rootX, int rootY) { + if (height[rootX] < height[rootY]) + father[rootX] = rootY; + else { + father[rootY] = rootX; + if (height[rootX] == height[rootY]) + height[rootX]++; + } + } +}; + +int main() { + int n, m; fin >> n >> m; + vector edg(m); + for (int i = 0; i < m; i++) + fin >> edg[i].x >> edg[i].y >> edg[i].z; + + int cost = 0; + vector> sol; + sol.reserve(n - 1); + + Forest forest(n); + make_heap(edg.begin(), edg.end()); + for (int i = 1; i < n; ) { + Edge top = edg.front(); + pop_heap(edg.begin(), edg.end()); + edg.pop_back(); + + int rootX = forest.find(top.x); + int rootY = forest.find(top.y); + if (rootX != rootY) { + forest.unite(rootX, rootY); + sol.emplace_back(top.x, top.y); + cost += top.z; + i++; + } + } + + fout << cost << '\n' << n - 1 << '\n'; + for (auto edg : sol) + fout << edg.first << ' ' << edg.second << '\n'; + + fout.close(); + return 0; +} diff --git a/mst/kruskal/e76bb284-c2e9-4641-921e-b03390a473c1.cpp b/mst/kruskal/e76bb284-c2e9-4641-921e-b03390a473c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e92b4e77c3ccef4543d07558bd795da2bff718a5 --- /dev/null +++ b/mst/kruskal/e76bb284-c2e9-4641-921e-b03390a473c1.cpp @@ -0,0 +1,143 @@ +#include +#include +using namespace std; + +const int MMAX = 400000; + +const int NMAX = 200000; + +struct muchie + +{ + + int x, y, c, viz; + + muchie(int tx = 0, int ty = 0, int tc = 0, int tviz = 0) + + { + + x = tx; + + y = ty; + + c = tc; + + viz = tviz; + + } + +}; + +muchie v[MMAX + 5]; + +int t[NMAX + 5], h[NMAX + 5]; + +bool cmp(muchie a, muchie b) + +{ + + return a.c < b.c; + +} + +int findMD(int x) + +{ + + while(x != t[x]) + + x = t[x]; + + return x; + +} + +void unionMD(int x, int y) + +{ + + if(h[x] > h[y]) + + t[y] = x; + + else if(h[x] < h[y]) + + t[x] = y; + + else + + { + + t[y] = x; + + h[x] ++; + + } + +} + +int main() + +{ + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int n, m, x, y, c, cnt, i; + + scanf("%d%d", &n, &m); + + for(i = 1 ; i <= m ; i ++) + + { + + scanf("%d%d%d", &x, &y, &c); + + v[i] = {x, y, c}; + + } + + for(i = 1 ; i <= n ; i ++) + + t[i] = i, h[i] = 1; + + sort(v + 1, v + m + 1, cmp); + + c = 0; + + for(i = 1, cnt = 0 ; i <= m && cnt < n - 1 ; i ++) + + { + + x = findMD(v[i].x); + + y = findMD(v[i].y); + + if(x != y) + + { + + unionMD(x, y); + + cnt ++; + + c += v[i].c; + + v[i].viz = 1; + + } + + } + + printf("%d\n%d\n", c, n - 1); + + for(i = 1 ; i <= m ; i ++) + + if(v[i].viz) + + printf("%d %d\n", v[i].x, v[i].y); + + return 0; + +} diff --git a/mst/kruskal/e7bfce7c-d35b-4798-af8b-048fb7c46883.cpp b/mst/kruskal/e7bfce7c-d35b-4798-af8b-048fb7c46883.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db561a80182e04091077165f1586981fb8a7c1ca --- /dev/null +++ b/mst/kruskal/e7bfce7c-d35b-4798-af8b-048fb7c46883.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +const int NMAX=200003; + +int n,m,father[NMAX],s[NMAX],costmin; + +int Find(int node){ + + if(father[node]==node) + + return node; + + return father[node]=Find(father[node]); + +} + +void Union(int a,int b){ + + int fa=Find(a),fb=Find(b); + + if(fa==fb) + + return; + + if(s[fa]cost; + + } + +}; + +vectorv,rasp; + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=n;i++) + + father[i]=i,s[i]=1; + + for(int i=1,a,b,cost;i<=m;i++){ + + in>>a>>b>>cost; + + v.push_back({a,b,cost}); + + } + + sort(v.begin(),v.end()); + + for(auto y:v){ + + int a=y.a,b=y.b,cost=y.cost; + + int fa=Find(a),fb=Find(b); + + if(fa==fb) + + continue; + + Union(a,b); + + rasp.push_back({a,b}); + + costmin+=cost; + + } + + out< +#include +#include +using namespace std; + +ifstream be("apm.in"); + +ofstream ki("apm.out"); + + + +const int NMAX=200000; + + + +int findset(int i,vector&parent) + +{ + + if(i==parent[i]) + + return i; + + else{ + + int root=findset(parent[i],parent); + + parent[i]=root; + + return root; + + } + + + +} + + + +void unionset(int x,int y,vector&parent,vector&rank) + +{ + + int rx=findset(x,parent); + + int ry=findset(y,parent); + + if(rank[rx] > >&adj,vector >&t,vector&parent,vector&rank,int &db,int &m,int &k) + +{ + + int xRep,yRep; + + sort(adj.begin(),adj.end()); + + for(int i=0;i>n>>m; + + vector > >adj; + + for(int i=0;i>x>>y>>c; + + adj.push_back({c,{x,y}}); + + } + + vectorparent(n+1); + + vectorrank(n+1,0); + + for(int i=1;i<=n;i++) + + parent[i]=i; + + vector >t(NMAX); + + kruskal(adj,t,parent,rank,db,m,k); + + ki< + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 2e5 + 5; + + + +struct solve + +{ + + int x, y, w; + + bool operator < (const solve & nxt) const + + { + + return w < nxt.w; + + } + +}; + + + +vector < solve > edge; + +vector < pair < int, int > > sol; + + + +int ans = 0; + +int Size[N], parent[N]; + + + +void make_set(int k) + +{ + + Size[k] = 1; + + parent[k] = k; + +} + + + +int find_set(int k) + +{ + + if(parent[k] == k) return k; + + return parent[k] = find_set(parent[k]); + +} + + + +void union_sets(int a, int b) + +{ + + a = find_set(a), b = find_set(b); + + if(a != b) { + + if(Size[b] > Size[a]) swap(a, b); + + Size[a] += Size[b]; + + parent[b] = a; + + } + +} + + + +int main() + +{ + + usain_bolt(); + + + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y, w; + + + + fin >> x >> y >> w; + + edge.push_back({x, y, w}); + + } + + sort(edge.begin(), edge.end()); + + for(int i = 1; i <= n; ++i) make_set(i); + + for(auto v : edge) { + + if(find_set(v.x) != find_set(v.y)) { + + sol.push_back({v.x, v.y}), ans += v.w; + + union_sets(v.x, v.y); + + } + + } + + fout << ans << "\n" << sol.size() << "\n"; + + for(auto v : sol) fout << v.first << " " << v.second << "\n"; + + return 0; + +} diff --git a/mst/kruskal/e953e297-c8ee-43d0-8c57-47a45831e1d3.cpp b/mst/kruskal/e953e297-c8ee-43d0-8c57-47a45831e1d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20dad7984f8d5617a23c0956d652aa57ef6c34d8 --- /dev/null +++ b/mst/kruskal/e953e297-c8ee-43d0-8c57-47a45831e1d3.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +int t[200010], S[200010]; + +int a, b, ra, rb, n, m, i, sol, alese; + + + +int rad(int x) { + + while(t[x] > 0) + + x = t[x]; + + return x; + +} + + + +pair< int, pair > v[400010]; + + + +int main (){ + + fin>>n>>m; + + for (i=1;i<=m;i++) + + fin>>v[i].second.first>>v[i].second.second>>v[i].first; + + sort(v+1, v+m+1); + + + + for (i=1;i<=n;i++) + + t[i] = -1; + + + + for (i=1;i<=m;i++) { + + a = v[i].second.first; + + b = v[i].second.second; + + + + ra = rad(a); + + rb = rad(b); + if (ra != rb) { + + if (-t[ra] > -t[rb]) { + + + + t[ra] += t[rb]; + + t[rb] = ra; + + } else { + + t[rb] += t[ra]; + + t[ra] = rb; + + } + + sol += v[i].first; + + + + + + alese++; + + S[alese] = i; + + if (alese == n-1) + + break; + + } + + } + + fout< +#include +#include +using namespace std; + + + +const int NMAX = 400000; + + + +struct muchie + +{ + + int u, v, c, pus; + +}; + + + +muchie x[NMAX + 5]; + +int t[200005], h[200005]; + + + +int varf(int x) + +{ + + while(t[x] != x) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if(h[x] > h[y]) + + t[y] = x; + + else + + { + + if(h[x] < h[y]) + + t[x] = y; + + else + + { + + t[y] = x; + + ++h[x]; + + } + + } + +} + + + +bool cmp(muchie a, muchie b) + +{ + + return a.c>n>>m; + + for(i = 1; i <= n; ++i) + + t[i] = i, h[i] = 1; + + for(i = 1; i <= m; ++i) + + fin>>x[i].u>>x[i].v>>x[i].c; + + cost = 0; + + sort(x + 1, x + m + 1, cmp); + + int rx, ry, ms; + + for(ms = 0, i = 1; ms < n-1; ++i) + + { + + rx = varf(x[i].u); + + ry = varf(x[i].v); + + if(rx != ry) + + { + + unire(rx, ry); + + x[i].pus = 1; + + cost = cost + x[i].c; + + ++ms; + + } + + } + + fout< + + + +using namespace std; + + + +const int nMax = 4e5 + 7; + + + +int n, m, cost, p[nMax]; + +vector > sol; + + + +struct edge + +{ + + int a, b, d; + +} v[nMax]; + + + +bool comp(edge x, edge y) + +{ + + return x.d < y.d; + +} + + + +int Find(int x) + +{ + + int r = x; + + for (; r != p[r]; r = p[r]); + + while (p[x] != r) + + { + + int cop = p[x]; + + p[x] = r; + + x = cop; + + } + + return r; + +} + + + +void Solve(edge x) + +{ + + int ra = Find(x.a); + + int rb = Find(x.b); + + if (ra != rb) + + { + + cost += x.d; + + p[ra] = rb; + + sol.push_back({x.b, x.a}); + + } + +} + + + +main() + +{ + + freopen("apm.in", "rt", stdin); + + freopen("apm.out", "wt", stdout); + + + + scanf("%d%d", &n, &m); + + + + for (int i = 1; i <= m; ++i) + + scanf("%d%d%d", &v[i].a, &v[i].b, &v[i].d); + + + + sort(v + 1, v + m + 1, comp); + + + + for (int i = 1; i <= n; ++i) + + p[i] = i; + + + + for (int i = 1; sol.size() < (n-1); ++i) + + Solve(v[i]); + + + + printf("%d\n%d\n", cost, n - 1); + + for (auto i : sol) + + printf("%d %d\n", i.first, i.second); + + + + return 0; + +} diff --git a/mst/kruskal/eb5c0368-b70c-496f-bacf-6b91d73de49a.cpp b/mst/kruskal/eb5c0368-b70c-496f-bacf-6b91d73de49a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41fdbb1022962d4704425cc0e49b05655727144f --- /dev/null +++ b/mst/kruskal/eb5c0368-b70c-496f-bacf-6b91d73de49a.cpp @@ -0,0 +1,137 @@ +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int nmax=400005; + +int n,m,k,total,tt[nmax],rg[nmax]; + +pair p[nmax]; + + + +struct edge{ + + int x,y,c; + +}v[nmax]; + + + +bool compare(edge a,edge b){ + + return a.c>n>>m; + +for(int i=1;i<=m;i++){ + + in >>v[i].x>>v[i].y>>v[i].c; + +} + +sort(v+1, v+m+1,compare); + + + +for(int i=1;i<=m;i++){ + + tt[i]=i; + + rg[i]=1; + +} + +rez(); + +out < +#include + + + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int N,M; + +int r[200005], tata[200005], h[200005]; + +int counter = 1, cost = 0; + +vector> muchii; + +vector> APM; + + + +void initializare(int nod) + +{ + + tata[nod] = r[nod]=0; + +} + + + +int reprez(int nod) + +{ + + while(tata[nod]!=0) + + { + + nod = tata[nod]; + + } + + return nod; + +} + +void reuneste(int a, int b) + +{ + + int r1 = reprez(a); + + int r2 = reprez(b); + + if(h[r1]>h[r2]) + + tata[r2]=r1; + + else + + { + + tata[r1]=r2; + + if(h[r1]==h[r2]) + + h[r2]++; + + } + +} + +bool cmp(tuple a, tuple b) + +{ + + return get<2>(a) < get<2>(b); + +} + +int main() + +{ + + in>>N>>M; + + muchii.reserve(M); + + APM.reserve(N); + + int x,y,z; + + for(int i=1; i<=M; i++) + + { + + in>>x>>y>>z; + + muchii.push_back({x,y,z}); + + } + + sort(muchii.begin(),muchii.end(),cmp); + + for(int i=1; i<=N; i++) + + initializare(i); + + + + for(auto &muchie:muchii) + + { + + x = get<0>(muchie); + + y = get<1>(muchie); + + if(reprez(x)!=reprez(y)) + + { + + APM.push_back(muchie); + + cost+= get<2>(muchie); + + reuneste(x,y); + + counter++; + + if(counter==N) + + break; + + } + + } + + out<(muchie)<<' '<(muchie)<<'\n'; + + return 0; + +} diff --git a/mst/kruskal/ed34da64-354e-48ca-b29d-00e1b07221d7.cpp b/mst/kruskal/ed34da64-354e-48ca-b29d-00e1b07221d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2863704525ca62ca0a3663bfd65da7129f8e332d --- /dev/null +++ b/mst/kruskal/ed34da64-354e-48ca-b29d-00e1b07221d7.cpp @@ -0,0 +1,156 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int nMax = 200005; + +const int mMax = 400005; + +int n, m; + +int t[nMax]; + + + +struct triplet + +{ + + int x, y, cost, viz; + +}; + +triplet a[mMax]; + + + +bool cmp(const triplet A, const triplet B) + +{ + + return A.cost < B.cost; + +} + + + + + +void Union(int x, int y) + +{ + + t[y] = x; + +} + + + +int Find(int x) + +{ + + int aux, rad; + + rad = x; + + while (t[rad]) + + { + + rad = t[rad]; + + } + + while (t[x]) + + { + + aux = t[x]; + + t[x] = rad; + + x = aux; + + } + + return rad; + +} + + + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + fin >> a[i].x >> a[i].y >> a[i].cost; + + sort(a + 1, a + m + 1, cmp); + + + + int cost, v, w, nrc = n,costt = 0; + + for (int i = 1; i <= m and nrc > 1; i++) + + { + + v = a[i].x; + + w = a[i].y; + + v = Find(v); + + w = Find(w); + + if (v != w) + + { + + Union(v, w); + + a[i].viz = 1; + + costt += a[i].cost; + + nrc--; + + } + + } + + fout << costt << "\n"; + + fout << n - 1 << "\n"; + + for (int i = 1; i <= m; i++) + + if (a[i].viz == 1) + + fout << a[i].x << " " << a[i].y << "\n"; + + + + + + return 0; + +} diff --git a/mst/kruskal/ed97ced7-7339-4518-bb36-c02f0632ef6e.cpp b/mst/kruskal/ed97ced7-7339-4518-bb36-c02f0632ef6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..916f03821d0991f29a2eb2a3b65746958f21e9cf --- /dev/null +++ b/mst/kruskal/ed97ced7-7339-4518-bb36-c02f0632ef6e.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + + + +pair P[400005]; + + + +int N, M, Total, k, TT[400005], RG[400005]; + + + +struct miaw + +{ + + int x,y,c; + + + +} V[400005]; + + + +bool cmp(miaw a, miaw b) + +{ + + return a.c < b.c; + +} + +int Find(int nod) + +{ + + while(TT[nod] != nod) + + nod = TT[nod]; + + return nod; + +} + +void Unite(int x,int y) + +{ + + if(RG[x] < RG[y]) + + TT[x] = y; + + if(RG[y] < RG[x]) + + TT[y] = x; + + if(RG[x] == RG[y]) + + { + + TT[x]=y; + + RG[y]++; + + } + +} + + + +void Kruskal() + +{ + + for(int i=1; i<=M; i++) + + { + + + + + + if(Find(V[i].x) != Find(V[i].y)) + + { + + Unite(Find(V[i].x), Find(V[i].y)); + + k++; + + P[k].first = V[i].x; + + P[k].second = V[i].y; + + Total += V[i].c; + + } + + } + +} + + + +int main() + +{ + + + + f>>N>>M; + + for(int i=1; i<=M; i++) + + f>> V[i].x >> V[i].y >> V[i].c; + + + + sort(V+1,V+M+1,cmp); + + for(int i=1; i<=M; i++) + + { + + TT[i]=i; + + RG[i]=1; + + } + + + + Kruskal(); + + + + + + g< +using namespace std; + + + + + + +using ull = unsigned long long; +using ll = long long; +using pii = pair; +using pll = pair; + +template +ostream& operator<<(ostream &os, const vector &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } +template +ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } + +const int INF = INT_MAX; +const int MOD = 1e9 + 7; +const double EPS = 1e-9; +const double PI = acos(-1); + +ll t, n; +struct DSU { + + vector parent, rank, size; + DSU(int _n = 0) { + + init(n); + + } + + + + void init(int n) { + + parent.resize(n + 1); + + rank.resize(n + 1); + + size.resize(n + 1); + + for (int i = 1; i <= n; i++) + + make_set(i); + + } + + + + void make_set(int v) { + + parent[v] = v; + + rank[v] = 1; + + } + + + + + + void union_sets(int x, int y) { + + x = find(x); + + y = find(y); + + if (x != y) { + + if (rank[x] < rank[y]) + + swap(x, y); + + parent[y] = x; + + if (rank[x] == rank[y]) + + rank[x]++; + + } + + } + + + + int find(int x) { + + if (parent[x] != x) + + parent[x] = find(parent[x]); + + return parent[x]; + + } + +}; + + +struct Edge { + + int from, to, w; + + + + Edge(int _from, int _to, int _w) : from(_from), to(_to), w(_w) {} + + + + bool operator<(const Edge& other) const { + + return w < other.w; + + } + +}; + + + +struct min_span_tree { + + + + vector adj, mst; + + DSU dsu; + + ll total_w = 0; + + + + min_span_tree(int n) { + + + + dsu.init(n); + + dsu.make_set(n); + + } + + + + void add_edge(int to, int from, int weight) { + + adj.push_back(Edge(from, to, weight)); + + + + + + + + } + + + + void create_mst() { + + sort((adj).begin(),(adj).end()); + + for (auto e: adj) { + + if (dsu.find(e.to) != dsu.find(e.from)) { + + total_w += e.w; + + mst.push_back(e); + + dsu.union_sets(e.to, e.from); + + } + + } + + } + +}; + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("apm.in"); + + ofstream cout("apm.out"); + + + + ll m, from, to, w; + + cin >> n >> m; + + min_span_tree MST(n); + + for (int i = 1; i <= m; i++) { + + cin >> from >> to >> w; + + MST.add_edge(from, to, w); + + } + + + + MST.create_mst(); + + + + cout << MST.total_w << "\n" << n - 1 << "\n"; + + for (auto edg: MST.mst) cout << edg.from << " " << edg.to << "\n"; + + + + return 0; + +} diff --git a/mst/kruskal/efe57e77-ff37-42e3-947b-9ef95f91a6eb.cpp b/mst/kruskal/efe57e77-ff37-42e3-947b-9ef95f91a6eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..734b8676e6166a0db50c955f22b272a5c7f901aa --- /dev/null +++ b/mst/kruskal/efe57e77-ff37-42e3-947b-9ef95f91a6eb.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +typedef pair iPair; + +struct DS +{ + int nodeCnt; + vector father, connected; + + explicit DS(int N) : nodeCnt(N), connected(N + 1, 1), father(N + 1, -1) {} + + int root(int child) + { + while (father[child] != -1) child = father[child]; + + return child; + } + + bool Connected(int a, int b) + { + return root(a) == root(b); + } + + void Link(int st, int dr) + { + int rootSt = root(st); + int rootDr = root(dr); + if (connected[rootSt] > connected[rootDr]) + swap(rootSt, rootDr); + father[rootSt] = rootDr; + connected[rootDr] += connected[rootSt]; + } +}; + + +struct Edge { + int src, dest, cost; + + Edge(int src, int dest, int cost) : src(src), dest(dest), cost(cost) {} + + bool operator<(const Edge &oth) const + { + return cost < oth.cost; + } +}; + +struct Graph { + int nodeCnt, edgeCnt; + DS *ds = nullptr; + vector edges, apmEdges; + + Graph(int V, int E) + { + this->nodeCnt = V; + this->edgeCnt = E; + edges.reserve(edgeCnt); + + for(int i = 0, a, b, c; i < edgeCnt; ++i) + { + fin >> a >> b >> c; + edges.emplace_back(a, b, c); + } + + this->ds = new DS(nodeCnt); + } + + void displayAPM() { + fout << APM() << '\n'; + fout << apmEdges.size() << '\n'; + for (auto e : apmEdges) + { + fout << e.src << " " << e.dest << '\n'; + } + } + + long long APM() + { + sort(edges.begin(), edges.end()); + + long long TotalCostAPM = 0, edgesAdded = 0; + for (auto e : edges) + { + if(edgesAdded == nodeCnt - 1) break; + if (!ds->Connected(e.src, e.dest)) + { + ds->Link(e.src, e.dest); + TotalCostAPM += e.cost; + apmEdges.emplace_back(e); + edgesAdded++; + } + } + + return TotalCostAPM; + } +}; + +int main() +{ + int n, m; + fin >> n >> m; + + Graph graph(n, m); + + graph.displayAPM(); +} diff --git a/mst/kruskal/f0e12885-7ab3-44e9-a94e-883e3d750e98.cpp b/mst/kruskal/f0e12885-7ab3-44e9-a94e-883e3d750e98.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5fa8d3ef64d33189c05cb56156926ecb4d7092d --- /dev/null +++ b/mst/kruskal/f0e12885-7ab3-44e9-a94e-883e3d750e98.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +struct Edge { + int x, y, z; + Edge(int x = 0, int y = 0, int z = 0) : + x(x), y(y), z(z) { } + inline bool operator<(Edge e) const { + return z > e.z; + } +}; + +class Forest { + private: + vector height; + vector father; + + public: + Forest(int n) : + height(n + 1), father(n + 1) { } + + int find(int x) { + int root = x; + while (father[root]) + root = father[root]; + + while (father[x]) { + int aux = father[x]; + father[x] = root; + x = aux; + } + return root; + } + + void unite(int rootX, int rootY) { + if (height[rootX] < height[rootY]) + father[rootX] = rootY; + else { + father[rootY] = rootX; + if (height[rootX] == height[rootY]) + height[rootX]++; + } + } +}; + +int main() { + int n, m; fin >> n >> m; + vector edg(m); + for (int i = 0; i < m; i++) + fin >> edg[i].x >> edg[i].y >> edg[i].z; + + int cost = 0; + vector> sol; + sol.reserve(n - 1); + + Forest forest(n); + make_heap(edg.begin(), edg.end()); + for (int i = 1; i < n; ) { + Edge top = edg.front(); + pop_heap(edg.begin(), edg.end()); + edg.pop_back(); + + int rootX = forest.find(top.x); + int rootY = forest.find(top.y); + if (rootX != rootY) { + forest.unite(rootX, rootY); + sol.emplace_back(top.x, top.y); + cost += top.z; + i++; + } + } + + fout << cost << '\n' << n - 1 << '\n'; + for (auto edg : sol) + fout << edg.first << ' ' << edg.second << '\n'; + + fout.close(); + return 0; +} diff --git a/mst/kruskal/f12c9195-59a6-4a45-b6fd-a0c762ca9019.cpp b/mst/kruskal/f12c9195-59a6-4a45-b6fd-a0c762ca9019.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abda0f36fc9efe05e603138ed5106ccb17b03621 --- /dev/null +++ b/mst/kruskal/f12c9195-59a6-4a45-b6fd-a0c762ca9019.cpp @@ -0,0 +1,192 @@ +#include + + +using namespace std; + + + +int const CAP = 200001; + + + +struct Edge { + + int from; + + int to; + + int cost; + + + + bool operator < (Edge e) const { + + if (cost != e.cost) { + + return cost < e.cost; + + } else if (to != e.to) { + + return to < e.to; + + } + + return from < e.from; + + } + +}; + + + +int n, m; + +vector g; + +int boss[CAP]; + +int sz[CAP]; + + + +int sol = 0; + +vector solEdges; + + + +void setUpNode(int curr) { + + boss[curr] = curr; + + sz[curr] = 1; + +} + + + +int getBoss(int curr) { + + if (boss[curr] != curr) { + + boss[curr] = getBoss(boss[curr]); + + } + + + + return boss[curr]; + +} + + + +void combineComponents(int fromBoss, int toBoss) { + + if (sz[fromBoss] < sz[toBoss]) { + + boss[fromBoss] = toBoss; + + + + sz[toBoss] += sz[fromBoss]; + + } + + + + else { + + boss[toBoss] = fromBoss; + + + + sz[fromBoss] += sz[toBoss]; + + } + +} + + + +ofstream out("apm.out"); + +ifstream in("apm.in"); + + + +int main() { + + in >> n >> m; + + + + for (int i = 0; i < m; i++) { + + int x, y, c; + + in >> x >> y >> c; + + + + g.push_back({x, y, c}); + + } + + + + sort(g.begin(), g.end()); + + + + for (int i = 0; i < n; i++) { + + setUpNode(i); + + } + + + + for (int i = 0; i < m; i++) { + + int fromBoss = getBoss(g[i].from); + + + + int toBoss = getBoss(g[i].to); + + + + if (fromBoss != toBoss) { + + combineComponents(fromBoss, toBoss); + + + + sol += g[i].cost; + + + + solEdges.push_back(g[i]); + + } + + } + + + + out << sol << "\n" << solEdges.size() << "\n"; + + + + for(int i=0; i + + +using namespace std; + + + +struct ura{ + + int nod1,nod2,cost; + +}; + +ura v[400001],ans[400001]; + +bool ord(ura a,ura b){ + + if(a.cost!=b.cost) + + return a.cost>n>>m; + + for(i=1;i<=n;++i) + + sef[i]=i; + + for(i=1;i<=m;++i) + + cin>>v[i].nod1>>v[i].nod2>>v[i].cost; + + sort(v+1,v+m+1,ord); + + nr_muchii=0; + + costmin=0; + + for(i=1;i<=m && nr_muchii +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector t; + + + +int find(int x) + +{ + + if (t[x] == 0) + + { + + return x; + + } + + + + t[x] = find(t[x]); + + return t[x]; + +} + + + +void unite(int x, int y) + +{ + + int rx = find(x); + + int ry = find(y); + + t[rx] = ry; + +} + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + t.resize(n + 1); + + vector>> drumuri(m); + + + + for (int i = 0; i < m; i++) + + { + + int x, y, cost; + + fin >> x >> y >> cost; + + + + drumuri[i] = { cost, { x, y } }; + + } + + + + sort(drumuri.begin(), drumuri.end()); + + + + vector> muchii; + + int CostMin = 0; + + + + for (auto drum : drumuri) + + { + + int cost = drum.first; + + int x = drum.second.first; + + int y = drum.second.second; + + + + if (find(x) != find(y)) + + { + + CostMin += cost; + + muchii.push_back({ x, y }); + + unite(x, y); + + } + + } + + + + fout << CostMin << '\n' << n - 1 << '\n'; + + for (auto muchie : muchii) + + { + + fout << muchie.first << ' ' << muchie.second << '\n'; + + } + + + + return 0; + +} diff --git a/mst/kruskal/f582aeb6-a313-456f-945e-3762740150ef.cpp b/mst/kruskal/f582aeb6-a313-456f-945e-3762740150ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3035350cb24adb2f9338b13c69197578b9bdd4ff --- /dev/null +++ b/mst/kruskal/f582aeb6-a313-456f-945e-3762740150ef.cpp @@ -0,0 +1,153 @@ +#include +#include + + +using namespace std; + + + + + + + +struct edge { + + int from; + + int to; + + int cost; + +}; + + + +struct rasp{ + + int from; + + int to; + +}; + + + +struct edge v[200000 +1]; + +int sef[200000 +1]; + +struct rasp raspuns[200000 +1]; + + + +int find(int x){ + + if(sef[x]==x){ + + return x; + + } + + sef[x]=find(sef[x]); + + return sef[x]; + +} + + + +void unite(int x,int y){ + + sef[find(x)]=find(y); + +} + + + +bool comp(edge a,edge b){ + + return(a.cost +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n, m, i, a, b, c, t[200004]; + +pair > edge[400002]; + +vector > sol; + + + +int rad(int nod){ + + while(t[nod] > 0) + + nod = t[nod]; + + return nod; + +} + + + +int main(){ + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>a>>b>>c; + + edge[i] = {c, {a, b}}; + + } + + sort(edge+1, edge+m+1); + + for(i=1;i<=n;i++) + + t[i] = -1; + + pair muchie = edge[1].second; + + sol.push_back(muchie); + + t[muchie.first]--; + + t[muchie.second] = muchie.first; + + int ind = 2; + + int cost = edge[1].first; + + while(sol.size() < n-1){ + + muchie = edge[ind].second; + + a = muchie.first; + + b = muchie.second; + + int rad1 = rad(a); + + int rad2 = rad(b); + + if(rad1 != rad2){ + + sol.push_back(muchie); + + cost += edge[ind].first; + + if(t[rad1] < t[rad2]){ + + t[rad1] += t[rad2]; + + t[rad2] = rad1; + + } + + else{ + + t[rad2] += t[rad1]; + + t[rad1] = rad2; + + } + + } + + ind++; + + } + + fout< +#include +#include + + +using namespace std; + +struct Edge { + int x, y, cost; +}; + +int n, m, i, j, rad1, rad2, cmin; +int h[200005], rad[200005]; +Edge muchie; +vector mc; +vector> apcm; + +bool cmp(Edge e1, Edge e2) { + return e1.cost < e2.cost; +} + +int root(int x) { + while(rad[x] != 0) + x = rad[x]; + return x; +} + +int main() { + ifstream f("apm.in"); + ofstream g("apm.out"); + + f >> n >> m; + for(i = 0; i < m; i++) { + f >> muchie.x >> muchie.y >> muchie.cost; + mc.push_back(muchie); + } + f.close(); + + sort(mc.begin(), mc.end(), cmp); + for(i = 1; i <= n; i++) { + rad[i] = 0; + h[i] = 0; + } + + j = 0; + cmin = 0; + for(i = 0; i < m; i++) { + rad1 = root(mc[i].x); + rad2 = root(mc[i].y); + if(rad1 != rad2) { + apcm.emplace_back(mc[i].x, mc[i].y); + cmin += mc[i].cost; + if(h[rad1] > h[rad2]) + rad[rad2] = rad1; + else { + rad[rad1] = rad2; + if(h[rad1] == h[rad2]) + h[rad2]++; + } + j++; + if(j == n - 1) + break; + } + } + + g << cmin << '\n' << n - 1 << '\n'; + for(i = 0; i < apcm.size(); i++) + g << apcm[i].first << ' ' << apcm[i].second << '\n'; + g.close(); + + return 0; +} diff --git a/mst/kruskal/f724b6ee-f7c7-4cf4-955f-fff8bd4fbc73.cpp b/mst/kruskal/f724b6ee-f7c7-4cf4-955f-fff8bd4fbc73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b389dd2e51274d362b8c9f9dc0cf9288753a46d2 --- /dev/null +++ b/mst/kruskal/f724b6ee-f7c7-4cf4-955f-fff8bd4fbc73.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int maxn = 2e5+5, maxm = 4e5+5; + + + +struct muchie { + + int x, y, cost; + +}; + +muchie mc[maxm], mcans[maxn]; + + + +int n, m, x, y, cost, ans, k; + + + +vector nod[maxn]; + + + +int cmp(muchie a, muchie b) { + + return a.cost < b.cost; + +} + + + +int tt[maxn]; + + + +int rad(int x) { + + int r, y; + + for(r = x; tt[r] != r; r = tt[r]); + + while(tt[x] != x) { + + y = tt[x]; + + tt[x] = r; + + x = y; + + } + + return r; + +} + + + +void unite(int x, int y) { + + tt[y] = x; + +} + + + +int main() + +{ + + int i; + + f >> n >> m; + + for(i = 1; i <= n; i ++) { + + tt[i] = i; + + } + + for(i = 1; i <= m; i ++) { + + f >> x >> y >> cost; + + nod[x].push_back(y); + + nod[y].push_back(x); + + mc[i] = {x, y, cost}; + + } + + sort(mc + 1, mc + m + 1, cmp); + + + + for(i = 1; i <= m; i ++) { + + x = mc[i].x; + + y = mc[i].y; + + cost = mc[i].cost; + + if((rad(x) != rad(y))) { + + unite(rad(x), rad(y)); + + ans += cost; + + mcans[++k] = {x, y, 0}; + + } + + } + + + + g << ans << '\n'; + + g << k << '\n'; + + for(i = 1; i <= k; i ++) { + + g << mcans[i].x << ' ' << mcans[i].y << '\n'; + + } + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/mst/kruskal/f7bccb23-21bf-49b4-ba30-9f273fe86ba2.cpp b/mst/kruskal/f7bccb23-21bf-49b4-ba30-9f273fe86ba2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b961a72bbd36c5741b4450c25bc3572180f9238 --- /dev/null +++ b/mst/kruskal/f7bccb23-21bf-49b4-ba30-9f273fe86ba2.cpp @@ -0,0 +1,145 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct MUCHIE + +{ + + int x, y, c; + + bool s; + +}; + + + +MUCHIE v[400005]; + +int t[200005], h[200005]; + +int n, m, x, y, c, vfx, vfy, ms, cost, i; + + + +bool cmp(MUCHIE a, MUCHIE b) + +{ + + return a.c < b.c; + +} + + + +int varf(int x) + +{ + + while (x != t[x]) + + x = t[x]; + + return x; + +} + + + +void unire(int x, int y) + +{ + + if (h[x] > h[y]) + + t[y] = x; + + else if (h[y] > h[x]) + + t[x] = y; + + else + + { + + t[y] = x; + + ++h[x]; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (i = 1; i <= n; ++i) + + t[i] = i, h[i] = 1; + + for (i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + v[i].x = x; + + v[i].y = y; + + v[i].c = c; + + } + + sort(v + 1, v + m + 1, cmp); + + for (ms = 0, i = 1; ms < n - 1 && i <= m; ++i) + + { + + vfx = varf(v[i].x); + + vfy = varf(v[i].y); + + if (vfx != vfy) + + { + + unire(vfx, vfy); + + v[i].s = true; + + cost += v[i].c; + + ++ms; + + } + + } + + fout << cost << '\n'<< ms << '\n'; + + for (i = 1; i <= m; ++i) + + if (v[i].s) + + fout << v[i].x << ' ' << v[i].y << '\n'; + + return 0; + +} diff --git a/mst/kruskal/f7dd7b3d-30c3-49b2-8f7d-5e8fe76a1ce7.cpp b/mst/kruskal/f7dd7b3d-30c3-49b2-8f7d-5e8fe76a1ce7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff718e6c2fca90e648b0a3590729aa52656c0d1f --- /dev/null +++ b/mst/kruskal/f7dd7b3d-30c3-49b2-8f7d-5e8fe76a1ce7.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct Op{int x;int y;int c;}; + +vector edge,sol; + +bool mycmp(Op s,Op t) + +{return s.c>n>>m; + +for(int i=0;i>x>>y>>c,edge.push_back({x,y,c}); + +sort(edge.begin(),edge.end(),mycmp); + +for(int i=1;i<=n;++i)link[i]=i,dim[i]=1; + +for(auto t:edge)if(tata(t.x)!=tata(t.y)) + +unite(t.x,t.y),sol.push_back(t),s+=t.c; + +cout< +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie{ + + int x, y; + + int cost; + + + + bool operator<(const muchie &other) const{ + + if(cost==other.cost){ + + if(x == other.x) + + return y>n>>m; + + for(int i=0; i>muchii[i].x>>muchii[i].y>>muchii[i].cost; + + } + +} + +int main() + +{ + + citire(); + + init_t(); + + sort(muchii, muchii+m); + + solve(); + + g< + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +vector>> edges; + +vector> sol; + +int parent[200005],n,m; + +int Find(int i) + +{ + + if (parent[i] != i) + + parent[i] = Find(parent[i]); + + return parent[i]; + +} + +void Union(int x,int y) + +{ + + int a = Find(x); + + int b = Find(y); + + parent[a] = b; + +} + +int main() + +{ + + in>>n>>m; + + for(int i=0;i>a>>b>>c; + + edges.push_back({c,{a,b}}); + + } + + sort(edges.begin(), edges.end()); + + for(int i=1;i<=n;i++) + + parent[i] = i; + + int sum = 0; + + for(auto it:edges) + + { + + int a = Find(it.second.first); + + int b = Find(it.second.second); + + if(a != b) + + { + + sum += it.first; + + sol.push_back({it.second.first,it.second.second}); + + Union(a,b); + + } + + } + + out< + + + + +struct edge { + int first_node; + int second_node; + int cost; +}; + +bool compareEdges(const struct edge& e1, const struct edge& e2) { + return e1.cost < e2.cost; +} + +class Task { + public: + void solve() { + read_input(); + get_result(); + print_output(); + } + + private: + int n, m, cost_apm, parent[200005]; + std::vector edges; + std::vector apm; + + void read_input() { + std::cin >> n >> m; + cost_apm = 0; + for (int i = 1, x, y, c; i <= m; ++i) { + std::cin >> x >> y >> c; + + struct edge new_edge; + new_edge.first_node = x; + new_edge.second_node = y; + new_edge.cost = c; + + edges.push_back(new_edge); + } + } + + int get_set(int node) { + if (parent[node] != node) { + parent[node] = get_set(parent[node]); + } + return parent[node]; + } + + void reunion_set(int x, int y) { + parent[get_set(x)] = get_set(y); + } + + void get_result() { + std::sort(edges.begin(), edges.end(), compareEdges); + for (int i = 1; i <= n; ++i) { + parent[i] = i; + } + + for (auto& edge : edges) { + int x, y, c; + + x = edge.first_node; + y = edge.second_node; + c = edge.cost; + + if (get_set(x) != get_set(y)) { + cost_apm += c; + apm.push_back(edge); + reunion_set(x, y); + } + } + } + + void print_output() { + std::cout << cost_apm << "\n"; + std::cout << apm.size() << "\n"; + for (auto& edge : apm) { + std::cout << edge.first_node << " " << edge.second_node << "\n"; + } + } +}; + + +int main() { + auto cin_buff = std::cin.rdbuf(); + auto cout_buff = std::cout.rdbuf(); + + std::ifstream fin("apm.in"); + std::cin.rdbuf(fin.rdbuf()); + + std::ofstream fout("apm.out"); + std::cout.rdbuf(fout.rdbuf()); + + Task *task = new Task(); + task->solve(); + delete task; + + std::cin.rdbuf(cin_buff); + std::cout.rdbuf(cout_buff); + return 0; +} diff --git a/mst/kruskal/fb9d3116-8cf1-478a-8011-8c244b11f79c.cpp b/mst/kruskal/fb9d3116-8cf1-478a-8011-8c244b11f79c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64f268d7f44dc469b14b1b08eab232c13b3add3a --- /dev/null +++ b/mst/kruskal/fb9d3116-8cf1-478a-8011-8c244b11f79c.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +const int lim=2e5+3; + +struct Op + +{ + + int src; + + int dest; + + int cost; + +}; + +vector > sol; + +vector edge; + +int link[lim]; + +int dim[lim]; + +int tata(int x) + +{ + + int cpy=x,aux; + + while(x!=link[x]) x=link[x]; + + while(cpy!=link[cpy]) aux=cpy,cpy=link[cpy],link[aux]=x; + + return x; + +} + +void unite(int x,int y) + +{ + + x=tata(x); + + y=tata(y); + + if(dim[x]>n>>m; + + for(int i=1;i<=m;++i) + + { + + cin>>x>>y>>c; + + edge.push_back({x,y,c}); + + } + + sort(edge.begin(),edge.end(),mycmp); + + for(int i=1;i<=n;++i) + + { + + link[i]=i; + + dim[i]=1; + + } + + for(auto t:edge) + + if(tata(t.src)!=tata(t.dest)) + + { + + ans+=t.cost; + + sol.push_back({t.src,t.dest}); + + unite(t.src,t.dest); + + } + + cout< +#include +#include +#include + + + +class DisjointSet { +private: + int n; + std::vector p = std::vector(200000 + 1); + std::vector h = std::vector(200000 + 1); +public: + DisjointSet(int nr) { + n = nr; + for(int i = 1; i <= n; ++i) { + p[i] = i; + h[i] = 1; + } + } + + int Find(int x) { + int r, aux; + r = x; + while(p[r] != r) + r = p[r]; + while(p[x] != r) { + aux = p[x]; + p[x] = r; + x = aux; + } + return r; + } + + bool Union(int x, int y) { + int rx, ry; + rx = Find(x); + ry = Find(y); + if(rx != ry) { + if(h[rx] < h[ry]) + p[rx] = ry; + else if(h[rx] > h[ry]) + p[ry] = rx; + else { + p[rx] = ry; + ++h[ry]; + } + return true; + } else return false; + } + + ~DisjointSet() { + p.clear(); + h.clear(); + p.shrink_to_fit(); + h.shrink_to_fit(); + } +}; + +class Edge { +private: + int x; + int y; + int cost; +public: + int getX() { + return x; + } + + int getY() { + return y; + } + + int getCost() { + return cost; + } + + void setX(int X) { + x = X; + } + + void setY(int Y) { + y = Y; + + } + + void setCost(int Cost) { + cost = Cost; + } +}; + +class CompareCost { +public: + bool operator() (Edge& a, Edge& b) { + return a.getCost() < b.getCost(); + } +}; + +class Graph { +private: + int V; + int E; + std::vectorg; +public: + void readGraph(std::string name_fin) { + Edge p; + int x, y, c; + std::ifstream fin(name_fin); + fin >> V >> E; + for(int i = 0; i < E; ++i) { + fin >> x >> y >> c; + p.setX(x); + p.setY(y); + p.setCost(c); + g.push_back(p); + } + } + + void sortEdges() { + sort(g.begin(), g.end(), CompareCost()); + } + + void Kruskal() { + std::ofstream fout("apm.out"); + DisjointSet ds(this->V); + sortEdges(); + int i, nrEdges, apmCost; + std::vector>apm; + i = nrEdges = apmCost = 0; + while(i < this->E && nrEdges < V - 1) { + if(ds.Union(g[i].getX(), g[i].getY())) { + apm.push_back(std::make_pair(g[i].getX(), g[i].getY())); + apmCost += g[i].getCost(); + ++nrEdges; + } + ++i; + } + fout << apmCost << '\n' << nrEdges << '\n'; + for(auto i : apm) + fout << i.first << " " << i.second << "\n"; + fout.close(); + } + + ~Graph() { + g.clear(); + g.shrink_to_fit(); + } +}; + +int main() { + Graph G; + G.readGraph("apm.in"); + G.Kruskal(); + return 0; +} diff --git a/mst/kruskal/fed889a0-4f6e-40bc-8218-f3b9ba3e4e29.cpp b/mst/kruskal/fed889a0-4f6e-40bc-8218-f3b9ba3e4e29.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3edb7cd16239d0eeb28df65405cf32d8e6649f5 --- /dev/null +++ b/mst/kruskal/fed889a0-4f6e-40bc-8218-f3b9ba3e4e29.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n, m, rez; + +int Union[200001], UnSize[200001]; + +tuple muchie[400001]; + +stack > per; + + + +int rad(int nod) + +{ + + while(nod != Union[nod]) + + nod = Union[nod]; + + + + return nod; + +} + + + +int main() + +{ + + in >> n >> m; + + + + for(int i, j, c, k = 1; k <= m; k++) + + { + + in >> i >> j >> c; + + muchie[k] = make_tuple(c, i, j); + + } + + + + sort(muchie + 1, muchie + 1 + m); + + + + for(int i = 1; i <= n; i++) + + { + + Union[i] = i; + + UnSize[i] = 1; + + } + + + + for(int q = n-1, k = 1; q; k++) + + { + + int c, i, j, ri, rj; + + tie(c, i, j) = muchie[k]; + + ri = rad(i); + + rj = rad(j); + + + + if(ri == rj) + + continue; + + + + q--; + + rez += c; + + per.push({i,j}); + + + + if(UnSize[ri] < UnSize[rj]) + + swap(ri, rj); + + + + UnSize[ri] += UnSize[rj]; + + Union[rj] = ri; + + } + + + + out << rez << '\n'; + + out << n-1 << '\n'; + + + + while(!per.empty()) + + { + + out << per.top().first << ' ' << per.top().second << '\n'; + + per.pop(); + + } + + + + return 0; + +} diff --git a/mst/kruskal/ff0d7086-b101-4285-93e2-e218387d22c5.cpp b/mst/kruskal/ff0d7086-b101-4285-93e2-e218387d22c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28482c0b2d9902b125dbe4e3871c443506a4b24b --- /dev/null +++ b/mst/kruskal/ff0d7086-b101-4285-93e2-e218387d22c5.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct ura + +{ + + int x,y,c; + +}; + +ura v[400001]; + +int sef[200001],sol[400001]; + +bool cmp (ura x,ura y) + +{ + + if (x.cy.c) + + return false; + + else + + if (x.xy.x) + + return false; + + else + + if (x.y>n>>m; + + for (i=1;i<=n;i++) + + sef[i]=i; + + for (i=1;i<=m;i++) + + fin>>v[i].x>>v[i].y>>v[i].c; + + sort(v+1,v+m+1,cmp); + + for (i=1;i<=m;i++) + + { + + sef[v[i].x]=sefsuprem(v[i].x); + + sef[v[i].y]=sefsuprem(v[i].y); + + if (sef[v[i].x]!=sef[v[i].y]) + + { + + nr++; + + sum+=v[i].c; + + sol[nr]=i; + + unire(v[i].x,v[i].y); + + } + + } + + fout< +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +struct idk + +{ + + int x,y,c; + +}; + +idk v[400004]; + +int v1[200004],a[200004],n,m,m1,s; + +bool cmp(idk x, idk y) + +{ + + return x.c0) + + x=a[x]; + + return x; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>v[i].x>>v[i].y>>v[i].c; + + if(i<=n) + + a[i]=-1; + + } + + sort(v+1,v+m+1,cmp); + + for(int i=1;i<=m;i++) + + { + + int x1=arad(v[i].x); + + int y1=arad(v[i].y); + + if(x1==y1) + + continue; + + v1[++m1]=i; + + s+=v[i].c; + + if(a[x1]>a[y1]) + + { + + a[y1]+=a[x1]; + + a[x1]=y1; + + } + + else + + { + + a[x1]+=a[y1]; + + a[y1]=x1; + + } + + } + + fout< + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + +int a[400010],b[400010],c[400010],grupa[400010],ind[400010]; +int ans; +vector ANS; +int Grupa(int i) +{ + if(grupa[i]==i) return i; + grupa[i]=Grupa(grupa[i]); + return grupa[i]; +} + +void unite(int i,int j) +{ + grupa[Grupa(i)]=Grupa(j); +} + + +bool ok(int a,int b) +{ + return (c[a]>n>>m; + for(int i=1;i<=m;i++) + { + in>>a[i]>>b[i]>>c[i]; + ind[i]=i; + } + for(int i=1;i<=n;i++) + grupa[i]=i; + sort(ind+1,ind+m+1,ok); + for(int i=1;i<=m;i++) + { + if(Grupa(a[ind[i]])!=Grupa(b[ind[i]])) + { + ans+=c[ind[i]]; + unite(a[ind[i]],b[ind[i]]); + ANS.push_back(ind[i]); + } + } + out< +#include + +using namespace std; + +struct Edge { + int x, y, cost; +}; + +int n, m, i, j, k, cmin; +bool viz[200005]; +Edge mc, aux, heap[200005]; +vector> arbore, v[200005]; + +void pushup(int p) { + int val; + + val = heap[p].cost; + while(p > 1 && val < heap[p / 2].cost) { + swap(heap[p], heap[p / 2]); + p /= 2; + } +} + +void pushdown(int p) { + int fiu; + + fiu = 1; + while(fiu) { + fiu = 0; + if(p * 2 <= m) { + fiu = p * 2; + if(p * 2 + 1 <= m && heap[p * 2 + 1].cost < heap[p * 2].cost) + fiu = p * 2 + 1; + if(heap[p].cost < heap[fiu].cost) + fiu = 0; + } + if(fiu) { + swap(heap[p], heap[fiu]); + p = fiu; + } + } +} + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + + f >> n >> m; + while(m) { + f >> i >> j >> k; + v[i].emplace_back(j, k); + v[j].emplace_back(i, k); + m--; + } + f.close(); + + for(i = 1; i <= n; i++) + viz[i] = false; + viz[1] = true; + m = 0; + for(i = 0; i < v[1].size(); i++) { + mc.x = 1; + mc.y = v[1][i].first; + mc.cost = v[1][i].second; + heap[++m] = mc; + pushup(m); + } + cmin = 0; + for(k = 1; k < n; k++) { + mc = heap[1]; + cmin += mc.cost; + heap[1] = heap[m--]; + pushdown(1); + arbore.emplace_back(mc.x, mc.y); + viz[mc.y] = true; + for(i = 0; i < v[mc.y].size(); i++) + if(!viz[v[mc.y][i].first]) { + aux.x = mc.y; + aux.y = v[mc.y][i].first; + aux.cost = v[mc.y][i].second; + heap[++m] = aux; + pushup(m); + } + while(viz[heap[1].x] && viz[heap[1].y]) { + heap[1] = heap[m--]; + pushdown(1); + } + } + + g << cmin << '\n' << n - 1 << '\n'; + for(i = 0; i < arbore.size(); i++) + g << arbore[i].first << ' ' << arbore[i].second << '\n'; + g.close(); + + return 0; +} diff --git a/mst/prim/009058da-e861-4b36-a643-3edf70bfbe3b.cpp b/mst/prim/009058da-e861-4b36-a643-3edf70bfbe3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5513ca2004e8b7db77a1e66e91c0acc37c52282 --- /dev/null +++ b/mst/prim/009058da-e861-4b36-a643-3edf70bfbe3b.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int INF = 1e9; + + + +vector dist, t; + + + +vector vis; + + + +vector>> adj; + +vector> ans; + +priority_queue > pq; + + + +int main() + +{ + + int n, m, costTotal = 0; + + in >> n >> m; + + + + dist.resize(n + 1, INF); + + t.resize(n + 1, 0); + + vis.resize(n + 1, false); + + adj.resize(n + 1); + + + + for(int i = 0; i < m; ++i) + + { + + int x, y, cost; + + in >> x >> y >> cost; + + adj[x].push_back({y, cost}); + + adj[y].push_back({x, cost}); + + } + + + + dist[1] = 0; + + + + pq.push({0, 1}); + + + + while(!pq.empty()) + + { + + int node = pq.top().second; + + pq.pop(); + + + + if(vis[node] == false) + + { + + vis[node] = true; + + costTotal += dist[node]; + + if(t[node]) + + ans.push_back({node, t[node]}); + + + + for(auto it : adj[node]) + + { + + if(it.second < dist[it.first]) + + { + + dist[it.first] = it.second; + + t[it.first] = node; + + pq.push({-it.second, it.first}); + + } + + } + + } + + } + + + + out << costTotal << '\n' << n - 1 << '\n'; + + for(auto it : ans) + + { + + out << it.first << " " << it.second << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/00eff57c-6906-49b7-8b09-46115a0cf2d4.cpp b/mst/prim/00eff57c-6906-49b7-8b09-46115a0cf2d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cb0ae01f9e2f764833e7c3baf0dd8292346ce2d --- /dev/null +++ b/mst/prim/00eff57c-6906-49b7-8b09-46115a0cf2d4.cpp @@ -0,0 +1,406 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int INF = 2100000000; + + + +int n, m; + +vector > * lv; + + + +class Edge{ + + + +public: + + + + int first; + + int second; + + + + int price; + + + + Edge(int f, int s, int p): first(f), second(s), price(p) {}; + +}; + + + +int min_price; + +vector selected; + +int * pair_node; + + + +int * d; + +bool * v; + + + +int * q_pos; + +int * q; + +int q_dim; + + + +int pop(){ + + + + swap(q_pos[q[1]], q_pos[q[q_dim]]); + + swap(q[1], q[q_dim]); + + + + q_dim -= 1; + + + + bool ok = 0; + + + + int pos = 1; + + while(!ok) + + + + if(pos * 2 > q_dim && pos * 2 + 1 > q_dim) + + + + ok = true; + + + + else if(pos * 2 > q_dim){ + + + + if(d[q[pos * 2 + 1]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + else if(pos * 2 + 1 > q_dim){ + + + + if(d[q[pos * 2]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + } + + else{ + + + + if(d[q[pos]] < d[q[pos * 2]] && d[q[pos]] < d[q[pos * 2 + 1]]) + + + + ok = true; + + + + else if(d[q[pos * 2]] < d[q[pos * 2 + 1]]){ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + + + return q[q_dim + 1]; + +} + + + +void update(int node){ + + + + int pos = q_pos[node]; + + + + bool ok = 0; + + + + while(!ok) + + + + if(pos / 2 == 0 || d[q[pos]] > d[q[pos / 2]]) + + + + ok = true; + + else{ + + + + swap(q_pos[q[pos]], q_pos[q[pos / 2]]); + + swap(q[pos], q[pos / 2]); + + + + pos /= 2; + + } + +} + + + +void prim(){ + + + + while(q_dim){ + + + + int current_node = pop(); + + v[current_node] = 1; + + + + if(current_node != 1 && d[current_node] == INF) + + return; + + + + + + if(current_node != 1){ + + + + min_price += d[current_node]; + + selected.push_back(Edge(current_node, pair_node[current_node], d[current_node])); + + } + + + + for(int i = 0; i < lv[current_node].size(); i++){ + + + + int next_node = lv[current_node][i].first; + + int new_price = lv[current_node][i].second; + + + + if(!v[next_node] && d[next_node] > new_price){ + + + + d[next_node] = new_price; + + pair_node[next_node] = current_node; + + + + update(next_node); + + } + + } + + } + +} + + + +int main(){ + + + + f >> n >> m; + + + + lv = new vector > [n + 1]; + + + + d = new int [n + 1]; + + v = new bool [n + 1]; + + + + pair_node = new int [n + 1]; + + + + q = new int [n + 1]; + + q_pos = new int [n + 1]; + + q_dim = n; + + + + for(int i = 1 ; i <= n; i++){ + + + + d[i] = INF; + + v[i] = 0; + + + + q[i] = i; + + q_pos[i] = i; + + } + + + + int x, y, c; + + for(int i = 0; i < m ; i++){ + + + + f >> x >> y >> c; + + + + lv[x].push_back(make_pair(y, c)); + + lv[y].push_back(make_pair(x, c)); + + } + + + + prim(); + + + + g << min_price << '\n'; + + + + g << selected.size() << '\n'; + + for(int i = 0; i < selected.size(); i++) + + + + g << selected[i].first << " " << selected[i].second << '\n'; + + + + + + return 0; + +} diff --git a/mst/prim/0154aa10-ead5-4e08-bc24-e14c40bd8337.cpp b/mst/prim/0154aa10-ead5-4e08-bc24-e14c40bd8337.cpp new file mode 100644 index 0000000000000000000000000000000000000000..023c6d8aa4c88b29b263a914cd63bdf9d0d2fe2b --- /dev/null +++ b/mst/prim/0154aa10-ead5-4e08-bc24-e14c40bd8337.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; +struct muchii { + int x,y,cost; + bool operator < (const muchii &a) const { + return cost>a.cost; + } +}var,var1; +int n,m,viz[200003],kontor,suma;vector > graf[200003];priority_queue chestie;pair sol[200003]; +void prim () { + while(!chestie.empty()) { + var=chestie.top(); + if(viz[var.x]==1) { + chestie.pop(); + continue; + } + viz[var.x]=1;chestie.pop(); + if(var.x!=var.y) { + sol[++kontor]={var.x,var.y}; + suma+=var.cost; + } + for(int i=0;i<(int)graf[var.x].size();++i) + if(viz[graf[var.x][i].first]==0) { + var1.x=graf[var.x][i].first,var1.y=var.x,var1.cost=graf[var.x][i].second,chestie.push(var1); + } + } +} +int main () { + int nr,nr1,nr2; + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + scanf("%d%d", &n, &m); + for(int i=1;i<=m;++i) { + scanf("%d%d%d", &nr, &nr1, &nr2); + graf[nr].push_back({nr1,nr2});graf[nr1].push_back({nr,nr2}); + } + var.x=1;var.y=1;var.cost=0; + chestie.push(var);prim(); + printf("%d\n%d\n", suma, kontor); + for(int i=1;i<=kontor;++i) + printf("%d %d\n", sol[i].first, sol[i].second); + return 0; +} diff --git a/mst/prim/01da26b2-9cd7-43c5-b604-d3a3c7f0c45a.cpp b/mst/prim/01da26b2-9cd7-43c5-b604-d3a3c7f0c45a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..380f48d24aec8cbaf5ce04366e52d50dcb143346 --- /dev/null +++ b/mst/prim/01da26b2-9cd7-43c5-b604-d3a3c7f0c45a.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + + + +const int NMAX = 2e5 + 5; + + + +int n , m , u , v , c , node , nxt , f[NMAX] , ans_cost; + +vector > g[NMAX] , ans; + +priority_queue > , vector > > , greater > > > Q; + +pair< int , pair > p; + +int main() + +{ + + ifstream cin("apm.in"); + + ofstream cout("apm.out"); + + + + cin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + + + cin >> u >> v >> c; + + g[u].push_back({v , c}); + + g[v].push_back({u , c}); + + + + } + + + + Q.push({0 , {0 , 1} }); + + while(!Q.empty()) + + { + + + + p = Q.top(); + + Q.pop(); + + + + node = p.second.second; + + + + if(f[node])continue; + + ans.push_back({p.second.first , node}); + + f[node] = 1; + + + + ans_cost += p.first; + + for(int i = 0; i < g[node].size(); i++) + + Q.push({g[node][i].second , {node , g[node][i].first} }); + + } + + + + cout << ans_cost << "\n"; + + cout << ans.size() - 1 << "\n"; + + for(int i = 1; i < ans.size(); i++) + + cout << ans[i].first << " " << ans[i].second << "\n"; + + return 0; + +} diff --git a/mst/prim/031446be-bf3c-4bd4-b1f9-bebdc3954a00.cpp b/mst/prim/031446be-bf3c-4bd4-b1f9-bebdc3954a00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc3165e369dc4484e5e51ab2bd56779b52aae09a --- /dev/null +++ b/mst/prim/031446be-bf3c-4bd4-b1f9-bebdc3954a00.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue> pq; + +void add(int); + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + add(1); + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + tie(c,x,y)=pq.top(); + + pq.pop(); + + if(used[y]) + + continue; + + cost-=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + add(y); + + } + + g< + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int nmax = 2e5 + 1; + +int a[nmax]; + +struct Graf + +{ + + int x, y, cost; + +}; + +Graf v[nmax], rasp[nmax]; + +bool compare(Graf a, Graf b) + +{ + + return a.cost < b.cost; + +} + +int uwu(int i) + +{ + + if(a[i] == i) + + return i; + + return a[i] = uwu(a[i]); + +} + +int i; + +int main() + +{ + + int n, m, cnt = 0, k = 0; + + fin>> n >> m; + + for( i = 1; i <= m; ++i)fin>> v[i].x >> v[i].y >> v[i].cost; + + for( i = 1; i <= n; ++i)a[i]=i; + + sort(v + 1, v + m + 1, compare); + + for( i = 1; i <= m; ++i) + + { + + if(uwu(v[i].x) != uwu(v[i].y)) + + { + + a[uwu(v[i].y)] = v[i].x; + + cnt += v[i].cost; + + rasp[++k].x = v[i].x; + + rasp[k].y = v[i].y; + + } + + } + + fout< +#include +#include +#include +using namespace std; +ifstream f("apm.in"); +ofstream g("apm.out"); +priority_queue >, vector > >, greater > > >h; +vector >v[200005]; +int n,n2,x,y,c,viz[200005]; +struct solutie +{ + int x,y; +}sol[200005]; +struct muchie +{ + int x,y,c; +}m[400005]; +int prim(int nod) +{ + int i,cost=0,cc=0,j,nodc; + viz[nod] = 1; + for(i=1;i<=n-1;i++) + { + for(j=0;j>n>>n2; + for(i=1;i<=n2;i++) + { + f>>x>>y>>c; + v[x].push_back({c,y}); + v[y].push_back({c,x}); + } + g< + + + + +using namespace std; + + + +int main() + +{ + + int n , m, cost = 0; + + ifstream fin("apm.in"); + + fin >> n >> m; + + priority_queue, vector>, greater>> q; + + vector>> adj(n + 1); + + vector dist(n + 1, 1e9); + + vector parent(n + 1, 0); + + vector used(n + 1, false); + + for (int i = 1, x, y, c; i <= m; ++i) { + + fin >> x >> y >> c; + + adj[x].push_back({c, y}); + + adj[y].push_back({c, x}); + + } + + fin.close(); + + + + dist[1] = 0; + + q.push({dist[1], 1}); + + + + for (int i = 1; i <= n; ++i) { + + int node; + + do { + + node = q.top().second; + + q.pop(); + + } while (used[node]); + + + + cost += dist[node]; + + used[node] = true; + + + + for (auto &neigh : adj[node]) { + + if (!used[neigh.second] && neigh.first < dist[neigh.second]) { + + dist[neigh.second] = neigh.first; + + parent[neigh.second] = node; + + q.push({neigh.first, neigh.second}); + + } + + } + + } + + + + ofstream fout("apm.out"); + + fout << cost << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; ++i) { + + fout << i << ' ' << parent[i] << '\n'; + + } + + fout.close(); + + return 0; + +} diff --git a/mst/prim/056b18fd-4036-4034-a773-6f6878641200.cpp b/mst/prim/056b18fd-4036-4034-a773-6f6878641200.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df62ab39a7a4c7f6f3570a10bc0b597172be69d8 --- /dev/null +++ b/mst/prim/056b18fd-4036-4034-a773-6f6878641200.cpp @@ -0,0 +1,173 @@ +#include + + + + +using namespace std; + + + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +void read(); + +int n, m, t[200001], ct,viz[200001]; + +struct pereche + +{ + + int y,c; + +}; + + + +struct muchie + +{ + + int x, y, c; + +}; + + + +struct cmp + +{ + + bool operator()(muchie u, muchie v) + + { + + return u.c > v.c; + + } + +}; + + + + + +priority_queue, cmp> q; + + + +vector G[200001]; + + + +void citeste() + +{ + + int x,y,i,cost; + + fin >> n >> m; + + for(i=1; i<=m; i++) + + { + + pereche p; + + fin >> x >> y >> cost; + + p={y,cost}; + + G[x].push_back(p); + + p={x,cost}; + + G[y].push_back(p); + + } + +} + + + +int main() + +{ + + int i,nr; + + muchie e; + + pereche p; + + + + citeste(); + + for(i=0;i + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + + + +typedef pair pii; + +vector adj[200001]; + +bitset<200001> inAPM; + +int n, m; + +void Prim() + +{ + + int x; + + priority_queue, greater > q; + + vector cost(n + 1, 0x3f3f3f3f); + + vector t(n + 1, 0); + + q.push({ 0,1 }); + + cost[1] = 0; + + t[1] = 0; + + while (!q.empty()) + + { + + x = q.top().second; + + q.pop(); + + inAPM[x] = true; + + for (auto i : adj[x]) + + { + + if (!inAPM[i.first] && cost[i.first] > i.second) + + { + + cost[i.first] = i.second; + + q.push({ cost[i.first], i.first }); + + t[i.first] = x; + + } + + } + + } + + int ansval = 0; + + for (int i = 1; i <= n; i++) + + ansval += cost[i]; + + fout << ansval<< "\n"; + + fout << n - 1 << "\n"; + + for (unsigned int i = 2; i < t.size(); i++) + + fout << t[i] << " " << i << "\n"; + +} + +int main() + +{ + + int x, y, cost; + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> cost; + + adj[x].push_back({ y,cost }); + + adj[y].push_back({ x, cost }); + + } + + Prim(); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/prim/0671146d-8223-4b17-b35f-cb6f44ca4b54.cpp b/mst/prim/0671146d-8223-4b17-b35f-cb6f44ca4b54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9472da5de98f5485f11738307cf0ae27543b7513 --- /dev/null +++ b/mst/prim/0671146d-8223-4b17-b35f-cb6f44ca4b54.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int NMAX = 2e5 + 5; + +const int INF = 1e9; + +int dist[NMAX], from[NMAX], ans; + +bool vis[NMAX]; + +priority_queue, vector>, greater>> pq; + +vector> graf[NMAX]; + + + +int main() + +{ + + int n, m; + + f >> n >> m; + + for(int i = 1; i <= m; i++) { + + int x, y, c; + + f >> x >> y >> c; + + graf[x].push_back(make_pair(y, c)); + + graf[y].push_back(make_pair(x, c)); + + } + + for(int i = 1; i <= n; i++) { + + dist[i] = INF; + + } + + dist[1] = 0; + + pq.push(make_pair(dist[1], 1)); + + while(!pq.empty()) { + + int nod = pq.top().second; + + pq.pop(); + + if(vis[nod]) { + + continue; + + } + + vis[nod] = 1; + + ans += dist[nod]; + + for(int i = 0; i < (int)graf[nod].size(); i++) { + + int next = graf[nod][i].first; + + int cost = graf[nod][i].second; + + if(!vis[next] && dist[next] > cost) { + + dist[next] = cost; + + from[next] = nod; + + pq.push(make_pair(dist[next], next)); + + } + + } + + } + + g << ans << '\n'; + + g << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) { + + g << i << ' ' << from[i] << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/06b554af-0c99-4612-93cb-d317192a1339.cpp b/mst/prim/06b554af-0c99-4612-93cb-d317192a1339.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca28774dfd7caeca197b0199992c348c512483bc --- /dev/null +++ b/mst/prim/06b554af-0c99-4612-93cb-d317192a1339.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int curr; + + int dest; + + int cost; + + bool operator>(const muchie other) const + + { + + if(other.cost == cost) + + return dest > other.dest; + + return cost > other.cost; + + } + +}; + +vector graf[200005]; + +bool viz[200005]; + +int n, m; + +vector > sol; + +priority_queue, greater > Q; + +int cost; + + + +void read() + +{ + + f>>n>>m; + + int c, x, y; + + for(int i = 0; i>x>>y>>c; + + graf[x].push_back({x, y, c}); + + graf[y].push_back({y, x, c}); + + } + +} + + + +void addNode(int node) + +{ + + viz[node] = true; + + for(auto p : graf[node]) + + { + + if(!viz[p.dest]) + + Q.push(p); + + } + +} + + + +void solve() + +{ + + addNode(1); + + while(sol.size() < n-1) + + { + + muchie mu = Q.top(); + + Q.pop(); + + if(viz[mu.dest]) + + continue; + + sol.push_back({mu.curr, mu.dest}); + + addNode(mu.dest); + + cost += mu.cost; + + } + +} + + + +void afisare() + +{ + + g< +#include + + +using namespace std; + +struct Edge { + int x, y, cost; +}; + +int n, m, i, j, k, cmin; +bool viz[200005]; +Edge mc, aux, heap[200005]; +vector> arbore, v[200005]; + +void pushup(int p) { + int val; + + val = heap[p].cost; + while(p > 1 && val < heap[p / 2].cost) { + swap(heap[p], heap[p / 2]); + p /= 2; + } +} + +void pushdown(int p) { + int fiu; + + fiu = 1; + while(fiu) { + fiu = 0; + if(p * 2 <= m) { + fiu = p * 2; + if(p * 2 + 1 <= m && heap[p * 2 + 1].cost < heap[p * 2].cost) + fiu = p * 2 + 1; + if(heap[p].cost < heap[fiu].cost) + fiu = 0; + } + if(fiu) { + swap(heap[p], heap[fiu]); + p = fiu; + } + } +} + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + + f >> n >> m; + while(m) { + f >> i >> j >> k; + v[i].emplace_back(j, k); + v[j].emplace_back(i, k); + m--; + } + f.close(); + + for(i = 1; i <= n; i++) + viz[i] = false; + viz[1] = true; + m = 0; + for(i = 0; i < v[1].size(); i++) { + mc.x = 1; + mc.y = v[1][i].first; + mc.cost = v[1][i].second; + heap[++m] = mc; + pushup(m); + } + cmin = 0; + for(k = 1; k < n; k++) { + mc = heap[1]; + cmin += mc.cost; + heap[1] = heap[m--]; + pushdown(1); + arbore.emplace_back(mc.x, mc.y); + viz[mc.y] = true; + for(i = 0; i < v[mc.y].size(); i++) + if(!viz[v[mc.y][i].first]) { + aux.x = mc.y; + aux.y = v[mc.y][i].first; + aux.cost = v[mc.y][i].second; + heap[++m] = aux; + pushup(m); + } + while(viz[heap[1].x] && viz[heap[1].y]) { + heap[1] = heap[m--]; + pushdown(1); + } + } + + g << cmin << '\n' << n - 1 << '\n'; + for(i = 0; i < arbore.size(); i++) + g << arbore[i].first << ' ' << arbore[i].second << '\n'; + g.close(); + + return 0; +} diff --git a/mst/prim/09d045cd-b863-431c-a5fa-885397878842.cpp b/mst/prim/09d045cd-b863-431c-a5fa-885397878842.cpp new file mode 100644 index 0000000000000000000000000000000000000000..201c747abbd5a331288ab10c8d8a214d77871f47 --- /dev/null +++ b/mst/prim/09d045cd-b863-431c-a5fa-885397878842.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + +int n, m, s; + +int d[200005], t[200005], viz[200005]; + +vector>graf[200005]; + + + +void read() + +{ + + f>>n>>m; + + for (int i=0;i>x>>y>>c; + + graf[x].push_back({y,c}); + + graf[y].push_back({x,c}); + + } + + for (int i=0;i<=n;++i) + + { + + d[i]=999999999; + + t[i]=0; + + viz[i]=0; + + } + + + +} + + + +void output() + +{ + + int cost = 0; + + for(int i=1; i<=n; i++) + + cost += d[i]; + + + + g<>Q; + + Q.push({-d[s],s}); + + while (!Q.empty()) + + { + + int nod = Q.top().second; + + viz[nod] = 1; + + Q.pop(); + + for (pair i : graf[nod]) + + { + + int y = i.first; + + int j = i.second; + + if (viz[y] == 0 && d[y]>j) + + { + + t[y] = nod; + + d[y] = j; + + Q.push({-d[y],y}); + + } + + } + + + + } + + + + output(); + + + +} diff --git a/mst/prim/09fa2a72-c059-49f4-a274-8f7e316743b0.cpp b/mst/prim/09fa2a72-c059-49f4-a274-8f7e316743b0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d34c3fd4be04b23f83a93692ec545b5d9a04e5f6 --- /dev/null +++ b/mst/prim/09fa2a72-c059-49f4-a274-8f7e316743b0.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + fin >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + fin >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0; + + for(int i = 1; i <= n; i ++) + + { + + + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(!q.empty()) + + { + + + + pair least = q.top(); + + int node = least.second; + + + + q.pop(); + + + + if(viz[node] == 0) + + continue; + + viz[node] = 0; + + cost += least.first; + + + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + fout< +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + + + + void kruskal(std::vector& toate) + + { + + heap inventar; + + for (auto muchie: toate) + + inventar.push(muchie); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(muchie.a), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + } + + } + + } + +} + + + + + + + +std::vector muchii; + +int main() + +{ + + int n, m; + + in >> n >> m; + + muchii.resize(m); + + for (auto& muchie: muchii) + + in >> muchie; + + dj::reset(n); + + apm::kruskal(muchii); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/0c762cff-a674-4123-abfa-4638c4247a61.cpp b/mst/prim/0c762cff-a674-4123-abfa-4638c4247a61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7eff59edbd6b1488f1f078aa8b587cbd471b11e1 --- /dev/null +++ b/mst/prim/0c762cff-a674-4123-abfa-4638c4247a61.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include +using namespace std; +int vizitat[200001], n, m, d[200001], tata[200001], cost; +vector> adj[200001]; +set sol; +priority_queue>,greater>> q; +ifstream f("apm.in"); +ofstream g("apm.out"); + +void initializare() +{ + for(int i = 1; i <= n; i++) + { + d[i] = INT_MAX; + tata[i] = 0; + vizitat[i] = 1; + } +} + +int main() { + f >> n >> m; + for(int i = 0; i < m; i++) + { + int x, y, z; + f >> x >> y >> z; + adj[x].emplace_back(y, z); + adj[y].emplace_back(x, z); + } + initializare(); + d[1] = 0; + for(int i = 1; i <= n; i++) + q.push(make_pair(d[i], i)); + while(!q.empty() && q.top().first != INT_MAX) + { + pair m = q.top(); + int u = m.second; + vizitat[u] = 0; + q.pop(); + if (u != 1 && sol.find(u) == sol.end()) + { + cost += d[u]; + sol.insert(u); + } + for(auto x : adj[u]) + { + if(vizitat[x.first] && x.second < d[x.first]) + { + d[x.first] = x.second; + tata[x.first] = u; + q.push(make_pair(d[x.first], x.first)); + } + } + } + g << cost << '\n' << n - 1 << '\n'; + for (auto x : sol) + g << x << " " << tata[x] << '\n'; + return 0; +} diff --git a/mst/prim/0de9159f-e80f-4f91-8b22-2de2237efd50.cpp b/mst/prim/0de9159f-e80f-4f91-8b22-2de2237efd50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6014e952987ca3f6e94a4648700adb981c86c3d3 --- /dev/null +++ b/mst/prim/0de9159f-e80f-4f91-8b22-2de2237efd50.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +using namespace std; + + + + +int n, m, h[400004], s, tata[200003], sol[200003]; + +typedef struct muchii{int x, y, z;} muc; +muc a[400004]; + +void citire(); + +bool cmp ( int b, int c){ + return ( a[b].z < a[c].z); +} + +int main(){ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + citire(); + + int cn, i, j, k, u = 0, r, w; + char rezidual[100]; + + for (cn = 1; cn <= m; cn++){ + r = h[cn]; + for (i = a[r].x; tata[i] != 0; i = tata[i]); + for (j = a[r].y; tata[j] != 0; j = tata[j]); + + if (i != j){ + s += a[r].z; + sol[++u] = r; + tata[i] = j; + if ( tata[a[r].x] > 0) + for (k = a[r].x; tata[k] != 0; k = w){ + w = tata[k]; + tata[k] = j; + } + + if (tata[a[r].y] > 0) + for (k = a[r].y; tata[k] != 0; k = w){ + w = tata[k]; + tata[k] = j; + } + + } + } + printf("%d\n%d\n", s, u); + for (i = 1; i <= u; i++) + printf("%d %d\n", a[sol[i]].x, a[sol[i]].y); + + return 0; +} + +void citire(){ + int i; + scanf("%d %d", &n, &m); + for (i = 1; i <= m; i++){ + scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z); + h[i] = i; + } + sort(h+1, h+m+1, cmp ); +} diff --git a/mst/prim/0e32d59c-6e75-4c09-bce6-3dce0dec0b84.cpp b/mst/prim/0e32d59c-6e75-4c09-bce6-3dce0dec0b84.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f987b8e7cc8dbdc1d16e47b619f59d4656e7cd92 --- /dev/null +++ b/mst/prim/0e32d59c-6e75-4c09-bce6-3dce0dec0b84.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +using namespace std; +int N,M; +vector< pair > V[200002]; +auto compare = [](pair a, pair b) { return a.second > b.second; }; + +priority_queue < pair, vector >, decltype(compare) > pq(compare); +int p[200002], d[200002]; +bool visited[200002]; +vector > ANS; +priority_queue pq2; +int cost = 0; +int main() +{ + ifstream f1("apm.in"); + ofstream f2("apm.out"); + f1>>N>>M; + int x,y,z; + for (int i=0; i>x>>y>>z; + V[x].push_back(make_pair(y,z)); + V[y].push_back(make_pair(x,z)); + } + for (int i=1; i<=N; i++) + d[i] = 1<<20; + d[1] = 0; + pq.push(make_pair(1,0)); + while (!pq.empty()) + { + int nod = pq.top().first; + + if (visited[nod]) + { + pq.pop(); + continue; + } + visited[nod] = 1; + cost+= pq.top().second; + + pq.pop(); + if (nod != 1) + ANS.push_back(make_pair(p[nod], nod)); + for (int i=0; i V[nod][i].second) + { + d[V[nod][i].first] = V[nod][i].second; + p[V[nod][i].first] = nod; + pq.push(make_pair(V[nod][i].first,d[V[nod][i].first])); + + } + } + } + f2< +#include +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +struct muc{ + + int a,cost; + + bool operator<(const muc &other)const{ + + return costlis[200002]; + +multiset s; + +int v[200002],cor[200002],used[200002]; + +int n,m; + +void insertie(int nod) + +{ + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + lis[x].push_back({y,c}); + + lis[y].push_back({x,c}); + + } + + for(i=1;i<=n;i++) + + { + + v[i]=1001; + + } + + insertie(1); + + used[1]=1; + + for(i=2;i<=n;i++) + + { + + x=s.begin()->a; + + while(used[x]==1) + + s.erase(s.begin()),x=s.begin()->a; + + used[x]=1; + + insertie(x); + + ct+=v[x]; + + sol[i-1]={x,cor[x]}; + + } + + out< + + + + + + +using namespace std; + + + + + +typedef tuple edge; +int n, m, cost_apm, root; + +int dist[200010], parent[200010]; + +vector G[200010]; + +vector apm; + +priority_queue, std::greater> pq; + +bitset<200010> used; + + + +void read_input() { + + ifstream fin("apm.in"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= m; ++i) { + + int x, y, c; + + fin >> x >> y >> c; + + + + G[x].push_back(edge(c, y)); + + G[y].push_back(edge(c, x)); + + } + + + + fin.close(); + +} + + + +void Prim() { + + for (int i = 1; i <= n; ++i) { + + dist[i] = (1 << 30); + + parent[i] = 0; + + } + + + + root = 1; + + dist[root] = 0; + + parent[root] = 0; + + + + pq.push(edge(dist[root], root)); + + + + cost_apm = 0; + + + + for (int i = 1; i <= n; ++i) { + + std::tuple e; + + int node; + + + + do { + + e = pq.top(); + + pq.pop(); + + + + node = std::get<1>(e); + + } while (used[node]); + + + + cost_apm += dist[node]; + + used[node] = 1; + + + + for (auto it = G[node].begin(); it != G[node].end(); ++it) { + + int neighbour = std::get<1>(*it), cost = std::get<0>(*it); + + + + if (!used[neighbour] && cost < dist[neighbour]) { + + dist[neighbour] = cost; + + parent[neighbour] = node; + + pq.push(edge(dist[neighbour], neighbour)); + + } + + } + + } + +} + + + +void print_output() { + + ofstream fout("apm.out"); + + + + fout << cost_apm << "\n"; + + fout << (n - 1) << "\n"; + + + + for (int i = 1; i <= n; ++i) { + + if (i != root) { + + fout << i << " " << parent[i] << "\n"; + + } + + } + + + + fout.close(); + +} + + + + + + + +int main() { + + read_input(); + + Prim(); + + print_output(); + + + + return 0; + +} diff --git a/mst/prim/10074c88-48af-4410-aa19-8ff5d6dccca3.cpp b/mst/prim/10074c88-48af-4410-aa19-8ff5d6dccca3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7810c92779710f5c52d4a0d0b72a2d69c2518cde --- /dev/null +++ b/mst/prim/10074c88-48af-4410-aa19-8ff5d6dccca3.cpp @@ -0,0 +1,352 @@ +#include +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + + + + void kruskal(std::vector& toate) + + { + + heap inventar; + + for (auto muchie: toate) + + inventar.push(muchie); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(muchie.a), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + } + + } + + } + +} + + + + + + + +std::vector muchii; + +int main() + +{ + + int n, m; + + in >> n >> m; + + muchii.resize(m); + + for (auto& muchie: muchii) + + in >> muchie; + + dj::reset(n); + + apm::kruskal(muchii); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/11e0dc3d-e674-4f37-a8fb-9f2eb791a1dc.cpp b/mst/prim/11e0dc3d-e674-4f37-a8fb-9f2eb791a1dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0af618f3d2b76528c62403b82bfd06f16606004a --- /dev/null +++ b/mst/prim/11e0dc3d-e674-4f37-a8fb-9f2eb791a1dc.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +using namespace std; + +struct node{ + int value, depth; + node *parent; + + node(int v, int d, node *p = nullptr){ + this->value = v; + this->depth = d; + this->parent = p; + } + node* root(){ + if(this->parent == nullptr) return this; + this->parent = this->parent->root(); + return this->parent; + } + +}; + +struct edge{ + int x, y, cost; + edge(int d1, int d2, int c){ + this->x = d1; + this->y = d2; + this->cost = c; + } + bool operator< (const edge &other) const { + return this->cost > other.cost; + } +}; + +bool query(node* x, node* y){ + return (x->root() == y->root()); +} + +void update(node* x, node* y){ + if(x->depth < y->depth) x->root()->parent = y; + else if(x->depth > y->depth) y->root()->parent = x; + else{ + x->depth++, y->depth++; + x->root()->parent = y; + } +} +int main() { + int n, m; + ifstream f("apm.in"); + ofstream g("apm.out"); + f>>n>>m; + vector nodes; + nodes.reserve(n); + priority_queue edges; + for (int i = 0; i < n; ++i) { + nodes.push_back(new node(i, 1, nullptr)); + } + for (int i = 0; i < m; ++i) { + int x, y, o; + f>>x>>y>>o; + edges.push(edge(x - 1, y - 1, o)); + } + + int totalCost = 0; + vector> result; + result.reserve(n - 1); + while(!edges.empty()){ + edge t = edges.top(); + edges.pop(); + if(!query(nodes[t.x], nodes[t.y])){ + update(nodes[t.x], nodes[t.y]); + totalCost += t.cost; + result.push_back(make_pair(t.x, t.y)); + } + } + g< + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 2e5+10; + + + +struct nume{ + + int x,y,val; + +}; + + + +vector vec; + +priority_queue heap; + + + +int tata[DMAX]; + + + +int n,m; + +int ans; + + + +inline bool operator<(nume x,nume y){ + + return x.val>y.val; + +} + + + +int comp(int x); + +void unire(int x,int y); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y,z; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>z; + + heap.push({x,y,z}); + + } + + for(i=1;i +using namespace std; + + +using edge = std::tuple; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m, cost_apm; + + + std::vector edges[200010]; + std::priority_queue, std::greater> pq; + std::vector p; + + void read_input() { + std::ifstream fin("apm.in"); + fin >> n >> m; + for (int i = 0; i < m; i++) { + int src, dst, cost; + fin >> src >> dst >> cost; + edges[src].push_back(std::make_tuple(cost, dst)); + edges[dst].push_back(std::make_tuple(cost,src)); + } + fin.close(); + return ; + } + + void Prim() { + std::vector d(n + 1, (1 << 30)); + p = std::vector(n + 1, 0); + std::vector used(n + 1, false); + int root = 1; + d[root] = 0; + p[root] = 0; + pq.push(std::make_tuple(d[root], root)); + + for (int i = 1; i <= n; i++) { + std::tuple e; + int node; + do { + e = pq.top(); + pq.pop(); + node = std::get<1>(e); + } while (used[node]); + + cost_apm += d[node]; + used[node] = true; + for (auto e : edges[node]) { + int cost = std::get<0>(e); + int neigh = std::get<1>(e); + if (!used[neigh] && cost < d[neigh]) { + d[neigh] = cost; + p[neigh] = node; + pq.push(std::make_tuple(cost, neigh)); + } + } + } + + + } + + void get_result() { + Prim(); + } + + void print_output() { + std::ofstream fout("apm.out"); + fout<solve(); + delete task; + return 0; +} diff --git a/mst/prim/1539058f-49c9-4bc9-b02b-3b88daf16b75.cpp b/mst/prim/1539058f-49c9-4bc9-b02b-3b88daf16b75.cpp new file mode 100644 index 0000000000000000000000000000000000000000..03e6ecc2a941f2691e6d1fd6a7bf89d051f7d239 --- /dev/null +++ b/mst/prim/1539058f-49c9-4bc9-b02b-3b88daf16b75.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.insert({ y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/1579339b-c673-4fae-81d2-a2a6f5c8b6a5.cpp b/mst/prim/1579339b-c673-4fae-81d2-a2a6f5c8b6a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..693ac599dd3ebd2bc98a61fe2067bddef28befb0 --- /dev/null +++ b/mst/prim/1579339b-c673-4fae-81d2-a2a6f5c8b6a5.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + priority_queue> heap; + + heap.push({0, 1}); + + for(int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while(!heap.empty()) + + { + + cout << heap.top().second << ' ' << heap.top().first << '\n'; + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if(vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal -= cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.push({-y.second, y.first}); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/160fecc7-6a9e-4a76-adcb-7073008bc19e.cpp b/mst/prim/160fecc7-6a9e-4a76-adcb-7073008bc19e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4462b66425a8efa1837d0f2f4949af07a5b411df --- /dev/null +++ b/mst/prim/160fecc7-6a9e-4a76-adcb-7073008bc19e.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +vector > v[200010]; + +priority_queue >, vector > >, greater > > >q; + +int n,m,i,x,y,c,viz[200010],t[200010],sum; + +pair > aux; + +void prim(int st) + +{ + + viz[st]=1; + + int nrm=0,ok=0; + + while(nrm>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + v[x].push_back(make_pair(c,y)); + + v[y].push_back(make_pair(c,x)); + + } + + prim(1); + + fout< +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int n,m,c,p[200001],v[200001]; + +struct muchie + +{ int c,x,y; + +}; + +bool operator <(muchie a,muchie b) + +{ return a.c>b.c; + +} + +priority_queue h; + +vector < pair > r; + +int main() + +{ in>>n>>m; + + for(int i=1;i<=m;i++) + + { muchie a; + + in>>a.x>>a.y>>a.c; + + h.push(a); + + } + + while(!h.empty()) + + { muchie a=h.top(); + + pair x=make_pair(h.top().x,h.top().y); + + h.pop(); + + while(p[a.x]!=0) + + a.x=p[a.x]; + + while(p[a.y]!=0) + + a.y=p[a.y]; + + if(a.x==a.y) + + continue; + + if(v[a.x] +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector >> G[200003]; + +priority_queue >, vector>>, greater>>> H; + +vector > sol; + + + +int N, M, X, Y, C; + +long long sum; + +bool sel[200003]; + + + +void solve() { + + for (int i = 0; i < G[1].size(); i++) + + H.push(G[1][i]); + + + + sel[1] = true; + + + + for (int i = 1; i < N; i++) { + + while (sel[H.top().second.second]) H.pop(); + + + + auto curr = H.top(); + + sol.push_back({ curr.second.first, curr.second.second }); + + sum += curr.first; + + sel[curr.second.second] = true; + + + + for (int j = 0; j < G[curr.second.second].size(); j++) { + + if (sel[G[curr.second.second][j].second.second] == 0) { + + H.push(G[curr.second.second][j]); + + } + + } + + } + +} + +int main() + +{ + + f >> N >> M; + + + + for (int i = 1; i <= M; i++) { + + f >> X >> Y >> C; + + G[X].push_back({ C, {X, Y} }); + + G[Y].push_back({ C, {Y, X } }); + + } + + + + solve(); + + + + g << sum << "\n" << sol.size() << "\n"; + + + + for (auto i : sol) + + g << i.first << " " << i.second << "\n"; + + return 0; + +} diff --git a/mst/prim/16f75719-235a-4899-b648-6430bd38240e.cpp b/mst/prim/16f75719-235a-4899-b648-6430bd38240e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c4592f470352e50effbe893eaa226fc580dc693 --- /dev/null +++ b/mst/prim/16f75719-235a-4899-b648-6430bd38240e.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + +struct Edge { + int x, y, cost; +}; + +int n, m, i, j, k, cmin; +bool viz[200005]; +Edge mc, aux, heap[200005]; +vector> arbore, v[200005]; + +void pushup(int p) { + int val; + + val = heap[p].cost; + while(p > 1 && val < heap[p / 2].cost) { + swap(heap[p], heap[p / 2]); + p /= 2; + } +} + +void pushdown(int p) { + int fiu; + + fiu = 1; + while(fiu) { + fiu = 0; + if(p * 2 <= m) { + fiu = p * 2; + if(p * 2 + 1 <= m && heap[p * 2 + 1].cost < heap[p * 2].cost) + fiu = p * 2 + 1; + if(heap[p].cost < heap[fiu].cost) + fiu = 0; + } + if(fiu) { + swap(heap[p], heap[fiu]); + p = fiu; + } + } +} + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + + f >> n >> m; + while(m) { + f >> i >> j >> k; + v[i].emplace_back(j, k); + v[j].emplace_back(i, k); + m--; + } + f.close(); + + for(i = 1; i <= n; i++) + viz[i] = false; + viz[1] = true; + m = 0; + for(i = 0; i < v[1].size(); i++) { + mc.x = 1; + mc.y = v[1][i].first; + mc.cost = v[1][i].second; + heap[++m] = mc; + pushup(m); + } + cmin = 0; + for(k = 1; k < n; k++) { + mc = heap[1]; + cmin += mc.cost; + heap[1] = heap[m--]; + pushdown(1); + arbore.emplace_back(mc.x, mc.y); + viz[mc.y] = true; + for(i = 0; i < v[mc.y].size(); i++) + if(!viz[v[mc.y][i].first]) { + aux.x = mc.y; + aux.y = v[mc.y][i].first; + aux.cost = v[mc.y][i].second; + heap[++m] = aux; + pushup(m); + } + while(viz[heap[1].x] && viz[heap[1].y]) { + heap[1] = heap[m--]; + pushdown(1); + } + } + + g << cmin << '\n' << n - 1 << '\n'; + for(i = 0; i < arbore.size(); i++) + g << arbore[i].first << ' ' << arbore[i].second << '\n'; + g.close(); + + return 0; +} diff --git a/mst/prim/17c0a3dd-af15-4428-9e59-cda8fcce1952.cpp b/mst/prim/17c0a3dd-af15-4428-9e59-cda8fcce1952.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfb0cb697f2f37910407d8bb2f7eef125a936360 --- /dev/null +++ b/mst/prim/17c0a3dd-af15-4428-9e59-cda8fcce1952.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200005; + + + +vector> g[N]; + +int CostMin[N]; + +bool vizitat[N]; + +int tata[N]; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + for (int i = 2; i <= n; i++) + + { + + CostMin[i] = INT_MAX; + + } + + + + priority_queue> heap; + + heap.push({ 0, 1 }); + + + + int CostTotal = 0; + + while (!heap.empty()) + + { + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if (vizitat[nod]) + + { + + continue; + + } + + + + vizitat[nod] = true; + + CostTotal -= cost; + + + + for (auto neigh : g[nod]) + + { + + if (!vizitat[neigh.first] && neigh.second < CostMin[neigh.first]) + + { + + heap.push({ -neigh.second, neigh.first }); + + CostMin[neigh.first] = neigh.second; + + tata[neigh.first] = nod; + + } + + } + + } + + + + fout << CostTotal << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + { + + fout << i << ' ' << tata[i] << '\n'; + + } + +} diff --git a/mst/prim/18b41ed1-a57a-4cfc-98a7-8545ec68f443.cpp b/mst/prim/18b41ed1-a57a-4cfc-98a7-8545ec68f443.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f3b1bbfae4dc3c82277a62759daf8cd18c13978 --- /dev/null +++ b/mst/prim/18b41ed1-a57a-4cfc-98a7-8545ec68f443.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct ura1 + +{ + + int nod,cost; + + bool operator <(const ura1 &other)const + + { + + return cost l[200005]; + +multiset s; + +ura2 sol[200005]; + +int v[200005],cor[200005],vc[200005]; + +void apm (int x) + +{ + + int i,y,c; + + for (i=0;ic) + + { + + v[y]=c; + + s.insert(l[x][i]); + + cor[y]=x; + + } + + } + +} + +int main () + +{ + + int rez=0,n,m,i,x,y,c,nod; + + fin>>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + l[x].push_back({y,c}); + + l[y].push_back({x,c}); + + } + + for (i=2;i<=n;i++) + + { + + v[i]=2000000000; + + s.insert({i,v[i]}); + + } + + apm(1); + + vc[1]=1; + + for (i=1;inod; + + while (vc[nod]) + + { + + s.erase(s.begin()); + + nod=s.begin()->nod; + + } + + s.erase(s.begin()); + + vc[nod]=1; + + apm(nod); + + rez+=v[nod]; + + sol[i]={nod,cor[nod]}; + + } + + fout< + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair , vector < pair > , greater < pair > > q; + +vector < pair > v[200100]; + +int CT,n,viz[200100],cost_muchie[200100],tata[200100]; + + + +void prim (int nod_start) + +{ + + for(int i=1;i<=n;i++)cost_muchie[i]=INT_MAX/2; + + cost_muchie[nod_start]=0; + + q.push({0,nod_start}); + + + + while(!q.empty()) + + { + + int nod=q.top().second; + + int cost_muchie_nod=q.top().first; + + q.pop(); + + + + if(viz[nod]==0) + + { + + viz[nod]=1; + + CT+=cost_muchie_nod; + + + + for(int i=0;icost_intre && viz[vecin]==0) + + { + + cost_muchie[vecin]=cost_intre; + + q.push({cost_muchie[vecin],vecin}); + + tata[vecin]=nod; + + } + + } + + } + + } + +} + + + +int m,i,x,y,cost; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + v[x].push_back({y,cost}); + + v[y].push_back({x,cost}); + + } + + + + prim(1); + + + + g< +#include +#include +#include + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +const int marime = 200005; + +int suma; +bool f[marime]; +vector> sir; +vector> mat[marime]; +priority_queue>> q; + +int main() { + int n, m; + fin >> n >> m; + + for (int i = 1; i <= m; ++i) { + int x, y, cost; + fin >> x >> y >> cost; + + mat[x].push_back({y, cost}); + mat[y].push_back({x, cost}); + } + + f[1] = 1; + + for (int i = 0; i < mat[1].size(); ++i) { + q.push({-mat[1][i].second, {1, mat[1][i].first}}); + } + + while (!q.empty()) { + int cost = q.top().first; + int from = q.top().second.first; + int to = q.top().second.second; + q.pop(); + + if (f[to] == 1) { + continue; + } + + sir.push_back({from, to}); + suma += cost; + f[to] = 1; + + for (int i = 0; i < mat[to].size(); ++i) { + if (f[mat[to][i].first] == 1) { + continue; + } + + q.push({-mat[to][i].second, {to, mat[to][i].first}}); + } + } + + fout << -suma << "\n" << n - 1 << "\n"; + + for (int i = 0; i < sir.size(); ++i) { + fout << sir[i].first << " " << sir[i].second << "\n"; + } + + return 0; +} diff --git a/mst/prim/1af88379-3aa8-458b-a116-8ed3d90508e4.cpp b/mst/prim/1af88379-3aa8-458b-a116-8ed3d90508e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b466948c06765843fcdb8bacd5cf3b1704d8efac --- /dev/null +++ b/mst/prim/1af88379-3aa8-458b-a116-8ed3d90508e4.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + fin >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + fin >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0, length = n; + + for(int i = 1; i <= n; i ++) + + { + + + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(length) + + { + + + + pair least = q.top(); + + int node = least.second; + + + + + + + + q.pop(); + + if(viz[node] == 0) + + continue; + + length -- ; + + viz[node] = 0; + + cost += least.first; + + + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + fout< + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +typedef pair < int, int > Pair; + +int N, M, ans; + +vector < Pair > G[200002]; + +list < Pair > sol; + +priority_queue < Pair, vector < Pair > , greater < Pair > > Heap; + +vector < int > key (200002, INT_MAX), parent (200002); + +vector < bool > viz (200002); + + + +void Read () { + + fin >> N >> M; + + while (M --) { + + int u, v, wt; + + fin >> u >> v >> wt; + + G[u].push_back ({v, wt}); + + G[v].push_back ({u, wt}); + + } + +} + + + +void Prim () { + + Heap.push ({0, 1}); + + key[1] = 0; + + while (!Heap.empty()) { + + int u = Heap.top().second; + + Heap.pop(); + + viz[u] = true; + + for (auto it : G[u]) { + + int v = it.first, weight = it.second; + + if (viz[v] == false && key[v] > weight) { + + key[v] = weight; + + Heap.push ({key[v], v}); + + parent[v] = u; + + } + + } + + } + + for (int i = 2; i <= N; i ++) { + + sol.push_back ({parent[i], i}); + + ans += key[i]; + + } + +} + + + +void Solve () { + + Read(); + + Prim(); + + fout << ans << '\n' << N - 1 << '\n'; + + for (auto it : sol) + + fout << it.first << ' ' << it.second << '\n'; + +} + + + +int main() { + + Solve(); + + return 0; + +} diff --git a/mst/prim/1d8cc048-7614-433f-a934-3822b1696b95.cpp b/mst/prim/1d8cc048-7614-433f-a934-3822b1696b95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50bee1493d363736442c8e5326e3228e99aa7693 --- /dev/null +++ b/mst/prim/1d8cc048-7614-433f-a934-3822b1696b95.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + + +using namespace std; + +const int NMAX = 200010; + +int N,M; +priority_queue< pair >, vector< pair > >, greater< pair > > >pq; + +vector> G[NMAX], apm; +bool viz[NMAX]; + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + f>>N>>M; + int s,d,c; + for(int i=0;i>s>>d>>c; + G[s].push_back(make_pair(d,c)); + G[d].push_back(make_pair(s,c)); + } + + int rad = 1; + viz[rad] = true; + + for(auto muchie: G[rad]) + pq.push(make_pair(muchie.second,make_pair(rad,muchie.first))); + + int cost = 0; + + while(!pq.empty()) + { + pair> aux = pq.top(); + pq.pop(); + + int c = aux.first; + int next = aux.second.second; + + if(!viz[next]) + { + viz[next] = true; + cost+= c; + apm.push_back(aux.second); + + for(auto m: G[next]) + pq.push(make_pair(m.second,make_pair(next,m.first))); + + } + } + + g< +#include +#include + + + + + + +using namespace std; + + + +const int maxn = 200200; + +const int INF = 200000200; + + + +int VEC[maxn],ANS,V[maxn],H[maxn * 2 + 100],POZ[maxn]; + +vector > VECT[maxn],VANS; + +int N,M,L,C[maxn]; + + + +void introduce_in_apm(int x) + +{ + + for(unsigned int i = 0;i < VECT[x].size(); ++i) + + { + + int nod = VECT[x][i].first; + + int cost = VECT[x][i].second; + + V[nod] = min(V[nod],cost); + + if (V[nod] == cost) VEC[nod] = x; + + } + +} + + + +void push(int poz) + +{ + + while((poz * 2 <= L && V[H[poz]] > V[H[poz * 2]]) || (poz * 2 + 1 <= L && V[H[poz]] > V[H[poz * 2 + 1]])) + + { + + if (V[H[poz * 2]] < V[H[poz * 2 + 1]] || poz * 2 + 1 > L) + + { + + swap(H[poz],H[poz * 2]); + + swap(POZ[H[poz]],POZ[H[poz * 2]]); + + poz *= 2; + + } + + else + + { + + swap(H[poz],H[poz * 2 + 1]); + + swap(POZ[H[poz]],POZ[H[poz * 2 + 1]]); + + poz = poz * 2 + 1; + + } + + } + +} + + + + + +void pop(int poz) + +{ + + while(poz > 1 && V[H[poz]] < V[H[poz / 2]]) + + { + + swap(H[poz],H[poz / 2]); + + swap(POZ[H[poz]],POZ[H[poz / 2]]); + + poz = poz / 2; + + } + +} + + + +void introduce_in_heap(int nod) + +{ + + H[++L] = nod; + + POZ[nod] = L; + + pop(L); + +} + + + +int scoate_radacina_heap() + +{ + + int x = H[1]; + + swap(H[1],H[L]); + + swap(POZ[H[1]],POZ[H[L]]); + + L--; + + push(1); + + POZ[x] = 0; + + return x; + +} + + + +int main() + +{ + + freopen("apm.in","r",stdin); + + freopen("apm.out","w",stdout); + + scanf("%d %d\n",&N,&M); + + for(int i = 1;i <= M; ++i) + + { + + int x,y,c; + + scanf("%d %d %d",&x,&y,&c); + + VECT[x].push_back(make_pair(y,c)); + + VECT[y].push_back(make_pair(x,c)); + + } + + + + for(int i = 1;i <= N; ++i) V[i] = INF; + + V[1] = 0; + + introduce_in_apm(1); + + for(int i = 2;i <= N; ++i) + + { + + introduce_in_heap(i); + + } + + for(int i = 1;i < N; ++i) + + { + + int x = scoate_radacina_heap(); + + introduce_in_apm(x); + + ANS += V[x]; + + VANS.push_back(make_pair(x,VEC[x])); + + for(unsigned int j = 0;j < VECT[x].size(); ++j) + + { + + int nod = VECT[x][j].first; + + if (POZ[nod]) pop(POZ[nod]); + + } + + } + + printf("%d\n",ANS); + + printf("%d\n",N-1); + + for(unsigned int i = 0;i < N - 1; ++i) + + printf("%d %d\n",VANS[i].first,VANS[i].second); + + return 0; + +} diff --git a/mst/prim/1ed7b08e-0e11-4dd0-9f92-f1c7454ee776.cpp b/mst/prim/1ed7b08e-0e11-4dd0-9f92-f1c7454ee776.cpp new file mode 100644 index 0000000000000000000000000000000000000000..282f11edc0a63764194a30b9fb0682ee90593560 --- /dev/null +++ b/mst/prim/1ed7b08e-0e11-4dd0-9f92-f1c7454ee776.cpp @@ -0,0 +1,132 @@ +#include + + + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector >adj[200000 + 5]; + +vectorkey(200000 + 5, 1e9); + +priority_queue, vector >, greater > > pq; + +int parent[200000 + 5]; + +bool inMST[200000 + 5]; + +int n, m; + + + +int main() + +{ + + ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); + + + + fin>>n>>m; + + + + for(int i = 1; i <= m; i++) + + { + + int u, v, weight; + + fin>>u>>v>>weight; + + + + adj[u].push_back(make_pair(weight, v)); + + adj[v].push_back(make_pair(weight, u)); + + } + + + + pq.push(make_pair(0, 1)); + + key[1] = 0; + + + + while(!pq.empty()) + + { + + int u = pq.top().second; + + pq.pop(); + + + + inMST[u] = true; + + + + for(int i = 0; i < adj[u].size(); i++) + + { + + int v = adj[u][i].second; + + int weight = adj[u][i].first; + + + + if(!inMST[v] && key[v] > weight) + + { + + key[v] = weight; + + pq.push(make_pair(key[v], v)); + + parent[v] = u; + + } + + } + + } + + int s = 0; + + + + for(int i = 1; i <= n; i++) + + s += key[i]; + + + + fout< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + set> heap; + + heap.insert({0, 1}); + + for(int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while(!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if(vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first] && y.second < minCost[y.first]) + + { + + if(vis[y.first] && y.second < minCost[y.first]) + + heap.erase(heap.find({minCost[y.first], y.first})); + + + + heap.insert({y.second, y.first}); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/207fe32d-8db2-4894-b80a-a02f7eaefeb8.cpp b/mst/prim/207fe32d-8db2-4894-b80a-a02f7eaefeb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd36428b6dcdc504a3ded61b6ef28d2b04f7d90f --- /dev/null +++ b/mst/prim/207fe32d-8db2-4894-b80a-a02f7eaefeb8.cpp @@ -0,0 +1,76 @@ +#include +using namespace std; + + + + + +typedef vector vi; +typedef vector vvi; +typedef pair ii; +typedef vector vii; +typedef long long ll; +typedef vector vll; +typedef vector vvll; +typedef double ld; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +int n, m; +vii G[200010]; +vector visited; +int dist[200010]; +int p[200010]; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + fin >> n >> m; + for (int i = 0; i < m; i++) { + int x, y, c; + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, c)); + } + + for (int i = 1; i <= n; i++) { + visited.push_back(0); + dist[i] = INT_MAX; + } + + int cost_total = 0; + set s; + dist[1] = 0; + s.insert(make_pair(0, 1)); + p[1] = 0; + for (int i = 1; i <= n; i++) { + int node = s.begin()->second; + int d = s.begin()->first; + s.erase(s.begin()); + visited[node - 1] = 1; + cost_total += d; + + for (auto &it : G[node]) { + int vecin = it.first; + int cost = it.second; + if (!visited[vecin - 1] && dist[vecin] > cost) { + if (dist[vecin] != INT_MAX) { + s.erase(s.find(make_pair(dist[vecin], vecin))); + } + dist[vecin] = cost; + s.insert(make_pair(dist[vecin], vecin)); + p[vecin] = node; + } + } + } + + fout << cost_total << "\n" << n - 1 << "\n"; + + for (int i = 2; i <= n; i++) { + fout << i << " " << p[i] << "\n"; + } + + return 0; +} diff --git a/mst/prim/21d68f15-2b42-4374-880d-ec463f93c3ce.cpp b/mst/prim/21d68f15-2b42-4374-880d-ec463f93c3ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68cf652e98c6bd3bbfe4f1127b425fadb348ae6f --- /dev/null +++ b/mst/prim/21d68f15-2b42-4374-880d-ec463f93c3ce.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include + + +const int MAXN = 200000 + 5; + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +typedef pair > pii; + + + +vector >graf[MAXN]; + +priority_queue, greater >coada; + +bool viz[MAXN]; + +int n,m; + + + +int main() + +{ + + + + in>>n>>m; + + for(int i = 1; i <= m; i++){ + + int x,y,cost; + + in>>x>>y>>cost; + + graf[x].push_back({cost,y}); + + graf[y].push_back({cost,x}); + + } + + viz[1] = 1; + + for(int i = 0; i < graf[1].size(); i++) + + coada.push({graf[1][i].first,{1,graf[1][i].second}}); + + long long s = 0; + + vector>arbore; + + while(!coada.empty() && arbore.size() < n - 1){ + + pii ceva = coada.top(); + + int tata = ceva.second.first,nod = ceva.second.second; + + int cost = ceva.first; + + coada.pop(); + + if(viz[nod]) + + continue; + + s += cost; + + arbore.push_back({tata,nod}); + + viz[nod] = true; + + for(int i = 0; i < graf[nod].size(); i++){ + + int vecin = graf[nod][i].second,cost = graf[nod][i].first; + + if(!viz[vecin]){ + + coada.push({cost,{nod,vecin}}); + + } + + } + + } + + out< + + + + +using namespace std; + + + +int main() + +{ + + int n , m, cost = 0; + + ifstream fin("apm.in"); + + fin >> n >> m; + + priority_queue, vector>, greater>> q; + + vector>> adj(n + 1); + + vector dist(n + 1, 1e9); + + vector parent(n + 1, 0); + + vector used(n + 1, false); + + for (int i = 1, x, y, c; i <= m; ++i) { + + fin >> x >> y >> c; + + adj[x].push_back({c, y}); + + adj[y].push_back({c, x}); + + } + + fin.close(); + + + + dist[1] = 0; + + q.push({dist[1], 1}); + + + + for (int i = 1; i <= n; ++i) { + + int node; + + do { + + node = q.top().second; + + q.pop(); + + } while (used[node]); + + + + cost += dist[node]; + + used[node] = true; + + + + for (auto &neigh : adj[node]) { + + if (!used[neigh.second] && neigh.first < dist[neigh.second]) { + + dist[neigh.second] = neigh.first; + + parent[neigh.second] = node; + + q.push({neigh.first, neigh.second}); + + } + + } + + } + + + + ofstream fout("apm.out"); + + fout << cost << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; ++i) { + + fout << i << ' ' << parent[i] << '\n'; + + } + + fout.close(); + + return 0; + +} diff --git a/mst/prim/2316f819-45d1-405c-931d-dc6edaafb615.cpp b/mst/prim/2316f819-45d1-405c-931d-dc6edaafb615.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22052ad9115c8cd43c6b383f1f3f71c138c2411c --- /dev/null +++ b/mst/prim/2316f819-45d1-405c-931d-dc6edaafb615.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + + +template +struct triplet +{ + T from, to, cost; +}; + +template +triplet make_triplet(const T &m1, const T &m2, const T &m3) +{ + triplet ans; + ans.from = m1; + ans.to = m2; + ans.cost = m3; + return ans; +} + +class Compare +{ +public: + bool operator() (triplet t1, triplet t2) + { + return (t1.cost > t2.cost); + } +}; + +void Prims(int = 1); +void addEdges(int node); + +const int VMAX = 200001, EMAX = 400001; +int v, e; +bool viz[VMAX]; + + +vector> ad[VMAX]; + + +priority_queue, vector>, Compare> pq; + + +pair rez[VMAX-1]; +int mstCost = 0; + +int main() +{ + in >> v >> e; + for(int i=0; i> from >> to >> cost; + ad[from].push_back(make_pair(to, cost)); + ad[to].push_back(make_pair(from, cost)); + + } + + Prims(); + + return 0; +} + +void Prims(int start_node) +{ + int i= 0; + addEdges(start_node); + + while(!pq.empty() && i != v - 1) + { + triplet curr_edge = pq.top(); + pq.pop(); + if(!viz[curr_edge.to]) + { + rez[i++] = make_pair(curr_edge.from, curr_edge.to); + mstCost += curr_edge.cost; + addEdges(curr_edge.to); + } + } + + out << mstCost << '\n'; + out << v - 1 << '\n'; + for(int i=0; i +#include +#include + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct amp{ + + int x, y, c; + +}; + + + +struct lat{ + + int index, cost; + +}; + + + +struct comp { + + bool operator()(amp const& a, amp const& b) { + + return a.c > b.c; + + } + +}; + + + +int n, m, frecNod[200005] , totalCost; + +vector < vector > L(200005); + +vector rezultat; + +priority_queue , comp> coada; + + + +amp muchie; + + + +int main() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; ++i){ + + int x, y, c; + + lat latura; + + in>>x>>y>>c; + + latura.cost = c; + + + + latura.index = y; + + L[x].push_back(latura); + + + + latura.index = x; + + L[y].push_back(latura); + + } + + + + muchie.x = 0; + + muchie.y = 1; + + muchie.c = 0; + + coada.push(muchie); + + + + while(!coada.empty()){ + + + + amp curent = coada.top(); + + + + coada.pop(); + + + + if(frecNod[curent.y]) continue; + + rezultat.push_back(curent); + + frecNod[curent.y] = 1; + + + + totalCost += curent.c; + + for(int i = 0; i < L[curent.y].size(); i++){ + + muchie.x = curent.y; + + muchie.y = L[curent.y][i].index; + + muchie.c = L[curent.y][i].cost; + + + + coada.push(muchie); + + } + + } + + + + out< +#include +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue> pq; + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + used[1]=1; + + for(auto it:v[1]) + + { + + tie(x,c)=it; + + pq.push(make_tuple(-c,1,x)); + + } + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + tie(c,x,y)=pq.top();c=-c;; + + pq.pop(); + + if(used[y]) + + continue; + + used[y]=1; + + cost+=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + for(auto it:v[y]) + + { + + tie(x,c)=it; + + if(!used[x]) + + pq.push(make_tuple(-c,y,x)); + + } + + + + } + + g< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200005; + + + +vector> g[N]; + +int CostMin[N]; + +bool vizitat[N]; + +int tata[N]; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + for (int i = 2; i <= n; i++) + + { + + CostMin[i] = INT_MAX; + + } + + + + priority_queue> heap; + + heap.push({ 0, 1 }); + + + + int CostTotal = 0; + + while (!heap.empty()) + + { + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if (vizitat[nod]) + + { + + continue; + + } + + + + vizitat[nod] = true; + + CostTotal -= cost; + + + + for (auto neigh : g[nod]) + + { + + if (!vizitat[neigh.first] && neigh.second < CostMin[neigh.first]) + + { + + heap.push({ -neigh.second, neigh.first }); + + CostMin[neigh.first] = neigh.second; + + tata[neigh.first] = nod; + + } + + } + + } + + + + fout << CostTotal << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + { + + fout << i << ' ' << tata[i] << '\n'; + + } + +} diff --git a/mst/prim/26a84341-b427-43c3-9a48-4dabb3828cfb.cpp b/mst/prim/26a84341-b427-43c3-9a48-4dabb3828cfb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4462b66425a8efa1837d0f2f4949af07a5b411df --- /dev/null +++ b/mst/prim/26a84341-b427-43c3-9a48-4dabb3828cfb.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +vector > v[200010]; + +priority_queue >, vector > >, greater > > >q; + +int n,m,i,x,y,c,viz[200010],t[200010],sum; + +pair > aux; + +void prim(int st) + +{ + + viz[st]=1; + + int nrm=0,ok=0; + + while(nrm>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + v[x].push_back(make_pair(c,y)); + + v[y].push_back(make_pair(c,x)); + + } + + prim(1); + + fout< +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +vector > v[200001]; +int cmin[200001], p[200001]; + +struct el +{ + int nod, c; + bool operator <(const el &alt) const + { + return c > alt.c; + } +}; + +priority_queue q; + +int main() +{ + int n, m, i, x, y, c, j; + int cost = 0; + el nmin; + fin >> n >> m; + for (i = 1; i<=m; i++) + { + fin >> x >> y >> c; + v[x].push_back({y, c}); + v[y].push_back({x, c}); + } + for (i = 1; i<=n; i++) + cmin[i] = 1<<30; + cmin[1] = -(1<<30); + for (i = 0; i cmin[q.top().nod]) + q.pop(); + nmin = q.top(); + cost += nmin.c; + cmin[nmin.nod] = -(1<<30); + for (j = 0; j +#include +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue> pq; + +void add(int); + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + add(1); + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + tie(c,x,y)=pq.top(); + + pq.pop(); + + if(used[y]) + + continue; + + cost-=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + add(y); + + } + + g< +#include +#include +#include +#include +using namespace std; +ifstream f ("apm.in"); +ofstream g ("apm.out"); +int n, m; +vector < pair> *vec; +int x, y, c; +int *d, *t, *viz; +int s; +void citire() +{ + f >> n >> m; + vec = new vector < pair>[n + 1]; + for (int i = 1; i <= m; ++i) + { + f >> x >> y >> c; + vec[x].push_back ({y, c}); + vec[y].push_back ({x, c}); + } + f.close(); +} +void golire_alocare_dinamica() +{ + d=new int[n+1]; + t=new int[n+1]; + viz=new int[n+1]; + for (int i = 1; i <= n; ++i) + { + d[i] = INT_MAX; + t[i] = 0; + viz[i] = 0; + } +} +void afisare() +{ + s = 0; + for (int i = 1; i <= n; i++) + s += d[i]; + g << s << '\n' << n - 1 << '\n'; + for (int i = 1; i <= n; i++) + if (t[i] != 0) + g << t[i] << " " << i << '\n'; + g.close(); +} +void prim() +{ + s = 1; + d[s] = 0; + + priority_queue < pair > q; + q.push ({-d[s], s}); + while (!q.empty() ) + { + x = q.top().second; + q.pop(); + viz[x] = 1; + for (auto i : vec[x]) + { + y = i.first; + c = i.second; + if (viz[y] == 0 && d[y] > c) + { + t[y] = x; + d[y] = c; + q.push ({-d[y], y}); + } + } + } +} +int main() +{ + citire(); + + golire_alocare_dinamica(); + + prim(); + + afisare(); + + return 0; +} diff --git a/mst/prim/28bb7165-8734-4aad-9a70-067bad3dc5a0.cpp b/mst/prim/28bb7165-8734-4aad-9a70-067bad3dc5a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7493e3f559d9b01282b89626585f72590fd8ccba --- /dev/null +++ b/mst/prim/28bb7165-8734-4aad-9a70-067bad3dc5a0.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +vector > > v[200020]; + +priority_queue >, vector > >, greater > > > pq; + +queue > q; + +pair > elem; + +int n, m, viz[200020], cost, nrm; + +void citire () + +{ + + fin>>n>>m; + + int i, x, y, co; + + for (i=1; i<=m; i++) + + { + + fin>>x>>y>>co; + + v[x].push_back(make_pair(co,make_pair(x, y))); + + v[y].push_back(make_pair(co,make_pair(y, x))); + + } + +} + +void prim () + +{ + + cost=0; nrm=0; + + int i, j; + + for (i=0; i +#include +#include + + +using namespace std; + + + + + + + +vector>> arcs; + +vector> usedArcs; + +vector heap, poz, inTreeCost, parents; + + + +int heapSize; + + + + + +void add_to_apm(int x) { + + for(int i=0; i 1 && inTreeCost[heap[current_node]] < inTreeCost[heap[current_node / 2]]) { + + aux = heap[current_node]; + + heap[current_node] = heap[current_node / 2]; + + heap[current_node / 2] = aux; + + + + poz[heap[current_node]] = current_node; + + poz[heap[current_node / 2]] = current_node / 2; + + + + current_node /= 2; + + } + +} + + + + + +void sift(int current_node) { + + int aux, current_son = 1; + + while (current_son) { + + current_son = 0; + + if (current_node * 2 <= heapSize && inTreeCost[heap[current_node]] >inTreeCost[heap[current_node * 2]]) + + current_son = current_node * 2; + + + + if (current_node * 2 + 1 <= heapSize && inTreeCost[heap[current_node]] > inTreeCost[heap[current_node * 2 + 1]] && + + (current_son == 0 || inTreeCost[heap[current_son]] > inTreeCost[heap[current_node * 2 + 1]])) + + current_son = current_node * 2 + 1; + + + + if (current_son) { + + aux = heap[current_son]; + + heap[current_son] = heap[current_node]; + + heap[current_node] = aux; + + + + poz[heap[current_node]] = current_node; + + poz[heap[current_son]] = current_son; + + + + current_node = current_son; + + } + + } + +} + + + + + +void add_to_heap(int x) { + + ++heapSize; + + heap[heapSize] = x; + + poz[x] = heapSize; + + percolate(heapSize); + +} + + + + + +void delete_node(int current_node) { + + if (! heapSize) + + return; + + poz[heap[current_node]] = -1; + + heap[current_node] = heap[heapSize]; + + poz[heap[current_node]] = current_node; + + --heapSize; + + + + if (current_node > 1 && inTreeCost[heap[current_node / 2]] > inTreeCost[heap[current_node]]) + + percolate(current_node); + + else + + sift(current_node); + + + +} + + + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + int n, m, a, b, c; + + scanf("%d%d", &n, &m); + + arcs.resize(n + 1); + + heap.resize(m * 2 + 10, 0); + + poz.resize(n + 1, 0); + + inTreeCost.resize(n+1, 1<<30); + + parents.resize(n+1, 0); + + + + for(int i=0; i(b, c)); + + arcs[b].push_back(pair(a, c)); + + } + + + + inTreeCost[1] = 0; + + add_to_apm(1); + + + + for(int i=2; i<=n; ++i) + + add_to_heap(i); + + + + c = 0; + + for(int i=1; i(parents[a], a)); + + + + for(int j=0; j + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > q; + +vector < pair < int , int > > v[200100]; + +int totaldist,n,viz[200100],dist[200100],tata[200100]; + +int m,i,x,y,cost; + +int oo=2000000009; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(int i=1;i<=n;i++) { + + dist[i]=oo; + + } + + dist[1]=0; + + q.push(make_pair(0,1)); + + while(q.empty()==0) { + + int nod = q.top().second; + + int distnod = q.top().first; + + q.pop(); + + if(viz[nod]==0) { + + viz[nod] = 1; + + totaldist += distnod; + + for (int i=0;i distance && viz[nodtoviz]==0) { + + dist[nodtoviz] = distance; + + q.push(make_pair(dist[nodtoviz],nodtoviz)); + + tata[nodtoviz] = nod; + + } + + } + + } + + } + + g << totaldist << '\n'; + + g << n-1 << '\n'; + + for (i=1;i<=n;i++) { + + if (tata[i]!=0) { + + g << i << " " << tata[i] << '\n'; + + } + + } + + return 0; + +} diff --git a/mst/prim/2b6bdde3-ade7-4b97-8d85-47cf3f713400.cpp b/mst/prim/2b6bdde3-ade7-4b97-8d85-47cf3f713400.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9d4ef50032890704cf58e49a15c17b8a7e0c774 --- /dev/null +++ b/mst/prim/2b6bdde3-ade7-4b97-8d85-47cf3f713400.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout("apm.out"); + +struct muchie + +{ + + int x,y,c; + +}; + +struct pereche + +{ + + int y,c; + +}; + +struct comp + +{ + + bool operator()(muchie u, muchie v) + + { + + if(u.c>v.c) + + return 1; + + return 0; + + } + +}; + +vectorG[200001]; + +priority_queue,comp>q; + +int n,m,x,y,c,t[200001],nr, viz[200001],ct; + +int main() + +{ + + pereche p; + + int i; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + p={y,c}; + + G[x].push_back(p); + + p={x,c}; + + G[y].push_back(p); + + } + + for(i=0;i + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim]; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + Solve1(); + + + + return 0; + +} diff --git a/mst/prim/2fd5a35f-43d9-4330-8cdf-0378100cb6d4.cpp b/mst/prim/2fd5a35f-43d9-4330-8cdf-0378100cb6d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb93a0ea19807fa3e4f07cfe71c458b680c4831e --- /dev/null +++ b/mst/prim/2fd5a35f-43d9-4330-8cdf-0378100cb6d4.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +using namespace std; + + + +const int maxi=1<<30; + + + +ifstream be("apm.in"); + +ofstream ki("apm.out"); + + + +void prim(vector > >&adj ,int n) + +{ + + vectormstset(n+1,false); + + vectorparent(n+1,-1); + + vectorkey(n+1,maxi); + + priority_queue,deque >,greater > >q; + + int src=1; + + int db=0; + + parent[src]=src; + + q.push({0,src}); + + key[src]=0; + + while(!q.empty()) + + { + + int u=q.top().second; + + int cost=q.top().first; + + q.pop(); + + if(mstset[u] || cost!=key[u])continue; + + mstset[u]=true; + + db+=cost; + + for(auto p:adj[u]) + + { + + int v=p.first; + + int c=p.second; + + if(mstset[v]==false && key[v]>c) + + { + + key[v]=c; + + q.push({key[v],v}); + + parent[v]=u; + + } + + } + + } + + printf("%d\n%d\n",db,n-1); + + for (int i = 1; i <= n; i++) + + { + + if(i!=src) + + { + + printf("%d %d\n",i,parent[i]); + + } + + } + + + +} + + + + + +int main() + +{ + + freopen("apm.in","r",stdin); + + freopen("apm.out","w",stdout); + + int n,m; + + scanf("%d %d",&n,&m); + + vector > >adj(n+1); + + for(int i=0;i +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + priority_queue> heap; + + heap.push({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + cout << heap.top().second << ' ' << heap.top().first << '\n'; + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal -= cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.push({ -y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/3236d5fa-979a-4143-8452-7ae56ef8ddc6.cpp b/mst/prim/3236d5fa-979a-4143-8452-7ae56ef8ddc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bda99bf1a6494170194d0bdb3892faca070990a --- /dev/null +++ b/mst/prim/3236d5fa-979a-4143-8452-7ae56ef8ddc6.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +pair > elem, aux; + +priority_queue >, vector > >, greater > > > pq; + +queue > q2; + +pair elem2; + +int n, m, t[200005], cost, nrm, viz[200005], auxx; + +struct nod { int et,ct; + + nod *urm; + + } *l[200005]; + +void add_nod (int x, int y, int c) + +{ + + nod *q; + + q=new nod; + + q->et=y; + + q->ct=c; + + q->urm=l[x]; + + l[x]=q; + +} + +void citire () + +{ + + int x, y, co; + + fin>>n>>m; + + for (int i=1; i<=m; i++) + + { + + fin>>x>>y>>co; + + add_nod(y, x, co); + + add_nod(x, y, co); + + } + +} + +int main () + +{ + + int i; + + citire(); + + nod *q; + + q=l[1]; + + while (q!=NULL) + + { + + elem.first=q->ct; + + elem.second.first=1; + + elem.second.second=q->et; + + pq.push(elem); + + q=q->urm; + + } + + i=1; nrm=0; + + viz[1]=1; + + t[1]=0; + + while (ict; + + aux.second.first=x; + + aux.second.second=q->et; + + pq.push(aux); + + q=q->urm; + + } + + elem2.first=elem.second.first; + + elem2.second=elem.second.second; + + q2.push(elem2); + + i++; + + } + + fout< +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int INF = 2100000000; + + + +int n, m; + +vector > * lv; + + + +class Edge{ + + + +public: + + + + int first; + + int second; + + + + int price; + + + + Edge(int f, int s, int p): first(f), second(s), price(p) {}; + +}; + + + +int min_price; + +vector selected; + +int * pair_node; + + + +int * d; + +bool * v; + + + +int * q_pos; + +int * q; + +int q_dim; + + + +int pop(){ + + + + swap(q_pos[q[1]], q_pos[q[q_dim]]); + + swap(q[1], q[q_dim]); + + + + q_dim -= 1; + + + + bool ok = 0; + + + + int pos = 1; + + while(!ok) + + + + if(pos * 2 > q_dim && pos * 2 + 1 > q_dim) + + + + ok = true; + + + + else if(pos * 2 > q_dim){ + + + + if(d[q[pos * 2 + 1]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + else if(pos * 2 + 1 > q_dim){ + + + + if(d[q[pos * 2]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + } + + else{ + + + + if(d[q[pos]] < d[q[pos * 2]] && d[q[pos]] < d[q[pos * 2 + 1]]) + + + + ok = true; + + + + else if(d[q[pos * 2]] < d[q[pos * 2 + 1]]){ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + + + return q[q_dim + 1]; + +} + + + +void update(int node){ + + + + int pos = q_pos[node]; + + + + bool ok = 0; + + + + while(!ok) + + + + if(pos / 2 == 0 || d[q[pos]] > d[q[pos / 2]]) + + + + ok = true; + + else{ + + + + swap(q_pos[q[pos]], q_pos[q[pos / 2]]); + + swap(q[pos], q[pos / 2]); + + + + pos /= 2; + + } + +} + + + +void prim(){ + + + + while(q_dim){ + + + + int current_node = pop(); + + v[current_node] = 1; + + + + if(current_node != 1 && d[current_node] == INF) + + return; + + + + + + if(current_node != 1){ + + + + min_price += d[current_node]; + + selected.push_back(Edge(current_node, pair_node[current_node], d[current_node])); + + } + + + + for(int i = 0; i < lv[current_node].size(); i++){ + + + + int next_node = lv[current_node][i].first; + + int new_price = lv[current_node][i].second; + + + + if(!v[next_node] && d[next_node] > new_price){ + + + + d[next_node] = new_price; + + pair_node[next_node] = current_node; + + + + update(next_node); + + } + + } + + } + +} + + + +int main(){ + + + + f >> n >> m; + + + + lv = new vector > [n + 1]; + + + + d = new int [n + 1]; + + v = new bool [n + 1]; + + + + pair_node = new int [n + 1]; + + + + q = new int [n + 1]; + + q_pos = new int [n + 1]; + + q_dim = n; + + + + for(int i = 1 ; i <= n; i++){ + + + + d[i] = INF; + + v[i] = 0; + + + + q[i] = i; + + q_pos[i] = i; + + } + + + + int x, y, c; + + for(int i = 0; i < m ; i++){ + + + + f >> x >> y >> c; + + + + lv[x].push_back(make_pair(y, c)); + + lv[y].push_back(make_pair(x, c)); + + } + + + + prim(); + + + + g << min_price << '\n'; + + + + g << selected.size() << '\n'; + + for(int i = 0; i < selected.size(); i++) + + + + g << selected[i].first << " " << selected[i].second << '\n'; + + + + + + return 0; + +} diff --git a/mst/prim/38021c4f-a00a-4e85-b0e4-ae25b65f75d3.cpp b/mst/prim/38021c4f-a00a-4e85-b0e4-ae25b65f75d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14c34a6a21212a1eeee2d3a23fb6ca29a3c4cd7e --- /dev/null +++ b/mst/prim/38021c4f-a00a-4e85-b0e4-ae25b65f75d3.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim]; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt +#include +#include +#include +#include + +const int INF = 2e9; +const int NMAX = 2e5 + 1; + +std::ifstream in("apm.in"); +std::ofstream out("apm.out"); + +typedef std::pair > pip; +std::priority_queue , std::greater<> > heap; +std::vector > graf[NMAX]; +std::vector > sol; +bool viz[NMAX]; +int N, M; +int costTotal = 0; + +void Dijkstra (); + +int main() { + in >> N >> M; + int n1, n2, val; + for (int i = 1; i <= M; i++) { + in >> n1 >> n2 >> val; + graf[n1].push_back({n2, val}); + graf[n2].push_back({n1, val}); + } + + Dijkstra(); + out << costTotal << " " << N - 1 << "\n"; + for (auto elem : sol) + out << elem.first << " " << elem.second << "\n"; +} + +void Dijkstra () { + + int root = 1; + viz[root] = true; + for (auto elem : graf[root]) { + heap.push({elem.second,{root, elem.first}}); + } + + while (!heap.empty()) { + pip best = heap.top(); + heap.pop(); + + int cost = best.first; + int new_node = best.second.second; + + if (!viz[new_node]) { + viz[new_node] = true; + sol.push_back(best.second); + costTotal += cost; + + for (auto edge : graf[new_node]) { + if (!viz[edge.first]) + heap.push({edge.second, {new_node, edge.first}}); + } + } + } +} diff --git a/mst/prim/38700712-122e-459c-ad7d-304c249dce4d.cpp b/mst/prim/38700712-122e-459c-ad7d-304c249dce4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f770568f1681e40b502e12ce244d4ebcc05e3f8 --- /dev/null +++ b/mst/prim/38700712-122e-459c-ad7d-304c249dce4d.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +vector< vector< pair > > w; + +vector< pair >apm; + + + +priority_queue< pair >, vector< pair > >, greater< pair > > >h; + + + +int s,i; + + + +bool b[200001]; + + + +void prim (int x) + +{ + + for(i = 0; i < w[x].size(); i++) + + if(!b[w[x][i].first]) + + h.push(make_pair(w[x][i].second, make_pair(x, w[x][i].first))); + + + + b[x] = 1; + + + + while ( !h.empty() && b[h.top().second.second]) + + h.pop(); + + + + + + if(!h.empty()) + + { + + int val = h.top().first, xn = h.top().second.first, yn = h.top().second.second; + + h.pop(); + + + + apm.push_back(make_pair(xn,yn)); + + s += val; + + + + prim(yn); + + } + + + +} + + + +int main() + +{ + + int n, m, x, y, cost; + + + + in >> n >> m; + + + + w.resize(n+1); + + + + for(i = 0; i < m; i++) + + { + + in >> x >> y >> cost; + + + + w[x].push_back(make_pair(y,cost)); + + w[y].push_back(make_pair(x,cost)); + + } + + + + prim(1); + + + + out << s << '\n' << apm.size() << '\n'; + + + + for( auto elem =apm.begin(); elem != apm.end(); elem++) + + out << (*elem).first << " " << (*elem).second << '\n'; + +} diff --git a/mst/prim/397e7819-c787-4d81-8998-9e8e1f8ccb85.cpp b/mst/prim/397e7819-c787-4d81-8998-9e8e1f8ccb85.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61cbaa1a6eb1930ba391ff947d25b0d1832c42c6 --- /dev/null +++ b/mst/prim/397e7819-c787-4d81-8998-9e8e1f8ccb85.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +using namespace std; + +struct edge { + int from, to, cost; +}; + +struct comparator { + bool operator()(edge a, edge b) { + return a.cost > b.cost; + } +}; + +ifstream f("apm.in"); +ofstream g("apm.out"); + +int n, m, a, b, c; +bool viz[200001]; +vector > graph[200001]; +vector > rez; +priority_queue, comparator> q; + +void rezolvare() { + for (auto &v:graph[1]) + q.push({1, v.first, v.second}); + edge sus; + int cost = 0; + while (!q.empty()) { + sus = q.top(); + q.pop(); + if (!viz[sus.to]) { + cost += sus.cost; + rez.emplace_back(sus.from, sus.to); + viz[sus.from] = viz[sus.to] = 1; + for (auto &v:graph[sus.to]) { + if (!viz[v.first]) { + q.push({sus.to, v.first, v.second}); + } + } + } + } + g << cost << '\n' << n - 1 << '\n'; + for (auto &v:rez) + g << v.first << ' ' << v.second << '\n'; +} +void citire(){ + f >> n >> m; + for (int i = 1; i <= m; ++i) { + f >> a >> b >> c; + graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); + } +} + + +int main() { + citire(); + rezolvare(); + return 0; +} diff --git a/mst/prim/3a6de6f7-fa89-4afe-ac3a-b263203dff6e.cpp b/mst/prim/3a6de6f7-fa89-4afe-ac3a-b263203dff6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f78269cc93fd6ae7bc4b32674618679da168f08 --- /dev/null +++ b/mst/prim/3a6de6f7-fa89-4afe-ac3a-b263203dff6e.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +using namespace std; +ifstream cin("apm.in"); +ofstream cout("apm.out"); +int n ,m ; +set >> s; +vector > L[1000001],sol; +int costmin; +int ans,viz[1000001]; +int main() { + int x , y ,cost; + cin >> n >> m; + for(int i =1 ;i <=m; i ++) + { + cin >> x >> y >> cost; + L[x].push_back({y,cost}); + L[y].push_back({x,cost}); + } + viz[1] = 1; + for(auto i:L[1]) + s.insert({i.second,{1,i.first}}); + for(int i =2 ;i <=n ;i ++) + { + auto nod= s.begin(); + while(true){ + nod = s.begin(); + if(viz[nod ->second.second]) + s.erase(nod); + else + { + break; + } + } + sol.push_back(nod->second); + viz[nod->second.second] =1; + costmin +=nod->first; + for(auto j : L[nod ->second.second]) + { + s.insert({j.second, {nod ->second.second, j.first}}); + } + } + cout < +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >V; + +vector >v[200005]; + +int n, m, x, y, c, viz[200005]; + +struct { + + int x, y; + +} sol[200005]; + +int prim(int nod) + +{ + + int i, cost = 0, b = 0, j, nodaux; + + viz[nod] = 1; + + for (i = 1; i <= n - 1; i++) + + { + + for (j = 0; j < v[nod].size(); j++) + + { + + nodaux = v[nod][j].second; + + b = v[nod][j].first; + + if (!viz[nodaux]) + + V.push({ b,{nod,nodaux} }); + + } + + while (viz[V.top().second.second]) + + V.pop(); + + cost += V.top().first; + + viz[V.top().second.second] = 1; + + sol[i].x = V.top().second.first; + + sol[i].y = V.top().second.second; + + nod = V.top().second.second; + + V.pop(); + + } + + return cost; + +} + +int main() + +{ + + int i; + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + v[x].push_back({ c,y }); + + v[y].push_back({ c,x }); + + } + + g << prim(1) << '\n' << n - 1 << '\n'; + + for (i = 1; i < n; i++) + + g << sol[i].x << " " << sol[i].y << '\n'; + + return 0; + +} diff --git a/mst/prim/3ab44ce3-57d8-40a9-8cf6-dd8be5b0c13f.cpp b/mst/prim/3ab44ce3-57d8-40a9-8cf6-dd8be5b0c13f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df75a64013dd7323a39ff1a4b5fba3eed0c53f22 --- /dev/null +++ b/mst/prim/3ab44ce3-57d8-40a9-8cf6-dd8be5b0c13f.cpp @@ -0,0 +1,220 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +const int maxn = 200200; + +const int INF = 200000200; + + + +int VEC[maxn],ANS,V[maxn],H[maxn * 2 + 100],POZ[maxn]; + +vector > VECT[maxn],VANS; + +int N,M,L,C[maxn]; + + + +void introduce_in_apm(int x) + +{ + + for(unsigned int i = 0;i < VECT[x].size(); ++i) + + { + + int nod = VECT[x][i].first; + + int cost = VECT[x][i].second; + + V[nod] = min(V[nod],cost); + + if (V[nod] == cost) VEC[nod] = x; + + } + +} + + + +void push(int poz) + +{ + + while((poz * 2 <= L && V[H[poz]] > V[H[poz * 2]]) || (poz * 2 + 1 <= L && V[H[poz]] > V[H[poz * 2 + 1]])) + + { + + if (V[H[poz * 2]] < V[H[poz * 2 + 1]] || poz * 2 + 1 > L) + + { + + swap(H[poz],H[poz * 2]); + + swap(POZ[H[poz]],POZ[H[poz * 2]]); + + poz *= 2; + + } + + else + + { + + swap(H[poz],H[poz * 2 + 1]); + + swap(POZ[H[poz]],POZ[H[poz * 2 + 1]]); + + poz = poz * 2 + 1; + + } + + } + +} + + + + + +void pop(int poz) + +{ + + while(poz > 1 && V[H[poz]] < V[H[poz / 2]]) + + { + + swap(H[poz],H[poz / 2]); + + swap(POZ[H[poz]],POZ[H[poz / 2]]); + + poz = poz / 2; + + } + +} + + + +void introduce_in_heap(int nod) + +{ + + H[++L] = nod; + + POZ[nod] = L; + + pop(L); + +} + + + +int scoate_radacina_heap() + +{ + + int x = H[1]; + + swap(H[1],H[L]); + + swap(POZ[H[1]],POZ[H[L]]); + + L--; + + push(1); + + POZ[x] = 0; + + return x; + +} + + + +int main() + +{ + + freopen("apm.in","r",stdin); + + freopen("apm.out","w",stdout); + + scanf("%d %d\n",&N,&M); + + for(int i = 1;i <= M; ++i) + + { + + int x,y,c; + + scanf("%d %d %d",&x,&y,&c); + + VECT[x].push_back(make_pair(y,c)); + + VECT[y].push_back(make_pair(x,c)); + + } + + + + for(int i = 1;i <= N; ++i) V[i] = INF; + + V[1] = 0; + + introduce_in_apm(1); + + for(int i = 2;i <= N; ++i) + + { + + introduce_in_heap(i); + + } + + for(int i = 1;i < N; ++i) + + { + + int x = scoate_radacina_heap(); + + introduce_in_apm(x); + + ANS += V[x]; + + VANS.push_back(make_pair(x,VEC[x])); + + for(unsigned int j = 0;j < VECT[x].size(); ++j) + + { + + int nod = VECT[x][j].first; + + if (POZ[nod]) pop(POZ[nod]); + + } + + } + + printf("%d\n",ANS); + + printf("%d\n",N-1); + + for(unsigned int i = 0;i < N - 1; ++i) + + printf("%d %d\n",VANS[i].first,VANS[i].second); + + return 0; + +} diff --git a/mst/prim/3e27f1fa-7d67-40f0-a1c4-67e980f5ee69.cpp b/mst/prim/3e27f1fa-7d67-40f0-a1c4-67e980f5ee69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9dfc86a0b0e3873c56f40ab9435662b68cf1755f --- /dev/null +++ b/mst/prim/3e27f1fa-7d67-40f0-a1c4-67e980f5ee69.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int nmax = 2e5 + 1; + +int a[nmax]; + +struct Graf + +{ + + int x, y, cost; + +}; + +Graf v[nmax], rasp[nmax]; + +bool compare(Graf a, Graf b) + +{ + + return a.cost < b.cost; + +} + +int uwu(int i) + +{ + + if(a[i] == i) + + return i; + + return a[i] = uwu(a[i]); + +} + +int i; + +int main() + +{ + + int n, m, cnt = 0, k = 0; + + fin>> n >> m; + + for( i = 1; i <= m; ++i)fin>> v[i].x >> v[i].y >> v[i].cost; + + for( i = 1; i <= n; ++i)a[i]=i; + + sort(v + 1, v + m + 1, compare); + + for( i = 1; i <= m; ++i) + + { + + if(uwu(v[i].x) != uwu(v[i].y)) + + { + + a[uwu(v[i].y)] = v[i].x; + + cnt += v[i].cost; + + rasp[++k].x = v[i].x; + + rasp[k].y = v[i].y; + + } + + } + + fout< +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +constexpr int NX = 200010; + +constexpr int inf = 1010; + +int N, M, cost; + +int dist[NX], daddy[NX]; + +bool viz[NX]; + +typedef pair Pair; + +vector V[NX]; + +priority_queue < Pair, vector < Pair >, greater < Pair >> pq; + + + +int main() + +{ + + fin>>N>>M; + + int x, y, z; + + for(int i=1; i<=M; ++i) + + { + + fin>>x>>y>>z; + + V[x].push_back(make_pair(y, z)); + + V[y].push_back(make_pair(x, z)); + + } + + + + for(int i=1; i<=N; ++i) + + { + + dist[i]=inf; + + daddy[i]=-1; + + } + + + + + + pq.push(make_pair(0, 1)); + + dist[1] = 0; + + daddy[1] = 0; + + while(!pq.empty()) + + { + + int prim = pq.top().second; + + pq.pop(); + + if(viz[prim]==0) + + { + + viz[prim] = 1; + + cost+=dist[prim]; + + for(auto &i : V[prim]) + + { + + int nod=i.first; + + if(viz[nod]==0 && i.second < dist[nod]) + + { + + + + dist[nod]=i.second; + + pq.push(make_pair(dist[nod], nod)); + + daddy[nod]=prim; + + } + + + + } + + } + + } + + + + fout< +#include +#include +#include + + + + +using namespace std; + +vector >> graph; +vector > apm; +priority_queue >, vector > >, greater > >> heap; +vector visited; +int n; + +void read(){ + ifstream fin("apm.in"); + + int m, x, y, c; + fin>>n>>m; + graph.resize(n); + visited.resize(n, false); + while(m--){ + fin>>x>>y>>c; + graph[x-1].emplace_back(y-1, c); + graph[y-1].emplace_back(x-1, c); + } +} + +int main() { + ofstream fout("apm.out"); + read(); + int total_cost = 0; + for(auto &e: graph[0]) + heap.push({e.second, {0, e.first}}); + visited[0] = true; + while(!heap.empty()){ + auto top = heap.top(); + heap.pop(); + int cost = top.first; + int node = top.second.second; + if(!visited[node]){ + visited[node] = true; + apm.push_back(top.second); + total_cost += cost; + for(auto &e: graph[node]) + heap.push({e.second, {node, e.first}}); + } + } + fout< +#include +#include +#include + + + + +using namespace std; + + + +const int NMAX = 200005; + +const int INF = 1e9; + + + +int n, m; + +vector > graph[NMAX]; + + + +int main() { + + + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + int x, y, w; + + for (int i = 0; i < m; i++) { + + fin >> x >> y >> w; + + graph[x].emplace_back(y, w); + + graph[y].emplace_back(x, w); + + } + + + + + + priority_queue, vector >, greater<>> pq; + + int s = 1; + + vector dist(n + 1, INF); + + vector parent(n + 1, 0); + + vector viz(n, false); + + + + pq.push(make_pair(0, s)); + + dist[s] = 0; + + + + int cost = 0; + + + + pair pair; + + while (!pq.empty()) { + + pair = pq.top(); + + pq.pop(); + + x = pair.second; + + + + if (viz[x]) { + + continue; + + } + + + + cost += pair.first; + + viz[x] = true; + + + + for (auto & i : graph[x]) { + + y = i.first; + + w = i.second; + + + + if (!viz[y] && w < dist[y]) { + + dist[y] = w; + + pq.push(make_pair(dist[y], y)); + + parent[y] = x; + + } + + } + + } + + + + fout << cost << "\n" << n - 1 << "\n"; + + for (int i = 1; i <= n; i++) { + + if (i != s) + + fout << parent[i] << " " << i << "\n"; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/prim/3f8815b5-5d87-4754-9be1-65f29f31946d.cpp b/mst/prim/3f8815b5-5d87-4754-9be1-65f29f31946d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b7057b213f2d28825124debd9c30692fc054aa4 --- /dev/null +++ b/mst/prim/3f8815b5-5d87-4754-9be1-65f29f31946d.cpp @@ -0,0 +1,333 @@ +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +int tata[N]; + +bool vis[N]; + +int n, m; + + + + + +class MinHeap + +{ + + vector> heap; + + vector nodePosition; + + int lastIndex; + + + +public: + + MinHeap(int capacity) + + { + + lastIndex = 1; + + heap.resize(capacity + 1); + + nodePosition.resize(capacity + 1, -1); + + } + + + + void push(pair x) + + { + + heap[lastIndex++] = x; + + nodePosition[x.second] = lastIndex - 1; + + bubbleUp(lastIndex-1); + + } + + + + void pop() + + { + + nodePosition[heap[1].second] = -1; + + heap[1] = heap[--lastIndex]; + + nodePosition[heap[lastIndex].second] = 1; + + + + bubbleDown(1); + + } + + + + pair top() + + { + + return heap[1]; + + } + + + + int getNodeValue(int node) + + { + + int nodeIndex = nodePosition[node]; + + return heap[nodeIndex].first; + + } + + + + void updateNodeValue(int node, int newValue) + + { + + int nodeIndex = nodePosition[node]; + + heap[nodeIndex].first = newValue; + + bubbleUp(nodeIndex); + + } + + + + bool nodeExists(int node) + + { + + return nodePosition[node] != -1; + + } + + + + bool isEmpty() + + { + + return lastIndex == 1; + + } + + + +private: + + void bubbleUp(int i) + + { + + if(i == 1) + + return; + + + + if(heap[i/2] > heap[i]) + + { + + swapNode(i/2, i); + + bubbleUp(i/2); + + } + + } + + + + void bubbleDown(int i) + + { + + int st = i*2; + + int dr = st+1; + + + + if(dr > lastIndex) + + return; + + + + if(heap[st] <= heap[dr] && heap[st] < heap[i]) + + { + + swapNode(st, i); + + bubbleDown(st); + + } + + else if(heap[dr] <= heap[st] && heap[dr] < heap[i]) + + { + + swapNode(dr, i); + + bubbleDown(dr); + + } + + } + + + + void swapNode(int nodeIndex1, int nodeIndex2) + + { + + int node1 = heap[nodeIndex1].second; + + int node2 = heap[nodeIndex2].second; + + nodePosition[node1] = nodeIndex2; + + nodePosition[node2] = nodeIndex1; + + + + swap(heap[nodeIndex1], heap[nodeIndex2]); + + } + + + +}; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + MinHeap heap(n); + + heap.push({0, 1}); + + + + int costTotal = 0; + + while(!heap.isEmpty()) + + { + + int nod = heap.top().second; + + costTotal += heap.top().first; + + vis[nod] = true; + + heap.pop(); + + + + + + for(auto y : g[nod]) + + { + + if(vis[y.first]) + + continue; + + + + if(!heap.nodeExists(y.first)) + + { + + heap.push({y.second, y.first}); + + tata[y.first] = nod; + + } + + else if(y.second < heap.getNodeValue(y.first)) + + { + + heap.updateNodeValue(y.first, y.second); + + tata[y.first] = nod; + + } + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/402723c4-65c9-4fe6-bcc5-be583bb0b44b.cpp b/mst/prim/402723c4-65c9-4fe6-bcc5-be583bb0b44b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..084b3396d7b5ff204f23c60383299428d83494df --- /dev/null +++ b/mst/prim/402723c4-65c9-4fe6-bcc5-be583bb0b44b.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +vector> graf[200005]; + +int n,m,viz[200005],parent[200005],key[200005]; + +priority_queue,vector>,greater>> q; + +int main() + +{ + + in>>n>>m; + + for(int i=0;i>a>>b>>c; + + graf[a-1].push_back({b-1,c}); + + graf[b-1].push_back({a-1,c}); + + } + + q.push({0,0}); + + for(int i=0;i it.second) + + { + + key[it.first] = it.second; + + parent[it.first+1] = nod+1; + + q.push({it.second,it.first}); + + } + + } + + int sum=0; + + for(int i=1;i +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int INF = 2100000000; + + + +int n, m; + +vector > * lv; + + + +class Edge{ + + + +public: + + + + int first; + + int second; + + + + int price; + + + + Edge(int f, int s, int p): first(f), second(s), price(p) {}; + +}; + + + +int min_price; + +vector selected; + +int * pair_node; + + + +int * d; + +bool * v; + + + +int * q_pos; + +int * q; + +int q_dim; + + + +int pop(){ + + + + swap(q_pos[q[1]], q_pos[q[q_dim]]); + + swap(q[1], q[q_dim]); + + + + q_dim -= 1; + + + + bool ok = 0; + + + + int pos = 1; + + while(!ok) + + + + if(pos * 2 > q_dim && pos * 2 + 1 > q_dim) + + + + ok = true; + + + + else if(pos * 2 > q_dim){ + + + + if(d[q[pos * 2 + 1]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + else if(pos * 2 + 1 > q_dim){ + + + + if(d[q[pos * 2]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + } + + else{ + + + + if(d[q[pos]] < d[q[pos * 2]] && d[q[pos]] < d[q[pos * 2 + 1]]) + + + + ok = true; + + + + else if(d[q[pos * 2]] < d[q[pos * 2 + 1]]){ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + + + return q[q_dim + 1]; + +} + + + +void update(int node){ + + + + int pos = q_pos[node]; + + + + bool ok = 0; + + + + while(!ok) + + + + if(pos / 2 == 0 || d[q[pos]] > d[q[pos / 2]]) + + + + ok = true; + + else{ + + + + swap(q_pos[q[pos]], q_pos[q[pos / 2]]); + + swap(q[pos], q[pos / 2]); + + + + pos /= 2; + + } + +} + + + +void prim(){ + + + + while(q_dim){ + + + + int current_node = pop(); + + v[current_node] = 1; + + + + + + if(current_node != 1){ + + + + min_price += d[current_node]; + + selected.push_back(Edge(current_node, pair_node[current_node], d[current_node])); + + } + + + + for(int i = 0; i < lv[current_node].size(); i++){ + + + + int next_node = lv[current_node][i].first; + + int new_price = lv[current_node][i].second; + + + + if(!v[next_node] && d[next_node] > new_price){ + + + + d[next_node] = new_price; + + pair_node[next_node] = current_node; + + + + update(next_node); + + } + + } + + } + +} + + + +int main(){ + + + + f >> n >> m; + + + + lv = new vector > [n + 1]; + + + + d = new int [n + 1]; + + v = new bool [n + 1]; + + + + pair_node = new int [n + 1]; + + + + q = new int [n + 1]; + + q_pos = new int [n + 1]; + + q_dim = n; + + + + for(int i = 1 ; i <= n; i++){ + + + + d[i] = INF; + + v[i] = 0; + + + + q[i] = i; + + q_pos[i] = i; + + } + + + + int x, y, c; + + for(int i = 0; i < m ; i++){ + + + + f >> x >> y >> c; + + + + lv[x].push_back(make_pair(y, c)); + + lv[y].push_back(make_pair(x, c)); + + } + + + + prim(); + + + + g << min_price << '\n'; + + + + g << selected.size() << '\n'; + + for(int i = 0; i < selected.size(); i++) + + + + g << selected[i].first << " " << selected[i].second << '\n'; + + + + + + return 0; + +} diff --git a/mst/prim/40a9d196-7600-41fe-b23a-758d768595d8.cpp b/mst/prim/40a9d196-7600-41fe-b23a-758d768595d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..084b3396d7b5ff204f23c60383299428d83494df --- /dev/null +++ b/mst/prim/40a9d196-7600-41fe-b23a-758d768595d8.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +vector> graf[200005]; + +int n,m,viz[200005],parent[200005],key[200005]; + +priority_queue,vector>,greater>> q; + +int main() + +{ + + in>>n>>m; + + for(int i=0;i>a>>b>>c; + + graf[a-1].push_back({b-1,c}); + + graf[b-1].push_back({a-1,c}); + + } + + q.push({0,0}); + + for(int i=0;i it.second) + + { + + key[it.first] = it.second; + + parent[it.first+1] = nod+1; + + q.push({it.second,it.first}); + + } + + } + + int sum=0; + + for(int i=1;i +#include +#include + + + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muc{ + + int x; + + int y; + + int pret; + + + + bool operator<(const muc &other) const { + + if(pret != other.pret) + + return pret > other.pret; + + return y > other.y; + + } + + + +}; + + + + + +int n, m, s; + +int viz[200005]; + +muc fin[200005]; + +vector graph[200005]; + +priority_queue Q; + + + +void read() + +{ + + int x, y, p; + + + + f>>n>>m; + + for(int i=1; i<=m; ++i) + + { + + f>>x>>y>>p; + + graph[x].push_back({x, y, p}); + + graph[y].push_back({y, x, p}); + + } + +} + + + +void addToQ(int nod) + +{ + + for(auto &v:graph[nod]) + + Q.push(v); + +} + + + +void del_first() + +{ + + muc f = Q.top(); + + while(viz[f.y] == 1) + + { + + Q.pop(); + + f = Q.top(); + + } + +} + + + + + +void solve() + +{ + + int muchii_act = 0; + + addToQ(1); + + viz[1] = 1; + + while(muchii_act < n-1) + + { + + del_first(); + + muc f = Q.top(); + + s += f.pret; + + viz[f.y] = 1; + + fin[muchii_act ++] = f; + + addToQ(f.y); + + } + +} + + + +void write() + +{ + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +vector< vector< pair > > w; + +vector< pair >apm; + + + +priority_queue< pair >, vector< pair > >, greater< pair > > >h; + + + +int s,i; + + + +bool b[200001]; + + + +void prim (int x) + +{ + + for(i = 0; i < w[x].size(); i++) + + if(!b[w[x][i].first]) + + h.push(make_pair(w[x][i].second, make_pair(x, w[x][i].first))); + + + + b[x] = 1; + + + + while ( !h.empty() && b[h.top().second.second]) + + h.pop(); + + + + + + if(!h.empty()) + + { + + int val = h.top().first, xn = h.top().second.first, yn = h.top().second.second; + + h.pop(); + + + + apm.push_back(make_pair(xn,yn)); + + s += val; + + + + prim(yn); + + } + + + +} + + + +int main() + +{ + + int n, m, x, y, cost; + + + + in >> n >> m; + + + + w.resize(n+1); + + + + for(i = 0; i < m; i++) + + { + + in >> x >> y >> cost; + + + + w[x].push_back(make_pair(y,cost)); + + w[y].push_back(make_pair(x,cost)); + + } + + + + prim(1); + + + + out << s << '\n' << apm.size() << '\n'; + + + + for( auto elem =apm.begin(); elem != apm.end(); elem++) + + out << (*elem).first << " " << (*elem).second << '\n'; + +} diff --git a/mst/prim/433dd0d1-03ec-4a71-8100-ffa26f6b55d7.cpp b/mst/prim/433dd0d1-03ec-4a71-8100-ffa26f6b55d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46430ed5ebd9f12e6aeb3e95b433f5c4f462d766 --- /dev/null +++ b/mst/prim/433dd0d1-03ec-4a71-8100-ffa26f6b55d7.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +const int MAX_N = 200000; +const int MAX_M = 400000; +const int INF = 1000000000; + +bool marked[1+MAX_N]; + +struct Edge { + int a, b, c; + + int other(int x) { + return a ^ b ^ x; + } +} edges[1+MAX_M]; + +struct Node { + int nod, i; + bool operator< (const Node& x) const { + return edges[i].c < edges[x.i].c || + (edges[i].c == edges[x.i].c && nod < x.nod); + } +}; + +vector graph[1+MAX_N]; + +int bestEdge[1+MAX_N]; +set nodes; + +void markNode(int node) { + marked[node] = true; + for(auto it: graph[node]) { + int son = edges[it].other(node); + if(!marked[son] && edges[it].c < edges[bestEdge[son]].c) { + nodes.erase({son, bestEdge[son]}); + bestEdge[son] = it; + nodes.insert({son, bestEdge[son]}); + } + } + nodes.erase({node, bestEdge[node]}); +} + +int top; +int apmA[1+MAX_N], apmB[1+MAX_N]; + +int main() { + int N, M, sumapm = 0; + + FILE *fin = fopen("apm.in", "r"); + fscanf(fin, "%d%d", &N, &M); + + for(int i = 0; i < M; ++i) { + int a, b, c; + fscanf(fin, "%d%d%d", &a, &b, &c); + edges[i] = {a, b, c}; + graph[a].push_back(i); + graph[b].push_back(i); + } + edges[M] = {0, 0, INF}; + + for(int i = 1; i <= N; ++i) { + bestEdge[i] = M; + nodes.insert({i, bestEdge[i]}); + } + fclose(fin); + + markNode(1); + for(int i = 2; i <= N; ++i) { + int nod = nodes.begin()->nod, edge = nodes.begin()->i; + + sumapm += edges[edge].c; + apmA[top] = edges[edge].a; + apmB[top++] = edges[edge].b; + + markNode(nod); + } + + FILE *fout = fopen("apm.out", "w"); + fprintf(fout, "%d\n%d\n", sumapm, top); + for(int i = 0; i < top; ++i) + fprintf(fout, "%d %d\n", apmA[i], apmB[i]); + fclose(fout); + return 0; +} diff --git a/mst/prim/44d63303-7ba6-4948-9a0b-2bdd1fde8ce9.cpp b/mst/prim/44d63303-7ba6-4948-9a0b-2bdd1fde8ce9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6400557a89bfc448a0c59bb8fa2f8d3afafbd182 --- /dev/null +++ b/mst/prim/44d63303-7ba6-4948-9a0b-2bdd1fde8ce9.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >h; + +vector >v[200003]; + +vector >sol; + +int n,m,x,y,c; + +bool viz[200003]; + + + +int prim(int x) + +{ + + int i,j,cst=0,ct,nod; + + viz[x]=1; + + for(i=1; i<=n-1; i++) + + { + + for(j=0; j>n>>m; + + for(i=1; i<=m; i++) + + { + + f>>x>>y>>c; + + v[x].push_back({c,y}); + + v[y].push_back({c,x}); + + } + + g< +#include + + +using namespace std; + + + +ifstream in ("apm.in"); + +ofstream out ("apm.out"); + + + + + +vector>adiacenta[200000]; + +int n,m; + +int total = 0; + +priority_queue< pair, vector >, greater> > pq; + +vectorvizitat; + +vector parinte; + +vector cost; + + + + + +void Prim(int i) + +{ + + + + pq.push(make_pair(0, i)); + + cost[i] = 0; + + + + + + int varf; + + while (!pq.empty()) + + { + + varf = pq.top().second; + + vizitat[varf] = true; + + + + + + + + pq.pop(); + + + + for (pair e : adiacenta[varf]) + + { + + int vecin = e.first; + + int c = e.second; + + + + if (!vizitat[vecin] && c < cost[vecin]) + + { + + + + + + + + cost[vecin] = c; + + pq.push(make_pair(cost[vecin], vecin)); + + + + + + + + parinte[vecin] = varf; + + + + } + + + + } + + + + + + } + + + + + +} + + + + + +int main() + +{ + + + + + + in>>n>>m; + + int x,y,c; + + + + for(int i = 0; i>x>>y>>c; + + + + adiacenta[x-1].push_back(make_pair(y-1, c)); + + adiacenta[y-1].push_back(make_pair(x-1, c)); + + + + } + + + + vizitat.assign(n, false); + + cost.assign(n,10000); + + parinte.assign(n,-1); + + + + Prim(0); + + + + + + + + for(int i = 1; i + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m; + +int cost; + +bool ap[200005]; + +vector >nod[200005],ans; + +priority_queue > ,vector > >,greater > > >pq; + +void add_edges_of(int x) + +{ + + + + ap[x]=true; + + int y,c; + + for(int i=0;i>n>>m; + + int c,x,y; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + nod[x].push_back({y,c}); + + nod[y].push_back({x,c}); + + } + + compute_answer(); + + + + fout< +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +vector < pair >v[200010],vec; + +priority_queue< pair >,vector< pair > >,greater< pair > > >pq; + +pair > tt; + +int n,m,a,b,c; + +bool viz[200010]; + +long long cost; + +int main() + +{ + + fin>>n>>m; + + for(;m;m--) + + { + + fin>>a>>b>>c; + + v[a].push_back(make_pair(c,b)); + + v[b].push_back(make_pair(c,a)); + + } + + viz[1]=1; + + for(auto it:v[1]) + + { + + pq.push(make_pair(it.first,make_pair(1,it.second))); + + } + + + + for(int i=1;i<=n-1;i++) + + { + + while(viz[pq.top().second.second]) + + pq.pop(); + + tt=pq.top(); + + cost+=tt.first; + + vec.push_back(make_pair(tt.second.first,tt.second.second)); + + viz[tt.second.second]=1; + + for(auto it:v[tt.second.second]) + + { + + if(viz[it.second]==0) + + { + + pq.push(make_pair(it.first,make_pair(tt.second.second,it.second))); + + } + + } + + + + + + } + + fout< +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue>> pq; + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + used[1]=1; + + for(auto it:v[1]) + + { + + x=it.first; + + c=it.second; + + pq.push(make_pair(-c,make_pair(1,x))); + + } + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + c = -pq.top().first; + + x= pq.top().second.first; + + y= pq.top().second.second; + + pq.pop(); + + if(used[y]) + + continue; + + used[y]=1; + + cost+=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + for(auto it:v[y]) + + { + + x=it.first; + + c=it.second; + + if(!used[x]) + + pq.push(make_pair(-c,make_pair(y,x))); + + } + + + + } + + g< +#include +#include +using namespace std; +ifstream cin("apm.in"); +ofstream cout("apm.out"); +int n ,m ; +set >> s; +vector > L[1000001],sol; +int costmin; +int ans,viz[1000001]; +int main() { + int x , y ,cost; + cin >> n >> m; + for(int i =1 ;i <=m; i ++) + { + cin >> x >> y >> cost; + L[x].push_back({y,cost}); + L[y].push_back({x,cost}); + } + viz[1] = 1; + for(auto i:L[1]) + s.insert({i.second,{1,i.first}}); + for(int i =2 ;i <=n ;i ++) + { + auto nod= s.begin(); + while(true){ + nod = s.begin(); + if(viz[nod ->second.second]) + s.erase(nod); + else + { + break; + } + } + sol.push_back(nod->second); + viz[nod->second.second] =1; + costmin +=nod->first; + for(auto j : L[nod ->second.second]) + { + s.insert({j.second, {nod ->second.second, j.first}}); + } + } + cout < +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + + + +class MinHeap + +{ + + vector> heap; + + vector nodePosition; + + int lastIndex; + + + +public: + + MinHeap(int capacity) + + { + + lastIndex = 1; + + heap.resize(capacity + 1); + + nodePosition.resize(capacity + 1, -1); + + } + + + + void push(pair x) + + { + + heap[lastIndex++] = x; + + nodePosition[x.second] = lastIndex - 1; + + bubbleUp(lastIndex-1); + + } + + + + void pop() + + { + + nodePosition[heap[1].second] = -1; + + heap[1] = heap[--lastIndex]; + + nodePosition[heap[1].second] = 1; + + + + bubbleDown(1); + } + + + + pair top() + + { + + return heap[1]; + + } + + + + int getNodeValue(int node) + + { + + int nodeIndex = nodePosition[node]; + + return heap[nodeIndex].first; + + } + + + + + + void updateNodeValue(int node, int newValue) + + { + + int nodeIndex = nodePosition[node]; + + heap[nodeIndex].first = newValue; + + bubbleUp(nodeIndex); + + } + + + + bool nodeExists(int node) + + { + + return nodePosition[node] != -1; + + } + + + + bool isEmpty() + + { + + return lastIndex == 1; + + } + + + +private: + + void bubbleUp(int i) + + { + + if (i == 1) + + return; + + + + if (heap[i/2] > heap[i]) + + { + + + + + + + + swapNode(i / 2, i); + + bubbleUp(i / 2); + + } + + } + + + + void bubbleDown(int i) + + { + + int st = i*2; + + int dr = st+1; + + + + if (dr > lastIndex) + + return; + + + + if (heap[st] <= heap[dr] && heap[st] < heap[i]) + + { + + swapNode(st, i); + + bubbleDown(st); + + } + + else if (heap[dr] <= heap[st] && heap[dr] < heap[i]) + + { + + swapNode(dr, i); + + bubbleDown(dr); + + } + + } + + + + void swapNode(int nodeIndex1, int nodeIndex2) + + { + + + + + + int node1 = heap[nodeIndex1].second; + + int node2 = heap[nodeIndex2].second; + + nodePosition[node2] = nodeIndex1; + + nodePosition[node1] = nodeIndex2; + + + + swap(heap[nodeIndex1], heap[nodeIndex2]); + + } + + + +}; + + + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N]; + +int n, m; + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + MinHeap heap(n + 1); + + heap.push({0, 1}); + + + + int costTotal = 0; + + while(!heap.isEmpty()) + + { + + pair minim = heap.top(); + + int nod = minim.second; + + int cost = minim.first; + + heap.pop(); + + + + if(vis[nod]) + + continue; + + + + + + vis[nod] = true; + + costTotal += cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first]) + + { + + if(!heap.nodeExists(y.first)) + + { + + heap.push({y.second, y.first}); + + tata[y.first] = nod; + + } + + else if(y.second < heap.getNodeValue(y.first)) + + { + + heap.updateNodeValue(y.first, y.second); + + tata[y.first] = nod; + + } + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/4e38c12a-d579-4098-a7eb-2c42e7db99b6.cpp b/mst/prim/4e38c12a-d579-4098-a7eb-2c42e7db99b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f83f04a59a993c0585bdb19740a49c29033f6f0b --- /dev/null +++ b/mst/prim/4e38c12a-d579-4098-a7eb-2c42e7db99b6.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct mucie { + + int nod1, nod2; + +} muchii[200005]; + + + +struct node { + + int nod, cost; + +}; + + + +struct heapNode { + + int nod_init, nod_fin, cost; + + inline bool operator < (const heapNode &other) const + + { + + return cost > other.cost; + + } + +}; + + + +int nr_noduri, nr_muchii, total, nr_bagate; + +vector G[200005]; + +priority_queue heap; + +bool isIn[200005]; + + + +void citire() + +{ + + fin>>nr_noduri>>nr_muchii; + + for(int i=1; i<=nr_muchii; i++) + + { + + node nod1, nod2; + + fin>>nod1.nod>>nod2.nod>>nod1.cost; + + nod2.cost=nod1.cost; + + G[nod1.nod].push_back(nod2); + + G[nod2.nod].push_back(nod1); + + } + +} + + + +void apm_prim() + +{ + + isIn[1]=true; + + for(int i=0; i + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost,h[dim]; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim],Muchii; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +vector < edge > A; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + A.push_back({a,b,c}); + + } + + sort(A.begin(),A.end(), + + [&](edge X,edge Y) + + { + + return (X.val +#include +#include + + + +using namespace std; + +const int maxn = 200200; +const int INF = 200000200; + +int VEC[maxn],ANS,V[maxn],H[maxn * 2 + 100],POZ[maxn]; +vector > VECT[maxn],VANS; +int N,M,L,C[maxn]; + +void introduce_in_apm(int x) +{ + for(unsigned int i = 0;i < VECT[x].size(); ++i) + { + int nod = VECT[x][i].first; + int cost = VECT[x][i].second; + V[nod] = min(V[nod],cost); + if (V[nod] == cost) VEC[nod] = x; + } +} + +void push(int poz) +{ + while((poz * 2 <= L && V[H[poz]] > V[H[poz * 2]]) || (poz * 2 + 1 <= L && V[H[poz]] > V[H[poz * 2 + 1]])) + { + if (V[H[poz * 2]] < V[H[poz * 2 + 1]] || poz * 2 + 1 > L) + { + swap(H[poz],H[poz * 2]); + swap(POZ[H[poz]],POZ[H[poz * 2]]); + poz *= 2; + } + else + { + swap(H[poz],H[poz * 2 + 1]); + swap(POZ[H[poz]],POZ[H[poz * 2 + 1]]); + poz = poz * 2 + 1; + } + } +} + + +void pop(int poz) +{ + while(poz > 1 && V[H[poz]] < V[H[poz / 2]]) + { + swap(H[poz],H[poz / 2]); + swap(POZ[H[poz]],POZ[H[poz / 2]]); + poz = poz / 2; + } +} + +void introduce_in_heap(int nod) +{ + H[++L] = nod; + POZ[nod] = L; + pop(L); +} + +int scoate_radacina_heap() +{ + int x = H[1]; + swap(H[1],H[L]); + swap(POZ[H[1]],POZ[H[L]]); + L--; + push(1); + POZ[x] = 0; + return x; +} + +int main() +{ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + scanf("%d %d\n",&N,&M); + for(int i = 1;i <= M; ++i) + { + int x,y,c; + scanf("%d %d %d",&x,&y,&c); + VECT[x].push_back(make_pair(y,c)); + VECT[y].push_back(make_pair(x,c)); + } + + for(int i = 1;i <= N; ++i) V[i] = INF; + V[1] = 0; + introduce_in_apm(1); + for(int i = 2;i <= N; ++i) + { + introduce_in_heap(i); + } + for(int i = 1;i < N; ++i) + { + int x = scoate_radacina_heap(); + introduce_in_apm(x); + ANS += V[x]; + VANS.push_back(make_pair(x,VEC[x])); + for(unsigned int j = 0;j < VECT[x].size(); ++j) + { + int nod = VECT[x][j].first; + if (POZ[nod]) pop(POZ[nod]); + } + } + printf("%d\n",ANS); + printf("%d\n",N-1); + for(unsigned int i = 0;i < N - 1; ++i) + printf("%d %d\n",VANS[i].first,VANS[i].second); + return 0; +} diff --git a/mst/prim/4f351424-4c3d-4f4b-a087-f2a468d32988.cpp b/mst/prim/4f351424-4c3d-4f4b-a087-f2a468d32988.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df62ab39a7a4c7f6f3570a10bc0b597172be69d8 --- /dev/null +++ b/mst/prim/4f351424-4c3d-4f4b-a087-f2a468d32988.cpp @@ -0,0 +1,173 @@ +#include + + + + +using namespace std; + + + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +void read(); + +int n, m, t[200001], ct,viz[200001]; + +struct pereche + +{ + + int y,c; + +}; + + + +struct muchie + +{ + + int x, y, c; + +}; + + + +struct cmp + +{ + + bool operator()(muchie u, muchie v) + + { + + return u.c > v.c; + + } + +}; + + + + + +priority_queue, cmp> q; + + + +vector G[200001]; + + + +void citeste() + +{ + + int x,y,i,cost; + + fin >> n >> m; + + for(i=1; i<=m; i++) + + { + + pereche p; + + fin >> x >> y >> cost; + + p={y,cost}; + + G[x].push_back(p); + + p={x,cost}; + + G[y].push_back(p); + + } + +} + + + +int main() + +{ + + int i,nr; + + muchie e; + + pereche p; + + + + citeste(); + + for(i=0;i +#include +#include + + +using namespace std; + + + +const int nmax = 200000; + +const int mmax = 400000; + +const int INF = 2000000007; + + + +vector > g[nmax + 1], sol; + +int h[nmax + 1], u[nmax + 1]; + +int v[nmax + 1], last[nmax + 1]; + +int h_size; + +bool viz[nmax + 1]; + + + + + +void heap_swap(int a, int b){ + + swap(u[h[a]], u[h[b]]); + + swap(h[a], h[b]); + +} + + + +void heap_up(int p) { + + int f = p / 2; + + while (p > 1 && v[h[p]] < v[h[f]]) { + + heap_swap(p, f); + + p = p / 2; + + f = p / 2; + + } + +} + + + +void heap_down(int p) { + + int best, l, r; + + while (2 * p <= h_size) { + + l = 2 * p; + + best = l; + + + + r = 2 * p + 1; + + if (r <= h_size && v[h[r]] < v[h[l]]) { + + best = r; + + } + + + + if (v[h[p]] > v[h[best]]) { + + heap_swap(p, best); + + } else { + + break; + + } + + + + p = best; + + } + + + +} + + + +void heap_insert(int x) { + + ++h_size; + + h[h_size] = x; + + u[x] = h_size; + + + + heap_up(h_size); + +} + + + +void heap_erase(int x) { + + int p = u[x]; + + heap_swap(p, h_size); + + --h_size; + + + + heap_up(p); + + heap_down(p); + + + + u[x] = -1; + +} + + + +void heap_update(int x) { + + int p = u[x]; + + + + heap_up(p); + + heap_down(p); + +} + + + +int main() + +{ + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + int n, m, ans = 0, x, y, c; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; ++i) { + + scanf("%d%d%d", &x, &y, &c); + + g[x].push_back(make_pair(y, c)); + + g[y].push_back(make_pair(x, c)); + + } + + + + v[1] = 0; + + heap_insert(1); + + for (int i = 2; i <= n; ++i) { + + v[i] = INF; + + heap_insert(i); + + } + + + + for (int i = 1; i <= n; ++i) { + + x = h[1]; + + heap_erase(x); + + + + ans += v[x]; + + sol.push_back(make_pair(x, last[x])); + + + + for (int j = 0; j < g[x].size(); ++j) { + + y = g[x][j].first; + + c = g[x][j].second; + + if (u[y] != -1 && v[y] > c) { + + v[y] = c; + + last[y] = x; + + heap_update(y); + + } + + } + + + + } + + printf("%d\n", ans); + + printf("%d\n", n - 1); + + for (int i = 1; i < sol.size(); ++i) { + + printf("%d %d\n", sol[i].first, sol[i].second); + + } + + return 0; + +} diff --git a/mst/prim/5184b743-f136-45d4-a475-2b41b6fc5e28.cpp b/mst/prim/5184b743-f136-45d4-a475-2b41b6fc5e28.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9732258089e9781f2d039f571f9e8f22db01d9d --- /dev/null +++ b/mst/prim/5184b743-f136-45d4-a475-2b41b6fc5e28.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int Dim = 4e5 + 5; + +const int Inf = 1e9 + 5; + + + +int dist[Dim], t[Dim]; + +int n, m, costTotal; + + + +bool used[Dim]; + + + +vector > G[Dim], ans; + +priority_queue > coada; + + + +int main() { + + f >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y, cost; + + f >> x >> y >> cost; + + G[x].push_back({y, cost}); + + G[y].push_back({x, cost}); + + } + + + + for(int i = 1; i <= n; ++i) dist[i] = Inf; + + dist[1] = 0; + + + + coada.push({0, 1}); + + while(!coada.empty()) { + + int node = coada.top().second; + + coada.pop(); + + + + if(used[node] == false) { + + used[node] = true; + + costTotal += dist[node]; + + if(t[node]) ans.push_back({node, t[node]}); + + + + for(auto it = G[node].begin(); it != G[node].end(); ++it) { + + if(it -> second < dist[it -> first]) { + + dist[it -> first] = it -> second; + + t[it -> first] = node; + + coada.push({-it -> second, it -> first}); + + } + + } + + } + + } + + + + g << costTotal << '\n' << n - 1 << '\n'; + + for(auto it = ans.begin(); it != ans.end(); ++it) { + + g << it -> first << " " << it -> second << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/5188144b-14ce-4de7-9531-f94e30916921.cpp b/mst/prim/5188144b-14ce-4de7-9531-f94e30916921.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81516f9b6cb3f2ea26efc43ed002945bbbd1a3b3 --- /dev/null +++ b/mst/prim/5188144b-14ce-4de7-9531-f94e30916921.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + + + + +using namespace std; + +vector >> graph; +vector > apm; +priority_queue >, vector > >, greater > >> heap; +vector visited; +int n; + +void read(){ + ifstream fin("apm.in"); + + int m, x, y, c; + fin>>n>>m; + graph.resize(n); + visited.resize(n, false); + while(m--){ + fin>>x>>y>>c; + graph[x-1].emplace_back(y-1, c); + graph[y-1].emplace_back(x-1, c); + } +} + +int main() { + ofstream fout("apm.out"); + read(); + int total_cost = 0; + for(auto &e: graph[0]) + heap.push({e.second, {0, e.first}}); + visited[0] = true; + while(!heap.empty()){ + auto top = heap.top(); + heap.pop(); + int cost = top.first; + int node = top.second.second; + if(!visited[node]){ + visited[node] = true; + apm.push_back(top.second); + total_cost += cost; + for(auto &e: graph[node]) + heap.push({e.second, {node, e.first}}); + } + } + fout< +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int NMAX=200005,INF=0x3F3F3F3F; + +int n,m,x,y,price,cost[NMAX]; + +vector>edge[NMAX]; + +bitsetok; + +struct stalker + +{ + + int a,b; + +} solution[NMAX]; + +void prim(int n) + +{ + + int mn,node,k=0; + + long long answer=0; + + priority_queue>,vector>>,greater>>>pq; + + ok[1]=1; + + for(int i=1; i<=n; i++) + + cost[i]=INF; + + for(auto next: edge[1]) + + { + + cost[next.first]=next.second; + + pq.push({next.second,{next.first,1}}); + + } + + for(int i=1; i<=n-1; i++) + + { + + while(ok[pq.top().second.first]==1) + + pq.pop(); + + mn=pq.top().first; + + node=pq.top().second.first; + + answer+=mn; + + solution[++k].a=node; + + solution[k].b=pq.top().second.second; + + ok[node]=1; + + for(auto next:edge[node]) + + { + + cost[next.first]=min(cost[next.first],next.second); + + pq.push({next.second,{next.first,node}}); + + } + + } + + g<>n>>m; + + for(int i=0; i<=m; i++) + + { + + f>>x>>y>>price; + + edge[x].push_back({y,price}); + + edge[y].push_back({x,price}); + + } + + prim(n); + + return 0; + +} diff --git a/mst/prim/51df3bd7-a294-4978-883b-6f2174320385.cpp b/mst/prim/51df3bd7-a294-4978-883b-6f2174320385.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e4bc9ca1d89bc0c7212716d1b5d7d4fb18520c7 --- /dev/null +++ b/mst/prim/51df3bd7-a294-4978-883b-6f2174320385.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int N = 2e5; +const int M = 4e5; +const int INF = 2e9; + +struct struct_node { + int node, cost; + bool operator < ( const struct_node &other )const { + return cost < other.cost; + } +}; + +multiset dist; +vector edge[N]; +bitset used (false); +int min_cost_to[N+1]; +int pairs[N+1]; pair result[N]; + +void insert_apm ( int node ) { + used[node] = true; + for ( int i = 0; i < (int) edge[node].size (); i ++ ) { + int pay = edge[node][i].cost; + int adj = edge[node][i].node; + + if ( pay < min_cost_to[adj] ) { + min_cost_to[adj] = pay; + dist.insert ( edge[node][i] ); + pairs[adj] = node; + } + } +} + +void init_INF () { + for ( int i = 1; i <= N; i ++ ) + min_cost_to[i] = INF; +} + +ifstream fin ( "apm.in" ); +ofstream fout ( "apm.out" ); + + +int main() + +{ + + int n, m; fin >> n >> m; + for ( int i = 0; i < m; i ++ ) { + int x, y, pay; fin >> x >> y >> pay; + edge[x].push_back ( {y, pay} ); + edge[y].push_back ( {x, pay} ); + } + + init_INF (); + + insert_apm ( 1 ); int final_dist = 0; + for ( int i = 1; i < n; i ++ ) { + int get_node = dist.begin () -> node; + while ( used[get_node] == true ) { + dist.erase ( dist.begin () ); + get_node = dist.begin () -> node; + } + + dist.erase ( dist.begin () ); + insert_apm ( get_node ); + final_dist += min_cost_to[get_node]; + result[i-1].first = get_node; result[i-1].second = pairs[get_node]; + } + + fout << final_dist << '\n'; + fout << n - 1 << '\n'; + for ( int i = 0; i < n - 1; i ++ ) { + fout << result[i].first << ' ' << result[i].second; + fout << '\n'; + } + + + + + return 0; + +} diff --git a/mst/prim/527fd3af-65f2-4855-bc77-e52c67a829f0.cpp b/mst/prim/527fd3af-65f2-4855-bc77-e52c67a829f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..affce7aa063f33482cf55c5466bd69ac03be11fd --- /dev/null +++ b/mst/prim/527fd3af-65f2-4855-bc77-e52c67a829f0.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct muchie + +{ + + int cost; + + int elem; + +}; + +struct ura + +{ + + int ct; + + int nod1; + + int nod2; + +}; + +bool operator < (const ura& lhs, const ura& rhs) + +{ + + return (lhs.ct) < (rhs.ct); + +} + + + +vector> v; + +vector> raspuns; + +multiset costuri; + +bool viz[200001]; + +int main() + +{ + + int n, m, x, y, c, k, j, s; + + cin>>n>>m; + + v.resize(n+1); + + for(int i=1; i<=m; i++) + + { + + cin>>x>>y>>c; + + v[x].push_back({c, y}); + + v[y].push_back({c, x}); + + } + + s=0; + + k=n; + + j=1; + + viz[1]=true; + + while(k>1) + + { + + for(size_t i=0; i + + + + + + +using namespace std; + + + + + +typedef tuple edge; +int n, m, cost_apm, root; + +int dist[200010], parent[200010]; + +vector G[200010]; + +vector apm; + +priority_queue, std::greater> pq; + +bitset<200010> used; + + + +void read_input() { + + ifstream fin("apm.in"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= m; ++i) { + + int x, y, c; + + fin >> x >> y >> c; + + + + G[x].push_back(edge(c, y)); + + G[y].push_back(edge(c, x)); + + } + + + + fin.close(); + +} + + + +void Prim() { + + for (int i = 1; i <= n; ++i) { + + dist[i] = (1 << 30); + + parent[i] = 0; + + } + + + + root = 1; + + dist[root] = 0; + + parent[root] = 0; + + + + pq.push(edge(dist[root], root)); + + + + cost_apm = 0; + + + + for (int i = 1; i <= n; ++i) { + + std::tuple e; + + int node; + + + + do { + + e = pq.top(); + + pq.pop(); + + + + node = std::get<1>(e); + + } while (used[node]); + + + + cost_apm += dist[node]; + + used[node] = 1; + + + + for (auto it = G[node].begin(); it != G[node].end(); ++it) { + + int neighbour = std::get<1>(*it), cost = std::get<0>(*it); + + + + if (!used[neighbour] && cost < dist[neighbour]) { + + dist[neighbour] = cost; + + parent[neighbour] = node; + + pq.push(edge(dist[neighbour], neighbour)); + + } + + } + + } + +} + + + +void print_output() { + + ofstream fout("apm.out"); + + + + fout << cost_apm << "\n"; + + fout << (n - 1) << "\n"; + + + + for (int i = 1; i <= n; ++i) { + + if (i != root) { + + fout << i << " " << parent[i] << "\n"; + + } + + } + + + + fout.close(); + +} + + + + + + + +int main() { + + read_input(); + + Prim(); + + print_output(); + + + + return 0; + +} diff --git a/mst/prim/53f26f97-cd40-471d-b8d7-e4bdd0372ce7.cpp b/mst/prim/53f26f97-cd40-471d-b8d7-e4bdd0372ce7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..693ac599dd3ebd2bc98a61fe2067bddef28befb0 --- /dev/null +++ b/mst/prim/53f26f97-cd40-471d-b8d7-e4bdd0372ce7.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + priority_queue> heap; + + heap.push({0, 1}); + + for(int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while(!heap.empty()) + + { + + cout << heap.top().second << ' ' << heap.top().first << '\n'; + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if(vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal -= cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.push({-y.second, y.first}); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/541ae005-b935-4629-a3c3-daca63597332.cpp b/mst/prim/541ae005-b935-4629-a3c3-daca63597332.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b43b41c0cbc29892deb8e2fe799f5f9764459aad --- /dev/null +++ b/mst/prim/541ae005-b935-4629-a3c3-daca63597332.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int main() + +{ + + int n, m, x, y, c, s; + + fin >> n>> m; + + int d[n+1], t[n+1], viz[n+1]; + + vector < pair> g[n+1]; + + for(int i=1; i<=m; ++i) + + { + + fin >> x >>y>> c; + + g[x].push_back({y,c}); + + g[y].push_back({x,c}); + + } + + for(int i=1; i<=n;++i) + + { + + d[i] = INT_MAX; + + t[i] = 0; + + viz[i] = 0; + + } + + s = 1; + + d[s] = 0; + + + + priority_queue < pair > q; + + q.push({-d[s], s}); + + while(!q.empty()) + + { + + x=q.top().second; + + q.pop(); + + viz[x]=1; + + for(auto i: g[x]) + + { + + y=i.first; + + c=i.second; + + if(viz[y]==0 && d[y]>c) + + { + + t[y]=x; + + d[y]=c; + + q.push({-d[y], y}); + + } + + } + + } + + s=0; + + for(int i=1; i<=n; i++) s+=d[i]; + + fout< +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + + int N, M, a, b, c; + fin >> N >> M; + vector > g(N); + vector edges(M); + for(int i = 0; i < (int)(M); i++) { + fin >> a >> b >> c; + --a; + --b; + edges[i] = {a,b}; + g[a].push_back({c, i}); + g[b].push_back({c, i}); + } + + vector vis(N); + priority_queue, greater > pq; + int tc = 0; + vector tree; + bool start = true; + + pq.push({0, 0}); + while(!pq.empty()) { + auto P = pq.top(); pq.pop(); + auto e = edges[P.second]; + if (vis[e.first] && vis[e.second]) { + continue; + } + if (start) { + start = false; + } else { + tc += P.first; + tree.push_back(P.second); + } + int x = vis[e.first] ? e.second : e.first; + vis[x] = 1; + for(auto const &Q: g[x]) { + auto ne = edges[Q.second]; + int y = x ^ ne.first ^ ne.second; + if (!vis[y]) { + pq.push(Q); + } + } + } + + fout << tc << "\n"; + fout << tree.size() << "\n"; + for(auto i: tree) { + auto e = edges[i]; + fout << e.first + 1 << " " << e.second + 1 << "\n"; + } + + return 0; +} diff --git a/mst/prim/55d0e491-8609-4613-b5dd-818b0f4e739f.cpp b/mst/prim/55d0e491-8609-4613-b5dd-818b0f4e739f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b23e450bf44d6456251361024a62aa46ad3657d --- /dev/null +++ b/mst/prim/55d0e491-8609-4613-b5dd-818b0f4e739f.cpp @@ -0,0 +1,451 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 400000000; + +const int MAX_HEAP_SIZE=200001; + +typedef int heap[MAX_HEAP_SIZE]; + + + +heap h; + + + +struct nod + +{ + + int info, cost, sursa; + + nod * urm; + +}*vecin[200001]; + + + +nod * prim[200001]; + +nod * ultim[200001]; + +int v[200001], p[200001]; + + + +struct nod2 + +{ + + nod * info; + + nod2 * urm; + +}; + + + +inline int father (int x) + +{ + + return x/2; + +} + + + +inline int left_son (int x) + +{ + + return x*2; + +} + + + +inline int right_son(int x) + +{ + + return x*2 + 1; + +} + + + +void sift(heap h, int n, int k) + +{ + + int son; + + do + + { + + son=0; + + + + if(left_son(k)<=n) + + { + + son=left_son(k); + + + + if(right_son(k)<=n && v[ h[right_son(k)] ] < v[ h[son] ] ) + + { + + son=right_son(k); + + } + + + + if(v[ h[k] ] <= v[ h[son] ]) + + { + + son=0; + + } + + } + + + + if(son) + + { + + swap(p[ h[k] ], p[ h[son] ]); + + swap(h[k], h[son]); + + k=son; + + } + + + + }while(son); + +} + + + +void percolate (heap h, int n, int k) + +{ + + int key=h[k]; + + + + while(k>1 && v[key] < v[ h[father(k)] ]) + + { + + p[ h[father(k)] ]=k; + + h[k]=h[father(k)]; + + k=father(k); + + } + + + + p[key]=k; + + h[k]=key; + +} + + + +int main() + +{ + + int n, m, i, a, b, c, l; + + fin>>n>>m; + + + + for(i=1; i<=m; i++) + + { + + fin>>a>>b>>c; + + + + + + nod * aux1= new nod; + + aux1->info=b; + + aux1->cost=c; + + aux1->sursa=a; + + aux1->urm=NULL; + + + + if(prim[a]==NULL) + + { + + prim[a]=aux1; + + vecin[a]=aux1; + + } + + + + else + + { + + vecin[a]->urm=aux1; + + vecin[a]=aux1; + + } + + + + nod * aux2 = new nod; + + aux2->info=a; + + aux2->cost=c; + + aux2->sursa=b; + + aux2->urm=NULL; + + + + if(prim[b]==NULL) + + { + + prim[b]=aux2; + + vecin[b]=aux2; + + } + + + + else + + { + + vecin[b]->urm=aux2; + + vecin[b]=aux2; + + } + + } + + + + + + l=n-1; + + for(i=2; i<=n; i++) + + { + + v[i]=INFINIT; + + h[i-1]=i; + + p[i]=i-1; + + } + + + + v[1]=0; + + p[1]=n; + + nod * t = prim[1]; + + + + while(t!=NULL) + + { + + v[t->info] = t->cost; + + percolate(h, l, p[t->info]); + + ultim[t->info] = t; + + + + t=t->urm; + + } + + + + nod * prim_lista = NULL; + + nod * lista = NULL; + + + + for(i=1; iurm=NULL; + + afis->info = ultim[x]->sursa; + + afis->sursa= ultim[x]->info; + + afis->cost=ultim[x]->cost; + + + + if(prim_lista==NULL) + + { + + prim_lista=afis; + + lista=afis; + + } + + else + + { + + lista->urm=afis; + + lista=afis; + + } + + + + nod * t = prim[x]; + + + + while(t!=NULL) + + { + + if(p[t->info]<=l && t->cost < v[t->info]) + + { + + v[t->info] = t->cost; + + percolate(h, l, p[t->info]); + + ultim[t->info] = t; + + } + + + + t=t->urm; + + } + + } + + + + nod * aux3= prim_lista; + + int suma=0; + + + + while(aux3!=NULL) + + { + + suma=suma+aux3->cost; + + aux3=aux3->urm; + + } + + + + fout<info<<' '<sursa<<"\n"; + + aux3=aux3->urm; + + } + + + + + + + +} diff --git a/mst/prim/5616ccf3-fa26-49d4-a120-35ab9f986110.cpp b/mst/prim/5616ccf3-fa26-49d4-a120-35ab9f986110.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0ecf1a746d293d81c3c2c69e62d2c6ebafc9f1 --- /dev/null +++ b/mst/prim/5616ccf3-fa26-49d4-a120-35ab9f986110.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +ifstream fin("apm.in");ofstream fout("apm.out"); + +vector > >a[200005]; + +vector > >::iterator it; + +pair >var; + +pair >var2; + +vector >sol; + +vector >::iterator it2; + +pairvarsol; + +priority_queue >,vector > >,greater > > >q; + +int n,m,x,y,co,nrviz,cst,viz[200005],t[200005],nrm; + +void citire() + +{ + + fin>>n>>m; + + while(fin>>x>>y>>co) + + { + + var.first=co; + + var.second.first=y; + + var.second.second=x; + + a[x].push_back(var); + + var.first=co; + + var.second.first=x; + + var.second.second=y; + + a[y].push_back(var); + + } + +} + +int main() + +{ + + citire(); + + for(it=a[1].begin();itfirst; + + var.second.first=it->second.first; + + var.second.second=it->second.second; + + q.push(var); + + } + + viz[1]=1;t[1]=0;nrviz+=1; + + while(nrvizfirst; + + var2.second.first=it->second.first; + + var2.second.second=it->second.second; + + q.push(var2); + + } + + } + + } + + fout<first<<" "<second<<'\n'; + + } + + fin.close();fout.close();return 0; + +} diff --git a/mst/prim/56ea102d-4259-4b71-9b6f-848553bcc927.cpp b/mst/prim/56ea102d-4259-4b71-9b6f-848553bcc927.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1180256b78800b7a4a7a8cb52cf795906a95473 --- /dev/null +++ b/mst/prim/56ea102d-4259-4b71-9b6f-848553bcc927.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream o("apm.out"); + +typedef pair pii; + +vector adj[200001]; + +bitset<200001> APM; + +int n, m; + +void Prim() + +{ + + int x; + + priority_queue, greater > q; + + vector cost(n + 1, 1000001); + + vector t(n + 1, 0); + + q.push({ 0,1 }); + + cost[1] = 0; + + t[1] = 0; + + while (!q.empty()) + + { + + x = q.top().second; + + q.pop(); + + APM[x] = true; + + for (auto i : adj[x]) + + { + + if (!APM[i.first] && cost[i.first] > i.second) + + { + + cost[i.first] = i.second; + + q.push({ cost[i.first], i.first }); + + t[i.first] = x; + + } + + } + + } + + int val = 0; + + for (int i = 1; i <= n; i++) + + val += cost[i]; + + o << val<> n >> m; + + for(int i=1;i<=m;i++) + + { + + f >> x >> y >> c; + + adj[x].push_back({ y,c }); + + adj[y].push_back({ x, c }); + + } + + Prim(); + + return 0; + +} diff --git a/mst/prim/575c73b8-f86e-42f8-90d2-de59dd7c4042.cpp b/mst/prim/575c73b8-f86e-42f8-90d2-de59dd7c4042.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ae47f15beec0e1c27093e307db17b58dafec00a --- /dev/null +++ b/mst/prim/575c73b8-f86e-42f8-90d2-de59dd7c4042.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct edg{ + + int a, b, v; + + bool operator<(const edg &rhs)const{ + + if(v != rhs.v){ + + return v > rhs.v; + + }else if(b != rhs.b){ + + return b > rhs.b; + + }else{ + + return a > rhs.a; + + } + + } + + int ott(int w){ + + if(w == a){ + + return b; + + }else if(w == b){ + + return a; + + }else{ + + return 0; + + } + + } + +}; + + + +const int N = 200041; + + + +int n, m; + +vector edgs; + +vector gra[N]; + +bool vi[N]; + + + +int skibidibapmdada(const edg &ed){ + + if(!vi[ed.a]){ + + return ed.a; + + }else if(!vi[ed.b]){ + + return ed.b; + + }else{ + + return 0; + + } + +} + + + +priority_queue pq; + +void addme(int a){ + + vi[a] = true; + + for(auto edi : gra[a]){ + + edg &ed = edgs[edi]; + + if(!vi[ed.ott(a)]){ + + pq.push(ed); + + } + + } + +} + + + +int cost = 0; + +vector naisu; + + + +int main(){ + + + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + edg ed; + + fin >> ed.a >> ed.b >> ed.v; + + edgs.push_back(ed); + + gra[ed.a].push_back(i); + + gra[ed.b].push_back(i); + + } + + + + addme(1); + + while(!pq.empty()){ + + edg ed = pq.top(); + + pq.pop(); + + int w = skibidibapmdada(ed); + + if(w != 0){ + + cost += ed.v; + + naisu.push_back(ed); + + addme(w); + + } + + } + + + + fout << cost << "\n"; + + fout << naisu.size() << "\n"; + + for(auto ed : naisu){ + + fout << ed.a << " " << ed.b << "\n"; + + } + + + + return 0; + +} diff --git a/mst/prim/5783c712-6692-4548-8ace-a0b95d9c42e5.cpp b/mst/prim/5783c712-6692-4548-8ace-a0b95d9c42e5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee709f73bb634f617e9596c468eb63ad4d0c0153 --- /dev/null +++ b/mst/prim/5783c712-6692-4548-8ace-a0b95d9c42e5.cpp @@ -0,0 +1,352 @@ +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + + + +class MinHeap + +{ + + vector> heap; + + vector nodePosition; + + int lastIndex; + + + +public: + + MinHeap(int capacity) + + { + + lastIndex = 1; + + heap.resize(capacity + 1); + + nodePosition.resize(capacity + 1, -1); + + } + + + + void push(pair x) + + { + + heap[lastIndex++] = x; + + nodePosition[x.second] = lastIndex - 1; + + bubbleUp(lastIndex-1); + + } + + + + void pop() + + { + + nodePosition[heap[1].second] = -1; + + heap[1] = heap[--lastIndex]; + + nodePosition[heap[1].second] = 1; + + + + bubbleDown(1); + } + + + + pair top() + + { + + return heap[1]; + + } + + + + int getNodeValue(int node) + + { + + int nodeIndex = nodePosition[node]; + + return heap[nodeIndex].first; + + } + + + + + + void updateNodeValue(int node, int newValue) + + { + + int nodeIndex = nodePosition[node]; + + heap[nodeIndex].first = newValue; + + bubbleUp(nodeIndex); + + } + + + + bool nodeExists(int node) + + { + + return nodePosition[node] != -1; + + } + + + + bool isEmpty() + + { + + return lastIndex == 1; + + } + + + +private: + + void bubbleUp(int i) + + { + + if (i == 1) + + return; + + + + if (heap[i/2] > heap[i]) + + { + + + + + + + + swapNode(i / 2, i); + + bubbleUp(i / 2); + + } + + } + + + + void bubbleDown(int i) + + { + + int st = i*2; + + int dr = st+1; + + + + if (dr > lastIndex) + + return; + + + + if (heap[st] <= heap[dr] && heap[st] < heap[i]) + + { + + swapNode(st, i); + + bubbleDown(st); + + } + + else if (heap[dr] <= heap[st] && heap[dr] < heap[i]) + + { + + swapNode(dr, i); + + bubbleDown(dr); + + } + + } + + + + void swapNode(int nodeIndex1, int nodeIndex2) + + { + + + + + + int node1 = heap[nodeIndex1].second; + + int node2 = heap[nodeIndex2].second; + + nodePosition[node2] = nodeIndex1; + + nodePosition[node1] = nodeIndex2; + + + + swap(heap[nodeIndex1], heap[nodeIndex2]); + + } + + + +}; + + + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N]; + +int n, m; + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + MinHeap heap(n + 1); + + heap.push({0, 1}); + + + + int costTotal = 0; + + while(!heap.isEmpty()) + + { + + pair minim = heap.top(); + + int nod = minim.second; + + int cost = minim.first; + + heap.pop(); + + + + if(vis[nod]) + + continue; + + + + + + vis[nod] = true; + + costTotal += cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first]) + + { + + if(!heap.nodeExists(y.first)) + + { + + heap.push({y.second, y.first}); + + tata[y.first] = nod; + + } + + else if(y.second < heap.getNodeValue(y.first)) + + { + + heap.updateNodeValue(y.first, y.second); + + tata[y.first] = nod; + + } + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/58a32058-fda2-4333-9dc5-fcc0e71d9f6b.cpp b/mst/prim/58a32058-fda2-4333-9dc5-fcc0e71d9f6b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..809f1c1d3cbf2431246d38717814c94f63bb3d58 --- /dev/null +++ b/mst/prim/58a32058-fda2-4333-9dc5-fcc0e71d9f6b.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m; + +int x, y, c, sum; + +int viz[200005], nrmuchii; + + + + + +struct muchie{ + + int x; + + int y; + + int cost; + + + + bool operator<(const muchie &other) const { + + if(cost!=other.cost) + + return cost>other.cost; + + return y>other.y; + + } + +}; + +muchie sol[200005]; + +vector graph[200005]; + +priority_queue Q; + + + +void solve(){ + + for(int i=2; i<=n; i++){ + + muchie minima = Q.top(); + + while(viz[minima.y] == 1){ + + Q.pop(); + + minima = Q.top(); + + } + + + + sol[nrmuchii++] = {minima.x, minima.y}; + + viz[minima.y]=1; + + + + sum =sum + minima.cost; + + for(auto &v:graph[minima.y]) + + Q.push(v); + + } + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++){ + + f>>x>>y>>c; + + graph[x].push_back({x, y, c}); + + graph[y].push_back({y, x, c}); + + } + + for(auto &v:graph[1]) + + Q.push(v); + + viz[1] = 1; + + solve(); + + g< +using namespace std; + + + + + +typedef vector vi; +typedef vector vvi; +typedef pair ii; +typedef vector vii; +typedef long long ll; +typedef vector vll; +typedef vector vvll; +typedef double ld; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +int n, m; +vii G[200010]; +vector visited; +int dist[200010]; +int p[200010]; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + + fin >> n >> m; + for (int i = 0; i < m; i++) { + int x, y, c; + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, c)); + } + + for (int i = 1; i <= n; i++) { + visited.push_back(0); + dist[i] = INT_MAX; + } + + int cost_total = 0; + set s; + dist[1] = 0; + s.insert(make_pair(0, 1)); + p[1] = 0; + for (int i = 1; i <= n; i++) { + int node = s.begin()->second; + int d = s.begin()->first; + s.erase(s.begin()); + visited[node - 1] = 1; + cost_total += d; + + for (auto &it : G[node]) { + int vecin = it.first; + int cost = it.second; + if (!visited[vecin - 1] && dist[vecin] > cost) { + if (dist[vecin] != INT_MAX) { + s.erase(s.find(make_pair(dist[vecin], vecin))); + } + dist[vecin] = cost; + s.insert(make_pair(dist[vecin], vecin)); + p[vecin] = node; + } + } + } + + fout << cost_total << "\n" << n - 1 << "\n"; + + for (int i = 2; i <= n; i++) { + fout << i << " " << p[i] << "\n"; + } + + return 0; +} diff --git a/mst/prim/5a7fd024-d22e-407f-845f-8ce83b0107e9.cpp b/mst/prim/5a7fd024-d22e-407f-845f-8ce83b0107e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0ecf1a746d293d81c3c2c69e62d2c6ebafc9f1 --- /dev/null +++ b/mst/prim/5a7fd024-d22e-407f-845f-8ce83b0107e9.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +ifstream fin("apm.in");ofstream fout("apm.out"); + +vector > >a[200005]; + +vector > >::iterator it; + +pair >var; + +pair >var2; + +vector >sol; + +vector >::iterator it2; + +pairvarsol; + +priority_queue >,vector > >,greater > > >q; + +int n,m,x,y,co,nrviz,cst,viz[200005],t[200005],nrm; + +void citire() + +{ + + fin>>n>>m; + + while(fin>>x>>y>>co) + + { + + var.first=co; + + var.second.first=y; + + var.second.second=x; + + a[x].push_back(var); + + var.first=co; + + var.second.first=x; + + var.second.second=y; + + a[y].push_back(var); + + } + +} + +int main() + +{ + + citire(); + + for(it=a[1].begin();itfirst; + + var.second.first=it->second.first; + + var.second.second=it->second.second; + + q.push(var); + + } + + viz[1]=1;t[1]=0;nrviz+=1; + + while(nrvizfirst; + + var2.second.first=it->second.first; + + var2.second.second=it->second.second; + + q.push(var2); + + } + + } + + } + + fout<first<<" "<second<<'\n'; + + } + + fin.close();fout.close();return 0; + +} diff --git a/mst/prim/5b76c3f3-7e3c-4b0e-ac3e-97ef2ae8e6b0.cpp b/mst/prim/5b76c3f3-7e3c-4b0e-ac3e-97ef2ae8e6b0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a10baa9d179847df393285cadf9871888d220541 --- /dev/null +++ b/mst/prim/5b76c3f3-7e3c-4b0e-ac3e-97ef2ae8e6b0.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int NMAX = 200005; + +vector < pair > v[NMAX],rasp; + +priority_queue < pair < int , pair > , vector < pair < int,pair > > , greater < pair < int, pair > > > q; + +bool fixat[NMAX]; + +int n,m,x,y,c,cost; + +int main() + +{ + + fin >> n >> m; + + for(int i=1;i<=m;i++){ + + fin >> x >> y >> c; + + v[x].push_back({y,c}); + + v[y].push_back({x,c}); + + } + + q.push({0,{0,1}}); + + while(!q.empty()){ + + pair < int , pair > aux=q.top(); + + q.pop(); + + int xx=aux.second.first,yy=aux.second.second; + + if(fixat[yy]==true) continue; + + fixat[yy]=true; + + rasp.push_back({xx,yy}); + + cost+=aux.first; + + for(int i=0;i +#include +#include +#include +#include +using namespace std; +int N,M; +vector< pair > V[200002]; +auto compare = [](pair a, pair b) { return a.second > b.second; }; + +priority_queue < pair, vector >, decltype(compare) > pq(compare); +int p[200002], d[200002]; +bool visited[200002]; +vector > ANS; +priority_queue pq2; +int cost = 0; +int main() +{ + ifstream f1("apm.in"); + ofstream f2("apm.out"); + f1>>N>>M; + int x,y,z; + for (int i=0; i>x>>y>>z; + V[x].push_back(make_pair(y,z)); + V[y].push_back(make_pair(x,z)); + } + for (int i=1; i<=N; i++) + d[i] = 1<<20; + d[1] = 0; + pq.push(make_pair(1,0)); + while (!pq.empty()) + { + int nod = pq.top().first; + + if (visited[nod]) + { + pq.pop(); + continue; + } + visited[nod] = 1; + cost+= pq.top().second; + + pq.pop(); + if (nod != 1) + ANS.push_back(make_pair(p[nod], nod)); + for (int i=0; i V[nod][i].second) + { + d[V[nod][i].first] = V[nod][i].second; + p[V[nod][i].first] = nod; + pq.push(make_pair(V[nod][i].first,d[V[nod][i].first])); + + } + } + } + f2< +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = (int)vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + +} + + + + + + + +std::vector vecini[N]; + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + Muchie m; + + in >> m; + + vecini[m.a].push_back({m.b, m.cost}); + + vecini[m.b].push_back({m.a, m.cost}); + + } + + dj::reset(n); + + apm::prim(vecini, 0); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/5d07cd9c-cd8b-491a-b17a-ca58860e13da.cpp b/mst/prim/5d07cd9c-cd8b-491a-b17a-ca58860e13da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a60e2decd3a8c04a84f27ff69d69f1060841ab01 --- /dev/null +++ b/mst/prim/5d07cd9c-cd8b-491a-b17a-ca58860e13da.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int n,m,c,p[200001],v[200001]; + +struct muchie + +{ int c,x,y; + +}; + +bool operator <(muchie a,muchie b) + +{ return a.c>b.c; + +} + +priority_queue h; + +vector < pair > r; + +int main() + +{ in>>n>>m; + + for(int i=1;i<=m;i++) + + { muchie a; + + in>>a.x>>a.y>>a.c; + + h.push(a); + + } + + while(!h.empty()) + + { muchie a=h.top(); + + pair x=make_pair(h.top().x,h.top().y); + + h.pop(); + + while(p[a.x]!=0) + + a.x=p[a.x]; + + while(p[a.y]!=0) + + a.y=p[a.y]; + + if(a.x==a.y) + + continue; + + if(v[a.x] +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +vector > v[400001]; + +vector> vrez; + +int used[200001],cost[200001],cor[200001]; + +priority_queue > pq; + +int intro_apm(int nod){ + + int i,l=v[nod].size(); + + for(i=0;i-(v[nod][i].first)) + + { + + pq.push({v[nod][i]}); + + cost[v[nod][i].second]=-(v[nod][i].first); + + cor[v[nod][i].second]=nod; + + } + + } + +} + +int main() + +{ + + int n,m,i,a,b,c,costf=0,l,x; + + cin>>n>>m; + + for(i=1;i<=m;i++) + + { + + cin>>a>>b>>c; + + v[a].push_back({-c,b}); + + v[b].push_back({-c,a}); + + } + + for(i=2;i<=n;i++) + + { + + cost[i]=1e9; + + pq.push({-cost[i],i}); + + x=pq.top().second; + + } + + intro_apm(1); + + used[1]=1; + + for(i=1;i +#include +#include + + + + + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muc{ + + int x; + + int y; + + int pret; + + + + bool operator<(const muc &other) const { + + if(pret != other.pret) + + return pret > other.pret; + + return y > other.y; + + } + + + +}; + + + + + +int n, m, s; + +int viz[200005]; + +muc fin[200005]; + +vector graph[200005]; + +priority_queue Q; + + + +void read() + +{ + + int x, y, p; + + + + f>>n>>m; + + for(int i=1; i<=m; ++i) + + { + + f>>x>>y>>p; + + graph[x].push_back({x, y, p}); + + graph[y].push_back({y, x, p}); + + } + +} + + + +void addToQ(int nod) + +{ + + for(auto &v:graph[nod]) + + Q.push(v); + +} + + + +void del_first() + +{ + + muc f = Q.top(); + + while(viz[f.y] == 1) + + { + + Q.pop(); + + f = Q.top(); + + } + +} + + + + + +void solve() + +{ + + int muchii_act = 0; + + addToQ(1); + + viz[1] = 1; + + while(muchii_act < n-1) + + { + + del_first(); + + muc f = Q.top(); + + s += f.pret; + + viz[f.y] = 1; + + fin[muchii_act ++] = f; + + addToQ(f.y); + + } + +} + + + +void write() + +{ + + g< + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +pair h[400001]; + + + +vector < pair > g[200001]; + + + +int n,cost[200001],fr[200001],parent[200001]; + + + +int father(int x) + +{ + + return x/2; + +} + + + +int lson(int x) + +{ + + return 2*x; + +} + + + +int rson(int x) + +{ + + return 2*x+1; + +} + + + +void sift(int k) + +{ + + int son=lson(k); + + while(son!=0) + + { + + son=lson(k); + + if(son<=n) + + { + + if(son+1<=n && h[son+1].first aux=h[k]; + + while(k>1 && h[father(k)].first>aux.first) + + { + + h[k]=h[father(k)]; + + k=father(k); + + } + + h[k]=aux; + +} + + + +void cut(int k) + +{ + + h[k]=h[n]; + + n--; + + if(k>1 && h[k].first val) + +{ + + h[++n]=val; + + percolate(n); + +} + + + +int main() + +{ + + int p,m,i,j,x,y,c,s=0; + + in>>p>>m; + + for(i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + g[x].push_back({y,c}); + + g[y].push_back({x,c}); + + cost[i]=1e9; + + } + + cost[1]=0; + + h_insert({0,1}); + + while(n>0) + + { + + x=h[1].second; + + cut(1); + + if(fr[x]==0) + + { + + fr[x]=1; + + for(i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct edg{ + + int a, b, v; + + bool operator<(const edg &rhs)const{ + + if(v != rhs.v){ + + return v > rhs.v; + + }else if(b != rhs.b){ + + return b > rhs.b; + + }else{ + + return a > rhs.a; + + } + + } + + int ott(int w){ + + if(w == a){ + + return b; + + }else if(w == b){ + + return a; + + }else{ + + return 0; + + } + + } + +}; + + + +const int N = 200041; + + + +int n, m; + +vector edgs; + +vector gra[N]; + +bool vi[N]; + + + +int skibidibapmdada(const edg &ed){ + + if(!vi[ed.a]){ + + return ed.a; + + }else if(!vi[ed.b]){ + + return ed.b; + + }else{ + + return 0; + + } + +} + + + +priority_queue pq; + +void addme(int a){ + + vi[a] = true; + + for(auto edi : gra[a]){ + + edg &ed = edgs[edi]; + + if(!vi[ed.ott(a)]){ + + pq.push(ed); + + } + + } + +} + + + +int cost = 0; + +vector naisu; + + + +int main(){ + + + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + edg ed; + + fin >> ed.a >> ed.b >> ed.v; + + edgs.push_back(ed); + + gra[ed.a].push_back(i); + + gra[ed.b].push_back(i); + + } + + + + addme(1); + + while(!pq.empty()){ + + edg ed = pq.top(); + + pq.pop(); + + int w = skibidibapmdada(ed); + + if(w != 0){ + + cost += ed.v; + + naisu.push_back(ed); + + addme(w); + + } + + } + + + + fout << cost << "\n"; + + fout << naisu.size() << "\n"; + + for(auto ed : naisu){ + + fout << ed.a << " " << ed.b << "\n"; + + } + + + + return 0; + +} diff --git a/mst/prim/6457274f-4d7b-41f1-997e-8b75b3fefa68.cpp b/mst/prim/6457274f-4d7b-41f1-997e-8b75b3fefa68.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7493e3f559d9b01282b89626585f72590fd8ccba --- /dev/null +++ b/mst/prim/6457274f-4d7b-41f1-997e-8b75b3fefa68.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +vector > > v[200020]; + +priority_queue >, vector > >, greater > > > pq; + +queue > q; + +pair > elem; + +int n, m, viz[200020], cost, nrm; + +void citire () + +{ + + fin>>n>>m; + + int i, x, y, co; + + for (i=1; i<=m; i++) + + { + + fin>>x>>y>>co; + + v[x].push_back(make_pair(co,make_pair(x, y))); + + v[y].push_back(make_pair(co,make_pair(y, x))); + + } + +} + +void prim () + +{ + + cost=0; nrm=0; + + int i, j; + + for (i=0; i +#include +#include +#include + + + + +using namespace std; + + + +const int NMAX = 200005; + +const int INF = 1e9; + + + +int n, m; + +vector > graph[NMAX]; + + + +int main() { + + + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + int x, y, w; + + for (int i = 0; i < m; i++) { + + fin >> x >> y >> w; + + graph[x].emplace_back(y, w); + + graph[y].emplace_back(x, w); + + } + + + + + + priority_queue, vector >, greater<>> pq; + + int s = 1; + + vector dist(n + 1, INF); + + vector parent(n + 1, 0); + + vector viz(n, false); + + + + pq.push(make_pair(0, s)); + + dist[s] = 0; + + + + int cost = 0; + + + + pair pair; + + while (!pq.empty()) { + + pair = pq.top(); + + pq.pop(); + + x = pair.second; + + + + if (viz[x]) { + + continue; + + } + + + + cost += pair.first; + + viz[x] = true; + + + + for (auto & i : graph[x]) { + + y = i.first; + + w = i.second; + + + + if (!viz[y] && w < dist[y]) { + + dist[y] = w; + + pq.push(make_pair(dist[y], y)); + + parent[y] = x; + + } + + } + + } + + + + fout << cost << "\n" << n - 1 << "\n"; + + for (int i = 1; i <= n; i++) { + + if (i != s) + + fout << parent[i] << " " << i << "\n"; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/prim/658da69a-a926-4d98-9248-49038112d583.cpp b/mst/prim/658da69a-a926-4d98-9248-49038112d583.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddfa7039a1530e74e1a3b9600653ae969947d9aa --- /dev/null +++ b/mst/prim/658da69a-a926-4d98-9248-49038112d583.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > pq; + +int tat[200005],dist[200005]; + +vector < pair < int , int > > v[200005]; + +int n,m; + +int x,y,cost; + +int oo=2000000009; + +bool viz[200005]; + +int sumdist; + + + +int main() + +{ + + f >> n >> m; + + for (int i=1;i<=m;i++) { + + f >> x >> y >> cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for (int i=2;i<=n;i++) { + + dist[i]=oo; + + } + + pq.push(make_pair(0,1)); + + while (pq.empty()==0) { + + int nod = pq.top().second; + + int distnod = pq.top().first; + + pq.pop(); + + if (viz[nod]==0) { + + viz[nod] = 1; + + sumdist += distnod; + + for (int i=0;i disttoviz && viz[nodtoviz] == 0) { + + dist[nodtoviz] = disttoviz; + + pq.push(make_pair(disttoviz,nodtoviz)); + + tat[nodtoviz] = nod; + + } + + } + + } + + } + + g << sumdist << '\n' << n-1 << '\n'; + + for (int i=2;i<=n;i++) { + + g << i << " " << tat[i]<<'\n'; + + } + + return 0; + +} diff --git a/mst/prim/65fc8024-5a68-4a13-b9c6-afe169bc0227.cpp b/mst/prim/65fc8024-5a68-4a13-b9c6-afe169bc0227.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1788916023f1532bd3ea2bda34e9867e4e406e62 --- /dev/null +++ b/mst/prim/65fc8024-5a68-4a13-b9c6-afe169bc0227.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >h; + +vector >v[200005]; + +int n,m,x,y,c,viz[200005]; + +struct soll + +{ + + int x,y; + +}sol[200005]; + +struct muchie + +{ + + int x,y,c; + +}mu[400005]; + +int prim(int nod) + +{ + + int i,cost=0,cc=0,j,nodc; + + viz[nod]=1; + + for(i=1;i<=n-1;i++) + + { + + for(j=0;j>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>c; + + v[x].push_back({c,y}); + + v[y].push_back({c,x}); + + } + + g< +#include +#include +#include +const int N_MAX = 1000005; + +std :: vector > graph[N_MAX], apm; +std :: priority_queue > > pq; +bool vis[N_MAX]; +int n, m, t_cost; +int start; + +int main(){ + + std :: ifstream fin("apm.in"); + std :: ofstream fout("apm.out"); + + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y, c; + fin >> x >> y >> c; + graph[x].push_back(std :: make_pair(y, c)); + graph[y].push_back(std :: make_pair(x, c)); + + } + + start = 1; + vis[start] = true; + for (auto i : graph[start]) { + pq.push({-i.second, {start, i.first }}); + } + + + while(!pq.empty()) { + std :: pair > best_cost = pq.top(); + + pq.pop(); + + int cost = best_cost.first; + int next = best_cost.second.second; + + if (!vis[next]) { + vis[next] = true; + apm.push_back(best_cost.second); + + t_cost += -cost; + + for (auto i : graph[next]) { + pq.push({-i.second, {next, i.first}}); + } + } + } + + fout << t_cost << "\n" << n-1 << '\n'; + for (auto i : apm) fout << i.first << " " << i.second << '\n'; + return 0; +} diff --git a/mst/prim/67b3a1cd-8f8d-4367-94d9-e48bb9960408.cpp b/mst/prim/67b3a1cd-8f8d-4367-94d9-e48bb9960408.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0af618f3d2b76528c62403b82bfd06f16606004a --- /dev/null +++ b/mst/prim/67b3a1cd-8f8d-4367-94d9-e48bb9960408.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +using namespace std; + +struct node{ + int value, depth; + node *parent; + + node(int v, int d, node *p = nullptr){ + this->value = v; + this->depth = d; + this->parent = p; + } + node* root(){ + if(this->parent == nullptr) return this; + this->parent = this->parent->root(); + return this->parent; + } + +}; + +struct edge{ + int x, y, cost; + edge(int d1, int d2, int c){ + this->x = d1; + this->y = d2; + this->cost = c; + } + bool operator< (const edge &other) const { + return this->cost > other.cost; + } +}; + +bool query(node* x, node* y){ + return (x->root() == y->root()); +} + +void update(node* x, node* y){ + if(x->depth < y->depth) x->root()->parent = y; + else if(x->depth > y->depth) y->root()->parent = x; + else{ + x->depth++, y->depth++; + x->root()->parent = y; + } +} +int main() { + int n, m; + ifstream f("apm.in"); + ofstream g("apm.out"); + f>>n>>m; + vector nodes; + nodes.reserve(n); + priority_queue edges; + for (int i = 0; i < n; ++i) { + nodes.push_back(new node(i, 1, nullptr)); + } + for (int i = 0; i < m; ++i) { + int x, y, o; + f>>x>>y>>o; + edges.push(edge(x - 1, y - 1, o)); + } + + int totalCost = 0; + vector> result; + result.reserve(n - 1); + while(!edges.empty()){ + edge t = edges.top(); + edges.pop(); + if(!query(nodes[t.x], nodes[t.y])){ + update(nodes[t.x], nodes[t.y]); + totalCost += t.cost; + result.push_back(make_pair(t.x, t.y)); + } + } + g< +#include +#include +#include + +using namespace std; + +struct edge { + int from, to, cost; +}; + +struct comparator { + bool operator()(edge a, edge b) { + return a.cost > b.cost; + } +}; + +ifstream f("apm.in"); +ofstream g("apm.out"); + +int n, m, a, b, c; +bool viz[200001]; +vector > graph[200001]; +vector > rez; +priority_queue, comparator> q; + +void rezolvare() { + for (auto &v:graph[1]) + q.push({1, v.first, v.second}); + edge sus; + int cost = 0; + while (!q.empty()) { + sus = q.top(); + q.pop(); + if (!viz[sus.to]) { + cost += sus.cost; + rez.emplace_back(sus.from, sus.to); + viz[sus.from] = viz[sus.to] = 1; + for (auto &v:graph[sus.to]) { + if (!viz[v.first]) { + q.push({sus.to, v.first, v.second}); + } + } + } + } + g << cost << '\n' << n - 1 << '\n'; + for (auto &v:rez) + g << v.first << ' ' << v.second << '\n'; +} +void citire(){ + f >> n >> m; + for (int i = 1; i <= m; ++i) { + f >> a >> b >> c; + graph[a].emplace_back(b, c); graph[b].emplace_back(a, c); + } +} + + +int main() { + citire(); + rezolvare(); + return 0; +} diff --git a/mst/prim/68b37b01-37f9-45bb-9f68-18983ce10cd1.cpp b/mst/prim/68b37b01-37f9-45bb-9f68-18983ce10cd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9732258089e9781f2d039f571f9e8f22db01d9d --- /dev/null +++ b/mst/prim/68b37b01-37f9-45bb-9f68-18983ce10cd1.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int Dim = 4e5 + 5; + +const int Inf = 1e9 + 5; + + + +int dist[Dim], t[Dim]; + +int n, m, costTotal; + + + +bool used[Dim]; + + + +vector > G[Dim], ans; + +priority_queue > coada; + + + +int main() { + + f >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y, cost; + + f >> x >> y >> cost; + + G[x].push_back({y, cost}); + + G[y].push_back({x, cost}); + + } + + + + for(int i = 1; i <= n; ++i) dist[i] = Inf; + + dist[1] = 0; + + + + coada.push({0, 1}); + + while(!coada.empty()) { + + int node = coada.top().second; + + coada.pop(); + + + + if(used[node] == false) { + + used[node] = true; + + costTotal += dist[node]; + + if(t[node]) ans.push_back({node, t[node]}); + + + + for(auto it = G[node].begin(); it != G[node].end(); ++it) { + + if(it -> second < dist[it -> first]) { + + dist[it -> first] = it -> second; + + t[it -> first] = node; + + coada.push({-it -> second, it -> first}); + + } + + } + + } + + } + + + + g << costTotal << '\n' << n - 1 << '\n'; + + for(auto it = ans.begin(); it != ans.end(); ++it) { + + g << it -> first << " " << it -> second << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/6bf67251-f005-44c0-aab7-39dde4a55c9c.cpp b/mst/prim/6bf67251-f005-44c0-aab7-39dde4a55c9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b43b41c0cbc29892deb8e2fe799f5f9764459aad --- /dev/null +++ b/mst/prim/6bf67251-f005-44c0-aab7-39dde4a55c9c.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int main() + +{ + + int n, m, x, y, c, s; + + fin >> n>> m; + + int d[n+1], t[n+1], viz[n+1]; + + vector < pair> g[n+1]; + + for(int i=1; i<=m; ++i) + + { + + fin >> x >>y>> c; + + g[x].push_back({y,c}); + + g[y].push_back({x,c}); + + } + + for(int i=1; i<=n;++i) + + { + + d[i] = INT_MAX; + + t[i] = 0; + + viz[i] = 0; + + } + + s = 1; + + d[s] = 0; + + + + priority_queue < pair > q; + + q.push({-d[s], s}); + + while(!q.empty()) + + { + + x=q.top().second; + + q.pop(); + + viz[x]=1; + + for(auto i: g[x]) + + { + + y=i.first; + + c=i.second; + + if(viz[y]==0 && d[y]>c) + + { + + t[y]=x; + + d[y]=c; + + q.push({-d[y], y}); + + } + + } + + } + + s=0; + + for(int i=1; i<=n; i++) s+=d[i]; + + fout< +#include +#include +#include + + +const int MAXN = 200000 + 5; + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +typedef pair > pii; + + + +vector >graf[MAXN]; + +priority_queue, greater >coada; + +bool viz[MAXN]; + +int n,m; + + + +int main() + +{ + + + + in>>n>>m; + + for(int i = 1; i <= m; i++){ + + int x,y,cost; + + in>>x>>y>>cost; + + graf[x].push_back({cost,y}); + + graf[y].push_back({cost,x}); + + } + + viz[1] = 1; + + for(int i = 0; i < graf[1].size(); i++) + + coada.push({graf[1][i].first,{1,graf[1][i].second}}); + + long long s = 0; + + vector>arbore; + + while(!coada.empty() && arbore.size() < n - 1){ + + pii ceva = coada.top(); + + int tata = ceva.second.first,nod = ceva.second.second; + + int cost = ceva.first; + + coada.pop(); + + if(viz[nod]) + + continue; + + s += cost; + + arbore.push_back({tata,nod}); + + viz[nod] = true; + + for(int i = 0; i < graf[nod].size(); i++){ + + int vecin = graf[nod][i].second,cost = graf[nod][i].first; + + if(!viz[vecin]){ + + coada.push({cost,{nod,vecin}}); + + } + + } + + } + + out< +#include +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +const int DIM = 200001, INF = 10000; + +vector a[DIM], c[DIM]; + +struct afis + +{ + + + + int nx, ny; + + + +} af[DIM]; + +int h[DIM], d[DIM], poz[DIM], pred[DIM]; + +bool viz[DIM]; + +int nh, n, kt = 1; + +long long S; + + + +void urca(int p); + +void sterge(int p); + +void coboara(int p); + +void adauga(int val); + +void prim(int x0); + + + +int main() + +{ + + + + int m; + + cin >> n >> m; + + + + for(int i = 1; i <= m; ++i) + + { + + int x, y, cost; + + cin >> x >> y >> cost; + + + + a[x].push_back(y); + + a[y].push_back(x); + + c[x].push_back(cost); + + c[y].push_back(cost); + + } + + + + prim(1); + + cout << S << '\n' << n - 1 << '\n'; + + + + for(int i = 1; i < n; ++i) + + cout << af[i].nx << ' ' << af[i].ny << '\n'; + + return 0; + +} + + + +void prim(int x0) + +{ + + int x; + + for(int i = 1; i <= n; ++i) + + { + + d[i] = INF; + + h[i] = i; + + poz[i] = i; + + } + + + + d[x0] = 0; + + urca(poz[x0]); + + nh = n; + + + + while(nh > 0) + + { + + x = h[1]; + + + + viz[x] = true; + + if(nh < n) + + { + + af[kt].nx = x; + + af[kt].ny = pred[x]; + + + + S += d[x]; + + kt++; + + } + + + + sterge(1); + + for(int i = 0; i < a[x].size(); ++i) + + { + + int y = a[x][i]; + + if(viz[y]) continue; + + + + int cost = c[x][i]; + + if(pred[x] != y && cost < d[y]) + + { + + d[y] = cost; + + pred[y] = x; + + urca(poz[y]); + + } + + } + + } + +} + + + +void urca(int p) + +{ + + while(p > 1 && d[h[p]] < d[h[p / 2]]) + + { + + swap(h[p], h[p / 2]); + + poz[h[p]] = p; + + poz[h[p / 2]] = p / 2; + + p /= 2; + + } + +} + + + +void coboara(int p) + +{ + + int fs = p * 2, fd = p * 2 + 1, bun = p; + + if(fs <= nh && d[h[fs]] < d[h[bun]]) + + { + + bun = fs; + + } + + + + if(fd <= nh && d[h[fd]] < d[h[bun]]) + + { + + bun = fd; + + } + + if(bun != p) + + { + + swap(h[bun], h[p]); + + poz[h[p]] = p; + + poz[h[bun]] = bun; + + coboara(bun); + + } + +} + + + +void adauga(int val) + +{ + + h[++nh] = val; + + urca(nh); + +} + + + +void sterge(int p) + +{ + + swap(h[p], h[nh--]); + + poz[h[p]] = p; + + poz[h[nh + 1]] = nh + 1; + + urca(p); + + coboara(p); + +} diff --git a/mst/prim/6e051bb6-c026-425e-8c01-bb3c0a66db43.cpp b/mst/prim/6e051bb6-c026-425e-8c01-bb3c0a66db43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0d0f1260eccb6d7c87dc78cc5875f8c26551bed --- /dev/null +++ b/mst/prim/6e051bb6-c026-425e-8c01-bb3c0a66db43.cpp @@ -0,0 +1,400 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int INF = 2100000000; + + + +int n, m; + +vector > * lv; + + + +class Edge{ + + + +public: + + + + int first; + + int second; + + + + int price; + + + + Edge(int f, int s, int p): first(f), second(s), price(p) {}; + +}; + + + +int min_price; + +vector selected; + +int * pair_node; + + + +int * d; + +bool * v; + + + +int * q_pos; + +int * q; + +int q_dim; + + + +int pop(){ + + + + swap(q_pos[q[1]], q_pos[q[q_dim]]); + + swap(q[1], q[q_dim]); + + + + q_dim -= 1; + + + + bool ok = 0; + + + + int pos = 1; + + while(!ok) + + + + if(pos * 2 > q_dim && pos * 2 + 1 > q_dim) + + + + ok = true; + + + + else if(pos * 2 > q_dim){ + + + + if(d[q[pos * 2 + 1]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + else if(pos * 2 + 1 > q_dim){ + + + + if(d[q[pos * 2]] > d[q[pos]]) + + + + ok = true; + + + + else{ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + } + + else{ + + + + if(d[q[pos]] < d[q[pos * 2]] && d[q[pos]] < d[q[pos * 2 + 1]]) + + + + ok = true; + + + + else if(d[q[pos * 2]] < d[q[pos * 2 + 1]]){ + + + + swap(q_pos[q[pos * 2]], q_pos[q[pos]]); + + swap(q[pos * 2], q[pos]); + + + + pos *= 2; + + } + + else{ + + + + swap(q_pos[q[pos * 2 + 1]], q_pos[q[pos]]); + + swap(q[pos * 2 + 1], q[pos]); + + + + pos *= 2; + + pos += 1; + + } + + } + + + + return q[q_dim + 1]; + +} + + + +void update(int node){ + + + + int pos = q_pos[node]; + + + + bool ok = 0; + + + + while(!ok) + + + + if(pos / 2 == 0 || d[q[pos]] > d[q[pos / 2]]) + + + + ok = true; + + else{ + + + + swap(q_pos[q[pos]], q_pos[q[pos / 2]]); + + swap(q[pos], q[pos / 2]); + + + + pos /= 2; + + } + +} + + + +void prim(){ + + + + while(q_dim){ + + + + int current_node = pop(); + + v[current_node] = 1; + + + + + + if(current_node != 1){ + + + + min_price += d[current_node]; + + selected.push_back(Edge(current_node, pair_node[current_node], d[current_node])); + + } + + + + for(int i = 0; i < lv[current_node].size(); i++){ + + + + int next_node = lv[current_node][i].first; + + int new_price = lv[current_node][i].second; + + + + if(!v[next_node] && d[next_node] > new_price){ + + + + d[next_node] = new_price; + + pair_node[next_node] = current_node; + + + + update(next_node); + + } + + } + + } + +} + + + +int main(){ + + + + f >> n >> m; + + + + lv = new vector > [n + 1]; + + + + d = new int [n + 1]; + + v = new bool [n + 1]; + + + + pair_node = new int [n + 1]; + + + + q = new int [n + 1]; + + q_pos = new int [n + 1]; + + q_dim = n; + + + + for(int i = 1 ; i <= n; i++){ + + + + d[i] = INF; + + v[i] = 0; + + + + q[i] = i; + + q_pos[i] = i; + + } + + + + int x, y, c; + + for(int i = 0; i < m ; i++){ + + + + f >> x >> y >> c; + + + + lv[x].push_back(make_pair(y, c)); + + lv[y].push_back(make_pair(x, c)); + + } + + + + prim(); + + + + g << min_price << '\n'; + + + + g << selected.size() << '\n'; + + for(int i = 0; i < selected.size(); i++) + + + + g << selected[i].first << " " << selected[i].second << '\n'; + + + + + + return 0; + +} diff --git a/mst/prim/6f95328b-3582-4ab0-a5eb-c00caa99fb0a.cpp b/mst/prim/6f95328b-3582-4ab0-a5eb-c00caa99fb0a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b23e450bf44d6456251361024a62aa46ad3657d --- /dev/null +++ b/mst/prim/6f95328b-3582-4ab0-a5eb-c00caa99fb0a.cpp @@ -0,0 +1,451 @@ +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 400000000; + +const int MAX_HEAP_SIZE=200001; + +typedef int heap[MAX_HEAP_SIZE]; + + + +heap h; + + + +struct nod + +{ + + int info, cost, sursa; + + nod * urm; + +}*vecin[200001]; + + + +nod * prim[200001]; + +nod * ultim[200001]; + +int v[200001], p[200001]; + + + +struct nod2 + +{ + + nod * info; + + nod2 * urm; + +}; + + + +inline int father (int x) + +{ + + return x/2; + +} + + + +inline int left_son (int x) + +{ + + return x*2; + +} + + + +inline int right_son(int x) + +{ + + return x*2 + 1; + +} + + + +void sift(heap h, int n, int k) + +{ + + int son; + + do + + { + + son=0; + + + + if(left_son(k)<=n) + + { + + son=left_son(k); + + + + if(right_son(k)<=n && v[ h[right_son(k)] ] < v[ h[son] ] ) + + { + + son=right_son(k); + + } + + + + if(v[ h[k] ] <= v[ h[son] ]) + + { + + son=0; + + } + + } + + + + if(son) + + { + + swap(p[ h[k] ], p[ h[son] ]); + + swap(h[k], h[son]); + + k=son; + + } + + + + }while(son); + +} + + + +void percolate (heap h, int n, int k) + +{ + + int key=h[k]; + + + + while(k>1 && v[key] < v[ h[father(k)] ]) + + { + + p[ h[father(k)] ]=k; + + h[k]=h[father(k)]; + + k=father(k); + + } + + + + p[key]=k; + + h[k]=key; + +} + + + +int main() + +{ + + int n, m, i, a, b, c, l; + + fin>>n>>m; + + + + for(i=1; i<=m; i++) + + { + + fin>>a>>b>>c; + + + + + + nod * aux1= new nod; + + aux1->info=b; + + aux1->cost=c; + + aux1->sursa=a; + + aux1->urm=NULL; + + + + if(prim[a]==NULL) + + { + + prim[a]=aux1; + + vecin[a]=aux1; + + } + + + + else + + { + + vecin[a]->urm=aux1; + + vecin[a]=aux1; + + } + + + + nod * aux2 = new nod; + + aux2->info=a; + + aux2->cost=c; + + aux2->sursa=b; + + aux2->urm=NULL; + + + + if(prim[b]==NULL) + + { + + prim[b]=aux2; + + vecin[b]=aux2; + + } + + + + else + + { + + vecin[b]->urm=aux2; + + vecin[b]=aux2; + + } + + } + + + + + + l=n-1; + + for(i=2; i<=n; i++) + + { + + v[i]=INFINIT; + + h[i-1]=i; + + p[i]=i-1; + + } + + + + v[1]=0; + + p[1]=n; + + nod * t = prim[1]; + + + + while(t!=NULL) + + { + + v[t->info] = t->cost; + + percolate(h, l, p[t->info]); + + ultim[t->info] = t; + + + + t=t->urm; + + } + + + + nod * prim_lista = NULL; + + nod * lista = NULL; + + + + for(i=1; iurm=NULL; + + afis->info = ultim[x]->sursa; + + afis->sursa= ultim[x]->info; + + afis->cost=ultim[x]->cost; + + + + if(prim_lista==NULL) + + { + + prim_lista=afis; + + lista=afis; + + } + + else + + { + + lista->urm=afis; + + lista=afis; + + } + + + + nod * t = prim[x]; + + + + while(t!=NULL) + + { + + if(p[t->info]<=l && t->cost < v[t->info]) + + { + + v[t->info] = t->cost; + + percolate(h, l, p[t->info]); + + ultim[t->info] = t; + + } + + + + t=t->urm; + + } + + } + + + + nod * aux3= prim_lista; + + int suma=0; + + + + while(aux3!=NULL) + + { + + suma=suma+aux3->cost; + + aux3=aux3->urm; + + } + + + + fout<info<<' '<sursa<<"\n"; + + aux3=aux3->urm; + + } + + + + + + + +} diff --git a/mst/prim/7054882d-1e2b-4f21-86ba-9db8ee7a7bf9.cpp b/mst/prim/7054882d-1e2b-4f21-86ba-9db8ee7a7bf9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5303dc1801fb8723339c5b3f9c0427c03ef2f265 --- /dev/null +++ b/mst/prim/7054882d-1e2b-4f21-86ba-9db8ee7a7bf9.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct nod + +{ + + int x,y,c; + + bool operator<(const nod&other)const + + { + + return c > other.c; + + } + +}; + +priority_queue q; + +vector> gr[200005],sol; + + + +bool seen[200005]; + +int n,m,costmin,nr; + + + +void citire() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y,c; + + in>>x>>y>>c; + + gr[x].push_back({y,c}); + + gr[y].push_back({x,c}); + + } + +} + +void rezolv() + +{ + + seen[1]=1; + + for(const auto& it:gr[1]) + + q.push({1,it.first,it.second}); + + while(!q.empty()) + + { + + nod nd=q.top(); + + int x=nd.x,y=nd.y; + + q.pop(); + + if(seen[x]==1 && seen[y]==1) + + continue; + + costmin+=nd.c; + + sol.push_back({x,y}); + + if(seen[x]==0) + + y=x; + + seen[y]=1; + + nr++; + + if(nr==m-1) + + break; + + for(const auto &it:gr[y]) + + if(seen[it.first]==0) + + q.push({y,it.first,it.second}); + + } + +} + +void afisare(){ + + out< +#include +#include +#include + + + +using namespace std; + +vector >> graph; +vector > apm; +priority_queue >> heap; +vector visited; +int n; + +void read(){ + ifstream fin("apm.in"); + + int m, x, y, c; + fin>>n>>m; + graph.resize(n); + visited.resize(n, false); + while(m--){ + fin>>x>>y>>c; + graph[x-1].emplace_back(y-1, c); + graph[y-1].emplace_back(x-1, c); + } +} + +int main() { + ofstream fout("apm.out"); + read(); + int total_cost = 0; + for(auto &e: graph[0]) + heap.push({-e.second, {0, e.first}}); + visited[0] = true; + while(!heap.empty()){ + auto top = heap.top(); + heap.pop(); + int cost = -top.first; + int node = top.second.second; + if(!visited[node]){ + visited[node] = true; + apm.push_back(top.second); + total_cost += cost; + for(auto &e: graph[node]) + heap.push({-e.second, {node, e.first}}); + } + } + fout< +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 200000; + +const int CMAX = 1000; + + + +struct elem { + + int nod, cost; + + + + elem(int cnod = 0, int ccost = 0) { + + nod = cnod; + + cost = ccost; + + } + +}; + + + +bool operator > (elem e1, elem e2) { + + return e1.cost > e2.cost; + +} + + + +int n, m, APM; + +vector graf[NMAX + 5]; + +priority_queue, greater> pq; + +bool sel[NMAX + 5]; + +int cmin[NMAX + 5]; + +int tata[NMAX + 5]; + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + + + scanf("%d %d", &n, &m); + + + + for(int i = 1; i <= m; i++) { + + int x, y, c; + + + + scanf("%d %d %d", &x, &y, &c); + + + + graf[x].push_back(elem(y, c)); + + graf[y].push_back(elem(x, c)); + + } + + + + for(int i = 1; i <= n; i++) + + cmin[i] = CMAX + 5; + + + + cmin[1] = 0; + + pq.push(elem(1, 0)); + + + + while(!pq.empty()) { + + elem crt = pq.top(); + + pq.pop(); + + if(crt.cost > cmin[crt.nod] || sel[crt.nod]) + + continue; + + + + sel[crt.nod] = true; + + APM += crt.cost; + + + + + + for(elem vec: graf[crt.nod]) + + + + + + if(!sel[vec.nod] && vec.cost < cmin[vec.nod]) { + + cmin[vec.nod] = vec.cost; + + tata[vec.nod] = crt.nod; + + pq.push(elem(vec.nod, vec.cost)); + + } + + } + + + + + + printf("%d\n%d\n", APM, n - 1); + + for(int i = 2; i <= n; i++) + + printf("%d %d\n", tata[i], i); + + + + return 0; + +} diff --git a/mst/prim/72439dc4-fdc5-4922-a734-606cc5fcd397.cpp b/mst/prim/72439dc4-fdc5-4922-a734-606cc5fcd397.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d5f0039baf1cf67d692ebb8dbc0e1c79ec36877 --- /dev/null +++ b/mst/prim/72439dc4-fdc5-4922-a734-606cc5fcd397.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +using namespace std; + + + + + +struct Edge { + int node; + + int cost; + + bool operator < (Edge e) const { + if (cost != e.cost) { + return cost < e.cost; + } + + return node < e.node; + } +}; + +int n, m; + +vector g[200001]; + +set available; + +bool visited[200001]; + +int dist[200001]; + +int sol = 0; + +int solEdge[200001]; + +int main() { + ofstream out("apm.out"); + + ifstream in("apm.in"); + + in >> n >> m; + + for (int i = 0; i < m; i++) { + int x, y, c; + + in >> x >> y >> c; + + x--; y--; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + } + + for (int i = 0; i < 200001; i++) { + dist[i] = 1e9; + } + + dist[0] = 0; + + available.insert({0, 0}); + + while (!available.empty()) { + Edge curr = *available.begin(); + + available.erase(available.begin()); + + visited[curr.node] = true; + + int bestEdge = -1; + + for (int i = 0; i < g[curr.node].size(); i++) { + if (g[curr.node][i].cost < dist[g[curr.node][i].node] && !visited[g[curr.node][i].node]) { + dist[g[curr.node][i].node] = g[curr.node][i].cost; + + available.insert({g[curr.node][i].node, dist[g[curr.node][i].node]}); + + bestEdge = g[curr.node][i].node; + + solEdge[g[curr.node][i].node] = curr.node; + } + } + } + + for (int i = 0; i < n; i++) { + sol += dist[i]; + } + + out << sol << '\n'; + + out << n - 1 << '\n'; + + for (int i = 1; i < n; i++) { + out << solEdge[i] + 1 << " " << i + 1 << '\n'; + } + + return 0; +} diff --git a/mst/prim/738f4213-95bb-4234-acaa-ebc6fda91d0d.cpp b/mst/prim/738f4213-95bb-4234-acaa-ebc6fda91d0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b59915e987b2394c61ff6663bd70072c1dc7b783 --- /dev/null +++ b/mst/prim/738f4213-95bb-4234-acaa-ebc6fda91d0d.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + + + +struct muchie { + + int nod, tata, cost; + + + + muchie(int nn = 0, int tt = 0, int cc = 0) { + + nod = nn, tata = tt, cost = cc; + + } + +}; + + + +int n, m, nm, capm; + +int d[200000 + 5]; + +muchie apm[200000 + 5]; + +vector v[200000 + 5]; + +priority_queue, greater> pq; + + + +bool operator > (muchie m1, muchie m2) { + + return m1.cost > m2.cost; + +} + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int x, y, z; + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= n; i++) + + d[i] = 2000000000 + 5; + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &z); + + v[x].push_back(muchie(y, x, z)); + + v[y].push_back(muchie(x, y, z)); + + } + + + + pq.push(muchie(1, 0, 0)); + + nm = 0; + + while(!pq.empty() && nm < n - 1) { + + muchie crt = pq.top(); + + pq.pop(); + + if(d[crt.nod] <= crt.cost) + + continue; + + + + d[crt.nod] = crt.cost; + + capm += crt.cost; + + if(crt.tata) + + apm[++nm] = crt; + + + + for(muchie vec: v[crt.nod]) + + if(d[vec.nod] == 2000000000 + 5) + + pq.push(vec); + + } + + + + printf("%d\n%d\n", capm, nm); + + for(int i = 1; i <= nm; i++) + + printf("%d %d\n", apm[i].nod, apm[i].tata); + + + + return 0; + +} diff --git a/mst/prim/73c87fc4-eddb-4f86-a336-8a52ed7584e7.cpp b/mst/prim/73c87fc4-eddb-4f86-a336-8a52ed7584e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f27a9516be7a6aaaf40fc540f65f0916a58fd555 --- /dev/null +++ b/mst/prim/73c87fc4-eddb-4f86-a336-8a52ed7584e7.cpp @@ -0,0 +1,244 @@ +#include +#include +#include + + +using namespace std; + + + +const int nmax = 200000; + +const int mmax = 400000; + +const int INF = 2000000007; + + + +vector > g[nmax + 1], sol; + +int h[nmax + 1], u[nmax + 1]; + +int v[nmax + 1], last[nmax + 1]; + +int h_size; + +bool viz[nmax + 1]; + + + + + +void heap_swap(int a, int b){ + + swap(u[h[a]], u[h[b]]); + + swap(h[a], h[b]); + +} + + + +void heap_up(int p) { + + int f = p / 2; + + while (p > 1 && v[h[p]] < v[h[f]]) { + + heap_swap(p, f); + + p = p / 2; + + f = p / 2; + + } + +} + + + +void heap_down(int p) { + + int best, l, r; + + while (2 * p <= h_size) { + + l = 2 * p; + + best = l; + + + + r = 2 * p + 1; + + if (r <= h_size && v[h[r]] < v[h[l]]) { + + best = r; + + } + + + + if (v[h[p]] > v[h[best]]) { + + heap_swap(p, best); + + } else { + + break; + + } + + + + p = best; + + } + + + +} + + + +void heap_insert(int x) { + + ++h_size; + + h[h_size] = x; + + u[x] = h_size; + + + + heap_up(h_size); + +} + + + +void heap_erase(int x) { + + int p = u[x]; + + heap_swap(p, h_size); + + --h_size; + + + + heap_up(p); + + heap_down(p); + + + + u[x] = -1; + +} + + + +void heap_update(int x) { + + int p = u[x]; + + + + heap_up(p); + + heap_down(p); + +} + + + +int main() + +{ + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + int n, m, ans = 0, x, y, c; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; ++i) { + + scanf("%d%d%d", &x, &y, &c); + + g[x].push_back(make_pair(y, c)); + + g[y].push_back(make_pair(x, c)); + + } + + + + v[1] = 0; + + heap_insert(1); + + for (int i = 2; i <= n; ++i) { + + v[i] = INF; + + heap_insert(i); + + } + + + + for (int i = 1; i <= n; ++i) { + + x = h[1]; + + heap_erase(x); + + + + ans += v[x]; + + sol.push_back(make_pair(x, last[x])); + + + + for (int j = 0; j < g[x].size(); ++j) { + + y = g[x][j].first; + + c = g[x][j].second; + + if (u[y] != -1 && v[y] > c) { + + v[y] = c; + + last[y] = x; + + heap_update(y); + + } + + } + + + + } + + printf("%d\n", ans); + + printf("%d\n", n - 1); + + for (int i = 1; i < sol.size(); ++i) { + + printf("%d %d\n", sol[i].first, sol[i].second); + + } + + return 0; + +} diff --git a/mst/prim/745498d9-0c03-4b14-89ee-4764e689a2a4.cpp b/mst/prim/745498d9-0c03-4b14-89ee-4764e689a2a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c3ab8f0a8c6722a30037b805012b1fe80cba2cb --- /dev/null +++ b/mst/prim/745498d9-0c03-4b14-89ee-4764e689a2a4.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +using namespace std; + +using pii = pair; + +const int INF = 0x3f3f3f3f; + +int N, M; + +vector> adj; + + + + + +void Prim(const int &beg_node) + +{ + + int total_weight = 0; + + vector is_in_MST(N + 1); + + for (int i = 1; i <= N; ++i) + + { + + assert(!is_in_MST[i]); + + } + + vector key(N + 1, INF), parent(N + 1); + + set Q; + + + + Q.insert({0, beg_node}); + + key[beg_node] = 0; + + + + for (int node = 1; node <= N; ++node) + + { + + assert(!Q.empty()); + + + + total_weight += begin(Q)->first; + + int min_node = begin(Q)->second; + + assert(!is_in_MST[min_node]); + + is_in_MST[min_node] = true; + + Q.erase(begin(Q)); + + + + for (const pii &adj_node : adj[min_node]) + + { + + if (!is_in_MST[adj_node.second] && adj_node.first < key[adj_node.second]) + + { + + Q.erase({key[adj_node.second], adj_node.second}); + + key[adj_node.second] = adj_node.first; + + parent[adj_node.second] = min_node; + + Q.emplace(adj_node); + + } + + } + + } + + + + ofstream fout("apm.out"); + + + + fout << total_weight << "\n" << N - 1 << "\n"; + + for (int node = 1; node <= N; ++node) + + { + + if (node != beg_node) + + { + + fout << node << " " << parent[node] << "\n"; + + } + + } + +} + + + + + +int main() + +{ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + adj.resize(N + 1); + + + + while (M--) + + { + + int i, j, weight; + + fin >> i >> j >> weight; + + + + adj[i].push_back({weight, j}); + + adj[j].push_back({weight, i}); + + } + + + + Prim(1); + +} diff --git a/mst/prim/752796ab-c2b1-4547-a774-98c04ff9ba90.cpp b/mst/prim/752796ab-c2b1-4547-a774-98c04ff9ba90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..380f48d24aec8cbaf5ce04366e52d50dcb143346 --- /dev/null +++ b/mst/prim/752796ab-c2b1-4547-a774-98c04ff9ba90.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + + + +const int NMAX = 2e5 + 5; + + + +int n , m , u , v , c , node , nxt , f[NMAX] , ans_cost; + +vector > g[NMAX] , ans; + +priority_queue > , vector > > , greater > > > Q; + +pair< int , pair > p; + +int main() + +{ + + ifstream cin("apm.in"); + + ofstream cout("apm.out"); + + + + cin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + + + cin >> u >> v >> c; + + g[u].push_back({v , c}); + + g[v].push_back({u , c}); + + + + } + + + + Q.push({0 , {0 , 1} }); + + while(!Q.empty()) + + { + + + + p = Q.top(); + + Q.pop(); + + + + node = p.second.second; + + + + if(f[node])continue; + + ans.push_back({p.second.first , node}); + + f[node] = 1; + + + + ans_cost += p.first; + + for(int i = 0; i < g[node].size(); i++) + + Q.push({g[node][i].second , {node , g[node][i].first} }); + + } + + + + cout << ans_cost << "\n"; + + cout << ans.size() - 1 << "\n"; + + for(int i = 1; i < ans.size(); i++) + + cout << ans[i].first << " " << ans[i].second << "\n"; + + return 0; + +} diff --git a/mst/prim/7591cb98-167e-474a-baf1-e017e050b926.cpp b/mst/prim/7591cb98-167e-474a-baf1-e017e050b926.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca9918185ee783c77c39d20f298e05df02a4ca0b --- /dev/null +++ b/mst/prim/7591cb98-167e-474a-baf1-e017e050b926.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200005, INF = INT_MAX; + + + +vector> g[N]; + +bool vizitat[N]; + +int CostMin[N]; + +int tata[N]; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + for (int i = 0; i < m; i++) + + { + + int a, b, c; + + fin >> a >> b >> c; + + g[a].push_back({ b, c }); + + g[b].push_back({ a, c }); + + } + + + + for (int i = 2; i <= n; i++) + + { + + CostMin[i] = INF; + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + + + int CostTotal = 0; + + while (!heap.empty()) + + { + + + + int nod = heap.begin()->second; + + CostTotal += heap.begin()->first; + + heap.erase(heap.begin()); + + vizitat[nod] = true; + + + + for (auto neigh : g[nod]) + + { + + int NodVecin = neigh.first; + + int CostVecin = neigh.second; + + + + if (!vizitat[NodVecin] && CostVecin < CostMin[NodVecin]) + + { + + if (CostMin[NodVecin] != INF) + + { + + heap.erase(heap.find({ CostMin[NodVecin], NodVecin })); + + } + + + + heap.insert({ CostVecin, NodVecin }); + + CostMin[NodVecin] = CostVecin; + + tata[NodVecin] = nod; + + } + + } + + } + + + + fout << CostTotal << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + { + + fout << i << ' ' << tata[i] << '\n'; + + } + +} diff --git a/mst/prim/75da0551-4ea6-4c9c-9e0f-e623c508047b.cpp b/mst/prim/75da0551-4ea6-4c9c-9e0f-e623c508047b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac601c6c79d83b62f771dc1d2c9e7c453bd6ecbd --- /dev/null +++ b/mst/prim/75da0551-4ea6-4c9c-9e0f-e623c508047b.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("apm.in"); + + + +ofstream out("apm.out"); + + + +const int INF = 100000000; + + + +vector d; + + + +vector tata; + + + +vector cost; + + + +vector viz; + + + +vector>adiacenta[200000]; + + + +priority_queue< pair, vector >, greater> > Q; + + + + + +int n,m,i,s; + + + +void Prim() + +{ + + d[s] = 0; + + Q.push(make_pair(0,s)); + + + + while(!Q.empty()) + + { + + int u = Q.top().second; + + viz[u] = 1; + + Q.pop(); + + for (pair e : adiacenta[u]) + + { + + int vecin = e.first; + + int c = e.second; + + + + if (!viz[vecin] && c < cost[vecin]) + + { + + + + cost[vecin] = c; + + Q.push(make_pair(cost[vecin], vecin)); + + + + + + + + tata[vecin] = u; + + + + } + + + + } + + } + +} + + + +int main() + +{ + + in >> n >> m; + + d.resize(n+1); + + tata.resize(n+1); + + viz.resize(n+1); + + cost.resize(n+1); + + int x,y,c; + + for(i=0;i>x>>y>>c; + + adiacenta[x].push_back(make_pair(y, c)); + + adiacenta[y].push_back(make_pair(x, c)); + + } + + s=1; + + + + + + for(int i=1;i<=n;i++) + + { + + cost[i] = INF; + + viz[i] = 0; + + tata[i] = -1; + + } + + + + Prim(); + + + + + + int d_arbore = 0; + + for(int i=1;i<=n;i++) + + { + + if(i==s) + + continue; + + d_arbore += cost[i]; + + } + + out << d_arbore << endl; + + out << n-1 << endl; + + for(int i=1;i<=n;i++) + + { + + if(i==s) + + continue; + + out << tata[i] << " " << i << '\n'; + + } + + + +} diff --git a/mst/prim/78315f77-0ff6-44e2-b5c1-7d26e4e21d9e.cpp b/mst/prim/78315f77-0ff6-44e2-b5c1-7d26e4e21d9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9472da5de98f5485f11738307cf0ae27543b7513 --- /dev/null +++ b/mst/prim/78315f77-0ff6-44e2-b5c1-7d26e4e21d9e.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +const int NMAX = 2e5 + 5; + +const int INF = 1e9; + +int dist[NMAX], from[NMAX], ans; + +bool vis[NMAX]; + +priority_queue, vector>, greater>> pq; + +vector> graf[NMAX]; + + + +int main() + +{ + + int n, m; + + f >> n >> m; + + for(int i = 1; i <= m; i++) { + + int x, y, c; + + f >> x >> y >> c; + + graf[x].push_back(make_pair(y, c)); + + graf[y].push_back(make_pair(x, c)); + + } + + for(int i = 1; i <= n; i++) { + + dist[i] = INF; + + } + + dist[1] = 0; + + pq.push(make_pair(dist[1], 1)); + + while(!pq.empty()) { + + int nod = pq.top().second; + + pq.pop(); + + if(vis[nod]) { + + continue; + + } + + vis[nod] = 1; + + ans += dist[nod]; + + for(int i = 0; i < (int)graf[nod].size(); i++) { + + int next = graf[nod][i].first; + + int cost = graf[nod][i].second; + + if(!vis[next] && dist[next] > cost) { + + dist[next] = cost; + + from[next] = nod; + + pq.push(make_pair(dist[next], next)); + + } + + } + + } + + g << ans << '\n'; + + g << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) { + + g << i << ' ' << from[i] << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/79322415-304a-417c-97ee-4fade4d1e1be.cpp b/mst/prim/79322415-304a-417c-97ee-4fade4d1e1be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c82e78710ffbbdf5bac5df1e8dee661d88c900b4 --- /dev/null +++ b/mst/prim/79322415-304a-417c-97ee-4fade4d1e1be.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +struct muchie { + + int nod; + + int cost; + + bool operator <(const muchie& alt) const{ + + return this->cost < alt.cost; + + } + +}; + + + +const int N = 2e5, INF = 1 << 30; + + + +vector gr[N]; + +multiset s; + +int cmin[N], cor[N]; + +bool exista[N]; + +pair rez[N]; + + + +void cost_minim(int nod) { + + for (auto i : gr[nod]) + + if (!exista[i.nod] && i.cost < cmin[i.nod]) { + + cmin[i.nod] = i.cost; + + cor[i.nod] = nod; + + s.insert(i); + + } + +} + + + +int main() { + + ifstream in("apm.in"); + + ofstream out("apm.out"); + + + + int n, m, x, y, c; + + in >> n >> m; + + while (m--) { + + in >> x >> y >> c; + + --x, --y; + + gr[x].push_back({ y, c }); + + gr[y].push_back({ x, c }); + + } + + for (int i = 0; i < N; ++i) + + cmin[i] = INF; + + cost_minim(0); + + exista[0] = true; + + int nod, cost, ctot = 0; + + for (int i = 1; i < n; ++i) { + + nod = s.begin()->nod; + + while (exista[nod]) { + + s.erase(s.begin()); + + nod = s.begin()->nod; + + } + + s.erase(s.begin()); + + exista[nod] = true; + + cost = cmin[nod]; + + ctot += cost; + + rez[i - 1] = { nod, cor[nod] }; + + cost_minim(nod); + + } + + out << ctot << '\n' << n - 1 << '\n'; + + for (int i = 0; i < n - 1; ++i) + + out << rez[i].first + 1 << ' ' << rez[i].second + 1 << '\n'; + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/mst/prim/7a4aa020-e2ae-4c25-9d02-2b4fd31527db.cpp b/mst/prim/7a4aa020-e2ae-4c25-9d02-2b4fd31527db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b466948c06765843fcdb8bacd5cf3b1704d8efac --- /dev/null +++ b/mst/prim/7a4aa020-e2ae-4c25-9d02-2b4fd31527db.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + fin >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + fin >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0, length = n; + + for(int i = 1; i <= n; i ++) + + { + + + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(length) + + { + + + + pair least = q.top(); + + int node = least.second; + + + + + + + + q.pop(); + + if(viz[node] == 0) + + continue; + + length -- ; + + viz[node] = 0; + + cost += least.first; + + + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + fout< +#include +#include + + + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct muchie + +{ + + int nod, cost; + + bool operator < (const muchie& other) const + + { + + return cost < other.cost; + + } + +}; + + + +multiset s; + +vector v[200000 +5]; + +int viz[200000 +5], C[200000 +5], n, m, total; + +int cor[200000 +5]; + +vector> sol; + + + +void ins_apm(int nod) + +{ + + viz[nod]=1; + + for(muchie& i : v[nod]) if(i.cost < C[i.nod]) + + { + + C[i.nod] = i.cost; + + s.insert(i); + + cor[i.nod] = nod; + + } + +} + + + +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y, c; + + in>>x>>y>>c; + + v[x].push_back({y, c}); + + v[y].push_back({x, c}); + + } + + for(int i=1; i<=n; i++) C[i]=2e9; + + + + ins_apm(1); + + for(int i=2; i<=n; i++) + + { + + int x = s.begin()->nod; + + while(viz[x]==1) + + { + + s.erase(s.begin()); + + x = s.begin()->nod; + + } + + s.erase(s.begin()); + + ins_apm(x); + + total += C[x]; + + sol.push_back(make_pair(x, cor[x])); + + } + + + + out< +#include +#include +#include + + +using namespace std; + + + +struct muchie { + + int from, to, cost; + +}; + + + +class comp { + +public: + + bool operator()(muchie a, muchie b) { + + return a.cost > b.cost; + + } + +}; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, a, b, c; + +bool vizitat[200001]; + +vector > graph[200001]; + +vector > rez; + +priority_queue, comp> PQ; + + + +void rezolvare() { + + for (auto &v:graph[1]) + + PQ.push({1, v.first, v.second}); + + muchie sus; + + int cost = 0; + + while (!PQ.empty()) { + + sus = PQ.top(); + + PQ.pop(); + + if (!vizitat[sus.to]) { + + cost += sus.cost; + + rez.emplace_back(sus.from, sus.to); + + vizitat[sus.from] = vizitat[sus.to] = 1; + + for (auto &v:graph[sus.to]) { + + if (!vizitat[v.first]) { + + PQ.push({sus.to, v.first, v.second}); + + } + + } + + } + + } + + g << cost << '\n'; + + g << n - 1 << '\n'; + + for (auto &v:rez) + + g << v.first << ' ' << v.second << '\n'; + +} + +void citire(){ + + f >> n >> m; + + for (int i = 1; i <= m; ++i) { + + f >> a >> b >> c; + + graph[a].emplace_back(b, c); + + graph[b].emplace_back(a, c); + + } + +} + + + + + +int main() { + + citire(); + + rezolvare(); + + return 0; + +} diff --git a/mst/prim/7fa1c8dc-c3ed-4da7-9f09-057acaf5aa47.cpp b/mst/prim/7fa1c8dc-c3ed-4da7-9f09-057acaf5aa47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8903569a6b98c14628b967e472e91319ffa0a0d --- /dev/null +++ b/mst/prim/7fa1c8dc-c3ed-4da7-9f09-057acaf5aa47.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >V; + +vector >v[200005]; + +int n, m, x, y, c, viz[200005]; + +struct Banana + +{ + + int x, y; + +}sol[200005]; + +int prim(int nod) + +{ + + int i, cost = 0, b = 0, j, nodaux; + + viz[nod] = 1; + + for (i = 1; i <= n - 1; i++) + + { + + for (j = 0; j < v[nod].size(); j++) + + { + + nodaux = v[nod][j].second; + + b = v[nod][j].first; + + if (!viz[nodaux]) V.push({ b,{nod,nodaux} }); + + } + + while (viz[V.top().second.second]) + + V.pop(); + + cost += V.top().first; + + viz[V.top().second.second] = 1; + + sol[i].x = V.top().second.first; + + sol[i].y = V.top().second.second; + + nod = V.top().second.second; + + V.pop(); + + } + + return cost; + +} + +int main() + +{ + + int i; + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + v[x].push_back({ c,y }); + + v[y].push_back({ c,x }); + + } + + g << prim(1) << '\n' << n - 1 << '\n'; + + for (i = 1; i < n; i++) + + g << sol[i].x << " " << sol[i].y << '\n'; + + return 0; + +} diff --git a/mst/prim/82467361-6339-4eca-b56c-af4ee61d09d4.cpp b/mst/prim/82467361-6339-4eca-b56c-af4ee61d09d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..836c506d5b09cecf04a4d6976ae843fb345934c4 --- /dev/null +++ b/mst/prim/82467361-6339-4eca-b56c-af4ee61d09d4.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +FILE *fin = fopen("apm.in", "r"); + +FILE *fout = fopen("apm.out", "w"); + + + +typedef pair pi; + + + +priority_queue < pi, vector , greater > pq; + +vector graf[200005]; + + + +int n,m,x,y,c,cost[200005],tata[200005],nod,nod2,costmuch,ans,i; + +bool inAPM[200005]; + + + +int main() + +{ + + fscanf(fin, "%d%d", &n,&m); + + for(i=1; i<=m; ++i) + + { + + fscanf(fin, "%d%d%d", &x,&y,&c); + + graf[x].push_back(make_pair(y, c)); + + graf[y].push_back(make_pair(x, c)); + + } + + + + + + for(i=1; i<=n; ++i) + + cost[i] = 1e9; + + + + pq.push(make_pair(0, 1)); + + while(!pq.empty()) + + { + + nod = pq.top().second; + + pq.pop(); + + inAPM[nod] = true; + + + + for(i=0; i +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int t[200010], n, m, cost; + +vector < pair > a[200010], rez; + +priority_queue< pair >, vector > >, greater > > >pq; + +pair< int, pair >x; + + + +int main() + +{ + + int i, j, k; + + fin>>n>>m; + + for(k=1; k<=m; k++) + + { + + fin>>i>>j>>cost; + + a[i].push_back({cost,j}); + + a[j].push_back({cost,i}); + + } + + for(auto it:a[1]) + + { + + pq.push( {it.first, {1, it.second}} ); + + } + + cost=0; + + t[1]=1; + + while(pq.empty()==0) + + { + + x = pq.top(); + + pq.pop(); + + if( t[x.second.second]==0 ) + + { + + t[x.second.second] = x.second.first; + + cost = cost + x.first; + + rez.push_back({x.second.first, x.second.second}); + + for(auto it: a[x.second.second]) + + { + + if(t[it.second]==0) + + pq.push( {it.first, {x.second.second, it.second}} ); + + } + + } + + } + + t[1]=0; + + fout< +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +typedef pair p; + + + +const int NMAX = 200002; + +const int oo = 0x3f3f3f3f; + + + +vector

g[NMAX]; + + + +int n, x, y, c, m, d[NMAX], t[NMAX], cost; + + + +bitset used; + + + +struct cmp + +{ + + bool operator()(const p& x, const p& y) const + + { + + return x.second > y.second; + + } + +}; + + + +priority_queue, cmp > q; + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + g[x].push_back({ y,c }); + + g[y].push_back({ x,c }); + + } + + fill(d + 2, d + n + 1, oo); + + + + q.push({ 1, 0 }); + + used[0] = 1; + + + + int vn = n; + + + + while (vn) + + { + + x = 0; + + while (used[x]) + + { + + x = q.top().first; + + q.pop(); + + } + + used[x] = 1; + + cost += d[x]; + + --vn; + + vector

::iterator it = g[x].begin(), f = g[x].end(); + + for (; it != f; ++it) + + if (it->second < d[it->first] && used[it->first] == 0) + + { + + t[it->first] = x; + + d[it->first] = it->second; + + q.push({ it->first, it->second }); + + } + + } + + + + fout << cost << "\n" << n - 1 << "\n"; + + + + for (int i = 2; i <= n; ++i) + + fout << i << " " << t[i] << "\n"; + + + + return 0; + +} diff --git a/mst/prim/84455154-c2c2-4545-b012-87e009ddbba9.cpp b/mst/prim/84455154-c2c2-4545-b012-87e009ddbba9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7275081cac520c06969caa4fe3d25985482b4273 --- /dev/null +++ b/mst/prim/84455154-c2c2-4545-b012-87e009ddbba9.cpp @@ -0,0 +1,281 @@ +#include +#include +using namespace std; + + + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +const int DIM = 200001, INF = 10000; + +vector a[DIM], c[DIM]; + +struct afis + +{ + + + + int nx, ny; + + + +} af[DIM]; + +int h[DIM], d[DIM], poz[DIM], pred[DIM]; + +bool viz[DIM]; + +int nh, n, kt = 1; + +long long S; + + + +void urca(int p); + +void sterge(int p); + +void coboara(int p); + +void adauga(int val); + +void prim(int x0); + + + +int main() + +{ + + + + int m; + + cin >> n >> m; + + + + for(int i = 1; i <= m; ++i) + + { + + int x, y, cost; + + cin >> x >> y >> cost; + + + + a[x].push_back(y); + + a[y].push_back(x); + + c[x].push_back(cost); + + c[y].push_back(cost); + + } + + + + prim(1); + + cout << S << '\n' << n - 1 << '\n'; + + + + for(int i = 1; i < n; ++i) + + cout << af[i].nx << ' ' << af[i].ny << '\n'; + + return 0; + +} + + + +void prim(int x0) + +{ + + int x; + + for(int i = 1; i <= n; ++i) + + { + + d[i] = INF; + + h[i] = i; + + poz[i] = i; + + } + + + + d[x0] = 0; + + urca(poz[x0]); + + nh = n; + + + + while(nh > 0) + + { + + x = h[1]; + + + + viz[x] = true; + + if(nh < n) + + { + + af[kt].nx = x; + + af[kt].ny = pred[x]; + + + + S += d[x]; + + kt++; + + } + + + + sterge(1); + + for(int i = 0; i < a[x].size(); ++i) + + { + + int y = a[x][i]; + + if(viz[y]) continue; + + + + int cost = c[x][i]; + + if(pred[x] != y && cost < d[y]) + + { + + d[y] = cost; + + pred[y] = x; + + urca(poz[y]); + + } + + } + + } + +} + + + +void urca(int p) + +{ + + while(p > 1 && d[h[p]] < d[h[p / 2]]) + + { + + swap(h[p], h[p / 2]); + + poz[h[p]] = p; + + poz[h[p / 2]] = p / 2; + + p /= 2; + + } + +} + + + +void coboara(int p) + +{ + + int fs = p * 2, fd = p * 2 + 1, bun = p; + + if(fs <= nh && d[h[fs]] < d[h[bun]]) + + { + + bun = fs; + + } + + + + if(fd <= nh && d[h[fd]] < d[h[bun]]) + + { + + bun = fd; + + } + + if(bun != p) + + { + + swap(h[bun], h[p]); + + poz[h[p]] = p; + + poz[h[bun]] = bun; + + coboara(bun); + + } + +} + + + +void adauga(int val) + +{ + + h[++nh] = val; + + urca(nh); + +} + + + +void sterge(int p) + +{ + + swap(h[p], h[nh--]); + + poz[h[p]] = p; + + poz[h[nh + 1]] = nh + 1; + + urca(p); + + coboara(p); + +} diff --git a/mst/prim/84958afc-ba0b-4e91-b897-6506ed7cc8cf.cpp b/mst/prim/84958afc-ba0b-4e91-b897-6506ed7cc8cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b9a65a7a895a1c1ee3ad0b7733aa2d308e579ed --- /dev/null +++ b/mst/prim/84958afc-ba0b-4e91-b897-6506ed7cc8cf.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int MAX_N = 200000; +const int MAX_M = 400000; +const int INF = 1000000000; + +bool marked[1+MAX_N]; + +struct Edge { + int a, b, c; + + bool operator< (const Edge& x) const { + return c > x.c; + } +} edges[1+MAX_M]; + +priority_queue pq; +vector graph[1+MAX_N]; + +void markNode(int node) { + marked[node] = true; + for(auto it: graph[node]) + pq.push(edges[it]); +} + +int top; +int apmA[1+MAX_N], apmB[1+MAX_N]; + +int main() { + int N, M, sumapm = 0; + + FILE *fin = fopen("apm.in", "r"); + fscanf(fin, "%d%d", &N, &M); + + for(int i = 0; i < M; ++i) { + int a, b, c; + fscanf(fin, "%d%d%d", &a, &b, &c); + edges[i] = {a, b, c}; + graph[a].push_back(i); + graph[b].push_back(i); + } + fclose(fin); + + markNode(1); + while(!pq.empty()) { + Edge edge = pq.top(); + pq.pop(); + + if(marked[edge.a] + marked[edge.b] == 1) { + apmA[top] = edge.a; + apmB[top++] = edge.b; + sumapm += edge.c; + if(!marked[edge.a]) + markNode(edge.a); + else + markNode(edge.b); + } + } + + FILE *fout = fopen("apm.out", "w"); + fprintf(fout, "%d\n%d\n", sumapm, top); + for(int i = 0; i < top; ++i) + fprintf(fout, "%d %d\n", apmA[i], apmB[i]); + fclose(fout); + return 0; +} diff --git a/mst/prim/85111f9d-e507-4cc5-b24c-1d5b664cdd77.cpp b/mst/prim/85111f9d-e507-4cc5-b24c-1d5b664cdd77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49338ae372f6106209b48c6eae6286c8b96017a0 --- /dev/null +++ b/mst/prim/85111f9d-e507-4cc5-b24c-1d5b664cdd77.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 1005; + +const int NMAX = 200005; + + + +vector < vector < pair < int, int > > > costuri(NMAX); + +vector < int > vx; + +vector < int > vy; + +int tati[NMAX], N, M; + +bool vizitat[NMAX]; + +int dist[NMAX]; + +long long cost_minim; + +int nod_start = -1; + + + +set < pair < int, int > > heap; + + + +void init() + +{ + + for(int i = 0; i < NMAX; i++) + + dist[i] = INFINIT; + +} + + + +void citire() + +{ + + fin >> N >> M; + + int x, y, cost; + + int cost_min = 1005; + + for(int i = 0; i < M; i++) + + { + + fin >> x >> y >> cost; + + pair < int, int > pair_1 = make_pair(y, cost); + + pair < int, int > pair_2 = make_pair(x, cost); + + costuri[x].push_back(pair_1); + + costuri[y].push_back(pair_2); + + if(cost < cost_min) + + { + + cost_min = cost; + + nod_start = x; + + } + + } + + init(); + +} + + + +void Prim() + +{ + + for(unsigned int i = 0; i < costuri[nod_start].size(); i++) + + { + + tati[costuri[nod_start][i].first] = nod_start; + + heap.insert( {costuri[nod_start][i].second, costuri[nod_start][i].first} ); + + dist[costuri[nod_start][i].first] = costuri[nod_start][i].second; + + } + + vizitat[nod_start] = 1; + + + + while(!heap.empty()) + + { + + int mini = heap.begin() -> second; + + int cost = heap.begin() -> first; + + heap.erase(heap.begin()); + + + + if(vizitat[mini]) + + continue; + + cost_minim += cost; + + vizitat[mini] = 1; + + vx.push_back(tati[mini]); vy.push_back(mini); + + for(unsigned int k = 0; k < costuri[mini].size(); k++) + + if(!vizitat[costuri[mini][k].first]) + + { + + if(costuri[mini][k].second < dist[costuri[mini][k].first]) + + { + + dist[costuri[mini][k].first] = costuri[mini][k].second; + + tati[costuri[mini][k].first] = mini; + + } + + heap.insert( {costuri[mini][k].second, costuri[mini][k].first} ); + + } + + } + +} + + + +void afisare() + +{ + + fout << cost_minim << '\n'; + + fout << N - 1 << '\n'; + + for(int i = 0; i < N - 1; i++) + + fout << vx[i] << ' ' << vy[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + Prim(); + + afisare(); + + return 0; + +} diff --git a/mst/prim/881d7dce-4bf0-475c-8f95-606dbae3f437.cpp b/mst/prim/881d7dce-4bf0-475c-8f95-606dbae3f437.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0af6e7e4a9348663deac03be27a99a14a3e1ec7 --- /dev/null +++ b/mst/prim/881d7dce-4bf0-475c-8f95-606dbae3f437.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; +typedef long long ll; +typedef pair PII; +typedef vector VI; +typedef vector VPII; +const int INF=0x3f3f3f3f; + +int n, m; +int h[4*200005], poz[200005],dep, father[200005],ans, cost[200005]; +VPII g[200005]; +VPII vans; + +void push (int node) { + int x, y = poz[node]; + while (y != x) { + x = y; + if (((x << 1) <= dep) && cost[h[y]] > cost[h[(x << 1)]]) { + y = (x << 1); + } + if ((((x << 1) + 1) <= dep) && cost[h[y]] > cost[h[(x << 1) + 1]]) { + y = (x << 1) + 1; + } + + swap(h[x], h[y]); + swap(poz[h[x]], poz[h[y]]); + } +} + +int read_head() { + int node = h[1]; + h[1] = h[dep]; + poz[h[1]] = poz[node]; + --dep; + push(h[1]); + return node; +} + +void repair_heap (int node) { + while (poz[node] > 1 && cost[h[poz[node]]] < cost[h[poz[node] / 2]]) { + int p = poz[node]; + swap(poz[h[p]], poz[h[p / 2]]); + swap(h[p], h[p / 2]); + } +} + +void put_in_heap (int node) { + h[++dep] = node; + poz[node] = dep; + repair_heap(node); +} + +void put_in_apm (int node) { + for(auto it: g[node]) { + if (cost[it.first] >= it.second) { + + cost[it.first] = it.second; + father[it.first] = node; + repair_heap(it.first); + } + } +} + +int main() +{ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + cin.tie(0); + ios_base::sync_with_stdio(0); + int x, y, c; + cin >> n >> m; + + for(int i=(1);i<=(m);++i) { + cin >> x >> y >> c; + g[x].push_back({y,c}); + g[y].push_back({x,c}); + } + for(int i=(1);i<=(n);++i) { + cost[i] = INF; + } + cost[1] = 0; + put_in_apm(1); + for(int i=(2);i<=(n);++i) { + put_in_heap(i); + } + for(int i=(1);i<(n);++i) { + int node = read_head(); + put_in_apm(node); + ans += cost[node]; + vans.push_back({node, father[node]}); + } + cout << ans << '\n' << ((int)(vans).size()) <<'\n'; + for (auto it: vans) { + cout << it.first <<' ' << it.second << '\n'; + } + +} diff --git a/mst/prim/8883e08f-53bd-4088-9c38-e94b2168a47b.cpp b/mst/prim/8883e08f-53bd-4088-9c38-e94b2168a47b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..201c747abbd5a331288ab10c8d8a214d77871f47 --- /dev/null +++ b/mst/prim/8883e08f-53bd-4088-9c38-e94b2168a47b.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + + + +int n, m, s; + +int d[200005], t[200005], viz[200005]; + +vector>graf[200005]; + + + +void read() + +{ + + f>>n>>m; + + for (int i=0;i>x>>y>>c; + + graf[x].push_back({y,c}); + + graf[y].push_back({x,c}); + + } + + for (int i=0;i<=n;++i) + + { + + d[i]=999999999; + + t[i]=0; + + viz[i]=0; + + } + + + +} + + + +void output() + +{ + + int cost = 0; + + for(int i=1; i<=n; i++) + + cost += d[i]; + + + + g<>Q; + + Q.push({-d[s],s}); + + while (!Q.empty()) + + { + + int nod = Q.top().second; + + viz[nod] = 1; + + Q.pop(); + + for (pair i : graf[nod]) + + { + + int y = i.first; + + int j = i.second; + + if (viz[y] == 0 && d[y]>j) + + { + + t[y] = nod; + + d[y] = j; + + Q.push({-d[y],y}); + + } + + } + + + + } + + + + output(); + + + +} diff --git a/mst/prim/8a509bd1-30d9-408c-b1be-fdc51dca71f6.cpp b/mst/prim/8a509bd1-30d9-408c-b1be-fdc51dca71f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9d4ef50032890704cf58e49a15c17b8a7e0c774 --- /dev/null +++ b/mst/prim/8a509bd1-30d9-408c-b1be-fdc51dca71f6.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout("apm.out"); + +struct muchie + +{ + + int x,y,c; + +}; + +struct pereche + +{ + + int y,c; + +}; + +struct comp + +{ + + bool operator()(muchie u, muchie v) + + { + + if(u.c>v.c) + + return 1; + + return 0; + + } + +}; + +vectorG[200001]; + +priority_queue,comp>q; + +int n,m,x,y,c,t[200001],nr, viz[200001],ct; + +int main() + +{ + + pereche p; + + int i; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + p={y,c}; + + G[x].push_back(p); + + p={x,c}; + + G[y].push_back(p); + + } + + for(i=0;i +#include + + +using namespace std; + + + + + + + +priority_queue< pair > > myheap; + +vector< pair > graph[400005], apm; + +bool viz[400005]; + +int n, m, cost_total; + + + +int main() { + + int a, b, c; + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w",stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d%d", &a, &b, &c); + + graph[a].push_back(make_pair(b, c)); + + graph[b].push_back(make_pair(a, c)); + + } + + + + int root = 1; + + viz[root] = true; + + for(auto edge : graph[root]) { + + myheap.push(make_pair(-edge.second, make_pair(root, edge.first))); + + } + + + + while(!myheap.empty()) { + + pair > best = myheap.top(); + + myheap.pop(); + + + + int cost = best.first; + + int new_node = best.second.second; + + + + if(!viz[new_node]) { + + viz[new_node] = true; + + apm.push_back(best.second); + + cost_total += -cost; + + + + for(auto edge : graph[new_node]) { + + myheap.push(make_pair(-edge.second, make_pair(new_node, edge.first))); + + } + + } + + } + + + + printf("%d\n%d\n", cost_total, n - 1); + + for(auto edge : apm) { + + printf("%d %d\n", edge.first, edge.second); + + } + + + + + + return 0; + +} diff --git a/mst/prim/8b5d861b-a0a7-47ac-a6bf-c936150681b1.cpp b/mst/prim/8b5d861b-a0a7-47ac-a6bf-c936150681b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddfa7039a1530e74e1a3b9600653ae969947d9aa --- /dev/null +++ b/mst/prim/8b5d861b-a0a7-47ac-a6bf-c936150681b1.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > pq; + +int tat[200005],dist[200005]; + +vector < pair < int , int > > v[200005]; + +int n,m; + +int x,y,cost; + +int oo=2000000009; + +bool viz[200005]; + +int sumdist; + + + +int main() + +{ + + f >> n >> m; + + for (int i=1;i<=m;i++) { + + f >> x >> y >> cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for (int i=2;i<=n;i++) { + + dist[i]=oo; + + } + + pq.push(make_pair(0,1)); + + while (pq.empty()==0) { + + int nod = pq.top().second; + + int distnod = pq.top().first; + + pq.pop(); + + if (viz[nod]==0) { + + viz[nod] = 1; + + sumdist += distnod; + + for (int i=0;i disttoviz && viz[nodtoviz] == 0) { + + dist[nodtoviz] = disttoviz; + + pq.push(make_pair(disttoviz,nodtoviz)); + + tat[nodtoviz] = nod; + + } + + } + + } + + } + + g << sumdist << '\n' << n-1 << '\n'; + + for (int i=2;i<=n;i++) { + + g << i << " " << tat[i]<<'\n'; + + } + + return 0; + +} diff --git a/mst/prim/8ce2bbe4-1fdc-4eb4-91d4-a420622fba83.cpp b/mst/prim/8ce2bbe4-1fdc-4eb4-91d4-a420622fba83.cpp new file mode 100644 index 0000000000000000000000000000000000000000..237af1ea98763737f308b6f231fdccf600bed1d0 --- /dev/null +++ b/mst/prim/8ce2bbe4-1fdc-4eb4-91d4-a420622fba83.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +vector > adj[200001]; + +bitset <200001> reached; + +pair minCost[200001]; + + + +void dijkstra(int start) { + + priority_queue > best; + + best.push(make_pair(0, start)); + + + + while (!best.empty()) { + + int front = best.top().second; + + long long cost = -best.top().first; + + best.pop(); + + + + reached[front] = 1; + + for (pair edge : adj[front]) + + if (!reached[edge.first]) + + if (edge.second <= minCost[edge.first].first) { + + minCost[edge.first].first = edge.second; + + minCost[edge.first].second = front; + + best.push(make_pair(-edge.second, edge.first)); + + } + + } + +} + + + +void addEdge(int x, int y, int c) { + + adj[x].push_back(make_pair(y, c)); + + adj[y].push_back(make_pair(x, c)); + +} + + + +int main() { + + int n, m, x, y, c; + + + + fin >> n >> m; + + for (int i = 1; i <= m; i++) { + + fin >> x >> y >> c; + + addEdge(x, y, c); + + } + + + + for (int i = 2; i <= n; i++) + + minCost[i].first = INT_MAX; + + + + dijkstra(1); + + + + long long result = 0; + + for (int i = 1; i <= n; i++) + + result += minCost[i].first; + + + + fout << result << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << minCost[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/8ff4d8ac-161a-4d76-a99d-ecf4518c6e5b.cpp b/mst/prim/8ff4d8ac-161a-4d76-a99d-ecf4518c6e5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b5254b04b4b9ac0569ff7e550461590935d3e97 --- /dev/null +++ b/mst/prim/8ff4d8ac-161a-4d76-a99d-ecf4518c6e5b.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, cost; + +int parent[200005], card[200005]; + +bool ap[200005]; + +vector < pair< int, int > > nod[200005], ans; + +priority_queue < pair < int, pair< int, int > >, vector < pair < int, pair< int, int > > >, greater < pair < int, pair< int, int > > > > pq; + + + +void add_edges(int x){ + + ap[x] = true; + + int y, c; + + for(auto it: nod[x]){ + + y = it.first; + + c = it.second; + + if(ap[y]) + + continue; + + pq.push({c, {y, x}}); + + } + +} + + + +void answer(){ + + add_edges(1); + + int c, x, y; + + while(ans.size() != n-1){ + + c = pq.top().first; + + y = pq.top().second.first; + + x = pq.top().second.second; + + pq.pop(); + + if(ap[y]) + + continue; + + cost += c; + + ans.push_back({x, y}); + + add_edges(y); + + } + +} + + + +void read(){ + + f>>n>>m; + + int c, x, y; + + for(int i = 1; i <= m; ++i){ + + f>>x>>y>>c; + + nod[x].push_back({y, c}); + + nod[y].push_back({x, c}); + + } + + answer(); + + g< +#include +#include +#include + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +const int marime = 200005; + +int suma; +bool f[marime]; +vector> sir; +vector> mat[marime]; +priority_queue>> q; + +int main() { + int n, m; + fin >> n >> m; + + for (int i = 1; i <= m; ++i) { + int x, y, cost; + fin >> x >> y >> cost; + + mat[x].push_back({y, cost}); + mat[y].push_back({x, cost}); + } + + f[1] = 1; + + for (int i = 0; i < mat[1].size(); ++i) { + q.push({-mat[1][i].second, {1, mat[1][i].first}}); + } + + while (!q.empty()) { + int cost = q.top().first; + int from = q.top().second.first; + int to = q.top().second.second; + q.pop(); + + if (f[to] == 1) { + continue; + } + + sir.push_back({from, to}); + suma += cost; + f[to] = 1; + + for (int i = 0; i < mat[to].size(); ++i) { + if (f[mat[to][i].first] == 1) { + continue; + } + + q.push({-mat[to][i].second, {to, mat[to][i].first}}); + } + } + + fout << -suma << "\n" << n - 1 << "\n"; + + for (int i = 0; i < sir.size(); ++i) { + fout << sir[i].first << " " << sir[i].second << "\n"; + } + + return 0; +} diff --git a/mst/prim/914f16fc-772c-487a-a380-81fe4c2e7104.cpp b/mst/prim/914f16fc-772c-487a-a380-81fe4c2e7104.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a10baa9d179847df393285cadf9871888d220541 --- /dev/null +++ b/mst/prim/914f16fc-772c-487a-a380-81fe4c2e7104.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +const int NMAX = 200005; + +vector < pair > v[NMAX],rasp; + +priority_queue < pair < int , pair > , vector < pair < int,pair > > , greater < pair < int, pair > > > q; + +bool fixat[NMAX]; + +int n,m,x,y,c,cost; + +int main() + +{ + + fin >> n >> m; + + for(int i=1;i<=m;i++){ + + fin >> x >> y >> c; + + v[x].push_back({y,c}); + + v[y].push_back({x,c}); + + } + + q.push({0,{0,1}}); + + while(!q.empty()){ + + pair < int , pair > aux=q.top(); + + q.pop(); + + int xx=aux.second.first,yy=aux.second.second; + + if(fixat[yy]==true) continue; + + fixat[yy]=true; + + rasp.push_back({xx,yy}); + + cost+=aux.first; + + for(int i=0;i + + +using namespace std; + +ifstream r("apm.in"); + +ofstream w("apm.out"); + +int viz[200002]; + +vector>g[200002], rez; + +struct qu + +{ + + int nod, cost, prev; + + qu(int nod, int cost, int prev) + + { + + this->nod=nod; + + this->cost=cost; + + this->prev=prev; + + } + + bool operator < (qu b)const + + { + + return cost>b.cost; + + } + +}; + +priority_queueq; + +int main() + +{ + + int n, m; + + r>>n>>m; + + viz[1]=1; + + for(int i=0; i>x>>y>>c; + + g[x].push_back(make_pair(y, c)); + + g[y].push_back(make_pair(x, c)); + + } + + long long sum=0; + + for(int i=0; i +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("trees.in"); +ofstream fout("trees.out"); + + + +const int NR = 2e5 + 10 , oo = 2e9; +int n , m ; +vector < int > d ( NR , oo ) ; +struct node{ + int nod , cost , tata; +}; +struct cmp { + bool operator() ( node i , node j ) { + return i.cost > j.cost ; + } +}; +ifstream in("apm.in"); +ofstream out("apm.out"); +priority_queue < node,vector,cmp> pq; +vector < pair < int , int > > v [ NR ] , sol ; +int viz [ NR ] ; +signed main() { + int x , y , i , z ; + in >> n >> m ; + for ( i = 1 ; i <= m ; ++ i ) { + in >> x >> y >> z ; + v [ x ].emplace_back( make_pair( y , z ) ) ; + v [ y ].emplace_back( make_pair( x , z ) ) ; + } + d [ 1 ] = 0 ; + node temp ; + temp.cost = 0; + temp.nod = 1; + temp.tata = -1; + pq.push(temp); + int nod ; + long long sum = 0 ; + while ( !pq.empty() ) { + nod = pq.top().nod ; + node temp2 = pq.top(); + pq.pop(); + if ( viz [ nod ] ) + continue ; + sum += temp2.cost; + viz [ nod ] = 1 ; + sol.push_back(make_pair(temp2.nod,temp2.tata)); + for ( auto it : v [ nod ] ) { + if ( !viz [ it.first ] ) { + temp.nod = it.first ; + temp.cost = it.second ; + temp.tata = nod ; + pq.push(temp); + } + } + } + out << sum << '\n' ; + out << sol.size() - 1 << '\n' ; + for ( auto it : sol ) { + if ( it.second != -1 ) + out << it.second << ' ' << it.first << '\n' ; + } + return 0; +} diff --git a/mst/prim/929b181a-3683-402d-8df9-3f739f8098e2.cpp b/mst/prim/929b181a-3683-402d-8df9-3f739f8098e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f83f04a59a993c0585bdb19740a49c29033f6f0b --- /dev/null +++ b/mst/prim/929b181a-3683-402d-8df9-3f739f8098e2.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct mucie { + + int nod1, nod2; + +} muchii[200005]; + + + +struct node { + + int nod, cost; + +}; + + + +struct heapNode { + + int nod_init, nod_fin, cost; + + inline bool operator < (const heapNode &other) const + + { + + return cost > other.cost; + + } + +}; + + + +int nr_noduri, nr_muchii, total, nr_bagate; + +vector G[200005]; + +priority_queue heap; + +bool isIn[200005]; + + + +void citire() + +{ + + fin>>nr_noduri>>nr_muchii; + + for(int i=1; i<=nr_muchii; i++) + + { + + node nod1, nod2; + + fin>>nod1.nod>>nod2.nod>>nod1.cost; + + nod2.cost=nod1.cost; + + G[nod1.nod].push_back(nod2); + + G[nod2.nod].push_back(nod1); + + } + +} + + + +void apm_prim() + +{ + + isIn[1]=true; + + for(int i=0; i + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > q; + +vector < pair < int , int > > v[200100]; + +int totaldist,n,viz[200100],dist[200100],tata[200100]; + +int m,i,x,y,cost; + +int oo=2000000009; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(int i=1;i<=n;i++) { + + dist[i]=oo; + + } + + dist[1]=0; + + q.push(make_pair(0,1)); + + while(q.empty()==0) { + + int nod = q.top().second; + + int distnod = q.top().first; + + q.pop(); + + if(viz[nod]==0) { + + viz[nod] = 1; + + totaldist += distnod; + + for (int i=0;i distance && viz[nodtoviz]==0) { + + dist[nodtoviz] = distance; + + q.push(make_pair(dist[nodtoviz],nodtoviz)); + + tata[nodtoviz] = nod; + + } + + } + + } + + } + + g << totaldist << '\n'; + + g << n-1 << '\n'; + + for (i=1;i<=n;i++) { + + if (tata[i]!=0) { + + g << i << " " << tata[i] << '\n'; + + } + + } + + return 0; + +} diff --git a/mst/prim/93fb5634-2393-4fc9-8eb1-b417d66a10a3.cpp b/mst/prim/93fb5634-2393-4fc9-8eb1-b417d66a10a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..229050ffca6fc7c97800ff29c672eb5059636f84 --- /dev/null +++ b/mst/prim/93fb5634-2393-4fc9-8eb1-b417d66a10a3.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include + +const int INF = 2e9; +const int NMAX = 2e5 + 1; + +std::ifstream in("apm.in"); +std::ofstream out("apm.out"); + +typedef std::pair > pip; +std::priority_queue , std::greater<> > heap; +std::vector > graf[NMAX]; +std::vector > sol; +bool viz[NMAX]; +int N, M; +int costTotal = 0; + +void Dijkstra (); + +int main() { + in >> N >> M; + int n1, n2, val; + for (int i = 1; i <= M; i++) { + in >> n1 >> n2 >> val; + graf[n1].push_back({n2, val}); + graf[n2].push_back({n1, val}); + } + + Dijkstra(); + out << costTotal << " " << N - 1 << "\n"; + for (auto elem : sol) + out << elem.first << " " << elem.second << "\n"; +} + +void Dijkstra () { + + int root = 1; + viz[root] = true; + for (auto elem : graf[root]) { + heap.push({elem.second,{root, elem.first}}); + } + + while (!heap.empty()) { + pip best = heap.top(); + heap.pop(); + + int cost = best.first; + int new_node = best.second.second; + + if (!viz[new_node]) { + viz[new_node] = true; + sol.push_back(best.second); + costTotal += cost; + + for (auto edge : graf[new_node]) { + if (!viz[edge.first]) + heap.push({edge.second, {new_node, edge.first}}); + } + } + } +} diff --git a/mst/prim/94fb902a-e825-4519-8c5f-f97cedad4065.cpp b/mst/prim/94fb902a-e825-4519-8c5f-f97cedad4065.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d252ad8d24046b11a6b3939b71b8b43bc81e8323 --- /dev/null +++ b/mst/prim/94fb902a-e825-4519-8c5f-f97cedad4065.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; + +ifstream fin( "apm.in" ); +ofstream fout( "apm.out" ); + +const int MaxN = 200002; + +vector G[MaxN]; +vector cost[MaxN]; +priority_queue>, vector>>, greater>>> q; +int viz[MaxN]; +int arb[MaxN][2]; + +void solve( int stNode ) { + int node, ne = 0, minc = 0; + + q.push( { 0, { stNode, stNode } } ); + while ( !q.empty() ) { + if ( !viz[q.top().second.second] ) { + node = q.top().second.second; + minc += q.top().first; + arb[ne][0] = node; + arb[ne++][1] = q.top().second.first; + viz[node] = 1; + for ( int i = 0; i < G[node].size(); ++i ) { + if ( !viz[G[node][i]] ) { + q.push( { cost[node][i], { node, G[node][i] } } ); + } + } + } else { + q.pop(); + } + } + fout << minc << "\n" << ne - 1 << "\n"; + for ( int i = 1; i < ne; ++i ) { + fout << arb[i][0] << " " << arb[i][1] << "\n"; + } +} + +int main() { + int n, m, i, x, y, c; + + fin >> n >> m; + for ( i = 0; i < m; ++i ) { + fin >> x >> y >> c; + G[x].push_back( y ); + G[y].push_back( x ); + cost[x].push_back( c ); + cost[y].push_back( c ); + } + solve( 1 ); + fin.close(); + fout.close(); + return 0; +} diff --git a/mst/prim/958ece7d-ac9d-46ec-907b-ca74c03265db.cpp b/mst/prim/958ece7d-ac9d-46ec-907b-ca74c03265db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2658258a5f7e4e1498caf81443bc9b6554c0636 --- /dev/null +++ b/mst/prim/958ece7d-ac9d-46ec-907b-ca74c03265db.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int NMAX=200005,INF=0x3F3F3F3F; + +int n,m,x,y,price,cost[NMAX]; + +vector>edge[NMAX]; + +bitsetok; + +struct stalker + +{ + + int a,b; + +} solution[NMAX]; + +void prim(int n) + +{ + + int mn,node,k=0; + + long long answer=0; + + priority_queue>,vector>>,greater>>>pq; + + ok[1]=1; + + for(int i=1; i<=n; i++) + + cost[i]=INF; + + for(auto next: edge[1]) + + { + + cost[next.first]=next.second; + + pq.push({next.second,{next.first,1}}); + + } + + for(int i=1; i<=n-1; i++) + + { + + while(ok[pq.top().second.first]==1) + + pq.pop(); + + mn=pq.top().first; + + node=pq.top().second.first; + + answer+=mn; + + solution[++k].a=node; + + solution[k].b=pq.top().second.second; + + ok[node]=1; + + for(auto next:edge[node]) + + { + + cost[next.first]=min(cost[next.first],next.second); + + pq.push({next.second,{next.first,node}}); + + } + + } + + g<>n>>m; + + for(int i=0; i<=m; i++) + + { + + f>>x>>y>>price; + + edge[x].push_back({y,price}); + + edge[y].push_back({x,price}); + + } + + prim(n); + + return 0; + +} diff --git a/mst/prim/9810087c-8c6a-464c-baca-de7aa83daa5e.cpp b/mst/prim/9810087c-8c6a-464c-baca-de7aa83daa5e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7591fa77189737cbdb40c1a0f76a9e26dbfe4a5e --- /dev/null +++ b/mst/prim/9810087c-8c6a-464c-baca-de7aa83daa5e.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +vector > v[200001]; +int cmin[200001], p[200001]; + +struct el +{ + int nod, c; + bool operator <(const el &alt) const + { + return c > alt.c; + } +}; + +priority_queue q; + +int main() +{ + int n, m, i, x, y, c, j; + int cost = 0; + el nmin; + fin >> n >> m; + for (i = 1; i<=m; i++) + { + fin >> x >> y >> c; + v[x].push_back({y, c}); + v[y].push_back({x, c}); + } + for (i = 1; i<=n; i++) + cmin[i] = 1<<30; + cmin[1] = -(1<<30); + for (i = 0; i cmin[q.top().nod]) + q.pop(); + nmin = q.top(); + cost += nmin.c; + cmin[nmin.nod] = -(1<<30); + for (j = 0; j + + +using namespace std; + + + +struct ura{ + + int nod,c; + + bool operator <(const ura &other)const{ + + return c v[200001]; + +multiset s; + +int cost[200001],parc[200001],cor[200001]; + +ura ans[200001]; + + + +void apm(int nodadaug){ + + int i,x,cs; + + ura nod; + + for(i=0;ics){ + + cor[x]=nodadaug; + + cost[x]=cs; + + s.insert(v[nodadaug][i]); + + } + + } + +} + + + +int main() + +{ + + ifstream cin("apm.in"); + + ofstream cout("apm.out"); + + + + int n,m,x,y,cs,i,costmin; + + cin>>n>>m; + + ura nod; + + for(i=1;i<=m;++i){ + + cin>>x>>y>>cs; + + nod.nod=y; + + nod.c=cs; + + v[x].push_back(nod); + + nod.nod=x; + + v[y].push_back(nod); + + } + + for(i=1;i<=n;++i) + + cost[i]=2e9; + + apm(1); + + parc[1]=1; + + costmin=0; + + int nod1; + + for(i=2;i<=n;++i){ + + nod1=s.begin()->nod; + + while(parc[nod1]==1){ + + s.erase(s.begin()); + + nod1=s.begin()->nod; + + } + + if(s.size()>0) + + s.erase(s.begin()); + + parc[nod1]=1; + + apm(nod1); + + costmin+=cost[nod1]; + + nod.nod=nod1; + + nod.c=cor[nod1]; + + ans[i]=nod; + + } + + cout< +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = (int)vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + +} + + + + + + + +std::vector vecini[N]; + +int main() + +{ + + + + int n, m; + + in >> n >> m; + + while (m--) + + { + + Muchie m; + + in >> m; + + vecini[m.a].push_back({m.b, m.cost}); + + vecini[m.b].push_back({m.a, m.cost}); + + } + + dj::reset(n); + + apm::prim(vecini, 0); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/9e4767a3-c6c8-4b8f-b571-a80f3a36356e.cpp b/mst/prim/9e4767a3-c6c8-4b8f-b571-a80f3a36356e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9a05a014f7da6ba6ae815c1eb9abff4e0a7415d --- /dev/null +++ b/mst/prim/9e4767a3-c6c8-4b8f-b571-a80f3a36356e.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct pereche { + + int y, c; + +}; + + + +struct muchie { + + int x, y, c; + +}; + + + +struct comp { + + bool operator() (muchie u, muchie v) { + + return u.c > v.c; + + } + +}; + + + +int viz[200001], t[200001], d[200001]; + +int n, m, x, y, cost, nr, dm, im, ct, i, j; + + + +vector G[200001]; + +priority_queue, comp> q; + + + + + +int main() { + + fin >> n >> m; + + + + for (int i = 1; i <= m; i++) { + + fin >> x >> y >> cost; + + G[x].push_back({ y, cost }); + + G[y].push_back({ x, cost }); + + } + + + + for (int i = 2; i <= n; ++i) { + + d[i] = 1001; + + t[i] = 1; + + } + + + + for (int i = 0; i < G[1].size(); i++) { + + pereche pt = G[1][i]; + + d[pt.y] = pt.c; + + q.push({ 1, pt.y, pt.c }); + + } + + + + viz[1] = 1; + + nr = 1; + + ct = 0; + + + + while (nr < n) + + { + + muchie e; + + + + e = q.top(); q.pop(); + + + + if (viz[e.y] == 0) { + + dm = e.c; + + + + + + im = e.y; + + + + nr++; + + viz[im] = 1; + + ct = ct + dm; + + + + pereche pt; + + for (int i = 0; i < G[im].size(); ++i) { + + pt = G[im][i]; + + if (viz[pt.y] == 0 && d[pt.y] > pt.c) { + + d[pt.y] = pt.c; + + t[pt.y] = im; + + q.push({ im, pt.y, pt.c }); + + } + + } + + } + + } + + + + fout << ct << '\n'; + + fout << n - 1 << '\n'; + + for (i = 2; i <= n; i++) + + fout << i << ' ' << t[i] << '\n'; + +} diff --git a/mst/prim/9e578869-da8a-4746-b422-6de0f3982b99.cpp b/mst/prim/9e578869-da8a-4746-b422-6de0f3982b99.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7817a7ec68cda7fcc5ce1beb98339958dba973eb --- /dev/null +++ b/mst/prim/9e578869-da8a-4746-b422-6de0f3982b99.cpp @@ -0,0 +1,121 @@ +#include +#include + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int N,M; + +int tata[200005], d[200005]; + +bool viz[200005]; + +int cost_total = 0; + +vector> Graph[200005]; + +auto Compare = [](pair &a, pair &b) + +{ + + return get<1>(a)>get<1>(b); + +}; + +priority_queue,vector>,decltype(Compare) > que(Compare); + + + +void initializare(int nod) + +{ + + d[nod] = 2e9; + + tata[nod]=0; + +} + + + +int main() + +{ + + in>>N>>M; + + int x,y,z; + + for(int i=1; i<=M; i++) + + { + + in>>x>>y>>z; + + Graph[x].push_back({y,z}); + + Graph[y].push_back({x,z}); + + } + + for(int i=1; i<=N; i++) + + initializare(i); + + d[1] = 0; + + que.push({1,0}); + + + + while(!que.empty()) + + { + + int u = que.top().first; + + que.pop(); + + viz[u] = true; + + for(auto &vecin:Graph[u]) + + { + + int v = vecin.first; + + int w = vecin.second; + + if(!viz[v] && w +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fi("apm.in"); + +ofstream fo("apm.out"); + + + +int n,m,nod,s,k; + +int cost[200005],b[200005],dist[200005]; + +bool sel[200005]; + + + +vector >a[200005]; + +priority_queue > q; + + + +int main() + +{ + + fi>>n>>m; + + + + for(int i=1;i<=m;i++) + + { + + int x,y,c; + + fi>>x>>y>>c; + + + + a[x].push_back({y,c}); + + a[y].push_back({x,c}); + + } + + + + for(int i=2; i<=n;i++) + + { + + dist[i]=INT_MAX; + + q.push({-INT_MAX,i}); + + } + + + + q.push({0,1}); + + + + while(1) + + { + + if(k==n) + + break; + + + + if(q.empty()) + + break; + + + + int i=q.top().second; + + q.pop(); + + + + if(!sel[i]) + + { + + sel[i]=true; + + k++; + + for(int j=0;ja[i][j].second and !sel[nod]) + + { + + dist[nod]=a[i][j].second; + + b[nod]=i; + + q.push({-dist[nod],nod}); + + } + + + + } + + } + + + + } + + + + for(int i=1;i<=n;i++) + + s+=dist[i]; + + + + fo< +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +struct muchie + +{ + + int curr; + + int dest; + + int cost; + + bool operator>(const muchie other) const + + { + + if(other.cost == cost) + + return dest > other.dest; + + return cost > other.cost; + + } + +}; + +vector graf[200005]; + +bool viz[200005]; + +int n, m; + +vector > sol; + +priority_queue, greater > Q; + +int cost; + + + +void read() + +{ + + f>>n>>m; + + int c, x, y; + + for(int i = 0; i>x>>y>>c; + + graf[x].push_back({x, y, c}); + + graf[y].push_back({y, x, c}); + + } + +} + + + +void addNode(int node) + +{ + + viz[node] = true; + + for(auto p : graf[node]) + + { + + if(!viz[p.dest]) + + Q.push(p); + + } + +} + + + +void solve() + +{ + + addNode(1); + + while(sol.size() < n-1) + + { + + muchie mu = Q.top(); + + Q.pop(); + + if(viz[mu.dest]) + + continue; + + sol.push_back({mu.curr, mu.dest}); + + addNode(mu.dest); + + cost += mu.cost; + + } + +} + + + +void afisare() + +{ + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + if (vis[y.first] && y.second < minCost[y.first]) + + heap.erase(heap.find({ minCost[y.first], y.first })); + + + + heap.insert({ y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/9f85678e-fb0e-4730-98ad-8beeeb307c08.cpp b/mst/prim/9f85678e-fb0e-4730-98ad-8beeeb307c08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50bee1493d363736442c8e5326e3228e99aa7693 --- /dev/null +++ b/mst/prim/9f85678e-fb0e-4730-98ad-8beeeb307c08.cpp @@ -0,0 +1,65 @@ +#include +#include +#include + + +using namespace std; + +const int NMAX = 200010; + +int N,M; +priority_queue< pair >, vector< pair > >, greater< pair > > >pq; + +vector> G[NMAX], apm; +bool viz[NMAX]; + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + f>>N>>M; + int s,d,c; + for(int i=0;i>s>>d>>c; + G[s].push_back(make_pair(d,c)); + G[d].push_back(make_pair(s,c)); + } + + int rad = 1; + viz[rad] = true; + + for(auto muchie: G[rad]) + pq.push(make_pair(muchie.second,make_pair(rad,muchie.first))); + + int cost = 0; + + while(!pq.empty()) + { + pair> aux = pq.top(); + pq.pop(); + + int c = aux.first; + int next = aux.second.second; + + if(!viz[next]) + { + viz[next] = true; + cost+= c; + apm.push_back(aux.second); + + for(auto m: G[next]) + pq.push(make_pair(m.second,make_pair(next,m.first))); + + } + } + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.insert({ y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/a0a668d4-2454-48e9-9b1a-5081b476fdd7.cpp b/mst/prim/a0a668d4-2454-48e9-9b1a-5081b476fdd7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0af6e7e4a9348663deac03be27a99a14a3e1ec7 --- /dev/null +++ b/mst/prim/a0a668d4-2454-48e9-9b1a-5081b476fdd7.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; +typedef long long ll; +typedef pair PII; +typedef vector VI; +typedef vector VPII; +const int INF=0x3f3f3f3f; + +int n, m; +int h[4*200005], poz[200005],dep, father[200005],ans, cost[200005]; +VPII g[200005]; +VPII vans; + +void push (int node) { + int x, y = poz[node]; + while (y != x) { + x = y; + if (((x << 1) <= dep) && cost[h[y]] > cost[h[(x << 1)]]) { + y = (x << 1); + } + if ((((x << 1) + 1) <= dep) && cost[h[y]] > cost[h[(x << 1) + 1]]) { + y = (x << 1) + 1; + } + + swap(h[x], h[y]); + swap(poz[h[x]], poz[h[y]]); + } +} + +int read_head() { + int node = h[1]; + h[1] = h[dep]; + poz[h[1]] = poz[node]; + --dep; + push(h[1]); + return node; +} + +void repair_heap (int node) { + while (poz[node] > 1 && cost[h[poz[node]]] < cost[h[poz[node] / 2]]) { + int p = poz[node]; + swap(poz[h[p]], poz[h[p / 2]]); + swap(h[p], h[p / 2]); + } +} + +void put_in_heap (int node) { + h[++dep] = node; + poz[node] = dep; + repair_heap(node); +} + +void put_in_apm (int node) { + for(auto it: g[node]) { + if (cost[it.first] >= it.second) { + + cost[it.first] = it.second; + father[it.first] = node; + repair_heap(it.first); + } + } +} + +int main() +{ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + cin.tie(0); + ios_base::sync_with_stdio(0); + int x, y, c; + cin >> n >> m; + + for(int i=(1);i<=(m);++i) { + cin >> x >> y >> c; + g[x].push_back({y,c}); + g[y].push_back({x,c}); + } + for(int i=(1);i<=(n);++i) { + cost[i] = INF; + } + cost[1] = 0; + put_in_apm(1); + for(int i=(2);i<=(n);++i) { + put_in_heap(i); + } + for(int i=(1);i<(n);++i) { + int node = read_head(); + put_in_apm(node); + ans += cost[node]; + vans.push_back({node, father[node]}); + } + cout << ans << '\n' << ((int)(vans).size()) <<'\n'; + for (auto it: vans) { + cout << it.first <<' ' << it.second << '\n'; + } + +} diff --git a/mst/prim/a2a9ce68-6bb1-4505-964f-b36703023349.cpp b/mst/prim/a2a9ce68-6bb1-4505-964f-b36703023349.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14c34a6a21212a1eeee2d3a23fb6ca29a3c4cd7e --- /dev/null +++ b/mst/prim/a2a9ce68-6bb1-4505-964f-b36703023349.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim]; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost,h[dim]; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim],Muchii; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +vector < edge > A; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + A.push_back({a,b,c}); + + } + + sort(A.begin(),A.end(), + + [&](edge X,edge Y)->bool + + { + + return (X.val +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef double db; + +typedef string str; + + + +typedef pair pi; + +typedef pair pl; + +typedef pair pd; + + + +typedef vector vi; + +typedef vector vl; + +typedef vector vd; + +typedef vector vs; + +typedef vector vpi; + +typedef vector vpl; +const int MOD = 1e9+7; + +const int NMAX = 2e5+5; + +const ll INF = 1e18; + +const ld PI = 4*atan((ld)1); + + + +namespace input { + + template void re(complex& x); + + template void re(pair& p); + + template void re(vector& a); + + template void re(array& a); + + + + template void re(T& x) { cin >> x; } + + template void re(T& t, Ts&... ts) { + + re(t); re(ts...); + + } + + + + template void re(complex& x) { T a,b; re(a,b); x = cd(a,b); } + + template void re(pair& p) { re(p.first,p.second); } + + template void re(vector& a) { for (int i = (0); i < ((int)a.size()); ++i) re(a[i]); } + + template void re(array& a) { for (int i = (0); i < (SZ); ++i) re(a[i]); } + +} + + + +using namespace input; + + + +namespace output { + + void pr(int x) { cout << x; } + + void pr(long x) { cout << x; } + + void pr(ll x) { cout << x; } + + void pr(unsigned x) { cout << x; } + + void pr(unsigned long x) { cout << x; } + + void pr(unsigned long long x) { cout << x; } + + void pr(float x) { cout << x; } + + void pr(double x) { cout << x; } + + void pr(ld x) { cout << x; } + + void pr(char x) { cout << x; } + + void pr(const char* x) { cout << x; } + + void pr(const string& x) { cout << x; } + + void pr(bool x) { pr(x ? "true" : "false"); } + + template void pr(const complex& x) { cout << x; } + + + + template void pr(const pair& x); + + template void pr(const T& x); + + + + template void pr(const T& t, const Ts&... ts) { + + pr(t); pr(ts...); + + } + + template void pr(const pair& x) { + + pr("{",x.first,", ",x.second,"}"); + + } + + template void pr(const T& x) { + + pr("{"); + + bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; + + pr("}"); + + } + + + + void ps() { pr("\n"); } + + template void ps(const T& t, const Ts&... ts) { + + pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); + + } + + + + void pc() { pr("]\n"); } + + template void pc(const T& t, const Ts&... ts) { + + pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); + + } + + + +} + +using namespace output; + + + +int n,m; + +struct tip{ + +int nod,cost,tata; + +}; + +bool operator<(tip x,tip y) + +{ + + return x.cost>y.cost; + +} + +priority_queuecoada; + +vectorG[NMAX]; + +vectormu; + +bool uz[NMAX]; + +int ans; + +void solve() { + +fin>>n>>m; + +for (int i = (1); i < (m+1); ++i) + + {int x,y,c; + + fin>>x>>y>>c; + + G[x].push_back({y,c,x}); + + G[y].push_back({x,c,y}); + + } + +for (int i = (0); i < (G[1].size()); ++i) + + coada.push({G[1][i].nod,G[1][i].cost,1}); + + uz[1]=1; + + while(!coada.empty()) + + {tip x; + + x=coada.top(); + + coada.pop(); + + if(!uz[x.nod]) + + {ans+=x.cost; + + uz[x.nod]=1; + + mu.push_back({x.tata,x.nod}); + + for (int i = (0); i < (G[x.nod].size()); ++i) + + coada.push({G[x.nod][i].nod,G[x.nod][i].cost,x.nod}); + + } + + } + +fout< +#include +#include +#include + + +using namespace std; + + + +int N, M, viz[200005], cost_total; + +vector > L[200005], sol; + +priority_queue < pair > > pq; + + + +int main() + +{ + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> N >> M; + + + + int x,y,c; + + + + for (int i = 1; i <= M; i++) + + { + + fin >> x >> y >> c; + + L[x].push_back({y, c}); + + L[y].push_back({x, c}); + + } + + + + int r = 1; + + pair > minim; + + viz[r] = 1; + + for (int i = 0; i < L[r].size(); i++) + + { + + pq.push({-L[r][i].second, {r, L[r][i].first}}); + + } + + + + while (!pq.empty()) + + { + + minim = pq.top(); + + pq.pop(); + + + + int next = minim.second.second; + + + + if (viz[next] == 0) + + { + + viz[next] = 1; + + sol.push_back(minim.second); + + cost_total = cost_total - minim.first; + + + + for (int i = 0; i < L[next].size(); i++) + + { + + pq.push({-L[next][i].second, {next, L[next][i].first}}); + + } + + } + + + + } + + + + fout << cost_total << "\n" << sol.size() << "\n"; + + for (int i = 0; i < sol.size(); i++) + + { + + fout << sol[i].first << " " << sol[i].second << "\n"; + + } + + + + fin.close(); + + return 0; + +} diff --git a/mst/prim/a4b81a40-1400-4098-af73-ea7ed592974b.cpp b/mst/prim/a4b81a40-1400-4098-af73-ea7ed592974b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b59915e987b2394c61ff6663bd70072c1dc7b783 --- /dev/null +++ b/mst/prim/a4b81a40-1400-4098-af73-ea7ed592974b.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + + + +struct muchie { + + int nod, tata, cost; + + + + muchie(int nn = 0, int tt = 0, int cc = 0) { + + nod = nn, tata = tt, cost = cc; + + } + +}; + + + +int n, m, nm, capm; + +int d[200000 + 5]; + +muchie apm[200000 + 5]; + +vector v[200000 + 5]; + +priority_queue, greater> pq; + + + +bool operator > (muchie m1, muchie m2) { + + return m1.cost > m2.cost; + +} + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int x, y, z; + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= n; i++) + + d[i] = 2000000000 + 5; + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &z); + + v[x].push_back(muchie(y, x, z)); + + v[y].push_back(muchie(x, y, z)); + + } + + + + pq.push(muchie(1, 0, 0)); + + nm = 0; + + while(!pq.empty() && nm < n - 1) { + + muchie crt = pq.top(); + + pq.pop(); + + if(d[crt.nod] <= crt.cost) + + continue; + + + + d[crt.nod] = crt.cost; + + capm += crt.cost; + + if(crt.tata) + + apm[++nm] = crt; + + + + for(muchie vec: v[crt.nod]) + + if(d[vec.nod] == 2000000000 + 5) + + pq.push(vec); + + } + + + + printf("%d\n%d\n", capm, nm); + + for(int i = 1; i <= nm; i++) + + printf("%d %d\n", apm[i].nod, apm[i].tata); + + + + return 0; + +} diff --git a/mst/prim/a4c61d84-d79b-465c-ba9b-c0d8cafa0605.cpp b/mst/prim/a4c61d84-d79b-465c-ba9b-c0d8cafa0605.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6014e952987ca3f6e94a4648700adb981c86c3d3 --- /dev/null +++ b/mst/prim/a4c61d84-d79b-465c-ba9b-c0d8cafa0605.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include + +using namespace std; + + + + +int n, m, h[400004], s, tata[200003], sol[200003]; + +typedef struct muchii{int x, y, z;} muc; +muc a[400004]; + +void citire(); + +bool cmp ( int b, int c){ + return ( a[b].z < a[c].z); +} + +int main(){ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + citire(); + + int cn, i, j, k, u = 0, r, w; + char rezidual[100]; + + for (cn = 1; cn <= m; cn++){ + r = h[cn]; + for (i = a[r].x; tata[i] != 0; i = tata[i]); + for (j = a[r].y; tata[j] != 0; j = tata[j]); + + if (i != j){ + s += a[r].z; + sol[++u] = r; + tata[i] = j; + if ( tata[a[r].x] > 0) + for (k = a[r].x; tata[k] != 0; k = w){ + w = tata[k]; + tata[k] = j; + } + + if (tata[a[r].y] > 0) + for (k = a[r].y; tata[k] != 0; k = w){ + w = tata[k]; + tata[k] = j; + } + + } + } + printf("%d\n%d\n", s, u); + for (i = 1; i <= u; i++) + printf("%d %d\n", a[sol[i]].x, a[sol[i]].y); + + return 0; +} + +void citire(){ + int i; + scanf("%d %d", &n, &m); + for (i = 1; i <= m; i++){ + scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].z); + h[i] = i; + } + sort(h+1, h+m+1, cmp ); +} diff --git a/mst/prim/a4e44247-6148-4478-9ea5-7e766a04bf84.cpp b/mst/prim/a4e44247-6148-4478-9ea5-7e766a04bf84.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c3ab8f0a8c6722a30037b805012b1fe80cba2cb --- /dev/null +++ b/mst/prim/a4e44247-6148-4478-9ea5-7e766a04bf84.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +using namespace std; + +using pii = pair; + +const int INF = 0x3f3f3f3f; + +int N, M; + +vector> adj; + + + + + +void Prim(const int &beg_node) + +{ + + int total_weight = 0; + + vector is_in_MST(N + 1); + + for (int i = 1; i <= N; ++i) + + { + + assert(!is_in_MST[i]); + + } + + vector key(N + 1, INF), parent(N + 1); + + set Q; + + + + Q.insert({0, beg_node}); + + key[beg_node] = 0; + + + + for (int node = 1; node <= N; ++node) + + { + + assert(!Q.empty()); + + + + total_weight += begin(Q)->first; + + int min_node = begin(Q)->second; + + assert(!is_in_MST[min_node]); + + is_in_MST[min_node] = true; + + Q.erase(begin(Q)); + + + + for (const pii &adj_node : adj[min_node]) + + { + + if (!is_in_MST[adj_node.second] && adj_node.first < key[adj_node.second]) + + { + + Q.erase({key[adj_node.second], adj_node.second}); + + key[adj_node.second] = adj_node.first; + + parent[adj_node.second] = min_node; + + Q.emplace(adj_node); + + } + + } + + } + + + + ofstream fout("apm.out"); + + + + fout << total_weight << "\n" << N - 1 << "\n"; + + for (int node = 1; node <= N; ++node) + + { + + if (node != beg_node) + + { + + fout << node << " " << parent[node] << "\n"; + + } + + } + +} + + + + + +int main() + +{ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + adj.resize(N + 1); + + + + while (M--) + + { + + int i, j, weight; + + fin >> i >> j >> weight; + + + + adj[i].push_back({weight, j}); + + adj[j].push_back({weight, i}); + + } + + + + Prim(1); + +} diff --git a/mst/prim/a50b345f-c9e2-4054-a795-da9d27995c71.cpp b/mst/prim/a50b345f-c9e2-4054-a795-da9d27995c71.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4292562dd4e4e46137f38d5ca339e89f5474d1a6 --- /dev/null +++ b/mst/prim/a50b345f-c9e2-4054-a795-da9d27995c71.cpp @@ -0,0 +1,300 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost,h[dim]; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim],Muchii; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +vector < edge > A; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + A.push_back({a,b,c}); + + } + + sort(A.begin(),A.end(), + + [&](edge X,edge Y) + + { + + return (X.val + + +using namespace std; + + + +const int N = 200005; + + + +int n, m; + +int cost[N]; + +vector>graf[N]; + +vector> ans; + +bool amfost[N]; + + + +struct cmp{ + + bool operator()(pair> a,pair> b){ + + return a.second.second > b.second.second; + + } + +}; + + + +priority_queue> , vector>> , cmp> coada; + + + +void calc(int nod, int last) { + + cost[nod] = 0; + + coada.push(make_pair(nod, make_pair(last, cost[nod]))); + + + + while(coada.empty() == false) { + + last = coada.top().second.first; + + nod = coada.top().first; + + coada.pop(); + + + + if(amfost[nod] == true) { + + continue; + + } + + + + ans.push_back(make_pair(nod, last)); + + amfost[nod] = true; + + + + for (int i = 0; i < (int)graf[nod].size(); ++i) { + + + + int to = graf[nod][i].first; + + int drum = graf[nod][i].second; + + + + if(cost[to] > drum && amfost[to] == false){ + + cost[to] = drum; + + coada.push(make_pair(to, make_pair(nod, cost[to]))); + + } + + + + } + + + + } + + + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= n; ++i) + + cost[i] = INT_MAX; + + + + int costMin = INT_MAX, nodMin; + + for (int i = 1; i <= m; ++i) { + + int a, b, c; + + fin >> a >> b >> c; + + graf[a].push_back(make_pair(b,c)); + + graf[b].push_back(make_pair(a,c)); + + + + if(costMin > c) { + + costMin = c; + + nodMin = a; + + } + + } + + calc(nodMin, 0); + + + + int sum = 0; + + for (int i = 1; i <= n; ++i) + + sum += cost[i]; + + + + fout << sum << '\n' << (int)ans.size() - 1<< '\n'; + + for (int i = 1; i < (int)ans.size(); ++i) + + fout << ans[i].first << " " << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/a639c111-7ddd-46b4-9dd9-5562403e6ab2.cpp b/mst/prim/a639c111-7ddd-46b4-9dd9-5562403e6ab2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91bc41b0d0ea79a7b32b72d1d7adae0f1f0d320e --- /dev/null +++ b/mst/prim/a639c111-7ddd-46b4-9dd9-5562403e6ab2.cpp @@ -0,0 +1,71 @@ +#include +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + + int N, M, a, b, c; + fin >> N >> M; + vector > g(N); + vector edges(M); + for(int i = 0; i < (int)(M); i++) { + fin >> a >> b >> c; + --a; + --b; + edges[i] = {a,b}; + g[a].push_back({c, i}); + g[b].push_back({c, i}); + } + + vector vis(N); + priority_queue, greater > pq; + int tc = 0; + vector tree; + bool start = true; + + pq.push({0, 0}); + while(!pq.empty()) { + auto P = pq.top(); pq.pop(); + auto e = edges[P.second]; + if (vis[e.first] && vis[e.second]) { + continue; + } + if (start) { + start = false; + } else { + tc += P.first; + tree.push_back(P.second); + } + int x = vis[e.first] ? e.second : e.first; + vis[x] = 1; + for(auto const &Q: g[x]) { + auto ne = edges[Q.second]; + int y = x ^ ne.first ^ ne.second; + if (!vis[y]) { + pq.push(Q); + } + } + } + + fout << tc << "\n"; + fout << tree.size() << "\n"; + for(auto i: tree) { + auto e = edges[i]; + fout << e.first + 1 << " " << e.second + 1 << "\n"; + } + + return 0; +} diff --git a/mst/prim/a6dd49c8-7b55-4d20-abf5-301d6b62439b.cpp b/mst/prim/a6dd49c8-7b55-4d20-abf5-301d6b62439b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..237af1ea98763737f308b6f231fdccf600bed1d0 --- /dev/null +++ b/mst/prim/a6dd49c8-7b55-4d20-abf5-301d6b62439b.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +vector > adj[200001]; + +bitset <200001> reached; + +pair minCost[200001]; + + + +void dijkstra(int start) { + + priority_queue > best; + + best.push(make_pair(0, start)); + + + + while (!best.empty()) { + + int front = best.top().second; + + long long cost = -best.top().first; + + best.pop(); + + + + reached[front] = 1; + + for (pair edge : adj[front]) + + if (!reached[edge.first]) + + if (edge.second <= minCost[edge.first].first) { + + minCost[edge.first].first = edge.second; + + minCost[edge.first].second = front; + + best.push(make_pair(-edge.second, edge.first)); + + } + + } + +} + + + +void addEdge(int x, int y, int c) { + + adj[x].push_back(make_pair(y, c)); + + adj[y].push_back(make_pair(x, c)); + +} + + + +int main() { + + int n, m, x, y, c; + + + + fin >> n >> m; + + for (int i = 1; i <= m; i++) { + + fin >> x >> y >> c; + + addEdge(x, y, c); + + } + + + + for (int i = 2; i <= n; i++) + + minCost[i].first = INT_MAX; + + + + dijkstra(1); + + + + long long result = 0; + + for (int i = 1; i <= n; i++) + + result += minCost[i].first; + + + + fout << result << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << minCost[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/a71b59a1-8fef-47c4-8c98-7b3001e3c171.cpp b/mst/prim/a71b59a1-8fef-47c4-8c98-7b3001e3c171.cpp new file mode 100644 index 0000000000000000000000000000000000000000..116708fadc3d0ae73dbe19625ffe2a405dd28b9e --- /dev/null +++ b/mst/prim/a71b59a1-8fef-47c4-8c98-7b3001e3c171.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +struct ura{ + + int nod,c; + + bool operator <(const ura &other)const{ + + return c v[200001]; + +multiset s; + +int cost[200001],parc[200001],cor[200001]; + +ura ans[200001]; + + + +void apm(int nodadaug){ + + int i,x,cs; + + ura nod; + + for(i=0;ics){ + + cor[x]=nodadaug; + + cost[x]=cs; + + s.insert(v[nodadaug][i]); + + } + + } + +} + + + +int main() + +{ + + ifstream cin("apm.in"); + + ofstream cout("apm.out"); + + + + int n,m,x,y,cs,i,costmin; + + cin>>n>>m; + + ura nod; + + for(i=1;i<=m;++i){ + + cin>>x>>y>>cs; + + nod.nod=y; + + nod.c=cs; + + v[x].push_back(nod); + + nod.nod=x; + + v[y].push_back(nod); + + } + + for(i=1;i<=n;++i) + + cost[i]=2e9; + + apm(1); + + parc[1]=1; + + costmin=0; + + int nod1; + + for(i=2;i<=n;++i){ + + nod1=s.begin()->nod; + + while(parc[nod1]==1){ + + s.erase(s.begin()); + + nod1=s.begin()->nod; + + } + + if(s.size()>0) + + s.erase(s.begin()); + + parc[nod1]=1; + + apm(nod1); + + costmin+=cost[nod1]; + + nod.nod=nod1; + + nod.c=cor[nod1]; + + ans[i]=nod; + + } + + cout< +using namespace std; +const int maxN = 2e5 + 5; +typedef tuple triplePair; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +int n, m, totalCost; +vector> graph[maxN], compressed; +bitset visited; + +class smallestCost { + public: + bool operator()(const triplePair &a, const triplePair &b) const { + return get<0>(a) > get<0>(b); + } +}; +priority_queue, smallestCost> available; + +void makeAvailable(int node) { + visited[node] = true; + for (unsigned i = 0; i < graph[node].size(); ++i) { + pair neighbour = graph[node][i]; + if (!visited[neighbour.first]) + available.emplace(make_tuple(neighbour.second, neighbour.first, node)); + } +} + +void calcNextMove() { + while (!available.empty() && visited[get<1>(available.top())]) + available.pop(); + if (available.empty()) + return; + + triplePair nextMove = available.top(); + available.pop(); + makeAvailable(get<1>(nextMove)); + + totalCost += get<0>(nextMove); + compressed.emplace_back(make_pair(get<2>(nextMove), get<1>(nextMove))); +} + +int main() { + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y, cost; + fin >> x >> y >> cost; + graph[x].emplace_back(make_pair(y, cost)); + graph[y].emplace_back(make_pair(x, cost)); + } + + makeAvailable(1); + while (!available.empty()) + calcNextMove(); + + fout << totalCost << "\n" << compressed.size() << "\n"; + for (pair edge : compressed) + fout << edge.first << " " << edge.second << "\n"; + return 0; +} diff --git a/mst/prim/a856c8f1-1b4f-4b7c-909f-0120a9c7cd70.cpp b/mst/prim/a856c8f1-1b4f-4b7c-909f-0120a9c7cd70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..129f4b67b78cd6b6d9b38df4a394f16c24c76a79 --- /dev/null +++ b/mst/prim/a856c8f1-1b4f-4b7c-909f-0120a9c7cd70.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue> pq; + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + used[1]=1; + + for(auto it:v[1]) + + { + + tie(x,c)=it; + + pq.push(make_tuple(-c,1,x)); + + } + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + tie(c,x,y)=pq.top();c=-c;; + + pq.pop(); + + if(used[y]) + + continue; + + used[y]=1; + + cost+=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + for(auto it:v[y]) + + { + + tie(x,c)=it; + + if(!used[x]) + + pq.push(make_tuple(-c,y,x)); + + } + + + + } + + g< + + +typedef std::tuple edge; + + + +class Task { + public: + void solve() { + read_input(); + get_result(); + print_output(); + } + + int n, m, cost_apm, root; + std::vector d, p; + std::vector G[200009]; + std::vector apm; + std::priority_queue, std::greater> pq; + std::bitset<200009> used; + + void read_input() { + std::cin >> n >> m; + + for (int i = 1, x, y, c; i <= m; ++i) { + std::cin >> x >> y >> c; + G[x].push_back(edge(c, y)); + G[y].push_back(edge(c, x)); + } + cost_apm = 0; + } + + void get_result() { + p = std::vector(n + 1, 0); + d = std::vector(n + 1, INT_MAX); + + root = 1; + d[root] = 0; + p[root] = 0; + pq.push(edge(d[root], root)); + + for (int i = 1; i <= n; ++i) { + std::tuple e; + int node; + do { + e = pq.top(); + pq.pop(); + + node = std::get<1>(e); + } while (used[node]); + + cost_apm += d[node]; + used[node] = 1; + + for (auto it = G[node].begin(); it != G[node].end(); ++it) { + int neighbour = std::get<1>(*it), cost = std::get<0>(*it); + + if (!used[neighbour] && cost < d[neighbour]) { + d[neighbour] = cost; + p[neighbour] = node; + pq.push(edge(d[neighbour], neighbour)); + } + } + } + } + + void print_output() { + std::cout << cost_apm << "\n"; + std::cout << n - 1 << "\n"; + for (int i = 1; i <= n; ++i) { + if (i != root) { + std::cout << i << " " << p[i] << "\n"; + } + } + } +}; + +int main() { + auto cin_buff = std::cin.rdbuf(); + auto cout_buff = std::cout.rdbuf(); + + std::ifstream fin("apm.in"); + std::cin.rdbuf(fin.rdbuf()); + + std::ofstream fout("apm.out"); + std::cout.rdbuf(fout.rdbuf()); + + Task *task = new Task(); + task->solve(); + delete task; + + std::cin.rdbuf(cin_buff); + std::cout.rdbuf(cout_buff); + return 0; +} diff --git a/mst/prim/aa930070-7b8d-4fdc-8c54-f78632ab019b.cpp b/mst/prim/aa930070-7b8d-4fdc-8c54-f78632ab019b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..282f11edc0a63764194a30b9fb0682ee90593560 --- /dev/null +++ b/mst/prim/aa930070-7b8d-4fdc-8c54-f78632ab019b.cpp @@ -0,0 +1,132 @@ +#include + + + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector >adj[200000 + 5]; + +vectorkey(200000 + 5, 1e9); + +priority_queue, vector >, greater > > pq; + +int parent[200000 + 5]; + +bool inMST[200000 + 5]; + +int n, m; + + + +int main() + +{ + + ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); + + + + fin>>n>>m; + + + + for(int i = 1; i <= m; i++) + + { + + int u, v, weight; + + fin>>u>>v>>weight; + + + + adj[u].push_back(make_pair(weight, v)); + + adj[v].push_back(make_pair(weight, u)); + + } + + + + pq.push(make_pair(0, 1)); + + key[1] = 0; + + + + while(!pq.empty()) + + { + + int u = pq.top().second; + + pq.pop(); + + + + inMST[u] = true; + + + + for(int i = 0; i < adj[u].size(); i++) + + { + + int v = adj[u][i].second; + + int weight = adj[u][i].first; + + + + if(!inMST[v] && key[v] > weight) + + { + + key[v] = weight; + + pq.push(make_pair(key[v], v)); + + parent[v] = u; + + } + + } + + } + + int s = 0; + + + + for(int i = 1; i <= n; i++) + + s += key[i]; + + + + fout< +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 1005; + +const int NMAX = 200005; + + + +vector < vector < pair < int, int > > > costuri(NMAX); + +vector < int > vx; + +vector < int > vy; + +int tati[NMAX], N, M; + +bool vizitat[NMAX]; + +int dist[NMAX]; + +long long cost_minim; + +int nod_start = -1; + + + +set < pair < int, int > > heap; + + + +void init() + +{ + + for(int i = 0; i < NMAX; i++) + + dist[i] = INFINIT; + +} + + + +void citire() + +{ + + fin >> N >> M; + + int x, y, cost; + + int cost_min = 1005; + + for(int i = 0; i < M; i++) + + { + + fin >> x >> y >> cost; + + pair < int, int > pair_1 = make_pair(y, cost); + + pair < int, int > pair_2 = make_pair(x, cost); + + costuri[x].push_back(pair_1); + + costuri[y].push_back(pair_2); + + if(cost < cost_min) + + { + + cost_min = cost; + + nod_start = x; + + } + + } + + init(); + +} + + + +void Prim() + +{ + + for(unsigned int i = 0; i < costuri[nod_start].size(); i++) + + { + + tati[costuri[nod_start][i].first] = nod_start; + + heap.insert( {costuri[nod_start][i].second, costuri[nod_start][i].first} ); + + dist[costuri[nod_start][i].first] = costuri[nod_start][i].second; + + } + + vizitat[nod_start] = 1; + + + + while(!heap.empty()) + + { + + int mini = heap.begin() -> second; + + int cost = heap.begin() -> first; + + heap.erase(heap.begin()); + + + + if(vizitat[mini]) + + continue; + + cost_minim += cost; + + vizitat[mini] = 1; + + vx.push_back(tati[mini]); vy.push_back(mini); + + for(unsigned int k = 0; k < costuri[mini].size(); k++) + + if(!vizitat[costuri[mini][k].first]) + + { + + if(costuri[mini][k].second < dist[costuri[mini][k].first]) + + { + + dist[costuri[mini][k].first] = costuri[mini][k].second; + + tati[costuri[mini][k].first] = mini; + + } + + heap.insert( {costuri[mini][k].second, costuri[mini][k].first} ); + + } + + } + +} + + + +void afisare() + +{ + + fout << cost_minim << '\n'; + + fout << N - 1 << '\n'; + + for(int i = 0; i < N - 1; i++) + + fout << vx[i] << ' ' << vy[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + Prim(); + + afisare(); + + return 0; + +} diff --git a/mst/prim/ad81c5c1-506a-4351-9aac-53be3135f872.cpp b/mst/prim/ad81c5c1-506a-4351-9aac-53be3135f872.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d34c3fd4be04b23f83a93692ec545b5d9a04e5f6 --- /dev/null +++ b/mst/prim/ad81c5c1-506a-4351-9aac-53be3135f872.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + fin >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + fin >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0; + + for(int i = 1; i <= n; i ++) + + { + + + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(!q.empty()) + + { + + + + pair least = q.top(); + + int node = least.second; + + + + q.pop(); + + + + if(viz[node] == 0) + + continue; + + viz[node] = 0; + + cost += least.first; + + + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + fout< + + + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int n,m; + +int cost; + +bool ap[200005]; + +vector >nod[200005],ans; + +priority_queue > ,vector > >,greater > > >pq; + +void add_edges_of(int x) + +{ + + + + ap[x]=true; + + int y,c; + + for(int i=0;i>n>>m; + + int c,x,y; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + nod[x].push_back({y,c}); + + nod[y].push_back({x,c}); + + } + + compute_answer(); + + + + fout< +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +vector < pair >v[200010],vec; + +priority_queue< pair >,vector< pair > >,greater< pair > > >pq; + +pair > tt; + +int n,m,a,b,c; + +bool viz[200010]; + +long long cost; + +int main() + +{ + + fin>>n>>m; + + for(;m;m--) + + { + + fin>>a>>b>>c; + + v[a].push_back(make_pair(c,b)); + + v[b].push_back(make_pair(c,a)); + + } + + viz[1]=1; + + for(auto it:v[1]) + + { + + pq.push(make_pair(it.first,make_pair(1,it.second))); + + } + + + + for(int i=1;i<=n-1;i++) + + { + + while(viz[pq.top().second.second]) + + pq.pop(); + + tt=pq.top(); + + cost+=tt.first; + + vec.push_back(make_pair(tt.second.first,tt.second.second)); + + viz[tt.second.second]=1; + + for(auto it:v[tt.second.second]) + + { + + if(viz[it.second]==0) + + { + + pq.push(make_pair(it.first,make_pair(tt.second.second,it.second))); + + } + + } + + + + + + } + + fout< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +const int N = 200005, INF = INT_MAX; + + + +vector> g[N]; + +bool vizitat[N]; + +int CostMin[N]; + +int tata[N]; + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + for (int i = 0; i < m; i++) + + { + + int a, b, c; + + fin >> a >> b >> c; + + g[a].push_back({ b, c }); + + g[b].push_back({ a, c }); + + } + + + + for (int i = 2; i <= n; i++) + + { + + CostMin[i] = INF; + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + + + int CostTotal = 0; + + while (!heap.empty()) + + { + + + + int nod = heap.begin()->second; + + CostTotal += heap.begin()->first; + + heap.erase(heap.begin()); + + vizitat[nod] = true; + + + + for (auto neigh : g[nod]) + + { + + int NodVecin = neigh.first; + + int CostVecin = neigh.second; + + + + if (!vizitat[NodVecin] && CostVecin < CostMin[NodVecin]) + + { + + if (CostMin[NodVecin] != INF) + + { + + heap.erase(heap.find({ CostMin[NodVecin], NodVecin })); + + } + + + + heap.insert({ CostVecin, NodVecin }); + + CostMin[NodVecin] = CostVecin; + + tata[NodVecin] = nod; + + } + + } + + } + + + + fout << CostTotal << '\n' << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + { + + fout << i << ' ' << tata[i] << '\n'; + + } + +} diff --git a/mst/prim/af1fa8be-be36-444f-8857-9789ff069d97.cpp b/mst/prim/af1fa8be-be36-444f-8857-9789ff069d97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f92cda63bb6743dc284d4b9db423f6aad5854690 --- /dev/null +++ b/mst/prim/af1fa8be-be36-444f-8857-9789ff069d97.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +struct ura + +{ + + int nod,cost; + + bool operator<(const ura &other) const{ + + return cost s; + +vector lista[200001]; + +int v[2000001]; + +int cor[200002]; + +bool used[200002]; + +void intro_apm(int nod) + +{ + + int i; + + for(i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + in>>n1>>n2>>cost; + + lista[n2].push_back({n1,cost}); + + lista[n1].push_back({n2,cost}); + + } + + for(i=1;i<=n;i++) + + { + + v[i]=10001; + + } + + intro_apm(1); + + used[1]=1; + + int nr,costotal=0; + + for(i=2;i<=n;i++) + + { + + nr=s.begin()->nod; + + while(used[nr]==1) + + { + + s.erase(s.begin()); + + nr=s.begin()->nod; + + } + + used[nr]=1; + + intro_apm(nr); + + costotal+=v[nr]; + + sol[i-1]={nr,cor[nr]}; + + } + + out< +#include +#include +#include +using namespace std; +int sum; + +int cost[200003]; + +int returns[200003]; + + + +bool marked[200003]; + + + +int N, M; + + + +struct Edge + +{ + + int to; + + int cost; + +}; + + + +vector G[200003]; + +class CompareClass + +{ + +public: + + bool operator() (const Edge& a, const Edge& b) const + + { + + if( a.cost < b.cost) + + return 0; + + return 1; + + } + +}; + + + +priority_queue, CompareClass> pq; + + + +void read(){ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + + + int X, Y, C; + + + + Edge aux; + + + + while( M-- ) + + { + + fin >> X >> Y >> C; + + aux.cost = C; + + aux.to = Y; + + G[X].push_back(aux); + + aux.to = X; + + G[Y].push_back(aux); + + } + + + + fin.close(); + +} + + + +void write(){ + + ofstream fout("apm.out"); + + fout << sum << "\n"; + + fout << N-1 << "\n"; + + + + for( int i = 2; i <= N; i++ ) + + fout << i << " " << returns[i] << "\n"; + + + + fout.close(); + +} + + + +int main() + +{ + + read(); + + + + marked[1] = true; + + + + + + for( int i = 1; i <= N; i++ ) + + cost[i] = 5000; + + + + Edge aux; + + for( int i = 0; i < G[1].size(); i++ ) + + { + + if( G[1][i].cost < cost[G[1][i].to] ) + + { + + returns[G[1][i].to] = 1; + + cost[G[1][i].to] = G[1][i].cost; + + aux.to = G[1][i].to; + + aux.cost =G[1][i].cost; + + pq.push(aux); + + } + + } + + + + int i; + + + + for( int j = 1; j < N; j++ ) + + { + + while( marked[pq.top().to] ) + + pq.pop(); + + + + aux = pq.top(); + + i = aux.to; + + marked[i] = 1; + + sum += cost[i]; + + for( int k = 0; k < G[i].size(); k++ ){ + + if( !marked[G[i][k].to] ){ + + if( cost[G[i][k].to] > G[i][k].cost ) + + { + + returns[G[i][k].to] = i; + + cost[G[i][k].to] = G[i][k].cost; + + aux.to = G[i][k].to; + + aux.cost = G[i][k].cost; + + pq.push(aux); + + } + + } + + } + + } + + + + write(); + + + + return 0; + + + +} diff --git a/mst/prim/b0bf474c-191c-4993-954e-81c60c9f3c7e.cpp b/mst/prim/b0bf474c-191c-4993-954e-81c60c9f3c7e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e92def66d03ba162638973af5a64969d49b34eb0 --- /dev/null +++ b/mst/prim/b0bf474c-191c-4993-954e-81c60c9f3c7e.cpp @@ -0,0 +1,192 @@ +# include +# include +# include + + +using namespace std; + + + +FILE *f = freopen("apm.in", "r", stdin); + +FILE *g = freopen("apm.out", "w", stdout); + + + +const int N_MAX = 200001; + + + +struct vecin{ + + int capat; + + int cost; + + + + vecin (int &a, int &b) + + { + + this -> capat= a; + + this -> cost= b; + + } + +}; + + + +struct muchie{ + + int capat1; + + int capat2; + + int cost; + + + + muchie(int &a, int &b, int &c) + + { + + this -> capat1 = a; + + this -> capat2= b; + + this -> cost= c; + + } + + + + const bool operator <(const muchie &other) const + + { + + return (this ->cost > other.cost); + + } + +}; + + + +int n, m, sol; + +int rez[N_MAX]; + +vector G[N_MAX]; + +vector solutie; + +priority_queue candidati; + + + +int main() + +{ + + scanf("%d %d", &n, &m); + + + + for (int i=1; i<=m; i++) + + { + + int x, y, c; + + scanf("%d %d %d", &x, &y, &c); + + + + G[x].push_back(vecin(y,c)); + + G[y].push_back(vecin(x,c)); + + } + + rez[1] = true; + + sol = 1; + + + + for (vecin i : G[1]) + + candidati.push(muchie (sol, i.capat, i.cost)); + + + + sol = 0; + + + + while (!candidati.empty()) + + { + + muchie v = candidati.top(); + + candidati.pop(); + + + + if (!rez[v.capat2]) + + { + + int nod_nou = v.capat2; + + + + rez[nod_nou] = true; + + sol += v.cost; + + + + solutie.push_back(vecin(v.capat1, v.capat2)); + + + + for (vecin i :G[nod_nou]) + + { + + if (!rez[i.capat]) + + { + + candidati.push(muchie(nod_nou, i.capat, i.cost)); + + } + + } + + } + + } + + + + printf("%d\n", sol); + + printf("%u\n", solutie.size()); + + for (int i=0; i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct road { + + int nod1, nod2, cost; + + inline bool operator < (const road &other) const + + { + + return cost > other.cost; + + } + +}; + + + +struct { + + int nod1, nod2; + +}muchii[200005]; + + + + + +int nr_muchii, nr_noduri, total, f; + +priority_queue heap; + +int root[200005], depth[200005]; + + + +void citire() + +{ + + fin>>nr_noduri>>nr_muchii; + + for(int i=1; i<=nr_muchii; i++) + + { + + road muchie; + + fin>>muchie.nod1>>muchie.nod2>>muchie.cost; + + heap.push(muchie); + + } + +} + + + +void join(int n, int m) + +{ + + if(depth[n] > depth[m]) + + { + + root[m]=n; + + } + + if(depth[n] < depth[m]) + + { + + root[n]=m; + + } + + if(depth[n] == depth[m]) + + { + + root[m]=n; + + depth[n]++; + + } + +} + + + +int findRoot(int n) + +{ + + if(root[n]==0) + + return n; + + else + + return findRoot(root[n]); + +} + + + +void apm_kruskal() + +{ + + while(!heap.empty()) + + { + + road curent=heap.top(); + + heap.pop(); + + int r1, r2; + + r1=findRoot(curent.nod1); + + r2=findRoot(curent.nod2); + + if(r1!=r2) + + { + + join(r1, r2); + + total+=curent.cost; + + f++; + + muchii[f].nod1=curent.nod1; + + muchii[f].nod2=curent.nod2; + + } + + } + + + +} + + + +void afisare() + +{ + + fout< +#include + + + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n , m; + + + + + + + +bitset<200005>f; + +int cost[200005]; + +int previous[200005]; + +struct edge + +{ + + int x , d; + +}; + +vector edges[200005]; + + + +priority_queue >pq; + + + +void refresh() + +{ + + for(int i = 1; i <= n; i++) + + cost[i] = 2e9; + +} + + + +int addtomst(int node) + +{ + + for(auto &i:edges[node]) + + { + + if(f[i.x]==0) + + { + + if(i.d < cost[i.x]) + + { + + pq.push({-i.d,i.x}); + + cost[i.x] = i.d; + + previous[i.x] = node; + + } + + } + + } + +} + + + +int main() + +{ + + int a , b , d; + + in >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + in >> a >> b >> d; + + edges[a].push_back({b,d}); + + edges[b].push_back({a,d}); + + } + + refresh(); + + f[1] = 1; + + addtomst(1); + + for(int i = 2; i <= n; i++) + + { + + int node = pq.top().second; + + while(f[node]==1) + + { + + pq.pop(); + + node = pq.top().second; + + } + + f[node] = 1; + + addtomst(node); + + } + + int cazan = 0; + + for(int i = 2; i <= n; ++i) + + cazan += cost[i]; + + out << cazan << '\n'; + + out << n - 1 << '\n'; + + for(int i = 2; i <= n; ++i) + + out << previous[i] << " " << i << "\n"; + + return 0; + +} diff --git a/mst/prim/b1cebec0-5df0-4303-8228-a6d8304dd790.cpp b/mst/prim/b1cebec0-5df0-4303-8228-a6d8304dd790.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3ba4f508c2daca66901569a598ed01391a37f04 --- /dev/null +++ b/mst/prim/b1cebec0-5df0-4303-8228-a6d8304dd790.cpp @@ -0,0 +1,176 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim]; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + Solve1(); + + + + return 0; + +} diff --git a/mst/prim/b2c855b0-bbc2-42dd-b2b5-3f9bf8f29e16.cpp b/mst/prim/b2c855b0-bbc2-42dd-b2b5-3f9bf8f29e16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e23dc32a30a2f6f6683e8f0f461924fe97af46b --- /dev/null +++ b/mst/prim/b2c855b0-bbc2-42dd-b2b5-3f9bf8f29e16.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + priority_queue> heap; + + heap.push({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + cout << heap.top().second << ' ' << heap.top().first << '\n'; + + int nod = heap.top().second; + + int cost = heap.top().first; + + heap.pop(); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal -= cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + heap.push({ -y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/b2f41c6a-04a6-483e-a982-64c7a1e1a6f3.cpp b/mst/prim/b2f41c6a-04a6-483e-a982-64c7a1e1a6f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1fff58c4a41a80f7922f18ea6379a5b8868946a --- /dev/null +++ b/mst/prim/b2f41c6a-04a6-483e-a982-64c7a1e1a6f3.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int t[200010], n, m, cost; + +vector < pair > a[200010], rez; + +priority_queue< pair >, vector > >, greater > > >pq; + +pair< int, pair >x; + + + +int main() + +{ + + int i, j, k; + + fin>>n>>m; + + for(k=1; k<=m; k++) + + { + + fin>>i>>j>>cost; + + a[i].push_back({cost,j}); + + a[j].push_back({cost,i}); + + } + + for(auto it:a[1]) + + { + + pq.push( {it.first, {1, it.second}} ); + + } + + cost=0; + + t[1]=1; + + while(pq.empty()==0) + + { + + x = pq.top(); + + pq.pop(); + + if( t[x.second.second]==0 ) + + { + + t[x.second.second] = x.second.first; + + cost = cost + x.first; + + rez.push_back({x.second.first, x.second.second}); + + for(auto it: a[x.second.second]) + + { + + + + pq.push( {it.first, {x.second.second, it.second}} ); + + } + + } + + } + + t[1]=0; + + fout< +#include +#include + + + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct muchie + +{ + + int nod, cost; + + bool operator < (const muchie& other) const + + { + + return cost < other.cost; + + } + +}; + + + +multiset s; + +vector v[200000 +5]; + +int viz[200000 +5], C[200000 +5], n, m, total; + +int cor[200000 +5]; + +vector> sol; + + + +void ins_apm(int nod) + +{ + + viz[nod]=1; + + for(muchie& i : v[nod]) if(i.cost < C[i.nod]) + + { + + C[i.nod] = i.cost; + + s.insert(i); + + cor[i.nod] = nod; + + } + +} + + + +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y, c; + + in>>x>>y>>c; + + v[x].push_back({y, c}); + + v[y].push_back({x, c}); + + } + + for(int i=1; i<=n; i++) C[i]=2e9; + + + + ins_apm(1); + + for(int i=2; i<=n; i++) + + { + + int x = s.begin()->nod; + + while(viz[x]==1) + + { + + s.erase(s.begin()); + + x = s.begin()->nod; + + } + + s.erase(s.begin()); + + ins_apm(x); + + total += C[x]; + + sol.push_back(make_pair(x, cor[x])); + + } + + + + out< +#include +#include +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int N = 200001; + +vector> v[N], sol; + +bitset used; + +int n,m,x,y,c,cost; + +priority_queue>> pq; + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y>>c; + + v[x].push_back(make_pair(y,c)); + + v[y].push_back(make_pair(x,c)); + + } + + used[1]=1; + + for(auto it:v[1]) + + { + + x=it.first; + + c=it.second; + + pq.push(make_pair(-c,make_pair(1,x))); + + } + + int muchiiRamase=n-1; + + while(muchiiRamase) + + { + + c = -pq.top().first; + + x= pq.top().second.first; + + y= pq.top().second.second; + + pq.pop(); + + if(used[y]) + + continue; + + used[y]=1; + + cost+=c; + + sol.push_back(make_pair(x,y)); + + muchiiRamase--; + + for(auto it:v[y]) + + { + + x=it.first; + + c=it.second; + + if(!used[x]) + + pq.push(make_pair(-c,make_pair(y,x))); + + } + + + + } + + g< +#include +#include +#include +#include +using namespace std; +ifstream f ("apm.in"); +ofstream g ("apm.out"); +int n, m; +vector < pair> *vec; +int x, y, c; +int *d, *t, *viz; +int s; +void citire() +{ + f >> n >> m; + vec = new vector < pair>[n + 1]; + for (int i = 1; i <= m; ++i) + { + f >> x >> y >> c; + vec[x].push_back ({y, c}); + vec[y].push_back ({x, c}); + } + f.close(); +} +void golire_alocare_dinamica() +{ + d=new int[n+1]; + t=new int[n+1]; + viz=new int[n+1]; + for (int i = 1; i <= n; ++i) + { + d[i] = INT_MAX; + t[i] = 0; + viz[i] = 0; + } +} +void afisare() +{ + s = 0; + for (int i = 1; i <= n; i++) + s += d[i]; + g << s << '\n' << n - 1 << '\n'; + for (int i = 1; i <= n; i++) + if (t[i] != 0) + g << t[i] << " " << i << '\n'; + g.close(); +} +void prim() +{ + s = 1; + d[s] = 0; + + priority_queue < pair > q; + q.push ({-d[s], s}); + while (!q.empty() ) + { + x = q.top().second; + q.pop(); + viz[x] = 1; + for (auto i : vec[x]) + { + y = i.first; + c = i.second; + if (viz[y] == 0 && d[y] > c) + { + t[y] = x; + d[y] = c; + q.push ({-d[y], y}); + } + } + } +} +int main() +{ + citire(); + + golire_alocare_dinamica(); + + prim(); + + afisare(); + + return 0; +} diff --git a/mst/prim/b440dc4e-3781-4f2e-864a-6b33006d7701.cpp b/mst/prim/b440dc4e-3781-4f2e-864a-6b33006d7701.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b156ba480b21ee8390dd57dfd39b9537384e3c37 --- /dev/null +++ b/mst/prim/b440dc4e-3781-4f2e-864a-6b33006d7701.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +const int N = 200005; + + + +int n, m; + +int cost[N]; + +vector>graf[N]; + +vector> ans; + +bool amfost[N]; + + + +struct cmp{ + + bool operator()(pair> a,pair> b){ + + return a.second.second > b.second.second; + + } + +}; + + + +priority_queue> , vector>> , cmp> coada; + + + +void calc(int nod, int last) { + + cost[nod] = 0; + + coada.push(make_pair(nod, make_pair(last, cost[nod]))); + + + + while(coada.empty() == false) { + + last = coada.top().second.first; + + nod = coada.top().first; + + coada.pop(); + + + + + + + + if(amfost[nod] == true) { + + cout << "JUCK" << '\n'; + + continue; + + } + + + + ans.push_back(make_pair(nod, last)); + + amfost[nod] = true; + + + + for (int i = 0; i < (int)graf[nod].size(); ++i) { + + + + int to = graf[nod][i].first; + + int drum = graf[nod][i].second; + + + + if(cost[to] > drum && amfost[to] == false){ + + cost[to] = drum; + + + + coada.push(make_pair(to, make_pair(nod, cost[to]))); + + } + + + + } + + + + + + } + + + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= n; ++i) + + cost[i] = INT_MAX; + + + + int costMin = INT_MAX, nodMin; + + for (int i = 1; i <= m; ++i) { + + int a, b, c; + + fin >> a >> b >> c; + + graf[a].push_back(make_pair(b,c)); + + graf[b].push_back(make_pair(a,c)); + + + + if(costMin > c) { + + costMin = c; + + nodMin = a; + + } + + } + + calc(nodMin, 0); + + + + int sum = 0; + + for (int i = 1; i <= n; ++i) { + + sum += cost[i]; + + + + + + } + + + + fout << sum << '\n'; + + fout << (int)ans.size() - 1<< '\n'; + + for (int i = 1; i < (int)ans.size(); ++i) + + fout << ans[i].first << " " << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/b4a7fa4c-60aa-433f-8926-555fe6db3f93.cpp b/mst/prim/b4a7fa4c-60aa-433f-8926-555fe6db3f93.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b5254b04b4b9ac0569ff7e550461590935d3e97 --- /dev/null +++ b/mst/prim/b4a7fa4c-60aa-433f-8926-555fe6db3f93.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, cost; + +int parent[200005], card[200005]; + +bool ap[200005]; + +vector < pair< int, int > > nod[200005], ans; + +priority_queue < pair < int, pair< int, int > >, vector < pair < int, pair< int, int > > >, greater < pair < int, pair< int, int > > > > pq; + + + +void add_edges(int x){ + + ap[x] = true; + + int y, c; + + for(auto it: nod[x]){ + + y = it.first; + + c = it.second; + + if(ap[y]) + + continue; + + pq.push({c, {y, x}}); + + } + +} + + + +void answer(){ + + add_edges(1); + + int c, x, y; + + while(ans.size() != n-1){ + + c = pq.top().first; + + y = pq.top().second.first; + + x = pq.top().second.second; + + pq.pop(); + + if(ap[y]) + + continue; + + cost += c; + + ans.push_back({x, y}); + + add_edges(y); + + } + +} + + + +void read(){ + + f>>n>>m; + + int c, x, y; + + for(int i = 1; i <= m; ++i){ + + f>>x>>y>>c; + + nod[x].push_back({y, c}); + + nod[y].push_back({x, c}); + + } + + answer(); + + g< + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +typedef pair < int, int > Pair; + +int N, M, ans; + +vector < Pair > G[200002]; + +list < Pair > sol; + + + +void Read () { + + fin >> N >> M; + + while (M --) { + + int u, v, wt; + + fin >> u >> v >> wt; + + G[u].push_back ({v, wt}); + + G[v].push_back ({u, wt}); + + } + +} + + + +void Prim () { + + priority_queue < Pair, vector < Pair > , greater < Pair > > Heap; + + vector < int > key (N + 2, INT_MAX), + + parent (N + 2); + + vector < bool > viz (N + 2, 0); + + Heap.push ({0, 1}); + + key[1] = 0; + + while (!Heap.empty()) { + + int u = Heap.top().second; + + Heap.pop(); + + viz[u] = true; + + for (auto it : G[u]) { + + int v = it.first, weight = it.second; + + if (viz[v] == false && key[v] > weight) { + + key[v] = weight; + + Heap.push ({key[v], v}); + + parent[v] = u; + + } + + } + + } + + for (int i = 2; i <= N; i ++) { + + sol.push_back ({parent[i], i}); + + ans += key[i]; + + } + +} + + + +void Solve () { + + Read(); + + Prim(); + + fout << ans << '\n' << N - 1 << '\n'; + + for (auto it : sol) + + fout << it.first << ' ' << it.second << '\n'; + +} + + + +int main() { + + Solve(); + + return 0; + +} diff --git a/mst/prim/b505d0ef-4858-4e7c-b5ac-a5675cf358a4.cpp b/mst/prim/b505d0ef-4858-4e7c-b5ac-a5675cf358a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0297800a4adc33ee68b39724ffd9dab252676284 --- /dev/null +++ b/mst/prim/b505d0ef-4858-4e7c-b5ac-a5675cf358a4.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct road { + + int nod1, nod2, cost; + + inline bool operator < (const road &other) const + + { + + return cost > other.cost; + + } + +}; + + + +struct { + + int nod1, nod2; + +}muchii[200005]; + + + + + +int nr_muchii, nr_noduri, total, f; + +priority_queue heap; + +int root[200005], depth[200005]; + + + +void citire() + +{ + + fin>>nr_noduri>>nr_muchii; + + for(int i=1; i<=nr_muchii; i++) + + { + + road muchie; + + fin>>muchie.nod1>>muchie.nod2>>muchie.cost; + + heap.push(muchie); + + } + +} + + + +void join(int n, int m) + +{ + + if(depth[n] > depth[m]) + + { + + root[m]=n; + + } + + if(depth[n] < depth[m]) + + { + + root[n]=m; + + } + + if(depth[n] == depth[m]) + + { + + root[m]=n; + + depth[n]++; + + } + +} + + + +int findRoot(int n) + +{ + + if(root[n]==0) + + return n; + + else + + return findRoot(root[n]); + +} + + + +void apm_kruskal() + +{ + + while(!heap.empty()) + + { + + road curent=heap.top(); + + heap.pop(); + + int r1, r2; + + r1=findRoot(curent.nod1); + + r2=findRoot(curent.nod2); + + if(r1!=r2) + + { + + join(r1, r2); + + total+=curent.cost; + + f++; + + muchii[f].nod1=curent.nod1; + + muchii[f].nod2=curent.nod2; + + } + + } + + + +} + + + +void afisare() + +{ + + fout< +#include +#include +#include +using namespace std; +int sum; + +int cost[200003]; + +int returns[200003]; + + + +bool marked[200003]; + + + +int N, M; + + + +struct Edge + +{ + + int to; + + int cost; + +}; + + + +vector G[200003]; + +class CompareClass + +{ + +public: + + bool operator() (const Edge& a, const Edge& b) const + + { + + if( a.cost < b.cost) + + return 0; + + return 1; + + } + +}; + + + +priority_queue, CompareClass> pq; + + + +void read(){ + + ifstream fin("apm.in"); + + + + fin >> N >> M; + + + + int X, Y, C; + + + + Edge aux; + + + + while( M-- ) + + { + + fin >> X >> Y >> C; + + aux.cost = C; + + aux.to = Y; + + G[X].push_back(aux); + + aux.to = X; + + G[Y].push_back(aux); + + } + + + + fin.close(); + +} + + + +void write(){ + + ofstream fout("apm.out"); + + fout << sum << "\n"; + + fout << N-1 << "\n"; + + + + for( int i = 2; i <= N; i++ ) + + fout << i << " " << returns[i] << "\n"; + + + + fout.close(); + +} + + + +int main() + +{ + + read(); + + + + marked[1] = true; + + + + + + for( int i = 1; i <= N; i++ ) + + cost[i] = 5000; + + + + Edge aux; + + for( int i = 0; i < G[1].size(); i++ ) + + { + + if( G[1][i].cost < cost[G[1][i].to] ) + + { + + returns[G[1][i].to] = 1; + + cost[G[1][i].to] = G[1][i].cost; + + aux.to = G[1][i].to; + + aux.cost =G[1][i].cost; + + pq.push(aux); + + } + + } + + + + int i; + + + + for( int j = 1; j < N; j++ ) + + { + + while( marked[pq.top().to] ) + + pq.pop(); + + + + aux = pq.top(); + + i = aux.to; + + marked[i] = 1; + + sum += cost[i]; + + for( int k = 0; k < G[i].size(); k++ ){ + + if( !marked[G[i][k].to] ){ + + if( cost[G[i][k].to] > G[i][k].cost ) + + { + + returns[G[i][k].to] = i; + + cost[G[i][k].to] = G[i][k].cost; + + aux.to = G[i][k].to; + + aux.cost = G[i][k].cost; + + pq.push(aux); + + } + + } + + } + + } + + + + write(); + + + + return 0; + + + +} diff --git a/mst/prim/b698141c-8a68-4ab8-aaeb-54664ed2d28a.cpp b/mst/prim/b698141c-8a68-4ab8-aaeb-54664ed2d28a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0cf3bd9f884e176440bf6da83aa5866b705537d --- /dev/null +++ b/mst/prim/b698141c-8a68-4ab8-aaeb-54664ed2d28a.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + f >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + f >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0, length = n; + + for(int i = 1; i <= n; i ++) + + { + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(length) + + { + + pair least = q.top(); + + int node = least.second; + + q.pop(); + + if(viz[node] == 0) + + continue; + + length -- ; + + viz[node] = 0; + + cost += least.first; + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + g< +#include +#include +#include + + + +using namespace std; + +vector >> graph; +vector > apm; +priority_queue >> heap; +vector visited; +int n; + +void read(){ + ifstream fin("apm.in"); + + int m, x, y, c; + fin>>n>>m; + graph.resize(n); + visited.resize(n, false); + while(m--){ + fin>>x>>y>>c; + graph[x-1].emplace_back(y-1, c); + graph[y-1].emplace_back(x-1, c); + } +} + +int main() { + ofstream fout("apm.out"); + read(); + int total_cost = 0; + for(auto &e: graph[0]) + heap.push({-e.second, {0, e.first}}); + visited[0] = true; + while(!heap.empty()){ + auto top = heap.top(); + heap.pop(); + int cost = -top.first; + int node = top.second.second; + if(!visited[node]){ + visited[node] = true; + apm.push_back(top.second); + total_cost += cost; + for(auto &e: graph[node]) + heap.push({-e.second, {node, e.first}}); + } + } + fout< +#include + + +using namespace std; + + + +ifstream in ("apm.in"); + +ofstream out ("apm.out"); + + + + + +vector>adiacenta[200000]; + +int n,m; + +int total = 0; + +priority_queue< pair, vector >, greater> > pq; + +vectorvizitat; + +vector parinte; + +vector cost; + + + + + +void Prim(int i) + +{ + + + + pq.push(make_pair(0, i)); + + cost[i] = 0; + + + + + + int varf; + + while (!pq.empty()) + + { + + varf = pq.top().second; + + vizitat[varf] = true; + + + + + + + + pq.pop(); + + + + for (pair e : adiacenta[varf]) + + { + + int vecin = e.first; + + int c = e.second; + + + + if (!vizitat[vecin] && c < cost[vecin]) + + { + + + + + + + + cost[vecin] = c; + + pq.push(make_pair(cost[vecin], vecin)); + + + + + + + + parinte[vecin] = varf; + + + + } + + + + } + + + + + + } + + + + + +} + + + + + +int main() + +{ + + + + + + in>>n>>m; + + int x,y,c; + + + + for(int i = 0; i>x>>y>>c; + + + + adiacenta[x-1].push_back(make_pair(y-1, c)); + + adiacenta[y-1].push_back(make_pair(x-1, c)); + + + + } + + + + vizitat.assign(n, false); + + cost.assign(n,10000); + + parinte.assign(n,-1); + + + + Prim(0); + + + + + + + + for(int i = 1; i +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fi("apm.in"); + +ofstream fo("apm.out"); + + + +int n,m,nod,s,k; + +int cost[200005],b[200005],dist[200005]; + +bool sel[200005]; + + + +vector >a[200005]; + +priority_queue > q; + + + +int main() + +{ + + fi>>n>>m; + + + + for(int i=1;i<=m;i++) + + { + + int x,y,c; + + fi>>x>>y>>c; + + + + a[x].push_back({y,c}); + + a[y].push_back({x,c}); + + } + + + + for(int i=2; i<=n;i++) + + { + + dist[i]=INT_MAX; + + q.push({-INT_MAX,i}); + + } + + + + q.push({0,1}); + + + + while(1) + + { + + if(k==n) + + break; + + + + if(q.empty()) + + break; + + + + int i=q.top().second; + + q.pop(); + + + + if(!sel[i]) + + { + + sel[i]=true; + + k++; + + for(int j=0;ja[i][j].second and !sel[nod]) + + { + + dist[nod]=a[i][j].second; + + b[nod]=i; + + q.push({-dist[nod],nod}); + + } + + + + } + + } + + + + } + + + + for(int i=1;i<=n;i++) + + s+=dist[i]; + + + + fo< +#include + + +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +int N,M; + +int tata[200005], d[200005]; + +bool viz[200005]; + +int cost_total = 0; + +vector> Graph[200005]; + +auto Compare = [](pair &a, pair &b) + +{ + + return get<1>(a)>get<1>(b); + +}; + +priority_queue,vector>,decltype(Compare) > que(Compare); + + + +void initializare(int nod) + +{ + + d[nod] = 2e9; + + tata[nod]=0; + +} + + + +int main() + +{ + + in>>N>>M; + + int x,y,z; + + for(int i=1; i<=M; i++) + + { + + in>>x>>y>>z; + + Graph[x].push_back({y,z}); + + Graph[y].push_back({x,z}); + + } + + for(int i=1; i<=N; i++) + + initializare(i); + + d[1] = 0; + + que.push({1,0}); + + + + while(!que.empty()) + + { + + int u = que.top().first; + + que.pop(); + + viz[u] = true; + + for(auto &vecin:Graph[u]) + + { + + int v = vecin.first; + + int w = vecin.second; + + if(!viz[v] && w + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +const int dim=2e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,n,m,a,b,c,cost,h[dim]; + + + +vector < int > T(dim,0),viz(dim,0); + +vector < pi > V[dim],Muchii; + + + +struct edge + +{ + + int st,dr,val; + +}; + + + +vector < edge > A; + + + +struct cmp + +{ + + bool operator()(edge X,edge Y) + + { + + if(X.val > Y.val) + + return 1; + + else + + { + + + + if(X.val==Y.val) + + return (X.st,cmp > minheap; + + + +void Solve1() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + V[a].push_back({b,c}); + + V[b].push_back({a,c}); + + } + + for(pi x:V[1]) + + minheap.push({1,x.first,x.second}); + + + + int cnt=0; + + viz[1]=1; + + while(cnt>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b>>c; + + A.push_back({a,b,c}); + + } + + sort(A.begin(),A.end(), + + [&](edge X,edge Y)->bool + + { + + return (X.val +#include +#include +#include + + +using namespace std; + + + +int N, M, viz[200005], cost_total; + +vector > L[200005], sol; + +priority_queue < pair > > pq; + + + +int main() + +{ + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> N >> M; + + + + int x,y,c; + + + + for (int i = 1; i <= M; i++) + + { + + fin >> x >> y >> c; + + L[x].push_back({y, c}); + + L[y].push_back({x, c}); + + } + + + + int r = 1; + + pair > minim; + + viz[r] = 1; + + for (int i = 0; i < L[r].size(); i++) + + { + + pq.push({-L[r][i].second, {r, L[r][i].first}}); + + } + + + + while (!pq.empty()) + + { + + minim = pq.top(); + + pq.pop(); + + + + int next = minim.second.second; + + + + if (viz[next] == 0) + + { + + viz[next] = 1; + + sol.push_back(minim.second); + + cost_total = cost_total - minim.first; + + + + for (int i = 0; i < L[next].size(); i++) + + { + + pq.push({-L[next][i].second, {next, L[next][i].first}}); + + } + + } + + + + } + + + + fout << cost_total << "\n" << sol.size() << "\n"; + + for (int i = 0; i < sol.size(); i++) + + { + + fout << sol[i].first << " " << sol[i].second << "\n"; + + } + + + + fin.close(); + + return 0; + +} diff --git a/mst/prim/bb7c1219-4505-4627-a017-75a489fd280c.cpp b/mst/prim/bb7c1219-4505-4627-a017-75a489fd280c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..088c07bf9431c4200ad0de5c6aae67eb8c248c67 --- /dev/null +++ b/mst/prim/bb7c1219-4505-4627-a017-75a489fd280c.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int t[200010], n, m, cost; + +vector < pair > a[200010], rez; + +priority_queue< pair >, vector > >, greater > > >pq; + +pair< int, pair >x; + + + +int main() + +{ + + int i, j, k; + + fin>>n>>m; + + for(k=1; k<=m; k++) + + { + + fin>>i>>j>>cost; + + a[i].push_back({cost,j}); + + a[j].push_back({cost,i}); + + } + + for(auto it:a[1]) + + { + + pq.push( {it.first, {1, it.second}} ); + + } + + cost=0; + + t[1]=1; + + while(pq.empty()==0) + + { + + x = pq.top(); + + pq.pop(); + + if( t[x.second.second]==0 ) + + { + + t[x.second.second] = x.second.first; + + cost = cost + x.first; + + rez.push_back({x.second.first, x.second.second}); + + for(auto it: a[x.second.second]) + + { + + if(t[it.second]==0) + + pq.push( {it.first, {x.second.second, it.second}} ); + + } + + } + + } + + t[1]=0; + + fout< +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n,m,viz[200005],cost; + +vector >graph[200005]; + +vector >sol; + +struct edge + +{ + + int x; + + int y; + + int c; + + + + bool operator<(const edge &other) const { + + if(c!=other.c) + + return c>other.c; + + return y>other.y; + + } + +}; + +priority_queue >q; + + + +void read() + +{ + + fin>>n>>m; + + int x,y,c; + + for(int i=0; i>x>>y>>c; + + graph[x].push_back(make_pair(y,c)); + + graph[y].push_back(make_pair(x,c)); + + } + +} + + + +void prim() + +{ + + viz[1]=1; + + int nr=graph[1].size(); + + for(int i=0; i + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +vector < vector < pair > > adjList; +int V, E; + +int main(){ + + fin >> V >> E; + + adjList.resize(V + 1); + + for(int e = 0; e < E; ++e){ + + int u, v, c; + fin >> u >> v >> c; + + adjList[u].push_back({v, c}); + adjList[v].push_back({u, c}); + } + + vector < pair > connEdge(V + 1, {0, INT_MAX}); + vector used(V + 1); + + set < pair > RB; + + used[1] = true; + for(pair &edge : adjList[1]) + connEdge[edge.first] = {1, edge.second}, RB.insert({edge.second, edge.first}); + + int mstCost = 0; + for(int it = 1; it < V; ++it){ + + + pair Find = *RB.begin(); + RB.erase(RB.begin()); + + int minCost = Find.first; + int addNode = Find.second; + + mstCost += minCost; + used[addNode] = true; + + for(pair &edge : adjList[addNode]) + if(!used[edge.first] and edge.second < connEdge[edge.first].second){ + + if(RB.count({connEdge[edge.first].second, edge.first})) + RB.erase({connEdge[edge.first].second, edge.first}); + + connEdge[edge.first] = {addNode, edge.second}; + RB.insert({edge.second, edge.first}); + } + } + + fout << mstCost << '\n' << V - 1 << '\n'; + + for(int node = 2; node <= V; ++node) + fout << node << ' ' << connEdge[node].first << '\n'; +} diff --git a/mst/prim/bec7ecea-5b3e-4c1d-bf24-6e832b0639da.cpp b/mst/prim/bec7ecea-5b3e-4c1d-bf24-6e832b0639da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46430ed5ebd9f12e6aeb3e95b433f5c4f462d766 --- /dev/null +++ b/mst/prim/bec7ecea-5b3e-4c1d-bf24-6e832b0639da.cpp @@ -0,0 +1,86 @@ +#include + +using namespace std; + +const int MAX_N = 200000; +const int MAX_M = 400000; +const int INF = 1000000000; + +bool marked[1+MAX_N]; + +struct Edge { + int a, b, c; + + int other(int x) { + return a ^ b ^ x; + } +} edges[1+MAX_M]; + +struct Node { + int nod, i; + bool operator< (const Node& x) const { + return edges[i].c < edges[x.i].c || + (edges[i].c == edges[x.i].c && nod < x.nod); + } +}; + +vector graph[1+MAX_N]; + +int bestEdge[1+MAX_N]; +set nodes; + +void markNode(int node) { + marked[node] = true; + for(auto it: graph[node]) { + int son = edges[it].other(node); + if(!marked[son] && edges[it].c < edges[bestEdge[son]].c) { + nodes.erase({son, bestEdge[son]}); + bestEdge[son] = it; + nodes.insert({son, bestEdge[son]}); + } + } + nodes.erase({node, bestEdge[node]}); +} + +int top; +int apmA[1+MAX_N], apmB[1+MAX_N]; + +int main() { + int N, M, sumapm = 0; + + FILE *fin = fopen("apm.in", "r"); + fscanf(fin, "%d%d", &N, &M); + + for(int i = 0; i < M; ++i) { + int a, b, c; + fscanf(fin, "%d%d%d", &a, &b, &c); + edges[i] = {a, b, c}; + graph[a].push_back(i); + graph[b].push_back(i); + } + edges[M] = {0, 0, INF}; + + for(int i = 1; i <= N; ++i) { + bestEdge[i] = M; + nodes.insert({i, bestEdge[i]}); + } + fclose(fin); + + markNode(1); + for(int i = 2; i <= N; ++i) { + int nod = nodes.begin()->nod, edge = nodes.begin()->i; + + sumapm += edges[edge].c; + apmA[top] = edges[edge].a; + apmB[top++] = edges[edge].b; + + markNode(nod); + } + + FILE *fout = fopen("apm.out", "w"); + fprintf(fout, "%d\n%d\n", sumapm, top); + for(int i = 0; i < top; ++i) + fprintf(fout, "%d %d\n", apmA[i], apmB[i]); + fclose(fout); + return 0; +} diff --git a/mst/prim/c2a19e7b-262d-4d2e-b2a8-6111c7e0bddb.cpp b/mst/prim/c2a19e7b-262d-4d2e-b2a8-6111c7e0bddb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9a05a014f7da6ba6ae815c1eb9abff4e0a7415d --- /dev/null +++ b/mst/prim/c2a19e7b-262d-4d2e-b2a8-6111c7e0bddb.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +struct pereche { + + int y, c; + +}; + + + +struct muchie { + + int x, y, c; + +}; + + + +struct comp { + + bool operator() (muchie u, muchie v) { + + return u.c > v.c; + + } + +}; + + + +int viz[200001], t[200001], d[200001]; + +int n, m, x, y, cost, nr, dm, im, ct, i, j; + + + +vector G[200001]; + +priority_queue, comp> q; + + + + + +int main() { + + fin >> n >> m; + + + + for (int i = 1; i <= m; i++) { + + fin >> x >> y >> cost; + + G[x].push_back({ y, cost }); + + G[y].push_back({ x, cost }); + + } + + + + for (int i = 2; i <= n; ++i) { + + d[i] = 1001; + + t[i] = 1; + + } + + + + for (int i = 0; i < G[1].size(); i++) { + + pereche pt = G[1][i]; + + d[pt.y] = pt.c; + + q.push({ 1, pt.y, pt.c }); + + } + + + + viz[1] = 1; + + nr = 1; + + ct = 0; + + + + while (nr < n) + + { + + muchie e; + + + + e = q.top(); q.pop(); + + + + if (viz[e.y] == 0) { + + dm = e.c; + + + + + + im = e.y; + + + + nr++; + + viz[im] = 1; + + ct = ct + dm; + + + + pereche pt; + + for (int i = 0; i < G[im].size(); ++i) { + + pt = G[im][i]; + + if (viz[pt.y] == 0 && d[pt.y] > pt.c) { + + d[pt.y] = pt.c; + + t[pt.y] = im; + + q.push({ im, pt.y, pt.c }); + + } + + } + + } + + } + + + + fout << ct << '\n'; + + fout << n - 1 << '\n'; + + for (i = 2; i <= n; i++) + + fout << i << ' ' << t[i] << '\n'; + +} diff --git a/mst/prim/c3770ca7-073f-40a2-936a-eab4bda52e56.cpp b/mst/prim/c3770ca7-073f-40a2-936a-eab4bda52e56.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00aa3810206336fdf702d77073450f4bacbfb51b --- /dev/null +++ b/mst/prim/c3770ca7-073f-40a2-936a-eab4bda52e56.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +typedef pair p; + + + +const int NMAX = 200002; + +const int oo = 0x3f3f3f3f; + + + +vector

g[NMAX]; + + + +int n, x, y, c, m, d[NMAX], t[NMAX], cost; + + + +bitset used; + + + +struct cmp + +{ + + bool operator()(const p& x, const p& y) const + + { + + return x.second > y.second; + + } + +}; + + + +priority_queue, cmp > q; + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + g[x].push_back({ y,c }); + + g[y].push_back({ x,c }); + + } + + fill(d + 2, d + n + 1, oo); + + + + q.push({ 1, 0 }); + + used[0] = 1; + + + + int vn = n; + + + + while (vn) + + { + + x = 0; + + while (used[x]) + + { + + x = q.top().first; + + q.pop(); + + } + + used[x] = 1; + + cost += d[x]; + + --vn; + + vector

::iterator it = g[x].begin(), f = g[x].end(); + + for (; it != f; ++it) + + if (it->second < d[it->first] && used[it->first] == 0) + + { + + t[it->first] = x; + + d[it->first] = it->second; + + q.push({ it->first, it->second }); + + } + + } + + + + fout << cost << "\n" << n - 1 << "\n"; + + + + for (int i = 2; i <= n; ++i) + + fout << i << " " << t[i] << "\n"; + + + + return 0; + +} diff --git a/mst/prim/c61044d8-b556-4ea6-9a9e-cd562a3942b9.cpp b/mst/prim/c61044d8-b556-4ea6-9a9e-cd562a3942b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..939f203d47b92e9f2e73428b86197798b99f7097 --- /dev/null +++ b/mst/prim/c61044d8-b556-4ea6-9a9e-cd562a3942b9.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("trees.in"); +ofstream fout("trees.out"); + + + +const int NR = 2e5 + 10 , oo = 2e9; +int n , m ; +vector < int > d ( NR , oo ) ; +struct node{ + int nod , cost , tata; +}; +struct cmp { + bool operator() ( node i , node j ) { + return i.cost > j.cost ; + } +}; +ifstream in("apm.in"); +ofstream out("apm.out"); +priority_queue < node,vector,cmp> pq; +vector < pair < int , int > > v [ NR ] , sol ; +int viz [ NR ] ; +signed main() { + int x , y , i , z ; + in >> n >> m ; + for ( i = 1 ; i <= m ; ++ i ) { + in >> x >> y >> z ; + v [ x ].emplace_back( make_pair( y , z ) ) ; + v [ y ].emplace_back( make_pair( x , z ) ) ; + } + d [ 1 ] = 0 ; + node temp ; + temp.cost = 0; + temp.nod = 1; + temp.tata = -1; + pq.push(temp); + int nod ; + long long sum = 0 ; + while ( !pq.empty() ) { + nod = pq.top().nod ; + node temp2 = pq.top(); + pq.pop(); + if ( viz [ nod ] ) + continue ; + sum += temp2.cost; + viz [ nod ] = 1 ; + sol.push_back(make_pair(temp2.nod,temp2.tata)); + for ( auto it : v [ nod ] ) { + if ( !viz [ it.first ] ) { + temp.nod = it.first ; + temp.cost = it.second ; + temp.tata = nod ; + pq.push(temp); + } + } + } + out << sum << '\n' ; + out << sol.size() - 1 << '\n' ; + for ( auto it : sol ) { + if ( it.second != -1 ) + out << it.second << ' ' << it.first << '\n' ; + } + return 0; +} diff --git a/mst/prim/c645f4dd-e37d-4e2c-b1bf-fc8d3e032971.cpp b/mst/prim/c645f4dd-e37d-4e2c-b1bf-fc8d3e032971.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cd65f51fe080dd84be23fa4e6df8dade28ae676 --- /dev/null +++ b/mst/prim/c645f4dd-e37d-4e2c-b1bf-fc8d3e032971.cpp @@ -0,0 +1,185 @@ +#include + + + + +using namespace std; + + + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +int n, m, t[200001], ct,viz[200001],d[200001]; + +struct pereche + +{ + + int y,c; + +}; + + + +struct muchie + +{ + + int x, y, c; + +}; + + + +struct cmp + +{ + + bool operator()(muchie u, muchie v) + + { + + return u.c > v.c; + + } + +}; + + + +priority_queue,cmp> q; + + + + + +vector G[200001]; + + + +void citeste() + +{ + + int x,y,i,cost; + + fin >> n >> m; + + for(i=1;i<=n;i++) + + d[i]=10000; + + for(i=1; i<=m; i++) + + { + + pereche p; + + fin >> x >> y >> cost; + + p={y,cost}; + + G[x].push_back(p); + + p={x,cost}; + + G[y].push_back(p); + + } + +} + + + +int main() + +{ + + int i,nr; + + muchie e; + + pereche p; + + + + citeste(); + + for(i=0;ip.c) + + { + + q.push({e.y, p.y,p.c}); + + d[p.y]=p.c; + + } + + } + + } + + } + + fout << ct << '\n'; + fout< +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + +int t[200010], n, m, cost; + +vector < pair > a[200010], rez; + +priority_queue< pair >, vector > >, greater > > >pq; + +pair< int, pair >x; + + + +int main() + +{ + + int i, j, k; + + fin>>n>>m; + + for(k=1; k<=m; k++) + + { + + fin>>i>>j>>cost; + + a[i].push_back({cost,j}); + + a[j].push_back({cost,i}); + + } + + for(auto it:a[1]) + + { + + pq.push( {it.first, {1, it.second}} ); + + } + + cost=0; + + t[1]=1; + + while(pq.empty()==0) + + { + + x = pq.top(); + + pq.pop(); + + if( t[x.second.second]==0 ) + + { + + t[x.second.second] = x.second.first; + + cost = cost + x.first; + + rez.push_back({x.second.first, x.second.second}); + + for(auto it: a[x.second.second]) + + { + + + + pq.push( {it.first, {x.second.second, it.second}} ); + + } + + } + + } + + t[1]=0; + + fout< +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = (int)vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + +} + + + + + + + +std::vector vecini[N]; + +int main() + +{ + + + + int n, m; + + in >> n >> m; + + while (m--) + + { + + Muchie m; + + in >> m; + + vecini[m.a].push_back({m.b, m.cost}); + + vecini[m.b].push_back({m.a, m.cost}); + + } + + dj::reset(n); + + apm::prim(vecini, 0); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/c70b9a97-9a36-4cee-a1e1-f23c8a74eae5.cpp b/mst/prim/c70b9a97-9a36-4cee-a1e1-f23c8a74eae5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d252ad8d24046b11a6b3939b71b8b43bc81e8323 --- /dev/null +++ b/mst/prim/c70b9a97-9a36-4cee-a1e1-f23c8a74eae5.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; + +ifstream fin( "apm.in" ); +ofstream fout( "apm.out" ); + +const int MaxN = 200002; + +vector G[MaxN]; +vector cost[MaxN]; +priority_queue>, vector>>, greater>>> q; +int viz[MaxN]; +int arb[MaxN][2]; + +void solve( int stNode ) { + int node, ne = 0, minc = 0; + + q.push( { 0, { stNode, stNode } } ); + while ( !q.empty() ) { + if ( !viz[q.top().second.second] ) { + node = q.top().second.second; + minc += q.top().first; + arb[ne][0] = node; + arb[ne++][1] = q.top().second.first; + viz[node] = 1; + for ( int i = 0; i < G[node].size(); ++i ) { + if ( !viz[G[node][i]] ) { + q.push( { cost[node][i], { node, G[node][i] } } ); + } + } + } else { + q.pop(); + } + } + fout << minc << "\n" << ne - 1 << "\n"; + for ( int i = 1; i < ne; ++i ) { + fout << arb[i][0] << " " << arb[i][1] << "\n"; + } +} + +int main() { + int n, m, i, x, y, c; + + fin >> n >> m; + for ( i = 0; i < m; ++i ) { + fin >> x >> y >> c; + G[x].push_back( y ); + G[y].push_back( x ); + cost[x].push_back( c ); + cost[y].push_back( c ); + } + solve( 1 ); + fin.close(); + fout.close(); + return 0; +} diff --git a/mst/prim/c71d603c-bd9f-4293-a205-1f61bee999a5.cpp b/mst/prim/c71d603c-bd9f-4293-a205-1f61bee999a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1788916023f1532bd3ea2bda34e9867e4e406e62 --- /dev/null +++ b/mst/prim/c71d603c-bd9f-4293-a205-1f61bee999a5.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >h; + +vector >v[200005]; + +int n,m,x,y,c,viz[200005]; + +struct soll + +{ + + int x,y; + +}sol[200005]; + +struct muchie + +{ + + int x,y,c; + +}mu[400005]; + +int prim(int nod) + +{ + + int i,cost=0,cc=0,j,nodc; + + viz[nod]=1; + + for(i=1;i<=n-1;i++) + + { + + for(j=0;j>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>c; + + v[x].push_back({c,y}); + + v[y].push_back({c,x}); + + } + + g< +#include +#include +#include +#include + +using namespace std; + +const int N = 2e5; +const int M = 4e5; +const int INF = 2e9; + +struct struct_node { + int node, cost; + bool operator < ( const struct_node &other )const { + return cost < other.cost; + } +}; + +multiset dist; +vector edge[N]; +bitset used (false); +int min_cost_to[N+1]; +int pairs[N+1]; pair result[N]; + +void insert_apm ( int node ) { + used[node] = true; + for ( int i = 0; i < (int) edge[node].size (); i ++ ) { + int pay = edge[node][i].cost; + int adj = edge[node][i].node; + + if ( pay < min_cost_to[adj] ) { + min_cost_to[adj] = pay; + dist.insert ( edge[node][i] ); + pairs[adj] = node; + } + } +} + +void init_INF () { + for ( int i = 1; i <= N; i ++ ) + min_cost_to[i] = INF; +} + +ifstream fin ( "apm.in" ); +ofstream fout ( "apm.out" ); + + +int main() + +{ + + int n, m; fin >> n >> m; + for ( int i = 0; i < m; i ++ ) { + int x, y, pay; fin >> x >> y >> pay; + edge[x].push_back ( {y, pay} ); + edge[y].push_back ( {x, pay} ); + } + + init_INF (); + + insert_apm ( 1 ); int final_dist = 0; + for ( int i = 1; i < n; i ++ ) { + int get_node = dist.begin () -> node; + while ( used[get_node] == true ) { + dist.erase ( dist.begin () ); + get_node = dist.begin () -> node; + } + + dist.erase ( dist.begin () ); + insert_apm ( get_node ); + final_dist += min_cost_to[get_node]; + result[i-1].first = get_node; result[i-1].second = pairs[get_node]; + } + + fout << final_dist << '\n'; + fout << n - 1 << '\n'; + for ( int i = 0; i < n - 1; i ++ ) { + fout << result[i].first << ' ' << result[i].second; + fout << '\n'; + } + + + + + return 0; + +} diff --git a/mst/prim/c90fb3cf-0121-4087-8a9f-d4a91c8a9811.cpp b/mst/prim/c90fb3cf-0121-4087-8a9f-d4a91c8a9811.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa9ee203a7682c0370749ca8f9769ac31ad1c3e4 --- /dev/null +++ b/mst/prim/c90fb3cf-0121-4087-8a9f-d4a91c8a9811.cpp @@ -0,0 +1,190 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +int n,m,viz[200005],cost; + +vector >graph[200005]; + +vector >sol; + +struct edge + +{ + + int x; + + int y; + + int c; + + + + bool operator<(const edge &other) const { + + if(c!=other.c) + + return c>other.c; + + return y>other.y; + + } + +}; + +priority_queue >q; + + + +void read() + +{ + + fin>>n>>m; + + int x,y,c; + + for(int i=0; i>x>>y>>c; + + graph[x].push_back(make_pair(y,c)); + + graph[y].push_back(make_pair(x,c)); + + } + +} + + + +void prim() + +{ + + viz[1]=1; + + int nr=graph[1].size(); + + for(int i=0; i +#include +#include +#include +using namespace std; +ifstream f("apm.in"); +ofstream g("apm.out"); +priority_queue >, vector > >, greater > > >h; +vector >v[200005]; +int n,n2,x,y,c,viz[200005]; +struct solutie +{ + int x,y; +}sol[200005]; +struct muchie +{ + int x,y,c; +}m[400005]; +int prim(int nod) +{ + int i,cost=0,cc=0,j,nodc; + viz[nod] = 1; + for(i=1;i<=n-1;i++) + { + for(j=0;j>n>>n2; + for(i=1;i<=n2;i++) + { + f>>x>>y>>c; + v[x].push_back({c,y}); + v[y].push_back({c,x}); + } + g< + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +set < pair < int , int > > s; + +vector < pair < int , int > > v[200005]; + +int totaldist,n,viz[200005],dist[200005],tata[200005]; + +int m,i,x,y,cost; + +int oo=2000000009; + + + +int main() + +{ + + f >> n >> m; + + for (i=1;i<=m;i++) { + + f >> x >> y >> cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(int i=1;i<=n;i++) { + + dist[i]=oo; + + } + + dist[1]=0; + + s.insert(make_pair(0,1)); + + while (s.empty()==0) { + + int nod = s.begin()->second; + + int distnod = s.begin()->first; + + s.erase(s.begin()); + + if (viz[nod]==0) { + + viz[nod] = 1; + + totaldist += distnod; + + for (int i=0;i disttoviz && viz[nodtoviz]==0) { + + dist[nodtoviz] = disttoviz; + + s.insert(make_pair(dist[nodtoviz],nodtoviz)); + + tata[nodtoviz] = nod; + + } + + } + + } + + } + + g << totaldist << '\n'; + + g << n-1 << '\n'; + + for (i=1;i<=n;i++) { + + if (tata[i]!=0) { + + g << i << " " << tata[i] << '\n'; + + } + + } + + return 0; + +} diff --git a/mst/prim/ca824512-daa6-4421-b2fa-73fa8514dc60.cpp b/mst/prim/ca824512-daa6-4421-b2fa-73fa8514dc60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21ddb9d1e5896f1a17266b066cdbd298cf1afd13 --- /dev/null +++ b/mst/prim/ca824512-daa6-4421-b2fa-73fa8514dc60.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; +const int size = 200001; +ifstream fin("apm.in"); +ofstream fout("apm.out"); +priority_queue>,greater>> heap; +vector> a[size]; +set rez; +int d[size], tata[size], isInHeap[size]; + +void init(int n) { + for(int i = 0; i <= n; ++i) { + d[i] = 20000000; + tata[i] = 0; + isInHeap[i] = 1; + } +} + +int main() { + int i, n, m, x, y, c, sum = 0; + fin >> n >> m; + init(n); + for(i = 1; i <= m; ++i) { + fin >> x >> y >> c; + a[x].push_back(make_pair(y, c)); + a[y].push_back(make_pair(x, c)); + } + d[1] = 0; + for(i = 1; i <= n; ++i) + heap.push(make_pair(d[i], i)); + while(!heap.empty() && heap.top().first != 20000000) { + pair p = heap.top(); + heap.pop(); + int u = p.second; + if(u != 1 && rez.find(u) == rez.end()){ sum += d[u]; rez.insert(u);} + isInHeap[u] = 0; + for(auto v:a[u]) { + if(isInHeap[v.first] == 1 && v.second < d[v.first]) { + d[v.first] = v.second; + tata[v.first] = u; + heap.push(make_pair(d[v.first], v.first)); + } + } + } + fout << sum << "\n" << rez.size() <<" \n"; + for(auto x:rez) + fout << x << " " << tata[x] << "\n"; + return 0; +} diff --git a/mst/prim/cbb653d9-1db0-43df-a9d0-8fed70c4a5c6.cpp b/mst/prim/cbb653d9-1db0-43df-a9d0-8fed70c4a5c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..67a4e24bd7be5ca6bc93df2154bbf14a10e83b4b --- /dev/null +++ b/mst/prim/cbb653d9-1db0-43df-a9d0-8fed70c4a5c6.cpp @@ -0,0 +1,275 @@ +#include +using namespace std; + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef double db; + +typedef string str; + + + +typedef pair pi; + +typedef pair pl; + +typedef pair pd; + + + +typedef vector vi; + +typedef vector vl; + +typedef vector vd; + +typedef vector vs; + +typedef vector vpi; + +typedef vector vpl; +const int MOD = 1e9+7; + +const int NMAX = 2e5+5; + +const ll INF = 1e18; + +const ld PI = 4*atan((ld)1); + + + +namespace input { + + template void re(complex& x); + + template void re(pair& p); + + template void re(vector& a); + + template void re(array& a); + + + + template void re(T& x) { cin >> x; } + + template void re(T& t, Ts&... ts) { + + re(t); re(ts...); + + } + + + + template void re(complex& x) { T a,b; re(a,b); x = cd(a,b); } + + template void re(pair& p) { re(p.first,p.second); } + + template void re(vector& a) { for (int i = (0); i < ((int)a.size()); ++i) re(a[i]); } + + template void re(array& a) { for (int i = (0); i < (SZ); ++i) re(a[i]); } + +} + + + +using namespace input; + + + +namespace output { + + void pr(int x) { cout << x; } + + void pr(long x) { cout << x; } + + void pr(ll x) { cout << x; } + + void pr(unsigned x) { cout << x; } + + void pr(unsigned long x) { cout << x; } + + void pr(unsigned long long x) { cout << x; } + + void pr(float x) { cout << x; } + + void pr(double x) { cout << x; } + + void pr(ld x) { cout << x; } + + void pr(char x) { cout << x; } + + void pr(const char* x) { cout << x; } + + void pr(const string& x) { cout << x; } + + void pr(bool x) { pr(x ? "true" : "false"); } + + template void pr(const complex& x) { cout << x; } + + + + template void pr(const pair& x); + + template void pr(const T& x); + + + + template void pr(const T& t, const Ts&... ts) { + + pr(t); pr(ts...); + + } + + template void pr(const pair& x) { + + pr("{",x.first,", ",x.second,"}"); + + } + + template void pr(const T& x) { + + pr("{"); + + bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; + + pr("}"); + + } + + + + void ps() { pr("\n"); } + + template void ps(const T& t, const Ts&... ts) { + + pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); + + } + + + + void pc() { pr("]\n"); } + + template void pc(const T& t, const Ts&... ts) { + + pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); + + } + + + +} + +using namespace output; + + + +int n,m; + +struct tip{ + +int nod,cost,tata; + +}; + +bool operator<(tip x,tip y) + +{ + + return x.cost>y.cost; + +} + +priority_queuecoada; + +vectorG[NMAX]; + +vectormu; + +bool uz[NMAX]; + +int ans; + +void solve() { + +fin>>n>>m; + +for (int i = (1); i < (m+1); ++i) + + {int x,y,c; + + fin>>x>>y>>c; + + G[x].push_back({y,c,x}); + + G[y].push_back({x,c,y}); + + } + +for (int i = (0); i < (G[1].size()); ++i) + + coada.push({G[1][i].nod,G[1][i].cost,1}); + + uz[1]=1; + + while(!coada.empty()) + + {tip x; + + x=coada.top(); + + coada.pop(); + + if(!uz[x.nod]) + + {ans+=x.cost; + + uz[x.nod]=1; + + mu.push_back({x.tata,x.nod}); + + for (int i = (0); i < (G[x.nod].size()); ++i) + + coada.push({G[x.nod][i].nod,G[x.nod][i].cost,x.nod}); + + } + + } + +fout< +#include +#include +#include +using namespace std; + + + +struct muchie { + + int nod, tata, cost; + + + + muchie(int nn = 0, int tt = 0, int cc = 0) { + + nod = nn, tata = tt, cost = cc; + + } + +}; + + + +struct minHeap { + + int tata[200000 + 5]; + + int d[200000 + 5]; + + int poz[200000 + 5]; + + int val[200000 + 5]; + + int dim; + + + + minHeap(int ddim = 0) { + + dim = 0; + + for(int i = 1; i <= ddim; i++) + + d[i] = 2000000000 + 5; + + } + + + + muchie top() { + + return muchie(val[1], tata[val[1]], d[val[1]]); + + } + + + + bool isEmpty() { + + return dim == 0; + + } + + + + bool inHeap(int nod) { + + return poz[nod] >= 1 && poz[nod] <= dim; + + } + + + + void urcare(int ind) { + + if(ind == 1) + + return; + + if(d[val[ind]] < d[val[ind / 2]]) { + + swap(val[ind], val[ind / 2]); + + poz[val[ind]] = ind; + + poz[val[ind / 2]] = ind / 2; + + urcare(ind / 2); + + } + + } + + + + void coborare(int ind) { + + int st = 2 * ind, dr = 2 * ind + 1, minim = ind; + + + + if(st <= dim) + + minim = (d[val[st]] < d[val[minim]]) ? st : minim; + + if(dr <= dim) + + minim = (d[val[dr]] < d[val[minim]]) ? dr : minim; + + + + if(minim != ind) { + + swap(val[minim], val[ind]); + + poz[val[minim]] = minim; + + poz[val[ind]] = ind; + + coborare(minim); + + } + + } + + + + void push(muchie crt) { + + if(inHeap(crt.nod)) { + + if(crt.cost < d[crt.nod]) { + + d[crt.nod] = crt.cost; + + tata[crt.nod] = crt.tata; + + urcare(poz[crt.nod]); + + } + + } + + else if(d[crt.nod] == 2000000000 + 5) { + + val[++dim] = crt.nod; + + poz[crt.nod] = dim; + + tata[crt.nod] = crt.tata; + + d[crt.nod] = crt.cost; + + urcare(dim); + + } + + } + + + + void pop() { + + poz[val[1]] = 0; + + if(dim > 1) { + + swap(val[1], val[dim--]); + + poz[val[1]] = 1; + + coborare(1); + + } + + else + + dim--; + + } + +}; + + + +int n, m, nm, capm; + +muchie muchii[200000 + 5]; + +vector v[200000 + 5]; + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int x, y, z; + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &z); + + v[x].push_back(muchie(y, x, z)); + + v[y].push_back(muchie(x, y, z)); + + } + + + + minHeap pq = minHeap(n); + + pq.push(muchie(1, 0, 0)); + + nm = 0; + + while(!pq.isEmpty() && nm < n - 1) { + + muchie crt = pq.top(); + + pq.pop(); + + + + capm += crt.cost; + + if(crt.tata) + + muchii[++nm] = crt; + + for(muchie vec: v[crt.nod]) + + pq.push(vec); + + } + + + + printf("%d\n%d\n", capm, nm); + + for(int i = 1; i <= nm; i++) + + printf("%d %d\n", muchii[i].nod, muchii[i].tata); + + + + return 0; + +} diff --git a/mst/prim/cc469994-4d80-4225-8a97-3831ed0df840.cpp b/mst/prim/cc469994-4d80-4225-8a97-3831ed0df840.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ac50287c571cca2fe69cbedf262cca45bac6acb --- /dev/null +++ b/mst/prim/cc469994-4d80-4225-8a97-3831ed0df840.cpp @@ -0,0 +1,111 @@ +#include +#include + + +using namespace std; + + + + + + + +priority_queue< pair > > myheap; + +vector< pair > graph[400005], apm; + +bool viz[400005]; + +int n, m, cost_total; + + + +int main() { + + int a, b, c; + + + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w",stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d%d", &a, &b, &c); + + graph[a].push_back(make_pair(b, c)); + + graph[b].push_back(make_pair(a, c)); + + } + + + + int root = 1; + + viz[root] = true; + + for(auto edge : graph[root]) { + + myheap.push(make_pair(-edge.second, make_pair(root, edge.first))); + + } + + + + while(!myheap.empty()) { + + pair > best = myheap.top(); + + myheap.pop(); + + + + int cost = best.first; + + int new_node = best.second.second; + + + + if(!viz[new_node]) { + + viz[new_node] = true; + + apm.push_back(best.second); + + cost_total += -cost; + + + + for(auto edge : graph[new_node]) { + + myheap.push(make_pair(-edge.second, make_pair(new_node, edge.first))); + + } + + } + + } + + + + printf("%d\n%d\n", cost_total, n - 1); + + for(auto edge : apm) { + + printf("%d %d\n", edge.first, edge.second); + + } + + + + + + return 0; + +} diff --git a/mst/prim/ccc83886-2944-4141-b81b-a1fb158df10a.cpp b/mst/prim/ccc83886-2944-4141-b81b-a1fb158df10a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8903569a6b98c14628b967e472e91319ffa0a0d --- /dev/null +++ b/mst/prim/ccc83886-2944-4141-b81b-a1fb158df10a.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >V; + +vector >v[200005]; + +int n, m, x, y, c, viz[200005]; + +struct Banana + +{ + + int x, y; + +}sol[200005]; + +int prim(int nod) + +{ + + int i, cost = 0, b = 0, j, nodaux; + + viz[nod] = 1; + + for (i = 1; i <= n - 1; i++) + + { + + for (j = 0; j < v[nod].size(); j++) + + { + + nodaux = v[nod][j].second; + + b = v[nod][j].first; + + if (!viz[nodaux]) V.push({ b,{nod,nodaux} }); + + } + + while (viz[V.top().second.second]) + + V.pop(); + + cost += V.top().first; + + viz[V.top().second.second] = 1; + + sol[i].x = V.top().second.first; + + sol[i].y = V.top().second.second; + + nod = V.top().second.second; + + V.pop(); + + } + + return cost; + +} + +int main() + +{ + + int i; + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + v[x].push_back({ c,y }); + + v[y].push_back({ c,x }); + + } + + g << prim(1) << '\n' << n - 1 << '\n'; + + for (i = 1; i < n; i++) + + g << sol[i].x << " " << sol[i].y << '\n'; + + return 0; + +} diff --git a/mst/prim/cd2fad3f-1dfc-4510-80ed-8869fb097657.cpp b/mst/prim/cd2fad3f-1dfc-4510-80ed-8869fb097657.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8c63996f8608829117d6871c55fc8336280080e --- /dev/null +++ b/mst/prim/cd2fad3f-1dfc-4510-80ed-8869fb097657.cpp @@ -0,0 +1,162 @@ +#include + + + + +using namespace std; + +const int DN=1e6+5; + +int n,m,pr[DN],sol[DN],f,cnt,g,cost,viz[DN]; + +pair >mc[DN]; + +set >s; + +long long sum; + +vector >v[DN]; + + + +int main() + +{ + + FILE *fin=fopen("apm.in","r"); + + FILE *fout=fopen("apm.out","w"); + + + + fscanf(fin,"%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + fscanf(fin,"%d%d%d",&mc[i].second.first,&mc[i].second.second,&mc[i].first); + + f=mc[i].second.first; + + g=mc[i].second.second; + + cost=mc[i].first; + + v[f].push_back({g,cost}); + + v[g].push_back({f,cost}); + + } + + for(int i=0;ifirst; + + int nod=s.begin()->second; + + s.erase(s.begin()); + + viz[nod]=1; + + for(auto i:v[nod]) + + if(!viz[i.first]) + + { + + if(sol[i.first]<=i.second) + + continue; + + s.erase(s.find({sol[i.first],i.first})); + + sol[i.first]=i.second; + + s.insert({sol[i.first],i.first}); + + pr[i.first]=nod; + + } + + } + + + + + + fprintf(fout,"%lld\n",sum); + + fprintf(fout,"%d\n",cnt); + + for(int i=0;i<=n;i++) + + { + + if(pr[i]==-1) + + continue; + + fprintf(fout,"%d %d\n",i,pr[i]); + + } + + + +} diff --git a/mst/prim/cd3990fc-75ca-46e0-a869-c83994a4bde2.cpp b/mst/prim/cd3990fc-75ca-46e0-a869-c83994a4bde2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..affce7aa063f33482cf55c5466bd69ac03be11fd --- /dev/null +++ b/mst/prim/cd3990fc-75ca-46e0-a869-c83994a4bde2.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + +struct muchie + +{ + + int cost; + + int elem; + +}; + +struct ura + +{ + + int ct; + + int nod1; + + int nod2; + +}; + +bool operator < (const ura& lhs, const ura& rhs) + +{ + + return (lhs.ct) < (rhs.ct); + +} + + + +vector> v; + +vector> raspuns; + +multiset costuri; + +bool viz[200001]; + +int main() + +{ + + int n, m, x, y, c, k, j, s; + + cin>>n>>m; + + v.resize(n+1); + + for(int i=1; i<=m; i++) + + { + + cin>>x>>y>>c; + + v[x].push_back({c, y}); + + v[y].push_back({c, x}); + + } + + s=0; + + k=n; + + j=1; + + viz[1]=true; + + while(k>1) + + { + + for(size_t i=0; i +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("apm.in"); + + + +ofstream out("apm.out"); + + + +const int INF = 100000000; + + + +vector d; + + + +vector tata; + + + +vector cost; + + + +vector viz; + + + +vector>adiacenta[200000]; + + + +priority_queue< pair, vector >, greater> > Q; + + + + + +int n,m,i,s; + + + +void Prim() + +{ + + d[s] = 0; + + Q.push(make_pair(0,s)); + + + + while(!Q.empty()) + + { + + int u = Q.top().second; + + viz[u] = 1; + + Q.pop(); + + for (pair e : adiacenta[u]) + + { + + int vecin = e.first; + + int c = e.second; + + + + if (!viz[vecin] && c < cost[vecin]) + + { + + + + cost[vecin] = c; + + Q.push(make_pair(cost[vecin], vecin)); + + + + + + + + tata[vecin] = u; + + + + } + + + + } + + } + +} + + + +int main() + +{ + + in >> n >> m; + + d.resize(n+1); + + tata.resize(n+1); + + viz.resize(n+1); + + cost.resize(n+1); + + int x,y,c; + + for(i=0;i>x>>y>>c; + + adiacenta[x].push_back(make_pair(y, c)); + + adiacenta[y].push_back(make_pair(x, c)); + + } + + s=1; + + + + + + for(int i=1;i<=n;i++) + + { + + cost[i] = INF; + + viz[i] = 0; + + tata[i] = -1; + + } + + + + Prim(); + + + + + + int d_arbore = 0; + + for(int i=1;i<=n;i++) + + { + + if(i==s) + + continue; + + d_arbore += cost[i]; + + } + + out << d_arbore << endl; + + out << n-1 << endl; + + for(int i=1;i<=n;i++) + + { + + if(i==s) + + continue; + + out << tata[i] << " " << i << '\n'; + + } + + + +} diff --git a/mst/prim/cdaf3b2e-1d65-40f7-9995-153308fe84bc.cpp b/mst/prim/cdaf3b2e-1d65-40f7-9995-153308fe84bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f3b1bbfae4dc3c82277a62759daf8cd18c13978 --- /dev/null +++ b/mst/prim/cdaf3b2e-1d65-40f7-9995-153308fe84bc.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +struct ura1 + +{ + + int nod,cost; + + bool operator <(const ura1 &other)const + + { + + return cost l[200005]; + +multiset s; + +ura2 sol[200005]; + +int v[200005],cor[200005],vc[200005]; + +void apm (int x) + +{ + + int i,y,c; + + for (i=0;ic) + + { + + v[y]=c; + + s.insert(l[x][i]); + + cor[y]=x; + + } + + } + +} + +int main () + +{ + + int rez=0,n,m,i,x,y,c,nod; + + fin>>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y>>c; + + l[x].push_back({y,c}); + + l[y].push_back({x,c}); + + } + + for (i=2;i<=n;i++) + + { + + v[i]=2000000000; + + s.insert({i,v[i]}); + + } + + apm(1); + + vc[1]=1; + + for (i=1;inod; + + while (vc[nod]) + + { + + s.erase(s.begin()); + + nod=s.begin()->nod; + + } + + s.erase(s.begin()); + + vc[nod]=1; + + apm(nod); + + rez+=v[nod]; + + sol[i]={nod,cor[nod]}; + + } + + fout< +#include +#include +#include +using namespace std; + + + +struct muchie { + + int nod, tata, cost; + + + + muchie(int nn = 0, int tt = 0, int cc = 0) { + + nod = nn, tata = tt, cost = cc; + + } + +}; + + + +struct minHeap { + + int tata[200000 + 5]; + + int d[200000 + 5]; + + int poz[200000 + 5]; + + int val[200000 + 5]; + + int dim; + + + + minHeap(int ddim = 0) { + + dim = 0; + + for(int i = 1; i <= ddim; i++) + + d[i] = 2000000000 + 5; + + } + + + + muchie top() { + + return muchie(val[1], tata[val[1]], d[val[1]]); + + } + + + + bool isEmpty() { + + return dim == 0; + + } + + + + bool inHeap(int nod) { + + return poz[nod] >= 1 && poz[nod] <= dim; + + } + + + + void urcare(int ind) { + + if(ind == 1) + + return; + + if(d[val[ind]] < d[val[ind / 2]]) { + + swap(val[ind], val[ind / 2]); + + poz[val[ind]] = ind; + + poz[val[ind / 2]] = ind / 2; + + urcare(ind / 2); + + } + + } + + + + void coborare(int ind) { + + int st = 2 * ind, dr = 2 * ind + 1, minim = ind; + + + + if(st <= dim) + + minim = (d[val[st]] < d[val[minim]]) ? st : minim; + + if(dr <= dim) + + minim = (d[val[dr]] < d[val[minim]]) ? dr : minim; + + + + if(minim != ind) { + + swap(val[minim], val[ind]); + + poz[val[minim]] = minim; + + poz[val[ind]] = ind; + + coborare(minim); + + } + + } + + + + void push(muchie crt) { + + if(inHeap(crt.nod)) { + + if(crt.cost < d[crt.nod]) { + + d[crt.nod] = crt.cost; + + tata[crt.nod] = crt.tata; + + urcare(poz[crt.nod]); + + } + + } + + else if(d[crt.nod] == 2000000000 + 5) { + + val[++dim] = crt.nod; + + poz[crt.nod] = dim; + + tata[crt.nod] = crt.tata; + + d[crt.nod] = crt.cost; + + urcare(dim); + + } + + } + + + + void pop() { + + poz[val[1]] = 0; + + if(dim > 1) { + + swap(val[1], val[dim--]); + + poz[val[1]] = 1; + + coborare(1); + + } + + else + + dim--; + + } + +}; + + + +int n, m, nm, capm; + +muchie muchii[200000 + 5]; + +vector v[200000 + 5]; + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + int x, y, z; + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &z); + + v[x].push_back(muchie(y, x, z)); + + v[y].push_back(muchie(x, y, z)); + + } + + + + minHeap pq = minHeap(n); + + pq.push(muchie(1, 0, 0)); + + nm = 0; + + while(!pq.isEmpty() && nm < n - 1) { + + muchie crt = pq.top(); + + pq.pop(); + + + + capm += crt.cost; + + if(crt.tata) + + muchii[++nm] = crt; + + for(muchie vec: v[crt.nod]) + + pq.push(vec); + + } + + + + printf("%d\n%d\n", capm, nm); + + for(int i = 1; i <= nm; i++) + + printf("%d %d\n", muchii[i].nod, muchii[i].tata); + + + + return 0; + +} diff --git a/mst/prim/cf5871ec-0c20-407f-a919-0ad642356f49.cpp b/mst/prim/cf5871ec-0c20-407f-a919-0ad642356f49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49338ae372f6106209b48c6eae6286c8b96017a0 --- /dev/null +++ b/mst/prim/cf5871ec-0c20-407f-a919-0ad642356f49.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 1005; + +const int NMAX = 200005; + + + +vector < vector < pair < int, int > > > costuri(NMAX); + +vector < int > vx; + +vector < int > vy; + +int tati[NMAX], N, M; + +bool vizitat[NMAX]; + +int dist[NMAX]; + +long long cost_minim; + +int nod_start = -1; + + + +set < pair < int, int > > heap; + + + +void init() + +{ + + for(int i = 0; i < NMAX; i++) + + dist[i] = INFINIT; + +} + + + +void citire() + +{ + + fin >> N >> M; + + int x, y, cost; + + int cost_min = 1005; + + for(int i = 0; i < M; i++) + + { + + fin >> x >> y >> cost; + + pair < int, int > pair_1 = make_pair(y, cost); + + pair < int, int > pair_2 = make_pair(x, cost); + + costuri[x].push_back(pair_1); + + costuri[y].push_back(pair_2); + + if(cost < cost_min) + + { + + cost_min = cost; + + nod_start = x; + + } + + } + + init(); + +} + + + +void Prim() + +{ + + for(unsigned int i = 0; i < costuri[nod_start].size(); i++) + + { + + tati[costuri[nod_start][i].first] = nod_start; + + heap.insert( {costuri[nod_start][i].second, costuri[nod_start][i].first} ); + + dist[costuri[nod_start][i].first] = costuri[nod_start][i].second; + + } + + vizitat[nod_start] = 1; + + + + while(!heap.empty()) + + { + + int mini = heap.begin() -> second; + + int cost = heap.begin() -> first; + + heap.erase(heap.begin()); + + + + if(vizitat[mini]) + + continue; + + cost_minim += cost; + + vizitat[mini] = 1; + + vx.push_back(tati[mini]); vy.push_back(mini); + + for(unsigned int k = 0; k < costuri[mini].size(); k++) + + if(!vizitat[costuri[mini][k].first]) + + { + + if(costuri[mini][k].second < dist[costuri[mini][k].first]) + + { + + dist[costuri[mini][k].first] = costuri[mini][k].second; + + tati[costuri[mini][k].first] = mini; + + } + + heap.insert( {costuri[mini][k].second, costuri[mini][k].first} ); + + } + + } + +} + + + +void afisare() + +{ + + fout << cost_minim << '\n'; + + fout << N - 1 << '\n'; + + for(int i = 0; i < N - 1; i++) + + fout << vx[i] << ' ' << vy[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + Prim(); + + afisare(); + + return 0; + +} diff --git a/mst/prim/cf79377e-405d-40a5-9251-d3c860835222.cpp b/mst/prim/cf79377e-405d-40a5-9251-d3c860835222.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f92cda63bb6743dc284d4b9db423f6aad5854690 --- /dev/null +++ b/mst/prim/cf79377e-405d-40a5-9251-d3c860835222.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +struct ura + +{ + + int nod,cost; + + bool operator<(const ura &other) const{ + + return cost s; + +vector lista[200001]; + +int v[2000001]; + +int cor[200002]; + +bool used[200002]; + +void intro_apm(int nod) + +{ + + int i; + + for(i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + in>>n1>>n2>>cost; + + lista[n2].push_back({n1,cost}); + + lista[n1].push_back({n2,cost}); + + } + + for(i=1;i<=n;i++) + + { + + v[i]=10001; + + } + + intro_apm(1); + + used[1]=1; + + int nr,costotal=0; + + for(i=2;i<=n;i++) + + { + + nr=s.begin()->nod; + + while(used[nr]==1) + + { + + s.erase(s.begin()); + + nr=s.begin()->nod; + + } + + used[nr]=1; + + intro_apm(nr); + + costotal+=v[nr]; + + sol[i-1]={nr,cor[nr]}; + + } + + out< +#include +#include +#include + + +using namespace std; + + + +struct muchie { + + int from, to, cost; + +}; + + + +class comp { + +public: + + bool operator()(muchie a, muchie b) { + + return a.cost > b.cost; + + } + +}; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m, a, b, c; + +bool vizitat[200001]; + +vector > graph[200001]; + +vector > rez; + +priority_queue, comp> PQ; + + + +void rezolvare() { + + for (auto &v:graph[1]) + + PQ.push({1, v.first, v.second}); + + muchie sus; + + int cost = 0; + + while (!PQ.empty()) { + + sus = PQ.top(); + + PQ.pop(); + + if (!vizitat[sus.to]) { + + cost += sus.cost; + + rez.emplace_back(sus.from, sus.to); + + vizitat[sus.from] = vizitat[sus.to] = 1; + + for (auto &v:graph[sus.to]) { + + if (!vizitat[v.first]) { + + PQ.push({sus.to, v.first, v.second}); + + } + + } + + } + + } + + g << cost << '\n'; + + g << n - 1 << '\n'; + + for (auto &v:rez) + + g << v.first << ' ' << v.second << '\n'; + +} + +void citire(){ + + f >> n >> m; + + for (int i = 1; i <= m; ++i) { + + f >> a >> b >> c; + + graph[a].emplace_back(b, c); + + graph[b].emplace_back(a, c); + + } + +} + + + + + +int main() { + + citire(); + + rezolvare(); + + return 0; + +} diff --git a/mst/prim/d09cfe24-ed2d-4859-a5e3-3767e4b67de5.cpp b/mst/prim/d09cfe24-ed2d-4859-a5e3-3767e4b67de5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e1539e8dd7daddadf3715e73c51fc4db3746bff2 --- /dev/null +++ b/mst/prim/d09cfe24-ed2d-4859-a5e3-3767e4b67de5.cpp @@ -0,0 +1,157 @@ +#include + + + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 2e5+10; + + + +struct nume{ + + int x,y,val; + +}; + + + +vector vec; + +priority_queue heap; + + + +int tata[DMAX]; + + + +int n,m; + +int ans; + + + +inline bool operator<(nume x,nume y){ + + return x.val>y.val; + +} + + + +int comp(int x); + +void unire(int x,int y); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y,z; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>z; + + heap.push({x,y,z}); + + } + + for(i=1;i +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({ y, c }); + + g[y].push_back({ x, c }); + + } + + + + set> heap; + + heap.insert({ 0, 1 }); + + for (int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while (!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if (vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for (auto y : g[nod]) + + if (!vis[y.first] && y.second < minCost[y.first]) + + { + + if (vis[y.first] && y.second < minCost[y.first]) + + heap.erase(heap.find({ minCost[y.first], y.first })); + + + + heap.insert({ y.second, y.first }); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for (int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/d49353c6-9d8c-462b-960e-51382c4600f0.cpp b/mst/prim/d49353c6-9d8c-462b-960e-51382c4600f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb93a0ea19807fa3e4f07cfe71c458b680c4831e --- /dev/null +++ b/mst/prim/d49353c6-9d8c-462b-960e-51382c4600f0.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +using namespace std; + + + +const int maxi=1<<30; + + + +ifstream be("apm.in"); + +ofstream ki("apm.out"); + + + +void prim(vector > >&adj ,int n) + +{ + + vectormstset(n+1,false); + + vectorparent(n+1,-1); + + vectorkey(n+1,maxi); + + priority_queue,deque >,greater > >q; + + int src=1; + + int db=0; + + parent[src]=src; + + q.push({0,src}); + + key[src]=0; + + while(!q.empty()) + + { + + int u=q.top().second; + + int cost=q.top().first; + + q.pop(); + + if(mstset[u] || cost!=key[u])continue; + + mstset[u]=true; + + db+=cost; + + for(auto p:adj[u]) + + { + + int v=p.first; + + int c=p.second; + + if(mstset[v]==false && key[v]>c) + + { + + key[v]=c; + + q.push({key[v],v}); + + parent[v]=u; + + } + + } + + } + + printf("%d\n%d\n",db,n-1); + + for (int i = 1; i <= n; i++) + + { + + if(i!=src) + + { + + printf("%d %d\n",i,parent[i]); + + } + + } + + + +} + + + + + +int main() + +{ + + freopen("apm.in","r",stdin); + + freopen("apm.out","w",stdout); + + int n,m; + + scanf("%d %d",&n,&m); + + vector > >adj(n+1); + + for(int i=0;i +#include + + + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +int n , m; + + + + + + + +bitset<200005>f; + +int cost[200005]; + +int previous[200005]; + +struct edge + +{ + + int x , d; + +}; + +vector edges[200005]; + + + +priority_queue >pq; + + + +void refresh() + +{ + + for(int i = 1; i <= n; i++) + + cost[i] = 2e9; + +} + + + +int addtomst(int node) + +{ + + for(auto &i:edges[node]) + + { + + if(f[i.x]==0) + + { + + if(i.d < cost[i.x]) + + { + + pq.push({-i.d,i.x}); + + cost[i.x] = i.d; + + previous[i.x] = node; + + } + + } + + } + +} + + + +int main() + +{ + + int a , b , d; + + in >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + in >> a >> b >> d; + + edges[a].push_back({b,d}); + + edges[b].push_back({a,d}); + + } + + refresh(); + + f[1] = 1; + + addtomst(1); + + for(int i = 2; i <= n; i++) + + { + + int node = pq.top().second; + + while(f[node]==1) + + { + + pq.pop(); + + node = pq.top().second; + + } + + f[node] = 1; + + addtomst(node); + + } + + int cazan = 0; + + for(int i = 2; i <= n; ++i) + + cazan += cost[i]; + + out << cazan << '\n'; + + out << n - 1 << '\n'; + + for(int i = 2; i <= n; ++i) + + out << previous[i] << " " << i << "\n"; + + return 0; + +} diff --git a/mst/prim/d69c0f38-b0f0-4ec4-8034-fd9975cbed17.cpp b/mst/prim/d69c0f38-b0f0-4ec4-8034-fd9975cbed17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22052ad9115c8cd43c6b383f1f3f71c138c2411c --- /dev/null +++ b/mst/prim/d69c0f38-b0f0-4ec4-8034-fd9975cbed17.cpp @@ -0,0 +1,103 @@ +#include + +using namespace std; + +ifstream in("apm.in"); +ofstream out("apm.out"); + + +template +struct triplet +{ + T from, to, cost; +}; + +template +triplet make_triplet(const T &m1, const T &m2, const T &m3) +{ + triplet ans; + ans.from = m1; + ans.to = m2; + ans.cost = m3; + return ans; +} + +class Compare +{ +public: + bool operator() (triplet t1, triplet t2) + { + return (t1.cost > t2.cost); + } +}; + +void Prims(int = 1); +void addEdges(int node); + +const int VMAX = 200001, EMAX = 400001; +int v, e; +bool viz[VMAX]; + + +vector> ad[VMAX]; + + +priority_queue, vector>, Compare> pq; + + +pair rez[VMAX-1]; +int mstCost = 0; + +int main() +{ + in >> v >> e; + for(int i=0; i> from >> to >> cost; + ad[from].push_back(make_pair(to, cost)); + ad[to].push_back(make_pair(from, cost)); + + } + + Prims(); + + return 0; +} + +void Prims(int start_node) +{ + int i= 0; + addEdges(start_node); + + while(!pq.empty() && i != v - 1) + { + triplet curr_edge = pq.top(); + pq.pop(); + if(!viz[curr_edge.to]) + { + rez[i++] = make_pair(curr_edge.from, curr_edge.to); + mstCost += curr_edge.cost; + addEdges(curr_edge.to); + } + } + + out << mstCost << '\n'; + out << v - 1 << '\n'; + for(int i=0; i +#include +#include + + + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct amp{ + + int x, y, c; + +}; + + + +struct lat{ + + int index, cost; + +}; + + + +struct comp { + + bool operator()(amp const& a, amp const& b) { + + return a.c > b.c; + + } + +}; + + + +int n, m, frecNod[200005] , totalCost; + +vector < vector > L(200005); + +vector rezultat; + +priority_queue , comp> coada; + + + +amp muchie; + + + +int main() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; ++i){ + + int x, y, c; + + lat latura; + + in>>x>>y>>c; + + latura.cost = c; + + + + latura.index = y; + + L[x].push_back(latura); + + + + latura.index = x; + + L[y].push_back(latura); + + } + + + + muchie.x = 0; + + muchie.y = 1; + + muchie.c = 0; + + coada.push(muchie); + + + + while(!coada.empty()){ + + + + amp curent = coada.top(); + + + + coada.pop(); + + + + if(frecNod[curent.y]) continue; + + rezultat.push_back(curent); + + frecNod[curent.y] = 1; + + + + totalCost += curent.c; + + for(int i = 0; i < L[curent.y].size(); i++){ + + muchie.x = curent.y; + + muchie.y = L[curent.y][i].index; + + muchie.c = L[curent.y][i].cost; + + + + coada.push(muchie); + + } + + } + + + + out< +#include + +using namespace std; + +struct Edge { + int x, y, cost; +}; + +int n, m, i, j, k, cmin; +bool viz[200005]; +Edge mc, aux, heap[200005]; +vector> arbore, v[200005]; + +void pushup(int p) { + int val; + + val = heap[p].cost; + while(p > 1 && val < heap[p / 2].cost) { + swap(heap[p], heap[p / 2]); + p /= 2; + } +} + +void pushdown(int p) { + int fiu; + + fiu = 1; + while(fiu) { + fiu = 0; + if(p * 2 <= m) { + fiu = p * 2; + if(p * 2 + 1 <= m && heap[p * 2 + 1].cost < heap[p * 2].cost) + fiu = p * 2 + 1; + if(heap[p].cost < heap[fiu].cost) + fiu = 0; + } + if(fiu) { + swap(heap[p], heap[fiu]); + p = fiu; + } + } +} + +int main() +{ + ifstream f("apm.in"); + ofstream g("apm.out"); + + f >> n >> m; + while(m) { + f >> i >> j >> k; + v[i].emplace_back(j, k); + v[j].emplace_back(i, k); + m--; + } + f.close(); + + for(i = 1; i <= n; i++) + viz[i] = false; + viz[1] = true; + m = 0; + for(i = 0; i < v[1].size(); i++) { + mc.x = 1; + mc.y = v[1][i].first; + mc.cost = v[1][i].second; + heap[++m] = mc; + pushup(m); + } + cmin = 0; + for(k = 1; k < n; k++) { + mc = heap[1]; + cmin += mc.cost; + heap[1] = heap[m--]; + pushdown(1); + arbore.emplace_back(mc.x, mc.y); + viz[mc.y] = true; + for(i = 0; i < v[mc.y].size(); i++) + if(!viz[v[mc.y][i].first]) { + aux.x = mc.y; + aux.y = v[mc.y][i].first; + aux.cost = v[mc.y][i].second; + heap[++m] = aux; + pushup(m); + } + while(viz[heap[1].x] && viz[heap[1].y]) { + heap[1] = heap[m--]; + pushdown(1); + } + } + + g << cmin << '\n' << n - 1 << '\n'; + for(i = 0; i < arbore.size(); i++) + g << arbore[i].first << ' ' << arbore[i].second << '\n'; + g.close(); + + return 0; +} diff --git a/mst/prim/d933c1cd-a841-4e26-9dba-75afd44e3a64.cpp b/mst/prim/d933c1cd-a841-4e26-9dba-75afd44e3a64.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbd190d30500d428161e159cf84a5528d8d650c5 --- /dev/null +++ b/mst/prim/d933c1cd-a841-4e26-9dba-75afd44e3a64.cpp @@ -0,0 +1,322 @@ +#include +#include + + + + +std::ifstream in("apm" ".in"); + +std::ofstream out("apm" ".out"); +const int + +N = 200000, + +M = 2*N, + +C = 1000; + + + +struct Arc {int b, cost;}; + +struct Muchie {int a, b, cost;}; + +inline bool operator < (Arc& a, Arc& b) {return a.cost < b.cost;} + +inline bool operator < (Muchie& a, Muchie& b) {return a.cost < b.cost;} + +inline std::istream& operator >> (std::istream& in, Muchie& m) {in >> m.a >> m.b >> m.cost; m.a--, m.b--; return in;} + +inline std::ostream& operator << (std::ostream& out, Muchie& m) {return out << m.a+1 << ' ' << m.b+1 << '\n';} + + + +namespace dj + +{ + + int set[N]; + + + + void reset(int n) + + {for (int i = 0; i < n; i++) set[i] = -1;} + + + + int rad(int x) + + { + + std::vector drum; + + while (set[x] >= 0) + + { + + drum.push_back(x); + + x = set[x]; + + } + + for (auto y: drum) + + set[y] = x; + + return x; + + } + + + + void uneste(int ra, int rb) + + { + + if (set[ra] > set[rb]) + + std::swap(ra, rb); + + set[ra] += set[rb]; + + set[rb] = ra; + + } + +} + + + +template class std::vector; + +template class std::vector; + +template + +struct heap + +{ + + std::vector vector; + + + + inline Type top() + + {return vector.front();} + + + + inline int _fiu0(int idx1) {return idx1 << 1;} + + inline int _fiu1(int idx1) {return (idx1 << 1)+1;} + + inline int _tata(int idx1) {return idx1 >> 1;} + + + + inline int fiu0(int idx) {return _fiu0(idx+1)-1;} + + inline int fiu1(int idx) {return _fiu1(idx+1)-1;} + + inline int tata(int idx) {return idx? _tata(idx+1)-1: 0;} + + + + void sink() + + { + + int i = 0, fiu; + + while (fiu0(i) < vector.size()) + + { + + if (fiu1(i) >= vector.size() || vector[fiu0(i)] < vector[fiu1(i)]) + + fiu = fiu0(i); + + else + + fiu = fiu1(i); + + if (vector[fiu] < vector[i]) + + { + + std::swap(vector[fiu], vector[i]); + + i = fiu; + + } + + else + + break; + + } + + + + } + + + + void rise() + + { + + int i = (int)vector.size() - 1; + + while (vector[i] < vector[tata(i)]) + + { + + std::swap(vector[i], vector[tata(i)]); + + i = tata(i); + + } + + } + + + + void pop() + + { + + std::swap(vector.front(), vector.back()); + + vector.pop_back(); + + sink(); + + } + + + + void push(Type val) + + { + + vector.push_back(val); + + rise(); + + } + +}; + + + +namespace apm + +{ + + Arc tata[N]; + + int rank[N], cost; + + std::vector muchii; + + + + void prim(std::vector* vecini, int radacina) + + { + + heap inventar; + + auto adauga_vecini = [&inventar, vecini](int a) + + { + + for (auto arc: vecini[a]) + + if (arc.b != a) + + inventar.push({a, arc.b, arc.cost}); + + }; + + adauga_vecini(radacina); + + while (inventar.vector.size()) + + { + + Muchie muchie = inventar.top(); + + inventar.pop(); + + int ra = dj::rad(radacina), rb = dj::rad(muchie.b); + + if (ra != rb) + + { + + dj::uneste(ra, rb); + + muchii.push_back(muchie); + + cost += muchie.cost; + + tata[muchie.b] = {muchie.a, muchie.cost}; + + rank[muchie.b] = rank[muchie.a] + 1; + + adauga_vecini(muchie.b); + + } + + } + + } + +} + + + + + + + +std::vector vecini[N]; + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + Muchie m; + + in >> m; + + vecini[m.a].push_back({m.b, m.cost}); + + vecini[m.b].push_back({m.a, m.cost}); + + } + + dj::reset(n); + + apm::prim(vecini, 0); + + out << apm::cost << '\n' << n - 1 << '\n'; + + for (Muchie muchie: apm::muchii) + + out << muchie; + +} diff --git a/mst/prim/daad61c3-c57d-49e3-8e30-3bdb6d67c108.cpp b/mst/prim/daad61c3-c57d-49e3-8e30-3bdb6d67c108.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0275b412b7178b5b047b478591c046e0fa8a9ba --- /dev/null +++ b/mst/prim/daad61c3-c57d-49e3-8e30-3bdb6d67c108.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +using namespace std; +ifstream fin("apm.in"); +ofstream fout("apm.out"); +int cost[200005],mcost[200005],f[200005]; +int n,m,i,j,sum; +struct muchie +{ + int nod; + int c; +}; +muchie aux; +vector G[200005]; +class cmp1 +{ +public: + bool operator() (const muchie& a, const muchie& b) const + { + if(a.c, cmp1> pq; +int x,y,c; +int main() +{ + fin>>n>>m; + while(m--) + { + fin>>x>>y>>c; + aux.c=c; + aux.nod=y; + G[x].push_back(aux); + aux.nod=x; + G[y].push_back(aux); + } + f[1]=1; + for(i=1;i<=n;i++) + cost[i]=1005; + for(i=0;iG[i][l].c) + { + mcost[G[i][l].nod]=i; + cost[G[i][l].nod]=G[i][l].c; + aux.nod=G[i][l].nod; + aux.c=G[i][l].c; + pq.push(aux); + } + } + fout< +#include +#include +#include +#include +#include +using namespace std; +int vizitat[200001], n, m, d[200001], tata[200001], cost; +vector> adj[200001]; +set sol; +priority_queue>,greater>> q; +ifstream f("apm.in"); +ofstream g("apm.out"); + +void initializare() +{ + for(int i = 1; i <= n; i++) + { + d[i] = INT_MAX; + tata[i] = 0; + vizitat[i] = 1; + } +} + +int main() { + f >> n >> m; + for(int i = 0; i < m; i++) + { + int x, y, z; + f >> x >> y >> z; + adj[x].emplace_back(y, z); + adj[y].emplace_back(x, z); + } + initializare(); + d[1] = 0; + for(int i = 1; i <= n; i++) + q.push(make_pair(d[i], i)); + while(!q.empty() && q.top().first != INT_MAX) + { + pair m = q.top(); + int u = m.second; + vizitat[u] = 0; + q.pop(); + if (u != 1 && sol.find(u) == sol.end()) + { + cost += d[u]; + sol.insert(u); + } + for(auto x : adj[u]) + { + if(vizitat[x.first] && x.second < d[x.first]) + { + d[x.first] = x.second; + tata[x.first] = u; + q.push(make_pair(d[x.first], x.first)); + } + } + } + g << cost << '\n' << n - 1 << '\n'; + for (auto x : sol) + g << x << " " << tata[x] << '\n'; + return 0; +} diff --git a/mst/prim/dae1b61c-6266-491c-bd75-25a7723687da.cpp b/mst/prim/dae1b61c-6266-491c-bd75-25a7723687da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6400557a89bfc448a0c59bb8fa2f8d3afafbd182 --- /dev/null +++ b/mst/prim/dae1b61c-6266-491c-bd75-25a7723687da.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >h; + +vector >v[200003]; + +vector >sol; + +int n,m,x,y,c; + +bool viz[200003]; + + + +int prim(int x) + +{ + + int i,j,cst=0,ct,nod; + + viz[x]=1; + + for(i=1; i<=n-1; i++) + + { + + for(j=0; j>n>>m; + + for(i=1; i<=m; i++) + + { + + f>>x>>y>>c; + + v[x].push_back({c,y}); + + v[y].push_back({c,x}); + + } + + g< +#include +#include + + + +using namespace std; + +const int maxn = 200200; +const int INF = 200000200; + +int VEC[maxn],ANS,V[maxn],H[maxn * 2 + 100],POZ[maxn]; +vector > VECT[maxn],VANS; +int N,M,L,C[maxn]; + +void introduce_in_apm(int x) +{ + for(unsigned int i = 0;i < VECT[x].size(); ++i) + { + int nod = VECT[x][i].first; + int cost = VECT[x][i].second; + V[nod] = min(V[nod],cost); + if (V[nod] == cost) VEC[nod] = x; + } +} + +void push(int poz) +{ + while((poz * 2 <= L && V[H[poz]] > V[H[poz * 2]]) || (poz * 2 + 1 <= L && V[H[poz]] > V[H[poz * 2 + 1]])) + { + if (V[H[poz * 2]] < V[H[poz * 2 + 1]] || poz * 2 + 1 > L) + { + swap(H[poz],H[poz * 2]); + swap(POZ[H[poz]],POZ[H[poz * 2]]); + poz *= 2; + } + else + { + swap(H[poz],H[poz * 2 + 1]); + swap(POZ[H[poz]],POZ[H[poz * 2 + 1]]); + poz = poz * 2 + 1; + } + } +} + + +void pop(int poz) +{ + while(poz > 1 && V[H[poz]] < V[H[poz / 2]]) + { + swap(H[poz],H[poz / 2]); + swap(POZ[H[poz]],POZ[H[poz / 2]]); + poz = poz / 2; + } +} + +void introduce_in_heap(int nod) +{ + H[++L] = nod; + POZ[nod] = L; + pop(L); +} + +int scoate_radacina_heap() +{ + int x = H[1]; + swap(H[1],H[L]); + swap(POZ[H[1]],POZ[H[L]]); + L--; + push(1); + POZ[x] = 0; + return x; +} + +int main() +{ + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + scanf("%d %d\n",&N,&M); + for(int i = 1;i <= M; ++i) + { + int x,y,c; + scanf("%d %d %d",&x,&y,&c); + VECT[x].push_back(make_pair(y,c)); + VECT[y].push_back(make_pair(x,c)); + } + + for(int i = 1;i <= N; ++i) V[i] = INF; + V[1] = 0; + introduce_in_apm(1); + for(int i = 2;i <= N; ++i) + { + introduce_in_heap(i); + } + for(int i = 1;i < N; ++i) + { + int x = scoate_radacina_heap(); + introduce_in_apm(x); + ANS += V[x]; + VANS.push_back(make_pair(x,VEC[x])); + for(unsigned int j = 0;j < VECT[x].size(); ++j) + { + int nod = VECT[x][j].first; + if (POZ[nod]) pop(POZ[nod]); + } + } + printf("%d\n",ANS); + printf("%d\n",N-1); + for(unsigned int i = 0;i < N - 1; ++i) + printf("%d %d\n",VANS[i].first,VANS[i].second); + return 0; +} diff --git a/mst/prim/ddf82c9b-7ba0-4e71-8d06-2bd679b47a5a.cpp b/mst/prim/ddf82c9b-7ba0-4e71-8d06-2bd679b47a5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..809f1c1d3cbf2431246d38717814c94f63bb3d58 --- /dev/null +++ b/mst/prim/ddf82c9b-7ba0-4e71-8d06-2bd679b47a5a.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int n, m; + +int x, y, c, sum; + +int viz[200005], nrmuchii; + + + + + +struct muchie{ + + int x; + + int y; + + int cost; + + + + bool operator<(const muchie &other) const { + + if(cost!=other.cost) + + return cost>other.cost; + + return y>other.y; + + } + +}; + +muchie sol[200005]; + +vector graph[200005]; + +priority_queue Q; + + + +void solve(){ + + for(int i=2; i<=n; i++){ + + muchie minima = Q.top(); + + while(viz[minima.y] == 1){ + + Q.pop(); + + minima = Q.top(); + + } + + + + sol[nrmuchii++] = {minima.x, minima.y}; + + viz[minima.y]=1; + + + + sum =sum + minima.cost; + + for(auto &v:graph[minima.y]) + + Q.push(v); + + } + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++){ + + f>>x>>y>>c; + + graph[x].push_back({x, y, c}); + + graph[y].push_back({y, x, c}); + + } + + for(auto &v:graph[1]) + + Q.push(v); + + viz[1] = 1; + + solve(); + + g< + +using namespace std; +struct muchii { + int x,y,cost; + bool operator < (const muchii &a) const { + return cost>a.cost; + } +}var,var1; +int n,m,viz[200003],kontor,suma;vector > graf[200003];priority_queue chestie;pair sol[200003]; +void prim () { + while(!chestie.empty()) { + var=chestie.top(); + if(viz[var.x]==1) { + chestie.pop(); + continue; + } + viz[var.x]=1;chestie.pop(); + if(var.x!=var.y) { + sol[++kontor]={var.x,var.y}; + suma+=var.cost; + } + for(int i=0;i<(int)graf[var.x].size();++i) + if(viz[graf[var.x][i].first]==0) { + var1.x=graf[var.x][i].first,var1.y=var.x,var1.cost=graf[var.x][i].second,chestie.push(var1); + } + } +} +int main () { + int nr,nr1,nr2; + freopen("apm.in","r",stdin); + freopen("apm.out","w",stdout); + scanf("%d%d", &n, &m); + for(int i=1;i<=m;++i) { + scanf("%d%d%d", &nr, &nr1, &nr2); + graf[nr].push_back({nr1,nr2});graf[nr1].push_back({nr,nr2}); + } + var.x=1;var.y=1;var.cost=0; + chestie.push(var);prim(); + printf("%d\n%d\n", suma, kontor); + for(int i=1;i<=kontor;++i) + printf("%d %d\n", sol[i].first, sol[i].second); + return 0; +} diff --git a/mst/prim/e15256c7-d64f-4cd2-98d0-be089b9f453b.cpp b/mst/prim/e15256c7-d64f-4cd2-98d0-be089b9f453b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b156ba480b21ee8390dd57dfd39b9537384e3c37 --- /dev/null +++ b/mst/prim/e15256c7-d64f-4cd2-98d0-be089b9f453b.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +const int N = 200005; + + + +int n, m; + +int cost[N]; + +vector>graf[N]; + +vector> ans; + +bool amfost[N]; + + + +struct cmp{ + + bool operator()(pair> a,pair> b){ + + return a.second.second > b.second.second; + + } + +}; + + + +priority_queue> , vector>> , cmp> coada; + + + +void calc(int nod, int last) { + + cost[nod] = 0; + + coada.push(make_pair(nod, make_pair(last, cost[nod]))); + + + + while(coada.empty() == false) { + + last = coada.top().second.first; + + nod = coada.top().first; + + coada.pop(); + + + + + + + + if(amfost[nod] == true) { + + cout << "JUCK" << '\n'; + + continue; + + } + + + + ans.push_back(make_pair(nod, last)); + + amfost[nod] = true; + + + + for (int i = 0; i < (int)graf[nod].size(); ++i) { + + + + int to = graf[nod][i].first; + + int drum = graf[nod][i].second; + + + + if(cost[to] > drum && amfost[to] == false){ + + cost[to] = drum; + + + + coada.push(make_pair(to, make_pair(nod, cost[to]))); + + } + + + + } + + + + + + } + + + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= n; ++i) + + cost[i] = INT_MAX; + + + + int costMin = INT_MAX, nodMin; + + for (int i = 1; i <= m; ++i) { + + int a, b, c; + + fin >> a >> b >> c; + + graf[a].push_back(make_pair(b,c)); + + graf[b].push_back(make_pair(a,c)); + + + + if(costMin > c) { + + costMin = c; + + nodMin = a; + + } + + } + + calc(nodMin, 0); + + + + int sum = 0; + + for (int i = 1; i <= n; ++i) { + + sum += cost[i]; + + + + + + } + + + + fout << sum << '\n'; + + fout << (int)ans.size() - 1<< '\n'; + + for (int i = 1; i < (int)ans.size(); ++i) + + fout << ans[i].first << " " << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/e1b996cb-1ace-462c-aaba-8b2d0c45cd47.cpp b/mst/prim/e1b996cb-1ace-462c-aaba-8b2d0c45cd47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5303dc1801fb8723339c5b3f9c0427c03ef2f265 --- /dev/null +++ b/mst/prim/e1b996cb-1ace-462c-aaba-8b2d0c45cd47.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +struct nod + +{ + + int x,y,c; + + bool operator<(const nod&other)const + + { + + return c > other.c; + + } + +}; + +priority_queue q; + +vector> gr[200005],sol; + + + +bool seen[200005]; + +int n,m,costmin,nr; + + + +void citire() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y,c; + + in>>x>>y>>c; + + gr[x].push_back({y,c}); + + gr[y].push_back({x,c}); + + } + +} + +void rezolv() + +{ + + seen[1]=1; + + for(const auto& it:gr[1]) + + q.push({1,it.first,it.second}); + + while(!q.empty()) + + { + + nod nd=q.top(); + + int x=nd.x,y=nd.y; + + q.pop(); + + if(seen[x]==1 && seen[y]==1) + + continue; + + costmin+=nd.c; + + sol.push_back({x,y}); + + if(seen[x]==0) + + y=x; + + seen[y]=1; + + nr++; + + if(nr==m-1) + + break; + + for(const auto &it:gr[y]) + + if(seen[it.first]==0) + + q.push({y,it.first,it.second}); + + } + +} + +void afisare(){ + + out< +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +int tata[N]; + +bool vis[N]; + +int n, m; + + + + + +class MinHeap + +{ + + vector> heap; + + vector nodePosition; + + int lastIndex; + + + +public: + + MinHeap(int capacity) + + { + + lastIndex = 1; + + heap.resize(capacity + 1); + + nodePosition.resize(capacity + 1, -1); + + } + + + + void push(pair x) + + { + + heap[lastIndex++] = x; + + nodePosition[x.second] = lastIndex - 1; + + bubbleUp(lastIndex-1); + + } + + + + void pop() + + { + + nodePosition[heap[1].second] = -1; + + heap[1] = heap[--lastIndex]; + + nodePosition[heap[lastIndex].second] = 1; + + + + bubbleDown(1); + + } + + + + pair top() + + { + + return heap[1]; + + } + + + + int getNodeValue(int node) + + { + + int nodeIndex = nodePosition[node]; + + return heap[nodeIndex].first; + + } + + + + void updateNodeValue(int node, int newValue) + + { + + int nodeIndex = nodePosition[node]; + + heap[nodeIndex].first = newValue; + + bubbleUp(nodeIndex); + + } + + + + bool nodeExists(int node) + + { + + return nodePosition[node] != -1; + + } + + + + bool isEmpty() + + { + + return lastIndex == 1; + + } + + + +private: + + void bubbleUp(int i) + + { + + if(i == 1) + + return; + + + + if(heap[i/2] > heap[i]) + + { + + swapNode(i/2, i); + + bubbleUp(i/2); + + } + + } + + + + void bubbleDown(int i) + + { + + int st = i*2; + + int dr = st+1; + + + + if(dr > lastIndex) + + return; + + + + if(heap[st] <= heap[dr] && heap[st] < heap[i]) + + { + + swapNode(st, i); + + bubbleDown(st); + + } + + else if(heap[dr] <= heap[st] && heap[dr] < heap[i]) + + { + + swapNode(dr, i); + + bubbleDown(dr); + + } + + } + + + + void swapNode(int nodeIndex1, int nodeIndex2) + + { + + int node1 = heap[nodeIndex1].second; + + int node2 = heap[nodeIndex2].second; + + nodePosition[node1] = nodeIndex2; + + nodePosition[node2] = nodeIndex1; + + + + swap(heap[nodeIndex1], heap[nodeIndex2]); + + } + + + +}; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + MinHeap heap(n); + + heap.push({0, 1}); + + + + int costTotal = 0; + + while(!heap.isEmpty()) + + { + + int nod = heap.top().second; + + costTotal += heap.top().first; + + vis[nod] = true; + + heap.pop(); + + + + + + for(auto y : g[nod]) + + { + + if(vis[y.first]) + + continue; + + + + if(!heap.nodeExists(y.first)) + + { + + heap.push({y.second, y.first}); + + tata[y.first] = nod; + + } + + else if(y.second < heap.getNodeValue(y.first)) + + { + + heap.updateNodeValue(y.first, y.second); + + tata[y.first] = nod; + + } + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/e22940ec-6da8-448a-89d6-afedb04e0f95.cpp b/mst/prim/e22940ec-6da8-448a-89d6-afedb04e0f95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0cf3bd9f884e176440bf6da83aa5866b705537d --- /dev/null +++ b/mst/prim/e22940ec-6da8-448a-89d6-afedb04e0f95.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +int inf = 100000; + +priority_queue,vector>,greater>> q; + +int n,m; + +int tata[200001],viz[200001],key[200001]; + +list > ad[200001]; + +int main() + +{ + + f >> n >> m; + + for( int i = 1; i <= m ; i++) + + { + + int a,b,c; + + f >> a >> b >> c; + + ad[a].push_back(make_pair(b,c)); + + ad[b].push_back(make_pair(a,c)); + + } + + + + int s = 1, cost = 0, length = n; + + for(int i = 1; i <= n; i ++) + + { + + tata[i] = 0; + + viz[i] = 1; + + key[i] = inf; + + } + + q.push(make_pair(0,s)); + + key[s] = 0; + + while(length) + + { + + pair least = q.top(); + + int node = least.second; + + q.pop(); + + if(viz[node] == 0) + + continue; + + length -- ; + + viz[node] = 0; + + cost += least.first; + + for(auto &edge: ad[node]) + + { + + if(viz[edge.first] && edge.second < key[edge.first]) + + { + + key[edge.first] = edge.second; + + tata[edge.first] = node; + + q.push(make_pair(key[edge.first],edge.first)); + + } + + } + + } + + g< +#include +#include +#include +using namespace std; +ifstream fin("apm.in"); +ofstream fout("apm.out"); +int cost[200005],mcost[200005],f[200005]; +int n,m,i,j,sum; +struct muchie +{ + int nod; + int c; +}; +muchie aux; +vector G[200005]; +class cmp1 +{ +public: + bool operator() (const muchie& a, const muchie& b) const + { + if(a.c, cmp1> pq; +int x,y,c; +int main() +{ + fin>>n>>m; + while(m--) + { + fin>>x>>y>>c; + aux.c=c; + aux.nod=y; + G[x].push_back(aux); + aux.nod=x; + G[y].push_back(aux); + } + f[1]=1; + for(i=1;i<=n;i++) + cost[i]=1005; + for(i=0;iG[i][l].c) + { + mcost[G[i][l].nod]=i; + cost[G[i][l].nod]=G[i][l].c; + aux.nod=G[i][l].nod; + aux.c=G[i][l].c; + pq.push(aux); + } + } + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< vector< pair > > w; + +vector< pair >apm; + +priority_queue< pair >, vector< pair > >, greater< pair > > >h; + + + +int s,i; + + + +bool b[200001]; + + + +void prim (int x) + +{ + + for(i = 0; i < w[x].size(); i++) + + if(!b[w[x][i].first]) + + h.push(make_pair(w[x][i].second, make_pair(x, w[x][i].first))); + + + + b[x] = 1; + + + + while ( !h.empty() && b[h.top().second.second]) + + h.pop(); + + + + + + if(!h.empty()) + + { + + int val = h.top().first, xn = h.top().second.first, yn = h.top().second.second; + + h.pop(); + + + + apm.push_back(make_pair(xn,yn)); + + s += val; + + + + prim(yn); + + } + + + +} + + + +int main() + +{ + + int n, m, x, y, cost; + + + + f >> n >> m; + + + + w.resize(n+1); + + + + for(i = 0; i < m; i++) + + { + + f>>x>>y>>cost; + + + + w[x].push_back(make_pair(y,cost)); + + w[y].push_back(make_pair(x,cost)); + + } + + + + prim(1); + + + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 200005; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +vector> g[N]; + +bool vis[N]; + +int tata[N], minCost[N]; + +int n, m; + + + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y, c; + + fin >> x >> y >> c; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + + } + + + + set> heap; + + heap.insert({0, 1}); + + for(int i = 2; i <= n; i++) + + minCost[i] = INT_MAX; + + + + int costTotal = 0; + + while(!heap.empty()) + + { + + pair minim = *heap.begin(); + + int nod = minim.second; + + int cost = minim.first; + + heap.erase(heap.begin()); + + + + if(vis[nod]) + + continue; + + + + vis[nod] = true; + + costTotal += cost; + + + + for(auto y : g[nod]) + + if(!vis[y.first] && y.second < minCost[y.first]) + + { + + if(vis[y.first] && y.second < minCost[y.first]) + + heap.erase(heap.find({minCost[y.first], y.first})); + + + + heap.insert({y.second, y.first}); + + minCost[y.first] = y.second; + + tata[y.first] = nod; + + } + + } + + + + fout << costTotal << '\n'; + + fout << n - 1 << '\n'; + + for(int i = 2; i <= n; i++) + + fout << i << ' ' << tata[i] << '\n'; + + + + return 0; + +} diff --git a/mst/prim/e45dddea-2d80-4b94-bd0e-c31ba1c21552.cpp b/mst/prim/e45dddea-2d80-4b94-bd0e-c31ba1c21552.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7457da5ce57704ce20604142fd958462eccb39d --- /dev/null +++ b/mst/prim/e45dddea-2d80-4b94-bd0e-c31ba1c21552.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream r("apm.in"); + +ofstream w("apm.out"); + +int viz[200002]; + +vector>g[200002], rez; + +struct qu + +{ + + int nod, cost, prev; + + qu(int nod, int cost, int prev) + + { + + this->nod=nod; + + this->cost=cost; + + this->prev=prev; + + } + + bool operator < (qu b)const + + { + + return cost>b.cost; + + } + +}; + +priority_queueq; + +int main() + +{ + + int n, m; + + r>>n>>m; + + viz[1]=1; + + for(int i=0; i>x>>y>>c; + + g[x].push_back(make_pair(y, c)); + + g[y].push_back(make_pair(x, c)); + + } + + long long sum=0; + + for(int i=0; i +#include +#include +using namespace std; + +ifstream in("apm.in"); + +ofstream out("apm.out"); + +struct muc{ + + int a,cost; + + bool operator<(const muc &other)const{ + + return costlis[200002]; + +multiset s; + +int v[200002],cor[200002],used[200002]; + +int n,m; + +void insertie(int nod) + +{ + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + lis[x].push_back({y,c}); + + lis[y].push_back({x,c}); + + } + + for(i=1;i<=n;i++) + + { + + v[i]=1001; + + } + + insertie(1); + + used[1]=1; + + for(i=2;i<=n;i++) + + { + + x=s.begin()->a; + + while(used[x]==1) + + s.erase(s.begin()),x=s.begin()->a; + + used[x]=1; + + insertie(x); + + ct+=v[x]; + + sol[i-1]={x,cor[x]}; + + } + + out< +#include +#include +#include + +using namespace std; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int f[200001], pereche[200001], cost[200001], n, m; +int cost_total; + +struct tip{ + int nod, ct; + + bool operator <(const tip &other) const{ + return ct < other.ct; + }; + +}; + +vector l[200001]; +multiset stiva; + +struct tip2{ + int x, y; +}final[200001]; + +void intro_apm(int nod) +{ + int x, c, i; + f[nod] = 1; + + for (i = 0; i< l[nod].size(); i++) + { + x = l[nod][i].nod; + c = l[nod][i].ct; + if(cost[x] > c) + { + cost[x] = c; + stiva.insert(l[nod][i]); + pereche[x] = nod; + } + } + +} + +int main() +{ + int C, i, j, x, y; + fin >> n >> m; + + for (i = 2; i<= n; i++) + cost[i] = 2000000001; + + for (i = 1; i<= m; i++) + { + fin >> x >> y >> C; + l[x].push_back({y, C}); + l[y].push_back({x, C}); + } + + for (i = 2; i <= n; i++) + stiva.insert({i, cost[i]}); + + intro_apm(1); + + for (i = 2; i<= n; i++) + { + x = stiva.begin()->nod; + while (f[x] == 1) + { + stiva.erase(stiva.begin()); + x = stiva.begin()->nod; + } + stiva.erase(stiva.begin()); + intro_apm(x); + + cost_total += cost[x]; + final[i-1].x = x; + final[i-1].y = pereche[x]; + } + + fout << cost_total << '\n' << n-1 << '\n'; + for (i = 1; i<= n-1; i++) + fout << final[i].x << " " << final[i].y << '\n'; +} diff --git a/mst/prim/e5b06c98-083d-443c-864d-e4e0d3bcbafe.cpp b/mst/prim/e5b06c98-083d-443c-864d-e4e0d3bcbafe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5513ca2004e8b7db77a1e66e91c0acc37c52282 --- /dev/null +++ b/mst/prim/e5b06c98-083d-443c-864d-e4e0d3bcbafe.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +const int INF = 1e9; + + + +vector dist, t; + + + +vector vis; + + + +vector>> adj; + +vector> ans; + +priority_queue > pq; + + + +int main() + +{ + + int n, m, costTotal = 0; + + in >> n >> m; + + + + dist.resize(n + 1, INF); + + t.resize(n + 1, 0); + + vis.resize(n + 1, false); + + adj.resize(n + 1); + + + + for(int i = 0; i < m; ++i) + + { + + int x, y, cost; + + in >> x >> y >> cost; + + adj[x].push_back({y, cost}); + + adj[y].push_back({x, cost}); + + } + + + + dist[1] = 0; + + + + pq.push({0, 1}); + + + + while(!pq.empty()) + + { + + int node = pq.top().second; + + pq.pop(); + + + + if(vis[node] == false) + + { + + vis[node] = true; + + costTotal += dist[node]; + + if(t[node]) + + ans.push_back({node, t[node]}); + + + + for(auto it : adj[node]) + + { + + if(it.second < dist[it.first]) + + { + + dist[it.first] = it.second; + + t[it.first] = node; + + pq.push({-it.second, it.first}); + + } + + } + + } + + } + + + + out << costTotal << '\n' << n - 1 << '\n'; + + for(auto it : ans) + + { + + out << it.first << " " << it.second << '\n'; + + } + + return 0; + +} diff --git a/mst/prim/e7f477c5-ddf6-4805-8950-75aee5850e70.cpp b/mst/prim/e7f477c5-ddf6-4805-8950-75aee5850e70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d5f0039baf1cf67d692ebb8dbc0e1c79ec36877 --- /dev/null +++ b/mst/prim/e7f477c5-ddf6-4805-8950-75aee5850e70.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +using namespace std; + + + + + +struct Edge { + int node; + + int cost; + + bool operator < (Edge e) const { + if (cost != e.cost) { + return cost < e.cost; + } + + return node < e.node; + } +}; + +int n, m; + +vector g[200001]; + +set available; + +bool visited[200001]; + +int dist[200001]; + +int sol = 0; + +int solEdge[200001]; + +int main() { + ofstream out("apm.out"); + + ifstream in("apm.in"); + + in >> n >> m; + + for (int i = 0; i < m; i++) { + int x, y, c; + + in >> x >> y >> c; + + x--; y--; + + g[x].push_back({y, c}); + + g[y].push_back({x, c}); + } + + for (int i = 0; i < 200001; i++) { + dist[i] = 1e9; + } + + dist[0] = 0; + + available.insert({0, 0}); + + while (!available.empty()) { + Edge curr = *available.begin(); + + available.erase(available.begin()); + + visited[curr.node] = true; + + int bestEdge = -1; + + for (int i = 0; i < g[curr.node].size(); i++) { + if (g[curr.node][i].cost < dist[g[curr.node][i].node] && !visited[g[curr.node][i].node]) { + dist[g[curr.node][i].node] = g[curr.node][i].cost; + + available.insert({g[curr.node][i].node, dist[g[curr.node][i].node]}); + + bestEdge = g[curr.node][i].node; + + solEdge[g[curr.node][i].node] = curr.node; + } + } + } + + for (int i = 0; i < n; i++) { + sol += dist[i]; + } + + out << sol << '\n'; + + out << n - 1 << '\n'; + + for (int i = 1; i < n; i++) { + out << solEdge[i] + 1 << " " << i + 1 << '\n'; + } + + return 0; +} diff --git a/mst/prim/e8b1520c-6228-4122-b2f2-f990b402a276.cpp b/mst/prim/e8b1520c-6228-4122-b2f2-f990b402a276.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a3180c3d3bbe583a05a75dece056fd46667d946 --- /dev/null +++ b/mst/prim/e8b1520c-6228-4122-b2f2-f990b402a276.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + +constexpr int NX = 200010; + +constexpr int inf = 1010; + +int N, M, cost; + +int dist[NX], daddy[NX]; + +bool viz[NX]; + +typedef pair Pair; + +vector V[NX]; + +priority_queue < Pair, vector < Pair >, greater < Pair >> pq; + + + +int main() + +{ + + fin>>N>>M; + + int x, y, z; + + for(int i=1; i<=M; ++i) + + { + + fin>>x>>y>>z; + + V[x].push_back(make_pair(y, z)); + + V[y].push_back(make_pair(x, z)); + + } + + + + for(int i=1; i<=N; ++i) + + { + + dist[i]=inf; + + daddy[i]=-1; + + } + + + + + + pq.push(make_pair(0, 1)); + + dist[1] = 0; + + daddy[1] = 0; + + while(!pq.empty()) + + { + + int prim = pq.top().second; + + pq.pop(); + + if(viz[prim]==0) + + { + + viz[prim] = 1; + + cost+=dist[prim]; + + for(auto &i : V[prim]) + + { + + int nod=i.first; + + if(viz[nod]==0 && i.second < dist[nod]) + + { + + + + dist[nod]=i.second; + + pq.push(make_pair(dist[nod], nod)); + + daddy[nod]=prim; + + } + + + + } + + } + + } + + + + fout< + + + + +using namespace std; + + + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +int n, m, t[200001], ct,viz[200001],d[200001]; + +struct pereche + +{ + + int y,c; + +}; + + + +struct muchie + +{ + + int x, y, c; + +}; + + + +struct cmp + +{ + + bool operator()(muchie u, muchie v) + + { + + return u.c > v.c; + + } + +}; + + + +priority_queue,cmp> q; + + + + + +vector G[200001]; + + + +void citeste() + +{ + + int x,y,i,cost; + + fin >> n >> m; + + for(i=1;i<=n;i++) + + d[i]=10000; + + for(i=1; i<=m; i++) + + { + + pereche p; + + fin >> x >> y >> cost; + + p={y,cost}; + + G[x].push_back(p); + + p={x,cost}; + + G[y].push_back(p); + + } + +} + + + +int main() + +{ + + int i,nr; + + muchie e; + + pereche p; + + + + citeste(); + + for(i=0;ip.c) + + { + + q.push({e.y, p.y,p.c}); + + d[p.y]=p.c; + + } + + } + + } + + } + + fout << ct << '\n'; + fout< + + +using namespace std; + + + +ifstream in("apm.in"); + +ofstream out("apm.out"); + + + +pair h[400001]; + + + +vector < pair > g[200001]; + + + +int n,cost[200001],fr[200001],parent[200001]; + + + +int father(int x) + +{ + + return x/2; + +} + + + +int lson(int x) + +{ + + return 2*x; + +} + + + +int rson(int x) + +{ + + return 2*x+1; + +} + + + +void sift(int k) + +{ + + int son=lson(k); + + while(son!=0) + + { + + son=lson(k); + + if(son<=n) + + { + + if(son+1<=n && h[son+1].first aux=h[k]; + + while(k>1 && h[father(k)].first>aux.first) + + { + + h[k]=h[father(k)]; + + k=father(k); + + } + + h[k]=aux; + +} + + + +void cut(int k) + +{ + + h[k]=h[n]; + + n--; + + if(k>1 && h[k].first val) + +{ + + h[++n]=val; + + percolate(n); + +} + + + +int main() + +{ + + int p,m,i,j,x,y,c,s=0; + + in>>p>>m; + + for(i=1;i<=m;i++) + + { + + in>>x>>y>>c; + + g[x].push_back({y,c}); + + g[y].push_back({x,c}); + + cost[i]=1e9; + + } + + cost[1]=0; + + h_insert({0,1}); + + while(n>0) + + { + + x=h[1].second; + + cut(1); + + if(fr[x]==0) + + { + + fr[x]=1; + + for(i=0;i +#include +using namespace std; + +ifstream cin("apm.in"); + +ofstream cout("apm.out"); + + + +vector > v[400001]; + +vector> vrez; + +int used[200001],cost[200001],cor[200001]; + +priority_queue > pq; + +int intro_apm(int nod){ + + int i,l=v[nod].size(); + + for(i=0;i-(v[nod][i].first)) + + { + + pq.push({v[nod][i]}); + + cost[v[nod][i].second]=-(v[nod][i].first); + + cor[v[nod][i].second]=nod; + + } + + } + +} + +int main() + +{ + + int n,m,i,a,b,c,costf=0,l,x; + + cin>>n>>m; + + for(i=1;i<=m;i++) + + { + + cin>>a>>b>>c; + + v[a].push_back({-c,b}); + + v[b].push_back({-c,a}); + + } + + for(i=2;i<=n;i++) + + { + + cost[i]=1e9; + + pq.push({-cost[i],i}); + + x=pq.top().second; + + } + + intro_apm(1); + + used[1]=1; + + for(i=1;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 200000; + +const int CMAX = 1000; + + + +struct elem { + + int nod, cost; + + + + elem(int cnod = 0, int ccost = 0) { + + nod = cnod; + + cost = ccost; + + } + +}; + + + +bool operator > (elem e1, elem e2) { + + return e1.cost > e2.cost; + +} + + + +int n, m, APM; + +vector graf[NMAX + 5]; + +priority_queue, greater> pq; + +bool sel[NMAX + 5]; + +int cmin[NMAX + 5]; + +int tata[NMAX + 5]; + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + + + scanf("%d %d", &n, &m); + + + + for(int i = 1; i <= m; i++) { + + int x, y, c; + + + + scanf("%d %d %d", &x, &y, &c); + + + + graf[x].push_back(elem(y, c)); + + graf[y].push_back(elem(x, c)); + + } + + + + for(int i = 1; i <= n; i++) + + cmin[i] = CMAX + 5; + + + + cmin[1] = 0; + + pq.push(elem(1, 0)); + + + + while(!pq.empty()) { + + elem crt = pq.top(); + + pq.pop(); + + if(crt.cost > cmin[crt.nod] || sel[crt.nod]) + + continue; + + + + sel[crt.nod] = true; + + APM += crt.cost; + + + + + + for(elem vec: graf[crt.nod]) + + + + + + if(!sel[vec.nod] && vec.cost < cmin[vec.nod]) { + + cmin[vec.nod] = vec.cost; + + tata[vec.nod] = crt.nod; + + pq.push(elem(vec.nod, vec.cost)); + + } + + } + + + + + + printf("%d\n%d\n", APM, n - 1); + + for(int i = 2; i <= n; i++) + + printf("%d %d\n", tata[i], i); + + + + return 0; + +} diff --git a/mst/prim/ea4a7823-524f-469b-bba6-2e276c8b56d5.cpp b/mst/prim/ea4a7823-524f-469b-bba6-2e276c8b56d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..836c506d5b09cecf04a4d6976ae843fb345934c4 --- /dev/null +++ b/mst/prim/ea4a7823-524f-469b-bba6-2e276c8b56d5.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +FILE *fin = fopen("apm.in", "r"); + +FILE *fout = fopen("apm.out", "w"); + + + +typedef pair pi; + + + +priority_queue < pi, vector , greater > pq; + +vector graf[200005]; + + + +int n,m,x,y,c,cost[200005],tata[200005],nod,nod2,costmuch,ans,i; + +bool inAPM[200005]; + + + +int main() + +{ + + fscanf(fin, "%d%d", &n,&m); + + for(i=1; i<=m; ++i) + + { + + fscanf(fin, "%d%d%d", &x,&y,&c); + + graf[x].push_back(make_pair(y, c)); + + graf[y].push_back(make_pair(x, c)); + + } + + + + + + for(i=1; i<=n; ++i) + + cost[i] = 1e9; + + + + pq.push(make_pair(0, 1)); + + while(!pq.empty()) + + { + + nod = pq.top().second; + + pq.pop(); + + inAPM[nod] = true; + + + + for(i=0; i +#include +#include +#include + +using namespace std; + +ifstream fin ("apm.in"); +ofstream fout ("apm.out"); + +int f[200001], pereche[200001], cost[200001], n, m; +int cost_total; + +struct tip{ + int nod, ct; + + bool operator <(const tip &other) const{ + return ct < other.ct; + }; + +}; + +vector l[200001]; +multiset stiva; + +struct tip2{ + int x, y; +}final[200001]; + +void intro_apm(int nod) +{ + int x, c, i; + f[nod] = 1; + + for (i = 0; i< l[nod].size(); i++) + { + x = l[nod][i].nod; + c = l[nod][i].ct; + if(cost[x] > c) + { + cost[x] = c; + stiva.insert(l[nod][i]); + pereche[x] = nod; + } + } + +} + +int main() +{ + int C, i, j, x, y; + fin >> n >> m; + + for (i = 2; i<= n; i++) + cost[i] = 2000000001; + + for (i = 1; i<= m; i++) + { + fin >> x >> y >> C; + l[x].push_back({y, C}); + l[y].push_back({x, C}); + } + + for (i = 2; i <= n; i++) + stiva.insert({i, cost[i]}); + + intro_apm(1); + + for (i = 2; i<= n; i++) + { + x = stiva.begin()->nod; + while (f[x] == 1) + { + stiva.erase(stiva.begin()); + x = stiva.begin()->nod; + } + stiva.erase(stiva.begin()); + intro_apm(x); + + cost_total += cost[x]; + final[i-1].x = x; + final[i-1].y = pereche[x]; + } + + fout << cost_total << '\n' << n-1 << '\n'; + for (i = 1; i<= n-1; i++) + fout << final[i].x << " " << final[i].y << '\n'; +} diff --git a/mst/prim/eacf3904-1363-47ca-8309-5fe0daed753f.cpp b/mst/prim/eacf3904-1363-47ca-8309-5fe0daed753f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10c63e756989e81c9422fb78b8b9fa6db6e5d551 --- /dev/null +++ b/mst/prim/eacf3904-1363-47ca-8309-5fe0daed753f.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + +ifstream f("apm.in"); + +ofstream g("apm.out"); + +priority_queue >, vector > >, greater > > >V; + +vector >v[200005]; + +int n, m, x, y, c, viz[200005]; + +struct { + + int x, y; + +} sol[200005]; + +int prim(int nod) + +{ + + int i, cost = 0, b = 0, j, nodaux; + + viz[nod] = 1; + + for (i = 1; i <= n - 1; i++) + + { + + for (j = 0; j < v[nod].size(); j++) + + { + + nodaux = v[nod][j].second; + + b = v[nod][j].first; + + if (!viz[nodaux]) + + V.push({ b,{nod,nodaux} }); + + } + + while (viz[V.top().second.second]) + + V.pop(); + + cost += V.top().first; + + viz[V.top().second.second] = 1; + + sol[i].x = V.top().second.first; + + sol[i].y = V.top().second.second; + + nod = V.top().second.second; + + V.pop(); + + } + + return cost; + +} + +int main() + +{ + + int i; + + f >> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y >> c; + + v[x].push_back({ c,y }); + + v[y].push_back({ c,x }); + + } + + g << prim(1) << '\n' << n - 1 << '\n'; + + for (i = 1; i < n; i++) + + g << sol[i].x << " " << sol[i].y << '\n'; + + return 0; + +} diff --git a/mst/prim/eb3a3147-3a8d-4410-9d40-e686b8d52687.cpp b/mst/prim/eb3a3147-3a8d-4410-9d40-e686b8d52687.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c82e78710ffbbdf5bac5df1e8dee661d88c900b4 --- /dev/null +++ b/mst/prim/eb3a3147-3a8d-4410-9d40-e686b8d52687.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +struct muchie { + + int nod; + + int cost; + + bool operator <(const muchie& alt) const{ + + return this->cost < alt.cost; + + } + +}; + + + +const int N = 2e5, INF = 1 << 30; + + + +vector gr[N]; + +multiset s; + +int cmin[N], cor[N]; + +bool exista[N]; + +pair rez[N]; + + + +void cost_minim(int nod) { + + for (auto i : gr[nod]) + + if (!exista[i.nod] && i.cost < cmin[i.nod]) { + + cmin[i.nod] = i.cost; + + cor[i.nod] = nod; + + s.insert(i); + + } + +} + + + +int main() { + + ifstream in("apm.in"); + + ofstream out("apm.out"); + + + + int n, m, x, y, c; + + in >> n >> m; + + while (m--) { + + in >> x >> y >> c; + + --x, --y; + + gr[x].push_back({ y, c }); + + gr[y].push_back({ x, c }); + + } + + for (int i = 0; i < N; ++i) + + cmin[i] = INF; + + cost_minim(0); + + exista[0] = true; + + int nod, cost, ctot = 0; + + for (int i = 1; i < n; ++i) { + + nod = s.begin()->nod; + + while (exista[nod]) { + + s.erase(s.begin()); + + nod = s.begin()->nod; + + } + + s.erase(s.begin()); + + exista[nod] = true; + + cost = cmin[nod]; + + ctot += cost; + + rez[i - 1] = { nod, cor[nod] }; + + cost_minim(nod); + + } + + out << ctot << '\n' << n - 1 << '\n'; + + for (int i = 0; i < n - 1; ++i) + + out << rez[i].first + 1 << ' ' << rez[i].second + 1 << '\n'; + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/mst/prim/ec68f14b-0be7-4c2e-a820-58b4652d64d3.cpp b/mst/prim/ec68f14b-0be7-4c2e-a820-58b4652d64d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85108ff15cdc9b7c8320e05a15e637c2deacc331 --- /dev/null +++ b/mst/prim/ec68f14b-0be7-4c2e-a820-58b4652d64d3.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; +const int maxN = 2e5 + 5; +typedef tuple triplePair; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +int n, m, totalCost; +vector> graph[maxN], compressed; +bitset visited; + +class smallestCost { + public: + bool operator()(const triplePair &a, const triplePair &b) const { + return get<0>(a) > get<0>(b); + } +}; +priority_queue, smallestCost> available; + +void makeAvailable(int node) { + visited[node] = true; + for (unsigned i = 0; i < graph[node].size(); ++i) { + pair neighbour = graph[node][i]; + if (!visited[neighbour.first]) + available.emplace(make_tuple(neighbour.second, neighbour.first, node)); + } +} + +void calcNextMove() { + while (!available.empty() && visited[get<1>(available.top())]) + available.pop(); + if (available.empty()) + return; + + triplePair nextMove = available.top(); + available.pop(); + makeAvailable(get<1>(nextMove)); + + totalCost += get<0>(nextMove); + compressed.emplace_back(make_pair(get<2>(nextMove), get<1>(nextMove))); +} + +int main() { + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y, cost; + fin >> x >> y >> cost; + graph[x].emplace_back(make_pair(y, cost)); + graph[y].emplace_back(make_pair(x, cost)); + } + + makeAvailable(1); + while (!available.empty()) + calcNextMove(); + + fout << totalCost << "\n" << compressed.size() << "\n"; + for (pair edge : compressed) + fout << edge.first << " " << edge.second << "\n"; + return 0; +} diff --git a/mst/prim/ec8ebe9f-a5af-4d86-af27-6c6b7b7221d5.cpp b/mst/prim/ec8ebe9f-a5af-4d86-af27-6c6b7b7221d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49338ae372f6106209b48c6eae6286c8b96017a0 --- /dev/null +++ b/mst/prim/ec8ebe9f-a5af-4d86-af27-6c6b7b7221d5.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +const int INFINIT = 1005; + +const int NMAX = 200005; + + + +vector < vector < pair < int, int > > > costuri(NMAX); + +vector < int > vx; + +vector < int > vy; + +int tati[NMAX], N, M; + +bool vizitat[NMAX]; + +int dist[NMAX]; + +long long cost_minim; + +int nod_start = -1; + + + +set < pair < int, int > > heap; + + + +void init() + +{ + + for(int i = 0; i < NMAX; i++) + + dist[i] = INFINIT; + +} + + + +void citire() + +{ + + fin >> N >> M; + + int x, y, cost; + + int cost_min = 1005; + + for(int i = 0; i < M; i++) + + { + + fin >> x >> y >> cost; + + pair < int, int > pair_1 = make_pair(y, cost); + + pair < int, int > pair_2 = make_pair(x, cost); + + costuri[x].push_back(pair_1); + + costuri[y].push_back(pair_2); + + if(cost < cost_min) + + { + + cost_min = cost; + + nod_start = x; + + } + + } + + init(); + +} + + + +void Prim() + +{ + + for(unsigned int i = 0; i < costuri[nod_start].size(); i++) + + { + + tati[costuri[nod_start][i].first] = nod_start; + + heap.insert( {costuri[nod_start][i].second, costuri[nod_start][i].first} ); + + dist[costuri[nod_start][i].first] = costuri[nod_start][i].second; + + } + + vizitat[nod_start] = 1; + + + + while(!heap.empty()) + + { + + int mini = heap.begin() -> second; + + int cost = heap.begin() -> first; + + heap.erase(heap.begin()); + + + + if(vizitat[mini]) + + continue; + + cost_minim += cost; + + vizitat[mini] = 1; + + vx.push_back(tati[mini]); vy.push_back(mini); + + for(unsigned int k = 0; k < costuri[mini].size(); k++) + + if(!vizitat[costuri[mini][k].first]) + + { + + if(costuri[mini][k].second < dist[costuri[mini][k].first]) + + { + + dist[costuri[mini][k].first] = costuri[mini][k].second; + + tati[costuri[mini][k].first] = mini; + + } + + heap.insert( {costuri[mini][k].second, costuri[mini][k].first} ); + + } + + } + +} + + + +void afisare() + +{ + + fout << cost_minim << '\n'; + + fout << N - 1 << '\n'; + + for(int i = 0; i < N - 1; i++) + + fout << vx[i] << ' ' << vy[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + Prim(); + + afisare(); + + return 0; + +} diff --git a/mst/prim/ecc8d996-d805-448e-a14f-6079d1564d3d.cpp b/mst/prim/ecc8d996-d805-448e-a14f-6079d1564d3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8c63996f8608829117d6871c55fc8336280080e --- /dev/null +++ b/mst/prim/ecc8d996-d805-448e-a14f-6079d1564d3d.cpp @@ -0,0 +1,162 @@ +#include + + + + +using namespace std; + +const int DN=1e6+5; + +int n,m,pr[DN],sol[DN],f,cnt,g,cost,viz[DN]; + +pair >mc[DN]; + +set >s; + +long long sum; + +vector >v[DN]; + + + +int main() + +{ + + FILE *fin=fopen("apm.in","r"); + + FILE *fout=fopen("apm.out","w"); + + + + fscanf(fin,"%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + fscanf(fin,"%d%d%d",&mc[i].second.first,&mc[i].second.second,&mc[i].first); + + f=mc[i].second.first; + + g=mc[i].second.second; + + cost=mc[i].first; + + v[f].push_back({g,cost}); + + v[g].push_back({f,cost}); + + } + + for(int i=0;ifirst; + + int nod=s.begin()->second; + + s.erase(s.begin()); + + viz[nod]=1; + + for(auto i:v[nod]) + + if(!viz[i.first]) + + { + + if(sol[i.first]<=i.second) + + continue; + + s.erase(s.find({sol[i.first],i.first})); + + sol[i.first]=i.second; + + s.insert({sol[i.first],i.first}); + + pr[i.first]=nod; + + } + + } + + + + + + fprintf(fout,"%lld\n",sum); + + fprintf(fout,"%d\n",cnt); + + for(int i=0;i<=n;i++) + + { + + if(pr[i]==-1) + + continue; + + fprintf(fout,"%d %d\n",i,pr[i]); + + } + + + +} diff --git a/mst/prim/ed4bed87-aa4e-4a31-8998-febeaa79fb53.cpp b/mst/prim/ed4bed87-aa4e-4a31-8998-febeaa79fb53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b5b5c20dcaecd48912a3130f175355e4f07ff91 --- /dev/null +++ b/mst/prim/ed4bed87-aa4e-4a31-8998-febeaa79fb53.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +const int N_MAX = 1000005; + +std :: vector > graph[N_MAX], apm; +std :: priority_queue > > pq; +bool vis[N_MAX]; +int n, m, t_cost; +int start; + +int main(){ + + std :: ifstream fin("apm.in"); + std :: ofstream fout("apm.out"); + + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y, c; + fin >> x >> y >> c; + graph[x].push_back(std :: make_pair(y, c)); + graph[y].push_back(std :: make_pair(x, c)); + + } + + start = 1; + vis[start] = true; + for (auto i : graph[start]) { + pq.push({-i.second, {start, i.first }}); + } + + + while(!pq.empty()) { + std :: pair > best_cost = pq.top(); + + pq.pop(); + + int cost = best_cost.first; + int next = best_cost.second.second; + + if (!vis[next]) { + vis[next] = true; + apm.push_back(best_cost.second); + + t_cost += -cost; + + for (auto i : graph[next]) { + pq.push({-i.second, {next, i.first}}); + } + } + } + + fout << t_cost << "\n" << n-1 << '\n'; + for (auto i : apm) fout << i.first << " " << i.second << '\n'; + return 0; +} diff --git a/mst/prim/f0800772-e2d3-49ec-bd8f-d2bf74f3d98d.cpp b/mst/prim/f0800772-e2d3-49ec-bd8f-d2bf74f3d98d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4830080c6017509d3b3387df8a41a329bcd19b78 --- /dev/null +++ b/mst/prim/f0800772-e2d3-49ec-bd8f-d2bf74f3d98d.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +priority_queue < pair , vector < pair > , greater < pair > > q; + +vector < pair > v[200100]; + +int CT,n,viz[200100],cost_muchie[200100],tata[200100]; + + + +void prim (int nod_start) + +{ + + for(int i=1;i<=n;i++)cost_muchie[i]=INT_MAX/2; + + cost_muchie[nod_start]=0; + + q.push({0,nod_start}); + + + + while(!q.empty()) + + { + + int nod=q.top().second; + + int cost_muchie_nod=q.top().first; + + q.pop(); + + + + if(viz[nod]==0) + + { + + viz[nod]=1; + + CT+=cost_muchie_nod; + + + + for(int i=0;icost_intre && viz[vecin]==0) + + { + + cost_muchie[vecin]=cost_intre; + + q.push({cost_muchie[vecin],vecin}); + + tata[vecin]=nod; + + } + + } + + } + + } + +} + + + +int m,i,x,y,cost; + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>cost; + + v[x].push_back({y,cost}); + + v[y].push_back({x,cost}); + + } + + + + prim(1); + + + + g< +# include +# include + + +using namespace std; + + + +FILE *f = freopen("apm.in", "r", stdin); + +FILE *g = freopen("apm.out", "w", stdout); + + + +const int N_MAX = 200001; + + + +struct vecin{ + + int capat; + + int cost; + + + + vecin (int &a, int &b) + + { + + this -> capat= a; + + this -> cost= b; + + } + +}; + + + +struct muchie{ + + int capat1; + + int capat2; + + int cost; + + + + muchie(int &a, int &b, int &c) + + { + + this -> capat1 = a; + + this -> capat2= b; + + this -> cost= c; + + } + + + + const bool operator <(const muchie &other) const + + { + + return (this ->cost > other.cost); + + } + +}; + + + +int n, m, sol; + +int rez[N_MAX]; + +vector G[N_MAX]; + +vector solutie; + +priority_queue candidati; + + + +int main() + +{ + + scanf("%d %d", &n, &m); + + + + for (int i=1; i<=m; i++) + + { + + int x, y, c; + + scanf("%d %d %d", &x, &y, &c); + + + + G[x].push_back(vecin(y,c)); + + G[y].push_back(vecin(x,c)); + + } + + rez[1] = true; + + sol = 1; + + + + for (vecin i : G[1]) + + candidati.push(muchie (sol, i.capat, i.cost)); + + + + sol = 0; + + + + while (!candidati.empty()) + + { + + muchie v = candidati.top(); + + candidati.pop(); + + + + if (!rez[v.capat2]) + + { + + int nod_nou = v.capat2; + + + + rez[nod_nou] = true; + + sol += v.cost; + + + + solutie.push_back(vecin(v.capat1, v.capat2)); + + + + for (vecin i :G[nod_nou]) + + { + + if (!rez[i.capat]) + + { + + candidati.push(muchie(nod_nou, i.capat, i.cost)); + + } + + } + + } + + } + + + + printf("%d\n", sol); + + printf("%u\n", solutie.size()); + + for (int i=0; i +using namespace std; + +ifstream f("apm.in"); + +ofstream o("apm.out"); + +typedef pair pii; + +vector adj[200001]; + +bitset<200001> APM; + +int n, m; + +void Prim() + +{ + + int x; + + priority_queue, greater > q; + + vector cost(n + 1, 1000001); + + vector t(n + 1, 0); + + q.push({ 0,1 }); + + cost[1] = 0; + + t[1] = 0; + + while (!q.empty()) + + { + + x = q.top().second; + + q.pop(); + + APM[x] = true; + + for (auto i : adj[x]) + + { + + if (!APM[i.first] && cost[i.first] > i.second) + + { + + cost[i.first] = i.second; + + q.push({ cost[i.first], i.first }); + + t[i.first] = x; + + } + + } + + } + + int val = 0; + + for (int i = 1; i <= n; i++) + + val += cost[i]; + + o << val<> n >> m; + + for(int i=1;i<=m;i++) + + { + + f >> x >> y >> c; + + adj[x].push_back({ y,c }); + + adj[y].push_back({ x, c }); + + } + + Prim(); + + return 0; + +} diff --git a/mst/prim/f3aace3f-a4af-4d49-a6a6-344f2d000e36.cpp b/mst/prim/f3aace3f-a4af-4d49-a6a6-344f2d000e36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dffe58876d408e0cf56759a4f044a2a61b76be24 --- /dev/null +++ b/mst/prim/f3aace3f-a4af-4d49-a6a6-344f2d000e36.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("apm.in"); + +ofstream fout("apm.out"); + + + + + +typedef pair pii; + +vector adj[200001]; + +bitset<200001> inAPM; + +int n, m; + +void Prim() + +{ + + int x; + + priority_queue, greater > q; + + vector cost(n + 1, 0x3f3f3f3f); + + vector t(n + 1, 0); + + q.push({ 0,1 }); + + cost[1] = 0; + + t[1] = 0; + + while (!q.empty()) + + { + + x = q.top().second; + + q.pop(); + + inAPM[x] = true; + + for (auto i : adj[x]) + + { + + if (!inAPM[i.first] && cost[i.first] > i.second) + + { + + cost[i.first] = i.second; + + q.push({ cost[i.first], i.first }); + + t[i.first] = x; + + } + + } + + } + + int ansval = 0; + + for (int i = 1; i <= n; i++) + + ansval += cost[i]; + + fout << ansval<< "\n"; + + fout << n - 1 << "\n"; + + for (unsigned int i = 2; i < t.size(); i++) + + fout << t[i] << " " << i << "\n"; + +} + +int main() + +{ + + int x, y, cost; + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y >> cost; + + adj[x].push_back({ y,cost }); + + adj[y].push_back({ x, cost }); + + } + + Prim(); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/mst/prim/f3e06f57-5d2e-4670-abc3-3197b4479c9f.cpp b/mst/prim/f3e06f57-5d2e-4670-abc3-3197b4479c9f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..302a1cb7c7f870834a85c60e051c5e1d0e7c3ddf --- /dev/null +++ b/mst/prim/f3e06f57-5d2e-4670-abc3-3197b4479c9f.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector >> G[200003]; + +priority_queue >, vector>>, greater>>> H; + +vector > sol; + + + +int N, M, X, Y, C; + +long long sum; + +bool sel[200003]; + + + +void solve() { + + for (int i = 0; i < G[1].size(); i++) + + H.push(G[1][i]); + + + + sel[1] = true; + + + + for (int i = 1; i < N; i++) { + + while (sel[H.top().second.second]) H.pop(); + + + + auto curr = H.top(); + + sol.push_back({ curr.second.first, curr.second.second }); + + sum += curr.first; + + sel[curr.second.second] = true; + + + + for (int j = 0; j < G[curr.second.second].size(); j++) { + + if (sel[G[curr.second.second][j].second.second] == 0) { + + H.push(G[curr.second.second][j]); + + } + + } + + } + +} + +int main() + +{ + + f >> N >> M; + + + + for (int i = 1; i <= M; i++) { + + f >> X >> Y >> C; + + G[X].push_back({ C, {X, Y} }); + + G[Y].push_back({ C, {Y, X } }); + + } + + + + solve(); + + + + g << sum << "\n" << sol.size() << "\n"; + + + + for (auto i : sol) + + g << i.first << " " << i.second << "\n"; + + return 0; + +} diff --git a/mst/prim/f615d8ea-1ca0-4fcd-b396-6dc9c981b2d8.cpp b/mst/prim/f615d8ea-1ca0-4fcd-b396-6dc9c981b2d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e69f621130bcaa55bbddd70c67121ad433792227 --- /dev/null +++ b/mst/prim/f615d8ea-1ca0-4fcd-b396-6dc9c981b2d8.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +typedef pair < int, int > Pair; + +int N, M, ans; + +vector < Pair > G[200002]; + +list < Pair > sol; + + + +void Read () { + + fin >> N >> M; + + while (M --) { + + int u, v, wt; + + fin >> u >> v >> wt; + + G[u].push_back ({v, wt}); + + G[v].push_back ({u, wt}); + + } + +} + + + +void Prim () { + + priority_queue < Pair, vector < Pair > , greater < Pair > > Heap; + + vector < int > key (N + 2, INT_MAX), + + parent (N + 2); + + vector < bool > viz (N + 2, 0); + + Heap.push ({0, 1}); + + key[1] = 0; + + while (!Heap.empty()) { + + int u = Heap.top().second; + + Heap.pop(); + + viz[u] = true; + + for (auto it : G[u]) { + + int v = it.first, weight = it.second; + + if (viz[v] == false && key[v] > weight) { + + key[v] = weight; + + Heap.push ({key[v], v}); + + parent[v] = u; + + } + + } + + } + + for (int i = 2; i <= N; i ++) { + + sol.push_back ({parent[i], i}); + + ans += key[i]; + + } + +} + + + +void Solve () { + + Read(); + + Prim(); + + fout << ans << '\n' << N - 1 << '\n'; + + for (auto it : sol) + + fout << it.first << ' ' << it.second << '\n'; + +} + + + +int main() { + + Solve(); + + return 0; + +} diff --git a/mst/prim/f62fd4f2-34b5-448c-932c-5fbb00a57e9c.cpp b/mst/prim/f62fd4f2-34b5-448c-932c-5fbb00a57e9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b9a65a7a895a1c1ee3ad0b7733aa2d308e579ed --- /dev/null +++ b/mst/prim/f62fd4f2-34b5-448c-932c-5fbb00a57e9c.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +const int MAX_N = 200000; +const int MAX_M = 400000; +const int INF = 1000000000; + +bool marked[1+MAX_N]; + +struct Edge { + int a, b, c; + + bool operator< (const Edge& x) const { + return c > x.c; + } +} edges[1+MAX_M]; + +priority_queue pq; +vector graph[1+MAX_N]; + +void markNode(int node) { + marked[node] = true; + for(auto it: graph[node]) + pq.push(edges[it]); +} + +int top; +int apmA[1+MAX_N], apmB[1+MAX_N]; + +int main() { + int N, M, sumapm = 0; + + FILE *fin = fopen("apm.in", "r"); + fscanf(fin, "%d%d", &N, &M); + + for(int i = 0; i < M; ++i) { + int a, b, c; + fscanf(fin, "%d%d%d", &a, &b, &c); + edges[i] = {a, b, c}; + graph[a].push_back(i); + graph[b].push_back(i); + } + fclose(fin); + + markNode(1); + while(!pq.empty()) { + Edge edge = pq.top(); + pq.pop(); + + if(marked[edge.a] + marked[edge.b] == 1) { + apmA[top] = edge.a; + apmB[top++] = edge.b; + sumapm += edge.c; + if(!marked[edge.a]) + markNode(edge.a); + else + markNode(edge.b); + } + } + + FILE *fout = fopen("apm.out", "w"); + fprintf(fout, "%d\n%d\n", sumapm, top); + for(int i = 0; i < top; ++i) + fprintf(fout, "%d %d\n", apmA[i], apmB[i]); + fclose(fout); + return 0; +} diff --git a/mst/prim/f87c654e-8bb7-4c20-b3a8-cfbee0176d3b.cpp b/mst/prim/f87c654e-8bb7-4c20-b3a8-cfbee0176d3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11e85fc309170fec05ea1624a30b72b62ffa3b58 --- /dev/null +++ b/mst/prim/f87c654e-8bb7-4c20-b3a8-cfbee0176d3b.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +vector< vector< pair > > w; + +vector< pair >apm; + +priority_queue< pair >, vector< pair > >, greater< pair > > >h; + + + +int s,i; + + + +bool b[200001]; + + + +void prim (int x) + +{ + + for(i = 0; i < w[x].size(); i++) + + if(!b[w[x][i].first]) + + h.push(make_pair(w[x][i].second, make_pair(x, w[x][i].first))); + + + + b[x] = 1; + + + + while ( !h.empty() && b[h.top().second.second]) + + h.pop(); + + + + + + if(!h.empty()) + + { + + int val = h.top().first, xn = h.top().second.first, yn = h.top().second.second; + + h.pop(); + + + + apm.push_back(make_pair(xn,yn)); + + s += val; + + + + prim(yn); + + } + + + +} + + + +int main() + +{ + + int n, m, x, y, cost; + + + + f >> n >> m; + + + + w.resize(n+1); + + + + for(i = 0; i < m; i++) + + { + + f>>x>>y>>cost; + + + + w[x].push_back(make_pair(y,cost)); + + w[y].push_back(make_pair(x,cost)); + + } + + + + prim(1); + + + + g< + + +typedef std::tuple edge; + + + +class Task { + public: + void solve() { + read_input(); + get_result(); + print_output(); + } + + int n, m, cost_apm, root; + std::vector d, p; + std::vector G[200009]; + std::vector apm; + std::priority_queue, std::greater> pq; + std::bitset<200009> used; + + void read_input() { + std::cin >> n >> m; + + for (int i = 1, x, y, c; i <= m; ++i) { + std::cin >> x >> y >> c; + G[x].push_back(edge(c, y)); + G[y].push_back(edge(c, x)); + } + cost_apm = 0; + } + + void get_result() { + p = std::vector(n + 1, 0); + d = std::vector(n + 1, INT_MAX); + + root = 1; + d[root] = 0; + p[root] = 0; + pq.push(edge(d[root], root)); + + for (int i = 1; i <= n; ++i) { + std::tuple e; + int node; + do { + e = pq.top(); + pq.pop(); + + node = std::get<1>(e); + } while (used[node]); + + cost_apm += d[node]; + used[node] = 1; + + for (auto it = G[node].begin(); it != G[node].end(); ++it) { + int neighbour = std::get<1>(*it), cost = std::get<0>(*it); + + if (!used[neighbour] && cost < d[neighbour]) { + d[neighbour] = cost; + p[neighbour] = node; + pq.push(edge(d[neighbour], neighbour)); + } + } + } + } + + void print_output() { + std::cout << cost_apm << "\n"; + std::cout << n - 1 << "\n"; + for (int i = 1; i <= n; ++i) { + if (i != root) { + std::cout << i << " " << p[i] << "\n"; + } + } + } +}; + +int main() { + auto cin_buff = std::cin.rdbuf(); + auto cout_buff = std::cout.rdbuf(); + + std::ifstream fin("apm.in"); + std::cin.rdbuf(fin.rdbuf()); + + std::ofstream fout("apm.out"); + std::cout.rdbuf(fout.rdbuf()); + + Task *task = new Task(); + task->solve(); + delete task; + + std::cin.rdbuf(cin_buff); + std::cout.rdbuf(cout_buff); + return 0; +} diff --git a/mst/prim/f96a6ddd-c329-42cd-b0cc-e2d8299b4e37.cpp b/mst/prim/f96a6ddd-c329-42cd-b0cc-e2d8299b4e37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dccbb7fb2a3b18f4902efd0d9a32800fb98f1d94 --- /dev/null +++ b/mst/prim/f96a6ddd-c329-42cd-b0cc-e2d8299b4e37.cpp @@ -0,0 +1,118 @@ +#include + + + + +using namespace std; + + + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + + + +typedef pair < int, int > Pair; + +int N, M, ans; + +vector < Pair > G[200002]; + +list < Pair > sol; + +priority_queue < Pair, vector < Pair > , greater < Pair > > Heap; + +vector < int > key (200002, INT_MAX), parent (200002); + +vector < bool > viz (200002); + + + +void Read () { + + fin >> N >> M; + + while (M --) { + + int u, v, wt; + + fin >> u >> v >> wt; + + G[u].push_back ({v, wt}); + + G[v].push_back ({u, wt}); + + } + +} + + + +void Prim () { + + Heap.push ({0, 1}); + + key[1] = 0; + + while (!Heap.empty()) { + + int u = Heap.top().second; + + Heap.pop(); + + viz[u] = true; + + for (auto it : G[u]) { + + int v = it.first, weight = it.second; + + if (viz[v] == false && key[v] > weight) { + + key[v] = weight; + + Heap.push ({key[v], v}); + + parent[v] = u; + + } + + } + + } + + for (int i = 2; i <= N; i ++) { + + sol.push_back ({parent[i], i}); + + ans += key[i]; + + } + +} + + + +void Solve () { + + Read(); + + Prim(); + + fout << ans << '\n' << N - 1 << '\n'; + + for (auto it : sol) + + fout << it.first << ' ' << it.second << '\n'; + +} + + + +int main() { + + Solve(); + + return 0; + +} diff --git a/mst/prim/fabc6790-6465-4b79-b063-199fa9162c49.cpp b/mst/prim/fabc6790-6465-4b79-b063-199fa9162c49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c310382c83aabdea305370f5406b842c678b61f2 --- /dev/null +++ b/mst/prim/fabc6790-6465-4b79-b063-199fa9162c49.cpp @@ -0,0 +1,63 @@ +#include + +using namespace std; + +ifstream fin("apm.in"); +ofstream fout("apm.out"); + +vector < vector < pair > > adjList; +int V, E; + +int main(){ + + fin >> V >> E; + + adjList.resize(V + 1); + + for(int e = 0; e < E; ++e){ + + int u, v, c; + fin >> u >> v >> c; + + adjList[u].push_back({v, c}); + adjList[v].push_back({u, c}); + } + + vector < pair > connEdge(V + 1, {0, INT_MAX}); + vector used(V + 1); + + set < pair > RB; + + used[1] = true; + for(pair &edge : adjList[1]) + connEdge[edge.first] = {1, edge.second}, RB.insert({edge.second, edge.first}); + + int mstCost = 0; + for(int it = 1; it < V; ++it){ + + + pair Find = *RB.begin(); + RB.erase(RB.begin()); + + int minCost = Find.first; + int addNode = Find.second; + + mstCost += minCost; + used[addNode] = true; + + for(pair &edge : adjList[addNode]) + if(!used[edge.first] and edge.second < connEdge[edge.first].second){ + + if(RB.count({connEdge[edge.first].second, edge.first})) + RB.erase({connEdge[edge.first].second, edge.first}); + + connEdge[edge.first] = {addNode, edge.second}; + RB.insert({edge.second, edge.first}); + } + } + + fout << mstCost << '\n' << V - 1 << '\n'; + + for(int node = 2; node <= V; ++node) + fout << node << ' ' << connEdge[node].first << '\n'; +} diff --git a/mst/prim/fd2c4e9c-bd72-4e03-94e7-ffc9251b11c0.cpp b/mst/prim/fd2c4e9c-bd72-4e03-94e7-ffc9251b11c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..488620dda7c2a1c4f4377b64774be235fc4cbf1c --- /dev/null +++ b/mst/prim/fd2c4e9c-bd72-4e03-94e7-ffc9251b11c0.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + + + +const int N = 200005; + + + +int n, m; + +int cost[N]; + +vector>graf[N]; + +vector> ans; + +bool amfost[N]; + + + +struct cmp{ + + bool operator()(pair> a,pair> b){ + + return a.second.second > b.second.second; + + } + +}; + + + +priority_queue> , vector>> , cmp> coada; + + + +void calc(int nod, int last) { + + cost[nod] = 0; + + coada.push(make_pair(nod, make_pair(last, cost[nod]))); + + + + while(coada.empty() == false) { + + last = coada.top().second.first; + + nod = coada.top().first; + + coada.pop(); + + + + if(amfost[nod] == true) { + + continue; + + } + + + + ans.push_back(make_pair(nod, last)); + + amfost[nod] = true; + + + + for (int i = 0; i < (int)graf[nod].size(); ++i) { + + + + int to = graf[nod][i].first; + + int drum = graf[nod][i].second; + + + + if(cost[to] > drum && amfost[to] == false){ + + cost[to] = drum; + + coada.push(make_pair(to, make_pair(nod, cost[to]))); + + } + + + + } + + + + } + + + +} + + + +int main() { + + ifstream fin("apm.in"); + + ofstream fout("apm.out"); + + + + fin >> n >> m; + + + + for (int i = 1; i <= n; ++i) + + cost[i] = INT_MAX; + + + + int costMin = INT_MAX, nodMin; + + for (int i = 1; i <= m; ++i) { + + int a, b, c; + + fin >> a >> b >> c; + + graf[a].push_back(make_pair(b,c)); + + graf[b].push_back(make_pair(a,c)); + + + + if(costMin > c) { + + costMin = c; + + nodMin = a; + + } + + } + + calc(nodMin, 0); + + + + int sum = 0; + + for (int i = 1; i <= n; ++i) + + sum += cost[i]; + + + + fout << sum << '\n' << (int)ans.size() - 1<< '\n'; + + for (int i = 1; i < (int)ans.size(); ++i) + + fout << ans[i].first << " " << ans[i].second << '\n'; + + + + return 0; + +} diff --git a/mst/prim/fd691fd4-59a6-415d-9747-04b2af7e28e6.cpp b/mst/prim/fd691fd4-59a6-415d-9747-04b2af7e28e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2137ca508aede903a6eb83bfd1e989172e26ca7c --- /dev/null +++ b/mst/prim/fd691fd4-59a6-415d-9747-04b2af7e28e6.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + + +using edge = std::tuple; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m, cost_apm; + + + std::vector edges[200010]; + std::priority_queue, std::greater> pq; + std::vector p; + + void read_input() { + std::ifstream fin("apm.in"); + fin >> n >> m; + for (int i = 0; i < m; i++) { + int src, dst, cost; + fin >> src >> dst >> cost; + edges[src].push_back(std::make_tuple(cost, dst)); + edges[dst].push_back(std::make_tuple(cost,src)); + } + fin.close(); + return ; + } + + void Prim() { + std::vector d(n + 1, (1 << 30)); + p = std::vector(n + 1, 0); + std::vector used(n + 1, false); + int root = 1; + d[root] = 0; + p[root] = 0; + pq.push(std::make_tuple(d[root], root)); + + for (int i = 1; i <= n; i++) { + std::tuple e; + int node; + do { + e = pq.top(); + pq.pop(); + node = std::get<1>(e); + } while (used[node]); + + cost_apm += d[node]; + used[node] = true; + for (auto e : edges[node]) { + int cost = std::get<0>(e); + int neigh = std::get<1>(e); + if (!used[neigh] && cost < d[neigh]) { + d[neigh] = cost; + p[neigh] = node; + pq.push(std::make_tuple(cost, neigh)); + } + } + } + + + } + + void get_result() { + Prim(); + } + + void print_output() { + std::ofstream fout("apm.out"); + fout<solve(); + delete task; + return 0; +} diff --git a/mst/prim/fd8ba5a5-2ed4-485c-b383-ccb09b9e687c.cpp b/mst/prim/fd8ba5a5-2ed4-485c-b383-ccb09b9e687c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2210bbb7f7af6d1f2926931d67efda4af5c647a3 --- /dev/null +++ b/mst/prim/fd8ba5a5-2ed4-485c-b383-ccb09b9e687c.cpp @@ -0,0 +1,260 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +vector>> arcs; + +vector> usedArcs; + +vector heap, poz, inTreeCost, parents; + + + +int heapSize; + + + + + +void add_to_apm(int x) { + + for(int i=0; i 1 && inTreeCost[heap[current_node]] < inTreeCost[heap[current_node / 2]]) { + + aux = heap[current_node]; + + heap[current_node] = heap[current_node / 2]; + + heap[current_node / 2] = aux; + + + + poz[heap[current_node]] = current_node; + + poz[heap[current_node / 2]] = current_node / 2; + + + + current_node /= 2; + + } + +} + + + + + +void sift(int current_node) { + + int aux, current_son = 1; + + while (current_son) { + + current_son = 0; + + if (current_node * 2 <= heapSize && inTreeCost[heap[current_node]] >inTreeCost[heap[current_node * 2]]) + + current_son = current_node * 2; + + + + if (current_node * 2 + 1 <= heapSize && inTreeCost[heap[current_node]] > inTreeCost[heap[current_node * 2 + 1]] && + + (current_son == 0 || inTreeCost[heap[current_son]] > inTreeCost[heap[current_node * 2 + 1]])) + + current_son = current_node * 2 + 1; + + + + if (current_son) { + + aux = heap[current_son]; + + heap[current_son] = heap[current_node]; + + heap[current_node] = aux; + + + + poz[heap[current_node]] = current_node; + + poz[heap[current_son]] = current_son; + + + + current_node = current_son; + + } + + } + +} + + + + + +void add_to_heap(int x) { + + ++heapSize; + + heap[heapSize] = x; + + poz[x] = heapSize; + + percolate(heapSize); + +} + + + + + +void delete_node(int current_node) { + + if (! heapSize) + + return; + + poz[heap[current_node]] = -1; + + heap[current_node] = heap[heapSize]; + + poz[heap[current_node]] = current_node; + + --heapSize; + + + + if (current_node > 1 && inTreeCost[heap[current_node / 2]] > inTreeCost[heap[current_node]]) + + percolate(current_node); + + else + + sift(current_node); + + + +} + + + + + +int main() { + + freopen("apm.in", "r", stdin); + + freopen("apm.out", "w", stdout); + + + + int n, m, a, b, c; + + scanf("%d%d", &n, &m); + + arcs.resize(n + 1); + + heap.resize(m * 2 + 10, 0); + + poz.resize(n + 1, 0); + + inTreeCost.resize(n+1, 1<<30); + + parents.resize(n+1, 0); + + + + for(int i=0; i(b, c)); + + arcs[b].push_back(pair(a, c)); + + } + + + + inTreeCost[1] = 0; + + add_to_apm(1); + + + + for(int i=2; i<=n; ++i) + + add_to_heap(i); + + + + c = 0; + + for(int i=1; i(parents[a], a)); + + + + for(int j=0; j +#include +#include +using namespace std; + +ifstream fin ("apm.in"); + +ofstream fout ("apm.out"); + +pair > elem, aux; + +priority_queue >, vector > >, greater > > > pq; + +queue > q2; + +pair elem2; + +int n, m, t[200005], cost, nrm, viz[200005], auxx; + +struct nod { int et,ct; + + nod *urm; + + } *l[200005]; + +void add_nod (int x, int y, int c) + +{ + + nod *q; + + q=new nod; + + q->et=y; + + q->ct=c; + + q->urm=l[x]; + + l[x]=q; + +} + +void citire () + +{ + + int x, y, co; + + fin>>n>>m; + + for (int i=1; i<=m; i++) + + { + + fin>>x>>y>>co; + + add_nod(y, x, co); + + add_nod(x, y, co); + + } + +} + +int main () + +{ + + int i; + + citire(); + + nod *q; + + q=l[1]; + + while (q!=NULL) + + { + + elem.first=q->ct; + + elem.second.first=1; + + elem.second.second=q->et; + + pq.push(elem); + + q=q->urm; + + } + + i=1; nrm=0; + + viz[1]=1; + + t[1]=0; + + while (ict; + + aux.second.first=x; + + aux.second.second=q->et; + + pq.push(aux); + + q=q->urm; + + } + + elem2.first=elem.second.first; + + elem2.second=elem.second.second; + + q2.push(elem2); + + i++; + + } + + fout< + + +using namespace std; + + + +ifstream f("apm.in"); + +ofstream g("apm.out"); + + + +set < pair < int , int > > s; + +vector < pair < int , int > > v[200005]; + +int totaldist,n,viz[200005],dist[200005],tata[200005]; + +int m,i,x,y,cost; + +int oo=2000000009; + + + +int main() + +{ + + f >> n >> m; + + for (i=1;i<=m;i++) { + + f >> x >> y >> cost; + + v[x].push_back(make_pair(y,cost)); + + v[y].push_back(make_pair(x,cost)); + + } + + for(int i=1;i<=n;i++) { + + dist[i]=oo; + + } + + dist[1]=0; + + s.insert(make_pair(0,1)); + + while (s.empty()==0) { + + int nod = s.begin()->second; + + int distnod = s.begin()->first; + + s.erase(s.begin()); + + if (viz[nod]==0) { + + viz[nod] = 1; + + totaldist += distnod; + + for (int i=0;i disttoviz && viz[nodtoviz]==0) { + + dist[nodtoviz] = disttoviz; + + s.insert(make_pair(dist[nodtoviz],nodtoviz)); + + tata[nodtoviz] = nod; + + } + + } + + } + + } + + g << totaldist << '\n'; + + g << n-1 << '\n'; + + for (i=1;i<=n;i++) { + + if (tata[i]!=0) { + + g << i << " " << tata[i] << '\n'; + + } + + } + + return 0; + +} diff --git a/mst/prim/fe7379b6-eb4b-4c70-bc76-8411cb09aaec.cpp b/mst/prim/fe7379b6-eb4b-4c70-bc76-8411cb09aaec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21ddb9d1e5896f1a17266b066cdbd298cf1afd13 --- /dev/null +++ b/mst/prim/fe7379b6-eb4b-4c70-bc76-8411cb09aaec.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; +const int size = 200001; +ifstream fin("apm.in"); +ofstream fout("apm.out"); +priority_queue>,greater>> heap; +vector> a[size]; +set rez; +int d[size], tata[size], isInHeap[size]; + +void init(int n) { + for(int i = 0; i <= n; ++i) { + d[i] = 20000000; + tata[i] = 0; + isInHeap[i] = 1; + } +} + +int main() { + int i, n, m, x, y, c, sum = 0; + fin >> n >> m; + init(n); + for(i = 1; i <= m; ++i) { + fin >> x >> y >> c; + a[x].push_back(make_pair(y, c)); + a[y].push_back(make_pair(x, c)); + } + d[1] = 0; + for(i = 1; i <= n; ++i) + heap.push(make_pair(d[i], i)); + while(!heap.empty() && heap.top().first != 20000000) { + pair p = heap.top(); + heap.pop(); + int u = p.second; + if(u != 1 && rez.find(u) == rez.end()){ sum += d[u]; rez.insert(u);} + isInHeap[u] = 0; + for(auto v:a[u]) { + if(isInHeap[v.first] == 1 && v.second < d[v.first]) { + d[v.first] = v.second; + tata[v.first] = u; + heap.push(make_pair(d[v.first], v.first)); + } + } + } + fout << sum << "\n" << rez.size() <<" \n"; + for(auto x:rez) + fout << x << " " << tata[x] << "\n"; + return 0; +} diff --git a/nrtri/binary_search/25f9114f-409f-11ec-875e-dc7196033e92.cpp b/nrtri/binary_search/25f9114f-409f-11ec-875e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41e7e164089ebbc82dc2ba1025efc363aa27a8e8 --- /dev/null +++ b/nrtri/binary_search/25f9114f-409f-11ec-875e-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int v[801],n,k; +int main() +{ + fin >>n; + for (int i=1;i<=n;i++) + {fin >>v[i];} + sort(v+1,v+n+1); + for (int i=1;i=v[mij]) {sol=mij; + st=mij+1;} + else dr=mij-1;} + if (sol!=-1) k=k+sol-j;}} + fout < +#include +#include + +using namespace std; + +int v[807]; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int s,n,i,j,st,mij,dr,rez,r=0; + cin>>n; + for(i=1; i<=n; i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + { + rez=0; + s=v[i]+v[j]; + st=1; + dr=n; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]>s) + dr=mij-1; + if(v[mij]<=s) + { + rez=mij; + st=mij+1; + } + } + if(rez!=0) + r=r+rez-j; + } + cout< +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMAX = 800; + +int v[NMAX + 5]; + +int bs(int i, int j, int n) +{ + int rez = 0, pas = 1 << 10, x = v[i] + v[j]; + while(pas) + { + if (rez + pas < n && v[rez + pas] <= x) + rez += pas; + pas >>= 1; + } + return rez; +} + +int main() +{ + int n, i, j, k, s = 0; + fin >> n; + for (i = 0; i < n; ++i) + { + fin >> v[i]; + } + sort(v, v + n); + for(i = 0; i < n - 2 ; ++i) + { + for(j = i + 1 ; j < n - 1; ++j) + { + k = bs(i, j, n); + if (k > j) + { + s += k - j; + } + } + } + fout << s; + return 0; +} diff --git a/nrtri/binary_search/26a90027-409f-11ec-917b-dc7196033e92.cpp b/nrtri/binary_search/26a90027-409f-11ec-917b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bcff1feb9a6e1012a8eb07df463b69bde58cd7e --- /dev/null +++ b/nrtri/binary_search/26a90027-409f-11ec-917b-dc7196033e92.cpp @@ -0,0 +1,59 @@ +#include +#define N 1000 +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,a[N]; + +void QSort(int st,int dr) +{ if(sta[j]) + { aux=a[i]; + a[i]=a[j]; + a[j]=aux; + d=1-d; + } + i+=d; + j-=(1-d); + } + QSort(st,i-1); + QSort(i+1,dr); + } +} + +int VF(int i,int j) +{ + int x=a[i]+a[j],st=j+1,dr=n,r=0,m; + while(st<=dr) + { + m=st+(dr-st)/2; + if(a[m]>x) dr=m-1; + else r=m,st=m+1; + } + return r; +} + +int main() +{ + int i; + fin>>n; + for(i=1; i<=n; ++i) fin>>a[i]; + QSort(1,n); + int j,k,ct=0; + bool ok; + for(i=1; i<=n; ++i) + for(j=i+1; j<=n; ++j) + { + k=VF(i,j); + if(k!=0) ct+=k-j; + } + fout< +#include + +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); +int n,i,j,l,x,y,z,v[100005],ok,nr,r,mij,k; +int main() +{ + cin >>n; + for (i=1; i<=n; i++) + { + cin >>v[i]; + } + sort(v+1, v+n+1); + for (i=1; i<=n; i++) + { + x=v[i]; + for (j=i+1; j<=n; j++) + { + y=v[j]; + l=j+1; + r=n; + k=-1; + while (l<=r) + { + mij=(l+r)/2; + if (x+y>=v[mij]) + { + k=mij; + l=mij+1; + } + else + { + r=mij-1; + } + } + if (k!=-1) nr=nr+k-j; + } + } + cout < +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int v[1000]; +int main() +{ + int n,i=0,j=0,cnt=0; + cin>>n; + for(;i>v[i]; + sort(v,v+n); + for(i=0;i=v[j+1]) + { + int s=j,d=n,m; + while (d-s>1) + { + m=(s+d)/2; + if (v[i]+v[j]>=v[m]) + s=m; + else + d=m; + } + cnt+=s-j; + } + } + } + } + cout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,k,p,u,mij,nr,v[1000],sol; +int main() +{ + fin >>n; + for (i=1;i<=n;i++) + { + fin >>v[i]; + } + sort(v+1,v+n+1); + for (i=1;i<=n;i++) + { + for (j=i+1;j<=n;j++) + { + p=j+1; + u=n; + sol=-1; + while (p<=u) + { + mij=(p+u)/2; + if (v[i]+v[j]>=v[mij]) {sol=mij;p=mij+1;} + else u=mij-1; + } + if (sol!=-1) nr=nr+sol-j; + } + } + fout < +#include +#define N 805 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, a[N]; + +void citire() +{ + int i; + fin >> n; + for(i = 1; i <= n; ++ i) + fin >> a[i]; +} + +int cb(int p1, int p2, int val) +{ + int st, dr, mij; + int sol = 0; + st = p2 + 1; dr = n; + while(st <= dr) + { + mij = (st + dr) / 2; + if(val >= a[mij]) + { + st = mij + 1; + sol = mij; + } + else + dr = mij - 1; + } + return sol; +} + +int main() +{ + int i, j; + citire(); + sort(a+1, a+n+1); + int ct = 0; + for(i = 1; i +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +const int NMAX = 800; +int n, v[NMAX + 5], cnt; + +int caut_bin(int i, int j) +{ + int st = j + 1, dr = n, mij, last = -1; + while (st <= dr) + { + mij = (st + dr) / 2; + if (v[mij] <= v[i] + v[j]) + { + last = mij; + st = mij + 1; + } + else + { + dr = mij - 1; + } + } + return last; +} + +int main() +{ + in >> n; + for (int i = 1; i <= n; i++) + { + in >> v[i]; + } + sort(v + 1, v + n + 1); + for (int i = 1; i < n - 1; i++) + { + for (int j = i + 1; j < n; j++) + { + int k = caut_bin(i, j); + if (k != -1) + { + cnt += k - j; + } + } + } + out << cnt; + return 0; +} diff --git a/nrtri/binary_search/28cd5f70-409f-11ec-9496-dc7196033e92.cpp b/nrtri/binary_search/28cd5f70-409f-11ec-9496-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09bf2f9603e0415a27518774557f699e6e690507 --- /dev/null +++ b/nrtri/binary_search/28cd5f70-409f-11ec-9496-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int v[1000]; + int n; + fin >> n; + for (int i = 1; i <= n; i++) + fin >> v[i]; + + sort(v + 1, v + n + 1); + + int cate_triunghiuri = 0; + + for (int i = 1; i <= n; i++) + { + for (int j = i + 1; j<= n; j++) + { + int sum = v[i] + v[j]; + //vrem sa gasim pozitia k a.i. v[k] <= sum si v[k+1] > sum + //stim astfel ca toate elementele de la pozitia j pana la k + // vor fi valide pentru a face un triunghi + int st, dr, mij; + st = j; + dr = n; + while (st <= dr) + { + mij = (st + dr) / 2; + if (v[mij] <= sum) + st = mij + 1; + else + dr = mij - 1; + } + mij = st + dr; + mij /= 2; + if (v[mij] > sum) + mij--; + cate_triunghiuri += mij - j; + + } + } + + fout << cate_triunghiuri; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/296c8038-409f-11ec-a0fe-dc7196033e92.cpp b/nrtri/binary_search/296c8038-409f-11ec-a0fe-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b99ece7fc5c488401cb49587dadfc42e32a18c8 --- /dev/null +++ b/nrtri/binary_search/296c8038-409f-11ec-a0fe-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int n,V[805],st,dr,sol,S,mid,nrtri = 0; + fin>>n; + for(int i = 1;i<=n;i++) + { + fin>>V[i]; + } + sort(V+1,V+n+1); + for(int i = 1;i<=n;i++) + { + for(int j = i+1;j<=n;j++) + { + sol = 0; + S = V[i] + V[j]; + st = j+1; + dr = n; + while(st<=dr) + { + mid = (st+dr)/2; + if(V[mid]<=S) + { + sol = mid-j; + st = mid+1; + } + else + { + if(V[mid]>S) + { + dr = mid-1; + } + } + } + nrtri = nrtri + sol; + } + } + fout< +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMAX = 800; + +int v[NMAX + 5]; + +int bs(int i, int j, int n) +{ + int st = j + 1, dr = n - 1, mij, rez = -1, x = v[i] + v[j]; + while (st <= dr) + { + mij = (st + dr) / 2; + if (x < v[mij]) + { + dr = mij - 1; + } + else + { + rez = mij; + st = mij + 1; + } + } + return rez; +} + +int main() +{ + int n, i, j, k, s = 0; + fin >> n; + for (i = 0; i < n; ++i) + { + fin >> v[i]; + } + sort(v, v + n); + for(i = 0; i < n - 2 ; ++i) + for(j = i + 1 ; j < n - 1; ++j) + { + k = bs(i, j, n); + if (k != -1) + { + s += k - j; + } + } + fout << s; + return 0; +} diff --git a/nrtri/binary_search/2a14a103-409f-11ec-b787-dc7196033e92.cpp b/nrtri/binary_search/2a14a103-409f-11ec-b787-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cc9f13350c467a05927185f7de32d2432a59c97 --- /dev/null +++ b/nrtri/binary_search/2a14a103-409f-11ec-b787-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[805]; +int main() +{ + int n,i,j,st,dr,mij,rez=-1,cnt=0; + in>>n; + for(i=1;i<=n;++i) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n;++i) + { + for(j=i+1;j<=n;++j) + { + st=j+1;dr=n;rez=-1; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[i]+v[j]>=v[mij]) + { + rez=mij; + st=mij+1; + } + else + dr=mij-1; + } + if(rez>0) + cnt=cnt+rez-j; + } + } + out< +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int v[1000]; + int n; + fin >> n; + for (int i = 1; i <= n; i++) + fin >> v[i]; + + sort(v + 1, v + n + 1); + + int cate_triunghiuri = 0; + + for (int i = 1; i <= n; i++) + { + for (int j = i + 1; j<= n; j++) + { + int sum = v[i] + v[j]; + //vrem sa gasim pozitia k a.i. v[k] <= sum si v[k+1] > sum + //stim astfel ca toate elementele de la pozitia j pana la k + // vor fi valide pentru a face un triunghi + int st, dr, mij; + st = j; + dr = n; + while (st <= dr) + { + mij = (st + dr) / 2; + if (v[mij] <= sum) + st = mij + 1; + else + dr = mij - 1; + } + mij = st + dr; + mij /= 2; + if (v[mij] > sum) + mij--; + cate_triunghiuri += mij - j; + + } + } + + fout << cate_triunghiuri; + return 0; +} diff --git a/nrtri/binary_search/2ac1b1e8-409f-11ec-834c-dc7196033e92.cpp b/nrtri/binary_search/2ac1b1e8-409f-11ec-834c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb7144bbac413d05ee72d2e3a36e9d4efc025075 --- /dev/null +++ b/nrtri/binary_search/2ac1b1e8-409f-11ec-834c-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, v[805], i, j, raspuns; + +int cautareBinara(int j) { + int stanga = j + 1, dreapta = n, t = v[i] + v[j]; + while (stanga <= dreapta) { + int mijloc = (stanga + dreapta) / 2; + if (t < v[mijloc]) + dreapta = mijloc - 1; + else + stanga = mijloc + 1; + } + return raspuns + dreapta - j; +} + +int main() { + + f >> n; + for (int i = 1; i <= n; i++) + f >> v[i]; + + sort(v + 1, v + n + 1); + + for (i = 1; i < n; i++) + for (j = i + 1; j <= n; j++) + raspuns = cautareBinara(j); + + g << raspuns << " "; + + return 0; +} diff --git a/nrtri/binary_search/2b733df3-409f-11ec-87b4-dc7196033e92.cpp b/nrtri/binary_search/2b733df3-409f-11ec-87b4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0b74b485215f1ae222f2cb6a6ea1af522742311 --- /dev/null +++ b/nrtri/binary_search/2b733df3-409f-11ec-87b4-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int n,a,b,c,s,poz,mij,i,st,dr,v[801],nr; +int main() +{ + fin>>n; + for (i=1; i<=n; i++) + fin>>v[i]; + sort (v+1,v+n+1); + for (a=1; a<=n; a++) + { + for (b=a+1; b<=n; b++) + { + s=v[a]+v[b]; + st=b+1; + dr=n; + poz=n+1; + while (st<=dr) + { + mij=(st+dr)/2; + if (v[mij]>s) + { + poz=mij; + dr=mij-1; + } + else + st=mij+1; + } + nr+=poz-b-1; + } + } + fout< +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, cnt, lat[801]; + +int main() { + fin >> n; + for(int i = 1; i <= n; i++){ + fin >> lat[i]; + } + fin.close(); + sort(lat + 1, lat + n + 1); + for(int i = 1; i <= n - 2; i++) { + for(int j = i + 1; j <= n - 1; j++) { + int a = lat[i]; + int b = lat[j]; + int st = j + 1, dr = n; + while(st <= dr) { + int mij = (dr + st) / 2; + if (lat[mij] <= a + b) { + st = mij + 1; + } else { + dr = mij - 1; + } + } + cnt += dr - j; + } + } + fout << cnt; + fout.close(); + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/2c08f84a-409f-11ec-b770-dc7196033e92.cpp b/nrtri/binary_search/2c08f84a-409f-11ec-b770-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..203f603690774849f26140e225dc444b147932ff --- /dev/null +++ b/nrtri/binary_search/2c08f84a-409f-11ec-b770-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,v[805],ans; + +int main() +{ + fin>>n; + + for(int i=1;i<=n;i++) + fin>>v[i]; + + sort(v+1,v+n+1); //pointer+intreg e ok, dar intreg+pointer nu e ok O(n log n) sortarea din STL + + + /** + Daca laturile triunghiului sunt v[i]=v[k], i +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int cautbin(int v[],int n,int i,int j) +{ + int st=j+1,dr=n,m,poz=0; + while(st<=dr) + { + m=st+(dr-st)/2; + if(v[m]<=v[i]+v[j]) + { + poz=m; + st=m+1; + } + else + dr=m-1; + } + return poz; +} + +int main() +{ + int n,v[1002],k=0; + fin>>n; + for(int i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + v[0]=0; + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + int poz=cautbin(v,n,i,j); + if(poz) + k+=poz-j; + } + } + fout< +#include + +using namespace std; + +int N; +int A[800]; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + fin >> N; + for (int i = 0; i < N; i++) + fin >> A[i]; + + sort(A, A + N); + + int count = 0; + for (int i = 0; i < N; i++) + for (int j = i + 1; j < N; j++) + { + int lo = j, hi = N - 1; + while (lo < hi) + { + int mid = (lo + hi + 1) / 2; + if (A[mid] <= A[i] + A[j]) + lo = mid; + else + hi = mid - 1; + } + count += lo - j; + } + + fout << count << '\n'; + + fin.close(); + fout.close(); +} \ No newline at end of file diff --git a/nrtri/binary_search/2e392861-409f-11ec-9ebc-dc7196033e92.cpp b/nrtri/binary_search/2e392861-409f-11ec-9ebc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb2a79b4fb9e67c1838f88718cee9c1e62776c51 --- /dev/null +++ b/nrtri/binary_search/2e392861-409f-11ec-9ebc-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; +int v[30005]; +int bs(int st,int dr,int x){ + int last=st-1,mij; + while(st<=dr){ + mij=(st+dr)/2; + if(v[mij]<=x){ + st=mij+1; + last=mij; + } else { + dr=mij-1; + } + } + return last; +} +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int i,s=0,j,n; + fin>>n; + for(i=1;i<=n;++i){ + fin>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-1;++i){ + for(j=i+1;j<=n-1;++j){ + s+=bs(j+1,n,v[i]+v[j])-j; + } + } + fout< +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, v[803]; + +int main() +{ + fin >> n; + for (int i = 0, x; i < n; i++) + fin >> v[i]; + sort(v, v+n); + + + int res = 0; + for (int i = 0; i < n - 2; i++) + { + for (int j = i + 1; j < n - 1; j++) + { + int val = v[i] + v[j]; + auto it = upper_bound(v+j-1, v+n, val); + it--; + int p = it - v; + + res += (p - j); + } + } + fout << res; + + + return 0; +} diff --git a/nrtri/binary_search/2e9ad19e-409f-11ec-b645-dc7196033e92.cpp b/nrtri/binary_search/2e9ad19e-409f-11ec-b645-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09cdfc260318cc88dbf7a718dd1aa352561a5c36 --- /dev/null +++ b/nrtri/binary_search/2e9ad19e-409f-11ec-b645-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main() +{ + int n,v[801],nr=0,p; + in>>n; + for(int i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for(int i=1; i<=n-2; i++) + { + for(int j=i+1; j<=n-1; j++) + { + int st=j+1; + int dr=n; + int s=v[i]+v[j]; + p=0; + while(st<=dr) + { + int mij=(st+dr)/2; + if(v[mij]==s) + { + p=mij; + st++; + } + else if(v[mij]>s) + dr=mij-1; + else + st=mij+1; + } + if(p==0) + p=dr; + nr=nr+p-j; + } + } + /*for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + for(int k=j+1;k<=n;k++) + { + if(v[i]+v[j]>=v[k] && v[i]+v[k]>=v[j] && v[j]+v[k]>=v[i]) + nr++; + } + } + }*/ + out< +#include + +using namespace std; + +ifstream cin ("nrtri.in") ; +ofstream cout ("nrtri.out") ; + +int n, S, p ; +int v[805] ; + +int main() +{ + + int x, st, dr, mij, triunghiuri = 0 ; + + cin >> n ; + + for (int i = 1 ; i <= n ; i ++) + cin >> v[i] ; + + sort (v + 1, v + n + 1) ; + + for (int i = 1 ; i <= n - 2 ; i ++) + { + + for (int j = i + 1 ; j < n ; j ++) + { + + S = v [i] + v [j] ; + p = -1 ; + st = j + 1 ; + dr = n ; + mij = ( st + dr ) / 2 ; + + while ( st <= dr ) + { + if ( v [ mij ] == S ) + { + p = mij ; + st ++ ; + } + else if ( v [ mij ] < S ) + { + st = mij + 1 ; + } + else + { + dr = mij - 1 ; + } + mij = ( st + dr ) / 2 ; + } + + if ( p == -1 ) + p = dr ; + + triunghiuri = triunghiuri + p - j ; + + } + } + + cout << triunghiuri ; + + return 0 ; + +} diff --git a/nrtri/binary_search/2f372845-409f-11ec-834f-dc7196033e92.cpp b/nrtri/binary_search/2f372845-409f-11ec-834f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..630d3b3468e0ebd5bdb78e82f467d8d93c0e7aeb --- /dev/null +++ b/nrtri/binary_search/2f372845-409f-11ec-834f-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n, i, j, dr, st, mid, sol, v[1000]; + +int main(){ + fin >> n; + for ( i = 1; i <= n; i++ ) + fin >> v[i]; + sort( v + 1, v + n + 1 ); + for ( i = 1; i <= n; i++ ){ + for ( j = i + 1; j <= n; j++ ){ + st = j + 1; + dr = n; + while ( st <= dr ){ + mid = ( st + dr ) / 2; + if ( v[mid] <= v[i] + v[j] ) + st = mid + 1; + else + dr = mid - 1; + } + sol += dr - j; + } + } + fout << sol; + return 0; +} diff --git a/nrtri/binary_search/2fa6b678-409f-11ec-b6b0-dc7196033e92.cpp b/nrtri/binary_search/2fa6b678-409f-11ec-b6b0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c8353ea1b0414df165add29b0e70fd3514956ed --- /dev/null +++ b/nrtri/binary_search/2fa6b678-409f-11ec-b6b0-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int a[805],n; + +int cauta(int s,int p) +{ + int st = p + 1,dr = n,mij; + while (st <= dr) + { + mij = (st + dr) / 2; + if (a[mij] > s) + dr = mij - 1; + else if (a[mij] <= s) + st = mij + 1; + } + //cout << st << " " << dr << '\n'; + return dr - p; + +} + +int main() +{ + int i,j,sol = 0; + in >> n; + for (i = 1; i <= n; i++) + in >> a[i]; + sort(a + 1,a + n + 1); + for (i = 1; i < n - 1; i++) + for (j = i + 1; j < n; j++) + { + sol += cauta(a[i] + a[j],j); + //cout << cauta(a[i] + a[j],j) << " " << i << " " << j << '\n'; + } + out << sol; + return 0; +} diff --git a/nrtri/binary_search/3012772c-409f-11ec-8dee-dc7196033e92.cpp b/nrtri/binary_search/3012772c-409f-11ec-8dee-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8558c20f266382b50a8be4126fea2bf1f81f31c8 --- /dev/null +++ b/nrtri/binary_search/3012772c-409f-11ec-8dee-dc7196033e92.cpp @@ -0,0 +1,37 @@ +//#include +#include +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int n,rasp,v[805]; +int cautbin(int val){ + int st=1,dr=n,sol=0; + while(st<=dr){ + int mid=(st+dr)/2; + if(v[mid]<=val){ + sol=mid; + st=mid+1; + } + else + dr=mid-1; + } + return sol; +} +int main() +{ + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i]; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++){ + for(int j=i+1;j<=n;j++){ + int a=v[i]+v[j]; + rasp+=max(0,cautbin(a)-j); + //cout< +#include +#include +using namespace std; +ifstream in ("nrtri.in"); +ofstream out("nrtri.out"); +int main() +{ + int n,v[801],nr=0,s=0,dr,st,p,mij; + in>>n; + for(int i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + /*for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + for(int k=j+1;k<=n;k++) + { + if(v[i]+v[j]>=v[k] && v[i]+v[k]>=v[j] && v[j]+v[k]>=v[i]) + nr++; + } + } + }*/ + for(int i=1; i<=n-2; i++) + { + for(int j=i+1; j<=n-1; j++) + { + s=v[i]+v[j]; + st=j+1; + dr=n; + p=0; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]==s) + { + p=mij; + st++; + } + else if(v[mij]>s) + dr=mij-1; + else st=mij+1; + } + if(p==0) + p=dr; + nr=nr+p-j; + } + } + out << nr << endl; + return 0; +} diff --git a/nrtri/binary_search/307587e7-409f-11ec-916f-dc7196033e92.cpp b/nrtri/binary_search/307587e7-409f-11ec-916f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53067ac0d1990eedaf509c2b9be15afac9b4458d --- /dev/null +++ b/nrtri/binary_search/307587e7-409f-11ec-916f-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +int a[801], n, nr; + +void read() { + int i; + ifstream f("nrtri.in"); + f >> n; + for (i = 1; i <= n; i++) + f >> a[i]; + f.close(); +} + +void solve() { + int i, j, c, st, dr, mij, k; + sort(a + 1, a + n + 1); + for (i = 1; i <= n - 2; i++) + for (j = i + 1; j <= n - 1; j++) { + c = a[i] + a[j]; + st = j + 1, dr = n, k = -1; + while (st <= dr) { + mij = (st + dr) / 2; + if (a[mij] <= c) { + st = mij + 1; + k = mij; + } + else dr = mij - 1; + } + if (k != -1) + nr += k - j; + } +} + +void output() { + ofstream g("nrtri.out"); + g << nr; + g.close(); +} + +int main() { + read(); + solve(); + output(); + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/30ae31d8-409f-11ec-bed7-dc7196033e92.cpp b/nrtri/binary_search/30ae31d8-409f-11ec-bed7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a090884b0c0bd1e8b380537aa090d6fbc88df25 --- /dev/null +++ b/nrtri/binary_search/30ae31d8-409f-11ec-bed7-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[805]; +int n, C, x; +int CB(int x) +{ + int st=1, dr=n, mij, sol=-1; + while(st<=dr) + { + mij=(st+dr)/2; + if(x> n; + for(int i=1; i<=n; i++) + { + fin >> v[i]; + } + sort(v+1, v+n+1); + for(int i=1; i<=n-2; i++) + { + for(int j=i+1; j<=n-1; j++) + { + x=CB(v[i]+v[j]); + if(x!=-1) + { + C+=x-j; + } + } + } + fout << C; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/3153f429-409f-11ec-a498-dc7196033e92.cpp b/nrtri/binary_search/3153f429-409f-11ec-a498-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b188cfda4070285254887617e467f37428ea4f03 --- /dev/null +++ b/nrtri/binary_search/3153f429-409f-11ec-a498-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fou("nrtri.out"); + +const int MAX = 800; +int N, i, v[1 + MAX], nr; +int main() +{ + fin >> N; + for(int i = 1; i <= N; i++) + fin >> v[i]; + sort(v + 1, v + N + 1); + for(int i = 1; i < N; i++) + { + for(int j = i + 1; j < N; j++) + { + int st = j + 1, dr = N, s = v[i] + v[j], mid; + while(st <= dr) + { + mid = (st + dr)/2; + if(s >= v[mid]) + st = mid + 1; + else + dr = mid - 1; + } + nr += dr - j; + } + } + + fou << nr; +} diff --git a/nrtri/binary_search/318e0a2c-409f-11ec-8b42-dc7196033e92.cpp b/nrtri/binary_search/318e0a2c-409f-11ec-8b42-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2432c5918a46de59ac5a03bf176f8571a73166f5 --- /dev/null +++ b/nrtri/binary_search/318e0a2c-409f-11ec-8b42-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,x,st,dr,mid,cnt,v[801]; +int main() { + fin>>n; + for (i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for (i=1;i=v[mid]) + st=mid+1; + else + dr=mid-1; + } + cnt+=dr-j; + } + fout< +#define N 808 + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n; +int v[N]; + +void Citire() +{ + int i; + fin >> n; + for( i=1; i<=n; i++ ) + fin >> v[i]; +} + +int CautareBinara( int i, int j ) +{ + int st, dr; + int mij; + int poz = -1; + st = 1; dr = n; + while( st <= dr ) + { + mij = ( st + dr ) / 2; + if( v[mij] <= v[i] + v[j] ) + { + st = mij + 1; + poz = mij; + } + else if( v[mij] > v[i] + v[j] ) + { + dr = mij - 1; + } + } + return poz; +} + +inline bool triunghi( int a, int b, int c ) +{ + if( a + b < c ) return 0; + if( a + c < b ) return 0; + if( c + b < a ) return 0; + return 1; +} + +void Rezolvare() +{ + int i, j; + int x; + int cb; + int ct = 0; + sort( v+1, v+n+1 ); + for( i=1; i +#include +#include +using namespace std; + + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int lungimi[805]; + + + +int binarySearch(int left, int right, int val) +{ + int bestVal = -1; + + while(left <= right) + { + int mid = (left + right) / 2; + //cout << mid << ' '; + if(lungimi[mid] <= val) + { + bestVal = mid; + left = mid + 1; + } + else + { + right = mid - 1; + } + } + //cout << '\n'; + return bestVal; + +} + + + +int main() +{ + int n; + fin >> n; + + for(int i = 0; i < n; i++) + { + fin >> lungimi[i]; + } + + sort(lungimi + 0, lungimi + n); + + int cazuri = 0; + + for(int i = 0; i < n; i++) + { + for(int j = i + 1; j < n; j++) + { + int a = lungimi[i]; + int b = lungimi[j]; + + int searchVal = a + b; + //cout << binarySearch(j + 1, n - 1, searchVal)<< ' '; + + int valBin = binarySearch(j + 1, n - 1, searchVal); + if (valBin > -1) + { + cazuri += valBin - j; + } + + + } + } + + fout << cazuri; + +} \ No newline at end of file diff --git a/nrtri/binary_search/328319c6-409f-11ec-931e-dc7196033e92.cpp b/nrtri/binary_search/328319c6-409f-11ec-931e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2c127e1ad539cd914ac17276fd5e6603be326b0 --- /dev/null +++ b/nrtri/binary_search/328319c6-409f-11ec-931e-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include +#include + +using namespace std; + +int v[805]; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n; + fin>>n; + for (int i = 1; i <= n; i++) + { + fin >> v[i]; + } + sort(v + 1,v + n + 1); + int st, dr, mij, val, cnt = 0; + for (int i = 1; i <= n - 2; i++) + { + for (int j = i + 1; j <= n - 1; j++) + { + int sum = v[i] + v[j]; + st = j + 1; + dr = n; + val = -1; + while (dr >= st) + { + mij=(dr + st) / 2; + if (v[mij] <= sum) + { + val = mij; + st = mij + 1; + } + else + { + dr = mij - 1; + } + } + if (val != -1) + { + cnt += val - j; + } + } + } + fout << cnt << '\n'; + + return 0; +} diff --git a/nrtri/binary_search/32b231f6-409f-11ec-97af-dc7196033e92.cpp b/nrtri/binary_search/32b231f6-409f-11ec-97af-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abd21f3fc6f520de18644bcd444e2c50aff428c0 --- /dev/null +++ b/nrtri/binary_search/32b231f6-409f-11ec-97af-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include + +std::ifstream fin("nrtri.in"); +std::ofstream fout("nrtri.out"); + +const int mxn = 8e2 + 2; + +int n, v[mxn]; + +int main() { + fin >> n; + for (int i = 1; i <= n; ++i) fin >> v[i]; + + std::sort(v + 1, v + n + 1); + + int ans = 0; + for (int i = 1; i <= n - 2; ++i) { + for (int j = i + 1; j <= n - 1; ++j) { + int lb = j + 1, rb = n; + + while (lb <= rb) { + int mb = lb + (rb - lb) / 2; + + if (v[i] + v[j] >= v[mb]) + lb = mb + 1; + else + rb = mb - 1; + } + ans += rb - j; + } + } + fout << ans; + return 0; +} diff --git a/nrtri/binary_search/32e060df-409f-11ec-b91d-dc7196033e92.cpp b/nrtri/binary_search/32e060df-409f-11ec-b91d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..973fbc1f42f79709abf1b8dbe6405a0c090ef2cd --- /dev/null +++ b/nrtri/binary_search/32e060df-409f-11ec-b91d-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +using namespace std; + +int n; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + vector a; + f >> n; + for (int i = 1; i <= n; i++) + { + int x; + f>> x; + a.push_back(x); + } + sort(begin(a), end(a)); + int total = 0; + + for (vector::iterator i = begin(a); i != end(a) - 2; i++) + for (vector::iterator j = i + 1; j != end(a) - 1; j++) + { + vector::iterator it = upper_bound(j + 1, end(a), *i + *j); + + if ((*(it - 1) <= *i + *j) && j != it - 1) + { + total += it - j - 1; + // << *i << " " << *j << " " << *(it - 1) << " ---- " << total << endl; + } + + } + g << total; + return 0; + +} \ No newline at end of file diff --git a/nrtri/binary_search/331428d9-409f-11ec-88f7-dc7196033e92.cpp b/nrtri/binary_search/331428d9-409f-11ec-88f7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6ba8cc00163ff5d4b9ef4c1d048ba9680afc2e4 --- /dev/null +++ b/nrtri/binary_search/331428d9-409f-11ec-88f7-dc7196033e92.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int laturi[805]; + +int nrtri,i,x; +int n,ok,maximlat,j,st,dr,mij,pozitie; + +int main() +{ + f>>n; + + for(i=1;i<=n;i++) + { + f>>laturi[i]; + } + + while(ok==0) + { + ok=1; + + for(i=2;i<=n;i++) + { + if(laturi[i]j) + nrtri+=pozitie-j; + + pozitie=0; + + + } + } + g< +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int stocare[805], n, sum, nrtri, mid, ans, st, dr; + +int main() +{ + fin >> n; + for (int i = 0; i < n; i++) + { + fin >> stocare[i]; + } + sort(stocare, stocare + n); + for (int i = 0; i < n - 2; i++) + { + for (int j = i + 1; j < n - 1; j++) + { + st = j + 1; + dr = n - 1; + sum = stocare[i] + stocare[j]; + ans = j; + while (st <= dr) + { + mid = (st + dr) / 2; + if (stocare[mid] <= sum) + { + st = mid + 1; + ans = mid; + } + else + { + dr = mid - 1; + } + } + nrtri += ans - j; + } + } + fout << nrtri; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/34105226-409f-11ec-be95-dc7196033e92.cpp b/nrtri/binary_search/34105226-409f-11ec-be95-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f70a5bb9bee9702bfa4f834975b07352c09095f9 --- /dev/null +++ b/nrtri/binary_search/34105226-409f-11ec-be95-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int l[805] , n; +int upperBound(int target) +{ + int s = 0,h = n ; + while(s < h) + { + int mid = (s + h) / 2; + if(l[mid] > target) + h = mid; + else s = mid + 1; + } + return h; +} + + + +int main() +{ + int nr = 0; + in >> n; + for(int i = 0;i < n;i ++) + in >>l[i]; + sort(l,l + n); + for(int i = 0;i +#include +#include + +using namespace std; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,v[801],i,j,s,d,r=0,mij,rez; + cin>>n; + for(i=1; i<=n; i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + { + s=j+1; + d=n; + rez=-1; + while(d>=s) + { + mij=(d+s)/2; + if(v[i]+v[j]>=v[mij]) + s=mij+1; + else + { + rez=mij; + d=mij-1; + } + } + if(rez!=-1) + r=r+rez-j-1; + else + r=r+n-j; + } + cout< +#include +#define nmax 801 +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int main(){ + int n, v[nmax], cnt = 0; + cin >> n; + for(int i = 1; i <= n; ++i){ + cin >> v[i]; + } + sort(v + 1, v + n + 1); + for(int i = 1; i <= n - 2; ++i){ + for(int j = i + 1; j <= n - 1; ++j){ + int st = j + 1, dr = n, mij, gasit = 0; + while(st <= dr && gasit == 0){ + mij = (st + dr) / 2; + if(v[mij] <= v[i] + v[j]){ + st = mij + 1; + } + else{ + dr = mij - 1; + } + } + cnt += dr - j; + } + } + cout << cnt << " "; + return 0; +} + diff --git a/nrtri/binary_search/34cbc4e5-409f-11ec-a96b-dc7196033e92.cpp b/nrtri/binary_search/34cbc4e5-409f-11ec-a96b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8312d5c5c8662ce92a94edee58b75e440453aae5 --- /dev/null +++ b/nrtri/binary_search/34cbc4e5-409f-11ec-a96b-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +int v[805]; +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n; + fin>>n; + for(int i=1;i<=n;i++) + { + fin>>v[i]; + } + sort(v+1,v+n+1); + int st,dr,mij,val,cnt=0; + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + int sum=v[i]+v[j]; + st=j+1;dr=n;val=-1; + while(dr>=st) + { + mij=(dr+st)/2; + if(v[mij]<=sum) + { + val=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + if(val!=-1) + { + cnt+=val-j; + } + } + } + fout< + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[801]; + +int main() +{ + int n,i,j,k; + int c=0; + fin>>n; + for(i=0; i>v[i]; + for(i=0; iv[j]) + { + int aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } + for(i=0; i=v[k]) + c++; + if(s +#include +#include + +using namespace std; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,v[801],i,j,s,d,r=0,mij,rez; + cin>>n; + for(i=1; i<=n; i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + { + s=j+1; + d=n; + rez=-1; + while(d>=s) + { + mij=(d+s)/2; + if(v[i]+v[j]>=v[mij]) + s=mij+1; + else + { + rez=mij; + d=mij-1; + } + } + if(rez!=-1) + r=r+rez-j-1; + else + r=r+n-j; + } + cout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[801],n,S; +int main() +{ + fin>>n; + for (int i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for (int i=1;i<=n-2;i++) + for (int j=i+1;j<=n-1;j++){ + int s=v[i]+v[j]; + int st=j+1,dr=n; + while (st<=dr){ + int mid=(st+dr)/2; + if (v[mid]>s) + dr=mid-1; + else + st=mid+1; + } + S=S+dr-j; + } + fout< +#include + +using namespace std; + +const int NMAX = 800; +int v[1 + NMAX]; + +int bin(int st, int dr, int val) +{ + int last = -1; + + while (st <= dr) + { + int mij = (st + dr) / 2; + + if (v[mij] <= val) + { + last = mij; + st = mij + 1; + } + else + { + dr = mij - 1; + } + } + + return last; +} + +int main() +{ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int n; + int sol = 0; + + in >> n; + for (int i = 1; i <= n; i++) + { + in >> v[i]; + } + + sort(v + 1, v + 1 + n); + + for (int i = 1; i < n - 1; i++) + { + for (int j = i + 1; j < n; j++) + { + int poz = bin(j + 1, n, v[i] + v[j]); + if (poz != -1) + { + sol += (poz - j); + } + } + } + + out << sol << '\n'; + + return 0; +} diff --git a/nrtri/binary_search/376c8b3a-409f-11ec-9d7a-dc7196033e92.cpp b/nrtri/binary_search/376c8b3a-409f-11ec-9d7a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..721ecaf0420ba80f9c720b58af781d183416c184 --- /dev/null +++ b/nrtri/binary_search/376c8b3a-409f-11ec-9d7a-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int n,v[801]; +int cautbin(int x){ + int high=n+1,mid,low=0; + while (high - low > 1) { + mid = (low + high) / 2; + if (v[mid] <= x) { + low = mid; + } else { // v[mid] >= x + high = mid; + } + } + return low; +} +int main(){ + +cin>>n; +for(int i=1;i<=n;i++){ + cin>>v[i]; +} +sort(v+1,v+n+1); +int sol=0; +for(int i=1;i +#include +#define NMAX 1001 + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int N, sol, V[NMAX]; + +int search(int x) { + int low = 0, high = N + 1, mid; + + while (high - low > 1) { + mid = (low + high) / 2; + if (V[mid] <= x) { + low = mid; + } else { + high = mid; + } + } + + return low; +} + +int main() { + fin >> N; + for (int i = 1; i <= N; ++i) { + fin >> V[i]; + } + + sort(V + 1, V + N + 1); + + for (int i = 1; i <= N; ++i) { + for(int j = i + 1; j <= N; ++j) { + sol += search(V[i] + V[j]) - j; + } + } + + fout << sol << '\n'; +} \ No newline at end of file diff --git a/nrtri/binary_search/37cd2147-409f-11ec-9684-dc7196033e92.cpp b/nrtri/binary_search/37cd2147-409f-11ec-9684-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8af6db32ce5aacea9f7603dd63d4da8891bf54e1 --- /dev/null +++ b/nrtri/binary_search/37cd2147-409f-11ec-9684-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; +int v[805]; +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n; + f>>n; + for(int i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + int cnt=0; + for(int i=1;i>1; + if(v[i]+v[j]>=v[mi]) + poz=mi,st=mi+1; + else dr=mi-1; + } + cnt+=poz-j; + } + g< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[1003],n,s,nr,i,j; +int st,dr,mid; +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++){ + s=v[i]+v[j]; + st=j+1; + dr=n; + while(st<=dr){ + mid=(st+dr)/2; + if(v[mid]>s) + dr=mid-1; + else + st=mid+1; + } + nr=nr+dr-j; + } + fout< +#include +using namespace std; +int v[801]; +int main() +{ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + long long int n,l=0,t=1; + in>>n; + while(t*2<=n){ + t*=2; + } + for(long long int i=1;i<=n;i++){ + in>>v[i]; + } + sort(v+1,v+n+1); + for(long long int i=1;i<=n-2;i++){ + for(long long int j=i+1;j<=n-1;j++){ + long long int a=v[i]+v[j]; + if(a0;k/=2){ + if(v[r+k]<=a && r+k<=n){ + r+=k; + } + } + l+=(r-j); + }else{ + l+=(n-j); + } + } + } + out< +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, v[805], sol; + +void citire(int &n, int v[]) +{ + f >> n; + + for(int i = 1; i <= n; ++ i) + f >> v[i]; +} + +int main() +{ + citire(n, v); + + sort(v + 1, v + n + 1); + + for(int i = 1; i <= n - 2; ++ i) + for(int j = i + 1; j <= n - 1; ++ j) + { + long s = v[i] + v[j]; + + int st = j + 1, dr = n; + while (st <= dr) + { + int mid = (st + dr) / 2; + + if (v[mid] > s) + dr = mid - 1; + else + st = mid + 1; + } + sol += dr - j; + } + + g << sol; + + f.close(); + g.close(); + return 0; +} diff --git a/nrtri/binary_search/38919182-409f-11ec-a5a8-dc7196033e92.cpp b/nrtri/binary_search/38919182-409f-11ec-a5a8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c3ded7729d22473dd8cc75998bc41567fe9a071 --- /dev/null +++ b/nrtri/binary_search/38919182-409f-11ec-a5a8-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +const int nmax = 800; +int n, i, j, c, s, v[nmax]; +int cb(int st, int dr, int x) { + int m, p; + while(st <= dr) { + m = (st+dr)/2; + if(x >= v[m]) + st = m+1, p = m; + else + dr = m-1; + } + return p; +} +int main() +{ + fin >> n; + for(i = 0; i < n; i++) + fin >> v[i]; + sort(v, v+n); + for(i = 0; i < n-2; i++) + for(j = i+1; j < n-1; j++) { + s = v[i]+v[j]; + if(s >= v[j+1]) + c += cb(j+1, n-1, s)-j; + } + fout << c; + fin.close(); + fout.close(); + return 0; +} diff --git a/nrtri/binary_search/38fa0880-409f-11ec-818b-dc7196033e92.cpp b/nrtri/binary_search/38fa0880-409f-11ec-818b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1eced3fe403d51b6a14afa415316838b867dfc78 --- /dev/null +++ b/nrtri/binary_search/38fa0880-409f-11ec-818b-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include +#define NMAX 1002 + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int N, sol, V[NMAX]; + +int search(int x) { + return int(upper_bound(V + 1, V + N + 1, x) - V) - 1; +} + +int main() { + fin >> N; + for (int i = 1; i <= N; ++i) { + fin >> V[i]; + } + + sort(V + 1, V + N + 1); + + for (int i = 1; i <= N; ++i) { + for(int j = i + 1; j <= N; ++j) { + sol += search(V[i] + V[j]) - j; + } + } + + fout << sol << '\n'; +} \ No newline at end of file diff --git a/nrtri/binary_search/396cf10f-409f-11ec-a294-dc7196033e92.cpp b/nrtri/binary_search/396cf10f-409f-11ec-a294-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b75e6b1acbae32abadfef093376d6dd6e339346 --- /dev/null +++ b/nrtri/binary_search/396cf10f-409f-11ec-a294-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; +int v[1000]; +int bs_right(int st, int dr, int s){ + int med,last=-1; + while(st<=dr){ + med=(st+dr)/2; + if(s>=v[med]){ + last=med; + st=med+1; + }else dr=med-1; + } + return last; +} +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int cnt=0,n,i,j,s,poz; + scanf("%d",&n); + for(i=1;i<=n;++i){ + scanf("%d",&v[i]); + } + sort(v+1,v+n+1); + for(i=1;i +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +const int NMAX=800; +int v[NMAX+5]; +int main() +{ + int n,dr,st,med,i,j,s,last,rasp=0,k; + fin>>n; + for(i=1;i<=n;i++) + { + fin>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i=v[med]) + { + last=med; + st=med+1; + } + else + { + dr=med-1; + } + } + if(last!=-1) + rasp+=last-j; + } + } + fout< +#include +using namespace std; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n, i , j, v[805], cnt = 0, st , dr , mij; + scanf("%d",&n); + for(i=1;i<=n;i++) + { + scanf("%d",&v[i]); + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + mij=(st+dr)/2; + while(st<=dr) + { + if(v[i]+v[j]>=v[mij]) + st = mij+1; + else + dr = mij-1; + mij=(st+dr)/2; + } + cnt += dr-j; + } + } + printf("%d",cnt); + return 0; +} diff --git a/nrtri/binary_search/3a0e599f-409f-11ec-a365-dc7196033e92.cpp b/nrtri/binary_search/3a0e599f-409f-11ec-a365-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f61bd7cd46707935d044fe42f0d6dae81a663c9 --- /dev/null +++ b/nrtri/binary_search/3a0e599f-409f-11ec-a365-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,v[805]; + +int BS(int st,int nr) +{ + int left=st,right=n,poz=0; + while(left<=right) + { + int mijl=(left+right)/2; + if(v[mijl]<=nr) + { + left=mijl+1; + poz=mijl; + } + else + right=mijl-1; + } + return poz; +} + +int main() +{ + int i,j; + long long ans=0; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[801]; + +int cb( int n, int ls, int x) +{ + int ld = n - 1, mij; + while (ls <= ld) + { + mij = (ls + ld) / 2; + if (v[mij] == x) return mij; + if (v[mij] < x) ls = mij + 1; + else ld = mij - 1; + } + return ld; +} + +int main(void) +{ + int n, ct = 0; + fin >> n; + for (int i = 0; i < n; i++) + fin >> v[i]; + sort(v, v + n); + for (int i = 0; i < n - 1; i++) + { + for (int j = i + 1; j < n; j++) + { + int s = v[i] + v[j]; + int poz = cb( n, j + 1, s); + while (v[poz] == v[poz + 1] && poz + 1 < n) + poz++; + ct += poz - j; + } + } + fout << ct; + return 0; +} diff --git a/nrtri/binary_search/3a857ac4-409f-11ec-9082-dc7196033e92.cpp b/nrtri/binary_search/3a857ac4-409f-11ec-9082-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82cfc1ceab6ac332d82519bac81c958cde402c96 --- /dev/null +++ b/nrtri/binary_search/3a857ac4-409f-11ec-9082-dc7196033e92.cpp @@ -0,0 +1,55 @@ +#pragma GCC optimize("Ofast") +#pragma GCC target("avx,avx2,fma") +#pragma GCC optimization ("unroll-loops") + +#include + +using namespace std; + +void __print(int x) {cerr << x;} +void __print(long x) {cerr << x;} +void __print(long long x) {cerr << x;} +void __print(unsigned x) {cerr << x;} +void __print(unsigned long x) {cerr << x;} +void __print(unsigned long long x) {cerr << x;} +void __print(float x) {cerr << x;} +void __print(double x) {cerr << x;} +void __print(long double x) {cerr << x;} +void __print(char x) {cerr << '\'' << x << '\'';} +void __print(const char *x) {cerr << '\"' << x << '\"';} +void __print(const string &x) {cerr << '\"' << x << '\"';} +void __print(bool x) {cerr << (x ? "true" : "false");} + +template +void __print(const pair &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} +template +void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} +void _print() {cerr << "]\n";} +template +void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} +#ifndef ONLINE_JUDGE +#define debug(x...) cerr << "[" << #x << "] = ["; _print(x) +#else +#define debug(x...) +#endif +#define ll long long int + +int main(){ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int n; + in>>n; + vector a(n); + for(int i=0;i>a[i]; + sort(a.begin(),a.end()); + int ans=0; + for(int i=0;i +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int n,i,j,mij,t=0,s,d; + + fin>>n; + + int v[n+1]; + + for(i=1; i<=n; i++) + { + fin>>v[i]; + } + + sort(v+1,v+n+1); + + for ( i=1; i<=n; i++ ) + for ( j=i+1; j= v[mij] ) + s = mij + 1; + else + d = mij - 1; + } + t = t + d - j; + } + + fout< +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int v[1005]; +int bs_right (int st,int dr,int x) +{ + int med,last=-1; + med=(st+dr)/2; + while (st<=dr) + { + med=(st+dr)/2; + if (v[med] <= x){ + st=med+1;last=med; + } + else + dr=med-1; + } + return last; +} +int main() +{ + int i,j,n,s,poz,cnt=0; + cin>>n; + for(i=1;i<=n;i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +const int NMAX=1005; +int v[NMAX]; +int bs_right(int st,int dr,int x) +{ + int med,last=-1; + while(st<=dr) + { + med=(st+dr)/2; + if(x>=v[med]) + { + last=med; + st=med+1; + } + else + dr=med-1; + } + return last; +} +int main() +{ + int n,i,j,s,poz,cnt=0; + cin>>n; + for(i=1;i<=n;i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + s=v[i]+v[j]; + poz=bs_right(j+1,n,s); + if(poz!=-1) + cnt=cnt+poz-(j+1)+1; + } + } + cout< + +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int v[1005]; + +int bs(int st, int dr, int x) +{ + int med, last = -1; + while (st <= dr) + { + med = (dr + st) / 2; + if (x >= v[med]) + { + last = med; + st = med + 1; + } + else + dr = med - 1; + } + return last; +} + +int main() +{ + int n, i, c = 0, j, s, poz; + in >> n; + for (i = 1; i <= n; i++) + in >> v[i]; + sort(v, v + n + 1); + for (i = 1; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { + s = v[i] + v[j]; + poz = bs(j + 1, n, s); + if (poz != -1) + c += poz - j; + } + } + out << c; + return 0; +} diff --git a/nrtri/binary_search/3b90daaf-409f-11ec-911e-dc7196033e92.cpp b/nrtri/binary_search/3b90daaf-409f-11ec-911e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c089501da5114c5a165b914e2360dc2bf18efe11 --- /dev/null +++ b/nrtri/binary_search/3b90daaf-409f-11ec-911e-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; +int v[1005]; +int bs_right(int st, int dr, int x) { + int med, last = -1; + while(st <= dr) { + med = (st + dr) >> 1; + if(x >= v[med]) { + last = med; + st = med + 1; + } else dr = med - 1; + } + return last; +} + +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n, s = 0, bsr; + scanf("%d", &n); + for(int i = 1; i <= n; i++) scanf("%d", &v[i]); + sort(v + 1, v + n + 1); + for(int i = 1; i < n - 1; i++) { + for(int j = i + 1; j < n; j++) { + bsr = bs_right(j + 1, n, v[i] + v[j]); + if(bsr != -1) + s += bsr - j; + } + } + printf("%d", s); + return 0; +} diff --git a/nrtri/binary_search/3c0b8f71-409f-11ec-a725-dc7196033e92.cpp b/nrtri/binary_search/3c0b8f71-409f-11ec-a725-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..484c78ee6d86d380732c9c6e003dc8470b0e27f2 --- /dev/null +++ b/nrtri/binary_search/3c0b8f71-409f-11ec-a725-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +using namespace std; +typedef long long ll; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[805], n; +int CautBn(int s, int val) +{ + int d = n, m; + while (s <= d) + { + m = (s + d) / 2; + if (v[m] > val) + d = m - 1; + else s = m + 1; + } + return d; +} +int main() +{ + ios::sync_with_stdio(false); + fin.tie(0); + + fin >> n; + for (int i = 1; i <= n; ++i) + fin >> v[i]; + sort(v + 1, v + 1 + n); + int cnt = 0; + for (int i = 1; i < n - 1; ++i) + for (int j = i + 1; j < n; ++j) + cnt += CautBn(j + 1, v[i] + v[j]) - j; + fout << cnt; + + return 0; +} diff --git a/nrtri/binary_search/3c47819b-409f-11ec-8fcf-dc7196033e92.cpp b/nrtri/binary_search/3c47819b-409f-11ec-8fcf-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a57ad5308b049499b1983a65623064670195ea6 --- /dev/null +++ b/nrtri/binary_search/3c47819b-409f-11ec-8fcf-dc7196033e92.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int a[801], b[801]; +int main() +{ + int n, i, j, st, dr, ct = 0, mij, s = 0; + fin >> n; + for(i = 1; i <= n; i++) + fin >> a[i]; + sort(a + 1, a + n + 1); + for(i = 1; i <= n; i++) + for(j = i + 1; j < n; j++) + { + st = j + 1; + dr = n; + while(st <= dr) + { + mij = (st + dr) / 2; + if(a[i] + a[j] >= a[mij]) + st = mij + 1; + else + dr = mij - 1; + } + ct = ct + dr - j; + } + fout << ct; + return 0; +} + diff --git a/nrtri/binary_search/3cbada4b-409f-11ec-be27-dc7196033e92.cpp b/nrtri/binary_search/3cbada4b-409f-11ec-be27-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d47044f95c9faef3746686b29c76144ba504fad3 --- /dev/null +++ b/nrtri/binary_search/3cbada4b-409f-11ec-be27-dc7196033e92.cpp @@ -0,0 +1,64 @@ +#include +#include +using namespace std; + +#define NMAX 805 + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[NMAX]; +int n; + +int valid(int a,int b,int c) +{ + return (a+b >= c && a+c >= b && b+c >= a ); +} + +int cauta(int a,int b,int st,int dr) +{ + int sol = -1; + while(st<=dr) + { + int m = (st+dr)/2; + if(valid(v[a],v[b],v[m])) + { + sol = m; + st = m+1; + } + else + { + dr=m-1; + } + } + + return sol; + +} + +int main() +{ + fin>>n; + for(int i = 0;i>v[i]; + + sort(v,v+n); + int sum = 0; + for(int i = 0;i +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, i, j, v[1001], nr, m, dr, st; +int main() +{ f >> n; + + for ( i=1; i<=n; i++ ) + f >> v[i]; + + sort ( v+1, v+n+1 ); + + for ( i=1; i= v[m] ) + st = m + 1; + else dr = m - 1; + } + nr = nr + dr - j; + } + + g << nr; + return 0; +} diff --git a/nrtri/binary_search/3da19841-409f-11ec-baf3-dc7196033e92.cpp b/nrtri/binary_search/3da19841-409f-11ec-baf3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..009a4519d6feac35e7936afd4b7c951fad4eb06b --- /dev/null +++ b/nrtri/binary_search/3da19841-409f-11ec-baf3-dc7196033e92.cpp @@ -0,0 +1,60 @@ +#pragma GCC optimize("Ofast") +#include + +typedef long long ll; + +const ll mod=1e9+7; + +const int dx[] = {0, 1, 0, -1}; +const int dy[] = {1, 0, -1, 0}; +const int nmax=805; +#define all(x) x.begin(),x.end() +#define allr(x) x.rbegin(),x.rend() +#define rc(x) return cout<> n; + for (int i=1; i<=n; i++) { + cin >> a[i]; + } + sort(a+1,a+n+1); + for (int i=1; ij) { + nr+=u-j; + } + } + } + cout << nr << '\n'; +} + diff --git a/nrtri/binary_search/3e8c8af4-409f-11ec-83c9-dc7196033e92.cpp b/nrtri/binary_search/3e8c8af4-409f-11ec-83c9-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cf75c4489eb59bd1a705dbd7e7dff53bab74335 --- /dev/null +++ b/nrtri/binary_search/3e8c8af4-409f-11ec-83c9-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n, v[801], nr; + +int main() +{ + fin>>n; + for(int i=1; i<=n; i++) + fin>>v[i]; + sort(v+1, v+n+1); + + for(int i=1; iv[i]+v[j]) + right=m-1; + else left=m; + } + nr+=left-j; + } + + fout< +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[860],n,nrsol; + +void cautareBinara(int a, int b) +{ + int sum,stg,dr,mid; + sum=v[a]+v[b]; + stg=max(a,b); + dr=n; + while (stg<=dr) + { + mid=(stg+dr)/2; + if (v[mid]>sum) + dr=mid-1; + if (v[mid]<=sum) + stg=mid+1; + } + mid=(stg+dr)/2; + if (v[mid]>sum) + mid--; + nrsol=mid-max(a,b)+nrsol; + +} + + + +int main() +{ + fin>>n; + for (int i=1; i<=n; i++) + { + fin>>v[i]; + } + sort(v+1,v+n+1); + for (int i=1; i<=n-2; i++) + { + for (int j=i+1; j<=n-1; j++) + { + + cautareBinara(i,j); + + } + } + fout< +#include + +using namespace std; + +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); + +int n, cnt, a[805]; + +int main () { + fin >> n; + for (int i = 1 ; i <= n ; i ++) + fin >> a[i]; + sort (a + 1, a + n + 1); + for (int i = 1 ; i <= n - 2; i ++) + for (int j = i + 1 ; j < n; j ++) { + int st = j + 1, dr = n , k = n; + while (st <= dr) { + k = (st + dr) / 2; + if (a[i] + a[j] >= a[k]) + st = k + 1; + else + dr = k - 1; + } + cnt += (dr - j); + } + fout << cnt; + return 0; +} diff --git a/nrtri/binary_search/3fafeb66-409f-11ec-bd2b-dc7196033e92.cpp b/nrtri/binary_search/3fafeb66-409f-11ec-bd2b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f8c371ec4976b8930263eeadf7c20d5ec0faa19 --- /dev/null +++ b/nrtri/binary_search/3fafeb66-409f-11ec-bd2b-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include +#include +using namespace std; +int v[800]; + +int search(int n, int suma) { + // cautam elementul cel mai mare <= decat suma + int st = 0, dr = n-1, mij; + while (st < dr){ + mij = (st + dr + 1)/2; + if (suma < v[mij]){ + dr = mij-1; + }else{ + st = mij; + } + } + return st; +} + +int main() +{ + int n, i, j, k, rasp; + + ifstream f ("nrtri.in"); + ofstream g ("nrtri.out"); + + f >> n; + + for (i = 0; i < n; i++){ + f >> v[i]; + } + + sort(v, v + n); + + rasp = 0; + for (i = 0; i < n-2; i++){ + for (j = i+1; j < n-1; j++){ + k = search(n, v[i] + v[j]); + if (k > j) { + rasp += k - j; + } + } + } + + g << rasp; + + f.close(); + g.close(); + return 0; +} diff --git a/nrtri/binary_search/3feb58c2-409f-11ec-b513-dc7196033e92.cpp b/nrtri/binary_search/3feb58c2-409f-11ec-b513-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd8919cdb5249054a2fd6d19655fe663f404e486 --- /dev/null +++ b/nrtri/binary_search/3feb58c2-409f-11ec-b513-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +int v[800],put; + +int serch(int n, int i, int j){ + int st = 0, dr = n - 1, mij; + while (st < dr) { + mij = (st + dr + 1) / 2; + if (v[i] + v[j] < v[mij]) { + dr = mij - 1; + } + else { + st = mij; + } + } + return st; +} + +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n, i, j, u, cnt; + f>>n; + for(i=0; i>v[i]; + } + sort(v, v+n); + cnt=0; + for(i=0;ij){ + cnt+=u-j; + } + } + } + g< +#include +#include +using namespace std; + +int v[800],put; + + +int serch(int n, int i, int j){ + int poz = 0; + for (int bit = put; bit > 0; bit /= 2) { + if (poz + bit < n && v[poz + bit] <= v[i] + v[j]) { + poz += bit; + } + } + return poz; +} + +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n, i, j, u, cnt; + f>>n; + for(i=0; i>v[i]; + } + sort(v, v+n); + // precalc + put = 1; + while (put <= n) { + put *= 2; + } + // la terminarea whileului put > n + // vrem expres cea mai mare putere a lui 2 <= n + put /= 2; + + cnt=0; + for(i=0;ij){ + cnt+=u-j; + } + + } + } + + g< +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int v[805]; +int main() +{ + int n; + cin>>n; + for(int i=1;i<=n;++i) + cin>>v[i]; + sort(v+1,v+n+1); + int ans=0; + for(int i=1;iv[i]+v[j]) + r=med-1; + else l=med; + } + ans+=l-j; + } + cout< +#include +#define N 800 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[N]; +int n; +int cautare_binara(int s, int d, int i, int j){ + int m, nr = 0; + while (s <= d){ + m = (s + d) / 2; + if (v[m] <= v[i] + v[j]){ + nr = m; + s = m + 1; + } + else + d = m - 1; + } + if (nr == 0) + return 0; + return nr - j;///numarul de muchii posibile de cuplat cu v[i] si v[j] +} +void citire() { + fin >> n; + for (int i = 0; i < n; i++) + fin >> v[i]; +} +int rez() { + int i, j, s = 0; + for (i = 0; i < n; i++){ + for (j = i + 1; j < n; j++){ + s = s + cautare_binara(j + 1, n-1, i, j); + } + } + return s; + +} +int main(){ + citire(); + sort(v, v + n); + fout << rez(); + return 0; +} +// Run program: Ctrl + F5 or Debug > Start Without Debugging menu +// Debug program: F5 or Debug > Start Debugging menu + +// Tips for Getting Started: +// 1. Use the Solution Explorer window to add/manage files +// 2. Use the Team Explorer window to connect to source control +// 3. Use the Output window to see build output and other messages +// 4. Use the Error List window to view errors +// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project +// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file diff --git a/nrtri/binary_search/41ff811b-409f-11ec-aa48-dc7196033e92.cpp b/nrtri/binary_search/41ff811b-409f-11ec-aa48-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a29866a200be3f8613c249734ad19fecae293823 --- /dev/null +++ b/nrtri/binary_search/41ff811b-409f-11ec-aa48-dc7196033e92.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; + +int v[802], n; +int cautare(int p, int u,int i , int j) +{ + int m; + m=(p+u)/2; + while (p<=u){ + if ((v[m]<=v[i]+v[j] && v[m+1]>v[i]+v[j]) || (v[m]<=v[i]+v[j] && m==n)) + return m; + else if (v[m]<=v[i]+v[j] && v[m+1]<=v[i]+v[j]) { + p=m+1; + m=(p+u)/2; + } + else { + u=m-1; + m=(p+u)/2; + } + } + return 0; +} +int main() { + + +freopen("nrtri.in", "r", stdin); +freopen("nrtri.out", "w", stdout); + + +int i, j, k; +scanf("%d",&n); +for(i = 1; i <= n; i++) { + scanf("%d", &v[i]); +} +int ok, aux; + +do { + +ok = 1; + for(i = 1; i < n ; i++) { + if(v[i] > v[i+1]) { + + aux = v[i]; + v[i] = v[i+1]; + v[i+1] = aux; + ok = 0; + } + } + + + +} while(ok == 0); + + + + +int nr = 0, i1, j1; + +for(i = 1; i <= n-2; i++) { + for(j = i+1; j <= n-1 ; j++) { + nr += cautare(1,n,i,j) - j; + + } +} + +printf("%d",nr); + + + +return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/42b0dd4b-409f-11ec-b873-dc7196033e92.cpp b/nrtri/binary_search/42b0dd4b-409f-11ec-b873-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28ae2557cf79ce7cd0d4305172fb118c85c49888 --- /dev/null +++ b/nrtri/binary_search/42b0dd4b-409f-11ec-b873-dc7196033e92.cpp @@ -0,0 +1,55 @@ +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMAX = 800; +int v[NMAX+5]; + +///a treia latura trebuie sa fie mai mica sau egala decat suma primelor doua laturi + + +int cautare_binara(int st, int dr, int i, int j) +{ + + int mid, sol =0; + while(st<=dr) + { + mid = (st + dr )>>1; + if(v[mid]<=v[i] + v[j]) + { + sol = mid; + st = mid + 1; + } + else + dr = mid-1; + } + if(sol == 0) + return 0; + return sol - j; +} + +int main() + +{ + + int i, j, n, sol =0; + + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1, v + n + 1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + sol = sol + cautare_binara(j+1, n, i, j); + } + } + fout< + +#include + +#include + +using namespace std; + +ifstream f("nrtri.in"); + +ofstream g("nrtri.out"); + +int v[801], n; + +int cautbin(int x) + +{ + + int p = 1, u = n, poz = -1; + + while(p <= u) + + { + + int m = (p + u) / 2; + + if(x >= v[m]) + + { + + poz = m; + + p = m + 1; + + } + + else + + u = m - 1; + + } + + return poz; + +} + + + +int main() + +{ + + int nr = 0; + + f >> n; + + for(int i = 1; i <= n; i++) + + f >> v[i]; + + sort(v + 1, v + n + 1); + + for(int i = 1; i < n - 1; i++) + + for(int j = i + 1; j < n; j++) + + { + + int poz = cautbin(v[i] + v[j]); + + if(poz != -1) + + nr += (poz - j); + + } + + g << nr; + + return 0; + +} diff --git a/nrtri/binary_search/4478ac75-409f-11ec-a583-dc7196033e92.cpp b/nrtri/binary_search/4478ac75-409f-11ec-a583-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..765c0b0bc9731e0518e9818f4dad8db42577b4c2 --- /dev/null +++ b/nrtri/binary_search/4478ac75-409f-11ec-a583-dc7196033e92.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; +int n; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[805]; + +int cbin(int val) +{ + int st=1,dr=n,ok=-1; + while(st>n; + for(int i=1;i<=n;i++) + in>>v[i]; + + for (int t=1; t<=n; ++t) { + for(int i=1;iv[i+1]) + { + int a=v[i]; + v[i]=v[i+1]; + v[i+1]=a; + } + } + } + + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + int a = max(cbin(abs(v[i] - v[j])) - 1, j); + int b = max(cbin(v[i] + v[j]), j); + ans+= b-a; + } + } + out< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,a[809],k=0; + +void sortare(){ + int nr1; + for(int i=0;i>n; +for(int i=0;i>a[i]; +} +sortare(); + for(int i=0;i +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, cnt; +int arr[805]; + +void interclasare(int st1, int dr1, int st2, int dr2){ + + int i = st1; + int j = st2; + + int k = 0; + + int * auxarr = new int[dr1 - st1 + dr2 - st2 + 5]; + + while(i <= dr1 && j <= dr2){ + + if(arr[i] <= arr[j]){ + + auxarr[k] = arr[i]; + i += 1; + } + else{ + + auxarr[k] = arr[j]; + j += 1; + } + k += 1; + } + + while(i <= dr1){ + + auxarr[k] = arr[i]; + i += 1; + k += 1; + } + + while(j <= dr2){ + + auxarr[k] = arr[j]; + j += 1; + k += 1; + } + + for(int i = 0; i < k; i++) + arr[st1 + i] = auxarr[i]; + + delete[] auxarr; +} + +void mergesort(int st, int dr){ + + if(st >= dr) + return; + + int m = (st + dr) / 2; + + mergesort(st, m); + mergesort(m + 1, dr); + + interclasare(st, m, m + 1, dr); +} + +int binarysearch(int x, int i, int j){ + + while(i < j){ + + int m = i + (j - i) / 2; + + if(arr[m] <= x){ + + i = m + 1; + } + else{ + + j = m; + } + } + + if(arr[(i + j) / 2] > x) + return ((i + j) / 2) - 1; + + return j; +} + +int main(){ + + f>>n; + + for(int i = 0; i < n; i++) + f>>arr[i]; + + mergesort(0, n - 1); + + for(int i = 0; i < n - 2; i++) + for(int j = i + 1; j < n - 1; j++){ + + int k = binarysearch(arr[i] + arr[j], j + 1, n - 1); + + if(k >= 0){ + + cnt += k - j; + } + } + + g< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int v[801]; + +int cautare(int x, int st, int dr){ + int m; + while (st < dr) + { + m = st + (dr - st) / 2; + if (v[m] <= x) + st = m + 1; + else + dr = m;} + return st - 1; +} +int n, i, j, k; +int main() +{ + + + f>>n; + for (i=0; i>v[i]; + for(i = 0; i < n-1; i++) + for(j = i + 1; j < n; j++) + if(v[i]>v[j]) + swap(v[i],v[j]); + + for (i = 0; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { + k += cautare(v[i] + v[j], j + 1, n) - j; + } + } + g << k; + + return 0; + +} diff --git a/nrtri/binary_search/47671369-409f-11ec-b7d1-dc7196033e92.cpp b/nrtri/binary_search/47671369-409f-11ec-b7d1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcb2fffae93bd085c8d27a7ac09ef142a59e825b --- /dev/null +++ b/nrtri/binary_search/47671369-409f-11ec-b7d1-dc7196033e92.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[800]; + +int cautare(int x, int st, int dr) +{ + + int m; + while (st < dr) + { + + m = st + (dr - st) / 2; + if (v[m] <= x) + st = m + 1; + else + dr = m; + } + return st - 1; + +} + + +int main() +{ + + int n, i, j, poz, cnt; + fin >> n; + for (i = 0; i < n; i++) + fin >> v[i]; + sort(v, v + n); + cnt = 0; + for (i = 0; i < n - 1; i++) + { + for (j = i + 1; j < n; j++) + { + + cnt += cautare(v[i] + v[j], j + 1, n) - j; + + } + } + fout << cnt; + + return 0; + +} diff --git a/nrtri/binary_search/47ce1603-409f-11ec-9f05-dc7196033e92.cpp b/nrtri/binary_search/47ce1603-409f-11ec-9f05-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..108f708dbc9f76be5f7289f814bbe7c0c9de3b54 --- /dev/null +++ b/nrtri/binary_search/47ce1603-409f-11ec-9f05-dc7196033e92.cpp @@ -0,0 +1,115 @@ +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, cnt; +int arr[805]; + +void interclasare(int st1, int dr1, int st2, int dr2){ + + int i = st1; + int j = st2; + + int k = 0; + + int * auxarr = new int[dr1 - st1 + dr2 - st2 + 5]; + + while(i <= dr1 && j <= dr2){ + + if(arr[i] <= arr[j]){ + + auxarr[k] = arr[i]; + i += 1; + } + else{ + + auxarr[k] = arr[j]; + j += 1; + } + k += 1; + } + + while(i <= dr1){ + + auxarr[k] = arr[i]; + i += 1; + k += 1; + } + + while(j <= dr2){ + + auxarr[k] = arr[j]; + j += 1; + k += 1; + } + + for(int i = 0; i < k; i++) + arr[st1 + i] = auxarr[i]; + + delete[] auxarr; +} + +void mergesort(int st, int dr){ + + if(st >= dr) + return; + + int m = (st + dr) / 2; + + mergesort(st, m); + mergesort(m + 1, dr); + + interclasare(st, m, m + 1, dr); +} + +int binarysearch(int x, int i, int j){ + + while(i < j){ + + int m = i + (j - i) / 2; + + if(arr[m] <= x){ + + i = m + 1; + } + else{ + + j = m; + } + } + + if(arr[j] == x) + return j; + + return j - 1; +} + +int main(){ + + f>>n; + + for(int i = 0; i < n; i++) + f>>arr[i]; + + mergesort(0, n - 1); + + for(int i = 0; i < n - 2; i++) + for(int j = i + 1; j < n - 1; j++){ + + int k = binarysearch(arr[i] + arr[j], j + 1, n); + + if(k >= 0){ + + cnt += k - j; + } + } + + g< +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[802]; + +int main() +{ + int n, rez = 0; + fin >> n; + for(int i = 0; i < n; i++) + fin >> v[i]; + + sort(v, v + n); + for(int x2 = 1; x2 < n - 1; x2++) + { + int rez_actual = 0, x1 = 0, y = x2 + 1; + while(x1 < x2) + { + while(v[x1] + v[x2] >= v[y] && y < n) + { + y++; + rez_actual++; + } + rez += rez_actual; + x1++; + } + } + fout << rez; + return 0; +} diff --git a/nrtri/binary_search/49180b30-409f-11ec-8ce1-dc7196033e92.cpp b/nrtri/binary_search/49180b30-409f-11ec-8ce1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40466272270271c29b2b704d9083f0d8c563a0d0 --- /dev/null +++ b/nrtri/binary_search/49180b30-409f-11ec-8ce1-dc7196033e92.cpp @@ -0,0 +1,69 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); + +ofstream g("nrtri.out"); + +void interschimbare( int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +int partition (int v[], int minim, int maxim) +{ + int pivot = v[maxim]; + int i = minim - 1; + for(int j = minim; j<=maxim-1;j++) + { + if(v[j] < pivot) + { + i++; + interschimbare(&v[i],&v[j]); + } + } + interschimbare(&v[i+1],&v[maxim]); + return (i+1); +} + +void quicksort(int v[],int minim, int maxim) +{ + if (minim>n; + for(int i = 0;i < n;i++ ) + f>>v[i]; + quicksort(v,0,n-1); + + int nr = 0; + + for(int i=0;i= v[k]) + k++; + if (k>j) + nr += k - j - 1; + } + } + + g< +#include + +using namespace std; + +ifstream fin ("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[1000]; + +int main() +{ + int n, sol = 0; + fin >> n; + for(int i = 1; i <= n; ++i) fin >> v[i]; + fin.close(); + + sort(v+1, v+n+1); + for(int i = 1; i < n-1; ++i) + for(int j = i+1; j < n; ++j) + { + int st = j, dr = n, mij, aux; + while(st <= dr) + { + mij = (st+dr) / 2; + if(v[i]+v[j] >= v[mij]) + { + aux = mij; + st = mij+1; + } + else dr = mij-1; + } + sol += (aux - j); + } + + fout << sol; + fout.close(); + return 0; +} diff --git a/nrtri/binary_search/4a852919-409f-11ec-96a3-dc7196033e92.cpp b/nrtri/binary_search/4a852919-409f-11ec-96a3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fcb6eb71cc3884a6a3040d98f1d50a8d07b9b2c --- /dev/null +++ b/nrtri/binary_search/4a852919-409f-11ec-96a3-dc7196033e92.cpp @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +const int Nmax = 800; + +int v[1 + Nmax]; + +int bs(int st, int dr, int val) +{ + int last = -1; + + while (st <= dr) + { + int med = (st + dr) / 2; + if (val == v[med]) + { + last = med; + st = med + 1; + } + else if (val < v[med]) + { + dr = med - 1; + } + else + { + last = med; + st = med + 1; + } + } + + return last; +} +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + + int n; + long long rasp = 0; + scanf("%d", &n); + + for (int i = 1; i <= n; i++) + { + scanf("%d", &v[i]); + } + + sort(v + 1, v + n + 1); + + for (int i = 1; i < n; i++) + { + for (int j = i + 1; j <= n; j++) + { + int sum = v[i] + v[j], cate; + cate = bs(1, n, sum) - (j + 1) + 1; + if (cate > 0) + { + rasp += cate; + } + } + } + + printf("%lld\n", rasp); + + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/4abd078b-409f-11ec-a94c-dc7196033e92.cpp b/nrtri/binary_search/4abd078b-409f-11ec-a94c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de3116908af5a5b60c844e997fbe509cde701689 --- /dev/null +++ b/nrtri/binary_search/4abd078b-409f-11ec-a94c-dc7196033e92.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; + +int cnt[300001] = {-1}, aux; +vector v; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int sum = 0; + +int cautbin(int l, int r, int nr) +{ + int last = -1; + while (l <= r) + { + int mid = l + (r - l) / 2; + if (v[mid] == nr) + { + last = mid; + l = mid + 1; + } + else if (nr < v[mid]) + { + r = mid - 1; + } + else + { + l = mid + 1; + last = mid; + } + } + return last; +} +int main() +{ + int nr; + f >> nr; + int x = 0; + for (int i = 0; i < nr; i++) + { + f >> aux; + cnt[aux]++; + + v.push_back(aux); + } + + sort(v.begin(), v.end()); + + for (int i = 0; i < nr - 2; i++) + { + for (int j = i + 1; j < nr - 1; j++) + { + // cout << "coord:" << i << ' ' << j << "\n"; + int head = v[i] + v[j]; + int c = cautbin(j, nr - 1, head); + if (c - j > 0) + { + sum += c - j; + } + //if (cnt[v[j]] > 0) + // j += cnt[v[j]] - 1; + // cout << "coord:" << i << ' ' << j << "\n\n"; + } + // if (cnt[v[i]] > 0) + // i += cnt[v[i]] - 1; + } + g << sum; +} \ No newline at end of file diff --git a/nrtri/binary_search/4b4baa16-409f-11ec-9669-dc7196033e92.cpp b/nrtri/binary_search/4b4baa16-409f-11ec-9669-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c75bee25d2be12f49a48122a483b7ded59daf72e --- /dev/null +++ b/nrtri/binary_search/4b4baa16-409f-11ec-9669-dc7196033e92.cpp @@ -0,0 +1,46 @@ +#include +#include +#define MAX_N 800 +using namespace std; + +int v[MAX_N+1], n, ans; + +void read() { + FILE* fin = fopen("nrtri.in","r"); + fscanf(fin,"%d",&n); + for(int i = 1; i <= n; i++) + fscanf(fin,"%d",&v[i]); + fclose(fin); +} + +int binarySearch(int Begin, int End, int val) { + int mid, b = Begin, e = End, pos = -1; + while(b <= e) { + mid = (b + e) >> 1; + if(v[mid] <= val) { + pos = mid; + b = mid + 1; + } else e = mid - 1; + } + return pos; +} + +void solve() { + FILE* fout = fopen("nrtri.out","w"); + int i, j, p; + for(i = 1; i <= n; i++) + for(j = i + 1; j <= n; j++) { + p = binarySearch(j+1,n,v[i]+v[j]); + if(p != -1) + ans += p - j; + } + fprintf(fout,"%d\n",ans); + fclose(fout); +} + +int main() { + read(); + sort(v+1,v+n+1); + solve(); + return 0; +} diff --git a/nrtri/binary_search/4b796cc5-409f-11ec-b8ca-dc7196033e92.cpp b/nrtri/binary_search/4b796cc5-409f-11ec-b8ca-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d27d8cf45f45fca06fd8655e70e936cbd6bb63d --- /dev/null +++ b/nrtri/binary_search/4b796cc5-409f-11ec-b8ca-dc7196033e92.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +using namespace std; + +int aux; +vector v; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int sum = 0; + +int cautbin(int l, int r, int nr) +{ + int last = -1; + while (l <= r) + { + int mid = l + (r - l) / 2; + if (v[mid] == nr) + { + last = mid; + l = mid + 1; + } + else if (nr < v[mid]) + { + r = mid - 1; + } + else + { + l = mid + 1; + last = mid; + } + } + return last; +} +int main() +{ + int nr; + f >> nr; + int x = 0; + for (int i = 0; i < nr; i++) + { + f >> aux; + v.push_back(aux); + } + + sort(v.begin(), v.end()); + + for (int i = 0; i < nr - 2; i++) + { + for (int j = i + 1; j < nr - 1; j++) + { + int head = v[i] + v[j]; + int c = cautbin(j, nr - 1, head); + if (c - j > 0) + { + sum += c - j; + } + } + } + g << sum; +} \ No newline at end of file diff --git a/nrtri/binary_search/4c0c8411-409f-11ec-891c-dc7196033e92.cpp b/nrtri/binary_search/4c0c8411-409f-11ec-891c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..385eca1a4c5bbbbf9c262b6a30c05f346ee4d9d7 --- /dev/null +++ b/nrtri/binary_search/4c0c8411-409f-11ec-891c-dc7196033e92.cpp @@ -0,0 +1,57 @@ +#include +#include +#include + +using namespace std; + +int main(){ + + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + + int n; + int v[801]; + + f >> n; + for( int i = 0; i < n; ++i) + f >> v[i]; + + sort(v, v + n); + + + int nr_tri = 0; + for( int i = 0; i < n - 2; ++i) + { + for(int j = i + 1; j < n - 1; ++j) + { + int st = j + 1; + int dr = n - 1; + int sol = -1; + while ( st <= dr ){ + int mid = (st + dr) / 2; + if ( v[i] + v[j] < v[mid]) + dr = mid - 1; + else + if ( mid == n - 1 && (v[i] + v[j] >= v[mid])) + { + sol = mid; + break; + } + else + if ( v[i] + v[j] >= v[mid] && v[i] + v[j] < v[mid + 1]) + { + sol = mid; + break; + } + else + st = mid + 1; + + } + if ( sol != -1 ) { + nr_tri += sol - j; + } + } + } + g << nr_tri << "\n"; + return 0; +} diff --git a/nrtri/binary_search/4d6cb182-409f-11ec-8feb-dc7196033e92.cpp b/nrtri/binary_search/4d6cb182-409f-11ec-8feb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aedc77adeb81a08e43dc34b71274f9d9d20c7f6d --- /dev/null +++ b/nrtri/binary_search/4d6cb182-409f-11ec-8feb-dc7196033e92.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; +ifstream f ("nrtri.in"); +ofstream g ("nrtri.out"); +int v[805], n, rez=0; + +int cautare_binara(int x, int st, int dr) +{ + if(st==dr) + { + if(v[st]>=x) + return st; + else return -1; + } + else if (dr-st==1) + { + if(v[st]>=x) + return st; + else if(v[dr]>=x) + return dr; + else return -1; + } + else if(st=x) + return mij; + else if (mij+1<=dr) cautare_binara(x, mij+1, dr); + } + else + { + return -1; + } +} + +int main() +{ + f>>n; + for(int i=0; i>v[i]; + sort(v, v+n); + for(int i=0; i<=n-3; i++) + { + for(int j=i+2; j<=n-1; j++) + { + int x=v[i]; + int y=v[j]; + for(int p=i+1; p=y) + { + rez+=j-p; + break; + } + } + } + } + g< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, i, j, t, st, dr, mij, a[805]; + +int main() +{ + f >> n; + for(i = 0; i < n; i++) + f >> a[i]; + f.close(); + + sort(a + 0, a + n); + t = 0; + for(i = 0; i < n - 2; i++) + for(j = i + 1; j < n - 1; j++) + { + st = j + 1; + dr = n - 1; + while(st <= dr) + { + mij = (st + dr) / 2; + if(a[mij] <= a[i] + a[j] && (mij == n - 1 || a[mij + 1] > a[i] + a[j])) + break; + else + if(a[mij] <= a[i] + a[j]) + st = mij + 1; + else + dr = mij - 1; + } + if(st <= dr) + t += mij - j; + } + + g << t << '\n'; + g.close(); + + return 0; +} diff --git a/nrtri/binary_search/4f3d198a-409f-11ec-ac26-dc7196033e92.cpp b/nrtri/binary_search/4f3d198a-409f-11ec-ac26-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7b879df71f931d4b49814b1bead36f1c61004ae --- /dev/null +++ b/nrtri/binary_search/4f3d198a-409f-11ec-ac26-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMAX = 803; + +int v[NMAX], n, cnt; + +int binary_search(int l, int r, int val) +{ + int ind = l - 1; + + while (l <= r) + { + int m = ((l + r) >> 1); + if (v[m] <= val) + { + ind = m; + l = m + 1; + } + else r = m - 1; + } + return ind; +} + +int main() +{ + fin >> n; + + for (int i = 1; i <= n; ++i) + fin >> v[i]; + + sort(v + 1, v + n + 1); + + for (int i = 1; i < n; ++i) + for (int j = i + 1; j <= n; ++j) + cnt += binary_search(j + 1, n, v[i] + v[j]) - j; + + fout << cnt << "\n"; + + return 0; +} diff --git a/nrtri/binary_search/4fab6e11-409f-11ec-81df-dc7196033e92.cpp b/nrtri/binary_search/4fab6e11-409f-11ec-81df-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25abec57b8f1e8c0866443b1e2f17211989454f5 --- /dev/null +++ b/nrtri/binary_search/4fab6e11-409f-11ec-81df-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[100005]; +int cautare_binara(int st, int dr, int i, int j) +{ int poz=0; + while(st<=dr) + { + int mij=(st+dr)/2; + if(v[mij]<=v[i]+v[j]) + { poz=mij; + st=mij+1; + + } + else + dr=mij-1; + } + if(poz==0) + return 0; + + return poz-j; + +} +int main() +{ + + int n,nr=0,x; + fin>>n; + for(int i=1;i<=n;i++) + { + fin>>v[i]; + + } + sort(v+1,v+n+1); + for( int i=1;i<=n;i++) + { + for(int j=i+1;j<=n;j++) + { + nr+=cautare_binara(j+1,n,i,j); + } + } + fout< +#include +#include +using namespace std; +int v[850],nr=0; +void cautare(int n,int j,int i) +{ + int r=0; + for(int p=15; p>=0; p--) + if(r+(1<>n; + for(int i=1; i<=n; i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(int i=2; i<=n; i++) + for(int j=1; j +#include +#define nmax 30005 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int mai_mare[nmax]; +int mai_mic[nmax]; +int nr_bete[nmax]; +int bat[805]; + +int main() +{ + int numar_bete; + fin >> numar_bete; + for(int i = 1; i <= numar_bete; i++) + { + fin >> bat[i]; + nr_bete[bat[i]]++; + } + sort(bat + 1, bat + numar_bete + 1); + for(int i = 1; i <= 30000; i++) + { + mai_mic[i] += mai_mic[i - 1] + nr_bete[i]; + } + int solutie = 0; + for(int i = 1; i < numar_bete; i++) + { + for(int j = i + 1; j < numar_bete; j++) + { + int limita_sus = bat[j] + bat[i]; + int dreapta = numar_bete; + int stanga = j + 1, mijloc; + int gasit = 0; + while(!gasit && dreapta >= stanga) + { + mijloc = (dreapta + stanga) / 2; + if(bat[mijloc] == limita_sus) + { + while(bat[mijloc] == limita_sus) + mijloc++; + gasit = 1; + mijloc--; + } + else + { + if(bat[mijloc] > limita_sus) + { + dreapta = mijloc - 1; + } + else + { + stanga = mijloc + 1; + } + } + } + if(!gasit) + { + if(bat[mijloc] > limita_sus) + { + while(bat[mijloc] > limita_sus) + mijloc--; + } + else + { + while(bat[mijloc] < limita_sus && mijloc <= numar_bete) + mijloc++; + mijloc--; + } + } + solutie += mijloc - j; + } + } + fout << solutie; + return 0; +} diff --git a/nrtri/binary_search/50d5de7b-409f-11ec-b337-dc7196033e92.cpp b/nrtri/binary_search/50d5de7b-409f-11ec-b337-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..380ce81bc45f3346428777017a09c59841a282d0 --- /dev/null +++ b/nrtri/binary_search/50d5de7b-409f-11ec-b337-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +using namespace std; +int nr,st,dr,mij,rez,a[809],n,i,j; +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",&a[i]); + sort(a+1,a+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + rez=j; + while(st<=dr) + { + mij=(st+dr)/2; + if(a[i]+a[j]>=a[mij]) + { + rez=mij; + st=mij+1; + } + else + dr=mij-1; + } + nr+=rez-j; + } + cout< +#include + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int aib[3000005],v[100005],x,y,n,nr; +int query(int x) +{ + int st=1; + int dr=n; + int poz=0; + while(st<=dr) + { + int mij=(st+dr)/2; + if(v[mij]<=x) + { + poz=mij; + st=mij+1; + } + else + dr=mij-1; + } + return poz; +} +int main() +{ + f>>n; + for(int i=1;i<=n;i++) + { + f>>v[i]; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++) + { + for(int j=i+1;j<=n;j++) + { + if(v[i]>v[j]) + { + x=v[i]-v[j]; + y=v[i]+v[j]; + } + else if(v[i]=x && v[i]<=y) + nr--; + if(v[j]>=x && v[j]<=y) + nr--; + } + } + g< +using namespace std; +#define pb push_back +#define mp make_pair +#define ft first +#define sc second + +ifstream fin("nrtri.in"); ofstream fout("nrtri.out"); +int n, a[1000]; + + +int bs(int x, int y, int l, int r){ +int m=(l+r)/2; + +if(l>r){return -1;} + +if(l==r){ + if( (a[m]+x)>=y ){return m;} + else{return -1;} +} + + + +if( (a[m]+x)=y ){ + return bs(x, y, l, m); +} + + +} + + + + + + + +int main(){ +fin>>n; + +for(int i=0; i>a[i]; +} +sort(a, a+n); + + + +int rez=0; +for(int i=0; i<(n-2); i++){ + for(int j=i+2; j +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +const int NMAX = 800; +int v[NMAX+5]; +///pentru aceasta problema folosim inegalitatea triunghiurilor c <= a + b, unde a, b, c sunt laturile triunghiului +int cautare_binara(int st, int dr, int i, int j) +{ + int mid, sol =0; + while(st<=dr) + { + mid = (st + dr )>>1; + if(v[mid]<=v[i] + v[j]) + { + sol = mid; + st = mid + 1; + } + else + dr = mid-1; + } + if(sol == 0) + return 0; + return sol - j; +} + +int main() +{ + int i, j, n, sol =0; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1, v + n + 1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + sol = sol + cautare_binara(j+1, n, i, j); + } + } + fout< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[2001]; + +int cbin(int n1, int n2, int n) +{ + int pas, pos = n2 + 1; + for (pas = 1 << 11; pas > 0; pas >>= 1) + if (pos + pas <= n && (v[n1] <= v[n2] + v[pos + pas] && v[n2] <= v[n1] + v[pos + pas] && v[pos + pas] <= v[n1] + v[n2])) + pos += pas; + return pos; +} + +int main() +{ + int n, i, i0, i1, ans = 0; + in >> n; + for (i = 1; i <= n; i++) + in >> v[i]; + sort(v + 1, v + n + 1); + for (i = 1; i <= n - 2; i++) + for (i0 = i + 1; i0 <= n - 1; i0++) + { + i1 = cbin(i, i0, n); + if (v[i] <= v[i0] + v[i1] && v[i0] <= v[i] + v[i1] && v[i1] <= v[i] + v[i0]) + { + ans += i1 - i0; + //out< i1 && i + i1 > i0 && i0 + i1 > i +// v[i] + v[i0] > v[i1] && v[i] + v[i1] > v[i0] && v[i0] + v[i1] > v[i] diff --git a/nrtri/binary_search/5341a6b4-409f-11ec-abd1-dc7196033e92.cpp b/nrtri/binary_search/5341a6b4-409f-11ec-abd1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dee58f7feb81f3bafa74010f7266299538ca4a58 --- /dev/null +++ b/nrtri/binary_search/5341a6b4-409f-11ec-abd1-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int v[2001],s; +bool ok; + +int cbin(int pr,int ul) +{ + int mij,r; + while (pr<=ul) + { + mij=(pr+ul)/2; + if(s>=v[mij]) + { + r=mij; + ok=1; + pr=mij+1; + } + else + ul=mij-1; + } + return r; +} + +int main() +{ + int nr,j,i,n,tr=0; + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + + for(i=1;i<=n;i++) + { + for(j=i+1;j<=n;j++) + { + ok=0; + s=v[i]+v[j]; + nr=cbin(j+1,n); + if(ok) + { + if(v[nr]+v[i]>=v[j]&&v[nr]+v[j]>=v[i]) + tr+=(nr-j); + } + } + } + + out< +#include +#include +#include + +using namespace std; +int n,i,j,t,c,x,st,dr,med,poz; +vectorv; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + fin>>n; + for (i=0;i>x; + v.push_back(x); + } + sort(v.begin(),v.end()); + for (i=0;i=st) + { + med=(st+dr)/2; + if (v[i]+v[j] +#define pb push_back +#define fi first +#define se second +typedef unsigned long long ul; +typedef long long ll; +using namespace std; + +ll n, t[805], sol = 0; + +ll cautbin(ll x, ll st, ll dr) +{ + ll left = st - 1; + ll ans = 0; + while (st <= dr) + { + ll mid = st + (dr - st) / 2; + if (t[mid] <= x) + { + ans = mid; + st = mid + 1; + } + else + dr = mid - 1; + } + return (ans == 0) ? 0 : ans - left; +} + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + + cin >> n; + for (ll i = 1; i <= n; i++) + cin >> t[i]; + sort(t + 1, t + n + 1); + + for (ll i = 1; i <= n - 2; i++) + { + for (ll j = i + 1; j <= n - 1; j++) + { + ll c = cautbin(t[i] + t[j], j + 1, n); + sol += c; + } + } + cout << sol; + return 0; +} diff --git a/nrtri/binary_search/544f172b-409f-11ec-9ec1-dc7196033e92.cpp b/nrtri/binary_search/544f172b-409f-11ec-9ec1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2ffb7b362017092cd9ddf104e89640da5a5bf88 --- /dev/null +++ b/nrtri/binary_search/544f172b-409f-11ec-9ec1-dc7196033e92.cpp @@ -0,0 +1,54 @@ +#include +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[805],n,midi,ok; +void cautbin(int x,int j) +{ + int left, right, mid; + left=j+1; + ok=0; + right=n; + while(left<=right) + { + mid=(left+right)/2; + if(v[mid]<=x) + { + midi=mid; + left=mid+1; + ok=1; + } + else + { + right=mid-1; + } + } +} +int main() +{ + int i,j,s,a=0; + fin>>n; + for(i=1;i<=n;i++) + { + fin>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + { + for(j=i+1;j<=n;j++) + { + s=v[i]+v[j]; + cautbin(s,j); + if(ok) + { + if(v[midi]+v[i]>=v[j]&&v[midi]+v[j]>=v[i]) + a=a+midi-j; + } + s=0; + } + } + fout< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,a[801],nr; + +int findBin(int poz,int val) +{ + for(int p=n-poz;p;p/=2) + while(poz+p<=n && a[poz+p]<=val) + poz+=p; + + return poz; +} + +int main() +{ + in>>n; + + for(int i=1;i<=n;i++) + in>>a[i]; + + sort(a+1,a+n+1); + + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { + int poz=findBin(j,a[i]+a[j]); + + nr+=poz-j; + } + + out< +#include +using namespace std; +int v[801]; +int main(){ + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); +int n,x,y,i,j,c=0,st,dr,mij,poz=-1; +cin>>n; +for(i=1;i<=n;i++) + cin>>v[i]; +sort(v+1,v+n+1); +for(i=1;i<=n;i++){ + x=v[i]; + for(j=i+1;j<=n;j++){ + y=v[j]; + poz=-1; + st=j+1; + dr=n; + while(st<=dr){ + mij=(st+dr)/2; + if(v[mij]>x+y) + dr=mij-1; + else{ + poz=mij; + st=mij+1; + } + } + if(poz!=(-1)) + c=c+(poz-j); + } +} +cout< +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n; +int bat[801]; +int nrTri=0; + +void binary_s(int i, int j) +{ + int l=j+1,r=n,mid,sol=-1; + + while(l<=r) + { + mid=(l+r)/2; + if(bat[mid]<=bat[i]+bat[j]) + { + sol=mid; + l=mid+1; + } + else + r=mid-1; + } + if(sol!=-1) + nrTri+=sol-j; + +} + +int main() +{ + fin>>n; + + for(int i=1;i<=n;i++) + fin>>bat[i]; + + sort(bat+1,bat+n+1); + + for(int i=1;i + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int main() +{ + int st,dr,n,m,v[801],k=0,s,p,aux; + fin>>n; + for(int i=1;i<=n;i++) + fin>>v[i]; + for(int i=1;i<=n;i++) + for(int j=i+1;j<=n;j++) + if(v[i]>v[j]) + { + aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } + for(int i=1;i<=n;i++) + for(int j=i+1;j<=n;j++) + { + p=0; + s=v[i]+v[j]; + st=j; dr=n; + while(st<=dr) + { + m=(st+dr)/2; + if(s +#include + +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int n, b[1001], nr; + +int main() +{ + fin >> n; + for (int i = 1; i <= n; i++) + fin >> b[i]; + + sort(b+1, b+n+1); + + for (int i = 1; i < n; i++) { + for (int j = i+1; j < n; j++) { + + int st = j+1, dr = n, s = b[i]+b[j], mid; + while (st <= dr) { + mid = st + (dr-st)/2; + if (b[mid] > s) + dr = mid-1; + else if (b[mid] < s) + st = mid+1; + else { + dr = mid; + break; + } + } + while (b[dr+1] <= s && dr+1 <= n) + dr++; + + nr += (dr - j); + } + } + fout << nr; + return 0; +} diff --git a/nrtri/binary_search/58414185-409f-11ec-b921-dc7196033e92.cpp b/nrtri/binary_search/58414185-409f-11ec-b921-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c9d7ab1741662d8e0a0755168c131a13c2c037 --- /dev/null +++ b/nrtri/binary_search/58414185-409f-11ec-b921-dc7196033e92.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; +const int nmax = 805; +int n, x,y; +int v[nmax]; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +void citire() +{ + fin >> n; + for(int i = 1; i <= n; i ++) + fin >> v[i]; +} + +int cautBinar(int st, int dr, int x) +{ + int mij; + int poz = 0; + + + while(st <= dr) + { + mij = (st + dr ) / 2; + if(v[mij] <= x){ + poz = mij; + st = mij + 1; + } + else{ + dr = mij - 1; + } + } +return poz; +} + + + + +void rezolva() +{ + int k; + int sol = 0; + sort(v + 1, v + 1 + n); + + for(int i = 1; i <= n; i ++){ + for(int j = i + 1; j <= n; j ++) + { + k = cautBinar(j + 1, n,v[i] + v[j]); + if(k != 0) + { + sol = sol + k - j; + } + } + } + fout << sol; +} + + + + +int main() + +{ + citire(); + rezolva(); + + + + return 0; + +} diff --git a/nrtri/binary_search/587662d0-409f-11ec-861a-dc7196033e92.cpp b/nrtri/binary_search/587662d0-409f-11ec-861a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb9fdec2d0aac4b2614d28c36d72cf32a432cd13 --- /dev/null +++ b/nrtri/binary_search/587662d0-409f-11ec-861a-dc7196033e92.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +using namespace std; +int const N = 1000; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int bsUltimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = 0; + int pas = 1 << 10; + + while (pas > 0) + { + if (sol + pas < n && LungimeBat[sol + pas] <= SumaLaturilor) + { + sol += pas; + } + pas /= 2; + } + + return sol; +} + +int bsPrimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = bsUltimul(n, LungimeBat, SumaLaturilor - 1); + if (sol < SumaLaturilor) + { + sol++; + } + + return sol; +} + +int main() +{ + /** Pentru a forma un triunghi: + * a + b >= c + * b + c >= a + * a + c >= b +{1} + [abs(i - j); i + j] + cautam binar numerele din interval + */ + + int n; + fin >> n; + + int contor = 0; + int LungimeBat[N]; + + for (int i = 0; i < n; i++) + { + fin >> LungimeBat[i]; + } + + sort(LungimeBat, LungimeBat + n); + + for (int i = 0; i < n - 2; i++) // Pentru ultimele 2 nr din sir nu vom putea forma un triunghi + { + for (int j = i + 1; j < n - 1; j++) // Mergem pana la penultima cifra + { + int Dreapta = bsUltimul(n, LungimeBat, LungimeBat[i] + LungimeBat[j]); + int Stanga = j + 1; + + int NrInInterval = Dreapta - Stanga + 1; + + contor += NrInInterval; + } + } + + fout << contor; +} + + + + diff --git a/nrtri/binary_search/58e8eb0f-409f-11ec-a4fb-dc7196033e92.cpp b/nrtri/binary_search/58e8eb0f-409f-11ec-a4fb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21570b00b8db78065521537e4d753481f2029b17 --- /dev/null +++ b/nrtri/binary_search/58e8eb0f-409f-11ec-a4fb-dc7196033e92.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; +int const N = 1000; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int bsUltimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = 0; + int pas = 1 << 10; + + while (pas > 0) + { + if (sol + pas < n && LungimeBat[sol + pas] <= SumaLaturilor) + { + sol += pas; + } + pas /= 2; + } + + return sol; +} + +int bsPrimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = bsUltimul(n, LungimeBat, SumaLaturilor - 1); + if (sol < SumaLaturilor) + { + sol++; + } + + return sol; +} + +int main() +{ + /** Pentru a forma un triunghi: + * a + b >= c + * b + c >= a + * a + c >= b +{1} + [abs(i - j); i + j] + cautam binar numerele din interval + */ + + int n; + fin >> n; + + int contor = 0; + int LungimeBat[N]; + + for (int i = 0; i < n; i++) + { + fin >> LungimeBat[i]; + } + + sort(LungimeBat, LungimeBat + n); + + for (int i = 0; i < n - 2; i++) // Pentru ultimele 2 nr din sir nu vom putea forma un triunghi + { + for (int j = i + 1; j < n - 1; j++) // Mergem pana la penultima cifra + { + int Dreapta = bsUltimul(n, LungimeBat, LungimeBat[i] + LungimeBat[j]); + int Stanga = j + 1; + + int NrInInterval = Dreapta - Stanga + 1; + + contor += NrInInterval; + } + } + + fout << contor; +} \ No newline at end of file diff --git a/nrtri/binary_search/594d818f-409f-11ec-b576-dc7196033e92.cpp b/nrtri/binary_search/594d818f-409f-11ec-b576-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a62296029a55ddbf1d6d2338c03770f3e502926 --- /dev/null +++ b/nrtri/binary_search/594d818f-409f-11ec-b576-dc7196033e92.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int freq[30001], v[801]; + +int main() +{ + + int i,j,n,nr,mj,dr,st,maxx=0,pos=0; + fin >> n; + for(i=1;i<=n;i++) + { + fin >> v[i]; + freq[v[i]]++; + if(v[i]>maxx) + maxx=v[i]; + + } + j=1; + for(i=1;i<=maxx;i++) + { + if(freq[i]>0) + { + while(freq[i]>0) + { + v[j]=i; + j++; + freq[i]--; + } + } + } + + for(i=1;i<=n-2;i++) + { + mj=i+2; + + for(j=i+1;j<=n-1;j++) + { + nr=v[i]+v[j]; + cout << nr << " "; + st=mj; + dr=n; + mj=(st+dr)/2; + while(v[mj]!=nr && st<=dr) + { + + if(nr>v[mj]) + { + st=mj+1; + mj=(dr+st)/2; + } + else + { + if(nrj) + { + pos+=mj-j; + } + } + + } + fout << pos; + return 0; + +} diff --git a/nrtri/binary_search/59834811-409f-11ec-8fb6-dc7196033e92.cpp b/nrtri/binary_search/59834811-409f-11ec-8fb6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fee9ce84cd9405143ffa146f8ad3d81425e42f6c --- /dev/null +++ b/nrtri/binary_search/59834811-409f-11ec-8fb6-dc7196033e92.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +using namespace std; +int const N = 1000; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int bsUltimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = 0; + int pas = 1 << 10; + + while (pas > 0) + { + if (sol + pas < n && LungimeBat[sol + pas] <= SumaLaturilor) + { + sol += pas; + } + pas /= 2; + } + + return sol; +} + +int bsPrimul(int n, int LungimeBat[], int SumaLaturilor) +{ + int sol = bsUltimul(n, LungimeBat, SumaLaturilor - 1); + if (sol < SumaLaturilor) + { + sol++; + } + + return sol; +} + +int main() +{ + /** Pentru a forma un triunghi: + * a + b >= c + * b + c >= a + * a + c >= b + + [abs(i - j); i + j] + cautam binar numerele din interval + */ + + int n; + fin >> n; + + int contor = 0; + int LungimeBat[N]; + + for (int i = 0; i < n; i++) + { + fin >> LungimeBat[i]; + } + + sort(LungimeBat, LungimeBat + n); + + for (int i = 0; i < n - 2; i++) // Pentru ultimele 2 nr din sir nu vom putea forma un triunghi + { + for (int j = i + 1; j < n - 1; j++) // Mergem pana la penultima cifra + { + int Dreapta = bsUltimul(n, LungimeBat, LungimeBat[i] + LungimeBat[j]); + int Stanga = bsPrimul(n, LungimeBat, abs(LungimeBat[i] - LungimeBat[j])); + + if (Stanga <= j) + { + Stanga = j + 1; + } + + int NrInInterval = Dreapta - Stanga + 1; + + if (NrInInterval < 0) + { + NrInInterval = 0; + } + + contor += NrInInterval; + } + } + + fout << contor; +} diff --git a/nrtri/binary_search/5a409151-409f-11ec-9025-dc7196033e92.cpp b/nrtri/binary_search/5a409151-409f-11ec-9025-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7dbe476c8dd8d8f944ae3b43acbce550f78fa7c1 --- /dev/null +++ b/nrtri/binary_search/5a409151-409f-11ec-9025-dc7196033e92.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +using namespace std; + + +int v[802]; + +int main() +{ + ifstream fin ("nrtri.in"); + ofstream fout ("nrtri.out"); + + int n, i, nr, s, z, p, u, mij; + bool ok; + + fin>>n; + + + v[n+1]=60001; + + for(i=1; i<=n; i++) fin>>v[i]; + sort(v+1, v+n+1); + + nr=0; + for(i=1; i<=n-2; i++) + { + for(z=i+1; z<=n-1; z++) + { + s=v[i]+v[z]; + + //cout<<"s= "< s && v[mij-1] <= s) ok=1; + else if(v[mij] <= s) p=mij+1; + else if(v[mij+1] > s) u=mij-1; + } + + //cout<<"i= "< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,u,a[1001],poz,p,nr,x,mj; +int main() +{ + fin>>n; + for(i=1; i<=n; i++)fin>>a[i]; + sort(a+1,a+n+1); + nr=0; + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + + { + x=a[i]+a[j]; + p=j+1; + u=n; + + while(p=a[mj]) + { + poz=mj; + p=mj+1; + } + else u=mj; + + } + mj= (p + u) / 2; + if (a[mj] j+1)nr=nr+poz-j-1; + } + +fout< +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int a[801], n; + +int cb(int start, int val) +{ + int st = start, dr = n, mij; + while (st <= dr) + { + mij = (st+dr)/2; + if (a[mij] > val) + dr = mij - 1; + else + st = mij + 1; + } + return st-1; +} + +int main() +{ + int i, j; + fin >> n; + for (i = 1; i<=n; i++) + fin >> a[i]; + sort(a+1, a+n+1); + int nr = 0, poz; + for (i = 1; i +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int x,N,contor,r,l,i,v[801],j,n,nr; + +int cautarebin(int x,int contor) +{ + int mijl,l=contor,r=N; + + while(l<=r) + { + mijl=(l+r)/2; + if(x>=v[mijl]) + l=mijl+1; + else + r=mijl-1; + } + return r; + +} + +int main() +{ + fin>>N; + for(int i=1; i<=N; i++) + fin>>v[i]; + sort(v+1,v+N+1); + for(int i=1; i +using namespace std; + +int a[1000], n, ans, l3; + +int cautbin(int x){ + + int st = 0, dr = n-1; + int mid; + while(st <= dr){ + mid = (st+dr)/2; + if(a[mid] <= x) + st = mid+1; + else dr = mid - 1; + } + return dr; +} + +int main(){ + + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + cin>>n; + for(int i=0;i>a[i]; + sort(a, a+n); + for(int i=0;i +#include +using namespace std; +int n,v[100001],ans=0,k; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int cautare(int x,int i) +{ + int st=i,dr=n,m; + while(st<=dr) + { + m=(st+dr)/2; + if(v[m]>x) + dr=m-1; + else + st=m+1; + } + return dr; +} + +int main() +{ + + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + for(int i=1; iv[j]) + { + int aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } + } + + for(int i=1; i +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,i,j,st,dr,m,pos,ans; +int v[802]; +int main() +{ + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+1+n); + for(i=1; iv[i]+v[j]) + dr=m-1; + else + { + pos=m; + st=m+1; + } + } + ans+=(pos-j); + } + out< +using namespace std; + +int a[1000], n, ans, l3; + +int cautbin(int x){ + + int st = 0, dr = n-1; + int mid; + while(st <= dr){ + mid = (st+dr)/2; + if(a[mid] <= x) + st = mid+1; + else dr = mid - 1; + } + return dr; +} + +int main(){ + + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + cin>>n; + for(int i=0;i>a[i]; + sort(a, a+n); + + for(int i=0;i +using namespace std; + +int a[1000], n, ans, l3; + +int cautbin(int x){ + + int st = 0, dr = n-1; + int mid; + while(st <= dr){ + mid = (st+dr)/2; + if(a[mid] <= x) + st = mid+1; + else dr = mid - 1; + } + return dr; +} + +int main(){ + + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + cin>>n; + for(int i=0;i>a[i]; + sort(a, a+n); + + for(int i=0;i +#include + +using namespace std; + +int v[1000001],n; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int cbdr(int x) +{ + int st=1,dr=n,last=0; + while(st<=dr) + { + int mid=(st+dr)/2; + if(v[mid]<=x) + { + last=mid; + st=mid+1; + } + else + dr=mid-1; + } + return last; +} + +int main(){ + fin>>n; + for(int i=1;i<=n;i++) + fin>>v[i]; + int s=0; + sort(v+1,v+n+1); + for(int i=1;i a && i <= b) + --s; + if (e > a && e <= b) + --s; + } + fout< +#include +#include + +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int v[805]; + +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + int L[n1], R[n2]; + + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + i = 0; + j = 0; + k = l; + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} + +void mergeSort(int arr[], int l, int r) +{ + if (l < r) + { + int m = l + (r - l) / 2; + + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } +} + +void afiseaza(int v[], int n) { + for (int i = 0; i < n; ++i) + cout << v[i] << " "; +} + +int cautare(int x, int l, int r) { + int mid; + while (l < r) { + mid = (l + r) / 2; + if (v[mid] <= x) + l = mid + 1; + else + r = mid; + } + return l - 1; +} + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; ++i) + cin >> v[i]; + + mergeSort(v, 0, n - 1); + //sort (v, v + n); + int rez = 0, i, j; + for (i = 0; i < n - 1; ++i) + for (j = i + 1; j < n; ++j) + rez += cautare(v[i] + v[j], j + 1, n) - j; + + cout << rez; + + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/5de11708-409f-11ec-87c6-dc7196033e92.cpp b/nrtri/binary_search/5de11708-409f-11ec-87c6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ec8de0944763d36ca90e45d791082ab8ca10503 --- /dev/null +++ b/nrtri/binary_search/5de11708-409f-11ec-87c6-dc7196033e92.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,a[801]; + +bool Triunghi(int a, int b, int c) +{ + if(a+b>=c && a+c>=b && b+c>=a) + return 1; + else + return 0; +} + +int CautBin(int x,int y) +{ + int m,st,dr,sol=-1; + st=y+1; + dr=n; + + while(st<=dr) + { + m=(st+dr)/2; + if(Triunghi(a[x],a[y],a[m])) + { + sol=m; + st=m+1; + } + else dr=m-1; + } + + return sol; +} + +int main() +{ + int ct=0; + + fin>>n; + for(int i=1;i<=n;i++) + fin>>a[i]; + + sort(a,a+n+1); + + + //rezolvare + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { int nr=CautBin(i,j); + if(nr!=-1) + ct+=nr-j; + } + + fout< + +using namespace std; +ifstream ci("nrtri.in"); +ofstream cou("nrtri.out"); +int n,v[802]; + +int cautarenr(int st,int dr,int a) +{ + int mijval,mijind,rasp=0; + + while(st<=dr) //cout<a) + { + dr=mijind-1; + } + else + { + rasp=mijind; + st=mijind+1; + } + + + + + } + + return rasp; + + +} + + +int main() +{ + int i,j,cn=0,k; + ci>>n; + for(i=1; i<=n; i++) + { + ci>>v[i]; + } + sort(v+1,v+n+1); + // cout< +#include + +using namespace std; + +int n, tr[3001], cnt; + +FILE *f=fopen("nrtri.in","r"); +FILE *g=fopen("nrtri.out","w"); +void citire() +{ + fscanf(f, "%d", &n); + for(int i=1; i<=n; i++) + fscanf(f, "%d", &tr[i]); + sort(tr+1, tr+1+n); +} + +bool verif(int a, int b, int c) +{ + if(a+b>=c && a+c>=b && b+c>=a) + { + return true; + } + return false; +} + +int cautbin(int x, int y) +{ + int st=y+1; + int dr=n; + int rez=-1; + while(st<=dr) + { + int mij = (st + dr) / 2; + if(verif(tr[x], tr[y], tr[mij])) + { + rez=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + return rez; +} + +void preluc() +{ + for(int i=1; i<=n-2; i++) + for(int j=i+1; j<=n-1; j++) + { + int nr=cautbin(i, j); + if(nr!=-1) + cnt+=nr-j; + } + fprintf(g, "%d", cnt); +} + +int main() +{ + citire(); + preluc(); + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/5e8c469c-409f-11ec-b82d-dc7196033e92.cpp b/nrtri/binary_search/5e8c469c-409f-11ec-b82d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b6a71de01f1d56346ec9586a7d87e628b7e830a --- /dev/null +++ b/nrtri/binary_search/5e8c469c-409f-11ec-b82d-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +int nr[30001],a[801]; + +int main() +{ + int sol=0,n,i,x,m=0,j,st,dr,val; + FILE *f=fopen("nrtri.in","r"); + fscanf(f,"%d",&n); + for(i=1;i<=n;i++) + { + fscanf(f,"%d",&x); + nr[x]++; + } + for(i=1;i<=30000;i++) + for(j=1;j<=nr[i];j++) + a[++m]=i; + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + {///caut binar ultima aparitzie a lui a[i]+a[j] - latura maxima pe care o putem avea + ///daca a[i] shi a[j] sunt celelalte doua laturi + st=j+1;dr=n;val=a[i]+a[j]; + while(st<=dr) + { + m=(st+dr)/2; + if(val>=a[m])st=m+1; + else dr=m-1; + } + //dr=indicele ultimei aparitzii ale lui val + sol+=dr-j; + } + f=fopen("nrtri.out","w"); + fprintf(f,"%d",sol); + return 0; +} diff --git a/nrtri/binary_search/5eba68b7-409f-11ec-a906-dc7196033e92.cpp b/nrtri/binary_search/5eba68b7-409f-11ec-a906-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..136f7e51aac2174edea5fa7d4050aa9028ff28d0 --- /dev/null +++ b/nrtri/binary_search/5eba68b7-409f-11ec-a906-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[800]; +int cautare( int x, int st, int dr ){ + int mid; + while ( st < dr ){ + mid = st + ( dr - st ) / 2; + if ( v[mid] <= x ) + st = mid + 1; + else + dr = mid; + } + return st - 1; +} +int main(){ + int n, i, j, poz, cnt; + fin >> n; + for ( i = 0; i < n; i++ ) + fin >> v[i]; + sort( v, v + n ); + cnt = 0; + for ( i = 0; i < n - 1; i++ ){ + for ( j = i + 1; j < n; j++){ + cnt+= cautare( v[i] + v[j], j + 1, n ) - j; + } + } + fout << cnt; + return 0; +} diff --git a/nrtri/binary_search/5eedcf83-409f-11ec-a7d9-dc7196033e92.cpp b/nrtri/binary_search/5eedcf83-409f-11ec-a7d9-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec300fda66a829e40c47a6468220f6aacf51192f --- /dev/null +++ b/nrtri/binary_search/5eedcf83-409f-11ec-a7d9-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,v[1001]; +bool verificare(int a, int b, int c); +int cautbin(int x, int y) +{ + int st=y+1; + int dr=n; + int sol=-1; + while(st<=dr) + { + int mij = (st + dr) / 2; + if(verificare(v[x], v[y], v[mij])) + { + sol=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + return sol; +} + +bool verificare(int a, int b ,int c) +{ + if(a+b>=c && a+c>=b && b+c>=a) + return true; + else + return false; +} +int contor=0; +int main() +{ + fin>>n; + for( int i =1;i<=n;i++) + fin>>v[i]; + sort(v, v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { + int z=cautbin(i,j); + if(z!=-1) + contor+=z-j; + } + fout< +#include + +using namespace std; + +int n, tr[3001], cnt; + + FILE *f=fopen("nrtri.in","r"); + FILE *g=fopen("nrtri.out","w"); +void citire() +{ + fscanf(f, "%d", &n); + for(int i=1; i<=n; i++) + fscanf(f, "%d", &tr[i]); + sort(tr+1, tr+1+n); +} + +bool verif(int a, int b, int c) +{ + if(a+b>=c && a+c>=b && b+c>=a) + { + return true; + } + return false; +} + +int cautbin(int x, int y) +{ + int st=y+1; + int dr=n; + int rez=-1; + while(st<=dr) + { + int mij = (st + dr) / 2; + if(verif(tr[x], tr[y], tr[mij])) + { + rez=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + return rez; +} + +void preluc() +{ + for(int i=1; i<=n-2; i++) + for(int j=i+1; j<=n-1; j++) + { + int nr=cautbin(i, j); + if(nr!=-1) + cnt+=nr-j; + } + fprintf(g, "%d", cnt); +} + +int main() +{ + citire(); + preluc(); + return 0; +} diff --git a/nrtri/binary_search/5fce1f5c-409f-11ec-bb3b-dc7196033e92.cpp b/nrtri/binary_search/5fce1f5c-409f-11ec-bb3b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4813f8f35a88ae7f3b64d0b77894509bf4fcb31 --- /dev/null +++ b/nrtri/binary_search/5fce1f5c-409f-11ec-bb3b-dc7196033e92.cpp @@ -0,0 +1,68 @@ +#include +#include + +#define input "nrtri.in" +#define output "nrtri.out" +#define NMAX 805 + +using namespace std; + +ifstream in(input); +ofstream out(output); + +int laturi[NMAX], N, total; + +void Read_Data() +{ + in >> N; + for(int i = 1; i <= N; i++) + in >> laturi[i]; + sort(laturi + 1, laturi + 1 + N); +} + +bool Check_Sol(int a, int b, int c) +{ + if(a + b >= c && a + c >= b && c + b >= a) + return true; + return false; +} + +int Binary_Search(int l1, int l2) +{ + int sol = -1; + int st = l2 + 1, dr = N; + while(st <= dr) + { + int midd = (st + dr) / 2; + if(Check_Sol(laturi[l1], laturi[l2], laturi[midd])) + { + //out << l1 << " " << l2 << " " << midd << "\n"; + sol = midd; + st = midd + 1; + } + else dr = midd - 1; + } + return sol; +} + +void Solve() +{ + for(int i = 1; i <= N - 2; i++) + for(int j = i + 1; j <= N - 1; j++) + { + //out << i << " " << j << " -> "; + int local = Binary_Search(i, j); + //out << local << " " << local - j<< "\n"; + if(local != -1) + total += local - j; + + } + out << total; +} + +int main() +{ + Read_Data(); + Solve(); + return 0; +} diff --git a/nrtri/binary_search/601f4242-409f-11ec-b5e4-dc7196033e92.cpp b/nrtri/binary_search/601f4242-409f-11ec-b5e4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d46a12229963f8f6c33faca96018d7c248041f8d --- /dev/null +++ b/nrtri/binary_search/601f4242-409f-11ec-b5e4-dc7196033e92.cpp @@ -0,0 +1,66 @@ + +#include + +#include + + + +using namespace std; + +ifstream fin("nrtri.in"); + +ofstream fout("nrtri.out"); + +int v[800]; + +int cautare(int x, int st, int dr) { + + int mid; + + while (st < dr) { + + mid = st + (dr - st) / 2; + + if (v[mid] <= x) + + st = mid + 1; + + else + + dr = mid; + + } + + return st - 1; + +} + +int main() { + + int n, i, j, poz, cnt; + + fin >> n; + + for (i = 0; i < n; i++) + + fin >> v[i]; + + sort(v, v + n); + + cnt = 0; + + for (i = 0; i < n - 1; i++) { + + for (j = i + 1; j < n; j++) { + + cnt += cautare(v[i] + v[j], j + 1, n) - j; + + } + + } + + fout << cnt; + + return 0; + +} \ No newline at end of file diff --git a/nrtri/binary_search/607ffc4c-409f-11ec-806d-dc7196033e92.cpp b/nrtri/binary_search/607ffc4c-409f-11ec-806d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0114d460aec140d2f864233ad2988a6df314c4df --- /dev/null +++ b/nrtri/binary_search/607ffc4c-409f-11ec-806d-dc7196033e92.cpp @@ -0,0 +1,62 @@ +//ALEXANDRU MICLEA + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#include +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; +int n; + +int main() { + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + int ans = 0; + + for (int i = 1; i <= n - 2; i++) { + for (int j = i + 1; j <= n - 1; j++) { + int st = j; + int dr = n; + int stop = 0; + while (st <= dr) { + int mid = st + dr; + mid /= 2; + if (v[mid] <= v[i] + v[j]) { + stop = mid; + st = mid + 1; + } + else { + dr = mid - 1; + } + } + ans += stop - j; + } + } + + cout << ans; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/60de7c5c-409f-11ec-b730-dc7196033e92.cpp b/nrtri/binary_search/60de7c5c-409f-11ec-b730-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..738d355a740ca1ef4ccbb5247c353161736bbd6d --- /dev/null +++ b/nrtri/binary_search/60de7c5c-409f-11ec-b730-dc7196033e92.cpp @@ -0,0 +1,65 @@ +//ALEX ENACHE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +//----------------------------------------------------------------- + +#include + +//ifstream cin("input"); ofstream cout("output"); +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; + +int main() { + + int n; + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + int ans = 0; + + for (int i = 1; i <= n-2; i++) { + for (int j = i+1; j <= n-1; j++) { + int st = j; + int dr = n; + int ras = 0; + while (st <= dr) { + int mij = st + dr; + mij /= 2; + if (v[i] + v[j] >= v[mij]) { + ras = mij; + st = mij + 1; + } + else { + dr = mij - 1; + } + } + ans += ras - j; + } + } + + cout << ans; + + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/610a3970-409f-11ec-9118-dc7196033e92.cpp b/nrtri/binary_search/610a3970-409f-11ec-9118-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..504ae34b9eb1f2a788f2c35c15612addac5163fa --- /dev/null +++ b/nrtri/binary_search/610a3970-409f-11ec-9118-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; + +int v[801], n, triunghiuri; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +void sortare(int v[], int n) { + for(int i = 2; i <= n; i++) { + int p = i; + while(p > 1 && v[p-1] > v[p]) { + int aux = v[p-1]; + v[p-1] = v[p]; + v[p] = aux; + p--; + } + } +} + +int cb(int v[], int st, int dr, int nr) { + int a = st-1; + if(nr >= v[dr]) + return dr-a; + else if(nr < v[1]) + return 0; + else + while(st < dr) { + int m = (st+dr) / 2; + if(v[m] <= nr) + st = m+1; + else + dr = m; + } + + if(v[st] > nr) + st--; + return st-a; +} + +int main() { + f >> n; + for(int i = 1; i <= n; i++) + f >> v[i]; + + sortare(v, n); + + for(int i = 1; i < n-1; i++) + for(int j = i+1; j < n; j++) { + int poz = cb(v, j+1, n, v[i]+v[j]); + triunghiuri += poz; + } + + g << triunghiuri; +} diff --git a/nrtri/binary_search/61366570-409f-11ec-986e-dc7196033e92.cpp b/nrtri/binary_search/61366570-409f-11ec-986e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12455864a549ba93ca01280e0b9e61470a97b3d2 --- /dev/null +++ b/nrtri/binary_search/61366570-409f-11ec-986e-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,nrtri; +int B[801]; + +int BS(int x,int st,int dr){ + int rez=-1; + while(st<=dr){ + int mij=st+(dr-st)/2; + if(B[mij]<=x){ + rez=mij; + st=mij+1; + } + else + dr=mij-1; + } + return rez; +} + +int main(){ + in>>n; + for(int i=1;i<=n;++i) + in>>B[i]; + sort(B+1,B+n+1); + for(int i=1;i0) + nrtri+=rez-j; + } + out << nrtri; + return 0; +} diff --git a/nrtri/binary_search/616d52d9-409f-11ec-a7d5-dc7196033e92.cpp b/nrtri/binary_search/616d52d9-409f-11ec-a7d5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaa4e0b6cc08a9c8199df7cc1ab6620782a86182 --- /dev/null +++ b/nrtri/binary_search/616d52d9-409f-11ec-a7d5-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#include +using namespace std; + +int main (){ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + int N; + fin >> N; + int i, v[805]; + for (i = 1; i <= N; ++i) + fin >> v[i]; + sort(v + 1, v + N + 1); + int nr = 0; + for (i = 1; i <= N-2; ++i) + for (int j = i + 1; j <= N-1; ++j){ + int l = 1, r = N, m; + int s = v[i] + v[j], c = 0; + while (l <= r){ + m = (l + r) / 2; + if (v[m] <= s){ + l = m + 1; + } + else{ + r = m - 1; + } + } + nr += (r - j); + } + fout << nr; + return 0; +} diff --git a/nrtri/binary_search/61db3ba8-409f-11ec-b07d-dc7196033e92.cpp b/nrtri/binary_search/61db3ba8-409f-11ec-b07d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92c79c7ab111587bf87333079a5e0df582373292 --- /dev/null +++ b/nrtri/binary_search/61db3ba8-409f-11ec-b07d-dc7196033e92.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int a[803]; + +void Merge(int st, int mij, int dr) +{ + int b[800], j, i, k; + i = st; + j = mij + 1; + k = st-1; + while(i <= mij && j <= dr) + { + if(a[i] > a[j]) b[++k] = a[j++]; + else b[++k] = a[i++]; + } + while(i<=mij) + b[++k]=a[i++]; + while(j<=dr) + b[++k]=a[j++]; + for(i=st;i<=dr;i++) + a[i]=b[i]; +} + + +void Mergesort(int st,int dr) +{ + int mij=(st+dr)/2; + if(st != dr) + { + Mergesort(st,mij); + Mergesort(mij+1,dr); + Merge(st,mij,dr); + } +} + +int main() +{ + int n, i, j, k, x, mij, st, dr, tr, poz; + fin >> n; + for(i = 1; i <= n; i++) + fin >> a[i]; + Mergesort(1, n); + + tr = 0; + for(i = 1; i <= n-2; i++) + for(j = i + 1; j <= n - 1; j++) + { + x = a[i] + a[j]; + st = j + 1; + dr = n; + poz=0; + while(st <= dr) + { + mij = (st + dr) / 2; + if(a[mij] == x) + { + poz = mij; + st = mij + 1; + } + else + if(a[mij] > x) dr = mij - 1; + else {poz = mij; st = mij + 1;} + + } + if(poz) + tr += poz-(j+1)+1; + } + fout << tr; + return 0; +} diff --git a/nrtri/binary_search/623f2aff-409f-11ec-9006-dc7196033e92.cpp b/nrtri/binary_search/623f2aff-409f-11ec-9006-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fed935388b9ab34c560da71a6ebc2d179545405 --- /dev/null +++ b/nrtri/binary_search/623f2aff-409f-11ec-9006-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#define range(x, y, contor) for(int contor = x; contor <= y; contor++) + +std::ifstream fin{"nrtri.in"}; +std::ofstream fout{"nrtri.out"}; + +int n, v[801], k, step, nrtri{0}, sum; + +int main() +{ + fin>>n; + range(1, n, i) fin>>v[i]; + + std::sort(v + 1, v + n + 1); + + range(1, n-2, i){ + range(i + 1, n - 1, j){ + sum = v[i] + v[j]; + + for(step = 1; step < n; step = (step<<1)); + + for(k = j; step; step = (step >> 1)) + if(k + step <= n && v[k + step] <= sum) k += step; + + if(k != j) { + nrtri += (k - j); + } + } + } + + fout< +#include +#include + +using namespace std; + +int a[801]; + +void cbin(int s, int d, int x,int &r) { + + long long m; + + if(s <= d) { + + m = (s + d) / 2; + + if(a[m] <= x) { + + r = m; + + cbin(m+1, d, x, r); + + } else if(a[m] > x) + + cbin(s, m-1, x, r); + } + +} + +int main() +{ + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + + int n, i, j, x, tri = 0; + cin>>n; + for(i = 1; i <= n; i++) + cin>>a[i]; + + sort(a + 1, a + n + 1); + + int temp; + + for(i = 1; i <= n-2; i++) { + for(j = i+1; j <= n-1; j++) { + temp = 0; + x = a[i] + a[j]; + if(x >= a[n]) { + tri += n - j; + continue; + } else if (x >= a[j+1]) { + cbin(j+1, n, x, temp); + tri += temp - j; + } + } + } + + cout< +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int cb(long v[], int l, int x) +{ + int st=1,dr=l,mij; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]<=x) + st=mij+1; + else + dr=mij-1; + } + return dr; +} + +long v[3002]; + +int main() +{ + int n,c=0; + long long s=0; + fin>>n; + for(int i=1;i<=n;++i) + fin>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;++i) + for(int j=i+1;j<=n-1;++j) + { + s=v[i]+v[j]; + c+=cb(v,n,s)-j; + } + fout< +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main () +{ + int v[30000],n,i,j,a,s=0; + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-1;i++) + for(j=i+1;j<=n;j++) + { + a=v[i]+v[j]; + int pos=0; + for(int msk=1<<20;msk>0;msk/=2) + { + if(pos+msk<=n && v[pos+msk]<=a) + pos+=msk; + } + s+=fabs(pos-j); + } + out< +#include +using namespace std; +int v[1000]; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main () +{ + int n,x,y,p1,p2,s=0; + + in>>n; + for(int i=1; i<=n; i++) + in>>v[i]; + + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) { + x=v[i]; + y=v[j]; + p1=j; + p2=j; + for(int msk=1<<20;msk>0;msk/=2) + if(p1+msk<=n && v[p1+msk]<=x+y) + p1+=msk; + for(int msk=1<<20;msk>0;msk/=2) + if(p2+msk<=n && v[p2+msk] +using namespace std; +int n; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[805]; + +int cbin(int val) +{ + int st=1,dr=n,ok=-1; + while(st>n; + for(int i=1;i<=n;i++) + in>>v[i]; + + for (int t=1; t<=n; ++t) { + for(int i=1;iv[i+1]) + { + int a=v[i]; + v[i]=v[i+1]; + v[i+1]=a; + } + } + } + + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + int a = max(cbin(abs(v[i] - v[j])) - 1, j); + int b = max(cbin(v[i] + v[j]), j); + ans+= b-a; + } + } + out< +#include +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int a[801]; +int n,pos; +int cautbin(int val) +{ + pos=0; + for(int msk=1<<30; msk>0; msk/=2) + { + if(pos+msk<=n && a[pos+msk]<=val) + pos+=msk; + } + if(a[pos]==val) + return pos; + else + return -1; +} + + +int main() +{ + int i,j,k,cont=0,x; + in >> n; + + for(i=1;i<=n;i++) + in >> a[i]; + + sort(a+1, a+n+1); + + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + x=a[i]+a[j]; + k=cautbin(x); + cont=cont+pos-j; + + } + } + + + + out << cont; + return 0; +} diff --git a/nrtri/binary_search/64555f5a-409f-11ec-a58e-dc7196033e92.cpp b/nrtri/binary_search/64555f5a-409f-11ec-a58e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a343c427da46ec6fd6cfb129b86c3a11590716dc --- /dev/null +++ b/nrtri/binary_search/64555f5a-409f-11ec-a58e-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include + +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[805]; +void ordonare(int n,int v[]) +{ + int aux; + for(int i=1;iv[j]) + { + aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } +} + +int main() +{ + int n,i,t,nr=0,msk,poz,s=0,j,lat; + in>>n; + for(t=1;t<=n;t++) + { + in>>v[t]; + } + ordonare(n,v); + for(i=1;i0;msk=msk/2) + if(poz+msk<=n&&v[poz+msk]<=lat) + poz=poz+msk; + if(poz>0) + s=s+(poz-j); + } + } + out< +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main() +{ + int n,v[2001],i,s=0,x,y,z; + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(x=1;x0;msk/=2) + if(pos+msk<=n && v[pos+msk]<=z) + pos+=msk; + if(pos>0) + s=s+(pos-y); + } + } + out< +#include +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +short a[2001]; +int n,pos; +int cautbin(int val) +{ + pos=0; + for(int msk=1<<20; msk>0; msk/=2) + { + if(pos+msk<=n && a[pos+msk]<=val) + pos+=msk; + } + if(a[pos]==val) + return pos; + else + return -1; +} + + +int main() +{ + int i,j,k,cont=0,x; + in >> n; + + for(i=1;i<=n;i++) + in >> a[i]; + + sort(a+1, a+n+1); + + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + x=a[i]+a[j]; + k=cautbin(x); + cont=cont+pos-j; + + } + } + + + + out << cont; + return 0; +} diff --git a/nrtri/binary_search/652579f9-409f-11ec-bfce-dc7196033e92.cpp b/nrtri/binary_search/652579f9-409f-11ec-bfce-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52002d330c6a8c8b0aa44ee5764e94b8e6df6767 --- /dev/null +++ b/nrtri/binary_search/652579f9-409f-11ec-bfce-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; + +const int nmax=800; +int v[nmax+1]; + +ifstream ci ("nrtri.in"); +ofstream co ("nrtri.out"); + +int main () +{ + int n,n2=1; + ci >> n; + while (n2*2<=n) + { + n2*=2; + } + for (int i=1; i<=n; i++) + { + ci >> v[i]; + } + sort (v+1,v+n+1); + int sol=0; + for (int i=1; i0; step/=2) + { + if (k+step<=n && v[k+step]<=v[i]+v[j]) + { + k+=step; + } + } + sol+=k-j; + } + } + co << sol; + return 0; +} diff --git a/nrtri/binary_search/65586425-409f-11ec-80a8-dc7196033e92.cpp b/nrtri/binary_search/65586425-409f-11ec-80a8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c96a14efc94d8ac79f5acf3d1f391475dafe0863 --- /dev/null +++ b/nrtri/binary_search/65586425-409f-11ec-80a8-dc7196033e92.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +#define nmax 805 + +int v[nmax],n,nr=0,aux,poz; + +int BS(int x) +{ + int sol=-1,left=0,right=n; + while(left<=right) + { + int mid=(left+right)/2; + if(v[mid]==x) + { + sol=mid; + for(int j=mid+1;j<=n;j++) + { + if(v[j]==v[mid]) + sol++; + else + break; + } + break; + } + if(v[mid]>x) + right=mid-1; + if(v[mid]v[n]) + sol=n; + } + return sol; +} + +int main() +{ + fin>>n; + for(int i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+1+n); + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + aux=v[i]+v[j]; + poz=BS(aux); + nr+=poz-j; + } + } + fout< + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,a[802],st,dr,j,sol,mij; +int main() +{ + f>>n; + for(int i=1; i<=n; ++i) + { + f>>a[i]; + } + int t=0; + for(int i=1; ia[j]) + { + swap(a[i],a[j]); + } + } + } + for(int i=1; i +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, a[801], st, dr, j, sol, m; +int main() +{ + f>>n; + for(int i=1;i<=n;++i) + f>>a[i]; + int t=0; + for(int i=1;ia[j]) + swap(a[i],a[j]); + for(int i=1;i +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int c=0,low,hi,m,s; + +int main() { + int n,v[800],c=0; + in>>n; + for(int i=0;i>v[i]; + sort(v,v+n); + for(int i=0;i + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,st,dr,v[810],i,j,sol,mij,t; +int main() +{ + fin>>n; + for(i = 1;i <= n;i++) + fin>>v[i]; + sort(v + 1,v + n + 1); + for(i = 1;i < n;i++) + for(j = i + 1;j <= n;j++) + { + st = j + 1;dr = n; + sol = 0; + while(st <= dr) + { + mij = (st + dr) / 2; + if(v[mij] <= v[i] + v[j]) + { + sol = mij; + st = mij + 1; + } + else + dr = mij - 1; + } + if(sol != 0) + t += (sol - j); + } + fout< +#include +#include +using namespace std; +int n; +int A[801]; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int found=0; + fin>>n; + for(int i = 1;i<=n;i++) + fin>>A[i]; + sort(A+1,A+n+1); + + for(int i = 1;i<=n;i++) + { + for(int j = i+1;j<=n;j++) + { + int s = A[i]+A[j]; + found+=(upper_bound(A+j+1,A+n+1,s)-A-j-1); + } + } + fout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,v[1000], nr = 0, s; +void cauta(int st, int dr, int s) +{ + int init = st - 1; + int rez; + while (st <= dr) + { + int mid = (st + dr) / 2; + if (s >= v[mid]) + + st = mid + 1; + + else + dr = mid - 1; + } + + nr += (dr - init); +} +int main() +{ + fin >> n; + for (int i = 1; i <= n; i++) + fin >> v[i]; + sort(v + 1, v + n + 1); + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + { + s = v[i] + v[j]; + cauta(j + 1, n , s); + } + fout << nr; +} diff --git a/nrtri/binary_search/67954c26-409f-11ec-93fc-dc7196033e92.cpp b/nrtri/binary_search/67954c26-409f-11ec-93fc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e61bb43defa647a10025e5353f1bfc4ea90d7a4 --- /dev/null +++ b/nrtri/binary_search/67954c26-409f-11ec-93fc-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; + +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); + +int n, v[801], s, st, dr, mid, sol, poz; + +int main (){ + + fin>>n; + + for(int i=1; i<=n; i++) + fin>>v[i]; + sort(v+1,v+1+n); + + for(int i=1; i +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int main() +{ + int n,v[805],i,j,x,st,dr,mij,nr=0; + fin >> n; + for (i=1;i<=n;i++) + { + fin >> v[i]; + } + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for (j=i+1;j<=n-1;j++) + { + x=v[i]+v[j]; + st=j+1; + dr=n; + while (st +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,i,j,triunghiuri,mij,st,dr,a[801]; +int main() +{ + in>>n; + for (i=1; i<=n; ++i) + { + in>>a[i]; + } + sort(a+1,a+n+1); + for (i=1; i<=n; ++i) + { + for (j=i+1; j=a[mij] && a[i]+a[mij]>=a[j] && a[j]+a[mij]>=a[i]) + st=mij; + else + dr=mij-1; + } + if (a[i]+a[j]>=a[st] && a[i]+a[st]>=a[j] && a[j]+a[st]>=a[i] && st!=i && st!=j) + triunghiuri+=(st-j); + } + } + out< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int ap,n,i,j,k,v[801],st,dr,mid; +int main(){ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1, v+n+1); + for(i=1;iv[i]+v[j]) + dr=mid-1; + mid=(st+dr)/2; + } + ap+=dr-j; + } + + fout< +#include +#include +using namespace std; +int n,v[810],i,j,st,dr,mid,sol; +int main () { + ifstream fin ("nrtri.in"); + ofstream fout ("nrtri.out"); + fin>>n; + for (i=1;i<=n;i++){ + fin>>v[i]; + } + sort (v+1,v+n+1); + for (i=1;i + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMax=805; + +int N,a[NMax],lg,s; + +int main() +{ + + fin >> N; + + for(int i=1;i<=N;i++) + fin >> a[i]; + + sort(a+1,a+1+N); + + for(int i=1;i>=1) + if(poz+lg<=N and a[poz+lg]<=a[i]+a[j]) + poz+=lg; + s+=poz-j; + } + + fout << s; + + return 0; + +} diff --git a/nrtri/binary_search/6952d007-409f-11ec-a56a-dc7196033e92.cpp b/nrtri/binary_search/6952d007-409f-11ec-a56a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc5f76744576f060aa97484e363c312110569083 --- /dev/null +++ b/nrtri/binary_search/6952d007-409f-11ec-a56a-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[801]; + +int main() +{ + int n; + long long s = 0; + in >> n; + for (int i = 1; i <= n; ++i) + in >> v[i]; + sort(v + 1, v + n + 1); + for (int i = 1; i < n; ++i) + { + for (int j = i + 1; j <= n; ++j) + { + int poz, lg; + for (lg = 1; lg <= n; lg <<= 1); + for (poz = 1; lg; lg >>= 1) + { + if (poz + lg <= n && v[poz + lg] <= v[i] + v[j]) + poz += lg; + } + s += poz - j; + } + } + out << s; + return 0; +} diff --git a/nrtri/binary_search/698b931b-409f-11ec-bb59-dc7196033e92.cpp b/nrtri/binary_search/698b931b-409f-11ec-bb59-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20e15625115201a09b258d0c4e8930166f7257f3 --- /dev/null +++ b/nrtri/binary_search/698b931b-409f-11ec-bb59-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +using namespace std; +ifstream si("nrtri.in"); +ofstream so("nrtri.out"); +int v[805]; +int main() +{ + int n; + si>>n; + for(int i=1; i<=n; ++i) { + si>>v[i]; + } + int sol=0; + sort(v+1, v+n+1); + for(int i=1; i<=n; ++i) { + for(int j=i+1; j<=n; ++j) { + int st=j+1, dr=n, mij, rez=j; + while(st<=dr) { + mij=(st+dr)/2; + if(v[mij]<=v[i]+v[j]) { + rez=mij; + st=mij+1; + } + else + dr=mij-1; + } + sol=sol+rez-j; + } + } + so< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,a,st,dr,mij,sol,v[900]; +int main () { + fin>>n; + for (i=1;i<=n;i++) { + fin>>v[i]; + } + sort(v+1, v+n+1); + for (i=1;i<=n-2;i++) { + for (j=i+1;j<=n-1;j++) { + a=v[i]+v[j]; + st=j+1; + dr=n; + while (st<=dr) { + mij=(st+dr)/2; + if (v[mij]>a) + dr=mij-1; + else + st=mij+1; + } + + sol+=dr - j; + } + } + fout< +#include +#include + + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + + +int a[900],r,n; + +int main() +{ + f>>n; + for (int i=1;i<=n;i++) + f>>a[i]; + sort(a+1,a+1+n); + for (int i=1;ia[i]+a[j]) + dr=m-1; + else st=m+1; + } + r+=dr-j; + } + g< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, v[805]; + +int bin(int lo , int hi , int val){ + int initial = lo - 1; + int best = initial; + int mid; + + while( lo <= hi){ + mid = (lo + hi) / 2; + + if(v[mid] > val) + hi = mid - 1; + else{ + lo = mid + 1; + best = mid; + } + } + + return best - initial; +} + +int main(){ + int i, x = 0, j; + f >> n; + for(i = 1 ; i <= n ; i++) + f >> v[i]; + + sort(v + 1, v + n + 1); + + for(i = 1 ; i < n ; i++) + for ( j = i + 1 ; j < n ; j++){ + x += bin(j + 1 , n, v[i] + v[j]); + } + + g << x ; + return 0; +} diff --git a/nrtri/binary_search/6abe6942-409f-11ec-98f2-dc7196033e92.cpp b/nrtri/binary_search/6abe6942-409f-11ec-98f2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02f939a3ae742a4a5ab45d41f5335abb9f80dd4e --- /dev/null +++ b/nrtri/binary_search/6abe6942-409f-11ec-98f2-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +int main() +{ + + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + int n,mij,rez,st=1,dr,v[805],sol=0; + + fin>>n; + + for(int i=1; i<=n; i++) + { + fin>>v[i]; + } + sort(v+1, v+n+1); + + + + for(int i=1; i<=n; i++) + { + for(int j=i+1; j<=n; j++) + { + st=j+1; + dr=n; + rez=j; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[i]+v[j]>=v[mij]) + { + st=mij+1; + rez=mij; + } + else + { + dr=mij-1; + } + } + sol=sol+rez-j; + } + } + + fout< +#include +using namespace std; + +int v[813],i,n; + +int cautare(int a) +{ + int li=1,ls=n,rez=-1,mij; + while(li<=ls) + { + mij=(li+ls)/2; + if(v[mij]<=a) { rez=mij; li=mij+1;} + else ls=mij-1; + } + return rez; + + +} + +int main() +{ + freopen("nrtri.in","r", stdin); + scanf("%d",&n); + for(i=1;i<=n;i++) scanf("%d",&v[i]); + fclose(stdin); + sort(v+1,v+n+1); + int k,contor=0,j; + + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + k=cautare(v[i]+v[j]); + if(k>0) contor+=k-j; + + } + freopen("nrtri.out","w", stdout); + printf("%d",contor); + fclose(stdout); + + return 0; +} diff --git a/nrtri/binary_search/6b914e9d-409f-11ec-a678-dc7196033e92.cpp b/nrtri/binary_search/6b914e9d-409f-11ec-a678-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dd9f221af3c339d78ae59f04bc9d9d8bb3db63e --- /dev/null +++ b/nrtri/binary_search/6b914e9d-409f-11ec-a678-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +int v[813],i,n; + +int cautare(int a) +{ + int li=1,ls=n,rez=-1,mij; + while(li<=ls) + { + mij=(li+ls)/2; + if(v[mij]<=a) { rez=mij; li=mij+1;} + else ls=mij-1; + } + return rez; + + +} + +int main() +{ + freopen("nrtri.in","r", stdin); + scanf("%d",&n); + for(i=1;i<=n;i++) scanf("%d",&v[i]); + fclose(stdin); + sort(v+1,v+n+1); + int k,contor=0,j; + + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + k=cautare(v[i]+v[j]); + if(k>0) contor+=k-j; + + } + freopen("nrtri.out","w", stdout); + printf("%d",contor); + fclose(stdout); + + return 0; +} diff --git a/nrtri/binary_search/6bc24faa-409f-11ec-891e-dc7196033e92.cpp b/nrtri/binary_search/6bc24faa-409f-11ec-891e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9880400e733ffb7e3db8c40ae1156ae7de7af74 --- /dev/null +++ b/nrtri/binary_search/6bc24faa-409f-11ec-891e-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[802]; +int main() +{ + int n, i, j, st, dr, mij, s=0; + fin >> n; + for(i=1; i<=n; i++) + fin >>v[i]; + sort(v+1, v+n+1); + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + { + st=j+1; dr=n; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]<=v[i]+v[j]) st=mij+1; + else dr=mij-1; + } + s=s+st-1-j; + } + fout << s; + return 0; +} diff --git a/nrtri/binary_search/6bf5dbb3-409f-11ec-9352-dc7196033e92.cpp b/nrtri/binary_search/6bf5dbb3-409f-11ec-9352-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb7144bbac413d05ee72d2e3a36e9d4efc025075 --- /dev/null +++ b/nrtri/binary_search/6bf5dbb3-409f-11ec-9352-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, v[805], i, j, raspuns; + +int cautareBinara(int j) { + int stanga = j + 1, dreapta = n, t = v[i] + v[j]; + while (stanga <= dreapta) { + int mijloc = (stanga + dreapta) / 2; + if (t < v[mijloc]) + dreapta = mijloc - 1; + else + stanga = mijloc + 1; + } + return raspuns + dreapta - j; +} + +int main() { + + f >> n; + for (int i = 1; i <= n; i++) + f >> v[i]; + + sort(v + 1, v + n + 1); + + for (i = 1; i < n; i++) + for (j = i + 1; j <= n; j++) + raspuns = cautareBinara(j); + + g << raspuns << " "; + + return 0; +} diff --git a/nrtri/binary_search/6c553569-409f-11ec-ae9c-dc7196033e92.cpp b/nrtri/binary_search/6c553569-409f-11ec-ae9c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142afc963dbb781d2edc8cd8911ff64e0a33e9ed --- /dev/null +++ b/nrtri/binary_search/6c553569-409f-11ec-ae9c-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int v[802]; +int main() +{ + int n,sol = 0; + cin >> n; + for (int i =1; i<= n; ++i) + { + cin >> v[i]; + } + sort (v + 1,v + n + 1); + for (int i =1; i<= n; ++i) + { + int k = v[i]; + for (int j = i + 1; j <= n; ++j) + { + int s = v[j]; + int st = j + 1; + int dr = n; + int h = j; + while (st <= dr) + { + int mid = (st + dr) / 2; + if (k + s >= v[mid]) + { + st = mid + 1; + h = mid; + } + else + dr = mid - 1; + } + sol += h - j; + } + } + cout << sol; + return 0; +} diff --git a/nrtri/binary_search/6c865880-409f-11ec-96d8-dc7196033e92.cpp b/nrtri/binary_search/6c865880-409f-11ec-96d8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dd9f221af3c339d78ae59f04bc9d9d8bb3db63e --- /dev/null +++ b/nrtri/binary_search/6c865880-409f-11ec-96d8-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +int v[813],i,n; + +int cautare(int a) +{ + int li=1,ls=n,rez=-1,mij; + while(li<=ls) + { + mij=(li+ls)/2; + if(v[mij]<=a) { rez=mij; li=mij+1;} + else ls=mij-1; + } + return rez; + + +} + +int main() +{ + freopen("nrtri.in","r", stdin); + scanf("%d",&n); + for(i=1;i<=n;i++) scanf("%d",&v[i]); + fclose(stdin); + sort(v+1,v+n+1); + int k,contor=0,j; + + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + k=cautare(v[i]+v[j]); + if(k>0) contor+=k-j; + + } + freopen("nrtri.out","w", stdout); + printf("%d",contor); + fclose(stdout); + + return 0; +} diff --git a/nrtri/binary_search/6cb95462-409f-11ec-95c1-dc7196033e92.cpp b/nrtri/binary_search/6cb95462-409f-11ec-95c1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a930d893ac0d7367d89ab9566d5a383833487811 --- /dev/null +++ b/nrtri/binary_search/6cb95462-409f-11ec-95c1-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,v[801]; + +int cautbin(int x){ + int p = 1, u = n, poz=0; + while(p <= u) + { + int m = (p + u) / 2; //m=p+(u-p)/2; + if(x>=v[m]){ + poz=m; + p=m+1; + } + else u=m-1; + } + return poz; +} +int main() +{ + f>>n; + for(int i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + int nr=0; + for(int i=1;iv[poz] + int poz=cautbin(v[i]+v[j]); + nr+=(poz-j); ///Cate pozitii avem intre j+1 si poz; + } + g< +using namespace std; +ifstream f ( "nrtri.in" ); ofstream g ( "nrtri.out" ); +int main() +{ int sol=0,n,v[100006]={0}; + f>>n; + for(int i=1;i<=n;i++) f>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i=1 and p<=n) sol+=(p-j); + } + g< +#include +#define NMax 800 +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int k,n,i,j,s,d,m,nrtri; +bool ok=1; +int lung[NMax+1]; +int main() +{ + f>>n; + for(i=1; i<=n; i++) + f>>lung[i]; + + for(i=1;ilung[j+1]) + swap(lung[j],lung[j+1]),ok=0; + + if(ok==1) + i=n; + } + + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + s=j; + d=n; + k=j; + while(s<=d) + { + m=s+(d-s)/2; + + if(lung[i]+lung[j]>=lung[m]) + k=m,s=m+1; + + else + d=m-1; + } + nrtri+=k-j; + } + g< +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, st, dr, mij, v[1001], t, u; +int main() +{ + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1, v+n+1); + for(int i=1; i +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +short n,b[801]; +int sol; +int main() +{ + f>>n; + for(int i=1;i<=n;++i) + f>>b[i]; + sort(b+1,b+n+1); + for(int i=1;i +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, l[804]; + +int cautare(int st, int dr) { + int p = st, P = dr; + while (st < dr) { + int mid = (st + dr) / 2; + if (l[mid] + l[p] >= l[P]) { + dr = mid; + } + else { + st = mid + 1; + } + } + if (st == P) return 0; + if (st == p) return P - p - 1; + return P - st; +} + + +int main() { + + fin >> n; + for (int i = 1; i <= n; ++i) { + fin >> l[i]; + } + + sort(l+1, l + n + 1); + + int nt = 0; + for (int i = 1; i < n - 1; ++i) { + for (int j = i + 2; j <= n; ++j){ + nt += cautare(i, j); + } + } + + fout << nt; + + + + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/6f69a9f6-409f-11ec-b4eb-dc7196033e92.cpp b/nrtri/binary_search/6f69a9f6-409f-11ec-b4eb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e8562aa5a82f247520dec9b8ad7b7efe1a75515 --- /dev/null +++ b/nrtri/binary_search/6f69a9f6-409f-11ec-b4eb-dc7196033e92.cpp @@ -0,0 +1,32 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,x,sol,st,dr,mid; +int v[805]; +int main(){ + fin>>n; + for(i=1;i<=n;i++){ + fin>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++){ + for(j=i+1;j<=n-1;j++){ + st=j+1; + dr=n; + while(st<=dr){ + mid=(st+dr)/2; + if(v[mid]>v[i]+v[j]) + dr=mid-1; + else + st=mid+1; + } + if(dr>j){ + sol+=(dr-j); + } + } + } + fout< +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, st, dr, mij, v[1001], t, u; +int main() +{ + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1, v+n+1); + for(int i=1; i +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, st, dr, mij, v[1001], t, u; +int main() +{ + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1, v+n+1); + for(int i=1; i +#include +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int main() +{ + int n,nr=0,mid,s; + in>>n; + int v[9002]={0}; + for (int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for (int i=1;i<=n-2;i++) + for (int k=i+1;k<=n-1;k++) + { + s=v[i]+v[k]; + if (v[k+1]<=s) + { + + int left=k+1; + int right=n; + while (left<=right) + { + mid =(right+left)/2; + if (v[mid]<=s && lefts) + right=mid-1; + if (right==n and left==n) + right--; + } + nr+=left-k; + if (v[left]>s) + nr--; + + } + } + out< +#include +#include + +using namespace std; +int v[801]; +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,mij,st,dr,i,y,x,cnt=0,j,cap1,cap2; + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",&v[i]); + sort(v+1,v+n+1); + for(i=1;i=y && y+v[mij]>=x) + if(mij>cap1) + cap1=mij; + } + } + if(cap1!=0) + { + st=j+1; + dr=n; + cap2=801; + while(st<=dr){ + mij=(st+dr)/2; + if(x+v[mij]=v[mij]) + if(mij +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, l[804]; + +int cautare(int st, int dr) { + int p = st, P = dr; + while (st < dr) { + int mid = (st + dr) / 2; + if (l[mid] + l[p] >= l[P]) { + dr = mid; + } + else { + st = mid + 1; + } + } + if (st == P) return 0; + if (st == p) return P - p - 1; + return P - st; +} + + +int main() { + + fin >> n; + for (int i = 1; i <= n; ++i) { + fin >> l[i]; + } + + sort(l+1, l + n + 1); + + int nt = 0; + for (int i = 1; i < n - 1; ++i) { + for (int j = i + 2; j <= n; ++j){ + nt += cautare(i, j); + } + } + + fout << nt; + + + + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/7133cb14-409f-11ec-9f84-dc7196033e92.cpp b/nrtri/binary_search/7133cb14-409f-11ec-9f84-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..52433fb4668d97bb5cc5c5c12102e6a79cbd8f7b --- /dev/null +++ b/nrtri/binary_search/7133cb14-409f-11ec-9f84-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[810],n,i,j,st,dr,sol; +int main(){ + fin>>n; + for (i=1;i<=n;i++) + fin>>v[i]; + + sort(v+1, v+n+1); + + for (i=1;iv[i]+v[j]) + dr=mid-1; + else + st=mid+1; + } + sol += dr-j; + } + fout< + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +long long n,o,v[30000],i,m,cnt,j,nrcautat; + +int cautbin(int s,int j) +{ int st=j+1,dr=n,poz=0; + while(st<=dr) + { + int mij=(st+dr)/2; + if(v[mij]<=s) + { + poz=mij; + st=mij+1; + } + else + dr=mij-1; + } + return poz; +} + +int main() +{ + f>>n; + for(i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-1;i++) + for(j=i+1;j<=n;j++) + if(cautbin(v[i]+v[j],j)) + nrcautat+=cautbin(v[i]+v[j],j)-j; + g< +#include +using namespace std; +int v[800]; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int cautbin(int val, int marime) +{ + int pos =0 ; + for(int msk=1<<9; msk>0; msk/=2) + if(msk+pos<=marime && v[msk+pos]<=val) + pos += msk; + + return pos; +} +int main() +{ + int n, nrt=0, nr, k; + + in >> n; + + for(int i=1; i<=n; i++) + in >> v[i]; + + sort(v+1, v+n+1); + + for(int i=1; i<=n; i++) + { + for(int j=i+1; j<=n; j++) + { + nr = cautbin(v[i] + v[j], n); + if(nr>j) + nrt += nr-j; + } + } + out << nrt; + return 0; +} diff --git a/nrtri/binary_search/7242b773-409f-11ec-b074-dc7196033e92.cpp b/nrtri/binary_search/7242b773-409f-11ec-b074-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..797773057bfca42853d051369fe438c0a887c1d3 --- /dev/null +++ b/nrtri/binary_search/7242b773-409f-11ec-b074-dc7196033e92.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[805]; + +int main() +{ + int n; + fin >> n; + + for(int i = 1; i <= n; ++i) + fin >> v[i]; + + sort(v + 1, v + n + 1); + + int rez = 0; + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j) { + int st = 1; + int dr = n; + int poz = -1; + while(st <= dr) { + int mij = (st + dr) / 2; + + if(v[mij] <= v[i] + v[j]){ + poz = mij; + st = mij + 1; + } + else dr = mij - 1; + } + + st = 1; + dr = n; + int poz2 = -1; + while(st <= dr) { + int mij = (st + dr) / 2; + + if(v[mij] >= v[j] - v[i]){ + poz2 = mij; + dr = mij - 1; + } + else st = mij + 1; + } + + if(poz != -1 && poz2 != -1){ + rez += (poz - poz2 + 1); + if(i <= poz && i >= poz2) + --rez; + if(j <= poz && j >= poz2) + --rez; + } + else if(poz != -1){ + rez += poz; + if(i <= poz) + --rez; + if(j <= poz) + --rez; + } + + } + fout << rez / 3 << '\n'; + return 0; +} diff --git a/nrtri/binary_search/7272d46a-409f-11ec-b71e-dc7196033e92.cpp b/nrtri/binary_search/7272d46a-409f-11ec-b71e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69416e84dd45d71b7804fa0b737c0dac31dd04fb --- /dev/null +++ b/nrtri/binary_search/7272d46a-409f-11ec-b71e-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include + +#include + +#include + +using namespace std; + +const int NMAX = 1000; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[1001],n; +int cautbin(int x) +{ + int p=1,u=n,poz=0; + while(p<=u) + { + int m=(p+u)/2; + if(x>=v[m]) + { + poz=m; + p=m+1; + } + else + u=m-1; + } + return poz; +} +int main() +{ + int nr=0; + in >> n; + for(int i=1; i<=n; i++) + in >> v[i]; + sort(v+1,v+n+1); + for(int i=1; i +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,st,dr,mid,v[1010],sol,rasp; +int main(){ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,v[805]; +int Cautare_Binara(int s,int j) +{ int st=j+1,dr=n,poz=0; + while(st<=dr) + { int mij=(st+dr)/2; + if(v[mij]<=s) + { poz=mij; + st=mij+1; + } + else dr=mij-1; + } + return poz; +} +int main() +{ f>>n; + int nrap=0; + for(int i=1; i<=n; i++) f>>v[i]; + sort(v+1,v+n+1); + for(int i=1; i +#include +#include +#define L 9 + +using namespace std; +int v[801], n; + +int caut( int x ){ + int r = 0, pas = 1 << L; + while ( pas != 0 ){ + if ( r + pas <= n && v[r + pas] <= x ){ + r += pas; + } + pas /= 2; + } + return r; +} + +int main(){ + FILE *fin, *fout; + fin = fopen( "nrtri.in", "r" ); + fout = fopen( "nrtri.out", "w" ); + int i, nrt, k, j; + fscanf( fin, "%d", &n ); + for ( i = 1; i <= n; i ++ ){ + fscanf( fin, "%d", &v[i] ); + } + sort( v + 1, v + n + 1 ); + /* + for ( i = 1; i <= n; i ++ ){ + fprintf( fout, "%d ", v[i] ); + } + */ + nrt = 0; + for ( i = 1; i <= n; i ++ ){ + for ( j = i + 1; j <= n; j ++ ){ + k = caut( v[i] + v[j] ); + //fprintf(fout, "%d %d %d\n", i, j, k); + if (k > j) + nrt += k - j; + } + } + fprintf( fout, "%d", nrt ); + fclose( fin ); + fclose( fout ); + return 0; +} diff --git a/nrtri/binary_search/74423062-409f-11ec-bc58-dc7196033e92.cpp b/nrtri/binary_search/74423062-409f-11ec-bc58-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc3272c50ac7ac656fb34dbf0179b20fa9c1b867 --- /dev/null +++ b/nrtri/binary_search/74423062-409f-11ec-bc58-dc7196033e92.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using namespace std; +const int NMAX = 1000; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int v[NMAX+1],n; + +int cautbin(int x) +{ + int p=1,u=n,poz=0; + while(p<=u) + { + int m=(p+u)/2; + if(x>=v[m]) + { + poz=m; + p=m+1; + } + else + u=m-1; + } + return poz; +} + +int main() +{ + int nr=0; + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(int i=1; iv[poz] + int poz =cautbin(v[i]+v[j]); + nr+=poz-j;///cate poz intre j+1 si poz + } + g< +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +const int NMAX = 1000; +int v[NMAX + 1]; +int n; +int cautbin(int x) +{ + int p=1,u=n,poz=0; + while(p<=u) + { + int m=(p+u)/2; + if(x>=v[m]) + { + poz=m;p=m+1; + } + else u=m-1; + } + return poz; +} +int main() +{ + int nr = 0; + f >> n; + for(int i = 1; i <= n; i++) + f >> v[i]; + sort(v+ 1, v + n + 1); + for(int i = 1; i < n - 1; i++) + for(int j = i + 1; j < n ; j++) + { + int poz = cautbin(v[i] + v[j]); + nr += poz - j; + } + g << nr; + return 0; +} diff --git a/nrtri/binary_search/74abd68d-409f-11ec-9005-dc7196033e92.cpp b/nrtri/binary_search/74abd68d-409f-11ec-9005-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e546207c95db3b1e8d55abf00ba11f99b9ac3941 --- /dev/null +++ b/nrtri/binary_search/74abd68d-409f-11ec-9005-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +int v[10005]; +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,i,j; + scanf("%d",&n); + for(i=1;i<=n;i++)scanf("%d",&v[i]); + sort(v+1,v+n+1); + int sum=0; + for(i=1;i(v[i]+v[j]))dr=med-1; + else + st=med+1; + } + sum+=dr-j; + } + printf("%d\n",sum); + return 0; +} diff --git a/nrtri/binary_search/76044e43-409f-11ec-9982-dc7196033e92.cpp b/nrtri/binary_search/76044e43-409f-11ec-9982-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2995bdee6c4be19cff6bf966defdc07a08304b43 --- /dev/null +++ b/nrtri/binary_search/76044e43-409f-11ec-9982-dc7196033e92.cpp @@ -0,0 +1,209 @@ +#include + + + +#include + + + +#include + + + +using namespace std; + + + + + + + +ifstream in("nrtri.in"); + + + +ofstream out("nrtri.out"); + + + + + + + +int BinarySearch(int low,int high,int k,int v[16001]) + + + +{ + + + + int mid,result=-1; + + + + for(; low<=high;) + + + + { + + + + mid=(low+high)/2; + + + + if(v[mid]<=k) + + + + { + + + + if(mid>result) + + + + result=mid; + + + + low=mid+1; + + + + } + + + + else + + + + high=mid-1; + + + + + + + + } + + + + return result; + + + + + + + +} + + + + + + + +int main() + + + +{ + + + + int v[801]; + + + + int i,j,p,n; + + + + int x,y,z; + + + + int sum=0; + + + + in>>n; + + + + for(i=0;i>v[i]; + + + + } + + + + sort(v,v+n); + + + + for(i=0;i + +#include + +#include + +using namespace std; + + + +ifstream in("nrtri.in"); + +ofstream out("nrtri.out"); + + + +int BinarySearch(int low,int high,int k,int v[16001]) + +{ + + int mid,result=-1; + + for(; low<=high;) + + { + + mid=(low+high)/2; + + if(v[mid]<=k) + + { + + if(mid>result) + + result=mid; + + low=mid+1; + + } + + else + + high=mid-1; + + + + } + + return result; + + + +} + + + +int main() + +{ + + int v[801]; + + int i,j,p,n; + + int x,y,z; + + int sum=0; + + in>>n; + + for(i=0;i>v[i]; + + } + + sort(v,v+n); + + for(i=0;i +#include + +#define input "nrtri.in" +#define output "nrtri.out" +#define NMAX 805 + +using namespace std; + +ifstream in(input); +ofstream out(output); + +int N, dist[NMAX]; + +void Read_Data() +{ + in >> N; + for (int i = 1; i <= N; i++) + in >> dist[i]; + sort(dist + 1, dist + 1 + N); +} + +void Solve() +{ + int counter = 0; + for (int i = 1; i <= N - 2; i++) + for (int j = i + 1; j <= N - 1; j++) + { + // Cautam printre valori cea mai mare valoare strict mai mica sau egala cu suma primelor 2 + int st = j + 1, dr = N; + int p = -1; + while (st <= dr) + { + int middle = (st + dr) / 2; + if (dist[i] + dist[j] >= dist[middle]) + p = middle, st = middle + 1; + else dr = middle - 1; + } + if (p != -1) counter = counter + p - j; + + } + out << counter; +} + +int main() +{ + Read_Data(); + Solve(); + return 0; +} + diff --git a/nrtri/binary_search/7b50a916-409f-11ec-bf4d-dc7196033e92.cpp b/nrtri/binary_search/7b50a916-409f-11ec-bf4d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ccac9a8f2fbf4d10b79b5932c891ece57d58e60 --- /dev/null +++ b/nrtri/binary_search/7b50a916-409f-11ec-bf4d-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include + +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int n,v[805],ans,st,dr,mij,sum,poz; +void read() +{ + fin>>n; + for(int i=1;i<=n;i++) fin>>v[i]; + sort(v+1,v+1+n); +} +void solve() +{ + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { + sum=v[i]+v[j]; + st=j+1;dr=n;poz=-1; + while(st<=dr) + { + mij=st+(dr-st)/2; + if(v[mij]>sum) + { + dr=mij-1; + } + else { + poz=mij; + st=mij+1; + } + } + if(poz!=-1) ans+=poz-j; + } + fout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,v[802],i,j,sum,m,st,dr,nr; +int main() +{ + fin>>n; + for(i=1;i<=n;i++)fin>>v[i]; + sort(v+1, v+n+1); + + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++){ + sum=v[i]+v[j]; + st=1; dr=n; while(st<=dr){m=(st+dr)/2; + if(v[m]sum)dr=m-1; + else if(v[m]==sum){st=m; break;} + } + while(v[st]==sum)st++; st--; + nr+=st-j; + } + fout< +#include +#include +using namespace std; +ofstream fo("nrtri.out"); +ifstream fi("nrtri.in"); + + +int n,trig[900]; +long long sol; +int cautbin(int cautat) +{ + int poz=0; + + for(int i=19;i>=0;i--) + { + if(poz+(1<>n; + for(int i=1;i<=n;i++) + fi>>trig[i]; + + sort(trig+1,trig+n+1); + + for(int i=1;i=trig[k]) + sol++; + else + break; + + } + + + + fo< +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int v[805]; +int main() +{ + int n,a,cnt=0; + cin >> n; + for(int i=0; i> a; + v[i]=a; + } + sort(v,v+n); + for(int m=0; mv[m]+v[k]) + dr=mij-1; + else + { + st=mij+1; + last=mij; + } + } + cnt+=last-k; + } + } + cout << cnt; + return 0; +} diff --git a/nrtri/binary_search/8110cd12-409f-11ec-a94c-dc7196033e92.cpp b/nrtri/binary_search/8110cd12-409f-11ec-a94c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b84d03dc32a55e1c5762b4ec5e532d5bff19326 --- /dev/null +++ b/nrtri/binary_search/8110cd12-409f-11ec-a94c-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +using namespace std; + +int plz[1005]; +int given[1005]; + +bool cmp(int x, int y){ + return x < y; +} + +int main(){ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + + int n; + scanf("%d", &n); + assert(n <= 800); + plz[1] = 1; + given[1000] = 133742; + + for(int i = 1; i <= n; ++i) + scanf("%d", &given[i]); + + sort(given + 1, given + n + 1, cmp); + + int ans = 0; + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j){ + assert(j <= n); + assert(i <= n); + int left = j + 1, right = n, mid, last = j; + while(left <= right){ + mid = (left + right) >> 1; + assert(mid <= n); + if(given[i] + given[j] >= given[mid]){ + last = mid; + left = mid + 1; + } + else + right = mid - 1; + } + ans += last - j; + } + + printf("%d", ans); + + return 0; +} diff --git a/nrtri/binary_search/82124d3a-409f-11ec-ac1e-dc7196033e92.cpp b/nrtri/binary_search/82124d3a-409f-11ec-ac1e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6866a162e97559070a7edc23b2278ac939cb3233 --- /dev/null +++ b/nrtri/binary_search/82124d3a-409f-11ec-ac1e-dc7196033e92.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[805],i,n,j; +int nrtri=0; +void Citire() +{ + fin>>n; + for(i=1; i<=n; i++) + fin>>v[i]; +} + +int Cautare(int sum,int i) +{ + bool ok=false; + int poz=-1; + int Left=i+1; + int Right=n; + int Mid; + while(Left<=Right) + { + Mid=(Left+Right)/2; + if(sum>=v[Mid]) + { + ok=true; + poz=Mid-i; + } + if(ok) + { + Left=Mid+1; + ok=false; + } + else + { + if(sumv[Mid]) + Left=Mid+1; + } + } + if(poz==-1) + return 0; + else + return poz; +} + +int main() +{ + int suma=0; + Citire(); + sort(v+1,v+n+1); + if(n==1) + { + fout<<0; + return 0; + } + if(n==2) + { + fout<<0; + return 0; + } + for(i=1; i +#include + +using namespace std; + +int n, v[810]; +int ans = 0; + +void Read() +{ + ifstream fin("nrtri.in"); + fin >> n; + for (int i = 1;i <= n;++i) + fin >> v[i]; + sort(v + 1, v + n + 1); + fin.close(); +} + +int BinarySearch(int x, int y, int val) +{ + int left = x, right = y, mid, poz = x - 1; + while (left <= right) + { + mid = (left + right) / 2; + if (v[mid] <= val) + { + poz = mid; + left = mid + 1; + } + else + right = mid - 1; + } + return poz; +} + +void Solve() +{ + int poz; + for (int i = 1;i <= n - 2;++i) + for (int j = i + 1;j <= n - 1;++j) + { + poz = BinarySearch(j + 1, n, v[i] + v[j]); + ans += poz - j; + } +} + +void Write() +{ + ofstream fout("nrtri.out"); + fout << ans << "\n"; + fout.close(); +} + +int main() +{ + Read(); + Solve(); + Write(); + return 0; +} diff --git a/nrtri/binary_search/82c0f994-409f-11ec-bd1d-dc7196033e92.cpp b/nrtri/binary_search/82c0f994-409f-11ec-bd1d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..560bf92d46326100b712e9253350c23cc254b79c --- /dev/null +++ b/nrtri/binary_search/82c0f994-409f-11ec-bd1d-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,a[801],i,j,st,dr,mij,val,nr,s; +int main() +{ + in>>n; + for(i=1; i<=n; i++) + { + in>>a[i]; + } + sort(a+1,a+n+1); + for(i=1; i<=n-2; i++) + { + for(j=i+1; j<=n-1; j++) + { + st=j+1; + dr=n; + s=a[i]+a[j]; + val=j; + while(st<=dr) + { + mij=(st+dr)/2; + if(s>=a[mij]) + { + val=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + nr=nr+val-j; + } + } + out< +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[801]; + +int cautare(int x,int poz1,int poz2) +{ + + int stg=poz1,dr=poz2,mij; + while(stg<=dr) + + { + mij=(stg+dr)/2; + if(v[mij]>x) dr=mij-1; + else stg=mij+1; + } + + return dr; +} + +int main() + +{ + + int n,rez=0,cb; + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i]; + + sort(v+1,v+n+1); + + for(int i=1;i<=n-2;i++) + + for(int j=i+1;j<=n-1;j++) + + { + + cb=cautare(v[i]+v[j],j+1,n); + + if(cb>j) + + rez+=cb-j; + + } + + fout< +#include +using namespace std; +int v[801]; +int main (){ + freopen ("nrtri.in","r",stdin); + freopen ("nrtri.out","w",stdout); + int i,n,j,s,l1,l2,m,poz,nr=0; + scanf ("%d",&n); + for (i=1;i<=n;i++) + scanf ("%d",&v[i]); + sort(v+1,v+n+1); + for (i=1;i<=n;i++) + for (j=i+1;j<=n;j++){ + s=v[i]+v[j]; + l1=1; + l2=n; + while (l1<=l2){ + m=(l1+l2)/2; + if (v[m]>s) + l2=m-1; + else{ + poz=m; + l1=m+1; + } + } + nr=nr+poz-j; + } + printf ("%d",nr); + return 0; +} diff --git a/nrtri/binary_search/83640f28-409f-11ec-a193-dc7196033e92.cpp b/nrtri/binary_search/83640f28-409f-11ec-a193-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c18fadf668b3d5a5a036f8210f1a820bb667d57 --- /dev/null +++ b/nrtri/binary_search/83640f28-409f-11ec-a193-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[805],i,n,j,cont,st,dr,mij,s,val; +int main() +{ + f>>n; + for(i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + s=v[i]+v[j]; + val=j; + while(st<=dr) + { + mij=(st+dr)/2; + if(s>=v[mij]) + { + st=mij+1; + val=mij; + } + else + { + dr=mij-1; + } + } + cont=cont+val-j; + } + } + g< +#include + +using namespace std; + +int n, v[810]; +int ans = 0; + +void Read() +{ + ifstream fin("nrtri.in"); + fin >> n; + for (int i = 1;i <= n;++i) + fin >> v[i]; + sort(v + 1, v + n + 1); + fin.close(); +} + +int BinarySearch(int x, int y, int val) +{ + int left = x, right = y, mid, poz = x - 1; + while (left <= right) + { + mid = (left + right) / 2; + if (v[mid] <= val) + { + poz = mid; + left = mid + 1; + } + else + right = mid - 1; + } + return poz; +} + +void Solve() +{ + int poz; + for (int i = 1;i <= n - 2;++i) + for (int j = i + 1;j <= n - 1;++j) + { + poz = BinarySearch(j + 1, n, v[i] + v[j]); + ans += poz - j; + } +} + +void Write() +{ + ofstream fout("nrtri.out"); + fout << ans << "\n"; + fout.close(); +} + +int main() +{ + Read(); + Solve(); + Write(); + return 0; +} diff --git a/nrtri/binary_search/840ab848-409f-11ec-87bc-dc7196033e92.cpp b/nrtri/binary_search/840ab848-409f-11ec-87bc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a13a5e221b3bc05412f44676c5146ce4cb3f2954 --- /dev/null +++ b/nrtri/binary_search/840ab848-409f-11ec-87bc-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int n,nr,l[830],s,d; +int caut(int st,int dr,int limdr); +int main() +{ + cin>>n; + for(int i=1;i<=n;i++) + cin>>l[i]; + sort(l+1,l+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;js);///imposibil phew + nr+=(caut(j+1,n,s)); + } + cout< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[801]; + +int cautBin(int sum, int st, int dr) +{ + int mij; + while (st <= dr) + { + mij = (st + dr) / 2; + if (v[mij] > sum) + dr = mij - 1; + else + st = mij + 1; + } + return dr; +} + +int main() +{ + int n, i, j, x, s = 0; + in >> n; + for (i = 1; i <= n; ++i) + in >> v[i]; + sort(v + 1, v + n + 1); + for (i = 1; i < n; ++i) + { + for (j = i + 1; j < n; ++j) + { + x = cautBin(v[i] + v[j], j + 1, n); + if (x > j) + s += x - j; + } + } + out << s; + return 0; +} diff --git a/nrtri/binary_search/846a36e6-409f-11ec-85ad-dc7196033e92.cpp b/nrtri/binary_search/846a36e6-409f-11ec-85ad-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f1827b0549b8e7a9a761b298aea8a28739dd5ea --- /dev/null +++ b/nrtri/binary_search/846a36e6-409f-11ec-85ad-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[802],n; +int cautbin(int x) +{ + int in=1,sf=n,poz=-1; + while(in<=sf) + { + int mij=(in+sf)/2; + if(x>=v[mij]) + { + poz=mij; + in=mij+1; + } + else sf=mij-1; + } + return poz; +} +int main() +{ + int i,j,h,nr=0; + f>>n; + for(i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + { + int a=v[i]; + for(j=i+1; j<=n-1; j++) + { + int s=a+v[j]; + int poz = cautbin(s); + nr += (poz - j); + } + } + g< + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,a[803]; + +void Citire() +{ + int i; + fin>>n; + for(i=1;i<=n;i++) + fin>>a[i]; +} +int CautBin(int x) +{ + int st,dr,mij,poz; + poz = 0; + st = 1; + dr = n; + while(st <= dr) + { + mij = (st + dr) / 2; + if(x >= a[mij]) + { + poz = mij; + st = mij + 1; + } + else dr = mij - 1; + } + return poz; +} + +int cautbin(int x) +{ + int p=1,u=n,poz=0; + while(p<=u) + { + int mij=(p+u)/2; + if(x>=a[mij]) + { + poz=mij; + p=mij+1; + } + else u=mij-1; + + } + return poz; +} + + +void Rezolvare() +{ + int i,j,sol; + sol = 0; + sort(a+1,a+n+1); + for(i=1;i +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +const int MAX = 8e2 + 5; + +int n, answer; +int v[MAX]; + +int Binary_Search(int i, int j) +{ + int r = 0, pas = 1 << 15; + + while(pas) + { + if(r + pas <= n && v[r + pas] <= v[i] + v[j]) + r+=pas; + pas/=2; + } + return r; +} + +int main() +{ + f >> n; + for(int i = 1; i <= n; ++i) + f >> v[i]; + sort(v + 1, v + n + 1); + + for(int i = 1; i <= n - 1; ++i) + for(int j = i + 1; j <= n; ++j) + answer += Binary_Search(i, j) - j; + + g << answer << "\n"; + return 0; +} diff --git a/nrtri/binary_search/85a8da30-409f-11ec-881b-dc7196033e92.cpp b/nrtri/binary_search/85a8da30-409f-11ec-881b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..920c153ed8361f6c4675b2baf1c1021033342f49 --- /dev/null +++ b/nrtri/binary_search/85a8da30-409f-11ec-881b-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +using namespace std; +vector v; +int cautare_binara(int l,int r,int nr) +{ + int mij; + while(lnr)//daca v[mij]>nr + { + mij--;//il scadem pe mij, pana ajungem la un element care sa convina + //proprietatii de triunghi + } + return mij; +} +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n,i,j,s_laturi,pos,nr,counter=0; + fin>>n;//citim numarul de laturi + for(i=0;i<=n-1;i++) + { + fin>>nr;//citim elementele + v.push_back(nr);//le introducem in v prin push_back, ca sa isi pastreze ordinea + } + sort(v.begin(),v.end());//le sortam prin functia sort din STL + for(i=0;i<=n-2;i++) + {for(j=i+1;j<=n-1;j++) + { + s_laturi=v[i]+v[j];//punem in s_laturi suma a doua elemente v[i] si v[j] + pos=cautare_binara(j+1,n-1,s_laturi);//il "cautam" pe al treilea element care sa corespunda + //si care sa verifice proprietatea de triunghi + counter=counter+pos-j;//in counter adunam pos, adica numarul de posibilitati pentru al treilea element din triplet + //din caare scadem j,indicele elementului al doilea ca marime din triplet + } + } + fout< +#define N 805 +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int a[N],n,ok; + +int Pivotare(int s,int d) +{ int i,j,pasi,pasj,aux; + i=s; j=d; pasi=0; pasj=1; + while(ia[j]) + { aux=a[i]; a[i]=a[j]; a[j]=aux; + pasi=1-pasi; + pasj=1-pasj; + } + i=i+pasi; + j=j-pasj; + } + return i; +} + +void QS(int s,int d) +{ if(sd) + { if(ok==0) return d; + else return d; + } + else{ int mij=(s+d)/2; + if(x==a[mij]) {ok=1; return CB(mij+1,d,x);} + else if(x>n; + for(i=1;i<=n;i++) + fin>>a[i]; + QS(1,n); + for(i=1;i +#include +#include +using namespace std; + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + +long n; +vector v; + +long cautare_binara(long start, long stop, long numar){ + long mij = (start + stop) / 2; + while(start <= stop){ + if(v[mij] <= numar){ + start = mij + 1; + } + else{ + stop = mij - 1; + } + mij = (start + stop) / 2; + } + + return mij; +} + +int main() +{ +f>>n; +long x; +for(long i = 0 ; i < n ; i++){ + f>>x; + v.push_back(x); +} +sort(v.begin(), v.end()); + +long sol = 0; + +for(long i = 0 ; i < n - 2 ; i++){ + for(long j = i + 1 ; j < n - 1 ; j++){ + long sum = v[i] + v[j]; + long poz = cautare_binara(j + 1, n - 1, sum); + if(sum >= v[poz] && poz > j) + //g< +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout ("nrtri.out"); +int n, v[801], k; + +void citire() +{ + fin >> n; + for (int i = 0; i < n; ++i) + fin >> v[i]; +} + +int cautare_binara(int x, int s) +{ + int st = s, dr = n - 1; + while (st <= dr) + { + int mid = (st + dr) / 2; + if (v[mid] <= x) + st = mid + 1; + else + dr = mid - 1; + } + return st; +} + +void rezolvare() +{ + sort(v, v + n); + for (int i = 0; i < n - 1; ++i) + for (int j = i + 1; j < n; ++j) + k += cautare_binara(v[i] + v[j], j + 1) - j - 1; + fout << k; +} + +int main() +{ + citire(); + rezolvare(); + return 0; +} diff --git a/nrtri/binary_search/87209438-409f-11ec-bf95-dc7196033e92.cpp b/nrtri/binary_search/87209438-409f-11ec-bf95-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f68a931fbd128bf682666b8dfb6141af460715b8 --- /dev/null +++ b/nrtri/binary_search/87209438-409f-11ec-bf95-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[805],n; +int cautbin(int x) +{ + int p=1,u=n,poz ; + while(p<=u) + { + int mij=(p+u)/2; + if(x>=v[mij]) + { + poz=mij; + p=mij+1; + } + else u=mij-1; + + } + return poz; +} +int main() +{ + int nr=0; + f>>n; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(int i=1; i +#include + +using namespace std; + +int bete[1000], n; + +int cautbindr(int val) +{ + int st, dr, med, last; + st = 1; + dr = n; + while(st <= dr) + { + med = (st + dr) >> 1; + if(bete[med] <= val) + { + last = med; + st = med + 1; + } + else + dr = med - 1; + } + return last; +} + +int cautbinst(int val) +{ + int st, dr, med, last; + st = 1; + dr = n; + while(st <= dr) + { + med = (st + dr) >> 1; + if(bete[med] >= val) + { + last = med; + dr = med - 1; + } + else + st = med + 1; + } + return last; +} + +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int i, j, s = 0, sbete, dbete, st, dr; + scanf("%d", &n); + for(i = 1; i <= n; ++ i) + scanf("%d", &bete[i]); + sort(bete + 1, bete + n + 1); + for(i = 1; i <= n; ++ i) + for(j = 1; j <= n; ++ j) + if(i != j) + { + sbete = bete[i] + bete[j]; + dbete = bete[i] - bete[j]; + if(dbete < 0) + dbete = 0 - dbete; + dr = cautbindr(sbete); + st = cautbinst(dbete); + s += dr - st + 1; + if(st <= i && i <= dr) + -- s; + if(st <= j && j <= dr) + -- s; + } + printf("%d", s / 6); + return 0; +} diff --git a/nrtri/binary_search/8783a510-409f-11ec-8d0d-dc7196033e92.cpp b/nrtri/binary_search/8783a510-409f-11ec-8d0d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e4d7f9240b92f00872e850e33071b4e37c7293f --- /dev/null +++ b/nrtri/binary_search/8783a510-409f-11ec-8d0d-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + + +vector v; +int cautare_binara(int l, int r, int x) +{ + int mijloc; +//cat timp cele doua capete nu s-au depasit + while(l < r) + { + mijloc = ( l + r) / 2; + + if( v[mijloc] <= x) + l = mijloc + 1; + else r = mijloc ; + } + mijloc = ( l + r) / 2; + if( v[mijloc] > x) + //daca elementul cautat este mai mic decat v[mijloc], scad mijlocul + mijloc --; + return mijloc; +} +int main() { + int n, i, x; + int suma, k, nr=0; + f>>n; + + for(i = 1; i <= n; i++) + { + f >> x; + v.push_back(x); + } + //sortez crescator + sort(v.begin(), v.end()); + for(i = 0; i<= n - 2; i++) + { + for(int j = i+1; j <= n - 1; j++) + { + suma = v[i] + v[j]; + k = cautare_binara(j+1, n-1, suma); + //adaug la fostul nr rezutatul returnat de functia catutare-binara si scad pozitia pe care ma aflu + nr = nr + k - j ; + + } + } + g << nr; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/87bc1eae-409f-11ec-84ad-dc7196033e92.cpp b/nrtri/binary_search/87bc1eae-409f-11ec-84ad-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61a2e32db9e555c98cdfaec10741aa8e0289449b --- /dev/null +++ b/nrtri/binary_search/87bc1eae-409f-11ec-84ad-dc7196033e92.cpp @@ -0,0 +1,61 @@ +#include +#define N 805 +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int a[N],n,ok; + +int Pivotare(int s,int d) +{ int i,j,pasi,pasj,aux; + i=s; j=d; pasi=0; pasj=1; + while(ia[j]) + { aux=a[i]; a[i]=a[j]; a[j]=aux; + pasi=1-pasi; + pasj=1-pasj; + } + i=i+pasi; + j=j-pasj; + } + return i; +} + +void QS(int s,int d) +{ if(sd) + { if(ok==0) return d; + else return d; + } + else{ int mij=(s+d)/2; + if(x==a[mij]) {ok=1; return CB(mij+1,d,x);} + else if(x>n; + for(i=1;i<=n;i++) + fin>>a[i]; + QS(1,n); + for(i=1;i +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,a[801],i,j,st,dr,mij,val,nr,s; +int main() +{ + f>>n; + for(i=1;i<=n;i++) + { + f>>a[i]; + } + sort(a+1,a+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + s=a[i]+a[j]; + val=j; + while(st<=dr) + { + mij=(st+dr)/2; + if(s>=a[mij]) + { + val=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + nr=nr+val-j; + } + } + g< +#include +#include +#include +using namespace std; +vector v; +int cautare_binara(int l,int r,int nr) +{ + int mij; + while(lnr) + { + mij--; + } + return mij; +} +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n,i,j,s_laturi,pos,nr,counter=0; + fin>>n; + for(i=0;i<=n-1;i++) + { + fin>>nr; + v.push_back(nr); + } + sort(v.begin(),v.end()); + for(i=0;i<=n-2;i++) + {for(j=i+1;j<=n-1;j++) + { + s_laturi=v[i]+v[j]; + pos=cautare_binara(j+1,n-1,s_laturi); + counter=counter+pos-j; + } + } + fout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +const int NMAX= 800; +int n; +int v[NMAX+5]; +int cautare_binara(int st, int s) +{ + int dr=n, m; + while(st<=dr) + { + m=(st+dr)/2; + if(v[m]<=s) + st=m+1; + else + dr=m-1; + } + return dr; +} +int main() +{ + int nrt=0, i, j, s; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1, v+n+1); + for(i=1;i<=n-1;i++) + { + for(j=i+1;j<=n;j++) + { + s=v[i]+v[j]; + nrt=nrt+cautare_binara(j+1, s)-j; + + } + } + fout< +#define N 805 +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int a[N],n,ok; + +int Pivotare(int s,int d) +{ int i,j,pasi,pasj,aux; + i=s; j=d; pasi=0; pasj=1; + while(ia[j]) + { aux=a[i]; a[i]=a[j]; a[j]=aux; + pasi=1-pasi; + pasj=1-pasj; + } + i=i+pasi; + j=j-pasj; + } + return i; +} + +void QS(int s,int d) +{ if(sd) + { if(ok==0) return d; + else return d; + } + else{ int mij=(s+d)/2; + if(x==a[mij]) {ok=1; return CB(mij+1,d,x);} + else if(x>n; + for(i=1;i<=n;i++) + fin>>a[i]; + QS(1,n); + for(i=1;i +#include + +using namespace std; + +inline int caut(int *v, int n, int x) { + int pos, i; + for (pos = 1; pos < n; pos <<= 1); + for (i = 0; pos; pos >>= 1) + if (i + pos <= n - 1 && v[i + pos] <= x) + i += pos; + return i; +} + +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n, l[30000], sol = 0; + scanf("%i", &n); + for (int i = 0; i < n; i++) + scanf("%i", &l[i]); + sort(l, l + n); + for (int i = 0; i < n - 1; i++) + for(int j = i + 1; j < n; j++) + if (l[i] + l[j] > l[n - 1]) + sol += n - 1 - j; + else + sol += caut(l, n, l[i] + l[j]) - j; + printf("%i", sol); + return 0; +} diff --git a/nrtri/binary_search/89a497a8-409f-11ec-8b9b-dc7196033e92.cpp b/nrtri/binary_search/89a497a8-409f-11ec-8b9b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..118e30303ede46db69c333bafdcb1131115791a5 --- /dev/null +++ b/nrtri/binary_search/89a497a8-409f-11ec-8b9b-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int n, v[8005], ans; + +int caut_bin(int st, int dr, int val) +{ + int mj; + while(st <= dr) + { + mj = (st + dr) / 2; + if(val < v[mj]) + dr = mj - 1; + else + st = mj + 1; + } + return dr; +} + +int main() +{ + cin >> n; + for(int i = 1; i <= n; ++i) + cin >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j) + { + ans += caut_bin(j + 1, n, v[i] + v[j]) - j; + } + cout << ans; + return 0; +} diff --git a/nrtri/binary_search/89e80a8a-409f-11ec-aa02-dc7196033e92.cpp b/nrtri/binary_search/89e80a8a-409f-11ec-aa02-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2d50dd9a7eab5aa82188e4eeb1c15adb341948f --- /dev/null +++ b/nrtri/binary_search/89e80a8a-409f-11ec-aa02-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include + + + +using namespace std; + + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + + +const int L=9; +const int N=801; +int n; +int v[N]; + +int caut(int a){ + int r=0,pas=1<>n; + for(int i=1;i<=n;i++){ + in>>v[i]; + } + sort(v+1,v+n+1); + for(int i=1;i<=n;i++){ + int j=i+1; + for(j=i+1;j + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +const int DimMax=1024; + +int n,m,s=0; +int v[DimMax]; +int mid,st,dr,sol,i,j; + +void BinarySearch() +{ + for(i=1; is) + dr=mid-1; + else + st=mid+1; + } + sol+=dr-j; + } + } +} + +int main() +{ + in>>n; + for(i=1; i<=n; i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + BinarySearch(); + out< +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + + +vector v; +int cautare_binara(int l, int r, int x) +{ + int mijloc; + + while(l < r) + { + mijloc = ( l + r) / 2; + + if( v[mijloc] <= x) + l = mijloc + 1; + else r = mijloc ; + } + mijloc = ( l + r) / 2; + if( v[mijloc] > x) + //daca elementul cautat este mai mare decat v[mijloc], cresc mijlocul + mijloc --; + return mijloc; +} +int main() { + int n, i, x; + int suma, k, nr=0; + f>>n; + + for(i = 1; i <= n; i++) + { + f >> x; + v.push_back(x); + } + sort(v.begin(), v.end()); + for(i = 0; i<= n - 2; i++) + { + for(int j = i+1; j <= n - 1; j++) + { + suma = v[i] + v[j]; + k = cautare_binara(j+1, n-1, suma); + nr = nr + k - j ; + + } + } + g << nr; + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/8c3d53b9-409f-11ec-9902-dc7196033e92.cpp b/nrtri/binary_search/8c3d53b9-409f-11ec-9902-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..247cd286f1e6c1ce2a08cbdb506c54a584e96e81 --- /dev/null +++ b/nrtri/binary_search/8c3d53b9-409f-11ec-9902-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,m,v[1024],x,s,sol,mid,st,dr; +int main(){ + in>>n; + for(int i=1;i<=n;i++){ + in>>v[i]; + } + sort(v+1,v+n+1); + for(int i=1;is) + dr=mid-1; + else + st=mid+1; + } + sol+=dr-j; + } + } + out< +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, i, a[804], j, st, dr, sol, mij; +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + { + fin>>a[i]; + } + sort(a+1,a+1+n); + int cnt=0; + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + sol=-1; + while(st<=dr) + { + mij=(st+dr)/2; + if(a[i]+a[j]>=a[mij]) + { + sol=mij; + st=mij+1; + } + else + { + dr=mij-1; + } + } + if(sol!=-1) + { + cnt=cnt+sol-j; + } + } + } + fout< +using namespace std; +typedef long long LL; +#define mp make_pair +#define CHECK(x) if(!(x)) return false; +#define CHECKRET(x, y) if(!(x)) return (y); +#define SKIP(x) if((x)) continue; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "nrtri" +#else +#define ProblemName "fis" +#endif + +#define InFile ProblemName ".in" +#define OuFile ProblemName ".out" + +const int MAXN = 810; +int v[MAXN]; + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + int N; + scanf("%d", &N); + for (int i = 0; i < N; ++i) + scanf("%d", &v[i]); + sort(v, v + N); + int ans = 0; + for (int i = 0; i < N; ++i) + for (int j = i + 1; j < N; ++j) { + int a = v[i], b = v[j]; + //a + b >= c + int idx = upper_bound(v + j + 1, v + N, a + b) - v; + ans += idx - (j + 1); + } + printf("%d\n", ans); + return 0; +} diff --git a/nrtri/binary_search/8d161b61-409f-11ec-a539-dc7196033e92.cpp b/nrtri/binary_search/8d161b61-409f-11ec-a539-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7864a61ac60903c2b61c113cef06127c0f48ea4 --- /dev/null +++ b/nrtri/binary_search/8d161b61-409f-11ec-a539-dc7196033e92.cpp @@ -0,0 +1,39 @@ + +#include +#include +using namespace std; +int v[801],s,S,i,j,dr,st,mij,n,p; + + + +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); + +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + p=0; + s=v[i]+v[j]; + st=j; + dr=n; + while(st<=dr){ + mij=(st+dr)/2; + if(s // reimplementare pentru lab ASD +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int N, A[801], i, j, cnt, mask; +int cautbin (int val, int mask) { + int sol = 0; + while (mask) { + if (sol + mask <= N) + if (A[sol + mask] <= val) + sol += mask; + mask >>= 1; + } + return min(sol, N); +} +int main() { + f >> N; + for (i = 1; i <= N; i++) + f >> A[i]; + sort(A + 1, A + N + 1); + cnt = 0; + mask = 1; + while (mask <= N) + mask <<= 1; + mask >>= 1; + for (i = 1; i <= N; i++) + for (j = i + 1; j <= N; j++) + cnt += cautbin(A[i] + A[j], mask) - j; + g << cnt; + return 0; +} diff --git a/nrtri/binary_search/8e1ccfb2-409f-11ec-b785-dc7196033e92.cpp b/nrtri/binary_search/8e1ccfb2-409f-11ec-b785-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ff29201fa7ac2dcfd738ce49b5e279547c10dbb --- /dev/null +++ b/nrtri/binary_search/8e1ccfb2-409f-11ec-b785-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +int v[801], i, j, n, st, dr, I, x, mid; +int main() { + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + fin >> n; + int sol = 0; + for (i = 1; i <= n; i++) + fin >> v[i]; + for (i = 1; i <= n - 1; i++) + for (j = i + 1; j <= n; j++) + if (v[i] > v[j]) { + I = v[i]; + v[i] = v[j]; + v[j] = I; + } + for (i = 1; i < n - 1; i++) + for (j = i + 1; j <= n - 1; j++) { + st = j + 1; + dr = n; + x = v[i] + v[j]; + while (st <= dr) { + mid = (st + dr) / 2; + if (x +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,a,st,dr,mij,sol,v[900]; +int main () { + fin>>n; + for (i=1;i<=n;i++) { + fin>>v[i]; + } + sort(v+1, v+n+1); + for (i=1;i<=n-2;i++) { + for (j=i+1;j<=n-1;j++) { + a=v[i]+v[j]; + /// caut pozitia ultimei valori mai mica strict decat a + st=j+1; + dr=n; + while (st<=dr) { + mij=(st+dr)/2; + if (v[mij]>a) + dr=mij-1; + else + st=mij+1; + } + + sol+=dr - j; + } + } + fout< +#include +using namespace std; +int v[2001]; +int main() +{ + FILE *fin,*fout; + fin=fopen("nrtri.in","r"); + fout=fopen("nrtri.out","w"); + int n,i,j,mij,poz,st,dr,x,s=0; + fscanf(fin,"%d",&n); + for(i=1;i<=n;i++){ + fscanf(fin,"%d",&v[i]); + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++){ + for(j=i+1;j<=n-1;j++){ + x=v[i]+v[j]; + st=j+1; + dr=n; + poz=-1; + while(st<=dr){ + mij=(st+dr)/2; + if(v[mij]>x) + dr=mij-1; + else{ + poz=mij; + st=mij+1; + } + } + if(poz!=-1) + s=s+poz-j; + } + } + fprintf(fout,"%d",s); + fclose(fin); + fclose(fout); + return 0; +} diff --git a/nrtri/binary_search/8ed1afea-409f-11ec-a9c2-dc7196033e92.cpp b/nrtri/binary_search/8ed1afea-409f-11ec-a9c2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0c1000bfc09b908ed2f75ed376dbd31843ed3bc --- /dev/null +++ b/nrtri/binary_search/8ed1afea-409f-11ec-a9c2-dc7196033e92.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[802],a[802]; +void interclasare(int st,int dr,int m){ +int i,j,k=st-1; +i=st; +j=m+1; +while(i<=m && j<=dr) + if(v[i]<=v[j]){ + k++; + a[k]=v[i]; + i++; + } + else { + k++; + a[k]=v[j]; + j++; + } +if(i<=m) + for(j=i;j<=m;j++) + {k++; + a[k]=v[j]; + } +else for(i=j;i<=dr;i++) + {k++; + a[k]=v[i]; + } +for(i=st;i<=dr;i++) + v[i]=a[i]; +} +void mergesort(int st,int dr){ + if(st x) m --; + return m; +} +int main() +{ + int n,m,s,r=0,i,j; + f>>n; + for(i=1;i<=n;i++) + f>>v[i]; + mergesort(1,n); + for(i=1;i +#include + +FILE *fin = freopen("nrtri.in", "r", stdin); FILE *fout = freopen("nrtri.out", "w", stdout); + +const int MAX_N = 800 + 5; + +int n; +int v[MAX_N]; +int max_bit; + +int BinarySearch(int value) { + int k = 0; + for(int step = max_bit; step > 0; step >>= 1) { + if(k + step <= n && v[k + step] <= value) { + k += step; + } + } + + return k; +} + +int GetTriangleNumber() { + int answer = 0; + + for(max_bit = 1; max_bit <= n; max_bit <<= 1); + + for(int i = 1; i <= n; i++) { + for(int j = i + 1; j <= n; j++) { + int idx = BinarySearch(v[i] + v[j]); + answer += idx - j; + } + } + + return answer; +} + +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; i++) { + scanf("%d", &v[i]); + } + std::sort(v + 1, v + n + 1); + + printf("%d\n", GetTriangleNumber()); + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/8f7b63ea-409f-11ec-bc7c-dc7196033e92.cpp b/nrtri/binary_search/8f7b63ea-409f-11ec-bc7c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8db1cb7b935b8754cddf3b52a70d0de5849030ab --- /dev/null +++ b/nrtri/binary_search/8f7b63ea-409f-11ec-bc7c-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +#include + +using namespace std; + +int v[2001]; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,i,nr,ct=0,j,s,st,dr,poz,m; + scanf("%d",&n); + for(i=1;i<=n;i++) + { + scanf("%d",&v[i]); + } + sort(v+1,v+1+n); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + s=v[i]+v[j]; + st=j+1; + dr=n; + poz=-1; + while(st<=dr) + { + m=(st+dr)/2; + if(v[m]>s) + { + dr=m-1; + } + else + { + st=m+1; + poz=m; + } + } + if(poz!=-1) + { + ct=ct+poz-j; + } + } + } + printf("%d",ct); + + return 0; +} diff --git a/nrtri/binary_search/8ff91734-409f-11ec-a517-dc7196033e92.cpp b/nrtri/binary_search/8ff91734-409f-11ec-a517-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c02fd3009f23e1ed1baa1a580c4935271287dd0 --- /dev/null +++ b/nrtri/binary_search/8ff91734-409f-11ec-a517-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +using namespace std; + +int n,st,dr,m,a[2000],dif,sum,p1,p2,ans; +int main() +{ + ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + cin>>n; + for(int i=1;i<=n;i++) cin>>a[i]; + sort(a+1,a+n+1); + for(int i=1;i1) + { + m=(st+dr)/2; + if(a[m]>sum) dr=m; + else st=m; + } + if(st<=n) + ans+=st-j; + + } + } + cout< +#include +#include +using namespace std; + +int v[801]; + +int main() +{ + int N, r, pas, i, j, x, p=0; + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + + in>>N; + + + for (i=1; i<=N; i++) + in>>v[i]; + + sort (v+1,v+N+1); + + for (i=1; i<=N-2; i++) + { + for (j=i+1; j<=N-1; j++) + { + x=v[i]+v[j]; + r=0; + pas=1<<9; + + + while (pas!=0) + { + if ((pas+r)<=N && v[pas+r]<=x) + r+=pas; + pas/=2; + } + + p+=r-j; + } + + } + out<< p << endl; + + out.close (); + in.close (); + return 0; +} + diff --git a/nrtri/binary_search/906b80e3-409f-11ec-ae93-dc7196033e92.cpp b/nrtri/binary_search/906b80e3-409f-11ec-ae93-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea355a1b94a703c8d564cd3f349fe463e1d6f7a9 --- /dev/null +++ b/nrtri/binary_search/906b80e3-409f-11ec-ae93-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,v[803],s,st,dr,mid,ultimul,sol; +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + for(i=1;i<=n-1;i++) + for(j=i+1;j<=n;j++) + if(v[i]>v[j])swap(v[i],v[j]); + v[n+1]=90000; + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + s=v[i]+v[j]; + st=j+1;dr=n; + ultimul=0; + while(st<=dr) + { + mid=(st+dr)/2; + if(v[mid]<=s && v[mid+1]>s) + { + ultimul=mid; + break; + } + else if(v[mid]<=s) + st=mid+1; + else + dr=mid-1; + } + if(ultimul!=0) + sol+=ultimul-j; + } + fout< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int v[802],suma[321201]; + +int main() +{ + int n,i,l,j,k=0; + f>>n; + for(i=1;i<=n;i++) + { + f>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + + { + l=v[j+1]; + int st,dr,mij,gasit; + st=j+1; + dr=n; + gasit=j; + while(st<=dr) + { + mij=(st+dr) / 2; + if(v[mij]>v[i]+v[j]) + { + dr=mij-1; + } + else + { + gasit=mij; + st=mij+1; + } + } + k=k+gasit-j; + } + } + g< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,v[803],s,st,dr,mid,ultimul,sol; +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + /*for(i=1;i<=n-1;i++) + for(j=i+1;j<=n;j++) + if(v[i]>v[j])swap(v[i],v[j]);*/ + sort(v+1,v+n+1); + v[n+1]=90000; + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + s=v[i]+v[j]; + st=j+1;dr=n; + ultimul=0; + while(st<=dr) + { + mid=(st+dr)/2; + if(v[mid]<=s && v[mid+1]>s) + { + ultimul=mid; + break; + } + else if(v[mid]<=s) + st=mid+1; + else + dr=mid-1; + } + if(ultimul!=0) + sol+=ultimul-j; + } + fout< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +const int N = 801, L = 9; +int v[N], n; + +int NrTri(int s, int poz) +{ + int r = poz, pas = 1<> n; + for(int i=1; i<=n; i++) f >> v[i]; + sort(v+1, v+n+1); + int nr=0; + //cout << NrTri(7, 4); + for(int i=1; i +#include +using namespace std; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,i,j,aux,nr=0,k; + cin>>n; + int v[n+1]; + for(i=1; i<=n; i++) + cin>>v[i]; + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + if(v[i]>v[j]) + { + aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } + int st,dr,mij,elem; + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + { + st=j+1;dr=n;elem=-1; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]>v[i]+v[j]) + dr=mij-1; + else{ + elem=mij; + st=mij+1; + } + } + if(elem!=-1) + nr=nr+(elem-j); + } + cout< +#include +#include +#include +#include + +using namespace std; + +int n; + +vector a; + +int find(int l, int h, int x) { + if (l > h) { + return -1; + } + + int mid = (l + h) / 2; + + if (mid == h) { + if (a[mid] >= x) { + return mid; + } + else { + return -1; + } + } + + if (a[mid] >= x && a[mid + 1] < x) { + return mid; + } + + if (a[mid] >= x) { + return find(mid + 1, h, x); + } + + return find(l, mid - 1, x); + +} + +int main() { + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + + scanf("%d", &n); + + for (int i = 0; i < n; ++i) { + int x; + scanf("%d", &x); + a.push_back(x); + } + + sort(a.begin(), a.end(), greater()); + + int sol = 0; + + for (int i = 0; i < n; ++i) { + int x = a[i]; + for (int j = i + 1; j < n; ++j) { + int y = a[j]; + + int ndx = find(j + 1, n - 1, x - y); + + if (ndx != -1) { + sol += ndx - j; + } + } + } + + printf("%d", sol); + + return 0; +} diff --git a/nrtri/binary_search/92858953-409f-11ec-acfc-dc7196033e92.cpp b/nrtri/binary_search/92858953-409f-11ec-acfc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6587b29017da74482bf0450e89acb6343d672e7d --- /dev/null +++ b/nrtri/binary_search/92858953-409f-11ec-acfc-dc7196033e92.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +using namespace std; +int v[803]; +int main() +{ + int st,dr,mijl,i,n,poz,j,nr=0,p,min,aux; + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + st=j+1; + dr=n; + poz=-1; + while(st<=dr) + { + mijl=(st+dr)/2; + if(v[mijl]>v[i]+v[j])dr=mijl-1; + else {poz=mijl; + st=mijl+1;} + } + if(poz!=-1) + nr=nr+(poz-j);} + out< +#include +using namespace std; + +int N, S, L[800]; + +int exista(int x, int a){ + int i, j, m; + + i=a; j=N-1; + while (i<=j){ + m=(i+j)/2; + if (L[m]>x) + j=m-1; + else + i=m+1; + } + + return j-a+1; +} + +int main(){ + int i, j; + + ifstream fin ("nrtri.in"); + fin >> N; + for (i=0; i> L[i]; + fin.close(); + + sort(L, L+N); + + for (i=0; i +#include +#define MAX 801 + +using namespace std; +int v[MAX]; +int main() +{ + int n, i, j, st, dr, mij, contor = 0, elem; + + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + fin >> n; + + for(i = 1; i <= n; i++)fin >> v[i]; + sort(v + 1, v + 1 + n); + + for(i = 1; i <= n - 2; i++) + { + for(j = i + 1; j <= n - 1; j++) + { + st = j + 1; + dr = n; + elem = -1; + while(st <= dr) + { + mij = (st + dr) / 2; + if(v[i] + v[j] < v[mij])dr = mij - 1; + else + { + elem = mij; + st = mij + 1; + } + } + if(elem != -1)contor += elem - j; + } + } + + fout << contor; + + fin.close(); + fout.close(); + + return 0; +} diff --git a/nrtri/binary_search/933beed9-409f-11ec-b83d-dc7196033e92.cpp b/nrtri/binary_search/933beed9-409f-11ec-b83d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1126534f34337d095464e9b48751b6b93dee7346 --- /dev/null +++ b/nrtri/binary_search/933beed9-409f-11ec-b83d-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +int v[805]; + +int main() +{ + int i,n,j,mij,st,dr,aux,S=0,p; + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + cin>>n; + for(i=1;i<=n;i++) + cin>>v[i]; + for(i=1;i<=n-1;i++) + for(j=i+1;j<=n;j++) + if(v[i]>v[j]) + { + aux=v[i]; + v[i]=v[j]; + v[j]=aux; + } + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + st=j+1; + p=j; + dr=n; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]>v[i]+v[j]) + dr=mij-1; + else + { + p=mij; + st=mij+1; + } + } + S=S+p-j; + } + cout< +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +const int NMAX=800; +int v[NMAX+1],n; +int caut1(int x,int inceput) +{ + int p=inceput,u=n,poz=-1,m; + while(p<=u) + { + m=(p+u)/2; + if(x>=v[m]) + { + poz=m; + p=m+1; + } + else + u=m-1; + } + return poz; +} +int main() +{ + f>>n; + int s=0; + for(int i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(int i=1; i<=n-2; i++) + for(int j=i+1; j<=n-1; j++) + s+=(caut1(v[i]+v[j],i+1)-j); + g< +#include + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int laturi[801]; +int cautBinara(int st, int dr, int x) +{ + int mij; + while(st <= dr) { + mij = (st + dr) / 2; + if(laturi[mij] >x) + dr = mij - 1; + else + st = mij + 1; + } + return dr; +} +int nrLaturi, i, j, x, nrTriunghiuri = 0; +int main() +{ + f >> nrLaturi; + for(i = 1; i <= nrLaturi; i++) + f >> laturi[i]; + sort(laturi + 1, laturi + nrLaturi + 1); + for(i = 1; i < nrLaturi - 1; i++) + for(j = i + 1; j < nrLaturi; j++) { + x = cautBinara(j + 1, nrLaturi, laturi[i] + laturi[j]); + if(x > j) + nrTriunghiuri+= (x - j); + } + g << nrTriunghiuri << '\n'; + return 0; +} diff --git a/nrtri/binary_search/9439dd8d-409f-11ec-9074-dc7196033e92.cpp b/nrtri/binary_search/9439dd8d-409f-11ec-9074-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d55824cf7eb48de5d55f0a2e1aa825bc58b5dd --- /dev/null +++ b/nrtri/binary_search/9439dd8d-409f-11ec-9074-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, nrt, v[801]; + +int cautbin(int val) +{ + int st = 1, dr = n, poz = -1; + while(st <= dr) + { + int med = (st + dr) / 2; + if(val >= v[med]) + { + st = med + 1; + poz = med; + } + else + dr = med - 1; + } + return poz; +} + +int main() +{ + f >> n; + for(int i = 1; i <= n; i++) + f >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i < n - 1; i++) + for(int j = i + 1; j < n; j++) + { + int poz = cautbin(v[i] + v[j]); + if(poz != -1) + nrt += (poz - j); + } + g << nrt; + return 0; +} diff --git a/nrtri/binary_search/946f4807-409f-11ec-81c5-dc7196033e92.cpp b/nrtri/binary_search/946f4807-409f-11ec-81c5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e3d669575136cc72e6f23f0c67a2f65b7a2cd56 --- /dev/null +++ b/nrtri/binary_search/946f4807-409f-11ec-81c5-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + + +ifstream fi("nrtri.in"); +ofstream fo("nrtri.out"); + + +int n,a[1300],r,caut; + +int CautBin(int x) +{ + int poz=0; + + + for(int i=(1<<10); i>0; i/=2) + { + if(poz+i<=n && a[poz+i]<=x) + { + poz+=i; + } + } + + return poz; +} +int main() +{ + fi>>n; + for(int i=1; i<=n; i++) + fi>>a[i]; + + sort(a+1,a+n+1); + + for(int i=1; i +#include +using namespace std; +int v[801]; +int cautare(int x,int poz1,int poz2) +{ + int stg=poz1,dr=poz2,mij; + while(stg<=dr) + { + mij=(stg+dr)/2; + if(v[mij]>x) dr=mij-1; + else stg=mij+1; + } + return dr; +} +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n,rez=0,cb; + f>>n; + for(int i=1;i<=n;i++) f>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { + cb=cautare(v[i]+v[j],j+1,n); + if(cb>j) + rez+=cb-j; + } + g< +#include +#include +using namespace std; +int n,i,a[1000],st,dr,nr; +int main() +{ +ifstream fin ("nrtri.in"); +ofstream fout("nrtri.out"); +fin>>n; +for(i=1;i<=n;i++) + fin>>a[i]; +sort(a+1,a+n+1); +for(i=3;i<=n;i++) +{ + st=1; + dr=i-1; + while(st=a[i]) + { + nr=nr+ dr-st; + dr--; + } + else + st++; + } +} +fout< +#include + +FILE *fin = freopen("nrtri.in", "r", stdin); FILE *fout = freopen("nrtri.out", "w", stdout); + +const int MAX_N = 800 + 5; + +int n; +int v[MAX_N]; +int max_bit; + +int BinarySearch(int value) { + int k = 0; + for(int step = max_bit; step > 0; step >>= 1) { + if(k + step <= n && v[k + step] <= value) { + k += step; + } + } + + return k; +} + +int GetTriangleNumber() { + int answer = 0; + + for(max_bit = 1; max_bit <= n; max_bit <<= 1); + + for(int i = 1; i <= n; i++) { + for(int j = i + 1; j <= n; j++) { + int idx = BinarySearch(v[i] + v[j]); + answer += idx - j; + } + } + + return answer; +} + +int main() { + scanf("%d", &n); + for(int i = 1; i <= n; i++) { + scanf("%d", &v[i]); + } + std::sort(v + 1, v + n + 1); + + printf("%d\n", GetTriangleNumber()); + return 0; +} \ No newline at end of file diff --git a/nrtri/binary_search/95dd8d9f-409f-11ec-ad5f-dc7196033e92.cpp b/nrtri/binary_search/95dd8d9f-409f-11ec-ad5f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8b38849e5b51bea3e03206f46627de6aa5de76d --- /dev/null +++ b/nrtri/binary_search/95dd8d9f-409f-11ec-ad5f-dc7196033e92.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; + +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); + +int v[803], n; + +int cautbin(int x) +{ + int stanga = 1, dreapta = n, rasp = -1, mijloc; + while( stanga <= dreapta ) + { + mijloc = ( stanga + dreapta ) / 2; + if( v[mijloc] <= x) + { + stanga = mijloc + 1; + rasp = mijloc; + } + else + { + dreapta = mijloc - 1; + } + } + return rasp; +} + +int main() +{ + int x, k, nr = 0; + fin >> n; + for( int i = 1 ; i <= n ; i++) + fin >> v[i]; + sort(v+1, v+n+1); + for( int i = 1 ; i <= n - 1 ; i++) + for( int j = i + 1 ; j <= n; j++) + { + x = v[i] + v[j]; + k = cautbin(x); + nr = nr + ( k - j ); + } + fout << nr << '\n'; + return 0; +} diff --git a/nrtri/binary_search/960e0a99-409f-11ec-8a56-dc7196033e92.cpp b/nrtri/binary_search/960e0a99-409f-11ec-8a56-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa67c70b6e636f872c82331bfe1c95aa0439df5c --- /dev/null +++ b/nrtri/binary_search/960e0a99-409f-11ec-8a56-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +const int MAX_SIZE = 1002; +int binarySearch(int x[], int n, int i, int j){ + int left = j + 1, right = n, middle, value = x[i] + x[j]; + while(left <= right){ + middle = (left + right) / 2; + if(x[middle] <= value) + left = middle + 1; + else + right = middle - 1; + } + return right; +} +int main() +{ + int n, x[MAX_SIZE], result = 0, third; + in>>n; + for(int i=1;i<=n;i++) + in>>x[i]; + in.close(); + sort(x+1,x+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++){ + third = binarySearch(x,n,i,j); + if(third > j) + result += (third - j); + } + out< +#include +#include +#include +using namespace std; +int n,i,x,p,tri,s,j; +vector a; +vector ::iterator it; +int cb(int li, int ls, int s) +{ + int m; + while(li<=ls) + { + m=(li+ls)/2; + if(a[m]==s) return m; + if(a[m]>n; + for(i=1;i<=n;i++) cin>>x,a.push_back(x); + sort(a.begin(),a.end()); + for(i=0;ij) tri+=p-j; + } + cout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[803], n; + +int cautbin(int p, int u, int x) +{ + int mij, sol; + while(p<=u) { + mij=(p+u)/2; + if(v[mij]>x) + u=mij-1; + else + p=mij+1; + } + return u; +} + +int main() +{ + fin>>n; + for(int i=1; i<=n; ++i) + fin>>v[i]; + sort(v+1,v+n+1); + int x,sum=0; + for(int i=1; ij) + sum+=(x-j); + } + fout< +#include + +const int NMAX = 800 + 5; +FILE *fin = freopen("nrtri.in","r",stdin); FILE *fout = freopen("nrtri.out","w",stdout); + +/* ======= DATA ========= */ +int n, v[NMAX]; +/* ======= DATA ========= */ + +int cb(int val) +{ + int pas= 1, k= 0; + for(; pas <= n; pas<<=1); + for(; pas; pas >>=1) + { + if(k+pas<= n && v[k+pas]<= val) + k+=pas; + } + return k; +} + +void Rezolva() +{ + int ct =0; + + for(int i =1 ;i< n;i++) + { + for(int j = i + 1; j<= n;j++) + { + int x = cb(v[i] + v[j]); + ct+= (x - j); + } + } + printf("%d",ct); +} + +int main() +{ + scanf("%d",&n); + for(int i =1 ;i<= n; ++i) + { + scanf("%d",&v[i]); + } + std::sort(v + 1, v + n + 1); + + Rezolva(); +} diff --git a/nrtri/binary_search/97250500-409f-11ec-9c64-dc7196033e92.cpp b/nrtri/binary_search/97250500-409f-11ec-9c64-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8147c3ecc30cbae37a1ab52f7eaeeaefe13c1945 --- /dev/null +++ b/nrtri/binary_search/97250500-409f-11ec-9c64-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include + +const int NMAX = 800 + 5; +FILE *fin = freopen("nrtri.in","r",stdin); FILE *fout = freopen("nrtri.out","w",stdout); + +/* ======= DATA ========= */ +int n, v[NMAX]; +/* ======= DATA ========= */ + +int cb(int val) +{ + int st = 1, dr = n , res = -1; + while(st<= dr) + { + int mij = (st + dr) >> 1; + if(v[mij] <= val) + { + st = mij + 1; + res = mij; + } + else + dr = mij - 1; + } + return res; +} + +void Rezolva() +{ + int ct =0; + + for(int i =1 ;i< n;i++) + { + for(int j = i + 1; j<= n;j++) + { + int x = cb(v[i] + v[j]); + ct+= (x - j); + } + } + printf("%d",ct); +} + +int main() +{ + scanf("%d",&n); + for(int i =1 ;i<= n; ++i) + { + scanf("%d",&v[i]); + } + std::sort(v + 1, v + n + 1); + + Rezolva(); +} diff --git a/nrtri/binary_search/97a904a1-409f-11ec-99c7-dc7196033e92.cpp b/nrtri/binary_search/97a904a1-409f-11ec-99c7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be41c80d7b14b34ae2032cd2a00acc962db1513e --- /dev/null +++ b/nrtri/binary_search/97a904a1-409f-11ec-99c7-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n; +int v[802]; + +int cautbin(int x) +{ + int stanga = 1, dreapta = n, rasp = -1, mijloc; + while(stanga <= dreapta) + { + mijloc = (stanga + dreapta) / 2; + if(v[mijloc] <= x) + { + rasp = mijloc; + stanga = mijloc + 1; + } + else + dreapta = mijloc - 1; + } + return rasp; +} + +int main() +{ + int nr = 0, k; + fin >> n; + for(int i = 1; i <= n; i++) + fin >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n - 1; i++) + for(int j = i + 1; j <= n; j++) + { + k = cautbin(v[i] + v[j]); + nr += k - j; + } + fout << nr; + return 0; +} diff --git a/nrtri/binary_search/97df30cc-409f-11ec-b155-dc7196033e92.cpp b/nrtri/binary_search/97df30cc-409f-11ec-b155-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9dd951aff0b53ea4d2468095d443ec25a96ad9eb --- /dev/null +++ b/nrtri/binary_search/97df30cc-409f-11ec-b155-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#include +#define DN 805 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,a[DN],st,dr,mij,rez,f; +int main() +{ + fin>>n; + for(int i=1;i<=n;i++) + fin>>a[i]; + sort(a+1,a+n+1); + for(int i=n;i>2;i--) + for(int j=i-1;j>1;j--) + { + st=1; + dr=j-1; + f=a[i]-a[j]; + if(a[i]>a[j]+a[j-1]) + continue; + while(st=f) + dr=mij; + else + st=mij+1; + } + rez+=j-st; + } + fout< +#include +using namespace std; +int N, List[805], i, S, j; +int Binary(int st, int dr, int Find){ + int inplus=st-1, mij; + while(stList[mij]) st=mij+1; + else dr=mij-1; + } + if(List[st]==Find)while(List[st+1]==List[st])st++; + if(Find +#include +#include +#define MAX 800 + +using namespace std; + +int main(){ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int nr, i, j, k, arr[MAX]; + in >> nr; + for (i = 0; i != nr; i++){ + in >>arr[i]; + } + sort(arr, arr + nr); + int sum = 0; + for (i = 0 ;i < nr; i++){ + for (j = i + 1 ;j < nr; j++){ + for (k = j + 1 ;k < nr; k++){ + if(arr[i] + arr[j] >= arr[k] && arr[i] + arr[k] >= arr[j] && arr[k] + arr[j] >= arr[i] ) { + sum++; + } + else break; + } + } + } + out < +#include + +using namespace std; + +int v[803]; + +int cb(int x, int n, int i, int j) { + int st, mij, nsol, dr, ok = 0, sol; + sol = 0; + st = 0; + dr = n - 1; + while (st <= dr) { + mij = (st + dr) / 2; + if (v[mij] > x) + dr = mij - 1; + else { + st = mij + 1; + sol = mij; + } + } + return sol - j; +} + +int main() { + ifstream cin ("nrtri.in"); + ofstream cout ("nrtri.out"); + int n, i, sol = 0, j, x; + cin >> n; + for (i = 0; i < n; i++) + cin >> v[i]; + sort (v, v + n); + for (i = 0; i < n; i++) { + for (j = i + 1; j < n; j++) { + if (i != j) { + x = v[i] + v[j]; + sol += cb(x, n, i, j); + } + } + } + cout << sol; + return 0; +} diff --git a/nrtri/binary_search/98cac4b3-409f-11ec-bf5d-dc7196033e92.cpp b/nrtri/binary_search/98cac4b3-409f-11ec-bf5d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be41c80d7b14b34ae2032cd2a00acc962db1513e --- /dev/null +++ b/nrtri/binary_search/98cac4b3-409f-11ec-bf5d-dc7196033e92.cpp @@ -0,0 +1,43 @@ +#include +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n; +int v[802]; + +int cautbin(int x) +{ + int stanga = 1, dreapta = n, rasp = -1, mijloc; + while(stanga <= dreapta) + { + mijloc = (stanga + dreapta) / 2; + if(v[mijloc] <= x) + { + rasp = mijloc; + stanga = mijloc + 1; + } + else + dreapta = mijloc - 1; + } + return rasp; +} + +int main() +{ + int nr = 0, k; + fin >> n; + for(int i = 1; i <= n; i++) + fin >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n - 1; i++) + for(int j = i + 1; j <= n; j++) + { + k = cautbin(v[i] + v[j]); + nr += k - j; + } + fout << nr; + return 0; +} diff --git a/nrtri/binary_search/99054ff0-409f-11ec-8304-dc7196033e92.cpp b/nrtri/binary_search/99054ff0-409f-11ec-8304-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dca52b4c9c88dbc633c79b00d8e1535380f9a60f --- /dev/null +++ b/nrtri/binary_search/99054ff0-409f-11ec-8304-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; +int N, List[805], i, S, j; +int Binary(int st, int dr, int Find){ + int inplus=st-1, mij; + while(stList[mij]) st=mij+1; + else dr=mij-1; + } + if(List[st]==Find)while(List[st+1]==List[st])st++; + if(Find +#include +#define MAX_N 800 +using namespace std; + +int v[MAX_N+1], n, ans; + +void read() { + FILE* fin = fopen("nrtri.in","r"); + fscanf(fin,"%d",&n); + for(int i = 1; i <= n; i++) + fscanf(fin,"%d",&v[i]); + fclose(fin); +} + +int binarySearch(int Begin, int End, int val) { + int mid, b = Begin, e = End, pos = -1; + while(b <= e) { + mid = (b + e) >> 1; + if(v[mid] <= val) { + pos = mid; + b = mid + 1; + } else e = mid - 1; + } + return pos; +} + +void solve() { + FILE* fout = fopen("nrtri.out","w"); + int i, j, p; + for(i = 1; i <= n; i++) + for(j = i + 1; j <= n; j++) { + p = binarySearch(j+1,n,v[i]+v[j]); + if(p != -1) + ans += p - j; + } + fprintf(fout,"%d\n",ans); + fclose(fout); +} + +int main() { + read(); + sort(v+1,v+n+1); + solve(); + return 0; +} diff --git a/nrtri/binary_search/9ac992c0-409f-11ec-8066-dc7196033e92.cpp b/nrtri/binary_search/9ac992c0-409f-11ec-8066-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a664c61ba99a0c8e8c33cf989d967951485c1cac --- /dev/null +++ b/nrtri/binary_search/9ac992c0-409f-11ec-8066-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +//ifstream f("/home/mihai/Documents/C++/Infoarena/file.in"); +//ofstream g("/home/mihai/Documents/C++/Infoarena/file.out"); + + +const int NMAX = 825; + +int a[NMAX]; + +int n; + +bool OK(int a, int b, int c) +{ + return (a + b >= c && b + c >= a && a + c >= b); +} + +int bs(int p1, int p2, int dr = n) +{ + int st = p2 + 1; + int sol = 0; + while(st <= dr) + { + int mij = (dr + st) / 2; + if(OK(a[p1], a[p2], a[mij])) + sol = mij, st = mij + 1; + else + dr = mij - 1; + } + if(sol) + return sol - p2; + return 0; +} + +int main() +{ + f >> n; + for(int i = 1; i <= n; i++) + f >> a[i]; + int sol = 0; + sort(a + 1, a + 1 + n); + for(int i = 1; i < n - 1; i++) + for(int j = i + 1; j < n; j++) + sol += bs(i, j); + g << sol; +} \ No newline at end of file diff --git a/nrtri/binary_search/9af6d4a5-409f-11ec-b01d-dc7196033e92.cpp b/nrtri/binary_search/9af6d4a5-409f-11ec-b01d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b93cd97313e0a3682e68d67f366427e7b32c81bd --- /dev/null +++ b/nrtri/binary_search/9af6d4a5-409f-11ec-b01d-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +inline int binSearch(const vector &Arr, const int &lft, const int &rgt) { + + int low = rgt + 1, high = Arr.size() - 1, pos = rgt; + + while (low <= high) { + + int mid = low + ((high - low) >> 1); + + if (Arr[mid] <= Arr[rgt] + Arr[lft]) { + + pos = mid; + low = mid + 1; + } + else high = mid - 1; + } + + return pos - rgt; +} + +int main() { + + int N; + fin >> N; + + vector Arr; + Arr.resize(N); + + for (int idx = 0; idx < N; ++idx) + fin >> Arr[idx]; + + sort(Arr.begin(), Arr.end()); + + int cnt = 0; + for (int low = 0; low < N - 2; ++low) + for (int high = low + 1; high < N - 1; ++high) + cnt += binSearch(Arr, low, high); + fout << cnt; +} \ No newline at end of file diff --git a/nrtri/binary_search/9b283275-409f-11ec-aa2c-dc7196033e92.cpp b/nrtri/binary_search/9b283275-409f-11ec-aa2c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb224cd14c1918c9f894da18fb10c0ebaba085bb --- /dev/null +++ b/nrtri/binary_search/9b283275-409f-11ec-aa2c-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v,n,s=0,A[801],ni; + +int cbin(int st,int dr){ + int m=st+(dr-st)/2; + if(m!=st){ + if (st>n; + for(int i=0;i>A[i]; + } + sort(A,A+n); + for(int i=0;i +#include +#include +using namespace std; + +const int L = 1 << 10; +int cautare_binara(int v[], int n, int nr) { + int pas = L; + int p = 0; + + while (pas != 0) { + if (p + pas <= n && v[p + pas] <= nr) { + p += pas; + } + pas /= 2; + } + + return p; +} + +int main() +{ + ifstream cin("nrtri.in"); + ofstream cout("nrtri.out"); + + int v[801], n, sol = 0; + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + + for (int i = 1; i <= n - 1; ++i) { + for (int j = i + 1; j <= n; ++j) { + int sum = v[i] + v[j]; + int p1 = cautare_binara(v, n, sum); + sol += p1 - j; + } + } + + cout << sol; + + + return 0; +} diff --git a/nrtri/binary_search/9c32460c-409f-11ec-83c3-dc7196033e92.cpp b/nrtri/binary_search/9c32460c-409f-11ec-83c3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..235f4c69ed1ca726ab7efd892c7fd51428af7ca5 --- /dev/null +++ b/nrtri/binary_search/9c32460c-409f-11ec-83c3-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +#include +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[801]; +int main() +{ + int n,i,j,st,dr,k,mij,s,cnt,poz; + cnt=0; + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + { + for(j=i+1;j<=n;j++) + { + s=v[i]+v[j]; + st=j+1; dr=n; + poz=-1; + while(st<=dr) + { + mij=(st+dr)/2; + k=v[mij]; + if(k<=s) + { + poz=mij; + st=mij+1; + } + else + dr=mij-1; + } + if(poz!=-1) + cnt+=poz-j; + } + } + out< +#include +#include +using namespace std; +ifstream f ("nrtri.in"); +ofstream g ("nrtri.out"); +int v[805]; +int main() +{ + int n,i,j,st,dr,x,poz,ct,mijl; + f>>n; + for (i=1; i<=n; i++) + f>>v[i]; + sort (v+1,v+n+1); + ct=0; + for (i=1; i<=n; i++) + for (j=i+1; j<=n; j++) + { + x=v[i]+v[j]; + st=j+1; + dr=n; + poz=0; + while (st<=dr) + { + mijl=(st+dr)/2; + if (v[mijl]>x) + dr=mijl-1; + else + { + poz=mijl; + st=mijl+1; + } + } + if (poz!=0) + ct=ct+(poz-j); + } + g< +#include +using namespace std; +int i,j,m,n,a,b,s,t[100005],e,l,x,d,x1,rs; +int c; + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); +void caut(int x,int y) +{ + if (x>=y) {s=x; return;} + int mid=(x+y+1)/2; + if (t[mid]>x1) caut(x,mid-1); + else caut(mid,y); + return; +} +int main() +{ + + fin>>n; + for (i=1;i<=n;i++) + fin>>t[i]; + sort(t+1,t+n+1); + for (i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + x1=t[i]+t[j]; + caut(1,n); + rs+=s-j; + } + fout< +#include + +using namespace std; + +int n, x, solution, v[801]; + +int binarySearch(int value, int lt, int rt) +{ + int mid, result = -1; + while (lt <= rt) { + mid = lt + (rt - lt) / 2; + if (v[mid] <= value) { + result = mid; + lt = mid + 1; + } + else rt = mid - 1; + } + return result; +} + +int main() +{ + FILE *in, *out; + in = freopen("nrtri.in", "r", stdin); + out = freopen("nrtri.out", "w", stdout); + + scanf("%d", &n); + for (int i = 1; i <= n; ++i) scanf("%d", &v[i]); + fclose(in); + sort(v + 1, v + n + 1); + + for (int i = 1; i < n; ++i) + for (int j = i + 1; j <= n; ++j) { + x = binarySearch(v[i] + v[j], j + 1, n); + if (x != -1) solution += (x - j); + } + + printf("%d", solution); + fclose(out); + + return 0; +} diff --git a/nrtri/binary_search/9d60550b-409f-11ec-80c6-dc7196033e92.cpp b/nrtri/binary_search/9d60550b-409f-11ec-80c6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39eaf4e8d4dce8cf3020f852fceea074864bfb93 --- /dev/null +++ b/nrtri/binary_search/9d60550b-409f-11ec-80c6-dc7196033e92.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace std; +int v[801]; +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,mij,st,dr,i,y,x,cnt=0,j,cap1,cap2; + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",&v[i]); + sort(v+1,v+n+1); + for(i=1;i=y && y+v[mij]>=x) + if(mij>cap1) + cap1=mij; + } + } + if(cap1!=0) + { + st=j+1; + dr=n; + cap2=801; + while(st<=dr){ + mij=(st+dr)/2; + if(x+v[mij]=v[mij]) + if(mij +#include +#include +#define DIM 1002 +#define MAX 30002 + +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out("nrtri.out"); + +int n, v[DIM], viz[MAX], sol; + +int main(int argc, const char * argv[]) { + in>>n; + for(int i = 1; i <= n; ++ i){ + in>>v[i]; + ++ viz[v[i]]; + } + + sort(v + 1, v + n + 1); + for(int i = 1; i <= 30001; ++ i) + viz[i] += viz[i - 1]; + + for(int i = 1; i <= n; ++ i){ + for(int j = i + 1; j <= n; ++ j){ + int lgMax = v[j] + v[i]; + lgMax = min(MAX - 1, lgMax); + int st = j + 1, dr = n; + while(st <= dr){ + int mid = (st + dr) / 2; + if(v[mid] <= lgMax) + st = mid + 1; + else + dr = mid - 1; + } + sol += dr - j; + } + } + + out< +#include +#define NMAX 800 + +using namespace std; +ifstream fi("nrtri.in"); +ofstream fo("nrtri.out"); +int n, rez, x; +int X[NMAX+5]; + +void citire(void) +{ + fi>>n; + for(int i=1; i<=n; i++) + fi>>X[i]; +} + +int cautBin(int val, int st, int dr) +{ + int mid, result=-1; + while(st<=dr) + { + mid=st+(dr-st)/2; + if(X[mid]<=val) + { + result=mid; + st=mid+1; + } + else + dr=mid-1; + } + return result; +} + +int main() +{ + citire(); + sort(X+1, X+n+1); + for(int i=1; i<=n-1; i++) + { + for(int j=i+1; j<=n; j++) + { + x=cautBin(X[i]+X[j], j+1, n); + if(x!=-1) + rez+=(x-j); + } + } + fo< +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +const int NMAX =1e3 + 5; +int a[NMAX]; +int t; +int main() +{ + int n; + f >> n; + for(int i = 1; i <= n; i++) + f >> a[i]; + sort(a + 1, a + 1 + n); + for(int i = 1; i < n - 1; i++) + for(int j = i + 1; j < n; j++) + { + int s = a[i] + a[j]; + int ind = lower_bound(a + j, a + 1 + n, s + 1) - a - 1; + t += ind - j; + } + g << t; +} \ No newline at end of file diff --git a/nrtri/binary_search/9e6f4e50-409f-11ec-90df-dc7196033e92.cpp b/nrtri/binary_search/9e6f4e50-409f-11ec-90df-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3a4878adb64260b20487b80c0eafa2394c973ae --- /dev/null +++ b/nrtri/binary_search/9e6f4e50-409f-11ec-90df-dc7196033e92.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#define nmax 801 + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int N,a[nmax],nrcomp,ok,maxim,maxim2,nr,d,ans; +int cautbinar(int st, int dr, int val) +{ + int mij; + int rez=-2; + while(st <= dr) + { + int mij=(st+dr)/2; + if(val < a[mij]) + dr=mij-1; + else if(val >= a[mij]) + { + rez=mij;//calculez cea mai mare pozitie pe care se afla o valoare mai mica + //sau egala cu numarul meu; + st=mij+1; + } + } + return rez; + + } +int main() +{ + fin>>N; + for(int i =1; i <= N; i ++) + fin>>a[i]; + sort(a+1,a+N+1); + for(int i =1 ;i <= N- 2; i ++) + for(int j = i + 1 ;j <= N-1 ; j++) + { + d=a[i]+a[j]; + nr=cautbinar(j+1,N,d); + if(nr!=-2)//obtin un interval de valori bune de la nr la j; + ans+=nr-j; + } + fout< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801], n; + +int caut(int x, int p) { + int u = n, poz = -1; + while(p <= u) { + int m = (p+u) / 2; + if(x >= v[m]) { + poz = m; + p = m+1; + } else + u = m-1; + } + return poz; +} + +int main() +{ + int p, r = 0; + in >> n; + for(int i = 1; i <= n; i++) + in >> v[i]; + sort(v+1, v+n+1); + //2 3 4 7 + for(int i = 1; i < n; i++) { + for(int j = i+1; j <= n; j++) { + p = caut(v[i]+v[j], j); + if(p != -1) r += p-j; + } + } + out << r; + return 0; +} diff --git a/nrtri/binary_search/9ed65a74-409f-11ec-b9a0-dc7196033e92.cpp b/nrtri/binary_search/9ed65a74-409f-11ec-b9a0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2620cf64209598b7c094e8c68efc70e5ffca5631 --- /dev/null +++ b/nrtri/binary_search/9ed65a74-409f-11ec-b9a0-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include + +#include +using namespace std; +int v[801]; +//a+b>=c +int main() +{ + int n,i,t=0,j,st,dr,s,m,nr,poz; + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",&v[i]); + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + s=v[i]+v[j]; + st=j+1; + dr=n; + poz=0; + while(st<=dr) + { + m=(st+dr)/2; + if(v[m]>s)dr=m-1; + else + { + poz=m; + st=m+1; + } + } + if(poz!=0) + t+=poz-j; + + } + printf("%d",t); + return 0; +} diff --git a/nrtri/binary_search/9f078fbb-409f-11ec-869a-dc7196033e92.cpp b/nrtri/binary_search/9f078fbb-409f-11ec-869a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18e06df083f5907d4f711677f2bd02d9ced8f742 --- /dev/null +++ b/nrtri/binary_search/9f078fbb-409f-11ec-869a-dc7196033e92.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; +char const in [] = "nrtri.in"; +char const out [] = "nrtri.out"; +int const NM = 5e5 + 7 , BUFF = 1e7; +int heap [NM] , n , point; +char buff [BUFF]; +int getbuff () +{ + int val = 0; + while(! isdigit (buff [point])) + { + ++ point; + if(point == BUFF) + { + point = 0; + fread (buff , 1 , BUFF , stdin); + } + } + while(isdigit (buff [point])) + { + val = val * 10 + (buff [point] - '0' ); + ++ point; + if(point == BUFF ) + { + point = 0; + fread (buff , 1, BUFF , stdin); + } + } + return val; +} +int father (int nod) +{ + return nod / 2; +} +int left_son (int nod) +{ + return nod * 2; +} +int right_son (int nod) +{ + return nod * 2 + 1; +} +void sift (int strat) +{ + int son = 1; + while(son) + { + son = 0; + if(left_son (strat) <= n) + { + son = left_son (strat); + if(right_son (strat) <= n && heap [son] < heap [right_son (strat)]) + son = right_son (strat); + if(heap [son] <= heap [strat]) + son = 0; + } + if(son) + { + swap (heap [son] , heap [strat]); + strat = son; + } + } +} +void heapsort () +{ + int i; + for(i = n ; i > 0 ; -- i) + { + // printf ("%d " , heap [1]); + swap (heap [1] , heap [n]); + -- n; + sift (1); + } +} +int search_ (int x , int from , int to) +{ + int found = -1; + while(from <= to) + { + int m = (from + to) / 2; + if(heap [m] <= x) + { + found = m; + from = m + 1; + } + else + to = m - 1; + } + return found; +} +int main() +{ + int i; + freopen (in , "r" , stdin); + freopen (out , "w" , stdout); + fread (buff , 1 , BUFF , stdin); + n = getbuff (); + int nx = n , j , best = 0; + for(i = 1 ; i <= n ; ++ i) + heap [i] = getbuff (); + for(i = n / 2 ; i > 0 ; -- i) + sift (i); + heapsort (); + n = nx; + for(i = 1 ; i < n ; ++ i ) + for(j = i + 1 ; j <= n ; ++ j) + { + int x = search_ (heap [i] + heap [j] , j + 1 , n); + if(x != -1) + best += (x - j); + } + printf ("%d" , best); + return 0; +} diff --git a/nrtri/binary_search/9f3afedd-409f-11ec-99b1-dc7196033e92.cpp b/nrtri/binary_search/9f3afedd-409f-11ec-99b1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ef4baa8ead0ff26f21ccaca8e72b249830419ba --- /dev/null +++ b/nrtri/binary_search/9f3afedd-409f-11ec-99b1-dc7196033e92.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; +int v[801]; + int n ; + void Inputu() + { + freopen("nrtri.in","r",stdin); + cin >> n ; + for(int i =1; i<=n ;++i) + { + cin>>v[i]; + } + sort(v+1,v+n+1); + } + + inline bool triunghi (int a, int b, int c) + { + if(a+b>=c and a+c>=b and b+c>=a)return true; + return false; + } + + int caut_pozitia( int ST, int DR ) + { int latura1=v[ST]; + int latura2=v[DR]; + int poz = -1; + // cea mai din dreapta care merge sa fac un triunghi + int st=DR+1 , dr=n; + while(st<=dr ) + { + int m = (st+dr)>>1; + if(triunghi(latura1,latura2,v[m])) + { + poz=m; + st=m+1; + } + else + { + dr=m-1; + } + } + if(poz==-1)return 0; + return poz-DR; + } + + +int main() +{ + Inputu(); + int rez= 0; + for(int i =1; i <=n-2; ++i) + for(int j =i +1 ; j<=n-1; ++j) + rez+=caut_pozitia(i,j); + freopen("nrtri.out","w",stdout); + cout< +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801], n; +int caut(int x, int p) +{ int u = n, poz = -1; + while(p <= u) + { int m = (p+u) / 2; + if(x >= v[m]) + { poz = m; + p = m+1; + } + else u = m-1; + } + return poz; +} +int main() +{ int p, r = 0; + in >> n; + for(int i = 1; i <= n; i++) in >> v[i]; + sort(v+1, v+n+1); + for(int i = 1; i < n; i++) + { for(int j = i+1; j <= n; j++) + { p = caut(v[i]+v[j], j); + if(p != -1) r += p-j; + } + } + out << r; + out.close(); return 0; +} diff --git a/nrtri/binary_search/9f9c06ea-409f-11ec-be10-dc7196033e92.cpp b/nrtri/binary_search/9f9c06ea-409f-11ec-be10-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aca3c1de79af83491b25a58e2ea831e1834ea7c9 --- /dev/null +++ b/nrtri/binary_search/9f9c06ea-409f-11ec-be10-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +using namespace std; +int a[805]; +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int i,j,n; + int suma,left,right,mid; + int ans,contor = 0; + f>>n; + for (i=1; i<=n; i++) + f>>a[i]; + sort(a+1,a+n+1); + for (i=1; i a[j] - a[i] + 1) + { + contor += ans - j; + ///g<< i << " " << " " << j << " " << ans << "\n"; + }*/ + } + } + g << contor; + return 0; +} diff --git a/nrtri/binary_search/9fcf3730-409f-11ec-9934-dc7196033e92.cpp b/nrtri/binary_search/9fcf3730-409f-11ec-9934-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e48a46309e06dd26dbb8f6e66c375d6be76c1573 --- /dev/null +++ b/nrtri/binary_search/9fcf3730-409f-11ec-9934-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,i=1,j,v[1000],f,d,nr; + +int cb(int st,int dr,int x) +{ + int rez=-2,m; + while(st<=dr) + { + m=(st+dr)/2; + if(v[m]>x) dr=m-1; + else if(v[m]<=x) + { + st=m+1; + rez=m; + } + } + + + return rez; +} + +int main() +{ + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + + sort(v+1,v+n+1); + + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + d=v[i]+v[j]; + nr=cb(j+1,n,d); + + if(nr!=-2) + f+=nr-j; + + + } + + + fout< +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); + int v[801] ; + int cbin(int st, int dr, int x) + { int rasp=-1,mij ; + while(st<=dr) + { + mij=(st+dr)/2 ; + if(v[mij]>x) dr=mij-1 ; + if(v[mij]<=x) + { + rasp=mij ; + st=mij+1 ; + } + } + return rasp ; + } + int main() + { + int i,n,p,nr=0,j ; + fin>>n ; + for(i=1;i<=n;i++) fin>>v[i] ; + sort(v+1,v+n+1) ; + + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + p=cbin(j+1,n,v[i]+v[j]) ; + if(p!=-1) nr=nr+p-j ; + } + fout< +#include +using namespace std; + +const int L = 9 ; +int v[801] ; +int cnt ,n ; +int cautbin ( int x ) +{ + int r = 0, pas = 1< j) + cnt += k - j ; + } + } + printf("%d",cnt) ; + return 0; +} diff --git a/nrtri/binary_search/a0a84920-409f-11ec-9113-dc7196033e92.cpp b/nrtri/binary_search/a0a84920-409f-11ec-9113-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d15ba395d82d095d22c5be61319530b5c239b99a --- /dev/null +++ b/nrtri/binary_search/a0a84920-409f-11ec-9113-dc7196033e92.cpp @@ -0,0 +1,51 @@ +#include +#include + +using namespace std; + +int const L = 9; +int v[801]; +int n; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int cautbin ( int x ) +{ + int r = 0, pas = 1<> n; + for ( int i = 0; i < n; i++ ) + { + fin >> v[i]; + } + sort ( v, v + n ); + for ( int i = 0; i < n - 2; i++ ) + { + for ( int j = i + 1; j < n - 1; j++ ) + { + k = cautbin ( v[i] + v[j] ); + if ( k > j) + { + rez += k - j; + } + } + } + fout << rez; + fin.close(); + fout.close(); + return 0; +} diff --git a/nrtri/binary_search/a0dd483a-409f-11ec-a27b-dc7196033e92.cpp b/nrtri/binary_search/a0dd483a-409f-11ec-a27b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5b36083f99401268fbcaeba216c68d0d6f29402 --- /dev/null +++ b/nrtri/binary_search/a0dd483a-409f-11ec-a27b-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[801]; +int n; +const int L=9; +int cautbin(int x) +{ + int r=0, pas=1<>n; + for(int i=0; i>v[i]; + } + sort(v, v+n); + for(int i=0; ij) + { + rez+=k-j; + } + } + } + out< +#include +#include +using namespace std; +int v[1001]; +int cautbin(int x,int n) +{ + int p=1,u=n,poz=-1,m; + while(p<=u) + { + m=(p+u)/2; + if(x>=v[m]) + { + poz=m; + p=m+1; + } + else + u=m-1; + } + return poz; +} + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,nr=0,i,j,poz; + scanf("%d",&n); + for(i=1;i<=n;i++) + scanf("%d",&v[i]); + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + { + poz=cautbin(v[i]+v[j],n); + if(poz!=-1) + nr+=poz-j; + } + cout< +#include + +using namespace std; + +int const L = 9; +int v[801]; +int n; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int cautbin ( int x ) +{ + int r = 0, pas = 1<> n; + for ( int i = 0; i < n; i++ ) + { + fin >> v[i]; + } + sort ( v, v + n ); + for ( int i = 0; i < n - 2; i++ ) + { + for ( int j = i + 1; j < n - 1; j++ ) + { + k = cautbin ( v[i] + v[j] ); + if ( k > j) + { + rez += k - j; + } + } + } + fout << rez; + fin.close(); + fout.close(); + return 0; +} + diff --git a/nrtri/binary_search/a175473a-409f-11ec-91fb-dc7196033e92.cpp b/nrtri/binary_search/a175473a-409f-11ec-91fb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f6ff0adaee514f58b0b1e550140fe5a7ae97501 --- /dev/null +++ b/nrtri/binary_search/a175473a-409f-11ec-91fb-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include +#include + +using namespace std; +const int L=9; +const int N = 800; + +int n, v[N]; + +int cb(int k){ + int r=0, pas=1<>n; + rez=0; + + for(int i=0; i>v[i]; + } + sort(v, v+n); + for(int i=0; ij){ + rez+=k-j; + } + } + } + out< +#include +using namespace std; + +int v[10001]; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int cautbin(int x,int n) +{ + int p=1,u=n,poz=-1,m; + while(p<=u) + { + m=(p+u)/2; + if(x>=v[m]) + { + poz=m; + p=m+1; + } + else + u=m-1; + } + return poz; +} + +int main() +{ + int n, poz=0, nr=0; + f>>n; + for(int i=1;i<=n;++i) f>>v[i]; + sort(v+1 ,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + { + poz=cautbin(v[i]+v[j],n); + if(poz!=-1) nr=nr+poz-j; + } + g< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int v[1001], n; + +int cautbin(int x) +{ + int p = 1, u = n, poz = -1; + while(p <= u) + { + int m = (p + u) / 2; + if(x >= v[m]) + { + poz = m; + p = m + 1; + } + else + u = m - 1; + } + return poz; +} + +int main() +{ + int nr = 0; + f >> n; + for(int i = 1; i <= n; i++) + f >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n - 2; i++) + for(int j = i + 1; j <= n - 1; j++) + { + int poz = cautbin(v[i] + v[j]); + if(poz != -1) + nr += poz - j; + } + g << nr << endl; + return 0; +} diff --git a/nrtri/binary_search/a27ade4e-409f-11ec-8586-dc7196033e92.cpp b/nrtri/binary_search/a27ade4e-409f-11ec-8586-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad720c0c725c8ea91f8db0c4457ec15daff6e91f --- /dev/null +++ b/nrtri/binary_search/a27ade4e-409f-11ec-8586-dc7196033e92.cpp @@ -0,0 +1,75 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int nr, a[808], n; + +int divide(int p, int q) +{ + int st = p, dr = q, x = a[p]; + while(st < dr){ + while(st < dr && a[dr] >= x) + dr--; + a[st] = a[dr]; + while(st < dr && a[st] <= x) + st++; + a[dr] = a[st]; + } + a[st] = x; + return st; +} + +void QSort(int p,int q) +{ + int m = divide(p,q); + if(m - 1 > p) + QSort(p, m - 1); + if(m + 1 < q) + QSort(m + 1, q); +} + +void afisare() +{ + for(int i = 1;i <= n;i ++) + cout<>n; + for(int i = 1;i <= n;i ++) + f>>a[i]; + QSort(1, n); +} + +int bsearch(int x,int s) +{ + int dr = n, st = s, mid; + while(st<=dr) + { + mid = (st + dr) / 2; + if(a[mid] > x) + dr = mid - 1; + else st = mid + 1; + } + return dr - s; +} + +void solve() +{ + for(int i = 1; i <= n - 2;i++) + for(int j = i + 1;j <= n - 1;j++) + nr+=bsearch(a[i] + a[j],j); +} + +int main() +{ + citeste(); + solve(); + g< +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, sol, v[805]; +void read() +{ + f >> n ; + for ( int i = 1 ;i <= n ;i ++ ) + f >> v[i]; + sort(v+1,v+n+1); +} +int bsearch(int val,int s) +{ + int dr=n,st=s; + int mj=0; + while(st <= dr) + { + mj=(st+dr)/2; + if (v[mj]>val) + { + dr=mj-1; + } + else + st=mj+1; + } + return dr-s; +} +void solve() +{ + for ( int i = 1 ; i <= n -2 ; i ++ ) + for ( int j = i + 1 ; j <= n-1 ; j ++ ) + { + sol+=bsearch(v[i]+v[j],j); + } + g << sol; +} +int main() +{ + read(); + solve(); + return 0; +} diff --git a/nrtri/binary_search/a36ee5fa-409f-11ec-8e0e-dc7196033e92.cpp b/nrtri/binary_search/a36ee5fa-409f-11ec-8e0e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cd49d08d9b38ba5816cdb29fea45e469728b86c --- /dev/null +++ b/nrtri/binary_search/a36ee5fa-409f-11ec-8e0e-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, v[3001], s, i, j, st, dr, sol, mid; +int main() +{ + fin >> n; + for (i=1; i<=n; i++) + fin >> v[i]; + sort(v + 1, v + n + 1); + for (i=1; i<=n-2; i++) + { + for (j=i+1; j<=n-1; j++) + { + s = v[i] + v[j]; + st = j + 1; + dr = n; + while (st <= dr) + { + mid = (st + dr)/2; + if (s >= v[mid]) + st = mid + 1; + else + dr = mid - 1; + } + sol = sol + dr - j; + } + } + fout << sol; + return 0; +} diff --git a/nrtri/binary_search/a3a08734-409f-11ec-98b4-dc7196033e92.cpp b/nrtri/binary_search/a3a08734-409f-11ec-98b4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5de8bfc8b2233f158859857110c2e6f3f8138cbb --- /dev/null +++ b/nrtri/binary_search/a3a08734-409f-11ec-98b4-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +#include +using namespace std; +int n, i, j, k, sol, s, st, dr; +int v[810]; + +int main () { + ifstream fin ("nrtri.in"); + ofstream fout ("nrtri.out"); + fin>>n; + for (i=1;i<=n;i++) + fin>>v[i]; + + sort (v+1, v+n+1); + for (i=1;i<=n-2;i++) { + for (j=i+1;j<=n-1;j++) { + s=v[i]+v[j]; + st=j+1; + dr=n; + while (st<=dr) { + int mid = (st+dr)/2; + if (v[mid]>s) + dr=mid-1; + else + st=mid+1; + } + for (k=j+1;k<=dr;k++) + sol++; + } + } + + fout< +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,i,j,s,st,dr,mijl,sol; +int a[810]; +int main() +{ + f>>n; + for(i=1;i<=n;i++) f>>a[i]; + sort(a+1,a+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++){ + s=a[i]+a[j]; + st=j+1; dr=n; + while(st<=dr){ + mijl=(st+dr)/2; + if(a[mijl]>s) dr=mijl-1; + else st=mijl+1; + } + sol+=(dr-j); + } + g< +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, i,v[801],aux[799], st, dr, mij, sum,j; + +int main() +{ + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + {int s=v[i]+v[j]; + st=j+1; + dr=n; + mij=(st+dr)/2; + while(st<=dr) + { + if(v[mij]<=s)st=mij+1; + else if(v[mij]>s)dr=mij-1; + mij=(st+dr)/2; + } + sum+=dr-j;} + } + g< +#include + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, v[3001], s, i, j, st, dr, sol, mid; +int main() { + fin >> n; + for (i=1; i<=n; i++) + fin >> v[i]; + sort(v + 1, v + n + 1); + for (i=1; i<=n-2; i++){ + for (j=i+1; j<=n-1; j++){ + s = v[i] + v[j]; + st = j + 1; + dr = n; + while (st <= dr){ + mid = (st + dr)/2; + if (s >= v[mid]) + st = mid + 1; + else + dr = mid - 1; + } + sol = sol + dr - j; + } + } + fout << sol; + return 0; +} diff --git a/nrtri/bruteforce/01d3a52f-0342-4f59-9765-5c1bc2ed41cf.cpp b/nrtri/bruteforce/01d3a52f-0342-4f59-9765-5c1bc2ed41cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a212c13e598c4fa65a2a9b90dbf8e6c011ca84 --- /dev/null +++ b/nrtri/bruteforce/01d3a52f-0342-4f59-9765-5c1bc2ed41cf.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int n, v[8005], ans; + +int main() +{ + cin >> n; + for(int i = 1; i <= n; ++i) + cin >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j) + for(int k = j + 1; k <= n; ++k) + if(v[i] + v[j] >= v[k]) + ans++; + else + break; + cout << ans; + return 0; +} diff --git a/nrtri/bruteforce/0416d99e-7f9f-4ae8-8774-0d09340a2bf7.cpp b/nrtri/bruteforce/0416d99e-7f9f-4ae8-8774-0d09340a2bf7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..447b8916579f8e99acef253a12301d6a1ac81bbc --- /dev/null +++ b/nrtri/bruteforce/0416d99e-7f9f-4ae8-8774-0d09340a2bf7.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N, cont; + +int main() +{ + in >> N; + int v[N]; + for(int i = 0; i < N; ++i) + in >> v[i]; + sort(v, v + N); + for(int i = 0; i < N - 2; ++i) + for(int j = i + 1; j < N - 1; ++j) + for(int k = j + 1; k < N; ++k) + if(v[i] <= v[j] + v[k] && v[j] <= v[i] + v[k] && v[k] <= v[i] + v[j]) + ++cont; + else break; + out << cont; + return 0; +} diff --git a/nrtri/bruteforce/0458df5f-71a1-442d-a9c3-01817fbbf6c7.cpp b/nrtri/bruteforce/0458df5f-71a1-442d-a9c3-01817fbbf6c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2be53c95e2a1f294b261dad013bbc30da127e53a --- /dev/null +++ b/nrtri/bruteforce/0458df5f-71a1-442d-a9c3-01817fbbf6c7.cpp @@ -0,0 +1,39 @@ +// Numarare_Triunghiuri.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include +#include + +std::ifstream in("nrtri.in"); +std::ofstream out("nrtri.out"); + +int main() { + long long array[1000]; + long long n, elem; + + in >> n; + for (auto i = 1; i <= n; i++) { + in >> array[i]; + } + + long long counter = 0; + std::sort(array + 1, array + n + 1); + + for (int i = 1; i <= n - 2; i++) { + int k = i + 2; + + for (int j = i + 1; j <= n - 1; j++) { + while (array[i] + array[j] >= array[k] && k <= n) + k++; + + if (k > j) + counter += k - j - 1; + } + + } + out << counter; + + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/05eddeb7-702a-4230-8620-76e6e3b63332.cpp b/nrtri/bruteforce/05eddeb7-702a-4230-8620-76e6e3b63332.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98fc8c609f7fe675aa48bf9c139965d341bd4470 --- /dev/null +++ b/nrtri/bruteforce/05eddeb7-702a-4230-8620-76e6e3b63332.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +int a[800]; + +int main() { + std::ifstream is{"nrtri.in"}; + + int n; + is >> n; + + for (auto i = 0; i < n; ++i) + is >> a[i]; + + std::sort(a, a + n); + + std::int64_t s{}; + + for (auto i = 0; i < n - 2; ++i) + for (auto j = i + 1; j < n - 1; ++j) + for (auto k = j + 1; k < n; ++k) + if (a[i] + a[j] >= a[k]) + ++s; + else + break; + + std::ofstream{"nrtri.out"} << s; +} diff --git a/nrtri/bruteforce/18b881a2-9de7-4dfc-bb9b-7220505f4de7.cpp b/nrtri/bruteforce/18b881a2-9de7-4dfc-bb9b-7220505f4de7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4dce781d367c1fb657958fa7ba13e4ebabea4b2 --- /dev/null +++ b/nrtri/bruteforce/18b881a2-9de7-4dfc-bb9b-7220505f4de7.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream cin ("nrtri.in"); + +ofstream cout ("nrtri.out"); + +const int L =9; +int v[801]; + +int main() + +{ + int n; + long long nr=0; + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i]; + } + sort(v+1,v+n+1); + int k; + for(int i=1;i<=n;i++){ + k=i+1; + int j; + for(j=i+1;jj){ + nr=nr+k-j-1; + } + } + } + cout< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + long long a[1005]; + int contor=0; + long long n; + f>>n; + for (int i = 1; i<=n;i ++) + f>>a[i]; + + sort(a+1,a+n+1); + for (int i=1; i<=n; i++) + for(int j=i+1; j<=n; j++) + for (int k=j+1; k<=n; k++) + if (a[i]+a[j]>=a[k]) + contor++; + else break; + g< + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,k,sum,a[801]; +int main() +{ + fin>>n; + for(i=1; i<=n; i++) + { + fin>>a[i]; + } + sort(a+1,+a+n+1); + for(i=1; i<=n-2; i++) + {k=i+2; + for(j=i+1; j<=n-1; j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + sum+=k-j-1; + } + } + fout< +#include + +using namespace std; + +int caut(int *v, int n, int x) { + int pos, i; + for (pos = 1; pos < n; pos <<= 1); + for (i = 0; pos; pos >>= 1) + if (i + pos <= n - 1 && v[i + pos] <= x) + i += pos; + return i; +} + +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n, l[30000], sol = 0; + scanf("%i", &n); + for (int i = 0; i < n; i++) + scanf("%i", &l[i]); + sort(l, l + n); + for (int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) { + sol += caut(l, n, l[i] + l[j]) - j; + } + printf("%i", sol); + return 0; +} diff --git a/nrtri/bruteforce/272b31b7-1694-4610-b39d-93ed0d37aa25.cpp b/nrtri/bruteforce/272b31b7-1694-4610-b39d-93ed0d37aa25.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0be69add8f00ba664b62329e9b1fe0a2ca1e1143 --- /dev/null +++ b/nrtri/bruteforce/272b31b7-1694-4610-b39d-93ed0d37aa25.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() { + + int b[801], n; + long long sol = 0; + + f>>n; + + for(int i = 0; i < n; i++) { + f>>b[i]; + } + + sort(b, b + n); + + for(int i = 0; i < n - 2; i++) + for(int j = i + 1; j < n - 1; j++) + for(int k = j + 1; k < n; k++){ + if(b[k] <= b[i] + b[j]) sol++; + else break; + } + + + g<< sol; +} \ No newline at end of file diff --git a/nrtri/bruteforce/27681e7c-8d00-4cfe-a263-32d2adee35d5.cpp b/nrtri/bruteforce/27681e7c-8d00-4cfe-a263-32d2adee35d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75820fc7807b935ec11fbcbf0b603049a689d6f8 --- /dev/null +++ b/nrtri/bruteforce/27681e7c-8d00-4cfe-a263-32d2adee35d5.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +const int LMAX = 805; +int v[LMAX], n, ans; + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) + cin >> v[i]; + + sort(v + 1, v + n + 1); + for (int i = 1; i <= n - 2; ++i) + { + int k = i + 1; + for (int j = k; j <= n; ++j) + { + while (k <= n && v[k] <= v[i] + v[j]) + k++; + if (k > j) + ans += k - j - 1; + } + } + cout << ans; + return 0; +} diff --git a/nrtri/bruteforce/276faa1e-8474-4a60-bf87-9b1803f040d2.cpp b/nrtri/bruteforce/276faa1e-8474-4a60-bf87-9b1803f040d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..447b8916579f8e99acef253a12301d6a1ac81bbc --- /dev/null +++ b/nrtri/bruteforce/276faa1e-8474-4a60-bf87-9b1803f040d2.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N, cont; + +int main() +{ + in >> N; + int v[N]; + for(int i = 0; i < N; ++i) + in >> v[i]; + sort(v, v + N); + for(int i = 0; i < N - 2; ++i) + for(int j = i + 1; j < N - 1; ++j) + for(int k = j + 1; k < N; ++k) + if(v[i] <= v[j] + v[k] && v[j] <= v[i] + v[k] && v[k] <= v[i] + v[j]) + ++cont; + else break; + out << cont; + return 0; +} diff --git a/nrtri/bruteforce/27fbcd5e-14bc-4d4b-a4ca-3a8d37285dd3.cpp b/nrtri/bruteforce/27fbcd5e-14bc-4d4b-a4ca-3a8d37285dd3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..516e13bd989047b511e653c8ea78c9d8a7910bb7 --- /dev/null +++ b/nrtri/bruteforce/27fbcd5e-14bc-4d4b-a4ca-3a8d37285dd3.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[30005]; + +int main() +{ + + int n; + fin >> n; + for(int i = 1 ; i <= n ; ++i) + fin >> v[i]; + sort(v + 1 , v + n + 1); + int contor = 0; + for(int i = 1 ; i <= n - 2 ; ++i) + for(int j = i + 1 ; j <= n - 1 ; ++j) + for(int k = j + 1 ; k <= n ; ++k) + if((v[i] <= v[j] + v[k])&&(v[k] <= v[i] + v[j])&&(v[j] <= v[i] + v[k])) + ++contor; + else + break; + fout << contor; +} diff --git a/nrtri/bruteforce/2834504f-3fd3-4174-b218-c00e68498b6e.cpp b/nrtri/bruteforce/2834504f-3fd3-4174-b218-c00e68498b6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d41936e3915ddb3ddb254b225587eaa38081810 --- /dev/null +++ b/nrtri/bruteforce/2834504f-3fd3-4174-b218-c00e68498b6e.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); +int v[801]; +int main() +{ + int n, c = 0, i, i2, i3; + cin >> n; + for (i = 0; i < n; i++) + cin >> v[i]; + sort(v, v + n); + for (i = 0; i < n - 2; i++) + { + for (i2 = i + 1; i2 < n - 1; i2++) + { + for (i3 = i2 + 1; i3 < n; i3++) + { + if (v[i] + v[i2] < v[i3]) + break; + else c++; + } + } + } + cout << c; + return 0; +} diff --git a/nrtri/bruteforce/28704812-7826-4bd4-b343-b3cbee0eb3d3.cpp b/nrtri/bruteforce/28704812-7826-4bd4-b343-b3cbee0eb3d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a7c3006e31a45f3fc0b5d167a0dd17dd1989b9c --- /dev/null +++ b/nrtri/bruteforce/28704812-7826-4bd4-b343-b3cbee0eb3d3.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main() +{ + int n,v[805], c=0; + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+1+n); + for(int i=1;i<=n;i++) + { + for(int j=i+1;j<=n;j++) + { + for(int z=j+1;z<=n;z++) + { + + if(v[i]+v[j]>=v[z]) + c++; + else break; + + } + } + } + out< +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int N = 801; +int v[N]; + +int main() +{ + int n, i, j, e, rasp; + fin >> n; + for(i = 0; i < n; i++) + fin >> v[i]; + + sort(v, v + n); + + rasp = 0; + for(i = 0; i < n - 2; i++){ + queue Q; + for(j = i + 1; j < n; j++){ + while(!Q.empty() && v[i] + Q.front() < v[j]) + Q.pop(); + rasp += Q.size(); + Q.push(v[j]); + } + } + fout << rasp; + return 0; +} diff --git a/nrtri/bruteforce/289a10ed-3cd1-44a4-9acd-e8f43526d6b5.cpp b/nrtri/bruteforce/289a10ed-3cd1-44a4-9acd-e8f43526d6b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..821e08d826d3332db5c09f7e7a5980716aecd162 --- /dev/null +++ b/nrtri/bruteforce/289a10ed-3cd1-44a4-9acd-e8f43526d6b5.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + int main() +{ +int i,v[805],j,k,sum,n,cont; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +cont=0; +fin>>n; +for(i=1;i<=n;i++){ + fin>>v[i]; +} +sort(v+1,v+n+1); +for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + for(k=j+1;k<=n;k++){ + sum=v[i]+v[j]; + if(sum>=v[k]) + cont++; + + else + break; + } +fout< +using namespace std; +ifstream F("nrtri.in"); +ofstream G("nrtri.out"); +int a[800],l,i,j,k,n; +int main() +{ + F>>n; + for(i=0;i>a[i]; + sort(a,a+n); + for(i=0;i=a[k];++k); + l+=k-j-1; + } + G< +#include +using namespace std; +int a[800],l,i,j,k,n; +int main() +{ + freopen("nrtri.in","r",stdin),freopen("nrtri.out","w",stdout),scanf("%d",&n); + for(i=0;i=a[k];++k); + l+=k-j-1; + } + printf("%d",l); + return 0; +} diff --git a/nrtri/bruteforce/29dd5c51-409f-11ec-b55a-dc7196033e92.cpp b/nrtri/bruteforce/29dd5c51-409f-11ec-b55a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2951d240ce0a97479f6af8dd5850d38d1fdaba29 --- /dev/null +++ b/nrtri/bruteforce/29dd5c51-409f-11ec-b55a-dc7196033e92.cpp @@ -0,0 +1,17 @@ +#include +using namespace std; +ifstream F("nrtri.in"); +ofstream G("nrtri.out"); +int a[800],l,i,j,k,n; +int main() +{ + for(F>>n;i>a[i]; + for(sort(a,a+n),i=0;i=a[k];++k); + l+=k-j-1; + } + G< + +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int v[2001]; + +int main() +{ + int n,i,p=0,j,ac,pas; + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + { + pas=1<<9; + ac=j; + while(pas) + { + if(ac+pas<=n) + if(v[i]+v[j]>=v[ac+pas]) + ac=ac+pas; + pas>>=1; + } + p=p+ac-j; + } + out< + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +short int F[30001]; + +int v[800]; + +int main() { + int N, mx = 0; + f >> N; + + for(int i = 1, x;i <= N;i++) { + f >> x, F[x]++; + mx = max(mx, x); + } + + N = 0; + for(int i = 1;i <= mx;i++) + while(F[i] > 0) { + v[++N] = i, F[i]--; + } + + long long cnt = 0; + for(int i = N;i >= 2;i--) { + int l = 1, r = i - 1; + while(l < r) { + if(v[l] + v[r] >= v[i]) { + cnt += r - l; + r--; + } else l++; + } + } + + g << cnt; +} \ No newline at end of file diff --git a/nrtri/bruteforce/2bd74557-409f-11ec-bd12-dc7196033e92.cpp b/nrtri/bruteforce/2bd74557-409f-11ec-bd12-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e7c215813f67245129c567d6d6f0095bf311934 --- /dev/null +++ b/nrtri/bruteforce/2bd74557-409f-11ec-bd12-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; + +int v[801]; +int main() +{ + ifstream cin ("nrtri.in"); + ofstream cout ("nrtri.out"); + + int n, s, cnt=0; + cin>>n; + for(int i=1; i<=n; i++) + { + cin>>v[i]; + } + sort(v+1, v+n+1); + for(int i=1; i=v[k]) + cnt++; + else + break; + } + } + } + cout< +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n,a[801],nr=0; + f>>n; + + for(int i=0;i>a[i]; + + int ok=1; + + while(ok) + { + ok=0; + + for(int i=0;ia[i+1]) + { + int aux=a[i]; + a[i]=a[i+1]; + a[i+1]=aux; + ok=1; + } + } + } + + for(int i=0;i>=1) + { + if(k+lg +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n, a[801]; + +void citire() +{ + fin>>n; + for(int i=0; i>a[i]; + for(int i=0; i>=1) + if(i+lg +#include +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout("nrtri.out"); +int v[1000]; +int main() +{ + int n, cnt=0, st, dr, ok=0, s=0; + cin>>n; + for(int i=1; i<=n; i++) + { + cin>>v[i]; + } + sort(v+1, v+n+1); + for(int i=1; i=v[k]) + cnt++; + else + break; + } + } + } + cout< +#include +#include +using namespace std; + +int v[1000]; +int main() +{ + ifstream cin ("nrtri.in"); + ofstream cout("nrtri.out"); + + int n, cnt=0, st, dr, ok=0, s=0; + cin>>n; + for(int i=1; i<=n; i++) + { + cin>>v[i]; + } + sort(v+1, v+n+1); + /*for(int i=1; i<=n; i++) + { + cout<=1) + { + k=(dr+st)/2; + if(s>=v[k]) + { + cnt++; + st=k; + } + else + { + dr=k; + } + }*/ + for(int k=j+1; k<=n; k++) + { + if(s>=v[k]) + cnt++; + else + break; + } + } + } + cout< +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int nr,i,j,k,n,v[801]; +int main() { + fin>>n; + for(i=1; i<=n; ++i) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1; i=v[k]) + nr++; + else + break; + fout< + +#include + +using namespace std; + +ifstream in ( "nrtri.in" ) ; + +ofstream out ( "nrtri.out" ) ; + +int n, i1, i2, i3, v [ 801 ], k, a, b, c, st, dr, p, mij, i, j, S ; + +bool triunghi ( int a, int b, int c ) +{ + if ( v [ i1 ] + v [ i2 ] >= v [ i3 ] && v [ i3 ] + v [ i2 ] >= v [ i1 ] && v [ i1 ] + v [ i3 ] >= v [ i2 ] ) + return true ; + else return false ; +} +int main() +{ + in >> n ; + /*for ( i1 = 1 ; i1 <= n ; i1 ++ ) + { + in >> v [ i1 ] ; + } + for ( i1 = 1 ; i1 <= n - 2 ; i1 ++ ) + { + for ( i2 = i1 + 1 ; i2 <= n - 1 ; i2 ++ ) + { + for ( i3 = i2 + 1 ; i3 <= n ; i3 ++ ) + { + if ( triunghi ( v [ i1 ] , v [ i2 ] , v [ i3 ] ) ) + k ++ ; + } + } + }*/ + for ( i = 1 ; i <= n ; i ++ ) + { + in >> v [ i ] ; + } + sort ( v + 1, v + n + 1 ) ; + for ( i = 1 ; i <= n - 2 ; i ++ ) + { + for ( j = i + 1 ; j < n ; j ++ ) + { + S = v [ i ] + v [ j ] ; + p = -1 ; + st = j + 1 ; + dr = n ; + mij = ( st + dr ) / 2 ; + while ( st <= dr ) + { + if ( v [ mij ] == S ) + { + p = mij ; + st ++ ; + } + else if ( v [ mij ] < S ) + { + st = mij + 1 ; + } + else + { + dr = mij - 1 ; + } + mij = ( st + dr ) / 2 ; + } + if ( p == -1 ) + p = dr ; + k = k + p - j ; + } + } + out << k ; + return 0; +} diff --git a/nrtri/bruteforce/2f6d07b5-409f-11ec-bd88-dc7196033e92.cpp b/nrtri/bruteforce/2f6d07b5-409f-11ec-bd88-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd4a4084e786e5b5e2048eb7bc1220bf923f653a --- /dev/null +++ b/nrtri/bruteforce/2f6d07b5-409f-11ec-bd88-dc7196033e92.cpp @@ -0,0 +1,64 @@ +#include //Andrei Alexandru a.k.a Sho +#define ll long long +#define double long double +#pragma GCC optimize("O3") +#pragma GCC optimize("Ofast") +#define aint(a) (a).begin(), (a).end() +#define f first +#define s second +#define pb push_back +#define mp make_pair +#define pi pair +#define rc(s) return cout<>n; +ll mx=0; +for(ll i=1;i<=n;i++) +{ + cin>>a[i]; + update(a[i],1ll); + mx=max(mx,a[i]); +} +ll ans=0; +sort(a+1,a+n+1); +for(ll i=1;i<=n;i++) +{ + for(ll j=i+1;j<=n;j++) + { + update(a[i],-1ll); + update(a[j],-1ll); + ll x=a[i]+a[j]; + x=min(x,mx); + ans+=query(x); + if(a[j]-a[i]>=2) + ans-=query((a[j]-a[i]-1)); + update(a[i],1ll); + update(a[j],1ll); + } +} +cout< +int a[900],n,x,i,j,k,r;main(){std::ifstream f{"nrtri.in"};for(f>>n;x>a[x++]);std::sort(a,a+n);for(;i +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int s[100004]; +int n,m,t,x, i; + +int main(){ + fin >> n; + for (int i = 0; i> s[i]; + } + sort(s, s+n); + + for (int i = 0; i +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int v[1005]; +int main() +{ + int n,i,s=0,j,ok; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + ok=i+2; + for(j=i+1;j<=n-1;j++) + { + while(ok<=n && v[ok]<=v[i]+v[j]) + ok=ok+1; + if(ok>j) + s=s+ok-j-1; + } + } + fout< +#include +#include +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,cnt; +int v[816]; + +int main() +{ + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int v[300001],n,i,j,k,nr; + f>>n; + nr = 0; + for(i=0; i>v[i]; + } + sort(v,v+n); + for(i=0; i=v[k]; k++) + { + nr++; + } + } + } + g< +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int v[1005]; +int main() +{ + int n,i,s=0,j,ok; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + ok=i+2; + for(j=i+1;j<=n-1;j++) + { + while(ok<=n && v[ok]<=v[i]+v[j]) + ok=ok+1; + if(ok>j) + s=s+ok-j-1; + } + } + fout< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,c,i,j,k,v[2001]; + +int main() +{ + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + { + for(j=i+1; j<=n-1; j++) + { + for(k=j+1; k<=n; k++) + { + if(v[k]<=v[i]+v[j]) + c++; + else + break; + } + } + } + out< + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,k,nr,a[801]; +int main() +{ + fin>>n; + for(int i=1;i<=n;i++) + fin>>a[i]; + sort(a+1,a+n+1); + for(int i=1;i<=n-2;i++) + { + k=i+2; + for(int j=i+1;j<=n-1;j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + nr+=k-j-1; + } + } + fout << nr <<'\n'; + return 0; +} diff --git a/nrtri/bruteforce/36889ade-409f-11ec-9b38-dc7196033e92.cpp b/nrtri/bruteforce/36889ade-409f-11ec-9b38-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2a4080592e08e8794a412254601e5c505e7cd4d --- /dev/null +++ b/nrtri/bruteforce/36889ade-409f-11ec-9b38-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; +int v[2000],put; + +int main(){ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + + int n, i, j, k, cnt; + f>>n; + for(i=0; i>v[i]; + } + sort(v, v+n); + cnt = 0; + + for (k = n - 1; k >= 2; k--) { + i = 0; + j = k - 1; + while (i < j) { + if (v[i] + v[j] >= v[k]) { + cnt += j - i; + j--; + } + else { + i++; + } + } + } + g << cnt << '\n'; + f.close(); + g.close(); + return 0; +} diff --git a/nrtri/bruteforce/36bbe67a-409f-11ec-a38f-dc7196033e92.cpp b/nrtri/bruteforce/36bbe67a-409f-11ec-a38f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d6e3809110225a0b12d633e945d94be6eb35067 --- /dev/null +++ b/nrtri/bruteforce/36bbe67a-409f-11ec-a38f-dc7196033e92.cpp @@ -0,0 +1,57 @@ +#include +#define N 805 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int Triunghi(int a, int b, int c) +{ + if(c<= a+b ) + return 1; + return 0; +} + +void Sortare(int n, int a[]) +{ + int i, ordo=0; + int aux; + while(ordo==0) + { + ordo=1; + for(i=1; i<=n-1; i++) + if(a[i]>a[i+1]) + { + aux=a[i]; + a[i]=a[i+1]; + a[i+1]=aux; + ordo=0; + } + } +} + + +int main() +{ + int n, a[N], i, j, ct=0, k; + fin >> n; + for(i=1; i<=n ; i++) + fin>>a[i]; + + Sortare(n, a); + for(i=1; i<=n-2; i++) + { + k=i+2; + for(j=i+1; j<=n-1; j++) + { + + while(Triunghi(a[i], a[j], a[k])==1 && k<=n) + k++; + if (k>j) + ct=ct+k-j-1; + } + } + + fout< + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,k,sum,a[801]; +int main() +{ + fin>>n; + for(i=1; i<=n; i++) + { + fin>>a[i]; + } + sort(a+1,+a+n+1); + for(i=1; i<=n-2; i++) + {k=i+2; + for(j=i+1; j<=n-1; j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + sum+=k-j-1; + } + } + fout< +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n, k, i, j, ans, a[805]; +int main() { + fin >> n; + for (int i=1;i<=n;i++){ + fin >> a[i]; + } + sort(a+1,a+n+1); + for (i=1;i<=n-2;i++){ + k = i+2; + for (j=i+1;j<=n-1;j++){ + while(k<=n && a[k] <= a[i] + a[j]) + ++k; + if (k>j) + ans += k - j-1; + } + } + + fout << ans << '\n'; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/396fb71e-3a13-4a69-bdd4-081add50bc1d.cpp b/nrtri/bruteforce/396fb71e-3a13-4a69-bdd4-081add50bc1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91e9b234a3c86461b47bfc38859fffe1e44065df --- /dev/null +++ b/nrtri/bruteforce/396fb71e-3a13-4a69-bdd4-081add50bc1d.cpp @@ -0,0 +1,85 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +/*int Triangle (int a, int b, int c) +{ + return ((a+b>=c) && (b+c>=a) && (a+c>=b)); +} */ +void Swap(int &a, int &b) +{ + int temp; + temp = b; + b=a; + a = temp; +} +void BubbleSort (int *v, int n) // v[] +{ + int i, j; + + for (i=1; i<=n; i++) + { + for (j=1; jv[j+1]) + { + Swap (v[j],v[j+1]); + } + } + } +} + +int main() +{ + int N, V[803], contor=0; + cin>> N; + + for(int i=1; i<=N; i++) + cin>> V[i]; + + BubbleSort(V,N); + + //for (int i=1; i=V[mijloc]) + { + stanga=mijloc+1; + k=mijloc; + } + else + { + dreapta=mijloc-1; + } + } + contor+=k-j; + } + + } + + cout< +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,i,v[1000],j,ii,rez; +int main() +{ + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j=v[ii];ii++) + { + rez++; + } + } + } + out< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,a[900],s=0; + +int main() +{ + in >> n; + for(int i=1; i<=n; i++) + in >> a[i]; + sort(a+1,a+n+1); + for(int i=1; i0){ + if(b+x <= n && a[b+x]<=sum) + x+=b; + b/=2; + } + s+=x-j; + } + } + out << s; + return 0; +} diff --git a/nrtri/bruteforce/3bd22e7b-409f-11ec-ad10-dc7196033e92.cpp b/nrtri/bruteforce/3bd22e7b-409f-11ec-ad10-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..419e10278112171f8e70073cd867ecd68602c715 --- /dev/null +++ b/nrtri/bruteforce/3bd22e7b-409f-11ec-ad10-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +int main() +{ + FILE * fin = fopen("nrtri.in", "r"); + FILE * fout = fopen("nrtri.out", "w"); + + int v[801], n=0; + fscanf(fin, "%d", &n); + for (int i=0; i=v[k]); k++){ + tri++; + } + } + } + + fprintf(fout, "%d", tri); + + fclose(fin); + fclose(fout); +} diff --git a/nrtri/bruteforce/3d2c7abc-409f-11ec-86df-dc7196033e92.cpp b/nrtri/bruteforce/3d2c7abc-409f-11ec-86df-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d1115f2a207d71b93a9a71f1edb7c14e13eadb6 --- /dev/null +++ b/nrtri/bruteforce/3d2c7abc-409f-11ec-86df-dc7196033e92.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; +int i,j,n,v[805],sol,k; +int main() +{ +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) { + for (j=i+1;jv[i]+v[j]) sol+=k-j-1; + else sol+=n-j; + } + else if (v[k]>v[i]+v[j]) sol+=k-j-1; + } + } + g< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[805]; + +int main() +{ + int n,rez=0; + in>>n; + for(int i=1; i>v[i]; + } + sort(v+1,v+n+1); + int k; + for(int i=1; i<=n; i++) + { + k=i+1; + for(int j=i+1; jj) + { + rez+=k-j-1; + } + } + } + out< +#include +#include +#include + +#define Nmax 805 + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +vector < int > Numere; + +int aux; + +void Read() +{ + + int n; + fin >> n; + aux = n; + while(n --) + { + int x; + fin >> x; + Numere.push_back(x); + } + sort(Numere.begin(), Numere.end()); + +} + +int main() +{ + Read(); + int raspuns = 0; + for(int k = aux - 1; k > 1; k --) + { + //Verificam mereu primul numar cu toate numerele plecand de la final -1 de fiecare data + int i = 0; + int j = k - 1; + while(i < j) + { + int nr1 = Numere[i]; + int nr2 = Numere[j]; + int nr3 = Numere[k]; + if(Numere[i] + Numere[j] < Numere[k]) + i ++; + else + { + raspuns += j - i; + j --; + } + } + } + + fout << raspuns; + + return 0; +} diff --git a/nrtri/bruteforce/3dea5211-4f67-4455-879c-a696e0ab76d2.cpp b/nrtri/bruteforce/3dea5211-4f67-4455-879c-a696e0ab76d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca7cf1d60041450671b6cd95b2aa4d5e0b614e52 --- /dev/null +++ b/nrtri/bruteforce/3dea5211-4f67-4455-879c-a696e0ab76d2.cpp @@ -0,0 +1,32 @@ +#include + +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int v[801]; + +int main() +{ + int cnt=0,i,j,k,n; + in>>n; + for (i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for (j=i+1;j<=n-1;j++) + { + for (k=j+1;k<=n;k++) + { + if (v[k]<=v[i]+v[j]) + cnt++; + else + break; + } + } + } + out< +#include +using namespace std; +int v[800]; +int main() +{ + int n, i, j, k, rasp; + + ifstream f ("nrtri.in"); + ofstream g ("nrtri.out"); + + f >> n; + + for (i = 0; i < n; i++){ + f >> v[i]; + } + + sort(v, v + n); + + rasp = 0; + for (k = n - 1; k > 1; k--){ + i = 0; + j = k - 1; + while(i < j){ + if (v[i] + v[j] < v[k]){ + i++; + }else{ + rasp += j - i; + j--; + } + } + } + + g << rasp; + + f.close(); + g.close(); + return 0; +} diff --git a/nrtri/bruteforce/3e5776a8-409f-11ec-b1c5-dc7196033e92.cpp b/nrtri/bruteforce/3e5776a8-409f-11ec-b1c5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e51847e0ba7fc86c127629be88f3572957bc6167 --- /dev/null +++ b/nrtri/bruteforce/3e5776a8-409f-11ec-b1c5-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +using namespace std; + +int v[800],put; + + +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n, i, j, k, cnt=0; + f>>n; + for(i=0; i>v[i]; + } + sort(v, v+n); + + for(k=n-1;k>1;k--){ + i=0; + j=k-1; + while(i=v[k]) { + cnt+=j-i; + j--; + } + } + } + + + g< +#include + +using namespace std; + +int main() { + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n,sir[801],contor=0,ok=1; + f>>n; + for(int i=1;i<=n;i++) { + f>>sir[i]; + } + sort(sir+1,sir+n+1); + for(int i=1;i<=n-2;i++){ + for(int j=i+1;j<=n-1;j++){ + ok=1; + for(int k=j+1;k<=n && ok;k++){ + if(sir[i]+sir[j]>=sir[k]){ + contor++; + }else{ + ok=0; + } + } + } + } + g< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, i, k, j, v[30001], rez=0; + f >> n; + + for(int i = 0; i < n; i ++) + f >> v[i]; + + sort(v, v + n); + + for(i = 0; i < n - 2; i ++) + for(k = i + 1; k < n - 1; k ++) + { + j = k; + while((j < n - 1) and (v[i] + v[k]) >= v[j + 1]) + { + rez++; + j++; + } + + } + + + g << rez; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/40118556-5fe8-4716-b3ef-87ffa206e276.cpp b/nrtri/bruteforce/40118556-5fe8-4716-b3ef-87ffa206e276.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4819425d4ea1ddef759932c573c8c8adfdd478d5 --- /dev/null +++ b/nrtri/bruteforce/40118556-5fe8-4716-b3ef-87ffa206e276.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[100000]; +int main() +{ + int n,i,j,k,nr_triunghiuri=0; + fin>>n; + + for(i=0;i>v[i]; + } + //sortam vectorul ca sa avem laturile in ordine crescatoare pentru a verifica daca suma a doua laturi este mai mare decat o a treia latura + sort(v,v+n); + //prima latura + for(i=0;i=v[k]) + nr_triunghiuri++; + else break; + } + } + } + fout< +#include +using namespace std; + +FILE * fin, * fout; + +int v[800]; + +int main(){ + fin = fopen("nrtri.in","r"); + fout = fopen("nrtri.out","w"); + + int N; + + fscanf(fin,"%d",&N); + + for ( int i = 0; i < N; i++ ){ + fscanf(fin,"%d",v+i); + } + + std::sort(v, v+N); + + int cases = 0; + + for( int i = 0; i < N - 2; i++ ){ + for( int j = i+1; j < N - 1; j++ ){ + for( int k = j+1; k < N; k++ ){ + if(v[i] + v[j] >= v[k]) + cases++; + else + break; + + } + } + } + + //cout << cases; + fprintf(fout,"%d",cases); + + fclose(fin); + fclose(fout); + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/40c776cb-409f-11ec-8d54-dc7196033e92.cpp b/nrtri/bruteforce/40c776cb-409f-11ec-8d54-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99d2eab370cf8b9868c4c6bcfb4beac38a765122 --- /dev/null +++ b/nrtri/bruteforce/40c776cb-409f-11ec-8d54-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, i, k, j, v[30001], nrtri=0; + f >> n; + + for(int i = 0; i < n; i ++) + f >> v[i]; + + sort(v, v + n); + + for(i = 0; i < n - 2; i ++) + for(k = i + 1; k < n - 1; k ++) + { + j = k; + while((j < n - 1) and (v[i] + v[k]) >= v[j + 1]) + { + nrtri++; + j++; + } + + } + + + g << nrtri; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/40fd06b8-409f-11ec-9021-dc7196033e92.cpp b/nrtri/bruteforce/40fd06b8-409f-11ec-9021-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57d8bd32500476d83a4d3a223614dffedc49e0c6 --- /dev/null +++ b/nrtri/bruteforce/40fd06b8-409f-11ec-9021-dc7196033e92.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; + +int main() +{ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n, l[801], nrtri = 0, i, j, k; +in >> n; +for (int i = 0 ; i < n ; ++i) + in >> l[i]; +in.close(); +sort(l,l+n); +for (i = 0 ; i < n-2 ; i++) + for (j = i+1 ; j < n-1 ; j++) + for (k = j+1 ; k < n; k++) + if ((l[i] + l[j]) >= l[k] && (l[i] + l[k]) >= l[j] && (l[j] + l[k]) >= l[i]) + nrtri++; + else break; +out << nrtri; +out.close(); + +} diff --git a/nrtri/bruteforce/41b9009e-409f-11ec-8d0c-dc7196033e92.cpp b/nrtri/bruteforce/41b9009e-409f-11ec-8d0c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..565c6a17c0e8dfca637d93911a14097c5efad961 --- /dev/null +++ b/nrtri/bruteforce/41b9009e-409f-11ec-8d0c-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int n; + fin >> n; + vector v(n); + int cnt = 0; + for(auto &it:v) + fin >> it; + sort(v.begin(), v.end()); + for(int i = 0; i <= n - 3; ++i) + for(int j = i + 1; j <= n - 2; ++j) + for(int k = j + 1; k < n; ++k) + if(v[i] + v[j] >= v[k]) + cnt++; + else + break; + fout << cnt; + return 0; +} diff --git a/nrtri/bruteforce/42316e10-409f-11ec-a9a1-dc7196033e92.cpp b/nrtri/bruteforce/42316e10-409f-11ec-a9a1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5acf2b9c8428e8fd01d5b9decc22e33d8805f1b0 --- /dev/null +++ b/nrtri/bruteforce/42316e10-409f-11ec-a9a1-dc7196033e92.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n, i, j, k, nrtri = 0; + fin >> n; + vector v(n); + for(auto &it:v) + fin >> it; + sort(v.begin(), v.end()); + for(i = 0; i < n - 2; ++i) + for(j = i + 1; j < n - 1; ++j) + for(k = j + 1; k < n; ++k) + if(v[i] + v[j] >= v[k]) + nrtri++; + else + break; + fout << nrtri; + return 0; +} diff --git a/nrtri/bruteforce/4276371b-409f-11ec-a9a7-dc7196033e92.cpp b/nrtri/bruteforce/4276371b-409f-11ec-a9a7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b15d9a1d268178a10ac6d7351b524e6ace9afd20 --- /dev/null +++ b/nrtri/bruteforce/4276371b-409f-11ec-a9a7-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + + +int main() +{ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + + int n; + in >> n; + + vector v(n); + for (auto & i : v) + in >> i; + + sort(v.begin(), v.end()); + + int ans = 0; + + for (int i = 0; i < (int)v.size(); i++) { + for (int j = 0; j < i; j++) { + for (int k = j - 1; k >= 0; k--) { + if (v[j] + v[k] >= v[i]) + ans++; + else + break; + } + } + } + out << ans << '\n'; + + return 0; +} diff --git a/nrtri/bruteforce/42e31fee-409f-11ec-9d0e-dc7196033e92.cpp b/nrtri/bruteforce/42e31fee-409f-11ec-9d0e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2af0e90f0486b7d4a8b7a73a91ce3bfb6da559 --- /dev/null +++ b/nrtri/bruteforce/42e31fee-409f-11ec-9d0e-dc7196033e92.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, v[801], s=0; + f>>n; + for(int i=0; i>v[i]; + sort(v,v+n); + for(int i=0; i=v[k]) + s++; + else break; + g< +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,v[1000],i,j,k,nr; +int main() +{ + fin>>n; + for(i=0;i>v[i]; +sort(v,v+n); + + for(i=0;i= v[k]) + nr ++; + else + break; + + fout< +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + + +int main() +{ + int n, v[801], i, j, k, nr = 0; + + f>>n; + for(i=0;i>v[i]; + + sort(v,v+n); + + for(i=0;i= v[k]) + nr ++; + else + break; + + g< + +#include + + + +using namespace std; + + + +ifstream f("nrtri.in"); + +ofstream g("nrtri.out"); + + + +int n, v[801], i, j, k, nr = 0; + + + +int main() + +{ + + f>>n; + + for(i=0;i>v[i]; + + + + sort(v,v+n); + + + + for(i=0;i= v[k]) + + nr ++; + + else + + break; + + + + g< +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + + +int main() +{ + int n, v[801], i, j, k, nr = 0; + + f>>n; + for(i=0;i>v[i]; + + sort(v,v+n); + + for(i=0;i= v[k]) + nr ++; + else + break; + + g< +#include +#include +#define Maxx 1001 + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +long long N, A[Maxx],cnt; + +int main() +{ + f >> N; + for ( int i=1;i<=N;i++ ) + f >> A[i]; + sort ( A+1,A+N+1 ); + for ( int i=1;i<=N-2;i++ ) + for ( int j=1+i;j<=N-1;j++ ) + for ( int k=1+j;k<=N && A[k]<= A[i]+A[j]; k++ ) + cnt++; + g << cnt; + return 0; +} diff --git a/nrtri/bruteforce/44b09ed1-409f-11ec-b50c-dc7196033e92.cpp b/nrtri/bruteforce/44b09ed1-409f-11ec-b50c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2be53c95e2a1f294b261dad013bbc30da127e53a --- /dev/null +++ b/nrtri/bruteforce/44b09ed1-409f-11ec-b50c-dc7196033e92.cpp @@ -0,0 +1,39 @@ +// Numarare_Triunghiuri.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include +#include +#include +#include + +std::ifstream in("nrtri.in"); +std::ofstream out("nrtri.out"); + +int main() { + long long array[1000]; + long long n, elem; + + in >> n; + for (auto i = 1; i <= n; i++) { + in >> array[i]; + } + + long long counter = 0; + std::sort(array + 1, array + n + 1); + + for (int i = 1; i <= n - 2; i++) { + int k = i + 2; + + for (int j = i + 1; j <= n - 1; j++) { + while (array[i] + array[j] >= array[k] && k <= n) + k++; + + if (k > j) + counter += k - j - 1; + } + + } + out << counter; + + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/44e40bbd-409f-11ec-98d3-dc7196033e92.cpp b/nrtri/bruteforce/44e40bbd-409f-11ec-98d3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82528c5157f8e98de624001108dc61093fbd1706 --- /dev/null +++ b/nrtri/bruteforce/44e40bbd-409f-11ec-98d3-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, v[801], s; + +int main() +{ + f>>n; + for(int i=0; i>v[i]; + sort(v, v+n); + for(int x=0; x +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n, v[801], i, j, k, nr = 0; + +int main() +{ + f>>n; + for(i=0;i>v[i]; + + sort(v,v+n); + + for(i=0;i= v[k]) + nr ++; + else + break; + + g< +#include +using namespace std; +void interclasare(int a[], int i, int j,int m) +{ + + int aux[i+j-1]; + int p=i; + int q=m+1; + int k=0; + while(p<=m && q<=j) + { + if(a[p]c + ///iterand prin lista ordonata,vom cauta cel mai mare c pentru care conditia ete satisfacuta + ///evident, toate numerele mai mici decat acest c gasit vor respecta de asemenea conditia-> + /// o sa pot sa formeze triunghiuri cu elementele de pe poz i si j + while(k=a[k])k++; + if(j)nr+=k-j-1; + + j++; + if(j==N) + { + ///am terminat de verificat elementul i cu latura de pe pozitia j, j+1,.... + /// iteram la urmatorul element care sa fie prima latura a triunghiului + /// atentie: trebuie sa schimbam si j-ul in functie de noul i + i++; + j=i+1; + } + + } + return nr; + +} +int main() +{ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int a[801]; + int N; + in>>N; + for(int i=0;i>a[i]; + mergesort(a,0,N-1); + out< +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[100001]; +int main() +{ + int n, i, j, k, cnt = 0; + in>>n; + + for (i=1; i<=n; ++i) + { + in >> v[i]; + } + + sort(v, v+n+1); + + for (i=1; i<=n-2; ++i) + { + for (j=i+1; j<=n-1; ++j) + { + for (k=j+1; k<=n; ++k) + { + if ((v[i]+v[j]) >= v[k]) + cnt+=1; + else break; + } + } + } + out << cnt; + + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/45bec49d-409f-11ec-9f93-dc7196033e92.cpp b/nrtri/bruteforce/45bec49d-409f-11ec-9f93-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4819425d4ea1ddef759932c573c8c8adfdd478d5 --- /dev/null +++ b/nrtri/bruteforce/45bec49d-409f-11ec-9f93-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[100000]; +int main() +{ + int n,i,j,k,nr_triunghiuri=0; + fin>>n; + + for(i=0;i>v[i]; + } + //sortam vectorul ca sa avem laturile in ordine crescatoare pentru a verifica daca suma a doua laturi este mai mare decat o a treia latura + sort(v,v+n); + //prima latura + for(i=0;i=v[k]) + nr_triunghiuri++; + else break; + } + } + } + fout< +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int v[300001],n,i,j,k,nr; + f>>n; + nr = 0; + for(i=0; i>v[i]; + } + sort(v,v+n); + for(i=0; i=v[k]; k++) + { + nr++; + } + } + } + g< +using namespace std; +ifstream F("nrtri.in"); +ofstream G("nrtri.out"); +int a[800],l,i,j,k,n; +int main() +{ + for(F>>n;i>a[i]; + for(sort(a,a+n),i=0;i=a[k];++k); + l+=k-j-1; + } + G< +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[810]; + +void quick_sort(int v[], int s, int d) { + int i, j, i1, j1, c, k; + if (s < d) { + i = s; + j = d; + i1 = 0; + j1 = -1; + while (i < j) { + if (v[i] > v[j]) { + c = v[i]; + v[i] = v[j]; + v[j] = c; + c = i1; + i1 = -j1; + j1 = -c; + } + i = i + i1; + j = j + j1; + } + k=i; + quick_sort(v, s, k - 1); + quick_sort(v, k + 1, d); + } +} + +int main() +{ + int i, j, k, n, nr; + nr = 0; + f>>n; + for(i = 0; i < n; i++) + f>>v[i]; + quick_sort(v, 0, n - 1); + for(i = 0; i < n - 2; i++) + { + k = i + 2; + for(j = i + 1; j < n - 1; j++) + { + while(v[i] + v[j] >= v[k] && k < n) + k++; + if(k>j) + nr = nr + k - j - 1; + } + } + g< +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int i, j, n, sum, x, a[801], mx = 1024, c; + +int bs(){ + int step = mx, s = 0; + + while(step > 0){ + if(s + step <= n && a[s + step] <= sum) + s += step; + + step /= 2; + } + + return s; +} + +int main(){ + in >> n; + + for(i = 1; i <= n; i++) + in >> a[i]; + + sort(a + 1, a + n + 1); + + for(i = 1; i <= n - 1; i++) + for(j = i + 1; j <= n; j++){ + sum = a[i] + a[j]; + x = bs(); + c += x - j; + } + + out << c; + + return 0; +} diff --git a/nrtri/bruteforce/47052739-409f-11ec-8dff-dc7196033e92.cpp b/nrtri/bruteforce/47052739-409f-11ec-8dff-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9cd52286041f807249f4dba7a30596b7b4a221d --- /dev/null +++ b/nrtri/bruteforce/47052739-409f-11ec-8dff-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[800]; +int main() +{ + int i,j,k,nr=0,n; + f>>n; + for(i=0;i>v[i]; + sort(v,v+n); + for(i=0;i=v[k]&&kj) + nr+=k-j-1; + } + } + g< +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,a[801]; +int main() +{ + f>>n; + for(int i=0;i>a[i]; + sort(a,a+n); + int r=0; + for(int i=0;i=a[l]) + r++; + else + break; + g< +#include +#include +using namespace std; + +int main(){ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + long long int n,i,j,k,betisor,counter = 0; + fin>>n; + long long int betisoare[n]; + for(i=0;i>betisor; + betisoare[i] = betisor; + } + sort(betisoare,betisoare+n); + for (i=0;i=betisoare[k]) + counter++; + else + break; + + fout< +#include + +using namespace std; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + int n, v[805]; + fin>>n; + for (int i=0; i>v[i]; + } + + sort(v,v+n); + int rez=0; + for (int i=0; i= v[k]) + rez++; + else + break; + } + } + } + + fout< +#include +#include + +int main() { + + std :: ifstream fin ("nrtri.in"); + std :: ofstream fout ("nrtri.out"); + + int n, v[805]; + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> v[i]; + } + + std :: sort (v, v+n); + + int triunghi = 0; + for (int i = 0; i < n-2; ++i) { + for (int j = i+1; j < n-1; ++j) { + for (int k = j+1; k < n and v[i]+v[j] >= v[k]; ++k) { + triunghi++; + } + } + } + fout << triunghi; + + return 0; +} diff --git a/nrtri/bruteforce/49482512-409f-11ec-851a-dc7196033e92.cpp b/nrtri/bruteforce/49482512-409f-11ec-851a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8763ee91a8addd8caf75be478e7148094ae805a --- /dev/null +++ b/nrtri/bruteforce/49482512-409f-11ec-851a-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +#include +using namespace std; + + +int main(){ + ifstream f("nrtri.in"); + ofstream f1("nrtri.out"); + int n; + f>>n; + int v[n]; + for (int i=0;i>v[i]; + sort (v,v+n); + int nr=0; + for (int i=0;i +#include +#include + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, v[801], nr = 0; + f>>n; + for (int i=0;i>v[i]; + sort(v, v+n); + + for(int i = 0; i= v[k]) + nr++; + else break; + g< +#include +#include +#include +using namespace std; + + +int main(){ + ifstream f("nrtri.in"); + ofstream f1("nrtri.out"); + int n; + f>>n; + int v[n]; + for (int i=0;i>v[i]; + sort (v,v+n); + int nr=0; + for (int i=0;i +#include +#include +#include + +auto count(std::vector& v) -> int +{ + std::size_t const n = v.size(); + int count{ 0 }; + + for(std::size_t i = 0; i < n - 2; ++i) { + for(std::size_t j = i + 1; j < n - 1; ++j) { + for(std::size_t k = j + 1; k < n; ++k) { + if(v[i] + v[j] >= v[k]) { + ++count; + } + else { + break; + } + } + } + } + + return count; +} + +auto main() noexcept -> int +{ + std::ifstream f{ "nrtri.in" }; + std::ofstream g{ "nrtri.out" }; + + int num{ 0 }; + std::vector nums{}; + + f >> num; + nums.reserve(static_cast(num)); + + for(int i = 0; i < num; ++i) { + int x{ 0 }; + f >> x; + nums.push_back(x); + } + + std::sort(nums.begin(), nums.end()); + g << count(nums) << std::endl; +} diff --git a/nrtri/bruteforce/4a1b81dc-839e-4869-a7f9-feb355506420.cpp b/nrtri/bruteforce/4a1b81dc-839e-4869-a7f9-feb355506420.cpp new file mode 100644 index 0000000000000000000000000000000000000000..56d4c7f3546a2c4fd8d0719d750ab171583744a6 --- /dev/null +++ b/nrtri/bruteforce/4a1b81dc-839e-4869-a7f9-feb355506420.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,i,j,k; +int st,dr,m; +int poz1,poz2,rez; +int v[805]; + +int main() +{ + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for (j=i+1;j<=n-1;j++) + { + for (k=j+1;k<=n && v[i]+v[j]>=v[k];k++) + rez++; + } + } + g< +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int n, x; + vector v; + fin >> n; + for(int i = 0; i < n; i++) + { + fin >> x; + v.push_back(x); + } + // sortez vectorul + bool schimb = true; + while (schimb) + { + schimb = false; + for(int i = 0; i < n-1; i++) + if(v[i] > v[i+1]) + { + swap(v[i], v[i+1]); + schimb = true; + } + } + + fout << endl; + int count = 0; + for(int i = 0; i < n - 2; i++) + for(int j = i + 1; j < n - 1; j++) + for(int k = j + 1; k < n && v[i] + v[j] >= v[k]; k++) + count++; + fout << count; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/4b197f84-409f-11ec-8d61-dc7196033e92.cpp b/nrtri/bruteforce/4b197f84-409f-11ec-8d61-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b65d9e920bb19e7c2b4ba6bb5ac6d748bfd41843 --- /dev/null +++ b/nrtri/bruteforce/4b197f84-409f-11ec-8d61-dc7196033e92.cpp @@ -0,0 +1,69 @@ +// By Stefan Radu + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +#define sz(x) (int)(x).size() + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +int main() { + +#ifdef STEF + freopen("input", "r", stdin); + freopen("output", "w", stdout); +#endif + + ios::sync_with_stdio(false); + cin.tie(0);cout.tie(0); + + int n; cin >> n; + vector < int > lengths(n); + vector < int > mars(60007); + for (int i = 0; i < n; ++ i) { + cin >> lengths[i]; + mars[lengths[i]] += 1; + } + + for (int i = 1; i < sz(mars); ++ i) mars[i] += mars[i - 1]; + + int cnt = 0; + for (int i = 0; i < n; ++ i) { + for (int j = i + 1; j < n; ++ j) { + + int a = abs(lengths[i] - lengths[j]) - 1; + int b = lengths[i] + lengths[j]; + + cnt += mars[b] - mars[a]; + + if (a < min(lengths[i], lengths[j])) { + cnt -= 2; + } + else { + cnt -= 1; + } + } + } + + cout << cnt / 3 << '\n'; +} diff --git a/nrtri/bruteforce/4b198fe6-5a9c-40a7-a9d0-cd7f1c8cd683.cpp b/nrtri/bruteforce/4b198fe6-5a9c-40a7-a9d0-cd7f1c8cd683.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85509f87bd7a6fb071e6e75f5da247e68cd7f5a9 --- /dev/null +++ b/nrtri/bruteforce/4b198fe6-5a9c-40a7-a9d0-cd7f1c8cd683.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +int main() { + + std :: ifstream fin ("nrtri.in"); + std :: ofstream fout ("nrtri.out"); + + int n, v[805]; + fin >> n; + for (int i = 0; i < n; ++i) { + fin >> v[i]; + } + + std :: sort (v, v+n); + + int triunghi = 0; + for (int i = 0; i < n-2; ++i) { + for (int j = i+1; j < n-1; ++j) { + for (int k = j+1; k < n and v[i]+v[j] >= v[k]; ++k) { + triunghi++; + } + } + } + fout << triunghi; + + return 0; +} diff --git a/nrtri/bruteforce/4bac5403-409f-11ec-aab2-dc7196033e92.cpp b/nrtri/bruteforce/4bac5403-409f-11ec-aab2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8585a20ef2801540202486055081405b52af8eee --- /dev/null +++ b/nrtri/bruteforce/4bac5403-409f-11ec-aab2-dc7196033e92.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; +ifstream f ("nrtri.in"); +ofstream g ("nrtri.out"); +int n,k=0; +int main() +{ + f>>n; + int v[n]; + for(int i=0; i>v[i]; + + sort(v,v+n); + + for(int i=0; i= v[l]; ++l) + k++; + + g< +#include + +using namespace std; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + int i, j, k, v[800],n, aux, nr = 0; + + fin >> n; + for (i = 0; i < n; i++) + fin >> v[i]; + for (i = 0; i < n-1; i++) + for (j = i + 1; j < n; j++) + if (v[i] > v[j]) + { + aux = v[i]; + v[i] = v[j]; + v[j] = aux; + } + for (i = 0; i < n - 2; i ++) + for (j = i + 1; j < n - 1; j ++) + for (k = j + 1; k < n && v[i] + v[j] >= v[k]; k ++) + nr++; + fout << nr; + + fin.close(); + fout.close(); + return 0; +} diff --git a/nrtri/bruteforce/4c3ea56f-409f-11ec-aac5-dc7196033e92.cpp b/nrtri/bruteforce/4c3ea56f-409f-11ec-aac5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97e3ba1e76eb100f76973894ee71a505ab58866b --- /dev/null +++ b/nrtri/bruteforce/4c3ea56f-409f-11ec-aac5-dc7196033e92.cpp @@ -0,0 +1,55 @@ +// By Stefan Radu + +#include +#include + +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +#define sz(x) (int)(x).size() + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +const int MAX_CNT = 30001, MAX_N = 800; +int mars[MAX_CNT], lengths[MAX_N]; + +int main() { + +#ifdef STEF + freopen("input", "r", stdin); + freopen("output", "w", stdout); +#endif + + ios::sync_with_stdio(false); + cin.tie(0);cout.tie(0); + + int n; cin >> n; + for (int i = 0; i < n; ++ i) { + cin >> lengths[i]; + mars[lengths[i]] += 1; + } + + sort(lengths, lengths + n); + + for (int i = 1; i < MAX_CNT; ++ i) mars[i] += mars[i - 1]; + + int cnt = 0; + for (int i = 0; i < n; ++ i) { + for (int j = i + 1; j < n; ++ j) { + + int a = max(0, lengths[j] - lengths[i] - 1); + int b = min(lengths[i] + lengths[j], 30000); + + cnt += mars[b] - mars[a]; + cnt -= a < lengths[i] ? 2 : 1; + } + } + + cout << cnt / 3 << '\n'; +} diff --git a/nrtri/bruteforce/4caf3bbe-409f-11ec-8560-dc7196033e92.cpp b/nrtri/bruteforce/4caf3bbe-409f-11ec-8560-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6144903fea007f97e2bef8a69f61bac25fec5aba --- /dev/null +++ b/nrtri/bruteforce/4caf3bbe-409f-11ec-8560-dc7196033e92.cpp @@ -0,0 +1,79 @@ +// By Stefan Radu + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +#define sz(x) (int)(x).size() + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +vector < int > aib(70001); + +void update(int val, int index) { + while (index < sz(aib)) { + aib[index] += val; + index += index & (-index); + } +} + +int query(int index) { + if (index < 0) return 0; + int ret = 0; + while (index) { + ret += aib[index]; + index -= index & (-index); + } + return ret; +} + +int main() { + +#ifdef STEF + freopen("input", "r", stdin); + freopen("output", "w", stdout); +#endif + + ios::sync_with_stdio(false); + cin.tie(0);cout.tie(0); + + int n; cin >> n; + + vector < int > lengths(n); + for (int i = 0; i < n; ++ i) { + cin >> lengths[i]; + update(1, lengths[i]); + } + + int cnt = 0; + for (int i = 0; i < n; ++ i) { + update(-1, lengths[i]); + for (int j = i + 1; j < n; ++ j) { + update(-1, lengths[j]); + cnt += query(lengths[i] + lengths[j]) - query(abs(lengths[i] - lengths[j]) - 1); + update(1, lengths[j]); + } + update(1, lengths[i]); + } + + cout << cnt / 3 << '\n'; +} diff --git a/nrtri/bruteforce/4ce684b3-409f-11ec-9890-dc7196033e92.cpp b/nrtri/bruteforce/4ce684b3-409f-11ec-9890-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..491f02256d70b29907465b10431884c8be1b0127 --- /dev/null +++ b/nrtri/bruteforce/4ce684b3-409f-11ec-9890-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +// #include + +using namespace std; + +// Reference: http://www.cplusplus.com/reference/algorithm/nth_element/ + + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +long int n, v[801], nr; +int main() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + nr = 0; + f >> n; + for (int i = 0; i < n; i++) + f >> v[i]; + + sort(v, v + n); + + for (int i = 0; i < n; i++) + for (int j = i + 1; j < n; j++) + for (int k = j + 1; k < n && v[i] + v[j] >= v[k]; k++) + nr++; + + g << nr; + return 0; +} diff --git a/nrtri/bruteforce/4d2b4f84-409f-11ec-936b-dc7196033e92.cpp b/nrtri/bruteforce/4d2b4f84-409f-11ec-936b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f3af7140615081f067249230b2b750f60cf8f11 --- /dev/null +++ b/nrtri/bruteforce/4d2b4f84-409f-11ec-936b-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int main() +{ int n, v[30000], rez=0; + f>>n; + for(int i=0;i>v[i]; + sort(v, v+n); + for(int i=0; i=v[j+1]) + { + rez++; + j++; + } + + } + + + g << rez; + return 0; +} diff --git a/nrtri/bruteforce/4d9d67df-409f-11ec-a86b-dc7196033e92.cpp b/nrtri/bruteforce/4d9d67df-409f-11ec-a86b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..279cb2c77fe302289dd5e46358c56cc31d204bac --- /dev/null +++ b/nrtri/bruteforce/4d9d67df-409f-11ec-a86b-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; + +int main() +{ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int x,i,j,n,a[801],nr=0; + in>>n; + for(i=1; i<=n; i++) + in>>a[i]; + sort(a+1,a+n+1); + for(i=1; i<=n; i++) + { + x=i+2; + for(j=i+1; j<=n; j++) + { + while(x<=n && a[i]+a[j]>=a[x]) + x++; + if(x-j-1>0) + nr=nr+x-j-1; + } + } + out< +using namespace std; +//#define oo 2000000010; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +const int N=810; +int n,v[N],nr; +int main() +{ + f>>n; + for(int i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i=v[poz+1]) + poz++; + nr+=poz-j; + } + + g< +#include +#include + +using namespace std; +ifstream f ("nrtri.in"); +ofstream g ("nrtri.out"); +int v[805], n, rez=0; + +int main() +{ + ///citire + f>>n; + for(int i=0; i>v[i]; + sort(v, v+n); + for(int i=0; i<=n-3; i++) + { + for(int j=i+2; j<=n-1; j++) + { + /// perechi de 2 numere si vad daca il gasesc pe al 3-lea astfel + ///incat a+b>=c + int x=v[i]; + int y=v[j]; + for(int p=i+1; p=y) + { + rez+=j-p;///cum numerele sunt sortate, toate numerele de + ///la dreapta unui nr ce satisface proprietatea + ///vor satisface proprietatea + break; + } + } + } + } + g< +std::ifstream fin("nrtri.in"); +std::ofstream fout("nrtri.out"); + +int n, nrOfTri, v[801]; + +int main() +{ + int i, j, k; + fin >> n; + for(i = 0; i < n; i++) + fin >> v[i]; + std::sort(v, v + n); + //cond triunghi: a + b <= c + for(i = 0; i < n - 2; i++) + { + for(j = i + 1; j < n - 1; j ++) + { + k = j; //ramanea k prost de la prima parcurgere + while(k < n - 1 && v[i] + v[j] >= v[k + 1]) //n-1 pt ca ne uitam la k + 1 + { + nrOfTri++; + k++; + } + } + } + + fout << nrOfTri; + + return 0; + +} + +/* +2 3 4 7 + i j + k +*/ \ No newline at end of file diff --git a/nrtri/bruteforce/4efdd42b-409f-11ec-b405-dc7196033e92.cpp b/nrtri/bruteforce/4efdd42b-409f-11ec-b405-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbca9253d2a3a80dbb4156e1350e0b48beb01d51 --- /dev/null +++ b/nrtri/bruteforce/4efdd42b-409f-11ec-b405-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,v[801],rasp; +int main() +{ + f >> n; + for(int i = 1; i <= n; ++i) + f >> v[i]; + sort(v + 1,v + n + 1); + for(int i = 1; i <= n - 2; ++i) + { + int poz = i + 1; + for(int j = i + 1; j < n; ++j) + { + while(poz < n && v[i] + v[j] >= v[poz + 1]) + poz++; + rasp += poz - j; + } + } + g << rasp << '\n'; + return 0; +} diff --git a/nrtri/bruteforce/4f74617d-409f-11ec-bc23-dc7196033e92.cpp b/nrtri/bruteforce/4f74617d-409f-11ec-bc23-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29a1b317779823be2230baa3c5d44c24fa709a65 --- /dev/null +++ b/nrtri/bruteforce/4f74617d-409f-11ec-bc23-dc7196033e92.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int nmax=800; + +int v[nmax+1]; + +int main(){ + int n; + fin>>n; + int n2=1; + while(n2<=n){ + n2*=2; + } + n2/=2; + for(int i=1;i<=n;++i){ + fin>>v[i]; + } + for(int i=1;i<=n;i++){ + for(int j=i;j<=n;++j){ + if(v[i]>v[j]){ + int a=v[i]; + v[i]=v[j]; + v[j]=a; + } + } + } + int sol=0; + for(int i=1;i<=n-2;++i){ + for(int j=i+1;j<=n-1;++j){ + int p=j; + for(int k=n2;k>=1;k/=2){ + if(p+k<=n&&v[p+k]<=v[i]+v[j]){ + p+=k; + } + } + sol+=p-j; + } + } + fout< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +const int dim = 810; + +int n, v[dim], rez, p; + +int main() +{ + in>>n; + for(int i = 1;i <= n;i++) + in>>v[i]; + sort(v + 1, v + n + 1); + for(int i = 1;i <= n - 1;i++) + { + for(int j = i + 1;j <= n - 1;j++) + { + p = j + 1; + while(v[i] + v[j] >= v[p] && p <= n) + p++; + rez += p - j - 1; + } + } + out< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main() +{ + int n,i,j,k,v[2001],nt=0; + in>>n; + for (i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for (i=1; i<=n-2; i++) + { + k=i+2; + for (j=i+1; j<=n; j++) + { + while (k<=n&&v[i]+v[j]>=v[k]) + k++; + if (k>j) + nt+=k-j-1; + } + } + out< +using namespace std;long v[900],i,j,k,n,r;main(){ifstream f("nrtri.in");ofstream g("nrtri.out");f>>n;for(i=1;i<=n;i++)f>>v[i];sort(v+1,v+n+1);for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)for(k=j+1;k<=n&&v[k]<=v[i]+v[j];k++)r++;g< +#include + +using namespace std; + +//ifstream cin("text.in"); ofstream cout("text.out"); +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; + +int main() +{ + int n; + cin >> n; + for (int i = 1; i <= n; ++i) + cin >> v[i]; + + sort(v + 1, v + n + 1); + + int ans = 0; + for (int i = 1; i <= n - 2; ++i) + { + int pnt = i + 1; + for (int j = i + 1; j <= n - 1; ++j) + { + while (pnt <= n && v[i] + v[j] >= v[pnt]) + pnt++; + ans += pnt - j - 1; + } + } + cout << ans; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/51a1c017-409f-11ec-bf32-dc7196033e92.cpp b/nrtri/bruteforce/51a1c017-409f-11ec-bf32-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d9697064c36200158f0163f21e7a190d79934b --- /dev/null +++ b/nrtri/bruteforce/51a1c017-409f-11ec-bf32-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,v[801],nr=0,i,j,k,ok; +int main() +{ + f>>n; + for(i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + { + ok=1; + for(k=j+1; k<=n && ok==1; k++) + { + if(v[i]+v[j]>=v[k]) + nr++; + if(v[i]+v[j] +#include + +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801]; + +int main() +{ + int cnt=0,n,i,j,k; + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + for(k=j+1;v[k]<=(v[i]+v[j]) && k <=n ;k++) + cnt++; + out< +#include +#include + +using namespace std; +ifstream r("nrtri.in"); +ofstream w("nrtri.out"); +int v[801]; +int main() +{ + int n, x=0, y=1, yy, z=2, zz, nr=0, cnt=0; + r>>n; + for(int i=0; i>v[i]; + } + sort(v, v+n); + while(x<=n-3) + { + yy=y; + zz=z; + while(yy<=n-2) + { + while(v[x]+v[yy]>=v[zz] && zz<=n-1) + { + zz++; + cnt++; + } + yy++; + nr+=cnt; + cnt--; + } + z++; + y++; + x++; + cnt=0; + } + w< + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,i,j,k,sum,a[801]; +int main() +{ + fin>>n; + for(i=1; i<=n; i++) + { + fin>>a[i]; + } + sort(a+1,+a+n+1); + for(i=1; i<=n-2; i++) + {k=i+2; + for(j=i+1; j<=n-1; j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + sum+=k-j-1; + } + } + fout< + +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,k,nr,a[801]; +int main() +{ + fin>>n; + for(int i=1;i<=n;i++) + fin>>a[i]; + sort(a+1,a+n+1); + for(int i=1;i<=n-2;i++) + { + k=i+2; + for(int j=i+1;j<=n-1;j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + nr+=k-j-1; + } + } + fout << nr <<'\n'; + return 0; +} diff --git a/nrtri/bruteforce/556cb37f-409f-11ec-b5e6-dc7196033e92.cpp b/nrtri/bruteforce/556cb37f-409f-11ec-b5e6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..707a2a516faa4f20339402ad6edae5d8edc1b248 --- /dev/null +++ b/nrtri/bruteforce/556cb37f-409f-11ec-b5e6-dc7196033e92.cpp @@ -0,0 +1,27 @@ + #include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n,k,a[805]; +long long sol; +int main() +{ + fin>>n; + for (int i=1;i<=n;i++) + fin>>a[i]; + sort(a+1,a+n+1); + for (int i=1;i<=n-2;i++) + { + k=i+2; + for (int j=i+1;j<=n-1;j++) + { + + while(a[i]+a[j]>=a[k] && k<=n) + k++; + if (k>j) + sol+=k-j-1; + } + } + fout< +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main() +{ + int count,a[801],n,k; + in>>n; + for(int i=0;i>a[i]; + } + sort(a,a+n); + count = 0; + for (int i = 0; i < n - 2; ++i) + { + k = i + 2; + + for (int j = i + 1; j < n - 1; ++j) + { + while (k < n && a[k] <= a[i] + a[j]) + ++k; + + if (k > j) + count += k - j-1; + } + } + out< +#include + +using namespace std; + +int main() +{ ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + int v[1000],j,i,z,n,cnt=0,o=0; + cin>>n; + for(i=0;i>v[i]; + sort(v,v+n); + for(i=0;i=v[z]) + cnt++; + else + o=-1;}} + cout< +#include + +using namespace std; + int main() +{ +int i,v[805],j,k,sum,n,cont; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +cont=0; +fin>>n; +for(i=1;i<=n;i++){ + fin>>v[i]; +} +sort(v+1,v+n+1); +for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + for(k=j+1;k<=n;k++){ + sum=v[i]+v[j]; + if(sum>=v[k]) + cont++; + + else + break; + } +fout< + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,nr; +int a[801]; + +void Citire() +{ + f>>n; + for(int i=1;i<=n;++i) + f>>a[i]; + f.close(); +} + +void Solve() +{ + sort(a+1,a+1+n); + for(int i=1;i<=n-2;++i) + for(int j=i+1;j<=n-1;++j) + for(int k=j+1;k<=n;++k) + if(a[i] + a[j] >= a[k]) ++nr; + else break; //daca nu e mai mare decat a[k], nu e nici mai mare decat a[k+1], vectorul fiind sortat +} + +void Afisare() +{ + g< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int v[805], n, i, j, cnt, suma, k; + +void read() +{ + f >> n; + for (int i=1; i<=n; i++) + f >> v[i]; +} + +void solve() +{ + sort(v+1, v+n+1); + for (i=1; i<=n-2; i++) + for (j=i+1; j<=n-1; j++) + for (k=j+1; k<=n; k++) + { + suma = v[i] + v[j]; + if(suma >= v[k]) + cnt ++; + else + break; + } + g << cnt; +} + +int main() +{ + read(); + solve(); + return 0; +} diff --git a/nrtri/bruteforce/5919b000-409f-11ec-81d0-dc7196033e92.cpp b/nrtri/bruteforce/5919b000-409f-11ec-81d0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7be08166f9a7eb02aaa2498a36a627fa54054b --- /dev/null +++ b/nrtri/bruteforce/5919b000-409f-11ec-81d0-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[801]; +int main() { + int n, i, j, k, cont=0, sum, lat; + f>>n; + for (i=1; i<=n; i++){ + f>>v[i]; + } + sort (v+1, v+n+1); + for (i=1; i<=n-2; i++){ + for (j=i+1; j<=n-1; j++){ + for (k=j+1; k<=n; k++) { + sum=v[i]+v[j]; + lat=v[k]; + if (sum>=lat){ + cont++; + } + else{ + break; + } + } + } + } + g< +#include +#include +#define N 805 +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int n,a[N]; + +void read() +{ + int i; + fin>>n; + for(i=1;i<=n;++i) + fin>>a[i]; +} + +void solve() +{ + sort(a+1,a+n+1); + int i,j,k,ct=0,sum,dif; + //alegem 2 betisoare, il cautam si pe al 3 lea in mod convenabil + //suma oricaror 2 mai mare decat al 3 lea + //dif oricaror 2 mai mica decat a 3a + for(i=1;i<=n;++i) + for(j=i+1;j<=n;++j) + for(k=j+1;k<=n;++k) + { + sum=a[i]+a[j]; + if(sum>=a[k])ct++; + else break; + + } + fout< + +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[10007],sum[100007]; +int main() +{ + int n,i,maxi=0; + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + sum[v[i]]++; + maxi=max(maxi,v[i]); + } + for(i=1;i<=100000;i++) + sum[i]+=sum[i-1]; + int j; + int s; + int a,x; + int total=0; + for(i=1;i<=n;i++) + { + for(j=i+1;j<=n;j++) + { + s=v[i]+v[j]; + a=max(v[i],v[j]); + x=a-min(v[i],v[j]); + total+=(sum[s]-sum[x-1]); + if(v[i]>=x&&v[i]<=s) + total--; + if(v[j]>=x&&v[j]<=s) + total--; + } + } + out< +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int main() +{ + int count,a[801],n,k; + in>>n; + for(int i=0;i>a[i]; + } + sort(a,a+n); + count = 0; + for (int i = 0; i < n - 2; ++i) + { + k = i + 2; + + for (int j = i + 1; j < n - 1; ++j) + { + while (k < n && a[k] <= a[i] + a[j]) + ++k; + + if (k > j) + count += k - j-1; + } + } + out< +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int v[801]; +int main() +{ + int i,n,j,k,cnt=0; + cin>>n; + for(i=1;i<=n;i++) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + for (k=j+1; k<=n; k++) + { + int s2=v[i]+v[j]; + int l3=v[k]; + if (s2>=l3) + cnt++; + else + break; + } + } + } + cout< +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801]; +int main() +{ + int n, i, j, k, cnt=0, s2, l3; + in>>n; + for (i=1; i<=n; i++) + in>>v[i]; + sort (v+1, v+n+1); + for (i=1; i<=n-2; i++) + for (j=i+1; j<=n-1; j++) + for (k=j+1; k<=n; k++) + { + s2=v[i]+v[j]; + l3=v[k]; + if (s2>=l3) + cnt++; + else + break; + } + out< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +//----------------------------------------------------------------- + +#include + +//ifstream cin("input"); ofstream cout("output"); +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; + +int main() { + + int n; + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + int ans = 0; + + for (int i = 1; i <= n - 2; i++) { + int pnt = i + 1; + for (int j = i + 1; j <= n - 1; j++) { + while (pnt <= n && v[i] + v[j] >= v[pnt]) { + pnt++; + } + ans += pnt - j - 1; + } + } + + cout << ans; + + return 0; +} diff --git a/nrtri/bruteforce/5d91b394-409f-11ec-b2e7-dc7196033e92.cpp b/nrtri/bruteforce/5d91b394-409f-11ec-b2e7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87ca255249b0bd2d126f8622bb9e7b59516d4374 --- /dev/null +++ b/nrtri/bruteforce/5d91b394-409f-11ec-b2e7-dc7196033e92.cpp @@ -0,0 +1,60 @@ +#include +#define nmax 801 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int v[nmax]; +int n; +/* +int CautareBinara(int x) +{ + int dr=n,mij; + while(st<=dr) + { + mij=(st+dr)/2; + if(v[mij]==x) + { + return mij; + } + else if(v[mij]>n; + for(int i=1;i<=n;i++) + { + fin>>v[i]; + } + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + for(int k=j+1;k<=n;k++) + { + if(v[k]<=v[i]+v[j]) + { + s++; + } + else + { + break; + } + } + ///s+=maxbun-j; + } + } + fout< +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N,sol; +int v[802]; + +void Read(){ + + in >> N; + for(int i = 1; i <= N; ++i) + in >> v[i]; + sort(v+1,v+N+1); +} + +bool triunghi(int a, int b, int c){ + return (a+b >= c) && (a+c >= b) && (b+c >= a); +} +int main(){ + + Read(); + + for(int i = 1; i <= N-2; i++) + for(int j = i + 1; j <= N-1; j++) + for(int k = j + 1; k <= N && triunghi(v[i],v[j],v[k]); k++)sol++; + + out << sol; + out.close(); + + return 0; +} diff --git a/nrtri/bruteforce/60aee797-409f-11ec-b32f-dc7196033e92.cpp b/nrtri/bruteforce/60aee797-409f-11ec-b32f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..026f4f0aaa4a3399d8c4f43e3248298b69508d3e --- /dev/null +++ b/nrtri/bruteforce/60aee797-409f-11ec-b32f-dc7196033e92.cpp @@ -0,0 +1,55 @@ +//ALEX ENACHE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +//----------------------------------------------------------------- + +#include + +//ifstream cin("input"); ofstream cout("output"); +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; + +int main() { + + int n; + cin >> n; + + for (int i = 1; i <= n; i++) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + int ans = 0; + + for (int i = 1; i <= n - 2; i++) { + int pnt = i + 1; + for (int j = i + 1; j <= n - 1; j++) { + while (pnt <= n && v[i] + v[j] >= v[pnt]) { + pnt++; + } + ans += pnt - j - 1; + } + } + + cout << ans; + + return 0; +} diff --git a/nrtri/bruteforce/61a5646d-409f-11ec-a903-dc7196033e92.cpp b/nrtri/bruteforce/61a5646d-409f-11ec-a903-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e16f1e49de392c1e5c1ebdd5ae4ba18f3d35a9af --- /dev/null +++ b/nrtri/bruteforce/61a5646d-409f-11ec-a903-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,v[1000],cnt; + +int main() +{ + f >> n; + for(int i=1;i<=n;i++) + f >> v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int h=j+1;h<=n;h++) + if(v[h]<=v[i]+v[j]) + cnt++; + else + break; + g << cnt; +} diff --git a/nrtri/bruteforce/61b00961-6c35-411f-9e6f-899b9efc3908.cpp b/nrtri/bruteforce/61b00961-6c35-411f-9e6f-899b9efc3908.cpp new file mode 100644 index 0000000000000000000000000000000000000000..274d954b7df3e6b68a8119c1bd481d5ed8b228b8 --- /dev/null +++ b/nrtri/bruteforce/61b00961-6c35-411f-9e6f-899b9efc3908.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int contor = 0; + int n; + f >> n; + int s[805]; + for(int i = 1; i <= n; i++) + f >> s[i]; + sort(s + 1, s + n + 1); + for(int i = 1; i <= n; i++) + for(int j = i+1; j <= n; j++) + for(int k = j+1; k <= n; k++) + if((s[i] + s[j] >= s[k])) + contor++; + else break; + g << contor; + + return 0; +} diff --git a/nrtri/bruteforce/620b60f7-409f-11ec-b920-dc7196033e92.cpp b/nrtri/bruteforce/620b60f7-409f-11ec-b920-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..222da86606a6949b809b7667523ee94d93b0dbaa --- /dev/null +++ b/nrtri/bruteforce/620b60f7-409f-11ec-b920-dc7196033e92.cpp @@ -0,0 +1,52 @@ +//ALEXANDRU MICLEA + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +#include +ifstream cin("nrtri.in"); ofstream cout("nrtri.out"); + +int v[805]; +int n; + +int main() { + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> v[i]; + } + + sort(v + 1, v + n + 1); + + int ans = 0; + + for (int i = 1; i <= n - 2; i++) { + int pnt = i + 1; + for (int j = i + 1; j <= n - 1; j++) { + while (pnt <= n && v[i] + v[j] >= v[pnt]) { + pnt++; + } + ans += pnt - j - 1; + } + } + + cout << ans; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/6272c3a8-409f-11ec-a99e-dc7196033e92.cpp b/nrtri/bruteforce/6272c3a8-409f-11ec-a99e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a1f0b5f236974e6ae9107a1fcc5a8c88fbfc834 --- /dev/null +++ b/nrtri/bruteforce/6272c3a8-409f-11ec-a99e-dc7196033e92.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[801], i, j, k, n, cnt; +int main() +{ + f >> n; + for(i = 1; i <= n; ++ i) + f >> v[i]; + sort(v + 1, v + n + 1); + for(i = 1; i <= n - 2; ++ i) + for(j = i + 1; j <= n - 1; ++ j) + for(k = j + 1; k <= n; ++ k) + if(v[k] <= v[i] + v[j]) cnt ++; + else break; + g << cnt; + return 0; +} diff --git a/nrtri/bruteforce/63e306d1-409f-11ec-ba1b-dc7196033e92.cpp b/nrtri/bruteforce/63e306d1-409f-11ec-ba1b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f0f5bff577c0d59b9ee265dd711185389d880cb --- /dev/null +++ b/nrtri/bruteforce/63e306d1-409f-11ec-ba1b-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +short a[2001]; +int n,pos; +int cautbin(int val) +{ + pos=0; + for(int msk=1<<30; msk>0; msk/=2) + { + if(pos+msk<=n && a[pos+msk]<=val) + pos+=msk; + } + if(a[pos]==val) + return pos; + return -1; +} + + +int main() +{ + int k,cont=0,x; + cin >> n; + for(int i=1;i<=n;i++) + cin >> a[i]; + sort(a+1, a+n+1); + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + x=a[i]+a[j]; + k=cautbin(x); + cont=cont+pos-j; + } + } + cout << cont; + return 0; +} diff --git a/nrtri/bruteforce/644c9f10-89ac-4d04-bb1e-ea22ac197037.cpp b/nrtri/bruteforce/644c9f10-89ac-4d04-bb1e-ea22ac197037.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57d8bd32500476d83a4d3a223614dffedc49e0c6 --- /dev/null +++ b/nrtri/bruteforce/644c9f10-89ac-4d04-bb1e-ea22ac197037.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +using namespace std; + +int main() +{ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n, l[801], nrtri = 0, i, j, k; +in >> n; +for (int i = 0 ; i < n ; ++i) + in >> l[i]; +in.close(); +sort(l,l+n); +for (i = 0 ; i < n-2 ; i++) + for (j = i+1 ; j < n-1 ; j++) + for (k = j+1 ; k < n; k++) + if ((l[i] + l[j]) >= l[k] && (l[i] + l[k]) >= l[j] && (l[j] + l[k]) >= l[i]) + nrtri++; + else break; +out << nrtri; +out.close(); + +} diff --git a/nrtri/bruteforce/6552a4bb-f790-4ece-8489-9e62803a64ac.cpp b/nrtri/bruteforce/6552a4bb-f790-4ece-8489-9e62803a64ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3888549851bac217efb98c264785b92f5f94cb9b --- /dev/null +++ b/nrtri/bruteforce/6552a4bb-f790-4ece-8489-9e62803a64ac.cpp @@ -0,0 +1,27 @@ +#include + +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int n,v[805],ans,st,dr,mij,sum,poz; +void read() +{ + fin>>n; + for(int i=1;i<=n;i++) fin>>v[i]; + sort(v+1,v+1+n); +} +void solve() +{ + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int k=j+1;k<=n;k++) + if(v[i]+v[j]>=v[k]) ans++; + else break; + fout< +using namespace std;long v[900],i,j,k,n,r;main(){ifstream f("nrtri.in");ofstream g("nrtri.out");f>>n;for(i=1;i<=n;i++)f>>v[i];sort(v+1,v+n+1);for(i=1;i<=n;i++)for(j=i+1;j<=n;j++)for(k=j+1;k<=n&&v[k]<=v[i]+v[j];k++)r++;g< +#include +#define daca(a,b)if(a>b) +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[803],n,i,j,k,sol; +void citire() +{ + in>>n; + for(i=1;i<=n;i++)in>>v[i]; + +} +void sortare() +{ + int ok,aux; + do{ + ok=0; + for(i=1;i<=n-1;i++) + { + daca(v[i],v[i+1]) + { + aux=v[i]; + v[i]=v[i+1]; + v[i+1]=aux; + ok=1; + } + } + }while(ok); +} + +int triunghi(int a,int b,int c) +{ + if(a+b>=c&&a+c>=b&&b+c>=a)return 1; + else return 0; +} + +int main() +{ + citire(); + sortare(); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + for(k=j+1;k<=n&&triunghi(v[i],v[j],v[k]);k++)sol++; + + } + } + out< + +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,nr,a[801]; +int triunghiuri(int a,int b,int c) +{ + if(a+b>=c&&a+c>=b&&b+c>=a) + return 1; + return 0; +} + +int main() +{ + f>>n; + for(int i=1;i<=n;i++) + f>>a[i]; + sort(a+1,a+n+1); + for(int i=1;i +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N,sol; +int v[802]; + +void Read(){ + + in >> N; + for(int i = 1; i <= N; ++i) + in >> v[i]; + sort(v+1,v+N+1); +} + +bool triunghi(int a, int b, int c){ + return (a+b >= c) && (a+c >= b) && (b+c >= a); +} +int main(){ + + Read(); + + for(int i = 1; i <= N-2; i++) + for(int j = i + 1; j <= N-1; j++) + for(int k = j + 1; k <= N && triunghi(v[i],v[j],v[k]); k++)sol++; + + out << sol; + out.close(); + + return 0; +} diff --git a/nrtri/bruteforce/69f17c16-409f-11ec-81b6-dc7196033e92.cpp b/nrtri/bruteforce/69f17c16-409f-11ec-81b6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96dd485d181f5cc0da5a0fd16911d5188c51ad38 --- /dev/null +++ b/nrtri/bruteforce/69f17c16-409f-11ec-81b6-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,i,j,k,nr; +int v[801]; + +int triunghi(int a, int b, int c) +{ + if (a+b>=c && a+c>=b && b+c>=a) + return 1; + return 0; +} + +int main() +{ + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + for (j=i+1;j<=n-1;j++) + for (k=j+1;k<=n && triunghi(v[i],v[j],v[k]);k++) + nr++; + g< + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +const int NMax=805; + +int N,a[NMax],lg,s; + +int main() +{ + + fin >> N; + + for(int i=1;i<=N;i++) + fin >> a[i]; + + sort(a+1,a+1+N); + + for(int i=1;i>=1) + if(poz+lg<=N and a[poz+lg]<=a[i]+a[j]) + poz+=lg; + s+=poz-j; + } + + fout << s; + + return 0; + +} diff --git a/nrtri/bruteforce/6af2e7cf-409f-11ec-bdb9-dc7196033e92.cpp b/nrtri/bruteforce/6af2e7cf-409f-11ec-bdb9-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5bae0b8e37190a562239eafe8d53457cdea8a01 --- /dev/null +++ b/nrtri/bruteforce/6af2e7cf-409f-11ec-bdb9-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include +#define ff first +#define ss second + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; + +const string file = "nrtri"; +const ll INF = 9223372036854775807ll; +const int inf = 2147483647, nmax = 805; + +int n, ans, v[nmax]; + +int main() +{ + ifstream fin (file+".in"); + ofstream fout (file+".out"); + fin >> n; + for (int i = 1; i <= n; ++i) + fin >> v[i]; + sort(v+1, v+n+1); + for (int i = 1; i < n; ++i){ + int p1 = 0, p2 = 0; + for (int j = i+1; j <= n; ++j){ + int a = v[i], b = v[j]; + while(p1+1 <= n && v[p1+1] < b-a) + ++p1; + while(p2+1 <= n && v[p2+1] <= b+a) + ++p2; + ans += p2-p1; + if(i > p1 && i <= p2) + --ans; + if(j > p1 && j <= p2) + --ans; + } + } + fout << ans/3 << "\n"; + return 0; +} diff --git a/nrtri/bruteforce/6c2725c2-409f-11ec-be1e-dc7196033e92.cpp b/nrtri/bruteforce/6c2725c2-409f-11ec-be1e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b3e42c5c669d7d58a42e8df119d4dfaa2784e93 --- /dev/null +++ b/nrtri/bruteforce/6c2725c2-409f-11ec-be1e-dc7196033e92.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int v[802]; +int main() +{ + int n,sol = 0; + cin >> n; + for (int i =1; i<= n; ++i) + { + cin >> v[i]; + } + sort (v + 1,v + n + 1); + for (int i =1; i<= n; ++i) + { + int k = v[i]; + int h = i + 2; + for (int j = i + 1; j <= n; ++j) + { + int s = v[j]; + int st = j + 1; + int dr = n; + while (h <= n and k + s >= v[h]) + ++h; + --h; + sol += h - j; + } + + } + cout << sol; + return 0; +} diff --git a/nrtri/bruteforce/6c814d3e-d335-4fe3-8328-ddcee1bffcfc.cpp b/nrtri/bruteforce/6c814d3e-d335-4fe3-8328-ddcee1bffcfc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38eae2ffac9a1884ee059ad7937d66cc30143538 --- /dev/null +++ b/nrtri/bruteforce/6c814d3e-d335-4fe3-8328-ddcee1bffcfc.cpp @@ -0,0 +1,28 @@ +#include +#include +#include +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int v[1005]; +int main() +{ + int n,i,s=0,j,ok; + fin>>n; + for(i=1;i<=n;i++) + fin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + ok=i+2; + for(j=i+1;j<=n-1;j++) + { + while(ok<=n && v[ok]<=v[i]+v[j]) + ok=ok+1; + if(ok>j) + s=s+ok-j-1; + } + } + fout< +#include +#include +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main(){ + int n, v[801], i, j, k, nr = 0; + in>>n; + for(i = 1; i <= n; i++) + in>>v[i]; + sort(v + 1, v + n + 1); + for(i = 1; i <= n - 2; i++){ + for(j = i + 1; j <= n - 1; j++){ + for(k = j + 1; k <= n; k++){ + if(v[i] + v[j] >= v[k]) + nr++; + else + break; + } + } + } + out< +#include +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int v[802]; +int main() +{ + int n,sol = 0; + cin >> n; + for (int i =1; i<= n; ++i) + { + cin >> v[i]; + } + sort (v + 1,v + n + 1); + for (int i =1; i<= n; ++i) + { + int k = v[i]; + for (int j = i + 1; j <= n; ++j) + { + int s = v[j]; + for (int h =j + 1; h <= n; ++h) + { + int l = v[h]; + if (k + s >= l) + ++sol; + else + break; + + } + } + } + cout << sol; + return 0; +} diff --git a/nrtri/bruteforce/6e518231-6432-43af-a367-f324b097a9af.cpp b/nrtri/bruteforce/6e518231-6432-43af-a367-f324b097a9af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e0aa0f51173df4be49042c56d3c3f3b25725b1c --- /dev/null +++ b/nrtri/bruteforce/6e518231-6432-43af-a367-f324b097a9af.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); +int n, k, i, j, ans, a[805]; +int main() { + fin >> n; + for (int i=1;i<=n;i++){ + fin >> a[i]; + } + sort(a+1,a+n+1); + for (i=1;i<=n-2;i++){ + k = i+2; + for (j=i+1;j<=n-1;j++){ + while(k<=n && a[k] <= a[i] + a[j]) + ++k; + if (k>j) + ans += k - j-1; + } + } + + fout << ans << '\n'; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/6e7a5713-409f-11ec-88f8-dc7196033e92.cpp b/nrtri/bruteforce/6e7a5713-409f-11ec-88f8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd26b519ffaa2fa51436ddbc3bf592c6e72f3df7 --- /dev/null +++ b/nrtri/bruteforce/6e7a5713-409f-11ec-88f8-dc7196033e92.cpp @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +/*int Triangle (int a, int b, int c) +{ + return ((a+b>=c) && (b+c>=a) && (a+c>=b)); +} */ +void Swap(int &a, int &b) +{ + int temp; + temp = b; + b=a; + a = temp; +} +void BubbleSort (int *v, int n) // v[] +{ + int i, j; + + for (i=1; i<=n; i++) + { + for (j=1; jv[j+1]) + { + Swap (v[j],v[j+1]); + } + } + } +} + +int main() +{ + int N, V[803], contor=0; + cin>> N; + + for(int i=1; i<=N; i++) + cin>> V[i]; + + BubbleSort(V,N); + + //for (int i=1; i=V[k]) + ++k; + contor+=k-j-1; + + + } + + } + + cout< +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int Triangle (int a, int b, int c) +{ + return ((a+b>=c) && (b+c>=a) && (a+c>=b)); +} +void Swap(int &a, int &b) +{ + int temp; + temp = b; + b=a; + a = temp; +} +void BubbleSort (int *v, int n) // v[] +{ + int i, j; + + for (i=1; i<=n; i++) + { + for (j=1; jv[j+1]) + { + Swap (v[j],v[j+1]); + } + } + } +} + +int main() +{ + int N, V[803], contor=0; + cin>> N; + + for(int i=1; i<=N; i++) + cin>> V[i]; + + BubbleSort(V,N); + + //for (int i=1; i +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,v[805],i,j,k,rez,gasit2; +void citire() +{ + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); +} +void cautare() +{ + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + for(k=j+1; k<=n &&v[i]+v[j]>=v[k]; k++) + rez++; +} +void afisare() +{ + out< +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +/*int Triangle (int a, int b, int c) +{ + return ((a+b>=c) && (b+c>=a) && (a+c>=b)); +} */ +void Swap(int &a, int &b) +{ + int temp; + temp = b; + b=a; + a = temp; +} +void BubbleSort (int *v, int n) // v[] +{ + int i, j; + + for (i=1; i<=n; i++) + { + for (j=1; jv[j+1]) + { + Swap (v[j],v[j+1]); + } + } + } +} + +int main() +{ + int N, V[803], contor=0; + cin>> N; + + for(int i=1; i<=N; i++) + cin>> V[i]; + + BubbleSort(V,N); + + //for (int i=1; i=V[mijloc]) + { + stanga=mijloc+1; + k=mijloc; + } + else + { + dreapta=mijloc-1; + } + } + contor+=k-j; + } + + } + + cout< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,i,j,k; +int st,dr,m; +int poz1,poz2,rez; +int v[805]; + +int main() +{ + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for (j=i+1;j<=n-1;j++) + { + for (k=j+1;k<=n && v[i]+v[j]>=v[k];k++) + rez++; + } + } + g< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,v[1000],cnt; + +int main() +{ + f >> n; + for(int i=1;i<=n;i++) + f >> v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int h=j+1;h<=n;h++) + if(v[h]<=v[i]+v[j]) + cnt++; + else + break; + g << cnt; +} diff --git a/nrtri/bruteforce/72a2c6f4-660d-4480-9140-9086364a567f.cpp b/nrtri/bruteforce/72a2c6f4-660d-4480-9140-9086364a567f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e16f1e49de392c1e5c1ebdd5ae4ba18f3d35a9af --- /dev/null +++ b/nrtri/bruteforce/72a2c6f4-660d-4480-9140-9086364a567f.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,v[1000],cnt; + +int main() +{ + f >> n; + for(int i=1;i<=n;i++) + f >> v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int h=j+1;h<=n;h++) + if(v[h]<=v[i]+v[j]) + cnt++; + else + break; + g << cnt; +} diff --git a/nrtri/bruteforce/740eaa56-409f-11ec-a5ff-dc7196033e92.cpp b/nrtri/bruteforce/740eaa56-409f-11ec-a5ff-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..890a1b9eef5e0d632522eb1d56a260c8e334deac --- /dev/null +++ b/nrtri/bruteforce/740eaa56-409f-11ec-a5ff-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801]; + +int main() +{ + int N,T=0,i,j,k; + in>>N; + for(i=1; i<=N; i++) + { + in>>v[i]; + } + sort(v+1, v+N+1); + for(i=1; i<=N-2; i++) + { + for(j=i+1; j<=N-1; j++) + { + for(k=j+1; k<=N; k++) + { + if(v[i]+v[j]>=v[k]) + { + T++; + } + else + { + break; + } + } + } + } + out< +#include + +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); +int v[801]; +int main() +{ + int n,c=0,i,i2,i3; + cin>>n; + for (i=0;i>v[i]; + sort(v,v+n); + for (i=0;i +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int v[801]; +int main() +{ + int n,i,triunghiuri=0,j,z; + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + for(z=j+1; z<=n; z++) + if(v[i]+v[j]>=v[z]) + triunghiuri++; + else + break; + out< +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[801],nr; +int main() +{ + int n,i,j,k; + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + + sort(v+1,v+n+1); + for(i=1;i<=n;i++) + for(j=i+1;j<=n;j++) + for(k=j+1;k<=n;k++) + { + if (v[k]<=v[i]+v[j]) + nr++; + else + break; + } + out< + +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[801],n; + +int main() +{ + int contor=0,i,j,m; + in>>n; + for(i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(j=i+1;j<=n-1;j++) + for(m=j+1;m<=n;m++) + { + if(v[m]<=v[i]+v[j]) + contor++; + else + break; + } + out< +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int v[801],n; +int main() +{ + int i,j,q,cnt=0; + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + for(q=j+1; q<=n; q++) + { + if(v[q]<=v[j]+v[i]) + cnt++; + else + break; + } + + out< +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int v[801]; + +int main() +{ + int cnt=0,i,j,k,n; + cin>>n; + for (i=0;i>v[i]; + sort(v,v+n); + for (i=0;i + +using namespace std; + +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int v[801]; + +int main() +{ + int cnt=0,i,j,k,n; + in>>n; + for (i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + { + for (j=i+1;j<=n-1;j++) + { + for (k=j+1;k<=n;k++) + { + if (v[k]<=v[i]+v[j]) + cnt++; + else + break; + } + } + } + out< +using namespace std; +int v[801]; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); +int main() +{ + int n, i, j, h, c=0; + in >> n; + for(i=1;i<=n;i++) + in >> v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(j=1+i;j<=n-1;j++) + for(h=1+j;h<=n;h++) + if(v[h]<=v[i]+v[j]) + c++; + else + break; + out << c; + return 0; +} diff --git a/nrtri/bruteforce/77558682-409f-11ec-80fe-dc7196033e92.cpp b/nrtri/bruteforce/77558682-409f-11ec-80fe-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..decffd23bbb8e316071bf757250f5cd1a4ecc135 --- /dev/null +++ b/nrtri/bruteforce/77558682-409f-11ec-80fe-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; +ifstream in ("nrtri.in"); +ofstream out ("nrtri.out"); + +int main() +{ + int n,i,v[801],j,t,c=0; + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j<=n-1;j++) + { + for(t=j+1;t<=n;t++) + { + if(v[t]<=v[i]+v[j]) + { + c++; + } + else + { + break; + } + } + } + } + out< +#include + +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); +int v[801]; +int main() +{ + int n, c = 0, i, i2, i3; + cin >> n; + for (i = 0; i < n; i++) + cin >> v[i]; + sort(v, v + n); + for (i = 0; i < n - 2; i++) + { + for (i2 = i + 1; i2 < n - 1; i2++) + { + for (i3 = i2 + 1; i3 < n; i3++) + { + if (v[i] + v[i2] < v[i3]) + break; + else c++; + } + } + } + cout << c; + return 0; +} diff --git a/nrtri/bruteforce/78dd851a-409f-11ec-a904-dc7196033e92.cpp b/nrtri/bruteforce/78dd851a-409f-11ec-a904-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3a15ff69e5915412c017398acec008405b9c886 --- /dev/null +++ b/nrtri/bruteforce/78dd851a-409f-11ec-a904-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,v[1000],cnt; + +int main() +{ + f >> n; + for(int i=1;i<=n;i++) + f >> v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int h=j+1;h<=n;h++) + if(v[h]<=v[i]+v[j]) + cnt++; + else + break; + g << cnt; +} diff --git a/nrtri/bruteforce/7969e79e-409f-11ec-9cbe-dc7196033e92.cpp b/nrtri/bruteforce/7969e79e-409f-11ec-9cbe-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3489badb78280d8fa13b4d671a68ae509fe1ff71 --- /dev/null +++ b/nrtri/bruteforce/7969e79e-409f-11ec-9cbe-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; +int v[805]; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int n,i,j; +/*int cbin(int val) +{ + int msk,pos=0,cnt=0; + for(msk=1<<20;msk>0;msk/=2) + { + if(pos+msk<=n && v[pos+msk]>=val) + pos+=msk; + } + if(pos==0) + return 0; + while(v[pos]>=val) + { + if(v[j]+v[pos]>=v[i] && v[pos]+v[i]>=v[j] ) + if(v[pos]>=val) + ++cnt; + else + return cnt; + pos--; + } + return cnt; +}*/ +int main() +{ + int k,cnt=0,val; + cin>>n; + for(i=1;i<=n;++i) + cin>>v[i]; + sort(v+1,v+n+1); + for(i=1;i +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[900]; +int main() +{ int n; + fin>>n; + for(int i=1 ; i<=n ; i++) + fin>>v[i]; + sort(v+1,v+n+1); + + int i,j,k; + int nr=0; + + for( i = 1; i <= n - 2; i++){ + for( j = i + 1 ; j <= n - 1; j++){ + for(k = j + 1 ; k <= n; k++){ + if(v[i] + v[j] >= v[k]){ + nr++; + } + else + break; + } + } + } + +fout< +#include +using namespace std; +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n,i,ii,iii,v[830],c=0; + f>>n; + for(i=1;i<=n;++i) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + for(ii=i+1;ii=v[iii];iii++) + c++; + g< +#include +using namespace std; +int v[802], i, j, q, cnt, n; +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + f >> n; + for(i = 1; i <= n; i ++) + f >> v[i]; + sort(v + 1, v + n + 1); + for(i = 1; i <= n - 2; i ++) + for(j = i + 1; j < n; j ++) + for(q = j + 1; q <= n && v[i] + v[j] >= v[q]; q ++) cnt ++; + g << cnt; + return 0; +} diff --git a/nrtri/bruteforce/7a92dd8b-409f-11ec-ba1d-dc7196033e92.cpp b/nrtri/bruteforce/7a92dd8b-409f-11ec-ba1d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b494b376b70c2e47aae5228c192220b617d354ef --- /dev/null +++ b/nrtri/bruteforce/7a92dd8b-409f-11ec-ba1d-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,c,i,j,k,v[2001]; + +int main() +{ + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n-2; i++) + { + for(j=i+1; j<=n-1; j++) + { + for(k=j+1; k<=n; k++) + { + if(v[k]<=v[i]+v[j]) + c++; + else + break; + } + } + } + out< +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main() { + int n, v[900], i, j, k, nr = 0; + in>>n; + for( i = 1; i <= n; i++) + in>>v[i]; + sort(v+1, v+1+n); + for( j = 1; j <= n - 2; j++){ + for( k = j + 1 ; k <= n - 1; k++){ + for(i = k + 1 ; i <= n; i++){ + if(v[j] + v[k] >= v[i]){ + nr++; + } + else + break; + } + } + } + out< + +using namespace std; +ifstream fin ("nrtri.in"); +ofstream fout ("nrtri.out"); +int n,v[805],ans,st,dr,mij,sum,poz; +void read() +{ + fin>>n; + for(int i=1;i<=n;i++) fin>>v[i]; + sort(v+1,v+1+n); +} +void solve() +{ + for(int i=1;i<=n-2;i++) + for(int j=i+1;j<=n-1;j++) + for(int k=j+1;k<=n;k++) + if(v[i]+v[j]>=v[k]) ans++; + else break; + fout< +#include +#include +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,cnt; +int v[816]; + +int main() +{ + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[30005]; + +int main() +{ + + int n; + fin >> n; + for(int i = 1 ; i <= n ; ++i) + fin >> v[i]; + sort(v + 1 , v + n + 1); + int contor = 0; + for(int i = 1 ; i <= n - 2 ; ++i) + for(int j = i + 1 ; j <= n - 1 ; ++j) + for(int k = j + 1 ; k <= n ; ++k) + if((v[i] <= v[j] + v[k])&&(v[k] <= v[i] + v[j])&&(v[j] <= v[i] + v[k])) + ++contor; + else + break; + fout << contor; +} diff --git a/nrtri/bruteforce/7d002ff2-409f-11ec-a43c-dc7196033e92.cpp b/nrtri/bruteforce/7d002ff2-409f-11ec-a43c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..274d954b7df3e6b68a8119c1bd481d5ed8b228b8 --- /dev/null +++ b/nrtri/bruteforce/7d002ff2-409f-11ec-a43c-dc7196033e92.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int contor = 0; + int n; + f >> n; + int s[805]; + for(int i = 1; i <= n; i++) + f >> s[i]; + sort(s + 1, s + n + 1); + for(int i = 1; i <= n; i++) + for(int j = i+1; j <= n; j++) + for(int k = j+1; k <= n; k++) + if((s[i] + s[j] >= s[k])) + contor++; + else break; + g << contor; + + return 0; +} diff --git a/nrtri/bruteforce/7d3d3679-409f-11ec-89c0-dc7196033e92.cpp b/nrtri/bruteforce/7d3d3679-409f-11ec-89c0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ea218e95443cf21fa4256f57594ecfd9070402d --- /dev/null +++ b/nrtri/bruteforce/7d3d3679-409f-11ec-89c0-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int N,i,a[805],contor=0,j,h; + fin >> N; + for (i=1;i<=N;++i) + { + fin >> a[i]; + } + sort(a+1,a+N+1); + for (i=1;i<=N;++i) + { + for (j=i+1;j<=N;++j) + { + for (h=j+1;h<=N;++h) + { + if (a[i]+a[j]>=a[h]) + { + ++contor; + } + else + { + break; + } + } + } + } + fout << contor; + return 0; +} diff --git a/nrtri/bruteforce/7d8ebed4-409f-11ec-9156-dc7196033e92.cpp b/nrtri/bruteforce/7d8ebed4-409f-11ec-9156-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a7c3006e31a45f3fc0b5d167a0dd17dd1989b9c --- /dev/null +++ b/nrtri/bruteforce/7d8ebed4-409f-11ec-9156-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main() +{ + int n,v[805], c=0; + in>>n; + for(int i=1;i<=n;i++) + in>>v[i]; + sort(v+1,v+1+n); + for(int i=1;i<=n;i++) + { + for(int j=i+1;j<=n;j++) + { + for(int z=j+1;z<=n;z++) + { + + if(v[i]+v[j]>=v[z]) + c++; + else break; + + } + } + } + out< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N, cont; + +int main() +{ + in >> N; + int v[N]; + for(int i = 0; i < N; ++i) + in >> v[i]; + sort(v, v + N); + for(int i = 0; i < N - 2; ++i) + for(int j = i + 1; j < N - 1; ++j) + for(int k = j + 1; k < N; ++k) + if(v[i] <= v[j] + v[k] && v[j] <= v[i] + v[k] && v[k] <= v[i] + v[j]) + ++cont; + else break; + out << cont; + return 0; +} diff --git a/nrtri/bruteforce/7e276eb5-409f-11ec-9f08-dc7196033e92.cpp b/nrtri/bruteforce/7e276eb5-409f-11ec-9f08-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8614065214ca0a2b8cd23d46f5a1ed9553403ffb --- /dev/null +++ b/nrtri/bruteforce/7e276eb5-409f-11ec-9f08-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + long long a[1005]; + int contor=0; + long long n; + f>>n; + for (int i = 1; i<=n;i ++) + f>>a[i]; + + sort(a+1,a+n+1); + for (int i=1; i<=n; i++) + for(int j=i+1; j<=n; j++) + for (int k=j+1; k<=n; k++) + if (a[i]+a[j]>=a[k]) + contor++; + else break; + g< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int N, cont; + +int main() +{ + in >> N; + int v[N]; + for(int i = 0; i < N; ++i) + in >> v[i]; + sort(v, v + N); + for(int i = 0; i < N - 2; ++i) + for(int j = i + 1; j < N - 1; ++j) + for(int k = j + 1; k < N; ++k) + if(v[i] <= v[j] + v[k] && v[j] <= v[i] + v[k] && v[k] <= v[i] + v[j]) + ++cont; + else break; + out << cont; + return 0; +} diff --git a/nrtri/bruteforce/7f39205a-409f-11ec-adb8-dc7196033e92.cpp b/nrtri/bruteforce/7f39205a-409f-11ec-adb8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98fc8c609f7fe675aa48bf9c139965d341bd4470 --- /dev/null +++ b/nrtri/bruteforce/7f39205a-409f-11ec-adb8-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +#include + +int a[800]; + +int main() { + std::ifstream is{"nrtri.in"}; + + int n; + is >> n; + + for (auto i = 0; i < n; ++i) + is >> a[i]; + + std::sort(a, a + n); + + std::int64_t s{}; + + for (auto i = 0; i < n - 2; ++i) + for (auto j = i + 1; j < n - 1; ++j) + for (auto k = j + 1; k < n; ++k) + if (a[i] + a[j] >= a[k]) + ++s; + else + break; + + std::ofstream{"nrtri.out"} << s; +} diff --git a/nrtri/bruteforce/7f77d620-409f-11ec-a998-dc7196033e92.cpp b/nrtri/bruteforce/7f77d620-409f-11ec-a998-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0be69add8f00ba664b62329e9b1fe0a2ca1e1143 --- /dev/null +++ b/nrtri/bruteforce/7f77d620-409f-11ec-a998-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() { + + int b[801], n; + long long sol = 0; + + f>>n; + + for(int i = 0; i < n; i++) { + f>>b[i]; + } + + sort(b, b + n); + + for(int i = 0; i < n - 2; i++) + for(int j = i + 1; j < n - 1; j++) + for(int k = j + 1; k < n; k++){ + if(b[k] <= b[i] + b[j]) sol++; + else break; + } + + + g<< sol; +} \ No newline at end of file diff --git a/nrtri/bruteforce/7fbe91ae-86ef-4a74-bf11-952f18729a1f.cpp b/nrtri/bruteforce/7fbe91ae-86ef-4a74-bf11-952f18729a1f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..594480d39b5edc442b6dac4a2636d98f19f75286 --- /dev/null +++ b/nrtri/bruteforce/7fbe91ae-86ef-4a74-bf11-952f18729a1f.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int main() { + int n, v[900], i, j, k, nr = 0; + in>>n; + for( i = 1; i <= n; i++) + in>>v[i]; + sort(v+1, v+1+n); + for( j = 1; j <= n - 2; j++){ + for( k = j + 1 ; k <= n - 1; k++){ + for(i = k + 1 ; i <= n; i++){ + if(v[j] + v[k] >= v[i]){ + nr++; + } + else + break; + } + } + } + out< +#include +#include + +int a[800]; + +int main() { + std::ifstream is{"nrtri.in"}; + + int n; + is >> n; + + for (auto i = 0; i < n; ++i) + is >> a[i]; + + std::sort(a, a + n); + + std::int64_t s{}; + + for (auto i = 0; i < n - 2; ++i) + for (auto j = i + 1; j < n - 1; ++j) + for (auto k = j + 1; k < n; ++k) + if (a[i] + a[j] >= a[k]) + ++s; + else + break; + + std::ofstream{"nrtri.out"} << s; +} diff --git a/nrtri/bruteforce/80d629ed-409f-11ec-9bf4-dc7196033e92.cpp b/nrtri/bruteforce/80d629ed-409f-11ec-9bf4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..adb840c315d44872d3c880d7b6b36cc380a6da23 --- /dev/null +++ b/nrtri/bruteforce/80d629ed-409f-11ec-9bf4-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include + +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int n, a[1005], sol; + +int main() +{ + cin >> n; + + for(int i = 1; i <= n; i++) + cin >> a[i]; + + sort(a + 1, a + n + 1); + + for(int i = 1; i <= n - 2; i++) + for(int j = i + 1; j <= n - 1; j++) + for(int r = j + 1; r <= n and (a[i] + a[j] >= a[r]) and (a[i] + a[r] >= a[j]) and (a[j] + a[r] >= a[i]); r++) + sol++; + + cout << sol; + + return 0; +} diff --git a/nrtri/bruteforce/81c336f3-409f-11ec-9baf-dc7196033e92.cpp b/nrtri/bruteforce/81c336f3-409f-11ec-9baf-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..596e1e147fa3f804958b044486b7bb431e41af47 --- /dev/null +++ b/nrtri/bruteforce/81c336f3-409f-11ec-9baf-dc7196033e92.cpp @@ -0,0 +1,26 @@ +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[805],n,mod; +int main() +{ + f >> n; + for (int i=1;i<=n;i++) { + f >> v[i]; + } + sort (v+1 , v+n+1); + for (int i=1;i<=n-2;i++) { + for (int k=i+1;k<=n-1;k++) { + for (int j=k+1;j<=n;j++) { + if (v[i]+v[k]>=v[j] && v[i]+v[j]>=v[k] && v[j]+v[k]>=v[i]) + mod++; + else + break; + } + } + } + g << mod; + return 0; +} diff --git a/nrtri/bruteforce/83eda8a0-b25b-4fdb-9ffe-43b391905ed8.cpp b/nrtri/bruteforce/83eda8a0-b25b-4fdb-9ffe-43b391905ed8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6e4cc2904d2eb6af341781f6637efaf216eae58 --- /dev/null +++ b/nrtri/bruteforce/83eda8a0-b25b-4fdb-9ffe-43b391905ed8.cpp @@ -0,0 +1,49 @@ +#include +#define N 800 +#define B 60000 +#define d(x) x&(-x) +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int v[N + 1]; +int AIB[B + 1]; + +void updateAIB(int x){ + for(; x <= B; x += d(x)) + AIB[x]++; +} + +int queryAIB(int x){ + int ans = 0; + + for(; x > 0; x -= d(x)) + ans += AIB[x]; + + return ans; +} + +int main(){ + int n; + cin>>n; + + for(int i = 0; i < n; i++){ + cin>>v[i]; + //updateAIB(v[i]); + } + + int ans = 0; + for(int i = 0; i < n; i++){ + for(int j = i + 1; j < n; j++){ + ans += (queryAIB(v[i] + v[j]) - queryAIB(abs(v[i] - v[j]) - 1)); + updateAIB(v[j]); + } + + for(int j = 1; j <= B; j++) + AIB[j] = 0; + } + + cout< +#include + +FILE *fin = freopen("nrtri.in","r",stdin); +FILE *fout = freopen("nrtri.out","w",stdout); + +static const int NMAX = 805; + +int n; +int v[NMAX]; + +int cb(int x) +{ + int logN=1,k=0; + for(;logN<=n;logN<<=1); + + for(; logN ; logN >>=1) + { + if(logN + k <= n) + { + if(v[logN+k] <= x) + k+=logN; + } + } + return k; +} + +void Solve() +{ + int ct =0; + for(int i = 1; i< n; ++i) + { + for(int j = i + 1; j<= n; ++j) + { + int x = cb(v[i]+v[j]); + ct += (x - j); + } + } + printf("%d",ct); +} + +void ReadInput() +{ + scanf("%d",&n); + for(int i = 1;i<=n ; ++i) + scanf("%d",&v[i]); + std::sort(v+1,v+n+1); +} + +int main() +{ + ReadInput(); + Solve(); +} \ No newline at end of file diff --git a/nrtri/bruteforce/86ec9d76-409f-11ec-9e6f-dc7196033e92.cpp b/nrtri/bruteforce/86ec9d76-409f-11ec-9e6f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eab63391875c722c6e5545c963c22cd5bd44e58d --- /dev/null +++ b/nrtri/bruteforce/86ec9d76-409f-11ec-9e6f-dc7196033e92.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; +ifstream in ("nrtri.in"); +ofstream out("nrtri.out"); + +int x[801],n; + +int main() +{ + int piv1,piv2,piv3; + long long int s=0; + in>>n; + for(int i=0;i>x[i]; + sort(x,x+n); + for(piv1=0;piv1=x[piv3])++s; + else break; + out< +#include +#include + +using namespace std; +ifstream in ("nrtri.in"); +ofstream out("nrtri.out"); + +int x[801],n; + +int main() +{ + int piv1,piv2,piv3; + long long int s=0; + in>>n; + for(int i=0;i>x[i]; + sort(x,x+n); + for(piv1=0;piv1=x[piv3])++s; + else break; + out< +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +const int LMAX = 805; +int v[LMAX], n, ans; + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) + cin >> v[i]; + + sort(v + 1, v + n + 1); + for (int i = 1; i <= n - 2; ++i) + { + int k = i + 1; + for (int j = k; j <= n; ++j) + { + while (k <= n && v[k] <= v[i] + v[j]) + k++; + if (k > j) + ans += k - j - 1; + } + } + cout << ans; + return 0; +} diff --git a/nrtri/bruteforce/8a60e198-409f-11ec-839b-dc7196033e92.cpp b/nrtri/bruteforce/8a60e198-409f-11ec-839b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08a8c23d745d056a704881e1d46aa7985aa8ba25 --- /dev/null +++ b/nrtri/bruteforce/8a60e198-409f-11ec-839b-dc7196033e92.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int caut(int *v, int n, int x) { + int pos, i; + for (pos = 1; pos < n; pos <<= 1); + for (i = 0; pos; pos >>= 1) + if (i + pos <= n - 1 && v[i + pos] <= x) + i += pos; + return i; +} + +int main() +{ + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n, l[30000], sol = 0; + scanf("%i", &n); + for (int i = 0; i < n; i++) + scanf("%i", &l[i]); + sort(l, l + n); + for (int i = 0; i < n; i++) + for(int j = i + 1; j < n; j++) { + sol += caut(l, n, l[i] + l[j]) - j; + } + printf("%i", sol); + return 0; +} diff --git a/nrtri/bruteforce/8a90986a-409f-11ec-9a03-dc7196033e92.cpp b/nrtri/bruteforce/8a90986a-409f-11ec-9a03-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a212c13e598c4fa65a2a9b90dbf8e6c011ca84 --- /dev/null +++ b/nrtri/bruteforce/8a90986a-409f-11ec-9a03-dc7196033e92.cpp @@ -0,0 +1,26 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int n, v[8005], ans; + +int main() +{ + cin >> n; + for(int i = 1; i <= n; ++i) + cin >> v[i]; + sort(v + 1, v + n + 1); + for(int i = 1; i <= n; ++i) + for(int j = i + 1; j <= n; ++j) + for(int k = j + 1; k <= n; ++k) + if(v[i] + v[j] >= v[k]) + ans++; + else + break; + cout << ans; + return 0; +} diff --git a/nrtri/bruteforce/8ac68d5b-409f-11ec-805d-dc7196033e92.cpp b/nrtri/bruteforce/8ac68d5b-409f-11ec-805d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4dce781d367c1fb657958fa7ba13e4ebabea4b2 --- /dev/null +++ b/nrtri/bruteforce/8ac68d5b-409f-11ec-805d-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; + +ifstream cin ("nrtri.in"); + +ofstream cout ("nrtri.out"); + +const int L =9; +int v[801]; + +int main() + +{ + int n; + long long nr=0; + cin>>n; + for(int i=1;i<=n;i++){ + cin>>v[i]; + } + sort(v+1,v+n+1); + int k; + for(int i=1;i<=n;i++){ + k=i+1; + int j; + for(j=i+1;jj){ + nr=nr+k-j-1; + } + } + } + cout< +#include +#include + +using namespace std; + +const int L=10; +int n; +int v[805]; + +int caut(int x) +{ + int r=0,pas=1<>n; + for (int i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + int contor=0,r,k; + for (int i=1;i<=n;i++) + { + k=i+1; + for (int j=i+1;j<=n;j++) + { + while (k<=n && v[k]<=v[i]+v[j]) + { + k++; + } + if (k>j) + { + contor+=k-j-1; + } + } + } + out< +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int v[805]; + +int main() +{ + int n,rez=0; + in>>n; + for(int i=1; i>v[i]; + } + sort(v+1,v+n+1); + int k; + for(int i=1; i<=n; i++) + { + k=i+1; + for(int j=i+1; jj) + { + rez+=k-j-1; + } + } + } + out< +#include +#include + +using namespace std; + +const int L=10; +int n; +int v[805]; + +int caut(int x) +{ + int r=0,pas=1<>n; + for (int i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + int contor=0,ras; + for (int i=1;i<=n-2;i++) + { + for (int j=i+1;j +#include +#include + +using namespace std; + +const int L=10; +int n; +int v[805]; + +int caut(int x) +{ + int r=0,pas=1<>n; + for (int i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + int contor=0,ras; + for (int i=1;i<=n-2;i++) + { + for (int j=i+1;j +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int n,a[1005],ans; +int cbr(int x){ + int step=1; + while(step<=n) step<<=1; + int i=0; + while(step>0){ + if(i+step<=n && a[i+step]<=x) + i+=step; + step>>=1; + } + return i; +} +int main(){ + cin>>n; + for(int i=1;i<=n;i++) + cin>>a[i]; + sort(a+1,a+n+1); + int r,d; + for(int i=1;i +#define N 800 +#define B 60000 +#define d(x) x&(-x) +using namespace std; + +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); + +int v[N + 1]; +int AIB[B + 1]; + +void updateAIB(int x){ + for(; x <= B; x += d(x)) + AIB[x]++; +} + +int queryAIB(int x){ + int ans = 0; + + for(; x > 0; x -= d(x)) + ans += AIB[x]; + + return ans; +} + +int main(){ + int n; + cin>>n; + + for(int i = 0; i < n; i++){ + cin>>v[i]; + //updateAIB(v[i]); + } + + int ans = 0; + for(int i = 0; i < n; i++){ + for(int j = i + 1; j < n; j++){ + ans += (queryAIB(v[i] + v[j]) - queryAIB(abs(v[i] - v[j]) - 1)); + updateAIB(v[j]); + } + + for(int j = 1; j <= B; j++) + AIB[j] = 0; + } + + cout< +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, i, k, j, v[30001], nrtri=0; + f >> n; + + for(int i = 0; i < n; i ++) + f >> v[i]; + + sort(v, v + n); + + for(i = 0; i < n - 2; i ++) + for(k = i + 1; k < n - 1; k ++) + { + j = k; + while((j < n - 1) and (v[i] + v[k]) >= v[j + 1]) + { + nrtri++; + j++; + } + + } + + + g << nrtri; + return 0; +} \ No newline at end of file diff --git a/nrtri/bruteforce/8f48c7a9-409f-11ec-aa31-dc7196033e92.cpp b/nrtri/bruteforce/8f48c7a9-409f-11ec-aa31-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ae6f47f8ea669e6f59540a83759617243dda36b --- /dev/null +++ b/nrtri/bruteforce/8f48c7a9-409f-11ec-aa31-dc7196033e92.cpp @@ -0,0 +1,20 @@ +#include +#include + +int main() { + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n; + scanf("%d", &n); + int len[2 + n]; + for (int i = 1; i <= n; i++) + scanf("%d", &len[i]); + std::sort(len + 1, len + n + 1); + int sol = 0; + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + for (int k = j + 1; k <= n && len[i] + len[j] >= len[k]; k++) + sol++; + printf("%d", sol); + return 0; +} diff --git a/nrtri/bruteforce/91b81bd8-409f-11ec-9470-dc7196033e92.cpp b/nrtri/bruteforce/91b81bd8-409f-11ec-9470-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f67ac7ba6a4bfc968636847ca31d794c592d8c04 --- /dev/null +++ b/nrtri/bruteforce/91b81bd8-409f-11ec-9470-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include + +using namespace std; + +int main() +{ + int N, length[801], rezult = 0, aux, ok; + freopen("nrtri.in", "rt", stdin); + freopen("nrtri.out", "wt", stdout); + cin >> N; + + for(int i = 0; i < N; i++) + cin >> length[i]; + + if(N == 1 || N == 2) + cout << 0; + else{ + do{ + ok = 0; + for(int i = 0; i < N - 1; i++) + if(length[i] < length[i+1]){ + aux = length[i]; + length[i] = length[i+1]; + length[i+1] = aux; + ok = 1; + } + }while(ok == 1); + + for(int i = 0; i < N - 2; i++) + for(int j = i + 1; j < N - 1; j++) + for(int l = j + 1; l < N && (length[i] <= length[j] + length[l]); l++) + rezult++; + cout << rezult; + + } + return 0; +} diff --git a/nrtri/bruteforce/9219b00b-409f-11ec-bec0-dc7196033e92.cpp b/nrtri/bruteforce/9219b00b-409f-11ec-bec0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73387d520bc9791fb1dfbfd42e9baa163bcefae7 --- /dev/null +++ b/nrtri/bruteforce/9219b00b-409f-11ec-bec0-dc7196033e92.cpp @@ -0,0 +1,28 @@ +#include +#include +using namespace std; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + int n,i,j,aux,nr=0,k; + cin>>n; + int v[n+1]; + for(i=1; i<=n; i++) + cin>>v[i]; + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + if(v[i] + +using namespace std; + +long v[900], i, j, k, n, r; + +int main() + +{ + ifstream f("nrtri.in"); + + ofstream g("nrtri.out"); + + f >> n; + + for (i = 1; i <= n; i++) + f >> v[i]; + + sort(v + 1, v + n + 1); + + for (i = 1; i <= n; i++) + for (j = i + 1; j <= n; j++) + + for (k = j + 1; k <= n && v[k] <= v[i] + v[j]; k++) + r++; + + g << r; + + return 0; +} diff --git a/nrtri/bruteforce/951cfd35-409f-11ec-8907-dc7196033e92.cpp b/nrtri/bruteforce/951cfd35-409f-11ec-8907-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a7db48a663224947f0d8b5ceceb8a142873c15b --- /dev/null +++ b/nrtri/bruteforce/951cfd35-409f-11ec-8907-dc7196033e92.cpp @@ -0,0 +1,69 @@ +/*#include +#include +#include + +using namespace std; + +long long rasp; +long i,j,q,v[1000],n,st,dr,mij; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + scanf("%ld",&n); + for (i=1;i<=n;i++) scanf("%ld",&v[i]); + sort(v+1,v+n+1); + rasp=0; + for (i=1;i<=n-1;i++) + for (j=i+1;j<=n;j++) + { + st=j+1; dr=n; + mij=(st+dr)/2; + while (stv[i]+v[j]) dr=mij-1; + else if (v[j]>v[i]+v[mij]) st=mij+1; + else break; + } + for (q=mij;q<=n;q++) + if ((v[i]+v[j]>=v[q]) && (v[i]+v[q]>=v[j]) && (v[j]+v[q]>=v[i]) && (i!=q) && (j!=q)) + { + rasp++; + //printf("%ld %ld %ld\n",v[i],v[j],v[q]); + } + else if ((v[i]+v[j]=j+1;q--) + if ((v[i]+v[j]>=v[q]) && (v[i]+v[q]>=v[j]) && (v[j]+v[q]>=v[i]) && (i!=q) && (j!=q)) + { + rasp++; + //printf("%ld %ld %ld\n",v[i],v[j],v[q]); + } + else if ((v[i]+v[j] +#include + +using namespace std; + +long v[1000],i,j,k,n,rasp; + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + scanf("%ld",&n); + for (i=1;i<=n;i++) scanf("%ld",&v[i]); + rasp=0; + sort(v+1,v+n+1); + for (i=1;i<=n;i++) + for (j=i+1;j<=n;j++) + for (k=j+1;v[k]<=v[i]+v[j] && k<=n;k++) rasp++; + printf("%ld",rasp); + return 0; +} diff --git a/nrtri/bruteforce/954d5ace-b838-41b8-99d7-42c8230787bb.cpp b/nrtri/bruteforce/954d5ace-b838-41b8-99d7-42c8230787bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f0f5bff577c0d59b9ee265dd711185389d880cb --- /dev/null +++ b/nrtri/bruteforce/954d5ace-b838-41b8-99d7-42c8230787bb.cpp @@ -0,0 +1,42 @@ +#include +#include +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +short a[2001]; +int n,pos; +int cautbin(int val) +{ + pos=0; + for(int msk=1<<30; msk>0; msk/=2) + { + if(pos+msk<=n && a[pos+msk]<=val) + pos+=msk; + } + if(a[pos]==val) + return pos; + return -1; +} + + +int main() +{ + int k,cont=0,x; + cin >> n; + for(int i=1;i<=n;i++) + cin >> a[i]; + sort(a+1, a+n+1); + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + x=a[i]+a[j]; + k=cautbin(x); + cont=cont+pos-j; + } + } + cout << cont; + return 0; +} diff --git a/nrtri/bruteforce/95823126-409f-11ec-b8e8-dc7196033e92.cpp b/nrtri/bruteforce/95823126-409f-11ec-b8e8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f75e97c05cb7b0ba2a42b38cca65176151dc8306 --- /dev/null +++ b/nrtri/bruteforce/95823126-409f-11ec-b8e8-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +using namespace std; +#define DIM 1810 +int v[DIM]; +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + int n,c=0; + fin>>n; + for(int i=1;i<=n;i++){ + fin>>v[i]; + } + sort(v,v+n+1); + for(int i=1;i<=n-2;i++){ + for(int j=i+1;j<=n-1;j++){ + for(int k=j+1;k<=n;k++){ + if(v[i]+v[j]>=v[k]){ + c++; + } + else{ + k=n+1; + } + } + } + } + fout< +#include +using namespace std; +long v[1000],i,j,k,n,rasp; +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + scanf("%ld",&n); + for (i=1;i<=n;i++) scanf("%ld",&v[i]); + rasp=0; + sort(v+1,v+n+1); + for (i=1;i<=n;i++) + for (j=i+1;j<=n;j++) + for (k=j+1;k<=n;k++) if (v[k]<=v[i]+v[j]) rasp++; + else break; + printf("%ld",rasp); + return 0; +} diff --git a/nrtri/bruteforce/985368df-409f-11ec-9fb4-dc7196033e92.cpp b/nrtri/bruteforce/985368df-409f-11ec-9fb4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65fab9370b567baac2b9d83af52050518f345564 --- /dev/null +++ b/nrtri/bruteforce/985368df-409f-11ec-9fb4-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include +#include +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int v[1000],n,nrtri; + +int main() +{ + fin>>n; + for(int i=1;i<=n;i++)fin>>v[i]; + sort(v+1,v+n+1); + for(int i=1;i<=n-2;i++) + { + for(int j=i+1;j<=n-1;j++) + { + int k=j+1; + while(v[i]+v[j]>=v[k] && k<=n) + { + nrtri++; + k++; + } + } + } + fout< +#include +#include +#define MAX 800 + +using namespace std; + +int main(){ + ifstream in("nrtri.in"); + ofstream out("nrtri.out"); + int nr, i, j, k, arr[MAX]; + in >> nr; + for (i = 0; i != nr; i++){ + in >>arr[i]; + } + sort(arr, arr + nr); + int sum = 0; + for (i = 0 ;i < nr; i++){ + for (j = i + 1 ;j < nr; j++){ + for (k = j + 1 ;k < nr; k++){ + if(arr[i] + arr[j] >= arr[k] && arr[i] + arr[k] >= arr[j] && arr[k] + arr[j] >= arr[i] ) { + sum++; + } + else break; + } + } + } + out < +#include + +using namespace std; + +int main() { + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + int n,sir[801],contor=0,ok=1; + f>>n; + for(int i=1;i<=n;i++) { + f>>sir[i]; + } + sort(sir+1,sir+n+1); + for(int i=1;i<=n-2;i++){ + for(int j=i+1;j<=n-1;j++){ + ok=1; + for(int k=j+1;k<=n && ok;k++){ + if(sir[i]+sir[j]>=sir[k]){ + contor++; + }else{ + ok=0; + } + } + } + } + g< +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int v[807]; +int main() +{ + int n,nrtri=0; + f>>n; + for(int i=0;i>v[i]; + sort(v,v+n); + for(int i=0;i +#include +using namespace std; +int v[100001]; +void qSort(int v[100001],int st,int dr){ +int minim,maxim,temp,mijl; +maxim=dr;minim=st; +mijl=v[minim+(maxim-minim)/2]; +do +{ + while(v[minim]mijl) maxim--; + if(minim<=maxim){ + temp=v[minim]; + v[minim++]=v[maxim]; + v[maxim--]=temp;} +}while (minim<=maxim); +if(st>n; + for(i=0;i>v[i]; + qSort(v,0,n-1); + for(i=0;i +#include +#include + +using namespace std; + +const int L=10; +int n; +int v[805]; + +int caut(int x) +{ + int r=0,pas=1<>n; + for (int i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + int contor=0,r,k; + for (int i=1;i<=n;i++) + { + k=i+1; + for (int j=i+1;j<=n;j++) + { + while (k<=n && v[k]<=v[i]+v[j]) + { + k++; + } + if (k>j) + { + contor+=k-j-1; + } + } + } + out< +#include +#include + +using namespace std; +ifstream r("nrtri.in"); +ofstream w("nrtri.out"); +int v[801]; +int main() +{ + int n, x=0, y=1, yy, z=2, zz, nr=0, cnt=0; + r>>n; + for(int i=0; i>v[i]; + } + sort(v, v+n); + while(x<=n-3) + { + yy=y; + zz=z; + while(yy<=n-2) + { + while(v[x]+v[yy]>=v[zz] && zz<=n-1) + { + zz++; + cnt++; + } + yy++; + nr+=cnt; + cnt--; + } + z++; + y++; + x++; + cnt=0; + } + w< + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int n,a[900],s=0; + +int main() +{ + in >> n; + for(int i=1; i<=n; i++) + in >> a[i]; + sort(a+1,a+n+1); + for(int i=1; i0){ + if(b+x <= n && a[b+x]<=sum) + x+=b; + b/=2; + } + s+=x-j; + } + } + out << s; + return 0; +} diff --git a/nrtri/bruteforce/9b9d8ac5-cfea-4fd2-958e-c65c7f9e935f.cpp b/nrtri/bruteforce/9b9d8ac5-cfea-4fd2-958e-c65c7f9e935f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a933720a404d334436e3499d05f77abca0991509 --- /dev/null +++ b/nrtri/bruteforce/9b9d8ac5-cfea-4fd2-958e-c65c7f9e935f.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,i,v[1000],j,k,sol; +int main() +{ + f>>n; + for(i=1;i<=n;++i) f>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;++i) + for(j=i+1;j=v[k];++k) + sol++; + g< +#include +#include + +using namespace std; + +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); + +int i, j, n, sum, x, a[801], mx = 1024, c; + +int bs(){ + int step = mx, s = 0; + + while(step > 0){ + if(s + step <= n && a[s + step] <= sum) + s += step; + + step /= 2; + } + + return s; +} + +int main(){ + in >> n; + + for(i = 1; i <= n; i++) + in >> a[i]; + + sort(a + 1, a + n + 1); + + for(i = 1; i <= n - 1; i++) + for(j = i + 1; j <= n; j++){ + sum = a[i] + a[j]; + x = bs(); + c += x - j; + } + + out << c; + + return 0; +} diff --git a/nrtri/bruteforce/9cd1bc55-409f-11ec-88c5-dc7196033e92.cpp b/nrtri/bruteforce/9cd1bc55-409f-11ec-88c5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b9f93b9555dd57aec0ef2c1b1786865a8c589ae --- /dev/null +++ b/nrtri/bruteforce/9cd1bc55-409f-11ec-88c5-dc7196033e92.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,i,v[1000],j,ii,rez; +int main() +{ + in>>n; + for(i=1;i<=n;i++) + { + in>>v[i]; + } + sort(v+1,v+n+1); + for(i=1;i<=n-2;i++) + { + for(j=i+1;j=v[ii];ii++) + { + rez++; + } + } + } + out< +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,i,v[1000],j,k,sol; +int main() +{ + f>>n; + for(i=1;i<=n;++i) f>>v[i]; + sort(v+1,v+n+1); + for(i=1;i<=n-2;++i) + for(j=i+1;j=v[k];++k) + sol++; + g< +#define N 805 +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int Triunghi(int a, int b, int c) +{ + if(c<= a+b ) + return 1; + return 0; +} + +void Sortare(int n, int a[]) +{ + int i, ordo=0; + int aux; + while(ordo==0) + { + ordo=1; + for(i=1; i<=n-1; i++) + if(a[i]>a[i+1]) + { + aux=a[i]; + a[i]=a[i+1]; + a[i+1]=aux; + ordo=0; + } + } +} + + +int main() +{ + int n, a[N], i, j, ct=0, k; + fin >> n; + for(i=1; i<=n ; i++) + fin>>a[i]; + + Sortare(n, a); + for(i=1; i<=n-2; i++) + { + k=i+2; + for(j=i+1; j<=n-1; j++) + { + + while(Triunghi(a[i], a[j], a[k])==1 && k<=n) + k++; + if (k>j) + ct=ct+k-j-1; + } + } + + fout< +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int n,i,j,k,nr; +int v[801]; + +int triunghi(int a, int b, int c) +{ + if (a+b>=c && a+c>=b && b+c>=a) + return 1; + return 0; +} + +int main() +{ + f>>n; + for (i=1;i<=n;i++) + f>>v[i]; + sort(v+1,v+n+1); + for (i=1;i<=n-2;i++) + for (j=i+1;j<=n-1;j++) + for (k=j+1;k<=n && triunghi(v[i],v[j],v[k]);k++) + nr++; + g< +#include + +using namespace std; + +int main() +{ + ifstream fin("nrtri.in"); + ofstream fout("nrtri.out"); + + int n, v[805]; + fin>>n; + for (int i=0; i>v[i]; + } + + sort(v,v+n); + int rez=0; + for (int i=0; i= v[k]) + rez++; + else + break; + } + } + } + + fout< +#include +#include + +int a[800]; + +int main() { + std::ifstream is{"nrtri.in"}; + + int n; + is >> n; + + for (auto i = 0; i < n; ++i) + is >> a[i]; + + std::sort(a, a + n); + + std::int64_t s{}; + + for (auto i = 0; i < n - 2; ++i) + for (auto j = i + 1; j < n - 1; ++j) + for (auto k = j + 1; k < n; ++k) + if (a[i] + a[j] >= a[k]) + ++s; + else + break; + + std::ofstream{"nrtri.out"} << s; +} diff --git a/nrtri/bruteforce/a3e01c04-409f-11ec-ba44-dc7196033e92.cpp b/nrtri/bruteforce/a3e01c04-409f-11ec-ba44-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..409156a9bfe9f7677578d99f495c45c5c72022c9 --- /dev/null +++ b/nrtri/bruteforce/a3e01c04-409f-11ec-ba44-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include + +#define MAXN 800 + +int n, result; +int v[MAXN]; + +void read() +{ + scanf("%d",&n); + for (int i = 0; i < n; ++i) { + scanf("%d",&v[i]); + } +} + +void print() +{ + printf("%d\n",result); +} + +int cmp (const void *a, const void *b) +{ + return *((int *) a) - *((int *) b); +} + +void compute() +{ + qsort(v,n,sizeof(int),cmp); + for (int k = n - 1; k > 1; --k) { + int i = 0, j = k - 1; + while (i < j) { + int sum = v[i] + v[j]; + if (sum >= v[k]) { + result += j - i; + --j; + } else { + ++i; + } + } + } +} + +int main() +{ + freopen("nrtri.in","r",stdin); + freopen("nrtri.out","w",stdout); + read(); + compute(); + print(); + return 0; +} diff --git a/nrtri/bruteforce/a450da6a-104c-4286-a64e-5c18d263934b.cpp b/nrtri/bruteforce/a450da6a-104c-4286-a64e-5c18d263934b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b9cb1fc993f96592d2c5742ff8fd46e2cfc20f2 --- /dev/null +++ b/nrtri/bruteforce/a450da6a-104c-4286-a64e-5c18d263934b.cpp @@ -0,0 +1,68 @@ +#include +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int Triangle (int a, int b, int c) +{ + return ((a+b>=c) && (b+c>=a) && (a+c>=b)); +} +void Swap(int &a, int &b) +{ + int temp; + temp = b; + b=a; + a = temp; +} +void BubbleSort (int *v, int n) // v[] +{ + int i, j; + + for (i=1; i<=n; i++) + { + for (j=1; jv[j+1]) + { + Swap (v[j],v[j+1]); + } + } + } +} + +int main() +{ + int N, V[803], contor=0; + cin>> N; + + for(int i=1; i<=N; i++) + cin>> V[i]; + + BubbleSort(V,N); + + //for (int i=1; i + +#include + +int v[2000]; + +void myqsort(int be, int en) +{ + + int aux, b = be, e = en, pivot = v[be + rand() % (en - be + 1)]; + + while (b <= e) + { + + while (pivot > v[b]) + + ++b; + + while (pivot < v[e]) + + --e; + + if (b <= e) + { + + aux = v[b]; + v[b] = v[e]; + v[e] = aux; + + ++b; + --e; + } + } + + if (be < e) + + myqsort(be, e); + + if (b < en) + + myqsort(b, en); +} + +int main() + +{ + + FILE *fin, *fout; + + int n, i, j, k, tri; + + fin = fopen("nrtri.in", "r"); + + fscanf(fin, "%d", &n); + + for (i = 0; i < n; i++) + + fscanf(fin, "%d", &v[i]); + + fclose(fin); + + myqsort(0, n - 1); + + tri = 0; + + for (i = 0; i < n - 2; i++) + { + + k = i + 2; + + for (j = i + 1; j < n - 1; j++) + { + + if (k == j) + + ++k; + + while (k < n && v[i] + v[j] >= v[k]) + + ++k; + + tri += k - j - 1; + } + } + + fout = fopen("nrtri.out", "w"); + + fprintf(fout, "%d\n", tri); + + fclose(fout); + + return 0; +} diff --git a/nrtri/bruteforce/af811d89-3d11-4e58-a9fa-fec2218bbcb8.cpp b/nrtri/bruteforce/af811d89-3d11-4e58-a9fa-fec2218bbcb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ea218e95443cf21fa4256f57594ecfd9070402d --- /dev/null +++ b/nrtri/bruteforce/af811d89-3d11-4e58-a9fa-fec2218bbcb8.cpp @@ -0,0 +1,36 @@ +#include +#include +#include +using namespace std; +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int N,i,a[805],contor=0,j,h; + fin >> N; + for (i=1;i<=N;++i) + { + fin >> a[i]; + } + sort(a+1,a+N+1); + for (i=1;i<=N;++i) + { + for (j=i+1;j<=N;++j) + { + for (h=j+1;h<=N;++h) + { + if (a[i]+a[j]>=a[h]) + { + ++contor; + } + else + { + break; + } + } + } + } + fout << contor; + return 0; +} diff --git a/nrtri/bruteforce/b58ca85e-359a-4ca1-8186-2fb84f5dc7b7.cpp b/nrtri/bruteforce/b58ca85e-359a-4ca1-8186-2fb84f5dc7b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4d9697064c36200158f0163f21e7a190d79934b --- /dev/null +++ b/nrtri/bruteforce/b58ca85e-359a-4ca1-8186-2fb84f5dc7b7.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n,v[801],nr=0,i,j,k,ok; +int main() +{ + f>>n; + for(i=1; i<=n; i++) + f>>v[i]; + sort(v+1,v+n+1); + for(i=1; i<=n; i++) + for(j=i+1; j<=n; j++) + { + ok=1; + for(k=j+1; k<=n && ok==1; k++) + { + if(v[i]+v[j]>=v[k]) + nr++; + if(v[i]+v[j] +#include +using namespace std; +ifstream cin("nrtri.in"); +ofstream cout("nrtri.out"); +int n,a[1005],ans; +int cbr(int x){ + int step=1; + while(step<=n) step<<=1; + int i=0; + while(step>0){ + if(i+step<=n && a[i+step]<=x) + i+=step; + step>>=1; + } + return i; +} +int main(){ + cin>>n; + for(int i=1;i<=n;i++) + cin>>a[i]; + sort(a+1,a+n+1); + int r,d; + for(int i=1;i +#include +using namespace std; +ifstream in("nrtri.in"); +ofstream out("nrtri.out"); +int n,v[805],i,j,k,rez,gasit2; +void citire() +{ + in>>n; + for(i=1; i<=n; i++) + in>>v[i]; + sort(v+1,v+n+1); +} +void cautare() +{ + for(i=1; i<=n-2; i++) + for(j=i+1; j<=n-1; j++) + for(k=j+1; k<=n &&v[i]+v[j]>=v[k]; k++) + rez++; +} +void afisare() +{ + out< +#include +#include +#include + +auto count(std::vector& v) -> int +{ + std::size_t const n = v.size(); + int count{ 0 }; + + for(std::size_t i = 0; i < n - 2; ++i) { + for(std::size_t j = i + 1; j < n - 1; ++j) { + for(std::size_t k = j + 1; k < n; ++k) { + if(v[i] + v[j] >= v[k]) { + ++count; + } + else { + break; + } + } + } + } + + return count; +} + +auto main() noexcept -> int +{ + std::ifstream f{ "nrtri.in" }; + std::ofstream g{ "nrtri.out" }; + + int num{ 0 }; + std::vector nums{}; + + f >> num; + nums.reserve(static_cast(num)); + + for(int i = 0; i < num; ++i) { + int x{ 0 }; + f >> x; + nums.push_back(x); + } + + std::sort(nums.begin(), nums.end()); + g << count(nums) << std::endl; +} diff --git a/nrtri/bruteforce/ca377aaf-f49f-4ad2-8ea2-fc356a17ca50.cpp b/nrtri/bruteforce/ca377aaf-f49f-4ad2-8ea2-fc356a17ca50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82528c5157f8e98de624001108dc61093fbd1706 --- /dev/null +++ b/nrtri/bruteforce/ca377aaf-f49f-4ad2-8ea2-fc356a17ca50.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); +int n, v[801], s; + +int main() +{ + f>>n; + for(int i=0; i>v[i]; + sort(v, v+n); + for(int x=0; x +#include + +using namespace std; + +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); + +int v[801]; + +int main() +{ + int cnt=0,i,j,k,n; + cin>>n; + for (i=0;i>v[i]; + sort(v,v+n); + for (i=0;i +#include +#include +#include +using namespace std; + + +int main(){ + ifstream f("nrtri.in"); + ofstream f1("nrtri.out"); + int n; + f>>n; + int v[n]; + for (int i=0;i>v[i]; + sort (v,v+n); + int nr=0; + for (int i=0;i +#include + +int main() { + freopen("nrtri.in", "r", stdin); + freopen("nrtri.out", "w", stdout); + int n; + scanf("%d", &n); + int len[2 + n]; + for (int i = 1; i <= n; i++) + scanf("%d", &len[i]); + std::sort(len + 1, len + n + 1); + int sol = 0; + for (int i = 1; i <= n; i++) + for (int j = i + 1; j <= n; j++) + for (int k = j + 1; k <= n && len[i] + len[j] >= len[k]; k++) + sol++; + printf("%d", sol); + return 0; +} diff --git a/nrtri/bruteforce/d8f8add3-fe6b-41fd-a9e1-46a5384eb000.cpp b/nrtri/bruteforce/d8f8add3-fe6b-41fd-a9e1-46a5384eb000.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b2af0e90f0486b7d4a8b7a73a91ce3bfb6da559 --- /dev/null +++ b/nrtri/bruteforce/d8f8add3-fe6b-41fd-a9e1-46a5384eb000.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; + +ifstream f("nrtri.in"); +ofstream g("nrtri.out"); + +int main() +{ + int n, v[801], s=0; + f>>n; + for(int i=0; i>v[i]; + sort(v,v+n); + for(int i=0; i=v[k]) + s++; + else break; + g< +#include +#include +#include + +using namespace std; + +ifstream fin("nrtri.in"); +ofstream fout("nrtri.out"); + +int main() +{ + int n; + fin >> n; + vector v(n); + int cnt = 0; + for(auto &it:v) + fin >> it; + sort(v.begin(), v.end()); + for(int i = 0; i <= n - 3; ++i) + for(int j = i + 1; j <= n - 2; ++j) + for(int k = j + 1; k < n; ++k) + if(v[i] + v[j] >= v[k]) + cnt++; + else + break; + fout << cnt; + return 0; +} diff --git a/nrtri/bruteforce/eab600da-392c-448c-95b8-beb6200d06c5.cpp b/nrtri/bruteforce/eab600da-392c-448c-95b8-beb6200d06c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8c02709c5de780c732abd049c6872c144f0e6d3 --- /dev/null +++ b/nrtri/bruteforce/eab600da-392c-448c-95b8-beb6200d06c5.cpp @@ -0,0 +1,18 @@ +#include +#include +using namespace std; +int v[802], i, j, q, cnt, n; +int main() +{ + ifstream f("nrtri.in"); + ofstream g("nrtri.out"); + f >> n; + for(i = 1; i <= n; i ++) + f >> v[i]; + sort(v + 1, v + n + 1); + for(i = 1; i <= n - 2; i ++) + for(j = i + 1; j < n; j ++) + for(q = j + 1; q <= n && v[i] + v[j] >= v[q]; q ++) cnt ++; + g << cnt; + return 0; +} diff --git a/nrtri/bruteforce/fdcc21f8-9f30-451e-8029-93fc14d785a6.cpp b/nrtri/bruteforce/fdcc21f8-9f30-451e-8029-93fc14d785a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..15baf44b4be1a8e908a3d207fa8f21ccddf12715 --- /dev/null +++ b/nrtri/bruteforce/fdcc21f8-9f30-451e-8029-93fc14d785a6.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; +ifstream cin ("nrtri.in"); +ofstream cout ("nrtri.out"); +int v[801]; +int main() +{ + int n,c=0,i,i2,i3; + cin>>n; + for (i=0;i>v[i]; + sort(v,v+n); + for (i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const int mx=1000; + +ifstream in("party.in"); +ofstream out("party.out"); + +vector g[mx],gt[mx],q; +bool visited[mx],possible=true; +int n,m,result[mx],cnt[mx]; + +int node(int x){ + if(x<0){ + return n+abs(x); + } + return x; +} + +int val(int node){ + if(node>n){ + return n-node; + } + return node; +} + + +void read(){ + in>>n>>m; + int a,b,c; + for(int i=0;i>a>>b>>c; + if(c==3){ + a=-a; + b=-b; + } + else if(c==1){ + b=-b; + } + else if(c==2){ + a=-a; + } + g[node(-a)].push_back(node(b)); + gt[node(b)].push_back(node(-a)); + g[node(-b)].push_back(node(a)); + gt[node(a)].push_back(node(-b)); + } +} + +void dfs1(int x){ + visited[x]=true; + for(int k:g[x]){ + if(!visited[k]) + dfs1(k); + } + q.push_back(x); +} + +void dfs2(int x,int value,int comp){ + cnt[x]=comp; + visited[x]=true; + result[x]=value; + result[node(-val(x))]=1-value; + + for(int k:gt[x]){ + if(!visited[k]){ + dfs2(k,value,comp); + } + } +} +void preprocess(){ + for(int i=1;i<=2*n;i++) + result[i]=-1; +} + +void solve(){ + + for(int i=1;i<=2*n;i++){ + if(!visited[i]){ + dfs1(i); + } + } + for(int i=1;i ans; + + for(int i=1;i<=n;i++) + if(result[i]) + ans.push_back(i); + + out< +#include +#include +#include + + +std::ifstream fin("party.in"); + +std::ofstream fout("party.out"); + + + +const int NMAX = 500; + + + +std::vector g[NMAX], rg[NMAX]; + +std::bitset used, ans; + +std::stack topo; + +bool not_ok; + +int n, m, x, y, c; + + + +inline int neg(int x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void DFS1(int node) + +{ + + used[node] = 1; + + for (auto& i : g[node]) + + if (used[i] == 0) + + DFS1(i); + + topo.push(node); + +} + + + +void DFS2(int node) + +{ + + if (ans[node] == 1) + + not_ok = true; + + used[node] = 1; + + ans[neg(node)] = 1; + + for (auto& i : rg[node]) + + if (used[i] == 0) + + DFS2(i); + +} + + + +void poz() + +{ + + if (c == 1) + + y += n; + + else if (c == 2) + + x += n; + + else if (c == 3) + + { + + x += n; + + y += n; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + poz(); + + int negx = neg(x), negy = neg(y); + + g[negx].push_back(y); + + g[negy].push_back(x); + + rg[x].push_back(negy); + + rg[y].push_back(negx); + + } + + + + for (int i = 1; i <= 2 * n; ++i) + + if (used[i] == 0) + + DFS1(i); + + + + used.reset(); + + + + while (!topo.empty()) + + { + + x = topo.top(); + + topo.pop(); + + if (used[x] == 0 && used[neg(x)] == 0) + + DFS2(x); + + } + + + + int cnt(0); + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + cnt++; + + fout << cnt << "\n"; + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + fout << i << "\n"; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/004b5d98-0b56-11ec-be5f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/004b5d98-0b56-11ec-be5f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2241576717ce49777437e0aa65dc2250fd4f5359 --- /dev/null +++ b/party/2sat_implication_reduction/004b5d98-0b56-11ec-be5f-2cf05d84bfc2.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/004b850a-0b56-11ec-aa15-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/004b850a-0b56-11ec-aa15-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78dfe2c5ea0680d7179048e706f2d4452489efdb --- /dev/null +++ b/party/2sat_implication_reduction/004b850a-0b56-11ec-aa15-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +vectora[202],inv[202]; + +int n,k,kk[202],ctc[202],sol[102]; + +bool viz[202]; + + + +int neg(int x) + +{ + + if(x>n) + + return x-n; + + return x+n; + +} + + + +void dfs(int x) + +{ + + viz[x]=1; + + for(auto y:a[x]) + + if(viz[y]==0) + + dfs(y); + + kk[++k]=x; + +} + + + +void dfs_inv(int x) + +{ + + viz[x]=1; + + ctc[x]=k; + + for(auto y:inv[x]) + + if(viz[y]==0) + + dfs_inv(y); + +} + + + +void kosaraju() + +{ + + int i; + + for(i=1;i<=2*n;i++) + + if(viz[i]==0) + + dfs(i); + + for(i=1;i<=2*n;i++) + + viz[i]=0; + + k=0; + + for(i=2*n;i>=1;i--) + + if(viz[kk[i]]==0) + + { + + k++; + + dfs_inv(kk[i]); + + } + +} + + + +int main() + +{ + + int m,i,x,y,p; + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>p; + + if(p==0) + + { + + a[neg(x)].push_back(y); + + a[neg(y)].push_back(x); + + inv[y].push_back(neg(x)); + + inv[x].push_back(neg(y)); + + } + + else + + if(p==1) + + { + + a[neg(x)].push_back(neg(y)); + + a[y].push_back(x); + + inv[neg(y)].push_back(neg(x)); + + inv[x].push_back(y); + + } + + else + + if(p==2) + + { + + a[neg(y)].push_back(neg(x)); + + a[x].push_back(y); + + inv[neg(x)].push_back(neg(y)); + + inv[y].push_back(x); + + } + + else + + if(p==3) + + { + + a[x].push_back(neg(y)); + + a[y].push_back(neg(x)); + + inv[neg(y)].push_back(x); + + inv[neg(x)].push_back(y); + + } + + } + + kosaraju(); + + k=0; + + for(i=1;i<=n;i++) + + if(ctc[i]>ctc[i+n]) + + sol[++k]=i; + + g< +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/03906904-6443-4917-a947-07713bf82929.cpp b/party/2sat_implication_reduction/03906904-6443-4917-a947-07713bf82929.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/03906904-6443-4917-a947-07713bf82929.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/08597a9d-d98b-4035-a7ff-1832431cfe4a.cpp b/party/2sat_implication_reduction/08597a9d-d98b-4035-a7ff-1832431cfe4a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779abbe2a478041abf51b4dcbdcdb67c356f4aac --- /dev/null +++ b/party/2sat_implication_reduction/08597a9d-d98b-4035-a7ff-1832431cfe4a.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + +vector>lv,lv2; + +vectornoduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + +int neg(int x) { + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + +void sorttop(int nod) { + + int i; + + v[nod]=1; + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/112b3187-0b54-11ec-84de-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/112b3187-0b54-11ec-84de-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..06a344585c879ece21cffa257727053441708b63 --- /dev/null +++ b/party/2sat_implication_reduction/112b3187-0b54-11ec-84de-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void dfsfirst (int nod, vector < vector < int > > &gr, + vector < bool > &folosite, stack < int > &stk) { + + folosite[nod] = true; + for (int x : gr[nod]) { + if (folosite[x]) { + continue; + } + + dfsfirst (x, gr, folosite, stk); + } + + stk.push (nod); +} + +void dfstr (int nod, vector < vector < int > > &gr, + vector < bool > &folosite, vector < int > &ctc, int &cur_ctc) { + + folosite[nod] = true; + ctc[nod] = cur_ctc; + for (int x : gr[nod]) { + if (folosite[x]) { + continue; + } + + dfstr (x, gr, folosite, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > folosite (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (folosite[i]) { + continue; + } + + dfsfirst (i, gr, folosite, stk); + } + + fill (folosite.begin (), folosite.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (folosite[cur]) { + continue; + } + + ++ nr_of_ctc; + dfstr (cur, inv, folosite, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/1af5394c-0b53-11ec-983c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/1af5394c-0b53-11ec-983c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9e45986ab1d9075797d6e03458ff09d5d223d08 --- /dev/null +++ b/party/2sat_implication_reduction/1af5394c-0b53-11ec-983c-2cf05d84bfc2.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream fin ("party.in"); + +ofstream fout ("party.out"); + + + +const int maxval = 300; + + + +int x, y, tip, n, m, i, nrc, where[maxval]; + +vector v[maxval], vt[maxval], ord, come; + +bool fol[maxval]; + + + +int no(int x) {return x + n;} + + + +void addEdge(int x, int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs(int node) + +{ + + fol[node] = 1; + + for(auto go : v[node]) + + if(!fol[go]) dfs(go); + + ord.push_back(node); + +} + + + +void dfsinv(int node) + +{ + + where[node] = nrc; + + for(auto go : vt[node]) + + if(!where[go]) dfsinv(go); + +} + + + +int main() + +{ + + fin >> n >> m; + + while(m--) + + { + + fin >> x >> y >> tip; + + if(tip == 0) addEdge(no(x), y), addEdge(no(y), x); + + else if(tip == 1) addEdge(no(x), no(y)), addEdge(y, x); + + else if(tip == 2) addEdge(no(y), no(x)), addEdge(x, y); + + else if(tip == 3) addEdge(x, no(y)), addEdge(y, no(x)); + + } + + + + for(i=1; i<=2*n; ++i) + + if(!fol[i]) dfs(i); + + + + reverse(ord.begin(), ord.end()); + + for(auto node : ord) + + if(!where[node]) + + ++nrc, dfsinv(node); + + + + for(i=1; i<=n; ++i) + + if(where[i] > where[no(i)]) come.push_back(i); + + + + fout << come.size() << '\n'; + + for(auto who : come) fout << who << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/1de6674c-0b56-11ec-bab6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/1de6674c-0b56-11ec-bab6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b984831a459df210e6cb35dec89173ab01052eb --- /dev/null +++ b/party/2sat_implication_reduction/1de6674c-0b56-11ec-bab6-2cf05d84bfc2.cpp @@ -0,0 +1,230 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = 200; + + + +int N, M; + +vector g[NMAX + 5]; + +vector t[NMAX + 5]; + + + +bool seen[NMAX + 5]; + +stack st; + +void dfs1(int node) + +{ + + seen[node] = true; + + + + for(auto it : g[node]) + + if(!seen[it]) dfs1(it); + + + + st.push(node); + +} + + + +int kComp; + +int comp[NMAX + 5]; + +void dfs2(int node) + +{ + + comp[node] = kComp; + + + + for(auto it : t[node]) + + if(comp[it] == 0) dfs2(it); + +} + + + +vector sol; + + + +int Not(int x) + +{ + + return x; + +} + + + +int Nrm(int x) + +{ + + return x + N; + +} + + + +int main() + +{ + + cin >> N >> M; + + + + for(int i = 1; i <= M; i++) + + { + + int x, y, z; + + cin >> x >> y >> z; + + + + if(z == 0) + + { + + g[Not(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Nrm(x)); + + + + t[Nrm(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Not(y)); + + } + + else if(z == 1) + + { + + g[Not(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Nrm(x)); + + + + t[Not(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Nrm(y)); + + } + + else if(z == 2) + + { + + g[Nrm(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Not(x)); + + + + t[Nrm(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Not(y)); + + } + + else + + { + + g[Nrm(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Not(x)); + + + + t[Not(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Nrm(y)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) + + if(!seen[i]) dfs1(i); + + + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(comp[node] == 0) + + { + + ++kComp; + + dfs2(node); + + } + + } + + + + for(int i = 1; i <= N; i++) + + if(comp[i + N] > comp[i]) + + sol.push_back(i); + + + + cout << sol.size() << '\n'; + + for(auto it : sol) + + cout << it << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/1de6674d-0b56-11ec-acda-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/1de6674d-0b56-11ec-acda-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2ea30710c6cee6b11c5533d8c7d39ea4389d0f0 --- /dev/null +++ b/party/2sat_implication_reduction/1de6674d-0b56-11ec-acda-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +int n, m, a, b, c, F = 101, T = 102, viz[202], uz[202]; + +vector v[2 * 202], v2[2 * 202], H, ans; + + + +void dfs(int nod){ + + uz[nod] = 1; + + for (auto it : v[nod]) if (!uz[it]) dfs(it); + + H.push_back(nod); + +} + + + +void dfs2(int nod){ + + + + uz[nod] = 0; viz[nod ^ 1] = 1; + + if (nod&1) ans.push_back(nod/2 + 1); + + for (auto it : v2[nod]) if (uz[it]) dfs2(it); + +} + + + +void add(int a, int b){ + + a = (a < 0 ? -a * 2 - 1 : a * 2 - 2); + + b = (b < 0 ? -b * 2 - 1 : b * 2 - 2); + + + + + + v[a^1].push_back(b); + + v[b^1].push_back(a); + + v2[a].push_back(b^1); + + v2[b].push_back(a^1); + +} + + + +int main(){ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + cin >> n >> m; + + + + + + for (int i=1;i<=m;i++){ + + cin >> a >> b >> c; + + if (c == 0){ + + add(a, b); + + } + + else if (c == 1){ + + add(a, -b); + + } + + else if (c == 2){ + + add(-a, b); + + } + + else{ + + add(-a, -b); + + } + + } + + + + for (int i=0;i<2*n;i++) if (!uz[i]) dfs(i); + + reverse(H.begin(), H.end()); + + for (auto it : H){ + + + + if (uz[it] && uz[it^1]) dfs2(it); + + } + + + + cout << ans.size() << '\n'; + + for (auto it : ans) + + cout << it << '\n'; + + + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/1de6674e-0b56-11ec-baa2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/1de6674e-0b56-11ec-baa2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e64023449f1fba59846143d87b59ec8e8d66d39 --- /dev/null +++ b/party/2sat_implication_reduction/1de6674e-0b56-11ec-baa2-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; +ifstream f("party.in"); +ofstream g("party.out"); +int n, m, x, y, t, comp, rez_nr; +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; +int c[2 * 100 + 5]; +stack st; +bool viz[2 * 100 + 5], rez[100 + 5]; + +int Not(int x) +{ + if(x < n) + return x + n; + return x - n; +} + +void add(int x, int y) +{ + graf[Not(x)].push_back(y); + graf[Not(y)].push_back(x); + graf_t[y].push_back(Not(x)); + graf_t[x].push_back(Not(y)); +} + +void Read() +{ + f>>n>>m; + for(int i = 1;i <= m;++i) + { + f>>x>>y>>t; + if(t == 0) + add(x, y); + else if(t == 1) + add(x, Not(y)); + else if(t == 2) + add(Not(x), y); + else + add(Not(x), Not(y)); + } +} + +void dfs(int nod) +{ + viz[nod] = true; + for(auto it : graf[nod]) + if(!viz[it]) + dfs(it); + st.push(nod); +} + +void dfs_t(int nod) +{ + c[nod] = comp; + for(auto it : graf_t[nod]) + if(!c[it]) + dfs_t(it); +} + +void Solve() +{ + for(int i = 1;i <= 2 * n;++i) + if(!viz[i]) + dfs(i); + while(!st.empty()) + { + int node = st.top(); st.pop(); + + if(!c[node]) + { + ++comp; + dfs_t(node); + } + } + for(int i = 1;i <= n;++i) + if(c[i] > c[Not(i)]) + ++rez_nr, rez[i] = true; + g< + + + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +class _2sat{ + + private: + + int n; + + vector > V,gV; + + vector uz; + + vector ord,comp; + + vector ans; + + + + void dfs1(int node){ + + uz[node]=true; + + for(auto& it:V[node]) + + if(!uz[it]) + + dfs1(it); + + ord.push_back(node); + + } + + void dfs2(int node,int c){ + + comp[node]=c; + + for(auto& it:gV[node]) + + if(!comp[it]) + + dfs2(it,c); + + } + + public: + + _2sat(int n){ + + this->n=n; + + V.resize(2*n+1); + + gV.resize(2*n+1); + + uz.resize(2*n+1); + + comp.resize(2*n+1); + + ans.resize(n+1); + + } + + void addegde(int x,int y){ + + cerr << "x,y" << " ->", debug_out(x,y); + + V[x].push_back(y); + + gV[y].push_back(x); + + } + + void solve(){ + + for(int i=1;i<=2*n;i++) + + if(!uz[i]) + + dfs1(i); + + for(int i=1,cates=0;i<=2*n;i++){ + + int node=ord[2*n-i]; + + if(!comp[node]) + + dfs2(node,++cates); + + } + + for(int i=1;i<=n;i++){ + + if(comp[i] == comp[i+n]){ + + fout<<"-1\n"; + + return; + + } + + ans[i]=(comp[i] > comp[i+n]); + + } + + int cate=0; + + for(int i=1;i<=n;i++) + + if(ans[i]) + + cate++; + + fout< vec; + +vector arb[DMAX]; + + + +int V[DMAX]; + + + +int n,m; + + + +int NOT(int node){ + + if(node > n) + + return node-n; + + return node+n; + +} + + + +int main(){ + + int t,i,j; + + int op,x,y; + + + + fin>>n>>m; + + _2sat var(n); + + for(i=1;i<=m;i++){ + + fin>>x>>y>>op; + + if(op == 1) + + y=NOT(y); + + else if(op == 2) + + x=NOT(x); + + else if(op == 3){ + + x=NOT(x); + + y=NOT(y); + + } + + var.addegde(NOT(x),y); + + var.addegde(NOT(y),x); + + } + + var.solve(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/1de66750-0b56-11ec-aab9-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/1de66750-0b56-11ec-aab9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6fa6fdb0c55ac1256012298d81a5ff2adf792a --- /dev/null +++ b/party/2sat_implication_reduction/1de66750-0b56-11ec-aab9-2cf05d84bfc2.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + + +std::ifstream fin("party.in"); + +std::ofstream fout("party.out"); + + + +const int NMAX = 500; + + + +std::vector g[NMAX], rg[NMAX]; + +std::bitset used, ans; + +std::stack topo; + +bool not_ok; + +int n, m, x, y, c; + + + +inline int neg(int x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void DFS1(int node) + +{ + + used[node] = 1; + + for (auto& i : g[node]) + + if (used[i] == 0) + + DFS1(i); + + topo.push(node); + +} + + + +void DFS2(int node) + +{ + + if (ans[node] == 1) + + not_ok = true; + + used[node] = 1; + + ans[neg(node)] = 1; + + for (auto& i : rg[node]) + + if (used[i] == 0) + + DFS2(i); + +} + + + +void poz() + +{ + + if (c == 1) + + y += n; + + else if (c == 2) + + x += n; + + else if (c == 3) + + { + + x += n; + + y += n; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + poz(); + + int negx = neg(x), negy = neg(y); + + g[negx].push_back(y); + + g[negy].push_back(x); + + rg[x].push_back(negy); + + rg[y].push_back(negx); + + } + + + + for (int i = 1; i <= 2 * n; ++i) + + if (used[i] == 0) + + DFS1(i); + + + + used.reset(); + + + + while (!topo.empty()) + + { + + x = topo.top(); + + topo.pop(); + + if (used[x] == 0 && used[neg(x)] == 0) + + DFS2(x); + + } + + + + int cnt(0); + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + cnt++; + + fout << cnt << "\n"; + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + fout << i << "\n"; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/22127b62-f52a-4d1d-bd49-20bb5e12653e.cpp b/party/2sat_implication_reduction/22127b62-f52a-4d1d-bd49-20bb5e12653e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f060d8989122d18189f43915d7bd5df2cd0f4eb --- /dev/null +++ b/party/2sat_implication_reduction/22127b62-f52a-4d1d-bd49-20bb5e12653e.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/313c29cc-b195-4c8a-905d-a67405d45836.cpp b/party/2sat_implication_reduction/313c29cc-b195-4c8a-905d-a67405d45836.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/313c29cc-b195-4c8a-905d-a67405d45836.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< + + + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +class _2sat{ + + private: + + int n; + + vector > V,gV; + + vector uz; + + vector ord,comp; + + vector ans; + + + + void dfs1(int node){ + + uz[node]=true; + + for(auto& it:V[node]) + + if(!uz[it]) + + dfs1(it); + + ord.push_back(node); + + } + + void dfs2(int node,int c){ + + comp[node]=c; + + for(auto& it:gV[node]) + + if(!comp[it]) + + dfs2(it,c); + + } + + public: + + _2sat(int n){ + + this->n=n; + + V.resize(2*n+1); + + gV.resize(2*n+1); + + uz.resize(2*n+1); + + comp.resize(2*n+1); + + ans.resize(n+1); + + } + + void addegde(int x,int y){ + + cerr << "x,y" << " ->", debug_out(x,y); + + V[x].push_back(y); + + gV[y].push_back(x); + + } + + void solve(){ + + for(int i=1;i<=2*n;i++) + + if(!uz[i]) + + dfs1(i); + + for(int i=1,cates=0;i<=2*n;i++){ + + int node=ord[2*n-i]; + + if(!comp[node]) + + dfs2(node,++cates); + + } + + for(int i=1;i<=n;i++){ + + if(comp[i] == comp[i+n]){ + + fout<<"-1\n"; + + return; + + } + + ans[i]=(comp[i] > comp[i+n]); + + } + + int cate=0; + + for(int i=1;i<=n;i++) + + if(ans[i]) + + cate++; + + fout< vec; + +vector arb[DMAX]; + + + +int V[DMAX]; + + + +int n,m; + + + +int NOT(int node){ + + if(node > n) + + return node-n; + + return node+n; + +} + + + +int main(){ + + int t,i,j; + + int op,x,y; + + + + fin>>n>>m; + + _2sat var(n); + + for(i=1;i<=m;i++){ + + fin>>x>>y>>op; + + if(op == 1) + + y=NOT(y); + + else if(op == 2) + + x=NOT(x); + + else if(op == 3){ + + x=NOT(x); + + y=NOT(y); + + } + + var.addegde(NOT(x),y); + + var.addegde(NOT(y),x); + + } + + var.solve(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/32ef009f-0b56-11ec-94a8-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/32ef009f-0b56-11ec-94a8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e64023449f1fba59846143d87b59ec8e8d66d39 --- /dev/null +++ b/party/2sat_implication_reduction/32ef009f-0b56-11ec-94a8-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; +ifstream f("party.in"); +ofstream g("party.out"); +int n, m, x, y, t, comp, rez_nr; +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; +int c[2 * 100 + 5]; +stack st; +bool viz[2 * 100 + 5], rez[100 + 5]; + +int Not(int x) +{ + if(x < n) + return x + n; + return x - n; +} + +void add(int x, int y) +{ + graf[Not(x)].push_back(y); + graf[Not(y)].push_back(x); + graf_t[y].push_back(Not(x)); + graf_t[x].push_back(Not(y)); +} + +void Read() +{ + f>>n>>m; + for(int i = 1;i <= m;++i) + { + f>>x>>y>>t; + if(t == 0) + add(x, y); + else if(t == 1) + add(x, Not(y)); + else if(t == 2) + add(Not(x), y); + else + add(Not(x), Not(y)); + } +} + +void dfs(int nod) +{ + viz[nod] = true; + for(auto it : graf[nod]) + if(!viz[it]) + dfs(it); + st.push(nod); +} + +void dfs_t(int nod) +{ + c[nod] = comp; + for(auto it : graf_t[nod]) + if(!c[it]) + dfs_t(it); +} + +void Solve() +{ + for(int i = 1;i <= 2 * n;++i) + if(!viz[i]) + dfs(i); + while(!st.empty()) + { + int node = st.top(); st.pop(); + + if(!c[node]) + { + ++comp; + dfs_t(node); + } + } + for(int i = 1;i <= n;++i) + if(c[i] > c[Not(i)]) + ++rez_nr, rez[i] = true; + g< +using namespace std; + + + +int n, m, a, b, c, F = 101, T = 102, viz[202], uz[202]; + +vector v[2 * 202], v2[2 * 202], H, ans; + + + +void dfs(int nod){ + + uz[nod] = 1; + + for (auto it : v[nod]) if (!uz[it]) dfs(it); + + H.push_back(nod); + +} + + + +void dfs2(int nod){ + + + + uz[nod] = 0; viz[nod ^ 1] = 1; + + if (nod&1) ans.push_back(nod/2 + 1); + + for (auto it : v2[nod]) if (uz[it]) dfs2(it); + +} + + + +void add(int a, int b){ + + a = (a < 0 ? -a * 2 - 1 : a * 2 - 2); + + b = (b < 0 ? -b * 2 - 1 : b * 2 - 2); + + + + + + v[a^1].push_back(b); + + v[b^1].push_back(a); + + v2[a].push_back(b^1); + + v2[b].push_back(a^1); + +} + + + +int main(){ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + cin >> n >> m; + + + + + + for (int i=1;i<=m;i++){ + + cin >> a >> b >> c; + + if (c == 0){ + + add(a, b); + + } + + else if (c == 1){ + + add(a, -b); + + } + + else if (c == 2){ + + add(-a, b); + + } + + else{ + + add(-a, -b); + + } + + } + + + + for (int i=0;i<2*n;i++) if (!uz[i]) dfs(i); + + reverse(H.begin(), H.end()); + + for (auto it : H){ + + + + if (uz[it] && uz[it^1]) dfs2(it); + + } + + + + cout << ans.size() << '\n'; + + for (auto it : ans) + + cout << it << '\n'; + + + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/32ef00a1-0b56-11ec-a25f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/32ef00a1-0b56-11ec-a25f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b984831a459df210e6cb35dec89173ab01052eb --- /dev/null +++ b/party/2sat_implication_reduction/32ef00a1-0b56-11ec-a25f-2cf05d84bfc2.cpp @@ -0,0 +1,230 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = 200; + + + +int N, M; + +vector g[NMAX + 5]; + +vector t[NMAX + 5]; + + + +bool seen[NMAX + 5]; + +stack st; + +void dfs1(int node) + +{ + + seen[node] = true; + + + + for(auto it : g[node]) + + if(!seen[it]) dfs1(it); + + + + st.push(node); + +} + + + +int kComp; + +int comp[NMAX + 5]; + +void dfs2(int node) + +{ + + comp[node] = kComp; + + + + for(auto it : t[node]) + + if(comp[it] == 0) dfs2(it); + +} + + + +vector sol; + + + +int Not(int x) + +{ + + return x; + +} + + + +int Nrm(int x) + +{ + + return x + N; + +} + + + +int main() + +{ + + cin >> N >> M; + + + + for(int i = 1; i <= M; i++) + + { + + int x, y, z; + + cin >> x >> y >> z; + + + + if(z == 0) + + { + + g[Not(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Nrm(x)); + + + + t[Nrm(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Not(y)); + + } + + else if(z == 1) + + { + + g[Not(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Nrm(x)); + + + + t[Not(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Nrm(y)); + + } + + else if(z == 2) + + { + + g[Nrm(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Not(x)); + + + + t[Nrm(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Not(y)); + + } + + else + + { + + g[Nrm(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Not(x)); + + + + t[Not(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Nrm(y)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) + + if(!seen[i]) dfs1(i); + + + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(comp[node] == 0) + + { + + ++kComp; + + dfs2(node); + + } + + } + + + + for(int i = 1; i <= N; i++) + + if(comp[i + N] > comp[i]) + + sol.push_back(i); + + + + cout << sol.size() << '\n'; + + for(auto it : sol) + + cout << it << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/32ef27b1-0b56-11ec-b0d0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/32ef27b1-0b56-11ec-b0d0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b751a9fbf0eb0850f07aa858fe5ba43803e5a40 --- /dev/null +++ b/party/2sat_implication_reduction/32ef27b1-0b56-11ec-b0d0-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n, m, x, y, t, comp, rez_nr; + +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; + +int c[2 * 100 + 5]; + +stack st; + +bool viz[2 * 100 + 5], rez[100 + 5]; + + + +int Not(int x) + +{ + + if(x < n) + + return x + n; + + return x - n; + +} + + + +void add(int x, int y) + +{ + + graf[Not(x)].push_back(y); + + graf[Not(y)].push_back(x); + + graf_t[y].push_back(Not(x)); + + graf_t[x].push_back(Not(y)); + +} + + + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= m;++i) + + { + + f>>x>>y>>t; + + if(t == 0) + + add(x, y); + + else if(t == 1) + + add(x, Not(y)); + + else if(t == 2) + + add(Not(x), y); + + else + + add(Not(x), Not(y)); + + } + +} + + + +void dfs(int nod) + +{ + + viz[nod] = true; + + for(auto it : graf[nod]) + + if(!viz[it]) + + dfs(it); + + st.push(nod); + +} + + + +void dfs_t(int nod) + +{ + + c[nod] = comp; + + for(auto it : graf_t[nod]) + + if(!c[it]) + + dfs_t(it); + +} + + + +void Solve() + +{ + + for(int i = 1;i <= 2 * n;++i) + + if(!viz[i]) + + dfs(i); + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(!c[node]) + + { + + ++comp; + + dfs_t(node); + + } + + } + + for(int i = 1;i <= n;++i) + + if(c[i] > c[Not(i)]) + + ++rez_nr, rez[i] = true; + + g< +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void do_dfspl (int cur_node, vector < vector < int > > &gr, + vector < bool > &fol, stack < int > &stk) { + + fol[cur_node] = true; + for (int x : gr[cur_node]) { + if (fol[x]) { + continue; + } + + do_dfspl (x, gr, fol, stk); + } + + stk.push (cur_node); +} + +void do_dfsmin (int cur_node, vector < vector < int > > &gr, + vector < bool > &fol, vector < int > &ctc, int &cur_ctc) { + + fol[cur_node] = true; + ctc[cur_node] = cur_ctc; + for (int x : gr[cur_node]) { + if (fol[x]) { + continue; + } + + do_dfsmin (x, gr, fol, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > fol (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (fol[i]) { + continue; + } + + do_dfspl (i, gr, fol, stk); + } + + fill (fol.begin (), fol.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (fol[cur]) { + continue; + } + + ++ nr_of_ctc; + do_dfsmin (cur, inv, fol, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/380907b9-0b55-11ec-a62a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/380907b9-0b55-11ec-a62a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c03e675a674d96da31c38d103905d1c7bb65cb0 --- /dev/null +++ b/party/2sat_implication_reduction/380907b9-0b55-11ec-a62a-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,x,y,tip,cur=1,viz[203]; + +vector Sol,O,G[203],Q[203]; + +void add(int x,int y) + +{ + + G[x].push_back(y); + + Q[y].push_back(x); + +} + +void dfs(int x) + +{ + + viz[x]=1; + + for(int i=0;i<(int)G[x].size();++i) + + if(!viz[G[x][i]]) dfs(G[x][i]); + + O.push_back(x); + +} + +void DFS(int x) + +{ + + viz[x]=cur; + + for(int i=0;i<(int)Q[x].size();++i) + + if(viz[Q[x][i]]==1) DFS(Q[x][i]); + +} + +int main() + +{ + + f>>n>>m; + + while(m--) + + { + + f>>x>>y>>tip; + + if(!tip) add(x+n,y),add(y+n,x); + + else if(tip==1) add(x+n,y+n),add(y,x); + + else if(tip==2) add(y+n,x+n),add(x,y); + + else add(x,y+n),add(y,x+n); + + } + + for(int i=1;i<=2*n;++i) + + if(!viz[i]) dfs(i); + + for(int i=O.size()-1;i>=0;--i) + + if(viz[O[i]]==1) + + { + + cur++; + + DFS(O[i]); + + } + + for(int i=1;i<=n;++i) + + if(viz[i]>viz[i+n]) Sol.push_back(i); + + g< +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +using namespace std; + +typedef long long LL; + +typedef pair pii; +const int MAXN = 110; + +template class jmenvect : public vector { +public: + T& operator[](int n) { + if (n > 0) + return this->at(n); + else + return this->at(-n + (this->size() >> 1)); + } +}; + +class graph { +private: + vector< list > adiac; + vector< list > neg_adiac; +public: + graph(); + graph(int); + int size(); + void newnod(); + void newedge(int, int); + const list& getList(int); + void rev_DFS(int, jmenvect&, jmenvect&); + bool SCC_DFS(int, int, jmenvect&); +}; + +graph::graph() { + adiac.reserve(MAXN); + neg_adiac.reserve(MAXN); +} + +graph::graph(int n) { + adiac.resize(n + 1); + neg_adiac.resize(n + 1); +} + +int graph::size() { + return adiac.size(); +} + +void graph::newnod() { + neg_adiac.resize(size() + 1); + adiac.resize(size() + 1); +} + +void graph::newedge(int n1, int n2) { + if (n1 > 0) + adiac[n1].push_back(n2); + else + neg_adiac[-n1].push_back(n2); +} + +const list& graph::getList(int n) { + if (n > 0) + return adiac[n]; + else + return neg_adiac[-n]; +} + +void graph::rev_DFS(int nod, jmenvect& visited, jmenvect &finished) { + auto L = this->getList(-nod); + visited[nod] = 1; + for (auto i = L.begin(); i != L.end(); ++i) + if (!visited[-(*i)]) + rev_DFS(-(*i), visited, finished); + finished.push_back(nod); +} + +bool graph::SCC_DFS(int curComp, int nod, jmenvect& visited) { + visited[nod] = curComp; + auto L = this->getList(nod); + for (auto i = L.begin(); i != L.end(); ++i) { + if (!visited[*i]) { + if (!SCC_DFS(curComp, *i, visited)) + return false; + } + } + if (visited[-nod]) + return false; + visited[-nod] = -curComp; + return true; +} + +pair, int> korasaju(graph* G) { + int N = G->size() - 1; + jmenvect visisted; + visisted.resize((N << 1) + 1); + jmenvect finished; + finished.reserve((N << 1) + 1); + + for (int i = 1; i <= N; i++) { + if (!visisted[i]) + G->rev_DFS(i, visisted, finished); + if (!visisted[-i]) + G->rev_DFS(-i, visisted, finished); + } + + visisted.assign((N << 1) + 1, 0); + int curComp = 0; + for (auto i = finished.rbegin(); i != finished.rend() - 1; ++i) { + curComp++; + if (!visisted[*i]) { + if (!G->SCC_DFS(curComp, *i, visisted)) { + jmenvect dummyvect; + return{ dummyvect, 0 }; + } + } + else + curComp--; + } + return{ visisted, curComp }; +} + + + +int main() { + + assert(freopen("fis.in", "r", stdin)); + + assert(freopen("fis.out", "w", stdout)); + int N, M; + scanf("%d%d", &N, &M); + graph* G = new graph(N); + for (int i = 0; i < M; i++) { + int tip, a, b; + scanf("%d%d%d", &a, &b, &tip); + switch (tip) { + case 0: + break; + case 1: + b = -b; + break; + case 2: + a = -a; + break; + default: + a = -a; + b = -b; + break; + } + G->newedge(-a, b); + G->newedge(-b, a); + } + pair, int> p = korasaju(G); + jmenvect& SCC = p.first; + if (!SCC.size()) { + printf("-1\n"); + return 0; + } + int nrComp = p.second; + jmenvect cval; + cval.resize((nrComp << 1) + 1); + for (int i = 1; i <= nrComp; i++) { + cval[-i] = 0; + cval[i] = 1; + } + vector sol; + for (int i = 1; i <= N; i++) { + if (cval[SCC[i]] == 1) + sol.push_back(i); + } + printf("%d\n", (int)sol.size()); + + for (const auto &it : sol) + + printf("%d\n", it); + + return 0; + +} diff --git a/party/2sat_implication_reduction/401e1f88-0b55-11ec-89f2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/401e1f88-0b55-11ec-89f2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0259d2689f58bffdac4b64c0de19aace0c7a1414 --- /dev/null +++ b/party/2sat_implication_reduction/401e1f88-0b55-11ec-89f2-2cf05d84bfc2.cpp @@ -0,0 +1,146 @@ +#include + + + + +using namespace std; + + + +ifstream fin("party.in") ; + +ofstream fout("party.out") ; + + + +vector graf[205] ; + +vector graft[205] ; + +vector ord , sol ; + +bitset<205> bt ; + +int n , m , rng[205] , cmp ; + + + +void add(int x , int y) + +{ + + graf[x].push_back(y) ; + + graft[y].push_back(x) ; + +} + + + +void citire() + +{ + + int x , y , tp , i ; + + fin >> n >> m ; + + for ( i = 1 ; i <= m ; i++ ) + + { + + fin >> x >> y >> tp ; + + if ( tp == 0 ) + + add(n+x,y) , add(n+y,x) ; + + else if ( tp == 1 ) + + add(n+x,n+y) , add(y,x) ; + + else if ( tp == 2 ) + + add(x,y) , add(n+y,n+x) ; + + else + + add(x,n+y) , add(y,n+x) ; + + } + +} + + + +void dfs(int nod) + +{ + + bt[nod] = 1 ; + + for ( auto vec : graf[nod] ) + + if ( bt[vec] == 0 ) + + dfs(vec) ; + + ord.push_back(nod); + +} + + + +void dfst(int nod) + +{ + + rng[nod] = cmp ; + + for ( auto vec : graft[nod] ) + + if ( rng[vec] == 0 ) + + dfst(vec) ; + +} + + + +int main() + +{ + + int i ; + + citire() ; + + for ( i = 1 ; i <= 2*n ; i++ ) + + if ( bt[i] == 0 ) + + dfs(i) ; + + reverse(ord.begin(),ord.end()) ; + + cmp = 1 ; + + for ( auto vv : ord ) + + if ( rng[vv] == 0 ) + + dfst(vv) , cmp++ ; + + for ( i = 1 ; i <= n ; i++ ) + + if ( rng[i] > rng[n+i] ) + + sol.push_back(i) ; + + fout << sol.size() << '\n' ; + + for ( auto vv : sol ) + + fout << vv << '\n' ; + +} diff --git a/party/2sat_implication_reduction/4150af53-0b53-11ec-ad32-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4150af53-0b53-11ec-ad32-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a2fe72dc92dba322b038791fe6011ef6050d51e --- /dev/null +++ b/party/2sat_implication_reduction/4150af53-0b53-11ec-ad32-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +#include +#include + + +using namespace std; + + + +int n,nc,x,y,i,j,k,nr; + +int f[201],ctc[201],s[201],t[201]; + +vector v[201],inv[201]; + + + +inline int nt(int x){return (x+n)%(2*n);} + +inline void add(int x,int y){v[nt(x)].push_back(y);v[nt(y)].push_back(x);inv[x].push_back(nt(y));inv[y].push_back(nt(x));} + + + +void unudfs(int x) + +{ + + if(f[x]) + + return; + + f[x]=1; + + for(vector ::iterator i=v[x].begin();i!=v[x].end();++i) + + unudfs(*i); + + s[++s[0]]=x; + +} + + + +void doidfs(int x,int y) + +{ + + if(f[x]) + + return; + + f[x]=1; + + ctc[x]=y; + + for(int i=0;i +#include +#include + + +const int MAX_N = 1000; + + + +int n; + +std::vector g[1 + 2 * MAX_N]; + +std::vector trans[1 + 2 * MAX_N]; + +std::vector topo; + +std::vector sol; + +bool viz[1 + 2 * MAX_N]; + +bool val[1 + 2 * MAX_N]; + +bool possible = true; + + + +int conj(int x) { + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void make_edge(int u, int v) { + + g[u].push_back(v); + + trans[v].push_back(u); + +} + + + +void topoSort(int u) { + + viz[u] = true; + + for (auto v : g[u]) { + + if (!viz[v]) + + topoSort(v); + + } + + topo.push_back(u); + +} + + + +void markNodes(int u) { + + viz[u] = true; + + if (val[u]) { + + possible = false; + + return; + + } + + val[u] = false; + + val[conj(u)] = true; + + for (auto v : trans[u]) { + + if (!viz[v]) + + markNodes(v); + + } + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + int m; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; i++) { + + int x, y, restriction; + + scanf("%d%d%d", &x, &y, &restriction); + + switch (restriction) { + + case 0: + + make_edge(conj(x), y); + + make_edge(conj(y), x); + + break; + + case 1: + + make_edge(conj(x), conj(y)); + + make_edge(y, x); + + break; + + case 2: + + make_edge(conj(y), conj(x)); + + make_edge(x, y); + + break; + + case 3: + + make_edge(x, conj(y)); + + make_edge(y, conj(x)); + + break; + + } + + } + + for (int i = 1; i <= 2 * n; i++) { + + if (!viz[i]) + + topoSort(i); + + } + + std::reverse(topo.begin(), topo.end()); + + for (int i = 1; i <= 2 * n; i++) + + viz[i] = false; + + for (auto node : topo) { + + if (!viz[node] && !viz[conj(node)]) + + markNodes(node); + + if (!possible) { + + printf("-1\n"); + + return 0; + + } + + } + + for (int i = 1; i <= n; i++) { + + if (val[i]) + + sol.push_back(i); + + } + + printf("%d\n", sol.size()); + + for (auto node : sol) + + printf("%d\n", node); + + printf("\n"); + + return 0; + +} diff --git a/party/2sat_implication_reduction/426b9fd4-1c2a-4ba5-b777-6180dfd70261.cpp b/party/2sat_implication_reduction/426b9fd4-1c2a-4ba5-b777-6180dfd70261.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cea448c6dc660d3ad6b09afa1daecbdf5a67dae1 --- /dev/null +++ b/party/2sat_implication_reduction/426b9fd4-1c2a-4ba5-b777-6180dfd70261.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + + + +using namespace std; + +FILE*f=fopen("party.in","r"); +FILE*g=fopen("party.out","w"); + +int n,m,index; +int viz[105<<1],lowest[105<<1],niv[105<<1],comp[105<<1],last[105<<1],gr_in[105<<1],sol[105<<1]; +vectorG[105<<1],W[105<<1],v; +stackst; int inst[105<<1]; +vector< vector >C; +queueQ; + +inline int NOT ( const int &x ){ + + if ( x <= n ){ + return n+x; + } + return x-n; +} + +inline void componenta ( const int &x ){ + int nod; v.clear(); + + do{ + nod = st.top(); v.push_back(nod); comp[nod] = C.size()+1; + st.pop(); inst[nod] = 0; + }while(x != nod); + + C.push_back(v); +} + +void DFS ( int nod ){ + viz[nod] = 1; niv[nod] = lowest[nod] = ++index; + st.push(nod); inst[nod] = 1; + + for ( vector::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){ + int nodvcn = (*itt); + + if ( !viz[nodvcn] ){ + DFS(nodvcn); + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + else{ + if ( inst[nodvcn] ){ + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + } + } + + if ( lowest[nod] == niv[nod] ){ + componenta(nod); + } +} + +inline void CTC () { + + for ( int i = 1 ; i <= (n<<1) ; ++i ){ + if ( !viz[i] ){ + DFS(i); + } + } +} + +inline void solve () { + + int Ncomp = C.size(); + for ( int i = 0 ; i < Ncomp ; ++i ){ + for ( vector::iterator itt = C[i].begin() ; itt != C[i].end() ; ++itt ){ + int x = (*itt); + + for ( vector::iterator vecin = G[x].begin() ; vecin != G[x].end() ; ++vecin ){ + int compvcn = comp[*vecin]; + if ( last[compvcn] != i+1 && compvcn != i+1 ){ + W[i+1].push_back(compvcn); ++gr_in[compvcn]; + last[compvcn] = i+1; + } + } + } + } + + for ( int i = 1 ; i <= Ncomp ; ++i ){ + if ( !gr_in[i] ){ + Q.push(i); + } + } + + int nod; + for ( ; !Q.empty() ; ){ + nod = Q.front(); Q.pop(); + + for ( vector::iterator itt = W[nod].begin() ; itt != W[nod].end() ; ++itt ){ + int nodvcn = (*itt); + --gr_in[nodvcn]; + if ( !gr_in[nodvcn] ) + Q.push(nodvcn); + } + if ( last[nod] == -1 ) continue ; + + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 0; + } + nod = comp[NOT(C[nod-1][0])]; last[nod] = -1; + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 1; + } + } + + int Nsol = 0; + for ( int i = 1 ; i <= n ; ++i ){ + Nsol += sol[i]; + } + fprintf(g,"%d\n",Nsol); + for ( int i = 1 ; i <= n ; ++i ){ + if ( sol[i] ){ + fprintf(g,"%d\n",i); + } + } +} + +int main () { + + fscanf(f,"%d %d",&n,&m); + int a,b,tip; + for ( int i = 1 ; i <= m ; ++i ){ + fscanf(f,"%d %d %d",&a,&b,&tip); + + if ( tip == 1 ){ + b = -b; + } + else{ + if ( tip == 2 ){ + a = -a; + } + else{ + if ( tip == 3 ){ + a = -a; b = -b; + } + } + } + + if ( a < 0 ) a = -a+n; + if ( b < 0 ) b = -b+n; + G[NOT(a)].push_back(b); + G[NOT(b)].push_back(a); + } + + CTC(); + solve(); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/party/2sat_implication_reduction/43a6a27f-0b55-11ec-8f82-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/43a6a27f-0b55-11ec-8f82-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..152cb62a9adde9975d7e6728078d7f4a02a14da8 --- /dev/null +++ b/party/2sat_implication_reduction/43a6a27f-0b55-11ec-8f82-2cf05d84bfc2.cpp @@ -0,0 +1,258 @@ +#include + + +using namespace std; + + + + + + + +int N, M; + +vector edg[205], redg[205]; + +int O, ord[205]; + +int f[205]; + +int C, cmp[205]; + +vector scc[205]; + +vector sccedg[205]; + +int g[205]; + +int ans[205], v[205]; + +vector party; + + + +inline int node(int x) { return x > 0 ? x : -x + N; } + +inline int value(int x) { return x <= N ? x : -x + N; } + + + +void DFS1(int nod) + +{ + + if(f[nod]) return; + + f[nod] = 1; + + for(auto nxt: edg[nod]) + + DFS1(nxt); + + ord[++O] = nod; + +} + + + +void DFS2(int nod) + +{ + + if(f[nod] == 2) return; + + f[nod] = 2; + + cmp[nod] = C; + + scc[C].push_back(nod); + + for(auto nxt: redg[nod]) + + DFS2(nxt); + +} + + + +void solveDFS(int nod, int fth) + +{ + + if(ans[nod] == -1) + + { + + ans[nod] = fth; + + for(auto x: scc[nod]) + + { + + int rev = node( -value(x) ); + + ans[ cmp[rev] ] = 1 ^ fth; + + } + + } + + assert(!fth || (fth == ans[nod])); + + + + for(auto nxt: sccedg[nod]) + + { + + g[nxt]--; + + if(g[nxt] == 0) + + solveDFS(nxt, fth | ans[nod]); + + } + +} + + + +int main() + +{ + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + + + scanf("%d%d", &N, &M); + + for(int i = 1; i <= M; i++) + + { + + int x, y, op; + + scanf("%d%d%d", &x, &y, &op); + + if(op == 0) + + { + + edg[node(-x)].push_back(y); + + edg[node(-y)].push_back(x); + + } + + else if(op == 1) + + { + + edg[node(-x)].push_back(node(-y)); + + edg[node(y)].push_back(node(x)); + + } + + else if(op == 2) + + { + + edg[node(x)].push_back(node(y)); + + edg[node(-y)].push_back(node(-x)); + + } + + else if(op == 3) + + { + + edg[node(x)].push_back(node(-y)); + + edg[node(y)].push_back(node(-x)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) DFS1(i); + + for(int i = 1; i <= 2 * N; i++) + + for(auto x: edg[i]) + + redg[x].push_back(i); + + reverse(ord + 1, ord + 2 * N + 1); + + for(int i = 1; i <= 2 * N; i++) + + if( f[ ord[i] ] != 2 ) ++C, DFS2(ord[i]); + + + + for(int i = 1; i <= N; i++) assert(cmp[i] != cmp[i + N]); + + + + for(int i = 1; i <= C; i++) f[i] = 0; + + for(int i = 1; i <= C; i++) + + for(auto nod: scc[i]) + + for(auto nxt: edg[nod]) + + if(i != cmp[nxt]) + + if(f[ cmp[nxt] ] != i) + + sccedg[i].push_back(cmp[nxt]), g[ cmp[nxt] ]++, f[ cmp[nxt] ] = i; + + + + for(int i = 1; i <= C; i++) ans[i] = -1; + + for(int i = 1; i <= C; i++) + + if(ans[i] == -1) solveDFS(i, 0); + + + + for(int i = 1; i <= C; i++) + + for(auto nod: scc[i]) + + v[nod] = ans[i]; + + + + for(int i = 1; i <= N; i++) + + if(v[i]) + + party.push_back(i); + + + + printf("%d\n", party.size()); + + for(auto x: party) + + printf("%d\n", x); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/484c1101-e52c-4cdb-a2c9-ce602b77d9bc.cpp b/party/2sat_implication_reduction/484c1101-e52c-4cdb-a2c9-ce602b77d9bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779abbe2a478041abf51b4dcbdcdb67c356f4aac --- /dev/null +++ b/party/2sat_implication_reduction/484c1101-e52c-4cdb-a2c9-ce602b77d9bc.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + +vector>lv,lv2; + +vectornoduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + +int neg(int x) { + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + +void sorttop(int nod) { + + int i; + + v[nod]=1; + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/4b852038-02b2-11ec-9cb2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4b852038-02b2-11ec-9cb2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4adbfbf0f1168f7f94c96a6c29a9e9a096183c6d --- /dev/null +++ b/party/2sat_implication_reduction/4b852038-02b2-11ec-9cb2-2cf05d84bfc2.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const int mx=1000; + +ifstream in("party.in"); +ofstream out("party.out"); + +vector g[mx],gt[mx],q; +bool visited[mx],possible=true; +int n,m,result[mx],cnt[mx]; + +int node(int x){ + if(x<0){ + return n+abs(x); + } + return x; +} + +int val(int node){ + if(node>n){ + return n-node; + } + return node; +} + + +void read(){ + in>>n>>m; + int a,b,c; + for(int i=0;i>a>>b>>c; + if(c==3){ + a=-a; + b=-b; + } + else if(c==1){ + b=-b; + } + else if(c==2){ + a=-a; + } + g[node(-a)].push_back(node(b)); + gt[node(b)].push_back(node(-a)); + g[node(-b)].push_back(node(a)); + gt[node(a)].push_back(node(-b)); + } +} + +void dfs1(int x){ + visited[x]=true; + for(int k:g[x]){ + if(!visited[k]) + dfs1(k); + } + q.push_back(x); +} + +void dfs2(int x,int value,int comp){ + cnt[x]=comp; + visited[x]=true; + result[x]=value; + result[node(-val(x))]=1-value; + + for(int k:gt[x]){ + if(!visited[k]){ + dfs2(k,value,comp); + } + } +} +void preprocess(){ + for(int i=1;i<=2*n;i++) + result[i]=-1; +} + +void solve(){ + + for(int i=1;i<=2*n;i++){ + if(!visited[i]){ + dfs1(i); + } + } + for(int i=1;i ans; + + for(int i=1;i<=n;i++) + if(result[i]) + ans.push_back(i); + + out< + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/4bc45159-02b2-11ec-9acc-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4bc45159-02b2-11ec-9acc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98bc936345445d61fd9f1bffa1f06bd9010efed1 --- /dev/null +++ b/party/2sat_implication_reduction/4bc45159-02b2-11ec-9acc-2cf05d84bfc2.cpp @@ -0,0 +1,172 @@ +#include + + +using namespace std; + + + +const int NMAX = 100; + +int N, M; + +vector > graph, revgraph; + +bitset <2 * NMAX> seen; + + + +int neg(int x){ + + if (x < N){ + + return x + N; + + } + + else{ + + return x - N; + + } + +} + + + +void AddEdge(int x, int y){ + + int negx = neg(x); + + int negy = neg(y); + + graph[negx].push_back(y); + + graph[negy].push_back(x); + + revgraph[y].push_back(negx); + + revgraph[x].push_back(negy); + +} + + + +vector topo; + +vector color; + + + +void dfs1(int node){ + + seen[node] = 1; + + for (auto& x : graph[node]){ + + if (seen[x] == 0){ + + dfs1(x); + + } + + } + + topo.push_back(node); + +} + + + +void dfs2(int node){ + + seen[node] = seen[neg(node)] = 0; + + color[neg(node)] = true; + + for (auto& x : revgraph[node]) + + if (seen[x] == 1) + + dfs2(x); + +} + + + +int main() + +{ + + ifstream fin("party.in"); + + ofstream fout("party.out"); + + fin >> N >> M; + + graph = vector >(2 * N, vector ()); + + revgraph = vector >(2 * N, vector ()); + + color = vector (2 * N, false); + + for (int i = 0;i < M;++i){ + + int t, x, y; + + fin >> x >> y >> t; + + --x; --y; + + if (t == 0) + + AddEdge(x, y); + + else if (t == 1) + + AddEdge(x, neg(y)); + + else if (t == 2) + + AddEdge(neg(x), y); + + else + + AddEdge(neg(x), neg(y)); + + } + + for (int i = 0;i < 2 * N;++i) + + if (seen[i] == 0) + + dfs1(i); + + reverse(topo.begin(), topo.end()); + + for (auto& i : topo) + + if (seen[i] == 1 && seen[neg(i)] == 1) + + dfs2(i); + + vector answer; + + for (int i = 0;i < N;++i) + + if (color[i] == true) + + answer.push_back(i); + + fout << answer.size() << "\n"; + + for (auto& i : answer) + + fout << i + 1 << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/4be2c92c-02b2-11ec-b089-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4be2c92c-02b2-11ec-b089-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b4e8f1dd5770d60038dd44fb734bac6547d34ac --- /dev/null +++ b/party/2sat_implication_reduction/4be2c92c-02b2-11ec-b089-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N=200000+7; + +int n,m,top,o[N],tip[N],cur; + +bool one[N]; + +bool vis[N]; + +vector g[N],ginv[N]; + + + +void dfs1(int a) { + + vis[a]=1; + + for (auto &b:g[a]) { + + if (vis[b]==0) { + + dfs1(b); + + } + + } + + o[++top]=a; + +} + + + +void dfs2(int a) { + + vis[a]=1; + + tip[a]=cur; + + for (auto &b:ginv[a]) { + + if (vis[b]==0) { + + dfs2(b); + + } + + } + +} + + + +signed main() { + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for (int i=1;i<=m;i++) { + + int x,y,t; + + scanf("%d%d%d",&x,&y,&t); + + if (t==0) { + + g[x+n].push_back(y); + + g[y+n].push_back(x); + + } + + if (t==1) { + + g[x+n].push_back(y+n); + + g[y].push_back(x); + + } + + if (t==2) { + + g[x].push_back(y); + + g[y+n].push_back(x+n); + + } + + if (t==3) { + + g[x].push_back(y+n); + + g[y].push_back(x+n); + + } + + + + } + + + + for (int i=1;i<=2*n;i++) { + + for (auto &j : g[i]) { + + ginv[j].push_back(i); + + } + + } + + + + for (int i=1;i<=2*n;i++) { + + if (vis[i]==0) { + + dfs1(i); + + } + + } + + for (int i=1;i<=2*n;i++) { + + vis[i]=0; + + } + + for (int i=2*n;i>=1;i--) { + + int node=o[i]; + + if (vis[node]==0) { + + cur++; + + dfs2(node); + + } + + } + + int cnt=0; + + for (int i=1;i<=n;i++) { + + cnt+=(tip[i]>tip[i+n]); + + } + + printf("%d\n",cnt); + + for (int i=1;i<=n;i++) { + + if (tip[i]>tip[i+n]) { + + printf("%d\n",i); + + } + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/4c021101-02b2-11ec-8486-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4c021101-02b2-11ec-8486-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b4e8f1dd5770d60038dd44fb734bac6547d34ac --- /dev/null +++ b/party/2sat_implication_reduction/4c021101-02b2-11ec-8486-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N=200000+7; + +int n,m,top,o[N],tip[N],cur; + +bool one[N]; + +bool vis[N]; + +vector g[N],ginv[N]; + + + +void dfs1(int a) { + + vis[a]=1; + + for (auto &b:g[a]) { + + if (vis[b]==0) { + + dfs1(b); + + } + + } + + o[++top]=a; + +} + + + +void dfs2(int a) { + + vis[a]=1; + + tip[a]=cur; + + for (auto &b:ginv[a]) { + + if (vis[b]==0) { + + dfs2(b); + + } + + } + +} + + + +signed main() { + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for (int i=1;i<=m;i++) { + + int x,y,t; + + scanf("%d%d%d",&x,&y,&t); + + if (t==0) { + + g[x+n].push_back(y); + + g[y+n].push_back(x); + + } + + if (t==1) { + + g[x+n].push_back(y+n); + + g[y].push_back(x); + + } + + if (t==2) { + + g[x].push_back(y); + + g[y+n].push_back(x+n); + + } + + if (t==3) { + + g[x].push_back(y+n); + + g[y].push_back(x+n); + + } + + + + } + + + + for (int i=1;i<=2*n;i++) { + + for (auto &j : g[i]) { + + ginv[j].push_back(i); + + } + + } + + + + for (int i=1;i<=2*n;i++) { + + if (vis[i]==0) { + + dfs1(i); + + } + + } + + for (int i=1;i<=2*n;i++) { + + vis[i]=0; + + } + + for (int i=2*n;i>=1;i--) { + + int node=o[i]; + + if (vis[node]==0) { + + cur++; + + dfs2(node); + + } + + } + + int cnt=0; + + for (int i=1;i<=n;i++) { + + cnt+=(tip[i]>tip[i+n]); + + } + + printf("%d\n",cnt); + + for (int i=1;i<=n;i++) { + + if (tip[i]>tip[i+n]) { + + printf("%d\n",i); + + } + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/4c2131bb-02b2-11ec-bdbf-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4c2131bb-02b2-11ec-bdbf-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2241576717ce49777437e0aa65dc2250fd4f5359 --- /dev/null +++ b/party/2sat_implication_reduction/4c2131bb-02b2-11ec-bdbf-2cf05d84bfc2.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< +#include +#include +#include + + +using namespace std; + + + +const int N=200000+7; + +int n,m,top,o[N],tip[N],cur; + +bool one[N]; + +bool vis[N]; + +vector g[N],ginv[N]; + + + +void dfs1(int a) { + + vis[a]=1; + + for (auto &b:g[a]) { + + if (vis[b]==0) { + + dfs1(b); + + } + + } + + o[++top]=a; + +} + + + +void dfs2(int a) { + + vis[a]=1; + + tip[a]=cur; + + for (auto &b:ginv[a]) { + + if (vis[b]==0) { + + dfs2(b); + + } + + } + +} + + + +signed main() { + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for (int i=1;i<=m;i++) { + + int x,y,t; + + scanf("%d%d%d",&x,&y,&t); + + if (t==0) { + + g[x+n].push_back(y); + + g[y+n].push_back(x); + + } + + if (t==1) { + + g[x+n].push_back(y+n); + + g[y].push_back(x); + + } + + if (t==2) { + + g[x].push_back(y); + + g[y+n].push_back(x+n); + + } + + if (t==3) { + + g[x].push_back(y+n); + + g[y].push_back(x+n); + + } + + + + } + + + + for (int i=1;i<=2*n;i++) { + + for (auto &j : g[i]) { + + ginv[j].push_back(i); + + } + + } + + + + for (int i=1;i<=2*n;i++) { + + if (vis[i]==0) { + + dfs1(i); + + } + + } + + for (int i=1;i<=2*n;i++) { + + vis[i]=0; + + } + + for (int i=2*n;i>=1;i--) { + + int node=o[i]; + + if (vis[node]==0) { + + cur++; + + dfs2(node); + + } + + } + + int cnt=0; + + for (int i=1;i<=n;i++) { + + cnt+=(tip[i]>tip[i+n]); + + } + + printf("%d\n",cnt); + + for (int i=1;i<=n;i++) { + + if (tip[i]>tip[i+n]) { + + printf("%d\n",i); + + } + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/4c399647-0b56-11ec-bb56-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4c399647-0b56-11ec-bb56-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78dfe2c5ea0680d7179048e706f2d4452489efdb --- /dev/null +++ b/party/2sat_implication_reduction/4c399647-0b56-11ec-bb56-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +vectora[202],inv[202]; + +int n,k,kk[202],ctc[202],sol[102]; + +bool viz[202]; + + + +int neg(int x) + +{ + + if(x>n) + + return x-n; + + return x+n; + +} + + + +void dfs(int x) + +{ + + viz[x]=1; + + for(auto y:a[x]) + + if(viz[y]==0) + + dfs(y); + + kk[++k]=x; + +} + + + +void dfs_inv(int x) + +{ + + viz[x]=1; + + ctc[x]=k; + + for(auto y:inv[x]) + + if(viz[y]==0) + + dfs_inv(y); + +} + + + +void kosaraju() + +{ + + int i; + + for(i=1;i<=2*n;i++) + + if(viz[i]==0) + + dfs(i); + + for(i=1;i<=2*n;i++) + + viz[i]=0; + + k=0; + + for(i=2*n;i>=1;i--) + + if(viz[kk[i]]==0) + + { + + k++; + + dfs_inv(kk[i]); + + } + +} + + + +int main() + +{ + + int m,i,x,y,p; + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>p; + + if(p==0) + + { + + a[neg(x)].push_back(y); + + a[neg(y)].push_back(x); + + inv[y].push_back(neg(x)); + + inv[x].push_back(neg(y)); + + } + + else + + if(p==1) + + { + + a[neg(x)].push_back(neg(y)); + + a[y].push_back(x); + + inv[neg(y)].push_back(neg(x)); + + inv[x].push_back(y); + + } + + else + + if(p==2) + + { + + a[neg(y)].push_back(neg(x)); + + a[x].push_back(y); + + inv[neg(x)].push_back(neg(y)); + + inv[y].push_back(x); + + } + + else + + if(p==3) + + { + + a[x].push_back(neg(y)); + + a[y].push_back(neg(x)); + + inv[neg(y)].push_back(x); + + inv[neg(x)].push_back(y); + + } + + } + + kosaraju(); + + k=0; + + for(i=1;i<=n;i++) + + if(ctc[i]>ctc[i+n]) + + sol[++k]=i; + + g< +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/4c41d911-02b2-11ec-9bf0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4c41d911-02b2-11ec-9bf0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2241576717ce49777437e0aa65dc2250fd4f5359 --- /dev/null +++ b/party/2sat_implication_reduction/4c41d911-02b2-11ec-9bf0-2cf05d84bfc2.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/4c8137a7-02b2-11ec-8b67-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4c8137a7-02b2-11ec-8b67-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78dfe2c5ea0680d7179048e706f2d4452489efdb --- /dev/null +++ b/party/2sat_implication_reduction/4c8137a7-02b2-11ec-8b67-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +vectora[202],inv[202]; + +int n,k,kk[202],ctc[202],sol[102]; + +bool viz[202]; + + + +int neg(int x) + +{ + + if(x>n) + + return x-n; + + return x+n; + +} + + + +void dfs(int x) + +{ + + viz[x]=1; + + for(auto y:a[x]) + + if(viz[y]==0) + + dfs(y); + + kk[++k]=x; + +} + + + +void dfs_inv(int x) + +{ + + viz[x]=1; + + ctc[x]=k; + + for(auto y:inv[x]) + + if(viz[y]==0) + + dfs_inv(y); + +} + + + +void kosaraju() + +{ + + int i; + + for(i=1;i<=2*n;i++) + + if(viz[i]==0) + + dfs(i); + + for(i=1;i<=2*n;i++) + + viz[i]=0; + + k=0; + + for(i=2*n;i>=1;i--) + + if(viz[kk[i]]==0) + + { + + k++; + + dfs_inv(kk[i]); + + } + +} + + + +int main() + +{ + + int m,i,x,y,p; + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>p; + + if(p==0) + + { + + a[neg(x)].push_back(y); + + a[neg(y)].push_back(x); + + inv[y].push_back(neg(x)); + + inv[x].push_back(neg(y)); + + } + + else + + if(p==1) + + { + + a[neg(x)].push_back(neg(y)); + + a[y].push_back(x); + + inv[neg(y)].push_back(neg(x)); + + inv[x].push_back(y); + + } + + else + + if(p==2) + + { + + a[neg(y)].push_back(neg(x)); + + a[x].push_back(y); + + inv[neg(x)].push_back(neg(y)); + + inv[y].push_back(x); + + } + + else + + if(p==3) + + { + + a[x].push_back(neg(y)); + + a[y].push_back(neg(x)); + + inv[neg(y)].push_back(x); + + inv[neg(x)].push_back(y); + + } + + } + + kosaraju(); + + k=0; + + for(i=1;i<=n;i++) + + if(ctc[i]>ctc[i+n]) + + sol[++k]=i; + + g< +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +vector v[205]; + + + +int n, k; + + + +int llink[205], idx[205], ctc[205], ct; + + + +stack st; + + + +bool inst[205]; + + + +int NEG(int x) + +{ + + return n - x; + +} + + + +void tarjan(int node) + +{ + + llink[node] = idx[node] = ++ k; + + + + inst[node] = true; + + + + st.push(node); + + + + for(int i = 0; i < v[node].size(); i++) + + if(idx[v[node][i]] == 0) + + { + + tarjan(v[node][i]); + + + + llink[node] = min(llink[node], llink[v[node][i]]); + + } + + else + + if(inst[v[node][i]] == true) + + llink[node] = min(llink[node], llink[v[node][i]]); + + + + if(idx[node] == llink[node]) + + { + + int naux = st.top(); + + + + ctc[naux] = ++ct; + + + + inst[naux] = false; + + + + st.pop(); + + + + while(naux != node) + + { + + naux = st.top(); + + + + ctc[naux] = ct; + + + + inst[naux] = false; + + + + st.pop(); + + } + + } + +} + + + +int main() + +{ + + int m; + + + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) + + { + + int x, y, t; + + + + fin >> x >> y >> t; + + + + if(t == 0) + + { + + v[NEG(x)].push_back(y + n); + + v[NEG(y)].push_back(x + n); + + } + + + + if(t == 1) + + { + + v[NEG(x)].push_back(NEG(y)); + + v[y + n].push_back(x + n); + + } + + + + if(t == 2) + + { + + v[x + n].push_back(y + n); + + v[NEG(y)].push_back(NEG(x)); + + } + + + + if(t == 3) + + { + + v[x + n].push_back(NEG(y)); + + v[y + n].push_back(NEG(x)); + + } + + } + + + + for(int i = 0; i <= 2 * n; i++) + + if(idx[i] == 0 && i != n) + + tarjan(i); + + + + int sol[205], kk = 0; + + + + for(int i = n - 1; i >= 0; i--) + + if(ctc[i] < ctc[2 * n - i]) + + continue; + + else + + sol[++kk] = n - i; + + + + fout << kk << '\n'; + + + + for(int i = 1; i <= kk; i++) + + fout << sol[i] << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/4cbfe7cf-02b2-11ec-8626-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4cbfe7cf-02b2-11ec-8626-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b751a9fbf0eb0850f07aa858fe5ba43803e5a40 --- /dev/null +++ b/party/2sat_implication_reduction/4cbfe7cf-02b2-11ec-8626-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n, m, x, y, t, comp, rez_nr; + +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; + +int c[2 * 100 + 5]; + +stack st; + +bool viz[2 * 100 + 5], rez[100 + 5]; + + + +int Not(int x) + +{ + + if(x < n) + + return x + n; + + return x - n; + +} + + + +void add(int x, int y) + +{ + + graf[Not(x)].push_back(y); + + graf[Not(y)].push_back(x); + + graf_t[y].push_back(Not(x)); + + graf_t[x].push_back(Not(y)); + +} + + + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= m;++i) + + { + + f>>x>>y>>t; + + if(t == 0) + + add(x, y); + + else if(t == 1) + + add(x, Not(y)); + + else if(t == 2) + + add(Not(x), y); + + else + + add(Not(x), Not(y)); + + } + +} + + + +void dfs(int nod) + +{ + + viz[nod] = true; + + for(auto it : graf[nod]) + + if(!viz[it]) + + dfs(it); + + st.push(nod); + +} + + + +void dfs_t(int nod) + +{ + + c[nod] = comp; + + for(auto it : graf_t[nod]) + + if(!c[it]) + + dfs_t(it); + +} + + + +void Solve() + +{ + + for(int i = 1;i <= 2 * n;++i) + + if(!viz[i]) + + dfs(i); + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(!c[node]) + + { + + ++comp; + + dfs_t(node); + + } + + } + + for(int i = 1;i <= n;++i) + + if(c[i] > c[Not(i)]) + + ++rez_nr, rez[i] = true; + + g< + + +using namespace std; + + + +ifstream fin ("party.in"); + +ofstream fout ("party.out"); + + + +const int Nmax = 300; + + + +int x, y, tip, n, m, i, nrc, where[Nmax]; + +vector v[Nmax], vt[Nmax], ord, come; + +bool used[Nmax]; + + + +int no(int x) {return x + n;} + + + +void add_edge(int x, int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs1(int node) + +{ + + used[node] = 1; + + for(auto go : v[node]) + + if(!used[go]) dfs1(go); + + ord.push_back(node); + +} + + + +void dfs2(int node) + +{ + + where[node] = nrc; + + for(auto go : vt[node]) + + if(!where[go]) dfs2(go); + +} + + + +int main() + +{ + + fin >> n >> m; + + while(m--) + + { + + fin >> x >> y >> tip; + + if(tip == 0) add_edge(no(x), y), add_edge(no(y), x); + + else if(tip == 1) add_edge(no(x), no(y)), add_edge(y, x); + + else if(tip == 2) add_edge(no(y), no(x)), add_edge(x, y); + + else if(tip == 3) add_edge(x, no(y)), add_edge(y, no(x)); + + } + + + + for(i=1; i<=2*n; ++i) + + if(!used[i]) dfs1(i); + + + + reverse(ord.begin(), ord.end()); + + for(auto node : ord) + + if(!where[node]) + + ++nrc, dfs2(node); + + + + for(i=1; i<=n; ++i) + + if(where[i] > where[no(i)]) come.push_back(i); + + + + fout << come.size() << '\n'; + + for(auto who : come) fout << who << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/4d173d75-02b2-11ec-a7a4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4d173d75-02b2-11ec-a7a4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e64023449f1fba59846143d87b59ec8e8d66d39 --- /dev/null +++ b/party/2sat_implication_reduction/4d173d75-02b2-11ec-a7a4-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; +ifstream f("party.in"); +ofstream g("party.out"); +int n, m, x, y, t, comp, rez_nr; +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; +int c[2 * 100 + 5]; +stack st; +bool viz[2 * 100 + 5], rez[100 + 5]; + +int Not(int x) +{ + if(x < n) + return x + n; + return x - n; +} + +void add(int x, int y) +{ + graf[Not(x)].push_back(y); + graf[Not(y)].push_back(x); + graf_t[y].push_back(Not(x)); + graf_t[x].push_back(Not(y)); +} + +void Read() +{ + f>>n>>m; + for(int i = 1;i <= m;++i) + { + f>>x>>y>>t; + if(t == 0) + add(x, y); + else if(t == 1) + add(x, Not(y)); + else if(t == 2) + add(Not(x), y); + else + add(Not(x), Not(y)); + } +} + +void dfs(int nod) +{ + viz[nod] = true; + for(auto it : graf[nod]) + if(!viz[it]) + dfs(it); + st.push(nod); +} + +void dfs_t(int nod) +{ + c[nod] = comp; + for(auto it : graf_t[nod]) + if(!c[it]) + dfs_t(it); +} + +void Solve() +{ + for(int i = 1;i <= 2 * n;++i) + if(!viz[i]) + dfs(i); + while(!st.empty()) + { + int node = st.top(); st.pop(); + + if(!c[node]) + { + ++comp; + dfs_t(node); + } + } + for(int i = 1;i <= n;++i) + if(c[i] > c[Not(i)]) + ++rez_nr, rez[i] = true; + g< +using namespace std; + + + +int n, m, a, b, c, F = 101, T = 102, viz[202], uz[202]; + +vector v[2 * 202], v2[2 * 202], H, ans; + + + +void dfs(int nod){ + + uz[nod] = 1; + + for (auto it : v[nod]) if (!uz[it]) dfs(it); + + H.push_back(nod); + +} + + + +void dfs2(int nod){ + + + + uz[nod] = 0; viz[nod ^ 1] = 1; + + if (nod&1) ans.push_back(nod/2 + 1); + + for (auto it : v2[nod]) if (uz[it]) dfs2(it); + +} + + + +void add(int a, int b){ + + a = (a < 0 ? -a * 2 - 1 : a * 2 - 2); + + b = (b < 0 ? -b * 2 - 1 : b * 2 - 2); + + + + + + v[a^1].push_back(b); + + v[b^1].push_back(a); + + v2[a].push_back(b^1); + + v2[b].push_back(a^1); + +} + + + +int main(){ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + cin >> n >> m; + + + + + + for (int i=1;i<=m;i++){ + + cin >> a >> b >> c; + + if (c == 0){ + + add(a, b); + + } + + else if (c == 1){ + + add(a, -b); + + } + + else if (c == 2){ + + add(-a, b); + + } + + else{ + + add(-a, -b); + + } + + } + + + + for (int i=0;i<2*n;i++) if (!uz[i]) dfs(i); + + reverse(H.begin(), H.end()); + + for (auto it : H){ + + + + if (uz[it] && uz[it^1]) dfs2(it); + + } + + + + cout << ans.size() << '\n'; + + for (auto it : ans) + + cout << it << '\n'; + + + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4d55e6e9-02b2-11ec-a888-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4d55e6e9-02b2-11ec-a888-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b70d7dbb47258c571efdc9b2bd203da8c5484ea8 --- /dev/null +++ b/party/2sat_implication_reduction/4d55e6e9-02b2-11ec-a888-2cf05d84bfc2.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in ("party.in"); + +ofstream out("party.out"); + +int n, m, x, y, timp, c; + +vector < vector > muchii, componente; + +vector disc, low; + +stack stk; + +vector realVal; + +inline int getPoz(int val) {return (val<0) ? -val: val+n;}; + +inline int getVal(int poz) {return (poz>n) ? poz-n: -poz;}; + +void tarjrec(int nod) + +{ + + low[nod]=disc[nod]=++timp; + + stk.push(nod); + + for(auto & x : muchii[nod]) + + { + + if(disc[x]==-1) + + { + + tarjrec(x); + + low[nod]=min(low[nod], low[x]); + + } + + else + + low[nod]=min(low[nod], disc[x]); + + } + + if(low[nod]==disc[nod]) + + { + + componente.push_back( vector()); int val; + + do + + { + + val=stk.top(); stk.pop(); + + componente.back().push_back(val); + + disc[val]=2*n+1; + + }while(val!=nod); + + } + +} + +inline void Tarjan() + +{ + + for(int i=1; i<=2*n; i++) + + if(disc[i]==-1) + + tarjrec(i); + +} + +int main() + +{ + + in>>n>>m; + + muchii.resize(2*n+1); + + disc.resize(2*n+1, -1); low.resize(2*n+1); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y>>c; + + if(c==1) + + y=-y; + + else if(c==2) + + x=-x; + + else if(c==3) + + x=-x, y=-y; + + muchii[getPoz(-x)].push_back(getPoz(y)); + + muchii[getPoz(-y)].push_back(getPoz(x)); + + } + + + + Tarjan(); muchii.clear(); low.clear(); disc.clear(); + + muchii.shrink_to_fit(); disc.shrink_to_fit(); low.shrink_to_fit(); + + + + + + + + realVal.resize(2*n+1, 0); + + + + for(auto itr=componente.rbegin(); itr!=componente.rend(); itr++) + + for(auto itr2=itr->begin(); itr2!=itr->end(); itr2++) + + if(realVal[getPoz(-getVal(*itr2))]==0) + + realVal[*itr2]=1, realVal[getPoz(-getVal(*itr2))]=2; + + + + int rez=0; + + for(int i=1; i<=n; i++) + + if(realVal[i+n]==2) + + rez++; + + + + out< +#include +#include + + + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = 200; + + + +int N, M; + +vector g[NMAX + 5]; + +vector t[NMAX + 5]; + + + +bool seen[NMAX + 5]; + +stack st; + +void dfs1(int node) + +{ + + seen[node] = true; + + + + for(auto it : g[node]) + + if(!seen[it]) dfs1(it); + + + + st.push(node); + +} + + + +int kComp; + +int comp[NMAX + 5]; + +void dfs2(int node) + +{ + + comp[node] = kComp; + + + + for(auto it : t[node]) + + if(comp[it] == 0) dfs2(it); + +} + + + +vector sol; + + + +int Not(int x) + +{ + + return x; + +} + + + +int Nrm(int x) + +{ + + return x + N; + +} + + + +int main() + +{ + + cin >> N >> M; + + + + for(int i = 1; i <= M; i++) + + { + + int x, y, z; + + cin >> x >> y >> z; + + + + if(z == 0) + + { + + g[Not(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Nrm(x)); + + + + t[Nrm(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Not(y)); + + } + + else if(z == 1) + + { + + g[Not(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Nrm(x)); + + + + t[Not(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Nrm(y)); + + } + + else if(z == 2) + + { + + g[Nrm(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Not(x)); + + + + t[Nrm(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Not(y)); + + } + + else + + { + + g[Nrm(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Not(x)); + + + + t[Not(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Nrm(y)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) + + if(!seen[i]) dfs1(i); + + + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(comp[node] == 0) + + { + + ++kComp; + + dfs2(node); + + } + + } + + + + for(int i = 1; i <= N; i++) + + if(comp[i + N] > comp[i]) + + sol.push_back(i); + + + + cout << sol.size() << '\n'; + + for(auto it : sol) + + cout << it << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4da376c2-02b2-11ec-9555-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4da376c2-02b2-11ec-9555-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e6256aece0adfe0d7302dbfa3a085548e2a608f --- /dev/null +++ b/party/2sat_implication_reduction/4da376c2-02b2-11ec-9555-2cf05d84bfc2.cpp @@ -0,0 +1,248 @@ +#include +std::ifstream fin ("party.in"); + +std::ofstream fout ("party.out"); + + + +int guest[105]; + +bool found_solution = false; + + + +std::vector < std::pair < int, int > > edge[105]; + +std::vector < std::pair < int, int > > reverse[105]; + + + +bool OR (int node1, int node2){ + + return guest[node1] | guest[node2]; + +} + + + +bool result (int node1, int node2){ + + if (guest[node1] == 1 and guest[node2] == 0) + + return 0; + + return 1; + +} + + + +bool AND (int node1, int node2){ + + return guest[node1] & guest[node2]; + +} + + + +bool verif (int node){ + + for (int i=0; i> V >> E; + + for (i=1; i<=E; i++){ + + fin >> src >> dest >> type; + + if (type == 1){ + + type = 2; + + std::swap (src, dest); + + } + + edge[src].push_back ({dest, type}); + + reverse[dest].push_back ({src, type}); + + } + + + + for (i=1; i<=V; i++) + + guest[i] = -1; + + + + for (i=1; i<=V; i++){ + + if (guest[i] == -1){ + + DFS (i); + + + + } + + } + + + + int ans = 0; + + for (i=1; i<=V; i++){ + + if (guest[i] == 1) + + ans ++; + + } + + fout << ans << '\n'; + + for (i=1; i<=V; i++) + + if (guest[i] == 1) + + fout << i << '\n'; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4dc271bd-02b2-11ec-922f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4dc271bd-02b2-11ec-922f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b37a0b66de23803d49b5c7100a8b4f0c3ba7c33 --- /dev/null +++ b/party/2sat_implication_reduction/4dc271bd-02b2-11ec-922f-2cf05d84bfc2.cpp @@ -0,0 +1,227 @@ +#include + + + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +class _2sat{ + + private: + + int n; + + vector > V,gV; + + vector uz; + + vector ord,comp; + + vector ans; + + + + void dfs1(int node){ + + uz[node]=true; + + for(auto& it:V[node]) + + if(!uz[it]) + + dfs1(it); + + ord.push_back(node); + + } + + void dfs2(int node,int c){ + + comp[node]=c; + + for(auto& it:gV[node]) + + if(!comp[it]) + + dfs2(it,c); + + } + + public: + + _2sat(int n){ + + this->n=n; + + V.resize(2*n+1); + + gV.resize(2*n+1); + + uz.resize(2*n+1); + + comp.resize(2*n+1); + + ans.resize(n+1); + + } + + void addegde(int x,int y){ + + cerr << "x,y" << " ->", debug_out(x,y); + + V[x].push_back(y); + + gV[y].push_back(x); + + } + + void solve(){ + + for(int i=1;i<=2*n;i++) + + if(!uz[i]) + + dfs1(i); + + for(int i=1,cates=0;i<=2*n;i++){ + + int node=ord[2*n-i]; + + if(!comp[node]) + + dfs2(node,++cates); + + } + + for(int i=1;i<=n;i++){ + + if(comp[i] == comp[i+n]){ + + fout<<"-1\n"; + + return; + + } + + ans[i]=(comp[i] > comp[i+n]); + + } + + int cate=0; + + for(int i=1;i<=n;i++) + + if(ans[i]) + + cate++; + + fout< vec; + +vector arb[DMAX]; + + + +int V[DMAX]; + + + +int n,m; + + + +int NOT(int node){ + + if(node > n) + + return node-n; + + return node+n; + +} + + + +int main(){ + + int t,i,j; + + int op,x,y; + + + + fin>>n>>m; + + _2sat var(n); + + for(i=1;i<=m;i++){ + + fin>>x>>y>>op; + + if(op == 1) + + y=NOT(y); + + else if(op == 2) + + x=NOT(x); + + else if(op == 3){ + + x=NOT(x); + + y=NOT(y); + + } + + var.addegde(NOT(x),y); + + var.addegde(NOT(y),x); + + } + + var.solve(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4df88727-02b2-11ec-901a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4df88727-02b2-11ec-901a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6fa6fdb0c55ac1256012298d81a5ff2adf792a --- /dev/null +++ b/party/2sat_implication_reduction/4df88727-02b2-11ec-901a-2cf05d84bfc2.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + + +std::ifstream fin("party.in"); + +std::ofstream fout("party.out"); + + + +const int NMAX = 500; + + + +std::vector g[NMAX], rg[NMAX]; + +std::bitset used, ans; + +std::stack topo; + +bool not_ok; + +int n, m, x, y, c; + + + +inline int neg(int x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void DFS1(int node) + +{ + + used[node] = 1; + + for (auto& i : g[node]) + + if (used[i] == 0) + + DFS1(i); + + topo.push(node); + +} + + + +void DFS2(int node) + +{ + + if (ans[node] == 1) + + not_ok = true; + + used[node] = 1; + + ans[neg(node)] = 1; + + for (auto& i : rg[node]) + + if (used[i] == 0) + + DFS2(i); + +} + + + +void poz() + +{ + + if (c == 1) + + y += n; + + else if (c == 2) + + x += n; + + else if (c == 3) + + { + + x += n; + + y += n; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + poz(); + + int negx = neg(x), negy = neg(y); + + g[negx].push_back(y); + + g[negy].push_back(x); + + rg[x].push_back(negy); + + rg[y].push_back(negx); + + } + + + + for (int i = 1; i <= 2 * n; ++i) + + if (used[i] == 0) + + DFS1(i); + + + + used.reset(); + + + + while (!topo.empty()) + + { + + x = topo.top(); + + topo.pop(); + + if (used[x] == 0 && used[neg(x)] == 0) + + DFS2(x); + + } + + + + int cnt(0); + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + cnt++; + + fout << cnt << "\n"; + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + fout << i << "\n"; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4e1d310f-02b2-11ec-b346-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4e1d310f-02b2-11ec-b346-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09056d35f508702c5188005677d33812d1ae422c --- /dev/null +++ b/party/2sat_implication_reduction/4e1d310f-02b2-11ec-b346-2cf05d84bfc2.cpp @@ -0,0 +1,282 @@ +#include +#include +#include +using namespace std; + +const int NMAX = 100; + +const int MMAX = 1000; + +int n; + +class guests + +{ + +private: + + bool a[NMAX + 5]; + +public: + + guests(int x = 0) + + { + + memset(a , 0 , sizeof(guests)); + + } + + guests operator&(const guests& b) + + { + + guests c; + + for(int i = 1 ; i <= n ; i ++) + + c.a[i] = (a[i] & b.a[i]); + + return c; + + } + + guests operator|(const guests& b) + + { + + guests c; + + for(int i = 1 ; i <= n ; i ++) + + c.a[i] = (a[i] | b.a[i]); + + return c; + + } + + bool operator==(const guests& b) + + { + + for(int i = 1 ; i <= n ; i ++) + + if(a[i] != b.a[i]) + + return 0; + + return 1; + + } + + void init(int x = 0 , int y = 0) + + { + + a[x] = a[y] = 1; + + } + + void init(bool b[NMAX + 5]) + + { + + for(int i = 1 ; i <= n ; i ++) + + a[i] = b[i]; + + } + + int nr_elim() + + { + + int nr = 0 , i; + + for(i = 1 ; i <= n ; i ++) + + nr += a[i]; + + return nr; + + } + + void afisare() + + { + + for(int i = 1 ; i <= n ; i ++) + + if(!a[i]) + + printf("%d\n" , i); + + } + +}; + +guests t0[MMAX + 5] , t3[2 * MMAX + 5] , sol; + +int a[MMAX + 5] , b[MMAX + 5] , l , maxim , k0; + +bool elim[NMAX + 5]; + +vector G[NMAX + 5]; + +void dfs(int u) + +{ + + int v , j; + + elim[u] = 1; + + for(j = 0 ; j < G[u].size() ; j ++) + + { + + v = G[u][j]; + + if(!elim[v]) + + dfs(v); + + } + +} + +int valid(guests p1 , guests p2) + +{ + + guests aux; + + aux = (p1 | p2); + + for(int i = 1 ; i <= k0 ; i ++) + + if((aux & t0[i]) == t0[i]) + + return 0; + + return 1; + +} + +void backt(int k , guests p) + +{ + + if(k == l + 1) + + { + + int aux = p.nr_elim(); + + if(n - aux > maxim) + + { + + maxim = n - aux; + + sol = p; + + } + + } + + else + + { + + if(valid(p , t3[k])) + + backt(k + 2 , (p | t3[k])); + + if(valid(p , t3[k + 1])) + + backt(k + 2 , (p | t3[k + 1])); + + } + +} + +void add(int x) + +{ + + memset(elim , 0 , sizeof(elim)); + + dfs(x); + + t3[++ l].init(elim); + +} + +int main() + +{ + + freopen("party.in" , "r" , stdin); + + freopen("party.out" , "w" , stdout); + + int m , i , x , y , type , k3; + + scanf("%d%d" , &n , &m); + + k3 = 0; + + for(i = 1 ; i <= m ; i ++) + + { + + scanf("%d%d%d" , &x , &y , &type); + + if(type == 0) + + t0[++ k0].init(x , y); + + else if(type == 1) + + G[x].push_back(y); + + else if(type == 2) + + G[y].push_back(x); + + else + + { + + a[++ k3] = x; + + b[k3] = y; + + } + + } + + l = 0; + + for(i = 1 ; i <= k3 ; i ++) + + { + + add(a[i]); + + add(b[i]); + + } + + guests p; + + backt(1 , p); + + printf("%d\n" , n - sol.nr_elim()); + + sol.afisare(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/4e3d79d5-02b2-11ec-91f8-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4e3d79d5-02b2-11ec-91f8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2e65386b41319b767b9b8104ad0b54910e05d74 --- /dev/null +++ b/party/2sat_implication_reduction/4e3d79d5-02b2-11ec-91f8-2cf05d84bfc2.cpp @@ -0,0 +1,188 @@ +#include + + +using namespace std; + + + +vector L[210],ans; + +int level[210],low[210],viz[210],sol[210]; + +deque s; + +int n,m,i,x,y,tip,idx,nx,ny; + + + +void dfs (int nod){ + + + + level[nod] = low[nod] = ++idx; + + viz[nod] = 1; + + s.push_back(nod); + + for (auto vecin : L[nod]){ + + if (!level[vecin]){ + + dfs (vecin); + + low[nod] = min (low[nod],low[vecin]); + + } else { + + if (viz[vecin]) + + low[nod] = min (low[nod],low[vecin]); + + } + + } + + if (low[nod] == level[nod]){ + + int x; + + do{ + + x = s.back(); + + int nx; + + if (x <= n) + + nx = x + n; + + else nx = x - n; + + + + if (sol[x] == 0){ + + sol[x] = 1; + + sol[nx] = -1; + + } + + + + s.pop_back(); + + viz[x] = 0; + + + + } while (x != nod); + + + + } + +} + + + +int main (){ + + + + ifstream fin ("party.in"); + + ofstream fout ("party.out"); + + + + fin>>n>>m; + + for (i=1;i<=m;i++){ + + fin>>x>>y>>tip; + + if (tip == 0){ + + nx = x + n; + + ny = y + n; + + } else { + + if (tip == 1){ + + nx = x + n; + + ny = y; + + y += n; + + } else { + + if (tip == 2){ + + nx = x; + + x += n; + + ny = y + n; + + } else { + + nx = x; + + x += n; + + ny = y; + + y += n; + + }}} + + + + L[nx].push_back(y); + + L[ny].push_back(x); + + } + + + + + + for (i=1;i<=2*n;i++){ + + if (!viz[i]) + + dfs (i); + + } + + + + for (i=1;i<=n;i++) + + if (sol[i] == 1) + + ans.push_back(i); + + + + sort (ans.begin(),ans.end()); + + fout< +std::ifstream fin ("party.in"); + +std::ofstream fout ("party.out"); + + + +int guest[105]; + +bool found_solution = false; + + + +std::vector < std::pair < int, int > > edge[105]; + +std::vector < std::pair < int, int > > reverse[105]; + + + +bool OR (int node1, int node2){ + + return guest[node1] | guest[node2]; + +} + + + +bool result (int node1, int node2){ + + if (guest[node1] == 1 and guest[node2] == 0) + + return 0; + + return 1; + +} + + + +bool AND (int node1, int node2){ + + return guest[node1] & guest[node2]; + +} + + + +bool verif (int node){ + + for (int i=0; i> V >> E; + + for (i=1; i<=E; i++){ + + fin >> src >> dest >> type; + + if (type == 1){ + + type = 2; + + std::swap (src, dest); + + } + + edge[src].push_back ({dest, type}); + + reverse[dest].push_back ({src, type}); + + } + + + + for (i=1; i<=V; i++) + + guest[i] = -1; + + + + for (i=1; i<=V; i++){ + + if (guest[i] == -1){ + + DFS (i); + + + + } + + } + + + + int ans = 0; + + for (i=1; i<=V; i++){ + + if (guest[i] == 1) + + ans ++; + + } + + fout << ans << '\n'; + + for (i=1; i<=V; i++) + + if (guest[i] == 1) + + fout << i << '\n'; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4ed00815-02b2-11ec-a422-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4ed00815-02b2-11ec-a422-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e7c2a0598cab9c3455e742a3230ad76f92f5913 --- /dev/null +++ b/party/2sat_implication_reduction/4ed00815-02b2-11ec-a422-2cf05d84bfc2.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; + +ifstream fin("party.in"); +ofstream fout("party.out"); + +class TwoSat { + private: + int n; + vector> adG, adT; + + inline int non(int x) { + return (x > n ? x - n : x + n); + } + + void addEdge(int x, int y) { + adG[x].push_back(y); + adT[y].push_back(x); + } + + void dfsG(int node, vector& vis, vector& topo) { + vis[node] = true; + for (int nghb : adG[node]) + if (!vis[nghb]) + dfsG(nghb, vis, topo); + topo.push_back(node); + } + + void dfsT(int node, vector& vis, vector& ans, bool& ok) { + vis[node] = false; + if (ans[node]) + ok = false; + ans[non(node)] = true; + for (int nghb : adT[node]) + if (vis[nghb]) + dfsT(nghb, vis, ans, ok); + } + + public: + TwoSat(int n) : + n(n), adG(2 * n + 1), adT(2 * n + 1) { } + + void addProp(int x, int y) { + if (x < 0) x = n - x; + if (y < 0) y = n - y; + addEdge(non(x), y); + addEdge(non(y), x); + } + + vector solve() { + vector vis(2 * n + 1); + vector topo; + for (int i = 1; i <= 2 * n; i++) + if (!vis[i]) + dfsG(i, vis, topo); + reverse(topo.begin(), topo.end()); + + bool ok = true; + vector ans(2 * n + 1); + for (int node : topo) + if (vis[node] && vis[non(node)]) + dfsT(node, vis, ans, ok); + + if (!ok) + return vector(); + ans.resize(n + 1); + return ans; + } +}; + +int main() { + int n, m; fin >> n >> m; + TwoSat graph(n); + for (int i = 0; i < m; i++) { + int x, y, t; fin >> x >> y >> t; + if (t == 0) + graph.addProp(+x, +y); + else if (t == 1) + graph.addProp(+x, -y); + else if (t == 2) + graph.addProp(-x, +y); + else + graph.addProp(-x, -y); + } + + auto ans = graph.solve(); + int cnt = 0; + for (int i = 1; i <= n; i++) + cnt += ans[i]; + fout << cnt << '\n'; + for (int i = 1; i <= n; i++) + if (ans[i]) + fout << i << '\n'; + + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/4eee8c91-02b2-11ec-a422-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4eee8c91-02b2-11ec-a422-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7d0d92106751db098b6c4dbf6c5561732291737 --- /dev/null +++ b/party/2sat_implication_reduction/4eee8c91-02b2-11ec-a422-2cf05d84bfc2.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("party.in"); + +ofstream cout ("party.out"); + + + +int n, m, x, y, type; + + + +bool viz[200005]; + +int val[100005], ind[200005], deg[200005]; + +vector g[200005], ginv[200005]; + +vector comp, st; + +vector > comps; + +queue q; + + + +int indx(int val) { + + if(val < 0) + + return n - val; + + return val; + +} + + + +int non(int val) { + + if(val > n) + + return val - n; + + return val + n; + +} + + + +void dfsp(int nod) { + + viz[nod] = 1; + + for(auto &fiu : g[nod]) { + + if(!viz[fiu]) + + dfsp(fiu); + + } + + st.push_back(nod); + +} + + + +void dfsm(int nod) { + + viz[nod] = 1; + + comp.push_back(nod); + + for(auto &fiu : ginv[nod]) { + + if(!viz[fiu]) + + dfsm(fiu); + + } + +} + + + +int main() { + + cin >> n >> m; + + for(int i = 1; i <= m; i++) { + + cin >> x >> y >> type; + + if(type == 0) { + + g[non(x)].push_back(y); + + g[non(y)].push_back(x); + + ginv[x].push_back(non(y)); + + ginv[y].push_back(non(x)); + + } else if(type == 3) { + + ginv[non(x)].push_back(y); + + ginv[non(y)].push_back(x); + + g[x].push_back(non(y)); + + g[y].push_back(non(x)); + + } else if(type == 1) { + + g[non(x)].push_back(non(y)); + + g[y].push_back(x); + + ginv[x].push_back(y); + + ginv[non(y)].push_back(non(x)); + + } else { + + ginv[non(x)].push_back(non(y)); + + ginv[y].push_back(x); + + g[x].push_back(y); + + g[non(y)].push_back(non(x)); + + } + + } + + for(int i = 1; i <= 2 * n; i++) { + + if(!viz[i]) + + dfsp(i); + + } + + for(int i = 1; i <= 2 * n; i++) + + viz[i] = 0; + + while(!st.empty()) { + + int nod = st.back(); + + st.pop_back(); + + if(!viz[nod]) { + + comp.clear(); + + dfsm(nod); + + for(auto &Nod : comp) + + ind[Nod] = comps.size(); + + comps.push_back(comp); + + } + + } + + for(int i = 1; i <= n; i++) { + + + + if(ind[i] == ind[i + n]) { + + + + cout << -1; + + return 0; + + } + + } + + for(int i = 1; i <= 2 * n; i++) { + + for(auto &fiu : g[i]) { + + if(ind[fiu] != ind[i]) + + deg[ind[fiu]]++; + + } + + } + + for(int i = 1; i <= n; i++) + + val[i] = 2; + + for(int i = 0; i < comps.size(); i++) { + + if(!deg[i]) + + q.push(i); + + } + + while(!q.empty()) { + + int cmp = q.front(); + + q.pop(); + + for(auto &nod : comps[cmp]) { + + int trueNod = (nod > n ? nod - n : nod); + + if(val[trueNod] == 2) + + val[trueNod] = (nod > n); + + } + + for(auto &nod : comps[cmp]) { + + for(auto &fiu : g[nod]) { + + if(ind[nod] != ind[fiu]) { + + deg[ind[fiu]]--; + + if(!deg[ind[fiu]]) + + q.push(ind[fiu]); + + } + + } + + } + + } + + vector guests; + + for(int i = 1; i <= n; i++) { + + if(val[i]) + + guests.push_back(i); + + } + + cout << guests.size() << "\n"; + + for(auto &guest : guests) + + cout << guest << "\n"; + + return 0; + +} diff --git a/party/2sat_implication_reduction/4f0db090-02b2-11ec-8a34-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4f0db090-02b2-11ec-8a34-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1538e2cfb17f3e14a8daaec76176242823b9fbf0 --- /dev/null +++ b/party/2sat_implication_reduction/4f0db090-02b2-11ec-8a34-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +const int NMax = 200; + + + +vector G[NMax + 5], GT[NMax + 5], Topo; + +bool Sol[NMax + 5], Use[NMax + 5]; + +int N, M, Nr; + + + +inline int non(int x) + +{ + + return (x > N ? x - N : x + N); + +} + + + +void AddEdge(int x, int y) + +{ + + G[x].push_back(y); + + GT[y].push_back(x); + +} + + + +void DFSP(int Node) + +{ + + Use[Node] = 1; + + + + for(auto Next : G[Node]) + + if(!Use[Next]) + + DFSP(Next); + + + + Topo.push_back(Node); + +} + + + +void DFSM(int Node) + +{ + + Use[Node] = Sol[non(Node)] = 1; + + + + for(auto Next : GT[Node]) + + if(!Use[Next]) + + DFSM(Next); + +} + + + +void Kosaraju() + +{ + + for(int i = 1; i <= 2 * N; i++) + + if(!Use[i]) + + DFSP(i); + + + + for(int i = 1; i <= 2 * N; i++) + + Use[i] = 0; + + + + while(!Topo.empty()) { + + int Node = Topo.back(); Topo.pop_back(); + + + + if(!Use[Node] && !Use[non(Node)]) + + DFSM(Node); + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1, type, x, y; i <= M; i++) + + { + + fin >> x >> y >> type; + + x += (type & 2) / 2 * N; + + y += (type & 1) * N; + + + + AddEdge(non(x), y); + + AddEdge(non(y), x); + + } + + Kosaraju(); + + + + for(int i = 1; i <= N; i++) + + Nr += Sol[i]; + + + + fout << Nr << '\n'; + + + + for(int i = 1; i <= N; i++) + + if(Sol[i]) + + fout << i << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/4f2d9fdb-02b2-11ec-a5c7-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/4f2d9fdb-02b2-11ec-a5c7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99f26a1222bd07cf6cb1c43348d3237f88eb8046 --- /dev/null +++ b/party/2sat_implication_reduction/4f2d9fdb-02b2-11ec-a5c7-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + +int n , sol , g , elem; + +vector v[200010]; + +set sl; + +int niv[200010] , low[200010], s[200010] , f[200010] , val[200010]; + +int invers (int x){ + + if (x <= n) + + return x + n; + + else return x - n; + +} + +void dfs (int nod){ + + int vecin,i; + + g++; + + niv[nod]=g; + + low[nod]=g; + + s[++elem]=nod; + + f[nod]=1; + + for (i=0;i +#include +#include +#include +#include +using namespace std; + +vector g[205]; + +vector gt[205]; + +vector l; + +vector comp[205]; + +int pred[205]; + +vector succ[205]; + +int val[205]; + +bool ans[205]; + +bool viz[205]; + +int f[205]; + +queue q; + +int cont; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(int v : g[nod]) + + if(!viz[v]) + + dfs(v); + + l.push_back(nod); + +} + +void dfst(int nod) + +{ + + viz[nod]=1; + + for(int v : gt[nod]) + + if(!viz[v]) + + dfst(v); + + comp[cont].push_back(nod); + + f[nod]=cont; + +} + +FILE *_fin; + +FILE *_fout; + +int _in_loc; char _in_buff[4096]; + + + + + +void read_init(const char* nume) + +{ + + _fin = fopen(nume, "r"); + + _in_loc = 4095; + +} + + + +char read_ch() + +{ + + ++_in_loc; + + if (_in_loc == 4096) { + + _in_loc = 0; + + fread(_in_buff, 1, 4096, _fin); + + } + + return _in_buff[_in_loc]; + +} + + + +int read_u32() + +{ + + int u32 = 0; char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + sgn = -1; + + } else { + + u32 = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + u32 = u32 * 10 + c - '0'; + + } + + return u32 * sgn; + +} + + + +long long read_u64() + +{ + + long long u64 = 0; char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + sgn = -1; + + } else { + + u64 = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + u64 = u64 * 10 + c - '0'; + + } + + return u64 * sgn; + +} + +int main() + +{ read_init("party.in"); + + _fout=fopen("party.out", "w"); + + int n,m,i,x,y,j,nr=0,z; + + n=read_u32(); + + m=read_u32(); + + for(i=1; i<=m; i++){ + + x=read_u32(); + + y=read_u32(); + + z=read_u32(); + + if(z==0){ + + g[2*x+1].push_back(2*y); + + g[2*y+1].push_back(2*x); + + gt[2*y].push_back(2*x+1); + + gt[2*x].push_back(2*y+1); + + } + + else{ + + if(z==1){ + + g[2*x+1].push_back(2*y+1); + + g[2*y].push_back(2*x); + + gt[2*y+1].push_back(2*x+1); + + gt[2*x].push_back(2*y); + + } + + else{ + + if(z==2){ + + g[2*x].push_back(2*y); + + g[2*y+1].push_back(2*x+1); + + gt[2*x+1].push_back(2*y+1); + + gt[2*y].push_back(2*x); + + } + + else{ + + g[2*x].push_back(2*y+1); + + g[2*y].push_back(2*x+1); + + gt[2*y+1].push_back(2*x); + + gt[2*x+1].push_back(2*y); + + } + + } + + } + + } + + for(i=1; i<=2*n+1; i++) + + val[i]=-1; + + for(i=2; i<=2*n+1; i++) + + if(!viz[i]) + + dfs(i); + + reverse(l.begin(), l.end()); + + for(int u : l) + + viz[u]=0; + + for(int u : l){ + + if(!viz[u]){ + + cont++; + + dfst(u); + + } + + } + + for(i=2; i<=2*n+1; i++){ + + if(f[(i/2)*2+(i%2+1)%2]==f[i]){ + + fprintf(_fout, "-1"); + + return 0; + + } + + for(j=0; j + + +using namespace std; + +int n, m, i, j, v, cnt, out; + +int ctc[201], sol[201]; + +stack s; + +bool check[201], ins[201], done[201]; + +vector graph[201], ctclist[201]; + +pair p[201]; + + + +void read(); + +void solve(); + +void write(); + +int nt(int node); + +void dfs(int node); + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} + +void read(){ + + freopen("party.in", "r", stdin); + + scanf("%d%d", &n, &m); + + for(i=1; i<=m; ++i){ + + int x, y, z; + + scanf("%d%d%d", &x, &y, &z); + + switch(z){ + + case 0:{graph[nt(x)].push_back(y); graph[nt(y)].push_back(x); break;} + + case 1:{graph[nt(x)].push_back(nt(y)); graph[y].push_back(x); break;} + + case 2:{graph[nt(y)].push_back(nt(x)); graph[x].push_back(y); break;} + + default:{ + + graph[x].push_back(nt(y)); + + graph[y].push_back(nt(x)); + + } + + } + + } + + return; + +} + +void solve(){ + + for(i=1; i<=2*n; ++i) if(!check[i]) dfs(i); + + for(i=v; i>=1; --i) if(!done[i]){ + + int ch=ctc[nt(ctclist[i][0])]; + + done[i]=done[ch]=true; + + for(auto j:ctclist[i]) sol[j]=0; + + for(auto j:ctclist[ch]) sol[j]=1; + + } + + for(i=1; i<=n; ++i) if(sol[i]) ++out; + + return; + +} + +void write(){ + + freopen("party.out", "w", stdout); + + printf("%d\n", out); + + for(i=1; i<=n; ++i) if(sol[i]) printf("%d\n", i); + +} + +int nt(int node){ + + if(node<=n) return node+n; + + return node-n; + +} + +void dfs(int node){ + + ++cnt; s.push(node); + + p[node]={cnt, cnt}; + + check[node]=ins[node]=true; + + for(auto i:graph[node]){ + + if(!check[i]){ + + dfs(i); + + p[node].second=min(p[node].second, p[i].second); + + } + + else if(ins[i]) p[node].second=min(p[node].second, p[i].first); + + } + + if(p[node].first==p[node].second){ + + ++v; + + while(s.top()!=node){ + + ctclist[v].push_back(s.top()); + + ctc[s.top()]=v; + + ins[s.top()]=false; + + s.pop(); + + } + + ctclist[v].push_back(s.top()); + + ctc[s.top()]=v; + + ins[s.top()]=false; + + s.pop(); + + } + + return; + +} diff --git a/party/2sat_implication_reduction/50536c80-02b2-11ec-92c5-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/50536c80-02b2-11ec-92c5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5328949a23e28e0ef1a5be02b096c44cbe001f3f --- /dev/null +++ b/party/2sat_implication_reduction/50536c80-02b2-11ec-92c5-2cf05d84bfc2.cpp @@ -0,0 +1,296 @@ +#include + + +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int DIM = 2007; + + + +vector v[2 * DIM]; + +vector v_back[2 * DIM]; + + + +int n; + + + +int Not(int nod) + +{ + + return (nod <= n ? nod + n : nod - n); + +} + + + +vector st; + + + +bitset <2 * DIM> vis; + + + +int componenta[2 * DIM]; + + + +void dfs1(int nod) + +{ + + vis[nod] = true; + + + + for(auto i : v[nod]) + + if(vis[i] == 0) + + dfs1(i); + + + + st.push_back(nod); + +} + + + +vector CTC[2 * DIM]; + + + +int nr; + + + +void dfs2(int nod) + +{ + + componenta[nod] = nr; + + + + CTC[nr].push_back(nod); + + + + for(auto i : v_back[nod]) + + if(componenta[i] == 0) + + dfs2(i); + +} + + + +bool sol[2 * DIM]; + + + +void atrib(int ord) + +{ + + vis[ord] = false; + + + + for(auto i : CTC[ord]) + + { + + sol[i] = 2; + + sol[Not(i)] = 1; + + } + +} + + + +int main() + +{ + + int m; + + in >> n >> m; + + + + while(m--) + + { + + int x, y, op; + + in >> x >> y >> op; + + + + int X = Not(x); + + int Y = Not(y); + + + + if(op == 0) + + { + + v[X].push_back(y); + + v[Y].push_back(x); + + + + v_back[y].push_back(X); + + v_back[x].push_back(Y); + + } + + else + + if(op == 1) + + { + + v[X].push_back(Y); + + v[y].push_back(x); + + + + v_back[Y].push_back(X); + + v_back[x].push_back(y); + + } + + else + + if(op == 2) + + { + + v[x].push_back(y); + + v[Y].push_back(X); + + + + v_back[y].push_back(x); + + v_back[X].push_back(Y); + + } + + else + + { + + v[x].push_back(Y); + + v[y].push_back(X); + + + + v_back[Y].push_back(x); + + v_back[X].push_back(y); + + } + + } + + + + for(int i = 1; i <= 2 * n; i++) + + if(vis[i] == 0) + + dfs1(i); + + + + for(int i = 2 * n; i >= 1; i--) + + if(componenta[st[i - 1]] == 0) + + { + + nr++; + + + + dfs2(st[i - 1]); + + } + + + + for(int i = 2 * n - 1; i >= 0; i--) + + if(vis[componenta[st[i]]] == true) + + { + + int nod = st[i]; + + int inv = Not(nod); + + + + vis[componenta[nod]] = false; + + vis[componenta[inv]] = false; + + + + for(auto j : CTC[componenta[nod]]) sol[j] = false, sol[Not(j)] = true; + + } + + + + int nr = 0; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + nr++; + + + + out << nr << '\n'; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + out << i << '\n'; + +} diff --git a/party/2sat_implication_reduction/512032a7-02b2-11ec-8c6e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/512032a7-02b2-11ec-8c6e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c8e349acf9d76a96f39aec3e5dade3be902489 --- /dev/null +++ b/party/2sat_implication_reduction/512032a7-02b2-11ec-8c6e-2cf05d84bfc2.cpp @@ -0,0 +1,322 @@ +#include +#include +#include + + +using namespace std; + + + +FILE *f = fopen("party.in","r"); + +FILE *g = fopen("party.out","w"); + + + +const int NMAX = 1e2; + +const int OFFSET = NMAX + 2; + + + +vector graph[2 * OFFSET + 5]; + + + +int n,m; + +bool viz[2 * OFFSET + 5]; + +int st[2 * OFFSET + 5],len; + +bool inst[2 * OFFSET + 5]; + +int ctc[2 * OFFSET + 5],last_ctc; + +int id[2 * OFFSET + 5],last_id; + +int low[2 * OFFSET + 5]; + +vector ctc_nodes[2 * OFFSET + 5]; + + + +void dfs(int nod){ + + viz[nod] = true; + + st[++len] = nod;inst[nod] = true; + + id[nod] = low[nod] = ++last_id; + + + + for(auto it:graph[nod]){ + + if(viz[it] == false){ + + dfs(it); + + } + + if(inst[it]){ + + low[nod] = min(low[nod],low[it]); + + } + + } + + + + if(low[nod] == id[nod]){ + + last_ctc++; + + while(st[len] != nod){ + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + +} + + + +int ans[2 * OFFSET + 5]; + +int ctc_ans[2 * OFFSET + 5]; + +int ctc_gr[2 * OFFSET + 5]; + + + +vector solve_clauses(){ + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + ans[i] = -1; + + if(viz[i] == false){ + + dfs(i); + + } + + } + + + + for(int i = 1;i <= last_ctc;i++){ + + ctc_ans[i] = -1; + + } + + + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + for(auto it:graph[i]){ + + if(ctc[it] != ctc[i]){ + + ctc_gr[ctc[it]]++; + + } + + } + + } + + + + vector topo; + + + + for(int i = 1;i <= last_ctc;i++){ + + if(ctc_gr[i] == 0){ + + topo.push_back(i); + + } + + } + + + + for(int i = 0;i < (int)topo.size();i++){ + + int comp = topo[i]; + + int val = ctc_ans[comp]; + + + + for(auto it:ctc_nodes[comp]){ + + val = max(val,ans[it]); + + } + + + + val = max(val,0); + + + + ctc_ans[comp] = val; + + + + for(auto it:ctc_nodes[comp]){ + + ans[it] = val; + + ans[2 * OFFSET - it] = 1- val; + + for(auto it2:graph[it]){ + + if(ctc[it2] != ctc[it]){ + + ctc_gr[ctc[it2]]--; + + if(ctc_gr[ctc[it2]] == 0){ + + topo.push_back(ctc[it2]); + + } + + if(val == 1){ + + ctc_ans[ctc[it2]] = 1; + + } + + } + + } + + } + + } + + + + vector cnt; + + + + for(int i = OFFSET + 1;i <= OFFSET + n;i++){ + + if(ans[i]){ + + cnt.push_back(i - OFFSET); + + } + + } + + + + return cnt; + +} + + + +int main(){ + + + + fscanf(f,"%d %d",&n,&m); + + + + for(int i = 1;i <= m;i++){ + + int x,y,z; + + fscanf(f,"%d %d %d",&x,&y,&z); + + if(z == 0){ + + graph[OFFSET - x].push_back(OFFSET + y); + + graph[OFFSET - y].push_back(OFFSET + x); + + } + + else if(z == 1){ + + graph[OFFSET - x].push_back(OFFSET - y); + + } + + else if(z == 2){ + + graph[OFFSET - y].push_back(OFFSET - x); + + } + + else{ + + graph[OFFSET + x].push_back(OFFSET - y); + + graph[OFFSET + y].push_back(OFFSET - x); + + } + + } + + + + vector tmp = solve_clauses(); + + + + fprintf(g,"%d\n",(int)tmp.size()); + + + + for(auto it:tmp){ + + fprintf(g,"%d\n",it); + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/52070ff1-02b2-11ec-b316-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/52070ff1-02b2-11ec-b316-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be2661f5377e6c2a8b86702aae714f63ea908d10 --- /dev/null +++ b/party/2sat_implication_reduction/52070ff1-02b2-11ec-b316-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +ifstream cin("a.in"); +ofstream cout("a.out"); + +template +void print(v Vector, type nr) { + for_each(Vector.begin(),Vector.end(), [](type x) { + cout << x << ' '; + }); +} + +struct vertex { + list adi; + list invadi; +}; + +int n; +vertex nod[300]; +bitset<300> viz; +vector > ctc; +stack finish; +int nrcomp = 0; +int sol[300]; + +int non(int nod) { + if (nod <= n) + return nod + n; + else return nod - n; +} + +void edge(const int p1, const int p2) { + nod[p1].adi.push_back(p2); + nod[p2].invadi.push_back(p1); +} + +void dfs(const int start) { + viz[start] = true; + for (auto it = nod[start].adi.begin(); it != nod[start].adi.end(); ++it) { + if (!viz[*it]) + dfs(*it); + } + finish.push(start); +} + +void dfsinv(const int start) { + viz[start] = true; + ctc[nrcomp].emplace_back(start); + for (auto it = nod[start].invadi.begin(); it != nod[start].invadi.end(); ++it) { + if (!viz[*it]) + dfsinv(*it); + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + clock_t tStart = clock(); + int m, x, y, cod; + cin >> n >> m; + while (m--) { + cin >> x >> y >> cod; + + switch (cod) { + case 0: { + edge(non(x), y); + edge(non(y), x); + break; + } + case 1: { + edge(non(x), non(y)); + edge(y, x); + break; + } + case 2: { + edge(non(y), non(x)); + edge(x, y); + break; + } + case 3: { + edge(x, non(y)); + edge(y, non(x)); + break; + } + } + } + for (int i = 1; i <= 2*n; ++i) { + if (!viz[i]) + dfs(i); + } + + viz.reset(); + vector temp; + ctc.push_back(temp); + + while (!finish.empty()) { + if (!viz[finish.top()]) { + ctc.push_back(temp); + ++nrcomp; + dfsinv(finish.top()); + } + finish.pop(); + } + for (int i = 0; i <= 2*n; ++i) + sol[i] = -1; + + int participanti = 0; + + for (int i = nrcomp; i >= 1; --i) { + if (sol[ctc[i][0]] == -1) { + for (auto it = ctc[i].begin(); it != ctc[i].end(); ++it) { + sol[*it] = 1; + sol[non(*it)] = 0; + } + } + } + for (int i = 1; i <= n; ++i) { + if (sol[i] == -1) { + sol[i] = 1; + } + participanti += sol[i]; + } + cout << participanti << '\n'; + for (int i = 1; i <= n; ++i) { + if (sol[i]) + cout << i << '\n'; + } + + printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); +} diff --git a/party/2sat_implication_reduction/52446711-02b2-11ec-95ef-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/52446711-02b2-11ec-95ef-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0259d2689f58bffdac4b64c0de19aace0c7a1414 --- /dev/null +++ b/party/2sat_implication_reduction/52446711-02b2-11ec-95ef-2cf05d84bfc2.cpp @@ -0,0 +1,146 @@ +#include + + + + +using namespace std; + + + +ifstream fin("party.in") ; + +ofstream fout("party.out") ; + + + +vector graf[205] ; + +vector graft[205] ; + +vector ord , sol ; + +bitset<205> bt ; + +int n , m , rng[205] , cmp ; + + + +void add(int x , int y) + +{ + + graf[x].push_back(y) ; + + graft[y].push_back(x) ; + +} + + + +void citire() + +{ + + int x , y , tp , i ; + + fin >> n >> m ; + + for ( i = 1 ; i <= m ; i++ ) + + { + + fin >> x >> y >> tp ; + + if ( tp == 0 ) + + add(n+x,y) , add(n+y,x) ; + + else if ( tp == 1 ) + + add(n+x,n+y) , add(y,x) ; + + else if ( tp == 2 ) + + add(x,y) , add(n+y,n+x) ; + + else + + add(x,n+y) , add(y,n+x) ; + + } + +} + + + +void dfs(int nod) + +{ + + bt[nod] = 1 ; + + for ( auto vec : graf[nod] ) + + if ( bt[vec] == 0 ) + + dfs(vec) ; + + ord.push_back(nod); + +} + + + +void dfst(int nod) + +{ + + rng[nod] = cmp ; + + for ( auto vec : graft[nod] ) + + if ( rng[vec] == 0 ) + + dfst(vec) ; + +} + + + +int main() + +{ + + int i ; + + citire() ; + + for ( i = 1 ; i <= 2*n ; i++ ) + + if ( bt[i] == 0 ) + + dfs(i) ; + + reverse(ord.begin(),ord.end()) ; + + cmp = 1 ; + + for ( auto vv : ord ) + + if ( rng[vv] == 0 ) + + dfst(vv) , cmp++ ; + + for ( i = 1 ; i <= n ; i++ ) + + if ( rng[i] > rng[n+i] ) + + sol.push_back(i) ; + + fout << sol.size() << '\n' ; + + for ( auto vv : sol ) + + fout << vv << '\n' ; + +} diff --git a/party/2sat_implication_reduction/52455428-d5af-43e1-bd27-311288db6b67.cpp b/party/2sat_implication_reduction/52455428-d5af-43e1-bd27-311288db6b67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779abbe2a478041abf51b4dcbdcdb67c356f4aac --- /dev/null +++ b/party/2sat_implication_reduction/52455428-d5af-43e1-bd27-311288db6b67.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + +vector>lv,lv2; + +vectornoduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + +int neg(int x) { + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + +void sorttop(int nod) { + + int i; + + v[nod]=1; + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/525402da-0b54-11ec-aecc-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/525402da-0b54-11ec-aecc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..852a09dfb95160b2903b8f78bbf915595857faca --- /dev/null +++ b/party/2sat_implication_reduction/525402da-0b54-11ec-aecc-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +vector g[200010]; + +vector gt[200010]; + +int st[200010],nr,sol[200010],tip ; + +int idx[200010]; + +bool viz[200010]; + +int n,m,i,j,x,y,ok; + + + +int dominus(int x) + +{ + + if(x>n) return x-n; + + return x+n; + + + +} + +void get_muchie(int x, int y) + +{ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void faDfs(int nod) + +{ + + viz[nod]=1; + + + + for(int i=0; i>n>>m; + + ok=1; + + for(i=1; i<=m; ++i) + + { + + cin>>x>>y>>tip; + + if(!tip) get_muchie(x+n,y),get_muchie(y+n,x); + + else if(tip==1) get_muchie(x+n,y+n),get_muchie(y,x); + + else if(tip==2) get_muchie(y+n,x+n),get_muchie(x,y); + + else get_muchie(x,y+n),get_muchie(y,x+n); + + } + + + + for(i=1; i<=2*n; ++i) + + { + + if(!viz[i]) + + faDfs(i); + + } + + + + memset(viz,0,sizeof(viz)); + + for(i=nr; i>=1; --i) + + { + + if(!viz[st[i]] && !viz[dominus(st[i])]) + + { + + dfsTrav(st[i]); + + } + + } + + if(!ok) + + { + + cout<<-1; + + return 0; + + } + + + + nr=0; + + for(i=1; i<=n; ++i) + + if(sol[i]==1) nr++; + + + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("data"".in"); + +ofstream cout("data"".out"); + + + +template + +void print(a v, type t) { + + for_each(v.begin(),v.end(), [](type x) { cout << x << ' '; }); + + cout << '\n'; + +} + + + +struct nod { + + vector v1; + + vector v2; + +}; + + + +int n, m; + +nod v[2 * 207 + 1]; + +stack s; + +bitset<2*207> viz; + +vector > componente; + +int nrP; + +inline void add(const int &ind,const int &next){ + + v[ind].v1.push_back(next); + + v[next].v2.push_back(ind); + +} + +void dfs(int i){ + + viz[i] = 1; + + for(auto it = v[i].v1.begin();it!=v[i].v1.end();++it){ + + if(!viz[*it]) + + dfs(*it); + + } + + s.push(i); + +} + +int nrGr; + +void dfs_inv(int i){ + + viz[i] = 1; + + componente[nrGr].emplace_back(i); + + for(auto it = v[i].v2.begin();it!=v[i].v2.end();++it){ + + if(!viz[*it]) + + dfs_inv(*it); + + } + +} + +inline int negat(int nr){ + + return (nr<=n?nr+n:nr-n); + +} + +int main() { + + ios_base::sync_with_stdio(false); + + clock_t tStart = clock(); + + cin >> n >> m; + + int x1,x2,cod; + + for(int i=0;i> x1 >> x2 >> cod; + + if(!cod){ + + add(negat(x1),x2); + + add(negat(x2),x1); + + } else if(cod == 1){ + + add(negat(x1),negat(x2)); + + add(x2,x1); + + } else if(cod == 2) { + + add(x1,x2); + + add(negat(x2),negat(x1)); + + } else if (cod == 3){ + + add(x1,negat(x2)); + + add(x2,negat(x1)); + + } + + } + + for(int i=1;i<=2*n;++i){ + + if(!viz[i]) + + dfs(i); + + } + + viz.reset(); + + vector temp; + + componente.push_back(temp); + + while(!s.empty()){ + + if(!viz[s.top()]){ + + componente.push_back(temp); + + ++nrGr; + + dfs_inv(s.top()); + + } + + s.pop(); + + } + + + + vector sol(2*207,-1); + + + + for(int i = nrGr;i;--i){ + + if(sol[componente[i][0]] == -1){ + + for(auto it = componente[i].begin();it != componente[i].end(); ++it){ + + sol[*it] = 1; + + sol[negat(*it)] = 0; + } + + } + + } + + + + for(int i=1;i<=n;++i){ + + if(sol[i]<0){ + + sol[i] = 1; + + } + + nrP += sol[i]; + + } + + cout << nrP << '\n'; + + for (int i = 1; i <= n; ++i) { + + if (sol[i]) + + cout << i << '\n'; + + } + + cout.close(); + + printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); + +} diff --git a/party/2sat_implication_reduction/5341de51-5261-4d98-9694-c3d7ae8bd9b2.cpp b/party/2sat_implication_reduction/5341de51-5261-4d98-9694-c3d7ae8bd9b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c8e349acf9d76a96f39aec3e5dade3be902489 --- /dev/null +++ b/party/2sat_implication_reduction/5341de51-5261-4d98-9694-c3d7ae8bd9b2.cpp @@ -0,0 +1,322 @@ +#include +#include +#include + + +using namespace std; + + + +FILE *f = fopen("party.in","r"); + +FILE *g = fopen("party.out","w"); + + + +const int NMAX = 1e2; + +const int OFFSET = NMAX + 2; + + + +vector graph[2 * OFFSET + 5]; + + + +int n,m; + +bool viz[2 * OFFSET + 5]; + +int st[2 * OFFSET + 5],len; + +bool inst[2 * OFFSET + 5]; + +int ctc[2 * OFFSET + 5],last_ctc; + +int id[2 * OFFSET + 5],last_id; + +int low[2 * OFFSET + 5]; + +vector ctc_nodes[2 * OFFSET + 5]; + + + +void dfs(int nod){ + + viz[nod] = true; + + st[++len] = nod;inst[nod] = true; + + id[nod] = low[nod] = ++last_id; + + + + for(auto it:graph[nod]){ + + if(viz[it] == false){ + + dfs(it); + + } + + if(inst[it]){ + + low[nod] = min(low[nod],low[it]); + + } + + } + + + + if(low[nod] == id[nod]){ + + last_ctc++; + + while(st[len] != nod){ + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + +} + + + +int ans[2 * OFFSET + 5]; + +int ctc_ans[2 * OFFSET + 5]; + +int ctc_gr[2 * OFFSET + 5]; + + + +vector solve_clauses(){ + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + ans[i] = -1; + + if(viz[i] == false){ + + dfs(i); + + } + + } + + + + for(int i = 1;i <= last_ctc;i++){ + + ctc_ans[i] = -1; + + } + + + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + for(auto it:graph[i]){ + + if(ctc[it] != ctc[i]){ + + ctc_gr[ctc[it]]++; + + } + + } + + } + + + + vector topo; + + + + for(int i = 1;i <= last_ctc;i++){ + + if(ctc_gr[i] == 0){ + + topo.push_back(i); + + } + + } + + + + for(int i = 0;i < (int)topo.size();i++){ + + int comp = topo[i]; + + int val = ctc_ans[comp]; + + + + for(auto it:ctc_nodes[comp]){ + + val = max(val,ans[it]); + + } + + + + val = max(val,0); + + + + ctc_ans[comp] = val; + + + + for(auto it:ctc_nodes[comp]){ + + ans[it] = val; + + ans[2 * OFFSET - it] = 1- val; + + for(auto it2:graph[it]){ + + if(ctc[it2] != ctc[it]){ + + ctc_gr[ctc[it2]]--; + + if(ctc_gr[ctc[it2]] == 0){ + + topo.push_back(ctc[it2]); + + } + + if(val == 1){ + + ctc_ans[ctc[it2]] = 1; + + } + + } + + } + + } + + } + + + + vector cnt; + + + + for(int i = OFFSET + 1;i <= OFFSET + n;i++){ + + if(ans[i]){ + + cnt.push_back(i - OFFSET); + + } + + } + + + + return cnt; + +} + + + +int main(){ + + + + fscanf(f,"%d %d",&n,&m); + + + + for(int i = 1;i <= m;i++){ + + int x,y,z; + + fscanf(f,"%d %d %d",&x,&y,&z); + + if(z == 0){ + + graph[OFFSET - x].push_back(OFFSET + y); + + graph[OFFSET - y].push_back(OFFSET + x); + + } + + else if(z == 1){ + + graph[OFFSET - x].push_back(OFFSET - y); + + } + + else if(z == 2){ + + graph[OFFSET - y].push_back(OFFSET - x); + + } + + else{ + + graph[OFFSET + x].push_back(OFFSET - y); + + graph[OFFSET + y].push_back(OFFSET - x); + + } + + } + + + + vector tmp = solve_clauses(); + + + + fprintf(g,"%d\n",(int)tmp.size()); + + + + for(auto it:tmp){ + + fprintf(g,"%d\n",it); + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/5422b27f-02b2-11ec-a4bb-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5422b27f-02b2-11ec-a4bb-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..51bc6b760a874b6d142e9d42951d9ccf2629bf5e --- /dev/null +++ b/party/2sat_implication_reduction/5422b27f-02b2-11ec-a4bb-2cf05d84bfc2.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,ID,nr; + +stack s; + +struct Nod + +{ + + int id,low; + + int rez; + + bool onstack; + +}nod[202]; + +vector v[202]; + + + + + +void dfs(int i) + +{ + + nod[i].id = nod[i].low=ID++; + + nod[i].onstack=true; + + s.push(i); + + for(vector::iterator it=v[i].begin();it!=v[i].end();it++) + + { + + if(nod[*it].id==-1) + + dfs(*it); + + if(nod[*it].onstack) + + nod[i].low=min(nod[i].low,nod[*it].low); + + } + + if(nod[i].id==nod[i].low) + + { + + int node; + + do + + { + + node=s.top(); + + s.pop(); + + nod[i].onstack=false; + + if(nod[node].rez==-1) + + { + + nod[node].rez=1; + + nod[node^1].rez=0; + + nr+= !(node%2); + + } + + }while(i!=node); + + } + +} + + + + + +int main() + +{ + + f>>n>>m; + + int x,y,c; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y>>c; + + switch(c) + + { + + case 0: + + v[x*2+1].push_back(y*2); + + v[y*2+1].push_back(x*2); + + break; + + case 1: + + v[x*2+1].push_back(y*2+1); + + v[y*2].push_back(x*2); + + break; + + case 2: + + v[x*2].push_back(y*2); + + v[y*2+1].push_back(x*2+1); + + break; + + case 3: + + v[x*2].push_back(y*2+1); + + v[y*2].push_back(x*2+1); + + } + + } + + for(int i=2;i<=n*2+1;i++) + + nod[i].id=nod[i].rez=-1; + + for(int i=2;i<=n*2+1;i++) + + if(nod[i].id==-1 && nod[i].rez==-1) + + dfs(i); + + g< +#include +#include +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +vector g[200010]; + +vector gt[200010]; + +int st[200010],nr,sol[200010],tip ; + +int idx[200010]; + +bool viz[200010]; + +int n,m,i,j,x,y,ok; + + + +int negat(int x) + +{ + + if(x>n) return x-n; + + return x+n; + + + +} + +void muchie(int x, int y) + +{ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + + + for(int i=0; i>n>>m; + + ok=1; + + for(i=1; i<=m; ++i) + + { + + cin>>x>>y>>tip; + + if(!tip) muchie(x+n,y),muchie(y+n,x); + + else if(tip==1) muchie(x+n,y+n),muchie(y,x); + + else if(tip==2) muchie(y+n,x+n),muchie(x,y); + + else muchie(x,y+n),muchie(y,x+n); + + } + + + + for(i=1; i<=2*n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + + + + memset(viz,0,sizeof(viz)); + + for(i=nr; i>=1; --i) + + { + + if(!viz[st[i]] && !viz[negat(st[i])]) + + { + + dfs2(st[i]); + + } + + } + + if(!ok) + + { + + cout<<-1; + + return 0; + + } + + + + nr=0; + + for(i=1; i<=n; ++i) + + if(sol[i]==1) nr++; + + + + cout< +#include +using namespace std; + +fstream f1("party.in", ios::in); + +fstream f2("party.out", ios::out); + +int n, m, viz[205], l[205], nrl, sol[205], ctc[205], nrcomp, nrsol; + +vector v[205], vr[205], comp[205]; + +int neg(int nod) + +{ + + if(nod<=n) return n+nod; + + else return nod-n; + +} + +void mu(int x, int y) + +{ + + v[x].push_back(y); + + vr[y].push_back(x); + +} + +void citire() + +{ + + int i, x, y, cer; + + f1>>n>>m; + + for(i=1; i<=m; i++) + + { + + f1>>x>>y>>cer; + + switch(cer) + + { + + case 0: {mu(neg(x), y); mu(neg(y), x);break;} + + case 1: {mu(neg(x), neg(y)); mu(y, x);break;} + + case 2: {mu(neg(y), neg(x)); mu(x, y);break;} + + case 3: {mu(x, neg(y)); mu(y, neg(x));break;} + + } + + } + +} + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto it=v[nod].begin(); it!=v[nod].end(); ++it) + + if(!viz[*it]) + + dfs(*it); + + nrl++; l[nrl]=nod; + +} + +void dfs2(int nod) + +{ + + ctc[nod]=nrcomp; + + comp[nrcomp].push_back(nod); + + for(auto it=vr[nod].begin(); it!=vr[nod].end(); ++it) + + if(!ctc[*it]) + + dfs2(*it); + +} + +void kos() + +{ + + for(int i=1; i<=2*n; i++) + + if(!viz[i]) + + dfs(i); + + for(int i=nrl; i>=1; i--) + + if(!ctc[l[i]]) + + { + + nrcomp++; + + dfs2(l[i]); + + } + +} + +void solutie() + +{ + + int i; + + for(i=1; i<=2*n; i++) sol[i]=-1; + for(i=nrcomp; i>=1; i--) + + if(sol[comp[i][0]]==-1) + + { + + for(auto it=comp[i].begin(); it!=comp[i].end(); ++it) + + { + + sol[*it]=1; + + sol[neg(*it)]=0; + + } + + } + + for(i=1; i<=n; i++) + + { + + if(sol[i]==-1) sol[i]=1; + + nrsol+=sol[i]; + + } + + f2< +#include + + +using namespace std; + +ifstream fin("party.in"); +ofstream fout("party.out"); + +int N, M, i, j, A, B, neg[205], CVAL[205]; +int comp[205], SortTOP[205], K, grad[205]; +int top, st[205], op, NR, CNEG[205]; +bool ok[205]; +vector G[205], Ginv[205], CG[205], SOL; +vector :: iterator it; + +void VISIT(int nod) +{ + vector :: iterator it; + ok[nod]=true; + for (it=G[nod].begin(); it!=G[nod].end(); it++) + if (ok[*it]==false) + VISIT(*it); + st[++top]=nod; +} + +void ASSIGN_NODE(int nod) +{ + vector :: iterator it; + comp[nod]=K; + for (it=Ginv[nod].begin(); it!=Ginv[nod].end(); it++) + if (comp[*it]==0) + ASSIGN_NODE(*it); +} + +int main() +{ + fin >> N >> M; + for (i=1; i<=2*N; i++) + { + if (i<=N) + neg[i]=i+N; + else + neg[i]=i-N; + } + for (i=1; i<=M; i++) + { + fin >> A >> B >> op; + if (op>1) + A=neg[A]; + if (op % 2==1) + B=neg[B]; + G[neg[A]].push_back(B); + G[neg[B]].push_back(A); + Ginv[A].push_back(neg[B]); + Ginv[B].push_back(neg[A]); + } + for (i=1; i<=2*N; i++) + if (ok[i]==false) + VISIT(i); + while (top>0) + { + if (comp[st[top]]==0) + { + K++; + ASSIGN_NODE(st[top]); + } + top--; + } + for (i=1; i<=2*N; i++) + { + CNEG[comp[i]]=comp[neg[i]]; + for (it=G[i].begin(); it!=G[i].end(); it++) + { + if (comp[i]!=comp[*it]) + { + CG[comp[i]].push_back(comp[*it]); + grad[comp[*it]]++; + } + } + } + for (i=1; i<=K; i++) + if (grad[i]==0) + SortTOP[++NR]=i; + for (i=1; i<=K; i++) + { + A=SortTOP[i]; + CVAL[i]=-1; + for (it=CG[A].begin(); it!=CG[A].end(); it++) + { + grad[*it]--; + if (grad[*it]==0) + SortTOP[++NR]=*it; + } + } + for (i=1; i<=K; i++) + { + if (CVAL[i]==-1) + { + CVAL[i]=0; + CVAL[CNEG[i]]=1; + } + } + for (i=1; i<=N; i++) + if (CVAL[comp[i]]==1) + SOL.push_back(i); + fout << SOL.size() << '\n'; + for (it=SOL.begin(); it!=SOL.end(); it++) + fout << *it << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/54ae50bd-02b2-11ec-b383-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/54ae50bd-02b2-11ec-b383-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66f4542cc84544ff201c292b6a591b07552d4fba --- /dev/null +++ b/party/2sat_implication_reduction/54ae50bd-02b2-11ec-b383-2cf05d84bfc2.cpp @@ -0,0 +1,177 @@ +#include +using namespace std; + +typedef long long LL; + +typedef pair pii; +const int MAXN = 110; + +template class jmenvect : public vector { +public: + T& operator[](int n) { + if (n > 0) + return this->at(n); + else + return this->at(-n + (this->size() >> 1)); + } +}; + +class graph { +private: + vector< list > adiac; + vector< list > neg_adiac; +public: + graph(); + graph(int); + int size(); + void newnod(); + void newedge(int, int); + const list& getList(int); + void rev_DFS(int, jmenvect&, jmenvect&); + bool SCC_DFS(int, int, jmenvect&); +}; + +graph::graph() { + adiac.reserve(MAXN); + neg_adiac.reserve(MAXN); +} + +graph::graph(int n) { + adiac.resize(n + 1); + neg_adiac.resize(n + 1); +} + +int graph::size() { + return adiac.size(); +} + +void graph::newnod() { + neg_adiac.resize(size() + 1); + adiac.resize(size() + 1); +} + +void graph::newedge(int n1, int n2) { + if (n1 > 0) + adiac[n1].push_back(n2); + else + neg_adiac[-n1].push_back(n2); +} + +const list& graph::getList(int n) { + if (n > 0) + return adiac[n]; + else + return neg_adiac[-n]; +} + +void graph::rev_DFS(int nod, jmenvect& visited, jmenvect &finished) { + auto L = this->getList(-nod); + visited[nod] = 1; + for (auto i = L.begin(); i != L.end(); ++i) + if (!visited[-(*i)]) + rev_DFS(-(*i), visited, finished); + finished.push_back(nod); +} + +bool graph::SCC_DFS(int curComp, int nod, jmenvect& visited) { + visited[nod] = curComp; + auto L = this->getList(nod); + for (auto i = L.begin(); i != L.end(); ++i) { + if (!visited[*i]) { + if (!SCC_DFS(curComp, *i, visited)) + return false; + } + } + if (visited[-nod]) + return false; + visited[-nod] = -curComp; + return true; +} + +pair, int> korasaju(graph* G) { + int N = G->size() - 1; + jmenvect visisted; + visisted.resize((N << 1) + 1); + jmenvect finished; + finished.reserve((N << 1) + 1); + + for (int i = 1; i <= N; i++) { + if (!visisted[i]) + G->rev_DFS(i, visisted, finished); + if (!visisted[-i]) + G->rev_DFS(-i, visisted, finished); + } + + visisted.assign((N << 1) + 1, 0); + int curComp = 0; + for (auto i = finished.rbegin(); i != finished.rend() - 1; ++i) { + curComp++; + if (!visisted[*i]) { + if (!G->SCC_DFS(curComp, *i, visisted)) { + jmenvect dummyvect; + return{ dummyvect, 0 }; + } + } + else + curComp--; + } + return{ visisted, curComp }; +} + + + +int main() { + + assert(freopen("fis.in", "r", stdin)); + + assert(freopen("fis.out", "w", stdout)); + int N, M; + scanf("%d%d", &N, &M); + graph* G = new graph(N); + for (int i = 0; i < M; i++) { + int tip, a, b; + scanf("%d%d%d", &a, &b, &tip); + switch (tip) { + case 0: + break; + case 1: + b = -b; + break; + case 2: + a = -a; + break; + default: + a = -a; + b = -b; + break; + } + G->newedge(-a, b); + G->newedge(-b, a); + } + pair, int> p = korasaju(G); + jmenvect& SCC = p.first; + if (!SCC.size()) { + printf("-1\n"); + return 0; + } + int nrComp = p.second; + jmenvect cval; + cval.resize((nrComp << 1) + 1); + for (int i = 1; i <= nrComp; i++) { + cval[-i] = 0; + cval[i] = 1; + } + vector sol; + for (int i = 1; i <= N; i++) { + if (cval[SCC[i]] == 1) + sol.push_back(i); + } + printf("%d\n", (int)sol.size()); + + for (const auto &it : sol) + + printf("%d\n", it); + + return 0; + +} diff --git a/party/2sat_implication_reduction/550b64e1-02b2-11ec-b971-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/550b64e1-02b2-11ec-b971-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a868f64315f03bdb000e841f7b094b0d8635851 --- /dev/null +++ b/party/2sat_implication_reduction/550b64e1-02b2-11ec-b971-2cf05d84bfc2.cpp @@ -0,0 +1,138 @@ +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int n, nr; + +stack st; + +vector G[205], GT[205]; + +bool viz[205], stare[205]; + + + +inline int negat(int first) { + + if(first<=n) return first+n; + + return first-n; + +} + + + +void DFSp(int first) { + + viz[first] = 1; + + + + for(auto it:G[first]) + + if(!viz[it]) DFSp(it); + + st.push(first); + +} + + + +void DFSm(int first) { + + viz[first] = 0; + + + + if(first > n) { + + ++nr; + + stare[negat(first)] = 1; + + } + + + + for(auto it:GT[first]) + + if(viz[it]) DFSm(it); + +} + + + +int main() { + + int m,i,first,second,t; + + + + fin>>n>>m; + + for(i=1;i<=m;++i) { + + fin>>first>>second>>t; + + + + if(t == 1) second = negat(second); + + else if(t == 2) first = negat(first); + + else if(t == 3) first = negat(first), second = negat(second); + + + + G[negat(first)].push_back(second); + + G[negat(second)].push_back(first); + + + + GT[first].push_back(negat(second)); + + GT[second].push_back(negat(first)); + + } + + + + for(i=1;i<=2*n;++i) + + if(!viz[i]) + + DFSp(i); + + + + while(!st.empty()) { + + if(viz[st.top()] && viz[negat(st.top())]) + + DFSm(st.top()); + + st.pop(); + + } + + + + fout << nr << '\n'; + + for(i=1;i<=2*n;++i) + + if(stare[i]) fout << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/552a5e99-02b2-11ec-95b7-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/552a5e99-02b2-11ec-95b7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc517c2d82343d75f91cd16f55521fde7b8d8cea --- /dev/null +++ b/party/2sat_implication_reduction/552a5e99-02b2-11ec-95b7-2cf05d84bfc2.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + +ifstream fin("party.in"); +ofstream fout("party.out"); + +int N, M, i, j, A, B, neg[205], CVAL[205]; +int comp[205], SortTOP[205], K, grad[205]; +int top, st[205], op, NR, CNEG[205]; +bool ok[205]; +vector G[205], Ginv[205], CG[205], SOL; +vector :: iterator it; + +void VISIT(int nod) +{ + vector :: iterator it; + ok[nod]=true; + for (it=G[nod].begin(); it!=G[nod].end(); it++) + if (ok[*it]==false) + VISIT(*it); + st[++top]=nod; +} + +void ASSIGN_NODE(int nod) +{ + vector :: iterator it; + comp[nod]=K; + for (it=Ginv[nod].begin(); it!=Ginv[nod].end(); it++) + if (comp[*it]==0) + ASSIGN_NODE(*it); +} + +int main() +{ + fin >> N >> M; + for (i=1; i<=2*N; i++) + { + if (i<=N) + neg[i]=i+N; + else + neg[i]=i-N; + } + for (i=1; i<=M; i++) + { + fin >> A >> B >> op; + if (op>1) + A=neg[A]; + if (op % 2==1) + B=neg[B]; + G[neg[A]].push_back(B); + G[neg[B]].push_back(A); + Ginv[A].push_back(neg[B]); + Ginv[B].push_back(neg[A]); + } + for (i=1; i<=2*N; i++) + if (ok[i]==false) + VISIT(i); + while (top>0) + { + if (comp[st[top]]==0) + { + K++; + ASSIGN_NODE(st[top]); + } + top--; + } + for (i=1; i<=2*N; i++) + { + CNEG[comp[i]]=comp[neg[i]]; + for (it=G[i].begin(); it!=G[i].end(); it++) + { + if (comp[i]!=comp[*it]) + { + CG[comp[i]].push_back(comp[*it]); + grad[comp[*it]]++; + } + } + } + for (i=1; i<=K; i++) + if (grad[i]==0) + SortTOP[++NR]=i; + for (i=1; i<=K; i++) + { + A=SortTOP[i]; + CVAL[i]=-1; + for (it=CG[A].begin(); it!=CG[A].end(); it++) + { + grad[*it]--; + if (grad[*it]==0) + SortTOP[++NR]=*it; + } + } + for (i=1; i<=K; i++) + { + if (CVAL[i]==-1) + { + CVAL[i]=0; + CVAL[CNEG[i]]=1; + } + } + for (i=1; i<=N; i++) + if (CVAL[comp[i]]==1) + SOL.push_back(i); + fout << SOL.size() << '\n'; + for (it=SOL.begin(); it!=SOL.end(); it++) + fout << *it << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/554a1b8e-02b2-11ec-bf20-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/554a1b8e-02b2-11ec-bf20-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc517c2d82343d75f91cd16f55521fde7b8d8cea --- /dev/null +++ b/party/2sat_implication_reduction/554a1b8e-02b2-11ec-bf20-2cf05d84bfc2.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + +ifstream fin("party.in"); +ofstream fout("party.out"); + +int N, M, i, j, A, B, neg[205], CVAL[205]; +int comp[205], SortTOP[205], K, grad[205]; +int top, st[205], op, NR, CNEG[205]; +bool ok[205]; +vector G[205], Ginv[205], CG[205], SOL; +vector :: iterator it; + +void VISIT(int nod) +{ + vector :: iterator it; + ok[nod]=true; + for (it=G[nod].begin(); it!=G[nod].end(); it++) + if (ok[*it]==false) + VISIT(*it); + st[++top]=nod; +} + +void ASSIGN_NODE(int nod) +{ + vector :: iterator it; + comp[nod]=K; + for (it=Ginv[nod].begin(); it!=Ginv[nod].end(); it++) + if (comp[*it]==0) + ASSIGN_NODE(*it); +} + +int main() +{ + fin >> N >> M; + for (i=1; i<=2*N; i++) + { + if (i<=N) + neg[i]=i+N; + else + neg[i]=i-N; + } + for (i=1; i<=M; i++) + { + fin >> A >> B >> op; + if (op>1) + A=neg[A]; + if (op % 2==1) + B=neg[B]; + G[neg[A]].push_back(B); + G[neg[B]].push_back(A); + Ginv[A].push_back(neg[B]); + Ginv[B].push_back(neg[A]); + } + for (i=1; i<=2*N; i++) + if (ok[i]==false) + VISIT(i); + while (top>0) + { + if (comp[st[top]]==0) + { + K++; + ASSIGN_NODE(st[top]); + } + top--; + } + for (i=1; i<=2*N; i++) + { + CNEG[comp[i]]=comp[neg[i]]; + for (it=G[i].begin(); it!=G[i].end(); it++) + { + if (comp[i]!=comp[*it]) + { + CG[comp[i]].push_back(comp[*it]); + grad[comp[*it]]++; + } + } + } + for (i=1; i<=K; i++) + if (grad[i]==0) + SortTOP[++NR]=i; + for (i=1; i<=K; i++) + { + A=SortTOP[i]; + CVAL[i]=-1; + for (it=CG[A].begin(); it!=CG[A].end(); it++) + { + grad[*it]--; + if (grad[*it]==0) + SortTOP[++NR]=*it; + } + } + for (i=1; i<=K; i++) + { + if (CVAL[i]==-1) + { + CVAL[i]=0; + CVAL[CNEG[i]]=1; + } + } + for (i=1; i<=N; i++) + if (CVAL[comp[i]]==1) + SOL.push_back(i); + fout << SOL.size() << '\n'; + for (it=SOL.begin(); it!=SOL.end(); it++) + fout << *it << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/556a26bb-02b2-11ec-8430-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/556a26bb-02b2-11ec-8430-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c53564686c2c83a968934828e616d53acb1cffd --- /dev/null +++ b/party/2sat_implication_reduction/556a26bb-02b2-11ec-8430-2cf05d84bfc2.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +vector A[205],AT[205]; + +int n,m,i,x,y,z,rez,R[205],Viz[205],top,S[205]; + + + +void dfs(int nod) + +{ + + vector::iterator it; + + Viz[nod]=1; + + for(it=A[nod].begin(); it!=A[nod].end(); it++) + + if(!Viz[*it]) + + dfs(*it); + + S[++top]=nod; + +} + + + +void dfs(int nod, int val) + +{ + + R[nod]=val; + + vector::iterator it; + + for(it=AT[nod].begin(); it!=AT[nod].end(); it++) + + if(R[*it]==-1) + + dfs(*it,val); + +} + + + +int main() + +{ + + fi>>n>>m; + + for(i=1; i<=m; i++) + + { + + fi>>x>>y>>z; + + x=2*x; + + y=2*y; + + if(z==1 || z==3) + + y^=1; + + if(z==2 || z==3) + + x^=1; + + A[x^1].push_back(y); + + A[y^1].push_back(x); + + AT[x].push_back(y^1); + + AT[y].push_back(x^1); + + } + + for(i=2; i<=2*n+1; i++) + + if(!Viz[i]) + + dfs(i); + + for(i=2; i<=2*n+1; i++) + + R[i]=-1; + + for(i=2*n; i>=1; i--) + + if(R[S[i]]==-1 && R[S[i]^1]==-1) + + dfs(S[i],0); + + for(i=2; i<=2*n; i+=2) + + if(R[i]==-1) + + { + + R[i]=1; + + rez++; + + } + + fo< +using namespace std; + +typedef long long LL; + +typedef pair pii; +const int MAXN = 110; + +template class jmenvect : public vector { +public: + T& operator[](int n) { + if (n > 0) + return this->at(n); + else + return this->at(-n + (this->size() >> 1)); + } +}; + +class graph { +private: + vector< list > adiac; + vector< list > neg_adiac; +public: + graph(); + graph(int); + int size(); + void newnod(); + void newedge(int, int); + const list& getList(int); + void rev_DFS(int, jmenvect&, jmenvect&); + bool SCC_DFS(int, int, jmenvect&); +}; + +graph::graph() { + adiac.reserve(MAXN); + neg_adiac.reserve(MAXN); +} + +graph::graph(int n) { + adiac.resize(n + 1); + neg_adiac.resize(n + 1); +} + +int graph::size() { + return adiac.size(); +} + +void graph::newnod() { + neg_adiac.resize(size() + 1); + adiac.resize(size() + 1); +} + +void graph::newedge(int n1, int n2) { + if (n1 > 0) + adiac[n1].push_back(n2); + else + neg_adiac[-n1].push_back(n2); +} + +const list& graph::getList(int n) { + if (n > 0) + return adiac[n]; + else + return neg_adiac[-n]; +} + +void graph::rev_DFS(int nod, jmenvect& visited, jmenvect &finished) { + auto L = this->getList(-nod); + visited[nod] = 1; + for (auto i = L.begin(); i != L.end(); ++i) + if (!visited[-(*i)]) + rev_DFS(-(*i), visited, finished); + finished.push_back(nod); +} + +bool graph::SCC_DFS(int curComp, int nod, jmenvect& visited) { + visited[nod] = curComp; + auto L = this->getList(nod); + for (auto i = L.begin(); i != L.end(); ++i) { + if (!visited[*i]) { + if (!SCC_DFS(curComp, *i, visited)) + return false; + } + } + if (visited[-nod]) + return false; + visited[-nod] = -curComp; + return true; +} + +pair, int> korasaju(graph* G) { + int N = G->size() - 1; + jmenvect visisted; + visisted.resize((N << 1) + 1); + jmenvect finished; + finished.reserve((N << 1) + 1); + + for (int i = 1; i <= N; i++) { + if (!visisted[i]) + G->rev_DFS(i, visisted, finished); + if (!visisted[-i]) + G->rev_DFS(-i, visisted, finished); + } + + visisted.assign((N << 1) + 1, 0); + int curComp = 0; + for (auto i = finished.rbegin(); i != finished.rend() - 1; ++i) { + curComp++; + if (!visisted[*i]) { + if (!G->SCC_DFS(curComp, *i, visisted)) { + jmenvect dummyvect; + return{ dummyvect, 0 }; + } + } + else + curComp--; + } + return{ visisted, curComp }; +} + + + +int main() { + + assert(freopen("fis.in", "r", stdin)); + + assert(freopen("fis.out", "w", stdout)); + int N, M; + scanf("%d%d", &N, &M); + graph* G = new graph(N); + for (int i = 0; i < M; i++) { + int tip, a, b; + scanf("%d%d%d", &a, &b, &tip); + switch (tip) { + case 0: + break; + case 1: + b = -b; + break; + case 2: + a = -a; + break; + default: + a = -a; + b = -b; + break; + } + G->newedge(-a, b); + G->newedge(-b, a); + } + pair, int> p = korasaju(G); + jmenvect& SCC = p.first; + if (!SCC.size()) { + printf("-1\n"); + return 0; + } + int nrComp = p.second; + jmenvect cval; + cval.resize((nrComp << 1) + 1); + for (int i = 1; i <= nrComp; i++) { + cval[-i] = 0; + cval[i] = 1; + } + vector sol; + for (int i = 1; i <= N; i++) { + if (cval[SCC[i]] == 1) + sol.push_back(i); + } + printf("%d\n", (int)sol.size()); + + for (const auto &it : sol) + + printf("%d\n", it); + + return 0; + +} diff --git a/party/2sat_implication_reduction/56372812-02b2-11ec-b169-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/56372812-02b2-11ec-b169-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c03e675a674d96da31c38d103905d1c7bb65cb0 --- /dev/null +++ b/party/2sat_implication_reduction/56372812-02b2-11ec-b169-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,x,y,tip,cur=1,viz[203]; + +vector Sol,O,G[203],Q[203]; + +void add(int x,int y) + +{ + + G[x].push_back(y); + + Q[y].push_back(x); + +} + +void dfs(int x) + +{ + + viz[x]=1; + + for(int i=0;i<(int)G[x].size();++i) + + if(!viz[G[x][i]]) dfs(G[x][i]); + + O.push_back(x); + +} + +void DFS(int x) + +{ + + viz[x]=cur; + + for(int i=0;i<(int)Q[x].size();++i) + + if(viz[Q[x][i]]==1) DFS(Q[x][i]); + +} + +int main() + +{ + + f>>n>>m; + + while(m--) + + { + + f>>x>>y>>tip; + + if(!tip) add(x+n,y),add(y+n,x); + + else if(tip==1) add(x+n,y+n),add(y,x); + + else if(tip==2) add(y+n,x+n),add(x,y); + + else add(x,y+n),add(y,x+n); + + } + + for(int i=1;i<=2*n;++i) + + if(!viz[i]) dfs(i); + + for(int i=O.size()-1;i>=0;--i) + + if(viz[O[i]]==1) + + { + + cur++; + + DFS(O[i]); + + } + + for(int i=1;i<=n;++i) + + if(viz[i]>viz[i+n]) Sol.push_back(i); + + g< +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void Dfs1 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, stack < int > &stk) { + + used[cur_node] = true; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs1 (x, gr, used, stk); + } + + stk.push (cur_node); +} + +void Dfs2 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, vector < int > &ctc, int &cur_ctc) { + + used[cur_node] = true; + ctc[cur_node] = cur_ctc; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs2 (x, gr, used, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > used (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (used[i]) { + continue; + } + + Dfs1 (i, gr, used, stk); + } + + fill (used.begin (), used.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (used[cur]) { + continue; + } + + ++ nr_of_ctc; + Dfs2 (cur, inv, used, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/567ddadf-9d0b-4ec0-a785-f0b821521e51.cpp b/party/2sat_implication_reduction/567ddadf-9d0b-4ec0-a785-f0b821521e51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c11305dc56850c9a785e23a9aa559e7e48e6c127 --- /dev/null +++ b/party/2sat_implication_reduction/567ddadf-9d0b-4ec0-a785-f0b821521e51.cpp @@ -0,0 +1,195 @@ +#include +#include + + +using namespace std; + + + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + + + +vector G[210], T[210]; + + + +int non ( int x ) + +{ + + if( x <= n ) return x+n; + + return x - n ; + +} + + + +void sortaret ( int nod ) + +{ + + int N = G[nod].size(),i; + + + + viz[nod] = 1 ; + + + + for( i = 0 ; i < N ; i++ ) + + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + + + S[--nn] = nod ; + +} + + + +void dfs ( int nod ) + +{ + + int N = T[nod].size(),i; + + + + viz[nod] = 0 ; + + + + sol[ non(nod) ] = 1 ; + + if( nod > n ) invitati++; + + + + for( i = 0 ; i < N ; i++ ) + + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + + + for( i = 1 ; i <= m ; i++ ) + + { + + scanf("%d %d %d",&a,&b,&tip); + + + + if( tip == 0 ) + + { + + G[non(a)].push_back(b); + + G[non(b)].push_back(a); + + T[a].push_back(non(b)); + + T[b].push_back(non(a)); + + } + + else if( tip == 1 ) + + { + + G[non(a)].push_back(non(b)); + + G[b].push_back(a); + + + + T[non(b)].push_back(non(a)); + + T[a].push_back(b); + + } + + else if( tip == 2 ) + + { + + G[non(b)].push_back(non(a)); + + G[a].push_back(b); + + + + T[non(a)].push_back(non(b)); + + T[b].push_back(a); + + } + + else + + { + + G[a].push_back(non(b)); + + G[b].push_back(non(a)); + + T[non(a)].push_back(b); + + T[non(b)].push_back(a); + + } + + } + + + + nn = n + n + 1 ; + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( !viz[i]) sortaret(i); + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + + dfs( S[i] ) ; + + + + printf("%d\n",invitati); + + + + for( i = 1 ; i <= n ; i++ ) + + if( sol[i] ) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/569f1753-6603-444b-a60c-6ce06acaee17.cpp b/party/2sat_implication_reduction/569f1753-6603-444b-a60c-6ce06acaee17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c8e349acf9d76a96f39aec3e5dade3be902489 --- /dev/null +++ b/party/2sat_implication_reduction/569f1753-6603-444b-a60c-6ce06acaee17.cpp @@ -0,0 +1,322 @@ +#include +#include +#include + + +using namespace std; + + + +FILE *f = fopen("party.in","r"); + +FILE *g = fopen("party.out","w"); + + + +const int NMAX = 1e2; + +const int OFFSET = NMAX + 2; + + + +vector graph[2 * OFFSET + 5]; + + + +int n,m; + +bool viz[2 * OFFSET + 5]; + +int st[2 * OFFSET + 5],len; + +bool inst[2 * OFFSET + 5]; + +int ctc[2 * OFFSET + 5],last_ctc; + +int id[2 * OFFSET + 5],last_id; + +int low[2 * OFFSET + 5]; + +vector ctc_nodes[2 * OFFSET + 5]; + + + +void dfs(int nod){ + + viz[nod] = true; + + st[++len] = nod;inst[nod] = true; + + id[nod] = low[nod] = ++last_id; + + + + for(auto it:graph[nod]){ + + if(viz[it] == false){ + + dfs(it); + + } + + if(inst[it]){ + + low[nod] = min(low[nod],low[it]); + + } + + } + + + + if(low[nod] == id[nod]){ + + last_ctc++; + + while(st[len] != nod){ + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + +} + + + +int ans[2 * OFFSET + 5]; + +int ctc_ans[2 * OFFSET + 5]; + +int ctc_gr[2 * OFFSET + 5]; + + + +vector solve_clauses(){ + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + ans[i] = -1; + + if(viz[i] == false){ + + dfs(i); + + } + + } + + + + for(int i = 1;i <= last_ctc;i++){ + + ctc_ans[i] = -1; + + } + + + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + for(auto it:graph[i]){ + + if(ctc[it] != ctc[i]){ + + ctc_gr[ctc[it]]++; + + } + + } + + } + + + + vector topo; + + + + for(int i = 1;i <= last_ctc;i++){ + + if(ctc_gr[i] == 0){ + + topo.push_back(i); + + } + + } + + + + for(int i = 0;i < (int)topo.size();i++){ + + int comp = topo[i]; + + int val = ctc_ans[comp]; + + + + for(auto it:ctc_nodes[comp]){ + + val = max(val,ans[it]); + + } + + + + val = max(val,0); + + + + ctc_ans[comp] = val; + + + + for(auto it:ctc_nodes[comp]){ + + ans[it] = val; + + ans[2 * OFFSET - it] = 1- val; + + for(auto it2:graph[it]){ + + if(ctc[it2] != ctc[it]){ + + ctc_gr[ctc[it2]]--; + + if(ctc_gr[ctc[it2]] == 0){ + + topo.push_back(ctc[it2]); + + } + + if(val == 1){ + + ctc_ans[ctc[it2]] = 1; + + } + + } + + } + + } + + } + + + + vector cnt; + + + + for(int i = OFFSET + 1;i <= OFFSET + n;i++){ + + if(ans[i]){ + + cnt.push_back(i - OFFSET); + + } + + } + + + + return cnt; + +} + + + +int main(){ + + + + fscanf(f,"%d %d",&n,&m); + + + + for(int i = 1;i <= m;i++){ + + int x,y,z; + + fscanf(f,"%d %d %d",&x,&y,&z); + + if(z == 0){ + + graph[OFFSET - x].push_back(OFFSET + y); + + graph[OFFSET - y].push_back(OFFSET + x); + + } + + else if(z == 1){ + + graph[OFFSET - x].push_back(OFFSET - y); + + } + + else if(z == 2){ + + graph[OFFSET - y].push_back(OFFSET - x); + + } + + else{ + + graph[OFFSET + x].push_back(OFFSET - y); + + graph[OFFSET + y].push_back(OFFSET - x); + + } + + } + + + + vector tmp = solve_clauses(); + + + + fprintf(g,"%d\n",(int)tmp.size()); + + + + for(auto it:tmp){ + + fprintf(g,"%d\n",it); + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/56b50488-02b2-11ec-a895-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/56b50488-02b2-11ec-a895-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd8c990edeac5b460377988d7bf56f2cd4ccc98e --- /dev/null +++ b/party/2sat_implication_reduction/56b50488-02b2-11ec-a895-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +vector g[200010]; + +vector gt[200010]; + +int st[200010],nr,sol[200010],tip ; + +int idx[200010]; + +bool viz[200010]; + +int n,m,i,j,x,y,ok; + + + +int negat(int x) + +{ + + if(x>n) return x-n; + + return x+n; + + + +} + +void muchie(int x, int y) + +{ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + + + for(int i=0; i>n>>m; + + ok=1; + + for(i=1; i<=m; ++i) + + { + + cin>>x>>y>>tip; + + if(!tip) muchie(x+n,y),muchie(y+n,x); + + else if(tip==1) muchie(x+n,y+n),muchie(y,x); + + else if(tip==2) muchie(y+n,x+n),muchie(x,y); + + else muchie(x,y+n),muchie(y,x+n); + + } + + + + for(i=1; i<=2*n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + + + + memset(viz,0,sizeof(viz)); + + for(i=nr; i>=1; --i) + + { + + if(!viz[st[i]] && !viz[negat(st[i])]) + + { + + dfs2(st[i]); + + } + + } + + if(!ok) + + { + + cout<<-1; + + return 0; + + } + + + + nr=0; + + for(i=1; i<=n; ++i) + + if(sol[i]==1) nr++; + + + + cout< +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void Dfs1 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, stack < int > &stk) { + + used[cur_node] = true; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs1 (x, gr, used, stk); + } + + stk.push (cur_node); +} + +void Dfs2 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, vector < int > &ctc, int &cur_ctc) { + + used[cur_node] = true; + ctc[cur_node] = cur_ctc; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs2 (x, gr, used, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > used (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (used[i]) { + continue; + } + + Dfs1 (i, gr, used, stk); + } + + fill (used.begin (), used.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (used[cur]) { + continue; + } + + ++ nr_of_ctc; + Dfs2 (cur, inv, used, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/576800cd-02b2-11ec-b426-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/576800cd-02b2-11ec-b426-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18461c0b504e9941e901580e34abcaff3df17228 --- /dev/null +++ b/party/2sat_implication_reduction/576800cd-02b2-11ec-b426-2cf05d84bfc2.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream fin ("party.in"); + +ofstream fout ("party.out"); + + + +const int Nmax = 300; + + + +int x, y, tip, n, m, i, nrc, where[Nmax]; + +vector v[Nmax], vt[Nmax], ord, come; + +bool used[Nmax]; + + + +int no(int x) {return x + n;} + + + +void add_edge(int x, int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs1(int node) + +{ + + used[node] = 1; + + for(auto go : v[node]) + + if(!used[go]) dfs1(go); + + ord.push_back(node); + +} + + + +void dfs2(int node) + +{ + + where[node] = nrc; + + for(auto go : vt[node]) + + if(!where[go]) dfs2(go); + +} + + + +int main() + +{ + + fin >> n >> m; + + while(m--) + + { + + fin >> x >> y >> tip; + + if(tip == 0) add_edge(no(x), y), add_edge(no(y), x); + + else if(tip == 1) add_edge(no(x), no(y)), add_edge(y, x); + + else if(tip == 2) add_edge(no(y), no(x)), add_edge(x, y); + + else if(tip == 3) add_edge(x, no(y)), add_edge(y, no(x)); + + } + + + + for(i=1; i<=2*n; ++i) + + if(!used[i]) dfs1(i); + + + + reverse(ord.begin(), ord.end()); + + for(auto node : ord) + + if(!where[node]) + + ++nrc, dfs2(node); + + + + for(i=1; i<=n; ++i) + + if(where[i] > where[no(i)]) come.push_back(i); + + + + fout << come.size() << '\n'; + + for(auto who : come) fout << who << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/5787bdd9-02b2-11ec-9939-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5787bdd9-02b2-11ec-9939-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3d3e7d407cfb5d294490390a7c769670512af8b --- /dev/null +++ b/party/2sat_implication_reduction/5787bdd9-02b2-11ec-9939-2cf05d84bfc2.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream cin("party.in"); +ofstream cout("party.out"); + +int n, m; + +vector > gr(200100); +vector > inv(200100); + +vector used(200100); + +vector verif; +vector now; + +vector > comp(200100); +vector COMP(200100); +int cont = 0; +vector > GR(200100); +vector enter(200100); + +queue Q; +vector ord_top; + +vector ans(200100); + +bool ret = false; + +void dfs(int root) { + + used[root] = true; + for (auto &x : gr[root]) { + if (!used[x]) { + dfs(x); + } + } + verif.push_back(root); + +} + +void DFS(int root) { + + used[root] = true; + COMP[root] = cont; + now.push_back(root); + for (auto &x : inv[root]) { + if (!used[x]) { + DFS(x); + } + } + +} + +void bfs() { + + while (!Q.empty()) { + + int Now = Q.front(); + Q.pop(); + ord_top.push_back(Now); + + for (auto &x : GR[Now]) { + enter[x]--; + if (!enter[x]) { + Q.push(x); + } + } + + } + +} + +int val(int nr) { + if (nr < 0) { + return n - nr; + } + return nr; +} + +int OPUS(int nr) { + if (nr > n) { + return nr - n; + } + return nr + n; +} + +int main() { + + ios::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + + int a, b, t; + cin >> a >> b >> t; + + int A = val(-a); + int B = val(-b); + a = val(a); + b = val(b); + + int x1, x2, y1, y2; + + if (t == 0) { + x1 = A; + x2 = B; + y1 = b; + y2 = a; + } + if (t == 1) { + x1 = b; + x2 = A; + y1 = a; + y2 = B; + } + if (t == 2) { + x1 = a; + x2 = B; + y1 = b; + y2 = A; + } + if (t == 3) { + x1 = a; + x2 = b; + y1 = B; + y2 = A; + } + + gr[x1].push_back(y1); + gr[x2].push_back(y2); + + inv[y1].push_back(x1); + inv[y2].push_back(x2); + + } + + + + for (int i = 1; i <= 2 * n; i++) { + if (!used[i]) { + dfs(i); + } + } + + for (int i = 1; i <= 2 * n; i++) { + used[i] = false; + } + + reverse(verif.begin(), verif.end()); + + for (auto &x : verif) { + if (!used[x]) { + now.clear(); + cont++; + DFS(x); + comp[cont] = now; + } + } + for (int i = n + 1; i <= n; i++) { + if (COMP[i] == COMP[i - n]) { + ret = true; + + } + } + + + + for (int i = 1; i <= cont; i++) { + for (auto &x : comp[i]) { + for (auto &y : gr[x]) { + if (COMP[y] != i) { + GR[i].push_back(COMP[y]); + enter[COMP[y]]++; + } + } + } + } + + for (int i = 1; i <= cont; i++) { + if (!enter[i]) { + Q.push(i); + } + } + + bfs(); + for (int i = 1; i <= cont; i++) { + ans[i] = -1; + } + + for (auto &i : ord_top) { + + if (ans[i] == -1) { + ans[i] = 0; + } + for (auto &x : comp[i]) { + int opus = OPUS(x); + + if (ans[COMP[opus]] == -1) { + ans[COMP[opus]] = ans[i] ^ 1; + } else { + if (ans[COMP[opus]] == ans[i]) { + ret = true; + + } + } + for (auto &y : gr[x]) { + if (COMP[y] == COMP[x]) { + continue; + } + if (ans[i] == 1) { + if (ans[COMP[y]] == 0) { + ret = true; + + } + ans[COMP[y]] = 1; + } + } + + } + + } + + int cnt = 0; + for (int i = 1; i <= n; i++) { + if (ans[COMP[i]]) { + cnt++; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; i++) { + if (ans[COMP[i]]) { + cout << i << '\n'; + } + } + + + return 0; +} diff --git a/party/2sat_implication_reduction/57a6de98-02b2-11ec-9301-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/57a6de98-02b2-11ec-9301-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76f1f4a4c729e465371e446e92c9efeeda5301c5 --- /dev/null +++ b/party/2sat_implication_reduction/57a6de98-02b2-11ec-9301-2cf05d84bfc2.cpp @@ -0,0 +1,166 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream F("party.in"); + +ofstream G("party.out"); + + + +int n, m, A, B, x, y, k, K, sol[ 1005 ], w[ 205 ], z, p, q, r, s; + +stack st; + +bool v[505]; + +vector< int > a[ 205 ]; + +vector< int > rev[ 205 ]; + + + +int Id( int x ) + +{ + + if( x <= n ) x +=n; + + else x -= n; + + return x; + +} + + + +void dfs1( int nod ) + +{ + + v[ nod ] = 1; + + vector< int > :: iterator it; + + for( it = a[ nod ].begin(); it != a[ nod ].end(); ++it ) + + if( !v[ *it ] ) dfs1( *it ); + + st.push( nod ); + +} + + + +void dfs2( int nod ) + +{ + + v[ nod ] = 0; + + w[ Id( nod ) ] = 1; + + vector< int > :: iterator it; + + for( it = rev[ nod ].begin(); it != rev[ nod ].end(); ++it ) + + if( v[ *it ] ) dfs2( *it ); + +} + + + +int main() + +{ + + F >> n >> m; + + for( int i = 1; i <= m; ++ i ) + + { + + F >> A >> B >> z; + + if( !z ) + + { + + x = A; y = B; + + } + + else if( z == 1 ) + + { + + x = A; y = Id( B ); + + } + + else if( z == 2 ) + + { + + x = B; y = Id( A ); + + } + + else + + { + + x = Id( A ); y = Id( B ); + + } + + p = Id( x ); q = x; + + r = Id( y ); s = y; + + a[ p ].push_back( s ); + + a[ r ].push_back( q ); + + rev[ s ].push_back( p ); + + rev[ q ].push_back( r ); + + } + + for( int i = 1; i <= 2 * n; ++ i ) + + if( !v[ i ] ) dfs1( i ); + + while( !st.empty() ) + + { + + x = st.top(); + + if( v[ x ] && v[ Id( x ) ] ) + + dfs2( x ); + + st.pop(); + + } + + for( int i = 1; i <= n; ++ i ) + + if( w[ i ] ) + + sol[ ++ K ] = i; + + G << K << '\n'; + + for( int i = 1; i <= K; ++ i ) G << sol[ i ] << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/58841f39-02b2-11ec-aa7d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/58841f39-02b2-11ec-aa7d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eadbef54b5fd8d5497240362ebaec1e54997c0e --- /dev/null +++ b/party/2sat_implication_reduction/58841f39-02b2-11ec-aa7d-2cf05d84bfc2.cpp @@ -0,0 +1,156 @@ +#include +using namespace std; + + + +int n, m, k, nr, NR; + +int st[2005], val[2005], d[2005], id[2005]; + +int r[2005]; + +bool f[2005]; + +vector v[2005]; + +vector ctc[2005]; + +inline int neg(int x){ + + if(x > 0) return x + n; + + else return abs(x); + +} + +inline int ret(int x){ + + if(x <= n) return x; + + else return x - n; + +} + +inline void dfs(int nod){ + + st[++NR] = nod; + + f[nod] = 1; + + d[nod] = id[nod] = ++k; + + for(auto it : v[nod]){ + + if(d[it] == 0){ + + dfs(it); + + d[nod] = min(d[nod], d[it]); + + } + + else if(f[it]) d[nod] = min(d[nod], d[it]); + + } + + if(d[nod] == id[nod]){ + + ++nr; + + while(st[NR] != nod){ + + int x = st[NR--]; + + f[x] = 0; + + ctc[nr].push_back(x); + + } + + int x = st[NR--]; + + f[x] = 0; + + ctc[nr].push_back(x); + + } + +} + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d%d", &n, &m); + + int x, y, nx, ny, t; + + for(int i = 1; i <= m ; ++i){ + + scanf("%d%d%d", &x, &y, &t); + + if(t == 1) y = -y; + + else if(t == 2) x = -x; + + else if(t == 3) x = -x, y = -y; + + nx = -x; ny = -y; + + x = neg(x); ny = neg(ny); + + nx = neg(nx); y = neg(y); + + v[nx].push_back(y); + + v[ny].push_back(x); + + } + + for(int i = 1; i <= n * 2 ; ++i){ + + if(d[i]) continue ; + + NR = 0; dfs(i); + + } + + for(int i = 1; i <= n ; ++i) val[i] = -1; + + bool ok = 1; + + int sol = 0; + + for(int i = nr; i >= 1 ; --i){ + + for(auto it : ctc[i]){ + + int x = it; + + x = ret(it); + + r[x] = i; + + if(val[x] != -1) continue ; + + if(it <= n) val[x] = 1, ++sol; + + else val[x] = 0; + + } + + } + + printf("%d\n", sol); + + for(int i = 1; i <= n ; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/58a47874-02b2-11ec-9235-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/58a47874-02b2-11ec-9235-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/58a47874-02b2-11ec-9235-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< + + +using namespace std; + + + + + + + +int N, M; + +vector edg[205], redg[205]; + +int O, ord[205]; + +int f[205]; + +int C, cmp[205]; + +vector scc[205]; + +vector sccedg[205]; + +int g[205]; + +int ans[205], v[205]; + +vector party; + + + +inline int node(int x) { return x > 0 ? x : -x + N; } + +inline int value(int x) { return x <= N ? x : -x + N; } + + + +void DFS1(int nod) + +{ + + if(f[nod]) return; + + f[nod] = 1; + + for(auto nxt: edg[nod]) + + DFS1(nxt); + + ord[++O] = nod; + +} + + + +void DFS2(int nod) + +{ + + if(f[nod] == 2) return; + + f[nod] = 2; + + cmp[nod] = C; + + scc[C].push_back(nod); + + for(auto nxt: redg[nod]) + + DFS2(nxt); + +} + + + +void solveDFS(int nod, int fth) + +{ + + if(ans[nod] == -1) + + { + + ans[nod] = fth; + + for(auto x: scc[nod]) + + { + + int rev = node( -value(x) ); + + ans[ cmp[rev] ] = 1 ^ fth; + + } + + } + + assert(!fth || (fth == ans[nod])); + + + + for(auto nxt: sccedg[nod]) + + { + + g[nxt]--; + + if(g[nxt] == 0) + + solveDFS(nxt, fth | ans[nod]); + + } + +} + + + +int main() + +{ + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + + + scanf("%d%d", &N, &M); + + for(int i = 1; i <= M; i++) + + { + + int x, y, op; + + scanf("%d%d%d", &x, &y, &op); + + if(op == 0) + + { + + edg[node(-x)].push_back(y); + + edg[node(-y)].push_back(x); + + } + + else if(op == 1) + + { + + edg[node(-x)].push_back(node(-y)); + + edg[node(y)].push_back(node(x)); + + } + + else if(op == 2) + + { + + edg[node(x)].push_back(node(y)); + + edg[node(-y)].push_back(node(-x)); + + } + + else if(op == 3) + + { + + edg[node(x)].push_back(node(-y)); + + edg[node(y)].push_back(node(-x)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) DFS1(i); + + for(int i = 1; i <= 2 * N; i++) + + for(auto x: edg[i]) + + redg[x].push_back(i); + + reverse(ord + 1, ord + 2 * N + 1); + + for(int i = 1; i <= 2 * N; i++) + + if( f[ ord[i] ] != 2 ) ++C, DFS2(ord[i]); + + + + for(int i = 1; i <= N; i++) assert(cmp[i] != cmp[i + N]); + + + + for(int i = 1; i <= C; i++) f[i] = 0; + + for(int i = 1; i <= C; i++) + + for(auto nod: scc[i]) + + for(auto nxt: edg[nod]) + + if(i != cmp[nxt]) + + if(f[ cmp[nxt] ] != i) + + sccedg[i].push_back(cmp[nxt]), g[ cmp[nxt] ]++, f[ cmp[nxt] ] = i; + + + + for(int i = 1; i <= C; i++) ans[i] = -1; + + for(int i = 1; i <= C; i++) + + if(ans[i] == -1) solveDFS(i, 0); + + + + for(int i = 1; i <= C; i++) + + for(auto nod: scc[i]) + + v[nod] = ans[i]; + + + + for(int i = 1; i <= N; i++) + + if(v[i]) + + party.push_back(i); + + + + printf("%d\n", party.size()); + + for(auto x: party) + + printf("%d\n", x); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/5972fb9e-02b2-11ec-9956-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5972fb9e-02b2-11ec-9956-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aadb8d3d3d4813fecf5cbf7bde7c3b256caddc0b --- /dev/null +++ b/party/2sat_implication_reduction/5972fb9e-02b2-11ec-9956-2cf05d84bfc2.cpp @@ -0,0 +1,194 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,sol[205],ans; + +vector v[205],p[205]; + +stack s; + +bitset <205> b; + + + + + +int cod(int x) + +{ + + if (x<0) + + return n-x; + + return x; + + + +} + +void dfs(int x) + +{ + + int i,y; + + b[x]=1; + + for (i=0;i>n>>m; + + for (;m;m--) { + + f>>i>>j>>k; + + if (k==0) { + + implic(-i,j); + + implic(-j,i); + + } + + else + + if (k==1) { + + implic(-i,-j); + + implic(j,i); + + } + + else + + if (k==2) { + + implic(-j,-i); + + implic(i,j); + + } + + else { + + implic(i,-j); + + implic(j,-i); + + } + + } + + for (i=1;i<=2*n;i++) + + if (b[i]==0) + + dfs(i); + + while (!s.empty()) { + + i=s.top(); + + s.pop(); + + if (b[i]==1&&b[opus(i)]==1) + + dfst(i); + + } + + for (i=1;i<=n;i++) + + ans+=sol[i]; + + g< +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 210; + + + +int noInvit , noCond , typeCond ; + + + +vector < int > edges [ N ]; + + + +int varValues [ N ]; + + + +stack < int > stk ; + +int inStack [ N ]; + + + +int low [ N ] , preord [ N ] , nrCr = 1 ,nrSol ; + + + +void CalculateSCC( int node ){ + + vector < int >::iterator it ; + + int vec ; + + + + stk.push( node ); + + inStack [ node ] = 1 ; + + low [ node ] = preord [ node ] = nrCr ++ ; + + + + for ( it = edges [ node ].begin() ; it != edges [ node ].end() ; it++ ){ + + vec = *it ; + + + + if ( !preord [ *it ] ){ + + + + CalculateSCC( *it ); + + low[ node ] = min( low [ node ] , low[ *it ] ); + + + + }else{ + + if ( inStack [ *it ] ){ + + low [ node ] = min ( low [ node ] , low [ *it ] ); + + } + + } + + } + + + + if ( low [ node ] == preord [ node ] ){ + + int x ; + + do { + + x = stk.top() ; + + stk.pop() ; + + inStack [ x ] = 0 ; + + + + if ( x > noInvit ){ + + if ( varValues [ x - noInvit ] == -1 ){ + + varValues [ x - noInvit ] = 0 ; + + } + + }else{ + + if ( varValues [ x ] == -1 ){ + + varValues [ x ] = 1 ; + + nrSol++; + + } + + } + + + + }while ( x != node ); + + + + } + + + +} + + + +int main(){ + + int i , x , y , xnot , ynot ; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&noInvit , &noCond ); + + + + for ( i = 0 ; i < noCond ; i++ ){ + + scanf("%d%d%d", &x , &y , &typeCond ); + + + + xnot = x + noInvit ; + + ynot = y + noInvit ; + + + + switch( typeCond ){ + + case 0 : + + edges[ xnot ].push_back( y ); + + edges[ ynot ].push_back( x ); + + break ; + + case 1 : + + edges[ xnot ].push_back( ynot ); + + edges[ y ].push_back( x ); + + break ; + + case 2 : + + edges[ x ].push_back( y ); + + edges[ ynot ].push_back( xnot ); + + break ; + + case 3 : + + edges[ x ].push_back( ynot ); + + edges[ y ].push_back( xnot ); + + break ; + + } + + } + + + + memset ( varValues , -1 , sizeof varValues ); + + + + for ( i = 1 ; i <= 2 * noInvit ; i ++ ){ + + if ( preord [ i ] == 0 ) + + CalculateSCC( i ); + + } + + + + printf("%d\n",nrSol); + + for ( i = 1 ; i <= noInvit ; i++ ){ + + if ( varValues [ i ] == 1 ){ + + printf("%d\n",i); + + } + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/59d0fa12-02b2-11ec-8de0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/59d0fa12-02b2-11ec-8de0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a35e24538442347a75084f443a86b42e396d0c62 --- /dev/null +++ b/party/2sat_implication_reduction/59d0fa12-02b2-11ec-8de0-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + +bool in_stk[205]; + +int sol[205]; + +int n,m,i,j,x,y,z; + +int stk[205],top,cntscc; + +int low[205],idx[205],curr,scc[205]; + +vector v[205],invite; + +inline int opposite(int x) + +{ + + if(x>n) + + return x-n; + + return x+n; + +} + +void tarjan(int nod) + +{ + + low[nod]=idx[nod]=++curr; + + stk[++top]=nod; + + in_stk[nod]=true; + + + + for(auto it:v[nod]) + + if(!idx[it]){ + + tarjan(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(in_stk[it]) + + low[nod]=min(low[nod],idx[it]); + + + + if(low[nod]==idx[nod]) + + { + + int newn; + + ++cntscc; + + do + + { + + newn=stk[top--]; + + in_stk[newn]=false; + + scc[newn]=cntscc; + + if(!sol[newn]) + + sol[newn]=1,sol[opposite(newn)]=-1; + + }while(nod!=newn); + + } + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for(i=1;i<=m;i++) + + { + + scanf("%d %d %d",&x,&y,&z); + + if(z==0) + + v[opposite(x)].push_back(y),v[opposite(y)].push_back(x); + + if(z==1) + + v[opposite(x)].push_back(opposite(y)),v[y].push_back(x); + + if(z==2) + + v[opposite(y)].push_back(opposite(x)),v[x].push_back(y); + + if(z==3) + + v[x].push_back(opposite(y)),v[y].push_back(opposite(x)); + + } + + for(i=1;i<=n;i++) + + if(!idx[i]) + + tarjan(i); + + for(i=1;i<=n;i++) + + if(sol[i]==1) + + invite.push_back(i); + + printf("%d\n",invite.size()); + + for(auto it:invite) + + printf("%d ",it); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5a90eeb5-02b2-11ec-a1b2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5a90eeb5-02b2-11ec-a1b2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29b5139e6fa7f5f31f8d73b52bbdac58f8dc3b3f --- /dev/null +++ b/party/2sat_implication_reduction/5a90eeb5-02b2-11ec-a1b2-2cf05d84bfc2.cpp @@ -0,0 +1,172 @@ +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +const int NMax = 202; + + + +int n,m,x,y,p,top,wrong; + +int viz[NMax],ans[NMax],s[NMax]; + +vector G[NMax],GT[NMax],v; + + + +int non(int x){ + + if(x <= n) + + return x + n; + + return x - n; + +} + +void dfs(int nod){ + + viz[nod] = 1; + + for(int i = 0; i < G[nod].size(); ++i){ + + if(!viz[G[nod][i]]){ + + dfs(G[nod][i]); + + } + + } + + s[++top] = nod; + +} + +void dfsT(int nod){ + + viz[nod] = 0; + + ans[nod] = 0; + + ans[non(nod)] = 1; + + for(int i = 0; i < GT[nod].size(); ++i){ + + if(viz[GT[nod][i]]){ + + dfsT(GT[nod][i]); + + } + + } + +} + +int main() + +{ + + f >> n >> m; + + for(int i = 1; i <= m; ++i){ + + f >> x >> y >> p; + + if(p == 0){ + + G[non(x)].push_back(y); + + G[non(y)].push_back(x); + + GT[y].push_back(non(x)); + + GT[x].push_back(non(y)); + + } + + if(p == 1){ + + G[non(x)].push_back(non(y)); + + G[y].push_back(x); + + GT[non(y)].push_back(non(x)); + + GT[x].push_back(y); + + } + + if(p == 2){ + + G[x].push_back(y); + + G[non(y)].push_back(non(x)); + + GT[y].push_back(x); + + GT[non(x)].push_back(non(y)); + + } + + if(p == 3){ + + G[x].push_back(non(y)); + + G[y].push_back(non(x)); + + GT[non(y)].push_back(x); + + GT[non(x)].push_back(y); + + } + + } + + for(int i = 1; i <= 2 * n; ++i){ + + if(!viz[i]){ + + dfs(i); + + } + + } + + for(int i = top; i >= 1; --i){ + + if(viz[s[i]] && viz[non(s[i])]){ + + dfsT(s[i]); + + } + + } + + vector v; + + for(int i = 1; i <= n; ++i){ + + if(ans[i]) + + v.push_back(i); + + } + + g << v.size() << '\n'; + + for(int i = 0; i < v.size(); ++i){ + + g << v[i] << '\n'; + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/5af0b293-02b2-11ec-a3e8-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5af0b293-02b2-11ec-a3e8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac62dc9c524e2d47b184aba1176863145bc09244 --- /dev/null +++ b/party/2sat_implication_reduction/5af0b293-02b2-11ec-a3e8-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +using namespace std; + +const int nmax=205; + +vector v[nmax],vt[nmax]; + +bool viz[nmax],val[nmax],ap[nmax]; + +int r[nmax]; + +int i,j,n,x,m,tip,y,k,nr; + +inline int no(int x) + +{ + + if(x<=n) return n+x; + + return x-n; + +} + +void dfs(int x) + +{ + + viz[x]=1; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>tip; + + if(tip==0) + + { + + + + v[no(x)].push_back(y); + + vt[y].push_back(no(x)); + + v[no(y)].push_back(x); + + vt[x].push_back(no(y)); + + } + + if(tip==1) + + { + + + + v[no(x)].push_back(no(y)); + + vt[no(y)].push_back(no(x)); + + v[y].push_back(x); + + vt[x].push_back(y); + + } + + if(tip==2) + + { + + + + v[x].push_back(y); + + vt[y].push_back(x); + + v[no(y)].push_back(no(x)); + + vt[no(x)].push_back(no(y)); + + } + + if(tip==3) + + { + + + + v[x].push_back(no(y)); + + vt[no(y)].push_back(x); + + v[y].push_back(no(x)); + + vt[no(x)].push_back(y); + + } + + } + + for(i=1;i<=2*n;i++) + + if(!viz[i]) + + dfs(i); + + for(i=2*n;i>=1;i--) + + { + + x=r[i]; + + if(viz[x]&&viz[no(x)])dfst(x); + + if(val[x]&&x<=n) + + nr++; + + } + + g< +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< +#include +#include +#include + + +using namespace std; + + + +const int N=200000+7; + +int n,m,top,o[N],tip[N],cur; + +bool one[N]; + +bool vis[N]; + +vector g[N],ginv[N]; + + + +void dfs1(int a) { + + vis[a]=1; + + for (auto &b:g[a]) { + + if (vis[b]==0) { + + dfs1(b); + + } + + } + + o[++top]=a; + +} + + + +void dfs2(int a) { + + vis[a]=1; + + tip[a]=cur; + + for (auto &b:ginv[a]) { + + if (vis[b]==0) { + + dfs2(b); + + } + + } + +} + + + +signed main() { + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for (int i=1;i<=m;i++) { + + int x,y,t; + + scanf("%d%d%d",&x,&y,&t); + + if (t==0) { + + g[x+n].push_back(y); + + g[y+n].push_back(x); + + } + + if (t==1) { + + g[x+n].push_back(y+n); + + g[y].push_back(x); + + } + + if (t==2) { + + g[x].push_back(y); + + g[y+n].push_back(x+n); + + } + + if (t==3) { + + g[x].push_back(y+n); + + g[y].push_back(x+n); + + } + + + + } + + + + for (int i=1;i<=2*n;i++) { + + for (auto &j : g[i]) { + + ginv[j].push_back(i); + + } + + } + + + + for (int i=1;i<=2*n;i++) { + + if (vis[i]==0) { + + dfs1(i); + + } + + } + + for (int i=1;i<=2*n;i++) { + + vis[i]=0; + + } + + for (int i=2*n;i>=1;i--) { + + int node=o[i]; + + if (vis[node]==0) { + + cur++; + + dfs2(node); + + } + + } + + int cnt=0; + + for (int i=1;i<=n;i++) { + + cnt+=(tip[i]>tip[i+n]); + + } + + printf("%d\n",cnt); + + for (int i=1;i<=n;i++) { + + if (tip[i]>tip[i+n]) { + + printf("%d\n",i); + + } + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/5b1cccc7-0b56-11ec-b3c6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5b1cccc7-0b56-11ec-b3c6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98bc936345445d61fd9f1bffa1f06bd9010efed1 --- /dev/null +++ b/party/2sat_implication_reduction/5b1cccc7-0b56-11ec-b3c6-2cf05d84bfc2.cpp @@ -0,0 +1,172 @@ +#include + + +using namespace std; + + + +const int NMAX = 100; + +int N, M; + +vector > graph, revgraph; + +bitset <2 * NMAX> seen; + + + +int neg(int x){ + + if (x < N){ + + return x + N; + + } + + else{ + + return x - N; + + } + +} + + + +void AddEdge(int x, int y){ + + int negx = neg(x); + + int negy = neg(y); + + graph[negx].push_back(y); + + graph[negy].push_back(x); + + revgraph[y].push_back(negx); + + revgraph[x].push_back(negy); + +} + + + +vector topo; + +vector color; + + + +void dfs1(int node){ + + seen[node] = 1; + + for (auto& x : graph[node]){ + + if (seen[x] == 0){ + + dfs1(x); + + } + + } + + topo.push_back(node); + +} + + + +void dfs2(int node){ + + seen[node] = seen[neg(node)] = 0; + + color[neg(node)] = true; + + for (auto& x : revgraph[node]) + + if (seen[x] == 1) + + dfs2(x); + +} + + + +int main() + +{ + + ifstream fin("party.in"); + + ofstream fout("party.out"); + + fin >> N >> M; + + graph = vector >(2 * N, vector ()); + + revgraph = vector >(2 * N, vector ()); + + color = vector (2 * N, false); + + for (int i = 0;i < M;++i){ + + int t, x, y; + + fin >> x >> y >> t; + + --x; --y; + + if (t == 0) + + AddEdge(x, y); + + else if (t == 1) + + AddEdge(x, neg(y)); + + else if (t == 2) + + AddEdge(neg(x), y); + + else + + AddEdge(neg(x), neg(y)); + + } + + for (int i = 0;i < 2 * N;++i) + + if (seen[i] == 0) + + dfs1(i); + + reverse(topo.begin(), topo.end()); + + for (auto& i : topo) + + if (seen[i] == 1 && seen[neg(i)] == 1) + + dfs2(i); + + vector answer; + + for (int i = 0;i < N;++i) + + if (color[i] == true) + + answer.push_back(i); + + fout << answer.size() << "\n"; + + for (auto& i : answer) + + fout << i + 1 << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5b1cccc8-0b56-11ec-a9d0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5b1cccc8-0b56-11ec-a9d0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9394e3bee2087899f82040b21e0ef2b9674c9b37 --- /dev/null +++ b/party/2sat_implication_reduction/5b1cccc8-0b56-11ec-a9d0-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/5c063db7-02b2-11ec-a7db-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c063db7-02b2-11ec-a7db-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ea24e3ca60e21820a11dccf803b20a1521740e9 --- /dev/null +++ b/party/2sat_implication_reduction/5c063db7-02b2-11ec-a7db-2cf05d84bfc2.cpp @@ -0,0 +1,171 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream af("party.out"); + +int low[210],niv[210],w[210]; + +int n,m,sol,t,x,y,g,i,j; + +set r; + +stack s; + +vector l[210]; + +void dfs (int nod) + +{ + + niv[nod]=low[nod]=++g; + + s.push(nod); + + for(int i=0;i0) + + low[nod]=min(low[nod],low[l[nod][i]]); + + } + + if (low[nod]==niv[nod]) { + + r.clear(); + + do { + + x=s.top(); + + s.pop(); + + r.insert(x); + + + + if (w[x]==0){ + + w[x]=1; + + if (x<=n) + + w[x+n]=-1; + + else + + w[x-n]=-1; + + } + + niv[x]*=-1; + + }while (x!=nod); + + } + +} + +int main () + +{ + + f>>n>>m; + + for (i=1;i<=m;i++) { + + f>>x>>y>>t; + + if (t==1){ + + y*=-1; + + }else{ + + if (t==2) + + x*=-1; + + else + + if (t==3){ + + x*=-1; + + y*=-1; + + } + + } + + + + if (x>0){ + + if (y>0) { + + l[x+n].push_back(y); + + l[y+n].push_back(x); + + }else { + + l[x+n].push_back(-y+n); + + l[-y].push_back(x); + + } + + }else{ + + if (y>0) { + + l[-x].push_back(y); + + l[y+n].push_back(-x+n); + + }else{ + + l[-x].push_back(-y+n); + + l[-y].push_back(-x+n); + + } + + } + + } + + for (i=1;i<=2*n;i++) + + if (niv[i]==0) + + dfs(i); + + for (i=1; i<=n; i++) + + if (w[i]==1) + + sol++; + + af< + + +using namespace std; + + + +const int MAXN = 2e2; + + + +vector g[MAXN + 1], gt[MAXN + 1], srt; + +int seen[MAXN + 1], val[MAXN + 1], n; + + + +int non(int node) { + + if (node > n) + + return node - n; + + return node + n; + +} + + + +void dfs(int node) { + + seen[node] = 1; + + for (auto it : g[node]) + + if (seen[it] == 0) + + dfs(it); + + srt.push_back(node); + +} + + + +void rev_dfs(int node) { + + seen[node] = 0; + + val[non(node)] = 1; + + for (auto it : gt[node]) + + if (seen[it]) + + rev_dfs(it); + +} + + + +inline void add_edg(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +int main() + +{ + + int m, x, y, z, nr; + + ifstream fin("party.in"); + + fin >> n >> m; + + for (int i = 0; i < m; ++i) { + + fin >> x >> y >> z; + + switch (z) { + + case 0: + + add_edg(non(x), y); + + add_edg(non(y), x); + + break; + + case 1: + + add_edg(non(x), non(y)); + + add_edg(y, x); + + break; + + case 2: + + add_edg(x, y); + + add_edg(non(y), non(x)); + + break; + + case 3: + + add_edg(x, non(y)); + + add_edg(y, non(x)); + + break; + + } + + } + + fin.close(); + + for (int i = 1; i <= 2 * n; ++i) + + if (seen[i] == 0) + + dfs(i); + + for (int i = 2 * n - 1; i >= 0; --i) + + if (seen[srt[i]] && seen[non(srt[i])]) + + rev_dfs(srt[i]); + + nr = 0; + + for (int i = 1; i <= n; ++i) + + nr += val[i]; + + ofstream fout("party.out"); + + fout << nr << '\n'; + + for (int i = 1; i <= n; ++i) + + if (val[i]) + + fout << i << '\n'; + + fout.close(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5c434f2c-02b2-11ec-853e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c434f2c-02b2-11ec-853e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed7d09d0fff80481f10bcc90311a55f4dc4038a --- /dev/null +++ b/party/2sat_implication_reduction/5c434f2c-02b2-11ec-853e-2cf05d84bfc2.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +using namespace std; + +int n, m, i, t, x, y, nr, cnt, num, j; + +int viz[205], ff[205], f[205], ctc[205], low[205], niv[205], sol[205]; + +stack s; + +vector v[205], w[205]; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int inv(int x){ + + if(x > n){ + + return x - n; + + } + + return x + n; + +} + +void dfs(int nod){ + + viz[nod] = f[nod] = 1; + + cnt++; + + low[nod] = niv[nod] = cnt; + + s.push(nod); + + for(int i = 0; i < v[nod].size(); i++){ + + int vecin = v[nod][i]; + + if(viz[vecin] == 0){ + + dfs(vecin); + + } + + if(f[vecin] == 1){ + + low[nod] = min(low[nod], low[vecin]); + + } + + } + + if(low[nod] == niv[nod]){ + + nr++; + + int x; + + do{ + + x = s.top(); + + s.pop(); + + ctc[x] = nr; + + f[x] = 0; + + w[nr].push_back(x); + + }while(x != nod); + + } + +} + +int main(){ + + fin>> n >> m; + + for(i = 1; i <= m; i++){ + + fin>> x >> y >> t; + + if(t == 1){ + + y = inv(y); + + } + + else{ + + if(t == 2){ + + x = inv(x); + + } + + else{ + + if(t == 3){ + + x = inv(x); + + y = inv(y); + + } + + } + + } + + v[ inv(x) ].push_back(y); + + v[ inv(y) ].push_back(x); + + } + + for(i = 1; i <= n + n; i++){ + + if(viz[i] == 0){ + + dfs(i); + + } + + } + + for(i = 1; i <= nr; i++){ + + if(ff[i] == 1){ + + continue; + + } + + for(j = 0; j < w[i].size(); j++){ + + x = w[i][j]; + + sol[x] = 1; + + sol[ inv(x)] = 0; + + ff[ ctc[ inv(x) ] ] = 1; + + } + + } + + for(i = 1; i <= n; i++){ + + num += sol[i]; + + } + + fout<< num <<"\n"; + + for(i = 1; i <= n; i++){ + + if(sol[i] == 1){ + + fout<< i <<"\n"; + + } + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/5c84c4f5-02b2-11ec-8a49-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c84c4f5-02b2-11ec-8a49-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0291dd4aceda23ea67f55c459e8a05698a96502a --- /dev/null +++ b/party/2sat_implication_reduction/5c84c4f5-02b2-11ec-8a49-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + +using namespace std; + +const int NMAX = 100; +const int NIL = -1; + +vector G[2 * NMAX]; +int idx[2 * NMAX], stk[2 * NMAX]; +bool onStack[2 * NMAX]; + +vector component[2 * NMAX]; +int numComponents; + +bool value[2 * NMAX]; + +int tarjan(const int& node) { + static int idxPtr, ss; + int minPtr = idx[node] = idxPtr++; + + stk[ss++] = node; + onStack[node] = true; + + for (const int& adj: G[node]) { + if (idx[adj] == NIL) { + minPtr = min(minPtr, tarjan(adj)); + } else if (onStack[adj]) { + minPtr = min(minPtr, idx[adj]); + } + } + + if (idx[node] == minPtr) { + int v; + do { + v = stk[--ss]; + onStack[v] = false; + component[numComponents].push_back(v); + } while (v != node); + numComponents++; + } + return minPtr; +} + +int main() { + ifstream cin("party.in"); + ofstream cout("party.out"); + cin.tie(0); + ios_base::sync_with_stdio(false); + + int n, m; cin >> n >> m; + for (int i = 0; i < m; i++) { + int x, y, type; cin >> x >> y >> type; x--; y--; + x <<= 1; y <<= 1; + x |= ((type >> 1) & 1); + y |= (type & 1); + + G[x ^ 1].push_back(y); + G[y ^ 1].push_back(x); + } + cin.close(); + + memset(idx, NIL, sizeof idx); + for (int i = 0; i < 2 * n; i++) { + if (idx[i] == NIL) { + tarjan(i); + } + } + + for (int i = numComponents - 1; i >= 0; i--) { + if (not value[component[i].front()]) { + for (const int& node: component[i]) { + value[node ^ 1] = true; + } + } + } + + int solutionSize = 0; + for (int i = 0; i < n; i++) { + solutionSize += value[2 * i]; + } + cout << solutionSize << '\n'; + for (int i = 0; i < n; i++) { + if (value[2 * i]) { + cout << 1 + i << '\n'; + } + } + cout.close(); +} diff --git a/party/2sat_implication_reduction/5c9969b9-0b55-11ec-b361-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969b9-0b55-11ec-b361-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcb20f5b3a3b714ab4e49e6419162eed09cf101b --- /dev/null +++ b/party/2sat_implication_reduction/5c9969b9-0b55-11ec-b361-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void Dfs1 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, stack < int > &stk) { + + used[cur_node] = true; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs1 (x, gr, used, stk); + } + + stk.push (cur_node); +} + +void Dfs2 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, vector < int > &ctc, int &cur_ctc) { + + used[cur_node] = true; + ctc[cur_node] = cur_ctc; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs2 (x, gr, used, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > used (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (used[i]) { + continue; + } + + Dfs1 (i, gr, used, stk); + } + + fill (used.begin (), used.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (used[cur]) { + continue; + } + + ++ nr_of_ctc; + Dfs2 (cur, inv, used, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/5c9969ba-0b55-11ec-8ba8-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969ba-0b55-11ec-8ba8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcb20f5b3a3b714ab4e49e6419162eed09cf101b --- /dev/null +++ b/party/2sat_implication_reduction/5c9969ba-0b55-11ec-8ba8-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream cin ("party.in"); +ofstream cout ("party.out"); + +void Dfs1 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, stack < int > &stk) { + + used[cur_node] = true; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs1 (x, gr, used, stk); + } + + stk.push (cur_node); +} + +void Dfs2 (int cur_node, vector < vector < int > > &gr, + vector < bool > &used, vector < int > &ctc, int &cur_ctc) { + + used[cur_node] = true; + ctc[cur_node] = cur_ctc; + for (int x : gr[cur_node]) { + if (used[x]) { + continue; + } + + Dfs2 (x, gr, used, ctc, cur_ctc); + } +} + +int main () { + + ios::sync_with_stdio (false); + cin.tie (0); cout.tie (0); + + int n, m; + cin >> n >> m; + + const int SIZE = 2 * n + 1; + + vector < int > opp (SIZE); + vector < vector < int > > gr (SIZE), inv (SIZE); + + for (int i = 1; i <= n; ++ i) { + opp[i] = i + n; + opp[i + n] = i; + } + + int x, y, t; + while (m --) { + + cin >> x >> y >> t; + if (t == 1) { + y = opp[y]; + } + else if (t == 2) { + x = opp[x]; + } + else if (t == 3) { + x = opp[x]; + y = opp[y]; + } + + gr[opp[x]].push_back (y); + gr[opp[y]].push_back (x); + inv[y].push_back (opp[x]); + inv[x].push_back (opp[y]); + } + + stack < int > stk; + vector < bool > used (SIZE); + vector < int > ctc (SIZE); + + for (int i = 1; i < SIZE; ++ i) { + if (used[i]) { + continue; + } + + Dfs1 (i, gr, used, stk); + } + + fill (used.begin (), used.end (), false); + + int nr_of_ctc = 0; + while (stk.size ()) { + + int cur = stk.top (); + stk.pop (); + + if (used[cur]) { + continue; + } + + ++ nr_of_ctc; + Dfs2 (cur, inv, used, ctc, nr_of_ctc); + } + + vector < int > lvl (nr_of_ctc + 1); + vector < vector < int > > gr_ctc (nr_of_ctc + 1); + + for (int i = 1; i < SIZE; ++ i) { + for (int x : gr[i]) { + if (ctc[i] == ctc[x]) { + continue; + } + + ++ lvl[ctc[x]]; + gr_ctc[ctc[i]].push_back (ctc[x]); + } + } + + queue < int > q; + vector < int > val (nr_of_ctc + 1, -1); + vector < int > opp_ctc (nr_of_ctc + 1); + + for (int i = 1; i <= n; ++ i) { + opp_ctc[ctc[i]] = ctc[opp[i]]; + opp_ctc[ctc[opp[i]]] = ctc[i]; + } + + for (int i = 1; i <= nr_of_ctc; ++ i) { + if (lvl[i] == 0) { + q.push (i); + } + } + + while (not q.empty ()) { + + int cur = q.front (); + q.pop (); + + if (val[cur] != -1) { + continue; + } + + val[cur] = 0; + val[opp_ctc[cur]] = 1; + + for (int x : gr_ctc[cur]) { + if (-- lvl[x] == 0) { + q.push (x); + } + } + } + + int cnt = 0; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + ++ cnt; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; ++ i) { + if (val[ctc[i]] == 1) { + cout << i << '\n'; + } + } +} diff --git a/party/2sat_implication_reduction/5c9969bb-0b55-11ec-b359-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969bb-0b55-11ec-b359-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd8c990edeac5b460377988d7bf56f2cd4ccc98e --- /dev/null +++ b/party/2sat_implication_reduction/5c9969bb-0b55-11ec-b359-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +vector g[200010]; + +vector gt[200010]; + +int st[200010],nr,sol[200010],tip ; + +int idx[200010]; + +bool viz[200010]; + +int n,m,i,j,x,y,ok; + + + +int negat(int x) + +{ + + if(x>n) return x-n; + + return x+n; + + + +} + +void muchie(int x, int y) + +{ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + + + for(int i=0; i>n>>m; + + ok=1; + + for(i=1; i<=m; ++i) + + { + + cin>>x>>y>>tip; + + if(!tip) muchie(x+n,y),muchie(y+n,x); + + else if(tip==1) muchie(x+n,y+n),muchie(y,x); + + else if(tip==2) muchie(y+n,x+n),muchie(x,y); + + else muchie(x,y+n),muchie(y,x+n); + + } + + + + for(i=1; i<=2*n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + + + + memset(viz,0,sizeof(viz)); + + for(i=nr; i>=1; --i) + + { + + if(!viz[st[i]] && !viz[negat(st[i])]) + + { + + dfs2(st[i]); + + } + + } + + if(!ok) + + { + + cout<<-1; + + return 0; + + } + + + + nr=0; + + for(i=1; i<=n; ++i) + + if(sol[i]==1) nr++; + + + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +ifstream cin("a.in"); +ofstream cout("a.out"); + +template +void print(v Vector, type nr) { + for_each(Vector.begin(),Vector.end(), [](type x) { + cout << x << ' '; + }); +} + +struct vertex { + list adi; + list invadi; +}; + +int n; +vertex nod[300]; +bitset<300> viz; +vector > ctc; +stack finish; +int nrcomp = 0; +int sol[300]; + +int non(int nod) { + if (nod <= n) + return nod + n; + else return nod - n; +} + +void edge(const int p1, const int p2) { + nod[p1].adi.push_back(p2); + nod[p2].invadi.push_back(p1); +} + +void dfs(const int start) { + viz[start] = true; + for (auto it = nod[start].adi.begin(); it != nod[start].adi.end(); ++it) { + if (!viz[*it]) + dfs(*it); + } + finish.push(start); +} + +void dfsinv(const int start) { + viz[start] = true; + ctc[nrcomp].emplace_back(start); + for (auto it = nod[start].invadi.begin(); it != nod[start].invadi.end(); ++it) { + if (!viz[*it]) + dfsinv(*it); + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + clock_t tStart = clock(); + int m, x, y, cod; + cin >> n >> m; + while (m--) { + cin >> x >> y >> cod; + + switch (cod) { + case 0: { + edge(non(x), y); + edge(non(y), x); + break; + } + case 1: { + edge(non(x), non(y)); + edge(y, x); + break; + } + case 2: { + edge(non(y), non(x)); + edge(x, y); + break; + } + case 3: { + edge(x, non(y)); + edge(y, non(x)); + break; + } + } + } + for (int i = 1; i <= 2*n; ++i) { + if (!viz[i]) + dfs(i); + } + + viz.reset(); + vector temp; + ctc.push_back(temp); + + while (!finish.empty()) { + if (!viz[finish.top()]) { + ctc.push_back(temp); + ++nrcomp; + dfsinv(finish.top()); + } + finish.pop(); + } + for (int i = 0; i <= 2*n; ++i) + sol[i] = -1; + + int participanti = 0; + + for (int i = nrcomp; i >= 1; --i) { + if (sol[ctc[i][0]] == -1) { + for (auto it = ctc[i].begin(); it != ctc[i].end(); ++it) { + sol[*it] = 1; + sol[non(*it)] = 0; + } + } + } + for (int i = 1; i <= n; ++i) { + if (sol[i] == -1) { + sol[i] = 1; + } + participanti += sol[i]; + } + cout << participanti << '\n'; + for (int i = 1; i <= n; ++i) { + if (sol[i]) + cout << i << '\n'; + } + + printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); +} diff --git a/party/2sat_implication_reduction/5c9969bd-0b55-11ec-953c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969bd-0b55-11ec-953c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5328949a23e28e0ef1a5be02b096c44cbe001f3f --- /dev/null +++ b/party/2sat_implication_reduction/5c9969bd-0b55-11ec-953c-2cf05d84bfc2.cpp @@ -0,0 +1,296 @@ +#include + + +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int DIM = 2007; + + + +vector v[2 * DIM]; + +vector v_back[2 * DIM]; + + + +int n; + + + +int Not(int nod) + +{ + + return (nod <= n ? nod + n : nod - n); + +} + + + +vector st; + + + +bitset <2 * DIM> vis; + + + +int componenta[2 * DIM]; + + + +void dfs1(int nod) + +{ + + vis[nod] = true; + + + + for(auto i : v[nod]) + + if(vis[i] == 0) + + dfs1(i); + + + + st.push_back(nod); + +} + + + +vector CTC[2 * DIM]; + + + +int nr; + + + +void dfs2(int nod) + +{ + + componenta[nod] = nr; + + + + CTC[nr].push_back(nod); + + + + for(auto i : v_back[nod]) + + if(componenta[i] == 0) + + dfs2(i); + +} + + + +bool sol[2 * DIM]; + + + +void atrib(int ord) + +{ + + vis[ord] = false; + + + + for(auto i : CTC[ord]) + + { + + sol[i] = 2; + + sol[Not(i)] = 1; + + } + +} + + + +int main() + +{ + + int m; + + in >> n >> m; + + + + while(m--) + + { + + int x, y, op; + + in >> x >> y >> op; + + + + int X = Not(x); + + int Y = Not(y); + + + + if(op == 0) + + { + + v[X].push_back(y); + + v[Y].push_back(x); + + + + v_back[y].push_back(X); + + v_back[x].push_back(Y); + + } + + else + + if(op == 1) + + { + + v[X].push_back(Y); + + v[y].push_back(x); + + + + v_back[Y].push_back(X); + + v_back[x].push_back(y); + + } + + else + + if(op == 2) + + { + + v[x].push_back(y); + + v[Y].push_back(X); + + + + v_back[y].push_back(x); + + v_back[X].push_back(Y); + + } + + else + + { + + v[x].push_back(Y); + + v[y].push_back(X); + + + + v_back[Y].push_back(x); + + v_back[X].push_back(y); + + } + + } + + + + for(int i = 1; i <= 2 * n; i++) + + if(vis[i] == 0) + + dfs1(i); + + + + for(int i = 2 * n; i >= 1; i--) + + if(componenta[st[i - 1]] == 0) + + { + + nr++; + + + + dfs2(st[i - 1]); + + } + + + + for(int i = 2 * n - 1; i >= 0; i--) + + if(vis[componenta[st[i]]] == true) + + { + + int nod = st[i]; + + int inv = Not(nod); + + + + vis[componenta[nod]] = false; + + vis[componenta[inv]] = false; + + + + for(auto j : CTC[componenta[nod]]) sol[j] = false, sol[Not(j)] = true; + + } + + + + int nr = 0; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + nr++; + + + + out << nr << '\n'; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + out << i << '\n'; + +} diff --git a/party/2sat_implication_reduction/5c9969be-0b55-11ec-bd10-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969be-0b55-11ec-bd10-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3d3e7d407cfb5d294490390a7c769670512af8b --- /dev/null +++ b/party/2sat_implication_reduction/5c9969be-0b55-11ec-bd10-2cf05d84bfc2.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream cin("party.in"); +ofstream cout("party.out"); + +int n, m; + +vector > gr(200100); +vector > inv(200100); + +vector used(200100); + +vector verif; +vector now; + +vector > comp(200100); +vector COMP(200100); +int cont = 0; +vector > GR(200100); +vector enter(200100); + +queue Q; +vector ord_top; + +vector ans(200100); + +bool ret = false; + +void dfs(int root) { + + used[root] = true; + for (auto &x : gr[root]) { + if (!used[x]) { + dfs(x); + } + } + verif.push_back(root); + +} + +void DFS(int root) { + + used[root] = true; + COMP[root] = cont; + now.push_back(root); + for (auto &x : inv[root]) { + if (!used[x]) { + DFS(x); + } + } + +} + +void bfs() { + + while (!Q.empty()) { + + int Now = Q.front(); + Q.pop(); + ord_top.push_back(Now); + + for (auto &x : GR[Now]) { + enter[x]--; + if (!enter[x]) { + Q.push(x); + } + } + + } + +} + +int val(int nr) { + if (nr < 0) { + return n - nr; + } + return nr; +} + +int OPUS(int nr) { + if (nr > n) { + return nr - n; + } + return nr + n; +} + +int main() { + + ios::sync_with_stdio(false); + cin.tie(NULL); + cout.tie(NULL); + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + + int a, b, t; + cin >> a >> b >> t; + + int A = val(-a); + int B = val(-b); + a = val(a); + b = val(b); + + int x1, x2, y1, y2; + + if (t == 0) { + x1 = A; + x2 = B; + y1 = b; + y2 = a; + } + if (t == 1) { + x1 = b; + x2 = A; + y1 = a; + y2 = B; + } + if (t == 2) { + x1 = a; + x2 = B; + y1 = b; + y2 = A; + } + if (t == 3) { + x1 = a; + x2 = b; + y1 = B; + y2 = A; + } + + gr[x1].push_back(y1); + gr[x2].push_back(y2); + + inv[y1].push_back(x1); + inv[y2].push_back(x2); + + } + + + + for (int i = 1; i <= 2 * n; i++) { + if (!used[i]) { + dfs(i); + } + } + + for (int i = 1; i <= 2 * n; i++) { + used[i] = false; + } + + reverse(verif.begin(), verif.end()); + + for (auto &x : verif) { + if (!used[x]) { + now.clear(); + cont++; + DFS(x); + comp[cont] = now; + } + } + for (int i = n + 1; i <= n; i++) { + if (COMP[i] == COMP[i - n]) { + ret = true; + + } + } + + + + for (int i = 1; i <= cont; i++) { + for (auto &x : comp[i]) { + for (auto &y : gr[x]) { + if (COMP[y] != i) { + GR[i].push_back(COMP[y]); + enter[COMP[y]]++; + } + } + } + } + + for (int i = 1; i <= cont; i++) { + if (!enter[i]) { + Q.push(i); + } + } + + bfs(); + for (int i = 1; i <= cont; i++) { + ans[i] = -1; + } + + for (auto &i : ord_top) { + + if (ans[i] == -1) { + ans[i] = 0; + } + for (auto &x : comp[i]) { + int opus = OPUS(x); + + if (ans[COMP[opus]] == -1) { + ans[COMP[opus]] = ans[i] ^ 1; + } else { + if (ans[COMP[opus]] == ans[i]) { + ret = true; + + } + } + for (auto &y : gr[x]) { + if (COMP[y] == COMP[x]) { + continue; + } + if (ans[i] == 1) { + if (ans[COMP[y]] == 0) { + ret = true; + + } + ans[COMP[y]] = 1; + } + } + + } + + } + + int cnt = 0; + for (int i = 1; i <= n; i++) { + if (ans[COMP[i]]) { + cnt++; + } + } + + cout << cnt << '\n'; + for (int i = 1; i <= n; i++) { + if (ans[COMP[i]]) { + cout << i << '\n'; + } + } + + + return 0; +} diff --git a/party/2sat_implication_reduction/5c9969bf-0b55-11ec-b740-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9969bf-0b55-11ec-b740-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c8d98b6745c298dd3e76471cc0d4506f937815b --- /dev/null +++ b/party/2sat_implication_reduction/5c9969bf-0b55-11ec-b740-2cf05d84bfc2.cpp @@ -0,0 +1,176 @@ +#include +#include +using namespace std; + +fstream f1("party.in", ios::in); + +fstream f2("party.out", ios::out); + +int n, m, viz[205], l[205], nrl, sol[205], ctc[205], nrcomp, nrsol; + +vector v[205], vr[205], comp[205]; + +int neg(int nod) + +{ + + if(nod<=n) return n+nod; + + else return nod-n; + +} + +void mu(int x, int y) + +{ + + v[x].push_back(y); + + vr[y].push_back(x); + +} + +void citire() + +{ + + int i, x, y, cer; + + f1>>n>>m; + + for(i=1; i<=m; i++) + + { + + f1>>x>>y>>cer; + + switch(cer) + + { + + case 0: {mu(neg(x), y); mu(neg(y), x);break;} + + case 1: {mu(neg(x), neg(y)); mu(y, x);break;} + + case 2: {mu(neg(y), neg(x)); mu(x, y);break;} + + case 3: {mu(x, neg(y)); mu(y, neg(x));break;} + + } + + } + +} + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto it=v[nod].begin(); it!=v[nod].end(); ++it) + + if(!viz[*it]) + + dfs(*it); + + nrl++; l[nrl]=nod; + +} + +void dfs2(int nod) + +{ + + ctc[nod]=nrcomp; + + comp[nrcomp].push_back(nod); + + for(auto it=vr[nod].begin(); it!=vr[nod].end(); ++it) + + if(!ctc[*it]) + + dfs2(*it); + +} + +void kos() + +{ + + for(int i=1; i<=2*n; i++) + + if(!viz[i]) + + dfs(i); + + for(int i=nrl; i>=1; i--) + + if(!ctc[l[i]]) + + { + + nrcomp++; + + dfs2(l[i]); + + } + +} + +void solutie() + +{ + + int i; + + for(i=1; i<=2*n; i++) sol[i]=-1; + for(i=nrcomp; i>=1; i--) + + if(sol[comp[i][0]]==-1) + + { + + for(auto it=comp[i].begin(); it!=comp[i].end(); ++it) + + { + + sol[*it]=1; + + sol[neg(*it)]=0; + + } + + } + + for(i=1; i<=n; i++) + + { + + if(sol[i]==-1) sol[i]=1; + + nrsol+=sol[i]; + + } + + f2< +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +vector A[205],AT[205]; + +int n,m,i,x,y,z,rez,R[205],Viz[205],top,S[205]; + + + +void dfs(int nod) + +{ + + vector::iterator it; + + Viz[nod]=1; + + for(it=A[nod].begin(); it!=A[nod].end(); it++) + + if(!Viz[*it]) + + dfs(*it); + + S[++top]=nod; + +} + + + +void dfs(int nod, int val) + +{ + + R[nod]=val; + + vector::iterator it; + + for(it=AT[nod].begin(); it!=AT[nod].end(); it++) + + if(R[*it]==-1) + + dfs(*it,val); + +} + + + +int main() + +{ + + fi>>n>>m; + + for(i=1; i<=m; i++) + + { + + fi>>x>>y>>z; + + x=2*x; + + y=2*y; + + if(z==1 || z==3) + + y^=1; + + if(z==2 || z==3) + + x^=1; + + A[x^1].push_back(y); + + A[y^1].push_back(x); + + AT[x].push_back(y^1); + + AT[y].push_back(x^1); + + } + + for(i=2; i<=2*n+1; i++) + + if(!Viz[i]) + + dfs(i); + + for(i=2; i<=2*n+1; i++) + + R[i]=-1; + + for(i=2*n; i>=1; i--) + + if(R[S[i]]==-1 && R[S[i]^1]==-1) + + dfs(S[i],0); + + for(i=2; i<=2*n; i+=2) + + if(R[i]==-1) + + { + + R[i]=1; + + rez++; + + } + + fo< +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int n, nr; + +stack st; + +vector G[205], GT[205]; + +bool viz[205], stare[205]; + + + +inline int negat(int first) { + + if(first<=n) return first+n; + + return first-n; + +} + + + +void DFSp(int first) { + + viz[first] = 1; + + + + for(auto it:G[first]) + + if(!viz[it]) DFSp(it); + + st.push(first); + +} + + + +void DFSm(int first) { + + viz[first] = 0; + + + + if(first > n) { + + ++nr; + + stare[negat(first)] = 1; + + } + + + + for(auto it:GT[first]) + + if(viz[it]) DFSm(it); + +} + + + +int main() { + + int m,i,first,second,t; + + + + fin>>n>>m; + + for(i=1;i<=m;++i) { + + fin>>first>>second>>t; + + + + if(t == 1) second = negat(second); + + else if(t == 2) first = negat(first); + + else if(t == 3) first = negat(first), second = negat(second); + + + + G[negat(first)].push_back(second); + + G[negat(second)].push_back(first); + + + + GT[first].push_back(negat(second)); + + GT[second].push_back(negat(first)); + + } + + + + for(i=1;i<=2*n;++i) + + if(!viz[i]) + + DFSp(i); + + + + while(!st.empty()) { + + if(viz[st.top()] && viz[negat(st.top())]) + + DFSm(st.top()); + + st.pop(); + + } + + + + fout << nr << '\n'; + + for(i=1;i<=2*n;++i) + + if(stare[i]) fout << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/5c9990e9-0b55-11ec-87ec-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5c9990e9-0b55-11ec-87ec-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76f1f4a4c729e465371e446e92c9efeeda5301c5 --- /dev/null +++ b/party/2sat_implication_reduction/5c9990e9-0b55-11ec-87ec-2cf05d84bfc2.cpp @@ -0,0 +1,166 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream F("party.in"); + +ofstream G("party.out"); + + + +int n, m, A, B, x, y, k, K, sol[ 1005 ], w[ 205 ], z, p, q, r, s; + +stack st; + +bool v[505]; + +vector< int > a[ 205 ]; + +vector< int > rev[ 205 ]; + + + +int Id( int x ) + +{ + + if( x <= n ) x +=n; + + else x -= n; + + return x; + +} + + + +void dfs1( int nod ) + +{ + + v[ nod ] = 1; + + vector< int > :: iterator it; + + for( it = a[ nod ].begin(); it != a[ nod ].end(); ++it ) + + if( !v[ *it ] ) dfs1( *it ); + + st.push( nod ); + +} + + + +void dfs2( int nod ) + +{ + + v[ nod ] = 0; + + w[ Id( nod ) ] = 1; + + vector< int > :: iterator it; + + for( it = rev[ nod ].begin(); it != rev[ nod ].end(); ++it ) + + if( v[ *it ] ) dfs2( *it ); + +} + + + +int main() + +{ + + F >> n >> m; + + for( int i = 1; i <= m; ++ i ) + + { + + F >> A >> B >> z; + + if( !z ) + + { + + x = A; y = B; + + } + + else if( z == 1 ) + + { + + x = A; y = Id( B ); + + } + + else if( z == 2 ) + + { + + x = B; y = Id( A ); + + } + + else + + { + + x = Id( A ); y = Id( B ); + + } + + p = Id( x ); q = x; + + r = Id( y ); s = y; + + a[ p ].push_back( s ); + + a[ r ].push_back( q ); + + rev[ s ].push_back( p ); + + rev[ q ].push_back( r ); + + } + + for( int i = 1; i <= 2 * n; ++ i ) + + if( !v[ i ] ) dfs1( i ); + + while( !st.empty() ) + + { + + x = st.top(); + + if( v[ x ] && v[ Id( x ) ] ) + + dfs2( x ); + + st.pop(); + + } + + for( int i = 1; i <= n; ++ i ) + + if( w[ i ] ) + + sol[ ++ K ] = i; + + G << K << '\n'; + + for( int i = 1; i <= K; ++ i ) G << sol[ i ] << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/5ce2272f-02b2-11ec-ad29-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5ce2272f-02b2-11ec-ad29-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4eb4164ac05e76e369cd6d8e5e1fc68593712bb1 --- /dev/null +++ b/party/2sat_implication_reduction/5ce2272f-02b2-11ec-ad29-2cf05d84bfc2.cpp @@ -0,0 +1,245 @@ +#include +#include +#include +#include +using namespace std; + +FILE *fin = freopen("party.in", "r", stdin); + +FILE *fout = freopen("party.out", "w", stdout); + + + +const int MAX_N = 1 + 100; + +const int MAX_NODES = MAX_N << 1; + + + + + +int N, M; + + + +int total_nodes; + +vector< int > graph[MAX_NODES]; + + + +int crt_index; + +int node_index[MAX_NODES], node_lowlink[MAX_NODES]; + + + +bool in_stack[MAX_NODES]; + +stack< int > my_stack; + + + +vector< int > crt_scc; + +int node_value[MAX_NODES]; + + + +vector< int > participants; + + + + + +int OppositeNode(const int node){ + + if(node <= N) { + + return node + N; + + } else { + + return node - N; + + } + +} + + + +void ReadInput() { + + scanf("%d%d", &N, &M); + + for(int i = 1; i <= M; i++) { + + int x, y, z; scanf("%d%d%d", &x, &y, &z); + + if(z == 0) { + + graph[OppositeNode(x)].push_back(y); graph[OppositeNode(y)].push_back(x); + + } else if(z == 1) { + + graph[OppositeNode(x)].push_back(OppositeNode(y)); graph[y].push_back(x); + + } else if(z == 2) { + + graph[x].push_back(y); graph[OppositeNode(y)].push_back(OppositeNode(x)); + + } else { + + graph[x].push_back(OppositeNode(y)); graph[y].push_back(OppositeNode(x)); + + } + + } + +} + + + +void ProcessScc() { + + bool value = true; + + for(int node : crt_scc) { + + if(node_value[node] == 0) { + + value = false; break; + + } + + } + + + + for(int node : crt_scc) { + + node_value[node] = value; + + node_value[OppositeNode(node)] = value ^ true; + + } + +} + + + +void StrongConnect(const int node) { + + crt_index ++; + + node_index[node] = node_lowlink[node] = crt_index; + + my_stack.push(node); in_stack[node] = true; + + + + for(int nxt_node : graph[node]) { + + if(!node_index[nxt_node]) { + + StrongConnect(nxt_node); + + node_lowlink[node] = min(node_lowlink[node], node_lowlink[nxt_node]); + + } else if(in_stack[nxt_node]) { + + node_lowlink[node] = min(node_lowlink[node], node_index[nxt_node]); + + } + + } + + + + if(node_index[node] == node_lowlink[node]) { + + crt_scc.clear(); + + int crt_node; + + do { + + crt_node = my_stack.top(); my_stack.pop(); + + in_stack[crt_node] = false; + + crt_scc.push_back(crt_node); + + }while(crt_node != node); + + + + ProcessScc(); + + } + +} + + + +void RunTarjan() { + + total_nodes = N << 1; + + for(int i = 1; i <= total_nodes; i++) { + + node_value[i] = -1; + + } + + for(int i = 1; i <= total_nodes; i++) { + + if(!node_index[i]) { + + StrongConnect(i); + + } + + } + +} + + + +void WriteOutput() { + + for(int i = 1; i <= N; i++) { + + if(node_value[i] == 1) { + + participants.push_back(i); + + } + + } + + + + printf("%d\n", (int)participants.size()); + + for(int i : participants) { + + printf("%d\n", i); + + } + +} + + + +int main() { + + ReadInput(); + + RunTarjan(); + + WriteOutput(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5d025967-02b2-11ec-9645-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5d025967-02b2-11ec-9645-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4465c48c32ff41ff178c5a0e2909d4231c3ec1e6 --- /dev/null +++ b/party/2sat_implication_reduction/5d025967-02b2-11ec-9645-2cf05d84bfc2.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +using namespace std; + + + +const int MAX_N = 100005; + + + +int n, m; + +vector G[2 * MAX_N], G2[2 * MAX_N]; + +int st[2 * MAX_N]; + +bool viz[2 * MAX_N]; + +bool ans[2 * MAX_N], sol = true; + + + +inline int neg(int val) { return val > n ? val - n : val + n; } + + + +inline void add_edge(int x, int y) + +{ + + G[x].push_back(y); + + G2[y].push_back(x); + +} + + + +void df(int nod) + +{ + + viz[nod] = true; + + vector :: iterator it = G[nod].begin(); + + while(it != G[nod].end()) + + { + + if(!viz[*it]) df(*it); + + it++; + + } + + st[++st[0]] = nod; + +} + + + +void df2(int nod) + +{ + + if(ans[nod]) sol = false; + + viz[nod] = true; + + ans[neg(nod)] = true; + + vector :: iterator it = G2[nod].begin(); + + while(it != G2[nod].end()) + + { + + if(!viz[*it]) df2(*it); + + it++; + + } + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d %d", &n, &m); + + for(int i=0; i=0; i--) + + if(!viz[st[i]] && !viz[neg(st[i])]) + + df2(st[i]); + + if(!sol) printf("-1\n"); + + else + + { + + int nr = 0; + + for(int i=1; i<=n; i++) + + if(ans[i]) nr++; + + printf("%d\n", nr); + + for(int i=1; i<=n; i++) + + if(ans[i]) printf("%d\n", i); + + } + +} diff --git a/party/2sat_implication_reduction/5d228b98-02b2-11ec-b9f0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5d228b98-02b2-11ec-b9f0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d83c9150658110e627e7fa9d05fab1cc58ffbd11 --- /dev/null +++ b/party/2sat_implication_reduction/5d228b98-02b2-11ec-b9f0-2cf05d84bfc2.cpp @@ -0,0 +1,80 @@ +#include +using namespace std; + +const int NMAX = 2e2 + 5; + +vector stk; +bitset fins, instk; +int hgt, n; + +vector g[NMAX]; +int lvl[NMAX], low[NMAX], val[NMAX]; + +int inv(int x) { + if (x <= n) return n + x; + if (x > n) return x - n; } + +void sat(int u) { + stk.push_back(u); + instk[u] = true; + lvl[u] = low[u] = ++hgt; + + for (auto v: g[u]) { + if (lvl[v] == 0) + sat(v); + if (instk[v]) + low[u] = min(low[u], low[v]); } + + if (lvl[u] == low[u]) { + int top; + do { + top = stk.back(); + stk.pop_back(); + instk[top] = false; + + if (!val[top]) { + val[top] = 1; + val[inv(top)] = -1; } } + while (top != u); } } + +int main(void) { + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int m, x, y, op; + + scanf("%d%d ", &n, &m); + while (m--) { + scanf("%d%d%d ", &x, &y, &op); + switch(op) { + case 0: { + g[inv(x)].push_back(y); + g[inv(y)].push_back(x); + break; } + case 1: { + g[inv(x)].push_back(inv(y)); + g[y].push_back(x); + break; } + case 2: { + g[inv(y)].push_back(inv(x)); + g[x].push_back(y); + break; } + case 3: { + g[x].push_back(inv(y)); + g[y].push_back(inv(x)); + break; } } } + + for (int i = 1; i <= 2 * n; ++i) + if (lvl[i] == 0) + sat(i); + + for (int i = 1; i <= n; ++i) + if (val[i] == 1) + stk.push_back(i); + + printf("%d\n", stk.size()); + for (auto i: stk) + printf("%d\n", i); + printf("\n"); + + return 0; } diff --git a/party/2sat_implication_reduction/5dbe0850-02b2-11ec-948b-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5dbe0850-02b2-11ec-948b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c770523e1114a33911b31d235e10e726fd8dce4 --- /dev/null +++ b/party/2sat_implication_reduction/5dbe0850-02b2-11ec-948b-2cf05d84bfc2.cpp @@ -0,0 +1,181 @@ +# include +# include + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +vector Lista[210],sol[210]; + +int val[210],Marcat[210],poz[210],low[210],s[210]; + +int n,m,i,j,nr,u,ctc,ok,loc,x,y,r,sum; + +int sw(int x){ + + if(x>n) + + return x-n; + + else + + return x+n; + +} + +void tarjan(int nc){ + + nr++; + + poz[nc]=nr; + + low[nc]=nr; + + Marcat[nc]=1; + + s[++u]=nc; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y>>r; + + if(r==0){ + + Lista[x+n].push_back(y); + + Lista[y+n].push_back(x); + + } + + if(r==1){ + + Lista[x+n].push_back(y+n); + + Lista[y].push_back(x); + + } + + if(r==2){ + + Lista[y+n].push_back(x+n); + + Lista[x].push_back(y); + + } + + if(r==3){ + + Lista[x].push_back(y+n); + + Lista[y].push_back(x+n); + + } + + } + + for(i=1;i<=2*n;i++) + + val[i]=-1; + + for(i=1;i<=2*n;i++) + + if(!poz[i]) + + tarjan(i); + + for(i=1;i<=ctc;i++) + + for(j=sol[i].size()-2;j>=0;j--) + + val[sol[i][j]]=val[sol[i][j+1]]; + + for(i=1;i<=n;i++) + + sum+=val[i]; + + fout< +using namespace std; + +ifstream in ( "party.in" ); +ofstream out( "party.out" ); + +const int DIM = 2e2 + 5; +const int INF = 0x3f3f3f3f; + +int N, M, X, Y, Z, cnt, ans, Low[DIM], Lev[DIM], Sol[DIM]; +vector G[DIM]; vector St; + +inline int f( int X ) { + return (X <= N) ? (X + N) : (X - N); +} + +void dfs( int node ) { + Low[node] = Lev[node] = ++ cnt; + St.push_back( node ); + + for( auto next : G[node] ) { + if( Lev[next] == 0 ) + dfs( next ); + + if( Lev[next] > 0 ) + Low[node] = min( Low[node], Low[next] ); + } + + if( Low[node] == Lev[node] ) { + int aux; + + do { + aux = St.back(); St.pop_back(); + Lev[aux] *= -1; + + if( Sol[aux] == 0 ) { + Sol[aux] = 1; + Sol[f(aux)] = -1; + } + } while( aux != node ); + } + + return; +} + +int main( int argc, const char *argv[] ) { + + in >> N >> M; + for( int i = 1; i <= M; i ++ ) { + in >> X >> Y >> Z; + + switch( Z ) { + case 0: { + G[f(X)].push_back(Y); + G[f(Y)].push_back(X); + break; + } + case 1: { + G[f(X)].push_back(f(Y)); + G[Y].push_back(X); + break; + } + case 2: { + G[f(Y)].push_back(f(X)); + G[X].push_back(Y); + break; + } + case 3: { + G[X].push_back(f(Y)); + G[Y].push_back(f(X)); + break; + } + } + } + + for( int i = 1; i <= N * 2; i ++ ) { + if( Lev[i] == 0 ) + dfs( i ); + } + + for( int i = 1; i <= N; i ++ ) { + if( Sol[i] == 1 ) + ans ++; + } out << ans << '\n'; + + for( int i = 1; i <= N; i ++ ) { + if( Sol[i] == 1 ) + out << i << "\n"; + } + + return 0; +} diff --git a/party/2sat_implication_reduction/5e1bb89a-02b2-11ec-a8da-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5e1bb89a-02b2-11ec-a8da-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6f842ac81a449637928eefc43633cee898c3b9f --- /dev/null +++ b/party/2sat_implication_reduction/5e1bb89a-02b2-11ec-a8da-2cf05d84bfc2.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +using namespace std; + + + +int sol[300], v[300]; + +vector L[300]; + + + +int n, m, nr, index, lev[300], low[300], st[300], varf, x, y, z, in[300], is[300]; + + + +inline int Minus(int x) { + + if (x <= n) + + return n+x; + + else + + return x-n; + +} + + + +void tarjan(int nod) { + + index ++; + + lev[nod] = index; + + low[nod] = index; + + st[++varf] = nod; + + v[nod] = 1; + + is[nod] = 1; + + for (int i=0;i>n>>m; + + for (int i=1;i<=m;i++) { + + fin>>x>>y>>z; + + if (z == 0) { + + L[Minus(x)].push_back(y); + + L[Minus(y)].push_back(x); + + in[x] = 1; + + in[y] = 1; + + } else + + if (z == 1) { + + L[Minus(x)].push_back(Minus(y)), in[ Minus(y) ] = 1; + + L[y].push_back(x), in[ x ] = 1; + + } + + else + + if (z == 2) { + + L[Minus(y)].push_back(Minus(x)), in[ Minus(x) ] = 1; + + L[x].push_back(y), in[ y ] = 1; + + } else { + + L[x].push_back(Minus(y)), in[Minus(y)] = 1; + + L[y].push_back(Minus(x)), in[Minus(x)] = 1; + + } + + } + + + + for (int i=1;i<=n+n;i++) + + if (v[i] == 0) { + + tarjan(i); + + } + + + + for (int i=1;i<=n;i++) + + if (sol[i] == 1) { + + nr++; + + } + + + + fout< + + +using namespace std; + +int n, m, vis[205], f[205], inv[205], ans; + +vector < int > V[205], v[205]; + +int Not(int x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + +void dfs(int x) + +{ + + vis[x] = 1; + + int i, l = V[x].size(); + + for (i = 0; i < l; ++ i) + + if (!vis[V[x][i]]) + + dfs(V[x][i]); + + + + f[++ f[0]] = x; + +} + +void DFS(int x) + +{ + + vis[x] = 0; + + if (!inv[Not(x)] && x > n) + + ++ ans; + + inv[Not(x)] = 1; + + int i, l = v[x].size(); + + for (i = 0; i < l; ++ i) + + if (vis[v[x][i]]) + + DFS(v[x][i]); + +} + +void add_edge(int x, int y) + +{ + + + + V[Not(x)].push_back(y); + + v[y].push_back(Not(x)); + + V[Not(y)].push_back(x); + + v[x].push_back(Not(y)); + +} + +void read() + +{ + + freopen("party.in", "r", stdin); + + scanf("%d %d\n", &n, &m); + + while (m --) + + { + + int x, y, op; + + scanf("%d %d %d\n", &x, &y, &op); + + if (!op) + + add_edge(x, y); + + else if (op == 1) + + add_edge(x, Not(y)); + + else if (op == 2) + + add_edge(Not(x), y); + + else + + add_edge(Not(x), Not(y)); + + } + +} + +void solve() + +{ + + int i; + + for (i = 1; i <= 2 * n; ++ i) + + if (!vis[i]) + + dfs(i); + + for (i = f[0]; i >= 1; -- i) + + if (vis[f[i]] && vis[Not(f[i])]) + + DFS(f[i]); + +} + +void write() + +{ + + int i; + + freopen("party.out", "w", stdout); + + printf("%d\n", ans); + + for (i = 1; i <= n; ++ i) + + if (inv[i]) + + printf("%d\n", i); + + if (!ans) + + printf("%d\n%d\n", 1, 1); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5e5e67d1-02b2-11ec-b674-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5e5e67d1-02b2-11ec-b674-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86ebaadf13617192a55eda3e97d56ca86f693dd5 --- /dev/null +++ b/party/2sat_implication_reduction/5e5e67d1-02b2-11ec-b674-2cf05d84bfc2.cpp @@ -0,0 +1,125 @@ +#include + +using namespace std; + + +ifstream fin("party.in"); +ofstream fout("party.out"); + +const int NMAX = 102; + +int n; int m; + +vector g[NMAX * 2]; +vector gt[NMAX * 2]; + +int ord[NMAX * 2]; int viz[NMAX * 2]; + +int solved[NMAX * 2]; + +bool existSolution = true; + +int cnt; + +int neg(int x) { + + if(x + n == 2 * n) + return 2 * n; + + return (x + n) % (2 * n); +} + +void read() { + + fin >> n >> m; + + for(int i = 1; i <= m ; ++i) { + + int x; int y; int z; + fin >> x >> y >> z; + + + + if(z == 1) + y = neg(y); + + if(z == 2) + x = neg(x); + + if(z == 3) + x = neg(x), y = neg(y); + + g[neg(x)].push_back(y); + g[neg(y)].push_back(x); + + gt[y].push_back(neg(x)); + gt[x].push_back(neg(y)); + } + + +} + +void dfs(int node) { + + viz[node] = 1; + + for(int x : g[node]) + if(viz[x] == 0) + dfs(x); + + ord[++ord[0]] = node; +} + +void dfsT(int node) { + + if(solved[node] == 1) + existSolution = false; + + viz[node] = 1; + solved[node] = 0; + solved[neg(node)] = 1; + + for(int x : gt[node]) + if(viz[x] == 0) + dfsT(x); +} + +void solve() { + + + + for(int i = 1; i <= 2 * n; ++i) + if(viz[i] == 0) + dfs(i); + + memset(viz, 0, sizeof(viz)); + + + + + for(int i = ord[0]; i ; --i) + if(viz[ord[i]] == 0 && viz[ neg(ord[i]) ] == 0) + dfsT(ord[i]); + + + + for(int i = 1; i <= n ; ++i) + if(solved[i] == 1) + cnt++; + + fout << cnt << '\n'; + + for(int i = 1; i <= n; ++i) + if(solved[i] == 1) + fout << i << '\n'; + +} + +int main() { + + read(); + + solve(); + + return 0; +} diff --git a/party/2sat_implication_reduction/5e81320b-02b2-11ec-8a06-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5e81320b-02b2-11ec-8a06-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b860a3fdf18a797c5f16e03029b0cdf91e0bbbb --- /dev/null +++ b/party/2sat_implication_reduction/5e81320b-02b2-11ec-8a06-2cf05d84bfc2.cpp @@ -0,0 +1,123 @@ +#include + +using namespace std; + + +ifstream fin("party.in"); +ofstream fout("party.out"); + +const int NMAX = 102; + +int n; int m; + +vector g[NMAX * 2]; +vector gt[NMAX * 2]; + +int ord[NMAX * 2]; int viz[NMAX * 2]; + +int solved[NMAX * 2]; + +bool existSolution = true; + +int cnt; + +int neg(int x) { + + if(x + n == 2 * n) + return 2 * n; + + return (x + n) % (2 * n); +} + +void read() { + + fin >> n >> m; + + for(int i = 1; i <= m ; ++i) { + + int x; int y; int z; + fin >> x >> y >> z; + + + + if(z == 1) + y = neg(y); + + if(z == 2) + x = neg(x); + + if(z == 3) + x = neg(x), y = neg(y); + + g[neg(x)].push_back(y); + g[neg(y)].push_back(x); + + gt[y].push_back(neg(x)); + gt[x].push_back(neg(y)); + } + + +} + +void dfs(int node) { + + viz[node] = 1; + + for(int x : g[node]) + if(viz[x] == 0) + dfs(x); + + ord[++ord[0]] = node; +} + +void dfsT(int node) { + + if(solved[node] == 1) + existSolution = false; + + viz[node] = 1; + solved[node] = 0; + solved[neg(node)] = 1; + + for(int x : gt[node]) + if(viz[x] == 0) + dfsT(x); +} + +void solve() { + + + + for(int i = 1; i <= 2 * n; ++i) + if(viz[i] == 0) + dfs(i); + + memset(viz, 0, sizeof(viz)); + + + + + for(int i = ord[0]; i ; --i) + if(viz[ord[i]] == 0 && viz[ neg(ord[i]) ] == 0) + dfsT(ord[i]); + + for(int i = 1; i <= n ; ++i) + if(solved[i] == 1) + cnt++; + + fout << cnt << '\n'; + + for(int i = 1; i <= n; ++i) + if(solved[i] == 1) + fout << i << '\n'; + +} + +int main() { + + read(); + + solve(); + + return 0; +} diff --git a/party/2sat_implication_reduction/5eab9d6e-02b2-11ec-ab77-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5eab9d6e-02b2-11ec-ab77-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa0092e4ebbbfd6e73bf32f1130a97933bdfb49d --- /dev/null +++ b/party/2sat_implication_reduction/5eab9d6e-02b2-11ec-ab77-2cf05d84bfc2.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + +vector g[2*110],gt[2*110]; + +int n,current=0; + +int order[2*110],seen[2*110],answer[2*110]; + +void AddEdge(int x,int y){ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + +int Non(int x){ + + if(x<=n) + + return x+n; + + return x-n; + +} + +void Or(int x,int y){ + + AddEdge(Non(x),y); + + AddEdge(Non(y),x); + +} + +void FirstDFS(int node){ + + int i; + + seen[node]=1; + + for(i=0;i=1;i--) + + if(seen[order[i]]==1&&seen[Non(order[i])]==1) + + SecondDFS(order[i]); + + current=0; + + for(i=1;i<=n;i++) + + if(answer[i]==1) + + current++; + + printf("%d\n",current); + + for(i=1;i<=n;i++) + + if(answer[i]==1) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/5f4b38cc-02b2-11ec-aaa0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5f4b38cc-02b2-11ec-aaa0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bcfc31f91e4030e53d5307228f41003475b21c2 --- /dev/null +++ b/party/2sat_implication_reduction/5f4b38cc-02b2-11ec-aaa0-2cf05d84bfc2.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +ifstream in ( "party.in" ); +ofstream out( "party.out" ); + +const int DIM = 2e2 + 5; +const int INF = 0x3f3f3f3f; + +int N, M, X, Y, Z, cnt, ans, Low[DIM], Lev[DIM], Sol[DIM]; +vector G[DIM]; vector St; + +inline int f( int X ) { + return (X <= N) ? (X + N) : (X - N); +} + +void dfs( int node ) { + Low[node] = Lev[node] = ++ cnt; + St.push_back( node ); + + for( auto next : G[node] ) { + if( Lev[next] == 0 ) + dfs( next ); + + if( Lev[next] > 0 ) + Low[node] = min( Low[node], Low[next] ); + } + + if( Low[node] == Lev[node] ) { + int aux; + + do { + aux = St.back(); St.pop_back(); + Lev[aux] *= -1; + + if( Sol[aux] == 0 ) { + Sol[aux] = 1; + Sol[f(aux)] = -1; + } + } while( aux != node ); + } + + return; +} + +int main( int argc, const char *argv[] ) { + + in >> N >> M; + for( int i = 1; i <= M; i ++ ) { + in >> X >> Y >> Z; + + switch( Z ) { + case 0: { + G[f(X)].push_back(Y); + G[f(Y)].push_back(X); + break; + } + case 1: { + G[f(X)].push_back(f(Y)); + G[Y].push_back(X); + break; + } + case 2: { + G[f(Y)].push_back(f(X)); + G[X].push_back(Y); + break; + } + case 3: { + G[X].push_back(f(Y)); + G[Y].push_back(f(X)); + break; + } + } + } + + for( int i = 1; i <= N * 2; i ++ ) { + if( Lev[i] == 0 ) + dfs( i ); + } + + for( int i = 1; i <= N; i ++ ) { + if( Sol[i] == 1 ) + ans ++; + } out << ans << '\n'; + + for( int i = 1; i <= N; i ++ ) { + if( Sol[i] == 1 ) + out << i << "\n"; + } + + return 0; +} diff --git a/party/2sat_implication_reduction/5f6c2e45-02b2-11ec-abf3-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/5f6c2e45-02b2-11ec-abf3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8226b53e84637a16cfc191e58090b861822dbef8 --- /dev/null +++ b/party/2sat_implication_reduction/5f6c2e45-02b2-11ec-abf3-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include + + + + +using namespace std; + + + +int sol[300], v[300]; + +vector L[300]; + + + +int n, m, nr, index, lev[300], low[300], st[300], varf, x, y, z, in[300], is[300]; + + + +inline int Minus(int x) { + + if (x <= n) + + return n+x; + + else + + return x-n; + +} + + + +void tarjan(int nod) { + + index ++; + + lev[nod] = index; + + low[nod] = index; + + st[++varf] = nod; + + v[nod] = 1; + + is[nod] = 1; + + for (int i=0;i>n>>m; + + for (int i=1;i<=m;i++) { + + fin>>x>>y>>z; + + if (z == 0) { + + L[Minus(x)].push_back(y); + + L[Minus(y)].push_back(x); + + in[x] = 1; + + in[y] = 1; + + } else + + if (z == 1) { + + L[Minus(x)].push_back(Minus(y)), in[ Minus(y) ] = 1; + + L[y].push_back(x), in[ x ] = 1; + + } + + else + + if (z == 2) { + + L[Minus(y)].push_back(Minus(x)), in[ Minus(x) ] = 1; + + L[x].push_back(y), in[ y ] = 1; + + } else { + + L[x].push_back(Minus(y)), in[Minus(y)] = 1; + + L[y].push_back(Minus(x)), in[Minus(x)] = 1; + + } + + } + for (int i=1;i<=n+n;i++) + + if (v[i] == 0) { + + tarjan(i); + + } + + + + for (int i=1;i<=n;i++) + + if (sol[i] == 1) { + + nr++; + + } + + + + fout< + +using namespace std; + + +ifstream fin("party.in"); +ofstream fout("party.out"); + +const int NMAX = 102; + +int n; int m; + +vector g[NMAX * 2]; +vector gt[NMAX * 2]; + +int ord[NMAX * 2]; int viz[NMAX * 2]; + +int solved[NMAX * 2]; + +bool existSolution = true; + +int cnt; + +int neg(int x) { + + if(x + n == 2 * n) + return 2 * n; + + return (x + n) % (2 * n); +} + +void read() { + + fin >> n >> m; + + for(int i = 1; i <= m ; ++i) { + + int x; int y; int z; + fin >> x >> y >> z; + + + + if(z == 1) + y = neg(y); + + if(z == 2) + x = neg(x); + + if(z == 3) + x = neg(x), y = neg(y); + + g[neg(x)].push_back(y); + g[neg(y)].push_back(x); + + gt[y].push_back(neg(x)); + gt[x].push_back(neg(y)); + } + + +} + +void dfs(int node) { + + viz[node] = 1; + + for(int x : g[node]) + if(viz[x] == 0) + dfs(x); + + ord[++ord[0]] = node; +} + +void dfsT(int node) { + + if(solved[node] == 1) + existSolution = false; + + viz[node] = 1; + solved[node] = 0; + solved[neg(node)] = 1; + + for(int x : gt[node]) + if(viz[x] == 0) + dfsT(x); +} + +void solve() { + + + + for(int i = 1; i <= 2 * n; ++i) + if(viz[i] == 0) + dfs(i); + + memset(viz, 0, sizeof(viz)); + + + + + for(int i = ord[0]; i ; --i) + if(viz[ord[i]] == 0 && viz[ neg(ord[i]) ] == 0) + dfsT(ord[i]); + + + + for(int i = 1; i <= n ; ++i) + if(solved[i] == 1) + cnt++; + + fout << cnt << '\n'; + + for(int i = 1; i <= n; ++i) + if(solved[i] == 1) + fout << i << '\n'; + +} + +int main() { + + read(); + + solve(); + + return 0; +} diff --git a/party/2sat_implication_reduction/602d995b-02b2-11ec-a3ad-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/602d995b-02b2-11ec-a3ad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7660ba4cedc7bc16cc0bd0c896ad2c9b16f026df --- /dev/null +++ b/party/2sat_implication_reduction/602d995b-02b2-11ec-a3ad-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int N = 105; + + + +vector g[2*N], gt[2*N], lst, finSol; + +bool sol[2*N], vis[2*N]; + +int n, m; + + + +void firstPass(int x) { + + vis[x] = 1; + + for(auto y : g[x]) { + + if(!vis[y]) { + + firstPass(y); + + } + + } + + lst.push_back(x); + +} + + + +void secondPass(int x) { + + vis[x] = 1; + + sol[(x <= n ? x + n : x - n)] = 1; + + for(auto y : gt[x]) { + + if(!vis[y]) { + + secondPass(y); + + } + + } + +} + + + +inline void addEdge(int x, int y, int t) { + + if(t == 0) { + + g[(x <= n ? x + n : x - n)].push_back(y); + + g[(y <= n ? y + n : y - n)].push_back(x); + + gt[y].push_back((x <= n ? x + n : x - n)); + + gt[x].push_back((y <= n ? y + n : y - n)); + + } + + else if(t == 1) { + + g[y].push_back(x); + + gt[x].push_back(y); + + g[(x <= n ? x + n : x - n)].push_back((y <= n ? y + n : y - n)); + + gt[(y <= n ? y + n : y - n)].push_back((x <= n ? x + n : x - n)); + + } + + else if(t == 2) { + + g[x].push_back(y); + + gt[y].push_back(x); + + g[(y <= n ? y + n : y - n)].push_back((x <= n ? x + n : x - n)); + + gt[(x <= n ? x + n : x - n)].push_back((y <= n ? y + n : y - n)); + + } + + else { + + g[x].push_back((y <= n ? y + n : y - n)); + + gt[(y <= n ? y + n : y - n)].push_back(x); + + } + +} + + + +int main() { + + int i, x, y, t; + + + + in >> n >> m; + + for(i = 1; i <= m; i++) { + + in >> x >> y >> t; + + addEdge(x, y, t); + + } + + + + for(i = 1; i <= n; i++) { + + if(!vis[i]) { + + firstPass(i); + + } + + } + + fill(vis, vis + 2*N, 0); + + reverse(lst.begin(), lst.end()); + + for(auto i : lst) { + + if(!vis[i] && !vis[(i <= n ? i + n : i - n)]) { + + secondPass(i); + + } + + } + + + + for(i = 1; i <= n; i++) { + + if(sol[i]) { + + finSol.push_back(i); + + } + + } + + + + out << finSol.size() << '\n'; + + for(auto i : finSol) out << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/604a792c-7a1b-4c01-b09f-f9c637e3ba1e.cpp b/party/2sat_implication_reduction/604a792c-7a1b-4c01-b09f-f9c637e3ba1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..333edac894e9ebe91c9181a9b3c276c4084dea14 --- /dev/null +++ b/party/2sat_implication_reduction/604a792c-7a1b-4c01-b09f-f9c637e3ba1e.cpp @@ -0,0 +1,198 @@ +#include +#include +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/604c9307-02b2-11ec-8a5c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/604c9307-02b2-11ec-8a5c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7660ba4cedc7bc16cc0bd0c896ad2c9b16f026df --- /dev/null +++ b/party/2sat_implication_reduction/604c9307-02b2-11ec-8a5c-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int N = 105; + + + +vector g[2*N], gt[2*N], lst, finSol; + +bool sol[2*N], vis[2*N]; + +int n, m; + + + +void firstPass(int x) { + + vis[x] = 1; + + for(auto y : g[x]) { + + if(!vis[y]) { + + firstPass(y); + + } + + } + + lst.push_back(x); + +} + + + +void secondPass(int x) { + + vis[x] = 1; + + sol[(x <= n ? x + n : x - n)] = 1; + + for(auto y : gt[x]) { + + if(!vis[y]) { + + secondPass(y); + + } + + } + +} + + + +inline void addEdge(int x, int y, int t) { + + if(t == 0) { + + g[(x <= n ? x + n : x - n)].push_back(y); + + g[(y <= n ? y + n : y - n)].push_back(x); + + gt[y].push_back((x <= n ? x + n : x - n)); + + gt[x].push_back((y <= n ? y + n : y - n)); + + } + + else if(t == 1) { + + g[y].push_back(x); + + gt[x].push_back(y); + + g[(x <= n ? x + n : x - n)].push_back((y <= n ? y + n : y - n)); + + gt[(y <= n ? y + n : y - n)].push_back((x <= n ? x + n : x - n)); + + } + + else if(t == 2) { + + g[x].push_back(y); + + gt[y].push_back(x); + + g[(y <= n ? y + n : y - n)].push_back((x <= n ? x + n : x - n)); + + gt[(x <= n ? x + n : x - n)].push_back((y <= n ? y + n : y - n)); + + } + + else { + + g[x].push_back((y <= n ? y + n : y - n)); + + gt[(y <= n ? y + n : y - n)].push_back(x); + + } + +} + + + +int main() { + + int i, x, y, t; + + + + in >> n >> m; + + for(i = 1; i <= m; i++) { + + in >> x >> y >> t; + + addEdge(x, y, t); + + } + + + + for(i = 1; i <= n; i++) { + + if(!vis[i]) { + + firstPass(i); + + } + + } + + fill(vis, vis + 2*N, 0); + + reverse(lst.begin(), lst.end()); + + for(auto i : lst) { + + if(!vis[i] && !vis[(i <= n ? i + n : i - n)]) { + + secondPass(i); + + } + + } + + + + for(i = 1; i <= n; i++) { + + if(sol[i]) { + + finSol.push_back(i); + + } + + } + + + + out << finSol.size() << '\n'; + + for(auto i : finSol) out << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/606bdad6-02b2-11ec-89ad-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/606bdad6-02b2-11ec-89ad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4fa2423f7bfa5b25246d39e5cd9427f90560ef7 --- /dev/null +++ b/party/2sat_implication_reduction/606bdad6-02b2-11ec-89ad-2cf05d84bfc2.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int variables, expressions; + +int value[235], discover[235], viz[235], Sol[235]; + +vector G[235], GT[235]; + +inline int non(int x) + +{ + + return (x>variables) ? x-variables : x+variables; + +} + +void sau(int x, int y) + +{ + + G[non(x)].push_back(y); + + GT[y].push_back(non(x)); + + G[non(y)].push_back(x); + + GT[x].push_back(non(y)); + +} + +void read() + +{ + + int x, y, z; + + fin>>variables>>expressions; + + + + for(int i=1;i<=expressions;i++) + + { + + fin>>x>>y>>z; + + + + switch(z) + + { + + case 0:sau(x, y); break; + + case 1:sau(x, non(y)); break; + + case 2:sau(non(x), y); break; + + case 3:sau(non(x), non(y)); break; + + }; + + } + +} + +void dfs(int node) + +{ + + viz[node]=1; + + + + for(vector::iterator it=G[node].begin();it!=G[node].end();it++) + + { + + int fiu=*it; + + if(!viz[fiu]) + + dfs(fiu); + + } + + + + discover[++discover[0]]=node; + +} + +void dfst(int node) + +{ + + viz[node]=0; + + value[non(node)]=1; + + + + for(vector::iterator it=GT[node].begin();it!=GT[node].end();it++) + + { + + int fiu=*it; + + if(viz[fiu]) + + dfst(fiu); + + } + +} + +void _2sat() + +{ + + for(int i=1;i<=2*variables;i++) + + if(!viz[i]) + + dfs(i); + + + + for(int i=discover[0];i>=1;i--) + + if(viz[discover[i]] && viz[non(discover[i])]) + + dfst(discover[i]); + +} + +void write() + +{ + + for(int i=1;i<=variables;i++) + + if(value[i]) + + Sol[++Sol[0]]=i; + + + + for(int i=0;i<=Sol[0];i++) + + fout< +#include +#include +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int variables, expressions; + +int value[235], discover[235], viz[235], Sol[235]; + +vector G[235], GT[235]; + +inline int non(int x) + +{ + + return (x>variables) ? x-variables : x+variables; + +} + +void sau(int x, int y) + +{ + + G[non(x)].push_back(y); + + GT[y].push_back(non(x)); + + G[non(y)].push_back(x); + + GT[x].push_back(non(y)); + +} + +void read() + +{ + + int x, y, z; + + fin>>variables>>expressions; + + + + for(int i=1;i<=expressions;i++) + + { + + fin>>x>>y>>z; + + + + switch(z) + + { + + case 0:sau(x, y); break; + + case 1:sau(x, non(y)); break; + + case 2:sau(non(x), y); break; + + case 3:sau(non(x), non(y)); break; + + }; + + } + +} + +void dfs(int node) + +{ + + viz[node]=1; + + + + for(vector::iterator it=G[node].begin();it!=G[node].end();it++) + + { + + int fiu=*it; + + if(!viz[fiu]) + + dfs(fiu); + + } + + + + discover[++discover[0]]=node; + +} + +void dfst(int node) + +{ + + viz[node]=0; + + value[non(node)]=1; + + + + for(vector::iterator it=GT[node].begin();it!=GT[node].end();it++) + + { + + int fiu=*it; + + if(viz[fiu]) + + dfst(fiu); + + } + +} + +void _2sat() + +{ + + for(int i=1;i<=2*variables;i++) + + if(!viz[i]) + + dfs(i); + + + + for(int i=discover[0];i>=1;i--) + + if(viz[discover[i]] && viz[non(discover[i])]) + + dfst(discover[i]); + +} + +void write() + +{ + + for(int i=1;i<=variables;i++) + + if(value[i]) + + Sol[++Sol[0]]=i; + + + + for(int i=0;i<=Sol[0];i++) + + fout< + + +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); +int n, m, dublun; + +int lst[2 * 101], vf[4 * 1001], urm[4 * 1001], nvf = 0; + +bool intoarcere[4 * 1001]; + +bool ok1[2 * 101]; + +int st[2 * 101], nst = 0; + +bool ok2[2 * 101]; + +bool sol[2 * 101]; + +int nsol = 0; + + + +inline void dfs1(int x) + +{ + + ok1[x] = 1; + + for(int i = lst[x]; i; i = urm[i]) + + if(!intoarcere[i] && !ok1[vf[i]]) + + dfs1(vf[i]); + + st[++nst] = x; + +} + + + +inline void dfs2(int x) + +{ + + ok2[x] = 1; + + sol[dublun - x] = 1; + + if(dublun - x <= n) + + nsol++; + + for(int i = lst[x]; i; i = urm[i]) + + if(intoarcere[i] && !ok2[vf[i]]) + + dfs2(vf[i]); + +} + + + +inline void adaugaMuchie(int x, int y) + +{ + + vf[++nvf] = y; + + urm[nvf] = lst[x]; + + lst[x] = nvf; + + vf[++nvf] = x; + + urm[nvf] = lst[y]; + + intoarcere[nvf] = 1; + + lst[y] = nvf; + +} + + + +int main() + +{ + + in >> n >> m; + + dublun = 2 * n + 1; + + while(m--) + + { + + int x, y, op; + + in >> x >> y >> op; + + int notx = dublun - x, noty = dublun - y; + + if(op == 0) + + { + + adaugaMuchie(notx, y); + + adaugaMuchie(noty, x); + + } + + else if(op == 1) + + { + + adaugaMuchie(y, x); + + adaugaMuchie(notx, noty); + + } + + else if(op == 2) + + { + + adaugaMuchie(x, y); + + adaugaMuchie(noty, notx); + + } + + else + + { + + adaugaMuchie(x, noty); + + adaugaMuchie(y, notx); + + } + + } + + + + for(int i = 1; i < dublun; i++) + + if(!ok1[i]) + + dfs1(i); + + + + for(int i = nst; i >= 1; i--) + + if(!ok2[st[i]] && !ok2[dublun - st[i]]) + + dfs2(st[i]); + + + + out << nsol << '\n'; + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + out << i << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/610d23e7-02b2-11ec-a727-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/610d23e7-02b2-11ec-a727-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d6c4d05b779e848eb27552cc9d6ea5ea20b5548 --- /dev/null +++ b/party/2sat_implication_reduction/610d23e7-02b2-11ec-a727-2cf05d84bfc2.cpp @@ -0,0 +1,156 @@ +#include + + + + + + +using namespace std; + + + +vector L[205],T[205],sol; + +int ans[205],viz[205],v[205],l,marked[205],n; + + + +inline int non(int x) + +{ + + if(x<=n) return x+n; + + return x-n; + +} + + + +inline void Dfs(int nod) + +{ + + viz[nod]=1; + + for(auto it : L[nod]) + + if(!viz[it]) Dfs(it); + + v[++l]=nod; + +} + + + +inline void Dfs1(int nod) + +{ + + marked[nod]=1; + + ans[nod]=0; ans[non(nod)]=1; + + for(auto it : T[nod]) + + { + + if(marked[it]) continue; + + Dfs1(it); + + } + +} + + + +int main() + +{ + + int i,m,x,y,z; + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + cin>>n>>m; + + while(m--) + + { + + cin>>x>>y>>z; + + if(!z) + + { + + L[non(x)].push_back(y); T[y].push_back(non(x)); + + L[non(y)].push_back(x); T[x].push_back(non(y)); + + } + + else + + if(z==1) + + { + + L[y].push_back(x); T[x].push_back(y); + + } + + else + + if(z==2) + + { + + L[x].push_back(y); T[y].push_back(x); + + } + + else + + { + + L[x].push_back(non(y)); T[non(y)].push_back(x); + + L[y].push_back(non(x)); T[non(x)].push_back(y); + + } + + } + + for(i=1;i<=2*n;++i) + + { + + if(!viz[i]) Dfs(i); + + ans[i]=-1; + + } + + + + for(i=2*n;i;--i) + + if(ans[v[i]]==-1) Dfs1(v[i]); + + + + for(i=1;i<=n;++i) + + if(ans[i]) sol.push_back(i); + + cout< +#include +#include +#include +using namespace std; + +void dfs(const int cur, const vector>& graf, vector& e_viz, + vector& rez){ + e_viz[cur] = true; + for(const auto next : graf[cur]){ + if(!e_viz[next]){ + dfs(next, graf, e_viz, rez); } } + rez.push_back(cur); } + +void kosoraju(const vector>& graf, const vector>& graf_t, + vector>& rez){ + + const int n = graf.size(); + vector e_viz(n, false); + vector st; + for(int i = 0; i < n; ++i){ + if(!e_viz[i]){ + dfs(i, graf, e_viz, st); } } + fill(begin(e_viz), end(e_viz), false); + while(!st.empty()){ + const int cur = st.back(); + st.pop_back(); + if(!e_viz[cur]){ + rez.emplace_back(0, 0); + dfs(cur, graf_t, e_viz, rez.back()); } } } + +void sat2(const vector>& graf, const vector>& graf_t, + vector& rez){ + const int n = graf.size(); + + vector> comps; + + kosoraju(graf, graf_t, comps); + + for(const auto& c : comps){ + if(none_of(begin(c), end(c), [&rez](const int x){ return rez[x]; })){ + for(const auto x : c){ + rez[x^1] = true; } } } } + +int main(){ + ifstream f("party.in"); + ofstream g("party.out"); + + int n, m; + + f >> n >> m; + + vector> graf(2*n), graf_t(2*n); + + auto add_muc = [&graf, &graf_t](const int a, const int b){ + graf[a].push_back(b); + graf_t[b].push_back(a); }; + + auto add_implication = [&add_muc](const int a, const int b){ + add_muc(a, b); + add_muc(b^1, a^1); }; + + for(int i = 0, a, b, x; i < m; ++i){ + f >> a >> b >> x; + --a, --b; + a *= 2, b *= 2; + if(x == 0){ + add_implication(a, b+1); + add_implication(b, a+1); } + else if(x == 1){ + add_implication(a, b); } + else if(x == 2){ + add_implication(b, a); } + else if(x == 3){ + add_implication(a+1, b); + add_implication(b+1, a); } } + + vector e_invitat(2*n, false); + + sat2(graf, graf_t, e_invitat); + + vector rez; + + for(int i = 0; i < n; ++i){ + if(e_invitat[2*i+1]){ + rez.push_back(i+1); } } + + g << rez.size() << '\n'; + + for(const auto x : rez){ + g << x << '\n'; } + + return 0; } diff --git a/party/2sat_implication_reduction/614b5866-02b2-11ec-b5af-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/614b5866-02b2-11ec-b5af-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9488b955c2c39d8aea36550ca2525ea70724aa5a --- /dev/null +++ b/party/2sat_implication_reduction/614b5866-02b2-11ec-b5af-2cf05d84bfc2.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int NMAX = 200; + + + +bool viz[NMAX+2]; + +vector G[NMAX+2], GP[NMAX+2], ord; + +int ind_ctc = 0; + +int N, M; + +int ctc[NMAX+2]; + + + +int normal( int x ) { + + if( x < 0 ) return -x; + + return x + N; + +} + + + +int negat( int x ) { + + if( x <= N ) return x + N; + + return x - N; + +} + + + +void Dfs_Plus( int nod ) { + + viz[nod] = 1; + + + + for( int i = 0; i < (int)G[nod].size(); ++i ) { + + int x = G[nod][i]; + + if( !viz[x] ) { + + Dfs_Plus( x ); + + } + + } + + + + ord.push_back( nod ); + +} + + + +void Dfs_Minus( int nod ) { + + viz[nod] = 1; + + ctc[nod] = ind_ctc; + + + + for( int i = 0; i < (int)GP[nod].size(); ++i ) { + + int x = GP[nod][i]; + + if( !viz[x] ) { + + Dfs_Minus( x ); + + } + + } + +} + + + +void HueHue( int x, int y ) { + + G[ negat(x) ].push_back( y ); + + G[ negat(y) ].push_back( x ); + + + + GP[ y ].push_back( negat(x) ); + + GP[ x ].push_back( negat(y) ); + +} + + + +int main() { + + in >> N >> M; + + for( int i = 1; i <= M; ++i ) { + + int x,y, op; + + in >> x >> y >> op; + + if( op == 0 ) HueHue( x,y ); + + else if( op == 1 ) HueHue( x, negat(y) ); + + else if( op == 2 ) HueHue( y, negat(x) ); + + else HueHue( negat(x), negat(y) ); + + } + + + + for( int i = 1; i <= 2*N; ++i ) { + + if( !viz[i] ) Dfs_Plus(i); + + } + + memset( viz, 0, sizeof(viz) ); + + for( int i = (int)ord.size() - 1; i >= 0; --i ) { + + if( !viz[ ord[i] ] ) { + + ++ind_ctc; + + + + Dfs_Minus( ord[i] ); + + } + + } + + + + bool ok = 1; + + for( int i = 1; i <= N; ++i ) { + + if( ctc[i] == ctc[i + N] ) ok = 0; + + } + + if( !ok ) { + + out << "-1\n"; + + return 0; + + } + + + + vector sol; + + for( int i = 1; i <= N; ++i ) { + + if( ctc[i] > ctc[i + N] ) sol.push_back( i ); + + } + + out << sol.size() << '\n'; + + sort( sol.begin(), sol.end() ); + + for( int i = 0; i < (int)sol.size(); ++i ) out << sol[i] << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/616aee52-02b2-11ec-804a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/616aee52-02b2-11ec-804a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f44a298c2c5ac051505b9c10eec8d456d4ffc1f6 --- /dev/null +++ b/party/2sat_implication_reduction/616aee52-02b2-11ec-804a-2cf05d84bfc2.cpp @@ -0,0 +1,176 @@ +#include + + + + +int q, val[2*1000 +1], next[2*1000 +1], e[2*1000 +1], urm[2*1000 +1]; + +int u, n, k, sol[200 +1], list[200 +1], lista[200 +1], v[200 +1]; + +bool viz[200 +1], ans[200 +1]; + +inline int non(int x){ + + if(x<=n)return x+n; + + return x-n; + +} + +inline void add(int x, int y){ + + q++; + + e[q]=y; + + urm[q]=list[x]; + + list[x]=q; + +} + +inline void adauga(int x, int y){ + + add(y, x); + + k++; + + val[k]=y; + + next[k]=lista[x]; + + lista[x]=k; + +} + +inline void sau(int x, int y){ + + adauga(non(x), y); + + adauga(non(y), x); + +} + +void dfs1(int x){ + + int p=lista[x]; + + viz[x]=1; + + while(p){ + + if(viz[val[p]]==0){ + + dfs1(val[p]); + + } + + p=next[p]; + + } + + v[++u]=x; + +} + +void dfs2(int x){ + + int p=list[x]; + + ans[non(x)]=1; + + viz[x]=0; + + while(p){ + + if(viz[e[p]]){ + + dfs2(e[p]); + + } + + p=urm[p]; + + } + +} + +int main(){ + + int m, x, y, z, i; + + FILE *fin, *fout; + + fin=fopen("party.in", "r"); + + fout=fopen("party.out", "w"); + + fscanf(fin, "%d%d", &n, &m); + + for(i=0; i0; i--){ + + if((viz[v[i]])&&(viz[non(v[i])])){ + + dfs2(v[i]); + + } + + } + + for(i=1; i<=n; i++){ + + if(ans[i]){ + + sol[++sol[0]]=i; + + } + + } + + for(i=0; i<=sol[0]; i++){ + + fprintf(fout, "%d\n", sol[i]); + + } + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6193e261-2eb3-410e-81b9-f1c4fed39d20.cpp b/party/2sat_implication_reduction/6193e261-2eb3-410e-81b9-f1c4fed39d20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/6193e261-2eb3-410e-81b9-f1c4fed39d20.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/61b40542-02b2-11ec-96d4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/61b40542-02b2-11ec-96d4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3da8b4ae95b3c7f14c668813c91509a35199a6e --- /dev/null +++ b/party/2sat_implication_reduction/61b40542-02b2-11ec-96d4-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include + + +using namespace std; + + + +typedef struct lista + +{ + + int info; + + lista * next; + +} *nod; + + + +nod a[222]; + +nod b[222]; + +int ctc[222]; + +int st[222]; + +bool viz[222]; + +int i,j,k,m,n,u,nr; + + + + + +void dfs1(int x) + +{ + + viz[x]=1; + + for (nod r=a[x];r;r=r->next) + + { + + if (!viz[r->info]) dfs1(r->info); + + } + + st[++u]=x; + +} + + + + + +void dfs2(int x) + +{ + + viz[x]=0; + + ctc[x]=nr; + + for (nod r=b[x];r;r=r->next) + + { + + if (viz[r->info]) dfs2(r->info); + + } + +} + +int neg(int x) + +{ + + if (x>n) return x-n; + + return x+n; + +} + + + + + +void add(int x, nod &p) + +{ + + nod r=new lista; + + r->info=x; + + r->next=p; + + p=r; + +} + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +int main() + +{ + + cin>>n>>m; + + while(m--) + + { + + int x,y,op; + + cin>>x>>y>>op; + + if (op==1) {y=neg(y);} + + if (op==2) {x=neg(x);} + + if (op==3) {x=neg(x); y=neg(y);} + + add(y,a[neg(x)]); + + add(x,a[neg(y)]); + + add(neg(x),b[y]); + + add(neg(y),b[x]); + + } + + for (i=1; i<=2*n; ++i) + + if (!viz[i]) dfs1(i); + + for (i=u; i>=1; --i) + + if (viz[st[i]]) + + { + + nr++; + + ctc[st[i]]=nr; + + dfs2(st[i]); + + } + + int sol[123]; + + u=0; + + + + for (i=1; i<=n; ++i) + + if (ctc[i]>ctc[i+n]) sol[++u]=i; + + cout< +#include +#include + +using namespace std; + +ifstream fin( "party.in" ); ofstream fout( "party.out" ); + +typedef vector< int > graf; + +const int nmax = 100; +int n, nr_comp; +int c[ 2 * nmax + 1 ]; +bool viz[ 2 * nmax + 1 ]; +graf g[ 2 * nmax + 1 ][ 2 ]; + +vector< int > ord, ans; + +inline int neg( int x ) { + if( x <= n ) { + return x + n; + } + return x - n; +} +void muchie( int x, int y ) { + g[ neg( x ) ][ 0 ].push_back( y ); + g[ neg( y ) ][ 0 ].push_back( x ); + + g[ y ][ 1 ].push_back( neg( x ) ); + g[ x ][ 1 ].push_back( neg( y ) ); +} +void dfs( int nod, int ind ) { + viz[ nod ] = 1; + + for( graf::iterator it = g[ nod ][ ind ].begin(); it != g[ nod ][ ind ].end(); ++ it ) { + if ( viz[ *it ] == 0 ) { + dfs( *it, ind ); + } + } + + if ( ind == 0 ) { + ord.push_back( nod ); + } else { + c[ nod ] = nr_comp; + } +} +void plus_minus() { + for( int i = 1; i <= 2 * n; ++ i ) { + if ( viz[ i ] == 0 ) { + dfs( i, 0 ); + } + } + memset( viz, 0, sizeof( viz ) ); + nr_comp = 0; + for( int i = ord.size() - 1; i >= 0; -- i ) { + if ( viz[ ord[ i ] ] == 0 ) { + ++ nr_comp; + dfs( ord[ i ], 1 ); + } + } +} +int main() { + int m; + fin >> n >> m; + for( int i = 0; i < m; ++ i ) { + int x, y, z; + fin >> x >> y >> z; + if ( z == 0 ) { + muchie( x, y ); + } else if ( z == 1 ) { + muchie( x, neg( y ) ); + } else if ( z == 2 ) { + muchie( y, neg( x ) ); + } else { + muchie( neg( x ), neg( y ) ); + } + } + + plus_minus(); + + for( int i = 1; i <= n; ++ i ) { + if ( c[ i ] > c[ i + n ] ) { + ans.push_back( i ); + } + } + fout << ( int )ans.size() << "\n"; + for( int i = 0; i < ( int )ans.size(); ++ i ) { + fout << ans[ i ] << "\n"; + } + fin.close(); + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/61fb11d8-02b2-11ec-807a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/61fb11d8-02b2-11ec-807a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3da8b4ae95b3c7f14c668813c91509a35199a6e --- /dev/null +++ b/party/2sat_implication_reduction/61fb11d8-02b2-11ec-807a-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include + + +using namespace std; + + + +typedef struct lista + +{ + + int info; + + lista * next; + +} *nod; + + + +nod a[222]; + +nod b[222]; + +int ctc[222]; + +int st[222]; + +bool viz[222]; + +int i,j,k,m,n,u,nr; + + + + + +void dfs1(int x) + +{ + + viz[x]=1; + + for (nod r=a[x];r;r=r->next) + + { + + if (!viz[r->info]) dfs1(r->info); + + } + + st[++u]=x; + +} + + + + + +void dfs2(int x) + +{ + + viz[x]=0; + + ctc[x]=nr; + + for (nod r=b[x];r;r=r->next) + + { + + if (viz[r->info]) dfs2(r->info); + + } + +} + +int neg(int x) + +{ + + if (x>n) return x-n; + + return x+n; + +} + + + + + +void add(int x, nod &p) + +{ + + nod r=new lista; + + r->info=x; + + r->next=p; + + p=r; + +} + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +int main() + +{ + + cin>>n>>m; + + while(m--) + + { + + int x,y,op; + + cin>>x>>y>>op; + + if (op==1) {y=neg(y);} + + if (op==2) {x=neg(x);} + + if (op==3) {x=neg(x); y=neg(y);} + + add(y,a[neg(x)]); + + add(x,a[neg(y)]); + + add(neg(x),b[y]); + + add(neg(y),b[x]); + + } + + for (i=1; i<=2*n; ++i) + + if (!viz[i]) dfs1(i); + + for (i=u; i>=1; --i) + + if (viz[st[i]]) + + { + + nr++; + + ctc[st[i]]=nr; + + dfs2(st[i]); + + } + + int sol[123]; + + u=0; + + + + for (i=1; i<=n; ++i) + + if (ctc[i]>ctc[i+n]) sol[++u]=i; + + cout< +#include +#include +using namespace std; + +int n, m, x, y, p, viz[1007], sol[1007], ct; + +vector g[1007], gt[1007]; + +stack order; + +void convert(int &a) + +{ + + if(a < 0) a = (((0-a)<<1) | 1); + + else a = (a<<1); + +} + +void add(const int &a, const int &b) + +{ + + g[a].push_back(b); + + gt[b].push_back(a); + +} + +void create() + +{ + + if(p == 0) + + { + + add((x^1), y); + + add((y^1), x); + + } + + if(p == 1) + + { + + add((x^1), (y^1)); + + add(y, x); + + } + + if(p == 2) + + { + + add(x, y); + + add((y^1), (x^1)); + + } + + if(p == 3) + + { + + add(x, (y^1)); + + add(y, (x^1)); + + } + +} + +void dfsort(const int &nod) + +{ + + viz[nod] = 1; + + for(int i = g[nod].size()-1; i+1; --i) + + { + + if(!viz[g[nod][i]]) dfsort(g[nod][i]); + + } + + order.push(nod); + +} + +void dfsat(const int &nod) + +{ + + viz[nod] = 0; + + if(sol[nod]) printf("check\n"); + + sol[nod] = 0; + + sol[(nod^1)] = 1; + + if(nod&1) ++ct; + + for(int i = gt[nod].size()-1; i+1; --i) + + { + + if(viz[gt[nod][i]]) dfsat(gt[nod][i]); + + } + +} + +void sortare() + +{ + + int tmp; + + for(int i = 1; i<= n; ++i) + + { + + tmp = (i<<1); + + if(!viz[tmp]) dfsort(tmp); + + tmp = (tmp|1); + + if(!viz[tmp]) dfsort(tmp); + + } + +} + +void sat() + +{ + + int tmp; + + while(!order.empty()) + + { + + tmp = order.top(); + + order.pop(); + + if(viz[tmp] && viz[(tmp^1)]) dfsat(tmp); + + } + +} + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d %d", &n, &m); + + for(int i = 1; i<= m; ++i) + + { + + scanf("%d %d %d", &x, &y, &p); + + convert(x); convert(y); + + create(); + + } + + sortare(); + + sat(); + + printf("%d\n", ct); + + for(int i = 1; i<= n; ++i) + + { + + if(sol[(i<<1)]) printf("%d\n", i); + + } + + return 0; + +} diff --git a/party/2sat_implication_reduction/626d8f4d-6a84-4275-afbf-001b2769cce0.cpp b/party/2sat_implication_reduction/626d8f4d-6a84-4275-afbf-001b2769cce0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f060d8989122d18189f43915d7bd5df2cd0f4eb --- /dev/null +++ b/party/2sat_implication_reduction/626d8f4d-6a84-4275-afbf-001b2769cce0.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +vector < int > g[1111]; + +vector < int > gr[1111]; + +vector < int > order; + +int b[1111]; + +int sol[1111]; + +void dfs1(int node) + +{ + + b[node] = 1; + + for (auto it : g[node]) + + if (!b[it]) + + dfs1(it); + + order.push_back(node); + +} + +void dfs2(int node) + +{ + + b[node] = 0; + + sol[node] = 0; + + sol[node^1] = 1; + + for (auto it : gr[node]) + + if (b[it]) + + dfs2(it); + +} + +int main(void) + +{ + + int n,m; + + fi>>n>>m; + + int x,y,z; + + while (m --) + + { + + fi>>x>>y>>z; + + x <<= 1;y <<= 1; + + if (!z) + + g[x^1].push_back(y), + + g[y^1].push_back(x), + + gr[x].push_back(y^1), + + gr[y].push_back(x^1); + + else + + if (z == 1) + + g[y].push_back(x), + + gr[x].push_back(y); + + else + + if (z == 2) + + g[x].push_back(y), + + gr[y].push_back(x); + + else + + if (z == 3) + + g[x].push_back(y^1), + + g[y].push_back(x^1), + + gr[x^1].push_back(y), + + gr[y^1].push_back(x); + + } + + for (int i = 2;i <= n+n;i += 2) + + if (!b[i]) dfs1(i); + + for (int i = 3;i <= n+n+1;i += 2) + + if (!b[i]) dfs1(i); + + for (int i = n+n-1;i+1;--i) + + if (b[order[i]] && b[order[i]^1]) + + dfs2(order[i]); + + vector < int > ans; + + for (int i = 1;i <= n;++i) + + if (sol[i<<1]) + + ans.push_back(i); + + fo << ans.size() << '\n'; + + for (auto it : ans) + + fo << it << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/62b6f1e4-02b2-11ec-bf33-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/62b6f1e4-02b2-11ec-bf33-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4265b5258d376c2ddd6405b99af5236a8d0fbc11 --- /dev/null +++ b/party/2sat_implication_reduction/62b6f1e4-02b2-11ec-bf33-2cf05d84bfc2.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +#include +#include +using namespace std; + +constexpr int my_negate(const int x, const int n){ + return (x < n) ? x+n : x-n; } + +constexpr int undecided = 2, fa = 0, tr = 1; + +class managed_stack{ + stack st; + vector v; + void cl(){ + while(!st.empty() && !v[st.top()]){ + st.pop(); } } +public: + managed_stack(const int n): v(n, false){} + int top(){ + cl(); + return st.top(); } + void push(const int x){ + st.push(x); + v[x] = true; } + void pop(){ + v[st.top()] = false; + st.pop(); } + void remove(const int x){ + v[x] = false; } + bool check(const int x){ + return v[x]; } }; + +void dfs1(const vector >& v, const int cur, managed_stack& st, + vector& e_folosit){ + for(const auto next : v[cur]){ + if(!e_folosit[next]){ + e_folosit[next] = true; + dfs1(v, next, st, e_folosit); } } + st.push(cur); } + +void dfs2(const vector >& v, const int cur, managed_stack& st, + vector& componenta, vector& which_comp, const int comp_cur){ + componenta.push_back(cur); + which_comp[cur] = comp_cur; + st.remove(cur); + for(const auto next : v[cur]){ + if(st.check(next)){ + dfs2(v, next, st, componenta, which_comp, comp_cur); } } } + +void do_print(const int x, const int n){ + if(x < n){ + cout << x+1; } + else{ + cout << '!' << x-n+1; } } + +class graf{ + vector > v; + vector > tr; +public: + void print(const int n){ + cout << v.size() << '\n'; + for(int i = 0; i < v.size(); ++i){ + do_print(i, n); + cout << " -> "; + for(const auto next : v[i]){ + do_print(next, n); + cout << ' '; } + cout << endl; } } + graf(const int sz): + v(sz), tr(sz){} + int size(){ + return v.size(); } + void resize(const int nsz){ + v.resize(nsz); + tr.resize(nsz); } + void add(const int a, const int b){ + v[a].push_back(b); + tr[b].push_back(a); } + void fix(){ + for(auto& x : v){ + sort(begin(x), end(x)); + x.erase(unique(begin(x), end(x)), end(x)); } + for(auto& x : tr){ + sort(begin(x), end(x)); + x.erase(unique(begin(x), end(x)), end(x)); } } + void mk_condensation(vector >& componente, + vector& which_comp, + graf& cond){ + managed_stack st(v.size()); + { + vector e_folosit(v.size(), false); + for(int i = 0; i < v.size(); ++i){ + if(!e_folosit[i]){ + e_folosit[i] = true; + dfs1(v, i, st, e_folosit); } } } + for(int i = 0; i < v.size(); ++i){ + if(st.check(i)){ + const int comp_cur = componente.size(); + componente.push_back({}); + dfs2(tr, i, st, componente.back(), which_comp, comp_cur); } } + cond.resize(componente.size()); + for(int i = 0; i < componente.size(); ++i){ + for(const auto cur : componente[i]){ + for(const auto next : v[cur]){ + if(i != which_comp[next]){ + cond.add(i, which_comp[next]); } } } } + cond.fix(); } + + void toposort(vector& ordine){ + ordine.reserve(v.size()); + vector in_deg(v.size()); + for(const auto& y : v){ + for(const auto x : y){ + ++in_deg[x]; } } + stack de_viz; + for(int i = 0; i < v.size(); ++i){ + if(in_deg[i] == 0){ + de_viz.push(i); } } + while(!de_viz.empty()){ + const int cur = de_viz.top(); + de_viz.pop(); + ordine.push_back(cur); + for(const auto next : v[cur]){ + --in_deg[next]; + if(in_deg[next] == 0){ + de_viz.push(next); } } } } + + const vector& get_adiac(const int x)const{ + return v[x]; } }; + +int main(){ + ifstream f("party.in"); + int n, m; + f >> n >> m; + const int graf_sz = 2*n; + graf g(graf_sz); + + for(int i = 0, a, b, t; i < m; ++i){ + f >> a >> b >> t; + --a, --b; + switch(t){ + case 0: + g.add(my_negate(a, n), b); + g.add(my_negate(b, n), a); + break; + case 1: + g.add(my_negate(a, n), my_negate(b, n)); + g.add(b, a); + break; + case 2: + g.add(my_negate(b, n), my_negate(a, n)); + g.add(a, b); + break; + case 3: + g.add(a, my_negate(b, n)); + g.add(b, my_negate(a, n)); + break; } } + + + vector > componente; + vector which_comp(2*n); + graf cond(0); + g.mk_condensation(componente, which_comp, cond); + vector topo; + cond.toposort(topo); + reverse(begin(topo), end(topo)); + vector e_invitat(g.size(), undecided); + for(const auto comp : topo){ + if(e_invitat[componente[comp].front()] == undecided){ + for(const auto x : componente[comp]){ + do_print(x, n); + e_invitat[x] = tr; + e_invitat[my_negate(x, n)] = fa; } } } + { + ofstream g("party.out"); + g << count(begin(e_invitat), begin(e_invitat)+n, tr) << '\n'; + for(int i = 0; i < n; ++i){ + if(e_invitat[i]){ + g << i+1 << '\n'; } } } + return 0; } diff --git a/party/2sat_implication_reduction/6313df29-02b2-11ec-8bbd-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6313df29-02b2-11ec-8bbd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4e475d5019ab993770a4540f96e5c5a28f6d2b5 --- /dev/null +++ b/party/2sat_implication_reduction/6313df29-02b2-11ec-8bbd-2cf05d84bfc2.cpp @@ -0,0 +1,270 @@ +#include + + + + + + +using namespace std; + + + +const int NMAX = 2 * 100; + + + +int N, M; + +int comp[NMAX], answer[NMAX]; + +bool used[NMAX]; + +vector G[NMAX], GT[NMAX], discover, C[NMAX]; + + + +inline int idx(int value) { + + if (value < 0) + + return abs(value) + N; + + return value; + +} + + + +inline int neg(int value) { + + if (value > N) + + return value - N; + + return value + N; + +} + + + +void DFP(int node) { + + used[node] = 1; + + for (auto it : G[node]) { + + if (used[it]) + + continue; + + DFP(it); + + } + + discover.emplace_back(node); + +} + + + +void DFM(int node, int index) { + + used[node] = 1; + + comp[node] = index; + + C[index].emplace_back(node); + + for (auto it : GT[node]) { + + if (used[it]) + + continue; + + DFM(it, index); + + } + +} + + + +int main() { + + ios_base::sync_with_stdio(false); + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + freopen("debug.err", "w", stderr); + + + + + + int i; + + + + cin >> N >> M; + + for (i = 1; i <= M; ++i) { + + int x, y, c; + + cin >> x >> y >> c; + + + + if (c == 0) { + + x = idx(x), y = idx(y); + + G[neg(x)].emplace_back(y); + + G[neg(y)].emplace_back(x); + + GT[y].emplace_back(neg(x)); + + GT[x].emplace_back(neg(y)); + + } else if (c == 1) { + + x = idx(x), y = neg(idx(y)); + + G[neg(x)].emplace_back(y); + + G[neg(y)].emplace_back(x); + + GT[y].emplace_back(neg(x)); + + GT[x].emplace_back(neg(y)); + + } else if (c == 2) { + + x = neg(idx(x)), y = idx(y); + + G[neg(x)].emplace_back(y); + + G[neg(y)].emplace_back(x); + + GT[y].emplace_back(neg(x)); + + GT[x].emplace_back(neg(y)); + + } else { + + x = neg(idx(x)), y = neg(idx(y)); + + G[neg(x)].emplace_back(y); + + G[neg(y)].emplace_back(x); + + GT[y].emplace_back(neg(x)); + + GT[x].emplace_back(neg(y)); + + } + + } + + + + for (i = 1; i <= 2 * N; ++i) { + + if (used[i]) + + continue; + + DFP(i); + + } + + + + reverse(discover.begin(), discover.end()); + + memset(used, 0, sizeof(used)); + + int index = 0; + + for (auto it : discover) { + + if (used[it]) + + continue; + + DFM(it, index++); + + } + + + + for (i = 1; i <= N; ++i) { + + if (comp[i] == comp[i + N]) { + + cout << "-1\n"; + + return 0; + + } + + } + + + + memset(answer, -1, sizeof(answer)); + + for (i = 0; i < index; ++i) { + + for (auto it : C[i]) { + + if (it <= N && answer[it] != -1) + + continue; + + if (it > N && answer[it - N] != -1) + + continue; + + if (it <= N) + + answer[it] = 0; + + else + + answer[it - N] = 1; + + } + + } + + + + int cnt = 0; + + for (i = 1; i <= N; ++i) + + if (answer[i]) + + ++cnt; + + cout << cnt << '\n'; + + for (i = 1; i <= N; ++i) + + if (answer[i]) + + cout << i << '\n'; + + cout << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/63327008-02b2-11ec-80af-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/63327008-02b2-11ec-80af-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7cbb6f1531f3690bcd0c6435da1e39bebdc8eb9 --- /dev/null +++ b/party/2sat_implication_reduction/63327008-02b2-11ec-80af-2cf05d84bfc2.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 202; + + + +vector graph [N], graph2 [N]; + +stack st; + +bool used [N]; + +int n, value [N]; + + + +inline int no (const int &x) { + + if (x > n) + + return x - n; + + else return n + x; + +} + + + +void dfs (int x) { + + vector :: iterator it; + + + + used [x] = 1; + + for (it = graph [x].begin (); it != graph [x].end (); ++ it) + + if (!used [*it]) + + dfs (*it); + + st.push (x); + +} + + + +void dfs2 (int x) { + + vector :: iterator it; + + + + used [x] = 1; + + value [no (x)] = 1; + + for (it = graph2 [x].begin (); it != graph2 [x].end (); ++ it) + + if (!used [*it]) + + dfs2 (*it); + +} + + + +int main () { + + int i, a, b, m, ineg, c; + + vector :: iterator it; + + + + freopen ("party.in", "r", stdin); + + freopen ("party.out", "w", stdout); + + + + scanf ("%d%d", &n, &m); + + for (i = 1; i <= m; i ++) { + + scanf ("%d%d%d", &a, &b, &c); + + if (c == 1) + + b = no (b); + + if (c == 2) + + a = no (a); + + if (c == 3) { + + a = no (a); + + b = no (b); + + } + + graph [no (a)].push_back (b); + + graph2 [b].push_back (no (a)); + + graph [no (b)].push_back (a); + + graph2 [a].push_back (no (b)); + + } + + for (i = 1; i <= 2 * n; i ++) + + if (!used [i]) + + dfs (i); + + memset (used, 0, sizeof (used)); + + while (!st.empty () ) { + + i = st.top (); + + ineg = no (i); + + st.pop (); + + if (!used [i] && !used [ineg]) { + + dfs2 (i); + + } + + } + + int ans = 0; + + for (i = 1; i <= n; i ++) + + ans += value [i]; + + printf ("%d\n", ans); + + for (i = 1; i <= n; i ++) + + if (value [i]) + + printf ("%d\n", i); + + printf ("\n"); + + return 0; + +} diff --git a/party/2sat_implication_reduction/63511b95-02b2-11ec-9606-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/63511b95-02b2-11ec-9606-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/63511b95-02b2-11ec-9606-2cf05d84bfc2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/63dbabea-02b2-11ec-92e4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/63dbabea-02b2-11ec-92e4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b09bb336892cf2f915e015a340fcd4cf5feb1e40 --- /dev/null +++ b/party/2sat_implication_reduction/63dbabea-02b2-11ec-92e4-2cf05d84bfc2.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +using namespace std; + + + +const int MAX_N = 100005; + + + +int n, m; + +vector G[2 * MAX_N], G2[2 * MAX_N]; + +int st[2 * MAX_N]; + +bool viz[2 * MAX_N]; + +bool value[2 * MAX_N], no_sol; + + + +inline int neg(int val) { return val > n ? val - n : val + n; } + + + +inline void add ( int x , int y ) + +{ + + G [ x ].push_back ( y ) ; + + G2 [ y ].push_back ( x ) ; + +} + + + +void df(int nod) { + + viz[nod] = true; + + for (vector::iterator it = G[nod].begin(); it != G[nod].end(); ++it) + + if (!viz[*it]) df(*it); + + st[++st[0]] = nod; + +} + + + +void df2(int nod) { + + if (value[nod]) no_sol = true; + + viz[nod] = true; + + value[neg(nod)] = true; + + for (vector::iterator it = G2[nod].begin(); it != G2[nod].end(); ++it) + + if (!viz[*it]) df2(*it); + +} + + + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &n, &m); + + for (int i = 0; i < m; ++i) { + + int x, y , c ; + + scanf("%d %d %d ", &x, &y, &c); + + + + + + if ( c == 0 ) + + { + + add(neg(x),y); + + add(neg(y),x); + + } + + else if ( c == 1 ) + + { + + add(neg(x),neg(y)); + + add(y,x); + + } + + else if ( c == 2 ) + + { + + add(x,y); + + add(neg(y),neg(x)); + + } + + else { + + add(x,neg(y)); + + add(y,neg(x)); + + } + + } + + + + for (int i = 1; i <= 2*n; ++i) + + if (!viz[i]) df(i); + + memset(viz, false, sizeof(viz)); + + for (int i = st [ 0 ] ; i; --i) + + if (!viz[st[i]] && !viz[neg(st[i])]) + + df2(st[i]); + + + + int cate = 0 ; + + for (int i = 1; i <= n; ++i) + + if ( value [i] ) ++ cate ; + + printf ( "%d\n" , cate ) ; + + for (int i = 1; i <= n; ++i) + + if ( value [i] ) + + printf("%d\n", i ); + +} diff --git a/party/2sat_implication_reduction/641c93ad-02b2-11ec-8a68-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/641c93ad-02b2-11ec-8a68-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f34da0233a72e67e71886d8cb2cb334183072a1d --- /dev/null +++ b/party/2sat_implication_reduction/641c93ad-02b2-11ec-8a68-2cf05d84bfc2.cpp @@ -0,0 +1,83 @@ +#include +#include +#include +#include +using namespace std; +const int NMAX=205; +vector g[NMAX], gt[NMAX]; +int n, k, x, y, z, u, i; +int t[NMAX], sol[NMAX]; +bool vis[NMAX]; +int neg(int x) +{ + if(x<=n)return x+n; + else return x-n; +} +int DFS(int node) +{ + vis[node]=1; + for(vector::iterator it=g[node].begin(); it!=g[node].end(); it++) + if(!vis[*it])DFS(*it); + ++u; + t[u]=node; +} +int DFST(int node) +{ + sol[node]=0; + sol[neg(node)]=1; + vis[node]=0; + for(vector::iterator it=gt[node].begin(); it!=gt[node].end(); it++) + if(vis[*it])DFST(*it); + +} +int main() +{ + ifstream cin("party.in"); + ofstream cout("party.out"); + cin>>n>>k; + for(i=1; i<=k; i++) + { + cin>>x>>y>>z; + if(z==0) + { + gt[x].push_back(neg(y)); + gt[y].push_back(neg(x)); + g[neg(x)].push_back(y); + g[neg(y)].push_back(x); + } + else if(z==1) + { + g[y].push_back(x); + gt[x].push_back(y); + } + else if(z==2) + { + g[x].push_back(y); + gt[y].push_back(x); + } + else + { + g[x].push_back(neg(y)); + g[y].push_back(neg(x)); + gt[neg(x)].push_back(y); + gt[neg(y)].push_back(x); + } + + } + for(i=1; i<=2*n; i++) + if(!vis[i]) + DFS(i); + reverse(t+1,t+u+1); + for(i=1; i<=u; i++) + if(vis[t[i]]&&vis[neg(t[i])]) + DFST(t[i]); + int nr=0; + for(i=1; i<=n; i++) + if(sol[i]) + nr++; + cout< +#include +#include + + + + +using namespace std; + + + +vector L[505],T[505]; + +int Sorted[505],len,sol[505],n; + +bool viz[505]; + + + +inline int non(int x) + +{ + + if(x>n) return x-n; + + return x+n; + +} + + + +inline void Dfs(int nod) + +{ + + viz[nod]=true; + + vector ::iterator it; + + for(it=L[nod].begin();it!=L[nod].end();++it) + + if(!viz[*it]) Dfs(*it); + + Sorted[++len]=nod; + +} + + + +inline void Dfs2(int nod) + +{ + + viz[nod]=false; sol[nod]=0; sol[non(nod)]=1; + + vector ::iterator it; + + for(it=T[nod].begin();it!=T[nod].end();++it) + + if(viz[*it]) Dfs2(*it); + + + +} + + + +int main() + +{ + + int m,x,y,tip,i,cnt; + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + scanf("%d%d", &n,&m); + + while(m--) + + { + + scanf("%d%d%d", &x,&y,&tip); + + if(!tip) + + { + + L[non(x)].push_back(y); + + L[non(y)].push_back(x); + + T[y].push_back(non(x)); + + T[x].push_back(non(y)); + + } + + else + + if(tip==1) + + { + + L[y].push_back(x); + + T[x].push_back(y); + + } + + else + + if(tip==2) + + { + + L[x].push_back(y); + + T[y].push_back(x); + + } + + else + + { + + L[y].push_back(non(x)); + + L[x].push_back(non(y)); + + T[non(x)].push_back(y); + + T[non(y)].push_back(x); + + } + + } + + for(i=1;i<=2*n;++i) + + if(!viz[i]) Dfs(i); + + reverse(Sorted+1,Sorted+len+1); + + for(i=1;i<=len;++i) + + if(viz[Sorted[i]] && viz[non(Sorted[i])]) Dfs2(Sorted[i]); + + for(i=1,cnt=0;i<=n;++i) cnt+=(sol[i]==1); + + printf("%d\n", cnt); + + for(i=1;i<=n;++i) + + if(sol[i]) printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/645c82e3-02b2-11ec-860c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/645c82e3-02b2-11ec-860c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f51ec5d24f13540011636b07d0b74c111c3d25b2 --- /dev/null +++ b/party/2sat_implication_reduction/645c82e3-02b2-11ec-860c-2cf05d84bfc2.cpp @@ -0,0 +1,159 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + + + + + +int n,m,i,x,y,z,st,nr; + +vector G[210]; + +int stack[210]; + +bool X[210],viz[210]; +void dfs(int nod) + +{ + + (viz+100)[nod]=true; + + for(vector:: iterator it=(G+100)[nod].begin();it!=(G+100)[nod].end();++it) + + if(! (viz+100)[*it]) dfs(*it); + + + + stack[++st]=nod; + +} + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>z; + + switch(z) + + { + + case 0: + + { + + + + (G+100)[-x].push_back(y); + + (G+100)[-y].push_back(x); + + break; + + } + + case 1: + + { + + + + (G+100)[-x].push_back(-y); + + (G+100)[y].push_back(x); + + break; + + } + + case 2: + + { + + + + (G+100)[x].push_back(y); + + (G+100)[-y].push_back(-x); + + break; + + } + + case 3: + + { + + + + (G+100)[x].push_back(-y); + + (G+100)[y].push_back(-x); + + break; + + } + + } + + } + + + + for(i=-n;i<=n;++i) + + if(!(viz+100)[i]) dfs(i); + + + + for(;st;--st) + + { + + if(!(X+100)[stack[st]] && !(X+100)[-stack[st]]) (X+100)[-stack[st]]=true; + + } + + + + for(i=1;i<=n;++i) + + { + + if ((X+100)[i]) ++nr; + + } + + g< +#include +#include + + +using namespace std; + + + +int n, m, x, y, t; + +vector G[220], GT[220], v; + +bool uz[220], sol[220]; + + + +inline int neg(int x) { + + return (x <= n)?(x + n):(x - n); + +} + + + +void DFSP(int u) { + + uz[u] = 1; + + for(auto x: G[u]) + + if(!uz[x]) + + DFSP(x); + + v.push_back(u); + +} + + + +void DFST(int p) { + + uz[p] = 1; + + sol[neg(p)] = 1; + + + + for(auto x: GT[p]) + + if(!uz[x]) + + DFST(x); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d %d %d", &x, &y, &t); + + switch(t) { + + case 0: x = neg(x); break; + + case 1: swap(x, y); break; + + case 3: y = neg(y); + + } + + G[x].push_back(y); + + G[neg(y)].push_back(neg(x)); + + GT[y].push_back(x); + + GT[neg(x)].push_back(neg(y)); + + } + + + + for(int i = 1; i <= 2 * n; i++) + + if(!uz[i]) + + DFSP(i); + + + + memset(uz, 0, sizeof(uz)); + + for(int i = v.size() - 1; i >= 0; i--) { + + if(!uz[v[i]] && !uz[neg(v[i])]) + + DFST(v[i]); + + } + + + + int cnt = 0; + + for(int i = 1; i <= n; i++) + + cnt += sol[i]; + + printf("%d\n", cnt); + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/64875c4b-4404-4ea5-8436-f055a45f05fe.cpp b/party/2sat_implication_reduction/64875c4b-4404-4ea5-8436-f055a45f05fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c8e349acf9d76a96f39aec3e5dade3be902489 --- /dev/null +++ b/party/2sat_implication_reduction/64875c4b-4404-4ea5-8436-f055a45f05fe.cpp @@ -0,0 +1,322 @@ +#include +#include +#include + + +using namespace std; + + + +FILE *f = fopen("party.in","r"); + +FILE *g = fopen("party.out","w"); + + + +const int NMAX = 1e2; + +const int OFFSET = NMAX + 2; + + + +vector graph[2 * OFFSET + 5]; + + + +int n,m; + +bool viz[2 * OFFSET + 5]; + +int st[2 * OFFSET + 5],len; + +bool inst[2 * OFFSET + 5]; + +int ctc[2 * OFFSET + 5],last_ctc; + +int id[2 * OFFSET + 5],last_id; + +int low[2 * OFFSET + 5]; + +vector ctc_nodes[2 * OFFSET + 5]; + + + +void dfs(int nod){ + + viz[nod] = true; + + st[++len] = nod;inst[nod] = true; + + id[nod] = low[nod] = ++last_id; + + + + for(auto it:graph[nod]){ + + if(viz[it] == false){ + + dfs(it); + + } + + if(inst[it]){ + + low[nod] = min(low[nod],low[it]); + + } + + } + + + + if(low[nod] == id[nod]){ + + last_ctc++; + + while(st[len] != nod){ + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + +} + + + +int ans[2 * OFFSET + 5]; + +int ctc_ans[2 * OFFSET + 5]; + +int ctc_gr[2 * OFFSET + 5]; + + + +vector solve_clauses(){ + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + ans[i] = -1; + + if(viz[i] == false){ + + dfs(i); + + } + + } + + + + for(int i = 1;i <= last_ctc;i++){ + + ctc_ans[i] = -1; + + } + + + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + for(auto it:graph[i]){ + + if(ctc[it] != ctc[i]){ + + ctc_gr[ctc[it]]++; + + } + + } + + } + + + + vector topo; + + + + for(int i = 1;i <= last_ctc;i++){ + + if(ctc_gr[i] == 0){ + + topo.push_back(i); + + } + + } + + + + for(int i = 0;i < (int)topo.size();i++){ + + int comp = topo[i]; + + int val = ctc_ans[comp]; + + + + for(auto it:ctc_nodes[comp]){ + + val = max(val,ans[it]); + + } + + + + val = max(val,0); + + + + ctc_ans[comp] = val; + + + + for(auto it:ctc_nodes[comp]){ + + ans[it] = val; + + ans[2 * OFFSET - it] = 1- val; + + for(auto it2:graph[it]){ + + if(ctc[it2] != ctc[it]){ + + ctc_gr[ctc[it2]]--; + + if(ctc_gr[ctc[it2]] == 0){ + + topo.push_back(ctc[it2]); + + } + + if(val == 1){ + + ctc_ans[ctc[it2]] = 1; + + } + + } + + } + + } + + } + + + + vector cnt; + + + + for(int i = OFFSET + 1;i <= OFFSET + n;i++){ + + if(ans[i]){ + + cnt.push_back(i - OFFSET); + + } + + } + + + + return cnt; + +} + + + +int main(){ + + + + fscanf(f,"%d %d",&n,&m); + + + + for(int i = 1;i <= m;i++){ + + int x,y,z; + + fscanf(f,"%d %d %d",&x,&y,&z); + + if(z == 0){ + + graph[OFFSET - x].push_back(OFFSET + y); + + graph[OFFSET - y].push_back(OFFSET + x); + + } + + else if(z == 1){ + + graph[OFFSET - x].push_back(OFFSET - y); + + } + + else if(z == 2){ + + graph[OFFSET - y].push_back(OFFSET - x); + + } + + else{ + + graph[OFFSET + x].push_back(OFFSET - y); + + graph[OFFSET + y].push_back(OFFSET - x); + + } + + } + + + + vector tmp = solve_clauses(); + + + + fprintf(g,"%d\n",(int)tmp.size()); + + + + for(auto it:tmp){ + + fprintf(g,"%d\n",it); + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/649ab68b-02b2-11ec-bb2c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/649ab68b-02b2-11ec-bb2c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a3038b85f8aba8fb040495edaccb6ca93edb0f3 --- /dev/null +++ b/party/2sat_implication_reduction/649ab68b-02b2-11ec-bb2c-2cf05d84bfc2.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector L[505],T[505]; + +int Sorted[505],len,sol[505],n; + +bool viz[505]; + + + +inline int non(int x) + +{ + + if(x>n) return x-n; + + return x+n; + +} + + + +inline void Dfs(int nod) + +{ + + viz[nod]=true; + + vector ::iterator it; + + for(it=L[nod].begin();it!=L[nod].end();++it) + + if(!viz[*it]) Dfs(*it); + + Sorted[++len]=nod; + +} + + + +inline void Dfs2(int nod) + +{ + + viz[nod]=false; sol[nod]=0; sol[non(nod)]=1; + + vector ::iterator it; + + for(it=T[nod].begin();it!=T[nod].end();++it) + + if(viz[*it]) Dfs2(*it); + + + +} + + + +int main() + +{ + + int m,x,y,tip,i,cnt; + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + scanf("%d%d", &n,&m); + + while(m--) + + { + + scanf("%d%d%d", &x,&y,&tip); + + if(!tip) + + { + + L[non(x)].push_back(y); + + L[non(y)].push_back(x); + + T[y].push_back(non(x)); + + T[x].push_back(non(y)); + + } + + else + + if(tip==1) + + { + + L[y].push_back(x); + + T[x].push_back(y); + + } + + else + + if(tip==2) + + { + + L[x].push_back(y); + + T[y].push_back(x); + + } + + else + + { + + L[y].push_back(non(x)); + + L[x].push_back(non(y)); + + T[non(x)].push_back(y); + + T[non(y)].push_back(x); + + } + + } + + for(i=1;i<=2*n;++i) + + if(!viz[i]) Dfs(i); + + reverse(Sorted+1,Sorted+len+1); + + for(i=1;i<=len;++i) + + if(viz[Sorted[i]] && viz[non(Sorted[i])]) Dfs2(Sorted[i]); + + for(i=1,cnt=0;i<=n;++i) cnt+=(sol[i]==1); + + printf("%d\n", cnt); + + for(i=1;i<=n;++i) + + if(sol[i]) printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/64bbac05-02b2-11ec-8f86-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/64bbac05-02b2-11ec-8f86-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a3038b85f8aba8fb040495edaccb6ca93edb0f3 --- /dev/null +++ b/party/2sat_implication_reduction/64bbac05-02b2-11ec-8f86-2cf05d84bfc2.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector L[505],T[505]; + +int Sorted[505],len,sol[505],n; + +bool viz[505]; + + + +inline int non(int x) + +{ + + if(x>n) return x-n; + + return x+n; + +} + + + +inline void Dfs(int nod) + +{ + + viz[nod]=true; + + vector ::iterator it; + + for(it=L[nod].begin();it!=L[nod].end();++it) + + if(!viz[*it]) Dfs(*it); + + Sorted[++len]=nod; + +} + + + +inline void Dfs2(int nod) + +{ + + viz[nod]=false; sol[nod]=0; sol[non(nod)]=1; + + vector ::iterator it; + + for(it=T[nod].begin();it!=T[nod].end();++it) + + if(viz[*it]) Dfs2(*it); + + + +} + + + +int main() + +{ + + int m,x,y,tip,i,cnt; + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + scanf("%d%d", &n,&m); + + while(m--) + + { + + scanf("%d%d%d", &x,&y,&tip); + + if(!tip) + + { + + L[non(x)].push_back(y); + + L[non(y)].push_back(x); + + T[y].push_back(non(x)); + + T[x].push_back(non(y)); + + } + + else + + if(tip==1) + + { + + L[y].push_back(x); + + T[x].push_back(y); + + } + + else + + if(tip==2) + + { + + L[x].push_back(y); + + T[y].push_back(x); + + } + + else + + { + + L[y].push_back(non(x)); + + L[x].push_back(non(y)); + + T[non(x)].push_back(y); + + T[non(y)].push_back(x); + + } + + } + + for(i=1;i<=2*n;++i) + + if(!viz[i]) Dfs(i); + + reverse(Sorted+1,Sorted+len+1); + + for(i=1;i<=len;++i) + + if(viz[Sorted[i]] && viz[non(Sorted[i])]) Dfs2(Sorted[i]); + + for(i=1,cnt=0;i<=n;++i) cnt+=(sol[i]==1); + + printf("%d\n", cnt); + + for(i=1;i<=n;++i) + + if(sol[i]) printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/64da308a-02b2-11ec-b16b-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/64da308a-02b2-11ec-b16b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9dfdfd6ad0b315e8bc49368d18844a29d2a52d92 --- /dev/null +++ b/party/2sat_implication_reduction/64da308a-02b2-11ec-b16b-2cf05d84bfc2.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +using namespace std; + + + +long n,m,i,nn,x,y,op,cnt; +vector gIn[2011],gOut[2011]; +vector Comp[2011],tmp; +bool vis[2011]; +long ctc[2011],col[2011]; + +stack S; + +inline void addImp(long a,long b){ + a+=nn; b+=nn; + gIn[a].push_back(b); + gOut[b].push_back(a); +} + +inline void aORb(long a,long b){ + addImp(-a,b); + addImp(-b,a); +} + +void dfsF(long node){ + vis[node] = true; + for(long i=0;i +#include +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int M,N; + +vector G[1005],GT[1005]; + +int O[1005],Solution[1005],K; + +bool Use[1005]; + +inline int Node(int x) + +{ + + if(x<0) + + return N-x; + + return x; + +} + +inline int Non(int x) + +{ + + if(x<=N) + + return x+N; + + else + + return x-N; + +} + +void fillGraph(int x,int y) + +{ + + GT[x].push_back(Non(y)); + + GT[y].push_back(Non(x)); + + G[Non(x)].push_back(y); + + G[Non(y)].push_back(x); + +} + +void Read() + +{ + + int i,type; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { + + int x,y; + + f>>x>>y>>type; + + if(type==1) + + y*=-1; + + if(type==2) + + x*=-1; + + if(type==3) + + { + + x*=-1; + + y*=-1; + + } + + x=Node(x); + + y=Node(y); + + fillGraph(x,y); + + } + +} + +void DFP(int Nod) + +{ + + Use[Nod]=1; + + for(unsigned int i=0;i=1;i--) + + { + + if(!Use[O[i]] && !Use[Non(O[i])]) + + DFM(O[i]); + + } + +} + +void Print() + +{ + + int i; + + int result=0; + + if(Solution[0]==-1) + + { + + g<<0<<"\n"; + + return; + + } + + for(i=1;i<=N;i++) + + if(Solution[i]==1) + + result++; + + g< +#include + + +using namespace std; + + + +vector v[210],vt[210]; + +int q[210],n,nr; + +char vaz[210],sol[210]; + + + +int non(int x) + +{ + + if(x<=n) return x+n; + + else return x-n; + +} + + + +void add_edge(int x,int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs1(int nod) + +{ + + vaz[nod]=1; + + for(vector::iterator it=v[nod].begin();it!=v[nod].end();it++) + + if(!vaz[*it]) dfs1(*it); + + q[++nr]=nod; + +} + + + +void dfs2(int nod) + +{ + + vaz[nod]=sol[nod]=0; + + sol[non(nod)]=1; + + for(vector::iterator it=vt[nod].begin();it!=vt[nod].end();it++) + + if(vaz[*it]) dfs2(*it); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int m,x,y,a; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&a); + + if(a==0) + + { + + add_edge(non(x),y); + + add_edge(non(y),x); + + } + + else if(a==1) + + { + + add_edge(non(x),non(y)); + + add_edge(y,x); + + } + + else if(a==2) + + { + + add_edge(x,y); + + add_edge(non(y),non(x)); + + } + + else + + { + + add_edge(x,non(y)); + + add_edge(y,non(x)); + + } + + } + + for(int i=1;i<=2*n;i++) if(!vaz[i]) dfs1(i); + + for(int i=nr;i;i--) if(vaz[q[i]] && vaz[non(q[i])]) dfs2(q[i]); + + nr=0; + + for(int i=1;i<=n;i++) if(sol[i]) nr++; + + printf("%d\n",nr); + + for(int i=1;i<=n;i++) if(sol[i]) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6570f232-02b2-11ec-8175-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6570f232-02b2-11ec-8175-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bb50e152454d06b932e9ecba7be04e85235a52e --- /dev/null +++ b/party/2sat_implication_reduction/6570f232-02b2-11ec-8175-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + + + +vector v[210],vt[210]; + +int q[210],n,nr; + +char vaz[210],sol[210]; + + + +int non(int x) + +{ + + if(x<=n) return x+n; + + else return x-n; + +} + + + +void add_edge(int x,int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs1(int nod) + +{ + + vaz[nod]=1; + + for(vector::iterator it=v[nod].begin();it!=v[nod].end();it++) + + if(!vaz[*it]) dfs1(*it); + + q[++nr]=nod; + +} + + + +void dfs2(int nod) + +{ + + vaz[nod]=sol[nod]=0; + + sol[non(nod)]=1; + + for(vector::iterator it=vt[nod].begin();it!=vt[nod].end();it++) + + if(vaz[*it]) dfs2(*it); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int m,x,y,a; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&a); + + if(a==0) + + { + + add_edge(non(x),y); + + add_edge(non(y),x); + + } + + else if(a==1) + + { + + add_edge(non(x),non(y)); + + add_edge(y,x); + + } + + else if(a==2) + + { + + add_edge(x,y); + + add_edge(non(y),non(x)); + + } + + else + + { + + add_edge(x,non(y)); + + add_edge(y,non(x)); + + } + + } + + for(int i=1;i<=2*n;i++) if(!vaz[i]) dfs1(i); + + for(int i=nr;i;i--) if(vaz[q[i]] && vaz[non(q[i])]) dfs2(q[i]); + + nr=0; + + for(int i=1;i<=n;i++) if(sol[i]) nr++; + + printf("%d\n",nr); + + for(int i=1;i<=n;i++) if(sol[i]) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/65ae1f8e-02b2-11ec-95f3-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/65ae1f8e-02b2-11ec-95f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa8d72a339726437a8db310fab6e58b656250273 --- /dev/null +++ b/party/2sat_implication_reduction/65ae1f8e-02b2-11ec-95f3-2cf05d84bfc2.cpp @@ -0,0 +1,209 @@ +#include +#include + + +using namespace std; + + + +int M, n_ans, ans[109]; + + + +class SAT + +{ + + public: + + + + int n_Q, n_componente, N, sol[209], ap[209], Q[209]; + + vector < int > v[209], h[209]; + + + + int cod (int v) + + { + + if (v < 0) return N - v; + + return v; + + } + + + + int opus (int x) + + { + + if (x<=N) return N + x; + + return x - N; + + } + + + + void Add_Implication (int x, int y) + + { + + v [ cod ( x ) ] . push_back ( cod ( y ) ); + + h [ cod ( y ) ] . push_back ( cod ( x ) ); + + } + + + + void Add_Clauza (int x, int y) + + { + + Add_Implication (-x, y); + + Add_Implication (-y, x); + + } + + + + void dfs (int nod) + + { + + ap[nod] = 1; + + for (vector < int > :: iterator it = v[nod].begin(); it != v[nod].end(); it++) + + if (ap[*it] == 0) + + dfs (*it); + + Q[++n_Q] = nod; + + } + + + + void dfp (int nod) + + { + + if (sol[nod]) + + { + + sol[0] = -1; + + return ; + + } + + + + ap[nod] = 0; + + sol[opus(nod)] = 1; + + for (vector < int > :: iterator it = h[nod].begin(); it != h[nod].end(); it++) + + if (ap[*it] == 1) + + dfp (*it); + + } + + + + int solve () + + { + + for (int i=1; i<=2*N; i++) + + if (ap[i] == 0) + + dfs (i); + + + + for (int i=n_Q; i>=1; i--) + + if (ap[Q[i]] && ap[opus(Q[i])]) + + dfp (Q[i]); + + + + return sol[0]; + + } + +}sistem; + + + +int main() + +{ + +freopen ("party.in", "r", stdin); + +freopen ("party.out", "w", stdout); + + + +scanf ("%d %d", &sistem.N, &M); + +for (int i=1; i<=M; i++) + +{ + + int X, Y, tip; + + scanf ("%d %d %d", &X, &Y, &tip); + + if (tip == 0) sistem . Add_Clauza (X, Y); + + else + + if (tip == 1) sistem . Add_Clauza (-Y, X); + + else + + if (tip == 2) sistem . Add_Clauza (-X, Y); + + else sistem . Add_Clauza (-X, -Y); + +} + + + +sistem.solve(); + + + +for (int i=1; i<=sistem.N; i++) + + if (sistem.sol[i]) + + ans[++n_ans] = i; + + + +printf ("%d\n", n_ans); + +for (int i=1; i<=n_ans; i++) + + printf ("%d\n", ans[i]); + + + +return 0; + +} diff --git a/party/2sat_implication_reduction/65f11bf9-02b2-11ec-b5e9-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/65f11bf9-02b2-11ec-b5e9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a56f2b006c3a3e85a365ead99cb36bcf92186a44 --- /dev/null +++ b/party/2sat_implication_reduction/65f11bf9-02b2-11ec-b5e9-2cf05d84bfc2.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("party.in"); + +ofstream fout ("party.out"); + + + +int low[210],niv[210],w[210]; + +int n,m,sol,t,x,y,g,i,j; + +set r; + +stack s; + +vector l[210]; + + + +void dfs (int nod) { + + + + niv[nod]=low[nod]=++g; + + s.push(nod); + + for (int i=0;i0) + + low[nod]=min(low[nod],low[l[nod][i]]); + + } + + if (low[nod]==niv[nod]) { + + r.clear(); + + do { + + x=s.top(); + + s.pop(); + + r.insert(x); + + + + if (w[x]==0){ + + w[x]=1; + + if (x<=n) + + w[x+n]=-1; + + else + + w[x-n]=-1; + + } + + niv[x]*=-1; + + }while (x!=nod); + + } + +} + + + +int main () { + + + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y>>t; + + if (t==1){ + + y*=-1; + + }else{ + + if (t==2) + + x*=-1; + + else + + if (t==3){ + + x*=-1; + + y*=-1; + + } + + } + + + + if (x>0){ + + if (y>0) { + + l[x+n].push_back(y); + + l[y+n].push_back(x); + + }else { + + l[x+n].push_back(-y+n); + + l[-y].push_back(x); + + } + + }else{ + + if (y>0) { + + l[-x].push_back(y); + + l[y+n].push_back(-x+n); + + }else{ + + l[-x].push_back(-y+n); + + l[-y].push_back(-x+n); + + } + + } + + } + + + + for (i=1;i<=2*n;i++) + + if (niv[i]==0) + + dfs(i); + + + + for (i=1;i<=n;i++) + + if (w[i]==1) + + sol++; + + fout< +#include +#include +#include +using namespace std; + + + +const int NMAX = 210; + + + +int N, M, X, Y, Type, Value[NMAX], Stack[NMAX], Top; + +bool Used[NMAX]; + +vector G[NMAX], GT[NMAX]; + + + +int Neg(int X) + +{ + + if(X <= N) return X + N; + + return X - N; + +} + + + +void DFP(int Node) + +{ + + Used[Node] = 1; + + for(auto it : G[Node]) + + if(!Used[it]) + + DFP(it); + + Stack[++Top] = Node; + +} + + + +void DFM(int Node) + +{ + + Used[Node] = 1; + + Value[Neg(Node)] = 1; + + + + for(auto it : GT[Node]) + + if(!Used[it]) + + DFM(it); + +} + + + +void AddEdge(int A, int B) + +{ + + G[Neg(A)].push_back(B); + + G[Neg(B)].push_back(A); + + GT[A].push_back(Neg(B)); + + GT[B].push_back(Neg(A)); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%i %i", &N, &M); + + for(int i = 1; i <= M; ++ i) + + { + + scanf("%i %i %i", &X, &Y, &Type); + + if(Type == 0) AddEdge(X, Y); + + else if(Type == 1) AddEdge(X, Neg(Y)); + + else if(Type == 2) AddEdge(Neg(X), Y); + + else AddEdge(Neg(X), Neg(Y)); + + } + + + + for(int i = 1; i <= N; ++ i) + + if(!Used[i]) + + DFP(i); + + + + memset(Used, 0, sizeof(Used)); + + + + for(int i = Top; i; -- i) + + if(!Used[ Stack[i] ] && !Used[ Neg(Stack[i]) ]) + + DFM(Stack[i]); + + + + int Cnt = 0; + + for(int i = 1; i <= N; ++ i) + + Cnt += Value[i]; + + + + printf("%i\n", Cnt); + + for(int i = 1; i <= N; ++ i) + + if(Value[i]) + + printf("%i\n", i); + +} diff --git a/party/2sat_implication_reduction/664e572a-02b2-11ec-8b1d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/664e572a-02b2-11ec-8b1d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bb50e152454d06b932e9ecba7be04e85235a52e --- /dev/null +++ b/party/2sat_implication_reduction/664e572a-02b2-11ec-8b1d-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + + + +vector v[210],vt[210]; + +int q[210],n,nr; + +char vaz[210],sol[210]; + + + +int non(int x) + +{ + + if(x<=n) return x+n; + + else return x-n; + +} + + + +void add_edge(int x,int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + + + +void dfs1(int nod) + +{ + + vaz[nod]=1; + + for(vector::iterator it=v[nod].begin();it!=v[nod].end();it++) + + if(!vaz[*it]) dfs1(*it); + + q[++nr]=nod; + +} + + + +void dfs2(int nod) + +{ + + vaz[nod]=sol[nod]=0; + + sol[non(nod)]=1; + + for(vector::iterator it=vt[nod].begin();it!=vt[nod].end();it++) + + if(vaz[*it]) dfs2(*it); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int m,x,y,a; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&a); + + if(a==0) + + { + + add_edge(non(x),y); + + add_edge(non(y),x); + + } + + else if(a==1) + + { + + add_edge(non(x),non(y)); + + add_edge(y,x); + + } + + else if(a==2) + + { + + add_edge(x,y); + + add_edge(non(y),non(x)); + + } + + else + + { + + add_edge(x,non(y)); + + add_edge(y,non(x)); + + } + + } + + for(int i=1;i<=2*n;i++) if(!vaz[i]) dfs1(i); + + for(int i=nr;i;i--) if(vaz[q[i]] && vaz[non(q[i])]) dfs2(q[i]); + + nr=0; + + for(int i=1;i<=n;i++) if(sol[i]) nr++; + + printf("%d\n",nr); + + for(int i=1;i<=n;i++) if(sol[i]) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/666cb496-02b2-11ec-aebc-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/666cb496-02b2-11ec-aebc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e0ea2351386104c5482cba58414db54fc4b138b --- /dev/null +++ b/party/2sat_implication_reduction/666cb496-02b2-11ec-aebc-2cf05d84bfc2.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include + + +using namespace std; + + + +class P2SAT { + +public: + + P2SAT(const int N) { + + Size = 2 * N + 2; + + Offset = N; + + G = vector< vector >(Size); + + Gt = G; + + values = vector(Size, 0); + + used = vector(Size, false); + + } + + + + void addRel(int x, int y) { + + addEdge(Offset - x, Offset + y); + + addEdge(Offset - y, Offset + x); + + } + + + + void addMust(int x) { + + addEdge(Size - 1, Offset + x); + + addEdge(Offset - x, Offset); + + } + + + + vector getValues() { + + stk.reserve(Size); + + for (int i = 0; i < Size; ++i) + + if (!used[i]) + + Dfs1(i); + + + + fill(used.begin(), used.end(), false); + + Dfs2(Offset); + + for (int i = Size - 1; i >= 0; --i) + + if (!values[stk[i]] && !values[Non(stk[i])]) + + Dfs2(stk[i]); + + + + stk.clear(); + + if (values[Offset] == -1) return vector(1, -1); + + for (int i = 0; i <= Offset; ++i) + + if (values[i] == values[Non(i)]) return vector(1, -1); + + + + vector ret(Offset + 1); + + for (int i = 1; i <= Offset; ++i) + + ret[i] = values[Offset + i]; + + + + return ret; + + } + + + +private: + + vector< vector > G, Gt; + + vector values, stk; + + vector used; + + int Offset, Size; + + + + void addEdge(int x, int y) { + + G[x].push_back(y); + + Gt[y].push_back(x); + + } + + + + void Dfs1(const int node) { + + used[node] = 1; + + for (int p: G[node]) + + if (!used[p]) Dfs1(p); + + + + stk.push_back(node); + + } + + + + void Dfs2(const int node) { + + used[node] = 1; + + if (values[node]) + + values[Offset] = -1; + + + + values[Non(node)] = 1; + + for (int p: Gt[node]) + + if (!used[p]) Dfs2(p); + + } + + + + int Non(const int n) const { + + if (n == Offset) return Size - 1; + + if (n == Size - 1) return Offset; + + return 2 * Offset - n; + + } + +}; + + + +vector getNonZero(const vector& A) { + + vector ret; + + for (int i = 1; i < int(A.size()); ++i) + + if (A[i]) + + ret.push_back(i); + + return ret; + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + int N, M; + + scanf("%d%d", &N, &M); + + + + P2SAT S(N); + + while (M--) + + { + + int x, y, op; + + scanf("%d%d%d", &x, &y, &op); + + + + if (op == 1) y = -y; + + else if (op == 2) x = -x; + + else if (op == 3) + + { + + x = -x; + + y = -y; + + } + + + + S.addRel(x, y); + + } + + + + vector Ans = getNonZero(S.getValues()); + + + + printf("%u\n", Ans.size()); + + for (const int p: Ans) printf("%d\n", p); + +} diff --git a/party/2sat_implication_reduction/66ab6b44-02b2-11ec-8ac5-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/66ab6b44-02b2-11ec-8ac5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f51ec5d24f13540011636b07d0b74c111c3d25b2 --- /dev/null +++ b/party/2sat_implication_reduction/66ab6b44-02b2-11ec-8ac5-2cf05d84bfc2.cpp @@ -0,0 +1,159 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + + + + + +int n,m,i,x,y,z,st,nr; + +vector G[210]; + +int stack[210]; + +bool X[210],viz[210]; +void dfs(int nod) + +{ + + (viz+100)[nod]=true; + + for(vector:: iterator it=(G+100)[nod].begin();it!=(G+100)[nod].end();++it) + + if(! (viz+100)[*it]) dfs(*it); + + + + stack[++st]=nod; + +} + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>z; + + switch(z) + + { + + case 0: + + { + + + + (G+100)[-x].push_back(y); + + (G+100)[-y].push_back(x); + + break; + + } + + case 1: + + { + + + + (G+100)[-x].push_back(-y); + + (G+100)[y].push_back(x); + + break; + + } + + case 2: + + { + + + + (G+100)[x].push_back(y); + + (G+100)[-y].push_back(-x); + + break; + + } + + case 3: + + { + + + + (G+100)[x].push_back(-y); + + (G+100)[y].push_back(-x); + + break; + + } + + } + + } + + + + for(i=-n;i<=n;++i) + + if(!(viz+100)[i]) dfs(i); + + + + for(;st;--st) + + { + + if(!(X+100)[stack[st]] && !(X+100)[-stack[st]]) (X+100)[-stack[st]]=true; + + } + + + + for(i=1;i<=n;++i) + + { + + if ((X+100)[i]) ++nr; + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +vector L[205]; + + + +stack s; + + + +int n, m, nivel, x, y, op; + + + +int Niv[205], Low[205], R[205]; + + + +int sol, Sol[205]; + + + +void DFS (int nod) { + + Niv[nod] = Low[nod] = ++nivel; + + s.push(nod); + + for (vector::iterator it=L[nod].begin(); it!=L[nod].end(); ++it) { + + if (Niv[*it] == 0) + + DFS(*it); + + if (Niv[*it]>0 && Low[nod]>Low[*it]) + + Low[nod] = Low[*it]; + + } + + if (Low[nod] == Niv[nod]) { + + int nnod; + + do { + + nnod = s.top(); + + s.pop(); + + Niv[nnod] *= -1; + + if (R[nnod] == 0) { + + R[nnod] = 1; + + if (nnod <= n) + + R[nnod + n] = -1; + + else + + R[nnod - n] = -1; + + } + + } while (nnod != nod); + + } + +} + + + +int main () { + + f >> n >> m; + + for (int i=1; i<=m; ++i) { + + f >> x >> y >> op; + + if (op == 1) { + + L[y].push_back(x); + + L[x+n].push_back(y+n); + + } + + else + + if (op == 2) { + + L[x].push_back(y); + + L[y+n].push_back(x+n); + + } + + else + + if (op == 3) { + + L[x].push_back(y+n); + + L[y].push_back(x+n); + + } + + else + + if (op == 0) { + + L[x+n].push_back(y); + + L[y+n].push_back(x); + + } + + } + + for (int i=1; i<=n; ++i) + + if (Niv[i] == 0) + + DFS (i); + + for (int i=1; i<=n; ++i) + + if (R[i] == 1) + + Sol[++sol] = i; + + g << sol << "\n"; + + for (int i=1; i<=sol; ++i) + + g << Sol[i] << "\n"; + + return 0; + +} diff --git a/party/2sat_implication_reduction/67085846-02b2-11ec-8bc4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/67085846-02b2-11ec-8bc4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4390556bd64d84b903957d544934d6a839c58e5 --- /dev/null +++ b/party/2sat_implication_reduction/67085846-02b2-11ec-8bc4-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 105; + +vector graph[NMAX * 2], trans[NMAX * 2]; +int n, val[NMAX * 2], vis[NMAX * 2]; +stack st; + +inline int neg (int x) { + return x > n ? x - n : x + n; +} + +void dfsPlus (int node) { + vis[node] = 1; + for(vector ::iterator it = graph[node].begin(); it != graph[node].end(); ++ it) + if(!vis[*it]) + dfsPlus(*it); + st.push(node); +} + +void dfsMinus (int node) { + vis[node] = 1; + val[neg(node)] = 1; + + for(vector ::iterator it = trans[node].begin(); it != trans[node].end(); ++ it) + if(!vis[*it]) + dfsMinus(*it); +} + +void sau (int x, int y) { + graph[neg(x)].push_back(y); + trans[y].push_back(neg(x)); + + graph[neg(y)].push_back(x); + trans[x].push_back(neg(y)); +} + +int main() { + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + int m, i, j, a, b, c; + + scanf("%d%d", &n, &m); + for(i = 1; i <= m; ++ i) { + scanf("%d%d%d", &a, &b, &c); + if(c == 0) + sau(a, b); + + if(c == 1) + sau(a, neg(b)); + + if(c == 2) + sau(neg(a), b); + + if(c == 3) + sau(neg(a), neg(b)); + } + + for(i = 1; i <= 2 * n; ++ i) + if(!vis[i]) + dfsPlus(i); + + memset(vis, 0, sizeof(vis)); + while(!st.empty()) { + if(!vis[st.top()] && !vis[neg(st.top())]) { + + dfsMinus(st.top()); + } + st.pop(); + } + + c = 0; + for(i = 1; i <= n; ++ i) + c += val[i]; + + printf("%d\n", c); + for(i = 1; i <= n; ++ i) + if(val[i]) + printf("%d\n", i); + return 0; +} diff --git a/party/2sat_implication_reduction/670aed9a-7c6b-4a69-b390-facbc5dc05af.cpp b/party/2sat_implication_reduction/670aed9a-7c6b-4a69-b390-facbc5dc05af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/670aed9a-7c6b-4a69-b390-facbc5dc05af.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/675df25a-02b2-11ec-917d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/675df25a-02b2-11ec-917d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e5c0432e7fec768b1cf44d892e702ecedf3b6e3 --- /dev/null +++ b/party/2sat_implication_reduction/675df25a-02b2-11ec-917d-2cf05d84bfc2.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +using namespace std; +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +const int MAXN = 105; + +vector G[2 * MAXN], actual_component; + +vector< vector > Components; + +stack S; + +int N, M, indexCount, indx[2 * MAXN], level[2 * MAXN]; + +bool in_stack[2 * MAXN], TruthValue[MAXN]; +inline int non(int x) { + + return x > N ? x - N : x + N; + +} + + + +void Read() { + + fin >> N >> M; + + int x, y, cod; + + for (int i = 0; i < M; ++i) { + + fin >> x >> y >> cod; + + switch (cod) { + + case 1: + + y = non(y); + + break; + + case 2: + + x = non(x); + + break; + + case 3: + + x = non(x); + + y = non(y); + + break; + + default: + + break; + + } + + G[ non(x) ].push_back(y); + + G[ non(y) ].push_back(x); + + } + +} + + + +void Tarjan(int father) { + + indx[father] = level[father] = indexCount++; + + S.push(father); in_stack[father] = true; + + vector::iterator it; + + for (it = G[father].begin(); it != G[father].end(); ++it) { + + if (indx[*it] == -1) { + + Tarjan(*it); + + level[father] = min(level[father], level[*it]); + + }else if (in_stack[*it]) { + + level[father] = min(level[father], level[*it]); + + } + + } + + if (indx[father] == level[father]) { + + actual_component.clear(); + + int node; + + do { + + node = S.top(); + + S.pop(); in_stack[node] = false; + + actual_component.push_back(node); + + }while (father != node); + + Components.push_back( actual_component ); + + } + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + indx[i] = -1; + + for (int i = 1; i <= 2 * N; ++i) { + + if (indx[i] == -1) + + Tarjan(i); + + } + + for (int i = Components.size() - 1; i >= 0; --i) { + + for (size_t j = 0; j < Components[i].size(); ++j) { + + if (not TruthValue[ Components[i][j] ] && not TruthValue[ non(Components[i][j]) ]) { + + TruthValue[ Components[i][j] ] = false; + + TruthValue[ non(Components[i][j]) ] = true; + + } + + } + + } + +} + + + +void Write() { + + int Sol = 0; + + for (int i = 1; i <= N; ++i) + + if (TruthValue[i] == true) + + ++Sol; + + fout << Sol << '\n'; + + for (int i = 1; i <= N; ++i) + + if (TruthValue[i] == true) + + fout << i << '\n'; + +} + + + +int main() { + + Read(); + + Solve(); + + Write(); + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/677cc510-02b2-11ec-825e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/677cc510-02b2-11ec-825e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ddf645108cf80d9e52ddf74e196ba8b025023be --- /dev/null +++ b/party/2sat_implication_reduction/677cc510-02b2-11ec-825e-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +typedef struct celula { + + int nod; + + celula *next; + + }*lista; + +lista graf[205],v,tgraf[205]; + +int x,y,op,n,m,i,stiva[205],st,comp[205],nrc,assign[205]; + +bool viz[205]; + + + +int negat(int x) { + + if (x>n) return x-n; + + else return x+n; + +} + + + +void dfs(int nod) { + + viz[nod]=1; + + for (lista p=graf[nod]; p; p=p->next) + + if (viz[p->nod]==0) dfs(p->nod); + + + + ++st; stiva[st]=nod; + + + +} + + + +void dfs2(int nod) { + + comp[nod]=nrc; + + assign[negat(nod)]=1; + + for (lista p=tgraf[nod]; p; p=p->next) + + if (comp[p->nod]==0) dfs2(p->nod); + +} + + + +int main(void) { + + ifstream fin("party.in"); + + ofstream fout("party.out"); + + + + fin>>n>>m; + + + + for (i=1; i<=m; ++i){ + + fin>>x>>y>>op; + + if (op==1) y=negat(y); + + else if (op==2) x=negat(x); + + else if (op==3) { x=negat(x); y=negat(y); } + + + + int xn=negat(x), yn=negat(y); + + + + v=new celula; v->nod=y; v->next=graf[xn]; graf[xn]=v; + + v=new celula; v->nod=x; v->next=graf[yn]; graf[yn]=v; + + + + v=new celula; v->nod=xn; v->next=tgraf[y]; tgraf[y]=v; + + v=new celula; v->nod=yn; v->next=tgraf[x]; tgraf[x]=v; + + } + + + + + + for (i=1; i<=2*n; ++i) + + if (viz[i]==0) dfs(i); + + + + for (i=st; i>=1; --i) + + if ( comp[stiva[i]]==0 && comp[negat(stiva[i])]==0 ) { ++nrc; dfs2(stiva[i]); } + + + + int sol=0; + + for (i=1; i<=n; ++i) sol+=assign[i]; + + fout< +#include +#include +#include +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,gr,nr,pas; + + + +int low[111*2],niv[111*2],V[2*111]; + + + +list L[2*111]; + +stack S; + +bitset<111*2> viz,IS; + + + +int val(int x){ + + return (x%2==0?-1:1); + +} + + + +void dfs(int nod){ + + int x,q; + + list::iterator it; + + low[nod]=niv[nod]=++gr; + + S.push(nod); + + viz[nod]=IS[nod]=1; + + + + for(it=L[nod].begin();it!=L[nod].end();it++){ + + if(!viz[*it]){ + + dfs(*it); + + low[nod]=min(low[nod],low[*it]); + + } + + else if(IS[*it]) + + low[nod]=min(low[nod],low[*it]); + + } + + + + if(low[nod]==niv[nod]){ + + do{ x=S.top(),S.pop(); + + IS[x]=0; + + if(!V[x]) + + V[x]=1,(x<=n?V[x+n]=-1,nr++:V[x-n]=-1); + + }while(x!=nod); + + } + +} + + + +int main(void){ + + register int i,j,x,y,z; + + + + f>>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y>>z; + + switch(z){ + + case 0: + + L[x+n].push_back(y); + + L[y+n].push_back(x); + + break; + + case 1: + + L[x+n].push_back(y+n); + + L[y].push_back(x); + + break; + + case 2: + + L[x].push_back(y); + + L[y+n].push_back(x+n); + + break; + + default: + + L[x].push_back(y+n); + + L[y].push_back(x+n); + + } + + } + + + + for(i=1;i<=2*n;i++) if(!viz[i]) dfs(i); + + + + g<0) g< +using namespace std; + +typedef struct celula { + + int nod; + + celula *next; + + }*lista; + +lista graf[205],v,tgraf[205]; + +int x,y,op,n,m,i,stiva[205],st,comp[205],nrc,assign[205]; + +bool viz[205]; + + + +int negat(int x) { + + if (x>n) return x-n; + + else return x+n; + +} + + + +void dfs(int nod) { + + viz[nod]=1; + + for (lista p=graf[nod]; p; p=p->next) + + if (viz[p->nod]==0) dfs(p->nod); + + + + ++st; stiva[st]=nod; + + + +} + + + +void dfs2(int nod) { + + comp[nod]=nrc; + + assign[negat(nod)]=1; + + for (lista p=tgraf[nod]; p; p=p->next) + + if (comp[p->nod]==0) dfs2(p->nod); + +} + + + +int main(void) { + + ifstream fin("party.in"); + + ofstream fout("party.out"); + + + + fin>>n>>m; + + + + for (i=1; i<=m; ++i){ + + fin>>x>>y>>op; + + if (op==1) y=negat(y); + + else if (op==2) x=negat(x); + + else if (op==3) { x=negat(x); y=negat(y); } + + + + int xn=negat(x), yn=negat(y); + + + + v=new celula; v->nod=y; v->next=graf[xn]; graf[xn]=v; + + v=new celula; v->nod=x; v->next=graf[yn]; graf[yn]=v; + + + + v=new celula; v->nod=xn; v->next=tgraf[y]; tgraf[y]=v; + + v=new celula; v->nod=yn; v->next=tgraf[x]; tgraf[x]=v; + + } + + + + + + for (i=1; i<=2*n; ++i) + + if (viz[i]==0) dfs(i); + + + + for (i=st; i>=1; --i) + + if ( comp[stiva[i]]==0 && comp[negat(stiva[i])]==0 ) { ++nrc; dfs2(stiva[i]); } + + + + int sol=0; + + for (i=1; i<=n; ++i) sol+=assign[i]; + + fout< +#include +#include +#include + +using namespace std; + +const int Nmax = 105; + +vector G[2 * Nmax], Gt[2 * Nmax]; +bitset<2 * Nmax> V; +int SortedNodes[2 * Nmax], Cc[2 * Nmax]; + +int N, P, Ncc; + +inline int Non(const int x) +{ + return (x > N ? x - N: x + N); +} + +void Dfs(const int node) +{ + V[node] = 1; + for (const int next: G[node]) + if (!V[next]) + Dfs(next); + + SortedNodes[++P] = node; +} + +void Dfs1(const int node) +{ + Cc[node] = Ncc; + for (const int next: Gt[node]) + if (!Cc[next]) + Dfs1(next); +} + +int main() +{ + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int M; + scanf("%d%d", &N, &M); + + while (M--) + { + int x, y, op; + scanf("%d%d%d", &x, &y, &op); + + if (op == 1) y = Non(y); + else if (op == 2) x = Non(x); + else if (op == 3) + { + x = Non(x); + y = Non(y); + } + + G[Non(x)].push_back(y); + G[Non(y)].push_back(x); + Gt[x].push_back(Non(y)); + Gt[y].push_back(Non(x)); + } + + for (int i = 1; i <= 2 * N; i++) + if (!V[i]) + Dfs(i); + + for (int i = 2 * N; i; i--) + { + const int node = SortedNodes[i]; + + if (!Cc[node]) + { + Ncc++; + Dfs1(node); + } + } + + vector Ans; + for (int i = 1; i <= N; i++) + if (Cc[i] > Cc[Non(i)]) + Ans.push_back(i); + + printf("%u\n", Ans.size()); + for (const int p: Ans) printf("%d\n", p); +} diff --git a/party/2sat_implication_reduction/69334f60-02b2-11ec-8376-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/69334f60-02b2-11ec-8376-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..779abbe2a478041abf51b4dcbdcdb67c356f4aac --- /dev/null +++ b/party/2sat_implication_reduction/69334f60-02b2-11ec-8376-2cf05d84bfc2.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + +vector>lv,lv2; + +vectornoduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + +int neg(int x) { + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + +void sorttop(int nod) { + + int i; + + v[nod]=1; + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6a623815-02b2-11ec-813f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6a623815-02b2-11ec-813f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f31f1037bef5baad5a970489a2bf7292b22737f6 --- /dev/null +++ b/party/2sat_implication_reduction/6a623815-02b2-11ec-813f-2cf05d84bfc2.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin ("party.in"); + +ofstream fout ("party.out"); + + + +const int Zero = 105, N = 210; + + + +int sol[N], m, n; + +vector g[N], gT[N], o; + +vector viz (N, 0); + + + +void dfs(int x) { + + viz[x] = 1; + + for (vector :: iterator it = g[x].begin(); it != g[x].end(); ++it) + + if (!viz[*it]) + + dfs (*it); + + o.push_back (x); + +} + + + +void dfsT(int x) { + + viz[x] = 0; + + sol[x] = 1; + + sol[Zero * 2 - x] = 0; + + for (vector :: iterator it = gT[x].begin(); it != gT[x].end(); ++it) + + if (viz[*it]) + + dfsT(*it); + +} + + + +int main() { + + fin >> n >> m; + + for (int x, y, t, i = 0; i < m; ++i) { + + fin >> x >> y >> t; + + if (t & 1) + + y = -y; + + if (t & 2) + + x = -x; + + g[Zero - x].push_back (Zero + y); + + gT[Zero + y].push_back (Zero - x); + + g[Zero - y].push_back (Zero + x); + + gT[Zero + x].push_back (Zero - y); + + } + + for (int i = Zero + 1; i <= Zero + n; ++i) + + if (!viz[i]) + + dfs(i); + + for (int i = Zero - 1; i >= Zero - n; --i) + + if (!viz[i]) + + dfs(i); + + for (vector :: reverse_iterator it = o.rbegin(); it != o.rend(); ++it) + + if (viz[*it]) + + dfsT(*it); + + int nr = 0; + + for (int i = 1; i <= n; ++i) + + if (sol[Zero + i]) + + nr++; + + fout << nr << "\n"; + + for (int i = 1; i <= n; ++i) + + if (sol[Zero + i]) + + fout << i << "\n"; + +} diff --git a/party/2sat_implication_reduction/6adf7e68-02b2-11ec-90a2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6adf7e68-02b2-11ec-90a2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0245a14d428a901afe6e624ef12188bc8e77bf1 --- /dev/null +++ b/party/2sat_implication_reduction/6adf7e68-02b2-11ec-90a2-2cf05d84bfc2.cpp @@ -0,0 +1,326 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int N, M, K; + +int MinLevel[210], Level[210], Component[210]; + +int In[210], Out[210]; + +int Simetric[210], Value[210]; + +vector G[210], NG[210]; + +bool InStack[210], E[210][210], Removed[210]; + +stack S; + +queue Q; + + + +int Negate (int x) + +{ + + return (x<=N?x+N:x-N); + +} + + + +void Build (int x, int y) + +{ + + G[Negate(x)].push_back(y); + + G[Negate(y)].push_back(x); + +} + + + +void Read () + +{ + + int x, y, z; + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + f >> x >> y >> z; + + + + if (z>1) x=Negate(x); + + if (z%2) y=Negate(y); + + Build(x, y); + + } + + M=0; + + + + f.close(); + +} + + + +void DoTarjan (int node) + +{ + + Level[node]=MinLevel[node]=++K; + + S.push(node); + + InStack[node]=1; + + + + for (vector::iterator it=G[node].begin(); it!=G[node].end(); ++it) + + { + + if (!Level[*it]) + + { + + DoTarjan(*it); + + MinLevel[node]=min(MinLevel[node], MinLevel[*it]); + + continue; + + } + + if (InStack[*it]) + + MinLevel[node]=min(MinLevel[node], Level[*it]); + + } + + + + if (Level[node]==MinLevel[node]) + + { + + ++M; + + int cnode; + + do { + + cnode=S.top(); + + InStack[S.top()]=0; + + Component[S.top()]=M; + + S.pop(); + + } while (cnode!=node); + + } + +} + + + +void BuildComp () + +{ + + for (int i=1; i<=2*N; i++) + + if (!Level[i]) + + DoTarjan(i); + + + + for (int i=1; i<=2*N; i++) + + for (vector::iterator it=G[i].begin(); it!=G[i].end(); ++it) + + if (Component[i]!=Component[*it]) + + E[Component[i]][Component[*it]]=1; + + + + for (int i=1; i<=2*N; i++) + + { + + if (Component[i]==Component[Negate(i)]) + + { + + g << 0 << '\n'; + + g.close(); + + exit(0); + + } + + + + Simetric[Component[i]]=Component[Negate(i)]; + + } + + + + for (int i=1; i<=M; i++) + + for (int j=1; j<=M; j++) + + if (E[i][j]) + + { + + NG[i].push_back(j); + + Out[i]++; + + In[j]++; + + } + +} + + + +void Solve () + +{ + + for (int i=1; i<=M; i++) + + if (In[i]==0) + + Q.push(i); + + + + while (!Q.empty()) + + { + + int node=Q.front(); + + Q.pop(); + + + + Removed[node]=1; + + Removed[Simetric[node]]=1; + + Value[node]=0; + + Value[Simetric[node]]=1; + + + + for (vector::iterator it=NG[node].begin(); it!=NG[node].end(); ++it) + + if (!Removed[*it]) + + { + + --In[*it]; + + if (In[*it]<=0) + + Q.push(*it); + + } + + } + +} + + + +void Print () + +{ + + int ANS=0; + + + + for (int i=1; i<=N; i++) + + if (Value[Component[i]]==1) + + ANS++; + + + + g << ANS << '\n'; + + + + for (int i=1; i<=N; i++) + + if (Value[Component[i]]==1) + + g << i << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + BuildComp(); + + Solve(); + + Print(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6b90a647-02b2-11ec-923d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6b90a647-02b2-11ec-923d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c6046ba567a3096eec562f36ab36ab80bb13ebe --- /dev/null +++ b/party/2sat_implication_reduction/6b90a647-02b2-11ec-923d-2cf05d84bfc2.cpp @@ -0,0 +1,314 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +class TKosarajuGraph + +{ + + protected: + + int m_iLength; + + int Non (int X); + + public: + + void Kosaraju (); + + void ReadData (FILE * fIn); + + + + int m_vSolution[200005 ]; + + + + int GetLength(); + + private: + + int m_vTSort[200005 ]; + + + + void DFM (int X); + + void DFP (int X); + + + + vector m_vGraph[200005 ], m_vGraphT[200005 ]; + + bool m_vUsed[200005 ]; + +}; + + + + + +int TKosarajuGraph::GetLength() + +{ + + return m_iLength; + +} + + + +int TKosarajuGraph::Non (int X) + +{ + + if (X<=m_iLength) + + { + + return X+m_iLength; + + } + + return X-m_iLength; + +} + + + +void Print (TKosarajuGraph * objGraph) + +{ + + FILE * fOut = fopen("party.out", "w"); + + + + for (int i=1; i<=objGraph->GetLength(); ++i) + + { + + objGraph->m_vSolution[0] +=objGraph->m_vSolution[i]; + + } + + fprintf (fOut,"%d\n", objGraph->m_vSolution[0]); + + for (int i=1; i<=objGraph->GetLength(); ++i) + + { + + if (objGraph->m_vSolution[i]!=0) + + { + + fprintf (fOut,"%d\n", i); + + } + + } + + + +} + + + +void TKosarajuGraph::ReadData (FILE * fIn) + +{ + + int M; + + fscanf (fIn,"%d %d", &m_iLength, &M); + + for (; M>0; --M) + + { + + int X, Y, iType; + + fscanf (fIn,"%d %d %d", &X, &Y, &iType); + + if (iType==0) + + { + + m_vGraph[Non (X)].push_back (Y); + + m_vGraph[Non (Y)].push_back (X); + + m_vGraphT[X].push_back (Non (Y)); + + m_vGraphT[Y].push_back (Non (X)); + + } + + if (iType==1) + + { + + m_vGraph[Non (X)].push_back (Non (Y)); + + m_vGraph[Y].push_back (X); + + m_vGraphT[Non (Y)].push_back (Non (X)); + + m_vGraphT[X].push_back (Y); + + } + + if (iType==2) + + { + + m_vGraph[Non (Y)].push_back (Non (X)); + + m_vGraph[X].push_back (Y); + + m_vGraphT[Non (X)].push_back (Non (Y)); + + m_vGraphT[Y].push_back (X); + + } + + if (iType==3) + + { + + m_vGraph[X].push_back (Non (Y)); + + m_vGraph[Y].push_back (Non (X)); + + m_vGraphT[Non (X)].push_back (Y); + + m_vGraphT[Non (Y)].push_back (X); + + } + + } + +} + + + +void TKosarajuGraph::DFP (int X) + +{ + + m_vUsed[X]=true; + + for (vector :: iterator V=m_vGraph[X].begin (); V!=m_vGraph[X].end (); ++V) + + { + + if (!m_vUsed[*V]) + + { + + DFP (*V); + + } + + } + + m_vTSort[++m_vTSort[0]]=X; + +} + + + +void TKosarajuGraph::DFM (int X) + +{ + + m_vUsed[X]=true; + + m_vSolution[Non (X)]=1; + + for (vector :: iterator V=m_vGraphT[X].begin (); V!=m_vGraphT[X].end (); ++V) + + { + + if (!m_vUsed[*V]) + + { + + DFM (*V); + + } + + } + +} + + + +void TKosarajuGraph::Kosaraju () + +{ + + for (int i=1; i<=this->m_iLength+this->m_iLength; ++i) + + { + + if (!m_vUsed[i]) + + { + + DFP (i); + + } + + } + + memset (m_vUsed, 0, sizeof (m_vUsed)); + + for (int i=m_vTSort[0]; i>0; --i) + + { + + if (!m_vUsed[m_vTSort[i]] and !m_vUsed[Non (m_vTSort[i])]) + + { + + DFM (m_vTSort[i]); + + } + + } + +} + + + +int main() + +{ + + TKosarajuGraph * objGraph = new TKosarajuGraph(); + + + + FILE * fIn = fopen("party.in","r"); + + objGraph->ReadData (fIn); + + objGraph->Kosaraju (); + + Print (objGraph); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6bb1e9e9-02b2-11ec-87e0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6bb1e9e9-02b2-11ec-87e0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6cfb9e3eefd77fd8b0d31d08cf1307d753857ff --- /dev/null +++ b/party/2sat_implication_reduction/6bb1e9e9-02b2-11ec-87e0-2cf05d84bfc2.cpp @@ -0,0 +1,173 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +vector v[201],vt[201],SCC[201]; + +int viz[201],p[201],sol[201],poz[201],n,nr; + + + +inline int NEG(int x) + +{ + + int aux = x + n; + + if(aux>2*n) + + aux=aux-2*n; + + return aux; + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(vector :: iterator it=v[nod].begin();it!=v[nod].end();it++) + + if(viz[*it]==0) + + dfs(*it); + + p[++p[0]]=nod; + +} + + + +void dfst(int nod) + +{ + + viz[nod]=1; + + SCC[nr].push_back(nod); + + poz[nod]=nr; + + for(vector :: iterator it=vt[nod].begin();it!=vt[nod].end();it++) + + if(viz[*it]==0) + + dfst(*it); + +} + + + +int main () + +{ + + int m,i,x,y,opt; + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n>>m; + + for(i=1;i<=m;i++) { + + f>>x>>y>>opt; + + if(opt==1) + + y=y+n; + + if(opt==2) + + x=x+n; + + if(opt==3) { + + x=x+n; + + y=y+n; + + } + + v[NEG(x)].push_back(y); + + v[NEG(y)].push_back(x); + + vt[y].push_back(NEG(x)); + + vt[x].push_back(NEG(y)); + + } + + f.close(); + + for(i=1;i<=2*n;i++) + + if(viz[i]==0) + + dfs(i); + + memset(viz,0,sizeof(viz)); + + for(i=p[0];i>=1;i--) + + if(viz[p[i]]==0) { + + nr++; + + dfst(p[i]); + + } + + for(i=1;i<=2*n;i++) + + sol[i]=-1; + + for(i=nr;i>=1;i--) { + + if(sol[SCC[i][0]]!=-1) + + continue; + + for(vector :: iterator it=SCC[i].begin();it!=SCC[i].end();it++) { + + sol[*it]=1; + + sol[NEG(*it)]=0; + + } + + } + + nr=0; + + for(i=1;i<=n;i++) + + nr=nr+sol[i]; + + g< +#include +#include +#include + + + + +using namespace std; + + + +int n,m,ind,nrc,sol; + +int lev[205],low[205],v[205],c[205],ok[205]; + +vector l[205]; + +stack s; + + + +void read() + +{ + + int x,y,t; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&t); + + if(!t) + + { + + l[x+n].push_back(y); l[y+n].push_back(x); + + continue; + + } + + if(t==1) + + { + + l[x+n].push_back(y+n); l[y].push_back(x); + + continue; + + } + + if(t==2) + + { + + l[x].push_back(y); l[y+n].push_back(x+n); + + continue; + + } + + l[x].push_back(y+n); l[y].push_back(x+n); + + } + +} + + + +void get(int k) + +{ + + for(nrc++;s.top()!=k;s.pop()) c[s.top()]=nrc,v[s.top()]=0; + + c[k]=nrc; v[k]=0; s.pop(); + +} + + + +void dfs(int k) + +{ + + lev[k]=low[k]=++ind; v[k]=1; s.push(k); + + for(unsigned int i=0;i +#include +using namespace std; + +int n, m, nrsol; + +vector G[210], GT[210]; + +bool viz[210], sol[210]; + +int postordine[210], nrord; + + + +inline int Not(int x) + +{ + + if(x <= n) + + return x + n; + + return x - n; + +} + + + +inline void Citire() + +{ + + int i, x, y, tip; + + ifstream fin("party.in"); + + fin >> n >> m; + + for(i = 1; i <= m; ++i) + + { + + fin >> x >> y >> tip; + + if(tip == 0); + + else + + { + + if(tip == 1) + + y = Not(y); + + else + + { + + if(tip == 2) + + x = Not(x); + + else + + { + + x = Not(x); + + y = Not(y); + + } + + } + + } + + G[Not(x)].push_back(y); + + G[Not(y)].push_back(x); + + GT[x].push_back(Not(y)); + + GT[y].push_back(Not(x)); + + } + + fin.close(); + +} + + + +inline void DFS(int x) + +{ + + viz[x] = true; + + vector ::iterator it; + + for(it = G[x].begin(); it != G[x].end(); ++it) + + if(!viz[*it]) + + DFS(*it); + + postordine[++nrord] = x; + +} + + + +inline void DFST(int x) + +{ + + viz[x] = false; + + sol[Not(x)] = true; + + vector ::iterator it; + + for(it = GT[x].begin(); it != GT[x].end(); ++it) + + if(viz[*it]) + + DFST(*it); + +} + + + +inline void Rezolvare() + +{ + + int i; + + for(i = 1; i <= 2 * n; ++i) + + if(!viz[i]) + + DFS(i); + + for(i = 2 * n; i > 0; --i) + + if(viz[postordine[i]] && viz[Not(postordine[i])]) + + DFST(postordine[i]); + + for(i = 1; i <= n; ++i) + + if(sol[i]) + + nrsol++; + +} + + + +inline void Afisare() + +{ + + ofstream fout("party.out"); + + fout << nrsol << "\n"; + + for(int i = 1; i <= n; ++i) + + if(sol[i]) + + fout << i << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Citire(); + + Rezolvare(); + + Afisare(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6c1afa76-02b2-11ec-9ac0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6c1afa76-02b2-11ec-9ac0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92a1950683381136ca5ec8c60a0abc5844bfeb7c --- /dev/null +++ b/party/2sat_implication_reduction/6c1afa76-02b2-11ec-9ac0-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +using namespace std; + +const int nmax = 105; + +int n,m,i,x,y,z,tsort[2*nmax],cnt,comp[2*nmax],sol; + +vector v[2*nmax]; + +vector vt[2*nmax]; + +bool viz[2*nmax]; +void dfs(int x) + +{ + + (viz+nmax)[x]=1; + + for(vector::iterator it=(v+nmax)[x].begin();it!=(v+nmax)[x].end();it++) + + if(!(viz+nmax)[*it]) dfs(*it); + + tsort[++cnt]=x; + +} + + + +void dfst(int x) + +{ + + (viz+nmax)[x]=1; (comp+nmax)[x]=cnt; + + for(vector::iterator it=(vt+nmax)[x].begin();it!=(vt+nmax)[x].end();it++) + + if(!(viz+nmax)[*it]) dfst(*it); + +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + scanf("%d%d",&n,&m); + + for(;m;m--) + + { + + scanf("%d%d%d",&x,&y,&z); + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + + (v+nmax)[-x].push_back(y); + + (v+nmax)[-y].push_back(x); + + (vt+nmax)[y].push_back(-x); + + (vt+nmax)[x].push_back(-y); + + } + + + + for(i=-n;i<=n;i++) if(!(viz+nmax)[i] && i) dfs(i); + + for(i=-n;i<=n;i++) (viz+nmax)[i]=0; cnt=0; + + for(i=2*n;i;i--) if(!(viz+nmax)[tsort[i]]) {cnt++; dfst(tsort[i]);} + + + + for(i=1;i<=n;i++) if((comp+nmax)[i]>(comp+nmax)[-i]) sol++; printf("%d\n",sol); + + for(i=1;i<=n;i++) if((comp+nmax)[i]>(comp+nmax)[-i]) printf("%d\n",i); + + + return 0; +} diff --git a/party/2sat_implication_reduction/6c5ac29a-02b2-11ec-84d7-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6c5ac29a-02b2-11ec-84d7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c6046ba567a3096eec562f36ab36ab80bb13ebe --- /dev/null +++ b/party/2sat_implication_reduction/6c5ac29a-02b2-11ec-84d7-2cf05d84bfc2.cpp @@ -0,0 +1,314 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +class TKosarajuGraph + +{ + + protected: + + int m_iLength; + + int Non (int X); + + public: + + void Kosaraju (); + + void ReadData (FILE * fIn); + + + + int m_vSolution[200005 ]; + + + + int GetLength(); + + private: + + int m_vTSort[200005 ]; + + + + void DFM (int X); + + void DFP (int X); + + + + vector m_vGraph[200005 ], m_vGraphT[200005 ]; + + bool m_vUsed[200005 ]; + +}; + + + + + +int TKosarajuGraph::GetLength() + +{ + + return m_iLength; + +} + + + +int TKosarajuGraph::Non (int X) + +{ + + if (X<=m_iLength) + + { + + return X+m_iLength; + + } + + return X-m_iLength; + +} + + + +void Print (TKosarajuGraph * objGraph) + +{ + + FILE * fOut = fopen("party.out", "w"); + + + + for (int i=1; i<=objGraph->GetLength(); ++i) + + { + + objGraph->m_vSolution[0] +=objGraph->m_vSolution[i]; + + } + + fprintf (fOut,"%d\n", objGraph->m_vSolution[0]); + + for (int i=1; i<=objGraph->GetLength(); ++i) + + { + + if (objGraph->m_vSolution[i]!=0) + + { + + fprintf (fOut,"%d\n", i); + + } + + } + + + +} + + + +void TKosarajuGraph::ReadData (FILE * fIn) + +{ + + int M; + + fscanf (fIn,"%d %d", &m_iLength, &M); + + for (; M>0; --M) + + { + + int X, Y, iType; + + fscanf (fIn,"%d %d %d", &X, &Y, &iType); + + if (iType==0) + + { + + m_vGraph[Non (X)].push_back (Y); + + m_vGraph[Non (Y)].push_back (X); + + m_vGraphT[X].push_back (Non (Y)); + + m_vGraphT[Y].push_back (Non (X)); + + } + + if (iType==1) + + { + + m_vGraph[Non (X)].push_back (Non (Y)); + + m_vGraph[Y].push_back (X); + + m_vGraphT[Non (Y)].push_back (Non (X)); + + m_vGraphT[X].push_back (Y); + + } + + if (iType==2) + + { + + m_vGraph[Non (Y)].push_back (Non (X)); + + m_vGraph[X].push_back (Y); + + m_vGraphT[Non (X)].push_back (Non (Y)); + + m_vGraphT[Y].push_back (X); + + } + + if (iType==3) + + { + + m_vGraph[X].push_back (Non (Y)); + + m_vGraph[Y].push_back (Non (X)); + + m_vGraphT[Non (X)].push_back (Y); + + m_vGraphT[Non (Y)].push_back (X); + + } + + } + +} + + + +void TKosarajuGraph::DFP (int X) + +{ + + m_vUsed[X]=true; + + for (vector :: iterator V=m_vGraph[X].begin (); V!=m_vGraph[X].end (); ++V) + + { + + if (!m_vUsed[*V]) + + { + + DFP (*V); + + } + + } + + m_vTSort[++m_vTSort[0]]=X; + +} + + + +void TKosarajuGraph::DFM (int X) + +{ + + m_vUsed[X]=true; + + m_vSolution[Non (X)]=1; + + for (vector :: iterator V=m_vGraphT[X].begin (); V!=m_vGraphT[X].end (); ++V) + + { + + if (!m_vUsed[*V]) + + { + + DFM (*V); + + } + + } + +} + + + +void TKosarajuGraph::Kosaraju () + +{ + + for (int i=1; i<=this->m_iLength+this->m_iLength; ++i) + + { + + if (!m_vUsed[i]) + + { + + DFP (i); + + } + + } + + memset (m_vUsed, 0, sizeof (m_vUsed)); + + for (int i=m_vTSort[0]; i>0; --i) + + { + + if (!m_vUsed[m_vTSort[i]] and !m_vUsed[Non (m_vTSort[i])]) + + { + + DFM (m_vTSort[i]); + + } + + } + +} + + + +int main() + +{ + + TKosarajuGraph * objGraph = new TKosarajuGraph(); + + + + FILE * fIn = fopen("party.in","r"); + + objGraph->ReadData (fIn); + + objGraph->Kosaraju (); + + Print (objGraph); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6c79e357-02b2-11ec-868d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6c79e357-02b2-11ec-868d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00a3416ff0951b8e3fe2749eb1d496d2a686f62e --- /dev/null +++ b/party/2sat_implication_reduction/6c79e357-02b2-11ec-868d-2cf05d84bfc2.cpp @@ -0,0 +1,190 @@ + #include + #include + #include + using namespace std; + + + + + + int N , M , p[201] , sol[201] , nr , k , poz[201] , rez; + + vector G[201] , Gt[201] , CC[201]; + + bool u[201]; + + + + void read(); + + void solve(); + + void write(); + + int abs(int x) + + { + + if(x < 0)return -x+N; + + return x; + + } + + int neg(int x) + + { + + if(x > N)return x-N; + + return x+N; + + } + + void dfs1(int nod); + + void dfs2(int nod); + + + + int main() + + { + + read(); + + solve(); + + write(); + + return 0; + + } + + + + void read() + + { + + int x , y , t; + + freopen("party.in" , "r" , stdin ); + + scanf("%d%d" , &N , &M ); + + for(int i = 1 ; i<= M ; ++i ) + + { + + scanf("%d%d%d" , &x , &y , &t); + + if(t == 1)y = neg(y); + + if(t == 2)x = neg(x); + + if(t == 3)x = neg(x),y = neg(y); + + G[neg(x)].push_back(y); + + G[neg(y)].push_back(x); + + Gt[y].push_back(neg(x)); + + Gt[x].push_back(neg(y)); + + } + + } + + + + void solve() + + { + + for(int i = 1 ; i <= 2*N ; ++i ) + + if(!u[i])dfs1(i); + + memset(u,0,sizeof(u)); + + for(int i = 2*N ; i ; i-- ) + + if(!u[p[i]])nr++,dfs2(p[i]); + + memset(sol,0-1,sizeof(sol)); + + for(int i = 1 ; i<= nr ; ++i ) + + { + + if(sol[CC[i][0]] != -1)continue; + + for(int j = 0 ; j <(int)CC[i].size() ; ++j ) + + { + + sol[CC[i][j]] = 0; + + sol[neg(CC[i][j])] = 1; + + } + + } + + } + + + + void dfs1(int nod) + + { + + u[nod] = 1; + + for(int j = 0 ; j <(int)G[nod].size() ; ++j ) + + if(!u[G[nod][j]])dfs1(G[nod][j]); + + p[++k] = nod; + + } + + + + void dfs2(int nod) + + { + + u[nod] = 1; + + CC[nr].push_back(nod); + + poz[nod] = nr; + + for(int j = 0 ; j < (int)Gt[nod].size() ; ++j ) + + if(!u[Gt[nod][j]])dfs2(Gt[nod][j]); + + } + + + + void write() + + { + + freopen("party.out" , "w" , stdout ); + + for(int i = 1 ; i<= N ; ++i ) + + rez+=sol[i]; + + printf("%d\n" , rez); + + for(int i = 1 ; i<= N ; ++i ) + + if(sol[i])printf("%d\n" , i); + + } diff --git a/party/2sat_implication_reduction/6cb98478-02b2-11ec-9345-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6cb98478-02b2-11ec-9345-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3119f38c95ef30f8dfc09053806216169e762a5 --- /dev/null +++ b/party/2sat_implication_reduction/6cb98478-02b2-11ec-9345-2cf05d84bfc2.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int n,m,ind,nrc,sol; + +int lev[205],low[205],v[205],c[205],ok[205]; + +vector l[205]; + +stack s; + + + +void read() + +{ + + int x,y,t; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&t); + + if(!t) + + { + + l[x+n].push_back(y); l[y+n].push_back(x); + + continue; + + } + + if(t==1) + + { + + l[x+n].push_back(y+n); l[y].push_back(x); + + continue; + + } + + if(t==2) + + { + + l[x].push_back(y); l[y+n].push_back(x+n); + + continue; + + } + + l[x].push_back(y+n); l[y].push_back(x+n); + + } + +} + + + +void get(int k) + +{ + + for(nrc++;s.top()!=k;s.pop()) c[s.top()]=nrc,v[s.top()]=0; + + c[k]=nrc; v[k]=0; s.pop(); + +} + + + +void dfs(int k) + +{ + + lev[k]=low[k]=++ind; v[k]=1; s.push(k); + + for(unsigned int i=0;i +#include +#include +#include + +using namespace std; + +const int nmax = 105; + +int n,m,i,x,y,z,tsort[2*nmax],cnt,comp[2*nmax],sol; + +vector v[2*nmax]; + +vector vt[2*nmax]; + +bool viz[2*nmax]; +void dfs(int x) + +{ + + (viz+nmax)[x]=1; + + for(vector::iterator it=(v+nmax)[x].begin();it!=(v+nmax)[x].end();it++) + + if(!(viz+nmax)[*it]) dfs(*it); + + tsort[++cnt]=x; + +} + + + +void dfst(int x) + +{ + + (viz+nmax)[x]=1; (comp+nmax)[x]=cnt; + + for(vector::iterator it=(vt+nmax)[x].begin();it!=(vt+nmax)[x].end();it++) + + if(!(viz+nmax)[*it]) dfst(*it); + +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + scanf("%d%d",&n,&m); + + for(;m;m--) + + { + + scanf("%d%d%d",&x,&y,&z); + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + + (v+nmax)[-x].push_back(y); + + (v+nmax)[-y].push_back(x); + + (vt+nmax)[y].push_back(-x); + + (vt+nmax)[x].push_back(-y); + + } + + + + for(i=-n;i<=n;i++) if(!(viz+nmax)[i] && i) dfs(i); + + for(i=-n;i<=n;i++) (viz+nmax)[i]=0; cnt=0; + + for(i=2*n;i;i--) if(!(viz+nmax)[tsort[i]]) {cnt++; dfst(tsort[i]);} + + + + for(i=1;i<=n;i++) if((comp+nmax)[i]>(comp+nmax)[-i]) sol++; printf("%d\n",sol); + + for(i=1;i<=n;i++) if((comp+nmax)[i]>(comp+nmax)[-i]) printf("%d\n",i); + + + return 0; +} diff --git a/party/2sat_implication_reduction/6d19cd05-02b2-11ec-a875-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6d19cd05-02b2-11ec-a875-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed99b98fcc40d735ea3deaa8f6798cc202e8bb3e --- /dev/null +++ b/party/2sat_implication_reduction/6d19cd05-02b2-11ec-a875-2cf05d84bfc2.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = int(1e5) + 2; + +int N, M; + +vector G[NMAX << 1], GT[NMAX << 1], s; + +bitset< NMAX << 1 > visited, truthValue; + +bool solution = true; + + + + + +inline void addEdge(int a, int b) { + + G[(a > N ? a - N : a + N)].push_back(b); + + G[(b > N ? b - N : b + N)].push_back(a); + + GT[a].push_back((b > N ? b - N : b + N)); + + GT[b].push_back((a > N ? a - N : a + N)); + + + +} + + + +void readData(){ + + cin >> N >> M; + + int a, b, t; + + for (int i = 0; i < M; i++) { + + cin >> a >> b >> t; + + if (t == 0) { + + addEdge(a, b); + + } + + else + + if (t == 1) { + + addEdge(b + N, a); + + } + + else + + if (t == 2) { + + addEdge(a + N, b); + + } + + else { + + addEdge(a + N, b + N); + + } + + } + +} + + + +void df(const int &v) { + + visited[v] = true; + + for (vector::const_iterator w = G[v].begin(); w != G[v].end(); w++) { + + if (visited[*w] == false) { + + df(*w); + + } + + } + + s.push_back(v); + +} + + + +void df1(const int &v) { + + if (truthValue[v]) { + + solution = false; + + } + + truthValue[(v > N ? v - N : v + N)] = true; + + visited[v] = true; + + for (vector::const_iterator w = GT[v].begin(); w != GT[v].end(); w++) { + + if (visited[*w] == false) { + + df1(*w); + + } + + } + +} + + + +void solve() { + + for (int i = 1; i <= (N << 1); i++){ + + if (!visited[i]) { + + df(i); + + } + + } + + visited.reset(); + + for (vector::const_reverse_iterator it = s.rbegin(); it != s.rend(); it++) { + + if (!visited[*it] && !visited[(*it > N ? *it - N : *it + N)]) { + + df1(*it); + + } + + } + + if (solution) { + + int nr = 0; + + for (int i = 1; i <= N; i++) { + + nr += truthValue[i]; + + } + + cout << nr << "\n"; + + for (int i = 1; i <= N; i++) { + + if (truthValue[i]) { + + cout << i << "\n"; + + } + + } + + } + + else { + + cout << "-1\n"; + + } + +} + + + +int main() + +{ + + readData(); + + solve(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6dac6fec-02b2-11ec-8ee6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6dac6fec-02b2-11ec-8ee6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93a7da910b1ff253362a7adcd941d0b10c248d7b --- /dev/null +++ b/party/2sat_implication_reduction/6dac6fec-02b2-11ec-8ee6-2cf05d84bfc2.cpp @@ -0,0 +1,239 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int Nmax = 102; + + + +vector G[2 * Nmax], GT[2 * Nmax]; + +int sol[2 * Nmax]; + +int vis[2 * Nmax]; + +int postorder[2 * Nmax]; + + + +int N, M, P, EXIST_SOL = 1; + + + +inline int neg( int x ) + +{ + + if ( x > N ) + + return x - N; + + else + + return x + N; + +} + + + +void DFS( int nod ) + +{ + + vis[nod] = 1; + + + + for ( auto x: G[nod] ) + + if ( !vis[x] ) + + DFS( x ); + + + + postorder[ ++P ] = nod; + +} + + + +void DFST( int nod ) + +{ + + if ( sol[nod] ) EXIST_SOL = 0; + + + + vis[nod] = 0; + + sol[ neg( nod ) ] = 1; + + + + for ( auto x: GT[nod] ) + + if ( vis[x] ) + + DFST( x ); + +} + + + +void SAT2() + +{ + + for ( int i = 1; i <= 2 * N; ++i ) + + if ( !vis[i] ) + + DFS( i ); + + + + for ( int i = P; i >= 1; i-- ) + + { + + int nod = postorder[i]; + + + + if ( vis[nod] && vis[ neg( nod ) ] ) + + DFST( nod ); + + } + +} + + + +int main() + +{ + + ifstream f("party.in"); + + ofstream g("party.out"); + + + + f >> N >> M; + + + + for ( int i = 1, tip, x, y; i <= M; ++i ) + + { + + f >> x >> y >> tip; + + + + switch( tip ) + + { + + case 0: + + break; + + + + case 1: + + y = neg( y ); + + + + break; + + + + case 2: + + x = neg( x ); + + + + break; + + + + + + case 3: + + x = neg( x ); + + y = neg( y ); + + + + break; + + + + default: break; + + } + + + + G[ neg( x ) ].push_back( y ); + + G[ neg( y ) ].push_back( x ); + + + + GT[y].push_back( neg( x ) ); + + GT[x].push_back( neg( y ) ); + + } + + + + SAT2(); + + + + if ( EXIST_SOL ) + + { + + g << count( sol + 1, sol + N + 1, 1 ) << "\n"; + + + + for ( int i = 1; i <= N; ++i ) + + if( sol[i] ) + + g << i << "\n"; + + } + + else + + { + + g << "0\n"; + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6dcababf-02b2-11ec-a0d3-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6dcababf-02b2-11ec-a0d3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7f842203d4498e43ae99c41c900591bf83c56e8 --- /dev/null +++ b/party/2sat_implication_reduction/6dcababf-02b2-11ec-a0d3-2cf05d84bfc2.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 210; + + + +int N, M, X, Y, Z, TopSort[NMAX], Val[NMAX]; + +bool Used[NMAX]; + +vector G[NMAX], GT[NMAX]; + + + +int Neg(int X) + +{ + + if(X <= N) return X + N; + + else return X - N; + +} + + + +void DFP(int Node) + +{ + + Used[Node] = 1; + + for(vector :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it) + + if(!Used[*it]) + + DFP(*it); + + TopSort[ ++ TopSort[0] ] = Node; + +} + + + +void DFM(int Node) + +{ + + Val[ Neg(Node) ] = 1; + + Used[Node] = 0; + + + + for(vector :: iterator it = GT[Node].begin(); it != GT[Node].end(); ++ it) + + if(Used[*it]) + + DFM(*it); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%i %i", &N, &M); + + for(int i = 1; i <= M; ++ i) + + { + + scanf("%i %i %i", &X, &Y, &Z); + + + + if(Z == 1) Y = Neg(Y); + + else if(Z == 2) X = Neg(X); + + else if(Z == 3) X = Neg(X), Y = Neg(Y); + + + + G[Neg(Y)].push_back(X); + + GT[X].push_back(Neg(Y)); + + G[Neg(X)].push_back(Y); + + GT[Y].push_back(Neg(X)); + + } + + + + for(int i = 1; i <= 2 * N; ++ i) + + if(!Used[i]) + + DFP(i); + + reverse(TopSort + 1, TopSort + TopSort[0] + 1); + + for(int i = 1; i <= 2 * N; ++ i) + + if(Used[ TopSort[i] ] && Used[ Neg(TopSort[i]) ]) + + DFM(TopSort[i]); + + + + int Ans = 0; + + for(int i = 1; i <= N; ++ i) Ans += Val[i]; + + + + printf("%i\n", Ans); + + for(int i = 1; i <= N; ++ i) + + if(Val[i]) + + printf("%i\n", i); + +} diff --git a/party/2sat_implication_reduction/6e4582f1-02b2-11ec-a780-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6e4582f1-02b2-11ec-a780-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93a7da910b1ff253362a7adcd941d0b10c248d7b --- /dev/null +++ b/party/2sat_implication_reduction/6e4582f1-02b2-11ec-a780-2cf05d84bfc2.cpp @@ -0,0 +1,239 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int Nmax = 102; + + + +vector G[2 * Nmax], GT[2 * Nmax]; + +int sol[2 * Nmax]; + +int vis[2 * Nmax]; + +int postorder[2 * Nmax]; + + + +int N, M, P, EXIST_SOL = 1; + + + +inline int neg( int x ) + +{ + + if ( x > N ) + + return x - N; + + else + + return x + N; + +} + + + +void DFS( int nod ) + +{ + + vis[nod] = 1; + + + + for ( auto x: G[nod] ) + + if ( !vis[x] ) + + DFS( x ); + + + + postorder[ ++P ] = nod; + +} + + + +void DFST( int nod ) + +{ + + if ( sol[nod] ) EXIST_SOL = 0; + + + + vis[nod] = 0; + + sol[ neg( nod ) ] = 1; + + + + for ( auto x: GT[nod] ) + + if ( vis[x] ) + + DFST( x ); + +} + + + +void SAT2() + +{ + + for ( int i = 1; i <= 2 * N; ++i ) + + if ( !vis[i] ) + + DFS( i ); + + + + for ( int i = P; i >= 1; i-- ) + + { + + int nod = postorder[i]; + + + + if ( vis[nod] && vis[ neg( nod ) ] ) + + DFST( nod ); + + } + +} + + + +int main() + +{ + + ifstream f("party.in"); + + ofstream g("party.out"); + + + + f >> N >> M; + + + + for ( int i = 1, tip, x, y; i <= M; ++i ) + + { + + f >> x >> y >> tip; + + + + switch( tip ) + + { + + case 0: + + break; + + + + case 1: + + y = neg( y ); + + + + break; + + + + case 2: + + x = neg( x ); + + + + break; + + + + + + case 3: + + x = neg( x ); + + y = neg( y ); + + + + break; + + + + default: break; + + } + + + + G[ neg( x ) ].push_back( y ); + + G[ neg( y ) ].push_back( x ); + + + + GT[y].push_back( neg( x ) ); + + GT[x].push_back( neg( y ) ); + + } + + + + SAT2(); + + + + if ( EXIST_SOL ) + + { + + g << count( sol + 1, sol + N + 1, 1 ) << "\n"; + + + + for ( int i = 1; i <= N; ++i ) + + if( sol[i] ) + + g << i << "\n"; + + } + + else + + { + + g << "0\n"; + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6e64a42d-02b2-11ec-940c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6e64a42d-02b2-11ec-940c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d6b260272196a1af0b117184dba3eb590690174 --- /dev/null +++ b/party/2sat_implication_reduction/6e64a42d-02b2-11ec-940c-2cf05d84bfc2.cpp @@ -0,0 +1,186 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 102; + + + +int N, M, ans; + +int c[2 * MAX_N], sol[2 * MAX_N]; + +vector < int > v[2 * MAX_N], w[2 * MAX_N], scc[2 * MAX_N], a; + +bool m[2 * MAX_N], s[2 * MAX_N]; + + + +inline int neg(int x) { + + if(x <= N) + + return x + N; + + return x - N; + +} + + + +void DFS1(int x) { + + m[x] = 1; + + for(size_t i = 0; i < v[x].size(); ++i) + + if(m[v[x][i]] == 0) + + DFS1(v[x][i]); + + a.push_back(x); + +} + + + +void DFS2(int x, int nr) { + + m[x] = 1; + + c[x] = nr; + + scc[nr].push_back(x); + + for(size_t i = 0; i < w[x].size(); ++i) + + if(m[w[x][i]] == 0) + + DFS2(w[x][i], nr); + +} + + + +int main() { + + ifstream f("party.in"); + + ofstream g("party.out"); + + + + f >> N >> M; + + for(int i = 1, x, y, t; i <= M; ++i) { + + f >> x >> y >> t; + + + + if(t == 1) + + y = neg(y); + + else if(t == 2) + + x = neg(x); + + else if(t == 3) + + x = neg(x), y = neg(y); + + + + v[neg(x)].push_back(y); + + v[neg(y)].push_back(x); + + + + w[x].push_back(neg(y)); + + w[y].push_back(neg(x)); + + } + + + + for(int i = 1; i <= 2 * N; ++i) + + if(m[i] == 0) + + DFS1(i); + + + + memset(m, 0, sizeof(m)); + + int nr = 0; + + for(int i = a.size() - 1; i >= 0; --i) + + if(m[a[i]] == 0) { + + ++nr; + + DFS2(a[i], nr); + + } + + + + for(int i = nr; i >= 1; --i) { + + if(s[i]) + + continue; + + + + for(size_t j = 0; j < scc[i].size(); ++j) { + + int x = scc[i][j]; + + + + sol[x] = 1; + + s[c[neg(x)]] = 1; + + } + + } + + + + for(int i = 1; i <= N; ++i) + + ans += sol[i]; + + + + g << ans << "\n"; + + for(int i = 1; i <= N; ++i) + + if(sol[i]) + + g << i << "\n"; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/6e8328aa-02b2-11ec-a2ce-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6e8328aa-02b2-11ec-a2ce-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9556a3ba494a3dffeeb4a87569d43edcaf5d7638 --- /dev/null +++ b/party/2sat_implication_reduction/6e8328aa-02b2-11ec-a2ce-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include +using namespace std; + + + +ifstream F("party.in"); + +ofstream G("party.out"); + + + + + + + +const int N = 110; + +vector v[N<<1],w[N<<1],stack; + +bool mark[N<<1],ans[N<<1],no_sol; + +int n,m; + + + +int neg(int x) + +{ + + return x > n ? x - n : x + n; + +} + + + +void dff(int x) + +{ + + mark[x] = 1; + + for (vector::iterator y=v[x].begin();y!=v[x].end();++y) + + if ( !mark[*y] ) + + dff(*y); + + stack.push_back( x ); + +} + + + +void dfs(int x) + +{ + + if ( ans[x] ) no_sol = 1; + + mark[x] = 1; + + ans[neg(x)] = 1; + + for (vector::iterator y=w[x].begin();y!=w[x].end();++y) + + if ( !mark[*y] ) + + dfs(*y); + +} + + + +int main() + +{ + + F>>n>>m; + + for (int i=1,t,x,y;i<=m;++i) + + { + + F>>x>>y>>t; + + if ( t == 3 ) + + { + + x = neg(x); + + y = neg(y); + + } else + + if ( t == 1 ) + + { + + y = neg(y); + + } else + + if ( t == 2 ) + + { + + x = neg(x); + + } + + v[neg(x)].push_back(y); + + v[neg(y)].push_back(x); + + w[y].push_back(neg(x)); + + w[x].push_back(neg(y)); + + } + + for (int i=1;i<=2*n;++i) + + if ( !mark[i] ) + + dff(i); + + memset(mark,0,sizeof(mark)); + + for (;!stack.empty();stack.pop_back()) + + { + + int x = stack.back(); + + if ( !mark[x] && !mark[neg(x)] ) + + dfs(x); + + } + + if ( no_sol ) + + G<<"-1\n"; + + else + + { + + int co = 0; + + for (int i=1;i<=n;++i) + + if ( ans[i] ) + + ++co; + + G< +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +const int Nmax = 250; + + + +int N, M; + +queue Q, Initialize; + +vector G[Nmax], sol; + +vector viz(Nmax), val(Nmax); + + + +inline int Not(int x) + +{ + + return (x <= N ? N + x : x - N); + +} + + + +bool DFS(int nod) + +{ + + Q.push(nod); + + for(unsigned i = 0; i < G[nod].size(); i++) + + if(viz[G[nod][i]]) + + if(!val[G[nod][i]]) + + return 0; + + else; + + else + + { + + viz[G[nod][i]] = viz[Not(G[nod][i])] = 1; + + val[G[nod][i]] = 1; val[Not(G[nod][i])] = 0; + + if(!DFS(Not(G[nod][i]))) + + return 0; + + } + + return 1; + +} + + + +void Clear() + +{ + + while(!Q.empty()) + + { + + int nod = Q.front(); Q.pop(); + + viz[nod] = viz[Not(nod)] = 0; + + } + +} + + + +bool Right(int nod, bool v) + +{ + + viz[nod] = viz[Not(nod)] = 1; + + val[nod] = v; val[Not(nod)] = !v; + + if(v == 0) + + return DFS(nod); + + else + + return DFS(Not(nod)); + +} + + + +int main() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x, y, tip; + + fin >> x >> y >> tip; + + + + if(!tip); + + else if(tip == 1) + + y = Not(y); + + else if(tip == 2) + + x = Not(x); + + else + + x = Not(x), y = Not(y); + + + + G[x].push_back(y); + + G[y].push_back(x); + + } + + + + for(int i = 1; i <= N; i++) + + if(!viz[i]) + + if(!Right(i, 1)) + + { + + Clear(); + + if(!Right(i, 0)) + + { + + fout << "0"; + + return 0; + + } + + Q = Initialize; + + } + + int nr = 0; + + for(int i = 1; i <= N; i++) + + if(val[i]) + + { + + nr++; + + sol.push_back(i); + + } + + fout << nr << '\n'; + + for(int i = 0; i < nr; i++) + + fout<< sol[i] << '\n'; + + return 0; + +} diff --git a/party/2sat_implication_reduction/6f1dfb21-02b2-11ec-94a9-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6f1dfb21-02b2-11ec-94a9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d4f0f18ad1338bf2d1b04a34a39c243bc8e54324 --- /dev/null +++ b/party/2sat_implication_reduction/6f1dfb21-02b2-11ec-94a9-2cf05d84bfc2.cpp @@ -0,0 +1,219 @@ +#include +#include + + + + + + +using namespace std; + + + +bool existaSol; + +int n, m; + +bool sol[2*110]; + +bool viz[2*110]; + +vector G[2*110], GT[2*110], ans; + +int postordine[2*110], npostordine; + + + +inline int neg(const int &x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void Read() + +{ + + ifstream f ("party.in"); + + f>>n>>m; + + for (int i = 1; i<=m; ++i) + + { + + int x, y, cod; + + f>>x>>y>>cod; + + switch(cod) + + { + + case 0: + + + + break; + + case 1: + + + + y = neg(y); + + break; + + case 2: + + + + x = neg(x); + + break; + + case 3: + + + + x = neg(x); + + y = neg(y); + + break; + + } + + G[neg(x)].push_back(y); + + G[neg(y)].push_back(x); + + GT[y].push_back(neg(x)); + + GT[x].push_back(neg(y)); + + } + + f.close(); + +} + + + +inline void DFS(const int node) + +{ + + viz[node] = true; + + for (vector ::iterator it = G[node].begin(); it!=G[node].end(); ++it) + + if (!viz[*it]) + + DFS(*it); + + postordine[++npostordine] = node; + +} + + + +inline void DFST(const int node) + +{ + + if (sol[node]) + + { + + existaSol = false; + + return ; + + } + + sol[neg(node)] = true; + + viz[node] = false; + + for (vector ::iterator it = GT[node].begin(); it!=GT[node].end(); ++it) + + if (viz[*it]) + + DFST(*it); + +} + + + +void Solve() + +{ + + existaSol = true; + + int i, n2; + + n2 = n + n; + + for (i=1; i<=n2; ++i) + + if (!viz[i]) + + DFS(i); + + for (i=n2; i; --i) + + if (viz[postordine[i]] && viz[postordine[neg(i)]]) + + DFST(postordine[i]); + + for (i=1; i<=n; ++i) + + if (sol[i]) + + ans.push_back(i); + +} + + + +void Write() + +{ + + ofstream g("party.out"); + + g<::iterator it = ans.begin(); it != ans.end(); ++it) + + g<<*it<<"\n"; + + g.close(); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Write(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6f3e2d67-02b2-11ec-9427-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6f3e2d67-02b2-11ec-9427-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30449e0165a45b701210e23ebb8660d1d315083c --- /dev/null +++ b/party/2sat_implication_reduction/6f3e2d67-02b2-11ec-9427-2cf05d84bfc2.cpp @@ -0,0 +1,193 @@ +#include +#include +using namespace std; + +int n, m, nrsol; + +vector G[210], GT[210]; + +bool viz[210], sol[210]; + +int postordine[210], nrord; + + + +inline int Not(int x) + +{ + + if(x <= n) + + return x + n; + + return x - n; + +} + + + +inline void Citire() + +{ + + int i, x, y, tip; + + ifstream fin("party.in"); + + fin >> n >> m; + + for(i = 1; i <= m; ++i) + + { + + fin >> x >> y >> tip; + + if(tip == 0); + + else + + { + + if(tip == 1) + + y = Not(y); + + else + + { + + if(tip == 2) + + x = Not(x); + + else + + { + + x = Not(x); + + y = Not(y); + + } + + } + + } + + G[Not(x)].push_back(y); + + G[Not(y)].push_back(x); + + GT[x].push_back(Not(y)); + + GT[y].push_back(Not(x)); + + } + + fin.close(); + +} + + + +inline void DFS(int x) + +{ + + viz[x] = true; + + vector ::iterator it; + + for(it = G[x].begin(); it != G[x].end(); ++it) + + if(!viz[*it]) + + DFS(*it); + + postordine[++nrord] = x; + +} + + + +inline void DFST(int x) + +{ + + viz[x] = false; + + sol[Not(x)] = true; + + vector ::iterator it; + + for(it = GT[x].begin(); it != GT[x].end(); ++it) + + if(viz[*it]) + + DFST(*it); + +} + + + +inline void Rezolvare() + +{ + + int i; + + for(i = 1; i <= 2 * n; ++i) + + if(!viz[i]) + + DFS(i); + + for(i = 2 * n; i > 0; --i) + + if(viz[postordine[i]] && viz[Not(postordine[i])]) + + DFST(postordine[i]); + + for(i = 1; i <= n; ++i) + + if(sol[i]) + + nrsol++; + +} + + + +inline void Afisare() + +{ + + ofstream fout("party.out"); + + fout << nrsol << "\n"; + + for(int i = 1; i <= n; ++i) + + if(sol[i]) + + fout << i << "\n"; + + fout.close(); + +} + + + +int main() + +{ + + Citire(); + + Rezolvare(); + + Afisare(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/6f7ac633-02b2-11ec-9349-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/6f7ac633-02b2-11ec-9349-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7659e459d1f6d4a6fa106a5cb7c9e29775716c2a --- /dev/null +++ b/party/2sat_implication_reduction/6f7ac633-02b2-11ec-9349-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = int(1e5) + 2; + +int N, M; + +vector G[NMAX<<1], GT[NMAX<<1], s; + +bitset< NMAX<<1 > visited, truthValue; + +bool solution = true; + + + + + +inline void addEdge(int a,int b) { + + G[(a > N ? a - N : a + N)].push_back(b); + + G[(b > N ? b - N : b + N)].push_back(a); + + GT[a].push_back((b > N ? b - N : b + N)); + + GT[b].push_back((a > N ? a - N : a + N)); + + + +} + + + +void readData(){ + + cin>>N>>M; + + int a, b, t; + + for(int i = 0;i < M;i++) { + + cin>>a>>b>>t; + + if(t == 0) { + + addEdge(a,b); + + } else + + if(t == 1) { + + addEdge(b + N,a); + + } else + + if(t == 2) { + + addEdge(a + N,b); + + } else { + + addEdge(a + N,b + N); + + } + + } + +} + + + +void df(const int &v) { + + visited[v] = true; + + for(vector::const_iterator w = G[v].begin();w != G[v].end();w++) { + + if(visited[*w] == false) { + + df(*w); + + } + + } + + s.push_back(v); + +} + + + +void df1(const int &v) { + + if(truthValue[v]) { + + solution = false; + + } + + truthValue[(v > N ? v - N : v + N)] = true; + + visited[v] = true; + + for(vector::const_iterator w = GT[v].begin();w != GT[v].end();w++) { + + if(visited[*w] == false) { + + df1(*w); + + } + + } + +} + + + +void solve() { + + for(int i = 1;i <= (N<<1);i++){ + + if(!visited[i]) { + + df(i); + + } + + } + + visited.reset(); + + for(vector::const_reverse_iterator it = s.rbegin();it != s.rend();it++) { + + if(!visited[*it] && !visited[(*it > N ? *it - N : *it + N)]) { + + df1(*it); + + } + + } + + if(solution) { + + int nr = 0; + + for(int i = 1;i <= N;i++) { + + nr += truthValue[i]; + + } + + cout< +#include + + + + +using namespace std; + +vectoroo[2*100 +1],OO[2*100 +1]; + +vector*o=oo+100,*O=OO+100; + +bool C[2*100 +1],V[2*100 +1]; + +bool*c=C+100,*v=V+100; + +int L[2*100],l,S; + +void dfs1(int i){ + + c[i]=true; + + int s=o[i].size(); + + for(int j=0;j +#include +#include +#include +using namespace std; + +const int MAX_N=110; + +vector G[2*MAX_N]; +vector G2[2*MAX_N]; + +bool viz[2*MAX_N]; +bool val[2*MAX_N]; +int st[2*MAX_N]; + +int n; + +inline int nott(int val) { + if(val>n) { + return val-n; + } + return val+n; +} + +void dfs1(int nod) { + viz[nod]=true; + for(vector::iterator it=G[nod].begin(); it!=G[nod].end(); it++) { + if(!viz[*it]) { + dfs1(*it); + } + } + st[++st[0]]=nod; +} + +void dfs2(int nod) { + viz[nod]=true; + + val[nott(nod)]=true; + + for(vector::iterator it=G2[nod].begin(); it!=G2[nod].end(); it++) { + if(!viz[*it]) { + dfs2(*it); + } + } +} + +int main() { + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + int m; + scanf("%d%d",&n,&m); + for(int i=1;i<=m;i++) { + int t,a,b; + scanf("%d%d%d",&a,&b,&t); + if(t==0) { + + } + else if(t==1) { + b=nott(b); + } + else if(t==2) { + a=nott(a); + } + else if(t==3) { + a=nott(a); + b=nott(b); + } + + + G[nott(a)].push_back(b); + G[nott(b)].push_back(a); + + G2[b].push_back(nott(a)); + G2[a].push_back(nott(b)); + } + + for(int i=1;i<=2*n;i++) { + if(!viz[i]) { + dfs1(i); + } + } + memset(viz,0,sizeof(viz)); + for(int i=2*n;i>=1;i--) { + if(!viz[st[i]]&&!viz[nott(st[i])]) { + dfs2(st[i]); + } + } + + vector sol; + for(int i=1;i<=n;i++) { + if(val[i]) { + sol.push_back(i); + } + } + + printf("%d\n",sol.size()); + for(vector::iterator it=sol.begin(); it!=sol.end(); it++) { + printf("%d\n",*it); + } + + return 0; +} diff --git a/party/2sat_implication_reduction/7036ea2f-02b2-11ec-ad6e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7036ea2f-02b2-11ec-ad6e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0245a14d428a901afe6e624ef12188bc8e77bf1 --- /dev/null +++ b/party/2sat_implication_reduction/7036ea2f-02b2-11ec-ad6e-2cf05d84bfc2.cpp @@ -0,0 +1,326 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int N, M, K; + +int MinLevel[210], Level[210], Component[210]; + +int In[210], Out[210]; + +int Simetric[210], Value[210]; + +vector G[210], NG[210]; + +bool InStack[210], E[210][210], Removed[210]; + +stack S; + +queue Q; + + + +int Negate (int x) + +{ + + return (x<=N?x+N:x-N); + +} + + + +void Build (int x, int y) + +{ + + G[Negate(x)].push_back(y); + + G[Negate(y)].push_back(x); + +} + + + +void Read () + +{ + + int x, y, z; + + f >> N >> M; + + for (int i=1; i<=M; i++) + + { + + f >> x >> y >> z; + + + + if (z>1) x=Negate(x); + + if (z%2) y=Negate(y); + + Build(x, y); + + } + + M=0; + + + + f.close(); + +} + + + +void DoTarjan (int node) + +{ + + Level[node]=MinLevel[node]=++K; + + S.push(node); + + InStack[node]=1; + + + + for (vector::iterator it=G[node].begin(); it!=G[node].end(); ++it) + + { + + if (!Level[*it]) + + { + + DoTarjan(*it); + + MinLevel[node]=min(MinLevel[node], MinLevel[*it]); + + continue; + + } + + if (InStack[*it]) + + MinLevel[node]=min(MinLevel[node], Level[*it]); + + } + + + + if (Level[node]==MinLevel[node]) + + { + + ++M; + + int cnode; + + do { + + cnode=S.top(); + + InStack[S.top()]=0; + + Component[S.top()]=M; + + S.pop(); + + } while (cnode!=node); + + } + +} + + + +void BuildComp () + +{ + + for (int i=1; i<=2*N; i++) + + if (!Level[i]) + + DoTarjan(i); + + + + for (int i=1; i<=2*N; i++) + + for (vector::iterator it=G[i].begin(); it!=G[i].end(); ++it) + + if (Component[i]!=Component[*it]) + + E[Component[i]][Component[*it]]=1; + + + + for (int i=1; i<=2*N; i++) + + { + + if (Component[i]==Component[Negate(i)]) + + { + + g << 0 << '\n'; + + g.close(); + + exit(0); + + } + + + + Simetric[Component[i]]=Component[Negate(i)]; + + } + + + + for (int i=1; i<=M; i++) + + for (int j=1; j<=M; j++) + + if (E[i][j]) + + { + + NG[i].push_back(j); + + Out[i]++; + + In[j]++; + + } + +} + + + +void Solve () + +{ + + for (int i=1; i<=M; i++) + + if (In[i]==0) + + Q.push(i); + + + + while (!Q.empty()) + + { + + int node=Q.front(); + + Q.pop(); + + + + Removed[node]=1; + + Removed[Simetric[node]]=1; + + Value[node]=0; + + Value[Simetric[node]]=1; + + + + for (vector::iterator it=NG[node].begin(); it!=NG[node].end(); ++it) + + if (!Removed[*it]) + + { + + --In[*it]; + + if (In[*it]<=0) + + Q.push(*it); + + } + + } + +} + + + +void Print () + +{ + + int ANS=0; + + + + for (int i=1; i<=N; i++) + + if (Value[Component[i]]==1) + + ANS++; + + + + g << ANS << '\n'; + + + + for (int i=1; i<=N; i++) + + if (Value[Component[i]]==1) + + g << i << '\n'; + + + + g.close(); + +} + + + +int main () + +{ + + Read(); + + BuildComp(); + + Solve(); + + Print(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/7054f97c-02b2-11ec-b89d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7054f97c-02b2-11ec-b89d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b67777eeb7340ee4d0f1ee3b35405bf6bf9a88f --- /dev/null +++ b/party/2sat_implication_reduction/7054f97c-02b2-11ec-b89d-2cf05d84bfc2.cpp @@ -0,0 +1,376 @@ +#include +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/7073f33f-02b2-11ec-9cd9-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7073f33f-02b2-11ec-9cd9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d09f1ff2c3f746954ea26850a7f220b468c93fce --- /dev/null +++ b/party/2sat_implication_reduction/7073f33f-02b2-11ec-9cd9-2cf05d84bfc2.cpp @@ -0,0 +1,201 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int MAXN = 105; + +const int oo = (1<<31)-1; + + + +typedef vector Graph[MAXN<<1]; + +typedef vector :: iterator It; + + + +const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; } + +const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; } + +const inline void Get_min(int &a, const int b) { if( a > b ) a = b; } + +const inline void Get_max(int &a, const int b) { if( a < b ) a = b; } + + + +Graph G; + +int N, M, ans, Idx[MAXN<<1], LowLink[MAXN<<1], Deep, Ans[MAXN<<1]; + +vector< set > SCComponent; + +stack Stack; + +bitset inStack; + + + + + +struct ClassComp { + + inline bool operator () (const int &a, const int &b) const { + + return a > b; + + } + +}; + + + +inline int Non(int x) { + + return x <= N ? x + N : x - N; + +} + + + +inline void Relation(int x, int y) { + + G[Non(x)].push_back(y); + + G[Non(y)].push_back(x); + +} + + + +inline void Tarjan(int Node) { + + Idx[Node] = LowLink[Node] = ++ Deep; + + Stack.push(Node); + + inStack[Node] = true; + + for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) + + if(!Idx[*it]) { + + Tarjan(*it); + + Get_min(LowLink[Node], LowLink[*it]); + + } + + else if(inStack[*it]) + + Get_min(LowLink[Node], LowLink[*it]); + + if(Idx[Node] == LowLink[Node]) { + + int nod; + + set ActSCC; + + do { + + ActSCC.insert(nod = Stack.top()); + + Stack.pop(); + + inStack[nod] = false; + + } while(nod != Node); + + SCComponent.push_back(ActSCC); + + } + +} + + + +int main() { + cin >> N >> M; + + for(int i = 1 ; i <= M ; ++ i) { + + int x, y, z; + + cin >> x >> y >> z; + + if( 2 & z ) x = Non(x); + + if( 1 & z ) y = Non(y); + + G[Non(x)].push_back(y); + + G[Non(y)].push_back(x); + + + + } + + for(int i = 1 ; i <= 2 * N ; ++ i) + + if(!Idx[i]) + + Tarjan(i); + + for(vector< set > :: reverse_iterator it = SCComponent.rbegin() , fin = SCComponent.rend() ; it != fin ; ++ it) + + for(set :: iterator i = it->begin(), fi = it->end(); i != fi ; ++ i) { + + int actNode = *i <= N ? *i : Non(*i); + + if(!Ans[actNode]) { + + Ans[actNode] = (*i > N) + 1; + + ans += (*i > N); + + } + + else break; + + } + + cout << ans << "\n"; + + for(int i = 1 ; i <= N ; ++ i) + + if(Ans[i] == 2) + + cout << i << "\n"; + + cin.close(); + + cout.close(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/70ce6f3c-02b2-11ec-92cf-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/70ce6f3c-02b2-11ec-92cf-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a47777133f180dde161af2713681ad695b898aae --- /dev/null +++ b/party/2sat_implication_reduction/70ce6f3c-02b2-11ec-92cf-2cf05d84bfc2.cpp @@ -0,0 +1,293 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const string file = "party"; + + + +const string infile = file + ".in"; + +const string outfile = file + ".out"; + + + + + +vector values; + +vector > G; + + + +int N; + +int M; + + + +inline int non(int x) + +{ + + return x <= N ? x + N : x - N; + +} +vector ind; + +vector lowlink; + +vector assined; + +stack stiva; + +vector > ctc; + + + +void Tarjan(int x, int& c) + +{ + + ind[x] = lowlink[x] = ++c; + + stiva.push(x); + + + + for(vector::iterator itr = G[x].begin(); + + itr != G[x].end(); + + itr++) + + { + + if(ind[*itr] == 0) + + { + + Tarjan(*itr, c); + + lowlink[x] = min(lowlink[x], lowlink[*itr]); + + } + + else if(assined[*itr] == false) + + { + + lowlink[x] = min(lowlink[x], lowlink[*itr]); + + } + + } + + + + + + if(ind[x] == lowlink[x]) + + { + + vector newCtc; + + int nod = -1; + + while(nod != x) + + { + + nod = stiva.top(); + + stiva.pop(); + + assined[nod] = true; + + newCtc.push_back(nod); + + } + + ctc.push_back(newCtc); + + } + + + + + +} + + + +int main() + +{ + + fstream fin(infile.c_str(), ios::in); + + + + fin >> N >> M; + + G.resize(2*N + 1); + + + + for(int i = 0; i < M; i++) + + { + + int x, y, z; + + + + fin >> x >> y >> z; + + if( 2 & z) x = non(x); + + if( 1 & z) y = non(y); + + + + G[non(x)].push_back(y); + + G[non(y)].push_back(x); + + + + } + + + + fin.close(); + + int c = 0; + + ind.resize(2* N + 1); + + lowlink.resize(2 * N + 1); + + assined.resize(2 * N + 1); + + + + for(int i = 1; i <= 2*N; i++) + + { + + if(ind[i] == 0) + + { + + Tarjan(i, c); + + } + + } + + + + + + values.resize(N + 1, -1); + + int size = 0; + + + + for(vector >::reverse_iterator itr = ctc.rbegin(); + + itr != ctc.rend(); + + itr++) + + { + + for(vector::iterator itr2 = itr->begin(); + + itr2 != itr->end(); + + itr2++) + + { + + int value = *itr2 <= N ? 0 : 1; + + int nod = *itr2 <= N ? *itr2 : non(*itr2); + + + + if(values[nod] == -1) + + { + + values[nod] = value; + + if(value == 1) size++; + + } + + else + + { + + break; + + } + + + + } + + } + fstream fout(outfile.c_str(), ios::out); + + fout << size << "\n"; + + for(int i = 1; i <= N; i++) + + { + + if(values[i] == 1) fout << i << "\n"; + + + + } + + + + + + fout.close(); + + + +} diff --git a/party/2sat_implication_reduction/714ee9db-02b2-11ec-a281-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/714ee9db-02b2-11ec-a281-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c41e2b33ef14020aeb62e8cb6a84a57bc8a894 --- /dev/null +++ b/party/2sat_implication_reduction/714ee9db-02b2-11ec-a281-2cf05d84bfc2.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/71e95525-02b2-11ec-9bc5-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/71e95525-02b2-11ec-9bc5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..666480d7deab1aa1447a45d7d44084b361fde88f --- /dev/null +++ b/party/2sat_implication_reduction/71e95525-02b2-11ec-9bc5-2cf05d84bfc2.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + +using namespace std; + +int n; + +int no(int x){ + if(x <= n) + return x + n; + return x - n; +} + +vector graph[205], gt[205]; + +void implying(int x, int y){ + graph[x].push_back(y); + gt[y].push_back(x); +} + +vector st; +int vis[205]; + +void dfs1(int x){ + vis[x] = 1; + for(int i = 0; i < graph[x].size(); ++i) + if(!vis[graph[x][i]]) + dfs1(graph[x][i]); + st.push_back(x); +} + +int impossible, vis2[205], val[205]; + +void dfs2(int x){ + if(val[x]) + impossible = 1; + val[no(x)] = 1; + vis2[x] = 1; + for(int i = 0; i < gt[x].size(); ++i) + if(!vis2[gt[x][i]]) + dfs2(gt[x][i]); +} + +int main(){ + ifstream in("party.in"); + ofstream out("party.out"); + + int m; + in >> n >> m; + + for(int i = 1; i <= m; ++i){ + int x, y, tip; + in >> x; + in >> y; + in >> tip; + if(tip == 0){ + implying(no(x), y); + implying(no(y), x); + } + else if(tip == 1){ + implying(no(x), no(y)); + implying(y, x); + } + else if(tip == 2){ + implying(no(y), no(x)); + implying(x, y); + } + else{ + implying(x, no(y)); + implying(y, no(x)); + } + } + + for(int i = 1; i <= n * 2; ++i) + if(!vis[i]) + dfs1(i); + + for(int i = n * 2 - 1; i >= 0; --i) + if(!vis2[st[i]] && !vis2[no(st[i])]) + dfs2(st[i]); + + int ans = 0; + + for(int i = 1; i <= n; ++i) + if(val[i]) + ++ans; + + assert(ans != 0); + + out << ans << "\n"; + + for(int i = 1; i <= n; ++i) + if(val[i]) + out << i << "\n"; + + return 0; +} diff --git a/party/2sat_implication_reduction/727c4721-02b2-11ec-a7e4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/727c4721-02b2-11ec-a7e4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b245b44a4d594e273b522a8cf143b56fadc30a22 --- /dev/null +++ b/party/2sat_implication_reduction/727c4721-02b2-11ec-a7e4-2cf05d84bfc2.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +using namespace std; + +int n,nc,x,y,i,j,k,nr; +int f[201],ctc[201],s[201],t[201]; +vector v[201],inv[201]; + +inline int nt(int x){return (x+n)%(2*n);} +inline void add(int x,int y){v[nt(x)].push_back(y);v[nt(y)].push_back(x);inv[x].push_back(nt(y));inv[y].push_back(nt(x));} + +void dfs(int x) +{ + if(f[x]) + return; + f[x]=1; + for(vector ::iterator i=v[x].begin();i!=v[x].end();++i) + dfs(*i); + s[++s[0]]=x; +} + +void dfs2(int x,int y) +{ + if(f[x]) + return; + f[x]=1; + ctc[x]=y; + for(int i=0;i +#include +#include + + + + + + +using namespace std; + + + +vector G[200005], GT[200005]; + +int N, TSort[200005], Solution[200005]; + +bool Used[200005]; + + + +inline int Non (int X) + +{ + + if (X<=N) + + { + + return X+N; + + } + + return X-N; + +} + + + +void Read () + +{ + + freopen ("party.in", "r", stdin); + + int M; + + scanf ("%d %d", &N, &M); + + for (; M>0; --M) + + { + + int X, Y, Type; + + scanf ("%d %d %d", &X, &Y, &Type); + + if (Type==0) + + { + + G[Non (X)].push_back (Y); + + G[Non (Y)].push_back (X); + + GT[X].push_back (Non (Y)); + + GT[Y].push_back (Non (X)); + + } + + if (Type==1) + + { + + G[Non (X)].push_back (Non (Y)); + + G[Y].push_back (X); + + GT[Non (Y)].push_back (Non (X)); + + GT[X].push_back (Y); + + } + + if (Type==2) + + { + + G[Non (Y)].push_back (Non (X)); + + G[X].push_back (Y); + + GT[Non (X)].push_back (Non (Y)); + + GT[Y].push_back (X); + + } + + if (Type==3) + + { + + G[X].push_back (Non (Y)); + + G[Y].push_back (Non (X)); + + GT[Non (X)].push_back (Y); + + GT[Non (Y)].push_back (X); + + } + + } + +} + + + +void Print () + +{ + + freopen ("party.out", "w", stdout); + + for (int i=1; i<=N; ++i) + + { + + Solution[0]+=Solution[i]; + + } + + printf ("%d\n", Solution[0]); + + for (int i=1; i<=N; ++i) + + { + + if (Solution[i]!=0) + + { + + printf ("%d\n", i); + + } + + } + +} + + + +inline void DFP (int X) + +{ + + Used[X]=true; + + for (vector :: iterator V=G[X].begin (); V!=G[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFP (*V); + + } + + } + + TSort[++TSort[0]]=X; + +} + + + +inline void DFM (int X) + +{ + + Used[X]=true; + + Solution[Non (X)]=1; + + for (vector :: iterator V=GT[X].begin (); V!=GT[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFM (*V); + + } + + } + +} + + + +void Kosaraju () + +{ + + for (int i=1; i<=N+N; ++i) + + { + + if (!Used[i]) + + { + + DFP (i); + + } + + } + + memset (Used, 0, sizeof (Used)); + + for (int i=TSort[0]; i>0; --i) + + { + + if (!Used[TSort[i]] and !Used[Non (TSort[i])]) + + { + + DFM (TSort[i]); + + } + + } + +} + + + +int main() + +{ + + Read (); + + Kosaraju (); + + Print (); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7350de5f-0b54-11ec-962c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7350de5f-0b54-11ec-962c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6eec04876673ac5e77e67d51053a7707cc48822 --- /dev/null +++ b/party/2sat_implication_reduction/7350de5f-0b54-11ec-962c-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +ifstream cin("a.in"); +ofstream cout("a.out"); + +template +void afis(v Vector, type nr) { + for_each(Vector.begin(),Vector.end(), [](type x) { + cout << x << ' '; + }); +} + +struct nod { + list adi; + list invadi; +}; + +int n; +nod nod[300]; +bitset<300> viz; +vector > ctc; +stack finish; +int nrcomp = 0; +int sol[300]; + +int neg(int nod) { + if (nod <= n) + return nod + n; + else return nod - n; +} + +void edge(const int p1, const int p2) { + nod[p1].adi.push_back(p2); + nod[p2].invadi.push_back(p1); +} + +void fapedfs(const int start) { + viz[start] = true; + for (auto it = nod[start].adi.begin(); it != nod[start].adi.end(); ++it) { + if (!viz[*it]) + fapedfs(*it); + } + finish.push(start); +} + +void invdfs(const int start) { + viz[start] = true; + ctc[nrcomp].emplace_back(start); + for (auto it = nod[start].invadi.begin(); it != nod[start].invadi.end(); ++it) { + if (!viz[*it]) + invdfs(*it); + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + clock_t tStart = clock(); + int m, x, y, cod; + cin >> n >> m; + while (m--) { + cin >> x >> y >> cod; + + switch (cod) { + case 0: { + edge(neg(x), y); + edge(neg(y), x); + break; + } + case 1: { + edge(neg(x), neg(y)); + edge(y, x); + break; + } + case 2: { + edge(neg(y), neg(x)); + edge(x, y); + break; + } + case 3: { + edge(x, neg(y)); + edge(y, neg(x)); + break; + } + } + } + for (int i = 1; i <= 2*n; ++i) { + if (!viz[i]) + fapedfs(i); + } + + viz.reset(); + vector temp; + ctc.push_back(temp); + + while (!finish.empty()) { + if (!viz[finish.top()]) { + ctc.push_back(temp); + ++nrcomp; + invdfs(finish.top()); + } + finish.pop(); + } + for (int i = 0; i <= 2*n; ++i) + sol[i] = -1; + + int participanti = 0; + + for (int i = nrcomp; i >= 1; --i) { + if (sol[ctc[i][0]] == -1) { + for (auto it = ctc[i].begin(); it != ctc[i].end(); ++it) { + sol[*it] = 1; + sol[neg(*it)] = 0; + } + } + } + for (int i = 1; i <= n; ++i) { + if (sol[i] == -1) { + sol[i] = 1; + } + participanti += sol[i]; + } + cout << participanti << '\n'; + for (int i = 1; i <= n; ++i) { + if (sol[i]) + cout << i << '\n'; + } + + printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); +} diff --git a/party/2sat_implication_reduction/735940da-02b2-11ec-b420-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/735940da-02b2-11ec-b420-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f060d8989122d18189f43915d7bd5df2cd0f4eb --- /dev/null +++ b/party/2sat_implication_reduction/735940da-02b2-11ec-b420-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include +#include + +using namespace std; + +namespace tsat { + + + +int N; +vector > G, GT; +vector val; +vector st, viz; +int no_sol; +inline int neg(int n) { + if (n <= N) return n + N; + else return n - N; +} +inline void sau(int a, int b) { + + G[neg(a)].push_back(b); G[neg(b)].push_back(a); + GT[b].push_back(neg(a)); GT[a].push_back(neg(b)); +} +inline void dfs(int nod) { + viz[nod] = 1; + for (int i = 0; i < G[nod].size(); ++i) { + int nod2 = G[nod][i]; + if (!viz[nod2]) dfs(nod2); + } + st.push_back(nod); +} +inline void dfs_t(int nod) { + viz[nod] = 1; + if (val[nod]) no_sol = 1; + val[nod] = 0; val[neg(nod)] = 1; + for (int i = 0; i < GT[nod].size(); ++i) { + int nod2 = GT[nod][i]; + if (!viz[nod2]) dfs_t(nod2); + } +} +int two_sat() { + viz.assign(2*N+1, 0); + for (int i = 1; i <= 2 * N; ++i) + if (!viz[i]) dfs(i); + viz.assign(2*N+1, 0); + for (int i = st.size() - 1; i >= 0; --i) { + if (!viz[st[i]] && !viz[neg(st[i])]) dfs_t(st[i]); + } + return no_sol == 0; +} +void clear() { + N = 0; + G.clear(); GT.clear(); val.clear(); st.clear(); viz.clear(); no_sol = 0; +} +void adapt(); +} + +namespace in { +int N, M; +int rel[1024][3]; +} + +using namespace in; +int main() { + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + scanf("%d %d", &N, &M); + for (int i = 1; i <= M; ++i) scanf("%d %d %d", &rel[i][0], &rel[i][1], &rel[i][2]); + tsat::adapt(); + tsat::two_sat(); + int total = 0; + for (int i = 1; i <= N; ++i) if (tsat::val[i]) ++total; + printf("%d\n", total); + for (int i = 1; i <= N; ++i) if (tsat::val[i]) + printf("%d\n", i); + return 0; +} + +void tsat::adapt() { + clear(); + N = in::N; + G.resize(2 * N + 1); GT.resize(2 * N + 1); + val.resize(2 * N + 1); + for (int i = 1; i <= in::M; ++i) { + switch(rel[i][2]) { + case 0: + sau(rel[i][0], rel[i][1]); + break; + case 1: + sau(rel[i][0], neg(rel[i][1])); + break; + case 2: + sau(neg(rel[i][0]), rel[i][1]); + break; + case 3: + sau(neg(rel[i][0]), neg(rel[i][1])); + break; + } + } +} diff --git a/party/2sat_implication_reduction/73b89ef4-02b2-11ec-b30c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/73b89ef4-02b2-11ec-b30c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b8fc23c63948da7ca342335cd5c571d7da1fdf2 --- /dev/null +++ b/party/2sat_implication_reduction/73b89ef4-02b2-11ec-b30c-2cf05d84bfc2.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +using namespace std; +vector v[10004],vt[10004]; + +int t[10004],nrt,n,m,sol[10004]; + +int a=0,b=0,tip=-1; + +char viz[10004]; + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + int i,vec,lim=v[nod].size(); + + + + for(i=0;i=1;i--) + + if(!viz[t[i]] && !viz[(t[i]<=n ? t[i]+n : t[i]-n)]) + + dfs2(t[i]); + + + + for(i=1;i<=n;i++) + + if(sol[i]) + + sol[0]++; + + + + printf("%d\n",sol[0]); + + + + for(i=1;i<=n;i++) + + if(sol[i]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/74328f65-02b2-11ec-a4c0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/74328f65-02b2-11ec-a4c0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c54bc72aee0f51f267f131e971e91ea7e3c84dbd --- /dev/null +++ b/party/2sat_implication_reduction/74328f65-02b2-11ec-a4c0-2cf05d84bfc2.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + +using namespace std; + + + +typedef vector::iterator vii; + + + +const int MaxN = 205; + + + +vector G[MaxN], GT[MaxN]; + +int N, TSort[MaxN]; + +bool Used[MaxN], Value[MaxN], Solution; + + + +inline int Non(int X) { + + return X > N ? X - N : X + N; + +} + + + +void DFP(int X) { + + Used[X] = X; + + for (vii Y = G[X].begin(); Y != G[X].end(); ++Y) + + if (!Used[*Y]) + + DFP(*Y); + + TSort[++TSort[0]] = X; + +} + + + +void DFM(int X) { + + if (Value[X]) + + Solution = false; + + Value[X] = false, Value[Non(X)] = true; + + Used[X] = false; + + for (vii Y = GT[X].begin(); Y != GT[X].end(); ++Y) + + if (Used[*Y]) + + DFM(*Y); + +} + + + +void Solve2Sat() { + + Solution = true; + + for (int X = 1; X <= 2 * N; ++X) + + if (!Used[X]) + + DFP(X); + + reverse(TSort + 1, TSort + TSort[0] + 1); + + for (int i = 1; i <= TSort[0]; ++i) { + + int X = TSort[i]; + + if (Used[X] && Used[Non(X)]) + + DFM(X); + + } + +} + + + +inline void AddEdge(int X, int Y) { + + G[X].push_back(Y), GT[Y].push_back(X); + +} + + + +inline void Disjunction(int X, int Y) { + + AddEdge(Non(X), Y), AddEdge(Non(Y), X); + +} + + + +void Read() { + + assert(freopen("party.in", "r", stdin)); + + int M; assert(scanf("%d %d", &N, &M) == 2); + + for (; M > 0; --M) { + + int Type, X, Y; assert(scanf("%d %d %d", &X, &Y, &Type) == 3); + + if (Type == 0) + + Disjunction(X, Y); + + if (Type == 1) + + Disjunction(X, Non(Y)); + + if (Type == 2) + + Disjunction(Non(X), Y); + + if (Type == 3) + + Disjunction(Non(X), Non(Y)); + + } + +} + + + +void Print() { + + assert(freopen("party.out", "w", stdout)); + + int NTrue = 0; + + for (int X = 1; X <= N; ++X) + + NTrue += Value[X]; + + printf("%d\n", NTrue); + + for (int X = 1; X <= N; ++X) + + if (Value[X]) + + printf("%d\n", X); + +} + + + +int main() { + + Read(); + + Solve2Sat(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7450ecd6-02b2-11ec-8ef0-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7450ecd6-02b2-11ec-8ef0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87b996184d6439fc19ee66cd8ee2ad1336f971e5 --- /dev/null +++ b/party/2sat_implication_reduction/7450ecd6-02b2-11ec-8ef0-2cf05d84bfc2.cpp @@ -0,0 +1,247 @@ +#include +#include +#include +#include +using namespace std; + + + +const int N = 101; + +const int a[] = {1, 1, -1, -1}; + +const int b[] = {1, -1, 1, -1}; + + + +class Sat{ + + vector etc1[2 * N], *graph; + + vector etc2[2 * N], *trans; + + int etc3[2 * N], *ctc, size; + + bool etc4[2 * N], *use; + + stack S; + + + + void reset(){ + + memset(etc4, false, sizeof(etc4)); + + } + + + + void unalloc(vector& v){ + + vector a; + + a.swap(v); + + } + + + + void dfs(int x){ + + use[x] = true; + + + + for (vector :: iterator it = trans[x].begin() ; it != trans[x].end() ; it++) + + if (!use[*it]) + + dfs(*it); + + + + S.push(x); + + } + + + + void dfs(int x, int C){ + + ctc[x] = C; + + use[x] = true; + + + + for (vector :: iterator it = graph[x].begin() ; it != graph[x].end() ; it++) + + if (!use[*it]) + + dfs(*it, C); + + } + + + + void get_ctc(){ + + int nr = 0; + + + + reset(); + + + + for (int i = -size ; i <= size ; i++) + + if (!use[i]) + + dfs(i); + + + + reset(); + + + + while (!S.empty()){ + + if (!use[S.top()]) + + dfs(S.top(), ++nr); + + + + S.pop(); + + } + + } + +public: + + Sat(){ + + graph = etc1 + N; + + trans = etc2 + N; + + ctc = etc3 + N; + + use = etc4 + N; + + } + + + + void push(int x, int y){ + + graph[-x].push_back(y); + + graph[-y].push_back(x); + + + + trans[x].push_back(-y); + + trans[y].push_back(-x); + + } + + + + vector get_sat(int n){ + + size = n; + + + + get_ctc(); + + + + vector v; + + + + for (int i = 1 ; i <= size ; i++) + + if (ctc[i] == ctc[-i]){ + + v.clear(); + + v.push_back(-1); + + return v; + + } else + + v.push_back(ctc[i] < ctc[-i]); + + return v; + + } + +}; + + + +Sat S; + +int ans[N]; + +vector v; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +int main(){ + + int n, m, x, y, z; + + + + in >> n >> m; + + + + while (m--){ + + in >> x >> y >> z; + + + + S.push(x * a[z], y * b[z]); + + } + + + + v = S.get_sat(n); + + + + for (int i = 0 ; i < v.size() ; i++) + + if (v[i]) + + ans[++ans[0]] = i + 1; + + + + for (int i = 0 ; i <= ans[0] ; i++) + + out << ans[i] << "\n"; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/746efc27-02b2-11ec-843a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/746efc27-02b2-11ec-843a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..924816d31e9bb5775b1a9f818922e8e6866f7b2f --- /dev/null +++ b/party/2sat_implication_reduction/746efc27-02b2-11ec-843a-2cf05d84bfc2.cpp @@ -0,0 +1,309 @@ +#include +#include + + +using namespace std; + + + +const char InFile[]="party.in"; + +const char OutFile[]="party.out"; + +const int MaxN=205; + + + +ifstream fin(InFile); + +ofstream fout(OutFile); + + + +int N,M,x,y,op,ind,comp,C[MaxN],low[MaxN],niv[MaxN],S[MaxN],SOL[MaxN]; + +vector G[MaxN],GC[MaxN],El[MaxN]; + + + +void Tarjan(int nod) + +{ + + low[nod]=niv[nod]=++ind; + + S[++S[0]]=nod; + + for(vector::iterator it=G[nod].begin();it!=G[nod].end();++it) + + { + + int &vcn=*it; + + if(!low[vcn]) + + { + + Tarjan(vcn); + + } + + else if(low[vcn]>0) + + { + + if(low[vcn]::iterator it=GC[nod].begin();it!=GC[nod].end();++it) + + { + + int &vcn=*it; + + if(low[vcn]) + + { + + DFS(vcn); + + } + + } + + S[++S[0]]=nod; + +} + + + +inline int NON(int x) + +{ + + if(x>N) + + { + + return x-N; + + } + + return x+N; + +} + + + +int main() + +{ + + fin>>N>>M; + + for(register int i=1;i<=M;++i) + + { + + fin>>x>>y>>op; + + if(op==1) + + { + + y+=N; + + } + + else if(op==2) + + { + + x+=N; + + } + + else if(op==3) + + { + + y+=N; + + x+=N; + + } + + G[NON(x)].push_back(y); + + G[NON(y)].push_back(x); + + } + + fin.close(); + + + + for(register int i=1;i<=(N<<1);++i) + + { + + if(!low[i]) + + { + + Tarjan(i); + + } + + } + + + + for(register int i=1;i<=(N<<1);++i) + + { + + for(vector::iterator it=G[i].begin();it!=G[i].end();++it) + + { + + GC[C[i]].push_back(C[*it]); + + } + + } + + + + for(register int i=1;i<=(N<<1);++i) + + { + + if(low[i]) + + { + + DFS(i); + + } + + } + + + + while(S[0]) + + { + + int c=S[S[0]]; + + for(vector::iterator it=El[c].begin();it!=El[c].end();++it) + + { + + int &nod=*it; + + if(SOL[nod]==0) + + { + + SOL[nod]=-1; + + SOL[NON(nod)]=1; + + } + + } + + --S[0]; + + } + + + + int nrsol=0; + + for(register int i=1;i<=N;++i) + + { + + if(SOL[i]==1) + + { + + ++nrsol; + + } + + } + + + + fout< +#include +#include +#include + + +using namespace std; + + + +typedef vector::iterator vii; + + + +const int MaxN = 205; + + + +vector G[MaxN], GT[MaxN]; + +int N, TSort[MaxN]; + +bool Used[MaxN], Value[MaxN], Solution; + + + +inline int Non(int X) { + + return X > N ? X - N : X + N; + +} + + + +void DFP(int X) { + + Used[X] = true; + + for (vii Y = G[X].begin(); Y != G[X].end(); ++Y) + + if (!Used[*Y]) + + DFP(*Y); + + TSort[++TSort[0]] = X; + +} + + + +void DFM(int X) { + + if (Value[X]) + + Solution = false; + + Value[X] = false, Value[Non(X)] = true; + + Used[X] = false; + + for (vii Y = GT[X].begin(); Y != GT[X].end(); ++Y) + + if (Used[*Y]) + + DFM(*Y); + +} + + + +void Solve2Sat() { + + Solution = true; + + for (int X = 1; X <= 2 * N; ++X) + + if (!Used[X]) + + DFP(X); + + reverse(TSort + 1, TSort + TSort[0] + 1); + + for (int i = 1; i <= TSort[0]; ++i) { + + int X = TSort[i]; + + if (Used[X] && Used[Non(X)]) + + DFM(X); + + } + +} + + + +inline void AddEdge(int X, int Y) { + + G[X].push_back(Y), GT[Y].push_back(X); + +} + + + +inline void Disjunction(int X, int Y) { + + AddEdge(Non(X), Y), AddEdge(Non(Y), X); + +} + + + +void Read() { + + assert(freopen("party.in", "r", stdin)); + + int M; assert(scanf("%d %d", &N, &M) == 2); + + for (; M > 0; --M) { + + int Type, X, Y; assert(scanf("%d %d %d", &X, &Y, &Type) == 3); + + if (Type == 0) + + Disjunction(X, Y); + + if (Type == 1) + + Disjunction(X, Non(Y)); + + if (Type == 2) + + Disjunction(Non(X), Y); + + if (Type == 3) + + Disjunction(Non(X), Non(Y)); + + } + +} + + + +void Print() { + + assert(freopen("party.out", "w", stdout)); + + int NTrue = 0; + + for (int X = 1; X <= N; ++X) + + NTrue += Value[X]; + + printf("%d\n", NTrue); + + for (int X = 1; X <= N; ++X) + + if (Value[X]) + + printf("%d\n", X); + +} + + + +int main() { + + Read(); + + Solve2Sat(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/75030839-5f37-48f3-a56a-37bdcc244e65.cpp b/party/2sat_implication_reduction/75030839-5f37-48f3-a56a-37bdcc244e65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/75030839-5f37-48f3-a56a-37bdcc244e65.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/751155a1-02b2-11ec-9ead-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/751155a1-02b2-11ec-9ead-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cea448c6dc660d3ad6b09afa1daecbdf5a67dae1 --- /dev/null +++ b/party/2sat_implication_reduction/751155a1-02b2-11ec-9ead-2cf05d84bfc2.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + + + +using namespace std; + +FILE*f=fopen("party.in","r"); +FILE*g=fopen("party.out","w"); + +int n,m,index; +int viz[105<<1],lowest[105<<1],niv[105<<1],comp[105<<1],last[105<<1],gr_in[105<<1],sol[105<<1]; +vectorG[105<<1],W[105<<1],v; +stackst; int inst[105<<1]; +vector< vector >C; +queueQ; + +inline int NOT ( const int &x ){ + + if ( x <= n ){ + return n+x; + } + return x-n; +} + +inline void componenta ( const int &x ){ + int nod; v.clear(); + + do{ + nod = st.top(); v.push_back(nod); comp[nod] = C.size()+1; + st.pop(); inst[nod] = 0; + }while(x != nod); + + C.push_back(v); +} + +void DFS ( int nod ){ + viz[nod] = 1; niv[nod] = lowest[nod] = ++index; + st.push(nod); inst[nod] = 1; + + for ( vector::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){ + int nodvcn = (*itt); + + if ( !viz[nodvcn] ){ + DFS(nodvcn); + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + else{ + if ( inst[nodvcn] ){ + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + } + } + + if ( lowest[nod] == niv[nod] ){ + componenta(nod); + } +} + +inline void CTC () { + + for ( int i = 1 ; i <= (n<<1) ; ++i ){ + if ( !viz[i] ){ + DFS(i); + } + } +} + +inline void solve () { + + int Ncomp = C.size(); + for ( int i = 0 ; i < Ncomp ; ++i ){ + for ( vector::iterator itt = C[i].begin() ; itt != C[i].end() ; ++itt ){ + int x = (*itt); + + for ( vector::iterator vecin = G[x].begin() ; vecin != G[x].end() ; ++vecin ){ + int compvcn = comp[*vecin]; + if ( last[compvcn] != i+1 && compvcn != i+1 ){ + W[i+1].push_back(compvcn); ++gr_in[compvcn]; + last[compvcn] = i+1; + } + } + } + } + + for ( int i = 1 ; i <= Ncomp ; ++i ){ + if ( !gr_in[i] ){ + Q.push(i); + } + } + + int nod; + for ( ; !Q.empty() ; ){ + nod = Q.front(); Q.pop(); + + for ( vector::iterator itt = W[nod].begin() ; itt != W[nod].end() ; ++itt ){ + int nodvcn = (*itt); + --gr_in[nodvcn]; + if ( !gr_in[nodvcn] ) + Q.push(nodvcn); + } + if ( last[nod] == -1 ) continue ; + + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 0; + } + nod = comp[NOT(C[nod-1][0])]; last[nod] = -1; + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 1; + } + } + + int Nsol = 0; + for ( int i = 1 ; i <= n ; ++i ){ + Nsol += sol[i]; + } + fprintf(g,"%d\n",Nsol); + for ( int i = 1 ; i <= n ; ++i ){ + if ( sol[i] ){ + fprintf(g,"%d\n",i); + } + } +} + +int main () { + + fscanf(f,"%d %d",&n,&m); + int a,b,tip; + for ( int i = 1 ; i <= m ; ++i ){ + fscanf(f,"%d %d %d",&a,&b,&tip); + + if ( tip == 1 ){ + b = -b; + } + else{ + if ( tip == 2 ){ + a = -a; + } + else{ + if ( tip == 3 ){ + a = -a; b = -b; + } + } + } + + if ( a < 0 ) a = -a+n; + if ( b < 0 ) b = -b+n; + G[NOT(a)].push_back(b); + G[NOT(b)].push_back(a); + } + + CTC(); + solve(); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/party/2sat_implication_reduction/7533aac7-02b2-11ec-ab98-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7533aac7-02b2-11ec-ab98-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4da5b8a809f3eaa6a56225d718b47110bce6473a --- /dev/null +++ b/party/2sat_implication_reduction/7533aac7-02b2-11ec-ab98-2cf05d84bfc2.cpp @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +using namespace std; +vector G1[210], G2[210], Now, Top; + +vector > V; + +int N, M, A, B, C, D, Tip, ans[210], ctc[210]; + +bool Used[210]; + + + +int Opp(int X) + +{ + + if(X <= N) return X + N; + + return X - N; + +} + + + +void DFS1(int node) + +{ + + Used[node] = 1; + + for(vector :: iterator it = G1[node].begin(); it != G1[node].end(); ++it) + + if(!Used[*it]) + + DFS1(*it); + + Top.push_back(node); + +} + + + +void DFS2(int node, int comp) + +{ + + ctc[node] = comp; + + Now.push_back(node); + + for(vector :: iterator it = G2[node].begin(); it != G2[node].end(); ++it) + + if(!ctc[*it]) + + DFS2(*it, comp); + +} + + + +void Kosaraju() + +{ + + for(int i = 1; i <= 2 * N; i++) + + if(!Used[i]) + + DFS1(i); + + int cnt = 1; + + while(Top.size()) + + { + + if(!ctc[Top.back()]) + + { + + Now.clear(); + + DFS2(Top.back(), cnt); + + cnt ++; + + V.push_back(Now); + + } + + Top.pop_back(); + + } + +} + + + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int i; + + scanf("%i %i", &N, &M); + + for(i = 1; i <= M; i++) + + { + + scanf("%i %i %i", &A, &B, &Tip); + + if(Tip == 0) + + { + + C = Opp(A); + + D = Opp(B); + + } + + if(Tip == 1) + + { + + C = Opp(A); + + D = B; + + B = Opp(B); + + } + + if(Tip == 2) + + { + + C = A; + + D = Opp(B); + + A = Opp(A); + + } + + if(Tip == 3) + + { + + C = A; + + D = B; + + A = Opp(A); + + B = Opp(B); + + } + + G1[C].push_back(B); + + G1[D].push_back(A); + + G2[A].push_back(D); + + G2[B].push_back(C); + + } + + Kosaraju(); + + for(i = 0; i < V.size(); i++) + + { + + if(!ans[i + 1]) ans[i + 1] = 2; + + for(vector :: iterator it = V[i].begin(); it != V[i].end(); ++it) + + if(!ans[ctc[Opp(*it)]]) + + ans[ctc[Opp(*it)]] = 3 - ans[i + 1]; + + } + + int cnt = 0; + + for(i = 1; i <= N; i++) + + if(ans[ctc[i]] == 1) + + cnt ++; + + printf("%i\n", cnt); + + for(i = 1; i <= N; i++) + + if(ans[ctc[i]] == 1) + + printf("%i\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/753b8c32-450d-412f-b863-19072ecea081.cpp b/party/2sat_implication_reduction/753b8c32-450d-412f-b863-19072ecea081.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c41e2b33ef14020aeb62e8cb6a84a57bc8a894 --- /dev/null +++ b/party/2sat_implication_reduction/753b8c32-450d-412f-b863-19072ecea081.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/75525657-02b2-11ec-9527-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/75525657-02b2-11ec-9527-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5da2a7e9c248e4c0a99e6b6714f08c2af9bd0aa6 --- /dev/null +++ b/party/2sat_implication_reduction/75525657-02b2-11ec-9527-2cf05d84bfc2.cpp @@ -0,0 +1,208 @@ +#include +#include +#include +using namespace std; + + + +vector Go[256], Gi[256], discovered, part; + +vector< vector < int > > v; + +int where[256], value[256], n, m, a, b, c, first, second; + +bool used[256]; + + + +inline int getOpposite(int a) + +{ + + if(a <= n) return a + n; + + else return a - n; + +} + + + +void Citire() + +{ + + ifstream in("party.in"); + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + in>>a>>b>>c; + + switch(c) + + { + + case 0: first = getOpposite(a); second = getOpposite(b); break; + + case 1: first = getOpposite(a); second = b; b = getOpposite(b); break; + + case 2: first = a; second = getOpposite(b); a = getOpposite(a);break; + + case 3: first = a; second = b; a = getOpposite(a); b = getOpposite(b); break; + + } + + Go[first].push_back(b); + + Go[second].push_back(a); + + Gi[a].push_back(second); + + Gi[b].push_back(first); + + } + + in.close(); + +} + + + +void DFF(int nod) + +{ + + used[nod] = true; + + for(int i = 0; i < Go[nod].size(); i++) + + if(!used[Go[nod][i]]) + + DFF(Go[nod][i]); + + discovered.push_back(nod); + +} + + + +void DFS(int nod, int value) + +{ + + where[nod] = value; + + part.push_back(nod); + + for(int i = 0; i < Gi[nod].size(); i++) + + if(!where[Gi[nod][i]]) + + DFS(Gi[nod][i], value); + +} + + + +void Kosaraju() + +{ + + for(int i = 1; i <= 2 * n; i++) + + if(!used[i]) DFF(i); + + int contor = 1; + + for(; discovered.size(); discovered.pop_back()) + + if(!where[discovered.back()]) + + { + + part.clear(); + + DFS(discovered.back(), contor++); + + v.push_back(part); + + } + +} + + + +void Assign() + +{ + + for(int i = 0; i < v.size(); i++) + + { + + if(!value[i + 1]) + + value[i + 1] = 2; + + for(int j = 0; j < v[i].size(); j++) + + { + + int nod = v[i][j]; + + if(!value[where[getOpposite(nod)]]) + + value[where[getOpposite(nod)]] = 3 - value[i + 1]; + + } + + } + +} + + + +void Afisare() + +{ + + ofstream out("party.out"); + + int contor = 0; + + for(int i = 1; i <= n; i++) + + if(value[where[i]] == 1) contor++; + + out< +#include +#include + + + + + + +using namespace std; + + + +vector G[200005], GT[200005]; + +int N, TSort[200005], Solution[200005]; + +bool Used[200005]; + + + +inline int Non (int X) + +{ + + if (X<=N) + + { + + return X+N; + + } + + return X-N; + +} + + + +void Read () + +{ + + freopen ("party.in", "r", stdin); + + int M; + + scanf ("%d %d", &N, &M); + + for (; M>0; --M) + + { + + int X, Y, Type; + + scanf ("%d %d %d", &X, &Y, &Type); + + if (Type==0) + + { + + G[Non (X)].push_back (Y); + + G[Non (Y)].push_back (X); + + GT[X].push_back (Non (Y)); + + GT[Y].push_back (Non (X)); + + } + + if (Type==1) + + { + + G[Non (X)].push_back (Non (Y)); + + G[Y].push_back (X); + + GT[Non (Y)].push_back (Non (X)); + + GT[X].push_back (Y); + + } + + if (Type==2) + + { + + G[Non (Y)].push_back (Non (X)); + + G[X].push_back (Y); + + GT[Non (X)].push_back (Non (Y)); + + GT[Y].push_back (X); + + } + + if (Type==3) + + { + + G[X].push_back (Non (Y)); + + G[Y].push_back (Non (X)); + + GT[Non (X)].push_back (Y); + + GT[Non (Y)].push_back (X); + + } + + } + +} + + + +void Print () + +{ + + freopen ("party.out", "w", stdout); + + for (int i=1; i<=N; ++i) + + { + + Solution[0]+=Solution[i]; + + } + + printf ("%d\n", Solution[0]); + + for (int i=1; i<=N; ++i) + + { + + if (Solution[i]!=0) + + { + + printf ("%d\n", i); + + } + + } + +} + + + +inline void DFP (int X) + +{ + + Used[X]=true; + + for (vector :: iterator V=G[X].begin (); V!=G[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFP (*V); + + } + + } + + TSort[++TSort[0]]=X; + +} + + + +inline void DFM (int X) + +{ + + Used[X]=true; + + Solution[Non (X)]=1; + + for (vector :: iterator V=GT[X].begin (); V!=GT[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFM (*V); + + } + + } + +} + + + +void Kosaraju () + +{ + + for (int i=1; i<=N+N; ++i) + + { + + if (!Used[i]) + + { + + DFP (i); + + } + + } + + memset (Used, 0, sizeof (Used)); + + for (int i=TSort[0]; i>0; --i) + + { + + if (!Used[TSort[i]] and !Used[Non (TSort[i])]) + + { + + DFM (TSort[i]); + + } + + } + +} + + + +int main() + +{ + + Read (); + + Kosaraju (); + + Print (); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7683bee6-02b2-11ec-8481-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7683bee6-02b2-11ec-8481-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/7683bee6-02b2-11ec-8481-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/76c582e7-02b2-11ec-8683-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/76c582e7-02b2-11ec-8683-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afa5933da5b2b6e9365226dbb213e816c06e40f3 --- /dev/null +++ b/party/2sat_implication_reduction/76c582e7-02b2-11ec-8683-2cf05d84bfc2.cpp @@ -0,0 +1,133 @@ +#include +#include +#include + + + +using namespace std; + +vector G[200005], GT[200005]; +int N, TSort[200005], Solution[200005]; +bool Used[200005]; + +inline int Non (int X) +{ + if (X<=N) + { + return X+N; + } + return X-N; +} + +void Read () +{ + freopen ("party.in", "r", stdin); + int M; + scanf ("%d %d", &N, &M); + + + for (; M>0; --M) + { + int X, Y, Type; + scanf ("%d %d %d", &X, &Y, &Type); + + if (Type==0) + { + G[Non (X)].push_back (Y); + G[Non (Y)].push_back (X); + GT[X].push_back (Non (Y)); + GT[Y].push_back (Non (X)); + } + if (Type==1) + { + G[Non (X)].push_back (Non (Y)); + G[Y].push_back (X); + GT[Non (Y)].push_back (Non (X)); + GT[X].push_back (Y); + } + if (Type==2) + { + G[Non (Y)].push_back (Non (X)); + G[X].push_back (Y); + GT[Non (X)].push_back (Non (Y)); + GT[Y].push_back (X); + } + if (Type==3) + { + G[X].push_back (Non (Y)); + G[Y].push_back (Non (X)); + GT[Non (X)].push_back (Y); + GT[Non (Y)].push_back (X); + } + } +} + +void Print () +{ + freopen ("party.out", "w", stdout); + for (int i=1; i<=N; ++i) + { + Solution[0]+=Solution[i]; + } + printf ("%d\n", Solution[0]); + for (int i=1; i<=N; ++i) + { + if (Solution[i]!=0) + { + printf ("%d\n", i); + } + } +} + +inline void DFP (int X) +{ + Used[X]=true; + for (vector :: iterator V=G[X].begin (); V!=G[X].end (); ++V) + { + if (!Used[*V]) + { + DFP (*V); + } + } + TSort[++TSort[0]]=X; +} + +inline void DFM (int X) +{ + Used[X]=true; + Solution[Non (X)]=1; + for (vector :: iterator V=GT[X].begin (); V!=GT[X].end (); ++V) + { + if (!Used[*V]) + { + DFM (*V); + } + } +} + +void Kosaraju () +{ + for (int i=1; i<=N+N; ++i) + { + if (!Used[i]) + { + DFP (i); + } + } + memset (Used, 0, sizeof (Used)); + for (int i=TSort[0]; i>0; --i) + { + if (!Used[TSort[i]] and !Used[Non (TSort[i])]) + { + DFM (TSort[i]); + } + } +} + +int main() +{ + Read (); + Kosaraju (); + Print (); + return 0; +} diff --git a/party/2sat_implication_reduction/76e5dc2a-02b2-11ec-affd-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/76e5dc2a-02b2-11ec-affd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edecd46e088ceeab3f9643140c734bc2c77b24d9 --- /dev/null +++ b/party/2sat_implication_reduction/76e5dc2a-02b2-11ec-affd-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +using namespace std; + + + + + +vector v[10004],vt[10004]; +int t[10004],nrt,n,m,sol[10004]; +int a=0,b=0,tip=-1; +char viz[10004]; + +void dfs(int nod) +{ + viz[nod]=1; + int i,vec,lim=v[nod].size(); + + for(i=0;i=1;i--) + if(!viz[t[i]] && !viz[(t[i]<=n ? t[i]+n : t[i]-n)]) + dfs2(t[i]); + + for(i=1;i<=n;i++) + if(sol[i]) + sol[0]++; + + printf("%d\n",sol[0]); + + for(i=1;i<=n;i++) + if(sol[i]) + printf("%d\n",i); + return 0; +} diff --git a/party/2sat_implication_reduction/76f4e01f-fedb-41c7-90f3-fb8e5e872514.cpp b/party/2sat_implication_reduction/76f4e01f-fedb-41c7-90f3-fb8e5e872514.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98c8e349acf9d76a96f39aec3e5dade3be902489 --- /dev/null +++ b/party/2sat_implication_reduction/76f4e01f-fedb-41c7-90f3-fb8e5e872514.cpp @@ -0,0 +1,322 @@ +#include +#include +#include + + +using namespace std; + + + +FILE *f = fopen("party.in","r"); + +FILE *g = fopen("party.out","w"); + + + +const int NMAX = 1e2; + +const int OFFSET = NMAX + 2; + + + +vector graph[2 * OFFSET + 5]; + + + +int n,m; + +bool viz[2 * OFFSET + 5]; + +int st[2 * OFFSET + 5],len; + +bool inst[2 * OFFSET + 5]; + +int ctc[2 * OFFSET + 5],last_ctc; + +int id[2 * OFFSET + 5],last_id; + +int low[2 * OFFSET + 5]; + +vector ctc_nodes[2 * OFFSET + 5]; + + + +void dfs(int nod){ + + viz[nod] = true; + + st[++len] = nod;inst[nod] = true; + + id[nod] = low[nod] = ++last_id; + + + + for(auto it:graph[nod]){ + + if(viz[it] == false){ + + dfs(it); + + } + + if(inst[it]){ + + low[nod] = min(low[nod],low[it]); + + } + + } + + + + if(low[nod] == id[nod]){ + + last_ctc++; + + while(st[len] != nod){ + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + + ctc[st[len]] = last_ctc; + + ctc_nodes[last_ctc].push_back(st[len]); + + inst[st[len]] = false; + + len--; + + } + +} + + + +int ans[2 * OFFSET + 5]; + +int ctc_ans[2 * OFFSET + 5]; + +int ctc_gr[2 * OFFSET + 5]; + + + +vector solve_clauses(){ + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + ans[i] = -1; + + if(viz[i] == false){ + + dfs(i); + + } + + } + + + + for(int i = 1;i <= last_ctc;i++){ + + ctc_ans[i] = -1; + + } + + + + for(int i = OFFSET - n;i <= OFFSET + n;i++){ + + if(i == OFFSET){ + + continue; + + } + + for(auto it:graph[i]){ + + if(ctc[it] != ctc[i]){ + + ctc_gr[ctc[it]]++; + + } + + } + + } + + + + vector topo; + + + + for(int i = 1;i <= last_ctc;i++){ + + if(ctc_gr[i] == 0){ + + topo.push_back(i); + + } + + } + + + + for(int i = 0;i < (int)topo.size();i++){ + + int comp = topo[i]; + + int val = ctc_ans[comp]; + + + + for(auto it:ctc_nodes[comp]){ + + val = max(val,ans[it]); + + } + + + + val = max(val,0); + + + + ctc_ans[comp] = val; + + + + for(auto it:ctc_nodes[comp]){ + + ans[it] = val; + + ans[2 * OFFSET - it] = 1- val; + + for(auto it2:graph[it]){ + + if(ctc[it2] != ctc[it]){ + + ctc_gr[ctc[it2]]--; + + if(ctc_gr[ctc[it2]] == 0){ + + topo.push_back(ctc[it2]); + + } + + if(val == 1){ + + ctc_ans[ctc[it2]] = 1; + + } + + } + + } + + } + + } + + + + vector cnt; + + + + for(int i = OFFSET + 1;i <= OFFSET + n;i++){ + + if(ans[i]){ + + cnt.push_back(i - OFFSET); + + } + + } + + + + return cnt; + +} + + + +int main(){ + + + + fscanf(f,"%d %d",&n,&m); + + + + for(int i = 1;i <= m;i++){ + + int x,y,z; + + fscanf(f,"%d %d %d",&x,&y,&z); + + if(z == 0){ + + graph[OFFSET - x].push_back(OFFSET + y); + + graph[OFFSET - y].push_back(OFFSET + x); + + } + + else if(z == 1){ + + graph[OFFSET - x].push_back(OFFSET - y); + + } + + else if(z == 2){ + + graph[OFFSET - y].push_back(OFFSET - x); + + } + + else{ + + graph[OFFSET + x].push_back(OFFSET - y); + + graph[OFFSET + y].push_back(OFFSET - x); + + } + + } + + + + vector tmp = solve_clauses(); + + + + fprintf(g,"%d\n",(int)tmp.size()); + + + + for(auto it:tmp){ + + fprintf(g,"%d\n",it); + + } + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/7704aeb8-02b2-11ec-8937-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7704aeb8-02b2-11ec-8937-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34c47698a14d8ef81bd688098ce8bf16d7cb1b2b --- /dev/null +++ b/party/2sat_implication_reduction/7704aeb8-02b2-11ec-8937-2cf05d84bfc2.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +using namespace std; + + + +int I[205], minI[205], index, S[205], inS[205], t; +int scc_cnt, node_to_scc[205], scc_in_deg[205]; +vector G[205], scc_G[205], scc_list[205]; + +inline int non(int x, int N){ + return (x > N)? x-N: x+N; +} + +void scc(int n){ + + I[n]=minI[n]=++index; + S[++t]=n; inS[n]=1; + + vector::iterator ii; + for(ii=G[n].begin(); ii!=G[n].end(); ii++){ + if(I[*ii] == -1){ + scc(*ii); + minI[n]=min(minI[n], minI[*ii]); + } + else if(inS[*ii]) + minI[n]=min(minI[n], minI[*ii]); + } + + if(I[n] == minI[n]){ + scc_cnt++; + do { + node_to_scc[S[t]]=scc_cnt; + scc_list[scc_cnt].push_back(S[t]); + inS[S[t]]=0; + } while(S[t--] != n); + } + +} + +int main(){ + + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int N, M, i, a, b, c, l, r, n, aux, guest; + static int Q[205], value[205]; + vector::iterator ii; + + scanf("%d%d", &N, &M); + for(i=0; i N)? *ii-N: *ii; + if(value[aux] == -1){ + value[aux]=(*ii > N)? 1: 0; + guest+=value[aux]; + } + } + for(ii=scc_G[n].begin(); ii!=scc_G[n].end(); ii++){ + scc_in_deg[*ii]--; + if(scc_in_deg[*ii] == 0) + Q[++r]=*ii; + } + } + + printf("%d\n", guest); + for(i=1; i<=N; i++) + if(value[i]) + printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/772526f5-02b2-11ec-90a3-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/772526f5-02b2-11ec-90a3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a85254e7ac3519698b186ebbcc0fbe0be5fc987e --- /dev/null +++ b/party/2sat_implication_reduction/772526f5-02b2-11ec-90a3-2cf05d84bfc2.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +using namespace std; + + + +int I[200010], minI[200010], index, S[200010], inS[200010], t; +int scc_cnt, node_to_scc[200010], scc_in_deg[200010]; +vector G[200010], scc_G[200010], scc_list[200010]; + +inline int non(int x, int N){ + return (x > N)? x-N: x+N; +} + +void scc(int n){ + + I[n]=minI[n]=++index; + S[++t]=n; inS[n]=1; + + vector::iterator ii; + for(ii=G[n].begin(); ii!=G[n].end(); ii++){ + if(I[*ii] == -1){ + scc(*ii); + minI[n]=min(minI[n], minI[*ii]); + } + else if(inS[*ii]) + minI[n]=min(minI[n], minI[*ii]); + } + + if(I[n] == minI[n]){ + scc_cnt++; + do { + node_to_scc[S[t]]=scc_cnt; + scc_list[scc_cnt].push_back(S[t]); + inS[S[t]]=0; + } while(S[t--] != n); + } + +} + +int main(){ + + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int N, M, i, a, b, c, l, r, n, aux, guest; + static int Q[200010], value[200010]; + vector::iterator ii; + + scanf("%d%d", &N, &M); + for(i=0; i N)? *ii-N: *ii; + if(value[aux] == -1){ + value[aux]=(*ii > N)? 1: 0; + guest+=value[aux]; + } + } + for(ii=scc_G[n].begin(); ii!=scc_G[n].end(); ii++){ + scc_in_deg[*ii]--; + if(scc_in_deg[*ii] == 0) + Q[++r]=*ii; + } + } + + printf("%d\n", guest); + for(i=1; i<=N; i++) + if(value[i]) + printf("%d\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/77605981-d049-4a75-ab2e-64455798a5af.cpp b/party/2sat_implication_reduction/77605981-d049-4a75-ab2e-64455798a5af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cea448c6dc660d3ad6b09afa1daecbdf5a67dae1 --- /dev/null +++ b/party/2sat_implication_reduction/77605981-d049-4a75-ab2e-64455798a5af.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + + + +using namespace std; + +FILE*f=fopen("party.in","r"); +FILE*g=fopen("party.out","w"); + +int n,m,index; +int viz[105<<1],lowest[105<<1],niv[105<<1],comp[105<<1],last[105<<1],gr_in[105<<1],sol[105<<1]; +vectorG[105<<1],W[105<<1],v; +stackst; int inst[105<<1]; +vector< vector >C; +queueQ; + +inline int NOT ( const int &x ){ + + if ( x <= n ){ + return n+x; + } + return x-n; +} + +inline void componenta ( const int &x ){ + int nod; v.clear(); + + do{ + nod = st.top(); v.push_back(nod); comp[nod] = C.size()+1; + st.pop(); inst[nod] = 0; + }while(x != nod); + + C.push_back(v); +} + +void DFS ( int nod ){ + viz[nod] = 1; niv[nod] = lowest[nod] = ++index; + st.push(nod); inst[nod] = 1; + + for ( vector::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){ + int nodvcn = (*itt); + + if ( !viz[nodvcn] ){ + DFS(nodvcn); + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + else{ + if ( inst[nodvcn] ){ + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + } + } + + if ( lowest[nod] == niv[nod] ){ + componenta(nod); + } +} + +inline void CTC () { + + for ( int i = 1 ; i <= (n<<1) ; ++i ){ + if ( !viz[i] ){ + DFS(i); + } + } +} + +inline void solve () { + + int Ncomp = C.size(); + for ( int i = 0 ; i < Ncomp ; ++i ){ + for ( vector::iterator itt = C[i].begin() ; itt != C[i].end() ; ++itt ){ + int x = (*itt); + + for ( vector::iterator vecin = G[x].begin() ; vecin != G[x].end() ; ++vecin ){ + int compvcn = comp[*vecin]; + if ( last[compvcn] != i+1 && compvcn != i+1 ){ + W[i+1].push_back(compvcn); ++gr_in[compvcn]; + last[compvcn] = i+1; + } + } + } + } + + for ( int i = 1 ; i <= Ncomp ; ++i ){ + if ( !gr_in[i] ){ + Q.push(i); + } + } + + int nod; + for ( ; !Q.empty() ; ){ + nod = Q.front(); Q.pop(); + + for ( vector::iterator itt = W[nod].begin() ; itt != W[nod].end() ; ++itt ){ + int nodvcn = (*itt); + --gr_in[nodvcn]; + if ( !gr_in[nodvcn] ) + Q.push(nodvcn); + } + if ( last[nod] == -1 ) continue ; + + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 0; + } + nod = comp[NOT(C[nod-1][0])]; last[nod] = -1; + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 1; + } + } + + int Nsol = 0; + for ( int i = 1 ; i <= n ; ++i ){ + Nsol += sol[i]; + } + fprintf(g,"%d\n",Nsol); + for ( int i = 1 ; i <= n ; ++i ){ + if ( sol[i] ){ + fprintf(g,"%d\n",i); + } + } +} + +int main () { + + fscanf(f,"%d %d",&n,&m); + int a,b,tip; + for ( int i = 1 ; i <= m ; ++i ){ + fscanf(f,"%d %d %d",&a,&b,&tip); + + if ( tip == 1 ){ + b = -b; + } + else{ + if ( tip == 2 ){ + a = -a; + } + else{ + if ( tip == 3 ){ + a = -a; b = -b; + } + } + } + + if ( a < 0 ) a = -a+n; + if ( b < 0 ) b = -b+n; + G[NOT(a)].push_back(b); + G[NOT(b)].push_back(a); + } + + CTC(); + solve(); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/party/2sat_implication_reduction/7790e0ff-02b2-11ec-84a4-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7790e0ff-02b2-11ec-84a4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f4460dbaccf4cfd27102bf01eb7ad86217a06a3 --- /dev/null +++ b/party/2sat_implication_reduction/7790e0ff-02b2-11ec-84a4-2cf05d84bfc2.cpp @@ -0,0 +1,232 @@ +#include +#include +#include +#include + + +using namespace std; +vector< int > G[256],GT[256]; + +stack< int > Q; + +bool viz[256],SOL[256]; + +int N,M,ANS; + + + +inline int mnot( int value ) + +{ + + return value<=N ? value+N : value-N; + +} + + + +inline void push_edge( int node1, int node2 ) + +{ + + G[node1].push_back( node2 ); + + GT[node2].push_back( node1 ); + +} + + + +void DF1( int node ) + +{ + + viz[node]=1; + + + + unsigned int i,v; + + for(i=0; i +# include +# include +# include +using namespace std; + + + +const char *FIN = "party.in", *FOU = "party.out"; + +const int MAX = 215; + + + +int N, M; + +vector GP[MAX], GM[MAX], Q; + +bool VIZ[MAX], sol[MAX]; + + + +inline int NOT (int A) { + + return A > N ? A - N : A + N; + +} + + + +void dfp (int S) { + + VIZ[S] = 1; + + for (vector :: iterator it = GP[S].begin (); it != GP[S].end (); ++it) + + if (VIZ[*it] == 0) + + dfp (*it); + + Q.push_back (S); + +} + + + +void dfm (int S) { + + VIZ[S] = sol[NOT(S)] = 1; + + for (vector :: iterator it = GM[S].begin (); it != GM[S].end (); ++it) + + if (VIZ[*it] == 0) + + dfm (*it); + +} + + + +void baga (int x, int y) { + + GP[NOT (x)].push_back (y); + + GP[NOT (y)].push_back (x); + + GM[x].push_back (NOT (y)); + + GM[y].push_back (NOT (x)); + +} + + + +int main (void) { + + freopen (FIN, "r", stdin); + + + + scanf ("%d %d", &N, &M); + + for (int i = 1, x, y, z; i <= M; ++i) { + + scanf ("%d %d %d", &x, &y, &z); + + if (z == 0) baga (x, y); + + else if (z == 1) baga (x, NOT (y)); + + else if (z == 2) baga (NOT (x), y); + + else baga (NOT (x), NOT (y)); + + } + + for (int i = 1; i <= N << 1; ++i) + + if (VIZ[i] == 0) + + dfp (i); + + memset (VIZ, 0, sizeof (VIZ)), reverse (Q.begin (), Q.end ()); + + for (vector :: iterator it = Q.begin (); it != Q.end (); ++it) + + if (VIZ[*it] == 0 && VIZ[NOT (*it)] == 0) + + dfm (*it); + + Q.resize (0); + + for (int i = 1; i <= N; ++i) + + if (sol[i]) Q.push_back (i); + + + + freopen (FOU, "w", stdout); + + printf ("%d\n", Q.size ()); + + for (size_t i = 0; i < Q.size (); ++i) + + printf ("%d\n", Q[i]); + +} diff --git a/party/2sat_implication_reduction/77e3bbf9-02b2-11ec-b7dc-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/77e3bbf9-02b2-11ec-b7dc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd51819b75e2facb2143d399b7397bd31fe30a3b --- /dev/null +++ b/party/2sat_implication_reduction/77e3bbf9-02b2-11ec-b7dc-2cf05d84bfc2.cpp @@ -0,0 +1,97 @@ +#include +#include +#include + +using namespace std; + + + +int n, m, tip, nod, nc, x, y, i, j, k, nr; +int f[220], ctc[220], s[220], t[220]; +vector v[220], inv[220]; + +int nt(int x) +{ + return (x+n)%(2*n); +} + +void add(int x, int y) +{ + v[nt(x)].push_back(y); + v[nt(y)].push_back(x); + inv[x].push_back(nt(y)); + inv[y].push_back(nt(x)); +} + +void df1(int nod) +{ + if(f[nod]) + return; + f[nod]=1; + + for(int i=0; i +#include +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/7824958b-02b2-11ec-a8a1-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7824958b-02b2-11ec-a8a1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aa7b765f396b4afafa7af2f8c12cd26f4aa107c --- /dev/null +++ b/party/2sat_implication_reduction/7824958b-02b2-11ec-a8a1-2cf05d84bfc2.cpp @@ -0,0 +1,232 @@ +#include +#include +#include +#include + + +using namespace std; +vector< int > G[256],GT[256]; + +stack< int > Q; + +bool viz[256],SOL[256]; + +int N,M,ANS; + + + +inline int mnot( int value ) + +{ + + return value<=N ? value+N : value-N; + +} + + + +inline void push_edge( int node1, int node2 ) + +{ + + G[node1].push_back( node2 ); + + GT[node2].push_back( node1 ); + +} + + + +void DF1( int node ) + +{ + + viz[node]=1; + + + + int i,v; + + for(i=0; i<(unsigned)G[node].size(); ++i) + + { + + v=G[node][i]; + + if( !viz[v] ) + + DF1(v); + + } + + + + Q.push( node ); + +} + + + +void DF2( int node ) + +{ + + viz[node]=0; + + SOL[ mnot(node) ]=1; + + SOL[node]=0; + + + + int i,v; + + for(i=0; i<(unsigned)GT[node].size(); ++i) + + { + + v=GT[node][i]; + + if( viz[v] ) + + DF2(v); + + } + +} + + + +int main() + +{ + + assert( freopen("party.in","r",stdin)!=NULL ); + + assert( freopen("party.out","w",stdout)!=NULL ); + + + + assert( scanf("%d%d",&N,&M)!=EOF ); + + + + int i,a1,a2,a3; + + for(i=1; i<=M; ++i) + + { + + assert( scanf("%d%d%d",&a1,&a2,&a3)!=EOF ); + + switch( a3 ) + + { + + case 0: + + { + + push_edge( mnot(a1), a2 ); + + push_edge( mnot(a2), a1 ); + + break; + + } + + case 1: + + { + + push_edge( mnot(a1), mnot(a2) ); + + push_edge( a2, a1 ); + + break; + + } + + case 2: + + { + + push_edge( mnot(a2), mnot(a1) ); + + push_edge( a1, a2 ); + + break; + + } + + case 3: + + { + + push_edge( a1, mnot(a2) ); + + push_edge( a2, mnot(a1) ); + + break; + + } + + } + + } + + + + for(i=1; i<=(N<<1); ++i) + + { + + if( !viz[i] ) + + DF1( i ); + + } + + + + int node; + + while( !Q.empty() ) + + { + + node=Q.top(); + + Q.pop(); + + + + if( viz[node] && viz[ mnot(node) ] ) + + DF2( node ); + + } + + + + for(i=1; i<=N; ++i) + + ANS+=SOL[i]; + + + + printf("%d\n",ANS); + + + + for(i=1; i<=N; ++i) + + if( SOL[i] ) + + printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/78459dc9-02b2-11ec-8570-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/78459dc9-02b2-11ec-8570-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81deb946e6e4ea9fb6a4e67fdb8915f64033ca4d --- /dev/null +++ b/party/2sat_implication_reduction/78459dc9-02b2-11ec-8570-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +#include +#include + + +using namespace std; + + + +int n,nc,x,y,i,j,k,nr; + +int f[201],ctc[201],s[201],t[201]; + +vector v[201],inv[201]; + + + +inline int nt(int x){return (x+n)%(2*n);} + +inline void add(int x,int y){v[nt(x)].push_back(y);v[nt(y)].push_back(x);inv[x].push_back(nt(y));inv[y].push_back(nt(x));} + + + +void dfs(int x) + +{ + + if(f[x]) + + return; + + f[x]=1; + + for(vector ::iterator i=v[x].begin();i!=v[x].end();++i) + + dfs(*i); + + s[++s[0]]=x; + +} + + + +void dfs2(int x,int y) + +{ + + if(f[x]) + + return; + + f[x]=1; + + ctc[x]=y; + + for(int i=0;i +#include +using namespace std; + +const int N = 205; + +int v[N], n, m, post[N], nr, value[N], ok; + +vector a[N], at[N]; + + + +inline int neg(int a){ + + if(a > n) + + return a - n; + + return a + n; + +} + + + +void dfs(int k) { + + int i; + + v[k] = 1; + + for(i = 0 ; i < a[k].size(); ++i) + + if(!v[a[k][i]]) + + dfs(a[k][i]); + + post[++nr] = k; + +} + + + +void dfst(int k) { + + int i; + + v[k] = 0; + + + + if(value[k]) + + ok = 0; + + value[k] = 0; value[neg(k)] = 1; + + for(i = 0; i < at[k].size(); ++i) + + if(v[at[k][i]]) + + dfst(at[k][i]); + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int i, x, y, op; + + scanf("%d %d", &n, &m); + + for(i = 1; i <= m; ++i){ + + scanf("%d %d %d", &x, &y, &op); + + if(op == 0){ + + a[neg(x)].push_back(y); + + a[neg(y)].push_back(x); + + at[y].push_back(neg(x)); + + at[x].push_back(neg(y)); + + } + + + + if(op == 2) { + + a[x].push_back(y); + + at[y].push_back(x); + + a[neg(y)].push_back(neg(x)); + + at[neg(x)].push_back(neg(y)); + + } + + + + if(op == 1) { + + a[neg(x)].push_back(neg(y)); + + at[neg(y)].push_back(neg(x)); + + a[y].push_back(x); + + at[x].push_back(y); + + } + + + + if(op == 3) { + + a[x].push_back(neg(y)); + + at[neg(y)].push_back(x); + + a[y].push_back(neg(x)); + + at[neg(x)].push_back(y); + + } + + } + + + + for(i = 1; i <=2 * n; ++i) + + if(!v[i]) + + dfs(i); + + + + + + ok = 1; + + for(i = nr; i >= 1; --i) + + if(v[post[i]] && v[neg(post[i])]) + + dfst(post[i]); + + nr = 0; + + if(ok) + + for(i = 1; i <= n; ++i) + + if(value[i]) + + post[++nr] = i; + + printf("%d\n", nr); + + for(i = 1; i <= nr; ++i) + + printf("%d\n", post[i]); + + return 0; + +} diff --git a/party/2sat_implication_reduction/789ecddd-319f-4396-86a9-ec8d6bfc9481.cpp b/party/2sat_implication_reduction/789ecddd-319f-4396-86a9-ec8d6bfc9481.cpp new file mode 100644 index 0000000000000000000000000000000000000000..333edac894e9ebe91c9181a9b3c276c4084dea14 --- /dev/null +++ b/party/2sat_implication_reduction/789ecddd-319f-4396-86a9-ec8d6bfc9481.cpp @@ -0,0 +1,198 @@ +#include +#include +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/78dcb82f-02b2-11ec-aeeb-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/78dcb82f-02b2-11ec-aeeb-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22a0e07e7df45e018753398ffaccd6d25bee88ee --- /dev/null +++ b/party/2sat_implication_reduction/78dcb82f-02b2-11ec-aeeb-2cf05d84bfc2.cpp @@ -0,0 +1,161 @@ +#include #include +#include +using namespace std; + + + + + +vector G[210], Gt[210], V[210]; + +int C[210], R[210], out[210]; + +int viz[210], st[210]; + +int n, m, k; + +int pun = 1, comp, pas; + +inline int real(int x){ + + if(x > 0) return x; + + return n-x; + +} + +inline void go_muchii(int x, int y){ + + G[real(-x)].push_back(real(y)); + + G[real(-y)].push_back(real(x)); + + + + Gt[real(y)].push_back(real(-x)); + + Gt[real(x)].push_back(real(-y)); + +} + +void df(int x, vector G[]){ + + viz[x] = 1; + + if(!pas) C[x] = comp; + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(!viz[*i]) + + df(*i, G); + + out[x] = ++k; + + if(pun) st[++st[0]] = x; + +} + +void CTC(){ + + for(int i = 1; i <= 2*n; ++i) + + if(!viz[i]) + + df(i, G); + + pun = 0; + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + for(int i = st[0]; i; --i) + + if(!viz[st[i]]){ + + comp++; + + df(st[i], Gt); + + } + + + + for(int x = 1; x <= 2*n; ++x) + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(C[x] != C[*i]) V[C[x]].push_back(C[*i]); + + + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + k = 0; + + pas = 1; + + for(int x = 1; x <= comp; ++x) + + if(!viz[x]) + + df(x, V); + +} + +void sat() { + + for(int x = 1; x <= n; ++x) + + if( out[C[x]] < out[C[real(-x)]]) R[C[x]] = 1; + + else R[C[real(-x)]] = 1; + + int c = 0; + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) c++; + + printf("%d\n", c); + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) printf("%d\n", i); + +} + +int main(){ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int x, y, c; + + scanf("%d%d%d", &x, &y, &c); + + if(c==0) ; + + else if(c == 1) y = -y; + + else if(c==2) x = -x; + + else {x = -x; y = -y;} + + go_muchii(x, y); + + } + + CTC(); + + sat(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/78fc4e04-02b2-11ec-813d-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/78fc4e04-02b2-11ec-813d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36080f1097295dd131ca98b55f1a862dece39dcf --- /dev/null +++ b/party/2sat_implication_reduction/78fc4e04-02b2-11ec-813d-2cf05d84bfc2.cpp @@ -0,0 +1,229 @@ +#include +#include + + +using namespace std; + + + +const int N = 2005; + + + +vector L[N], LT[N]; + + + +int n, v[N]; + + + +bool sol[N], viz[N]; + + + +int non(int x) { + + return x > n ? (x - n) : (x + n); + +} + + + +void trage_muchii(int x, int y) { + + L[non(x)].push_back(y); + + L[non(y)].push_back(x); + + LT[x].push_back(non(y)); + + LT[y].push_back(non(x)); + +} + + + +void read() { + + int type, x, y, m; + + + + scanf("%d%d", &n, &m); + + + + for (int i = 1; i <= m; ++ i) { + + scanf("%d%d%d", &x, &y, &type); + + + + if (type == 0) { + + trage_muchii(x, y); + + continue; + + } + + + + if (type == 1) { + + trage_muchii(x, non(y)); + + continue; + + } + + + + if (type == 2) { + + trage_muchii(non(x), y); + + continue; + + } + + + + trage_muchii(non(x), non(y)); + + } + +} + + + +void make_vec(int nod) { + + viz[nod] = 1; + + + + for (int i = 0; i < L[nod].size(); ++ i) + + if (! viz[L[nod][i]]) + + make_vec(L[nod][i]); + + + + v[++ v[0]] = nod; + +} + + + +void init() { + + for (int i = 1; i <= 2 * n; ++ i) + + if (! viz[i]) + + make_vec(i); + +} + + + +void reset(bool f[N]) { + + for (int i = 1; i <= 2 * n; ++ i) + + f[i] = false; + +} + + + +void ctc(int nod) { + + viz[nod] = 1; + + sol[non(nod)] = 1; + + for (int i = 0; i < LT[nod].size(); ++ i) + + if (! viz[LT[nod][i]]) + + ctc(LT[nod][i]); + +} + + + +void solve() { + + reset(viz); + + + + for (int i = 2 * n; i >= 1; -- i) + + if ((! viz[v[i]]) && (! viz[non(v[i])])) + + ctc(v[i]); + +} + + + +void afis() { + + int rez = 0; + + + + for (int i = 1; i <= n; ++ i) + + if (sol[i]) + + ++ rez; + + + + printf("%d\n", rez); + + + + for (int i = 1; i <= n; ++ i) + + if (sol[i]) + + printf("%d\n", i); + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + read(); + + + + init(); + + + + solve(); + + + + afis(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/796502cb-3700-4af9-92d5-6e4d9f560056.cpp b/party/2sat_implication_reduction/796502cb-3700-4af9-92d5-6e4d9f560056.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/796502cb-3700-4af9-92d5-6e4d9f560056.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/79f294db-02b2-11ec-b335-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/79f294db-02b2-11ec-b335-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c11305dc56850c9a785e23a9aa559e7e48e6c127 --- /dev/null +++ b/party/2sat_implication_reduction/79f294db-02b2-11ec-b335-2cf05d84bfc2.cpp @@ -0,0 +1,195 @@ +#include +#include + + +using namespace std; + + + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + + + +vector G[210], T[210]; + + + +int non ( int x ) + +{ + + if( x <= n ) return x+n; + + return x - n ; + +} + + + +void sortaret ( int nod ) + +{ + + int N = G[nod].size(),i; + + + + viz[nod] = 1 ; + + + + for( i = 0 ; i < N ; i++ ) + + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + + + S[--nn] = nod ; + +} + + + +void dfs ( int nod ) + +{ + + int N = T[nod].size(),i; + + + + viz[nod] = 0 ; + + + + sol[ non(nod) ] = 1 ; + + if( nod > n ) invitati++; + + + + for( i = 0 ; i < N ; i++ ) + + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + + + for( i = 1 ; i <= m ; i++ ) + + { + + scanf("%d %d %d",&a,&b,&tip); + + + + if( tip == 0 ) + + { + + G[non(a)].push_back(b); + + G[non(b)].push_back(a); + + T[a].push_back(non(b)); + + T[b].push_back(non(a)); + + } + + else if( tip == 1 ) + + { + + G[non(a)].push_back(non(b)); + + G[b].push_back(a); + + + + T[non(b)].push_back(non(a)); + + T[a].push_back(b); + + } + + else if( tip == 2 ) + + { + + G[non(b)].push_back(non(a)); + + G[a].push_back(b); + + + + T[non(a)].push_back(non(b)); + + T[b].push_back(a); + + } + + else + + { + + G[a].push_back(non(b)); + + G[b].push_back(non(a)); + + T[non(a)].push_back(b); + + T[non(b)].push_back(a); + + } + + } + + + + nn = n + n + 1 ; + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( !viz[i]) sortaret(i); + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + + dfs( S[i] ) ; + + + + printf("%d\n",invitati); + + + + for( i = 1 ; i <= n ; i++ ) + + if( sol[i] ) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/79f907f7-1719-49b0-bbe2-cab2ec65c008.cpp b/party/2sat_implication_reduction/79f907f7-1719-49b0-bbe2-cab2ec65c008.cpp new file mode 100644 index 0000000000000000000000000000000000000000..333edac894e9ebe91c9181a9b3c276c4084dea14 --- /dev/null +++ b/party/2sat_implication_reduction/79f907f7-1719-49b0-bbe2-cab2ec65c008.cpp @@ -0,0 +1,198 @@ +#include +#include +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/7a12ee1b-02b2-11ec-8fa2-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7a12ee1b-02b2-11ec-8fa2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..261eae90961b0742e8cb59ecb016b7f5a771111a --- /dev/null +++ b/party/2sat_implication_reduction/7a12ee1b-02b2-11ec-8fa2-2cf05d84bfc2.cpp @@ -0,0 +1,159 @@ +#include #include +#include +using namespace std; + + + + + +vector G[210], Gt[210], V[210]; + +int C[210], R[210], out[210]; + +int viz[210], st[210]; + +int n, m, k; + +int pun = 1, comp, pas; + +inline int real(int x){ + + if(x > 0) return x; + + return n-x; + +} + +inline void go_muchii(int x, int y){ + + G[real(-x)].push_back(real(y)); + + G[real(-y)].push_back(real(x)); + + + + Gt[real(y)].push_back(real(-x)); + + Gt[real(x)].push_back(real(-y)); + +} + +void df(int x, vector G[]){ + + viz[x] = 1; + + if(!pas) C[x] = comp; + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(!viz[*i]) + + df(*i, G); + + out[x] = ++k; + + if(pun) st[++st[0]] = x; + +} + +void CTC(){ + + for(int i = 1; i <= 2*n; ++i) + + if(!viz[i]) + + df(i, G); + + pun = 0; + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + for(int i = st[0]; i; --i) + + if(!viz[st[i]]){ + + comp++; + + df(st[i], Gt); + + } + + + + for(int x = 1; x <= 2*n; ++x) + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(C[x] != C[*i]) V[C[x]].push_back(C[*i]); + + + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + k = 0; + + pas = 1; + + for(int x = 1; x <= comp; ++x) + + if(!viz[x]) + + df(x, V); + +} + +void sat() { + + for(int x = 1; x <= n; ++x) + + if( out[C[x]] < out[C[real(-x)]]) R[C[x]] = 1; + + else R[C[real(-x)]] = 1; + + int c = 0; + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) c++; + + printf("%d\n", c); + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) printf("%d\n", i); + +} + +int main(){ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int x, y, c; + + scanf("%d%d%d", &x, &y, &c); + + if(c==0) ; + + else if(c == 1) y = -y; + + else if(c==2) x = -x; + + else {x = -x; y = -y;} + + go_muchii(x, y); + + } + + CTC(); + + sat(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7a52b641-02b2-11ec-af51-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7a52b641-02b2-11ec-af51-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec6de1155fdef9bc7d383aef9ffe5440eecdc0a5 --- /dev/null +++ b/party/2sat_implication_reduction/7a52b641-02b2-11ec-af51-2cf05d84bfc2.cpp @@ -0,0 +1,161 @@ +#include #include +#include +using namespace std; + + + + + +vector G[210], Gt[210], V[210]; + +int C[210], R[210], out[210]; + +int viz[210], st[210]; + +int n, m, k; + +int pun = 1, comp, pas; + +inline int real(int x){ + + if(x > 0) return x; + + return n-x; + +} + +inline void go_muchii(int x, int y){ + + G[real(-x)].push_back(real(y)); + + G[real(-y)].push_back(real(x)); + + + + Gt[real(y)].push_back(real(-x)); + + Gt[real(x)].push_back(real(-y)); + +} + +void df(int x, vector G[]){ + + viz[x] = 1; + + if(!pas) C[x] = comp; + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(!viz[*i]) + + df(*i, G); + + out[x] = ++k; + + if(pun) st[++st[0]] = x; + +} + +void CTC(){ + + for(int i = 1; i <= 2*n; ++i) + + if(!viz[i]) + + df(i, G); + + pun = 0; + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + for(int i = st[0]; i; --i) + + if(!viz[st[i]]){ + + comp++; + + df(st[i], Gt); + + } + + + + for(int x = 1; x <= 2*n; ++x) + + for(vector::iterator i = G[x].begin(); i != G[x].end(); ++i) + + if(C[x] != C[*i]) V[C[x]].push_back(C[*i]); + + + + memset(viz, 0, (2*n+3)*sizeof(viz[0])); + + k = 0; + + pas = 1; + + for(int x = 1; x <= comp; ++x) + + if(!viz[x]) + + df(x, V); + +} + +void sat() { + + for(int x = 1; x <= n; ++x) + + if( out[C[x]] < out[C[real(-x)]]) R[C[x]] = 1; + + else R[C[real(-x)]] = 1; + + int c = 0; + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) c++; + + printf("%d\n", c); + + for(int i = 1; i <= n; ++i) + + if(R[C[i]]) printf("%d\n", i); + +} + +int main(){ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; ++i){ + + int x, y, c; + + scanf("%d%d%d", &x, &y, &c); + + if(c==0) ; + + else if(c == 1) y = -y; + + else if(c==2) x = -x; + + else {x = -x; y = -y;} + + go_muchii(x, y); + + } + + CTC(); + + sat(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/7afe8fa5-02b2-11ec-915c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7afe8fa5-02b2-11ec-915c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91f0ed20a5ce899fc7f168f04ee5febcd01308e3 --- /dev/null +++ b/party/2sat_implication_reduction/7afe8fa5-02b2-11ec-915c-2cf05d84bfc2.cpp @@ -0,0 +1,141 @@ +#include +#include + + + + +bool ok=true,viz[4000005]; + +int sol[4000005],ord[4000005],n,m,nr; + + + +struct nod { + + int x; + + nod *urm; + +} *g[4000005],*gt[4000005]; + + + +inline int non (int a) { + + return (a>n)?a-n:a+n; + +} + + + +void adaugare(int x, int y, bool z) { + + nod *p; + + p=new nod; + + p->x=y; + + if(z) { + + p->urm=g[x]; + + g[x]=p; + + }else { + + p->urm=gt[x]; + + gt[x]=p; + + } + +} + + + +void dfs(int sursa) { + + nod *p; + + if(viz[sursa]) return; + + viz[sursa]=true; + + for(p=g[sursa];p!=NULL; p=p->urm) + + if(!viz[p->x]) dfs(p->x); + + ord[++nr]=sursa; + +} + + + +void dfs_2(int sursa) { + + nod *p; + + if(viz[sursa]) return; + + viz[sursa]=true; + + sol[non(sursa)]=1; + + for(p=gt[sursa];p!=NULL; p=p->urm) + + if(!viz[p->x]) dfs_2(p->x); + +} + + + +int main() + +{ + + int x,y,z,i; + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for(i=1; i<=m; ++i) { + + scanf("%d %d %d",&x,&y,&z); + + if(z==1) y+=n; + + else if(z==2) x+=n; + + else if(z==3) y+=n,x+=n; + + adaugare(non(x),y,true); + + adaugare(non(y),x,true); + + adaugare(y,non(x),false); + + adaugare(x,non(y),false); + + } + + for(i=1; i<=n+n; ++i) if(!viz[i]) dfs(i); + + memset(viz,0,sizeof(viz)); + + for(i=n+n; i; --i) if(!viz[ord[i]] && !viz[non(ord[i])]) dfs_2(ord[i]); + + nr=0; + + for(i=1; i<=n; ++i) if(sol[i]) ++nr; + + printf("%d\n",nr); + + for(i=1; i<=n; ++i) if(sol[i]) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7b5cb532-02b2-11ec-8048-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7b5cb532-02b2-11ec-8048-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbb9db317f65cf277e09cd2959f78a2e1ace9035 --- /dev/null +++ b/party/2sat_implication_reduction/7b5cb532-02b2-11ec-8048-2cf05d84bfc2.cpp @@ -0,0 +1,225 @@ +#include +#include +#include +#include +#include + + +using namespace std; +int n,m; + +vector G[232133]; + +vector Gt[232133]; + +int ok,nr; + +int viz[232133]; + +int sol[232133]; + +int ord[232133]; + + + + + +inline int neg(int a) + +{ + + if (a>n) + + return a-n; + + else + + return a+n; + +} + + + +void baga(int x, int y) + +{ + + G[2*n-x+1].push_back(y); + + G[2*n-y+1].push_back(x); + + Gt[y].push_back(2*n-x+1); + + Gt[x].push_back(2*n-y+1); + +} + + + + + +void citire() + +{ + + int i,a,b,c; + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d", &n, &m); + + while(m--) + + { + + scanf("%d %d %d", &a, &b,&c); + + a+=n*((c&2)!=0); + + b+=n*(c&1); + + G[neg(a)].push_back(b); + + Gt[b].push_back(neg(a)); + + G[neg(b)].push_back(a); + + Gt[a].push_back(neg(b)); + + + + } + + + +} + + + +void dfs(int nod) + +{ + + int i; + + if (viz[nod]) + + return ; + + viz[nod]=1; + + for (i=0;i=1;--i) + + if (!viz[ord[i]] && !viz[neg(ord[i])]) + + dfst(ord[i]); + + nr=0; + + for (i=1;i<=n;++i) + + if (sol[i]) { + + + + nr++; + + } + + printf("%d\n", nr); + + for (i=1;i<=n;++i) + + if (sol[i]) { + + printf("%d\n", i); + + + + } + +} + + + +int main() + +{ + + citire(); + + solve(); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/7bbd4bd2-02b2-11ec-ba5f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7bbd4bd2-02b2-11ec-ba5f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcb7b4d3f86ebd181bb3f1e5b1c51174537a1b68 --- /dev/null +++ b/party/2sat_implication_reduction/7bbd4bd2-02b2-11ec-ba5f-2cf05d84bfc2.cpp @@ -0,0 +1,205 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef vector VI; + +typedef pair pi; + +typedef vector VS; + +template string toString(T n) {ostringstream ost;ost< A((1<<8)),B((1<<8)); + +bitset<(1<<8)> viz,Sol; + +bool Aedge[(1<<8)][(1<<8)],Bedge[(1<<8)][(1<<8)]; + + + +inline void add_edge(int x,int y) + +{ + + if(!Aedge[x][y]) + + { + + A[x].push_back(y); + + Aedge[x][y] = true; + + } + + if(!Bedge[y][x]) + + { + + B[y].push_back(x); + + Bedge[y][x] = true; + + } + +} + + + +inline void scan() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + int x,y,z; + + for(int (i)=(1);(i)<=(M);++(i)) + + { + + scanf("%d%d%d",&x,&y,&z); + + switch(z) + + { + + case 0: { add_edge( ( (x) <= N ? (x) + N : (x) - N ),y); add_edge( ( (y) <= N ? (y) + N : (y) - N ),x ); break;}; + + case 1: { add_edge( ( (x) <= N ? (x) + N : (x) - N ),( (y) <= N ? (y) + N : (y) - N ) ); add_edge( y,x ); break;}; + + case 2: { add_edge( ( (y) <= N ? (y) + N : (y) - N ),( (x) <= N ? (x) + N : (x) - N ) ); add_edge( x,y ); break;}; + + case 3: { add_edge( x,( (y) <= N ? (y) + N : (y) - N ) ); add_edge( y,( (x) <= N ? (x) + N : (x) - N ) ); break;}; + + } + + } + +} + + + +inline void DFP(int nod) + +{ + + viz[nod] = true; + + + + for (__typeof((A[nod]).begin()) it = (A[nod]).begin(); it != (A[nod]).end(); ++it) + + if(!viz[*it]) + + DFP(*it); + + S[++S[0]] = nod; + +} + + + +inline void DFM(int nod) + +{ + + viz[nod] = true; + + Sol[ ( (nod) <= N ? (nod) + N : (nod) - N ) ] = true; + + + + for (__typeof((B[nod]).begin()) it = (B[nod]).begin(); it != (B[nod]).end(); ++it) + + if(!viz[*it]) + + DFM(*it); + +} + + + +inline void solve() + +{ + + for(int (i)=(1);(i)<=(N + N);++(i)) + + if(!viz[i]) + + DFP(i); + + + + viz.reset(); + + + + for(int i = N + N;i >= 1;--i) + + if( !viz[ S[i] ] && !viz[ ( (S[i]) <= N ? (S[i]) + N : (S[i]) - N ) ] ) + + DFM(S[i]); + + + + S[0] = 0; + + for(int (i)=(1);(i)<=(N);++(i)) + + if(Sol[i] == true) + + S[++S[0]] = i; + + + + printf("%d\n",S[0]); + + for(int (i)=(1);(i)<=(S[0]);++(i)) + + printf("%d\n",S[i]); + +} + + + +int main() + +{ + + scan(); + + solve(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/7c258377-02b2-11ec-a4ba-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7c258377-02b2-11ec-a4ba-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7364aafde431f123f6450040b481bdf4b3e0f347 --- /dev/null +++ b/party/2sat_implication_reduction/7c258377-02b2-11ec-a4ba-2cf05d84bfc2.cpp @@ -0,0 +1,269 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[]="party.in"; + +const char oname[]="party.out"; + +const int maxn=200005; + + + +ifstream f(iname); + +ofstream g(oname); + + + +vector E[maxn],S,aux; + +vector > CTC; + + + +queue Q; + + + +int n,i,j,m,lowlink[maxn],burst[maxn],burstt,value[maxn]; + +int x,y,inS[maxn],node,scc[maxn],inG[maxn],type,k; + + + +int id(const int& x),deny(const int& y); + +void tarjan(int x),build_grades(); + + + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;++i) + + { + + f>>x>>y>>type; + + switch(type) + + { + + case 0:E[deny(y)].push_back(id(x)),E[deny(x)].push_back(id(y));break; + + case 1:E[deny(x)].push_back(deny(y)),E[id(y)].push_back(id(x));break; + + case 2:E[deny(y)].push_back(deny(x)),E[id(x)].push_back(id(y));break; + + case 3:E[id(x)].push_back(deny(y)),E[id(y)].push_back(deny(x));break; + + } + + + + } + + for(i=n<<1;i;--i) + + if(burst[i]==0) + + tarjan(i); + + + + for(i=1;i<=n;++i) + + if(scc[i]==scc[i+n]) + + { + + g<<-1<<"\n"; + + f.close(); + + g.close(); + + + + return 0; + + } + + + + build_grades(); + + for(i=0;i::iterator it=CTC[x].begin();it!=CTC[x].end();++it) + + for(vector::iterator jt=E[*it].begin();jt!=E[*it].end();++jt) + + if(scc[*it]!=scc[*jt]) + + { + + --inG[scc[*jt]]; + + if(inG[scc[*jt]]==0) + + Q.push(scc[*jt]); + + } + + + + for(vector::iterator it=CTC[x].begin();it!=CTC[x].end();++it) + + if(value[*it]==-1) + + if(*it>n) + + value[*it]=0,value[*it-n]=1; + + else + + value[*it]=0,value[*it+n]=1; + + } + + for(i=1;i<=n;++i) + + k+=value[i]; + + g<::iterator it=E[i].begin();it!=E[i].end();++it) + + if(scc[*it]!=scc[i]) + + ++inG[scc[*it]]; + +} + + + +void tarjan(int x) + +{ + + lowlink[x]=burst[x]=++burstt; + + S.push_back(x);inS[x]=1; + + for(vector::iterator it=E[x].begin();it!=E[x].end();++it) + + if(burst[*it]==0) + + tarjan(*it), + + lowlink[x]=min(lowlink[x],lowlink[*it]); + + else + + if(inS[*it]) + + lowlink[x]=min(lowlink[x],lowlink[*it]); + + if(lowlink[x]==burst[x]) + + { + + aux.clear(); + + do + + { + + aux.push_back(node=S.back()); + + S.pop_back();inS[node]=0;scc[node]=CTC.size(); + + } while(node!=x); + + CTC.push_back(aux); + + } + +} + + + +int deny(const int& x) + +{ + + if(x<0) + + return -x; + + if(x<=n) + + return x+n; + + return x-n; + +} + +int id(const int& x) + +{ + + if(x<0) + + return n-x; + + return x; + +} diff --git a/party/2sat_implication_reduction/7c883cf8-02b2-11ec-bbc6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7c883cf8-02b2-11ec-bbc6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..410eeb8d19808432a47520dd91cd78ceb9ad5dd7 --- /dev/null +++ b/party/2sat_implication_reduction/7c883cf8-02b2-11ec-bbc6-2cf05d84bfc2.cpp @@ -0,0 +1,298 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + + + +typedef set::iterator siter; + +typedef vector::iterator viter; + + + +vector g[210]; + +set cg[210]; + +vector > cc; + +int idx[210],llink[210],comp[210],ts[210],tidx=1,n,m,nc=0,tnc; + +bitset<210> viz,inStack,val; + +stack stk; + + + +inline int non(int x){ + + return (x>n)?x-n:x+n; + +} + + + +void tarjan(int nod){ + + idx[nod]=llink[nod]=tidx++; + + stk.push(nod),inStack[nod]=true; + + + + for(viter it=g[nod].begin();it!=g[nod].end();++it){ + + if(!idx[*it]){ + + tarjan(*it); + + llink[nod]=min(llink[*it],llink[nod]); + + }else if(inStack[*it]){ + + llink[nod]=min(llink[*it],llink[nod]); + + } + + } + + + + if(llink[nod]==idx[nod]){ + + vector con; + + int nd; + + do{ + + con.push_back(nd = stk.top()); + + stk.pop(),inStack[nd]=false; + + comp[nd]=nc; + + }while(nd!=nod); + + nc++; + + cc.push_back(con); + + } + +} + + + +void build(){ + + for(int i=0;i sol; + + for(int i=1;i<=n;i++){ + + if(val[i]){ + + sol.push_back(i); + + } + + } + + fprintf(fout,"%d\n",sol.size()); + + for(viter it=sol.begin();it!=sol.end();++it){ + + fprintf(fout,"%d\n",*it); + + } + + }else{ + + fprintf(fout,"0\n"); + + } + + fclose(fin); + + fclose(fout); + +} diff --git a/party/2sat_implication_reduction/7ca89629-02b2-11ec-af22-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/7ca89629-02b2-11ec-af22-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caf0402cebc25f5f55a89b11f8ba29bde94e40ed --- /dev/null +++ b/party/2sat_implication_reduction/7ca89629-02b2-11ec-af22-2cf05d84bfc2.cpp @@ -0,0 +1,188 @@ +#include #include +#include +#include +using namespace std; + + + +const int MAX_N = 200007; + +vector G[MAX_N], Gt[MAX_N], L[MAX_N]; + +int C[MAX_N], P, N, M, viz[MAX_N], d[MAX_N], sol[MAX_N]; + +vector A[MAX_N], pl; + +queueQ; + +inline int non(int x) { return (x > N)?x-N:x+N; } + +void DFP(int x) + +{ + + if(viz[x]) return; + + viz[x] = 1; + + int i; + + for(i = 0; i < (int)G[x].size(); ++i) + + DFP(G[x][i]); + + pl.push_back(x); + +} + +void DFM(int x) + +{ + + if(viz[x]) return; + + int i; + + C[x] = P; L[P].push_back(x); viz[x] = 1; + + for(i = 0; i < Gt[x].size(); ++i) + + DFM(Gt[x][i]); + +} + +int main() + +{ + + ifstream in("party.in"); ofstream out("party.out"); + + in>>N>>M; + + int x, y, i, j, op, aux; + + for(i = 1; i <= M; ++i) + + { + + in>>x>>y>>op; + + if(op == 1) y = -y; + + else if (op == 2) { x = -x; } + + else if(op == 3) {x = -x, y = -y; } + + if(x < 0 ) x = -x + N; + + if(y < 0 ) y = -y + N; + + G[non(x)].push_back(y); Gt[y].push_back(non(x)); + + G[non(y)].push_back(x); Gt[x].push_back(non(y)); + + } + + for(i=1; i <= 2*N; ++i) + + if(!viz[i]) + + DFP(i); + + memset(viz, 0, sizeof(viz)); + + for(i = pl.size() - 1; i >= 0; --i) + + if( !viz[ pl[i] ] ) + + { + + P++; + + DFM(pl[i]); + + } + + int ok = 1; + + for(i = 1; i <= N; ++i) + + if( C[i] == C[ non(i) ] ) ok = 0; + + if(ok == 0) { out<<"-1\n"; return 0; } + + + + for(x = 1; x <= 2*N; ++x) + + for(j = 0; j < (int)G[x].size(); ++j) + + { + + y = G[x][j]; + + if( C[x] != C[y] ) + + { + + A[ C[x]].push_back(C[y]); + + d[C[y]]++; + + } + + } + + for(i = 1; i <= P; ++i) + + if(d[i] == 0) Q.push(i); + + for(;!Q.empty();Q.pop()) + + { + + x = Q.front(); + + for(i = 0; i < L[x].size(); ++i) + + { + + y = L[x][i]; + + if(sol[y]) continue; + + sol[y] = 1; + + sol[non(y)] = 2; + + } + + for(i = 0; i < A[x].size(); ++i) + + { + + y = A[x][i]; + + d[y]--; + + if(d[y] == 0) Q.push(y); + + } + + } + + int cnt = 0; + + for(i = 1; i <= N; ++i) + + if(sol[i] == 2) ++cnt; + + out< +#include +#include +#include + +using namespace std; +ifstream fi("party.in"); +ofstream fo("party.out"); +int N,M; +vector G[2][210]; +vector GComp[210]; +int WhatComp[210],used[210],NComps,usedComp[210],assoc[210],val[210]; +stack S; +vector > Comps; +vector sol; + + + + + +void make_graph(int x, int y){ + G[0][N-x+1].push_back(y); + G[1][y].push_back(N-x+1); + G[0][N-y+1].push_back(x); + G[1][x].push_back(N-y+1); +} + +void afis(int i){ + if ((i*2)<=N) fo<>N>>M; + N*=2; + for (int i=1;i<=M;++i){ + fi>>x>>y>>z; + if (z==0) make_graph(x,y); else + if (z==1) make_graph(x,N-y+1); else + if (z==2) make_graph(N-x+1,y); else + make_graph(N-x+1,N-y+1); + } + fi.close(); +} + + + +void DFSPlus(int nod,vector* G){ + used[nod]=1; + vector::iterator it; + for (it=G[nod].begin();it!=G[nod].end();++it) + if (!used[*it]) DFSPlus(*it,G); + S.push(nod); +} + + + +void DFSMinus(int nod,vector* G,int componenta){ + WhatComp[nod]=componenta; + vector::iterator it; + for (it=G[nod].begin();it!=G[nod].end();++it) + if (!WhatComp[*it]) DFSMinus(*it,G,componenta); +} + + + +void topologica(int nod,vector* G){ + used[nod]=1; + vector::iterator it; + for (it=G[nod].begin();it!=G[nod].end();++it) + if (!used[*it]) topologica(*it,G); + S.push(nod); +} + +void get_output(){ + fo<::iterator it=sol.begin();it!=sol.end();++it) fo<<(*it)<<"\n"; + fo.close(); +} + + +int main(){ + get_input(); + + + memset(used,0,sizeof(used)); + memset(WhatComp,0,sizeof(WhatComp)); + for (int i=1;i<=N;++i) if (!used[i]) DFSPlus(i,G[0]); + + NComps=0; + while (!S.empty()){ + if (!WhatComp[S.top()]){ ++NComps; DFSMinus(S.top(),G[1],NComps); } + S.pop(); + } + Comps.resize(NComps+1); + for (int i=1;i<=N;++i) Comps[WhatComp[i]].push_back(i); + + + for (int i=1;i<=N;++i){ + memset(usedComp,0,sizeof(usedComp)); + for (vector::iterator it=G[0][i].begin();it!=G[0][i].end();++it) + if (WhatComp[i]!=WhatComp[*it] && !usedComp[WhatComp[*it]]){ + GComp[WhatComp[i]].push_back(WhatComp[*it]); + usedComp[WhatComp[*it]]=1; + } + } + for (int i=1;i<=N;++i) assoc[WhatComp[i]]=WhatComp[N-i+1]; + + + memset(used,0,sizeof(used)); + for (int i=1;i<=NComps;++i) + if (!used[i]) topologica(i,GComp); + + + memset(used,0,sizeof(used)); + memset(val,0,sizeof(val)); + while (!S.empty()){ + if (!used[S.top()]){ + for (vector::iterator it=Comps[S.top()].begin();it!=Comps[S.top()].end();++it) { val[*it]=0;val[N-(*it)+1]=1; } + used[S.top()]=1; + used[assoc[S.top()]]=1; + } + S.pop(); + } + + for (int i=1;(i*2)<=N;++i) + if (val[i]) sol.push_back(i); + + get_output(); + + return 0; +} diff --git a/party/2sat_implication_reduction/807b0d11-02b2-11ec-893e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/807b0d11-02b2-11ec-893e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec2094194f7d8b069e72238a70aa7feec044958d --- /dev/null +++ b/party/2sat_implication_reduction/807b0d11-02b2-11ec-893e-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include +#include #include +#include +using namespace std; + + + + + +char Viz[256]; + +int St[256],A[256],n,k; + +vector G[256],Gt[256]; + + + +void read() + +{ + + int m,x,y,z; + + + + freopen("party.in","r",stdin); + + scanf("%d%d",&n,&m); + + while(m--) + + { + + scanf("%d%d%d",&x,&y,&z); + + x+=n*((z&2)!=0); y+=n*(z&1); + + G[((x)>n?(x)-n:(x)+n)].push_back(y); + + Gt[y].push_back(((x)>n?(x)-n:(x)+n)); + + G[((y)>n?(y)-n:(y)+n)].push_back(x); + + Gt[x].push_back(((y)>n?(y)-n:(y)+n)); + + } + +} + + + +void DFS(int x) + +{ + + vector::iterator it; + + + + Viz[x]=1; + + for(it=G[x].begin();it!=G[x].end();++it) + + if(!Viz[*it]) + + DFS(*it); + + St[++k]=x; + +} + + + +void DFST(int x) + +{ + + vector::iterator it; + + + + Viz[x]=1; A[k++]=x; + + for(it=Gt[x].begin();it!=Gt[x].end();++it) + + if(!Viz[*it]) + + DFST(*it); + +} + + + +void solve() + +{ + + int i,j; + + + + for(i=1;i<=n<<1;++i) + + if(!Viz[i]) + + DFS(i); + + + + memset(Viz,0,256*sizeof(char)); + + for(i=k;i;--i) + + if(!Viz[St[i]]) + + { + + k=0; + + DFST(St[i]); + + for(j=0;jn?(A[j])-n:(A[j])+n)]=2; + + } + + } + + + + for(k=0,i=1;i<=n;++i) + + if(Viz[i]==2) + + A[k++]=i; + +} + + + +void write() + +{ + + int i; + + + + freopen("party.out","w",stdout); + + printf("%d\n",k); + + for(i=0;i +#include +#include +using namespace std; + + + + + +int X[1000],Y[1000],Z[1000],XX[1000],YY[1000],ZZ[1000],V[101],A[101],n,m,mm,k; + +vector C[101]; + + + +void read() + +{ + + int i; + + + + freopen("party.in","r",stdin); + + scanf("%d%d",&n,&m); + + for(i=0;i::iterator it; + + + + V[x]=v; + + for(it=C[x].begin();it!=C[x].end();++it) + + { + + if(V[X[*it]] && V[Y[*it]]) + + if(V[X[*it]]-1^((Z[*it]&2)!=0) || V[Y[*it]]-1^((Z[*it]&1)!=0)) + + continue; + + else + + { + + V[x]=0; + + return 0; + + } + + else + + { + + if(X[*it]==x && !(V[x]-1^((Z[*it]&2)!=0))) + + if(!set(Y[*it],(1^((Z[*it]&1)!=0))+1)) + + { + + V[x]=0; + + return 0; + + } + + if(Y[*it]==x && !(V[x]-1^((Z[*it]&1)!=0))) + + if(!set(X[*it],(1^((Z[*it]&2)!=0))+1)) + + { + + V[x]=0; + + return 0; + + } + + } + + } + + return 1; + +} + + + +void solve() + +{ + + int i; + + + + while(m) + + { + + if(!set(X[0],1)) + + set(X[0],2); + + for(i=1;i<=n;++i) + + C[i].clear(); + + for(mm=i=0;i +#include + + +using namespace std; + + + +vector v[210],vt[210]; + +int q[210],n,nr; + +char vaz[210],sol[210]; + + + +int neg(int x) + +{ + + if(x<=n) return x+n; + + else return x-n; + +} + +void dfs_dir(int nod) + +{ + + vaz[nod]=1; + + for(vector::iterator it=v[nod].begin();it!=v[nod].end();it++) + + if(!vaz[*it]) dfs_dir(*it); + + q[++nr]=nod; + +} + +void add_edge(int x,int y) + +{ + + v[x].push_back(y); + + vt[y].push_back(x); + +} + +void dfs_indir(int nod) + +{ + + vaz[nod]=sol[nod]=0; + + sol[neg(nod)]=1; + + for(vector::iterator it=vt[nod].begin();it!=vt[nod].end();it++) + + if(vaz[*it]) dfs_indir(*it); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int m,x,y,a; + + scanf("%d%d",&n,&m); + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d%d",&x,&y,&a); + + if(a==0) + + { + + add_edge(neg(x),y); + + add_edge(neg(y),x); + + } + + else if(a==1) + + { + + add_edge(neg(x),neg(y)); + + add_edge(y,x); + + } + + else if(a==2) + + { + + add_edge(x,y); + + add_edge(neg(y),neg(x)); + + } + + else + + { + + add_edge(x,neg(y)); + + add_edge(y,neg(x)); + + } + + } + + for(int i=1;i<=2*n;i++) if(!vaz[i]) dfs_dir(i); + + for(int i=nr;i;i--) if(vaz[q[i]] && vaz[neg(q[i])]) dfs_indir(q[i]); + + nr=0; + + for(int i=1;i<=n;i++) if(sol[i]) nr++; + + printf("%d\n",nr); + + for(int i=1;i<=n;i++) if(sol[i]) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/82a92e01-02b2-11ec-9f09-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/82a92e01-02b2-11ec-9f09-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c8d8c21ab4988da779ef5520b0ad6725f6a153f --- /dev/null +++ b/party/2sat_implication_reduction/82a92e01-02b2-11ec-9f09-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +#include + + + + + +using namespace std; + + + + + +int N,M; +vector L[2*105]; +vector T[2*105]; +short int timp[2*105],order[2*105]; +int next_time; +short int CTC[2*105],cnt_ctc=0; + +vector LCTC[2*105]; +short int timeCTC[2*105]; +vector sol; + +inline short int opus(short int x) +{ + if(x<=N) + return x+N; + else + return x-N; +} + +inline void add(short int a, short int b) +{ + L[opus(a)].push_back(b); T[b].push_back(opus(a)); + L[opus(b)].push_back(a); T[a].push_back(opus(b)); +} + +void read_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i::iterator i=L[vertex].begin(); i!=L[vertex].end(); ++i) + if(timp[*i]==-1) + { + timp[*i]=0; + df1(*i); + ++next_time; + } + timp[vertex]=next_time; +} + +void df2(int vertex) +{ + for(vector ::iterator i=T[vertex].begin(); i!=T[vertex].end(); ++i) + if(!CTC[*i]) + { + CTC[*i]=cnt_ctc; + df2(*i); + } +} + +void df3(int vertex) +{ + for(vector ::iterator i=LCTC[vertex].begin(); i!=LCTC[vertex].end(); ++i) + if(timeCTC[*i]==-1) + { + timeCTC[*i]=0; + df3(*i); + ++next_time; + } + timeCTC[vertex]=next_time; +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read_data(); + + int i; + next_time=1; + for(i=1;i<=2*N;i++) + timp[i]=-1; + for(i=1;i<=2*N;i++) + if(timp[i]==-1) + { + timp[i]=0; + df1(i); + ++next_time; + } + for(i=1;i<=2*N;i++) + { + order[2*N-timp[i]+1]=i; + CTC[i]=0; + } + for(i=1;i<=2*N;i++) + if(!CTC[order[i]]) + { + CTC[order[i]]=++cnt_ctc; + df2(order[i]); + } + + for(i=1;i<=2*N;i++) + for(vector ::iterator it=L[i].begin(); it!=L[i].end(); ++it) + if(CTC[i]!=CTC[*it]) + LCTC[CTC[i]].push_back(CTC[*it]); + next_time=1; + for(i=1;i<=cnt_ctc;i++) + timeCTC[i]=-1; + for(i=1;i<=cnt_ctc;i++) + if(timeCTC[i]==-1) + { + timeCTC[i]=0; + df3(i); + ++next_time; + } + + for(i=1;i<=N;i++) + if(timeCTC[CTC[i]]::iterator it=sol.begin(); it!=sol.end(); ++it) + printf("%d\n",(int)(*it)); + + return 0; +} diff --git a/party/2sat_implication_reduction/82c73d54-02b2-11ec-a055-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/82c73d54-02b2-11ec-a055-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35e6eefcf3aeb57645cc4498adcdc7d8c2b337b9 --- /dev/null +++ b/party/2sat_implication_reduction/82c73d54-02b2-11ec-a055-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include + + + + + +using namespace std; + + + + + +int N,M; +vector L[2*105]; +vector T[2*105]; +short int timp[2*105],order[2*105]; +int next_time; +short int CTC[2*105],cnt_ctc=0; + +vector LCTC[2*105]; +short int timeCTC[2*105]; +vector sol; + +inline short int opus(short int x) +{ + if(x<=N) + return x+N; + else + return x-N; +} + +inline void add(short int a, short int b) +{ + L[opus(a)].push_back(b); T[b].push_back(opus(a)); + L[opus(b)].push_back(a); T[a].push_back(opus(b)); +} + +void read_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i::iterator i=L[vertex].begin(); i!=L[vertex].end(); ++i) + if(timp[*i]==-1) + { + timp[*i]=0; + df1(*i); + ++next_time; + } + timp[vertex]=next_time; +} + +void df2(int vertex) +{ + for(vector ::iterator i=T[vertex].begin(); i!=T[vertex].end(); ++i) + if(!CTC[*i]) + { + CTC[*i]=cnt_ctc; + df2(*i); + } +} + +void df3(int vertex) +{ + for(vector ::iterator i=LCTC[vertex].begin(); i!=LCTC[vertex].end(); ++i) + if(timeCTC[*i]==-1) + { + timeCTC[*i]=0; + df3(*i); + ++next_time; + } + timeCTC[vertex]=next_time; +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read_data(); + + int i; + next_time=1; + for(i=1;i<=2*N;i++) + timp[i]=-1; + for(i=1;i<=2*N;i++) + if(timp[i]==-1) + { + timp[i]=0; + df1(i); + ++next_time; + } + for(i=1;i<=2*N;i++) + { + order[2*N-timp[i]+1]=i; + CTC[i]=0; + } + for(i=1;i<=2*N;i++) + if(!CTC[order[i]]) + { + CTC[order[i]]=++cnt_ctc; + df2(order[i]); + } + + for(i=1;i<=2*N;i++) + for(vector ::iterator it=L[i].begin(); it!=L[i].end(); ++it) + if(CTC[i]!=CTC[*it]) + LCTC[CTC[i]].push_back(CTC[*it]); + next_time=1; + for(i=1;i<=cnt_ctc;i++) + timeCTC[i]=-1; + for(i=1;i<=cnt_ctc;i++) + if(timeCTC[i]==-1) + { + timeCTC[i]=0; + df3(i); + ++next_time; + } + + for(i=1;i<=N;i++) + if(timeCTC[CTC[i]]::iterator it=sol.begin(); it!=sol.end(); ++it) + printf("\n%d",(int)(*it)); + + + return 0; +} diff --git a/party/2sat_implication_reduction/830de344-02b2-11ec-910b-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/830de344-02b2-11ec-910b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c8d8c21ab4988da779ef5520b0ad6725f6a153f --- /dev/null +++ b/party/2sat_implication_reduction/830de344-02b2-11ec-910b-2cf05d84bfc2.cpp @@ -0,0 +1,148 @@ +#include +#include + + + + + +using namespace std; + + + + + +int N,M; +vector L[2*105]; +vector T[2*105]; +short int timp[2*105],order[2*105]; +int next_time; +short int CTC[2*105],cnt_ctc=0; + +vector LCTC[2*105]; +short int timeCTC[2*105]; +vector sol; + +inline short int opus(short int x) +{ + if(x<=N) + return x+N; + else + return x-N; +} + +inline void add(short int a, short int b) +{ + L[opus(a)].push_back(b); T[b].push_back(opus(a)); + L[opus(b)].push_back(a); T[a].push_back(opus(b)); +} + +void read_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i::iterator i=L[vertex].begin(); i!=L[vertex].end(); ++i) + if(timp[*i]==-1) + { + timp[*i]=0; + df1(*i); + ++next_time; + } + timp[vertex]=next_time; +} + +void df2(int vertex) +{ + for(vector ::iterator i=T[vertex].begin(); i!=T[vertex].end(); ++i) + if(!CTC[*i]) + { + CTC[*i]=cnt_ctc; + df2(*i); + } +} + +void df3(int vertex) +{ + for(vector ::iterator i=LCTC[vertex].begin(); i!=LCTC[vertex].end(); ++i) + if(timeCTC[*i]==-1) + { + timeCTC[*i]=0; + df3(*i); + ++next_time; + } + timeCTC[vertex]=next_time; +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read_data(); + + int i; + next_time=1; + for(i=1;i<=2*N;i++) + timp[i]=-1; + for(i=1;i<=2*N;i++) + if(timp[i]==-1) + { + timp[i]=0; + df1(i); + ++next_time; + } + for(i=1;i<=2*N;i++) + { + order[2*N-timp[i]+1]=i; + CTC[i]=0; + } + for(i=1;i<=2*N;i++) + if(!CTC[order[i]]) + { + CTC[order[i]]=++cnt_ctc; + df2(order[i]); + } + + for(i=1;i<=2*N;i++) + for(vector ::iterator it=L[i].begin(); it!=L[i].end(); ++it) + if(CTC[i]!=CTC[*it]) + LCTC[CTC[i]].push_back(CTC[*it]); + next_time=1; + for(i=1;i<=cnt_ctc;i++) + timeCTC[i]=-1; + for(i=1;i<=cnt_ctc;i++) + if(timeCTC[i]==-1) + { + timeCTC[i]=0; + df3(i); + ++next_time; + } + + for(i=1;i<=N;i++) + if(timeCTC[CTC[i]]::iterator it=sol.begin(); it!=sol.end(); ++it) + printf("%d\n",(int)(*it)); + + return 0; +} diff --git a/party/2sat_implication_reduction/837111e4-02b2-11ec-8c3c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/837111e4-02b2-11ec-8c3c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c59daec1d24187dfc91068989bbb3e6e5c6529 --- /dev/null +++ b/party/2sat_implication_reduction/837111e4-02b2-11ec-8c3c-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include + + + + + +using namespace std; + + + + + +int N,M; +vector L[2*105]; +vector T[2*105]; +short int timp[2*105],order[2*105]; +int next_time; +short int CTC[2*105],cnt_ctc=0; + +vector LCTC[2*105]; +short int timeCTC[2*105]; +vector sol; + +inline short int opus(short int x) +{ + if(x<=N) + return x+N; + else + return x-N; +} + +inline void add(short int a, short int b) +{ + L[opus(a)].push_back(b); T[b].push_back(opus(a)); + L[opus(b)].push_back(a); T[a].push_back(opus(b)); +} + +void read_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i::iterator i=L[vertex].begin(); i!=L[vertex].end(); ++i) + if(timp[*i]==-1) + { + timp[*i]=0; + df1(*i); + ++next_time; + } + timp[vertex]=next_time; +} + +void df2(int vertex) +{ + for(vector ::iterator i=T[vertex].begin(); i!=T[vertex].end(); ++i) + if(!CTC[*i]) + { + CTC[*i]=cnt_ctc; + df2(*i); + } +} + +void df3(int vertex) +{ + for(vector ::iterator i=LCTC[vertex].begin(); i!=LCTC[vertex].end(); ++i) + if(timeCTC[*i]==-1) + { + timeCTC[*i]=0; + df3(*i); + ++next_time; + } + timeCTC[vertex]=next_time; +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read_data(); + + int i; + next_time=1; + for(i=1;i<=2*N;i++) + timp[i]=-1; + for(i=1;i<=2*N;i++) + if(timp[i]==-1) + { + timp[i]=0; + df1(i); + ++next_time; + } + for(i=1;i<=2*N;i++) + { + order[2*N-timp[i]+1]=i; + CTC[i]=0; + } + for(i=1;i<=2*N;i++) + if(!CTC[order[i]]) + { + CTC[order[i]]=++cnt_ctc; + df2(order[i]); + } + + for(i=1;i<=2*N;i++) + for(vector ::iterator it=L[i].begin(); it!=L[i].end(); ++it) + if(CTC[i]!=CTC[*it]) + LCTC[CTC[i]].push_back(CTC[*it]); + next_time=1; + for(i=1;i<=cnt_ctc;i++) + timeCTC[i]=-1; + for(i=1;i<=cnt_ctc;i++) + if(timeCTC[i]==-1) + { + timeCTC[i]=0; + df3(i); + ++next_time; + } + + for(i=1;i<=N;i++) + if(timeCTC[CTC[i]]::iterator it=sol.begin(); it!=sol.end(); ++it) + printf("%d\n",(int)(*it)); + printf("23"); + + return 0; +} diff --git a/party/2sat_implication_reduction/850d433e-02b2-11ec-983b-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/850d433e-02b2-11ec-983b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8be636c51d1d69eb97945cf66f1ad147006b5778 --- /dev/null +++ b/party/2sat_implication_reduction/850d433e-02b2-11ec-983b-2cf05d84bfc2.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +vector < vector < int > > lv,lv2; + +vector noduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + + + +int neg(int x) + +{ + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + + + +void citire() + +{ + + int a,b,t; + + scanf("%d%d\n",&n,&m); + + lv.resize(2*n+1); + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/852c61e8-0ae8-497b-bf43-36fc7bd7d4b9.cpp b/party/2sat_implication_reduction/852c61e8-0ae8-497b-bf43-36fc7bd7d4b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/852c61e8-0ae8-497b-bf43-36fc7bd7d4b9.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/852c63fe-02b2-11ec-a7c7-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/852c63fe-02b2-11ec-a7c7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50d30fc4771d5dce163e5f18f246e35c86b49cbf --- /dev/null +++ b/party/2sat_implication_reduction/852c63fe-02b2-11ec-a7c7-2cf05d84bfc2.cpp @@ -0,0 +1,269 @@ +#include +#include + + + + + + +using namespace std; + + + +int n,m,x,y,o,i,j,timp,ma[2*105][2*105],a[2*105][2*105],viz[2*105]; + +int pot[2*105][2*105],bun[2*105][2*105],cate,scc[2*105][2*105],st[2*105]; + +int val[2*105],ma1[2*105][2*105]; + +int stiv[2*105]; + + + +int negat(int x) { + + if(x>n) return x-n; + + else return x+n; + +} + + + +void bfs(int nod) { + + for(int j=1;j<=2*105;j++) viz[j]=0; + + int q=1,p=1; + + stiv[1]=nod; + + viz[1]=1; + + while(q<=p) { + + for(int i=1;i<=a[stiv[q]][0];i++) { + + int n1=a[stiv[q]][i]; + + if(!viz[n1]) { + + pot[nod][++pot[nod][0]]=n1; + + bun[nod][n1]=1; + + stiv[++p]=n1; + + viz[n1]=1; + + } + + } + + q++; + + } + +} + + + +void ca_viz(int nod) { + + viz[nod]=1; + + for(int i=1;i<=ma1[nod][0];i++) { + + if(!viz[ma1[nod][i]]) ca_viz(ma1[nod][i]); + + } + + st[++st[0]]=nod; + +} + + + +void sortare_topologica() { + + for(int i=1;i<=cate;i++) viz[i]=0; + + for(int i=1;i<=cate;i++) + + if(!viz[i]) ca_viz(i); + +} + + + +int main() { + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d\n",&n,&m); + + for(int i=1;i<=m;i++) { + + scanf("%d %d %d\n",&x,&y,&o); + + if(o==0) { + + ma[negat(x)][y]=1; + + ma[negat(y)][x]=1; + + a[negat(x)][++a[negat(x)][0]]=y; + + a[negat(y)][++a[negat(y)][0]]=x; + + } + + if(o==1) { + + ma[negat(x)][negat(y)]=1; + + ma[y][x]=1; + + a[negat(x)][++a[negat(x)][0]]=negat(y); + + a[y][++a[y][0]]=x; + + } + + if(o==2) { + + ma[x][y]=1; + + ma[negat(y)][negat(x)]=1; + + a[negat(y)][++a[negat(y)][0]]=negat(x); + + a[x][++a[x][0]]=y; + + } + + if(o==3) { + + ma[x][negat(y)]=1; + + ma[y][negat(x)]=1; + + a[x][++a[x][0]]=negat(y); + + a[y][++a[y][0]]=negat(x); + + } + + } + + for(int i=1;i<=2*n;i++) bfs(i); + + + + cate=0; + + for(int i=1;i<=2*n;i++) { + + int gata=0; + + for(int j=1;j<=cate;j++) + + if((bun[scc[j][1]][i]) && (bun[i][scc[j][1]])) { + + scc[j][++scc[j][0]]=i; + + gata=1; + + break; + + } + + if(!gata) { + + cate++; + + scc[cate][++scc[cate][0]]=i; + + } + + } + + for(int i=1;i<=cate;i++) + + for(int j=1;j<=cate;j++) + + if(i!=j) { + + int bagam=0; + + for(int k=1;k<=scc[i][0];k++) { + + for(int k1=1;k1<=scc[j][0];k1++) + + if(ma[scc[i][k]][scc[j][k1]]) { + + bagam=1; + + break; + + } + + if(bagam) break; + + } + + if(bagam) + + ma1[i][++ma1[i][0]]=j; + + } + + + + sortare_topologica(); + + + + for(int i=1;i<=st[0];i++) { + + int nod=st[i]; + + for(int j=1;j<=scc[st[i]][0];j++) { + + nod=scc[st[i]][j]; + + if(val[negat(nod)]==0) { + + val[nod]=1; + + val[negat(nod)]=2; + + } + + } + + } + + int cate1=0; + + for(int i=1;i<=n;i++) if(val[i]==1) cate1++; + + if(cate1==0) { + + printf("%d\n",n); + + for(int i=1;i<=n;i++) printf("%d\n",i); + + return 0; + + } + + printf("%d\n",cate1); + + for(int i=1;i<=n;i++) if(val[i]==1) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/85665fcd-5c1b-473a-8573-8e80b21f33e8.cpp b/party/2sat_implication_reduction/85665fcd-5c1b-473a-8573-8e80b21f33e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..696dab1188fb69221ce5924b91a98ceed2769d6c --- /dev/null +++ b/party/2sat_implication_reduction/85665fcd-5c1b-473a-8573-8e80b21f33e8.cpp @@ -0,0 +1,200 @@ +#include +#include +#include + + +const int MAX_N = 1000; + + + +int n; + +std::vector g[1 + 2 * MAX_N]; + +std::vector trans[1 + 2 * MAX_N]; + +std::vector topo; + +std::vector sol; + +bool viz[1 + 2 * MAX_N]; + +bool val[1 + 2 * MAX_N]; + +bool possible = true; + + + +int conj(int x) { + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void make_edge(int u, int v) { + + g[u].push_back(v); + + trans[v].push_back(u); + +} + + + +void topoSort(int u) { + + viz[u] = true; + + for (auto v : g[u]) { + + if (!viz[v]) + + topoSort(v); + + } + + topo.push_back(u); + +} + + + +void markNodes(int u) { + + viz[u] = true; + + if (val[u]) { + + possible = false; + + return; + + } + + val[u] = false; + + val[conj(u)] = true; + + for (auto v : trans[u]) { + + if (!viz[v]) + + markNodes(v); + + } + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + int m; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; i++) { + + int x, y, restriction; + + scanf("%d%d%d", &x, &y, &restriction); + + switch (restriction) { + + case 0: + + make_edge(conj(x), y); + + make_edge(conj(y), x); + + break; + + case 1: + + make_edge(conj(x), conj(y)); + + make_edge(y, x); + + break; + + case 2: + + make_edge(conj(y), conj(x)); + + make_edge(x, y); + + break; + + case 3: + + make_edge(x, conj(y)); + + make_edge(y, conj(x)); + + break; + + } + + } + + for (int i = 1; i <= 2 * n; i++) { + + if (!viz[i]) + + topoSort(i); + + } + + std::reverse(topo.begin(), topo.end()); + + for (int i = 1; i <= 2 * n; i++) + + viz[i] = false; + + for (auto node : topo) { + + if (!viz[node] && !viz[conj(node)]) + + markNodes(node); + + if (!possible) { + + printf("-1\n"); + + return 0; + + } + + } + + for (int i = 1; i <= n; i++) { + + if (val[i]) + + sol.push_back(i); + + } + + printf("%d\n", sol.size()); + + for (auto node : sol) + + printf("%d\n", node); + + printf("\n"); + + return 0; + +} diff --git a/party/2sat_implication_reduction/86bf92a0-54f2-4ee8-b032-d7f9ab0e75f5.cpp b/party/2sat_implication_reduction/86bf92a0-54f2-4ee8-b032-d7f9ab0e75f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f060d8989122d18189f43915d7bd5df2cd0f4eb --- /dev/null +++ b/party/2sat_implication_reduction/86bf92a0-54f2-4ee8-b032-d7f9ab0e75f5.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/89c5700d-25ad-4606-b79d-de14904f32a7.cpp b/party/2sat_implication_reduction/89c5700d-25ad-4606-b79d-de14904f32a7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c41e2b33ef14020aeb62e8cb6a84a57bc8a894 --- /dev/null +++ b/party/2sat_implication_reduction/89c5700d-25ad-4606-b79d-de14904f32a7.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/953964b1-345c-427e-8a10-37664b775ebd.cpp b/party/2sat_implication_reduction/953964b1-345c-427e-8a10-37664b775ebd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/953964b1-345c-427e-8a10-37664b775ebd.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/9655c08e-478d-4fbe-9c8a-1c56e12a5ba5.cpp b/party/2sat_implication_reduction/9655c08e-478d-4fbe-9c8a-1c56e12a5ba5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/9655c08e-478d-4fbe-9c8a-1c56e12a5ba5.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/9671e087-60a2-4f45-aa0c-3552712c1eb8.cpp b/party/2sat_implication_reduction/9671e087-60a2-4f45-aa0c-3552712c1eb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/9671e087-60a2-4f45-aa0c-3552712c1eb8.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/97b29c3b-a7e8-4bde-ab90-55a609fbace6.cpp b/party/2sat_implication_reduction/97b29c3b-a7e8-4bde-ab90-55a609fbace6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/97b29c3b-a7e8-4bde-ab90-55a609fbace6.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/97c9374a-a368-487a-8e8f-e6c74004c313.cpp b/party/2sat_implication_reduction/97c9374a-a368-487a-8e8f-e6c74004c313.cpp new file mode 100644 index 0000000000000000000000000000000000000000..986493fb684c691c9b1efddaae4ea3513b5af6a7 --- /dev/null +++ b/party/2sat_implication_reduction/97c9374a-a368-487a-8e8f-e6c74004c313.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + +vector G[210], T[210]; + +int non ( int x ) +{ + if( x <= n ) return x+n; + return x - n ; +} + +void sortaret ( int nod ) +{ + int N = G[nod].size(),i; + + viz[nod] = 1 ; + + for( i = 0 ; i < N ; i++ ) + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + S[--nn] = nod ; +} + +void dfs ( int nod ) +{ + int N = T[nod].size(),i; + + viz[nod] = 0 ; + + sol[ non(nod) ] = 1 ; + if( nod > n ) invitati++; + + for( i = 0 ; i < N ; i++ ) + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; +} + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for( i = 1 ; i <= m ; i++ ) + { + scanf("%d %d %d",&a,&b,&tip); + + if( tip == 0 ) + { + G[non(a)].push_back(b); + G[non(b)].push_back(a); + T[a].push_back(non(b)); + T[b].push_back(non(a)); + } + else if( tip == 1 ) + { + G[non(a)].push_back(non(b)); + G[b].push_back(a); + + T[non(b)].push_back(non(a)); + T[a].push_back(b); + } + else if( tip == 2 ) + { + G[non(b)].push_back(non(a)); + G[a].push_back(b); + + T[non(a)].push_back(non(b)); + T[b].push_back(a); + } + else + { + G[a].push_back(non(b)); + G[b].push_back(non(a)); + T[non(a)].push_back(b); + T[non(b)].push_back(a); + } + } + + nn = n + n + 1 ; + + for( i = 1 ; i <= n+n ; i++ ) + if( !viz[i]) sortaret(i); + + for( i = 1 ; i <= n+n ; i++ ) + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + dfs( S[i] ) ; + + printf("%d\n",invitati); + + for( i = 1 ; i <= n ; i++ ) + if( sol[i] ) printf("%d\n",i); + + return 0; +} diff --git a/party/2sat_implication_reduction/995b7ad0-10d0-42c9-826a-32219eb0f39f.cpp b/party/2sat_implication_reduction/995b7ad0-10d0-42c9-826a-32219eb0f39f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b67777eeb7340ee4d0f1ee3b35405bf6bf9a88f --- /dev/null +++ b/party/2sat_implication_reduction/995b7ad0-10d0-42c9-826a-32219eb0f39f.cpp @@ -0,0 +1,376 @@ +#include +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/99708838-0b54-11ec-838a-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/99708838-0b54-11ec-838a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8733758b06327ef3d06736da82f525c908f1ed8d --- /dev/null +++ b/party/2sat_implication_reduction/99708838-0b54-11ec-838a-2cf05d84bfc2.cpp @@ -0,0 +1,296 @@ +#include + + +using namespace std; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +const int nmax = 2007; + + + +vector v[2 * nmax]; + +vector v_back[2 * nmax]; + + + +int n; + + + +int negare(int nod) + +{ + + return (nod <= n ? nod + n : nod - n); + +} + + + +vector st; + + + +bitset <2 * nmax> vis; + + + +int componenta[2 * nmax]; + + + +void dfs1(int nod) + +{ + + vis[nod] = true; + + + + for(auto i : v[nod]) + + if(vis[i] == 0) + + dfs1(i); + + + + st.push_back(nod); + +} + + + +vector CTC[2 * nmax]; + + + +int nr; + + + +void dfs2(int nod) + +{ + + componenta[nod] = nr; + + + + CTC[nr].push_back(nod); + + + + for(auto i : v_back[nod]) + + if(componenta[i] == 0) + + dfs2(i); + +} + + + +bool sol[2 * nmax]; + + + +void atrib(int ord) + +{ + + vis[ord] = false; + + + + for(auto i : CTC[ord]) + + { + + sol[i] = 2; + + sol[negare(i)] = 1; + + } + +} + + + +int main() + +{ + + int m; + + in >> n >> m; + + + + while(m--) + + { + + int x, y, op; + + in >> x >> y >> op; + + + + int X = negare(x); + + int Y = negare(y); + + + + if(op == 0) + + { + + v[X].push_back(y); + + v[Y].push_back(x); + + + + v_back[y].push_back(X); + + v_back[x].push_back(Y); + + } + + else + + if(op == 1) + + { + + v[X].push_back(Y); + + v[y].push_back(x); + + + + v_back[Y].push_back(X); + + v_back[x].push_back(y); + + } + + else + + if(op == 2) + + { + + v[x].push_back(y); + + v[Y].push_back(X); + + + + v_back[y].push_back(x); + + v_back[X].push_back(Y); + + } + + else + + { + + v[x].push_back(Y); + + v[y].push_back(X); + + + + v_back[Y].push_back(x); + + v_back[X].push_back(y); + + } + + } + + + + for(int i = 1; i <= 2 * n; i++) + + if(vis[i] == 0) + + dfs1(i); + + + + for(int i = 2 * n; i >= 1; i--) + + if(componenta[st[i - 1]] == 0) + + { + + nr++; + + + + dfs2(st[i - 1]); + + } + + + + for(int i = 2 * n - 1; i >= 0; i--) + + if(vis[componenta[st[i]]] == true) + + { + + int nod = st[i]; + + int inv = negare(nod); + + + + vis[componenta[nod]] = false; + + vis[componenta[inv]] = false; + + + + for(auto j : CTC[componenta[nod]]) sol[j] = false, sol[negare(j)] = true; + + } + + + + int nr = 0; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + nr++; + + + + out << nr << '\n'; + + + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + out << i << '\n'; + +} diff --git a/party/2sat_implication_reduction/9b934e58-0b53-11ec-95b6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9b934e58-0b53-11ec-95b6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6422182d64ba4a082cef335e2974f41271056568 --- /dev/null +++ b/party/2sat_implication_reduction/9b934e58-0b53-11ec-95b6-2cf05d84bfc2.cpp @@ -0,0 +1,174 @@ +#include +#include +#include +#include + + +using namespace std; + + + +typedef vector::iterator vii; + + + +const int MaxN = 205; + + + +vector G[MaxN], GT[MaxN]; + +int N, graph[MaxN]; + +bool fol[MaxN], values[MaxN], solutie; + + + +void df_Trav(int X) { + + fol[X] = X; + + for (vii Y = G[X].begin(); Y != G[X].end(); ++Y) + + if (!fol[*Y]) + + df_Trav(*Y); + + graph[++graph[0]] = X; + +} + +inline int Non(int X) { + + return X > N ? X - N : X + N; + +} + + +void df_Dir(int X) { + + if (values[X]) + + solutie = false; + + values[X] = false, values[Non(X)] = true; + + fol[X] = false; + + for (vii Y = GT[X].begin(); Y != GT[X].end(); ++Y) + + if (fol[*Y]) + + df_Dir(*Y); + +} + + + +void Solve2Sat() { + + solutie = true; + + for (int X = 1; X <= 2 * N; ++X) + + if (!fol[X]) + + df_Trav(X); + + reverse(graph + 1, graph + graph[0] + 1); + + for (int i = 1; i <= graph[0]; ++i) { + + int X = graph[i]; + + if (fol[X] && fol[Non(X)]) + + df_Dir(X); + + } + +} + + + +inline void AddEdge(int X, int Y) { + + G[X].push_back(Y), GT[Y].push_back(X); + +} + + + +inline void Disjunction(int X, int Y) { + + AddEdge(Non(X), Y), AddEdge(Non(Y), X); + +} + + + +void Read() { + + assert(freopen("party.in", "r", stdin)); + + int M; assert(scanf("%d %d", &N, &M) == 2); + + for (; M > 0; --M) { + + int Type, X, Y; assert(scanf("%d %d %d", &X, &Y, &Type) == 3); + + if (Type == 0) + + Disjunction(X, Y); + + if (Type == 1) + + Disjunction(X, Non(Y)); + + if (Type == 2) + + Disjunction(Non(X), Y); + + if (Type == 3) + + Disjunction(Non(X), Non(Y)); + + } + +} + + + +void Print() { + + assert(freopen("party.out", "w", stdout)); + + int NTrue = 0; + + for (int X = 1; X <= N; ++X) + + NTrue += values[X]; + + printf("%d\n", NTrue); + + for (int X = 1; X <= N; ++X) + + if (values[X]) + + printf("%d\n", X); + +} + + + +int main() { + + Read(); + + Solve2Sat(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c506469-0b55-11ec-aa90-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c506469-0b55-11ec-aa90-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd8c990edeac5b460377988d7bf56f2cd4ccc98e --- /dev/null +++ b/party/2sat_implication_reduction/9c506469-0b55-11ec-aa90-2cf05d84bfc2.cpp @@ -0,0 +1,202 @@ +#include +#include +#include +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +vector g[200010]; + +vector gt[200010]; + +int st[200010],nr,sol[200010],tip ; + +int idx[200010]; + +bool viz[200010]; + +int n,m,i,j,x,y,ok; + + + +int negat(int x) + +{ + + if(x>n) return x-n; + + return x+n; + + + +} + +void muchie(int x, int y) + +{ + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + + + for(int i=0; i>n>>m; + + ok=1; + + for(i=1; i<=m; ++i) + + { + + cin>>x>>y>>tip; + + if(!tip) muchie(x+n,y),muchie(y+n,x); + + else if(tip==1) muchie(x+n,y+n),muchie(y,x); + + else if(tip==2) muchie(y+n,x+n),muchie(x,y); + + else muchie(x,y+n),muchie(y,x+n); + + } + + + + for(i=1; i<=2*n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + + + + memset(viz,0,sizeof(viz)); + + for(i=nr; i>=1; --i) + + { + + if(!viz[st[i]] && !viz[negat(st[i])]) + + { + + dfs2(st[i]); + + } + + } + + if(!ok) + + { + + cout<<-1; + + return 0; + + } + + + + nr=0; + + for(i=1; i<=n; ++i) + + if(sol[i]==1) nr++; + + + + cout< +using namespace std; + +typedef long long LL; + +typedef pair pii; +const int MAXN = 110; + +template class jmenvect : public vector { +public: + T& operator[](int n) { + if (n > 0) + return this->at(n); + else + return this->at(-n + (this->size() >> 1)); + } +}; + +class graph { +private: + vector< list > adiac; + vector< list > neg_adiac; +public: + graph(); + graph(int); + int size(); + void newnod(); + void newedge(int, int); + const list& getList(int); + void rev_DFS(int, jmenvect&, jmenvect&); + bool SCC_DFS(int, int, jmenvect&); +}; + +graph::graph() { + adiac.reserve(MAXN); + neg_adiac.reserve(MAXN); +} + +graph::graph(int n) { + adiac.resize(n + 1); + neg_adiac.resize(n + 1); +} + +int graph::size() { + return adiac.size(); +} + +void graph::newnod() { + neg_adiac.resize(size() + 1); + adiac.resize(size() + 1); +} + +void graph::newedge(int n1, int n2) { + if (n1 > 0) + adiac[n1].push_back(n2); + else + neg_adiac[-n1].push_back(n2); +} + +const list& graph::getList(int n) { + if (n > 0) + return adiac[n]; + else + return neg_adiac[-n]; +} + +void graph::rev_DFS(int nod, jmenvect& visited, jmenvect &finished) { + auto L = this->getList(-nod); + visited[nod] = 1; + for (auto i = L.begin(); i != L.end(); ++i) + if (!visited[-(*i)]) + rev_DFS(-(*i), visited, finished); + finished.push_back(nod); +} + +bool graph::SCC_DFS(int curComp, int nod, jmenvect& visited) { + visited[nod] = curComp; + auto L = this->getList(nod); + for (auto i = L.begin(); i != L.end(); ++i) { + if (!visited[*i]) { + if (!SCC_DFS(curComp, *i, visited)) + return false; + } + } + if (visited[-nod]) + return false; + visited[-nod] = -curComp; + return true; +} + +pair, int> korasaju(graph* G) { + int N = G->size() - 1; + jmenvect visisted; + visisted.resize((N << 1) + 1); + jmenvect finished; + finished.reserve((N << 1) + 1); + + for (int i = 1; i <= N; i++) { + if (!visisted[i]) + G->rev_DFS(i, visisted, finished); + if (!visisted[-i]) + G->rev_DFS(-i, visisted, finished); + } + + visisted.assign((N << 1) + 1, 0); + int curComp = 0; + for (auto i = finished.rbegin(); i != finished.rend() - 1; ++i) { + curComp++; + if (!visisted[*i]) { + if (!G->SCC_DFS(curComp, *i, visisted)) { + jmenvect dummyvect; + return{ dummyvect, 0 }; + } + } + else + curComp--; + } + return{ visisted, curComp }; +} + + + +int main() { + + assert(freopen("fis.in", "r", stdin)); + + assert(freopen("fis.out", "w", stdout)); + int N, M; + scanf("%d%d", &N, &M); + graph* G = new graph(N); + for (int i = 0; i < M; i++) { + int tip, a, b; + scanf("%d%d%d", &a, &b, &tip); + switch (tip) { + case 0: + break; + case 1: + b = -b; + break; + case 2: + a = -a; + break; + default: + a = -a; + b = -b; + break; + } + G->newedge(-a, b); + G->newedge(-b, a); + } + pair, int> p = korasaju(G); + jmenvect& SCC = p.first; + if (!SCC.size()) { + printf("-1\n"); + return 0; + } + int nrComp = p.second; + jmenvect cval; + cval.resize((nrComp << 1) + 1); + for (int i = 1; i <= nrComp; i++) { + cval[-i] = 0; + cval[i] = 1; + } + vector sol; + for (int i = 1; i <= N; i++) { + if (cval[SCC[i]] == 1) + sol.push_back(i); + } + printf("%d\n", (int)sol.size()); + + for (const auto &it : sol) + + printf("%d\n", it); + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c508b7e-0b55-11ec-b14f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c508b7e-0b55-11ec-b14f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..700a1ad0017c5e89a1dafad828fbd70191d5de2d --- /dev/null +++ b/party/2sat_implication_reduction/9c508b7e-0b55-11ec-b14f-2cf05d84bfc2.cpp @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream cin("data"".in"); + +ofstream cout("data"".out"); + + + +template + +void print(a v, type t) { + + for_each(v.begin(),v.end(), [](type x) { cout << x << ' '; }); + + cout << '\n'; + +} + + + +struct nod { + + vector v1; + + vector v2; + +}; + + + +int n, m; + +nod v[2 * 207 + 1]; + +stack s; + +bitset<2*207> viz; + +vector > componente; + +int nrP; + +inline void add(const int &ind,const int &next){ + + v[ind].v1.push_back(next); + + v[next].v2.push_back(ind); + +} + +void dfs(int i){ + + viz[i] = 1; + + for(auto it = v[i].v1.begin();it!=v[i].v1.end();++it){ + + if(!viz[*it]) + + dfs(*it); + + } + + s.push(i); + +} + +int nrGr; + +void dfs_inv(int i){ + + viz[i] = 1; + + componente[nrGr].emplace_back(i); + + for(auto it = v[i].v2.begin();it!=v[i].v2.end();++it){ + + if(!viz[*it]) + + dfs_inv(*it); + + } + +} + +inline int negat(int nr){ + + return (nr<=n?nr+n:nr-n); + +} + +int main() { + + ios_base::sync_with_stdio(false); + + clock_t tStart = clock(); + + cin >> n >> m; + + int x1,x2,cod; + + for(int i=0;i> x1 >> x2 >> cod; + + if(!cod){ + + add(negat(x1),x2); + + add(negat(x2),x1); + + } else if(cod == 1){ + + add(negat(x1),negat(x2)); + + add(x2,x1); + + } else if(cod == 2) { + + add(x1,x2); + + add(negat(x2),negat(x1)); + + } else if (cod == 3){ + + add(x1,negat(x2)); + + add(x2,negat(x1)); + + } + + } + + for(int i=1;i<=2*n;++i){ + + if(!viz[i]) + + dfs(i); + + } + + viz.reset(); + + vector temp; + + componente.push_back(temp); + + while(!s.empty()){ + + if(!viz[s.top()]){ + + componente.push_back(temp); + + ++nrGr; + + dfs_inv(s.top()); + + } + + s.pop(); + + } + + + + vector sol(2*207,-1); + + + + for(int i = nrGr;i;--i){ + + if(sol[componente[i][0]] == -1){ + + for(auto it = componente[i].begin();it != componente[i].end(); ++it){ + + sol[*it] = 1; + + sol[negat(*it)] = 0; + } + + } + + } + + + + for(int i=1;i<=n;++i){ + + if(sol[i]<0){ + + sol[i] = 1; + + } + + nrP += sol[i]; + + } + + cout << nrP << '\n'; + + for (int i = 1; i <= n; ++i) { + + if (sol[i]) + + cout << i << '\n'; + + } + + cout.close(); + + printf("\nTime taken: %.2fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); + +} diff --git a/party/2sat_implication_reduction/9c508b7f-0b55-11ec-80fd-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c508b7f-0b55-11ec-80fd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..098b2e6fbbb6592fa4e3c94a7677cedae4b3c31d --- /dev/null +++ b/party/2sat_implication_reduction/9c508b7f-0b55-11ec-80fd-2cf05d84bfc2.cpp @@ -0,0 +1,352 @@ +#include +#include +#include +#include +#include +using namespace std; + +vector g[205]; + +vector gt[205]; + +vector l; + +vector comp[205]; + +int pred[205]; + +vector succ[205]; + +int val[205]; + +bool ans[205]; + +bool viz[205]; + +int f[205]; + +queue q; + +int cont; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(int v : g[nod]) + + if(!viz[v]) + + dfs(v); + + l.push_back(nod); + +} + +void dfst(int nod) + +{ + + viz[nod]=1; + + for(int v : gt[nod]) + + if(!viz[v]) + + dfst(v); + + comp[cont].push_back(nod); + + f[nod]=cont; + +} + +FILE *_fin; + +FILE *_fout; + +int _in_loc; char _in_buff[4096]; + + + + + +void read_init(const char* nume) + +{ + + _fin = fopen(nume, "r"); + + _in_loc = 4095; + +} + + + +char read_ch() + +{ + + ++_in_loc; + + if (_in_loc == 4096) { + + _in_loc = 0; + + fread(_in_buff, 1, 4096, _fin); + + } + + return _in_buff[_in_loc]; + +} + + + +int read_u32() + +{ + + int u32 = 0; char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + int sgn = 1; + + if (c == '-') { + + sgn = -1; + + } else { + + u32 = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + u32 = u32 * 10 + c - '0'; + + } + + return u32 * sgn; + +} + + + +long long read_u64() + +{ + + long long u64 = 0; char c; + + while (!isdigit(c = read_ch()) && c != '-'); + + long long sgn = 1; + + if (c == '-') { + + sgn = -1; + + } else { + + u64 = c - '0'; + + } + + while (isdigit(c = read_ch())) { + + u64 = u64 * 10 + c - '0'; + + } + + return u64 * sgn; + +} + +int main() + +{ read_init("party.in"); + + _fout=fopen("party.out", "w"); + + int n,m,i,x,y,j,nr=0,z; + + n=read_u32(); + + m=read_u32(); + + for(i=1; i<=m; i++){ + + x=read_u32(); + + y=read_u32(); + + z=read_u32(); + + if(z==0){ + + g[2*x+1].push_back(2*y); + + g[2*y+1].push_back(2*x); + + gt[2*y].push_back(2*x+1); + + gt[2*x].push_back(2*y+1); + + } + + else{ + + if(z==1){ + + g[2*x+1].push_back(2*y+1); + + g[2*y].push_back(2*x); + + gt[2*y+1].push_back(2*x+1); + + gt[2*x].push_back(2*y); + + } + + else{ + + if(z==2){ + + g[2*x].push_back(2*y); + + g[2*y+1].push_back(2*x+1); + + gt[2*x+1].push_back(2*y+1); + + gt[2*y].push_back(2*x); + + } + + else{ + + g[2*x].push_back(2*y+1); + + g[2*y].push_back(2*x+1); + + gt[2*y+1].push_back(2*x); + + gt[2*x+1].push_back(2*y); + + } + + } + + } + + } + + for(i=1; i<=2*n+1; i++) + + val[i]=-1; + + for(i=2; i<=2*n+1; i++) + + if(!viz[i]) + + dfs(i); + + reverse(l.begin(), l.end()); + + for(int u : l) + + viz[u]=0; + + for(int u : l){ + + if(!viz[u]){ + + cont++; + + dfst(u); + + } + + } + + for(i=2; i<=2*n+1; i++){ + + if(f[(i/2)*2+(i%2+1)%2]==f[i]){ + + fprintf(_fout, "-1"); + + return 0; + + } + + for(j=0; j +#include +#include + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +const int NMax = 200; + + + +vector G[NMax + 5], GT[NMax + 5], Topo; + +bool Sol[NMax + 5], Use[NMax + 5]; + +int N, M, Nr; + + + +inline int non(int x) + +{ + + return (x > N ? x - N : x + N); + +} + + + +void AddEdge(int x, int y) + +{ + + G[x].push_back(y); + + GT[y].push_back(x); + +} + + + +void DFSP(int Node) + +{ + + Use[Node] = 1; + + + + for(auto Next : G[Node]) + + if(!Use[Next]) + + DFSP(Next); + + + + Topo.push_back(Node); + +} + + + +void DFSM(int Node) + +{ + + Use[Node] = Sol[non(Node)] = 1; + + + + for(auto Next : GT[Node]) + + if(!Use[Next]) + + DFSM(Next); + +} + + + +void Kosaraju() + +{ + + for(int i = 1; i <= 2 * N; i++) + + if(!Use[i]) + + DFSP(i); + + + + for(int i = 1; i <= 2 * N; i++) + + Use[i] = 0; + + + + while(!Topo.empty()) { + + int Node = Topo.back(); Topo.pop_back(); + + + + if(!Use[Node] && !Use[non(Node)]) + + DFSM(Node); + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + + + for(int i = 1, type, x, y; i <= M; i++) + + { + + fin >> x >> y >> type; + + x += (type & 2) / 2 * N; + + y += (type & 1) * N; + + + + AddEdge(non(x), y); + + AddEdge(non(y), x); + + } + + Kosaraju(); + + + + for(int i = 1; i <= N; i++) + + Nr += Sol[i]; + + + + fout << Nr << '\n'; + + + + for(int i = 1; i <= N; i++) + + if(Sol[i]) + + fout << i << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c508b81-0b55-11ec-ba36-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c508b81-0b55-11ec-ba36-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7d0d92106751db098b6c4dbf6c5561732291737 --- /dev/null +++ b/party/2sat_implication_reduction/9c508b81-0b55-11ec-ba36-2cf05d84bfc2.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("party.in"); + +ofstream cout ("party.out"); + + + +int n, m, x, y, type; + + + +bool viz[200005]; + +int val[100005], ind[200005], deg[200005]; + +vector g[200005], ginv[200005]; + +vector comp, st; + +vector > comps; + +queue q; + + + +int indx(int val) { + + if(val < 0) + + return n - val; + + return val; + +} + + + +int non(int val) { + + if(val > n) + + return val - n; + + return val + n; + +} + + + +void dfsp(int nod) { + + viz[nod] = 1; + + for(auto &fiu : g[nod]) { + + if(!viz[fiu]) + + dfsp(fiu); + + } + + st.push_back(nod); + +} + + + +void dfsm(int nod) { + + viz[nod] = 1; + + comp.push_back(nod); + + for(auto &fiu : ginv[nod]) { + + if(!viz[fiu]) + + dfsm(fiu); + + } + +} + + + +int main() { + + cin >> n >> m; + + for(int i = 1; i <= m; i++) { + + cin >> x >> y >> type; + + if(type == 0) { + + g[non(x)].push_back(y); + + g[non(y)].push_back(x); + + ginv[x].push_back(non(y)); + + ginv[y].push_back(non(x)); + + } else if(type == 3) { + + ginv[non(x)].push_back(y); + + ginv[non(y)].push_back(x); + + g[x].push_back(non(y)); + + g[y].push_back(non(x)); + + } else if(type == 1) { + + g[non(x)].push_back(non(y)); + + g[y].push_back(x); + + ginv[x].push_back(y); + + ginv[non(y)].push_back(non(x)); + + } else { + + ginv[non(x)].push_back(non(y)); + + ginv[y].push_back(x); + + g[x].push_back(y); + + g[non(y)].push_back(non(x)); + + } + + } + + for(int i = 1; i <= 2 * n; i++) { + + if(!viz[i]) + + dfsp(i); + + } + + for(int i = 1; i <= 2 * n; i++) + + viz[i] = 0; + + while(!st.empty()) { + + int nod = st.back(); + + st.pop_back(); + + if(!viz[nod]) { + + comp.clear(); + + dfsm(nod); + + for(auto &Nod : comp) + + ind[Nod] = comps.size(); + + comps.push_back(comp); + + } + + } + + for(int i = 1; i <= n; i++) { + + + + if(ind[i] == ind[i + n]) { + + + + cout << -1; + + return 0; + + } + + } + + for(int i = 1; i <= 2 * n; i++) { + + for(auto &fiu : g[i]) { + + if(ind[fiu] != ind[i]) + + deg[ind[fiu]]++; + + } + + } + + for(int i = 1; i <= n; i++) + + val[i] = 2; + + for(int i = 0; i < comps.size(); i++) { + + if(!deg[i]) + + q.push(i); + + } + + while(!q.empty()) { + + int cmp = q.front(); + + q.pop(); + + for(auto &nod : comps[cmp]) { + + int trueNod = (nod > n ? nod - n : nod); + + if(val[trueNod] == 2) + + val[trueNod] = (nod > n); + + } + + for(auto &nod : comps[cmp]) { + + for(auto &fiu : g[nod]) { + + if(ind[nod] != ind[fiu]) { + + deg[ind[fiu]]--; + + if(!deg[ind[fiu]]) + + q.push(ind[fiu]); + + } + + } + + } + + } + + vector guests; + + for(int i = 1; i <= n; i++) { + + if(val[i]) + + guests.push_back(i); + + } + + cout << guests.size() << "\n"; + + for(auto &guest : guests) + + cout << guest << "\n"; + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c508b82-0b55-11ec-8fa1-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c508b82-0b55-11ec-8fa1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e7c2a0598cab9c3455e742a3230ad76f92f5913 --- /dev/null +++ b/party/2sat_implication_reduction/9c508b82-0b55-11ec-8fa1-2cf05d84bfc2.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; + +ifstream fin("party.in"); +ofstream fout("party.out"); + +class TwoSat { + private: + int n; + vector> adG, adT; + + inline int non(int x) { + return (x > n ? x - n : x + n); + } + + void addEdge(int x, int y) { + adG[x].push_back(y); + adT[y].push_back(x); + } + + void dfsG(int node, vector& vis, vector& topo) { + vis[node] = true; + for (int nghb : adG[node]) + if (!vis[nghb]) + dfsG(nghb, vis, topo); + topo.push_back(node); + } + + void dfsT(int node, vector& vis, vector& ans, bool& ok) { + vis[node] = false; + if (ans[node]) + ok = false; + ans[non(node)] = true; + for (int nghb : adT[node]) + if (vis[nghb]) + dfsT(nghb, vis, ans, ok); + } + + public: + TwoSat(int n) : + n(n), adG(2 * n + 1), adT(2 * n + 1) { } + + void addProp(int x, int y) { + if (x < 0) x = n - x; + if (y < 0) y = n - y; + addEdge(non(x), y); + addEdge(non(y), x); + } + + vector solve() { + vector vis(2 * n + 1); + vector topo; + for (int i = 1; i <= 2 * n; i++) + if (!vis[i]) + dfsG(i, vis, topo); + reverse(topo.begin(), topo.end()); + + bool ok = true; + vector ans(2 * n + 1); + for (int node : topo) + if (vis[node] && vis[non(node)]) + dfsT(node, vis, ans, ok); + + if (!ok) + return vector(); + ans.resize(n + 1); + return ans; + } +}; + +int main() { + int n, m; fin >> n >> m; + TwoSat graph(n); + for (int i = 0; i < m; i++) { + int x, y, t; fin >> x >> y >> t; + if (t == 0) + graph.addProp(+x, +y); + else if (t == 1) + graph.addProp(+x, -y); + else if (t == 2) + graph.addProp(-x, +y); + else + graph.addProp(-x, -y); + } + + auto ans = graph.solve(); + int cnt = 0; + for (int i = 1; i <= n; i++) + cnt += ans[i]; + fout << cnt << '\n'; + for (int i = 1; i <= n; i++) + if (ans[i]) + fout << i << '\n'; + + fout.close(); + return 0; +} diff --git a/party/2sat_implication_reduction/9c508b83-0b55-11ec-a1ef-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c508b83-0b55-11ec-a1ef-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd6fa6fdb0c55ac1256012298d81a5ff2adf792a --- /dev/null +++ b/party/2sat_implication_reduction/9c508b83-0b55-11ec-a1ef-2cf05d84bfc2.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + + +std::ifstream fin("party.in"); + +std::ofstream fout("party.out"); + + + +const int NMAX = 500; + + + +std::vector g[NMAX], rg[NMAX]; + +std::bitset used, ans; + +std::stack topo; + +bool not_ok; + +int n, m, x, y, c; + + + +inline int neg(int x) + +{ + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void DFS1(int node) + +{ + + used[node] = 1; + + for (auto& i : g[node]) + + if (used[i] == 0) + + DFS1(i); + + topo.push(node); + +} + + + +void DFS2(int node) + +{ + + if (ans[node] == 1) + + not_ok = true; + + used[node] = 1; + + ans[neg(node)] = 1; + + for (auto& i : rg[node]) + + if (used[i] == 0) + + DFS2(i); + +} + + + +void poz() + +{ + + if (c == 1) + + y += n; + + else if (c == 2) + + x += n; + + else if (c == 3) + + { + + x += n; + + y += n; + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + fin >> x >> y >> c; + + poz(); + + int negx = neg(x), negy = neg(y); + + g[negx].push_back(y); + + g[negy].push_back(x); + + rg[x].push_back(negy); + + rg[y].push_back(negx); + + } + + + + for (int i = 1; i <= 2 * n; ++i) + + if (used[i] == 0) + + DFS1(i); + + + + used.reset(); + + + + while (!topo.empty()) + + { + + x = topo.top(); + + topo.pop(); + + if (used[x] == 0 && used[neg(x)] == 0) + + DFS2(x); + + } + + + + int cnt(0); + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + cnt++; + + fout << cnt << "\n"; + + for (int i = 1; i <= n; ++i) + + if (ans[i]) + + fout << i << "\n"; + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c50b296-0b55-11ec-8d18-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c50b296-0b55-11ec-8d18-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b37a0b66de23803d49b5c7100a8b4f0c3ba7c33 --- /dev/null +++ b/party/2sat_implication_reduction/9c50b296-0b55-11ec-8d18-2cf05d84bfc2.cpp @@ -0,0 +1,227 @@ +#include + + + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +class _2sat{ + + private: + + int n; + + vector > V,gV; + + vector uz; + + vector ord,comp; + + vector ans; + + + + void dfs1(int node){ + + uz[node]=true; + + for(auto& it:V[node]) + + if(!uz[it]) + + dfs1(it); + + ord.push_back(node); + + } + + void dfs2(int node,int c){ + + comp[node]=c; + + for(auto& it:gV[node]) + + if(!comp[it]) + + dfs2(it,c); + + } + + public: + + _2sat(int n){ + + this->n=n; + + V.resize(2*n+1); + + gV.resize(2*n+1); + + uz.resize(2*n+1); + + comp.resize(2*n+1); + + ans.resize(n+1); + + } + + void addegde(int x,int y){ + + cerr << "x,y" << " ->", debug_out(x,y); + + V[x].push_back(y); + + gV[y].push_back(x); + + } + + void solve(){ + + for(int i=1;i<=2*n;i++) + + if(!uz[i]) + + dfs1(i); + + for(int i=1,cates=0;i<=2*n;i++){ + + int node=ord[2*n-i]; + + if(!comp[node]) + + dfs2(node,++cates); + + } + + for(int i=1;i<=n;i++){ + + if(comp[i] == comp[i+n]){ + + fout<<"-1\n"; + + return; + + } + + ans[i]=(comp[i] > comp[i+n]); + + } + + int cate=0; + + for(int i=1;i<=n;i++) + + if(ans[i]) + + cate++; + + fout< vec; + +vector arb[DMAX]; + + + +int V[DMAX]; + + + +int n,m; + + + +int NOT(int node){ + + if(node > n) + + return node-n; + + return node+n; + +} + + + +int main(){ + + int t,i,j; + + int op,x,y; + + + + fin>>n>>m; + + _2sat var(n); + + for(i=1;i<=m;i++){ + + fin>>x>>y>>op; + + if(op == 1) + + y=NOT(y); + + else if(op == 2) + + x=NOT(x); + + else if(op == 3){ + + x=NOT(x); + + y=NOT(y); + + } + + var.addegde(NOT(x),y); + + var.addegde(NOT(y),x); + + } + + var.solve(); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c50b297-0b55-11ec-a44f-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c50b297-0b55-11ec-a44f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e64023449f1fba59846143d87b59ec8e8d66d39 --- /dev/null +++ b/party/2sat_implication_reduction/9c50b297-0b55-11ec-a44f-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; +ifstream f("party.in"); +ofstream g("party.out"); +int n, m, x, y, t, comp, rez_nr; +vector graf[2 * 100 + 5], graf_t[2 * 100 + 5]; +int c[2 * 100 + 5]; +stack st; +bool viz[2 * 100 + 5], rez[100 + 5]; + +int Not(int x) +{ + if(x < n) + return x + n; + return x - n; +} + +void add(int x, int y) +{ + graf[Not(x)].push_back(y); + graf[Not(y)].push_back(x); + graf_t[y].push_back(Not(x)); + graf_t[x].push_back(Not(y)); +} + +void Read() +{ + f>>n>>m; + for(int i = 1;i <= m;++i) + { + f>>x>>y>>t; + if(t == 0) + add(x, y); + else if(t == 1) + add(x, Not(y)); + else if(t == 2) + add(Not(x), y); + else + add(Not(x), Not(y)); + } +} + +void dfs(int nod) +{ + viz[nod] = true; + for(auto it : graf[nod]) + if(!viz[it]) + dfs(it); + st.push(nod); +} + +void dfs_t(int nod) +{ + c[nod] = comp; + for(auto it : graf_t[nod]) + if(!c[it]) + dfs_t(it); +} + +void Solve() +{ + for(int i = 1;i <= 2 * n;++i) + if(!viz[i]) + dfs(i); + while(!st.empty()) + { + int node = st.top(); st.pop(); + + if(!c[node]) + { + ++comp; + dfs_t(node); + } + } + for(int i = 1;i <= n;++i) + if(c[i] > c[Not(i)]) + ++rez_nr, rez[i] = true; + g< +using namespace std; + + + +int n, m, a, b, c, F = 101, T = 102, viz[202], uz[202]; + +vector v[2 * 202], v2[2 * 202], H, ans; + + + +void dfs(int nod){ + + uz[nod] = 1; + + for (auto it : v[nod]) if (!uz[it]) dfs(it); + + H.push_back(nod); + +} + + + +void dfs2(int nod){ + + + + uz[nod] = 0; viz[nod ^ 1] = 1; + + if (nod&1) ans.push_back(nod/2 + 1); + + for (auto it : v2[nod]) if (uz[it]) dfs2(it); + +} + + + +void add(int a, int b){ + + a = (a < 0 ? -a * 2 - 1 : a * 2 - 2); + + b = (b < 0 ? -b * 2 - 1 : b * 2 - 2); + + + + + + v[a^1].push_back(b); + + v[b^1].push_back(a); + + v2[a].push_back(b^1); + + v2[b].push_back(a^1); + +} + + + +int main(){ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + cin >> n >> m; + + + + + + for (int i=1;i<=m;i++){ + + cin >> a >> b >> c; + + if (c == 0){ + + add(a, b); + + } + + else if (c == 1){ + + add(a, -b); + + } + + else if (c == 2){ + + add(-a, b); + + } + + else{ + + add(-a, -b); + + } + + } + + + + for (int i=0;i<2*n;i++) if (!uz[i]) dfs(i); + + reverse(H.begin(), H.end()); + + for (auto it : H){ + + + + if (uz[it] && uz[it^1]) dfs2(it); + + } + + + + cout << ans.size() << '\n'; + + for (auto it : ans) + + cout << it << '\n'; + + + + + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c50d9a8-0b55-11ec-857e-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c50d9a8-0b55-11ec-857e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b984831a459df210e6cb35dec89173ab01052eb --- /dev/null +++ b/party/2sat_implication_reduction/9c50d9a8-0b55-11ec-857e-2cf05d84bfc2.cpp @@ -0,0 +1,230 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("party.in"); + +ofstream cout("party.out"); + + + +const int NMAX = 200; + + + +int N, M; + +vector g[NMAX + 5]; + +vector t[NMAX + 5]; + + + +bool seen[NMAX + 5]; + +stack st; + +void dfs1(int node) + +{ + + seen[node] = true; + + + + for(auto it : g[node]) + + if(!seen[it]) dfs1(it); + + + + st.push(node); + +} + + + +int kComp; + +int comp[NMAX + 5]; + +void dfs2(int node) + +{ + + comp[node] = kComp; + + + + for(auto it : t[node]) + + if(comp[it] == 0) dfs2(it); + +} + + + +vector sol; + + + +int Not(int x) + +{ + + return x; + +} + + + +int Nrm(int x) + +{ + + return x + N; + +} + + + +int main() + +{ + + cin >> N >> M; + + + + for(int i = 1; i <= M; i++) + + { + + int x, y, z; + + cin >> x >> y >> z; + + + + if(z == 0) + + { + + g[Not(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Nrm(x)); + + + + t[Nrm(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Not(y)); + + } + + else if(z == 1) + + { + + g[Not(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Nrm(x)); + + + + t[Not(y)].push_back(Not(x)); + + t[Nrm(x)].push_back(Nrm(y)); + + } + + else if(z == 2) + + { + + g[Nrm(x)].push_back(Nrm(y)); + + g[Not(y)].push_back(Not(x)); + + + + t[Nrm(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Not(y)); + + } + + else + + { + + g[Nrm(x)].push_back(Not(y)); + + g[Nrm(y)].push_back(Not(x)); + + + + t[Not(y)].push_back(Nrm(x)); + + t[Not(x)].push_back(Nrm(y)); + + } + + } + + + + for(int i = 1; i <= 2 * N; i++) + + if(!seen[i]) dfs1(i); + + + + while(!st.empty()) + + { + + int node = st.top(); st.pop(); + + + + if(comp[node] == 0) + + { + + ++kComp; + + dfs2(node); + + } + + } + + + + for(int i = 1; i <= N; i++) + + if(comp[i + N] > comp[i]) + + sol.push_back(i); + + + + cout << sol.size() << '\n'; + + for(auto it : sol) + + cout << it << '\n'; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/9c50d9a9-0b55-11ec-a2d6-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c50d9a9-0b55-11ec-a2d6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78dfe2c5ea0680d7179048e706f2d4452489efdb --- /dev/null +++ b/party/2sat_implication_reduction/9c50d9a9-0b55-11ec-a2d6-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +vectora[202],inv[202]; + +int n,k,kk[202],ctc[202],sol[102]; + +bool viz[202]; + + + +int neg(int x) + +{ + + if(x>n) + + return x-n; + + return x+n; + +} + + + +void dfs(int x) + +{ + + viz[x]=1; + + for(auto y:a[x]) + + if(viz[y]==0) + + dfs(y); + + kk[++k]=x; + +} + + + +void dfs_inv(int x) + +{ + + viz[x]=1; + + ctc[x]=k; + + for(auto y:inv[x]) + + if(viz[y]==0) + + dfs_inv(y); + +} + + + +void kosaraju() + +{ + + int i; + + for(i=1;i<=2*n;i++) + + if(viz[i]==0) + + dfs(i); + + for(i=1;i<=2*n;i++) + + viz[i]=0; + + k=0; + + for(i=2*n;i>=1;i--) + + if(viz[kk[i]]==0) + + { + + k++; + + dfs_inv(kk[i]); + + } + +} + + + +int main() + +{ + + int m,i,x,y,p; + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y>>p; + + if(p==0) + + { + + a[neg(x)].push_back(y); + + a[neg(y)].push_back(x); + + inv[y].push_back(neg(x)); + + inv[x].push_back(neg(y)); + + } + + else + + if(p==1) + + { + + a[neg(x)].push_back(neg(y)); + + a[y].push_back(x); + + inv[neg(y)].push_back(neg(x)); + + inv[x].push_back(y); + + } + + else + + if(p==2) + + { + + a[neg(y)].push_back(neg(x)); + + a[x].push_back(y); + + inv[neg(x)].push_back(neg(y)); + + inv[y].push_back(x); + + } + + else + + if(p==3) + + { + + a[x].push_back(neg(y)); + + a[y].push_back(neg(x)); + + inv[neg(y)].push_back(x); + + inv[neg(x)].push_back(y); + + } + + } + + kosaraju(); + + k=0; + + for(i=1;i<=n;i++) + + if(ctc[i]>ctc[i+n]) + + sol[++k]=i; + + g< + + + + +using namespace std; +typedef long long ll; +typedef long double ld; +typedef pair pi; +typedef pair pll; +typedef pair pld; + +ifstream iredirect; +ofstream oredirect; +const string file = "party"; +const ll INF = 9223372036854775807ll; +const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}, dy[] = {0, 0, 1, -1, 1, -1, 1, -1}, inf = 2147483647, nmax = 10015, offset = 10005; +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +int rnd(int l, int r) +{ + return uniform_int_distribution(l, r)(rng); +} + +void prepare_IO() +{ + + iredirect.open(file+".in"); + cin.rdbuf(iredirect.rdbuf()); + oredirect.open(file+".out"); + cout.rdbuf(oredirect.rdbuf()); + + + + + + +} + +int n, m, ordine[nmax*2], p, p2, belong[nmax*2]; +bool val[nmax], vis[nmax*2], ok[nmax*2]; +unordered_set v[nmax*2], vt[nmax*2], has[nmax*2]; + +void dfs(int x) +{ + vis[x+offset] = 1; + for (auto y : v[x+offset]) + if(!vis[y+offset]) + dfs(y); + ordine[++p] = x; +} + +void dfst(int x) +{ + vis[x+offset] = 0; + belong[x+offset] = p2; + has[p2].insert(x); + for (auto y : vt[x+offset]) + if(vis[y+offset]) + dfst(y); +} + +void solve() +{ + cin >> n >> m; + for (int i = 1; i <= m; ++i){ + int x, y, t; + cin >> x >> y >> t; + switch(t) { + case 1: { + y *= -1; + break; + } + case 2: { + x *= -1; + break; + } + case 3: { + x *= -1; + y *= -1; + break; + } + } + v[-x+offset].insert(y); + v[-y+offset].insert(x); + vt[y+offset].insert(-x); + vt[x+offset].insert(-y); + } + for (int i = -n; i <= n; ++i) + if(i != 0 && !vis[i+offset]) + dfs(i); + vector r; + for (int i = p; i >= 1; --i) + if(vis[ordine[i]+offset]){ + ++p2; + r.push_back(ordine[i]); + dfst(ordine[i]); + } + for (auto x : r) + if(!ok[belong[x+offset]]){ + int now = belong[x+offset], now2 = belong[-x+offset]; + ok[now] = ok[now2] = 1; + for (auto y : has[now]) + if(y > 0) + val[y] |= 0; + else val[-y] |= 1; + for (auto y : has[now2]) + if(y > 0) + val[y] |= 1; + else val[-y] |= 0; + } + vector sol; + for (int i = 1; i <= n; ++i) + if (val[i]) + sol.push_back(i); + cout << sol.size() << "\n"; + for (auto x : sol) + cout << x << " "; + cout << "\n"; +} + +signed main() +{ + prepare_IO(); + int number_of_tests = 1; + + for (int testcase = 1; testcase <= number_of_tests; ++testcase) + solve(); + return 0; +} diff --git a/party/2sat_implication_reduction/9c50d9ab-0b55-11ec-a513-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/9c50d9ab-0b55-11ec-a513-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2241576717ce49777437e0aa65dc2250fd4f5359 --- /dev/null +++ b/party/2sat_implication_reduction/9c50d9ab-0b55-11ec-a513-2cf05d84bfc2.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + +ifstream cin("party.in"); + +ofstream cout("party.out"); + +bool viz[205]; + +int order[205],ctc[205],cnt,cont; + +vector v[205],v1[205]; + +void add(int a,int b){ + + v[a].push_back(b); + + v1[b].push_back(a); + +} + +void dfs1(int w){ + + viz[w]=1; + + for(auto u:v[w]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=w; + +} + +void dfs2(int w){ + + viz[w]=1; + + ctc[w]=cnt; + + for(auto u:v1[w]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + +int main() + +{ + + int n,m,a,b,t; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b>>t; + + if(t==0){ + + add(a+n,b); + + add(b+n,a); + + } + + if(t==1){ + + add(a+n,b+n); + + add(b,a); + + } + + if(t==2){ + + add(a,b); + + add(b+n,a+n); + + } + + if(t==3){ + + add(a,b+n); + + add(b,a+n); + + } + + } + + for(int i=1;i<=2*n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=2*n;i++){ + + viz[i]=0; + + } + + for(int i=2*n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + int sum=0; + + for(int i=1;i<=n;i++){ + + sum+=(ctc[i]>ctc[i+n]); + + } + + cout<ctc[i+n]) + + cout< +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/a7a58e0d-2f61-44f2-9403-81e76883b343.cpp b/party/2sat_implication_reduction/a7a58e0d-2f61-44f2-9403-81e76883b343.cpp new file mode 100644 index 0000000000000000000000000000000000000000..986493fb684c691c9b1efddaae4ea3513b5af6a7 --- /dev/null +++ b/party/2sat_implication_reduction/a7a58e0d-2f61-44f2-9403-81e76883b343.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + +vector G[210], T[210]; + +int non ( int x ) +{ + if( x <= n ) return x+n; + return x - n ; +} + +void sortaret ( int nod ) +{ + int N = G[nod].size(),i; + + viz[nod] = 1 ; + + for( i = 0 ; i < N ; i++ ) + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + S[--nn] = nod ; +} + +void dfs ( int nod ) +{ + int N = T[nod].size(),i; + + viz[nod] = 0 ; + + sol[ non(nod) ] = 1 ; + if( nod > n ) invitati++; + + for( i = 0 ; i < N ; i++ ) + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; +} + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for( i = 1 ; i <= m ; i++ ) + { + scanf("%d %d %d",&a,&b,&tip); + + if( tip == 0 ) + { + G[non(a)].push_back(b); + G[non(b)].push_back(a); + T[a].push_back(non(b)); + T[b].push_back(non(a)); + } + else if( tip == 1 ) + { + G[non(a)].push_back(non(b)); + G[b].push_back(a); + + T[non(b)].push_back(non(a)); + T[a].push_back(b); + } + else if( tip == 2 ) + { + G[non(b)].push_back(non(a)); + G[a].push_back(b); + + T[non(a)].push_back(non(b)); + T[b].push_back(a); + } + else + { + G[a].push_back(non(b)); + G[b].push_back(non(a)); + T[non(a)].push_back(b); + T[non(b)].push_back(a); + } + } + + nn = n + n + 1 ; + + for( i = 1 ; i <= n+n ; i++ ) + if( !viz[i]) sortaret(i); + + for( i = 1 ; i <= n+n ; i++ ) + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + dfs( S[i] ) ; + + printf("%d\n",invitati); + + for( i = 1 ; i <= n ; i++ ) + if( sol[i] ) printf("%d\n",i); + + return 0; +} diff --git a/party/2sat_implication_reduction/a9271e13-17e1-42b4-80e1-a005dce4f7f0.cpp b/party/2sat_implication_reduction/a9271e13-17e1-42b4-80e1-a005dce4f7f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/a9271e13-17e1-42b4-80e1-a005dce4f7f0.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/ac0aebb4-fd08-4a20-bd86-d9692652ef52.cpp b/party/2sat_implication_reduction/ac0aebb4-fd08-4a20-bd86-d9692652ef52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c11305dc56850c9a785e23a9aa559e7e48e6c127 --- /dev/null +++ b/party/2sat_implication_reduction/ac0aebb4-fd08-4a20-bd86-d9692652ef52.cpp @@ -0,0 +1,195 @@ +#include +#include + + +using namespace std; + + + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + + + +vector G[210], T[210]; + + + +int non ( int x ) + +{ + + if( x <= n ) return x+n; + + return x - n ; + +} + + + +void sortaret ( int nod ) + +{ + + int N = G[nod].size(),i; + + + + viz[nod] = 1 ; + + + + for( i = 0 ; i < N ; i++ ) + + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + + + S[--nn] = nod ; + +} + + + +void dfs ( int nod ) + +{ + + int N = T[nod].size(),i; + + + + viz[nod] = 0 ; + + + + sol[ non(nod) ] = 1 ; + + if( nod > n ) invitati++; + + + + for( i = 0 ; i < N ; i++ ) + + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + + + for( i = 1 ; i <= m ; i++ ) + + { + + scanf("%d %d %d",&a,&b,&tip); + + + + if( tip == 0 ) + + { + + G[non(a)].push_back(b); + + G[non(b)].push_back(a); + + T[a].push_back(non(b)); + + T[b].push_back(non(a)); + + } + + else if( tip == 1 ) + + { + + G[non(a)].push_back(non(b)); + + G[b].push_back(a); + + + + T[non(b)].push_back(non(a)); + + T[a].push_back(b); + + } + + else if( tip == 2 ) + + { + + G[non(b)].push_back(non(a)); + + G[a].push_back(b); + + + + T[non(a)].push_back(non(b)); + + T[b].push_back(a); + + } + + else + + { + + G[a].push_back(non(b)); + + G[b].push_back(non(a)); + + T[non(a)].push_back(b); + + T[non(b)].push_back(a); + + } + + } + + + + nn = n + n + 1 ; + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( !viz[i]) sortaret(i); + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + + dfs( S[i] ) ; + + + + printf("%d\n",invitati); + + + + for( i = 1 ; i <= n ; i++ ) + + if( sol[i] ) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/ae80ca44-687b-43ac-9398-824e39bc41ba.cpp b/party/2sat_implication_reduction/ae80ca44-687b-43ac-9398-824e39bc41ba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8be636c51d1d69eb97945cf66f1ad147006b5778 --- /dev/null +++ b/party/2sat_implication_reduction/ae80ca44-687b-43ac-9398-824e39bc41ba.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +vector < vector < int > > lv,lv2; + +vector noduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + + + +int neg(int x) + +{ + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + + + +void citire() + +{ + + int a,b,t; + + scanf("%d%d\n",&n,&m); + + lv.resize(2*n+1); + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/ae98d598-d7b2-4fc5-a5ed-6e370e2af16e.cpp b/party/2sat_implication_reduction/ae98d598-d7b2-4fc5-a5ed-6e370e2af16e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8be636c51d1d69eb97945cf66f1ad147006b5778 --- /dev/null +++ b/party/2sat_implication_reduction/ae98d598-d7b2-4fc5-a5ed-6e370e2af16e.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +vector < vector < int > > lv,lv2; + +vector noduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + + + +int neg(int x) + +{ + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + + + +void citire() + +{ + + int a,b,t; + + scanf("%d%d\n",&n,&m); + + lv.resize(2*n+1); + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/afa5b10b-c559-4a5d-a0b3-cc218866df54.cpp b/party/2sat_implication_reduction/afa5b10b-c559-4a5d-a0b3-cc218866df54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c41e2b33ef14020aeb62e8cb6a84a57bc8a894 --- /dev/null +++ b/party/2sat_implication_reduction/afa5b10b-c559-4a5d-a0b3-cc218866df54.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/b0897235-abea-4a89-a234-a5edb27703ef.cpp b/party/2sat_implication_reduction/b0897235-abea-4a89-a234-a5edb27703ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/b0897235-abea-4a89-a234-a5edb27703ef.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< +#include +#include + + +using namespace std; + + + +vector < vector < int > > lv,lv2; + +vector noduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + + + +int neg(int x) + +{ + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + + + +void citire() + +{ + + int a,b,t; + + scanf("%d%d\n",&n,&m); + + lv.resize(2*n+1); + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/b5c12096-cf53-44cc-9af2-0a59862b4174.cpp b/party/2sat_implication_reduction/b5c12096-cf53-44cc-9af2-0a59862b4174.cpp new file mode 100644 index 0000000000000000000000000000000000000000..098f99a590129e5d8cc70ef62529c9aa13bae215 --- /dev/null +++ b/party/2sat_implication_reduction/b5c12096-cf53-44cc-9af2-0a59862b4174.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + + +const int MAX_N(105); +const int MAX_M(1005); + +int n, m; +std::vector> Exp; +std::vector Result; +bool Value [MAX_N << 1]; +std::bitset Mark; +std::deque Sorted; +std::vector Graph [MAX_N << 1]; +std::vector Transpose [MAX_N << 1]; +bool Flag [MAX_N]; + +inline void Read (void) +{ + std::freopen("party.in","r",stdin); + std::scanf("%d %d",&n,&m); + for (int i(1), a, b, t ; i <= m ; ++i) + { + std::scanf("%d %d %d",&a,&b,&t); + if (t == 0) + Exp.push_back(std::make_pair(a,b)); + else if (t == 1) + Exp.push_back(std::make_pair(a,b + n)); + else if (t == 2) + Exp.push_back(std::make_pair(a + n,b)); + else + Exp.push_back(std::make_pair(a + n,b + n)); + Flag[a] = Flag[b] = true; + } + std::fclose(stdin); +} + +inline void Print (void) +{ + std::freopen("party.out","w",stdout); + std::printf("%d\n",static_cast(Result.size())); + for (auto it : Result) + std::printf("%d\n",it); + std::fclose(stdout); +} + +inline int Non (const int X) +{ + if (X <= n) + return X + n; + return X - n; +} + +inline void Build (void) +{ + for (auto it : Exp) + { + Graph[Non(it.first)].push_back(it.second); + Graph[Non(it.second)].push_back(it.first); + Transpose[it.second].push_back(Non(it.first)); + Transpose[it.first].push_back(Non(it.second)); + } +} + +void Toposort (int node) +{ + for (auto it : Graph[node]) + if (!Mark[it]) + { + Mark[it] = true; + Toposort(it); + } + Sorted.push_front(node); +} + +void DepthFirstSearch (int node) +{ + Mark[node] = true; + Value[Non(node)] = true; + for (auto it : Transpose[node]) + if (!Mark[it]) + DepthFirstSearch(it); +} + +inline void Kosaraju (void) +{ + for (int i(1), end(n << 1) ; i <= end ; ++i) + if (!Mark[i] && Flag[i]) + { + Mark[i] = true; + Toposort(i); + } + Mark.reset(); + for (auto it : Sorted) + if (!(Value[it] || Value[Non(it)])) + DepthFirstSearch(it); +} + +inline void Sat (void) +{ + Build(); + Kosaraju(); +} + +inline void Count (void) +{ + for (int i(1) ; i <= n ; ++i) + if (Value[i] || !Flag[i]) + Result.push_back(i); +} + +int main (void) +{ + Read(); + Sat(); + Count(); + Print(); + return 0; +} diff --git a/party/2sat_implication_reduction/b7042588-3cc6-4a06-8e61-a3e2042175da.cpp b/party/2sat_implication_reduction/b7042588-3cc6-4a06-8e61-a3e2042175da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/b7042588-3cc6-4a06-8e61-a3e2042175da.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/bb4b72b6-4a18-469e-a518-949cb30969bf.cpp b/party/2sat_implication_reduction/bb4b72b6-4a18-469e-a518-949cb30969bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cea448c6dc660d3ad6b09afa1daecbdf5a67dae1 --- /dev/null +++ b/party/2sat_implication_reduction/bb4b72b6-4a18-469e-a518-949cb30969bf.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include + + + +using namespace std; + +FILE*f=fopen("party.in","r"); +FILE*g=fopen("party.out","w"); + +int n,m,index; +int viz[105<<1],lowest[105<<1],niv[105<<1],comp[105<<1],last[105<<1],gr_in[105<<1],sol[105<<1]; +vectorG[105<<1],W[105<<1],v; +stackst; int inst[105<<1]; +vector< vector >C; +queueQ; + +inline int NOT ( const int &x ){ + + if ( x <= n ){ + return n+x; + } + return x-n; +} + +inline void componenta ( const int &x ){ + int nod; v.clear(); + + do{ + nod = st.top(); v.push_back(nod); comp[nod] = C.size()+1; + st.pop(); inst[nod] = 0; + }while(x != nod); + + C.push_back(v); +} + +void DFS ( int nod ){ + viz[nod] = 1; niv[nod] = lowest[nod] = ++index; + st.push(nod); inst[nod] = 1; + + for ( vector::iterator itt = G[nod].begin() ; itt != G[nod].end() ; ++itt ){ + int nodvcn = (*itt); + + if ( !viz[nodvcn] ){ + DFS(nodvcn); + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + else{ + if ( inst[nodvcn] ){ + lowest[nod] = min(lowest[nod],lowest[nodvcn]); + } + } + } + + if ( lowest[nod] == niv[nod] ){ + componenta(nod); + } +} + +inline void CTC () { + + for ( int i = 1 ; i <= (n<<1) ; ++i ){ + if ( !viz[i] ){ + DFS(i); + } + } +} + +inline void solve () { + + int Ncomp = C.size(); + for ( int i = 0 ; i < Ncomp ; ++i ){ + for ( vector::iterator itt = C[i].begin() ; itt != C[i].end() ; ++itt ){ + int x = (*itt); + + for ( vector::iterator vecin = G[x].begin() ; vecin != G[x].end() ; ++vecin ){ + int compvcn = comp[*vecin]; + if ( last[compvcn] != i+1 && compvcn != i+1 ){ + W[i+1].push_back(compvcn); ++gr_in[compvcn]; + last[compvcn] = i+1; + } + } + } + } + + for ( int i = 1 ; i <= Ncomp ; ++i ){ + if ( !gr_in[i] ){ + Q.push(i); + } + } + + int nod; + for ( ; !Q.empty() ; ){ + nod = Q.front(); Q.pop(); + + for ( vector::iterator itt = W[nod].begin() ; itt != W[nod].end() ; ++itt ){ + int nodvcn = (*itt); + --gr_in[nodvcn]; + if ( !gr_in[nodvcn] ) + Q.push(nodvcn); + } + if ( last[nod] == -1 ) continue ; + + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 0; + } + nod = comp[NOT(C[nod-1][0])]; last[nod] = -1; + for ( vector::iterator itt = C[nod-1].begin() ; itt != C[nod-1].end() ; ++itt ){ + sol[*itt] = 1; + } + } + + int Nsol = 0; + for ( int i = 1 ; i <= n ; ++i ){ + Nsol += sol[i]; + } + fprintf(g,"%d\n",Nsol); + for ( int i = 1 ; i <= n ; ++i ){ + if ( sol[i] ){ + fprintf(g,"%d\n",i); + } + } +} + +int main () { + + fscanf(f,"%d %d",&n,&m); + int a,b,tip; + for ( int i = 1 ; i <= m ; ++i ){ + fscanf(f,"%d %d %d",&a,&b,&tip); + + if ( tip == 1 ){ + b = -b; + } + else{ + if ( tip == 2 ){ + a = -a; + } + else{ + if ( tip == 3 ){ + a = -a; b = -b; + } + } + } + + if ( a < 0 ) a = -a+n; + if ( b < 0 ) b = -b+n; + G[NOT(a)].push_back(b); + G[NOT(b)].push_back(a); + } + + CTC(); + solve(); + + fclose(f); + fclose(g); + + return 0; +} diff --git a/party/2sat_implication_reduction/bb62a308-bcff-4ca2-9bdb-5639978004ab.cpp b/party/2sat_implication_reduction/bb62a308-bcff-4ca2-9bdb-5639978004ab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a1c41e2b33ef14020aeb62e8cb6a84a57bc8a894 --- /dev/null +++ b/party/2sat_implication_reduction/bb62a308-bcff-4ca2-9bdb-5639978004ab.cpp @@ -0,0 +1,276 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/bb865efe-73cf-47e5-b369-1afc26288910.cpp b/party/2sat_implication_reduction/bb865efe-73cf-47e5-b369-1afc26288910.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/bb865efe-73cf-47e5-b369-1afc26288910.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/be9d13cc-0b54-11ec-b985-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/be9d13cc-0b54-11ec-b985-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e3aefce92d978c9eee5fab669084095634356298 --- /dev/null +++ b/party/2sat_implication_reduction/be9d13cc-0b54-11ec-b985-2cf05d84bfc2.cpp @@ -0,0 +1,232 @@ +#include +#include +#include +#include + + +using namespace std; +vector< int > G[256],GT[256]; + +stack< int > Q; + +bool viz[256],SOL[256]; + +int N,M,ANS; + + + +inline int faNegare( int value ) + +{ + + return value<=N ? value+N : value-N; + +} + + + +inline void push_edge( int node1, int node2 ) + +{ + + G[node1].push_back( node2 ); + + GT[node2].push_back( node1 ); + +} + + + +void DF1( int node ) + +{ + + viz[node]=1; + + + + unsigned int i,v; + + for(i=0; i +#include +#include +#include +#include +#include + + +const int MAX_N(105); +const int MAX_M(1005); + +int n, m; +std::vector> Exp; +std::vector Result; +bool Value [MAX_N << 1]; +std::bitset Mark; +std::deque Sorted; +std::vector Graph [MAX_N << 1]; +std::vector Transpose [MAX_N << 1]; +bool Flag [MAX_N]; + +inline void Read (void) +{ + std::freopen("party.in","r",stdin); + std::scanf("%d %d",&n,&m); + for (int i(1), a, b, t ; i <= m ; ++i) + { + std::scanf("%d %d %d",&a,&b,&t); + if (t == 0) + Exp.push_back(std::make_pair(a,b)); + else if (t == 1) + Exp.push_back(std::make_pair(a,b + n)); + else if (t == 2) + Exp.push_back(std::make_pair(a + n,b)); + else + Exp.push_back(std::make_pair(a + n,b + n)); + Flag[a] = Flag[b] = true; + } + std::fclose(stdin); +} + +inline void Print (void) +{ + std::freopen("party.out","w",stdout); + std::printf("%d\n",static_cast(Result.size())); + for (auto it : Result) + std::printf("%d\n",it); + std::fclose(stdout); +} + +inline int Non (const int X) +{ + if (X <= n) + return X + n; + return X - n; +} + +inline void Build (void) +{ + for (auto it : Exp) + { + Graph[Non(it.first)].push_back(it.second); + Graph[Non(it.second)].push_back(it.first); + Transpose[it.second].push_back(Non(it.first)); + Transpose[it.first].push_back(Non(it.second)); + } +} + +void Toposort (int node) +{ + for (auto it : Graph[node]) + if (!Mark[it]) + { + Mark[it] = true; + Toposort(it); + } + Sorted.push_front(node); +} + +void DepthFirstSearch (int node) +{ + Mark[node] = true; + Value[Non(node)] = true; + for (auto it : Transpose[node]) + if (!Mark[it]) + DepthFirstSearch(it); +} + +inline void Kosaraju (void) +{ + for (int i(1), end(n << 1) ; i <= end ; ++i) + if (!Mark[i] && Flag[i]) + { + Mark[i] = true; + Toposort(i); + } + Mark.reset(); + for (auto it : Sorted) + if (!(Value[it] || Value[Non(it)])) + DepthFirstSearch(it); +} + +inline void Sat (void) +{ + Build(); + Kosaraju(); +} + +inline void Count (void) +{ + for (int i(1) ; i <= n ; ++i) + if (Value[i] || !Flag[i]) + Result.push_back(i); +} + +int main (void) +{ + Read(); + Sat(); + Count(); + Print(); + return 0; +} diff --git a/party/2sat_implication_reduction/c8dda6f7-0dad-4718-a574-746f5611583f.cpp b/party/2sat_implication_reduction/c8dda6f7-0dad-4718-a574-746f5611583f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f060d8989122d18189f43915d7bd5df2cd0f4eb --- /dev/null +++ b/party/2sat_implication_reduction/c8dda6f7-0dad-4718-a574-746f5611583f.cpp @@ -0,0 +1,202 @@ +#include +#include +#include + + + + +using namespace std; + + + +vector G[256],Gt[256],deque; + +int N,Answer; + +bool used[256],mark[256]; + + + +void DFS(int Nod) { + + + + used[Nod]=0; + + mark[((Nod)<=N?(Nod+N):(Nod-N))]=1; + + + + for(int i=0;i>N>>M; + + + + while(M--) { + + + + in>>x>>y>>type; + + + + switch(type) { + + case 0:AddEdges(x,y);break; + + case 1:AddEdges(x,((y)<=N?(y+N):(y-N)));break; + + case 2:AddEdges(((x)<=N?(x+N):(x-N)),y);break; + + case 3:AddEdges(((x)<=N?(x+N):(x-N)),((y)<=N?(y+N):(y-N)));break; + + } + + + + } + + + + in.close(); + + + +} + +void write() { + + + + ofstream out("party.out"); + + + + for(int i=1;i<=N;i++) + + Answer+=mark[i]; + + + + out< +#include + + + + + +using namespace std; + + + + + +int N,M; +vector L[2*105]; +vector T[2*105]; +short int timp[2*105],order[2*105]; +int next_time; +short int CTC[2*105],cnt_ctc=0; + +vector LCTC[2*105]; +short int timeCTC[2*105]; +vector sol; + +inline short int cuminus(short int x) +{ + if(x<=N) + return x+N; + else + return x-N; +} + +inline void add(short int a, short int b) +{ + L[cuminus(a)].push_back(b); T[b].push_back(cuminus(a)); + L[cuminus(b)].push_back(a); T[a].push_back(cuminus(b)); +} + +void get_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i::iterator i=L[vertex].begin(); i!=L[vertex].end(); ++i) + if(timp[*i]==-1) + { + timp[*i]=0; + df1(*i); + ++next_time; + } + timp[vertex]=next_time; +} + +void df2(int vertex) +{ + for(vector ::iterator i=T[vertex].begin(); i!=T[vertex].end(); ++i) + if(!CTC[*i]) + { + CTC[*i]=cnt_ctc; + df2(*i); + } +} + +void df3(int vertex) +{ + for(vector ::iterator i=LCTC[vertex].begin(); i!=LCTC[vertex].end(); ++i) + if(timeCTC[*i]==-1) + { + timeCTC[*i]=0; + df3(*i); + ++next_time; + } + timeCTC[vertex]=next_time; +} + + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + get_data(); + + int i; + next_time=1; + for(i=1;i<=2*N;i++) + timp[i]=-1; + for(i=1;i<=2*N;i++) + if(timp[i]==-1) + { + timp[i]=0; + df1(i); + ++next_time; + } + for(i=1;i<=2*N;i++) + { + order[2*N-timp[i]+1]=i; + CTC[i]=0; + } + for(i=1;i<=2*N;i++) + if(!CTC[order[i]]) + { + CTC[order[i]]=++cnt_ctc; + df2(order[i]); + } + + for(i=1;i<=2*N;i++) + for(vector ::iterator it=L[i].begin(); it!=L[i].end(); ++it) + if(CTC[i]!=CTC[*it]) + LCTC[CTC[i]].push_back(CTC[*it]); + next_time=1; + for(i=1;i<=cnt_ctc;i++) + timeCTC[i]=-1; + for(i=1;i<=cnt_ctc;i++) + if(timeCTC[i]==-1) + { + timeCTC[i]=0; + df3(i); + ++next_time; + } + + for(i=1;i<=N;i++) + if(timeCTC[CTC[i]]::iterator it=sol.begin(); it!=sol.end(); ++it) + printf("%d\n",(int)(*it)); + printf("23"); + + return 0; +} diff --git a/party/2sat_implication_reduction/cee9236f-4f98-427a-b1c9-28bcdad0a0ff.cpp b/party/2sat_implication_reduction/cee9236f-4f98-427a-b1c9-28bcdad0a0ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..986493fb684c691c9b1efddaae4ea3513b5af6a7 --- /dev/null +++ b/party/2sat_implication_reduction/cee9236f-4f98-427a-b1c9-28bcdad0a0ff.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + +vector G[210], T[210]; + +int non ( int x ) +{ + if( x <= n ) return x+n; + return x - n ; +} + +void sortaret ( int nod ) +{ + int N = G[nod].size(),i; + + viz[nod] = 1 ; + + for( i = 0 ; i < N ; i++ ) + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + S[--nn] = nod ; +} + +void dfs ( int nod ) +{ + int N = T[nod].size(),i; + + viz[nod] = 0 ; + + sol[ non(nod) ] = 1 ; + if( nod > n ) invitati++; + + for( i = 0 ; i < N ; i++ ) + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; +} + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for( i = 1 ; i <= m ; i++ ) + { + scanf("%d %d %d",&a,&b,&tip); + + if( tip == 0 ) + { + G[non(a)].push_back(b); + G[non(b)].push_back(a); + T[a].push_back(non(b)); + T[b].push_back(non(a)); + } + else if( tip == 1 ) + { + G[non(a)].push_back(non(b)); + G[b].push_back(a); + + T[non(b)].push_back(non(a)); + T[a].push_back(b); + } + else if( tip == 2 ) + { + G[non(b)].push_back(non(a)); + G[a].push_back(b); + + T[non(a)].push_back(non(b)); + T[b].push_back(a); + } + else + { + G[a].push_back(non(b)); + G[b].push_back(non(a)); + T[non(a)].push_back(b); + T[non(b)].push_back(a); + } + } + + nn = n + n + 1 ; + + for( i = 1 ; i <= n+n ; i++ ) + if( !viz[i]) sortaret(i); + + for( i = 1 ; i <= n+n ; i++ ) + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + dfs( S[i] ) ; + + printf("%d\n",invitati); + + for( i = 1 ; i <= n ; i++ ) + if( sol[i] ) printf("%d\n",i); + + return 0; +} diff --git a/party/2sat_implication_reduction/cf26fdc6-b020-46f9-8787-ed06b2f02e57.cpp b/party/2sat_implication_reduction/cf26fdc6-b020-46f9-8787-ed06b2f02e57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c11305dc56850c9a785e23a9aa559e7e48e6c127 --- /dev/null +++ b/party/2sat_implication_reduction/cf26fdc6-b020-46f9-8787-ed06b2f02e57.cpp @@ -0,0 +1,195 @@ +#include +#include + + +using namespace std; + + + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + + + +vector G[210], T[210]; + + + +int non ( int x ) + +{ + + if( x <= n ) return x+n; + + return x - n ; + +} + + + +void sortaret ( int nod ) + +{ + + int N = G[nod].size(),i; + + + + viz[nod] = 1 ; + + + + for( i = 0 ; i < N ; i++ ) + + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + + + S[--nn] = nod ; + +} + + + +void dfs ( int nod ) + +{ + + int N = T[nod].size(),i; + + + + viz[nod] = 0 ; + + + + sol[ non(nod) ] = 1 ; + + if( nod > n ) invitati++; + + + + for( i = 0 ; i < N ; i++ ) + + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + + + for( i = 1 ; i <= m ; i++ ) + + { + + scanf("%d %d %d",&a,&b,&tip); + + + + if( tip == 0 ) + + { + + G[non(a)].push_back(b); + + G[non(b)].push_back(a); + + T[a].push_back(non(b)); + + T[b].push_back(non(a)); + + } + + else if( tip == 1 ) + + { + + G[non(a)].push_back(non(b)); + + G[b].push_back(a); + + + + T[non(b)].push_back(non(a)); + + T[a].push_back(b); + + } + + else if( tip == 2 ) + + { + + G[non(b)].push_back(non(a)); + + G[a].push_back(b); + + + + T[non(a)].push_back(non(b)); + + T[b].push_back(a); + + } + + else + + { + + G[a].push_back(non(b)); + + G[b].push_back(non(a)); + + T[non(a)].push_back(b); + + T[non(b)].push_back(a); + + } + + } + + + + nn = n + n + 1 ; + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( !viz[i]) sortaret(i); + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + + dfs( S[i] ) ; + + + + printf("%d\n",invitati); + + + + for( i = 1 ; i <= n ; i++ ) + + if( sol[i] ) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/cfcb42fe-0b53-11ec-9068-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/cfcb42fe-0b53-11ec-9068-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e19f357879d485b89ec5e3e4657dd1b77669a43d --- /dev/null +++ b/party/2sat_implication_reduction/cfcb42fe-0b53-11ec-9068-2cf05d84bfc2.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +using namespace std; + +const int MAX_N=110; + +vector G[2*MAX_N]; +vector G2[2*MAX_N]; + +bool viz[2*MAX_N]; +bool val[2*MAX_N]; +int st[2*MAX_N]; + +int n; + +inline int negare(int val) { + if(val>n) { + return val-n; + } + return val+n; +} + +void dfffs(int gr_nod) { + viz[gr_nod]=true; + for(vector::iterator it=G[gr_nod].begin(); it!=G[gr_nod].end(); it++) { + if(!viz[*it]) { + dfffs(*it); + } + } + st[++st[0]]=gr_nod; +} + +void dfstrv(int gr_nod) { + viz[gr_nod]=true; + + val[negare(gr_nod)]=true; + + for(vector::iterator it=G2[gr_nod].begin(); it!=G2[gr_nod].end(); it++) { + if(!viz[*it]) { + dfstrv(*it); + } + } +} + +int main() { + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + int m; + scanf("%d%d",&n,&m); + for(int i=1;i<=m;i++) { + int t,a,b; + scanf("%d%d%d",&a,&b,&t); + if(t==0) { + + } + else if(t==1) { + b=negare(b); + } + else if(t==2) { + a=negare(a); + } + else if(t==3) { + a=negare(a); + b=negare(b); + } + + + G[negare(a)].push_back(b); + G[negare(b)].push_back(a); + + G2[b].push_back(negare(a)); + G2[a].push_back(negare(b)); + } + + for(int i=1;i<=2*n;i++) { + if(!viz[i]) { + dfffs(i); + } + } + memset(viz,0,sizeof(viz)); + for(int i=2*n;i>=1;i--) { + if(!viz[st[i]]&&!viz[negare(st[i])]) { + dfstrv(st[i]); + } + } + + vector sol; + for(int i=1;i<=n;i++) { + if(val[i]) { + sol.push_back(i); + } + } + + printf("%d\n",sol.size()); + for(vector::iterator it=sol.begin(); it!=sol.end(); it++) { + printf("%d\n",*it); + } + + return 0; +} diff --git a/party/2sat_implication_reduction/d4cbafeb-1b07-4106-88cd-3088bfe7e722.cpp b/party/2sat_implication_reduction/d4cbafeb-1b07-4106-88cd-3088bfe7e722.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8be636c51d1d69eb97945cf66f1ad147006b5778 --- /dev/null +++ b/party/2sat_implication_reduction/d4cbafeb-1b07-4106-88cd-3088bfe7e722.cpp @@ -0,0 +1,194 @@ +#include +#include +#include + + +using namespace std; + + + +vector < vector < int > > lv,lv2; + +vector noduri,v,f,plus,minus; + +int n,m,i,j,pas,ct=0; + + + +int neg(int x) + +{ + + if (x<=n) + + return x+n; + + else + + return x-n; + +} + + + +void citire() + +{ + + int a,b,t; + + scanf("%d%d\n",&n,&m); + + lv.resize(2*n+1); + + for (i=0;if[i+n]) + + ct++; + + printf("%d\n",ct); + + for (i=1;i<=n;i++) + + if (f[i]>f[i+n]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/d50bf4db-9975-47aa-b0ae-b1c9c2ca213f.cpp b/party/2sat_implication_reduction/d50bf4db-9975-47aa-b0ae-b1c9c2ca213f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..696dab1188fb69221ce5924b91a98ceed2769d6c --- /dev/null +++ b/party/2sat_implication_reduction/d50bf4db-9975-47aa-b0ae-b1c9c2ca213f.cpp @@ -0,0 +1,200 @@ +#include +#include +#include + + +const int MAX_N = 1000; + + + +int n; + +std::vector g[1 + 2 * MAX_N]; + +std::vector trans[1 + 2 * MAX_N]; + +std::vector topo; + +std::vector sol; + +bool viz[1 + 2 * MAX_N]; + +bool val[1 + 2 * MAX_N]; + +bool possible = true; + + + +int conj(int x) { + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void make_edge(int u, int v) { + + g[u].push_back(v); + + trans[v].push_back(u); + +} + + + +void topoSort(int u) { + + viz[u] = true; + + for (auto v : g[u]) { + + if (!viz[v]) + + topoSort(v); + + } + + topo.push_back(u); + +} + + + +void markNodes(int u) { + + viz[u] = true; + + if (val[u]) { + + possible = false; + + return; + + } + + val[u] = false; + + val[conj(u)] = true; + + for (auto v : trans[u]) { + + if (!viz[v]) + + markNodes(v); + + } + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + int m; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; i++) { + + int x, y, restriction; + + scanf("%d%d%d", &x, &y, &restriction); + + switch (restriction) { + + case 0: + + make_edge(conj(x), y); + + make_edge(conj(y), x); + + break; + + case 1: + + make_edge(conj(x), conj(y)); + + make_edge(y, x); + + break; + + case 2: + + make_edge(conj(y), conj(x)); + + make_edge(x, y); + + break; + + case 3: + + make_edge(x, conj(y)); + + make_edge(y, conj(x)); + + break; + + } + + } + + for (int i = 1; i <= 2 * n; i++) { + + if (!viz[i]) + + topoSort(i); + + } + + std::reverse(topo.begin(), topo.end()); + + for (int i = 1; i <= 2 * n; i++) + + viz[i] = false; + + for (auto node : topo) { + + if (!viz[node] && !viz[conj(node)]) + + markNodes(node); + + if (!possible) { + + printf("-1\n"); + + return 0; + + } + + } + + for (int i = 1; i <= n; i++) { + + if (val[i]) + + sol.push_back(i); + + } + + printf("%d\n", sol.size()); + + for (auto node : sol) + + printf("%d\n", node); + + printf("\n"); + + return 0; + +} diff --git a/party/2sat_implication_reduction/d5f07158-8a42-44a4-9d7b-763b6deecc7a.cpp b/party/2sat_implication_reduction/d5f07158-8a42-44a4-9d7b-763b6deecc7a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/d5f07158-8a42-44a4-9d7b-763b6deecc7a.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/d721f0c0-2463-48f8-b041-b57368a2680e.cpp b/party/2sat_implication_reduction/d721f0c0-2463-48f8-b041-b57368a2680e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..098f99a590129e5d8cc70ef62529c9aa13bae215 --- /dev/null +++ b/party/2sat_implication_reduction/d721f0c0-2463-48f8-b041-b57368a2680e.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + + +const int MAX_N(105); +const int MAX_M(1005); + +int n, m; +std::vector> Exp; +std::vector Result; +bool Value [MAX_N << 1]; +std::bitset Mark; +std::deque Sorted; +std::vector Graph [MAX_N << 1]; +std::vector Transpose [MAX_N << 1]; +bool Flag [MAX_N]; + +inline void Read (void) +{ + std::freopen("party.in","r",stdin); + std::scanf("%d %d",&n,&m); + for (int i(1), a, b, t ; i <= m ; ++i) + { + std::scanf("%d %d %d",&a,&b,&t); + if (t == 0) + Exp.push_back(std::make_pair(a,b)); + else if (t == 1) + Exp.push_back(std::make_pair(a,b + n)); + else if (t == 2) + Exp.push_back(std::make_pair(a + n,b)); + else + Exp.push_back(std::make_pair(a + n,b + n)); + Flag[a] = Flag[b] = true; + } + std::fclose(stdin); +} + +inline void Print (void) +{ + std::freopen("party.out","w",stdout); + std::printf("%d\n",static_cast(Result.size())); + for (auto it : Result) + std::printf("%d\n",it); + std::fclose(stdout); +} + +inline int Non (const int X) +{ + if (X <= n) + return X + n; + return X - n; +} + +inline void Build (void) +{ + for (auto it : Exp) + { + Graph[Non(it.first)].push_back(it.second); + Graph[Non(it.second)].push_back(it.first); + Transpose[it.second].push_back(Non(it.first)); + Transpose[it.first].push_back(Non(it.second)); + } +} + +void Toposort (int node) +{ + for (auto it : Graph[node]) + if (!Mark[it]) + { + Mark[it] = true; + Toposort(it); + } + Sorted.push_front(node); +} + +void DepthFirstSearch (int node) +{ + Mark[node] = true; + Value[Non(node)] = true; + for (auto it : Transpose[node]) + if (!Mark[it]) + DepthFirstSearch(it); +} + +inline void Kosaraju (void) +{ + for (int i(1), end(n << 1) ; i <= end ; ++i) + if (!Mark[i] && Flag[i]) + { + Mark[i] = true; + Toposort(i); + } + Mark.reset(); + for (auto it : Sorted) + if (!(Value[it] || Value[Non(it)])) + DepthFirstSearch(it); +} + +inline void Sat (void) +{ + Build(); + Kosaraju(); +} + +inline void Count (void) +{ + for (int i(1) ; i <= n ; ++i) + if (Value[i] || !Flag[i]) + Result.push_back(i); +} + +int main (void) +{ + Read(); + Sat(); + Count(); + Print(); + return 0; +} diff --git a/party/2sat_implication_reduction/d943a7c4-eb7f-473a-a1b2-6fa0baeec03a.cpp b/party/2sat_implication_reduction/d943a7c4-eb7f-473a-a1b2-6fa0baeec03a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..986493fb684c691c9b1efddaae4ea3513b5af6a7 --- /dev/null +++ b/party/2sat_implication_reduction/d943a7c4-eb7f-473a-a1b2-6fa0baeec03a.cpp @@ -0,0 +1,99 @@ +#include +#include + +using namespace std; + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + +vector G[210], T[210]; + +int non ( int x ) +{ + if( x <= n ) return x+n; + return x - n ; +} + +void sortaret ( int nod ) +{ + int N = G[nod].size(),i; + + viz[nod] = 1 ; + + for( i = 0 ; i < N ; i++ ) + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + S[--nn] = nod ; +} + +void dfs ( int nod ) +{ + int N = T[nod].size(),i; + + viz[nod] = 0 ; + + sol[ non(nod) ] = 1 ; + if( nod > n ) invitati++; + + for( i = 0 ; i < N ; i++ ) + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; +} + +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for( i = 1 ; i <= m ; i++ ) + { + scanf("%d %d %d",&a,&b,&tip); + + if( tip == 0 ) + { + G[non(a)].push_back(b); + G[non(b)].push_back(a); + T[a].push_back(non(b)); + T[b].push_back(non(a)); + } + else if( tip == 1 ) + { + G[non(a)].push_back(non(b)); + G[b].push_back(a); + + T[non(b)].push_back(non(a)); + T[a].push_back(b); + } + else if( tip == 2 ) + { + G[non(b)].push_back(non(a)); + G[a].push_back(b); + + T[non(a)].push_back(non(b)); + T[b].push_back(a); + } + else + { + G[a].push_back(non(b)); + G[b].push_back(non(a)); + T[non(a)].push_back(b); + T[non(b)].push_back(a); + } + } + + nn = n + n + 1 ; + + for( i = 1 ; i <= n+n ; i++ ) + if( !viz[i]) sortaret(i); + + for( i = 1 ; i <= n+n ; i++ ) + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + dfs( S[i] ) ; + + printf("%d\n",invitati); + + for( i = 1 ; i <= n ; i++ ) + if( sol[i] ) printf("%d\n",i); + + return 0; +} diff --git a/party/2sat_implication_reduction/dc0ee683-0b54-11ec-96ad-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/dc0ee683-0b54-11ec-96ad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30d7d499910a9cbdef9d504cdfdb203660f9ecd4 --- /dev/null +++ b/party/2sat_implication_reduction/dc0ee683-0b54-11ec-96ad-2cf05d84bfc2.cpp @@ -0,0 +1,215 @@ +#include +#include +#include +#include +#include +using namespace std; +vector G1[210], G2[210], Now, Top; + +vector > V; + +int N, M, A, B, C, D, Tip, ans[210], ctc[210]; + +bool Used[210]; + + + +int Oposite(int X) + +{ + + if(X <= N) return X + N; + + return X - N; + +} + + + +void DFS1(int node) + +{ + + Used[node] = 1; + + for(vector :: iterator it = G1[node].begin(); it != G1[node].end(); ++it) + + if(!Used[*it]) + + DFS1(*it); + + Top.push_back(node); + +} + + + +void DFS2(int node, int comp) + +{ + + ctc[node] = comp; + + Now.push_back(node); + + for(vector :: iterator it = G2[node].begin(); it != G2[node].end(); ++it) + + if(!ctc[*it]) + + DFS2(*it, comp); + +} + + + +void Kosaraju() + +{ + + for(int i = 1; i <= 2 * N; i++) + + if(!Used[i]) + + DFS1(i); + + int cnt = 1; + + while(Top.size()) + + { + + if(!ctc[Top.back()]) + + { + + Now.clear(); + + DFS2(Top.back(), cnt); + + cnt ++; + + V.push_back(Now); + + } + + Top.pop_back(); + + } + +} + + + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + int i; + + scanf("%i %i", &N, &M); + + for(i = 1; i <= M; i++) + + { + + scanf("%i %i %i", &A, &B, &Tip); + + if(Tip == 0) + + { + + C = Oposite(A); + + D = Oposite(B); + + } + + if(Tip == 1) + + { + + C = Oposite(A); + + D = B; + + B = Oposite(B); + + } + + if(Tip == 2) + + { + + C = A; + + D = Oposite(B); + + A = Oposite(A); + + } + + if(Tip == 3) + + { + + C = A; + + D = B; + + A = Oposite(A); + + B = Oposite(B); + + } + + G1[C].push_back(B); + + G1[D].push_back(A); + + G2[A].push_back(D); + + G2[B].push_back(C); + + } + + Kosaraju(); + + for(i = 0; i < V.size(); i++) + + { + + if(!ans[i + 1]) ans[i + 1] = 2; + + for(vector :: iterator it = V[i].begin(); it != V[i].end(); ++it) + + if(!ans[ctc[Oposite(*it)]]) + + ans[ctc[Oposite(*it)]] = 3 - ans[i + 1]; + + } + + int cnt = 0; + + for(i = 1; i <= N; i++) + + if(ans[ctc[i]] == 1) + + cnt ++; + + printf("%i\n", cnt); + + for(i = 1; i <= N; i++) + + if(ans[ctc[i]] == 1) + + printf("%i\n", i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/e175c604-0b51-11ec-ad05-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/e175c604-0b51-11ec-ad05-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c106365647f079a4d24b769267b8a04eda76ac9 --- /dev/null +++ b/party/2sat_implication_reduction/e175c604-0b51-11ec-ad05-2cf05d84bfc2.cpp @@ -0,0 +1,236 @@ +#include +#include +#include +#include + + + + + + +using namespace std; +vector< int > G[256],GT[256]; + +stack< int > Q; + +bool viz[256],SOL[256]; + +int N,M,ANS; + + + +inline int mnot( int value ) + +{ + + return value<=N ? value+N : value-N; + +} + + + +inline void push_edge( int node1, int node2 ) + +{ + + G[node1].push_back( node2 ); + + GT[node2].push_back( node1 ); + +} + + + +void df_trav( int node ) + +{ + + viz[node]=1; + + + + unsigned int i,v; + + for(i=0; i +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + + + + + +int n, m ; + +vector graf[201], trans[201] ; + +bool sel[201] ; + + + +bool sol[201] ; + +int nrinv ; + + + +int lista[201], nr ; + + + +int functie(int x) + +{ + + if( x <= n ) + + return x + n ; + + return x - n ; + +} + + + +void citire_si_initializare() + +{ + + fin >> n >> m ; + + + + for(int i = 1; i <= m; ++i ) + + { + + int a, b, tip ; + + + + fin >> a >> b >> tip ; + + + + if( tip == 0 ) + + { + + graf[ functie(a) ].push_back(b) ; + + graf[ functie(b) ].push_back(a) ; + + + + trans[a].push_back( functie(b) ) ; + + trans[b].push_back( functie(a) ) ; + + + + continue ; + + } + + + + if( tip == 1 ) + + { + + graf[ functie(a) ].push_back( functie(b) ) ; + + graf[b].push_back(a) ; + + + + trans[ functie(b) ].push_back( functie(a) ) ; + + trans[a].push_back(b) ; + + + + continue ; + + } + + + + if( tip == 2 ) + + { + + graf[ functie(b) ].push_back( functie(a) ) ; + + graf[a].push_back(b) ; + + + + trans[ functie(a) ].push_back( functie(b) ); + + trans[b].push_back(a) ; + + + + continue ; + + } + + + + if( tip == 3 ) + + { + + graf[a].push_back( functie(b) ) ; + + graf[b].push_back( functie(a) ) ; + + + + trans[ functie(a) ].push_back(b) ; + + trans[ functie(b) ].push_back(a) ; + + + + continue ; + + } + + } + +} + + + +void sortare_topologica(int nod) + +{ + + sel[nod] = true ; + + + + for(size_t i = 0; i < graf[nod].size(); ++i ) + + if( sel[ graf[nod][i] ] == false ) + + sortare_topologica( graf[nod][i] ) ; + + + + lista[ --nr ] = nod ; + +} + + + +void dfs(int nod) + +{ + + sel[nod] = false ; + + + + sol[ functie(nod) ] = true ; + + + + if( nod > n ) + + ++nrinv ; + + + + for(size_t i = 0; i < trans[nod].size(); ++i ) + + if( sel[ trans[nod][i] ] == true ) + + dfs( trans[nod][i] ) ; + +} + + + +void rezolvare() + +{ + + nr = 2 * n + 1 ; + + int lim = 2 * n ; + + + + for(int i = 1 ;i <= lim; ++i ) + + if( sel[i] == false ) + + sortare_topologica(i) ; + + + + for(int i = 1; i <= lim; ++i ) + + if( sel[ lista[i] ] == true && sel[ functie( lista[i] ) ] == true ) + + dfs( lista[i] ) ; + +} + + + +void afisare() + +{ + + fout << nrinv << "\n" ; + + + + for(int i = 1 ;i <= n ; ++i ) + + if( sol[i] == true ) + + fout << i << "\n" ; + +} + + + +int main() + +{ + + citire_si_initializare() ; + + + + rezolvare() ; + + + + afisare() ; + + + + return 0 ; + +} diff --git a/party/2sat_implication_reduction/e5be7383-082d-47a3-bce4-1a98e0588266.cpp b/party/2sat_implication_reduction/e5be7383-082d-47a3-bce4-1a98e0588266.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/e5be7383-082d-47a3-bce4-1a98e0588266.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/e60e0758-d28a-4be3-9250-b3c4041d6f76.cpp b/party/2sat_implication_reduction/e60e0758-d28a-4be3-9250-b3c4041d6f76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..696dab1188fb69221ce5924b91a98ceed2769d6c --- /dev/null +++ b/party/2sat_implication_reduction/e60e0758-d28a-4be3-9250-b3c4041d6f76.cpp @@ -0,0 +1,200 @@ +#include +#include +#include + + +const int MAX_N = 1000; + + + +int n; + +std::vector g[1 + 2 * MAX_N]; + +std::vector trans[1 + 2 * MAX_N]; + +std::vector topo; + +std::vector sol; + +bool viz[1 + 2 * MAX_N]; + +bool val[1 + 2 * MAX_N]; + +bool possible = true; + + + +int conj(int x) { + + if (x <= n) + + return x + n; + + return x - n; + +} + + + +void make_edge(int u, int v) { + + g[u].push_back(v); + + trans[v].push_back(u); + +} + + + +void topoSort(int u) { + + viz[u] = true; + + for (auto v : g[u]) { + + if (!viz[v]) + + topoSort(v); + + } + + topo.push_back(u); + +} + + + +void markNodes(int u) { + + viz[u] = true; + + if (val[u]) { + + possible = false; + + return; + + } + + val[u] = false; + + val[conj(u)] = true; + + for (auto v : trans[u]) { + + if (!viz[v]) + + markNodes(v); + + } + +} + + + +int main() { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + int m; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; i++) { + + int x, y, restriction; + + scanf("%d%d%d", &x, &y, &restriction); + + switch (restriction) { + + case 0: + + make_edge(conj(x), y); + + make_edge(conj(y), x); + + break; + + case 1: + + make_edge(conj(x), conj(y)); + + make_edge(y, x); + + break; + + case 2: + + make_edge(conj(y), conj(x)); + + make_edge(x, y); + + break; + + case 3: + + make_edge(x, conj(y)); + + make_edge(y, conj(x)); + + break; + + } + + } + + for (int i = 1; i <= 2 * n; i++) { + + if (!viz[i]) + + topoSort(i); + + } + + std::reverse(topo.begin(), topo.end()); + + for (int i = 1; i <= 2 * n; i++) + + viz[i] = false; + + for (auto node : topo) { + + if (!viz[node] && !viz[conj(node)]) + + markNodes(node); + + if (!possible) { + + printf("-1\n"); + + return 0; + + } + + } + + for (int i = 1; i <= n; i++) { + + if (val[i]) + + sol.push_back(i); + + } + + printf("%d\n", sol.size()); + + for (auto node : sol) + + printf("%d\n", node); + + printf("\n"); + + return 0; + +} diff --git a/party/2sat_implication_reduction/e6acdd48-19f3-41d5-9318-97c994620364.cpp b/party/2sat_implication_reduction/e6acdd48-19f3-41d5-9318-97c994620364.cpp new file mode 100644 index 0000000000000000000000000000000000000000..feedf7527c2d591f6f4ee3e5ed34cc80bd35c6cf --- /dev/null +++ b/party/2sat_implication_reduction/e6acdd48-19f3-41d5-9318-97c994620364.cpp @@ -0,0 +1,202 @@ +#include +#include + + + + +using namespace std; + + + + + +vector v[210]; + +vector vComp[210]; + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + for(j = 0; j < vComp[i].size(); ++j) + + if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + t[i] = ++ult; + +} + + + +void comp_tare(int i) + +{ + + int j; + + (viz + N)[i] = 1; + + stiva[++stivaLast] = i; + + for(j = 0; j < (v + N)[i].size(); ++j) + + if(!(viz + N)[ (v + N)[i][j] ]) + + { + + (niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + comp_tare((v + N)[i][j]); + + if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + } + + else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + if((niv + N)[i] == (nivmin + N)[i]) + + { + + (comp + N)[i] = ++ncomp; + + for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + (comp + N)[stiva[j]] = ncomp; + + stivaLast = j - 1; + + } + +} + +void add(int x, int y, int t) + +{ + + if(t == 0) + + { + + (v + N)[-x].push_back(y); + + (v + N)[-y].push_back(x); + + } + + else if(t == 1) (v + N)[-x].push_back(-y); + + else if(t == 2) (v + N)[-y].push_back(-x); + + else if(t == 3) + + { + + (v + N)[x].push_back(-y); + + (v + N)[y].push_back(-x); + + } + +} + + + +int main() + +{ + + int i, j, x, y, tt; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &N, &M); + + for(i = 1; i <= M; ++i) + + { + + scanf("%d %d %d", &x, &y, &tt); + + add(x, y, tt); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else if(!(viz + N)[i]) + + { + + (niv + N)[i] = (nivmin + N)[i] = 1; + + comp_tare(i); + + } + + + + for(i = -N; i <= N; ++i) + + if(i == 0) continue; + + else for(j = 0; j < (v + N)[i].size(); ++j) + + if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + for(i = 1; i <= ncomp; ++i) + + if(!(viz + N)[i]) sortareTopologica(i); + + + + for(i = 1; i <= N; ++i) + + { + + if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + else val[i] = 0; + + } + + printf("%d\n", nInv); + + for(i = 1; i <= N; ++i) + + if(val[i]) printf("%d\n", i); + + return 0; + + + +} diff --git a/party/2sat_implication_reduction/e9822ee5-3524-4e6a-9a91-0236fdf46459.cpp b/party/2sat_implication_reduction/e9822ee5-3524-4e6a-9a91-0236fdf46459.cpp new file mode 100644 index 0000000000000000000000000000000000000000..12c9a873bf2a054dcfdb6e87f751036f4330b26f --- /dev/null +++ b/party/2sat_implication_reduction/e9822ee5-3524-4e6a-9a91-0236fdf46459.cpp @@ -0,0 +1,372 @@ +#include +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/ea736edf-cbbd-4e45-9ba0-85c599eff6a5.cpp b/party/2sat_implication_reduction/ea736edf-cbbd-4e45-9ba0-85c599eff6a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/ea736edf-cbbd-4e45-9ba0-85c599eff6a5.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< +#include +#include +#include +using namespace std; + + + +const int N = 101; + +const int a[] = {1, 1, -1, -1}; + +const int b[] = {1, -1, 1, -1}; + + + +class Sat{ + + vector etc1[2 * N], *graph; + + vector etc2[2 * N], *trans; + + int etc3[2 * N], *ctc, size; + + bool etc4[2 * N], *use; + + stack S; + + + + void reset(){ + + memset(etc4, false, sizeof(etc4)); + + } + + + + void unalloc(vector& v){ + + vector a; + + a.swap(v); + + } + + + + void dfs(int x){ + + use[x] = true; + + + + for (vector :: iterator it = trans[x].begin() ; it != trans[x].end() ; it++) + + if (!use[*it]) + + dfs(*it); + + + + S.push(x); + + } + + + + void dfs(int x, int C){ + + ctc[x] = C; + + use[x] = true; + + + + for (vector :: iterator it = graph[x].begin() ; it != graph[x].end() ; it++) + + if (!use[*it]) + + dfs(*it, C); + + } + + + + void get_ctc(){ + + int nr = 0; + + + + reset(); + + + + for (int i = -size ; i <= size ; i++) + + if (!use[i]) + + dfs(i); + + + + reset(); + + + + while (!S.empty()){ + + if (!use[S.top()]) + + dfs(S.top(), ++nr); + + + + S.pop(); + + } + + } + +public: + + Sat(){ + + graph = etc1 + N; + + trans = etc2 + N; + + ctc = etc3 + N; + + use = etc4 + N; + + } + + + + void push(int x, int y){ + + graph[-x].push_back(y); + + graph[-y].push_back(x); + + + + trans[x].push_back(-y); + + trans[y].push_back(-x); + + } + + + + vector get_sat(int n){ + + size = n; + + + + get_ctc(); + + + + vector v; + + + + for (int i = 1 ; i <= size ; i++) + + if (ctc[i] == ctc[-i]){ + + v.clear(); + + v.push_back(-1); + + return v; + + } else + + v.push_back(ctc[i] < ctc[-i]); + + return v; + + } + +}; + + + +Sat S; + +int ans[N]; + +vector v; + + + +ifstream in("party.in"); + +ofstream out("party.out"); + + + +int main(){ + + int n, m, x, y, z; + + + + in >> n >> m; + + + + while (m--){ + + in >> x >> y >> z; + + + + S.push(x * a[z], y * b[z]); + + } + + + + v = S.get_sat(n); + + + + for (int i = 0 ; i < v.size() ; i++) + + if (v[i]) + + ans[++ans[0]] = i + 1; + + + + for (int i = 0 ; i <= ans[0] ; i++) + + out << ans[i] << "\n"; + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/ed1ae324-0b53-11ec-9715-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/ed1ae324-0b53-11ec-9715-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e423a39e607b168e4bb425ec7b33876c4e3c83f9 --- /dev/null +++ b/party/2sat_implication_reduction/ed1ae324-0b53-11ec-9715-2cf05d84bfc2.cpp @@ -0,0 +1,236 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int M,N; + +vector G[1005],GT[1005]; + +int O[1005],Solution[1005],K; + +bool Use[1005]; + +inline int getNode(int x) + +{ + + if(x<0) + + return N-x; + + return x; + +} + +inline int nod_negat(int x) + +{ + + if(x<=N) + + return x+N; + + else + + return x-N; + +} + +void fillGraph(int x,int y) + +{ + + GT[x].push_back(nod_negat(y)); + + GT[y].push_back(nod_negat(x)); + + G[nod_negat(x)].push_back(y); + + G[nod_negat(y)].push_back(x); + +} + +void cit() + +{ + + int i,type; + + f>>N>>M; + + for(i=1;i<=M;i++) + + { + + int x,y; + + f>>x>>y>>type; + + if(type==1) + + y*=-1; + + if(type==2) + + x*=-1; + + if(type==3) + + { + + x*=-1; + + y*=-1; + + } + + x=getNode(x); + + y=getNode(y); + + fillGraph(x,y); + + } + +} + +void DFP(int Nod) + +{ + + Use[Nod]=1; + + for(unsigned int i=0;i=1;i--) + + { + + if(!Use[O[i]] && !Use[nod_negat(O[i])]) + + DFM(O[i]); + + } + +} + +void Print() + +{ + + int i; + + int result=0; + + if(Solution[0]==-1) + + { + + g<<0<<"\n"; + + return; + + } + + for(i=1;i<=N;i++) + + if(Solution[i]==1) + + result++; + + g< +#include + + +using namespace std; + + + +int n,m,i,a,b,tip,sol[210],invitati,viz[210],S[210],nn ; + + + +vector G[210], T[210]; + + + +int non ( int x ) + +{ + + if( x <= n ) return x+n; + + return x - n ; + +} + + + +void sortaret ( int nod ) + +{ + + int N = G[nod].size(),i; + + + + viz[nod] = 1 ; + + + + for( i = 0 ; i < N ; i++ ) + + if( !viz[ G[nod][i] ] ) sortaret( G[nod][i] ) ; + + + + S[--nn] = nod ; + +} + + + +void dfs ( int nod ) + +{ + + int N = T[nod].size(),i; + + + + viz[nod] = 0 ; + + + + sol[ non(nod) ] = 1 ; + + if( nod > n ) invitati++; + + + + for( i = 0 ; i < N ; i++ ) + + if( viz[ T[nod][i] ] ) dfs( T[nod][i] ) ; + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d",&n,&m); + + + + for( i = 1 ; i <= m ; i++ ) + + { + + scanf("%d %d %d",&a,&b,&tip); + + + + if( tip == 0 ) + + { + + G[non(a)].push_back(b); + + G[non(b)].push_back(a); + + T[a].push_back(non(b)); + + T[b].push_back(non(a)); + + } + + else if( tip == 1 ) + + { + + G[non(a)].push_back(non(b)); + + G[b].push_back(a); + + + + T[non(b)].push_back(non(a)); + + T[a].push_back(b); + + } + + else if( tip == 2 ) + + { + + G[non(b)].push_back(non(a)); + + G[a].push_back(b); + + + + T[non(a)].push_back(non(b)); + + T[b].push_back(a); + + } + + else + + { + + G[a].push_back(non(b)); + + G[b].push_back(non(a)); + + T[non(a)].push_back(b); + + T[non(b)].push_back(a); + + } + + } + + + + nn = n + n + 1 ; + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( !viz[i]) sortaret(i); + + + + for( i = 1 ; i <= n+n ; i++ ) + + if( viz[ S[i] ] && viz[ non(S[i]) ] ) + + dfs( S[i] ) ; + + + + printf("%d\n",invitati); + + + + for( i = 1 ; i <= n ; i++ ) + + if( sol[i] ) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_implication_reduction/efac12b5-4044-44fd-9919-c96ea4cc1e57.cpp b/party/2sat_implication_reduction/efac12b5-4044-44fd-9919-c96ea4cc1e57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..50d30fc4771d5dce163e5f18f246e35c86b49cbf --- /dev/null +++ b/party/2sat_implication_reduction/efac12b5-4044-44fd-9919-c96ea4cc1e57.cpp @@ -0,0 +1,269 @@ +#include +#include + + + + + + +using namespace std; + + + +int n,m,x,y,o,i,j,timp,ma[2*105][2*105],a[2*105][2*105],viz[2*105]; + +int pot[2*105][2*105],bun[2*105][2*105],cate,scc[2*105][2*105],st[2*105]; + +int val[2*105],ma1[2*105][2*105]; + +int stiv[2*105]; + + + +int negat(int x) { + + if(x>n) return x-n; + + else return x+n; + +} + + + +void bfs(int nod) { + + for(int j=1;j<=2*105;j++) viz[j]=0; + + int q=1,p=1; + + stiv[1]=nod; + + viz[1]=1; + + while(q<=p) { + + for(int i=1;i<=a[stiv[q]][0];i++) { + + int n1=a[stiv[q]][i]; + + if(!viz[n1]) { + + pot[nod][++pot[nod][0]]=n1; + + bun[nod][n1]=1; + + stiv[++p]=n1; + + viz[n1]=1; + + } + + } + + q++; + + } + +} + + + +void ca_viz(int nod) { + + viz[nod]=1; + + for(int i=1;i<=ma1[nod][0];i++) { + + if(!viz[ma1[nod][i]]) ca_viz(ma1[nod][i]); + + } + + st[++st[0]]=nod; + +} + + + +void sortare_topologica() { + + for(int i=1;i<=cate;i++) viz[i]=0; + + for(int i=1;i<=cate;i++) + + if(!viz[i]) ca_viz(i); + +} + + + +int main() { + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d\n",&n,&m); + + for(int i=1;i<=m;i++) { + + scanf("%d %d %d\n",&x,&y,&o); + + if(o==0) { + + ma[negat(x)][y]=1; + + ma[negat(y)][x]=1; + + a[negat(x)][++a[negat(x)][0]]=y; + + a[negat(y)][++a[negat(y)][0]]=x; + + } + + if(o==1) { + + ma[negat(x)][negat(y)]=1; + + ma[y][x]=1; + + a[negat(x)][++a[negat(x)][0]]=negat(y); + + a[y][++a[y][0]]=x; + + } + + if(o==2) { + + ma[x][y]=1; + + ma[negat(y)][negat(x)]=1; + + a[negat(y)][++a[negat(y)][0]]=negat(x); + + a[x][++a[x][0]]=y; + + } + + if(o==3) { + + ma[x][negat(y)]=1; + + ma[y][negat(x)]=1; + + a[x][++a[x][0]]=negat(y); + + a[y][++a[y][0]]=negat(x); + + } + + } + + for(int i=1;i<=2*n;i++) bfs(i); + + + + cate=0; + + for(int i=1;i<=2*n;i++) { + + int gata=0; + + for(int j=1;j<=cate;j++) + + if((bun[scc[j][1]][i]) && (bun[i][scc[j][1]])) { + + scc[j][++scc[j][0]]=i; + + gata=1; + + break; + + } + + if(!gata) { + + cate++; + + scc[cate][++scc[cate][0]]=i; + + } + + } + + for(int i=1;i<=cate;i++) + + for(int j=1;j<=cate;j++) + + if(i!=j) { + + int bagam=0; + + for(int k=1;k<=scc[i][0];k++) { + + for(int k1=1;k1<=scc[j][0];k1++) + + if(ma[scc[i][k]][scc[j][k1]]) { + + bagam=1; + + break; + + } + + if(bagam) break; + + } + + if(bagam) + + ma1[i][++ma1[i][0]]=j; + + } + + + + sortare_topologica(); + + + + for(int i=1;i<=st[0];i++) { + + int nod=st[i]; + + for(int j=1;j<=scc[st[i]][0];j++) { + + nod=scc[st[i]][j]; + + if(val[negat(nod)]==0) { + + val[nod]=1; + + val[negat(nod)]=2; + + } + + } + + } + + int cate1=0; + + for(int i=1;i<=n;i++) if(val[i]==1) cate1++; + + if(cate1==0) { + + printf("%d\n",n); + + for(int i=1;i<=n;i++) printf("%d\n",i); + + return 0; + + } + + printf("%d\n",cate1); + + for(int i=1;i<=n;i++) if(val[i]==1) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/f1546cef-0b54-11ec-aae3-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/f1546cef-0b54-11ec-aae3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b32ad1f4711acdbe278558b79c50dd79a0a6d45f --- /dev/null +++ b/party/2sat_implication_reduction/f1546cef-0b54-11ec-aae3-2cf05d84bfc2.cpp @@ -0,0 +1,256 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +vector G[200005], GT[200005]; + +int N, TSort[200005], Solution[200005]; + +bool Used[200005]; + + + +inline int Non (int X) + +{ + + if (X<=N) + + { + + return X+N; + + } + + return X-N; + +} + + + +void Read () + +{ + + freopen ("party.in", "r", stdin); + + int M; + + scanf ("%d %d", &N, &M); + + for (; M>0; --M) + + { + + int X, Y, Type; + + scanf ("%d %d %d", &X, &Y, &Type); + + if (Type==0) + + { + + G[Non (X)].push_back (Y); + + G[Non (Y)].push_back (X); + + GT[X].push_back (Non (Y)); + + GT[Y].push_back (Non (X)); + + } + + if (Type==1) + + { + + G[Non (X)].push_back (Non (Y)); + + G[Y].push_back (X); + + GT[Non (Y)].push_back (Non (X)); + + GT[X].push_back (Y); + + } + + if (Type==2) + + { + + G[Non (Y)].push_back (Non (X)); + + G[X].push_back (Y); + + GT[Non (X)].push_back (Non (Y)); + + GT[Y].push_back (X); + + } + + if (Type==3) + + { + + G[X].push_back (Non (Y)); + + G[Y].push_back (Non (X)); + + GT[Non (X)].push_back (Y); + + GT[Non (Y)].push_back (X); + + } + + } + +} + + + +void Print () + +{ + + freopen ("party.out", "w", stdout); + + for (int i=1; i<=N; ++i) + + { + + Solution[0]+=Solution[i]; + + } + + printf ("%d\n", Solution[0]); + + for (int i=1; i<=N; ++i) + + { + + if (Solution[i]!=0) + + { + + printf ("%d\n", i); + + } + + } + +} + + + +inline void DFP (int X) + +{ + + Used[X]=true; + + for (vector :: iterator V=G[X].begin (); V!=G[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFP (*V); + + } + + } + + TSort[++TSort[0]]=X; + +} + + + +inline void DFM (int X) + +{ + + Used[X]=true; + + Solution[Non (X)]=1; + + for (vector :: iterator V=GT[X].begin (); V!=GT[X].end (); ++V) + + { + + if (!Used[*V]) + + { + + DFM (*V); + + } + + } + +} + + + +void Kosaraju () + +{ + + for (int i=1; i<=N+N; ++i) + + { + + if (!Used[i]) + + { + + DFP (i); + + } + + } + + memset (Used, 0, sizeof (Used)); + + for (int i=TSort[0]; i>0; --i) + + { + + if (!Used[TSort[i]] and !Used[Non (TSort[i])]) + + { + + DFM (TSort[i]); + + } + + } + +} + + + +int main() + +{ + + Read (); + + Kosaraju (); + + Print (); + + return 0; + +} diff --git a/party/2sat_implication_reduction/f1d3163a-0b03-45f6-87bf-fbf3ee0854a2.cpp b/party/2sat_implication_reduction/f1d3163a-0b03-45f6-87bf-fbf3ee0854a2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c1fc47765b4cd0b9a2c8c81fe1e0f79d2a88979 --- /dev/null +++ b/party/2sat_implication_reduction/f1d3163a-0b03-45f6-87bf-fbf3ee0854a2.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +const int kMaxNodes = 205; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +int N; + +vector g[kMaxNodes]; + +vector gt[kMaxNodes]; + + + +bool use[kMaxNodes]; + +stack st; + + + +bitset val; + + + +int Not(int x) { + + if (x > N) return x - N; + + return x + N; + +} + + + +void AddEdge(int x, int y) { + + g[x].push_back(y); + + gt[y].push_back(x); + +} + + + +void Init() { + + int M; + + fin >> N >> M; + + while (M--) { + + int x, y, t; + + fin >> x >> y >> t; + + if (t & 1) y = Not(y); + + if (t & 2) x = Not(x); + + AddEdge(Not(x), y); + + AddEdge(Not(y), x); + + } + +} + + + +void SortDfs(int node) { + + use[node] = true; + + for (int i : g[node]) + + if (!use[i]) + + SortDfs(i); + + st.push(node); + +} + + + +void SolveDfs(int node) { + + use[node] = true; + + if (node > N) + + val[Not(node)] = true; + + for (int i : gt[node]) + + if (!use[i]) + + SolveDfs(i); + +} + + + +void Solve() { + + for (int i = 1; i <= 2 * N; ++i) + + if (!use[i]) + + SortDfs(i); + + memset(use, 0, sizeof use); + + while (!st.empty()) { + + int node = st.top(); + + st.pop(); + + if (!use[node] && !use[Not(node)]) + + SolveDfs(node); + + } + +} + + + +void Print() { + + fout << val.count() << "\n"; + + for (int i = 1; i <= N; ++i) + + if (val[i]) + + fout << i << "\n"; + +} + + + +int main() { + + Init(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/party/2sat_implication_reduction/f379d178-0b52-11ec-90dd-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/f379d178-0b52-11ec-90dd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bed420c2f07b8a8b4667e0122c31d3caeac4fbe9 --- /dev/null +++ b/party/2sat_implication_reduction/f379d178-0b52-11ec-90dd-2cf05d84bfc2.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +using namespace std; + +const int maxval = 105; + +vector G[2 * maxval], Gt[2 * maxval]; +bitset<2 * maxval> V; +int graph[2 * maxval], Cc[2 * maxval]; + +int N, P, Ncc; + +inline int Non(const int x) +{ + return (x > N ? x - N: x + N); +} + +void DFSSS(const int node) +{ + V[node] = 1; + for (const int next: G[node]) + if (!V[next]) + DFSSS(next); + + graph[++P] = node; +} + +void dfsTRAV(const int node) +{ + Cc[node] = Ncc; + for (const int next: Gt[node]) + if (!Cc[next]) + dfsTRAV(next); +} + +int main() +{ + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int M; + scanf("%d%d", &N, &M); + + while (M--) + { + int x, y, op; + scanf("%d%d%d", &x, &y, &op); + + if (op == 1) y = Non(y); + else if (op == 2) x = Non(x); + else if (op == 3) + { + x = Non(x); + y = Non(y); + } + + G[Non(x)].push_back(y); + G[Non(y)].push_back(x); + Gt[x].push_back(Non(y)); + Gt[y].push_back(Non(x)); + } + + for (int i = 1; i <= 2 * N; i++) + if (!V[i]) + DFSSS(i); + + for (int i = 2 * N; i; i--) + { + const int node = graph[i]; + + if (!Cc[node]) + { + Ncc++; + dfsTRAV(node); + } + } + + vector Ans; + for (int i = 1; i <= N; i++) + if (Cc[i] > Cc[Non(i)]) + Ans.push_back(i); + + printf("%u\n", Ans.size()); + for (const int p: Ans) printf("%d\n", p); +} diff --git a/party/2sat_implication_reduction/f5572631-0b54-11ec-a95c-2cf05d84bfc2.cpp b/party/2sat_implication_reduction/f5572631-0b54-11ec-a95c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ddf645108cf80d9e52ddf74e196ba8b025023be --- /dev/null +++ b/party/2sat_implication_reduction/f5572631-0b54-11ec-a95c-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +typedef struct celula { + + int nod; + + celula *next; + + }*lista; + +lista graf[205],v,tgraf[205]; + +int x,y,op,n,m,i,stiva[205],st,comp[205],nrc,assign[205]; + +bool viz[205]; + + + +int negat(int x) { + + if (x>n) return x-n; + + else return x+n; + +} + + + +void dfs(int nod) { + + viz[nod]=1; + + for (lista p=graf[nod]; p; p=p->next) + + if (viz[p->nod]==0) dfs(p->nod); + + + + ++st; stiva[st]=nod; + + + +} + + + +void dfs2(int nod) { + + comp[nod]=nrc; + + assign[negat(nod)]=1; + + for (lista p=tgraf[nod]; p; p=p->next) + + if (comp[p->nod]==0) dfs2(p->nod); + +} + + + +int main(void) { + + ifstream fin("party.in"); + + ofstream fout("party.out"); + + + + fin>>n>>m; + + + + for (i=1; i<=m; ++i){ + + fin>>x>>y>>op; + + if (op==1) y=negat(y); + + else if (op==2) x=negat(x); + + else if (op==3) { x=negat(x); y=negat(y); } + + + + int xn=negat(x), yn=negat(y); + + + + v=new celula; v->nod=y; v->next=graf[xn]; graf[xn]=v; + + v=new celula; v->nod=x; v->next=graf[yn]; graf[yn]=v; + + + + v=new celula; v->nod=xn; v->next=tgraf[y]; tgraf[y]=v; + + v=new celula; v->nod=yn; v->next=tgraf[x]; tgraf[x]=v; + + } + + + + + + for (i=1; i<=2*n; ++i) + + if (viz[i]==0) dfs(i); + + + + for (i=st; i>=1; --i) + + if ( comp[stiva[i]]==0 && comp[negat(stiva[i])]==0 ) { ++nrc; dfs2(stiva[i]); } + + + + int sol=0; + + for (i=1; i<=n; ++i) sol+=assign[i]; + + fout< +#include + + + + + + +using namespace std; + + + +int n,m,x,y,o,i,j,timp,ma[2*105][2*105],a[2*105][2*105],viz[2*105]; + +int pot[2*105][2*105],bun[2*105][2*105],cate,scc[2*105][2*105],st[2*105]; + +int val[2*105],ma1[2*105][2*105]; + +int stiv[2*105]; + + + +int negat(int x) { + + if(x>n) return x-n; + + else return x+n; + +} + + + +void bfs(int nod) { + + for(int j=1;j<=2*105;j++) viz[j]=0; + + int q=1,p=1; + + stiv[1]=nod; + + viz[1]=1; + + while(q<=p) { + + for(int i=1;i<=a[stiv[q]][0];i++) { + + int n1=a[stiv[q]][i]; + + if(!viz[n1]) { + + pot[nod][++pot[nod][0]]=n1; + + bun[nod][n1]=1; + + stiv[++p]=n1; + + viz[n1]=1; + + } + + } + + q++; + + } + +} + + + +void ca_viz(int nod) { + + viz[nod]=1; + + for(int i=1;i<=ma1[nod][0];i++) { + + if(!viz[ma1[nod][i]]) ca_viz(ma1[nod][i]); + + } + + st[++st[0]]=nod; + +} + + + +void sortare_topologica() { + + for(int i=1;i<=cate;i++) viz[i]=0; + + for(int i=1;i<=cate;i++) + + if(!viz[i]) ca_viz(i); + +} + + + +int main() { + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d\n",&n,&m); + + for(int i=1;i<=m;i++) { + + scanf("%d %d %d\n",&x,&y,&o); + + if(o==0) { + + ma[negat(x)][y]=1; + + ma[negat(y)][x]=1; + + a[negat(x)][++a[negat(x)][0]]=y; + + a[negat(y)][++a[negat(y)][0]]=x; + + } + + if(o==1) { + + ma[negat(x)][negat(y)]=1; + + ma[y][x]=1; + + a[negat(x)][++a[negat(x)][0]]=negat(y); + + a[y][++a[y][0]]=x; + + } + + if(o==2) { + + ma[x][y]=1; + + ma[negat(y)][negat(x)]=1; + + a[negat(y)][++a[negat(y)][0]]=negat(x); + + a[x][++a[x][0]]=y; + + } + + if(o==3) { + + ma[x][negat(y)]=1; + + ma[y][negat(x)]=1; + + a[x][++a[x][0]]=negat(y); + + a[y][++a[y][0]]=negat(x); + + } + + } + + for(int i=1;i<=2*n;i++) bfs(i); + + + + cate=0; + + for(int i=1;i<=2*n;i++) { + + int gata=0; + + for(int j=1;j<=cate;j++) + + if((bun[scc[j][1]][i]) && (bun[i][scc[j][1]])) { + + scc[j][++scc[j][0]]=i; + + gata=1; + + break; + + } + + if(!gata) { + + cate++; + + scc[cate][++scc[cate][0]]=i; + + } + + } + + for(int i=1;i<=cate;i++) + + for(int j=1;j<=cate;j++) + + if(i!=j) { + + int bagam=0; + + for(int k=1;k<=scc[i][0];k++) { + + for(int k1=1;k1<=scc[j][0];k1++) + + if(ma[scc[i][k]][scc[j][k1]]) { + + bagam=1; + + break; + + } + + if(bagam) break; + + } + + if(bagam) + + ma1[i][++ma1[i][0]]=j; + + } + + + + sortare_topologica(); + + + + for(int i=1;i<=st[0];i++) { + + int nod=st[i]; + + for(int j=1;j<=scc[st[i]][0];j++) { + + nod=scc[st[i]][j]; + + if(val[negat(nod)]==0) { + + val[nod]=1; + + val[negat(nod)]=2; + + } + + } + + } + + int cate1=0; + + for(int i=1;i<=n;i++) if(val[i]==1) cate1++; + + if(cate1==0) { + + printf("%d\n",n); + + for(int i=1;i<=n;i++) printf("%d\n",i); + + return 0; + + } + + printf("%d\n",cate1); + + for(int i=1;i<=n;i++) if(val[i]==1) printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_implication_reduction/fb6e982c-73c0-41d4-a8a0-a4b8aebeb903.cpp b/party/2sat_implication_reduction/fb6e982c-73c0-41d4-a8a0-a4b8aebeb903.cpp new file mode 100644 index 0000000000000000000000000000000000000000..098f99a590129e5d8cc70ef62529c9aa13bae215 --- /dev/null +++ b/party/2sat_implication_reduction/fb6e982c-73c0-41d4-a8a0-a4b8aebeb903.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include + + +const int MAX_N(105); +const int MAX_M(1005); + +int n, m; +std::vector> Exp; +std::vector Result; +bool Value [MAX_N << 1]; +std::bitset Mark; +std::deque Sorted; +std::vector Graph [MAX_N << 1]; +std::vector Transpose [MAX_N << 1]; +bool Flag [MAX_N]; + +inline void Read (void) +{ + std::freopen("party.in","r",stdin); + std::scanf("%d %d",&n,&m); + for (int i(1), a, b, t ; i <= m ; ++i) + { + std::scanf("%d %d %d",&a,&b,&t); + if (t == 0) + Exp.push_back(std::make_pair(a,b)); + else if (t == 1) + Exp.push_back(std::make_pair(a,b + n)); + else if (t == 2) + Exp.push_back(std::make_pair(a + n,b)); + else + Exp.push_back(std::make_pair(a + n,b + n)); + Flag[a] = Flag[b] = true; + } + std::fclose(stdin); +} + +inline void Print (void) +{ + std::freopen("party.out","w",stdout); + std::printf("%d\n",static_cast(Result.size())); + for (auto it : Result) + std::printf("%d\n",it); + std::fclose(stdout); +} + +inline int Non (const int X) +{ + if (X <= n) + return X + n; + return X - n; +} + +inline void Build (void) +{ + for (auto it : Exp) + { + Graph[Non(it.first)].push_back(it.second); + Graph[Non(it.second)].push_back(it.first); + Transpose[it.second].push_back(Non(it.first)); + Transpose[it.first].push_back(Non(it.second)); + } +} + +void Toposort (int node) +{ + for (auto it : Graph[node]) + if (!Mark[it]) + { + Mark[it] = true; + Toposort(it); + } + Sorted.push_front(node); +} + +void DepthFirstSearch (int node) +{ + Mark[node] = true; + Value[Non(node)] = true; + for (auto it : Transpose[node]) + if (!Mark[it]) + DepthFirstSearch(it); +} + +inline void Kosaraju (void) +{ + for (int i(1), end(n << 1) ; i <= end ; ++i) + if (!Mark[i] && Flag[i]) + { + Mark[i] = true; + Toposort(i); + } + Mark.reset(); + for (auto it : Sorted) + if (!(Value[it] || Value[Non(it)])) + DepthFirstSearch(it); +} + +inline void Sat (void) +{ + Build(); + Kosaraju(); +} + +inline void Count (void) +{ + for (int i(1) ; i <= n ; ++i) + if (Value[i] || !Flag[i]) + Result.push_back(i); +} + +int main (void) +{ + Read(); + Sat(); + Count(); + Print(); + return 0; +} diff --git a/party/2sat_implication_reduction/fd9f94ba-2a23-43d9-8376-b7180ec1f6f2.cpp b/party/2sat_implication_reduction/fd9f94ba-2a23-43d9-8376-b7180ec1f6f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e43855a94ca60b977c22a8533e22ee5fcd0d85a2 --- /dev/null +++ b/party/2sat_implication_reduction/fd9f94ba-2a23-43d9-8376-b7180ec1f6f2.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + + + +using namespace std; + +ifstream fin("party.in"); + +ofstream fout("party.out"); + +int n,m,a,b,type,viz[205],st[205],top,r[205],rez[205],nr; + +vectorg[205]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:g[nod]) + + if(!viz[i]) + + dfs(i); + + top++; + + st[top]=nod-n; + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>a>>b>>type; + + if(type==1) + + b=-b; + + else + + if(type==2) + + a=-a; + + else + + if(type==3) + + { + + a=-a; + + b=-b; + + } + + g[-a+n].push_back(b+n); + + g[-b+n].push_back(a+n); + + } + + for(int i=2*n;i>=0;i--) + + if(!viz[i]&&g[i].size()) + + { + + dfs(i); + + } + + for(int i=top;i>=1;i--) + + if(!r[st[i]+n]&&!r[-st[i]+n]) + + r[-st[i]+n]=1; + + for(int i=1;i<=n;i++) + + if(r[i+n]||(!r[i+n]&&!r[-i+n])) + + { + + nr++; + + rez[nr]=i; + + } + + fout< +#include + + +using namespace std; + + + +const int NMAX = 100+5; + +const int MMAX = 1000+5; + + + +void Read(),Solve(),Print(); + + + +int N,M,Top,NrCTC; + +vector V[2*NMAX]; + + + +vector W[2*NMAX]; + + + +bool Viz[2*NMAX]; + + + +int TSort[2*NMAX]; + +int Comp[2*NMAX]; + + + +int Opus[2*NMAX]; + +int Sol[NMAX]; + +vector CTC[2*NMAX]; + +vector G[2*NMAX]; + + + +void SortareTopologica(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=(V+NMAX)[nod].begin(); it!=(V+NMAX)[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SortareTopologica(*it); + + } + + TSort[Top--]=nod; + +} + + + +void DFS(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=0; + + for(it=(W+NMAX)[nod].begin(); it!=(W+NMAX)[nod].end(); it++) + + { + + if(!(Viz+NMAX)[*it]) continue; + + DFS(*it); + + } + + CTC[Top].push_back(nod); + + (Comp+NMAX)[nod]=Top; + +} + + + +void ComponenteTareConexe() + +{ + + int i,x; + + + + Top=2*N; + + + + for(i=-N; i<=N; i++) + + { + + if(!i) continue; + + if((Viz+NMAX)[i]) continue; + + SortareTopologica(i); + + } + + + + for(i=1; i<=2*N; i++) + + { + + x=TSort[i]; + + if(!(Viz+NMAX)[x]) continue; + + Top++; + + DFS(x); + + } + + + + NrCTC=Top; + +} + + + +void ConstruiesteGraf() + +{ + + + + + + + + int i,x; + + vector::iterator it,jt; + + + + for(i=1; i<=NrCTC; i++) + + { + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + { + + x=*it; + + for(jt=(V+NMAX)[x].begin(); jt!=(V+NMAX)[x].end(); jt++) + + { + + if((Comp+NMAX)[*jt]==i) continue; + + G[i].push_back((Comp+NMAX)[*jt]); + + } + + } + + } + + + + for(i=1; i<=N; i++) + + { + + Opus[(Comp+NMAX)[i]]=(Comp+NMAX)[-i]; + + Opus[(Comp+NMAX)[-i]]=(Comp+NMAX)[i]; + + } + +} + + + +void SorteazaTopologic(int nod) + +{ + + + + + + vector::iterator it; + + (Viz+NMAX)[nod]=1; + + for(it=G[nod].begin(); it!=G[nod].end(); it++) + + { + + if((Viz+NMAX)[*it]) continue; + + SorteazaTopologic(*it); + + } + + TSort[Top--]=nod; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} + + + +void Read() + +{ + + int x,y,z; + + + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d%d",&N,&M); + + + + for(; M; --M) + + { + + scanf("%d%d%d",&x,&y,&z); + + + + if(z==1) y=-y; + + else if(z==2) x=-x; + + else if(z==3) x=-x,y=-y; + (V+NMAX)[-x].push_back(y); + + (V+NMAX)[-y].push_back(x); + + (W+NMAX)[y].push_back(-x); + + (W+NMAX)[x].push_back(-y); + + } + +} + + + +void Solve() + +{ + + int i; + + vector::iterator it; + + + + ComponenteTareConexe(); + + + + ConstruiesteGraf(); + + + + Top=NrCTC; + + + + for(i=1; i<=NrCTC; i++) + + { + + if((Viz+NMAX)[i]) continue; + + SorteazaTopologic(i); + + } + + + + for(i=1; i<=NrCTC; i++) + + { + + if(!(Viz+NMAX)[i]) continue; + + for(it=CTC[i].begin(); it!=CTC[i].end(); it++) + + if(*it < 0) Sol[-(*it)]=1; + + (Viz+NMAX)[i]=0; + + (Viz+NMAX)[Opus[i]]=0; + + } + +} + + + +void Print() + +{ + + int i,ans=0; + + for(i=1; i<=N; i++) + + ans+=Sol[i]; + + printf("%d\n",ans); + + for(i=1; i<=N; i++) + + if(Sol[i]) printf("%d\n",i); + +} diff --git a/party/2sat_implication_reduction/fe758342-8343-4d84-b543-6797a8e3332e.cpp b/party/2sat_implication_reduction/fe758342-8343-4d84-b543-6797a8e3332e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b67777eeb7340ee4d0f1ee3b35405bf6bf9a88f --- /dev/null +++ b/party/2sat_implication_reduction/fe758342-8343-4d84-b543-6797a8e3332e.cpp @@ -0,0 +1,376 @@ +#include +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_implication_reduction/fec375d3-2d3c-4062-8160-3cc2ccf971f6.cpp b/party/2sat_implication_reduction/fec375d3-2d3c-4062-8160-3cc2ccf971f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b67777eeb7340ee4d0f1ee3b35405bf6bf9a88f --- /dev/null +++ b/party/2sat_implication_reduction/fec375d3-2d3c-4062-8160-3cc2ccf971f6.cpp @@ -0,0 +1,376 @@ +#include +#include +using namespace std; + + + + + + + +vector v[210]; + + + +vector vComp[210]; + + + + + + + +int comp[210], stiva[210], nivmin[210], niv[210], viz[210], t[210], val[210], nInv; +int N, M; + + + +int ncomp, stivaLast = -1, ult = -1; + + + +void sortareTopologica(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +for(j = 0; j < vComp[i].size(); ++j) + + + +if(!(viz + N)[ vComp[i][j] ]) sortareTopologica( vComp[i][j] ); + + + +t[i] = ++ult; + + + +} + + + + + + + +void comp_tare(int i) + + + +{ + + + +int j; + + + +(viz + N)[i] = 1; + + + +stiva[++stivaLast] = i; + + + +for(j = 0; j < (v + N)[i].size(); ++j) + + + +if(!(viz + N)[ (v + N)[i][j] ]) + + + +{ + + + +(niv + N)[ (v + N)[i][j] ] = (nivmin + N)[ (v + N)[i][j] ]= (niv + N)[i] + 1; + + + +comp_tare((v + N)[i][j]); + + + +if((nivmin + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (nivmin + N)[ (v + N)[i][j] ]; + + + +} + + + +else if((niv + N)[ (v + N)[i][j] ] < (nivmin + N)[i]) (nivmin + N)[i] = (niv + N)[ (v + N)[i][j] ]; + + + +if((niv + N)[i] == (nivmin + N)[i]) + + + +{ + + + +(comp + N)[i] = ++ncomp; + + + +for(j = stivaLast; j >= 0 && stiva[j] != i; --j) + + + +(comp + N)[stiva[j]] = ncomp; + + + +stivaLast = j - 1; + + + +} + + + +} + + + +void add(int x, int y, int t) + + + +{ + + + +if(t == 0) + + + +{ + + + +(v + N)[-x].push_back(y); + + + +(v + N)[-y].push_back(x); + + + +} + + + +else if(t == 1) (v + N)[-x].push_back(-y); + + + +else if(t == 2) (v + N)[-y].push_back(-x); + + + +else if(t == 3) + + + +{ + + + +(v + N)[x].push_back(-y); + + + +(v + N)[y].push_back(-x); + + + +} + + + +} + + + + + + + +int main() + + + +{ + + + +int i, j, x, y, tt; + + + + + + + +freopen("party.in", "r", stdin); + + + +freopen("party.out", "w", stdout); + + + + + +scanf("%d %d", &N, &M); + + + +for(i = 1; i <= M; ++i) + + + +{ + + + +scanf("%d %d %d", &x, &y, &tt); + + + +add(x, y, tt); + + + +} + + + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else if(!(viz + N)[i]) + + + +{ + + + +(niv + N)[i] = (nivmin + N)[i] = 1; + + + +comp_tare(i); + + + +} + + + +for(i = -N; i <= N; ++i) + + + +if(i == 0) continue; + + + +else for(j = 0; j < (v + N)[i].size(); ++j) + + + +if((comp + N)[i] != (comp + N)[ (v + N)[i][j] ]) + + + +vComp[ (comp + N)[i] ].push_back((comp + N)[ (v + N)[i][j] ]); + + + + + + + +for(i = 1; i <= ncomp; ++i) (viz + N)[i] = 0; + + + +for(i = 1; i <= ncomp; ++i) + + + +if(!(viz + N)[i]) sortareTopologica(i); + + + + + + + +for(i = 1; i <= N; ++i) + + + +{ + + + +if(t[ (comp + N)[i] ] < t[ (comp + N)[-i] ]) val[i] = 1, ++nInv; + + + +else val[i] = 0; + + + +} + + + +printf("%d\n", nInv); + + + +for(i = 1; i <= N; ++i) + + + +if(val[i]) printf("%d\n", i); + + + +return 0; + + + + + + + +} diff --git a/party/2sat_random/4f4c83e1-02b2-11ec-bc4a-2cf05d84bfc2.cpp b/party/2sat_random/4f4c83e1-02b2-11ec-bc4a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99ec4465e2b2c53e0837bfe3107e33d151aada3a --- /dev/null +++ b/party/2sat_random/4f4c83e1-02b2-11ec-bc4a-2cf05d84bfc2.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +vector >expr; + +int n, m, p; + +int sol[128]; + + + +void citire() { + + fin >> n >> m; + + for(int i = 1; i <= m; i++) { + + int x, y, z; + + fin >> x >> y>> z; + + if(z == 0) + + expr.push_back({x, y}); + + else if (z == 1) + + expr.push_back({x, -y}); + + else if (z == 2) + + expr.push_back({-x, y}); + + else if (z == 3) + + expr.push_back({-x, -y}); + + } + +} + + + +bool eval(pair p) { + + int x = p.first; + + int y = p.second; + + + + return ( (x > 0 ? sol[abs(x)] : !sol[abs(x)]) | (y > 0 ? sol[abs(y)] : !sol[abs(y)]) ); + +} + + + +void afis() { + + int nr = 0; + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + nr++; + + fout << nr << '\n'; + + for(int i = 1; i <= n; i++) + + if(sol[i]) + + fout << i << '\n'; + +} + + + +void solve() { + + srand(time(0)); + + for(int i = 1; i <= n; i++) + + sol[i] = rand()%2; + + + + while(true) { + + bool currRes = 1; + + for(int i = 0; i < expr.size(); i++) { + + currRes &= eval(expr[i]); + + if(!currRes) { + + p = i; + + break; + + } + + } + + + + if(currRes) { + + afis(); + + return; + + } + + + + if(rand()%2) + + sol[abs(expr[p].first)] ^= 1; + + else + + sol[abs(expr[p].second)] ^= 1; + + } + +} + + + +int main() { + + citire(); + + solve(); + +} diff --git a/party/2sat_random/4ff4f8d9-02b2-11ec-aea9-2cf05d84bfc2.cpp b/party/2sat_random/4ff4f8d9-02b2-11ec-aea9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0057b1f356a20c25c53de0f0d85cba406e8e44a7 --- /dev/null +++ b/party/2sat_random/4ff4f8d9-02b2-11ec-aea9-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + +ifstream in("party.in"); + +ofstream out("party.out"); + +int n,m; + +int vine[105],a[1005],b[1005],c[1005]; + +int main() + +{ + + in >> n >> m; + + for(int i=1; i<=n; i++) vine[i]=1; + + for(int i=1; i<=m; i++) + + { + + in >> a[i] >> b[i] >> c[i]; + + } + + int ok=0; + + while(!ok) + + { + + ok=1; + + for(int i=1; i<=m; i++) + + { + + switch(c[i]){ + + case 0: + + if(vine[a[i]]==0 && vine[b[i]]==0) + + { + + ok=0; + + int p = rand()%2; + + vine[a[i]]=p; + + vine[b[i]]=1-p; + + } + + break; + + case 1: + + if(vine[a[i]]==0 && vine[b[i]]==1) + + { + + ok=0; + + vine[b[i]]=0; + + } + + break; + + case 2: + + if(vine[a[i]]==1 && vine[b[i]]==0) + + { + + ok=0; + + vine[a[i]]=0; + + } + + break; + + case 3: + + if(vine[a[i]]==1 && vine[b[i]]==1) + + { + + ok=0; + + int p = rand()%2; + + vine[a[i]]=p; + + vine[b[i]]=1-p; + + } + + break; + + } + + } + + } + + int cnt=0; + + for(int i=1; i<=n; i++) if(vine[i]) cnt++; + + out << cnt << '\n'; + + for(int i=1; i<=n; i++) if(vine[i]) out << i << '\n'; + +} diff --git a/party/2sat_random/5077bd64-02b2-11ec-b21e-2cf05d84bfc2.cpp b/party/2sat_random/5077bd64-02b2-11ec-b21e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..256d652c02631da7dbce4ac05949e8abd2c89540 --- /dev/null +++ b/party/2sat_random/5077bd64-02b2-11ec-b21e-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +const int NMAX = 100 + 5; + +const int MMAX = 1000 + 5; + + + +int N, M; + +int x[2 * NMAX]; + +int a[MMAX], b[MMAX]; + + + +int main() { + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + cin >> N >> M; + + for (int i = 1; i <= M; ++i) { + + cin >> a[i] >> b[i]; + + --a[i], a[i] *= 2; + + --b[i], b[i] *= 2; + + int type; + + cin >> type; + + if (type == 1 || type == 3) + + b[i] ^= 1; + + if (type == 2 || type == 3) + + a[i] ^= 1; + + } + + for (int i = 1; i < 2 * N; i += 2) + + x[i] = true; + + + + while (true) { + + vector clauses; + + for (int i = 1; i <= M; ++i) + + if (!(x[a[i]] || x[b[i]])) + + clauses.push_back(i); + + if (clauses.empty()) + + break; + + const int where = clauses[rand() % clauses.size()]; + + const int pos = rand() % 2 ? a[where] : b[where]; + + x[pos] ^= 1, x[pos ^ 1] ^= 1; + + } + + vector sol; + + for (int i = 0; i < 2 * N; i += 2) + + if (x[i]) + + sol.push_back(i / 2 + 1); + + cout << sol.size() << endl; + + for (int i = 0; i < sol.size(); ++i) + + cout << sol[i] << '\n'; + + return 0; + +} diff --git a/party/2sat_random/50999d49-02b2-11ec-a070-2cf05d84bfc2.cpp b/party/2sat_random/50999d49-02b2-11ec-a070-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4891864186edaa191f8fe1ea3ae55a6516d9cd47 --- /dev/null +++ b/party/2sat_random/50999d49-02b2-11ec-a070-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +using namespace std; + +struct queries { + + int a, b, c; + +}; + +ifstream in("party.in"); + +ofstream out("party.out"); + +vector < queries > q; + +vector < int > ans; + +bool T[105]; + +bool modify() { + + int ok = false; + + for (auto it : q) { + + if (it.c == 0) { + + if (!T[it.a] && !T[it.b]) { + + bool p = rand() % 2; + + T[it.a] = p; + + T[it.b] = !p; + + ok = true; + + } + + } + + if (it.c == 1) { + + if (!T[it.a] && T[it.b]) { + + T[it.b] = 0; + + ok = true; + + } + + } + + if (it.c == 2) { + + if (T[it.a] && !T[it.b]) { + + T[it.a] = 0; + + ok = true; + + } + + } + + if (it.c == 3) { + + if (T[it.a] && T[it.b]) { + + bool p = rand() % 2; + + T[it.a] = p; + + T[it.b] = !p; + + ok = true; + + } + + } + + } + + return ok; + +} + +int main() { + + int n, m; + + in >> n >> m; + + while(m--) { + + int a, b, c; + + in >> a >> b >> c; + + q.push_back({a, b, c}); + + } + + while(modify()); + + for (int i = 1; i <= n; i++) { + + if (T[i]) { + + ans.push_back(i); + + } + + } + + out << ans.size() << "\n"; + + for (auto it : ans) out << it << "\n"; + + return 0; + +} diff --git a/party/2sat_random/50c17096-02b2-11ec-9ee1-2cf05d84bfc2.cpp b/party/2sat_random/50c17096-02b2-11ec-9ee1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e2b7954e279ecaae24ace976419fbc15bf976df --- /dev/null +++ b/party/2sat_random/50c17096-02b2-11ec-9ee1-2cf05d84bfc2.cpp @@ -0,0 +1,244 @@ +#include + + + + + + +using namespace std; + + + +ifstream in("party.in"); + + + +ofstream out("party.out"); + + + +int n,m; + + + +int vine[105],a[1005],b[1005],c[1005]; + + + +int main() + + + +{ + + + + in >> n >> m; + + + + for(int i=1; i<=n; i++) vine[i]=1; + + + + for(int i=1; i<=m; i++) + + + + { + + + + in >> a[i] >> b[i] >> c[i]; + + + + } + + + + int ok=0; + + + + while(!ok) + + + + { + + + + ok=1; + + + + for(int i=1; i<=m; i++) + + + + { + + + + switch(c[i]){ + + + + case 0: + + + + if(vine[a[i]]==0 && vine[b[i]]==0) + + + + { + + + + ok=0; + + + + int p = rand()%2; + + + + vine[a[i]]=p; + + + + vine[b[i]]=1-p; + + + + } + + + + break; + + + + case 1: + + + + if(vine[a[i]]==0 && vine[b[i]]==1) + + + + { + + + + ok=0; + + + + vine[b[i]]=0; + + + + } + + + + break; + + + + case 2: + + + + if(vine[a[i]]==1 && vine[b[i]]==0) + + + + { + + + + ok=0; + + + + vine[a[i]]=0; + + + + } + + + + break; + + + + case 3: + + + + if(vine[a[i]]==1 && vine[b[i]]==1) + + + + { + + + + ok=0; + + + + int p = rand()%2; + + + + vine[a[i]]=p; + + + + vine[b[i]]=1-p; + + + + } + + + + break; + + + + } + + + + } + + + + } + + + + int cnt=0; + + + + for(int i=1; i<=n; i++) if(vine[i]) cnt++; + + + + out << cnt << '\n'; + + + + for(int i=1; i<=n; i++) if(vine[i]) out << i << '\n'; + + + +} diff --git a/party/2sat_random/51440e12-02b2-11ec-9911-2cf05d84bfc2.cpp b/party/2sat_random/51440e12-02b2-11ec-9911-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e508b331338e6d437f41aeca9aba795f8c1bb0f --- /dev/null +++ b/party/2sat_random/51440e12-02b2-11ec-9911-2cf05d84bfc2.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +const int NMAX = 100 + 5; + +const int MMAX = 1000 + 5; + + + +int N, M; + +int x[2 * NMAX]; + +int a[MMAX], b[MMAX]; + + + +int main() { + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + cin >> N >> M; + + for (int i = 1; i <= M; ++i) { + + cin >> a[i] >> b[i]; + + --a[i], a[i] *= 2; + + --b[i], b[i] *= 2; + + int type; + + cin >> type; + + if (type == 1 || type == 3) { + + b[i] ^= 1; + + } + + if (type == 2 || type == 3) { + + a[i] ^= 1; + + } + + } + + for (int i = 1; i < 2 * N; i += 2) { + + x[i] = true; + + } + + while (true) { + + vector clauses; + + for (int i = 1; i <= M; ++i) { + + if (!(x[a[i]] || x[b[i]])) { + + clauses.push_back(i); + + } + + } + + if (clauses.empty()) { + + break; + + } + + const int where = clauses[rand() % clauses.size()]; + + const int pos = rand() % 2 ? a[where] : b[where]; + + x[pos] ^= 1, x[pos ^ 1] ^= 1; + + } + + vector sol; + + for (int i = 0; i < 2 * N; i += 2) { + + if (x[i]) { + + sol.push_back(i / 2 + 1); + + } + + } + + cout << sol.size() << endl; + + for (int i = 0; i < sol.size(); ++i) { + + cout << sol[i] << '\n'; + + } + + return 0; + +} diff --git a/party/2sat_random/52858ec0-02b2-11ec-b696-2cf05d84bfc2.cpp b/party/2sat_random/52858ec0-02b2-11ec-b696-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29ca350bf19562d367db865ffc6416033973b994 --- /dev/null +++ b/party/2sat_random/52858ec0-02b2-11ec-b696-2cf05d84bfc2.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +typedef long long int lint; + + + +const int NMAX = 100 + 5; + +const int MMAX = 1000 + 5; + + + +int N, M; + +int x[NMAX]; + +bool is_neg[MMAX][2]; + +int who[MMAX][2]; + + + +bool eval_clause(bool is_neg_a, bool is_neg_b, int a, int b) { + + bool a_prim = x[a]; + + if (is_neg_a) { + + a_prim = !x[a]; + + } + + bool b_prim = x[b]; + + if (is_neg_b) { + + b_prim = !x[b]; + + } + + return a_prim || b_prim; + +} + + + +int main() { + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + + + cin >> N >> M; + + for (int i = 1; i <= M; ++i) { + + cin >> who[i][0] >> who[i][1]; + + int type; + + cin >> type; + + if (type == 0) { + + } else if (type == 1) { + + is_neg[i][1] = 1; + + } else if (type == 2) { + + is_neg[i][0] = 1; + + } else { + + is_neg[i][0] = 1; + + is_neg[i][1] = 1; + + } + + } + + + + bool done = false; + + while (!done) { + + done = true; + + vector clauses; + + for (int i = 1; i <= M; ++i) { + + if (!eval_clause(is_neg[i][0], is_neg[i][1], who[i][0], who[i][1])) { + + clauses.push_back(i); + + done = false; + + } + + } + + if (!done) { + + random_shuffle(clauses.begin(), clauses.end()); + + const int where = clauses[0]; + + if (rand() % 2) { + + x[who[where][0]] ^= 1; + + } else { + + x[who[where][1]] ^= 1; + + } + + } + + } + + + + vector sol; + + for (int i = 1; i <= N; ++i) { + + if (x[i]) { + + sol.push_back(i); + + } + + } + + + + cout << sol.size() << endl; + + for (int i = 0; i < sol.size(); ++i) { + + cout << sol[i] << '\n'; + + } + + return 0; + +} diff --git a/party/2sat_random/561819f2-02b2-11ec-a2d4-2cf05d84bfc2.cpp b/party/2sat_random/561819f2-02b2-11ec-a2d4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e9312eac09fdc162400da307b3077e6569fd424 --- /dev/null +++ b/party/2sat_random/561819f2-02b2-11ec-a2d4-2cf05d84bfc2.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +using namespace std; + + + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +int n,m; + +int P[1001][2]; + +int X[101]; + + + +int cauta() + + + +{ + + for (int i=1;i<=m;i++) + + { + + + + int a,b; + + if (P[i][0]>0) + + a=X[P[i][0]]; + + else + + a=1-X[-P[i][0]]; + + if (P[i][1]>0) + + b=X[P[i][1]]; + + else + + b=1-X[-P[i][1]]; + + if (a+b==0) + + return i; + + } + + return 0; + +} + + + +int main() + +{ + + srand(time(NULL)); + + fi>>n>>m; + + for (int i=1;i<=m;i++) + + { + + int a,b,c; + + fi>>a>>b>>c; + + if (c==0) + + { + + P[i][0]=a; + + P[i][1]=b; + + } + + if (c==1) + + { + + P[i][0]=a; + + P[i][1]=-b; + + } + + if (c==2) + + { + + P[i][0]=-a; + + P[i][1]=b; + + } + + if (c==3) + + { + + P[i][0]=-a; + + P[i][1]=-b; + + } + + } + + + + for (int i=1;i<=n;i++) + + X[i]=1; + + + + while (1) + + { + + int poz=cauta(); + + if (poz==0) + + break; + + else + + { + + int r; + + r=rand() % 2; + + int var; + + var=P[poz][r]; + + if (var<0) + + var=-var; + + X[var]=1-X[var]; + + } + + } + + int rez; + + rez=0; + + for (int i=1;i<=n;i++) + + rez=rez+X[i]; + + fo< +#include +using namespace std; +ifstream fi("party.in"); +ofstream fo("party.out"); +int n,m; +int P[1001][2]; +int X[101]; + +int modul(int x) +{ + if (x>=0) + return x; + return -x; +} + +int main() +{ + fi>>n>>m; + for (int i=1;i<=m;i++) + { + int a,b,c; + fi>>a>>b>>c; + if (c==0) + { + P[i][0]=a; + P[i][1]=b; + } + if (c==1) + { + P[i][0]=a; + P[i][1]=-b; + } + if (c==2) + { + P[i][0]=-a; + P[i][1]=b; + } + if (c==3) + { + P[i][0]=-a; + P[i][1]=-b; + } + } + + for (int i=1;i<=n;i++) + X[i]=1; + while (1) + { + int poz=0; + for (int i=1;i<=m;i++) + { + int a,b; + if (P[i][0]>0) + a=X[P[i][0]]; + else + a=1-X[-P[i][0]]; + if (P[i][1]>0) + b=X[P[i][1]]; + else + b=1-X[-P[i][1]]; + if (a==0 && b==0) + { + poz=i; + break; + } + } + if (poz==0) + break; + int v; + v=rand()%2; + X[modul(P[poz][v])]=1-X[modul(P[poz][v])]; + } + int rez; + rez=0; + for (int i=1;i<=n;i++) + if (X[i]==1) + rez++; + fo< +#include +#include +#include +using namespace std; + + + +int N, M; + +pair EXPR[100100]; + +int SOL[100100]; + + + +bool eval_term(pair T) { + + int x, y; + + + + x = SOL[abs(T.first)]; + + y = SOL[abs(T.second)]; + + + + if (T.first < 0) + + x ^= 1; + + if (T.second < 0) + + y ^= 1; + + + + return (x | y); + +} + + + +void print_sol() { + + int i; + + for (i = 1; i <= N; i++) + + printf("%d ", SOL[i]); + +} + + + + + +void read() { + + int i, a, b, c; + + scanf("%d%d", &N, &M); + + for (i = 1; i <= M; i++) { + + scanf("%d%d%d", &a, &b, &c); + + if (c == 0) + + EXPR[i] = make_pair(a, b); + + if (c == 1) + + EXPR[i] = make_pair(a, -b); + + if (c == 2) + + EXPR[i] = make_pair(-a, b); + + if (c == 3) + + EXPR[i] = make_pair(-a, -b); + + } + + + +} + + + +void afis() { + + int i, rez = 0; + + for (i = 1; i <= N; i++) + + rez += SOL[i]; + + + + printf("%d\n", rez); + + for (i = 1; i <= N; i++) + + if (SOL[i] == 1) + + printf("%d\n", i); + +} + + + +int main() { + + + + int i, a, b, P; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + read(); + srand(time(0)); + + + + for (i = 1; i <= N; i++) + + SOL[i] = rand() % 2; + + + + for (int step = 0; step <= N * N * 6; step++) { + + int curr_res = 1; + + for (i = 1; i <= M; i++) { + + curr_res &= eval_term(EXPR[i]); + + if (curr_res == 0) { + + P = i; + + break; + + } + + } + + + + if (curr_res == 1) { + + afis(); + + + + return 0; + + } + + + + if (rand() % 2 == 0) + + SOL[abs(EXPR[P].first)] ^= 1; + + else + + SOL[abs(EXPR[P].second)] ^= 1; + + } + + + + printf("-1\n"); + + + + return 0; + +} diff --git a/party/2sat_random/57e6f4d7-02b2-11ec-b85b-2cf05d84bfc2.cpp b/party/2sat_random/57e6f4d7-02b2-11ec-b85b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b327ec8cef08789145ba245ea615d3370d0ba43b --- /dev/null +++ b/party/2sat_random/57e6f4d7-02b2-11ec-b85b-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include + + +using namespace std; + + + +struct muchie + + { + + int x,y,z; + + } + + v[1005]; + +bool a[105]; + + + +int check(int i) + +{ + + if(v[i].z==0) + + return a[v[i].x] || a[v[i].y]; + + if(v[i].z==1) + + return a[v[i].x] || !a[v[i].y]; + + if(v[i].z==2) + + return a[v[i].y] || !a[v[i].x]; + + if(v[i].z==3) + + return a[v[i].x]+a[v[i].y]!=2; + + return 0; + +} + + + +int main() + +{ + + int n,m; + + ifstream in("party.in"); + + ofstream out("party.out"); + + in>>n>>m; + + for(int i=1;i<=m;i++) + + in>>v[i].x>>v[i].y>>v[i].z; + + for(int i=1;i<=n;i++) + + a[i]=true; + + for(int i=1;i<=m;i++) + + if(!check(i)) + + { + + bool r = rand()%2; + + if(r) + + a[v[i].y] = !a[v[i].y]; + + else + + a[v[i].x] = !a[v[i].x]; + + i=0; + + } + + m=0; + + for(int i=1;i<=n;i++) + + if(a[i]) + + m++; + + out< + + +using namespace std; + + + +const int N = 1005; + +mt19937 gen(time(0)); + +uniform_int_distribution dis(0, 1e6); + + + +int i, n, m, x[N], y[N], z[N], val[N], nx[N], ny[N], rs; + + + +int main() { + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + ios_base::sync_with_stdio(0); + + + + cin >> n >> m; + + for(i = 1; i <= m; ++i) { + + cin >> x[i] >> y[i] >> z[i]; + + + + if(z[i] == 1) ny[i] = 1; + + if(z[i] == 2) nx[i] = 1; + + if(z[i] == 3) nx[i] = ny[i] = 1; + + } + + + + for(i = 1; i <= m; ++i) + + if(!(val[x[i]] ^ nx[i]) && !(val[y[i]] ^ ny[i])) { + + if(dis(gen) & 1) val[x[i]] ^= 1; + + else val[y[i]] ^= 1; + + i = 0; + + } + + + + for(i = 1; i <= n; ++i) if(val[i]) ++rs; + + + + cout << rs << '\n'; + + for(i = 1; i <= n; ++i) if(val[i]) cout << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_random/5935e2fb-02b2-11ec-8861-2cf05d84bfc2.cpp b/party/2sat_random/5935e2fb-02b2-11ec-8861-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..939dfedc2d8032a055a1bf42a616476f8759b0df --- /dev/null +++ b/party/2sat_random/5935e2fb-02b2-11ec-8861-2cf05d84bfc2.cpp @@ -0,0 +1,74 @@ +#include + + +using namespace std; + + + +const int N = 1005; + +mt19937 gen(time(0)); + +uniform_int_distribution dis(0, 1e6); + + + +int i, n, m, x[N], y[N], z[N], val[N], nx[N], ny[N], rs; + + + +int main() { + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + ios_base::sync_with_stdio(0); + + + + cin >> n >> m; + + for(i = 1; i <= m; ++i) { + + cin >> x[i] >> y[i] >> z[i]; + + + + if(z[i] == 1) ny[i] = 1; + + if(z[i] == 2) nx[i] = 1; + + if(z[i] == 3) nx[i] = ny[i] = 1; + + } + + + + for(i = 1; i <= m; ++i) + + if(!(val[x[i]] ^ nx[i]) && !(val[y[i]] ^ ny[i])) { + + if(dis(gen) & 1) val[x[i]] ^= 1; + + else val[y[i]] ^= 1; + + i = 0; + + } + + + + for(i = 1; i <= n; ++i) if(val[i]) ++rs; + + + + cout << rs << '\n'; + + for(i = 1; i <= n; ++i) if(val[i]) cout << i << '\n'; + + + + return 0; + +} diff --git a/party/2sat_random/5d41100a-02b2-11ec-b264-2cf05d84bfc2.cpp b/party/2sat_random/5d41100a-02b2-11ec-b264-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36a825abc62b525ed6c868488d527465926d2ab8 --- /dev/null +++ b/party/2sat_random/5d41100a-02b2-11ec-b264-2cf05d84bfc2.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); + + + +const int NMax = 1005; + + + +int A[NMax]; + +int B[NMax]; + +int C[NMax]; + +int ans[NMax]; + + + +inline bool check(const int &x) { + + + + if(C[x] == 0) return ans[A[x]] || ans[B[x]]; + + if(C[x] == 1) return ans[A[x]] || !ans[B[x]]; + + if(C[x] == 2) return !ans[A[x]] || ans[B[x]]; + + if(C[x] == 3) return (ans[A[x]] + ans[B[x]]) != 2; + + return false; + + + +} + + + +int main() { + + + + ios::sync_with_stdio(); + + fin.tie(NULL); + + + + int n, m; + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) fin >> A[i] >> B[i] >> C[i]; + + + + for(int i = 1; i <= n; i++) ans[i] = true; + + + + for(int i = 1; i <= m; i++) { + + + + if(check(i) == false) { + + int ran = rand() % 2; + + if(ran == 1) { + + ans[B[i]] = !ans[B[i]]; + + } else { + + ans[A[i]] = !ans[A[i]]; + + } + + + + i = 0; + + } + + } + + + + int no = 0; + + for(int i = 1; i <= n; i++) { + + if(ans[i] == true) no++; + + } + + + + fout << no << "\n"; + + + + for(int i = 1; i <= n; i++) { + + if(ans[i] == true) fout << i << "\n"; + + } + + + + return 0; + +} diff --git a/party/2sat_random/5d9dfd2a-02b2-11ec-b52d-2cf05d84bfc2.cpp b/party/2sat_random/5d9dfd2a-02b2-11ec-b52d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f7ffcb5c65173be67fb88267e26de26e8d3d9dd --- /dev/null +++ b/party/2sat_random/5d9dfd2a-02b2-11ec-b52d-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int c[1001],n,m,i,nr; + +struct inv + +{ + + int x,y; + + int c; + +}; + +inv v[1001]; + +int verificare(int k) + +{ + + + + if(k==0) + + return c[v[i].x]||c[v[i].y]; + + if(k==1) + + return c[v[i].x]||!c[v[i].y]; + + if(k==2) + + return c[v[i].y]||!c[v[i].x]; + + if(k==3) + + return c[v[i].x]+c[v[i].y]!=2; + + return 0; + +} + +int main() + +{ + + + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + c[i]=1; + + for(i=1;i<=m;i++) + + { + + + + if(!verificare(v[i].c)) + + { + + int a=rand()%2; + + if(a==1) + + c[v[i].y]=!c[v[i].y]; + + else + + c[v[i].x]=!c[v[i].x]; + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(c[i]) + + ++nr; + + g< +#include +#include +using namespace std; + +int n,m,x[1001],y[1001],z[1001],Z,i,t[202],cnt; + +int main(){ + + srand(time(0)); + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++)scanf("%d%d%d",&x[i],&y[i],&z[i]); + + for(i=0;i-m-1;) + + { + + for(i=1;i<=m;i++) + + { + + Z=(t[x[i]]<<1)|t[y[i]];if(Z-z[i])continue; + + if(rand()&1)t[x[i]]=1-t[x[i]];else t[y[i]]=1-t[y[i]];break; + + } + + } + + for(i=1;i<=n;i++)cnt+=t[i];printf("%d\n",cnt); + + for(i=1;i<=n;i++)if(t[i]&1)printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_random/5ef77380-02b2-11ec-93f5-2cf05d84bfc2.cpp b/party/2sat_random/5ef77380-02b2-11ec-93f5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f7ffcb5c65173be67fb88267e26de26e8d3d9dd --- /dev/null +++ b/party/2sat_random/5ef77380-02b2-11ec-93f5-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int c[1001],n,m,i,nr; + +struct inv + +{ + + int x,y; + + int c; + +}; + +inv v[1001]; + +int verificare(int k) + +{ + + + + if(k==0) + + return c[v[i].x]||c[v[i].y]; + + if(k==1) + + return c[v[i].x]||!c[v[i].y]; + + if(k==2) + + return c[v[i].y]||!c[v[i].x]; + + if(k==3) + + return c[v[i].x]+c[v[i].y]!=2; + + return 0; + +} + +int main() + +{ + + + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + c[i]=1; + + for(i=1;i<=m;i++) + + { + + + + if(!verificare(v[i].c)) + + { + + int a=rand()%2; + + if(a==1) + + c[v[i].y]=!c[v[i].y]; + + else + + c[v[i].x]=!c[v[i].x]; + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(c[i]) + + ++nr; + + g< +#include +#include +using namespace std; + + + +int a1[1003],a2[1003],b1[1003],b2[1003],val[103]; + +int N,M; + + + +ifstream cin ("party.in"); + +ofstream cout("party.out"); + + + +void solve() + +{ + + int l1,l2; + + for(int i=1;i<=M;++i) + + { + + l1=val[a1[i]];l2=val[a2[i]]; + + if(b1[i])l1^=1; + + if(b2[i])l2^=1; + + if(!l1&&!l2) + + { + + if(rand()%2) val[a1[i]]^= 1; + + else val[a2[i]]^= 1; + + i=0; + + } + + } + +} + + + +int main() + +{ + + int i,j,x,y,z; + + srand(int(time(NULL))); + + cin>>N>>M; + + for(i=1;i<=M;++i) + + { + + cin>>a1[i]>>a2[i]>>z; + + if(z==1||z==3)b2[i]=1; + + if(z==2||z==3)b1[i]=1; + + } + + solve(); + + int NR=0; + + for(i=1;i<=N;++i) + + if(val[i]) ++NR; + + cout< +#include +#include +using namespace std; + + + +int a1[1003], a2[1003], b1[1003], b2[1003], N, M; + +int val[103]; + + + +void solve(){ + + + + int l1,l2; + + + + for(int i=1;i<=M;++i) + + { + + l1 = val[a1[i]]; l2 = val[a2[i]]; + + if(b1[i]) l1 ^= 1; + + if(b2[i]) l2 ^= 1; + + if(!l1 && !l2) + + { + + if(rand() % 2) + + { + + val[a1[i]] ^= 1; + + } + + else + + { + + val[a2[i]] ^= 1; + + } + + i = 0; + + } + } + +} + + + +int main(){ + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + int i,j,x,y,z; + + + + srand(int(time(NULL))); + + + + cin >> N >> M; + + + + for(i=1;i<=M;++i) + + { + + cin >> a1[i] >> a2[i] >> z; + + if(z == 1 || z == 3) b2[i] = 1; + + if(z == 2 || z == 3) b1[i] = 1; + + } + + + + + + + + solve(); + + int NR = 0; + + for(i=1;i<=N;++i) if(val[i]) ++NR; + + + + cout << NR << "\n"; + + for(i=1;i<=N;++i) if(val[i]) cout << i << "\n"; + + + + return 0; + +} diff --git a/party/2sat_random/662fab8c-02b2-11ec-94e8-2cf05d84bfc2.cpp b/party/2sat_random/662fab8c-02b2-11ec-94e8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88b28ee39a09dfcb0302b72c73f51922f909a124 --- /dev/null +++ b/party/2sat_random/662fab8c-02b2-11ec-94e8-2cf05d84bfc2.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int nmax = 105; + +const int mmax = 1005; + + + +int n, m, i, ok, s, sol[nmax]; + + + +struct propozitie + +{ + + int x, y, z; + +}; + +propozitie a[mmax]; + + + +bool valid() + +{ + + for(i = 1; i <= m; i++) + + { + + if(a[i].z == 0) + + ok = sol[a[i].x] | sol[a[i].y]; + + else if(a[i].z == 1) + + ok = sol[a[i].x] | !sol[a[i].y]; + + else if(a[i].z == 2) + + ok = !sol[a[i].x] | sol[a[i].y]; + + else + + ok = !sol[a[i].x] | !sol[a[i].y]; + + + + if(!ok) + + { + + int mlc = rand() % 2; + + + + if(!mlc) sol[a[i].x] = !sol[a[i].x]; + + else sol[a[i].y] = !sol[a[i].y]; + + + + return 0; + + } + + } + + + + return 1; + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(0)); + + + + scanf("%d%d", &n, &m); + + + + for(i = 1; i <= m; i++) + + scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z); + + + + for(i = 1; i <= n; i++) + + sol[i] = rand() % 2; + + + + while(!valid()); + + + + for(i = 1; i <= n; i++) + + s += sol[i]; + + + + printf("%d\n", s); + + for(i = 1; i <= n; i++) + + if(sol[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/66cab32f-02b2-11ec-b664-2cf05d84bfc2.cpp b/party/2sat_random/66cab32f-02b2-11ec-b664-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ab6542b621f66b67991810cf72a6ec6ba52a9fc4 --- /dev/null +++ b/party/2sat_random/66cab32f-02b2-11ec-b664-2cf05d84bfc2.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +using namespace std; + + + +int a1[1003], a2[1003], b1[1003], b2[1003], N, M; + +int val[103]; + + + +void solve(){ + + + + int l1,l2; + + + + for(int i=1;i<=M;++i) + + { + + l1 = val[a1[i]]; l2 = val[a2[i]]; + + if(b1[i]) l1 ^= 1; + + if(b2[i]) l2 ^= 1; + + if(!l1 && !l2) + + { + + if(rand() % 2) + + { + + val[a1[i]] ^= 1; + + } + + else + + { + + val[a2[i]] ^= 1; + + } + + i = 0; + + } + } + +} + + + +int main(){ + + ifstream cin("party.in"); + + ofstream cout("party.out"); + + int i,j,x,y,z; + + + + srand(int(time(NULL))); + + + + cin >> N >> M; + + + + for(i=1;i<=M;++i) + + { + + cin >> a1[i] >> a2[i] >> z; + + if(z == 1 || z == 3) b2[i] = 1; + + if(z == 2 || z == 3) b1[i] = 1; + + } + + + + + + + + solve(); + + int NR = 0; + + for(i=1;i<=N;++i) if(val[i]) ++NR; + + + + cout << NR << "\n"; + + for(i=1;i<=N;++i) if(val[i]) cout << i << "\n"; + + + + return 0; + +} diff --git a/party/2sat_random/679bbeb0-02b2-11ec-8857-2cf05d84bfc2.cpp b/party/2sat_random/679bbeb0-02b2-11ec-8857-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79fac4084f5ec2025ede58539d12b9a425945f98 --- /dev/null +++ b/party/2sat_random/679bbeb0-02b2-11ec-8857-2cf05d84bfc2.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +int N, M; + +int x[1000], E[1000][2]; + +int i,v; + +int modul(int a) + +{ + + if (a < 0) return - a; + + return a; + +} + +int main() + +{ + + fi >> N >> M; + + for (i = 1; i <= M; i++) + + { + + int a; + + fi >> E[i][0] >> E[i][1]; + + fi >> a; + + if (a == 1) E[i][1] = -E[i][1]; + + if (a == 2) E[i][0] = -E[i][0]; + + if (a == 3) {E[i][1] = -E[i][1]; E[i][0] = -E[i][0];} + + + + } + + for (i = 1; i <= N; i++) + + x[i] = rand() % 2; + + while(1) + + { + + + + for (i = 1; i <= M; i++) + + { + + if (E[i][0] > 0 && E[i][1] > 0) v = x[E[i][0]] | x[E[i][1]]; + + if (E[i][0] > 0 && E[i][1] < 0) v = x[E[i][0]] | (1-x[-E[i][1]]); + + if (E[i][0] < 0 && E[i][1] > 0) v = (1-x[-E[i][0]]) | x[E[i][1]]; + + if (E[i][0] < 0 && E[i][1] < 0) v = (1-x[-E[i][0]]) | (1-x[-E[i][1]]); + + if (v==0) break; + + } + + int var; + + if (!v) + + { + + var = rand() % 2; + + x[modul(E[i][var])] = 1 - x[modul(E[i][var])]; + + } + + else break; + + } + + int rez = 0; + + for (i = 1; i <= N; i++) rez += x[i]; + + fo << rez<<'\n'; + + for (i = 1; i <= N; i++) if (x[i]) fo << i << '\n'; + + fi.close(); + + fo.close(); + + return 0; + +} diff --git a/party/2sat_random/67badfe9-02b2-11ec-94bd-2cf05d84bfc2.cpp b/party/2sat_random/67badfe9-02b2-11ec-94bd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dd04098ba31338ae63d302fb4fe9e8f17c7f285 --- /dev/null +++ b/party/2sat_random/67badfe9-02b2-11ec-94bd-2cf05d84bfc2.cpp @@ -0,0 +1,115 @@ +#include +#include + using namespace std; + +bool ok[109]; + +struct inv{ + + int x,y,z; + + bool check() + + { + + if(z==0) + + return ok[x]||ok[y]; + + if(z==1) + + return ok[x]||!ok[y]; + + if(z==2) + + return !ok[x]||ok[y]; + + if(z==3) + + return !(ok[x]&&ok[y]); + + return 0; + + } + + + + void random() + + { + + int v=rand()%2; + + if(v==0) + + ok[x]=!ok[x]; + + else + + ok[y]=!ok[y]; + + } + +}; + +inv inviat[1003]; + +int n,m; + + + +int main() + +{ + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n>>m; + + for(int i=1;i<=n;++i){ + + ok[i]=1; + + } + + + + for(int i=0;i>inviat[i].x>>inviat[i].y>>inviat[i].z; + + + + for(int i=0;i +#include + using namespace std; + +bool ok[109]; + +struct inv{ + + int x,y,z; + + bool check() + + { + + if(z==0) + + return ok[x]||ok[y]; + + if(z==1) + + return ok[x]||!ok[y]; + + if(z==2) + + return !ok[x]||ok[y]; + + if(z==3) + + return !(ok[x]&&ok[y]); + + return 0; + + } + + + + void random() + + { + + int v=rand()%2; + + if(v==0) + + ok[x]=!ok[x]; + + else + + ok[y]=!ok[y]; + + } + +}; + +inv inviat[1003]; + +int n,m; + + + +int main() + +{ + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n>>m; + + for(int i=1;i<=n;++i){ + + ok[i]=1; + + } + + + + for(int i=0;i>inviat[i].x>>inviat[i].y>>inviat[i].z; + + + + for(int i=0;i +#include + + +using namespace std; + + + +const int MAX_N = 105; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int n, m, sol; + +bool val[MAX_N]; + + + +struct party + +{ + + int x, y, z; + + bool ok; + + + + void check () + + { + + if (z == 0) + + ok = val[x] || val[y]; + + if (z == 1) + + ok = val[x] || !val[y]; + + if (z == 2) + + ok = !val[x] || val[y]; + + if (z == 3) + + ok = !val[x] || !val[y]; + + } + +} v[1005]; + + + +bool valid() + +{ + + for (int i = 0; i < m; i ++) + + { + + v[i].check(); + + if (!v[i].ok) + + { + + bool verif = rand() % 2; + + if (verif) + + val[v[i].x] = !val[v[i].x]; + + else + + val[v[i].y] = !val[v[i].y]; + + return false; + + } + + } + + return true; + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 0; i < m; i ++) + + f >> v[i].x >> v[i].y >> v[i].z; + + + + for (int i = 1; i <= n; i ++) + + val[i] = rand() % 2; + + + + while (!valid()); + + for (int i = 1; i <= n; i ++) + + sol += val[i]; + + + + g << sol << "\n"; + + for (int i = 1; i <= n; i ++) + + if (val[i]) + + g << i << "\n"; + + + + f.close(); + + g.close(); + + return 0; + + + +} diff --git a/party/2sat_random/681953e2-02b2-11ec-9560-2cf05d84bfc2.cpp b/party/2sat_random/681953e2-02b2-11ec-9560-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0860a27b5a5406b48179cd21bf13e6c9f23979f4 --- /dev/null +++ b/party/2sat_random/681953e2-02b2-11ec-9560-2cf05d84bfc2.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in ("party.in"); + +ofstream out ("party.out"); + + + +int C[10*101], n, m, i, Nr; + +struct inv + +{ + + int x,y; + + int c; + +}; + + + +inv v[10*101]; + + + +int verificare(int k) + +{ + + + + if(k==0) + + return C[v[i].x] || C[v[i].y]; + + + + if(k==1) + + return C[v[i].x] || !C[v[i].y]; + + + + if(k==2) + + return C[v[i].y]||!C[v[i].x]; + + + + if(k==3) + + return C[v[i].x]+C[v[i].y]!=2; + + + + return 0; + +} + +int main () + +{ + + + + in>>n>>m; + + + + for(i=1;i<=m;i++) + + in>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + C[i]=1; + + + + for(i=1;i<=m;i++) + + { + + + + if(!verificare(v[i].c)) + + { + + int a=rand()%2; + + + + if(a==1) C[v[i].y]=!C[v[i].y]; + + else C[v[i].x]=!C[v[i].x]; + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(C[i]) ++Nr; + + out< +#include +#include +#include + + +using namespace std; + + + +ifstream in ("party.in"); + +ofstream out ("party.out"); + + + +int C[10*101], n, m, i, Nr; + +struct inv + +{ + + int x,y; + + int c; + +}; + + + +inv v[10*101]; + + + +int verificare(int k) + +{ + + + + if(k==0) + + return C[v[i].x] || C[v[i].y]; + + + + if(k==1) + + return C[v[i].x] || !C[v[i].y]; + + + + if(k==2) + + return C[v[i].y]||!C[v[i].x]; + + + + if(k==3) + + return C[v[i].x]+C[v[i].y]!=2; + + + + return 0; + +} + +int main () + +{ + + + + in>>n>>m; + + + + for(i=1;i<=m;i++) + + in>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + C[i]=1; + + + + for(i=1;i<=m;i++) + + { + + + + if(!verificare(v[i].c)) + + { + + int a=rand()%2; + + + + if(a==1) C[v[i].y]=!C[v[i].y]; + + else C[v[i].x]=!C[v[i].x]; + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(C[i]) ++Nr; + + out< +#include +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + +int N,M; + +int E[2001][2]; + +int X[201]; + +int pasi,v,i; + +int var; + +int x,y; + +int t; + +int rez; + +int modul(int a) + +{ + + if (a<0) + + return -a; + + return a; + +} + +int main() + +{ + + srand (time(NULL)); + + fi>>N>>M; + + for(i=1;i<=M;i++) + + { + + fi>>x>>y>>t; + + if(t==0) + + { + + E[i][0]=x; + + E[i][1]=y; + + } + + if(t==1) + + { + + E[i][0]=x; + + E[i][1]=-y; + + } + + if(t==2) + + { + + E[i][0]=-x; + + E[i][1]=y; + + } + + if(t==3) + + { + + E[i][0]=-x; + + E[i][1]=-y; + + } + + } + + pasi=0; + + while(1) + + { + + + + for (i=1;i<=M;i++) + + { + + if (E[i][0]>0 && E[i][1]>0) + + v=X[E[i][0]] | X[E[i][1]]; + + if (E[i][0]>0 && E[i][1]<0) + + v=X[E[i][0]] | (1-X[-E[i][1]]); + + if (E[i][0]<0 && E[i][1]>0) + + v=(1-X[-E[i][0]]) | X[E[i][1]]; + + if (E[i][0]<0 && E[i][1]<0) + + v=(1-X[E[i][0]]) | (1-X[-E[i][1]]); + + if (v==0) + + break; + + } + + if (v==0) + + { + + var=rand() % 2; + + X[modul(E[i][var])]=1-X[modul(E[i][var])]; + + } + + else + + break; + + } + + for (i=1;i<=N;i++) + + if(X[i]==1) + + rez++; + + fo< +#include +#include + + +using namespace std; + + + +bool state[104]; + +class ecuations + +{ + +public: + + int x, y, z; + + bool ok; + + + + void updateOk() + + { + + switch (z) + + { + + case 0: + + ok = state[x] || state[y]; + + break; + + case 1: + + ok = state[x] || !state[y]; + + break; + + case 2: + + ok = !state[x] || state[y]; + + break; + + default: + + ok = !state[x] || !state[y]; + + } + + } + + ecuations(int X, int Y, int Z) + + { + + x = X; + + y = Y; + + z = Z; + + } + + + +}; + +vector v; + +bool satisfied(int m) + +{ + + for (int i = 0; i < m; ++i) + + { + + v[i].updateOk(); + + if (!v[i].ok) + + { + + bool aux = rand() % 2; + + if (aux) + + state[v[i].x] = !state[v[i].x]; + + else + + state[v[i].y] = !state[v[i].y]; + + + + return false; + + } + + + + } + + return true; + +} + + + +int main() + +{ + + + + FILE*f = fopen("party.in", "r"); + + int n, m; + + fscanf(f, "%d %d", &n, &m); + + for (int i = 1; i <= m; ++i) + + { + + int x, y, z; + + fscanf(f, "%d%d%d", &x, &y, &z); + + ecuations ec(x, y, z); + + v.push_back(ec); + + } + + fclose(f); + + + + + + for (int i = 1; i <= n; ++i) + + state[i] = rand() % 2; + + + + + + + + while (!satisfied(m)); + + + + int nr = 0; + + for (int i = 1; i <= n; ++i) + + nr += state[i]; + + + + FILE*g = fopen("party.out", "w"); + + fprintf(g, "%d\n", nr); + + for (int i = 1; i <= n; ++i) + + { + + if (state[i]) + + fprintf(g, "%d\n", i); + + } + + + + return 0; + +} diff --git a/party/2sat_random/68f2c3e8-02b2-11ec-9984-2cf05d84bfc2.cpp b/party/2sat_random/68f2c3e8-02b2-11ec-9984-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2feaf55ece3e1c28af8db651a68c31aa1d41e095 --- /dev/null +++ b/party/2sat_random/68f2c3e8-02b2-11ec-9984-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +const int MAX_N = 101; + +const int MAX_M = 1001; + + + +int stare[MAX_N], reqx[MAX_M],reqy[MAX_M],reqt[MAX_M]; + +int lst[MAX_M]; + + + +int main() + +{ + + + + freopen("party.in","r",stdin); + + + + int N, M; + + scanf("%d%d", &N, &M); + + + + for (int i = 1; i <= M; ++i) + + scanf("%d%d%d", &reqx[i], &reqy[i], &reqt[i]); + + int val = 0,cnt = 0, i; + + + + srand(time(0)); + + + + for (int i = 1; i <= N; ++i) + + if (rand() & 3) + + stare[i] = 1; + + else + + stare[i] = 0; + + + + for (;;){ + + + + lst[0] = 0; + + for (i = 1;i <= M; ++i){ + + + + int x = stare[reqx[i]]; + + int y = stare[reqy[i]]; + + if ( reqt[i] == 0 ) + + val=( x | y ); + + if ( reqt[i] == 1 ) + + val=( x | (y^1) ); + + if ( reqt[i] == 2 ) + + val=( (x^1) | y ); + + if ( reqt[i] == 3 ) + + val=( (x^1) | (y^1) ); + + if ( val == 0 ){ + + + + ++lst[0]; + + lst[lst[0]] = i; + + } + + } + + + + int cnt = 0; + + + + for (int j = 1;j <= N; ++j) + + cnt += stare[j]; + + + + if (lst[0] == 0 && cnt) + + break; + + + + if (lst[0]){ + + i = lst[rand()%lst[0] + 1]; + + if (rand()&3) + + stare[reqx[i]] ^=1; + + else + + stare[ reqy[i] ] ^=1; + + } + + } + + + + cnt = 0; + + + + for ( i = 1; i <= N ; ++i ) + + cnt += stare[i]; + + + + freopen("party.out","w",stdout); + + + + printf("%d\n",cnt); + + + + for ( i = 1; i <= N ; ++i ) + + if (stare[i]) printf("%d\n",i); + + + + + + return 0; + +} diff --git a/party/2sat_random/69136b47-02b2-11ec-9229-2cf05d84bfc2.cpp b/party/2sat_random/69136b47-02b2-11ec-9229-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2feaf55ece3e1c28af8db651a68c31aa1d41e095 --- /dev/null +++ b/party/2sat_random/69136b47-02b2-11ec-9229-2cf05d84bfc2.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +const int MAX_N = 101; + +const int MAX_M = 1001; + + + +int stare[MAX_N], reqx[MAX_M],reqy[MAX_M],reqt[MAX_M]; + +int lst[MAX_M]; + + + +int main() + +{ + + + + freopen("party.in","r",stdin); + + + + int N, M; + + scanf("%d%d", &N, &M); + + + + for (int i = 1; i <= M; ++i) + + scanf("%d%d%d", &reqx[i], &reqy[i], &reqt[i]); + + int val = 0,cnt = 0, i; + + + + srand(time(0)); + + + + for (int i = 1; i <= N; ++i) + + if (rand() & 3) + + stare[i] = 1; + + else + + stare[i] = 0; + + + + for (;;){ + + + + lst[0] = 0; + + for (i = 1;i <= M; ++i){ + + + + int x = stare[reqx[i]]; + + int y = stare[reqy[i]]; + + if ( reqt[i] == 0 ) + + val=( x | y ); + + if ( reqt[i] == 1 ) + + val=( x | (y^1) ); + + if ( reqt[i] == 2 ) + + val=( (x^1) | y ); + + if ( reqt[i] == 3 ) + + val=( (x^1) | (y^1) ); + + if ( val == 0 ){ + + + + ++lst[0]; + + lst[lst[0]] = i; + + } + + } + + + + int cnt = 0; + + + + for (int j = 1;j <= N; ++j) + + cnt += stare[j]; + + + + if (lst[0] == 0 && cnt) + + break; + + + + if (lst[0]){ + + i = lst[rand()%lst[0] + 1]; + + if (rand()&3) + + stare[reqx[i]] ^=1; + + else + + stare[ reqy[i] ] ^=1; + + } + + } + + + + cnt = 0; + + + + for ( i = 1; i <= N ; ++i ) + + cnt += stare[i]; + + + + freopen("party.out","w",stdout); + + + + printf("%d\n",cnt); + + + + for ( i = 1; i <= N ; ++i ) + + if (stare[i]) printf("%d\n",i); + + + + + + return 0; + +} diff --git a/party/2sat_random/69845581-02b2-11ec-8a1d-2cf05d84bfc2.cpp b/party/2sat_random/69845581-02b2-11ec-8a1d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77509d5274b0b1495be0bf63d96d1c90570740fc --- /dev/null +++ b/party/2sat_random/69845581-02b2-11ec-8a1d-2cf05d84bfc2.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int n, m, sol; + +bool val[105]; + + + +struct party + +{ + + int x, y, z; + + bool ok; + + + + void check () + + { + + if (z == 0) + + ok = val[x] || val[y]; + + if (z == 1) + + ok = val[x] || !val[y]; + + if (z == 2) + + ok = !val[x] || val[y]; + + if (z == 3) + + ok = !val[x] || !val[y]; + + } + +} v[1005]; + + + +bool valid() + +{ + + for (int i = 0; i < m; i ++) + + { + + v[i].check(); + + if (!v[i].ok) + + { + + bool verif = rand() % 2; + + if (verif) + + val[v[i].x] = !val[v[i].x]; + + else + + val[v[i].y] = !val[v[i].y]; + + return false; + + } + + } + + return true; + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 0; i < m; i ++) + + f >> v[i].x >> v[i].y >> v[i].z; + + + + for (int i = 1; i <= n; i ++) + + val[i] = rand() % 2; + + + + while (!valid()); + + for (int i = 1; i <= n; i ++) + + sol += val[i]; + + + + g << sol << "\n"; + + for (int i = 1; i <= n; i ++) + + if (val[i]) + + g << i << "\n"; + + + + f.close(); + + g.close(); + + return 0; + + + +} diff --git a/party/2sat_random/69a32838-02b2-11ec-a702-2cf05d84bfc2.cpp b/party/2sat_random/69a32838-02b2-11ec-a702-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7e30cceb7b539e1cb15c1571c742c4fcaf4c888 --- /dev/null +++ b/party/2sat_random/69a32838-02b2-11ec-a702-2cf05d84bfc2.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +bool ok[109]; +struct inv{ + int x,y,z; + bool check() + { + if(z==0) + return ok[x]||ok[y]; + if(z==1) + return ok[x]||!ok[y]; + if(z==2) + return !ok[x]||ok[y]; + if(z==3) + return !(ok[x]&&ok[y]); + return 0; + } + + void random() + { + int v=rand()%2; + if(v==0) + ok[x]=!ok[x]; + else + ok[y]=!ok[y]; + } +}; +inv inviat[1003]; +int n,m; + +int main() +{ + + + ifstream f("party.in"); + ofstream g("party.out"); + f>>n>>m; + for(int i=1;i<=n;++i){ + ok[i]=1; + } + + for(int i=0;i>inviat[i].x>>inviat[i].y>>inviat[i].z; + + for(int i=0;i +#include +using namespace std; +ifstream fi("party.in"); +ofstream fo("party.out"); + +const int maxz = 1005; +const int maxn = 105; + +bool este[maxn]; +int i,n,m,x,y,z; +int nr=0; + +struct triplet{ + int x,y,z; + + bool verific(){ + if(z==0) return(este[x] || este[y]); + if(z==1) return(este[x] || !este[y]); + if(z==2) return(!este[x] || este[y]); + if(z==3) return(!(este[x] && este[y])); + return 1; + } + + void select_random(){ + int r=rand()%2; + if(r==0) este[x]=!este[x]; + else este[y]=!este[y]; + } +}; + +triplet t[maxz]; + +int main(){ + fi>>n>>m; + for(i=1;i<=m;i++) fi>>t[i].x>>t[i].y>>t[i].z; + for(i=1;i<=n;i++) este[i]=1; + + i=1; + while(i<=m){ + if(!t[i].verific()){ t[i].select_random(); i=1; } + else i++; + } + + for(i=1;i<=n;i++) if(este[i]) nr++; + fo< +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + + + +const int maxz = 1005; + +const int maxn = 105; + + + +bool este[maxn]; + +int i,n,m,x,y,z; + +int nr=0; + + + +struct triplet{ + + int x,y,z; + + + + bool verific(){ + + if(z==0) return(este[x] || este[y]); + + if(z==1) return(este[x] || !este[y]); + + if(z==2) return(!este[x] || este[y]); + + if(z==3) return(!(este[x] && este[y])); + + return 1; + + } + + + + void select_random(){ + + int r=rand()%2; + + if(r==0) este[x]=!este[x]; + + else este[y]=!este[y]; + + } + +}; + + + +triplet t[maxz]; + + + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++) fi>>t[i].x>>t[i].y>>t[i].z; + + for(i=1;i<=n;i++) este[i]=1; + + + + i=1; + + while(i<=m){ + + if(!t[i].verific()){ t[i].select_random(); i=1; } + + else i++; + + } + + + + for(i=1;i<=n;i++) if(este[i]) nr++; + + fo< +#include +#include +#include + + + + + + +using namespace std; + + + +typedef struct cerinta { + + int x; + + int y; + + int z; + +} cer; + +cer c[1010]; + + + +int v[1010]; + +int main() + +{ + + srand(time(NULL)); + + int n, m, ok, r; + + + + FILE *fin, *fout; + + fin = fopen("party.in", "r"); + + fout=fopen("party.out", "w"); + + + + fscanf(fin, "%d %d", &n, &m); + + + + for(int i=0; i +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + + + +const int maxz = 1005; + +const int maxn = 105; + + + +bool este[maxn]; + +int i,n,m,x,y,z; + +int nr=0; + + + +struct triplet{ + + int x,y,z; + + + + bool verific(){ + + if(z==0) return(este[x] || este[y]); + + if(z==1) return(este[x] || !este[y]); + + if(z==2) return(!este[x] || este[y]); + + if(z==3) return(!(este[x] && este[y])); + + } + + + + void select_random(){ + + int r=rand()%2; + + if(r==0) este[x]=!este[x]; + + else este[y]=!este[y]; + + } + +}; + + + +triplet t[maxz]; + + + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++) fi>>t[i].x>>t[i].y>>t[i].z; + + for(i=1;i<=n;i++) este[i]=1; + + + + i=1; + + while(i<=m){ + + if(!t[i].verific()){ t[i].select_random(); i=1; } + + else i++; + + } + + + + for(i=1;i<=n;i++) if(este[i]) nr++; + + fo< +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + + + + + +int n, m, sol; + +bool val[105]; + + + +struct party + +{ + + int x, y, z; + + bool ok; + + void makeok() + + { + + if (z == 0) + + ok = val[x] || val[y]; + + if (z == 1) + + ok = val[x] || !val[y]; + + if (z == 2) + + ok = !val[x] || val[y]; + + if (z == 3) + + ok = !val[x] || !val[y]; + + } + +} p[1005]; + + + +bool valid() + +{ + + for (int i = 0; i < m; i ++) + + { + + p[i].makeok(); + + if (!p[i].ok) + + { + + bool loto = rand() % 2; + + if (loto) + + val[p[i].x] = !val[p[i].x]; + + else + + val[p[i].y] = !val[p[i].y]; + + return false; + + } + + } + + return true; + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 0; i < m; i ++) + + f >> p[i].x >> p[i].y >> p[i].z; + + for (int i = 1; i <= n; i ++) + + val[i] = rand() % 2; + + while (!valid()); + + for (int i = 1; i <= n; i ++) + + sol += val[i]; + + g << sol << '\n'; + + for (int i = 1; i <= n; i ++) + + if (val[i]) + + g << i << '\n'; + + + + f.close(); + + g.close(); + + return 0; + + + +} diff --git a/party/2sat_random/6a824330-02b2-11ec-adbd-2cf05d84bfc2.cpp b/party/2sat_random/6a824330-02b2-11ec-adbd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77509d5274b0b1495be0bf63d96d1c90570740fc --- /dev/null +++ b/party/2sat_random/6a824330-02b2-11ec-adbd-2cf05d84bfc2.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int n, m, sol; + +bool val[105]; + + + +struct party + +{ + + int x, y, z; + + bool ok; + + + + void check () + + { + + if (z == 0) + + ok = val[x] || val[y]; + + if (z == 1) + + ok = val[x] || !val[y]; + + if (z == 2) + + ok = !val[x] || val[y]; + + if (z == 3) + + ok = !val[x] || !val[y]; + + } + +} v[1005]; + + + +bool valid() + +{ + + for (int i = 0; i < m; i ++) + + { + + v[i].check(); + + if (!v[i].ok) + + { + + bool verif = rand() % 2; + + if (verif) + + val[v[i].x] = !val[v[i].x]; + + else + + val[v[i].y] = !val[v[i].y]; + + return false; + + } + + } + + return true; + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 0; i < m; i ++) + + f >> v[i].x >> v[i].y >> v[i].z; + + + + for (int i = 1; i <= n; i ++) + + val[i] = rand() % 2; + + + + while (!valid()); + + for (int i = 1; i <= n; i ++) + + sol += val[i]; + + + + g << sol << "\n"; + + for (int i = 1; i <= n; i ++) + + if (val[i]) + + g << i << "\n"; + + + + f.close(); + + g.close(); + + return 0; + + + +} diff --git a/party/2sat_random/6aa0a0a4-02b2-11ec-92e7-2cf05d84bfc2.cpp b/party/2sat_random/6aa0a0a4-02b2-11ec-92e7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7e30cceb7b539e1cb15c1571c742c4fcaf4c888 --- /dev/null +++ b/party/2sat_random/6aa0a0a4-02b2-11ec-92e7-2cf05d84bfc2.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +bool ok[109]; +struct inv{ + int x,y,z; + bool check() + { + if(z==0) + return ok[x]||ok[y]; + if(z==1) + return ok[x]||!ok[y]; + if(z==2) + return !ok[x]||ok[y]; + if(z==3) + return !(ok[x]&&ok[y]); + return 0; + } + + void random() + { + int v=rand()%2; + if(v==0) + ok[x]=!ok[x]; + else + ok[y]=!ok[y]; + } +}; +inv inviat[1003]; +int n,m; + +int main() +{ + + + ifstream f("party.in"); + ofstream g("party.out"); + f>>n>>m; + for(int i=1;i<=n;++i){ + ok[i]=1; + } + + for(int i=0;i>inviat[i].x>>inviat[i].y>>inviat[i].z; + + for(int i=0;i +#include +using namespace std; +bool ok[109]; +struct inv{ + int x,y,z; + bool check() + { + if(z==0) + return ok[x]||ok[y]; + if(z==1) + return ok[x]||!ok[y]; + if(z==2) + return !ok[x]||ok[y]; + if(z==3) + return !(ok[x]&&ok[y]); + return 0; + } + + void random() + { + int v=rand()%2; + if(v==0) + ok[x]=!ok[x]; + else + ok[y]=!ok[y]; + } +}; +inv inviat[1003]; +int n,m; + +int main() +{ + + + ifstream f("party.in"); + ofstream g("party.out"); + f>>n>>m; + for(int i=1;i<=n;++i){ + ok[i]=1; + } + + for(int i=0;i>inviat[i].x>>inviat[i].y>>inviat[i].z; + + for(int i=0;i +#include +#include +#include + + + + + + +using namespace std; + + + +typedef struct cerinta { + + int x; + + int y; + + int z; + +} cer; + +cer c[1010]; + + + +int v[1010]; + +int main() + +{ + + srand(time(NULL)); + + int n, m, ok, r; + + + + FILE *fin, *fout; + + fin = fopen("party.in", "r"); + + fout=fopen("party.out", "w"); + + + + fscanf(fin, "%d %d", &n, &m); + + + + for(int i=0; i +#include +using namespace std; + +ifstream fi("party.in"); + +ofstream fo("party.out"); + + + +const int maxz = 1005; + +const int maxn = 105; + + + +bool este[maxn]; + +int i,n,m,x,y,z; + +int nr=0; + + + +struct triplet{ + + int x,y,z; + + + + bool verific(){ + + if(z==0) return(este[x] || este[y]); + + if(z==1) return(este[x] || !este[y]); + + if(z==2) return(!este[x] || este[y]); + + if(z==3) return(!(este[x] && este[y])); + + return 1; + + } + + + + void select_random(){ + + int r=rand()%2; + + if(r==0) este[x]=!este[x]; + + else este[y]=!este[y]; + + } + +}; + + + +triplet t[maxz]; + + + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++) fi>>t[i].x>>t[i].y>>t[i].z; + + for(i=1;i<=n;i++) este[i]=1; + + + + i=1; + + while(i<=m){ + + if(!t[i].verific()){ t[i].select_random(); i=1; } + + else i++; + + } + + + + for(i=1;i<=n;i++) if(este[i]) nr++; + + fo< +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + + + + + +int n, m, sol; + +bool val[105]; + + + +struct party + +{ + + int x, y, z; + + bool ok; + + void makeok() + + { + + if (z == 0) + + ok = val[x] || val[y]; + + if (z == 1) + + ok = val[x] || !val[y]; + + if (z == 2) + + ok = !val[x] || val[y]; + + if (z == 3) + + ok = !val[x] || !val[y]; + + } + +} p[1005]; + + + +bool valid() + +{ + + for (int i = 0; i < m; i ++) + + { + + p[i].makeok(); + + if (!p[i].ok) + + { + + bool loto = rand() % 2; + + if (loto) + + val[p[i].x] = !val[p[i].x]; + + else + + val[p[i].y] = !val[p[i].y]; + + return false; + + } + + } + + return true; + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 0; i < m; i ++) + + f >> p[i].x >> p[i].y >> p[i].z; + + for (int i = 1; i <= n; i ++) + + val[i] = rand() % 2; + + while (!valid()); + + for (int i = 1; i <= n; i ++) + + sol += val[i]; + + g << sol << '\n'; + + for (int i = 1; i <= n; i ++) + + if (val[i]) + + g << i << '\n'; + + + + f.close(); + + g.close(); + + return 0; + + + +} diff --git a/party/2sat_random/6c3c3e16-02b2-11ec-94cd-2cf05d84bfc2.cpp b/party/2sat_random/6c3c3e16-02b2-11ec-94cd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9733ad9decad972e8f6a58f5d281aeef943965bd --- /dev/null +++ b/party/2sat_random/6c3c3e16-02b2-11ec-94cd-2cf05d84bfc2.cpp @@ -0,0 +1,102 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int C[10*102],n,m,i,Nr; + +struct inv { + + int x,y; + + int c; + +} + +v[10*102]; + +int verificare( int k ) { + + if( k==0) + + return C[v[i].x] || C[v[i].y] ; + + if(k==1) + + return C[v[i].x] || !C[v[i].y]; + + if(k==2) + + return C[v[i].y]||!C[v[i].x]; + + if(k==3) + + return C[v[i].x]+C[v[i].y]!=2 ; + + return 0; + +} + +int main () { + + f>>n>>m; + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + C[i]=1; + + for(i=1;i<=m;i++){ + + if(!verificare(v[i].c)) { + + int a=rand()%2; + + if( a==1 ) { + + C[v[i].y]=!C[v[i].y]; + + } + + else { + + C[v[i].x]=!C[v[i].x]; + + } + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(C[i]){ + + ++Nr; + + } + + g< +#include + + +bool take[103]; + + + +struct exp + +{ + + int x,y,z; + + bool check() + + { + + if(z==0)return take[x]||take[y]; + + if(z==1)return take[x]||!take[y]; + + if(z==2)return !take[x]||take[y]; + + if(z==3)return !(take[x]&&take[y]); + + } + + void rSwitch() + + { + + int v=rand()%2; + + if(v==0)take[x]=!take[x]; + + else take[y]=!take[y]; + + } + +}; + +exp exps[1003]; + +int n,m; + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=n;++i)take[i]=1; + + for(int i=0;i +#include +#include + + +using namespace std; + + + +bool state[104]; + +class ecuations + +{ + +public: + + int x, y, z; + + bool ok; + + + + void updateOk() + + { + + switch (z) + + { + + case 0: + + ok = state[x] || state[y]; + + break; + + case 1: + + ok = state[x] || !state[y]; + + break; + + case 2: + + ok = !state[x] || state[y]; + + break; + + default: + + ok = !state[x] || !state[y]; + + } + + } + + ecuations(int X, int Y, int Z) + + { + + x = X; + + y = Y; + + z = Z; + + } + + + +}; + +vector v; + +bool satisfied(int m) + +{ + + for (int i = 0; i < m; ++i) + + { + + v[i].updateOk(); + + if (!v[i].ok) + + { + + bool aux = rand() % 2; + + if (aux) + + state[v[i].x] = !state[v[i].x]; + + else + + state[v[i].y] = !state[v[i].y]; + + + + return false; + + } + + + + } + + return true; + +} + + + +int main() + +{ + + + + FILE*f = fopen("party.in", "r"); + + int n, m; + + fscanf(f, "%d %d", &n, &m); + + for (int i = 1; i <= m; ++i) + + { + + int x, y, z; + + fscanf(f, "%d%d%d", &x, &y, &z); + + ecuations ec(x, y, z); + + v.push_back(ec); + + } + + fclose(f); + + + + + + for (int i = 1; i <= n; ++i) + + state[i] = rand() % 2; + + + + + + + + while (!satisfied(m)); + + + + int nr = 0; + + for (int i = 1; i <= n; ++i) + + nr += state[i]; + + + + FILE*g = fopen("party.out", "w"); + + fprintf(g, "%d\n", nr); + + for (int i = 1; i <= n; ++i) + + { + + if (state[i]) + + fprintf(g, "%d\n", i); + + } + + + + return 0; + +} diff --git a/party/2sat_random/6d6be47c-02b2-11ec-a163-2cf05d84bfc2.cpp b/party/2sat_random/6d6be47c-02b2-11ec-a163-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..09e79ae2936a6273251f456b3e7f1761e4fd8b3f --- /dev/null +++ b/party/2sat_random/6d6be47c-02b2-11ec-a163-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + +using namespace std; + + + + + +ifstream f("party.in"); + +ofstream g("party.out"); + +int C[10*102],n,m,i,Nr; + +struct inv { + + int x,y; + + int c; + +} + +v[10*102]; + +int verificare( int k ) { + + + + if( k==0) + + return C[v[i].x] || C[v[i].y] ; + + + + if(k==1) + + return C[v[i].x] || !C[v[i].y]; + + + + if(k==2) + + return C[v[i].y]||!C[v[i].x]; + + + + if(k==3) + + return C[v[i].x]+C[v[i].y]!=2 ; + + + + return 0; + +} + +int main () { + + + + f>>n>>m; + + + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + C[i]=1; + + + + for(i=1;i<=m;i++){ + + + + if(!verificare(v[i].c)) { + + int a=rand()%2; + + + + if( a==1 ) { + + C[v[i].y]=!C[v[i].y]; + + } + + else { + + C[v[i].x]=!C[v[i].x]; + + } + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(C[i]){ + + ++Nr; + + } + + g< +#include +#include +using namespace std; + + + +typedef struct _clause { + + int x, y, type; + +} Clause; + + + +const int MAX_N = 102; + +const int MAX_M = 1002; + + + +int n, m; + + + +Clause clause[MAX_M]; + +bool assign[MAX_N]; + + + +void solve() { + + bool done = false; + + int r; + + + + while (!done) { + + done = true; + + for (int i = 1; i <= m; ++i) { + + if (clause[i].type == 0) { + + + + if (!assign[clause[i].x] && !assign[clause[i].y]) { + + r = rand() % 2; + + if (r) assign[clause[i].x] = true; + + else assign[clause[i].y] = true; + + done = false; + + } + + } else if (clause[i].type == 1) { + + + + + + if (!assign[clause[i].x] && assign[clause[i].y]) { + + assign[clause[i].y] = false; + + done = false; + + } + + } else if (clause[i].type == 2) { + + + + + + if (assign[clause[i].x] && !assign[clause[i].y]) { + + assign[clause[i].x] = false; + + done = false; + + } + + } else if (clause[i].type == 2) { + + + + if (assign[clause[i].x] && assign[clause[i].y]) { + + rand() % 2 ? assign[clause[i].x] = false + + : assign[clause[i].y] = false; + + done = false; + + } + + } + + } + + } + +} + + + +int main(int argc, char** argv) { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(NULL)); + + + + + + scanf("%d %d", &n, &m); + + for (int i = 1; i <= m; i++) { + + scanf("%d %d %d", &clause[i].x, &clause[i].y, &clause[i].type); + + } + + for (int i = 1; i <= n; ++i) assign[i] = rand() % 2; + + + + solve(); + + int vars = 0; + + for (int i = 1; i <= n; ++i) { + + if (assign[i]) { + + vars++; + + } + + } + + + + printf("%d\n", vars); + + for (int i = 1; i <= n; ++i) { + + if (assign[i]) { + + printf("%d\n", i); + + } + + } + + return 0; + +} diff --git a/party/2sat_random/6e07ddc1-02b2-11ec-b17e-2cf05d84bfc2.cpp b/party/2sat_random/6e07ddc1-02b2-11ec-b17e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ac89ad735115ad36e29d3cedc1a4a53c05f31a2 --- /dev/null +++ b/party/2sat_random/6e07ddc1-02b2-11ec-b17e-2cf05d84bfc2.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include + + +std::ifstream fin("party.in"); + +std::ofstream fout("party.out"); + + + + + + + +const int MAXN = 110; + +const int MAXM = 1100; + + + +int n, m, sol[MAXN], expresie[MAXM][2]; + + + +void read() { + + fin >> n >> m; + + + + for (int (i) = (1); (i) <= (m); ++(i)) { + + int type; + + fin >> expresie[i][0] >> expresie[i][1] >> type; + + + + if (type == 1 || type == 3) expresie[i][1] = -expresie[i][1]; + + if (type == 2 || type == 3) expresie[i][0] = -expresie[i][0]; + + } + +} + + + +int abs(int a) { + + if (a < 0) + + return -a; + + return a; + +} + + + +bool eval(int a, int b) { + + int x, y; + + + + x = sol[abs(a)]; + + y = sol[abs(b)]; + + + + if (a < 0) x ^= 1; + + if (b < 0) y ^= 1; + + + + return x | y; + +} + + + +void solve() { + + srand(time(0)); + + for (int (i) = (1); (i) <= (n); ++(i)) + + sol[i] = rand() % 2; + + + + for(;;) { + + int poz = 0; + + for (int (i) = (1); (i) <= (m); ++(i)) { + + if (!eval(expresie[i][0], expresie[i][1])) { + + poz = i; + + break; + + } + + } + + if (!poz) break; + + if ((rand() % 2) == 0) + + sol[abs(expresie[poz][0])] ^= 1; + + else + + sol[abs(expresie[poz][1])] ^= 1; + + } + +} + + + +void print() { + + int sum = 0; + + for (int (i) = (1); (i) <= (n); ++(i)) + + sum += sol[i]; + + + + fout << sum << "\n"; + + + + for (int (i) = (1); (i) <= (n); ++(i)) + + if (sol[i]) + + fout << i << "\n"; + +} + + + +int main() { + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/party/2sat_random/6ea4168a-02b2-11ec-887e-2cf05d84bfc2.cpp b/party/2sat_random/6ea4168a-02b2-11ec-887e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54fb01701aa8fd5413180143a432e1ca1508c861 --- /dev/null +++ b/party/2sat_random/6ea4168a-02b2-11ec-887e-2cf05d84bfc2.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +using namespace std; + + + +typedef struct _clause { + + int x, y, type; + +} Clause; + + + +const int MAX_N = 102; + +const int MAX_M = 1002; + + + +int n, m; + + + +Clause clause[MAX_M]; + +bool assign[MAX_N]; + + + +void solve() { + + bool done = false; + + int r; + + + + while (!done) { + + done = true; + + for (int i = 1; i <= m; ++i) { + + if (clause[i].type == 0) { + + + + if (!assign[clause[i].x] && !assign[clause[i].y]) { + + r = rand() % 2; + + if (r) assign[clause[i].x] = true; + + else assign[clause[i].y] = true; + + done = false; + + } + + } else if (clause[i].type == 1) { + + + + + + if (!assign[clause[i].x] && assign[clause[i].y]) { + + assign[clause[i].y] = false; + + done = false; + + } + + } else if (clause[i].type == 2) { + + + + + + if (assign[clause[i].x] && !assign[clause[i].y]) { + + assign[clause[i].x] = false; + + done = false; + + } + + } else if (clause[i].type == 2) { + + + + if (assign[clause[i].x] && assign[clause[i].y]) { + + rand() % 2 ? assign[clause[i].x] = false + + : assign[clause[i].y] = false; + + done = false; + + } + + } + + } + + } + +} + + + +int main(int argc, char** argv) { + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(NULL)); + + + + + + scanf("%d %d", &n, &m); + + for (int i = 1; i <= m; i++) { + + scanf("%d %d %d", &clause[i].x, &clause[i].y, &clause[i].type); + + } + + + + + + int vars = 0; + + while (vars == 0) { + + vars = 0; + + for (int i = 1; i <= n; ++i) assign[i] = rand() % 2; + + solve(); + + + + for (int i = 1; i <= n; ++i) { + + if (assign[i]) { + + vars++; + + } + + } + + } + + + + printf("%d\n", vars); + + for (int i = 1; i <= n; ++i) { + + if (assign[i]) { + + printf("%d\n", i); + + } + + } + + return 0; + +} diff --git a/party/2sat_random/6f5c68b3-02b2-11ec-a4ff-2cf05d84bfc2.cpp b/party/2sat_random/6f5c68b3-02b2-11ec-a4ff-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95d0da91e8a1bc87d25dd1469a714ef42318dc4f --- /dev/null +++ b/party/2sat_random/6f5c68b3-02b2-11ec-a4ff-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("party.in"); + +ofstream g("party.out"); + + + +int N, M, ans, i; + +int x[1020], y[1020], z[1020]; + +int sol[1020]; + + + +bool isWrong (int i) + +{ + + bool a=sol[x[i]]; + + bool b=sol[y[i]]; + + + + if (z[i]>1) a^=1; + + if (z[i]%2) b^=1; + + + + return !(a|b); + +} + + + +int main () + +{ + + srand(time(0)); + + + + f >> N >> M; + + for (i=1; i<=M; i++) + + f >> x[i] >> y[i] >> z[i]; + + + + bool ok=0; + + do + + { + + ok=1; + + + + for (i=1; i<=M; i++) + + if (isWrong(i)) + + { + + int a=x[i], b=y[i]; + + if (rand()%2) swap(a, b); + + + + ans-=sol[a]; + + sol[a]^=1; + + ans+=sol[a]; + + + + ok=0; + + } + + + + if (ans==0 && ok==1) + + { + + ok=0; + + ans=sol[rand()%N+1]=1; + + } + + } while (ok==0); + + + + g << ans << '\n'; + + for (i=1; i<=N; i++) + + if (sol[i]==1) + + g << i << '\n'; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/party/2sat_random/6fb8926d-02b2-11ec-9ab5-2cf05d84bfc2.cpp b/party/2sat_random/6fb8926d-02b2-11ec-9ab5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0561295895e313594d4b402fa5038062e666a4c9 --- /dev/null +++ b/party/2sat_random/6fb8926d-02b2-11ec-9ab5-2cf05d84bfc2.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + +bool sol[115]; + + + +struct relation + +{ + + int x, y, type; + +} x[1024]; + + + +bool bad(relation A) + +{ + + if (A.type == 0 && sol[A.x] == 0 && sol[A.y] == 0) + + return true; + + if (A.type == 1 && sol[A.x] == 0 && sol[A.y] == 1) + + return true; + + if (A.type == 2 && sol[A.x] == 1 && sol[A.y] == 0) + + return true; + + if (A.type == 3 && sol[A.x] == 1 && sol[A.y] == 1) + + return true; + + return false; + +} + + + +int main() + +{ + + int i, N, M, cnt = 0; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(0)); + + + + scanf("%d%d", &N, &M); + + for (i = 1; i <= M; i ++) + + scanf("%d%d%d", &x[i].x, &x[i].y, &x[i].type); + + + + bool isSolution; + + + + do + + { + + isSolution = true; + + for (i = 1; i <= M; i ++) + + if (bad(x[i])) + + { + + int X = x[i].x, Y = x[i].y; + + int which = rand() % 2; + + if (which == 0) + + { + + cnt = cnt - sol[X]; + + sol[X] = 1 - sol[X]; + + cnt = cnt + sol[X]; + + } + + else + + { + + cnt = cnt - sol[Y]; + + sol[Y] = 1 - sol[Y]; + + cnt = cnt + sol[Y]; + + } + + isSolution = false; + + break; + + } + + + + if (cnt == 0 && isSolution == true) + + { + + sol[rand() % N + 1] = 1; cnt ++; + + isSolution = false; + + } + + } + + while (isSolution == false); + + + + printf("%d\n", cnt); + + for (i = 1; i <= N; i ++) + + if (sol[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/6ff7e55c-02b2-11ec-84f5-2cf05d84bfc2.cpp b/party/2sat_random/6ff7e55c-02b2-11ec-84f5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..af45b4703632fecd1f5ba77ffbe58b3d0f2130b9 --- /dev/null +++ b/party/2sat_random/6ff7e55c-02b2-11ec-84f5-2cf05d84bfc2.cpp @@ -0,0 +1,195 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + +std::ifstream in("party.in"); + +std::ofstream out("party.out"); + + + +int N, M; + + + +struct data + +{ + + int x; + + int y; + + int type; + +}; + + + +int v[2000]; + + + +struct data d[2000]; + + + +int check(int i) + +{ + + if(d[i].type==0) + + return v[d[i].x] || v[d[i].y]; + + else if(d[i].type == 1) + + return v[d[i].x] || ! v[d[i].y]; + + else if(d[i].type == 2) + + return v[d[i].y] || ! v[d[i].x]; + + else + + return !v[d[i].x] || !v[d[i].y]; + + + + + + + +} + + + +void randomly(int i) + +{ + + if(rand()%2 == 0) + + v[d[i].x] = !v[d[i].x]; + + else + + v[d[i].y] = !v[d[i].y]; + +} + + + +void read() + +{ + + + + in>>N>>M; + + + + for(int j=1;j<=M;j++) + + in>>d[j].x>>d[j].y>>d[j].type; + + + + for(int j=1;j<=N;j++) + + v[j] = 1; + +} + + + +void calculate() + +{ + + int i; + + while(true) + + { + + for(i=1;i<=M;i++) + + if(check(i)==0) + + { + + randomly(i); + + break; + + } + + + + if(i==M+1) + + break; + + } + +} + + + +void print() + +{ + + int NR = 0; + + for(int i=1;i<=N;i++) + + if(v[i] == 1) + + NR++; + + + + out< +#include +#include +using namespace std; + +int n,m,x[1001],y[1001],z[1001],Z,i,t[202],cnt; + +int main() + +{ + + srand(time(0)); + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++)scanf("%d%d%d",&x[i],&y[i],&z[i]); + + for(i=0;i-m-1;) + + { + + for(i=1;i<=m;i++) + + { + + Z=(t[x[i]]<<1)|t[y[i]];if(Z-z[i])continue; + + if(rand()&1)t[x[i]]=1-t[x[i]];else t[y[i]]=1-t[y[i]];break; + + } + + } + + for(i=1;i<=n;i++)cnt+=t[i];printf("%d\n",cnt); + + for(i=1;i<=n;i++)if(t[i]&1)printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_random/710e8592-02b2-11ec-b009-2cf05d84bfc2.cpp b/party/2sat_random/710e8592-02b2-11ec-b009-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fce2de2ee41c144cf6f530eeb2bb8775398d6424 --- /dev/null +++ b/party/2sat_random/710e8592-02b2-11ec-b009-2cf05d84bfc2.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +vector a; + +struct Propozitie + +{ + + int x,y,z; + +}; + +vector p; + +bool sat(const Propozitie &p) + +{ + + if(p.z==0) + + return a[p.x] || a[p.y]; + + if(p.z==1) + + return a[p.x] || !a[p.y]; + + if(p.z==2) + + return !a[p.x] || a[p.y]; + + return !a[p.x] || !a[p.y]; + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + int n,m; srand(time(0)); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=n;i++) + + a.push_back(rand()%2); + + p.resize(m); + + for(int i=0;i sol; + + for(int i=0;i +#include +#include +#include +using namespace std; + +vector a; + +struct Propozitie + +{ + + int x,y,z; + +}; + +vector p; + +bool sat(const Propozitie &p) + +{ + + if(p.z==0) + + return a[p.x] || a[p.y]; + + if(p.z==1) + + return a[p.x] || !a[p.y]; + + if(p.z==2) + + return !a[p.x] || a[p.y]; + + return !a[p.x] || !a[p.y]; + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + int n,m; srand(time(0)); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=n;i++) + + a.push_back(rand()%2); + + p.resize(m); + + for(int i=0;i sol; + + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + +int v[2000], x[2000], y[2000], c[2000]; + + + +int valid(int i) + +{ + + switch(c[i]) + + { + + case 0: return v[x[i]] || v[y[i]]; break; + + case 1: return v[x[i]] || !v[y[i]]; break; + + case 2: return v[y[i]] || !v[x[i]]; break; + + case 3: return v[x[i]]+v[y[i]] != 2; + + } + + return 1; + +} + + + +void random(int i) + +{ + + int r = rand()%2; r++; + + if(r == 1) + + v[x[i]] = !v[x[i]]; + + if(r == 2) + + v[y[i]] = !v[y[i]]; + +} + + + +int main() + +{ + + ifstream in("party.in"); + + ofstream out("party.out"); + + + + int i, n, m, nr=0; + + in>>n>>m; + + for(i=1; i<=m; i++) + + in>>x[i]>>y[i]>>c[i]; + + for(i=1; i<=n; i++) + + v[i] = 1; + + while(1) + + { + + for(i=1; i<=m; i++) + + if(valid(i) == 0) + + { + + random(i); + + break; + + } + + if(i == m+1) + + break; + + } + + for(i=1; i<=n; i++) + + if(v[i]) + + nr++; + + out< +#include +#include +using namespace std; + +int n,m,x[1001],y[1001],z[1001],Z,i,t[202],cnt; + +int main() + +{ + + srand(time(0)); + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;i++)scanf("%d%d%d",&x[i],&y[i],&z[i]); + + for(i=0;i-m-1;) + + { + + for(i=1;i<=m;i++) + + { + + Z=(t[x[i]]<<1)|t[y[i]];if(Z-z[i])continue; + + if(rand()&1)t[x[i]]=1-t[x[i]];else t[y[i]]=1-t[y[i]];break; + + } + + } + + for(i=1;i<=n;i++)cnt+=t[i];printf("%d\n",cnt); + + for(i=1;i<=n;i++)if(t[i]&1)printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_random/71abafe5-02b2-11ec-977b-2cf05d84bfc2.cpp b/party/2sat_random/71abafe5-02b2-11ec-977b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..242c383e57402ebf1e24d9eec717ffd5d66ef5f9 --- /dev/null +++ b/party/2sat_random/71abafe5-02b2-11ec-977b-2cf05d84bfc2.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + + +using namespace std; + + +int n,m; + +const int MAX_M = 1100; +const int MAX_N = 110; + +int val[MAX_N]; +int type[MAX_M]; +pair which[MAX_M]; + +bool eval(int ind) +{ + int x = which[ind].first, y = which[ind].second; + x = val[x],y = val[y]; + if(type[ind] == 0){ + return x || y; + }else if(type[ind] == 1){ + return (!x && !y) || x; + }else if(type[ind] == 2){ + return (x && y) || !x; + }else{ + return !(x && y); + } +} + + +int main() + +{ + + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + + int i,x; + + scanf("%d %d",&n,&m); + srand(time(NULL)); + + for(i = 1 ; i <= m ; ++ i) + scanf("%d %d %d",&which[i].first,&which[i].second,&type[i]); + + while(true){ + for(i = 1 ; i <= m ; ++ i){ + if(eval(i) == 0){ + x = rand() & 1; + if(x == 0) + val[which[i].first] ^= 1; + else val[which[i].second] ^= 1; + break; + } + } + if(i == m + 1){ + x = 0; + for(i = 1 ; i <= n ; ++ i) + if(val[i]) + ++x; + printf("%d\n",x); + for(i = 1 ; i <= n ; ++ i) + if(val[i]) + printf("%d\n",i); + return 0; + } + } +} diff --git a/party/2sat_random/71cb45e7-02b2-11ec-8878-2cf05d84bfc2.cpp b/party/2sat_random/71cb45e7-02b2-11ec-8878-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fce2de2ee41c144cf6f530eeb2bb8775398d6424 --- /dev/null +++ b/party/2sat_random/71cb45e7-02b2-11ec-8878-2cf05d84bfc2.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +vector a; + +struct Propozitie + +{ + + int x,y,z; + +}; + +vector p; + +bool sat(const Propozitie &p) + +{ + + if(p.z==0) + + return a[p.x] || a[p.y]; + + if(p.z==1) + + return a[p.x] || !a[p.y]; + + if(p.z==2) + + return !a[p.x] || a[p.y]; + + return !a[p.x] || !a[p.y]; + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + int n,m; srand(time(0)); + + scanf("%d%d",&n,&m); + + for(int i=1;i<=n;i++) + + a.push_back(rand()%2); + + p.resize(m); + + for(int i=0;i sol; + + for(int i=0;i +#include +#include +#include + + +using namespace std; + +const int CONST = 20; +const int MAXN = 101; + +bool assign[MAXN]; + +bool eval(const pair& p) +{ + bool a = assign[abs(p.first)]; + bool b = assign[abs(p.second)]; + + if (p.first < 0) + a ^= 1; + if (p.second < 0) + b ^= 1; + + return a | b; +} + +void print(int N) +{ + vector res; + for (int i = 1; i <= N; ++i) + if (assign[i]) res.push_back(i); + + printf("%d\n", static_cast(res.size())); + for (vector::iterator it = res.begin(); it != res.end(); ++it) + printf("%d\n", *it); +} + +bool check(int N) +{ + for (int i = 1; i <= N; ++i) + if (assign[i]) return true; + + return false; +} + +int main() +{ + freopen("party.in", "r", stdin); + freopen("party.out", "w", stdout); + + int N, M; + scanf("%d %d", &N, &M); + + vector > vec; + for (int i = 0; i < M; ++i) { + int x, y, op; + scanf("%d %d %d", &x, &y, &op); + + if (op == 1) { + y = -y; + } else if (op == 2) { + x = -x; + } else if (op == 3) { + x = -x; + y = -y; + } + vec.push_back(make_pair(x, y)); + } + + srand(time(NULL)); + + for (int i = 1; i <= N; ++i) + assign[i] = static_cast(rand() % 2); + + while (true) { + typedef vector >::iterator iter; + + bool result = true; + iter pos; + for (iter it = vec.begin(); it != vec.end(); ++it) { + result &= eval(*it); + if (!result) { + pos = it; + break; + } + } + + if (result && check(N)) { + print(N); + return 0; + } else { + if (rand() % 2 == 0) + assign[abs(pos->first)] ^= 1; + else + assign[abs(pos->second)] ^= 1; + } + } + + return 0; +} diff --git a/party/2sat_random/72276f90-02b2-11ec-be65-2cf05d84bfc2.cpp b/party/2sat_random/72276f90-02b2-11ec-be65-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86119348743d5efef806d36acc7593fda10c37f9 --- /dev/null +++ b/party/2sat_random/72276f90-02b2-11ec-be65-2cf05d84bfc2.cpp @@ -0,0 +1,119 @@ +#include +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,i,ok,nr,r,s[101]; + +struct muchie{int x,y,c;}; + +muchie v[1001]; + +int egresit(muchie a) + +{ + + if(a.c==0&&s[a.x]==0&&s[a.y]==0) + + return 1; + + if(a.c==1&&s[a.x]==0&&s[a.y]==1) + + return 1; + + if(a.c==2&&s[a.x]==1&&s[a.y]==0) + + return 1; + + if(a.c==3&&s[a.x]==1&&s[a.y]==1) + + return 1; + + return 0; + +} + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;++i) + + f>>v[i].x>>v[i].y>>v[i].c; + + do + + { + + ok=1; + + for(i=1;i<=m;++i) + + if(egresit(v[i])) + + { + + r=rand()%2; + + if(r==0) + + { + + nr-=s[v[i].x]; + + s[v[i].x]=1-s[v[i].x]; + + nr+=s[v[i].x]; + + } + + else + + { + + nr-=s[v[i].y]; + + s[v[i].y]=1-s[v[i].y]; + + nr+=s[v[i].y]; + + } + + ok=0; + + break; + + } + + if(nr==0&&ok==1) + + { + + r=rand()%n+1; + + ++nr; + + s[r]=1; + + ok=0; + + } + + }while(!ok); + + g< +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + +int v[2000], x[2000], y[2000], c[2000]; + + + +int valid(int i) + +{ + + switch(c[i]) + + { + + case 0: return v[x[i]] || v[y[i]]; break; + + case 1: return v[x[i]] || !v[y[i]]; break; + + case 2: return v[y[i]] || !v[x[i]]; break; + + case 3: return v[x[i]]+v[y[i]] != 2; + + } + + return 1; + +} + + + +void random(int i) + +{ + + int r = rand()%2; r++; + + if(r == 1) + + v[x[i]] = !v[x[i]]; + + if(r == 2) + + v[y[i]] = !v[y[i]]; + +} + + + +int main() + +{ + + ifstream in("party.in"); + + ofstream out("party.out"); + + + + int i, n, m, nr=0; + + + + + + in>>n>>m; + + for(i=1; i<=m; i++) + + in>>x[i]>>y[i]>>c[i]; + + + + + + for(i=1; i<=n; i++) + + v[i] = 1; + + while(1) + + { + + for(i=1; i<=m; i++) + + if(valid(i) == 0) + + { + + random(i); + + break; + + } + + if(i == m+1) + + break; + + } + + + + + + for(i=1; i<=n; i++) + + if(v[i]) + + nr++; + + out< +#include +#include + + +bool sol[115]; + + + +struct relation + +{ + + int x, y, type; + +} x[1024]; + + + +bool bad(relation A) + +{ + + if (A.type == 0 && sol[A.x] == 0 && sol[A.y] == 0) + + return true; + + if (A.type == 1 && sol[A.x] == 0 && sol[A.y] == 1) + + return true; + + if (A.type == 2 && sol[A.x] == 1 && sol[A.y] == 0) + + return true; + + if (A.type == 3 && sol[A.x] == 1 && sol[A.y] == 1) + + return true; + + return false; + +} + + + +int main() + +{ + + int i, N, M, cnt = 0; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(0)); + + + + scanf("%d%d", &N, &M); + + for (i = 1; i <= M; i ++) + + scanf("%d%d%d", &x[i].x, &x[i].y, &x[i].type); + + + + bool isSolution; + + + + do + + { + + isSolution = true; + + for (i = 1; i <= M; i ++) + + if (bad(x[i])) + + { + + int X = x[i].x, Y = x[i].y; + + int which = rand() % 2; + + if (which == 0) + + { + + cnt = cnt - sol[X]; + + sol[X] = 1 - sol[X]; + + cnt = cnt + sol[X]; + + } + + else + + { + + cnt = cnt - sol[Y]; + + sol[Y] = 1 - sol[Y]; + + cnt = cnt + sol[Y]; + + } + + isSolution = false; + + break; + + } + + + + if (cnt == 0 && isSolution == true) + + { + + sol[rand() % N + 1] = 1; cnt ++; + + isSolution = false; + + } + + } + + while (isSolution == false); + + + + printf("%d\n", cnt); + + for (i = 1; i <= N; i ++) + + if (sol[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/72da6caf-02b2-11ec-aa8b-2cf05d84bfc2.cpp b/party/2sat_random/72da6caf-02b2-11ec-aa8b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..261e7210f81abaa8c93dce4905bbb04f57d6f595 --- /dev/null +++ b/party/2sat_random/72da6caf-02b2-11ec-aa8b-2cf05d84bfc2.cpp @@ -0,0 +1,156 @@ +#include +#include +#include + + + + + + +bool sol[115]; + + + +struct relation + +{ + + int x, y, type; + +} x[1024]; + + + +bool bad(relation A) + +{ + + if (A.type == 0 && sol[A.x] == 0 && sol[A.y] == 0) + + return true; + + if (A.type == 1 && sol[A.x] == 0 && sol[A.y] == 1) + + return true; + + if (A.type == 2 && sol[A.x] == 1 && sol[A.y] == 0) + + return true; + + if (A.type == 3 && sol[A.x] == 1 && sol[A.y] == 1) + + return true; + + return false; + +} + + + +int main() + +{ + + int i, N, M, cnt = 0; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(0)); + + + + scanf("%d%d", &N, &M); + + for (i = 1; i <= M; i ++) + + scanf("%d%d%d", &x[i].x, &x[i].y, &x[i].type); + + + + bool isSolution; + + + + do + + { + + isSolution = true; + + for (i = 1; i <= M; i ++) + + if (bad(x[i])) + + { + + int X = x[i].x, Y = x[i].y; + + int which = rand() % 2; + + if (which == 0) + + { + + cnt = cnt - sol[X]; + + sol[X] = 1 - sol[X]; + + cnt = cnt + sol[X]; + + } + + else + + { + + cnt = cnt - sol[Y]; + + sol[Y] = 1 - sol[Y]; + + cnt = cnt + sol[Y]; + + } + + isSolution = false; + + break; + + } + + + + if (cnt == 0 && isSolution == true) + + { + + sol[rand() % N + 1] = 1; cnt ++; + + isSolution = false; + + } + + } + + while (isSolution == false); + + + + printf("%d\n", cnt); + + for (i = 1; i <= N; i ++) + + if (sol[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/72f98d6e-02b2-11ec-9a56-2cf05d84bfc2.cpp b/party/2sat_random/72f98d6e-02b2-11ec-9a56-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98f27b7f0f4ad1c9eec2d6a1de6b347783dc129d --- /dev/null +++ b/party/2sat_random/72f98d6e-02b2-11ec-9a56-2cf05d84bfc2.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("party.in"); + +ofstream fout("party.out"); +int N, M; + +bool Friends[102]; + +struct Relation {int type, x, y;}; + +Relation R[1002]; + +void read(){ + + fin >> N >>M; + + for(int i = 1; i <= M; i++) + + fin >> R[i].x >> R[i].y >> R[i].type; + + + +} + +inline bool verifica(int i, bool a, bool b){ + + + + if(R[i].type == 0) return a || b; + + + + if(R[i].type == 1) return a || !b; + + + + if(R[i].type == 2) return b || !a; + + + + if(R[i].type == 3) return a + b != 2; + + + + + + + +} + +int main(){ + + read(); + + + + srand(time(NULL)); + + + + for(int i = 1; i <= N; i++) + + Friends[i] = true; + + int i; + + + + for(i = 1; i <= M; i++) + + if(!verifica(i, Friends[R[i].x] , Friends[R[i].y])){ + + bool r = rand() % 2; + + if(r) Friends[R[i].y] = !Friends[R[i].y]; + + else Friends[R[i].x] = !Friends[R[i].x]; + + + + i = 0; + + } + + int Number_Friends = 0; + + for(int i = 1; i <= N; i++) + + if(Friends[i] == true) + + Number_Friends++; + + + + fout < +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + + + +int v[2000], x[2000], y[2000], c[2000]; + + + +int valid(int i) + +{ + + switch(c[i]) + + { + + case 0: return v[x[i]] || v[y[i]]; break; + + case 1: return v[x[i]] || !v[y[i]]; break; + + case 2: return v[y[i]] || !v[x[i]]; break; + + case 3: return v[x[i]]+v[y[i]] != 2; + + } + + return 1; + +} + + + +void random(int i) + +{ + + int r = rand()%2; r++; + + if(r == 1) + + v[x[i]] = !v[x[i]]; + + if(r == 2) + + v[y[i]] = !v[y[i]]; + +} + + + +int main() + +{ + + ifstream in("party.in"); + + ofstream out("party.out"); + + + + int i, n, m, nr=0; + + + + + + in>>n>>m; + + for(i=1; i<=m; i++) + + in>>x[i]>>y[i]>>c[i]; + + + + + + for(i=1; i<=n; i++) + + v[i] = 1; + + while(1) + + { + + for(i=1; i<=m; i++) + + if(valid(i) == 0) + + { + + random(i); + + break; + + } + + if(i == m+1) + + break; + + } + + + + + + for(i=1; i<=n; i++) + + if(v[i]) + + nr++; + + out< +#include +using namespace std; + +ifstream f("party.in"); + +ofstream g("party.out"); + +int n,m,i,ok,nr,r,s[101]; + +struct muchie{int x,y,c;}; + +muchie v[1001]; + +int egresit(muchie a) + +{ + + if(a.c==0&&s[a.x]==0&&s[a.y]==0) + + return 1; + + if(a.c==1&&s[a.x]==0&&s[a.y]==1) + + return 1; + + if(a.c==2&&s[a.x]==1&&s[a.y]==0) + + return 1; + + if(a.c==3&&s[a.x]==1&&s[a.y]==1) + + return 1; + + return 0; + +} + +int main() + +{ + + f>>n>>m; + + for(i=1;i<=m;++i) + + f>>v[i].x>>v[i].y>>v[i].c; + + do + + { + + ok=1; + + for(i=1;i<=m;++i) + + if(egresit(v[i])) + + { + + r=rand()%2; + + if(r==0) + + { + + nr-=s[v[i].x]; + + s[v[i].x]=1-s[v[i].x]; + + nr+=s[v[i].x]; + + } + + else + + { + + nr-=s[v[i].y]; + + s[v[i].y]=1-s[v[i].y]; + + nr+=s[v[i].y]; + + } + + ok=0; + + break; + + } + + if(nr==0&&ok==1) + + { + + r=rand()%n+1; + + ++nr; + + s[r]=1; + + ok=0; + + } + + }while(!ok); + + g< +#include +int n,m,i,nr,x,a[1001][3],v[101]; + +inline int ver(int i) + +{ + + switch(a[i][2]) + + { + + case 0: + + return v[a[i][0]] || v[a[i][1]]; + + break; + + case 1: + + return v[a[i][0]] || !v[a[i][1]]; + + break; + + case 2: + + return v[a[i][1]] || !v[a[i][0]]; + + break; + + case 3: + + return v[a[i][0]]+v[a[i][1]]!=2; + + break; + + } + + return 1; + +} + +inline void correct(int i) + +{ + + x=rand()%2; + + v[a[i][x]]=!v[a[i][x]]; + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for(i=1;i<=m;i++) + + { + + scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]); + + } + + for(i=1;i<=n;i++) v[i]=1; + + while(1) + + { + + for(i=1;i<=m;i++) + + if(!ver(i)) + + { + + correct(i); + + break; + + } + + if(i==m+1) break; + + } + + for(i=1;i<=n;i++) + + if(v[i]) nr++; + + printf("%d\n",nr); + + for(i=1;i<=n;i++) + + if(v[i]) printf("%d\n",i); + + fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/75714ff7-02b2-11ec-aec3-2cf05d84bfc2.cpp b/party/2sat_random/75714ff7-02b2-11ec-aec3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5253660c49686413b9e557ae4f3d1e4a65b4fb1b --- /dev/null +++ b/party/2sat_random/75714ff7-02b2-11ec-aec3-2cf05d84bfc2.cpp @@ -0,0 +1,105 @@ +#include +#include +struct rel{int a,b,z;}; + +int n,m; + +rel x[1000]; + +char v[101]; + + + +void citire() + +{FILE *fin=fopen("party.in","rt"); + +fscanf(fin,"%d%d",&n,&m); + +int i; + +for (i=0;i +#include +#include + + +using namespace std; + + + + + +ifstream f("party.in"); + +ofstream g("party.out"); + +int C[10*102],n,m,i,Nr; + +struct inv { + + int x,y; + + int c; + +} + +v[10*102]; + +int verificare( int k ) { + + + + if( k==0) + + return C[v[i].x] || C[v[i].y] ; + + + + if(k==1) + + return C[v[i].x] || !C[v[i].y]; + + + + if(k==2) + + return C[v[i].y]||!C[v[i].x]; + + + + if(k==3) + + return C[v[i].x]+C[v[i].y]!=2 ; + + + + return 0; + +} + +int main () { + + + + f>>n>>m; + + + + for(i=1;i<=m;i++) + + f>>v[i].x>>v[i].y>>v[i].c; + + for(i=1;i<=n;i++) + + C[i]=1; + + + + for(i=1;i<=m;i++){ + + + + if(!verificare(v[i].c)) { + + int a=rand()%2; + + + + if( a==1 ) { + + C[v[i].y]=!C[v[i].y]; + + } + + else { + + C[v[i].x]=!C[v[i].x]; + + } + + i=0; + + } + + } + + for(i=1;i<=n;i++) + + if(C[i]){ + + ++Nr; + + } + + g< +#include +using namespace std; + +struct cerinta + +{ + + int x,y,z; + +} str[1001]; + +bool v[101]; + +inline bool check(int i) + +{ + + if(str[i].z==0) + + return v[str[i].x] || v[str[i].y]; + + if(str[i].z==1) + + return v[str[i].x] || !v[str[i].y]; + + if(str[i].z==2) + + return v[str[i].y] || !v[str[i].x]; + + if(str[i].z==3) + + return v[str[i].x]+v[str[i].y]!=2; + + return false; + +} + +int main() + +{ + + int n,m,i; + + ifstream q("party.in"); + + ofstream w("party.out"); + + q>>n>>m; + + for(i=1; i<=m; i++) + + q>>str[i].x>>str[i].y>>str[i].z; + + for(i=1; i<=n; i++) + + v[i]=true; + + for(i=1; i<=m; i++) + + if(!check(i)) + + { + + bool ii = rand()%2; + + if(ii) + + v[str[i].y] = !v[str[i].y]; + + else + + v[str[i].x] = !v[str[i].x]; + + i=0; + + } + + m = 0; + + for(i=1; i<=n; i++) + + if(v[i]) + + m++; + + w< +#include +struct rel{int a,b,z;}; + +int n,m; + +rel x[1000]; + +char v[101]; + + + +void citire() + +{FILE *fin=fopen("party.in","rt"); + +fscanf(fin,"%d%d",&n,&m); + +int i; + +for (i=0;i +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char use[101]; + + + +int main() + +{ + + int i,pos,N,M,end,ok; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) use[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==2) + + { + + if(use[v[i].x]!=0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(!ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==2) + + { + + if(use[v[i].x]!=0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(!ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + } + + + + freopen("party.out","w",stdout); + + + + use[0]=0; + + for(i=1;i<=N;i++) use[0]+=use[i]; + + + + printf("%d\n",use[0]); + + + + for(i=1;i<=N;i++) if(use[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_random/793c643e-02b2-11ec-a405-2cf05d84bfc2.cpp b/party/2sat_random/793c643e-02b2-11ec-a405-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2b3914f8db4a87c9da392ba0ab7c60d6c1d70c4 --- /dev/null +++ b/party/2sat_random/793c643e-02b2-11ec-a405-2cf05d84bfc2.cpp @@ -0,0 +1,250 @@ +#include +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char use[101]; + + + +int main() + +{ + + int i,pos,N,M,end,ok; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) + + { + + scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + if(v[i].z==2) + + { + + v[i].z=v[i].x; + + v[i].x=v[i].y; + + v[i].y=v[i].z; + + v[i].z=1; + + } + + } + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) use[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + end=0; + + break; + + } + + } + + } + + + + freopen("party.out","w",stdout); + + + + use[0]=0; + + for(i=1;i<=N;i++) use[0]+=use[i]; + + printf("%d\n",use[0]); + + for(i=1;i<=N;i++) + + if(use[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_random/795b36dc-02b2-11ec-877f-2cf05d84bfc2.cpp b/party/2sat_random/795b36dc-02b2-11ec-877f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01910968c89011532f2c66138b9d260b29d266fb --- /dev/null +++ b/party/2sat_random/795b36dc-02b2-11ec-877f-2cf05d84bfc2.cpp @@ -0,0 +1,280 @@ +#include +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char use[101]; + + + +int main() + +{ + + int i,pos,N,M,end,ok; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) use[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==2) + + { + + if(use[v[i].x]!=0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==2) + + { + + if(use[v[i].x]!=0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + } + + + + freopen("party.out","w",stdout); + + + + use[0]=0; + + for(i=1;i<=N;i++) use[0]+=use[i]; + + + + printf("%d\n",use[0]); + + + + for(i=1;i<=N;i++) if(use[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_random/79799455-02b2-11ec-b518-2cf05d84bfc2.cpp b/party/2sat_random/79799455-02b2-11ec-b518-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83031dc37e8d78a9cb27b1988aae82c8e24a9ace --- /dev/null +++ b/party/2sat_random/79799455-02b2-11ec-b518-2cf05d84bfc2.cpp @@ -0,0 +1,250 @@ +#include +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char use[101]; + + + +int main() + +{ + + int i,pos,N,M,end,ok; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) + + { + + scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + if(v[i].z==2) + + { + + v[i].z=v[i].x; + + v[i].x=v[i].y; + + v[i].y=v[i].z; + + v[i].z=1; + + } + + } + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) use[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + ok=rand()%2; + + if(ok) use[v[i].x]=1-use[v[i].x]; + + else use[v[i].y]=1-use[v[i].y]; + + end=0; + + break; + + } + + } + + } + + + + freopen("party.out","w",stdout); + + + + use[0]=0; + + for(i=1;i<=N;i++) use[0]+=use[i]; + + printf("%d\n",use[0]); + + for(i=1;i<=N;i++) + + if(use[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_random/7997cab3-02b2-11ec-8e7f-2cf05d84bfc2.cpp b/party/2sat_random/7997cab3-02b2-11ec-8e7f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1f758658ecaf5d3a7d87a0dfcca5eb0de1e4e25 --- /dev/null +++ b/party/2sat_random/7997cab3-02b2-11ec-8e7f-2cf05d84bfc2.cpp @@ -0,0 +1,228 @@ +#include +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char use[101]; + + + +int main() + +{ + + int i,pos,N,M,end; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) + + { + + scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + if(v[i].z==2) + + { + + v[i].z=v[i].x; + + v[i].x=v[i].y; + + v[i].y=v[i].z; + + v[i].z=1; + + } + + } + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) use[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==3) + + { + + if(use[v[i].x]==1&&use[v[i].y]==1) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==0) + + { + + if(use[v[i].x]==0&&use[v[i].y]==0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(use[v[i].x]==0&&use[v[i].y]!=0) + + { + + end=0; + + break; + + } + + } + + + + if(!end) + + { + + pos=rand()%2; + + if(pos) use[v[i].x]=!use[v[i].x]; + + else use[v[i].y]=!use[v[i].y]; + + } + + } + + + + freopen("party.out","w",stdout); + + + + use[0]=0; + + for(i=1;i<=N;i++) use[0]+=use[i]; + + printf("%d\n",use[0]); + + for(i=1;i<=N;i++) + + if(use[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/party/2sat_random/7a7143ca-02b2-11ec-8cc1-2cf05d84bfc2.cpp b/party/2sat_random/7a7143ca-02b2-11ec-8cc1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac27d71d570be3c73909fd54ca4bf49e19c4de90 --- /dev/null +++ b/party/2sat_random/7a7143ca-02b2-11ec-8cc1-2cf05d84bfc2.cpp @@ -0,0 +1,191 @@ +#include +#include +#include +#include +#include +using namespace std; +int n,m; + +pair< pair< int,int >,int > cer[1010]; + +bitset< 110 > rez; + + + +inline void citire() { + + scanf("%d%d",&n,&m); + + for(int i=1; i<=m; ++i) + + scanf("%d%d%d",&cer[i].first.first,&cer[i].first.second,&cer[i].second); + +} + + + +inline bool vezi(int x) { + + int a = cer[x].first.first; + + int b = cer[x].first.second; + + if(cer[x].second==0) + + return (rez[a]==1 || rez[b]==1); + + if(cer[x].second==1) + + return (rez[a]==1 || (rez[a]==0 && rez[b]==0)); + + if(cer[x].second==2) + + return (rez[b]==1 || (rez[a]==0 && rez[b]==0)); + + return (rez[a]==0 || rez[b]==0); + +} + + + +inline void rezolva() { + + srand(time(NULL)); + + + + int cati0 = n,unde; + + bool go = true,ok; + + int a,b; + + + + while(go) { + + ok = true; + + for(int i=1; i<=m && ok; ++i) { + + if(vezi(i)==false) { + + unde = i; + + ok = false; + + } + + } + + + + if(ok==false) { + + a = cer[unde].first.first; + + b = cer[unde].first.second; + + if(rand()&1) { + + rez[a] = ( (rez[a]==1) ? (0) : (1) ); + + if(rez[a]==0) + + ++cati0; + + else + + --cati0; + + } else { + + rez[b] = ( (rez[b]==1) ? (0) : (1) ); + + if(rez[b]==0) + + ++cati0; + + else + + --cati0; + + } + + continue; + + } + + + + if(cati0==n) { + + a = rand()%n + 1; + + rez[a] = 1; + + --cati0; + + continue; + + } + + + + go = false; + + } + +} + + + +inline void scrie() { + + int nrez = 0; + + for(int i=1; i<=n; ++i) { + + if(rez[i]==1) + + ++nrez; + + } + + + + printf("%d\n",nrez); + + for(int i=1; i<=n; ++i) { + + if(rez[i]==1) + + printf("%d\n",i); + + } + +} + + + + + +int main() { + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + citire(); + + rezolva(); + + scrie(); + + + + return 0; + +} diff --git a/party/2sat_random/7a91eb22-02b2-11ec-9b3d-2cf05d84bfc2.cpp b/party/2sat_random/7a91eb22-02b2-11ec-9b3d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a8b4a1fc392b36e1d0780c57dd11ceccb3070c --- /dev/null +++ b/party/2sat_random/7a91eb22-02b2-11ec-9b3d-2cf05d84bfc2.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 1010; + +const int MMAX = 10010; + + + +int N, M; + +int Bl[MMAX][4]; + +int val[NMAX]; + + + +void citire() + +{ + + cin >> N >> M; + + for(int i = 1 ; i <= M ; i++) + + cin >> Bl[i][1] >> Bl[i][2] >> Bl[i][0]; + +} + + + +bool corect(int rel, int x, int y) + +{ + + switch(rel) + + { + + case 0: return(x || y); + + case 1: return(x || (!x && !y)); + + case 2: return(y || (!y && !x)); + + case 3: return(!x || !y); + + } + +} + + + +bool dif_zero() + +{ + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + return 1; + + return 0; + +} + + + +void rezolva() + +{ + + int gasit, esuat; + + srand(time(NULL)); + + for(int i = 1 ; i <= N ; i++) + + val[i] = rand()%2; + + for(int i = 1 ; i <= 50 * N ; i++) + + { + + gasit = 1; esuat = 1; + + for(int j = 1 ; j <= M ; j++) + + if(!corect(Bl[j][0], val[Bl[j][1]], val[Bl[j][2]])) + + { + + gasit = 0; + + esuat = j; + + break; + + } + + if(gasit == 1 && dif_zero()) + + return; + + if(rand() % 2 + 1 == 1) + + val[Bl[esuat][1]] ^= 1; + + else + + val[Bl[esuat][2]] ^= 1; + + } + + + +} + + + +void scriere() + +{ + + int NR = 0; + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + NR++; + + printf("%d\n", NR); + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + printf("%d\n", i); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + citire(); + + rezolva(); + + scriere(); + + return 0; + +} diff --git a/party/2sat_random/7adc88bc-02b2-11ec-bc79-2cf05d84bfc2.cpp b/party/2sat_random/7adc88bc-02b2-11ec-bc79-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89a8b4a1fc392b36e1d0780c57dd11ceccb3070c --- /dev/null +++ b/party/2sat_random/7adc88bc-02b2-11ec-bc79-2cf05d84bfc2.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 1010; + +const int MMAX = 10010; + + + +int N, M; + +int Bl[MMAX][4]; + +int val[NMAX]; + + + +void citire() + +{ + + cin >> N >> M; + + for(int i = 1 ; i <= M ; i++) + + cin >> Bl[i][1] >> Bl[i][2] >> Bl[i][0]; + +} + + + +bool corect(int rel, int x, int y) + +{ + + switch(rel) + + { + + case 0: return(x || y); + + case 1: return(x || (!x && !y)); + + case 2: return(y || (!y && !x)); + + case 3: return(!x || !y); + + } + +} + + + +bool dif_zero() + +{ + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + return 1; + + return 0; + +} + + + +void rezolva() + +{ + + int gasit, esuat; + + srand(time(NULL)); + + for(int i = 1 ; i <= N ; i++) + + val[i] = rand()%2; + + for(int i = 1 ; i <= 50 * N ; i++) + + { + + gasit = 1; esuat = 1; + + for(int j = 1 ; j <= M ; j++) + + if(!corect(Bl[j][0], val[Bl[j][1]], val[Bl[j][2]])) + + { + + gasit = 0; + + esuat = j; + + break; + + } + + if(gasit == 1 && dif_zero()) + + return; + + if(rand() % 2 + 1 == 1) + + val[Bl[esuat][1]] ^= 1; + + else + + val[Bl[esuat][2]] ^= 1; + + } + + + +} + + + +void scriere() + +{ + + int NR = 0; + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + NR++; + + printf("%d\n", NR); + + for(int i = 1 ; i <= N ; i++) + + if(val[i]) + + printf("%d\n", i); + +} + + + +int main() + +{ + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + citire(); + + rezolva(); + + scriere(); + + return 0; + +} diff --git a/party/2sat_random/7b1dd776-02b2-11ec-b5c3-2cf05d84bfc2.cpp b/party/2sat_random/7b1dd776-02b2-11ec-b5c3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c382bf12cc5663628da10d8dcaecfbd097daad1 --- /dev/null +++ b/party/2sat_random/7b1dd776-02b2-11ec-b5c3-2cf05d84bfc2.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include + + +using namespace std; + +int n, m; + +int x[1001], y[1001], tip[1001]; +int use[101]; + + +inline bool isOk (int i) +{ + if (tip[i] == 0) + { + return (use[x[i]] || use[y[i]]); + } + + if (tip[i] == 1) + { + return (use[x[i]] || !use[y[i]]); + + } + + if (tip[i] == 2) + { + return (use[y[i]] || !use[x[i]]); + + } + + if (tip[i] == 3) + { + return (!use[x[i]] || !use[y[i]]); + } + + + return false; + +} +void solve () +{ + int ok = 1; + + int i, j; + + for (i = 1; i <= n; ++i) + use[i] = 1; + + while (ok) + { + ok = 0; + for (i = 1; i <= m; ++i) + if (isOk (i) == false) + { + ok = 1; + int p = rand () & 1; + if (p) + use[x[i]] ^= 1; + else + use[y[i]] ^= 1; + } + + } + + int nr = 0; + + for (i = 1; i <= n; ++i) + if (use[i]) + ++nr; + + printf ("%d\n", nr); + for (i = 1; i <= n; ++i) + if (use[i]) + printf ("%d ", i); + printf ("\n"); + + +} + + +int main () +{ + srand (time (0)); + + freopen ("party.in", "r", stdin); + freopen ("party.out", "w", stdout); + + scanf ("%d %d\n", &n, &m); + + int i; + + for (i = 1; i <= m; ++i) + scanf ("%d %d %d\n", &x[i], &y[i], &tip[i]); + + solve (); + + + return 0; +} diff --git a/party/2sat_random/7b3e30bf-02b2-11ec-9d62-2cf05d84bfc2.cpp b/party/2sat_random/7b3e30bf-02b2-11ec-9d62-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3bd31f09bc259a8692bd88ad8122a493240ecad0 --- /dev/null +++ b/party/2sat_random/7b3e30bf-02b2-11ec-9d62-2cf05d84bfc2.cpp @@ -0,0 +1,104 @@ +#include +#include +#include + + +int n,m,sol[105]; +struct muchie +{ + int a,b,c; +}; +muchie A[1005]; +void read() +{ + scanf("%d%d",&n,&m); + int i; + for (i=1; i<=m; i++) + scanf("%d%d%d",&A[i].a,&A[i].b,&A[i].c); +} +inline int eval(int tip,int x,int y) +{ + if (tip==0) + + return x | y; + + if (tip==1) + + return x | (!x && !y); + + if (tip==2) + + return y | (!x && !y); + + if (tip==3) + + return !x | !y; + + return 1; +} + +inline int exista() + +{ + + int i; + + for (i=1; i<=n; i++) + + if (sol[i]) + + return 1; + + return 0; + +} +void solve() +{ + srand(time(0)); + int i,j,found,failed; + for (i=1; i<=n; i++) + sol[i]=rand()%2; + for (i=1; i<=n*20; i++) + { + found=1; failed=1; + for (j=1; j<=m; j++) + if (!eval(A[j].c,sol[A[j].a],sol[A[j].b])) + { + found=0; + failed=j; + break ; + } + if (found) + + { + + if (exista()) + + return ; + } + if (rand()%2==0) + sol[A[failed].a]^=1; + else + sol[A[failed].b]^=1; + } +} +void show() +{ + int i,nr=0; + for (i=1; i<=n; i++) + if (sol[i]) + nr++; + printf("%d\n",nr); + for (i=1; i<=n; i++) + if (sol[i]) + printf("%d\n",i); +} +int main() +{ + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read(); + solve(); + show(); + return 0; +} diff --git a/party/2sat_random/7b9d8ee0-02b2-11ec-a071-2cf05d84bfc2.cpp b/party/2sat_random/7b9d8ee0-02b2-11ec-a071-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..463b7949680d4fe7e66b7229213ab33c243f3e28 --- /dev/null +++ b/party/2sat_random/7b9d8ee0-02b2-11ec-a071-2cf05d84bfc2.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +inline int get_rez(int tip, int x, int y) { + + if (tip == 0 && (x || y)) + + return 1; + + + + if (tip == 1 && (x || (!x && !y))) + + return 1; + + + + if (tip == 2 && (y || (!x && !y))) + + return 1; + + + + if (tip == 3 && (!x || !y)) + + return 1; + + + + return 0; + +} + + + +int main() { + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + srand(time(0)); + + + + int n, m, value[110], edge[3][1010]; + + + + scanf("%d %d", &n, &m); + + for (int i = 1; i <= m; i++) + + scanf("%d %d %d", &edge[0][i], &edge[1][i], &edge[2][i]); + + memset(value, 0, sizeof(value)); + + + + int ok = 0; + + while (!ok) { + + ok = 1; + + for (int i = 1; i <= m; i++) { + + int rez = get_rez(edge[2][i], value[edge[0][i]], value[edge[1][i]]); + + + + if (rez == 0) { + + ok = 0; + + + + int poz = rand() % 2; + + value[edge[poz][i]] = 1 - value[edge[poz][i]]; + + } + + } + + } + + + + int nrsol = 0; + + for (int i = 1; i <= n; i++) + + nrsol += value[i]; + + + + printf("%d\n", nrsol); + + for (int i = 1; i <= n; i++) + + if (value[i]) + + printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/7c063ba1-02b2-11ec-980b-2cf05d84bfc2.cpp b/party/2sat_random/7c063ba1-02b2-11ec-980b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d950119fba34f9233eadff11c3c183575a9cc001 --- /dev/null +++ b/party/2sat_random/7c063ba1-02b2-11ec-980b-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include +struct muchie{ + + int x,y,caz; + +}; + + + +int fol[1024]; + +muchie a[1024]; + +int nr,i,n,m,aux,ok; + + + +bool corect(int i) + +{ + + if (a[i].caz == 0) + + return (fol[a[i].x] || fol[a[i].y]); + + if (a[i].caz == 3) + + return ((!fol[a[i].x]) || (!fol[a[i].y])); + + return (fol[a[i].x] || (!fol[a[i].y])); + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for (i=1; i<=m; i++){ + + scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].caz); + + if (a[i].caz==2) { + + aux = a[i].x; + + a[i].x = a[i].y; + + a[i].y = aux; + + a[i].caz = 1; + + } + + } + + for (i=1; i<=n; i++) + + fol[i]=1; + + for (ok=0; !ok; ){ + + ok = 1; + + for (i=1; i<=m; i++) + + if (!corect(i)) { + + ok=0; + + aux = rand()%2; + + if (aux ==0) + + fol[a[i].x] ^=1; + + else + + fol[a[i].y] ^=1; + + } + + } + + for (i=1; i<=n; i++) + + nr +=fol[i]; + + printf("%d\n",nr); + + for (i=1; i<=n; i++) + + if (fol[i]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_random/7c49d464-02b2-11ec-b940-2cf05d84bfc2.cpp b/party/2sat_random/7c49d464-02b2-11ec-b940-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2232250115a0e34fad7309e9b06d2079ecb084fe --- /dev/null +++ b/party/2sat_random/7c49d464-02b2-11ec-b940-2cf05d84bfc2.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +struct relatie {int x, y, tip;} aux; + +int n, m, x, y, tip, i, nr, p, ok; + +int sol[105]; + +vector R; + + + +int main () { + + + + srand (time (0)); + + freopen ("party.in", "r", stdin); + + freopen ("party.out", "w", stdout); + + + + scanf ("%d %d", &n, &m); + + for (i = 1; i <= n; i++) + + sol[i] = rand () %2; + + + + for (i = 1; i <= m; i++) { + + scanf ("%d %d %d", &aux.x, &aux.y, &aux.tip); + + R.push_back (aux); + + } + + + + while (!ok) { + + + + ok = 1; + + for (i = 0; i < m; i++) { + + aux = R[i]; + + if (aux.tip == 0) { + + if (!sol[aux.x] && !sol[aux.y]) { + + p = i; + + ok = 0; + + break; + + } + + } + + + + else { + + if (aux.tip == 1) { + + if (!sol[aux.x] && sol[aux.y]) { + + p = i; + + ok = 0; + + break; + + } + + } + + + + else { + + if (aux.tip == 2) { + + if (sol[aux.x] && !sol[aux.y]) { + + p = i; + + ok = 0; + + break; + + } + + } + + + + else { + + if (sol[aux.x] && sol[aux.y]) { + + p = i; + + ok = 0; + + break; + + } + + } + + } + + } + + } + + + + if (!ok ) { + + aux = R[p]; + + if (rand () % 2) + + sol[aux.x]^= 1; + + else + + sol[aux.y]^= 1; + + + + + + } + + } + + + + for (i = 1; i <= n; i++) + + nr+= sol[i]; + + + + printf ("%d\n", nr); + + for (i = 1; i <= n; i++) + + if (sol[i]) printf ("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/7c69b88c-02b2-11ec-9c0a-2cf05d84bfc2.cpp b/party/2sat_random/7c69b88c-02b2-11ec-9c0a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..270fe654862bf8b5a5a36d516d2ff1b22ccb4692 --- /dev/null +++ b/party/2sat_random/7c69b88c-02b2-11ec-9c0a-2cf05d84bfc2.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +using namespace std; +int n,m,sol[102]; + +pair v[1002]; + + + +inline int abs(int a){return a<0?-a:a;} + + + +inline bool cond(int i) + +{ + + int x=sol[abs(v[i].first)],y=sol[abs(v[i].second)]; + + if(v[i].first<0) + + x^=1; + + if(v[i].second<0) + + y^=1; + + return x|y; + +} + + + +int main() + +{ + + srand(time(0)); + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + int i,j,a,b,c; + + for(i=1;i<=m;i++) + + { + + scanf("%d%d%d",&a,&b,&c); + + if(c==1) + + b=b*-1; + + if(c==2) + + a=a*-1; + + if(c==3) + + { + + a=a*-1; + + b=b*-1; + + } + + v[i]=make_pair(a,b); + + } + + for(i=1;i<=n;i++) + + sol[i]=rand()%2; + + + + while(1) + + { + + for(j=1;j<=m;j++) + + { + + if(cond(j)==0) + + { + + if(rand()&1) + + sol[abs(v[j].first)]^=1; + + else + + sol[abs(v[j].second)]^=1; + + break; + + } + + } + + if(j==m+1) + + { + + int nr=0; + + for(j=1;j<=n;j++) + + nr+=sol[j]; + + printf("%d\n",nr); + + for(j=1;j<=n;j++) + + if(sol[j]) + + printf("%d\n",j); + + return 0; + + } + + } + + return 0; + +} diff --git a/party/2sat_random/7ce88555-02b2-11ec-9692-2cf05d84bfc2.cpp b/party/2sat_random/7ce88555-02b2-11ec-9692-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f37f587d64bb61a1852812ecdc14bc9399930ed --- /dev/null +++ b/party/2sat_random/7ce88555-02b2-11ec-9692-2cf05d84bfc2.cpp @@ -0,0 +1,111 @@ +#include +#include + + + + +struct muchie{ + + int x,y,caz; + +}; + + + +int fol[1024]; + +muchie a[1024]; + +int nr,i,n,m,aux,ok; + + + +bool corect(int i) + +{ + + if (a[i].caz == 0) + + return (fol[a[i].x] || fol[a[i].y]); + + if (a[i].caz == 3) + + return ((!fol[a[i].x]) || (!fol[a[i].y])); + + return (fol[a[i].x] || (!fol[a[i].y])); + +} + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for (i=1; i<=m; i++){ + + scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].caz); + + if (a[i].caz==2) { + + aux = a[i].x; + + a[i].x = a[i].y; + + a[i].y = aux; + + a[i].caz = 1; + + } + + } + + for (i=1; i<=n; i++) + + fol[i]=1; + + for (ok=0; !ok; ){ + + ok = 1; + + for (i=1; i<=m; i++) + + if (!corect(i)) { + + ok=0; + + aux = rand()%2; + + if (aux ==0) + + fol[a[i].x] ^=1; + + else + + fol[a[i].y] ^=1; + + } + + } + + for (i=1; i<=n; i++) + + nr +=fol[i]; + + printf("%d\n",nr); + + for (i=1; i<=n; i++) + + if (fol[i]) + + printf("%d\n",i); + + return 0; + +} diff --git a/party/2sat_random/7d077f13-02b2-11ec-a3b7-2cf05d84bfc2.cpp b/party/2sat_random/7d077f13-02b2-11ec-a3b7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce1805fb44910229a39fee411720729b0438c1d8 --- /dev/null +++ b/party/2sat_random/7d077f13-02b2-11ec-a3b7-2cf05d84bfc2.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + +struct expresie{int a,b,ok;}v[1005]; + +int n,m,i,j,A,nr; + +bool a[103],ready; + + + +bool eval(expresie x){ + +if(x.ok == 0) + + return a[x.a]||a[x.b]; + +else + + if(x.ok == 1){ + + if(!a[x.a]) + + return (!a[x.b]); + + else + + return true; + + } + +else + + if(x.ok == 2){ + + if(!a[x.b]) + + return (!a[x.a]); + + else + + return true; + + } + +else + + return (!(a[x.a] && a[x.b])); + + + +} + + + +int main(){ + +freopen("party.in","r",stdin); + +freopen("party.out","w",stdout); + + + +scanf("%d %d",&n,&m); + +srand(time(0)); + +for(i = 1 ; i <= m ; i++) + + scanf("%d %d %d",&v[i].a,&v[i].b,&v[i].ok); + + + +for(i = 1; i <= n ; i++) + + a[i] = rand()%2; + +while(!ready){ + +ready = true; + + + +for(i = 1; i <= m ; i++) + + if(!eval(v[i])){ + + A = rand()%2+1; + + if(A == 1) + + a[v[i].a] = !a[v[i].a]; + + else + + a[v[i].b] = !a[v[i].b]; + + ready = false; + + } + +} + + + +for(i = 1 ; i <= n ;i++) + + if(a[i])nr++; + + + +printf("%d\n",nr); + +for(i = 1;i <= n ;i++) + + if(a[i]) + + printf("%d\n",i); + + + +return 0;} diff --git a/party/2sat_random/7d27b141-02b2-11ec-b0ad-2cf05d84bfc2.cpp b/party/2sat_random/7d27b141-02b2-11ec-b0ad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d9c9b6f5ad7ff513ed50fdc6f2c60a63a0b69b9 --- /dev/null +++ b/party/2sat_random/7d27b141-02b2-11ec-b0ad-2cf05d84bfc2.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +int n,m,x[100000],y[100000],v[100000],rez,poz,tip,nr,sol[1000000]; + + + +inline int abs(int a) { return a>=0?a:-a; } + + + +int verif(int i) + +{ + + int a,b; + + a=v[abs(x[i])]; + + b=v[abs(y[i])]; + + if (x[i]<0) + + a^=1; + + if (y[i]<0) + + b^=1; + + return a|b; + +} + + + +int main() + +{ + + int i,j; + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + scanf("%d %d", &n, &m); + + + + for (i=1;i<=m;++i) + + { + + scanf("%d %d %d", &x[i], &y[i],&tip); + + if (tip==1) y[i]=-y[i]; + + if (tip==2) x[i]=-x[i]; + + if (tip==3) x[i]=-x[i],y[i]=-y[i]; + + } + + + + + + srand(time(0)); + + for (i=1;i<=n;++i) + + v[i]=rand()%2; + + + + for (i=0;i<=n*25;++i) + + { + + rez=1; + + for (j=1;j<=m;++j) + + { + + rez&=verif(j); + + if (!rez) + + { + + poz=j; + + break; + + } + + } + + if (rez==1) + + { + + nr=0; + + for (j=1;j<=n;++j) + + if (v[j]) sol[++nr]=j; + + printf("%d\n", nr); + + for (j=1;j<=nr;++j) printf("%d\n", sol[j]); + + return 0; + + } + + if (rand()%2==0) + + v[abs(x[poz])]^=1; + + else + + v[abs(y[poz])]^=1; + + } + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + + + +} diff --git a/party/2sat_random/7db6f94d-02b2-11ec-b188-2cf05d84bfc2.cpp b/party/2sat_random/7db6f94d-02b2-11ec-b188-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0602a7d2b3430dfff43b1fc4d861d1141947f48e --- /dev/null +++ b/party/2sat_random/7db6f94d-02b2-11ec-b188-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + + + + + + +int n, m, a[101*10][3], v[101]; + + + +int main() { + + int i, ok, ct, op; + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d\n", &n, &m); + + for (i=1; i<=m; ++i) scanf("%d %d %d\n", &a[i][0], &a[i][1], &a[i][2]); + + + + srand((unsigned)time(NULL)); + + for (i=1; i<=n; ++i) v[i]=rand()%2; + + + + ok=0; + + while (!ok) { + + ok=1; + + for (i=1; i<=m && ok; ++i) + + if (a[i][2]==0) { + + if (!v[a[i][0]] && !v[a[i][1]]) { + + op=rand()%2; + + v[a[i][op]]=1; + + ok=0; + + } + + } + + else if (a[i][2]==1) { + + if (!v[a[i][0]] && v[a[i][1]]) { + + op=rand()%2; + + if (!op) v[a[i][0]]=1; + + else v[a[i][1]]=0; + + ok=0; + + } + + } + + else if (a[i][2]==2) { + + if (v[a[i][0]] && !v[a[i][1]]) { + + op=rand()%2; + + if (!op) v[a[i][0]]=0; + + else v[a[i][1]]=1; + + ok=0; + + } + + } + + else { + + if (v[a[i][0]] && v[a[i][1]]) { + + op=rand()%2; + + v[a[i][op]]=0; + + ok=0; + + } + + } + + } + + + + ct=0; + + for (i=1; i<=n; ++i) + + if (v[i]) ct++; + + + + printf("%d\n", ct); + + for (i=1; i<=n; ++i) + + if (v[i]) printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/7df8bd18-02b2-11ec-a763-2cf05d84bfc2.cpp b/party/2sat_random/7df8bd18-02b2-11ec-a763-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91dccc5ab3e9c345243d0be8ff55e63a1b41e166 --- /dev/null +++ b/party/2sat_random/7df8bd18-02b2-11ec-a763-2cf05d84bfc2.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include +using namespace std; + + + +int N, M; + +pair EXPR[100100]; + +int SOL[100100]; + + + +bool eval_term(pair T) { + + int x, y; + + + + x = SOL[abs(T.first)]; + + y = SOL[abs(T.second)]; + + + + if (T.first < 0) + + x ^= 1; + + if (T.second < 0) + + y ^= 1; + + + + return (x | y); + +} + + + +void print_sol() { + + int i; + + for (i = 1; i <= N; i++) + + printf("%d ", SOL[i]); + +} + + + + + +void read() { + + int i, a, b, c; + + scanf("%d%d", &N, &M); + + for (i = 1; i <= M; i++) { + + scanf("%d%d%d", &a, &b, &c); + + if (c == 0) + + EXPR[i] = make_pair(a, b); + + if (c == 1) + + EXPR[i] = make_pair(a, -b); + + if (c == 2) + + EXPR[i] = make_pair(-a, b); + + if (c == 3) + + EXPR[i] = make_pair(-a, -b); + + } + + + +} + + + +void afis() { + + int i, rez = 0; + + for (i = 1; i <= N; i++) + + rez += SOL[i]; + + + + printf("%d\n", rez); + + for (i = 1; i <= N; i++) + + if (SOL[i] == 1) + + printf("%d\n", i); + +} + + + +int main() { + + + + int i, a, b, P; + + + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + read(); + srand(time(0)); + + + + for (i = 1; i <= N; i++) + + SOL[i] = rand() % 2; + + + + for (int step = 0; step <= N * N * 6; step++) { + + int curr_res = 1; + + for (i = 1; i <= M; i++) { + + curr_res &= eval_term(EXPR[i]); + + if (curr_res == 0) { + + P = i; + + break; + + } + + } + + + + if (curr_res == 1) { + + afis(); + + + + return 0; + + } + + + + if (rand() % 2 == 0) + + SOL[abs(EXPR[P].first)] ^= 1; + + else + + SOL[abs(EXPR[P].second)] ^= 1; + + } + + + + printf("-1\n"); + + + + return 0; + +} diff --git a/party/2sat_random/7e16a55c-02b2-11ec-9f0b-2cf05d84bfc2.cpp b/party/2sat_random/7e16a55c-02b2-11ec-9f0b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fae78a8588c698981c156c16bc7cc8333867bc5 --- /dev/null +++ b/party/2sat_random/7e16a55c-02b2-11ec-9f0b-2cf05d84bfc2.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +struct op{ int tip,x,y; } a[1005]; + +int v[105]; + +int i,n,m,ok,x,nr; + + + +int main(){ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d%d",&n,&m); + + for(i=1;i<=m;++i) scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].tip); + + + + srand(time(NULL)); + + for(i=1;i<=n;++i) v[i]=rand()%2; + + + + for(ok=1; ok; ){ + + ok=0; + + for(i=1;i<=m;++i) + + if(a[i].tip == 0){ + + if(!v[a[i].x] && !v[a[i].y]){ + + ok=1; + + x=rand()%2; + + if(x) v[a[i].x]=1; else v[a[i].y]=1; + + } + + } else + + if(a[i].tip == 1){ + + if(!v[a[i].x] && v[a[i].y]){ + + ok=1; + + v[a[i].y]=0; + + } + + }else + + if(a[i].tip == 2){ + + if(!v[a[i].y] && v[a[i].x]){ + + ok=1; + + v[a[i].x]=0; + + } + + } else + + if(a[i].tip == 3){ + + if( v[a[i].x] && v[a[i].y]){ + + x=rand()%2; + + if(x) v[a[i].x]=0; else v[a[i].y]=0; + + ok=1; + + } + + } + + } + + + + for(i=1;i<=n;++i) nr+=v[i]; + + printf("%d\n",nr); + + for(i=1;i<=n;++i) if(v[i]) printf("%d\n",i); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/7e34dbbf-02b2-11ec-9853-2cf05d84bfc2.cpp b/party/2sat_random/7e34dbbf-02b2-11ec-9853-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd0728366917d7e1b696e962e548025a1b083cf3 --- /dev/null +++ b/party/2sat_random/7e34dbbf-02b2-11ec-9853-2cf05d84bfc2.cpp @@ -0,0 +1,142 @@ +#include +#include + + + + + + +struct cer { + + int x; + + int y; + + int t; + +}; + + + +int C[4][2][2] = {{{0,1},{1,1}}, {{1,0},{1,1}}, {{1,1},{0,1}}, {{1,1},{1,0}}}; + + + +cer v[1011]; + + + +int u[1011/10]; + + + +int n,m,ok,tot,i,aux,poz; + + + +int main(){ + + FILE *f = fopen("party.in","r"); + + fscanf(f,"%d %d",&n, &m); + + for (i = 1;i<=m;i++){ + + fscanf(f,"%d %d %d", &v[i].x, &v[i].y, &v[i].t); + + } + + fclose(f); + do{ + + ok = 1; + + for (i=1;i<=m;i++) { + + if (!C[v[i].t][u[v[i].x]][u[v[i].y]]) { + + ok = 0; + + if (aux = rand()%2) { + + u[v[i].x] = 1 - u[v[i].x]; + + } else { + + u[v[i].y] = 1 - u[v[i].y]; + + } + + break; + + } + + } + + tot = 1; + + if (ok) { + + tot = 0; + + for (i=1;i<=n;i++){ + + if (u[i]){ + + tot++; + + } + + } + + if (!tot){ + + if (aux = rand()%2) { + + u[v[1].x] = 1; + + } else { + + u[v[1].y] = 1; + + } + + continue; + + } + + } + + + + } while (!ok || !tot); + + + + tot = 0; + + for (i=1;i<=n;i++) + + if (u[i]) + + tot++; + + FILE *g = fopen("party.out","w"); + + fprintf(g,"%d\n",tot); + + for (i=1;i<=n;i++) + + if (u[i]) + + fprintf(g,"%d\n",i); + + fclose(g); + + + + + + return 0; + +} diff --git a/party/2sat_random/7e8608f7-02b2-11ec-9da0-2cf05d84bfc2.cpp b/party/2sat_random/7e8608f7-02b2-11ec-9da0-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1af38a65879ebaf54acc621e49acc0253a47280 --- /dev/null +++ b/party/2sat_random/7e8608f7-02b2-11ec-9da0-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int n, m; + +int pr[3][1024]; + +int cond[1024], val[1024]; + + + +inline int probeaza(int ind) + +{ + + if (cond[ind] == 0) + + return (val[pr[0][ind]] | val[pr[1][ind]]); + + if (cond[ind] == 3) + + return ((!val[pr[0][ind]]) | (!val[pr[1][ind]])); + + return (val[pr[0][ind]] | (!val[pr[1][ind]])); + +} + + + +int main() + +{ + + srand(time(0)); + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &n, &m); + + + + for (int i = 1; i <= m; i++) + + { + + scanf("%d %d %d", &pr[0][i], &pr[1][i], &cond[i]); + + + + if (cond[i] == 2) + + { + + swap(pr[0][i], pr[1][i]); + + cond[i] = 1; + + } + + } + + + + for (int i = 1; i <= n; i++) + + val[i] = 0; + + + + for (int ok = 0; !ok; ) + + { + + ok = 1; + + for (int i = 1; i <= m; i++) + + if (!probeaza(i)) + + { + + ok = 0; + + + + int sch = rand() % 2; + + val[pr[sch][i]] ^= 1; + + } + + } + + + + int nr = 0; + + for (int i = 1; i <= n; i++) + + nr += val[i]; + + + + printf("%d\n", nr); + + for (int i = 1; i <= n; i++) + + if (val[i]) + + printf("%d\n", i); + + + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/7ec757b9-02b2-11ec-8c73-2cf05d84bfc2.cpp b/party/2sat_random/7ec757b9-02b2-11ec-8c73-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a6e4b939653437bb95494c8d0b7c6f119b62961 --- /dev/null +++ b/party/2sat_random/7ec757b9-02b2-11ec-8c73-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + + + + + +using namespace std; + +int valoare[110]; + +struct prop{int tip,x,y;} v[1010]; + +int N,M; + + + +int main(void){ + + + + freopen("party.in","rt",stdin); + + freopen("party.out","wt",stdout); + + + + scanf("%d%d",&N,&M); + + + + for (int i=1;i<=M;++i){ + + + + scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].tip); + + } + + + + fclose(stdin); + + + + srand(time(NULL)); + + + + for (int i=1;i<=N;++i){ + + + + valoare[i]=rand()%2; + + } + + + + int ok=1; + + while (ok){ + + + + ok=0; + + for (int i=1;i<=M;++i){ + + if (v[i].tip==0){ + + + + if (valoare[v[i].x]==0 && valoare[v[i].y]==0){ + + int x=rand()%2; + + if (x){ valoare[v[i].x]=1;} else {valoare[v[i].y]=1;} + + ++ok; + + } + + } else if (v[i].tip==1){ + + + + if (valoare[v[i].x]==0 && valoare[v[i].y]==1){ valoare[v[i].y]=0; ++ok;} + + } else if (v[i].tip==2){ + + if (valoare[v[i].y]==0 && valoare[v[i].x]==1){ valoare[v[i].x]=0; ++ok;} + + } else { + + if (valoare[v[i].x]==1 && valoare[v[i].y]==1) { + + ++ok; + + int x=rand()%2; + + if (x){ valoare[v[i].x]=0;} else {valoare[v[i].y]=0;} + + } + + } + + } + + } + + int nrinv=0; + + + + for (int i=1;i<=N;++i) nrinv+=valoare[i]; + + printf("%d\n",nrinv); + + + + for (int i=1;i<=N;++i) + + if (valoare[i]) printf("%d\n",i); + + + + fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/7ee617c0-02b2-11ec-8773-2cf05d84bfc2.cpp b/party/2sat_random/7ee617c0-02b2-11ec-8773-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65625e5f2df3a6936a027a7dbd7f1d8dcc710fe1 --- /dev/null +++ b/party/2sat_random/7ee617c0-02b2-11ec-8773-2cf05d84bfc2.cpp @@ -0,0 +1,190 @@ +#include +#include +#include + + + + +int a[1005][2],viz[105]; + +int n,m,nrt; + +void read () + +{ + + int i,x,y,op; + + scanf ("%d%d",&n,&m); + + for (i=1; i<=m; ++i) + + { + + scanf ("%d%d%d",&x,&y,&op); + + if (!op) + + { + + a[i][0]=x; + + a[i][1]=y; + + } + + else if (op==1) + + { + + a[i][0]=x; + + a[i][1]=-y; + + } + + else if (op==2) + + { + + a[i][0]=-x; + + a[i][1]=y; + + } + + else if (op == 3) + + { + + a[i][0]=-x; + + a[i][1]=-y; + + } + + } + +} + +void solve () + +{ + + int i,ok,x,y,op; + + for (i=1; i<=n; ++i) + + viz[i]=1; + + for (ok=1; ok; ) + + { + + ok=0; + + for (i=1; i<=m; ++i) + + { + + if (a[i][0]>0) + + x=viz[a[i][0]]; + + else + + x=!viz[-a[i][0]]; + + if (a[i][1]>0) + + y=viz[a[i][1]]; + + else + + y=!viz[-a[i][1]]; + + if (!x && !y) + + { + + op = rand(); + + if (op%2==0) + + { + + x=a[i][0]; + + if (x<0) + + x=-x; + + viz[x]^=1; + + } + + else + + { + + y=a[i][1]; + + if (y<0) + + y=-y; + + viz[y]^=1; + + } + + ok=1; + + } + + } + + } + + for (i=1; i<=n; ++i) + + if (viz[i]) + + ++nrt; + +} + +void print () + +{ + + int i; + + printf ("%d\n",nrt); + + for (i=1; i<=n; ++i) + + if (viz[i]) + + printf ("%d\n",i); + +} + +int main () + +{ + + freopen ("party.in","r",stdin); + + freopen ("party.out","w",stdout); + + srand (time (0)); + + read (); + + solve (); + + print (); + + return 0; + +} diff --git a/party/2sat_random/7f0649e6-02b2-11ec-aa0a-2cf05d84bfc2.cpp b/party/2sat_random/7f0649e6-02b2-11ec-aa0a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d348fcae305d4c3e2516068b99b81d474b9d22fe --- /dev/null +++ b/party/2sat_random/7f0649e6-02b2-11ec-aa0a-2cf05d84bfc2.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +int val[128], a[1024][2], n,m; + + + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + + + srand(time(0)); + + + + scanf("%d%d", &n,&m); + + + + for (int i=1;i<=n;++i) val[i] = 1; + + + + int x,y,op; + + + + for (int i=1;i<=m;++i) + + { + + scanf("%d%d%d", &x,&y,&op); + + + + if (op == 0) { a[i][0] = x; a[i][1] = y; } else + + if (op == 1) { a[i][0] = x; a[i][1] = -y; } else + + if (op == 2) { a[i][0] = -x; a[i][1] = y; } else + + if (op == 3) { a[i][0] = -x; a[i][1] = -y; } + + } + + + + int notOK=1; + + + + while (notOK) + + { + + notOK = 0; + + for (int i=1;i<=m;++i) + + { + + if (a[i][0] > 0) x = val[a[i][0]]; else x = !val[-a[i][0]]; + + if (a[i][1] > 0) y = val[a[i][1]]; else y = !val[-a[i][1]]; + + if (y == 0 && x == 0) + + { + + op = rand(); + + if (op % 2 == 0) + + { + + x = a[i][0]; + + if (x < 0) x = -x; + + val[x] ^= 1; + + } + + else + + { + + y = a[i][1]; + + if (y < 0) y = -y; + + val[y] ^= 1; + + } + + notOK = 1; + + } + + } + + } + + + + int cnt=0; + + for (int i=1;i<=n;++i) cnt += val[i]; + + + + printf("%d\n", cnt); + + + + for (int i=1;i<=n;++i) if (val[i]==1) printf("%d\n", i); + + + + return 0; + +} diff --git a/party/2sat_random/7f24ce66-02b2-11ec-b433-2cf05d84bfc2.cpp b/party/2sat_random/7f24ce66-02b2-11ec-b433-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aa3de324c903e333890d7e679c53598fe1d8329 --- /dev/null +++ b/party/2sat_random/7f24ce66-02b2-11ec-b433-2cf05d84bfc2.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int n, m; + +int pr[3][1024]; + +int cond[1024], val[1024]; + + + +inline int probeaza(int ind) + +{ + + if (cond[ind] == 0) + + return (val[pr[0][ind]] | val[pr[1][ind]]); + + if (cond[ind] == 3) + + return ((!val[pr[0][ind]]) | (!val[pr[1][ind]])); + + return (val[pr[0][ind]] | (!val[pr[1][ind]])); + +} + + + +int main() + +{ + + srand(time(0)); + + freopen("party.in", "r", stdin); + + freopen("party.out", "w", stdout); + + + + scanf("%d %d", &n, &m); + + + + for (int i = 1; i <= m; i++) + + { + + scanf("%d %d %d", &pr[0][i], &pr[1][i], &cond[i]); + + + + if (cond[i] == 2) + + { + + swap(pr[0][i], pr[1][i]); + + cond[i] = 1; + + } + + } + + + + for (int i = 1; i <= n; i++) + + val[i] = 1; + + + + for (int ok = 0; !ok; ) + + { + + ok = 1; + + for (int i = 1; i <= m; i++) + + if (!probeaza(i)) + + { + + ok = 0; + + + + int sch = rand() % 2; + + val[pr[sch][i]] ^= 1; + + } + + } + + + + int nr = 0; + + for (int i = 1; i <= n; i++) + + nr += val[i]; + + + + printf("%d\n", nr); + + for (int i = 1; i <= n; i++) + + if (val[i]) + + printf("%d\n", i); + + + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/7f441664-02b2-11ec-ac81-2cf05d84bfc2.cpp b/party/2sat_random/7f441664-02b2-11ec-ac81-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16ed089686bd650480ca7f31e3c4188b49408284 --- /dev/null +++ b/party/2sat_random/7f441664-02b2-11ec-ac81-2cf05d84bfc2.cpp @@ -0,0 +1,181 @@ +#include +#include + + +struct rel { + + int a,b,z; + +}; + + + +int n,m; + +rel x[1000]; + +char v[101]; + + + +void citire() { + + FILE *fin = fopen("party.in", "rt"); + + + + fscanf(fin, " %d %d", &n, &m); + + + + int i; + + + + for (i = 0; i < m; ++i) + + fscanf(fin, " %d %d %d", &x[i].a, &x[i].b, &x[i].z); + + + + fclose(fin); + +} + + + +int check() { + + int i; + + + + for (i = 0; i < m; ++i) + + switch(x[i].z) { + + case 0: + + if (v[x[i].a] == 0 && v[x[i].b] == 0) + + return i; + + break; + + case 1: + + if (v[x[i].a] == 0 && v[x[i].b] == 1) + + return i; + + break; + + case 2: + + if (v[x[i].a] == 1 && v[x[i].b] == 0) + + return i; + + break; + + case 3: + + if (v[x[i].a] == 1 && v[x[i].b] == 1) + + return i; + + break; + + } + + return -1; + +} + + + +void aleator() { + + int i, k=0, q=32677; + + + + for (i = 0; i <= n; ++i) + + v[i]=0; + + + + while ((i = check()) != -1) { + + k = rand() & q; + + + + if (k < 16384) + + v[x[i].a] = 1 - v[x[i].a]; + + else + + v[x[i].b] = 1 - v[x[i].b]; + + } + + + +} + + + +void afisare() { + + FILE *fout=fopen("party.out","wt"); + + int i, nr = 0; + + + + for (i = 1;i <= n; ++i) + + if (v[i] == 1) ++nr; + + + + fprintf(fout, "%d\n", nr); + + + + for (i = 1; i <= n; ++i) + + if (v[i] == 1) + + fprintf(fout, "%d\n", i); + + + + fclose(fout); + +} + + + +int main() { + + + + citire(); + + + + aleator(); + + + + afisare(); + + + + return 0; + +} diff --git a/party/2sat_random/7ff4ed31-02b2-11ec-b622-2cf05d84bfc2.cpp b/party/2sat_random/7ff4ed31-02b2-11ec-b622-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cb73961accd139f75159eb337eb77ee8dd667d7 --- /dev/null +++ b/party/2sat_random/7ff4ed31-02b2-11ec-b622-2cf05d84bfc2.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +using namespace std; + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) break; + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< +#include +int n,m,i,nr,x,a[1001][3],v[101]; + +inline int ver(int i) + +{ + + switch(a[i][2]) + + { + + case 0: + + return v[a[i][0]] || v[a[i][1]]; + + break; + + case 1: + + return v[a[i][0]] || !v[a[i][1]]; + + break; + + case 2: + + return v[a[i][1]] || !v[a[i][0]]; + + break; + + case 3: + + return v[a[i][0]]+v[a[i][1]]!=2; + + break; + + } + + return 1; + +} + +inline void correct(int i) + +{ + + x=rand()%2; + + v[a[i][x]]=!v[a[i][x]]; + +} + +int main() + +{ + + freopen("party.in","r",stdin); + + freopen("party.out","w",stdout); + + scanf("%d %d",&n,&m); + + for(i=1;i<=m;i++) + + { + + scanf("%d %d %d",&a[i][0],&a[i][1],&a[i][2]); + + } + + for(i=1;i<=n;i++) v[i]=1; + + while(1) + + { + + for(i=1;i<=m;i++) + + if(!ver(i)) + + { + + correct(i); + + break; + + } + + if(i==m+1) break; + + } + + for(i=1;i<=n;i++) + + if(v[i]) nr++; + + printf("%d\n",nr); + + for(i=1;i<=n;i++) + + if(v[i]) printf("%d\n",i); + + fclose(stdout); + + return 0; + +} diff --git a/party/2sat_random/80bafc40-02b2-11ec-9640-2cf05d84bfc2.cpp b/party/2sat_random/80bafc40-02b2-11ec-9640-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6e826d3b9397e250ec659ff6308170cb615e368 --- /dev/null +++ b/party/2sat_random/80bafc40-02b2-11ec-9640-2cf05d84bfc2.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +using namespace std; + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + srand(time(0)); + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) break; + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< #include +#include +#include using namespace std; + + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + srand(time(0)); + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) + + if(ok()) break; + + else { c=rand()%n+1; + + x[c]=1-x[c]; + + } + + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< +#include +#include +using namespace std; + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + srand(time(0)); + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) + + if(ok()) break; + + else { c=rand()%n+1; + + x[c]=1-x[c]; + + } + + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< +#include +#include +#include +#include +using namespace std; + + + + + + + +int N, M, bad; +int v[2001], c[2001], b[2001]; +vector > exp; + +int eval(int pos) +{ + pair e = exp[pos]; + if (c[pos] == 0) + return (v[e.first] || v[e.second]); + if (c[pos] == 1) + return (v[e.first] || !v[e.second]); + if (c[pos] == 2) + return (!v[e.first] || v[e.second]); + return (!v[e.first] || !v[e.second]); +} + +int extra() +{ + int i; + for (i = (1); i <= (N); i++) + if (v[i]) return 1; + return 0; +} + +int main() +{ + int i, x, y, pas; + ifstream fin("party.in"); + ofstream fout("party.out"); + srand(time(NULL)); + fin >> N >> M; + for (i = (0); i <= (M - 1); i++) + { + fin >> x >> y >> c[i]; + exp.push_back(make_pair(x, y)); + } + + + + + for (i = (1); i <= (N); i++) v[i] = (i % 2); + + for (pas = (0); pas <= (100000); pas++) + { + bad = 0; + int ok = eval(0); + if (!eval(0)) b[++bad] = 0; + for (i = (1); i <= (M - 1); i++) + { + ok = ok && eval(i); + if (!eval(i)) b[++bad] = i; + } + + if (ok && extra()) break; + if (!bad) b[++bad] = rand() % N + 1; + + int nr = b[rand() % bad + 1], nr2 = rand() % 2; + + if (nr2) + v[exp[nr].first] = !v[exp[nr].first]; + else + v[exp[nr].second] = !v[exp[nr].second]; + } + + int res = 0; + for (i = 1; i <= N; i++) + if (v[i]) res++; + fout << res << '\n'; + for (i = (1); i <= (N); i++) + if (v[i]) fout << i << '\n'; + fin.close(), fout.close(); +} diff --git a/party/2sat_random/81381b7e-02b2-11ec-a039-2cf05d84bfc2.cpp b/party/2sat_random/81381b7e-02b2-11ec-a039-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d6b772f62fdb6be7b2e41431f9eef2077a42064 --- /dev/null +++ b/party/2sat_random/81381b7e-02b2-11ec-a039-2cf05d84bfc2.cpp @@ -0,0 +1,105 @@ +#include +#include + + + + +int X[1000],Y[1000],Z[1000],V[101],A[101],n,m,k; + + + +void read() + +{ + + int i; + + + + freopen("party.in","r",stdin); + + scanf("%d%d",&n,&m); + + for(i=0;i +#include +#include +using namespace std; + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + srand(time(0)); + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) + + if(ok()) break; + + else { c=rand()%n+1; + + x[c]=1-x[c]; + + } + + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< +#include +#include +using namespace std; + +int n, m, x[101], nr, T=(1<<20), S[1001][2], k; + + + +int eval(){ + + int i, v1, v2; + + for(i=1; i<=m; i++){ + + if(S[i][0]>0) v1 = x[S[i][0]]; + + else v1 = 1-x[-S[i][0]]; + + + + if(S[i][1]>0) v2 = x[S[i][1]]; + + else v2 = 1-x[-S[i][1]]; + + + + if(v1==0 && v2==0){ + + k=i; + + return 0; + + } + + } + + return 1; + +} + + + +void schimba(){ + + int pos=rand()%2, + + op=S[k][pos]; + + if(op<0) x[-op]=1-x[-op]; + + else x[op]=1-x[op]; + +} + + + +int ok(){ + + int i; + + for(i=1; i<=n; i++) if(x[i]) return 1; + + return 0; + +} + + + + + + + +int main(){ + + int i, c; + + srand(time(0)); + + ifstream f("party.in"); + + ofstream g("party.out"); + + f>>n; + + f>>m; + + for(i=1; i<=m; i++) { + + f>>S[i][0]>>S[i][1]>>c; + + if(c==1) S[i][1]*=-1; + + if(c==2) S[i][0]*=-1; + + if(c==3) S[i][0]*=-1, S[i][1]*=-1; + + } + + + + for(i=1; i<=n; i++){ + + c=rand(); + + x[i] = c%2; + + } + + + + while(T--){ + + if(eval()) + + if(ok()) break; + + else { c=rand()%n+1; + + x[c]=1-x[c]; + + } + + else schimba(); + + } + + if(T<0) while(1); + + for(i=1; i<=n; i++) if(x[i]) nr++; + + g< +#include +#include +#include +using namespace std; + + + + + +int N,M; +bool st[105]; +vector edge[105]; + +void read_data() +{ + short int u,v,type; + scanf("%d %d",&N,&M); + for(int i=0;i>2]) + new_state=1; + break; + case 1: if(st[edge[k][i]>>2]) + new_state=1; + break; + case 2: if(!st[edge[k][i]>>2]) + new_state=0; + break; + case 3: if(st[edge[k][i]>>2]) + new_state=0; + break; + } + if(state==-1) + state=new_state; + else + if(state==0) + { + if(new_state==1) + return; + } + else + { + if(new_state==0) + return; + } + } + + if(state==0) + { + st[k]=false; + back(k+1); + return; + } + else + if(state==1) + { + st[k]=true; + back(k+1); + return; + } + else + { + st[k]=(bool)(rand()%2); + back(k+1); + st[k]=!st[k]; + back(k+1); + } +} + + +int main() +{ + srand(N*10000+M); + freopen("party.in","r",stdin); + freopen("party.out","w",stdout); + read_data(); + back(1); + return 0; +} diff --git a/party/2sat_random/826a5040-02b2-11ec-88be-2cf05d84bfc2.cpp b/party/2sat_random/826a5040-02b2-11ec-88be-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29dc57e24f455e9058106ec98c793255505a2d16 --- /dev/null +++ b/party/2sat_random/826a5040-02b2-11ec-88be-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + +const int Nmax = 101; +const int Mmax = 1001; + +int N,M,stare[Nmax],reqx[Mmax],reqy[Mmax],reqt[Mmax]; +int lst[Mmax]; + +void readdata() +{ + int i; + + freopen("party.in","r",stdin); + + scanf("%d%d",&N,&M); + + for (i=1;i<=M;++i) + scanf("%d%d%d",&reqx[i],&reqy[i],&reqt[i]); +} + +void solve() +{ + int i,j,x,y,val=0,cnt=0; + + srand(time(0)); + + for (i=1;i<=N;++i) + if (rand()&3) + stare[i]=1; + else + stare[i]=0; + + for (;;) + { + lst[0]=0; + for (i=1;i<=M;++i) + { + x=stare[reqx[i]]; + y=stare[reqy[i]]; + if ( reqt[i] == 0 ) + val=( x | y ); + if ( reqt[i] == 1 ) + val=( x | (y^1) ); + if ( reqt[i] == 2 ) + val=( (x^1) | y ); + if ( reqt[i] == 3 ) + val=( (x^1) | (y^1) ); + if ( val == 0 ) + { + ++lst[0]; + lst[lst[0]]=i; + } + } + + cnt=0; + + for (j=1;j<=N;++j) + cnt+=stare[j]; + + if ( lst[0]==0 && cnt ) break; + + if ( lst[0] ) + { + i = lst[ rand()%lst[0] + 1 ]; + + if ( rand()&3 ) + stare[ reqx[i] ] ^=1; + else + stare[ reqy[i] ] ^=1; + } + } + + cnt=0; + + for ( i = 1; i <= N ; ++i ) + cnt+=stare[i]; + + freopen("party.out","w",stdout); + + printf("%d\n",cnt); + + for ( i = 1; i <= N ; ++i ) + if (stare[i]) printf("%d\n",i); +} + +int main() +{ + readdata(); + solve(); + return 0; +} diff --git a/party/2sat_random/828a3458-02b2-11ec-9138-2cf05d84bfc2.cpp b/party/2sat_random/828a3458-02b2-11ec-9138-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..843cf02f6b03ef5727c6f8575781be39b124fc7d --- /dev/null +++ b/party/2sat_random/828a3458-02b2-11ec-9138-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include +#include +#include + + + + +const int Nmax = 101; +const int Mmax = 1001; + +int N,M,stare[Nmax],reqx[Mmax],reqy[Mmax],reqt[Mmax]; + +void readdata() +{ + int i; + + freopen("party.in","r",stdin); + + scanf("%d%d",&N,&M); + + for (i=1;i<=M;++i) + scanf("%d%d%d",&reqx[i],&reqy[i],&reqt[i]); +} + +void solve() +{ + int i,j,x,y,val=0,cnt=0; + int stop; + + srand(time(0)); + + for (i=1;i<=N;++i) + if (rand()&3) + stare[i]=1; + else + stare[i]=0; + + for (;;) + { + stop=1; + + for (i=1;i<=M;++i) + { + x=stare[reqx[i]]; + y=stare[reqy[i]]; + if ( reqt[i] == 0 ) + val=( x | y ); + if ( reqt[i] == 1 ) + val=( x | (y^1) ); + if ( reqt[i] == 2 ) + val=( (x^1) | y ); + if ( reqt[i] == 3 ) + val=( (x^1) | (y^1) ); + if ( val == 0 ) + { + if (rand()&3) + stare[reqx[i]]^=1; + else + stare[reqy[i]]^=1; + stop=0; + } + } + + cnt=0; + + for (j=1;j<=N;++j) + cnt+=stare[j]; + + if ( stop && cnt ) break; + } + + cnt=0; + + for ( i = 1; i <= N ; ++i ) + cnt+=stare[i]; + + freopen("party.out","w",stdout); + + printf("%d\n",cnt); + + for ( i = 1; i <= N ; ++i ) + if (stare[i]) printf("%d\n",i); +} + +int main() +{ + readdata(); + solve(); + return 0; +} diff --git a/party/2sat_random/8352665b-02b2-11ec-8b35-2cf05d84bfc2.cpp b/party/2sat_random/8352665b-02b2-11ec-8b35-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5253660c49686413b9e557ae4f3d1e4a65b4fb1b --- /dev/null +++ b/party/2sat_random/8352665b-02b2-11ec-8b35-2cf05d84bfc2.cpp @@ -0,0 +1,105 @@ +#include +#include +struct rel{int a,b,z;}; + +int n,m; + +rel x[1000]; + +char v[101]; + + + +void citire() + +{FILE *fin=fopen("party.in","rt"); + +fscanf(fin,"%d%d",&n,&m); + +int i; + +for (i=0;i +#include +struct rel{int a,b,z;}; + +int n,m; + +rel x[1000]; + +char v[101]; + + + +void citire() + +{FILE *fin=fopen("party.in","rt"); + +fscanf(fin,"%d%d",&n,&m); + +int i; + +for (i=0;i +#include + + + + + + +int long e[1004][4],val[104],n,m; + + + +int long gaseste(int long x) + +{int long i; + + + + + + for(i=x;i<=m;i++) + + {if(e[i][0]==0) + + {if(!(val[e[i][1]]||val[e[i][2]])) + + return i; + + } + + if(e[i][0]==3) + + {if(val[e[i][1]]&&val[e[i][2]]) + + return i; + + } + + if(e[i][0]==1) + + {if(!val[e[i][1]]&&val[e[i][2]]) + + return i; + + } + + } + + for(i=1;i +#include + + + + + + +int e[1004][4],val[104],n,m; + + + +int gaseste(int x) + +{int i; + + + + + + for(i=x;i<=m;i++) + + {if(e[i][0]==0) + + {if(!(val[e[i][1]]||val[e[i][2]])) + + return i; + + } + + if(e[i][0]==3) + + {if(val[e[i][1]]&&val[e[i][2]]) + + return i; + + } + + if(e[i][0]==1) + + {if(!val[e[i][1]]&&val[e[i][2]]) + + return i; + + } + + } + + for(i=1;i +#include +#include + + + + + + +struct vector + +{ + + int x,y,z; + +}v[101*10]; + + + +char fol[101]; + + + +int main() + +{ + + int i,pos,N,M,end; + + + + freopen("party.in","r",stdin); + + + + scanf("%d%d",&N,&M); + + for(i=1;i<=M;i++) + + { + + scanf("%d%d%d",&v[i].x,&v[i].y,&v[i].z); + + if(v[i].z==2) + + { + + v[i].z=v[i].x; + + v[i].x=v[i].y; + + v[i].y=v[i].z; + + v[i].z=1; + + } + + } + + + + srand(time(NULL)); + + for(i=1;i<=N;i++) fol[i]=rand()%2; + + + + end=0; + + while(!end) + + { + + end=1; + + pos=rand()%M+1; + + + + for(i=pos;i<=M;i++) + + if(v[i].z==0) + + { + + if(fol[v[i].x]==0&&fol[v[i].y]==0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(fol[v[i].x]==0&&fol[v[i].y]!=0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==3) + + { + + if(fol[v[i].x]==1&&fol[v[i].y]==1) + + { + + end=0; + + break; + + } + + } + + + + if(end) + + for(i=1;i<=pos;i++) + + if(v[i].z==3) + + { + + if(fol[v[i].x]==1&&fol[v[i].y]==1) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==0) + + { + + if(fol[v[i].x]==0&&fol[v[i].y]==0) + + { + + end=0; + + break; + + } + + } + + else + + if(v[i].z==1) + + { + + if(fol[v[i].x]==0&&fol[v[i].y]!=0) + + { + + end=0; + + break; + + } + + } + + + + if(!end) + + { + + pos=rand()%2; + + if(pos) fol[v[i].x]=!fol[v[i].x]; + + else fol[v[i].y]=!fol[v[i].y]; + + } + + } + + + + freopen("party.out","w",stdout); + + + + fol[0]=0; + + for(i=1;i<=N;i++) fol[0]+=fol[i]; + + printf("%d\n",fol[0]); + + for(i=1;i<=N;i++) + + if(fol[i]) printf("%d\n",i); + + + + return 0; + +} diff --git a/scc/kosaraju/0223cded-3ac8-41c9-9d90-ce1797e31702.cpp b/scc/kosaraju/0223cded-3ac8-41c9-9d90-ce1797e31702.cpp new file mode 100644 index 0000000000000000000000000000000000000000..324466ac1e79fa9f302c0e11dee53bfcce30d172 --- /dev/null +++ b/scc/kosaraju/0223cded-3ac8-41c9-9d90-ce1797e31702.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vectorG[100005]; + +vectorGT[100005]; + +vectorCTC[100005]; + +int sortt[100005]; + +int N, M; + +int cicluri; + +short int frec[100005]; + +int dfs(int nod) + +{ + + frec[nod] = 1; + + for(int i=0; i=1; i--) + + { + + int nod = sortt[i]; + + if(frec[ nod ] == 1) + + { + + cicluri++; + + dfs_t(nod); + + } + + } + + afisare(); + +} + +void citire() + +{ + + fin>>N>>M; + + int x, y; + + for(int i=1; i<=M; i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + rezolvare(); + +} + +int main() + +{ + + citire(); + + return 0; + +} diff --git a/scc/kosaraju/02608aff-cd14-40cf-8cb8-8b83cd98249f.cpp b/scc/kosaraju/02608aff-cd14-40cf-8cb8-8b83cd98249f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d78c1d5648c593c094bc733fdefa36dc1bd74f6 --- /dev/null +++ b/scc/kosaraju/02608aff-cd14-40cf-8cb8-8b83cd98249f.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + + + +vector > G, GT,sol; + + + +int n, m; + + + +vector V; + +vector S,ans; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void read() + +{ + + fin >> n >> m; + + G = GT = vector>(n + 1); + + for (int i = 1; i <= m; i++) + + { + + int a, b; + + fin >> a >> b; + + G[a].push_back(b); + + GT[b].push_back(a); + + } + + + +} + + + +void dfs(int k) + +{ + + V[k] = true; + + for (auto x : G[k]) + + if (!V[x]) + + dfs(x); + + S.push_back(k); + +} + + + +void dfsGT(int k) + +{ + + V[k] = 1; + + for (auto x : GT[k]) + + if (!V[x]) + + dfsGT(x); + + ans.push_back(k); + + + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin.tie(0); + + read(); + + + + V = vector(n + 1, false); + + for (int i = 1; i <= n; i++) + + if (!V[i]) + + { + + dfs(i); + + + + } + + + + V = vector(n + 1, false); + + for (vector::reverse_iterator it = S.rbegin(); it != S.rend(); it++) + + if (!V[*it]) { + + ans.clear(); + + dfsGT(*it); + + sol.push_back(ans); + + + + } + + + + fout << sol.size()<<'\n'; + + for (auto it : sol) + + { + + for (auto ij : it) + + fout << ij << " "; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/02cf982f-2e1a-4ec6-a4a1-866f0fcd8a74.cpp b/scc/kosaraju/02cf982f-2e1a-4ec6-a4a1-866f0fcd8a74.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48ba5e2e230443a859450772dc5b176b2c1dd93a --- /dev/null +++ b/scc/kosaraju/02cf982f-2e1a-4ec6-a4a1-866f0fcd8a74.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vector adj[100005]; + +int n,m,low_link[100005],idx[100005],cnt=0,ctc=0; + +bool viz[100005],instack[100005]; + +vector sol[100005]; + +stack s; + +void dfs(int node) + +{ + + s.push(node); + + instack[node]=true; + + idx[node]=low_link[node]=++cnt; + + for(auto x:adj[node]) + + { + + if(idx[x]==0) {dfs(x);low_link[node]=min(low_link[node],low_link[x]);} + + if(instack[x]==1) + + { + + low_link[node]=min(low_link[node],low_link[x]); + + } + + } + + if(idx[node]==low_link[node]) + + { + + ctc++; + + int x=node; + + do + + { + + x=s.top(); + + sol[ctc].push_back(x); + + instack[x]=0; + + s.pop(); + + } + + while(x!=node); + + } + + + + + + + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y; + + f>>x>>y; + + adj[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + { + + if(idx[i]==0) + + { + + dfs(i); + + } + + } + + g< +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +stack S; + + + +vector G[100005], GT[100005], CTC[100005]; + + + +int N, M, nrCTC; + +int checked[100005]; + + + +void Read() { + + fin >> N >> M; + + + + for (int i = 1; i <= M; ++i) { + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFSP(int nod) { + + checked[nod] = 1; + + + + for (int i = 0; i < G[nod].size(); ++i) { + + int vecin = G[nod][i]; + + + + if (!checked[vecin]) + + DFSP(vecin); + + } + + S.push(nod); + +} + + + +void DFSM(int nod) { + + checked[nod] = 2; + + CTC[nrCTC].push_back(nod); + + + + for (int i = 0; i < GT[nod].size(); ++i) { + + int vecin = GT[nod][i]; + + + + if (checked[vecin] == 1) + + DFSM(vecin); + + } + +} + + + +void Solve() { + + for (int i = 1; i <= N; ++i) + + if (!checked[i]) + + DFSP(i); + + + + while (!S.empty()) { + + int nod = S.top(); + + if (checked[nod] == 1) { + + nrCTC++; + + DFSM(nod); + + } + + S.pop(); + + } + +} + + + +void Print() { + + fout << nrCTC << " "; + + + + for (int i = 1; i <= nrCTC; ++i) { + + for (unsigned int j = 0; j < CTC[i].size(); ++j) + + fout << CTC[i][j] << " "; + + fout << "\n"; + + } + +} + + + +int main() { + + + + Read(); + + Solve(); + + Print(); + + + + return 0; + +} diff --git a/scc/kosaraju/04ac9965-361e-47cc-b425-b6fcc0e43334.cpp b/scc/kosaraju/04ac9965-361e-47cc-b425-b6fcc0e43334.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3c9c0df8accbf0a010fd8594f8d47bc96464375 --- /dev/null +++ b/scc/kosaraju/04ac9965-361e-47cc-b425-b6fcc0e43334.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +using namespace std; + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +int n, m; + +int use[100001]; + +vector g[100001]; + +vector gt[100001]; + +vector s; + +vector>ans; + +vector v; + +void dfs1(int nod) + +{ + + use[nod] = 1; + + + + for(int i=0;i> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int x, y; + + cin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + for (int i = 1; i <= n; i++) + + { + + if (use[i] == 0) + + dfs1(i); + + } + + for(int i=s.size()-1;i>=0;i--) + + { + + if (use[s[i]] == 1) + + { + + v.clear(); + + dfs2(s[i]); + + ans.push_back(v); + + } + + } + + cout << ans.size() << "\n"; + + for (auto i : ans) + + { + + for (auto j : i) + + cout << j << " "; + + cout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/04c96b2f-b705-42b7-ba2f-2ed3dbb68a14.cpp b/scc/kosaraju/04c96b2f-b705-42b7-ba2f-2ed3dbb68a14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bb8ccb0330b1e30357bf91e5062fd213c3cd93d --- /dev/null +++ b/scc/kosaraju/04c96b2f-b705-42b7-ba2f-2ed3dbb68a14.cpp @@ -0,0 +1,140 @@ +#include +using namespace std; + +int viz[100009],n,m,k,nctc,SDV[100009]; + + + +vector G[100009],GT[100009],CTC[100009]; + + + + + +void DFS(int x) + +{ + + int i,j; + + viz[x]=1; + + for(i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for(i=1;i<=n;++i) + + if(!viz[i]) + + DFS(i); + + + + for(i=1;i<=n;i++) + + viz[i]=0; + + + + for(i=n;i>=1;i--) + + if(!viz[SDV[i]]) + + { + + nctc++; + + DFS2(SDV[i]); + + } + + + + o< +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int N=100010; + +int n,m,x,y,viz[N],ctc; + +vector G[N],GT[N],C[N]; + +stack st; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto it:G[nod]) + + if(!viz[it]) + + dfs(it); + + st.push(nod); + +} + +void dfst(int nod) + +{ + + viz[nod]=2; + + C[ctc].push_back(nod); + + for(auto it:GT[nod]) + + if(viz[it]==1) + + dfst(it); + +} + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(!viz[i]) + + dfs(i); + + while(st.size()) + + { + + int nodc=st.top(); + + if(viz[nodc]==1) + + { + + ctc++; + + dfst(nodc); + + } + + st.pop(); + + } + + g< + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + list ℴ + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void visit_post_order(int current_node, traversal_data &td); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + list order; + vector visited(n, false); + traversal_data td = {G, visited, order, 0, SCCs}; + + for (int i = 0; i < n; ++i) { + if (not visited[i]) visit_post_order(i, td); + } + + SCCs.resize(n); + traversal_data scc_td = {TG, visited, order, 0, SCCs}; + + while (not order.empty()) { + int node = order.front(); + order.pop_front(); + + if (visited[node]) { + get_scc(node, scc_td); + scc_td.SCC_index++; + } + } + + write_solution(scc_td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void visit_post_order(int current_node, traversal_data &td) { + td.visited[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) visit_post_order(neighbour, td); + } + td.order.push_front(current_node); +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = false; + td.SCCs[td.SCC_index].push_back(current_node); + + for (auto neighbour: td.G[current_node]) { + if (td.visited[neighbour]) get_scc(neighbour, td); + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/kosaraju/05d12abc-ae28-454e-a54e-74f79c762e51.cpp b/scc/kosaraju/05d12abc-ae28-454e-a54e-74f79c762e51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08bfedf1a71bf01a0bfefe1d1bef985c5cb2aa1f --- /dev/null +++ b/scc/kosaraju/05d12abc-ae28-454e-a54e-74f79c762e51.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + int n, m; + + vector adj[100010]; + vector adj_trans[100010]; + + vector> ctc; + vector visited; + vector topsort; + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + visited = vector(n + 1, 0); + + for (int i = 1; i <= m; ++i) { + int x, y; + fin >> x >> y; + + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + fin.close(); + } + + void get_result() { + kosaraju(); + } + + void kosaraju() { + topsort.push_back(-1); + + for (int i = 1; i <= n; ++i) { + if (visited[i] == 0) { + dfs(i); + } + } + + for (int i = n; i >= 1; --i) { + if (visited[topsort[i]] == 1) { + vector current_ctc; + + dfs_t(topsort[i], current_ctc); + + ctc.push_back(current_ctc); + } + } + } + + void dfs(int node) { + visited[node] = 1; + + for (auto &vecin : adj[node]) { + if (!visited[vecin]) { + dfs(vecin); + } + } + topsort.push_back(node); + } + + void dfs_t(int node, vector ¤t_ctc) { + visited[node] = 0; + current_ctc.push_back(node); + + for (auto &v : adj_trans[node]) { + if (visited[v]) { + dfs_t(v, current_ctc); + } + } + } + + void print_output() { + ofstream fout("ctc.out"); + int sol = ctc.size(); + fout << sol << '\n'; + + for (int i = 0; i < sol; ++i) { + for (auto &node : ctc[i]) { + fout << node << " "; + } + fout << '\n'; + } + fout.close(); + } +}; + +int main() { + Task *task = new Task(); + task->solve(); + delete task; + + return 0; +} diff --git a/scc/kosaraju/064d0ed5-a803-40e4-b6dc-1d0d1a9079ea.cpp b/scc/kosaraju/064d0ed5-a803-40e4-b6dc-1d0d1a9079ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28a936c62d0327c739a40acdabc8281a01405303 --- /dev/null +++ b/scc/kosaraju/064d0ed5-a803-40e4-b6dc-1d0d1a9079ea.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +const int NMAX=100005; + +bool viz[NMAX]; + +vectorv[NMAX],g[NMAX],sol; + +vector>grf; + +int n,m,a,b,i; + +void dfs(int nod,const auto &ad,int ok){ + + viz[nod]=1; + + if(!ok)grf.back().push_back(nod); + + for(int x:ad[nod]) + + if(!viz[x])dfs(x,ad,ok); + + if(ok)sol.push_back(nod); + +} + +int main(){ + + in>>n>>m; + + while(m--) + + in>>a>>b,v[a].push_back(b),g[b].push_back(a); + + for(i=1;i<=n;i++) + + if(!viz[i])dfs(i,v,1); + + memset(viz,0,sizeof(viz)); + + for(i=sol.size()-1;i>=0;i--) + + if(!viz[sol[i]]) + + grf.push_back(vector()),dfs(sol[i],g,0); + + out< +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +typedef long long ll; + +int node,ctc,n,m,x,y,use[2*50005]; + +vector g[2*50005],gt[2*50005],ans[2*50005]; + +stack s; + +void dfs(int nod) + +{ + + use[nod]=1; + + for(auto it : g[nod]) + + if(!use[it]) + + dfs(it); + + s.push(nod); + +} + +void dfs1(int nod) + +{ + + use[nod]=2; + + ans[ctc].push_back(nod); + + for(auto it : gt[nod]) + + if(use[it]==1) + + dfs1(it); + +} + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(!use[i]) + + dfs(i); + + while(!s.empty()) + + { + + node=s.top(); + + if(use[node]==1) + + { + + ctc++; + + dfs1(node); + + } + + s.pop(); + + } + + fout< +#include +#include + + +using namespace std; + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +stack < int > S; + +vector G[100005], GT[100005], CTC[100005]; + +int N, M, NrCTC; + +int beenThere[100005]; + +void Read() + +{ + + cin >> N >> M; + + for (int i = 1; i <= M; i++) + + { + + int x, y; + + cin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for (unsigned int i = 0; i < G[Nod].size(); i++) + + { + + int Vecin = G[Nod][i]; + + if (!beenThere[Vecin]) + + DFSP(Vecin); + + } + + S.push(Nod); + +} + +void DFSM(int Nod) + +{ + + beenThere[Nod] = 2; + + CTC[NrCTC].push_back(Nod); + + for (unsigned int i = 0; i < GT[Nod].size(); i++) + + { + + int Vecin = GT[Nod][i]; + + if (beenThere[Vecin] == 1) + + DFSM(Vecin); + + } + +} + +void Solve() + +{ + + for (int i = 1; i <= N; i++) + + if (!beenThere[i]) + + DFSP(i); + + while (!S.empty()) + + { + + int Nod = S.top(); + + if (beenThere[Nod] == 1) + + { + + NrCTC++; + + DFSM(Nod); + + } + + S.pop(); + + } + +} + +void Print() + +{ + + cout << NrCTC << "\n"; + + for (int i = 1; i <= NrCTC; i++) + + { + + for (unsigned int j = 0; j < CTC[i].size(); j++) + + cout << CTC[i][j] << " "; + + cout << "\n"; + + } + +} + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/scc/kosaraju/06a42b22-3838-45ba-8d15-065c19d0c966.cpp b/scc/kosaraju/06a42b22-3838-45ba-8d15-065c19d0c966.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a2f5634df301e2d7b40d326fe882c93c3916b3f --- /dev/null +++ b/scc/kosaraju/06a42b22-3838-45ba-8d15-065c19d0c966.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +int viz[100100],viz2[100100],n; + +vector sol, G[100100], G_inv[100100], List; + +vector < vector > ctc; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto nn:G[nod]) + + if(!viz[nn]) + + dfs(nn); + + List.push_back(nod); + +} + + + +void mark(int nod) + +{ + + viz2[nod]=ctc.size()+1; + + sol.push_back(nod); + + for(auto nn:G_inv[nod]) + + if(!viz2[nn]) + + mark(nn); + +} + + + +int main() + +{ + + int m,x,y,el,k=0; + + cin>>n>>m; + + for(int i=1; i<=m; ++i) + + { + + cin>>x>>y; + + G[x].push_back(y); + + G_inv[y].push_back(x); + + } + + + + for(int i=1; i<=n; ++i) + + if(!viz[i]) + + { + + ++k; + + dfs(i); + + } + + while(!List.empty()) + + { + + el=List.back(); + + if(!viz2[el]) + + { + + mark(el); + + ctc.push_back(sol); + + sol.clear(); + + } + + List.pop_back(); + + } + + cout< +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream o("ctc.out"); + +const int NMAX=100005; + +int vi[NMAX]; + +vectorv[NMAX],g[NMAX],sol; + +vector>gg; + +int n,m,a,b,i; + +void d(int nod,auto &ad,int ok){ + + vi[nod]=1; + + if(!ok)gg.back().push_back(nod); + + for(int x:ad[nod]) + + if(!vi[x])d(x,ad,ok); + + if(ok)sol.push_back(nod); + +} + +int main(){ + + in>>n>>m; + + while(m--) + + in>>a>>b,v[a].push_back(b),g[b].push_back(a); + + for(i=1;i<=n;i++) + + if(!vi[i])d(i,v,1); + + memset(vi,0,sizeof(vi)); + + for(i=sol.size()-1;i>=0;i--) + + if(!vi[sol[i]]) + + gg.push_back(vector()),d(sol[i],g,0); + + o< +#include // Common file +#include // Including tree_order_statistics_node_update + + + + + +using namespace std; + +using namespace __gnu_pbds; + +auto random_address = [] { char *p = new char; delete p; return uint64_t(p); }; + +const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); + +mt19937_64 rng(SEED); + +typedef tree< + +int, + +null_type, + +less, + +rb_tree_tag, + +tree_order_statistics_node_update> + +ordered_set; + +typedef long long ll; + +typedef unsigned long long ull; + +typedef long double ld; + +typedef pair pii; + +typedef pair pll; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m; + +vector muchii[100001],st,v; + +vector> sol; + +pii G[200001]; + +bool use[200001]; + +void dfs1(int nod) + +{ + + use[nod]=1; + + for(auto i:muchii[nod]) + + if(!use[i]) + + { + + use[i]=1; + + dfs1(i); + + } + + st.push_back(nod); + +} + +void dfs(int nod) + +{ + + use[nod]=1; + + v.push_back(nod); + + for(auto i:muchii[nod]) + + if(!use[i]) + + dfs(i); + +} + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b; + + fin>>a>>b; + + G[i]={a,b}; + + muchii[a].push_back(b); + + } + + for(int i=1;i<=n;i++) + + if(!use[i]) + + dfs1(i); + + + + + + + + reverse(st.begin(),st.end()); + + for(int i=1;i<=n;i++) + + { + + muchii[i].clear(); + + use[i]=0; + + } + + for(int i=1;i<=m;i++) + + muchii[G[i].second].push_back(G[i].first); + + for(auto i:st) + + if(!use[i]) + + { + + v.clear(); + + dfs(i); + + sol.push_back(v); + + } + + fout< +#include + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graph[100002], grapht[100002],sol[100002]; + +int st[100002]; + +bool viz[100002]; + + + +void so(int nod, int nr) + +{ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + grapht[y].push_back(x); + + } + + for(int i=1;i<=n;++i) + + if(!viz[i]) + + sortt(i); + + for(int i=1;i<=n;++i) viz[i]=0; + + for(int i=n;i>=1;--i) + + if(!viz[st[i]]) + + so(st[i],++nr); + + g< +#include +#include +#include +using namespace std; + + + +ofstream g("ctc.out"); + +const int NMAX = 100005; + +vector < int > G[NMAX], GT[NMAX], result[NMAX]; + +stack S; + +int NrCTC; + +int N,M; + +int beenThere[NMAX]; + +int rez[NMAX]; + + + +void read() + +{ + + ifstream f("ctc.in"); + + f >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS(int Nod){ + + beenThere[Nod] = 1; + + for(unsigned int i = 0; i < G[Nod].size();i++){ + + int Vecin = G[Nod][i]; + + if(!beenThere[Vecin]) + + DFS(Vecin); + + } + + S.push(Nod); + +} + + + +void DFS_T(int Nod){ + + beenThere[Nod] = 2; + + result[NrCTC].push_back(Nod); + + + + for(unsigned int i = 0; i < GT[Nod].size();i++){ + + int Vecin = GT[Nod][i]; + + if(beenThere[Vecin] == 1) + + DFS_T(Vecin); + + } + +} + +void Solve() + +{ + + for(int i = 1; i <= N;i++){ + + if(!beenThere[i]) + + DFS(i); + + } + + while(!S.empty()){ + + int Nod = S.top(); + + cout << Nod << " "; + + if(beenThere[Nod] == 1){ + + NrCTC++; + + DFS_T(Nod); + + } + + S.pop(); + + } + +} + + + +void Print() + +{ + + g << NrCTC << '\n'; + + + + for(int i = 1; i <= NrCTC;i++){ + + for(unsigned int j = 0; j < result[i].size();j++) + + g << result[i][j] << " "; + + g << '\n'; + +} + +} + +int main() + +{ + + read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/scc/kosaraju/0b0a7d0e-6905-4800-a681-34a621056ea8.cpp b/scc/kosaraju/0b0a7d0e-6905-4800-a681-34a621056ea8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0302d91c2aa18b533c0602a1021242b02ca06512 --- /dev/null +++ b/scc/kosaraju/0b0a7d0e-6905-4800-a681-34a621056ea8.cpp @@ -0,0 +1,85 @@ +#include +#include + + +const int nmax = 1e5 + 5; + + + +std::vectorin[nmax], out[nmax], ans[nmax], st; + +int viz[nmax], k; + + + +void dfs(int node, int type) { + + viz[node] = type; + + if (type == 1) { + + for (auto x : in[node]) if (viz[x] == 0) dfs(x, type); + + st.push_back(node); + + } + + else { + + ans[k].push_back(node); + + for (auto x : out[node]) if (viz[x] == 1) dfs(x, type); + + } + +} + + + +int main() { + + std::ifstream fin("ctc.in"); + + std::ofstream fout("ctc.out"); + + int n, m, u, v; + + fin >> n >> m; + + for (int i = 0; i < m; i++) { + + fin >> u >> v; + + in[u].push_back(v); + + out[v].push_back(u); + + } + + for (int i = 1; i <= n; i++) + + if (!viz[i]) dfs(i, 1); + + while (st.size()) { + + int node = st.back(); + + st.pop_back(); + + if (viz[node] == 1) k++, dfs(node, 2); + + } + + fout << k; + + for (int i = 1; i <= k; i++) { + + fout << "\n"; + + for (auto x : ans[i]) + + fout << x << " "; + + } + +} diff --git a/scc/kosaraju/0b1ddf11-89cf-40b6-9384-6d111ddc969d.cpp b/scc/kosaraju/0b1ddf11-89cf-40b6-9384-6d111ddc969d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f65af9c13174623a54758082e72473f68419cd0 --- /dev/null +++ b/scc/kosaraju/0b1ddf11-89cf-40b6-9384-6d111ddc969d.cpp @@ -0,0 +1,188 @@ +#include + + + + +using namespace std; + + + +class Task { + +public: + + void solve() { + + read_input(); + + + + print_output(get_result()); + + } + + + +private: + + + + int n, m; + + + + + + vector adj[100010]; + + vector adjt[100010]; + + + + void read_input() { + + ifstream in("ctc.in"); + + in >> n >> m; + + + + for (int i = 1; i <= m; ++i) { + + int x, y; + + in >> x >> y; + + adj[x].push_back(y); + + adjt[y].push_back(x); + + } + + + + in.close(); + + } + + + + void dfs(int node, vector &sort, vector &visited){ + + visited[node] = 1; + + for (auto &neighbor : adj[node]) + + if (!visited[neighbor]) + + dfs(neighbor,sort,visited); + + + + sort.push_back(node); + + } + + + + + + void dfs_transpus(int node, vector ¤t, vector &visited){ + + visited[node] = 0; + + current.push_back(node); + + + + for (auto &neighbor : adjt[node]) + + if (visited[neighbor]) + + dfs_transpus(neighbor,current,visited); + + } + + + + vector> get_result() { + + vector> solutie; + + vector visited(n + 1); + + vector sort; + + sort.push_back(-1); + + + + for (int i = 1; i <= n; ++i) { + + if (!visited[i]) { + + dfs(i, sort, visited); + + } + + } + + + + for (int i = n; i >= 1; --i) + + if (visited[sort[i]]) { + + vector current; + + dfs_transpus(sort[i], current, visited); + + solutie.push_back(current); + + } + + + + return solutie; + + } + + + + void print_output(vector> result) { + + ofstream out("ctc.out"); + + out << result.size() << '\n'; + + for (const auto& ctc : result) { + + for (int nod : ctc) { + + out << nod << ' '; + + } + + out << '\n'; + + } + + + + out.close(); + + } + +}; + + + +int main() { + Task *task = new Task(); + + task->solve(); + + delete task; + return 0; + +} diff --git a/scc/kosaraju/0b60f3b9-a761-4300-bb22-8c84ddf08cea.cpp b/scc/kosaraju/0b60f3b9-a761-4300-bb22-8c84ddf08cea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8bc37534d1bdcfe8a8263c9013e46c8dca850a59 --- /dev/null +++ b/scc/kosaraju/0b60f3b9-a761-4300-bb22-8c84ddf08cea.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + +using namespace std; + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +int N,M, vis1[100100], vis2[100100], mark; + +vector Ginit[100100], G_inv[100100]; + +inline void scan() { + + int x,y; + + cin>>N>>M; + + for(int i=1; i<=M; ++i) { + + cin>>x>>y; + + Ginit[x].push_back(y); + + G_inv[y].push_back(x); + + } + +} + +list List; + +inline void dfsmth(int x) { + + + + vis1[x]=1; + + for(auto it:Ginit[x]) + + if(!vis1[it]) + + dfsmth(it); + + List.push_back(x); + +} + + + +void dfmark(int x, int smth) { + + + + vis2[x]=smth; + + for(auto it: G_inv[x]) + + if(!vis2[it]) + + dfmark(it, smth); + +} + + + +vector compcon[100100]; + +inline void print() { + + + + cout< +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector graph[100000]; +vector transposed_graph[100000]; +bool visited[100000]; +vector order; +int node_count; + + +void read(){ + int edge_count; + int a,b; + in>>node_count>>edge_count; + for(int i=0;i>a>>b; + graph[a-1].push_back(b-1); + transposed_graph[b-1].push_back(a-1); + } +} + + +int dfs1(int node){ + visited[node]=true; + for(int i=0;i&to_fill){ + visited[node]=true; + to_fill.push_back(node); + for(int i=0;i> connected; + + for(int k=node_count-1;k>=0;k--){ + if(!visited[order[k]]){ + vector result; + dfs2(order[k],result); + connected.push_back(result); + } + } + + out< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector cc[100009]; + +vector v[100009]; + +vector tr[100009]; + +int viz[100009],nr; + +stacks; + + + +void dfs(int n) + +{ + + viz[n] = 1; + + for(int i=0; i>n>>m; + + for(int i =1; i <=m ; i++) + + { + + f>>x>>y; + + tr[y].push_back(x); + + v[x].push_back(y); + + + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i]==0) + + dfs(i); + + } + + + + + + + + while(s.empty()==0) + + { + + int n=s.top(); + + if(viz[n]==1) + + { + + nr++; + + dfst(n); + + } + + s.pop(); + + } + + g< + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + + + + + +int n, m, q; + +int k, DUD[100001 + 5]; + +bool viz[100001]; + +vector V1[100001]; + +vector V2[100001]; + +vector ctc[100001]; + + + +void DFS1(int nod) + +{ + + viz[nod] = 1; + + + + for(auto it : V1[nod]) + + if(!viz[it]) + + DFS1(it); + + + + DUD[++k] = nod; + +} + + + +void DFS2(int nod) + +{ + + viz[nod] = 0; + + ctc[q].push_back(nod); + + + + for(auto it : V2[nod]) + + if(viz[it] == 1) + + DFS2(it); + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; i++) { + + + + int x, y; + + fin >> x >> y; + + V1[x].push_back(y); + + V2[y].push_back(x); + + + + } + + + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS1(i); + + + + for(int i = n; i >= 1; i--) + + if(viz[DUD[i]] == 1) + + { + + q++; + + DFS2(DUD[i]); + + } + + + + fout << q << '\n'; + + for(int i = 1; i <= q; i++) + + { + + for(auto it : ctc[i]) + + fout << it << ' '; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/0ce91a21-9e24-4f58-a586-34e24cd8a432.cpp b/scc/kosaraju/0ce91a21-9e24-4f58-a586-34e24cd8a432.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30096e92c78d608ad2adcc1988e53687d1ec40cb --- /dev/null +++ b/scc/kosaraju/0ce91a21-9e24-4f58-a586-34e24cd8a432.cpp @@ -0,0 +1,97 @@ +#include +#include +std::ifstream fin("ctc.in"); + +std::ofstream fout("ctc.out"); + +int n,m,k,cmp,ts[100002],use[100002]; + +std::vector> V,Vt,cnxt; + +void DFS(int N) + +{ + + use[N]=1; + + for(int i=0; i<(int)V[N].size(); i++) + + if(!use[V[N][i]]) + + DFS(V[N][i]); + + ts[++k]=N; + +} + +void DFSt(int N,int cmp) + +{ + + use[N]=2; + + cnxt[cmp].push_back(N); + + for(int i=0; i<(int)Vt[N].size(); i++) + + if(use[Vt[N][i]]==1) + + DFSt(Vt[N][i],cmp); + +} + +int main() + +{ + + fin>>n>>m; + + V.resize(n+2); + + Vt.resize(n+2); + + cnxt.resize(n+2); + + for(int i=1; i<=m; i++) + + { + + int x,y; + + fin>>x>>y; + + V[x].push_back(y); + + Vt[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(!use[i]) + + DFS(i); + + for(int i=n; i>=1; i--) + + if(use[ts[i]]==1) + + DFSt(ts[i],++cmp); + + fout< + + +using namespace std; + + + +const int NMAX = 100005; + +int N, M, topo[NMAX], K; + +vector graph[NMAX]; + +vector revgraph[NMAX]; + +bitset seen; + +vector < vector > ctc; + +vector v; + + + +void dfs1(int node) + +{ + + seen[node] = 1; + + for (auto& x : graph[node]) + + if (seen[x] == 0) + + dfs1(x); + + topo[++K] = node; + +} + + + +void dfs2(int node) + +{ + + seen[node] = 1; + + for (auto& x : revgraph[node]) + + if (seen[x] == 0) + + dfs2(x); + + v.push_back(node); + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + fin >> N >> M; + + for (int i = 1;i <= M;++i) + + { + + int u, v; + + fin >> u >> v; + + graph[u].push_back(v); + + revgraph[v].push_back(u); + + } + + for (int i = 1;i <= N;++i) + + if (seen[i] == 0) + + dfs1(i); + + seen.reset(); + + for (int i = N;i >= 1;--i) + + if (seen[topo[i]] == 0) + + { + + v.clear(); + + dfs2(topo[i]); + + ctc.push_back(v); + + } + + fout << ctc.size() << "\n"; + + for (auto& i : ctc) + + { + + for (auto& j : i) + + fout << j << " "; + + fout << "\n"; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/0d3cc25f-b2f6-42cb-8c3f-7aea1f1b82b4.cpp b/scc/kosaraju/0d3cc25f-b2f6-42cb-8c3f-7aea1f1b82b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25a8ad963612502c4b3e4e90c80dcd1bf4d553d5 --- /dev/null +++ b/scc/kosaraju/0d3cc25f-b2f6-42cb-8c3f-7aea1f1b82b4.cpp @@ -0,0 +1,176 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + + + + + +int n,m,x,y,nr; + + + +vector G[100005],GT[100005],S,sol[100005]; + +bitset <100005> v; + + + +void dfs(int x) + +{ + + v[x]=1; + + for(int i=0;i>n>>m; + + + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + + + } + + + + + + for(int i=1;i<=n;i++) + + if(v[i]==0) + + dfs(i); + + + + v.reset(); + + + + for(int i=n-1;i>=0;i--) + + if(v[S[i]]==0) + + { + + nr++; + + dfsGT(S[i]); + + + + } + + + + + + fout< +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bool viz[100001]; + +vector A[100001]; + +vector T[100001]; + +vector rez[100001]; + +stack Q; + +int n,m,nm=0; + +void dfs1(int nod) + +{ + + viz[nod]=1; + + + + vector :: iterator it; + + for(it=A[nod].begin();it!=A[nod].end();it++) + + if(!viz[*it]) + + dfs1(*it); + + Q.push(nod); + +} + +void dfs2(int nod) + +{ + + viz[nod]=1; + + rez[nm].push_back(nod); + + vector :: iterator it; + + for(it=T[nod].begin();it!=T[nod].end();it++) + + if(!viz[*it]) + + dfs2(*it); + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b;; + + fin>>a>>b; + + A[a].push_back(b); + + T[b].push_back(a); + + } + + for(int i=1;i<=n;i++) + + if(viz[i]==0) + + dfs1(i); + + for(int i=1;i<=n;i++) + + viz[i]=0; + + while(!Q.empty()) + + { + + int v; + + v=Q.top(); + + Q.pop(); + + if(!viz[v]) + + { + + nm++; + + dfs2(v); + + } + + } + + fout< +#include + + +using namespace std; + + + +const int N = 200001; + +int n, m, nr, s, nc, q[N], distances[N], c[N]; + +int vf[2 * N], vf1[2 * N], lst[N], lst1[N], urm[2 * N], urm1[2 * N]; + +bool viz[N], viz1[N]; + +int st[N], st1[N]; + +int varf; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +void adauga(int x, int y) { + + vf[++nr] = y; + + urm[nr] = lst[x]; + + lst[x] = nr; + + vf1[nr] = x; + + urm1[nr] = lst1[y]; + + lst1[y] = nr; + +} + + + +void dfs(int x) { + + + + viz[x] = true; + + int y; + + for (int p = lst[x]; p != 0; p = urm[p]) { + + y = vf[p]; + + if (!viz[y]) dfs(y); + + } + + st[++varf] = x; + +} + + + +void dfs1(int x) { + + viz1[x] = true; + + c[nc]++; + + st1[++nr] = x; + + int y; + + for (int p = lst1[x]; p != 0; p = urm1[p]) { + + y = vf1[p]; + + if (!viz1[y]) dfs1(y); + + } + +} + + + +struct comp { + + int x, y; + +}v[N]; + + + + + +int main() { + + in >> n >> m; + + for (int i = 1; i <= m; i++) { + + in >> v[i].x >> v[i].y; + + adauga(v[i].x, v[i].y); + + } + + for (int i = 1; i <= n; i++) + + if (!viz[i]) dfs(i); + + nr = 0; + + for (int i = n; i >= 1; i--) { + + if (!viz1[st[i]]) { + + nc++; + + dfs1(st[i]); + + } + + } + + + + int a = 1; + + out << nc << '\n'; + + for (int i = 1; i <= nc; i++) { + + for (int j = 1; j <= c[i]; j++) + + out << st1[a++] << ' '; + + out << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/1024e30e-e253-4c97-be98-8043b099f0bb.cpp b/scc/kosaraju/1024e30e-e253-4c97-be98-8043b099f0bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..034c372bffb04538a3305b8818222ecfc5aef507 --- /dev/null +++ b/scc/kosaraju/1024e30e-e253-4c97-be98-8043b099f0bb.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +const int maxSize=100005; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m; + +vectornodes[maxSize], transpus[maxSize]; + +stacks; + +vector ctc[maxSize]; + +int visited[maxSize]; + +int k; + + + +void read(){ + + fin>>n>>m; + + int x, y; + + for(int i=0; i>x>>y; + + nodes[x].push_back(y); + + transpus[y].push_back(x); + + } + +} + + + +void dfs(int node){ + + visited[node] = 1; + + for(auto it:nodes[node]) + + if(visited[it] == 0) + + dfs(it); + + s.push(node); + +} + + + +void dfst(int node){ + + visited[node] = -1; + + for(auto it:transpus[node]) + + if(visited[it] > 0) + + dfst(it); + + ctc[k].push_back(node); + +} + + + +void da(){ + + for(int i=1; i<=n; i++) + + if(visited[i] == 0) + + dfs(i); + +} + + + +void da2(){ + + while(!s.empty()){ + + int v = s.top(); + + s.pop(); + + if(visited[v] < 0) + + continue; + + k++; + + dfst(v); + + } + +} + + + +void print(){ + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + +vectorgi[100002], gf[100002], comp[100002]; + +deque t; + +bool viz1[100002], viz2[100002]; + +int n, m, nr_componente=0; + + + +void DFS1(int x) + +{ + + viz1[x]=1; + + for(int i=0; i>n>>m; + + int i, j, x, y; + + for(i=1; i<=m; i++){ + + fin>>x>>y; + + gi[x].push_back(y); + + gf[y].push_back(x); + + } + + sortareTopologica(); + + + + for(i=t.size()-1; i>=0; i--){ + + if(!viz2[t[i]]){ + + + + nr_componente++; + + DFS2(t[i]); + + } + + } + + + + fout< +#include +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m; + +vector v[100005], in[100005]; + +vector sol[100005]; + +bool u1[100005], u2[100005]; + +int st[100005], top, ctc; + + + +void dfs1(int x) + +{ + + u1[x]=1; + + for (auto y:v[x]) + + { + + if (u1[y] == 1) continue; + + dfs1(y); + + } + + st[++top]=x; + +} + + + +void dfs2(int x) + +{ + + u2[x]=1; + + for (auto y:in[x]) + + { + + if (u2[y] == 1) continue; + + dfs2(y); + + } + + sol[ctc].push_back(x); + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i = 1, x, y; i <= m; i++) + + { + + f >> x >> y; + + v[x].push_back(y); + + in[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + if (u1[i] == 0) dfs1(i); + + + + + + while (top > 0) + + { + + if (u2[st[top]] == 1) top--; + + else + + { + + dfs2(st[top]); + + top--; + + ctc++; + + } + + } + + + + g << ctc << '\n'; + + for (int i = 0; i < ctc; i++) + + { + + for (auto y:sol[i]) + + g << y << ' '; + + g << '\n'; + + } + + + + + + return 0; + +} diff --git a/scc/kosaraju/135a783c-d82c-416e-b07b-95d32e181329.cpp b/scc/kosaraju/135a783c-d82c-416e-b07b-95d32e181329.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b49dff1bdb980afb4e681f8a0b5c723fdad9b0f --- /dev/null +++ b/scc/kosaraju/135a783c-d82c-416e-b07b-95d32e181329.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include + + + +using namespace std; + +vector G[100001], GT[100001]; +unordered_set viz; +stack st; +vector> result; + +int n, m; + +void DFS(int nod) +{ + viz.insert(nod); + + for (const int& vec : G[nod]) + if (viz.find(vec) == viz.end()) + DFS(vec); + + st.push(nod); +} + +void DFST(int nod, vector& v) +{ + viz.insert(nod); + + v.push_back(nod); + + for (const int& vec : GT[nod]) + if (viz.find(vec) == viz.end()) + DFST(vec, v); +} + + +int main(int argc, const char * argv[]) { + + ifstream fin("ctc.in"); + fin >> n >> m; + int x, y; + + for (int i = 0; i < m; ++i) + { + fin >> x >> y; + G[x].push_back(y); + GT[y].push_back(x); + } + + fin.close(); + + for (int nod = 1; nod <= n; ++nod) + if (viz.find(nod) == viz.end()) + DFS(nod); + + viz.clear(); + + for (; !st.empty(); ) + { + int nod = st.top(); + st.pop(); + + if (viz.find(nod) != viz.end()) + continue; + + vector v; + + DFST(nod, v); + + result.push_back(v); + } + + ofstream fout("ctc.out"); + + fout << result.size() << '\n'; + + for (int i = 0; i < result.size(); ++i) + { + for (const int& nod : result[i]) + fout << nod << ' '; + fout << '\n'; + } + + fout.close(); + + return 0; +} diff --git a/scc/kosaraju/145f1943-cc82-41bb-a1fe-e6bff4c86dc2.cpp b/scc/kosaraju/145f1943-cc82-41bb-a1fe-e6bff4c86dc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da74e711778d09520a28f3a60b32dbbbb942b5bc --- /dev/null +++ b/scc/kosaraju/145f1943-cc82-41bb-a1fe-e6bff4c86dc2.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int NMAX = 100'001; + +int n,m, beenThere[NMAX], strongly_conn; +vector G[NMAX] , GT[NMAX] , sol[NMAX]; +stack s; + +void Read() { + + f >> n >> m; + + while(m--) { + int x, y; + f >> x >> y; + G[x].push_back(y); + GT[y].push_back(x); + } +} + +void dfs(int node) { + + beenThere[node] = 1; + + for(int neighbor : G[node]) + if(!beenThere[neighbor]) + dfs(neighbor); + + s.push(node); +} + +void dfsReverse(int node) { + + beenThere[node] = 1; + sol[strongly_conn].push_back(node); + + for(int neighbor : GT[node]) + if(!beenThere[neighbor]) + dfsReverse(neighbor); +} + +void kosaraju() { + + for(int i = 1;i <= n;++i) + if(!beenThere[i]) + dfs(i); + + for(int i = 1;i <= n;++i) + beenThere[i] = 0; + + while(!s.empty()) { + int node = s.top(); + if(beenThere[node] == 0) { + strongly_conn++; + dfsReverse(node); + } + s.pop(); + } +} + +void print() { + + g << strongly_conn << '\n'; + + for(int i = 1;i <= strongly_conn;++i,g << '\n') + for(int node : sol[i]) + g << node << ' '; +} + +int main() { + + Read(); + kosaraju(); + print(); + + return 0; +} diff --git a/scc/kosaraju/147dd3d7-d2ea-479f-a623-24283897ecd9.cpp b/scc/kosaraju/147dd3d7-d2ea-479f-a623-24283897ecd9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e47922965c49c4e63d2aba183f308344181ff49e --- /dev/null +++ b/scc/kosaraju/147dd3d7-d2ea-479f-a623-24283897ecd9.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +bitset <100000 +1> seen; + +vector G[100000 +1], GT[100000 +1], S, tmp; + +vector > ans; + + + +void dfs (int x) { + + seen[x]=1; + + for (auto it: G[x]) + + if (!seen[it]) + + dfs(it); + + S.push_back(x); + +} + + + +void dfsT (int x) { + + seen[x]=1; + + for (auto it: GT[x]) + + if (!seen[it]) + + dfsT(it); + + tmp.push_back(x); + +} + + + +int main () { + + int n, m; + + ios::sync_with_stdio(false); + + fin >> n >> m; + + + + int i, j; + + for (; m; m--) { + + fin >> i >> j; + + G[i].push_back(j); + + GT[j].push_back(i); + + } + + + + for (i=1; i<=n; i++) + + if (!seen[i]) + + dfs(i); + + seen.reset(); + + + + int ct=0; + + for (auto it=S.rbegin(); it!=S.rend(); ++it) + + if (!seen[*it]) { + + ++ct; + + dfsT(*it); + + ans.push_back(tmp); + + tmp.clear(); + + } + + + + fout << ans.size() << '\n'; + + for (auto i: ans) { + + for (auto j: i) + + fout << j << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/14fe23aa-a1b6-462c-9244-a3ee41120c2e.cpp b/scc/kosaraju/14fe23aa-a1b6-462c-9244-a3ee41120c2e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4ef208f2aba0c1c2d1c5b5cae800288dce792b7 --- /dev/null +++ b/scc/kosaraju/14fe23aa-a1b6-462c-9244-a3ee41120c2e.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +vector < int > v1[100005], v2[100005], a[100005]; + +stack < int > s; + +int nr; + +bool viz1[100005], viz2[100005]; + + + +void bfs1 ( int nod ); + +void bfs2 ( int nod ); + + + +int main() + +{ + + int n, m, i, j, x, y; + + + + fin >> n >> m; + + for ( i = 1 ; i <= m ; i++ ) + + { + + fin >> x >> y; + + v1[x].push_back ( y ); + + v2[y].push_back ( x ); + + } + + + + for ( i = 1 ; i <= n ; i++ ) if ( viz1[i] == 0 ) bfs1 ( i ); + + + + while ( s.empty() == 0 ) + + { + + if ( viz2[s.top()] == 0 ) + + { + + nr++; + + bfs2 ( s.top() ); + + } + + + + s.pop(); + + } + + + + fout << nr << '\n'; + + for ( i = 1 ; i <= nr ; i++ ) + + { + + for ( j = 0 ; j < a[i].size() ; j++ ) fout << a[i][j] << ' '; + + fout << '\n'; + + } + + + + return 0; + +} + + + +void bfs1 ( int nod ) + +{ + + int i; + + + + viz1[nod] = 1; + + for ( i = 0 ; i < v1[nod].size() ; i++ ) if ( viz1[v1[nod][i]] == 0 ) bfs1 ( v1[nod][i] ); + + s.push ( nod ); + +} + + + +void bfs2 ( int nod ) + +{ + + int i; + + + + viz2[nod] = 1; + + for ( i = 0 ; i < v2[nod].size() ; i++ ) if ( viz2[v2[nod][i]] == 0 ) bfs2 ( v2[nod][i] ); + + a[nr].push_back ( nod ); + +} diff --git a/scc/kosaraju/15ae8711-f2d5-4d2f-b421-cf1fb9e9d313.cpp b/scc/kosaraju/15ae8711-f2d5-4d2f-b421-cf1fb9e9d313.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d78c1d5648c593c094bc733fdefa36dc1bd74f6 --- /dev/null +++ b/scc/kosaraju/15ae8711-f2d5-4d2f-b421-cf1fb9e9d313.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + + + +vector > G, GT,sol; + + + +int n, m; + + + +vector V; + +vector S,ans; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void read() + +{ + + fin >> n >> m; + + G = GT = vector>(n + 1); + + for (int i = 1; i <= m; i++) + + { + + int a, b; + + fin >> a >> b; + + G[a].push_back(b); + + GT[b].push_back(a); + + } + + + +} + + + +void dfs(int k) + +{ + + V[k] = true; + + for (auto x : G[k]) + + if (!V[x]) + + dfs(x); + + S.push_back(k); + +} + + + +void dfsGT(int k) + +{ + + V[k] = 1; + + for (auto x : GT[k]) + + if (!V[x]) + + dfsGT(x); + + ans.push_back(k); + + + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin.tie(0); + + read(); + + + + V = vector(n + 1, false); + + for (int i = 1; i <= n; i++) + + if (!V[i]) + + { + + dfs(i); + + + + } + + + + V = vector(n + 1, false); + + for (vector::reverse_iterator it = S.rbegin(); it != S.rend(); it++) + + if (!V[*it]) { + + ans.clear(); + + dfsGT(*it); + + sol.push_back(ans); + + + + } + + + + fout << sol.size()<<'\n'; + + for (auto it : sol) + + { + + for (auto ij : it) + + fout << ij << " "; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/1695c9c8-40db-4205-b9a3-12118a31712f.cpp b/scc/kosaraju/1695c9c8-40db-4205-b9a3-12118a31712f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a26a4fd6b59c72216893d92a5f1b3d690ddd54f --- /dev/null +++ b/scc/kosaraju/1695c9c8-40db-4205-b9a3-12118a31712f.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +using namespace std; + + + +vector compTareConexe1[200005]; + +vector gr[200005], gr_transpus[200005]; + +int n, m, nr, viz[200005], compTareConexe2[200005]; + +vector ord; + + + +void dfs1(int node) + +{ + + if(viz[node]) + + return ; + + viz[node] = 1; + + for(auto v : gr[node]) + + dfs1(v); + + ord.push_back(node); + +} + + + +void dfs2(int node, int index) + +{ + + if(compTareConexe2[node]) + + return ; + + compTareConexe2[node] = index; + + compTareConexe1[index].push_back(node); + + for(auto v : gr_transpus[node]) + + dfs2(v, index); + +} + + + +int main() + +{ + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + f>>n>>m; + + int x, y; + + for(int i = 0; i <= m; i++) + + { + + f>>x>>y; + + gr[x].push_back(y); + + gr_transpus[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + dfs1(i); + + for(int i = n - 1; i >= 0; i--) + + if(!compTareConexe2[ord[i]]) + + { + + nr++; + + dfs2(ord[i], nr); + + } + + g< +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector < int > M[100003], M_T[100003], M_afis[100003]; + +int Viz[100003], n, m, x, y, contor, Po[100003], nr; + +void dfs1 ( int nod ) + +{ + + Viz[nod] = 1; + + for ( vector < int > ::iterator it=M[nod].begin(); it != M[nod].end(); it++ ) + + if ( Viz[*it] == 0 ) + + dfs1(*it); + + Po[++nr] = nod; + +} + +void dfs2 ( int nod ) + +{ + + M_afis[contor].push_back(nod); + + Viz[nod] = 0; + + for ( vector < int > ::iterator it = M_T[nod].begin(); it != M_T[nod].end(); it++ ) + + if ( Viz[*it] ) + + dfs2(*it); + +} + + + +int main() + +{ + + f >> n >> m; + + for ( int i = 1; i <= m; i++ ) + + { + + f >> x >> y; + + M[x].push_back(y); + + M_T[y].push_back(x); + + } + + for ( int i = 1; i <= n; i++ ) + + if ( Viz[i] == 0 ) + + dfs1(i); + + for ( int i = n; i >= 1; i-- ) + + if ( Viz[Po[i]]) + + { + + contor++; + + dfs2(Po[i]); + + } + + g << contor << '\n'; + + for ( int i = 1; i <= contor; i++ ) + + { + + for ( vector < int > ::iterator it = M_afis[i].begin(); it != M_afis[i].end(); it ++ ) + + g << *it << " "; + + g << '\n';; + + } + + return 0; + +} diff --git a/scc/kosaraju/16fe415f-7bed-4da1-8350-6c6a0c16b4b6.cpp b/scc/kosaraju/16fe415f-7bed-4da1-8350-6c6a0c16b4b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fae7375bbd989b97f9f414e24788639e0d66696 --- /dev/null +++ b/scc/kosaraju/16fe415f-7bed-4da1-8350-6c6a0c16b4b6.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +stack < int > S; + + + +vector G[100005],GT[100005],CTC[100005]; + + + +int N, M, NrCTC; + +int beenThere[100005]; + + + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for(unsigned int i=0; i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int DIM = 100005; + + + +vector G[DIM]; + +vector GT[DIM]; + +vector A[DIM]; + + + +int n,m,x,y,ans=0; + + + +stack S; + + + +int viz[DIM]; + + + +void Read() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS1(int nod) + +{ + + viz[nod]=1; + + for(unsigned int i=0;i ::iterator it; + + for(it=A[i].begin();it!=A[i].end();it++) + + fout<<*it<<" "; + + fout<<'\n'; + + } + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + +} diff --git a/scc/kosaraju/174955cd-b950-4d5c-908c-0582d7e6ce3a.cpp b/scc/kosaraju/174955cd-b950-4d5c-908c-0582d7e6ce3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed42533e5abd11a7fdf52d466bd5fde8876791e6 --- /dev/null +++ b/scc/kosaraju/174955cd-b950-4d5c-908c-0582d7e6ce3a.cpp @@ -0,0 +1,150 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bool vizat1[100005], vizat2[100005]; + +vector graph1[100005]; + +vector graph2[100005]; + +vector ctc[100005]; + +stack ord; + +int n, m, ctc_size; + + + +void dfs1(int nod) + +{ + + vizat1[nod] = 1; + + unsigned len, i; + + int var; + + len = graph1[nod].size(); + + for(i = 0; i < len; i++) + + { + + var = graph1[nod][i]; + + if(!vizat1[var]) + + dfs1(var); + + } + + ord.push(nod); + +} + + + +void dfs2(int nod) + +{ + + vizat2[nod] = 1; + + unsigned len, i; + + int var; + + len = graph2[nod].size(); + + for(i = 0; i < len; i++) + + { + + var = graph2[nod][i]; + + if(!vizat2[var]) + + dfs2(var); + + } + + ctc[ctc_size].push_back(nod); + +} + + + +int main() + +{ + + int i, x, y; + + unsigned j, len; + + fin >> n >> m; + + for(i = 0; i < m; i++) + + { + + fin >> x >> y; + + graph1[x].push_back(y); + + graph2[y].push_back(x); + + } + + for(i = 1; i <= n; i++) + + if(!vizat1[i]) + + dfs1(i); + + while(!ord.empty()) + + { + + x = ord.top(); + + ord.pop(); + + if(!vizat2[x]) + + { + + dfs2(x); + + ctc_size++; + + } + + } + + fout << ctc_size << endl; + + for(i = 0; i < ctc_size; i++) + + { + + len = ctc[i].size(); + + for(j = 0; j < len; j++) + + fout << ctc[i][j] << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/1834f5f7-ddd0-43dd-afce-6ada27998fb7.cpp b/scc/kosaraju/1834f5f7-ddd0-43dd-afce-6ada27998fb7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65521551375ab2cafa2f1831f4a7501857791df5 --- /dev/null +++ b/scc/kosaraju/1834f5f7-ddd0-43dd-afce-6ada27998fb7.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int const NMAX = 100001; + + + +int n, m, nr_comp_tare_conexe, viz[NMAX]; + +vector arce[100001], arceTranspusa[NMAX]; + +stack stiva; + +vector componenteTareConexe[NMAX]; + + + +void citire() + +{ + + f >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + f >> x >> y; + + arce[x].push_back(y); + + arceTranspusa[y].push_back(x); + + } + +} + + + +void DFS(int nod) + +{ + + viz[nod] = 1; + + for(int i = 0; i < arce[nod].size(); i++) + + { + + int vecin = arce[nod][i]; + + if(viz[vecin] == 0) + + DFS(vecin); + + } + + stiva.push(nod); + +} + + + +void DFS_transpus(int nod) + +{ + + viz[nod] = 2; + + componenteTareConexe[nr_comp_tare_conexe].push_back(nod); + + for(int i = 0; i < arceTranspusa[nod].size(); i++) + + { + + int vecin = arceTranspusa[nod][i]; + + if(viz[vecin] == 1) + + DFS_transpus(vecin); + + } + +} + + + +void afis() + +{ + + g << nr_comp_tare_conexe << '\n'; + + for(int i = 1; i <= nr_comp_tare_conexe; i++) + + { + + for(int j = 0; j < componenteTareConexe[i].size(); j++) + + g << componenteTareConexe[i][j] << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citire(); + + + + for(int i = 1; i <= n; i++) + + if(viz[i] == 0) + + DFS(i); + + + + while(!stiva.empty()) + + { + + int nod_curent = stiva.top(); + + if(viz[nod_curent] == 1) + + { + + nr_comp_tare_conexe++; + + DFS_transpus(nod_curent); + + } + + stiva.pop(); + + } + + + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/18fa265c-6ca0-46b4-a72e-fb58534dc4b6.cpp b/scc/kosaraju/18fa265c-6ca0-46b4-a72e-fb58534dc4b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a643018c74d6e7dfd7f5567ed4e64c5a05c92e30 --- /dev/null +++ b/scc/kosaraju/18fa265c-6ca0-46b4-a72e-fb58534dc4b6.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +const int MAX_N = 100000; + + + +vector g[5 + MAX_N]; + +vector rev[5 + MAX_N]; + +bool vis[5 + MAX_N]; + +vector topo; + +int scc; + +vector comp[5 + MAX_N]; + + + +void dfs_topo(int node) { + + vis[node] = true; + + for (auto u : g[node]) { + + if (!vis[u]) { + + dfs_topo(u); + + } + + } + + topo.push_back(node); + +} + + + +void make_scc(int node) { + + vis[node] = true; + + comp[scc].push_back(node); + + for (auto u : rev[node]) { + + if (!vis[u]) + + make_scc(u); + + } + +} + + + +int main() { + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + + + int n, m; + + scanf("%d%d", &n, &m); + + for (int i = 1; i <= m; i++) { + + int u, v; + + scanf("%d%d", &u, &v); + + g[u].push_back(v); + + rev[v].push_back(u); + + } + + + + for (int i = 1; i <= n; i++) { + + if (!vis[i]) + + dfs_topo(i); + + } + + reverse(topo.begin(), topo.end()); + + for (int i = 1; i <= n; i++) + + vis[i] = false; + + + + for (auto node : topo) { + + if (!vis[node]) { + + scc++; + + make_scc(node); + + } + + } + + + + printf("%d\n", scc); + + for (int i = 1; i <= scc; i++) { + + for (auto node : comp[i]) + + printf("%d ", node); + + printf("\n"); + + } + + + + return 0; + +} diff --git a/scc/kosaraju/1973f066-a7db-4e1c-951a-874808cfdba9.cpp b/scc/kosaraju/1973f066-a7db-4e1c-951a-874808cfdba9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4ebd77d88f675ffbc7243b5c996c937e4e9ea6a --- /dev/null +++ b/scc/kosaraju/1973f066-a7db-4e1c-951a-874808cfdba9.cpp @@ -0,0 +1,149 @@ +#include +#include + + +constexpr auto max_n = 100005; + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, node_order_count, component_count, node_order[max_n], visited[max_n]; + +vector> graph, transposed_graph, components; + + + +void dfs1(int node) + +{ + + visited[node] = 1; + + + + for (int i = 0; i < (int)graph[node].size(); i++) + + if (!visited[graph[node][i]]) + + dfs1(graph[node][i]); + + + + node_order[node_order_count++] = node; + +} + + + +void dfs2(const int node) + +{ + + visited[node] = 2; + + + + components[component_count].push_back(node); + + + + for (int i = 0; i < (int)transposed_graph[node].size(); i++) + + if (visited[transposed_graph[node][i]] == 1) + + dfs2(transposed_graph[node][i]); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + graph.resize(max_n); + + transposed_graph.resize(max_n); + + components.resize(max_n); + + + + for (int i = 1; i <= m; i++) + + { + + int x, y; + + fin >> x >> y; + + + + graph[x].push_back(y); + + transposed_graph[y].push_back(x); + + } + + + + for (auto i = 1; i <= n; i++) + + if (!visited[i]) + + dfs1(i); + + + + for (auto i = node_order_count - 1; i >= 0; i--) + + { + + if (visited[node_order[i]] != 2) + + { + + dfs2(node_order[i]); + + component_count++; + + } + + } + + + + fout << component_count << "\n"; + + for (auto i = 0; i < component_count; i++) + + { + + for (auto node : components[i]) + + fout << node << " "; + + fout << "\n"; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/19e94536-5a0e-4efb-8147-f97acda4e280.cpp b/scc/kosaraju/19e94536-5a0e-4efb-8147-f97acda4e280.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28dd0d84bafd36a6cf332c4b33dcfdeeeaa7af90 --- /dev/null +++ b/scc/kosaraju/19e94536-5a0e-4efb-8147-f97acda4e280.cpp @@ -0,0 +1,63 @@ +#include +#include + +std::vector arce[100001]; +std::vector transpus[100001]; +std::vector> componente; +std::vector sort; +bool vazut[100001]; +bool vazut2[100001]; + +void sortare_topologica(int nod) { + vazut[nod] = true; + for (const int succ : arce[nod]) { + if (!vazut[succ]) { + sortare_topologica(succ); + } + } + sort.push_back(nod); +} + +void dfs2(int nod, std::vector& r) { + vazut2[nod] = true; + r.push_back(nod); + for (const int succ : transpus[nod]) { + if (!vazut2[succ]) { + dfs2(succ, r); + } + } +} + +int main() { + std::ifstream fin("ctc.in"); + std::ofstream fout("ctc.out"); + int n, m; + + fin >> n >> m; + for (int i = 0; i < m; i++) { + int a, b; + fin >> a >> b; + arce[a].push_back(b); + transpus[b].push_back(a); + } + + for (int i = 1; i <= n; i++) { + if (!vazut[i]) { + sortare_topologica(i); + } + } + for (int i = sort.size() - 1; i >= 0; i--) { + if (!vazut2[sort[i]]) { + componente.push_back({}); + dfs2(sort[i], componente.back()); + } + } + fout << componente.size() << '\n'; + for (const auto& c : componente) { + for (const int x : c) { + fout << x << ' '; + } + fout << '\n'; + } + return 0; +} diff --git a/scc/kosaraju/1b3a3fa2-c955-4eca-9cdd-8a52699d3e95.cpp b/scc/kosaraju/1b3a3fa2-c955-4eca-9cdd-8a52699d3e95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4857dd5a1c45606a7d9208c4b3684b1d272cfa66 --- /dev/null +++ b/scc/kosaraju/1b3a3fa2-c955-4eca-9cdd-8a52699d3e95.cpp @@ -0,0 +1,116 @@ +#include + + + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, x, y, conex, k, S[100001]; + +bool viz[100001]; + +vector G[100001], GT[100001], cx[100001]; + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:G[nod]) + + if(!viz[i]) + + dfs(i); + + S[++k]=nod; + +} + + + +void dfsgt(int nod) + +{ + + viz[nod]=1; + + cx[conex].push_back(nod); + + for(auto i:GT[nod]) + + if(!viz[i]) + + dfsgt(i); + +} + + + +int main() + +{ + + fin >> n >> m; + + while(m--) + + { + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs(i); + + for(int i=1; i<=n; i++) + + viz[i]=0; + + for(int i=k; i>=1; i--) + + if(!viz[S[i]]) + + { + + conex++; + + dfsgt(S[i]); + + } + + fout << conex << "\n"; + + for(int i=1; i<=conex; i++) + + { + + sort(cx[i].begin(), cx[i].end()); + + for(auto j:cx[i]) + + fout << j << " "; + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/1b3cbef4-25dd-4d8a-9e6e-e81477790828.cpp b/scc/kosaraju/1b3cbef4-25dd-4d8a-9e6e-e81477790828.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c3704551e05c57461af9675c257a9d7a9747514 --- /dev/null +++ b/scc/kosaraju/1b3cbef4-25dd-4d8a-9e6e-e81477790828.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +std::ifstream in("ctc.in"); + +std::ofstream out("ctc.out"); + +const int NMAX = 100000; + +std::vectorg[NMAX + 3],gt[NMAX + 3]; + +std::vectorsol[NMAX + 3]; + +std::stackst; + +int viz[NMAX + 3]; + +int visit[NMAX + 3]; + +int ans; + +void tpsort(int x) + +{ + + viz[x] = 1; + + for(int i = 0;i < g[x].size();i++) + + { + + if(!viz[g[x][i]]) + + tpsort(g[x][i]); + + } + + st.push(x); + +} + +void dfs(int u) + +{ + + visit[u] = 1; + + sol[ans].push_back(u); + + for(int i = 0;i < gt[u].size();i++) + + { + + if(!visit[gt[u][i]]) + + dfs(gt[u][i]); + + } + +} + +int main() + +{ + + int n,m,u,v,x; + + in>>n>>m; + + for(int i = 1;i <= m;i++) + + { + + in>>u>>v; + + g[u].push_back(v); + + gt[v].push_back(u); + + } + + for(int i = 1;i <= n;i++) + + { + + if(!viz[i]) + + tpsort(i); + + } + + while(!st.empty()) + + { + + x = st.top(); + + st.pop(); + + if(!visit[x]) + + { + + ans++; + + dfs(x); + + } + + } + + out< + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int N=100009; + +int n,m,nr; + +int d[N]; + +vector v1[N],v2[N],sol[N]; + +stack sk; + +void dfs(int nod) + +{ + + d[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + v1[x].push_back(y); + + v2[y].push_back(x); + + } + + solve(); + + return 0; + +} diff --git a/scc/kosaraju/1c971f8c-80be-42fd-a1a8-3e26796e4f67.cpp b/scc/kosaraju/1c971f8c-80be-42fd-a1a8-3e26796e4f67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..24d8155872fbf3b0016d79f2351f37b91c473c0e --- /dev/null +++ b/scc/kosaraju/1c971f8c-80be-42fd-a1a8-3e26796e4f67.cpp @@ -0,0 +1,182 @@ +#include +#include +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + + + +vector adiacenta[100009]; + +vector transpusa[100009]; + +vector componente_conexe[100009]; + +int vizitat [100009]; + +stack stiva; + + + + + +int nr_comp; + + + +void DFS(int nod) + +{ + + vizitat[nod] = 1; + + for(size_t i=0; i>N>>M; + + for(int i =1; i <=M ;i++) + + { + + in>>x>>y; + + adiacenta[x].push_back(y); + + transpusa[y].push_back(x); + + + + } + + + + + + + + for(int i = 1; i <= N; i++) + + { + + if(!vizitat[i]) + + DFS(i); + + } + + + + + + + + while(!stiva.empty()) + + { + + int nod = stiva.top(); + + if(vizitat[nod] == 1) + + { + + nr_comp ++; + + DFS2(nod); + + } + + + + stiva.pop(); + + } + + + + + + out< + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bool vizat1[100005], vizat2[100005]; + +vector graph1[100005]; + +vector graph2[100005]; + +vector ctc[100005]; + +stack ord; + +int n, m, ctc_size; + + + +void dfs1(int nod) + +{ + + vizat1[nod] = 1; + + unsigned len, i; + + int var; + + len = graph1[nod].size(); + + for(i = 0; i < len; i++) + + { + + var = graph1[nod][i]; + + if(!vizat1[var]) + + dfs1(var); + + } + + ord.push(nod); + +} + + + +void dfs2(int nod) + +{ + + vizat2[nod] = 1; + + unsigned len, i; + + int var; + + len = graph2[nod].size(); + + for(i = 0; i < len; i++) + + { + + var = graph2[nod][i]; + + if(!vizat2[var]) + + dfs2(var); + + } + + ctc[ctc_size].push_back(nod); + +} + + + +int main() + +{ + + int i, x, y; + + unsigned j, len; + + fin >> n >> m; + + for(i = 0; i < m; i++) + + { + + fin >> x >> y; + + graph1[x].push_back(y); + + graph2[y].push_back(x); + + } + + for(i = 1; i <= n; i++) + + if(vizat1[i] == 0) + + dfs1(i); + + while(!ord.empty()) + + { + + x = ord.top(); + + ord.pop(); + + if(!vizat2[x]) + + { + + dfs2(x); + + ctc_size++; + + } + + } + + fout << ctc_size << endl; + + for(i = 0; i < ctc_size; i++) + + { + + len = ctc[i].size(); + + for(j = 0; j < len; j++) + + fout << ctc[i][j] << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/1df1b877-5fee-46cc-879b-0ad1b4960f32.cpp b/scc/kosaraju/1df1b877-5fee-46cc-879b-0ad1b4960f32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c63c23a42d89c67be92ef652ece870594aa2a1a5 --- /dev/null +++ b/scc/kosaraju/1df1b877-5fee-46cc-879b-0ad1b4960f32.cpp @@ -0,0 +1,154 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +vector > G, GT, CTC; + +short viz[100005]; + +int TS[100005]; + +int N, M, k, nrCTC; + + + +void citire() + +{ + + in >> N >> M; + + G.resize(N + 2); + + GT.resize(N + 2); + + CTC.resize(N + 2); + + int x, y; + + for(int i = 1; i <= M; i++) + + { + + in >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS1(int z) + +{ + + viz[z] = 1; + + for(int i = 0; i < G[z].size(); i++) + + { + + int Vecin = G[z][i]; + + if(!viz[Vecin]) + + DFS1(Vecin); + + } + + TS[++k] = z; + +} + + + +void DFS2(int z) + +{ + + viz[z] = 2; + + CTC[nrCTC].push_back(z); + + for(int i = 0; i < GT[z].size(); i++) + + { + + int Vecin = GT[z][i]; + + if(viz[Vecin] == 1) + + DFS2(Vecin); + + } + +} + + + +int main() + +{ + + citire(); + + for(int i = 1; i <= N; i++) + + if(!viz[i]) + + DFS1(i); + + + + for(int i = N; i >= 1; i--) + + { + + if(viz[TS[i]] == 1) + + { + + nrCTC++; + + DFS2(TS[i]); + + } + + } + + + + out << nrCTC << '\n'; + + + + for(int i = 1; i <= nrCTC; i++) + + { + + for(int j = 0; j < CTC[i].size(); j++) + + out << CTC[i][j] << ' '; + + out << '\n'; + + } + + + +} diff --git a/scc/kosaraju/1e1d3be1-7ff9-4dfd-96d4-334a2dfc29c7.cpp b/scc/kosaraju/1e1d3be1-7ff9-4dfd-96d4-334a2dfc29c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a3b2a856fe9fc35b803b3b843a5ef037f87238a --- /dev/null +++ b/scc/kosaraju/1e1d3be1-7ff9-4dfd-96d4-334a2dfc29c7.cpp @@ -0,0 +1,150 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector g[100009], gt[100009], sol[100009]; + +stack H; + +int uz[100009]; + +int n,m; + +int nrc; + +void citire(); + +void dfs(int k); + +void dfs1(int k); + +void afis(); + + + +int main() + +{int i; + + citire(); + + for(i=1;i<=n;i++) + + if(!uz[i]) + + {nrc++;dfs(i);} + + nrc=0; + + memset(uz,0,sizeof(uz)); + + while(!H.empty()) + + { + + int act=H.top(); + + H.pop(); + + if(!uz[act]) + + nrc++,dfs1(act); + + } + + afis(); + + return 0; + +} + +void citire() + +{int i,x,y; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + +} + +void dfs1(int k) + +{ + + uz[k]=nrc; + + sol[nrc].push_back(k); + + for(int i=0;i +#include +using namespace std; + +ifstream cin ("ctc.in"); + +ofstream cout ("ctc.out"); + +vector lista[100005]; + +vector lista2[100005]; + +int viz[100005], k = 0, cnt = 0, st[100005], sol[200005]; + +void dfs(int nod) + +{ + + viz[nod] = 1; + + for (int i = 0; i < lista[nod].size(); i++) + + { + + int x = lista[nod][i]; + + if(!viz[x]) + + dfs(x); + + } + + st[++k] = nod; + +} + +void dfs2(int nod) + +{ + + viz[nod] = 2; + + for (int i = 0; i < lista2[nod].size(); i++) + + { + + int x = lista2[nod][i]; + + if (viz[x] == 1) + + dfs2(x); + + } + + sol[++cnt] = nod; + +} + +int main() + +{ + + int n, m, i, ans = 0; + + cin >> n >> m; + + for (i = 1; i <= m; i++) + + { + + int x, y; + + cin >> x >> y; + + lista[x].push_back(y); + + lista2[y].push_back(x); + + } + + for (i = 1; i <= n; i++) + + if (!viz[i]) + + dfs(i); + + while (k) + + { + + if (viz[st[k]] == 1) + + { + + ans++; + + dfs2(st[k]); + + sol[++cnt] = -1; + + } + + k--; + + } + + cout << ans << '\n'; + + for (i = 1; i <= cnt; i++) + + { + + if (sol[i] == -1) + + cout << '\n'; + + else + + cout << sol[i] << " "; + + } + + return 0; + +} diff --git a/scc/kosaraju/1e3b7db1-b209-48b3-9cee-94846e3e97e6.cpp b/scc/kosaraju/1e3b7db1-b209-48b3-9cee-94846e3e97e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74c278ce31f7efd850618685ffbd1cb991fc926d --- /dev/null +++ b/scc/kosaraju/1e3b7db1-b209-48b3-9cee-94846e3e97e6.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +const int MAX=100006; + +vectorgraf1[MAX]; + +vectorgraf2[MAX]; + +vectorcc[MAX]; + +int n,m,st,dr,cnt,nr,v[MAX]; + +bool vizs[MAX],vizp[MAX]; + + + +void dfss(int x) + +{ + + vizs[x]=true; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>st>>dr; + + graf1[st].push_back(dr); + + graf2[dr].push_back(st); + + } + + + + for(int i=1;i<=n;i++) + + { + + if(vizs[i]==false) dfss(i); + + } + + + + for(int i=nr;i>=1;i--) + + { + + if(vizp[v[i]]==false) + + { + + cnt++; + + dfsp(v[i]); + + } + + } + + + + out< +using namespace std; + +const int kNmax = 100005; + +class Task { +public: + void solve() { + read_input(); + print_output(get_result()); + } + +private: + int n; + int m; + + vector adj[kNmax]; + + vector adj_trans[kNmax]; + + vector> scc; + + + + vector visited; + + + + vector topsort; + + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + visited = vector(n + 1, 0); + for (int i = 1, x, y; i <= m; i++) { + fin >> x >> y; + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + fin.close(); + } + + vector> get_result() { + vector> sol; + kosaraju(); + sol = scc; + return sol; + } + + void kosaraju() { + + topsort.push_back(-1); + + + for (int i = 1; i <= n; ++i) { + if (!visited[i]) { + dfs(i); + } + } + + + + for (int i = n; i >= 1; --i) { + if (visited[topsort[i]]) { + + vector current_scc; + dfs_t(topsort[i], current_scc); + scc.push_back(current_scc); + } + } + } + + + void dfs(int node) { + visited[node] = 1; + + for (auto &ngh : adj[node]) { + if (!visited[ngh]) { + dfs(ngh); + } + } + + + topsort.push_back(node); + } + + + void dfs_t(int node, vector ¤t_scc) { + visited[node] = 0; + current_scc.push_back(node); + + for (auto &ngh : adj_trans[node]) { + if (visited[ngh]) { + dfs_t(ngh, current_scc); + } + } + } + + void print_output(const vector>& result) { + ofstream fout("ctc.out"); + fout << result.size() << '\n'; + for (const auto& ctc : result) { + for (int nod : ctc) { + fout << nod << ' '; + } + fout << '\n'; + } + fout.close(); + } +}; + + +int main() { + + + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/scc/kosaraju/21248623-4561-4f2a-b9d8-185c07f9dbda.cpp b/scc/kosaraju/21248623-4561-4f2a-b9d8-185c07f9dbda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c1b091e12f3e1ed18320329a85a6900efca09fe --- /dev/null +++ b/scc/kosaraju/21248623-4561-4f2a-b9d8-185c07f9dbda.cpp @@ -0,0 +1,190 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int DIM = 1e5 + 7; + + + +vector l[DIM]; + +vector lt[DIM]; + + + +int viz[DIM]; + + + +stack s; + + + +vector cc[DIM]; + + + +void dfs(int x) + +{ + + viz[x] = 1; + + + + for(int i = 0; i < l[x].size(); i++) + + { + + int vec = l[x][i]; + + + + if(viz[vec] == 0) + + dfs(vec); + + } + + + + s.push(x); + + + +} + + + +void dfst(int x,int cnt) + +{ + + viz[x] = 2; + + cc[cnt].push_back(x); + + + + for(int i = 0; i < lt[x].size(); i++) + + { + + int vec = lt[x][i]; + + + + if(viz[vec] == 1) + + { + + dfst(vec,cnt); + + } + + + + } + + + +} + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + in >> x >> y; + + + + l[x].push_back(y); + + lt[y].push_back(x); + + } + + + + for(int i = 1; i <= n; i++) + + if(viz[i] == 0) + + { + + dfs(i); + + } + + + + int cnt = 0; + + + + while(!s.empty()) + + { + + int nod = s.top(); + + s.pop(); + + + + if(viz[nod] == 1) + + { + + cnt++; + + dfst(nod,cnt); + + } + + } + + out << cnt <<'\n'; + + + + for(int i = 1; i <= cnt; i++) + + { + + for(int j = 0; j < cc[i].size(); j++) + + out << cc[i][j] <<" "; + + + + out <<'\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/21ab79ca-67ea-4198-a672-bfb2aa2972f0.cpp b/scc/kosaraju/21ab79ca-67ea-4198-a672-bfb2aa2972f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..febd2265ff81815361b21208b836f67dd1cb64ea --- /dev/null +++ b/scc/kosaraju/21ab79ca-67ea-4198-a672-bfb2aa2972f0.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + +int n, m, cc; + +vector cp[100005]; + +vector v[100005], in[100005]; + +vector sf; + +bitset<100005> viz; + +void dfs1(int nod) + +{ + + viz[nod] = 1; + + for(auto it:v[nod]) + + if(!viz[it]) + + dfs1(it); + + sf.push_back(nod); + +} + +void dfs2(int nod) + +{ + + viz[nod] = 1; + + for(auto it:in[nod]) + + if(!viz[it]) + + dfs2(it); + + cp[cc].push_back(nod); + +} + +void read(); + +int main() + +{ + + read(); + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs1(i); + + viz.reset(); + + for(int i=n-1; i>=0; i--) + + if(!viz[sf[i]]) + + { + + ++cc; + + dfs2(sf[i]); + + } + + fout << cc << '\n'; + + for(int i=1; i<=cc; i++) + + { + + sort(cp[i].begin(), cp[i].end()); + + for(auto it:cp[i]) + + fout << it << ' '; + + fout << '\n'; + + } + + return 0; + +} + +void read() + +{ + + fin >> n >> m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + fin >> x >> y; + + v[x].push_back(y); + + in[y].push_back(x); + + } + +} diff --git a/scc/kosaraju/224d400d-069c-4a64-91d4-05bedec69770.cpp b/scc/kosaraju/224d400d-069c-4a64-91d4-05bedec69770.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63c54cc875d61307eb2217abbfe5294257b02382 --- /dev/null +++ b/scc/kosaraju/224d400d-069c-4a64-91d4-05bedec69770.cpp @@ -0,0 +1,138 @@ +#include +#include +#include +using namespace std; + +int n, m, i, j, x, y, cnt; + +bool viz[100001]; + +vector gi[100001], go[100001], g[100001]; + +int lead [100001]; + +vector discovered; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +void DFP (int nod) + +{ + + viz[nod]=1; + + for (int i=0; i>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y; + + go[x].push_back(y); + + gi[y].push_back(x); + + } + + + + memset(viz, 0, sizeof (viz)); + + for (i=1;i<=n;i++) { + + if (viz[i]==0) { + + DFP(i); + + } + + } + + + + for (i=0;i<=n;i++) + + lead[i]=-1; + + for ( ; discovered.size(); discovered.pop_back() ) { + + if (lead[discovered.back()]==-1) { + + DFM (discovered.back(), cnt); + + cnt++; + + } + + } + + + + for (i=1;i<=n;i++) { + + g[lead[i]].push_back(i); + + } + + + + fout< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int N, M, K; + +vector G[100005], GT[100005], ans[100005], nodes; + +bool vis1[100005], vis2[100005]; + + + +void DFS1(int node) { + + vis1[node] = 1; + + for (auto it: G[node]) + + if (!vis1[it]) DFS1(it); + + nodes.push_back(node); + +} + +void DFS2(int node) { + + vis2[node] = 1; + + ans[K].push_back(node); + + for (auto it: GT[node]) + + if (!vis2[it]) DFS2(it); + +} + +int main() + +{ + + f >> N >> M; + + for (int i = 1; i <= M; ++i) { + + int x, y; + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for (int i = 1; i <= N; ++i) + + if (!vis1[i]) DFS1(i); + + + + reverse(nodes.begin(), nodes.end()); + + for (auto it : nodes) + + if (!vis2[it]) ++K, DFS2(it); + + + + g << K << '\n'; + + for (int i = 1; i <= K; ++i) { + + for (auto it: ans[i]) + + g << it << " "; + + g << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/24b00109-0265-4305-bcf0-419329751ffb.cpp b/scc/kosaraju/24b00109-0265-4305-bcf0-419329751ffb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f1504717bfbc5dc3a2a9edf501772335433e573 --- /dev/null +++ b/scc/kosaraju/24b00109-0265-4305-bcf0-419329751ffb.cpp @@ -0,0 +1,187 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m; + +int nr_componente; + +int viz[100005]; + +vector graf[100005]; + +vector graf_transpus[100005]; + +vector sol[100005]; + +stack a; + + + +void citire() + +{ + + int x, y; + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + fin >> x >> y; + + graf[x].push_back(y); + + graf_transpus[y].push_back(x); + + } + +} + + + +void DFS(int i) + +{ + + viz[i] = 1; + + for(unsigned int j = 0; j < graf[i].size(); j++) + + { + + if(!viz[graf[i][j]]) + + { + + viz[graf[i][j]] = 1; + + DFS(graf[i][j]); + + } + + } + + a.push(i); + +} + + + +void init_stack() + +{ + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS(i); + +} + + + +void DFS2(int i) + +{ + + viz[i] = 2; + + for(unsigned int j = 0; j < graf_transpus[i].size(); j++) + + if(viz[graf_transpus[i][j]] < 2) + + { + + viz[graf_transpus[i][j]] = 2; + + DFS2(graf_transpus[i][j]); + + } + + sol[nr_componente].push_back(i); + +} + + + + + +void solve() + +{ + + while(!a.empty()) + + { + + int nod = a.top(); + + a.pop(); + + if(viz[nod] > 1) + + continue; + + nr_componente++; + + DFS2(nod); + + } + +} + + + +void afisare() + +{ + + fout << nr_componente << '\n'; + + for(int i = 1; i <= nr_componente; i++) + + { + + for(auto &v : sol[i]) + + fout << v << ' '; + + fout << '\n'; + + } + +} + + + +int main() + +{ + + citire(); + + init_stack(); + + solve(); + + afisare(); + + return 0; + +} diff --git a/scc/kosaraju/24d1ac03-fbe1-4d8d-ae08-c12ecced668e.cpp b/scc/kosaraju/24d1ac03-fbe1-4d8d-ae08-c12ecced668e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbc0a09c1967014fb44bbefdb4b8f9c4a24993e6 --- /dev/null +++ b/scc/kosaraju/24d1ac03-fbe1-4d8d-ae08-c12ecced668e.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int DIM = 100005; + + + +int n,m,x,y,viz[DIM],cnt=0; + + + +stack St; + + + +vector G[DIM]; + +vector P[DIM]; + +vector Sol[DIM]; + + + +void DFS1(int nod) + +{ + + viz[nod]=1; + + for(auto it : G[nod]) + + { + + if(!viz[it]) + + DFS1(it); + + } + + St.push(nod); + +} + + + +void Read() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + P[y].push_back(x); + + } + +} + + + +void DFS2(int nod) + +{ + + viz[nod]=1; + + Sol[cnt].push_back(nod); + + for(auto it : P[nod]) + + { + + if(!viz[it]) + + DFS2(it); + + } + +} + + + +void Solve() + +{ + + for(int i=1;i<=n;i++) + + { + + if(!viz[i]) + + DFS1(i); + + } + + memset(viz,0,sizeof(viz)); + + while(!St.empty()) + + { + + int k=St.top(); + + if(!viz[k]) + + { + + cnt++; + + DFS2(k); + + } + + St.pop(); + + } + +} + + + +void Print() + +{ + + fout< +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +int n, m, x, y, nr; + +vector graf[100005], transpus[100005]; + +vector topologic; + +vector componente[100005]; + +bool viz[100005]; + + + +void Read() + +{ + + f>>n>>m; + + for(int i = 1;i <= m;++i) + + { + + f>>x>>y; + + graf[x].push_back(y); + + transpus[y].push_back(x); + + } + +} + + + +void dfs(int nod) + +{ + + viz[nod] = true; + + for(vector::iterator it = graf[nod].begin();it != graf[nod].end();++it) + + if(!viz[*it]) + + dfs(*it); + + topologic.push_back(nod); + +} + + + +void dfst(int nod) + +{ + + viz[nod] = false; + + componente[nr].push_back(nod); + + for(vector::iterator it = transpus[nod].begin();it != transpus[nod].end();++it) + + if(viz[*it]) + + dfst(*it); + +} + + + +int main() + +{ + + Read(); + + for(int i = 1;i <= n;++i) + + if(!viz[i]) + + dfs(i); + + for(int i = topologic.size() - 1;i >= 0;--i) + + if(viz[topologic[i]]) + + { + + ++nr; + + dfst(topologic[i]); + + } + + g<::iterator it = componente[i].begin();it != componente[i].end();++it) + + g<<*it<<" "; + + g<<'\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/27610423-42cd-47e2-a76a-5a883db1acc8.cpp b/scc/kosaraju/27610423-42cd-47e2-a76a-5a883db1acc8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec84d835231eeca2470a9e36a7a6b78abe6afce9 --- /dev/null +++ b/scc/kosaraju/27610423-42cd-47e2-a76a-5a883db1acc8.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int maxn = 1e5+5; + + + +int n, m, x, y, k, bn; + +int viz[maxn]; + +int member[maxn]; + + + +vector nod[maxn], tr[maxn], ans[maxn]; + +stack L; + + + +void visit(int x) { + + viz[x] = 1; + + for(auto u : nod[x]) { + + if(viz[u] == 0) { + + visit(u); + + } + + } + + L.push(x); + +} + + + +void assgn(int u) { + + ans[k].push_back(u); + + member[u] = k; + + viz[u] = 2; + + for(auto v : tr[u]) { + + if(viz[v] == 1) { + + assgn(v); + + } + + } + +} + + + +int main() + +{ + + int i; + + f >> n >> m; + + for(i = 1; i <= m; i ++) { + + f >> x >> y; + + nod[x].push_back(y); + + tr[y].push_back(x); + + } + + for(i = 1; i <= n; i ++) { + + if(viz[i] == 0) { + + visit(i); + + } + + } + + + + while(!L.empty()) { + + auto u = L.top(); + + if(viz[u] == 1) { + + ++k; + + assgn(u); + + } + + L.pop(); + + } + + + + g << k << '\n'; + + for(i = 1; i <= k; i ++) { + + for(auto u : ans[i]) { + + g << u << ' '; + + } + + g << '\n'; + + } + + + + f.close(); g.close(); + + return 0; + +} diff --git a/scc/kosaraju/286e2275-7bde-4dea-afe1-8d8095aebdf1.cpp b/scc/kosaraju/286e2275-7bde-4dea-afe1-8d8095aebdf1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f22b84fd40130bf0b2efa04d38680898d5dfe1ff --- /dev/null +++ b/scc/kosaraju/286e2275-7bde-4dea-afe1-8d8095aebdf1.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + +vector list_vec[nmax], list_rev[nmax], rez[nmax]; + +int viz[nmax]; + +stack t; + +int comp, n, m; + + + +void dfs1(int start) + +{ + + viz[start] = 1; + + for(int i : list_vec[start]) + + { + + if(!viz[i]) + + dfs1(i); + + } + + t.push(start); + +} + + + +void dfs2(int start) + +{ + + viz[start] = 0; + + rez[comp].push_back(start); + + + + for(int i : list_rev[start]) + + { + + if(viz[i]) + + dfs2(i); + + } + +} + +int main() + +{ + + int u, v, i; + + fin>>n>>m; + + for(i = 1; i <= m; ++i) + + { + + fin>>u>>v; + + list_vec[u].push_back(v); + + list_rev[v].push_back(u); + + } + + + + for(i = 1; i <= n; ++i) + + { + + if(!viz[i]) + + dfs1(i); + + } + + + + while(!t.empty()) + + { + + int temp = t.top(); + + t.pop(); + + if(viz[temp]) + + { + + ++comp; + + dfs2(temp); + + } + + } + + + + fout< + + +using namespace std; + + + +const int len = 100005; + +int n, m, x, y, cnt; + +vector g[len], gt[len], sol[len], v; + +bitset seen; + + + +void dfs1(int node) { + + seen[node] = true; + + for (auto& next : g[node]) + + if (!seen[next]) + + dfs1(next); + + v.push_back(node); + +} + + + +void dfs2(int node) { + + seen[node] = true; + + for (auto& next : gt[node]) + + if (!seen[next]) + + dfs2(next); + + sol[cnt].push_back(node); + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + cin.tie(0); + + cout.tie(0); + + + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + + cin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + if (!seen[i]) + + dfs1(i); + + + + for (auto& it : v) + + seen[it] = false; + + + + reverse(v.begin(), v.end()); + + for (auto& it : v) { + + if (!seen[it]) { + + cnt++; + + dfs2(it); + + } + + } + + + + cout << cnt << "\n"; + + for (int i = 1; i <= cnt; i++) { + + for (auto& it : sol[i]) + + cout << it << " "; + + cout << "\n"; + + } + +} diff --git a/scc/kosaraju/290229cd-431b-4c15-87c4-026952788100.cpp b/scc/kosaraju/290229cd-431b-4c15-87c4-026952788100.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e0bbb8a25211fe7c8b9fac334c9989265bc5d6d --- /dev/null +++ b/scc/kosaraju/290229cd-431b-4c15-87c4-026952788100.cpp @@ -0,0 +1,159 @@ +#include +#include + + +enum { normal, transpus }; + + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +int n, m, ctc, vis[100001], stack[100001], sp; + + + +vector G [100001]; + +vector Gt[100001]; + + + +vector sol[100001]; + + + +void DFS ( int nod, int Graf ) { + + vis[ nod ] = 1; + + + + int size = Graf == normal ? G[nod].size() : Gt[nod].size(); + + for ( int l = 0; l < size; l++ ) { + + + + int to = Graf == normal ? G[nod][l] : Gt[nod][l]; + + if ( !vis[to] ) + + DFS ( to, Graf ); + + } + + + + if ( Graf == normal ) + + stack[ ++sp ] = nod; + + else + + sol[ctc].push_back ( nod ); + +} + + + +void solve () { + + + + for ( int i = 1; i <= n; i++ ) + + if ( ! vis[ i ] ) + + DFS ( i, normal ); + + + + for ( int i = 1; i <= n; i++ ) + + vis[i] = 0; + + + + while ( sp > 0 ) { + + if ( ! vis[ stack[sp] ] ) { + + ctc++; + + DFS ( stack[sp], transpus ); + + } + + sp--; + + } + +} + + + +void write () { + + + + fout << ctc << "\n"; + + for (int i = 1; i <= ctc; i++) { + + for ( unsigned int l = 0; l < sol[i].size(); l++ ) { + + fout << sol[i][l] << " "; + + } + + fout << "\n"; + + } + +} + + + +void read () { + + fin >> n >> m; + + + + int from, to; + + for (int i = 1; i <= m; i++) { + + fin >> from >> to; + + G[ from ].push_back (to); + + Gt[ to ].push_back (from); + + } + +} + + + +int main() + +{ + + read (); + + solve (); + + write (); + + return 0; + +} diff --git a/scc/kosaraju/2904109f-29b6-46f3-9cf5-7910131700e0.cpp b/scc/kosaraju/2904109f-29b6-46f3-9cf5-7910131700e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..889c7b1ed64566e6cb0cc32687c2cc8dadc37d30 --- /dev/null +++ b/scc/kosaraju/2904109f-29b6-46f3-9cf5-7910131700e0.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int Nmax = 1e5 + 5; + +vector G[Nmax], GT[Nmax]; + +stack S; + +int Viz[Nmax]; + +int n, m, Nr_CTC; + +vector Rez[Nmax]; + + + +void Read() + +{ + + in>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + in>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS(int nod) + +{ + + Viz[nod] = 1; + + for(auto vec : G[nod]) + + if(!Viz[vec]) + + DFS(vec); + + S.push(nod); + +} + + + +void DFS_T(int nod) + +{ + + Viz[nod] = 2; + + Rez[Nr_CTC].push_back(nod); + + for(auto vec : GT[nod]) + + if(Viz[vec] == 1) + + DFS_T(vec); + +} + + + +void Solve() + +{ + + for(int i = 1; i <= n; i++) + + if(!Viz[i]) + + DFS(i); + + while(!S.empty()) + + { + + int Nod = S.top(); + + S.pop(); + + if(Viz[Nod] == 1) + + { + + Nr_CTC++; + + DFS_T(Nod); + + } + + } + +} + + + +void Print() + +{ + + out< +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 100005; + + + +int m, n; + +vectorG[NMAX]; + +vectorGt[NMAX]; + +vector >sol; + +vectorv; + +stackst; + +bool viz[NMAX]; + + + +void read() + +{ + + fin >> n >> m; + + while (m--) + + { + + int a, b; + + fin >> a >> b; + + G[a].push_back(b); + + Gt[b].push_back(a); + + } + +} + + + +void write() + +{ + + fout << sol.size() << "\n"; + + for (vectorv : sol) + + { + + for (int it : v) + + fout << it << " "; + + fout << "\n"; + + } + +} + + + +void dfs(int nod, stack& st) + +{ + + viz[nod] = true; + + for (vector::iterator it = G[nod].begin(); it != G[nod].end(); it++) + + { + + int nnod = *it; + + if (!viz[nnod]) + + dfs(nnod, st); + + } + + st.push(nod); + +} + + + +void dfsT(int nod, vector &v) + +{ + + viz[nod] = true; + + for (vector::iterator it = Gt[nod].begin(); it != Gt[nod].end(); it++) + + { + + int nnod = *it; + + if (!viz[nnod]) + + dfsT(nnod, v); + + } + + v.push_back(nod); + +} + + + +void solve() + +{ + + for (int i = 1; i <= n; i++) + + viz[i] = 0; + + + + for (int nod = 1; nod <= n; nod++) + + if (!viz[nod]) + + dfs(nod, st); + + + + for (int i = 1; i <= n; i++) + + viz[i] = 0; + + + + while (!st.empty()) + + { + + int nod = st.top(); + + v.clear(); + + st.pop(); + + if (!viz[nod]) + + { + + dfsT(nod, v); + + sort(v.begin(), v.end()); + + sol.push_back(v); + + } + + } + + + +} + + + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/scc/kosaraju/29ade982-024a-4ea6-b4cb-f56b5eefd732.cpp b/scc/kosaraju/29ade982-024a-4ea6-b4cb-f56b5eefd732.cpp new file mode 100644 index 0000000000000000000000000000000000000000..686b6ca149ba88e2b5f3a2a96cc3aa3d74453206 --- /dev/null +++ b/scc/kosaraju/29ade982-024a-4ea6-b4cb-f56b5eefd732.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stack < int > st; + +int viz[100005]; + +vector < int > adiac[100005]; + +vector < int > adiactrans[100005]; + +vector < int > compconex[100005]; + +int n,m; + +int nod1,nod2; + +int conex; + + + +void dfs(int nod) { + + viz[nod]=1; + + for (auto k=adiac[nod].begin(); k!=adiac[nod].end(); ++k) { + + if (viz[*k]==0) { + + dfs(*k); + + } + + } + + st.push(nod); + +} + + + +void dfs_t(int nod) { + + viz[nod]=2; + + compconex[conex].push_back(nod); + + for (auto k=adiactrans[nod].begin(); k!=adiactrans[nod].end(); ++k) { + + if (viz[*k]==1) { + + dfs_t(*k); + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1;i<=m;i++) { + + f >> nod1 >> nod2; + + adiac[nod1].push_back(nod2); + + adiactrans[nod2].push_back(nod1); + + } + + for (int i=1;i<=n;i++) { + + if (viz[i]==0) { + + dfs(i); + + } + + } + + while (st.empty()==0) { + + if (viz[st.top()]==1) { + + conex++; + + dfs_t(st.top()); + + } + + st.pop(); + + } + + g << conex << '\n'; + + for (int i=1;i<=conex;i++) { + + for (auto k=compconex[i].begin(); k!=compconex[i].end(); ++k) { + + g << *k <<" "; + + } + + g << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/29f53ff3-1002-4764-a3ff-16e4ded86241.cpp b/scc/kosaraju/29f53ff3-1002-4764-a3ff-16e4ded86241.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4ee43552c79be5830350388dfd1f755fafecae65 --- /dev/null +++ b/scc/kosaraju/29f53ff3-1002-4764-a3ff-16e4ded86241.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream o("ctc.out"); + + + +int n,m,cc=0; + +bool viz[100010],vizt[100010]; + +vector v[100010],vt[100010],ctc[100010]; + +stack S; + + + +void citire(){ + + f>>n>>m; int x,y,i; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + +} + +void dfs1(int k){ + + viz[k]=1; + + int i; + + for(i=0;i +#include +#include + + + +using namespace std; + +vector adj_list[200005], adj_list_transpus[200005], ordered_list, comp[200005]; + +int visited[200005], comp_nod[200005]; +int nrcomp; + +void dfs(int nod) { + if (visited[nod]) + return ; + + visited[nod] = 1; + + for (auto &vec : adj_list[nod]) { + dfs(vec); + } + ordered_list.push_back(nod); +} + +void dfs_comp(int nod, int index) +{ + if(comp_nod[nod]) + return; + + comp_nod[nod] = index; + comp[index].push_back(nod); + + for(auto it: adj_list_transpus[nod]) + { + dfs_comp(it, index); + } +} + +int main() +{ + ifstream f("ctc.in"); + ofstream g("ctc.out"); + + int n, m; + f >> n >> m; + while(m--) + { + int a, b; + f >> a >> b; + adj_list[a].push_back(b); + adj_list_transpus[b].push_back(a); + } + + for(int i = 1; i <= n; i++) { + if (!visited[i]) { + dfs(i); + } + } + + + + + + + for(int i = n - 1; i >= 0; i--) + { + if(!comp_nod[ordered_list[i]]) { + dfs_comp(ordered_list[i], ++nrcomp); + } + } + g<< nrcomp << "\n"; + for(int i = 1; i <= nrcomp; i++) + { + for(auto it: comp[i]) + { + g << it << " "; + } + g<< '\n'; + } +} diff --git a/scc/kosaraju/2affc53f-d6d2-4427-a847-b2a8671ef4b7.cpp b/scc/kosaraju/2affc53f-d6d2-4427-a847-b2a8671ef4b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46e32eaeb662c7180ceb3d8588fbbc96ec925497 --- /dev/null +++ b/scc/kosaraju/2affc53f-d6d2-4427-a847-b2a8671ef4b7.cpp @@ -0,0 +1,90 @@ +#include + + + +using namespace std; + + + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +vector < int > stk; +vector < bool > used; +vector < vector < int > > gr, grInv; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +void dfsInv(int node) { + used[node] = true; + for (int &x : grInv[node]) { + if (used[x]) continue; + dfsInv(x); + } + stk.emplace_back(node); +} + +void dfs(int node, vector < int > &accum) { + used[node] = true; + accum.emplace_back(node); + for (int &x : gr[node]) { + if (used[x]) continue; + dfs(x, accum); + } +} + + +int main() { + + + + + + + int n, m; + fin >> n >> m; + + gr.resize(n + 1); + grInv.resize(n + 1); + + for (int i = 0; i < m; ++ i) { + int a, b; + fin >> a >> b; + gr[a].emplace_back(b); + grInv[b].emplace_back(a); + } + + stk.reserve(n + 1); + used.resize(n + 1, false); + + for (int i = 1; i <= n; ++ i) { + if (used[i]) continue; + dfsInv(i); + } + + vector < vector < int > > sol; + fill(used.begin(), used.end(), false); + + while (not stk.empty()) { + int curr = stk.back(); + stk.pop_back(); + + if (used[curr]) continue; + + vector < int > aux; + dfs(curr, aux); + sol.emplace_back(aux); + } + + fout << (int)(sol).size() << '\n'; + for (auto &comp : sol) { + for (int &x : comp) { + fout << x << ' '; + } + fout << '\n'; + } +} diff --git a/scc/kosaraju/2b2b9087-bbf9-44f2-bd3d-b3f748fb1c17.cpp b/scc/kosaraju/2b2b9087-bbf9-44f2-bd3d-b3f748fb1c17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87b38017f0a954a08ca39a88cf428c76dc0a4205 --- /dev/null +++ b/scc/kosaraju/2b2b9087-bbf9-44f2-bd3d-b3f748fb1c17.cpp @@ -0,0 +1,164 @@ +#include +using namespace std; + +const int NMAX = 100010; + + + +FILE *IN, *OUT; + + + +int N, M, K; + +int srt[NMAX], ord[NMAX]; + +bool seen[NMAX]; + + + +vector edges[NMAX], vAns[NMAX], Gph[NMAX]; + + + +int pos, sign; + +char f[131072]; + + + +inline void Read(int &nr){ + + sign = 0; + + nr = 0; + + while(f[pos] < '0' || f[pos] > '9'){ + + if(f[pos] == '-') sign = 1; + + pos++; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + while(f[pos] >= '0' && f[pos] <= '9'){ + + nr = 10 * nr + f[pos++] - '0'; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + if(sign) nr =- nr; + +} + + + +void read(){ + + Read(N); Read(M); + + int a, b; + + for(int i = 1; i <= M; i++){ + + Read(a); Read(b); + + edges[a].push_back(b); + + Gph[b].push_back(a); + + } + +} + + + +void dfs(int node){ + + seen[node] = true; + + for(auto it : edges[node]) + + if(!seen[it]) + + dfs(it); + + srt[++K] = node; + +} + + + +void solDFS(int node){ + + seen[node] = false; + + vAns[K].push_back(node); + + for(auto it : Gph[node]) + + if(seen[it]) + + solDFS(it); + +} + + + +int main(){ + + + + IN = fopen("ctc.in", "r"); + + OUT = fopen("ctc.out", "w"); + + + + read(); + + + + for(int i = 1; i <= N; i++) + + if(!seen[i]) + + dfs(i); + + K = 0; + + for(int i = N; i > 0; i--) + + if(seen[srt[i]]) + + K++, solDFS(srt[i]); + + + + fprintf(OUT, "%d\n", K); + + for(int i = 1; i <= K; i++){ + + for(auto it : vAns[i]) + + fprintf(OUT, "%d ", it); + + fprintf(OUT, "\n"); + + } + + + + + + return 0; + +} diff --git a/scc/kosaraju/2c0e2dff-237e-4b62-95b9-86d4890b21d4.cpp b/scc/kosaraju/2c0e2dff-237e-4b62-95b9-86d4890b21d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a5c67459874d8072378fa26b55a9788bcecc1f4 --- /dev/null +++ b/scc/kosaraju/2c0e2dff-237e-4b62-95b9-86d4890b21d4.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +using namespace std; + + + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n, m, ctc; + +stack s; + +vector v[100005], t[100005], af[100005]; + +bool viz[100005]; + + + +void read() + +{ + + fin>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + fin>>x>>y; + + v[x].push_back(y); + + t[y].push_back(x); + + } + +} + + + +void dfs(int nod) + +{ + + cout< + + +using namespace std; + +ifstream fi("ctc.in"); + +ofstream fo("ctc.out"); + +vector V[100001],T[100001]; + +stack FIN; + +vector R[100001]; + +int n,m,rez; + +bool viz[100001],check[100001]; + +void dfs(int nod) + +{ + + if(viz[nod]) + + return; + + viz[nod]=1; + + for(auto i:V[nod]) + + dfs(i); + + FIN.push(nod); + +} + +void dfs1(int nod) + +{ + + if(check[nod]) + + return; + + check[nod]=1; + + for(auto i:T[nod]) + + dfs1(i); + + R[rez].push_back(nod); + +} + +int main() + +{ + + fi>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b; + + fi>>a>>b; + + V[a].push_back(b); + + T[b].push_back(a); + + } + + for(int i=1;i<=n;i++) + + { + + if(!viz[i]) + + dfs(i); + + } + + while(!FIN.empty()) + + { + + int start=FIN.top(); + + if(check[start]==0) + + { + + rez++; + + dfs1(start); + + } + + FIN.pop(); + + + + } + + fo< +#include +#include +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +bool f[100005], f2[100005]; +int n, m, nrCTC, maxim = -5, mat[100005][3]; +vector v[100005], ctc[100005], v2[100005]; +stack st; + +void dfs(int x) { + for(unsigned int i = 0; i < v[x].size(); ++i) { + int y = v[x][i]; + if(f[y] == 0) { + f[y] = 1; + dfs(y); + } + } + st.push(x); +} + +void dfs2(int x, int &nrMax) { + ctc[nrCTC].push_back(x); + for(unsigned int i = 0; i < v2[x].size(); ++i) { + int y = v2[x][i]; + if(f2[y] == 0) { + nrMax++; + f2[y] = 1; + dfs2(y, nrMax); + } + } +} + +int main() { + + in >> n >> m; + for(int i = 1; i <= m; ++i) { + int x, y; + in >> x >> y; + v[x].push_back(y); + v2[y].push_back(x); + } + + for(int i = 1; i <= n; ++i) { + if(f[i] == 0) { + f[i] = 1; + dfs(i); + } + } + + while(!st.empty()) { + int x = st.top(); + if(f2[x] == 0) { + f2[x] = 1; + int nrMax = 1; + ++nrCTC; + dfs2(x, nrMax); + if(nrMax > maxim) + maxim = nrMax; + } + st.pop(); + } + out << nrCTC << '\n'; + for(int i = 1; i <= nrCTC; ++i) { + for(unsigned int j = 0; j < ctc[i].size(); ++j) { + out << ctc[i][j] << ' '; + } + out << '\n'; + } + + return 0; +} diff --git a/scc/kosaraju/2eaa654c-7cd0-4762-9a38-451f37540c66.cpp b/scc/kosaraju/2eaa654c-7cd0-4762-9a38-451f37540c66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4dc50a951d069516cb6558d5d75a5bc12f0fd0a3 --- /dev/null +++ b/scc/kosaraju/2eaa654c-7cd0-4762-9a38-451f37540c66.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; +const int nMax = 100001; +vector< int > v1[nMax]; +vector< int > v2[nMax]; +stack< int > stiva; +bitset< nMax > vizitat; +int ctc[nMax]; +int co( 0 ); + +void dfs( int nod ) { + vizitat[ nod ] = true; + for ( vector< int >::iterator it = v1[ nod ].begin(); it != v1[ nod ].end(); it++ ) { + if ( vizitat[ *it ] == false ) + dfs( *it ); + } + stiva.push( nod ); +} + +void ctc_dfs( int nod ) { + ctc[ nod ] = co; + for ( vector< int >::iterator it = v2[ nod ].begin(); it != v2[ nod ].end(); it++ ) { + if ( ctc[ *it ] == 0 ) + ctc_dfs( *it ); + } +} + +int main() { + int n, m; + ifstream fin("ctc.in"); + FILE *fout; + fin >> n >> m; + for ( int i = 0; i < m; i++ ) { + int a, b; + fin >> a >> b; + a--; + b--; + v1[ a ].push_back( b ); + v2[ b ].push_back( a ); + } + for ( int i = 0; i < n; i++ ) + if ( vizitat[ i ] == false ) + dfs( i ); + while ( !stiva.empty() ) { + int nod = stiva.top(); + stiva.pop(); + if ( ctc[ nod ] == 0 ) { + co += 1; + ctc_dfs( nod ); + } + } + fout = fopen( "ctc.out", "w" ); + fprintf( fout, "%d\n", co ); + vector< int > afisare[n + 1]; + for ( int i = 0; i < n; i++ ) + afisare[ ctc[ i ] ].push_back( i + 1 ); + for ( int i = 1; i <= n; i++ ) { + for ( unsigned j = 0; j < afisare[ i ].size(); j++ ) { + fprintf( fout, "%d ", afisare[ i ][ j ] ); + } + fputc( '\n', fout ); + } + fclose( fout ); + return 0; +} diff --git a/scc/kosaraju/2eac852b-1da2-474b-bab7-9478a8f508c0.cpp b/scc/kosaraju/2eac852b-1da2-474b-bab7-9478a8f508c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c6455d711a6a7da61c6f3d9b815213a3f3a5137 --- /dev/null +++ b/scc/kosaraju/2eac852b-1da2-474b-bab7-9478a8f508c0.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include + + +using namespace std; + +FILE *fin, *fout; + +vector g[100009], gt[100009], gr[100009]; + +deque s; + +int n, ap[100009], nr=0; + +void dfs(int a) { + + ap[a]=1; + + for (int i=0;i +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stack < int > stiva; + + + +vector graf[100005],grafTranspus[100005],componente[100005]; + + + +int n, m, nr_componente; + +int viz[100005]; + + + + + + + +void dfs1(int x) + +{ + + viz[x] = 1; + + for( int i=0; i> n >> m; + + for(int i = 1; i <= m; i++) + + { + + int x,y; + + f >> x >> y; + + graf[x].push_back(y); + + grafTranspus[y].push_back(x); + + } + + + + + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs1(i); + + + + while(!stiva.empty()) + + { + + int x = stiva.top(); + + + + if (viz[x] == 1) + + { + + nr_componente++; + + dfs2(x); + + } + + stiva.pop(); + + } + + + + g << nr_componente <<"\n"; + + + + for(int i = 1; i <= nr_componente; i++) + + { + + for( int j = 0; j < componente[i].size(); j++) + + g << componente[i][j] <<" "; + + g<<"\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/2fc12875-9c95-40ab-9634-73bbebf6db86.cpp b/scc/kosaraju/2fc12875-9c95-40ab-9634-73bbebf6db86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d4d77b4c2bd24727250046c5950ac3c89cc3cd9 --- /dev/null +++ b/scc/kosaraju/2fc12875-9c95-40ab-9634-73bbebf6db86.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector g[100005], gt[100005], ctc[100005]; + +int n, m, nr; + +vector noduri; + +bool v[100005]; + + + +void citire() { + + fin >> n >> m; + + while(m--) { + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs1(int x) { + + v[x] = 1; + + for(auto next: g[x]) + + if(!v[next]) + + dfs1(next); + + noduri.push_back(x); + +} + + + +void dfs2(int x) { + + v[x] = 1; + + ctc[nr].push_back(x); + + for(auto next: gt[x]) + + if(!v[next]) + + dfs2(next); + +} + + + +int main() { + + citire(); + + for(int i = 1; i <= n; i++) + + if(!v[i]) + + dfs1(i); + + reverse(noduri.begin(), noduri.end()); + + memset(v, 0, sizeof v); + + for(auto x: noduri) + + if(!v[x]) { + + nr++; + + dfs2(x); + + } + + fout << nr << '\n'; + + for(int i = 1; i <= nr; i++) { + + for (auto x: ctc[i]) + + fout << x << ' '; + + fout << '\n'; + + } + +} diff --git a/scc/kosaraju/3000914e-c436-4032-bb0e-9041647db8ed.cpp b/scc/kosaraju/3000914e-c436-4032-bb0e-9041647db8ed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1a980a182a3c3e9cced69637a210e7ff8f466357 --- /dev/null +++ b/scc/kosaraju/3000914e-c436-4032-bb0e-9041647db8ed.cpp @@ -0,0 +1,117 @@ +#include +#include + + +using namespace std; + +vector ctc[200005], A[200005], A_tr[200005], ordered_list; + +int num_ctc = 0, n, m; + +int ctc_of[200005], viz[200005]; + + + +void dfs1(int node) { + + if(viz[node]) + + return; + + viz[node] = 1; + + for(int vecin : A[node]) { + + dfs1(vecin); + + } + + ordered_list.push_back(node); + +} + + + +void dfs2(int node, int index_ctc) + +{ + + if(ctc_of[node]) + + return; + + ctc_of[node] = index_ctc; + + + + ctc[index_ctc].push_back(node); + + for(int vecin : A_tr[node]) { + + dfs2(vecin, index_ctc); + + } + +} + + + +int main() { + + int i; + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + scanf("%d %d", &n, &m); + + for(i=1;i<=m;i++) { + + int a,b; + + scanf("%d %d", &a, &b); + + A[a].push_back(b); + + A_tr[b].push_back(a); + + } + + for(i=1;i<=n;i++) { + + if(!viz[i]) + + dfs1(i); + + } + + for(i=n-1;i>=0;i--) { + + if(!ctc_of[ordered_list[i]]) { + + dfs2(ordered_list[i], ++num_ctc); + + } + + } + + + + printf("%d\n", num_ctc); + + for(i=1;i<=num_ctc;i++) { + + for(int node : ctc[i]) { + + printf("%d ", node); + + } + + printf("\n"); + + } + + + +} diff --git a/scc/kosaraju/31b8ac8e-31bb-4bfd-9613-1b346e5141be.cpp b/scc/kosaraju/31b8ac8e-31bb-4bfd-9613-1b346e5141be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ce4e0a37ae10343f880517d191767e2caa45e79 --- /dev/null +++ b/scc/kosaraju/31b8ac8e-31bb-4bfd-9613-1b346e5141be.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +const int NMAX = 100001; + +int n, m, nrCTC; + +int vizitat[NMAX]; + +vector g[NMAX], gt[NMAX], ctc[NMAX]; + +stack s; + + + +void citire() { + + int i, x, y; + + + + fin >> n >> m; + + for (i = 1; i <= m; i++) { + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs(int nod) { + + int i; + + + + vizitat[nod] = 1; + + for (i = 0; i < g[nod].size(); i++) { + + if (vizitat[g[nod][i]] == 0) + + dfs(g[nod][i]); + + } + + s.push(nod); + +} + + + +void dfsT(int nod) { + + int i; + + + + vizitat[nod] = 2; + + ctc[nrCTC].push_back(nod); + + + + for (i = 0; i < gt[nod].size(); i++) { + + if (vizitat[gt[nod][i]] == 1) + + dfsT(gt[nod][i]); + + } + +} + + + +void kosaraju() { + + int i; + + + + for (i = 1; i <= n; i++) + + if (vizitat[i] == 0) + + dfs(i); + + + + while (!s.empty()) { + + int nod = s.top(); + + if (vizitat[nod] == 1) { + + nrCTC++; + + dfsT(nod); + + } + + s.pop(); + + } + +} + + + +void tiparire() { + + int i, j; + + + + fout << nrCTC << '\n'; + + for (i = 1; i <= nrCTC; i++) { + + for (j = 0; j < ctc[i].size(); j++) + + fout << ctc[i][j] << ' '; + + fout << '\n'; + + } + +} + + + +int main() { + + citire(); + + kosaraju(); + + tiparire(); + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/scc/kosaraju/31c60e56-ef8e-43f8-9631-de252317de2e.cpp b/scc/kosaraju/31c60e56-ef8e-43f8-9631-de252317de2e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..319c92f3bab1fe9be2bae1a95038e9670fe4d350 --- /dev/null +++ b/scc/kosaraju/31c60e56-ef8e-43f8-9631-de252317de2e.cpp @@ -0,0 +1,119 @@ +#include +#include + + +using namespace std; + +ifstream fi("ctc.in"); + +ofstream fo("ctc.out"); + +const int nmax=1e5; + +int n, m, nr, cnt; + +int viz[nmax+5], post[nmax+5]; + +vector sol[nmax+5], G[nmax+5], GT[nmax+5]; + + + +void dfsPost(int nod) + +{ + + viz[nod]=1; + + for(auto vec:GT[nod]) + + if(!viz[vec]) + + dfsPost(vec); + + post[++nr]=nod; + +} + + + +void dfs(int nod) + +{ + + viz[nod]=0; + + sol[cnt].push_back(nod); + + for(auto vec:G[nod]) + + if(viz[vec]) + + dfs(vec); + +} + + + +int main() + +{ + + fi>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + fi>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfsPost(i); + + + + for(int i=n; i>=1; i--) + + if(viz[post[i]]) + + { + + cnt++; + + dfs(post[i]); + + } + + + + fo< +#include +#include +using namespace std; + + + + + +vector d(100005 +1, 0); +vector LIST[100005], LISTT[100005], O, C[100005]; +int n, m, c; + +ofstream fout ("ctc.out"); + +void DFS (int x) +{ + d[x] = 1; + for (unsigned i = 0; i < LIST[x].size(); ++i) + if (!d[LIST[x][i]]) + DFS (LIST[x][i]); + O.push_back (x); +} + +void DFST (int x) +{ + d[x] = 0; + C[c].push_back (x); + for (unsigned i = 0; i < LISTT[x].size(); ++i) + if (d[LISTT[x][i]]) + DFST (LISTT[x][i]); +} + +int main () +{ + ifstream fin ("ctc.in"); + fin >> n >> m; + for (int i = 0; i < m; ++i) { + int x, y; + fin >> x >> y; + LIST[x].push_back (y); + LISTT[y].push_back (x); + } + fin.close(); + for (int i = 1; i <= n; ++i) { + sort (LIST[i].begin(), LIST[i].end()); + sort (LISTT[i].begin(), LISTT[i].end()); + } + for (int x = 1; x <= n; ++x) + if (!d[x]) + DFS (x); + for (int x = n-1; x >= 0; --x) + if (d[O[x]]) { + DFST (O[x]); + c++; + } + ofstream fout ("ctc.out"); + fout << c << "\n"; + for (int i = 0; i < c; ++i) { + for (unsigned j = 0; j < C[i].size(); ++j) + fout << C[i][j] << " "; + fout << "\n"; + } + return 0; +} diff --git a/scc/kosaraju/34aecae9-d8e3-40c6-8dcd-9a9af036e3b8.cpp b/scc/kosaraju/34aecae9-d8e3-40c6-8dcd-9a9af036e3b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf9a434f225abb2aa339367d1760ae334da7c570 --- /dev/null +++ b/scc/kosaraju/34aecae9-d8e3-40c6-8dcd-9a9af036e3b8.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int N = 1e5 + 5; + +vector g[N], gt[N], ordine, componenteConexe[N]; +bool vizitat[N], cc[N]; + +void dfs1(int nod) +{ + vizitat[nod] = true; + + for (int next : g[nod]) + if (!vizitat[next]) + dfs1(next); + + ordine.push_back(nod); +} + +void dfs2(int nod, int ccx) +{ + cc[nod] = true; + componenteConexe[ccx].push_back(nod); + + for (int next : gt[nod]) + if (!cc[next]) + dfs2(next, ccx); +} + +int main() +{ + int n, m; + fin >> n >> m; + + for (int i = 0; i < m; i++) + { + int x, y; + fin >> x >> y; + g[x].push_back(y); + gt[y].push_back(x); + } + + for (int i = 1; i <= n; i++) + { + if (!vizitat[i]) + dfs1(i); + } + + int cci = 1; + for (int i = ordine.size() - 1; i >= 0; i--) + { + if (!cc[ordine[i]]) + dfs2(ordine[i], cci++); + } + + fout << cci - 1 << '\n'; + for (int i = 1; i < cci; i++) + { + for (int nod : componenteConexe[i]) + fout << nod << ' '; + fout << '\n'; + } +} diff --git a/scc/kosaraju/35dae646-ff9f-44be-818d-502e471586e5.cpp b/scc/kosaraju/35dae646-ff9f-44be-818d-502e471586e5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49fd2fa113c3021912006a10c3acd7aa19770650 --- /dev/null +++ b/scc/kosaraju/35dae646-ff9f-44be-818d-502e471586e5.cpp @@ -0,0 +1,165 @@ +#include +using namespace std; + + + + + + +using ull = unsigned long long; +using ll = long long; +using pii = pair; +using pll = pair; + +template +ostream& operator<<(ostream &os, const vector &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } +template +ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } + +const int INF = INT_MAX; +const int MOD = 1e9 + 7; +const double EPS = 1e-9; +const double PI = acos(-1); + +ll t, n; + + +struct graph { + + vector> out, in; + + vector mark; + + vector ord, comp; + + vector> ans; + + + + graph(int n = 0) { + + out.resize(n + 1); + + in.resize(n + 1); + + mark.resize(n + 1, false); + + comp.resize(n + 1, -1); + + ans.resize(n + 1); + + } + + + + void add(int from, int to) { + + out[from].push_back(to); + + in[to].push_back(from); + + } + + + + void dfs_ord(int node) { + + if (mark[node]) return; + + mark[node] = true; + + + + for (auto next: out[node]) + + dfs_ord(next); + + + + ord.push_back(node); + + } + + + + void dfs_comp(int node, int c) { + + if (comp[node] != -1) return; + + comp[node] = c; + + ans[c].push_back(node); + + for (auto next: in[node]) + + dfs_comp(next, c); + + } + +}; + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + ifstream cin("ctc.in"); + ofstream cout("ctc.out"); + + + int m, from, to; + + cin >> n >> m; + + graph g(n); + + for (int i = 0; i < m; i++) { + + cin >> from >> to; + + g.add(from, to); + + } + + + + + + for (int i = 1; i <= n; i++) + + g.dfs_ord(i); + + g.mark.assign(n + 1, false); + + + + + + int c = 1; + + for (int i = n - 1; i >= 0; i--) { + + if (g.comp[g.ord[i]] == -1) { + + g.dfs_comp(g.ord[i], c); + + c++; + + } + + } + + + + cout << --c << '\n'; + + for (int i = 1; i <= c; i++) { + + for (auto v: g.ans[i]) + + cout << v << " "; + + cout << "\n"; + + } + + return 0; +} diff --git a/scc/kosaraju/367ed308-a2ce-47ea-8f93-9aa1df70465e.cpp b/scc/kosaraju/367ed308-a2ce-47ea-8f93-9aa1df70465e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2eb423274ece408bb4cebfa56a1177ce7e71fc7 --- /dev/null +++ b/scc/kosaraju/367ed308-a2ce-47ea-8f93-9aa1df70465e.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector Graf[100005], Graf_tr[100005], listaOrd, c_conex[100005]; + +int viz[100005], v_conex[100005]; + +int nr_conex = 0; + + + +void dfs(int node) + +{ + + if (viz[node] == 1) + + return; + + viz[node] = 1; + + for (auto i : Graf[node]) + + dfs(i); + + listaOrd.push_back(node); + +} + + + +void dfs1(int node, int index_conex) + +{ + + if (v_conex[node]) + + return; + + v_conex[node] = index_conex; + + c_conex[index_conex].push_back(node); + + for (auto i : Graf_tr[node]) + + { + + dfs1(i, index_conex); + + } + + + +} + + + +int main() { + + int n, m, a, b; + + fin >> n >> m; + + for (int i = 0; i < m; i++) + + { + + fin >> a >> b; + + Graf[a].push_back(b); + + Graf_tr[b].push_back(a); + + } + + for (int i = 1; i <= n; i++) + + { + + if (viz[i] != 1) + + { + + dfs(i); + + } + + } + + for (int i = n - 1; i >= 0; i--) + + { + + if (!v_conex[listaOrd[i]]) + + { + + nr_conex++; + + dfs1(listaOrd[i], nr_conex); + + } + + } + + fout << nr_conex << "\n"; + + for (int i = 1; i <= nr_conex; i++) + + { + + for (auto j : c_conex[i]) + + { + + fout << j << " "; + + } + + fout << "\n"; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/368bcbb1-6512-479b-9c9f-2ea96b01908b.cpp b/scc/kosaraju/368bcbb1-6512-479b-9c9f-2ea96b01908b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c45a474e07eea7aad3698b01c6bc9729662c17b --- /dev/null +++ b/scc/kosaraju/368bcbb1-6512-479b-9c9f-2ea96b01908b.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +const int len = 100005; + +int n, m, x, y, cnt; + +vector g[len], gt[len], sol[len], v; + +bitset seen1, seen2; + + + +void dfs1(int node) { + + seen1[node] = true; + + for (auto& next : g[node]) + + if (!seen1[next]) + + dfs1(next); + + v.push_back(node); + +} + + + +void dfs2(int node) { + + seen2[node] = true; + + for (auto& next : gt[node]) + + if (!seen2[next]) + + dfs2(next); + + sol[cnt].push_back(node); + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + cin.tie(0); + + cout.tie(0); + + + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + + cin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + if (!seen1[i]) + + dfs1(i); + + + + reverse(v.begin(), v.end()); + + for (auto& it : v) { + + if (!seen2[it]) { + + cnt++; + + dfs2(it); + + } + + } + + + + cout << cnt << "\n"; + + for (int i = 1; i <= cnt; i++) { + + for (auto& it : sol[i]) + + cout << it << " "; + + cout << "\n"; + + } + +} diff --git a/scc/kosaraju/378b9111-8538-456f-8dfb-4b6cd649b11f.cpp b/scc/kosaraju/378b9111-8538-456f-8dfb-4b6cd649b11f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d64152753a34990630952226f948e85a5b79df5 --- /dev/null +++ b/scc/kosaraju/378b9111-8538-456f-8dfb-4b6cd649b11f.cpp @@ -0,0 +1,139 @@ +#include +#include + + +using namespace std; + + + + + + + +vector ctc[200005]; + +int num_ctc, n, m; + +int ctc_of[200005], viz[200005]; + +vector graph[200005], graph_t[200005], ordered_list; + + + +void dfs1(int node) { + + if(viz[node]) + + return ; + + + + viz[node] = 1; + + for(auto vecin : graph[node]) { + + dfs1(vecin); + + } + + ordered_list.push_back(node); + +} + + + +void dfs2(int node, int index_ctc) { + + if(ctc_of[node]) + + return ; + + + + ctc_of[node] = index_ctc; + + + + ctc[index_ctc].push_back(node); + + + + for(auto vecin : graph_t[node]) { + + dfs2(vecin, index_ctc); + + } + +} + + + +int main() { + + int a, b; + + + + freopen("ctc.in","r",stdin); + +freopen("ctc.out", "w",stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d", &a, &b); + + graph[a].push_back(b); + + graph_t[b].push_back(a); + + } + + + + for(int i = 1; i <= n; i++) { + + if(!viz[i]) { + + dfs1(i); + + } + + } + + + + for(int i = n - 1; i >= 0; i--) { + + if(!ctc_of[ordered_list[i]]) { + + dfs2(ordered_list[i], ++num_ctc); + + } + + } + + + + printf("%d\n", num_ctc); + + for(int i = 1; i <= num_ctc; i++) { + + for(auto node : ctc[i]) { + + printf("%d ", node); + + } + + printf("\n"); + + } + + + + return 0; + +} diff --git a/scc/kosaraju/3932c470-18b6-4a2a-8906-8a73d48d4c82.cpp b/scc/kosaraju/3932c470-18b6-4a2a-8906-8a73d48d4c82.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d64152753a34990630952226f948e85a5b79df5 --- /dev/null +++ b/scc/kosaraju/3932c470-18b6-4a2a-8906-8a73d48d4c82.cpp @@ -0,0 +1,139 @@ +#include +#include + + +using namespace std; + + + + + + + +vector ctc[200005]; + +int num_ctc, n, m; + +int ctc_of[200005], viz[200005]; + +vector graph[200005], graph_t[200005], ordered_list; + + + +void dfs1(int node) { + + if(viz[node]) + + return ; + + + + viz[node] = 1; + + for(auto vecin : graph[node]) { + + dfs1(vecin); + + } + + ordered_list.push_back(node); + +} + + + +void dfs2(int node, int index_ctc) { + + if(ctc_of[node]) + + return ; + + + + ctc_of[node] = index_ctc; + + + + ctc[index_ctc].push_back(node); + + + + for(auto vecin : graph_t[node]) { + + dfs2(vecin, index_ctc); + + } + +} + + + +int main() { + + int a, b; + + + + freopen("ctc.in","r",stdin); + +freopen("ctc.out", "w",stdout); + + + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + scanf("%d%d", &a, &b); + + graph[a].push_back(b); + + graph_t[b].push_back(a); + + } + + + + for(int i = 1; i <= n; i++) { + + if(!viz[i]) { + + dfs1(i); + + } + + } + + + + for(int i = n - 1; i >= 0; i--) { + + if(!ctc_of[ordered_list[i]]) { + + dfs2(ordered_list[i], ++num_ctc); + + } + + } + + + + printf("%d\n", num_ctc); + + for(int i = 1; i <= num_ctc; i++) { + + for(auto node : ctc[i]) { + + printf("%d ", node); + + } + + printf("\n"); + + } + + + + return 0; + +} diff --git a/scc/kosaraju/393d514a-1a3f-47e0-a56c-58d91e058287.cpp b/scc/kosaraju/393d514a-1a3f-47e0-a56c-58d91e058287.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c95cb399da5fac957547d607e5cb3f6dc06e8a --- /dev/null +++ b/scc/kosaraju/393d514a-1a3f-47e0-a56c-58d91e058287.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +const int NMAX = 100001; + +int n, m, nrCTC; + +int vizitat[NMAX]; + +vector g[NMAX], gt[NMAX], ctc[NMAX]; + +stack s; + + + +void citire() { + + int i, x, y; + + + + fin >> n >> m; + + for (i = 1; i <= m; i++) { + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs(int nod) { + + int i; + + + + vizitat[nod] = 1; + + for (i = 0; i < g[nod].size(); i++) { + + if (vizitat[g[nod][i]] == 0) + + dfs(g[nod][i]); + + } + + s.push(nod); + +} + + + +void dfsT(int nod) { + + int i; + + + + vizitat[nod] = 2; + + ctc[nrCTC].push_back(nod); + + + + for (i = 0; i < gt[nod].size(); i++) { + + if (vizitat[gt[nod][i]] == 1) + + dfsT(gt[nod][i]); + + } + +} + + + +void kosaraju() { + + int i; + + + + for (i = 1; i <= n; i++) + + if (vizitat[i] == 0) + + dfs(i); + + + + while (!s.empty()) { + + int nod = s.top(); + + if (vizitat[nod] == 1) { + + nrCTC++; + + dfsT(nod); + + } + + s.pop(); + + } + +} + + + +void tiparire() { + + int i, j; + + + + fout << nrCTC << '\n'; + + for (i = 1; i <= nrCTC; i++) { + + for (j = 0; j < ctc[i].size(); j++) + + fout << ctc[i][j] << ' '; + + fout << '\n'; + + } + +} + + + +int main() { + + citire(); + + kosaraju(); + + tiparire(); + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/scc/kosaraju/39ee2989-d126-4eb0-a367-b7c0e68fb16c.cpp b/scc/kosaraju/39ee2989-d126-4eb0-a367-b7c0e68fb16c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c89bf56f10d4e02576c4e6c5471c48512c5faa1 --- /dev/null +++ b/scc/kosaraju/39ee2989-d126-4eb0-a367-b7c0e68fb16c.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int visited[100005], visited_t[100005], scc,n,m; +vector edges[100005]; +vector edges_t[100005]; +vector solution[100005]; +stack s; + +void dfs(int vertex){ + int i; + visited[vertex] = 1; + for(i = 0;i < edges[vertex].size();i++){ + if(!visited[edges[vertex][i]]){ + dfs(edges[vertex][i]); + } + } + s.push(vertex); +} + +void dfs_t(int vertex){ + int i; + visited_t[vertex] = 1; + solution[scc].push_back(vertex); + for(i = 0;i < edges_t[vertex].size();i++){ + if(!visited_t[edges_t[vertex][i]]){ + dfs_t(edges_t[vertex][i]); + } + } + +} + +void read(){ + int i,x,y; + fin>>n>>m; + for(i=1;i<=m;i++){ + fin>>x>>y; + edges[x].push_back(y); + edges_t[y].push_back(x); + } +} + +int main() +{ + read(); + int i,j; + for(i = 1;i <= n;i++){ + if(!visited[i]){ + dfs(i); + } + } + while(!s.empty()){ + if(!visited_t[s.top()]){ + scc++; + dfs_t(s.top()); + } + s.pop(); + } + + fout< +#include + + + + +using namespace std; + +ifstream fi("ctc.in"); + +ofstream fo("ctc.out"); + +struct nod{ + + int x; + + nod*urm; + + }; + + nod *L[100003],*LT[100003],*ctc[100003]; + + nod*p; + + int viz[100003],vizt[100003],i,j,k,n,m,x,y; + + int st[100003],nc; + +void adauga(int i,int j){ + + nod*p; + + p=new nod; + + p->x=j; + + + + p->urm=L[i]; + + L[i]=p; + + } + + void adaugat(int i,int j){ + + nod*p; + + p=new nod; + + p->x=i; + + + + p->urm=LT[j]; + + LT[j]=p; + + } + + void adaugactc(int i,int j){ + + nod*p; + + p=new nod; + + p->x=j; + + p->urm=ctc[i]; + + ctc[i]=p; + + } + +void dfs(int j){ + + nod *p; + + viz[j]=1; + + p=L[j]; + + while(p){ + + if(viz[p->x]==0)dfs(p->x); + + p=p->urm; + + } + + st[++k]=j; + +} + +void dfst(int j){ + + nod *p; + + adaugactc(nc,j); + + vizt[j]=1; + + p=LT[j]; + + while(p){ + + if(vizt[p->x]==0)dfst(p->x); + + p=p->urm; + + } + +} + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++){ + + fi>>x>>y; + + adauga(x,y); + + adaugat(x,y); + + } + + for(i=1;i<=n;i++) + + if(viz[i]==0)dfs(i); + + + + for(i=k;i>=1;i--) + + if(vizt[st[i]]==0) + + {nc++;dfst(st[i]);} + + fo<x<<" "; + + p=p->urm; + + } + + fo<<'\n'; + + } +return 0; + +} diff --git a/scc/kosaraju/3ad26d1f-0556-4595-9416-64337c64ac3a.cpp b/scc/kosaraju/3ad26d1f-0556-4595-9416-64337c64ac3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7866d598089a879379957d281620cbf0b16e9104 --- /dev/null +++ b/scc/kosaraju/3ad26d1f-0556-4595-9416-64337c64ac3a.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +int n, m, nr_comp, viz[100005]; +vector G[100005], G2[100005], comp[100005]; +stack S; + +void dfs(int vf) { + viz[vf] = -1; + for (auto &v:G[vf]) + if (viz[v] == 0) + dfs(v); + S.push(vf); +} + +void incaUnDFS(int vf, int nrc) { + viz[vf] = 1; + for (auto &v:G2[vf]) + if (viz[v] < 0) + incaUnDFS(v, nrc); + comp[nrc].push_back(vf); +} + +void citire() { + int x, y; + + f >> n >> m; + for (int i = 0; i < m; ++i) { + f >> x >> y; + G[x].push_back(y); + G2[y].push_back(x); + } +} + +void afisare() { + g << nr_comp << "\n"; + for (int i = 1; i <= nr_comp; ++i) { + for (auto &v:comp[i]) + g << v << " "; + g << "\n"; + } +} + + +int main() { + citire(); + for (int i = 1; i <= n; ++i) + if (viz[i] == 0) + dfs(i); + while (!S.empty()) { + int vf = S.top(); + S.pop(); + if (viz[vf] > 0) + continue; + nr_comp++; + incaUnDFS(vf, nr_comp); + } + afisare(); + return 0; +} diff --git a/scc/kosaraju/3b34bda5-9b11-4e10-82b4-04e86d4c8e4a.cpp b/scc/kosaraju/3b34bda5-9b11-4e10-82b4-04e86d4c8e4a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3066bb6691fdc31f73f7351beab8d597ebb3ca05 --- /dev/null +++ b/scc/kosaraju/3b34bda5-9b11-4e10-82b4-04e86d4c8e4a.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int N, M, ans; + +vector G[100005], GT[100005], nodes, ctc[100005]; + +bool vis[100005]; + + + +void DFS1(int node) { + + vis[node] = 1; + + for (auto it: G[node]) + + if (!vis[it]) DFS1(it); + + nodes.push_back(node); + +} + +void DFS2(int node) { + + vis[node] = 1; + + ctc[ans].push_back(node); + + for (auto it: GT[node]) + + if (!vis[it]) DFS2(it); + +} + +int main() + +{ + + f >> N >> M; + + for (int i = 1; i <= M; ++i) { + + int x, y; + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for (int i = 1; i <= N; ++i) + + if (!vis[i]) DFS1(i); + + reverse(nodes.begin(), nodes.end()); + + memset(vis, 0, sizeof(vis)); + + + + for (auto it : nodes) + + if (!vis[it]) ++ans, DFS2(it); + + + + g << ans << '\n'; + + for (int i = 1; i <= ans; ++i) { + + for (auto it : ctc[i]) + + g << it << " "; + + g << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/3c864137-67cc-4043-a21c-5c336f6185ff.cpp b/scc/kosaraju/3c864137-67cc-4043-a21c-5c336f6185ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ca76ae89a71581b60605ecf669f2c34ce3a2859 --- /dev/null +++ b/scc/kosaraju/3c864137-67cc-4043-a21c-5c336f6185ff.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +using namespace std; + +void dfs(vector>& edges, stack& result, vector& visited, int start = 1, int time = 1) { + + visited[start] = time; + + for (auto elem : edges[start]) { + + if (visited[elem] == 0) { + + dfs(edges, result, visited, elem, time); + + } + + } + + result.push(start); + +} + +void reverseDFS_result(stack& result, vector& visited, vector>& edges) { + + for (int i = 0; i < visited.size(); i++) { + + if (visited[i] == 0) { + + dfs(edges, result, visited, i); + + } + + } + +} + +void strongComponents(stack& result, vector>& edges) { + + ofstream out("ctc.out"); + + int n = result.size(); + + int time = 1, visit; + + vector visited(n, 0); + + for (int i = 0; i < n; i++) { + + stack s; + + visit = result.top(); + + result.pop(); + + if (visited[visit] == 0) { + + dfs(edges, s, visited, visit, time); + + } + + if (!s.empty()) { + + time++; + + } + + } + + vector> strongComponents(time); + + for (int i = 1; i < visited.size(); i++) { + + strongComponents[visited[i]].push_back(i); + + } + + out << time - 2 << "\n"; + + for (int i = 1; i < time; i++) { + + for (auto elem : strongComponents[i]) { + + out << elem << " "; + + } + + out << "\n"; + + } + + out.close(); + + return; + +} + +int main() + +{ + + ios_base::sync_with_stdio(false); + + int n, m, s, x, y; + + ifstream in("ctc.in"); + + in >> n >> m; + + vector> rEdges(n + 1); + + vector> edges(n + 1); + + for (int i = 0; i < m; i++) { + + in >> x >> y; + + edges[x].push_back(y); + + rEdges[y].push_back(x); + + } + + stack result; + + vector visited(n + 1, 0); + + reverseDFS_result(result, visited, rEdges); + strongComponents(result, edges); + + in.close(); + + return 0; + +} diff --git a/scc/kosaraju/3c866d40-e2e1-4a81-afe1-0cbd5a31a6f9.cpp b/scc/kosaraju/3c866d40-e2e1-4a81-afe1-0cbd5a31a6f9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fcab0ced701306dc8b94aa1a895e3a1b8287b43 --- /dev/null +++ b/scc/kosaraju/3c866d40-e2e1-4a81-afe1-0cbd5a31a6f9.cpp @@ -0,0 +1,114 @@ +#include + + + + + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +int n, m, nr; + +vectorlst, lst2; + +vectorv[100001], tr[100001], ctc[100001]; + +bool viz[200001], viz2[100001]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(int i=0; i>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + f>>x>>y; + + v[x].push_back(y); + + tr[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs(i); + + while(!lst.empty()) + + { + + int nod=lst.back(); + + lst.pop_back(); + + if(!viz2[nod]) + + { + + nr++; + + dfs2(nod); + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int n, m, k; + +int sortat[100001]; + +bool vizitat[100001]; + +vector > graf(200002), grafInv(200002), rezultat; + +queue coada; + + + +void sortareTopologica(){ + + int nod = coada.front(); + + coada.pop(); + + vizitat[nod] = true; + + + + for(size_t i = 0; i < graf[nod].size(); ++i) + + if(!vizitat[graf[nod][i]]){ + + coada.push(graf[nod][i]); + + sortareTopologica(); + + } + + sortat[++k] = nod; + +} + + + +void kosaraju(){ + + int nod = coada.front(); + + coada.pop(); + + vizitat[nod] = false; + + rezultat[rezultat.size() - 1].push_back(nod); + + + + for(size_t i = 0; i < grafInv[nod].size(); ++i) + + if(vizitat[grafInv[nod][i]]){ + + coada.push(grafInv[nod][i]); + + kosaraju(); + + } + +} + + + +int main() + +{ + + in>>n>>m; + + while(m--){ + + int x, y; + + in>>x>>y; + + + + graf[x].push_back(y); + + grafInv[y].push_back(x); + + } + + + + for(int i = 1; i <= n; ++i) + + if(!vizitat[i]){ + + coada.push(i); + + sortareTopologica(); + + } + + + + while(!coada.empty()) + + coada.pop(); + + + + vector aux; + + for(int i = n; i >= 1; --i) + + if(vizitat[sortat[i]]){ + + rezultat.push_back(aux); + + coada.push(sortat[i]); + + kosaraju(); + + } + + + + out< + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int N = 1e5 + 5; + +int n, m, i, j, scc, nod; + +int viz[N]; + +stack stk; + +vector ans[N]; + +vector Gx[N]; + +vector Gy[N]; + + + +void dfs1(int x) + +{ + + viz[x] = 1; + + vector::iterator it; + + for(it = Gx[x].begin(); it < Gx[x].end(); it++) + + if(viz[*it] == 0) dfs1(*it); + + stk.push(x); + +} + + + +void dfs2(int x, int scc) + +{ + + viz[x] = 2; + + ans[scc].push_back(x); + + vector::iterator it; + + for(it = Gy[x].begin(); it < Gy[x].end(); it++) + + if(viz[*it] == 1) dfs2(*it, scc); + +} + + + +int main() + +{ + + in >> n >> m; + + + + for(i = 1; i <= m; i++) + + { + + int x, y; + + in >> x >> y; + + Gx[x].push_back(y); + + Gy[y].push_back(x); + + } + + + + + + for(i = 1; i <= n; i++) + + if(viz[i] == 0) dfs1(i); + + + + while(!stk.empty()) + + { + + nod = stk.top(); + + stk.pop(); + + if(viz[nod] == 1) dfs2(nod, ++scc); + + } + + + + out << scc << '\n'; + + + + for(i = 1; i <= scc; i++) + + { + + for(j = 0; j < ans[i].size(); j++) + + out << ans[i][j] << ' '; + + out << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/3faffec4-11a3-4ed3-819e-d9e42d691e17.cpp b/scc/kosaraju/3faffec4-11a3-4ed3-819e-d9e42d691e17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5065302d06e0c8e953fcb1ce90853383a12cff61 --- /dev/null +++ b/scc/kosaraju/3faffec4-11a3-4ed3-819e-d9e42d691e17.cpp @@ -0,0 +1,177 @@ +#include +#include +using namespace std; + + + +const int NMAX = 100000; + + + +struct nod + +{ + + int x; + + nod *next; + +}; + + + +nod *G[NMAX + 1], *GT[NMAX + 1], *CTC[NMAX + 1]; + + + +int N, M, nrc, + + nr, post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +void add(nod *&cap_lst, int nod_ad) + +{ + + nod *p; + + p = new nod; + + p->x = nod_ad; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() + +{ + + int x, y; + + f >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(nod *p = G[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 0) + + DFS(p->x); + + post[++nr] = vf; + +} + +void DFSt(int vf) + +{ + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for(nod *p = GT[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 1) + + DFSt(p->x); + +} + + + +void componente() + +{ + + int i; + + for(i = 1; i <= N; i++) + + if(viz[i] == 0) + + DFS(i); + + for(i = N; i >= 1; i--) + + if(viz[post[i]] == 1) + + { + + nrc++; + + DFSt(post[i]); + + } + +} + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; i++) + + { + + for(nod *p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + +int main() + +{ + + citireGraf(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/40248191-67d7-497d-91d4-3338b9c16c1c.cpp b/scc/kosaraju/40248191-67d7-497d-91d4-3338b9c16c1c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..388bf9c86500da5015e24e2ce2ce11242cf0c9eb --- /dev/null +++ b/scc/kosaraju/40248191-67d7-497d-91d4-3338b9c16c1c.cpp @@ -0,0 +1,162 @@ +#include +#include + + + + +using namespace std; + +ifstream fi("ctc.in"); + +ofstream fo("ctc.out"); + +struct nod{ + + int x; + + nod*urm; + + }; + + nod *L[100003],*LT[100003],*ctc[100003]; + + nod*p; + + int viz[100003],vizt[100003],i,j,k,n,m,x,y; + + int st[100003],nc; + +void adauga(int i,int j){ + + nod*p; + + p=new nod; + + p->x=j; + + + + p->urm=L[i]; + + L[i]=p; + + } + + void adaugat(int i,int j){ + + nod*p; + + p=new nod; + + p->x=i; + + + + p->urm=LT[j]; + + LT[j]=p; + + } + + void adaugactc(int i,int j){ + + nod*p; + + p=new nod; + + p->x=j; + + + + p->urm=ctc[i]; + + ctc[i]=p; + + } + +void dfs(int j){ + + nod *p; + + viz[j]=1; + + p=L[j]; + + while(p){ + + if(viz[p->x]==0)dfs(p->x); + + p=p->urm; + + } + + st[++k]=j; + +} + +void dfst(int j){ + + nod *p; + + adaugactc(nc,j); + + vizt[j]=1; + + p=LT[j]; + + while(p){ + + if(vizt[p->x]==0)dfst(p->x); + + p=p->urm; + + } + +} + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++){ + + fi>>x>>y; + + adauga(x,y); + + adaugat(x,y); + + } + + for(i=1;i<=n;i++) + + if(viz[i]==0)dfs(i); + + + + for(i=k;i>=1;i--) + + if(vizt[st[i]]==0) + + {nc++;dfst(st[i]);} + + fo<x<<" "; + + p=p->urm; + + } + + fo<<'\n'; + + } +return 0; + +} diff --git a/scc/kosaraju/4030d730-9c2f-4eec-aed2-efac152ebc68.cpp b/scc/kosaraju/4030d730-9c2f-4eec-aed2-efac152ebc68.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc11ac3a6580046f0ee39b6d19b98547e250b894 --- /dev/null +++ b/scc/kosaraju/4030d730-9c2f-4eec-aed2-efac152ebc68.cpp @@ -0,0 +1,87 @@ +#include +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +const int N = 100001; +const int M = 200001; +int n, m, nr, x, y, ns; +int vf[2*M], lst[N], urm[2*M]; +int vf_inv[2*M], lst_inv[N], urm_inv[2*M]; +int sorttop[N]; +int sortare[2*N], comp, nrc; +bool viz[N]; + + +void adauga(int x, int y){ + ++nr; + vf[nr] = y; + urm[nr] = lst[x]; + lst[x] = nr; + vf_inv[nr] = x; + urm_inv[nr] = lst_inv[y]; + lst_inv[y] = nr; +} + +void dfs(int x) +{ + viz[x] = true; + for(int p=lst[x]; p!=0; p=urm[p]) + { + y = vf[p]; + if(!viz[y]) + dfs(y); + } + sorttop[++ns] = x; +} + +void dfs2(int x) +{ + sortare[++nrc] = x; + viz[x] = true; + + for(int p=lst_inv[x]; p!=0; p=urm_inv[p]) + { + y = vf_inv[p]; + if(!viz[y]) + dfs2(y); + } +} + +int main() +{ + in>>n>>m; + for(int i=1; i<=m; i++) + { + in>>x>>y; + adauga(x, y); + } + for(int i=1; i<=n; i++) + if(!viz[i]) + dfs(i); + + for(int i=1; i<=n; i++) + viz[i] = false; + + for(int i=ns; i>=1; i--) + { + x = sorttop[i]; + if(!viz[x]) + { + comp++; + dfs2(x); + sortare[++nrc] = -1; + } + } + + out< +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100005],vt[100005],ctc[100005]; + +int timp[100005], sw[100005], nrc[100005], nr, swt[100005], n, m, nrctc, ID[100005]; + + + +void df (int i ) + +{ + + vector :: iterator it; + + sw[i]=1; + + for (it=v[i].begin();it :: iterator it; + + swt[i]=1; + + ctc[nrctc].push_back(i); + + for (it=vt[i].begin();it> n >> m; + + for (i = 1; i <= m; i++) + + { + + f >> x >> y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + + comp(); + + + + vector ::iterator it; + + g << nrctc << '\n'; + + for (i = 1; i <= nrctc; i++) + + { + + for (it = ctc[i].begin(); it < ctc[i].end(); it++) + + g << *it << ' '; + + g << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/412f2a2b-841b-404d-a683-3c2e485c075b.cpp b/scc/kosaraju/412f2a2b-841b-404d-a683-3c2e485c075b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4754d6b714ea6e9e31d82489b9fd32dc98b90ba --- /dev/null +++ b/scc/kosaraju/412f2a2b-841b-404d-a683-3c2e485c075b.cpp @@ -0,0 +1,93 @@ +#include + +const int kNmax = 100005; + +class Task { + public: + void solve() { + read_input(); + print_output(get_result()); + } + + private: + int n; + int m; + std::vector adj[kNmax]; + std::vector adjt[kNmax]; + + void read_input() { + std::ifstream fin("ctc.in"); + fin >> n >> m; + for (int i = 1, x, y; i <= m; i++) { + fin >> x >> y; + adj[x].push_back(y); + adjt[y].push_back(x); + } + fin.close(); + } + + std::vector> get_result() { + std::vector> sol; + std::stack nodeStack; + std::vector visited(n + 1, false); + for (int i = 1; i <= n; i++) { + if (!visited[i]) { + dfsHelper(i, visited, nodeStack); + } + } + + visited = std::vector(n + 1, false); + while(nodeStack.size()) { + int node = nodeStack.top(); + nodeStack.pop(); + std::vector auxVector; + + if (!visited[node]) { + dfsTransposeHelper(node, visited, auxVector); + sol.push_back(auxVector); + } + } + return sol; + } + + + void dfsTransposeHelper(int node, std::vector &visited, std::vector &solutionPart) { + visited[node] = true; + for (int i = 0; i < adj[node].size(); i++) { + if (!visited[adj[node][i]]) { + dfsTransposeHelper(adj[node][i], visited, solutionPart); + } + } + solutionPart.push_back(node); + } + + void dfsHelper(int node, std::vector &visited, std::stack &nodeStack) { + visited[node] = true; + for (int i = 0; i < adjt[node].size(); i++) { + if (!visited[adjt[node][i]]) { + dfsHelper(adjt[node][i], visited, nodeStack); + } + } + nodeStack.push(node); + } + + + void print_output(std::vector> result) { + std::ofstream fout("ctc.out"); + fout << result.size() << '\n'; + for (const auto& ctc : result) { + for (int nod : ctc) { + fout << nod << ' '; + } + fout << '\n'; + } + fout.close(); + } +}; + +int main() { + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/scc/kosaraju/431f21cb-0ff3-468a-9790-9b7e7356236f.cpp b/scc/kosaraju/431f21cb-0ff3-468a-9790-9b7e7356236f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3589748bd1a6fa46bec07d17d1119cfcab060324 --- /dev/null +++ b/scc/kosaraju/431f21cb-0ff3-468a-9790-9b7e7356236f.cpp @@ -0,0 +1,177 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int n, m, ctc; + +stack s; + +vector> graph, trans, ans; + +bool fr[100100]; + + + +void DFS(int node) + +{ + + fr[node] = true; + + + + for(int i : graph[node]) + + if(fr[i] == false) + + DFS(i); + + + + s.push(node); + +} + + + +void transDFS(int node) + +{ + + fr[node] = false; + + ans[ctc].push_back(node); + + + + cout << ctc << ' ' << node << '\n'; + + + + for(int i : trans[node]) + + if(fr[i] == true) + + transDFS(i); + +} + + + +void kosaraju() + +{ + + for(int i = 1; i <= n; i++) + + if(fr[i] == false) + + DFS(i); + + + + + + ans.resize(n + 5); + + + + while(!s.empty()) + + { + + + + + + if(fr[s.top()] == true) + + { + + ctc++; + + transDFS(s.top()); + + } + + + + s.pop(); + + } + +} + + + +int main() + +{ + + in >> n >> m; + + + + graph.resize(n + 5); + + trans.resize(n + 5); + + + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + in >> x >> y; + + + + graph[x].push_back(y); + + trans[y].push_back(x); + + } + + + + kosaraju(); + + + + out << ctc << '\n'; + + + + for(int i = 1; i <= ctc; i++) + + { + + for(int j : ans[i]) + + out << j << ' '; + + + + out << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/435e2c80-4073-4c7a-84f8-8b10cb514262.cpp b/scc/kosaraju/435e2c80-4073-4c7a-84f8-8b10cb514262.cpp new file mode 100644 index 0000000000000000000000000000000000000000..409b0613b4fc78c699b93efae8f5691b9a9d3e6b --- /dev/null +++ b/scc/kosaraju/435e2c80-4073-4c7a-84f8-8b10cb514262.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +stack < int > S; + +vector G[100005],GT[100005],CTC[100005]; + +int N, M, NrCTC; + +int beenThere[100005]; + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for(unsigned int i=0; i +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + + + +int n, m, nrctc; + +stack < int > S; + +int viz[nmax]; + +vector < int > G[nmax], GT[nmax], CTC[nmax]; + + + +void read() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + + + } + +} + + + +void DFS1(int nod) + +{ + + viz[nod] = 1; + + for(unsigned int i = 0; i < G[nod].size(); ++i) + + { + + int vecin = G[nod][i]; + + if(!viz[vecin]) + + DFS1(vecin); + + } + + S.push(nod); + +} + + + +void DFS2(int nod) + +{ + + viz[nod] = 2; + + CTC[nrctc].push_back(nod); + + for(unsigned int i = 0; i < GT[nod].size(); ++i) + + { + + int vecin = GT[nod][i]; + + if(viz[vecin] == 1) + + DFS2(vecin); + + } + +} + + + +void solve() + +{ + + for(int i = 1; i <= n; ++i) + + if(!viz[i]) + + DFS1(i); + + while(!S.empty()) + + { + + int nod = S.top(); + + if(viz[nod] == 1) + + { + + nrctc++; + + DFS2(nod); + + } + + S.pop(); + + } + +} + + + +void afisare() + +{ + + fout << nrctc << "\n"; + + for(int i = 1; i <= nrctc; ++i) + + { + + for(unsigned int j = 0; j < CTC[i].size(); ++j) + + fout << CTC[i][j] << " "; + + fout << "\n"; + + } + +} + +int main() + +{ + + read(); + + solve(); + + afisare(); + + return 0; + +} diff --git a/scc/kosaraju/4463ae4d-3886-4fa3-9c19-092c292adabb.cpp b/scc/kosaraju/4463ae4d-3886-4fa3-9c19-092c292adabb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..979383fcf8cce457221a5fbf8196f65ec77aca40 --- /dev/null +++ b/scc/kosaraju/4463ae4d-3886-4fa3-9c19-092c292adabb.cpp @@ -0,0 +1,252 @@ +#include + + +const int NMAX = 100000; + + + +using namespace std; + + + +ifstream in("ctc.in"); + + + +ofstream out("ctc.out"); + + + +struct nod + +{ + + + + int x; + + + + nod* next; + + + +}; + + + + + + + +nod* G[NMAX + 1]; + +nod* GT[NMAX + 1]; + +nod* CTC[NMAX + 1]; + + + +int N, M, nrc, nr, post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +void add(nod*& cap_lst, int nod_add) + +{ + + + + nod* p; + + + + p = new nod; + + + + p->x = nod_add; + + + + p->next = cap_lst; + + + + cap_lst = p; + + + +} + + + +void citire() + +{ + + in >> N >> M; + + + + for(int i = 1 ; i <= M ; i ++) + + { + + int x, y; + + + + in >> x >> y; + + + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + + + for(nod* p = G[vf] ; p != NULL ; p = p -> next) + + if(!viz[p->x]) + + DFS(p->x); + + + + post[++nr] = vf; + +} + + + + + +void DFSt(int vf) + +{ + + + + viz[vf] = 0; + + + + add(CTC[nrc], vf); + + + + for (nod* p = GT[vf]; p != NULL; p = p->next) + + + + if (viz[p->x]) + + + + DFSt(p->x); + + + +} + + + +void rez() + +{ + + for(int i = 1 ; i <= N ; i++) + + if(!viz[i]) + + DFS(i); + + + + for(int i = N ; i >= 1 ; i--) + + if(viz[post[i]]) + + { + + nrc++; + + DFSt(post[i]); + + } + +} + + + + + +void afis() { + + + + out << nrc << '\n'; + + + + for (int i = 1; i <= nrc; i++) { + + + + for (nod* p = CTC[i]; p != NULL; p = p->next) + + + + out << p->x << ' '; + + + + out << '\n'; + + + + } + + + +} + + + +int main() + +{ + + citire(); + + + + rez(); + + + + afis(); + + + + return 0; + +} diff --git a/scc/kosaraju/44c56301-a9e0-439a-b025-d4732b7f80d7.cpp b/scc/kosaraju/44c56301-a9e0-439a-b025-d4732b7f80d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1b4a0e38ae881cfa63b4ff19f06de96c9a6fcec --- /dev/null +++ b/scc/kosaraju/44c56301-a9e0-439a-b025-d4732b7f80d7.cpp @@ -0,0 +1,86 @@ +#include + + + +std::ifstream f("ctc.in"); +std::ofstream g("ctc.out"); + + +std::stack firstDfsSt; +std::vector G[100005]; +std::vector T[100005]; +std::vector comps[100005]; +std::vector firstDfsViz(100005), inComp(100005); + +int n, m, cnt = 0; + + +void readData(){ + f >> n >> m; + int x, y; + for(int i = 0; i> x >> y; + G[x].push_back(y); + T[y].push_back(x); + } +} + +void dfsFirst(int start){ + firstDfsViz[start] = true; + for(auto& adj : G[start]){ + if(!firstDfsViz[adj]){ + dfsFirst(adj); + } + } + firstDfsSt.push(start); +} + + + +void dfsSecond(int start){ + inComp[start] = true; + for(auto& adj : T[start]){ + if(!inComp[adj]){ + dfsSecond(adj); + } + } + + comps[cnt].push_back(start); +} + + +void solve(){ + for(int i = 1; i<=n; i++){ + if(!firstDfsViz[i]){ + dfsFirst(i); + } + } + + while(!firstDfsSt.empty()){ + int node = firstDfsSt.top(); + if(!inComp[node]){ + dfsSecond(node); + cnt ++; + }else{ + firstDfsSt.pop(); + } + } +} + + +void printSolution(){ + g << cnt << '\n'; + for(int i = 0; i +#include + + +using namespace std; + + + +const int NMAX = 100000; + + + +vector graf[1 + NMAX]; + +vector grafInv[1 + NMAX]; + + + +bool vizitat[1 + NMAX]; + + + +vector lista; + + + +int nrComponente = 0; + +vector componente[1 + NMAX]; + + + +void dfs(int nod) + +{ + + vizitat[nod] = true; + + + + for (int i = 0; i < graf[nod].size(); i++) + + { + + if (!vizitat[graf[nod][i]]) + + { + + dfs(graf[nod][i]); + + } + + } + + + + lista.push_back(nod); + +} + + + +void dfsInv(int nod) + +{ + + vizitat[nod] = true; + + + + for (int i = 0; i < grafInv[nod].size(); i++) + + { + + if (!vizitat[grafInv[nod][i]]) + + { + + dfsInv(grafInv[nod][i]); + + } + + } + + + + componente[nrComponente].push_back(nod); + +} + + + +int main() + +{ + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + int n, m; + + + + in >> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int x, y; + + + + in >> x >> y; + + graf[x].push_back(y); + + grafInv[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + { + + if (!vizitat[i]) + + { + + dfs(i); + + } + + } + + + + for (int i = 1; i <= n; i++) + + { + + vizitat[i] = false; + + } + + + + for (int i = lista.size() - 1; i >= 0; i--) + + { + + if(!vizitat[lista[i]]) + + { + + nrComponente++; + + dfsInv(lista[i]); + + } + + } + + + + out << nrComponente << '\n'; + + for (int i = 1; i <= nrComponente; i++) + + { + + for (int j = 0; j < componente[i].size(); j++) + + { + + out << componente[i][j] << ' '; + + } + + out << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/46f9150d-dbc0-4bf0-8f72-973546322515.cpp b/scc/kosaraju/46f9150d-dbc0-4bf0-8f72-973546322515.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ad484d44c20532e305d139c6f526e32ad5292c5 --- /dev/null +++ b/scc/kosaraju/46f9150d-dbc0-4bf0-8f72-973546322515.cpp @@ -0,0 +1,166 @@ +#include + + +const int NMAX = 100000; + + + +struct nod { + + int x; + + nod* next; + +}; + + + +nod* G[NMAX + 1], + + * GT[NMAX + 1], + + * CTC[NMAX + 1]; + + + +int N, M, nrc, nr, post[NMAX + 1]; + +bool viz[NMAX + 1]; + + + +std::ifstream f("ctc.in"); + +std::ofstream g("ctc.out"); + + + +void add(nod*& cap_lst, int nod_add) { + + nod* p; + + p = new nod; + + p->x = nod_add; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() { + + f >> N >> M; + + while (M--) { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf){ + + viz[vf] = 1; + + for (nod* p = G[vf]; p != NULL; p = p->next) + + if (!viz[p->x]) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) { + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for (nod* p = GT[vf]; p != NULL; p = p->next) + + if (viz[p->x]) + + DFSt(p->x); + +} + + + + + +void componentectc() + +{ + + int i; + + for (int i = 1; i <= N; i++) + + if (viz[i] == 0) + + DFS(i); + + + + for(i=N;i>=1;i--) + + if (viz[post[i]] == 1) { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() { + + g << nrc << '\n'; + + for (int i = 1; i <= nrc; i++) { + + for (nod* p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citireGraf(); + + componentectc(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/47bea33e-fff4-4720-9ca2-55f780507886.cpp b/scc/kosaraju/47bea33e-fff4-4720-9ca2-55f780507886.cpp new file mode 100644 index 0000000000000000000000000000000000000000..59f108740370a3d666f249191eaf9b6dbb8c64c7 --- /dev/null +++ b/scc/kosaraju/47bea33e-fff4-4720-9ca2-55f780507886.cpp @@ -0,0 +1,132 @@ +#include +using namespace std; + + + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + int n, m; + + vector adj[100009]; + + + vector adjT[100009]; + + + + + + vector topsort; + + vector> scc; + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + + for (int i = 1; i <= m; ++i) { + int x, y; + + fin >> x >> y; + + adj[x].push_back(y); + + + adjT[y].push_back(x); + } + + fin.close(); + } + + void print_output() { + ofstream fout("ctc.out"); + + fout << scc.size() << "\n"; + + for (auto &vect : scc) { + for (auto &it : vect) { + fout << it << " "; + } + fout << "\n"; + } + + fout.close(); + } + + void get_result() { + kosaraju(); + } + + void kosaraju() { + vector visited(n + 1, 0); + + topsort.push_back(-1); + + + for (int i = 1; i <= n; ++i) { + if (!visited[i]) { + dfs(i, visited, topsort); + } + } + + for (int i = n; i >= 1; --i) { + + + + + + if (visited[topsort[i]]) { + vector component; + + dfsT(topsort[i], visited, component); + + scc.push_back(component); + } + } + } + + void dfs(int& node, vector& visited, vector &topsort) { + visited[node] = 1; + + for (auto& it : adj[node]) { + if (!visited[it]) { + dfs(it, visited, topsort); + } + } + + + topsort.push_back(node); + } + + void dfsT(int& node, vector& visited, vector &component) { + visited[node] = 0; + component.push_back(node); + + for (auto& it : adjT[node]) { + if (visited[it]) { + dfsT(it, visited, component); + } + } + } +}; + +int main() { + + + + + + + Task *task = new Task(); + task->solve(); + delete task; + + return 0; +} diff --git a/scc/kosaraju/48bd2771-7fa6-4b48-ab8a-dba00b993c3c.cpp b/scc/kosaraju/48bd2771-7fa6-4b48-ab8a-dba00b993c3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ce3e394f548b7fe5acb7df41024084ebe193225 --- /dev/null +++ b/scc/kosaraju/48bd2771-7fa6-4b48-ab8a-dba00b993c3c.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +vector < int > g[100005], gt[100005], sol[100005]; + +stack < int > st; + +int c[100005], n, m, x, y, nrctc; + + + + + +void dfs1(int k){ + + c[k] = 1; + + for(int i = 0; i < g[k].size(); i++){ + + int vec = g[k][i]; + + if(!c[vec]) + + dfs1(vec); + + } + + st.push(k); + +} + + + + + +void dfs2(int k){ + + c[k] = 2; + + sol[nrctc].push_back(k); + + for(int i = 0; i < gt[k].size(); i++){ + + int vec = gt[k][i]; + + if(c[vec] == 1) + + dfs2(vec); + + } + +} + + + +void Kosaraju(){ + + for(int i = 1; i <= n; i++) + + if(!c[i]) + + dfs1(i); + + + + while(!st.empty()){ + + int nod = st.top(); + + if(c[nod] == 1){ + + nrctc++; + + dfs2(nod); + + } + + st.pop(); + + } + + + +} + + + +int main(){ + + in>>n>>m; + + for(int i = 1; i <= m; i++){ + + in>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + Kosaraju(); + + + + out< +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + + + + + +vector conexe[100009]; + +vector m[100009]; + +vector t[100009]; + + + +int nr; + +int viz[100009]; + +stacks; + + + +void dfs(int n) + +{ + + viz[n] = 1; + + for(int i=0; i>nodes>>edges; + + for(int i = 1; i <= edges; i++) + + { + + f>>x>>y; + + + + t[y].push_back(x); + + + + m[x].push_back(y); + + } + + + + + + + + for(int i = 1; i <= nodes; i++) + + { + + if(viz[i]==0) + + dfs(i); + + } + + + + + + + + while(s.empty()==0) + + { + + int n=s.top(); + + + + if(viz[n]==1) + + { + + nr++; + + dfs_transpus(n); + + } + + s.pop(); + + } + + + + g< + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +int sel[100005],nrgrupe,nr,of[100005],n,m,i,x,y,j; + +vector v[100005],v2[100005],inv[100005]; + +void sorttop(int x) + +{ + + sel[x]=1; + + for (int i=0;i>n>>m; + + for (i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + inv[y].push_back(x); + + } + + for (i=1;i<=n;i++) + + { + + if (sel[i]==0) + + { + + sorttop(i); + + } + + } + + memset(sel,0,sizeof(sel)); + + reverse(of+1,of+nr+1); + + for (i=1;i<=n;i++) + + { + + if (sel[of[i]]==0) + + { + + nrgrupe++; + + dfs(of[i]); + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m; + +int comp[100001]; + +bool vis[100001]; + + + +list < int > L; + + + +vector < int > inNeighbour[100001]; + +vector < int > outNeighbour[100001]; + + + +int nrComp; + +vector < vector < int > > components; + + + +void visit(int node) + +{ + + if (vis[node]) + + return; + + + + vis[node] = true; + + + + for (auto it : outNeighbour[node]) + + if (!vis[it]) + + visit(it); + + + + L.push_back(node); + +} + + + +void assignComponent(int node, int c) + +{ + + if (comp[node]) + + return; + + + + comp[node] = c; + + + + components.back().push_back(node); + + + + for (auto it : inNeighbour[node]) + + if (!comp[it]) + + assignComponent(it, c); + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1; i<=m; i++) + + { + + int x, y; f >> x >> y; + + outNeighbour[x].push_back(y); + + inNeighbour[y].push_back(x); + + } + + + + for (int i=1; i<=n; i++) + + if (!vis[i]) + + visit(i); + + + + for (list < int > :: iterator it = L.end(); it != L.begin();) + + { + + it--; + + if (!comp[*it]) + + { + + nrComp++; + + components.resize(nrComp); + + assignComponent(*it, nrComp); + + } + + } + + + + g << nrComp << "\n"; + + for (int i=0; i + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v1[100001], v2[100001], v3[100001]; + + + +int st[100001]; + +int n,m,x,y,k,nr; + + + +bool ap[100001], ap2[100001]; + + + +void DFS(int node){ + + ap[node]=1; + + for(int i=0; i0; --i){ + + if(!ap2[st[i]]){ + + ++nr; + + DFS2(st[i]); + + } + + } + +} + + + +int main(){ + + f>>n>>m; + + + + for(int i=1; i<=m; ++i){ + + f>>x>>y; + + v1[x].push_back(y); + + v2[y].push_back(x); + + } + + + + ctc(n); + + g< + + +using namespace std; + +ifstream f ("ctc.in") ; + +ofstream g ("ctc.out") ; + +int N , M , x , y , cnt = 0; + +stack st; + +bool viz[100010] ; + +vector G[100010] , GT[100010] , CTC[100010]; + +void DFS (int nod) + +{ + + viz[nod] = true ; + + int lg = G[nod].size() ; + + for (int i = 0 ; i < lg; ++i) + + { + + int vec = G[nod][i] ; + + if (!viz[vec]) DFS(vec); + + } + + + + st.push(nod) ; + +} + +void DFST(int nod) + +{ + + viz[nod] = false; + + int lg = GT[nod].size() ; + + for (int i = 0 ; i < lg ; ++i) + + { + + int vec = GT[nod][i]; + + if (viz[vec])DFST(vec) ; + + } + + + + CTC[cnt].push_back(nod) ; + +} + +int main() + +{ + + f >> N >> M ; + + for (int i = 1 ; i <= M ; ++i) + + { + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for (int nod = 1; nod <= N ; ++nod) + + if (!viz[nod]) DFS(nod) ; + + + + while (!st.empty()) + + { + + int nod = st.top(); + + st.pop() ; + + if (viz[nod]) {cnt ++ ; DFST(nod) ;} + + } + + + + g << cnt << '\n' ; + + for (int i = 1 ; i <= cnt ; ++i) + + { + + int lg = CTC[i].size(); + + for (int j = 0 ; j < lg ; ++j) g << CTC[i][j] << ' ' ; + + g << '\n' ; + + } + + f.close(); + + g.close(); + + return 0 ; + +} diff --git a/scc/kosaraju/4a40d2b2-ca3a-4371-86d9-951a0144f8d7.cpp b/scc/kosaraju/4a40d2b2-ca3a-4371-86d9-951a0144f8d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a41aaca191e6841fac17b4f7b97866fa1916a4c --- /dev/null +++ b/scc/kosaraju/4a40d2b2-ca3a-4371-86d9-951a0144f8d7.cpp @@ -0,0 +1,147 @@ +#include +using namespace std; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m; + + + vector adj[100010]; + + + vector adj_trans[100010]; + + + vector> ctc; + + + + + + + + vector visited; + + + + vector topsort; + + void read_input() { + cin >> n >> m; + visited = vector(n + 1, 0); + + for (int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + } + + void get_result() { + kosaraju(); + } + + void kosaraju() { + topsort.push_back(-1); + + + for (int i = 1; i <= n; ++i) { + if (!visited[i]) { + dfs(i); + } + } + + visited = std::vector(n + 1, 0); + + for (int i = n; i >= 1; --i) { + if (!visited[topsort[i]]) { + + vector current_ctc; + + dfs_t(topsort[i], current_ctc); + + + ctc.push_back(current_ctc); + } + } + } + + + void dfs(int node) { + visited[node] = 1; + + for (auto &vecin : adj[node]) { + if (!visited[vecin]) { + dfs(vecin); + } + } + + + topsort.push_back(node); + } + + + void dfs_t(int node, vector ¤t_ctc) { + + visited[node] = 1; + current_ctc.push_back(node); + + for (auto &vecin : adj_trans[node]) { + if (!visited[vecin]) { + dfs_t(vecin, current_ctc); + } + } + } + + void print_output() { + + int sol = ctc.size(); + cout << sol << "\n"; + + for (int i = 0; i < sol; ++i) { + + for (auto &node : ctc[i]) { + cout << node << " "; + } + cout << '\n'; + } + } +}; + +int main() { + + auto cin_buff = cin.rdbuf(); + auto cout_buff = cout.rdbuf(); + + + ifstream fin("ctc.in"); + cin.rdbuf(fin.rdbuf()); + + + ofstream fout("ctc.out"); + cout.rdbuf(fout.rdbuf()); + + + Task *task = new Task(); + task->solve(); + delete task; + + + cin.rdbuf(cin_buff); + cout.rdbuf(cout_buff); + + + + + return 0; +} diff --git a/scc/kosaraju/4a86911b-027e-4fa5-9873-c475eff7c523.cpp b/scc/kosaraju/4a86911b-027e-4fa5-9873-c475eff7c523.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d21311e9c78217f4e36629b9451c6309590cd2 --- /dev/null +++ b/scc/kosaraju/4a86911b-027e-4fa5-9873-c475eff7c523.cpp @@ -0,0 +1,186 @@ +#include + + + + +using namespace std; + + + +ofstream fout("ctc.out"); + + + +struct nod + +{ + + int info; + + nod *urm; + +}; + + + +nod *g[100001], *gt[100001], *comp[100001]; + +int n, m; + +int viz[100001], stiva[100001], nrc, nr; + + + +void Citire() + +{ + + ifstream fin("ctc.in"); + + int x,y; + + nod *p; + + fin>>n>>m; + + while (m--) + + { + + fin>>x>>y; + + p=new nod; p->info=y; p->urm=g[x]; g[x]=p; + + p=new nod; p->info=x; p->urm=gt[y]; gt[y]=p; + + } + + fin.close(); + +} + + + + + +void dfs(int vf) + +{ + + int i; + + nod *p; + + viz[vf]=1; + + for (p=g[vf];p!=NULL;p=p->urm) + + { + + i=p->info; + + if (!viz[i]) + + dfs(i); + + } + + stiva[++nr]=vf; + +} + + + +void dfst(int vf) + +{ + + nod *p; + + int i; + + viz[vf]=0; + + p=new nod; p->info=vf; p->urm=comp[nrc]; comp[nrc]=p; + + for (p=gt[vf];p!=NULL;p=p->urm) + + { + + i=p->info; + + if (viz[i]) + + dfst(i); + + } + +} + + + +void Afisare() + +{ + + int i; + + nod *p; + + for (i=1;i<=nrc;i++) + + { + + for (p=comp[i];p!=NULL;p=p->urm) + + fout<info<<" "; + + fout<<"\n"; + + } + +} + +int main() + +{ + + + + int i; + + Citire(); + + + + for (i=1;i<=n;i++) + + if (!viz[i]) + + dfs(i); + + + + for (i=n;i>0;i--) + + if (viz[stiva[i]]) + + { + + nrc++; + + dfst(stiva[i]); + + } + + fout< +#include + + +using namespace std; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + +vector G[100002]; + +vector GT[100002]; + +int n,nr,v[100002]; + +vectortc[100002]; + +bool uz[100002]; + +void citire(); + +void postordine( int nod); + +void dfs (int nod); + +void afis(); + + + +int main() + +{ + + int i; + + citire(); + + for(i=1;i<=n;i++) + + if(!uz[i]) + + { + + uz[i]=1; + + postordine(i); + + } + + nr=0; + + for(i=n;i>0;i--) + + if(uz[v[i]]) + + { + + nr++; + + dfs(v[i]); + + } + + afis(); + + return 0; + +} + + + +void citire() + +{ + + int i,x,y,m; + + fin>>n>>m; + + for (i=1; i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void postordine( int nod) + +{ + + for(int i=0;i +#include + + + + +using namespace std; + +const int MAX_N = 1e5 + 1; + +vector graph[MAX_N], graph_t[MAX_N]; + +bool visited[MAX_N]; +vector exit_traversal_index; + +void dfs1(int node) { + visited[node] = true; + for (int neighbor : graph[node]) { + if (!visited[neighbor]) { + dfs1(neighbor); + } + } + + exit_traversal_index.push_back(node); +} + +vector component[MAX_N]; +int comp_index[MAX_N]; + +void dfs2(int node, int component_index) { + comp_index[node] = component_index; + component[component_index].push_back(node); + + for (int neighbor : graph_t[node]) { + if (!comp_index[neighbor]) { + dfs2(neighbor, component_index); + } + } +} + +int main() { + freopen("ctc.in","r", stdin); + freopen("ctc.out", "w", stdout); + + int n, m; + scanf("%d%d", &n, &m); + + for (int i = 0; i < m; i++) { + int x, y; + scanf("%d%d", &x, &y); + graph[x].push_back(y); + graph_t[y].push_back(x); + } + + for (int node = 1; node <= n; node++) { + if (!visited[node]) { + dfs1(node); + } + } + + int no_of_components = 0; + for (int i = n - 1; i >= 0; i--) { + if (!comp_index[exit_traversal_index[i]]) { + dfs2(exit_traversal_index[i], ++no_of_components); + } + } + + printf("%d\n", no_of_components); + for (int i = 1; i <= no_of_components; i++) { + for (int node : component[i]) { + printf("%d ", node); + } + printf("\n"); + } + + return 0; +} diff --git a/scc/kosaraju/4b5fa856-3d87-40df-90b8-007770c85fb6.cpp b/scc/kosaraju/4b5fa856-3d87-40df-90b8-007770c85fb6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92e34a582f4b15f3b02390581fbfe8ea6f2e34b6 --- /dev/null +++ b/scc/kosaraju/4b5fa856-3d87-40df-90b8-007770c85fb6.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include + + + +using namespace std; +class Task { + + std::vector matrix[100005]; + std::vector adj_matrix[100005]; + std::vector visited; + + + int N, M, S; + + + void read_input() { + + std::ifstream in("ctc.in"); + + in >> N; + in >> M; + + for (int i = 0; i < M; i++) { + + int x; + + int y; + + in >> x; + + in >> y; + + matrix[x].push_back(y); + adj_matrix[y].push_back(x); + } + + in.close(); + + } + + + void dfs_util(int source, std::vector &time_finish) { + visited[source] = true; + for (auto &x : matrix[source]) { + if (visited[x] == false) { + dfs_util(x, time_finish); + } + } + + time_finish.push_back(source); + + } + + void dfs_adjacent(int source, std::vector &solution) { + solution.push_back(source); + visited[source] = true; + for (auto &x: adj_matrix[source]) { + if (visited[x] == false) { + dfs_adjacent(x, solution); + } + } + } + + + std::vector> ctc_kosaraju() { + + std::vector time_finish; + time_finish.push_back(0); + std::vector> connex_comp; + visited = std::vector(N + 1, false); + + for (int i = 1; i <= N; i++) { + if (visited[i] == false) { + dfs_util(i, time_finish); + } + } + + visited.clear(); + visited = std::vector(N + 1, false); + + + for (int i = N; i > 0; i--) { + if (visited[time_finish[i]] == false) { + std::vector new_solution; + dfs_adjacent(time_finish[i], new_solution); + connex_comp.push_back(new_solution); + } + } + + + return connex_comp; + } + + + void print(std::vector> connex_components) { + + std::ofstream out("ctc.out"); + + out<solve(); + + delete(task); + + + + return 0; + +} diff --git a/scc/kosaraju/4bd49b18-d55c-4065-8177-ddd4464a56a4.cpp b/scc/kosaraju/4bd49b18-d55c-4065-8177-ddd4464a56a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdb61ae56ec8a12cf09cc98317b01a8c67a588e2 --- /dev/null +++ b/scc/kosaraju/4bd49b18-d55c-4065-8177-ddd4464a56a4.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int nlim=100005; + +int n,m,nr=0; + +int beenThere[nlim]; + +vector muchii[nlim],muchii_T[nlim],afis[nlim]; + +stack S; + + + +void DFS(int nod) + +{ + + beenThere[nod]=1; + + for(int i=0;i>n>>m; + + int x,y; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + muchii[x].push_back(y); + + muchii_T[y].push_back(x); + + } + +} + + + +int main() + +{ + + citire(); + + for(int i=1;i<=n;i++) + + if(beenThere[i]==0) + + DFS(i); + + while(!S.empty()) + + { + + int nod=S.top(); + + S.pop(); + + if(beenThere[nod]==1) + + { + + nr++; + + DFS_T(nod); + + } + + } + + g< +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,nr; + +vector v1[100001],v2[100001],sol[100001]; + +stack st; + +bool viz[100001]; + +void dfs1(int nod) + +{ + + viz[nod]=1; + + int i; + + for(i=0;i>n>>m; + + int i,x,y; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y; + + v1[x].push_back(y); + + v2[y].push_back(x); + + } + + for(i=1;i<=n;i++) + + { + + if(!viz[i])dfs1(i); + + } + + memset(viz,0,sizeof(viz)); + + for(i=1;i<=n;i++) + + { + + if(!viz[st.top()]) + + { + + nr++; + + dfs2(st.top()); + + } + + st.pop(); + + } + + fout< +#include +#include + +std:: ifstream in("ctc.in"); +std:: ofstream out("ctc.out"); + +int n, m, g_count; +bool marked[100001],marked_scc[100001]; +std:: vector g[100001],g_rev[100001],ordered,scc[1000001]; + +void dfs(int node){ + if(!marked[node]) { + marked[node] = true; + for(int i = 0; i < g[node].size(); i++) + dfs(g[node][i]); + ordered.push_back(node); + } +} + +void dfs_rev(int node, int counter) { + if(!marked_scc[node]) + { + marked_scc[node] = counter; + scc[counter].push_back(node); + for(int i = 0; i < g_rev[node].size(); i++) + dfs_rev(g_rev[node][i], counter); + } +} + +int main(){ + in >> n >> m; + for( int i = 0; i < m; i++){ + int a,b; + in >> a >> b; + g[a].push_back(b); + g_rev[b].push_back(a); + } + for(int i = 1; i <= n; i++) + if(!marked[i]) + dfs(i); + for(int i = n - 1; i >= 0; i--) + if(!marked_scc[ordered[i]]) + dfs_rev(ordered[i], ++g_count); + out << g_count << '\n'; + for(int i = 1; i <= g_count; i++) { + for (int j = 0; j < scc[i].size(); j++) + out << scc[i][j] << " "; + out << '\n'; + } + + return 0; +} diff --git a/scc/kosaraju/50ec48a1-c9e6-49cb-b690-58834d1c01ac.cpp b/scc/kosaraju/50ec48a1-c9e6-49cb-b690-58834d1c01ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37d28f510fc608aa3381b5b1fdd0f07a48d58568 --- /dev/null +++ b/scc/kosaraju/50ec48a1-c9e6-49cb-b690-58834d1c01ac.cpp @@ -0,0 +1,122 @@ +#include + + + + + + +using namespace std; + + + +FILE *fin = fopen("ctc.in" , "r"); + +FILE *fout = fopen("ctc.out" , "w"); + + + +vector muchii1[100005] , muchii2[100005] , H; + +int n , m; + +bool viz[100005]; + + + +vector > ans; + + + +void DFS1(int nod) + +{ + + viz[nod] = 1; + + for(int i = 0 ; i < muchii1[nod].size() ; i ++) + + if(!viz[muchii1[nod][i]]) + + DFS1(muchii1[nod][i]); + + H.push_back(nod); + +} + + + +void DFS2(int nod) + +{ + + viz[nod] = 1; + + for(int i = 0 ; i < muchii2[nod].size() ; i ++) + + if(!viz[muchii2[nod][i]]) + + DFS2(muchii2[nod][i]); + + ans.back().push_back(nod); + +} + + + +int main() + +{ + + fscanf(fin , "%d%d" , &n , &m); + + for(int i = 1 ; i <= m ; i ++) + + { + + int x , y; + + fscanf(fin , "%d%d" , &x , &y); + + muchii1[x].push_back(y); + + muchii2[y].push_back(x); + + } + + for(int i = 1 ; i <= n ; i ++) + + if(!viz[i]) + + DFS1(i); + + memset(viz , 0 , sizeof(viz)); + + for(int i = H.size() - 1 ; i >= 0 ; i --) + + if(!viz[H[i]]) + + { + + ans.push_back(vector ()); + + DFS2(H[i]); + + } + + fprintf(fout , "%d\n" , ans.size()); + + for(int i = 0 ; i < ans.size() ; i ++) + + { + + for(int j = 0 ; j < ans[i].size() ; j ++) + + fprintf(fout , "%d " , ans[i][j]); + + fprintf(fout , "\n"); + + } + + return 0; + +} diff --git a/scc/kosaraju/50f80345-aa74-4079-a298-5881c9f43126.cpp b/scc/kosaraju/50f80345-aa74-4079-a298-5881c9f43126.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd21f1e1be6dd0c4c84d09299c49037f53812864 --- /dev/null +++ b/scc/kosaraju/50f80345-aa74-4079-a298-5881c9f43126.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graf1[100001], graf2[100001], componente[100001]; + +int vizitat[100001], nr, n, m; + +stack s; + + + +void citire() { + + int x, y; + + f >> n >> m; + + for(int i = 1; i <= m; i++) { + + f >> x >> y; + + graf1[x].push_back(y); + + graf2[y].push_back(x); + + } + +} + + + +int DFS1(int nod) { + + vizitat[nod] = 1; + + for(int i=0; i < graf1[nod].size(); i++) + + if(vizitat[graf1[nod][i]] == 0) + + DFS1(graf1[nod][i]); + + s.push(nod); + +} + + + +int DFS2(int nod) { + + vizitat[nod] = 1; + + componente[nr].push_back(nod); + + for(int i=0; i < graf2[nod].size(); i++) + + if(vizitat[graf2[nod][i]] == 0) + + DFS2(graf2[nod][i]); + +} + + + +void afisare() { + + g << nr << "\n"; + + for(int i = 1; i <= nr; i++) { + + for(int j = 0; j < componente[i].size(); j++) + + g << componente[i][j] <<" "; + + g << "\n"; + + } + +} + + + +int main() { + + citire(); + + for(int i = 1; i <= n; i++) + + if(vizitat[i] == 0) + + DFS1(i); + + memset(vizitat, 0, sizeof(vizitat)); + + while(s.empty() == 0) { + + if(vizitat[s.top()] == 0) { + + nr++; + + DFS2(s.top()); + + } + + s.pop(); + + } + + afisare(); + + return 0; + +} diff --git a/scc/kosaraju/513b242e-5ce0-496b-942d-dcec794cb616.cpp b/scc/kosaraju/513b242e-5ce0-496b-942d-dcec794cb616.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ad484d44c20532e305d139c6f526e32ad5292c5 --- /dev/null +++ b/scc/kosaraju/513b242e-5ce0-496b-942d-dcec794cb616.cpp @@ -0,0 +1,166 @@ +#include + + +const int NMAX = 100000; + + + +struct nod { + + int x; + + nod* next; + +}; + + + +nod* G[NMAX + 1], + + * GT[NMAX + 1], + + * CTC[NMAX + 1]; + + + +int N, M, nrc, nr, post[NMAX + 1]; + +bool viz[NMAX + 1]; + + + +std::ifstream f("ctc.in"); + +std::ofstream g("ctc.out"); + + + +void add(nod*& cap_lst, int nod_add) { + + nod* p; + + p = new nod; + + p->x = nod_add; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() { + + f >> N >> M; + + while (M--) { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf){ + + viz[vf] = 1; + + for (nod* p = G[vf]; p != NULL; p = p->next) + + if (!viz[p->x]) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) { + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for (nod* p = GT[vf]; p != NULL; p = p->next) + + if (viz[p->x]) + + DFSt(p->x); + +} + + + + + +void componentectc() + +{ + + int i; + + for (int i = 1; i <= N; i++) + + if (viz[i] == 0) + + DFS(i); + + + + for(i=N;i>=1;i--) + + if (viz[post[i]] == 1) { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() { + + g << nrc << '\n'; + + for (int i = 1; i <= nrc; i++) { + + for (nod* p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citireGraf(); + + componentectc(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/51d1c94e-1a1b-47ca-a5c4-7686e157d8a7.cpp b/scc/kosaraju/51d1c94e-1a1b-47ca-a5c4-7686e157d8a7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74de20b33225695948bee1ba2e156e31c0c9a2b0 --- /dev/null +++ b/scc/kosaraju/51d1c94e-1a1b-47ca-a5c4-7686e157d8a7.cpp @@ -0,0 +1,143 @@ +#include +#include + + + + +using std::vector; + + + +std::ifstream fin("ctc.in"); + +std::ofstream fout("ctc.out"); + + + +int n, m; + +bool vis[100010]; + + + +vector ad1[100010]; + +vector ad2[100010]; + + + +int nrComp; + +vector comp[100010]; + + + +int top; + +int st[100010]; + + + +void dfs1(int node) { + + vis[node] = true; + + for (int i = 0; i < (int) ad1[node].size(); i++) + + if (!vis[ad1[node][i]]) + + dfs1(ad1[node][i]); + + st[++top] = node; + +} + + + +void dfs2(int node) { + + vis[node] = true; + + comp[nrComp].push_back(node); + + + + for (int i = 0; i < (int) ad2[node].size(); i++) + + if (!vis[ad2[node][i]]) + + dfs2(ad2[node][i]); + +} + + + +int main() { + + fin >> n >> m; + + for (int i = 0; i < m; i++) { + + int x, y; + + fin >> x >> y; + + + + ad1[x].push_back(y); + + ad2[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + if (!vis[i]) + + dfs1(i); + + + + for (int i = 1; i <= n; i++) + + vis[i] = false; + + + + while (top) { + + if (!vis[st[top]]) { + + dfs2(st[top]); + + nrComp++; + + } + + top--; + + } + + + + fout << nrComp << '\n'; + + for (int i = 0; i < nrComp; i++) { + + for (int j = 0; j < (int) comp[i].size(); j++) + + fout << comp[i][j] << ' '; + + fout << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/51e0660b-3c55-4ebf-9cfa-71cf9392f9c7.cpp b/scc/kosaraju/51e0660b-3c55-4ebf-9cfa-71cf9392f9c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b83dcfd00f2aa0c337450fa8b6de1321faf417b --- /dev/null +++ b/scc/kosaraju/51e0660b-3c55-4ebf-9cfa-71cf9392f9c7.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int nmax=100005; + +vector G[nmax],Gt[nmax],sol[nmax]; + +stack s; + +bool viz[nmax]; + +int n,m,i,j,x,y,k; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto i:G[nod]) + + if(!viz[i]) + + dfs(i); + + s.push(nod); + +} + +void dfs2(int nod) + +{ + + viz[nod]=1; + + sol[k].push_back(nod); + + for(auto i:Gt[nod]) + + if(!viz[i]) + + dfs2(i); + +} + +int main() + +{ + + f>>n>>m; + + for(i=1; i<=m; i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + Gt[y].push_back(x); + + } + + for(i=1; i<=n; i++) + + if(!viz[i]) + + dfs(i); + + for(i=1; i<=n; i++) + + viz[i]=0; + + while(!s.empty()) + + { + + while(!s.empty()&&viz[s.top()]) + + s.pop(); + + if(s.empty())break; + + k++; + + dfs2(s.top()); + + + + } + + g< + + +using namespace std; + +int i, j, aux, n, m, x, y, k; + +bool viz[100005], viz2[100005]; + +vector comp[100005], g[100005], gt[100005]; + +stack S1, S2; + +void dfs(int x) + +{ + + viz[x] = true; + + for(int i = 0; i < g[x].size(); i ++) + + { + + int aux = g[x][i]; + + if(viz[aux] == false)dfs(aux); + + } + + S1.push(x); + +} + +void dfst(int x) + +{ + + viz2[x] = true; + + for(int i = 0; i < gt[x].size(); i ++) + + { + + int aux = gt[x][i]; + + if(viz2[aux] == false)dfst(aux); + + } + + S2.push(x); + +} + +int main() + +{ + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + scanf("%d %d", &n, &m); + + for(i = 1; i <= m; i ++) + + { + + scanf("%d %d", &x, &y); + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + for(i = 1; i <= n; i ++) + + if(viz[i] == false)dfs(i); + + while(!S1.empty()) + + { + + x = S1.top(); + + if(viz2[x] == false) + + { + + k ++; + + dfst(x); + + while(!S2.empty()) + + { + + comp[k].push_back(S2.top()); + + S2.pop(); + + } + + } + + S1.pop(); + + } + + printf("%d\n", k); + + for(i = 1; i <= k; i ++) + + { + + for(j = 0; j < comp[i].size(); j ++) + + printf("%d ", comp[i][j]); + + printf("%s", "\n"); + + } + + return 0; + +} diff --git a/scc/kosaraju/52fbb546-cebb-4ec8-91c3-7972d128c16e.cpp b/scc/kosaraju/52fbb546-cebb-4ec8-91c3-7972d128c16e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbca9af09c50b0c921ce058e20d1be58e12dbc3a --- /dev/null +++ b/scc/kosaraju/52fbb546-cebb-4ec8-91c3-7972d128c16e.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector>graf(100002),graf_trans(100002),rez(100002); + +vectorvizitat(100001); + +vectorvect; + +int cnt; + + + +void dfs_graf(int nod) + +{ + + int i; + + vizitat[nod] = true; + + for(i=0; i>nr_varfuri>>nr_arce; + + for(i=0; i>x>>y; + + graf[x].push_back(y); + + graf_trans[y].push_back(x); + + } + + for(i=1 ; i<=nr_varfuri; i++) + + { + + if(!vizitat[i]) + + { + + dfs_graf(i); + + } + + } + + vizitat = vector(nr_varfuri+1,false); + + for(i=vect.size()-1; i>=0; i--) + + { + + if(!vizitat[vect[i]]) + + { + + cnt++; + + dfs_graf_trans(vect[i]); + + } + + } + + g< +using namespace std; + + + +const int NMAX = 100005; + +int cnt; + +vector edges[NMAX],reverse_edges[NMAX] , ctc[NMAX]; + +int viz[NMAX]; + +stack st; + +void dfs(int nod) + +{ + + viz[nod] = 1 ; + + for(auto point : edges[nod]) + + if(!viz[point]) + + dfs(point); + + st.push(nod); + +} + +void dfs1(int nod) + +{ + + ctc[cnt].push_back(nod); + + viz[nod] = 1; + + for(auto point : reverse_edges[nod]) + + if(!viz[point]) + + dfs1(point); + +} + +int main() + +{ + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + int n , m , i , j , a , b ; + + cin >> n >> m; + + for( i = 0 ; i < m ; ++i) + + { + + scanf("%d%d",&a,&b); + + edges[a].push_back(b); + + reverse_edges[b].push_back(a); + + } + + for(i = 1 ; i <= n; ++i) + + if(!viz[i])dfs(i); + + memset(viz,0,sizeof(viz)); + + while(!st.empty()) + + { + + if(!viz[st.top()]) + + {dfs1(st.top()); ++ cnt;} + + st.pop(); + + } + + cout< +#include +#include + + + + +using namespace std; + +int cont=0,cnt=0; + +int viz[100005]; + +vector v1[100005],v2[100005]; + +int order[100005]; + +vector comp[100005]; + +void dfs1(int nod){ + + viz[nod]=1; + + for(auto u:v1[nod]){ + + if(viz[u]==0){ + + dfs1(u); + + } + + } + + order[++cont]=nod; + +} + + + +void dfs2(int nod){ + + viz[nod]=1; + + comp[cnt].push_back(nod); + + for(auto u:v2[nod]){ + + if(viz[u]==0){ + + dfs2(u); + + } + + } + +} + + + +int main() + +{ + + ifstream cin("ctc.in"); + + ofstream cout("ctc.out"); + + int n,m,a,b; + + cin>>n>>m; + + for(int i=1;i<=m;i++){ + + cin>>a>>b; + + v1[a].push_back(b); + + v2[b].push_back(a); + + } + + for(int i=1;i<=n;i++){ + + if(viz[i]==0){ + + dfs1(i); + + } + + } + + for(int i=1;i<=n;i++){ + + viz[i]=0; + + } + + for(int i=n;i>=1;i--){ + + if(viz[order[i]]==0){ + + cnt++; + + dfs2(order[i]); + + } + + } + + cout< + + + +std::ifstream f("ctc.in"); +std::ofstream g("ctc.out"); + + +std::stack firstDfsSt; +std::vector G[100005]; +std::vector T[100005]; +std::vector comps[100005]; +std::vector firstDfsViz(100005), inComp(100005); + +int n, m, cnt = 0; + + +void readData(){ + f >> n >> m; + int x, y; + for(int i = 0; i> x >> y; + G[x].push_back(y); + T[y].push_back(x); + } +} + +void dfsFirst(int start){ + firstDfsViz[start] = true; + for(auto& adj : G[start]){ + if(!firstDfsViz[adj]){ + dfsFirst(adj); + } + } + firstDfsSt.push(start); +} + + + +void dfsSecond(int start){ + inComp[start] = true; + for(auto& adj : T[start]){ + if(!inComp[adj]){ + dfsSecond(adj); + } + } + + comps[cnt].push_back(start); +} + + +void solve(){ + for(int i = 1; i<=n; i++){ + if(!firstDfsViz[i]){ + dfsFirst(i); + } + } + + while(!firstDfsSt.empty()){ + int node = firstDfsSt.top(); + if(!inComp[node]){ + dfsSecond(node); + cnt ++; + }else{ + firstDfsSt.pop(); + } + } +} + + +void printSolution(){ + g << cnt << '\n'; + for(int i = 0; i +#include +#include + + +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out ("ctc.out"); + + + +int n,m,cnt; + +bool ok[100001]; + +vector dir[100001],inv[100001],v[200001]; + +deque d; + + + +void dfs(int x) + +{ + + ok[x]=1; + + for (int i=0;i>n>>m; + + for (int i=1;i<=m;i++) + + in>>a>>b,dir[a].push_back(b),inv[b].push_back(a); + + + + for (int i=1;i<=n;i++) + + { + + if (!ok[i]) + + dfs(i),d.push_front(i); + + } + + for (int i=1;i<=n;i++) + + ok[i]=0; + + for (int i : d) + + { + + if (!ok[i]) + + { + + cnt++; + + dfs2(i); + + v[cnt].push_back(i); + + } + + } + + out< +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int nlim=100005; + +int n,m,nrCTC=0; + +int beenThere[nlim]; + +vector muchii[nlim],muchii_T[nlim],result[nlim]; + +stack S; + + + +void citire() + +{ + + f>>n>>m; + + int x,y; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + muchii[x].push_back(y); + + muchii_T[y].push_back(x); + + } + +} + + + +void DFS(int nod) + +{ + + beenThere[nod]=1; + + for(int i=0;i + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m,nrc; + +vector edge[100001],revedge[100001]; + +vectorpopn; + +vector ans[100001]; + +bitset<100001> viz; + + + +void DFS(int x) + +{ + + viz[x] = 1; + + for (int i : edge[x]) + + if (!viz[i]) + + DFS(i); + + popn.push_back(x); + +} + +void DFSREV(int x) + +{ + + viz[x] = 1; + + ans[nrc - 1].push_back(x); + + for (int i : revedge[x]) + + if (!viz[i]) + + DFSREV(i); + +} + +int main() + +{ + + int x, y; + + fin >> n >> m; + + while (m--) + + { + + fin >> x >> y; + + edge[x].push_back(y); + + revedge[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) + + if (!viz[i]) + + DFS(i); + + viz.reset(); + + for(vector::reverse_iterator it = popn.rbegin(); it != popn.rend(); it++) + + if (!viz[*it]) + + { + + nrc++; + + DFSREV(*it); + + } + + + + fout << nrc << "\n"; + + for (int i = 0; i <= ans->size(); i++) + + sort(ans[i].begin(), ans[i].end()); + + for (int i = 0; i < nrc; i++) { + + for (int j : ans[i]) + + fout << j << " "; + + fout << "\n"; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/5705cb80-918e-4ee5-815c-3d02cc5d5776.cpp b/scc/kosaraju/5705cb80-918e-4ee5-815c-3d02cc5d5776.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c4314298d318083d53080b1a088ac1e7260d9f2 --- /dev/null +++ b/scc/kosaraju/5705cb80-918e-4ee5-815c-3d02cc5d5776.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +stack < int > S; + +vector G[100005],GT[100005],CTC[100005]; + +int N, M, NrCTC; + +int beenThere[100005]; + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for(unsigned int i=0; i + + +using namespace std; + + + +const int NMAX = 100005; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int N, M, x, y; + +vector < int > Ad[NMAX]; + +vector < int > Ad_r[NMAX]; + +vector < int > T; + + + +bool viz[NMAX]; + + + +int ctc; + +vector < int > C[NMAX]; + + + +void DFS( int nod ) + +{ + + viz[nod] = 1; + + + + for( int i = 0; i < Ad[nod].size(); ++i ) + + { + + int w = Ad[nod][i]; + + + + if( !viz[w] ) + + DFS( w ); + + } + + T.push_back( nod ); + +} + +void Read() + +{ + + fin >> N >> M; + + + + for( int i = 1; i <= M; ++i ) + + { + + fin >> x >> y; + + Ad[x].push_back( y ); + + Ad_r[y].push_back( x ); + + } + + + + for( int i = 1; i <= N; ++i ) + + if( !viz[i] )DFS( i ); + + for( int i = 1; i <= N; ++i ) + + viz[i] = 0; + +} + + + +void DFSr( int nod ) + +{ + + viz[nod] = 1; + + C[ctc].push_back( nod ); + + for( int i = 0; i < Ad_r[nod].size(); ++i ) + + { + + int w = Ad_r[nod][i]; + + + + if( !viz[w] ) + + DFSr( w ); + + } + + + +} + +void Solve() + +{ + + for( int i = T.size()-1; i >= 0; --i ) + + if( !viz[T[i]] ) + + { + + ctc++; + + DFSr( T[i] ); + + } + + fout << ctc << '\n'; + + + + for( int i = 1; i <= ctc; ++i ){ + + for( int j = 0; j < C[i].size(); ++j ) + + fout << C[i][j] << ' '; fout << '\n';} + +} + +int main() + +{ + + Read(); + + Solve(); + + return 0; + +} diff --git a/scc/kosaraju/58cc9d03-5efd-408b-9878-269e97b2086a.cpp b/scc/kosaraju/58cc9d03-5efd-408b-9878-269e97b2086a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..979121fe0ef3b781521660f674b605a7a9b0d4dd --- /dev/null +++ b/scc/kosaraju/58cc9d03-5efd-408b-9878-269e97b2086a.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector graf[100005]; + +vector ans[100005]; + +vector graf1[100005]; + +int viz[100005],ans1; + + + +stack s; + +int dfs(int nod,int ok,vector x[100005]) + +{ + + viz[nod]=1; + + if(ok<0) + + ans[ans1].push_back(nod); + + for(int i=0; i0) + + s.push(nod); + +} + + + + + +int main() + +{ + + int n,m; + + fin>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y; + + fin>>x>>y; + + graf[x].push_back(y); + + graf1[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(viz[i]==0) + + dfs(i,1,graf); + + memset(viz,0,sizeof(viz)); + + while(!s.empty()) + + { + + if(viz[s.top()]==0) + + { + + ans1++; + + dfs(s.top(),-1,graf1); + + } + + s.pop(); + + } + + fout< +using namespace std; +const int N=100009; +const int M = 200001; +int lst1[N],vf1[M],n,m,L,D,nr1,urm1[M],sorttop[N],ns,lst2[N],vf2[M],urm2[M],nr2,nrcomp,nrel[N], nrv, c[N]; +bool viz[N],v[N]; +ifstream in("ctc.in"); +ofstream out("ctc.out"); +void adauga1(int x, int y) +{ + vf1[++nr1]=y; + urm1[nr1]=lst1[x]; + lst1[x]=nr1; + +} + +void adauga2(int x,int y) +{ + vf2[++nr2]=y; + urm2[nr2]=lst2[x]; + lst2[x]=nr2; + +} +void dfs(int x) +{ + viz[x]=true; + int y; + for(int p=lst1[x]; p!=0; p=urm1[p]) + { + y=vf1[p]; + if(!viz[y]) dfs(y); + } + sorttop[++ns]=x; +} + +void dfs2(int x) +{ + v[x]=true; + nrel[nrcomp]++; + c[++nrv] = x; + int y; + for(int p=lst2[x]; p!=0; p=urm2[p]) + { + y=vf2[p]; + if(!v[y]) dfs2(y); + + } + +} +int main() +{ + in>>n>>m; + for(int i=1; i<=m; i++) + { + in>>L>>D; + adauga1(L,D); + adauga2(D,L); + } + for(int i=1; i<=n; i++) + { + if(!viz[i]) dfs(i); + } + + + for(int i=ns; i>=1; i--) + { + if(!v[sorttop[i]]) + { + nrcomp++; + dfs2(sorttop[i]); + + } + + } + + + out< +#include +using namespace std; + +ifstream in ("ctc.in"); +ofstream out ("ctc.out"); + +const int N = 100000; +const int M = 200000; +int n, m, vf[M], urm[M], lst[N], q[N], vf2[M], urm2[M], lst2[N], st[N], st2[N], nvf[N]; +bool viz1[N], viz2[N]; +int nc, nr; + +void adauga (int a, int b){ + vf[++nr] = b; + urm[nr] = lst[a]; + lst[a] = nr; + vf2[nr] = a; + urm2[nr] = lst2[b]; + lst2[b] = nr; +} + +void dfs1(int x){ + viz1[x] = true; + for(int p = lst[x]; p != 0; p = urm[p]){ + int y = vf[p]; + if(viz1[y] == false){ + dfs1(y); + } + } + st[++nr] = x; +} + +void dfs2(int x){ + viz2[x] = true; + nvf[nc]++; + st2[++nr] = x; + for(int p = lst2[x]; p != 0; p = urm2[p]){ + int y = vf2[p]; + if(!viz2[y]){ + dfs2(y); + } + } +} + + +int main() +{ + int x, y; + in >> n >> m; + for(int i = 0; i < m; i++){ + in >> x >> y; + adauga(x, y); + } + nr = 0; + for(int i = 1; i <= n; i++) + { + if(viz1[i] == 0) + { + dfs1(i); + } + } + nr = 0; + nc = 0; + for(int i = n; i >= 1; i--){ + if(viz2[st[i]] == false){ + nc++; + dfs2(st[i]); + } + } + out << nc << "\n"; + int a = 1; + for(int i = 1; i <= nc; i++){ + for(int j = 0; j < nvf[i]; j++) + { + out << st2[a++] << " "; + } + out << "\n"; + } + return 0; +} diff --git a/scc/kosaraju/597e5027-30f3-4d74-bf7d-aed66c04a8b2.cpp b/scc/kosaraju/597e5027-30f3-4d74-bf7d-aed66c04a8b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0e3b40b6fef5133b3490db9857a4d520f8d50aa --- /dev/null +++ b/scc/kosaraju/597e5027-30f3-4d74-bf7d-aed66c04a8b2.cpp @@ -0,0 +1,142 @@ +#include +using namespace std; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m; + + + vector adj[100010]; + + + vector adj_trans[100010]; + + + vector> all_ctc; + + + + + + + + vector visited; + + + + vector topsort; + + void read_input() { + cin >> n >> m; + visited = vector(n + 1, 0); + + for (int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + } + + void get_result() { + kosaraju(); + } + + void kosaraju() { + topsort.push_back(-1); + + + for (int i = 1; i <= n; ++i) { + if (!visited[i]) { + dfs(i); + } + } + + + for (int i = n; i >= 1; --i) { + if (visited[topsort[i]]) { + + vector current_ctc; + + dfs_t(topsort[i], current_ctc); + + + all_ctc.push_back(current_ctc); + } + } + } + + + void dfs(int node) { + visited[node] = 1; + + for (auto &vecin : adj[node]) { + if (!visited[vecin]) { + dfs(vecin); + } + } + + + topsort.push_back(node); + } + + + void dfs_t(int node, vector ¤t_ctc) { + + visited[node] = 0; + current_ctc.push_back(node); + + for (auto &vecin : adj_trans[node]) { + if (visited[vecin]) { + dfs_t(vecin, current_ctc); + } + } + } + + void print_output() { + cout << all_ctc.size() << "\n"; + for (const auto& ctc : all_ctc) { + for (auto &node : ctc) { + cout << node << " "; + } + cout << '\n'; + } + } +}; + +int main() { + + auto cin_buff = cin.rdbuf(); + auto cout_buff = cout.rdbuf(); + + + ifstream fin("ctc.in"); + cin.rdbuf(fin.rdbuf()); + + + ofstream fout("ctc.out"); + cout.rdbuf(fout.rdbuf()); + + + Task *task = new Task(); + task->solve(); + delete task; + + + cin.rdbuf(cin_buff); + cout.rdbuf(cout_buff); + + + + + return 0; +} diff --git a/scc/kosaraju/59fc395e-2910-4e13-b24f-73aefce051c5.cpp b/scc/kosaraju/59fc395e-2910-4e13-b24f-73aefce051c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4362d83438eab857afdf64f97ffeb0e49bdfca34 --- /dev/null +++ b/scc/kosaraju/59fc395e-2910-4e13-b24f-73aefce051c5.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + +using namespace std; + + + + + + + +bool viz1[100005], viz2[100005]; + +vector graph1[100005]; + +vector graph2[100005]; + +vector comp[100005]; + +stack ord; + +int n, m, ncomp; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + + + +void dfs1(int nod) + +{ + + viz1[nod] = 1; + + int len, i, var; + + len = graph1[nod].size(); + + + + for(i = 0; i < len; i++) + + { + + var = graph1[nod][i]; + + if(!viz1[var]) + + dfs1(var); + + } + + ord.push(nod); + +} + + + +void dfs2(int nod) + +{ + + viz2[nod] = 1; + + int len, i, var; + + len = graph2[nod].size(); + + + + for(i = 0; i < len; i++) + + { + + var = graph2[nod][i]; + + if(!viz2[var]) + + dfs2(var); + + } + + comp[ncomp].push_back(nod); + +} + + + +int main() + +{ + + int i, j, x, y, len; + + f >> n >> m; + + + + for(i = 0; i < m; i++) + + { + + f >> x >> y; + + graph1[x].push_back(y); + + graph2[y].push_back(x); + + } + + + + for(i = 1; i <= n; i++) + + if(viz1[i] == 0) + + dfs1(i); + + + + while(!ord.empty()) + + { + + x = ord.top(); + + ord.pop(); + + if(!viz2[x]) + + { + + dfs2(x); + + ncomp++; + + } + + } + + + + g << ncomp << endl; + + + + for(i = 0; i < ncomp; i++) + + { + + len = comp[i].size(); + + + + for(j = 0; j < len; j++) + + g << comp[i][j] << ' '; + + + + g << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/5a4d5d4e-c8fc-4bbe-91a5-4af6f543f76b.cpp b/scc/kosaraju/5a4d5d4e-c8fc-4bbe-91a5-4af6f543f76b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f499ef5f877da7a528c1beb70dd36af0428f0e2c --- /dev/null +++ b/scc/kosaraju/5a4d5d4e-c8fc-4bbe-91a5-4af6f543f76b.cpp @@ -0,0 +1,147 @@ +#include +#include + + + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +vector adjList[100001], adjTrList[100001], ordList, component; + +bool isVisited[100001]; + +bool isVisitedTr[100001]; + +vector> result; + + + +void dfs(int startNode) { + + isVisited[startNode] = true; + + + + for (const auto& node : adjList[startNode]) { + + if (!isVisited[node]) { + + dfs(node); + + } + + } + + ordList.push_back(startNode); + +} + + + +void dfsTr(int startNode) { + + isVisitedTr[startNode] = true; + + component.push_back(startNode); + + + + for (const auto& node : adjTrList[startNode]) { + + if (!isVisitedTr[node]) { + + dfsTr(node); + + } + + } + +} + + + +int main() { + + + + int N, M; + + + + in >> N >> M; + + for (int i = 0; i < M; ++i) { + + int startNode, endNode; + + in >> startNode >> endNode; + + adjList[startNode].push_back(endNode); + + adjTrList[endNode].push_back(startNode); + + } + + + + for (int node = 1; node <= N; ++node) { + + if (!isVisited[node]) { + + dfs(node); + + } + + } + + + + int stronglyConnComp = 0; + + for (int i = 1; i <= N; ++i) { + + int node = ordList[N - i]; + + if (!isVisitedTr[node]) { + + ++stronglyConnComp; + + dfsTr(node); + + result.push_back(component); + + component.clear(); + + } + + } + + + + out << stronglyConnComp << '\n'; + + for (const auto& resultComponent: result) { + + for (const auto& node : resultComponent) { + + out << node << ' '; + + } + + out << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/5ab7f17a-1e60-474f-832c-4415303795cb.cpp b/scc/kosaraju/5ab7f17a-1e60-474f-832c-4415303795cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4ef208f2aba0c1c2d1c5b5cae800288dce792b7 --- /dev/null +++ b/scc/kosaraju/5ab7f17a-1e60-474f-832c-4415303795cb.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +vector < int > v1[100005], v2[100005], a[100005]; + +stack < int > s; + +int nr; + +bool viz1[100005], viz2[100005]; + + + +void bfs1 ( int nod ); + +void bfs2 ( int nod ); + + + +int main() + +{ + + int n, m, i, j, x, y; + + + + fin >> n >> m; + + for ( i = 1 ; i <= m ; i++ ) + + { + + fin >> x >> y; + + v1[x].push_back ( y ); + + v2[y].push_back ( x ); + + } + + + + for ( i = 1 ; i <= n ; i++ ) if ( viz1[i] == 0 ) bfs1 ( i ); + + + + while ( s.empty() == 0 ) + + { + + if ( viz2[s.top()] == 0 ) + + { + + nr++; + + bfs2 ( s.top() ); + + } + + + + s.pop(); + + } + + + + fout << nr << '\n'; + + for ( i = 1 ; i <= nr ; i++ ) + + { + + for ( j = 0 ; j < a[i].size() ; j++ ) fout << a[i][j] << ' '; + + fout << '\n'; + + } + + + + return 0; + +} + + + +void bfs1 ( int nod ) + +{ + + int i; + + + + viz1[nod] = 1; + + for ( i = 0 ; i < v1[nod].size() ; i++ ) if ( viz1[v1[nod][i]] == 0 ) bfs1 ( v1[nod][i] ); + + s.push ( nod ); + +} + + + +void bfs2 ( int nod ) + +{ + + int i; + + + + viz2[nod] = 1; + + for ( i = 0 ; i < v2[nod].size() ; i++ ) if ( viz2[v2[nod][i]] == 0 ) bfs2 ( v2[nod][i] ); + + a[nr].push_back ( nod ); + +} diff --git a/scc/kosaraju/5c18515a-fc93-4fb2-866d-ae92cebbb30b.cpp b/scc/kosaraju/5c18515a-fc93-4fb2-866d-ae92cebbb30b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85efdc1e12187f85672eb7399c006ff72eafb1f5 --- /dev/null +++ b/scc/kosaraju/5c18515a-fc93-4fb2-866d-ae92cebbb30b.cpp @@ -0,0 +1,68 @@ +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int nmax = 1e5 + 6; + +vector G[nmax]; +vector TG[nmax]; +stack topologicSort; + +bool vizited[nmax]; + +void normalDfs(int node) +{ + vizited[node] = true; + for (auto next : G[node]) + if (!vizited[next]) + normalDfs(next); + topologicSort.push(node); +} + +vector components[nmax]; + +void trsDfs(int node, const int comp) +{ + vizited[node] = false; + components[comp].push_back(node); + for (auto next : TG[node]) + if (vizited[next]) + trsDfs(next, comp); +} + +int main() +{ + int n, m; + f >> n >> m; + for (int i = 1; i <= m; ++ i) + { + int u, v; + f >> u >> v; + G[u].push_back(v); + TG[v].push_back(u); + } + for (int node = 1; node <= n; ++ node) + if (!vizited[node]) + normalDfs(node); + + int cnt = 0; + while (!topologicSort.empty()) + { + int startNode = topologicSort.top(); + topologicSort.pop(); + if (vizited[startNode]) + trsDfs(startNode, ++ cnt); + } + g << cnt << '\n'; + for (int i = 1; i <= cnt; ++ i) + { + for (auto el : components[i]) + g << el << " "; + g << '\n'; + } + + +} diff --git a/scc/kosaraju/5c7bbf2c-b389-4173-8cf7-e8d0f6042945.cpp b/scc/kosaraju/5c7bbf2c-b389-4173-8cf7-e8d0f6042945.cpp new file mode 100644 index 0000000000000000000000000000000000000000..700a05ea55622e0ff7d4132e67344f28f1afb6c2 --- /dev/null +++ b/scc/kosaraju/5c7bbf2c-b389-4173-8cf7-e8d0f6042945.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int N = 100010; + +int n,m,comp[N],nc; + +vector vd[N],vi[N]; + +bitset viz; + +stack st; + +void dfsD(int); + +void dfsI(int); + +vector c; + +vector> C; + +int main() + +{ + + f>>n>>m; + + for(;m;m--) + + { + + int x,y; + + f>>x>>y; + + vd[x].push_back(y); + + vi[y].push_back(x); + + + + } + + for(int i=1;i<=n;i++) + + if(!viz[i]) + + dfsD(i); + + + + do + + { + + while(st.size()&&comp[st.top()]) + + st.pop(); + + if(st.size()) + + { + + nc++; + + int x=st.top(); + + c.resize(0); + + dfsI(x); + + C.push_back(c); + + } + + } + + while(st.size()); + + g< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],vT[100100]; + +int nr,t,vectorT[100100],viz[100100]; + +vector sol[100100]; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + vT[y].push_back(x); + + } + + + + for(i=1;i<=n;i++)if(viz[i]==0)DFS(i); + + for(i=n;i>=1;i--)if(viz[vectorT[i]]==1)nr++,DFS_T(vectorT[i]); + + + + g< + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 100000; + +vector G[NMAX + 1]; + +vector G_transpose[NMAX + 1]; + +stack st; + +vector> sol; + +vector v; + + + +int viz[NMAX + 1]; + + + +void dfs( int node ) { + + viz[node] = 1; + + + + for( const auto &it : G[node] ) { + + if( !viz[it] ) + + dfs(it); + + } + + + + st.push(node); + +} + + + +void tareconexe( int node ) { + + viz[node] = 1; + + v.push_back(node); + + for( const auto &it : G_transpose[node] ) + + if( !viz[it] ) + + tareconexe(it); + +} + + + +int main() { + + int n, m; + + fin>>n>>m; + + for( int i = 1; i <= m; i ++ ) { + + int x, y; + + fin>>x>>y; + + G[x].push_back(y); + + G_transpose[y].push_back(x); + + } + + for( int i = 1; i <= n; i ++ ) { + + if( !viz[i] ) { + + dfs(i); + + } + + } + + memset(viz, 0 ,sizeof(viz)); + + while( !st.empty() ) { + + int node = st.top(); + + st.pop(); + + if( !viz[node] ) { + + tareconexe(node); + + sol.push_back(v); + + v.clear(); + + } + + } + + fout< +#include +#include +#include +using namespace std; + +vector v[100010],vt[100010],cc[100010]; + +stack S; + +int n,ctc=0; bool viz[100010],vizt[100010]; + +void citire(){ + + int i,m,x,y; ifstream f("ctc.in"); + + f>>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + +} + +void dfs(int k){ + + viz[k]=1; int i,nv; + + for(i=0;i + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int Nmax = 1e5 + 5; + + + +int n, m, lg, nrc; + +int sortTop[Nmax]; + +bitset viz; + +vector L[Nmax], G[Nmax], sol[Nmax]; + + + +void SortTop(int nod) + +{ + + viz[nod] = 1; + + for(auto i : L[nod]) + + if(!viz[i]) SortTop(i); + + + + sortTop[++lg] = nod; + +} + + + +void Componente(int nod) + +{ + + viz[nod] = 1; + + for(auto i : G[nod]) + + if(!viz[i]) Componente(i); + + + + sol[nrc].push_back(nod); + +} + + + +int main() + +{ + + fin >> n >> m; + + + + int x, y; + + for(int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + L[x].push_back(y); + + G[y].push_back(x); + + } + + + + for(int i = 1; i <= n; i++) + + if(!viz[i]) SortTop(i); + + + + for(int i = 1; i <= n ; i++) + + cout << sortTop[i] << " "; + + + + viz.reset(); + + for(int i = n; i >= 1; i--) + + if(!viz[sortTop[i]]) + + { + + nrc++; + + Componente(sortTop[i]); + + } + + + + fout << nrc << "\n"; + + + + for(int i = 1; i <= nrc; fout << "\n", i++) + + for(auto j : sol[i]) + + fout << j << " "; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/6025d79c-0d42-4600-aa4b-e94c09acb1bd.cpp b/scc/kosaraju/6025d79c-0d42-4600-aa4b-e94c09acb1bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9bc13b5311aedf58ffbc546dd9ba10fa89457b3 --- /dev/null +++ b/scc/kosaraju/6025d79c-0d42-4600-aa4b-e94c09acb1bd.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; typedef vector vii; typedef vector vll; + +template std::istream& operator>>(std::istream& stream, vector &v) { for (T& t: v) { stream >> t; } return stream; } template std::ostream& operator<<(std::ostream& stream, const vector &v) { for (const T& t : v) { stream << t << ' '; } return stream; } template std::istream& operator>>(std::istream& stream, pair& p) { return stream >> p.first >> p.second; } template std::ostream& operator<<(std::ostream& stream, const pair& p) { return stream << p.first << ' ' << p.second << ' '; } + +template void read(T& t) { cin >> t; } template void read(T& t, TArgs&&... args) { read(t); read(forward(args)...); } template void readc(T& t); template<> void readc(int& v) { scanf("%d", &v); } template<> void readc(unsigned int& v) { scanf("%u", &v); } template<> void readc(char& v) { scanf(" %c", &v); } template<> void readc(long long& v) { scanf("%lld", &v); } template<> void readc(unsigned long long& v) { scanf("%llu", &v); } template<> void readc(float& v) { scanf("%f", &v); } template<> void readc(double& v) { scanf("%lf", &v); } template void readc(char (&str)[sz]) { scanf("%s", str); } template<> void readc(char*& str) { scanf("%s", str); } template void readc(pair& v) { readc(v.first); readc(v.second); } template void readc(vector& v) { for (E& e : v) readc(e); } template void readc(T& t, TArgs&&... args) { readc(t); readc(forward(args)...); } + +template void printc(T t); template<> void printc(int v) { printf("%d ", v); } template<> void printc(unsigned int v) { printf("%u ", v); } template<> void printc(char v) { printf("%c ", v); } template<> void printc(long long v) { printf("%lld ", v); } template<> void printc(unsigned long long v) { printf("%llu ", v); } template<> void printc(float v) { printf("%f ", v); } template<> void printc(double v) { printf("%lf ", v); } template<> void printc(char *str) { printf("%s ", str); } template<> void printc(const char *str) { printf("%s ", str); } template void printc(const pair& v) { printc(v.first); printc(v.second); } template void printc(const vector& v) { for (const E& e : v) printc(e); } template void printc(const T& t, TArgs&&... args) { printc(t); printc(forward(args)...); } + +template void __DEBUG_BASE(const T& t) { cout << t; } template void __DEBUG_BASE(const T& t, TArgs&&... args) { __DEBUG_BASE(t); cout << ", "; __DEBUG_BASE(forward(args)...); } template void __DEBUG_IMPL(TArgs&&... args) { __DEBUG_BASE(forward(args)...); } + +char lower(char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; return ch; } char upper(char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 'A'; return ch; } array VOWELS = {'a','e','i','o','u'}; bool isvowel(char ch) { return find(VOWELS.begin(), VOWELS.end(), lower(ch)) != VOWELS.end(); }; bool isupper(char ch) { return ch >= 'A' && ch <= 'Z'; } bool islower(char ch) { return ch >= 'a' && ch <= 'z'; } +int main() +{ + ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); + + freopen("ctc" ".in", "r", stdin); freopen("ctc" ".out", "w", stdout); + + int n, m; + readc(n, m); + + vector> adj(n + 1); + vector> adj_in(n + 1); + while (m--) { + int x, y; + readc(x, y); + adj[x].push_back(y); + adj_in[y].push_back(x); + } + + vector vis(n + 1); + + vector st; + st.reserve(n); + function order_em = [&] (int node) { + if (vis[node]) return; + vis[node] = true; + + for (int neigh : adj[node]) + order_em(neigh); + st.push_back(node); + }; + + for (int i = 1; i <= n; i++) { + order_em(i); + } + vector roots(n + 1); + + unordered_map> comps; + + function root_em = [&] (int node, int root) { + if (roots[node]) return; + roots[node] = root; + comps[root].push_back(node); + + for (int neigh : adj_in[node]) + root_em(neigh, root); + }; + + for (int i = n - 1; i >= 0; i--) { + root_em(st[i], st[i]); + } + + printf("%d\n", (int)comps.size()); + for (auto v : comps) { + printc(v.second); + puts(""); + } +} diff --git a/scc/kosaraju/609f91ba-202e-455f-96de-32fa15497ae7.cpp b/scc/kosaraju/609f91ba-202e-455f-96de-32fa15497ae7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0ce3e058a06bfa054167e703ecfc392f268e05d --- /dev/null +++ b/scc/kosaraju/609f91ba-202e-455f-96de-32fa15497ae7.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +using namespace std; + +int N, M, nrc; + + + +vector > G, GT, CTC; + +vector viz; + +vector Post; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +void citire() + +{ + + int x, y; + + f >> N >> M; + + G = vector >(N + 1); + + GT = vector >(N + 1); + + CTC = vector >(N + 1); + + viz = vector(N + 1, 0); + + Post = vector(N + 1); + + for(int i = 1; i <= M; i++) + + { + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; ++i) + + { + + for(auto &x : CTC[i]) + + g << x << ' '; + + g << '\n'; + + } + +} + +void dfs(int vf) + +{ + + viz[vf] = 1; + + for(auto &x : G[vf]) + + if(viz[x] == 0) + + dfs(x); + + Post.push_back(vf); + +} + + + +void dfsGT(int vf) + +{ + + viz[vf] = 0; + + CTC[nrc].push_back(vf); + + for(auto &x : GT[vf]) + + if(viz[x] == 1) + + dfsGT(x); + +} + + + +void componente() + +{ + + for(int i = 1; i <= N; i++) + + if(viz[i] == 0) + + dfs(i); + + for(auto it = Post.rbegin(); it != Post.rend(); ++it) + + if(viz[*it] == 1) + + { + + nrc++; + + dfsGT(*it); + + } + +} + +int main() + +{ + + citire(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/61a5e960-fdc5-4e88-ae4e-c0af9988bab6.cpp b/scc/kosaraju/61a5e960-fdc5-4e88-ae4e-c0af9988bab6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..409b0613b4fc78c699b93efae8f5691b9a9d3e6b --- /dev/null +++ b/scc/kosaraju/61a5e960-fdc5-4e88-ae4e-c0af9988bab6.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +stack < int > S; + +vector G[100005],GT[100005],CTC[100005]; + +int N, M, NrCTC; + +int beenThere[100005]; + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for(unsigned int i=0; i + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int Inf = INT_MAX; + +vector < vector > g, tg, comps; +vector topoSort; +vector seen; +int V, E; + + +void readGraph(){ + + fin >> V >> E; + + g.resize(V + 1); + tg.resize(V + 1); + seen.resize(V + 1); + comps.reserve(V); + topoSort.reserve(V); + + for(int e = 0; e < E; ++e){ + + int from, to; + fin >> from >> to; + + g[from].push_back(to); + tg[to].push_back(from); + } +} + + +void topologicalSort(int node){ + + seen[node] = true; + + for(const int &adj : g[node]) + if(!seen[adj]) + topologicalSort(adj); + + topoSort.push_back(node); +} + + +void DFS(int node, vector &SCC){ + + seen[node] = false; + SCC.push_back(node); + + for(const int &adj : tg[node]) + if(seen[adj]) + DFS(adj, SCC); +} + + +int main(){ + + readGraph(); + + for(int node = 1; node <= V; ++node) + if(!seen[node]) + topologicalSort(node); + + + for(int idx = V - 1; idx >= 0; --idx) + if(seen[topoSort[idx]]){ + + vector SCC; + SCC.reserve(V); + + DFS(topoSort[idx], SCC); + comps.push_back(SCC); + } + + + + fout << comps.size() << '\n'; + for(int c = 0; c < (int)comps.size(); ++c){ + + for(const int &node : comps[c]) + fout << node << ' '; + fout << '\n'; + } +} diff --git a/scc/kosaraju/62696952-828d-4d78-9575-35e80529dfbd.cpp b/scc/kosaraju/62696952-828d-4d78-9575-35e80529dfbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a6cf0e39f883ad298b114f04558674a08b98917 --- /dev/null +++ b/scc/kosaraju/62696952-828d-4d78-9575-35e80529dfbd.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int N = 1e5 + 5; + +int n, m, i, j, scc, nod; + +int viz[N], stk[N]; + +vector ans[N]; + +vector Gx[N]; + +vector Gy[N]; + + + +void dfs1(int x) + +{ + + viz[x] = 1; + + vector::iterator it; + + for(it = Gx[x].begin(); it < Gx[x].end(); it++) + + if(viz[*it] == 0) dfs1(*it); + + stk[++m] = x; + +} + + + +void dfs2(int x, int scc) + +{ + + viz[x] = 2; + + ans[scc].push_back(x); + + vector::iterator it; + + for(it = Gy[x].begin(); it < Gy[x].end(); it++) + + if(viz[*it] == 1) dfs2(*it, scc); + +} + + + +int main() + +{ + + in >> n >> m; + + + + for(i = 1; i <= m; i++) + + { + + int x, y; + + in >> x >> y; + + Gx[x].push_back(y); + + Gy[y].push_back(x); + + } + + + + m = 0; + + + + for(i = 1; i <= n; i++) + + if(viz[i] == 0) dfs1(i); + + + + scc = 0; + + + + while(m > 0) + + { + + nod = stk[m]; + + m--; + + if(viz[nod] == 1) dfs2(nod, ++scc); + + } + + + + out << scc << '\n'; + + + + for(i = 1; i <= scc; i++) + + { + + for(j = 0; j < ans[i].size(); j++) + + out << ans[i][j] << ' '; + + out << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/63267dee-5e5e-47a8-af2b-7f5b8c4c6596.cpp b/scc/kosaraju/63267dee-5e5e-47a8-af2b-7f5b8c4c6596.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6eec1604b41d7f6e048336e82472d0043673123 --- /dev/null +++ b/scc/kosaraju/63267dee-5e5e-47a8-af2b-7f5b8c4c6596.cpp @@ -0,0 +1,186 @@ +#include + + + + + + +using namespace std; +int n,m,postord[100005], t, viz[100005], k; + +vector G[100005], GT[100005], ctc[100005]; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +void DFS(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + + + + + + + for (i = 0;i< G[x].size();i++) + + { + + w=G[x][i]; + + if (!viz[w]) + + DFS(w); + + } + +t++; + +postord[t] = x; + +} + + + + + + + +void DFST(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + ctc[k].push_back(x); + + for (i = 0;i>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for (i = 1; i <= n; i++) + + if (!viz[i]) + + DFS(i); + for(i=1;i<=n;i++) + + viz[i]=0; + + + + + + + + k=0; + + + + for (i = n; i>=1; i--) + + if (!viz[postord[i]]) + + { + + k++; + + DFST(postord[i]); + + } + + + + + + + + fout< + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int N=100008; + +int n,m,nr; + +int d[N]; + +vector v1[N],v2[N],sol[N]; + +stack sk; + +void dfs(int nod) + +{ + + d[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + v1[x].push_back(y); + + v2[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(!d[i]) + + dfs(i); + + memset(d,0,sizeof(d)); + + for(int i=1;i<=n;i++) + + { + + int x=sk.top(); + + if(!d[sk.top()]) + + { + + nr++; + + dfs2(sk.top(),nr); + + } + + sk.pop(); + + } + + g< +#include +#include +#include + + + + +FILE* fin, * fout; + + + +int n, m; + +std::vector G[100005], GT[100005]; + +int nr = 0; + +std::vector res[100005]; + +std::stack s; + +bool viz[100005]; + + + +void dfs(int u) { + + if (viz[u] == true) + + return; + + viz[u] = true; + + for (int v : G[u]) + + dfs(v); + + s.push(u); + +} + + + +void dfs_t(int u) { + + if (viz[u] == true) + + return; + + viz[u] = true; + + res[nr].push_back(u); + + for (int v : GT[u]) + + dfs_t(v); + +} + + + + + +int main() { + + fin = fopen("ctc.in", "r"); + + fout = fopen("ctc.out", "w"); + + + + fscanf(fin, "%i %i", &n, &m); + + + + while (m--) { + + int x, y; + + fscanf(fin, "%i %i", &x, &y); + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + memset(viz, false, sizeof(viz)); + + for (int i = 1;i <= n;++i) + + dfs(i); + + memset(viz, false, sizeof(viz)); + + while (!s.empty()) { + + if (!viz[s.top()]) { + + dfs_t(s.top()); + + ++nr; + + } + + s.pop(); + + } + + + + fprintf(fout,"%i\n", nr); + + for (int i = 0;i < nr;++i) { + + for (int x : res[i]) + + fprintf(fout,"%i ", x); + + fprintf(fout,"\n"); + + } + + + + return 0; + +} diff --git a/scc/kosaraju/699df61f-9a30-453b-b233-f3fc167c4233.cpp b/scc/kosaraju/699df61f-9a30-453b-b233-f3fc167c4233.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e27b0fa4e516fad19849311021609e91624e0ce --- /dev/null +++ b/scc/kosaraju/699df61f-9a30-453b-b233-f3fc167c4233.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int DMAX = 100005; + + + +unsigned int n, m, nrCTC; + +vector G[DMAX], GT[DMAX], CTC[DMAX]; + + + +stack stiva; + + + +unsigned int viz[DMAX]; + + + +void read() + +{ + + f >> n >> m; + + + + for (unsigned int i = 1; i <= m; i++) + + { + + unsigned int x, y; + + + + f >> x >> y; + + + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void dfs(unsigned int nod) + +{ + + viz[nod] = 1; + + + + for (auto vecin : G[nod]) + + { + + if (viz[vecin] == 0) + + dfs(vecin); + + } + + + + stiva.push(nod); + +} + + + +void dfs_T(unsigned int nod) + +{ + + viz[nod] = 2; + + CTC[nrCTC].push_back(nod); + + + + for (auto vecin : GT[nod]) + + { + + if (viz[vecin] == 1) + + dfs_T(vecin); + + } + +} + + + +void kosaraju() + +{ + + for (unsigned int i = 1; i <= n; i++) + + if (viz[i] == 0) + + dfs(i); + + + + while (!stiva.empty()) + + { + + unsigned int nod = stiva.top(); + + + + if (viz[nod] == 1) + + { + + nrCTC++; + + dfs_T(nod); + + } + + stiva.pop(); + + } + +} + + + +void show() + +{ + + g << nrCTC << '\n'; + + + + for (unsigned int i = 1; i <= nrCTC; i++) + + { + + for (auto nod : CTC[i]) + + g << nod << ' '; + + + + g << '\n'; + + } + +} + + + +int main() + +{ + + read(); + + kosaraju(); + + show(); + + + + return 0; + +} diff --git a/scc/kosaraju/6aff5ad3-c58b-480a-9b3c-d3a05be33a45.cpp b/scc/kosaraju/6aff5ad3-c58b-480a-9b3c-d3a05be33a45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3501ce4a7a64e489aebd8ff256703e9e62ae3e03 --- /dev/null +++ b/scc/kosaraju/6aff5ad3-c58b-480a-9b3c-d3a05be33a45.cpp @@ -0,0 +1,70 @@ +#include +#include + +using namespace std; + +int n, m, i, j, k; +bool v1[100005], v2[100005]; +vector a, b, G[100005], GT[100005], comp[100005]; + +void dfs1(int nod) { + int p; + + if(!v1[nod]) { + v1[nod] = true; + b.push_back(nod); + for(p = 0; p < G[nod].size(); p++) + if(!v1[G[nod][p]]) + dfs1(G[nod][p]); + } +} + +void dfs2(int nod) { + int p; + + if(!v2[nod]) { + v2[nod] = true; + comp[k].push_back(nod); + for(p = 0; p < GT[nod].size(); p++) + if(!v2[GT[nod][p]]) + dfs2(GT[nod][p]); + } +} + +int main() { + ifstream f("ctc.in"); + ofstream g("ctc.out"); + + f >> n >> m; + while(m) { + f >> i >> j; + + G[i].push_back(j); + GT[j].push_back(i); + m--; + } + f.close(); + + for(i = 1; i <= n; i++) + if(!v1[i]) { + dfs1(i); + for(j = b.size() - 1; j >= 0; j--) + a.push_back(b[j]); + b.clear(); + } + + k = 0; + for(i = a.size() - 1; i >= 0; i--) + if(!v2[a[i]]) { + k++; + dfs2(a[i]); + } + + g << k << '\n'; + for(i = 1; i <= k; i++, g << '\n') + for(j = 0; j < comp[i].size(); j++) + g << comp[i][j] << ' '; + g.close(); + + return 0; +} diff --git a/scc/kosaraju/6b49a39d-4124-4025-9631-27c1134284a8.cpp b/scc/kosaraju/6b49a39d-4124-4025-9631-27c1134284a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2f364ca1632c95c559512945a62f72d1b2f5756 --- /dev/null +++ b/scc/kosaraju/6b49a39d-4124-4025-9631-27c1134284a8.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int NMAX = 1e5+5; + +vector graf[NMAX], grafT[NMAX], sol[NMAX]; + +int n, m, cnt; + +vector postorder; + +bool vis[NMAX], visT[NMAX]; + + + +void dfs(int nod) { + + vis[nod] = 1; + + for(int i = 0; i < (int)graf[nod].size(); i++) { + + int next = graf[nod][i]; + + if(!vis[next]) { + + dfs(next); + + } + + } + + postorder.push_back(nod); + +} + + + +void dfsT(int nod) { + + visT[nod] = 1; + + sol[cnt].push_back(nod); + + for(int i = 0; i < (int)grafT[nod].size(); i++) { + + int next = grafT[nod][i]; + + if(!visT[next]) { + + dfsT(next); + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + while(m--) { + + int x, y; + + f >> x >> y; + + graf[x].push_back(y); + + grafT[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) { + + if(!vis[i]) { + + dfs(i); + + } + + } + + for(int i = postorder.size() - 1; i >= 0; i--) { + + if(!visT[postorder[i]]) { + + cnt++; + + dfsT(postorder[i]); + + } + + } + + g << cnt << '\n'; + + for(int i = 1; i <= cnt; i++) { + + for(int j = 0; j < (int)sol[i].size(); j++) { + + g << sol[i][j] << ' '; + + } + + g << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/6b7baa13-c0bd-450d-93e1-8e10f549c17d.cpp b/scc/kosaraju/6b7baa13-c0bd-450d-93e1-8e10f549c17d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cfc48c800f55010665bb79386fea647093de7680 --- /dev/null +++ b/scc/kosaraju/6b7baa13-c0bd-450d-93e1-8e10f549c17d.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector v[100005], v2[100005], sol[100005]; + +stack st; + + + +int n, m, d[100005], x, y, k, nr; + + + +void dfs1(int nod) { + + d[nod] = 1; + + for(int i = 0; i < v[nod].size(); i++) { + + if(!d[v[nod][i]]) dfs1(v[nod][i]); + + } + + st.push(nod); + +} + + + +void dfs2(int nod) { + + d[nod] = 1; + + sol[nr].push_back(nod); + + for(int i = 0; i < v2[nod].size(); i++) { + + if(!d[v2[nod][i]]) dfs2(v2[nod][i]); + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; i++) { + + fin >> x >> y; + + v[x].push_back(y); + + v2[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) { + + if(!d[i]) dfs1(i); + + } + + memset(d, 0, sizeof(d)); + + while(!st.empty()) { + + if(!d[st.top()]) { + + nr++; + + dfs2(st.top()); + + } + + st.pop(); + + } + + fout << nr << '\n'; + + for(int i = 1; i <= nr; i++) { + + for(int j = 0; j < sol[i].size(); j++) { + + fout << sol[i][j] << " "; + + } + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/6baab319-5770-4032-8658-921c3bbc0934.cpp b/scc/kosaraju/6baab319-5770-4032-8658-921c3bbc0934.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd21f1e1be6dd0c4c84d09299c49037f53812864 --- /dev/null +++ b/scc/kosaraju/6baab319-5770-4032-8658-921c3bbc0934.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graf1[100001], graf2[100001], componente[100001]; + +int vizitat[100001], nr, n, m; + +stack s; + + + +void citire() { + + int x, y; + + f >> n >> m; + + for(int i = 1; i <= m; i++) { + + f >> x >> y; + + graf1[x].push_back(y); + + graf2[y].push_back(x); + + } + +} + + + +int DFS1(int nod) { + + vizitat[nod] = 1; + + for(int i=0; i < graf1[nod].size(); i++) + + if(vizitat[graf1[nod][i]] == 0) + + DFS1(graf1[nod][i]); + + s.push(nod); + +} + + + +int DFS2(int nod) { + + vizitat[nod] = 1; + + componente[nr].push_back(nod); + + for(int i=0; i < graf2[nod].size(); i++) + + if(vizitat[graf2[nod][i]] == 0) + + DFS2(graf2[nod][i]); + +} + + + +void afisare() { + + g << nr << "\n"; + + for(int i = 1; i <= nr; i++) { + + for(int j = 0; j < componente[i].size(); j++) + + g << componente[i][j] <<" "; + + g << "\n"; + + } + +} + + + +int main() { + + citire(); + + for(int i = 1; i <= n; i++) + + if(vizitat[i] == 0) + + DFS1(i); + + memset(vizitat, 0, sizeof(vizitat)); + + while(s.empty() == 0) { + + if(vizitat[s.top()] == 0) { + + nr++; + + DFS2(s.top()); + + } + + s.pop(); + + } + + afisare(); + + return 0; + +} diff --git a/scc/kosaraju/6c48e21c-c5e3-44c9-a950-4fc7b93536a3.cpp b/scc/kosaraju/6c48e21c-c5e3-44c9-a950-4fc7b93536a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88ce83778f1f2bb13708a0b2b6fe559e26b5978e --- /dev/null +++ b/scc/kosaraju/6c48e21c-c5e3-44c9-a950-4fc7b93536a3.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N = 100005; + + + +int n, m; + +int cc[N]; + +vector g[N], gt[N], compConexa[N], ord; + +bool viz[N]; + + + +void dfs1(int x) + +{ + + viz[x] = true; + + for(int y : g[x]) + + if(!viz[y]) + + dfs1(y); + + + + ord.push_back(x); + +} + + + +void dfs2(int x, int ccx) + +{ + + cc[x] = ccx; + + compConexa[ccx].push_back(x); + + + + for(int y : gt[x]) + + if(!cc[y]) + + dfs2(y, ccx); + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 0; i < m; i++) + + { + + int x, y; + + fin >> x >> y; + + + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + dfs1(i); + + + + int cci = 1; + + for(int i = ord.size() - 1; i >= 0; i--) + + if(cc[ord[i]] == 0) + + dfs2(ord[i], cci++); + + + + fout << cci-1 << '\n'; + + for(int i = 1; i < cci; i++) + + { + + for(int x : compConexa[i]) + + fout << x << ' '; + + fout << '\n'; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/6c5529af-c893-4901-9562-20a44465342c.cpp b/scc/kosaraju/6c5529af-c893-4901-9562-20a44465342c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a62b9f558ab525fea1edebfed39764c26bf8078 --- /dev/null +++ b/scc/kosaraju/6c5529af-c893-4901-9562-20a44465342c.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vector G[100001]; + +vector GT[100001]; + +vector rezultat[100001]; + +stack < int > stiva; + +int n,m,a,b,viz[100001],nrctc; + +int dfs(int i){ + + viz[i]=1; + + for(auto j:G[i]){ + + if(viz[j]==0){ + + dfs(j); + + } + + } + + stiva.push(i); + +} + +int dfst(int i){ + + viz[i]=2; + + + + rezultat[nrctc].push_back(i); + + for(auto j:GT[i]){ + + if(viz[j]==1){ + + dfst(j); + + } + + } + + + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++){ + + f>>a>>b; + + G[a].push_back(b); + + GT[b].push_back(a); + + } + + + + for(int i=1;i<=n;i++){ + + if(viz[i]==0)dfs(i); + + } + + + + + + + + while(!stiva.empty()){ + + int i=stiva.top(); + + + + if(viz[i]==1){ + + nrctc++; + + dfst(i); + + } + + stiva.pop(); + + + + } + + g< +#include +#include + +using namespace std; + +ifstream in ("ctc.in"); +ofstream out ("ctc.out"); + +const int N=100001; +const int M=200001; + +int lst[N],lst_inv[N],urm[M],urm_inv[M],vf[M],vf_inv[M],nr,q[N]; +bool viz[N],viz_inv[N]; +vector comp[N]; + +void dfs (int nod) +{ + viz[nod]=true; + for (int p=lst[nod];p!=0;p=urm[p]) + { + int y=vf[p]; + if (!viz[y]) + dfs (y); + } + q[++nr]=nod; +} +void dfs_inv (int nod) +{ + viz_inv[nod]=true; + comp[nr].push_back(nod); + for (int p=lst_inv[nod];p!=0;p=urm_inv[p]) + { + int y=vf_inv[p]; + if (!viz_inv[y]) + dfs_inv(y); + } +} +void adauga (int x, int y) +{ + ++nr; + vf[nr]=y; + urm[nr]=lst[x]; + lst[x]=nr; + vf_inv[nr]=x; + urm_inv[nr]=lst_inv[y]; + lst_inv[y]=nr; +} +int main() +{ + int n,m,x,y; + in>>n>>m; + for (int i=1;i<=m;i++) + { + in>>x>>y; + adauga (x,y); + } + nr=0; + for (int i=1;i<=n;i++) + if (!viz[i]) dfs (i); + nr=0; + for (int i=n;i>0;i--) + if (!viz_inv[q[i]]) + { + nr++; + dfs_inv(q[i]); + } + out< +#include +#include +const int N_MAX = 100005; + +int no_conex, n, m; +std :: vector conex[N_MAX]; +int conex_nd[N_MAX]; +bool vis[N_MAX]; +std :: vector graph[N_MAX], trans[N_MAX]; +std :: vector sorted; +int ind; + +void dfs( int node) { + if(vis[node]) return; + + vis[node] = true; + for (int i : graph[node]) { + + dfs(i); + } + sorted.push_back(node); +} + +void dfs_t(int node) { + if (conex_nd[node]) return; + + conex_nd[node] = ind; + conex[ind].push_back(node); + + for( int i : trans[node]) { + dfs_t(i); + } +} + +int main() { + + std :: ifstream fin ("ctc.in"); + std :: ofstream fout ("ctc.out"); + + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + int a, b; + fin >> a >> b; + graph[a].push_back(b); + trans[b].push_back(a); + } + + for (int i = 1; i <= n; ++i) { + if (!vis[i]) { + dfs(i); + } + } + + + for (int i = n - 1; i >= 0; --i) { + if (!conex_nd[sorted[i]]) { + ind ++; + dfs_t(sorted[i]); + + } + } + fout << ind << "\n"; + for (int i = 1; i <= ind; ++i) { + for (int node: conex[i]) + fout << node << " "; + fout << '\n'; + } + return 0; +} diff --git a/scc/kosaraju/6d929919-25dd-45f5-9237-0704e6d40bda.cpp b/scc/kosaraju/6d929919-25dd-45f5-9237-0704e6d40bda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fae406a9f01babc8eafa38c6351f28d4b520ad97 --- /dev/null +++ b/scc/kosaraju/6d929919-25dd-45f5-9237-0704e6d40bda.cpp @@ -0,0 +1,146 @@ +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +struct node{ + + int v; + + node *next; + +}*L[100005], *LT[100005], *cpt[100005]; + + + +int N,M,comp; + +stack finalizare; + +bool parcurs1[100005], parcurs2[100005]; + + + +void add(int x, int y, node *L[]){ + + node *p = new node; + + p->v = y; p->next = L[x]; + + L[x] = p; + +} + + + +void citire(){ + + f>>N>>M; + + int x,y; + + for(int i=1; i<=M; i++){ + + f>>x>>y; + + add(x,y,L); + + add(y,x,LT); + + } + +} + + + +void dfs1(int i, bool parcurs[], node *L[]){ + + parcurs[i] = 1; + + for(node *p = L[i]; p != nullptr; p = p->next) + + if(parcurs[p->v] == 0) + + dfs1(p->v, parcurs, L); + + finalizare.push(i); + +} + + + +void dfs2(int i, bool parcurs[], node *L[]){ + + parcurs[i] = 1; + + for(node *p = L[i]; p != nullptr; p = p->next) + + if(parcurs[p->v] == 0) + + dfs2(p->v, parcurs, L); + + add(comp,i,cpt); + +} + + + +void tareConex(){ + + while(!finalizare.empty()){ + + if(parcurs2[finalizare.top()] == 0){ + + dfs2(finalizare.top(), parcurs2, LT); + + comp++; + + } + + finalizare.pop(); + + } + + g<v<<" "; + + cpt[i] = cpt[i]->next; + + } + + g<<"\n"; + + } + +} + + + +int main(){ + + citire(); + + for(int i=1; i<=N; i++) + + if(parcurs1[i] == 0) + + dfs1(i, parcurs1, L); + + tareConex(); + +} diff --git a/scc/kosaraju/6f0ea941-e06e-4a0c-841e-19e248666c93.cpp b/scc/kosaraju/6f0ea941-e06e-4a0c-841e-19e248666c93.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a98471b83ef379832b5fa14b4413cbb967745fa1 --- /dev/null +++ b/scc/kosaraju/6f0ea941-e06e-4a0c-841e-19e248666c93.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int NMAX = 1e5+5; + +vector graf[NMAX], grafT[NMAX], sol[NMAX]; + +int n, m, cnt; + +vector postorder; + +bool vis[NMAX], visT[NMAX]; + + + +void dfs(int nod) { + + vis[nod] = 1; + + for(int i = 0; i < (int)graf[nod].size(); i++) { + + int next = graf[nod][i]; + + if(!vis[next]) { + + dfs(next); + + } + + } + + postorder.push_back(nod); + +} + + + +void dfsT(int nod) { + + visT[nod] = 1; + + sol[cnt].push_back(nod); + + for(int i = 0; i < (int)grafT[nod].size(); i++) { + + int next = grafT[nod][i]; + + if(!visT[next]) { + + dfsT(next); + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + while(m--) { + + int x, y; + + f >> x >> y; + + graf[x].push_back(y); + + grafT[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) { + + if(!vis[i]) { + + dfs(i); + + } + + } + + for(int i = postorder.size() - 1; i >= 0; i--) { + + if(!visT[postorder[i]]) { + + cnt++; + + dfsT(postorder[i]); + + } + + } + + g << cnt << '\n'; + + for(int i = 1; i <= n; i++) { + + for(int j = 0; j < (int)sol[i].size(); j++) { + + g << sol[i][j] << ' '; + + } + + g << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/7047b6b2-857e-4d1e-88ba-9770e893e756.cpp b/scc/kosaraju/7047b6b2-857e-4d1e-88ba-9770e893e756.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14c76f7b4cbbc4f2d4db2a5171b79fd28dbef8ea --- /dev/null +++ b/scc/kosaraju/7047b6b2-857e-4d1e-88ba-9770e893e756.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("ctc.in"); ofstream cout("ctc.out"); + + + + + + + +const int maxn = 100005; + + + +vector gr[maxn], gr2[maxn]; + + + +int used[maxn]; + + + +vector ord, comp; + +vector > ans; + + + + + + + +void dfs(int nod){ + + used[nod] = 1; + + + + for (auto& x : gr[nod]){ + + if (!used[x]) dfs(x); + + } + + + + comp.push_back(nod); + +} + + + + + + + +int main() { + + + + int n, m; cin >> n >> m; + + + + for (int i = 1; i <= m; i++){ + + int a, b; cin >> a >> b; + + + + gr[a].push_back(b); + + gr2[b].push_back(a); + + } + + + + for (int i = 1; i <= n; i++){ + + if (!used[i]) dfs(i); + + for (auto& el : comp) ord.push_back(el); + + comp.clear(); + + } + + + + for (int i = 1; i <= n; i++){ + + used[i] = 0; + + gr[i] = gr2[i]; + + } + + + + reverse (ord.begin(), ord.end()); + + + + for (auto & i : ord){ + + if (!used[i]) dfs(i); + + if (!comp.empty()) ans.push_back(comp); + + comp.clear(); + + } + + + + cout << ans.size() << '\n'; + + for (auto& vec : ans){ + + for (auto& elem : vec){ + + cout << elem << ' '; + + } + + cout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/70ac541d-317f-481d-85a6-6472da0fbbfc.cpp b/scc/kosaraju/70ac541d-317f-481d-85a6-6472da0fbbfc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1979ff3e1e953712f1de9f61ff7d1ad9d4c7316 --- /dev/null +++ b/scc/kosaraju/70ac541d-317f-481d-85a6-6472da0fbbfc.cpp @@ -0,0 +1,165 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); +int n, contor; + + + +vector < int > g1[100010], g2[100010], sol[100010]; + +stack < int > stiva; + + + +bitset < 100010 > viz1; + +bitset < 100010 > viz2; + + + +void read() { + + int m; + + fin >> n >> m; + + while(m--) + + { + + int x, y; + + fin >> x >> y; + + g1[x].push_back(y); + + g2[y].push_back(x); + + } + +} + + + +void DFS(int nod) { + + viz1[nod] = 1; + + + + for(const auto it : g1[nod]) { + + if(viz1[it] == 0) { + + DFS(it); + + } + + } + + stiva.push(nod); + +} + + + +void dfs(int nod) { + + sol[contor].push_back(nod); + + viz2[nod] = 1; + + + + for(const auto it : g2[nod]) { + + if(viz2[it] == 0) { + + dfs(it); + + } + + } + +} + + + + + +void solve() { + + for(int i=1; i<=n; i++) { + + if(viz1[i] == 0) + + { + + DFS(i); + + } + + } + + + + while(!stiva.empty()) { + + int nod = stiva.top(); + + stiva.pop(); + + + + if(viz2[nod] == 0) { + + contor++; + + dfs(nod); + + } + + } + +} + + + +void afis() { + + fout << contor << "\n"; + + for(int i=1; i<=contor; i++) + + { + + for(const auto it : sol[i]) { + + fout << it << " "; + + } + + fout << "\n"; + + } + +} + + + +int main() { + + read(); + + solve(); + + afis(); + +} diff --git a/scc/kosaraju/70dbfd4c-9837-4ea6-b607-ef2df29ae5e3.cpp b/scc/kosaraju/70dbfd4c-9837-4ea6-b607-ef2df29ae5e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b9a4b8926fa921d16ba73cbd81a9e0966d234ef --- /dev/null +++ b/scc/kosaraju/70dbfd4c-9837-4ea6-b607-ef2df29ae5e3.cpp @@ -0,0 +1,140 @@ +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +list topoSort; + +vector < vector > la; + +vector < vector > li; + +vector < vector > componenta; + +int n,m,nrComponente; + +bool used[100002]; + + + +void citire() + +{ + + f >> n >> m; + + la.resize(n+1); + + li.resize(n+1); + + for(int i=1;i<=m;++i) + + { + + int x,y; + + f >> x >> y; + + la[x].push_back(y); + + li[y].push_back(x); + + } + +} + + + +void dfs(int x) + +{ + + used[x] = 1; + + for(int i : la[x]) + + if(!used[i])dfs(i); + + topoSort.push_front(x); + +} + + + +void puneInComponenta(int x,int nrComp) + +{ + + componenta[nrComp].push_back(x); + + used[x] = 1; + + for(int i : li[x]) + + if(!used[i]) + + puneInComponenta(i,nrComp); + +} + + + +void Kosaraju() + +{ + + for(int i=1;i<=n;++i) + + if(!used[i])dfs(i); + + + + memset(used,0,sizeof(used)); + + componenta.resize(n+1); + + + + for(int i : topoSort) + + if(!used[i])puneInComponenta(i,nrComponente++); + + + + componenta.resize(nrComponente+1); + + g << nrComponente << '\n'; + + for(vector i : componenta) + + { + + for(int j : i)g << j << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citire(); + + Kosaraju(); + +} diff --git a/scc/kosaraju/71567341-8c62-4fd6-a364-e3982a10cfb0.cpp b/scc/kosaraju/71567341-8c62-4fd6-a364-e3982a10cfb0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..697640ebc5b47556a6ce62aece3235fd57dd4530 --- /dev/null +++ b/scc/kosaraju/71567341-8c62-4fd6-a364-e3982a10cfb0.cpp @@ -0,0 +1,243 @@ +#include +#include + + +using namespace std; + + + +ifstream g("ctc.in"); + +ofstream o("ctc.out"); + +int nrc,n,m,n1,n2,viz[100003],stiva[100003],k,sol[2*100003],nr; + +struct nod{ + + + +int x; + +nod *urm; + + + +}; + + + +nod *L[100003]; + +nod *LT[100003]; + + + +void uneste(int i, int j) + +{ + + nod *p; + + p=new nod; + + p->x=j; + + p->urm=L[i]; + + L[i]=p; + +} + + + +void afisare_lista() + +{ + + nod *p; + + p=new nod; + + for(int i=1;i<=n;i++) + + { + + p=LT[i]; + + while(p) + + { + + o<x<<" "; + + p=p->urm; + + } + + o<<"\n"; + + } + +} + + + +void DFS(int nod_pornire) + +{ + + nod *p; + + p=new nod; + + p=L[nod_pornire]; + + viz[nod_pornire]=1; + + while(p) + + { + + if(viz[p->x]==0) + + DFS(p->x); + + p=p->urm; + + } + + stiva[++k]=nod_pornire; + +} + + + +void unesteT(int i, int j) + +{ + + nod *p; + + p=new nod; + + p->x=i; + + p->urm=LT[j]; + + LT[j]=p; + +} + + + +void DFST(int nod_pornire) + +{ + + sol[++nr]=nod_pornire; + + nod *p; + + p=new nod; + + p=LT[nod_pornire]; + + viz[nod_pornire]=1; + + while(p) + + { + + if(viz[p->x]==0) + + { + + DFST(p->x); + + } + + p=p->urm; + + } + +} + + + +int main() + +{ + + g>>n>>m; + + for(int i=1;i<=m;i++) + + { + + g>>n1>>n2; + + uneste(n1,n2); + + unesteT(n1,n2); + + } + + + + + + + + for(int i=1;i<=n;i++) + + if(viz[i]==0) + + DFS(i); + + + + for(int i=1;i<=n;i++) + + viz[i]=0; + + + + for(int i=k;i>=1;i--) + + { + + if(viz[stiva[i]]==0) + + { + + DFST(stiva[i]); + + nrc++; + + sol[++nr]=-1; + + } + + } + +o< + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const lim = 100001; + +int n,m,t; + +bool ver[lim],vr[lim]; + +vector < int > c[lim]; + +vector < int > c2[lim]; + +vector < int > st; + +vector < int > afis[lim]; + + + +void dfs(int nod) + +{ + + ver[nod]=1; + + + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + in>>x>>y; + + c[x].push_back(y); + + c2[y].push_back(x); + + } + + + + for(int i=1;i<=n;i++) + + if(ver[i]==0) + + { + + dfs(i); + + } + + + + for(int i=st.size()-1;i>=0;i--) + + if(vr[st[i]]==0) + + { + + t++; + + dfs2(st[i]); + + } + + + + out< // for memset +#include +#include +#include +#include + +using namespace std; + +ifstream in ("ctc.in"); +ofstream out("ctc.out"); + + +vector transposed_graph[100000]; +vector graph[100000]; +vector order; +bool visited[100000]; +int num_nodes,num_edges; + + +void read(){ + int a,b; + in>>num_nodes>>num_edges; + for(int i=0;i>a>>b; + graph[a-1].push_back(b-1); + transposed_graph[b-1].push_back(a-1); + + } +} + +void dfs1(int node){ + visited[node]=true; + for(int neighbour:graph[node]){ + if(!visited[neighbour]) + dfs1(neighbour); + } + order.push_back(node); +} + +void dfs2(int node,vector&to_add){ + visited[node]=true; + to_add.push_back(node); + for(int neighbour:transposed_graph[node]){ + if(!visited[neighbour]){ + dfs2(neighbour,to_add); + } + } +} +void solve(){ + + for(int i=0;i> components; + + for(int a:order){ + if(!visited[a]){ + vector queue; + dfs2(a,queue); + components.push_back(queue); + } + } + + out<&queue:components){ + for(int a:queue) + out< +#include + +using namespace std; + +const int NMAX = 200005; + +vector graf[NMAX]; +vector graf_t[NMAX]; +vector ordered_list; +vector comp[NMAX]; + +int comp_nod[NMAX]; +int viz[NMAX]; + +int nrcomp; + +void dfs1(int nod) { + viz[nod] = 1; + + for (auto &vec : graf[nod]) { + if (!viz[vec]) { + dfs1(vec); + } + } + ordered_list.push_back(nod); +} + +void dfs2(int nod, int index) { + comp_nod[nod] = index; + comp[index].push_back(nod); + + for (auto &vec : graf_t[nod]) { + if (!comp_nod[vec]) { + dfs2(vec, index); + } + } +} + +int main() { + ifstream fin("ctc.in"); + ofstream fout("ctc.out"); + + int a,b,m,n; + fin >> n >> m; + for(int i = 0; i < m; i++) { + fin >> a >> b; + graf[a].push_back(b); + graf_t[b].push_back(a); + } + + for(int i = 1; i <= n; i++) { + if (!viz[i]) { + dfs1(i); + } + } + + for(int i = n - 1; i >= 0; i--) { + if (!comp_nod[ordered_list[i]]) { + dfs2(ordered_list[i], ++nrcomp); + } + } + + fout << nrcomp << "\n"; + for (int i = 1; i <= nrcomp; i++) { + for (auto &nod : comp[i]) { + fout << nod << " "; + } + fout << "\n"; + } + + + fin.close(); + fout.close(); + return 0; +} diff --git a/scc/kosaraju/72ac1d0e-8823-4044-96f0-1b348c10f240.cpp b/scc/kosaraju/72ac1d0e-8823-4044-96f0-1b348c10f240.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d32f3c99aca35a333e3e62792b2df7cb0ad78771 --- /dev/null +++ b/scc/kosaraju/72ac1d0e-8823-4044-96f0-1b348c10f240.cpp @@ -0,0 +1,90 @@ +#include +#include + +using namespace std; + +const int N = 100005, M = 200005; +int n, m, vf[M], urm[M], lst[N], q[N], vf2[M], urm2[M], lst2[N], nr, cnt, st[N], st2[N], nvf[N]; +bool viz[N], viz2[N]; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +void adauga(int a, int b) +{ + vf[++nr] = b; + urm[nr] = lst[a]; + lst[a] = nr; + vf2[nr] = a; + urm2[nr] = lst2[b]; + lst2[b] = nr; +} + +void dfs(int x) +{ + viz[x] = true; + for(int p = lst[x]; p != 0; p = urm[p]) + { + int y = vf[p]; + if(!viz[y]) + { + dfs(y); + } + } + st[++nr] = x; +} + +void dfs2(int x) +{ + viz2[x] = true; + nvf[cnt]++; + st2[++nr] = x; + for(int p = lst2[x]; p != 0; p = urm2[p]) + { + int y = vf2[p]; + if(!viz2[y]) + { + dfs2(y); + } + } +} + +int main() +{ + int x, y; + in >> n >> m; + for(int i = 0; i < m; i++) + { + in >> x >> y; + adauga(x, y); + } + nr = 0; + for(int i = 1; i <= n; i++) + { + if(viz[i] == 0) + { + dfs(i); + } + } + nr = 0; + cnt = 0; + for(int i = n; i >= 1; i--) + { + if(!viz2[st[i]]) + { + cnt++; + dfs2(st[i]); + } + } + out << cnt << "\n"; + int j = 1; + for (int i = 1; i <= cnt; i++) + { + for (int k = 0; k < nvf[i]; k++) + { + out << st2[j++] << " "; + } + out << "\n"; + } + return 0; +} diff --git a/scc/kosaraju/7468af74-2e18-4fd9-8428-5126c4bd57bf.cpp b/scc/kosaraju/7468af74-2e18-4fd9-8428-5126c4bd57bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a8a1665644cf3668d520461d9a5fb52dd011055e --- /dev/null +++ b/scc/kosaraju/7468af74-2e18-4fd9-8428-5126c4bd57bf.cpp @@ -0,0 +1,155 @@ +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + + + +int n, m, q; + +int k, DUD[100006]; + +bool viz[100001]; + + + +vector V1[100001]; + +vector V2[100001]; + +vector ctc[100001]; + + + + + + + +void DFS1(int nod){ + + + + viz[nod] = 1; + + + + for(auto it : V1[nod]) + + if(!viz[it]) + + DFS1(it); + + + + DUD[++k] = nod; + +} + + + + + + + +void DFS2(int nod){ + + + + viz[nod] = 0; + + ctc[q].push_back(nod); + + + + for(auto it : V2[nod]) + + if(viz[it] == 1) + + DFS2(it); + +} + + + + + + + +int main(){ + + + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++){ + + + + int x, y; + + fin >> x >> y; + + + + V1[x].push_back(y); + + V2[y].push_back(x); + + } + + + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS1(i); + + + + for(int i = n; i >= 1; i--) + + if(viz[DUD[i]] == 1){ + + q++; + + DFS2(DUD[i]); + + } + + + + fout << q << '\n'; + + + + for(int i = 1; i <= q; i++){ + + + + for(auto it : ctc[i]) + + fout << it << ' '; + + + + fout << '\n'; + + } + + + + return 0; + + + +} diff --git a/scc/kosaraju/74ed82d8-f8ce-4a47-a2ce-ba5081fa1dd5.cpp b/scc/kosaraju/74ed82d8-f8ce-4a47-a2ce-ba5081fa1dd5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..682f1c6c1f75cd8760696f5ee49111e8fbbb161f --- /dev/null +++ b/scc/kosaraju/74ed82d8-f8ce-4a47-a2ce-ba5081fa1dd5.cpp @@ -0,0 +1,160 @@ +#include +const int NMAX = 100000; + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +struct nod { + + int x; + + nod* next; + +}; + + + +nod* G[NMAX + 1], + + * GT[NMAX + 1], + + * CTC[NMAX + 1]; + + + +int N, M, nrc, nr, post[NMAX + 1]; + +bool viz[NMAX + 1]; + +void add(nod*& cap_lst, int nod_add) { + + nod* p; + + p = new nod; + + p->x = nod_add; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() { + + f >> N >> M; + + while (M--) { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf){ + + viz[vf] = 1; + + for (nod* p = G[vf]; p != NULL; p = p->next) + + if (!viz[p->x]) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) { + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for (nod* p = GT[vf]; p != NULL; p = p->next) + + if (viz[p->x]) + + DFSt(p->x); + +} + + + + + +void componentectc() + +{ + + int i; + + for (int i = 1; i <= N; i++) + + if (viz[i] == 0) + + DFS(i); + + + + for(i=N;i>=1;i--) + + if (viz[post[i]] == 1) { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() { + + g << nrc << '\n'; + + for (int i = 1; i <= nrc; i++) { + + for (nod* p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citireGraf(); + + componentectc(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/751fe8ee-b9ed-4c1a-ac7d-faa61d231693.cpp b/scc/kosaraju/751fe8ee-b9ed-4c1a-ac7d-faa61d231693.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d4b07822eccc891d86399b55629d56be5ac99c --- /dev/null +++ b/scc/kosaraju/751fe8ee-b9ed-4c1a-ac7d-faa61d231693.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int vizitat[100001], nrCompTareConexe; + +vector listaMuchii[100001], listaMuchiiTranspusa[100001]; + +vector componenteTareConexe[100005]; + +stack stiva; + +void dfs(int nod) + +{ + + int vecin; + + vizitat[nod] = 1; + + + + for (int i = 0; i < listaMuchii[nod].size(); i++) + + { + + vecin = listaMuchii[nod][i]; + + if (!vizitat[vecin]) + + dfs(vecin); + + } + + stiva.push(nod); + +} + +void dfs_transpus(int nod) + +{ + + int vecin; + + vizitat[nod] = 2; + + componenteTareConexe[nrCompTareConexe].push_back(nod); + + for (int i = 0; i < listaMuchiiTranspusa[nod].size(); i++) + + { + + vecin = listaMuchiiTranspusa[nod][i]; + + + + if (vizitat[vecin] == 1) + + dfs_transpus(vecin); + + } + +} + +int main() + +{ + + int x, y, i, noduri, muchii, nodCurent; + + fin >> noduri >> muchii; + + + + for (i = 1; i <= muchii; i++) + + { + + fin >> x >> y; + + listaMuchii[x].push_back(y); + + listaMuchiiTranspusa[y].push_back(x); + + } + + + + for (i = 1; i <= noduri; i++) + + { + + if (vizitat[i] == 0) + + dfs(i); + + } + + + + while (!stiva.empty()) + + { + + + + nodCurent = stiva.top(); + + + + + + if (vizitat[nodCurent] == 1) + + { + + nrCompTareConexe++; + + dfs_transpus(nodCurent); + + } + + stiva.pop(); + + } + + fout << nrCompTareConexe << '\n'; + + for (i = 1; i <= nrCompTareConexe; i++) + + { + + for (int j = 0; j < componenteTareConexe[i].size(); j++) + + { + + fout << componenteTareConexe[i][j] << " "; + + } + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/76066b87-dfa4-4965-8d2c-2a86fb474d17.cpp b/scc/kosaraju/76066b87-dfa4-4965-8d2c-2a86fb474d17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d36d1361873f5cbdb46e6b049257f1f1b695554b --- /dev/null +++ b/scc/kosaraju/76066b87-dfa4-4965-8d2c-2a86fb474d17.cpp @@ -0,0 +1,129 @@ +#include +#include + + + + +using namespace std; + +ifstream fi("ctc.in"); + +ofstream fo("ctc.out"); + +struct nod{ + + int x; + + nod*urm; + + }; + + nod *L[100003],*LT[100003],*ctc[100003]; + + nod*p; + + int viz[100003],vizt[100003],i,j,k,n,m,x,y; + + int st[100003],nc; + +void Adauga(nod*V[],int i,int j){ + + nod*p; + + p=new nod; + + p->x=j; + + + + p->urm=V[i]; + + V[i]=p; + + } + +void dfs(int j){ + + nod *p; + + viz[j]=1; + + p=L[j]; + + while(p){ + + if(viz[p->x]==0)dfs(p->x); + + p=p->urm; + + } + + st[++k]=j; + +} + +void dfst(int j){ + + nod *p; + + Adauga(ctc,nc,j); + + vizt[j]=1; + + p=LT[j]; + + while(p){ + + if(vizt[p->x]==0)dfst(p->x); + + p=p->urm; + + } + +} + +int main(){ + + fi>>n>>m; + + for(i=1;i<=m;i++){ + + fi>>x>>y; + + Adauga(L,x,y); + + Adauga(LT,y,x); + + } + + for(i=1;i<=n;i++) + + if(viz[i]==0)dfs(i); + + for(i=k;i>=1;i--) + + if(vizt[st[i]]==0) + + {nc++;dfst(st[i]);} + + fo<x<<" "; + + p=p->urm; + + } + + fo<<'\n'; + + } + +return 0; + +} diff --git a/scc/kosaraju/7768a8dc-f6e7-42ea-8930-9affedc28b70.cpp b/scc/kosaraju/7768a8dc-f6e7-42ea-8930-9affedc28b70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..862b1afa44e8372ce1f4ac796418de9dd64c9a8a --- /dev/null +++ b/scc/kosaraju/7768a8dc-f6e7-42ea-8930-9affedc28b70.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector A[100005],TA[100005],CTC[100005]; + +stack S; + + + +int N,M,cate_conexe; + + + +int VIZ[100005],TVIZ[100005]; + + + +void df (int x) + +{ + + VIZ[x]=1; + + vector :: iterator it; + + for (it=A[x].begin();it!=A[x].end();++it) + + { + + int val=(*it); + + if (VIZ[val]==0) + + { + + df(val); + + } + + } + + S.push(x); + +} + + + +void tdf (int x) + +{ + + TVIZ[x]=1; + + CTC[cate_conexe].push_back(x); + + vector :: iterator it; + + for (it=TA[x].begin();it!=TA[x].end();++it) + + { + + int val=(*it); + + if (TVIZ[val]==0) + + { + + tdf(val); + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + int a,b,i; + + for (i=1;i<=M;++i) + + { + + fin >> a >> b; + + A[a].push_back(b); + + TA[b].push_back(a); + + } + + for (i=1;i<=N;++i) + + { + + if (VIZ[i]==0) + + { + + df(i); + + } + + } + while (!S.empty()) + + { + + i=S.top(); + + S.pop(); + + if (TVIZ[i]==0) + + { + + ++cate_conexe; + + tdf(i); + + } + + } + + fout << cate_conexe << '\n'; + + for (i=1;i<=cate_conexe;++i) + + { + + vector :: iterator it; + + for (it=CTC[i].begin();it!=CTC[i].end();++it) + + { + + int val=(*it); + + fout << val << " "; + + } + + fout << '\n'; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/778eb02a-e9b4-408a-aebc-1623a24d3cff.cpp b/scc/kosaraju/778eb02a-e9b4-408a-aebc-1623a24d3cff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78a400b8648dda0faaa69e9dc508462ab44354f3 --- /dev/null +++ b/scc/kosaraju/778eb02a-e9b4-408a-aebc-1623a24d3cff.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector nodes[100005]; + +vector nodes2[100005]; + +vector ctc[100005]; + +stack < int > st; + +int viz[100005], n, m; + +void dfs(int node){ + + viz[node] = 1; + + for (auto &it : nodes[node]) { + + if(viz[it] != 1){ + + dfs(it); + + } + + } + + st.push(node); + +} + +void dfst(int node,int nr){ + + viz[node] = 2; + + ctc[nr].push_back(node); + + for (auto &it : nodes2[node]) { + + if(viz[it]!= 2){ + + dfst(it,nr); + + } + + } + + + +} + +int main() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; ++i) { + + int x, y; + + fin >> x >> y; + + nodes[x].push_back(y); + + nodes2[y].push_back(x); + + } + + for (int i = 1; i <= n; ++i) { + + if(!viz[i]){ + + dfs(i); + + } + + } + + int nr = 0; + + while(!st.empty()) + + { + + int top = st.top(); + + st.pop(); + + if(viz[top] != 2) + + { + + nr++; + + dfst(top,nr); + + } + + } + + fout << nr << "\n"; + + for(int i = 1; i <= nr; i++) + + { + + for(auto &v : ctc[i]) + + fout << v << " "; + + fout << "\n"; + + } + + + + + + + + return 0; + +} diff --git a/scc/kosaraju/793b02df-3a25-4e09-9213-3b98a167838a.cpp b/scc/kosaraju/793b02df-3a25-4e09-9213-3b98a167838a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6a7434a22410d3a6c6d8ae12d342063776b5a2c --- /dev/null +++ b/scc/kosaraju/793b02df-3a25-4e09-9213-3b98a167838a.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include + + +using namespace std; + +int N, M, viz[100001], comp; + +vector l[100001],lT[100001],rez[100001]; + +stack s; + + + +void DFS(int nod) + +{ + + viz[nod] = 1; + + for (int i = 0; i < l[nod].size(); i++) + + { + + if (viz[l[nod][i]] == 0) + + DFS(l[nod][i]); + + } + + s.push(nod); + +} + + + +void DFS2(int nod) + +{ + + viz[nod] = 1; + + rez[comp].push_back(nod); + + + + for (int i = 0; i < lT[nod].size(); i++) + + if (viz[lT[nod][i]] == 0) + + DFS2(lT[nod][i]); + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + int i, j, k; + + + + fin >> N >> M; + + for (i = 0; i < M; i++) + + { + + fin >> j >> k; + + l[j].push_back(k); + + lT[k].push_back(j); + + } + + + + for (i = 1; i <= N; i++) + + { + + if (viz[i] == 0) + + DFS(i); + + } + + + + for (i = 1; i <= N; i++) + + viz[i] = 0; + + + + while(!s.empty()) + + { + + int nod = s.top(); + + if(viz[nod] == 0) + + { + + comp++; + + DFS2(nod); + + } + + s.pop(); + + } + + + + fout << comp << "\n"; + + for (i = 1; i <= comp; i++) + + { + + for (j = 0; j < rez[i].size(); j++) + + fout << rez[i][j] << " "; + + fout << "\n"; + + } + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/scc/kosaraju/794f0abf-21fe-4937-afc7-19f48977ccff.cpp b/scc/kosaraju/794f0abf-21fe-4937-afc7-19f48977ccff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b65a51fa8e5b0ad80603310d61d7da3ef2856fe3 --- /dev/null +++ b/scc/kosaraju/794f0abf-21fe-4937-afc7-19f48977ccff.cpp @@ -0,0 +1,185 @@ +#include + + +using namespace std; + + + +ifstream fin( "ctc.in" ); + +ofstream fout( "ctc.out" ); + + + +const int NMAX = 100005; + + + +int N, M; + +vector Ad[NMAX]; + +vector Adrev[NMAX]; + + + +vector t_sort; + +bool v[NMAX]; + + + +vector < vector > sol; + +vector stk; + + + +void Read() { + + fin >> N >> M; + + + + int a, b; + + for( int i = 1; i <= M; ++i ) { + + fin >> a >> b; + + + + Ad[a].push_back( b ); + + Adrev[b].push_back( a ); + + } + +} + + + +void T_dfs( int nod ) { + + v[nod] = true; + + + + int w; + + for( int i = 0; i < Ad[nod].size(); ++i ) { + + w = Ad[nod][i]; + + + + if( !v[w] ) T_dfs( w ); + + } + + + + t_sort.push_back( nod ); + +} + + + +void TopoSort() { + + for( int i = 1; i <= N; ++i ) + + if( !v[i] ) T_dfs( i ); + +} + + + +void Dfs2( int nod ) { + + stk.push_back( nod ); + + v[nod] = true; + + + + int w; + + for( int i = 0; i < Adrev[nod].size(); ++i ) { + + w = Adrev[nod][i]; + + + + if( !v[w] ) Dfs2( w ); + + } + +} + + + +void Do() { + + TopoSort(); + + + + for( int i = 1; i <= N; ++i ) + + v[i] = false; + + + + for( int i = t_sort.size() - 1; i >= 0; --i ) { + + int u = t_sort[i]; + + + + if( v[u] ) continue; + + + + stk.clear(); + + + + Dfs2( u ); + + + + sol.push_back( stk ); + + } + fout << sol.size() << '\n'; + + + + for( int i = 0; i < sol.size(); ++i ) { + + for( int j = 0; j < sol[i].size(); ++j ) + + fout << sol[i][j] << ' '; + + fout << '\n'; + + } + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/scc/kosaraju/7a44e456-5303-49e5-a3f5-84b53cca392b.cpp b/scc/kosaraju/7a44e456-5303-49e5-a3f5-84b53cca392b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0f019aeca0c566b4bdc6f72fdffbd4fb2046e40 --- /dev/null +++ b/scc/kosaraju/7a44e456-5303-49e5-a3f5-84b53cca392b.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +const int N_MAX = 100002; + + + +int n, m; + + + +vector outEdges[N_MAX]; + +vector inEdges[N_MAX]; + + + +int order[N_MAX]; + +int currentIndex; + + + +bool visited[N_MAX]; + + + +void dfs (int u) + +{ + + visited[u] = true; + + for(int v : outEdges[u]) + + if(visited[v] == false) + + dfs(v); + + order[++currentIndex] = u; + +} + + + +int cntSCC; + +vector SCC[N_MAX]; + + + +void dfsSCC (int u) + +{ + + visited[u] = true; + + SCC[cntSCC].push_back(u); + + for(int v : inEdges[u]) + + if(visited[v] == false) + + dfsSCC(v); + +} + + + + + +int main() + +{ + + ifstream fin ("ctc.in"); + + ofstream fout ("ctc.out"); + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + int u, v; + + fin >> u >> v; + + outEdges[u].push_back(v); + + inEdges[v].push_back(u); + + } + + for(int i = 1; i <= n; i++) + + if(visited[i] == false) + + dfs(i); + + memset(visited, false, sizeof(visited)); + + for(int i = n; i >= 1; i--) + + if(visited[order[i]] == false) + + { + + cntSCC++; + + dfsSCC(order[i]); + + } + + fout << cntSCC << "\n"; + + for(int i = 1; i <= cntSCC; i++, fout << "\n") + + for(int u : SCC[i]) + + fout << u << " "; + + return 0; + +} diff --git a/scc/kosaraju/7ad959a9-1466-41b1-9f67-21a19a2e3569.cpp b/scc/kosaraju/7ad959a9-1466-41b1-9f67-21a19a2e3569.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a155838fc6ca8851cbd15a62bba96a53c0c880fd --- /dev/null +++ b/scc/kosaraju/7ad959a9-1466-41b1-9f67-21a19a2e3569.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + + +int num_nodes,num_edges; +vector graph[100000]; +vector transposed[100000]; +vector order; +bool visited[100000]; + +void read(){ + in>>num_nodes>>num_edges; + int a,b; + + for(int i=0;i>a>>b; + graph[a-1].push_back(b-1); + transposed[b-1].push_back(a-1); + } +} + + +void dfs1(int node){ + visited[node]=true; + for(int i=0;i&to_add){ + visited[node]=true; + to_add.push_back(node); + for(int i=0;i> result; + for(int i=order.size()-1;i>=0;i--){ + int node=order[i]; + if(!visited[node]){ + vector results; + dfs2(node,results); + result.push_back(results); + } + } + out<&a:result){ + for(int p:a) + out< +#include +#include +#include +#include +#include + + + + +using std::cin; +using std::cout; + +template +struct Node { + T node; + Node *next; + C weight; +}; + +template +class Graph { +public: + size_t size; + + void add(const T& x, const T& y) { + _nodes[++size].node = y; + _nodes[size].next = _list[x]; + _list[x] = &_nodes[size]; + } + + void add_weighted(const T& x, const T& y, const T& weight) { + _nodes[++size].node = y; + _nodes[size].weight = weight; + _nodes[size].next = _list[x]; + _list[x] = &_nodes[size]; + } + + Node& get_node(const T& x) { + return *_list[x]; + } + + bool visited(const T& x) { + return _visited[x]; + } + + void visit(const T& x) { + _visited[x] = true; + } + + void reset() { + for (auto &it : _visited) it = false; + } +private: + std::array, M> _nodes; + std::array*, N> _list; + + std::array _visited; +}; + +std::vector s; + +template +void top_sort(Graph& g, const T& x, Container& c) { + g.visit(x); + + Node* p = &g.get_node(x); + + while (p) { + if (!g.visited(p->node)) + top_sort(g, p->node, c); + p = p->next; + } + + c.push_back(x); +} + +Graph g, d; +std::vector> f; + +int main(int argc, char const *argv[]) +{ + std::ifstream fin("ctc.in"); + std::ofstream fout("ctc.out"); + + int n, m, x, y; + + fin >> n >> m; + for (int i = 1 ; i <= m ; i++) { + fin >> x >> y; + g.add(x, y); + d.add(y, x); + } + + for (int i = 1 ; i <= n ; i++) + if (!g.visited(i)) top_sort(g, i, s); + + for (auto i = s.rbegin() ; i != s.rend() ; i++) { + if (!d.visited(*i)) { + f.push_back(std::vector()); + top_sort(d, *i, f.back()); + } + } + + fout << f.size() << '\n'; + for (const auto& it : f) { + for (const auto& vf : it) + fout << vf << ' '; + fout << '\n'; + } + return 0; +} diff --git a/scc/kosaraju/7bf52e60-f479-4e6e-bd00-37ad8b5f2bfc.cpp b/scc/kosaraju/7bf52e60-f479-4e6e-bd00-37ad8b5f2bfc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5af98f405a8c2a105865c7bcaa94ef1243f397b --- /dev/null +++ b/scc/kosaraju/7bf52e60-f479-4e6e-bd00-37ad8b5f2bfc.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +int n,m,x,y,ans; + +bool acc[100010]; + +vector nn[100010],nt[100010],cc[100010],vn; + + + +void dfs(int nod,vector nd[100010],vector &ca){ + + acc[nod]=1; + + for(auto i:nd[nod]) + + if(!acc[i]){ + + dfs(i,nd,ca); + + } + + ca.push_back(nod); + +} + + + +int main() + +{ + + ifstream f ("ctc.in"); + + ofstream g ("ctc.out"); + + f>>n>>m; + + for(int i=1;i<=m;i++) + + f>>x>>y, + + nn[x].push_back(y), + + nt[y].push_back(x); + + for(int i=1;i<=n;i++) + + if(!acc[i])dfs(i,nn,vn); + + for(int i=1;i<=n;i++)acc[i]=0; + + for(auto i:vn) cout<=1;i--) + + if(!acc[vn[i-1]])dfs(vn[i-1],nt,cc[++ans]); + + g< +#include +#include + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +int n,m,i,j,x,y,c; + +int viz[100002]; + +vector < int > gf[100002],gft[100002]; + +vector < int > v[100002]; + +stack < int > q; + +void dfs(int nod){ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + gf[x].push_back(y); + + gft[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(viz[i]==0) + + dfs(i); + + while(!q.empty()){ + + int x; + + x=q.top(); + + if(viz[x]==1){ + + c++; + + dfst(x); + + } + + q.pop(); + + } + + g< +#include +#include +#include + +using namespace std; +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); +int N, M, nr_ctc; +vector> gr, grt; +vector passed; +vector ctc[100005]; +stack L; +void read() { + int x, y; + fin >> N >> M; + gr.resize(N + 1); + grt.resize(N + 1); + for (int i = 0; i < M; ++i) { + fin >> x >> y; + gr[x].push_back(y); + grt[y].push_back(x); + } +} +void normalDfs(int nod) { + passed[nod] = true; + for (int i : gr[nod]) { + if (passed[i] == false) { + normalDfs(i); + } + } + L.push(nod); +} +void inverseDfs(int nod) { + ctc[nr_ctc].push_back(nod); + passed[nod] = true; + for (int i : grt[nod]) { + if (passed[i] == false) { + inverseDfs(i); + } + } +} +void print() { + fout << nr_ctc << "\n"; + for (int i = 0; i < nr_ctc; ++i) { + for (int j = 0; j < ctc[i].size(); ++j) { + fout << ctc[i][j] << " "; + } + fout << "\n"; + } +} +int main() +{ + read(); + passed.resize(N + 1, false); + for (int i = 1; i <= N; ++i) { + if (passed[i] == false) { + normalDfs(i); + } + } + passed.clear(); + passed.resize(N + 1, false); + int node; + while (!L.empty()) { + node = L.top(); + if (passed[node] == false) { + inverseDfs(node); + nr_ctc++; + } + L.pop(); + } + print(); + return 0; +} diff --git a/scc/kosaraju/814f3086-0ef8-4e04-b02a-96a432ed638d.cpp b/scc/kosaraju/814f3086-0ef8-4e04-b02a-96a432ed638d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b88e504a9239dcc50763a49e9f80f810ec0a0fc --- /dev/null +++ b/scc/kosaraju/814f3086-0ef8-4e04-b02a-96a432ed638d.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector orderedbyf; + + + +void dfs(int start, list ad[],int d[]) + +{ + + + + d[start] = 1; + + for(auto &child : ad[start]) + + { + + if(d[child] == 0) + + dfs(child,ad,d); + + + + } + + orderedbyf.push_back(start); + + + +} + +void dfst(int start, list adt[],int dt[],int nrComp, list scc[]) + +{ + + + + + + dt[start] = 1; + + scc[nrComp].push_back(start); + + for(auto &child : adt[start]) + + { + + if(dt[child] == 0) + + dfst(child,adt,dt,nrComp,scc); + + + + } + + + +} + +int main() + +{ + + int n,m,start; + + fin>>n>>m; + + list ad[n+1]; + + list adt[n + 1]; + + for(int i = 0; i < m; i ++) + + { + + + + int x,y; + + fin >> x >> y; + + ad[x].push_back(y); + + adt[y].push_back(x); + + + + + + } + + int d[n+1]= {0}; + + int dt[n+1] = {0}; + + list scc [n+1]; + + + + for(int i=1; i<=n; i++) + + if(d[i] == 0) + + { + + start = i; + + dfs(start,ad,d); + + } + int nrComp = 0; + + for(int i = n - 1; i >= 0; i--) + + if(dt[orderedbyf[i]] == 0) + + { + + + + nrComp ++; + + dfst(orderedbyf[i],adt,dt,nrComp,scc); + + } + + + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +int n, m, soll; +int used[100008]; +vector g1[100008],g2[100008],sol[100008]; +stack s; + +void dfs(int nod){ + used[nod] = 1; + for(int i = 0; i < g1[nod].size(); i++) + if(!used[g1[nod][i]]) + dfs(g1[nod][i]); + s.push(nod); +} +void dfs2(int nod, int nr){ + sol[nr].push_back(nod); + used[nod] = 1; + for(int i = 0; i < g2[nod].size(); i++) + if(!used[g2[nod][i]]) + dfs2(g2[nod][i], nr); +} + +void Kosarju(){ + for(int i = 1; i <= n; i++) + if(!used[i]) + dfs(i); + + memset(used,0,sizeof(used)); + + for(int i = 1; i <= n; i++){ + if(!used[s.top()]){ + soll++; + dfs2(s.top(),soll); + } + s.pop(); + } + out << soll <<'\n'; + for(int i = 1; i <= soll; i++){ + for(int j = 0; j < sol[i].size(); j++) + out << sol[i][j] <<" "; + out << '\n'; + } +} + +int main(){ + in >> n >> m; + for(int i = 1; i <= m; i++){ + int x, y; + in >> x >> y; + g1[x].push_back(y); + g2[y].push_back(x); + } + Kosarju(); + return 0; +} diff --git a/scc/kosaraju/81e1092c-23bb-4d62-9cfe-80826ecfd158.cpp b/scc/kosaraju/81e1092c-23bb-4d62-9cfe-80826ecfd158.cpp new file mode 100644 index 0000000000000000000000000000000000000000..734aa109bab4f5ef0e153f13f4c15082d2a6f888 --- /dev/null +++ b/scc/kosaraju/81e1092c-23bb-4d62-9cfe-80826ecfd158.cpp @@ -0,0 +1,194 @@ +#include +class Task { + + int n, m; + + std::vector< std::vector> adj; + + std::vector< std::vector> adj_t; + + std::vector t_out; + + std::vector< std::vector> solutie; + + std::vector vizitat; + + int pas = 1; + + + + public: + + void solve() { + + read(); + + fa(); + + print(); + + } + + + + private: + + void read() { + + std::ifstream fin("ctc.in"); + + fin >> n >> m; + + vizitat = std::vector(n+1, 0); + + adj.resize(n + 1); + + adj_t.resize(n + 1); + + int x, y; + + for (int i = 0; i < m; ++i) { + + fin >> x >> y; + + adj[x].push_back(y); + + adj_t[y].push_back(x); + + } + + fin.close(); + + } + + + + void fa() { + + sortare_timp_iesire(); + + + + parcurge_transpusa(); + + } + + + + + + void sortare_timp_iesire() { + + for (int i = 1; i <= n; ++i) { + + if (vizitat[i] == 0) { + + dfs(i); + + } + + } + + } + + + + void dfs(int nod) { + + vizitat[nod] = 1; + + for (auto &x : adj[nod]) { + + if (vizitat[x] == 0) { + + dfs(x); + + } + + } + + t_out.push_back(nod); + + } + + + + + + void parcurge_transpusa() { + + for (auto x = t_out.end() - 1; x >= t_out.begin(); --x) { + + if (vizitat[*x] == 1) { + + std::vector rand_solutie; + + dfs_transpusa(*x, rand_solutie); + + solutie.push_back(rand_solutie); + + } + + } + + } + + + + void dfs_transpusa(int nod, std::vector &rand_solutie) { + + vizitat[nod] = 0; + + rand_solutie.push_back(nod); + + for (auto &x : adj_t[nod]) { + + if (vizitat[x] == 1) { + + dfs_transpusa(x, rand_solutie); + + } + + } + + } + + + + void print() { + + std::ofstream fout ("ctc.out"); + + fout << solutie.size() << "\n"; + + for (auto &x : solutie) { + + for (auto &y : x) { + + fout << y << " "; + + } + + fout << "\n"; + + } + + fout.close(); + + } + +}; + + + +int main() { + + Task *t = new Task(); + + t->solve(); + + delete t; + + return 0; + +} diff --git a/scc/kosaraju/825a4375-d4b7-44cc-9be8-8789b40d3dc6.cpp b/scc/kosaraju/825a4375-d4b7-44cc-9be8-8789b40d3dc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e81aa3273c527c5c6f876cd8be10dacf43f7ff5 --- /dev/null +++ b/scc/kosaraju/825a4375-d4b7-44cc-9be8-8789b40d3dc6.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +const int lim=1e5+4; + +vector vec[lim]; + +vector rev[lim]; + +stack st; + +bool ok[lim]; + +bool df(int nod) + +{ + + ok[nod]=true; + + for(int x:vec[nod]) + + if(!ok[x]) df(x); + + st.push(nod); + +} + +vector grupe[lim]; + +int cnt; + +void df2(int nod) + +{ + + ok[nod]=true; + + grupe[cnt].push_back(nod); + + for(int x:rev[nod]) + + if(!ok[x]) df2(x); + +} + +int main() + +{ + + int n,m,x,y; + + in>>n>>m; + + for(int i=1;i<=m;++i) + + { + + in>>x>>y; + + vec[x].push_back(y); + + rev[y].push_back(x); + + } + + for(int i=1;i<=n;++i) + + if(!ok[i]) df(i); + + for(int i=1;i<=n;++i) + + ok[i]=false; + + while(!st.empty()) + + { + + int x=st.top(); st.pop(); + + if(ok[x]) continue; + + ++cnt; + + df2(x); + + } + + out< + + + + +using namespace std; + +const int MAXN = (int) 1e5; + + vector g[MAXN + 1], gr[MAXN + 1]; +bool vis[MAXN + 1]; +vector top; + +void dfs(int nod) { + vis[nod] = 1; + for(auto it : g[nod]) { + if(vis[it] == 0) { + dfs(it); + } + } + top.push_back(nod); +} + +vector > sol; + +void dfs1(int nod) { + sol.back().push_back(nod); + vis[nod] = 1; + for(auto it : gr[nod]) { + if(vis[it] == 0) { + dfs1(it); + } + } +} + +int main() { + + + + + ifstream cin("ctc.in"); + ofstream cout("ctc.out"); + int i, n, m; + ios::sync_with_stdio(false); + cin.tie(0), cout.tie(0); + + cin >> n >> m; + for(i = 1; i <= m; i++) { + int x, y; cin >> x >> y; + g[x].push_back(y); + gr[y].push_back(x); + } + for(i = 1; i <= n; i++) { + if(vis[i] == 0) { + dfs(i); + } + } + memset(vis, 0, sizeof(vis)); + while(top.size()) { + if(vis[top.back()] == 0) { + sol.push_back(vector ()); + dfs1(top.back()); + } + top.pop_back(); + } + + cout << sol.size() << "\n"; + for(auto &it : sol) { + for(auto nod : it) { + cout << nod << " "; + } + cout << "\n"; + } + + return 0; +} diff --git a/scc/kosaraju/8458596f-0eb5-47b3-bf19-d7e9b3833aec.cpp b/scc/kosaraju/8458596f-0eb5-47b3-bf19-d7e9b3833aec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96bae3f7ebad70fb1de7dae1ed1ad2efe13b9f31 --- /dev/null +++ b/scc/kosaraju/8458596f-0eb5-47b3-bf19-d7e9b3833aec.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +const int dim = 200005; + +int n,nr1 = 0,nr2 = 0,urm1[dim],urm2[dim],lst1[dim],lst2[dim],vf1[dim],vf2[dim],m,viz[dim],q[dim],st; +vector componente[dim]; +int cnt; + +void Adauga1(int x,int y) +{ + vf1[++nr1] = y; + urm1[nr1] = lst1[x]; + lst1[x] = nr1; +} + +void Adauga2(int x,int y) +{ + vf2[++nr2] = y; + urm2[nr2] = lst2[x]; + lst2[x] = nr2; +} + +void DFS1(int nod) +{ + viz[nod] = 1; + + for (int p= lst1[nod]; p!=0; p = urm1[p]) + { + int y = vf1[p]; + if (!viz[y]) + { + DFS1(y); + } + } + q[++st] = nod; +} + +void DFS2(int nod) +{ + componente[cnt].push_back(nod); + viz[nod] = 1; + + for (int p= lst2[nod]; p!=0; p = urm2[p]) + { + int y = vf2[p]; + if (!viz[y]) + { + DFS2(y); + } + } +} + +int main() +{ + in >> n >> m; + for (int i=1,x,y; i<=m; i++) + { + in >> x >> y; + Adauga1(x,y); + Adauga2(y,x); + } + + for (int i=1; i<=n; i++) + { + if (!viz[i]) + { + DFS1(i); + } + } + memset(viz,0,sizeof(viz)); + + for (int i=st; i>=1; i--) + { + if (!viz[q[i]]) + { + cnt++; + DFS2(q[i]); + } + } + + out << cnt << "\n"; + + for (int i=1; i<=cnt; i++) + { + for (auto j:componente[i]) + { + out << j << " "; + } + out << "\n"; + } + return 0; +} diff --git a/scc/kosaraju/85f81a0c-0be4-4497-9556-f281245dfd0e.cpp b/scc/kosaraju/85f81a0c-0be4-4497-9556-f281245dfd0e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dad0867cf46c42f42af69f826506227fb3a86d67 --- /dev/null +++ b/scc/kosaraju/85f81a0c-0be4-4497-9556-f281245dfd0e.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector a[200004],b[200004],c[200004],s; + +int v[100004],n1,x,y,m,n; + +void dfs(int i) + +{ + + v[i]=1; + + for(int j=0;j>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + a[x].push_back(y); + + b[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + { + + if(v[i]==0) + + dfs(i); + + } + + for(int i=1;i<=n;i++) + + v[i]=0; + + + + for(int i=n-1;i>=0;i--) + + { + + if(v[s[i]]==0) + + { + + n1++; + + dfs1(s[i]); + + } + + } + + fout< + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 1e5 + 3; + +int n, m, nrSCCs; + +array, NMAX> graph, tranGraph, SCC; + +array nrVis; + +stack st; + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++) + + { + + int x, y; + + fin >> x >> y; + + graph[x].push_back(y); + + tranGraph[y].push_back(x); + + } + +} + + + +void dfs(int start) + +{ + + nrVis[start] = 1; + + for (auto nei : graph[start]) + + if (!nrVis[nei]) + + dfs(nei); + + st.push(start); + +} + + + +void dfsTran(int start) + +{ + + nrVis[start] = 2; + + SCC[nrSCCs].push_back(start); + + + + for (auto nei : tranGraph[start]) + + if (nrVis[nei] == 1) + + dfsTran(nei); + +} + + + +void solve() + +{ + + for (int i = 1; i <= n; i++) + + if (!nrVis[i]) + + dfs(i); + + + + while (!st.empty()) + + { + + if (nrVis[st.top()] == 1) + + { + + nrSCCs++; + + dfsTran(st.top()); + + } + + st.pop(); + + } + +} + + + +void display() + +{ + + fout << nrSCCs << '\n'; + + for (int i = 1; i <= nrSCCs; i++) + + { + + for (auto node : SCC[i]) + + fout << node << ' '; + + fout << '\n'; + + } + +} + + + +int main() + +{ + + read(); + + solve(); + + display(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/8950fb83-5cc2-40bf-95d1-5a66dfc1d971.cpp b/scc/kosaraju/8950fb83-5cc2-40bf-95d1-5a66dfc1d971.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da2fa5197d1016affcb86259ec66136da4059fff --- /dev/null +++ b/scc/kosaraju/8950fb83-5cc2-40bf-95d1-5a66dfc1d971.cpp @@ -0,0 +1,127 @@ +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector noduri, g[100005], gt[100005], ctc[100005]; + +int n, m, currComp, v[100005]; + + + +void citire() { + + fin >> n >> m; + + while(m--) { + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs1(int x) { + + v[x] = 1; + + for(auto next: g[x]) + + if(!v[next]) + + dfs1(next); + + noduri.push_back(x); + +} + + + +void dfs2(int x) { + + v[x] = 1; + + ctc[currComp].push_back(x); + + for(auto next: gt[x]) + + if(!v[next]) + + dfs2(next); + +} + + + +void solve() { + + for(int i = 1; i <= n; i++) v[i] = 0; + + for(int i = 1; i <= n; i++) + + if(!v[i]) dfs1(i); + + + + for(int i = 1; i <= n; i++) v[i] = 0; + + for(int i = noduri.size()-1; i >= 0; i--) { + + int x = noduri[i]; + + if(!v[x]) { + + currComp++; + + dfs2(x); + + } + + } + +} + + + +void afis() { + + fout << currComp << '\n'; + + for(int i = 1; i <= currComp; i++) { + + for(auto x: ctc[i]) + + fout << x << ' '; + + fout << '\n'; + + } + +} + + + +int main() { + + citire(); + + solve(); + + afis(); + +} diff --git a/scc/kosaraju/8a36427e-53fc-4e09-a796-46438328ecf2.cpp b/scc/kosaraju/8a36427e-53fc-4e09-a796-46438328ecf2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8a05c7e5d1a10e8de38210c40cef76930abeda4 --- /dev/null +++ b/scc/kosaraju/8a36427e-53fc-4e09-a796-46438328ecf2.cpp @@ -0,0 +1,123 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +bool visited[100001]; + +vector graph1[100001]; + +vector graph2[100001]; + +vector ctc[100001]; + +int q, k, s[100001]; + + + +void dfs1(int node) { + + visited[node] = true; + + for(auto n : graph1[node]) + + if(!visited[n]) + + dfs1(n); + + s[++k] = node; + +} + + + + + +void dfs2(int node) { + + visited[node] = false; + + ctc[q].push_back(node); + + for(auto n : graph2[node]) + + if(visited[n] == 1) + + dfs2(n); + +} + + + + + +int main() { + + int n, m; + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) { + + int x, y; + + fin >> x >> y; + + + + graph1[x].push_back(y); + + graph2[y].push_back(x); + + } + + + + for(int i = 1; i <= n; i++) { + + if(!visited[i]) { + + dfs1(i); + + } + + } + + for(int i = n; i >= 1; i--) { + + if(visited[s[i]] == 1) { + + q++; + + dfs2(s[i]); + + } + + } + + + + fout << q << '\n'; + + for(int i = 1; i <= q; i++) { + + for(auto node : ctc[i]) + + fout << node << ' '; + + fout << '\n'; + + } + +} diff --git a/scc/kosaraju/8a9a51f9-37ca-47ef-b18f-922648ca85eb.cpp b/scc/kosaraju/8a9a51f9-37ca-47ef-b18f-922648ca85eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba713ff1f424b7ada05bbeb29ff125ef9d3a124b --- /dev/null +++ b/scc/kosaraju/8a9a51f9-37ca-47ef-b18f-922648ca85eb.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + +using namespace std; + +const int NMAX = 1e5 + 5; + +ifstream cin ("ctc.in"); +ofstream cout ("ctc.out"); + +vector g1[NMAX]; +vector g2[NMAX]; +int viz[NMAX]; +vector ans[NMAX]; +vector nodes; +void dfs1(int node) +{ + viz[node] = 1; + for(auto x: g1[node]) { + if(viz[x] == 0) { + dfs1(x); + } + } + nodes.push_back(node); +} + +void dfs2(int node, int cm) +{ + viz[node] = 1; + ans[cm].push_back(node); + for(auto x: g2[node]) { + if(viz[x] == 0) { + dfs2(x, cm); + } + } +} + +int main() { + int n, m; + cin >> n >> m; + + for(int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + g1[x].push_back(y); + g2[y].push_back(x); + } + for(int i = 1; i <= n; ++i) { + if(viz[i] == 0) { + dfs1(i); + } + } + reverse(nodes.begin(), nodes.end()); + for(int i = 1; i <= n; ++i) + viz[i] = 0; + int cnt = 0; + for(int i = 0; i < nodes.size(); ++i) { + if(viz[nodes[i]] == 0) { + ans[cnt++].clear(); + dfs2(nodes[i], cnt - 1); + } + } + cout << cnt << "\n"; + for(int i = 0; i < cnt; ++i) { + for(auto x: ans[i]) + cout << x << " "; + cout << "\n"; + } + return 0; +} diff --git a/scc/kosaraju/8aa83219-7b6c-410a-9d8e-2c350d3f3a22.cpp b/scc/kosaraju/8aa83219-7b6c-410a-9d8e-2c350d3f3a22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd358ef785c63ba3d5783e3d0787f62cfe58cc8f --- /dev/null +++ b/scc/kosaraju/8aa83219-7b6c-410a-9d8e-2c350d3f3a22.cpp @@ -0,0 +1,61 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m; +vector> ad, adT; + +vector topo; +vector vis; +vector> sccs; + +void dfs1(int node) { + vis[node] = true; + for (int nghb : ad[node]) + if (!vis[nghb]) + dfs1(nghb); + topo.push_back(node); +} + +void dfs2(int node) { + vis[node] = true; + sccs.back().push_back(node); + for (int nghb : adT[node]) + if (!vis[nghb]) + dfs2(nghb); +} + +int main() { + fin >> n >> m; + ad.resize(n + 1); + adT.resize(n + 1); + for (int i = 0; i < m; i++) { + int x, y; fin >> x >> y; + ad[x].push_back(y); + adT[y].push_back(x); + } + + vis.resize(n + 1); + for (int i = 1; i <= n; i++) + if (!vis[i]) + dfs1(i); + + fill(vis.begin() + 1, vis.end(), false); + for (int i = n - 1; i >= 0; i--) + if (!vis[topo[i]]) { + sccs.emplace_back(); + dfs2(topo[i]); + } + + fout << sccs.size() << '\n'; + for (auto& scc : sccs) { + for (int node : scc) + fout << node << ' '; + fout << '\n'; + } + + fout.close(); + return 0; +} diff --git a/scc/kosaraju/8b3ad1fb-b252-469f-b0a3-9afcc650bb04.cpp b/scc/kosaraju/8b3ad1fb-b252-469f-b0a3-9afcc650bb04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6294776eb90a1ca2baa7f65bdcbe9fecf51833ae --- /dev/null +++ b/scc/kosaraju/8b3ad1fb-b252-469f-b0a3-9afcc650bb04.cpp @@ -0,0 +1,426 @@ +#include + + + + +using namespace std; + + + +class InParser + +{ + +private: + + FILE *fin; + + char *buff; + + int sp; + + + + char read_ch() + + { + + ++sp; + + if(sp==4096) + + { + + sp=0; + + fread(buff,1,4096,fin); + + } + + return buff[sp]; + + } + + + +public: + + InParser(const char* nume) + + { + + fin=fopen(nume,"r"); + + buff=new char[4096](); + + sp=4095; + + } + + + + InParser& operator >> (int &n) + + { + + char c; + + while(!isdigit(c=read_ch())&&c!='-'); + + int sgn=1; + + if (c=='-') + + { + + n=0; + + sgn=-1; + + } + + else + + { + + n=c-'0'; + + } + + while(isdigit(c=read_ch())) + + { + + n=10*n+c-'0'; + + } + + n*=sgn; + + return *this; + + } + + + + InParser& operator >> (long long &n) + + { + + char c; + + n=0; + + while(!isdigit(c=read_ch())&&c!='-'); + + long long sgn=1; + + if(c=='-') + + { + + n=0; + + sgn=-1; + + } + + else + + { + + n=c-'0'; + + } + + while(isdigit(c=read_ch())) + + { + + n=10*n+c-'0'; + + } + + n*=sgn; + + return *this; + + } + +}; + + + +class OutParser + +{ + +private: + + FILE *fout; + + char *buff; + + int sp; + + + + void write_ch(char ch) + + { + + if(sp==50000) + + { + + fwrite(buff,1,50000,fout); + + sp=0; + + buff[sp++]=ch; + + } + + else + + { + + buff[sp++]=ch; + + } + + } + + + +public: + + OutParser(const char* name) + + { + + fout=fopen(name,"w"); + + buff=new char[50000](); + + sp=0; + + } + + ~OutParser() + + { + + fwrite(buff,1,sp,fout); + + fclose(fout); + + } + + + + OutParser& operator <<(int vu32) + + { + + if(vu32<=9) + + { + + write_ch(vu32+'0'); + + } + + else + + { + + (*this) <<(vu32/10); + + write_ch(vu32%10+'0'); + + } + + return *this; + + } + + + + OutParser& operator <<(long long vu64) + + { + + if(vu64<=9) + + { + + write_ch(vu64+'0'); + + } + + else + + { + + (*this) <<(vu64/10); + + write_ch(vu64%10+'0'); + + } + + return *this; + + } + + + + OutParser& operator <<(char ch) + + { + + write_ch(ch); + + return *this; + + } + + OutParser& operator <<(const char *ch) + + { + + while(*ch) + + { + + write_ch(*ch); + + ++ch; + + } + + return *this; + + } + +}; + +InParser f ("ctc.in"); + +OutParser g ("ctc.out"); + + + +int n, m, x, y, viz[100005], nr; + +vector < int > G[100005], GT[100005], CTC[100005]; + +stack < int > S; + + + +void read () { + + f >> n >> m; + + while (m --) { + + f >> x >> y; + + G[x].push_back (y); + + GT[y].push_back (x); + + } + +} + + + +void DFSP (int k) { + + viz[k] = 1; + + for (unsigned int i = 0; i < G[k].size(); ++i) { + + int vecin = G[k][i]; + + if (!viz[vecin]) DFSP (vecin); + + } + + S.push (k); + +} + + + +void DFSM (int k) { + + viz[k] = 2; + + CTC[nr].push_back (k); + + for (unsigned int i = 0; i < GT[k].size(); ++i) { + + int vecin = GT[k][i]; + + if (viz[vecin] == 1) DFSM (vecin); + + } + +} + + + +void solve () { + + for (int i = 1; i <= n; ++i) if (!viz[i]) DFSP (i); + + while (!S.empty()) { + + int nod = S.top(); + + S.pop(); + + if (viz[nod] == 1) { + + nr ++; + + DFSM (nod); + + } + + } + +} + + + +void print () { + + g << nr << '\n'; + + for (int i = 1; i <= nr; ++i, g << '\n') + + for (unsigned int j = 0; j < CTC[i].size(); ++j) + + g << CTC[i][j] << " "; + +} + + + +int main() + +{ + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/scc/kosaraju/8b893db6-e070-43a5-ad5e-bfd99f79a5b2.cpp b/scc/kosaraju/8b893db6-e070-43a5-ad5e-bfd99f79a5b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1479793498d22f3c53021b055eca8916b7a75584 --- /dev/null +++ b/scc/kosaraju/8b893db6-e070-43a5-ad5e-bfd99f79a5b2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +bool f[100005], f2[100005]; +int n, m, nrCTC, maxim = -5; +vector v[100005], ctc[100005], v2[100005]; +stack st; + +void dfs(int x) { + for(unsigned int i = 0; i < v[x].size(); ++i) { + int y = v[x][i]; + if(f[y] == 0) { + f[y] = 1; + dfs(y); + } + } + st.push(x); +} + +void dfs2(int x, int &nrMax) { + ctc[nrCTC].push_back(x); + for(unsigned int i = 0; i < v2[x].size(); ++i) { + int y = v2[x][i]; + if(f2[y] == 0) { + nrMax++; + f2[y] = 1; + dfs2(y, nrMax); + } + } +} + +int main() { + + in >> n >> m; + for(int i = 1; i <= m; ++i) { + int x, y; + in >> x >> y; + v[x].push_back(y); + v2[y].push_back(x); + } + + for(int i = 1; i <= n; ++i) { + if(f[i] == 0) { + f[i] = 1; + dfs(i); + } + } + + while(!st.empty()) { + int x = st.top(); + if(f2[x] == 0) { + f2[x] = 1; + int nrMax = 1; + ++nrCTC; + dfs2(x, nrMax); + if(nrMax > maxim) + maxim = nrMax; + } + st.pop(); + } + out << nrCTC << '\n'; + for(int i = 1; i <= nrCTC; ++i) { + for(unsigned int j = 0; j < ctc[i].size(); ++j) { + out << ctc[i][j] << ' '; + } + out << '\n'; + } + + return 0; +} diff --git a/scc/kosaraju/8e1927c7-5a9b-4ee3-856b-ed7cbac00494.cpp b/scc/kosaraju/8e1927c7-5a9b-4ee3-856b-ed7cbac00494.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3d732f48e44a6ba7cd48cb577c15a844cc408843 --- /dev/null +++ b/scc/kosaraju/8e1927c7-5a9b-4ee3-856b-ed7cbac00494.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + +const int kNmax = 100005; + +class Task { +public: + void solve() { + read_input(); + do_algo(); + print_output(sol); + } + +private: + int n; + int m; + vector adj[kNmax]; + vector adj_trans[kNmax]; + vector visited; + vector visited_trans; + stack s; + vector> sol; + + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + for (int i = 1, x, y; i <= m; i++) { + fin >> x >> y; + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + visited.resize(n+1, 0); + fin.close(); + } + + + void dfs(int source) { + + visited[source] = 1; + for (auto &node : adj[source]) { + if (!visited[node]) { + dfs(node); + s.push(node); + } + } + } + + void dfs_trans(int source, int idx) { + visited_trans[source] = 1; + + sol[idx].push_back(source); + for (auto &node : adj_trans[source]) { + if (!visited_trans[node]) { + dfs_trans(node, idx); + } + } + } + + void do_algo() { + sol.resize(n); + int count = 0; + + + for (int i = 1; i <= n; i++) { + if (!visited[i]) { + dfs(i); + s.push(i); + } + } + + visited_trans.resize(n+1, 0); + while (!s.empty()) { + if (!visited_trans[s.top()]) { + dfs_trans(s.top(), count); + } + count++; + s.pop(); + } + } + void printGraph() { + for (int i = 1; i <= n; i++) { + cout << "["<< i << "]: "; + for (int x : adj[i]) { + cout << x << " "; + } + cout << endl; + } + } + void printStack() { + while(!s.empty()) { + cout << s.top() << endl; + s.pop(); + } + } + int get_zeros(const vector> result) { + int zeroes = 0; + for (const auto& ctc : result) { + if (!ctc.size()) { + zeroes++; + } + } + return zeroes; + } + void print_output(const vector>& result) { + ofstream fout("ctc.out"); + fout << result.size() - get_zeros(result) << '\n'; + for (const auto& ctc : result) { + if (ctc.size()) { + for (int nod : ctc) { + fout << nod << ' '; + } + fout << '\n'; + } + } + fout.close(); + } +}; + + +int main() { + + + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/scc/kosaraju/8eab515a-7509-4191-a71d-6d7cd23c759f.cpp b/scc/kosaraju/8eab515a-7509-4191-a71d-6d7cd23c759f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92800bc21608398b2c05517828d66778266ca1cb --- /dev/null +++ b/scc/kosaraju/8eab515a-7509-4191-a71d-6d7cd23c759f.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector < vector > G, Gt; + +vector s, viz; + +queue C; + +int nr; + + + +void Dfs2(int x) + +{ + + viz[x] = nr; + + C.push(x); + + for(int i=0; i>n>>m; + + G.resize(n+1); Gt.resize(n+1); viz.resize(n+1); + + while(m) + + { + + fin>>x>>y; + + G[x].push_back(y); + + Gt[y].push_back(x); + + m--; + + } + + + + for(int i=1; i<=n; i++) + + if(viz[i] == 0) + + Dfs1(i); + + + + for(int i=s.size()-1; i>=0; i--) + + if(viz[s[i]] == -1) + + { + + nr++; + + Dfs2(s[i]); + + } + + fout< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector < int > G[100001], GT[100001], ass[100001]; + +stack < int > S; + +int n, m, nr, v[100001]; + + + +void citire(){ + + int x, y; + + f >> n >> m; + + for(int i = 0; i <= m; i ++){ + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void dfsG(int k){ + + v[k] = 1; + + for(auto x: G[k]) + + if(v[x] == 0) + + dfsG(x); + + S.push(k); + +} + + + +void dfsGT(int k){ + + v[k] = -1; + + ass[nr].push_back(k); + + for(auto x: GT[k]) + + if(v[x] == 1) + + dfsGT(x); + +} + + + +void rezolvare(){ + + for(int i = 1; i <= n; i ++) + + if(!v[i]) + + dfsG(i); + + + + while(!S.empty()){ + + int nod = S.top(); + + if(v[nod] == 1){ + + nr ++; + + dfsGT(nod); + + } + + S.pop(); + + } + + g << nr << '\n'; + + for(int i = 1; i <= nr; i ++){ + + sort(ass[i].begin(), ass[i].end()); + + for(int j = 0; j < ass[i].size(); j ++) + + g << ass[i][j] << ' '; + + g << '\n'; + + } + +} + + + + + +int main(){ + + citire(); + + rezolvare(); + +} diff --git a/scc/kosaraju/90e702ea-0cbc-4a25-aceb-b866a784ae73.cpp b/scc/kosaraju/90e702ea-0cbc-4a25-aceb-b866a784ae73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64cd01702a4c494feb516429ac7a044305696f8c --- /dev/null +++ b/scc/kosaraju/90e702ea-0cbc-4a25-aceb-b866a784ae73.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include + + + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, viz[100005], vizT[100005], ct; + +vector muchii[100005]; + +vector muchiiT[100005]; + +vector sol[100005]; + +stack S; + + + +void Citire() + +{ + + int i, x, y; + + fin >> n >> m; + + for (i = 1; i <= m; i++) + + { + + fin >> x >> y; + + muchii[x].push_back(y); + + muchiiT[y].push_back(x); + + } + +} + + + +void DFS(int x) + +{ + + int i, y; + + viz[x] = 1; + + for (i = 0; i < muchii[x].size();i++) + + if (viz[muchii[x][i]] == 0) + + DFS(muchii[x][i]); + + S.push(x); + +} + + + + + +void DFST(int x) + +{ + + int y, i; + + vizT[x] = 1; + + sol[ct].push_back(x); + + for (i = 0; i < muchiiT[x].size(); i++) + + if (vizT[muchiiT[x][i]] == 0) + + DFST(muchiiT[x][i]); + + + +} + + + + + +void CompTareConexe() + +{ + + int i, x, j; + + for (i = 1; i <= n; i++) + + if (viz[i]==0) + + DFS(i); + + + + while (S.empty()==0) + + { + + if (vizT[S.top()]==0) + + { + + ct++; + + DFST(S.top()); + + } + + S.pop(); + + } + + + + fout << ct << "\n"; + + for (i = 1; i <= ct; i++) + + { + + for (j = 0; j < sol[i].size(); j++) + + fout << sol[i][j] << " "; + + fout << "\n"; + + } + + + +} + + + + + +int main() + +{ + + Citire(); + + CompTareConexe(); + + + + return 0; + +} diff --git a/scc/kosaraju/90fca23a-a456-431c-a8d5-093274538988.cpp b/scc/kosaraju/90fca23a-a456-431c-a8d5-093274538988.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e510f685110c4f017eeab3aebd82e529357a153e --- /dev/null +++ b/scc/kosaraju/90fca23a-a456-431c-a8d5-093274538988.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; + + + + + + + +vector Go[100005], Gi[100005], G[100005]; + + + +void read_in(vector * Go, vector * Gi, int& n, const char iname[]) + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) { + + in >> x >> y; + + x --, y --; + + Go[x].push_back(y); + + Gi[y].push_back(x); + + } + + in.close(); + +} + + + +vector discovered, used; + + + +void DFP(const int n, vector * G) + +{ + + vector ::iterator it; + + used[n] = true; + + for (it = G[n].begin(); it != G[n].end(); ++ it) + + if (used[*it] == false) + + DFP(*it, G); + + discovered.push_back(n); + +} + + + +vector where; + + + +void DFM(const int n, vector * G, const int which) + +{ + + vector ::iterator it; + + where[n] = which; + + for (it = G[n].begin(); it != G[n].end(); ++ it) + + if (where[*it] == -1) + + DFM(*it, G, which); + +} + + + +void print_out(const vector * G, const int n, const char oname[]) + +{ + + ofstream out(oname); + + vector ::const_iterator it; + + + + out << n <<'\n'; + + for (int i = 0; i < n; ++ i) { + + for (it = G[i].begin(); it != G[i].end(); ++ it) + + out << *it + 1 << " "; + + out << '\n'; + + } + + out.close(); + +} + + + +int main(void) + +{ + int n; + read_in(Go, Gi, n, iname); + used.resize(n); + used.assign(used.size(), 0); + for (int i = 0; i < n; ++ i) if (used[i] == false) + DFP(i, Go); + where.resize(n); + where.assign(where.size(), -1); + int count = 0; + for (; discovered.size(); discovered.pop_back()) + if (where[discovered.back()] == -1) { + DFM(discovered.back(), Gi, count); + count ++; + } + for (int i = 0; i < n; ++ i) + G[where[i]].push_back(i); + print_out(G, count, oname); + return 0; + +} diff --git a/scc/kosaraju/91302b1c-bb5d-43dd-b4f7-6c6bb313df80.cpp b/scc/kosaraju/91302b1c-bb5d-43dd-b4f7-6c6bb313df80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4f8f723b74efb4867d7d2b5051c4118a75c90ec --- /dev/null +++ b/scc/kosaraju/91302b1c-bb5d-43dd-b4f7-6c6bb313df80.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +const int NMAX = 1e5; + + + +int N, M; + +vector g[NMAX + 2], r[NMAX + 2]; + + + +bool vis[NMAX + 2]; + +stack st; + + + +vector < int > comp; + +vector < vector > scc; + + + +void dfs(int node) { + + vis[node] = true; + + + + for(auto it : g[node]) + + if(!vis[it]) + + dfs(it); + + + + st.push(node); + +} + + + +void dfs2(int node) { + + comp.push_back(node); + + vis[node] = true; + + + + for(auto it : r[node]) + + if(!vis[it]) + + dfs2(it); + +} + + + +int main() + +{ + + cin >> N >> M; + + + + for(int i = 1; i <= M; i++) { + + int x, y; + + cin >> x >> y; + + + + g[x].push_back(y); + + r[y].push_back(x); + + } + + + + for(int i = 1; i <= N; i++) + + if(vis[i] == 0) dfs(i); + + + + for(int i = 1; i <= N; i++) + + vis[i] = 0; + + + + while(!st.empty()) { + + int node = st.top(); + + st.pop(); + + + + if(!vis[node]) { + + comp.clear(); + + dfs2(node); + + scc.push_back(comp); + + } + + } + + + + cout << (int)scc.size() << '\n'; + + for(auto comp : scc) + + { + + for(auto v : comp) + + cout << v << ' '; + + cout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/913f3c35-f732-4ef5-821d-5cb3516f42ff.cpp b/scc/kosaraju/913f3c35-f732-4ef5-821d-5cb3516f42ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cea12c68d229cd8acecc343d3d39250fc2952586 --- /dev/null +++ b/scc/kosaraju/913f3c35-f732-4ef5-821d-5cb3516f42ff.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +const int N=100009; + +int n,m,nr; + +int d[N]; + +vector v1[N],v2[N],sol[N]; + +stack sk; + +void dfs(int nod) + +{ + + d[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + fin>>x>>y; + + v1[x].push_back(y); + + v2[y].push_back(x); + + } + + solve(); + + return 0; + +} diff --git a/scc/kosaraju/921eddda-b63c-470e-a2f1-82658b0bc2b3.cpp b/scc/kosaraju/921eddda-b63c-470e-a2f1-82658b0bc2b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91dd4d58461a20d564cfbf6cac1faf8c94886f9b --- /dev/null +++ b/scc/kosaraju/921eddda-b63c-470e-a2f1-82658b0bc2b3.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m; + +struct kosaraju{ + struct nod{ + bool viz, viz2; + vector in; + vector out; + }; + vector g; + struct comp{ + vector v; + }; + vector v; + stack s; + void visit(int root){ + g[root].viz=1; + for(auto it:g[root].out){ + if(!g[it].viz) visit(it); + } + s.push(root); + } + void build(int root){ + v.back().v.push_back(root); + g[root].viz2=1; + for(auto it:g[root].in){ + if(!g[it].viz2) build(it); + } + } + bool get(){ + if(s.empty()) return 0; + v.push_back({}); + while(!s.empty()&&g[s.top()].viz2) s.pop(); + if(s.empty()) return 0; + build(s.top()); + return 1; + } +}; + +int main(){ + fin>>n>>m; + kosaraju x; + x.g.resize(n+1); + for(int i=1;i<=m;++i){ + int a, b; + fin>>a>>b; + x.g[a].out.push_back(b); + x.g[b].in.push_back(a); + } + for(int i=1;i<=n;++i) if(!x.g[i].viz) x.visit(i); + while(x.get()); + fout< //Andrei Alexandru a.k.a Sho10 + + + + + +using namespace std; + +long long n,m,s1=0; + +vectorg[100005],gr[100005]; + +bitset<100005>viz; + +vectorv,res[100005]; + +void dfs(long long node){ + +viz[node]=1; + +for(auto it : g[node]){ + + if(viz[it]==0){ + + dfs(it); + + } + +} + +v.push_back(node); + +} + +void dfs2(long long node){ + +viz[node]=1; + +res[s1].push_back(node); + +for(auto it : gr[node]){ + +if(viz[it]==0){ + + dfs2(it); + +} + +} + +} + +int32_t main(){ + +ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);; + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +cin>>n>>m; + +for(long long i=1;i<=m;i++) + +{ + + long long x,y; + + cin>>x>>y; + + g[x].push_back(y); + + gr[y].push_back(x); + +} + +for(long long i=1;i<=n;i++) + +{ + + if(!viz[i]){ + + dfs(i); + + } + +} + +viz.reset(); + +reverse(v.begin(),v.end()); + +for(auto it : v){ + + if(!viz[it]){ + + ++s1; + + dfs2(it); + + } + +} + +cout< + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector G[100001]; + +vector Gt[100001]; + +vector compcon[100001]; + +int viz[100001], TSORT[100001], viz2[100001]; + +int n,m,k,l; + + + +void dfs(int nod){ + + viz[nod] = 1; + + for(auto vecin: G[nod]){ + + if(!viz[vecin]) + + dfs(vecin); + + } + + TSORT[++k] = nod; + +} + + + +void parcdfs(int nod,int comp){ + + viz2[nod] = 1; + + compcon[comp].push_back(nod); + + for(auto vecin: Gt[nod]){ + + if(!viz2[vecin]){ + + parcdfs(vecin,comp); + + } + + } + +} + + + +int main(){ + + int i,a,b; + + fin>>n>>m; + + for(i = 1; i <= m; i++){ + + fin>>a>>b; + + G[a].push_back(b); + + Gt[b].push_back(a); + + } + + for(i = 1; i<= n; i++){ + + if(!viz[i]){ + + dfs(i); + + } + + } + + for(i = n; i >= 1; i--){ + + if(!viz2[TSORT[i]]){ + + l++; + + parcdfs(TSORT[i],l); + + } + + } + + + + fout< +#include +using namespace std; +ifstream cin("ctc.in"); +ofstream cout("ctc.out"); +const int N = 100005; +int viz[N]; +vector L[N], trans[N],postord; +int n ,m,x,y,k; +vector ctc[N]; +void dfs(int x) +{ + viz[x] = 1; + for(int i : L[x]) + if(!viz[i]) + dfs(i); + postord.push_back(x); +} +void dfs1(int x) +{ + viz[x] = 0; + for(int i : trans[x]) + if(viz[i]) + dfs1(i); + ctc[k].push_back(x); +} +int main() { + cin >> n >>m ; + for(int i = 1 ;i <=m ;i ++) + { + cin >> x >> y; + L[x].push_back(y); + trans[y].push_back(x); + } + for(int i = 1 ;i <=n; i ++) + { + if(!viz[i]) + dfs(i); + } + for(int i = postord.size() - 1 ;i >= 0 ;i --) + { + if(viz[postord[i]]) + dfs1(postord[i]),k++; + } + cout << k<<'\n'; + for(int i = 0 ; i < k ; i ++,cout << '\n') + for(int j : ctc[i]) + cout << j << " "; + return 0; +} diff --git a/scc/kosaraju/93acadae-986a-4a82-8772-506bc5f39017.cpp b/scc/kosaraju/93acadae-986a-4a82-8772-506bc5f39017.cpp new file mode 100644 index 0000000000000000000000000000000000000000..482b0f5f357439a9a3c6e6ad9d9cfa4b096c3378 --- /dev/null +++ b/scc/kosaraju/93acadae-986a-4a82-8772-506bc5f39017.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +const int maxn = 100005; + +vector ctc[maxn]; + +vector v[maxn]; + +vector u[maxn]; + +vector ord; + +bitset viz; + +bitset Ass; + + + +void dfs(int nod) + +{ + + viz[nod] = 1; + + for(auto it : v[nod]) + + if(!viz[it]) + + dfs(it); + + ord.push_back(nod); + +} + + + +void Assign(int nod, int rad) + +{ + + Ass[nod] = 1; + + ctc[rad].push_back(nod); + + for(auto it : u[nod]) + + if(!Ass[it]) + + Assign(it, rad); + +} + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + in >> x >> y; + + v[x].push_back(y); + + u[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + dfs(i); + + reverse(ord.begin(), ord.end()); + + int comp = 0; + + for(auto it : ord) + + if(!Ass[it]) + + Assign(it, ++comp); + + out << comp << "\n"; + + for(int i = 1; i <= comp; i++, out << "\n") + + for(auto it : ctc[i]) + + out << it << " "; + + return 0; + +} diff --git a/scc/kosaraju/93bf1d95-c20b-443c-ad68-bd716eed415a.cpp b/scc/kosaraju/93bf1d95-c20b-443c-ad68-bd716eed415a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2d0161ee00d2f10ed2bd66b33a33d5dd0b045c1 --- /dev/null +++ b/scc/kosaraju/93bf1d95-c20b-443c-ad68-bd716eed415a.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 100001; + + + +vectorA[NMAX] , tA[NMAX]; + +int N , E , StronglyConnectedComponents = 0; + + + +void Read(){ + + int x , y; + + fin>>N>>E; + + for(int i = 1 ; i<=E ; i++){ + + fin>>x>>y; + + A[x].push_back(y); + + tA[y].push_back(x); + + } + +} + + + +stacks; + + + +void DFSstack(int Node , bool viz[NMAX]) + +{ + + viz[Node] = true; + + for(vector::iterator it = A[Node].begin() ; it!=A[Node].end() ; it++){ + + if(viz[*it] == false) + + DFSstack(*it , viz); + + } + + + + s.push(Node); + +} + + + +void DFStranspose(int Node , int viz[]){ + + viz[Node] = StronglyConnectedComponents; + + for(vector::iterator it = tA[Node].begin() ; it != tA[Node].end() ; it++){ + + if(viz[*it] == 0) + + DFStranspose(*it , viz); + + } + +} + + + +vector Answer[NMAX]; + + + + + +int main() + +{ + +Read(); + + + + bool viz[NMAX]; + + memset(viz , false , sizeof(viz)); + + for(int i = 1 ; i<=N ;i++){ + + if(viz[i] == false) + + DFSstack(i , viz); + + } + + + + int x , viz1[NMAX]; + + memset(viz1 , 0 , sizeof(viz1)); + + + + while(!s.empty()){ + + x = s.top(); + + s.pop(); + + if(viz1[x] == 0){ + + StronglyConnectedComponents++; + + DFStranspose(x , viz1); + + } + + } + + + + for(int i = 1 ; i<=N ; i++){ + + Answer[ viz1[i] ].push_back(i); + + } + + fout<::iterator it = Answer[i].begin() ; it!=Answer[i].end() ; it++ ){ + + fout << *it<<' '; + + } + + fout<<'\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/93e35e6c-1eb1-4ccd-9859-516793f7cbfd.cpp b/scc/kosaraju/93e35e6c-1eb1-4ccd-9859-516793f7cbfd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c6e761f870791dce502e4da8aaf4a94fde5e883a --- /dev/null +++ b/scc/kosaraju/93e35e6c-1eb1-4ccd-9859-516793f7cbfd.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream Fin("ctc.in"); + +ofstream Fout("ctc.out"); + + + +int const limita = 100001; + +int n,m,t; + +bool ver[limita],vr[limita]; + +vector < int > vector1[limita]; + +vector < int > c2[limita]; + +vector < int > st; + +vector < int > afisare[limita]; + + + +void dfs(int NOD) + +{ + + ver[NOD]=1; + + + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + Fin>>x>>y; + + vector1[x].push_back(y); + + c2[y].push_back(x); + + } + + + + for(int i=1;i<=n;i++) + + if(ver[i]==0) + + { + + dfs(i); + + } + + + + for(int i=st.size()-1;i>=0;i--) + + if(vr[st[i]]==0) + + { + + t++; + + dfs2(st[i]); + + } + + + + Fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,i,m,a,b,j,nr; + + + +vectorv[100001],vt[100001]; + + + +bool viz[100001],Viz[100001]; + + + +vectorcompcon[100001]; + +stackst; + + + +void citire(){ + + + + f >> n>>m; + + + + for(int i = 1; i <= m; i++){ + + + + f>> a >> b; + + + + v[a].push_back(b); + + vt[b].push_back(a); + + } + + + +} + + + + + +void dfs(int vf){ + + + + viz[vf]=true; + + + + for(int i=0; i +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m; + +vectorgraph[100005], gt[100005]; + +stacks; + +vector comp[100005]; + +int viz[100005]; + +int nrc; + + + +void citire(){ + + f>>n>>m; + + int x, y; + + for(int i=0; i>x>>y; + + graph[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void DFS(int v){ + + viz[v] = 1; + + for(auto &vf:graph[v]) + + if(viz[vf] == 0) + + DFS(vf); + + s.push(v); + +} + + + +void DFSt(int v, int nrc){ + + viz[v] = -1; + + for(auto &vf:gt[v]) + + if(viz[vf] > 0) + + DFSt(vf, nrc); + + comp[nrc].push_back(v); + +} + + + +void parcurgere(){ + + for(int i=1; i<=n; i++) + + if(viz[i] == 0) + + DFS(i); + +} + + + +void solve(){ + + while(!s.empty()){ + + int vf = s.top(); + + s.pop(); + + if(viz[vf] < 0) + + continue; + + nrc++; + + DFSt(vf, nrc); + + } + +} + + + +void afis(){ + + g< +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int nlim=100005; + +int n,m,nrCTC=0; + +int beenThere[nlim]; + +vector muchii[nlim],muchii_T[nlim],result[nlim]; + +stack S; + + + +void citire() + +{ + + f>>n>>m; + + int x,y; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + muchii[x].push_back(y); + + muchii_T[y].push_back(x); + + } + +} + + + +void DFS(int nod) + +{ + + beenThere[nod]=1; + + for(int i=0;i +#include +#include +#include + + + + + + + +using namespace std; + +int v, e; +int nodeColor[100001]; +int nrSCC = 0; + +vector graph[100001]; +vector graphTransposed[100001]; +vector SCC[100001]; +stack st; + +void read(); +void DFS(int); +void DFStranspose(int); +void Kosaraju(); +void topologicalSort(); +void write(); + +int main() { + + read(); + + Kosaraju(); + + write(); + + return 0; +} + +void read() +{ + freopen("ctc.in", "r", stdin); + + scanf("%d%d", &v , &e); + + int x, y; + + for (int i = 1; i <= e; i++) + { + scanf("%d%d", &x, &y); + + graph[x].push_back(y); + graphTransposed[y].push_back(x); + } +} + +void Kosaraju() +{ + topologicalSort(); + + while (!st.empty()) + { + int node = st.top(); + printf("%d ", node); + + if (nodeColor[node] == 1) + { + ++nrSCC; + DFStranspose(node); + } + st.pop(); + } +} + +void topologicalSort() +{ + for (int i = 1; i <= v; i++) + { + if (nodeColor[i] == 0) + DFS(i); + } +} + +void DFS(int node) +{ + nodeColor[node] = 1; + + for (unsigned int i = 0; i < graph[node].size(); i++) + { + int neighbour = graph[node][i]; + + if (nodeColor[neighbour] == 0) + DFS(neighbour); + } + + st.push(node); +} + +void DFStranspose(int node) +{ + nodeColor[node] = 2; + SCC[nrSCC].push_back(node); + + for (unsigned int i = 0; i < graphTransposed[node].size(); i++) + { + int neighbour = graphTransposed[node][i]; + + if (nodeColor[neighbour] == 1) { + DFStranspose(neighbour); + } + } +} + +void write() +{ + freopen("ctc.out", "w", stdout); + + printf("%d\n", nrSCC); + + for (int i = 1; i <= nrSCC; i++) + { + for (auto j = SCC[i].begin(); j != SCC[i].end(); ++j) + printf("%d ", *j); + printf("\n"); + } + + fclose(stdout); +} diff --git a/scc/kosaraju/97a7f98a-27ea-4a31-8032-7e11eba02dd1.cpp b/scc/kosaraju/97a7f98a-27ea-4a31-8032-7e11eba02dd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1494c00a64385bae2b39a3d39578c674b9eba6d0 --- /dev/null +++ b/scc/kosaraju/97a7f98a-27ea-4a31-8032-7e11eba02dd1.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vectorg[100005]; + +vectorgt[100005]; + +vectorv[100005]; + +stacks; + +int viz[100005]; + +int n,m,c=0; + +void dfs(int k) + +{ + + viz[k]=1; + + for(int i=0; i>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y; + + fin>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + { + + if(viz[i]==0) + + { + + dfs(i); + + } + + } + + while(s.empty()==0) + + { + + int nod=s.top(); + + if(viz[nod]==1) + + { + + c++; + + dfsgt(nod); + + } + + s.pop(); + + } + + fout< + + + + + + + +using namespace std; + + + +const long long nmax = 100005; + + + +long long n,m,_curr = -1; + +vectornod[nmax],nod_R[nmax],vecc,scc[nmax]; + +bitsetviz; + + + +void DFS(long long s){ + + viz[s] = 1; + + for(auto it : nod[s]){ + + if(!viz[it]){ + + DFS(it); + + } + + } + + vecc.push_back(s); + +} + + + +void DFS2(long long s){ + + viz[s] = 1; + + scc[_curr].push_back(s); + + for(auto it : nod_R[s]){ + + if(!viz[it]){ + + DFS2(it); + + } + + } + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + cin >> n >> m; + + for(long long i=1;i<=m;i++){ + + long long x,y; + + cin >> x >> y; + + nod[x].push_back(y); + + nod_R[y].push_back(x); + + } + + for(long long i=1;i<=n;i++){ + + if(!viz[i]) DFS(i); + + } + + viz.reset(); + + reverse((vecc).begin(), (vecc).end()); + + for(auto it : vecc){ + + if(!viz[it]){ + + ++_curr; + + DFS2(it); + + } + + } + + cout << (_curr + 1LL) << '\n'; + + for(long long i=0;i<=_curr;i++){ + + for(auto it : scc[i]) cout << it << ' '; + + cout << '\n'; + + } + +} diff --git a/scc/kosaraju/99078ffa-7690-4d02-8e8a-f1dc4d10b998.cpp b/scc/kosaraju/99078ffa-7690-4d02-8e8a-f1dc4d10b998.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96d08da3d2ad717e6f5b06bd93962cd80d09d120 --- /dev/null +++ b/scc/kosaraju/99078ffa-7690-4d02-8e8a-f1dc4d10b998.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +int N, M; + + + +vector links[100002]; + +vector tlinks[100002]; + +stack order; + +bool visited[100002]; + + + +vector components[100002]; + +int nr; + + + +void dfs( int node ){ + + visited[node] = true; + + for ( auto &x : links[node] ){ + + if ( !visited[x] ) + + dfs(x); + + } + + order.push(node); + +} + + + +void tdfs( int node ){ + + components[nr].push_back(node); + + visited[node] = false; + + for ( auto &x : tlinks[node] ){ + + if ( visited[x] ) + + tdfs(x); + + } + +} + + + +int main(){ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + fin >> N >> M; + + + + int a, b; + + for ( int i = 0; i < M; i++ ){ + + fin >> a >> b; + + links[a].push_back(b); + + tlinks[b].push_back(a); + + } + + + + for ( int i = 1; i <= N; i++ ){ + + if ( !visited[i] ) + + dfs(i); + + } + + + + while(!order.empty()){ + + int x = order.top(); + + + + if ( visited[x] ){ + + nr++; + + tdfs(x); + + } + + + + order.pop(); + + } + + + + fout << nr << '\n'; + + + + for ( int i = 1; i<= nr; i++ ){ + + for ( auto &x : components[i] ){ + + fout << x << ' '; + + } + + + + fout << "\n"; + + } + + + +} diff --git a/scc/kosaraju/995c729c-5f40-4060-96a4-7ef315e74afd.cpp b/scc/kosaraju/995c729c-5f40-4060-96a4-7ef315e74afd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8674d02fa5c6c309e7f18a8e89c4d87049e1a07b --- /dev/null +++ b/scc/kosaraju/995c729c-5f40-4060-96a4-7ef315e74afd.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +using namespace std; +ifstream f("ctc.in"); +ofstream g("ctc.out"); +vectorgf[100005],gft[100005],sol[100005]; +stackst; +int n,m,x,y,cnt; +bool use[100005]; +void dfs(int nod) +{ + use[nod]=1; + int lg=gf[nod].size(); + for(int i=0;i>n>>m; + for(int i=1;i<=m;i++) + { + f>>x>>y; + gf[x].push_back(y); + gft[y].push_back(x); + } + for(int i=1;i<=n;i++) + if(use[i]==0)dfs(i); + while(!st.empty()) + { + int nod=st.top(); + st.pop(); + if(use[nod]) + { + cnt++; + dfst(nod); + } + } + g< +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +void top_sort(int node, vector &vis, vector> &g, vector &sorted) { + + vis[node] = 1; + + for(auto it : g[node]) + + if(!vis[it]) + + top_sort(it, vis, g, sorted); + + sorted.push_back(node); + +} + + + +int dfs(int node, vector &vis, vector> &g, vector &component) { + + vis[node] = 1; + + component.push_back(node); + + for(auto it : g[node]) + + if(!vis[it]) + + dfs(it, vis, g, component); + +} + + + +int main() { + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + + + int n, m; + + in >> n >> m; + + vector> g(n + 1); + + vector> gT(n + 1); + + for(int i = 1; i <= m; i ++) { + + int x, y; + + in >> x >> y; + + g[x].push_back(y); + + gT[y].push_back(x); + + } + + + + vector vis(n + 1, 0); + + vector top_sorted; + + for(int i = 1; i <= n; i ++) + + if(!vis[i]) + + top_sort(i, vis, g, top_sorted); + + reverse(top_sorted.begin(), top_sorted.end()); + + + + for(int i = 1; i <= n; i ++) + + vis[i] = 0; + + vector> ans; + + vector aux; + + for(auto i : top_sorted) { + + if(!vis[i]) { + + aux.clear(); + + dfs(i, vis, gT, aux); + + reverse(aux.begin(), aux.end()); + + ans.push_back(aux); + + } + + } + + out << ans.size() << "\n"; + + for(auto it : ans) { + + for(auto it2 : it) + + out << it2 << " "; + + out << "\n"; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/99d85a88-813e-4916-9a27-e94fdd6cbc0d.cpp b/scc/kosaraju/99d85a88-813e-4916-9a27-e94fdd6cbc0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faaf3ef7a58892d9fd2a9e19b7356fe6d24fb048 --- /dev/null +++ b/scc/kosaraju/99d85a88-813e-4916-9a27-e94fdd6cbc0d.cpp @@ -0,0 +1,64 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m, x, y, fr[100005]; +vector a[100005]; +vector tr[100005]; +vector s; +set comp[100005]; + +int r; + +void dfs(int nod) { + fr[nod] = 1; + + for(int vecin: a[nod]) + if (!fr[vecin]) + dfs(vecin); + + s.push_back(nod); +} + +void Tdfs(int nod) { + fr[nod] = 1; + comp[r].insert(nod); + + for(int vecin: tr[nod]) + if (!fr[vecin]) + Tdfs(vecin); +} + +int main() +{ + fin >> n >> m; + + while (fin >> x >> y) { + a[x].push_back(y); + tr[y].push_back(x); + } + + for (int i=1;i<=n;++i) + if (!fr[i]) + dfs(i); + + reverse(s.begin(), s.end()); + + memset(fr, 0, sizeof(fr)); + + for (int nod: s) + if (!fr[nod]) { + r++; + Tdfs(nod); + } + + fout << r << "\n"; + + for (int i=1;i<=r;++i, fout << "\n") + for (int it: comp[i]) + fout << it << " "; +} diff --git a/scc/kosaraju/9c53d89b-a3b0-40b6-b718-c65c39a5bcb8.cpp b/scc/kosaraju/9c53d89b-a3b0-40b6-b718-c65c39a5bcb8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddbd5f9536ec4e526f3acf8a1019922f0d8648d2 --- /dev/null +++ b/scc/kosaraju/9c53d89b-a3b0-40b6-b718-c65c39a5bcb8.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int N, M; + +stack S; + +vector adiac[100005]; + +vector inv_adiac[100005]; + +vector ctc[100005]; + +int nr_ctc; + +char vizitat[100005]; + + + +void DFSP(vector m[], int nod) + +{ + + int i; + + + + vizitat[nod] = 1; + + + + for(i = 0; i < m[nod].size(); i++) + + if(vizitat[m[nod][i]] != 1) + + DFSP(m, m[nod][i]); + + + + S.push(nod); + +} + + + +void DFSS(vector m[], int nod) + +{ + + int i; + + + + vizitat[nod] = 2; + + ctc[nr_ctc].push_back(nod); + + + + for(i = 0; i < m[nod].size(); i++) + + if(vizitat[m[nod][i]] != 2) + + DFSS(m, m[nod][i]); + +} + + + +int main() + +{ + + int x, y, i, j; + + + + in >> N >> M; + + for(i = 0; i < M; i++) { + + in >> x >> y; + + + + adiac[x].push_back(y); + + inv_adiac[y].push_back(x); + + } + + + + for(i = 1; i <= N; i++) + + if(!vizitat[i]) + + DFSP(inv_adiac, i); + + + + while(!S.empty()) { + + if(vizitat[S.top()] == 1) { + + DFSS(adiac, S.top()); + + nr_ctc++; + + } + + S.pop(); + + } + + + + out << nr_ctc << '\n'; + + for(i = 0; i < nr_ctc; i++) { + + for(j = 0; j < ctc[i].size(); j++) + + out << ctc[i][j] << ' '; + + out << '\n'; + + } + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/scc/kosaraju/9cc3d1ba-208e-4ea9-b59b-296c203d86bc.cpp b/scc/kosaraju/9cc3d1ba-208e-4ea9-b59b-296c203d86bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc52a4694112e8373ee0b6a227bf216cf9e33af3 --- /dev/null +++ b/scc/kosaraju/9cc3d1ba-208e-4ea9-b59b-296c203d86bc.cpp @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m; + +vector gra[100041]; + +vector tra[100041]; + + + +bool vi[100041]; + +stack sta; + +void defeseta(int a){ + + vi[a] = true; + + for(auto b : gra[a]){ + + if(!vi[b])defeseta(b); + + } + + sta.push(a); + +} + + + +vector> ans; + +void defetista(int a, vector &v){ + + vi[a] = false; + + v.push_back(a); + + for(auto b : tra[a]){ + + if(vi[b])defetista(b, v); + + } + +} + + + +int main(){ + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + int a, b;fin>> a >> b; + + gra[a].push_back(b); + + tra[b].push_back(a); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(!vi[i])defeseta(i); + + } + + while(!sta.empty()){ + + if(vi[sta.top()]){ + + ans.push_back({}); + + defetista(sta.top(), ans.back()); + + } + + sta.pop(); + + } + + + + fout << ans.size() << "\n"; + + for(auto v : ans){ + + for(auto a : v){ + + fout << a << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/9d2c23cb-73d0-4011-a123-7a1fae086e08.cpp b/scc/kosaraju/9d2c23cb-73d0-4011-a123-7a1fae086e08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ed7830b87c4c6314e5f6d7c42ea9d6b89b0cacd --- /dev/null +++ b/scc/kosaraju/9d2c23cb-73d0-4011-a123-7a1fae086e08.cpp @@ -0,0 +1,196 @@ +#include + + +using namespace std; + + + +const int dim = 100100; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int n, m, nrc, st[dim], vf, viz[dim]; + +vector g[dim]; + +vector gt[dim]; + +vector cmp[dim]; + + + +void push(int x) + +{ + + if(vf 0) + + return st[vf--]; + +} + + + +void read() + +{ + + in>>n>>m; + + int x, y; + + for(int i = 1;i <= m;i++) + + { + + in>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs1(int st) + +{ + + viz[st] = 1; + + for(vector::iterator it = g[st].begin(); it != g[st].end();it++) + + { + + if(!viz[*(it)]) + + dfs1(*(it)); + + } + + push(st); + +} + + + +void dfs2(int st) + +{ + + viz[st] = 1; + + for(vector::iterator it = gt[st].begin(); it != gt[st].end();it++) + + { + + if(!viz[*(it)]) + + dfs2(*(it)); + + } + + cmp[nrc].push_back(st); + +} + + + +void solve() + +{ + + vf = 0; + + for(int i = 1;i <= n;i++) + + { + + if(!viz[i]) + + dfs1(i); + + } + + for(int i = 1;i <= n;i++) + + viz[i] = 0; + + while(vf) + + { + + int curent = pop(); + + if(!viz[curent]) + + { + + nrc++; + + dfs2(curent); + + } + + } + +} + + + +void print() + +{ + + out<::iterator it = cmp[i].begin(); it != cmp[i].end();it++) + + out<<*(it)<<' '; + + out<<'\n'; + + } + +} + + + +int main() + +{ + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/scc/kosaraju/9e073ba9-6eda-45f7-aa84-8e4dc3202595.cpp b/scc/kosaraju/9e073ba9-6eda-45f7-aa84-8e4dc3202595.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9294b260c08ef15e1c2ee5fb3ece63fe65c66a20 --- /dev/null +++ b/scc/kosaraju/9e073ba9-6eda-45f7-aa84-8e4dc3202595.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector G[100005]; + +vector GT[100005]; + +stack postordine; + +vector Ctc[100005]; + +int viz[100005]; + +int n,m,X,NrCtc; + + + +void citire() + +{ + + fin>>n>>m; + + for(int i=0,x,y; i>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS(int X) + +{ + + viz[X]=1; + + int N=G[X].size(); + + for(int i=0; i + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +list topoSort; + +vector < vector > la; + +vector < vector > li; + +vector < vector > componenta; + +int n,m,nrComponente; + +bool used[100002]; + + + +void citire() + +{ + + f >> n >> m; + + la.resize(n+1); + + li.resize(n+1); + + for(int i=1;i<=m;++i) + + { + + int x,y; + + f >> x >> y; + + la[x].push_back(y); + + li[y].push_back(x); + + } + +} + + + +void dfs(int x) + +{ + + used[x] = 1; + + for(int i : la[x]) + + if(!used[i])dfs(i); + + topoSort.push_front(x); + +} + + + +void puneInComponenta(int x,int nrComp) + +{ + + componenta[nrComp].push_back(x); + + used[x] = 1; + + for(int i : li[x]) + + if(!used[i]) + + puneInComponenta(i,nrComp); + +} + + + +void Kosaraju() + +{ + + for(int i=1;i<=n;++i) + + if(!used[i])dfs(i); + + + + memset(used,0,sizeof(used)); + + componenta.resize(n+1); + + + + for(int i : topoSort) + + if(!used[i])puneInComponenta(i,nrComponente++); + + + + componenta.resize(nrComponente+1); + + g << nrComponente << '\n'; + + for(vector i : componenta) + + { + + for(int j : i)g << j << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citire(); + + Kosaraju(); + +} diff --git a/scc/kosaraju/9e9fe4e1-f9fb-42a0-8294-2702742a5f3e.cpp b/scc/kosaraju/9e9fe4e1-f9fb-42a0-8294-2702742a5f3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d028823f88ac950cc15c00e1f61314c505b2ab56 --- /dev/null +++ b/scc/kosaraju/9e9fe4e1-f9fb-42a0-8294-2702742a5f3e.cpp @@ -0,0 +1,120 @@ +#include + + +using namespace std; + + + +const int len = 100005; + +int n, m, x, y, cnt; + +vector g[len], gt[len], sol[len], v; + +bitset seen; + + + +void dfs1(int curr) { + + seen[curr] = true; + + for (auto& it : g[curr]) + + if (!seen[it]) + + dfs1(it); + + v.push_back(curr); + +} + + + +void dfs2(int curr) { + + seen[curr] = true; + + for (auto& it : gt[curr]) + + if (!seen[it]) + + dfs2(it); + + sol[cnt].push_back(curr); + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + cin.tie(0); + + cout.tie(0); + + + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + + cin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + if (!seen[i]) + + dfs1(i); + + + + for (auto& it : v) + + seen[it] = false; + + + + reverse(v.begin(), v.end()); + + for (auto& it : v) + + if (!seen[it]) { + + cnt++; + + dfs2(it); + + } + + + + cout << cnt << "\n"; + + for (int i = 1; i <= cnt; i++) { + + for (auto& it : sol[i]) + + cout << it << " "; + + cout << "\n"; + + } + +} diff --git a/scc/kosaraju/9efdad13-d118-479f-b80f-44c741ddec43.cpp b/scc/kosaraju/9efdad13-d118-479f-b80f-44c741ddec43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e52ed77a7c3bebe9edc6c356717703af6c76815 --- /dev/null +++ b/scc/kosaraju/9efdad13-d118-479f-b80f-44c741ddec43.cpp @@ -0,0 +1,187 @@ +#include +#include + + +using namespace std; + +const int NMAX = 100000; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +struct nod + +{ + + int x; + + nod *next; + +}; + + + +nod *G[NMAX + 1], + + *GT[NMAX + 1], + + *CTC[NMAX + 1]; + + + +int N, M, nrc, nr, post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +void add(nod *&cap_lst, int nod_ad) + +{ + + nod*p; + + p = new nod; + + p->x = nod_ad; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() + +{ + + f >> N >> M; + + while(M--) + + { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(nod *p = G[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 0) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) + +{ + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for(nod *p = GT[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 1) + + DFSt(p->x); + +} + + + +void componente() + +{ + + int i; + + for(i = 1; i <= N; i++) + + if(viz[i] == 0) + + DFS(i); + + + + for(int i = N; i >= 1; i--) + + if(viz[post[i]] == 1) + + { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; i++) + + { + + for(nod *p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citireGraf(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/9f2beebb-8f0b-42d2-aeb2-baea8de55133.cpp b/scc/kosaraju/9f2beebb-8f0b-42d2-aeb2-baea8de55133.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db7b38dbf133dd632c203199593000f429bbfe5a --- /dev/null +++ b/scc/kosaraju/9f2beebb-8f0b-42d2-aeb2-baea8de55133.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector G[100010],GT[100010],comp[100010]; + +int n, m, nrcomp, nr, ord[100010]; + +bitset <100010> viz; + + + +void DFS(int x){ + + viz[x]=true; + + for(int i=0;i>n>>m; + + for(int i=1,x,y;i<=m;++i){ + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for(int i=1;i<=n;++i) + + if(!viz[i])DFS(i); + + + + for(int i=n;i>0;--i) + + if(viz[ord[i]]){ + + nrcomp++; + + DFST(ord[i]); + + } + + + + g< +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector > w, w2, ctc; + +bool viz[100002]; + +int ap[100002]; + +int N, M, k, nrctc; + + + +void dfs1(int x) + +{ + + viz[x] = 1; + + + + for(int i = 0; i < w[x].size(); i++) + + { + + if(!viz[w[x][i]]) + + dfs1(w[x][i]); + + } + + k++; + + ap[k] = x; + +} + + + +void dfs2(int x) + +{ + + viz[x] = 0; + + ctc[nrctc].push_back(x); + + + + for(int i = 0; i < w2[x].size(); i++) + + { + + if(viz[w2[x][i]]) + + dfs2(w2[x][i]); + + } + +} + + + +int main() + +{ + + f>> N >> M; + + + + w.resize(N + 2); + + w2.resize(N + 2); + + ctc.push_back({}); + + + + int x, y; + + + + for(int i = 1; i <= M; i++) + + { + + f >> x >> y; + + w[x].push_back(y); + + w2[y].push_back(x); + + } + + + + for(int i = 1; i <= N; i++) + + if(!viz[i]) + + dfs1(i); + + + + for(int i = N; i >= 1; i--) + + { + + if(viz[ap[i]] == 1) + + { + + nrctc++; + + ctc.push_back({}); + + dfs2(ap[i]); + + } + + } + + + + g << nrctc << '\n'; + + + + for(int i = 1; i <= nrctc; i++) + + { + + for(int j = 0; j < ctc[i].size(); j++) + + g << ctc[i][j] << ' '; + + g << '\n'; + + } + + + +} diff --git a/scc/kosaraju/a1369bbe-1af2-45b2-bbe6-486cbbfafabf.cpp b/scc/kosaraju/a1369bbe-1af2-45b2-bbe6-486cbbfafabf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d04038e3fdf4fddca6f367a005759526e80361f --- /dev/null +++ b/scc/kosaraju/a1369bbe-1af2-45b2-bbe6-486cbbfafabf.cpp @@ -0,0 +1,189 @@ +#include +#include +using namespace std; + + + +const int NMAX = 100000; + + + +struct nod + +{ + + int x; + + nod*next; + +}; + + + +nod *G[NMAX + 1], + + *GT[NMAX + 1], + + *CTC[NMAX + 1]; + + + +int N, M, nrc, + + nr, post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +void add(nod *&cap_lst, int nod_ad) + +{ + + nod*p; + + p = new nod; + + p->x = nod_ad; + + p->next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() + +{ + + f >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(nod*p = G[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 0) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFST(int vf) + +{ + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for(nod*p = GT[vf]; p != NULL; p = p->next) + + if(viz[p->x] == 1) + + DFST(p->x); + +} + + + +void componente() + +{ + + int i; + + for(int i = 1; i <= N; i++) + + if(!viz[i]) + + DFS(i); + + + + for(int i = N; i >= 1; i--) + + if(viz[post[i]] == 1) + + { + + nrc++; + + DFST(post[i]); + + } + +} + + + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; i++) + + { + + for(nod*p = CTC[i]; p != NULL; p = p->next) + + g << p->x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citireGraf(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/a15d0d96-a906-4e58-b4a9-d5c6388060c5.cpp b/scc/kosaraju/a15d0d96-a906-4e58-b4a9-d5c6388060c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a491f4cee4751271207495dace9646544fd961a --- /dev/null +++ b/scc/kosaraju/a15d0d96-a906-4e58-b4a9-d5c6388060c5.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vector InitGraph[100005], ReverseGraph[100005]; + +vector StrongComponents[100005]; + +stack Stack; + +int N, M, strongComponents; + +bool visited[100005]; + + + +void FirstDFS(int node) + +{ + + int currentNode; + + visited[node] = true; + + for(int i = 0; i < InitGraph[node].size(); i++){ + + currentNode = InitGraph[node][i]; + + if(visited[currentNode] == false){ + + FirstDFS(currentNode); + + } + + } + + Stack.push(node); + +} + + + +void SecondDFS(int node) + +{ + + int currentNode; + + visited[node] = true; + + StrongComponents[strongComponents].push_back(node); + + for(int i = 0; i < ReverseGraph[node].size(); i++){ + + currentNode = ReverseGraph[node][i]; + + if(!visited[currentNode]){ + + SecondDFS(currentNode); + + } + + } + +} + + + +int main() + +{ + + int nodeX, nodeY, currentNode; + + cin >> N >> M; + + for(int i = 1; i <= M; i++){ + + cin >> nodeX >> nodeY; + + InitGraph[nodeX].push_back(nodeY); + + ReverseGraph[nodeY].push_back(nodeX); + + } + + for(int i = 1; i <= N; i++){ + + if(!visited[i]){ + + FirstDFS(i); + + } + + } + + memset(visited,false,sizeof(visited)); + + while(!Stack.empty()){ + + currentNode = Stack.top(); + + if(visited[currentNode] == false){ + + ++strongComponents; + + SecondDFS(currentNode); + + } + + Stack.pop(); + + } + + cout << strongComponents << "\n"; + + for(int i = 1; i <= strongComponents; i++){ + + for(int j = 0; j < StrongComponents[i].size(); j++){ + + cout << StrongComponents[i][j] << " "; + + } + + cout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/a31696c4-0e66-441e-8e7f-10a5d3898358.cpp b/scc/kosaraju/a31696c4-0e66-441e-8e7f-10a5d3898358.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5f34855a87ceb77c234b476eedaa9a9b06cdad5 --- /dev/null +++ b/scc/kosaraju/a31696c4-0e66-441e-8e7f-10a5d3898358.cpp @@ -0,0 +1,118 @@ +#include +using namespace std; + +int viz[100001],v[100001]; + +vector G[100001],GT[100001],L[100001]; + +int mn[100001]; + +int loc; + +int nrc; + +int DFS(int x) + +{ + + viz[x]=1; + + for(auto i:G[x]) + + if(viz[i]==0) DFS(i); + + loc++; + + v[loc]=x; + +} + +int DFS2(int x) + +{ + + viz[x]=nrc; + + L[nrc-1].push_back(x); + + for(auto i:GT[x]) + + if(viz[i]==1) DFS2(i); + +} + + + +int main() + +{ + + int n,m,i,x,y,mini,j,ok; + + loc=0; + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(i=1;i<=n;i++) + + if(viz[i]==0) + + { + + DFS(i); + + } + + nrc=1; + + for(i=loc;i>=1;i--) + + if(viz[v[i]]==1) + + { + + nrc++; + + DFS2(v[i]); + + } + + nrc--; + + g< + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + + + + + +int n, m, q; + +int k, DUD[100001 + 5]; + +bool viz[100001]; + +vector V1[100001]; + +vector V2[100001]; + +vector ctc[100001]; + + + +void DFS1(int nod) + +{ + + viz[nod] = 1; + + + + for(auto it : V1[nod]) + + if(!viz[it]) + + DFS1(it); + + + + DUD[++k] = nod; + +} + + + +void DFS2(int nod) + +{ + + viz[nod] = 0; + + ctc[q].push_back(nod); + + + + for(auto it : V2[nod]) + + if(viz[it] == 1) + + DFS2(it); + +} + + + +int main() + +{ + + fin >> n >> m; + + for(int i = 1; i <= m; i++) { + + + + int x, y; + + fin >> x >> y; + + V1[x].push_back(y); + + V2[y].push_back(x); + + + + } + + + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS1(i); + + + + for(int i = n; i >= 1; i--) + + if(viz[DUD[i]] == 1) + + { + + q++; + + DFS2(DUD[i]); + + } + + + + fout << q << '\n'; + + for(int i = 1; i <= q; i++) + + { + + for(auto it : ctc[i]) + + fout << it << ' '; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/a46b6fa0-54b0-4175-a66c-8c87c791723b.cpp b/scc/kosaraju/a46b6fa0-54b0-4175-a66c-8c87c791723b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf18976a0f9a4f40e8da83eac81fda0bfb0ac83d --- /dev/null +++ b/scc/kosaraju/a46b6fa0-54b0-4175-a66c-8c87c791723b.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream got ("ctc.out"); + + + + + + + +vector g[100005],gt[100005],rez[100005]; + +stack s; + + + +unsigned int n,m,nr_ctc; + + + +unsigned int vizitat[100005]; + + + +void read() + +{ + + fin>>n>>m; + + int x,y; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs(int nod) + +{ + + vizitat[nod]=1; + + unsigned int i,vecin; + + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector G[100005],Gt[100005]; + +vector ctc[100005]; + +stack s; + +int n,m,x,y; + +int viz[100005]; + +int cnt; + +void DFS_T(int nod_curent) + +{ + + viz[nod_curent]++; + + ctc[cnt].push_back(nod_curent); + + for(vector::iterator nod_vecin=Gt[nod_curent].begin(); nod_vecin!=Gt[nod_curent].end(); ++nod_vecin) + + if(viz[*nod_vecin]==1) + + DFS_T(*nod_vecin); + +} + +void DFS(int nod_curent) + +{ + + viz[nod_curent]=true; + + + + for(vector::iterator nod_vecin=G[nod_curent].begin(); nod_vecin!=G[nod_curent].end(); ++nod_vecin) + + if(!viz[*nod_vecin]) + + DFS(*nod_vecin); + + + + s.push(nod_curent); + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + Gt[y].push_back(x); + + } + + + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + DFS(i); + + + + while(!s.empty()) + + { + + int Nod=s.top(); + + + + if(viz[Nod]==1) + + { + + cnt++; + + DFS_T(Nod); + + } + + s.pop(); + + } + + + + g<::iterator it=ctc[i].begin(); it!=ctc[i].end(); ++it) + + g<<*it <<" "; + + + + g<<'\n'; + + } + + + + + + return 0; + +} diff --git a/scc/kosaraju/a7bc5e2d-e32d-4ac6-8351-974376984197.cpp b/scc/kosaraju/a7bc5e2d-e32d-4ac6-8351-974376984197.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f69b1b8cdd39532e6fafe879111243a93b5ac63 --- /dev/null +++ b/scc/kosaraju/a7bc5e2d-e32d-4ac6-8351-974376984197.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stack S; + +vector V[100005],Copie,V1[100005]; + +int n,m; + +bitset <100005> viz; + +vector > Sol; + +void dfs(int x) + +{ + + viz[x]=true; + + for(int i=0; i>n>>m; + + while(m--) + + { + + int x,y; + + f>>x>>y; + + V[x].push_back(y); + + V1[y].push_back(x); + + } + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs(i); + + for(; !S.empty(); S.pop()) + + if(viz[S.top()]) + + { + + dfs2(S.top()); + + Sol.push_back(Copie); + + Copie.clear(); + + } + + g< +#include +using namespace std; +const int mmax = 200000; +const int nmax = 100000; + +int nr,ctc,poz,inside; +int lst[nmax], lst_inv[nmax]; +int urm[mmax], urm_inv[mmax]; +int vf[mmax], vf_inv[mmax]; +bool visited[nmax], visited_inv[nmax]; +int top[nmax], componenta[nmax], noduri[nmax]; + +void adauga(int x, int y){ + nr++; + vf[nr] = y; + urm[nr] = lst[x]; + lst[x] = nr; + + vf_inv[nr] = x; + urm_inv[nr] = lst_inv[y]; + lst_inv[y] = nr; +} + +void dfs(int node){ + visited[node] = true; + for(int p=lst[node]; p!=0; p=urm[p]){ + int next = vf[p]; + if(!visited[next]) + dfs(next); + } + top[++nr] = node; +} + +void dfs_inv(int node){ + visited_inv[node] = true; + for(int p=lst_inv[node]; p!=0; p=urm_inv[p]){ + int next = vf_inv[p]; + if(!visited_inv[next]) + dfs_inv(next); + } + componenta[++poz] = node; + inside++; +} + +int main() +{ + FILE *fin, *fout; + int n,m,i,x,y; + fin = fopen("ctc.in","r"); + fout = fopen("ctc.out","w"); + fscanf(fin,"%d %d",&n,&m); + for(i=0; i0; i--) + if(!visited_inv[top[i]]){ + inside = 0; + ctc++; + dfs_inv(top[i]); + noduri[ctc] = inside; + } + fprintf(fout,"%d\n",ctc); + int j=1; + for(i=1; i<=poz; i++){ + if(noduri[j] == 0){ + j++; + fprintf(fout,"\n"); + } + fprintf(fout,"%d ",componenta[i]); + noduri[j]--; + } + fclose(fin); + fclose(fout); + return 0; +} diff --git a/scc/kosaraju/a94c70f9-b6e7-4cdc-88b4-718f2fe47ec5.cpp b/scc/kosaraju/a94c70f9-b6e7-4cdc-88b4-718f2fe47ec5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f276a7e3b38eee394d3c1bf8c7de20f1ef03622 --- /dev/null +++ b/scc/kosaraju/a94c70f9-b6e7-4cdc-88b4-718f2fe47ec5.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +map > components; + +void dfs(int node, vector& uz, vector >&g, stack& nodes); + +void dft(int node, vector& uz, vector >&g, int color); + + + +int n, m;; + + + +int main() + +{ + + in >> n >> m; + + + + vector >gn (n + 1, vector()); + + vector >gt (n + 1, vector()); + + + + for(int i = 1; i<=m; ++i) + + { + + int x,y; + + in >> x >> y; + + gn[x].push_back(y); + + gt[y].push_back(x); + + } + + + + vector uz(n + 1, false); + + stack nodes; + + + + for(int i = 1; i<=n; ++i) + + { + + if(!uz[i]) + + { + + dfs(i, uz, gn, nodes); + + } + + } + + + + int color = 0; + + while(nodes.size()) + + { + + int node = nodes.top(); + + nodes.pop(); + + + + if(uz[node]) + + { + + ++color; + + dft(node, uz, gt, color); + + } + + } + + + + out << color << '\n'; + + for(int i = 1; i<=color; ++i) + + { + + vector component = components[i]; + + for(int j = 0; j < component.size(); ++j) + + out << component[j] << " "; + + + + out << '\n'; + + } + + + + + + return 0; + +} + + + +void dfs(int node, vector& uz, vector >&g, stack& nodes) + +{ + + uz[node] = true; + + for(vector:: iterator it = g[node].begin(); it != g[node].end(); ++it) + + { + + if(!uz[*it]) + + dfs(*it, uz, g, nodes); + + } + + + + nodes.push(node); + +} + + + +void dft(int node, vector& uz, vector >&g, int color) + +{ + + uz[node] = false; + + components[color].push_back(node); + + for(vector:: iterator it = g[node].begin(); it != g[node].end(); ++it) + + { + + if(uz[*it]) + + { + + dft(*it, uz, g, color); + + } + + } + +} diff --git a/scc/kosaraju/aa3b0f51-ea77-4cc8-8554-a4e0a8a824a3.cpp b/scc/kosaraju/aa3b0f51-ea77-4cc8-8554-a4e0a8a824a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e3cc71a8519a4463b895067ac628eeb38d05aa --- /dev/null +++ b/scc/kosaraju/aa3b0f51-ea77-4cc8-8554-a4e0a8a824a3.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +const int N = 100010; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vector v[N]; + +vector inv[N]; + + + +vector answer[N]; + +stack stiva; + +int n, m, k, viz[N]; + + + +void DFS(int), DFST(int); + +bool myfunction(vector a, vector b) + +{ + + return a[0] < b[0]; + +} + +int main() + +{ + + f >> n >> m; + + for(; m; m--) + + { + + int x, y; + + f >> x >> y; + + v[x].push_back(y); + + inv[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS(i); + + while(!stiva.empty()) + + { + + int nr = stiva.top(); + + stiva.pop(); + + if(viz[nr] != 2) + + { + + ++k; + + DFST(nr); + + + + } + + } + + g << k << '\n'; + + for(int i = 1; i <= k; i++, g << '\n') + + for(auto it:answer[i]) + + g << it << ' '; + + + + return 0; + +} + + + +void DFS(int nod) + +{ + + viz[nod] = 1; + + for(auto it:v[nod]) + + if(!viz[it]) + + DFS(it); + + stiva.push(nod); + +} + +void DFST(int nod) + +{ + + viz[nod] = 2; + + answer[k].push_back(nod); + + for(auto it:inv[nod]) + + if(viz[it] != 2) + + DFST(it); + +} diff --git a/scc/kosaraju/ad0adcbc-c19d-4fcf-8b8a-421a011c0486.cpp b/scc/kosaraju/ad0adcbc-c19d-4fcf-8b8a-421a011c0486.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f17f739640617e627886b9c74d94ed62c9da81e --- /dev/null +++ b/scc/kosaraju/ad0adcbc-c19d-4fcf-8b8a-421a011c0486.cpp @@ -0,0 +1,125 @@ +#include +#include +#include + + +using namespace std; + +const int NMAX = 100000; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector > ad(NMAX + 5); + +vector > ad_t(NMAX + 5); + +vector > ans(NMAX + 5); + + + +bitset viz; + + + +int postordine[NMAX + 5], cnt; + + + +void DFS(int node) + +{ + + int i; + + viz[node] = 1; + + for(i = 0; i < ad[node].size(); i++) + + if(!viz[ad[node][i]]) + + DFS(ad[node][i]); + + postordine[++cnt] = node; + +} + + + +void DFS_t(int node, int comp) + +{ + + int i; + + viz[node] = 0; + + ans[comp].push_back(node); + + for(i = 0; i < ad_t[node].size(); i++) + + if(viz[ad_t[node][i]]) + + DFS_t(ad_t[node][i],comp); + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin.tie(NULL); + int n,m,i,j,a,b,tconx = 0; + + fin >> n >> m; + + while(m--) + + { + + fin >> a >> b; + + ad[a].push_back(b); + + ad_t[b].push_back(a); + + } + + for(i = 1; i <= n; i++) + + if(!viz[i]) + + DFS(i); + + for(i = n; i > 0; i--) + + if(viz[postordine[i]]) + + DFS_t(postordine[i],++tconx); + + fout << tconx << "\n"; + + for(i = 1; i <= tconx; i++) + + { + + for(j = 0; j < ans[i].size(); j++) + + fout << ans[i][j] << " "; + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/af16f1e6-642d-4f76-b876-cbb2ea2faa41.cpp b/scc/kosaraju/af16f1e6-642d-4f76-b876-cbb2ea2faa41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2048fb2a0ebb739a711162419cc9fe0a1285c574 --- /dev/null +++ b/scc/kosaraju/af16f1e6-642d-4f76-b876-cbb2ea2faa41.cpp @@ -0,0 +1,136 @@ +#include + + + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +stack < int > s; + +vector < int > v1[100005], v2[100005], r[100005]; + +bitset < 100005 > viz1, viz2; + +int nr; + + + +void dfs1 ( int nod ); + +void dfs2 ( int nod ); + + + +int main() + +{ + + int n, m, x, y, i, j; + + + + fin >> n >> m; + + while ( m-- ) + + { + + fin >> x >> y; + + v1[x].push_back ( y ); + + v2[y].push_back ( x ); + + } + + + + for ( i = 1 ; i <= n ; i++ ) if ( viz1[i] == 0 ) dfs1 ( i ); + + + + while ( s.empty() == 0 ) + + { + + if ( viz2[s.top()] == 0 ) + + { + + nr++; + + dfs2 ( s.top() ); + + } + + + + s.pop(); + + } + + + + fout << nr << '\n'; + + for ( i = 1 ; i <= nr ; i++ ) + + { + + for ( j = 0 ; j < r[i].size() ; j++ ) fout << r[i][j] << ' '; + + fout << '\n'; + + } + + + + return 0; + +} + + + +void dfs1 ( int nod ) + +{ + + int i; + + + + viz1[nod] = 1; + + for ( i = 0 ; i < v1[nod].size() ; i++ ) if ( viz1[v1[nod][i]] == 0 ) dfs1 ( v1[nod][i] ); + + + + s.push ( nod ); + +} + + + +void dfs2 ( int nod ) + +{ + + int i; + + + + viz2[nod] = 1; + + for ( i = 0 ; i < v2[nod].size() ; i++ ) if ( viz2[v2[nod][i]] == 0 ) dfs2 ( v2[nod][i] ); + + r[nr].push_back ( nod ); + +} diff --git a/scc/kosaraju/afa46127-c011-425c-8dd6-fb3967625485.cpp b/scc/kosaraju/afa46127-c011-425c-8dd6-fb3967625485.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7378a2b2941a855e5ad6b8ee9bd717a7078e5c9 --- /dev/null +++ b/scc/kosaraju/afa46127-c011-425c-8dd6-fb3967625485.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + + + +struct node { + + bool b, b2; + + vector in; + + vector out; + +}; + +vector g; + +vector> v; + +stack s; + + + +void d1(int n) { + + g[n].b=true; + + for(auto it:g[n].out) + + if(!g[it].b) + + d1(it); + + s.push(n); + +} + + + +void d2(int n) { + + v.back().push_back(n); + + g[n].b2=1; + + for(auto i:g[n].in) + + if(!g[i].b2) + + d2(i); + +} + + + +int main() { + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + int n, m; + + fin>>n>>m; + + g.resize(n+1); + + for(; m; m--) { + + int a, b; + + fin>>a>>b; + + g[a].out.push_back(b); + + g[b].in.push_back(a); + + } + + for(m=1; m<=n; m++) + + if(!g[m].b) + + d1(m); + + while(true) { + + v.push_back({}); + + while(!s.empty() && g[s.top()].b2) + + s.pop(); + + if(s.empty()) + + break; + + d2(s.top()); + + } + + fout< +#include + +using namespace std; + +vectorg[100000 + 1], scc[100000 + 1]; +int visitedTime[100000 + 1], lowTime[100000 + 1], st[100000 + 1], n, m, time, numSCC, vf; +bool onStack[100000 + 1]; + +inline int minim(int x, int y) { + if(x <= y) return x; + return y; +} + +void readGraph() { + int x, y; + FILE* fin = fopen("ctc.in","r"); + fscanf(fin,"%d%d",&n,&m); + for(int i = 0; i < m; i++) { + fscanf(fin,"%d%d",&x,&y); + g[x].push_back(y); + } + fclose(fin); +} + +void DFS(int node) { + vector::iterator it; + visitedTime[node] = lowTime[node] = ++time; + st[++vf] = node; + onStack[node] = true; + for(it = g[node].begin(); it != g[node].end(); it++) { + if(visitedTime[*it] == 0) { + DFS(*it); + lowTime[node] = minim(lowTime[node],lowTime[*it]); + } else if(onStack[*it]) + lowTime[node] = minim(lowTime[node],lowTime[*it]); + } + if(visitedTime[node] == lowTime[node]) { + do { + scc[numSCC].push_back(st[vf]); + onStack[st[vf]] = false; + vf--; + }while(st[vf+1] != node); + numSCC++; + } +} + +void DFS_master() { + for(int i = 1; i <= n; i++) + if(visitedTime[i] == 0) + DFS(i); +} + +void printSCC() { + int i, j, k; + FILE* fout = fopen("ctc.out","w"); + fprintf(fout,"%d\n",numSCC); + for(i = 0; i < numSCC; i++) { + k = scc[i].size(); + for(j = 0; j < k; j++) + fprintf(fout,"%d ",scc[i][j]); + fprintf(fout,"\n"); + } + fclose(fout); +} + +int main() { + readGraph(); + DFS_master(); + printSCC(); + return 0; +} diff --git a/scc/kosaraju/b1cd93e8-efed-4592-b6a5-2411d4907788.cpp b/scc/kosaraju/b1cd93e8-efed-4592-b6a5-2411d4907788.cpp new file mode 100644 index 0000000000000000000000000000000000000000..444a60c18fadc989795b82a38f83984eeeee1e3d --- /dev/null +++ b/scc/kosaraju/b1cd93e8-efed-4592-b6a5-2411d4907788.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + list ℴ + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void visit_post_order(int current_node, traversal_data &td); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + list order; + vector visited(n, false); + traversal_data td = {G, visited, order, 0, SCCs}; + + for (int i = 0; i < n; ++i) { + if (not visited[i]) visit_post_order(i, td); + } + + SCCs.resize(n); + traversal_data scc_td = {TG, visited, order, 0, SCCs}; + + while (not order.empty()) { + int node = order.front(); + order.pop_front(); + + if (visited[node]) { + get_scc(node, scc_td); + scc_td.SCC_index++; + } + } + + write_solution(scc_td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void visit_post_order(int current_node, traversal_data &td) { + td.visited[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) visit_post_order(neighbour, td); + } + td.order.push_front(current_node); +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = false; + td.SCCs[td.SCC_index].push_back(current_node); + + for (auto neighbour: td.G[current_node]) { + if (td.visited[neighbour]) get_scc(neighbour, td); + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/kosaraju/b1ef46cd-7f8d-4ae1-b16f-45bef17cd1ca.cpp b/scc/kosaraju/b1ef46cd-7f8d-4ae1-b16f-45bef17cd1ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d78c1d5648c593c094bc733fdefa36dc1bd74f6 --- /dev/null +++ b/scc/kosaraju/b1ef46cd-7f8d-4ae1-b16f-45bef17cd1ca.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + + + +vector > G, GT,sol; + + + +int n, m; + + + +vector V; + +vector S,ans; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void read() + +{ + + fin >> n >> m; + + G = GT = vector>(n + 1); + + for (int i = 1; i <= m; i++) + + { + + int a, b; + + fin >> a >> b; + + G[a].push_back(b); + + GT[b].push_back(a); + + } + + + +} + + + +void dfs(int k) + +{ + + V[k] = true; + + for (auto x : G[k]) + + if (!V[x]) + + dfs(x); + + S.push_back(k); + +} + + + +void dfsGT(int k) + +{ + + V[k] = 1; + + for (auto x : GT[k]) + + if (!V[x]) + + dfsGT(x); + + ans.push_back(k); + + + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + fin.tie(0); + + read(); + + + + V = vector(n + 1, false); + + for (int i = 1; i <= n; i++) + + if (!V[i]) + + { + + dfs(i); + + + + } + + + + V = vector(n + 1, false); + + for (vector::reverse_iterator it = S.rbegin(); it != S.rend(); it++) + + if (!V[*it]) { + + ans.clear(); + + dfsGT(*it); + + sol.push_back(ans); + + + + } + + + + fout << sol.size()<<'\n'; + + for (auto it : sol) + + { + + for (auto ij : it) + + fout << ij << " "; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/b28bae6b-abe7-4da3-9687-7e8f7b12e821.cpp b/scc/kosaraju/b28bae6b-abe7-4da3-9687-7e8f7b12e821.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16fcf39919f811557136f13e6693542e3a51390c --- /dev/null +++ b/scc/kosaraju/b28bae6b-abe7-4da3-9687-7e8f7b12e821.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,Nr,viz[100001]; + +vectorG[100001],GT[100001],sol[100001]; + +stackS; + + + +void dfs1(int nod) + +{ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + { + + if(!viz[i])dfs1(i); + + } + + while(!S.empty()) + + { + + int Nod=S.top(); + + if(viz[Nod]==1) + + { + + Nr++; + + dfs2(Nod); + + } + + S.pop(); + + } + + + + g< + + + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +stack < int > s; + +vector < int > v1[100005], v2[100005], R[100005]; + +bitset < 100005 > viz1, viz2; + +int nr; + + + +void dfs1 ( int nod ); + +void dfs2 ( int nod ); + + + +int main() + +{ + + int n, m, x, y, i, j; + + + + fin >> n >> m; + + while ( m-- ) + + { + + fin >> x >> y; + + v1[x].push_back ( y ); + + v2[y].push_back ( x ); + + } + + + + for ( i = 1 ; i <= n ; i++ ) if ( viz1[i] == 0 ) dfs1 ( i ); + + + + while ( s.empty() == 0 ) + + { + + x = s.top(); + + if ( viz2[x] == 0 ) + + { + + nr++; + + dfs2 ( x ); + + } + + + + s.pop(); + + } + + + + fout << nr << '\n'; + + for ( i = 1 ; i <= nr ; i++ ) + + { + + for ( j = 0 ; j < R[i].size() ; j++ ) fout << R[i][j] << ' '; + + fout << '\n'; + + } + + + + return 0; + +} + + + +void dfs1 ( int nod ) + +{ + + int i; + + + + viz1[nod] = 1; + + for ( i = 0 ; i < v1[nod].size() ; i++ ) if ( viz1[v1[nod][i]] == 0 ) dfs1 ( v1[nod][i] ); + + s.push ( nod ); + +} + + + +void dfs2 ( int nod ) + +{ + + int i; + + + + viz2[nod] = 1; + + for ( i = 0 ; i < v2[nod].size() ; i++ ) if ( viz2[v2[nod][i]] == 0 ) dfs2 ( v2[nod][i] ); + + R[nr].push_back ( nod ); + +} diff --git a/scc/kosaraju/b322df68-09f5-4a54-9cb2-84381f80c438.cpp b/scc/kosaraju/b322df68-09f5-4a54-9cb2-84381f80c438.cpp new file mode 100644 index 0000000000000000000000000000000000000000..688e3b59f7979194dfe6da9dd19c85e81d3f9992 --- /dev/null +++ b/scc/kosaraju/b322df68-09f5-4a54-9cb2-84381f80c438.cpp @@ -0,0 +1,167 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector compcon[100009]; + +vector v[100009]; + +vector t[100009]; + +int viz[100009],nr; + +stacks; + + + +void dfs(int n) + +{ + + viz[n] = 1; + + + + for(int i=0; i>n>>m; + + + + for(int i =1; i <=m ; i++) + + { + + f>>x>>y; + + + + t[y].push_back(x); + + + + v[x].push_back(y); + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i]==0) + + dfs(i); + + } + + + + while(s.empty()==0) + + { + + int n=s.top(); + + + + if(viz[n]==1) + + { + + nr++; + + dfs_t(n); + + } + + + + s.pop(); + + } + + + + g< + + + + +using namespace std; + + + +ifstream f ("ctc.in"); + +ofstream g ("ctc.out"); + + + +int n, m, x, y, viz[100005], nr; + +vector < int > G[100005], GT[100005], CTC[100005]; + +stack < int > S; + + + +void read () { + + f >> n >> m; + + while (m --) { + + f >> x >> y; + + G[x].push_back (y); + + GT[y].push_back (x); + + } + +} + + + +void DFSP (int k) { + + viz[k] = 1; + + for (unsigned int i = 0; i < G[k].size(); ++i) { + + int vecin = G[k][i]; + + if (!viz[vecin]) DFSP (vecin); + + } + + S.push (k); + +} + + + +void DFSM (int k) { + + viz[k] = 2; + + CTC[nr].push_back (k); + + for (unsigned int i = 0; i < GT[k].size(); ++i) { + + int vecin = GT[k][i]; + + if (viz[vecin] == 1) DFSM (vecin); + + } + +} + + + +void solve () { + + for (int i = 1; i <= n; ++i) if (!viz[i]) DFSP (i); + + while (!S.empty()) { + + int nod = S.top(); + + S.pop(); + + if (viz[nod] == 1) { + + nr ++; + + DFSM (nod); + + } + + } + +} + + + +void print () { + + g << nr << '\n'; + + for (int i = 1; i <= nr; ++i, g << '\n') + + for (unsigned int j = 0; j < CTC[i].size(); ++j) + + g << CTC[i][j] << " "; + +} + + + +void Close () { + + f.close(); + + g.close(); + +} + + + +int main() + +{ + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/scc/kosaraju/b3798c93-8429-42a6-82a8-a3070944e1e9.cpp b/scc/kosaraju/b3798c93-8429-42a6-82a8-a3070944e1e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d073fdf02283ede65748d8ad7553f8ea56cd96ba --- /dev/null +++ b/scc/kosaraju/b3798c93-8429-42a6-82a8-a3070944e1e9.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +using VI = vector; +using VVI = vector; +using VB = vector; + +VVI G, GT; +VB v; +stack stk; + +VI c; +VVI ctc; + +int n, m; + +void CitesteGraf(); +void Kosaraju(); +void Df(int x); +void DfT(int x); +void AfiseazaCompTareConexe(); + + +int main() +{ + CitesteGraf(); + Kosaraju(); + AfiseazaCompTareConexe(); +} + +void CitesteGraf() +{ + fin >> n >> m; + G = GT = VVI(n + 1); + v = VB(n + 1); + int x, y; + for (int i = 1; i <= m; ++i) + { + fin >> x >> y; + G[x].emplace_back(y); + GT[y].emplace_back(x); + } +} + +void Kosaraju() +{ + for (int x = 1; x <= n; ++x) + if (!v[x]) + Df(x); + + v.assign(n + 1, false); + + int x; + while (!stk.empty()) + { + x = stk.top(); stk.pop(); + if (v[x]) continue; + c.clear(); + DfT(x); + ctc.emplace_back(c); + } +} + +void Df(int x) +{ + v[x] = true; + for (const int y : G[x]) + { + if (v[y]) continue; + Df(y); + } + stk.push(x); +} + +void DfT(int x) +{ + v[x] = true; + c.emplace_back(x); + + for (const int& y : GT[x]) + { + if (v[y]) continue; + DfT(y); + } +} + +void AfiseazaCompTareConexe() +{ + fout << ctc.size() << '\n'; + + for (const auto& comp : ctc) + { + for (const int& x : comp) + fout << x << ' '; + fout << '\n'; + } +} diff --git a/scc/kosaraju/b5e67928-9797-4115-b8fd-9ffed9d46975.cpp b/scc/kosaraju/b5e67928-9797-4115-b8fd-9ffed9d46975.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95942b83b41340451619a693b2964d1c902449eb --- /dev/null +++ b/scc/kosaraju/b5e67928-9797-4115-b8fd-9ffed9d46975.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int nmax=1e5+3; + + + +int n,m,a,b,viz[nmax],p,k,actual,viz2[nmax],nod; + +vector x[nmax],y[nmax],sol[nmax]; + +stack stiva; + + + +void dfs1(int nod) + +{ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + while(m--) + + { + + f>>a>>b; + + x[a].push_back(b); + + y[b].push_back(a); + + } + + for(int i=1;i<=n;++i) if(!viz[i]) dfs1(i); + + while(!stiva.empty()) + + { + + nod=stiva.top(); + + stiva.pop(); + + if(viz2[nod]) continue; + + ++p; + + actual=viz[nod]; + + dfs2(nod); + + } + + g< +#include +using namespace std; + + + +int N, M, nrc; + + + +vector> G, GT, CTC; + +vectorviz; + +vectorPost; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +void citire() + +{ + + int x, y; + + f >> N >> M; + + G = vector>(N + 1); + + GT = vector>(N + 1); + + CTC = vector>(N + 1); + + viz = vector(N + 1, 0); + + Post = vector(N + 1); + + + + for(int i = 1; i <= M; i++) + + { + + f >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(auto &x:G[vf]) + + if(viz[x] == 0) + + DFS(x); + + Post.push_back(vf); + +} + + + +void DFST(int vf) + +{ + + viz[vf] = 0; + + CTC[nrc].push_back(vf); + + for(auto &x:GT[vf]) + + if(viz[x] == 1) + + DFST(x); + +} + + + +void componente() + +{ + + for(int i = 1; i <= N; i++) + + if(!viz[i]) + + DFS(i); + + + + + + for(auto it = Post.rbegin(); it != Post.rend(); ++it) + + if(viz[*it] == 1) + + { + + nrc++; + + DFST(*it); + + } + +} + + + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; i++) + + { + + for(auto &x : CTC[i]) + + g << x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + citire(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/b8045aef-e504-436b-b452-b949794d6354.cpp b/scc/kosaraju/b8045aef-e504-436b-b452-b949794d6354.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bfa960b3d2aec9c28f7836a82b642e23c8f761cb --- /dev/null +++ b/scc/kosaraju/b8045aef-e504-436b-b452-b949794d6354.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, viz[100005], vizT[100005], ct; + +vector muchii[100005]; + +vector muchiiT[100005]; + +vector sol[100005]; + +stack S; + + + +void Citire() + +{ + + int i, x, y; + + fin >> n >> m; + + for (i = 1; i <= m; i++) + + { + + fin >> x >> y; + + muchii[x].push_back(y); + + muchiiT[y].push_back(x); + + } + +} + + + +void DFS(int x) + +{ + + int i, y; + + viz[x] = 1; + + for (i = 0; i < muchii[x].size();i++) + + if (viz[muchii[x][i]] == 0) + + DFS(muchii[x][i]); + + S.push(x); + +} + + + + + +void DFST(int x) + +{ + + int y, i; + + vizT[x] = 1; + + sol[ct].push_back(x); + + for (i = 0; i < muchiiT[x].size(); i++) + + if (vizT[muchiiT[x][i]] == 0) + + DFST(muchiiT[x][i]); + + + +} + + + + + +void CompTareConexe() + +{ + + int i, x, j; + + for (i = 1; i <= n; i++) + + if (viz[i]==0) + + DFS(i); + + + + while (S.empty()==0) + + { + + if (vizT[S.top()]==0) + + { + + ct++; + + DFST(S.top()); + + } + + S.pop(); + + } + + + + fout << ct << "\n"; + + for (i = 1; i <= ct; i++) + + { + + for (j = 0; j < sol[i].size(); j++) + + fout << sol[i][j] << " "; + + fout << "\n"; + + } + + + +} + + + + + +int main() + +{ + + Citire(); + + CompTareConexe(); + + + + return 0; + +} diff --git a/scc/kosaraju/b9423a16-0eee-467d-87a3-a5ebdcaf2ded.cpp b/scc/kosaraju/b9423a16-0eee-467d-87a3-a5ebdcaf2ded.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98d245ada4978f01ce30361561d693afc14da9f8 --- /dev/null +++ b/scc/kosaraju/b9423a16-0eee-467d-87a3-a5ebdcaf2ded.cpp @@ -0,0 +1,278 @@ +#include +#include +#include +#include +using namespace std; + + + + + + + +const char iname[] = "ctc.in"; + + + +const char oname[] = "ctc.out"; +vector Go[100005], Gi[100005], G[100005]; + + + + + + + +void read_in(vector * Go, vector * Gi, int& n, const char iname[]) + + + +{ + + + + ifstream in(iname); + + + + int cnt_edges, x, y; + + + + + + + + in >> n; + + + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) { + + + + in >> x >> y; + + + + x --, y --; + + + + Go[x].push_back(y); + + + + Gi[y].push_back(x); + + + + } + + + + in.close(); + + + +} + + + + + + + +vector discovered, used; + + + + + + + +void DFP(const int n, vector * G) + + + +{ + + + + vector ::iterator it; + + + + used[n] = true; + + + + for (it = G[n].begin(); it != G[n].end(); ++ it) + + + + if (used[*it] == false) + + + + DFP(*it, G); + + + + discovered.push_back(n); + + + +} + + + + + + + +vector where; + + + + + + + +void DFM(const int n, vector * G, const int which) + + + +{ + + + + vector ::iterator it; + + + + where[n] = which; + + + + for (it = G[n].begin(); it != G[n].end(); ++ it) + + + + if (where[*it] == -1) + + + + DFM(*it, G, which); + + + +} + + + + + + + +void print_out(const vector * G, const int n, const char oname[]) + + + +{ + + + + ofstream out(oname); + + + + vector ::const_iterator it; + + + + + + + + out << n <<'\n'; + + + + for (int i = 0; i < n; ++ i) { + + + + for (it = G[i].begin(); it != G[i].end(); ++ it) + + + + out << *it + 1 << " "; + + + + out << '\n'; + + + + } + + + + out.close(); + + + +} + + + + + + + +int main(void) + + + +{ + + int n; + + read_in(Go, Gi, n, iname); + + used.resize(n); + + used.assign(used.size(), 0); + + for (int i = 0; i < n; ++ i) if (used[i] == false) + + DFP(i, Go); + + where.resize(n); + + where.assign(where.size(), -1); + + int count = 0; + + for (; discovered.size(); discovered.pop_back()) + + if (where[discovered.back()] == -1) { + + DFM(discovered.back(), Gi, count); + + count ++; + + } + + for (int i = 0; i < n; ++ i) + + G[where[i]].push_back(i); + + print_out(G, count, oname); + + return 0; + + + +} diff --git a/scc/kosaraju/ba0731c8-9f59-4888-8747-db2c5dfa6c24.cpp b/scc/kosaraju/ba0731c8-9f59-4888-8747-db2c5dfa6c24.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c536a7f2cc6eb330fcdce9980262360fba08b9c --- /dev/null +++ b/scc/kosaraju/ba0731c8-9f59-4888-8747-db2c5dfa6c24.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +bitset <100000 +1> seen; + +vector G[100000 +1], GT[100000 +1], S; + + + +void dfs (int x) { + + seen[x]=1; + + for (auto it: G[x]) + + if (!seen[it]) + + dfs(it); + + S.push_back(x); + +} + + + +void dfsT (int x) { + + seen[x]=1; + + for (auto it: GT[x]) + + if (!seen[it]) + + dfsT(it); + + fout << x << ' '; + +} + + + +int main () { + + int n, m; + + ios::sync_with_stdio(false); + + fin >> n >> m; + + + + int i, j; + + for (; m; m--) { + + fin >> i >> j; + + G[i].push_back(j); + + GT[j].push_back(i); + + } + + + + for (i=1; i<=n; i++) + + if (!seen[i]) + + dfs(i); + + + + fout << " \n"; + + seen.reset(); + + int ct=0; + + + + for (auto it=S.rbegin(); it!=S.rend(); ++it) + + if (!seen[*it]) { + + dfsT(*it); + + ++ct; + + fout << '\n'; + + } + + + + fout.seekp(0); + + fout << ct; + + return 0; + +} diff --git a/scc/kosaraju/ba0d4b63-cc7a-43db-a5c6-eaa50ceb2bc4.cpp b/scc/kosaraju/ba0d4b63-cc7a-43db-a5c6-eaa50ceb2bc4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74447c46af8d5ccaf2bc154a3df07951f789f037 --- /dev/null +++ b/scc/kosaraju/ba0d4b63-cc7a-43db-a5c6-eaa50ceb2bc4.cpp @@ -0,0 +1,193 @@ +#include +using namespace std; +class Task { + + public: + + void solve() { + + read_input(); + + print_output(get_result()); + + } + + + + private: + + int n, m; + + vector lista_vecini[100001]; + + vector lista_vecini_graf_transpus[100001]; + + + + void read_input() { + + ifstream in("ctc.in"); + + in >> n >> m; + + + + for (int i = 1; i <= m; i++) { + + int x, y; + + in >> x >> y; + + lista_vecini[x].push_back(y); + + lista_vecini_graf_transpus[y].push_back(x); + + } + + + + in.close(); + + } + + + + void dfs(int source, vector &visited, vector &finish_sort) { + + visited[source] = 1; + + for (auto &vecin : lista_vecini[source]) { + + if (!visited[vecin]) { + + dfs(vecin, visited, finish_sort); + + } + + } + + + + finish_sort.push_back(source); + + } + + + + + + + + void dfs_transpus(int source, vector &visited, vector &componenta_noua) { + + componenta_noua.push_back(source); + + visited[source] = 0; + + + + for (auto &vecin: lista_vecini_graf_transpus[source]) { + + if (visited[vecin]) { + + dfs_transpus(vecin, visited, componenta_noua); + + } + + } + + } + + + + vector> get_result() { + + vector> componente_biconexe; + + vector finish_sort; + + vector visited(n + 1, 0); + + + + finish_sort.push_back(-1); + + + + for (int i = 1; i <= n; ++i) { + + if (!visited[i]) { + + dfs(i, visited, finish_sort); + + } + + } + + + + for (int i = n; i >= 1; --i) { + + if (visited[finish_sort[i]]) { + + vector componenta_noua; + + dfs_transpus(finish_sort[i], visited, componenta_noua); + + componente_biconexe.push_back(componenta_noua); + + } + + } + + + + return componente_biconexe; + + } + + + + void print_output(vector> componente_biconexe) { + + ofstream out("ctc.out"); + + out << componente_biconexe.size() << "\n"; + + + + for (const auto &ctc : componente_biconexe) { + + for (auto &elem : ctc) { + + out << elem << " "; + + } + + out << "\n"; + + } + + + + out.close(); + + } + +}; + + + + + +int main() { + + Task* task = new Task(); + + task->solve(); + + delete(task); + + return 0; + +} diff --git a/scc/kosaraju/bb242c22-45c1-4595-8e03-0e9d89143a11.cpp b/scc/kosaraju/bb242c22-45c1-4595-8e03-0e9d89143a11.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3a7b36247cf75f17b8e099ec12c5eda255049d1 --- /dev/null +++ b/scc/kosaraju/bb242c22-45c1-4595-8e03-0e9d89143a11.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +using namespace std; + +vector ms[100005]; +vector invertedms[100005]; + +stack ns; +int comp[100005]; +vector compis[100005]; +bool visited[100005]; + +void dfs(int node){ + if(!visited[node]){ + visited[node] = 1; + for (auto it : ms[node]) { + dfs(it); + } + ns.push(node); + } +} + +void assign(int n, int c){ + if(comp[n] == 0){ + comp[n] = c; + compis[c].push_back(n); + for(auto it : invertedms[n]){ + assign(it, c); + } + } +} + +int main() { + int n, m; + ifstream f("ctc.in"); + ofstream g("ctc.out"); + f>>n>>m; + for (int i = 0; i < m; ++i) { + int x, y; + f>>x>>y; + ms[x].push_back(y); + invertedms[y].push_back(x); + } + for (int i = 0; i < n; ++i) { + if(!visited[i]) dfs(i); + } + int c = 0; + while(!ns.empty()) { + int node = ns.top(); + ns.pop(); + if (comp[node] == 0) { + ++c; + assign(node, c); + } + + } + --c; + g< + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +int n,m; +int vf[200001],urm[200001],last[100001],nr; +int vfT[200001],urmT[200001],lastT[100001],nrT; +int comp[100001],urmC[100001],lastC[100001],nrC; +int ord[100001],t; +bitset <100001> viz; +int k; + +void adauga(int nod,int vec) +{ + vf[++nr]=vec; + urm[nr]=last[nod]; + last[nod]=nr; +} + +void adaugaT(int nod,int vec) +{ + vfT[++nrT]=vec; + urmT[nrT]=lastT[nod]; + lastT[nod]=nrT; +} + +void adaugaC(int nod,int c) +{ + comp[++nrC]=nod; + urmC[nrC]=lastC[c]; + lastC[c]=nrC; +} + +void dfs(int nod) +{ + viz[nod]=1; + + for(int k=last[nod];k;k=urm[k]) + if(!viz[ vf[k] ]) + dfs(vf[k]); + + ord[++t]=nod; +} + +void dfsT(int nod,int c) +{ + viz[nod]=1; + adaugaC(nod,c); + + for(int k=lastT[nod];k;k=urmT[k]) + if(!viz[ vfT[k] ]) + dfsT(vfT[k],c); +} + +int main() +{ + in>>n>>m; + + for(int i,j,k=1;k<=m;k++) + { + in>>i>>j; + + adauga(i,j); + adaugaT(j,i); + } + + for(int i=1;i<=n;i++) + if(!viz[i]) + dfs(i); + + viz.reset(); + + for(int i=n;i>=1;i--) + if(!viz[ ord[i] ]) + dfsT(ord[i],++k); + + out< +#include +#include +#include +using namespace std; + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +vector > muchii[2]; + +vector marcat; + +vector sortat; + +vector > componente; + +vector componentaTata; + +int n, m, x, y; + +void dfs(int nod) + +{ + + marcat[nod]=true; + + for(auto & x:muchii[0][nod]) + + if(!marcat[x]) + + dfs(x); + + sortat.push_back(nod); + +} + +void topologica() + +{ + + for(int i=1; i<=n; i++) + + if(!marcat[i]) + + dfs(i); + + reverse(sortat.begin(), sortat.end()); + +} + +void dfs2(int nod) + +{ + + if(!componentaTata[nod]) + + { + + componentaTata[nod]=componente.size(); + + componente.back().push_back(nod); + + for(auto & x : muchii[1][nod]) + + dfs2(x); + + } + +} + +void Kosaraju() + +{ + + for(auto & nod : sortat) + + if(!componentaTata[nod]) + + { + + componente.push_back(vector ()); + + dfs2(nod); + + } + +} + +int main() + +{ + + in>>n>>m; + + marcat.resize(n+1, false); + + componentaTata.resize(n+1, 0); + + muchii[0].resize(n+1); muchii[1].resize(n+1); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y; + + muchii[0][x].push_back(y); + + muchii[1][y].push_back(x); + + } + + topologica(); + + Kosaraju(); + + out< +using namespace std; + + + +int n,m,postord[100001], t, viz[100001], k; + +vector G[100001], GT[100001], ctc[100001]; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +void DFS(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + + + + + + + for (i = 0;i< G[x].size();i++) + + { + + w=G[x][i]; + + if (!viz[w]) + + DFS(w); + + } + +t++; + +postord[t] = x; + +} + + + + + + + +void DFST(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + ctc[k].push_back(x); + + for (i = 0;i>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + t=0; + + for (i = 1; i <= n; i++) + + if (!viz[i]) + + DFS(i); + for(i=1;i<=n;i++) + + viz[i]=0; + + + + + + + + k=0; + + + + for (i = n; i>=1; i--) + + if (!viz[postord[i]]) + + { + + k++; + + DFST(postord[i]); + + } + + + + + + + + fout< + + +using namespace std; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +vector v[100005], vt[100005], c[100005]; + +int viz[100005], a[100005], k, n, m; + + + +void dfs1(int x) + +{ + + viz[x]=1; + + for(auto it:v[x]) + + if(viz[it]==0) + + dfs1(it); + + a[++k]=x; + +} + +void dfs2(int x) + +{ + + viz[x]=1; + + for(auto it:vt[x]) + + if(viz[it]==0) + + dfs2(it); + + c[k].push_back(x); + +} + +int main() + +{ + + fin >> n >> m; + + for(int i=1;i<=m;i++) + + { + + int x, y; + + fin >> x >> y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(!viz[i]) + + dfs1(i); + + memset(viz, 0, sizeof(viz)); + + k=0; + + for(int i=n;i>=1;i--) + + if(!viz[a[i]]) + + { + + k++; + + dfs2(a[i]); + + } + + fout << k << '\n'; + + for(int i=1;i<=k;i++) + + { + + for(auto it:c[i]) + + fout << it << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/bf6459f4-e33b-47d1-a67d-19d2d7b31e0e.cpp b/scc/kosaraju/bf6459f4-e33b-47d1-a67d-19d2d7b31e0e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9e88f7b3bbeb66d636b32b7bb7a68d84f82dfaf --- /dev/null +++ b/scc/kosaraju/bf6459f4-e33b-47d1-a67d-19d2d7b31e0e.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + + +FILE *fin,*fout; + +int ctc; + +char viz[100001],vizt[100001]; + +std::vectorl[100001]; + +std::vectorlt[100001]; + +std::stacks; + +std::vectorr[100001]; + + + +void dfs(int nod){ + + viz[nod]=1; + + for(auto i: l[nod]) + + if(!viz[i]) + + dfs(i); + + s.push(nod); + +} + + + +void dfst(int nod){ + + vizt[nod]=1; + + r[ctc].push_back(nod); + + for(auto i: lt[nod]) + + if(!vizt[i]) + + dfst(i); + +} + + + +int main(){ + + fin=fopen("ctc.in","r"); + + fout=fopen("ctc.out","w"); + + int n,m,i,x,y; + + + + fscanf(fin,"%d%d",&n,&m); + + for(i=0; i +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector compcon[100009]; + +vector v[100009]; + +vector t[100009]; + +int viz[100009],nr; + +stacks; + + + +void dfs(int n) + +{ + + viz[n] = 1; + + for(int i=0; i>n>>m; + + for(int i =1; i <=m ; i++) + + { + + f>>x>>y; + +t[y].push_back(x); + + v[x].push_back(y); + + + + } + + + + + + + + for(int i = 1; i <= n; i++) + + { + + if(viz[i]==0) + + dfs(i); + + } + + + + + + + + while(s.empty()==0) + + { + + int n=s.top(); + + if(viz[n]==1) + + { + + nr++; + + dfs_t(n); + + } + + s.pop(); + + } + + g< +#include +#include +#include +#include +#include + +using namespace std; + +const int MX = 100001; +vector graf[MX]; +vector grafReverse[MX]; +stack parcurgereInversa; +int vizite[MX]; +map> harta; +int counter; +int n, m; + +void parcurgereDFS(int poz) { + vizite[poz] = 1; + for (auto &vecin : graf[poz]) { + if (vizite[vecin] == 0) + parcurgereDFS(vecin); + } + parcurgereInversa.push(poz); +} + +void parcurgereDFSReverse(int poz) { + vizite[poz] = counter; + harta[counter].emplace_back(poz); + + for (auto &vecin : grafReverse[poz]) { + if (vizite[vecin] == 0) + parcurgereDFSReverse(vecin); + } +} + +void parcurgereDFSR() { + + while (parcurgereInversa.empty() == 0) { + int poz = parcurgereInversa.top(); + if (vizite[poz] == 0) { + harta.insert(pair>(counter, vector{})); + counter++; + parcurgereDFSReverse(poz); + } + parcurgereInversa.pop(); + } +} + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +int main() { + f >> n >> m; + for (int i = 0; i < m; i++) { + int sursa, destinatie; + f >> sursa >> destinatie; + graf[sursa].emplace_back(destinatie); + grafReverse[destinatie].emplace_back(sursa); + } + + for (int i = 1; i <= n; i++) + if (vizite[i] == 0) + parcurgereDFS(i); + + memset(vizite, 0, (n + 1) * sizeof(int)); + + parcurgereDFSR(); + + g << counter << '\n'; + + for (int i = 1; i <= counter; i++) { + for (unsigned int j = 0; j < harta[i].size(); j++) + g << harta[i][j] << ' '; + g << '\n'; + } + f.close(); + g.close(); + return 0; +} diff --git a/scc/kosaraju/c1ef5fea-59ea-45bd-b986-773b2ba89873.cpp b/scc/kosaraju/c1ef5fea-59ea-45bd-b986-773b2ba89873.cpp new file mode 100644 index 0000000000000000000000000000000000000000..704e9edd75a1a8a0e90f3e290508f1d7d601db8b --- /dev/null +++ b/scc/kosaraju/c1ef5fea-59ea-45bd-b986-773b2ba89873.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + +typedef long long ll; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 1e5 + 5; + + + +int ctc; + +vector g[NMAX], tr[NMAX], rez[NMAX]; + +vector viz(NMAX); + +stack po; + + + +void DFS(int nod) + +{ + + viz[nod] = 1; + + for (size_t i = 0; i < g[nod].size(); ++i) + + if (!viz[g[nod][i]]) DFS(g[nod][i]); + + po.push(nod); + +} + + + +void DFST(int nod) + +{ + + viz[nod] = 0, rez[ctc].push_back(nod); + + for (size_t i = 0; i < tr[nod].size(); ++i) + + if (viz[tr[nod][i]] == 1) DFST(tr[nod][i]); + +} + + + +int main() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + + + + int n, m; + + fin >> n >> m; + + + + while (m--) + + { + + int x, y; + + fin >> x >> y; + + + + g[x].push_back(y), tr[y].push_back(x); + + } + + + + for (int i = 1; i <= n; ++i) + + if (!viz[i]) DFS(i); + + + + while (!po.empty()) + + { + + if (viz[po.top()] == 1) + + { + + ++ctc; + + DFST(po.top()); + + } + + po.pop(); + + } + + + + fout << ctc << "\n"; + + + + for (int i = 1; i <= ctc; ++i) + + { + + for (size_t j = 0; j < rez[i].size(); ++j) + + fout << rez[i][j] << " "; + + fout << "\n"; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/c313ed75-a60a-49d2-bdb5-77c0be0881f1.cpp b/scc/kosaraju/c313ed75-a60a-49d2-bdb5-77c0be0881f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6301d8176c7b41a80c873c01cf4275b7e220590f --- /dev/null +++ b/scc/kosaraju/c313ed75-a60a-49d2-bdb5-77c0be0881f1.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + +ifstream r("ctc.in"); + +ofstream w("ctc.out"); + +int d[100001],n,o,nr,m; + +bool v[100001]; + +vector a[100001],a2[100001],c[100001]; + +void f(int x) + +{ + + if(!v[x]) + + { + + v[x]=1; + + for(int t : a[x]) + + f(t); + + d[++nr]=x; + + } + +} + +void g(int x) + +{ + + if(v[x]) + + { + + v[x]=0; + + for(int t : a2[x]) + + g(t); + + c[o].push_back(x); + + } + +} + +int main() + +{ + + r>>n>>m; + + int x,y; + + while(r>>x>>y){ + + a[x].push_back(y); + + a2[y].push_back(x); + + } + + for(int i=1; i<=n; i++){ + + f(i); + + } + + for(int i=n; i>=1; i--){ + + if(v[d[i]]) + + { + + o++; + + g(d[i]); + + } + + } + + w< +#include +#include +#include +using namespace std; + +FILE *f,*g; + +vector graph[100010],grapht[100010],ctc[100010]; + +deque Q; + +int N,M,been[100010],Nr; + +void Read() + +{ + + int i,X,Y; + + fscanf(f,"%d %d",&N,&M); + + for(i=1;i<=M;++i) + + { + + fscanf(f,"%d %d",&X,&Y); + + graph[X].push_back(Y); + + grapht[Y].push_back(X); + + } + +} + +void DFS(int Node) + +{ + + int i,t=graph[Node].size(); + + been[Node]=1; + + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vectorv[100005],v2[100005],sol[100005]; + +stackst; + +int n,m,d[100005],x,y,k,nr; + +void dfs1(int nod) + +{ + + d[nod]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + v2[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + { + + if(!d[i]) dfs1(i); + + } + + memset(d,0,sizeof(d)); + + while(!st.empty()) + + { + + if(!d[st.top()]) {nr++;dfs2(st.top());} + + st.pop(); + + } + + g< + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, nr, vf[200001], urm[200001], last[100001]; + +int nrt, vft[200001], urmt[200001], lastt[100001]; + +int top[100001], nrtop, nrc; + +bitset < 100001 > viz; + +vector < int > answer[100001]; + + + +void adauga(int from, int to) + +{ + + vf[++nr] = to; + + urm[nr] = last[from]; + + last[from] = nr; + +} + + + +void adaugat(int from, int to) + +{ + + vft[++nrt] = to; + + urmt[nrt] = lastt[from]; + + lastt[from] = nrt; + +} + + + + + +void citire() + +{ + + fin>>n>>m; + + for(int x, y, i=1; i<=m; i++) + + { + + fin>>x>>y; + + adauga(x, y); + + adaugat(y, x); + + + + } + + + +} + + + +void dfs(int nod) + +{ + + viz[nod] = 1; + + + + for(int k = last[nod]; k; k = urm[k]) + + if(viz[ vf[k] ] == 0) + + dfs(vf[k]); + + + + top[++nrtop] = nod; + + + +} + + + +void dfst(int nod) + +{ + + viz[nod] = 1; + + answer[nrc].push_back(nod); + + for(int k = lastt[nod]; k; k = urmt[k]) + + if(viz[ vft[k] ] == 0) + + dfst(vft[k]); + +} + + + + + +int main(){ + + citire(); + + for(int i=1; i<=n; i++) + + if(!viz[i]) + + dfs(i); + + + + viz = 0; + + + + for(int i = nrtop; i>0; i--) + + { + + if(!viz[ top[i] ]) + + { + + nrc++; + + dfst(top[i]); + + } + + + + } + + fout< +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector A[100005],TA[100005],CTC[100005]; + +stack S; + + + +int N,M,cate_conexe; + + + +int VIZ[100005],TVIZ[100005]; + + + +int df (int x) + +{ + + VIZ[x]=1; + + vector :: iterator it; + + for (it=A[x].begin();it!=A[x].end();++it) + + { + + int val=(*it); + + if (VIZ[val]==0) + + { + + df(val); + + } + + } + + S.push(x); + +} + + + +int tdf (int x) + +{ + + TVIZ[x]=1; + + CTC[cate_conexe].push_back(x); + + vector :: iterator it; + + for (it=TA[x].begin();it!=TA[x].end();++it) + + { + + int val=(*it); + + if (TVIZ[val]==0) + + { + + tdf(val); + + } + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + int a,b,i; + + for (i=1;i<=M;++i) + + { + + fin >> a >> b; + + A[a].push_back(b); + + TA[b].push_back(a); + + } + + for (i=1;i<=N;++i) + + { + + if (VIZ[i]==0) + + { + + df(i); + + } + + } + while (!S.empty()) + + { + + i=S.top(); + + S.pop(); + + if (TVIZ[i]==0) + + { + + ++cate_conexe; + + tdf(i); + + } + + } + + fout << cate_conexe << '\n'; + + for (i=1;i<=cate_conexe;++i) + + { + + vector :: iterator it; + + for (it=CTC[i].begin();it!=CTC[i].end();++it) + + { + + int val=(*it); + + fout << val << " "; + + } + + fout << '\n'; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/kosaraju/c65e79c5-911b-4828-bc88-d2aa578fbae9.cpp b/scc/kosaraju/c65e79c5-911b-4828-bc88-d2aa578fbae9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdd2bdde369293f9008a51ffb7de008558d7e590 --- /dev/null +++ b/scc/kosaraju/c65e79c5-911b-4828-bc88-d2aa578fbae9.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +struct noduri{vector out;vector in;}v[100003]; + +struct comp{vector nd;}emptyaux; + +vector cmp; + +stack st; + +int n,m,i; + +bool trout[100003],trin[100003]; + +void dfsout(int nod) + +{ + + trout[nod]=1; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + v[x].out.push_back(y); + + v[y].in.push_back(x); + + } + + i=0; + + for(i=1;i<=n;i++) + + if(trout[i]==0) + + dfsout(i); + + while(st.size()) + + { + + int nod=st.top(); + + if(trin[nod]==0) + + dfsin(nod,nod); + + st.pop(); + + } + + g< + + + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int n,m; + +int vf[2*100000 +1],urm[2*100000 +1],last[100000 +1],nr; + +int vf2[2*100000 +1],urm2[2*100000 +1],last2[100000 +1]; + +int ord[100000 +1],t; + +bitset <100000 +1> viz; + +int vf3[100000 +1],urm3[100000 +1],last3[100000 +1],nrc; + + + +void adauga(int nod,int vec) + +{ + + vf[++nr]=vec; + + urm[nr]=last[nod]; + + last[nod]=nr; + + + + swap(nod,vec); + + + + vf2[nr]=vec; + + urm2[nr]=last2[nod]; + + last2[nod]=nr; + +} + + + +void adaugaT(int nod,int nrc) + +{ + + vf3[++nr]=nod; + + urm3[nr]=last3[nrc]; + + last3[nrc]=nr; + +} + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + + + for(int k=last[nod];k;k=urm[k]) + + if(!viz[ vf[k] ]) + + dfs(vf[k]); + + + + ord[++t]=nod; + +} + + + +void dfst(int nod,int nrc) + +{ + + viz[nod]=1; + + adaugaT(nod,nrc); + + + + for(int k=last2[nod];k;k=urm2[k]) + + if(!viz[ vf2[k] ]) + + dfst(vf2[k],nrc); + +} + + + +int main() + +{ + + in>>n>>m; + + + + for(int k=1,i,j;k<=m;k++) + + { + + in>>i>>j; + + adauga(i,j); + + } + + + + for(int i=1;i<=n;i++) + + if(!viz[i]) + + dfs(i); + + + + nr=0;viz=0; + + for(int i=n;i>=1;i--) + + if(!viz[ ord[i] ]) + + dfst(ord[i],++nrc); + + + + out< + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +const int Max=100005; + +bool viz[Max]; int n,m,nr; + +stack s; + +vector v[Max],vt[Max],ctc[Max]; + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(int j=0;j>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; in>>x>>y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + +} + +int main() + +{ + + citire(); + + for(int i=1;i<=n;i++) + + if(!viz[i]) + + dfs(i); + + while(!s.empty()) + + { + + int nod=s.top(); s.pop(); + + if(viz[nod]) + + { + + nr++; + + dfs2(nod); + + } + + + + } + + out< +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int NMAX = 100'001; + +int n,m, beenThere[NMAX], strongly_conn; +vector G[NMAX] , GT[NMAX] , sol[NMAX]; +stack s; + +void Read() { + + f >> n >> m; + + while(m--) { + int x, y; + f >> x >> y; + G[x].push_back(y); + GT[y].push_back(x); + } +} + +void dfs(int node) { + + beenThere[node] = 1; + + for(int neighbor : G[node]) + if(!beenThere[neighbor]) + dfs(neighbor); + + s.push(node); +} + +void dfsReverse(int node) { + + beenThere[node] = 1; + sol[strongly_conn].push_back(node); + + for(int neighbor : GT[node]) + if(!beenThere[neighbor]) + dfsReverse(neighbor); +} + +void kosaraju() { + + for(int i = 1;i <= n;++i) + if(!beenThere[i]) + dfs(i); + + for(int i = 1;i <= n;++i) + beenThere[i] = 0; + + while(!s.empty()) { + int node = s.top(); + if(beenThere[node] == 0) { + strongly_conn++; + dfsReverse(node); + } + s.pop(); + } +} + +void print() { + + g << strongly_conn << '\n'; + + for(int i = 1;i <= strongly_conn;++i,g << '\n') + for(int node : sol[i]) + g << node << ' '; +} + +int main() { + + Read(); + kosaraju(); + print(); + + return 0; +} diff --git a/scc/kosaraju/c8d32544-ac74-4f22-b01b-c23794a195f0.cpp b/scc/kosaraju/c8d32544-ac74-4f22-b01b-c23794a195f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d30cdc86bba9d849c156aa332082e483b8d7ddb --- /dev/null +++ b/scc/kosaraju/c8d32544-ac74-4f22-b01b-c23794a195f0.cpp @@ -0,0 +1,86 @@ +#include +#include + +using namespace std; + +ifstream fin ("ctc.in"); +ofstream fout ("ctc.out"); + +const int N = 100001; +const int M = 200001; + +int stiva[N], cnt, inv_stiva[N]; +int lst[N], vf[2*M], urm[2*M], n, nr, viz[N]; +int inv_lst[N], inv_vf[2*M], inv_urm[2*M], inv_viz[N]; +int ans[2*N+1], anspos; + +void topological_sort_dfs(int x) { + viz[x] = true; + int y; + for ( int p = lst[x]; p != 0; p = urm[p] ) { + y = vf[p]; + if ( !viz[y] ) + topological_sort_dfs(y); + } + stiva[++cnt] = x; +} + +void inverse_topological_sort_dfs(int x) { + inv_viz[x] = true; + ans[anspos++] = x; + int y; + for ( int p = inv_lst[x]; p != 0; p = inv_urm[p] ) { + y = inv_vf[p]; + if ( !inv_viz[y] ) + inverse_topological_sort_dfs(y); + } + inv_stiva[++cnt] = x; +} + +void adauga (int x, int y) { + ++nr; + + vf[nr] = y; + urm[nr] = lst[x]; + lst[x] = nr; + + inv_vf[nr] = x; + inv_urm[nr] = inv_lst[y]; + inv_lst[y] = nr; +} + +int main () { + int n, m, x, y; + fin>>n>>m; + + for ( int i = 0; i < m; i++ ) { + fin>>x>>y; + adauga(x,y); + } + + for (int i = 1; i <= n; i++ ) { + if ( !viz[i] ) + topological_sort_dfs(i); + } + + cnt = 0; + int nrof = 0; + for (int i = n; i > 0; i-- ) { + if ( !inv_viz[stiva[i]] ) + { + inverse_topological_sort_dfs(stiva[i]); + ans[anspos++] = -1; + ++nrof; + } + } + + fout< +#include +#include +using namespace std; +const int NMAX = 100505; +int N, M; +vector G[NMAX], GT[NMAX]; + +void read() { + scanf("%d%d", &N, &M); + int x, y; + for (int i = 1; i <= M; i++) { + scanf("%d%d", &x, &y); + G[x].push_back(y); + GT[y].push_back(x); + } +} + +void dfsNormalGraph(int node, vector& visited, vector& sortedByExitTime) { + visited[node] = true; + for (auto& neighbour : G[node]) { + if (!visited[neighbour]) { + dfsNormalGraph(neighbour, visited, sortedByExitTime); + } + } + + sortedByExitTime.push_back(node); +} + +void dfsTransposeGraph(int node, vector& visited, vector& currentComponent) { + visited[node] = true; + currentComponent.push_back(node); + for (auto& neighbour : GT[node]) { + if (!visited[neighbour]) { + dfsTransposeGraph(neighbour, visited, currentComponent); + } + } +} + +void solve() { + vector sortedByExitTime; + vector visited(N + 1, false); + for (int node = 1; node <= N; node++) { + if (!visited[node]) { + dfsNormalGraph(node, visited, sortedByExitTime); + } + } + reverse(sortedByExitTime.begin(), sortedByExitTime.end()); + + visited.assign(N + 1, false); + vector> result; + for (auto node : sortedByExitTime) { + if (!visited[node]) { + vector currentComponent; + dfsTransposeGraph(node, visited, currentComponent); + result.push_back(currentComponent); + } + } + + printf("%d\n", (int) result.size()); + for (auto& component : result) { + for (auto& node : component) { + printf("%d ", node); + } + printf("\n"); + } +} + +int main() { + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + + read(); + solve(); + return 0; +} diff --git a/scc/kosaraju/c975960d-781b-4cab-ba2f-0e9a816f578f.cpp b/scc/kosaraju/c975960d-781b-4cab-ba2f-0e9a816f578f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9747d9d77906f684e84cf2cf9d40cbb913151ef --- /dev/null +++ b/scc/kosaraju/c975960d-781b-4cab-ba2f-0e9a816f578f.cpp @@ -0,0 +1,188 @@ +#include + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +const int N = 100001, M = 200001; + + + +int vf[M], urm[M], lst[N], viz[N], st[N], vf1[M], urm1[M], lst1[N], viz1[N], st1[N], nrst1, nrst, nr, nr1, c; + + + +void adauga(int x, int y) + +{ + + vf[++nr] = y; + + urm[nr] = lst[x]; + + lst[x] = nr; + +} + + + +void adauga1(int x, int y) + +{ + + vf1[++nr1] = y; + + urm1[nr1] = lst1[x]; + + lst1[x] = nr1; + +} + + + +void dfs(int x) + +{ + + viz[x] = true; + + for(int p = lst[x]; p != 0; p = urm[p]) + + { + + int y = vf[p]; + + if(!viz[y]) + + dfs(y); + + } + + st[++nrst] = x; + +} + + + +void dfs1(int x) + +{ + + viz1[x] = true; + + for(int p = lst1[x]; p != 0; p = urm1[p]) + + { + + int y = vf1[p]; + + if(!viz1[y]) + + dfs1(y); + + } + +} + + + +void dfs2(int x) + +{ + + viz1[x] = true; + + + + for(int p = lst1[x]; p != 0; p = urm1[p]) + + { + + int y = vf1[p]; + + if(!viz1[y]) + + cout << y << " ", dfs2(y); + + } + +} + + + +int main() + +{ + + int n, m, x, y; + + cin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + cin >> x >> y; + + adauga(x, y); + + adauga1(y, x); + + } + + for(int i = 1; i <= n; i++) + + { + + if(!viz[i]) + + dfs(i); + + } + + + + for(int i = nrst; i >= 1; i--) + + { + + if(!viz1[st[i]]) + + c++, dfs1(st[i]); + + } + + cout << c << "\n"; + + for(int i = 1; i <= nrst; i++) + + viz1[st[i]] = false; + + for(int i = nrst; i >= 1; i--) + + { + + if(!viz1[st[i]]) + + { + + cout << st[i] << " "; + + dfs2(st[i]); + + cout << "\n"; + + } + + } + + return 0; + +} diff --git a/scc/kosaraju/c994b4ab-f60c-4686-b00c-6a05ea4b1b53.cpp b/scc/kosaraju/c994b4ab-f60c-4686-b00c-6a05ea4b1b53.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92fd69f036475bd22328a8e24137c06ed4ffc627 --- /dev/null +++ b/scc/kosaraju/c994b4ab-f60c-4686-b00c-6a05ea4b1b53.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int N, M, nR; + +bool Viz[100005],VizT[100005]; + +vectorAd[100005],AdT[100005],Ord,TareCon[100005]; + + + +void citire() + +{ + + int x,y; + + fin>>N>>M; + + for(int i=1;i<=M;i++) + + { + + fin>>x>>y; + + Ad[x].push_back(y); + + AdT[y].push_back(x); + + } + +} + + + +void sortareTopologica(int x) + +{ + + Viz[x]=1; + + for(unsigned i=0;i=0;x--) + + if(!VizT[Ord[x]]) + + { + + nR++; + + DFS(Ord[x]); + + } + + fout< +#include + + +using namespace std; + + + +const int N=100000+7; + +int n,m,top,o[N],sol[N],tip[N],cur; + +bool vis[N]; + +vector g[N],ginv[N]; + + + +void dfs1(int a) { + + vis[a]=1; + + for (auto &b:g[a]) { + + if (vis[b]==0) { + + dfs1(b); + + } + + } + + o[++top]=a; + +} + + + +void dfs2(int a) { + + vis[a]=1; + + sol[++top]=a; + + tip[top]=cur; + + for (auto &b:ginv[a]) { + + if (vis[b]==0) { + + dfs2(b); + + } + + } + +} + + + +signed main() { + + freopen ("ctc.in","r",stdin); + + freopen ("ctc.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + for (int i=1;i<=m;i++) { + + int x,y; + + scanf("%d%d",&x,&y); + + g[x].push_back(y); + + ginv[y].push_back(x); + + } + + for (int i=1;i<=n;i++) { + + if (vis[i]==0) { + + dfs1(i); + + } + + } + + for (int i=1;i<=n;i++) { + + vis[i]=0; + + } + + top=0; + + for (int i=n;i>=1;i--) { + + int node=o[i]; + + if (vis[node]==0) { + + cur++; + + dfs2(node); + + } + + } + + printf("%d",cur); + + for (int i=1;i<=n;i++) { + + if (tip[i]!=tip[i-1]) { + + printf("\n"); + + } + + printf("%d ",sol[i]); + + } + + return 0; + +} diff --git a/scc/kosaraju/cc3cda74-562f-4e2f-8be0-3eb69a1f1ed8.cpp b/scc/kosaraju/cc3cda74-562f-4e2f-8be0-3eb69a1f1ed8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..595ebb3e6b7378de1807508472b56e44fb718a93 --- /dev/null +++ b/scc/kosaraju/cc3cda74-562f-4e2f-8be0-3eb69a1f1ed8.cpp @@ -0,0 +1,161 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N=100000+5; + + + +int n,m,va[N],vb[N]; + +vectorA[N]; + +vectorB[N]; + +int st[N],vf=0; + + + +inline void da(int a) + +{ + + va[a]=1; + + for(auto b:A[a]) + + { + + if(va[b]==0) + + { + + da(b); + + } + + } + + st[++vf]=a; + +} + + + +int k; + + + +vectorlol[N]; + + + +inline void db(int a) + +{ + + lol[k].push_back(a); + + vb[a]=1; + + for(auto b:B[a]) + + { + + if(vb[b]==0) + + { + + db(b); + + } + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b; + + fin>>a>>b; + + A[a].push_back(b); + + B[b].push_back(a); + + } + + for(int i=1;i<=n;i++) + + { + + if(va[i]==0) + + { + + da(i); + + } + + } + + for(int i=n;i>=1;i--) + + { + + int x=st[i]; + + if(vb[x]==0) + + { + + k++; + + db(x); + + } + + } + + fout< +#include +using namespace std; + +const int N=100001; + +vector s[N],u[N],v[N]; + +int n,m,a[N],c,r,x,y,i,j; + +bool b[N]; + +void A(int i) + +{ + + if(b[i]) + + return; + + int j,k; + + for(b[i]=1,k=u[i].size(),j=0;j +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int MAX = 1e5 + 5; + + + +int n, m, ctc; + +int viz[MAX]; + + + +vector neighbors[MAX], neighborsT[MAX], L, vertices[MAX]; + + + +void dfs(int u) + +{ + + viz[u] = 1; + + for(int v : neighbors[u]) + + if(!viz[v]) + + dfs(v); + + L.push_back(u); + +} + + + +void dfsT(int u) + +{ + + viz[u] = 1; + + for(int v : neighborsT[u]) + + if(!viz[v]) + + dfsT(v); + + vertices[ctc].push_back(u); + +} + + + +int main() + +{ + + f >> n >> m; + + for(int i = 1; i <= m; ++i) + + { + + int x, y; + + f >> x >> y; + + neighbors[x].push_back(y); + + neighborsT[y].push_back(x); + + } + + + + for(int i = 1; i <= n; ++i) + + if(!viz[i]) + + dfs(i); + + + + for(int u : L) + + viz[u] = 0; + + + + reverse(L.begin(), L.end()); + + + + for(int u : L) + + if(!viz[u]) + + { + + ++ctc; + + dfsT(u); + + } + + + + g << ctc << "\n"; + + for(int i = 1; i <= ctc; ++i) + + { + + for(int u : vertices[i]) + + g << u << " "; + + g << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/cdaf65b1-c6b3-4df3-ad6f-820ac0ef59fb.cpp b/scc/kosaraju/cdaf65b1-c6b3-4df3-ad6f-820ac0ef59fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28c5b413d50ac4f1cdadb9ec60be00ad7279a085 --- /dev/null +++ b/scc/kosaraju/cdaf65b1-c6b3-4df3-ad6f-820ac0ef59fb.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; +ifstream f("ctc.in"); +ofstream g("ctc.out"); +vector G[100001]; +vector drumuri_intoare[100001]; +int viz[100001]; +stack stiva; +int N , M; +int nrctc; +vectorctc[100001]; +void dfs(int nod) +{ + viz[nod] = 1; + for(auto it = G[nod].begin(); it!=G[nod].end();it++) + { + if(!viz[*it]) + dfs(*it); + } + stiva.push(nod); +} +void dfs2(int nod) +{ + viz[nod] = 2; + ctc[nrctc].push_back(nod); + for(auto it = drumuri_intoare[nod].begin(); it!=drumuri_intoare[nod].end();it++) + { + if(viz[*it] == 1) + { + + dfs2(*it); + } + } +} +int main() +{ + f>>N>>M; + for(int i=1;i<=M;i++) + { + int a , b; + f>>a>>b; + G[a].push_back(b); + drumuri_intoare[b].push_back(a); + } + for(int i=1;i<=N;i++) + { + if(!viz[i]) + dfs(i); + } + while(!stiva.empty()) + { + int nod = stiva.top(); + if(viz[nod] == 1) + { + nrctc++; + dfs2(nod); + } + stiva.pop(); + } + g< +#include +#include +using namespace std; +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +vector v[100003],vt[100003],sol[100003]; +int N,M,S,nr,postord[100003],viz[100003]; + +void DF(int x) +{ + viz[x] = 1; + for(int k = 0 ; k < v[x].size() ; k++) + if( viz[ v[x][k] ] == 0 ) + DF( v[x][k] ); + nr++; + postord[nr] = x; +} + +void DFT(int x,int s) +{ + viz[x] = 0; + sol[s].push_back(x); + for(int k = 0 ; k < vt[x].size() ; k++) + if( viz[ vt[x][k] ] == 1 ) + DFT( vt[x][k] , s ); +} + +void TareConex() +{ + int i; + for( i = 1 ; i <= N ; i++) + if( viz[i] == 0 ) + DF(i); + for( i = N ; i >= 1 ; i--) + if( viz[ postord[i] ] == 1 ) + { + S++; + DFT(postord[i],S); + } + g<>N>>M; + for(int i = 1 ; i <= M ; i++) + { + f>>x>>y; + v[x].push_back(y); + vt[y].push_back(x); + } + TareConex(); + return 0; +} diff --git a/scc/kosaraju/ce94545e-bdbf-43df-9de2-7694af2c7870.cpp b/scc/kosaraju/ce94545e-bdbf-43df-9de2-7694af2c7870.cpp new file mode 100644 index 0000000000000000000000000000000000000000..14ef6c0d0151d8bc9dbf12ec2059e388d1740123 --- /dev/null +++ b/scc/kosaraju/ce94545e-bdbf-43df-9de2-7694af2c7870.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N = 1e5 + 5; + + + +vector g[N], gt[N], ordine, componenteConexe[N]; + +bool vizitat[N], cc[N]; + + + +void dfs1(int nod) + +{ + + vizitat[nod] = true; + + + + for (int next : g[nod]) + + if (!vizitat[next]) + + dfs1(next); + + + + ordine.push_back(nod); + +} + + + +void dfs2(int nod, int ccx) + +{ + + cc[nod] = true; + + componenteConexe[ccx].push_back(nod); + + + + for (int next : gt[nod]) + + if (!cc[next]) + + dfs2(next, ccx); + +} + + + +int main() + +{ + + int n, m; + + fin >> n >> m; + + + + for (int i = 0; i < m; i++) + + { + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + + + + for (int i = 1; i <= n; i++) + + { + + if (!vizitat[i]) + + dfs1(i); + + } + + + + int cci = 1; + + for (int i = ordine.size() - 1; i >= 0; i--) + + { + + if (!cc[ordine[i]]) + + dfs2(ordine[i], cci++); + + } + + + + fout << cci - 1 << '\n'; + + for (int i = 1; i < cci; i++) + + { + + for (int nod : componenteConexe[i]) + + fout << nod << ' '; + + fout << '\n'; + + } + +} diff --git a/scc/kosaraju/cec5eba5-eae5-408a-8ff4-23eca92f2765.cpp b/scc/kosaraju/cec5eba5-eae5-408a-8ff4-23eca92f2765.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a67351268f7904dffcb3c8cd0017bcf4c4206eb5 --- /dev/null +++ b/scc/kosaraju/cec5eba5-eae5-408a-8ff4-23eca92f2765.cpp @@ -0,0 +1,186 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +struct nod{ + +int info; + +nod *urm; + +}; + +nod *l1[100001]; + +nod *l2[100001]; + +nod *sol[100001]; + + + +int n,m,ct; + +int viz1[100001],viz2[100001]; + +int c[100001],cp,cu; + + + +void citire() + +{ + + int i,x,y; + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>x>>y; + + nod *p=new nod; + + p->info=y; + + p->urm=l1[x]; + + l1[x]=p; + + + + p=new nod; + + p->info=x; + + p->urm=l2[y]; + + l2[y]=p; + + } + +} + +void dfs1(int i) + +{ + + viz1[i]=1; + + for(nod *p=l1[i];p!=NULL;p=p->urm)if(viz1[p->info]==0)dfs1(p->info); + + + + c[++cu]=i; + +} + +void parc1() + +{ + + int i; + + for(i=1;i<=n;i++) + + if(viz1[i]==0)dfs1(i); + +} + +void dfs2(int i) + +{ + + viz2[i]=1; + + + + nod *q=new nod; + + q->info=i; + + q->urm=sol[ct]; + + sol[ct]=q; + + + + for(nod *p=l2[i];p!=NULL;p=p->urm)if(viz2[p->info]==0)dfs2(p->info); + +} + +void parc2() + +{ + + int j; + + cp=1; + + while(cu>0) + + { + + j=c[cu--]; + + if(viz2[j]==0) + + { + + ct++; + + dfs2(j); + + } + + } + +} + +void afisare(){ + + nod *p; + + int i; + + fout<info<<" "; + + p=p->urm; + + } + + fout<<"\n"; + + } + +} + +int main() + +{ + + citire(); + + parc1(); + + parc2(); + + afisare(); + + return 0; + +} diff --git a/scc/kosaraju/cf85290e-3db0-4d68-893d-3bac759d0a35.cpp b/scc/kosaraju/cf85290e-3db0-4d68-893d-3bac759d0a35.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6269c4fa5efd1c3287fdd5515fc10d2fd5f2fc1f --- /dev/null +++ b/scc/kosaraju/cf85290e-3db0-4d68-893d-3bac759d0a35.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,i,m,a,b,j,nr; + +vectorv[100001],vt[100001]; + +bool viz[100001],Viz[100001]; + +vectorctc[100001]; + +stackst; + +void dfs(int vf) + +{ + + viz[vf]=true; + + for(int i=0; i>n>>m; + + for(i=1; i<=m; i++) + + { + + fin>>a>>b; + + v[a].push_back(b); + + vt[b].push_back(a); + + } + + for(j=1; j<=n; j++) + + { + + if(!viz[j]) + + dfs(j); + + } + + while(!st.empty()) + + { + + j=st.top(); + + st.pop(); + + if(!Viz[j]) + + { + + Dfs(j); + + nr++; + + ctc[j].push_back(j); + + } + + } + + fout< +using namespace std; +int n,m,postord[100005], t, viz[100005], k; + +vector G[100005], GT[100005], ctc[100005]; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +void DFS(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + + + + + + + for (i = 0;i< G[x].size();i++) + + { + + w=G[x][i]; + + if (!viz[w]) + + DFS(w); + + } + +t++; + +postord[t] = x; + +} + + + + + + + +void DFST(int x) + +{ + + int i, w; + + + + + + + + viz[x] = 1; + + ctc[k].push_back(x); + + for (i = 0;i>n>>m; + + for (i=1;i<=m;i++) + + { + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + + + for (i = 1; i <= n; i++) + + if (!viz[i]) + + DFS(i); + for(i=1;i<=n;i++) + + viz[i]=0; + + + + + + + + k=0; + + + + for (i = n; i>=1; i--) + + if (!viz[postord[i]]) + + { + + k++; + + DFST(postord[i]); + + } + + + + + + + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("ctc.in"); ofstream cout("ctc.out"); + + + + + + + +const int maxn = 100005; + + + +vector gr[maxn], gr2[maxn]; + + + +int used[maxn]; + + + +vector ord, comp; + +vector > ans; + + + + + + + +void dfs1(int nod){ + + used[nod] = 1; + + + + for (auto& x : gr[nod]){ + + if (!used[x]) dfs1(x); + + } + + ord.push_back(nod); + +} + + + +void dfs2 (int nod){ + + used[nod] = 1; + + comp.push_back(nod); + + + + for (auto & x : gr2[nod]){ + + if (!used[x]) dfs2(x); + + } + +} + + + + + + + +int main() { + + + + int n, m; cin >> n >> m; + + + + for (int i = 1; i <= m; i++){ + + int a, b; cin >> a >> b; + + + + gr[a].push_back(b); + + gr2[b].push_back(a); + + } + + + + for (int i = 1; i <= n; i++){ + + if (!used[i]) dfs1(i); + + } + + + + for (int i = 1; i <= n; i++) used[i] = 0; + + + + reverse (ord.begin(), ord.end()); + + + + for (auto & i : ord){ + + if (!used[i]) dfs2(i); + + if (!comp.empty()) ans.push_back(comp); + + comp.clear(); + + } + + + + cout << ans.size() << '\n'; + + for (auto& vec : ans){ + + for (auto& elem : vec){ + + cout << elem << ' '; + + } + + cout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/d2bef7c6-feae-4bca-820c-40ebdd6e6c69.cpp b/scc/kosaraju/d2bef7c6-feae-4bca-820c-40ebdd6e6c69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..600558474ea8e6b84eb8ae898dda6d8925419429 --- /dev/null +++ b/scc/kosaraju/d2bef7c6-feae-4bca-820c-40ebdd6e6c69.cpp @@ -0,0 +1,143 @@ +#include +using namespace std; + +ifstream fin( "ctc.in" ); + +ofstream fout( "ctc.out" ); + +void fast() + +{ + + cin.tie( 0 ); + + ios_base::sync_with_stdio( 0 ); + +} + +vectorg[100005], sol[100005]; + +vectorgt[100005]; + +vectorord; + +int use[100005]; + +int m, n, nrctc; + +void dfs( int x ) + +{ + + use[x] = 1; + + + + for( auto it : g[x] ) + + if( !use[it] ) + + dfs( it ); + + + + ord.push_back( x ); + +} + +void ctc( int x ) + +{ + + sol[nrctc].push_back( x ); + + use[x] = 1; + + + + for( auto it : gt[x] ) + + if( !use[it] ) + + ctc( it ); + +} + +int main() + +{ + + fast(); + + fin >> n >> m; + + + + for( int i = 1; i <= m; i++ ) + + { + + int x, y; + + fin >> x >> y; + + g[x].push_back( y ); + + gt[y].push_back( x ); + + } + + + + for( int i = 1; i <= n; i++ ) + + if( !use[i] ) + + dfs( i ); + reverse( ord.begin(), ord.end() ); + + + + for( int i = 1; i <= n; i++ ) + + use[i] = 0; + + + + for( auto it : ord ) + + if( !use[it] ) + + { + + nrctc++; + + ctc( it ); + + } + + + + fout << nrctc << '\n'; + + + + for( int i = 1; i <= nrctc; i++ ) + + { + + for( auto it : sol[i] ) + + fout << it << ' '; + + + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/d36c2d08-f041-4976-b92d-247a2bb81795.cpp b/scc/kosaraju/d36c2d08-f041-4976-b92d-247a2bb81795.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ed4e6dd5e0eabfc887fa0799c801b45ab5cd35f --- /dev/null +++ b/scc/kosaraju/d36c2d08-f041-4976-b92d-247a2bb81795.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,vis[100005],k,ans,st[100005]; + +vector v[100005],vt[100005],comp[100005]; + +void dfs(int nod) + +{ + + vis[nod]=1; + + for(auto it:v[nod]) + + if(vis[it]==0) dfs(it); + + st[++k]=nod; + +} + +void dfst(int nod) + +{ + + vis[nod]=1; + + comp[ans].push_back(nod); + + for(auto it:vt[nod]) + + if(vis[it]==0) dfst(it); + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(vis[i]==0) dfs(i); + + memset(vis,0,sizeof(vis)); + + while(k>0) + + { + + ans++; + + dfst(st[k]); + + while(vis[st[k]]==1&&k>0) { + + k--; + + } + + } + + out< +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, nr; + +bool v[100005]; + +vector g[100005], gt[100005]; + +vector noduri; + +vector ctc[100005]; + + + +void citire() { + + fin >> n >> m; + + while(m--) { + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs1(int x) { + + v[x] = 1; + + for(auto next: g[x]) + + if(!v[next]) + + dfs1(next); + + noduri.push_back(x); + +} + + + +void dfs2(int x) { + + v[x] = 1; + + ctc[nr].push_back(x); + + for(auto next: gt[x]) + + if(!v[next]) + + dfs2(next); + +} + + + +void solve() { + + for(int i = 1; i <= n; i++) + + if(!v[i]) + + dfs1(i); + + for(int i = 1; i <= n; i++) v[i] = 0; + + for(int i = noduri.size()-1; i >= 0; i--) { + + if(!v[noduri[i]]) { + + nr++; + + dfs2(noduri[i]); + + } + + } + +} + + + +void afis() { + + fout << nr << '\n'; + + for(int i = 1; i <= nr; i++) { + + for(auto x: ctc[i]) + + fout << x << ' '; + + fout << '\n'; + + } + +} + + + +int main() { + + citire(); + + solve(); + + afis(); + +} diff --git a/scc/kosaraju/d428cb26-b7c4-42f8-a98a-421a036d9ead.cpp b/scc/kosaraju/d428cb26-b7c4-42f8-a98a-421a036d9ead.cpp new file mode 100644 index 0000000000000000000000000000000000000000..910da06c269dd6ef78057d4b847e6303a6608dea --- /dev/null +++ b/scc/kosaraju/d428cb26-b7c4-42f8-a98a-421a036d9ead.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,st[100005],nrc,vis[100005],k; + +vector v[100005],vt[100005]; + +vector comp[100005]; + +void dfs(int nod) + +{ + + vis[nod]=1; + + for(auto it:v[nod]) + + if(vis[it]==0) dfs(it); + + st[++k]=nod; + +} + +void dfst(int nod) + +{ + + vis[nod]=1; + + comp[nrc].push_back(nod); + + for(auto it:vt[nod]) + + if(vis[it]==0) dfst(it); + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + vt[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + if(vis[i]==0) + + dfs(i); + + memset(vis,0,sizeof(vis)); + + while(k>0) + + { + + nrc++; + + dfst(st[k]); + + while(k>0&&vis[st[k]]==1) k--; + + } + + out< +#include +using namespace std; + +const int NMAX = 100005; + +vector g[NMAX]; + +vector gv[NMAX]; + +bool viz[NMAX]; + +vector toposort; + +void dfs(int u) { + + viz[u] = 1; + + for(int v : g[u]) { + + if(viz[v] == 0) { + + dfs(v); + + } + + } + + toposort.push_back(u); + +} + +vector sol[NMAX]; + +int sz = 0; + +void gdfs(int u) { + + viz[u] = 1; + + for(int v : gv[u]) { + + if(viz[v] == 0) { + + gdfs(v); + + } + + } + + sol[sz].push_back(u); + +} + + + +int main() { + + int n, m; + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + scanf("%d%d", &n, &m); + + for(int i = 1; i <= m; i++) { + + int x, y; + + scanf("%d%d", &x, &y); + + g[x].push_back(y); + + gv[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) { + + if(viz[i] == 0){ + + dfs(i); + + } + + } + + for(int i = 0; i <= n; i++) { + + viz[i] = 0; + + } + + for(int i = toposort.size() - 1; i > -1; i--) { + + int val = toposort[i]; + + if(viz[toposort[i]] == 0) { + + sz++; + + gdfs(toposort[i]); + + } + + } + + printf("%d\n", sz); + + for(int i = 1; i <= sz; i++) { + + for(int j = 0; j < sol[i].size(); j++) { + + printf("%d ", sol[i][j]); + + } + + printf("\n"); + + } + + return 0; + +} diff --git a/scc/kosaraju/d5f17804-2c79-4965-80df-2e0c2705065b.cpp b/scc/kosaraju/d5f17804-2c79-4965-80df-2e0c2705065b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8e19f20916a91ef23f6a876675792db1b205962b --- /dev/null +++ b/scc/kosaraju/d5f17804-2c79-4965-80df-2e0c2705065b.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector muchii[100005], muchiit[100005], rez[100005]; + +int n, m, viz[100005], S[100005], top, vizt[100005], cate; + + + +void DFS(int nod) { + + + + int i; + + viz[nod] = 1; + + for ( auto i: muchii[nod]) + + if (viz[i] == 0) + + DFS(i); + + + + S[++top] = nod; + +} + + + +void DFSt(int nod) { + + + + int i; + + vizt[nod] = 1; + + + + rez[cate].push_back(nod); + + + + for( auto i: muchiit[nod]) + + if(vizt[i] == 0) { + + DFSt(i); + + } + +} + + + +int main() + +{ + + int i, x, y, j; + + + + fin >> n >> m; + + + + for (i = 1; i <= m; i++) { + + + + fin >> x >> y; + + muchii[x].push_back(y); + + muchiit[y].push_back(x); + + } + + + + for ( i = 1; i <= n; i++) + + if(viz[i] == 0) + + DFS(i); + + + + while(top != 0) { + + + + if(vizt[S[top]] == 0) { + + cate++; + + + + DFSt(S[top]); + + + + } + + top--; + + } + + + + fout << cate << "\n"; + + + + for ( i = 1; i <= cate; i++) { + + + + for ( auto j: rez[i]) + + fout << j << " "; + + + + fout << "\n"; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/d60e8080-fcbb-4aa4-9f5c-44b7969c1bc0.cpp b/scc/kosaraju/d60e8080-fcbb-4aa4-9f5c-44b7969c1bc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d05b6bb9874de7cd720174fcdc4beabc45ad7290 --- /dev/null +++ b/scc/kosaraju/d60e8080-fcbb-4aa4-9f5c-44b7969c1bc0.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +using namespace std; + +int n, m, ctc; + +stack st; + +vector v1[100005]; +vector v2[100005]; +vector ans[100005]; +vector comp; + +void dfs1(int); +void dfs2(int); + +bool viz[100005], viz1[100005], viz2[100005]; + + +int main(int argc, const char * argv[]) { + + FILE *f = fopen("ctc.in","r"); + FILE *g = fopen("ctc.out", "w"); + fscanf(f, "%d %d\n", &n, &m); + while (m--) { + int x, y; + fscanf(f, "%d %d\n", &x, &y); + v1[x].push_back(y); + v2[y].push_back(x); + } + for (int i = 1; i <= n; ++i) { + if (!viz1[i]) { + dfs1(i); + } + } + + while (!st.empty()) { + if (!viz2[st.top()]) { + ++ctc; + dfs2(st.top()); + } + st.pop(); + } + fprintf(g, "%d\n",ctc); + for (int i = 1; i <= ctc; ++i) { + for (auto it:ans[i]) + fprintf(g, "%d ", it); + fprintf(g, "\n"); + } + return 0; +} + +void dfs1(int node) { + viz1[node] = 1; + for (auto it:v1[node]) + if (viz1[it] == 0) { + dfs1(it); + } + st.push(node); +} + +void dfs2(int node) { + viz2[node] = 1; + ans[ctc].push_back(node); + + for (auto it:v2[node]) + if (viz2[it] == 0) { + dfs2(it); + } +} diff --git a/scc/kosaraju/d6a9b3c4-507a-4404-a82f-43f765f86933.cpp b/scc/kosaraju/d6a9b3c4-507a-4404-a82f-43f765f86933.cpp new file mode 100644 index 0000000000000000000000000000000000000000..992903027b2da3ff80159b41fbd64fb8f7ba20f5 --- /dev/null +++ b/scc/kosaraju/d6a9b3c4-507a-4404-a82f-43f765f86933.cpp @@ -0,0 +1,180 @@ +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +const int NMAX = 100000; + + + +struct nod + +{ + + int x; + + nod * next; + +}; +nod *G[NMAX +1], *GT[NMAX +1], *CTC[NMAX +1]; + +int N, M, nrc, nr, post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +void add(nod *&cap_lst, int nod_ad) + +{ + + nod *p; + + p = new nod; + + p -> x = nod_ad; + + p -> next = cap_lst; + + cap_lst = p; + +} + + + +void citireGraf() + +{ + + f >> N >> M; + + while(M--) + + { + + int x, y; + + f >> x >> y; + + add(G[x], y); + + add(GT[y], x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(nod *p = G[vf]; p != NULL; p = p -> next) + + if(viz[p -> x] == 0) + + DFS(p -> x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) + +{ + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for(nod *p = GT[vf]; p != NULL; p = p -> next) + + if(viz[p -> x] == 1) + + DFSt(p -> x); + +} + + + +void componente() + +{ + + int i; + + for(i = 1; i <= N; i++) + + if(viz[i] == 0) + + DFS(i); + + + + for(i = N; i >= 1; i--) + + if(viz[post[i]] == 1) + + { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() + +{ + + g << nrc << '\n'; + + for(int i = 1; i <= nrc; i++) + + { + + for(nod *p = CTC[i]; p != NULL; p = p -> next) + + g << p -> x << ' '; + + g << '\n'; + + } + +} + + + +int main() + +{ + + + + citireGraf(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/d71928b5-d5d7-49a8-a60d-23daec275952.cpp b/scc/kosaraju/d71928b5-d5d7-49a8-a60d-23daec275952.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1870f6cf3556e039daac7401d94c916c1ce77a36 --- /dev/null +++ b/scc/kosaraju/d71928b5-d5d7-49a8-a60d-23daec275952.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, nr_ctc; + +int vizitat[100005]; + +vector G[100005], GT[100005], CTC[100005]; + +stack Stiva; + + + +void read() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x, y; + + fin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void dfs_graf(int nod) + +{ + + vizitat[nod]=1; + + for(int i=0;i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int DMAX = 100005; + + + +unsigned int n, m, nrCTC; + + + +vector G[DMAX], GT[DMAX], CTC[DMAX]; + + + +unsigned int viz[DMAX]; + + + +stack stiva; + + + +void read() + +{ + + fin >> n >> m; + + + + for (unsigned int i = 1; i <= m; i++) + + { + + unsigned int x, y; + + + + fin >> x >> y; + + + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void dfs(unsigned int nod) + +{ + + viz[nod] = 1; + + + + for (auto vecin : G[nod]) + + { + + if (viz[vecin] == 0) + + { + + dfs(vecin); + + } + + } + + + + stiva.push(nod); + +} + + + +void dfs_t(unsigned int nod) + +{ + + viz[nod] = 2; + + CTC[nrCTC].push_back(nod); + + + + for (auto vecin : GT[nod]) + + { + + if (viz[vecin] == 1) + + { + + dfs_t(vecin); + + } + + } + +} + + + +void kosaraju() + +{ + + for (unsigned int i = 1; i <= n; i++) + + { + + if (viz[i] == 0) + + { + + dfs(i); + + } + + } + + + + while (!stiva.empty()) + + { + + unsigned int nod = stiva.top(); + + + + if (viz[nod] == 1) + + { + + nrCTC++; + + dfs_t(nod); + + } + + + + stiva.pop(); + + } + +} + + + +void solve() + +{ + + + +} + + + +void show() + +{ + + fout << nrCTC << '\n'; + + + + for (unsigned int i = 1; i <= nrCTC; i++) + + { + + for (auto nod : CTC[i]) + + { + + fout << nod << ' '; + + } + + + + fout << '\n'; + + } + +} + + + +int main() + +{ + + read(); + + kosaraju(); + + solve(); + + show(); + + + + return 0; + +} diff --git a/scc/kosaraju/d9d20bf8-1b24-4a38-8fdb-bb1572ad90bb.cpp b/scc/kosaraju/d9d20bf8-1b24-4a38-8fdb-bb1572ad90bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08a48a7b15cf882fbb16dbf314f1948ea634c05d --- /dev/null +++ b/scc/kosaraju/d9d20bf8-1b24-4a38-8fdb-bb1572ad90bb.cpp @@ -0,0 +1,95 @@ +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +const int N = 100001, M = 200001; + +int vf[M], urm[M], lst[N], viz[N], st[N], vf1[M], urm1[M], lst1[N], viz1[N], st1[N], nrst1, nrst, nr, nr1, c; + +void adauga(int x, int y) +{ + vf[++nr] = y; + urm[nr] = lst[x]; + lst[x] = nr; +} + +void adauga1(int x, int y) +{ + vf1[++nr1] = y; + urm1[nr1] = lst1[x]; + lst1[x] = nr1; +} + +void dfs(int x) +{ + viz[x] = true; + for(int p = lst[x]; p != 0; p = urm[p]) + { + int y = vf[p]; + if(!viz[y]) + dfs(y); + } + st[++nrst] = x; +} + +void dfs1(int x) +{ + viz1[x] = true; + for(int p = lst1[x]; p != 0; p = urm1[p]) + { + int y = vf1[p]; + if(!viz1[y]) + dfs1(y); + } +} + +void dfs2(int x) +{ + viz1[x] = true; + + for(int p = lst1[x]; p != 0; p = urm1[p]) + { + int y = vf1[p]; + if(!viz1[y]) + out << y << " ", dfs2(y); + } +} + +int main() +{ + int n, m, x, y; + in >> n >> m; + for(int i = 1; i <= m; i++) + { + in >> x >> y; + adauga(x, y); + adauga1(y, x); + } + for(int i = 1; i <= n; i++) + { + if(!viz[i]) + dfs(i); + } + + for(int i = nrst; i >= 1; i--) + { + if(!viz1[st[i]]) + c++, dfs1(st[i]); + } + out << c << "\n"; + for(int i = 1; i <= nrst; i++) + viz1[st[i]] = false; + for(int i = nrst; i >= 1; i--) + { + if(!viz1[st[i]]) + { + out << st[i] << " "; + dfs2(st[i]); + out << "\n"; + } + } + return 0; +} diff --git a/scc/kosaraju/db90b041-90a8-4c42-8a6c-9ae91b441fee.cpp b/scc/kosaraju/db90b041-90a8-4c42-8a6c-9ae91b441fee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cc62ee1990243b85518c2ddc3b5fcf851e28065 --- /dev/null +++ b/scc/kosaraju/db90b041-90a8-4c42-8a6c-9ae91b441fee.cpp @@ -0,0 +1,213 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m; + +int n_dfs_forest; + +int n_scc; + + + +vector * lvg; + +vector * lvgt; + + + +vector > dfs_forest; + +int * visited; + + + + + +vector > scc; + + + +void DFS(int node){ + + + + for (int i = 0; i < lvg[node].size(); i++) + + + + if (!visited[lvg[node][i]]){ + + + + visited[lvg[node][i]] = visited[node]; + + DFS(lvg[node][i]); + + } + + + + dfs_forest[n_dfs_forest - 1].push_back(node); + +} + + + +void DFS_transposed(int node){ + + + + scc[n_scc - 1].push_back(node); + + + + for (int i = 0; i < lvgt[node].size(); i++){ + + + + if (visited[lvgt[node][i]] > 0){ + + + + visited[lvgt[node][i]] = -1; + + DFS_transposed(lvgt[node][i]); + + } + + } + +} + + + + + +int main(){ + + + + f >> n >> m; + + + + lvg = new vector [n + 1]; + + lvgt = new vector [n + 1]; + + + + visited = new int[n + 1]; + + + + for(int i = 0; i <= n; i++){ + + + + visited[i] = 0; + + } + + + + int x, y; + + for (int cnt = 0; cnt < m; cnt++){ + + + + f >> x >> y; + + + + lvg[x].push_back(y); + + lvgt[y].push_back(x); + + } + for (int i = 1; i <= n; i++){ + + + + if (!visited[i]){ + + + + n_dfs_forest = dfs_forest.size() + 1; + + dfs_forest.resize(n_dfs_forest); + + + + visited[i] = i; + + DFS(i); + + + + while (dfs_forest[n_dfs_forest - 1].size() > 0){ + + + + int current_node = dfs_forest[n_dfs_forest - 1].back(); + + dfs_forest[n_dfs_forest - 1].pop_back(); + + + + if (visited[current_node] > 0){ + + + + n_scc = scc.size() + 1; + + scc.resize(n_scc); + + + + visited[current_node] = -1; + + DFS_transposed(current_node); + + } + + } + + } + + } + + + + g << scc.size() << '\n'; + + for (int i = 0; i < scc.size(); i++){ + + + + for (int j = 0; j < scc[i].size(); j++){ + + + + g << scc[i][j] << " "; + + } + + g << '\n'; + + } + return 0; + +} diff --git a/scc/kosaraju/db96a475-6253-4fc1-847b-9c2890f8d408.cpp b/scc/kosaraju/db96a475-6253-4fc1-847b-9c2890f8d408.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd707ea907377e22205681a381ea8190fbab6580 --- /dev/null +++ b/scc/kosaraju/db96a475-6253-4fc1-847b-9c2890f8d408.cpp @@ -0,0 +1,129 @@ +#include +using namespace std; +int N,M; + +vectorGraf1[100001],Graf2[100001]; + +stack stiva; + + + +short int vizitat[100001]; + + + +int Nrctc; + +vector Ctc[100001]; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +void citire() + +{ in>>N>>M; + + for(int i=1;i<=M;i++) + + { int x,y; + + in>>x>>y; + + Graf1[x].push_back(y); + + Graf2[y].push_back(x); + + } + +} + +void dfs1(int k) + +{ vizitat[k]=1; + + for(size_t i=0;i +#include +using namespace std; + +ifstream f ("ctc.in"); + +ofstream g ("ctc.out"); + +int n, m; + +vector v[100005]; + +vector vr[100005]; + +vector sk; + +int viz[100005], viz2[100005]; + +vector> componente; +void dfs(int x) + +{ + + if(viz[x]==0) + + { + + viz[x]=1; + + for(int i=0; i>n>>m; + + for(int i=0; i>x>>y; + + v[x].push_back(y); + + vr[y].push_back(x); + + } + + + + int modificari=1; + + while(modificari==1) + + { + + modificari=0; + + for(int i=1; i<=n; i++) + + { + + if(viz[i]==0) + + { + + dfs(i); + + modificari=1; + + } + + } + + } + + + + int index=0; + + reverse(sk.begin(), sk.end()); + + modificari=1; + + while(modificari==1) + + { + + modificari=0; + + for(int i=0; i r; + + componente.push_back(r); + + dfs_reverse(sk[i], index); + + modificari=1; + + index++; + + } + + } + + } + + g< + + + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +stack < int > s; + +vector < int > v1[100005], v2[100005], R[100005]; + +bitset < 100005 > viz1, viz2; + +int nr; + + + +void dfs1 ( int nod ); + +void dfs2 ( int nod ); + + + +int main() + +{ + + int n, m, x, y, i, j; + + + + fin >> n >> m; + + while ( m-- ) + + { + + fin >> x >> y; + + v1[x].push_back ( y ); + + v2[y].push_back ( x ); + + } + + + + for ( i = 1 ; i <= n ; i++ ) if ( viz1[i] == 0 ) dfs1 ( i ); + + + + while ( s.empty() == 0 ) + + { + + x = s.top(); + + if ( viz2[x] == 0 ) + + { + + nr++; + + dfs2 ( x ); + + } + + + + s.pop(); + + } + + + + fout << nr << '\n'; + + for ( i = 1 ; i <= nr ; i++ ) + + { + + for ( j = 0 ; j < R[i].size() ; j++ ) fout << R[i][j] << ' '; + + fout << '\n'; + + } + + + + return 0; + +} + + + +void dfs1 ( int nod ) + +{ + + int i; + + + + viz1[nod] = 1; + + for ( i = 0 ; i < v1[nod].size() ; i++ ) if ( viz1[v1[nod][i]] == 0 ) dfs1 ( v1[nod][i] ); + + s.push ( nod ); + +} + + + +void dfs2 ( int nod ) + +{ + + int i; + + + + viz2[nod] = 1; + + for ( i = 0 ; i < v2[nod].size() ; i++ ) if ( viz2[v2[nod][i]] == 0 ) dfs2 ( v2[nod][i] ); + + R[nr].push_back ( nod ); + +} diff --git a/scc/kosaraju/de4dbad3-120a-4efc-86f3-07baf263b786.cpp b/scc/kosaraju/de4dbad3-120a-4efc-86f3-07baf263b786.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e22f874f53f23d57c0fc8dbc7b59414f9d9fb112 --- /dev/null +++ b/scc/kosaraju/de4dbad3-120a-4efc-86f3-07baf263b786.cpp @@ -0,0 +1,116 @@ +#include +using namespace std; + +int viz[100001],v[100001]; + +vector G[100001],GT[100001],L[100001]; + +int mn[100001]; + +int loc; + +int nrc; + +int DFS(int x) + +{ + + viz[x]=1; + + for(auto i:G[x]) + + if(viz[i]==0) DFS(i); + + loc++; + + v[loc]=x; + +} + +int DFS2(int x) + +{ + + viz[x]=nrc; + + L[nrc-1].push_back(x); + + for(auto i:GT[x]) + + if(viz[i]==1) DFS2(i); + +} + + + +int main() + +{ + + int n,m,i,x,y,mini,j,ok; + + loc=0; + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(i=1;i<=n;i++) + + if(viz[i]==0) + + { + + DFS(i); + + } + + nrc=1; + + for(i=loc;i>=1;i--) + + if(viz[v[i]]==1) + + { + + nrc++; + + DFS2(v[i]); + + } + + nrc--; + + g< +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vector G[100002] , GT[100002] , CTC[100002]; + +int member_of_CTC[100002] , nr_CTC = 0; + +int viz[100002]; + +stack s; + + + +int n , m; + +void dfs1(int nod){ + + viz[nod] = 1; + + int size_ = G[nod].size(); + + int vecin ; + + + + for(int i = 0 ; i>n>>m; + + int x , y , nod; + + while(m--){ + + cin>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(int i = 1 ; i<=n ; i++){ + + if(!viz[i]) dfs1(i); + + } + + + + while(!s.empty()){ + + nod = s.top(); + + + + if(viz[nod]==1){ + + nr_CTC ++; + + dfs2(nod); + + } + + s.pop(); + + } + + cout< + + +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +const int dim=1e5+10; + +typedef long long ll; + +typedef pair pi; + +int t,T,n,m,a,b,cnt; + + + +vector < int > V[dim],A,viz(dim,0),VT[dim],B[dim]; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + for(unsigned int vecin:V[nod]) + + { + + if(!viz[vecin]) + + DFS(vecin); + + } + + viz[nod]=2; + + A.push_back(nod); + +} + + + +void DFST(int nod) + +{ + + B[cnt].push_back(nod); + + viz[nod]=1; + + for(unsigned int vecin:VT[nod]) + + { + + if(!viz[vecin]) + + DFST(vecin); + + } + + viz[nod]=2; + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>a>>b; + + V[a].push_back(b); + + VT[b].push_back(a); + + } + + for(int i=1;i<=n;i++) + + if(!viz[i]) DFS(i); + + + + for(int i=1;i<=n;i++) viz[i]=0; + + + + reverse(A.begin(),A.end()); + + for(unsigned int x:A) + + if(!viz[x]) + + { + + cnt++; + + DFST(x); + + } + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m, nrc, viz[100005]; + +vector graph[100005], gt[100005]; + +stack S; + +vector cc[100005]; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = -1; + + for(auto &v:graph[vf]) + + if(viz[v] == 0) + + DFS(v); + + S.push(vf); + +} + + + +void DFSt(int vf, int nrc) + +{ + + viz[vf] = 1; + + for(auto &v:gt[vf]) + + if(viz[v] < 0) + + DFSt(v, nrc); + + cc[nrc].push_back(vf); + +} + + + +void write() + +{ + + g< 0) + + continue; + + nrc++; + + DFSt(vf, nrc); + + } + + write(); + + return 0; + +} diff --git a/scc/kosaraju/e438b9e5-86f7-4709-b770-d8a8a29b0e1f.cpp b/scc/kosaraju/e438b9e5-86f7-4709-b770-d8a8a29b0e1f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d736899a6d05c69e7c56437d9ce76faf6f6f01c7 --- /dev/null +++ b/scc/kosaraju/e438b9e5-86f7-4709-b770-d8a8a29b0e1f.cpp @@ -0,0 +1,164 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMAX = 100005; + + + +stack < int > S; + +vector < int > G[NMAX], GT[NMAX], CTC[NMAX]; + + + +int n, m, nrCTC; + +int vis[NMAX]; + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; i++){ + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFS(int nod) + +{ + + vis[nod] = 1; + + for (unsigned int i = 0; i < G[nod].size(); i++){ + + int vecin = G[nod][i]; + + if (!vis[vecin]) + + DFS(vecin); + + } + + S.push(nod); + +} + + + +void DFSM(int nod) + +{ + + vis[nod] = 2; + + CTC[nrCTC].push_back(nod); + + for (unsigned int i = 0; i < GT[nod].size(); i++){ + + int vecin = GT[nod][i]; + + if (vis[vecin] == 1) + + DFSM(vecin); + + } + + + +} + + + +void solve() + +{ + + for (int i = 1; i <= n; i++) + + if (!vis[i]) + + DFS(i); + + + + while (!S.empty()){ + + int nod = S.top(); + + if (vis[nod] == 1){ + + nrCTC++; + + DFSM(nod); + + } + + S.pop(); + + } + +} + + + +void print() + +{ + + fout << nrCTC << "\n"; + + for (int i = 1; i <= nrCTC; i++){ + + for (unsigned int j = 0; j < CTC[i].size(); j++) + + fout << CTC[i][j] << " "; + + fout << "\n"; + + } + +} + + + +int main() + +{ + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/scc/kosaraju/e5d01590-6d31-4bee-aa30-87f0e58d8b32.cpp b/scc/kosaraju/e5d01590-6d31-4bee-aa30-87f0e58d8b32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4362d83438eab857afdf64f97ffeb0e49bdfca34 --- /dev/null +++ b/scc/kosaraju/e5d01590-6d31-4bee-aa30-87f0e58d8b32.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + +using namespace std; + + + + + + + +bool viz1[100005], viz2[100005]; + +vector graph1[100005]; + +vector graph2[100005]; + +vector comp[100005]; + +stack ord; + +int n, m, ncomp; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + + + +void dfs1(int nod) + +{ + + viz1[nod] = 1; + + int len, i, var; + + len = graph1[nod].size(); + + + + for(i = 0; i < len; i++) + + { + + var = graph1[nod][i]; + + if(!viz1[var]) + + dfs1(var); + + } + + ord.push(nod); + +} + + + +void dfs2(int nod) + +{ + + viz2[nod] = 1; + + int len, i, var; + + len = graph2[nod].size(); + + + + for(i = 0; i < len; i++) + + { + + var = graph2[nod][i]; + + if(!viz2[var]) + + dfs2(var); + + } + + comp[ncomp].push_back(nod); + +} + + + +int main() + +{ + + int i, j, x, y, len; + + f >> n >> m; + + + + for(i = 0; i < m; i++) + + { + + f >> x >> y; + + graph1[x].push_back(y); + + graph2[y].push_back(x); + + } + + + + for(i = 1; i <= n; i++) + + if(viz1[i] == 0) + + dfs1(i); + + + + while(!ord.empty()) + + { + + x = ord.top(); + + ord.pop(); + + if(!viz2[x]) + + { + + dfs2(x); + + ncomp++; + + } + + } + + + + g << ncomp << endl; + + + + for(i = 0; i < ncomp; i++) + + { + + len = comp[i].size(); + + + + for(j = 0; j < len; j++) + + g << comp[i][j] << ' '; + + + + g << '\n'; + + } + + return 0; + +} diff --git a/scc/kosaraju/e753312f-65e7-4531-8595-0bf86c50f18b.cpp b/scc/kosaraju/e753312f-65e7-4531-8595-0bf86c50f18b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48de0e8531d829082a4e81e88a5ad481679b936c --- /dev/null +++ b/scc/kosaraju/e753312f-65e7-4531-8595-0bf86c50f18b.cpp @@ -0,0 +1,333 @@ +#include +#include + + + + + + +using namespace __gnu_pbds; + +using namespace std; +typedef pair< int, int > pii; + +typedef pair< long long, long long > pll; + +typedef long long ll; + +typedef unsigned long long ull; + +typedef vector< int > vi; + +typedef vector< vi > vvi; + +typedef vector< ll > vll; + +typedef vector< vll > vvll; + +typedef vector< pii > vpii; + +typedef vector< vpii > vvpii; + +typedef vector< pll > vpll; + +typedef long double ld; + +typedef vector< ld > vld; + + + +const ll MOD = 998244353; + + + +const ull infull = numeric_limits::max(); + + + +void fix(int &x, ll MOD) { + + x = (x % MOD); + + if(x < 0) x += MOD; + + return; + +} + + + +ll lgput(ll a, ll b, ll MOD) { + + ll ret = 1; + + a %= MOD; + + while(b) { + + if(b&1) ret = ret*a % MOD; + + a = a*a % MOD; + + b >>= 1; + + } + + + + return ret; + +} + + + +ll inv(ll a, ll MOD) { + + return lgput(a, MOD-2, MOD); + +} + + + +struct f { + + int a, b; + + f(int _a = 0, int _b = 0) : a(_a) ,b(_b) {} + + ll eval(int x) { + + return 1ll*x*a + b; + + } + +}; + + + +int binarySearch(int x, vi &v) { + + int pos = 0; + + int l = 1, r = (int)((v).size()); + + while(l <= r) { + + int m = l + r >> 1; + + if(v[m] <= x) { + + pos = m; + + l = m + 1; + + } else { + + r = m - 1; + + } + + } + + + + return pos; + +} + + + +using Matrix = vvi; + + + +Matrix mult(Matrix a, Matrix b) { + + assert(a[0].size() == b.size()); + + + + Matrix ret(a.size(), vector< int >(b[0].size())); + + + + for(int i = 0; i < a.size(); ++i) { + + for(int j = 0; j < b[0].size(); ++j) { + + for(int k = 0; k < a[0].size(); ++k) { + + ret[i][j] += ((1ll*a[i][k]*b[k][j])%MOD); + + if(ret[i][j] >= MOD) ret[i][j] -= MOD; + + } + + } + + } + + + + return ret; + +} + + + +const double PI = acos(-1); + +const double eps = 1e-7; + + + +const int MAXN = 1e5 + 10; + +vi gr[MAXN]; + +vi gri[MAXN]; + +int viz[MAXN]; + +vi v; + +vvi ctc; + + + +void dfs1(int node) { + + viz[node] = 1; + + for(auto &x : gr[node]) { + + if(!viz[x]) + + dfs1(x); + + } + + + + v.emplace_back(node); + +} + + + +void dfs2(int node) { + + ctc.back().emplace_back(node); + + + + viz[node] = 0; + + for(auto &x : gri[node]) { + + if(viz[x]) { + + dfs2(x); + + } + + } + +} + + + +int main() { + {freopen("ctc"".in","r",stdin);freopen("ctc"".out","w",stdout);}; + + + + + + ios::sync_with_stdio(false); + + cin.tie(0); + + cout.tie(0); + + cout.precision(12); + + srand(time(NULL)); + + + + int n, m; + + cin >> n >> m; + + + + for(int i = 0; i < m; ++i) { + + int a, b; + + cin >> a >> b; + + gr[a].emplace_back(b); + + gri[b].emplace_back(a); + + } + + + + for(int i = 1; i <= n; ++i) { + + if(!viz[i]) { + + dfs1(i); + + } + + } + + + + reverse((v).begin(),(v).end()); + + + + for(auto &x : v) { + + if(viz[x]) { + + ctc.emplace_back(vi()); + + dfs2(x); + + } + + } + + + + + + cout << ctc.size() << '\n'; + + for(auto &x : ctc) { + + sort((x).begin(),(x).end()); + + for(auto &y : x) { + + cout << y << ' '; + + } + + + + cout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/kosaraju/e790eabb-6cf7-4796-aece-6df28cfea2ff.cpp b/scc/kosaraju/e790eabb-6cf7-4796-aece-6df28cfea2ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc0d55cf6b013fdb2c7a643fa53c0337592f0de0 --- /dev/null +++ b/scc/kosaraju/e790eabb-6cf7-4796-aece-6df28cfea2ff.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m, x, y, no_SCC; +vector> arches, op_arches, SCCs; +vector visited; +stack order; + +void dfs(int node){ + visited[node] = true; + for(auto &arch: arches[node]) + if(!visited[arch]) + dfs(arch); + order.push(node); +} + +void dfs_oposite(vector &SCC, int node){ + visited[node] = false; + SCC.emplace_back(node); + for(auto &arch: op_arches[node]) + if(visited[arch]) + dfs_oposite(SCC, arch); +} + +void read(){ + fin>>n>>m; + arches.resize(n); + op_arches.resize(n); + while(m--){ + fin>>x>>y; + x--;y--; + arches[x].emplace_back(y); + op_arches[y].emplace_back(x); + } +} + +void kosarju(){ + visited.resize(n, false); + for(int i=0;i +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graf[100005], grafInvers[100005]; + +int n, m; + +vector > totSol; + +vector sol; + +int viz[100005]; + +bool parc; + +stack S; + + + +void citire() + +{ + + f>>n>>m; + + int x, y; + + for(int i = 0; i>x>>y; + + graf[x].push_back(y); + + grafInvers[y].push_back(x); + + } + +} + + + +vector getGraf(int poz) + +{ + + if(!parc) + + return graf[poz]; + + else + + return grafInvers[poz]; + +} + + + +void dfs(int currNode) + +{ + + viz[currNode] = parc+1; + + if(parc) + + sol.push_back(currNode); + + for(int i : getGraf(currNode)) + + { + + if(viz[i] != parc+1) + + { + + dfs(i); + + } + + } + + if(!parc) + + S.push(currNode); + +} + + + +void parcUnu() + +{ + + for(int i = 1; i<=n; ++i) + + { + + if(viz[i] != parc+1) + + dfs(i); + + } + +} + + + +void parcDoi() + +{ + + parc = 1; + + int nod; + + while(!S.empty()) + + { + + nod = S.top(); + + S.pop(); + + if(viz[nod] != parc+1){ + + sol.clear(); + + dfs(nod); + + totSol.push_back(sol); + + } + + } + +} + + + +void afisare() + +{ + + g< +#include +#include +using namespace std; + +const int N=100001; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vectordus[N]; + +vectorintors[N]; + +vector>comp; + +vectorstiva; + +int n,m,x,y; + +int viz[N]; + + + +void dfs(int nod) + +{ + + viz[nod]=1; + + for(auto it: dus[nod]) + + { + + if(viz[it])continue; + + dfs(it); + + } + + stiva.push_back(nod); + +} + + + +void dfsIntors(int nod) + +{ + + viz[nod]=1; + + for(auto it:intors[nod]) + + { + + if(viz[it])continue; + + dfsIntors(it); + + } + + comp.back().push_back(nod); + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + fin>>x>>y; + + dus[x].push_back(y); + + intors[y].push_back(x); + + } + + for(int i=1;i<=n;i++) + + { + + if(!viz[i])dfs(i); + + } + + memset(viz,0,sizeof(viz)); + + for(int i=stiva.size()-1;i>=0;i--) + + { + + if(!viz[stiva[i]]){ + + comp.push_back(vector()); + + dfsIntors(stiva[i]); + + } + + } + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stack S; + +vector G[100005], tG[100005], CTC[100005]; + +int N, M, NrCTC; + +int vizitat[100005]; + + + +void Read() + +{ + + f >> N >> M; + + for (int i = 1; i <= M; i++) { + + int x, y; + + f >> x >> y; + + G[x].push_back(y); + + tG[y].push_back(x); + + } + +} + +void DFSP(int Nod) + +{ + + vizitat[Nod] = 1; + + for (unsigned int i = 0; i < G[Nod].size(); i++) { + + int vecin = G[Nod][i]; + + if (!vizitat[vecin]) + + DFSP(vecin); + + } + + S.push(Nod); + +} + +void DFSM(int Nod) + +{ + + vizitat[Nod] = 2; + + CTC[NrCTC].push_back(Nod); + + for (unsigned int i = 0; i < tG[Nod].size(); i++) { + + int vecin = tG[Nod][i]; + + if (vizitat[vecin] == 1) + + DFSM(vecin); + + } + +} + +void Kosoraju() + +{ + + for (int i = 1; i <= N; i++) + + if (!vizitat[i]) + + DFSP(i); + + while (!S.empty()) { + + int Nod = S.top(); + + cout << Nod << " "; + + if (vizitat[Nod] == 1) { + + NrCTC++; + + DFSM(Nod); + + } + + S.pop(); + + } + +} + +void Print() + +{ + + g << NrCTC << "\n"; + + for (int i = 1; i <= NrCTC; i++) { + + for (unsigned int j = 0; j < CTC[i].size(); j++) + + g << CTC[i][j] << " "; + + g << "\n"; + + } + +} + +int main() + +{ + + Read(); + + Kosoraju(); + + Print(); + + return 0; + +} diff --git a/scc/kosaraju/e9efeb53-98a2-4e7c-87b3-8915855f923d.cpp b/scc/kosaraju/e9efeb53-98a2-4e7c-87b3-8915855f923d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7319cff8bf7a597ae7d45c5355fe56df167a9ecc --- /dev/null +++ b/scc/kosaraju/e9efeb53-98a2-4e7c-87b3-8915855f923d.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m; + +vectorv[100005], g[100005], answer[100005]; + +int viz[100005], parc[100005], top, nrc; + +void DFS1(int x) + +{ + + viz[x] = 1; + + for(auto nod: v[x]) + + if(!viz[nod]) + + DFS1(nod); + + + + parc[++top] = x; + + + +} + +void DFS2(int x) + +{ + + viz[x] = 1; + + for(auto nod: g[x]) + + if(!viz[nod]) + + DFS2(nod); + + answer[nrc].push_back(x); + +} + + + + + +int main() + +{ + + int x, y; + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + v[x].push_back(y); + + g[y].push_back(x); + + } + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + DFS1(i); + + for(int i = 1; i <= n; i++) + + viz[i] = 0; + + for(int i = n; i >= 1; i--) + + if(!viz[parc[i]]) + + { + + nrc++; + + DFS2(parc[i]); + + } + + fout << nrc << "\n"; + + for(int i = 1; i <= nrc; i++) + + { + + for(auto nod: answer[i]) + + fout << nod << " "; + + fout << "\n"; + + + + } + + + + return 0; + +} diff --git a/scc/kosaraju/eb67528c-6987-4852-94db-24f2c6bb55c6.cpp b/scc/kosaraju/eb67528c-6987-4852-94db-24f2c6bb55c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40a9ab9f33c72245b610e6dc45ac174de8cfb414 --- /dev/null +++ b/scc/kosaraju/eb67528c-6987-4852-94db-24f2c6bb55c6.cpp @@ -0,0 +1,168 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector muchie[100005],c[100005],transpus[100005]; + +queue coada; + +int n, m, dist, a[100005], nr_comp; + +bool vizitat[100005]; + + + +void dfs(int x) + +{ + + vizitat[x]=1; + + + + for(unsigned int i=0; i>n>>m; + + for(unsigned int i=1; i<=m; i++) + + { + + int x, y; + + fin>>x>>y; + + muchie[x].push_back(y); + + transpus[y].push_back(x); + + } + + + + for(unsigned int i=1; i<=n; i++) + + if (!vizitat[i]) + + dfs(i); + + + + memset(vizitat,0,sizeof(vizitat)); + + + + + + for(unsigned int i=dist; i>=1; i--) + + if (!vizitat[a[i]]) + + { + + nr_comp++; + + bfs(a[i]); + + } + + + + + + fout< +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int NMAX = 100'001; + +int n,m, beenThere[NMAX], strongly_conn; +vector G[NMAX] , GT[NMAX] , sol[NMAX]; +stack s; + +void Read() { + + f >> n >> m; + + while(m--) { + int x, y; + f >> x >> y; + G[x].push_back(y); + GT[y].push_back(x); + } +} + +void dfs(int node) { + + beenThere[node] = 1; + + for(int neighbor : G[node]) + if(!beenThere[neighbor]) + dfs(neighbor); + + s.push(node); +} + +void dfsReverse(int node) { + + beenThere[node] = 1; + sol[strongly_conn].push_back(node); + + for(int neighbor : GT[node]) + if(!beenThere[neighbor]) + dfsReverse(neighbor); +} + +void kosaraju() { + + for(int i = 1;i <= n;++i) + if(!beenThere[i]) + dfs(i); + + for(int i = 1;i <= n;++i) + beenThere[i] = 0; + + while(!s.empty()) { + int node = s.top(); + if(beenThere[node] == 0) { + strongly_conn++; + dfsReverse(node); + } + s.pop(); + } +} + +void print() { + + g << strongly_conn << '\n'; + + for(int i = 1;i <= strongly_conn;++i,g << '\n') + for(int node : sol[i]) + g << node << ' '; +} + +int main() { + + Read(); + kosaraju(); + print(); + + return 0; +} diff --git a/scc/kosaraju/f06f1264-fe62-4681-99d6-d2a9a8dcb3bc.cpp b/scc/kosaraju/f06f1264-fe62-4681-99d6-d2a9a8dcb3bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5030493b0f483bf5f28618ef6ff4de4292418378 --- /dev/null +++ b/scc/kosaraju/f06f1264-fe62-4681-99d6-d2a9a8dcb3bc.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int N=100001; +const int M=2*N; + +int lst[N], vf[2*M], urm[2*M], lst_inv[N], vf_inv[2*M], urm_inv[2*M], sorttop[N], ctc[2*M]; +int nr, ct, cnt; + +bool viz[N]; + +void adauga(int x, int y) +{ + ++nr; + vf[nr]=y; + urm[nr]=lst[x]; + lst[x]=nr; + + vf_inv[nr]=x; + urm_inv[nr]=lst_inv[y]; + lst_inv[y]=nr; +} + +void dfs1(int x) +{ + viz[x]=true; + for(int p=lst[x]; p!=0; p=urm[p]) + { + int y=vf[p]; + if(!viz[y]) + dfs1(y); + } + sorttop[++nr]=x; +} + +void dfs2(int x) +{ + viz[x]=true; + for(int p=lst_inv[x]; p!=0; p=urm_inv[p]) + { + int y=vf_inv[p]; + if(!viz[y]) + { + cnt++; + ctc[cnt]=y; + dfs2(y); + } + } +} + +int main() +{ + int n,m; + fin>>n>>m; + for(int i=1; i<=m; i++) + { + int x,y; + fin>>x>>y; + adauga(x,y); + } + nr=0; + for(int i=1; i<=n; i++) + if(!viz[i]) + dfs1(i); + for(int i=1; i<=n; i++) + viz[i]=false; + for(int i=n; i>=1; i--) + { + if(!viz[sorttop[i]]) + { + ct++; + cnt++; + ctc[cnt]=sorttop[i]; + dfs2(sorttop[i]); + cnt++; + ctc[cnt]=-1; + } + } + fout< +#include + + +using namespace std; + + + +FILE* fin=fopen("ctc.in", "r"); + +FILE* fout=fopen("ctc.out", "w"); + + + +vector lista[100001]; + +vector listaTrans[100001]; + +vector comp[100001]; + +int n, m, ordine[100001], vizitat[100001], total, tot[100001], nr; + + + +void DFFinal(int nod) + +{ + + vizitat[nod]=1; + + for (int i=0; i +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +int n,m1,m[100001],p[100001],i,j,nrc,y,x; + +stack st; + +vector G[100001],GT[100001],ctc[100001]; + +void dfs1(int nod) + +{ + + p[nod]=1; + + for(int i=0;i>n>>m1; + + for(i=1;i<=m1;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + + for(i=1;i<=n;i++) + + { + + if(p[i]==0) + + dfs1(i); + + } + + while(!st.empty()) + + { + + int k=st.top(); + + + + st.pop(); + + if(m[k]==0) + + { + + nrc++; + + dfs2(k); + + } + + } + + for(i=1;i<=n;i++) + + ctc[m[i]].push_back(i); + + g< +#include +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, nr; + +bool v[100005]; + +vector g[100005], gt[100005], noduri, ctc[100005]; + + + +void citire() { + + fin >> n >> m; + + while(m--) { + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void dfs1(int x) { + + v[x] = 1; + + for(auto next: g[x]) + + if(!v[next]) + + dfs1(next); + + noduri.push_back(x); + +} + + + +void dfs2(int x) { + + v[x] = 1; + + ctc[nr].push_back(x); + + for(auto next: gt[x]) + + if(!v[next]) + + dfs2(next); + +} + + + +void solve() { + + for(int i = 1; i <= n; i++) + + if(!v[i]) + + dfs1(i); + + for(int i = 1; i <= n; i++) + + v[i] = 0; + + for(int i = noduri.size()-1; i >= 0; i--) + + if(!v[noduri[i]]) { + + nr++; + + dfs2(noduri[i]); + + } + +} + + + +void afis() { + + fout << nr << '\n'; + + for(int i = 1; i <= nr; i++) { + + for(auto x: ctc[i]) + + fout << x << ' '; + + fout << '\n'; + + } + +} + + + +int main() { + + citire(); + + solve(); + + afis(); + +} diff --git a/scc/kosaraju/f432b5a5-6673-4d98-970b-0eadfdffd80e.cpp b/scc/kosaraju/f432b5a5-6673-4d98-970b-0eadfdffd80e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc81f78e0dc0a5082a929a905f52cafa09ea90ac --- /dev/null +++ b/scc/kosaraju/f432b5a5-6673-4d98-970b-0eadfdffd80e.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m, nrc, viz[100005]; + +vector graph[100005], gt[100005]; + +stack S; + +vector cc[100005]; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = -1; + + for(auto &v:graph[vf]) + + if(viz[v] == 0) + + DFS(v); + + S.push(vf); + +} + + + +void DFSt(int nod, int nrc) + +{ + + viz[nod] = 1; + + for(auto &v:gt[nod]) + + if(viz[v] < 0) + + DFSt(v, nrc); + + cc[nrc].push_back(nod); + +} + + + + + +void write() + +{ + + g< 0) + + continue; + + nrc++; + + DFSt(vf, nrc); + + } + + write(); + + return 0; + +} diff --git a/scc/kosaraju/f4b0a8bf-6da0-43cd-ab96-50d9cc3c85b9.cpp b/scc/kosaraju/f4b0a8bf-6da0-43cd-ab96-50d9cc3c85b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..823af0a7685a7313caecaf7a5b451fe0dfc783d3 --- /dev/null +++ b/scc/kosaraju/f4b0a8bf-6da0-43cd-ab96-50d9cc3c85b9.cpp @@ -0,0 +1,79 @@ +#include +#include + +using namespace std; + +const int NMAX = 200005; + +vector graf[NMAX]; +vector graf_t[NMAX]; +vector ordered_list; +vector comp[NMAX]; + +int comp_nod[NMAX]; +int viz[NMAX]; + +int nrcomp; + +void dfs1(int nod) { + if (viz[nod]) + return ; + + viz[nod] = 1; + + for (auto &vec : graf[nod]) { + dfs1(vec); + } + ordered_list.push_back(nod); +} + +void dfs2(int nod, int index) { + if (comp_nod[nod]) + return ; + + comp_nod[nod] = index; + comp[index].push_back(nod); + + for (auto &vec : graf_t[nod]) { + dfs2(vec, index); + } + +} + +int main() { + ifstream fin("ctc.in"); + ofstream fout("ctc.out"); + + int a,b,m,n; + fin >> n >> m; + for(int i = 0; i < m; i++) { + fin >> a >> b; + graf[a].push_back(b); + graf_t[b].push_back(a); + } + + for(int i = 1; i <= n; i++) { + if (!viz[i]) { + dfs1(i); + } + } + + for(int i = n - 1; i >= 0; i--) { + if (!comp_nod[ordered_list[i]]) { + dfs2(ordered_list[i], ++nrcomp); + } + } + + fout << nrcomp << "\n"; + for (int i = 1; i <= nrcomp; i++) { + for (auto &nod : comp[i]) { + fout << nod << " "; + } + fout << "\n"; + } + + + fin.close(); + fout.close(); + return 0; +} diff --git a/scc/kosaraju/f5ff2b18-d2b0-4559-9c01-12b79249fadc.cpp b/scc/kosaraju/f5ff2b18-d2b0-4559-9c01-12b79249fadc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..876709b769b238da8206801496b439301efd7edf --- /dev/null +++ b/scc/kosaraju/f5ff2b18-d2b0-4559-9c01-12b79249fadc.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector graf[100005]; +vector ans[100005]; +vector graf1[100005]; +int viz[100005],ans1; + +stack s; +int dfs(int nod,int ok,vector x[100005]) +{ + viz[nod]=1; + if(ok<0) + ans[ans1].push_back(nod); + for(int i=0;i0) + s.push(nod); +} + + +int main() +{ + int n,m; + in>>n>>m; + for(int i=1;i<=m;i++) + { + int x,y; + in>>x>>y; + graf[x].push_back(y); + graf1[y].push_back(x); + } + for(int i=1;i<=n;i++) + if(viz[i]==0) + dfs(i,1,graf); + memset(viz,0,sizeof(viz)); + while(!s.empty()) + { + if(viz[s.top()]==0) + { + ans1++; + dfs(s.top(),-1,graf1); + } + s.pop(); + } + out< +#include +#include +using namespace std; + +const int NMAX = 100000; + +vector G[NMAX + 5] , T[NMAX + 5] , ctc[NMAX + 5]; + +int viz[NMAX + 5] , st[NMAX + 5] , top , nr; + +void dfs(int u) + +{ + + int v , j; + + viz[u] = 1; + + for(j = 0 ; j < G[u].size() ; j ++) + + { + + v = G[u][j]; + + if(viz[v] == 0) + + dfs(v); + + } + + st[++ top] = u; + +} + +void dfst(int u) + +{ + + int v , j; + + viz[u] = 1; + + ctc[nr].push_back(u); + + for(j = 0 ; j < T[u].size() ; j ++) + + { + + v = T[u][j]; + + if(viz[v] == 0) + + dfst(v); + + } + +} + +int main() + +{ + + freopen("ctc.in" , "r" , stdin); + + freopen("ctc.out" , "w" , stdout); + + int n , m , i , j , x , y; + + scanf("%d%d" , &n , &m); + + for(i = 1 ; i <= m ; i ++) + + { + + scanf("%d%d" , &x , &y); + + G[x].push_back(y); + + T[y].push_back(x); + + } + + for(i = 1 ; i <= n ; i ++) + + if(viz[i] == 0) + + dfs(i); + + memset(viz , 0 , sizeof(viz)); + + for(i = top ; i >= 1 ; i --) + + if(viz[st[i]] == 0) + + { + + nr ++; + + dfst(st[i]); + + } + + printf("%d\n" , nr); + + for(i = 1 ; i <= nr ; i ++) + + { + + for(j = 0 ; j < ctc[i].size() ; j ++) + + printf("%d " , ctc[i][j]); + + printf("\n"); + + } + + return 0; + +} diff --git a/scc/kosaraju/f9261e28-73d7-49db-9cfe-496aa6775b0c.cpp b/scc/kosaraju/f9261e28-73d7-49db-9cfe-496aa6775b0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcbab6a16781b9b2b4b7ef5afb36ed7680e07b64 --- /dev/null +++ b/scc/kosaraju/f9261e28-73d7-49db-9cfe-496aa6775b0c.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int CMAX = 100015; + +int n , m , x , y , comp; + +bool viz[CMAX] , viz1[CMAX]; + + + +vector < int > v[CMAX]; + +vector < int > trans[CMAX]; + +stack < int > st; + +vector < int > afis[CMAX]; + + + +void citire() + +{ + + fin >> n >> m; + + for(int i=1;i<=m;i++) + + { + + fin >> x >> y; + + v[x].push_back(y); + + trans[y].push_back(x); + + } + +} + + + +void dfs(int nod) + +{ + + viz[nod] = 1; + + for(int i=0;i +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +stack < int > S; + + + +vector G[100005],GT[100005],CTC[100005]; + + + +int N, M, NrCTC; + +int beenThere[100005]; + + + +void Read() + +{ + + fin >> N >> M; + + for(int i = 1; i <= M; i++) + + { + + int x,y; + + fin >> x >> y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void DFSP(int Nod) + +{ + + beenThere[Nod] = 1; + + for(unsigned int i=0; i + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector sol[100100],v[100100],vT[100100]; + +int t,nr,vTranspus[100100],viz[100100]; + + + +void DFS(int nod) + +{ + + viz[nod]=1; + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + vT[y].push_back(x); + + } + + + + for(i=1;i<=n;i++)if(viz[i]==0)DFS(i); + + for(i=n;i>=1;i--)if(viz[vTranspus[i]]==1)nr++,DFS_T(vTranspus[i]); + + + + g< +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bool viz[100001]; + +vector A[100001]; + +vector T[100001]; + +vector rez[100001]; + +stack Q; + +int n,m,nm=0; + +void dfs1(int nod) + +{ + + viz[nod]=1; + + + + vector :: iterator it; + + for(it=A[nod].begin();it!=A[nod].end();it++) + + if(!viz[*it]) + + dfs1(*it); + + Q.push(nod); + +} + +void dfs2(int nod) + +{ + + viz[nod]=1; + + rez[nm].push_back(nod); + + vector :: iterator it; + + for(it=T[nod].begin();it!=T[nod].end();it++) + + if(!viz[*it]) + + dfs2(*it); + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int a,b;; + + fin>>a>>b; + + A[a].push_back(b); + + T[b].push_back(a); + + } + + for(int i=1;i<=n;i++) + + if(viz[i]==0) + + dfs1(i); + + for(int i=1;i<=n;i++) + + viz[i]=0; + + while(!Q.empty()) + + { + + int v; + + v=Q.top(); + + Q.pop(); + + if(!viz[v]) + + { + + nm++; + + dfs2(v); + + } + + } + + fout< + + +using namespace std; + + + +const int NMAX = 100000; + + + +struct nod + +{ + + int x; + + nod *next; + +}; + + + +nod *G[NMAX + 1], + + *GT[NMAX + 1], + + *CTC[NMAX + 1]; + + + +int n,m,nrc, + + nr,post[NMAX + 1]; + + + +bool viz[NMAX + 1]; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +void add(nod *&cap_1st,int nod_ad) + +{ + + nod *p; + + p = new nod; + + p -> x = nod_ad; + + p -> next = cap_1st; + + cap_1st = p; + +} + + + +void citireGraf() + +{ + + int x,y; + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + add(G[x],y); + + add(GT[y],x); + + } + +} + + + +void DFS(int vf) + +{ + + viz[vf] = 1; + + for(nod *p = G[vf]; p != NULL; p = p -> next) + + if(viz[p->x] == 0) + + DFS(p->x); + + post[++nr] = vf; + +} + + + +void DFSt(int vf) + +{ + + viz[vf] = 0; + + add(CTC[nrc], vf); + + for(nod *p = GT[vf]; p!=NULL;p = p ->next) + + if(viz[p->x] == 1) + + DFSt(p->x); + +} + + + +void componente() + +{ + + for(int i=1;i<=n;i++) + + if(viz[i] == 0) + + DFS(i); + + + + for(int i=n;i>=1;i--) + + if(viz[post[i]] == 1) + + { + + nrc++; + + DFSt(post[i]); + + } + +} + + + +void afis() + +{ + + g< next) + + g<< p->x <<' '; + + g<<'\n'; + + } + +} + +int main() + +{ + + citireGraf(); + + componente(); + + afis(); + + return 0; + +} diff --git a/scc/kosaraju/fa36f9cd-c521-4390-91b1-77f841ec9797.cpp b/scc/kosaraju/fa36f9cd-c521-4390-91b1-77f841ec9797.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d0e4120afa9a0a48134029e089755b88f359cd2 --- /dev/null +++ b/scc/kosaraju/fa36f9cd-c521-4390-91b1-77f841ec9797.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +using namespace std; +vector> Graph, GraphTranspose, components; +stack s; +bitset<100000> used; + +void DFS(int k) +{ + used[k] = true; + for (auto v : Graph[k]) + if (!used[v]) + DFS(v); + s.push(k); +} + +void DFST(int k) +{ + used[k] = true; + components[(int)components.size() - 1].emplace_back(k); + for (auto v : GraphTranspose[k]) + if (!used[v]) + DFST(v); +} + +int main() +{ + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + + int N, M; + scanf("%d%d", &N, &M); + + Graph.resize(N); + GraphTranspose.resize(N); + + for (int i = 0; i < M; ++i) { + int a, b; + scanf("%d%d", &a, &b); + --a; --b; + Graph[a].emplace_back(b); + GraphTranspose[b].emplace_back(a); + } + + for (int i = 0; i < N; ++i) + if (!used[i]) + DFS(i); + + used = 0; + while(!s.empty()) { + int k = s.top(); s.pop(); + if (used[k]) + continue; + components.emplace_back(vector()); + DFST(k); + } + + printf("%d\n", (int)components.size()); + for (auto c : components) { + for (auto n : c) + printf("%d ", n + 1); + printf("\n"); + } + + return 0; +} diff --git a/scc/kosaraju/faca77fa-7022-4e67-bc48-907cce629206.cpp b/scc/kosaraju/faca77fa-7022-4e67-bc48-907cce629206.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3691115dcc40e4694091deea0382a4e7051c1f81 --- /dev/null +++ b/scc/kosaraju/faca77fa-7022-4e67-bc48-907cce629206.cpp @@ -0,0 +1,161 @@ +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,nrc; + + + +vector > G,GT,CTC; + +vector viz; + +vector Post; + + + +void citire() + +{ + + int x,y; + + f>>n>>m; + + + + G = vector >(n+1); + + GT = vector >(n+1); + + CTC = vector >(n+1); + + viz = vector(n+1,0); + + Post = vector(n+1); + + + + for(int i=1;i<=m;i++) + + { + + f>>x>>y; + + G[x].push_back(y); + + GT[y].push_back(x); + + } + +} + + + +void dfs(int vf) + +{ + + viz[vf] = 1; + + for(auto &x : G[vf]) + + if(viz[x] == 0) + + dfs(x); + + Post.push_back(vf); + +} + + + +void dfsGT(int vf) + +{ + + viz[vf] = 0; + + CTC[nrc].push_back(vf); + + for(auto &x : GT[vf]) + + if(viz[x] == 1) + + dfsGT(x); + +} + + + +void componente() + +{ + + for(int i=1;i<=n;i++) + + if(viz[i] == 0) + + dfs(i); + + + + for(auto it = Post.rbegin(); it != Post.rend(); ++it) + + if(viz[*it] == 1) + + { nrc++; + + dfsGT(*it); + + } + +} + + + +void afis() + +{ + + g< +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vectora[100002],inv[100002],ctc[100002]; + +int n,k,kk[100002]; + +bool viz[100002]; + + + +void dfs(int x) + +{ + + viz[x]=1; + + for(auto y:a[x]) + + if(viz[y]==0) + + dfs(y); + + kk[++k]=x; + +} + + + +void dfs_inv(int x) + +{ + + viz[x]=1; + + ctc[k].push_back(x); + + for(auto y:inv[x]) + + if(viz[y]==0) + + dfs_inv(y); + +} + + + +void kosaraju() + +{ + + int i; + + for(i=1;i<=n;i++) + + if(viz[i]==0) + + dfs(i); + + for(i=1;i<=n;i++) + + viz[i]=0; + + k=0; + + for(i=n;i>=1;i--) + + if(viz[kk[i]]==0) + + { + + k++; + + dfs_inv(kk[i]); + + } + +} + + + +int main() + +{ + + int m,i,x,y; + + f>>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + a[x].push_back(y); + + inv[y].push_back(x); + + } + + kosaraju(); + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int NMax = 100000; + + + +vector g[NMax + 5], gt[NMax + 5], sol[NMax + 5]; + +int n, m, nr, code; + +int top[NMax + 5]; + +bool use[NMax + 5]; + + + +void Read(){ + + fin >> n >> m; + + for (int i = 1; i <= m; i++){ + + int x, y; + + fin >> x >> y; + + g[x].push_back(y); + + gt[y].push_back(x); + + } + +} + + + +void TopSort(int node){ + + use[node] = 1; + + for (auto other : g[node]) + + if (!use[other]) + + TopSort(other); + + top[++nr] = node; + +} + + + +void DFS(int node, int code){ + + use[node] = 1; + + sol[code].push_back(node); + + for (auto other : gt[node]) + + if (!use[other]) + + DFS(other, code); + +} + + + +void Solve(){ + + for (int i = 1; i <= n; i++) + + if (!use[i]) + + TopSort(i); + + memset(use, 0, sizeof use); + + for (int i = nr; i >= 1; i--) + + if (!use[top[i]]){ + + ++code; + + DFS(top[i], code); + + } + +} + + + +void Print(){ + + fout << code << '\n'; + + for (int i = 1; i <= code; i++){ + + for (auto node : sol[i]) + + fout << node << ' '; + + fout << '\n'; + + } + +} + + + +int main(){ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/scc/kosaraju/fbdfc00e-1781-43e7-ad24-22c5fa2ead61.cpp b/scc/kosaraju/fbdfc00e-1781-43e7-ad24-22c5fa2ead61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3daec89efbde1ed0f26825033fd8d83ccabf8d78 --- /dev/null +++ b/scc/kosaraju/fbdfc00e-1781-43e7-ad24-22c5fa2ead61.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int vf, muchii; + + + +int Vizitat[100005]; + +vector < int > Numbers[100005]; + +vector < int > Numbers_SensInversMuchii[100005]; + +vector < int > SalvamCTC[100005]; + + + +stack < int > Stiva; + + + +int compconex, ctc; + + + +inline void DFS( int Nod ) + +{ + + int Vecin; + + + + Vizitat[Nod] = 1; + + for( unsigned int i = 0; i < Numbers[Nod].size(); ++ i ) + + { + + Vecin = Numbers[Nod][i]; + + if( !Vizitat[Vecin] ) + + DFS(Vecin); + + } + + Stiva.push(Nod); + + + +} + + + +inline void DFS_2( int Nod ) + +{ + + int Vecin; + + + + SalvamCTC[ctc].push_back(Nod); + + Vizitat[Nod] = 2; + + + + for( unsigned int i = 0; i < Numbers_SensInversMuchii[Nod].size(); ++ i ) + + { + + Vecin = Numbers_SensInversMuchii[Nod][i]; + + if( Vizitat[Vecin] == 1 ) + + DFS_2(Vecin); + + } + + + +} + + + +int main() + +{ + + fin >> vf >> muchii; + + for( int i = 1; i <= muchii; ++ i ) + + { + + int x, y; + + fin >> x >> y; + + Numbers[x].push_back(y); + + Numbers_SensInversMuchii[y].push_back(x); + + } + + + + for( int i = 1; i <= vf; ++ i ) + + if( !Vizitat[i] ) + + DFS(i); + + + + while( !Stiva.empty() ) + + { + + int Numar = Stiva.top(); + + if( Vizitat[Numar] == 1 ) + + { + + ctc++; + + DFS_2(Numar); + + } + + Stiva.pop(); + + } + + fout << ctc << "\n"; + + for( int i = 1; i <= ctc; ++ i ) + + { + + for( unsigned int j = 0; j < SalvamCTC[i].size(); ++ j ) + + { + + fout << SalvamCTC[i][j] << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/kosaraju/fdabd3b1-0a5c-48ab-a1d6-972fba70d27b.cpp b/scc/kosaraju/fdabd3b1-0a5c-48ab-a1d6-972fba70d27b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..44931efccd369e1c8bcf15d1eecdf36dd55f1411 --- /dev/null +++ b/scc/kosaraju/fdabd3b1-0a5c-48ab-a1d6-972fba70d27b.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 1e5 + 5; + + + +vector L[N], T[N], scc[N]; + +int n, nr_scc; + + + +void Read () + +{ + + ifstream fin ("ctc.in"); + + int m; + + fin >> n >> m; + + while (m--) + + { + + int x, y; + + fin >> x >> y; + + L[x].push_back(y); + + T[y].push_back(x); + + } + + fin.close(); + +} + + + +stack st; + +bitset seen; + + + +void DFS1(int vertex) + +{ + + seen[vertex] = 1; + + for (auto next : L[vertex]) + + if (!seen[next]) + + DFS1(next); + + st.push(vertex); + +} + + + +void DFS2(int vertex) + +{ + + seen[vertex] = 0; + + for (auto next : T[vertex]) + + if (seen[next]) + + DFS2(next); + + scc[nr_scc].push_back(vertex); + +} + + + +void Kosaraju () + +{ + + for (int i = 1; i <= n; i++) + + if (!seen[i]) + + DFS1(i); + + while (!st.empty()) + + { + + if (seen[st.top()]) + + { + + nr_scc++; + + DFS2(st.top()); + + } + + st.pop(); + + } + + ofstream fout ("ctc.out"); + + fout << nr_scc << "\n"; + + for (int i = 1; i <= nr_scc; i++) + + { + + for (auto j : scc[i]) + + fout << j << " "; + + fout << "\n"; + + } + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Kosaraju(); + + return 0; + +} diff --git a/scc/kosaraju/ff44dafd-deb4-443b-bfdf-1dcca2b55604.cpp b/scc/kosaraju/ff44dafd-deb4-443b-bfdf-1dcca2b55604.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d464aab77da28515bc5c1c2cfec1c761e5d8f479 --- /dev/null +++ b/scc/kosaraju/ff44dafd-deb4-443b-bfdf-1dcca2b55604.cpp @@ -0,0 +1,146 @@ +#include +using namespace std; + +class Task { +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + + int n, m; + + + vector adj[100010]; + + + vector adj_trans[100010]; + + + vector> ctc; + + + + + + + + vector visited; + + + + vector topsort; + + void read_input() { + cin >> n >> m; + visited = vector(n + 1, 0); + + for (int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + + adj[x].push_back(y); + adj_trans[y].push_back(x); + } + } + + void get_result() { + kosaraju(); + } + + void kosaraju() { + topsort.push_back(-1); + + + for (int i = 1; i <= n; ++i) { + if (!visited[i]) { + dfs(i); + } + } + + + for (int i = n; i >= 1; --i) { + if (visited[topsort[i]]) { + + vector current_ctc; + + dfs_t(topsort[i], current_ctc); + + + ctc.push_back(current_ctc); + } + } + } + + + void dfs(int node) { + visited[node] = 1; + + for (auto &vecin : adj[node]) { + if (!visited[vecin]) { + dfs(vecin); + } + } + + + topsort.push_back(node); + } + + + void dfs_t(int node, vector ¤t_ctc) { + + visited[node] = 0; + current_ctc.push_back(node); + + for (auto &vecin : adj_trans[node]) { + if (visited[vecin]) { + dfs_t(vecin, current_ctc); + } + } + } + + void print_output() { + + int sol = ctc.size(); + cout << sol << "\n"; + + for (int i = 0; i < sol; ++i) { + + for (auto &node : ctc[i]) { + cout << node << " "; + } + cout << '\n'; + } + } +}; + +int main() { + + auto cin_buff = cin.rdbuf(); + auto cout_buff = cout.rdbuf(); + + + ifstream fin("ctc.in"); + cin.rdbuf(fin.rdbuf()); + + + ofstream fout("ctc.out"); + cout.rdbuf(fout.rdbuf()); + + + Task *task = new Task(); + task->solve(); + delete task; + + + cin.rdbuf(cin_buff); + cout.rdbuf(cout_buff); + + + + + return 0; +} diff --git a/scc/tarjan/014f2623-89cd-49d1-bcce-597f76661b39.cpp b/scc/tarjan/014f2623-89cd-49d1-bcce-597f76661b39.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d10b4074f87d1d59af2a4989e5ebd4078dca15a3 --- /dev/null +++ b/scc/tarjan/014f2623-89cd-49d1-bcce-597f76661b39.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +using namespace std; + +int n,id=0,ids[100010],low[100010],ctc=0; + +ofstream o("ctc.out"); + +ifstream f("ctc.in"); + +vector v[100010],cc[100010]; + +stack S; + +bool onStack[100010],viz[100010]; + +void dfs(int k){ + + ids[k]=low[k]=++id; int i,nv; + + S.push(k); onStack[k]=1; + + viz[k]=true; + + for(nv=0;nv>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++) + + if(!viz[i]) dfs(i); + + + + o< +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n,m,i,x,y,nr,viz[100005],low[100005],niv; + +bool s[100005]; + +vector g[100005],c[100005]; + +stack st; + + + +void dfs(int nod){ + + viz[nod]=low[nod]=++niv; + + s[nod]=1; + + st.push(nod); + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + g[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!viz[i]) + + dfs(i); + + fout< +#include +#include + + +using namespace std; + +vector a[100010]; + +vector sol[100010]; + +stack s; + +int f[100010]; + +int niv[100010]; + +int low[100010]; + +int is[100010]; + +int i,n,m,u,g,x,y; + + + +void dfs (int nod) { + + g++; + + niv[nod]=g; + + low[nod]=g; + + f[nod]=1; + + is[nod]=1; + + s.push(nod); + + is[nod]=1; + + for (auto vecin:a[nod]) { + + if (f[vecin]==0) { + + dfs(vecin); + + low[nod]=min(low[nod],low[vecin]); + + } + + else if (is[vecin]){ + + low[nod]=min(low[nod],low[vecin]); + + } + + } + + + + if (low[nod]==niv[nod]) { + + u++; + + while (s.top()!=nod) { + + sol[u].push_back(s.top()); + + is[s.top()]=0; + + s.pop(); + + } + + sol[u].push_back(s.top()); + + is[s.top()]=0; + + s.pop(); + + } + +} + + + +int main() { + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y; + + a[x].push_back(y); + + } + + for (i=1;i<=n;i++) { + + if (f[i]==0) { + + dfs(i); + + } + + } + + fout< +#include +#include +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,a1,b1,ap[100005],ind,indice[100005],t; + +bool b[100005]; + +vector v[100005]; + +vector sol1[100005]; + +stack s; + +void sol(int nod) + +{ + + indice[nod]=++ind; + + ap[nod]=ind; + + s.push(nod); + + b[nod]=1; + + for(auto i:v[nod]) + + { + + if(indice[i]==0) + + { + + sol(i); + + ap[nod]=min(ap[nod],ap[i]); + + } + + else if(b[i]) + + { + + ap[nod]=min(ap[nod],ap[i]); + + } + + } + + if(ap[nod]==indice[nod]) + + { + + t++; + + while(s.top()!=nod) + + { + + sol1[t].push_back(s.top()); + + b[s.top()]=0; + + s.pop(); + + } + + b[s.top()]=0; + + sol1[t].push_back(s.top()); + + s.pop(); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>a1>>b1; + + v[a1].push_back(b1); + + } + + for(int i=1;i<=n;i++) + + { + + if(indice[i]==0) + + { + + sol(i); + + } + + } + + out< +#include +#include +#include +using namespace std; + +int n,id=0,ids[100010],low[100010],ctc=0; + +ofstream o("ctc.out"); + +ifstream f("ctc.in"); + +vector v[100010],cc[100010]; + +stack S; + +bool onStack[100010]; + +void dfs(int k){ + + ids[k]=low[k]=++id; int i,nv; + + S.push(k); onStack[k]=1; + + for(nv=0;nv>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++) + + if(!ids[i]) dfs(i); + + + + o< +#include +#include +#include +#include + +using namespace std; + +const int maxi = 200001; +vector a[maxi], compo; +vector> componente; +stack stiva; +int disc[maxi], low[maxi], vizitat[maxi]; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int index = 0; + +void dfs(int u) { + disc[u] = low[u] = ++index; + stiva.push(u); + vizitat[u] = 1; + for (int v : a[u]) { + if (disc[v] == -1) { + dfs(v); + if (low[u] > low[v]) + low[u] = low[v]; + } else if (vizitat[v] && disc[v] < low[u]) + low[u] = disc[v]; + } + int w = 0; + if (low[u] == disc[u]) { + compo.clear(); + while (stiva.top() != u) + { + w = stiva.top(); + + compo.push_back(w); + vizitat[w] = 0; + stiva.pop(); + } + w = stiva.top(); + + compo.push_back(w); + vizitat[w] = 0; + stiva.pop(); + componente.push_back(compo); + } +} + +void ctc(int n) { + int i; + for (i = 1; i <= n; ++i) { + disc[i] = low[i] = -1; + } + for (i = 1; i <= n; ++i) { + if (disc[i] == -1) { + dfs(i); + } + } + fout << componente.size() << "\n"; + for(auto lst : componente) { + for(auto nod : lst) + fout << nod << " "; + fout << "\n"; + } + +} + +int main() { + int n, m, x, y; + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + fin >> x >> y; + a[x].push_back(y); + } + ctc(n); + return 0; +} diff --git a/scc/tarjan/070ec3b5-da98-4d49-ab10-6f73a5426718.cpp b/scc/tarjan/070ec3b5-da98-4d49-ab10-6f73a5426718.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caf123c026d12c2526904af21faea9c755f2673f --- /dev/null +++ b/scc/tarjan/070ec3b5-da98-4d49-ab10-6f73a5426718.cpp @@ -0,0 +1,66 @@ +#include + +std::ifstream fin("ctc.in"); +std::ofstream fout("ctc.out"); + +int n, m, nodeIndex; +std::vector nei[100001], id, lowLink; +std::vector inStack; +std::stack stack; +std::vector> comp; + +void dfs(int x){ + id[x] = lowLink[x] = nodeIndex++; + stack.push(x); + inStack[x] = true; + for(auto node: nei[x]){ + if(id[node] == -1) + dfs(node); + if(inStack[node]) + lowLink[x] = std::min(lowLink[node], lowLink[x]); + } + + if(id[x] == lowLink[x]){ + std::vector aux; + while(stack.top() != x){ + inStack[stack.top()] = false; + aux.push_back(stack.top()); + stack.pop(); + } + inStack[stack.top()] = false; + aux.push_back(stack.top()); + stack.pop(); + comp.push_back(aux); + } +} + + +int main() +{ + int x, y, i; + fin >> n >> m; + id.resize(n + 1, -1); + lowLink.resize(n + 1); + inStack.resize(n + 1, false); + + for(i = 0; i < m; i++){ + fin >> x >> y; + nei[x].push_back(y); + } + + for(i = 1; i <= n; i++) + if(id[i] == -1) + dfs(i); + + fout << comp.size() << '\n'; + for(auto i: comp){ + for(auto j: i) + fout << j << " "; + fout << '\n'; + } + + + + + return 0; +} diff --git a/scc/tarjan/0749f728-3c26-4c0a-b1b8-b2b7c567d23f.cpp b/scc/tarjan/0749f728-3c26-4c0a-b1b8-b2b7c567d23f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a03873b89a2fe9c8deb1f634b503fd79fb839a2 --- /dev/null +++ b/scc/tarjan/0749f728-3c26-4c0a-b1b8-b2b7c567d23f.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int NMAX = 100001; + +int N, M, cnt, level[NMAX], low[NMAX], cmp; +vector < int > G[NMAX], components[NMAX]; +bitset < NMAX > vis, InStack; +stack < int > Stack; + +void dfs(const int& node) { + + cnt++; + level[node] = cnt; + low[node] = cnt; + vis[node] = 1; + Stack.push(node); + InStack[node] = 1; + + for(const int& neighbor : G[node]) { + if(level[neighbor] == 0) { + dfs(neighbor); + low[node] = min(low[node], low[neighbor]); + }else if(InStack[neighbor]) { + low[node] = min(low[node], low[neighbor]); + } + } + + if(low[node] == level[node]) { + + cmp++; + + int x{}; + do { + x = Stack.top(); + Stack.pop(); + InStack[x] = 0; + components[cmp].push_back(x); + }while(x != node); + } +} + +int main() { + + f >> N >> M; + + while(M--) { + int u, v; + f >> u >> v; + + G[u].push_back(v); + } + + for(int i = 1;i <= N;++i) + if(!vis[i]) + dfs(i); + + g << cmp << '\n'; + for(int i = 1;i <= cmp;++i, g << '\n') + for(const int& it : components[i]) + g << it << ' '; + + return 0; +} diff --git a/scc/tarjan/0789d9b1-0e82-4a5f-bbb8-21202bfc8dbd.cpp b/scc/tarjan/0789d9b1-0e82-4a5f-bbb8-21202bfc8dbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0479c6bc5a1383ea69dd271a7ffaa2cadde9ff7a --- /dev/null +++ b/scc/tarjan/0789d9b1-0e82-4a5f-bbb8-21202bfc8dbd.cpp @@ -0,0 +1,148 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + + + +const int MAXN = 100000, MAXM = 200000; + +int N, M, sccCount = 0, cnt = 0; + +stack st; + +bool onSt[MAXN + 1]; + +int vis[MAXN + 1], low[MAXN + 1]; + +vector adj[MAXN + 1], con; + +vector > C; + + + + + + + +void dfs(int curr) + +{ + + st.push(curr); + + onSt[curr] = true; + + ++cnt; + + vis[curr] = cnt; + + low[curr] = cnt; + + + + for (int nbh : adj[curr]) + + { + + if (vis[nbh] == 0) + + dfs(nbh); + + if (onSt[nbh]) + + low[curr] = min(low[nbh], low[curr]); + + } + + + + if (vis[curr] == low[curr]) + + { + + ++sccCount; + + con.clear(); + + while(st.top() != curr) + + { + + onSt[st.top()] = false; + + low[st.top()] = vis[curr]; + + con.push_back(st.top()); + + st.pop(); + + } + + onSt[curr] = false; + + low[curr] = vis[curr]; + + con.push_back(curr); + + st.pop(); + + C.push_back(con); + + } + +} + + + +int main() + +{ + + in >> N >> M; + + int m1, m2; + + for (int i = 0; i < M; ++i) + + { + + in >> m1 >> m2; + + adj[m1].push_back(m2); + + } + + + + for (int i = 1; i <= N; ++i) + + if (vis[i] == 0) + + dfs(i); + + out << sccCount << "\n"; + + for (int i = 0; i < C.size(); ++i) + + { + + for (int j = 0; j < C[i].size(); ++j) + + out << C[i][j]<< " "; + + out << "\n"; + + } + + + + return 0; + +} diff --git a/scc/tarjan/07bee4cc-1284-4ce2-a06f-5859ced92434.cpp b/scc/tarjan/07bee4cc-1284-4ce2-a06f-5859ced92434.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff423e2a5412c21277c4ba71a0e90d050186670f --- /dev/null +++ b/scc/tarjan/07bee4cc-1284-4ce2-a06f-5859ced92434.cpp @@ -0,0 +1,77 @@ +# include +# include + + +using namespace std; + + + + + + +int N, M, i, vf, ct, Time; +int St[100005]; +int Lvl[100005]; +int Low[100005]; +vector G[100005]; +vector CTC[100005]; + + void Dfs(int nod) + { + vector :: iterator it; + + St[++vf] = nod; + Lvl[nod] = Low[nod] = ++Time; + for (it = G[nod].begin(); it != G[nod].end(); ++it) + { + if (!Lvl[*it]) + { + Dfs(*it); + Low[nod] = (Low[nod] < Low[*it] ? Low[nod] : Low[*it]); + } else + if (Lvl[*it] > 0) + Low[nod] = (Low[nod] < Lvl[*it] ? Low[nod] : Lvl[*it]); + } + + if (Low[nod] == Lvl[nod]) + { + ++ct; + for (i = vf; St[i + 1] != nod; --i) + { + CTC[ct].push_back(St[i]); + Lvl[St[i]] = -1; + } + vf = i; + } + } + + int main() + { + freopen("ctc.in","r",stdin); + freopen("ctc.out","w",stdout); + + scanf("%d %d",&N, &M); + for (i = 1; i <= M; ++i) + { + int x, y; + scanf("%d %d",&x, &y); + + G[x].push_back(y); + } + + vf = ct = Time = 0; + for (i = 1; i <= N; ++i) + if (!Lvl[i]) Dfs(i); + + printf("%d\n",ct); + + vector :: iterator it; + for (i = 1; i <= ct; ++i) + { + for (it = CTC[i].begin(); it != CTC[i].end(); ++it) + printf("%d ",*it); + printf("\n"); + } + + return 0; + } diff --git a/scc/tarjan/0ebebccf-d21a-4aa0-909b-f9d5e0050819.cpp b/scc/tarjan/0ebebccf-d21a-4aa0-909b-f9d5e0050819.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa320f6ec8e62236e7cce6bb4a54100631646f14 --- /dev/null +++ b/scc/tarjan/0ebebccf-d21a-4aa0-909b-f9d5e0050819.cpp @@ -0,0 +1,92 @@ +#include + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int Inf = INT_MAX; + +vector < vector > g, comps; +vector lowlink, pre; +int V, E; + +int traversalOrd; + +stack Stack; + +void readGraph(){ + + fin >> V >> E; + + g.resize(V + 1); + lowlink.resize(V + 1); + pre.resize(V + 1); + comps.reserve(V); + + for(int e = 0; e < E; ++e){ + + int from, to; + fin >> from >> to; + + g[from].push_back(to); + } +} + + +void cacheSCC(int node){ + + vector SCC; + SCC.reserve(V); + + int stackNode; + do{ + + stackNode = Stack.top(); + Stack.pop(); + + lowlink[stackNode] = Inf; + SCC.push_back(stackNode); + }while(stackNode != node); + + comps.push_back(SCC); +} + + +void DFS(int node){ + + lowlink[node] = pre[node] = ++traversalOrd; + Stack.push(node); + + for(const int &adj : g[node]) + if(lowlink[adj] == 0){ + + DFS(adj); + lowlink[node] = min(lowlink[node], lowlink[adj]); + } + else lowlink[node] = min(lowlink[node], lowlink[adj]); + + if(lowlink[node] == pre[node]) + cacheSCC(node); +} + + +int main(){ + + readGraph(); + + for(int node = 1; node <= V; ++node) + if(lowlink[node] == 0) + DFS(node); + + + + + fout << comps.size() << '\n'; + for(int c = 0; c < (int)comps.size(); ++c){ + + for(const int &node : comps[c]) + fout << node << ' '; + fout << '\n'; + } +} diff --git a/scc/tarjan/0faf8bf9-21d6-4261-968e-5c686cf12863.cpp b/scc/tarjan/0faf8bf9-21d6-4261-968e-5c686cf12863.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c428bf29138ed23dbcb5d975536ab4402034d50 --- /dev/null +++ b/scc/tarjan/0faf8bf9-21d6-4261-968e-5c686cf12863.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nMax = 100010; + + + +int nrctc, t; + +vector v[nMax], ct[nMax], disc, low, isInStack; + +stack s; + + + +void ctc(int nod) + +{ + + t++; + + disc[nod] = low[nod] = t; + + s.push(nod); + + isInStack[nod] = true; + + + + for(auto it : v[nod]) + + { + + if(!disc[it]) + + { + + ctc(it); + + low[nod] = min(low[nod], low[it]); + + } + + else if(isInStack[it])low[nod] = min(low[nod], disc[it]); + + } + + + + if(low[nod] == disc[nod]) + + { + + while(s.top() != nod) + + { + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + } + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + nrctc++; + + } + + + +} + + + +int main() + +{ + + int n, m; fin >> n >> m; + + disc.assign(n, 0); + + low.assign(n, 0); + + isInStack.assign(n, 0); + + for(int i=0; i> x >> y; x--; y--; + + v[x].push_back(y); + + } + + for(int i=0; i + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< + + +using namespace std; + +const int NMAX=100005; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +vector g[NMAX],c; + +stacks; + +vector>ans; + +int n,m,f,low[NMAX],in_stack[NMAX],val[NMAX]; + +int idx=1; + +void tarjan(int node){ + + val[node]=low[node]=idx; + + s.push(node), in_stack[node]=1; + + idx++; + + for(auto y:g[node]){ + + if(!val[y]) + + tarjan(y),low[node]=min(low[node],low[y]); + + else if(in_stack[y]) + + low[node]=min(low[node],low[y]); + + } + + if(val[node]==low[node]){ + + c.clear(); + + int n; + + do{ + + c.push_back(n=s.top()); + + in_stack[n]=0; + + s.pop(); + + }while(n!=node); + + ans.push_back(c); + + } + +} + +int main() + +{ + + in>>n; + + int x,y; + + for(in>>m;m>0;m--){ + + in>>x>>y; + + g[x].push_back(y); + + } + + for(int i=1;i<=n;i++){ + + if(!val[i]) + + tarjan(i); + + } + + out< + + +int c, N, M; + +std::vector > graph; + +inline void addEdge(int u, int v) { + + graph[u].push_back(v); + +} + + + +class SCCCalculator { + +public: + + SCCCalculator (std::vector > &graph) { calculate(graph); } + + void calculate(std::vector > &graph) { + + _time = 0; + + N = graph.size(); + + low .resize(graph.size(), 0); + + disc .resize(graph.size(), 0); + + inStack.resize(graph.size(), false); + + for (int i=0; i> getComponents() { return components; } + + + + std::vector > getSCCGraph(std::vector >& graph) { + + std::vector > SCCGraph; + + SCCGraph.resize(components.size()); + + std::vector seen(components.size(), false); + + for (int i=0; i> &graph, int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + for (auto &it:graph[vertex]) { + + if (!disc[it]) { DFS(graph, it); + + low[vertex] = std::min(low[vertex], low[it]); + + } else + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + if (low[vertex] == disc[vertex]) addComponent(vertex); + + } + + + + void addComponent(int vertex) { + + components.push_back(std::vector ()); + + while (!stack.empty() && stack.top() != vertex) { + + components.back().push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } components.back().push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + + + int _time; + + int N; + + std::vector low, disc, comp; + + std::vector > components; + + std::vector inStack; + + std::stack stack; + +}; + + + +std::ifstream input ("ctc.in"); + +std::ofstream output("ctc.out"); + + + +int main() + +{ + + input >> N >> M; + + graph.resize(N, std::vector ()); + + for (int i=0, u, v; i> u >> v, addEdge(u-1, v-1); + + + + SCCCalculator calc(graph); + + output << calc.getSize() << '\n'; + + for (auto &it:calc.getComponents()) { + + for (auto &it2:it) output << it2+1 << ' '; + + output << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/11e08767-a96a-431d-a843-93300eaa90c9.cpp b/scc/tarjan/11e08767-a96a-431d-a843-93300eaa90c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f22c9a40d024373cca5673226166736e9aba755c --- /dev/null +++ b/scc/tarjan/11e08767-a96a-431d-a843-93300eaa90c9.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m,nrOrd; + +vector> ad; + +vector ord, low; + + + +stack st; + +vector onStack; + +vector> sccs; + + + +void dfs(int node) { + + st.push(node); + + onStack[node] = true; + + ord[node] = low[node] = ++nrOrd; + + for (int vecin : ad[node]) + + if (!ord[vecin]) { + + dfs(vecin); + + low[node] = min(low[node], low[vecin]); + + } + + else if (onStack[vecin]) + + low[node] = min(low[node], low[vecin]); + + + + if (ord[node] == low[node]) { + + sccs.emplace_back(); + + while (true) { + + int top = st.top(); + + st.pop(); + + onStack[top] = false; + + + + low[top] = ord[node]; + + sccs.back().push_back(top); + + if (top == node) + + break; + + } + + } + +} + + + +int main() { + + fin >> n >> m; + + ad.resize(n + 1); + + for (int i = 0; i < m; i++) { + + int x, y; + + fin >> x >> y; + + ad[x].push_back(y); + + } + + + + ord.resize(n + 1); + + low.resize(n + 1); + + onStack.resize(n + 1); + + + + for (int i = 1; i <= n; i++) + + if (!ord[i]) + + dfs(i); + + + + fout << sccs.size() << '\n'; + + for (auto& scc : sccs) { + + for (int node : scc) + + fout << node << ' '; + + fout << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/12f236c6-105f-45ab-93f2-4e4cb73fdcc0.cpp b/scc/tarjan/12f236c6-105f-45ab-93f2-4e4cb73fdcc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa33120e456b78d1057125cb4695c71a70c6621 --- /dev/null +++ b/scc/tarjan/12f236c6-105f-45ab-93f2-4e4cb73fdcc0.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n, iname); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C, oname); + + + + return 0; + +} diff --git a/scc/tarjan/133dbfaa-9fb2-4934-9f6c-b5d646144238.cpp b/scc/tarjan/133dbfaa-9fb2-4934-9f6c-b5d646144238.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec9b69e82b957804b805b1ccdfc6da242813aa27 --- /dev/null +++ b/scc/tarjan/133dbfaa-9fb2-4934-9f6c-b5d646144238.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +const int N=100005; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,x,y; + +vector vecini[N]; + +int id[N],low[N],a[N],k,inq[N],s; + +int nrctc,nodes[N],ctcsize[N]; + + + +void dfs(int x) + +{ + + ++k; ++s; + + low[x]=id[x]=k; a[s]=x; inq[x]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) f>>x>>y, vecini[x].push_back(y); + + for(int i=1;i<=n;++i) if(!id[i]) + + { + + k=0; s=0; + + dfs(i); + + } + + g< +#include +#include + + +using namespace std; + +vector a[100010]; + +vector sol[100010]; + +stack s; + +int f[100010]; + +int niv[100010]; + +int low[100010]; + +int is[100010]; + +int i,n,m,u,g,x,y; + + + +void dfs (int nod) { + + g++; + + niv[nod]=g; + + low[nod]=g; + + f[nod]=1; + + is[nod]=1; + + s.push(nod); + + is[nod]=1; + + for (auto vecin:a[nod]) { + + if (f[vecin]==0) { + + dfs(vecin); + + low[nod]=min(low[nod],low[vecin]); + + } + + else if (is[vecin]){ + + low[nod]=min(low[nod],low[vecin]); + + } + + } + + + + if (low[nod]==niv[nod]) { + + u++; + + while (s.top()!=nod) { + + sol[u].push_back(s.top()); + + is[s.top()]=0; + + s.pop(); + + } + + sol[u].push_back(s.top()); + + is[s.top()]=0; + + s.pop(); + + } + +} + + + +int main() { + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y; + + a[x].push_back(y); + + } + + for (i=1;i<=n;i++) { + + if (f[i]==0) { + + dfs(i); + + } + + } + + fout< +#include +#include +#include +#include + +using namespace std; + +struct node{ + int index; + int lowlink; + bool onStack; + node(int i, int l){ + this->index = i; + this->lowlink = l; + this->onStack = false; + } +}; + +vector neighbours[100000]; +ifstream f("ctc.in"); +ofstream g("ctc.out"); +vector nods; +int n, m; +int index; +stack S; +vector> output; + +void con(int z){ + nods[z].index = index; + nods[z].lowlink = index; + index++; + S.push(z); + nods[z].onStack = true; + for (auto it : neighbours[z]) { + if(nods[it].index == INT_MIN){ + con(it); + nods[z].lowlink = min(nods[z].lowlink, nods[it].lowlink); + }else if(nods[it].onStack){ + nods[z].lowlink = min(nods[z].lowlink, nods[it].index); + } + } + if(nods[z].lowlink == nods[z].index){ + vector comp; + int w; + do{ + w = S.top(); + S.pop(); + nods[w].onStack = false; + comp.push_back(w + 1); + } while (w != z); + output.push_back(comp); + } +} + +int main() { + f>>n>>m; + for (int i = 0; i < n; ++i) { + nods.emplace_back(INT_MIN, INT_MAX); + } + for (int i = 0; i < m; ++i) { + int x, y; + f>>x>>y; + x--, y--; + neighbours[x].push_back(y); + } + for(int i = 0; i < n; ++i){ + if(nods[i].index == INT_MIN){ + con(i); + } + } + g< +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vector adj[100005]; + +int n,m,low_link[100005],idx[100005],cnt=0,ctc=0; + +bool viz[100005],instack[100005]; + +vector sol[100005]; + +stack s; + +void dfs(int node) + +{ + + s.push(node); + + instack[node]=true; + + idx[node]=low_link[node]=++cnt; + + for(auto x:adj[node]) + + { + + if(idx[x]==0) {dfs(x);} + + if(instack[x]==1) + + { + + low_link[node]=min(low_link[node],low_link[x]); + + } + + } + + if(idx[node]==low_link[node]) + + { + + ctc++; + + int x=node; + + do + + { + + x=s.top(); + + low_link[x]=idx[node]; + + sol[ctc].push_back(x); + + instack[x]=0; + + s.pop(); + + } + + while(x!=node); + + } + + + + + + + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y; + + f>>x>>y; + + adj[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + { + + if(idx[i]==0) + + { + + dfs(i); + + } + + } + + g< +#include +#include +#include +#include +#include +#include +using namespace std; + +const string fileName="ctc"; + +ifstream in (fileName+".in"); + +ofstream out(fileName+".out"); + +const int nmax=1e5; + +int n, m; + +vector muchii[nmax+2]; + +int low[nmax+2]; + +int x, y; + +bool onStack[nmax+2]; + +int tin[nmax+2]; + +vector > comp; + +vector stk; + +int mom; + +int partComp[nmax+2]; + +void dfs(int nod){ + + stk.push_back(nod); + + onStack[nod]=true; + + low[nod]=tin[nod]=++mom; + + for(auto &x:muchii[nod]){ + + if(onStack[x]){ + + low[nod]=min(low[nod], tin[x]); + + } + + else if(tin[x]==0){ + + dfs(x); + + low[nod]=min(low[nod], low[x]); + + } + + } + + if(low[nod]>=tin[nod]){ + + comp.push_back(vector()); + + while(!stk.empty()&&stk.back()!=nod){ + + comp.back().push_back(stk.back()); + + partComp[stk.back()]=comp.size(); + + onStack[stk.back()]=false; + + stk.pop_back(); + + } + + comp.back().push_back(nod); + + partComp[stk.back()]=comp.size(); + + onStack[stk.back()]=false; + + stk.pop_back(); + + } + +} + +ostream & operator << (ostream & out, pair & per){ + + out< + +ostream & operator << (ostream & out, vector & vec){ + + for(auto &x:vec) + + out< > sol; + +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=m; i++){ + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(tin[i]==0) + + dfs(i); + + + + out< +#include +#include +#include +using namespace std; + +const int DIM = 100005; + +int low[DIM], lev[DIM]; +bool vis[DIM], ins[DIM]; +vector stk, edg[DIM]; +vector> ctc; + +void dfs(int x) { + static int ctr = 0; + low[x] = lev[x] = ++ctr; + vis[x] = ins[x] = true; + stk.push_back(x); + + for (int y : edg[x]) { + if (!vis[y]) + dfs(y); + if (ins[y]) + low[x] = min(low[x], low[y]); + } + + if (low[x] == lev[x]) { + ctc.push_back(vector()); + int y; + do { + y = stk.back(); + ins[y] = false; stk.pop_back(); + ctc.back().push_back(y); + } while (y != x); + } +} + +int main(void) { + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + int n, m; + cin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + edg[x].push_back(y); + } + for (int i = 1; i <= n; ++i) { + if (!vis[i]) + dfs(i); + } + cout << ctc.size() << "\n"; + for (auto ct : ctc) { + for (int x : ct) + cout << x << " "; + cout << "\n"; + } + return 0; +} diff --git a/scc/tarjan/15cdcac4-fe9f-43e8-94c1-808ad0057c81.cpp b/scc/tarjan/15cdcac4-fe9f-43e8-94c1-808ad0057c81.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d302b743d07dbc340fdb92759caa763f6071b412 --- /dev/null +++ b/scc/tarjan/15cdcac4-fe9f-43e8-94c1-808ad0057c81.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int NMAX=1e5+10; + +vector < int > Edge[NMAX], Components[NMAX]; + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + +bitset < NMAX > Seen, Belongs; + +stack < int > Stack; + + + +void Read() + +{ + + in>>n>>m; + + int x, y; + + for(int i=1; i<=m; i++) + + in>>x>>y, Edge[x].push_back(y); + +} + + + +void Tarjan(int x) + +{ + + niv++; + + Seen[x]=1; + + Belongs[x]=1; + + Stack.push(x); + + RootLevel[x]=niv; + + NodeLevel[x]=niv; + + for(int i : Edge[x]) + + { + + if(!Seen[i]) + + { + + Tarjan(i); + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + else + + if(Belongs[i]) + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + if(RootLevel[x]==NodeLevel[x]) + + { + + nrCMP++; + + int node=0; + + do + + { + + node=Stack.top(); + + Stack.pop(); + + Components[nrCMP].push_back(node); + + Belongs[node]=0; + + }while(node!=x); + + } + +} + + + +int main() + +{ + + Read(); + + for(int i=1; i<=n; i++) + + if(!Seen[i]) + + niv=0, Tarjan(i); + + out< +using namespace std; + +int n, m, t; +vector adj[100001]; +int ind[100001]; +int low[100001]; +int vizitat[100001]; +stack s; +vector> ans; + +void components(int current) +{ + ind[current] = ++t; + low[current] = t; + vizitat[current] = 1; + s.push(current); + for(auto x : adj[current]) + { + if(ind[x] == -1) + { + components(x); + low[current] = min(low[current], low[x]); + } + else if(vizitat[x] == 1) + low[current] = min(low[current], ind[x]); + } + int w = 0; + set temp; + if(low[current] == ind[current]) + { + while(s.top() != current) + { + w = s.top(); + temp.insert(w); + vizitat[w] = 0; + s.pop(); + } + w = s.top(); + temp.insert(w); + vizitat[w] = 0; + s.pop(); + ans.push_back(temp); + } +} + +void Tarjan() +{ + for(int i = 1; i <= n; i++) + { + ind[i] = -1; + low[i] = -1; + vizitat[i] = 0; + } + for(int i = 1; i <= n; i++) + if(ind[i] == -1) + components(i); +} +int main() { + ifstream f("ctc.in"); + ofstream g("ctc.out"); + f >> n >> m; + for(int i = 0; i < m; i++) + { + int x,y; + f >> x >> y; + adj[x].push_back(y); + } + Tarjan(); + g << ans.size() << '\n'; + for(auto &x : ans) + { + for(auto el : x) + g << el << " "; + g << '\n'; + } + f.close(); + g.close(); + return 0; +} diff --git a/scc/tarjan/180be3d2-027e-4b3d-ae77-0362dd339f45.cpp b/scc/tarjan/180be3d2-027e-4b3d-ae77-0362dd339f45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c53e7519d814aed94c87a32d6f38324ff735663 --- /dev/null +++ b/scc/tarjan/180be3d2-027e-4b3d-ae77-0362dd339f45.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + +int n, m, id[nmax], low[nmax], z; + +bool inStack[nmax]; + +vector G[nmax]; + +stack s; + +vector > ans; + +void tarjan(int nod){ + + id[nod] = low[nod] = ++z; + + s.push(nod); + + inStack[nod] = true; + + for (auto it : G[nod]){ + + if (id[it] == 0){ + + tarjan(it); + + low[nod] = min(low[nod], low[it]); + + } + + else{ + + if (inStack[it]){ + + low[nod] = min(low[nod], id[it]); + + } + + } + + } + + if (id[nod] == low[nod]){ + + vector aux; + + int snod; + + do{ + + snod = s.top(); + + inStack[snod] = false; + + s.pop(); + + aux.push_back(snod); + + } while (snod != nod); + + ans.push_back(aux); + + } + +} + + + +int main(){ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i){ + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i){ + + if (id[i] == 0){ + + tarjan(i); + + } + + } + + fout << ans.size() << "\n"; + + for (int i = 0; i < ans.size(); ++i){ + + for (int j = 0; j < ans[i].size(); ++j){ + + fout << ans[i][j] << " "; + + } + + fout << "\n"; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/185c333f-7f2e-4910-aead-d00f09682845.cpp b/scc/tarjan/185c333f-7f2e-4910-aead-d00f09682845.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b315e6faa5ae38a389d5b1f7c8fd6f1cae2cdb0d --- /dev/null +++ b/scc/tarjan/185c333f-7f2e-4910-aead-d00f09682845.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +const int NMAX = 100005; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int lowlink[NMAX]; + +int ids[NMAX]; + +bool inStack[NMAX]; + +int groupCounter = 1; + + + + + +int n, m; + +vector graf[NMAX]; + +stack stac; + +vector> components; + +void ctc(int node) + +{ + + lowlink[node] = groupCounter++; + + ids[node] = lowlink[node]; + + inStack[node] = true; + + stac.push(node); + + for(auto i:graf[node]) + + { + + if(ids[i]==0) ctc(i); + + if(inStack[i]) lowlink[node]=min(lowlink[node], lowlink[i]); + + } + + if(lowlink[node]==ids[node]) + + { + + vector component; + + while(stac.top()!=node) + + { + + component.push_back(stac.top()); + + lowlink[stac.top()]=lowlink[node]; + + inStack[stac.top()]=false; + + stac.pop(); + + } + + component.push_back(node); + + inStack[node]=false; + + stac.pop(); + + components.push_back(component); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m--) + + { + + int a, b; + + fin>>a>>b; + + graf[a].push_back(b); + + } + + for(int i = 1;i<=n;i++) + + { + + if(ids[i]==0) + + ctc(i); + + } + + fout< +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("ctc.in"); +ofstream fout ("ctc.out"); + +const int Nmax = 100555; +int counter = 0; +int ind[Nmax], lowkey[Nmax]; +stack st; +char inStack[Nmax]; +vi G[Nmax]; +int ans = 0; +vector ctc; + +void dfs(int nod) { + if (ind[nod] != INF) { return; } + + ind[nod] = lowkey[nod] = ++counter; + st.push(nod); inStack[nod] = 1; + for(auto y: G[nod]) { + if (ind[y] == INF) { + dfs(y); + } + if (inStack[y]) { + lowkey[nod] = min(lowkey[nod], lowkey[y]); + } + } + if (ind[nod] == lowkey[nod]) { + ctc.resize(++ans); + vi &comp = ctc.back(); + while(!st.empty()) { + int x = st.top(); st.pop(); + inStack[x] = 0; + comp.push_back(x); + if (x == nod) { + break; + } + } + } +} + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + memset(ind, 0x3f, sizeof(ind)); + memset(lowkey, 0x3f, sizeof(ind)); + int N, M, a, b; + fin >> N >> M; + for(int i = 0; i < (int)(M); i++) { + fin >> a >> b; + --a, --b; + G[a].push_back(b); + } + for(int i = 0; i < (int)(N); i++) { + if (ind[i] == INF) { + dfs(i); + } + } + fout << ans << '\n'; + for(const auto& comp: ctc) { + for(auto x: comp) { + fout << x+1 << " "; + } + fout << '\n'; + } + + return 0; +} diff --git a/scc/tarjan/18c0436c-ba33-432c-bfcb-f8e659f0a34a.cpp b/scc/tarjan/18c0436c-ba33-432c-bfcb-f8e659f0a34a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..761d3e3b57829d05d4a56700938f7a3a9bcb146f --- /dev/null +++ b/scc/tarjan/18c0436c-ba33-432c-bfcb-f8e659f0a34a.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 100001; + +int id=0, tot=0; + +string resp=""; + +vector graf[NMAX]; + +int ids[NMAX]; + +int low[NMAX]; + +bool onStack[NMAX]; + +stack s; + + + +void DFS(int curr) { + + onStack[curr] = true; + + ids[curr] = ++id; + + low[curr] = id; + + s.push(curr); + + for (int i = 0; i < graf[curr].size(); i++) { + + if (ids[graf[curr][i]] == 0) { + + DFS(graf[curr][i]); + + } + + if (onStack[graf[curr][i]]) { + + low[curr] = min(low[curr], low[graf[curr][i]]); + + } + + } + + + + if (ids[curr] == low[curr]) { + + while (!s.empty()) { + + int node = s.top(); + + resp += to_string(node); + + resp += " "; + + s.pop(); + + onStack[node] = false; + + low[node] = ids[curr]; + + if (node == curr) { + + break; + + } + + } + + tot++; + + resp += "\n"; + + } + +} + + + +int main() { + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + int n, m, x, y; + + in >> n >> m; + + + + for (int i = 0; i < m; i++) { + + in >> x >> y; + + graf[x].push_back(y); + + } + + + + for (int i = 1; i <= n; i++) { + + if (ids[i] == 0) { + + DFS(i); + + } + + } + + + + out << tot << "\n" << resp; + + return 0; + +} diff --git a/scc/tarjan/19e1100a-a8f1-40df-b93b-ec843e653a4d.cpp b/scc/tarjan/19e1100a-a8f1-40df-b93b-ec843e653a4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ade74d3fe6892a6fda4b861515cbfb920d849504 --- /dev/null +++ b/scc/tarjan/19e1100a-a8f1-40df-b93b-ec843e653a4d.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +#include + + +using namespace std; +vector adj[100000],con,idx,lowlink,in_stack; + +vector > G; + +stack S; + +int n,m,x,y,indecs; + +void citire() + + { + + ifstream fin ("ctc.in"); + + fin>>n>>m; + + for (int i=1;i<=m;i++) + + { + + fin>>x>>y; + + adj[x-1].push_back(y-1); + + } + + fin.close(); + + } + +void afisare() + + { + + ofstream fout ("ctc.out"); + + int l1=G.size(); + + fout<::const_iterator it; + + for (it = adj[n].begin();it!=adj[n].end();it++) + + { + + if (idx[*it]==-1) + + { + + tarjan(*it); + + lowlink[n]=((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + else if (in_stack[*it]) + + lowlink[n]=((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do { + + node=S.top(); + + con.push_back(node); + + S.pop(); + + in_stack[node]=0; + + } + + while (node != n); + + G.push_back(con); + + } + + } + +int main () + + { + + citire(); + + idx.resize(n); + + lowlink.resize(n); + + in_stack.resize(n); + + idx.assign(n,-1); + + in_stack.assign(n,0); + + for (int i=0;i +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N_MAX = 1e5 + 1; + + + + + +int N, M; + +vector> graph(N_MAX, vector()); + + + + + +vector> answer; + +int id[N_MAX], low[N_MAX], t; + +bool onStack[N_MAX]; + +stack node_stack; + + + +void dfs_scc(int node) + +{ + + ++t; + + id[node] = t; + + low[node] = t; + + onStack[node] = true; + + node_stack.push(node); + + + + for(auto& next : graph[node]) + + { + + if(id[next] == 0) + + dfs_scc(next); + + + + if(onStack[next]) + + low[node] = min(low[node], low[next]); + + } + + + + if(id[node] == low[node]) + + { + + answer.push_back(vector()); + + + + while(node_stack.top() != node) + + { + + answer.back().push_back(node_stack.top()); + + onStack[node_stack.top()] = false; + + node_stack.pop(); + + } + + + + answer.back().push_back(node_stack.top()); + + onStack[node_stack.top()] = false; + + node_stack.pop(); + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + + + for(int x, y, i = 1; i <= M; ++i) + + { + + fin >> x >> y; + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= N; ++i) + + { + + if(id[i] == 0) + + { + + dfs_scc(i); + + } + + } + + + + + + fout << answer.size() << '\n'; + + + + for(auto& ctc : answer) + + { + + for(auto& node : ctc) + + { + + fout << node << ' '; + + } + + + + fout << '\n'; + + } + +} diff --git a/scc/tarjan/1ad57649-52bb-440e-955d-271c3845985d.cpp b/scc/tarjan/1ad57649-52bb-440e-955d-271c3845985d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..32ecc2b5a42def85710ee819b264c575d6f8a217 --- /dev/null +++ b/scc/tarjan/1ad57649-52bb-440e-955d-271c3845985d.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int maxi = 200001; +vector a[maxi], compo; +vector> componente; +stack stiva; +int disc[maxi], low[maxi], vizitat[maxi]; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int index = 0; + +void dfs(int u) { + disc[u] = low[u] = ++index; + stiva.push(u); + vizitat[u] = 1; + for (int v : a[u]) { + if (disc[v] == -1) { + dfs(v); + if (low[u] > low[v]) + low[u] = low[v]; + } else if (vizitat[v] && disc[v] < low[u]) + low[u] = disc[v]; + } + int w = 0; + if (low[u] == disc[u]) { + compo.clear(); + while (stiva.top() != u) + { + w = stiva.top(); + + compo.push_back(w); + vizitat[w] = 0; + stiva.pop(); + } + w = stiva.top(); + + compo.push_back(w); + vizitat[w] = 0; + stiva.pop(); + componente.push_back(compo); + } +} + +void ctc(int n) { + int i; + for (i = 1; i <= n; ++i) { + disc[i] = low[i] = -1; + } + for (i = 1; i <= n; ++i) { + if (disc[i] == -1) { + dfs(i); + } + } + fout << componente.size() << "\n"; + for(auto lst : componente) { + for(auto nod : lst) + fout << nod << " "; + fout << "\n"; + } + +} + +int main() { + int n, m, x, y; + fin >> n >> m; + for (int i = 1; i <= m; ++i) { + fin >> x >> y; + a[x].push_back(y); + } + ctc(n); + return 0; +} diff --git a/scc/tarjan/1b6c793e-bd8b-487d-b19c-8558229cc438.cpp b/scc/tarjan/1b6c793e-bd8b-487d-b19c-8558229cc438.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff423e2a5412c21277c4ba71a0e90d050186670f --- /dev/null +++ b/scc/tarjan/1b6c793e-bd8b-487d-b19c-8558229cc438.cpp @@ -0,0 +1,77 @@ +# include +# include + + +using namespace std; + + + + + + +int N, M, i, vf, ct, Time; +int St[100005]; +int Lvl[100005]; +int Low[100005]; +vector G[100005]; +vector CTC[100005]; + + void Dfs(int nod) + { + vector :: iterator it; + + St[++vf] = nod; + Lvl[nod] = Low[nod] = ++Time; + for (it = G[nod].begin(); it != G[nod].end(); ++it) + { + if (!Lvl[*it]) + { + Dfs(*it); + Low[nod] = (Low[nod] < Low[*it] ? Low[nod] : Low[*it]); + } else + if (Lvl[*it] > 0) + Low[nod] = (Low[nod] < Lvl[*it] ? Low[nod] : Lvl[*it]); + } + + if (Low[nod] == Lvl[nod]) + { + ++ct; + for (i = vf; St[i + 1] != nod; --i) + { + CTC[ct].push_back(St[i]); + Lvl[St[i]] = -1; + } + vf = i; + } + } + + int main() + { + freopen("ctc.in","r",stdin); + freopen("ctc.out","w",stdout); + + scanf("%d %d",&N, &M); + for (i = 1; i <= M; ++i) + { + int x, y; + scanf("%d %d",&x, &y); + + G[x].push_back(y); + } + + vf = ct = Time = 0; + for (i = 1; i <= N; ++i) + if (!Lvl[i]) Dfs(i); + + printf("%d\n",ct); + + vector :: iterator it; + for (i = 1; i <= ct; ++i) + { + for (it = CTC[i].begin(); it != CTC[i].end(); ++it) + printf("%d ",*it); + printf("\n"); + } + + return 0; + } diff --git a/scc/tarjan/1ba94e77-b763-4fa7-85fd-a61f4a99c635.cpp b/scc/tarjan/1ba94e77-b763-4fa7-85fd-a61f4a99c635.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54f22dccb8c776c6745689f3d097faecacef9cd6 --- /dev/null +++ b/scc/tarjan/1ba94e77-b763-4fa7-85fd-a61f4a99c635.cpp @@ -0,0 +1,136 @@ +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100001], tc[100001]; + +stack stiva; + +bool inStiva[100001]; + +int n, ctc, id, viz[100001], leg[100001]; + + + +void citire() + +{ + + int m; + + f>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + f>>x>>y; + + v[x].push_back(y); + + } + +} + + + +void dfs(int nod) + +{ + + stiva.push(nod); + + inStiva[nod] = 1; + + viz[nod] = leg[nod] = ++id; + + for(int i = 0; i < v[nod].size(); i++) + + { + + if(!viz[v[nod][i]]) + + { + + dfs(v[nod][i]); + + leg[nod] = min(leg[nod], leg[v [nod][i]]); + + } + + else if(inStiva[v[nod][i]]) + + leg[nod] = min(leg[nod], leg[v[nod][i]]); + + } + + if(leg[nod] == viz[nod]) + + { + + ctc++; + + while(true) + + { + + tc[ctc].push_back(stiva.top()); + + inStiva[stiva.top()] = 0; + + if(stiva.top() == nod) + + { + + stiva.pop(); + + break; + + } + + stiva.pop(); + + } + + } + +} + + + +int main() + +{ + + citire(); + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + dfs(i); + + g< + + + + + + +using namespace std; +const int NMAX=1e5+10; + + + +vector < int > Edge[NMAX], Components[NMAX]; + + + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + + + +bitset < NMAX > Seen, Belongs; + + + +stack < int > Stack; + + + + + + + +void Read() + + + +{ + + freopen("ctc.in", "r", stdin); + + scanf("%d%d", &n, &m); + + + + + + int x, y; + + + + for(int i=1; i<=m; i++) + + + + { + + + + scanf("%d%d", &x, &y); + + Edge[x].push_back(y); + + } + + + +} + + + + + + + +void Tarjan(int x) + + + +{ + + + + niv++; + + + + Seen[x]=1; + + + + Belongs[x]=1; + + + + Stack.push(x); + + + + RootLevel[x]=niv; + + + + NodeLevel[x]=niv; + + + + for(int i : Edge[x]) + + + + { + + + + if(!Seen[i]) + + + + { + + + + Tarjan(i); + + + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + + + } + + + + else + + + + if(Belongs[i]) + + + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + + + } + + + + if(RootLevel[x]==NodeLevel[x]) + + + + { + + + + nrCMP++; + + + + int node=0; + + + + do + + + + { + + + + node=Stack.top(); + + + + Stack.pop(); + + + + Components[nrCMP].push_back(node); + + + + Belongs[node]=0; + + + + } + + while(node!=x); + + + + } + + + +} + + + + + + + +int main() + + + +{ + + + + Read(); + + + + for(int i=1; i<=n; i++) + + + + if(!Seen[i]) + + + + niv=0, Tarjan(i); + + freopen("ctc.out", "w", stdout); + + printf("%d", nrCMP); + + printf("\n"); + + + + + + for(int i=1; i<=nrCMP; i++) + + + + { + + + + for(int it : Components[i]) + + printf("%d ", it); + + + + printf("\n"); + + + + + + } + + + + return 0; + + + + + + + +} diff --git a/scc/tarjan/21074e2a-f6f8-4bba-8f4c-df3aba98daf5.cpp b/scc/tarjan/21074e2a-f6f8-4bba-8f4c-df3aba98daf5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..753744c86ebeabf4e688fe10cfc709f45ebc3cc4 --- /dev/null +++ b/scc/tarjan/21074e2a-f6f8-4bba-8f4c-df3aba98daf5.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + +using namespace std; + + + +vector> arcs; + +vector lowlink, index; + +vector onStack; + + + +vector> components; + + + +stack st; + + + +int currentComp; + + + +int min(int x, int y) { + + return x()); + + + + int n; + + do { + + n = st.top(); + + onStack[st.top()] = false; + + st.pop(); + + + + components[components.size() - 1].push_back(n); + + } while (n != x); + + } + +} + + + + + +int main() { + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + int n, m, x, y; + + in >> n >> m; + + + + arcs.resize(n+2); + + lowlink.resize(n+2, 0); + + index.resize(n+2, 0); + + onStack.resize(n+2, false); + + + + for(int i=0; i> x >> y; + + arcs[x].push_back(y); + + } + + + + for(int i=1; i<=n; ++i) + + if (index[i] == 0) + + strongConnect(i); + + + + out << components.size() << "\n"; + + for(int i=0; i +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +std::stack stack; + +std::vector cnx[100005]; + +int disc[100005], low[100005], _time, cnxCount; + +bool inStack[100005]; + + + +void DFS(int vertex = 1) { + + stack.push(vertex); + + inStack[vertex] = true; + + disc[vertex] = low[vertex] = ++_time; + + + + for (auto &it:ADC[vertex]) { + + if (disc[it]) { + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + else { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (disc[vertex] == low[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + inStack[stack.top()] = false; + + cnx[cnxCount].push_back(stack.top()); + + stack.pop(); + + } + + inStack[stack.top()] = false; + + cnx[cnxCount].push_back(stack.top()); + + stack.pop(); + + ++ cnxCount; + + } + +} + + + +int main() + +{ + + input >> N >> M; + + for (int i=0, x, y; i> x >> y, addEdge(x, y); + + + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + + + output << cnxCount << '\n'; + + for (int i=0; i +#include +#include +#include +#include + + +using namespace std; +const int N_MAX = 1e5 + 1; + + + +struct TarjanVertex{ + + int index, tarjanIndex = -1, lowlink; + + bool onStack = false; + +}; + + + +vector graph[N_MAX]; + +stack stk; + +vector graphVertexes; + +vector > solutions; + + + +int nextAvailableIndex = 1; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void Tarjan(int nodeIndex) + +{ + + graphVertexes[nodeIndex].tarjanIndex = nextAvailableIndex; + + graphVertexes[nodeIndex].lowlink = nextAvailableIndex; + + nextAvailableIndex++; + + stk.push(nodeIndex); + + graphVertexes[nodeIndex].onStack = true; + + + + for(auto neighborIndex: graph[graphVertexes[nodeIndex].index]) + + { + + if(graphVertexes[neighborIndex].tarjanIndex == -1) + + { + + Tarjan(graphVertexes[neighborIndex].index); + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].lowlink, graphVertexes[nodeIndex].lowlink); + + } + + else if (graphVertexes[neighborIndex].onStack == true) + + { + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].tarjanIndex, graphVertexes[nodeIndex].lowlink); + + } + + } + + + + if(graphVertexes[nodeIndex].tarjanIndex == graphVertexes[nodeIndex].lowlink) + + { + + vector sol; + + int neighborIndex; + + do + + { + + neighborIndex = stk.top(); + + sol.push_back(neighborIndex); + + stk.pop(); + + graphVertexes[neighborIndex].onStack = false; + + } while (neighborIndex != nodeIndex); + + + + solutions.push_back(sol); + + } + +} + + + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + + + graphVertexes.resize(n + 1); + + for(int i = 1; i <= n; i++) + + { + + graphVertexes[i].index = i; + + } + + + + for(int i = 0, st, dr; i < m; i++) + + { + + fin >> st >> dr; + + graph[st].push_back(dr); + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(graphVertexes[i].tarjanIndex != -1) + + continue; + + + + Tarjan(i); + + } + + + + + + fout << solutions.size() << '\n'; + + + + for(auto sol: solutions) + + { + + for(auto val: sol) + + { + + fout << val << ' '; + + } + + + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/2320a75d-e813-4772-bd71-ca50f28d1e94.cpp b/scc/tarjan/2320a75d-e813-4772-bd71-ca50f28d1e94.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b6e5d6dd430645f5da3341b088fe118953857a7 --- /dev/null +++ b/scc/tarjan/2320a75d-e813-4772-bd71-ca50f28d1e94.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, ctc, t, T[100000 + 1], M[100000 + 1]; + +vector G[100000 + 1], C[100000 + 1]; + +stack N; + +bitset<100000 + 1> inN; + + + +void Df(int nod); + + + +int main() + +{ + + fin >> n >> m; + + + + for (int i = 1, x, y; i <= m; ++i) + + { + + fin >> x >> y; + + G[x].push_back(y); + + } + + + + for (int i = 1; i <= n; ++i) + + { + + if (T[i] == 0) + + { + + Df(i); + + } + + } + + + + fout << ctc << '\n'; + + for (int i = 1; i <= ctc; ++i) + + { + + for (int nod : C[i]) + + { + + fout << nod << ' '; + + } + + fout << '\n'; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} + + + +void Df(int nod) + +{ + + M[nod] = T[nod] = ++t; + + + + N.push(nod); + + for (int vecin : G[nod]) + + { + + if (T[vecin] == 0) + + { + + Df(vecin); + + M[nod] = min(M[nod], M[vecin]); + + } + + else + + { + + if (!inN[vecin]) + + { + + M[nod] = min(M[nod], T[vecin]); + + } + + } + + } + + + + if (T[nod] == M[nod]) + + { + + ++ctc; + + + + while (N.top() != nod) + + { + + C[ctc].push_back(N.top()); + + inN[N.top()] = true; + + N.pop(); + + } + + + + C[ctc].push_back(N.top()); + + inN[N.top()] = true; + + N.pop(); + + } + +} diff --git a/scc/tarjan/23317260-49f9-452f-837e-dadb496c96fd.cpp b/scc/tarjan/23317260-49f9-452f-837e-dadb496c96fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5592d66996ded280bf2fae308ee50aa5363bc8ef --- /dev/null +++ b/scc/tarjan/23317260-49f9-452f-837e-dadb496c96fd.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N = 100041; + + + +int n, m; + +vector gra[N]; + +int idx[N], lol[N]; + +bool vi[N]; + + + +stack staketa; + +vector> answereta; + + + +int indexeta = 1; + + + +void defeseta(int a){ + + staketa.push(a); + + idx[a] = indexeta++; + + lol[a] = idx[a]; + + + + vi[a] = true; + + + + for(auto b : gra[a]){ + + if(idx[b] == 0){ + + defeseta(b); + + lol[a] = min(lol[a], lol[b]); + + }else if(vi[b]){ + + lol[a] = min(lol[a], idx[b]); + + } + + } + + + + if(lol[a] == idx[a]){ + + answereta.push_back({}); + + vector &baketa = answereta.back(); + + while(staketa.top() != a){ + + baketa.push_back(staketa.top()); + + vi[staketa.top()] = false; + + staketa.pop(); + + } + + baketa.push_back(staketa.top()); + + vi[staketa.top()] = false; + + staketa.pop(); + + } + +} + + + +int main(){ + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + int a, b; + + fin>> a >> b; + + gra[a].push_back(b); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(idx[i]==0)defeseta(i); + + } + + + + fout << answereta.size() << "\n"; + + for(auto v : answereta){ + + for(auto a : v){ + + fout << a << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/236b07ed-daff-4f48-b6d7-ca7c71a54510.cpp b/scc/tarjan/236b07ed-daff-4f48-b6d7-ca7c71a54510.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ac040fb08551cd59a15f7a0f11c6184b3912d6f --- /dev/null +++ b/scc/tarjan/236b07ed-daff-4f48-b6d7-ca7c71a54510.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +std::ifstream in("ctc" ".in"); + +std::ofstream out("ctc" ".out"); + +const int N = 100000; + +std::vector L[N], S; + +int LL[N], ID[N], nId; + +std::bitset E, pS; + +std::vector> CTC; + +void dfs(int t) + +{ + + pS[t] = true; + + ID[t] = LL[t] = nId++; + + S.push_back(t); + + for (int f: L[t]) + + if (not E[f]) + + { + + if (not pS[f]) + + dfs(f); + + LL[t] = std::min(LL[t], LL[f]); + + } + + if (ID[t] == LL[t]) + + { + + CTC.emplace_back(); + + int back; + + do + + { + + back = S.back(); + + S.pop_back(); + + CTC.back().push_back(back); + + E[back] = true; + + } + + while (back != t); + + } + +} + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + int a, b; + + in >> a >> b; + + L[--a].push_back(--b); + + } + + for (int i = 0; i < n; i++) + + if (not E[i]) + + dfs(i); + + out << CTC.size() << '\n'; + + for (auto ctc: CTC) + + { + + for (int i: ctc) + + out << i + 1 << ' '; + + out << '\n'; + + } + +} diff --git a/scc/tarjan/241cbbdc-2092-4ea7-8e39-2666d94d1033.cpp b/scc/tarjan/241cbbdc-2092-4ea7-8e39-2666d94d1033.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ee6d9817a5c3bb48ddabe5aeb4a46ff7038fc10 --- /dev/null +++ b/scc/tarjan/241cbbdc-2092-4ea7-8e39-2666d94d1033.cpp @@ -0,0 +1,182 @@ +#include + + +using namespace std; + + + +int id_n[100005], low_link[100005]; + +bool inStack[100005]; + +vector edge[100005]; + +vector> ctc; + +vector conex; + +stack stk; + +int dist = 0; + +int n,m; + + + + + +void Tarjan(int node){ + + + + id_n[node] = dist; + + inStack[node] = true; + + low_link[node] = dist; + + stk.push(node); + + dist ++; + + + + for (int i = 0 ; i < edge[node].size(); i++) + + { + + if (id_n[edge[node][i]] == -1) + + { + + + + Tarjan(edge[node][i]); + + + + low_link[node] = min(low_link[node], + + low_link[edge[node][i]]); + + } + + else if (inStack[edge[node][i]]) + + { + + int aux = edge[node][i]; + + low_link[node] = min (low_link[node], low_link[aux]); + + } + + } + + if(id_n[node] == low_link[node]) + + { + + conex.clear(); + + int aux; + + do + + { + + aux = stk.top(); + + conex.push_back(aux); + + stk.pop(); + + inStack[aux] = 0; + + } + + while (node != aux); + + ctc.push_back(conex); + + } + + + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + int x, y; + + fin >> n >> m ; + + + + for (int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + edge[x].push_back(y); + + } + + + + for (int i = 1; i<= n; i++) + + { + + inStack[i] = 0; + + id_n[i] = -1; + + } + + + + for (int i = 1; i <= n; i++) + + if (id_n[i] == -1) + + Tarjan(i); + + + + + + fout << ctc.size(); + + for (int i = 0 ; i < ctc.size(); i++) + + { + + fout << "\n"; + + for (int j = 0; j < ctc[i].size(); j++) + + fout << ctc[i][j] <<" "; + + } + + + + + + + + return 0; + +} diff --git a/scc/tarjan/25e68514-ff0e-49dd-88f6-d2caa9511fb9.cpp b/scc/tarjan/25e68514-ff0e-49dd-88f6-d2caa9511fb9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25ebd0e243495f91e9b38be13eccb418f1c5c511 --- /dev/null +++ b/scc/tarjan/25e68514-ff0e-49dd-88f6-d2caa9511fb9.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int g,low[100005],niv[100005],ctc,n,m,x,y,i,j; + +bitset<100005> IQ; + +deque q; + +vector L[100005],C[100005]; + +void dfs(int nod) + +{ + + g++; + + low[nod]=g; + + niv[nod]=g; + + IQ[nod]=1; + + q.push_back(nod); + + for(int i=0; i>n>>m; + + for(; m--;) + + { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(niv[i]==0) + + { + + dfs(i); + + } + + } + + fout< + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, comp = 0, id = 1; + +vector M[100002], sol; + +bool instk[100002]; + +stack stk; + +int low[100002], ids[100002]; + +vector> Solutii; + + + +void DFS(int k) + +{ + + stk.push(k); + + instk[k] = true; + + ids[k] = low[k] = id++; + + + + for(int y : M[k]) + + { + + if(!ids[y]) + + DFS(y); + + + + if(instk[y]) + + low[k] = min(low[y], low[k]); + + } + + + + if(ids[k] == low[k]) + + { + + comp++; + + int node; + + while(1) + + { + + node = stk.top(); + + sol.push_back(node); + + instk[node] = false; + + low[node] = ids[k]; + + stk.pop(); + + if(node == k) break; + + } + + Solutii.push_back(sol); + + sol.clear(); + + + + } + +} + + + + + +int main() + +{ + + int x, y; + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + M[x].push_back(y); + + } + + + + for(int i = 1; i <= n; i++) + + if(!ids[i]) + + DFS(i); + + + + fout << comp << '\n'; + + for(int i = 0; i < comp; i++) + + { + + for(int j : Solutii[i]) + + fout << j << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/281c9a12-9072-4a01-a6f2-48cb464b2b18.cpp b/scc/tarjan/281c9a12-9072-4a01-a6f2-48cb464b2b18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85deadb58f96fefdc7c5a39add890a6beab9d934 --- /dev/null +++ b/scc/tarjan/281c9a12-9072-4a01-a6f2-48cb464b2b18.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + +int n, m, id[nmax], low[nmax], z; + +bool inStack[nmax]; + +vector G[nmax]; + +stack s; + +vector > ans; + +void tarjan(int nod){ + + id[nod] = low[nod] = ++z; + + s.push(nod); + + inStack[nod] = true; + + for (auto it : G[nod]){ + + if (id[it] == 0){ + + tarjan(it); + + low[nod] = min(low[nod], low[it]); + + } + + else{ + + if (inStack[it]){ + + low[nod] = min(low[nod], low[it]); + + } + + } + + } + + if (id[nod] == low[nod]){ + + vector aux; + + int snod; + + do{ + + snod = s.top(); + + inStack[snod] = false; + + s.pop(); + + aux.push_back(snod); + + } while (snod != nod); + + ans.push_back(aux); + + } + +} + + + +int main(){ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i){ + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i){ + + if (id[i] == 0){ + + tarjan(i); + + } + + } + + fout << ans.size() << "\n"; + + for (int i = 0; i < ans.size(); ++i){ + + for (int j = 0; j < ans[i].size(); ++j){ + + fout << ans[i][j] << " "; + + } + + fout << "\n"; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/282a2fc1-4480-46f7-a7e1-3a1677c49b4e.cpp b/scc/tarjan/282a2fc1-4480-46f7-a7e1-3a1677c49b4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..308d3c275ff69b4bf6fc93dbd693270321fc5ef8 --- /dev/null +++ b/scc/tarjan/282a2fc1-4480-46f7-a7e1-3a1677c49b4e.cpp @@ -0,0 +1,144 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, comp = 0, id = 1; + +vector M[100002], sol; + +bool instk[100002]; + +stack stk; + +int low[100002], ids[100002]; + +vector> Solutii; + + + +void DFS(int k) + +{ + + stk.push(k); + + instk[k] = true; + + ids[k] = low[k] = id++; + + + + for(int y : M[k]) + + { + + if(!ids[y]) + + DFS(y); + + + + if(instk[y]) + + low[k] = min(low[y], low[k]); + + } + + + + if(ids[k] == low[k]) + + { + + comp++; + + int node; + + while(1) + + { + + node = stk.top(); + + sol.push_back(node); + + instk[node] = false; + + low[node] = ids[k]; + + stk.pop(); + + if(node == k) break; + + } + + Solutii.push_back(sol); + + sol.clear(); + + + + } + +} + + + + + +int main() + +{ + + int x, y; + + fin >> n >> m; + + for(int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + M[x].push_back(y); + + } + + + + for(int i = 1; i <= n; i++) + + if(!ids[i]) + + DFS(i); + + + + fout << comp << '\n'; + + for(int i = 0; i < comp; i++) + + { + + for(int j : Solutii[i]) + + fout << j << ' '; + + fout << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/29e2bf1d-322f-4f82-a4f5-e0deddde8bdf.cpp b/scc/tarjan/29e2bf1d-322f-4f82-a4f5-e0deddde8bdf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0df79abf2bb715ae8de02755d903ff72e0efe13 --- /dev/null +++ b/scc/tarjan/29e2bf1d-322f-4f82-a4f5-e0deddde8bdf.cpp @@ -0,0 +1,138 @@ +#include + + + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int lmt = 1e5+1; + + + +int n,m,id,ids[lmt], low[lmt], stccCount; + +vector aList[lmt], rsp[lmt]; + +stack stacc; + +bool on_stacc[lmt]; + + + +void dfs(int nod) + +{ + + on_stacc[nod]=1; + + ids[nod]=low[nod]=++id; + + stacc.push(nod); + + + + vector::iterator i; + + for(i=aList[nod].begin();i!=aList[nod].end();++i) + + { + + if(!ids[*i]) + + dfs(*i); + + if(on_stacc[*i]) + + low[nod]=min(low[nod], low[*i]); + + } + + if(low[nod]==ids[nod]) + + { + + ++stccCount; + + while(!stacc.empty()) + + { + + int poz = stacc.top(); + + rsp[stccCount].push_back(poz); + + on_stacc[stacc.top()]=0; + + stacc.pop(); + + + + if(poz==nod) + + break; + + } + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y; + + fin>>x>>y; + + aList[x].push_back(y); + + } + + for(int i=1;i<=n;++i) + + { + + if(!ids[i]) + + dfs(i); + + } + + fout<::iterator j; + + for(j=rsp[i].begin();j!=rsp[i].end();++j) + + fout<<*j<<' '; + + + + fout<<'\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/2a52fb69-2fa9-40ef-89ee-ce5598b8c983.cpp b/scc/tarjan/2a52fb69-2fa9-40ef-89ee-ce5598b8c983.cpp new file mode 100644 index 0000000000000000000000000000000000000000..161e2e09b1cb3148491d8865f26a0469ac6dca45 --- /dev/null +++ b/scc/tarjan/2a52fb69-2fa9-40ef-89ee-ce5598b8c983.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n) + +{ + + int cnt_edges, x, y; + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + +} + + + +void print_out(const vector < vector >& G) + +{ + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main() + +{ + + int n; + + read_in(adj, n); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C); + + + + return 0; + +} diff --git a/scc/tarjan/2a787851-e527-45f7-b45e-b9ded6ab7b8c.cpp b/scc/tarjan/2a787851-e527-45f7-b45e-b9ded6ab7b8c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28838298314b1993b156ebca98c7bb992ad7b9b6 --- /dev/null +++ b/scc/tarjan/2a787851-e527-45f7-b45e-b9ded6ab7b8c.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream o("ctc.out"); + + + +int id=0,n,m,cc=0,low[100010],ids[100010]; + +bool onStack[100010]; + +vector v[100010],ctc[100010]; + +stack S; + +void citire(){ + + f>>n>>m; int i,x,y; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + +} + +void dfs(int k){ + + ids[k]=low[k]=++id; + + S.push(k); int i,nv; + + onStack[k]=1; + + for(i=0;i + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stacks; + +vectorgraph[100005]; + +vectorrezultate[100005]; + +int n, m, a, viz[100005], b, llk[100005], ix[100005], ind=1, ind_rez; + + + +void solve(int val) + +{ + + int sus; + + llk[val]=ix[val]=ind++; + + s.push(val); + + viz[val]=1; + + for (auto &v:graph[val]) + + { + + if (!ix[v]) + + { + + solve(v); + + llk[val]=min(llk[val],llk[v]); + + } + + if (viz[v]) + + llk[val]=min(llk[val],llk[v]); + + } + + if (llk[val]==ix[val]) + + { + + while (!s.empty()) + + { + + sus=s.top(); + + s.pop(); + + viz[sus]=false; + + if (llk[sus]==ix[sus]) + + { + + rezultate[ind_rez].push_back(sus); + + ind_rez++; + + break; + + } + + else + + { + + rezultate[ind_rez].push_back(sus); + + } + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1; i<=m; ++i) + + { + + f >> a >> b; + + graph[a].push_back(b); + + } + + for (int i=1; i<=n; ++i) + + { + + if (!ix[i]) + + solve(i); + + } + + g << ind_rez << '\n'; + + for (int i=0; i + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,low[100005],idx[100005],nrc,x,y,t,st[100005],k,ap[100005]; + +vector comp[100005]; + +vector v[100005]; + +void dfs(int nod) + +{ + + low[nod]=t; + + idx[nod]=t; + + ++t; + + st[++k]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + if(idx[it]==0) + + { + + dfs(it); + + low[nod]=min(low[it],low[nod]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + if(low[nod]==idx[nod]) + + { + + nrc++; + + while(st[k]!=nod) + + { + + comp[nrc].push_back(st[k]); + + ap[st[k]]=0; + + k--; + + } + + comp[nrc].push_back(st[k]); + + ap[st[k]]=0; + + k--; + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) + + if(idx[i]==0) + + { + + t=1; + + k=0; + + dfs(i); + + } + + out< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +stacks; + +vectorgraph[100005]; + +vectorrezultate[100005]; + +int n, m, a, viz[100005], b, llk[100005], ix[100005], ind=1, ind_rez; + + + +void solve(int val) + +{ + + int sus; + + llk[val]=ix[val]=ind++; + + s.push(val); + + viz[val]=1; + + for (auto &v:graph[val]) + + { + + if (!ix[v]) + + { + + solve(v); + + llk[val]=min(llk[val],llk[v]); + + } + + if (viz[v]) + + llk[val]=min(llk[val],llk[v]); + + } + + if (llk[val]==ix[val]) + + { + + while (!s.empty()) + + { + + sus=s.top(); + + s.pop(); + + viz[sus]=false; + + if (llk[sus]==ix[sus]) + + { + + rezultate[ind_rez].push_back(sus); + + ind_rez++; + + break; + + } + + else + + { + + rezultate[ind_rez].push_back(sus); + + } + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1; i<=m; ++i) + + { + + f >> a >> b; + + graph[a].push_back(b); + + } + + for (int i=1; i<=n; ++i) + + { + + if (!ix[i]) + + solve(i); + + } + + g << ind_rez << '\n'; + + for (int i=0; i + + +using namespace std; + + + +ifstream f ("ctc.in"); + +ofstream g("ctc.out"); + + + +int N,M; + +const int NMAX = 100005; + + + + + + + +vector adj[NMAX], con, idx, lowlink, in_stack; + + + +vector < vector > Solution; + + + +stack S; + + + +int indecs; + + + +void read(vector * adj ) + +{ + + int x, y; + + f>>N>>M; + + for(int i=0;i> x >> y, + + adj[x - 1].push_back(y - 1); + + + +} + + + +void print(const vector < vector >& G) + +{ + + g << G.size() << "\n"; + + for (int i = 0; i < G.size(); ++ i) + + { + + for (int j = 0; j < G[i].size(); ++ j) + + g << G[i][j] + 1 << " "; + + g << "\n"; + + } + + + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n); + + in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) + + { + + if (idx[*it] == -1) + + { + + tarjan(*it, adj); + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(); + + in_stack[node] = 0; + + } + + while (node != n); + + Solution.push_back(con); + + } + +} + + + +int main(void) + +{ + + read(adj); + + idx.resize(N), lowlink.resize(N), in_stack.resize(N); + + idx.assign(N, -1), in_stack.assign(N, 0); + + + + for (int i = 0; i < N; ++ i) + + if (idx[i] == -1) + + tarjan(i, adj); + + + + print(Solution); + + + + return 0; + +} diff --git a/scc/tarjan/2d31df83-ffd2-4f28-833d-55b3e9a0d9bc.cpp b/scc/tarjan/2d31df83-ffd2-4f28-833d-55b3e9a0d9bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a6540fc9cd41983d5341ae03ed68247178b73f8 --- /dev/null +++ b/scc/tarjan/2d31df83-ffd2-4f28-833d-55b3e9a0d9bc.cpp @@ -0,0 +1,178 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector> rez; + +stack s; + + + +void CTC(int nod, int low[], int disc[],int onS[], vector graf[]) + +{ + + static int i=1; + + disc[nod]=i; + + low[nod]=i; + + i++; + + onS[nod]=1; + + s.push(nod); + + + + for(int it : graf[nod]) + + { + + if(disc[it] == 0) + + { + + CTC(it, low, disc,onS,graf); + + low[nod]=min(low[nod],low[it]); + + } + + else if(onS[it] == 1) + + { + + low[nod]=min(low[nod],disc[it]); + + } + + } + + + + if(low[nod] == disc[nod]) + + { + + vector v; + + while(s.top()!=nod) + + { + + int x=s.top(); + + cout<>n>>m; + + vector graf[n+1]; + + int low[n+1]= {0}; + + int disc[n+1]= {0}; + + for(auto i: disc) cout<>x>>y; + + graf[x].push_back(y); + + + + } + + + + for(int i=1; i<=n; i++) + + { + + if(disc[i] == 0) + + CTC(i,low,disc,onS,graf); + + } + + + + fout< +#include +#include + + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + +nivel++; +niv[nod] = nivel; +low[nod] = nivel; +s.push(nod); +fr[nod] = 1; + +for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + +if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); +} + +} + +int main() { + +in>>n>>m; +for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); +} + +for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + +out< +#include +#include +#include + +using namespace std; + +int n, m, i, j, k, index, ord[100005], niv_min[100005]; +vector vec[100005], comp[100005]; +stack st; +set s; + +void tarjan(int nod) { + int ind, x; + + ord[nod] = index; + niv_min[nod] = index; + index++; + st.push(nod); + s.insert(nod); + + for(ind = 0; ind < vec[nod].size(); ind++) + if(!ord[vec[nod][ind]]) { + tarjan(vec[nod][ind]); + niv_min[nod] = min(niv_min[nod], niv_min[vec[nod][ind]]); + } + else if(s.find(vec[nod][ind]) != s.end()) + niv_min[nod] = min(niv_min[nod], ord[vec[nod][ind]]); + + if(ord[nod] == niv_min[nod]) + while(!st.empty()) { + x = st.top(); + st.pop(); + s.erase(x); + comp[k].push_back(x); + if(x == nod) { + k++; + break; + } + } +} + +int main() { + ifstream f("ctc.in"); + ofstream g("ctc.out"); + + f >> n >> m; + while(m) { + f >> i >> j; + + vec[i].push_back(j); + m--; + } + f.close(); + + k = 0; + index = 1; + for(i = 1; i <= n; i++) + if(!ord[i]) + tarjan(i); + + g << k << '\n'; + for(i = 0; i < k; i++, g << '\n') + for(j = 0; j < comp[i].size(); j++) + g << comp[i][j] << ' '; + g.close(); + + return 0; +} diff --git a/scc/tarjan/30778bea-eb26-425a-b197-2fa54f6bc86d.cpp b/scc/tarjan/30778bea-eb26-425a-b197-2fa54f6bc86d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d10b4074f87d1d59af2a4989e5ebd4078dca15a3 --- /dev/null +++ b/scc/tarjan/30778bea-eb26-425a-b197-2fa54f6bc86d.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +using namespace std; + +int n,id=0,ids[100010],low[100010],ctc=0; + +ofstream o("ctc.out"); + +ifstream f("ctc.in"); + +vector v[100010],cc[100010]; + +stack S; + +bool onStack[100010],viz[100010]; + +void dfs(int k){ + + ids[k]=low[k]=++id; int i,nv; + + S.push(k); onStack[k]=1; + + viz[k]=true; + + for(nv=0;nv>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++) + + if(!viz[i]) dfs(i); + + + + o< +#include +#include +#include +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +const int NMAX = 100001; + +int N, M, cnt, level[NMAX], low[NMAX], cmp; +vector < int > G[NMAX], components[NMAX]; +bitset < NMAX > vis, InStack; +stack < int > Stack; + +void dfs(const int& node) { + + cnt++; + level[node] = cnt; + low[node] = cnt; + vis[node] = 1; + Stack.push(node); + InStack[node] = 1; + + for(const int& neighbor : G[node]) { + if(level[neighbor] == 0) { + dfs(neighbor); + low[node] = min(low[node], low[neighbor]); + }else if(InStack[neighbor]) { + low[node] = min(low[node], low[neighbor]); + } + } + + if(low[node] == level[node]) { + + cmp++; + + int x{}; + do { + x = Stack.top(); + Stack.pop(); + InStack[x] = 0; + components[cmp].push_back(x); + }while(x != node); + } +} + +int main() { + + f >> N >> M; + + while(M--) { + int u, v; + f >> u >> v; + + G[u].push_back(v); + } + + for(int i = 1;i <= N;++i) + if(!vis[i]) + dfs(i); + + g << cmp << '\n'; + for(int i = 1;i <= cmp;++i, g << '\n') + for(const int& it : components[i]) + g << it << ' '; + + return 0; +} diff --git a/scc/tarjan/32da8ee0-a1d4-4143-aefd-bd143de96a0c.cpp b/scc/tarjan/32da8ee0-a1d4-4143-aefd-bd143de96a0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2faefcc2ba8e8ba11ff73704970e5454459e44e8 --- /dev/null +++ b/scc/tarjan/32da8ee0-a1d4-4143-aefd-bd143de96a0c.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector graf[100005]; +vector ans[100005]; + +stack st; +int a[100005],b[100005]; +bool instack[100005]; +int t,ans1; +void tarjan(int x) +{ + t++; + a[x]=t; + b[x]=t; + st.push(x); + instack[x]=1; + for(auto k:graf[x]) + { + if(a[k]==0) + { + tarjan(k); + b[x]=min(b[x],b[k]); + } + else if(instack[k]) + b[x]=min(b[x],b[k]); + } + if(b[x]==a[x]) + { + while(!st.empty() && b[x]<=b[st.top()]) + { + ans[ans1].push_back(st.top()); + instack[st.top()]=0; + st.pop(); + } + ans1++; + } +} +int main() +{ + int n,m; + in>>n>>m; + for(int i=1;i<=m;i++) + { + int x,y; + in>>x>>y; + graf[x].push_back(y); + } + for(int i=1;i<=n;i++) + if(a[i]==0) + tarjan(i); + out< +using namespace std; + + + +class Task { + +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + vector adj[100010]; + vector> ctc; + vector found; + vector low_link; + vector in_stack; + stack st; + int n, m; + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + in_stack = vector(n + 1, 0); + found = vector(n + 1, -1); + low_link = vector(n + 1, 0); + int x, y; + for (int i = 0; i < m; ++i) { + fin >> x >> y; + adj[x].push_back(y); + } + } + + void get_result() { + int current_start = 0; + for (int i = 1; i <= n; ++i) { + if (found[i] == -1) { + tarjan(i, current_start); + } + } + } + + void tarjan(int node, int ¤t_start) { + found[node] = current_start; + low_link[node] = current_start; + in_stack[node] = 1; + st.push(node); + current_start = current_start + 1; + for (int neighbour : adj[node]) { + if (found[neighbour] == -1) { + tarjan(neighbour, current_start); + low_link[node] = min(low_link[node], low_link[neighbour]); + } else { + if (in_stack[neighbour] == 1) { + low_link[node] = min(low_link[node], found[neighbour]); + } + } + } + if (low_link[node] == found[node]) { + int current; + vector new_ctc; + while (!st.empty() && current != node) { + current = st.top(); + st.pop(); + new_ctc.push_back(current); + in_stack[current] = 0; + } + ctc.push_back(new_ctc); + } + } + + void print_output() { + ofstream fout("ctc.out"); + fout << ctc.size() << "\n"; + for (vector comp : ctc) { + for (int el : comp) { + fout << el << " "; + } + fout << "\n"; + } + } +}; + +int main() { + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/scc/tarjan/340c93d4-d5eb-4243-ae85-ff1c7e5553f2.cpp b/scc/tarjan/340c93d4-d5eb-4243-ae85-ff1c7e5553f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2054779e596ea7f72ab27f1eab2a220c914264fc --- /dev/null +++ b/scc/tarjan/340c93d4-d5eb-4243-ae85-ff1c7e5553f2.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,i,x,y,ctc,vecin,nod,nivel,niv[100010],low[100010]; + +bitset<100010> f,instack; + +vector L[100010],c[100010]; + +stack st; + +void dfs(int nod) { + + nivel++; + + f[nod]=1; + + low[nod]=niv[nod]=nivel; + + st.push(nod); + + instack[nod]=1; + + for (auto vecin:L[nod]) { + + if (f[vecin]==0) { + + dfs(vecin); + + low[nod]=min(low[nod],low[vecin]); + + } + + else + + if (instack[vecin]) + + low[nod]=min(low[nod],niv[vecin]); + + } + + if (low[nod]==niv[nod]) { + + ctc++; + + do { + + x=st.top(); + + st.pop(); + + instack[x]=0; + + c[ctc].push_back(x); + + } while (x!=nod); + + } + +} + +int main() { + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for (i=1;i<=n;i++) + + if (f[i]==0) + + dfs(i); + + fout< + + +using namespace std; + + + +const int N=100005; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,x,y; + +vector vecini[N]; + +int id[N],low[N],a[N],k,inq[N],s; + +int nrctc,nodes[N],ctcsize[N]; + + + +void dfs(int x) + +{ + + ++k; ++s; + + low[x]=id[x]=k; a[s]=x; inq[x]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) f>>x>>y, vecini[x].push_back(y); + + for(int i=1;i<=n;++i) if(!id[i]) + + { + + k=0; s=0; + + dfs(i); + + } + + g< +#include +#include + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + + nivel++; + niv[nod] = nivel; + low[nod] = nivel; + s.push(nod); + fr[nod] = 1; + + for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + + if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); + } + +} + +int main() { + + in>>n>>m; + for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); + } + + for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + + out< +#include +#include + + + + +using namespace std; + +int x,y; + +int nrcomp; + +stacks; + +const int MAXN=100001; + +vector < int > g[MAXN]; + +vectorctc[MAXN]; + +int viz[MAXN]; + +int iss[MAXN]; + +int nivel[MAXN],idk; + +int L[MAXN]; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +void tarjan(int x) { + + viz[x] = 1; + + iss[x] = 1; + + s.push(x); + + nivel[x] = ++idk; + + L[x] = nivel[x]; + + for ( auto y : g[x]) { + + if(!viz[y]) { + + tarjan(y); + + L[x] = min(L[x],L[y]); + +} + + else + + if(iss[y]) { + + L[x] = min(nivel[y],L[x]); + +} + + + + + +} + + if(nivel[x] == L[x]) { + + ++nrcomp; + +while(!s.empty()) { + + int y = s.top(); + + s.pop(); + + iss[y]=false; + + ctc[nrcomp].push_back(y); + + if(y == x) + + break; + +} + +} + +} + + + +int n,m; + +int main() { + + cin >> n >>m; + + for ( int i = 1; i <= m; ++i) { + + cin >> x >> y; + + g[x].push_back(y); + +} + + for ( int i = 1; i <= n; ++i) + + if(!viz[i]) + + tarjan(i); + + + + cout << nrcomp <<"\n"; + +for (int i = 1; i <=nrcomp; ++i) { + + for ( auto y : ctc[i]) + + cout << y <<" "; + +cout << "\n"; + +} + + + +} diff --git a/scc/tarjan/379487e0-c843-4ce3-9d07-866ea3315e86.cpp b/scc/tarjan/379487e0-c843-4ce3-9d07-866ea3315e86.cpp new file mode 100644 index 0000000000000000000000000000000000000000..65ae017194124ff5a6a88fe5ff5e8bff0c1c1895 --- /dev/null +++ b/scc/tarjan/379487e0-c843-4ce3-9d07-866ea3315e86.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, k = 0; + + + +vector v[100005]; + + + +vector > sol; + + + +vector temp; + + + +int idx[100005], llink[100005]; + + + +bool inst[100005]; + + + +stack st; + + + +void tarjan(int node) + +{ + + idx[node] = llink[node] = ++k; + + st.push(node); + + inst[node] = true; + + + + for(int i = 0; i < v[node].size(); i++) + + if(idx[v[node][i]] == 0) + + { + + tarjan(v[node][i]); + + llink[node] = min(llink[node], llink[v[node][i]]); + + } + + else + + if(inst[v[node][i]]) + + llink[node] = min(llink[node], llink[v[node][i]]); + + + + if(idx[node] == llink[node]) + + { + + temp.clear(); + + + + int naux = 0; + + + + while(naux != node) + + { + + naux = st.top(); + + + + temp.push_back(naux); + + + + st.pop(); + + + + inst[naux] = false; + + } + + + + sol.push_back(temp); + + } + + + +} + + + +int main() + +{ + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + + + fin >> x >> y; + + + + v[x].push_back(y); + + } + + + + for(int i = 1; i <= n; i++) + + if(idx[i] == 0) + + tarjan(i); + + + + fout << sol.size() << '\n'; + + + + for(int i = 0; i < sol.size(); i++) + + { + + for(int j = 0; j < sol[i].size(); j++) + + fout << sol[i][j] << " "; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/3b3191a5-b5cc-4f07-a2f3-ba20321602b8.cpp b/scc/tarjan/3b3191a5-b5cc-4f07-a2f3-ba20321602b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3ca5d03a6723eb62d91baa4652d1710ba74c750 --- /dev/null +++ b/scc/tarjan/3b3191a5-b5cc-4f07-a2f3-ba20321602b8.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + stack &post_order_stack; + vector &in_stack; + + int current_time; + vector &found_time; + vector &lowlink; + + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + vector visited(n, false); + stack post_order_stack; + vector in_stack(n, false); + SCCs.resize(n); + vector found_time(n, -1); + vector lowlink(n, -1); + + traversal_data td = {G, visited, post_order_stack, in_stack, 0, found_time, lowlink, 0, SCCs}; + for (int i = 0; i < n; ++i) { + if (not visited[i]) get_scc(i, td); + } + + write_solution(td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = true; + td.found_time[current_node] = td.current_time++; + td.lowlink[current_node] = td.found_time[current_node]; + td.post_order_stack.push(current_node); + td.in_stack[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) { + get_scc(neighbour, td); + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } else if (td.in_stack[neighbour]) { + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.found_time[neighbour] + ); + } + } + + if (td.found_time[current_node] == td.lowlink[current_node]) { + int top_node; + do { + top_node = td.post_order_stack.top(); + td.post_order_stack.pop(); + + td.SCCs[td.SCC_index].push_back(top_node); + td.in_stack[top_node] = false; + } while (top_node != current_node); + td.SCC_index++; + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/tarjan/3b763bb8-3f96-4684-98bb-c65602e5d318.cpp b/scc/tarjan/3b763bb8-3f96-4684-98bb-c65602e5d318.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cad53c024d285af839d41860c8b2474338509911 --- /dev/null +++ b/scc/tarjan/3b763bb8-3f96-4684-98bb-c65602e5d318.cpp @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +class Graph { + + int vertices; + + list *adj; + + + + vector > ans; + + + +public: + + Graph(int v) : vertices(v) { + + adj = new list [v]; + + } + + + + void addEdge(int from, int to) { + + adj[from].push_back(to); + + } + + + + void depthFirst(int i, vector &disc, vector &low, stack &st, vector &stackMember) { + + static int id = 0; + + + + disc[i] = low[i] = ++id; + + st.push(i); + + stackMember[i] = true; + + + + for (auto &adjVal : adj[i]) { + + if (!disc[adjVal]) { + + depthFirst(adjVal, disc, low, st, stackMember); + + + + low[i] = min(low[i], low[adjVal]); + + } + + + + else + + if (stackMember[adjVal]) + + low[i] = min(low[i], disc[adjVal]); + + } + + + + if (low[i] == disc[i]) { + + vector newAns; + + + + while (st.top() != i) { + + int topVal = st.top(); + + st.pop(); + + stackMember[topVal] = false; + + newAns.push_back(topVal); + + } + + + + int topVal = st.top(); + + st.pop(); + + stackMember[topVal] = false; + + newAns.push_back(topVal); + + + + ans.push_back(newAns); + + } + + } + + + + void findSCC() { + + vector disc(vertices), low(vertices); + + vector stackMember(vertices); + + stack st; + + + + for (int i = 1; i < vertices; ++i) { + + if (!disc[i]) + + depthFirst(i, disc, low, st, stackMember); + + } + + } + + + + void printData() { + + out << ans.size() << "\n"; + + + + for (size_t j = 0; j < ans.size(); ++j) { + + sort(ans[j].begin(), ans[j].end()); + + for (size_t i = 0; i < ans[j].size(); ++i) { + + out << ans[j][i]; + + if (i + 1 != ans[j].size()) + + out << " "; + + } + + if (j + 1 != ans.size()) + + out << "\n"; + + } + + } + + + +}; + + + +int main() { + + + + int N, M; + + + + in >> N >> M; + + + + Graph *myGraph = new Graph(N + 1); + + + + while (M--) { + + int from, to; + + in >> from >> to; + + myGraph -> addEdge(from, to); + + } + + + + myGraph -> findSCC(); + + + + myGraph -> printData(); + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/scc/tarjan/3bfb3172-b983-4ae6-abf7-41444b77605b.cpp b/scc/tarjan/3bfb3172-b983-4ae6-abf7-41444b77605b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d12c5c0f3b79562d9e3706cd0705e7674ce688d4 --- /dev/null +++ b/scc/tarjan/3bfb3172-b983-4ae6-abf7-41444b77605b.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +const string fileName="ctc"; + +ifstream in (fileName+".in"); + +ofstream out(fileName+".out"); + +const int nmax=1e5; + +int n, m; + +vector muchii[nmax+2]; + +int low[nmax+2]; + +int x, y; + +bool onStack[nmax+2]; + +int tin[nmax+2]; + +vector > comp; + +vector stk; + +int mom; + +int partComp[nmax+2]; + +void dfs(int nod){ + + stk.push_back(nod); + + onStack[nod]=true; + + low[nod]=tin[nod]=++mom; + + for(auto &x:muchii[nod]){ + + if(onStack[x]){ + + low[nod]=min(low[nod], tin[x]); + + } + + else if(tin[x]==0){ + + dfs(x); + + low[nod]=min(low[nod], low[x]); + + } + + } + + if(low[nod]>=tin[nod]){ + + comp.push_back(vector()); + + while(!stk.empty()&&stk.back()!=nod){ + + comp.back().push_back(stk.back()); + + partComp[stk.back()]=comp.size(); + + onStack[stk.back()]=false; + + stk.pop_back(); + + } + + comp.back().push_back(nod); + + partComp[stk.back()]=comp.size(); + + onStack[stk.back()]=false; + + stk.pop_back(); + + } + +} + +ostream & operator << (ostream & out, pair & per){ + + out< + +ostream & operator << (ostream & out, vector & vec){ + + for(auto &x:vec) + + out< > sol; + +int main() + +{ + + in>>n>>m; + + for(int i=1; i<=m; i++){ + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(tin[i]==0) + + dfs(i); + + + + out< +#include +#include +#include +using namespace std; + +const int DIM = 100005; + +int low[DIM], lev[DIM]; +bool vis[DIM], ins[DIM]; +vector stk, edg[DIM]; +vector> ctc; + +void dfs(int x) { + static int ctr = 0; + low[x] = lev[x] = ++ctr; + vis[x] = ins[x] = true; + stk.push_back(x); + + for (int y : edg[x]) { + if (!vis[y]) + dfs(y); + if (ins[y]) + low[x] = min(low[x], low[y]); + } + + if (low[x] == lev[x]) { + ctc.push_back(vector()); + int y; + do { + y = stk.back(); + ins[y] = false; stk.pop_back(); + ctc.back().push_back(y); + } while (y != x); + } +} + +int main(void) { + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + int n, m; + cin >> n >> m; + for (int i = 1; i <= m; ++i) { + int x, y; + cin >> x >> y; + edg[x].push_back(y); + } + for (int i = 1; i <= n; ++i) { + if (!vis[i]) + dfs(i); + } + cout << ctc.size() << "\n"; + for (auto ct : ctc) { + for (int x : ct) + cout << x << " "; + cout << "\n"; + } + return 0; +} diff --git a/scc/tarjan/3cf06241-0900-4897-823f-f6231c480173.cpp b/scc/tarjan/3cf06241-0900-4897-823f-f6231c480173.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa880f04d6ab5d055ac17cf5281d720e13a75e5f --- /dev/null +++ b/scc/tarjan/3cf06241-0900-4897-823f-f6231c480173.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct node{ + int index; + int lowlink; + bool onStack; + node(int i, int l){ + this->index = i; + this->lowlink = l; + this->onStack = false; + } +}; + +vector neighbours[100000]; +ifstream f("ctc.in"); +ofstream g("ctc.out"); +vector nods; +int n, m; +int index; +stack S; +vector> output; + +void con(int z){ + nods[z].index = index; + nods[z].lowlink = index; + index++; + S.push(z); + nods[z].onStack = true; + for (auto it : neighbours[z]) { + if(nods[it].index == INT_MIN){ + con(it); + nods[z].lowlink = min(nods[z].lowlink, nods[it].lowlink); + }else if(nods[it].onStack){ + nods[z].lowlink = min(nods[z].lowlink, nods[it].index); + } + } + if(nods[z].lowlink == nods[z].index){ + vector comp; + int w; + do{ + w = S.top(); + S.pop(); + nods[w].onStack = false; + comp.push_back(w + 1); + } while (w != z); + output.push_back(comp); + } +} + +int main() { + f>>n>>m; + for (int i = 0; i < n; ++i) { + nods.emplace_back(INT_MIN, INT_MAX); + } + for (int i = 0; i < m; ++i) { + int x, y; + f>>x>>y; + x--, y--; + neighbours[x].push_back(y); + } + for(int i = 0; i < n; ++i){ + if(nods[i].index == INT_MIN){ + con(i); + } + } + g< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, i, nrc, h[100005], low[100005], x, inaltime; + +bitset<100005> viz, eInStiva; + +vectorL[100005]; + +vectorc[100005]; + +stackst; + + + +void citireGraf(){ + + int i, x, y; + + fin >> n >> m; + + for(i = 1; i <= m; i++){ + + fin >> x >> y; + + L[x].push_back(y); + + } + +} + + + +void dfs(int x) { + + int z; + + inaltime++; + + viz[x] = 1; + + low[x] = h[x] = inaltime; + + + + st.push(x); + + eInStiva[x] = 1; + + + + for(auto y : L[x]) { + + if(viz[y] == 0){ + + dfs(y); + + low[x] = min(low[x], low[y]); + + } + + else if(eInStiva[y]) + + low[x] = min(low[x], h[y]); + + } + + + + + + if (low[x] == h[x]){ + + nrc++; + + do{ + + z = st.top(); + + st.pop(); + + eInStiva[z] = 0; + + c[nrc].push_back(z); + + }while(z!=x); + + } + +} + + + + + +int main() { + + + + citireGraf(); + + for(i = 1; i <= n; i++) + + if(viz[i] == 0) + + dfs(i); + + + + + + fout<< nrc << '\n'; + + for(i = 1; i <= nrc; i++){ + + for (auto x : c[i]) + + fout << x <<' '; + + fout <<'\n'; + + } + + + + fin.close(); fout.close(); + + return 0; + +} diff --git a/scc/tarjan/3e0a0d8f-6c78-4b96-9db1-e45ea68239c8.cpp b/scc/tarjan/3e0a0d8f-6c78-4b96-9db1-e45ea68239c8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cd92c47726538639279df4f1df49bc4c54d2a06 --- /dev/null +++ b/scc/tarjan/3e0a0d8f-6c78-4b96-9db1-e45ea68239c8.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vectoradj[100002]; + +vectorstiva; + +vectorscc[100002]; + +int nrc,disc[100002],low[100002],cc[100002],t; + + + +void dfs(int n1) + +{ + + disc[n1]=low[n1]=++t; + + stiva.push_back(n1); + + for(vector::iterator it=adj[n1].begin();it!=adj[n1].end();it++) + + { + + int n2=*it; + + if(!cc[n2]) + + { + + if(!disc[n2]) + + dfs(n2); + + low[n1]=min(low[n1],low[n2]); + + } + + } + + if(disc[n1]==low[n1]) + + { + + nrc++; + + while(stiva.back()!=n1) + + { + + scc[nrc].push_back(stiva.back()); + + cc[stiva.back()]=nrc; + + stiva.pop_back(); + + } + + scc[nrc].push_back(n1); + + cc[n1]=nrc; + + stiva.pop_back(); + + } + +} + + + +int main() + +{ + + int n,m,i,x,y; + + cin>>n>>m; + + for(i=1; i<=m; i++) + + { + + cin>>x>>y; + + adj[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(!disc[i]) + + dfs(i); + + } + + cout<::iterator it=scc[i].begin();it!=scc[i].end();it++) + + cout<<*it<<' '; + + cout<<'\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/3f954216-2cc4-450f-88b6-1669c591f650.cpp b/scc/tarjan/3f954216-2cc4-450f-88b6-1669c591f650.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80260f9a3bc575ec41b81601c03ad71debf7f801 --- /dev/null +++ b/scc/tarjan/3f954216-2cc4-450f-88b6-1669c591f650.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m; +vector> ad; + +int id; +vector ind, low; + +stack st; +vector onStack; +vector> sccs; + +void dfs(int node) { + st.push(node); onStack[node] = true; + ind[node] = low[node] = ++id; + for (int nghb : ad[node]) + if (!ind[nghb]) { + dfs(nghb); + low[node] = min(low[node], low[nghb]); + } + else if (onStack[nghb]) + low[node] = min(low[node], low[nghb]); + + if (ind[node] == low[node]) { + sccs.emplace_back(); + while (true) { + int top = st.top(); + st.pop(); onStack[top] = false; + low[top] = ind[node]; + sccs.back().push_back(top); + if (top == node) + break; + } + } +} + +int main() { + fin >> n >> m; + ad.resize(n + 1); + for (int i = 0; i < m; i++) { + int x, y; fin >> x >> y; + ad[x].push_back(y); + } + + ind.resize(n + 1); + low.resize(n + 1); + onStack.resize(n + 1); + for (int i = 1; i <= n; i++) + if (!ind[i]) + dfs(i); + + fout << sccs.size() << '\n'; + for (auto& scc : sccs) { + for (int node : scc) + fout << node << ' '; + fout << '\n'; + } + + fout.close(); + return 0; +} diff --git a/scc/tarjan/3feb4cf3-b7a5-493f-9489-a1b388055930.cpp b/scc/tarjan/3feb4cf3-b7a5-493f-9489-a1b388055930.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80260f9a3bc575ec41b81601c03ad71debf7f801 --- /dev/null +++ b/scc/tarjan/3feb4cf3-b7a5-493f-9489-a1b388055930.cpp @@ -0,0 +1,65 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m; +vector> ad; + +int id; +vector ind, low; + +stack st; +vector onStack; +vector> sccs; + +void dfs(int node) { + st.push(node); onStack[node] = true; + ind[node] = low[node] = ++id; + for (int nghb : ad[node]) + if (!ind[nghb]) { + dfs(nghb); + low[node] = min(low[node], low[nghb]); + } + else if (onStack[nghb]) + low[node] = min(low[node], low[nghb]); + + if (ind[node] == low[node]) { + sccs.emplace_back(); + while (true) { + int top = st.top(); + st.pop(); onStack[top] = false; + low[top] = ind[node]; + sccs.back().push_back(top); + if (top == node) + break; + } + } +} + +int main() { + fin >> n >> m; + ad.resize(n + 1); + for (int i = 0; i < m; i++) { + int x, y; fin >> x >> y; + ad[x].push_back(y); + } + + ind.resize(n + 1); + low.resize(n + 1); + onStack.resize(n + 1); + for (int i = 1; i <= n; i++) + if (!ind[i]) + dfs(i); + + fout << sccs.size() << '\n'; + for (auto& scc : sccs) { + for (int node : scc) + fout << node << ' '; + fout << '\n'; + } + + fout.close(); + return 0; +} diff --git a/scc/tarjan/4022abc7-07aa-41ce-91b7-78225a637ea9.cpp b/scc/tarjan/4022abc7-07aa-41ce-91b7-78225a637ea9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6bf594a957c2717620ebd46847ba236f3f5feca --- /dev/null +++ b/scc/tarjan/4022abc7-07aa-41ce-91b7-78225a637ea9.cpp @@ -0,0 +1,150 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int noduri, muchii, componente_tari, a, b, ind, ids[100005]; + +bool instack[100005], visited[100005]; + +int lowkey[100005]; + +vector mat[100005]; + +vector componente[100005]; + +stack s; + + + +void dfs(int node) + +{ + + s.push(node); + + instack[node] = 1; + + ids[node] = ind; + + lowkey[node] = ind; + + visited[node] = 1; + + ++ind; + + for(int i = 0; i < mat[node].size(); ++i) + + { + + int next_node = mat[node][i]; + + if(!visited[next_node]) + + { + + dfs(next_node); + + } + + if(instack[next_node]) + + { + + lowkey[node] = min(lowkey[node], lowkey[next_node]); + + } + + } + + if(ids[node] == lowkey[node]) + + { + + while(!s.empty()) + + { + + int next = s.top(); + + componente[componente_tari].push_back(next); + + s.pop(); + + instack[next] = 0; + + lowkey[next] = ids[node]; + + if(node == next) break; + + } + + componente_tari++; + + } + +} + + + +int main() + +{ + + fin >> noduri >> muchii; + + for(int i = 1; i <= muchii; ++i) + + { + + fin >> a >> b; + + mat[a].push_back(b); + + } + + for(int node = 1; node <= noduri; ++node) + + { + + if(!visited[node]) + + { + + dfs(node); + + } + + } + + fout << componente_tari << "\n"; + + for(int i = 0; i < componente_tari; ++i) + + { + + for(int j = 0; j < componente[i].size(); ++j) + + { + + fout << componente[i][j] << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/40a217f6-bb90-4e66-a4e7-b9125b95d6d8.cpp b/scc/tarjan/40a217f6-bb90-4e66-a4e7-b9125b95d6d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5a432d8072c6ae43a1e83a9ca9ee14d3a4adccb3 --- /dev/null +++ b/scc/tarjan/40a217f6-bb90-4e66-a4e7-b9125b95d6d8.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vector adj[100005], con, idx, lowlink, in_stack; + +vector < vector > C; + +stack S; + +int index; + + + +void read_in(vector * adj, int& n) + +{ + + int cnt_edges, x, y; + + + + cin >> n; + + for (cin >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + cin >> x >> y, + + adj[x - 1].push_back(y - 1); + + cin.close(); + +} + + + +void print_out() + +{ + + cout << C.size() << "\n"; + + for (size_t i = 0; i < C.size(); ++ i) + + { + + for (size_t j = 0; j < C[i].size(); ++ j) + + cout << C[i][j] + 1 << " "; + + cout << "\n"; + + } + + cout.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = index; + + index ++; + + S.push(n), in_stack[n] = 1; + + + + + + for (int i = 0; i < adj[n].size(); i++) + + { + + int it = adj[n][i]; + + + + if (idx[it] == -1) + + tarjan(it, adj), + + lowlink[n] = min(lowlink[n], lowlink[it]); + + + + + + else if (in_stack[it]) + + lowlink[n] = min(lowlink[n], lowlink[it]); + + } + + + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do + + { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) + + if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(); + + + + return 0; + +} diff --git a/scc/tarjan/41018298-adfb-497f-ac7f-0ad6f333ebd8.cpp b/scc/tarjan/41018298-adfb-497f-ac7f-0ad6f333ebd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9bb8d68d175b97dfbfa9fa342c130b6d0b49eca --- /dev/null +++ b/scc/tarjan/41018298-adfb-497f-ac7f-0ad6f333ebd8.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int oo=INT_MAX/2; + +typedef vector vi; + +int n,m,k; +vector g; + +vi lowlink, idx, inStk; +vi stk; + +vector ctcs; + +int _index=1; +void tarjan(int nd){ + lowlink[nd]=idx[nd]=_index++; + inStk[nd]=1; + stk.push_back(nd); + + for(auto vc:g[nd]){ + if(idx[vc]==0){ + tarjan(vc); + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + else if(inStk[vc]){ + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + } + if(lowlink[nd]==idx[nd]){ + + ctcs.push_back(vi()); + int aux; + do { + ctcs.back().push_back(aux=stk.back()); + inStk[stk.back()]=0; + stk.pop_back(); + } + while(aux!=nd); + } +} + +int main(){ + fin>>n>>m; + g.resize(n+1), lowlink.resize(n+1), idx.resize(n+1,0), inStk.resize(n+1,0); + for(int i=m;i--;){ + int x,y; fin>>x>>y; + g[x].push_back(y); + } + for(int i=1;i<=n;++i) + if(idx[i]==0) tarjan(i); + fout< + + +using namespace std; + + + +int n, m; + +vector> edges; + +vector> scc; + +int nrScc = -1, currentIndex = 0; + +vector lowlink; + +vector onStack; + +vector idx; + +stack st; + +vector comp; + + + +void read() { + + cin >> n >> m; + + edges.resize(n); + + for(int i=0; i> u >> v; + + edges[u-1].push_back(v-1); + + } + +} + + + +void dfs(int v) { + + lowlink[v] = idx[v] = currentIndex++; + + onStack[v] = true; + + st.push(v); + + for(int u : edges[v]) { + + if(idx[u] == -1) { + + dfs(u); + + lowlink[v] = min(lowlink[v], lowlink[u]); + + } else if(onStack[u] == true) + + lowlink[v] = min(lowlink[v], idx[u]); + + } + + + + if(lowlink[v] == idx[v]) { + + comp.clear(); + + int node; + + do { + + node = st.top(); + + comp.push_back(node); + + onStack[node] = false; + + st.pop(); + + } while(node!=v); + + scc.push_back(comp); + + } + +} + + + +void tarjan() { + + for(int i=0; i<(int)edges.size(); i++) + + if(idx[i] == -1) + + dfs(i); + +} + + + +void show() { + + for(int v=0; v +#include +#include +#include +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +int n, m, x, y, compAct, mom; + +vector < vector > muchii, componente; + +vector disc, low, componentaTata; + +stack stk; + +vector realVal; + +void Tarjan(int nod) + +{ + + low[nod]=disc[nod] = ++mom; + + stk.push(nod); + + for(auto & x : muchii[nod]) + + { + + if(disc[x]==-1) + + { + + Tarjan(x); + + low[nod]=min(low[nod], low[x]); + + } + + else + + { + + low[nod]=min(low[nod], disc[x]); + + } + + } + + + + if(low[nod]==disc[nod]) + + { + + componente.push_back( vector()); + + while(!stk.empty()&&stk.top()!=nod) + + { + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + +} + + + +int main() + +{ + + in>>n>>m; + + muchii.resize(n+1); componentaTata.resize(n+1, -1); + + disc.resize(n+1, -1); low.resize(n+1, -1); realVal.resize(n+1, 0); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(disc[i]==-1) + + mom=0, Tarjan(i); + + + + out< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vector adj[100005], con, idx, lowlink, in_stack; + +vector < vector > C; + +stack S; + +int index; + + + +void read_in(vector * adj, int& n) + +{ + + int cnt_edges, x, y; + + + + cin >> n; + + for (cin >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + cin >> x >> y, + + adj[x - 1].push_back(y - 1); + + cin.close(); + +} + + + +void print_out() + +{ + + cout << C.size() << "\n"; + + for (size_t i = 0; i < C.size(); ++ i) + + { + + for (size_t j = 0; j < C[i].size(); ++ j) + + cout << C[i][j] + 1 << " "; + + cout << "\n"; + + } + + cout.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = index; + + index ++; + + S.push(n), in_stack[n] = 1; + + + + + + for (int i = 0; i < adj[n].size(); i++) + + { + + int it = adj[n][i]; + + + + if (idx[it] == -1) + + tarjan(it, adj), + + lowlink[n] = min(lowlink[n], lowlink[it]); + + + + + + else if (in_stack[it]) + + lowlink[n] = min(lowlink[n], lowlink[it]); + + } + + + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do + + { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) + + if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(); + + + + return 0; + +} diff --git a/scc/tarjan/424d1452-ed29-40e4-b2c5-ac5fade7c2a6.cpp b/scc/tarjan/424d1452-ed29-40e4-b2c5-ac5fade7c2a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47c582616aae01a4924ff38de4dac2f6dd03c541 --- /dev/null +++ b/scc/tarjan/424d1452-ed29-40e4-b2c5-ac5fade7c2a6.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +vector graf[100005]; + +int low[100005],id[100005]; + +bool onstack[100005]; + +vector> sol; + +stack s; + +int n,m,a,b; + +int idc = 0; + +void dfs(int x) + +{ + + id[x] = idc; + + low[x] = idc++; + + s.push(x); + + onstack[x] = 1; + + for(auto it:graf[x]){ + + if(id[it]==-1) + + dfs(it); + + if(onstack[it]) + + low[x] = min(low[x],low[it]); + + } + + if(id[x] == low[x]){ + + int node; + + vector aux; + + do{ + + node = s.top(); + + aux.push_back(node); + + low[node] = id[x]; + + onstack[node] = 0; + + s.pop(); + + }while(node != x); + + sol.push_back(aux); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=0;i>a>>b; + + graf[a-1].emplace_back(b-1); + + } + + for(int i=0;i + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,st[100100],idx[100100],low[100100],top,c,ap[100100],t; + +vector v[100100]; + +vector comp[100100]; + +void tarjan(int nod) + +{ + + idx[nod]=t; + + low[nod]=t; + + t++; + + st[++top]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + { + + if(idx[it]==-1) { + + tarjan(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + } + + if(low[nod]==idx[nod]) + + { + + c++; + + do + + { + + x=st[top--]; + + ap[x]=0; + + comp[c].push_back(x); + + }while(x!=nod); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) idx[i]=-1; + + for(int i=1;i<=n;i++) + + { + + if(idx[i]==-1) + + { + + t=0; + + top=0; + + tarjan(i); + + } + + } + + out< +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100001], tc[100001]; + +stack stiva; + +bool inStiva[100001]; + +int n, ctc, id, viz[100001], leg[100001]; + + + +void citire() + +{ + + int m; + + f>>n>>m; + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + f>>x>>y; + + v[x].push_back(y); + + } + +} + + + +void dfs(int nod) + +{ + + stiva.push(nod); + + inStiva[nod] = 1; + + viz[nod] = leg[nod] = ++id; + + for(int i = 0; i < v[nod].size(); i++) + + { + + if(!viz[v[nod][i]]) + + { + + dfs(v[nod][i]); + + leg[nod] = min(leg[nod], leg[v [nod][i]]); + + } + + else if(inStiva[v[nod][i]]) + + leg[nod] = min(leg[nod], leg[v[nod][i]]); + + } + + if(leg[nod] == viz[nod]) + + { + + ctc++; + + while(true) + + { + + tc[ctc].push_back(stiva.top()); + + inStiva[stiva.top()] = 0; + + if(stiva.top() == nod) + + { + + stiva.pop(); + + break; + + } + + stiva.pop(); + + } + + } + +} + + + +int main() + +{ + + citire(); + + for(int i = 1; i <= n; i++) + + if(!viz[i]) + + dfs(i); + + g< +#include +#include + + + + + + +using namespace std; + + + +vector< vector> Componente; + +vectorgraph[100005]; + +vectorajut; + + + +stack S; + +int nr; + +int n, m, x, y; + +int low_link[100005]; + +int is_in_stack[100005]; + +int viz[100005]; + +int idx[100005]; + +int index = 1; + + + +void citire(){ + + ifstream f("ctc.in"); + + f>>n>>m; + + for(int i=1; i<=m; i++){ + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int i){ + + low_link[i] = idx[i] = index; + + index ++; + + + + viz[i] = 1; + + is_in_stack[i] = 1; + + S.push(i); + + + + for(auto &v:graph[i]){ + + if(viz[v] == 0){ + + tarjan(v); + + low_link[i] = min(low_link[i], low_link[v]); + + } + + else if(is_in_stack[v] == 1){ + + low_link[i] = min(low_link[i], low_link[v]); + + } + + } + + + + if(idx[i] == low_link[i]){ + + ajut.clear(); + + int el; + + do{ + + el = S.top(); + + ajut.push_back(el); + + S.pop(); + + is_in_stack[el] = 0; + + }while(el!=i); + + Componente.push_back(ajut); + + } + +} + +void parcurg_componente_conexe_graf(){ + + for(int i=1; i<=n; i++){ + + if(viz[i] == 0){ + + tarjan(i); + + } + + } + +} + + + +void afisare(){ + + ofstream g("ctc.out"); + + int m = Componente.size(); + + g< + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int indice,nr,inStiva[100100],nivel[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + nivel[nod]=indice; + + low[nod]=min(low[nod],nivel[nod]); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++)if(nivel[i]==0)nivel[i]=1,Tarjan(i); + + + + g< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N_MAX = 1e5 + 1; + + + + + +int N, M; + +vector> graph(N_MAX, vector()); + + + + + +vector> answer; + +int id[N_MAX], low[N_MAX], t; + +bool onStack[N_MAX]; + +stack node_stack; + + + +void dfs_scc(int node) + +{ + + ++t; + + id[node] = t; + + low[node] = t; + + onStack[node] = true; + + node_stack.push(node); + + + + for(auto& next : graph[node]) + + { + + if(id[next] == 0) + + dfs_scc(next); + + + + if(onStack[next]) + + low[node] = min(low[node], low[next]); + + } + + + + if(id[node] == low[node]) + + { + + answer.push_back(vector()); + + + + while(node_stack.top() != node) + + { + + answer.back().push_back(node_stack.top()); + + onStack[node_stack.top()] = false; + + node_stack.pop(); + + } + + + + answer.back().push_back(node_stack.top()); + + onStack[node_stack.top()] = false; + + node_stack.pop(); + + } + +} + + + +int main() + +{ + + fin >> N >> M; + + + + for(int x, y, i = 1; i <= M; ++i) + + { + + fin >> x >> y; + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= N; ++i) + + { + + if(id[i] == 0) + + { + + dfs_scc(i); + + } + + } + + + + + + fout << answer.size() << '\n'; + + + + for(auto& ctc : answer) + + { + + for(auto& node : ctc) + + { + + fout << node << ' '; + + } + + + + fout << '\n'; + + } + +} diff --git a/scc/tarjan/466373fd-f1f9-4264-8d4f-6fcad37269ec.cpp b/scc/tarjan/466373fd-f1f9-4264-8d4f-6fcad37269ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da72aaa55c171c975c8a931ec7c83a8ad25f2b86 --- /dev/null +++ b/scc/tarjan/466373fd-f1f9-4264-8d4f-6fcad37269ec.cpp @@ -0,0 +1,258 @@ +#include +#include +#include + + + + +using namespace std; + + + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + + + +void Read( + + vector> &nodes + +) { + + uint32_t n; + + uint32_t m; + + in >> n; + + in >> m; + + + + nodes.resize(n); + + + + uint32_t node1; + + uint32_t node2; + + + + for (; m; --m) { + + in >> node1; + + in >> node2; + + + + --node1; + + --node2; + + + + nodes[node1].push_back(node2); + + } + +} + + + + + +inline void Add_SCC( + + vector> &SCCs, + + stack &stk, + + vector &on_stack, + + uint32_t const node + +) { + + SCCs.push_back(vector()); + + + + uint32_t top; + + + + do { + + top = stk.top(); + + stk.pop(); + + on_stack[top] = false; + + + + SCCs.back().push_back(top); + + } while (top != node); + +} + + + + + +inline void DFS( + + vector> const &nodes, + + vector &order, + + vector> &SCCs, + + uint32_t const node + +) { + + static uint32_t order_count; + + static vector low(nodes.size()); + + static stack stk; + + static vector on_stack(nodes.size()); + + + + ++order_count; + + order[node] = order_count; + + low[node] = order_count; + + + + stk.push(node); + + on_stack[node] = true; + + + + uint32_t neighbour; + + + + for (uint32_t i = 0; i < nodes[node].size(); ++i) { + + neighbour = nodes[node][i]; + + + + if (order[neighbour] == 0) { + + DFS(nodes, order, SCCs, neighbour); + + } + + + + if (on_stack[neighbour]) { + + low[node] = min(low[node], low[neighbour]); + + } + + } + + + + if (order[node] == low[node]) { + + Add_SCC(SCCs, stk, on_stack, node); + + } + +} + + + + + +void Solve( + + vector> const &nodes + +) { + + vector> SCCs; + + vector order(nodes.size()); + + + + for (uint32_t i = 0; i < nodes.size(); ++i) { + + if (order[i] == 0) { + + DFS(nodes, order, SCCs, i); + + } + + } + + + + out << SCCs.size() << '\n'; + + + + for (uint32_t j, i = 0; i < SCCs.size(); ++i) { + + for (j = 0; j < SCCs[i].size(); ++j) { + + out << SCCs[i][j] + 1 << ' '; + + } + + + + out << '\n'; + + } + +} + + + + + +int main() { + + vector> nodes; + + + + Read(nodes); + + + + Solve(nodes); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/scc/tarjan/47649b41-b614-4eb0-8822-99fdf912a726.cpp b/scc/tarjan/47649b41-b614-4eb0-8822-99fdf912a726.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5592d66996ded280bf2fae308ee50aa5363bc8ef --- /dev/null +++ b/scc/tarjan/47649b41-b614-4eb0-8822-99fdf912a726.cpp @@ -0,0 +1,144 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int N = 100041; + + + +int n, m; + +vector gra[N]; + +int idx[N], lol[N]; + +bool vi[N]; + + + +stack staketa; + +vector> answereta; + + + +int indexeta = 1; + + + +void defeseta(int a){ + + staketa.push(a); + + idx[a] = indexeta++; + + lol[a] = idx[a]; + + + + vi[a] = true; + + + + for(auto b : gra[a]){ + + if(idx[b] == 0){ + + defeseta(b); + + lol[a] = min(lol[a], lol[b]); + + }else if(vi[b]){ + + lol[a] = min(lol[a], idx[b]); + + } + + } + + + + if(lol[a] == idx[a]){ + + answereta.push_back({}); + + vector &baketa = answereta.back(); + + while(staketa.top() != a){ + + baketa.push_back(staketa.top()); + + vi[staketa.top()] = false; + + staketa.pop(); + + } + + baketa.push_back(staketa.top()); + + vi[staketa.top()] = false; + + staketa.pop(); + + } + +} + + + +int main(){ + + + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + int a, b; + + fin>> a >> b; + + gra[a].push_back(b); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(idx[i]==0)defeseta(i); + + } + + + + fout << answereta.size() << "\n"; + + for(auto v : answereta){ + + for(auto a : v){ + + fout << a << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/477008c2-3a26-4ace-b9e3-dcfe9a16530a.cpp b/scc/tarjan/477008c2-3a26-4ace-b9e3-dcfe9a16530a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5069ec124c7e1548fc398ce3438f6351a67023c2 --- /dev/null +++ b/scc/tarjan/477008c2-3a26-4ace-b9e3-dcfe9a16530a.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int NMAX = 100002; + +int n,m,ssc=0,id=1; +int ids[NMAX],low[NMAX]; +bool onStack[NMAX],visited[NMAX]; +vector g[NMAX],Stack,ans[NMAX]; +void dfs(int node) { + visited[node]=true; + ids[node]=low[node]=id++; + onStack[node]=true; + Stack.push_back(node); + for(int i=0; i>n>>m; + for(int i=1; i<=m; i++) { + int x,y; + fin>>x>>y; + g[x].push_back(y); + } + for(int i=1; i<=n; i++) + if(!visited[i]) + dfs(i); + + fout< +using namespace std; + + + + + +class Task { + + public: + + void solve() { + + read_input(); + + get_result(); + + print_output(); + + } + + + + private: + + vector adj[100009]; + + vector found; + + vector low_link; + + vector in_stack; + + vector> ctc; + + stack st; + + int n, m; + + + + void read_input() { + + ifstream fin("ctc.in"); + + fin >> n >> m; + + + + found = vector(n + 1, -1); + + low_link = vector(n + 1, 0); + + in_stack = vector(n + 1, 0); + + + + for (int i = 1; i <= m; ++i) { + + int x, y; + + fin >> x >> y; + + + + adj[x].push_back(y); + + + + } + + fin.close(); + + } + + + + void get_result() { + + int current_start = 0; + + + + for (int i = 1; i <= n; ++i) { + + if (found[i] == -1) { + + tarjan(i, current_start); + + } + + } + + } + + + + void tarjan(int node, int ¤t_start) { + + found[node] = current_start; + + low_link[node] = current_start; + + + + in_stack[node] = 1; + + st.push(node); + + + + ++current_start; + + + + for (int neighbour : adj[node]) { + + if (found[neighbour] == -1) { + + tarjan(neighbour, current_start); + + low_link[node] = min(low_link[node], low_link[neighbour]); + + } else { + + if (in_stack[neighbour] == 1) { + + low_link[node] = min(low_link[node], found[neighbour]); + + } + + } + + } + + + + if (low_link[node] == found[node]) { + + int current; + + vector new_ctc; + + + + while (!st.empty() && current != node) { + + current = st.top(); + + st.pop(); + + new_ctc.push_back(current); + + + + in_stack[current] = 0; + + } + + ctc.push_back(new_ctc); + + } + + } + + + + void print_output() { + + ofstream fout("ctc.out"); + + fout << ctc.size() << "\n"; + + for (vector comp : ctc) { + + for (int el : comp) { + + fout << el << " "; + + } + + fout << "\n"; + + } + + fout.close(); + + } + +}; + + + +int main() { + + Task *task = new Task(); + + task->solve(); + + delete task; + + + + return 0; + +} diff --git a/scc/tarjan/4918d031-d7a5-478c-8646-8131afa9fe17.cpp b/scc/tarjan/4918d031-d7a5-478c-8646-8131afa9fe17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74b25566c1d700cc3269266d27f3ef6721581a53 --- /dev/null +++ b/scc/tarjan/4918d031-d7a5-478c-8646-8131afa9fe17.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + + nivel++; + niv[nod] = nivel; + low[nod] = nivel; + s.push(nod); + fr[nod] = 1; + + for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + + if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); + } + +} + +int main() { + + in>>n>>m; + for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); + } + + for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + + out< + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + +int n, m, id[nmax], low[nmax], z; + +bool inStack[nmax]; + +vector G[nmax]; + +stack s; + +vector > ans; + +void tarjan(int nod){ + + id[nod] = low[nod] = ++z; + + s.push(nod); + + inStack[nod] = true; + + for (auto it : G[nod]){ + + if (id[it] == 0){ + + tarjan(it); + + low[nod] = min(low[nod], low[it]); + + } + + else{ + + if (inStack[it]){ + + low[nod] = min(low[nod], id[it]); + + } + + } + + } + + if (id[nod] == low[nod]){ + + vector aux; + + int snod; + + do{ + + snod = s.top(); + + inStack[snod] = false; + + s.pop(); + + aux.push_back(snod); + + } while (snod != nod); + + ans.push_back(aux); + + } + +} + + + +int main(){ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i){ + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i){ + + if (id[i] == 0){ + + tarjan(i); + + } + + } + + fout << ans.size() << "\n"; + + for (int i = 0; i < ans.size(); ++i){ + + for (int j = 0; j < ans[i].size(); ++j){ + + fout << ans[i][j] << " "; + + } + + fout << "\n"; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/499cf991-4177-467d-bb33-08cf42f4344f.cpp b/scc/tarjan/499cf991-4177-467d-bb33-08cf42f4344f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28838298314b1993b156ebca98c7bb992ad7b9b6 --- /dev/null +++ b/scc/tarjan/499cf991-4177-467d-bb33-08cf42f4344f.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream o("ctc.out"); + + + +int id=0,n,m,cc=0,low[100010],ids[100010]; + +bool onStack[100010]; + +vector v[100010],ctc[100010]; + +stack S; + +void citire(){ + + f>>n>>m; int i,x,y; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + +} + +void dfs(int k){ + + ids[k]=low[k]=++id; + + S.push(k); int i,nv; + + onStack[k]=1; + + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,k,x,y,NR; + +int nivel[100010],low[100010]; + +vector L[100010]; + +vector componente[100010]; + +stack stiva; + +bitset <100010> f; + + + +void dfs(int nod){ + + f[nod]=1; + + nivel[nod]=++k; + + low[nod]=k; + + stiva.push(nod); + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++){ + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(int i=1;i<=n;i++){ + + if(nivel[i]==0){ + + dfs(i); + + } + + } + + fout< +#include +#include +#include +#include + +std::vector > adj, sccList; +std::vector idx, lowlink, conex, onStack; +std::stack stack; +int atIDX = 0; + +void read(int &n) { + std::ifstream f("ctc.in"); + f >> n;; + int edges; + adj.resize(n); + f >> edges; + for (int i = 0; i < edges; i++) { + int x, y; + f >> x >> y; + adj[x-1].push_back(y-1); + } + f.close(); +} + +void print(const std::vector > sccList) { + std::ofstream g("ctc.out"); + g << sccList.size() << '\n'; + for (int i = 0; i < sccList.size(); i++) { + for (int j = 0; j < sccList[i].size(); j++) { + g << sccList[i][j] + 1 << " "; + } + g << '\n'; + } + g.close(); +} + +void tarjan(int x) { + idx[x] = lowlink[x] = atIDX; + stack.push(x); + onStack[x] = 1; + atIDX++; + for (int i = 0; i < adj[x].size(); i++) { + if (idx[adj[x][i]] == -1) { + tarjan(adj[x][i]); + lowlink[x] = std::min(lowlink[x], lowlink[adj[x][i]]); + } else if (onStack[adj[x][i]] == 1) { + lowlink[x] = std::min(lowlink[x], lowlink[adj[x][i]]); + } + } + + if (idx[x] == lowlink[x]) { + conex.clear(); + int node; + do { + conex.push_back(node = stack.top()); + stack.pop(); + onStack[node] = 0; + } while (node != x); + + sccList.push_back(conex); + } +} + +int main() { + int n; + read(n); + + + idx.resize(n); + lowlink.resize(n); + onStack.resize(n); + idx.assign(n, -1); + onStack.assign(n, 0); + + for (int i = 0; i < n; i++) { + if (idx[i] == -1) { + tarjan(i); + } + } + + print(sccList); + return 0; +} diff --git a/scc/tarjan/4d57fda4-dd30-4752-a762-6e675db36111.cpp b/scc/tarjan/4d57fda4-dd30-4752-a762-6e675db36111.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2af3ea385ce0855df9b6cd1e7215a4173896c5b6 --- /dev/null +++ b/scc/tarjan/4d57fda4-dd30-4752-a762-6e675db36111.cpp @@ -0,0 +1,83 @@ +#include +#include +#include + +const int NMAX = 1e5 + 1; + +std :: ifstream in ("ctc.in"); +std :: ofstream out("ctc.out"); + +std :: vector graf[NMAX]; +std :: vector> sol; +std :: stack stk; + +int low[NMAX], id[NMAX]; +bool inStack[NMAX]; + +int n, m, a, b; +int idc = 0; + +void DFS (int); + +int main() +{ + + in >> n >> m; + for( int i = 0; i < m; i++ ){ + in>>a>>b; + graf[a-1].emplace_back(b-1); + } + + for(int i = 0; i < n; i++ ) + { + low[i] = 0; + id[i] = -1; + inStack[i] = 0; + } + + for(int i = 0; i < n; i++) + if ( id[i] == -1) + DFS(i); + + + out << sol.size() << "\n"; + for (auto list : sol){ + for (int elem : list) + out << elem + 1 << " "; + out << "\n"; + } + return 0; +} + +void DFS(int x) +{ + id[x] = low[x] = idc; + idc += 1; + stk.push(x); + inStack[x] = 1; + + for (int elem : graf[x]){ + if(id[elem] == -1) + DFS(elem); + if(inStack[elem]) + low[x] = std :: min (low[x], low[elem]); + } + if(id[x] == low[x]){ + std :: vector aux; + + while (stk.top() != x){ + aux.push_back( stk.top() ); + low[ stk.top() ] = id[x]; + inStack[ stk.top() ] = 0; + stk.pop(); + } + + aux.push_back( stk.top() ); + low[ stk.top() ] = id[x]; + inStack[ stk.top() ] = 0; + stk.pop(); + + sol.push_back(aux); + } + +} diff --git a/scc/tarjan/4db3bfa3-37fc-4140-ba63-115797d4140a.cpp b/scc/tarjan/4db3bfa3-37fc-4140-ba63-115797d4140a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..764e7498985883ad4ea4c23a1ab454d6104226c8 --- /dev/null +++ b/scc/tarjan/4db3bfa3-37fc-4140-ba63-115797d4140a.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m, _index = 1; + +bool in_st[100100]; + +int idx[100100], lowlink[100100]; + +vector graf[100100], componenta; + +stack st; + +vector> CTC; + + + + + + + +void Read() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + graf[x].push_back(y); + + } + + f.close(); + +} + + + + + +void Tarjan(int i) + +{ + + idx[i] = _index; + + lowlink[i] = _index; + + _index++; + + st.push(i); + + in_st[i] = 1; + + + + for(int j=0;j +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +int nctc; + +int low[100005], disc[100005], _time; + +std::vector ctc[100005]; + +bool inStack[100005]; + +std::stack stack; + +void DFS(int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + + + for (auto &it:ADC[vertex]) { + + if (!disc[it]) { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + else if (inStack[it]) { + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (low[vertex] == disc[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + ++ nctc; + + } + +} + + + +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int main() + +{ + + input >> N >> M; + + for (int i=1, u, v; i<=M; ++i) input >> u >> v, addEdge(u, v); + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + output << nctc << '\n'; + + for (int i=0; i +#include +#include + + +using namespace std; + + + +vector> arcs; + +vector lowlink, index; + +vector onStack; + + + +vector> components; + + + +stack st; + + + +int currentComp; + + + +int min(int x, int y) { + + return x()); + + + + int n; + + do { + + n = st.top(); + + onStack[st.top()] = false; + + st.pop(); + + + + components[components.size() - 1].push_back(n); + + } while (n != x); + + } + +} + + + + + +int main() { + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + int n, m, x, y; + + in >> n >> m; + + + + arcs.resize(n+2); + + lowlink.resize(n+2, 0); + + index.resize(n+2, 0); + + onStack.resize(n+2, false); + + + + for(int i=0; i> x >> y; + + arcs[x].push_back(y); + + } + + + + for(int i=1; i<=n; ++i) + + if (index[i] == 0) + + strongConnect(i); + + + + out << components.size() << "\n"; + + for(int i=0; i + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n) + +{ + + int cnt_edges, x, y; + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + +} + + + +void print_out(const vector < vector >& G) + +{ + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main() + +{ + + int n; + + read_in(adj, n); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C); + + + + return 0; + +} diff --git a/scc/tarjan/5319785a-f643-4fc5-8ad4-ea1f61d0d7b2.cpp b/scc/tarjan/5319785a-f643-4fc5-8ad4-ea1f61d0d7b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ac040fb08551cd59a15f7a0f11c6184b3912d6f --- /dev/null +++ b/scc/tarjan/5319785a-f643-4fc5-8ad4-ea1f61d0d7b2.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +std::ifstream in("ctc" ".in"); + +std::ofstream out("ctc" ".out"); + +const int N = 100000; + +std::vector L[N], S; + +int LL[N], ID[N], nId; + +std::bitset E, pS; + +std::vector> CTC; + +void dfs(int t) + +{ + + pS[t] = true; + + ID[t] = LL[t] = nId++; + + S.push_back(t); + + for (int f: L[t]) + + if (not E[f]) + + { + + if (not pS[f]) + + dfs(f); + + LL[t] = std::min(LL[t], LL[f]); + + } + + if (ID[t] == LL[t]) + + { + + CTC.emplace_back(); + + int back; + + do + + { + + back = S.back(); + + S.pop_back(); + + CTC.back().push_back(back); + + E[back] = true; + + } + + while (back != t); + + } + +} + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + int a, b; + + in >> a >> b; + + L[--a].push_back(--b); + + } + + for (int i = 0; i < n; i++) + + if (not E[i]) + + dfs(i); + + out << CTC.size() << '\n'; + + for (auto ctc: CTC) + + { + + for (int i: ctc) + + out << i + 1 << ' '; + + out << '\n'; + + } + +} diff --git a/scc/tarjan/53dd814d-a995-47dd-bb13-7a6b69bb16e0.cpp b/scc/tarjan/53dd814d-a995-47dd-bb13-7a6b69bb16e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b44e1105253920ab05a1ce0a3a762b8859cb82d --- /dev/null +++ b/scc/tarjan/53dd814d-a995-47dd-bb13-7a6b69bb16e0.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include + + +using namespace std; +const int N_MAX = 1e5 + 1; + + + +struct TarjanVertex{ + + int index, tarjanIndex = -1, lowlink; + + bool onStack = false; + +}; + + + +vector graph[N_MAX]; + +stack stk; + +vector graphVertexes; + +vector > solutions; + + + +int nextAvailableIndex = 1; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void Tarjan(int nodeIndex) + +{ + + graphVertexes[nodeIndex].tarjanIndex = nextAvailableIndex; + + graphVertexes[nodeIndex].lowlink = nextAvailableIndex; + + nextAvailableIndex++; + + stk.push(nodeIndex); + + graphVertexes[nodeIndex].onStack = true; + + + + for(auto neighborIndex: graph[graphVertexes[nodeIndex].index]) + + { + + if(graphVertexes[neighborIndex].tarjanIndex == -1) + + { + + Tarjan(graphVertexes[neighborIndex].index); + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].lowlink, graphVertexes[nodeIndex].lowlink); + + } + + else if (graphVertexes[neighborIndex].onStack == true) + + { + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].tarjanIndex, graphVertexes[nodeIndex].lowlink); + + } + + } + + + + if(graphVertexes[nodeIndex].tarjanIndex == graphVertexes[nodeIndex].lowlink) + + { + + vector sol; + + int neighborIndex; + + do + + { + + neighborIndex = stk.top(); + + sol.push_back(neighborIndex); + + stk.pop(); + + graphVertexes[neighborIndex].onStack = false; + + } while (neighborIndex != nodeIndex); + + + + solutions.push_back(sol); + + } + +} + + + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + + + graphVertexes.resize(n + 1); + + for(int i = 1; i <= n; i++) + + { + + graphVertexes[i].index = i; + + } + + + + for(int i = 0, st, dr; i < m; i++) + + { + + fin >> st >> dr; + + graph[st].push_back(dr); + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(graphVertexes[i].tarjanIndex != -1) + + continue; + + + + Tarjan(i); + + } + + + + + + fout << solutions.size() << '\n'; + + + + for(auto sol: solutions) + + { + + for(auto val: sol) + + { + + fout << val << ' '; + + } + + + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/55812430-4c66-4851-bc7e-9cee3667e9b1.cpp b/scc/tarjan/55812430-4c66-4851-bc7e-9cee3667e9b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58bfac6533367d0a8a5494970724615d941de5d9 --- /dev/null +++ b/scc/tarjan/55812430-4c66-4851-bc7e-9cee3667e9b1.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int cnt, nrctc, n, m, mini[100000 +10], idx[100000 +10], inStack[100000 +10]; + +stack stiva; + +vector nod[100000 +10], ctc[100000 +10]; + + + +void dfs(int x) + +{ cnt++; + + mini[x] = idx[x] = cnt; + + stiva.push(x); + + inStack[x] = 1; + + for(int i=0; i> n >> m; + + for(int i=1; i<=m; i++) + + { int nod1, nod2; + + f >> nod1 >> nod2; + + nod[nod1].push_back(nod2); + + } + + for(int i=1; i<=n; i++) + + if(!idx[i]) dfs(i); + + g << nrctc << '\n'; + + for(int i=1; i<=nrctc; i++) + + { for(int j=0; j + + +using namespace std; + + + +const int N=100005; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,x,y; + +vector vecini[N]; + +int id[N],low[N],a[N],k,inq[N],s; + +int nrctc,nodes[N],ctcsize[N]; + + + +void dfs(int x) + +{ + + ++k; ++s; + + low[x]=id[x]=k; a[s]=x; inq[x]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) f>>x>>y, vecini[x].push_back(y); + + for(int i=1;i<=n;++i) if(!id[i]) + + { + + k=0; s=0; + + dfs(i); + + } + + g< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int DIM = 1e5 + 5; + + + +int low[DIM], niv[DIM], cod, N, M, K; + +bool f[DIM]; + +stack st; + +vector edge[DIM], ctc[DIM]; + + + +void dfs( int nod ){ + + niv[nod] = low[nod] = ++cod; + + st.push( nod ); + + f[nod] = true; + + for( int i = 0; i < edge[nod].size(); i++ ){ + + int vec = edge[nod][i]; + + if( niv[vec] == 0 ){ + + dfs( vec ); + + low[nod] = min( low[nod], low[vec] ); + + }else{ + + if( f[vec] == true ) + + low[nod] = min( low[nod], low[vec] ); + + } + + } + + if( low[nod] == niv[nod] ){ + + K++; + + while( !st.empty() && st.top() != nod ){ + + ctc[K].push_back( st.top() ); + + f[st.top()] = false; + + st.pop(); + + } + + ctc[K].push_back( st.top() ); + + f[st.top()] = false; + + st.pop(); + + } + +} + + + +int main(){ + + fin >> N >> M; + + for( int i = 1; i <= M; i++ ){ + + int x, y; fin >> x >> y; + + edge[x].push_back( y ); + + } + + + + for( int i = 1; i <= N; i++ ){ + + if( niv[i] == 0 ){ + + dfs( i ); + + } + + } + + + + fout << K << "\n"; + + for( int i = 1; i <= K; i++, fout << "\n" ) + + for( int j = 0; j < ctc[i].size(); j++, fout << " " ) + + fout << ctc[i][j]; + + + + return 0; + +} diff --git a/scc/tarjan/588e2963-6ce6-4f18-8ffb-3e43d34d720f.cpp b/scc/tarjan/588e2963-6ce6-4f18-8ffb-3e43d34d720f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cdcb6dcf10d0466edc5f431433ff696151676c8 --- /dev/null +++ b/scc/tarjan/588e2963-6ce6-4f18-8ffb-3e43d34d720f.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int index = 1; + +int n, m, nrc; + +int isInStack[100005], viz[100005], lowlink[100005], idx[100005]; + +vector graph[100005]; + +vector CC[100005]; + +stack S; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + + + +void addCC() + +{ + + nrc++; + + int top; + + do{ + + top = S.top(); + + CC[nrc].push_back(top); + + S.pop(); + + isInStack[top] = 0; + + }while(lowlink[top] != idx[top]); + + + +} +void solve(int nod) + +{ + + S.push(nod); + + isInStack[nod] = 1; + + viz[nod] = 1; + + idx[nod] = index; + + lowlink[nod] = index; + + index ++; + + + + for(auto &v:graph[nod]) + + { + + if(viz[v] == 0) + + { + + solve(v); + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + else{ + + if(isInStack[v] == 1) + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + } + + + + if(lowlink[nod] == idx[nod]) + + { + + addCC(); + + } + + + +} +void tarjan() + +{ + + + + + + for(int i = 1; i <= n; ++i) + + if(viz[i] == 0) + + solve(i); + +} + + + + + +void write() + +{ + + g< + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,idx[100005],low[100005],t,st[100005],k,nrc,ap[100005]; + +vector v[100005]; + +vector comp[100005]; + +void dfs(int nod) + +{ + + idx[nod]=t; + + low[nod]=t; + + t++; + + st[++k]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + if(idx[it]==0) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + if(low[nod]==idx[nod]) + + { + + nrc++; + + while(st[k]!=nod) + + { + + ap[st[k]]=0; + + comp[nrc].push_back(st[k]); + + k--; + + } + + comp[nrc].push_back(nod); + + ap[st[k]]=0; + + k--; + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) + + if(idx[i]==0) + + { + + t=1; + + k=0; + + dfs(i); + + } + + out< +#include +#include +#include +#include + +std::vector > adj, sccList; +std::vector idx, lowlink, conex, onStack; +std::stack stack; +int atIDX = 0; + +void read(int &n) { + std::ifstream f("ctc.in"); + f >> n;; + int edges; + adj.resize(n); + f >> edges; + for (int i = 0; i < edges; i++) { + int x, y; + f >> x >> y; + adj[x-1].push_back(y-1); + } + f.close(); +} + +void print(const std::vector > sccList) { + std::ofstream g("ctc.out"); + g << sccList.size() << '\n'; + for (int i = 0; i < sccList.size(); i++) { + for (int j = 0; j < sccList[i].size(); j++) { + g << sccList[i][j] + 1 << " "; + } + g << '\n'; + } + g.close(); +} + +void tarjan(int x) { + idx[x] = lowlink[x] = atIDX; + stack.push(x); + onStack[x] = 1; + atIDX++; + for (int i = 0; i < adj[x].size(); i++) { + if (idx[adj[x][i]] == -1) { + tarjan(adj[x][i]); + lowlink[x] = std::min(lowlink[x], lowlink[adj[x][i]]); + } else if (onStack[adj[x][i]] == 1) { + lowlink[x] = std::min(lowlink[x], lowlink[adj[x][i]]); + } + } + + if (idx[x] == lowlink[x]) { + conex.clear(); + int node; + do { + conex.push_back(node = stack.top()); + stack.pop(); + onStack[node] = 0; + } while (node != x); + + sccList.push_back(conex); + } +} + +int main() { + int n; + read(n); + + + idx.resize(n); + lowlink.resize(n); + onStack.resize(n); + idx.assign(n, -1); + onStack.assign(n, 0); + + for (int i = 0; i < n; i++) { + if (idx[i] == -1) { + tarjan(i); + } + } + + print(sccList); + return 0; +} diff --git a/scc/tarjan/5d02f0c8-ea75-4de3-9200-9fb5ef3257dc.cpp b/scc/tarjan/5d02f0c8-ea75-4de3-9200-9fb5ef3257dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10d0f4b65f4ae5af3dc004208ec9dc98c83a3617 --- /dev/null +++ b/scc/tarjan/5d02f0c8-ea75-4de3-9200-9fb5ef3257dc.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + + + +using namespace std; + +int x,y; + +int nrcomp; + +stacks; + +const int MAXN=100001; + +vector < int > g[MAXN]; + +vectorctc[MAXN]; + +int viz[MAXN]; + +int iss[MAXN]; + +int nivel[MAXN],idk; + +int L[MAXN]; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + +void tarjan(int x) { + + viz[x] = 1; + + iss[x] = 1; + + s.push(x); + + nivel[x] = ++idk; + + L[x] = nivel[x]; + + for ( auto y : g[x]) { + + if(!viz[y]) { + + tarjan(y); + + L[x] = min(L[x],L[y]); + +} + + else + + if(iss[y]) { + + L[x] = min(nivel[y],L[x]); + +} + + + + + +} + + if(nivel[x] == L[x]) { + + ++nrcomp; + +while(!s.empty()) { + + int y = s.top(); + + s.pop(); + + iss[y]=false; + + ctc[nrcomp].push_back(y); + + if(y == x) + + break; + +} + +} + +} + + + +int n,m; + +int main() { + + cin >> n >>m; + + for ( int i = 1; i <= m; ++i) { + + cin >> x >> y; + + g[x].push_back(y); + +} + + for ( int i = 1; i <= n; ++i) + + if(!viz[i]) + + tarjan(i); + + + + cout << nrcomp <<"\n"; + +for (int i = 1; i <=nrcomp; ++i) { + + for ( auto y : ctc[i]) + + cout << y <<" "; + +cout << "\n"; + +} + + + +} diff --git a/scc/tarjan/5f412955-56e1-4e44-85f5-652008c2225b.cpp b/scc/tarjan/5f412955-56e1-4e44-85f5-652008c2225b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd8185cc0b9fa331dc2c922bf748f1470c5084a --- /dev/null +++ b/scc/tarjan/5f412955-56e1-4e44-85f5-652008c2225b.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +vector> adiacenta(100001); +int ids[100001], low[100001]; +bool onStack[100001]; +stack stck; +int n, m, id = 0; +vector> rez; + +void dfs(int node) { + stck.push(node); + onStack[node] = true; + ids[node] = low[node] = id++; + + for(int x : adiacenta[node]) { + if(ids[x] == -1) { + dfs(x); + low[node] = min(low[node], low[x]); + } + else if(onStack[x]) + low[node] = min(low[node], ids[x]); + } + + vector v_aux; + int aux; + + if(ids[node] == low[node]) { + + while(stck.top() != node) { + + aux = stck.top(); + stck.pop(); + + onStack[aux] = false; + v_aux.push_back(aux); + + } + + aux = stck.top(); + v_aux.push_back(aux); + stck.pop(); + + rez.push_back(v_aux); + onStack[aux] = false; + } +} + +void findSccs() { + for(int i = 1; i <= n; i++) { + ids[i] = -1; + } + + for(int i = 1; i <= n; i++) + if(ids[i] == -1) + dfs(i); +} + + +int main() { + int x, y; + f >> n >> m; + + adiacenta.resize(n); + + for(int i = 0; i < n; i++) + onStack[i] = false; + + for(int i = 0; i < m; i++) { + f >> x >> y; + adiacenta[x].push_back(y); + } + + findSccs(); + g << rez.size() << '\n'; + + for(auto &i : rez) { + for (auto j : i) + g << j << " "; + g << '\n'; + } + + f.close(); + g.close(); + return 0; +} diff --git a/scc/tarjan/5ff38993-0d6b-49f6-a30d-42bfd80cc5fa.cpp b/scc/tarjan/5ff38993-0d6b-49f6-a30d-42bfd80cc5fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5fbe3ea369f6d0b975f377108788c91eb725215 --- /dev/null +++ b/scc/tarjan/5ff38993-0d6b-49f6-a30d-42bfd80cc5fa.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fi ("ctc.in"); + +ofstream fo ("ctc.out"); + +int n, m, a, b, idx, cnt; + +stack s; + +vector ctc[100001]; + +class Node { + + public: + + bool stack = false; + + int idx = -1, low; + + vector edges; + + + + void addNode(int x) { + + edges.push_back(x); + + } + +} v[100001]; + + + +void connect_component(int x) { + + v[x].idx = v[x].low = ++idx; + + v[x].stack = true; + + s.push(x); + + for (auto i:v[x].edges) { + + if (v[i].idx == -1) { + + connect_component(i); + + v[x].low = min(v[x].low, v[i].low); + + } else if (v[i].stack && v[i].idx < v[x].idx) { + + v[x].low = min(v[x].low, v[i].idx); + + } + + } + + if (v[x].idx == v[x].low) { + + while (1) { + + a = s.top(); + + s.pop(); + + ctc[cnt].push_back(a); + + v[a].stack = false; + + if (a == x) + + break; + + } + + ++cnt; + + } + +} + + + +int main() { + + fi >> n >> m; + + for (int i = 1; i <= m; ++i) { + + fi >> a >> b; + + v[a].addNode(b); + + } + + for (int i = 1; i <= n; ++i) + + if (v[i].idx == -1) + + connect_component(i); + + fo << cnt << '\n'; + + for (int i = 0; i < cnt; ++i) + + for (auto j:ctc[i]) + + fo << j << " \n"[j==ctc[i].back()]; + + return 0; + +} diff --git a/scc/tarjan/60833155-0b51-4ad9-968f-d1bf0bcf7fb9.cpp b/scc/tarjan/60833155-0b51-4ad9-968f-d1bf0bcf7fb9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60e9df06097cddc0a39e89770dd3f8fb3bf3f874 --- /dev/null +++ b/scc/tarjan/60833155-0b51-4ad9-968f-d1bf0bcf7fb9.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 1e5 + 5; + + + +vector < int > a[N]; + +vector < vector < int > > sol; + +stack < int > st; + + + +int disc[N], low[N], temp; + +bool in_stack[N]; + + + +void tarjan(int k) + +{ + + disc[k] = low[k] = ++temp; + + st.push(k); + + in_stack[k] = true; + + for(auto v : a[k]) { + + if(disc[v] == 0) { + + tarjan(v); + + low[k] = min(low[k], low[v]); + + } + + else if(in_stack[v] == true) low[k] = min(low[k], low[v]); + + } + + if(disc[k] == low[k]) { + + vector < int > add; + + int node = k; + + do { + + node = st.top(); + + add.push_back(node); + + in_stack[node] = false; + + st.pop(); + + } + + while(node != k); + + sol.push_back(add); + + } + +} + +int main() + +{ + + usain_bolt(); + + + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y; + + + + fin >> x >> y; + + a[x].push_back(y); + + } + + for(int i = 1; i <= n; ++i) + + if(disc[i] == 0) tarjan(i); + + fout << sol.size() << "\n"; + + for(auto x : sol) { + + for(auto y : x) { + + fout << y << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/61238e4f-ef9e-4edd-8e8c-44f68d335f3b.cpp b/scc/tarjan/61238e4f-ef9e-4edd-8e8c-44f68d335f3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d20fd701e9df0c3c858158881dd7a9f87b36a7c8 --- /dev/null +++ b/scc/tarjan/61238e4f-ef9e-4edd-8e8c-44f68d335f3b.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + + + +struct Node { + + int visited; + + int minReach; + + bool onStack; + +}; + + + +int n, m; + +int strongSiz; + + + +Node v[1 + NMAX]; + +vector g[1 + NMAX]; + +vector strong[1 + NMAX]; + +stack st; + + + +int step = 0; + + + +void computeStrong(int from) { + + step++; + + v[from].visited = step; + + v[from].minReach = from; + + st.push(from); + + v[from].onStack = true; + + for(int i = 0; i < g[from].size(); i++) { + + int to = g[from][i]; + + if(v[to].visited == 0){ + + computeStrong(to); + + if(v[v[from].minReach].visited > v[v[to].minReach].visited) { + + v[from].minReach = v[to].minReach; + + } + + } else if(v[to].onStack == true) { + + if(v[v[from].minReach].visited > v[to].visited) { + + v[from].minReach = to; + + } + + } + + } + + if(v[from].minReach == from) { + + strongSiz++; + + int popped; + + do { + + popped = st.top(); + + st.pop(); + + strong[strongSiz].push_back(popped); + + v[popped].onStack = false; + + } while (popped != from); + + } + +} + + + +int main() { + + + + int a, b; + + in >> n >> m; + + for(int i = 1; i <= m; i++) { + + in >> a >> b; + + g[a].push_back(b); + + } + + for(int i = 1; i <= n; i++) { + + if(v[i].visited == 0) { + + computeStrong(i); + + } + + } + + out << strongSiz << '\n'; + + for(int i = 1; i <= strongSiz; i++) { + + for(int j = 0; j < strong[i].size(); j++) { + + out << strong[i][j] << ' '; + + } + + out << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/61ffcf40-8bed-4b6f-926e-d57cf4e61189.cpp b/scc/tarjan/61ffcf40-8bed-4b6f-926e-d57cf4e61189.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89d128bd7104edb02b156ad8706a65f275bb2a1a --- /dev/null +++ b/scc/tarjan/61ffcf40-8bed-4b6f-926e-d57cf4e61189.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,a,b,cnt; + +bitset<100010> ok; + +int x[100010],y[100010]; + +vector v[100010]; + +vector > C; + +stack St; + + + +void dfs(int poz){ + + x[poz]=y[poz]=++cnt; + + St.push(poz);ok[poz]=1; + + for(auto it:v[poz]) + + if(!x[it]){ + + dfs(it); + + y[poz]=min(y[poz],y[it]); + + }else + + if(ok[it]) + + y[poz]=min(y[poz],x[it]); + + if(x[poz]!=y[poz])return; + + vector c; + + while(1){ + + int aux=St.top();St.pop();ok[aux]=0; + + c.push_back(aux); + + if(aux==poz)break; + + } + + C.push_back(c); + +} + + + +int main(){ + + f>>n>>m; + + for(int i=1;i<=m;i++){ + + f>>a>>b; + + v[a].push_back(b); + + } + + for(int i=1;i<=n;i++) + + if(!x[i]){ + + dfs(i); + + } + + g< +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int oo=INT_MAX/2; + +typedef vector vi; + +int n,m,k; +vector g; + +vi lowlink, idx, inStk; +vi stk; + +vector ctcs; + +int _index=1; + +void tarjan(int nd, int id=1){ + lowlink[nd]=idx[nd]=_index++; + inStk[nd]=1; + stk.push_back(nd); + + for(auto vc:g[nd]){ + if(idx[vc]==0){ + tarjan(vc,id+1); + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + else if(inStk[vc]){ + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + } + if(lowlink[nd]==idx[nd]){ + cout<>n>>m; + g.resize(n+1), lowlink.resize(n+1), idx.resize(n+1,0), inStk.resize(n+1,0); + for(int i=m;i--;){ + int x,y; fin>>x>>y; + g[x].push_back(y); + } + for(int i=1;i<=n;++i) + if(idx[i]==0) tarjan(i); + fout< + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, l, sol; + +pair p[100005]; + +vector graph[100005], out[100005]; + +stack stk; + +bitset<100005> check, inS; + + + +void dfs(int now); + + + +int main() + +{ + + + + fin >> n >> m; + + while(m--){ + + int x, y; + + fin >> x >> y; + + + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(!check[i]) dfs(i); + + } + + + + fout << sol << "\n"; + + for(int i = 1; i <= sol; ++i){ + + for(auto it:out[i]) fout << it << " "; + + fout << "\n"; + + } + + + + return 0; + +} + + + +void dfs(int now){ + + ++l; + + p[now] = {l, l}; + + check[now] = inS[now] = true; + + stk.push(now); + + + + for(auto next:graph[now]){ + + if(!check[next]){ + + dfs(next); + + p[now].second = min(p[now].second, p[next].second); + + } + + else if(inS[next]) p[now].second = min(p[now].second, p[next].first); + + } + + + + if(p[now].first == p[now].second){ + + ++sol; + + + + while(stk.top() != now){ + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + +} diff --git a/scc/tarjan/63e8176f-50a8-4925-a5c1-10505fbc631a.cpp b/scc/tarjan/63e8176f-50a8-4925-a5c1-10505fbc631a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4a9c80ef18a275a3e667b53ad3ae8f5bf77c906 --- /dev/null +++ b/scc/tarjan/63e8176f-50a8-4925-a5c1-10505fbc631a.cpp @@ -0,0 +1,177 @@ +#include +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int N,M,x,y; + +vector Graph[100004]; + +int ids[100004]; + +int lowLink[100004]; + +bool OnStack[100004]; + +stack Stack; + +int id = 0; + +int counter = 0; + +int check=0; + +vector answer[100004]; + + + +void DFS(int nod) + +{ + + + + Stack.push(nod); + + OnStack[nod] = true; + + + + + + ids[nod] = id; + + + + lowLink[nod] = id; + + + + id++; + + + + for(int i=0; i>N>>M; + + for(int i=1; i<=M; i++) + + { + + in>>x>>y; + + Graph[x].push_back(y); + + } + + + + memset(ids,0,sizeof(ids)); + + memset(lowLink,0, sizeof(lowLink)); + + memset(OnStack,0,sizeof(OnStack)); + + + + for(int i=1; i<=N; i++) + + if(ids[i]==0) + + DFS(i); + + + + + + for(int i=1; i<=N; i++) + + answer[lowLink[i]].push_back(i); + + for(int i=0; i<100004; i++) + + if(answer[i].size()!=0) + + check+=1; + + out< +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bitset<100005> iq; + +deque q; + +vector L[100005],aux; + +vector< vector > sol; + +int low[100005],niv[100005],g,n,m,i,x,y; + +void dfs(int nod) + +{ + + iq[nod]=1; + + niv[nod]=++g; + + low[nod]=g; + + q.push_back(nod); + + for(auto it:L[nod]) + + { + + if(niv[it]==0) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(iq[it]==1) + + { + + low[nod]=min(low[nod],low[it]); + + + + } + + } + + if(low[nod]==niv[nod]) + + { + + aux.clear(); + + int x=0; + + while(x!=nod) + + { + + x=q.back(); + + iq[x]=0; + + q.pop_back(); + + aux.push_back(x); + + } + + sol.push_back(aux); + + } + +} + +int main() + +{ + + fin>>n>>m; + + for(i=1; i<=m; i++) + + { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(niv[i]==0) + + { + + dfs(i); + + } + + } + + fout< +#include + + +using namespace std; + + + +ifstream cin("ctc.in"); + +ofstream cout("ctc.out"); + + + +vectoradj[100002]; + +vectorstiva; + +vectorscc[100002]; + +int nrc,disc[100002],low[100002],cc[100002],t; + + + +void dfs(int n1) + +{ + + disc[n1]=low[n1]=++t; + + stiva.push_back(n1); + + for(vector::iterator it=adj[n1].begin();it!=adj[n1].end();it++) + + { + + int n2=*it; + + if(!cc[n2]) + + { + + if(!disc[n2]) + + dfs(n2); + + low[n1]=min(low[n1],low[n2]); + + } + + } + + if(disc[n1]==low[n1]) + + { + + nrc++; + + while(stiva.back()!=n1) + + { + + scc[nrc].push_back(stiva.back()); + + cc[stiva.back()]=nrc; + + stiva.pop_back(); + + } + + scc[nrc].push_back(n1); + + cc[n1]=nrc; + + stiva.pop_back(); + + } + +} + + + +int main() + +{ + + int n,m,i,x,y; + + cin>>n>>m; + + for(i=1; i<=m; i++) + + { + + cin>>x>>y; + + adj[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(!disc[i]) + + dfs(i); + + } + + cout<::iterator it=scc[i].begin();it!=scc[i].end();it++) + + cout<<*it<<' '; + + cout<<'\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/6589a2de-a9cc-4d67-bde3-d4c39da0c2b5.cpp b/scc/tarjan/6589a2de-a9cc-4d67-bde3-d4c39da0c2b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff1b6dbb770c6477c94ca76c003262c279a903a6 --- /dev/null +++ b/scc/tarjan/6589a2de-a9cc-4d67-bde3-d4c39da0c2b5.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int n, m, i, j, k, start, ord[100003], levMin[100003]; + +vector vec[100003], comp[100003]; + +stack stiva; + +set s; + + + +void func(int nod) { + + int p, x; + + + + ord[nod] = start; + + levMin[nod] = start; + + start++; + + stiva.push(nod); + + s.insert(nod); + + + + for(p = 0; p < vec[nod].size(); p++) + + if(!ord[vec[nod][p]]) { + + func(vec[nod][p]); + + levMin[nod] = min(levMin[nod], levMin[vec[nod][p]]); + + } + + else if(s.find(vec[nod][p]) != s.end()) + + levMin[nod] = min(levMin[nod], ord[vec[nod][p]]); + + + + if(ord[nod] == levMin[nod]) + + while(!stiva.empty()) { + + x = stiva.top(); + + stiva.pop(); + + s.erase(x); + + comp[k].push_back(x); + + if(x == nod) { + + k++; + + break; + + } + + } + +} + + + +int main() { + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + + + f >> n >> m; + + while(m) { + + f >> i >> j; + + + + vec[i].push_back(j); + + m--; + + } + + f.close(); + + + + k = 0; + + start = 1; + + for(i = 1; i <= n; i++) + + if(!ord[i]) + + func(i); + + g << k << '\n'; + + + + for(i = 0; i < k; i++, g << '\n') + + for(j = 0; j < comp[i].size(); j++) + + g << comp[i][j] << ' '; + + + + g.close(); + + + + return 0; + +} diff --git a/scc/tarjan/65f9f26c-a4e8-4fed-afa7-6e8ca93c583b.cpp b/scc/tarjan/65f9f26c-a4e8-4fed-afa7-6e8ca93c583b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6bf594a957c2717620ebd46847ba236f3f5feca --- /dev/null +++ b/scc/tarjan/65f9f26c-a4e8-4fed-afa7-6e8ca93c583b.cpp @@ -0,0 +1,150 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int noduri, muchii, componente_tari, a, b, ind, ids[100005]; + +bool instack[100005], visited[100005]; + +int lowkey[100005]; + +vector mat[100005]; + +vector componente[100005]; + +stack s; + + + +void dfs(int node) + +{ + + s.push(node); + + instack[node] = 1; + + ids[node] = ind; + + lowkey[node] = ind; + + visited[node] = 1; + + ++ind; + + for(int i = 0; i < mat[node].size(); ++i) + + { + + int next_node = mat[node][i]; + + if(!visited[next_node]) + + { + + dfs(next_node); + + } + + if(instack[next_node]) + + { + + lowkey[node] = min(lowkey[node], lowkey[next_node]); + + } + + } + + if(ids[node] == lowkey[node]) + + { + + while(!s.empty()) + + { + + int next = s.top(); + + componente[componente_tari].push_back(next); + + s.pop(); + + instack[next] = 0; + + lowkey[next] = ids[node]; + + if(node == next) break; + + } + + componente_tari++; + + } + +} + + + +int main() + +{ + + fin >> noduri >> muchii; + + for(int i = 1; i <= muchii; ++i) + + { + + fin >> a >> b; + + mat[a].push_back(b); + + } + + for(int node = 1; node <= noduri; ++node) + + { + + if(!visited[node]) + + { + + dfs(node); + + } + + } + + fout << componente_tari << "\n"; + + for(int i = 0; i < componente_tari; ++i) + + { + + for(int j = 0; j < componente[i].size(); ++j) + + { + + fout << componente[i][j] << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/6707f8f8-53e1-4ef4-b78c-95372214aaed.cpp b/scc/tarjan/6707f8f8-53e1-4ef4-b78c-95372214aaed.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6e5f1bd331c53434289905ef9af79961f18ccbf --- /dev/null +++ b/scc/tarjan/6707f8f8-53e1-4ef4-b78c-95372214aaed.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +ifstream inf("ctc.in"); + +ofstream outf("ctc.out"); + + + +const int N=100010; + + + +int n, m, disc[N], low[N], t; + +bitset b, vis; + +vector v[N]; + +stack s; + +vector> sol; + + + +void ctc(int ); + + + +int main() + +{ + + inf>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + inf>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + disc[i]=low[i]=-1; + + for(int i=1; i<=n; i++) + + if(!vis[i]) + + ctc(i); + + outf< cctc; + + do + + { + + tp=s.top(); + + s.pop(); + + b[tp]=0; + + cctc.push_back(tp); + + }while(tp!=nod); + + sol.push_back(cctc); + + } + +} diff --git a/scc/tarjan/68118042-9f76-48b9-8f86-a4e86bc87e02.cpp b/scc/tarjan/68118042-9f76-48b9-8f86-a4e86bc87e02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0479c6bc5a1383ea69dd271a7ffaa2cadde9ff7a --- /dev/null +++ b/scc/tarjan/68118042-9f76-48b9-8f86-a4e86bc87e02.cpp @@ -0,0 +1,148 @@ +#include +#include +#include + + +using namespace std; + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + + + +const int MAXN = 100000, MAXM = 200000; + +int N, M, sccCount = 0, cnt = 0; + +stack st; + +bool onSt[MAXN + 1]; + +int vis[MAXN + 1], low[MAXN + 1]; + +vector adj[MAXN + 1], con; + +vector > C; + + + + + + + +void dfs(int curr) + +{ + + st.push(curr); + + onSt[curr] = true; + + ++cnt; + + vis[curr] = cnt; + + low[curr] = cnt; + + + + for (int nbh : adj[curr]) + + { + + if (vis[nbh] == 0) + + dfs(nbh); + + if (onSt[nbh]) + + low[curr] = min(low[nbh], low[curr]); + + } + + + + if (vis[curr] == low[curr]) + + { + + ++sccCount; + + con.clear(); + + while(st.top() != curr) + + { + + onSt[st.top()] = false; + + low[st.top()] = vis[curr]; + + con.push_back(st.top()); + + st.pop(); + + } + + onSt[curr] = false; + + low[curr] = vis[curr]; + + con.push_back(curr); + + st.pop(); + + C.push_back(con); + + } + +} + + + +int main() + +{ + + in >> N >> M; + + int m1, m2; + + for (int i = 0; i < M; ++i) + + { + + in >> m1 >> m2; + + adj[m1].push_back(m2); + + } + + + + for (int i = 1; i <= N; ++i) + + if (vis[i] == 0) + + dfs(i); + + out << sccCount << "\n"; + + for (int i = 0; i < C.size(); ++i) + + { + + for (int j = 0; j < C[i].size(); ++j) + + out << C[i][j]<< " "; + + out << "\n"; + + } + + + + return 0; + +} diff --git a/scc/tarjan/6848e53b-8d24-4687-b547-328bc25f5901.cpp b/scc/tarjan/6848e53b-8d24-4687-b547-328bc25f5901.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff1b6dbb770c6477c94ca76c003262c279a903a6 --- /dev/null +++ b/scc/tarjan/6848e53b-8d24-4687-b547-328bc25f5901.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int n, m, i, j, k, start, ord[100003], levMin[100003]; + +vector vec[100003], comp[100003]; + +stack stiva; + +set s; + + + +void func(int nod) { + + int p, x; + + + + ord[nod] = start; + + levMin[nod] = start; + + start++; + + stiva.push(nod); + + s.insert(nod); + + + + for(p = 0; p < vec[nod].size(); p++) + + if(!ord[vec[nod][p]]) { + + func(vec[nod][p]); + + levMin[nod] = min(levMin[nod], levMin[vec[nod][p]]); + + } + + else if(s.find(vec[nod][p]) != s.end()) + + levMin[nod] = min(levMin[nod], ord[vec[nod][p]]); + + + + if(ord[nod] == levMin[nod]) + + while(!stiva.empty()) { + + x = stiva.top(); + + stiva.pop(); + + s.erase(x); + + comp[k].push_back(x); + + if(x == nod) { + + k++; + + break; + + } + + } + +} + + + +int main() { + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + + + f >> n >> m; + + while(m) { + + f >> i >> j; + + + + vec[i].push_back(j); + + m--; + + } + + f.close(); + + + + k = 0; + + start = 1; + + for(i = 1; i <= n; i++) + + if(!ord[i]) + + func(i); + + g << k << '\n'; + + + + for(i = 0; i < k; i++, g << '\n') + + for(j = 0; j < comp[i].size(); j++) + + g << comp[i][j] << ' '; + + + + g.close(); + + + + return 0; + +} diff --git a/scc/tarjan/69c21c53-db4a-44e3-bc04-80c06d4a79a9.cpp b/scc/tarjan/69c21c53-db4a-44e3-bc04-80c06d4a79a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3891967c74b4bdabe0b1ceee79afeb1e303041fb --- /dev/null +++ b/scc/tarjan/69c21c53-db4a-44e3-bc04-80c06d4a79a9.cpp @@ -0,0 +1,160 @@ +#include + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; + + + + + + + +vector adj[100005], con, idx, low, in_stack; + +vector < vector > C; + +stack S; + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + + + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) + + { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + + + +{ + + idx[n] = low[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) + + { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + low[n] = ((low[n]) < (low[*it]) ? (low[n]) : (low[*it])); + + else if (in_stack[*it]) + + low[n] = ((low[n]) < (low[*it]) ? (low[n]) : (low[*it])); + + } + + if (idx[n] == low[n]) + + { + + con.clear(); + + int node; + + do + + { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + + + +{ + + int n; + + read_in(adj, n, iname); + + idx.resize(n), low.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + print_out(C, oname); + + return 0; + + + +} diff --git a/scc/tarjan/6af74401-0d98-440a-b622-a2df2513c65f.cpp b/scc/tarjan/6af74401-0d98-440a-b622-a2df2513c65f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaa5d2f29b99001202fb6dd0a6705bba3c032626 --- /dev/null +++ b/scc/tarjan/6af74401-0d98-440a-b622-a2df2513c65f.cpp @@ -0,0 +1,52 @@ +#include + + + + + + +using namespace std; + +template +void SCC(Graph& graph, CB cb) { + int n = graph.size(), timer = 0; + vector val(n, 0), stk, cont; + function dfs = [&](int node) { + int low; low = val[node] = ++timer; + int sz = stk.size(); stk.push_back(node); + for (auto vec : graph[node]) + if (val[vec] != -1) + low = min(low, val[vec] ?: dfs(vec)); + if (low == val[node]) { + cont = {stk.begin() + sz, stk.end()}; + for (auto x : cont) val[x] = -1; + cb(cont); stk.resize(sz); + } else val[node] = low; + return low; + }; + for (int i = 0; i < n; ++i) + if (val[i] != -1) + dfs(i); +} + +int main() { + ifstream cin("ctc.in"); + ofstream cout("ctc.out"); + int n, m; cin >> n >> m; + vector> graph(n); + for (int i = 0; i < m; ++i) { + int a, b; cin >> a >> b; + graph[--a].push_back(--b); + } + vector> comps; + SCC(graph, [&](vector comp) { + comps.push_back(comp); + }); + cout << comps.size() << '\n'; + for (auto comp : comps) { + for (auto x : comp) + cout << x + 1 << " "; + cout << '\n'; + } + return 0; +} diff --git a/scc/tarjan/6ba4b89b-2d71-4827-8075-a318ede4a594.cpp b/scc/tarjan/6ba4b89b-2d71-4827-8075-a318ede4a594.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb17a70eee38d3fbcbf261a9aaaa7fcaf8604d16 --- /dev/null +++ b/scc/tarjan/6ba4b89b-2d71-4827-8075-a318ede4a594.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +typedef struct { + + int index; + + int lowlink; + + bool onStack; + +} nod; + + + +vector listaNoduri; + +vector listaArce[100010]; + +vector> listaComponente; + +stack S; + +int n, m; + +int ix = 0; + +int k = 0; + + + +void strongconnect(int v) + +{ + + listaNoduri[v].index = ix; + + listaNoduri[v].lowlink = ix; + + ix++; + + S.push(v); + + listaNoduri[v].onStack = true; + + + + for (int i = 0; i < listaArce[v].size(); i++) + + { + + int w = listaArce[v][i]; + + if (listaNoduri[w].index == -1) + + { + + strongconnect(w); + + listaNoduri[v].lowlink = min(listaNoduri[v].lowlink, listaNoduri[w].lowlink); + + } + + else if (listaNoduri[w].onStack) + + listaNoduri[v].lowlink = min(listaNoduri[v].lowlink, listaNoduri[w].index); + + } + + + + if (listaNoduri[v].lowlink == listaNoduri[v].index) + + { + + k++; + + vector L; + + int w; + + do { + + w = S.top(); + + S.pop(); + + listaNoduri[w].onStack = false; + + L.push_back(w); + + } while (v != w); + + listaComponente.push_back(L); + + } + + + +} + + + + + + + +int main() + +{ + + f >> n >> m; + + for (unsigned int i = 0; i < m; i++) + + { + + int nodS, nodD; + + f >> nodS >> nodD; + + listaArce[nodS].push_back(nodD); + + } + + + + for (unsigned int i = 1; i <= n + 1; i++) + + listaNoduri.push_back({-1, -1, false}); + + + + for (unsigned int i = 1; i <= n; i++) + + { + + if (listaNoduri[i].index == -1) + + strongconnect(i); + + } + + + + g << k << '\n'; + + for (int i = 0; i < listaComponente.size(); i++) + + { + + for (int j = 0; j < listaComponente[i].size(); j++) + + g << listaComponente[i][j] << ' ' ; + + g << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/6cf356cd-b5b4-4675-bddc-b0fa905d610b.cpp b/scc/tarjan/6cf356cd-b5b4-4675-bddc-b0fa905d610b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b6e5d6dd430645f5da3341b088fe118953857a7 --- /dev/null +++ b/scc/tarjan/6cf356cd-b5b4-4675-bddc-b0fa905d610b.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, ctc, t, T[100000 + 1], M[100000 + 1]; + +vector G[100000 + 1], C[100000 + 1]; + +stack N; + +bitset<100000 + 1> inN; + + + +void Df(int nod); + + + +int main() + +{ + + fin >> n >> m; + + + + for (int i = 1, x, y; i <= m; ++i) + + { + + fin >> x >> y; + + G[x].push_back(y); + + } + + + + for (int i = 1; i <= n; ++i) + + { + + if (T[i] == 0) + + { + + Df(i); + + } + + } + + + + fout << ctc << '\n'; + + for (int i = 1; i <= ctc; ++i) + + { + + for (int nod : C[i]) + + { + + fout << nod << ' '; + + } + + fout << '\n'; + + } + + + + fin.close(); + + fout.close(); + + return 0; + +} + + + +void Df(int nod) + +{ + + M[nod] = T[nod] = ++t; + + + + N.push(nod); + + for (int vecin : G[nod]) + + { + + if (T[vecin] == 0) + + { + + Df(vecin); + + M[nod] = min(M[nod], M[vecin]); + + } + + else + + { + + if (!inN[vecin]) + + { + + M[nod] = min(M[nod], T[vecin]); + + } + + } + + } + + + + if (T[nod] == M[nod]) + + { + + ++ctc; + + + + while (N.top() != nod) + + { + + C[ctc].push_back(N.top()); + + inN[N.top()] = true; + + N.pop(); + + } + + + + C[ctc].push_back(N.top()); + + inN[N.top()] = true; + + N.pop(); + + } + +} diff --git a/scc/tarjan/6e78b358-5dc8-40f5-9cdc-749e2c164c3b.cpp b/scc/tarjan/6e78b358-5dc8-40f5-9cdc-749e2c164c3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45620f8556a4676e06d400d4107415d7445aab5b --- /dev/null +++ b/scc/tarjan/6e78b358-5dc8-40f5-9cdc-749e2c164c3b.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +typedef struct { + + int index; + + int lowlink; + + bool onStack; + +} nod; + + + +vector listaNoduri; + +vector listaArce[100010]; + +vector> listaComponente; + +stack S; + +int n, m; + +int ix = 0; + +int k = 0; + + + +void strongconnect(int v) + +{ + + listaNoduri[v].index = ix; + + listaNoduri[v].lowlink = ix; + + ix++; + + S.push(v); + + listaNoduri[v].onStack = true; + + + + for (int i = 0; i < listaArce[v].size(); i++) + + { + + int w = listaArce[v][i]; + + if (listaNoduri[w].index == -1) + + { + + strongconnect(w); + + listaNoduri[v].lowlink = min(listaNoduri[v].lowlink, listaNoduri[w].lowlink); + + } + + else if (listaNoduri[w].onStack) + + listaNoduri[v].lowlink = min(listaNoduri[v].lowlink, listaNoduri[w].index); + + } + + + + if (listaNoduri[v].lowlink == listaNoduri[v].index) + + { + + k++; + + vector L; + + int w; + + do { + + w = S.top(); + + S.pop(); + + listaNoduri[w].onStack = false; + + L.push_back(w); + + } while (v != w); + + listaComponente.push_back(L); + + } + + + +} + + + + + + + +int main() + +{ + + f >> n >> m; + + for (unsigned int i = 0; i < m; i++) + + { + + int nodS, nodD; + + f >> nodS >> nodD; + + listaArce[nodS].push_back(nodD); + + } + + + + for (unsigned int i = 1; i <= n + 1; i++) + + listaNoduri.push_back({-1, -1, false}); + + + + for (unsigned int i = 1; i <= n; i++) + + { + + if (listaNoduri[i].index == -1) + + strongconnect(i); + + } + + + + g << k << '\n'; + + for (int i = 0; i < listaComponente.size(); i++) + + { + + for (int j = 0; j < listaComponente[i].size(); j++) + + g << listaComponente[i][j] << ' ' ; + + g << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/6f845b7f-bcae-479e-aab9-87c648a5abe3.cpp b/scc/tarjan/6f845b7f-bcae-479e-aab9-87c648a5abe3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77abe53657f523419dd77b73baf24a475fc012e9 --- /dev/null +++ b/scc/tarjan/6f845b7f-bcae-479e-aab9-87c648a5abe3.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m, sccCount = 0, cnt = 0; +stack stiva; +bool onSt[1000001]; +int vis[1000001], low[1000001]; +vector adj[1000001], con; +vector> result; + +void dfs(int node) +{ + stiva.push(node); + onSt[node] = true; + ++cnt; + vis[node] = cnt; + low[node] = cnt; + + for (int nbh : adj[node]) + { + if (vis[nbh] == 0) + dfs(nbh); + if (onSt[nbh]) + low[node] = min(low[nbh], low[node]); + } + if (vis[node] == low[node]) + { + ++sccCount; + con.clear(); + while (stiva.top() != node) + { + onSt[stiva.top()] = false; + low[stiva.top()] = vis[node]; + con.push_back(stiva.top()); + stiva.pop(); + } + onSt[node] = false; + low[node] = vis[node]; + con.push_back(node); + stiva.pop(); + result.push_back(con); + } +} + +int main() +{ + fin >> n >> m; + for (int i = 0; i < m; ++i) + { + int x, y; + fin >> x >> y; + adj[x].push_back(y); + } + + for (int i = 1; i <= n; ++i) + if (vis[i] == 0) + dfs(i); + fout << sccCount << "\n"; + for (int i = 0; i < result.size(); ++i) + { + for (int j = 0; j < result[i].size(); ++j) + fout << result[i][j] << " "; + fout << "\n"; + } + return 0; +} diff --git a/scc/tarjan/70513916-847e-4eea-bd2c-862e8c85b006.cpp b/scc/tarjan/70513916-847e-4eea-bd2c-862e8c85b006.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cdb495569f148b1782e48e5112c527b7017b038 --- /dev/null +++ b/scc/tarjan/70513916-847e-4eea-bd2c-862e8c85b006.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +struct Node{ + + int index=0, lowlink=0; + + bool onstack = false; + + Node(){} + + Node(int index, int lowlink) : index(index), lowlink(lowlink){ + + onstack = true; + + } + +}; + + + +int n, m; + +vector gra[100041]; + +void read(){ + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + int a, b; + + fin >> a >> b; + + gra[a].push_back(b); + + } + +} + + + +vector> components; + + + +stack s; + +int index = 1; + +Node nodes[100041]; + +void strongconnect(int a){ + + s.push(a); + + Node &an = nodes[a]; + + an = Node(index, index); + + index++; + + for(auto b : gra[a]){ + + Node &bn = nodes[b]; + + if(bn.index == 0){ + + strongconnect(b); + + an.lowlink = min(an.lowlink, bn.lowlink); + + }else{ + + an.lowlink = min(an.lowlink, bn.index); + + } + + } + + if(an.index == an.lowlink){ + + components.push_back(vector()); + + vector &component = components.back(); + + int b; + + do{ + + b = s.top(); + + s.pop(); + + nodes[b].onstack = false; + + component.push_back(b); + + }while(b != a); + + } + +} + + + +void tarzan(){ + + for(int i = 1; i <= n; ++i){ + + if(nodes[i].index == 0){ + + strongconnect(i); + + while(!s.empty()){ + + components.push_back({s.top()}); + + s.pop(); + + } + + } + + } + +} + + + +void write(){ + + fout << components.size() << "\n"; + + for(auto component : components){ + + for(auto a : component){ + + fout << a << " "; + + } + + fout << "\n"; + + } + +} + + + +int main(){ + + read(); + + tarzan(); + + write(); + + return 0; + +} diff --git a/scc/tarjan/72980b20-5cb0-4cd1-a633-410b9ed4a950.cpp b/scc/tarjan/72980b20-5cb0-4cd1-a633-410b9ed4a950.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc2986fc4c4e8c3323c8e547f707619b79156119 --- /dev/null +++ b/scc/tarjan/72980b20-5cb0-4cd1-a633-410b9ed4a950.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +ifstream cin ("ctc.in"); +ofstream cout ("ctc.out"); + +const int NMAX = 1e5 + 14; + +vector gr[NMAX]; +bitset inStack; + +pair info [NMAX]; + +int currentNodeLabel = 0; +vector stack; + +void dfs(int node, vector < vector > &ctc) { + ++ currentNodeLabel; + info[node] = {currentNodeLabel, currentNodeLabel}; + stack.push_back(node); + inStack[node] = 1; + for (auto &neigh : gr[node]) { + if (info[neigh].first == 0) { + dfs(neigh, ctc); + info[node].second = min(info[node].second, info[neigh].second); + } + else if (inStack[neigh] == 1) { + info[node].second = min(info[node].second, info[neigh].first); + } + } + if (info[node].first == info[node].second) { + + int current; + vector aux; + do { + current = stack.back(); + aux.push_back(current); + inStack[current] = 0; + stack.pop_back(); + }while (current != node); + ctc.push_back(aux); + } +} + +int main() { + ios :: sync_with_stdio(false); + cin.tie(nullptr); + int n, m; + cin >> n >> m; + while (m --) { + int x, y; + cin >> x >> y; + gr[x].push_back(y); + } + vector < vector > ctc; + for (int i = 1; i <= n; ++ i) { + if (info[i].first == 0) { + dfs(i, ctc); + } + } + + + + cout << ctc.size() << '\n'; + for (auto &x : ctc) { + for (auto &y : x) { + cout << y << ' '; + } + cout << '\n'; + } + return 0; +} diff --git a/scc/tarjan/73ba31dd-622a-4b53-9516-d5c262fd53c9.cpp b/scc/tarjan/73ba31dd-622a-4b53-9516-d5c262fd53c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..faf1b553aba3d2e90a3058f4ef2509c4e426ab4c --- /dev/null +++ b/scc/tarjan/73ba31dd-622a-4b53-9516-d5c262fd53c9.cpp @@ -0,0 +1,164 @@ +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +class Graph { + + private: + + int n; + + vector> ad; + + + + vector ind, low; + + vector> sccs; + + + + void dfs(int node, int& id, stack& st, vector& onStack) { + + ind[node] = low[node] = ++id; + + st.push(node); onStack[node] = true; + + + + for (int nghb : ad[node]) + + if (!ind[nghb]) { + + dfs(nghb, id, st, onStack); + + low[node] = min(low[node], low[nghb]); + + } + + else if (onStack[nghb]) + + low[node] = min(low[node], low[nghb]); + + + + if (ind[node] == low[node]) { + + sccs.emplace_back(); + + while (true) { + + int top = st.top(); + + st.pop(); onStack[top] = false; + + + + low[top] = ind[node]; + + sccs.back().push_back(top); + + + + if (top == node) + + break; + + } + + } + + } + + + + public: + + Graph(int n) : + + n(n), ad(n + 1), + + ind(n + 1), low(n + 1) { } + + + + void addEdge(int x, int y) { + + ad[x].push_back(y); + + } + + + + vector>& tarjan() { + + stack st; + + vector onStack(n + 1); + + + + int id = 0; + + for (int i = 1; i <= n; i++) + + if (!ind[i]) + + dfs(i, id, st, onStack); + + return sccs; + + } + +}; + + + +int main() { + + int n, m; + + fin >> n >> m; + + + + Graph graph(n); + + for (int i = 0; i < m; i++) { + + int x, y; fin >> x >> y; + + graph.addEdge(x, y); + + } + + + + vector>& sccs = graph.tarjan(); + + fout << sccs.size() << '\n'; + + for (vector& scc : sccs) { + + for (int node : scc) + + fout << node << ' '; + + fout << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/758aa407-377a-40df-acb4-14cfa0432494.cpp b/scc/tarjan/758aa407-377a-40df-acb4-14cfa0432494.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c53b93373906f6a48ca2a3ebdaa4abed94de736 --- /dev/null +++ b/scc/tarjan/758aa407-377a-40df-acb4-14cfa0432494.cpp @@ -0,0 +1,168 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +vector< vector> Componente; + +vectorgraph[100005]; + +vectorajut; + + + +stack S; + +int nr; + +int n, m, x, y; + +int low_link[100005]; + +int is_in_stack[100005]; + +int viz[100005]; + +int idx[100005]; + +int index = 1; + + + +void citire(){ + + ifstream f("ctc.in"); + + f>>n>>m; + + for(int i=1; i<=m; i++){ + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int i){ + + low_link[i] = idx[i] = index; + + index ++; + + + + viz[i] = 1; + + is_in_stack[i] = 1; + + S.push(i); + + + + for(auto &v:graph[i]){ + + if(viz[v] == 0){ + + tarjan(v); + + low_link[i] = min(low_link[i], low_link[v]); + + } + + else if(is_in_stack[v] == 1){ + + low_link[i] = min(low_link[i], low_link[v]); + + } + + } + + + + if(idx[i] == low_link[i]){ + + ajut.clear(); + + int el; + + do{ + + el = S.top(); + + ajut.push_back(el); + + S.pop(); + + is_in_stack[el] = 0; + + }while(el!=i); + + Componente.push_back(ajut); + + } + +} + +void parcurg_componente_conexe_graf(){ + + for(int i=1; i<=n; i++){ + + if(viz[i] == 0){ + + tarjan(i); + + } + + } + +} + + + +void afisare(){ + + ofstream g("ctc.out"); + + int m = Componente.size(); + + g< +#include +#include +#include + + +using namespace std; + + + +int N,M; + +stack Stack; + +vectorGraf[100010],Lowlink(100010,-1),Indx(100010,-1),ONstack(100010,0); + +vector> result; + + + +void Tarjan(int x) + +{ + + static int index = 0; + + Indx[x] = Lowlink[x] = index; + + index++; + + Stack.push(x); + + ONstack[x] = 1; + + + + for (int it : Graf[x]) + + { + + if (Indx[it] == -1) + + Tarjan(it); + + if (ONstack[it]) + + Lowlink[x] = min(Lowlink[x], Lowlink[it]); + + } + + if (Indx[x] == Lowlink[x]) + + { + + vector aux; + + + + while (Stack.top() != x) + + { + + aux.push_back(Stack.top()); + + ONstack[Stack.top()] = 0; + + Stack.pop(); + + } + + + + aux.push_back(Stack.top()); + + ONstack[Stack.top()] = 0; + + Stack.pop(); + + + + result.push_back(aux); + + } + +} + +int main() + +{ + + ifstream in ("ctc.in"); + + ofstream out("ctc.out"); + + + + int x,y; + + + + in>>N>>M; + + for( int i = 0; i < M; ++i) + + { + + in>>x>>y; + + Graf[x].push_back(y); + + + + } + + + + for(int i = 1; i <=N; ++i) + + if (Indx[i] == -1) + + Tarjan(i); + + + + + + out << result.size() << "\n"; + + + + for (auto it : result) + + { for (int x : it) + + out<< x << " "; + + out << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/776a1cfc-ffcd-4769-b00a-2b1024843e2f.cpp b/scc/tarjan/776a1cfc-ffcd-4769-b00a-2b1024843e2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be5e7d7e3881ca6bb850f189dd022b8289b1e057 --- /dev/null +++ b/scc/tarjan/776a1cfc-ffcd-4769-b00a-2b1024843e2f.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,idx[100005],low[100005],t,st[100005],k,nrc,ap[100005]; + +vector v[100005]; + +vector comp[100005]; + +void dfs(int nod) + +{ + + idx[nod]=t; + + low[nod]=t; + + t++; + + st[++k]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + if(idx[it]==0) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + if(low[nod]==idx[nod]) + + { + + nrc++; + + while(st[k]!=nod) + + { + + ap[st[k]]=0; + + comp[nrc].push_back(st[k]); + + k--; + + } + + comp[nrc].push_back(nod); + + ap[st[k]]=0; + + k--; + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) + + if(idx[i]==0) + + { + + t=1; + + k=0; + + dfs(i); + + } + + out< +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graf[100005]; + +int n, m, index; + +vector > totSol; + +bool viz[100005], inStack[100005]; + +int low[100005], niv[100005]; + +stack S; + + + +void citire() + +{ + + f>>n>>m; + + int x, y; + + for(int i = 0; i>x>>y; + + graf[x].push_back(y); + + } + +} + + + +void dfs(int currNode) + +{ + + viz[currNode] = true; + + S.push(currNode); + + inStack[currNode] = true; + + low[currNode] = index; + + niv[currNode] = index; + + index++; + + for(int i : graf[currNode]) + + { + + if(!viz[i]) + + { + + dfs(i); + + } + + if(inStack[i]) + + low[currNode] = min(low[i], low[currNode]); + + } + + if(low[currNode] == niv[currNode]) + + { + + vector sol; + + int el; + + do + + { + + el = S.top(); + + S.pop(); + + sol.push_back(el); + + inStack[el] = false; + + }while(el != currNode); + + totSol.push_back(sol); + + } + +} + + + +void parcurgere() + +{ + + for(int i = 1; i<=n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + +} + + + +void afisare() + +{ + + g< +#include +#include + + +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out ("ctc.out"); + + + +const int LIM = 1e5 + 7; + + + +void tarjan ( int nod ); + + + +vector < int > g[LIM]; + + + +vector < int > ctc[LIM]; + + + +stack < int > s; + + + +int n, m; + + + +int x, y; + + + +int viz[LIM]; + + + +int iss[LIM]; + + + +int niv[LIM]; + + + +int l[LIM]; + + + +int ct; + + + +int sorin; + + + +int main() + +{ + + in >> n >> m; + + for ( int i = 1 ; i <= m ; ++i ) + + { + + in >> x >> y; + + g[x].push_back (y); + + } + + for ( int i = 1 ; i <= n ; ++i ) + + if ( !viz[i] ) + + tarjan (i); + + out << sorin << '\n'; + + for ( int i = 1 ; i <= sorin ; ++i ) + + { + + for ( auto y : ctc[i] ) + + out << y << " "; + + out << '\n'; + + } + + return 0; + +} + + + +void tarjan ( int nod ) + +{ + + iss[nod] = true; + + viz[nod] = true; + + s.push(nod); + + niv[nod] = l[nod] = ++ct; + + for ( auto y : g[nod] ) + + { + + if ( !viz[y] ) + + { + + tarjan (y); + + l[nod] = min ( l[nod], l[y] ); + + } + + else + + { + + if ( iss[y] ) + + l[nod] = min ( l[nod], niv[y] ); + + } + + } + + if ( niv[nod] == l[nod] ) + + { + + ++sorin; + + while ( !s.empty () ) + + { + + ctc[sorin].push_back ( s.top () ); + + iss[s.top ()] = false; + + if ( s.top () == nod ) + + { + + s.pop (); + + break; + + } + + s.pop (); + + } + + } + +} diff --git a/scc/tarjan/7b64badd-f0cb-4f25-b01c-2ecf6d0dabd1.cpp b/scc/tarjan/7b64badd-f0cb-4f25-b01c-2ecf6d0dabd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8172fb23608ca3901181e5c11748a8a7199034dd --- /dev/null +++ b/scc/tarjan/7b64badd-f0cb-4f25-b01c-2ecf6d0dabd1.cpp @@ -0,0 +1,145 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +vector arb[DMAX]; + +vector ans[DMAX]; + + + +stack s; + + + +int uz[DMAX]; + +int lowlink[DMAX]; + + + +int n,m,cnt; + +int comp; + + + +void dfsT(int node); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + arb[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!uz[i]) + + dfsT(i); + + fout< 0) + + lowlink[node]=min(lowlink[node],lowlink[it]); + + } + + if(uz[node] == lowlink[node]){ + + comp++; + + do{ + + int node2=s.top(); + + s.pop(); + + ans[comp].push_back(node2); + + uz[node2]=-1; + + if(node2 == node) + + break; + + }while(true); + + } + +} diff --git a/scc/tarjan/7b78ac85-6268-4973-aa67-483f21b795c3.cpp b/scc/tarjan/7b78ac85-6268-4973-aa67-483f21b795c3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6976975f2cad75bf2a48e29ce4cca66c3eec6536 --- /dev/null +++ b/scc/tarjan/7b78ac85-6268-4973-aa67-483f21b795c3.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +const int N=100005; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,x,y; + +vector vecini[N]; + +int id[N],low[N],a[N],k,inq[N],s; + +int nrctc,nodes[N],ctcsize[N]; + + + +void dfs(int x) + +{ + + ++k; ++s; + + low[x]=id[x]=k; a[s]=x; inq[x]=1; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) f>>x>>y, vecini[x].push_back(y); + + for(int i=1;i<=n;++i) if(!id[i]) + + { + + k=0; s=0; + + dfs(i); + + } + + g< +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int cnt, nrctc, n, m, mini[100000 +10], idx[100000 +10], inStack[100000 +10]; + +stack stiva; + +vector nod[100000 +10], ctc[100000 +10]; + + + +void dfs(int x) + +{ cnt++; + + mini[x] = idx[x] = cnt; + + stiva.push(x); + + inStack[x] = 1; + + for(int i=0; i> n >> m; + + for(int i=1; i<=m; i++) + + { int nod1, nod2; + + f >> nod1 >> nod2; + + nod[nod1].push_back(nod2); + + } + + for(int i=1; i<=n; i++) + + if(!idx[i]) dfs(i); + + g << nrctc << '\n'; + + for(int i=1; i<=nrctc; i++) + + { for(int j=0; j +#include +#include +#include + +using namespace std; + + + +ofstream out("ctc.out"); +ifstream in("ctc.in"); + +int n, m; + +vector graph[100001]; + +int isStack[100001]; + +stack s; + +bool visited[100001]; + +int tm[100001]; + +int boss[100001]; + +int t = 0; + +vector sol[100001]; + +int sz = 0; + +void dfsScc(int v) { + visited[v] = true; + + t++; + + boss[v] = t; + + tm[v] = t; + + isStack[v] = true; + + s.push(v); + + for (int i = 0; i < graph[v].size(); i++) { + if (!visited[graph[v][i]]) { + dfsScc(graph[v][i]); + } + + if (isStack[graph[v][i]] && boss[graph[v][i]] < boss[v]) { + boss[v] = boss[graph[v][i]]; + } + } + + if (tm[v] == boss[v]) { + while (s.top() != v) { + int curr = s.top(); + + s.pop(); + + isStack[curr] = false; + + sol[sz].push_back(curr + 1); + + + } + + if (s.top() == v) { + isStack[s.top()] = false; + + sol[sz].push_back(s.top() + 1); + + + + s.pop(); + } + + sz++; + + + } +} + +int main() { + in >> n >> m; + + for (int i = 0; i < m; i++) { + int u, v; + + in >> u >> v; + + u--; + + v--; + + graph[u].push_back(v); + } + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + dfsScc(i); + } + } + + out << sz << '\n'; + + for (int i = 0; i < sz; i++) { + for (int j = 0; j < sol[i].size(); j++) { + out << sol[i][j] << " "; + } + + out << '\n'; + } + + return 0; +} diff --git a/scc/tarjan/7c5c2e36-f563-4b68-b696-9c4d062616f4.cpp b/scc/tarjan/7c5c2e36-f563-4b68-b696-9c4d062616f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd8185cc0b9fa331dc2c922bf748f1470c5084a --- /dev/null +++ b/scc/tarjan/7c5c2e36-f563-4b68-b696-9c4d062616f4.cpp @@ -0,0 +1,90 @@ +#include + +using namespace std; + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + +vector> adiacenta(100001); +int ids[100001], low[100001]; +bool onStack[100001]; +stack stck; +int n, m, id = 0; +vector> rez; + +void dfs(int node) { + stck.push(node); + onStack[node] = true; + ids[node] = low[node] = id++; + + for(int x : adiacenta[node]) { + if(ids[x] == -1) { + dfs(x); + low[node] = min(low[node], low[x]); + } + else if(onStack[x]) + low[node] = min(low[node], ids[x]); + } + + vector v_aux; + int aux; + + if(ids[node] == low[node]) { + + while(stck.top() != node) { + + aux = stck.top(); + stck.pop(); + + onStack[aux] = false; + v_aux.push_back(aux); + + } + + aux = stck.top(); + v_aux.push_back(aux); + stck.pop(); + + rez.push_back(v_aux); + onStack[aux] = false; + } +} + +void findSccs() { + for(int i = 1; i <= n; i++) { + ids[i] = -1; + } + + for(int i = 1; i <= n; i++) + if(ids[i] == -1) + dfs(i); +} + + +int main() { + int x, y; + f >> n >> m; + + adiacenta.resize(n); + + for(int i = 0; i < n; i++) + onStack[i] = false; + + for(int i = 0; i < m; i++) { + f >> x >> y; + adiacenta[x].push_back(y); + } + + findSccs(); + g << rez.size() << '\n'; + + for(auto &i : rez) { + for (auto j : i) + g << j << " "; + g << '\n'; + } + + f.close(); + g.close(); + return 0; +} diff --git a/scc/tarjan/7e84e843-7444-4c12-b2d0-cc877dbc53bd.cpp b/scc/tarjan/7e84e843-7444-4c12-b2d0-cc877dbc53bd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f8246581d921b5dfac3f91308cfab27c4d0f0fa --- /dev/null +++ b/scc/tarjan/7e84e843-7444-4c12-b2d0-cc877dbc53bd.cpp @@ -0,0 +1,83 @@ +#include + +const int MAXN = 100005; + +using namespace std; + +struct tarjan_node { + int index, lowlink, on_stack; +}; + +void tarjan_DFS(int node, vector *Graph, vector > &output, stack &s, tarjan_node *nodes) { + static int pos = 0; + nodes[node].index = nodes[node].lowlink = ++pos; + nodes[node].on_stack = 1; + s.push(node); + + for(long unsigned int i = 0; i < Graph[node].size(); i++) { + int son = Graph[node][i]; + + if(nodes[son].index == 0) { + tarjan_DFS(son, Graph, output, s, nodes); + nodes[node].lowlink = min(nodes[node].lowlink, nodes[son].lowlink); + } else if(nodes[son].on_stack == 1) { + nodes[node].lowlink = min(nodes[node].lowlink, nodes[son].lowlink); + } + } + + if(nodes[node].lowlink == nodes[node].index) { + vector SCC; + int new_node; + + do { + new_node = s.top(); + s.pop(); + nodes[new_node].on_stack = 0; + SCC.push_back(new_node); + } while(new_node != node); + + output.push_back(SCC); + } +} + +void tarjan(int N, vector *Graph, vector > &output) { + stack s; + tarjan_node nodes[MAXN]; + memset(nodes, 0, sizeof(nodes)); + + for(int i = 1; i <= N; i++) + if(nodes[i].index == 0) + tarjan_DFS(i, Graph, output, s, nodes); +} + +int main() { + FILE *in = fopen("ctc.in", "r"); + FILE *out = fopen("ctc.out", "w"); + + int N, M, X, Y; + vector Graph[MAXN]; + vector > output; + + fscanf(in, "%d %d", &N, &M); + + for(int i = 1; i <= M; i++) { + fscanf(in, "%d %d", &X, &Y); + Graph[X].push_back(Y); + } + + tarjan(N, Graph, output); + + fprintf(out, "%lu\n", output.size()); + + for(long unsigned int i = 0; i < output.size(); i++) { + for(long unsigned int j = 0; j < output[i].size(); j++) { + fprintf(out, "%d ", output[i][j]); + } + fprintf(out, "\n"); + } + + fclose(in); + fclose(out); + + return 0; +} diff --git a/scc/tarjan/7f180c16-0b42-4629-b3b6-7abcf4da15e4.cpp b/scc/tarjan/7f180c16-0b42-4629-b3b6-7abcf4da15e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c428bf29138ed23dbcb5d975536ab4402034d50 --- /dev/null +++ b/scc/tarjan/7f180c16-0b42-4629-b3b6-7abcf4da15e4.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nMax = 100010; + + + +int nrctc, t; + +vector v[nMax], ct[nMax], disc, low, isInStack; + +stack s; + + + +void ctc(int nod) + +{ + + t++; + + disc[nod] = low[nod] = t; + + s.push(nod); + + isInStack[nod] = true; + + + + for(auto it : v[nod]) + + { + + if(!disc[it]) + + { + + ctc(it); + + low[nod] = min(low[nod], low[it]); + + } + + else if(isInStack[it])low[nod] = min(low[nod], disc[it]); + + } + + + + if(low[nod] == disc[nod]) + + { + + while(s.top() != nod) + + { + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + } + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + nrctc++; + + } + + + +} + + + +int main() + +{ + + int n, m; fin >> n >> m; + + disc.assign(n, 0); + + low.assign(n, 0); + + isInStack.assign(n, 0); + + for(int i=0; i> x >> y; x--; y--; + + v[x].push_back(y); + + } + + for(int i=0; i + + +using namespace std; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + +struct nodS + +{ + + int i, low; + + bool on; + + vector vecini; + +}; + +int n,m,x,y,i,t; + +stack s; + +vector < vector > ans; + +nodS nod[100005]; + +void dfs(int poz) + +{ + + t++; + + nod[poz].i=t; + + nod[poz].low=t; + + nod[poz].on=true; + + s.push(poz); + + + + for(int it : nod[poz].vecini) + + { + + if(!nod[it].i) + + { + + dfs(it); + + nod[poz].low=min(nod[poz].low, nod[it].low); + + } + + else if(nod[it].on) + + { + + nod[poz].low=min(nod[poz].low, nod[it].i); + + } + + } + + + + if(nod[poz].i==nod[poz].low) + + { + + vector nscc; + + while(!s.empty()) + + { + + int it=s.top(); + + nod[it].on=false; + + nscc.push_back(it); + + s.pop(); + + if(it==poz) + + break; + + } + + ans.push_back(nscc); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m) + + { + + m--; + + fin>>x>>y; + + nod[x].vecini.push_back(y); + + } + + + + for(i=1;i<=n;i++) + + { + + if(!nod[i].i) + + dfs(i); + + } + + + + fout< + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorv[100001],sol[100001]; + +int n,m,k,t,low[100001],disc[100001]; + +bool fol[100001]; + +stacksk; + + + +void dfs(int nod) + +{ + + t++; + + sk.push(nod); + + fol[nod]=true; + + disc[nod]=low[nod]=t; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y; + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;++i) + + if(!disc[i]) + + { + + t = 0; + + dfs(i); + + } + + + + g< +#include +#include +#include +#include +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector g[100002]; +int discoverTime[100002]; +int lowTime[100002]; +bool onStack[100002]; +int id; +stack s; +vector rasp[100000]; +int n, m, nr_componente; + +void dfs(int from) +{ + id++; + s.push(from); + onStack[from] = true; + discoverTime[from] = lowTime[from] = id; + + for (auto &to : g[from]) + { + if (discoverTime[to] == 0) + { + dfs(to); + } + if (onStack[to]) + { + lowTime[from] = min(lowTime[from], lowTime[to]); + } + } + if (lowTime[from] == discoverTime[from]) + { + while (s.top() != from) + { + rasp[nr_componente].push_back(s.top()); + lowTime[s.top()] = min(lowTime[from], lowTime[s.top()]); + onStack[s.top()] = false; + s.pop(); + } + rasp[nr_componente].push_back(s.top()); + onStack[s.top()] = false; + s.pop(); + ++nr_componente; + } +} +int main() +{ + in >> n >> m; + for (int i = 0; i < m; i++) + { + int x, y; + in >> x >> y; + g[x].push_back(y); + } + for (int i = 1; i <= n; i++) + { + if (discoverTime[i] == 0) + { + dfs(i); + } + } + out << nr_componente << '\n'; + for (int i = 0; i < nr_componente; i++) + { + for (auto &el : rasp[i]) + { + out << el << ' '; + } + out << '\n'; + } +} diff --git a/scc/tarjan/82004f1f-8674-434e-9900-62582241b133.cpp b/scc/tarjan/82004f1f-8674-434e-9900-62582241b133.cpp new file mode 100644 index 0000000000000000000000000000000000000000..73656449c87f11921167d9c9936c3ecf24566776 --- /dev/null +++ b/scc/tarjan/82004f1f-8674-434e-9900-62582241b133.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + +const int NMAX=100005; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +vector g[NMAX],c; + +stacks; + +vector>ans; + +int n,m,f,low[NMAX],in_stack[NMAX],val[NMAX]; + +int idx=1; + +void tarjan(int node){ + + val[node]=low[node]=idx; + + s.push(node), in_stack[node]=1; + + idx++; + + for(auto y:g[node]){ + + if(!val[y]) + + tarjan(y),low[node]=min(low[node],low[y]); + + else if(in_stack[y]) + + low[node]=min(low[node],low[y]); + + } + + if(val[node]==low[node]){ + + c.clear(); + + int n; + + do{ + + c.push_back(n=s.top()); + + in_stack[n]=0; + + s.pop(); + + }while(n!=node); + + ans.push_back(c); + + } + +} + +int main() + +{ + + in>>n; + + int x,y; + + for(in>>m;m>0;m--){ + + in>>x>>y; + + g[x].push_back(y); + + } + + for(int i=1;i<=n;i++){ + + if(!val[i]) + + tarjan(i); + + } + + out< +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,nrOrd; + +vector ord; + +vector lowLink; + +vectorinStack; + +stack st; + + + +vector> v; + +vector> sccs; + + + +void Read() + +{ + + f>>n>>m; + + v.resize(m+1); + + ord.resize(n+1); + + lowLink.resize(n+1); + + inStack.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + v[x].emplace_back(y); + + } + + f.close(); + +} + + + +void dfs(int nod) + +{ + + st.push(nod); + + inStack[nod] = true; + + lowLink[nod] = ord[nod] = ++nrOrd; + + + + for(auto& vecin : v[nod]) + + { + + if(!ord[vecin]) + + { + + dfs(vecin); + + lowLink[nod] = min(lowLink[nod], lowLink[vecin]); + + } + + else if(inStack[vecin]) + + lowLink[nod] = min(lowLink[nod], lowLink[vecin]); + + } + + + + if(lowLink[nod] == ord[nod]) + + { + + sccs.emplace_back(); + + while(true) + + { + + int top = st.top(); + + st.pop(); + + inStack[top] = false; + + + + lowLink[top] = ord[nod]; + + sccs.back().push_back(top); + + if(top == nod) + + break; + + } + + } + +} + + + +int main() + +{ + + Read(); + + dfs(1); + + for(int i=1;i<=n;i++) + + if(!ord[i]) + + dfs(i); + + + + g< +#include +#include + + +using namespace std; + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +vector graf[100005]; + +int low[100005],id[100005]; + +bool onstack[100005]; + +vector> sol; + +stack s; + +int n,m,a,b; + +int idc = 0; + +void dfs(int x) + +{ + + id[x] = idc; + + low[x] = idc++; + + s.push(x); + + onstack[x] = 1; + + for(auto it:graf[x]){ + + if(id[it]==-1) + + dfs(it); + + if(onstack[it]) + + low[x] = min(low[x],low[it]); + + } + + if(id[x] == low[x]){ + + int node; + + vector aux; + + do{ + + node = s.top(); + + aux.push_back(node); + + low[node] = id[x]; + + onstack[node] = 0; + + s.pop(); + + }while(node != x); + + sol.push_back(aux); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=0;i>a>>b; + + graf[a-1].emplace_back(b-1); + + } + + for(int i=0;i + + +int c, N, M; + +std::vector > graph; + +inline void addEdge(int u, int v) { + + graph[u].push_back(v); + +} + + + +class SCCCalculator { + +public: + + SCCCalculator (std::vector > &graph) { calculate(graph); } + + void calculate(std::vector > &graph) { + + _time = 0; + + N = graph.size(); + + low .resize(graph.size(), 0); + + disc .resize(graph.size(), 0); + + inStack.resize(graph.size(), false); + + for (int i=0; i> getComponents() { return components; } + + + + std::vector > getSCCGraph(std::vector >& graph) { + + std::vector > SCCGraph; + + SCCGraph.resize(components.size()); + + std::vector seen(components.size(), false); + + for (int i=0; i> &graph, int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + for (auto &it:graph[vertex]) { + + if (!disc[it]) { DFS(graph, it); + + low[vertex] = std::min(low[vertex], low[it]); + + } else + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + if (low[vertex] == disc[vertex]) addComponent(vertex); + + } + + + + void addComponent(int vertex) { + + components.push_back(std::vector ()); + + while (!stack.empty() && stack.top() != vertex) { + + components.back().push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } components.back().push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + + + int _time; + + int N; + + std::vector low, disc, comp; + + std::vector > components; + + std::vector inStack; + + std::stack stack; + +}; + + + +std::ifstream input ("ctc.in"); + +std::ofstream output("ctc.out"); + + + +int main() + +{ + + input >> N >> M; + + graph.resize(N, std::vector ()); + + for (int i=0, u, v; i> u >> v, addEdge(u-1, v-1); + + + + SCCCalculator calc(graph); + + output << calc.getSize() << '\n'; + + for (auto &it:calc.getComponents()) { + + for (auto &it2:it) output << it2+1 << ' '; + + output << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/882f1b46-1bd5-4ae8-81f7-4482f2c45352.cpp b/scc/tarjan/882f1b46-1bd5-4ae8-81f7-4482f2c45352.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d065db220ef284c3718fa01758fd3e8fdddfec5b --- /dev/null +++ b/scc/tarjan/882f1b46-1bd5-4ae8-81f7-4482f2c45352.cpp @@ -0,0 +1,273 @@ +#include + + + + + + +using namespace std; +const int NMAX=1e5+10; + + + +vector < int > Edge[NMAX], Components[NMAX]; + + + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + + + +bitset < NMAX > Seen, Belongs; + + + +stack < int > Stack; + + + + + + + +void Read() + + + +{ + + freopen("ctc.in", "r", stdin); + + scanf("%d%d", &n, &m); + + + + + + int x, y; + + + + for(int i=1; i<=m; i++) + + + + { + + + + scanf("%d%d", &x, &y); + + Edge[x].push_back(y); + + } + + + +} + + + + + + + +void Tarjan(int x) + + + +{ + + + + niv++; + + + + Seen[x]=1; + + + + Belongs[x]=1; + + + + Stack.push(x); + + + + RootLevel[x]=niv; + + + + NodeLevel[x]=niv; + + + + for(int i : Edge[x]) + + + + { + + + + if(!Seen[i]) + + + + { + + + + Tarjan(i); + + + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + + + } + + + + else + + + + if(Belongs[i]) + + + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + + + } + + + + if(RootLevel[x]==NodeLevel[x]) + + + + { + + + + nrCMP++; + + + + int node=0; + + + + do + + + + { + + + + node=Stack.top(); + + + + Stack.pop(); + + + + Components[nrCMP].push_back(node); + + + + Belongs[node]=0; + + + + } + + while(node!=x); + + + + } + + + +} + + + + + + + +int main() + + + +{ + + + + Read(); + + + + for(int i=1; i<=n; i++) + + + + if(!Seen[i]) + + + + niv=0, Tarjan(i); + + freopen("ctc.out", "w", stdout); + + printf("%d", nrCMP); + + printf("\n"); + + + + + + for(int i=1; i<=nrCMP; i++) + + + + { + + + + for(int it : Components[i]) + + printf("%d ", it); + + + + printf("\n"); + + + + + + } + + + + return 0; + + + + + + + +} diff --git a/scc/tarjan/88911e3e-393a-4710-a932-185abf1a984a.cpp b/scc/tarjan/88911e3e-393a-4710-a932-185abf1a984a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..014a1531a04c33c50091bfbfe96ad45329cb519c --- /dev/null +++ b/scc/tarjan/88911e3e-393a-4710-a932-185abf1a984a.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +using namespace std; + +using VI = vector; +using VVI = vector; +using VB = vector; + +int n, m; +VVI G, ctc; +VI niv, L, c; +VB inStk; +stack stk; + +void Read(); +void Tarjan(int x, int& level); +void Write(); + +int main() +{ + Read(); + int level{1}; + for (int i = 1; i <= n; i++) + if (!niv[i]) + Tarjan(i, level); + Write(); +} + +void Tarjan(int x, int& level) +{ + niv[x] = L[x] = level; + level++; + stk.push(x); + inStk[x] = true; + + for (const int& y : G[x]) + { + if (!niv[y]) + { + Tarjan(y, level); + L[x] = min(L[x], L[y]); + } + else + if (inStk[y]) + L[x] = min(L[x], niv[y]); + } + + if (L[x] == niv[x]) + { + c = VI(); + while (!stk.empty()) + { + int y = stk.top(); + stk.pop(); + inStk[y] = false; + c.emplace_back(y); + if (y == x) + break; + } + ctc.emplace_back(c); + } +} + +void Write() +{ + ofstream fout("ctc.out"); + fout << ctc.size() << '\n'; + for (const VI& c : ctc) + { + for (const int& x : c) + fout << x << ' '; + fout << '\n'; + } +} + +void Read() +{ + ifstream fin("ctc.in"); + fin >> n >> m; + G = VVI(n + 1); + niv = L = VI(n + 1); + inStk = VB(n + 1); + + int x, y; + for (int i = 0; i < m; i++) + { + fin >> x >> y; + G[x].emplace_back(y); + } +} diff --git a/scc/tarjan/88a9b971-7a26-4df4-aa56-248161930354.cpp b/scc/tarjan/88a9b971-7a26-4df4-aa56-248161930354.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b57d4dfc3ff5a003ec3a8241e8c819a18195b075 --- /dev/null +++ b/scc/tarjan/88a9b971-7a26-4df4-aa56-248161930354.cpp @@ -0,0 +1,64 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int oo=INT_MAX/2; + +typedef vector vi; + +int n,m,k; +vector g; + +vi lowlink, idx, inStk; +vi stk; + +vector ctcs; + +int _index=1; + +void tarjan(int nd, int id=1){ + lowlink[nd]=idx[nd]=_index++; + inStk[nd]=1; + stk.push_back(nd); + + for(auto vc:g[nd]){ + if(idx[vc]==0){ + tarjan(vc,id+1); + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + else if(inStk[vc]){ + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + } + if(lowlink[nd]==idx[nd]){ + cout<>n>>m; + g.resize(n+1), lowlink.resize(n+1), idx.resize(n+1,0), inStk.resize(n+1,0); + for(int i=m;i--;){ + int x,y; fin>>x>>y; + g[x].push_back(y); + } + for(int i=1;i<=n;++i) + if(idx[i]==0) tarjan(i); + fout< +#include +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n, iname); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C, oname); + + + + return 0; + +} diff --git a/scc/tarjan/8ab01792-097e-4eed-8879-fde9c5ee2769.cpp b/scc/tarjan/8ab01792-097e-4eed-8879-fde9c5ee2769.cpp new file mode 100644 index 0000000000000000000000000000000000000000..598d09499000689e256014e6f73a08ff6adc6e82 --- /dev/null +++ b/scc/tarjan/8ab01792-097e-4eed-8879-fde9c5ee2769.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +using namespace std; + + + +int n,m,v[100000],nrdf[100000],inst[100000]; + +vector l[100000]; + +int c,ndf,pozc=0;; + + + +struct Muchie{ + + int i,j; + +} ; + + + +stack s; + + + +vector comp; + + + +void bloc(int x,int tata){ + + int u; + + s.push(x); + + ndf++; + + nrdf[x]=ndf; + + v[x]=ndf; + + inst[x]=1; + + for(int t=0;tv[j]) + + v[x]=v[j]; + + } + + else + + if(inst[j]){ + + if(v[x]>nrdf[j]) + + v[x]=nrdf[j]; + + } + + } + + + + if (v[x] == nrdf[x]) { + + do { + + u = s.top(); + + comp.push_back(u); + + s.pop(); + + inst[u]=0; + + + + } + + while (u != x); + + + + c++; + + comp.push_back(-1); + + } + + + + } + +void bloc(){ + + int i; + + c=0; + + for(i=0;i=0) + + fprintf(f,"%d ",comp[j]+1); + + else + + fprintf(f,"\n"); + + } + + + + + + fclose(f); + return 0; + +} diff --git a/scc/tarjan/8d585f80-134a-4c50-8b2e-534f11f927ac.cpp b/scc/tarjan/8d585f80-134a-4c50-8b2e-534f11f927ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62eb87bcf59103b0dfb4298330165fbc8f6fe577 --- /dev/null +++ b/scc/tarjan/8d585f80-134a-4c50-8b2e-534f11f927ac.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,a1,b1,ap[100005],ind,indice[100005],t; + +bool b[100005]; + +vector v[100005]; + +vector sol1[100005]; + +stack s; + +void sol(int nod) + +{ + + indice[nod]=++ind; + + ap[nod]=ind; + + s.push(nod); + + b[nod]=1; + + for(auto i:v[nod]) + + { + + if(indice[i]==0) + + { + + sol(i); + + ap[nod]=min(ap[nod],ap[i]); + + } + + else if(b[i]) + + { + + ap[nod]=min(ap[nod],ap[i]); + + } + + } + + if(ap[nod]==indice[nod]) + + { + + t++; + + while(s.top()!=nod) + + { + + sol1[t].push_back(s.top()); + + b[s.top()]=0; + + s.pop(); + + } + + b[s.top()]=0; + + sol1[t].push_back(s.top()); + + s.pop(); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>a1>>b1; + + v[a1].push_back(b1); + + } + + for(int i=1;i<=n;i++) + + { + + if(indice[i]==0) + + { + + sol(i); + + } + + } + + out< + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +const int MAXN = 100005; + + + +bool inSt[MAXN]; + +int ll[MAXN], mark[MAXN], n, m, val, nr; + +vector graph[MAXN], comp[MAXN]; + +stack st; + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; ++i) { + + int x, y; + + fin >> x >> y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int node) + +{ + + st.push(node); + + inSt[node] = true; + + mark[node] = ll[node] = ++val; + + for (const auto& it: graph[node]) + + if (mark[it] == 0) { + + tarjan(it); + + ll[node] = min(ll[node], ll[it]); + + } + + else if (inSt[it] == true) + + ll[node] = min(ll[node], mark[it]); + + if (ll[node] == mark[node]) { + + while (!st.empty()) { + + int top = st.top(); + + st.pop(); + + inSt[top] = false; + + comp[nr].push_back(top); + + if (top == node) + + break; + + } + + ++nr; + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + for (int i = 0; i < nr; ++i) { + + for (const auto &it: comp[i]) + + fout << it << ' '; + + fout << '\n'; + + } + +} + + + +int main() + +{ + + read(); + + for (int i = 1; i <= n; ++i) + + if (mark[i] == 0) + + tarjan(i); + + print(); + + return 0; + +} diff --git a/scc/tarjan/901b4051-b570-4d0b-92f1-77b1197fc116.cpp b/scc/tarjan/901b4051-b570-4d0b-92f1-77b1197fc116.cpp new file mode 100644 index 0000000000000000000000000000000000000000..148a880a100c62bd8f3d3829d1220bbaf67a8bdd --- /dev/null +++ b/scc/tarjan/901b4051-b570-4d0b-92f1-77b1197fc116.cpp @@ -0,0 +1,142 @@ +#include +using namespace std; + + + +const int maxN=(1e5)+5; + + + +int niv,lvl[maxN],low[maxN],st[maxN],vf,ctc; + + + +vector v[maxN],C[maxN]; + +bool inStack[maxN]; + +void dfs(int nod) + +{ + + niv++; + + low[nod]=niv; + + lvl[nod]=niv; + + st[++vf]=nod; + + inStack[nod]=1; + + + + for(auto it:v[nod]) + + { + + if(!lvl[it]) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else + + if(inStack[it]) + + { + + low[nod]=min(low[nod],low[it]); + + } + + } + + + + if(low[nod]==lvl[nod]) + + { + + ctc++; + + int x=-1; + + while(x!=nod) + + { + + x=st[vf]; + + inStack[x]=0; + + vf--; + + C[ctc].push_back(x); + + } + + } + +} + +int n,m,x,y; + +int main() + +{ + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d",&x,&y); + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;i++) + + if(!lvl[i]) dfs(i); + + + + printf("%d\n",ctc); + + + + for(int i=1;i<=ctc;i++) + + { + + for(auto it:C[i]) + + printf("%d ",it); + + + + printf("\n"); + + } + + return 0; + +} diff --git a/scc/tarjan/909f665f-3ee8-4864-8df1-78e77fd23c9d.cpp b/scc/tarjan/909f665f-3ee8-4864-8df1-78e77fd23c9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cad53c024d285af839d41860c8b2474338509911 --- /dev/null +++ b/scc/tarjan/909f665f-3ee8-4864-8df1-78e77fd23c9d.cpp @@ -0,0 +1,223 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +class Graph { + + int vertices; + + list *adj; + + + + vector > ans; + + + +public: + + Graph(int v) : vertices(v) { + + adj = new list [v]; + + } + + + + void addEdge(int from, int to) { + + adj[from].push_back(to); + + } + + + + void depthFirst(int i, vector &disc, vector &low, stack &st, vector &stackMember) { + + static int id = 0; + + + + disc[i] = low[i] = ++id; + + st.push(i); + + stackMember[i] = true; + + + + for (auto &adjVal : adj[i]) { + + if (!disc[adjVal]) { + + depthFirst(adjVal, disc, low, st, stackMember); + + + + low[i] = min(low[i], low[adjVal]); + + } + + + + else + + if (stackMember[adjVal]) + + low[i] = min(low[i], disc[adjVal]); + + } + + + + if (low[i] == disc[i]) { + + vector newAns; + + + + while (st.top() != i) { + + int topVal = st.top(); + + st.pop(); + + stackMember[topVal] = false; + + newAns.push_back(topVal); + + } + + + + int topVal = st.top(); + + st.pop(); + + stackMember[topVal] = false; + + newAns.push_back(topVal); + + + + ans.push_back(newAns); + + } + + } + + + + void findSCC() { + + vector disc(vertices), low(vertices); + + vector stackMember(vertices); + + stack st; + + + + for (int i = 1; i < vertices; ++i) { + + if (!disc[i]) + + depthFirst(i, disc, low, st, stackMember); + + } + + } + + + + void printData() { + + out << ans.size() << "\n"; + + + + for (size_t j = 0; j < ans.size(); ++j) { + + sort(ans[j].begin(), ans[j].end()); + + for (size_t i = 0; i < ans[j].size(); ++i) { + + out << ans[j][i]; + + if (i + 1 != ans[j].size()) + + out << " "; + + } + + if (j + 1 != ans.size()) + + out << "\n"; + + } + + } + + + +}; + + + +int main() { + + + + int N, M; + + + + in >> N >> M; + + + + Graph *myGraph = new Graph(N + 1); + + + + while (M--) { + + int from, to; + + in >> from >> to; + + myGraph -> addEdge(from, to); + + } + + + + myGraph -> findSCC(); + + + + myGraph -> printData(); + + + + in.close(); + + out.close(); + + return 0; + +} diff --git a/scc/tarjan/90fce4a4-c555-4f97-9076-f6dcce18517d.cpp b/scc/tarjan/90fce4a4-c555-4f97-9076-f6dcce18517d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e25dd11a1e6fd951b4d831944935fc0016131c5b --- /dev/null +++ b/scc/tarjan/90fce4a4-c555-4f97-9076-f6dcce18517d.cpp @@ -0,0 +1,206 @@ +#include +using namespace std; + + + + + +class Task { + + public: + + void solve() { + + read_input(); + + get_result(); + + print_output(); + + } + + + + private: + + vector adj[100009]; + + vector found; + + vector low_link; + + vector in_stack; + + vector> ctc; + + stack st; + + int n, m; + + + + void read_input() { + + ifstream fin("ctc.in"); + + fin >> n >> m; + + + + found = vector(n + 1, -1); + + low_link = vector(n + 1, 0); + + in_stack = vector(n + 1, 0); + + + + for (int i = 1; i <= m; ++i) { + + int x, y; + + fin >> x >> y; + + + + adj[x].push_back(y); + + + + } + + fin.close(); + + } + + + + void get_result() { + + int current_start = 0; + + + + for (int i = 1; i <= n; ++i) { + + if (found[i] == -1) { + + tarjan(i, current_start); + + } + + } + + } + + + + void tarjan(int node, int ¤t_start) { + + found[node] = current_start; + + low_link[node] = current_start; + + + + in_stack[node] = 1; + + st.push(node); + + + + ++current_start; + + + + for (int neighbour : adj[node]) { + + if (found[neighbour] == -1) { + + tarjan(neighbour, current_start); + + low_link[node] = min(low_link[node], low_link[neighbour]); + + } else { + + if (in_stack[neighbour] == 1) { + + low_link[node] = min(low_link[node], found[neighbour]); + + } + + } + + } + + + + if (low_link[node] == found[node]) { + + int current; + + vector new_ctc; + + + + while (!st.empty() && current != node) { + + current = st.top(); + + st.pop(); + + new_ctc.push_back(current); + + + + in_stack[current] = 0; + + } + + ctc.push_back(new_ctc); + + } + + } + + + + void print_output() { + + ofstream fout("ctc.out"); + + fout << ctc.size() << "\n"; + + for (vector comp : ctc) { + + for (int el : comp) { + + fout << el << " "; + + } + + fout << "\n"; + + } + + fout.close(); + + } + +}; + + + +int main() { + + Task *task = new Task(); + + task->solve(); + + delete task; + + + + return 0; + +} diff --git a/scc/tarjan/9142b5ca-f019-408d-aa5a-9af91826fb5a.cpp b/scc/tarjan/9142b5ca-f019-408d-aa5a-9af91826fb5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b39a56e7d99784852d5d4cef1a113a02eb6cfe4 --- /dev/null +++ b/scc/tarjan/9142b5ca-f019-408d-aa5a-9af91826fb5a.cpp @@ -0,0 +1,150 @@ +#include + + +using namespace std; + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + +struct nodS + +{ + + int i, low; + + bool on; + + vector vecini; + +}; + +int n,m,x,y,i,t; + +stack s; + +vector < vector > ans; + +nodS nod[100005]; + +void dfs(int poz) + +{ + + t++; + + nod[poz].i=t; + + nod[poz].low=t; + + nod[poz].on=true; + + s.push(poz); + + + + for(int it : nod[poz].vecini) + + { + + if(!nod[it].i) + + { + + dfs(it); + + nod[poz].low=min(nod[poz].low, nod[it].low); + + } + + else if(nod[it].on) + + { + + nod[poz].low=min(nod[poz].low, nod[it].i); + + } + + } + + + + if(nod[poz].i==nod[poz].low) + + { + + vector nscc; + + while(!s.empty()) + + { + + int it=s.top(); + + nod[it].on=false; + + nscc.push_back(it); + + s.pop(); + + if(it==poz) + + break; + + } + + ans.push_back(nscc); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m) + + { + + m--; + + fin>>x>>y; + + nod[x].vecini.push_back(y); + + } + + + + for(i=1;i<=n;i++) + + { + + if(!nod[i].i) + + dfs(i); + + } + + + + fout< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m,nrOrd; + +vector> ad; + +vector ord, low; + + + +stack st; + +vector onStack; + +vector> sccs; + + + +void dfs(int node) { + + st.push(node); + + onStack[node] = true; + + ord[node] = low[node] = ++nrOrd; + + for (int vecin : ad[node]) + + if (!ord[vecin]) { + + dfs(vecin); + + low[node] = min(low[node], low[vecin]); + + } + + else if (onStack[vecin]) + + low[node] = min(low[node], low[vecin]); + + + + if (ord[node] == low[node]) { + + sccs.emplace_back(); + + while (true) { + + int top = st.top(); + + st.pop(); + + onStack[top] = false; + + + + low[top] = ord[node]; + + sccs.back().push_back(top); + + if (top == node) + + break; + + } + + } + +} + + + +int main() { + + fin >> n >> m; + + ad.resize(n + 1); + + for (int i = 0; i < m; i++) { + + int x, y; + + fin >> x >> y; + + ad[x].push_back(y); + + } + + + + ord.resize(n + 1); + + low.resize(n + 1); + + onStack.resize(n + 1); + + + + for (int i = 1; i <= n; i++) + + if (!ord[i]) + + dfs(i); + + + + fout << sccs.size() << '\n'; + + for (auto& scc : sccs) { + + for (int node : scc) + + fout << node << ' '; + + fout << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/92002be5-ac1b-4ab3-a034-f4355b55ad93.cpp b/scc/tarjan/92002be5-ac1b-4ab3-a034-f4355b55ad93.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85deadb58f96fefdc7c5a39add890a6beab9d934 --- /dev/null +++ b/scc/tarjan/92002be5-ac1b-4ab3-a034-f4355b55ad93.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int nmax = 100005; + +int n, m, id[nmax], low[nmax], z; + +bool inStack[nmax]; + +vector G[nmax]; + +stack s; + +vector > ans; + +void tarjan(int nod){ + + id[nod] = low[nod] = ++z; + + s.push(nod); + + inStack[nod] = true; + + for (auto it : G[nod]){ + + if (id[it] == 0){ + + tarjan(it); + + low[nod] = min(low[nod], low[it]); + + } + + else{ + + if (inStack[it]){ + + low[nod] = min(low[nod], low[it]); + + } + + } + + } + + if (id[nod] == low[nod]){ + + vector aux; + + int snod; + + do{ + + snod = s.top(); + + inStack[snod] = false; + + s.pop(); + + aux.push_back(snod); + + } while (snod != nod); + + ans.push_back(aux); + + } + +} + + + +int main(){ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i){ + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i){ + + if (id[i] == 0){ + + tarjan(i); + + } + + } + + fout << ans.size() << "\n"; + + for (int i = 0; i < ans.size(); ++i){ + + for (int j = 0; j < ans[i].size(); ++j){ + + fout << ans[i][j] << " "; + + } + + fout << "\n"; + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/92762892-8fec-4d57-b0c7-b661f4c18ca6.cpp b/scc/tarjan/92762892-8fec-4d57-b0c7-b661f4c18ca6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f8246581d921b5dfac3f91308cfab27c4d0f0fa --- /dev/null +++ b/scc/tarjan/92762892-8fec-4d57-b0c7-b661f4c18ca6.cpp @@ -0,0 +1,83 @@ +#include + +const int MAXN = 100005; + +using namespace std; + +struct tarjan_node { + int index, lowlink, on_stack; +}; + +void tarjan_DFS(int node, vector *Graph, vector > &output, stack &s, tarjan_node *nodes) { + static int pos = 0; + nodes[node].index = nodes[node].lowlink = ++pos; + nodes[node].on_stack = 1; + s.push(node); + + for(long unsigned int i = 0; i < Graph[node].size(); i++) { + int son = Graph[node][i]; + + if(nodes[son].index == 0) { + tarjan_DFS(son, Graph, output, s, nodes); + nodes[node].lowlink = min(nodes[node].lowlink, nodes[son].lowlink); + } else if(nodes[son].on_stack == 1) { + nodes[node].lowlink = min(nodes[node].lowlink, nodes[son].lowlink); + } + } + + if(nodes[node].lowlink == nodes[node].index) { + vector SCC; + int new_node; + + do { + new_node = s.top(); + s.pop(); + nodes[new_node].on_stack = 0; + SCC.push_back(new_node); + } while(new_node != node); + + output.push_back(SCC); + } +} + +void tarjan(int N, vector *Graph, vector > &output) { + stack s; + tarjan_node nodes[MAXN]; + memset(nodes, 0, sizeof(nodes)); + + for(int i = 1; i <= N; i++) + if(nodes[i].index == 0) + tarjan_DFS(i, Graph, output, s, nodes); +} + +int main() { + FILE *in = fopen("ctc.in", "r"); + FILE *out = fopen("ctc.out", "w"); + + int N, M, X, Y; + vector Graph[MAXN]; + vector > output; + + fscanf(in, "%d %d", &N, &M); + + for(int i = 1; i <= M; i++) { + fscanf(in, "%d %d", &X, &Y); + Graph[X].push_back(Y); + } + + tarjan(N, Graph, output); + + fprintf(out, "%lu\n", output.size()); + + for(long unsigned int i = 0; i < output.size(); i++) { + for(long unsigned int j = 0; j < output[i].size(); j++) { + fprintf(out, "%d ", output[i][j]); + } + fprintf(out, "\n"); + } + + fclose(in); + fclose(out); + + return 0; +} diff --git a/scc/tarjan/93aa9aff-8f62-4968-bcdb-3b3a27d355d4.cpp b/scc/tarjan/93aa9aff-8f62-4968-bcdb-3b3a27d355d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25ebd0e243495f91e9b38be13eccb418f1c5c511 --- /dev/null +++ b/scc/tarjan/93aa9aff-8f62-4968-bcdb-3b3a27d355d4.cpp @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int g,low[100005],niv[100005],ctc,n,m,x,y,i,j; + +bitset<100005> IQ; + +deque q; + +vector L[100005],C[100005]; + +void dfs(int nod) + +{ + + g++; + + low[nod]=g; + + niv[nod]=g; + + IQ[nod]=1; + + q.push_back(nod); + + for(int i=0; i>n>>m; + + for(; m--;) + + { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(niv[i]==0) + + { + + dfs(i); + + } + + } + + fout< + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; + + + + + + + +vector adj[100005], con, idx, low, in_stack; + +vector < vector > C; + +stack S; + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + + + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) + + { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + + + +{ + + idx[n] = low[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) + + { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + low[n] = ((low[n]) < (low[*it]) ? (low[n]) : (low[*it])); + + else if (in_stack[*it]) + + low[n] = ((low[n]) < (low[*it]) ? (low[n]) : (low[*it])); + + } + + if (idx[n] == low[n]) + + { + + con.clear(); + + int node; + + do + + { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + + + +{ + + int n; + + read_in(adj, n, iname); + + idx.resize(n), low.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + print_out(C, oname); + + return 0; + + + +} diff --git a/scc/tarjan/961397c1-61dc-45b4-89ab-12cc8e978a3a.cpp b/scc/tarjan/961397c1-61dc-45b4-89ab-12cc8e978a3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefd11e8a65733c4d2c2d9b77a72744dc81d9f33 --- /dev/null +++ b/scc/tarjan/961397c1-61dc-45b4-89ab-12cc8e978a3a.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +using namespace std; + +vector G[100010]; +vector> sol; +stack s; +int N,M; +vectorlowlink(100010,-1); +vectordisc(100010,-1); +vectoronStack(100010,false); + + +void DFS(int x) { + static int index = 0; + disc[x] = lowlink[x] = index; + index++; + s.push(x); + onStack[x] = true; + + for (int elem : G[x]) { + if (disc[elem] == -1) + DFS(elem); + if (onStack[elem]) + lowlink[x] = min(lowlink[x], lowlink[elem]); + } + if (disc[x] == lowlink[x]) { + vector aux; + + while (s.top() != x) { + aux.push_back(s.top()); + onStack[s.top()] = 0; + s.pop(); + } + + aux.push_back(s.top()); + onStack[s.top()] = 0; + s.pop(); + + sol.push_back(aux); + } +} +int main() +{ + + ifstream f ("ctc.in"); + ofstream g("ctc.out"); + + f>> N >>M; + int x,y; + for( int i = 0; i < M; i++ ) + { + f>>x>>y; + G[x].push_back(y); + + } + + + + + for(int i = 1; i <=N; i++) + if ( disc[i] == -1) + DFS(i); + + + g << sol.size() << "\n"; + for (auto vec : sol){ + for (int x : vec) + g<< x << " "; + g << "\n"; + } + return 0; +} diff --git a/scc/tarjan/9794596e-7604-4a41-ae98-53dda2f06726.cpp b/scc/tarjan/9794596e-7604-4a41-ae98-53dda2f06726.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa33120e456b78d1057125cb4695c71a70c6621 --- /dev/null +++ b/scc/tarjan/9794596e-7604-4a41-ae98-53dda2f06726.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n, iname); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C, oname); + + + + return 0; + +} diff --git a/scc/tarjan/99fdfd65-e7ed-4819-8075-7eede826f19c.cpp b/scc/tarjan/99fdfd65-e7ed-4819-8075-7eede826f19c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ca9f169bc7e450dc6acfa76c8916ec4a9765960 --- /dev/null +++ b/scc/tarjan/99fdfd65-e7ed-4819-8075-7eede826f19c.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,i,a,b,low[100010],niv[100010],l,nr,d[100010]; + +vector v[100010],s,sol[100010]; + +void dfs(int nod) + +{ + + low[nod]=niv[nod]=++l; + + s.push_back(nod);d[nod]=1; + + for(auto it:v[nod]) + + { + + if(niv[it]&&d[it]) low[nod]=min(low[nod],niv[it]); + + if(niv[it]==0) + + { + + dfs(it); + + low[nod]=min(low[it],low[nod]); + + } + + } + + if(low[nod]==niv[nod]) + + { + + nr++;while(s.back()!=nod) sol[nr].push_back(s.back()),d[s.back()]=0,s.pop_back(); + + sol[nr].push_back(s.back()),d[s.back()]=0,s.pop_back(); + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>a>>b; + + v[a].push_back(b); + + } + + for(i=1;i<=n;i++) if(niv[i]==0) dfs(i); + + fout< + +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int Inf = INT_MAX; + +vector < vector > g, comps; +vector lowlink, pre; +int V, E; + +int traversalOrd; + +stack Stack; + +void readGraph(){ + + fin >> V >> E; + + g.resize(V + 1); + lowlink.resize(V + 1); + pre.resize(V + 1); + comps.reserve(V); + + for(int e = 0; e < E; ++e){ + + int from, to; + fin >> from >> to; + + g[from].push_back(to); + } +} + + +void cacheSCC(int node){ + + vector SCC; + SCC.reserve(V); + + int stackNode; + do{ + + stackNode = Stack.top(); + Stack.pop(); + + lowlink[stackNode] = Inf; + SCC.push_back(stackNode); + }while(stackNode != node); + + comps.push_back(SCC); +} + + +void DFS(int node){ + + lowlink[node] = pre[node] = ++traversalOrd; + Stack.push(node); + + for(const int &adj : g[node]) + if(lowlink[adj] == 0){ + + DFS(adj); + lowlink[node] = min(lowlink[node], lowlink[adj]); + } + else lowlink[node] = min(lowlink[node], lowlink[adj]); + + if(lowlink[node] == pre[node]) + cacheSCC(node); +} + + +int main(){ + + readGraph(); + + for(int node = 1; node <= V; ++node) + if(lowlink[node] == 0) + DFS(node); + + + + + fout << comps.size() << '\n'; + for(int c = 0; c < (int)comps.size(); ++c){ + + for(const int &node : comps[c]) + fout << node << ' '; + fout << '\n'; + } +} diff --git a/scc/tarjan/9da82e0a-532e-4787-8107-94e28b64672f.cpp b/scc/tarjan/9da82e0a-532e-4787-8107-94e28b64672f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2faefcc2ba8e8ba11ff73704970e5454459e44e8 --- /dev/null +++ b/scc/tarjan/9da82e0a-532e-4787-8107-94e28b64672f.cpp @@ -0,0 +1,64 @@ +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector graf[100005]; +vector ans[100005]; + +stack st; +int a[100005],b[100005]; +bool instack[100005]; +int t,ans1; +void tarjan(int x) +{ + t++; + a[x]=t; + b[x]=t; + st.push(x); + instack[x]=1; + for(auto k:graf[x]) + { + if(a[k]==0) + { + tarjan(k); + b[x]=min(b[x],b[k]); + } + else if(instack[k]) + b[x]=min(b[x],b[k]); + } + if(b[x]==a[x]) + { + while(!st.empty() && b[x]<=b[st.top()]) + { + ans[ans1].push_back(st.top()); + instack[st.top()]=0; + st.pop(); + } + ans1++; + } +} +int main() +{ + int n,m; + in>>n>>m; + for(int i=1;i<=m;i++) + { + int x,y; + in>>x>>y; + graf[x].push_back(y); + } + for(int i=1;i<=n;i++) + if(a[i]==0) + tarjan(i); + out< +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + +vector g[1 + NMAX]; + +vector strongA[1 + NMAX];int strAsiz; + +int visited[1 + NMAX]; + +int minReach[1 + NMAX]; + +bool onStack[1 + NMAX]; + +stack st; + + + +int n, m, step; + + + +void strong(int nod) { + + + + step++; + + visited[nod] = step; + + minReach[nod] = step; + + st.push(nod); + + onStack[nod] = true; + + + + for(int i = 0;i < g[nod].size();i++){ + + + + + + if(visited[g[nod][i]] == 0){ + + + + strong(g[nod][i]); + + minReach[nod] = min(minReach[nod], minReach[g[nod][i]]); + + }else if(onStack[g[nod][i]] == true){ + + + + minReach[nod] = min(minReach[nod], visited[g[nod][i]]); + + } + + } + + if(minReach[nod] == visited[nod]){ + + strAsiz++; + + int popped; + + do{ + + + + popped = st.top(); + + + + st.pop(); + + + + onStack[popped] = false; + + strongA[strAsiz].push_back(popped); + + } while(popped != nod); + + } + + + +} + + + +int main(){ + + + + int from, to; + + in >> n >> m; + + + + for(int i = 1;i <= m;i++){ + + + + in >> from >> to; + + g[from].push_back(to); + + } + + + + for(int i = 1;i <= n;i++){ + + + + if(visited[i] == 0) { + + strong(i); + + } + + } + + + + out << strAsiz << '\n'; + + for(int i = 1;i <= strAsiz;i++){ + + + + for(int j = 0;j < strongA[i].size() ;j++){ + + out << strongA[i][j] << ' '; + + } + + out << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/9f21bacc-c1ce-44c5-bbec-a6566b1cb4f7.cpp b/scc/tarjan/9f21bacc-c1ce-44c5-bbec-a6566b1cb4f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d302b743d07dbc340fdb92759caa763f6071b412 --- /dev/null +++ b/scc/tarjan/9f21bacc-c1ce-44c5-bbec-a6566b1cb4f7.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int NMAX=1e5+10; + +vector < int > Edge[NMAX], Components[NMAX]; + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + +bitset < NMAX > Seen, Belongs; + +stack < int > Stack; + + + +void Read() + +{ + + in>>n>>m; + + int x, y; + + for(int i=1; i<=m; i++) + + in>>x>>y, Edge[x].push_back(y); + +} + + + +void Tarjan(int x) + +{ + + niv++; + + Seen[x]=1; + + Belongs[x]=1; + + Stack.push(x); + + RootLevel[x]=niv; + + NodeLevel[x]=niv; + + for(int i : Edge[x]) + + { + + if(!Seen[i]) + + { + + Tarjan(i); + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + else + + if(Belongs[i]) + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + if(RootLevel[x]==NodeLevel[x]) + + { + + nrCMP++; + + int node=0; + + do + + { + + node=Stack.top(); + + Stack.pop(); + + Components[nrCMP].push_back(node); + + Belongs[node]=0; + + }while(node!=x); + + } + +} + + + +int main() + +{ + + Read(); + + for(int i=1; i<=n; i++) + + if(!Seen[i]) + + niv=0, Tarjan(i); + + out< +#include +#include + +const int NMAX = 1e5 + 1; + +std :: ifstream in ("ctc.in"); +std :: ofstream out("ctc.out"); + +std :: vector graf[NMAX]; +std :: vector> sol; +std :: stack stk; + +int low[NMAX], id[NMAX]; +bool inStack[NMAX]; + +int n, m, a, b; +int idc = 0; + +void DFS (int); + +int main() +{ + + in >> n >> m; + for( int i = 0; i < m; i++ ){ + in>>a>>b; + graf[a-1].emplace_back(b-1); + } + + for(int i = 0; i < n; i++ ) + { + low[i] = 0; + id[i] = -1; + inStack[i] = 0; + } + + for(int i = 0; i < n; i++) + if ( id[i] == -1) + DFS(i); + + + out << sol.size() << "\n"; + for (auto list : sol){ + for (int elem : list) + out << elem + 1 << " "; + out << "\n"; + } + return 0; +} + +void DFS(int x) +{ + id[x] = low[x] = idc; + idc += 1; + stk.push(x); + inStack[x] = 1; + + for (int elem : graf[x]){ + if(id[elem] == -1) + DFS(elem); + if(inStack[elem]) + low[x] = std :: min (low[x], low[elem]); + } + if(id[x] == low[x]){ + std :: vector aux; + + while (stk.top() != x){ + aux.push_back( stk.top() ); + low[ stk.top() ] = id[x]; + inStack[ stk.top() ] = 0; + stk.pop(); + } + + aux.push_back( stk.top() ); + low[ stk.top() ] = id[x]; + inStack[ stk.top() ] = 0; + stk.pop(); + + sol.push_back(aux); + } + +} diff --git a/scc/tarjan/a17a4511-9cd8-4e3d-94cc-d5caf1159b58.cpp b/scc/tarjan/a17a4511-9cd8-4e3d-94cc-d5caf1159b58.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80c4939bae6bfe4d3df22b71800f825b0247e2e7 --- /dev/null +++ b/scc/tarjan/a17a4511-9cd8-4e3d-94cc-d5caf1159b58.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int MAX = 100001; + + + +int nrctc, t; + +vector v[MAX], ct[MAX], disc, low, isInStack; + +stack s; + + + +void ctc(int nod) + +{ + + t++; + + disc[nod] = low[nod] = t; + + s.push(nod); + + isInStack[nod] = true; + + + + for(auto it : v[nod]) + + { + + if(!disc[it]) + + { + + ctc(it); + + low[nod] = min(low[nod], low[it]); + + } + + else if(isInStack[it])low[nod] = min(low[nod], disc[it]); + + } + + + + if(low[nod] == disc[nod]) + + { + + while(s.top() != nod) + + { + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + } + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + nrctc++; + + } + + + +} + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + disc.assign(n, 0); + + low.assign(n, 0); + + isInStack.assign(n, 0); + + for(int i=0; i> x >> y; x--; y--; + + v[x].push_back(y); + + } + + for(int i=0; i +#include +#include + +const int NMAX = 1e5 + 1; + +std :: ifstream in ("ctc.in"); +std :: ofstream out("ctc.out"); + +std :: vector graf[NMAX]; +std :: vector> sol; +std :: stack stk; + +int low[NMAX], id[NMAX]; +bool inStack[NMAX]; + +int n, m, a, b; +int idc = 0; + +void DFS (int); + +int main() +{ + + in >> n >> m; + for( int i = 0; i < m; i++ ){ + in>>a>>b; + graf[a-1].emplace_back(b-1); + } + + for(int i = 0; i < n; i++ ) + { + low[i] = 0; + id[i] = -1; + inStack[i] = 0; + } + + for(int i = 0; i < n; i++) + if ( id[i] == -1) + DFS(i); + + + out << sol.size() << "\n"; + for (auto list : sol){ + for (int elem : list) + out << elem + 1 << " "; + out << "\n"; + } + return 0; +} + +void DFS(int x) +{ + id[x] = low[x] = idc; + idc += 1; + stk.push(x); + inStack[x] = 1; + + for (int elem : graf[x]){ + if(id[elem] == -1) + DFS(elem); + if(inStack[elem]) + low[x] = std :: min (low[x], low[elem]); + } + if(id[x] == low[x]){ + std :: vector aux; + + while (stk.top() != x){ + aux.push_back( stk.top() ); + inStack[ stk.top() ] = 0; + stk.pop(); + } + + aux.push_back( stk.top() ); + inStack[ stk.top() ] = 0; + stk.pop(); + + sol.push_back(aux); + } + +} diff --git a/scc/tarjan/a4cbd1e5-6532-40bd-8263-d3020418a437.cpp b/scc/tarjan/a4cbd1e5-6532-40bd-8263-d3020418a437.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b146122243fb20dc18bfbdd3fd067571ef1aec64 --- /dev/null +++ b/scc/tarjan/a4cbd1e5-6532-40bd-8263-d3020418a437.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,low[100005],idx[100005],nrc,x,y,t,st[100005],k,ap[100005]; + +vector comp[100005]; + +vector v[100005]; + +void dfs(int nod) + +{ + + low[nod]=t; + + idx[nod]=t; + + ++t; + + st[++k]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + if(idx[it]==0) + + { + + dfs(it); + + low[nod]=min(low[it],low[nod]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + if(low[nod]==idx[nod]) + + { + + nrc++; + + while(st[k]!=nod) + + { + + comp[nrc].push_back(st[k]); + + ap[st[k]]=0; + + k--; + + } + + comp[nrc].push_back(st[k]); + + ap[st[k]]=0; + + k--; + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) + + if(idx[i]==0) + + { + + t=1; + + k=0; + + dfs(i); + + } + + out< +#include +#include +#include +#include +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +vector g[100002]; +int discoverTime[100002]; +int lowTime[100002]; +bool onStack[100002]; +int id; +stack s; +vector rasp[100000]; +int n, m, nr_componente; + +void dfs(int from) +{ + id++; + s.push(from); + onStack[from] = true; + discoverTime[from] = lowTime[from] = id; + + for (auto &to : g[from]) + { + if (discoverTime[to] == 0) + { + dfs(to); + } + if (onStack[to]) + { + lowTime[from] = min(lowTime[from], lowTime[to]); + } + } + if (lowTime[from] == discoverTime[from]) + { + while (s.top() != from) + { + rasp[nr_componente].push_back(s.top()); + lowTime[s.top()] = min(lowTime[from], lowTime[s.top()]); + onStack[s.top()] = false; + s.pop(); + } + rasp[nr_componente].push_back(s.top()); + onStack[s.top()] = false; + s.pop(); + ++nr_componente; + } +} +int main() +{ + in >> n >> m; + for (int i = 0; i < m; i++) + { + int x, y; + in >> x >> y; + g[x].push_back(y); + } + for (int i = 1; i <= n; i++) + { + if (discoverTime[i] == 0) + { + dfs(i); + } + } + out << nr_componente << '\n'; + for (int i = 0; i < nr_componente; i++) + { + for (auto &el : rasp[i]) + { + out << el << ' '; + } + out << '\n'; + } +} diff --git a/scc/tarjan/a541f006-df12-4cb0-9739-df96e7c51bc0.cpp b/scc/tarjan/a541f006-df12-4cb0-9739-df96e7c51bc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8172fb23608ca3901181e5c11748a8a7199034dd --- /dev/null +++ b/scc/tarjan/a541f006-df12-4cb0-9739-df96e7c51bc0.cpp @@ -0,0 +1,145 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +vector arb[DMAX]; + +vector ans[DMAX]; + + + +stack s; + + + +int uz[DMAX]; + +int lowlink[DMAX]; + + + +int n,m,cnt; + +int comp; + + + +void dfsT(int node); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + arb[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!uz[i]) + + dfsT(i); + + fout< 0) + + lowlink[node]=min(lowlink[node],lowlink[it]); + + } + + if(uz[node] == lowlink[node]){ + + comp++; + + do{ + + int node2=s.top(); + + s.pop(); + + ans[comp].push_back(node2); + + uz[node2]=-1; + + if(node2 == node) + + break; + + }while(true); + + } + +} diff --git a/scc/tarjan/a5a0f186-f6bc-48a4-8f72-405cbbbe5e4f.cpp b/scc/tarjan/a5a0f186-f6bc-48a4-8f72-405cbbbe5e4f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..92fe81aa09f5062fa962aaebf978942757189893 --- /dev/null +++ b/scc/tarjan/a5a0f186-f6bc-48a4-8f72-405cbbbe5e4f.cpp @@ -0,0 +1,148 @@ +#include +using namespace std; + + + +const int maxN=(1e5)+5; + + + +int lvl,niv[maxN],low[maxN],st[maxN],vf,ctc; + +bool inStack[maxN]; + + + +vector v[maxN],C[maxN]; + + + +void dfs(int nod) + +{ + + lvl++; + + niv[nod]=low[nod]=lvl; + + st[++vf]=nod; + + inStack[nod]=1; + + + + for(auto it:v[nod]) + + { + + if(!niv[it]) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else + + if(inStack[it]) + + low[nod]=min(low[nod],low[it]); + + } + + + + if(low[nod]==niv[nod]) + + { + + int x=-1; + + ctc++; + + while(x!=nod) + + { + + x=st[vf]; + + C[ctc].push_back(x); + + inStack[x]=0; + + vf--; + + } + + } + +} + + + +int n,m,x,y; + + + +int main() + +{ + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d",&x,&y); + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;i++) + + if(!niv[i]) dfs(i); + + + + + + printf("%d\n",ctc); + + + + for(int i=1;i<=ctc;i++) + + { + + for(auto it:C[i]) + + printf("%d ",it); + + + + printf("\n"); + + } + + + + + + return 0; + +} diff --git a/scc/tarjan/a63092ff-f535-4dd7-b3e5-895ec1e674aa.cpp b/scc/tarjan/a63092ff-f535-4dd7-b3e5-895ec1e674aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84284702f3410962b043e180f22db0e17aa037ae --- /dev/null +++ b/scc/tarjan/a63092ff-f535-4dd7-b3e5-895ec1e674aa.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, l, sol; + +pair p[100005]; + +vector graph[100005], out[100005]; + +stack stk; + +bitset<100005> check, inS; + + + +void dfs(int now); + + + +int main() + +{ + + + + fin >> n >> m; + + while(m--){ + + int x, y; + + fin >> x >> y; + + + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(!check[i]) dfs(i); + + } + + + + fout << sol << "\n"; + + for(int i = 1; i <= sol; ++i){ + + for(auto it:out[i]) fout << it << " "; + + fout << "\n"; + + } + + + + return 0; + +} + + + +void dfs(int now){ + + ++l; + + p[now] = {l, l}; + + check[now] = inS[now] = true; + + stk.push(now); + + + + for(auto next:graph[now]){ + + if(!check[next]){ + + dfs(next); + + p[now].second = min(p[now].second, p[next].second); + + } + + else if(inS[next]) p[now].second = min(p[now].second, p[next].first); + + } + + + + if(p[now].first == p[now].second){ + + ++sol; + + + + while(stk.top() != now){ + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + +} diff --git a/scc/tarjan/a8117d32-ff80-4082-8c13-10ecc389af56.cpp b/scc/tarjan/a8117d32-ff80-4082-8c13-10ecc389af56.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9eb48c123116b0233078bb42e6e4e44e9d89d5ac --- /dev/null +++ b/scc/tarjan/a8117d32-ff80-4082-8c13-10ecc389af56.cpp @@ -0,0 +1,110 @@ +#include +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +int nctc; + +int low[100005], disc[100005], _time; + +std::vector ctc[100005]; + +bool inStack[100005]; + +std::stack stack; + +void DFS(int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + + + for (auto &it:ADC[vertex]) { + + if (disc[it]) { + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + else { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (low[vertex] == disc[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + ++ nctc; + + } + +} + + + +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int main() + +{ + + input >> N >> M; + + for (int i=1, u, v; i<=M; ++i) input >> u >> v, addEdge(u, v); + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + output << nctc << '\n'; + + for (int i=0; i +#include +#include + + + + +using namespace std; + + + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + + + +void Read( + + vector> &nodes + +) { + + uint32_t n; + + uint32_t m; + + in >> n; + + in >> m; + + + + nodes.resize(n); + + + + uint32_t node1; + + uint32_t node2; + + + + for (; m; --m) { + + in >> node1; + + in >> node2; + + + + --node1; + + --node2; + + + + nodes[node1].push_back(node2); + + } + +} + + + + + +inline void Add_SCC( + + vector> &SCCs, + + stack &stk, + + vector &on_stack, + + uint32_t const node + +) { + + SCCs.push_back(vector()); + + + + uint32_t top; + + + + do { + + top = stk.top(); + + stk.pop(); + + on_stack[top] = false; + + + + SCCs.back().push_back(top); + + } while (top != node); + +} + + + + + +inline void DFS( + + vector> const &nodes, + + vector &order, + + vector> &SCCs, + + uint32_t const node + +) { + + static uint32_t order_count; + + static vector low(nodes.size()); + + static stack stk; + + static vector on_stack(nodes.size()); + + + + ++order_count; + + order[node] = order_count; + + low[node] = order_count; + + + + stk.push(node); + + on_stack[node] = true; + + + + uint32_t neighbour; + + + + for (uint32_t i = 0; i < nodes[node].size(); ++i) { + + neighbour = nodes[node][i]; + + + + if (order[neighbour] == 0) { + + DFS(nodes, order, SCCs, neighbour); + + } + + + + if (on_stack[neighbour]) { + + low[node] = min(low[node], low[neighbour]); + + } + + } + + + + if (order[node] == low[node]) { + + Add_SCC(SCCs, stk, on_stack, node); + + } + +} + + + + + +void Solve( + + vector> const &nodes + +) { + + vector> SCCs; + + vector order(nodes.size()); + + + + for (uint32_t i = 0; i < nodes.size(); ++i) { + + if (order[i] == 0) { + + DFS(nodes, order, SCCs, i); + + } + + } + + + + out << SCCs.size() << '\n'; + + + + for (uint32_t j, i = 0; i < SCCs.size(); ++i) { + + for (j = 0; j < SCCs[i].size(); ++j) { + + out << SCCs[i][j] + 1 << ' '; + + } + + + + out << '\n'; + + } + +} + + + + + +int main() { + + vector> nodes; + + + + Read(nodes); + + + + Solve(nodes); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/scc/tarjan/a88bac52-775e-481d-8562-d6ef23d252f7.cpp b/scc/tarjan/a88bac52-775e-481d-8562-d6ef23d252f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..60e9df06097cddc0a39e89770dd3f8fb3bf3f874 --- /dev/null +++ b/scc/tarjan/a88bac52-775e-481d-8562-d6ef23d252f7.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 1e5 + 5; + + + +vector < int > a[N]; + +vector < vector < int > > sol; + +stack < int > st; + + + +int disc[N], low[N], temp; + +bool in_stack[N]; + + + +void tarjan(int k) + +{ + + disc[k] = low[k] = ++temp; + + st.push(k); + + in_stack[k] = true; + + for(auto v : a[k]) { + + if(disc[v] == 0) { + + tarjan(v); + + low[k] = min(low[k], low[v]); + + } + + else if(in_stack[v] == true) low[k] = min(low[k], low[v]); + + } + + if(disc[k] == low[k]) { + + vector < int > add; + + int node = k; + + do { + + node = st.top(); + + add.push_back(node); + + in_stack[node] = false; + + st.pop(); + + } + + while(node != k); + + sol.push_back(add); + + } + +} + +int main() + +{ + + usain_bolt(); + + + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y; + + + + fin >> x >> y; + + a[x].push_back(y); + + } + + for(int i = 1; i <= n; ++i) + + if(disc[i] == 0) tarjan(i); + + fout << sol.size() << "\n"; + + for(auto x : sol) { + + for(auto y : x) { + + fout << y << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/a8f07f50-27cf-4272-8d27-21ac6ebfdbf6.cpp b/scc/tarjan/a8f07f50-27cf-4272-8d27-21ac6ebfdbf6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dc2986fc4c4e8c3323c8e547f707619b79156119 --- /dev/null +++ b/scc/tarjan/a8f07f50-27cf-4272-8d27-21ac6ebfdbf6.cpp @@ -0,0 +1,75 @@ +#include +#include +#include + +using namespace std; + +ifstream cin ("ctc.in"); +ofstream cout ("ctc.out"); + +const int NMAX = 1e5 + 14; + +vector gr[NMAX]; +bitset inStack; + +pair info [NMAX]; + +int currentNodeLabel = 0; +vector stack; + +void dfs(int node, vector < vector > &ctc) { + ++ currentNodeLabel; + info[node] = {currentNodeLabel, currentNodeLabel}; + stack.push_back(node); + inStack[node] = 1; + for (auto &neigh : gr[node]) { + if (info[neigh].first == 0) { + dfs(neigh, ctc); + info[node].second = min(info[node].second, info[neigh].second); + } + else if (inStack[neigh] == 1) { + info[node].second = min(info[node].second, info[neigh].first); + } + } + if (info[node].first == info[node].second) { + + int current; + vector aux; + do { + current = stack.back(); + aux.push_back(current); + inStack[current] = 0; + stack.pop_back(); + }while (current != node); + ctc.push_back(aux); + } +} + +int main() { + ios :: sync_with_stdio(false); + cin.tie(nullptr); + int n, m; + cin >> n >> m; + while (m --) { + int x, y; + cin >> x >> y; + gr[x].push_back(y); + } + vector < vector > ctc; + for (int i = 1; i <= n; ++ i) { + if (info[i].first == 0) { + dfs(i, ctc); + } + } + + + + cout << ctc.size() << '\n'; + for (auto &x : ctc) { + for (auto &y : x) { + cout << y << ' '; + } + cout << '\n'; + } + return 0; +} diff --git a/scc/tarjan/aaeb1bda-8e1d-474c-af05-358094c75ec6.cpp b/scc/tarjan/aaeb1bda-8e1d-474c-af05-358094c75ec6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88c6609737e5169b3d38f1d8e904ddbf9940da38 --- /dev/null +++ b/scc/tarjan/aaeb1bda-8e1d-474c-af05-358094c75ec6.cpp @@ -0,0 +1,148 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int noduri, muchii, componente_tari, a, b, ind = 1, ids[100005]; + +bool instack[100005]; + +int lowkey[100005]; + +vector mat[100005]; + +vector componente[100005]; + +stack s; + + + +void dfs(int node) + +{ + + s.push(node); + + instack[node] = 1; + + ids[node] = ind; + + lowkey[node] = ind; + + ++ind; + + for(int i = 0; i < mat[node].size(); ++i) + + { + + int next_node = mat[node][i]; + + if(!ids[next_node]) + + { + + dfs(next_node); + + } + + if(instack[next_node]) + + { + + lowkey[node] = min(lowkey[node], lowkey[next_node]); + + } + + } + + if(ids[node] == lowkey[node]) + + { + + while(!s.empty()) + + { + + int next = s.top(); + + componente[componente_tari].push_back(next); + + s.pop(); + + instack[next] = 0; + + lowkey[next] = ids[node]; + + if(node == next) break; + + } + + componente_tari++; + + } + +} + + + +int main() + +{ + + fin >> noduri >> muchii; + + for(int i = 1; i <= muchii; ++i) + + { + + fin >> a >> b; + + mat[a].push_back(b); + + } + + for(int node = 1; node <= noduri; ++node) + + { + + if(!ids[node]) + + { + + dfs(node); + + } + + } + + fout << componente_tari << "\n"; + + for(int i = 0; i < componente_tari; ++i) + + { + + for(int j = 0; j < componente[i].size(); ++j) + + { + + fout << componente[i][j] << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/ab2be278-2088-4c68-986f-f257b0121b3b.cpp b/scc/tarjan/ab2be278-2088-4c68-986f-f257b0121b3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4cdb495569f148b1782e48e5112c527b7017b038 --- /dev/null +++ b/scc/tarjan/ab2be278-2088-4c68-986f-f257b0121b3b.cpp @@ -0,0 +1,175 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +struct Node{ + + int index=0, lowlink=0; + + bool onstack = false; + + Node(){} + + Node(int index, int lowlink) : index(index), lowlink(lowlink){ + + onstack = true; + + } + +}; + + + +int n, m; + +vector gra[100041]; + +void read(){ + + fin >> n >> m; + + for(int i = 0; i < m; ++i){ + + int a, b; + + fin >> a >> b; + + gra[a].push_back(b); + + } + +} + + + +vector> components; + + + +stack s; + +int index = 1; + +Node nodes[100041]; + +void strongconnect(int a){ + + s.push(a); + + Node &an = nodes[a]; + + an = Node(index, index); + + index++; + + for(auto b : gra[a]){ + + Node &bn = nodes[b]; + + if(bn.index == 0){ + + strongconnect(b); + + an.lowlink = min(an.lowlink, bn.lowlink); + + }else{ + + an.lowlink = min(an.lowlink, bn.index); + + } + + } + + if(an.index == an.lowlink){ + + components.push_back(vector()); + + vector &component = components.back(); + + int b; + + do{ + + b = s.top(); + + s.pop(); + + nodes[b].onstack = false; + + component.push_back(b); + + }while(b != a); + + } + +} + + + +void tarzan(){ + + for(int i = 1; i <= n; ++i){ + + if(nodes[i].index == 0){ + + strongconnect(i); + + while(!s.empty()){ + + components.push_back({s.top()}); + + s.pop(); + + } + + } + + } + +} + + + +void write(){ + + fout << components.size() << "\n"; + + for(auto component : components){ + + for(auto a : component){ + + fout << a << " "; + + } + + fout << "\n"; + + } + +} + + + +int main(){ + + read(); + + tarzan(); + + write(); + + return 0; + +} diff --git a/scc/tarjan/ac255ec4-dd66-458f-8aa8-ff8f1717c6d4.cpp b/scc/tarjan/ac255ec4-dd66-458f-8aa8-ff8f1717c6d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0ecad9ce713c2ec565433cbc78e7df1d5cc8772 --- /dev/null +++ b/scc/tarjan/ac255ec4-dd66-458f-8aa8-ff8f1717c6d4.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + +nivel++; +niv[nod] = nivel; +low[nod] = nivel; +s.push(nod); +fr[nod] = 1; + +for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + +if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); +} + +} + +int main() { + +in>>n>>m; +for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); +} + +for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + +out< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, k = 0; + + + +vector v[100005]; + + + +vector > sol; + + + +vector temp; + + + +int idx[100005], llink[100005]; + + + +bool inst[100005]; + + + +stack st; + + + +void tarjan(int node) + +{ + + idx[node] = llink[node] = ++k; + + st.push(node); + + inst[node] = true; + + + + for(int i = 0; i < v[node].size(); i++) + + if(idx[v[node][i]] == 0) + + { + + tarjan(v[node][i]); + + llink[node] = min(llink[node], llink[v[node][i]]); + + } + + else + + if(inst[v[node][i]]) + + llink[node] = min(llink[node], llink[v[node][i]]); + + + + if(idx[node] == llink[node]) + + { + + temp.clear(); + + + + int naux = 0; + + + + while(naux != node) + + { + + naux = st.top(); + + + + temp.push_back(naux); + + + + st.pop(); + + + + inst[naux] = false; + + } + + + + sol.push_back(temp); + + } + + + +} + + + +int main() + +{ + + fin >> n >> m; + + + + for(int i = 1; i <= m; i++) + + { + + int x, y; + + + + fin >> x >> y; + + + + v[x].push_back(y); + + } + + + + for(int i = 1; i <= n; i++) + + if(idx[i] == 0) + + tarjan(i); + + + + fout << sol.size() << '\n'; + + + + for(int i = 0; i < sol.size(); i++) + + { + + for(int j = 0; j < sol[i].size(); j++) + + fout << sol[i][j] << " "; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/ac9b0598-308b-4de0-82ff-5ef462f9e73d.cpp b/scc/tarjan/ac9b0598-308b-4de0-82ff-5ef462f9e73d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba70f7a47a8f6c4357fbb2537574923f3ca83879 --- /dev/null +++ b/scc/tarjan/ac9b0598-308b-4de0-82ff-5ef462f9e73d.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorgraph[100005]; + +vectorrezultate[100005]; + +stacks; + + + +int n, m, a, b, llk[100005],ind_rez, ix[100005]; + +int indice; + +bool viz[100005]; + + + +void adaugare(int ind) + +{ + + viz[ind]=1; + + ix[ind]=llk[ind]=++indice; + + s.push(ind); + + for (auto &v:graph[ind]) + + { + + if (!ix[v]) + + { + + adaugare(v); + + llk[ind]=min(llk[v],llk[ind]); + + } + + else if (viz[v]) + + { + + llk[ind]=min(llk[ind],llk[v]); + + } + + } + + if (ix[ind]==llk[ind]) + + { + + while(!s.empty()) + + { + + int nod=s.top(); + + viz[nod]=0; + + s.pop(); + + if (llk[nod]==ix[nod]) + + { + + rezultate[ind_rez++].push_back(nod); + + break; + + } + + rezultate[ind_rez].push_back(nod); + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1; i<=m; ++i) + + { + + f >> a >> b; + + graph[a].push_back(b); + + } + + for (int i=1; i<=n; ++i) + + { + + if (!ix[i]) + + { + + adaugare(i); + + } + + } + + g << ind_rez << '\n'; + + for (int i=0; i +#include + + +using namespace std; + + + +const int MAXN = 100001; + + + +vector g[MAXN], dfs_st; + +bool viz[MAXN]; + +bool onS[MAXN]; + +int min_back[MAXN]; + +vector > sol; + + + +FILE *in, *out; + + + +void get_ctc(int v) { + + vector rez = vector (); + + while(dfs_st.back() != v) { + + rez.push_back(dfs_st.back()); + + onS[dfs_st.back()] = false; + + dfs_st.pop_back(); + + } + + + + rez.push_back(v); + + dfs_st.pop_back(); + + onS[v] = false; + + sol.push_back(rez); + +} + + + +int tarj(int n) { + + + + + + viz[n] = true; + + min_back[n] = dfs_st.size(); + + int poz = min_back[n]; + + dfs_st.push_back(n); + + onS[n] = true; + + for(auto &it : g[n]) { + + if(!viz[it]) { + + int x = tarj(it); + + min_back[n] = min(min_back[n], x); + + } else { + + if(onS[it]) { + + min_back[n] = min(min_back[n], min_back[it]); + + } + + } + + } + + + + if(min_back[n] == poz) { + + get_ctc(n); + + } + + + + + + + + return min_back[n]; + +} +int main () { + + in = fopen("ctc.in", "r"); + + out = fopen("ctc.out", "w"); + + + + int n, m; + + fscanf(in, "%d %d", &n, &m); + + + + for(int i = 0; i < m; i++) { + + int x, y; + + fscanf(in, "%d %d", &x, &y); + + g[x].push_back(y); + + } + + + + fclose(in); + + + + for(int i = 1; i <= n; i++) { + + if(!viz[i]) { + + tarj(i); + + } + + } + + + + fprintf(out, "%d\n", sol.size()); + + for(auto &ctc : sol) { + + for(auto &elem : ctc) { + + fprintf(out, "%d ", elem); + + } + + fprintf(out, "\n"); + + } + + + + fclose(out); + + + + return 0; + +} diff --git a/scc/tarjan/ae20d759-b908-498b-8bf0-f813c520bb67.cpp b/scc/tarjan/ae20d759-b908-498b-8bf0-f813c520bb67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9baca5ea88e8b5e7c9de1ad1a9ab7efc2bfd921c --- /dev/null +++ b/scc/tarjan/ae20d759-b908-498b-8bf0-f813c520bb67.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + + + +class Task { + +public: + void solve() { + read_input(); + get_result(); + print_output(); + } + +private: + vector adj[100010]; + vector> ctc; + vector found; + vector low_link; + vector in_stack; + stack st; + int n, m; + + void read_input() { + ifstream fin("ctc.in"); + fin >> n >> m; + in_stack = vector(n + 1, 0); + found = vector(n + 1, -1); + low_link = vector(n + 1, 0); + int x, y; + for (int i = 0; i < m; ++i) { + fin >> x >> y; + adj[x].push_back(y); + } + } + + void get_result() { + int current_start = 0; + for (int i = 1; i <= n; ++i) { + if (found[i] == -1) { + tarjan(i, current_start); + } + } + } + + void tarjan(int node, int ¤t_start) { + found[node] = current_start; + low_link[node] = current_start; + in_stack[node] = 1; + st.push(node); + current_start = current_start + 1; + for (int neighbour : adj[node]) { + if (found[neighbour] == -1) { + tarjan(neighbour, current_start); + low_link[node] = min(low_link[node], low_link[neighbour]); + } else { + if (in_stack[neighbour] == 1) { + low_link[node] = min(low_link[node], found[neighbour]); + } + } + } + if (low_link[node] == found[node]) { + int current; + vector new_ctc; + while (!st.empty() && current != node) { + current = st.top(); + st.pop(); + new_ctc.push_back(current); + in_stack[current] = 0; + } + ctc.push_back(new_ctc); + } + } + + void print_output() { + ofstream fout("ctc.out"); + fout << ctc.size() << "\n"; + for (vector comp : ctc) { + for (int el : comp) { + fout << el << " "; + } + fout << "\n"; + } + } +}; + +int main() { + Task *task = new Task(); + task->solve(); + delete task; + return 0; +} diff --git a/scc/tarjan/ae5d116b-5bc0-4899-8938-59c98d4ed1d8.cpp b/scc/tarjan/ae5d116b-5bc0-4899-8938-59c98d4ed1d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..288fe4d9ce0287ccfa685c3ee4037272b269153d --- /dev/null +++ b/scc/tarjan/ae5d116b-5bc0-4899-8938-59c98d4ed1d8.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, l, sol; + +pair p[100005]; + +vector graph[100005], out[100005]; + +stack stk; + +bitset<100005> check, inS; + + + +void dfs(int now); + + + +int main() + +{ + + + + fin >> n >> m; + + while(m--){ + + int x, y; + + fin >> x >> y; + + + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(!check[i]) dfs(i); + + } + + + + fout << sol << "\n"; + + for(int i = 1; i <= sol; ++i){ + + for(auto it:out[i]) fout << it << " "; + + fout << "\n"; + + } + + + + return 0; + +} + + + +void dfs(int now){ + + ++l; + + p[now] = {l, l}; + + check[now] = inS[now] = true; + + stk.push(now); + + + + for(auto next:graph[now]){ + + if(!check[next]){ + + dfs(next); + + p[now].second = min(p[now].second, p[next].second); + + } + + else if(inS[next]) p[now].second = min(p[now].second, p[next].second); + + } + + + + if(p[now].first == p[now].second){ + + ++sol; + + + + while(stk.top() != now){ + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + +} diff --git a/scc/tarjan/ae7fbfc2-6085-42eb-a5c6-6474b74c4bc0.cpp b/scc/tarjan/ae7fbfc2-6085-42eb-a5c6-6474b74c4bc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b22bb0679ded52c03076b81b8602562e95226cf9 --- /dev/null +++ b/scc/tarjan/ae7fbfc2-6085-42eb-a5c6-6474b74c4bc0.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +const int DIM = 100000 + 5; + + + +vector < int > a[DIM]; + +vector < vector < int > > sol; + +stack < int > st; + + + +int disc[DIM], low[DIM], temp; + +bool in_stack[DIM]; + + + +void dfs(int k) + +{ + + disc[k] = low[k] = ++temp; + + st.push(k); + + in_stack[k] = true; + + for(auto v : a[k]) { + + if(disc[v] == 0) { + + dfs(v); + + low[k] = min(low[k], low[v]); + + } + + else if(in_stack[v] == true) low[k] = min(low[k], low[v]); + + } + + if(disc[k] == low[k]) { + + vector < int > add; + + int node = k; + + do { + + node = st.top(); + + add.push_back(node); + + in_stack[node] = false; + + st.pop(); + + } + + while(node != k); + + sol.push_back(add); + + } + +} + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y; + + + + fin >> x >> y; + + a[x].push_back(y); + + } + + for(int i = 1; i <= n; ++i) { + + if(disc[i] == false) dfs(i); + + } + + fout << sol.size() << "\n"; + + for(auto v : sol) { + + for(auto w : v) { + + fout << w << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/af57f11f-19a1-4282-b4a3-054e5a1b26eb.cpp b/scc/tarjan/af57f11f-19a1-4282-b4a3-054e5a1b26eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7a53acc28bd60f1122a901a5febce4620a082b2 --- /dev/null +++ b/scc/tarjan/af57f11f-19a1-4282-b4a3-054e5a1b26eb.cpp @@ -0,0 +1,143 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +vector ans[DMAX]; + +vector arb[DMAX]; + +stack s; + + + +int uz[DMAX]; + +int lowlink[DMAX]; + + + +int n,m,comp,cnt; + + + +void dfsT(int node); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + arb[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!uz[i]) + + dfsT(i); + + fout< 0) + + lowlink[node]=min(lowlink[node],lowlink[it]); + + } + + if(uz[node] == lowlink[node]){ + + comp++; + + int node2; + + do{ + + node2=s.top(); + + ans[comp].push_back(node2); + + s.pop(); + + uz[node2]=-1; + + if(node2 == node) + + break; + + }while(true); + + } + +} diff --git a/scc/tarjan/af99049e-c4f9-4a2a-82a4-ae6bd3578728.cpp b/scc/tarjan/af99049e-c4f9-4a2a-82a4-ae6bd3578728.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88c6609737e5169b3d38f1d8e904ddbf9940da38 --- /dev/null +++ b/scc/tarjan/af99049e-c4f9-4a2a-82a4-ae6bd3578728.cpp @@ -0,0 +1,148 @@ +#include + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int noduri, muchii, componente_tari, a, b, ind = 1, ids[100005]; + +bool instack[100005]; + +int lowkey[100005]; + +vector mat[100005]; + +vector componente[100005]; + +stack s; + + + +void dfs(int node) + +{ + + s.push(node); + + instack[node] = 1; + + ids[node] = ind; + + lowkey[node] = ind; + + ++ind; + + for(int i = 0; i < mat[node].size(); ++i) + + { + + int next_node = mat[node][i]; + + if(!ids[next_node]) + + { + + dfs(next_node); + + } + + if(instack[next_node]) + + { + + lowkey[node] = min(lowkey[node], lowkey[next_node]); + + } + + } + + if(ids[node] == lowkey[node]) + + { + + while(!s.empty()) + + { + + int next = s.top(); + + componente[componente_tari].push_back(next); + + s.pop(); + + instack[next] = 0; + + lowkey[next] = ids[node]; + + if(node == next) break; + + } + + componente_tari++; + + } + +} + + + +int main() + +{ + + fin >> noduri >> muchii; + + for(int i = 1; i <= muchii; ++i) + + { + + fin >> a >> b; + + mat[a].push_back(b); + + } + + for(int node = 1; node <= noduri; ++node) + + { + + if(!ids[node]) + + { + + dfs(node); + + } + + } + + fout << componente_tari << "\n"; + + for(int i = 0; i < componente_tari; ++i) + + { + + for(int j = 0; j < componente[i].size(); ++j) + + { + + fout << componente[i][j] << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/afe2d433-e5c1-4c2d-b3f4-eac10f4fc24b.cpp b/scc/tarjan/afe2d433-e5c1-4c2d-b3f4-eac10f4fc24b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..210aac38e40f6d8ebcb23eadc791793d5a5a3522 --- /dev/null +++ b/scc/tarjan/afe2d433-e5c1-4c2d-b3f4-eac10f4fc24b.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + +int n, m; + +vector g[1 + NMAX]; + +vector scc[1 + NMAX]; + + + +int nComp; + +stack st; + +int time1[1 + NMAX]; + +int lowLink[1 + NMAX]; + +int t = 0; + +bool onStack[1 + NMAX]; + + + +void tarjan(int from) { + + + + t++; + + time1[from] = t; + + lowLink[from] = t; + + st.push(from); + + onStack[from] = true; + + for(int to : g[from]) { + + if(time1[to] == 0) { + + tarjan(to); + + lowLink[from] = min(lowLink[from], lowLink[to]); + + } else if(onStack[to]) { + + lowLink[from] = min(lowLink[from], lowLink[to]); + + } + + } + + + + if(lowLink[from] == time1[from]) { + + + + while(!st.empty() && lowLink[from] <= lowLink[st.top()]) { + + scc[nComp].push_back(st.top()); + + onStack[st.top()] = false; + + st.pop(); + + } + + + + nComp++; + + } + +} + + + +int main(){ + + in >> n >> m; + + for(int i = 1; i <= m; i++) { + + int a, b; + + in >> a >> b; + + g[a].push_back(b); + + } + + for(int i = 1; i <= n; i++) { + + if(time1[i] == 0) { + + tarjan(i); + + } + + } + + out << nComp << "\n"; + + for(int i = 0; i < nComp; i++) { + + for(int j = 0; j < scc[i].size(); j++) { + + out << scc[i][j] << " "; + + } + + out << "\n"; + + } + +} diff --git a/scc/tarjan/b00b0d1f-6c15-436b-b14f-5381e2ffa837.cpp b/scc/tarjan/b00b0d1f-6c15-436b-b14f-5381e2ffa837.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4a9c80ef18a275a3e667b53ad3ae8f5bf77c906 --- /dev/null +++ b/scc/tarjan/b00b0d1f-6c15-436b-b14f-5381e2ffa837.cpp @@ -0,0 +1,177 @@ +#include +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int N,M,x,y; + +vector Graph[100004]; + +int ids[100004]; + +int lowLink[100004]; + +bool OnStack[100004]; + +stack Stack; + +int id = 0; + +int counter = 0; + +int check=0; + +vector answer[100004]; + + + +void DFS(int nod) + +{ + + + + Stack.push(nod); + + OnStack[nod] = true; + + + + + + ids[nod] = id; + + + + lowLink[nod] = id; + + + + id++; + + + + for(int i=0; i>N>>M; + + for(int i=1; i<=M; i++) + + { + + in>>x>>y; + + Graph[x].push_back(y); + + } + + + + memset(ids,0,sizeof(ids)); + + memset(lowLink,0, sizeof(lowLink)); + + memset(OnStack,0,sizeof(OnStack)); + + + + for(int i=1; i<=N; i++) + + if(ids[i]==0) + + DFS(i); + + + + + + for(int i=1; i<=N; i++) + + answer[lowLink[i]].push_back(i); + + for(int i=0; i<100004; i++) + + if(answer[i].size()!=0) + + check+=1; + + out< +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorgraph[100005]; + +vectorcomp[100005]; + +stack S; + + + +int viz[100005], is_in_stack[100005], idx[100005], low_link[100005]; + +int index, nrc, n, m, x, y; + + + +void citire(){ + + f>>n>>m; + + for(int i=0; i>x>>y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int vf){ + + + + low_link[vf] = idx[vf] = index; + + index++; + + + + viz[vf] = 1; + + is_in_stack[vf] = 1; + + S.push(vf); + + + + for(auto &v:graph[vf]){ + + if(viz[v] == 0){ + + tarjan(v); + + low_link[vf] = min(low_link[v], low_link[vf]); + + } + + else if(is_in_stack[v] == 1){ + + low_link[vf] = min(low_link[v], low_link[vf]); + + } + + } + + + + if(idx[vf] == low_link[vf]){ + + nrc++; + + int el; + + do{ + + el = S.top(); + + comp[nrc].push_back(el); + + S.pop(); + + is_in_stack[el] = 0; + + }while(el != vf); + + } + +} + +void afis(){ + + g< + + +using namespace std; + + + +ifstream inf("ctc.in"); + +ofstream outf("ctc.out"); + + + +const int N=100010; + + + +int n, m, disc[N], low[N], t; + +bitset b, vis; + +vector v[N]; + +stack s; + +vector> sol; + + + +void ctc(int ); + + + +int main() + +{ + + inf>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x, y; + + inf>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + disc[i]=low[i]=-1; + + for(int i=1; i<=n; i++) + + if(!vis[i]) + + ctc(i); + + outf< cctc; + + do + + { + + tp=s.top(); + + s.pop(); + + b[tp]=0; + + cctc.push_back(tp); + + }while(tp!=nod); + + sol.push_back(cctc); + + } + +} diff --git a/scc/tarjan/b0540454-6a9e-4d46-b55d-7deb07f4714b.cpp b/scc/tarjan/b0540454-6a9e-4d46-b55d-7deb07f4714b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..761d3e3b57829d05d4a56700938f7a3a9bcb146f --- /dev/null +++ b/scc/tarjan/b0540454-6a9e-4d46-b55d-7deb07f4714b.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 100001; + +int id=0, tot=0; + +string resp=""; + +vector graf[NMAX]; + +int ids[NMAX]; + +int low[NMAX]; + +bool onStack[NMAX]; + +stack s; + + + +void DFS(int curr) { + + onStack[curr] = true; + + ids[curr] = ++id; + + low[curr] = id; + + s.push(curr); + + for (int i = 0; i < graf[curr].size(); i++) { + + if (ids[graf[curr][i]] == 0) { + + DFS(graf[curr][i]); + + } + + if (onStack[graf[curr][i]]) { + + low[curr] = min(low[curr], low[graf[curr][i]]); + + } + + } + + + + if (ids[curr] == low[curr]) { + + while (!s.empty()) { + + int node = s.top(); + + resp += to_string(node); + + resp += " "; + + s.pop(); + + onStack[node] = false; + + low[node] = ids[curr]; + + if (node == curr) { + + break; + + } + + } + + tot++; + + resp += "\n"; + + } + +} + + + +int main() { + + ifstream in("ctc.in"); + + ofstream out("ctc.out"); + + int n, m, x, y; + + in >> n >> m; + + + + for (int i = 0; i < m; i++) { + + in >> x >> y; + + graf[x].push_back(y); + + } + + + + for (int i = 1; i <= n; i++) { + + if (ids[i] == 0) { + + DFS(i); + + } + + } + + + + out << tot << "\n" << resp; + + return 0; + +} diff --git a/scc/tarjan/b15f8fb7-ccb8-4b93-b262-4b04e34ed7a4.cpp b/scc/tarjan/b15f8fb7-ccb8-4b93-b262-4b04e34ed7a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaa5d2f29b99001202fb6dd0a6705bba3c032626 --- /dev/null +++ b/scc/tarjan/b15f8fb7-ccb8-4b93-b262-4b04e34ed7a4.cpp @@ -0,0 +1,52 @@ +#include + + + + + + +using namespace std; + +template +void SCC(Graph& graph, CB cb) { + int n = graph.size(), timer = 0; + vector val(n, 0), stk, cont; + function dfs = [&](int node) { + int low; low = val[node] = ++timer; + int sz = stk.size(); stk.push_back(node); + for (auto vec : graph[node]) + if (val[vec] != -1) + low = min(low, val[vec] ?: dfs(vec)); + if (low == val[node]) { + cont = {stk.begin() + sz, stk.end()}; + for (auto x : cont) val[x] = -1; + cb(cont); stk.resize(sz); + } else val[node] = low; + return low; + }; + for (int i = 0; i < n; ++i) + if (val[i] != -1) + dfs(i); +} + +int main() { + ifstream cin("ctc.in"); + ofstream cout("ctc.out"); + int n, m; cin >> n >> m; + vector> graph(n); + for (int i = 0; i < m; ++i) { + int a, b; cin >> a >> b; + graph[--a].push_back(--b); + } + vector> comps; + SCC(graph, [&](vector comp) { + comps.push_back(comp); + }); + cout << comps.size() << '\n'; + for (auto comp : comps) { + for (auto x : comp) + cout << x + 1 << " "; + cout << '\n'; + } + return 0; +} diff --git a/scc/tarjan/b1be2025-e872-4d07-b50f-0b78862e0ec5.cpp b/scc/tarjan/b1be2025-e872-4d07-b50f-0b78862e0ec5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4260dc8ea0fb67814f1e12eb6b4621cb38f2cd77 --- /dev/null +++ b/scc/tarjan/b1be2025-e872-4d07-b50f-0b78862e0ec5.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + + nivel++; + niv[nod] = nivel; + low[nod] = nivel; + s.push(nod); + fr[nod] = 1; + + for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + + if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); + } + +} + +int main() { + in>>n>>m; + for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); + } + + for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + + out< +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector>G; + +vector>CC; + +vectorC; + +stackS; + +int L[100001],niv[100001]; + +bool InS[100001]; + +int n,m,nv; + +void Citire() + +{ + + fin>>n>>m; + + G.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + fin>>x>>y; + + G[x].push_back(y); + + } + +} + +void Afisare() + +{ + + fout< +#include +#include +#include +#include +using namespace std; + +using VI = vector; +using VVI = vector; +using VB = vector; + +int n, m; +VVI G, ctc; +VI niv, L, c; +VB inStk; +stack stk; + +void Read(); +void Tarjan(int x, int& level); +void Write(); + +int main() +{ + Read(); + int level{1}; + for (int i = 1; i <= n; i++) + if (!niv[i]) + Tarjan(i, level); + Write(); +} + +void Tarjan(int x, int& level) +{ + niv[x] = L[x] = level; + level++; + stk.push(x); + inStk[x] = true; + + for (const int& y : G[x]) + { + if (!niv[y]) + { + Tarjan(y, level); + L[x] = min(L[x], L[y]); + } + else + if (inStk[y]) + L[x] = min(L[x], niv[y]); + } + + if (L[x] == niv[x]) + { + c = VI(); + while (!stk.empty()) + { + int y = stk.top(); + stk.pop(); + inStk[y] = false; + c.emplace_back(y); + if (y == x) + break; + } + ctc.emplace_back(c); + } +} + +void Write() +{ + ofstream fout("ctc.out"); + fout << ctc.size() << '\n'; + for (const VI& c : ctc) + { + for (const int& x : c) + fout << x << ' '; + fout << '\n'; + } +} + +void Read() +{ + ifstream fin("ctc.in"); + fin >> n >> m; + G = VVI(n + 1); + niv = L = VI(n + 1); + inStk = VB(n + 1); + + int x, y; + for (int i = 0; i < m; i++) + { + fin >> x >> y; + G[x].emplace_back(y); + } +} diff --git a/scc/tarjan/b399a4d4-27e4-40ed-ab26-982bb8ccbc07.cpp b/scc/tarjan/b399a4d4-27e4-40ed-ab26-982bb8ccbc07.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f388a79cf0f78bd1cfdb3dcd48cbd7d34866ee1e --- /dev/null +++ b/scc/tarjan/b399a4d4-27e4-40ed-ab26-982bb8ccbc07.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, cnt = 0, niv = 0, Niv[100001], NivMin[100001]; + +vector G[100001], Ctc[100001]; + +stack Stack; + +bool V[100001], InStack[100001]; + + + +void Dfs(int x) + +{ + + V[x] = true; + + ++niv; + + Niv[x] = NivMin[x] = niv; + + Stack.push(x); + + InStack[x] = true; + + for (int y : G[x]) + + { + + if (!V[y]) + + { + + Dfs(y); + + NivMin[x] = min(NivMin[x], NivMin[y]); + + } + + else if (InStack[y]) + + NivMin[x] = min(NivMin[x], NivMin[y]); + + } + + if (Niv[x] == NivMin[x]) + + { + + ++cnt; + + while (true) + + { + + Ctc[cnt].push_back(Stack.top()); + + InStack[Stack.top()] = false; + + if (Stack.top() == x) + + { + + Stack.pop(); + + break; + + } + + Stack.pop(); + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i) + + { + + if (!V[i]) + + Dfs(i); + + } + + fout << cnt << '\n'; + + for (int i = 1; i <= cnt; ++i) + + { + + for (int x : Ctc[i]) + + fout << x << " "; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/b4536738-a0a5-485a-b46e-b4c262a92ba3.cpp b/scc/tarjan/b4536738-a0a5-485a-b46e-b4c262a92ba3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77abe53657f523419dd77b73baf24a475fc012e9 --- /dev/null +++ b/scc/tarjan/b4536738-a0a5-485a-b46e-b4c262a92ba3.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +int n, m, sccCount = 0, cnt = 0; +stack stiva; +bool onSt[1000001]; +int vis[1000001], low[1000001]; +vector adj[1000001], con; +vector> result; + +void dfs(int node) +{ + stiva.push(node); + onSt[node] = true; + ++cnt; + vis[node] = cnt; + low[node] = cnt; + + for (int nbh : adj[node]) + { + if (vis[nbh] == 0) + dfs(nbh); + if (onSt[nbh]) + low[node] = min(low[nbh], low[node]); + } + if (vis[node] == low[node]) + { + ++sccCount; + con.clear(); + while (stiva.top() != node) + { + onSt[stiva.top()] = false; + low[stiva.top()] = vis[node]; + con.push_back(stiva.top()); + stiva.pop(); + } + onSt[node] = false; + low[node] = vis[node]; + con.push_back(node); + stiva.pop(); + result.push_back(con); + } +} + +int main() +{ + fin >> n >> m; + for (int i = 0; i < m; ++i) + { + int x, y; + fin >> x >> y; + adj[x].push_back(y); + } + + for (int i = 1; i <= n; ++i) + if (vis[i] == 0) + dfs(i); + fout << sccCount << "\n"; + for (int i = 0; i < result.size(); ++i) + { + for (int j = 0; j < result[i].size(); ++j) + fout << result[i][j] << " "; + fout << "\n"; + } + return 0; +} diff --git a/scc/tarjan/b8886c9b-e896-43dd-b676-eaefecced7e2.cpp b/scc/tarjan/b8886c9b-e896-43dd-b676-eaefecced7e2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23b1f7fd1b743f5f3cac5675bf22f597a90fe5e7 --- /dev/null +++ b/scc/tarjan/b8886c9b-e896-43dd-b676-eaefecced7e2.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int i,n,m,x,y,niv[100002],low[100002],f[100002],nivel,ctc; + +vector v[100002],sol[100002]; + +stack s; + +void dfs(int nod){ + + + + niv[nod]=low[nod]=++nivel; + + s.push(nod); + + f[nod]=1; + + for(auto vecin: v[nod]) + + if(!niv[vecin]){ + + dfs(vecin); + + low[nod]=min(low[vecin],low[nod]); + + }else if(f[vecin]) + + low[nod]=min(low[vecin],low[nod]); + + + + if(niv[nod]==low[nod]){ + + ctc++; + + int x=0; + + while(x!=nod){ + + x=s.top(); + + sol[ctc].push_back(x); + + f[x]=0; + + s.pop(); + + } + + } + +} + +int main(){ + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + v[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!niv[i]) + + dfs(i); + + fout< +#include +#include +#include + +using namespace std; + +const int NMAX = 1e5; + + + +vector g[1 + NMAX]; +vector rasp[1 + NMAX]; +int n, m; + +void citire() { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; i ++) { + int u, v; + scanf("%d%d", &u, &v); + + g[u].push_back(v); + } +} + + + +int nrComponenteConexte, idCurent; +int lowLink[1 + NMAX], nodeID[1 + NMAX]; +bool visited[1 + NMAX], onStack[1 + NMAX]; +stack myStack; + +void TarjanDFS(int from) { + visited[from] = true; + onStack[from] = true; + myStack.push(from); + lowLink[from] = nodeID[from] = ++idCurent; + + for(auto &to : g[from]) { + if(visited[to] == false) { + TarjanDFS(to); + } + if(onStack[to] == true) { + lowLink[from] = min(lowLink[from], lowLink[to]); + } + } + + if(lowLink[from] == nodeID[from]) { + + while(!myStack.empty() && myStack.top() != from) { + lowLink[myStack.top()] = from; + rasp[nrComponenteConexte].push_back(myStack.top()); + + onStack[myStack.top()] = false; + myStack.pop(); + } + rasp[nrComponenteConexte].push_back(myStack.top()); + onStack[myStack.top()] = false; + myStack.pop(); + + nrComponenteConexte ++; + } +} + +void TarjanSSC() { + for(int i = 1; i <= n; i ++) { + if(visited[i] == false) { + TarjanDFS(i); + } + } +} + + + +void afisare() { + printf("%d\n", nrComponenteConexte); + for(int i = 0; i < nrComponenteConexte; i ++) { + for(auto &nod: rasp[i]) { + printf("%d ", nod); + } + printf("\n"); + } +} + +int main() { + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + + citire(); + TarjanSSC(); + afisare(); + + return 0; +} diff --git a/scc/tarjan/baca11ef-8cd2-48e9-847f-8e5fe37c86b8.cpp b/scc/tarjan/baca11ef-8cd2-48e9-847f-8e5fe37c86b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ac040fb08551cd59a15f7a0f11c6184b3912d6f --- /dev/null +++ b/scc/tarjan/baca11ef-8cd2-48e9-847f-8e5fe37c86b8.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +std::ifstream in("ctc" ".in"); + +std::ofstream out("ctc" ".out"); + +const int N = 100000; + +std::vector L[N], S; + +int LL[N], ID[N], nId; + +std::bitset E, pS; + +std::vector> CTC; + +void dfs(int t) + +{ + + pS[t] = true; + + ID[t] = LL[t] = nId++; + + S.push_back(t); + + for (int f: L[t]) + + if (not E[f]) + + { + + if (not pS[f]) + + dfs(f); + + LL[t] = std::min(LL[t], LL[f]); + + } + + if (ID[t] == LL[t]) + + { + + CTC.emplace_back(); + + int back; + + do + + { + + back = S.back(); + + S.pop_back(); + + CTC.back().push_back(back); + + E[back] = true; + + } + + while (back != t); + + } + +} + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + int a, b; + + in >> a >> b; + + L[--a].push_back(--b); + + } + + for (int i = 0; i < n; i++) + + if (not E[i]) + + dfs(i); + + out << CTC.size() << '\n'; + + for (auto ctc: CTC) + + { + + for (int i: ctc) + + out << i + 1 << ' '; + + out << '\n'; + + } + +} diff --git a/scc/tarjan/baf93fa1-da66-4843-93fc-3196da7c5ad8.cpp b/scc/tarjan/baf93fa1-da66-4843-93fc-3196da7c5ad8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ba70f7a47a8f6c4357fbb2537574923f3ca83879 --- /dev/null +++ b/scc/tarjan/baf93fa1-da66-4843-93fc-3196da7c5ad8.cpp @@ -0,0 +1,148 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorgraph[100005]; + +vectorrezultate[100005]; + +stacks; + + + +int n, m, a, b, llk[100005],ind_rez, ix[100005]; + +int indice; + +bool viz[100005]; + + + +void adaugare(int ind) + +{ + + viz[ind]=1; + + ix[ind]=llk[ind]=++indice; + + s.push(ind); + + for (auto &v:graph[ind]) + + { + + if (!ix[v]) + + { + + adaugare(v); + + llk[ind]=min(llk[v],llk[ind]); + + } + + else if (viz[v]) + + { + + llk[ind]=min(llk[ind],llk[v]); + + } + + } + + if (ix[ind]==llk[ind]) + + { + + while(!s.empty()) + + { + + int nod=s.top(); + + viz[nod]=0; + + s.pop(); + + if (llk[nod]==ix[nod]) + + { + + rezultate[ind_rez++].push_back(nod); + + break; + + } + + rezultate[ind_rez].push_back(nod); + + } + + } + +} + + + +int main() + +{ + + f >> n >> m; + + for (int i=1; i<=m; ++i) + + { + + f >> a >> b; + + graph[a].push_back(b); + + } + + for (int i=1; i<=n; ++i) + + { + + if (!ix[i]) + + { + + adaugare(i); + + } + + } + + g << ind_rez << '\n'; + + for (int i=0; i +#include +#include +#include + +using namespace std; + +const int NMAX = 1e5; + + + +vector g[1 + NMAX]; +vector rasp[1 + NMAX]; +int n, m; + +void citire() { + scanf("%d%d", &n, &m); + for(int i = 0; i < m; i ++) { + int u, v; + scanf("%d%d", &u, &v); + + g[u].push_back(v); + } +} + + + +int nrComponenteConexte, idCurent; +int lowLink[1 + NMAX], nodeID[1 + NMAX]; +bool visited[1 + NMAX], onStack[1 + NMAX]; +stack myStack; + +void TarjanDFS(int from) { + visited[from] = true; + onStack[from] = true; + myStack.push(from); + lowLink[from] = nodeID[from] = ++idCurent; + + for(auto &to : g[from]) { + if(visited[to] == false) { + TarjanDFS(to); + } + if(onStack[to] == true) { + lowLink[from] = min(lowLink[from], lowLink[to]); + } + } + + if(lowLink[from] == nodeID[from]) { + + while(!myStack.empty() && myStack.top() != from) { + lowLink[myStack.top()] = from; + rasp[nrComponenteConexte].push_back(myStack.top()); + + onStack[myStack.top()] = false; + myStack.pop(); + } + rasp[nrComponenteConexte].push_back(myStack.top()); + onStack[myStack.top()] = false; + myStack.pop(); + + nrComponenteConexte ++; + } +} + +void TarjanSSC() { + for(int i = 1; i <= n; i ++) { + if(visited[i] == false) { + TarjanDFS(i); + } + } +} + + + +void afisare() { + printf("%d\n", nrComponenteConexte); + for(int i = 0; i < nrComponenteConexte; i ++) { + for(auto &nod: rasp[i]) { + printf("%d ", nod); + } + printf("\n"); + } +} + +int main() { + freopen("ctc.in", "r", stdin); + freopen("ctc.out", "w", stdout); + + citire(); + TarjanSSC(); + afisare(); + + return 0; +} diff --git a/scc/tarjan/bb85479d-75f0-4d9c-be4f-5f32f13ce54d.cpp b/scc/tarjan/bb85479d-75f0-4d9c-be4f-5f32f13ce54d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9eb48c123116b0233078bb42e6e4e44e9d89d5ac --- /dev/null +++ b/scc/tarjan/bb85479d-75f0-4d9c-be4f-5f32f13ce54d.cpp @@ -0,0 +1,110 @@ +#include +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +int nctc; + +int low[100005], disc[100005], _time; + +std::vector ctc[100005]; + +bool inStack[100005]; + +std::stack stack; + +void DFS(int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + + + for (auto &it:ADC[vertex]) { + + if (disc[it]) { + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + else { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (low[vertex] == disc[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + ++ nctc; + + } + +} + + + +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int main() + +{ + + input >> N >> M; + + for (int i=1, u, v; i<=M; ++i) input >> u >> v, addEdge(u, v); + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + output << nctc << '\n'; + + for (int i=0; i +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,nrOrd; + +vector ord; + +vector lowLink; + +vectorinStack; + +stack st; + + + +vector> v; + +vector> sccs; + + + +void Read() + +{ + + f>>n>>m; + + v.resize(m+1); + + ord.resize(n+1); + + lowLink.resize(n+1); + + inStack.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + v[x].emplace_back(y); + + } + + f.close(); + +} + + + +void dfs(int nod) + +{ + + st.push(nod); + + inStack[nod] = true; + + lowLink[nod] = ord[nod] = ++nrOrd; + + + + for(auto& vecin : v[nod]) + + { + + if(!ord[vecin]) + + { + + dfs(vecin); + + lowLink[nod] = min(lowLink[nod], lowLink[vecin]); + + } + + else if(inStack[vecin]) + + lowLink[nod] = min(lowLink[nod], lowLink[vecin]); + + } + + + + if(lowLink[nod] == ord[nod]) + + { + + sccs.emplace_back(); + + while(true) + + { + + int top = st.top(); + + st.pop(); + + inStack[top] = false; + + + + lowLink[top] = ord[nod]; + + sccs.back().push_back(top); + + if(top == nod) + + break; + + } + + } + +} + + + +int main() + +{ + + Read(); + + dfs(1); + + for(int i=1;i<=n;i++) + + if(!ord[i]) + + dfs(i); + + + + g< + +std::ifstream fin("ctc.in"); +std::ofstream fout("ctc.out"); + +int n, m, nodeIndex; +std::vector nei[100001], id, lowLink; +std::vector inStack; +std::stack stack; +std::vector> comp; + +void dfs(int x){ + id[x] = lowLink[x] = nodeIndex++; + stack.push(x); + inStack[x] = true; + for(auto node: nei[x]){ + if(id[node] == -1) + dfs(node); + if(inStack[node]) + lowLink[x] = std::min(lowLink[node], lowLink[x]); + } + + if(id[x] == lowLink[x]){ + std::vector aux; + while(stack.top() != x){ + inStack[stack.top()] = false; + aux.push_back(stack.top()); + stack.pop(); + } + inStack[stack.top()] = false; + aux.push_back(stack.top()); + stack.pop(); + comp.push_back(aux); + } +} + + +int main() +{ + int x, y, i; + fin >> n >> m; + id.resize(n + 1, -1); + lowLink.resize(n + 1); + inStack.resize(n + 1, false); + + for(i = 0; i < m; i++){ + fin >> x >> y; + nei[x].push_back(y); + } + + for(i = 1; i <= n; i++) + if(id[i] == -1) + dfs(i); + + fout << comp.size() << '\n'; + for(auto i: comp){ + for(auto j: i) + fout << j << " "; + fout << '\n'; + } + + + + + return 0; +} diff --git a/scc/tarjan/be8e4a62-c17c-43ee-aa32-9f217a9b1a1b.cpp b/scc/tarjan/be8e4a62-c17c-43ee-aa32-9f217a9b1a1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff1b6dbb770c6477c94ca76c003262c279a903a6 --- /dev/null +++ b/scc/tarjan/be8e4a62-c17c-43ee-aa32-9f217a9b1a1b.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include + + + + +using namespace std; + + + +int n, m, i, j, k, start, ord[100003], levMin[100003]; + +vector vec[100003], comp[100003]; + +stack stiva; + +set s; + + + +void func(int nod) { + + int p, x; + + + + ord[nod] = start; + + levMin[nod] = start; + + start++; + + stiva.push(nod); + + s.insert(nod); + + + + for(p = 0; p < vec[nod].size(); p++) + + if(!ord[vec[nod][p]]) { + + func(vec[nod][p]); + + levMin[nod] = min(levMin[nod], levMin[vec[nod][p]]); + + } + + else if(s.find(vec[nod][p]) != s.end()) + + levMin[nod] = min(levMin[nod], ord[vec[nod][p]]); + + + + if(ord[nod] == levMin[nod]) + + while(!stiva.empty()) { + + x = stiva.top(); + + stiva.pop(); + + s.erase(x); + + comp[k].push_back(x); + + if(x == nod) { + + k++; + + break; + + } + + } + +} + + + +int main() { + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + + + f >> n >> m; + + while(m) { + + f >> i >> j; + + + + vec[i].push_back(j); + + m--; + + } + + f.close(); + + + + k = 0; + + start = 1; + + for(i = 1; i <= n; i++) + + if(!ord[i]) + + func(i); + + g << k << '\n'; + + + + for(i = 0; i < k; i++, g << '\n') + + for(j = 0; j < comp[i].size(); j++) + + g << comp[i][j] << ' '; + + + + g.close(); + + + + return 0; + +} diff --git a/scc/tarjan/bec913ee-43ab-42ac-9f7c-fb240a76efdb.cpp b/scc/tarjan/bec913ee-43ab-42ac-9f7c-fb240a76efdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aefd11e8a65733c4d2c2d9b77a72744dc81d9f33 --- /dev/null +++ b/scc/tarjan/bec913ee-43ab-42ac-9f7c-fb240a76efdb.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +using namespace std; + +vector G[100010]; +vector> sol; +stack s; +int N,M; +vectorlowlink(100010,-1); +vectordisc(100010,-1); +vectoronStack(100010,false); + + +void DFS(int x) { + static int index = 0; + disc[x] = lowlink[x] = index; + index++; + s.push(x); + onStack[x] = true; + + for (int elem : G[x]) { + if (disc[elem] == -1) + DFS(elem); + if (onStack[elem]) + lowlink[x] = min(lowlink[x], lowlink[elem]); + } + if (disc[x] == lowlink[x]) { + vector aux; + + while (s.top() != x) { + aux.push_back(s.top()); + onStack[s.top()] = 0; + s.pop(); + } + + aux.push_back(s.top()); + onStack[s.top()] = 0; + s.pop(); + + sol.push_back(aux); + } +} +int main() +{ + + ifstream f ("ctc.in"); + ofstream g("ctc.out"); + + f>> N >>M; + int x,y; + for( int i = 0; i < M; i++ ) + { + f>>x>>y; + G[x].push_back(y); + + } + + + + + for(int i = 1; i <=N; i++) + if ( disc[i] == -1) + DFS(i); + + + g << sol.size() << "\n"; + for (auto vec : sol){ + for (int x : vec) + g<< x << " "; + g << "\n"; + } + return 0; +} diff --git a/scc/tarjan/bfe5cf2c-8d74-4c60-a49f-133a3a0145f0.cpp b/scc/tarjan/bfe5cf2c-8d74-4c60-a49f-133a3a0145f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a3ca5d03a6723eb62d91baa4652d1710ba74c750 --- /dev/null +++ b/scc/tarjan/bfe5cf2c-8d74-4c60-a49f-133a3a0145f0.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + stack &post_order_stack; + vector &in_stack; + + int current_time; + vector &found_time; + vector &lowlink; + + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + vector visited(n, false); + stack post_order_stack; + vector in_stack(n, false); + SCCs.resize(n); + vector found_time(n, -1); + vector lowlink(n, -1); + + traversal_data td = {G, visited, post_order_stack, in_stack, 0, found_time, lowlink, 0, SCCs}; + for (int i = 0; i < n; ++i) { + if (not visited[i]) get_scc(i, td); + } + + write_solution(td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = true; + td.found_time[current_node] = td.current_time++; + td.lowlink[current_node] = td.found_time[current_node]; + td.post_order_stack.push(current_node); + td.in_stack[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) { + get_scc(neighbour, td); + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } else if (td.in_stack[neighbour]) { + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.found_time[neighbour] + ); + } + } + + if (td.found_time[current_node] == td.lowlink[current_node]) { + int top_node; + do { + top_node = td.post_order_stack.top(); + td.post_order_stack.pop(); + + td.SCCs[td.SCC_index].push_back(top_node); + td.in_stack[top_node] = false; + } while (top_node != current_node); + td.SCC_index++; + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/tarjan/c021ef44-cb96-4c9f-9fcd-330ec45f5c99.cpp b/scc/tarjan/c021ef44-cb96-4c9f-9fcd-330ec45f5c99.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a042a6ba5c709d6d88d438d0212f5d085f3fd227 --- /dev/null +++ b/scc/tarjan/c021ef44-cb96-4c9f-9fcd-330ec45f5c99.cpp @@ -0,0 +1,116 @@ +#include +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +std::stack stack; + +std::vector cnx[100005]; + +int disc[100005], low[100005], _time, cnxCount; + +bool inStack[100005]; + + + +void DFS(int vertex = 1) { + + stack.push(vertex); + + inStack[vertex] = true; + + disc[vertex] = low[vertex] = ++_time; + + + + for (auto &it:ADC[vertex]) { + + if (disc[it]) { + + if (inStack[it]) low[vertex] = std::min(low[vertex], low[it]); + + } + + else { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (disc[vertex] == low[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + inStack[stack.top()] = false; + + cnx[cnxCount].push_back(stack.top()); + + stack.pop(); + + } + + inStack[stack.top()] = false; + + cnx[cnxCount].push_back(stack.top()); + + stack.pop(); + + ++ cnxCount; + + } + +} + + + +int main() + +{ + + input >> N >> M; + + for (int i=0, x, y; i> x >> y, addEdge(x, y); + + + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + + + output << cnxCount << '\n'; + + for (int i=0; i +#include +#include +#include + + +using namespace std; + + + +int N,M; + +stack Stack; + +vectorGraf[100010],Lowlink(100010,-1),Indx(100010,-1),ONstack(100010,0); + +vector> result; + + + +void Tarjan(int x) + +{ + + static int index = 0; + + Indx[x] = Lowlink[x] = index; + + index++; + + Stack.push(x); + + ONstack[x] = 1; + + + + for (int it : Graf[x]) + + { + + if (Indx[it] == -1) + + Tarjan(it); + + if (ONstack[it]) + + Lowlink[x] = min(Lowlink[x], Lowlink[it]); + + } + + if (Indx[x] == Lowlink[x]) + + { + + vector aux; + + + + while (Stack.top() != x) + + { + + aux.push_back(Stack.top()); + + ONstack[Stack.top()] = 0; + + Stack.pop(); + + } + + + + aux.push_back(Stack.top()); + + ONstack[Stack.top()] = 0; + + Stack.pop(); + + + + result.push_back(aux); + + } + +} + +int main() + +{ + + ifstream in ("ctc.in"); + + ofstream out("ctc.out"); + + + + int x,y; + + + + in>>N>>M; + + for( int i = 0; i < M; ++i) + + { + + in>>x>>y; + + Graf[x].push_back(y); + + + + } + + + + for(int i = 1; i <=N; ++i) + + if (Indx[i] == -1) + + Tarjan(i); + + + + + + out << result.size() << "\n"; + + + + for (auto it : result) + + { for (int x : it) + + out<< x << " "; + + out << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/c1299da6-fcb6-4e17-a148-159e8d233e9a.cpp b/scc/tarjan/c1299da6-fcb6-4e17-a148-159e8d233e9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7fc18c39989458ea05d000481fb9b271e286fc2f --- /dev/null +++ b/scc/tarjan/c1299da6-fcb6-4e17-a148-159e8d233e9a.cpp @@ -0,0 +1,130 @@ +#include +using namespace std; + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + +vector adj[100005]; + +int n,m,low_link[100005],idx[100005],cnt=0,ctc=0; + +bool viz[100005],instack[100005]; + +vector sol[100005]; + +stack s; + +void dfs(int node) + +{ + + s.push(node); + + instack[node]=true; + + idx[node]=low_link[node]=++cnt; + + for(auto x:adj[node]) + + { + + if(idx[x]==0) {dfs(x);} + + if(instack[x]==1) + + { + + low_link[node]=min(low_link[node],low_link[x]); + + } + + } + + if(idx[node]==low_link[node]) + + { + + ctc++; + + int x=node; + + do + + { + + x=s.top(); + + low_link[x]=idx[node]; + + sol[ctc].push_back(x); + + instack[x]=0; + + s.pop(); + + } + + while(x!=node); + + } + + + + + + + +} + +int main() + +{ + + f>>n>>m; + + for(int i=1; i<=m; i++) + + { + + int x,y; + + f>>x>>y; + + adj[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + { + + if(idx[i]==0) + + { + + dfs(i); + + } + + } + + g< +int N, M; + +std::vector ADC[100005]; + +inline void addEdge(int u, int v) { + + ADC[u].push_back(v); + +} + + + +int nctc; + +int low[100005], disc[100005], _time; + +std::vector ctc[100005]; + +bool inStack[100005]; + +std::stack stack; + +void DFS(int vertex) { + + disc[vertex] = low[vertex] = ++_time; + + inStack[vertex] = true; + + stack.push(vertex); + + + + for (auto &it:ADC[vertex]) { + + if (!disc[it]) { + + DFS(it); + + low[vertex] = std::min(low[vertex], low[it]); + + } + + else if (inStack[it]) { + + low[vertex] = std::min(low[vertex], low[it]); + + } + + } + + + + if (low[vertex] == disc[vertex]) { + + while (!stack.empty() && stack.top() != vertex) { + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + } + + ctc[nctc].push_back(stack.top()); + + inStack[stack.top()] = false; + + stack.pop(); + + ++ nctc; + + } + +} + + + +std::ifstream input (std::string("ctc")+".in"); + +std::ofstream output(std::string("ctc")+".out"); + + + +int main() + +{ + + input >> N >> M; + + for (int i=1, u, v; i<=M; ++i) input >> u >> v, addEdge(u, v); + + for (int i=1; i<=N; ++i) + + if (!disc[i]) DFS(i); + + output << nctc << '\n'; + + for (int i=0; i +#include +#include + + +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out ("ctc.out"); + + + +const int LIM = 1e5 + 7; + + + +void tarjan ( int nod ); + + + +vector < int > g[LIM]; + + + +vector < int > ctc[LIM]; + + + +stack < int > s; + + + +int n, m; + + + +int x, y; + + + +int viz[LIM]; + + + +int iss[LIM]; + + + +int niv[LIM]; + + + +int l[LIM]; + + + +int ct; + + + +int sorin; + + + +int main() + +{ + + in >> n >> m; + + for ( int i = 1 ; i <= m ; ++i ) + + { + + in >> x >> y; + + g[x].push_back (y); + + } + + for ( int i = 1 ; i <= n ; ++i ) + + if ( !viz[i] ) + + tarjan (i); + + out << sorin << '\n'; + + for ( int i = 1 ; i <= sorin ; ++i ) + + { + + for ( auto y : ctc[i] ) + + out << y << " "; + + out << '\n'; + + } + + return 0; + +} + + + +void tarjan ( int nod ) + +{ + + iss[nod] = true; + + viz[nod] = true; + + s.push(nod); + + niv[nod] = l[nod] = ++ct; + + for ( auto y : g[nod] ) + + { + + if ( !viz[y] ) + + { + + tarjan (y); + + l[nod] = min ( l[nod], l[y] ); + + } + + else + + { + + if ( iss[y] ) + + l[nod] = min ( l[nod], niv[y] ); + + } + + } + + if ( niv[nod] == l[nod] ) + + { + + ++sorin; + + while ( !s.empty () ) + + { + + ctc[sorin].push_back ( s.top () ); + + iss[s.top ()] = false; + + if ( s.top () == nod ) + + { + + s.pop (); + + break; + + } + + s.pop (); + + } + + } + +} diff --git a/scc/tarjan/c3d531a3-c928-45d5-af8b-4e1149dc7960.cpp b/scc/tarjan/c3d531a3-c928-45d5-af8b-4e1149dc7960.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b156cc4fdea0d80727ad10475117b8e8d776bc7 --- /dev/null +++ b/scc/tarjan/c3d531a3-c928-45d5-af8b-4e1149dc7960.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + +vector g[1 + NMAX]; + +vector strongA[1 + NMAX];int strAsiz; + +int visited[1 + NMAX]; + +int minReach[1 + NMAX]; + +bool onStack[1 + NMAX]; + +stack st; + + + +int n, m, step; + + + +void strong(int nod) { + + + + step++; + + visited[nod] = step; + + minReach[nod] = step; + + st.push(nod); + + onStack[nod] = true; + + + + for(int i = 0;i < g[nod].size();i++){ + + + + + + if(visited[g[nod][i]] == 0){ + + + + strong(g[nod][i]); + + minReach[nod] = min(minReach[nod], minReach[g[nod][i]]); + + }else if(onStack[g[nod][i]] == true){ + + + + minReach[nod] = min(minReach[nod], visited[g[nod][i]]); + + } + + } + + if(minReach[nod] == visited[nod]){ + + strAsiz++; + + int popped; + + do{ + + + + popped = st.top(); + + + + st.pop(); + + + + onStack[popped] = false; + + strongA[strAsiz].push_back(popped); + + } while(popped != nod); + + } + + + +} + + + +int main(){ + + + + int from, to; + + in >> n >> m; + + + + for(int i = 1;i <= m;i++){ + + + + in >> from >> to; + + g[from].push_back(to); + + } + + + + for(int i = 1;i <= n;i++){ + + + + if(visited[i] == 0) { + + strong(i); + + } + + } + + + + out << strAsiz << '\n'; + + for(int i = 1;i <= strAsiz;i++){ + + + + for(int j = 0;j < strongA[i].size() ;j++){ + + out << strongA[i][j] << ' '; + + } + + out << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/c5456b9c-d16f-44b7-a71f-5980a80a832d.cpp b/scc/tarjan/c5456b9c-d16f-44b7-a71f-5980a80a832d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f6ff0554062a34a128ff397a7fc8ee2adc43975 --- /dev/null +++ b/scc/tarjan/c5456b9c-d16f-44b7-a71f-5980a80a832d.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector>G,CC; + +vectorniv,L,C; + +vectorinS; + +stackS; + +int n,m,k,nv; + +void Citire() + +{ + + fin>>n>>m; + + G.resize(n+1); + + L.resize(n+1); + + inS.resize(n+1); + + niv.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + fin>>x>>y; + + G[x].push_back(y); + + } + +} + +void Tarjan(int x) + +{ + + niv[x] = L[x] = ++nv; + + S.push(x); + + inS[x] = true; + + for(auto y : G[x]) + + { + + if(niv[y]==0) + + { + + Tarjan(y); + + L[x]=min(L[x],L[y]); + + } + + else + + { + + if(inS[y]==true) + + L[x]=min(L[x],niv[y]); + + } + + } + + if(niv[x]==L[x]) + + { + + C.clear(); + + int y=-1; + + while(x!=y) + + { + + y=S.top(); + + S.pop(); + + C.push_back(y); + + inS[y]=false; + + } + + CC.push_back(C); + + } + +} + +void Afisare() + +{ + + fout< +#include +#include + +const int NMAX = 1e5 + 1; + +std :: ifstream in ("ctc.in"); +std :: ofstream out("ctc.out"); + +std :: vector graf[NMAX]; +std :: vector> sol; +std :: stack stk; + +int low[NMAX], id[NMAX]; +bool inStack[NMAX]; + +int n, m, a, b; +int idc = 0; + +void DFS (int); + +int main() +{ + + in >> n >> m; + for( int i = 0; i < m; i++ ){ + in>>a>>b; + graf[a-1].emplace_back(b-1); + } + + for(int i = 0; i < n; i++ ) + { + low[i] = 0; + id[i] = -1; + inStack[i] = 0; + } + + for(int i = 0; i < n; i++) + if ( id[i] == -1) + DFS(i); + + + out << sol.size() << "\n"; + for (auto list : sol){ + for (int elem : list) + out << elem + 1 << " "; + out << "\n"; + } + return 0; +} + +void DFS(int x) +{ + id[x] = low[x] = idc; + idc += 1; + stk.push(x); + inStack[x] = 1; + + for (int elem : graf[x]){ + if(id[elem] == -1) + DFS(elem); + if(inStack[elem]) + low[x] = std :: min (low[x], low[elem]); + } + if(id[x] == low[x]){ + std :: vector aux; + + while (stk.top() != x){ + aux.push_back( stk.top() ); + inStack[ stk.top() ] = 0; + stk.pop(); + } + + aux.push_back( stk.top() ); + inStack[ stk.top() ] = 0; + stk.pop(); + + sol.push_back(aux); + } + +} diff --git a/scc/tarjan/c9c2697d-c606-4034-a164-db3c2bebf4be.cpp b/scc/tarjan/c9c2697d-c606-4034-a164-db3c2bebf4be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f498eba1c03d0e95cf53e680458065b73e183b6 --- /dev/null +++ b/scc/tarjan/c9c2697d-c606-4034-a164-db3c2bebf4be.cpp @@ -0,0 +1,174 @@ +#include + + +using namespace std; + + + +ifstream f ("ctc.in"); + +ofstream g("ctc.out"); + + + +int N,M; + +const int NMAX = 100005; + + + + + + + +vector adj[NMAX], con, idx, lowlink, in_stack; + + + +vector < vector > Solution; + + + +stack S; + + + +int indecs; + + + +void read(vector * adj ) + +{ + + int x, y; + + f>>N>>M; + + for(int i=0;i> x >> y, + + adj[x - 1].push_back(y - 1); + + + +} + + + +void print(const vector < vector >& G) + +{ + + g << G.size() << "\n"; + + for (int i = 0; i < G.size(); ++ i) + + { + + for (int j = 0; j < G[i].size(); ++ j) + + g << G[i][j] + 1 << " "; + + g << "\n"; + + } + + + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n); + + in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) + + { + + if (idx[*it] == -1) + + { + + tarjan(*it, adj); + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(); + + in_stack[node] = 0; + + } + + while (node != n); + + Solution.push_back(con); + + } + +} + + + +int main(void) + +{ + + read(adj); + + idx.resize(N), lowlink.resize(N), in_stack.resize(N); + + idx.assign(N, -1), in_stack.assign(N, 0); + + + + for (int i = 0; i < N; ++ i) + + if (idx[i] == -1) + + tarjan(i, adj); + + + + print(Solution); + + + + return 0; + +} diff --git a/scc/tarjan/cbe1ae84-25a4-4a6a-82e3-8a640f565107.cpp b/scc/tarjan/cbe1ae84-25a4-4a6a-82e3-8a640f565107.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d8619b9b746823f14894fc25a3fc4ee5a891036 --- /dev/null +++ b/scc/tarjan/cbe1ae84-25a4-4a6a-82e3-8a640f565107.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + + + + + +int index, scc; + +vector g[100005], ids, lowlink, temp; + +vector> sol; + +stack s; + +vector inStack; + +int n, m; + + + + + +void read() + +{ + + for (in >> n >> m; m--;) + + { + + int x, y; + + in >> x >> y; + + g[x].push_back(y); + + } + + ids.resize(n + 5), ids.assign(n + 5, -1); + + inStack.resize(n + 5), inStack.assign(n + 5, 0); + + lowlink.resize(n + 5); + +} + + + + + +void tarjan(int node) + +{ + + ids[node] = lowlink[node] = index++; + + s.push(node); + + inStack[node] = true; + + + + for (int it = 0; it < g[node].size(); it++) + + { + + int adjNode = g[node][it]; + + if (ids[adjNode] == -1) + + { + + tarjan(adjNode); + + lowlink[node] = min(lowlink[node], lowlink[adjNode]); + + } + + else if (inStack[adjNode]) + + lowlink[node] = min(lowlink[node], lowlink[adjNode]); + + } + + + + if (ids[node] == lowlink[node]) + + { + + temp.clear(); + + int nd; + + do + + { + + nd = s.top(); + + temp.push_back(nd); + + s.pop(); + + inStack[nd] = false; + + + + } while (nd != node); + + sol.push_back(temp); + + } + +} + + + +void print() + +{ + + out << sol.size() << '\n'; + + for (vector it : sol) + + { + + copy(it.begin(), it.end(), ostream_iterator(out, " ")); + + out << '\n'; + + } + +} + + + +void solve() + +{ + + read(); + + for (int i = 1; i <= n; i++) + + if (ids[i] == -1) + + tarjan(i); + + + + print(); + +} + + + +int main() + +{ + + solve(); + +} diff --git a/scc/tarjan/cbf0f46b-b47f-4af8-a535-becd8b2e9dc5.cpp b/scc/tarjan/cbf0f46b-b47f-4af8-a535-becd8b2e9dc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b44e1105253920ab05a1ce0a3a762b8859cb82d --- /dev/null +++ b/scc/tarjan/cbf0f46b-b47f-4af8-a535-becd8b2e9dc5.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include + + +using namespace std; +const int N_MAX = 1e5 + 1; + + + +struct TarjanVertex{ + + int index, tarjanIndex = -1, lowlink; + + bool onStack = false; + +}; + + + +vector graph[N_MAX]; + +stack stk; + +vector graphVertexes; + +vector > solutions; + + + +int nextAvailableIndex = 1; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void Tarjan(int nodeIndex) + +{ + + graphVertexes[nodeIndex].tarjanIndex = nextAvailableIndex; + + graphVertexes[nodeIndex].lowlink = nextAvailableIndex; + + nextAvailableIndex++; + + stk.push(nodeIndex); + + graphVertexes[nodeIndex].onStack = true; + + + + for(auto neighborIndex: graph[graphVertexes[nodeIndex].index]) + + { + + if(graphVertexes[neighborIndex].tarjanIndex == -1) + + { + + Tarjan(graphVertexes[neighborIndex].index); + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].lowlink, graphVertexes[nodeIndex].lowlink); + + } + + else if (graphVertexes[neighborIndex].onStack == true) + + { + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].tarjanIndex, graphVertexes[nodeIndex].lowlink); + + } + + } + + + + if(graphVertexes[nodeIndex].tarjanIndex == graphVertexes[nodeIndex].lowlink) + + { + + vector sol; + + int neighborIndex; + + do + + { + + neighborIndex = stk.top(); + + sol.push_back(neighborIndex); + + stk.pop(); + + graphVertexes[neighborIndex].onStack = false; + + } while (neighborIndex != nodeIndex); + + + + solutions.push_back(sol); + + } + +} + + + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + + + graphVertexes.resize(n + 1); + + for(int i = 1; i <= n; i++) + + { + + graphVertexes[i].index = i; + + } + + + + for(int i = 0, st, dr; i < m; i++) + + { + + fin >> st >> dr; + + graph[st].push_back(dr); + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(graphVertexes[i].tarjanIndex != -1) + + continue; + + + + Tarjan(i); + + } + + + + + + fout << solutions.size() << '\n'; + + + + for(auto sol: solutions) + + { + + for(auto val: sol) + + { + + fout << val << ' '; + + } + + + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/cc671199-73f1-411c-9f25-32e5ead72ada.cpp b/scc/tarjan/cc671199-73f1-411c-9f25-32e5ead72ada.cpp new file mode 100644 index 0000000000000000000000000000000000000000..598d09499000689e256014e6f73a08ff6adc6e82 --- /dev/null +++ b/scc/tarjan/cc671199-73f1-411c-9f25-32e5ead72ada.cpp @@ -0,0 +1,198 @@ +#include +#include +#include +#include +using namespace std; + + + +int n,m,v[100000],nrdf[100000],inst[100000]; + +vector l[100000]; + +int c,ndf,pozc=0;; + + + +struct Muchie{ + + int i,j; + +} ; + + + +stack s; + + + +vector comp; + + + +void bloc(int x,int tata){ + + int u; + + s.push(x); + + ndf++; + + nrdf[x]=ndf; + + v[x]=ndf; + + inst[x]=1; + + for(int t=0;tv[j]) + + v[x]=v[j]; + + } + + else + + if(inst[j]){ + + if(v[x]>nrdf[j]) + + v[x]=nrdf[j]; + + } + + } + + + + if (v[x] == nrdf[x]) { + + do { + + u = s.top(); + + comp.push_back(u); + + s.pop(); + + inst[u]=0; + + + + } + + while (u != x); + + + + c++; + + comp.push_back(-1); + + } + + + + } + +void bloc(){ + + int i; + + c=0; + + for(i=0;i=0) + + fprintf(f,"%d ",comp[j]+1); + + else + + fprintf(f,"\n"); + + } + + + + + + fclose(f); + return 0; + +} diff --git a/scc/tarjan/ccd36e16-7b4a-435b-8287-fc4746c263e9.cpp b/scc/tarjan/ccd36e16-7b4a-435b-8287-fc4746c263e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ca9f169bc7e450dc6acfa76c8916ec4a9765960 --- /dev/null +++ b/scc/tarjan/ccd36e16-7b4a-435b-8287-fc4746c263e9.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,i,a,b,low[100010],niv[100010],l,nr,d[100010]; + +vector v[100010],s,sol[100010]; + +void dfs(int nod) + +{ + + low[nod]=niv[nod]=++l; + + s.push_back(nod);d[nod]=1; + + for(auto it:v[nod]) + + { + + if(niv[it]&&d[it]) low[nod]=min(low[nod],niv[it]); + + if(niv[it]==0) + + { + + dfs(it); + + low[nod]=min(low[it],low[nod]); + + } + + } + + if(low[nod]==niv[nod]) + + { + + nr++;while(s.back()!=nod) sol[nr].push_back(s.back()),d[s.back()]=0,s.pop_back(); + + sol[nr].push_back(s.back()),d[s.back()]=0,s.pop_back(); + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(i=1;i<=m;i++) + + { + + fin>>a>>b; + + v[a].push_back(b); + + } + + for(i=1;i<=n;i++) if(niv[i]==0) dfs(i); + + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int index = 1; + +int n, m, nrc; + +int isInStack[100005], viz[100005], lowlink[100005], idx[100005]; + +vector graph[100005]; + +vector CC[100005]; + +stack S; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + + + +void addCC() + +{ + + nrc++; + + int top; + + do{ + + top = S.top(); + + CC[nrc].push_back(top); + + S.pop(); + + isInStack[top] = 0; + + }while(lowlink[top] != idx[top]); + + + +} +void solve(int nod) + +{ + + S.push(nod); + + isInStack[nod] = 1; + + viz[nod] = 1; + + idx[nod] = index; + + lowlink[nod] = index; + + index ++; + + + + for(auto &v:graph[nod]) + + { + + if(viz[v] == 0) + + { + + solve(v); + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + else{ + + if(isInStack[v] == 1) + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + } + + + + if(lowlink[nod] == idx[nod]) + + { + + addCC(); + + } + + + +} +void tarjan() + +{ + + + + + + for(int i = 1; i <= n; ++i) + + if(viz[i] == 0) + + solve(i); + +} + + + + + +void write() + +{ + + g< +using namespace std; + + + +const int maxN=(1e5)+5; + + + +int niv,lvl[maxN],low[maxN],st[maxN],vf,ctc; + + + +vector v[maxN],C[maxN]; + +bool inStack[maxN]; + +void dfs(int nod) + +{ + + niv++; + + low[nod]=niv; + + lvl[nod]=niv; + + st[++vf]=nod; + + inStack[nod]=1; + + + + for(auto it:v[nod]) + + { + + if(!lvl[it]) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else + + if(inStack[it]) + + { + + low[nod]=min(low[nod],low[it]); + + } + + } + + + + if(low[nod]==lvl[nod]) + + { + + ctc++; + + int x=-1; + + while(x!=nod) + + { + + x=st[vf]; + + inStack[x]=0; + + vf--; + + C[ctc].push_back(x); + + } + + } + +} + +int n,m,x,y; + +int main() + +{ + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d",&x,&y); + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;i++) + + if(!lvl[i]) dfs(i); + + + + printf("%d\n",ctc); + + + + for(int i=1;i<=ctc;i++) + + { + + for(auto it:C[i]) + + printf("%d ",it); + + + + printf("\n"); + + } + + return 0; + +} diff --git a/scc/tarjan/d0dfef5f-4e27-4534-96a3-8b1fcf067cb4.cpp b/scc/tarjan/d0dfef5f-4e27-4534-96a3-8b1fcf067cb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41f9a0eb98bd221aae3ba37e4fb0c3c60c317f06 --- /dev/null +++ b/scc/tarjan/d0dfef5f-4e27-4534-96a3-8b1fcf067cb4.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +int n, m, x, y, compAct, mom; + +vector < vector > muchii, componente; + +vector disc, low, componentaTata; + +stack stk; + +vector realVal; + +void Tarjan(int nod) + +{ + + low[nod]=disc[nod] = ++mom; + + stk.push(nod); + + for(auto & x : muchii[nod]) + + { + + if(disc[x]==-1) + + { + + Tarjan(x); + + low[nod]=min(low[nod], low[x]); + + } + + else + + { + + low[nod]=min(low[nod], disc[x]); + + } + + } + + + + if(low[nod]==disc[nod]) + + { + + componente.push_back( vector()); + + while(!stk.empty()&&stk.top()!=nod) + + { + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + +} + + + +int main() + +{ + + in>>n>>m; + + muchii.resize(n+1); componentaTata.resize(n+1, -1); + + disc.resize(n+1, -1); low.resize(n+1, -1); realVal.resize(n+1, 0); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(disc[i]==-1) + + mom=0, Tarjan(i); + + + + out< + + +using namespace std; + + + +vector> graph(100000 + 1, vector()); + +int N, M; + +stack node_stack; + +bool viz[100000 + 1]; + +bool onStack[100000 + 1]; + + + +int id[100000 + 1], low[100000 + 1], counter = 0; + + + +vector> ctcs; + + + +void dfs(int node) + +{ + + id[node] = low[node] = ++counter; + + viz[node] = onStack[node] = true; + + + + node_stack.push(node); + + + + for(auto& next : graph[node]) + + { + + if(viz[next] == false) + + dfs(next); + + + + + + if(onStack[next] == true) + + low[node] = min(low[node], low[next]); + + } + + + + if(low[node] == id[node]) + + { + + vector ctc; + + int x; + + do { + + x = node_stack.top(); + + node_stack.pop(); + + onStack[x] = false; + + ctc.push_back(x); + + + + }while(x != node); + + + + + + ctcs.push_back(ctc); + + } + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + fin >> N >> M; + + + + for(int i = 1; i <= M; ++i){ + + int x, y; + + fin >> x >> y; + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= N; ++i) + + { + + if(viz[i] == false) + + dfs(i); + + } + + + + fout << ctcs.size() << '\n'; + + + + for(auto& ctc : ctcs){ + + for(auto& node : ctc) + + fout << node << ' '; + + + + fout << '\n'; + + } + +} diff --git a/scc/tarjan/d2812037-dd60-4229-89c8-47b61767a46e.cpp b/scc/tarjan/d2812037-dd60-4229-89c8-47b61767a46e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..624188337104e704269a2abf23038ca971b9deb4 --- /dev/null +++ b/scc/tarjan/d2812037-dd60-4229-89c8-47b61767a46e.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + +int n,m,x,y,st[100100],idx[100100],low[100100],top,c,ap[100100],t; + +vector v[100100]; + +vector comp[100100]; + +void tarjan(int nod) + +{ + + idx[nod]=t; + + low[nod]=t; + + t++; + + st[++top]=nod; + + ap[nod]=1; + + for(auto it:v[nod]) + + { + + if(idx[it]==-1) { + + tarjan(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(ap[it]) low[nod]=min(low[nod],idx[it]); + + } + + if(low[nod]==idx[nod]) + + { + + c++; + + do + + { + + x=st[top--]; + + ap[x]=0; + + comp[c].push_back(x); + + }while(x!=nod); + + } + +} + +int main() + +{ + + in>>n>>m; + + for(int i=1;i<=m;i++) + + { + + in>>x>>y; + + v[x].push_back(y); + + } + + for(int i=1;i<=n;i++) idx[i]=-1; + + for(int i=1;i<=n;i++) + + { + + if(idx[i]==-1) + + { + + t=0; + + top=0; + + tarjan(i); + + } + + } + + out< +#include +#include +#include +using namespace std; + +int n,id=0,ids[100010],low[100010],ctc=0; + +ofstream o("ctc.out"); + +ifstream f("ctc.in"); + +vector v[100010],cc[100010]; + +stack S; + +bool onStack[100010]; + +void dfs(int k){ + + ids[k]=low[k]=++id; int i,nv; + + S.push(k); onStack[k]=1; + + for(nv=0;nv>n>>m; + + for(i=1;i<=m;i++){ + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++) + + if(!ids[i]) dfs(i); + + + + o< +#include +#include +#include +#include + +using namespace std; + +struct nod { + int ind; + int lowlink; + bool inStiva; + + nod(int i, int l) { + this->ind = i; + this->lowlink = l; + this->inStiva = false; + } +}; + + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + + +vector noduri; +vector vecin[100000]; + +int n, m, index; +stack S; +vector> output; + +void citire() { + f >> n >> m; + for (int i = 0; i < n; ++i) { + noduri.emplace_back(INT_MIN, INT_MAX); + } + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + x--, y--; + vecin[x].push_back(y); + } +} + +void afisare() { + g << output.size() << '\n'; + for (auto it : output) { + for (auto it2 : it) { + g << it2 << ' '; + } + g << "\n"; + } +} + +void tarjaaaaan(int z) { + noduri[z].ind = index; + noduri[z].lowlink = index; + index++; + S.push(z); + noduri[z].inStiva = true; + for (auto it : vecin[z]) { + if (noduri[it].ind == INT_MIN) { + tarjaaaaan(it); + noduri[z].lowlink = min(noduri[z].lowlink, noduri[it].lowlink); + } else if (noduri[it].inStiva) { + noduri[z].lowlink = min(noduri[z].lowlink, noduri[it].ind); + } + } + if (noduri[z].lowlink == noduri[z].ind) { + vector comp; + int w; + do { + w = S.top(); + S.pop(); + noduri[w].inStiva = false; + comp.push_back(w + 1); + } while (w != z); + output.push_back(comp); + } +} + +int main() { + citire(); + + for (int i = 0; i < n; ++i) { + if (noduri[i].ind == INT_MIN) { + tarjaaaaan(i); + } + } + + afisare(); + return 0; +} diff --git a/scc/tarjan/d3f04d83-0f91-4b22-bb4d-bd958acbaa05.cpp b/scc/tarjan/d3f04d83-0f91-4b22-bb4d-bd958acbaa05.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b315e6faa5ae38a389d5b1f7c8fd6f1cae2cdb0d --- /dev/null +++ b/scc/tarjan/d3f04d83-0f91-4b22-bb4d-bd958acbaa05.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + +const int NMAX = 100005; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int lowlink[NMAX]; + +int ids[NMAX]; + +bool inStack[NMAX]; + +int groupCounter = 1; + + + + + +int n, m; + +vector graf[NMAX]; + +stack stac; + +vector> components; + +void ctc(int node) + +{ + + lowlink[node] = groupCounter++; + + ids[node] = lowlink[node]; + + inStack[node] = true; + + stac.push(node); + + for(auto i:graf[node]) + + { + + if(ids[i]==0) ctc(i); + + if(inStack[i]) lowlink[node]=min(lowlink[node], lowlink[i]); + + } + + if(lowlink[node]==ids[node]) + + { + + vector component; + + while(stac.top()!=node) + + { + + component.push_back(stac.top()); + + lowlink[stac.top()]=lowlink[node]; + + inStack[stac.top()]=false; + + stac.pop(); + + } + + component.push_back(node); + + inStack[node]=false; + + stac.pop(); + + components.push_back(component); + + } + +} + +int main() + +{ + + fin>>n>>m; + + while(m--) + + { + + int a, b; + + fin>>a>>b; + + graf[a].push_back(b); + + } + + for(int i = 1;i<=n;i++) + + { + + if(ids[i]==0) + + ctc(i); + + } + + fout< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int DIM = 1e5 + 5; + + + +int low[DIM], niv[DIM], cod, N, M, K; + +bool f[DIM]; + +stack st; + +vector edge[DIM], ctc[DIM]; + + + +void dfs( int nod ){ + + niv[nod] = low[nod] = ++cod; + + st.push( nod ); + + f[nod] = true; + + for( int i = 0; i < edge[nod].size(); i++ ){ + + int vec = edge[nod][i]; + + if( niv[vec] == 0 ){ + + dfs( vec ); + + low[nod] = min( low[nod], low[vec] ); + + }else{ + + if( f[vec] == true ) + + low[nod] = min( low[nod], low[vec] ); + + } + + } + + if( low[nod] == niv[nod] ){ + + K++; + + while( !st.empty() && st.top() != nod ){ + + ctc[K].push_back( st.top() ); + + f[st.top()] = false; + + st.pop(); + + } + + ctc[K].push_back( st.top() ); + + f[st.top()] = false; + + st.pop(); + + } + +} + + + +int main(){ + + fin >> N >> M; + + for( int i = 1; i <= M; i++ ){ + + int x, y; fin >> x >> y; + + edge[x].push_back( y ); + + } + + + + for( int i = 1; i <= N; i++ ){ + + if( niv[i] == 0 ){ + + dfs( i ); + + } + + } + + + + fout << K << "\n"; + + for( int i = 1; i <= K; i++, fout << "\n" ) + + for( int j = 0; j < ctc[i].size(); j++, fout << " " ) + + fout << ctc[i][j]; + + + + return 0; + +} diff --git a/scc/tarjan/d4c4f9c1-b962-4fd2-8846-8de9b2cc9298.cpp b/scc/tarjan/d4c4f9c1-b962-4fd2-8846-8de9b2cc9298.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb17a70eee38d3fbcbf261a9aaaa7fcaf8604d16 --- /dev/null +++ b/scc/tarjan/d4c4f9c1-b962-4fd2-8846-8de9b2cc9298.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int t,indice,inStiva[100100],viz[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + viz[nod]=indice; + + low[nod]=min(low[nod],indice); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++) + + if(viz[i]==0)Tarjan(i); + + + + g< +#include +#include + + +using namespace std; + + + +ifstream fi ("ctc.in"); + +ofstream fo ("ctc.out"); + +bool ins[100001]; + +int n, m, idx[100001], midx[100001], nodes, total, a, b; + +stack st; + +vector v[100001], ctc[100001]; + + + +void discover(int node) { + + idx[node] = midx[node] = ++nodes; + + ins[node] = true; + + st.push(node); + + for (auto i:v[node]) { + + if (!idx[i]) { + + discover(i); + + midx[node] = min(midx[node], midx[i]); + + } else if (ins[i]) { + + midx[node] = min(midx[node], idx[i]); + + } + + } + + if (idx[node] == midx[node]) { + + while (1) { + + a = st.top(); + + st.pop(); + + ctc[total].push_back(a); + + ins[a] = false; + + if (a == node) + + break; + + } + + ++total; + + } + +} + + + +int main() { + + fi >> n >> m; + + for (int i = 1; i <= m; ++i) { + + fi >> a >> b; + + v[a].push_back(b); + + } + + for (int i = 1; i <= n; ++i) + + if (!idx[i]) + + discover(i); + + fo << total << '\n'; + + for (int i = 0; i < total; ++i) + + for (auto j:ctc[i]) + + fo << j << " \n"[j==ctc[i].back()]; + + return 0; + +} diff --git a/scc/tarjan/d628528e-39eb-4f6b-87f7-dfae77951755.cpp b/scc/tarjan/d628528e-39eb-4f6b-87f7-dfae77951755.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69f5b1c9709d22bc5451eab40fb1da208589743a --- /dev/null +++ b/scc/tarjan/d628528e-39eb-4f6b-87f7-dfae77951755.cpp @@ -0,0 +1,150 @@ +#include + + +using namespace std; + + + +vector> graph(100000 + 1, vector()); + +int N, M; + +stack node_stack; + +bool viz[100000 + 1]; + +bool onStack[100000 + 1]; + + + +int id[100000 + 1], low[100000 + 1], counter = 0; + + + +vector> ctcs; + + + +void dfs(int node) + +{ + + id[node] = low[node] = ++counter; + + viz[node] = onStack[node] = true; + + + + node_stack.push(node); + + + + for(auto& next : graph[node]) + + { + + if(viz[next] == false) + + dfs(next); + + + + + + if(onStack[next] == true) + + low[node] = min(low[node], low[next]); + + } + + + + if(low[node] == id[node]) + + { + + vector ctc; + + int x; + + do { + + x = node_stack.top(); + + node_stack.pop(); + + onStack[x] = false; + + ctc.push_back(x); + + + + }while(x != node); + + + + + + ctcs.push_back(ctc); + + } + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + fin >> N >> M; + + + + for(int i = 1; i <= M; ++i){ + + int x, y; + + fin >> x >> y; + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= N; ++i) + + { + + if(viz[i] == false) + + dfs(i); + + } + + + + fout << ctcs.size() << '\n'; + + + + for(auto& ctc : ctcs){ + + for(auto& node : ctc) + + fout << node << ' '; + + + + fout << '\n'; + + } + +} diff --git a/scc/tarjan/d70b64e8-85f7-4c8e-a365-3c76d027f603.cpp b/scc/tarjan/d70b64e8-85f7-4c8e-a365-3c76d027f603.cpp new file mode 100644 index 0000000000000000000000000000000000000000..288fe4d9ce0287ccfa685c3ee4037272b269153d --- /dev/null +++ b/scc/tarjan/d70b64e8-85f7-4c8e-a365-3c76d027f603.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, l, sol; + +pair p[100005]; + +vector graph[100005], out[100005]; + +stack stk; + +bitset<100005> check, inS; + + + +void dfs(int now); + + + +int main() + +{ + + + + fin >> n >> m; + + while(m--){ + + int x, y; + + fin >> x >> y; + + + + graph[x].push_back(y); + + } + + + + for(int i = 1; i <= n; ++i){ + + if(!check[i]) dfs(i); + + } + + + + fout << sol << "\n"; + + for(int i = 1; i <= sol; ++i){ + + for(auto it:out[i]) fout << it << " "; + + fout << "\n"; + + } + + + + return 0; + +} + + + +void dfs(int now){ + + ++l; + + p[now] = {l, l}; + + check[now] = inS[now] = true; + + stk.push(now); + + + + for(auto next:graph[now]){ + + if(!check[next]){ + + dfs(next); + + p[now].second = min(p[now].second, p[next].second); + + } + + else if(inS[next]) p[now].second = min(p[now].second, p[next].second); + + } + + + + if(p[now].first == p[now].second){ + + ++sol; + + + + while(stk.top() != now){ + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + + out[sol].push_back(stk.top()); + + inS[stk.top()] = false; + + stk.pop(); + + } + + + +} diff --git a/scc/tarjan/d80f8473-3048-44c0-9f8f-5df752f803f3.cpp b/scc/tarjan/d80f8473-3048-44c0-9f8f-5df752f803f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33fcb8ae86dffe79198ea386d738425ba41fa7df --- /dev/null +++ b/scc/tarjan/d80f8473-3048-44c0-9f8f-5df752f803f3.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +using namespace std; + + + +ofstream out("ctc.out"); +ifstream in("ctc.in"); + +int n, m; + +vector graph[100001]; + +int isStack[100001]; + +stack s; + +bool visited[100001]; + +int tm[100001]; + +int boss[100001]; + +int t = 0; + +vector sol[100001]; + +int sz = 0; + +void dfsScc(int v) { + visited[v] = true; + + t++; + + boss[v] = t; + + tm[v] = t; + + isStack[v] = true; + + s.push(v); + + for (int i = 0; i < graph[v].size(); i++) { + if (!visited[graph[v][i]]) { + dfsScc(graph[v][i]); + } + + if (isStack[graph[v][i]] && boss[graph[v][i]] < boss[v]) { + boss[v] = boss[graph[v][i]]; + } + } + + if (tm[v] == boss[v]) { + while (s.top() != v) { + int curr = s.top(); + + s.pop(); + + isStack[curr] = false; + + sol[sz].push_back(curr + 1); + + + } + + if (s.top() == v) { + isStack[s.top()] = false; + + sol[sz].push_back(s.top() + 1); + + + + s.pop(); + } + + sz++; + + + } +} + +int main() { + in >> n >> m; + + for (int i = 0; i < m; i++) { + int u, v; + + in >> u >> v; + + u--; + + v--; + + graph[u].push_back(v); + } + + for (int i = 0; i < n; i++) { + if (!visited[i]) { + dfsScc(i); + } + } + + out << sz << '\n'; + + for (int i = 0; i < sz; i++) { + for (int j = 0; j < sol[i].size(); j++) { + out << sol[i][j] << " "; + } + + out << '\n'; + } + + return 0; +} diff --git a/scc/tarjan/d87436bf-76f1-4da9-8a90-3ba9d99f4946.cpp b/scc/tarjan/d87436bf-76f1-4da9-8a90-3ba9d99f4946.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23b1f7fd1b743f5f3cac5675bf22f597a90fe5e7 --- /dev/null +++ b/scc/tarjan/d87436bf-76f1-4da9-8a90-3ba9d99f4946.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int i,n,m,x,y,niv[100002],low[100002],f[100002],nivel,ctc; + +vector v[100002],sol[100002]; + +stack s; + +void dfs(int nod){ + + + + niv[nod]=low[nod]=++nivel; + + s.push(nod); + + f[nod]=1; + + for(auto vecin: v[nod]) + + if(!niv[vecin]){ + + dfs(vecin); + + low[nod]=min(low[vecin],low[nod]); + + }else if(f[vecin]) + + low[nod]=min(low[vecin],low[nod]); + + + + if(niv[nod]==low[nod]){ + + ctc++; + + int x=0; + + while(x!=nod){ + + x=s.top(); + + sol[ctc].push_back(x); + + f[x]=0; + + s.pop(); + + } + + } + +} + +int main(){ + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + v[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!niv[i]) + + dfs(i); + + fout< +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorgraph[100005]; + +vectorcomp[100005]; + +stack S; + + + +int viz[100005], is_in_stack[100005], idx[100005], low_link[100005]; + +int index, nrc, n, m, x, y; + + + +void citire(){ + + f>>n>>m; + + for(int i=0; i>x>>y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int vf){ + + + + low_link[vf] = idx[vf] = index; + + index++; + + + + viz[vf] = 1; + + is_in_stack[vf] = 1; + + S.push(vf); + + + + for(auto &v:graph[vf]){ + + if(viz[v] == 0){ + + tarjan(v); + + low_link[vf] = min(low_link[v], low_link[vf]); + + } + + else if(is_in_stack[v] == 1){ + + low_link[vf] = min(low_link[v], low_link[vf]); + + } + + } + + + + if(idx[vf] == low_link[vf]){ + + nrc++; + + int el; + + do{ + + el = S.top(); + + comp[nrc].push_back(el); + + S.pop(); + + is_in_stack[el] = 0; + + }while(el != vf); + + } + +} + +void afis(){ + + g< +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n,m,i,x,y,nr,viz[100005],low[100005],niv; + +bool s[100005]; + +vector g[100005],c[100005]; + +stack st; + + + +void dfs(int nod){ + + viz[nod]=low[nod]=++niv; + + s[nod]=1; + + st.push(nod); + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + g[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!viz[i]) + + dfs(i); + + fout< +#include +#include + + +using namespace std; + + + +ifstream fi ("ctc.in"); + +ofstream fo ("ctc.out"); + +bool ins[100001]; + +int n, m, idx[100001], midx[100001], nodes, total, a, b; + +stack st; + +vector v[100001], ctc[100001]; + + + +void discover(int node) { + + idx[node] = midx[node] = ++nodes; + + ins[node] = true; + + st.push(node); + + for (auto i:v[node]) { + + if (!idx[i]) { + + discover(i); + + midx[node] = min(midx[node], midx[i]); + + } else if (ins[i]) { + + midx[node] = min(midx[node], idx[i]); + + } + + } + + if (idx[node] == midx[node]) { + + while (1) { + + a = st.top(); + + st.pop(); + + ctc[total].push_back(a); + + ins[a] = false; + + if (a == node) + + break; + + } + + ++total; + + } + +} + + + +int main() { + + fi >> n >> m; + + for (int i = 1; i <= m; ++i) { + + fi >> a >> b; + + v[a].push_back(b); + + } + + for (int i = 1; i <= n; ++i) + + if (!idx[i]) + + discover(i); + + fo << total << '\n'; + + for (int i = 0; i < total; ++i) + + for (auto j:ctc[i]) + + fo << j << " \n"[j==ctc[i].back()]; + + return 0; + +} diff --git a/scc/tarjan/dc43ba7b-e48e-4eb5-a3da-a08af6ea50b2.cpp b/scc/tarjan/dc43ba7b-e48e-4eb5-a3da-a08af6ea50b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0afe9825ebc5fafcbe4e2a1df6a5b6a819ca7efe --- /dev/null +++ b/scc/tarjan/dc43ba7b-e48e-4eb5-a3da-a08af6ea50b2.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +using namespace std; + +struct nod { + int ind; + int lowlink; + bool inStiva; + + nod(int i, int l) { + this->ind = i; + this->lowlink = l; + this->inStiva = false; + } +}; + + +ifstream f("ctc.in"); +ofstream g("ctc.out"); + + +vector noduri; +vector vecin[100000]; + +int n, m, index; +stack S; +vector> output; + +void citire() { + f >> n >> m; + for (int i = 0; i < n; ++i) { + noduri.emplace_back(INT_MIN, INT_MAX); + } + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + x--, y--; + vecin[x].push_back(y); + } +} + +void afisare() { + g << output.size() << '\n'; + for (auto it : output) { + for (auto it2 : it) { + g << it2 << ' '; + } + g << "\n"; + } +} + +void tarjaaaaan(int z) { + noduri[z].ind = index; + noduri[z].lowlink = index; + index++; + S.push(z); + noduri[z].inStiva = true; + for (auto it : vecin[z]) { + if (noduri[it].ind == INT_MIN) { + tarjaaaaan(it); + noduri[z].lowlink = min(noduri[z].lowlink, noduri[it].lowlink); + } else if (noduri[it].inStiva) { + noduri[z].lowlink = min(noduri[z].lowlink, noduri[it].ind); + } + } + if (noduri[z].lowlink == noduri[z].ind) { + vector comp; + int w; + do { + w = S.top(); + S.pop(); + noduri[w].inStiva = false; + comp.push_back(w + 1); + } while (w != z); + output.push_back(comp); + } +} + +int main() { + citire(); + + for (int i = 0; i < n; ++i) { + if (noduri[i].ind == INT_MIN) { + tarjaaaaan(i); + } + } + + afisare(); + return 0; +} diff --git a/scc/tarjan/dc6487f2-b111-4f40-9ac3-61ed9d45bd30.cpp b/scc/tarjan/dc6487f2-b111-4f40-9ac3-61ed9d45bd30.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ec76df8e89247dc72650cc12f3883e6e50c6ede --- /dev/null +++ b/scc/tarjan/dc6487f2-b111-4f40-9ac3-61ed9d45bd30.cpp @@ -0,0 +1,111 @@ +#include + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + stack &post_order_stack; + vector &in_stack; + + int current_time; + vector &found_time; + vector &lowlink; + + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + vector visited(n, false); + stack post_order_stack; + vector in_stack(n, false); + SCCs.resize(n); + vector found_time(n, -1); + vector lowlink(n, -1); + + traversal_data td = {G, visited, post_order_stack, in_stack, 0, found_time, lowlink, 0, SCCs}; + for (int i = 0; i < n; ++i) { + if (not visited[i]) get_scc(i, td); + } + + write_solution(td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = true; + td.found_time[current_node] = td.current_time++; + td.lowlink[current_node] = td.found_time[current_node]; + td.post_order_stack.push(current_node); + td.in_stack[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) { + get_scc(neighbour, td); + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } else if (td.in_stack[neighbour]) { + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } + } + + if (td.found_time[current_node] == td.lowlink[current_node]) { + int top_node; + do { + top_node = td.post_order_stack.top(); + td.post_order_stack.pop(); + + td.SCCs[td.SCC_index].push_back(top_node); + td.in_stack[top_node] = false; + } while (top_node != current_node); + td.SCC_index++; + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/tarjan/dc7febff-78dd-4b75-b12b-785d031bfca8.cpp b/scc/tarjan/dc7febff-78dd-4b75-b12b-785d031bfca8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5069ec124c7e1548fc398ce3438f6351a67023c2 --- /dev/null +++ b/scc/tarjan/dc7febff-78dd-4b75-b12b-785d031bfca8.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int NMAX = 100002; + +int n,m,ssc=0,id=1; +int ids[NMAX],low[NMAX]; +bool onStack[NMAX],visited[NMAX]; +vector g[NMAX],Stack,ans[NMAX]; +void dfs(int node) { + visited[node]=true; + ids[node]=low[node]=id++; + onStack[node]=true; + Stack.push_back(node); + for(int i=0; i>n>>m; + for(int i=1; i<=m; i++) { + int x,y; + fin>>x>>y; + g[x].push_back(y); + } + for(int i=1; i<=n; i++) + if(!visited[i]) + dfs(i); + + fout< + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +vector> rez; + +stack s; + + + +void CTC(int nod, int low[], int disc[],int onS[], vector graf[]) + +{ + + static int i=1; + + disc[nod]=i; + + low[nod]=i; + + i++; + + onS[nod]=1; + + s.push(nod); + + + + for(int it : graf[nod]) + + { + + if(disc[it] == 0) + + { + + CTC(it, low, disc,onS,graf); + + low[nod]=min(low[nod],low[it]); + + } + + else if(onS[it] == 1) + + { + + low[nod]=min(low[nod],disc[it]); + + } + + } + + + + if(low[nod] == disc[nod]) + + { + + vector v; + + while(s.top()!=nod) + + { + + int x=s.top(); + + cout<>n>>m; + + vector graf[n+1]; + + int low[n+1]= {0}; + + int disc[n+1]= {0}; + + for(auto i: disc) cout<>x>>y; + + graf[x].push_back(y); + + + + } + + + + for(int i=1; i<=n; i++) + + { + + if(disc[i] == 0) + + CTC(i,low,disc,onS,graf); + + } + + + + fout< +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector>G,CC; + +vectorniv,L,C; + +vectorinS; + +stackS; + +int n,m,k,nv; + +void Citire() + +{ + + fin>>n>>m; + + G.resize(n+1); + + L.resize(n+1); + + inS.resize(n+1); + + niv.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + fin>>x>>y; + + G[x].push_back(y); + + } + +} + +void Tarjan(int x) + +{ + + niv[x] = L[x] = ++nv; + + S.push(x); + + inS[x] = true; + + for(auto y : G[x]) + + { + + if(niv[y]==0) + + { + + Tarjan(y); + + L[x]=min(L[x],L[y]); + + } + + else + + { + + if(inS[y]==true) + + L[x]=min(L[x],niv[y]); + + } + + } + + if(niv[x]==L[x]) + + { + + C.clear(); + + int y=-1; + + while(x!=y) + + { + + y=S.top(); + + S.pop(); + + C.push_back(y); + + inS[y]=false; + + } + + CC.push_back(C); + + } + +} + +void Afisare() + +{ + + fout< +#include +#include + + + + +using namespace std; +ifstream in ( "ctc.in" ); +ofstream out( "ctc.out" ); +int n, m, i, x, y, nivel, ctc; +int niv[100100], low[100100], fr[100100]; +vector L[100100], sol[100100]; +stack s; + +void dfs( int nod ){ + + nivel++; + niv[nod] = nivel; + low[nod] = nivel; + s.push(nod); + fr[nod] = 1; + + for ( auto vecin: L[nod] ) + if ( !niv[vecin] ){ + dfs(vecin); + low[nod] = min ( low[nod], low[vecin] ); + } else if ( fr[vecin] ) + low[nod] = min ( low[nod], low[vecin] ); + + if ( niv[nod] == low[nod] ){ + ctc++; + x = nod; + do { + x = s.top(); + sol[ctc].push_back(x); + fr[x] = 0; + s.pop(); + } while ( x != nod ); + } + +} + +int main() { + in>>n>>m; + for ( i=1; i <= m; i++ ){ + in>>x>>y; + L[x].push_back(y); + } + + for ( i=1; i <= n; i++ ) + if ( !niv[i] ) + dfs(i); + + out< + + + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 1e5+10; + + + +vector ans[DMAX]; + +vector arb[DMAX]; + +stack s; + + + +int uz[DMAX]; + +int lowlink[DMAX]; + + + +int n,m,comp,cnt; + + + +void dfsT(int node); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int x,y; + + + + fin>>n>>m; + + for(i=1;i<=m;i++){ + + fin>>x>>y; + + arb[x].push_back(y); + + } + + for(i=1;i<=n;i++) + + if(!uz[i]) + + dfsT(i); + + fout< 0) + + lowlink[node]=min(lowlink[node],lowlink[it]); + + } + + if(uz[node] == lowlink[node]){ + + comp++; + + int node2; + + do{ + + node2=s.top(); + + ans[comp].push_back(node2); + + s.pop(); + + uz[node2]=-1; + + if(node2 == node) + + break; + + }while(true); + + } + +} diff --git a/scc/tarjan/dffc79d6-e755-40d0-9159-ef7e687aae0c.cpp b/scc/tarjan/dffc79d6-e755-40d0-9159-ef7e687aae0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a4b2708d40d046d186efab2c2974fcde6214ebf7 --- /dev/null +++ b/scc/tarjan/dffc79d6-e755-40d0-9159-ef7e687aae0c.cpp @@ -0,0 +1,78 @@ +#include +#include + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("ctc.in"); +ofstream fout ("ctc.out"); + +const int Nmax = 100555; +int counter = 0; +int ind[Nmax], lowkey[Nmax]; +stack st; +char inStack[Nmax]; +vi G[Nmax]; +int ans = 0; +vector ctc; + +void dfs(int nod) { + if (ind[nod] != INF) { return; } + + ind[nod] = lowkey[nod] = ++counter; + st.push(nod); inStack[nod] = 1; + for(auto y: G[nod]) { + if (ind[y] == INF) { + dfs(y); + } + if (inStack[y]) { + lowkey[nod] = min(lowkey[nod], lowkey[y]); + } + } + if (ind[nod] == lowkey[nod]) { + ctc.resize(++ans); + vi &comp = ctc.back(); + while(!st.empty()) { + int x = st.top(); st.pop(); + inStack[x] = 0; + comp.push_back(x); + if (x == nod) { + break; + } + } + } +} + +int main(void) { + + std::ios_base::sync_with_stdio(false); + std::cin.tie(NULL); + memset(ind, 0x3f, sizeof(ind)); + memset(lowkey, 0x3f, sizeof(ind)); + int N, M, a, b; + fin >> N >> M; + for(int i = 0; i < (int)(M); i++) { + fin >> a >> b; + --a, --b; + G[a].push_back(b); + } + for(int i = 0; i < (int)(N); i++) { + if (ind[i] == INF) { + dfs(i); + } + } + fout << ans << '\n'; + for(const auto& comp: ctc) { + for(auto x: comp) { + fout << x+1 << " "; + } + fout << '\n'; + } + + return 0; +} diff --git a/scc/tarjan/e08ebe56-962e-4135-9e41-85aec6cf784c.cpp b/scc/tarjan/e08ebe56-962e-4135-9e41-85aec6cf784c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ac040fb08551cd59a15f7a0f11c6184b3912d6f --- /dev/null +++ b/scc/tarjan/e08ebe56-962e-4135-9e41-85aec6cf784c.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +std::ifstream in("ctc" ".in"); + +std::ofstream out("ctc" ".out"); + +const int N = 100000; + +std::vector L[N], S; + +int LL[N], ID[N], nId; + +std::bitset E, pS; + +std::vector> CTC; + +void dfs(int t) + +{ + + pS[t] = true; + + ID[t] = LL[t] = nId++; + + S.push_back(t); + + for (int f: L[t]) + + if (not E[f]) + + { + + if (not pS[f]) + + dfs(f); + + LL[t] = std::min(LL[t], LL[f]); + + } + + if (ID[t] == LL[t]) + + { + + CTC.emplace_back(); + + int back; + + do + + { + + back = S.back(); + + S.pop_back(); + + CTC.back().push_back(back); + + E[back] = true; + + } + + while (back != t); + + } + +} + +int main() + +{ + + int n, m; + + in >> n >> m; + + while (m--) + + { + + int a, b; + + in >> a >> b; + + L[--a].push_back(--b); + + } + + for (int i = 0; i < n; i++) + + if (not E[i]) + + dfs(i); + + out << CTC.size() << '\n'; + + for (auto ctc: CTC) + + { + + for (int i: ctc) + + out << i + 1 << ' '; + + out << '\n'; + + } + +} diff --git a/scc/tarjan/e1308dc9-1c3b-4c9c-a88a-d09db867eba8.cpp b/scc/tarjan/e1308dc9-1c3b-4c9c-a88a-d09db867eba8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b44e1105253920ab05a1ce0a3a762b8859cb82d --- /dev/null +++ b/scc/tarjan/e1308dc9-1c3b-4c9c-a88a-d09db867eba8.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include + + +using namespace std; +const int N_MAX = 1e5 + 1; + + + +struct TarjanVertex{ + + int index, tarjanIndex = -1, lowlink; + + bool onStack = false; + +}; + + + +vector graph[N_MAX]; + +stack stk; + +vector graphVertexes; + +vector > solutions; + + + +int nextAvailableIndex = 1; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +void Tarjan(int nodeIndex) + +{ + + graphVertexes[nodeIndex].tarjanIndex = nextAvailableIndex; + + graphVertexes[nodeIndex].lowlink = nextAvailableIndex; + + nextAvailableIndex++; + + stk.push(nodeIndex); + + graphVertexes[nodeIndex].onStack = true; + + + + for(auto neighborIndex: graph[graphVertexes[nodeIndex].index]) + + { + + if(graphVertexes[neighborIndex].tarjanIndex == -1) + + { + + Tarjan(graphVertexes[neighborIndex].index); + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].lowlink, graphVertexes[nodeIndex].lowlink); + + } + + else if (graphVertexes[neighborIndex].onStack == true) + + { + + graphVertexes[nodeIndex].lowlink = min(graphVertexes[neighborIndex].tarjanIndex, graphVertexes[nodeIndex].lowlink); + + } + + } + + + + if(graphVertexes[nodeIndex].tarjanIndex == graphVertexes[nodeIndex].lowlink) + + { + + vector sol; + + int neighborIndex; + + do + + { + + neighborIndex = stk.top(); + + sol.push_back(neighborIndex); + + stk.pop(); + + graphVertexes[neighborIndex].onStack = false; + + } while (neighborIndex != nodeIndex); + + + + solutions.push_back(sol); + + } + +} + + + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + + + graphVertexes.resize(n + 1); + + for(int i = 1; i <= n; i++) + + { + + graphVertexes[i].index = i; + + } + + + + for(int i = 0, st, dr; i < m; i++) + + { + + fin >> st >> dr; + + graph[st].push_back(dr); + + } + + + + for(int i = 1; i <= n; i++) + + { + + if(graphVertexes[i].tarjanIndex != -1) + + continue; + + + + Tarjan(i); + + } + + + + + + fout << solutions.size() << '\n'; + + + + for(auto sol: solutions) + + { + + for(auto val: sol) + + { + + fout << val << ' '; + + } + + + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/e1581582-02cb-48f4-80de-415102cf353b.cpp b/scc/tarjan/e1581582-02cb-48f4-80de-415102cf353b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94f7df67b15ec275b812f70e1cda898fb106e9e2 --- /dev/null +++ b/scc/tarjan/e1581582-02cb-48f4-80de-415102cf353b.cpp @@ -0,0 +1,166 @@ +#include + + +using namespace std; + + + +int n, m; + +vector> edges; + +vector> scc; + +int nrScc = -1, currentIndex = 0; + +vector lowlink; + +vector onStack; + +vector idx; + +stack st; + +vector comp; + + + +void read() { + + cin >> n >> m; + + edges.resize(n); + + for(int i=0; i> u >> v; + + edges[u-1].push_back(v-1); + + } + +} + + + +void dfs(int v) { + + lowlink[v] = idx[v] = currentIndex++; + + onStack[v] = true; + + st.push(v); + + for(int u : edges[v]) { + + if(idx[u] == -1) { + + dfs(u); + + lowlink[v] = min(lowlink[v], lowlink[u]); + + } else if(onStack[u] == true) + + lowlink[v] = min(lowlink[v], idx[u]); + + } + + + + if(lowlink[v] == idx[v]) { + + comp.clear(); + + int node; + + do { + + node = st.top(); + + comp.push_back(node); + + onStack[node] = false; + + st.pop(); + + } while(node!=v); + + scc.push_back(comp); + + } + +} + + + +void tarjan() { + + for(int i=0; i<(int)edges.size(); i++) + + if(idx[i] == -1) + + dfs(i); + +} + + + +void show() { + + for(int v=0; v +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +bitset<100005> iq; + +deque q; + +vector L[100005],aux; + +vector< vector > sol; + +int low[100005],niv[100005],g,n,m,i,x,y; + +void dfs(int nod) + +{ + + iq[nod]=1; + + niv[nod]=++g; + + low[nod]=g; + + q.push_back(nod); + + for(auto it:L[nod]) + + { + + if(niv[it]==0) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else if(iq[it]==1) + + { + + low[nod]=min(low[nod],low[it]); + + + + } + + } + + if(low[nod]==niv[nod]) + + { + + aux.clear(); + + int x=0; + + while(x!=nod) + + { + + x=q.back(); + + iq[x]=0; + + q.pop_back(); + + aux.push_back(x); + + } + + sol.push_back(aux); + + } + +} + +int main() + +{ + + fin>>n>>m; + + for(i=1; i<=m; i++) + + { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(i=1; i<=n; i++) + + { + + if(niv[i]==0) + + { + + dfs(i); + + } + + } + + fout< +#include +#include +#include +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector graf[100005]; + +int n, m, index; + +vector > totSol; + +bool viz[100005], inStack[100005]; + +int low[100005], niv[100005]; + +stack S; + + + +void citire() + +{ + + f>>n>>m; + + int x, y; + + for(int i = 0; i>x>>y; + + graf[x].push_back(y); + + } + +} + + + +void dfs(int currNode) + +{ + + viz[currNode] = true; + + S.push(currNode); + + inStack[currNode] = true; + + low[currNode] = index; + + niv[currNode] = index; + + index++; + + for(int i : graf[currNode]) + + { + + if(!viz[i]) + + { + + dfs(i); + + } + + if(inStack[i]) + + low[currNode] = min(low[i], low[currNode]); + + } + + if(low[currNode] == niv[currNode]) + + { + + vector sol; + + int el; + + do + + { + + el = S.top(); + + S.pop(); + + sol.push_back(el); + + inStack[el] = false; + + }while(el != currNode); + + totSol.push_back(sol); + + } + +} + + + +void parcurgere() + +{ + + for(int i = 1; i<=n; ++i) + + { + + if(!viz[i]) + + dfs(i); + + } + +} + + + +void afisare() + +{ + + g< + + +using namespace std; + + + +ifstream fin ("ctc.in"); + +ofstream fout ("ctc.out"); + + + +const int DIM = 100000 + 5; + + + +vector < int > a[DIM]; + +vector < vector < int > > sol; + +stack < int > st; + + + +int disc[DIM], low[DIM], temp; + +bool in_stack[DIM]; + + + +void dfs(int k) + +{ + + disc[k] = low[k] = ++temp; + + st.push(k); + + in_stack[k] = true; + + for(auto v : a[k]) { + + if(disc[v] == 0) { + + dfs(v); + + low[k] = min(low[k], low[v]); + + } + + else if(in_stack[v] == true) low[k] = min(low[k], low[v]); + + } + + if(disc[k] == low[k]) { + + vector < int > add; + + int node = k; + + do { + + node = st.top(); + + add.push_back(node); + + in_stack[node] = false; + + st.pop(); + + } + + while(node != k); + + sol.push_back(add); + + } + +} + +int main() + +{ + + int n, m; + + + + fin >> n >> m; + + for(int i = 1; i <= m; ++i) { + + int x, y; + + + + fin >> x >> y; + + a[x].push_back(y); + + } + + for(int i = 1; i <= n; ++i) { + + if(disc[i] == false) dfs(i); + + } + + fout << sol.size() << "\n"; + + for(auto v : sol) { + + for(auto w : v) { + + fout << w << " "; + + } + + fout << "\n"; + + } + + return 0; + +} diff --git a/scc/tarjan/e38c38e0-ef8e-4678-a2fc-c3dc113face0.cpp b/scc/tarjan/e38c38e0-ef8e-4678-a2fc-c3dc113face0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41f9a0eb98bd221aae3ba37e4fb0c3c60c317f06 --- /dev/null +++ b/scc/tarjan/e38c38e0-ef8e-4678-a2fc-c3dc113face0.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +int n, m, x, y, compAct, mom; + +vector < vector > muchii, componente; + +vector disc, low, componentaTata; + +stack stk; + +vector realVal; + +void Tarjan(int nod) + +{ + + low[nod]=disc[nod] = ++mom; + + stk.push(nod); + + for(auto & x : muchii[nod]) + + { + + if(disc[x]==-1) + + { + + Tarjan(x); + + low[nod]=min(low[nod], low[x]); + + } + + else + + { + + low[nod]=min(low[nod], disc[x]); + + } + + } + + + + if(low[nod]==disc[nod]) + + { + + componente.push_back( vector()); + + while(!stk.empty()&&stk.top()!=nod) + + { + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + +} + + + +int main() + +{ + + in>>n>>m; + + muchii.resize(n+1); componentaTata.resize(n+1, -1); + + disc.resize(n+1, -1); low.resize(n+1, -1); realVal.resize(n+1, 0); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(disc[i]==-1) + + mom=0, Tarjan(i); + + + + out< +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + + + +struct Node { + + int visited; + + int minReach; + + bool onStack; + +}; + + + +int n, m; + +int strongSiz; + + + +Node v[1 + NMAX]; + +vector g[1 + NMAX]; + +vector strong[1 + NMAX]; + +stack st; + + + +int step = 0; + + + +void computeStrong(int from) { + + step++; + + v[from].visited = step; + + v[from].minReach = from; + + st.push(from); + + v[from].onStack = true; + + for(int i = 0; i < g[from].size(); i++) { + + int to = g[from][i]; + + if(v[to].visited == 0){ + + computeStrong(to); + + if(v[v[from].minReach].visited > v[v[to].minReach].visited) { + + v[from].minReach = v[to].minReach; + + } + + } else if(v[to].onStack == true) { + + if(v[v[from].minReach].visited > v[to].visited) { + + v[from].minReach = to; + + } + + } + + } + + if(v[from].minReach == from) { + + strongSiz++; + + int popped; + + do { + + popped = st.top(); + + st.pop(); + + strong[strongSiz].push_back(popped); + + v[popped].onStack = false; + + } while (popped != from); + + } + +} + + + +int main() { + + + + int a, b; + + in >> n >> m; + + for(int i = 1; i <= m; i++) { + + in >> a >> b; + + g[a].push_back(b); + + } + + for(int i = 1; i <= n; i++) { + + if(v[i].visited == 0) { + + computeStrong(i); + + } + + } + + out << strongSiz << '\n'; + + for(int i = 1; i <= strongSiz; i++) { + + for(int j = 0; j < strong[i].size(); j++) { + + out << strong[i][j] << ' '; + + } + + out << '\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/e3dabb11-82ee-49e7-b831-dc644bd86ca4.cpp b/scc/tarjan/e3dabb11-82ee-49e7-b831-dc644bd86ca4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c85de64e1b823b999038f9a3b64040ed5b80bdd6 --- /dev/null +++ b/scc/tarjan/e3dabb11-82ee-49e7-b831-dc644bd86ca4.cpp @@ -0,0 +1,182 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int index = 1; + +int n, m, nrc; + +int isInStack[100005], viz[100005], lowlink[100005], idx[100005]; + +vector graph[100005]; + +vector CC[100005]; + +stack S; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + + + +void addCC() + +{ + + nrc++; + + int top; + + do{ + + top = S.top(); + + CC[nrc].push_back(top); + + S.pop(); + + isInStack[top] = 0; + + }while(lowlink[top] != idx[top]); + + + +} +void solve(int nod) + +{ + + S.push(nod); + + isInStack[nod] = 1; + + viz[nod] = 1; + + idx[nod] = index; + + lowlink[nod] = index; + + index ++; + + + + for(auto &v:graph[nod]) + + { + + if(viz[v] == 0) + + { + + solve(v); + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + else{ + + if(isInStack[v] == 1) + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + } + + + + if(lowlink[nod] == idx[nod]) + + { + + addCC(); + + } + + + +} +void tarjan() + +{ + + for(int i = 1; i <= n; ++i) + + if(viz[i] == 0) + + solve(i); + +} + + + + + +void write() + +{ + + g< +#include +#include +#include +using namespace std; + + + +ifstream in ("ctc.in"); + +ofstream out("ctc.out"); + +int n, m, x, y, compAct, mom; + +vector < vector > muchii, componente; + +vector disc, low, componentaTata; + +stack stk; + +vector realVal; + +void Tarjan(int nod) + +{ + + low[nod]=disc[nod] = ++mom; + + stk.push(nod); + + for(auto & x : muchii[nod]) + + { + + if(disc[x]==-1) + + { + + Tarjan(x); + + low[nod]=min(low[nod], low[x]); + + } + + else + + { + + low[nod]=min(low[nod], disc[x]); + + } + + } + + + + if(low[nod]==disc[nod]) + + { + + componente.push_back( vector()); + + while(!stk.empty()&&stk.top()!=nod) + + { + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + + componente.back().push_back(stk.top()); + + disc[stk.top()]+=2*n; + + componentaTata[stk.top()]=componente.size(); + + stk.pop(); + + } + +} + + + +int main() + +{ + + in>>n>>m; + + muchii.resize(n+1); componentaTata.resize(n+1, -1); + + disc.resize(n+1, -1); low.resize(n+1, -1); realVal.resize(n+1, 0); + + for(int i=1; i<=m; i++) + + { + + in>>x>>y; + + muchii[x].push_back(y); + + } + + for(int i=1; i<=n; i++) + + if(disc[i]==-1) + + mom=0, Tarjan(i); + + + + out< + +using namespace std; + +typedef vector> Graph; + +struct traversal_data { + Graph &G; + vector &visited; + stack &post_order_stack; + vector &in_stack; + + int current_time; + vector &found_time; + vector &lowlink; + + int SCC_index; + Graph &SCCs; +}; + +void read_input(int &n, int &m, Graph &G, Graph &TG); +void get_scc(int current_node, traversal_data &td); +void write_solution(int count, Graph &SCCs); + +int main() { + int n, m; + Graph G, TG, SCCs; + read_input(n, m, G, TG); + + vector visited(n, false); + stack post_order_stack; + vector in_stack(n, false); + SCCs.resize(n); + vector found_time(n, -1); + vector lowlink(n, -1); + + traversal_data td = {G, visited, post_order_stack, in_stack, 0, found_time, lowlink, 0, SCCs}; + for (int i = 0; i < n; ++i) { + if (not visited[i]) get_scc(i, td); + } + + write_solution(td.SCC_index, SCCs); + + return 0; +} + +const char* input_file = "ctc.in"; +void read_input(int &n, int &m, Graph &G, Graph &TG) { + ifstream f(input_file); + + f >> n >> m; + G.resize(n); + TG.resize(n); + + for (int i = 0; i < m; ++i) { + int x, y; + f >> x >> y; + + x--; y--; + + G[x].push_back(y); + TG[y].push_back(x); + } +} + +void get_scc(int current_node, traversal_data &td) { + td.visited[current_node] = true; + td.found_time[current_node] = td.current_time++; + td.lowlink[current_node] = td.found_time[current_node]; + td.post_order_stack.push(current_node); + td.in_stack[current_node] = true; + + for (auto neighbour: td.G[current_node]) { + if (not td.visited[neighbour]) { + get_scc(neighbour, td); + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } else if (td.in_stack[neighbour]) { + td.lowlink[current_node] = min( + td.lowlink[current_node], + td.lowlink[neighbour] + ); + } + } + + if (td.found_time[current_node] == td.lowlink[current_node]) { + int top_node; + do { + top_node = td.post_order_stack.top(); + td.post_order_stack.pop(); + + td.SCCs[td.SCC_index].push_back(top_node); + td.in_stack[top_node] = false; + } while (top_node != current_node); + td.SCC_index++; + } +} + +const char* output_file = "ctc.out"; +void write_solution(int count, Graph &SCCs) { + ofstream f(output_file); + f << count << "\n"; + for (int i = 0; i < count; ++i) { + for (auto node: SCCs[i]) { + f << node + 1 << " "; + } + f << "\n"; + } +} diff --git a/scc/tarjan/e7a8f038-39e4-4cae-8e1c-fca4716bbce5.cpp b/scc/tarjan/e7a8f038-39e4-4cae-8e1c-fca4716bbce5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..827232e01c8ab90a81b678e79f2ea41d16e4dd9d --- /dev/null +++ b/scc/tarjan/e7a8f038-39e4-4cae-8e1c-fca4716bbce5.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int NMAX=1e5+10; + +vector < int > Edge[NMAX], Components[NMAX]; + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + +bitset < NMAX > Seen, Belongs; + +stack < int > Stack; + + + +void Read() + +{ + + in>>n>>m; + + int x, y; + + for(int i=1; i<=m; i++) + + in>>x>>y, Edge[x].push_back(y); + +} + + + +void Tarjan(int x) + +{ + + niv++; + + Seen[x]=1; + + Belongs[x]=1; + + Stack.push(x); + + RootLevel[x]=niv; + + NodeLevel[x]=niv; + + for(int i : Edge[x]) + + { + + if(!Seen[i]) + + { + + Tarjan(i); + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + else + + if(Belongs[i]) + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + if(RootLevel[x]==NodeLevel[x]) + + { + + nrCMP++; + + int node=0; + + do + + { + + node=Stack.top(); + + Stack.pop(); + + Components[nrCMP].push_back(node); + + Belongs[node]=0; + + }while(node!=x); + + } + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + in.tie(NULL); + + Read(); + + for(int i=1; i<=n; i++) + + if(!Seen[i]) + + niv=0, Tarjan(i); + + out< +using namespace std; + +int n, m, t; +vector adj[100001]; +int ind[100001]; +int low[100001]; +int vizitat[100001]; +stack s; +vector> ans; + +void components(int current) +{ + ind[current] = ++t; + low[current] = t; + vizitat[current] = 1; + s.push(current); + for(auto x : adj[current]) + { + if(ind[x] == -1) + { + components(x); + low[current] = min(low[current], low[x]); + } + else if(vizitat[x] == 1) + low[current] = min(low[current], ind[x]); + } + int w = 0; + set temp; + if(low[current] == ind[current]) + { + while(s.top() != current) + { + w = s.top(); + temp.insert(w); + vizitat[w] = 0; + s.pop(); + } + w = s.top(); + temp.insert(w); + vizitat[w] = 0; + s.pop(); + ans.push_back(temp); + } +} + +void Tarjan() +{ + for(int i = 1; i <= n; i++) + { + ind[i] = -1; + low[i] = -1; + vizitat[i] = 0; + } + for(int i = 1; i <= n; i++) + if(ind[i] == -1) + components(i); +} +int main() { + ifstream f("ctc.in"); + ofstream g("ctc.out"); + f >> n >> m; + for(int i = 0; i < m; i++) + { + int x,y; + f >> x >> y; + adj[x].push_back(y); + } + Tarjan(); + g << ans.size() << '\n'; + for(auto &x : ans) + { + for(auto el : x) + g << el << " "; + g << '\n'; + } + f.close(); + g.close(); + return 0; +} diff --git a/scc/tarjan/e8bfbe48-7ee9-4ac3-95e1-f77110af2f6e.cpp b/scc/tarjan/e8bfbe48-7ee9-4ac3-95e1-f77110af2f6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5ed92118bc72e6785b0ecab9ca8cbe1fcada2e0 --- /dev/null +++ b/scc/tarjan/e8bfbe48-7ee9-4ac3-95e1-f77110af2f6e.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +const int MAXN = 100005; + + + +bool inSt[MAXN]; + +int ll[MAXN], mark[MAXN], n, m, val, nr; + +vector graph[MAXN], comp[MAXN]; + +stack st; + + + +void read() + +{ + + fin >> n >> m; + + for (int i = 0; i < m; ++i) { + + int x, y; + + fin >> x >> y; + + graph[x].push_back(y); + + } + +} + + + +void tarjan(int node) + +{ + + st.push(node); + + inSt[node] = true; + + mark[node] = ll[node] = ++val; + + for (const auto& it: graph[node]) + + if (mark[it] == 0) { + + tarjan(it); + + ll[node] = min(ll[node], ll[it]); + + } + + else if (inSt[it] == true) + + ll[node] = min(ll[node], mark[it]); + + if (ll[node] == mark[node]) { + + while (!st.empty()) { + + int top = st.top(); + + st.pop(); + + inSt[top] = false; + + comp[nr].push_back(top); + + if (top == node) + + break; + + } + + ++nr; + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + for (int i = 0; i < nr; ++i) { + + for (const auto &it: comp[i]) + + fout << it << ' '; + + fout << '\n'; + + } + +} + + + +int main() + +{ + + read(); + + for (int i = 1; i <= n; ++i) + + if (mark[i] == 0) + + tarjan(i); + + print(); + + return 0; + +} diff --git a/scc/tarjan/e8e283ec-ec38-4bf2-add5-3627258b837f.cpp b/scc/tarjan/e8e283ec-ec38-4bf2-add5-3627258b837f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9bb8d68d175b97dfbfa9fa342c130b6d0b49eca --- /dev/null +++ b/scc/tarjan/e8e283ec-ec38-4bf2-add5-3627258b837f.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +ifstream fin("ctc.in"); +ofstream fout("ctc.out"); + +const int oo=INT_MAX/2; + +typedef vector vi; + +int n,m,k; +vector g; + +vi lowlink, idx, inStk; +vi stk; + +vector ctcs; + +int _index=1; +void tarjan(int nd){ + lowlink[nd]=idx[nd]=_index++; + inStk[nd]=1; + stk.push_back(nd); + + for(auto vc:g[nd]){ + if(idx[vc]==0){ + tarjan(vc); + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + else if(inStk[vc]){ + lowlink[nd]=min(lowlink[nd],lowlink[vc]); + } + } + if(lowlink[nd]==idx[nd]){ + + ctcs.push_back(vi()); + int aux; + do { + ctcs.back().push_back(aux=stk.back()); + inStk[stk.back()]=0; + stk.pop_back(); + } + while(aux!=nd); + } +} + +int main(){ + fin>>n>>m; + g.resize(n+1), lowlink.resize(n+1), idx.resize(n+1,0), inStk.resize(n+1,0); + for(int i=m;i--;){ + int x,y; fin>>x>>y; + g[x].push_back(y); + } + for(int i=1;i<=n;++i) + if(idx[i]==0) tarjan(i); + fout< +#include +#include +#include + + +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, cnt = 0, niv = 0, Niv[100001], NivMin[100001]; + +vector G[100001], Ctc[100001]; + +stack Stack; + +bool V[100001], InStack[100001]; + + + +void Dfs(int x) + +{ + + V[x] = true; + + ++niv; + + Niv[x] = NivMin[x] = niv; + + Stack.push(x); + + InStack[x] = true; + + for (int y : G[x]) + + { + + if (!V[y]) + + { + + Dfs(y); + + NivMin[x] = min(NivMin[x], NivMin[y]); + + } + + else if (InStack[y]) + + NivMin[x] = min(NivMin[x], NivMin[y]); + + } + + if (Niv[x] == NivMin[x]) + + { + + ++cnt; + + while (true) + + { + + Ctc[cnt].push_back(Stack.top()); + + InStack[Stack.top()] = false; + + if (Stack.top() == x) + + { + + Stack.pop(); + + break; + + } + + Stack.pop(); + + } + + } + +} + + + +int main() + +{ + + fin >> n >> m; + + for (int i = 1; i <= m; ++i) + + { + + int x, y; + + fin >> x >> y; + + G[x].push_back(y); + + } + + for (int i = 1; i <= n; ++i) + + { + + if (!V[i]) + + Dfs(i); + + } + + fout << cnt << '\n'; + + for (int i = 1; i <= cnt; ++i) + + { + + for (int x : Ctc[i]) + + fout << x << " "; + + fout << '\n'; + + } + + + + return 0; + +} diff --git a/scc/tarjan/ecda7e64-529d-4a38-b344-158b0c69f1ec.cpp b/scc/tarjan/ecda7e64-529d-4a38-b344-158b0c69f1ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5fbe3ea369f6d0b975f377108788c91eb725215 --- /dev/null +++ b/scc/tarjan/ecda7e64-529d-4a38-b344-158b0c69f1ec.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fi ("ctc.in"); + +ofstream fo ("ctc.out"); + +int n, m, a, b, idx, cnt; + +stack s; + +vector ctc[100001]; + +class Node { + + public: + + bool stack = false; + + int idx = -1, low; + + vector edges; + + + + void addNode(int x) { + + edges.push_back(x); + + } + +} v[100001]; + + + +void connect_component(int x) { + + v[x].idx = v[x].low = ++idx; + + v[x].stack = true; + + s.push(x); + + for (auto i:v[x].edges) { + + if (v[i].idx == -1) { + + connect_component(i); + + v[x].low = min(v[x].low, v[i].low); + + } else if (v[i].stack && v[i].idx < v[x].idx) { + + v[x].low = min(v[x].low, v[i].idx); + + } + + } + + if (v[x].idx == v[x].low) { + + while (1) { + + a = s.top(); + + s.pop(); + + ctc[cnt].push_back(a); + + v[a].stack = false; + + if (a == x) + + break; + + } + + ++cnt; + + } + +} + + + +int main() { + + fi >> n >> m; + + for (int i = 1; i <= m; ++i) { + + fi >> a >> b; + + v[a].addNode(b); + + } + + for (int i = 1; i <= n; ++i) + + if (v[i].idx == -1) + + connect_component(i); + + fo << cnt << '\n'; + + for (int i = 0; i < cnt; ++i) + + for (auto j:ctc[i]) + + fo << j << " \n"[j==ctc[i].back()]; + + return 0; + +} diff --git a/scc/tarjan/ed2f0015-db6d-4aab-9b30-14eb882cf86a.cpp b/scc/tarjan/ed2f0015-db6d-4aab-9b30-14eb882cf86a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..680c4981d6f62bbdcb83d64bd431aef285d62dd0 --- /dev/null +++ b/scc/tarjan/ed2f0015-db6d-4aab-9b30-14eb882cf86a.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +using namespace std; + +int n, m, i, j, k, index, ord[100005], niv_min[100005]; +vector vec[100005], comp[100005]; +stack st; +set s; + +void tarjan(int nod) { + int ind, x; + + ord[nod] = index; + niv_min[nod] = index; + index++; + st.push(nod); + s.insert(nod); + + for(ind = 0; ind < vec[nod].size(); ind++) + if(!ord[vec[nod][ind]]) { + tarjan(vec[nod][ind]); + niv_min[nod] = min(niv_min[nod], niv_min[vec[nod][ind]]); + } + else if(s.find(vec[nod][ind]) != s.end()) + niv_min[nod] = min(niv_min[nod], ord[vec[nod][ind]]); + + if(ord[nod] == niv_min[nod]) + while(!st.empty()) { + x = st.top(); + st.pop(); + s.erase(x); + comp[k].push_back(x); + if(x == nod) { + k++; + break; + } + } +} + +int main() { + ifstream f("ctc.in"); + ofstream g("ctc.out"); + + f >> n >> m; + while(m) { + f >> i >> j; + + vec[i].push_back(j); + m--; + } + f.close(); + + k = 0; + index = 1; + for(i = 1; i <= n; i++) + if(!ord[i]) + tarjan(i); + + g << k << '\n'; + for(i = 0; i < k; i++, g << '\n') + for(j = 0; j < comp[i].size(); j++) + g << comp[i][j] << ' '; + g.close(); + + return 0; +} diff --git a/scc/tarjan/ed58ac6a-2ea0-414d-9103-c74339760d2b.cpp b/scc/tarjan/ed58ac6a-2ea0-414d-9103-c74339760d2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68c7ec1b88ae70ebe741306928ee9409b9efeae6 --- /dev/null +++ b/scc/tarjan/ed58ac6a-2ea0-414d-9103-c74339760d2b.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int nr,indice,low[100100],nivel[100100],inStiva[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + nivel[nod]=indice; + + low[nod]=nivel[nod]; + + inStiva[nod]=1; + + s.push(nod); + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)if(nivel[i]==0)Tarjan(i); + + g< +using namespace std; + + + +const int maxN=(1e5)+5; + + + +int lvl,niv[maxN],low[maxN],st[maxN],vf,ctc; + +bool inStack[maxN]; + + + +vector v[maxN],C[maxN]; + + + +void dfs(int nod) + +{ + + lvl++; + + niv[nod]=low[nod]=lvl; + + st[++vf]=nod; + + inStack[nod]=1; + + + + for(auto it:v[nod]) + + { + + if(!niv[it]) + + { + + dfs(it); + + low[nod]=min(low[nod],low[it]); + + } + + else + + if(inStack[it]) + + low[nod]=min(low[nod],low[it]); + + } + + + + if(low[nod]==niv[nod]) + + { + + int x=-1; + + ctc++; + + while(x!=nod) + + { + + x=st[vf]; + + C[ctc].push_back(x); + + inStack[x]=0; + + vf--; + + } + + } + +} + + + +int n,m,x,y; + + + +int main() + +{ + + freopen("ctc.in","r",stdin); + + freopen("ctc.out","w",stdout); + + + + scanf("%d%d",&n,&m); + + + + for(int i=1;i<=m;i++) + + { + + scanf("%d%d",&x,&y); + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;i++) + + if(!niv[i]) dfs(i); + + + + + + printf("%d\n",ctc); + + + + for(int i=1;i<=ctc;i++) + + { + + for(auto it:C[i]) + + printf("%d ",it); + + + + printf("\n"); + + } + + + + + + return 0; + +} diff --git a/scc/tarjan/eea58bdd-8f20-4de8-99ea-37a0a45c876b.cpp b/scc/tarjan/eea58bdd-8f20-4de8-99ea-37a0a45c876b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0df79abf2bb715ae8de02755d903ff72e0efe13 --- /dev/null +++ b/scc/tarjan/eea58bdd-8f20-4de8-99ea-37a0a45c876b.cpp @@ -0,0 +1,138 @@ +#include + + + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +const int lmt = 1e5+1; + + + +int n,m,id,ids[lmt], low[lmt], stccCount; + +vector aList[lmt], rsp[lmt]; + +stack stacc; + +bool on_stacc[lmt]; + + + +void dfs(int nod) + +{ + + on_stacc[nod]=1; + + ids[nod]=low[nod]=++id; + + stacc.push(nod); + + + + vector::iterator i; + + for(i=aList[nod].begin();i!=aList[nod].end();++i) + + { + + if(!ids[*i]) + + dfs(*i); + + if(on_stacc[*i]) + + low[nod]=min(low[nod], low[*i]); + + } + + if(low[nod]==ids[nod]) + + { + + ++stccCount; + + while(!stacc.empty()) + + { + + int poz = stacc.top(); + + rsp[stccCount].push_back(poz); + + on_stacc[stacc.top()]=0; + + stacc.pop(); + + + + if(poz==nod) + + break; + + } + + } + +} + + + +int main() + +{ + + fin>>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y; + + fin>>x>>y; + + aList[x].push_back(y); + + } + + for(int i=1;i<=n;++i) + + { + + if(!ids[i]) + + dfs(i); + + } + + fout<::iterator j; + + for(j=rsp[i].begin();j!=rsp[i].end();++j) + + fout<<*j<<' '; + + + + fout<<'\n'; + + } + + return 0; + +} diff --git a/scc/tarjan/eefb5873-af33-400f-ac3b-231c67f471fe.cpp b/scc/tarjan/eefb5873-af33-400f-ac3b-231c67f471fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68c7ec1b88ae70ebe741306928ee9409b9efeae6 --- /dev/null +++ b/scc/tarjan/eefb5873-af33-400f-ac3b-231c67f471fe.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int nr,indice,low[100100],nivel[100100],inStiva[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + nivel[nod]=indice; + + low[nod]=nivel[nod]; + + inStiva[nod]=1; + + s.push(nod); + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)if(nivel[i]==0)Tarjan(i); + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int index = 1; + +int n, m, nrc; + +int isInStack[100005], viz[100005], lowlink[100005], idx[100005]; + +vector graph[100005]; + +vector CC[100005]; + +stack S; + + + +void read() + +{ + + int x, y; + + + + f>>n>>m; + + for(int i = 0; i < m; ++i) + + { + + f>>x>>y; + + graph[x].push_back(y); + + } + +} + + + + + +void addCC() + +{ + + nrc++; + + int top; + + do{ + + top = S.top(); + + CC[nrc].push_back(top); + + S.pop(); + + isInStack[top] = 0; + + }while(lowlink[top] != idx[top]); + + + +} +void solve(int nod) + +{ + + S.push(nod); + + isInStack[nod] = 1; + + viz[nod] = 1; + + idx[nod] = index; + + lowlink[nod] = index; + + index ++; + + + + for(auto &v:graph[nod]) + + { + + if(viz[v] == 0) + + { + + solve(v); + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + else{ + + if(isInStack[v] == 1) + + lowlink[nod] = min(lowlink[nod], lowlink[v]); + + } + + } + + + + if(lowlink[nod] == idx[nod]) + + { + + addCC(); + + } + + + +} +void tarjan() + +{ + + for(int i = 1; i <= n; ++i) + + if(viz[i] == 0) + + solve(i); + +} + + + + + +void write() + +{ + + g< +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +class Graph { + + private: + + int n; + + vector> ad; + + + + vector ind, low; + + vector> sccs; + + + + void dfs(int node, int& id, stack& st, vector& onStack) { + + ind[node] = low[node] = ++id; + + st.push(node); onStack[node] = true; + + + + for (int nghb : ad[node]) + + if (!ind[nghb]) { + + dfs(nghb, id, st, onStack); + + low[node] = min(low[node], low[nghb]); + + } + + else if (onStack[nghb]) + + low[node] = min(low[node], low[nghb]); + + + + if (ind[node] == low[node]) { + + sccs.emplace_back(); + + while (true) { + + int top = st.top(); + + st.pop(); onStack[top] = false; + + + + low[top] = ind[node]; + + sccs.back().push_back(top); + + + + if (top == node) + + break; + + } + + } + + } + + + + public: + + Graph(int n) : + + n(n), ad(n + 1), + + ind(n + 1), low(n + 1) { } + + + + void addEdge(int x, int y) { + + ad[x].push_back(y); + + } + + + + vector>& tarjan() { + + stack st; + + vector onStack(n + 1); + + + + int id = 0; + + for (int i = 1; i <= n; i++) + + if (!ind[i]) + + dfs(i, id, st, onStack); + + return sccs; + + } + +}; + + + +int main() { + + int n, m; + + fin >> n >> m; + + + + Graph graph(n); + + for (int i = 0; i < m; i++) { + + int x, y; fin >> x >> y; + + graph.addEdge(x, y); + + } + + + + vector>& sccs = graph.tarjan(); + + fout << sccs.size() << '\n'; + + for (vector& scc : sccs) { + + for (int node : scc) + + fout << node << ' '; + + fout << '\n'; + + } + + + + fout.close(); + + return 0; + +} diff --git a/scc/tarjan/f21f6225-b4c9-4796-b6ac-7846bb0ab4b7.cpp b/scc/tarjan/f21f6225-b4c9-4796-b6ac-7846bb0ab4b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..657879cafb557ab937de45da785ae5a44203e556 --- /dev/null +++ b/scc/tarjan/f21f6225-b4c9-4796-b6ac-7846bb0ab4b7.cpp @@ -0,0 +1,138 @@ +#include +using namespace std; + + + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + + + +int n, m, i, nrc, h[100005], low[100005], x, inaltime; + +bitset<100005> viz, eInStiva; + +vectorL[100005]; + +vectorc[100005]; + +stackst; + + + +void citireGraf(){ + + int i, x, y; + + fin >> n >> m; + + for(i = 1; i <= m; i++){ + + fin >> x >> y; + + L[x].push_back(y); + + } + +} + + + +void dfs(int x) { + + int z; + + inaltime++; + + viz[x] = 1; + + low[x] = h[x] = inaltime; + + + + st.push(x); + + eInStiva[x] = 1; + + + + for(auto y : L[x]) { + + if(viz[y] == 0){ + + dfs(y); + + low[x] = min(low[x], low[y]); + + } + + else if(eInStiva[y]) + + low[x] = min(low[x], h[y]); + + } + + + + + + if (low[x] == h[x]){ + + nrc++; + + do{ + + z = st.top(); + + st.pop(); + + eInStiva[z] = 0; + + c[nrc].push_back(z); + + }while(z!=x); + + } + +} + + + + + +int main() { + + + + citireGraf(); + + for(i = 1; i <= n; i++) + + if(viz[i] == 0) + + dfs(i); + + + + + + fout<< nrc << '\n'; + + for(i = 1; i <= nrc; i++){ + + for (auto x : c[i]) + + fout << x <<' '; + + fout <<'\n'; + + } + + + + fin.close(); fout.close(); + + return 0; + +} diff --git a/scc/tarjan/f43922ea-8623-4717-92c3-02b9dcd69fa8.cpp b/scc/tarjan/f43922ea-8623-4717-92c3-02b9dcd69fa8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa33120e456b78d1057125cb4695c71a70c6621 --- /dev/null +++ b/scc/tarjan/f43922ea-8623-4717-92c3-02b9dcd69fa8.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + +const char iname[] = "ctc.in"; + +const char oname[] = "ctc.out"; +vector adj[100005], con, idx, lowlink, in_stack; + + + +vector < vector > C; + + + +stack S; + + + +int indecs; + + + +void read_in(vector * adj, int& n, const char iname[]) + +{ + + ifstream in(iname); + + int cnt_edges, x, y; + + + + in >> n; + + for (in >> cnt_edges; cnt_edges > 0; -- cnt_edges) + + in >> x >> y, + + adj[x - 1].push_back(y - 1); + + in.close(); + +} + + + +void print_out(const vector < vector >& G, const char oname[]) + +{ + + ofstream out(oname); + + + + out << G.size() << "\n"; + + for (size_t i = 0; i < G.size(); ++ i) { + + for (size_t j = 0; j < G[i].size(); ++ j) + + out << G[i][j] + 1 << " "; + + out << "\n"; + + } + + out.close(); + +} + + + +void tarjan(const int n, const vector * adj) + +{ + + idx[n] = lowlink[n] = indecs; + + indecs ++; + + S.push(n), in_stack[n] = 1; + + + + vector ::const_iterator it; + + for (it = adj[n].begin(); it != adj[n].end(); ++ it) { + + if (idx[*it] == -1) + + tarjan(*it, adj), + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + else if (in_stack[*it]) + + lowlink[n] = ((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) { + + con.clear(); + + int node; + + do { + + con.push_back(node = S.top()); + + S.pop(), in_stack[node] = 0; + + } + + while (node != n); + + C.push_back(con); + + } + +} + + + +int main(void) + +{ + + int n; + + read_in(adj, n, iname); + + + + idx.resize(n), lowlink.resize(n), in_stack.resize(n); + + idx.assign(n, -1), in_stack.assign(n, 0); + + for (int i = 0; i < n; ++ i) if (idx[i] == -1) + + tarjan(i, adj); + + + + print_out(C, oname); + + + + return 0; + +} diff --git a/scc/tarjan/f4e227d9-e5ea-4b36-b772-c5207402cf8b.cpp b/scc/tarjan/f4e227d9-e5ea-4b36-b772-c5207402cf8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80c4939bae6bfe4d3df22b71800f825b0247e2e7 --- /dev/null +++ b/scc/tarjan/f4e227d9-e5ea-4b36-b772-c5207402cf8b.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int MAX = 100001; + + + +int nrctc, t; + +vector v[MAX], ct[MAX], disc, low, isInStack; + +stack s; + + + +void ctc(int nod) + +{ + + t++; + + disc[nod] = low[nod] = t; + + s.push(nod); + + isInStack[nod] = true; + + + + for(auto it : v[nod]) + + { + + if(!disc[it]) + + { + + ctc(it); + + low[nod] = min(low[nod], low[it]); + + } + + else if(isInStack[it])low[nod] = min(low[nod], disc[it]); + + } + + + + if(low[nod] == disc[nod]) + + { + + while(s.top() != nod) + + { + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + } + + ct[nrctc].push_back(s.top()); + + isInStack[s.top()] = false; + + s.pop(); + + nrctc++; + + } + + + +} + + + +int main() + +{ + + int n, m; + + in >> n >> m; + + disc.assign(n, 0); + + low.assign(n, 0); + + isInStack.assign(n, 0); + + for(int i=0; i> x >> y; x--; y--; + + v[x].push_back(y); + + } + + for(int i=0; i + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n,m,a,b,cnt; + +bitset<100010> ok; + +int x[100010],y[100010]; + +vector v[100010]; + +vector > C; + +stack St; + + + +void dfs(int poz){ + + x[poz]=y[poz]=++cnt; + + St.push(poz);ok[poz]=1; + + for(auto it:v[poz]) + + if(!x[it]){ + + dfs(it); + + y[poz]=min(y[poz],y[it]); + + }else + + if(ok[it]) + + y[poz]=min(y[poz],x[it]); + + if(x[poz]!=y[poz])return; + + vector c; + + while(1){ + + int aux=St.top();St.pop();ok[aux]=0; + + c.push_back(aux); + + if(aux==poz)break; + + } + + C.push_back(c); + +} + + + +int main(){ + + f>>n>>m; + + for(int i=1;i<=m;i++){ + + f>>a>>b; + + v[a].push_back(b); + + } + + for(int i=1;i<=n;i++) + + if(!x[i]){ + + dfs(i); + + } + + g< +#include +#include +#include + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +int const NMAX = 1e5; + +int n, m; + +vector g[1 + NMAX]; + +vector scc[1 + NMAX]; + + + +int nComp; + +stack st; + +int time1[1 + NMAX]; + +int lowLink[1 + NMAX]; + +int t = 0; + +bool onStack[1 + NMAX]; + + + +void tarjan(int from) { + + + + t++; + + time1[from] = t; + + lowLink[from] = t; + + st.push(from); + + onStack[from] = true; + + for(int to : g[from]) { + + if(time1[to] == 0) { + + tarjan(to); + + lowLink[from] = min(lowLink[from], lowLink[to]); + + } else if(onStack[to]) { + + lowLink[from] = min(lowLink[from], lowLink[to]); + + } + + } + + + + if(lowLink[from] == time1[from]) { + + + + while(!st.empty() && lowLink[from] <= lowLink[st.top()]) { + + scc[nComp].push_back(st.top()); + + onStack[st.top()] = false; + + st.pop(); + + } + + + + nComp++; + + } + +} + + + +int main(){ + + in >> n >> m; + + for(int i = 1; i <= m; i++) { + + int a, b; + + in >> a >> b; + + g[a].push_back(b); + + } + + for(int i = 1; i <= n; i++) { + + if(time1[i] == 0) { + + tarjan(i); + + } + + } + + out << nComp << "\n"; + + for(int i = 0; i < nComp; i++) { + + for(int j = 0; j < scc[i].size(); j++) { + + out << scc[i][j] << " "; + + } + + out << "\n"; + + } + +} diff --git a/scc/tarjan/f7449a08-a50d-4ccf-9f16-2ee009caf184.cpp b/scc/tarjan/f7449a08-a50d-4ccf-9f16-2ee009caf184.cpp new file mode 100644 index 0000000000000000000000000000000000000000..764e7498985883ad4ea4c23a1ab454d6104226c8 --- /dev/null +++ b/scc/tarjan/f7449a08-a50d-4ccf-9f16-2ee009caf184.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +int n, m, _index = 1; + +bool in_st[100100]; + +int idx[100100], lowlink[100100]; + +vector graf[100100], componenta; + +stack st; + +vector> CTC; + + + + + + + +void Read() + +{ + + f>>n>>m; + + for(int i=1;i<=m;i++) + + { + + int x,y; + + f>>x>>y; + + graf[x].push_back(y); + + } + + f.close(); + +} + + + + + +void Tarjan(int i) + +{ + + idx[i] = _index; + + lowlink[i] = _index; + + _index++; + + st.push(i); + + in_st[i] = 1; + + + + for(int j=0;j + + +using namespace std; + + + +int id_n[100005], low_link[100005]; + +bool inStack[100005]; + +vector edge[100005]; + +vector> ctc; + +vector conex; + +stack stk; + +int dist = 0; + +int n,m; + + + + + +void Tarjan(int node){ + + + + id_n[node] = dist; + + inStack[node] = true; + + low_link[node] = dist; + + stk.push(node); + + dist ++; + + + + for (int i = 0 ; i < edge[node].size(); i++) + + { + + if (id_n[edge[node][i]] == -1) + + { + + + + Tarjan(edge[node][i]); + + + + low_link[node] = min(low_link[node], + + low_link[edge[node][i]]); + + } + + else if (inStack[edge[node][i]]) + + { + + int aux = edge[node][i]; + + low_link[node] = min (low_link[node], low_link[aux]); + + } + + } + + if(id_n[node] == low_link[node]) + + { + + conex.clear(); + + int aux; + + do + + { + + aux = stk.top(); + + conex.push_back(aux); + + stk.pop(); + + inStack[aux] = 0; + + } + + while (node != aux); + + ctc.push_back(conex); + + } + + + +} + + + +int main() + +{ + + ifstream fin("ctc.in"); + + ofstream fout("ctc.out"); + + + + int x, y; + + fin >> n >> m ; + + + + for (int i = 1; i <= m; i++) + + { + + fin >> x >> y; + + edge[x].push_back(y); + + } + + + + for (int i = 1; i<= n; i++) + + { + + inStack[i] = 0; + + id_n[i] = -1; + + } + + + + for (int i = 1; i <= n; i++) + + if (id_n[i] == -1) + + Tarjan(i); + + + + + + fout << ctc.size(); + + for (int i = 0 ; i < ctc.size(); i++) + + { + + fout << "\n"; + + for (int j = 0; j < ctc[i].size(); j++) + + fout << ctc[i][j] <<" "; + + } + + + + + + + + return 0; + +} diff --git a/scc/tarjan/f7ca6575-a15f-486a-99c6-2dee50705d5a.cpp b/scc/tarjan/f7ca6575-a15f-486a-99c6-2dee50705d5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6866aee0e7699fe48fdfa61ee7e9ad23c97be748 --- /dev/null +++ b/scc/tarjan/f7ca6575-a15f-486a-99c6-2dee50705d5a.cpp @@ -0,0 +1,79 @@ +#include + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +const int NMAX = 100000, UNVISITED = -1; +vector lst_ad[NMAX]; +stack st; +int n, m, ids[NMAX], lowLink[NMAX], id = 0, nr_ctc = 0; +bool onStack[NMAX]; +string ctc = ""; + +void dfs(int curr) +{ + ids[curr] = lowLink[curr] = id++; + st.push(curr); + onStack[curr] = true; + + for (auto next : lst_ad[curr]) + { + if (ids[next] == UNVISITED) + { + dfs(next); + } + + + if (onStack[next]) + { + lowLink[curr] = min(lowLink[curr], lowLink[next]); + } + } + + + if (ids[curr] == lowLink[curr]) + { + nr_ctc++; + int node; + do + { + node = st.top(); + st.pop(); + onStack[node] = false; + lowLink[node] = lowLink[curr]; + ctc += to_string(node + 1); + ctc += " "; + } while (node != curr); + ctc += "\n"; + } +} + +int main() +{ + in >> n >> m; + for (int i = 0; i < n; i++) + { + ids[i] = UNVISITED; + } + for (int i = 0; i < m; i++) + { + int x, y; + in >> x >> y; + x--; + y--; + lst_ad[x].push_back(y); + } + + for (int i = 0; i < n; i++) + { + if (ids[i] == UNVISITED) + { + dfs(i); + } + } + out << nr_ctc << '\n'; + out << ctc; + return 0; +} diff --git a/scc/tarjan/f89ce517-eee1-40d3-bc0c-85f1a9fbf116.cpp b/scc/tarjan/f89ce517-eee1-40d3-bc0c-85f1a9fbf116.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31fcf84bf0256953f352e12487a4bf5fcf9c5a37 --- /dev/null +++ b/scc/tarjan/f89ce517-eee1-40d3-bc0c-85f1a9fbf116.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +vector>G; + +vector>CC; + +vectorC; + +stackS; + +int L[100001],niv[100001]; + +bool InS[100001]; + +int n,m,nv; + +void Citire() + +{ + + fin>>n>>m; + + G.resize(n+1); + + for(int i=1;i<=m;i++) + + { + + int x,y; + + fin>>x>>y; + + G[x].push_back(y); + + } + +} + +void Afisare() + +{ + + fout< +#include +#include +#include +using namespace std; + +int N, M, i, j, V, VP; + +pair P[100001]; + +vector > Graph(100001); + +vector > Sol; vector Empty; + +stack S; + +bool Check[100001], SCheck[100001]; + +void DFS(int i); + +int main() + +{ + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + scanf("%d%d", &N, &M); + + Sol.push_back(Empty); + + for(i=1; i<=N; ++i) Graph[i].push_back(0); + + for(i=1; i<=M; ++i){ + + int x, y; + + scanf("%d%d", &x, &y); + + Graph[x].push_back(y); + + ++Graph[x][0]; + + } + + for(i=1; i<=N; ++i) + + if(Check[i]==false) DFS(i); + + printf("%d\n", V); + + for(i=1; i<=V; ++i){ + + for(j=0; j +#include +#include +#include + + +using namespace std; +vector adj[100000],con,idx,lowlink,in_stack; + +vector > G; + +stack S; + +int n,m,x,y,indecs; + +void citire() + + { + + ifstream fin ("ctc.in"); + + fin>>n>>m; + + for (int i=1;i<=m;i++) + + { + + fin>>x>>y; + + adj[x-1].push_back(y-1); + + } + + fin.close(); + + } + +void afisare() + + { + + ofstream fout ("ctc.out"); + + int l1=G.size(); + + fout<::const_iterator it; + + for (it = adj[n].begin();it!=adj[n].end();it++) + + { + + if (idx[*it]==-1) + + { + + tarjan(*it); + + lowlink[n]=((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + else if (in_stack[*it]) + + lowlink[n]=((lowlink[n]) < (lowlink[*it]) ? (lowlink[n]) : (lowlink[*it])); + + } + + if (idx[n] == lowlink[n]) + + { + + con.clear(); + + int node; + + do { + + node=S.top(); + + con.push_back(node); + + S.pop(); + + in_stack[node]=0; + + } + + while (node != n); + + G.push_back(con); + + } + + } + +int main () + + { + + citire(); + + idx.resize(n); + + lowlink.resize(n); + + in_stack.resize(n); + + idx.assign(n,-1); + + in_stack.assign(n,0); + + for (int i=0;i + +using namespace std; + +ifstream in("ctc.in"); +ofstream out("ctc.out"); + +const int NMAX = 100000, UNVISITED = -1; +vector lst_ad[NMAX]; +stack st; +int n, m, ids[NMAX], lowLink[NMAX], id = 0, nr_ctc = 0; +bool onStack[NMAX]; +string ctc = ""; + +void dfs(int curr) +{ + ids[curr] = lowLink[curr] = id++; + st.push(curr); + onStack[curr] = true; + + for (auto next : lst_ad[curr]) + { + if (ids[next] == UNVISITED) + { + dfs(next); + } + + + if (onStack[next]) + { + lowLink[curr] = min(lowLink[curr], lowLink[next]); + } + } + + + if (ids[curr] == lowLink[curr]) + { + nr_ctc++; + int node; + do + { + node = st.top(); + st.pop(); + onStack[node] = false; + lowLink[node] = lowLink[curr]; + ctc += to_string(node + 1); + ctc += " "; + } while (node != curr); + ctc += "\n"; + } +} + +int main() +{ + in >> n >> m; + for (int i = 0; i < n; i++) + { + ids[i] = UNVISITED; + } + for (int i = 0; i < m; i++) + { + int x, y; + in >> x >> y; + x--; + y--; + lst_ad[x].push_back(y); + } + + for (int i = 0; i < n; i++) + { + if (ids[i] == UNVISITED) + { + dfs(i); + } + } + out << nr_ctc << '\n'; + out << ctc; + return 0; +} diff --git a/scc/tarjan/fa55063e-b1ea-4f01-bcf6-27b9eb787b2b.cpp b/scc/tarjan/fa55063e-b1ea-4f01-bcf6-27b9eb787b2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9958247f2e8028f83ac7d7f73883d1ba6695c05 --- /dev/null +++ b/scc/tarjan/fa55063e-b1ea-4f01-bcf6-27b9eb787b2b.cpp @@ -0,0 +1,140 @@ +#include + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vector v[100100],sol[100100]; + +stack s; + +int indice,nr,inStiva[100100],nivel[100100],low[100100]; + + + +void Tarjan(int nod) + +{ + + indice++; + + nivel[nod]=indice; + + low[nod]=min(low[nod],nivel[nod]); + + s.push(nod); + + inStiva[nod]=1; + + + + for(int i=0;i>n>>m; + + for(i=1;i<=m;i++) + + { + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(i=1;i<=n;i++)low[i]=INT_MAX; + + for(i=1;i<=n;i++)if(nivel[i]==0)nivel[i]=1,Tarjan(i); + + + + g< + + +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,i,x,y,ctc,vecin,nod,nivel,niv[100010],low[100010]; + +bitset<100010> f,instack; + +vector L[100010],c[100010]; + +stack st; + +void dfs(int nod) { + + nivel++; + + f[nod]=1; + + low[nod]=niv[nod]=nivel; + + st.push(nod); + + instack[nod]=1; + + for (auto vecin:L[nod]) { + + if (f[vecin]==0) { + + dfs(vecin); + + low[nod]=min(low[nod],low[vecin]); + + } + + else + + if (instack[vecin]) + + low[nod]=min(low[nod],niv[vecin]); + + } + + if (low[nod]==niv[nod]) { + + ctc++; + + do { + + x=st.top(); + + st.pop(); + + instack[x]=0; + + c[ctc].push_back(x); + + } while (x!=nod); + + } + +} + +int main() { + + fin>>n>>m; + + for (i=1;i<=m;i++) { + + fin>>x>>y; + + L[x].push_back(y); + + } + + for (i=1;i<=n;i++) + + if (f[i]==0) + + dfs(i); + + fout< +#include +#include +#include + + + + +using namespace std; + + + +int n, m, i, j, k, start, ord[100003], levMin[100003]; + +vector vec[100003], comp[100003]; + +stack stiva; + +set s; + + + +void func(int nod) { + + int p, x; + + + + ord[nod] = start; + + levMin[nod] = start; + + start++; + + stiva.push(nod); + + s.insert(nod); + + + + for(p = 0; p < vec[nod].size(); p++) + + if(!ord[vec[nod][p]]) { + + func(vec[nod][p]); + + levMin[nod] = min(levMin[nod], levMin[vec[nod][p]]); + + } + + else if(s.find(vec[nod][p]) != s.end()) + + levMin[nod] = min(levMin[nod], ord[vec[nod][p]]); + + + + if(ord[nod] == levMin[nod]) + + while(!stiva.empty()) { + + x = stiva.top(); + + stiva.pop(); + + s.erase(x); + + comp[k].push_back(x); + + if(x == nod) { + + k++; + + break; + + } + + } + +} + + + +int main() { + + ifstream f("ctc.in"); + + ofstream g("ctc.out"); + + + + f >> n >> m; + + while(m) { + + f >> i >> j; + + + + vec[i].push_back(j); + + m--; + + } + + f.close(); + + + + k = 0; + + start = 1; + + for(i = 1; i <= n; i++) + + if(!ord[i]) + + func(i); + + g << k << '\n'; + + + + for(i = 0; i < k; i++, g << '\n') + + for(j = 0; j < comp[i].size(); j++) + + g << comp[i][j] << ' '; + + + + g.close(); + + + + return 0; + +} diff --git a/scc/tarjan/fb23a063-0271-47b9-97a5-eecf6f902183.cpp b/scc/tarjan/fb23a063-0271-47b9-97a5-eecf6f902183.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d50ca428413bd36e4193548387c0d79130b652f --- /dev/null +++ b/scc/tarjan/fb23a063-0271-47b9-97a5-eecf6f902183.cpp @@ -0,0 +1,121 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("ctc.in"); + +ofstream fout("ctc.out"); + +int n,m,k,x,y,NR; + +int nivel[100010],low[100010]; + +vector L[100010]; + +vector componente[100010]; + +stack stiva; + +bitset <100010> f; + + + +void dfs(int nod){ + + f[nod]=1; + + nivel[nod]=++k; + + low[nod]=k; + + stiva.push(nod); + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;i++){ + + fin>>x>>y; + + L[x].push_back(y); + + } + + for(int i=1;i<=n;i++){ + + if(nivel[i]==0){ + + dfs(i); + + } + + } + + fout< + + +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + +const int NMAX=1e5+10; + +vector < int > Edge[NMAX], Components[NMAX]; + +int n, m, RootLevel[NMAX], NodeLevel[NMAX], niv, nrCMP; + +bitset < NMAX > Seen, Belongs; + +stack < int > Stack; + + + +void Read() + +{ + + in>>n>>m; + + int x, y; + + for(int i=1; i<=m; i++) + + in>>x>>y, Edge[x].push_back(y); + +} + + + +void Tarjan(int x) + +{ + + niv++; + + Seen[x]=1; + + Belongs[x]=1; + + Stack.push(x); + + RootLevel[x]=niv; + + NodeLevel[x]=niv; + + for(int i : Edge[x]) + + { + + if(!Seen[i]) + + { + + Tarjan(i); + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + else + + if(Belongs[i]) + + RootLevel[x]=min(RootLevel[x], RootLevel[i]); + + } + + if(RootLevel[x]==NodeLevel[x]) + + { + + nrCMP++; + + int node=0; + + do + + { + + node=Stack.top(); + + Stack.pop(); + + Components[nrCMP].push_back(node); + + Belongs[node]=0; + + }while(node!=x); + + } + +} + + + +int main() + +{ + + ios_base::sync_with_stdio(false); + + in.tie(NULL); + + Read(); + + for(int i=1; i<=n; i++) + + if(!Seen[i]) + + niv=0, Tarjan(i); + + out< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("ctc.in"); + +ofstream out("ctc.out"); + + + + + + + +int index, scc; + +vector g[100005], ids, lowlink, temp; + +vector> sol; + +stack s; + +vector inStack; + +int n, m; + + + + + +void read() + +{ + + for (in >> n >> m; m--;) + + { + + int x, y; + + in >> x >> y; + + g[x].push_back(y); + + } + + ids.resize(n + 5), ids.assign(n + 5, -1); + + inStack.resize(n + 5), inStack.assign(n + 5, 0); + + lowlink.resize(n + 5); + +} + + + + + +void tarjan(int node) + +{ + + ids[node] = lowlink[node] = index++; + + s.push(node); + + inStack[node] = true; + + + + for (int it = 0; it < g[node].size(); it++) + + { + + int adjNode = g[node][it]; + + if (ids[adjNode] == -1) + + { + + tarjan(adjNode); + + lowlink[node] = min(lowlink[node], lowlink[adjNode]); + + } + + else if (inStack[adjNode]) + + lowlink[node] = min(lowlink[node], lowlink[adjNode]); + + } + + + + if (ids[node] == lowlink[node]) + + { + + temp.clear(); + + int nd; + + do + + { + + nd = s.top(); + + temp.push_back(nd); + + s.pop(); + + inStack[nd] = false; + + + + } while (nd != node); + + sol.push_back(temp); + + } + +} + + + +void print() + +{ + + out << sol.size() << '\n'; + + for (vector it : sol) + + { + + copy(it.begin(), it.end(), ostream_iterator(out, " ")); + + out << '\n'; + + } + +} + + + +void solve() + +{ + + read(); + + for (int i = 1; i <= n; i++) + + if (ids[i] == -1) + + tarjan(i); + + + + print(); + +} + + + +int main() + +{ + + solve(); + +} diff --git a/scc/tarjan/fe3816ef-7bf0-4b66-84ca-bef1869d0871.cpp b/scc/tarjan/fe3816ef-7bf0-4b66-84ca-bef1869d0871.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caae4a50d3fba304519fba18c0270a38304a0bbe --- /dev/null +++ b/scc/tarjan/fe3816ef-7bf0-4b66-84ca-bef1869d0871.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +using namespace std; + +int N, M, i, j, V, VP; + +pair P[100001]; + +vector > Graph(100001); + +vector > Sol; vector Empty; + +stack S; + +bool Check[100001], SCheck[100001]; + +void DFS(int i); + +int main() + +{ + + freopen("ctc.in", "r", stdin); + + freopen("ctc.out", "w", stdout); + + scanf("%d%d", &N, &M); + + Sol.push_back(Empty); + + for(i=1; i<=N; ++i) Graph[i].push_back(0); + + for(i=1; i<=M; ++i){ + + int x, y; + + scanf("%d%d", &x, &y); + + Graph[x].push_back(y); + + ++Graph[x][0]; + + } + + for(i=1; i<=N; ++i) + + if(Check[i]==false) DFS(i); + + printf("%d\n", V); + + for(i=1; i<=V; ++i){ + + for(j=0; j + + + + +using namespace std; + + + +ifstream f("ctc.in"); + +ofstream g("ctc.out"); + + + +vectorv[100001],sol[100001]; + +int n,m,k,t,low[100001],disc[100001]; + +bool fol[100001]; + +stacksk; + + + +void dfs(int nod) + +{ + + t++; + + sk.push(nod); + + fol[nod]=true; + + disc[nod]=low[nod]=t; + + for(int i=0;i>n>>m; + + for(int i=1;i<=m;++i) + + { + + int x,y; + + f>>x>>y; + + v[x].push_back(y); + + } + + + + for(int i=1;i<=n;++i) + + if(!disc[i]) + + { + + t = 0; + + dfs(i); + + } + + + + g< +#include + + +using namespace std; + + + +const int MAXN = 100001; + + + +vector g[MAXN], dfs_st; + +bool viz[MAXN]; + +bool onS[MAXN]; + +int min_back[MAXN]; + +vector > sol; + + + +FILE *in, *out; + + + +void get_ctc(int v) { + + vector rez = vector (); + + while(dfs_st.back() != v) { + + rez.push_back(dfs_st.back()); + + onS[dfs_st.back()] = false; + + dfs_st.pop_back(); + + } + + + + rez.push_back(v); + + dfs_st.pop_back(); + + onS[v] = false; + + sol.push_back(rez); + +} + + + +int tarj(int n) { + + + + + + viz[n] = true; + + min_back[n] = dfs_st.size(); + + int poz = min_back[n]; + + dfs_st.push_back(n); + + onS[n] = true; + + for(auto &it : g[n]) { + + if(!viz[it]) { + + int x = tarj(it); + + min_back[n] = min(min_back[n], x); + + } else { + + if(onS[it]) { + + min_back[n] = min(min_back[n], min_back[it]); + + } + + } + + } + + + + if(min_back[n] == poz) { + + get_ctc(n); + + } + + + + + + + + return min_back[n]; + +} +int main () { + + in = fopen("ctc.in", "r"); + + out = fopen("ctc.out", "w"); + + + + int n, m; + + fscanf(in, "%d %d", &n, &m); + + + + for(int i = 0; i < m; i++) { + + int x, y; + + fscanf(in, "%d %d", &x, &y); + + g[x].push_back(y); + + } + + + + fclose(in); + + + + for(int i = 1; i <= n; i++) { + + if(!viz[i]) { + + tarj(i); + + } + + } + + + + fprintf(out, "%d\n", sol.size()); + + for(auto &ctc : sol) { + + for(auto &elem : ctc) { + + fprintf(out, "%d ", elem); + + } + + fprintf(out, "\n"); + + } + + + + fclose(out); + + + + return 0; + +} diff --git a/ski/aib/6861abcc-137c-11eb-960d-309c234925a0.cpp b/ski/aib/6861abcc-137c-11eb-960d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fb571d128e4fd0de600e069a09721c660cce4e6 --- /dev/null +++ b/ski/aib/6861abcc-137c-11eb-960d-309c234925a0.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int N = 30005; + + + +int n; + +int arb[4 * N], loc[N], clasament[N]; + + + +void build(int left = 1, int right = n, int node = 1) + +{ + + if (left == right) + + { + + arb[node] = 1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + build(left, mid, node * 2); + + build(mid + 1, right, node * 2 + 1); + + arb[node] = arb[node * 2] + arb[node * 2 + 1]; + +} + + + +int GetPlace(int val, int left = 1, int right = n, int node = 1) + +{ + + arb[node]--; + + + + if (left == right) + + { + + return left; + + } + + + + int mid = (left + right) / 2; + + + + if (val <= arb[node * 2]) + + { + + return GetPlace(val, left, mid, node * 2); + + } + + else + + { + + return GetPlace(val - arb[node * 2], mid + 1, right, node * 2 + 1); + + } + +} + + + +int main() + +{ + + fin >> n; + + + + for (int i = 1; i <= n; i++) + + { + + fin >> loc[i]; + + } + + + + build(); + + + + for (int i = n; i >= 1; i--) + + { + + clasament[GetPlace(loc[i])] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + fout << clasament[i] << '\n'; + + } + +} diff --git a/ski/aib/68d9689e-137c-11eb-ae60-309c234925a0.cpp b/ski/aib/68d9689e-137c-11eb-ae60-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b38c064f23b57ca961e730b20acd64065d4fe12 --- /dev/null +++ b/ski/aib/68d9689e-137c-11eb-ae60-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + + + +int v[30005]; + +int aib[30005]; + +int r[30005]; + +int n; + + + +int lsb(int x) + +{ + + return x&-x; + +} + + + +int u(int poz , int val) + +{ + + while(poz <= n) + + { + + aib[poz] += val; + + poz += lsb(poz); + + } + +} + + + +int q(int poz) + +{ + + int sum = 0; + + while(poz) + + { + + sum += aib[poz]; + + poz -= lsb(poz); + + } + +} + + + +int main() + +{ + + int i , x , pas , poz; + + cin >> n; + + for(i = 1; i <= n; i++) + + u(i , 1); + + for(i = 1; i <= n; i++) + + { + + cin >> v[n - i + 1]; + + } + + for(i = 1; i <= n; i++) + + { + + x = v[i]; + + pas = 1<<16; + + poz = 0; + + while(pas) + + { + + if(poz + pas <= n) + + if(aib[poz + pas] < x) + + {x -= aib[poz + pas]; + + poz += pas;} + + pas>>=1; + + } + + poz++; + + r[poz] = n - i + 1; + + u(poz , -1); + + } + + for(i = 1; i <= n ; i++) + + cout << r[i] << '\n'; + + return 0; + +} diff --git a/ski/aib/69145d78-137c-11eb-8455-309c234925a0.cpp b/ski/aib/69145d78-137c-11eb-8455-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da6d2f50f5b6bc99a61085712ac09c19290958c7 --- /dev/null +++ b/ski/aib/69145d78-137c-11eb-8455-309c234925a0.cpp @@ -0,0 +1,70 @@ +#include +using namespace std; + +ifstream f("schi.in"); ofstream g("schi.out"); + + + +int aib[30001],v[30001],sol[30001],n; + +void update(int x) + +{ for(int i=x;i<=n;i+=(i&(-i))) aib[i]--; } + +int query(int poz) + +{ int T=0; + + for(int i=poz;i;i-=(i&(-i))) T+=aib[i]; + + return T; + +} + +int cautbin(int x) + +{ int S=1,D=n,T=0,Minim=INT_MAX; + + while(S<=D) + + { int mid=(S+D)/2; + + T=query(mid); + + if(T==x && mid=x) D=mid-1; + + else S=mid+1; + + } + + return Minim; + +} + +int main() + +{ f>>n; + + for(int i=1;i<=n;i++) f>>v[i]; + + for(int i=1;i<=n;i++) aib[i]=(i&(-i)); + + for(int x,i=n;i;--i) + + { x=cautbin(v[i]); + + sol[x]=i; + + update(x); + + } + + for(int i=1;i<=n;i++) g< + + + + + + +using namespace std; + + + + + + + +ifstream cin("schi.in"); + + + + + + + +ofstream cout("schi.out"); + + + + + + + +int tree[30000+5]; +int v[30000+5],poz[30000+5]; + + + +int n; + + + +int sum(int k) + + + +{ + + + + int s=0; + + + + while(k>=1) + + + + { + + + + s+=tree[k]; + + + + k-=k&-k; + + + + } + + + + return s; + + + +} + + + +int bs(int val) + + + +{ + + + + int l=1,r=n,med; + + + + while(lval) + + + + r=med-1; + + + + else l=med+1; + + + + } + + + + return l; + + + +} + + + +void add(int k,int x) + + + +{ + + + + while(k<=n) + + + + { + + + + tree[k]+=x; + + + + k+=k&-k; + + + + } + + + +} + + + +int main() + + + + + + + +{ + cin>>n; + + + + + + for(int i=1;i<=n;++i) + + + + + + + + cin>>v[i],add(i,1); + + + + + + + + for(int i=n;i>=1;--i) + + + + { + + + + int e=bs(v[i]); + + + + poz[e]=i; + + + + add(e,-1); + + + + } + + + + + + + + for(int i=1;i<=n;++i) + + + + + + + + cout< //Andrei Alexandru a.k.a Sho10 + + + +using namespace std; + +long long n,a[3000005],bit[3000005],ans[300005]; + +long long query(long long pos){ + +long long rs=0; + +for(;pos>=1;pos-=pos&(-pos)){ + + rs+=bit[pos]; + +} + +return rs; + +} + +void update(long long pos,long long val){ + +for(;pos<=n;pos+=pos&(-pos)){ + + bit[pos]+=val; + +} + +} + +int32_t main(){ + +ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);; + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + +cin>>n; + +for(long long i=1;i<=n;i++) + +{ + + cin>>a[i]; + + update(i,1); + +} + +for(long long i=n;i>=1;i--) + +{ + + long long x=a[i]; + + long long l=1,r=n; + + while(l<=r){ + + long long mid=(l+r)/2; + + if(query(mid)>=x){ + + r=mid-1; + + }else l=mid+1; + + } + + ans[l]=i; + + update(l,-1); + +} + +for(long long i=1;i<=n;i++) + +{ + + cout< + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int fn[30010],v[30010],rez[30010],n; + + + +void add(int k, int val) + +{ + + while(k<=n) + + { + + fn[k]+=val; + + k+=k&-k; + + } + +} + + + +int find(int val) + +{ + + int step,i; + + for (step = 1; step<=n; step<<=1); + + for (i = 0; step>0; step>>=1) + + if (i+step<=n && fn[i+step]>n; + + for(int i=1;i<=n;i++) + + { + + fin>>v[i]; + + add(i,1); + + } + + for (int i=n;i>=1;i--) + + { + + int poz=find(v[i]); + + rez[poz]=i; + + add(poz,-1); + + } + + for (int i = 1; i<=n; i++) + + fout << rez[i] << "\n"; + +} diff --git a/ski/aib/6a0f98da-137c-11eb-aa5e-309c234925a0.cpp b/ski/aib/6a0f98da-137c-11eb-aa5e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3687e53f3d35101eece422c365f926046c31eac --- /dev/null +++ b/ski/aib/6a0f98da-137c-11eb-aa5e-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + + + + + +int Aib[30007]; + +int n, Q, a[30007], Ans[30007]; + + + +void update(int poz, int val){ + + while(poz <= n){ + + Aib[poz] += val; + + poz += poz & (poz ^ (poz - 1)); + + } + +} + + + +int query(int poz){ + + int sol = 0; + + while(poz){ + + sol += Aib[poz]; + + poz -= poz & (poz ^ (poz - 1)); + + } + + return sol; + +} + + + +inline int cb(int Val){ + + int st = 1, dr = n; + + while(st <= dr){ + + int med = (st + dr) >> 1; + + if(Val <= query(med)) + + dr = med - 1; + + else + + st = med + 1; + + } + + return st; + +} + + + +int main(){ + + freopen("schi.in", "r", stdin); + + freopen("schi.out", "w", stdout); + + scanf("%d", &n); + + for(int i = 1; i <= n; ++i){ + + scanf("%d", &a[i]); + + update(i, 1); + + } + + for(int i = n; i >= 1; --i){ + + int poz = cb(a[i]); + + Ans[poz] = i; + + update(poz, -1); + + } + + for(int i = 1; i <= n; ++i) + + printf("%d\n", Ans[i]); + + return 0; + +} diff --git a/ski/aib/6a2e6b5a-137c-11eb-93b5-309c234925a0.cpp b/ski/aib/6a2e6b5a-137c-11eb-93b5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..29ee2ddac3f7106e0e8df5630972f9e1aba1522a --- /dev/null +++ b/ski/aib/6a2e6b5a-137c-11eb-93b5-309c234925a0.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, arbI[30005], v[30005], clasament[30005]; + + + +void Update(int pozitie, int element) + +{ + + while(pozitie <= n) + + { + + arbI[pozitie] += element; + + pozitie += (pozitie & (-pozitie)); + + } + +} + + + +int Suma(int pozitie) + +{ + + int s = 0; + + while(pozitie > 0) + + { + + s += arbI[pozitie]; + + pozitie -= (pozitie & (-pozitie)); + + } + + return s; + +} + + + +int CautBin(int pozitie, int element) + +{ + + int stanga, dreapta, mijloc, poz, suma = 0; + + stanga = 1; + + dreapta = pozitie; + + poz = -1; + + while(stanga <= dreapta) + + { + + mijloc = (stanga + dreapta) / 2; + + suma = Suma(mijloc); + + if(suma == element) + + { + + poz = mijloc; + + dreapta = mijloc - 1; + + } + + else if(suma > element) + + dreapta = mijloc - 1; + + else stanga = mijloc + 1; + + } + + return poz; + +} + + + +int main() + +{ + + fin>>n; + + for(int i = 1; i <= n; i++) + + { + + fin >> v[i]; + + Update(i, 1); + + } + + for(int i = n; i >= 1; i--) + + { + + int p = CautBin(n, v[i]); + + Update(p, -1); + + clasament[p] = i; + + } + + + + for(int i = 1; i <= n; i++) + + fout << clasament[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/6ae90c92-137c-11eb-9630-309c234925a0.cpp b/ski/aib/6ae90c92-137c-11eb-9630-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..913dc380222f480c043068027d1e61f5cc81a953 --- /dev/null +++ b/ski/aib/6ae90c92-137c-11eb-9630-309c234925a0.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +using ull = unsigned long long; +using ll = long long; +using namespace std; + +const int INF = INT_MAX, maxN = 30005, maxStep = (1 << 15); +ll t, n, aib[maxN], place[maxN], ans[maxN], step; + +bool viz[maxN]; + + +void update(ll i, ll val){ + + while (i <= n){ + + aib[i] += val; + + i += (-i & i); + + } + +} + + + +ll query(ll i){ + + ll ans = 0; + + while (i > 0){ + + ans += aib[i]; + + i -= (-i & i); + + } + + return ans; + +} + + +ll binarySearch(ll x){ + + step = maxStep; + + ll idx = 0; + + while (step){ + + if (idx + step <= n and aib[idx + step] <= x){ + + if (x - aib[idx + step] != 0 or viz[idx + step] != true){ + + x -= aib[idx + step]; + + idx += step; + + } + + } + + step >>= 1; + + } + + viz[idx] = true; + + return idx; + +} + + +int main(){ + ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); + ifstream cin("schi.in"); + ofstream cout("schi.out"); + + + cin >> n; + + for (int i = 1; i <= n; i++){ + + cin >> place[i]; + + update(i, 1); + + } + + for (int i = n; i >= 1; i--){ + + ll num = binarySearch(place[i]); + + ans[num] = i; + + update(num, -1); + + } + + for (int i = 1; i <= n; i++) + + cout << ans[i] << "\n"; + + return 0; +} diff --git a/ski/aib/6af7d692-137c-11eb-861f-309c234925a0.cpp b/ski/aib/6af7d692-137c-11eb-861f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0da9d1f00ffc33063699655d948e63c5951101e8 --- /dev/null +++ b/ski/aib/6af7d692-137c-11eb-861f-309c234925a0.cpp @@ -0,0 +1,118 @@ +#include +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int nmax=30005; + + + +int n,v[nmax],aib[nmax],sol[nmax] ; + + + +void update(int poz,int val) + +{ + + for(;poz<=n;poz+= poz & -poz) + + aib[poz]+=val; + +} + + + +void read() + +{ + + fin>>n; + + + + for(int i=1;i<=n;i++) + + { + + fin>>v[i]; + + + + update(i,1); + + } + +} + + + +void solve() + +{ + + for(int i=n;i>=1;i--) + + { + + int x=v[i],idx=0,p=1<<20; + for(;p>0;p/=2) + + if(idx+p<=n) + + if(aib[idx+p] +using namespace std; + +ifstream ci("schi.in"); + +ofstream cou("schi.out"); + + + +int aib[30005]; + +int i,j,v[30005],n,nr,sum,fin[30005]; + + + +void Update(int p,int x) + +{ + + while(p<=n) + + { + + aib[p]+=x; + + p=p+(p&(-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s=0; + + while(p>0) + + { + + s=s+aib[p]; + + p=p-(p & (-p)); + + } + + return s; + +} + + + + + +int main() + +{ + + + + ci>>n; + + for(i=1;i<=n;i++){ + + ci>>v[i]; + + } + + for(i=1;i<=n;i++){ + + Update(i,1); + + } + + int st,dr,mij,sol,sm; + + for(i=n;i>=1;i--){ + + nr=v[i]; + + st=1; + + dr=n; + + sol=1; + + while(st<=dr){ + + mij=(st+dr)/2; + + sm=Suma(mij); + + + + if(sm + + +using namespace std; + + + +ifstream cin("schi.in"); + + + +ofstream cout("schi.out"); + + + +int tree[30000+5]; + + + + + +int v[30000+5],poz[30000+5]; + +int n; + +int sum(int k) + +{ + + int s=0; + + while(k>=1) + + { + + s+=tree[k]; + + k-=k&-k; + + } + + return s; + +} + +int bs(int val) + +{ + + int l=1,r=n,med; + + while(lval) + + r=med-1; + + else l=med+1; + + } + + return l; + +} + +void add(int k,int x) + +{ + + while(k<=n) + + { + + tree[k]+=x; + + k+=k&-k; + + } + +} + +int main() + + + +{ + + + + + + cin>>n; + + + + for(int i=1;i<=n;++i) + + + + cin>>v[i],add(i,1); + + + + for(int i=n;i>=1;--i) + + { + + int e=bs(v[i]); + + poz[e]=i; + + add(e,-1); + + } + + + + for(int i=1;i<=n;++i) + + + + cout< + + + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N, C[30005], aib[30005], place[30005]; + + + +void Add(int x, int val){ + + for(int i = x; i <= N; i+=(i&(-i))) + + aib[i]+=val; + +} + + + +int Sum(int x){ + + int s = 0; + + for(int i = x; i > 0; i-=(i&(-i))) + + s+=aib[i]; + + return s; + +} + + + +int Search(int l, int r, int val){ + + if(l > r) + + return N+1; + + int m = l + (r-l)/2; + + int p = Sum(m); + + if(p == val) return min(m, Search(l, m-1, val)); + + else if(p > val) return Search(l, m-1, val); + + else return Search(m+1, r, val); + +} + + + +void Read(){ + + fin>>N; + + for(int i = 1; i <= N; i++){ + + fin>>C[i]; + + Add(i, 1); + + } + +} + + + +void Solve(){ + + for(int i = N; i > 0; i--){ + + int a = Search(1, N, C[i]); + + place[a] = i; + + Add(a, -1); + + } + + for(int i = 1; i <= N; i++) + + fout< + + + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int aib[30005], v[30005], sol[30005]; + +int n,m; + + + +void add(int val,int poz){ + + int i; + + for(i = poz; i <= n; i += (i&-i)){ + + aib[i] += val; + + } + +} + + + +int sum(int poz){ + + int i,sol = 0; + + for(i = poz; i >= 1; i -= (i&-i)){ + + sol += aib[i]; + + } + + return sol; + +} + + + +int sumsrc(int val){ + + int st = 1,dr = n,mid,midsum,sol = 900000005; + + while(st <= dr){ + + mid = (st+dr)/2; + + midsum = sum(mid); + + if(val < midsum){ + + dr = mid-1; + + }else if(val == midsum){ + + sol = min(sol,mid); + + dr = mid-1; + + }else if(midsum < val){ + + st = mid+1; + + } + + } + + return sol; + +} + + + +int main() + +{ + + int i,sumpoz; + + fin>>n; + + for(i = 1; i <= n; i++){ + + fin>>v[i]; + + add(1,i); + + } + + for(i = n; i >= 1; i--){ + + sumpoz = sumsrc(v[i]); + + sol[sumpoz] = i; + + add(-1,sumpoz); + + } + + for(i = 1; i <= n; i++){ + + fout< +#include + + +using namespace std; + + + +int v[30001], aib[30001], sol[30001], n; + + + +int len(int x) { + + return (x-(x&(x-1))); + +} + + + +void update(int poz, int val) { + + while(poz<=n) { + + aib[poz]+=val; + + poz+=len(poz); + + } + +} + + + +int query(int x) { + + int pas, rez; + + pas=(1<<30); + + rez=0; + + while(pas>0) { + + if(rez+pas<=n && aib[rez+pas]0;i--) { + + poz=query(v[i]); + + sol[poz]=i; + + update(poz,-1); + + } + + for(i=1;i<=n;i++) + + fprintf(fout, "%d\n",sol[i]); + + return 0; + +} diff --git a/ski/aib/719cf9a2-137c-11eb-baec-309c234925a0.cpp b/ski/aib/719cf9a2-137c-11eb-baec-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49f3aef42dfdd28f2153f03286d7d734c6b4ed6e --- /dev/null +++ b/ski/aib/719cf9a2-137c-11eb-baec-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +const int NMAX = 30002; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N, v[NMAX], aib[NMAX]; + +int S[NMAX]; + + + +void Update( int pos, int val ) + +{ + + while( pos <= N ) + + { + + aib[pos] += val; + + pos += ( pos & -pos ); + + } + +} + +int Query( int pos ) + +{ + + int sum = 0; + + while( pos ) + + { + + sum += aib[pos]; + + pos -= ( pos & -pos ); + + } + + return sum; + +} + +int BS( int lf, int rg, int val ) + +{ + + if( lf > rg ) return ( 1 << 30 ); + + + + int mid = lf + ( rg - lf )/2; + + + + int qmid = Query( mid ); + + + + if( qmid >= val ) return min( mid, BS( lf, mid-1, val ) ); + + else return BS( mid+1, rg, val ); + +} + +void Solve() + +{ + + fin >> N; + + + + for( int i = 1; i <= N; ++i ) + + { + + fin >> v[i]; + + Update( i, 1 ); + + } + + + + for( int i = N; i > 0; --i ) + + { + + int p = BS( 1, N, v[i] ); + + + + S[p] = i; + + Update( p, -1 ); + + } + + + + for( int i = 1; i <= N; ++i ) + + fout << S[i] << '\n'; + +} + +int main() + +{ + + Solve(); + + return 0; + +} diff --git a/ski/aib/71aaf1ba-137c-11eb-b9f1-309c234925a0.cpp b/ski/aib/71aaf1ba-137c-11eb-b9f1-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f145504ed6e8f27345f0b0ec9a429d1bbb59c539 --- /dev/null +++ b/ski/aib/71aaf1ba-137c-11eb-b9f1-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +int n,aib[30005],v[30005]; + +int len(int x) + +{ + + return (x-(x&(x-1))); + +} + +int update(int pz,int val) + +{ + + while (pz<=n) + + { + + aib[pz]+=val; + + pz+=len(pz); + + } + +} + +int sol[30005]; + +int main() + +{ + + int i,x,pas,rz; + + cin>>n; + + for (i=1;i<=n;i++) + + { + + cin>>v[i]; + + update(i,1); + + } + + for (i=n;i>=1;i--) + + { + + x=v[i]; + + pas=1<<20;rz=0; + + while (pas) + + { + + if (rz+pas<=n&&aib[rz+pas] +#include + + +using namespace std; + + + +const int NMAX = 100005; + +int sol[NMAX], s[NMAX], v[NMAX]; + +int n; + + + +int len(int x) { + + return (x - (x & (x - 1))); + +} + + + +void update(int pz, int val) { + + while (pz <= n) { + + s[pz] += val; + + pz += len(pz); + + } + +} + + + +int main() { + + + + freopen ("schi.in", "r", stdin); + + freopen ("schi.out", "w", stdout); + + + + int i, rz, pas, x; + + + + scanf ("%d", &n); + + for (i = 1; i <= n; i++) { + + scanf ("%d", &v[i]); + + update(i, 1); + + } + + + + for (i = n; i >= 1; i--) { + + x = v[i]; + + pas = (1 << 20); + + rz = 0; + + while (pas > 0) { + + if (rz + pas <= n && s[rz + pas] < x) { + + rz += pas; + + x -= s[rz]; + + } + + pas /= 2; + + } + + rz++; + + sol[rz] = i; + + update(rz, -1); + + } + + + + for (i = 1; i <= n; i++) + + printf ("%d\n", sol[i]); + + + + return 0; + +} diff --git a/ski/aib/72077aee-137c-11eb-a53d-309c234925a0.cpp b/ski/aib/72077aee-137c-11eb-a53d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aaa2b31de6e9653d4585cb06e057d6761d51f1f6 --- /dev/null +++ b/ski/aib/72077aee-137c-11eb-a53d-309c234925a0.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream fin ("schi.in"); + +ofstream fout ("schi.out"); + +struct ura + +{ + + int nr,a,sol; + +}; + +ura v[30001]; + +int len (int x) + +{ + + return x-(x&(x-1)); + +} + +int main () + +{ + + int n,i,cnr,r,p; + + fin>>n; + + for (i=1;i<=n;i++) + + { + + fin>>v[i].nr; + + v[i].a=len(i); + + } + + for (i=n;i>=1;i--) + + { + + cnr=v[i].nr; + + r=0; + + p=1<<15; + + while (p!=0) + + { + + if (p+r<=n && v[p+r].a +#include + + +using namespace std; + +int n; + +int aib[30001], v[30001], sol[30001]; + + + +int len(int r) { + + return r-(r&(r-1)); + +} + +void solve(int pz, int val) { + + while(pz<=n) { + + aib[pz]+=val; + + pz=pz+len(pz); + + } + +} + + + +int main() { + + int i, x, pas, rz; + + + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + fin>>n; + + for(i=1;i<=n;i++) { + + fin>>v[i]; + + solve(i, 1); + + } + + for(i=n;i>=1;i--) { + + x=v[i]; + + pas=1<<20; + + rz=0; + + while(pas>0) { + + if(pas+rz<=n && aib[pas+rz] +#include +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int AIB[30008]; + +int v[30009]; + +int place[30009]; + +int n; + + + +void Read() + +{ + + int i,j; + + fin>>n; + + for(i=1; i<=n; ++i) + + fin>>v[i]; + +} + + + +void Update(int pos, int val) + +{ + + while(pos <= n) + + { + + AIB[pos] += val; + + pos += pos &(-pos); + + } + +} + + + +int Query(int pos) + +{ + + int sum = 0; + + while( pos ) + + { + + sum += AIB[pos]; + + pos -= pos&(-pos); + + } + + return sum; + +} + + + +int BinSearch(int lf ,int rg, int val) + +{ + + if(lf > rg) + + return n+1; + + int mid = lf + (rg-lf)/2; + + int ans = Query(mid); + + if(ans == val) + + return min(mid, BinSearch(lf, mid-1, val)); + + if(ans < val) + + return BinSearch(mid+1,rg, val); + + else return BinSearch(lf, mid-1, val); + +} + + + + + + + +void Do() + +{ + + int i,j,ans; + + for(i = 1; i<=n; ++i) + + Update(i, 1); + + for(i = n; i>=1; --i) + + { + + int ans = BinSearch(1,n, v[i]); + + place[ans] = i; + + Update(ans, -1); + + } + + for(i=1; i<=n; ++i) + + fout< + + +using namespace std; + + + +ifstream fin( "schi.in" ); + +ofstream fout( "schi.out" ); + + + +const int NMAX = 30005; + + + +int N; + +int t[NMAX]; + +int a[NMAX]; + +int place[NMAX]; + + + +void Update( int idx, int v ) { + + while( idx <= N ) { + + t[idx] += v; + + idx += idx & (-idx ); + + } + +} + + + +int Query( int idx ) { + + int ans = 0; + + while( idx ) { + + ans += t[idx]; + + idx -= idx & ( -idx ); + + } + + return ans; + +} + + + +void Read() + +{ + + fin >> N; + + for( int i = 1; i <= N; ++i ) + + fin >> a[i]; + +} + + + +int BinSearch( int lf, int rg, int val ) { + + if( lf > rg ) return N + 1; + + + + int mid = lf + ( rg - lf ) / 2; + + int ans = Query( mid ); + + + + if( ans == val ) return min( mid, BinSearch( lf, mid - 1, val ) ); + + + + if( ans < val ) return BinSearch( mid + 1, rg, val ); + + else return BinSearch( lf, mid - 1, val ); + +} + + + +void Do() + +{ + + for( int i = 1; i <= N; ++i ) + + Update( i, 1 ); + + + + for( int i = N; i > 0; --i ) { + + int loc = BinSearch( 1, N, a[i] ); + + + + place[loc] = i; + + Update( loc, -1 ); + + } + + for( int i = 1; i <= N; ++i ) + + fout << place[i] << '\n'; + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/ski/aib/72869a68-137c-11eb-b2c5-309c234925a0.cpp b/ski/aib/72869a68-137c-11eb-b2c5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2a3ab66207b2900e0278f9f5a139a85173d7132 --- /dev/null +++ b/ski/aib/72869a68-137c-11eb-b2c5-309c234925a0.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +int aib[30005],v[30005],sol[30005],n; + +int len(int x){ + + return x-(x&(x-1)); + +}; + + + +void update(int poz,int val){ + + while(poz<=n){ + + aib[poz]+=val; + + poz+=len(poz); + + } + +}; + + + +int main() + +{ + + ifstream cin("schi.in"); + + ofstream cout("schi.out"); + + int i,x,poz,rez; + + cin>>n; + + for(i=1;i<=n;i++){ + + cin>>v[i]; + + update(i,1); + + } + + for(i=n;i>=1;i--){ + + x=v[i]; + + rez=0; + + poz=1<<20; + + while(poz>0){ + + if(rez+poz<=n && aib[rez+poz] +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,aib[30003],sol[30003],v[30003]; + +int suma(int poz) + +{ + + int S=0; + + while (poz>0) + + { + + S+=aib[poz]; + + poz-=(poz&(-poz)); + + } + + return S; + +} + +void update(int poz) + +{ + + while (poz<=n) + + { + + aib[poz]-=1; + + poz+=(poz&(-poz)); + + } + +} + +int cautare (int p,int u,int val) + +{ + + int min1=30003 +3; + + while (p<=u) + + { + + int m=(p+u)/2; + + int S=suma(m); + + if (S==val && m>n; + + for (i=1;i<=n;i++) + + { + + fin>>v[i]; + + aib[i]=(i&(-i)); + + } + + for (i=n;i>=1;i--) + + { + + int val=cautare(1,n,v[i]); + + update(val); + + sol[val]=i; + + } + + for (i=1;i<=n;i++) + + fout< + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,v[30005],i,poz; + +short aib[30005],sol[30005]; + +inline int query(int poz){ + + int r=0; + + for(;poz;poz-=poz&-poz) + + r+=aib[poz]; + + return r; + +} + +inline void update(int poz,int val){ + +for(;poz<=n;poz+=poz&-poz) + + aib[poz]+=val; + +} + +inline int solve(int x){ + +int st=1,dr=n,mid; + +while(st<=dr){ + + mid=(st+dr)/2; + + if(query(mid)>=x) + + dr=mid-1; + + else st=mid+1; + +} + +return st; + +} + +int main(){ + + + +fin>>n; + +for(i=1;i<=n;i++){ + + fin>>v[i]; + + update(i,1); + +} + +for(i=n;i>=1;i--){ + + poz=solve(v[i]); + + sol[poz]=i; + + update(poz,-1); + +} + +for(i=1;i<=n;i++) + + fout< +#include + + +using namespace std; + + + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + + + +int aib[30005]; + +int v[30005]; + +int r[30005]; + + + +int n; + + + +int len(int x) + +{ + + return x & -x; + +} + + + +int q(int poz) + +{ + + int sum = 0; + + while(poz) + + { + + sum += aib[poz]; + + poz -= len(poz); + + } + + return sum; + +} + + + +void u(int poz , int val) + +{ + + while(poz <= n) + + { + + aib[poz] += val; + + poz += len(poz); + + } + +} + + + +int main() + +{ + + int i , mij , ac , pas; + + + + cin >> n; + + + + for(i = 1; i <= n; i++) + + u(i , 1); + + for(i = 1; i <= n; i++) + + cin >> v[i]; + + reverse (v + 1, v + n + 1); + + for(i = 1; i <= n; i++) + + { + + mij = v[i]; + + ac = 0; + + pas = 1 << 20; + + while(pas) + + { + + if(ac + pas <= n) + + if(aib[ac + pas] < mij) + + mij -= aib[ac + pas] , ac += pas; + + pas >>= 1; + + } + + ac++; + + r[ac] = n - i + 1; + + u(ac , -1); + + } + + for(i = 1; i <= n; i++) + + cout << r[i] <<'\n'; + + + + return 0; + +} diff --git a/ski/aib/7305586c-137c-11eb-a730-309c234925a0.cpp b/ski/aib/7305586c-137c-11eb-a730-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f075ad1b78d81a076e9bf629b01efe0e8050a05e --- /dev/null +++ b/ski/aib/7305586c-137c-11eb-a730-309c234925a0.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +const int N=30005; + +int aib[N]; + +void update(int poz,int val){ + + for(int i=poz;i0;i-=i&(-i)){ + + ans+=aib[i]; + + } + + return ans; + +} + +int v[N]; + +int ans[N]; + +int main() + +{ + + FILE*fin,*fout; + + fin=fopen("schi.in","r"); + + fout=fopen("schi.out","w"); + + int n; + + fscanf(fin,"%d",&n); + + for(int i=1;i<=n;i++){ + + fscanf(fin,"%d",&v[i]); + + update(i,1); + + } + + for(int i=n;i>=1;i--){ + + int pas=0,p2=1<<15; + + int rnk=v[i]; + + while(p2){ + + if(pas+p2<=n && aib[pas+p2] + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,v[30005],i,poz; + +short aib[30005],sol[30005]; + +inline int query(int poz){ + + int r=0; + + for(;poz;poz-=poz&-poz) + + r+=aib[poz]; + + return r; + +} + +inline void update(int poz,int val){ + +for(;poz<=n;poz+=poz&-poz) + + aib[poz]+=val; + +} + +inline int solve(int x){ + +int st=1,dr=n,mid; + +while(st<=dr){ + + mid=(st+dr)/2; + + if(query(mid)>=x) + + dr=mid-1; + + else st=mid+1; + +} + +return st; + +} + +int main(){ + +ios_base::sync_with_stdio(false); + +fin>>n; + +for(i=1;i<=n;i++){ + + fin>>v[i]; + + update(i,1); + +} + +for(i=n;i>=1;i--){ + + poz=solve(v[i]); + + sol[poz]=i; + + update(poz,-1); + +} + +for(i=1;i<=n;i++) + + fout< + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +int n,a[30001]; + +int tree[30001]; + +int sol[30001]; + + + +void adauga(int poz,int val) + +{ + + while(poz<=n) + + { + + tree[poz]+=val; + + poz+=poz&-poz; + + } + +} + + + +int suma(int poz) + +{ + + int s=0; + + + + while(poz) + + { + + s+=tree[poz]; + + poz-=poz&-poz; + + } + + + + return s; + +} + + + +int main() + +{ + + in>>n; + + + + for(int i=1;i<=n;i++) + + in>>a[i]; + + + + for(int i=1;i<=n;i++) + + adauga(i,1); + + + + for(int i=n;i>=1;i--) + + { + + int poz=0; + + + + for(int pas=n;pas;pas/=2) + + while(poz+pas<=n && suma(poz+pas) +#include +#include +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int NMax = 30000; + + + +int N,V[NMax+5] ,AIB[NMax+5], Sol[NMax+5], sol = 1e9; + + + +void Init(int Pos) { + + for (int i = Pos; i <= N; i += i&(-i)) + + AIB[i]++; + +} + + + +void Update(int Pos) { + + for (int i = Pos; i <= N; i += i&(-i)) + + AIB[i]--; + +} + + + +int Query(int Pos) { + + int Sum = 0; + + for (int i = Pos; i > 0; i -= i&(-i)) + + Sum += AIB[i]; + + return Sum; + +} + +void BinarySearch(int x, int low, int hi) { + + int m; + + if(low<=hi) { + + m=low + (hi-low)/2; + + int val = Query(m); + + if(val == x) + + if(m val) BinarySearch(x,m+1,hi); + + else BinarySearch(x,low,m-1); + + } + +} + + + +void Solve() { + + for (int i = N; i >= 1; i--) { + + BinarySearch(V[i],1,N); + + Update(sol); + + Sol[sol] = i; + + sol = 1e9; + + } + +} + + + +int main() { + + in >> N; + + for(int i = 1; i <= N; i++) + + in >> V[i], Init(i); + + Solve(); + + + + for (int i = 1; i <= N; i++) + + out << Sol[i] << '\n'; + +} diff --git a/ski/aib/73a34f48-137c-11eb-b7e9-309c234925a0.cpp b/ski/aib/73a34f48-137c-11eb-b7e9-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..443f08c22912df78cf5f14fe1948fc01c669c56d --- /dev/null +++ b/ski/aib/73a34f48-137c-11eb-b7e9-309c234925a0.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +using namespace std; + + + + +string name = "schi"; + + +ifstream fin(name + ".in"); +ofstream fout(name + ".out"); + +struct Event { + int p; + int v; +}; + +vector q; + + +int aib[30005]; +int n; +int result[30005]; + +int nrz(int x) { + return x & (~x + 1); +} + +void update(int x, int v) { + while (x <= n ) { + aib[x] += v; + x += nrz(x); + } +} + +int query(int x) { + int result = 0; + + while (x > 0) { + result += aib[x]; + x -= nrz(x); + } + + return result; +} + +int binSearch(int low, int high, int target) { + if (low > high) { + return -1; + } + + int mid = (low + high) / 2; + + int q = query(mid); + + if (q == target && (mid == low || query(mid - 1) != target)) { + return mid; + } + + if (q >= target) { + return binSearch(low, mid - 1, target); + } + + return binSearch(mid + 1, high, target); +} + +int main() { + + fin >> n; + + for (int i = 1; i <= n; ++i) { + int x; + fin >> x; + q.push_back({x, i}); + } + + reverse(q.begin(), q.end()); + + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + + for (auto x: q) { + int ndx = binSearch(1, n, x.p); + update(ndx, -1); + result[ndx] = x.v; + } + + for (int i = 1; i <= n; ++i) { + fout << result[i] << "\n"; + } + + return 0; +} diff --git a/ski/aib/73b3f5d4-137c-11eb-8136-309c234925a0.cpp b/ski/aib/73b3f5d4-137c-11eb-8136-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7891f966532e9d80b01494c785c6cc4da77debf1 --- /dev/null +++ b/ski/aib/73b3f5d4-137c-11eb-8136-309c234925a0.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i,v[30010],sol[30010],A[30010],poz; + +void update(int poz,int val) { + + for (;poz<=n;poz+=(poz&-poz)) + + A[poz]+=val; + +} + +int query(int poz) { + + int val=0; + + for (;poz;poz-=(poz&-poz)) + + val+=A[poz]; + + return val; + +} + +int cb(int val) { + + int st=1,dr=n,sol=n+1; + + while (st<=dr) { + + int mid=(st+dr)/2; + + if (val<=query(mid)) + + dr=mid-1; + + else + + st=mid+1; + + } + + return st; + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) { + + fin>>v[i]; + + update(i,1); + + } + + for (i=n;i>=1;i--) { + + poz=cb(v[i]); + + sol[poz]=i; + + update(poz,-1); + + } + + for (i=1;i<=n;i++) + + fout< +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n,i,v[30001],aib[30001],sol[30001],st,dr,mid,aux,r; + + + +void upd(int p, int x){ + + for(;p<=n;p+=(p&-p)) + + aib[p]+=x; + +} + + + +int qry(int p){ + + int s=0; + + + + for(;p;p-=(p&-p)) + + s+=aib[p]; + + + + return s; + +} + + + +int cb(int x){ + + st=1; dr=n; + + r=1000000000; + + + + while(st<=dr){ + + mid=(st+dr)/2; + + aux=qry(mid); + + + + if(x<=aux) + + dr=mid-1; + + else + + st=mid+1; + + } + + + + return st; + +} + + + + + +int main(){ + + fin>>n; + + + + for(i=1;i<=n;i++){ + + fin>>v[i]; + + upd(i,1); + + } + + + + for(i=n;i>=1;i--){ + + aux=cb(v[i]); + + upd(aux,-1); + + sol[aux]=i; + + } + + + + for(i=1;i<=n;i++) + + fout< +#include +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int inf = 0x3f3f3f3f, Dim = 30005; + + + +int aib[Dim], c[Dim], a[Dim]; + +int n; + + + +void Update(int p, int val) + +{ + + for ( int i = p; i <= n; i += i & -i) + + aib[i] += val; + +} + + + +int aib_bs(int k) + +{ + + int i = 0; + + for (int p = 1 << 15; p; p >>= 1) + + if ( i + p <= n && aib[i + p] < k) + + { + + k -= aib[i + p]; + + i += p; + + } + + + + return i + 1; + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; ++i) + + { + + fin >> a[i]; + + Update(i, +1); + + } + + + + int loc; + + for (int i = n; i; --i) + + { + + loc = aib_bs(a[i]); + + Update(loc, -1); + + c[loc] = i; + + } + + + + for (int i = 1; i <= n; ++i) + + fout << c[i] << '\n'; + + return 0; + +} diff --git a/ski/aib/73e23bae-137c-11eb-8043-309c234925a0.cpp b/ski/aib/73e23bae-137c-11eb-8043-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30886bac37a5f9537f8aacc4d74803315237dcf7 --- /dev/null +++ b/ski/aib/73e23bae-137c-11eb-8043-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout ("schi.out"); + + + +int n, i, a; + +int aib[30005], v[30005], sol[30005]; + + + +int query (int poz){ + + int sol = 0; + + for (;poz>=1;poz-=(poz&(-poz))){ + + sol += aib[poz]; + + } + + return sol; + +} + + + +void update (int poz, int val){ + + for (;poz<=n;poz+=(poz&(-poz))){ + + aib[poz] += val; + + } + +} + + + +int cautareBinara (int x){ + + int st, dr, mid, sol = INT_MAX; + + st = 1, dr = n; + + while (st <= dr){ + + mid = st + (dr - st)/2; + + if (x == query(mid)){ + + sol = min (sol, mid); + + } + + if (x <= query(mid)){ + + dr = mid - 1; + + } + + else{ + + st = mid + 1; + + } + + } + + return sol; + +} + + + +int main(){ + + fin >> n; + + for (i=1; i<=n; i++){ + + fin >> v[i]; + + update (i, 1); + + } + + for (i=n; i>=1; i--){ + + a = cautareBinara (v[i]); + + update (a, -1); + + sol[a] = i; + + } + + for (i=1; i<=n; i++){ + + fout << sol[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/aib/7435fef6-137c-11eb-b98e-309c234925a0.cpp b/ski/aib/7435fef6-137c-11eb-b98e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a40279f0f2ddbcbe55b0082a0f2f04dbfb876b18 --- /dev/null +++ b/ski/aib/7435fef6-137c-11eb-b98e-309c234925a0.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +using namespace std; + + +ifstream fin; +ofstream fout; + +int n, i, j, m; + +int current_sum, last_sum, aux; + +vector BITREE(30002 , 0); +vector players(30002); +vector results(30002); + +inline int least_significant_bit (int value){ + return ((value ^ (value - 1)) & value); +} + +void update (int index, int val){ + for (int ind = index; ind <= n; ind += least_significant_bit(ind)) + BITREE[ind] += val; + +} + +int get_sum (int index){ + int sum = 0; + + for (int ind = index; ind > 0; ind -= least_significant_bit(ind)) + sum += BITREE[ind]; + + return sum; +} + +int main (void){ + fin.open("schi.in"); + fin>>n; + for (i=1; i <= n; i++) + fin>>players[i]; + fin.close(); + + for (i=n; i>0; i--){ + current_sum = get_sum(players[i]); + last_sum = 0; + + while (current_sum - last_sum) { + last_sum = current_sum; + current_sum = get_sum(players[i] + current_sum); + } + aux = players[i] + current_sum; + + results[aux] = i; + + update(aux , 1); + } + + fout.open("schi.out"); + for (i=1; i<=n; i++) + fout< +using namespace std; + +const int NMAX = 30000; + +int v[NMAX + 5] , aib[NMAX + 5] , ans[NMAX + 5] , n; + +inline void update(int poz) + +{ + + for( ; poz <= n ; poz += (poz & (-poz))) + + aib[poz] --; + +} + +inline int query(int poz) + +{ + + int suma = 0; + + for( ; poz > 0 ; poz -= (poz & (-poz))) + + suma += aib[poz]; + + return suma; + +} + +inline int bs(int st , int dr , int k) + +{ + + int last = 1000001; + + while(st <= dr) + + { + + int med = (st + dr) / 2; + + int aux = query(med); + + if(k == aux && med < last) + + last = med; + + if(k <= aux) + + dr = med - 1; + + else + + st = med + 1; + + } + + return last; + +} + +int main() + +{ + + freopen("schi.in" , "r" , stdin); + + freopen("schi.out" , "w" , stdout); + + int i; + + scanf("%d" , &n); + + for(i = 1 ; i <= n ; i ++) + + { + + scanf("%d" , &v[i]); + + aib[i] = (i & (-i)); + + } + + for(i = n ; i >= 1 ; i --) + + { + + int poz = bs(1 , n ,v[i]); + + update(poz); + + ans[poz] = i; + + } + + for(i = 1 ; i <= n ; i ++) + + printf("%d\n" , ans[i]); + + return 0; + +} diff --git a/ski/aib/747ec8f6-137c-11eb-828e-309c234925a0.cpp b/ski/aib/747ec8f6-137c-11eb-828e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb854dff18c3d9600c56a917a33319d88dfdd2f0 --- /dev/null +++ b/ski/aib/747ec8f6-137c-11eb-828e-309c234925a0.cpp @@ -0,0 +1,111 @@ +#include +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,v[30001],sol[30001],aib[30001]; + +int suma(int poz1) + +{ + + int S=0; + + for (int i=poz1;i>0;i-=(i&(-i))) + + { + + S+=aib[i]; + + } + + return S; + +} + +void update(int poz1) + +{ + + for (int i=poz1;i<=n;i+=(i&(-i))){ + + aib[i]--; + + } + +} + +int cautare(int p,int u,int val) + +{ + + int min1=1000001; + + while (p<=u) + + { + + int m=(p+u)/2; + + int S=suma(m); + + if (val==S && m>n; + + for (i=1;i<=n;i++) + + { + + fin>>v[i]; + + aib[i]=i&(-i); + + } + + for (i=n;i>=1;i--) + + { + + int q=cautare(1,n,v[i]); + + update(q); + + sol[q]=i; + + } + + for (i=1;i<=n;i++) + + fout< +using namespace std; + +const int NMAX = 30000; + +int v[NMAX + 5] , aib[NMAX + 5] , ans[NMAX + 5] , n; + +inline void update(int poz) + +{ + + for( ; poz <= n ; poz += (poz & (-poz))) + + aib[poz] --; + +} + +inline int query(int poz) + +{ + + int suma = 0; + + for( ; poz > 0 ; poz -= (poz & (-poz))) + + suma += aib[poz]; + + return suma; + +} + +inline int bs(int st , int dr , int k) + +{ + + int last = 1000001; + + while(st <= dr) + + { + + int med = (st + dr) / 2; + + int aux = query(med); + + if(k == aux && med < last) + + last = med; + + if(k <= aux) + + dr = med - 1; + + else + + st = med + 1; + + } + + return last; + +} + +int main() + +{ + + freopen("schi.in" , "r" , stdin); + + freopen("schi.out" , "w" , stdout); + + int i; + + scanf("%d" , &n); + + for(i = 1 ; i <= n ; i ++) + + { + + scanf("%d" , &v[i]); + + aib[i] = (i & (-i)); + + } + + for(i = n ; i >= 1 ; i --) + + { + + int poz = bs(1 , n , v[i]); + + update(poz); + + ans[poz] = i; + + } + + for(i = 1 ; i <= n ; i ++) + + printf("%d\n" , ans[i]); + + return 0; + +} diff --git a/ski/aib/74be16d8-137c-11eb-9185-309c234925a0.cpp b/ski/aib/74be16d8-137c-11eb-9185-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da8738684e1bc1cb9b59523c0ad4a5fbb5e48c80 --- /dev/null +++ b/ski/aib/74be16d8-137c-11eb-9185-309c234925a0.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; + + +int n,v[500005],AIB[500005],ans[500005]; + +void add(int pos, int val){ + for (;pos<=n;pos += pos & (-pos)) AIB[pos] += val; +} + +int find(int val){ + int p = 1, st = 0; + for (;p<=n;p<<=1); + for (;p>=1;p>>=1){ + if (st + p <= n && AIB[st + p] < val){ + st += p; + val -= AIB[st]; + } + } + return st + 1; +} + +int main(){ + ios::sync_with_stdio(false); + freopen("schi.in","r",stdin); + freopen("schi.out","w",stdout); + scanf("%d",&n); + for (int i=1;i<=n;i++) scanf("%d",&v[i]), add(i,1); + + for (int i=n;i>=1;i--){ + int x = find(v[i]); + ans[x] = i; + add(x, -1); + } + + for (int i=1;i<=n;i++) cout << ans[i] << '\n'; + return 0; +} diff --git a/ski/aib/75175efe-137c-11eb-9591-309c234925a0.cpp b/ski/aib/75175efe-137c-11eb-9591-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76729e436553c14e7d8a9dde306c78c557d6c2d0 --- /dev/null +++ b/ski/aib/75175efe-137c-11eb-9591-309c234925a0.cpp @@ -0,0 +1,145 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int ey(int a){ + + return a & -a; + +} + + + +int n; + +int aib[30041]; + + + +void chit(int p, int a){ + + for(int i = p; i <= n; i += ey(i)){ + + aib[i] += a; + + } + +} + + + +int reit(int p){ + + int s = 0; + + for(int i = p; i > 0; i -= ey(i)){ + + s += aib[i]; + + } + + return s; + +} + + + +int yo; + +void shi(){ + + yo = 1; + + while(yo < n){ + + yo <<= 1; + + } + +} + + + +int bs(int a){ + + int p = 0; + + int ap = 0; + + for(int i = yo; i > 0; i >>= 1){ + + int np = p+i; + + if(np <= n){ + + int na = a-aib[np]; + + if(na > 0){ + + a = na; + + p = np; + + }else if(na == 0){ + + ap = np; + + } + + } + + } + + return ap; + +} + + + +int va[30041], sol[30041]; + + + +int main(){ + + fin >> n; + + for(int i = 1; i <= n; ++i){ + + fin >> va[i]; + + chit(i, 1); + + } + + shi(); + + for(int i = n; i >= 1; --i){ + + int pos = bs(va[i]); + + sol[pos] = i; + + chit(pos, -1); + + } + + for(int i = 1; i <= n; ++i){ + + fout << sol[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/aib/75261d78-137c-11eb-ad92-309c234925a0.cpp b/ski/aib/75261d78-137c-11eb-ad92-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3be2499e626eb538c19b4d685d84c975c864249 --- /dev/null +++ b/ski/aib/75261d78-137c-11eb-ad92-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + +int n,vec[30000],tree[30000]; +int results[30000]; + +int lsb(int a) +{ + return a&(-a); +} + +void update(int index,int value) +{ + while(index<=n) + { + tree[index]+=value; + index+=lsb(index); + } +} + +int query(int index) +{ + int result=0; + while(index) + { + result+=tree[index]; + index-=lsb(index); + } + return result; +} + +void read() +{ + scanf("%d",&n); + + for(int i=0; i=0; i--) + { + int need_empty=vec[i]; + int index=get_index(need_empty); + + update(index,-1); + + results[index-1]=i+1; + } + + + for(int i=0; i +using namespace std; + +ifstream in("schi.in"); +ofstream out("schi.out"); + +int n,vec[30000],tree[30000]; +int results[30000]; + +int lsb(int a) +{ + return a&(-a); +} + +void update(int index,int value) +{ + while(index<=n) + { + tree[index]+=value; + index+=lsb(index); + } +} + +int query(int index) +{ + int result=0; + while(index) + { + result+=tree[index]; + index-=lsb(index); + } + return result; +} + +void read() +{ + in>>n; + for(int i=0; i>vec[i]; + } + + for(int i=1; i<=n; i++) + { + update(i,1); + } +} + + +int get_index(int needed) +{ + int left=1,right=n; + + while(left=0; i--) + { + int need_empty=vec[i]; + int index=get_index(need_empty); + + update(index,-1); + + results[index-1]=i+1; + + + } + + + for(int i=0; i + using namespace std; + +int N , x , y ; + +int v[30005] , aib[30005] , sol[30005]; + +void Update (int poz ,int val) + +{ + + int i ; + + for (int i = poz ; i <= N ; i += (i & (-i) )) + + aib[i] += val; + + return ; + +} + +int suma (int poz) + +{ + + int i ; + + int sum = 0 ; + + for (int i = poz ; i >= 1 ; i -= (i & (-i) )) + + sum += aib[i] ; + + + + return sum ; + +} + +int BinarySearch (int st , int dr , int val) + +{ + + int Min = N + 1; + + while (st <= dr) + + { + + int mij = (st + (dr - st) / 2) ; + + int Sum = suma(mij); + + if (val == Sum && mij < Min) Min = mij; + + else if (val <= Sum) dr = mij - 1 ; + + else st = mij + 1 ; + + } + + + + return Min ; + +} + +int main() + +{ + + freopen("schi.in" , "r" , stdin) ; + + freopen("schi.out" , "w" , stdout) ; + + scanf ("%d" , &N) ; + + for (int i = 1 ; i <= N ; ++i) + + { + + scanf("%d" , &v[i]) ; + + aib[i] = (i & (-i) ); + + } + + + + for (int i = N ; i >= 1 ; --i) + + { + + int conc = BinarySearch(1,N,v[i]) ; + + Update(conc,-1) ; + + + + sol[conc] = i ; + + } + + for (int i = 1 ; i <= N ; ++i) printf("%d\n", sol[i]) ; + + return 0 ; + +} diff --git a/ski/aib/7554c49c-137c-11eb-8a7c-309c234925a0.cpp b/ski/aib/7554c49c-137c-11eb-8a7c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2448f383ffe62834d23e5ddfd6a8a32966d493b0 --- /dev/null +++ b/ski/aib/7554c49c-137c-11eb-8a7c-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int MAXN = 30010; + + + +int bit[MAXN], res[MAXN], in[MAXN], n; + + + +void update(int pos, int val) { + + for (int i = pos; i <= n; i += i & (-i)) + + bit[i] += val; + +} + + + +int binarySearch(int val) { + + int i, step; + + for (step = 1; step < n; step <<= 1); + + for (i = 0; step; step >>= 1) + + if (i + step <= n) { + + if (bit[i + step] < val) { + + i += step; + + val -= bit[i]; + + } + + } + + return i + 1; + +} + + + +void read() { + + fin >> n; + + for (int i = 1; i <= n; ++i) { + + fin >> in[i]; + + update(i, 1); + + } + +} + + + +void solve() { + + for (int i = n; i >= 1; --i) { + + int pos = binarySearch(in[i]); + + res[pos] = i; + + update(pos, -1); + + } + +} + + + +void print() { + + for (int i = 1; i <= n; ++i) + + fout << res[i] << '\n'; + +} + + + +int main() { + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/ski/aib/7573664c-137c-11eb-967a-309c234925a0.cpp b/ski/aib/7573664c-137c-11eb-967a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..33c4a7d5496f7e1f05e9daefd577f2ba84c12cd3 --- /dev/null +++ b/ski/aib/7573664c-137c-11eb-967a-309c234925a0.cpp @@ -0,0 +1,101 @@ +#include +#include + + + + +using namespace std; + + + +int n; + +int v[30000 + 5], aint[4 * 30000 + 5], ord[30000 + 5]; + + + +void init(int nod, int st, int dr) { + + int mij = (st + dr) / 2; + + + + aint[nod] = dr - st + 1; + + if(dr != st) { + + init(2 * nod, st, mij); + + init(2 * nod + 1, mij + 1, dr); + + } + +} + + + +int query(int nod, int st, int dr, int qval) { + + int mij = (st + dr) / 2; + + + + aint[nod]--; + + + + if(st == dr) + + return st; + + + + if(qval <= aint[2 * nod]) + + return query(2 * nod, st, mij, qval); + + return query(2 * nod + 1, mij + 1, dr, qval - aint[2 * nod]); + +} + + + +int main() { + + freopen("schi.in", "r", stdin); + + freopen("schi.out", "w", stdout); + + int poz; + + + + scanf("%d", &n); + + for(int i = 1; i <= n; i++) + + scanf("%d", &v[i]); + + + + init(1, 1, n); + + for(int i = n; i >= 1; i--) { + + poz = query(1, 1, n, v[i]); + + ord[poz] = i; + + } + + + + for(int i = 1; i <= n; i++) + + printf("%d\n", ord[i]); + + + + return 0; + +} diff --git a/ski/aib/75a038ba-137c-11eb-a361-309c234925a0.cpp b/ski/aib/75a038ba-137c-11eb-a361-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f344f91a93c42a93a35c2d40a02faf0de6bf5e13 --- /dev/null +++ b/ski/aib/75a038ba-137c-11eb-a361-309c234925a0.cpp @@ -0,0 +1,103 @@ +#include +using namespace std; + +ifstream in("schi.in"); +ofstream out("schi.out"); + +int n,vec[30000],tree[30000]; +int results[30000]; + +int lsb(int a) +{ + return a&(-a); +} + +void update(int index,int value) +{ + while(index<=n) + { + tree[index]+=value; + index+=lsb(index); + } +} + +int query(int index) +{ + int result=0; + while(index) + { + result+=tree[index]; + index-=lsb(index); + } + return result; +} + +void read() +{ + in>>n; + for(int i=0; i>vec[i]; + } + + for(int i=1; i<=n; i++) + { + update(i,1); + } +} + + +int get_index(int needed) +{ + int left=1,right=n; + + while(left=0; i--) + { + int need_empty=vec[i]; + int index=get_index(need_empty); + + update(index,-1); + + results[index-1]=i+1; + } + + string to_output; + + + for(int i=0; i + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int MAXN = 30010; + + + +int bit[MAXN], res[MAXN], in[MAXN], n; + + + +void update(int pos, int val) { + + for (int i = pos; i <= n; i += i & (-i)) + + bit[i] += val; + +} + + + +int sum(int pos) { + + int s = 0; + + for (int i = pos; i >= 1; i -= i & (-i)) + + s += bit[i]; + + return s; + +} + + + +int binarySearch(int val) { + + int left = 1, right = n, pos = 0; + + while (left <= right) { + + int middle = left + (right - left) / 2, s; + + s = sum(middle); + + if (val == s) { + + pos = middle; + + right = middle - 1; + + } + + else if (val < s) + + right = middle - 1; + + else + + left = middle + 1; + + } + + return pos; + +} + + + +void read() { + + fin >> n; + + for (int i = 1; i <= n; ++i) { + + fin >> in[i]; + + update(i, 1); + + } + +} + + + +void solve() { + + for (int i = n; i >= 1; --i) { + + int pos = binarySearch(in[i]); + + res[pos] = i; + + update(pos, -1); + + } + +} + + + +void print() { + + for (int i = 1; i <= n; ++i) + + fout << res[i] << '\n'; + +} + + + +int main() { + + read(); + + solve(); + + print(); + + return 0; + +} diff --git a/ski/aib/75f2773a-137c-11eb-8b32-309c234925a0.cpp b/ski/aib/75f2773a-137c-11eb-8b32-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62bc22816dd3fd0e87eed3caa7fbba0221e63d9a --- /dev/null +++ b/ski/aib/75f2773a-137c-11eb-8b32-309c234925a0.cpp @@ -0,0 +1,183 @@ +#include +#include + + + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +int placement[30001], v[30001], aib[30001]; + +int n; + + + +void update(int val, int poz) + +{ + + for(int i = poz; i <= n; i += i&-i) + + aib[i] += val; + +} + + + +int query(int poz) + +{ + + int s = 0; + + + + for(int i = poz; i; i -= i&-i) + + s += aib[i]; + + + + return s; + +} + + + +int binarySearch(int s) + +{ + + int l = 1, r = n, poz = -1, mij = n; + + + + if(query(mij) == s) + + poz = mij; + + + + while(l <= r) + + { + + mij = (l+r) >> 1; + + int aux = query(mij); + + + + if(0) + + cout << "mij:" << mij << " aux:" << aux; + + + + if(aux == s) + + { + + poz = mij; + + r = mij-1; + + } + + else + + { + + if(aux > s) + + r = mij-1; + + else + + l = mij+1; + + } + + } + + + + return poz; + +} + + + +int main() + +{ + + in >> n; + + + + for(int i = 1; i <= n; i++) + + { + + in >> v[i]; + + update(1, i); + + } + + + + if(0) + + { + + for(int i = 1; i <= n; i++) + + { + + cout << i << ' ' << aib[i] << '\n'; + + } + + } + + + + for(int i = n; i; i--) + + { + + int poz = binarySearch(v[i]); + + + + if(0) + + cout << " poz:" << poz << '\n'; + + + + update(-1, poz); + + placement[poz] = i; + + } + + + + for(int i = 1; i <= n; i++) + + out << placement[i] << '\n'; + + return 0; + +} diff --git a/ski/aib/7601d6ac-137c-11eb-9c5a-309c234925a0.cpp b/ski/aib/7601d6ac-137c-11eb-9c5a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35d25c7227f5b753c22ec5f4ed1b0ef374b26ebf --- /dev/null +++ b/ski/aib/7601d6ac-137c-11eb-9c5a-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int inf = 0x3f3f3f3f, Dim = 30005; + +int aib[Dim], c[Dim], a[Dim]; +int n; + +void Update(int p, int val) +{ + for ( int i = p; i <= n; i += i & -i) + aib[i] += val; +} + +int aib_bs(int k) +{ + int i = 0; + for (int p = 1 << 15; p; p >>= 1) + if ( i + p <= n && aib[i + p] < k) + { + k -= aib[i + p]; + i += p; + } + + return i + 1; +} + +int main() +{ + fin >> n; + for (int i = 1; i <= n; ++i) + { + fin >> a[i]; + Update(i, +1); + } + + int loc; + for (int i = n; i; --i) + { + loc = aib_bs(a[i]); + Update(loc, -1); + c[loc] = i; + } + + for (int i = 1; i <= n; ++i) + fout << c[i] << '\n'; + return 0; +} diff --git a/ski/aib/76103568-137c-11eb-9006-309c234925a0.cpp b/ski/aib/76103568-137c-11eb-9006-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f2da6142046cfcdfc86025bef06e808cb4e7f4f --- /dev/null +++ b/ski/aib/76103568-137c-11eb-9006-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +vector v, fp; + +int n, aib[33033], x; + +inline void Update(int poz, int val) + +{ + + for (int i = poz; i <= n; i += i & -i) + + aib[i] += val; + +} + +inline int Query(int poz) + +{ + + int s = 0; + + for (int i = poz; i > 0; i -= i & -i) + + s += aib[i]; + + return s; + +} + +inline int Find(int val) + +{ + + int st = 1, dr = n, p, mid, res; + + while (st <= dr) + + { + + mid = (st + dr) / 2; + + p = Query(mid); + + if (p == val) + + res = mid, dr = mid - 1; + + else if (p < val) + + st = mid + 1; + + else dr = mid - 1; + + } + + return res; + +} + +int main() + +{ + + fin >> n; + + v = fp = vector(n + 1); + + for (int i = 1; i <= n; ++i) + + fin >> v[i], Update(i, 1); + + for (int i = n; i; --i) + + { + + x = Find(v[i]); + + fp[x] = i; + + Update(x, -1); + + } + + for (int i = 1; i <= n; ++i) + + fout << fp[i] << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/761f2c4a-137c-11eb-b22b-309c234925a0.cpp b/ski/aib/761f2c4a-137c-11eb-b22b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02b9e29d5c7668005bb8288dcb6436c42346f365 --- /dev/null +++ b/ski/aib/761f2c4a-137c-11eb-b22b-309c234925a0.cpp @@ -0,0 +1,80 @@ +#include +#include +using namespace std; +ifstream fin("schi.in"); +ofstream fout("schi.out"); +int v[30005], o[30005], aj[30005]; +int n; +void read() { + fin >> n; + for (int i = 1; i <= n; ++i) { + fin >> v[i]; + } +} +void update(int p, int val) { + for (int i = p; i <= n; i += i & -i) { + aj[i] += val; + } +} +void setToOne() { + for (int i = 1; i <= n; ++i) { + update(i, 1); + } +} +long query(int p) { + long s = 0; + for (int i = p; i > 0; i -= i & -i) { + s += aj[i]; + } + return s; +} +int searchSum(int sum) { + int pos = n + 1, p; + int st = 0, dr = n + 1; + p = n; + int s = query(p); + if (s == sum) { + pos = n; + } + while (p && p > st && p < dr) { + p = (st + dr) / 2; + s = query(p); + if (s > sum) + { + if (dr > p) + dr = p; + p -= 1; + } + else if (s == sum) { + pos = min(pos, p); + dr = p; + p -= 1; + } + else + { + if (st < p) { + st = p; + } + p += 1; + } + } + if (pos == n + 1) + return -1; + return pos; +} +int main() +{ + read(); + setToOne(); + o[v[n]] = n; + update(v[n], -1); + for (int i = n - 1; i >= 1; --i) { + int loc = searchSum(v[i]); + o[loc] = i; + update(loc, -1); + } + for (int i = 1; i <= n; ++i) { + fout << o[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/762db9e6-137c-11eb-a807-309c234925a0.cpp b/ski/aib/762db9e6-137c-11eb-a807-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c44908ad46b410b47173ce6ace2d6d3c613aca18 --- /dev/null +++ b/ski/aib/762db9e6-137c-11eb-a807-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + { + aib[p] += x; + p += (p & (-p)); + } +} + + + +int Suma(int p) + +{ + + int s = 0; + while(p > 0) + + { + s += aib[p]; + p -= (p & (-p)); + } + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + st = 1; + dr = p; + poz = -1; + while(st <= dr) + + { + mij = (st + dr) / 2; + suma = Suma(mij); + if(suma == x) + + { + poz = mij; + dr = mij - 1; + } + else if(suma > x) + dr = mij - 1; + else st = mij + 1; + } + return poz; + +} + + + +int main() + +{ + + int i, p; + fin >> n; + for(i = 1; i <= n; i++) + { + fin >> a[i]; + Update(i, 1); + } + + for(i = n; i >= 1; i--) + + { + p = CautBin(n, a[i]); + Update(p, -1); + cls[p] = i; + } + + for(i = 1; i <= n; i++) + fout << cls[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/763f8f3e-137c-11eb-be77-309c234925a0.cpp b/ski/aib/763f8f3e-137c-11eb-be77-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf4e1fcb99c1afcfd2b511b734b862ffc7c7ddd1 --- /dev/null +++ b/ski/aib/763f8f3e-137c-11eb-be77-309c234925a0.cpp @@ -0,0 +1,115 @@ +#include +#include + + +using namespace std; + + + +int aib[30001]; + +int v[30001]; + +int v2[30001]; + + + +int zeros( int x ) { + + return ( x ^ ( x - 1 ) ) & x; + +} + + + +void update( int x, int poz, int n ) { + + while ( poz <= n ) { + + aib[poz] += x; + + poz += zeros( poz ); + + } + +} + + + +int sum( int poz ) { + + int s = 0; + + while ( poz > 0 ) { + + s += aib[poz]; + + poz -= zeros( poz ); + + } + + return s; + +} + + + +int cbin( int x, int n ) { + + int poz = 0, i, s = 0; + + for ( i = 1 << 15; i > 0; i /= 2 ) { + + if ( poz + i <= n && s + aib[poz + i] <= x - 1 ) { + + poz += i; + + s += aib[poz]; + + } + + } + + return poz + 1; + +} + + + +int main() { + + FILE *fin = fopen( "schi.in", "r" ), *fout = fopen( "schi.out", "w" ); + + int n, i; + + fscanf( fin, "%d", &n ); + + for ( i = 1; i <= n; i ++ ) { + + fscanf( fin, "%d", &v[i] ); + + update( 1, i, n ); + + } + + for ( i = n; i > 0; i -- ) { + + v2[cbin( v[i], n )] = i; + + update( -1, cbin( v[i], n ), n ); + + } + + for ( i = 1; i <= n; i ++ ) { + + fprintf( fout, "%d\n", v2[i] ); + + } + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/ski/aib/764e8b2e-137c-11eb-bd58-309c234925a0.cpp b/ski/aib/764e8b2e-137c-11eb-bd58-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c02c7420c5baa37e590dfb10fcbb30ea8d6f4c0 --- /dev/null +++ b/ski/aib/764e8b2e-137c-11eb-bd58-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include +using namespace std; + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; + +int t[N]; + +void update(int poz, int vall) + +{ + + while (poz <= n) + + { + + t[poz] += vall; + + poz += poz&(-poz); + + } + +} + +int query(int poz) + +{ + + int ans(0); + + while (poz > 0) + + { + + ans += t[poz]; + + poz -= poz&(-poz); + + } + + return ans; + +} + +int cb(int lol) + +{ + + int pas = 1<<14, r(0); + + while (pas > 0) + + { + + if (r + pas <= n && t[r + pas] < lol) + + { + + lol -= t[r + pas]; + + r += pas; + + } + + pas>>=1; + + } + + return r + 1; + +} + +int main() + +{ + + cin >> n; + + for (int i = 1; i <= n; ++i) + + { + + cin >> v[i]; + + } + + for (int i = 1; i <= n; ++i) + + { + + update(i, 1); + + } + + for (int i = n; i >= 1; --i) + + { + + int ans = cb(v[i]); + + update(ans, -1); + + poz[ans] = i; + + } + + for (int i = 1; i <= n; ++i) + + { + + cout << poz[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/aib/765d8986-137c-11eb-8c16-309c234925a0.cpp b/ski/aib/765d8986-137c-11eb-8c16-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6749a8051c18b6ba649dc2c7418fb580cc7af585 --- /dev/null +++ b/ski/aib/765d8986-137c-11eb-8c16-309c234925a0.cpp @@ -0,0 +1,145 @@ +#include +#include + + + + +using namespace std; + + + +stack < int > st; + + + +int aib[30005], clas[30005], n, a; + + + +void update(int p, int val) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] += val; + +} + + + +int query(int p) + +{ + + int suma = 0; + + for (int i = p; i >= 1; i -= i & -i) + + suma += aib[i]; + + return suma; + +} + + + +int bsearch(int p, int s) + +{ + + int st, dr, pos, mij; + + int suma; + + st = 1; + + dr = p; + + pos = -1; + + while (st <= dr) + + { + + mij =(st + dr) / 2; + + suma = query(mij); + + if (suma == s) + + { + + pos = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return pos; + +} + + + +int main() + +{ + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + + + fin >> n; + + + + for (int i = 1; i <= n; ++i) + + { + + fin >> a; + + st.push(a); + + update(i, 1); + + } + + + + for (int i = n; i >= 1; --i) + + { + + a = st.top(); + + st.pop(); + + int p = bsearch(n, a); + + clas[p] = i; + + update(p, -1); + + } + + + + for (int i = 1; i <= n; ++i) + + fout << clas[i] << "\n"; + + + + return 0; + +} diff --git a/ski/aib/7690ed08-137c-11eb-a02e-309c234925a0.cpp b/ski/aib/7690ed08-137c-11eb-a02e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4005ff07636db7d5510bc3f075ff05fee69c9418 --- /dev/null +++ b/ski/aib/7690ed08-137c-11eb-a02e-309c234925a0.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +int n,aib[30005],cls[30005],a[30005]; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +void update(int p, int x) + +{ + + while (p<=n) + + { + + aib[p]+=x; + + p+=(p&(-p)); + + } + + + +} + + + +int Suma(int p) + +{ + + int s=0; + + while (p>0) + + { + + s+=aib[p]; + + p-=(p&(-p)); + + } + + return s; + +} + + + +int CautBin(int p, int s) + +{ + + int st,dr,pos,mij; + + int suma; + + st=1; + + dr=p; + + pos=-1; + + while (st<=dr) + + { + + mij=(st+dr)/2; + + suma=Suma(mij); + + if (suma==s) + + { + + pos=mij; + + dr=mij-1; + + } + + else if (suma>s) dr=mij-1; + + else st=mij+1; + + } + + return pos; + +} + + + + + +int main() + +{ + + int pos,x,i; + + f>>n; + + for (i=1; i<=n; i++) + + { + + f>>a[i]; + + update(i,1); + + } + + for (i=n; i>=1; i--) + + { + + x=a[i]; + + pos=CautBin(n,x); + + update(pos,-1); + + cls[pos]=i; + + } + + for (i=1; i<=n; i++) + + g< + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CB(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, p; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for(i = n; i >= 1; i--) + + { + + p = CB(n, a[i]); + + Update(p, -1); + + cls[p] = i; + + } + + for(i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/76ae2086-137c-11eb-aaa4-309c234925a0.cpp b/ski/aib/76ae2086-137c-11eb-aaa4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e9c75461d9eb2216502fb7d0cab6806eb991dee --- /dev/null +++ b/ski/aib/76ae2086-137c-11eb-aaa4-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for (i = n; i >= 1; i--) + + { + + x = a[i]; + + + + poz = CautBin(n, x); + + Update(poz, -1); + + cls[poz] = i; + + } + + for (i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/ski/aib/76bd62c8-137c-11eb-a301-309c234925a0.cpp b/ski/aib/76bd62c8-137c-11eb-a301-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3e1abcaf89361c808c7fc47d37317137830e23a --- /dev/null +++ b/ski/aib/76bd62c8-137c-11eb-a301-309c234925a0.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int aib[30005], n, m; + +int cls[30005],a[30005]; + +void update(int p, int x ) + +{ + + while(p<=n) + + { + + aib[p] +=x; + + p+=(p & ( - p )); + + } + +} + +int suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s+=aib[p]; + + p-=( p & ( - p)); + + } + + return s; + +} + + + +int caut(int p, int s) + +{ + + int st, dr, poz, mij,su ; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + su = suma(mij); + + if(su == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(su > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + + + +int main() + +{ + + int i, x, op,b; + + fin >> n; + + for(i = 1; i <= n; i ++) + + { + + fin >> a[i]; + + update(i,1); + + } + + int poz; + + for(i = n ; i >= 1; i--) + + { + + x = a[i]; + + + + poz = caut(n , x); + + update(poz, - 1); + + cls[poz] = i; + + } + + + + + + for(i = 1; i <= n ; i++) + + fout << cls[i]<<"\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/76ccb398-137c-11eb-b31b-309c234925a0.cpp b/ski/aib/76ccb398-137c-11eb-b31b-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..819eab2b52497c2189fd71d0b5a038f6e50c12c5 --- /dev/null +++ b/ski/aib/76ccb398-137c-11eb-b31b-309c234925a0.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +int n,aib[30005],cls[30005],a[30005]; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +void update(int p, int x) + +{ + + while (p<=n) + + { + + aib[p]+=x; + + p+=(p&(-p)); + + } + + + +} + + + +int Suma(int p) + +{ + + int s=0; + + while (p>0) + + { + + s+=aib[p]; + + p-=(p&(-p)); + + } + + return s; + +} + + + + + + + +int CautBin(int p, int s) + +{ + + int st,dr,poz,mij; + + int suma; + + st=1; + + dr=p; + + poz=-1; + + while (st<=dr) + + { + + mij=(st+dr)/2; + + suma=Suma(mij); + + if (suma==s) + + { + + poz=mij; + + dr=mij-1; + + } + + else if (suma>s) dr=mij-1; + + else st=mij+1; + + } + + return poz; + +} + + + + + +int main() + +{ + + int poz,x; + + fin >> n; + + for (int i=1; i<=n; i++) + + { + + fin >> a[i]; + + update(i,1); + + } + + for (int i=n; i>=1; i--) + + { + + x=a[i]; + + + + + + poz=CautBin(n,x); + + update(poz,-1); + + cls[poz]=i; + + } + + for (int i=1; i<=n; i++) + + fout << cls[i] << "\n"; + + + + + + return 0; + +} diff --git a/ski/aib/76de1cdc-137c-11eb-9618-309c234925a0.cpp b/ski/aib/76de1cdc-137c-11eb-9618-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e9c75461d9eb2216502fb7d0cab6806eb991dee --- /dev/null +++ b/ski/aib/76de1cdc-137c-11eb-9618-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for (i = n; i >= 1; i--) + + { + + x = a[i]; + + + + poz = CautBin(n, x); + + Update(poz, -1); + + cls[poz] = i; + + } + + for (i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/ski/aib/76ee6252-137c-11eb-bb2d-309c234925a0.cpp b/ski/aib/76ee6252-137c-11eb-bb2d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f084fc828e4499443e93c0258379db1c3f8f9c73 --- /dev/null +++ b/ski/aib/76ee6252-137c-11eb-bb2d-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +int n, aib[30003], a[30003], cls[30003]; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p+= (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautareBinara(int p, int s) + +{ + + int st, dr, mij, poz; + + int suma; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else + + if(suma > s) + + dr = mij - 1; + + else + + st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for(i = n; i >= 1; i--) + + { + + x = a[i]; + + + + + + + + poz = CautareBinara(n,x); + + Update(poz,-1); + + cls[poz] = i; + + } + + + + for(i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/76fc6d68-137c-11eb-bb25-309c234925a0.cpp b/ski/aib/76fc6d68-137c-11eb-bb25-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..35d25c7227f5b753c22ec5f4ed1b0ef374b26ebf --- /dev/null +++ b/ski/aib/76fc6d68-137c-11eb-bb25-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int inf = 0x3f3f3f3f, Dim = 30005; + +int aib[Dim], c[Dim], a[Dim]; +int n; + +void Update(int p, int val) +{ + for ( int i = p; i <= n; i += i & -i) + aib[i] += val; +} + +int aib_bs(int k) +{ + int i = 0; + for (int p = 1 << 15; p; p >>= 1) + if ( i + p <= n && aib[i + p] < k) + { + k -= aib[i + p]; + i += p; + } + + return i + 1; +} + +int main() +{ + fin >> n; + for (int i = 1; i <= n; ++i) + { + fin >> a[i]; + Update(i, +1); + } + + int loc; + for (int i = n; i; --i) + { + loc = aib_bs(a[i]); + Update(loc, -1); + c[loc] = i; + } + + for (int i = 1; i <= n; ++i) + fout << c[i] << '\n'; + return 0; +} diff --git a/ski/aib/770b136c-137c-11eb-9bb0-309c234925a0.cpp b/ski/aib/770b136c-137c-11eb-9bb0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e9c75461d9eb2216502fb7d0cab6806eb991dee --- /dev/null +++ b/ski/aib/770b136c-137c-11eb-9bb0-309c234925a0.cpp @@ -0,0 +1,149 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update (int p, int x) + +{ + + while (p <= n) + + { + + aib[p] += x; + p += (p & (-p)); + + } + +} + + + + + +int Suma (int p) + +{ + + int s = 0; + + while (p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin (int p, int s) + +{ + + int st, dr, poz, mij, suma; + + st = 1; + + dr = p; + + poz = -1; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if (suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if (suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for (i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for (i = n; i >= 1; i--) + + { + + x = a[i]; + + + + poz = CautBin(n, x); + + Update(poz, -1); + + cls[poz] = i; + + } + + for (i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + fout.close(); + + fin.close(); + + + + return 0; + +} diff --git a/ski/aib/7719dec0-137c-11eb-b2d3-309c234925a0.cpp b/ski/aib/7719dec0-137c-11eb-b2d3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0396bde4483bf3e83e5d85743f3e0699ac85d4af --- /dev/null +++ b/ski/aib/7719dec0-137c-11eb-b2d3-309c234925a0.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, p; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for(i = n; i >= 1; i--) + + { + + p = CautBin(n, a[i]); + + Update(p, -1); + + cls[p] = i; + + } + + for(i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/7728369e-137c-11eb-9666-309c234925a0.cpp b/ski/aib/7728369e-137c-11eb-9666-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a831064d91afa6fc0c7faf4a317438ae70c2aaf --- /dev/null +++ b/ski/aib/7728369e-137c-11eb-9666-309c234925a0.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); +int n,aib[100001],m,cls[30005],a[30005]; + + + +void Update(int p,int x) + +{ + + while(p<=n) + + { + + aib[p]+=x; + p+=(p&(-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s=0; + + while(p>0) + + { + + s+=aib[p]; + + p-=(p&(-p)); + + } + + return s; + +} + + + +int CB(int p,int s) + +{ + + int st,dr,poz,mij,suma; + + st=1;dr=p;poz=-1; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + suma=Suma(mij); + + if(suma==s) + + { + + poz=mij; + + dr=mij-1; + + } + + else if(suma>s)dr=mij-1; + + else st=mij+1; + + } + + return poz; + +} + + + + + +int main() + +{ + + int i,x,poz; + + fin>>n; + + for(i=1;i<=n;i++) + + { + + fin>>a[i]; + + Update(i,1); + + } + + for(i=n;i>0;i--) + + { + + x=a[i]; + + + + poz=CB(n,x); + + Update(poz,-1); + + cls[poz]=i; + + } + + for(i=1;i<=n;i++) + + fout< +#include +using namespace std; + +int n, aib[100005], a[30005], cls[30005]; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz , mij; + + int suma; + + st = 1; dr = p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == s) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > s) dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for(int i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + + + for(i = n; i >= 1; i--) + + { + + x = a[i]; + + + + poz = CautBin(n, x); + + Update(poz, -1); + + cls[poz] = i; + + } + + for(i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/774508de-137c-11eb-82db-309c234925a0.cpp b/ski/aib/774508de-137c-11eb-82db-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee7728851e2e4205b70ce919921172927903a832 --- /dev/null +++ b/ski/aib/774508de-137c-11eb-82db-309c234925a0.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +int n, aib[30005],a[30005],cls[30005]; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + + + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + + + +int CautBin(int p, int s) + +{ + + int st, dr, poz, mij; + + int sum; + + st = 1; dr= p; poz = -1; + + while(st <= dr) + + { + + mij = (st + dr)/2; + + sum = Suma(mij); + + if (sum == s) + + { + + poz = mij; + + dr = mij -1; + + } + + else if (sum > s) dr = mij -1; + + else st = mij + 1; + + } + + return poz; + +} + +int main() + +{ + + int i, x, poz; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for(i = n; i >= 1; i--) + + { + + x = a[i]; + + + + poz = CautBin(n , x); + + Update(poz, -1); + + cls[poz] = i; + + + + } + + for(i = 1; i <= n; i++) + + fout << cls[i] <<"\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/7753b0ac-137c-11eb-acfe-309c234925a0.cpp b/ski/aib/7753b0ac-137c-11eb-acfe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..746cc77e10722c41f00cad1ecaf3f0036fd3ff4a --- /dev/null +++ b/ski/aib/7753b0ac-137c-11eb-acfe-309c234925a0.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, aib[30005], a[30005], cls[30005]; + + + +void Update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p & (-p)); + + } + +} + + + +int Suma(int p) + +{ + + int s = 0; + + while(p > 0) + + { + + s += aib[p]; + + p -= (p & (-p)); + + } + + return s; + +} + + + +int CB(int p, int x) + +{ + + int st, dr, mij, poz, suma = 0; + + st = 1; + + dr = p; + + poz = -1; + + while(st <= dr) + + { + + mij = (st + dr) / 2; + + suma = Suma(mij); + + if(suma == x) + + { + + poz = mij; + + dr = mij - 1; + + } + + else if(suma > x) + + dr = mij - 1; + + else st = mij + 1; + + } + + return poz; + +} + + + +int main() + +{ + + int i, p; + + fin >> n; + + for(i = 1; i <= n; i++) + + { + + fin >> a[i]; + + Update(i, 1); + + } + + for(i = n; i >= 1; i--) + + { + + p = CB(n, a[i]); + + Update(p, -1); + + cls[p] = i; + + } + + for(i = 1; i <= n; i++) + + fout << cls[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/77fe86f4-137c-11eb-a2e2-309c234925a0.cpp b/ski/aib/77fe86f4-137c-11eb-a2e2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cec77d113d5d0cdb0bb2c824d3becbd90c27ea39 --- /dev/null +++ b/ski/aib/77fe86f4-137c-11eb-a2e2-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int bit[30003], n; + + + +int lsb(int x) + +{ + + return x&(-x); + +} + + + +void upd(int i, int d) + +{ + + for(;i<=n;i+=lsb(i)) bit[i]+=d; + +} + + + +int query(int i) + +{ + + int sol=0; + + for(;i;i-=lsb(i)) sol+=bit[i]; + + return sol; + +} + + + +int v[30003], sol[30003]; + + + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;++i) + + { + + upd(i, 1); + + fin>>v[i]; + + } + + for(int i=n;i;--i) + + { + + int st=1, dr=n, last=n; + + while(st<=dr) + + { + + int mij=(st+dr)/2; + + if(query(mij)>=v[i]) last=mij, dr=mij-1; + + else st=mij+1; + + } + + upd(last, -1); + + sol[last]=i; + + } + + for(int i=1;i<=n;++i) fout< + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int nmax = 30005; + +int n, aib[nmax], v[nmax], t[nmax]; + +void update(int p, int x) + +{ + + while(p <= n) + + { + + aib[p] += x; + + p += (p&(-p)); + + } + +} + +int Query(int p) + +{ + + int sum = 0; + + while(p > 0) + + { + + sum += aib[p]; + + p -= (p&(-p)); + + } + + return sum; + +} + +int Cauta(int x) + +{ + + int st = 1, dr = n, val, sol = -1; + + while(st <= dr) + + { + + int mid = (st + dr) / 2; + + val = Query(mid); + + if(val == x) + + { + + sol = mid; + + dr = mid - 1; + + } + + else if(val > x) + + dr = mid - 1; + + else + + st = mid + 1; + + } + + return sol; + +} + +int main() + +{ + + fin >> n; + + for(int i = 1; i <= n; ++i) + + { + + fin >> v[i]; + + update(i, 1); + + } + + for(int i = n; i > 0; i--) + + { + + int p = Cauta(v[i]); + + t[p] = i; + + update(p, -1); + + } + + for(int i = 1; i <= n; ++i) + + fout << t[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/786da56c-137c-11eb-b371-309c234925a0.cpp b/ski/aib/786da56c-137c-11eb-b371-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf4e1fcb99c1afcfd2b511b734b862ffc7c7ddd1 --- /dev/null +++ b/ski/aib/786da56c-137c-11eb-b371-309c234925a0.cpp @@ -0,0 +1,115 @@ +#include +#include + + +using namespace std; + + + +int aib[30001]; + +int v[30001]; + +int v2[30001]; + + + +int zeros( int x ) { + + return ( x ^ ( x - 1 ) ) & x; + +} + + + +void update( int x, int poz, int n ) { + + while ( poz <= n ) { + + aib[poz] += x; + + poz += zeros( poz ); + + } + +} + + + +int sum( int poz ) { + + int s = 0; + + while ( poz > 0 ) { + + s += aib[poz]; + + poz -= zeros( poz ); + + } + + return s; + +} + + + +int cbin( int x, int n ) { + + int poz = 0, i, s = 0; + + for ( i = 1 << 15; i > 0; i /= 2 ) { + + if ( poz + i <= n && s + aib[poz + i] <= x - 1 ) { + + poz += i; + + s += aib[poz]; + + } + + } + + return poz + 1; + +} + + + +int main() { + + FILE *fin = fopen( "schi.in", "r" ), *fout = fopen( "schi.out", "w" ); + + int n, i; + + fscanf( fin, "%d", &n ); + + for ( i = 1; i <= n; i ++ ) { + + fscanf( fin, "%d", &v[i] ); + + update( 1, i, n ); + + } + + for ( i = n; i > 0; i -- ) { + + v2[cbin( v[i], n )] = i; + + update( -1, cbin( v[i], n ), n ); + + } + + for ( i = 1; i <= n; i ++ ) { + + fprintf( fout, "%d\n", v2[i] ); + + } + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/ski/aib/787dcb86-137c-11eb-814a-309c234925a0.cpp b/ski/aib/787dcb86-137c-11eb-814a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f006585b5f475f957624f49be455149512c3b37 --- /dev/null +++ b/ski/aib/787dcb86-137c-11eb-814a-309c234925a0.cpp @@ -0,0 +1,77 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int v[30005], n, conc[30005], aib[30005]; + + + +void update(int poz, int val){ + + for (int i = poz; i <= n; i += i & (-i)) + + aib[i] += val; + +} + + + +int cauta(int val){ + + int poz = 0; + + for (int i = 1 << 15; i > 0; i >>= 1) + + if ((poz | i) <= n && aib[poz | i] <= val){ + + poz |= i; + + val -= aib[poz]; + + } + + return poz; + +} + + + +int main(){ + + fin >> n; + + for (int i = 1; i <= n; i++){ + + fin >> v[i]; + + update(i, 1); + + } + + for (int i = n; i >= 1; i--){ + + int poz = cauta(v[i] - 1) + 1; + + update(poz, -1); + + conc[poz] = i; + + } + + for (int i = 1; i <= n; i++) + + fout << conc[i] << '\n'; + + return 0; + +} diff --git a/ski/aib/788c8f8c-137c-11eb-bfec-309c234925a0.cpp b/ski/aib/788c8f8c-137c-11eb-bfec-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f62cafda254dea17506623279ea18f4022a9d2f --- /dev/null +++ b/ski/aib/788c8f8c-137c-11eb-bfec-309c234925a0.cpp @@ -0,0 +1,53 @@ +#include + + +using namespace std; + +struct Fenwick { + int n; + vector tree; + Fenwick(int n_) : n(n_), tree(n + 1) {} + inline int lsb(int x) { + return -x & x; + } + void Update(int pos, int val) { + for (++pos; pos <= n; pos += lsb(pos)) + tree[pos] += val; + } + int Query(int pos) { + int ans = 0; + for (++pos; pos > 0; pos -= lsb(pos)) + ans += tree[pos]; + return ans; + } +}; + +int main() { + ifstream cin("schi.in"); + ofstream cout("schi.out"); + + int n; cin >> n; + vector v(n); + for (int &x : v) { + cin >> x; --x; + } + Fenwick tree(n); + vector ans(n); + for (int i = n - 1; i >= 0; --i) { + int pos = -1, lo = 0, hi = n - 1; + while (lo <= hi) { + int mid = lo + (hi - lo) / 2; + if (mid - tree.Query(mid) >= v[i]) { + pos = mid; + hi = mid - 1; + } else + lo = mid + 1; + } + assert(pos != -1 && ans[pos] == 0); + ans[pos] = i + 1; + tree.Update(pos, 1); + } + for (int x : ans) { + cout << x << '\n'; + } +} diff --git a/ski/aib/78b8953a-137c-11eb-8024-309c234925a0.cpp b/ski/aib/78b8953a-137c-11eb-8024-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3f097f6aa5d9a5bdf894a566251aaa0de439f27 --- /dev/null +++ b/ski/aib/78b8953a-137c-11eb-8024-309c234925a0.cpp @@ -0,0 +1,111 @@ +#include +#include + + +using namespace std; + + + +int aib[30001]; + +int v[30001]; + +int v2[30001]; + + + +int zeros( int x ) { + + return ( x ^ ( x - 1 ) ) & x; + +} + + + +void update( int x, int poz, int n ) { + + while ( poz <= n ) { + + aib[poz] += x; + + poz += zeros( poz ); + + } + +} + + + +int sum( int poz ) { + + int s = 0; + + while ( poz > 0 ) { + + s += aib[poz]; + + poz -= zeros( poz ); + + } + + return s; + +} + + + +int cbin( int x, int n ) { + + int poz = 0, i; + + for ( i = 1 << 15; i > 0; i /= 2 ) { + + if ( poz + i <= n && sum( poz + i ) <= x - 1 ) + + poz += i; + + } + + return poz + 1; + +} + + + +int main() { + + FILE *fin = fopen( "schi.in", "r" ), *fout = fopen( "schi.out", "w" ); + + int n, i; + + fscanf( fin, "%d", &n ); + + for ( i = 1; i <= n; i ++ ) { + + fscanf( fin, "%d", &v[i] ); + + update( 1, i, n ); + + } + + for ( i = n; i > 0; i -- ) { + + v2[cbin( v[i], n )] = i; + + update( -1, cbin( v[i], n ), n ); + + } + + for ( i = 1; i <= n; i ++ ) { + + fprintf( fout, "%d\n", v2[i] ); + + } + + fclose( fin ); + + fclose( fout ); + + return 0; + +} diff --git a/ski/aib/78d52726-137c-11eb-9174-309c234925a0.cpp b/ski/aib/78d52726-137c-11eb-9174-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3df031f05c6c7c1ac1c3bb30d9c3c2b353eb5fd2 --- /dev/null +++ b/ski/aib/78d52726-137c-11eb-9174-309c234925a0.cpp @@ -0,0 +1,106 @@ +#include + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int ans[30000 + 5], v[30000 + 5], n, Sum[30000 + 5]; + + + +void Update(int i, int j) + +{ + + for (int k = i; k <= n; k += (k ^ (k & (k - 1)))) + + Sum[k] += j; + +} + + + +int Query(int i) + +{ + + int rez = 0; + + for (int k = i; k >= 1; k -= (k ^ (k & (k - 1)))) + + rez += Sum[k]; + + return rez; + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; ++i) + + fin >> v[i], Update(i, 1); + + for (int i = n; i >= 1; --i) + + { + + int st = 1, dr = n, sol = -1; + + while (st <= dr) + + { + + int mid = (st + dr) / 2; + + if (Query(mid) >= v[i]) + + { + + sol = mid; + + dr = mid - 1; + + } + + else + + { + + st = mid + 1; + + } + + } + + Update(sol, -1); + + ans[sol] = i; + + } + + for (int i = 1; i <= n; ++i) + + fout << ans[i] << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/78f59fda-137c-11eb-989e-309c234925a0.cpp b/ski/aib/78f59fda-137c-11eb-989e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c5d785a2b76edf2150afaa6c9561f39b56b162 --- /dev/null +++ b/ski/aib/78f59fda-137c-11eb-989e-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include +#include +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,aib[30003],a[30003],sol[30003]; + +int ub(int x) + +{ + + return (x&(-x)); + +} + +void add(int poz,int val) + +{ + + for(int i=poz; i<=n; i+=ub(i))aib[i]+=val; + +} + +int sum(int poz) + +{ + + int s=0; + + for(int i=poz; i>=1; i-=ub(i)) + + s+=aib[i]; + + return s; + +} + +int cb(int x) + +{ + + int st=1,dr=n,ans=2e9,mij=0; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + int nr=sum(mij); + + if(nr==x&&mij>n; + + for(int i=1; i<=n; ++i)aib[i]=ub(i),f>>a[i]; + + for(int i=n; i>=1; i--) + + { + + int poz=cb(a[i]); + + add(poz,-1); + + sol[poz]=i; + + } + + for(int i=1; i<=n; ++i)g< +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int NMax = 30000; + + + +int AIB[NMax + 5], Sol[NMax + 5], V[NMax + 5], N, T; + + + +void Update(int p, int val) + +{ + + for(int i = p; i <= N; i += (i & (-i))) + + AIB[i] += val; + +} + + + +int Sum(int p) + +{ + + int Sol = 0; + + + + for(int i = p; i > 0; i -= (i & (-i))) + + Sol += AIB[i]; + + + + return Sol; + +} + + + +int Find(int k) + +{ + + int Ans = 0; + + + + for(int i = T; i > 0; i >>= 1) + + { + + if(Ans + i <= N && Sum(Ans + i) < k) + + Ans += i; + + } + + return Ans + 1; + +} + + + +int main() + +{ + + fin >> N; + + T = 1 << ((int)log2(N)); + + + + for(int i = 1; i <= N; i++) + + fin >> V[i], Update(i, 1); + + + + for(int i = N, p; i >= 1; i--) + + { + + p = Find(V[i]); + + Sol[p] = i; + + Update(p, -1); + + } + + + + for(int i = 1; i <= N; i++) + + fout << Sol[i] << '\n'; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/ski/aib/7916007a-137c-11eb-9012-309c234925a0.cpp b/ski/aib/7916007a-137c-11eb-9012-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..364e399c1aef625acf129ac5f723f3d833c8e622 --- /dev/null +++ b/ski/aib/7916007a-137c-11eb-9012-309c234925a0.cpp @@ -0,0 +1,106 @@ +#include + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,i,nr,mij,st,dr,x,v[30005],loc[30005],aib[30005],p; + +int ub(int x) + +{ + + return (x&(-x)); + +} + +int sum(int x) + +{ + + int s=0; + + for(int i=x;i>=1;i-=ub(i)) + + s+=aib[i]; + + return s; + +} + +void Add(int x, int val) + +{ + + for(int i=x;i<=n;i+=ub(i)) + + aib[i]+=val; + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + f>>v[i]; + + for(i=1;i<=n;i++) + + Add(i,1); + + for(i=n;i>=1;i--) + + { + + x=v[i]; + + st=1; + + dr=n; + + p=0; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + nr=sum(mij); + + if(x>nr) + + st=mij+1; + + else + + { + + p=mij; + + dr=mij-1; + + } + + } + + loc[p]=i; + + Add(p,-1); + + } + + for(i=1;i<=n;i++) + + g< + + +using namespace std; + +int aint[4 * 30005], clasament[30005], val; +vector v; + +int query( int p, int st, int dr ) { + if( st == dr ) { + return st; + } + int mid = ( st + dr ) / 2; + if( val <= aint[2*p] ) + return query(2*p,st,mid); + else { + val -= aint[2*p]; + return query(2*p+1,mid+1,dr); + } +} + +void update( int p, int st, int dr, int poz ) { + if( st == dr ) { + aint[p] = val; + return ; + } + int mid = (st + dr) / 2; + if( poz <= mid ) + update(2*p,st,mid,poz); + else + update(2*p+1,mid+1,dr,poz); + aint[p] = aint[2*p] + aint[2*p+1]; +} + +int main() { + ifstream fin("schi.in"); + ofstream fout("schi.out"); + int n, i, x; + fin>>n; + for( i = 1; i <= n; i ++ ) { + fin>>x; + v.push_back(x); + val = 1; + update(1,1,n,i); + } + reverse(v.begin(),v.end()); + for( i = 0; i < v.size(); i ++ ) { + val = v[i]; + int poz = query(1,1,n); + val = 0; + update(1,1,n,poz); + clasament[poz] = n - i; + } + for( i = 1; i <= n; i ++ ) + fout< +using namespace std; + + + +FILE *fin = fopen("schi.in", "r"); + +FILE *fout = fopen("schi.out", "w"); + + + +int n,a[30005],aib[30005],i,ans[30005],loc; + + + +void update(int poz) + +{ + + for(int i=poz; i<=n; i+=i&(-i)) + + aib[i]--; + +} + + + +int sum(int poz) + +{ + + int rez = 0; + + + + for(int i=poz; i>=1; i-=i&(-i)) + + rez += aib[i]; + + + + return rez; + +} + + + +int query(int poz) + +{ + + int left = 1; + + int right = n,mid; + + int rez = -1; + + while(left <= right) + + { + + mid = (left+right) /2; + + + + if(sum(mid) >= poz) + + { + + rez = mid; + + right = mid-1; + + } + + else + + left = mid+1; + + } + + + + return rez; + +} + + + +int main() + +{ + + fscanf(fin, "%d", &n); + + for(i=1; i<=n; ++i) + + { + + fscanf(fin, "%d", &a[i]); + + aib[i] = i&(-i); + + } + + + + for(i=n; i>=1; --i) + + { + + loc = query(a[i]); + + ans[loc] = i; + + update(loc); + + } + + + + for(i=1; i<=n; ++i) + + fprintf(fout, "%d\n", ans[i]); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/ski/aib/79ce7f5c-137c-11eb-8cb4-309c234925a0.cpp b/ski/aib/79ce7f5c-137c-11eb-8cb4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7363c7f0ec1d0ac7970450ab17a2cfb584ddf7cb --- /dev/null +++ b/ski/aib/79ce7f5c-137c-11eb-8cb4-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int NMAX = 300001; + + + +int N; + +int tree[NMAX]; + +int C[NMAX]; + + + +stack S; + + + +void Update(int pos, int val) + +{ + + while( pos <= N ) + + { + + tree[pos] += val; + + pos += (pos & -pos); + + } + +} + + + +int Query(int pos) + +{ + + int sum = 0; + + + + while( pos > 0 ) + + { + + sum += tree[pos]; + + pos -= (pos & - pos); + + } + + + + return sum; + +} + + + +int BinSearch(int lf, int rg, int val) + +{ + + if(lf > rg) return 2000000000; + + + + int mid = (lf + rg) / 2; + + + + int m = Query( mid ); + + + + if(m < val) return BinSearch(mid + 1, rg, val); + + if(m == val) return min(mid, BinSearch(lf, mid - 1, val)); + + if(m > val) return BinSearch(lf, mid - 1, val); + +} + +void Read() + +{ + + fin >> N; + + + + int x; + + for(int i = 1; i <= N; ++i) + + { + + fin >> x; + + S.push(x); + + Update(i, 1); + + } + + int y = N; + + while(!S.empty()) + + { + + x = S.top(); + + S.pop(); + + int pos = BinSearch(1, N, x); + + C[pos] = y; + + Update(pos, -1); + + y--; + + } + + + + for(int i = 1; i <= N; ++i) + + fout << C[i] << '\n'; + +} + +int main() + +{ + + Read(); + + return 0; + +} diff --git a/ski/aib/79dcb01c-137c-11eb-8a5a-309c234925a0.cpp b/ski/aib/79dcb01c-137c-11eb-8a5a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aef319237fadfca1bee7b93c064cb762f33f616b --- /dev/null +++ b/ski/aib/79dcb01c-137c-11eb-8a5a-309c234925a0.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +int v[30005], t[30005], c[30005], n; + +int query(int i) + +{ + + int s = 0; + + for(i = i; i > 0; i -= (i & (-i))) + + s += t[i]; + + return s; + +} + + + +void update(int i, int val) + +{ + + for(i = i; i <= n; i += (i & (-i))) + + t[i] += val; + +} + + + +void citire() + +{ + + in >> n; + + for(int i = 1; i <= n; i++) + + { + + in >> v[i]; + + update(i, 1); + + } + +} + + + +void f() + +{ + + for(int i = n; i >= 1; i--) + + { + + int st = 0, dr = n, rez = 0; + + while(st <= dr) + + { + + int mij = (st + dr) / 2; + + if(query(mij) < v[i]) + + st = mij + 1, rez = mij; + + else + + dr = mij - 1; + + } + + rez++; + + c[rez] = i; + + update(rez, -1); + + } + + for(int i = 1; i <= n; i++) + + out << c[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + f(); + + return 0; + +} diff --git a/ski/aib/7a090d90-137c-11eb-ae42-309c234925a0.cpp b/ski/aib/7a090d90-137c-11eb-ae42-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..338398db3d3064f6d1819fec822faabd6f158880 --- /dev/null +++ b/ski/aib/7a090d90-137c-11eb-ae42-309c234925a0.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using std::vector; + +std::ifstream fin("schi.in"); + +std::ofstream fout("schi.out"); + + + +class FenTree { + + private: + + int n; + + vector bit; + + + + static inline int lsb(int x) { + + return x & (-x); + + } + + + + public: + + FenTree(int n) { + + this->n = n; + + bit.resize(n + 1); + + } + + + + void update(int pos, int val) { + + for (int i = pos; i <= n; i += lsb(i)) + + bit[i] += val; + + } + + + + int query(int pos) { + + int sum = 0; + + for (int i = pos; i >= 1; i -= lsb(i)) + + sum += bit[i]; + + return sum; + + } + + + + int query(int left, int right) { + + return query(right) - query(left - 1); + + } + + + + int search(int val) { + + int md, lo = 0, hi = n + 1; + + while (hi - lo > 1) { + + md = (lo + hi) >> 1; + + if (query(md) < val) + + lo = md; + + else + + hi = md; + + } + + return query(hi) == val ? hi : -1; + + } + +}; + + + +int main() { + + int n; fin >> n; + + vector pos(n + 1); + + + + for (int i = 1; i <= n; i++) + + fin >> pos[i]; + + + + FenTree bit(n); + + for (int i = 1; i <= n; i++) + + bit.update(i, 1); + + + + vector sol(n + 1); + + for (int i = n; i >= 1; i--) { + + int ind = bit.search(pos[i]); + + bit.update(ind, -1); + + sol[ind] = i; + + } + + + + for (int i = 1; i <= n; i++) + + fout << sol[i] << '\n'; + + + + fout.close(); + + return 0; + +} diff --git a/ski/aib/7a19c78c-137c-11eb-b59a-309c234925a0.cpp b/ski/aib/7a19c78c-137c-11eb-b59a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f43d68e18835150d2c26df8e63388c56288743b7 --- /dev/null +++ b/ski/aib/7a19c78c-137c-11eb-b59a-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout ("schi.out"); + + + +int N; + + + +int AIB[1 + 30000 + 5]; + +int A[1 + 30000 + 5]; + +int Answerts[1 + 30000 + 5]; + + + +inline int query (int value) + +{ + + int answer, sum; + + sum = answer = 0; + + int pow = (1 << 15); + + while (0 <= pow) + + { + + if (pow + answer <= N && sum + AIB[pow + answer] < value) + + answer += pow, sum += AIB[answer]; + + if (pow == 0) + + break; + + pow >>= 1; + + } + + return 1 + answer; + +} + + + +inline void update (int poz, int value) + +{ + + while (poz <= N) + + AIB[poz] += value, poz += ((-poz) & poz); + +} + + + +int main() + +{ + + int length = 0; + + fin >> N; + + for (int i = 1; i <= N; ++i) + + { + + int x; + + fin >> x; + + A[++length] = x; + + update (i, 1); + + } + + while (length) + + { + + int poz = query (A[length]); + + Answerts[poz] = length; + + update (poz, -1); + + --length; + + } + + for (int i = 1; i <= N; ++i) + + fout << Answerts[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/7a284678-137c-11eb-a0d0-309c234925a0.cpp b/ski/aib/7a284678-137c-11eb-a0d0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c695274f091aba62dcbb39fcb4595910544918d4 --- /dev/null +++ b/ski/aib/7a284678-137c-11eb-a0d0-309c234925a0.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + +int N, List[30001], i, j, Sol[30001]; + +class BIT{ + + int V[30001]; + + int Get(int target){ + + int out=0; + + for(j=target; j>=1; j-=j&(-j)) out+=V[j]; + + return out; + + } + + int BS(int val, int S, int D){ + + int mid=(S+D)/2; + + int x=Get(mid); + + if(x==val && Sol[mid]==0) return mid; + + if(x=1; --i){ + + int x=Arb.Find(List[i]); + + Sol[x]=i; + + Arb.Add(-1, x); + + } + + for(i=1; i<=N; ++i) printf("%d\n", Sol[i]); + + return 0; + +} diff --git a/ski/aib/7a369e3a-137c-11eb-8152-309c234925a0.cpp b/ski/aib/7a369e3a-137c-11eb-8152-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f82d55ba5b6b505b51a9f89068ca4e12811daa7b --- /dev/null +++ b/ski/aib/7a369e3a-137c-11eb-8152-309c234925a0.cpp @@ -0,0 +1,110 @@ +#include + + + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int AIB[30100],n; + + + +void adaug(int poz,int val) + +{ + + int i; + + for(i=poz;i<=n;i+=(i&(-i))) + + AIB[i]+=val; + +} + + + +int nr_de0(int poz) + +{ + + int i,nr0=0; + + for(i=poz;i>=1;i-=(i&(-i))) + + nr0+=AIB[i]; + + return nr0; + +} + + + + + +int i,x,p,u,mij,cp,pozitie,v[30100],sol[30100]; + + + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>v[i]; + + adaug(i,1); + + } + + + + for(i=n;i>=1;i--) + + { + + p=1;u=n;x=v[i];cp=0; + + + + while(p<=u) + + { + + mij=(p+u)/2; + + if(nr_de0(mij)>=x){cp=mij;u=mij-1;} + + else {p=mij+1;} + + } + + sol[cp]=i; + + adaug(cp,-1); + + } + + + + for(i=1;i<=n;i++) + + g< +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +int v[30005], t[30005], c[30005], n; + +int query(int i) + +{ + + int s = 0; + + for(i = i; i > 0; i -= (i & (-i))) + + s += t[i]; + + return s; + +} + + + +void update(int i, int val) + +{ + + for(i = i; i <= n; i += (i & (-i))) + + t[i] += val; + +} + + + +void citire() + +{ + + in >> n; + + for(int i = 1; i <= n; i++) + + { + + in >> v[i]; + + update(i, 1); + + } + +} + + + +void f() + +{ + + for(int i = n; i >= 1; i--) + + { + + int st = 0, dr = n, rez = 0; + + while(st <= dr) + + { + + int mij = (st + dr) / 2; + + if(query(mij) < v[i]) + + st = mij + 1, rez = mij; + + else + + dr = mij - 1; + + } + + rez++; + + c[rez] = i; + + update(rez, -1); + + } + + for(int i = 1; i <= n; i++) + + out << c[i] << '\n'; + +} + + + +int main() + +{ + + citire(); + + f(); + + return 0; + +} diff --git a/ski/aib/7ac4cc3e-137c-11eb-9540-309c234925a0.cpp b/ski/aib/7ac4cc3e-137c-11eb-9540-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a14ee042410bcef98dffeabf20b0110dddc9d17f --- /dev/null +++ b/ski/aib/7ac4cc3e-137c-11eb-9540-309c234925a0.cpp @@ -0,0 +1,176 @@ +#include + + +using namespace std; + + + +ifstream fin( "schi.in" ); + +ofstream fout( "schi.out" ); + + + +const int NMAX = 30005; + +const int INF = 2000000000; + + + +int N; + +int tree[NMAX]; + + + +int a[NMAX]; + + + +void Read() + +{ + + fin >> N; + + + + for( int i = 1; i <= N; ++i ) + + fin >> a[i]; + + + + fin.close(); + +} + + + +void Update( int pos, int val ) + +{ + + while( pos <= N ) + + { + + tree[pos] += val; + + pos += pos & -pos; + + } + +} + + + +int Query( int pos ) + +{ + + int sum = 0; + + + + while( pos > 0 ) + + { + + sum += tree[pos]; + + pos -= pos & -pos; + + } + + + + return sum; + +} + + + +int BinSearch( int lf, int rg, int val ) + +{ + + if( lf > rg ) return INF; + + + + int mid = ( lf + rg ) / 2; + + int q = Query( mid ); + + + + if( q == val ) return min( mid, BinSearch( lf, mid - 1, val ) ); + + + + if( q < val ) return BinSearch( mid + 1, rg, val ); + + else return BinSearch( lf, mid - 1, val ); + +} + + + +void Do() + +{ + + for( int i = 1; i <= N; ++i ) + + Update( i, 1 ); + + + + int sol[NMAX]; + + + + for( int i = N; i >= 1; --i ) + + { + + int p = BinSearch( 1, N, a[i] ); + + + + sol[p] = i; + + + + Update( p, -1 ); + + } + + + + for( int i = 1; i <= N; ++i ) + + fout << sol[i] << '\n'; + + + + fout.close(); + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/ski/aib/7af1e90a-137c-11eb-94ff-309c234925a0.cpp b/ski/aib/7af1e90a-137c-11eb-94ff-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6b3f1e393a3bb44e47987c4580edc5f857203817 --- /dev/null +++ b/ski/aib/7af1e90a-137c-11eb-94ff-309c234925a0.cpp @@ -0,0 +1,146 @@ +#include +#include +#include + + +using namespace std; + + + +class AIB + +{ + +public: + + AIB(int n) + + { + + this->n = n; + + v.resize(n+1); + + } + + void Add(int ind, int val) + + { + + while(ind <= n) + + { + + v[ind] += val; + + ind += ind & (-ind); + + } + + } + + int Query(int dr) + + { + + int ret = 0; + + while(dr >= 1) + + { + + ret += v[dr]; + + dr -= dr & (-dr); + + } + + return ret; + + } + + int GetFirstPos(int sum) + + { + + int ret; + + int step; + + for(step = 1; step < n; step <<= 1); + + for(ret = 0; step > 0; step >>= 1) + + if(ret + step < n && sum > v[ret+step]) + + ret += step, sum -= v[ret]; + + return ret+1; + + } + +private: + + int n; + + vector v; + +}; + + + +int main() + +{ + + ifstream in("schi.in"); + + int n; + + in >> n; + + vector v(n+1); + + AIB aib(n); + + for(int i = 1; i <= n; ++i) + + { + + in >> v[i]; + + aib.Add(i, 1); + + } + + in.close(); + + + + vector rasp(n+1); + + for(int i = n; i >= 1; --i) + + { + + int pos = aib.GetFirstPos(v[i]); + + rasp[pos] = i; + + aib.Add(pos, -1); + + } + + + + ofstream out("schi.out"); + + for(int i = 1; i <= n; ++i) + + out << rasp[i] << "\n"; + + out.close(); + + return 0; + +} diff --git a/ski/aib/7affe5ee-137c-11eb-b551-309c234925a0.cpp b/ski/aib/7affe5ee-137c-11eb-b551-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..984ec6e58ee7b47080bfbd3f47f917b625a4146e --- /dev/null +++ b/ski/aib/7affe5ee-137c-11eb-b551-309c234925a0.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N; + +int bit[30005], ans[30005]; + +stack S; + + + +void update(int pos, int val) + +{ + + for(; pos <= N; pos += ((pos ^ (pos - 1)) & pos)) + + bit[pos] += val; + +} + + + +int query(int X) + +{ + + int ret = 0; + + int sum = 0; + + for(int i = 14; i >= 0; i--) + + if(ret + (1 << i) <= N && sum + bit[ret + (1 << i)] < X) + + ret += (1 << i), sum += bit[ret]; + + return ret + 1; + +} + + + +int main() + +{ + + fin >> N; + + for(int i = 1; i <= N; i++) + + { + + int x; + + fin >> x; + + S.push(x); + + update(i, 1); + + } + + for(; !S.empty(); S.pop()) + + { + + int pos = query(S.top()); + + ans[pos] = S.size(); + + update(pos, -1); + + } + + for(int i = 1; i <= N; i++) + + fout << ans[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/7b355e0c-137c-11eb-b28f-309c234925a0.cpp b/ski/aib/7b355e0c-137c-11eb-b28f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd2f8401eb7465813e1da965abe6aeb91c39b29f --- /dev/null +++ b/ski/aib/7b355e0c-137c-11eb-b28f-309c234925a0.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; + +int j,b,p,a,i,v[1001*1001],n,aib[1001*1001]; + +int ssol[30001]; + +int sol(int x){ + +int i; + +int rez=0; + +for(i=x;i>0;i-=(i&(-i))) + + rez+=aib[i]; + +return rez; + + + +} + +void add(int x,int val){ + +int i; + +for(i=x;i<=n;i+=(i&(-i))) aib[i]+=val; + + + +} + +int ok=0; + +int cautbin(int x){ + + int valoare,st=1,dr=n; + +int mij; + +while(st<=dr){ + + mij=(st+dr)/2; + + int ssol=sol(mij); + + if(ssol==x) valoare=mij; + + if(ssol>=x) dr=mij-1; + + else st=mij+1; + + + + + +} + +return valoare; + + + +} + +int main() + +{ + + + + freopen("schi.in","r",stdin); + + freopen("schi.out","w",stdout); + + + + + + scanf("%d",&n); + + for(i=1;i<=n;i++) aib[i]=(i&(-i)); + + for(i=1;i<=n;i++)scanf("%d",&v[i]); + + int k; + +for(j=n;j>=1;j--){ + +k=cautbin(v[j]); ssol[k]=j; + +add(k,-1); + + + + + +} + +for(i=1;i<=n;i++) printf("%d\n",ssol[i]); + + return 0; + +} diff --git a/ski/aib/7b44b212-137c-11eb-8d72-309c234925a0.cpp b/ski/aib/7b44b212-137c-11eb-8d72-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b1f89a0784152bfb8686de2c459d56a9f538a78 --- /dev/null +++ b/ski/aib/7b44b212-137c-11eb-8d72-309c234925a0.cpp @@ -0,0 +1,134 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int N=30009; + +int n; + +int v[N],aib[N],a[N]; + +void update(int x,int y) + +{ + + while(x<=n) + + { + + aib[x]+=y; + + x+=x&(-x); + + } + +} + +int query(int x) + +{ + + int sum=0; + + while(x) + + { + + sum+=aib[x]; + + x-=x&(-x); + + } + + return sum; + +} + +int bin(int st,int dr,int ans) + +{ + + int sol; + + while(st<=dr) + + { + + int mij=(st+dr)/2; + + int val=query(mij); + + if(val==ans) + + sol=mij; + + if(val>=ans) + + dr=mij-1; + + else + + st=mij+1; + + } + + return sol; + +} + +void read() + +{ + + fin.sync_with_stdio(false); + + fin>>n; + + for(int i=1;i<=n; i++) + + fin>>v[i]; + +} + +void solve() + +{ + + for(int i=1;i<=n;i++) + + update(i,1); + + for(int i=n;i>=1;i--) + + { + + int ans=bin(1,n,v[i]); + + a[ans]=i; + + update(ans,-1); + + } + + for(int i=1;i<=n;i++) + + fout< +#include +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int n, x, a[30001], v[30001], s[30001]; + + + +void arb(int p) + +{ + + for (int i = p; i <= n; i += (i & (-i))) + + a[i] --; + +} + + + +int sum(int p) + +{ + + int y = 0; + + for (int i = p; i > 0; i -= (i & (-i))) + + y += a[i]; + + return y; + +} + + + +int ctb(int x) + +{ + + int st = 1, dr = n, mij = 0, ss = 0, mini = 30010; + + while (st <= dr) + + { + + mij = (st + dr) / 2; + + ss = sum(mij); + + if (ss == x && mij < mini) mini = mij; + + else if (ss >= x) dr = mij - 1; + + else st = mij + 1; + + } + + return mini; + +} + + + +int main() + +{ + + freopen("schi.in","r",stdin); + + freopen("schi.out","w",stdout); + + scanf("%d",&n); + + for (int i = 1; i <= n; i ++) + + scanf("%d",&v[i]); + + for (int i = 1; i <= n; i ++) + + a[i] = i & (-i); + + + + for (int i = n; i >= 1; i --) + + { + + x = ctb(v[i]); + + s[x] = i; + + arb(x); + + } + + + + for (int i = 1; i <= n; i ++) + + printf("%d\n",s[i]); + +} diff --git a/ski/aib/7b63fc64-137c-11eb-8182-309c234925a0.cpp b/ski/aib/7b63fc64-137c-11eb-8182-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eca571429e4b90e9e627c8a229d429f3f4a3e4ee --- /dev/null +++ b/ski/aib/7b63fc64-137c-11eb-8182-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +const int MAX = 3 * 1e4 + 5; + + + +int n, pas = 1; + +int tree[MAX], v[MAX], ans[MAX]; + + + +int Query(int idx) + +{ + + int sum = 0; + + for(; idx; idx -= (idx & (-idx))) + + sum += tree[idx]; + + return sum; + +} + + + +void Update(int idx, int val) + +{ + + for(; idx <= n; idx += (idx & (-idx))) + + tree[idx] += val; + +} + + + +int main() + +{ + + f >> n; + + + + for(; pas <= n; pas <<= 1); + + for(int i = 1; i <= n; ++i) + + { + + f >> v[i]; + + Update(i, 1); + + } + + + + for(int i = n; i >= 1; --i) + + { + + int k = 0, step = pas; + + for(; step; step >>= 1) + + if(k + step <= n && Query(k + step) < v[i]) + + k += step; + + ans[k + 1] = i; + + Update(k + 1, -1); + + } + + + + for(int i = 1; i <= n; ++i) + + g << ans[i] << "\n"; + + + + return 0; + +} diff --git a/ski/aib/7b725664-137c-11eb-b502-309c234925a0.cpp b/ski/aib/7b725664-137c-11eb-b502-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b8f98dd6f5d3ae0acaadf499ac56fe1e245b16a --- /dev/null +++ b/ski/aib/7b725664-137c-11eb-b502-309c234925a0.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int NMAX=30000; + +int sol[NMAX+5], aib[NMAX+5], cit[NMAX+5]; + +int n; + +void update(int val, int poz) + +{ + + for(;poz<=n;poz=poz+(poz&(-poz))) + + aib[poz]=aib[poz]+val; + +} + +int query(int poz) + +{ + + int s=0; + + for(;poz>0;poz=poz-(poz&(-poz))) + + s=aib[poz]+s; + + return s; + +} + +int cautare_binara(int val) + +{ + + int st=1, dr=n, mij, valq, sol=n+1; + + while(st<=dr) + + { + + mij=(st+dr)>>1; + + valq=query(mij); + + if(val==valq) + + { + + sol=min(sol, mij); + + dr=mij-1; + + continue; + + } + + if(valq>n; + + for(i=1;i<=n;i++) + + update(1, i); + + for(i=1;i<=n;i++) + + fin>>cit[i]; + + for(j=n;j>=1;j--) + + { + + i=cautare_binara(cit[j]); + + update(-1, i); + + sol[i]=j; + + } + + for(i=1;i<=n;i++) + + fout< +#include + + + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +const int NMAX = 30005; + +int v[NMAX],rasp[NMAX]; + +int init[NMAX]; + +int n; + + + +void Update(int poz,int x) + +{ + + for(int i=poz;i<=n;i+=(i & -i)) v[i]+=x; + +} + + + +int Suma(int poz) + +{ + + int sum=0; + + for(int i=poz;i>=1;i-=(i & -i)) sum+=v[i]; + + return sum; + +} + + + +int main() + +{ + + fin >> n; + + for(int i=1;i<=n;i++) + + { + + fin >> init[i]; + + Update(i,1); + + } + + int maxim=(n & -n)*2; + + for(int i=n;i>=1;i--) + + { + + int st=0,dr=n,mij; + + int k; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + if(Suma(mij) + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +int a[30005],r[30005],v[30005],n; + +void upd(int i){ + + while(i<=n){ + + ++a[i]; + + i+=(i&(-i)); + + } + +} + +int q(int i){ + + int s=0; + + while(i){ + + s+=a[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main(){ + + int i,x,y,aux,x1,y1,aux1; + + in>>n; + + for(i=1; i<=n; ++i) + + in>>v[i]; + + for(i=n; i>0; --i){ + + y=v[i]; + + x=q(y)+y; + + if(x-y){ + + x1=q(x), y1=x-y; + + while(x1!=y1){ + + aux=x, aux1=x1; + + x+=x1-y1; + + x1=q(x); + + y=aux, y1=aux1; + + } + + } + + r[x]=i; + + upd(x); + + } + + for(i=1; i<=n; ++i) + + out< +#include +using namespace std; + + + + + +inline int zeros (int x){ + + return x & (-x); + +} + + + +int aib[30300],i,n,v[30300],ans[30300],k; + + + +void update (int poz, int x){ + + for (int i = poz; i < n; i += zeros(i)) + + aib[i] += x; + +} + + + +int compute (int poz) { + + int ans = 0; + + for (int i = poz; i > 0; i -= zeros(i)) + + ans += aib[i]; + + return ans; + +} + + + +int bs (int val) { + + int poz = 0; + + for (int i = n - 1; i > 0; i >>=1) + + while (poz + i < n && compute(poz + i) < val) + + poz += i; + + return poz + 1; + +} + +int main() + +{ + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + fin >> n; + + ++n; + + for (i = 1; i < n; ++i){ + + fin >> v[i]; + + aib[i] = zeros(i); + + } + + for (i = n - 1; i > 0; --i){ + + k = bs(v[i]); + + ans[k] = i; + + update(k, -1); + + } + + for (i = 1; i < n; ++i) + + fout << ans[i] << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/ski/aib/7bfc80c0-137c-11eb-9540-309c234925a0.cpp b/ski/aib/7bfc80c0-137c-11eb-9540-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49e446541cb491e23fa7af987e1006af8ba7a33c --- /dev/null +++ b/ski/aib/7bfc80c0-137c-11eb-9540-309c234925a0.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int i,k,x,n,u[30001],w[30001],v[30001],p; + +void Add(int k, int x) + +{ + + int i; + + for (i = k; i <= n; i += ( (i ^ (i - 1)) & i )) + + v[i]+=x; + +} + +int Compute(int k) + +{ + + int i, ret=0; + + for (i = k; i > 0; i -= ( (i ^ (i - 1)) & i )) + + ret+=v[i]; + + return ret; + +} + +int cautbin(int st,int dr,int k) + +{ + + int x,p,mij; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + x=Compute(mij); + + if(x>=u[k]) + + { + + p=mij; + + dr=mij-1; + + } + + else + + { + + st=mij+1; + + } + + } + + return p; + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>u[i]; + + Add(i,1); + + } + + for(i=n;i>=1;i--) + + { + + p=cautbin(1,n,i); + + w[p]=i; + + Add(p,-1); + + } + + for(i=1;i<=n;i++) + + g< + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +int aib[30001],i,n,v[30001],ans[30001],k; + +void Add(int x, int quantity) + +{ + + int i; + + for (i=x; i <= n; i += ( (i ^ (i - 1)) & i )) + + { + + aib[i] += quantity; + + } + +} + +int Compute(int x) + +{ + + int i, ret = 0; + + for (i = x; i > 0; i -= ( (i ^ (i - 1)) & i )) + + { + + ret += aib[i]; + + } + + return ret; + +} + +int bs (int val) + +{ + + int poz = 0; + + for (int i = n - 1; i > 0; i >>=1) + + { + + while (poz + i < n && Compute(poz + i) < val) + + { + + poz += i; + + } + + } + + return poz + 1; + +} + +int main() + +{ + + in >> n; + + n+=1; + + for (i = 1; i < n; i++) + + { + + in >> v[i]; + + aib[i] = ( (i ^ (i - 1)) & i ); + + } + + for (i = n - 1; i > 0; i--) + + { + + k = bs(v[i]); + + ans[k] = i; + + Add(k, -1); + + } + + for (i = 1; i < n; i++) + + { + + if(i%1==0) + + { + + out< +#include + + +using namespace std; + +int aib[30001],index[30001],v[30001],N,poz; +void update(int poz,int val) + +{ + + int i; + + for(i=poz;i<=N;i+=(i&(-i))) + + { + + aib[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int s=0,i; + + for(i=poz;i>=1;i-=(i&(-i))) + + { + + s+=aib[i]; + + } + + return s; + +} + +int cautare(int st,int dr,int k) + +{ + int val,poz,mij; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + val=sum(mij); + + + + if(val>=v[k]) + + { + + poz=mij; + + dr=mij-1; + + } + + else + + { + + st=mij+1; + + } + + } + + return poz; + +} + +int main() + +{ + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + int i; + + fin>>N; + + for(i=1;i<=N;i++) + + { + + fin>>v[i]; + + update(i,1); + + } + + for(i=N;i>=1;i--) + + { + + poz=cautare(1,N,i); + + index[poz]=i; + + update(poz,-1); + + } + + for(i=1;i<=N;i++) + + { + + fout< +#include + + +using namespace std; + + + +const int N = 30002; + + + +int AIB[N], *v, *sol; + + + +void Update ( int poz, int x ) + +{ + + while ( poz < N ) + + { + + AIB[poz] += x; + + poz = poz + (poz & (-poz)); + + } + +} + + + +int Suma (int poz) + +{ + + int s = 0, i = poz; + + while (i > 0) + + { + + s += AIB[i]; + + i = i - (i & (-i)); + + } + + return s; + +} + + + + + +int Cautare(int st,int dr,int p,int x) + +{ + + if ( st > dr ) + + return p; + + int m = (st + dr)/2; + + if(Suma(m) >= x) + + { + + p = m; + + dr = m - 1; + + } + + else + + st = m + 1; + + return Cautare(st, dr,p, x); + +} + + + +int main() + +{ + + ifstream in ("schi.in"); + + ofstream out ("schi.out"); + + + + int poz, n, i; + + + + in >> n; + + v = new int [n + 1]; + + sol = new int [n + 1]; + + for ( i = 1; i <= n; i++ ) + + { + + Update(i, 1); + + in >> v[i]; + + } + + + + for(i = n ; i > 0; i--) + + { + + poz = Cautare (1, n,0, v[i]); + + sol[poz] = i; + + Update(poz, -1); + + } + + + + for(i = 1; i <= n; i++) + + out << sol[i] << '\n'; + + + + delete( v ); + + delete( sol ); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/ski/aib/7c8f9db0-137c-11eb-be4a-309c234925a0.cpp b/ski/aib/7c8f9db0-137c-11eb-be4a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0b9c192f1c15d6b8dedb71caf44e859e8e77519b --- /dev/null +++ b/ski/aib/7c8f9db0-137c-11eb-be4a-309c234925a0.cpp @@ -0,0 +1,129 @@ +#include +#include + + +using namespace std; + +int aib[30001],index[30001],v[30001],N,poz; + +void update(int poz,int val) + +{ + + int i; + + for(i=poz;i<=N;i+=(i&(-i))) + + { + + aib[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int s=0,i; + + for(i=poz;i>=1;i-=(i&(-i))) + + { + + s+=aib[i]; + + } + + return s; + +} + +int cautare(int st,int dr,int k) + +{ + + int val,poz,mij; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + val=sum(mij); + + if(val>=v[k]) + + { + + poz=mij; + + dr=mij-1; + + } + + else + + { + + st=mij+1; + + } + + } + + return poz; + +} + +int main() + +{ + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + int i; + + fin>>N; + + for(i=1;i<=N;i++) + + { + + fin>>v[i]; + + update(i,1); + + } + + for(i=N;i>=1;i--) + + { + + poz=cautare(1,N,i); + + index[poz]=i; + + update(poz,-1); + + } + + for(i=1;i<=N;i++) + + { + + fout< +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +int n,a[30002],loc[30002],fin[30002]; + +void update(int i) + +{ + + while(i<=n) + + { + + a[i]++; + + i+=(i&(-i)); + + } + +} + +int q(int i) + +{ + + int s=0; + + while(i) + + { + + s+=a[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + int i,nr,nrr; + + in>>n; + + for(i=1;i<=n;i++) + + in>>loc[i]; + + for(i=n;i>0;i--) + + { + + nr=q(loc[i]); + + if(nr) + + { + + nrr=q(loc[i]+nr); + + while(nr!=nrr) + + nr=nrr,nrr=q(loc[i]+nrr); + + } + + loc[i]+=nr; + + fin[loc[i]]=i; + + update(loc[i]); + + } + + for(i=1;i<=n;i++) + + out< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +int AIB[30001]; + +int N, A[30001], B[30001], i, step, pos; + + + +void Update(int poz, int val) { + + while (poz <= N) { + + AIB[poz] += val; + + poz = poz + (poz & (-poz)); + + } + +} + + + +int Querry(int x, int step) { + + int sol = 0; + + while (step) { + + if (step + sol <= N && AIB[step + sol] < x) { + + x -= AIB[step + sol]; + + sol += step; + + } + + step >>= 1; + + } + + return sol + 1; + +} + +int main() { + + f >> N; + + for (i = 1; i <= N; ++i) { + + Update(i, 1); + + f >> A[i]; + + } + + step = 1; + + while (step <= N) step <<= 1; + + step >>= 1; + + for (i = N; i >= 1; --i) { + + pos = Querry(A[i], step); + + Update(pos, -1); + + B[pos] = i; + + } + + for (i = 1; i <= N; ++i) + + g << B[i] << '\n'; + +} diff --git a/ski/aib/7cca0ddc-137c-11eb-9510-309c234925a0.cpp b/ski/aib/7cca0ddc-137c-11eb-9510-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f350043e7296851f5296c7c3a834621faad0066 --- /dev/null +++ b/ski/aib/7cca0ddc-137c-11eb-9510-309c234925a0.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +void actualizeaza(int start, int b, int n, int *AIB) { + + for (int i = start; i <= n; i += i & (-i)) + + AIB[i] += b; + +} + + + +int interogheaza(int start, int *AIB) { + + int suma = 0; + + while (start > 0) { + + suma += AIB[start]; + + start -= start & (-start); + + } + + return suma; + +} + + + +void prelucreaza(int pos, int ind, int *AIB, int *finish, int n) { + + int left = 1, right = n, mid, q, sol; + + while (left <= right) { + + mid = left + (right - left) / 2; + + q = interogheaza(mid, AIB); + + if (mid - q == pos) { + + sol = mid; + + right = mid - 1; + + continue; + + } + + if (mid - q > pos) + + right = mid - 1; + + else + + left = mid + 1; + + } + + actualizeaza(sol, 1, n, AIB); + + finish[sol] = ind; + +} + + + +int main() { + + freopen("schi.in", "r", stdin); + + freopen("schi.out", "w", stdout); + + int n; + + scanf("%d", &n); + + int A[n + 1], AIB[n + 1] = {0}, finish[n + 1] = {0}; + + for (int i = 1; i <= n; i++) + + scanf("%d", &A[i]); + + for (int i = n; i >= 1; i--) { + + prelucreaza(A[i], i, AIB, finish, n); + + } + + for (int i = 1; i <= n; i++) + + printf("%d\n", finish[i]); + + return 0; + +} diff --git a/ski/aib/7ce6f3cc-137c-11eb-84c0-309c234925a0.cpp b/ski/aib/7ce6f3cc-137c-11eb-84c0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae28c110dea2956f3a1393d559373e3af8a8d9d6 --- /dev/null +++ b/ski/aib/7ce6f3cc-137c-11eb-84c0-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +FILE *fin=freopen("schi.in","r",stdin); + +FILE *fout=freopen("schi.out","w",stdout); + + + +int AIB[30030], v[30030], ans[30030]; + +int n; + + + +inline void Add(int pos, int val) + +{ + + int i; + + for(i = pos; i <= n; i += ((i ^ (i - 1)) & i)) + + AIB[i] += val; + +} + + + +inline int Query(int pos) + +{ + + int i, ret = 0; + + for(i = pos; i; i -= ((i ^ (i - 1)) & i)) + + ret += AIB[i]; + + return ret; + +} + + + +int main() + +{ + + int i, j, p2, sol; + + scanf("%d", &n); + + for(i = 1; i <= n; ++i) + + { + + scanf("%d", &v[i]); + + Add(i, 1); + + } + + + + for(p2 = 1; p2 < n; p2 <<= 1); + + + + for(i = n; i; --i) + + { + + sol = 0; + + for(j = p2; j; j >>= 1) + + if(j + sol <= n && Query(sol + j) < v[i]) + + sol += j; + + ans[++sol] = i; + + Add(sol, -1); + + } + + + + + + for(i = 1; i <= n; ++i) + + printf("%d\n", ans[i]); + + return 0; + +} diff --git a/ski/aib/7cf5a864-137c-11eb-bdae-309c234925a0.cpp b/ski/aib/7cf5a864-137c-11eb-bdae-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f13a6ef696771b26c5f3c0340a71e8aabe57e355 --- /dev/null +++ b/ski/aib/7cf5a864-137c-11eb-bdae-309c234925a0.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + + + +const int N = 30002; + + + +int AIB[N], *v, *sol; + + + +void Update ( int poz, int x ) + +{ + + while ( poz < N ) + + { + + AIB[poz] += x; + + poz = poz + (poz & (-poz)); + + } + +} + + + +int Suma (int poz) + +{ + + int s = 0, i = poz; + + while (i > 0) + + { + + s += AIB[i]; + + i = i - (i & (-i)); + + } + + return s; + +} + + + + + +int Cautare(int st,int dr,int p,int x) + +{ + + if ( st > dr ) + + return p; + + int m = (st + dr)/2; + + if(Suma(m) >= x) + + { + + p = m; + + dr = m - 1; + + } + + else + + st = m + 1; + + return Cautare(st, dr,p, x); + +} + + + +int main() + +{ + + ifstream in ("schi.in"); + + ofstream out ("schi.out"); + + + + int poz, n, i; + + + + in >> n; + + v = new int [n + 1]; + + sol = new int [n + 1]; + + for ( i = 1; i <= n; i++ ) + + { + + Update(i, 1); + + in >> v[i]; + + } + + + + for(i = n ; i > 0; i--) + + { + + poz = Cautare (1, n,0, v[i]); + + sol[poz] = i; + + Update(poz, -1); + + } + + + + for(i = 1; i <= n; i++) + + out << sol[i] << '\n'; + + + + delete( v ); + + delete( sol ); + + + + in.close(); + + out.close(); + + + + return 0; + +} diff --git a/ski/aib/7d050cc0-137c-11eb-9f6e-309c234925a0.cpp b/ski/aib/7d050cc0-137c-11eb-9f6e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f63669049fe830627237047750e97abeac55433 --- /dev/null +++ b/ski/aib/7d050cc0-137c-11eb-9f6e-309c234925a0.cpp @@ -0,0 +1,69 @@ +#include + +using namespace std; + +int aib[30010], v[30010]; +int n; + +void update(int poz, int val) +{ + for (int i = poz; i <= n; i += (i & -i)) + aib[i] += val; +} + +int sum(int poz) +{ + int s = 0, i; + for (i = poz; i >= 1; i -= (i & -i)) + s += aib[i]; + + return s; +} + +int caut(int st, int dr, int p) +{ + int val, mij, poz; + while(st <= dr) + { + mij = (st + dr) / 2; + val = sum(mij); + if(val >= v[p]) + { + poz = mij; + dr = mij - 1; + } + else + { + st = mij + 1; + } + } + return poz; +} +int final[30010]; +int main() +{ + ifstream f("schi.in"); + ofstream g("schi.out"); + f>>n; + + int i, p; + + for (i = 1; i <= n; i++) + { + f>>v[i]; + update(i, 1); + } + + for (i = n; i >=1; i--) + { + p = caut(1, n, i); + final[p] = i; + update(p, -1); + } + + for(i = 1; i<= n; i++) + { + g< +using namespace std; + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + +int Poz[30005],Arb[30005],Sol[30005],n; + + + +void Update(int p,int val){ + + + + while(p<=n){ + + + + Arb[p]+=val; + + p+=p&-p; + + + + } + + + +} + + + +int Query(int p){ + + + + int s=0; + + while(p>0){ + + + + s+=Arb[p]; + + p-=p&-p; + + + + } + + + + return s; + + + +} + + + +int SearchPosition(int val) { + + + + int left=1,right=n,middle; + + + + while(right-left>1){ + + + + middle=(left+right)/2; + + + + if(Query(middle)>n; + + + + for(int i=1;i<=n;i++) + + cin>>Poz[i]; + + + + for(int i=1;i<=n;i++) + + Update(i,1); + + + + for(int i=n;i>=1;i--){ + + + + int index=SearchPosition(Poz[i]); + + Update(index,-1); + + Sol[index]=i; + + + + } + + + + for(int i=1;i<=n;i++) + + cout< +using namespace std; + + ifstream f("schi.in"); + + ofstream g("schi.out"); + + + +const int dim = 30001; + +long AIB[dim], n, v[dim], sol[dim]; + + + +inline long lowestTwo(long x) + +{ + + return x & (-x); + +} + + + +void update(long poz, long val) + +{ + + while(poz <= n){ + + AIB[poz] += val; + + poz += lowestTwo(poz); + + } + +} + + + +long sum(long val) + +{ + + long result = 0 ; + + for(long i = val ; i >= 1 ; i-= lowestTwo(i)){ + + result += AIB[i]; + + } + + return result; + +} + + + +long binarySearch(long searchVal) + +{ + + long st = 1, dr = n; + + long val; + + + + while(st <= dr){ + + long mij = (st + dr) / 2; + + long sumMij = sum(mij); + + + + if(sumMij == searchVal){ + + val = mij; + + dr = mij - 1; + + } + + + + else if( sumMij < searchVal){ + + st = mij + 1; + + } + + + + else{ + + dr = mij - 1; + + } + + } + + + + return val; + +} + + + +int main() + +{ + + f>>n; + + + + for(long i = 1 ; i <= n ; i++){ + + f>>v[i]; + + update(i, 1); + + } + + + + for(long i = n ; i >= 1 ; i--){ + + long poz = binarySearch(v[i]); + + sol[poz] = i; + + update(poz, -1); + + } + + + + for(long i = 1 ; i <= n ; i++){ + + g< +#include + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int aib[30005],n,v[30005],a[30005]; + +void update(int poz,int val) + +{ + + int i; + + for(i=poz;i<=n;i=i+( i & (-i) )) + + { + + aib[i]+=val; + + } + +} + +int suma(int poz) + +{ + + int i,s=0; + + for(i=poz;i>=1;i=i-( i & (-i) )) + + { + + s=s+aib[i]; + + } + + return s; + +} + +int main() + +{ + + int i,st,dr,mij,val,s; + + f>>n; + + for(i=1;i<=n;i++) + + { + + f>>a[i]; + + update(i,1); + + } + + for(i=n;i>=1;i--) + + { + + st=1; + + dr=n; + + val=0; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + s=suma(mij); + + if(s==a[i]) + + { + + val=mij; + + dr=mij-1; + + } + + else + + { + + if(s +#include +using namespace std; + +int aint[120001]; + +int v[30001]; + +int poz[30001],ok; + +void goup(int nod) + +{ + + aint[nod]++; + + if(nod==1) + + return; + + goup(nod/2); + +} + +void cauta(int nod,int st,int dr,int i) + +{ + + if(ok==1) + + return; + + if(st==dr) + + { + + goup(nod); + + poz[st]=i; + + ok=1; + + return; + + } + + int mij=(st+dr)/2; + + if(mij-st+1-aint[2*nod]>=v[i]) + + cauta(2*nod,st,mij,i); + + else + + v[i]-=(mij-st+1-aint[2*nod]); + + if(dr-mij-aint[2*nod+1]>=v[i]) + + cauta(2*nod+1,mij+1,dr,i); + + else + + v[i]-=(dr-mij-aint[2*nod+1]); + +} + +int main() + +{ + + freopen("schi.in","r",stdin); + + freopen("schi.out","w",stdout); + + int n,i; + + cin>>n; + + for(i=1; i<=n; i++) + + cin>>v[i]; + + for(i=n; i>=1; i--) + + {ok=0;cauta(1,1,n,i);} + + for(i=1; i<=n; i++) + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +using namespace std; + +ifstream f("schi.in"); +ofstream g("schi.out"); + +const int DIM = 3e4 + 17; + +int aib[DIM]; +int indx[DIM]; +int v[DIM]; + +int n; + +void update(int pos, int val) +{ + for(int i = pos; i <= n; i += i & (-i)) + { + aib[i] += val; + } +} + +int sum(int pos) +{ + int s = 0; + + for(int i = pos; i >= 1; i -= i & (-i)) + s += aib[i]; + + return s; +} + +int main() +{ + f >> n; + + for(int i = 1; i <= n; i++) + { + f >> v[i]; + update(i, 1); + } + + + for(int i = n; i >= 1; i--) + { + int pos = 0; + int l = 1; + int r = n; + + while(l <= r) + { + int mid = (l + r) / 2; + + int p = sum(mid); + + if(p >= v[i]) + { + pos = mid; + r = mid - 1; + } + else + l = mid + 1; + } + + indx[pos] = i; + + update(pos, -1); + } + + for(int i = 1; i <= n; i++) + g << indx[i] << '\n'; +} diff --git a/ski/aib/7da0e8a4-137c-11eb-ba1d-309c234925a0.cpp b/ski/aib/7da0e8a4-137c-11eb-ba1d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87a389db745ed58a22ad11180260476b6cdc8145 --- /dev/null +++ b/ski/aib/7da0e8a4-137c-11eb-ba1d-309c234925a0.cpp @@ -0,0 +1,104 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N,V[30005],AIB[30005],Sol[30005]; + + + +void Initializare(){ + + for(int i=1;i<=N;i++) + + AIB[i]=i&(-i); + +} + + + +int Query(int X){ + + int S=0; + + for(int i=X;i>=1;i-=i&(-i)) + + S=S+AIB[i]; + + return S; + +} + + + +void Update(int X){ + + for(int i=X;i<=N;i+=i&(-i)) + + AIB[i]--; + +} + + + +int Search(int Val){ + + int L=1,R=N; + + while(L<=R){ + + int Mid=(L+R)/2; + + if(Query(Mid)>=Val){ + + R=Mid-1; + + } + + else + + L=Mid+1; + + } + + return L; + +} + + + +int main(){ + + fin>>N; + + for(int i=1;i<=N;i++){ + + fin>>V[i]; + + } + + Initializare(); + + for(int i=N;i>=1;i--){ + + int K=Search(V[i]); + + Sol[K]=i; + + Update(K); + + } + + for(int i=1;i<=N;i++) + + fout< +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +int bit[30000],v[30000],v2[30000]; + +int n; + +int upt(int i,int add) { + + for(; i<=n; i+=(i&-i)) + + bit[i]+=add; + +} + +int see(int i) { + + int ans=0; + + for(; i>0; i-=(i&-i)) + + ans+=bit[i]; + + return ans; + +} + +int bs(int k) { + + int l=0,h=n; + + while(l>n; + + for(i=1; i<=n; i++) + + in>>v[i],upt(i,1); + + for(i=n; i>=1; i--) { + + ans=bs(v[i]); + + upt(ans,-1); + + v2[ans]=i; + + } + + for(i=1; i<=n; i++) + + out< +#include +#include +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N; + +vector input, output; + + + +struct FenwickTree { + + vector ft; + + int ft_size; + + + + FenwickTree(int n) { + + ft.resize(n, 0); + + ft_size = n; + + } + + + + int rsq(int x) { + + int sum = 0; + + for (; x; x -= (x & (-x))) sum += ft[x]; + + return sum; + + } + + + + void adjust(int k, int v) { + + for (; k < ft_size; k += (k & (-k))) ft[k] += v; + + } + +}; + + + +void Read() { + + fin >> N; + + input.resize(N + 1); + + output.resize(N + 1); + + for (int i = 1; i <= N; ++i) + + fin >> input[i]; + +} + + + +int lower_bound(FenwickTree &ft, int v) { + + int left = 1, right = ft.ft_size; + + + + while (left <= right) { + + int middle = (left + right) / 2; + + + + int middle_rsq = ft.rsq(middle); + + + + if (middle_rsq < v) + + left = middle + 1; + + else right = middle - 1; + + } + + + + return left; + +} + + + +void Solve() { + + FenwickTree ft(N + 1); + + + + for (int i = 1; i <= N; ++i) { + + ft.adjust(i, 1); + + } + + + + for (int i = N; i >= 1; --i) { + + int current_place = lower_bound(ft, input[i]); + + output[current_place] = i; + + ft.adjust(current_place, -1); + + } + +} + + + +void Print() { + + for (int i = 1; i <= N; ++i) + + fout << output[i] << "\n"; + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/ski/aib/7ddb7ed4-137c-11eb-ae08-309c234925a0.cpp b/ski/aib/7ddb7ed4-137c-11eb-ae08-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb7ec130ce2e07275f2047a263305cc59a464e4a --- /dev/null +++ b/ski/aib/7ddb7ed4-137c-11eb-ae08-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int N, pow = 1; + +int v[30005], sol[30005]; + +int aib[30005]; + + + +inline int LastBit(int x) + +{ + + return x & (-x); + +} + + + +void Update(int pos, int value) + +{ + + for(int i = pos; i <= N; i += LastBit(i)) + + aib[i] += value; + +} + + + +int Search(int pos) + +{ + + int cursor = 0; + + int step = pow; + + + + for(; step > 0; step >>= 1) + + if(cursor + step <= N && aib[cursor + step] < pos) + + { + + cursor += step; + + pos -= aib[cursor]; + + + + if(pos == 0) + + break; + + } + + + + return cursor + 1; + +} + + + +int main() + +{ + + fin >> N; + + for(; pow <= N; pow <<= 1); + + + + for(int i = 1; i <= N; i++) + + Update(i, 1); + + + + for(int i = 1; i <= N; i++) + + fin >> v[i]; + + + + for(int i = N; i >= 1; i--) + + { + + int resSearch = Search(v[i]); + + + + sol[resSearch] = i; + + Update(resSearch, -1); + + } + + + + for(int i = 1; i <= N; i++) + + fout << sol[i] << '\n'; + + + + return 0; + +} diff --git a/ski/aib/7df11e90-137c-11eb-a368-309c234925a0.cpp b/ski/aib/7df11e90-137c-11eb-a368-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69eb5ffeb91ef6eff0f05b176bb8ce06294a6614 --- /dev/null +++ b/ski/aib/7df11e90-137c-11eb-a368-309c234925a0.cpp @@ -0,0 +1,126 @@ +#include + + + + + + +std::ifstream InFile("schi.in"); + +std::ofstream OutFile("schi.out"); + + + +int N; + +int Tree[30005]; + +int Loc[30005]; + +int Ans[30005]; + + + +int Sum(int Index) { + + int S = 0; + + while (Index) { + + S += Tree[Index]; + + Index -= Index&(-Index); + + } return S; + +} + + + +void Add(int Index, int Value) { + + while (Index<=N) { + + Tree[Index] += Value; + + Index += Index&(-Index); + + } + +} + + + +int Query(int Index) { + + int Rez; + + int Left = 1, Right = N, Middle; + + while (Left <= Right) { + + Middle = (Left+Right)/2; + + if(Loc[Index] <= Sum(Middle)) { + + Right = Middle-1; + + Rez = Middle; + + } + + else Left = Middle+1; + + } return Rez; + +} + + + +void Citire() { + + InFile >> N; + + for (int i=0; i> Loc[i+1], + + Add(i+1, 1); + +} + +void Rezolvare() { + + for (int i=N; i>0; i--) { + + int Index = Query(i); + + Ans[Index] = i; + + Add(Index, -1); + + } + + + + for (int i=0; i + + + + + + +FILE *fin = freopen("schi.in","r",stdin); FILE *fout = freopen("schi.out","w",stdout); + + + +const int NMAX = 30000 + 5; + + + + + +int n, v[NMAX], aib[NMAX], sol[NMAX]; + + + + + +void Add(int position, int value) + +{ + + for(int i = position; i<= n; i+=(i & (-i))) + + aib[i]+=value; + +} + + + +int Query(int position) + +{ + + int res = 0; + + for(int i = position; i>0 ; i-=(i & (-i))) + + res+=aib[i]; + + return res; + +} + + + +void Read() + +{ + + scanf("%d",&n); + + for(int i =1 ; i<= n; ++i){ + + scanf("%d",&v[i]); + + Add(i,1); + + } + +} + + + +void Rezolva() + +{ + + int pas = 1, k = 0,aux; + + for(;pas<=n;pas<<=1); + + + + for(int i = n; i>= 1; i--) + + { + + aux = pas; k = 0; + + for(;aux;aux>>=1) + + if(k + aux <= n && Query(k+aux) < v[i]) + + k+=aux; + + sol[k+1]=i; + + Add(k+1,-1); + + } + + for(int i = 1; i<= n; ++i) + + printf("%d\n",sol[i]); + +} + + + +int main() + +{ + + Read(); + + Rezolva(); + + + +} diff --git a/ski/aib/7e114c64-137c-11eb-8f60-309c234925a0.cpp b/ski/aib/7e114c64-137c-11eb-8f60-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..965949bdb012a1ed3d04da022c07dfec054e7f4e --- /dev/null +++ b/ski/aib/7e114c64-137c-11eb-8f60-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include + + + + +const int MAX_N = 30000; + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n; + +int v[MAX_N + 5], sol[MAX_N + 5], AIB[MAX_N + 5]; + + + +void update(int p, int x) { + + for(; p <= n; p += p&(-p)) + + AIB[p] += x; + +} + + + +int bs(int x) { + + int i = 0; + + int p = (1 << 15); + + while(p > 0) { + + if((i + p <= n) && (AIB[i + p] < x)) { + + i += p; + + x -= AIB[i]; + + if(x == 0) + + break; + + } + + p >>= 1; + + } + + return i + 1; + +} + + + +int main() { + + fin >> n; + + for(int i = 1; i <= n; i++) { + + fin >> v[i]; + + update(i, 1); + + } + + + + for(int i = n; i >= 1; i--) { + + int ans = bs(v[i]); + + sol[ans] = i; + + update(ans, -1); + + } + + + + for(int i = 1; i <= n; i++) + + fout << sol[i] << '\n'; + + return 0; + +} diff --git a/ski/aib/7e2195a4-137c-11eb-bb76-309c234925a0.cpp b/ski/aib/7e2195a4-137c-11eb-bb76-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49d4788b70f4ef29c587b199a35275664d059c97 --- /dev/null +++ b/ski/aib/7e2195a4-137c-11eb-bb76-309c234925a0.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +int n,fin[30005],a[30005],aib[30005]; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +void update(int p,int x) + +{ + + for (;p<=n;p+=(p&(-p))) + + aib[p]+=x; + +} + +int query(int p) + +{ + + int s=0; + + for (;p>0;p-=(p&(-p))) + + s+=aib[p]; + + + + return s; + +} + +int cautbin(int x) + +{ + + int st=1,dr=n,fin=0,mij; + + while (st<=dr) + + { + + mij=(st+dr)/2; + + if (query(mij)>n; + + int i,poz; + + for (i=1;i<=n;i++)in>>a[i]; + + for (i=1;i<=n;i++)update(i,1); + + for (i=n;i>=1;i--) + + { + + poz=cautbin(a[i]); + + update(poz,-1); + + fin[poz]=i; + + } + + for (i=1;i<=n;i++)out< +#include +using namespace std; +ifstream cin ("schi.in"); +ofstream cout ("schi.out"); +int aib[30004],aib1[300004],v[30004],n; +struct ura +{ + int a,b; +}rez[30004]; +int lsb (int x) +{ + return x&-x; +} +void update (int poz) +{ + for(int i=poz;i<=n;i+=lsb(i)) + ++aib[i]; +} +int query (int poz) +{ + int s=0; + for(int i=poz;i>=1;i-=lsb(i)) + s+=aib[i]; + return s; +} +void update1 (int poz) +{ + for(int i=poz;i<=n;i+=lsb(i)) + ++aib1[i]; +} +int query1 (int poz) +{ + int s=0; + for(int i=poz;i>=1;i-=lsb(i)) + s+=aib1[i]; + return s; +} +bool cmp (ura a, ura b) +{ + if(a.a>n; + for(i=1;i<=n;++i) + {cin>>v[i];rez[i].b=i;} + for(i=n;i>=1;--i) + { + int x1,x,sc=0; + x1=0; + sc=0; + int pas=1<<20; + while(pas) + { + if(x1+pas<=n && x1+pas-sc-aib1[x1+pas]>=1; + } + pas=1<<20; + x=0; + sc=0; + while(pas) + { + if(x+pas<=n && x+pas-sc-aib1[x+pas]>=1; + } + ++x; + ++x1; + + if(i + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i,j,aib[30009],v[30009],poz[30009]; + +void add(int x,int val) + +{ + + for(int i=x;i<=n;i+=i&(-i)) + + aib[i]+=val; + +} + +int sum (int x ) + +{ + + int s=0; + + for(int i=x;i>=1;i-=i&(-i)) + + s+=aib[i]; + + return s; + +} + +int bin(int st,int dr,int val) + +{ + + int mij; + + int minim=INT_MAX; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + int S=sum(mij); + + if(S==val&&minim>mij) + + minim=mij; + + else + + if(S>=val) + + dr=mij-1; + + else + + st=mij+1; + + } + + return minim; + +} + +int main() + +{ + + fin.sync_with_stdio(false); + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i]; + + for(i=1;i<=n;i++) + + aib[i]=i&(-i); + + for(i=n;i>=1;i--) + + { + + int ans=bin(1,n,v[i]); + + add(ans,-1); + + poz[ans]=i; + + } + + for(i=1;i<=n;i++) + + fout< + + + + + + +using namespace std; + + + +int n, l2n, v[30002], c[30002]; + + + +int aib[30002]; + + + +void upd(int a) + +{ + + for(int i = a; i <= n; i += i & (-i)) + + aib[i]--; + +} + + + +int qry(int a) + +{ + + int ans = 0; + + for(int i = a; i >= 1; i -= i & (-i)) + + ans += aib[i]; + + return ans; + +} + + + +int main() + +{ + + ifstream fin ("schi.in"); + + ofstream fout ("schi.out"); + + fin >> n; + + l2n = 1; + + while(l2n * 2 <= n) + + l2n *= 2; + + for(int i = 1; i <= n; i++) + + fin >> v[i]; + + for(int i = 1; i <= n; i++) + + aib[i] = i & (-i); + + for(int i = n; i >= 1; i--) + + { + + int l = 1, r = n, mid; + + while(l < r) + + { + + mid = (l + r) / 2; + + if(qry(mid) < v[i]) + + l = mid + 1; + + else + + r = mid; + + } + + c[l] = i; + + upd(l); + + } + + for(int i = 1; i <= n; i++) + + fout << c[i] << "\n"; + + return 0; + +} diff --git a/ski/aib/7e5b25c6-137c-11eb-9009-309c234925a0.cpp b/ski/aib/7e5b25c6-137c-11eb-9009-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d29457478455db1908f1699ab2013d09f44e8d15 --- /dev/null +++ b/ski/aib/7e5b25c6-137c-11eb-9009-309c234925a0.cpp @@ -0,0 +1,197 @@ +#include +FILE *fin = freopen("schi.in","r",stdin); FILE *fout = freopen("schi.out","w",stdout); +const int NMAX = 30000 + 5; +int n, v[NMAX], aib[NMAX], sol[NMAX]; +void Add(int position, int value) + + + +{ + + + + for(int i = position; i <= n; i+= (i & (-i))) + + + + aib[i]+=value; + + + +} + + + + + + + +int Query(int position) + + + +{ + + + + int res = 0; + + + + for(int i = position; i> 0 ;i-=(i & (-i))) + + + + res+=aib[i]; + + + + return res; + + + +} + + + + + + + +void Read() + + + +{ + + + + int i; + + + + scanf("%d",&n); + + + + for(i = 1; i<= n; ++i) + + + + { + + + + scanf("%d",&v[i]); + + + + Add(i,1); + + + + } + + + + + + + + int pas=1,k=0; + + + + for(;pas<=n;pas<<=1); + + + + for(i = n; i>= 1; i--) + + + + { + + + + k = 0; + + + + int aux = pas; + + + + for(;aux;aux>>=1) + + + + { + + + + if(k+aux <= n && Query(k+aux) < v[i]){ + + + + k+=aux; + + + + } + + + + } + + + + Add(k+1,-1); + + + + sol[k+1]=i; + + + + } + + + +} + + + + + + + +int main() + + + +{ + + + + Read(); + + + + for(int i = 1; i<= n; ++i) + + + + { + + + + printf("%d\n",sol[i]); + + + + } + + + +} diff --git a/ski/aib/7e6c997e-137c-11eb-9e64-309c234925a0.cpp b/ski/aib/7e6c997e-137c-11eb-9e64-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7f3f98a2b3a86cc3dbb9c5267cd9917fc14781c --- /dev/null +++ b/ski/aib/7e6c997e-137c-11eb-9e64-309c234925a0.cpp @@ -0,0 +1,154 @@ +#include +using namespace std; + +char BUF[1<<17]; + +int poz; + + + +inline char getChar(){ + + poz++; + + if(poz>=1<<17){ + + fread(BUF,1<<17,1,stdin); + + poz=0; + + } + + return BUF[poz]; + +} + + + +inline int getNr(){ + + int r=0, semn=1; + + char ch=getChar(); + + while(isdigit(ch)==0 && ch!='-') ch=getChar(); + + if(ch=='-'){ + + semn=-1; + + ch=getChar(); + + } + + while(isdigit(ch)!=0){ + + r=r*10+semn*(ch-'0'); + + ch=getChar(); + + } + + return r; + +} + + + +int v[30001],aib[30001],rez[30001],n; + + + +int zeros(int x) + +{ + + return (x^(x-1))&x; + +} + + + +void update(int i,int x) + +{ + + for(int ct=i; ct<=n; ct+=zeros(ct)) + + aib[ct]+=x; + +} + + + +int query(int i) + +{ + + int rez=0; + + for(int ct=i; ct>0; ct-=zeros(ct)) + + rez+=aib[ct]; + + return rez; + +} + + + +int main() + +{ + + int i,pas,z; + + freopen("schi.in","r",stdin); + + freopen("schi.out","w",stdout); + + n=getNr(); + + for(i=1; i<=n; i++) + + v[i]=getNr(); + + for(i=1; i<=n; i++) + + update(i,1); + + for(i=n; i>0; i--) + + { + + pas=1<<14; + + z=0; + + while(pas) + + { + + if(z+pas<=n && query(z+pas) + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int aib[30005],a[30005],sol[30005]; + +int n,bitMax; + +void add(int k,int val) + +{ + + for(;k<=n;k+=(k&-k)) + + aib[k]+=val; + +} + +void read() + +{ + + f>>n; + + for(int i=1;i<=n;i++) + + { + + f>>a[i]; + + add(i,1); + + } + +} + +int sum(int val) + +{ + + int sol=0; + + for(;val>0;val-=(val&-val)) + + sol+=aib[val]; + + return sol; + +} + +int query(int val) + +{ + + int st=1,dr=n,sol,mij,x; + + while(st<=dr) + + { + + mij=(st+dr)/2; + + x=sum(mij); + + if(x>=val) + + { + + dr=mij-1; + + sol=mij; + + } + + else + + st=mij+1; + + } + + return sol; + +} + +void solve() + +{ + + for(int i=n;i>=1;i--) + + { + + int pos=query(a[i]); + + sol[pos]=i; + + add(pos,-1); + + } + + for(int i=1;i<=n;i++) + + g< + + + + +FILE *fin = freopen("schi.in","r",stdin); FILE *fout = freopen("schi.out","w",stdout); + + + + + +const int NMAX = 30000 + 5; + + + + + +int n, v[NMAX], aib[NMAX], sol[NMAX]; + + + + + + + +void Add(int position, int value) + +{ + + for(int i = position; i <= n; i+= (i & (-i))) + + aib[i]+=value; + +} + + + +int Query(int position) + +{ + + int res = 0; + + for(int i = position; i> 0 ;i-=(i & (-i))) + + res+=aib[i]; + + return res; + +} + + + +void Read() + +{ + + int i; + + scanf("%d",&n); + + for(i = 1; i<= n; ++i) + + { + + scanf("%d",&v[i]); + + Add(i,1); + + } + + + + int pas=1,k=0; + + for(;pas<=n;pas<<=1); + + for(i = n; i>= 1; i--) + + { + + k = 0; + + int aux = pas; + + for(;aux;aux>>=1) + + { + + if(k+aux <= n && Query(k+aux) < v[i]){ + + k+=aux; + + } + + } + + Add(k+1,-1); + + sol[k+1]=i; + + } + +} + + + +int main() + +{ + + Read(); + + for(int i = 1; i<= n; ++i) + + { + + printf("%d\n",sol[i]); + + } + +} diff --git a/ski/aib/7ecbe5c8-137c-11eb-8832-309c234925a0.cpp b/ski/aib/7ecbe5c8-137c-11eb-8832-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a16fad1ce69a5005ef72867e615c0c64eee031e --- /dev/null +++ b/ski/aib/7ecbe5c8-137c-11eb-8832-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int NMAX = 30005; + +int fn[NMAX],v[NMAX],rez[NMAX],n; + +void add(int k, int val) + +{ + + while (k<=n) + + { + + fn[k]+=val; + + k+=k&-k; + + } + +} + +int sum(int k) + +{ + + int s = 0; + + while (k>0) + + { + + s+=fn[k]; + + k-=k&-k; + + } + + return s; + +} + + + +int main() + +{ + + in >> n; + + for (int i = 1; i<=n; i++) + + { + + in >> v[i]; + + add(i,1); + + } + + for (int i = n; i>=1; i--) + + { + + int x = v[i],r; + + int st = 1, dr = n; + + while (st<=dr) + + { + + int mj = (st+dr)/2; + + int s = sum(mj); + + if (s == x) + + { + + r = mj; + + dr = mj-1; + + } + + else if (s>x) + + dr = mj-1; + + else + + st = mj+1; + + } + + rez[r] = i; + + add(r,-1); + + } + + for (int i = 1; i<=n; i++) + + out << rez[i] << "\n"; + +} diff --git a/ski/aib/7eda31c2-137c-11eb-b956-309c234925a0.cpp b/ski/aib/7eda31c2-137c-11eb-b956-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fcad5b78fcfa93251e3cefb81666b7119c3e7783 --- /dev/null +++ b/ski/aib/7eda31c2-137c-11eb-b956-309c234925a0.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,i,z,a[120010],b[30010],ans[30010]; + +int fnd(int val,int poz) + +{ + + if(poz>z)return poz-z; + + if(a[poz*2]>=val) + + return fnd(val,poz*2); + + else + + return fnd(val-a[poz*2],poz*2+1); + +} + +void upd(int poz) + +{ + + poz+=z; + + while(poz) + + { + + a[poz]--; + + poz>>=1; + + } + + return ; + +} + +int main() + +{ + + f>>n; + + z=1;while(z=1;i--) + + a[i]=a[i*2]+a[i*2+1]; + + for(i=1;i<=n;i++) + + f>>b[i]; + + for(i=n;i>=1;i--) + + { + + int y=fnd(b[i],1); + + upd(y); + + ans[y]=i; + + } + + for(i=1;i<=n;i++) + + g< + +using namespace std; + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; +int t[N]; + +void update(int poz, int vall) { + while (poz <= n) { + t[poz] += vall; + poz += poz&(-poz); + } +} + +int query(int poz) { + int ans(0); + while (poz > 0) { + ans += t[poz]; + poz -= poz&(-poz); + } + return ans; +} + +int cb(int lol) { + int pas = 1<<14, r(0); + while (pas > 0) { + if (r + pas <= n && t[r + pas] < lol) { + lol -= t[r + pas]; + r += pas; + } + pas>>=1; + } + return r + 1; +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + for (int i = n; i >= 1; --i) { + int ans = cb(v[i]); + update(ans, -1); + poz[ans] = i; + } + for (int i = 1; i <= n; ++i) { + cout << poz[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/7ef8632e-137c-11eb-96f8-309c234925a0.cpp b/ski/aib/7ef8632e-137c-11eb-96f8-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95f1213b2a052f9b6d9ee540f4de87d03be56e2 --- /dev/null +++ b/ski/aib/7ef8632e-137c-11eb-96f8-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; +int t[N]; + +void update(int poz, int vall) { + while (poz <= n) { + t[poz] += vall; + poz += poz&(-poz); + } +} + +int query(int poz) { + int ans(0); + while (poz > 0) { + ans += t[poz]; + poz -= poz&(-poz); + } + return ans; +} + +int cb(int lol) { + int pas = 1<<14, r(0); + while (pas > 0) { + if (r + pas <= n && t[r + pas] < lol) { + lol -= t[r + pas]; + r += pas; + } + pas>>=1; + } + return r + 1; +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + for (int i = n; i >= 1; --i) { + int ans = cb(v[i]); + update(ans, -1); + poz[ans] = i; + } + for (int i = 1; i <= n; ++i) { + cout << poz[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/7f05edf4-137c-11eb-86e6-309c234925a0.cpp b/ski/aib/7f05edf4-137c-11eb-86e6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4044755bb624e51539b6d439f8d4d68d63e856fa --- /dev/null +++ b/ski/aib/7f05edf4-137c-11eb-86e6-309c234925a0.cpp @@ -0,0 +1,46 @@ +#include + +using namespace std; + +int n, v[30005], aint[120005], sol[30005]; + +void update(int pos, int nod = 1, int st = 1, int dr = n) +{ + if(st == dr) + ++aint[nod]; + else{ + int m = (st+dr)/2; + if(pos <= m) + update(pos, nod*2, st, m); + else update(pos, nod*2+1, m+1, dr); + aint[nod] = aint[nod*2] + aint[nod*2+1]; + } +} + +int query(int x, int nod = 1, int st = 1, int dr = n) +{ + if(st == dr) + return st; + int m = (st+dr)/2; + if((m-st+1)-aint[nod*2] >= x) + return query(x, nod*2, st, m); + return query(x-((m-st+1)-aint[nod*2]), nod*2+1, m+1, dr); +} + +int main() +{ + ifstream fin ("schi.in"); + ofstream fout ("schi.out"); + fin >> n; + for (int i = 1; i <= n; ++i) + fin >> v[i]; + for (int i = n; i >= 1; --i){ + int x = v[i]; + int ans = query(x); + sol[ans] = i; + update(ans); + } + for (int i = 1; i <= n; ++i) + fout << sol[i] << "\n"; + return 0; +} diff --git a/ski/aib/7f2f565c-137c-11eb-b3ab-309c234925a0.cpp b/ski/aib/7f2f565c-137c-11eb-b3ab-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5be808388ae8a58579eb90b982d302a48f94ce17 --- /dev/null +++ b/ski/aib/7f2f565c-137c-11eb-b3ab-309c234925a0.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int NMAX = 30005; + +int fn[NMAX],v[NMAX],rez[NMAX],n; + +void add(int k, int val) + +{ + + while (k<=n) + + { + + fn[k]+=val; + + k+=k&-k; + + } + +} + + + +int find(int val) + +{ + + int step,i; + + for (step = 1; step<=n; step<<=1); + + for (i = 0; step>0; step>>=1) + + if (i+step<=n && fn[i+step]> n; + + for (int i = 1; i<=n; i++) + + { + + in >> v[i]; + + add(i,1); + + } + + for (int i = n; i>=1; i--) + + { + + int poz = find(v[i]); + + rez[poz] = i; + + add(poz,-1); + + } + + for (int i = 1; i<=n; i++) + + out << rez[i] << "\n"; + +} diff --git a/ski/aib/7f3f2628-137c-11eb-95f5-309c234925a0.cpp b/ski/aib/7f3f2628-137c-11eb-95f5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4aaf5d886285dae2388d23eeb0b82e36211296ca --- /dev/null +++ b/ski/aib/7f3f2628-137c-11eb-95f5-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + +ifstream cin("schi.in"); + +ofstream cout("schi.out"); + +int aib[30005],n,i,v[30005],solutie[30005]; + +void Update(int x,int quantity){ + + int i; + + + + for(i = x;i <= n;i+=(i ^ (i-1)) & i){ + + aib[i] += quantity; + + } + +} + +int sum(int x){ + + int i,sol = 0; + + for(i = x;i > 0;i -= (i ^ (i-1)) & i){ + + sol += aib[i]; + + } + + return sol; + +} + +int bs(int val){ + + int pas = 1 << 14; + + int r = 0; + + while(pas){ + + if(r+pas <= n && aib[r+pas] < val){ + + val-=aib[r+pas]; + + r+=pas; + + } + + pas/=2; + + } + + return r+1; + +} + +int main() + +{ + + cin >> n; + + for(i = 1;i <= n;i++){ + + cin >> v[i]; + + } + + for(i = 1;i <= n;i++){ + + Update(i,1); + + } + + for(i = n;i >= 1;i--){ + + int x = bs(v[i]); + + Update(x,-1); + + solutie[x] = i; + + } + + for(i = 1;i <= n;i++){ + + cout << solutie[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/aib/7f4e8f74-137c-11eb-80f3-309c234925a0.cpp b/ski/aib/7f4e8f74-137c-11eb-80f3-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95f1213b2a052f9b6d9ee540f4de87d03be56e2 --- /dev/null +++ b/ski/aib/7f4e8f74-137c-11eb-80f3-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; +int t[N]; + +void update(int poz, int vall) { + while (poz <= n) { + t[poz] += vall; + poz += poz&(-poz); + } +} + +int query(int poz) { + int ans(0); + while (poz > 0) { + ans += t[poz]; + poz -= poz&(-poz); + } + return ans; +} + +int cb(int lol) { + int pas = 1<<14, r(0); + while (pas > 0) { + if (r + pas <= n && t[r + pas] < lol) { + lol -= t[r + pas]; + r += pas; + } + pas>>=1; + } + return r + 1; +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + for (int i = n; i >= 1; --i) { + int ans = cb(v[i]); + update(ans, -1); + poz[ans] = i; + } + for (int i = 1; i <= n; ++i) { + cout << poz[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/7f6033f8-137c-11eb-8d24-309c234925a0.cpp b/ski/aib/7f6033f8-137c-11eb-8d24-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95f1213b2a052f9b6d9ee540f4de87d03be56e2 --- /dev/null +++ b/ski/aib/7f6033f8-137c-11eb-8d24-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; +int t[N]; + +void update(int poz, int vall) { + while (poz <= n) { + t[poz] += vall; + poz += poz&(-poz); + } +} + +int query(int poz) { + int ans(0); + while (poz > 0) { + ans += t[poz]; + poz -= poz&(-poz); + } + return ans; +} + +int cb(int lol) { + int pas = 1<<14, r(0); + while (pas > 0) { + if (r + pas <= n && t[r + pas] < lol) { + lol -= t[r + pas]; + r += pas; + } + pas>>=1; + } + return r + 1; +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + for (int i = n; i >= 1; --i) { + int ans = cb(v[i]); + update(ans, -1); + poz[ans] = i; + } + for (int i = 1; i <= n; ++i) { + cout << poz[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/7f6f6c62-137c-11eb-9634-309c234925a0.cpp b/ski/aib/7f6f6c62-137c-11eb-9634-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f95f1213b2a052f9b6d9ee540f4de87d03be56e2 --- /dev/null +++ b/ski/aib/7f6f6c62-137c-11eb-9634-309c234925a0.cpp @@ -0,0 +1,59 @@ +#include + +using namespace std; + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int N = 3e4 + 7; + +int v[N], poz[N], n; +int t[N]; + +void update(int poz, int vall) { + while (poz <= n) { + t[poz] += vall; + poz += poz&(-poz); + } +} + +int query(int poz) { + int ans(0); + while (poz > 0) { + ans += t[poz]; + poz -= poz&(-poz); + } + return ans; +} + +int cb(int lol) { + int pas = 1<<14, r(0); + while (pas > 0) { + if (r + pas <= n && t[r + pas] < lol) { + lol -= t[r + pas]; + r += pas; + } + pas>>=1; + } + return r + 1; +} + +int main() +{ + cin >> n; + for (int i = 1; i <= n; ++i) { + cin >> v[i]; + } + for (int i = 1; i <= n; ++i) { + update(i, 1); + } + for (int i = n; i >= 1; --i) { + int ans = cb(v[i]); + update(ans, -1); + poz[ans] = i; + } + for (int i = 1; i <= n; ++i) { + cout << poz[i] << "\n"; + } + return 0; +} diff --git a/ski/aib/7f7db0b0-137c-11eb-9342-309c234925a0.cpp b/ski/aib/7f7db0b0-137c-11eb-9342-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0bb616a6258bc03a677159c2bdfaefd681de540 --- /dev/null +++ b/ski/aib/7f7db0b0-137c-11eb-9342-309c234925a0.cpp @@ -0,0 +1,104 @@ +#include +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n; + +int aib[30002]; + +int pos[30002]; + +int v[30002]; + +void add(int nr, int val) + +{ + + for(;nr<=n;nr+=(nr&(-nr))) + + aib[nr]+=val; + +} + +int compute(int nr) + +{ + + int sol=0; + + for(;nr;nr-=(nr&(-nr))) + + sol+=aib[nr]; + + return sol; + +} + +int cb(int val) + +{ + + int b=1; + + int e=n; + + int sol=0; + + while(b<=e) + + { + + int m=(b+e)/2; + + int a=compute(m); + + int aa=compute(m-1); + + if(a==val && aa=val) + + e=m-1; + + else + + b=m+1; + + } + +} + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;++i) + + f>>v[i],add(i,1); + + for(int i=n;i>=1;--i) + + { + + int ans=cb(v[i]); + + pos[ans]=i; + + add(ans,-1); + + } + + for(int i=1;i<=n;++i) + + g< +#include +#include + +template +using UniteFN = T(T, T); + +template +class AINT { + std::array t; + FN& unite_fn; + +private: + void _update(int p, int st, int dr, int poz) { + if (st == dr) { + t[p] = !t[p]; + return; + } + + int m = (st + dr) / 2; + if (poz <= m) { + _update(2 * p, st, m, poz); + } else { + _update(2 * p + 1, m + 1, dr, poz); + } + t[p] = unite_fn(t[2 * p], t[2 * p + 1]); + } + + int _query(int p, int st, int dr, T x) { + if (st == dr) { + return st; + } + + int m = (st + dr) / 2; + if (t[2 * p] >= x) { + return _query(2 * p, st, m, x); + } else { + return _query(2 * p + 1, m + 1, dr, x - t[2 * p]); + } + } + +public: + AINT(FN& unite_fn) : unite_fn(unite_fn) {} + + inline void update(int n, int poz) { + _update(1, 1, n, poz); + } + + inline int query(int n, int x) { + return _query(1, 1, n, x); + } +}; + + + + + + + +class schi_uniter { +public: + int operator()(int a, int b) { + return a + b; + } +}; + +auto schi_unite = [](int a, int b) -> int { + return a + b; +}; +AINT aint(schi_unite); +std::array input; +std::array result; + +int main() { + std::ifstream fin("schi.in"); + std::ofstream fout("schi.out"); + int n; + + fin >> n; + for (int i = 1; i <= n; i++) { + fin >> input[i]; + aint.update(n, i); + } + + for (int i = n; i >= 1; i--) { + int poz = aint.query(n, input[i]); + result[poz] = i; + aint.update(n, poz); + } + + for (int i = 1; i <= n; i++) { + fout << result[i] << '\n'; + } + return 0; +} diff --git a/ski/seg_tree/0bd98531-20ae-4870-8170-f73454484a81.cpp b/ski/seg_tree/0bd98531-20ae-4870-8170-f73454484a81.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b908e6f685c0f56587e7971816b4300e375f0e9 --- /dev/null +++ b/ski/seg_tree/0bd98531-20ae-4870-8170-f73454484a81.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +FILE *f,*g; + +int arbint[4*30001],N,P,v[30002],S[30002],Value,NOD; + +void CreateFirstTree(int Nod,int Left,int Right) + +{ + + int Middle=(Right+Left)/2; + + if(Right==Left) + + { + + arbint[Nod]=1;return; + + } + + if(P<=Middle)CreateFirstTree(Nod*2,Left,Middle); + + else + + CreateFirstTree(Nod*2+1,Middle+1,Right); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + + + +} + +int M; + +void Read() + +{ + + int i; + + M=N; + + for(i=1;i<=N;i++) + + fscanf(f,"%d",&v[i]),P=i,CreateFirstTree(1,1,N); + +} + +void DeletePozX(int Nod,int Left,int Right,int PP) + +{ + + int Middle=(Left+Right)/2; + + if(Left==Right) + + { + + S[Left]=NOD;arbint[Nod]=0; + + return; + + } + + if(PP<=arbint[2*Nod]) + + DeletePozX(2*Nod,Left,Middle,PP); + + else + + DeletePozX(2*Nod+1,Middle+1,Right,PP-arbint[2*Nod]); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + +} + +void Displaying() + +{ + + int i=1; + + while(M) + + { + + fprintf(g,"%d\n",S[i]);M--;i++; + + } + +} + +int main() + +{ + + f=fopen("schi.in","r");g=fopen("schi.out","w"); + + fscanf(f,"%d",&N); + + Read(); + + while(N) + + { + + NOD=N; + + DeletePozX(1,1,M,v[N]); + + N--; + + } + + Displaying(); + + fclose(f);fclose(g); + + return 0; + +} diff --git a/ski/seg_tree/0c67a9c7-d18a-4773-be6a-5dba48d0cf22.cpp b/ski/seg_tree/0c67a9c7-d18a-4773-be6a-5dba48d0cf22.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b908e6f685c0f56587e7971816b4300e375f0e9 --- /dev/null +++ b/ski/seg_tree/0c67a9c7-d18a-4773-be6a-5dba48d0cf22.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + +FILE *f,*g; + +int arbint[4*30001],N,P,v[30002],S[30002],Value,NOD; + +void CreateFirstTree(int Nod,int Left,int Right) + +{ + + int Middle=(Right+Left)/2; + + if(Right==Left) + + { + + arbint[Nod]=1;return; + + } + + if(P<=Middle)CreateFirstTree(Nod*2,Left,Middle); + + else + + CreateFirstTree(Nod*2+1,Middle+1,Right); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + + + +} + +int M; + +void Read() + +{ + + int i; + + M=N; + + for(i=1;i<=N;i++) + + fscanf(f,"%d",&v[i]),P=i,CreateFirstTree(1,1,N); + +} + +void DeletePozX(int Nod,int Left,int Right,int PP) + +{ + + int Middle=(Left+Right)/2; + + if(Left==Right) + + { + + S[Left]=NOD;arbint[Nod]=0; + + return; + + } + + if(PP<=arbint[2*Nod]) + + DeletePozX(2*Nod,Left,Middle,PP); + + else + + DeletePozX(2*Nod+1,Middle+1,Right,PP-arbint[2*Nod]); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + +} + +void Displaying() + +{ + + int i=1; + + while(M) + + { + + fprintf(g,"%d\n",S[i]);M--;i++; + + } + +} + +int main() + +{ + + f=fopen("schi.in","r");g=fopen("schi.out","w"); + + fscanf(f,"%d",&N); + + Read(); + + while(N) + + { + + NOD=N; + + DeletePozX(1,1,M,v[N]); + + N--; + + } + + Displaying(); + + fclose(f);fclose(g); + + return 0; + +} diff --git a/ski/seg_tree/0f976c00-b72c-43ec-9d67-47a0e4d770a8.cpp b/ski/seg_tree/0f976c00-b72c-43ec-9d67-47a0e4d770a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf58558165efceb950abb26ecae3ebb67509e99a --- /dev/null +++ b/ski/seg_tree/0f976c00-b72c-43ec-9d67-47a0e4d770a8.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int a[30004],v[4*30004],sum,n,i,sol[30004]; + +void build(int nod,int st,int dr){ + + if(st==dr){ + + v[nod]=1; + + return; + + } + + int mid=(st+dr)/2; + + build(nod*2,st,mid); + + build(nod*2+1,mid+1,dr); + + v[nod]=v[nod*2]+v[nod*2+1]; + +} + +int query(int nod,int st,int dr){ + + + + if(st==dr){ + + v[nod]=0; + + return st; + + } + + int mid=(st+dr)/2; + + v[nod]--; + + if(v[2*nod]>=sum) + + return query(2*nod,st,mid); + + sum-=v[2*nod]; + + return query(2*nod+1,mid+1,dr); + +} + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>a[i]; + + build(1,1,n); + + for(i=n;i>=1;i--){ + + sum=a[i]; + + int poz= query(1,1,n); + + sol[poz]=i; + + } + + for(i=1;i<=n;i++) + + fout< +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + if (st == dr) + + { + + aint[idx] = 1; + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + aint[idx] = aint[fiu_st] + aint[fiu_dr]; + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + if (st == dr) + + { + + aint[idx]--; + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + aint[idx]--; + + return query(fiu_st, st, mij, val); + + } + + else + + { + + aint[idx]--; + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/19606286-442c-440f-933c-a23e9193a931.cpp b/ski/seg_tree/19606286-442c-440f-933c-a23e9193a931.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d8832cb010360a035ac62d66cdcdaef4bbe969 --- /dev/null +++ b/ski/seg_tree/19606286-442c-440f-933c-a23e9193a931.cpp @@ -0,0 +1,113 @@ +#include +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int A[90000],n,x[30000],sol[30000]; + +void creare(int st,int dr,int nod){ + + if(st==dr){ + + A[nod]=1; + + + + } + + else{ + + int mid=(st+dr)/2; + + creare(st,mid,2*nod); + + creare(mid+1,dr,2*nod+1); + + A[nod]=A[2*nod]+A[2*nod+1]; + + + + + + } + + + + + +} + + + +void update(int st, int dr, int nod,int poz,int k){ + + if(st==dr){ + + sol[st]=k; + + A[nod]=0; + + } + + else{ + + int mid=(st + dr)/2; + + if(A[2*nod]>=poz){ + + update(st,mid,2*nod,poz,k); + + } + + else{ + + update(mid+1,dr,2*nod+1,poz-A[2*nod],k); + + } + + A[nod]=(A[2*nod]+A[2*nod+1]); + + } + +} + + + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>x[i]; + + } + + + + creare(1,n,1); + + + + for(int i=n;i>=1;i--){ + + update(1,n,1,x[i],i); + + } + + for(int i=1;i<=n;i++){ + + fout< + +using namespace std; + +typedef unsigned long long ull; +typedef signed long long ll; +typedef unsigned int uint; +typedef unsigned short ush; +const int nmax = 30001; + +struct node_t { + short lower, mini, pos; +}; +node_t arbint[nmax * 4 + 66]; +short v[nmax]; + +void build(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].lower = 0; + arbint[nodei].mini = v[lefti]; + arbint[nodei].pos = lefti; + return ; + } + + uint midi = (lefti + righti) / 2; + build(2 * nodei + 1, lefti, midi); + build(2 * nodei + 2, midi + 1, righti); + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } +} + +void update(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].mini = 31000; + return ; + } + + uint midi = (lefti + righti) / 2; + + if (arbint[2 * nodei + 1].mini < arbint[2 * nodei + 2].mini) { + arbint[2 * nodei + 2].lower++; + arbint[2 * nodei + 2].mini--; + update(2 * nodei + 1, lefti, midi); + } else { + update(2 * nodei + 2, midi + 1, righti); + } + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } + arbint[nodei].mini -= arbint[nodei].lower; +} + +int main() { + freopen("schi.in", "r", stdin); + freopen("schi.out", "w", stdout); + + uint n; + scanf("%u", &n); + for (uint i =0;i + +using namespace std; + +const int Nmax = 30005; + +int finishes[Nmax]; +int segTree[3 * Nmax]; +ifstream cin ("schi.in"); +ofstream cout ("schi.out"); +void build(int node, int left, int right) { + if (left == right) { + segTree[node] = 1; + return; + } + int mid = (left + right) / 2; + build(2* node, left, mid); + build(2*node+1, mid+1, right); + segTree[node] = segTree[2*node] + segTree[2*node+1]; +} + +void update(int node, int left, int right, int position) { + if (left == right) { + segTree[node] = 0; + return; + } + int mid = (left + right) / 2; + if (position <= mid) { + update(2 * node, left, mid, position); + } else { + update(2 * node + 1, mid + 1, right, position); + } + segTree[node] = segTree[2 * node] + segTree[2 * node + 1]; +} + +int query(int node, int left, int right, int value) { + if (left == right) { + return left; + } + int mid = (left + right) / 2; + if (value <= segTree[2*node]) { + return query(2 * node, left, mid, value); + } else { + return query(2 * node + 1, mid + 1, right, value - segTree[2 * node]); + } +} + +int leaderboard[Nmax]; + +int main() { + int n; + cin >> n; + build(1, 1, n); + for (int i = 1; i <= n; ++i) { + cin >> finishes[i]; + } + for (int i = n; i >= 1; --i) { + int pos = query(1, 1, n, finishes[i]); + leaderboard[pos] = i; + update(1, 1, n, pos); + } + for (int i = 1; i <= n; ++i) { + cout << leaderboard[i] << "\n"; + } + return 0; +} diff --git a/ski/seg_tree/2a8505f7-c994-4a05-9a46-8071f1307f69.cpp b/ski/seg_tree/2a8505f7-c994-4a05-9a46-8071f1307f69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11ac7c39334b519497558425655a2684c91a0af0 --- /dev/null +++ b/ski/seg_tree/2a8505f7-c994-4a05-9a46-8071f1307f69.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int N = 30000; + +int arb[4*N]; +int ord[N + 1], available[N + 1], sol[N + 1]; + +void uniteNodes(int parent, int leftChild, int rightChild){ + arb[parent] = arb[leftChild] + arb[rightChild]; +} + +void build(int node, int left, int right){ + if(left == right){ + arb[node] = available[left] = 1; + return; + } + int mij = (left + right)/2; + build(2*node, left, mij); + build(2*node + 1, mij + 1, right); + uniteNodes(node, 2*node, 2*node + 1); +} + +void update(int node, int left, int right, int index, int val){ + if(left == right){ + sol[left] = val; + available[left] = 0; + arb[node] = 0; + return; + } + int mij = (left + right)/2; + if(index <= arb[2*node]) + update(2*node, left, mij, index, val); + else + update(2*node + 1, mij + 1, right, index - arb[2*node], val); + uniteNodes(node, 2*node, 2*node + 1); +} + +int main() +{ + int n,i; + fin >> n; + for(i=1; i<=n; i++) + fin >> ord[i]; + build(1, 1, n); + for(i=n; i>=1; i--) + update(1, 1, n, ord[i], i); + for(i=1; i<=n; i++) + fout << sol[i] << "\n"; + return 0; +} diff --git a/ski/seg_tree/2bdcccc1-049a-4f8a-815b-92ac7f9b0554.cpp b/ski/seg_tree/2bdcccc1-049a-4f8a-815b-92ac7f9b0554.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb10e516eb5e25b36ca726a46a84bf6d27a3ff2 --- /dev/null +++ b/ski/seg_tree/2bdcccc1-049a-4f8a-815b-92ac7f9b0554.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/2d1cfa16-ec4a-4681-8d01-c7bea5b77b66.cpp b/ski/seg_tree/2d1cfa16-ec4a-4681-8d01-c7bea5b77b66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47bbc929567fe5d3d46be0fe09cd1496776951e8 --- /dev/null +++ b/ski/seg_tree/2d1cfa16-ec4a-4681-8d01-c7bea5b77b66.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +const int N=1<<17; + +const int Nmax=30000; + + + +int t[N],v[Nmax],sol[Nmax],nod,poz; + + + +void actualizare (int p, int st, int dr) + +{ + + if (st==dr) + + { + + sol[st]=nod; + + t[p]++; + + return; + + } + + int m=(st+dr)/2; + + if (poz<=(m-st+1-t[2*p])) + + { + + actualizare (p*2,st,m); + + } + + else + + { + + poz-=(m-st+1-t[p*2]); + + actualizare (p*2+1,m+1,dr); + + } + + t[p]++; + +} + +int main() + +{ + + int n; + + in>>n; + + t[1]=1; + + for (int i=1;i<=n;i++) + + in>>v[i]; + + + + for (int i=n;i>0;i--) + + { + + nod=i; + + poz=v[i]; + + actualizare (1,1,n); + + } + + for (int i=1;i<=n;i++) + + out< + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +void update ( int nod, int left, int right, int poz, int val ); + + + +int f_find ( int nod, int left, int right, int poz ); + + + +int aint[120137]; + + + +int n; + + + +int v[30137]; + + + +int ans[30137]; + + + +int gasit; + + + +int main() + +{ + + in >> n; + + for ( register int i = 1 ; i <= n ; ++i ) + + { + + in >> v[i]; + + update ( 1 , 1 , n , i , 1 ); + + } + + + + for ( register int i = n ; i >= 1 ; --i ) + + { + + gasit = f_find ( 1 , 1 , n , v[i] ); + + ans[gasit] = i; + + update ( 1 , 1 , n , gasit , 0 ); + + } + + for ( register int i = 1 ; i <= n ; ++i ) + + out << ans[i] << '\n'; + + return 0; + +} + + + +void update ( int nod, int left, int right, int poz, int val ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + { + + aint[nod] = val; + + return ; + + } + + if ( poz <= mid ) + + update ( nod * 2 , left , mid , poz , val ); + + else + + update ( nod * 2 + 1 , mid + 1 , right , poz , val ); + + aint[nod] = aint[nod * 2] + aint[nod * 2 + 1]; + +} + + + +int f_find ( int nod, int left, int right, int poz ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + return left; + + if ( poz <= aint[nod * 2] ) + + return f_find ( nod * 2 , left , mid , poz ); + + else + + return f_find ( nod * 2 + 1 , mid + 1 , right , poz - aint[nod * 2] ); + +} diff --git a/ski/seg_tree/34f8367a-8d8a-49e5-a740-1cf98f891845.cpp b/ski/seg_tree/34f8367a-8d8a-49e5-a740-1cf98f891845.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf3ef973d2655b9c6ff1330c238d1dd358cec976 --- /dev/null +++ b/ski/seg_tree/34f8367a-8d8a-49e5-a740-1cf98f891845.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout("schi.out"); + + + +int v[30005], sol[30005], heap[3*30005]; + +int x, pos; + + + +inline void actualizare(int nod, int st, int dr) + +{ + + int mij; + + if(st == dr) + + heap[nod] = x; + + else + + { + + mij = (st+dr) / 2; + + if(st <= pos && pos <= mij) actualizare(nod*2, st, mij); + + if(mij+1 <= pos && pos <= dr) actualizare(nod*2+1, mij+1, dr); + + heap[nod] = heap[nod*2] + heap[nod*2+1]; + + } + +} + + + +inline int DEI(int nod, int st, int dr, int val) + +{ + + if(st == dr) return st; + + int mij = (st+dr) / 2; + + if(heap[nod*2] < val) return DEI(nod*2 + 1, mij+1, dr, val - heap[nod*2]); + + else return DEI(nod*2, st, mij, val); + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) fin >> v[i]; + + fin.close(); + + + + x = 1; + + for(int i = 1; i <= n; ++i) + + { + + pos = i; + + actualizare(1, 1, n); + + } + + for(int i = n; i; --i) + + { + + pos = DEI(1, 1, n, v[i]); + + sol[pos] = i; + + x = 0; + + actualizare(1, 1, n); + + } + + + + for(int i = 1; i <= n; ++i) fout << sol[i] << '\n'; + + fout.close(); + + return 0; + +} diff --git a/ski/seg_tree/38d45b9f-3e3e-4dea-a70e-8f95b15f7028.cpp b/ski/seg_tree/38d45b9f-3e3e-4dea-a70e-8f95b15f7028.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4933704aae26b5cd706962f65a7fbaf9ef791f02 --- /dev/null +++ b/ski/seg_tree/38d45b9f-3e3e-4dea-a70e-8f95b15f7028.cpp @@ -0,0 +1,130 @@ +#include + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +void update ( int nod, int left, int right, int poz, int val ); + + + +int f_find ( int nod, int left, int right, int poz ); + + + +int aint[120137]; + + + +int n; + + + +int v[30137]; + + + +int ans[30137]; + + + +int gasit; + + + +int main() + +{ + + in >> n; + + for ( register int i = 1 ; i <= n ; ++i ) + + { + + in >> v[i]; + + update ( 1 , 1 , n , i , 1 ); + + } + + + + for ( register int i = n ; i >= 1 ; --i ) + + { + + gasit = f_find ( 1 , 1 , n , v[i] ); + + ans[gasit] = i; + + update ( 1 , 1 , n , gasit , 0 ); + + } + + for ( register int i = 1 ; i <= n ; ++i ) + + out << ans[i] << '\n'; + + return 0; + +} + + + +void update ( int nod, int left, int right, int poz, int val ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + { + + aint[nod] = val; + + return ; + + } + + if ( poz <= mid ) + + update ( nod * 2 , left , mid , poz , val ); + + else + + update ( nod * 2 + 1 , mid + 1 , right , poz , val ); + + aint[nod] = aint[nod * 2] + aint[nod * 2 + 1]; + +} + + + +int f_find ( int nod, int left, int right, int poz ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + return left; + + if ( poz <= aint[nod * 2] ) + + return f_find ( nod * 2 , left , mid , poz ); + + else + + return f_find ( nod * 2 + 1 , mid + 1 , right , poz - aint[nod * 2] ); + +} diff --git a/ski/seg_tree/43d10b45-772d-4629-bda8-df3c87663d55.cpp b/ski/seg_tree/43d10b45-772d-4629-bda8-df3c87663d55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5776d3ba226fe1dc5aa0fb1bf7e1ec08fc28920 --- /dev/null +++ b/ski/seg_tree/43d10b45-772d-4629-bda8-df3c87663d55.cpp @@ -0,0 +1,136 @@ +#include + + +int v[30000]; + + + +int loc[30000]; + + + +int an[65536]; + + + +void update(int st,int dr,int nod,int p) + +{ + + if(st==dr) + + { + + an[p]+=1; + + } + + else + + { + + int mij=st+dr; + + mij/=2; + + if(nod<=mij) + + update(st,mij,nod,p*2); + + else update(mij+1,dr,nod,p*2+1); + + an[p]=an[p*2]+an[p*2+1]; + + } + +} + + + +int get(int st,int dr,int p,int s) + +{ + + if(st==dr) + + return st; + + else + + { + + int mij=st+dr; + + mij/=2; + + int opens=(mij-st+1)-an[p*2]; + + int opend=(dr-mij)-an[p*2+1]; + + if(opens + + + + + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout("schi.out"); + + + +int v[30005], sol[30005], heap[3*30005]; + +int x, pos; + + + +inline void actualizare(int nod, int st, int dr) + +{ + + int mij; + + if(st == dr) + + heap[nod] = x; + + else + + { + + mij = (st+dr) / 2; + + if(st <= pos && pos <= mij) actualizare(nod*2, st, mij); + + if(mij+1 <= pos && pos <= dr) actualizare(nod*2+1, mij+1, dr); + + heap[nod] = heap[nod*2] + heap[nod*2+1]; + + } + +} + + + +inline int DEI(int nod, int st, int dr, int val) + +{ + + if(st == dr) return st; + + int mij = (st+dr) / 2; + + if(heap[nod*2] < val) return DEI(nod*2 + 1, mij+1, dr, val - heap[nod*2]); + + else return DEI(nod*2, st, mij, val); + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) fin >> v[i]; + + fin.close(); + + + + x = 1; + + for(int i = 1; i <= n; ++i) + + { + + pos = i; + + actualizare(1, 1, n); + + } + + for(int i = n; i; --i) + + { + + pos = DEI(1, 1, n, v[i]); + + sol[pos] = i; + + x = 0; + + actualizare(1, 1, n); + + } + + + + for(int i = 1; i <= n; ++i) fout << sol[i] << '\n'; + + fout.close(); + + return 0; + +} diff --git a/ski/seg_tree/4fa276d8-46b7-4053-8e45-5bd1a362b181.cpp b/ski/seg_tree/4fa276d8-46b7-4053-8e45-5bd1a362b181.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db8c53daa45827cb18f3ce001e0f07ab10ea5ff4 --- /dev/null +++ b/ski/seg_tree/4fa276d8-46b7-4053-8e45-5bd1a362b181.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + + + +ifstream x("schi.in"); + +ofstream y("schi.out"); + + + +int n,i,nr,pos,v[30002]; + + + +struct elem + +{ + + int s,lazy; + +}a[4*30002]; + + + +void build(int nod, int left, int right) + +{ + + if(left==right) + + { + + a[nod].lazy=1; + + return; + + } + + + + int mij=(left+right)/2; + + + + build(nod*2,left,mij); + + build(nod*2+1,mij+1,right); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void update(int nod, int left, int right, int m, int val) + +{ + + if(left==right) + + { + + a[right].s=val; + + a[nod].lazy=0; + + return; + + } + + + + int mij=(left+right)/2; + + + + if(m<=a[nod*2].lazy) + + update(nod*2,left,mij,m,val); + + else + + update(nod*2+1,mij+1,right,m-a[nod*2].lazy,val); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void citire() + +{ + + x>>n; + + for(i=1;i<=n;i++) + + x>>v[i]; + + build(1,1,n); + +} + + + +int main() + +{ + + citire(); + + for(i=n;i>=1;i--) + + update(1,1,n,v[i],i); + + for(i=1;i<=n;i++) + + y< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +int tree[4 * 30001 + 13]; + +int N, M, A[30001], B[30001], pozi, i; + +void Update(int nod, int currL, int currR, int pos, int val) { + + if ( currL == currR ) { + + tree[nod] = val; + + return; + + } + + + + int mid = currL + (currR - currL) / 2; + + if ( pos <= mid ) Update(2 * nod, currL, mid, pos, val); + + else Update(2 * nod + 1, mid + 1, currR, pos, val); + + + + tree[nod] = tree[2 * nod] + tree[2 * nod + 1]; + +} + + + +int Query(int nod, int currL, int currR, int val){ + + if (currL == currR) return currL; + + int mid = currL + (currR - currL) / 2; + + if (val <= tree[2 * nod]) return Query(2 * nod, currL, mid, val); + + else { + + val -= tree[2 * nod]; + + return Query(2 * nod + 1, mid + 1, currR, val); + + } + +} + + + +int main() { + + f >> N; + + for (i = 1; i <= N; ++i) { + + Update(1, 1, N, i, 1); + + f >> A[i]; + + } + + for (i = N; i >= 1; --i) { + + pozi = Query(1, 1, N, A[i]); + + Update(1, 1, N, pozi, 0); + + B[pozi] = i; + + } + + for (i = 1; i <= N; ++i) + + g << B[i] << '\n'; + +} diff --git a/ski/seg_tree/532b5676-ce3b-4c32-bcb1-2df5109e3078.cpp b/ski/seg_tree/532b5676-ce3b-4c32-bcb1-2df5109e3078.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9987da84c84de3e21fc7b3aff9554d832f66dccb --- /dev/null +++ b/ski/seg_tree/532b5676-ce3b-4c32-bcb1-2df5109e3078.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +ifstream fin("schi.in"); ofstream fout("schi.out"); + + + + + + + +int v[300005]; + +int ans[300005]; + +int gr[1200005]; + + + +int n; + + + + + + + +void update(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + gr[nod] = val; + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (pos <= mid) update(nod * 2, st, mid, pos, val); + + else update(nod * 2 + 1, mid + 1, dr, pos, val); + + + + gr[nod] = gr[nod * 2] + gr[nod * 2 + 1]; + +} + + + +void query(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + ans[st] = val; + + update(1, 1, n, st, 0); + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (gr[nod * 2] >= pos) query(nod * 2, st, mid, pos, val); + + else query(nod * 2 + 1, mid + 1, dr, pos - gr[nod * 2] , val); + + + +} + + + + + +int main() { + + + + fin >> n; + + + + for (int i = 1; i <= n; i++) { + + update(1, 1, n, i, 1); + + fin >> v[i]; + + } + + + + for (int i = n; i >= 1; i--) query(1, 1, n, v[i], i); + + + + for (int i = 1; i <= n; i++) { + + fout << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/54d44e75-8365-4fbe-8601-34fb24f962a0.cpp b/ski/seg_tree/54d44e75-8365-4fbe-8601-34fb24f962a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a19a29b99e31a4000233d069896c9c59e21112c --- /dev/null +++ b/ski/seg_tree/54d44e75-8365-4fbe-8601-34fb24f962a0.cpp @@ -0,0 +1,83 @@ +#include + +using namespace std; + +typedef unsigned long long ull; +typedef signed long long ll; +typedef unsigned int uint; +typedef unsigned short ush; +const int nmax = 30001; + +struct node_t { + short lower, mini, pos; +}; +node_t arbint[nmax * 4 + 66]; +short v[nmax]; + +void build(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].lower = 0; + arbint[nodei].mini = v[lefti]; + arbint[nodei].pos = lefti; + return ; + } + + uint midi = (lefti + righti) / 2; + build(2 * nodei + 1, lefti, midi); + build(2 * nodei + 2, midi + 1, righti); + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } +} + +void update(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].mini = 31000; + return ; + } + + uint midi = (lefti + righti) / 2; + + if (arbint[2 * nodei + 1].mini < arbint[2 * nodei + 2].mini) { + arbint[2 * nodei + 2].lower++; + arbint[2 * nodei + 2].mini--; + update(2 * nodei + 1, lefti, midi); + } else { + update(2 * nodei + 2, midi + 1, righti); + } + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } + arbint[nodei].mini -= arbint[nodei].lower; +} + +int main() { + freopen("schi.in", "r", stdin); + freopen("schi.out", "w", stdout); + + uint n; + scanf("%u", &n); + for (uint i =0;i + + + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int NMAX = 30005; + +int v[NMAX], aint[4 * NMAX]; + + + +void update(int pos, int state, int node, int le, int ri) { + + if(le == ri) { + + aint[node] = state; + + } else { + + int mid = (le + ri) / 2; + + if(pos <= mid) + + update(pos, state, node * 2, le, mid); + + else + + update(pos, state, node * 2 + 1, mid + 1, ri); + + aint[node] = aint[node * 2] + aint[node * 2 + 1]; + + } + +} + + + +int query(int pos, int node, int le, int ri) { + + if(le == ri) + + return le; + + int mid = (le + ri) / 2; + + if(aint[node * 2] >= pos) + + return query(pos, node * 2, le, mid); + + else + + return query(pos - aint[node * 2], node * 2 + 1, mid + 1, ri); + +} + + + +int main() { + + int n; + + in >> n; + + for(int i = 1; i <= n; i ++) { + + in >> v[i]; + + update(i, 1, 1, 1, n); + + } + + vector sol(n + 1, 0); + + for(int i = n; i >= 1; i --) { + + int pos = query(v[i], 1, 1, n); + + sol[pos] = i; + + update(pos, 0, 1, 1, n); + + } + + for(int i = 1; i <= n; i ++) + + out << sol[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/570d0fb6-b36b-4e20-aeb0-49e0cb62be54.cpp b/ski/seg_tree/570d0fb6-b36b-4e20-aeb0-49e0cb62be54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a5ad327252c4c90f0250bf93fc53e0e6f1de80 --- /dev/null +++ b/ski/seg_tree/570d0fb6-b36b-4e20-aeb0-49e0cb62be54.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, i, a[100010], v[30010], sol[30010]; + + + +void initializeaza(int st, int dr, int nod){ + + if(st==dr){ + + a[nod]=1; + + }else{ + + int mid=(st+dr)/2; + + initializeaza(st, mid, nod*2); + + initializeaza(mid+1, dr, nod*2+1); + + a[nod]=a[nod*2]+a[nod*2+1]; + + } + +} + + + +void gaseste(int st, int dr, int nod, int pozCautata){ + + if(st==dr){ + + a[nod]=0; + + sol[st]=i; + + }else{ + + int mid=(st+dr)/2; + + if(a[2*nod]>=pozCautata){ + + + + gaseste(st, mid, 2*nod, pozCautata); + + }else{ + + + + + + gaseste(mid+1, dr, 2*nod+1, pozCautata-a[2*nod]); + + } + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + + + +int main(){ + + fin>>n; + + for(i=1; i<=n; i++){ + + fin>>v[i]; + + } + + initializeaza(1, n, 1); + + for(i=n; i>0; i--){ + + gaseste(1, n, 1, v[i]); + + } + + for(i=1; i<=n; i++){ + + fout< + + +int v[30000]; + + + +int loc[30000]; + + + +int an[65536]; + + + +void update(int st,int dr,int nod,int p) + +{ + + if(st==dr) + + { + + an[p]+=1; + + } + + else + + { + + int mij=st+dr; + + mij/=2; + + if(nod<=mij) + + update(st,mij,nod,p*2); + + else update(mij+1,dr,nod,p*2+1); + + an[p]=an[p*2]+an[p*2+1]; + + } + +} + + + +int get(int st,int dr,int p,int s) + +{ + + if(st==dr) + + return st; + + else + + { + + int mij=st+dr; + + mij/=2; + + int opens=(mij-st+1)-an[p*2]; + + int opend=(dr-mij)-an[p*2+1]; + + if(opens + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i,v[30010],sol[30010],A[3*30010]; + +void build(int nod,int st,int dr) { + + if (st==dr) + + A[nod]=1; + + else { + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +void update(int nod,int st,int dr,int x,int poz) { + + if (st==dr) { + + A[nod]=0; + + sol[st]=x; + + } + + else { + + int mid=(st+dr)/2; + + if (A[2*nod]>=poz) + + update(2*nod,st,mid,x,poz); + + else + + update(2*nod+1,mid+1,dr,x,poz-A[2*nod]); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for (i=n;i>=1;i--) + + update(1,1,n,i,v[i]); + + for (i=1;i<=n;i++) + + fout< +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +typedef pair pairINT; + +typedef long long ll; + +class SegmentTree{ + + public: + + int n; + + int query(int poz){ + + return qry(1,1,n,poz); + + } + + private: + + int segm[(int)(3e4+4)*4]; + + int qry(int node,int l,int r, int poz){ + + if(l == r){ + + segm[node]=1; + + return l; + + } + + int ans,mid=(l+r)>>1,amm=(mid-l+1)-segm[node<<1]; + + if(amm >= poz) + + ans=qry(node<<1,l,mid,poz); + + else + + ans=qry(node<<1|1,mid+1,r,poz-amm); + + segm[node]=segm[node<<1]+segm[node<<1|1]; + + return ans; + + } + +}; + + + +int n,v[(int)(3e4+4)],sol[(int)(3e4+4)]; + +SegmentTree Arb; + + + +int main(){ + + fin>>n; + + Arb.n=n;; + + for(int i=1;i<=n;++i) + + fin>>v[i]; + + for(int i=n;i>0;--i) + + sol[Arb.query(v[i])]=i; + + for(int i=1;i<=n;++i) + + fout< +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, m, val, pos, rez; + +int v[30002],sol[30002], Arbore[4 * 30002]; + + + + + +void update(int nod, int stanga, int dreapta) + +{ + + if (stanga == dreapta) + + { + + Arbore[nod] = val; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (pos <= mij) + + update(2 * nod, stanga, mij); + + else + + update(2 * nod + 1, mij + 1, dreapta); + + Arbore[nod] = Arbore[2*nod]+Arbore[2*nod+1]; + +} + + + +void query(int nod, int stanga, int dreapta) + +{ + + if (stanga==dreapta) + + { + + rez = stanga; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (Arbore[2 * nod] >= val) + + query(2 * nod, stanga, mij); + + else + + { + + val = val - Arbore[2 * nod]; + + query(2 * nod + 1, mij + 1, dreapta); + + } + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + { + + fin >> v[i]; + + pos = i; + + val = 1; + + update(1, 1, n); + + } + + for (int i = n; i >= 1; i--) + + { + + rez = 0; + + val = v[i]; + + query(1, 1, n); + + sol[rez] = i; + + pos = rez; + + val = 0; + + update(1, 1, n); + + } + + for (int i = 1; i <= n; i++) + + fout << sol[i] << "\n"; + + + + return 0; + +} diff --git a/ski/seg_tree/679e7406-f8df-4446-aa93-1d5c85b30fde.cpp b/ski/seg_tree/679e7406-f8df-4446-aa93-1d5c85b30fde.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d6c25f667dd0493b1dde244cc97fe47fc61fc4f --- /dev/null +++ b/ski/seg_tree/679e7406-f8df-4446-aa93-1d5c85b30fde.cpp @@ -0,0 +1,133 @@ +#include +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, m, val, pos, rez; + +int v[30002],sol[30002], Arbore[4 * 30002]; + + + + + +void update(int nod, int stanga, int dreapta) + +{ + + if (stanga == dreapta) + + { + + Arbore[nod] = val; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (pos <= mij) + + update(2 * nod, stanga, mij); + + else + + update(2 * nod + 1, mij + 1, dreapta); + + Arbore[nod] = Arbore[2*nod]+Arbore[2*nod+1]; + +} + + + +void query(int nod, int stanga, int dreapta) + +{ + + if (stanga==dreapta) + + { + + rez = stanga; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (Arbore[2 * nod] >= val) + + query(2 * nod, stanga, mij); + + else + + { + + val = val - Arbore[2 * nod]; + + query(2 * nod + 1, mij + 1, dreapta); + + } + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + { + + fin >> v[i]; + + pos = i; + + val = 1; + + update(1, 1, n); + + } + + for (int i = n; i >= 1; i--) + + { + + rez = 0; + + val = v[i]; + + query(1, 1, n); + + sol[rez] = i; + + pos = rez; + + val = 0; + + update(1, 1, n); + + } + + for (int i = 1; i <= n; i++) + + fout << sol[i] << "\n"; + + + + return 0; + +} diff --git a/ski/seg_tree/6870a06e-137c-11eb-a68d-309c234925a0.cpp b/ski/seg_tree/6870a06e-137c-11eb-a68d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..39cd7bc67abad96eaf0166b69357a12777f111f1 --- /dev/null +++ b/ski/seg_tree/6870a06e-137c-11eb-a68d-309c234925a0.cpp @@ -0,0 +1,158 @@ +#include +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + if (st == dr) + + { + + aint[idx] = 1; + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + aint[idx] = aint[fiu_st] + aint[fiu_dr]; + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + aint[idx]--; + + + + if (st == dr) + + { + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + return query(fiu_st, st, mij, val); + + } + + else + + { + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/687fe240-137c-11eb-ac92-309c234925a0.cpp b/ski/seg_tree/687fe240-137c-11eb-ac92-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16bf8963d57909ff50afb13ae12ebca16fa5c12f --- /dev/null +++ b/ski/seg_tree/687fe240-137c-11eb-ac92-309c234925a0.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +using namespace std; + +const int N = 30005; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +int n; +int arb[N * 4], init[N], place[N]; + +void build(int left = 1, int right = n, int index = 1) +{ + if(left == right) + { + arb[index] = 1; + return; + } + + int mid = (left + right) / 2; + build(left, mid, index * 2); + build(mid + 1, right, index * 2 + 1); + arb[index] = arb[index * 2] + arb[index * 2 + 1]; +} + +int getPlace(int p, int left = 1, int right = n, int index = 1) +{ + arb[index]--; + if(left == right) + return left; + + int mid = (left + right) / 2; + if(arb[index * 2] >= p) + return getPlace(p, left, mid, index*2); + return getPlace(p - arb[index * 2], mid + 1, right, index * 2 + 1); +} + +int main() +{ + fin >> n; + for(int i = 1; i <= n; i++) + fin >> init[i]; + + build(); + + for(int i = n; i > 0; i--) + { + int ans = getPlace(init[i]); + place[ans] = i; + } + + for(int i = 1; i <= n; i++) + fout << place[i] << '\n'; + fout << '\n'; + + return 0; +} diff --git a/ski/seg_tree/689ea224-137c-11eb-a3c4-309c234925a0.cpp b/ski/seg_tree/689ea224-137c-11eb-a3c4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..840f6d7595bbb14d69fdef6c65c9f271a0109e3f --- /dev/null +++ b/ski/seg_tree/689ea224-137c-11eb-a3c4-309c234925a0.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + +ifstream cin("schi.in"); +ofstream cout("schi.out"); + +const int MAXN = 3e4 + 5; +int n, v[MAXN], ans[MAXN], tree[4 * MAXN]; + +void Update (int nod, int st, int dr, int pos, int val){ + if (st == dr){ + tree[nod] = val; + return; + } + int mij = (st + dr) / 2; + if (pos <= mij){ + Update(nod * 2, st, mij, pos, val); + } + else{ + Update(nod * 2 + 1, mij + 1, dr, pos, val); + } + tree[nod] = tree[nod * 2] + tree[nod * 2 + 1]; +} + + +void read() { + cin>>n; + for (int i=1; i<=n; i++){ + Update (1, 1, n, i, 1); + cin>>v[i]; + } +} + +void Querry (int nod, int st, int dr, int pos, int val){ + if (st == dr){ + ans[st] = val; + Update (1, 1, n, st, 0); + return; + } + int mij = (st + dr) / 2; + if (tree[nod * 2] >= pos){ + Querry(nod * 2, st, mij, pos, val); + } + else{ + Querry(nod * 2 + 1, mij + 1, dr, pos - tree[nod * 2], val); + } +} + +void solve() { + read(); + for (int i=n; i>=1; i--) + Querry(1, 1, n, v[i], i); + + for (int i=1; i<=n; i++) + cout< + +using namespace std; + +const int Nmax = 30005; + +int finishes[Nmax]; +int segTree[3 * Nmax]; +ifstream cin ("schi.in"); +ofstream cout ("schi.out"); +void build(int node, int left, int right) { + if (left == right) { + segTree[node] = 1; + return; + } + int mid = (left + right) / 2; + build(2* node, left, mid); + build(2*node+1, mid+1, right); + segTree[node] = segTree[2*node] + segTree[2*node+1]; +} + +void update(int node, int left, int right, int position) { + if (left == right) { + segTree[node] = 0; + return; + } + int mid = (left + right) / 2; + if (position <= mid) { + update(2 * node, left, mid, position); + } else { + update(2 * node + 1, mid + 1, right, position); + } + segTree[node] = segTree[2 * node] + segTree[2 * node + 1]; +} + +int query(int node, int left, int right, int value) { + if (left == right) { + return left; + } + int mid = (left + right) / 2; + if (value <= segTree[2*node]) { + return query(2 * node, left, mid, value); + } else { + return query(2 * node + 1, mid + 1, right, value - segTree[2 * node]); + } +} + +int leaderboard[Nmax]; + +int main() { + int n; + cin >> n; + build(1, 1, n); + for (int i = 1; i <= n; ++i) { + cin >> finishes[i]; + } + for (int i = n; i >= 1; --i) { + int pos = query(1, 1, n, finishes[i]); + leaderboard[pos] = i; + update(1, 1, n, pos); + } + for (int i = 1; i <= n; ++i) { + cout << leaderboard[i] << "\n"; + } + return 0; +} diff --git a/ski/seg_tree/68bd480c-137c-11eb-bcfc-309c234925a0.cpp b/ski/seg_tree/68bd480c-137c-11eb-bcfc-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..932a12f9c6b39dbae12357d614047a416a14badf --- /dev/null +++ b/ski/seg_tree/68bd480c-137c-11eb-bcfc-309c234925a0.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + aint[idx] = dr - st + 1; + + + + if (st == dr) + + { + + + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + aint[idx]--; + + + + if (st == dr) + + { + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + return query(fiu_st, st, mij, val); + + } + + else + + { + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/68cb0c24-137c-11eb-bf55-309c234925a0.cpp b/ski/seg_tree/68cb0c24-137c-11eb-bf55-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..babcf37ade124ff93c81c50197bd3b08f5d10447 --- /dev/null +++ b/ski/seg_tree/68cb0c24-137c-11eb-bf55-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include + + + +typedef long long ll; + + + +const ll mod=1e9+7; + + + +const int dx[] = {0, 1, 0, -1}; + +const int dy[] = {1, 0, -1, 0}; + +const int nmax=1e7+5; +using namespace std; + + + +ll t[nmax*4]; + +int a[nmax],b[nmax]; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +void build(int v, int tl, int tr) { + + if (tl==tr) { + + t[v]=1; + + } + + else { + + int tm=(tl+tr)/2; + + build(2*v,tl,tm); + + build(2*v+1,tm+1,tr); + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +void update(int v, int tl, int tr, int val, int pos) { + + if (tl==tr) { + + t[v]=0; + + b[tl]=val; + + return; + + } + + else { + + int tm=(tl+tr)/2; + + if (pos<=t[2*v]) { + + update(2*v,tl,tm,val,pos); + + } + + else { + + update(2*v+1,tm+1,tr,val,pos-t[2*v]); + + } + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +int main() { + +ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + + int n; + + f >> n; + + build(1,1,n); + + for (int i=1; i<=n; i++) { + + f >> a[i]; + + } + + for (int i=n; i>=1; i--) { + + update(1,1,n,i,a[i]); + + } + + for (int i=1; i<=n; i++) { + + g << b[i] << '\n'; + + } + +} diff --git a/ski/seg_tree/68e8139c-137c-11eb-a816-309c234925a0.cpp b/ski/seg_tree/68e8139c-137c-11eb-a816-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..baf861b8c3da8d5a866e6fa243d0440c9f2682dc --- /dev/null +++ b/ski/seg_tree/68e8139c-137c-11eb-a816-309c234925a0.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + if (st == dr) + + { + + aint[idx] = 1; + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + aint[idx] = aint[fiu_st] + aint[fiu_dr]; + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + if (st == dr) + + { + + aint[idx]--; + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + aint[idx]--; + + return query(fiu_st, st, mij, val); + + } + + else + + { + + aint[idx]--; + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/68f64440-137c-11eb-b7d6-309c234925a0.cpp b/ski/seg_tree/68f64440-137c-11eb-b7d6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9987da84c84de3e21fc7b3aff9554d832f66dccb --- /dev/null +++ b/ski/seg_tree/68f64440-137c-11eb-b7d6-309c234925a0.cpp @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +ifstream fin("schi.in"); ofstream fout("schi.out"); + + + + + + + +int v[300005]; + +int ans[300005]; + +int gr[1200005]; + + + +int n; + + + + + + + +void update(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + gr[nod] = val; + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (pos <= mid) update(nod * 2, st, mid, pos, val); + + else update(nod * 2 + 1, mid + 1, dr, pos, val); + + + + gr[nod] = gr[nod * 2] + gr[nod * 2 + 1]; + +} + + + +void query(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + ans[st] = val; + + update(1, 1, n, st, 0); + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (gr[nod * 2] >= pos) query(nod * 2, st, mid, pos, val); + + else query(nod * 2 + 1, mid + 1, dr, pos - gr[nod * 2] , val); + + + +} + + + + + +int main() { + + + + fin >> n; + + + + for (int i = 1; i <= n; i++) { + + update(1, 1, n, i, 1); + + fin >> v[i]; + + } + + + + for (int i = n; i >= 1; i--) query(1, 1, n, v[i], i); + + + + for (int i = 1; i <= n; i++) { + + fout << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/6905645c-137c-11eb-b220-309c234925a0.cpp b/ski/seg_tree/6905645c-137c-11eb-b220-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e800e6418d0171dc9f9853e0d985a6c71ca68367 --- /dev/null +++ b/ski/seg_tree/6905645c-137c-11eb-b220-309c234925a0.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout ("schi.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 3e4 + 5; + + + +int tree[N * 4], pos[N], a[N]; + + + +void build(int left, int right, int node) + +{ + + if(left == right) { + + tree[node] = 1; + + return ; + + } + + int mid = (left + right) >> 1; + + build(left, mid, node * 2); + + build(mid + 1, right, node * 2 + 1); + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + +} + + + +void update(int left, int right, int node, int position) + +{ + + if(left == right) { + + tree[node] = 0; + + return ; + + } + + int mid = (left + right) >> 1; + + if(position <= mid) { + + update(left, mid, node * 2, position); + + } + + else { + + update(mid + 1, right, node * 2 + 1, position); + + } + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + +} + + + +int query(int left, int right, int node, int position) + +{ + + if(left == right) { + + return left; + + } + + int mid = (left + right) >> 1; + + if(position <= tree[node * 2]) { + + return query(left, mid, node * 2, position); + + } + + else { + + return query(mid + 1, right, node * 2 + 1, position - tree[node * 2]); + + } + +} + + + +int main() + +{ + + usain_bolt(); + + + + int n; + + + + fin >> n; + + for(int i = 1; i <= n; ++i) { + + fin >> pos[i]; + + } + + build(1, n, 1); + + for(int i = n; i >= 1; --i) { + + int place = query(1, n, 1, pos[i]); + + a[place] = i; + + update(1, n, 1, place); + + } + + for(int i = 1; i <= n; ++i) fout << a[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/6922dc38-137c-11eb-96cb-309c234925a0.cpp b/ski/seg_tree/6922dc38-137c-11eb-96cb-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..606f504ced176b19485076f66e2de0e36a2859e2 --- /dev/null +++ b/ski/seg_tree/6922dc38-137c-11eb-96cb-309c234925a0.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +ifstream in("schi.in"); +ofstream out("schi.out"); + +int arb[120005],n,a[30005],ans[30005]; + +void create(int nod,int st,int dr) +{ + if(st==dr) + arb[nod]=1; + else + { + int mid=(st+dr)/2; + create(2*nod,st,mid); + create(2*nod+1,mid+1,dr); + arb[nod]=arb[nod*2]+arb[nod*2+1]; + } +} + +void update(int nod,int st,int dr,int k,int who) +{ + if(st==dr) + { + arb[nod]=0; + ans[st]=who; + return; + } + int mid=(st+dr)/2; + if(arb[nod*2]>n; + for(int i=1;i<=n;i++) + { + in>>a[i]; + } + + + create(1,1,n); + for(int i=n;i>0;i--) + { + update(1,1,n,a[i],i); + } + for(int i=1;i<=n;i++) + out< +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +int v[30001], rasp[30001]; +int a[120001]; + +int poz; + +void constr(int nod, int st, int dr) +{ + if (st == dr) + a[nod] = 1; + else + { + int mij = (st+dr)>>1; + constr(nod<<1, st, mij); + constr(nod<<1|1, mij+1, dr); + a[nod] = a[nod<<1] + a[nod<<1|1]; + } +} + +void update(int nod, int st, int dr) +{ + if (st == dr) + a[nod] = 0; + else + { + int mij = (st+dr)>>1; + if (poz <= mij) + update(nod<<1, st, mij); + else + update(nod<<1|1, mij+1, dr); + a[nod] = a[nod<<1] + a[nod<<1|1]; + } +} + +int queryk(int n, int k) +{ + int nod = 1, st = 1, dr = n, mij; + while (st != dr) + { + mij = (st+dr)>>1; + if (k <= a[nod<<1]) + { + dr = mij; + nod = nod << 1; + } + else + { + k = k - a[nod<<1]; + st = mij + 1; + nod = nod << 1 | 1; + } + } + return st; +} + +int main() +{ + int i, n; + fin >> n; + constr(1, 1, n); + for (i = 1; i<=n; i++) + fin >> v[i]; + for (i = n; i>=1; i--) + { + poz = queryk(n, v[i]); + rasp[poz] = i; + update(1, 1, n); + } + for (i = 1; i<=n; i++) + fout << rasp[i] << '\n'; + return 0; +} diff --git a/ski/seg_tree/698332ee-137c-11eb-a15f-309c234925a0.cpp b/ski/seg_tree/698332ee-137c-11eb-a15f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8dbd5fe4bafa252edf7388d1b83ae455bcfe2478 --- /dev/null +++ b/ski/seg_tree/698332ee-137c-11eb-a15f-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +ifstream f("schi.in"); +ofstream g("schi.out"); + +int arb[3000001], v[3000001], rasp[3000001], sol, n; + +void update(int nod, int st, int dr, int index, int val) { + if(st > dr) + return; + + if(st == dr) { + arb[nod] = val; + return; + } + + int mid = (st + dr) / 2; + if(index <= mid) + update(2*nod, st, mid, index, val); + else + update(2*nod+1, mid+1, dr, index, val); + arb[nod] = arb[2*nod] + arb[2*nod+1]; +} + +void query(int nod, int st, int dr, int val) +{ + if (st == dr) + { + sol = st; + return; + } + int mij = (st + dr)/2; + if (arb[2 * nod] < val) + { val = val - arb[2 * nod]; + query(2 * nod + 1, mij + 1, dr, val); + + } + else + query(2 * nod, st, mij, val); +} + +int main() { + f >> n; + + for(int i = 1; i <= n; i++) { + f >> v[i]; + update(1, 1, n, i, 1); + } + + for(int i = n; i > 0; i--) { + sol = 0; + query(1, 1, n, v[i]); + rasp[sol] = i; + update(1, 1, n, sol, 0); + } + + for(int i = 1; i <= n; i++) + g << rasp[i] << "\n"; + return 0; +} diff --git a/ski/seg_tree/6992f440-137c-11eb-88b5-309c234925a0.cpp b/ski/seg_tree/6992f440-137c-11eb-88b5-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ff2d85cd0a899fd6a90310b0ad4997deb2e79dd --- /dev/null +++ b/ski/seg_tree/6992f440-137c-11eb-88b5-309c234925a0.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,i; + +int v[30005], fin[30005], tree[4 * 30005]; + + + +void update(int l, int r, int node) + +{ + + if(l == r) + + tree[node] = 1; + + else{ + + int mid = (l + r) / 2; + + update(l, mid, 2 * node); + + update(mid + 1, r, 2 * node + 1); + + tree[node] = tree[2 * node] + tree[2 * node + 1]; + + } + +} + + + +void clasament(int l, int r, int node ,int pos){ + + if(l == r){ + + tree[node] = 0; + + fin[l] = i; + + } + + else{ + + int mid = (l + r) / 2; + + if(tree[2 * node] >= pos){ + + clasament(l ,mid ,2 * node, pos); + + } + + else{ + + clasament(mid + 1, r ,2 * node + 1,pos - tree[2 * node]); + + } + + tree[node] = tree[2 * node] + tree[2 * node + 1]; + + } + +} + + + +int main(){ + + f>>n; + + for(int i=1;i<=n;i++){ + + f>>v[i]; + + } + + update (1, n, 1); + + for(i = n; i >= 1; i--) + + clasament(1, n, 1, v[i]); + + for(i = 1; i <= n; i++) + + g< +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/69c05de2-137c-11eb-8b26-309c234925a0.cpp b/ski/seg_tree/69c05de2-137c-11eb-8b26-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb10e516eb5e25b36ca726a46a84bf6d27a3ff2 --- /dev/null +++ b/ski/seg_tree/69c05de2-137c-11eb-8b26-309c234925a0.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/69d19712-137c-11eb-8c46-309c234925a0.cpp b/ski/seg_tree/69d19712-137c-11eb-8c46-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d94af9c27f13e72f53759f2a4368c1b5da03a034 --- /dev/null +++ b/ski/seg_tree/69d19712-137c-11eb-8c46-309c234925a0.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int Nmax = 30005; + +int N,v[Nmax],res[Nmax],A[16*Nmax]; + +int Query(int amo,int st,int dr,int i) + +{ + + if(st==dr) return st; + + int mij=(st+dr)/2; + + if(amo <= A[2*i]) + + return Query(amo,st,mij,2*i); + + else + + { + + amo-=A[2*i]; + + return Query(amo,mij+1,dr,2*i+1); + + } + +} + +void Update(int wh,int val,int st,int dr,int i) + +{ + + A[i]+=val; + + if(st==dr) return; + + int mij=(st+dr)/2; + + if(wh<=mij) Update(wh,val,st,mij,2*i); + + else Update(wh,val,mij+1,dr,2*i+1); + +} + +int main(){ + + in>>N; + + for(int i=1;i<=N;i++) + + { + + in>>v[i]; + + Update(i,1,1,N,1); + + } + + for(int i=N;i>=1;i--) + + { + + int wh=Query(v[i],1,N,1); + + res[wh]=i; + + Update(wh,-1,1,N,1); + + } + + for(int i=1;i<=N;i++) + + out< +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, m, val, pos, rez; + +int v[30002],sol[30002], Arbore[4 * 30002]; + + + + + +void update(int nod, int stanga, int dreapta) + +{ + + if (stanga == dreapta) + + { + + Arbore[nod] = val; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (pos <= mij) + + update(2 * nod, stanga, mij); + + else + + update(2 * nod + 1, mij + 1, dreapta); + + Arbore[nod] = Arbore[2*nod]+Arbore[2*nod+1]; + +} + + + +void query(int nod, int stanga, int dreapta) + +{ + + if (stanga==dreapta) + + { + + rez = stanga; + + return; + + } + + int mij = (stanga + dreapta) / 2; + + if (Arbore[2 * nod] >= val) + + query(2 * nod, stanga, mij); + + else + + { + + val = val - Arbore[2 * nod]; + + query(2 * nod + 1, mij + 1, dreapta); + + } + +} + + + +int main() + +{ + + fin >> n; + + for (int i = 1; i <= n; i++) + + { + + fin >> v[i]; + + pos = i; + + val = 1; + + update(1, 1, n); + + } + + for (int i = n; i >= 1; i--) + + { + + rez = 0; + + val = v[i]; + + query(1, 1, n); + + sol[rez] = i; + + pos = rez; + + val = 0; + + update(1, 1, n); + + } + + for (int i = 1; i <= n; i++) + + fout << sol[i] << "\n"; + + + + return 0; + +} diff --git a/ski/seg_tree/69efe0d8-137c-11eb-ab63-309c234925a0.cpp b/ski/seg_tree/69efe0d8-137c-11eb-ab63-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf3ef973d2655b9c6ff1330c238d1dd358cec976 --- /dev/null +++ b/ski/seg_tree/69efe0d8-137c-11eb-ab63-309c234925a0.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout("schi.out"); + + + +int v[30005], sol[30005], heap[3*30005]; + +int x, pos; + + + +inline void actualizare(int nod, int st, int dr) + +{ + + int mij; + + if(st == dr) + + heap[nod] = x; + + else + + { + + mij = (st+dr) / 2; + + if(st <= pos && pos <= mij) actualizare(nod*2, st, mij); + + if(mij+1 <= pos && pos <= dr) actualizare(nod*2+1, mij+1, dr); + + heap[nod] = heap[nod*2] + heap[nod*2+1]; + + } + +} + + + +inline int DEI(int nod, int st, int dr, int val) + +{ + + if(st == dr) return st; + + int mij = (st+dr) / 2; + + if(heap[nod*2] < val) return DEI(nod*2 + 1, mij+1, dr, val - heap[nod*2]); + + else return DEI(nod*2, st, mij, val); + +} + + + +int main() + +{ + + int n; + + fin >> n; + + for(int i = 1; i <= n; ++i) fin >> v[i]; + + fin.close(); + + + + x = 1; + + for(int i = 1; i <= n; ++i) + + { + + pos = i; + + actualizare(1, 1, n); + + } + + for(int i = n; i; --i) + + { + + pos = DEI(1, 1, n, v[i]); + + sol[pos] = i; + + x = 0; + + actualizare(1, 1, n); + + } + + + + for(int i = 1; i <= n; ++i) fout << sol[i] << '\n'; + + fout.close(); + + return 0; + +} diff --git a/ski/seg_tree/6a010186-137c-11eb-ace0-309c234925a0.cpp b/ski/seg_tree/6a010186-137c-11eb-ace0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..918b8c72632f946ef51765c6ab0bf8adeb9544cd --- /dev/null +++ b/ski/seg_tree/6a010186-137c-11eb-ace0-309c234925a0.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +ifstream f("schi.in"); +ofstream g("schi.out"); +int answer; +int sol[30005]; +int v[30005]; +int arbint[120050]; +void update(int nod, int st, int dr, int poz, int val) +{ + if (st == dr) + { + arbint[nod] = val; + return; + } + int mid = (st + dr) / 2; + if (poz <= mid) + { + update(2 * nod, st, mid, poz, val); + } + else + { + update(2 * nod + 1, mid + 1, dr, poz, val); + } + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; +} +void find(int nod, int st, int dr, int val) +{ + if (st == dr) + { + answer = st; + return; + } + int mid = (st + dr) / 2; + + if (arbint[2 * nod] < val) + { + find(2 * nod + 1, mid + 1, dr, val - arbint[2 * nod]); + } + else + { + find(2 * nod, st, mid, val); + } +} +int main() +{ + int n; + f >> n; + + + for (int i = 1; i <= n; i++) + { + update(1, 1, n, i, 1); + } + + for (int i = 1; i <= n; i++) + { + f >> v[i]; + } + + for (int i = n; i >= 1; i--) + { + answer = 0; + find(1, 1, n, v[i]); + sol[answer] = i; + update(1, 1, n, answer, 0); + } + for (int i = 1; i <= n; i++) + { + g << sol[i] << '\n'; + } +} diff --git a/ski/seg_tree/6a1f1de4-137c-11eb-9592-309c234925a0.cpp b/ski/seg_tree/6a1f1de4-137c-11eb-9592-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47bd7b2f60ac543fe9d6522bd1ba90cf7a02f755 --- /dev/null +++ b/ski/seg_tree/6a1f1de4-137c-11eb-9592-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + + + + + +int output[30050]; + +int v[30050]; + +int tree[4 * 30050]; + + + +void buildTree(int node, int L, int R) { + + if (L == R) { + + tree[node] = 1; + + } + + else { + + int mid = (L + R) / 2; + + buildTree(node * 2, L, mid); + + buildTree(node * 2 + 1, mid + 1, R); + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + + } + +} + + + +void update(int node, int L, int R, int pos, int val) { + + if (L == R) { + + output[R] = val; + + tree[node] = 0; + + } + + else { + + int mid = (L + R) / 2; + + if (tree[node * 2] >= pos) { + + update(node * 2, L, mid, pos, val); + + } + + else { + + update(node * 2 + 1, mid + 1, R, pos - tree[node * 2], val); + + } + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + + } + +} + + + +FILE * fin, * fout; + + + +int main() { + + fin = fopen("schi.in","r"); + + fout = fopen("schi.out","w"); + + int N; + + fscanf(fin,"%d",&N); + + + + buildTree(1, 0, N-1); + + + + for (int i = 0; i < N; i++) { + + fscanf(fin,"%d",v+i); + + } + + + + for (int i = N - 1; i >= 0; i--) { + + update(1, 0, N - 1, v[i], i + 1); + + } + + + + for (int i = 0; i < N; i++) { + + fprintf(fout,"%d\n",output[i]); + + } + + + + fclose(fin); + + fclose(fout); + + return 0; + + + +} diff --git a/ski/seg_tree/6a3d4be6-137c-11eb-8b54-309c234925a0.cpp b/ski/seg_tree/6a3d4be6-137c-11eb-8b54-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb10e516eb5e25b36ca726a46a84bf6d27a3ff2 --- /dev/null +++ b/ski/seg_tree/6a3d4be6-137c-11eb-8b54-309c234925a0.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/6a4ad1d4-137c-11eb-9766-309c234925a0.cpp b/ski/seg_tree/6a4ad1d4-137c-11eb-9766-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8ed1fc5cd51a3cf2f66c2d4e671394a43e99be4 --- /dev/null +++ b/ski/seg_tree/6a4ad1d4-137c-11eb-9766-309c234925a0.cpp @@ -0,0 +1,72 @@ +#include +#include + + +const int MAX_N = 3e4 + 1; + +int arr[MAX_N], final_order[MAX_N], tree[4 * MAX_N]; + +void build(int node, int left, int right) +{ + if (left == right) { + tree[node] = 1; + return; + } + + int mid = (left + right) >> 1; + + build(node * 2, left, mid); + + build(node * 2 + 1, mid + 1, right); + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; +} + +void update(int node, int left, int right, int position, int value_schior) +{ + if (left == right) { + tree[node] = 0; + final_order[left] = value_schior; + return; + } + + int mid = (left + right) / 2; + + if (tree[node * 2] >= position) { + update(node * 2 , left, mid, position, value_schior); + } + else { + update(node * 2 + 1, mid + 1, right, position - tree[node * 2], value_schior); + } + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; +} + + + + + +int main() +{ + std::ifstream fin("schi.in"); + std::ofstream fout("schi.out"); + + int n; + fin >> n; + + for (int i = 0; i < n; ++i) { + fin >> arr[i]; + } + + build(1, 0, n - 1); + + for (int i = n - 1; i >= 0; i--) { + update(1, 0, n - 1, arr[i], i + 1); + } + + for (int i = 0; i < n; i++) { + fout << final_order[i] << '\n'; + } + + return 0; +} diff --git a/ski/seg_tree/6a59b734-137c-11eb-8af6-309c234925a0.cpp b/ski/seg_tree/6a59b734-137c-11eb-8af6-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7a7a2e98f0860a9cc608aab27c7633cfb6f7886 --- /dev/null +++ b/ski/seg_tree/6a59b734-137c-11eb-8af6-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include + + +std::ifstream in ("schi.in"); + +std::ofstream out ("schi.out"); + + + +const int MAXN = 30000; + +int v[MAXN + 1], aint[4*MAXN], rez[MAXN+1]; + + + +int query (int p, int st, int dr, int val){ + + if(st == dr) + + return st; + + else{ + + int m = (st + dr) / 2; + + if(aint[2*p] < val) + + return query(2*p + 1, m + 1, dr, val - aint[2*p]); + + else + + return query(2*p, st, m, val); + + } + +} + +void update (int p, int st, int dr, int poz){ + + if(st == dr){ + + aint[p] = 0; + + } + + else{ + + int m = (st + dr) / 2; + + if(poz <= m) + + update(2*p, st, m, poz); + + else + + update(2*p + 1, m + 1, dr, poz); + + aint[p] = aint[2*p] + aint[2*p + 1]; + + } + +} + +void build(int p, int st, int dr) + +{ + + if (st == dr) + + aint[p] = 1; + + else{ + + int m = (st + dr) / 2; + + build(2*p, st, m); + + build(2*p + 1, m + 1, dr); + + aint[p] = aint[2*p] + aint[2*p + 1]; + + } + +} + + + + + +int main() + +{ + + int N, loc; + + in >> N; + + for (int i = 1; i <= N; ++i) + + in >> v[i]; + + build(1, 1, N); + + + + for (int i = N; i > 0; --i){ + + loc = query(1, 1, N, v[i]); + + rez[loc] = i; + + update(1, 1, N, loc); + + } + + for (int i = 1; i <= N; ++i) + + out << rez[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/6a684578-137c-11eb-927a-309c234925a0.cpp b/ski/seg_tree/6a684578-137c-11eb-927a-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1124935bdf35fea2eda6108a26c319b15951842c --- /dev/null +++ b/ski/seg_tree/6a684578-137c-11eb-927a-309c234925a0.cpp @@ -0,0 +1,62 @@ +#include +std::ifstream fin("schi.in"); +std::ofstream fout("schi.out"); + +const int N = 30001; +int n, tree[4 * N], v[N], res[N]; + + + +void build(int node, int left, int right) +{ + if(left == right) + { + tree[node] = 1; + return; + } + int mid = (left + right) / 2; + build(node * 2, left, mid); + build(node * 2 + 1, mid + 1, right); + tree[node] = tree[node * 2] + tree[node * 2 + 1]; +} + + +void update(int node, int left, int right, int index, int val) +{ + if(left == right) + { + tree[node] = 0; + res[right] = val; + return; + } + int mid = (left + right) / 2; + + if(tree[node * 2] >= index) + update(node * 2 , left, mid, index, val); + else + update(node * 2 + 1, mid + 1, right, index - tree[node * 2], val); + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + +} + + +int main() +{ + int i; + fin >> n; + for(i = 0; i < n; i++) + fin >> v[i]; + build(1, 0, n - 1); + + for(i = n - 1; i >= 0; i--) + update(1, 0, n - 1, v[i], i + 1); + + for(i = 0; i < n; i++) + fout << res[i] << '\n'; + + + + + return 0; +} diff --git a/ski/seg_tree/6a7a6d30-137c-11eb-886e-309c234925a0.cpp b/ski/seg_tree/6a7a6d30-137c-11eb-886e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3fc48727fcb5e5b56c6e6f7a51edc195c60d265 --- /dev/null +++ b/ski/seg_tree/6a7a6d30-137c-11eb-886e-309c234925a0.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; +ifstream f("schi.in"); +ofstream g("schi.out"); +int arbore[3000001], v[300001], solutie[30010]; +int val, poz, n, raspuns; + +void actualizare(int nod, int st, int dr) +{ + if (st == dr) + { + arbore[nod] = val; + return; + } + int mij = (st + dr) / 2; + if(poz <= mij) + actualizare(2 * nod, st, mij); + else + actualizare(2 * nod + 1, mij + 1, dr); + arbore[nod] = arbore[2 * nod] + arbore[2 * nod + 1]; +} + +void interogare(int nod, int st, int dr) +{ + if (st == dr) + { + raspuns = st; + return; + } + int mij = (st + dr)/2; + if (arbore[2 * nod] < val) + { val = val - arbore[2 * nod]; + interogare(2 * nod + 1, mij + 1, dr); + + } + else + interogare(2 * nod, st, mij); +} +int main() { + f >> n; + for(int i = 1; i <= n; i++) + { + f >> v[i]; + poz = i; + val = 1; + actualizare(1, 1, n); + } + int i = n; + while(i >= 1) + { + raspuns = 0; + val = v[i]; + interogare(1, 1, n); + solutie[raspuns] = i; + poz = raspuns; + val = 0; + actualizare(1,1,n); + i--; + } + for (int i = 1; i <=n ;i++) + g << solutie[i] << "\n"; + return 0; +} diff --git a/ski/seg_tree/6a8ad930-137c-11eb-bf0e-309c234925a0.cpp b/ski/seg_tree/6a8ad930-137c-11eb-bf0e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a65ee0b5ec5f54686dc55b90745a1d4d136eea4f --- /dev/null +++ b/ski/seg_tree/6a8ad930-137c-11eb-bf0e-309c234925a0.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +typedef long long ll; +typedef pair PII; +typedef vector VI; +typedef vector VPII; +const int INF=0x3f3f3f3f; + +int n,poz; +int v[30005],aint[4*30005],sol[30005]; + +void generator(int node,int val){ + aint[node]=val; + if(val==1) + return; + generator(node*2,val/2); + if(val&1) + generator(node*2+1,val/2+1); + else + generator(node*2+1,val/2); +} + +int solver(int node,int st, int dr,int val){ + aint[node]--; + if(st==dr) + return st; + int middle = (st+dr-1)/2; + if(aint[node*2]>=val) + return solver(node*2,st,middle,val); + else{ + val -= aint[node*2]; + return solver(node*2+1,middle+1,dr,val); + } +} + +int main() +{ + freopen("schi.in","r",stdin); + freopen("schi.out","w",stdout); + cin>>n; + for(int i=(1);i<=(n);++i) cin>>v[i]; + generator(1,n); + for(int i=n;i>0;--i){ + poz = solver(1,1,n,v[i]); + sol[poz]=i; + } + for(int i=(1);i<=(n);++i) + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +struct arbore + +{ + + int st, dr, val; + +}; + + + +int n, i, poz[30005], clasament[30005]; + +arbore a[120005]; + + + +void construieste_arbore(const int& p, const int& q, const int& pz) + +{ + + int mij; + + + + if(p == q) + + { + + a[pz].st = p; + + a[pz].dr = q; + + a[pz].val = 1; + + } + + else + + if(p < q) + + { + + mij = (p + q) / 2; + + construieste_arbore(p, mij, pz * 2 + 1); + + construieste_arbore(mij + 1, q, pz * 2 + 2); + + a[pz].st = p; + + a[pz].dr = q; + + a[pz].val = a[pz * 2 + 1].val + a[pz * 2 + 2].val; + + } + + else + + { + + a[pz].st = a[pz].dr = -1; + + a[pz].val = 0; + + } + +} + + + +int gaseste_pozitia(const int& p, const int& rad) + +{ + + a[rad].val--; + + if(a[rad].st == a[rad].dr) + + return a[rad].st; + + else + + if(a[rad * 2 + 1].val >= p) + + return gaseste_pozitia(p, rad * 2 + 1); + + else + + return gaseste_pozitia(p - a[rad * 2 + 1].val, rad * 2 + 2); + +} + + + +int main() + +{ + + f >> n; + + for(i = 0; i < n; i++) + + f >> poz[i]; + + f.close(); + + + + construieste_arbore(1, n, 0); + + for(i = n - 1; i >= 0; i--) + + clasament[gaseste_pozitia(poz[i], 0)] = i + 1; + + + + for(i = 1; i <= n; i++) + + g << clasament[i] << '\n'; + + g.close(); + + + + return 0; + +} diff --git a/ski/seg_tree/6b05a452-137c-11eb-9edc-309c234925a0.cpp b/ski/seg_tree/6b05a452-137c-11eb-9edc-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b94069420243942ae8b4e0e15147370a3d289940 --- /dev/null +++ b/ski/seg_tree/6b05a452-137c-11eb-9edc-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +const int MAX = 30001; + + + +int t[3 * MAX], a[MAX], b[MAX]; + + + +void create(int p, int st, int dr) + +{ + + if(st == dr) + + { + + t[p] = 1; + + } + + else + + { + + int m = (st + dr) / 2; + + create(2 * p, st, m); + + create(2 * p + 1, m + 1, dr); + + t[p] = t[2 * p] + t[2 * p + 1]; + + } + +} + + + +void actualizare(int p, int st, int dr, int c, int poz) + +{ + + if(st == dr) + + { + + t[p] = 0; + + a[st] = c; + + } + + else + + { + + int m = (st + dr) / 2; + + if(t[2 * p] >= poz) + + { + + actualizare(2 * p, st, m, c, poz); + + } + + else + + { + + actualizare(2 * p + 1, m + 1, dr, c, poz - t[2 * p]); + + } + + t[p] = t[2 * p] + t[2 * p + 1]; + + } + +} + + + +int main() + +{ + + int n; + + in >> n; + + for(int i = 1; i <= n; i++) + + { + + in >> b[i]; + + } + + create(1, 1, n); + + for(int i = n; i >= 1; i--) + + { + + actualizare(1, 1, n, i, b[i]); + + } + + for(int i = 1; i <= n; i++) + + { + + out << a[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/seg_tree/6b16069e-137c-11eb-a9fe-309c234925a0.cpp b/ski/seg_tree/6b16069e-137c-11eb-a9fe-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81e244f641b31d5642955acd9e7664f733916c59 --- /dev/null +++ b/ski/seg_tree/6b16069e-137c-11eb-a9fe-309c234925a0.cpp @@ -0,0 +1,112 @@ +#include + + + + +std::ifstream fin ("schi.in"); + +std::ofstream fout ("schi.out"); + + + +int tree[70000]; + +int x[70000], ans[70000]; + + + +void buildTree (int left, int right, int node){ + + if (left == right){ + + tree[node] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree (left, mid, 2*node+1); + + buildTree (mid+1, right, 2*node+2); + + + + tree[node] = tree[2*node+1] + tree[2*node+2]; + +} + + + +int findTree (int left, int right, int node, int val){ + + if (left == right){ + + tree[node] = 0; + + return left; + + } + + + + int mid = (left + right) / 2, ans; + + if (tree[2*node+1] >= val) ans = findTree (left, mid, 2*node+1, val); + + else ans = findTree (mid+1, right, 2*node+2, val - tree[2*node+1]); + + + + tree[node] = tree[2*node+1] + tree[2*node+2]; + + + + return ans; + + + +} + + + +int main () + +{ + + int n, i, pos; + + fin >> n; + + for (i=0; i> x[i]; + + + + buildTree (0, n-1, 0); + + + + for (i=n-1; i>=0; i--){ + + pos = findTree (0, n-1, 0, x[i]); + + ans[pos] = i + 1; + + } + + + + + + for (i=0; i +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int NMAX = 30020; + +int tree[4 * NMAX], rez[NMAX], v[NMAX]; + + + +void buildArbInt(int node, int st, int dr) { + + if (st == dr) { + + tree[node] = 1; + + } + + else { + + int mid = (st + dr) / 2; + + buildArbInt(node * 2, st, mid); + + buildArbInt(node * 2 + 1, mid + 1, dr); + + + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + + } + +} + + + +void update(int node, int st, int dr, int pos, int val) { + + if (st == dr) { + + rez[st] = val; + + tree[node] = 0; + + } + + else { + + int mid = (st + dr) / 2; + + if (tree[node * 2] >= pos) { + + update(node * 2, st, mid, pos, val); + + } + + else { + + update(node * 2 + 1, mid + 1, dr, pos - tree[node * 2], val); + + } + + tree[node] = tree[node * 2] + tree[node * 2 + 1]; + + } + +} + + + +int main() { + + int n; + + in >> n; + + buildArbInt(1, 0, n-1); + + for (int i = 0; i < n; i++) { + + in >> v[i]; + + } + + for (int i = n - 1; i >= 0; i--) { + + update(1, 0, n - 1, v[i], i + 1); + + } + + for (int i = 0; i < n; i++) { + + out << rez[i] << '\n'; + + } + + return 0; + +} diff --git a/ski/seg_tree/6b53e638-137c-11eb-b159-309c234925a0.cpp b/ski/seg_tree/6b53e638-137c-11eb-b159-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c49b41fb7375e27c7e5dfe00b2feb99966f8b9dc --- /dev/null +++ b/ski/seg_tree/6b53e638-137c-11eb-b159-309c234925a0.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int N = 1 << 17, M = 30001; + + + +int t[N], concurent, poz, rez[M], v[M]; + + + +void update(int p, int st, int dr) + +{ + + if(st == dr) + + { + + rez[st] = concurent; + + t[p]++; + + return; + + } + + int m = (st+dr) / 2; + + int libere = m - st + 1 - t[2*p]; + + if(poz <= libere) + + update(2*p, st, m); + + else + + { + + poz -= libere; + + update(2*p+1, m + 1, dr); + + } + + t[p] = t[2*p] + t[2*p + 1]; + +} + + + +int main() + +{ + + int n; + + in >> n; + + for(int i = 1; i <= n; i++) + + { + + in >> v[i]; + + } + + for(int i = n; i >= 1; i--) + + { + + concurent = i; + + poz = v[i]; + + update(1, 1, n); + + } + + for(int i = 1; i <= n; i++) + + { + + out << rez[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/seg_tree/6b62a69c-137c-11eb-a675-309c234925a0.cpp b/ski/seg_tree/6b62a69c-137c-11eb-a675-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..239cfe9aebf8b329c60f25948e1313af16a3c7a2 --- /dev/null +++ b/ski/seg_tree/6b62a69c-137c-11eb-a675-309c234925a0.cpp @@ -0,0 +1,91 @@ +#include +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int N = 1 << 17, Q = 30005; + +int t[N], v[Q], sol[Q], poz, val; + + + +void actualizare(int p, int st, int dr) + +{ + + if(st==dr){ + + sol[st] = val; + + t[p]++; + + return; + + } + + int m=(st + dr)/2; + + if(poz <= m - st + 1 - t[2 * p]) + + actualizare(2 * p, st, m); + + else{ + + poz -= (m - st + 1 - t[2 * p]); + + actualizare(2 * p + 1, m + 1, dr); + + } + + t[p]++; + +} + + + +int main() + +{ + + int n, q; + + in >> n; + + for(int i = 1; i <= n; i++) + + in >> v[i]; + + t[1] = 1; + + for(int i = n; i >= 1; i--){ + + val = i; + + poz = v[i]; + + actualizare(1, 1, n); + + } + + for(int i = 1; i <= n; i++){ + + out << sol[i] << "\n"; + + } + + + + return 0; + + + +} diff --git a/ski/seg_tree/6b75eea8-137c-11eb-ab03-309c234925a0.cpp b/ski/seg_tree/6b75eea8-137c-11eb-ab03-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69a7ee4f56c4e4c2b23fdc8cfc41ba118769d9da --- /dev/null +++ b/ski/seg_tree/6b75eea8-137c-11eb-ab03-309c234925a0.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int N=1 << 17, Q=30001; + + + +int t[N], v[Q], sol[Q]; + +int poz, val; + + + +void actualizare(int p, int st, int dr) + +{ + + if(st==dr) + + { + + sol[st]=val; + + t[p]++; + + return; + + } + + int m=(st+dr)/2; + + if(poz<=m-st+1-t[2*p]) + + actualizare(2*p, st, m); + + else + + { + + poz-=(m-st+1-t[2*p]); + + actualizare(2*p+1, m+1, dr); + + } + + t[p]++; + +} + + + +int main() + +{ + + int n, q; + + in>>n; + + for(int i=1; i<=n; i++) + + in>>v[i]; + + t[1]=1; + + for(int i=n; i>=1; i--) + + { + + val=i; + + poz=v[i]; + + actualizare(1,1,n); + + } + + for(int i=1; i<=n; i++) + + out< +#include + + + + + + +int v[30000 +1],aint[30000*4+1],v2[30000 +1]; + + + +void update(int nod,int st,int dr,int poz,int val){ + + if(st==dr){ + + aint[nod]=val; + + } + + else{ + + int mij=(st+dr)/2; + + if(poz<=mij){ + + update(2*nod,st,mij,poz,val); + + } + + else if(poz>mij){ + + update(2*nod+1,mij+1,dr,poz,val); + + } + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + + } + +} + + + +int query(int nod,int st,int dr,int poz){ + + if(st==dr){ + + return st; + + } + + else{ + + int mij=(st+dr)/2; + + if(poz<=aint[2*nod]){ + + return query(2*nod,st,mij,poz); + + } + + else if(poz>aint[2*nod]){ + + return query(2*nod+1,mij+1,dr,poz-aint[2*nod]); + + } + + } + +} + + + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("schi.in","r"); + + fout=fopen("schi.out","w"); + + + + int n,i,pozint; + + fscanf(fin,"%d",&n); + + for(i=1;i<=n;i++){ + + fscanf(fin,"%d",&v[i]); + + update(1,1,n,i,1); + + } + + for(i=n;i>=1;i--){ + + pozint=query(1,1,n,v[i]); + + update(1,1,n,pozint,0); + + v2[pozint]=i; + + } + + for(i=1;i<=n;i++){ + + fprintf(fout,"%d\n",v2[i]); + + } + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/ski/seg_tree/6c158bee-137c-11eb-8bd4-309c234925a0.cpp b/ski/seg_tree/6c158bee-137c-11eb-8bd4-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..489025f1c39a8ae339d5c8c30d8a9a0f722624a6 --- /dev/null +++ b/ski/seg_tree/6c158bee-137c-11eb-8bd4-309c234925a0.cpp @@ -0,0 +1,156 @@ +#include +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int N=30001; + +int n,v[N], sol[N],arb[3*N],poz,ans; + + + +void interogare(int p, int st, int dr) + +{ + + if(st==dr) + + { + + ans=st; + + return; + + } + + int m=(st+dr)/2; + + if(poz<=arb[2*p]) + + interogare(2*p,st,m); + + else + + { + + poz-=arb[2*p]; + + interogare(2*p+1,m+1,dr); + + } + + + + } + + + +void actualizare(int p, int st, int dr) + +{ + + if(st==dr) + + { + + arb[p]=0; + + return; + + } + + + + int m=(st+dr)/2; + + + + if(poz<=m) + + actualizare(2*p,st,m); + + else + + actualizare(2*p+1,m+1,dr); + + + + arb[p]=arb[2*p]+arb[2*p+1]; + +} + + + +void constr(int p, int st, int dr) + +{ + + if(st==dr) + + { + + arb[p]=1; + + return; + + } + + else + + { + + int m=(st+dr)/2; + + constr(2*p,st,m); + + constr(2*p+1,m+1,dr); + + arb[p]=arb[2*p]+arb[2*p+1]; + + } + +} + + + +int main() + +{ + + in>>n; + + for(int i=1;i<=n;i++) + + in>>v[i]; + + constr(1,1,n); + + + + for(int i=n;i>=1;i--) + + { + + poz=v[i]; + + interogare(1,1,n); + + sol[ans]=i; + + poz=ans; + + actualizare(1,1,n); + + } + + + + for(int i=1;i<=n;i++) + + out< +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int N=30001; + +int n,v[N], sol[N],arb[3*N],poz,ans; + + + + void query(int p, int st, int dr) + + { + + if(st==dr) + + { + + ans=st; + + return; + + } + + int m=(st+dr)/2; + + if(poz<=arb[2*p])query(2*p,st,m); + + else + + { + + poz-=arb[2*p]; + + query(2*p+1,m+1,dr); + + } + + + + } + + + +void update(int p, int st, int dr) + +{ + + if(st==dr) + + { + + arb[p]=0; + + return; + + } + + + + int m=(st+dr)/2; + + if(poz<=m) update(2*p,st,m); + + else update(2*p+1,m+1,dr); + + arb[p]=arb[2*p]+arb[2*p+1]; + + } + + + +void construieste_arb(int p, int st, int dr) + +{ + + if(st==dr) + + { + + arb[p]=1; + + return; + + } + + else + + { + + int m=(st+dr)/2; + + construieste_arb(2*p,st,m); + + construieste_arb(2*p+1,m+1,dr); + + arb[p]=arb[2*p]+arb[2*p+1]; + + } + +} + +int main() + +{ + + in>>n; + + for(int i=1;i<=n;i++) in>>v[i]; + + construieste_arb(1,1,n); + + + + for(int i=n;i>=1;i--) + + { + + poz=v[i]; + + query(1,1,n); + + sol[ans]=i; + + poz=ans; + + update(1,1,n); + + } + + + + for(int i=1;i<=n;i++) out< +#include +#include + +template +class AINT { + std::array t; + +private: + void _update(int p, int st, int dr, int poz) { + if (st == dr) { + t[p] = !t[p]; + return; + } + + int m = (st + dr) / 2; + if (poz <= m) { + _update(2 * p, st, m, poz); + } else { + _update(2 * p + 1, m + 1, dr, poz); + } + t[p] = t[2 * p] + t[2 * p + 1]; + } + + int _query(int p, int st, int dr, int x) { + if (st == dr) { + return st; + } + + int m = (st + dr) / 2; + if (t[2 * p] >= x) { + return _query(2 * p, st, m, x); + } else { + return _query(2 * p + 1, m + 1, dr, x - t[2 * p]); + } + } + +public: + inline void update(int n, int poz) { + _update(1, 1, n, poz); + } + + inline int query(int n, int x) { + return _query(1, 1, n, x); + } +}; + +AINT<30000> aint; +std::array input; +std::array result; + +int main() { + std::ifstream fin("schi.in"); + std::ofstream fout("schi.out"); + int n; + + fin >> n; + for (int i = 1; i <= n; i++) { + fin >> input[i]; + aint.update(n, i); + } + + for (int i = n; i >= 1; i--) { + int poz = aint.query(n, input[i]); + result[poz] = i; + aint.update(n, poz); + } + + for (int i = 1; i <= n; i++) { + fout << result[i] << '\n'; + } + return 0; +} diff --git a/ski/seg_tree/6c5580a4-137c-11eb-9c65-309c234925a0.cpp b/ski/seg_tree/6c5580a4-137c-11eb-9c65-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..18376b6ef5506cecc9c0adddb1db777a2512a06b --- /dev/null +++ b/ski/seg_tree/6c5580a4-137c-11eb-9c65-309c234925a0.cpp @@ -0,0 +1,96 @@ +#include + + + + + + +using namespace std; + + + +ifstream f ( "schi.in" ); + +ofstream g ( "schi.out" ); + + + +int ait[3 * 30001], sol[30001], v[30001]; + + + +void update ( int node, int st, int dr ){ + + if ( st == dr ){ + + ait[node] = 1; + + return; + + } + + int mid = ( st + dr ) >> 1; + + update ( 2 * node, st, mid ); + + update ( 2 * node + 1, mid + 1, dr ); + + ait[node] = ait[2 * node] + ait[ 2 * node + 1]; + +} + + + +int quary ( int node, int st, int dr, int k ){ + + ait[node]--; + + if ( st == dr ) + + return st; + + int mid = ( st + dr ) >> 1; + + if ( ait[2 * node] < k ){ + + k -= ait[2 * node]; + + return quary ( 2 * node + 1, mid + 1, dr, k ); + + } + + else + + return quary ( 2 * node, st, mid, k ); + +} + + + +int main() + +{ int n, i; + + f >> n; + + update ( 1, 1, n ); + + for ( i = 1; i <= n; i++ ) + + f >> v[i]; + + for ( i = n; i >= 1; i-- ){ + + int poz = quary ( 1, 1, n, v[i] ); + + sol[poz] = i; + + } + + for ( i = 1; i <= n; i++ ) + + g << sol[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/71c9a9ec-137c-11eb-8653-309c234925a0.cpp b/ski/seg_tree/71c9a9ec-137c-11eb-8653-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47bbc929567fe5d3d46be0fe09cd1496776951e8 --- /dev/null +++ b/ski/seg_tree/71c9a9ec-137c-11eb-8653-309c234925a0.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +const int N=1<<17; + +const int Nmax=30000; + + + +int t[N],v[Nmax],sol[Nmax],nod,poz; + + + +void actualizare (int p, int st, int dr) + +{ + + if (st==dr) + + { + + sol[st]=nod; + + t[p]++; + + return; + + } + + int m=(st+dr)/2; + + if (poz<=(m-st+1-t[2*p])) + + { + + actualizare (p*2,st,m); + + } + + else + + { + + poz-=(m-st+1-t[p*2]); + + actualizare (p*2+1,m+1,dr); + + } + + t[p]++; + +} + +int main() + +{ + + int n; + + in>>n; + + t[1]=1; + + for (int i=1;i<=n;i++) + + in>>v[i]; + + + + for (int i=n;i>0;i--) + + { + + nod=i; + + poz=v[i]; + + actualizare (1,1,n); + + } + + for (int i=1;i<=n;i++) + + out< +#include +#include + +template +using UniteFN = T(T, T); + +template +class AINT { + std::array t; + FN& unite_fn; + +private: + void _update(int p, int st, int dr, int poz) { + if (st == dr) { + t[p] = !t[p]; + return; + } + + int m = (st + dr) / 2; + if (poz <= m) { + _update(2 * p, st, m, poz); + } else { + _update(2 * p + 1, m + 1, dr, poz); + } + t[p] = unite_fn(t[2 * p], t[2 * p + 1]); + } + + int _query(int p, int st, int dr, T x) { + if (st == dr) { + return st; + } + + int m = (st + dr) / 2; + if (t[2 * p] >= x) { + return _query(2 * p, st, m, x); + } else { + return _query(2 * p + 1, m + 1, dr, x - t[2 * p]); + } + } + +public: + AINT(FN& unite_fn) : unite_fn(unite_fn) {} + + inline void update(int n, int poz) { + _update(1, 1, n, poz); + } + + inline int query(int n, int x) { + return _query(1, 1, n, x); + } +}; + + + + + + + +class schi_uniter { +public: + int operator()(int a, int b) { + return a + b; + } +}; + +auto schi_unite = [](int a, int b) -> int { + return a + b; +}; +AINT aint(schi_unite); +std::array input; +std::array result; + +int main() { + std::ifstream fin("schi.in"); + std::ofstream fout("schi.out"); + int n; + + fin >> n; + for (int i = 1; i <= n; i++) { + fin >> input[i]; + aint.update(n, i); + } + + for (int i = n; i >= 1; i--) { + int poz = aint.query(n, input[i]); + result[poz] = i; + aint.update(n, poz); + } + + for (int i = 1; i <= n; i++) { + fout << result[i] << '\n'; + } + return 0; +} diff --git a/ski/seg_tree/71e83676-137c-11eb-ab83-309c234925a0.cpp b/ski/seg_tree/71e83676-137c-11eb-ab83-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11ac7c39334b519497558425655a2684c91a0af0 --- /dev/null +++ b/ski/seg_tree/71e83676-137c-11eb-ab83-309c234925a0.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int N = 30000; + +int arb[4*N]; +int ord[N + 1], available[N + 1], sol[N + 1]; + +void uniteNodes(int parent, int leftChild, int rightChild){ + arb[parent] = arb[leftChild] + arb[rightChild]; +} + +void build(int node, int left, int right){ + if(left == right){ + arb[node] = available[left] = 1; + return; + } + int mij = (left + right)/2; + build(2*node, left, mij); + build(2*node + 1, mij + 1, right); + uniteNodes(node, 2*node, 2*node + 1); +} + +void update(int node, int left, int right, int index, int val){ + if(left == right){ + sol[left] = val; + available[left] = 0; + arb[node] = 0; + return; + } + int mij = (left + right)/2; + if(index <= arb[2*node]) + update(2*node, left, mij, index, val); + else + update(2*node + 1, mij + 1, right, index - arb[2*node], val); + uniteNodes(node, 2*node, 2*node + 1); +} + +int main() +{ + int n,i; + fin >> n; + for(i=1; i<=n; i++) + fin >> ord[i]; + build(1, 1, n); + for(i=n; i>=1; i--) + update(1, 1, n, ord[i], i); + for(i=1; i<=n; i++) + fout << sol[i] << "\n"; + return 0; +} diff --git a/ski/seg_tree/71f81b6e-137c-11eb-8187-309c234925a0.cpp b/ski/seg_tree/71f81b6e-137c-11eb-8187-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6b965f1f8a6838d8a45662ee4981abd7689a4c1 --- /dev/null +++ b/ski/seg_tree/71f81b6e-137c-11eb-8187-309c234925a0.cpp @@ -0,0 +1,131 @@ +#include +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int sol[30001],aint[90000],v[30001]; + +int rez,poz,n; + +void query(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + rez=st; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=aint[2*nod]) + + query(2*nod,st,mid); + + else + + { + + poz-=aint[2*nod]; + + query(2*nod+1,mid+1,dr); + + } + +} + +void update(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=0; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=mid) + + update(2*nod,st,mid); + + else + + update(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +void build(int nod,int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=1; + + return ; + + } + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +int main () + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for(int i=n;i>=1;i--) + + { + + poz=v[i]; + + query(1,1,n); + + sol[rez]=i; + + poz=rez; + + update(1,1,n); + + } + + for(int i=1;i<=n;i++) + + fout< + + +using namespace std; + + + +ifstream fin ("schi.in"); + +ofstream fout ("schi.out"); + + + +const int nmax = 3e4 + 5; + + + +int arb[3*nmax]; + + + +int n, a[nmax], sol[nmax]; + + + +void build (int ls = 1, int ld = n, int pos = 1) + +{ + + if (ls == ld) + + { + + arb[pos] = 1; + + return; + + } + + + + int mij = (ls + ld) / 2; + + build(ls, mij, 2*pos); + + build(mij+1, ld, 2*pos+1); + + + + arb[pos] = arb[2*pos] + arb[2*pos+1]; + +} + + + +void query (int ls, int ld, int pos, int val, int indice) + +{ + + if (ls == ld) + + { + + sol[ls] = indice; + + arb[pos] = 0; + + return; + + } + + + + int mij = (ls + ld) / 2; + + if (arb[2*pos] >= val) query(ls, mij, 2*pos, val, indice); + + else query(mij+1, ld, 2*pos+1, val - arb[2*pos], indice); + + + + arb[pos] = arb[2*pos] + arb[2*pos+1]; + +} + + + +int main() + +{ + + fin >> n; + + for (int i=1; i<=n; ++i) + + fin >> a[i]; + + + + build(); + + + + for (int i=n; i>=1; --i) + + { + + query(1,n,1,a[i],i); + + } + + + + for (int i=1; i<=n; ++i) + + fout << sol[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/72531fec-137c-11eb-a34f-309c234925a0.cpp b/ski/seg_tree/72531fec-137c-11eb-a34f-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c753241de3325f9e9b6efaea45b42fc42a16bf3 --- /dev/null +++ b/ski/seg_tree/72531fec-137c-11eb-a34f-309c234925a0.cpp @@ -0,0 +1,129 @@ +#include + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 3e4+10; + + + +int V[DMAX]; + +int ans[DMAX]; + +int tree[4*DMAX]; + + + +int n; + + + +void build(int node,int st,int dr); + +void query(int node,int st,int dr,int val,int x); + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>V[i]; + + build(1,1,n); + + for(i=n;i>=1;i--) + + query(1,1,n,V[i],i); + + + + for(i=1;i<=n;i++) + + fout< + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i,v[30010],sol[30010],A[3*30010]; + +void build(int nod,int st,int dr) { + + if (st==dr) + + A[nod]=1; + + else { + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +void update(int nod,int st,int dr,int x,int poz) { + + if (st==dr) { + + A[nod]=0; + + sol[st]=x; + + } + + else { + + int mid=(st+dr)/2; + + if (A[2*nod]>=poz) + + update(2*nod,st,mid,x,poz); + + else + + update(2*nod+1,mid+1,dr,x,poz-A[2*nod]); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for (i=n;i>=1;i--) + + update(1,1,n,i,v[i]); + + for (i=1;i<=n;i++) + + fout< + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int a[30004],v[4*30004],sum,n,i,sol[30004]; + +void build(int nod,int st,int dr){ + + if(st==dr){ + + v[nod]=1; + + return; + + } + + int mid=(st+dr)/2; + + build(nod*2,st,mid); + + build(nod*2+1,mid+1,dr); + + v[nod]=v[nod*2]+v[nod*2+1]; + +} + +int query(int nod,int st,int dr){ + + + + if(st==dr){ + + v[nod]=0; + + return st; + + } + + int mid=(st+dr)/2; + + v[nod]--; + + if(v[2*nod]>=sum) + + return query(2*nod,st,mid); + + sum-=v[2*nod]; + + return query(2*nod+1,mid+1,dr); + +} + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>a[i]; + + build(1,1,n); + + for(i=n;i>=1;i--){ + + sum=a[i]; + + int poz= query(1,1,n); + + sol[poz]=i; + + } + + for(i=1;i<=n;i++) + + fout< +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int A[90000],n,x[30000],sol[30000]; + +void creare(int st,int dr,int nod){ + + if(st==dr){ + + A[nod]=1; + + + + } + + else{ + + int mid=(st+dr)/2; + + creare(st,mid,2*nod); + + creare(mid+1,dr,2*nod+1); + + A[nod]=A[2*nod]+A[2*nod+1]; + + + + + + } + + + + + +} + + + +void update(int st, int dr, int nod,int poz,int k){ + + if(st==dr){ + + sol[st]=k; + + A[nod]=0; + + } + + else{ + + int mid=(st + dr)/2; + + if(A[2*nod]>=poz){ + + update(st,mid,2*nod,poz,k); + + } + + else{ + + update(mid+1,dr,2*nod+1,poz-A[2*nod],k); + + } + + A[nod]=(A[2*nod]+A[2*nod+1]); + + } + +} + + + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>x[i]; + + } + + + + creare(1,n,1); + + + + for(int i=n;i>=1;i--){ + + update(1,n,1,x[i],i); + + } + + for(int i=1;i<=n;i++){ + + fout< +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, i, a[100010], v[30010], sol[30010]; + + + +void initializeaza(int st, int dr, int nod){ + + if(st==dr){ + + a[nod]=1; + + }else{ + + int mid=(st+dr)/2; + + initializeaza(st, mid, nod*2); + + initializeaza(mid+1, dr, nod*2+1); + + a[nod]=a[nod*2]+a[nod*2+1]; + + } + +} + + + +void gaseste(int st, int dr, int nod, int pozCautata){ + + if(st==dr){ + + a[nod]=0; + + sol[st]=i; + + }else{ + + int mid=(st+dr)/2; + + if(a[2*nod]>=pozCautata){ + + + + gaseste(st, mid, 2*nod, pozCautata); + + }else{ + + + + + + gaseste(mid+1, dr, 2*nod+1, pozCautata-a[2*nod]); + + } + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + + + +int main(){ + + fin>>n; + + for(i=1; i<=n; i++){ + + fin>>v[i]; + + } + + initializeaza(1, n, 1); + + for(i=n; i>0; i--){ + + gaseste(1, n, 1, v[i]); + + } + + for(i=1; i<=n; i++){ + + fout< +#include +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int a[120001],v[30001],sol[30001]; + +int n,i,j,poz; + + + +void build(int nod, int st, int dr){ + + if(st==dr){ + + a[nod]=1; + + return; + + } + + + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + a[nod]=a[2*nod]+a[2*nod+1]; + +} + + + +void update(int nod, int st, int dr, int p){ + + if(st==dr){ + + a[nod]--; + + return; + + } + + + + int mid=(st+dr)/2; + + + + if(p<=mid) update(2*nod,st,mid,p); + + else update(2*nod+1,mid+1,dr,p); + + a[nod]=a[2*nod]+a[2*nod+1]; + +} + + + +int query(int nod, int st, int dr, int x){ + + if(st==dr) + + return st; + + + + int mid=(st+dr)/2; + + + + if(a[2*nod]>=x) + + return query(2*nod,st,mid,x); + + else + + return query(2*nod+1,mid+1,dr,x-a[2*nod]); + +} + + + +int main(){ + + fin>>n; + + build(1,1,n); + for(i=1;i<=n;i++) + + fin>>v[i]; + + + + for(i=n;i;i--){ + + poz=query(1,1,n,v[i]); + + update(1,1,n,poz); + + sol[poz]=i; + + + + } + + + + for(i=1;i<=n;i++) + + fout< +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i; + +int v[30010],loc[30010],a[90010]; + + + +void build(int st, int dr, int nod){ + + if(st==dr) + + a[nod]=1; + + else{ + + int mid=(st+dr)/2; + + build(st,mid,2*nod); + + build(mid+1,dr,2*nod+1); + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + + + +void clasament(int st, int dr, int nod,int poz2){ + + if(st==dr){ + + a[nod]=0; + + loc[st]=i; + + } + + else{ + + int mid=(st+dr)/2; + + if(a[2*nod]>=poz2){ + + clasament(st,mid,2*nod,poz2); + + } + + else{ + + clasament(mid+1,dr,2*nod+1,poz2-a[2*nod]); + + } + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + + + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>v[i]; + + } + + build(1,n,1); + + for(i=n;i>=1;i--) + + clasament(1,n,1,v[i]); + + for(int i=1;i<=n;i++) + + fout< + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, i, a[120002], f[30001], concurent[30001], poz_temp[30001]; + +void build(int st, int dr, int nod){ + + if(st == dr){ + + a[nod] = f[st]; + + return; + + } + + int mid = (st+dr)/2; + + build(st, mid, 2*nod); + + build(mid+1, dr, 2*nod+1); + + a[nod] = a[2*nod]+a[2*nod+1]; + +} + + + +void cauta(int st, int dr, int nod, int poz){ + + if(st == dr){ + + concurent[st] = i; + + f[st]=0; + + int x = nod; + + while(x){ + + a[x]--; + + x /= 2; + + } + + return; + + } + + int mid = (st+dr)/2; + + if(poz <= a[2*nod]) + + cauta(st, mid, 2*nod, poz); + + else + + cauta(mid+1, dr, 2*nod+1, poz-a[2*nod]); + +} + + + +int main(){ + + fin>>n; + + for(i=1;i<=n;i++){ + + fin>>poz_temp[i]; + + f[i] = 1; + + } + + build(1, n, 1); + + for(i=n;i>0;i--){ + + + + cauta(1, n, 1, poz_temp[i]); + + } + + for(i=1;i<=n;i++) + + fout< +using namespace std; + + + +int C[30001],L[30001]; + +int V[30001*3]; + + + +int n,i; + + + + + + + +void cre(int st, int dr, int nod){ + + int mij; + + if (st==dr){ + + V[nod] = 1; + + } + + else{ + + mij = (st+dr)/2; + + cre(st, mij, 2*nod); + + cre(mij+1, dr, 2*nod+1); + + V[nod] = V[2*nod]+V[2*nod+1]; + + } + +} + + + +void cautSiCorect(int st, int dr, int nod, int c, int poz){ + + int mij; + + if (st==dr){ + + V[nod] = 0; + + L[st] = c; + + } + + else{ + + mij = (st+dr)/2; + + if (V[2*nod]>=poz) + + cautSiCorect(st,mij,2*nod,c,poz); + + else + + cautSiCorect(mij+1,dr,2*nod+1,c,poz-V[2*nod]); + + V[nod] = V[2*nod]+V[2*nod+1]; + + } + +} + + + +int main(){ + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + fin>>n; + + for (i=1; i<=n; i++){ + + fin>>C[i]; + + } + + + + cre(1,n,1); + + + + for (i=n; i>=1; i--){ + + cautSiCorect(1,n,1,i,C[i]); + + } + + + + + + for (i=1; i<=n; i++){ + + fout< +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +typedef pair pairINT; + +typedef long long ll; + +class SegmentTree{ + + public: + + int n; + + int query(int poz){ + + return qry(1,1,n,poz); + + } + + private: + + int segm[(int)(3e4+4)*4]; + + int qry(int node,int l,int r, int poz){ + + if(l == r){ + + segm[node]=1; + + return l; + + } + + int ans,mid=(l+r)>>1,amm=(mid-l+1)-segm[node<<1]; + + if(amm >= poz) + + ans=qry(node<<1,l,mid,poz); + + else + + ans=qry(node<<1|1,mid+1,r,poz-amm); + + segm[node]=segm[node<<1]+segm[node<<1|1]; + + return ans; + + } + +}; + + + +int n,v[(int)(3e4+4)],sol[(int)(3e4+4)]; + +SegmentTree Arb; + + + +int main(){ + + fin>>n; + + Arb.n=n;; + + for(int i=1;i<=n;++i) + + fin>>v[i]; + + for(int i=n;i>0;--i) + + sol[Arb.query(v[i])]=i; + + for(int i=1;i<=n;++i) + + fout< + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int arb[4*30000],v[30000 +2]; + +int fr[30000 +2],val[30000 +2]; + +int n; + + + +void update(int st, int dr, int nod, int pos) + +{ + + if(st==dr) + + { + + arb[nod]=fr[pos]; + + } + + else + + { + + int mij=(st+dr)/2; + + if(pos<=mij) update(st,mij,2*nod,pos); + + else update(mij+1,dr,2*nod+1,pos); + + + + arb[nod]=arb[nod*2]+arb[nod*2+1]; + + } + +} + + + +int query(int st, int dr, int nod, int res) + +{ + + if(st==dr) + + { + + return st; + + } + + else + + { + + int mij=(st+dr)/2; + + if(arb[2*nod]>n; + + for(int i=1; i<=n; i++) + + { + + fin>>v[i]; + + fr[i]=1; + + update(1,n,1,i); + + } + + for(int i=n; i>0; i--) + + { + + int sol=query(1,n,1,v[i]); + + fr[sol]=0; + + update(1,n,1,sol); + + val[sol]=i; + + } + + for(int i=1; i<=n; i++) + + { + + fout< +#include + + + + +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int Arb[4 * 30005 + 5], rez[30005], sol, v[30005]; + + + +void Update(int st, int dr, int poz, int nod){ + + if(st == dr){ + + Arb[nod] = 0; + + return; + + } + + + + int mij = (st + dr) / 2; + + if(poz <= mij) + + Update(st, mij, poz, 2 * nod); + + else Update(mij + 1, dr, poz, 2 * nod + 1); + + + + Arb[nod] = Arb[2 * nod] + Arb[2 * nod + 1]; + +} + + + +void Query(int st, int dr, int nod, int val){ + + if(st == dr){ + + sol = st; + + return; + + } + + + + int mij = (st + dr) / 2; + + if(Arb[2 * nod] < val) + + Query(mij + 1, dr, 2 * nod + 1, val - Arb[2 * nod]); + + else Query(st, mij, 2 * nod, val); + +} + + + +int main() + +{ + + int n; + + fin >> n; + + + + int pw = log2(n) + 1; + + int N = (1 << pw); + + for(int i = 1; i <= n; ++i){ + + fin >> v[i]; + + Arb[N + i - 1] = 1; + + } + + + + for(int i = N - 1; i >= 1; --i) + + Arb[i] = Arb[2 * i] + Arb[2 * i + 1]; + + + + for(int i = n; i >= 1; --i){ + + Query(1, N, 1, v[i]); + + int poz = sol; + + rez[poz] = i; + + Update(1, N, poz, 1); + + } + + + + for(int i = 1; i <= n; ++i) + + fout << rez[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/749e8100-137c-11eb-b521-309c234925a0.cpp b/ski/seg_tree/749e8100-137c-11eb-b521-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c7b8276379ff6f5cefbb9db4a3b779ef6d07440 --- /dev/null +++ b/ski/seg_tree/749e8100-137c-11eb-b521-309c234925a0.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin; +ofstream fout; + +int Arb[4 * 30005 + 5], rez[30005], sol, v[30005]; + +void Update(int st, int dr, int poz, int nod){ + if(st == dr){ + Arb[nod] = 0; + return; + } + + int mij = (st + dr) / 2; + if(poz <= mij) + Update(st, mij, poz, 2 * nod); + else Update(mij + 1, dr, poz, 2 * nod + 1); + + Arb[nod] = Arb[2 * nod] + Arb[2 * nod + 1]; +} + +void Query(int st, int dr, int nod, int val){ + if(st == dr){ + sol = st; + return; + } + + int mij = (st + dr) / 2; + if(Arb[2 * nod] < val) + Query(mij + 1, dr, 2 * nod + 1, val - Arb[2 * nod]); + else Query(st, mij, 2 * nod, val); +} + +int main() +{ + fin.open("schi.in"); + + int n; + fin >> n; + + int N; + for (N=1; N<=n; N <<= 1); + + + for(int i = 1; i <= n; ++i){ + fin >> v[i]; + Arb[N + i - 1] = 1; + } + fin.close(); + + for(int i = N - 1; i >= 1; --i) + Arb[i] = Arb[2 * i] + Arb[2 * i + 1]; + + for(int i = n; i >= 1; --i){ + Query(1, N, 1, v[i]); + int poz = sol; + rez[poz] = i; + Update(1, N, poz, 1); + } + + fout.open("schi.out"); + for(int i = 1; i <= n; ++i) + fout << rez[i] << '\n'; + fout.close(); + + return 0; +} diff --git a/ski/seg_tree/74ae4712-137c-11eb-9abc-309c234925a0.cpp b/ski/seg_tree/74ae4712-137c-11eb-9abc-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db8c53daa45827cb18f3ce001e0f07ab10ea5ff4 --- /dev/null +++ b/ski/seg_tree/74ae4712-137c-11eb-9abc-309c234925a0.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + + + +ifstream x("schi.in"); + +ofstream y("schi.out"); + + + +int n,i,nr,pos,v[30002]; + + + +struct elem + +{ + + int s,lazy; + +}a[4*30002]; + + + +void build(int nod, int left, int right) + +{ + + if(left==right) + + { + + a[nod].lazy=1; + + return; + + } + + + + int mij=(left+right)/2; + + + + build(nod*2,left,mij); + + build(nod*2+1,mij+1,right); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void update(int nod, int left, int right, int m, int val) + +{ + + if(left==right) + + { + + a[right].s=val; + + a[nod].lazy=0; + + return; + + } + + + + int mij=(left+right)/2; + + + + if(m<=a[nod*2].lazy) + + update(nod*2,left,mij,m,val); + + else + + update(nod*2+1,mij+1,right,m-a[nod*2].lazy,val); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void citire() + +{ + + x>>n; + + for(i=1;i<=n;i++) + + x>>v[i]; + + build(1,1,n); + +} + + + +int main() + +{ + + citire(); + + for(i=n;i>=1;i--) + + update(1,1,n,v[i],i); + + for(i=1;i<=n;i++) + + y< + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +void update ( int nod, int left, int right, int poz, int val ); + + + +int f_find ( int nod, int left, int right, int poz ); + + + +int aint[120137]; + + + +int n; + + + +int v[30137]; + + + +int ans[30137]; + + + +int gasit; + + + +int main() + +{ + + in >> n; + + for ( register int i = 1 ; i <= n ; ++i ) + + { + + in >> v[i]; + + update ( 1 , 1 , n , i , 1 ); + + } + + + + for ( register int i = n ; i >= 1 ; --i ) + + { + + gasit = f_find ( 1 , 1 , n , v[i] ); + + ans[gasit] = i; + + update ( 1 , 1 , n , gasit , 0 ); + + } + + for ( register int i = 1 ; i <= n ; ++i ) + + out << ans[i] << '\n'; + + return 0; + +} + + + +void update ( int nod, int left, int right, int poz, int val ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + { + + aint[nod] = val; + + return ; + + } + + if ( poz <= mid ) + + update ( nod * 2 , left , mid , poz , val ); + + else + + update ( nod * 2 + 1 , mid + 1 , right , poz , val ); + + aint[nod] = aint[nod * 2] + aint[nod * 2 + 1]; + +} + + + +int f_find ( int nod, int left, int right, int poz ) + +{ + + int mid = ( left + right ) / 2; + + if ( left == right ) + + return left; + + if ( poz <= aint[nod * 2] ) + + return f_find ( nod * 2 , left , mid , poz ); + + else + + return f_find ( nod * 2 + 1 , mid + 1 , right , poz - aint[nod * 2] ); + +} diff --git a/ski/seg_tree/7508ccb8-137c-11eb-8a98-309c234925a0.cpp b/ski/seg_tree/7508ccb8-137c-11eb-8a98-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7b05462d067b0e7469bf1ad42ff3d815611415e4 --- /dev/null +++ b/ski/seg_tree/7508ccb8-137c-11eb-8a98-309c234925a0.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int NMAX=30003; + +int tree[4*NMAX],v[NMAX],ans[NMAX]; + +int n,poz,r; + +void build_tree(int node,int left,int right){ + + if(left==right){ + + tree[node]=1; + + return ; + + } + + int mid=left+(right-left)/2; + + build_tree(node<<1,left,mid); + + build_tree(node<<1|1,mid+1,right); + + tree[node]=tree[node<<1]+tree[node<<1|1]; + +} + +void querry(int node,int left,int right){ + + if(left==right){ + + ans[left]=r; + + tree[node]=0; + + return; + + } + + int mid=left+(right-left)/2; + + if(tree[node<<1]>=poz) + + querry(node<<1,left,mid); + + else + + poz-=tree[node<<1],querry(node<<1|1,mid+1,right); + + tree[node]=tree[node<<1]+tree[node<<1|1]; + +} + +int main() + +{ + + in>>n; + + for(int i=1;i<=n;i++) + + in>>v[i]; + + build_tree(1,1,n); + + for(int i=n;i>=1;i--){ + + r=i; + + poz=v[i]; + + querry(1,1,n); + + } + + for(int i=1;i<=n;i++) + + out< +#include + + + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int n, n1, v[(1<<15)+10], a[(1<<15)+10], sol[(1<<15)+10], aint[2*(1<<15)+10]; + + + +void build() + +{ for(int i=n; i<2*n; i++) aint[i] = a[i-n+1]; + + for(int i=n-1; i; i--) aint[i] = aint[2*i] + aint[2*i+1]; + +} + + + +void update(int poz) + +{ poz = poz + n - 1; + + while(poz) + + { aint[poz]--; + + poz /= 2; + + } + +} + + + +int query(int nod, int val) + +{ if(nod >= n) return nod-n+1; + + if(val <= aint[2*nod]) return query(2*nod, val); + + else return query(2*nod+1, val-aint[2*nod]); + +} + + + +int main() + +{ + + f >> n; + + n1 = n; + + for(int i=1; i<=n; i++) f >> v[i], a[i] = 1; + + + + if(n & (n-1)) + + { int lastOne = 0; + + for(int i=0; (1< +#include + + + + +using namespace std; + + + +int n; + +int v[30000 + 5], aint[4 * 30000 + 5], ord[30000 + 5]; + + + +void init(int nod, int st, int dr) { + + int mij = (st + dr) / 2; + + + + aint[nod] = dr - st + 1; + + if(dr != st) { + + init(2 * nod, st, mij); + + init(2 * nod + 1, mij + 1, dr); + + } + +} + + + +int query(int nod, int st, int dr, int qval) { + + int mij = (st + dr) / 2; + + + + aint[nod]--; + + + + if(st == dr) + + return st; + + + + if(qval <= aint[2 * nod]) + + return query(2 * nod, st, mij, qval); + + return query(2 * nod + 1, mij + 1, dr, qval - aint[2 * nod]); + +} + + + +int main() { + + freopen("schi.in", "r", stdin); + + freopen("schi.out", "w", stdout); + + int poz; + + + + scanf("%d", &n); + + for(int i = 1; i <= n; i++) + + scanf("%d", &v[i]); + + + + init(1, 1, n); + + for(int i = n; i >= 1; i--) { + + poz = query(1, 1, n, v[i]); + + ord[poz] = i; + + } + + + + for(int i = 1; i <= n; i++) + + printf("%d\n", ord[i]); + + + + return 0; + +} diff --git a/ski/seg_tree/75917a58-137c-11eb-87ff-309c234925a0.cpp b/ski/seg_tree/75917a58-137c-11eb-87ff-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a865d48566fab77b453256449e2989ae0801f84 --- /dev/null +++ b/ski/seg_tree/75917a58-137c-11eb-87ff-309c234925a0.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + +int v[300607],i,j,clasament[300607],ordine[300607],n; + +void build(int nod,int st,int dr) + +{ + + if(st==dr) + + { + + v[nod]=1; + + return; + + } + + int mij=(st+dr)/2; + + build(nod*2,st,mij); + + build(nod*2+1,mij+1,dr); + + v[nod]=v[nod*2]+v[nod*2+1]; + +} + +void update(int nod,int st,int dr,int val,int pos) + +{ + + if(st==dr) + + { + + v[nod]=val; + + return; + + } + + int mij=(st+dr)/2; + + if(pos<=mij) + + update(nod*2,st,mij,val,pos); + + else + + update(nod*2+1,mij+1,dr,val,pos); + + v[nod]=v[nod*2]+v[nod*2+1]; + +} + +int query(int nod,int st,int dr,int val) + +{ + + if(st==dr) + + return st; + + int mij=(st+dr)/2; + + if(val<=v[nod*2]) + + query(nod*2,st,mij,val); + + else + + query(nod*2+1,mij+1,dr,val-v[nod*2]); + +} + +int main() + +{ + + f>>n; + + for(i=1;i<=n;i++) + + f>>ordine[i]; + +build(1,1,n); + +for(i=n;i>=1;i--) + +{ + + int pozitie=query(1,1,n,ordine[i]); + + clasament[pozitie]=i; + + update(1,1,n,0,pozitie); + +} + +for(i=1;i<=n;i++) + + g< + +using namespace std; + +ifstream fin ("schi.in"); +ofstream fout ("schi.out"); + +int n, s = 1, p, k; +int v[30005], a[200005], ans[30005]; + +void Update(int poz, int val) { + a[poz] += val; + if (poz > 1) + Update(poz / 2, val); +} + +int Query(int val, int nod, int sum) { + if (nod >= s) + return nod - s + 1; + if (a[nod * 2] + sum >= val) + return Query(val, nod * 2, sum); + return Query(val, nod * 2 + 1, sum + a[nod * 2]); +} + +int main() { + fin >> n; + for (int i = 1; i <= n; ++i) + fin >> v[i]; + int cn = n; + while (cn) { + cn >>= 1; + s <<= 1; + } + if (s >> 1 == n) + s >>= 1; + k = n + 1; + for (int i = 0; i < n; ++i) + Update(s + i, 1); + for (int i = n; i; --i) { + p = Query(v[i], 1, 0); + Update(s + p - 1, -1); + ans[p] = --k; + } + for (int i = 1; i <= n; ++i) + fout << ans[i] << '\n'; + return 0; +} diff --git a/ski/seg_tree/7794e668-137c-11eb-a900-309c234925a0.cpp b/ski/seg_tree/7794e668-137c-11eb-a900-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..368f9033f0bdc67c262b9b542217865e78621328 --- /dev/null +++ b/ski/seg_tree/7794e668-137c-11eb-a900-309c234925a0.cpp @@ -0,0 +1,132 @@ +#include + + +using namespace std; + + + +const int NMAX = 30005; + +int aint[NMAX*4]; + +int v[NMAX]; + +int sol[NMAX]; + +int n; + + + +void build(int nod,int st,int dr) + +{ + + if(st == dr) + + { + + aint[nod] = 1; + + return; + + } + + int mij = (st+dr)/2; + + build(2*nod,st,mij); + + build(2*nod+1,mij+1,dr); + + aint[nod] = aint[2*nod]+aint[2*nod+1]; + +} + + + +void update(int nod,int st,int dr,int pos,int val) + +{ + + if(st == dr) + + { + + aint[nod] = val; + + return; + + } + + int mij = (st+dr)/2; + + if(pos <= mij) + + update(2*nod,st,mij,pos,val); + + else + + update(2*nod+1,mij+1,dr,pos,val); + + aint[nod] = aint[2*nod] + aint[2*nod+1]; + +} + + + +int query(int nod,int st,int dr,int val) + +{ + + if(st == dr) + + return st; + + int mij = (st+dr)/2; + + if(val <= aint[2*nod]) + + return query(2*nod,st,mij,val); + + else + + return query(2*nod+1,mij+1,dr,val-aint[2*nod]); + +} + + + +int main() + +{ + + freopen("schi.in","r",stdin); + + freopen("schi.out","w",stdout); + + scanf("%d",&n); + + for(int i = 1 ; i <= n ; i++) + + scanf("%d",&v[i]); + + build(1,1,n); + + for(int i = n ; i >= 1 ; i--) + + { + + int pos = query(1,1,n,v[i]); + + sol[pos] = i; + + update(1,1,n,pos,0); + + } + + for(int i = 1 ; i <= n ; i++) + + printf("%d\n",sol[i]); + + return 0; + +} diff --git a/ski/seg_tree/77a385fa-137c-11eb-a7b2-309c234925a0.cpp b/ski/seg_tree/77a385fa-137c-11eb-a7b2-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31458dd93d446833961fe4b8b36b9e4f51fec2a5 --- /dev/null +++ b/ski/seg_tree/77a385fa-137c-11eb-a7b2-309c234925a0.cpp @@ -0,0 +1,122 @@ +#include + + +using namespace std; + + + +const int nmax=3e4+5; + +int n,v[nmax],arb[4*nmax],rasp[nmax]; + + + +void build(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + arb[nod] = 1; + + return; + + } + + int mijl=(st+dr)/2; + + build(nod*2,st,mijl); + + build(nod*2+1,mijl+1,dr); + + arb[nod]=arb[nod*2]+arb[nod*2+1]; + +} + +void update(int nod,int st,int dr,int x) + +{ + + if (st==dr) + + { + + arb[nod]=0; + + return; + + } + + int mijl=(st+dr)/2; + + if (mijl>=x) + + update(nod*2,st,mijl,x); + + else + + update(nod*2+1,mijl+1,dr,x); + + arb[nod]=arb[nod*2]+arb[nod*2+1] ; + +} + +int query(int nod,int st,int dr,int val) + +{ + + if(st==dr) + + return st; + + int mijl=(st+dr)/2; + + if (arb[nod*2]>n; + + for(int i=1;i<=n;i++) + + cin>>v[i]; + + build(1,1,n); + + for(int i=n;i>0;i--) + + { + + poz=query(1,1,n,v[i]); + + rasp[poz]=i; + + update(1,1,n,poz); + + } + + for (int i=1;i<=n;i++) + + cout< + + +using namespace std; + + + +int n,v[30005],pom[120005],ans[30005]; + + + +void build(int nod, int st, int dr) { + + if (st == dr) { + + pom[nod] = 1; + + return; + + } + + int md = (st + dr) / 2; + + build(nod * 2, st, md); + + build(nod * 2 + 1, md + 1, dr); + + pom[nod] = pom[nod * 2] + pom[nod * 2 + 1]; + +} + +void update(int nod, int st, int dr, int x) { + + if (st == dr) { + + pom[nod] = 0; + + return; + + } + + int md = (st + dr) / 2; + + if (md >= x) + + update(nod * 2, st, md, x); + + else + + update(nod * 2 + 1, md + 1, dr, x); + + pom[nod] = pom[nod * 2] + pom[nod * 2 + 1] ; + +} + +int querry(int nod, int st, int dr, int x) { + + if(st == dr) + + return st; + + int md = (st + dr) / 2; + + if (pom[nod * 2] < x) + + return querry(nod * 2 + 1, md + 1, dr, x - pom[nod * 2]) ; + + else + + return querry(nod * 2, st, md, x) ; + +} + +int main() { + + ifstream fin("schi.in"); + + ofstream fout("schi.out"); + + int poz; + + fin >> n; + + for (int i = 1;i <= n;i++) + + fin >> v[i]; + + build(1, 1, n); + + for (int i = n;i > 0;i--) { + + poz = querry(1, 1, n, v[i]); + + ans[poz] = i; + + update(1, 1, n, poz); + + } + + for (int i = 1;i <= n;i++) + + fout << ans[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/77c124e2-137c-11eb-8026-309c234925a0.cpp b/ski/seg_tree/77c124e2-137c-11eb-8026-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1104318c3bde43fb9e50ea4ffdafb5245ff39c9a --- /dev/null +++ b/ski/seg_tree/77c124e2-137c-11eb-8026-309c234925a0.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + + + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); +const int MAXN=4e4; + +int n, place; + +int arbore[MAXN*4]; + +int v[MAXN], sol[MAXN]; + + + + + +void build(int left, int right, int nod) + +{ + + if(left==right) + + { + + arbore[nod]=1; + + return; + + } + + int mij=(left+right)/2; + + build(left, mij, nod*2); + + build(mij+1, right, nod*2+1); + + arbore[nod]=arbore[nod*2]+arbore[nod*2+1]; + +} + + + +int query(int kel, int left, int right, int nod) + +{ + + if(left==right) + + return left; + + int mij=(left+right)/2; + + if(arbore[2*nod]>=kel) + + return query(kel, left, mij, nod*2); + + return query(kel-arbore[nod*2], mij+1, right, nod*2+1); + +} + + + + + +void update(int pos, int left, int right, int nod) + +{ + + arbore[nod]--; + + if(left==right) + + return; + + int mij=(left+right)/2; + + if(pos<=mij) + + update(pos, left, mij, nod*2); + + else + + update(pos, mij+1, right, nod*2+1); + +} +int main() + +{ + + fin>>n; + + for(int i=1; i<=n; i++) + + fin>>v[i]; + + build(1,n,1); + + for(int i=n; i>=1; i--) + + { + + place=query(v[i], 1, n, 1); + + update(place, 1, n, 1); + + sol[place]=i; + + } + + for(int i=1; i<=n; i++) + + fout< +using namespace std; + + + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int t[1000000]; + + + +int n, m, sol[30002], v[30002]; + + + +void build(int tp, int tl, int tr) + +{ + + if(tl==tr) t[tp]=1; + + else + + { + + int m=(tl+tr)/2; + + build(tp*2, tl, m); + + build(tp*2+1, m+1, tr); + + t[tp]=t[tp*2]+t[tp*2+1]; + + } + +} + + + +void update(int tp, int tl, int tr, int poz) + +{ + + t[tp]--; + + if(tl!=tr) + + { + + int m=(tl+tr)/2; + + if(poz<=m) update(tp*2, tl, m, poz); + + else update(tp*2+1, m+1, tr, poz); + + } + +} + + + +int query(int tp, int tl, int tr, int x) + +{ + + if(tl==tr) return tl; + + else + + { + + int m=(tl+tr)/2; + + if(x<=t[tp*2]) return query(tp*2, tl, m, x); + + else return query(tp*2+1, m+1, tr, x-t[tp*2]); + + } + +} + + + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;++i) fin>>v[i]; + + build(1, 1, n); + + for(int i=n;i;--i) + + { + + int place=query(1, 1, n, v[i]); + + update(1, 1, n, place); + + sol[place]=i; + + } + + for(int i=1;i<=n;++i) fout< +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int dim = 30005; + + + +int a[dim],n,aib[dim],rasp[dim]; + + + +void update(int poz,int val) + +{ + + while (poz <= n) + + { + + aib[poz] += val; + + poz += poz& (-poz); + + } + +} + + + +int main() + +{ + + in >> n; + + int log = 1; + + while (log <= n) + + { + + log *= 2; + + } + + log /= 2; + + for (int i=1; i<=n; i++) + + { + + in >> a[i]; + + update(i,1); + + } + + + + for (int i=n; i>=1; i--) + + { + + int baza = 0; + + int off = log,s = 0,ok = 0,rap = 0; + + a[i]--; + + while (off >= 1) + + { + + while (baza + off > n) + + { + + off /= 2; + + } + + s += aib[baza + off]; + + if (s <= a[i]) + + { + + baza += off; + + } + + else if (s > a[i]) + + { + + s -= aib[baza + off]; + + } + + off /= 2; + + } + + rasp[baza+1] = i; + + update(baza+1, -1); + + } + + for (int i=1; i<=n; i++) + + { + + out << rasp[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/seg_tree/77eed786-137c-11eb-8e6d-309c234925a0.cpp b/ski/seg_tree/77eed786-137c-11eb-8e6d-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6389582bd7bfcb30d8c32b101bf8c65220ba3eb8 --- /dev/null +++ b/ski/seg_tree/77eed786-137c-11eb-8e6d-309c234925a0.cpp @@ -0,0 +1,173 @@ +#include +#include + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int dim = 30005; + + + +int n,m,arb[4*dim],a[dim],rasp[dim]; + + + +void update(int nod,int st,int dr,int index,int val) + +{ + + if (st == dr) + + { + + arb[nod] = val; + + return ; + + } + + int mid = (st+dr)/2; + + if (index <= mid) + + { + + update(2*nod,st,mid,index,val); + + } + + else + + { + + update(2*nod+1,mid+1,dr,index,val); + + } + + arb[nod] = arb[2*nod] + arb[2*nod+1]; + +} + + + +int query(int nod,int st,int dr,int value) + +{ + + if (st == dr) + + { + + return st; + + } + + int mid = (st+dr)/2; + + if (value == arb[nod]) + + { + + if (arb[2*nod] >= value) + + { + + return query(2*nod,st,mid,value); + + } + + else + + { + + return query(2*nod+1,mid+1,dr,value - arb[2*nod]); + + } + + } + + else + + { + + if (arb[2*nod] < value) + + { + + return query(2*nod+1,mid+1,dr,value-arb[2*nod]) ; + + } + + else + + { + + return query(2*nod,st,mid,value); + + } + + } + +} + + + +int main() + +{ + + in >> n; + + int log = 1; + + while (log <= n) + + { + + log *= 2; + + } + + log /= 2; + + for (int i=1; i<=n; i++) + + { + + update(1,1,n,i,1); + + in >> a[i]; + + } + + for (int i=n; i>=1; i--) + + { + + int poz = query(1,1,n,a[i]); + + rasp[poz] = i; + + update(1,1,n,poz,0); + + } + + for (int i=1; i<=n; i++) + + { + + out << rasp[i] << "\n"; + + } + + return 0; + +} diff --git a/ski/seg_tree/780cb2cc-137c-11eb-ac98-309c234925a0.cpp b/ski/seg_tree/780cb2cc-137c-11eb-ac98-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4155d497c018d4f6f00a3f8aa572fe1d0d06f1a6 --- /dev/null +++ b/ski/seg_tree/780cb2cc-137c-11eb-ac98-309c234925a0.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + + + +ifstream fin( "schi.in" ); + +ofstream fout( "schi.out" ); + + + +const int NMAX = 30000; + +int v[NMAX + 1]; + +int sol[NMAX + 1]; + +int aint[NMAX * 4 + 1]; + + + +void build( int nod, int st, int dr ){ + + int med; + + if( st == dr ){ + + aint[nod] = v[st]; + + return; + + } + + med = (st + dr) >> 1; + + build( nod << 1, st, med ); + + build( (nod << 1) | 1, med + 1, dr ); + + aint[nod] = aint[nod << 1] + aint[(nod << 1) | 1]; + +} + + + +int query_bun(int nod, int x, int val) + +{ + + int st, pas, s=0; + + st = 0; + + for(pas=x;pas>0;pas>>=1) + + if(aint[nod<<1]+s> 1; + + if( a <= med ) + + update( (nod << 1), st, med, a, val ); + + else + + update( (nod << 1) | 1, med + 1, dr, a, val ); + + aint[nod] = aint[nod << 1] + aint[(nod << 1) | 1]; + +} + + + +int main() { + + int n, i, nnou,s; + + fin >> n; + + nnou = 1; + + while(nnou> v[i]; + + for( i = n; i >= 1; --i ){ + + s=query_bun(1, nnou>>1, v[i]); + + sol[s] = i; + + update( 1, 1, nnou, s, 0 ); + + } + + for( i = 1; i <= n; ++i ) + + fout << sol[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/782a7c52-137c-11eb-8729-309c234925a0.cpp b/ski/seg_tree/782a7c52-137c-11eb-8729-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbaca3b0ac06bfe5d8e6e8d7dfdb8fe46e6ff357 --- /dev/null +++ b/ski/seg_tree/782a7c52-137c-11eb-8729-309c234925a0.cpp @@ -0,0 +1,154 @@ +#include + + +FILE *in = fopen("schi.in", "r"), *out = fopen("schi.out", "w") ; + + + +const int MV = 3e4 ; + +const int SIZE = 1 << 12 ; + + + +int n ; + +int v[MV + 5] ; + +int aint[(MV << 2) + 5] ; + +int ans[MV + 5] ; + +char Buffer[SIZE], ch ; + +int point(SIZE) ; + + + +void advance() { + + if (point == SIZE) { + + fread(Buffer, 1, SIZE, in) ; + + point = 0 ; + + } + + ch = Buffer[point ++] ; + +} + + + +int read() { + + int ret(0) ; + + while (!isdigit(ch)) { advance() ; } + + for ( ; isdigit(ch) ; advance()) { ret = ret * 10 + (ch - '0') ; } + + return ret ; + +} + + + +void Build(int node, int st, int dr) { + + if (st == dr) { + + aint[node] = 1 ; + + return ; + + } + + int mij((st + dr) >> 1) ; + + Build(node << 1, st, mij) ; + + Build((node << 1) + 1, mij + 1, dr) ; + + aint[node] = aint[node << 1] + aint[(node << 1) + 1] ; + +} + + + +void update(int node, int st, int dr, int x) { + + if (st == dr) { + + aint[node] = 0 ; + + return ; + + } + + int mij((st + dr) >> 1) ; + + if (mij >= x) { + + update(node << 1, st, mij, x) ; + + } else update((node << 1) + 1, mij + 1, dr, x) ; + + aint[node] = aint[node << 1] + aint[(node << 1) + 1] ; + +} + + + +int querry(int node, int st, int dr, int x) { + + if(st == dr) { + + return st ; + + } + + int mij((st + dr) >> 1) ; + + if (aint[node << 1] < x) { + + return querry((node << 1) + 1, mij + 1, dr, x - aint[node << 1]) ; + + } else return querry(node << 1, st, mij, x) ; + +} + + + +int main() { + + n = read() ; + + int poz ; + + for (int i = 1 ; i <= n ; ++ i) { + + v[i] = read() ; + + } + + Build(1, 1, n) ; + + for (int i = n ; i > 0 ; -- i) { + + poz = querry(1, 1, n, v[i]) ; + + ans[poz] = i ; + + update(1, 1, n, poz) ; + + } + + for (int i = 1 ; i <= n ; ++ i) { + + fprintf(out, "%d\n", ans[i]) ; + + } + +} diff --git a/ski/seg_tree/783935cc-137c-11eb-abc7-309c234925a0.cpp b/ski/seg_tree/783935cc-137c-11eb-abc7-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bacd93b7c12f228d8cdae0f55399e58c50e5db5f --- /dev/null +++ b/ski/seg_tree/783935cc-137c-11eb-abc7-309c234925a0.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +int a[3*30005],v[30005]; + + + +inline void Build(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + a[nod]=1; + + return ; + + } + + int mij=(st+dr)/2; + + Build(2*nod,st,mij); + + Build(2*nod+1,mij+1,dr); + + a[nod]=a[2*nod]+a[2*nod+1]; + +} + + + +inline void Update(int nod, int st, int dr, int poz) + +{ + + if(st==dr) + + { + + a[nod]=0; + + return ; + + } + + int mij=(st+dr)/2; + + if(poz<=mij) + + Update(2*nod,st,mij,poz); + + else + + Update(2*nod+1,mij+1,dr,poz); + + a[nod]=a[2*nod]+a[2*nod+1]; + +} + + + +inline int Query(int nod, int st, int dr, int x) + +{ + + if(st==dr) + + return st; + + int mij=(st+dr)/2; + + if(x<=a[2*nod]) + + return Query(2*nod,st,mij,x); + + else + + return Query(2*nod+1,mij+1,dr,x-a[2*nod]); + +} + + + +int main() + +{ + + freopen ("schi.in","r",stdin); + + freopen ("schi.out","w",stdout); + + int i,poz,sol[30005],N; + + scanf("%d", &N); + + Build(1,1,N); + + for(i=1; i<=N; ++i) + + scanf("%d", &v[i]); + + for(i=N; i>0; --i) + + { + + poz=Query(1,1,N,v[i]); + + sol[poz]=i; + + Update(1,1,N,poz); + + } + + for(i=1; i<=N; ++i) + + printf("%d\n", sol[i]); + + return 0; + +} diff --git a/ski/seg_tree/789b21cc-137c-11eb-b315-309c234925a0.cpp b/ski/seg_tree/789b21cc-137c-11eb-b315-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3dc7f94c239167cd2f1d3bae4f9d2c2909e213d9 --- /dev/null +++ b/ski/seg_tree/789b21cc-137c-11eb-b315-309c234925a0.cpp @@ -0,0 +1,184 @@ +#include + + +using namespace std; + + + +const int MAX = 131072; + +const int NMAX = 30100; + +const int INF = 1e6; + + + +struct arb{ + + int mi, del, poz; + +} T[3*NMAX]; + + + +int N, ans; + +int v[NMAX]; + + + +int pos; + +char f[MAX]; + + + +inline void Read(int &nr){ + + nr = 0; + + while(f[pos] < '0' || f[pos] > '9'){ + + pos++; + + if(pos == MAX) + + fread(f, MAX, 1, stdin), pos = 0; + + } + + while(f[pos] >= '0' && f[pos] <= '9'){ + + nr = 10 * nr + f[pos++] - '0'; + + if(pos == MAX) + + fread(f, MAX, 1, stdin), pos = 0; + + } + +} + + + +void build(int node, int st, int nd){ + + if(st == nd){ + + T[node].mi = v[st]; + + T[node].poz = st; + + } else { + + int mid = (st + nd) / 2; + + build(2 * node, st, mid); + + build(2 * node + 1, mid + 1, nd); + + T[node].mi = T[2 * node + 1].mi; + + T[node].poz = T[2 * node + 1].poz; + + if(T[2 * node].mi < T[2 * node + 1].mi){ + + T[node].mi = T[2 * node].mi; + + T[node].poz = T[2 * node].poz; + + } + + } + +} + + + +int Update(int node, int st, int nd){ + + if(st == nd){ + + T[node].mi = INF; + + return 0; + + } else { + + int mid = (st + nd) / 2; + + if(T[2 * node].mi < T[2 * node + 1].mi){ + + T[2 * node + 1].del++; + + T[2 * node + 1].mi--; + + Update(2 * node, st, mid); + + } else Update(2 * node + 1, mid + 1, nd); + + } + + T[node].mi = T[2 * node].mi; + + T[node].poz = T[2 * node].poz; + + if(T[node].mi >= T[2 * node + 1].mi){ + + T[node].mi = T[2 * node + 1].mi; + + T[node].poz = T[2 * node + 1].poz; + + } + + T[node].mi -= T[node].del; + +} + + + +int read(){ + + fread(f, 1, MAX, stdin); + + Read(N); + + for(int i = 1; i <= N; i++) + + Read(v[i]); + +} + + + +int main(){ + + + + freopen("schi.in", "r", stdin); + + freopen("schi.out", "w", stdout); + + + + read(); + + build(1, 1, N); + + + + for(int i = 1; i <= N; i++){ + + ans = T[1].poz; + + Update(1, 1, N); + + printf("%d\n", ans); + + } + + + + return 0; + +} diff --git a/ski/seg_tree/78a9529e-137c-11eb-8103-309c234925a0.cpp b/ski/seg_tree/78a9529e-137c-11eb-8103-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f991c0aa4ad2ecb0ef35c5f5ffe8a3410b420b84 --- /dev/null +++ b/ski/seg_tree/78a9529e-137c-11eb-8103-309c234925a0.cpp @@ -0,0 +1,58 @@ +#include + + + +using namespace std; + + +ifstream f("schi.in"); +ofstream g("schi.out"); + +int a[30005], n; +int tree[30005 << 2]; +int ans[30005]; + +void update (int nod, int i, int j, int poz, const int k ) +{ + if (i==j) + { + tree[nod]=0; + ans[i]=k; + } + else + { + int m=(i+j)/2; + if (poz<=tree[nod*2]) + update(nod*2, i, m, poz, k); + else + update(nod* 2+1, m+1, j, poz-tree[nod*2], k); + tree[nod]=tree[nod * 2 ]+tree[nod * 2 + 1 ]; + } +} + + +void create(int nod, int i, int j) +{ + if (i==j) + tree[nod]=1; + else + { + int m=(i+j)/2; + create (nod*2, i, m); + create (nod*2+1, m+1, j); + tree[nod]=tree[nod * 2]+tree[nod * 2 + 1]; + } +} + +int main() +{ + f>>n; + create(1 ,1 ,n); + for (int i=1; i <= n; i ++) + f>>a[i]; + for (int i=n; i>=1; i --) + update(1, 1, n, a[i], i); + for (int i=1; i <=n; i ++) + g< + + +using namespace std; + + + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +int n, i; + +int cate[30001 << 2], rez[30001], v[30001]; + + + +inline void build(int nod, int st, int dr) + +{ + + if (st == dr) + + { + + cate[nod] = 1; + + return; + + } + + int mid = (st + dr) / 2; + + build(2 * nod, st, mid); + + build(2 * nod + 1, mid + 1, dr); + + cate[nod] = cate[2 * nod] + cate[2 * nod + 1]; + +} + + + +inline void update(int nod, int st, int dr, int val) + +{ + + if (st == dr) + + { + + rez[st] = i; + + cate[nod] = 0; + + return; + + } + + int mid = (st + dr) / 2; + + if (cate[2 * nod] >= val) + + update(2 * nod, st, mid, val); + + else + + update(2 * nod + 1, mid + 1, dr, val - cate[2 * nod]); + + cate[nod] = cate[2 * nod] + cate[2 * nod + 1]; + +} + + + +int main() + +{ + + in >> n; + + for (i = 1; i <= n; ++i) + + in >> v[i]; + + build(1, 1, n); + + for (i = n; i >= 1; --i) + + update(1, 1, n, v[i]); + + for (i = 1; i <= n; ++i) + + out << rez[i] << '\n'; + + return 0; + +} diff --git a/ski/seg_tree/78e60b80-137c-11eb-8005-309c234925a0.cpp b/ski/seg_tree/78e60b80-137c-11eb-8005-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7325eac26854e32297f1c4c0ce62decb79de00ed --- /dev/null +++ b/ski/seg_tree/78e60b80-137c-11eb-8005-309c234925a0.cpp @@ -0,0 +1,108 @@ +#include + +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int DIM = 30007; + +int arb[DIM * 4]; +int sol[DIM]; + +void build(int pos, int l, int r) +{ + arb[pos] = r - l + 1; + + if(l != r) + { + int mid = (l + r) / 2; + + build(pos * 2, l, mid); + build(pos * 2 + 1, mid + 1, r); + } +} + +int query(int pos, int l, int r, int x) +{ + if(x >= r) + { + return arb[pos]; + } + + int mid = (l + r) / 2; + + int ans = 0; + + if(x <= mid) + { + ans = query(pos * 2, l, mid, x); + } + else + { + ans = arb[pos * 2] + query(pos * 2 + 1, mid + 1, r, x); + } + + return ans; +} + +void update(int pos, int l, int r, int x) +{ + if(l == r) + { + arb[pos] = 0; + return ; + } + + int mid = (l + r) / 2; + + if(x <= mid) + update(pos * 2, l, mid, x); + else + update(pos * 2 + 1, mid + 1, r, x); + + arb[pos] = arb[pos * 2] + arb[pos * 2 + 1]; +} + +int v[DIM]; + +main() +{ + int n; + fin >> n; + + build(1, 1, n); + + for(int i = 1; i <= n; i++) + fin >> v[i]; + + for(int i = n; i >= 1; i--) + { + int l = 1; + int r = n; + + int pos = 0; + + while(l <= r) + { + int mid = (l + r) / 2; + + if(query(1, 1, n, mid) < v[i]) + { + l = mid + 1; + } + else + { + pos = mid; + r = mid - 1; + } + } + + sol[pos] = i; + + update(1, 1, n, pos); + } + + for(int i = 1; i <= n; i++) + fout << sol[i] << '\n'; +} diff --git a/ski/seg_tree/7923c5dc-137c-11eb-aa90-309c234925a0.cpp b/ski/seg_tree/7923c5dc-137c-11eb-aa90-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0325efc03f133eda282fb1a008f8be5555edf5a9 --- /dev/null +++ b/ski/seg_tree/7923c5dc-137c-11eb-aa90-309c234925a0.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +const int N=30001; + +int concurent[N],v[N],nr[1<<16],n,poz; + + + +void prelucrare(int p,int st,int dr) + +{ + + if(st==dr) + + { + + concurent[st]=poz; + + nr[p]++; + + return; + + } + + int m=(st+dr)/2; + + if(m-st+1-nr[2*p]>=v[poz]) + + { + + prelucrare(2*p,st,m); + + } + + else + + { + + v[poz]-=m-st+1-nr[2*p]; + + prelucrare(2*p+1,m+1,dr); + + } + + nr[p]++; + +} + +int main() + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i]; + + for(int i=n;i>=1;i--) + + { + + poz=i; + + prelucrare(1,1,n); + + } + + for(int i=1;i<=n;i++) + + fout< + +using namespace std; + +ifstream fin ("schi.in"); +ofstream fout ("schi.out"); + +const int MAX = 1e5 + 14; + +int v [MAX]; +int tree [MAX << 2]; +int ans [MAX]; + +void build (int nod, int st, int dr) +{ + if (st == dr) tree [nod] = 1; + else + { + int mij = (st + dr) >> 1; + build(nod * 2, st, mij); + build(nod * 2 + 1, mij + 1, dr); + tree [nod] = tree[nod << 1] + tree [nod << 1 | 1]; + } +} + +void update (int nod, int st, int dr, int pos, const int ind) +{ + if (st == dr) + { + tree[nod] = 0; + ans[st] = ind; + } + else { + int mij = (st + dr) >> 1; + if (pos <= tree[nod << 1]) + update (nod * 2, st, mij, pos, ind); + else + update (nod * 2 + 1, mij + 1, dr, pos - tree[nod << 1], ind); + tree [nod] = tree[nod << 1] + tree [nod << 1 | 1]; + } +} + +int main() { + int n; + fin >> n; + for (int i = 1; i <= n; ++ i) + fin >> v [i]; + build(1, 1, n); + for (int i = n; i >= 1; -- i) + { + update(1, 1, n, v [i], i); + } + for (int i = 1; i <= n; ++ i) + fout << ans [i] << '\n'; + return 0; +} diff --git a/ski/seg_tree/79634982-137c-11eb-b28e-309c234925a0.cpp b/ski/seg_tree/79634982-137c-11eb-b28e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..827c0e6fec44cbcc39d5d7e0e770922746b4efef --- /dev/null +++ b/ski/seg_tree/79634982-137c-11eb-b28e-309c234925a0.cpp @@ -0,0 +1,35 @@ +#include + +using namespace std; + +int aint[131073]; +int vin[30000], vout[30000]; + +int mod ( int p, int st, int dr, int poz ) { + if ( st == dr ) { + aint[p] = 1; + return st; + } + int m = ( st + dr ) / 2, r; + int libere_st = m - st + 1 - aint[2*p]; + if ( poz <= libere_st ) + r = mod (2*p, st, m, poz); + else + r = mod (2*p+1, m+1, dr, poz-libere_st); + aint[p]++; + return r; +} + +int main () { + ifstream fin ("schi.in"); + ofstream fout ("schi.out"); + int n; + fin>>n; + for ( int i = 1; i <= n; i++ ) + fin>>vin[i]; + for ( int i = n; i >= 1; i-- ) + vout[mod(1,1,n,vin[i])] = i; + for ( int i = 1; i <= n; i++ ) + fout< + +using namespace std; +ifstream fin("schi.in"); +ofstream fout("schi.out"); +const int N=30001; +int concurent[N],v[N],nr[1<<16],n,poz; + +void prelucrare(int p,int st,int dr) +{ + if(st==dr) + { + concurent[st]=poz; + nr[p]++; + return; + } + int m=(st+dr)/2; + if(m-st+1-nr[2*p]>=v[poz]) + { + prelucrare(2*p,st,m); + } + else + { + v[poz]-=m-st+1-nr[2*p]; + prelucrare(2*p+1,m+1,dr); + } + nr[p]++; +} +int main() +{ + fin>>n; + for(int i=1;i<=n;i++) + fin>>v[i]; + for(int i=n;i>=1;i--) + { + poz=i; + prelucrare(1,1,n); + } + for(int i=1;i<=n;i++) + fout< + +int tree[1 << 16], i, n, x, v[30001], r[30001]; + +inline void add(int p, int st, int dr) { + ++tree[p]; + if (st == dr) r[st] = i; + else { + int m = (st + dr) / 2; + int l = m - st + 1 - tree[2 * p]; + if (l >= x) add(2 * p, st, m); + else { + x -= l; + add(2 * p + 1, m + 1, dr); + } + } +} + +int main() { + std::ifstream in("schi.in"); + std::ofstream out("schi.out"); + in >> n; + for (i = 1; i <= n; ++i) in >> v[i]; + for (i = n; i >= 1; --i) { + x = v[i]; + add(1, 1, n); + } + for (i = 1; i <= n; ++i) out << r[i] << '\n'; + return 0; +} diff --git a/ski/seg_tree/79eaeab6-137c-11eb-ab97-309c234925a0.cpp b/ski/seg_tree/79eaeab6-137c-11eb-ab97-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4d74d5902696a0648e460abe6f86ee7ef9d7a94 --- /dev/null +++ b/ski/seg_tree/79eaeab6-137c-11eb-ab97-309c234925a0.cpp @@ -0,0 +1,109 @@ +#include +#include + + + +using namespace std; + +int n, v[30005], l[2*30005]; +pair arbint[4*30005]; + +pair maxPair(pair a, pair b){ + if(a.firstb.second) + return a; + return b; +} + +void construieste(int st = 1, int dr = n, int pos = 1){ + if(st==dr){ + arbint[pos] = {v[st],st}; + return ; + } + + int mij = (st+dr)/2; + + construieste(st,mij,2*pos); + construieste(mij+1,dr,2*pos+1); + + arbint[pos] = maxPair(arbint[2*pos], arbint[2*pos+1]); +} + +void actualizareNod(int k, int st = 1, int dr = n, int pos = 1){ + if(l[pos]){ + if(st!=dr){ + arbint[2*pos].first += l[pos]; + arbint[2*pos+1].first += l[pos]; + l[2*pos] += l[pos]; + l[2*pos+1] += l[pos]; + } + l[pos] = 0; + } + + if(st==dr){ + arbint[pos].first = 30005; + return ; + } + + int mij = (st+dr)/2; + if(k<=mij) + actualizareNod(k,st,mij,2*pos); + else + actualizareNod(k,mij+1,dr,2*pos+1); + + arbint[pos] = maxPair(arbint[2*pos], arbint[2*pos+1]); +} + +void actualizareInterval(int qst, int qdr, int st = 1, int dr = n, int pos = 1){ + if(l[pos]){ + if(st!=dr){ + arbint[2*pos].first += l[pos]; + arbint[2*pos+1].first += l[pos]; + l[2*pos] += l[pos]; + l[2*pos+1] += l[pos]; + } + l[pos] = 0; + } + if(qst<=st && dr<=qdr){ + arbint[pos].first++; + l[pos]++; + return ; + } + + int mij = (st+dr)/2; + if(qdr<=mij) + actualizareInterval(qst,qdr,st,mij,2*pos); + else if(mij + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +const int NMAX = 30005; + +int n,arb[8 * NMAX],v[NMAX],ans[NMAX]; + + + +void build(int nod, int lo, int hi){ + + if(lo == hi){ + + arb[nod] = 1; + + return; + + } + + int mid = (lo + hi) / 2; + + build(2 * nod, lo , mid); + + build(2 * nod + 1, mid + 1, hi); + + arb[nod] = hi - lo + 1; + +} + + + +void update(int nod, int lo, int hi, int x, int y){ + + if(lo == hi){ + + arb[nod] = 0; + + ans[lo] = y; + + return; + + } + + int mid = (lo + hi) / 2; + + if(x <= arb[2 * nod]) + + update(2 * nod, lo, mid, x, y); + + else + + update(2 * nod + 1, mid + 1, hi, x - arb[2 * nod], y); + + + + arb[nod]--; + +} + + + +int main(){ + + int i; + + f >> n; + + build(1,1,n); + + for(i = 1 ; i <= n ; i++) + + f >> v[i]; + + + + for(i = n ; i >= 1 ; i--){ + + update(1,1,n,v[i],i); + + } + + + + for(i = 1 ; i <= n ; i++) + + g << ans[i] << "\n"; + + + + + + return 0; + +} diff --git a/ski/seg_tree/7a44495a-137c-11eb-9c52-309c234925a0.cpp b/ski/seg_tree/7a44495a-137c-11eb-9c52-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..423ac503e27f28503f3afb86053fc52002509668 --- /dev/null +++ b/ski/seg_tree/7a44495a-137c-11eb-9c52-309c234925a0.cpp @@ -0,0 +1,105 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n,i,j,nr,k; + +int v[30005],ct[4*30005],rz[30005]; + + + +void build(int n,int l,int r) + +{ + + if(l==r) + + { + + ct[n]=1; + + return; + + } + + int m=(l+r)/2; + + build(2*n, l, m); + + build(2*n+1,m+1,r); + + + + ct[n]=ct[2*n]+ct[2*n+1]; + +} + + + +void update(int n,int l,int r,int val) + +{ + + if(l==r) + + { + + rz[l]=i; + + ct[n]=0; + + return; + + } + + int m=(l+r)/2; + + if(ct[2*n]>=val) update(2*n,l,m,val); + + else update(2*n+1,m+1,r,val-ct[2*n]); + + + + ct[n]=ct[2*n]+ct[2*n+1]; + +} + + + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>v[i]; + + + + build(1,1,n); + + + + for(i=n;i>=1;i--) + + update(1,1,n,v[i]); + + + + for(i=1;i<=n;i++) + + fout< +using namespace std ; + +const int NR = 30005 ; + +ifstream in ("schi.in") ; + +ofstream out ("schi.out") ; + +vector < int > Tree ( NR * 4 ) ; + +int sol [ NR ] , n , v [ NR ] ; + +void query ( int nod , int st , int dr , int x , int i ) { + + if ( st == dr ) { + + sol [ st ] = i ; + + Tree[ nod ] -- ; + + return ; + + } + + int mid = ( st + dr ) >> 1 ; + + if ( x <= Tree [ nod << 1 ] ) query( nod << 1 , st , mid , x , i ) ; + + else query( nod << 1 | 1 , mid + 1 , dr , x - Tree [ nod << 1 ] , i ) ; + + Tree [ nod ] = Tree [ nod << 1 ] + Tree [ nod << 1 | 1 ] ; + +} + +void update ( int nod , int st , int dr , int pos ) { + + if ( st == dr ) { + + Tree[ nod ] = 1 ; + + return ; + + } + + int mid = ( st + dr ) >> 1 ; + + if ( pos <= mid ) update ( nod << 1 , st , mid , pos ) ; + + else update( nod << 1 | 1 , mid + 1 , dr , pos ) ; + + Tree[ nod ] = Tree[ nod << 1 ] + Tree[ nod << 1 | 1 ] ; + +} + +int main () { + + int i ; + + in >> n ; + + for ( i = 1 ; i <= n ; ++ i ) { + + in >> v [ i ] ; + + update( 1 , 1 , n , i ) ; + + } + + for ( i = n ; i ; i -- ) + + query( 1 , 1 , n , v [ i ] , i ) ; + + for( i =1 ; i <= n ; ++ i ) out << sol [ i ] << '\n' ; + +} diff --git a/ski/seg_tree/7a97a014-137c-11eb-9dfa-309c234925a0.cpp b/ski/seg_tree/7a97a014-137c-11eb-9dfa-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..727b949d63c279ac8eda2b6e91746cf10fd866ec --- /dev/null +++ b/ski/seg_tree/7a97a014-137c-11eb-9dfa-309c234925a0.cpp @@ -0,0 +1,48 @@ +#include + +int arb[100000], v[50000], rsp[50000]; + +int querry(int poz, int x, int put){ + if(poz>=put) + return poz; + if(arb[poz*2]>=x) + return querry(poz*2, x, put); + return querry(poz*2+1, x-arb[poz*2], put); +} + +void update(int poz){ + if(poz!=1){ + arb[poz/2]=arb[poz/2*2]+arb[poz/2*2+1]; + update(poz/2); + } +} + +int main(){ + FILE *fin, *fout; + int n, i, put, j, var; + fin=fopen("schi.in", "r"); + fout=fopen("schi.out", "w"); + fscanf(fin, "%d", &n); + for(i=0;i=0;i--){ + var=querry(1, v[i], put)-put+1; + rsp[var]=i+1; + arb[put+var-1]=0; + update(put+var-1); + } + for(i=1;i<=n;i++) + fprintf(fout, "%d\n", rsp[i]); + fclose(fin); + fclose(fout); + + return 0; +} diff --git a/ski/seg_tree/7ad3bb66-137c-11eb-901c-309c234925a0.cpp b/ski/seg_tree/7ad3bb66-137c-11eb-901c-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e6c096183777a015a971e6b41680cfd9687ca3b --- /dev/null +++ b/ski/seg_tree/7ad3bb66-137c-11eb-901c-309c234925a0.cpp @@ -0,0 +1,76 @@ +#include +using namespace std ; + +const int NR = 30005 ; + +ifstream in ("schi.in") ; + +ofstream out ("schi.out") ; + +vector < int > Tree ( NR * 4 ) ; + +int sol [ NR * 4 ] , n , v [ NR * 4 ] ; + +void query ( int nod , int st , int dr , int x , int i ) { + + if ( st == dr ) { + + sol [ st ] = i ; + + Tree[ nod ] -- ; + + return ; + + } + + int mid = ( st + dr ) >> 1 ; + + if ( x <= Tree [ nod << 1 ] ) query( nod << 1 , st , mid , x , i ) ; + + else query( nod << 1 | 1 , mid + 1 , dr , x - Tree [ nod << 1 ] , i ) ; + + Tree [ nod ] = Tree [ nod << 1 ] + Tree [ nod << 1 | 1 ] ; + +} + +void update ( int nod , int st , int dr , int pos ) { + + if ( st == dr ) { + + Tree[ nod ] = 1 ; + + return ; + + } + + int mid = ( st + dr ) >> 1 ; + + if ( pos <= mid ) update ( nod << 1 , st , mid , pos ) ; + + else update( nod << 1 | 1 , mid + 1 , dr , pos ) ; + + Tree[ nod ] = Tree[ nod << 1 ] + Tree[ nod << 1 | 1 ] ; + +} + +int main () { + + int i ; + + in >> n ; + + for ( i = 1 ; i <= n ; ++ i ) { + + in >> v [ i ] ; + + update( 1 , 1 , n , i ) ; + + } + + for ( i = n ; i ; i -- ) + + query( 1 , 1 , n , v [ i ] , i ) ; + + for( i =1 ; i <= n ; ++ i ) out << sol [ i ] << '\n' ; + +} diff --git a/ski/seg_tree/7ae2cbbe-137c-11eb-aa83-309c234925a0.cpp b/ski/seg_tree/7ae2cbbe-137c-11eb-aa83-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5776d3ba226fe1dc5aa0fb1bf7e1ec08fc28920 --- /dev/null +++ b/ski/seg_tree/7ae2cbbe-137c-11eb-aa83-309c234925a0.cpp @@ -0,0 +1,136 @@ +#include + + +int v[30000]; + + + +int loc[30000]; + + + +int an[65536]; + + + +void update(int st,int dr,int nod,int p) + +{ + + if(st==dr) + + { + + an[p]+=1; + + } + + else + + { + + int mij=st+dr; + + mij/=2; + + if(nod<=mij) + + update(st,mij,nod,p*2); + + else update(mij+1,dr,nod,p*2+1); + + an[p]=an[p*2]+an[p*2+1]; + + } + +} + + + +int get(int st,int dr,int p,int s) + +{ + + if(st==dr) + + return st; + + else + + { + + int mij=st+dr; + + mij/=2; + + int opens=(mij-st+1)-an[p*2]; + + int opend=(dr-mij)-an[p*2+1]; + + if(opens + +using namespace std; + +typedef unsigned long long ull; +typedef signed long long ll; +typedef unsigned int uint; +typedef unsigned short ush; +const int nmax = 30001; + +struct node_t { + short lower, mini, pos; +}; +node_t arbint[nmax * 4 + 66]; +short v[nmax]; + +void build(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].lower = 0; + arbint[nodei].mini = v[lefti]; + arbint[nodei].pos = lefti; + return ; + } + + uint midi = (lefti + righti) / 2; + build(2 * nodei + 1, lefti, midi); + build(2 * nodei + 2, midi + 1, righti); + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } +} + +void update(uint nodei, uint lefti, uint righti) { + if (lefti == righti) { + arbint[nodei].mini = 31000; + return ; + } + + uint midi = (lefti + righti) / 2; + + if (arbint[2 * nodei + 1].mini < arbint[2 * nodei + 2].mini) { + arbint[2 * nodei + 2].lower++; + arbint[2 * nodei + 2].mini--; + update(2 * nodei + 1, lefti, midi); + } else { + update(2 * nodei + 2, midi + 1, righti); + } + + arbint[nodei].mini = arbint[2 * nodei + 1].mini; + arbint[nodei].pos = arbint[2 * nodei + 1].pos; + + if (arbint[nodei].mini >= arbint[2 * nodei + 2].mini) { + arbint[nodei].mini = arbint[2 * nodei + 2].mini; + arbint[nodei].pos = arbint[2 * nodei + 2].pos; + + } + arbint[nodei].mini -= arbint[nodei].lower; +} + +int main() { + freopen("schi.in", "r", stdin); + freopen("schi.out", "w", stdout); + + uint n; + scanf("%u", &n); + for (uint i =0;i + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,v[30004],sol[30004],tree[4*30004]; + +void init (int node,int low,int hi,int pos) + +{ + + if (low==hi) + + { + + tree[node]=1; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=mid) init(nodes,low,mid,pos); + + else init(nodes+1,mid+1,hi,pos); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +void update (int node,int low,int hi,int pos,int val) + +{ + + if (low==hi) + + { + + sol[low]=val; + + tree[node]=0; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=tree[nodes]) update(nodes,low,mid,pos,val); + + else update(nodes+1,mid+1,hi,pos-tree[nodes],val); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +int main() + +{ + + f>>n; + + for (int i=1;i<=n;i++) f>>v[i]; + + for (int i=1;i<=n;i++) init(1,1,n,i); + + for (int i=n;i>=1;i--) update(1,1,n,v[i],i); + + for (int i=1;i<=n;i++) g< + + +using namespace std; + +FILE *f,*g; + +int arbint[4*30001],N,P,v[30002],S[30002],Value,NOD; + +void CreateFirstTree(int Nod,int Left,int Right) + +{ + + int Middle=(Right+Left)/2; + + if(Right==Left) + + { + + arbint[Nod]=1;return; + + } + + if(P<=Middle)CreateFirstTree(Nod*2,Left,Middle); + + else + + CreateFirstTree(Nod*2+1,Middle+1,Right); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + + + +} + +int M; + +void Read() + +{ + + int i; + + M=N; + + for(i=1;i<=N;i++) + + fscanf(f,"%d",&v[i]),P=i,CreateFirstTree(1,1,N); + +} + +void DeletePozX(int Nod,int Left,int Right,int PP) + +{ + + int Middle=(Left+Right)/2; + + if(Left==Right) + + { + + S[Left]=NOD;arbint[Nod]=0; + + return; + + } + + if(PP<=arbint[2*Nod]) + + DeletePozX(2*Nod,Left,Middle,PP); + + else + + DeletePozX(2*Nod+1,Middle+1,Right,PP-arbint[2*Nod]); + + arbint[Nod]=arbint[2*Nod]+arbint[2*Nod+1]; + +} + +void Displaying() + +{ + + int i=1; + + while(M) + + { + + fprintf(g,"%d\n",S[i]);M--;i++; + + } + +} + +int main() + +{ + + f=fopen("schi.in","r");g=fopen("schi.out","w"); + + fscanf(f,"%d",&N); + + Read(); + + while(N) + + { + + NOD=N; + + DeletePozX(1,1,M,v[N]); + + N--; + + } + + Displaying(); + + fclose(f);fclose(g); + + return 0; + +} diff --git a/ski/seg_tree/7cad3c98-137c-11eb-accf-309c234925a0.cpp b/ski/seg_tree/7cad3c98-137c-11eb-accf-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72904a31b997159f8617a4cadff8b6c5f0afc511 --- /dev/null +++ b/ski/seg_tree/7cad3c98-137c-11eb-accf-309c234925a0.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + +const int maxN=1e5+1; + + + +int n; + +int v[maxN]; + + + +int aint[4*maxN]; + +int sol[maxN]; + + + +void update(int nod,int st,int dr,int pos){ + + if(st==dr){ + + aint[nod]=1; + + return; + + } + + + + int mij=(st+dr)/2; + + + + if(pos<=mij){ + + update(2*nod,st,mij,pos); + + } else { + + update(2*nod+1,mij+1,dr,pos); + + } + + + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + + + +int query(int nod,int st,int dr,int k){ + + if(st==dr){ + + return st; + + } + + + + int mij=(st+dr)/2; + + int cntEmpty=(mij-st+1) - aint[2*nod]; + + + + if(cntEmpty >= k){ + + return query(2*nod,st,mij,k); + + } else { + + return query(2*nod+1,mij+1,dr,k-cntEmpty); + + } + +} + + + +int main(){ + + ifstream cin("schi.in"); + + ofstream cout("schi.out"); + + + + cin>>n; + + for(int i=1;i<=n;i++){ + + cin>>v[i]; + + } + + + + for(int i=n;i>=1;i--){ + + int where=query(1,1,n,v[i]); + + + + sol[where]=i; + + update(1,1,n,where); + + } + + + + for(int i=1;i<=n;i++){ + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +int tree[4 * 30001 + 13]; + +int N, M, A[30001], B[30001], pozi, i; + +void Update(int nod, int currL, int currR, int pos, int val) { + + if ( currL == currR ) { + + tree[nod] = val; + + return; + + } + + + + int mid = currL + (currR - currL) / 2; + + if ( pos <= mid ) Update(2 * nod, currL, mid, pos, val); + + else Update(2 * nod + 1, mid + 1, currR, pos, val); + + + + tree[nod] = tree[2 * nod] + tree[2 * nod + 1]; + +} + + + +int Query(int nod, int currL, int currR, int val){ + + if (currL == currR) return currL; + + int mid = currL + (currR - currL) / 2; + + if (val <= tree[2 * nod]) return Query(2 * nod, currL, mid, val); + + else { + + val -= tree[2 * nod]; + + return Query(2 * nod + 1, mid + 1, currR, val); + + } + +} + + + +int main() { + + f >> N; + + for (i = 1; i <= N; ++i) { + + Update(1, 1, N, i, 1); + + f >> A[i]; + + } + + for (i = N; i >= 1; --i) { + + pozi = Query(1, 1, N, A[i]); + + Update(1, 1, N, pozi, 0); + + B[pozi] = i; + + } + + for (i = 1; i <= N; ++i) + + g << B[i] << '\n'; + +} diff --git a/ski/seg_tree/7d72791a-137c-11eb-abb0-309c234925a0.cpp b/ski/seg_tree/7d72791a-137c-11eb-abb0-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f9e9862dc566a1325702875a3af2726d8fa0e1 --- /dev/null +++ b/ski/seg_tree/7d72791a-137c-11eb-abb0-309c234925a0.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int Arb[30001*4+250],n,sol[30001],v[30001]; + + + +void fa(int node,int st,int dr) + +{ + + if(st==dr) + + { + + Arb[node]=1; + + return; + + } + + int mij=(dr+st)/2; + + fa(node*2,st,mij); + + fa(node*2+1,mij+1,dr); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int Cauta(int node,int st,int dr,int loc) + +{ + + if(st==dr) + + { + + return dr; + + } + + int mij=(dr+st)/2; + + if(Arb[node*2]>=loc) + + { + + return Cauta(node*2,st,mij,loc); + + } + + else + + { + + return Cauta(node*2+1,mij+1,dr,loc-Arb[2*node]); + + } + +} + +void Up(int node,int st,int dr,int loc) + +{ + + if(dr==st) + + { + + Arb[node]=0; + + return ; + + } + + int mij=(st+dr)/2; + + if(loc<=mij) + + Up(node*2,st,mij,loc); + + else + + Up(node*2+1,mij+1,dr,loc); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;++i) + + f>>v[i]; + + fa(1,1,n); + + + + for(int i=n;i>=1;--i) + + { + + int vi=Cauta(1,1,n,v[i]); + + sol[vi]=i; + + Up(1,1,n,vi); + + } + + for(int i=1;i<=n;++i) + + g< + + + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int NMAX = 30005; + +int v[NMAX], aint[4 * NMAX]; + + + +void update(int pos, int state, int node, int le, int ri) { + + if(le == ri) { + + aint[node] = state; + + } else { + + int mid = (le + ri) / 2; + + if(pos <= mid) + + update(pos, state, node * 2, le, mid); + + else + + update(pos, state, node * 2 + 1, mid + 1, ri); + + aint[node] = aint[node * 2] + aint[node * 2 + 1]; + + } + +} + + + +int query(int pos, int node, int le, int ri) { + + if(le == ri) + + return le; + + int mid = (le + ri) / 2; + + if(aint[node * 2] >= pos) + + return query(pos, node * 2, le, mid); + + else + + return query(pos - aint[node * 2], node * 2 + 1, mid + 1, ri); + +} + + + +int main() { + + int n; + + in >> n; + + for(int i = 1; i <= n; i ++) { + + in >> v[i]; + + update(i, 1, 1, 1, n); + + } + + vector sol(n + 1, 0); + + for(int i = n; i >= 1; i --) { + + int pos = query(v[i], 1, 1, n); + + sol[pos] = i; + + update(pos, 0, 1, 1, n); + + } + + for(int i = 1; i <= n; i ++) + + out << sol[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/7f1593ae-137c-11eb-88db-309c234925a0.cpp b/ski/seg_tree/7f1593ae-137c-11eb-88db-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2616decdc54792e181ce664a51b31f17f4abaa13 --- /dev/null +++ b/ski/seg_tree/7f1593ae-137c-11eb-88db-309c234925a0.cpp @@ -0,0 +1,135 @@ +#include +#include + + + + + + + + +using namespace std; +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + + + +int arbint[120000], v[30001], locuriFinale[30001]; + + + +void build(int nod, int st, int dr) { + + if (st == dr) { + + arbint[nod] = 1; + + return; + + } + + int mid = st + (dr - st) / 2; + + build(2 * nod, st, mid); + + build(2 * nod + 1, mid + 1, dr); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +void update(int nod, int st, int dr, int poz) { + + if (st == dr) { + + arbint[nod] = 0; + + return; + + } + + int mid = st + (dr - st) / 2; + + + + if (poz <= mid) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, dr, poz); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int queryPozitieFinala(int nod, int st, int dr, int locConcurs) { + + if (st == dr) { + + return st; + + } + + int mid = st + (dr - st) / 2; + + + + if (arbint[2 * nod] >= locConcurs) { + + return queryPozitieFinala(2 * nod, st, mid, locConcurs); + + } + + else { + + return queryPozitieFinala(2 * nod + 1, mid + 1, dr, locConcurs - arbint[2 * nod]); + + } + +} +int main() { + + int nr,locFinal; + + fin >> nr; + + for (int i = 1; i <= nr; i++) + + fin >> v[i]; + + build(1, 1, nr); + + + + for (int i = nr; i >= 1; i--) { + + locFinal = queryPozitieFinala(1, 1, nr, v[i]); + + locuriFinale[locFinal] = i; + + update(1, 1, nr, locFinal); + + } + + + + for (int i = 1; i <= nr; i++) + + fout << locuriFinale[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/86dd7fc4-d59d-4c07-8a55-7baf92c10016.cpp b/ski/seg_tree/86dd7fc4-d59d-4c07-8a55-7baf92c10016.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8f9e9862dc566a1325702875a3af2726d8fa0e1 --- /dev/null +++ b/ski/seg_tree/86dd7fc4-d59d-4c07-8a55-7baf92c10016.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int Arb[30001*4+250],n,sol[30001],v[30001]; + + + +void fa(int node,int st,int dr) + +{ + + if(st==dr) + + { + + Arb[node]=1; + + return; + + } + + int mij=(dr+st)/2; + + fa(node*2,st,mij); + + fa(node*2+1,mij+1,dr); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int Cauta(int node,int st,int dr,int loc) + +{ + + if(st==dr) + + { + + return dr; + + } + + int mij=(dr+st)/2; + + if(Arb[node*2]>=loc) + + { + + return Cauta(node*2,st,mij,loc); + + } + + else + + { + + return Cauta(node*2+1,mij+1,dr,loc-Arb[2*node]); + + } + +} + +void Up(int node,int st,int dr,int loc) + +{ + + if(dr==st) + + { + + Arb[node]=0; + + return ; + + } + + int mij=(st+dr)/2; + + if(loc<=mij) + + Up(node*2,st,mij,loc); + + else + + Up(node*2+1,mij+1,dr,loc); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;++i) + + f>>v[i]; + + fa(1,1,n); + + + + for(int i=n;i>=1;--i) + + { + + int vi=Cauta(1,1,n,v[i]); + + sol[vi]=i; + + Up(1,1,n,vi); + + } + + for(int i=1;i<=n;++i) + + g< + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,v[30004],sol[30004],tree[4*30004]; + +void init (int node,int low,int hi,int pos) + +{ + + if (low==hi) + + { + + tree[node]=1; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=mid) init(nodes,low,mid,pos); + + else init(nodes+1,mid+1,hi,pos); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +void update (int node,int low,int hi,int pos,int val) + +{ + + if (low==hi) + + { + + sol[low]=val; + + tree[node]=0; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=tree[nodes]) update(nodes,low,mid,pos,val); + + else update(nodes+1,mid+1,hi,pos-tree[nodes],val); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +int main() + +{ + + f>>n; + + for (int i=1;i<=n;i++) f>>v[i]; + + for (int i=1;i<=n;i++) init(1,1,n,i); + + for (int i=n;i>=1;i--) update(1,1,n,v[i],i); + + for (int i=1;i<=n;i++) g< + + + +typedef long long ll; + + + +const ll mod=1e9+7; + + + +const int dx[] = {0, 1, 0, -1}; + +const int dy[] = {1, 0, -1, 0}; + +const int nmax=1e7+5; +using namespace std; + + + +ll t[nmax*4]; + +int a[nmax],b[nmax]; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +void build(int v, int tl, int tr) { + + if (tl==tr) { + + t[v]=1; + + } + + else { + + int tm=(tl+tr)/2; + + build(2*v,tl,tm); + + build(2*v+1,tm+1,tr); + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +void update(int v, int tl, int tr, int val, int pos) { + + if (tl==tr) { + + t[v]=0; + + b[tl]=val; + + return; + + } + + else { + + int tm=(tl+tr)/2; + + if (pos<=t[2*v]) { + + update(2*v,tl,tm,val,pos); + + } + + else { + + update(2*v+1,tm+1,tr,val,pos-t[2*v]); + + } + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +int main() { + +ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + + int n; + + f >> n; + + build(1,1,n); + + for (int i=1; i<=n; i++) { + + f >> a[i]; + + } + + for (int i=n; i>=1; i--) { + + update(1,1,n,i,a[i]); + + } + + for (int i=1; i<=n; i++) { + + g << b[i] << '\n'; + + } + +} diff --git a/ski/seg_tree/9ff6ce25-9e61-4c7d-9c60-f7ca06b8b9b1.cpp b/ski/seg_tree/9ff6ce25-9e61-4c7d-9c60-f7ca06b8b9b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d94af9c27f13e72f53759f2a4368c1b5da03a034 --- /dev/null +++ b/ski/seg_tree/9ff6ce25-9e61-4c7d-9c60-f7ca06b8b9b1.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int Nmax = 30005; + +int N,v[Nmax],res[Nmax],A[16*Nmax]; + +int Query(int amo,int st,int dr,int i) + +{ + + if(st==dr) return st; + + int mij=(st+dr)/2; + + if(amo <= A[2*i]) + + return Query(amo,st,mij,2*i); + + else + + { + + amo-=A[2*i]; + + return Query(amo,mij+1,dr,2*i+1); + + } + +} + +void Update(int wh,int val,int st,int dr,int i) + +{ + + A[i]+=val; + + if(st==dr) return; + + int mij=(st+dr)/2; + + if(wh<=mij) Update(wh,val,st,mij,2*i); + + else Update(wh,val,mij+1,dr,2*i+1); + +} + +int main(){ + + in>>N; + + for(int i=1;i<=N;i++) + + { + + in>>v[i]; + + Update(i,1,1,N,1); + + } + + for(int i=N;i>=1;i--) + + { + + int wh=Query(v[i],1,N,1); + + res[wh]=i; + + Update(wh,-1,1,N,1); + + } + + for(int i=1;i<=N;i++) + + out< + + + +typedef long long ll; + + + +const ll mod=1e9+7; + + + +const int dx[] = {0, 1, 0, -1}; + +const int dy[] = {1, 0, -1, 0}; + +const int nmax=1e7+5; +using namespace std; + + + +ll t[nmax*4]; + +int a[nmax],b[nmax]; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +void build(int v, int tl, int tr) { + + if (tl==tr) { + + t[v]=1; + + } + + else { + + int tm=(tl+tr)/2; + + build(2*v,tl,tm); + + build(2*v+1,tm+1,tr); + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +void update(int v, int tl, int tr, int val, int pos) { + + if (tl==tr) { + + t[v]=0; + + b[tl]=val; + + return; + + } + + else { + + int tm=(tl+tr)/2; + + if (pos<=t[2*v]) { + + update(2*v,tl,tm,val,pos); + + } + + else { + + update(2*v+1,tm+1,tr,val,pos-t[2*v]); + + } + + t[v]=t[2*v]+t[2*v+1]; + + } + +} + + + +int main() { + +ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + + int n; + + f >> n; + + build(1,1,n); + + for (int i=1; i<=n; i++) { + + f >> a[i]; + + } + + for (int i=n; i>=1; i--) { + + update(1,1,n,i,a[i]); + + } + + for (int i=1; i<=n; i++) { + + g << b[i] << '\n'; + + } + +} diff --git a/ski/seg_tree/b15bb8f5-4089-4579-aa54-13e2db16e18d.cpp b/ski/seg_tree/b15bb8f5-4089-4579-aa54-13e2db16e18d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..baf861b8c3da8d5a866e6fa243d0440c9f2682dc --- /dev/null +++ b/ski/seg_tree/b15bb8f5-4089-4579-aa54-13e2db16e18d.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + if (st == dr) + + { + + aint[idx] = 1; + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + aint[idx] = aint[fiu_st] + aint[fiu_dr]; + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + if (st == dr) + + { + + aint[idx]--; + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + aint[idx]--; + + return query(fiu_st, st, mij, val); + + } + + else + + { + + aint[idx]--; + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/b5267f07-72f7-4fd4-9342-693f4cb8b936.cpp b/ski/seg_tree/b5267f07-72f7-4fd4-9342-693f4cb8b936.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d8832cb010360a035ac62d66cdcdaef4bbe969 --- /dev/null +++ b/ski/seg_tree/b5267f07-72f7-4fd4-9342-693f4cb8b936.cpp @@ -0,0 +1,113 @@ +#include +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int A[90000],n,x[30000],sol[30000]; + +void creare(int st,int dr,int nod){ + + if(st==dr){ + + A[nod]=1; + + + + } + + else{ + + int mid=(st+dr)/2; + + creare(st,mid,2*nod); + + creare(mid+1,dr,2*nod+1); + + A[nod]=A[2*nod]+A[2*nod+1]; + + + + + + } + + + + + +} + + + +void update(int st, int dr, int nod,int poz,int k){ + + if(st==dr){ + + sol[st]=k; + + A[nod]=0; + + } + + else{ + + int mid=(st + dr)/2; + + if(A[2*nod]>=poz){ + + update(st,mid,2*nod,poz,k); + + } + + else{ + + update(mid+1,dr,2*nod+1,poz-A[2*nod],k); + + } + + A[nod]=(A[2*nod]+A[2*nod+1]); + + } + +} + + + +int main(){ + + fin>>n; + + for(int i=1;i<=n;i++){ + + fin>>x[i]; + + } + + + + creare(1,n,1); + + + + for(int i=n;i>=1;i--){ + + update(1,n,1,x[i],i); + + } + + for(int i=1;i<=n;i++){ + + fout< +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int sol[30001],aint[90000],v[30001]; + +int rez,poz,n; + +void query(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + rez=st; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=aint[2*nod]) + + query(2*nod,st,mid); + + else + + { + + poz-=aint[2*nod]; + + query(2*nod+1,mid+1,dr); + + } + +} + +void update(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=0; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=mid) + + update(2*nod,st,mid); + + else + + update(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +void build(int nod,int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=1; + + return ; + + } + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +int main () + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for(int i=n;i>=1;i--) + + { + + poz=v[i]; + + query(1,1,n); + + sol[rez]=i; + + poz=rez; + + update(1,1,n); + + } + + for(int i=1;i<=n;i++) + + fout< +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int sol[30001],aint[90000],v[30001]; + +int rez,poz,n; + +void query(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + rez=st; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=aint[2*nod]) + + query(2*nod,st,mid); + + else + + { + + poz-=aint[2*nod]; + + query(2*nod+1,mid+1,dr); + + } + +} + +void update(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=0; + + return ; + + } + + int mid=(st+dr)/2; + + if(poz<=mid) + + update(2*nod,st,mid); + + else + + update(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +void build(int nod,int st, int dr) + +{ + + if(st==dr) + + { + + aint[nod]=1; + + return ; + + } + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + +int main () + +{ + + fin>>n; + + for(int i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for(int i=n;i>=1;i--) + + { + + poz=v[i]; + + query(1,1,n); + + sol[rez]=i; + + poz=rez; + + update(1,1,n); + + } + + for(int i=1;i<=n;i++) + + fout< + +using namespace std; + +const int Nmax = 30005; + +int finishes[Nmax]; +int segTree[3 * Nmax]; +ifstream cin ("schi.in"); +ofstream cout ("schi.out"); +void build(int node, int left, int right) { + if (left == right) { + segTree[node] = 1; + return; + } + int mid = (left + right) / 2; + build(2* node, left, mid); + build(2*node+1, mid+1, right); + segTree[node] = segTree[2*node] + segTree[2*node+1]; +} + +void update(int node, int left, int right, int position) { + if (left == right) { + segTree[node] = 0; + return; + } + int mid = (left + right) / 2; + if (position <= mid) { + update(2 * node, left, mid, position); + } else { + update(2 * node + 1, mid + 1, right, position); + } + segTree[node] = segTree[2 * node] + segTree[2 * node + 1]; +} + +int query(int node, int left, int right, int value) { + if (left == right) { + return left; + } + int mid = (left + right) / 2; + if (value <= segTree[2*node]) { + return query(2 * node, left, mid, value); + } else { + return query(2 * node + 1, mid + 1, right, value - segTree[2 * node]); + } +} + +int leaderboard[Nmax]; + +int main() { + int n; + cin >> n; + build(1, 1, n); + for (int i = 1; i <= n; ++i) { + cin >> finishes[i]; + } + for (int i = n; i >= 1; --i) { + int pos = query(1, 1, n, finishes[i]); + leaderboard[pos] = i; + update(1, 1, n, pos); + } + for (int i = 1; i <= n; ++i) { + cout << leaderboard[i] << "\n"; + } + return 0; +} diff --git a/ski/seg_tree/c2c029cd-fd1c-41e5-869a-fd70548dd9f2.cpp b/ski/seg_tree/c2c029cd-fd1c-41e5-869a-fd70548dd9f2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b37b99d1afed02873b9deff1656708550ced1644 --- /dev/null +++ b/ski/seg_tree/c2c029cd-fd1c-41e5-869a-fd70548dd9f2.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + + + +const int NMAX = 30005; + +int v[NMAX], aint[4 * NMAX]; + + + +void update(int pos, int state, int node, int le, int ri) { + + if(le == ri) { + + aint[node] = state; + + } else { + + int mid = (le + ri) / 2; + + if(pos <= mid) + + update(pos, state, node * 2, le, mid); + + else + + update(pos, state, node * 2 + 1, mid + 1, ri); + + aint[node] = aint[node * 2] + aint[node * 2 + 1]; + + } + +} + + + +int query(int pos, int node, int le, int ri) { + + if(le == ri) + + return le; + + int mid = (le + ri) / 2; + + if(aint[node * 2] >= pos) + + return query(pos, node * 2, le, mid); + + else + + return query(pos - aint[node * 2], node * 2 + 1, mid + 1, ri); + +} + + + +int main() { + + int n; + + in >> n; + + for(int i = 1; i <= n; i ++) { + + in >> v[i]; + + update(i, 1, 1, 1, n); + + } + + vector sol(n + 1, 0); + + for(int i = n; i >= 1; i --) { + + int pos = query(v[i], 1, 1, n); + + sol[pos] = i; + + update(pos, 0, 1, 1, n); + + } + + for(int i = 1; i <= n; i ++) + + out << sol[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/c341d9f7-dfec-4b17-b5c3-caeaf438dd08.cpp b/ski/seg_tree/c341d9f7-dfec-4b17-b5c3-caeaf438dd08.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72904a31b997159f8617a4cadff8b6c5f0afc511 --- /dev/null +++ b/ski/seg_tree/c341d9f7-dfec-4b17-b5c3-caeaf438dd08.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + +const int maxN=1e5+1; + + + +int n; + +int v[maxN]; + + + +int aint[4*maxN]; + +int sol[maxN]; + + + +void update(int nod,int st,int dr,int pos){ + + if(st==dr){ + + aint[nod]=1; + + return; + + } + + + + int mij=(st+dr)/2; + + + + if(pos<=mij){ + + update(2*nod,st,mij,pos); + + } else { + + update(2*nod+1,mij+1,dr,pos); + + } + + + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + + + +int query(int nod,int st,int dr,int k){ + + if(st==dr){ + + return st; + + } + + + + int mij=(st+dr)/2; + + int cntEmpty=(mij-st+1) - aint[2*nod]; + + + + if(cntEmpty >= k){ + + return query(2*nod,st,mij,k); + + } else { + + return query(2*nod+1,mij+1,dr,k-cntEmpty); + + } + +} + + + +int main(){ + + ifstream cin("schi.in"); + + ofstream cout("schi.out"); + + + + cin>>n; + + for(int i=1;i<=n;i++){ + + cin>>v[i]; + + } + + + + for(int i=n;i>=1;i--){ + + int where=query(1,1,n,v[i]); + + + + sol[where]=i; + + update(1,1,n,where); + + } + + + + for(int i=1;i<=n;i++){ + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +ifstream fin("schi.in"); ofstream fout("schi.out"); + + + + + + + +int v[300005]; + +int ans[300005]; + +int gr[1200005]; + + + +int n; + + + + + + + +void update(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + gr[nod] = val; + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (pos <= mid) update(nod * 2, st, mid, pos, val); + + else update(nod * 2 + 1, mid + 1, dr, pos, val); + + + + gr[nod] = gr[nod * 2] + gr[nod * 2 + 1]; + +} + + + +void query(int nod, int st, int dr, int pos, int val) { + + if (st == dr) { + + ans[st] = val; + + update(1, 1, n, st, 0); + + return; + + } + + + + int mid = st + dr; + + mid /= 2; + + + + if (gr[nod * 2] >= pos) query(nod * 2, st, mid, pos, val); + + else query(nod * 2 + 1, mid + 1, dr, pos - gr[nod * 2] , val); + + + +} + + + + + +int main() { + + + + fin >> n; + + + + for (int i = 1; i <= n; i++) { + + update(1, 1, n, i, 1); + + fin >> v[i]; + + } + + + + for (int i = n; i >= 1; i--) query(1, 1, n, v[i], i); + + + + for (int i = 1; i <= n; i++) { + + fout << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/cc035ed7-5afe-40a5-8df4-ea67d49196e3.cpp b/ski/seg_tree/cc035ed7-5afe-40a5-8df4-ea67d49196e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..11ac7c39334b519497558425655a2684c91a0af0 --- /dev/null +++ b/ski/seg_tree/cc035ed7-5afe-40a5-8df4-ea67d49196e3.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +ifstream fin("schi.in"); +ofstream fout("schi.out"); + +const int N = 30000; + +int arb[4*N]; +int ord[N + 1], available[N + 1], sol[N + 1]; + +void uniteNodes(int parent, int leftChild, int rightChild){ + arb[parent] = arb[leftChild] + arb[rightChild]; +} + +void build(int node, int left, int right){ + if(left == right){ + arb[node] = available[left] = 1; + return; + } + int mij = (left + right)/2; + build(2*node, left, mij); + build(2*node + 1, mij + 1, right); + uniteNodes(node, 2*node, 2*node + 1); +} + +void update(int node, int left, int right, int index, int val){ + if(left == right){ + sol[left] = val; + available[left] = 0; + arb[node] = 0; + return; + } + int mij = (left + right)/2; + if(index <= arb[2*node]) + update(2*node, left, mij, index, val); + else + update(2*node + 1, mij + 1, right, index - arb[2*node], val); + uniteNodes(node, 2*node, 2*node + 1); +} + +int main() +{ + int n,i; + fin >> n; + for(i=1; i<=n; i++) + fin >> ord[i]; + build(1, 1, n); + for(i=n; i>=1; i--) + update(1, 1, n, ord[i], i); + for(i=1; i<=n; i++) + fout << sol[i] << "\n"; + return 0; +} diff --git a/ski/seg_tree/ce29a5ac-6c13-4da0-96e9-038dbfb08491.cpp b/ski/seg_tree/ce29a5ac-6c13-4da0-96e9-038dbfb08491.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1a5ad327252c4c90f0250bf93fc53e0e6f1de80 --- /dev/null +++ b/ski/seg_tree/ce29a5ac-6c13-4da0-96e9-038dbfb08491.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + +int n, i, a[100010], v[30010], sol[30010]; + + + +void initializeaza(int st, int dr, int nod){ + + if(st==dr){ + + a[nod]=1; + + }else{ + + int mid=(st+dr)/2; + + initializeaza(st, mid, nod*2); + + initializeaza(mid+1, dr, nod*2+1); + + a[nod]=a[nod*2]+a[nod*2+1]; + + } + +} + + + +void gaseste(int st, int dr, int nod, int pozCautata){ + + if(st==dr){ + + a[nod]=0; + + sol[st]=i; + + }else{ + + int mid=(st+dr)/2; + + if(a[2*nod]>=pozCautata){ + + + + gaseste(st, mid, 2*nod, pozCautata); + + }else{ + + + + + + gaseste(mid+1, dr, 2*nod+1, pozCautata-a[2*nod]); + + } + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + + + +int main(){ + + fin>>n; + + for(i=1; i<=n; i++){ + + fin>>v[i]; + + } + + initializeaza(1, n, 1); + + for(i=n; i>0; i--){ + + gaseste(1, n, 1, v[i]); + + } + + for(i=1; i<=n; i++){ + + fout< +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/d1a7d739-e320-4af7-80a4-f129dc458ced.cpp b/ski/seg_tree/d1a7d739-e320-4af7-80a4-f129dc458ced.cpp new file mode 100644 index 0000000000000000000000000000000000000000..932a12f9c6b39dbae12357d614047a416a14badf --- /dev/null +++ b/ski/seg_tree/d1a7d739-e320-4af7-80a4-f129dc458ced.cpp @@ -0,0 +1,160 @@ +#include +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + aint[idx] = dr - st + 1; + + + + if (st == dr) + + { + + + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + aint[idx]--; + + + + if (st == dr) + + { + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + return query(fiu_st, st, mij, val); + + } + + else + + { + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/d2080efb-fa5a-4628-8b94-fd2305526b48.cpp b/ski/seg_tree/d2080efb-fa5a-4628-8b94-fd2305526b48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..16b05b5814197c9c92135e73cc21982d7c80c704 --- /dev/null +++ b/ski/seg_tree/d2080efb-fa5a-4628-8b94-fd2305526b48.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + +ifstream f("schi.in"); + +ofstream g("schi.out"); + +int n,v[30004],sol[30004],tree[4*30004]; + +void init (int node,int low,int hi,int pos) + +{ + + if (low==hi) + + { + + tree[node]=1; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=mid) init(nodes,low,mid,pos); + + else init(nodes+1,mid+1,hi,pos); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +void update (int node,int low,int hi,int pos,int val) + +{ + + if (low==hi) + + { + + sol[low]=val; + + tree[node]=0; + + return; + + } + + int mid=(low+hi)/2,nodes=2*node; + + if (pos<=tree[nodes]) update(nodes,low,mid,pos,val); + + else update(nodes+1,mid+1,hi,pos-tree[nodes],val); + + tree[node]=tree[nodes]+tree[nodes+1]; + +} + +int main() + +{ + + f>>n; + + for (int i=1;i<=n;i++) f>>v[i]; + + for (int i=1;i<=n;i++) init(1,1,n,i); + + for (int i=n;i>=1;i--) update(1,1,n,v[i],i); + + for (int i=1;i<=n;i++) g< +#include + + +using namespace std; + + + +ifstream x("schi.in"); + +ofstream y("schi.out"); + + + +int n,i,nr,pos,v[30002]; + + + +struct elem + +{ + + int s,lazy; + +}a[4*30002]; + + + +void build(int nod, int left, int right) + +{ + + if(left==right) + + { + + a[nod].lazy=1; + + return; + + } + + + + int mij=(left+right)/2; + + + + build(nod*2,left,mij); + + build(nod*2+1,mij+1,right); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void update(int nod, int left, int right, int m, int val) + +{ + + if(left==right) + + { + + a[right].s=val; + + a[nod].lazy=0; + + return; + + } + + + + int mij=(left+right)/2; + + + + if(m<=a[nod*2].lazy) + + update(nod*2,left,mij,m,val); + + else + + update(nod*2+1,mij+1,right,m-a[nod*2].lazy,val); + + + + a[nod].lazy=a[nod*2].lazy+a[nod*2+1].lazy; + +} + + + +void citire() + +{ + + x>>n; + + for(i=1;i<=n;i++) + + x>>v[i]; + + build(1,1,n); + +} + + + +int main() + +{ + + citire(); + + for(i=n;i>=1;i--) + + update(1,1,n,v[i],i); + + for(i=1;i<=n;i++) + + y< +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +int Arb[30001*4+250],n,sol[30001],v[30001]; + + + +void fa(int node,int st,int dr) + +{ + + if(st==dr) + + { + + Arb[node]=1; + + return; + + } + + int mij=(dr+st)/2; + + fa(node*2,st,mij); + + fa(node*2+1,mij+1,dr); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int Cauta(int node,int st,int dr,int loc) + +{ + + if(st==dr) + + { + + return dr; + + } + + int mij=(dr+st)/2; + + if(Arb[node*2]>=loc) + + { + + return Cauta(node*2,st,mij,loc); + + } + + else + + { + + return Cauta(node*2+1,mij+1,dr,loc-Arb[2*node]); + + } + +} + +void Up(int node,int st,int dr,int loc) + +{ + + if(dr==st) + + { + + Arb[node]=0; + + return ; + + } + + int mij=(st+dr)/2; + + if(loc<=mij) + + Up(node*2,st,mij,loc); + + else + + Up(node*2+1,mij+1,dr,loc); + + Arb[node]=Arb[node*2]+Arb[node*2+1]; + +} + + + +int main() + +{ + + f>>n; + + for(int i=1;i<=n;++i) + + f>>v[i]; + + fa(1,1,n); + + + + for(int i=n;i>=1;--i) + + { + + int vi=Cauta(1,1,n,v[i]); + + sol[vi]=i; + + Up(1,1,n,vi); + + } + + for(int i=1;i<=n;++i) + + g< +#include + + + + + + + + +using namespace std; +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + + + +int arbint[120000], v[30001], locuriFinale[30001]; + + + +void build(int nod, int st, int dr) { + + if (st == dr) { + + arbint[nod] = 1; + + return; + + } + + int mid = st + (dr - st) / 2; + + build(2 * nod, st, mid); + + build(2 * nod + 1, mid + 1, dr); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +void update(int nod, int st, int dr, int poz) { + + if (st == dr) { + + arbint[nod] = 0; + + return; + + } + + int mid = st + (dr - st) / 2; + + + + if (poz <= mid) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, dr, poz); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int queryPozitieFinala(int nod, int st, int dr, int locConcurs) { + + if (st == dr) { + + return st; + + } + + int mid = st + (dr - st) / 2; + + + + if (arbint[2 * nod] >= locConcurs) { + + return queryPozitieFinala(2 * nod, st, mid, locConcurs); + + } + + else { + + return queryPozitieFinala(2 * nod + 1, mid + 1, dr, locConcurs - arbint[2 * nod]); + + } + +} +int main() { + + int nr,locFinal; + + fin >> nr; + + for (int i = 1; i <= nr; i++) + + fin >> v[i]; + + build(1, 1, nr); + + + + for (int i = nr; i >= 1; i--) { + + locFinal = queryPozitieFinala(1, 1, nr, v[i]); + + locuriFinale[locFinal] = i; + + update(1, 1, nr, locFinal); + + } + + + + for (int i = 1; i <= nr; i++) + + fout << locuriFinale[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/d6645223-915e-48b1-af90-d2c059279116.cpp b/ski/seg_tree/d6645223-915e-48b1-af90-d2c059279116.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47bbc929567fe5d3d46be0fe09cd1496776951e8 --- /dev/null +++ b/ski/seg_tree/d6645223-915e-48b1-af90-d2c059279116.cpp @@ -0,0 +1,99 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("schi.in"); + +ofstream out ("schi.out"); + + + +const int N=1<<17; + +const int Nmax=30000; + + + +int t[N],v[Nmax],sol[Nmax],nod,poz; + + + +void actualizare (int p, int st, int dr) + +{ + + if (st==dr) + + { + + sol[st]=nod; + + t[p]++; + + return; + + } + + int m=(st+dr)/2; + + if (poz<=(m-st+1-t[2*p])) + + { + + actualizare (p*2,st,m); + + } + + else + + { + + poz-=(m-st+1-t[p*2]); + + actualizare (p*2+1,m+1,dr); + + } + + t[p]++; + +} + +int main() + +{ + + int n; + + in>>n; + + t[1]=1; + + for (int i=1;i<=n;i++) + + in>>v[i]; + + + + for (int i=n;i>0;i--) + + { + + nod=i; + + poz=v[i]; + + actualizare (1,1,n); + + } + + for (int i=1;i<=n;i++) + + out< +using namespace std; + + + +const int NMAX = 30000; + + + +int c_partial[1 + NMAX]; + +int c_final[1 + NMAX]; + + + +int aint[3 * NMAX]; + + + +void build(int idx, int st, int dr) + +{ + + aint[idx] = dr - st + 1; + + + + if (st == dr) + + { + + + + return; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + build(fiu_st, st, mij); + + build(fiu_dr, mij + 1, dr); + + + + + +} + + + +int query(int idx, int st, int dr, int val) + +{ + + aint[idx]--; + + + + if (st == dr) + + { + + return st; + + } + + + + int mij = (st + dr) / 2; + + int fiu_st = idx * 2; + + int fiu_dr = idx * 2 + 1; + + + + if (val <= aint[fiu_st]) + + { + + return query(fiu_st, st, mij, val); + + } + + else + + { + + return query(fiu_dr, mij + 1, dr, val - aint[fiu_st]); + + } + +} + + + +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + + + int n; + + + + in >> n; + + for (int i = 1; i <= n; i++) + + { + + in >> c_partial[i]; + + } + + + + build(1, 1, n); + + + + for (int i = n; i >= 1; i--) + + { + + int poz = query(1, 1, n, c_partial[i]); + + c_final[poz] = i; + + } + + + + for (int i = 1; i <= n; i++) + + { + + out << c_final[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/seg_tree/d75ddd19-7a25-4503-923d-a1f54cdc6dfb.cpp b/ski/seg_tree/d75ddd19-7a25-4503-923d-a1f54cdc6dfb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72904a31b997159f8617a4cadff8b6c5f0afc511 --- /dev/null +++ b/ski/seg_tree/d75ddd19-7a25-4503-923d-a1f54cdc6dfb.cpp @@ -0,0 +1,128 @@ +#include +using namespace std; + + + +const int maxN=1e5+1; + + + +int n; + +int v[maxN]; + + + +int aint[4*maxN]; + +int sol[maxN]; + + + +void update(int nod,int st,int dr,int pos){ + + if(st==dr){ + + aint[nod]=1; + + return; + + } + + + + int mij=(st+dr)/2; + + + + if(pos<=mij){ + + update(2*nod,st,mij,pos); + + } else { + + update(2*nod+1,mij+1,dr,pos); + + } + + + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + + + +int query(int nod,int st,int dr,int k){ + + if(st==dr){ + + return st; + + } + + + + int mij=(st+dr)/2; + + int cntEmpty=(mij-st+1) - aint[2*nod]; + + + + if(cntEmpty >= k){ + + return query(2*nod,st,mij,k); + + } else { + + return query(2*nod+1,mij+1,dr,k-cntEmpty); + + } + +} + + + +int main(){ + + ifstream cin("schi.in"); + + ofstream cout("schi.out"); + + + + cin>>n; + + for(int i=1;i<=n;i++){ + + cin>>v[i]; + + } + + + + for(int i=n;i>=1;i--){ + + int where=query(1,1,n,v[i]); + + + + sol[where]=i; + + update(1,1,n,where); + + } + + + + for(int i=1;i<=n;i++){ + + cout< + + +using namespace std; + +ifstream in("schi.in"); + +ofstream out("schi.out"); + +const int Nmax = 30005; + +int N,v[Nmax],res[Nmax],A[16*Nmax]; + +int Query(int amo,int st,int dr,int i) + +{ + + if(st==dr) return st; + + int mij=(st+dr)/2; + + if(amo <= A[2*i]) + + return Query(amo,st,mij,2*i); + + else + + { + + amo-=A[2*i]; + + return Query(amo,mij+1,dr,2*i+1); + + } + +} + +void Update(int wh,int val,int st,int dr,int i) + +{ + + A[i]+=val; + + if(st==dr) return; + + int mij=(st+dr)/2; + + if(wh<=mij) Update(wh,val,st,mij,2*i); + + else Update(wh,val,mij+1,dr,2*i+1); + +} + +int main(){ + + in>>N; + + for(int i=1;i<=N;i++) + + { + + in>>v[i]; + + Update(i,1,1,N,1); + + } + + for(int i=N;i>=1;i--) + + { + + int wh=Query(v[i],1,N,1); + + res[wh]=i; + + Update(wh,-1,1,N,1); + + } + + for(int i=1;i<=N;i++) + + out< +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +typedef pair pairINT; + +typedef long long ll; + +class SegmentTree{ + + public: + + int n; + + int query(int poz){ + + return qry(1,1,n,poz); + + } + + private: + + int segm[(int)(3e4+4)*4]; + + int qry(int node,int l,int r, int poz){ + + if(l == r){ + + segm[node]=1; + + return l; + + } + + int ans,mid=(l+r)>>1,amm=(mid-l+1)-segm[node<<1]; + + if(amm >= poz) + + ans=qry(node<<1,l,mid,poz); + + else + + ans=qry(node<<1|1,mid+1,r,poz-amm); + + segm[node]=segm[node<<1]+segm[node<<1|1]; + + return ans; + + } + +}; + + + +int n,v[(int)(3e4+4)],sol[(int)(3e4+4)]; + +SegmentTree Arb; + + + +int main(){ + + fin>>n; + + Arb.n=n;; + + for(int i=1;i<=n;++i) + + fin>>v[i]; + + for(int i=n;i>0;--i) + + sol[Arb.query(v[i])]=i; + + for(int i=1;i<=n;++i) + + fout< +#include + + + + + + + + +using namespace std; +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + + + + + +int arbint[120000], v[30001], locuriFinale[30001]; + + + +void build(int nod, int st, int dr) { + + if (st == dr) { + + arbint[nod] = 1; + + return; + + } + + int mid = st + (dr - st) / 2; + + build(2 * nod, st, mid); + + build(2 * nod + 1, mid + 1, dr); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +void update(int nod, int st, int dr, int poz) { + + if (st == dr) { + + arbint[nod] = 0; + + return; + + } + + int mid = st + (dr - st) / 2; + + + + if (poz <= mid) + + update(2 * nod, st, mid, poz); + + else + + update(2 * nod + 1, mid + 1, dr, poz); + + + + arbint[nod] = arbint[2 * nod] + arbint[2 * nod + 1]; + +} + + + +int queryPozitieFinala(int nod, int st, int dr, int locConcurs) { + + if (st == dr) { + + return st; + + } + + int mid = st + (dr - st) / 2; + + + + if (arbint[2 * nod] >= locConcurs) { + + return queryPozitieFinala(2 * nod, st, mid, locConcurs); + + } + + else { + + return queryPozitieFinala(2 * nod + 1, mid + 1, dr, locConcurs - arbint[2 * nod]); + + } + +} +int main() { + + int nr,locFinal; + + fin >> nr; + + for (int i = 1; i <= nr; i++) + + fin >> v[i]; + + build(1, 1, nr); + + + + for (int i = nr; i >= 1; i--) { + + locFinal = queryPozitieFinala(1, 1, nr, v[i]); + + locuriFinale[locFinal] = i; + + update(1, 1, nr, locFinal); + + } + + + + for (int i = 1; i <= nr; i++) + + fout << locuriFinale[i] << "\n"; + + return 0; + +} diff --git a/ski/seg_tree/e20e386d-b528-4242-ac6b-1e53d45f3e5d.cpp b/ski/seg_tree/e20e386d-b528-4242-ac6b-1e53d45f3e5d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0358e4f1c07616133be81c0e6c1b69675170331 --- /dev/null +++ b/ski/seg_tree/e20e386d-b528-4242-ac6b-1e53d45f3e5d.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +int tree[4 * 30001 + 13]; + +int N, M, A[30001], B[30001], pozi, i; + +void Update(int nod, int currL, int currR, int pos, int val) { + + if ( currL == currR ) { + + tree[nod] = val; + + return; + + } + + + + int mid = currL + (currR - currL) / 2; + + if ( pos <= mid ) Update(2 * nod, currL, mid, pos, val); + + else Update(2 * nod + 1, mid + 1, currR, pos, val); + + + + tree[nod] = tree[2 * nod] + tree[2 * nod + 1]; + +} + + + +int Query(int nod, int currL, int currR, int val){ + + if (currL == currR) return currL; + + int mid = currL + (currR - currL) / 2; + + if (val <= tree[2 * nod]) return Query(2 * nod, currL, mid, val); + + else { + + val -= tree[2 * nod]; + + return Query(2 * nod + 1, mid + 1, currR, val); + + } + +} + + + +int main() { + + f >> N; + + for (i = 1; i <= N; ++i) { + + Update(1, 1, N, i, 1); + + f >> A[i]; + + } + + for (i = N; i >= 1; --i) { + + pozi = Query(1, 1, N, A[i]); + + Update(1, 1, N, pozi, 0); + + B[pozi] = i; + + } + + for (i = 1; i <= N; ++i) + + g << B[i] << '\n'; + +} diff --git a/ski/seg_tree/e27d808c-2575-4abd-9e9e-3e93a076f5b3.cpp b/ski/seg_tree/e27d808c-2575-4abd-9e9e-3e93a076f5b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6eb10e516eb5e25b36ca726a46a84bf6d27a3ff2 --- /dev/null +++ b/ski/seg_tree/e27d808c-2575-4abd-9e9e-3e93a076f5b3.cpp @@ -0,0 +1,116 @@ +#include +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/e4bf75c5-9fbc-457a-8721-09bc90aee558.cpp b/ski/seg_tree/e4bf75c5-9fbc-457a-8721-09bc90aee558.cpp new file mode 100644 index 0000000000000000000000000000000000000000..120014cc55191b8a73b9ca0c5dc841155163b0d0 --- /dev/null +++ b/ski/seg_tree/e4bf75c5-9fbc-457a-8721-09bc90aee558.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +template +using UniteFN = T(T, T); + +template +class AINT { + std::array t; + FN& unite_fn; + +private: + void _update(int p, int st, int dr, int poz) { + if (st == dr) { + t[p] = !t[p]; + return; + } + + int m = (st + dr) / 2; + if (poz <= m) { + _update(2 * p, st, m, poz); + } else { + _update(2 * p + 1, m + 1, dr, poz); + } + t[p] = unite_fn(t[2 * p], t[2 * p + 1]); + } + + int _query(int p, int st, int dr, T x) { + if (st == dr) { + return st; + } + + int m = (st + dr) / 2; + if (t[2 * p] >= x) { + return _query(2 * p, st, m, x); + } else { + return _query(2 * p + 1, m + 1, dr, x - t[2 * p]); + } + } + +public: + AINT(FN& unite_fn) : unite_fn(unite_fn) {} + + inline void update(int n, int poz) { + _update(1, 1, n, poz); + } + + inline int query(int n, int x) { + return _query(1, 1, n, x); + } +}; + + + + + + + +class schi_uniter { +public: + int operator()(int a, int b) { + return a + b; + } +}; + +auto schi_unite = [](int a, int b) -> int { + return a + b; +}; +AINT aint(schi_unite); +std::array input; +std::array result; + +int main() { + std::ifstream fin("schi.in"); + std::ofstream fout("schi.out"); + int n; + + fin >> n; + for (int i = 1; i <= n; i++) { + fin >> input[i]; + aint.update(n, i); + } + + for (int i = n; i >= 1; i--) { + int poz = aint.query(n, input[i]); + result[poz] = i; + aint.update(n, poz); + } + + for (int i = 1; i <= n; i++) { + fout << result[i] << '\n'; + } + return 0; +} diff --git a/ski/seg_tree/e4e1ebfb-b8bf-48f6-b1a1-c29fc1ffe65d.cpp b/ski/seg_tree/e4e1ebfb-b8bf-48f6-b1a1-c29fc1ffe65d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf58558165efceb950abb26ecae3ebb67509e99a --- /dev/null +++ b/ski/seg_tree/e4e1ebfb-b8bf-48f6-b1a1-c29fc1ffe65d.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int a[30004],v[4*30004],sum,n,i,sol[30004]; + +void build(int nod,int st,int dr){ + + if(st==dr){ + + v[nod]=1; + + return; + + } + + int mid=(st+dr)/2; + + build(nod*2,st,mid); + + build(nod*2+1,mid+1,dr); + + v[nod]=v[nod*2]+v[nod*2+1]; + +} + +int query(int nod,int st,int dr){ + + + + if(st==dr){ + + v[nod]=0; + + return st; + + } + + int mid=(st+dr)/2; + + v[nod]--; + + if(v[2*nod]>=sum) + + return query(2*nod,st,mid); + + sum-=v[2*nod]; + + return query(2*nod+1,mid+1,dr); + +} + +int main() + +{ + + fin>>n; + + for(i=1;i<=n;i++) + + fin>>a[i]; + + build(1,1,n); + + for(i=n;i>=1;i--){ + + sum=a[i]; + + int poz= query(1,1,n); + + sol[poz]=i; + + } + + for(i=1;i<=n;i++) + + fout< +#include +using namespace std; + +int locuri[30000]; + +int tree[120100]; + +int podium[30000]; + + + +void buildTree(int index, int left, int right) { + + + + if (left == right) { + + tree[index] = 1; + + return; + + } + + int mid = (left + right) / 2; + + buildTree(2 * index + 1, left, mid); + + buildTree(2 * index + 2, mid + 1, right); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} + + + +void query(int index, int left, int right, int loc, int nr_concurent) { + + if (left == right) { + + tree[index] --; + + podium[left] = nr_concurent+1; + + return; + + } + + + + int mid = (left + right) / 2; + + + + if (tree[index * 2 + 1] >= loc) + + query(2 * index + 1, left, mid, loc, nr_concurent); + + else + + query(2 * index + 2, mid+1, right, loc - tree[index *2 +1] , nr_concurent); + + + + tree[index] = tree[2 * index + 1] + tree[2 * index + 2]; + +} +int main() + +{ + + ifstream in("schi.in"); + + ofstream out("schi.out"); + + int n, x; + + in >> n; + + + + for (int i = 0; i < n; i++) { + + in >> x; + + locuri[i] = x; + + } + + + + buildTree(0, 0, n-1); + + + + for (int i = n - 1; i >= 0; i--) { + + query(0, 0, n - 1, locuri[i], i); + + } + + + + for (int i = 0; i < n; i++) { + + out << podium[i] << '\n'; + + } + + + + + +} diff --git a/ski/seg_tree/f8b1847c-5e8a-460a-a14f-7979903badbb.cpp b/ski/seg_tree/f8b1847c-5e8a-460a-a14f-7979903badbb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a36677ac3a63dbf458a8ad6714b31b8a844db264 --- /dev/null +++ b/ski/seg_tree/f8b1847c-5e8a-460a-a14f-7979903badbb.cpp @@ -0,0 +1,80 @@ +#include + + +using namespace std; + +ifstream fin("schi.in"); + +ofstream fout("schi.out"); + +int n,i,v[30010],sol[30010],A[3*30010]; + +void build(int nod,int st,int dr) { + + if (st==dr) + + A[nod]=1; + + else { + + int mid=(st+dr)/2; + + build(2*nod,st,mid); + + build(2*nod+1,mid+1,dr); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +void update(int nod,int st,int dr,int x,int poz) { + + if (st==dr) { + + A[nod]=0; + + sol[st]=x; + + } + + else { + + int mid=(st+dr)/2; + + if (A[2*nod]>=poz) + + update(2*nod,st,mid,x,poz); + + else + + update(2*nod+1,mid+1,dr,x,poz-A[2*nod]); + + A[nod]=A[2*nod]+A[2*nod+1]; + + } + +} + +int main() { + + fin>>n; + + for (i=1;i<=n;i++) + + fin>>v[i]; + + build(1,1,n); + + for (i=n;i>=1;i--) + + update(1,1,n,i,v[i]); + + for (i=1;i<=n;i++) + + fout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/019f19af-d20d-47d2-8250-7583cbd5d640.cpp b/ski/treap/019f19af-d20d-47d2-8250-7583cbd5d640.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/019f19af-d20d-47d2-8250-7583cbd5d640.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/0483c6d9-c047-467f-b256-2aadbc5fe9e4.cpp b/ski/treap/0483c6d9-c047-467f-b256-2aadbc5fe9e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/0483c6d9-c047-467f-b256-2aadbc5fe9e4.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/06f94332-7851-45b8-89d4-6ecc96b6d86c.cpp b/ski/treap/06f94332-7851-45b8-89d4-6ecc96b6d86c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/06f94332-7851-45b8-89d4-6ecc96b6d86c.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/07e15286-e1ea-4de3-91c7-b71b196bd38c.cpp b/ski/treap/07e15286-e1ea-4de3-91c7-b71b196bd38c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/07e15286-e1ea-4de3-91c7-b71b196bd38c.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/0932bd02-f5cd-450c-8162-29369ff8fca5.cpp b/ski/treap/0932bd02-f5cd-450c-8162-29369ff8fca5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/0932bd02-f5cd-450c-8162-29369ff8fca5.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/0dcbdc5e-7bdc-4621-9290-f97579b5549a.cpp b/ski/treap/0dcbdc5e-7bdc-4621-9290-f97579b5549a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/0dcbdc5e-7bdc-4621-9290-f97579b5549a.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/1266793d-12f1-4189-bee4-45f924ba0d9b.cpp b/ski/treap/1266793d-12f1-4189-bee4-45f924ba0d9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/1266793d-12f1-4189-bee4-45f924ba0d9b.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/17622264-c95b-4418-bf08-914a57cb2e54.cpp b/ski/treap/17622264-c95b-4418-bf08-914a57cb2e54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/17622264-c95b-4418-bf08-914a57cb2e54.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/1ef7ca35-39a0-432e-a11b-ce0c89302adf.cpp b/ski/treap/1ef7ca35-39a0-432e-a11b-ce0c89302adf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/1ef7ca35-39a0-432e-a11b-ce0c89302adf.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/214db4ea-57d9-4bc4-8218-1060933dd194.cpp b/ski/treap/214db4ea-57d9-4bc4-8218-1060933dd194.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/214db4ea-57d9-4bc4-8218-1060933dd194.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/229eafeb-ab96-4555-8a96-49b8286289a4.cpp b/ski/treap/229eafeb-ab96-4555-8a96-49b8286289a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/229eafeb-ab96-4555-8a96-49b8286289a4.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/29b2c520-648c-445e-9423-62921a1b3638.cpp b/ski/treap/29b2c520-648c-445e-9423-62921a1b3638.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/29b2c520-648c-445e-9423-62921a1b3638.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/29e8c524-2943-4367-927a-100f8a6e3a3e.cpp b/ski/treap/29e8c524-2943-4367-927a-100f8a6e3a3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/29e8c524-2943-4367-927a-100f8a6e3a3e.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/37cb13ad-29e0-406b-887b-bb4b137b197c.cpp b/ski/treap/37cb13ad-29e0-406b-887b-bb4b137b197c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/37cb13ad-29e0-406b-887b-bb4b137b197c.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/3e0e1f00-54c0-4e03-a134-11a0fdf6427e.cpp b/ski/treap/3e0e1f00-54c0-4e03-a134-11a0fdf6427e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/3e0e1f00-54c0-4e03-a134-11a0fdf6427e.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/420a794e-3342-4dcb-9407-f89f218879f8.cpp b/ski/treap/420a794e-3342-4dcb-9407-f89f218879f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/420a794e-3342-4dcb-9407-f89f218879f8.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/49e4497b-cb0c-4671-a1d9-f6e5070ef441.cpp b/ski/treap/49e4497b-cb0c-4671-a1d9-f6e5070ef441.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/49e4497b-cb0c-4671-a1d9-f6e5070ef441.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/4aab6548-6dbf-4894-a2ae-2813e1cb1592.cpp b/ski/treap/4aab6548-6dbf-4894-a2ae-2813e1cb1592.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/4aab6548-6dbf-4894-a2ae-2813e1cb1592.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/4e560ed5-ed6f-49c1-b255-588fa92e6973.cpp b/ski/treap/4e560ed5-ed6f-49c1-b255-588fa92e6973.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/4e560ed5-ed6f-49c1-b255-588fa92e6973.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/4fbbbcf4-95d0-4b77-85e5-45840aca9a98.cpp b/ski/treap/4fbbbcf4-95d0-4b77-85e5-45840aca9a98.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/4fbbbcf4-95d0-4b77-85e5-45840aca9a98.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/5a75d576-ec21-44a5-98e8-f27b54d3ffdb.cpp b/ski/treap/5a75d576-ec21-44a5-98e8-f27b54d3ffdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/5a75d576-ec21-44a5-98e8-f27b54d3ffdb.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/5cb646ac-3b92-4e78-a889-e210aadab1a9.cpp b/ski/treap/5cb646ac-3b92-4e78-a889-e210aadab1a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/5cb646ac-3b92-4e78-a889-e210aadab1a9.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/5cf1ae27-4838-4326-ae1b-083cc07d9b4a.cpp b/ski/treap/5cf1ae27-4838-4326-ae1b-083cc07d9b4a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/5cf1ae27-4838-4326-ae1b-083cc07d9b4a.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/5d2448cb-b2d8-46e2-a792-e21f5a84ee70.cpp b/ski/treap/5d2448cb-b2d8-46e2-a792-e21f5a84ee70.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/5d2448cb-b2d8-46e2-a792-e21f5a84ee70.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/5f6f82f7-0da6-4852-acd7-2a0240729e0f.cpp b/ski/treap/5f6f82f7-0da6-4852-acd7-2a0240729e0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/5f6f82f7-0da6-4852-acd7-2a0240729e0f.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/5f76360e-9383-44bc-b29a-df9f41e128a2.cpp b/ski/treap/5f76360e-9383-44bc-b29a-df9f41e128a2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/5f76360e-9383-44bc-b29a-df9f41e128a2.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/632fa4bb-0c47-413e-af77-bb56f3124f1a.cpp b/ski/treap/632fa4bb-0c47-413e-af77-bb56f3124f1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/632fa4bb-0c47-413e-af77-bb56f3124f1a.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/66285a00-cc1a-4029-95ee-8c30948febc9.cpp b/ski/treap/66285a00-cc1a-4029-95ee-8c30948febc9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/66285a00-cc1a-4029-95ee-8c30948febc9.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/69139e94-967a-4b43-b8c4-7b03482f8884.cpp b/ski/treap/69139e94-967a-4b43-b8c4-7b03482f8884.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/69139e94-967a-4b43-b8c4-7b03482f8884.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/6ac96d7e-137c-11eb-aa70-309c234925a0.cpp b/ski/treap/6ac96d7e-137c-11eb-aa70-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/6ac96d7e-137c-11eb-aa70-309c234925a0.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/6c9baeb6-0170-440e-a070-426b1b2603c0.cpp b/ski/treap/6c9baeb6-0170-440e-a070-426b1b2603c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/6c9baeb6-0170-440e-a070-426b1b2603c0.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/6df464ae-2923-4669-afde-028cf7f0aa63.cpp b/ski/treap/6df464ae-2923-4669-afde-028cf7f0aa63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/6df464ae-2923-4669-afde-028cf7f0aa63.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/725880de-396e-4e67-ad9c-922b252152d2.cpp b/ski/treap/725880de-396e-4e67-ad9c-922b252152d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/725880de-396e-4e67-ad9c-922b252152d2.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/75e35c0c-137c-11eb-a70e-309c234925a0.cpp b/ski/treap/75e35c0c-137c-11eb-a70e-309c234925a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/75e35c0c-137c-11eb-a70e-309c234925a0.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/83008fa1-6fb7-42bf-bd0a-118f2b5f0b43.cpp b/ski/treap/83008fa1-6fb7-42bf-bd0a-118f2b5f0b43.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/83008fa1-6fb7-42bf-bd0a-118f2b5f0b43.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/88b148a0-2745-4ab3-88a3-a781e09817cc.cpp b/ski/treap/88b148a0-2745-4ab3-88a3-a781e09817cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/88b148a0-2745-4ab3-88a3-a781e09817cc.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/8f8a897c-9f07-4649-8ddc-ad244ccb1406.cpp b/ski/treap/8f8a897c-9f07-4649-8ddc-ad244ccb1406.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/8f8a897c-9f07-4649-8ddc-ad244ccb1406.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/992678ba-ec48-45dd-8d9f-beb545034588.cpp b/ski/treap/992678ba-ec48-45dd-8d9f-beb545034588.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/992678ba-ec48-45dd-8d9f-beb545034588.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/993a60ec-11fe-4594-ad2d-bdc14125781e.cpp b/ski/treap/993a60ec-11fe-4594-ad2d-bdc14125781e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/993a60ec-11fe-4594-ad2d-bdc14125781e.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/9ab0faa4-f685-4159-b9d9-1d42b0b1cc0f.cpp b/ski/treap/9ab0faa4-f685-4159-b9d9-1d42b0b1cc0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/9ab0faa4-f685-4159-b9d9-1d42b0b1cc0f.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/9c784a95-1ab1-4024-abec-4100370f9a33.cpp b/ski/treap/9c784a95-1ab1-4024-abec-4100370f9a33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/9c784a95-1ab1-4024-abec-4100370f9a33.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/a23f87d4-eacf-4611-8a71-1c35591d9bf7.cpp b/ski/treap/a23f87d4-eacf-4611-8a71-1c35591d9bf7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/a23f87d4-eacf-4611-8a71-1c35591d9bf7.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/a669c638-5e3f-45ef-9785-942ca2412cd7.cpp b/ski/treap/a669c638-5e3f-45ef-9785-942ca2412cd7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/a669c638-5e3f-45ef-9785-942ca2412cd7.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/aa7f0887-55a6-485f-8311-d845a2092dd6.cpp b/ski/treap/aa7f0887-55a6-485f-8311-d845a2092dd6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/aa7f0887-55a6-485f-8311-d845a2092dd6.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/ab558dc0-d77e-4299-81f7-5bb2f7ee3938.cpp b/ski/treap/ab558dc0-d77e-4299-81f7-5bb2f7ee3938.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/ab558dc0-d77e-4299-81f7-5bb2f7ee3938.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/b37cdb34-3104-4b22-b1e0-9e4fbc5f6454.cpp b/ski/treap/b37cdb34-3104-4b22-b1e0-9e4fbc5f6454.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/b37cdb34-3104-4b22-b1e0-9e4fbc5f6454.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/b73bdfa3-16d2-4dd5-96ab-751b4ea60419.cpp b/ski/treap/b73bdfa3-16d2-4dd5-96ab-751b4ea60419.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/b73bdfa3-16d2-4dd5-96ab-751b4ea60419.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/c33c28f0-5787-473e-8f70-8f5135b749f6.cpp b/ski/treap/c33c28f0-5787-473e-8f70-8f5135b749f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/c33c28f0-5787-473e-8f70-8f5135b749f6.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/c3443663-f590-420b-bd11-2761a91697db.cpp b/ski/treap/c3443663-f590-420b-bd11-2761a91697db.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/c3443663-f590-420b-bd11-2761a91697db.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/c3653764-3578-4f05-a385-305b9a1d3d0b.cpp b/ski/treap/c3653764-3578-4f05-a385-305b9a1d3d0b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/c3653764-3578-4f05-a385-305b9a1d3d0b.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/c3da8d96-a930-4f2f-9a18-b042fc1652d5.cpp b/ski/treap/c3da8d96-a930-4f2f-9a18-b042fc1652d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/c3da8d96-a930-4f2f-9a18-b042fc1652d5.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/cf863587-b27e-4e92-b6c9-968014162b66.cpp b/ski/treap/cf863587-b27e-4e92-b6c9-968014162b66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/cf863587-b27e-4e92-b6c9-968014162b66.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/cfcc3f5b-bd12-420a-ba7e-3ca7100faff0.cpp b/ski/treap/cfcc3f5b-bd12-420a-ba7e-3ca7100faff0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/cfcc3f5b-bd12-420a-ba7e-3ca7100faff0.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/d034e82a-4e7e-4c83-bfbf-dd4d1ee89c51.cpp b/ski/treap/d034e82a-4e7e-4c83-bfbf-dd4d1ee89c51.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/d034e82a-4e7e-4c83-bfbf-dd4d1ee89c51.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/d1852779-73f1-4454-bac9-024524bf82d3.cpp b/ski/treap/d1852779-73f1-4454-bac9-024524bf82d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/d1852779-73f1-4454-bac9-024524bf82d3.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/d4b9ef9f-cbd3-4fb1-b937-d00075fa61f5.cpp b/ski/treap/d4b9ef9f-cbd3-4fb1-b937-d00075fa61f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/d4b9ef9f-cbd3-4fb1-b937-d00075fa61f5.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/d721cfbe-f5de-46eb-9ea8-5c7092763094.cpp b/ski/treap/d721cfbe-f5de-46eb-9ea8-5c7092763094.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/d721cfbe-f5de-46eb-9ea8-5c7092763094.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/d8fcad07-f28d-4d0f-ac55-af95e7dca8ef.cpp b/ski/treap/d8fcad07-f28d-4d0f-ac55-af95e7dca8ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/d8fcad07-f28d-4d0f-ac55-af95e7dca8ef.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/d980542a-ba64-4c45-b1db-91f4efa46328.cpp b/ski/treap/d980542a-ba64-4c45-b1db-91f4efa46328.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/d980542a-ba64-4c45-b1db-91f4efa46328.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/db9a670d-82ab-4c13-8cdc-a7435bf91abd.cpp b/ski/treap/db9a670d-82ab-4c13-8cdc-a7435bf91abd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/db9a670d-82ab-4c13-8cdc-a7435bf91abd.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/dc05791d-2be5-49e2-b27c-5383c2a8b284.cpp b/ski/treap/dc05791d-2be5-49e2-b27c-5383c2a8b284.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/dc05791d-2be5-49e2-b27c-5383c2a8b284.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/dfe6a459-4c65-4b2b-9873-f48dd04b52d5.cpp b/ski/treap/dfe6a459-4c65-4b2b-9873-f48dd04b52d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/dfe6a459-4c65-4b2b-9873-f48dd04b52d5.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/e5729701-a9ad-438d-af3d-99922b4fcea0.cpp b/ski/treap/e5729701-a9ad-438d-af3d-99922b4fcea0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/e5729701-a9ad-438d-af3d-99922b4fcea0.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/e971bd41-ccce-4fbe-9986-4b2cade2d25b.cpp b/ski/treap/e971bd41-ccce-4fbe-9986-4b2cade2d25b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/e971bd41-ccce-4fbe-9986-4b2cade2d25b.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/eb5fb439-e523-47f2-86f7-a570a25a7805.cpp b/ski/treap/eb5fb439-e523-47f2-86f7-a570a25a7805.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/eb5fb439-e523-47f2-86f7-a570a25a7805.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/ed638979-184b-4b6a-b739-7fdbc9a4a73d.cpp b/ski/treap/ed638979-184b-4b6a-b739-7fdbc9a4a73d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6de30d51ab7ca116a3f6f4e5e535c73135dbe1ed --- /dev/null +++ b/ski/treap/ed638979-184b-4b6a-b739-7fdbc9a4a73d.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream f("schi.in"); + +ofstream g("schi.out"); + + + +namespace Treap{ + + struct Node{ + + Node *l=0,*r=0; + + int x=0; + + int y=0; + + int sz=0; + + Node(int _x=0): x(_x), y(rand()), sz(1) {} + + void recalc(); + + }; + + int cnt(Node* t){ + + if(!t)return 0; + + return (t->sz); + + } + + void Node::recalc(){ + + sz=cnt(l)+cnt(r)+1; + + } + + pair split(Node* t,int k){ + + if(!t)return {}; + + t->recalc(); + + if(cnt(t->l)>=k){ + + auto pa=split(t->l,k); + + t->l=pa.second; + + t->recalc(); + + return {pa.first,t}; + + }else{ + + auto pa=split(t->r,k-cnt(t->l)-1); + + t->r=pa.first; + + t->recalc(); + + return {t,pa.second}; + + } + + return {}; + + } + + Node* join(Node *l,Node *r){ + + if(!l)return r; + + if(!r)return l; + + l->recalc(); + + r->recalc(); + + if(l->y > r->y){ + + l->r=join(l->r,r); + + l->recalc(); + + return l; + + }else{ + + r->l=join(l,r->l); + + r->recalc(); + + return r; + + } + + } + + Node* insert(Node* t,int k,int e){ + + auto pa=split(t,k-1); + + return join(pa.first,join(new Node(e),pa.second)); + + } + + void output(Node *t){ + + if(!t)return ; + + output(t->l); + + g<<(t->x)<<'\n'; + + output(t->r); + + return ; + + } + +} + + + +int main() + +{ + + int n;f>>n; + + srand(time(0)); + + Treap::Node *root=0; + + for(int i=1;i<=n;i++) + + { + + int x;f>>x; + + root=Treap::insert(root,x,i); + + } + + Treap::output(root); + + return 0; + +} diff --git a/ski/treap/f0b2aa3d-de4b-4327-a134-0bd12b930bff.cpp b/ski/treap/f0b2aa3d-de4b-4327-a134-0bd12b930bff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/f0b2aa3d-de4b-4327-a134-0bd12b930bff.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/f1e2b848-b8e1-4c58-918c-02624367f593.cpp b/ski/treap/f1e2b848-b8e1-4c58-918c-02624367f593.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/f1e2b848-b8e1-4c58-918c-02624367f593.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/ski/treap/f42845bf-2ea0-4dc1-a311-a958cd836d47.cpp b/ski/treap/f42845bf-2ea0-4dc1-a311-a958cd836d47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d498fc17addf6f7582739bfad502a7a27c21749 --- /dev/null +++ b/ski/treap/f42845bf-2ea0-4dc1-a311-a958cd836d47.cpp @@ -0,0 +1,263 @@ +#include +#include +using namespace std; + +ifstream cin ("schi.in"); + +ofstream cout ("schi.out"); + +struct treap + +{ + + int value; + + int size; + + long long priority; + + + + treap* left; + + treap* right; + +}; + +treap* emptytreap=new treap + +{ + + 0,0,-1,NULL,NULL + +}; + +bool find (treap* root, int value) + +{ + + if(root==emptytreap) + + return 0; + + else + + if(root->value == value) + + return 1; + + else + + if(root->value < value) + + return find(root->right,value); + + else + + return find(root->left,value); + +} + +treap* recomputetree (treap* root, treap* left, treap* right) + +{ + + root->left=left; + + root->right=right; + + root->size=1+left->size+right->size; + + return root; + +} + +pair split (treap* root, int value) + +{ + + if(root == emptytreap) + + return make_pair(emptytreap,emptytreap); + + if(value < root->value) + + { + + pair p=split(root->left,value); + + return make_pair(p.first,recomputetree(root,p.second,root->right)); + + } + + else + + { + + pairp=split(root->right,value); + + return make_pair(recomputetree(root,root->left,p.first),p.second); + + } + +} + +treap* join (treap* first, treap* second) + +{ + + if(first==emptytreap) + + return second; + + if(second==emptytreap) + + return first; + + if(first->priority > second->priority) + + return recomputetree(first,first->left,join(first->right,second)); + + else + + return recomputetree(second,join(first,second->left),second->right); + +} + +treap* Insert (treap* root, treap* value) + +{ + + if(root == emptytreap) + + return value; + + if(root->prioritypriority) + + { + + pair p=split(root,value->value); + + return recomputetree(value,p.first,p.second); + + } + + if(root->valuevalue) + + return recomputetree(root,root->left,Insert(root->right,value)); + + return recomputetree(root,Insert(root->left,value),root->right); + +} + +void insert (treap* &root, int val) + +{ + + treap* value = new treap + + { + + val,1,(rand()%10000000LL*(rand()%rand())%12346234LL+rand())%1000000000001LL,emptytreap,emptytreap + + }; + + if(!find(root,val)) + + root=Insert(root,value); + +} + +treap* Del (treap* root, int value) + +{ + + if(root==emptytreap) + + return emptytreap; + + if(root->value==value) + + return join(root->left,root->right); + + if(root->valueleft,Del(root->right,value)); + + if(valuevalue) + + return recomputetree(root,Del(root->left,value),root->right); + +} + +void del (treap* & root, int val) + +{ + + if(find(root,val)) + + root=Del(root,val); + +} + +int kth (treap* root, int k) + +{ + + if(root->left->size+1==k) + + return root->value; + + if(root->left->size+1>k) + + return kth(root->left,k); + + if(root->left->size+1right,k-root->left->size-1); + +} + +int v[30003]; + +int rez[30003]; + +int main() + +{ + + treap* mytreap = emptytreap; + + int n,m,i,j; + + cin>>n; + + for(i=1;i<=n;++i) + + { + + cin>>v[i]; + + insert(mytreap,i); + + } + + for(i=n;i>=1;--i) + + { + + int pz=kth(mytreap,v[i]); + + rez[pz]=i; + + del(mytreap,pz); + + } + + for(i=1;i<=n;++i) + + cout< + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/f699d762-a351-4c50-b28d-b33e6fe74055.cpp b/ski/treap/f699d762-a351-4c50-b28d-b33e6fe74055.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9afd6814ba58744dae472737f405e3fef1ec3347 --- /dev/null +++ b/ski/treap/f699d762-a351-4c50-b28d-b33e6fe74055.cpp @@ -0,0 +1,230 @@ +#include + + +using namespace std; + + + +ifstream f ("schi.in"); + +ofstream g ("schi.out"); + + + +constexpr int NMAX = 30005; + + + +struct nod { + + nod *st, *dr; + + int prior; + + int val; + + int sz; + +}; + + + +nod nil; + + + +int ans[NMAX], ask[NMAX]; + + + +nod *mod_fiu (nod *T, int care, nod *fiu) { + + if (care == 0) T->st = fiu; + + else T->dr = fiu; + + + + T->sz = T->st->sz + 1 + T->dr->sz; + + + + return T; + +} + + + +nod *join (nod *st, nod *dr) { + + if (st == &nil) return dr; + + if (dr == &nil) return st; + + + + if (st->prior >= dr->prior) { + + return mod_fiu(st, 1, join(st->dr, dr)); + + } + + else { + + return mod_fiu(dr, 0, join(st, dr->st)); + + } + +} + + + +pair split (nod *T, int K) { + + if (T == &nil) return {&nil, &nil}; + + + + if (T->st->sz >= K) { + + auto t = split(T->st, K); + + + + return {t.first, mod_fiu(T, 0, t.second)}; + + } + + else { + + auto t = split(T->dr, K - T->st->sz - 1); + + + + return {mod_fiu(T, 1, t.first), t.second}; + + } + +} + + + +int Acces (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(join(t_.first, t_.second), t.second); + + + + return t_.second->val; + +} + + + +nod *Adaug (nod *T, int val, int poz) { + + auto t = split(T, poz); + + + + nod *now = new nod; + + now->st = &nil; + + now->dr = &nil; + + now->prior = rand(); + + now->sz = 1; + + now->val = val; + + + + T = join(join(t.first, now), t.second); + + + + return T; + +} + + + +nod *Sterg (nod *T, int poz) { + + auto t = split(T, poz+1); + + auto t_ = split(t.first, poz); + + + + T = join(t_.first, t.second); + + + + return T; + +} + + + +int main () { + + srand(time(NULL)); + + int N; + + + + f >> N; + + + + nod *T = &nil; + + + + for (int i = 1; i <= N; ++ i ) { + + f >> ask[i]; + + + + T = Adaug(T, i, i-1); + + } + + + + for (int i = N; i >= 1; -- i ) { + + int val = Acces(T, ask[i]-1); + + T = Sterg(T, ask[i]-1); + + + + ans[val] = i; + + } + + + + for (int i = 1; i <= N; ++ i ) { + + g << ans[i] << '\n'; + + } + + + + return 0; + +} diff --git a/ski/treap/fa0aa011-29be-4353-abe0-84e57add48f1.cpp b/ski/treap/fa0aa011-29be-4353-abe0-84e57add48f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e123cc1b7e9b3064ccb074309dbfcfbefa5fd42 --- /dev/null +++ b/ski/treap/fa0aa011-29be-4353-abe0-84e57add48f1.cpp @@ -0,0 +1,107 @@ +#include +using namespace std; + +mt19937 rnd(time(0)); + +typedef struct Treap * Arbore; +typedef pair Paa; +Arbore NIL; + +struct Treap +{ + int prio, val, g; + Treap *st, *dr; + + void recalc() + { + g = 1 + st->g + dr->g; + } + Treap(int val) : prio(rnd()), val(val), g(1), st(NIL), dr(NIL) { } + ~Treap() + { + if (st != NIL) + delete st; + if (dr != NIL) + delete dr; + } +}; + +Paa split(Arbore a, int size_st) +{ + if (a == NIL) + return { NIL, NIL }; + + if (a->st->g >= size_st) { + Paa s = split(a->st, size_st); + a->st = s.second; + a->recalc(); + return { s.first, a }; + } + + Paa s = split(a->dr, size_st - a->st->g - 1); + a->dr = s.first; + a->recalc(); + return { a, s.second }; +} + +Arbore join(Arbore a, Arbore b) +{ + if (a == NIL) + return b; + if (b == NIL) + return a; + + if (a->prio > b->prio) { + a->dr = join(a->dr, b); + a->recalc(); + return a; + } + + b->st = join(a, b->st); + b->recalc(); + return b; +} + +Arbore insert(Arbore a, int where, int val) +{ + Paa s = split(a, where); + return join(s.first, join(new Treap(val), s.second)); +} + +void dfs(Arbore a, vector & v) +{ + if (a == NIL) + return; + dfs(a->st, v); + v.push_back(a->val); + dfs(a->dr, v); +} + + +int main() +{ + ifstream in("schi.in"); + ofstream out("schi.out"); + + NIL = new Treap(0); + NIL->g = 0; + + Arbore root = NIL; + + int n, poz; + in >> n; + + for (int i = 1; i <= n; i++) { + int poz; + in >> poz; + root = insert(root, poz - 1, i); + } + + vector ans; + dfs(root, ans); + + for (auto i : ans) + out << i << '\n'; + + return 0; +} diff --git a/strmatch/kmp/02103794-53e6-407d-ab4b-48627741deeb.cpp b/strmatch/kmp/02103794-53e6-407d-ab4b-48627741deeb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..77e402d2e00423c0e8d4d939d24429d9e07b9212 --- /dev/null +++ b/strmatch/kmp/02103794-53e6-407d-ab4b-48627741deeb.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +int const lim = 2000005; + +char a[lim],b[lim]; + +int n,m,k,pi[lim],d[lim],mx; + +vector < int > c; + + + +int main() + +{ + + in>>a+1>>b+1; + + n=strlen(a+1); + + m=strlen(b+1); + + + + pi[1]=0; + + k=0; + + for(int i=2;i<=n;i++) + + { + + while(k>0 && a[i]!=a[k+1]) + + k=pi[k]; + + + + if(a[i]==a[k+1]) + + k++; + + + + pi[i]=k; + + } + + + + k=0; + + for(int i=1;i<=m;i++) + + { + + while(k>0 && b[i]!=a[k+1]) + + k=pi[k]; + + + + if(b[i]==a[k+1]) + + k++; + + + + d[i]=k; + + if(d[i]==n) + + { + + mx++; + + if(c.size()<1000) + + { + + c.push_back(i-n); + + } + + } + + } + + + + out< +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + + + +ofstream fout("strmatch.out"); + + + +char a[2000005]; + + + +char b[2000005]; + + + +int c[20000005]; + + + +queue coada; + + + +int k = 0; + + + +void pattern(char *pat, int M, int *lps) + + + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + int i = 1; + + + + while (i < M) + + + + { + + + + if (pat[i] == pat[len]) + + + + { + + + + len++; + + + + lps[i] = len; + + + + i++; + + + + } + + + + else + + + + { + + + + if(len!=0) + + + + len = lps[len - 1]; + + + + else + + + + { + + + + c[i] = 0; + + + + i++; + + + + } + + + + } + + + + } + + + +} + + + +void KMP(char *pat,char *txt) + + + +{ + + + + int M = strlen(pat); + + + + int N = strlen(txt); + + + + pattern(pat, M, c); + + + + int i = 0; + + + + int j = 0; + + + + while (i < N) + + + + { + + + + if (pat[j] == txt[i]) + + + + { + + + + i++; + + + + j++; + + + + } + + + + if (j == M) + + + + { + + + + if (k < 1000) + + + + { + + + + coada.push(i - j); + + + + + + + + } + + + + k++; + + + + j = c[j - 1]; + + + + } + + + + else + + + + if (i < N && pat[j] != txt[i]) + + + + { + + + + if (j != 0) + + + + j = c[j - 1]; + + + + else + + + + i++; + + + + } + + + + } + + + +} + + + +int main() + + + +{ + + + + fin.getline(a, 2000005); + + + + fin.getline(b, 2000005); + + + + KMP(a, b); + + + + fout << k << "\n"; + + + + while (!coada.empty()) + + + + { + + + + fout << coada.front() << " "; + + + + coada.pop(); + + + + } + + + + return 0; + + + +} diff --git a/strmatch/kmp/05af1be4-11d6-410f-9c53-3a43e6d51598.cpp b/strmatch/kmp/05af1be4-11d6-410f-9c53-3a43e6d51598.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21ea2c711f2041a191fda1c1237b74001eb2b2cf --- /dev/null +++ b/strmatch/kmp/05af1be4-11d6-410f-9c53-3a43e6d51598.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void Citire(); + +void Pref(); + +void KMP(); + + + +char T[2000005],P[2000005]; + +int n,m,k; + +int a[2000005],urm[2000005]; + + + + + +int main() + +{ + + Citire(); + + KMP(); + + fout<1000) k=1000; + + for(int i=1;i<=k;++i) + + fout<0 && P[i]!=P[j]) j=urm[j-1]; + + if(P[i] == P[j]) ++j; + + urm[i]=j; + + } + +} + + + +void KMP() + +{ + + Pref(); + + int q=0; + + for(int i=0;i0 && P[q]!=T[i]) q=urm[q-1]; + + if(P[q]==T[i]) ++q; + + if(q==n) a[++k]=i-n+1; + + } + +} diff --git a/strmatch/kmp/0699ffdc-5181-47fc-903c-0ad333c43155.cpp b/strmatch/kmp/0699ffdc-5181-47fc-903c-0ad333c43155.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e80ab7b91f39502e2b7f8a4f9a63fdc1909a68a --- /dev/null +++ b/strmatch/kmp/0699ffdc-5181-47fc-903c-0ad333c43155.cpp @@ -0,0 +1,81 @@ +#include + +using namespace std; + +ifstream in; +ofstream out; + +string pat,s; +vector ap,sol; + +void pattern() +{ + ap.push_back(0); + int z=1; + while(z>pat>>s; + pattern(); + KMP(); + int k; + if(1000>sol.size()) + k=sol.size(); + else + k=1000; + out< + +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +const int MAX_L = 2000000; + +char P[MAX_L + 5], T[MAX_L + 5]; +int lps[MAX_L + 5]; +int cnt; + +vector ans; + +int main() { + fin >> (P + 1); + P[0] = '\0'; + int sizeP = strlen(P + 1); + + lps[0] = lps[1] = 0; + for (int i = 2; i <= sizeP; i++) { + int x = lps[i - 1]; + + while (x > 0 && P[x + 1] != P[i]) + x = lps[x]; + + if (P[x + 1] == P[i]) + lps[i] = x + 1; + else + lps[i] = 0; + } + + fin >> T; + int sizeT = strlen(T); + + int x = 0; + for (int i = 0; i < sizeT; i++) { + while(x > 0 && P[x + 1] != T[i]) + x = lps[x]; + + if (P[x + 1] == T[i]) + x++; + else + x = 0; + + if (x == sizeP) { + cnt++; + ans.push_back(i - sizeP + 1); + } + } + + fout << cnt << '\n'; + for (int i = 0; i < min(cnt, 1000); i++) + fout << ans[i] << ' '; + + return 0; +} diff --git a/strmatch/kmp/06fde903-8d0c-423d-ad8e-cb6b5fe3d53c.cpp b/strmatch/kmp/06fde903-8d0c-423d-ad8e-cb6b5fe3d53c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0bbc056165038a9f027f046e5acdbd1e6192d0d --- /dev/null +++ b/strmatch/kmp/06fde903-8d0c-423d-ad8e-cb6b5fe3d53c.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +int n,m,i,x,nr,kmp[4000005]; + +char c1[2000005],c2[2000005],v[4000005]; + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + f>>c1; + + f>>c2; + + while(c1[n]) + + { + + n++; + + v[n]=c1[n-1]; + + } + + while(c2[m]) + + { + + m++; + + v[n+m]=c2[m-1]; + + } + + kmp[0]=-1; + + for(i=1; i<=n+m; i++) + + { + + x=kmp[i-1]; + + while(x>=0&&v[x+1]!=v[i]) + + { + + x=kmp[x]; + + } + + x++; + + while(x>n) x=kmp[x]; + + kmp[i]=x; + + if(i>=2*n&&kmp[i]==n) + + { + + nr++; + + } + + } + + g< +#include +#include + + +using namespace std; + + + +int n, m, nro; + +int o[2000005], P[2000005]; + +char a[2000005], b[2000005]; + + + +void pref() + +{ + + int i = 1, j = 0; + + P[0] = -1; + + while(i <= n) + + { + + while(j > 0 && a[i] != a[j]) + + j = P[j-1] + 1; + + P[i] = j; + + ++i, ++j; + + } +} + + + +void kmp() + +{ + + int i = 1, j = 1; + + while(i <= m) + + { + + while(j > 0 && b[i] != a[j]) + + j = P[j-1]+1; + + ++i, ++j; + + if(j == n+1) + + { + + j = P[j-1]+1; + + nro++; + + if(nro <= 1000) + + o[nro] = i-n-1; + + } + + } + +} + + + +void write() + +{ + + printf("%d\n", nro); + + int vmin = min(nro, 1000); + + for(int i = 1; i <= vmin; ++i) + + printf("%d ", o[i]); + +} + + + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n%s", (a+1), (b+1)); + + n = strlen(a+1); + + m = strlen(b+1); + + pref(); + + kmp(); + + write(); + + return 0; + +} diff --git a/strmatch/kmp/0777003b-b46c-4f48-8cf1-4da0ca2a9fe8.cpp b/strmatch/kmp/0777003b-b46c-4f48-8cf1-4da0ca2a9fe8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f65fd5b3021e2b0d5f49e596cd846d9587066cf --- /dev/null +++ b/strmatch/kmp/0777003b-b46c-4f48-8cf1-4da0ca2a9fe8.cpp @@ -0,0 +1,148 @@ +#include +#include +#include +#include + + + +std::ifstream f("strmatch.in"); + +std::ofstream g("strmatch.out"); + + + +void get_lps(const std::string& string, std::vector& lps) + +{ + + unsigned int i = 0, j = 1; + + lps.resize(string.size()); + + lps[0] = 0; + + while (j < string.size()) + + { + + if (string[i] == string[j]) + + { + + lps[j] = i + 1; + + i++; + + j++; + + } + + else + + { + + if (i == 0) + + lps[j++] = 0; + + else + + i = lps[i - 1]; + + } + + } + +} + + + +int kmp(const std::string& string , const std::string& pattern, std::vector& results) + +{ + + int matches = 0; + + unsigned int i = 0, j = 0; + + std::vector lps; + + get_lps(pattern, lps); + + while (i < string.size()) + + { + + if (string[i] == pattern[j]) + + { + + i++; + + j++; + + } + + + + if (j == pattern.size()) + + { + + if(matches < 1000) + + results.push_back(i - j); + + matches++; + + j = j != 0 ? lps[j - 1] : 0; + + } + + else + + if (i < string.size() && string[i] != pattern[j]) + + { + + if (j == 0) + + i++; + + else + + j = lps[j - 1]; + + } + + } + + return matches; + +} + + + + + +int main() + +{ + + std::string string, pattern; + + f >> pattern; + + f >> string; + + std::vector matches; + + g << kmp(string, pattern, matches) << '\n'; + + for (int i : matches) + + g << i << ' '; + + + +} diff --git a/strmatch/kmp/07778a9a-a388-4500-89a8-8e228335fc91.cpp b/strmatch/kmp/07778a9a-a388-4500-89a8-8e228335fc91.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0316c7f3c5c4a31144d5b9b425c8eff65122c4ce --- /dev/null +++ b/strmatch/kmp/07778a9a-a388-4500-89a8-8e228335fc91.cpp @@ -0,0 +1,76 @@ +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n,m,k,i,nr,sol[1010],p[2000010]; + +char s[2000010],c[2000010]; + + + +int main() + +{ + + f>>(c+1);n=strlen(c+1); + + f>>(s+1);m=strlen(s+1); + + + + for(i=2;i<=n;i++) + + { + + while(k!=0 && c[k+1]!=c[i])k=p[k]; + + if(c[k+1]==c[i])k++; + + p[i]=k; + + } + + + + k=0; + + for(i=1;i<=m;i++) + + { + + while(k!=0 && c[k+1]!=s[i])k=p[k]; + + if(c[k+1]==s[i])k++; + + + + if(k==n) + + { + + nr++; + + if(nr<=1000)sol[nr]=i-n; + + } + + } + + + + g< +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a, b; + +int pi[2000005]; + +vector aparitii; + + + +int main() { + + fin >> a >> b; + + int l1 = a.length(), l2 = b.length(); + + int k = -1; + + pi[0] = -1; + + for(int i = 1; i < l1; i++) { + + while(k != -1 && a[k+1] != a[i]) + + k = pi[k]; + + if(a[k+1] == a[i]) + + k++; + + pi[i] = k; + + } + + + + k = -1; + + for(int i = 0; i < l2; i++) { + + while(k != -1 && a[k+1] != b[i]) + + k = pi[k]; + + if(a[k+1] == b[i]) + + k++; + + if(k == l1-1) + + aparitii.push_back(i-k); + + } + + + + fout << aparitii.size() << '\n'; + + for(int i = 0; i < min(int(aparitii.size()), 1000); i++) + + fout << aparitii[i] << ' '; + +} diff --git a/strmatch/kmp/0911adf9-f173-42d0-909e-65e552e0b577.cpp b/strmatch/kmp/0911adf9-f173-42d0-909e-65e552e0b577.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8645d6d8ec90c887d1e1025b9e2225e591e3fb0c --- /dev/null +++ b/strmatch/kmp/0911adf9-f173-42d0-909e-65e552e0b577.cpp @@ -0,0 +1,125 @@ +#include +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + + + +vectorrez; + + + +int n , m; + + + +int pi[2000005]; + + + +char w[2000005]; + +char s[2000005]; + + + +int cazan = 0; + + + +void generate_pi() + +{ + + pi[1] = 0; + + int k = 0; + + for(int i = 2; i <= n; ++i) + + { + + while(k && w[k + 1]!=w[i]) + + k = pi[k]; + + if(w[k + 1] == w[i]) + + k++; + + pi[i] = k; + + } + +} + + + +void kmp() + +{ + + int k = 0; + + for(int i = 1; i <= m; ++i) + + { + + while(k && w[k + 1]!=s[i]) + + k = pi[k]; + + if(w[k + 1] == s[i]) + + k++; + + if(k == n) + + {cazan++; + + if(cazan <= 1000) + + rez.push_back(i - n + 1);} + + + + } + +} + + + +int main() + +{ + + in >> (w + 1); + + in >> (s + 1); + + n = strlen(w + 1); + + m = strlen(s + 1); + + generate_pi(); + + kmp(); + + out << cazan << '\n'; + + for(auto &i : rez) + + out << i - 1 << " "; + + return 0; + +} diff --git a/strmatch/kmp/09259b49-e1f5-4c51-a2b4-8698781f092d.cpp b/strmatch/kmp/09259b49-e1f5-4c51-a2b4-8698781f092d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e90601b85a49c30242925a6aa374fa749602a2 --- /dev/null +++ b/strmatch/kmp/09259b49-e1f5-4c51-a2b4-8698781f092d.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + + + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX=2e6+5; + +char a[NMAX],b[NMAX]; + +int p[NMAX],poz[1005],k,n,m; + + + +int main() + +{ + + + + fin >> a >> b; + + n=strlen(a); m=strlen(b); + + + + int q=0; + + for(int i=1;i + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 2000005; + + + +int lps[nmax]; + + + +int c; + + + +string t, p; + + + +void CreateLPS() + +{ + + int k = 1, len = 0, s = p.length(); + + + + while (k < s) + + { + + if (p[k] == p[len]) + + lps[k++] = ++len; + + else + + { + + if (len) + + len = lps[len - 1]; + + else + + k++; + + } + + } + +} + + + +int main() + +{ + + fout << " \n"; + + fin >> p >> t; + + + + CreateLPS(); + + + + int i = 0, j = 0, s = t.length(); + + + + while (i < s) + + { + + if (t[i] == p[j]) + + { + + i++; + + j++; + + } + + else + + { + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + + + if (j == p.length()) + + { + + if (c < 1000) + + fout << i - j << ' '; + + + + c++; + + + + j = lps[j - 1]; + + } + + } + + + + fout.close(); + + fout.open("strmatch.out", ios::in|ios::out); + + + + fout << c; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/0a0ddbb1-8dba-463d-96da-90129ae93968.cpp b/strmatch/kmp/0a0ddbb1-8dba-463d-96da-90129ae93968.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6b4b4f03eb97787c47ca91cdb231c42818ffbe2 --- /dev/null +++ b/strmatch/kmp/0a0ddbb1-8dba-463d-96da-90129ae93968.cpp @@ -0,0 +1,144 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string cuv,sir; + +int sol[1001],nrS; + + + +int pref[2000001]; + + + +void addSol(int i) + +{ + + nrS++; + + + + if(nrS<=1000) + + sol[nrS]=i-cuv.size()+1; + +} + + + +void prefix() + +{ + + int lung=0; + + + + for(int i=2;i>cuv>>sir; + + cuv.insert(cuv.begin(),' '); + + sir.insert(sir.begin(),' '); + + + + KMP(); + + + + out< +#include +using namespace std; + +ifstream fin( "strmatch.in" ); + +ofstream fout( "strmatch.out" ); + +char caut[ 2000005 ], b[ 2000005 ]; + + + +void lps( int v[], char b[] ){ + + v[ 0 ] = 0; + + int i = 1, n = strlen( b ), len = 0; + + while( i < n ) + + if( b[ i ] == b[ len ] ){ + + len++; + + v[ i++ ] = len; + + } else if( len != 0 ) + + len = v[ len - 1 ]; + + else + + v[ i++ ] = 0; + + + +} + + + +void KMP( char a[], char b[] ){ + + int i = 0, j = 0, n = strlen( a ), m = strlen( b ); + + int v[ m ], cnt = 0, val[ 1000 ]; + + lps( v, b ); + + while( i < n ){ + + if( a[ i ] == b[ j ] ){ + + i++; + + j++; + + if( j == m ){ + + if( cnt < 1000 ) + + val[ cnt ] = i - j; + + cnt++; + + j = v[ j - 1 ]; + + } + + } else if( ( i < n ) && ( b[ j ] != a[ i ] ) ) + + if( j > 0 ) + + j = v[ j - 1 ]; + + else i++; + + } + + fout << cnt << "\n"; + + if( cnt > 1000 ) + + cnt = 1000; + + for( int i = 0; i < cnt; ++i ) + + fout << val[ i ] << ' '; + +} + + + +int main() + +{ + + fin >> b; + + fin >> caut; + + KMP( caut, b ); + + return 0; + +} diff --git a/strmatch/kmp/0ce57a23-243a-4d12-9b9a-f0615afb9b2a.cpp b/strmatch/kmp/0ce57a23-243a-4d12-9b9a-f0615afb9b2a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..54d1344938d0c65536b1235d8845b3e28948c79b --- /dev/null +++ b/strmatch/kmp/0ce57a23-243a-4d12-9b9a-f0615afb9b2a.cpp @@ -0,0 +1,103 @@ +#include + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); +const int Nmax = 2000005; + +const short L = 1000; + + + + + +char s1[Nmax] , s2[Nmax]; + + + + + +vector < int > ans; + + + +int n , m , pref[Nmax] , dim , k; + + + +int main() + +{ + + fin >> (s1 + 1); + + n = strlen(s1 + 1); + + fin >> (s2 + 1); + + m = strlen(s2 + 1); + + for(int i = 2 ; i <= n ; i++) + + { + + while(k > 0 && s1[k + 1] != s1[i]) + + k = pref[k]; + + if(s1[k + 1] == s1[i]) + + k++; + + pref[i] = k; + + } + + k = 0; + + for(int i = 1 ; i <= m ; i++) + + { + + while(k > 0 && s1[k + 1] != s2[i]) + + k = pref[k]; + + if(s1[k + 1] == s2[i]) + + ++k; + + if(k == n) + + { + + ++dim; + + if(dim <= L) + + ans.push_back(i - n); + + } + + } + + fout << dim << "\n"; + + for(auto &it : ans) + + fout << it << " "; + + fout << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/0d04e764-bae7-4822-b081-259102b25779.cpp b/strmatch/kmp/0d04e764-bae7-4822-b081-259102b25779.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e1190ae305006f94a7377c624e515e711a36841 --- /dev/null +++ b/strmatch/kmp/0d04e764-bae7-4822-b081-259102b25779.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + + +using namespace std; + + + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + char A[2000000], ch; + + int N, A_index = -1, B_index = -1, CONTOR = 0; + + vector prefix, SOL; + + + + f.getline(A, 2000001); + + N = strlen(A); + + + + prefix.push_back(-1); + + int j = -1; + + for(int i = 1; i < N; i++) + + { + + while(j > -1 && A[j + 1] != A[i]) + + j = prefix[j]; + + if(A[j + 1] == A[i]) + + j++; + + prefix.push_back(j); + + } + + + + while(f >> ch) + + { + + B_index++; + + while(A_index > -1 && A[A_index + 1] != ch) + + A_index = prefix[A_index]; + + if(A[A_index + 1] == ch) + + A_index++; + + if(A_index == N - 1) + + { + + if(CONTOR < 1000) + + SOL.push_back(B_index - N + 1); + + CONTOR++; + + A_index = prefix[A_index]; + + } + + } + + + + g << CONTOR << '\n'; + + if(CONTOR > 1000) + + CONTOR = 1000; + + for(int i = 0; i < CONTOR; i++) + + g << SOL[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/0dcd522b-d3fe-4f46-8b0e-91be7ffca40a.cpp b/strmatch/kmp/0dcd522b-d3fe-4f46-8b0e-91be7ffca40a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7a67c16cd452224f14807fe552c198fd5eb3af --- /dev/null +++ b/strmatch/kmp/0dcd522b-d3fe-4f46-8b0e-91be7ffca40a.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char A[2000005], B[2000005]; + +int lps[2000005]; + +vector sol; + + + +void preprocessing(char p[]){ + + int i=1, len=0, lsup = strlen(p); + + lps[0] = 0; + + while(i < lsup){ + + while(p[i] != p[len] && len > 0) + + len = lps[len-1]; + + if(p[i] == p[len]){ + + len++; + + lps[i] = len; + + } + + else + + lps[i] = 0; + + i++; + + } + +} + + + +void KMP(char pattern[], char s[]){ + + int ip=0, is=0, target = strlen(pattern), lsup = strlen(s); + + int contor = 0; + + while(is < lsup) + + { + + while(pattern[ip] != s[is] && ip > 0) + + ip = lps[ip-1]; + + if(pattern[ip] == s[is]) + + ip++; + + if(ip == target){ + + contor++; + + if(contor <= 1000) + + sol.push_back(is-ip+1); + + ip = lps[ip-1]; + + } + + is++; + + } + + g< v){ + + int l = v.size(); + + for(int i=0; i>A>>B; + + preprocessing(A); + + KMP(A,B); + + afisare(sol); + +} diff --git a/strmatch/kmp/0e02cccf-0c6e-4286-af96-8da84936c7bc.cpp b/strmatch/kmp/0e02cccf-0c6e-4286-af96-8da84936c7bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..109e7ea0bd48b5e520bcaf8e8d4f770724eba5c3 --- /dev/null +++ b/strmatch/kmp/0e02cccf-0c6e-4286-af96-8da84936c7bc.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int dp[2000010],dpp[2000010],N,M,K,ans,S[1001]; + +char A[2000010],B[2000010]; + + + +int main() + +{ + + f>>A>>B; + + N=strlen(A); M=strlen(B); + + + + for(int i=N-1;i>=0;i--) A[i+1]=A[i]; + + for(int i=M-1;i>=1;i--) B[i+1]=B[i]; + + + + dp[1]=0; + + for(int i=2;i<=N;i++) + + { + + K=dp[i-1]; + + while( K > 0 && A[i] != A[ K + 1 ] ) K=dp[K]; + + + + if( A[i] == A[ K + 1 ] ) dp[i]=K+1; + + } + + + + dpp[0]=0; + + for(int i=1;i<=M;i++) + + { + + K=dpp[i-1]; + + while( K > 0 && B[i] != A[ K + 1 ] ) K=dp[K]; + + if( B[i] == A[ K + 1 ] ) dpp[i]=K+1; + + + + if( dpp[i] == N ) + + { + + ans++; + + if(ans<=1000) S[ans]=i-N; + + } + + } + + g< + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +char a[2000005], c; + +int rez, m, kmp[2000005]; + +void build(char a[], int kmp[]) + +{ + + int i = 0, j = 1; + + kmp[0] = 0; + + while(j < m) + + { + + if(a[i] == a[j]) + + { + + kmp[j] = i+1; + + i++, j++; + + } + + else if(i) + + i = kmp[i-1]; + + else j++; + + } + +} + +vector sol; + +int main() + +{ + + fin >> a; + + m = strlen(a); + + build(a, kmp); + + int i = 0; + + for(int j=0; fin >> c; j++) + + { + + if(c == a[i]) + + { + + if(i == m-1) + + { + + if(rez <= 1000) + + sol.push_back(j-m+1); + + rez++; + + i = kmp[i]; + + } + + else + + i++; + + } + + else + + { + + while(i!=0 && c!=a[i]) + + i = kmp[i-1]; + + if(c == a[i]) + + i++; + + } + + } + + fout << rez << '\n'; + + for(int i=0; i + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX=2000005; + + + +string S,P; + +int lps[NMAX]; + +vector poz; + + + +void KMP(){ + + + + for(int i=1; i0 and S[i]!=P[q];) + q=lps[q-1]; + + if(S[i]==P[q]) ++q; + + if(q==P.size()) poz.push_back(i-q+1); + + } + +} + + + +int main(){ + + fin>>P>>S; + KMP(); + + + fout< +#include + + +using namespace std; + + + +char a[2000005], b[2000005]; + +int pi[2000005]; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + int n, m; + + int matches[1005]; + + + + fgets(a+1, sizeof(a), stdin); + + fgets(b+1, sizeof(b), stdin); + + + + n = strlen(a+1); + + if (a[n] == '\n') { + + a[n] = 0; + + --n; + + } + + + + m = strlen(b+1); + + if (b[m] == '\n') { + + b[m] = 0; + + --m; + + } + + + + pi[1] = pi[0] = 0; + + int k=0; + + + + for(int i=2; i<=n; ++i) { + + while(k > 0 && a[k+1] != a[i]) + + k = pi[k]; + + if (a[k+1] == a[i]) + + ++k; + + pi[i] = k; + + } + + + + int q=0, matchno = 0; + + for(int i=1; i<=m; ++i) { + + while (q>0 && a[q+1] != b[i]) + + q = pi[q]; + + if (a[q+1] == b[i]) + + ++q; + + if (q == n) { + + if (matchno < 1000) + + matches[matchno] = i - n; + + ++matchno; + + q = pi[n]; + + } + + } + + + + printf("%d\n", matchno); + + for(int i=0; i +using namespace std; +ifstream fin("strmatch.in"); ofstream fout("strmatch.out"); + + + + + + + +long long n ,m; + +string a, b; + +long long h1=0, h2=0, hb=0; + +long long ord[300]; + +vector pos; + + + + + +int32_t main(){ + +ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +getline(fin, b); + +getline(fin, a); + +n=a.length(); m=b.length(); + +if(n +#include +#include +#include + + +using namespace std; + + + +int solCnt; + +int n, m; + +int pos[1005], lps[2000005]; + +char pat[2000005], txt[2000005]; + + + + + + + +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + + + int i = 1; + + while (i < M) { + + if (pat[i] == pat[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + + + + + + + if (len != 0) { + + len = lps[len - 1]; + + + + + + + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +void KMPSearch(char* pat, char* txt) + +{ + + m = strlen(pat); + + n = strlen(txt); + + + + + + computeLPSArray(pat, m, lps); + + + + int i = 0; + + int j = 0; + + while (i < n) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + } + + + + if (j == m) { + + if (solCnt < 1000) + + pos[solCnt++] = i - j; + + else + + solCnt++; + + j = lps[j - 1]; + + } + + + + + + else if (i < n && pat[j] != txt[i]) { + + + + + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + + } + +} + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", pat); + + scanf("%s", txt); + + + + KMPSearch(pat, txt); + + + + printf("%d\n", solCnt); + + for (int i = 0; i < std::min(solCnt, 1000); ++i) + + printf("%d ", pos[i]); + + return 0; + +} diff --git a/strmatch/kmp/11fdbce3-8116-4df6-94fd-39ce9ed9bb6f.cpp b/strmatch/kmp/11fdbce3-8116-4df6-94fd-39ce9ed9bb6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f7636a33012bf152453a8f127288b028942cd4dc --- /dev/null +++ b/strmatch/kmp/11fdbce3-8116-4df6-94fd-39ce9ed9bb6f.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int la,lb,t[2000004],i,lun,nrap; + +char a[2000004],b[2000004]; + +vector sol; + +int main() + +{ + + fin>>(a+1)>>(b+1); + + la=strlen(a+1); + + lb=strlen(b+1); + + t[1]=0; + + for(i=2;i<=la;i++){ + + while(lun!=0&&a[lun+1]!=a[i]) + + lun=t[lun]; + + if(a[lun+1]==a[i]) + + lun++; + + t[i]=lun; + + } + + lun=0; + + for(i=1;i<=lb;i++){ + + while(lun!=0&&a[lun+1]!=b[i]) + + lun=t[lun]; + + if(a[lun+1]==b[i]) + + lun++; + + if(lun==la){ + + nrap++; + + if(nrap<=1000){ + + sol.push_back(i-la); + + } + + lun=t[lun]; + + } + + } + + fout< + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +const int NMAX=2000004; + +const int P=73; + +const int mod1=100007; + +const int mod2=100021; + +char a[NMAX],b[NMAX]; + +long long amod1,bmod1,amod2,bmod2,powm1=1,powm2=1,cnt,rasp[NMAX],la,lb; + +int main() + +{ + + in>>a>>b; + + la=strlen(a); + + lb=strlen(b); + + if(la>lb){ + + out<<0; + + return 0; + + } + + for(int i=0;i +#include +#include +using namespace std; + +const int NMAX = 2000000; + +const int MMAX = 2000000; + +int urm[MMAX + 5]; + +char t[NMAX + 5] , p[MMAX + 5]; + +vector ans; + +int main() + +{ + + freopen("strmatch.in" , "r" , stdin); + + freopen("strmatch.out" , "w" , stdout); + + int n , m , i , k , nr; + + scanf("%s\n%s" , (p + 1) , (t + 1)); + + n = strlen(t + 1); + + m = strlen(p + 1); + + k = 0; + + urm[1] = 0; + + for(i = 2 ; i <= m ; i ++) + + { + + while(k > 0 && p[k + 1] != p[i]) + + k = urm[k]; + + if(p[k + 1] == p[i]) + + k ++; + + urm[i] = k; + + } + + k = nr = 0; + + for(i = 1 ; i <= n ; i ++) + + { + + while(k > 0 && p[k + 1] != t[i]) + + k = urm[k]; + + if(p[k + 1] == t[i]) + + k ++; + + if(k == m) + + { + + nr ++; + + if(ans.size() < 1000) + + ans.push_back(i - m); + + k = urm[k]; + + } + + } + + printf("%d\n" , nr); + + for(i = 0 ; i < ans.size() ; i ++) + + printf("%d " , ans[i]); + + return 0; + +} diff --git a/strmatch/kmp/124ead7f-18e1-4057-b1ff-cd268aca1a6c.cpp b/strmatch/kmp/124ead7f-18e1-4057-b1ff-cd268aca1a6c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5dca2a17c434b844560995d7da87dff79df41c3 --- /dev/null +++ b/strmatch/kmp/124ead7f-18e1-4057-b1ff-cd268aca1a6c.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include + + +using namespace std; + + + +int main() { + + string minta, str; + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + getline(fin, minta); + + getline(fin, str); + + int minta_hossz = minta.size(); + + int str_hossz = str.size(); + + int db = 0; + + vector a; + int* prefix; + + prefix = new int[minta_hossz + 1]; + + + + prefix[1] = 0; + + int k = 0; + + for (int i = 1; i < minta_hossz; i++) { + + while (k > 0 && minta[k] != minta[i]) + + k = prefix[k]; + + + + if (minta[k] == minta[i]) + + k++; + + + + prefix[i + 1] = k; + + } + + k = 0; + + db = 0; + + for (int i = 0; i < str_hossz; i++) { + + while (k > 0 && minta[k] != str[i]) + + k = prefix[k]; + + if (minta[k] == str[i]) + + k++; + + if (k == minta_hossz) { + + a.push_back(i - minta_hossz + 1); + + db++; + + } + + } + + fout << db << "\n"; + + if (db > 1000) { + + for (int i = 0; i <= 999; i++) { + + fout << a[i] << " "; + + } + + } + + else { + + for (auto& e : a) { + + fout << e << " "; + + } + + } + +} diff --git a/strmatch/kmp/1474d3e8-a68c-4851-90e8-393720e9baf3.cpp b/strmatch/kmp/1474d3e8-a68c-4851-90e8-393720e9baf3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..641f27d33818007aba0e6b33c4d4fce6debe3224 --- /dev/null +++ b/strmatch/kmp/1474d3e8-a68c-4851-90e8-393720e9baf3.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +vector kmp(string s) { + + int n = (int) s.size(), j = 0; + + vector sol(n); + + for (int i = 1; i < n; i++) { + + while (j && s[i] != s[j]) { + + j = sol[j - 1]; + + } + + if (s[i] != s[j]) { + + sol[i] = 0; + + } else { + + sol[i] = ++j; + + } + + } + + return sol; + +} + + + +int main() { + + string s, t; + + cin >> s >> t; + + vector a = kmp(s + "." + t), b; + + int n = (int) s.size(); + + for (int i = n + 1; i < (int) a.size(); i++) { + + if (a[i] == n) { + + b.push_back(i - 2 * n); + + } + + } + + cout << (int) b.size() << "\n"; + + if ((int) b.size() > 1000) { + + b.resize(1000); + + } + + for (auto &i : b) { + + cout << i << " "; + + } + + cout << "\n"; + +} diff --git a/strmatch/kmp/1490654a-fd1e-4591-ba26-f8146b081227.cpp b/strmatch/kmp/1490654a-fd1e-4591-ba26-f8146b081227.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a33fc021b398f1e34bc68b0c9818ca10a6776837 --- /dev/null +++ b/strmatch/kmp/1490654a-fd1e-4591-ba26-f8146b081227.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + +int pi[2000005],res[1005]; + +stack s; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int l=0,n,m,cnt=0,r=0,c2=0,i; + + string a,b; + + cin >> a; + + cin >> b; + + n=a.size(); + + m=b.size(); + + for(int i=1;i0 && a[l-1]!=a[i]) + + l=pi[l-1]; + + pi[i]=l; + + } + + l=0; + + i=0; + + while (i < m) + + { + + if (a[l] == b[i]) + + { + + l++; + + i++; + + } + + if (l == n) + + { + + if(cnt<1000) + + { + + s.push(i-l); + + cnt++; + + } + + r++; + + l = pi[l - 1]; + + } + + else + + { + + if (a[l] != b[i]) + + { + + if (l != 0) + + l = pi[l - 1]; + + else + + i = i + 1; + + } + + } + + } + + cout << r << '\n'; + + while(s.size()>0) + + { + + res[c2]=s.top(); + + c2++; + + s.pop(); + + } + + for(int i=c2-1;i>=0;i--) + + cout << res[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/152dcc88-d94d-4129-9a37-57aaadac70a5.cpp b/strmatch/kmp/152dcc88-d94d-4129-9a37-57aaadac70a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..093f14a4528548f4b0d395a836e150ae874b5be5 --- /dev/null +++ b/strmatch/kmp/152dcc88-d94d-4129-9a37-57aaadac70a5.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +using namespace std; +string pattern, text; + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + cin.tie(NULL); + ios_base::sync_with_stdio(false); + + cin >> pattern >> text; + int N = pattern.size(); + string newString = pattern + "#" + text; + int M = (int) newString.size(); + + vector pi(N, 0); + vector sol; + sol.reserve(1000); + int totalSolutions = 0, len = 0; + for (int idx = 1; idx < M; idx++) { + while (len > 0 && newString[idx] != newString[len]) { + len = pi[len - 1]; + } + if (newString[len] == newString[idx]) { + len++; + } + + if (idx < N) { + pi[idx] = len; + } + if (len == N) { + totalSolutions++; + if (totalSolutions <= 1000) { + sol.push_back(idx - 2 * N); + } + } + } + + cout << totalSolutions << "\n"; + for (auto idx : sol) { + cout << idx << " "; + } + cout << "\n"; + return 0; +} diff --git a/strmatch/kmp/15d6618d-81c4-4ca4-9515-5e37835551de.cpp b/strmatch/kmp/15d6618d-81c4-4ca4-9515-5e37835551de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecd7c7770ca51045a13719775ee7a8e083da60a3 --- /dev/null +++ b/strmatch/kmp/15d6618d-81c4-4ca4-9515-5e37835551de.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in") ; + +ofstream fout("strmatch.out") ; + + + +vector prefix(string sir) + +{ + + int n , i , j ; + + n = sir.size() ; + + vector pr(n) ; + + pr[0] = 0 ; + + for ( i = 1 ; i < n ; i++ ) + + { + + j = pr[i-1] ; + + while ( j && sir[i] != sir[j] ) + + j = pr[j-1] ; + + if ( sir[i] == sir[j] ) + + j++ ; + + pr[i] = j ; + + } + + return pr ; + +} + + + + + +int main() + +{ + + string sir,sir1,sir2; + + int n1,n2,i,p; + + vector v; + + vector sol; + + fin >> sir1 ; + + fin >> sir2 ; + + sir = sir1+'#'+sir2 ; + + n1 = sir1.size() ; + + n2 = sir2.size() ; + + sir1.clear() ; + + sir2.clear() ; + + v = prefix(sir) ; + + + + for ( i = n1+1 ; i <= n1+n2 ; i++ ) + + { + + + + if ( v[i] == n1 ) + + sol.push_back(i-2*n1) ; + + } + + p = min(1000,(int)sol.size()) ; + + fout << sol.size() << '\n' ; + + for ( i = 0 ; i < p ; i++ ) + + fout << sol[i] << " " ; + +} diff --git a/strmatch/kmp/167148f1-da28-421e-91d9-9858add24f36.cpp b/strmatch/kmp/167148f1-da28-421e-91d9-9858add24f36.cpp new file mode 100644 index 0000000000000000000000000000000000000000..461b5dffb8a57348f159e002b876e2e452bfe9bc --- /dev/null +++ b/strmatch/kmp/167148f1-da28-421e-91d9-9858add24f36.cpp @@ -0,0 +1,29 @@ +#include +#include + + + + + +std::ifstream fin("strmatch.in"); +std::ofstream fout("strmatch.out"); + +int main() { + std::string s, t; + fin >> s >> t; + std::string aux = s + '#' + t; + std::vector pi(aux.size()); + int k = 0; + for (int i = 1; i < aux.size(); ++i) { + while (k && aux[i] != aux[k]) k = pi[k - 1]; + if (aux[i] == aux[k]) k++; + pi[i] = k; + } + int cnt = 0; + std::vector ans; + for (int i = s.size() + 1; i < aux.size(); ++i) + if (pi[i] == s.size() && ++cnt <= 1000) ans.push_back(i - 2 * s.size()); + fout << cnt << '\n'; + for (int pos : ans) fout << pos << ' '; + return 0; +} diff --git a/strmatch/kmp/18215308-87aa-45d1-a126-94e4e123a6d3.cpp b/strmatch/kmp/18215308-87aa-45d1-a126-94e4e123a6d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..198bbda9657cd504257a5627afebaaf4d32fbbf4 --- /dev/null +++ b/strmatch/kmp/18215308-87aa-45d1-a126-94e4e123a6d3.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; + +void buildLPS(vector &lps, string a){ + lps.reserve(a.length()); + for (int i = 0; i < a.length(); ++i) { + lps.push_back(0); + } + for (int i = 1, j = 0; i < a.length(); ++i) { + if(a[i] == a[j]) lps[i] = ++j; + else if(j) j = lps[j - 1], --i; + else lps[i] = 0; + } +} + +void kmp(vector &matches, vector lps, string a, string b){ + for (int i = 0, j = 0; j < b.length();) { + if(a[i] != b[j]){ + if(i == 0) ++j; + else i = lps[i - 1]; + }else{ + ++i, ++j; + if(i == a.size()){ + matches.push_back(j - i); + + i = lps[i - 1]; + } + } + } +} + +int main() { + ifstream f("strmatch.in"); + ofstream g("strmatch.out"); + string a, b; + f>>a>>b; + vector matches; + vector lps; + buildLPS(lps, a); + kmp(matches, lps, a, b); + int l = matches.size(); + if(1000 < matches.size()) + l = 1000; + g< +#include +using namespace std; + + + +char subsir[2000003], sir[2000003]; + +int nextState[2000003], matchPos[2000003]; + + + +void build_finite_automata(char *s, int n) + +{ + + nextState[0] = nextState[1] = 0; + + + + for (int i = 1, state = 0; i < n; ++i) { + + while (0 < state && s[state] != s[i]) { + + state = nextState[state]; + + } + + + + if (s[state] == s[i]) { + + state++; + + } + + + + nextState[i+1] = state; + + } + +} + + + +int kmp_match(char *subsir, int n, char *sir, int m) + +{ + + int numMatch = 0; + + + + for (int i = 0, state = 0; i < m; ++i) { + + while (0 < state && subsir[state] != sir[i]) { + + state = nextState[state]; + + } + + + + if (subsir[state] == sir[i]) { + + state++; + + } + + + + if (state == n) { + + if (numMatch < 1000) { + + matchPos[numMatch] = i-n+1; + + } + + numMatch++; + + state = nextState[state]; + + } + + } + + + + return numMatch; + +} + + + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + + + f.getline(subsir, 2000003); + + f.getline(sir, 2000003); + + + + int n = strlen(subsir); + + int m = strlen(sir); + + + + build_finite_automata(subsir, n); + + int numMatch = kmp_match(subsir, n, sir, m); + + + + g << numMatch << "\n"; + + + + numMatch = (1000 < numMatch) ? 1000 : numMatch; + + for (int i = 0; i < numMatch; ++i) { + + g << matchPos[i] << " "; + + } + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/strmatch/kmp/19f3d471-3af6-411a-9234-7c872edff9da.cpp b/strmatch/kmp/19f3d471-3af6-411a-9234-7c872edff9da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..80309b0e8e4bd194b1f9371c9fa1fc124f9708d3 --- /dev/null +++ b/strmatch/kmp/19f3d471-3af6-411a-9234-7c872edff9da.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char x[2222222],z[2222222]; + +int xx[2222222],zz[2222222],i,n,m,k,j,w[1111]; + +bool ok=false; + +void pr1(){ + + j=0; + + for(i=1;i0) + + j=xx[j-1]; + + if(x[i]==x[j])j++; + + xx[i]=j; + + } + +} + +void afrez(){ + + k++; + + if(k<1001) + + w[k]=i-n+1; + + j=xx[j-1]; + +} + +void pr2(){ + + j=0; + + for(i=0;i0) + + j=xx[j-1]; + + if(z[i]==x[j]) + + j++; + + } + +} + +int main(){ + + fin.getline(x,2222222); + + n=strlen(x); + + pr1(); + + fin.getline(z,2222222); + + m=strlen(z); + + pr2(); + + fout< +using namespace std; + +ifstream fin ( "strmatch.in" ); + +ofstream fout ( "strmatch.out" ); + + + +void read ( ); + +void preprocess ( ); + +void solve ( ); + +void afis ( ); + + + +string pat; + +string cuvant; + + + +vector < int > v; + +vector < int > sol; + + + +int k; + + + +int main ( ) + +{ + + read ( ); + + + + preprocess ( ); + + + + solve ( ); + + + + afis ( ); + +} + + + +void afis ( ) + +{ + + fout << k << '\n'; + + + + int lng = sol.size(); + + + + for ( int i = 0; i < lng; i++ ) + + fout << sol[i] << ' '; + + + + exit ( 0 ); + +} + + + +void solve ( ) + +{ + + int lng = cuvant.size(); + + int lg = pat.size(); + + + + int j = 0; + + int i = 0; + + + + while ( i < lng ) + + { + + if ( pat[j] == cuvant[i] ) + + { + + i++; + + j++; + + } + + + + if ( j == lg ) + + { + + k++; + + if ( k <= 1000 ) + + sol.push_back ( i - j ); + + j = v[lg-1]; + + } + + + + else if ( i < lng && pat[j] != cuvant[i] ) + + { + + if ( j ) + + j = v[j-1]; + + else + + i++; + + } + + } + +} + + + +void preprocess ( ) + +{ + + int lng = pat.size(); + + v.reserve ( lng+1 ); + + + + v[0] = 0; + + + + int len = 0; + + int i = 1; + + + + while ( i < lng ) + + { + + if ( pat[i] == pat[len] ) + + { + + len++; + + v[i] = len; + + i++; + + } + + + + else + + { + + if ( len ) + + len = v[len-1]; + + else + + { + + v[i] = 0; + + i++; + + } + + } + + } + +} + + + +void read ( ) + +{ + + getline ( fin, pat ); + + getline ( fin, cuvant ); + +} diff --git a/strmatch/kmp/1b1fb0a2-0f49-4b56-9a52-003e48e7b2dc.cpp b/strmatch/kmp/1b1fb0a2-0f49-4b56-9a52-003e48e7b2dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca393bab75d14f87ed484a1fb5c2f4df0609707b --- /dev/null +++ b/strmatch/kmp/1b1fb0a2-0f49-4b56-9a52-003e48e7b2dc.cpp @@ -0,0 +1,132 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX=2000010; + +char a[NMAX],b[NMAX]; + +int n,m; + +int prefix[NMAX]; + +vectorsol; + +int nr; + +void citire() + +{ + + fin.getline(a+1,NMAX); + + fin.getline(b+1,NMAX); + + n=strlen(a+1); + + m=strlen(b+1); + +} + +void calcularePrefix() + +{ + + int j=0; + + for (int i=2; i<=n; i++){ + + while (j>0 && a[j+1]!=a[i]){ + + j=prefix[j]; + + } + + if (a[j+1]==a[i]){ + + j++; + + } + + prefix[i]=j; + + } + +} + +void kmp() + +{ + + calcularePrefix(); + + int j=0; + + for (int i=1; i<=m; i++){ + + while (j>0 && a[j+1]!=b[i]){ + + j=prefix[j]; + + } + + if (a[j+1]==b[i]){ + + j++; + + } + + if (j==n){ + + sol.push_back(i-n); + + j=prefix[j]; + + } + + } + +} + +void afisare() + +{ + + int nr=sol.size(); + + fout<1000) + + nr=1000; + + for (int i=0; i +#include +#include +using namespace std; + +FILE *fin = fopen("strmatch.in", "r"); + +FILE *fout = fopen("strmatch.out", "w"); + + + + + + + +const int DIM = 2e6 + 7; + +int p,t,i,j,lps[2000005],nrap,ap[DIM],k,lg; + +char patt[2000005],txt[2000005]; + +int main() + + + +{ + + fgets(patt+1, 2000005, fin); + + fgets(txt+1, 2000005, fin); + + + + + + p = strlen( patt + 1 )-1; + + t = strlen( txt + 1 )-1; + + + + + + + + lps[0] = -1;lps[1]=0; + + lg=0; + + + + for(i=2; i<=p; ++i){ + + while(lg>0 and patt[i]!=patt[lg+1]) + + lg=lps[lg]; + + if(patt[i]==patt[lg+1]) + + lg++; + + lps[i]=lg; + + } + + + + i = 1; j = 1;lg=0; + + + + while(i<=t ) + + { + + while(lg>0 and txt[i]!=patt[lg+1]) + + lg=lps[lg]; + + if(txt[i]==patt[lg+1]) + + lg++; + + if(lg==p) + + { + + ap[++nrap]=i-p; + + lg=lps[p]; + + } + + i++; + + } + + + + fprintf(fout, "%d\n", nrap); + + + + if(nrap>1000) nrap=1000; + + for(i=1; i<=nrap; ++i) + + fprintf(fout,"%d ",ap[i]); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/strmatch/kmp/1d1ce73b-51a8-496f-b520-dfe6f0db072c.cpp b/strmatch/kmp/1d1ce73b-51a8-496f-b520-dfe6f0db072c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e703ef180c6cbe7825cbedf51dcafc6cc621541b --- /dev/null +++ b/strmatch/kmp/1d1ce73b-51a8-496f-b520-dfe6f0db072c.cpp @@ -0,0 +1,116 @@ +#include +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q+1] != A[i]) + + q = pi[q]; + + if (A[q+1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void) + +{ + + int i, q = 0, n = 0; + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + fgets(A, sizeof(A), stdin); + + fgets(B, sizeof(B), stdin); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i-1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i-1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q+1] != B[i]) + + q = pi[q]; + + if (A[q+1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i-M; + + } + + } + + + + printf("%d\n", n); + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + printf("%d ", pos[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/1dda3ba2-1ed4-4e0d-b63b-3ff483dbf779.cpp b/strmatch/kmp/1dda3ba2-1ed4-4e0d-b63b-3ff483dbf779.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c78c49f6317a4f0fcb3217d816887ef43e05407 --- /dev/null +++ b/strmatch/kmp/1dda3ba2-1ed4-4e0d-b63b-3ff483dbf779.cpp @@ -0,0 +1,77 @@ +#include + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX=2000005; + + + +string S,P; + +int lps[NMAX]; + +vector poz; + + + +void KMP(){ + + + + for(int i=1; i0 and S[i]!=P[q];) + q=lps[q-1]; + + if(S[i]==P[q]) ++q; + + if(q==P.size()) poz.push_back(i-q+1); + + } + +} + + + +int main(){ + + fin>>P>>S; + KMP(); + + + fout< +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s1[2000002],s2[2000002]; + +int lps[2000002],sol[2000002]; + +int main() + +{ + + int n1,n2,i,x,k=0; + + f.getline(s1+1,2000001); + + f.getline(s2+1,2000001); + + n1=strlen(s1+1); + + n2=strlen(s2+1); + + for(i=2;i<=n1;i++) + + { + + x=lps[i-1]; + + while(x>0&&s1[x+1]!=s1[i]) + + x=lps[x]; + + if(s1[x+1]==s1[i]) + + lps[i]=x+1; + + else + + lps[i]=0; + + } + + x=0; + + for(i=1;i<=n2;i++) + + { + + while(x>0&&s1[x+1]!=s2[i]) + + x=lps[x]; + + if(s1[x+1]==s2[i]) + + x++; + + if(x==n1) + + sol[++k]=i-n1; + + } + + g< +#include +using namespace std; + +int pi[2000005],res[1005]; + +stack s; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int l=0,n,m,cnt=0,r=0,c2=0,i; + + string a,b; + + cin >> a; + + cin >> b; + + n=a.size(); + + m=b.size(); + + for(int i=1;i0 && a[l-1]!=a[i]) + + l=pi[l-1]; + + pi[i]=l; + + } + + l=0; + + i=0; + + while (i < m) + + { + + if (a[l] == b[i]) + + { + + l++; + + i++; + + } + + if (l == n) + + { + + if(cnt<1000) + + { + + s.push(i-l); + + cnt++; + + } + + r++; + + l = pi[l - 1]; + + } + + else if (i < m && a[l] != b[i]) + + { + + if (l != 0) + + l = pi[l - 1]; + + else + + i = i + 1; + + } + + } + + cout << r << '\n'; + + while(s.size()>0) + + { + + res[c2]=s.top(); + + c2++; + + s.pop(); + + } + + for(int i=c2-1;i>=0;i--) + + cout << res[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/1f21a880-5982-4c8b-ae21-69bd18c474a3.cpp b/strmatch/kmp/1f21a880-5982-4c8b-ae21-69bd18c474a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cc5e30be2418ef706ba59c54fb0c8cd412d3a6a --- /dev/null +++ b/strmatch/kmp/1f21a880-5982-4c8b-ae21-69bd18c474a3.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include + + +using namespace std; + + + +int solCnt; + +int n, m; + +int pos[1005], lps[2000005]; + +char pat[2000005], txt[2000005]; +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int cnd = 0; + + int pos = 1; + + + + lps[0] = -1; + + while (pos < M) { + + if (pat[pos] == pat[cnd]) { + + lps[pos] = lps[cnd]; + + } + + else + + { + + lps[pos] = cnd; + + cnd = lps[cnd]; + + while (cnd >= 0 && pat[pos] != pat[cnd]) + + cnd = lps[cnd]; + + } + + pos++; + + cnd++; + + } + + lps[pos] = cnd; + +} + + + +void KMPSearch(char* pat, char* txt) + +{ + + m = strlen(pat); + + n = strlen(txt); + + + + + + computeLPSArray(pat, m, lps); + + + + int i = 0; + + int j = 0; + + while (i < n) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + if (j == m) { + + if (solCnt < 1000) + + pos[solCnt++] = i - j; + + else + + solCnt++; + + j = lps[j]; + + } + + } + + else { + + j = lps[j]; + + if (j < 0) + + { + + j++; + + i++; + + } + + } + + } + +} + + + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", pat); + + scanf("%s", txt); + + + + KMPSearch(pat, txt); + + + + printf("%d\n", solCnt); + + for (int i = 0; i < std::min(solCnt, 1000); ++i) + + printf("%d ", pos[i]); + + return 0; + +} diff --git a/strmatch/kmp/20fa6bd9-fa04-4a1c-bbc1-5490d3c4ce63.cpp b/strmatch/kmp/20fa6bd9-fa04-4a1c-bbc1-5490d3c4ce63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad51b151aa195463b7c3487356dce737376adc75 --- /dev/null +++ b/strmatch/kmp/20fa6bd9-fa04-4a1c-bbc1-5490d3c4ce63.cpp @@ -0,0 +1,169 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); +char text[2000001], pattern[2000001]; + +int t, p; + +int n, result[2000001]; + + + + + +int lps[2000001]; + + + + + +void calculateLPSArray() { + + int i=1, len=0; + + while(i 1000) { + + n = 1000; + + } + + for (int i=0; i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +const int Nmax = 2e6 + 5; + +int pi[Nmax], ans[Nmax]; + +int N; + +string S, C; + + + +void preproc() + +{ + + int K = 0; + + + + for(int i = 1; i < S.size(); ++i) + + { + + while(K && S[K] != S[i]) + + K = pi[K - 1]; + + + + if(S[K] == S[i]) + + K++; + + + + pi[i] = K; + + + + } + +} + + + +void solve() + +{ + + int K = 0, L = C.size(); + + + + for(int i = 0; i < L + + ; ++i) + + { + + while(K && S[K] != C[i]) + + K = pi[K - 1]; + + + + if(S[K] == C[i]) + + K++; + + + + if(K == S.size()) + + { + + if(N <= 1000) + + ans[++N] = (i - K + 1); + + else N++; + + K = pi[K - 1]; + + } + + } + + + + out << N << '\n'; + + + + for(int i = 1; i <= min(N,1000); ++i) + + out << ans[i] << " "; + + + +} + + + +int main() + +{ + + in >> S >> C; + + + + preproc(); + + solve(); + + return 0; + +} diff --git a/strmatch/kmp/23706ff8-ac25-442f-8bc6-d445768891c1.cpp b/strmatch/kmp/23706ff8-ac25-442f-8bc6-d445768891c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dc3aa0ce196462690cb751326569b23c3dac9fd --- /dev/null +++ b/strmatch/kmp/23706ff8-ac25-442f-8bc6-d445768891c1.cpp @@ -0,0 +1,130 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char a[2000005], b[2000005]; + +int p[2000005]; + +int sol[1005], nr; + +int m, n; + + + +void formprefix(){ + + p[1]=0; + + int i=0; + + for(int j=2; j<=m; j++){ + + while(i>0 && a[i+1]!=a[j]) + + i=p[i]; + + if(a[i+1] == a[j]) + + ++i; + + p[j] = i; + + } + +} + + + +void cautCuvant(){ + + int k=0; + + for(int i=1; i<=n; i++){ + + while(k && a[k+1]!=b[i]) + + k=p[k]; + + if(a[k+1] == b[i]) + + ++k; + + if(k == m){ + + nr++; + + if(nr<=1000) + + sol[nr] = i-m; + + } + + } + + + +} + + + +void afisprefix(){ + + for(int i=1; i<=m; i++) + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); +char text[2000001], pattern[2000001]; + +int t, p; + +int n, result[2000001]; + + + + + +int lps[2000001]; + + + + + +void calculateLPSArray() { + + int len = 0; + + lps[0] = 0; + + + + for(int i = 1; i < p; ++i) { + + if(pattern[i] == pattern[len]) lps[i] = ++len; + + else if(len) len = lps[len - 1], --i; + + else lps[i] = 0; + + } + +} + + + +void printLPSArray() { + + for (int i=0; i 1000) { + + n = 1000; + + } + + + + for (int i=0; i + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +char a[2000005], b[2000005]; + + + +int n, m, i, j, cnt; + +int sol[2000005], l[2000005]; + + + +int main(){ + + fin >> a + 1; + + fin >> b + 1; + + n = strlen (a + 1); + + m = strlen (b + 1); + + for (i=2; i<=n; i++){ + + while (a[i] != a[j+1] && j){ + + j = l[j]; + + } + + if (a[i] == a[j+1]){ + + j++; + + } + + l[i] = j; + + } + + j = 0; + + for (i=1; i<=m; i++){ + + while (b[i] != a[j+1] && j){ + + j = l[j]; + + } + + if (b[i] == a[j+1]){ + + j++; + + } + + if (j == n){ + + + + sol[++cnt] = i - n; + + j = l[j]; + + } + + } + + fout << cnt << "\n"; + + cnt = min (cnt, 1000); + + for (i=1; i<=cnt; i++){ + + fout << sol[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/kmp/2474370c-3fa0-4c89-899c-26740ec95a55.cpp b/strmatch/kmp/2474370c-3fa0-4c89-899c-26740ec95a55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec64b8bf2ef3f26fb7636ddb3a965f999b84cfbf --- /dev/null +++ b/strmatch/kmp/2474370c-3fa0-4c89-899c-26740ec95a55.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + + + +char A[2000005], B[2000005]; + +vector v; + + + +int pr[2000005]; + + + +int main() + +{ + + ios_base::sync_with_stdio(0); + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + cin.getline(A + 1, 2000005); + + cin.getline(B + 1, 2000005); + + + + int lg1 = strlen(A + 1), lg2 = strlen(B + 1); + + int k = 0; + + for(int i = 2; i <= lg1; ++i){ + + while(k > 0 && A[k + 1] != A[i]) + + k = pr[k]; + + if(A[i] == A[k + 1]) + + ++k; + + pr[i] = k; + + } + + + + k = 0; + + int cnt = 0; + + for(int i = 1; i <= lg2; ++i){ + + while(k > 0 && A[k + 1] != B[i]) + + k = pr[k]; + + if(A[k + 1] == B[i]) + + ++k; + + if(k == lg1){ + + k = pr[lg1]; + + if(v.size() < 1000) + + v.push_back(i - lg1); + + ++cnt; + + } + + } + + + + cout << cnt << '\n'; + + for(auto it: v) + + cout << it << ' '; + + cout << '\n'; + + return 0; + +} diff --git a/strmatch/kmp/24d8acc4-e305-42f0-abb8-023bb0ba0c6a.cpp b/strmatch/kmp/24d8acc4-e305-42f0-abb8-023bb0ba0c6a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e8ae0fc85f2b99eadddf1d8cdad5ae2c881871ca --- /dev/null +++ b/strmatch/kmp/24d8acc4-e305-42f0-abb8-023bb0ba0c6a.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + + + +const int N = 2e6 + 10; + + + +int a[2 * N]; + +string A , B; + +vector < int > start; + + + +signed main() + +{ + + + + ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0) , freopen("strmatch.in" , "r" , stdin) , freopen("strmatch.out" , "w" , stdout); + + + + + + cin >> A; + + cin >> B; + + + + int ans = 0 , M = A.size(); + + + + A += "#" + B; + + + + for(int i = 1 ; i < A.size() ; i++) + + { + + int j = a[i - 1]; + + + + while(j && A[j] != A[i]) + + j = a[j - 1]; + + + + a[i] = j + (A[j] == A[i]); + + + + if(a[i] == M) + + start.push_back(i - M + 1); + + } + + + + cout << start.size() << '\n'; + + + + for(int i = 0 ; i < min(1000 , (int)start.size()) ; i++) + + cout << start[i] - M - 1 << ' '; + + + + return 0; + +} diff --git a/strmatch/kmp/25447a88-d442-42a6-a03f-94690d289995.cpp b/strmatch/kmp/25447a88-d442-42a6-a03f-94690d289995.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d36eac1339a21e2a88d54042c57efcaa3bd2622 --- /dev/null +++ b/strmatch/kmp/25447a88-d442-42a6-a03f-94690d289995.cpp @@ -0,0 +1,75 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int stare[2000001],nr; + +string s,s1; + +vector v; + +int main() + +{ + + fin>>s>>s1; + + int x=s.size(),st=0,i; + + for (i=2;i<=x;i++) + + { + + while (st>0 && s[st]!=s[i-1]) + + st=stare[st]; + + if (s[st]==s[i-1]) + + st++; + + stare[i]=st; + + } + + int x1=s1.size(); + + st=0; + + for (i=1;i<=x1;i++) + + { + + while (st>0 && s[st]!=s1[i-1]) + + st=stare[st]; + + if (s[st]==s1[i-1]) + + st++; + + if (st==x){ + + nr++; + + if (nr<=1000) + + v.push_back(i-st); + + } + + } + + fout< +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int lps[2000010],v[1010],n,m; + +char A[2000010],B[2000010]; + +void BuildLPS() + +{ + + int i,k=0; + + lps[1]=0; + + for (i=2;i<=n;i++) + + { + + while (k>0 && B[i-1]!=B[k]) k=lps[k]; + + if (B[i-1]==B[k]) k++; + + lps[i]=k; + + } + +} + +void KMP() + +{ + + int k=0,q=0,i; + + for (i=0;i0 && A[i]!=B[q]) q=lps[q]; + + if (B[q]==A[i]) q++; + + if (q==n) { + + k++; + + if (k<=1000) v[k]=i-n+1; + + } + + } + + g<=1000) k=1000; + + for (i=1;i<=k;i++) g< +#include + + + + + + +using namespace std; + + + +FILE *fin = fopen("strmatch.in", "r"); + +FILE *fout = fopen("strmatch.out", "w"); + + + +int lgp,n,lg,lps[2000005],nrsol,sol[1005],i; + +char patt[2000005],sir[2000005]; + + + +int main() + +{ + + fgets(patt+1, 2000003, fin); + + fgets(sir+1, 2000003, fin); + + + + patt[0] = sir[0] = 'a'; + + lgp = strlen(patt)-2; + + n = strlen(sir)-2; + + + + lg = 0; + + for(i=2; i<=lgp; ++i) + + { + + while(patt[i] != patt[lg+1] and lg > 0) + + lg = lps[lg]; + + if(patt[i] == patt[lg+1]) + + ++lg; + + lps[i] = lg; + + } + + + + lg = 0; + + for(i=1; i<=n; ++i) + + { + + while(sir[i] != patt[lg+1] and lg > 0) + + lg = lps[lg]; + + if(sir[i] == patt[lg+1]) + + lg++; + + if(lg == lgp) + + { + + ++nrsol; + + if(nrsol <= 1000) + + sol[nrsol] = i-lgp; + + lg = lps[lg]; + + } + + } + + + + if(nrsol > 1000) + + { + + fprintf(fout, "%d\n", nrsol); + + for(i=1; i<=1000; ++i) + + fprintf(fout, "%d ", sol[i]); + + } + + else + + { + + fprintf(fout, "%d\n", nrsol); + + for(i=1; i<=nrsol; ++i) + + fprintf(fout, "%d ", sol[i]); + + } + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/strmatch/kmp/2a7a7c36-dd6a-4942-8283-fdbac4f9de80.cpp b/strmatch/kmp/2a7a7c36-dd6a-4942-8283-fdbac4f9de80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7665930d2b1833d69e2cbc30b1afc6b1db26c5a8 --- /dev/null +++ b/strmatch/kmp/2a7a7c36-dd6a-4942-8283-fdbac4f9de80.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + + +using namespace std; + + + +const int N = (int) 2e6 + 7; + +string pat, s; + +int m, n; + +int ps[N]; + + + +int main() { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + + + cin >> pat >> s; + + m = (int) pat.size(); + + n = (int) s.size(); + + int j = 0; + + for (int i = 1; i < m; i++) { + + while (j && pat[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[i] == pat[j]) { + + j++; + + } + + ps[i] = j; + + } + + j = 0; + + vector ret; + + for (int i = 0; i < n; i++) { + + while (j && s[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[j] == s[i]) { + + j++; + + } + + if (j == m) { + + j = ps[m - 1]; + + ret.push_back(i); + + } + + } + + cout << (int) ret.size() << "\n"; + + while ((int) ret.size() > 1000) { + + ret.pop_back(); + + } + + for (auto &i : ret) { + + cout << i - m + 1 << " "; + + } + + cout << "\n"; + +} diff --git a/strmatch/kmp/2a9c5fd4-e4dc-4bd5-b25e-3f4402511f78.cpp b/strmatch/kmp/2a9c5fd4-e4dc-4bd5-b25e-3f4402511f78.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e93a25d020b6439ebf622c32aabf4ae864b57ca2 --- /dev/null +++ b/strmatch/kmp/2a9c5fd4-e4dc-4bd5-b25e-3f4402511f78.cpp @@ -0,0 +1,146 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n, m, l; + +int lps[2000005], sol[1005]; + +string text, pattern; + + + +void build_lps(); + +void solve(); + + + +int main() + +{ + + + + fin >> pattern >> text; + + n = text.length(); + + m = pattern.length(); + + + + build_lps(); + + solve(); + + + + fout << l << "\n"; + + l = min(l, 1000); + + for(int i = 1; i <= l; ++i) fout << sol[i] << " "; + + + + return 0; + +} + + + +void build_lps(){ + + + + int len = 0, i = 1; + + + + while(i < m){ + + if(pattern[i] == pattern[len]){ + + lps[i++] = ++len; + + } + + else{ + + if(len){ + + len = lps[len - 1]; + + } + + else{ + + lps[i++] = 0; + + } + + } + + } + + + +} + + + +void solve(){ + + + + int i = 0, j = 0; + + + + while(i < n){ + + if(text[i] == pattern[j]){ + + ++i; + + ++j; + + } + + + + if(j == m){ + + ++l; + + if(l <= 1000) sol[l] = i - j; + + j = lps[j - 1]; + + } + + else if(i < n && text[i] != pattern[j]){ + + if(j != 0){ + + j = lps[j - 1]; + + } + + else ++i; + + } + + } + + + +} diff --git a/strmatch/kmp/2b090f77-fdc0-48a4-aff4-2ef159d3c676.cpp b/strmatch/kmp/2b090f77-fdc0-48a4-aff4-2ef159d3c676.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8c0f30720f1202d36a5dac1adc03b15f26348c83 --- /dev/null +++ b/strmatch/kmp/2b090f77-fdc0-48a4-aff4-2ef159d3c676.cpp @@ -0,0 +1,80 @@ +#include + + + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int nxt[2000005], ocr[2000005]; + +string s, c; + + + +int main() + +{ + + fin >> s >> c; + + int n=s.size(); + + int m=c.size(); + + nxt[0]=0; + + int j=0; + + for(int i=1; i0 && s[i]!=s[j]) + + j=nxt[j-1]; + + if(s[i]==s[j]) + + j++; + + nxt[i]=j; + + } + + int nr=0; + + j=0; + + for(int i=0; i0 && s[j]!=c[i]) + + j=nxt[j-1]; + + if(s[j]==c[i]) + + j++; + + if(j==n) + + ocr[++nr]=i-n+1; + + } + + fout << nr << "\n"; + + for(int i=1; i<=min(nr, 1000); i++) + + fout << ocr[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/2bae1f0e-66cf-439d-a1e8-90ed4dffc067.cpp b/strmatch/kmp/2bae1f0e-66cf-439d-a1e8-90ed4dffc067.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf4bbf48c5876f4134e3861179f2f53b7c1a4a24 --- /dev/null +++ b/strmatch/kmp/2bae1f0e-66cf-439d-a1e8-90ed4dffc067.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +void Citire(); +void Pref(); +void KMP(); + + +string T,P; +int k; +int a[2000005],lps[2000005]; + +int main(){ + fin>>P>>T; + + KMP(); + + fout<1000) k=1000; + for(int i=1;i<=k;++i) + fout<0 && P[q]!=T[i]) q=lps[q-1]; + + if(P[q]==T[i]) ++q; + if(q==P.size()) a[++k]=i-P.size()+1; + } + + + +} diff --git a/strmatch/kmp/2bc3593e-ae85-4a28-8f84-92c72df190ef.cpp b/strmatch/kmp/2bc3593e-ae85-4a28-8f84-92c72df190ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..718900b93ae7bdc9810c4e07b5f2f9e508b6d72f --- /dev/null +++ b/strmatch/kmp/2bc3593e-ae85-4a28-8f84-92c72df190ef.cpp @@ -0,0 +1,87 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char s1[2000009],s2[2000009]; + +int prefix[2000009],ans[1009],k,j; + +int main() + +{ + + fin.getline(s1,2000005); + + fin.getline(s2,2000005); + + int n=strlen(s1); + + int m=strlen(s2); + + j=0; + + for(int i=1;i + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + + + +int lps[2000005]; + +char s1[2000005], s2[2000005]; + + + +int ans1 = 0; + +vector < int > ans2; + + + +void make_lps(){ + + int j = 0; + + for(int i = 1; s1[i] != '\0'; ++i){ + + if(s1[i] == s1[j]){ + + lps[i] = ++j; + + }else if(j != 0){ + + j = lps[j-1]; + + i--; + + } + + } + +} + + + +void solve(){ + + int j = 0; + + for(int i = 0; s2[i] != '\0'; ++i){ + + if(s1[j] == s2[i]){ + + j++; + + if(s1[j] == '\0'){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-j+1); + + j = lps[j-1]; + + } + + }else if(j != 0){ + + j = lps[j-1]; + + i--; + + } + + } + +} + + + +int main(){ + + + + f >> s1 >> s2; + + make_lps(); + + solve(); + + + + g << ans1 << '\n'; + + for(int a=0;a +#include + +using namespace std; + +ifstream f("strmatch.in"); +ofstream g("strmatch.out"); + + + +char text[2000001], pattern[2000001]; +int t, p; +int n, result[2000001], lps[2000001]; + + +void calculateLPSArray() { + int lenght = 0; + lps[0] = 0; + for (int i = 1; i < p; ++i) { + if (pattern[lenght] == pattern[i]) + lps[i] = ++lenght; + else if (lenght) + lenght = lps[lenght - 1], i--; + else + lps[i] = 0; + } +} + +void printLPSArray() { + for (int i = 0; i < p; i++) + g << lps[i] << " "; + g << endl; +} + +void findPatternMatches() { + calculateLPSArray(); + int j = 0; + for (int i = 0; i < t;) { + if (pattern[j] == text[i]) + j++, i++; + else if (j == p) + result[n++] = i - p, j = lps[j - 1]; + else if (j) + j = lps[j - 1]; + else + i++; + } + if (j == p) + result[n++] = t - p; +} + +void printResult() { + g << n << endl; + if (n > 1000) + n = 1000; + for (int i = 0; i < n; i++) + g << result[i] << " "; +} + +int main() { + f.getline(pattern, 2000001); + f.getline(text, 2000001); + p = strlen(pattern); + t = strlen(text); + + findPatternMatches(); + printResult(); + + return 0; +} diff --git a/strmatch/kmp/2ca508cd-35a8-4a27-85dd-3c5e3c70c44d.cpp b/strmatch/kmp/2ca508cd-35a8-4a27-85dd-3c5e3c70c44d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d51e1a48e333bf749246571ef39ae081d046c28 --- /dev/null +++ b/strmatch/kmp/2ca508cd-35a8-4a27-85dd-3c5e3c70c44d.cpp @@ -0,0 +1,68 @@ +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int urm[2000005],n,k,q,m,sol[1005],i,p; + +char N[2000005],M[2000005]; + +int main() + +{ + + f>>N; + + n=strlen(N); + + for(i=2;i<=n;i++) + + { + + while(k>0&&N[k]!=N[i-1]) k=urm[k]; + + if(N[k]==N[i-1]) k++; + + urm[i]=k; + + } + + f>>M; + + m=strlen(M); + + for(i=0;i0&&N[q]!=M[i]) q=urm[q]; + + if(N[q]==M[i]) q++; + + if(q==n) + + { + + p++; + + if(p<=1000) + + { + + sol[p]=i; + + } + + } + + } + + g< +#include +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 2000005; + +string a, b; + +string s; + +int phi[2*NMAX]; + + + +void kmp(string s, int phi[NMAX]) + +{ + + int ns = s.size(); + + phi[0] = -1; + + for (int i = 1; i < ns; i++) + + { + + int k = i - 1; + + while (phi[k] != -1 && s[phi[k] + 1] != s[i]) + + k = phi[k]; + + if (s[phi[k] + 1] == s[i]) + + phi[i] = phi[k] + 1; + + else + + phi[i] = -1; + + } + +} + + + +vectorsol; + + + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + cin >> a >> b; + + s = (a + '#' + b); + + kmp(s, phi); + + int na = a.size(), nb = b.size(),ns = s.size(); + + for (int i = na + 1; i < ns; i++) + + if (phi[i] == na - 1) + + sol.push_back(i - 2 * na); + + cout << sol.size() << "\n"; + + int mi = min((int)sol.size(), 1000); + + for (int i = 0; i < mi; i++) + + cout << sol[i] << " "; + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/kmp/2f3cde3f-7c23-4e1e-9c46-44cb74ea5f80.cpp b/strmatch/kmp/2f3cde3f-7c23-4e1e-9c46-44cb74ea5f80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9f276ee681dfe1ac2966e588c905f8a04209e446 --- /dev/null +++ b/strmatch/kmp/2f3cde3f-7c23-4e1e-9c46-44cb74ea5f80.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000009]; + +char b[2000009]; + +int lga,lgb; + +int vala[2000009]; + +int valb[2000009]; + +int k; + +int nrs; + +vectorsol; + +void citire(); + +void strmatch(); + +void afis(); + +int main() + +{citire(); + + strmatch(); + + afis(); + + return 0; + +} + +void citire() + +{ + + fin>>(a+1)>>(b+1); + + lga=strlen(a+1); + + lgb=strlen(b+1); + +} + + + +void strmatch() + +{ + + int i; + + for(i=2;i<=lga;i++) + + { + + while( a[i]!= a[k+1] && k) + + k=vala[k]; + + if(a[k+1]==a[i]) + + k++; + + vala[i]=k; + + } + + + + if(a[1]==b[1]) + + {valb[1]=1;k=1;}else k=0; + + for(i=2;i<=lgb;i++) + + { + + while(a[k+1]!= b[i] && k) + + k=vala[k]; + + if(a[k+1]==b[i]) + + k++; + + valb[i]=k; + + if(k==lga ) + + {nrs++; + + if(sol.size()<1000) + + sol.push_back(i-lga); + + } + + } + +} + +void afis() + +{ + + int i; + + fout< +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char sir1[4000005],sir2[2000005]; + +int p[4000005],lg1,lg2; + + + +void f_pref(char s[], int pref[], int n) + +{ + + pref[0] = 0; + + for (int i = 1; i < n; i++) + + { + + int j = i; + + while (j > 0 && s[pref[j - 1]] != s[i]) + + { + + j = pref[j - 1]; + + } + + if (j == 0) + + pref[i] = 0; + + else + + pref[i] = pref[j - 1] + 1; + + } + +} + + + +int main() + +{ + + fin>>sir1>>sir2; + + lg1=strlen(sir1); + + lg2=strlen(sir2); + + sir1[lg1]='#'; + + for(int i=lg1+1; i<=lg1+lg2; ++i) + + sir1[i]=sir2[i-lg1-1]; + + f_pref(sir1,p,lg1+lg2+1); + + int ct=0; + + for(int i=0; i<=lg1+lg2; ++i) + + if(p[i]==lg1) + + ct++; + + + + fout< +#include +using namespace std; + + + +char A[2000005], B[2000005]; + +int prefix[2000005], sol[1005]; + + + +int main() { + + int lenA, lenB, i, j, n = 0; + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s%s", A, B); + + lenA = strlen(A); + + lenB = strlen(B); + + + + for(i = lenA; i >= 1; --i) + + A[i] = A[i-1]; + + for(i = lenB; i >= 1; --i) + + B[i] = B[i-1]; + + + + for(i = 2, j = 0; i <= lenA; ++i) { + + while(j && A[i] != A[j+1]) + + j = prefix[j]; + + if(A[i] == A[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + for(i = 1, j = 0; i <= lenB; ++i) { + + while(j && B[i] != A[j+1]) + + j = prefix[j]; + + if(B[i] == A[j+1]) + + ++j; + + if(j == lenA) { + + j = prefix[j]; + + ++n; + + if(n <= 1000) + + sol[n] = i - lenA; + + } + + } + + + + printf("%d\n", n); + + n = (n < 1000 ? n : 1000); + + for(i = 1; i <= n; ++i) + + printf("%d ", sol[i]); + + printf("\n"); + +} diff --git a/strmatch/kmp/314a9d3e-36ef-4db7-ba59-43dcab2f79cc.cpp b/strmatch/kmp/314a9d3e-36ef-4db7-ba59-43dcab2f79cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5500102052bbfdbc6dc8cf58da48222aac369c9 --- /dev/null +++ b/strmatch/kmp/314a9d3e-36ef-4db7-ba59-43dcab2f79cc.cpp @@ -0,0 +1,147 @@ +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +vector sufix_and_prefix(string pattern) { + + vector aux; + + aux.resize(pattern.size(), 0); + + + + int i = 0; + + int j = 1; + + + + while(j < pattern.size()){ + + if(pattern[i] == pattern[j]) { + + aux[j] = i + 1; + + i++; + + j++; + + } else if(i != 0){ + + i = aux[i - 1]; + + } else { + + aux[j] = 0; + + j++; + + } + + } + + + + return aux; + +} + + + +vector KMP(string s, string pattern, int& nr){ + + vector positions; + + vector aux = sufix_and_prefix(pattern); + + int i = 0; + + int j = 0; + + while(i < s.size()) { + + if(s[i] == pattern[j]) { + + i++; + + j++; + + } else { + + if(j != 0) { + + j = aux [j - 1]; + + } else { + + i++; + + } + + } + + if(j == pattern.size()) { + + nr++; + + j = aux[j-1]; + + positions.push_back(i - pattern.size()); + + } + + + + } + + + + return positions; + +} + +int main() + +{ + + string s; + + string pattern; + + f>>pattern>>s; + + int nr = 0; + + vector result = KMP(s, pattern, nr); + + + + g< 1000) + + nr = 1000; + + else + + nr = result.size(); + + for(int i = 0; i < nr; i++) + + g< + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000004],b[2000004]; + +vector sol; + +int nrsol,lun,i,la,lb,t[2000004]; + +int main(){ + + fin>>(a+1)>>(b+1); + + la=strlen(a+1); + + lb=strlen(b+1); + + for(i=2;i<=la;i++){ + + while(lun!=0&&a[i]!=a[lun+1]) + + lun=t[lun]; + + if(a[lun+1]==a[i]) + + lun++; + + t[i]=lun; + + } + + lun=0; + + for(i=1;i<=lb;i++){ + + while(lun!=0&&b[i]!=a[lun+1]) + + lun=t[lun]; + + if(b[i]==a[lun+1]) + + lun++; + + if(lun==la){ + + nrsol++; + + if(nrsol<=1000){ + + sol.push_back(i-la); + + } + + lun=t[lun]; + + } + + } + + fout< +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char sir1[4000005], sir2[2000005]; + +int p[4000005], lg1, lg2; + + + +void f_pref(char s[], int pref[], int n) + +{ + + pref[0] = 0; + + for(int i = 1; i < n; i++){ + + int j; + + j = i; + + while(j > 0 && s[pref[j - 1]] != s[i]){ + + j = pref[j - 1]; + + } + + if(j == 0){ + + pref[i] = 0; + + } + + else{ + + pref[i] = pref[j - 1] + 1; + + } + + } + +} + + + +int main() + +{ + + int i, ct; + + + + fin >> sir1 >> sir2; + + lg1 = strlen(sir1); + + lg2 = strlen(sir2); + + sir1[lg1] = '#'; + + for(i = lg1+1; i <= lg1+lg2; i++){ + + sir1[i] = sir2[i-lg1-1]; + + } + + f_pref(sir1, p, lg1+lg2+1); + + ct = 0; + + for(i=0; i<=lg1+lg2; i++){ + + if(p[i] == lg1){ + + ct++; + + } + + } + + fout << ct << '\n'; + + ct = 0; + + for(int i=0; i<=lg1+lg2; ++i){ + + if(p[i]==lg1 && ct<1000){ + + ct++; + + fout< +#include +#include + + + +using namespace std; + +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int pi[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + scanf("%s\n%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + + for(int i=1;i +#include +#include +using namespace std; + +char pattern[2000001], text[2000001],*p; + +int lps[2000001],len,n,nr,v[2000001]; + + + +void LPS() + +{ + + n=strlen(pattern); + +lps[0]=0; + +int i=1; + + while(i1000) + + nr=1000; + + for(int i=0;i +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n, phi[4000000 +10]; + +string s1, s2, s; + +vector sol; + + + +int main() + +{ + + f >> s1 >> s2; + + s = s1 + '&' + s2; + + n = s.size(); + + int i=1, j=0; + + while(i < n) + + { if(s[i] == s[j]) phi[i] = j+1, i++, j++; + + else + + { if(j > 0) j = phi[j-1]; + + else i++; + + } + + } + + for(int i=0; i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int p[2000005]; + +char A[2000005],B[2000005]; + +vector Sol; + +int main() + +{ + + f>>A>>B; + + int n=strlen(A); + + for(int i=1,j=0; A[i];) + + { + + if(A[i]==A[j]) + + p[i]=++j,i++; + + else + + { + + if(j) + + j=p[j-1]; + + else + + i++; + + } + + } + + for(int i=0,j=0; B[i];) + + { + + if(B[i]==A[j]) + + i++,j++; + + if(j==n) + + { + + Sol.push_back(i-j); + + j=p[j-1]; + + } + + else if(B[i]!=A[j]) + + { + + if(j) + + j=p[j-1]; + + else + + i++; + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char p[2000001], t[2000001]; + +int f[2000001]; + +vector < int > poz; + + + +int kmp(char p[], char t[], vector < int > &poz) + +{ + + int n, m, i, j, nr; + + n = strlen(t); + + m = strlen(p); + + + + f[0] = 0; + + i = 0, j = 1; + + while (j < m) + + { + + while (j < m && p[i] != p[j]) + + { + + f[j] = 0; + + j++; + + } + + + + if (j < m) + + { + + if (p[i] == p[j]) + + { + + f[j] = i + 1; + + i++, j++; + + } + + if (p[i] != p[j]) + + i = 0; + + } + + } + + + + nr = 0; + + i = j = 0; + + while (i < n) + + { + + if (t[i] == p[j]) + + { + + if (j == m - 1) + + { + + nr++; + + poz.push_back(i - m + 1); + + i++, j = f[j]; + + } + + else + + i++, j++; + + } + + else if (j > 0) + + j = f[j-1]; + + else + + i++; + + } + + + + return nr; + +} + + + +int main() + +{ + + fin >> p >> t; + + fout << kmp(p, t, poz) << "\n"; + + for (int i=0; i + +using namespace std; +char s1[2000005],s2[2000005]; +int n,m,k,pi[2000005]; +vectorrez; + +ifstream ccin("strmatch.in"); +ofstream ccout("strmatch.out"); + + +void prefix(){ + k=0; + for(int i=2; i<=n; i++){ + while(k>0&&s1[k+1]!=s1[i]) + k=pi[k]; + if(s1[k+1]==s1[i]) + k+=1; + pi[i]=k; + } +} + +int main(){ + ccin>>s1+1>>s2+1; + n=strlen(s1+1); m=strlen(s2+1); + prefix(); + k=0; + for(int i=1; i<=m; i++){ + while(k>0&&s1[k+1]!=s2[i]) + k=pi[k]; + if(s1[k+1]==s2[i]) + k++; + if(k==n) + rez.push_back(i-n); + } + k=rez.size(); + ccout< +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a, b; + +int pi[2000005]; + +vector aparitii; + + + +int main() { + + fin >> a >> b; + + int l1 = a.length(), l2 = b.length(); + + + + int k = -1; + + pi[0] = -1; + + for(int i = 1; i < l1; i++) { + + while(k > -1 && a[i] != a[k+1]) + + k = pi[k]; + + if(a[i] == a[k+1]) k++; + + pi[i] = k; + + } + + k = -1; + + for(int i = 0; i < l2; i++) { + + while(k > -1 && b[i] != a[k+1]) + + k = pi[k]; + + if(b[i] == a[k+1]) k++; + + if(k == l1-1) + + aparitii.push_back(i-l1+1); + + } + + fout << aparitii.size() << '\n'; + + int z = min(1000, int(aparitii.size())); + + for(int i = 0; i < z; i++) + + fout << aparitii[i] << ' '; + +} diff --git a/strmatch/kmp/379ef197-17b6-4099-8bf5-c65da4274248.cpp b/strmatch/kmp/379ef197-17b6-4099-8bf5-c65da4274248.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8504283e6a3823177903e25026061b81fd21b7b2 --- /dev/null +++ b/strmatch/kmp/379ef197-17b6-4099-8bf5-c65da4274248.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +string s1,s2,s; + +int afis[1001],pi[4000001]; + +int main() + +{ + + int i,cnt=0; + + in>>s1>>s2; + + s=" "+s1+"#"+s2; + + for(i=2; is1.size()+1&&pi[i]==s1.size()) + + { + + cnt++; + + if(cnt<=1000) + + afis[cnt]=i-2*s1.size()-1; + + } + + } + + out< + +typedef unsigned long long ull; +typedef long long ll; +using namespace std; +ll ceildiv(ll one, ll two) { + if (one % two == 0) {return one / two;} + else {return one / two + 1;} +} ll power(ll n, ll pow, ll m) { + if (pow == 0) return 1; + if (pow % 2 == 0) { + ll x = power(n, pow / 2, m); + return (x * x) % m; + } + else return (power(n, pow - 1, m) * n) % m; +} ll gcd(ll a, ll b) { + if (!b)return a; + return gcd(b, a % b); +} ll factorial(ll n, ll mod) { + if (n > 1) + return (n * factorial(n - 1, mod)) % mod; + else + return 1; +} ll lcm(ll a, ll b) { + return (a * b) / gcd(a, b); +} vector read(ll n) {vector a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x);} return a;}vector> adj;void init(ll n) {for (ll i = 0; i <= n; i++) { vector a; adj.push_back(a);}} + + +int32_t main() { + ios_base::sync_with_stdio(false);cin.tie(0);; + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + string s, t; + cin >> s >> t; + if(s.size() > t.size()) + { + cout << 0 << '\n'; + return 0; + } + t = (s+'#'+t); + ll pi[t.size()+1]; + pi[0] = 0; + for (ll i = 1; i < t.size(); i++) + { + ll j = pi[i-1]; + while (j > 0 && t[i] != t[j]) + { + j = pi[j-1]; + } + if(t[i] == t[j]) + { + j++; + } + pi[i] = j; + } + vector a; + ll size = 0; + for(ll i = s.size(); i < t.size(); i++) + { + if(pi[i] == s.size() && a.size() <= 1000) + { + a.push_back(i); + } + if(pi[i] == s.size()) + { + size++; + } + } + ll k = a.size(); + ll x = min(k, 1000LL); + cout << size << '\n'; + for(ll i = 0; i < x; i++) + { + cout << a[i]-2*s.size() << " "; + } +} diff --git a/strmatch/kmp/3907c738-536c-4ac0-9150-c8f09f2b5a32.cpp b/strmatch/kmp/3907c738-536c-4ac0-9150-c8f09f2b5a32.cpp new file mode 100644 index 0000000000000000000000000000000000000000..769a3f45875008f9cc4d5943f533bf207eea2349 --- /dev/null +++ b/strmatch/kmp/3907c738-536c-4ac0-9150-c8f09f2b5a32.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +string word, sir; +int N, M; +int lps[2000020]; +void createLPS() { + lps[0] = 0; + int len = 0, i = 1; + while (i < M) { + if (word[i] == word[len]) { + ++len; + lps[i] = len; + ++i; + } + else { + if (len == 0) { + lps[i] = 0; + ++i; + } + else { + len = lps[len - 1]; + } + } + } +} +int main() +{ + vector v; + fin >> word >> sir; + N = sir.length(); + M = word.length(); + createLPS(); + int i = 0, j = 0; + while (i < N) { + while (i < N && j < M && word[j] == sir[i]) { + ++j; + ++i; + if (j == M) { + v.push_back(i - j); + } + } + if (i < N && j <= M) { + if (j) { + j = lps[j - 1]; + } + else + ++i; + } + } + fout << v.size() << "\n"; + for (int i = 0; i < min((int)v.size(), 1000); ++i) { + fout << v[i] << " "; + } + return 0; +} diff --git a/strmatch/kmp/3a2775a0-1a9a-4f06-a567-c3cc1ae01d19.cpp b/strmatch/kmp/3a2775a0-1a9a-4f06-a567-c3cc1ae01d19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28c23725e92531f285a472011040cd146dba13c0 --- /dev/null +++ b/strmatch/kmp/3a2775a0-1a9a-4f06-a567-c3cc1ae01d19.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + +int i, j, sufix[2000005], n, m, ans[1005], k; + +char s[2000005], t[2000005]; + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + f.getline(t, sizeof(t)); + + f.getline(s, sizeof(s)); + + n = strlen(s); + + m = strlen(t); + + j = 0; + + i = 1; + + while(i < m) + + { + + if(t[j] == t[i]) + + { + + sufix[i] = j + 1; + + j ++; + + i ++; + + } + + else if(j == 0)sufix[i] = 0, i ++; + + else j = sufix[j - 1]; + + + + } + + j = 0; + + for(i = 0; i < n; i ++) + + if(s[i] == t[j]) + + { + + j ++; + + if(j == m) + + { + + if(k <= 1002)ans[++ k] = i - m + 1, j = sufix[j - 1]; + + else k ++; + + } + + + + } + + else if(j == 0)continue; + + else j = sufix[j - 1], i --; + + g << k << "\n"; + + for(i = 1; i <= min(k, 1000); i ++) + + g << ans[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/3a4e6b26-6d80-40c0-95f0-90245dedec48.cpp b/strmatch/kmp/3a4e6b26-6d80-40c0-95f0-90245dedec48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6076e0c324d77dc259478d62965d194b294923c9 --- /dev/null +++ b/strmatch/kmp/3a4e6b26-6d80-40c0-95f0-90245dedec48.cpp @@ -0,0 +1,89 @@ +#include //Andrei Alexandru a.k.a Sho10 + + + +using namespace std; + +int n,p[5000005]; + +string second,t; + +vectorans; + +void solve(){ + +t=(second+'#'+t); + +int j=0; + +for(int i=1;i>second>>t; + +solve(); + +cout< + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int prefix[2000005], l1, l2, nr; + +char sir1[2000005], sir2[2000005]; + + + +vectorrez; + + + +void preproc() + +{ + + int i=1, j=0; + + prefix[0]=0; + + while(i> sir1; + + f >> sir2; + + l1=strlen(sir1); + + l2=strlen(sir2); + + preproc(); + + solve(); + + return 0; + +} diff --git a/strmatch/kmp/3bb1f32a-c575-49b9-8c3f-3a374f5de558.cpp b/strmatch/kmp/3bb1f32a-c575-49b9-8c3f-3a374f5de558.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb5a934db8f4647c3d540605d59d4f976e8a8753 --- /dev/null +++ b/strmatch/kmp/3bb1f32a-c575-49b9-8c3f-3a374f5de558.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +using namespace std; + +string A, B; +vector prefix, ans; + +void makePrefix() +{ + int p = 0; + for (int i = 2; i < A.size(); ++i) { + while (p && A[p + 1] != A[i]) + p = prefix[p]; + p += (A[p + 1] == A[i]); + prefix[i] = p; + } +} + +int countMatches() +{ + int p = 0; + int total = 0; + for (int i = 1; i < B.size(); ++i) { + while (p && A[p + 1] != B[i]) + p = prefix[p]; + p += (A[p + 1] == B[i]); + if (p == (int)A.size() - 1) { + ++total; + if (ans.size() < 1000) { + ans.emplace_back(i - (int)A.size() + 1); + p = prefix[p]; + } + } + } + return total; +} + +int main() +{ + ifstream fileIn("strmatch.in"); + ofstream fileOut("strmatch.out"); + + fileIn.sync_with_stdio(false); + fileIn.tie(0); + + fileIn >> A; + fileIn >> B; + A = "#" + A; + B = "#" + B; + + prefix.resize(A.size()); + + makePrefix(); + fileOut << countMatches() << "\n"; + for (auto it : ans) + fileOut << it << " "; + + return 0; +} diff --git a/strmatch/kmp/3c7193bd-2241-4b12-bca0-4017301cad97.cpp b/strmatch/kmp/3c7193bd-2241-4b12-bca0-4017301cad97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61e05fe2883e88cf51a9b0a3e71f2d1099b1aa5f --- /dev/null +++ b/strmatch/kmp/3c7193bd-2241-4b12-bca0-4017301cad97.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char pat[2000005],txt[2000005]; + +int n,m,i,j,len,lps[2000005],v[2000005],nr,val; + +void crearelps(int n,char pat[]) + +{ + + lps[0]=0; + + len=0; + + i=1; + + while(i0) + + len=lps[len-1]; + + else + + lps[i++]=0; + + } + + } + +} + +void kmp() + +{ + + crearelps(m,pat); + + i=j=0; + + while(i0) + + j=lps[j-1]; + + else + + i++; + + } + + } + +} + +int main() + +{ + + fin>>pat; + + m=strlen(pat); + + fin>>txt; + + n=strlen(txt); + + kmp(); + + fout< +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q+1] != A[i]) + + q = pi[q]; + + if (A[q+1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void) + +{ + + int i, q = 0, n = 0; + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + fgets(A, sizeof(A), stdin); + + fgets(B, sizeof(B), stdin); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i-1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i-1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q+1] != B[i]) + + q = pi[q]; + + if (A[q+1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i-M; + + } + + } + + + + printf("%d\n", n); + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + printf("%d ", pos[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/3dea75ce-5955-40cd-b68d-b507635d6071.cpp b/strmatch/kmp/3dea75ce-5955-40cd-b68d-b507635d6071.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d3326dfff57caccfdfbd2fe92d096564cae567e --- /dev/null +++ b/strmatch/kmp/3dea75ce-5955-40cd-b68d-b507635d6071.cpp @@ -0,0 +1,159 @@ +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int lps[2000005]; + +string pat, txt; + + + +void ComputeLps() + +{ + + int len = 0; + + int i = 1; + + lps[0] = 0; + + while(i < pat.size()) + + { + + if(pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if(len != 0) + + { + + len = lps[len-1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +int main() + +{ + + fin>>pat>>txt; + + ComputeLps(); + + int n = txt.size(), m = pat.size(), i = 0, j = 0; + + + + fout<<" \n"; + + int nr=0; + + while(i < n) + + { + + if(txt[i] == pat[j]) + + { + + i++; + + j++; + + } + + if(j == m) + + { + + if(nr < 1000) + + { + + fout< +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +string a,b; + +int pi[4000001],nr; + +vectorsol; + +int main() + +{ + + fin>>a>>b; + + int s=a.size(); + + a=a+"*"+b; + + for(int i=1;i<(int)a.size();i++) + + { + + if(a[pi[i-1]]==a[i]) + + { + + pi[i]=pi[i-1]+1; + + } + + else + + { + + if(pi[i-1]==0) + + continue; + + int j=pi[pi[i-1]-1]; + + while(j>0&&a[j]!=a[i]) + + { + + j=pi[j-1]; + + } + + if(j==0&&a[j]==a[i]) + + pi[i]=1; + + else if(j==0) + + pi[i]=0; + + else pi[i]=j+1; + + } + + if(pi[i]==s) + + { + + if(sol.size()<1000) + + sol.push_back(i-s*2); + + nr++; + + } + + } + + fout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +vector rez; + +int main() + +{ + + string pat, txt; + + f >> pat >> txt; + + vector lps(pat.size(), 0); + + int i = 1, len = 0; + + while (i < pat.size()) + + if (pat[i] == pat[len]) + + len++, lps[i] = len, i++; + + else if (len) + + len = lps[len - 1]; + + else + + i++; + + i = 0; + + int j = 0; + + while (i < txt.size()) + + { + + if (txt[i] == pat[j]) + + i++, j++; + + if (j == pat.size()) + + rez.push_back(i - j), j = lps[j - 1]; + + else if (i < txt.size() && pat[j] != txt[i]) + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + g << rez.size() << '\n'; + + for (int i = 0; i < min(int(rez.size()), 1000); i++) + + g << rez[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/4221c521-3128-4313-9b06-a15c8a90c8fb.cpp b/strmatch/kmp/4221c521-3128-4313-9b06-a15c8a90c8fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..baefda87ea9c5da1100430c6f153d4839d3733a3 --- /dev/null +++ b/strmatch/kmp/4221c521-3128-4313-9b06-a15c8a90c8fb.cpp @@ -0,0 +1,111 @@ +#include +#include + + +using namespace std; + + + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + + + +char word[2000137]; + +int n; + + + + + +char sentence[2000137]; + +int m; + + + +int pi[2000137]; + +int k; + + + +int v[1137]; + +int sorin; + + + +int main() + +{ + + in >> word + 1 >> sentence + 1; + + n = strlen ( word + 1 ); + + m = strlen ( sentence + 1); + + k = 0; + + for ( register int i = 2 ; i <= n ; ++i ) + + { + + while ( k > 0 && word[k+1] != word[i] ) + + k = pi[k]; + + if ( word[k+1] == word[i] ) + + ++k; + + pi[i] = k; + + } + + + + k = 0; + + for ( register int i = 1 ; i <= m ; ++i ) + + { + + while ( k > 0 && word[k+1] != sentence[i] ) + + k = pi[k]; + + if ( word[k+1] == sentence[i] ) + + ++k; + + if ( k == n ) + + { + + ++sorin; + + if ( sorin <= 1000 ) + + v[sorin] = i - n; + + k = pi[k]; + + } + + } + + out << sorin << '\n'; + + sorin = min ( 1000, sorin ); + + for ( register int i = 1 ; i <= sorin ; ++i ) + + out << v[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/42316e92-a055-4277-a02b-6c42ad08fbf4.cpp b/strmatch/kmp/42316e92-a055-4277-a02b-6c42ad08fbf4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64d708043dfa85154976a3b0dd6366de21717658 --- /dev/null +++ b/strmatch/kmp/42316e92-a055-4277-a02b-6c42ad08fbf4.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char sir1[4000005], sir2[2000005]; + +int p[4000005], lg1, lg2; + + + +void f_pref(char s[], int pref[], int n) + +{ + + pref[0] = 0; + + for (int i = 1; i < n; i++) + + { + + int j = i; + + while (j > 0 && s[pref[j - 1]] != s[i]) + + { + + j = pref[j - 1]; + + } + + if (j == 0) + + pref[i] = 0; + + else + + pref[i] = pref[j - 1] + 1; + + } + +} + + + +int main() + +{ + + fin >> sir1 >> sir2; + + lg1 = strlen(sir1); + + lg2 = strlen(sir2); + + sir1[lg1] = '#'; + + for (int i = lg1 + 1; i <= lg1 + lg2; ++i) + + sir1[i] = sir2[i - lg1 - 1]; + + f_pref(sir1, p, lg1 + lg2 + 1); + + int ct = 0; + + for (int i = 0; i <= lg1 + lg2; ++i) + + if (p[i] == lg1) + + ct++; + + + + fout << ct << '\n'; + + ct = 0; + + for (int i = 0; i <= lg1 + lg2; ++i) + + if (p[i] == lg1 && ct < 1000) + + { + + ct++; + + fout << i - lg1 * 2 << ' '; + + } + + return 0; + +} diff --git a/strmatch/kmp/434c7643-887b-49f9-b0bd-8acf53698b84.cpp b/strmatch/kmp/434c7643-887b-49f9-b0bd-8acf53698b84.cpp new file mode 100644 index 0000000000000000000000000000000000000000..516aac85f12cd7627916c9ec414790e8c4756986 --- /dev/null +++ b/strmatch/kmp/434c7643-887b-49f9-b0bd-8acf53698b84.cpp @@ -0,0 +1,103 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +const int NMAX = 2e6+5; + + + +int n,m,pi[NMAX],pos[(1<<10)]; + +char a[NMAX],b[NMAX]; + +void make_prefix() + +{ + + int q=0,i; + + for(i=2,pi[1]=0;i<=n;++i) + + { + + while(q && a[q+1]!=a[i]) + + q=pi[q]; + + if(a[q+1]==a[i]) + + ++q; + + pi[i]=q; + + } + +} + +int main() + +{ + + int q=0,nr=0; + + cin>>a>>b; + + n=strlen(a); + + m=strlen(b); + + for(int i=n;i;i--) + + a[i]=a[i-1]; + + a[0]=' '; + + for(int i=m;i;--i) + + b[i]=b[i-1]; + + b[0]=' '; + + make_prefix(); + + for(int i=1;i<=m;i++) + + { + + while(q && a[q+1]!=b[i]) + + q=pi[q]; + + if(a[q+1]==b[i]) + + q++; + + if(q==n) + + { + + q=pi[n]; + + ++nr; + + if(nr<=1000) + + pos[nr]=i-n; + + } + + } + + cout< +#include +using namespace std; + + + +char sir[2000005], pattern[2000005]; + +int prefix[2000005] {}, rez[1005]; + + + +int main() { + + int lsir = 0, lpattern = 0, i = 0, j = 0, n = 0; + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + + + scanf("%s%s", pattern, sir); + + lsir = strlen(sir); + + lpattern = strlen(pattern); + + + + for(i = lsir; i >= 1; --i) + + sir[i] = sir[i-1]; + + for(i = lpattern; i >= 1; --i) + + pattern[i] = pattern[i-1]; + + + + for(i = 2; i <= lpattern; ++i) { + + while(j && pattern[j + 1] != pattern[i]) + + j = prefix[j]; + + if(pattern[i] == pattern[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + j = 0; + + for(i = 1; i <= lsir; ++i) { + + while(j && sir[i] != pattern[j+1]) + + j = prefix[j]; + + if(sir[i] == pattern[j+1]) + + ++j; + + if(j == lpattern) { + + j = prefix[j]; + + ++n; + + if(n <= 1000) + + rez[n] = i - lpattern; + + } + + } + + + + + + printf("%d\n", n); + + n = (n < 1000 ? n : 1000); + + for(i = 1; i <= n; ++i) + + + + printf("%d ", rez[i]); + + + + printf("\n"); + +} diff --git a/strmatch/kmp/449c052c-ca32-404e-8a36-c00726f3b799.cpp b/strmatch/kmp/449c052c-ca32-404e-8a36-c00726f3b799.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae7a67c16cd452224f14807fe552c198fd5eb3af --- /dev/null +++ b/strmatch/kmp/449c052c-ca32-404e-8a36-c00726f3b799.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char A[2000005], B[2000005]; + +int lps[2000005]; + +vector sol; + + + +void preprocessing(char p[]){ + + int i=1, len=0, lsup = strlen(p); + + lps[0] = 0; + + while(i < lsup){ + + while(p[i] != p[len] && len > 0) + + len = lps[len-1]; + + if(p[i] == p[len]){ + + len++; + + lps[i] = len; + + } + + else + + lps[i] = 0; + + i++; + + } + +} + + + +void KMP(char pattern[], char s[]){ + + int ip=0, is=0, target = strlen(pattern), lsup = strlen(s); + + int contor = 0; + + while(is < lsup) + + { + + while(pattern[ip] != s[is] && ip > 0) + + ip = lps[ip-1]; + + if(pattern[ip] == s[is]) + + ip++; + + if(ip == target){ + + contor++; + + if(contor <= 1000) + + sol.push_back(is-ip+1); + + ip = lps[ip-1]; + + } + + is++; + + } + + g< v){ + + int l = v.size(); + + for(int i=0; i>A>>B; + + preprocessing(A); + + KMP(A,B); + + afisare(sol); + +} diff --git a/strmatch/kmp/44d28a5a-2ffc-4a72-a5c2-4219df590753.cpp b/strmatch/kmp/44d28a5a-2ffc-4a72-a5c2-4219df590753.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e703ef180c6cbe7825cbedf51dcafc6cc621541b --- /dev/null +++ b/strmatch/kmp/44d28a5a-2ffc-4a72-a5c2-4219df590753.cpp @@ -0,0 +1,116 @@ +#include +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q+1] != A[i]) + + q = pi[q]; + + if (A[q+1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void) + +{ + + int i, q = 0, n = 0; + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + fgets(A, sizeof(A), stdin); + + fgets(B, sizeof(B), stdin); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i-1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i-1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q+1] != B[i]) + + q = pi[q]; + + if (A[q+1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i-M; + + } + + } + + + + printf("%d\n", n); + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + printf("%d ", pos[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/44ffabe3-1e71-4400-894d-3df6169452ce.cpp b/strmatch/kmp/44ffabe3-1e71-4400-894d-3df6169452ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a319d911a761012f7edfa04a3f7b4ac67a2c1559 --- /dev/null +++ b/strmatch/kmp/44ffabe3-1e71-4400-894d-3df6169452ce.cpp @@ -0,0 +1,53 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +const int NMAX=2e6; +string A, B; +int ps[2*NMAX+6], k, cnt, q[NMAX+1], vf; + +void citire() { + getline(fin, A); + A.push_back('#'); + k=A.size(); + getline(fin, B); + A+=B; +} + +void build_ps(string s) { + int m=(int)s.size(); + ps[0]=0; + int i=1, cur=0; + while(i + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void Citire(); + +void Pref(); + +void KMP(); + + + +char T[2000005],P[2000005]; + +int n,m,k; + +int a[2000005],urm[2000005]; + + + + + +int main() + +{ + + Citire(); + + KMP(); + + fout<1000) k=1000; + + for(int i=1;i<=k;++i) + + fout<0 && P[i]!=P[j]) j=urm[j-1]; + + if(P[i] == P[j]) ++j; + + urm[i]=j; + + } + +} + + + +void KMP() + +{ + + Pref(); + + int q=0; + + for(int i=0;i0 && P[q]!=T[i]) q=urm[q-1]; + + if(P[q]==T[i]) ++q; + + if(q==n) a[++k]=i-n+1; + + } + +} diff --git a/strmatch/kmp/46459e01-c6a6-48a3-81f0-1d7e62dd98df.cpp b/strmatch/kmp/46459e01-c6a6-48a3-81f0-1d7e62dd98df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9e4be6fe52653ee85e7a1ba2c7d57653952172fe --- /dev/null +++ b/strmatch/kmp/46459e01-c6a6-48a3-81f0-1d7e62dd98df.cpp @@ -0,0 +1,111 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int positions[1000]; + +int pos_size = 0, hide = 0; + +int prefix[20000000]; + +int main() { + + string pat, str; + + cin>>pat>>str; + + if(pat.size() > str.size()) { + + cout<<"0\n"; + + return 0; + + } + + + + prefix[0] = 0; + + for(int x = 1, p = 0; x +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void kmp(string s) { + + int n = s.length(); + + + + vector pi(n); + + for (int i = 1; i < n; i++) { + + int j = pi[i - 1]; + + while (j > 0 && s[i] != s[j]) j = pi[j - 1]; + + if (s[i] == s[j]) j++; + + pi[i] = j; + + + + if (j == a.size()) { + + ans.push_back(i - 2 * a.size()); + + } + + } + + + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + kmp(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + + + return 0; + +} diff --git a/strmatch/kmp/480978b7-cbf6-45f5-8e42-d291a92a3ed1.cpp b/strmatch/kmp/480978b7-cbf6-45f5-8e42-d291a92a3ed1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3453d579c8ec513a062e8ecf591005386dcef824 --- /dev/null +++ b/strmatch/kmp/480978b7-cbf6-45f5-8e42-d291a92a3ed1.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include + + +using namespace std; + + + +int solCnt; + +int n, m; + +int pos[1005], lps[2000005]; + +char pat[2000005], txt[2000005]; + + + + + +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + + + int i = 1; + + while (i < M) { + + if (pat[i] == pat[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + + + + + + + if (len != 0) { + + len = lps[len - 1]; + + + + + + + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +void KMPSearch(char* pat, char* txt) + +{ + + m = strlen(pat); + + n = strlen(txt); + + + + + + computeLPSArray(pat, m, lps); + + + + int i = 0; + + int j = 0; + + while (i < n) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + } + + + + if (j == m) { + + if (solCnt < 1000) + + pos[solCnt++] = i - j; + + else + + solCnt++; + + j = lps[j - 1]; + + } + + + + + + else if (i < n && pat[j] != txt[i]) { + + + + + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + + } + +} + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", pat); + + scanf("%s", txt); + + + + KMPSearch(pat, txt); + + + + printf("%d\n", solCnt); + + for (int i = 0; i < std::min(solCnt, 1000); ++i) + + printf("%d ", pos[i]); + + return 0; + +} diff --git a/strmatch/kmp/4957079b-3af2-47a4-a639-307379cf8869.cpp b/strmatch/kmp/4957079b-3af2-47a4-a639-307379cf8869.cpp new file mode 100644 index 0000000000000000000000000000000000000000..79fded040fdd58d01dd6c21afdb41ec15223c5e6 --- /dev/null +++ b/strmatch/kmp/4957079b-3af2-47a4-a639-307379cf8869.cpp @@ -0,0 +1,62 @@ +#include +#include +#include + + + +using namespace std; + +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int pi[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + scanf("%s\n%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + + int q=0; + for(int i=1;i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int N = 2000041; + + + +int lps[N]; + +char s1[N], s2[N]; + + + +int ans1 = 0; + +vector ans2; + + + +void make_lps(){ + + int j = 0; + + for(int i = 1; s1[i] != '\0'; ++i){ + + if(s1[i] == s1[j]){ + + lps[i] = ++j; + + }else if(j != 0){ + + j = lps[j-1]; + + i--; + + } + + } + +} + + + +void solve(){ + + int j = 0; + + for(int i = 0; s2[i] != '\0'; ++i){ + + if(s1[j] == s2[i]){ + + j++; + + if(s1[j] == '\0'){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-j+1); + + j = lps[j-1]; + + } + + }else if(j != 0){ + + j = lps[j-1]; + + i--; + + } + + } + +} + + + +int main(){ + + + + fin >> s1 >> s2; + + + + make_lps(); + + solve(); + + + + fout << ans1 << "\n"; + + for(auto a : ans2)fout << a << " "; + + return 0; + +} diff --git a/strmatch/kmp/4a84c17e-f745-42ef-b3b3-820fa0b8f9ca.cpp b/strmatch/kmp/4a84c17e-f745-42ef-b3b3-820fa0b8f9ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4b0aa00ef4aac2a40026e90de8b8986b04d622cb --- /dev/null +++ b/strmatch/kmp/4a84c17e-f745-42ef-b3b3-820fa0b8f9ca.cpp @@ -0,0 +1,100 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000005], S[2000005]; + +int N, M, pi[2000005], k, cnt; + + + +vector < int > Ans; + + + +int main() + +{ + + fin >> (s + 1); + + N = strlen(s + 1); + + + + for (int i = 2; i <= N; i++) + + { + + while ((k > 0) && s[k + 1] != s[i]) + + k = pi[k]; + + if (s[k + 1] == s[i]) + + k++; + + pi[i] = k; + + } + + + + fin >> (S + 1); + + M = strlen(S + 1); + + + + k = 0; + + for (int i = 1; i <= M; i++) + + { + + while (k > 0 && s[k + 1] != S[i]) + + k = pi[k]; + + if (s[k + 1] == S[i]) + + k++; + + if (k == N) + + { + + cnt++; + + if (cnt <= 1000) + + Ans.push_back(i - N); + + } + + } + + + + fout << cnt << "\n"; + + for (int poz: Ans) + + fout << poz << " "; + + return 0; + +} diff --git a/strmatch/kmp/4aec7d68-9462-4fb3-89a9-980ff888be63.cpp b/strmatch/kmp/4aec7d68-9462-4fb3-89a9-980ff888be63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0316c7f3c5c4a31144d5b9b425c8eff65122c4ce --- /dev/null +++ b/strmatch/kmp/4aec7d68-9462-4fb3-89a9-980ff888be63.cpp @@ -0,0 +1,76 @@ +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n,m,k,i,nr,sol[1010],p[2000010]; + +char s[2000010],c[2000010]; + + + +int main() + +{ + + f>>(c+1);n=strlen(c+1); + + f>>(s+1);m=strlen(s+1); + + + + for(i=2;i<=n;i++) + + { + + while(k!=0 && c[k+1]!=c[i])k=p[k]; + + if(c[k+1]==c[i])k++; + + p[i]=k; + + } + + + + k=0; + + for(i=1;i<=m;i++) + + { + + while(k!=0 && c[k+1]!=s[i])k=p[k]; + + if(c[k+1]==s[i])k++; + + + + if(k==n) + + { + + nr++; + + if(nr<=1000)sol[nr]=i-n; + + } + + } + + + + g< + + + + + + +const int lMax = 2000005; + + + +using namespace std; + + + +char a[lMax], b[lMax]; + +int lga, k, lgb, ans1; + +int pi[lMax]; + +vector ans2; + + + +main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + + + cin >> a + 1 >> b + 1; + + lga = strlen(a + 1); + + lgb = strlen(b + 1); + + + + k = 0; + + + + for (int i = 2; i <= lga; ++i) + + { + + k = pi[i - 1]; + + while (k && a[k + 1] != a[i]) + + k = pi[k]; + + + + if (a[k + 1] == a[i]) + + ++k; + + pi[i] = k; + + } + + + + k = 0; + + + + for (int i = 1; i <= lgb; ++i) + + { + + while (k && a[k + 1] != b[i]) + + k = pi[k]; + + if (a[k + 1] == b[i]) + + ++k; + + if (k == lga) + + { + + ++ans1; + + if (ans2.size() < 1000) + + ans2.emplace_back(i - lga); + + } + + } + + + + cout << ans1 << "\n"; + + for (auto i : ans2) + + cout << i << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/4efa1e95-1643-4ba4-885e-d77c6f8c6310.cpp b/strmatch/kmp/4efa1e95-1643-4ba4-885e-d77c6f8c6310.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2d14bf42ab4d076dd6d491d67535ddcdace95cd --- /dev/null +++ b/strmatch/kmp/4efa1e95-1643-4ba4-885e-d77c6f8c6310.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX = 2000005; + +string a, b; + +int p[NMAX], cnt, size_a, size_b; + +vector v; + + + +void kmp() + +{ + + int k = 0; + + for (int i = 1; i <= size_b; ++i) + + { + + while (k && a[k + 1] != b[i]) + + k = p[k]; + + + + if (a[k + 1] == b[i]) ++k; + + + + if (k == size_a) + + { + + cnt++; + + if (cnt <= 1000) + + v.push_back(i - size_a); + + } + + } + +} + + + +void make_prefix() + +{ + + int k = 0; + + for (int i = 2; i <= size_a; ++i) + + { + + while (k && a[k + 1] != a[i]) + + k = p[k]; + + + + if (a[k + 1] == a[i]) ++k; + + + + p[i] = k; + + } + +} + + + +int main() + +{ + + fin >> a >> b; + + + + size_a = a.size(); + + size_b = b.size(); + + + + a.insert(a.begin(), 0); + + b.insert(b.begin(), 0); + + + + make_prefix(); + + + + kmp(); + + + + fout << cnt << "\n"; + + + + for (size_t i = 0; i < v.size(); ++i) + + fout << v[i] << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/50f617a9-dcd1-48bf-9196-a50ca49e9105.cpp b/strmatch/kmp/50f617a9-dcd1-48bf-9196-a50ca49e9105.cpp new file mode 100644 index 0000000000000000000000000000000000000000..30541defe749c129ddb370359a4e3fecb417e45a --- /dev/null +++ b/strmatch/kmp/50f617a9-dcd1-48bf-9196-a50ca49e9105.cpp @@ -0,0 +1,100 @@ +#include + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +string N, M; + +int v[2000001]; + +vector sol; + +void make_prefix (string N) + +{ + + int n = N.size (); + + int j = 0; + + for (int i = 1; i < n; i++) + + { + + if (N[i] != N[j]) + + while (j > 0 && N[j] != N[i]) + + j = v[j - 1]; + + if (N[j] == N[i]) + + ++j; + + v[i] = j; + + } + +} + +void match (string N, string M) + +{ + + int m = M.size (); + + int n = N.size (); + + int i = 0; + + for (int j = 0; j < m; j++) + + { + + if (N[i] != M[j]) + + while (i > 0 && M[j] != N[i]) + + i = v[i - 1]; + + if (N[i] == M[j]) + + i++; + + if (i == n) + + { + + sol.push_back (j - i + 1); + + i = v[i - 1]; + + } + + } + +} + +int main () + +{ + + fin >> N >> M; + + make_prefix (N); + + match (N, M); + + fout << sol.size () << "\n"; + + int n = sol.size (); + + for (int i = 0; i < min (n , 1000); i++) + + fout << sol[i] << " "; + +} diff --git a/strmatch/kmp/519c8e67-fa0f-43e7-82d7-ea2a45057ae4.cpp b/strmatch/kmp/519c8e67-fa0f-43e7-82d7-ea2a45057ae4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..109b323c2f167c53d41a496d5613078a4c33a666 --- /dev/null +++ b/strmatch/kmp/519c8e67-fa0f-43e7-82d7-ea2a45057ae4.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + +const int NMAX = 2000100; + + + +int ans; + +int lg1, lg2; + +int pi_n[NMAX], pos[NMAX]; + +char N[NMAX], M[NMAX]; + + + +void cal_pi_n(){ + + int k = 0; + + for(int i = 2; i <= lg1; i++){ + + while(k > 0 && N[k + 1] != N[i]) + + k = pi_n[k]; + + if(N[k + 1] == N[i]) + + k++; + + pi_n[i] = k; + + } + +} + + + +void cal_pi_m(){ + + int k = 0; + + for(int i = 1; i <= lg2; i++){ + + while(k > 0 && N[k + 1] != M[i]) + + k = pi_n[k]; + + if(N[k + 1] == M[i]) + + k++; + + if(k == lg1) + + pos[++ans] = i - lg1; + + } + +} + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s%s", N + 1, M + 1); + + lg1 = strlen(N + 1); + + lg2 = strlen(M + 1); + + cal_pi_n(); + + cal_pi_m(); + + printf("%d\n", ans); + + for(int i = 1; i <= min(ans, 1000); i++) + + printf("%d ", pos[i]); + + + + + + return 0; + +} diff --git a/strmatch/kmp/539e7a76-63f0-4b05-8223-523cced2a21a.cpp b/strmatch/kmp/539e7a76-63f0-4b05-8223-523cced2a21a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70f82e7728897dddd2f3fd4a639deb3cd5a1b1bd --- /dev/null +++ b/strmatch/kmp/539e7a76-63f0-4b05-8223-523cced2a21a.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + +char text[2000005],secv[2000005]; + +int cnt; + +int lps[2000005]; + +int lungimeText,lungimeSecv; + +vectorpoz; + +void formare_lps() + +{ + + int st = 0,i = 1; + + + + while(i> secv>> text; + + lungimeSecv = strlen(secv); + + lungimeText = strlen(text); + + formare_lps(); + + verificare_secv_in_text(); + + int lungime_v = poz.size(); + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char p[2000001], t[2000001]; + +int f[2000001]; + +vector < int > poz; + + + +int kmp(char p[], char t[], vector < int > &poz) + +{ + + int n, m, i, j, nr; + + n = strlen(t); + + m = strlen(p); + + + + f[0] = 0; + + i = 0, j = 1; + + while (j < m) + + { + + while (j < m && p[i] != p[j]) + + { + + f[j] = 0; + + j++; + + } + + + + if (j < m) + + { + + if (p[i] == p[j]) + + { + + f[j] = i + 1; + + i++, j++; + + } + + if (p[i] != p[j]) + + i = f[i-1]; + + } + + } + + + + nr = 0; + + i = j = 0; + + while (i < n) + + { + + if (t[i] == p[j]) + + { + + if (j == m - 1) + + { + + nr++; + + poz.push_back(i - m + 1); + + i++, j = f[j]; + + } + + else + + i++, j++; + + } + + else if (j > 0) + + j = f[j-1]; + + else + + i++; + + } + + + + return nr; + +} + + + +int main() + +{ + + fin >> p >> t; + + fout << kmp(p, t, poz) << "\n"; + + for (int i=0; i +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int lps[2000010],v[1010],n,m; + +char A[2000010],B[2000010]; + +void BuildLPS() + +{ + + int i,k=0; + + lps[1]=0; + + for (i=2;i<=n;i++) + + { + + while (k>0 && B[i-1]!=B[k]) k=lps[k]; + + if (B[i-1]==B[k]) k++; + + lps[i]=k; + + } + +} + +void KMP() + +{ + + int k=0,q=0,i; + + for (i=0;i0 && A[i]!=B[q]) q=lps[q]; + + if (B[q]==A[i]) q++; + + if (q==n) { + + k++; + + if (k<=1000) v[k]=i-n+1; + + } + + } + + g<=1000) k=1000; + + for (i=1;i<=k;i++) g< +using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector vi; typedef vector vl; typedef pair pii; typedef pair pll; typedef vector vii; typedef vector vll; + +template std::istream& operator>>(std::istream& stream, vector &v) { for (T& t: v) { stream >> t; } return stream; } template std::ostream& operator<<(std::ostream& stream, const vector &v) { for (const T& t : v) { stream << t << ' '; } return stream; } template std::istream& operator>>(std::istream& stream, pair& p) { return stream >> p.first >> p.second; } template std::ostream& operator<<(std::ostream& stream, const pair& p) { return stream << p.first << ' ' << p.second << ' '; } + +template void read(T& t) { cin >> t; } template void read(T& t, TArgs&&... args) { read(t); read(forward(args)...); } template void readc(T& t); template<> void readc(int& v) { scanf("%d", &v); } template<> void readc(unsigned int& v) { scanf("%u", &v); } template<> void readc(char& v) { scanf(" %c", &v); } template<> void readc(long long& v) { scanf("%lld", &v); } template<> void readc(unsigned long long& v) { scanf("%llu", &v); } template<> void readc(float& v) { scanf("%f", &v); } template<> void readc(double& v) { scanf("%lf", &v); } template void readc(char (&str)[sz]) { scanf("%s", str); } template<> void readc(char*& str) { scanf("%s", str); } template void readc(pair& v) { readc(v.first); readc(v.second); } template void readc(vector& v) { for (E& e : v) readc(e); } template void readc(T& t, TArgs&&... args) { readc(t); readc(forward(args)...); } + +template void printc(T t); template<> void printc(int v) { printf("%d ", v); } template<> void printc(unsigned int v) { printf("%u ", v); } template<> void printc(char v) { printf("%c ", v); } template<> void printc(long long v) { printf("%lld ", v); } template<> void printc(unsigned long long v) { printf("%llu ", v); } template<> void printc(float v) { printf("%f ", v); } template<> void printc(double v) { printf("%lf ", v); } template<> void printc(char *str) { printf("%s ", str); } template<> void printc(const char *str) { printf("%s ", str); } template void printc(const pair& v) { printc(v.first); printc(v.second); } template void printc(const vector& v) { for (const E& e : v) printc(e); } template void printc(const T& t, TArgs&&... args) { printc(t); printc(forward(args)...); } + +template void __DEBUG_BASE(const T& t) { cout << t; } template void __DEBUG_BASE(const T& t, TArgs&&... args) { __DEBUG_BASE(t); cout << ", "; __DEBUG_BASE(forward(args)...); } template void __DEBUG_IMPL(TArgs&&... args) { __DEBUG_BASE(forward(args)...); } + +char lower(char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; return ch; } char upper(char ch) { if (ch >= 'a' && ch <= 'z') return ch - 'a' + 'A'; return ch; } array VOWELS = {'a','e','i','o','u'}; bool isvowel(char ch) { return find(VOWELS.begin(), VOWELS.end(), lower(ch)) != VOWELS.end(); }; bool isupper(char ch) { return ch >= 'A' && ch <= 'Z'; } bool islower(char ch) { return ch >= 'a' && ch <= 'z'; } +int main() +{ + ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); + + freopen("strmatch" ".in", "r", stdin); freopen("strmatch" ".out", "w", stdout); + + constexpr int mxN = 2e6 + 6; + char A[mxN], B[mxN]; + scanf("%2000000s %2000000s", A, B); + + int m = strlen(B); + int n = strlen(A); + vector pref(n); + + for (int j = 1, i = 0; j < n; j++) { + while (i && A[i] != A[j]) { + i = pref[i - 1]; + } + if (A[i] == A[j]) { + ++i; + } + pref[j] = i; + } + + vector pos; + + { + int j = 0; + int i = 0; + while (j < m) { + while (i && B[j] != A[i]) { + i = pref[i-1]; + } + if (B[j] == A[i]) { + ++i; + } + if (i == n) { + pos.push_back(j - i + 1); + i = pref[i - 1]; + } + ++j; + } + } + printf("%d\n", (int)pos.size()); + for (size_t i = 0; i < min((size_t)1000, pos.size()); ++i) { + printf("%d ", pos[i]); + } + puts(""); +} diff --git a/strmatch/kmp/55905f2d-d19e-422c-b441-c49e7a7bdb87.cpp b/strmatch/kmp/55905f2d-d19e-422c-b441-c49e7a7bdb87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa5ee699885555251067227ce7f414376febc8a5 --- /dev/null +++ b/strmatch/kmp/55905f2d-d19e-422c-b441-c49e7a7bdb87.cpp @@ -0,0 +1,53 @@ +#include + +const int LMAX = 2000005; + +std::string p, t; +int pref[LMAX]; + +void build_pref() { + for (int i = 1, m = 0; i < p.size(); ++i) { + if (p[i] == p[m]) + pref[i] = ++m; + else if (m != 0) + m = pref[m - 1], --i; + else + pref[i] = 0; + } +} + +void kmp() { + std::vector matches; + + for (int i = 0, j = 0; j < t.size();) { + if (p[i] != t[j]) { + if (i == 0) + ++j; + else + i = pref[i - 1]; + } else { + ++i, ++j; + + if (i == p.size()) { + matches.push_back(j - i); + i = pref[i - 1]; + } + } + + } + + std::cout << matches.size() << '\n'; + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + std::cout << matches[i] << ' '; +} + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + std::cin >> p >> t; + + build_pref(); + kmp(); +} diff --git a/strmatch/kmp/5703e318-d8fd-40bc-ad91-11a75dac2d48.cpp b/strmatch/kmp/5703e318-d8fd-40bc-ad91-11a75dac2d48.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf9941d51ef88a2ab7460f04fe5dcfaf765b1351 --- /dev/null +++ b/strmatch/kmp/5703e318-d8fd-40bc-ad91-11a75dac2d48.cpp @@ -0,0 +1,178 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +int const maxim = 2000005; + + + +vector stocare; + + + + + +void compute_lps(string model, int dimensiune_model, int* lps) { + + + + int len = 0; + + lps[0] = 0; + + + + int i = 1; + + while (i < dimensiune_model) { + + if (model[i] == model[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else { + + if (len != 0) { + + len = lps[len - 1]; + + } + + else { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + + + +void kmp(string model, string sir) { + + + + int dimensiune_model = model.size(); + + int dimensiune_sir = sir.size(); + + + + int* lps= new int[dimensiune_model]; + + + + compute_lps(model, dimensiune_model, lps); + + + + int index_model = 0; + + int index_sir = 0; + + while (index_sir < dimensiune_sir) { + + if (model[index_model] == sir[index_sir]) { + + index_model++; + + index_sir++; + + } + + if (index_model == dimensiune_model) { + + stocare.push_back(index_sir - index_model); + + index_model = lps[index_model - 1]; + + } + + else if (index_sir < dimensiune_sir && model[index_model] != sir[index_sir]) { + + if (index_model == 0) { + + index_sir++; + + } + + else { + + index_model = lps[index_model - 1]; + + } + + } + + } + +} + + + +void afisare() { + + out << stocare.size() << endl; + + int x = stocare.size(); + + for (size_t i = 0; i < min(x, 1000); i++) { + + out << stocare[i] << " "; + + } + +} + + + + + + + +int main() { + + string model; + + string sir; + + + + in >> model; + + in >> sir; + + + + kmp(model, sir); + + afisare(); + + return 0; + +} diff --git a/strmatch/kmp/58203638-cef4-465e-ac4c-f865e453f4bc.cpp b/strmatch/kmp/58203638-cef4-465e-ac4c-f865e453f4bc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89b0ee69fe3d0e4a174f573f998a546603b1779f --- /dev/null +++ b/strmatch/kmp/58203638-cef4-465e-ac4c-f865e453f4bc.cpp @@ -0,0 +1,72 @@ +#include + + +using namespace std; + + + +vector matches (string s, int target) { + + vector pi (s.size ()); + + pi[0] = 0; + + vector sol; + + for (int i = 1; i < s.size (); i++) { + + int cur = pi[i - 1]; + + while (cur && s[i] != s[cur]) + + cur = pi[cur - 1]; + + if (s[i] == s[cur]) + + cur++; + + pi[i] = cur; + + if (pi[i] == target) + + sol.push_back (i - 2 * target); + + } + + return sol; + +} + + + +int main () { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + + + string a, b; + + cin >> a >> b; + + string c = a + "#" + b; + + vector sol = matches (c, a.size ()); + + cout << sol.size () << "\n"; + + if (sol.size () > 1000) + + sol.resize (1000); + + for (int x : sol) + + cout << x << " "; + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/kmp/598d4e3a-32d1-4b8a-b86c-18380794526a.cpp b/strmatch/kmp/598d4e3a-32d1-4b8a-b86c-18380794526a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b184cb0617dfb7010e57381a500ec07329ade708 --- /dev/null +++ b/strmatch/kmp/598d4e3a-32d1-4b8a-b86c-18380794526a.cpp @@ -0,0 +1,80 @@ +#include +#include + + +using namespace std; + +const int N = 2e6; +const int M = 2e6; +char txt[N], pat[M]; +int lps[M]; +int result[N]; +int n, m; + +ifstream fin ( "strmatch.in" ); +ofstream fout ( "strmatch.out" ); + +void process_lps () { + + lps[0] = 0; + + int i = 1, len = 0; + while ( i < m ) { + if ( pat[i] == pat[len] ) { + len ++ ; + lps[i] = len; + i ++; + } + else if ( len != 0 ) + len = lps[len-1]; + else + len = 0, i ++; + } +} + + +int main() + +{ + + char ch; + m = 0; + ch = fin.get (); + + while ( ch != '\n' ){ + pat[m++] = ch; + ch = fin.get (); + } + + n = 0, ch = fin.get (); + while ( ch != EOF && ch != '\n' ){ + txt[n++] = ch; + ch = fin.get (); + } + + process_lps (); + + int i = 0, j = 0; + int n_res = 0; + while ( i < n ) { + if ( pat[j] == txt[i] ) + i ++, j ++; + if ( j == m ) { + result[n_res++] = i - j; + j = lps[j-1]; + } + else if ( i < n && pat[j] != txt[i] ){ + if ( j > 0 ) + j = lps[j-1]; + else + i ++; + } + } + + fout << n_res << '\n'; + for ( int i = 0; i < min ( n_res, 1000 ); i ++ ) + fout << result[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/5b195179-c2b5-4fbf-991b-ecdabb92b444.cpp b/strmatch/kmp/5b195179-c2b5-4fbf-991b-ecdabb92b444.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72cd2b0a2d94cee94681d6bbd1908918f8dbc83d --- /dev/null +++ b/strmatch/kmp/5b195179-c2b5-4fbf-991b-ecdabb92b444.cpp @@ -0,0 +1,72 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b,s; + +int urm[4000005],k,i,sol[4000005],numar; + +int main() + +{ + + f>>a; + + f>>b; + + s=a+'#'+b; + + urm[0]=0; + + k=0; + + for (i=1;i0&&s[i]!=s[k]) + + { + + k=urm[k-1]; + + } + + if (s[i]==s[k]) + + { + + k++; + + } + + urm[i]=k; + + if (urm[i]==a.size()&&i>a.size()) + + { + + sol[++numar]=i; + + } + + } + + g< + + +using namespace std; + + + + + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX=2000005; + +char a[NMAX],b[NMAX]; + +int p[NMAX],poz[1024],k,n,m; + + + +int main() + +{ + + + + fin >> a >> b; + + n=strlen(a); m=strlen(b); + + + + for(int i=n;i;i--) + + a[i]=a[i-1]; + + int q=0; + + for(int i=2;i<=n;i++) + + { + + while(q and a[q+1]!=a[i]) + + q=p[q]; + + if(a[i]==a[q+1]) + + q++; + + p[i]=q; + + } + + q=0; + + for(int i=0;i +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +int aparitii[2000001], k=0; + +char A[2000001], B[2000001]; + + + +void creareLps(char pat[], int M, int lps[]){ + + + + int len = 0; + + lps[0] = 0; + + + + int i = 1; + + while (i < M) { + + if (pat[i] == pat[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else{ + + if (len != 0) + + len = lps[len - 1]; + + else{ + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +void KMPSearch(char pat[], char txt[]) + +{ + + int M = strlen(pat); + + int N = strlen(txt); + + + + int lps[M]; + + + + creareLps(pat, M, lps); + + + + int i = 0; + + int j = 0; + + while (i < N) { + + if (pat[j] == txt[i]) + + j++, i++; + + + + if (j == M) { + + aparitii[++k]=i-j; + + j = lps[j - 1]; + + } + + else if (i < N && pat[j] != txt[i]){ + + if (j != 0) j = lps[j - 1]; + + else i = i + 1; + + } + + } + +} + + + +int main() + +{ + + in.getline(A,2000001); + + in.getline(B,2000001); + + + + KMPSearch(A,B); + + + + out<1000)k=1000; + + + + for(int i=1; i<=k; ++i) + + out< +#include +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int ans; + +vector sol; + +string s1, s2, s; + +int phi[2*2000000 +10]; + + + +int main() + +{ + + f >> s1 >> s2; + + s = s1 + '&' + s2; + + int n1 = s1.size(), n2 = s2.size(), n = s.size(); + + int i = 1, j = 0; + + while(i < n) + + { if(s[i] == s[j]) phi[i] = j + 1, i++, j++; + + else + + { if(j > 0) j = phi[j-1]; + + else i++; + + } + + } + + for(int i=n1+1; i +#include + + + + +char s[2000001], t[2000001]; + +int pi[2000001]; + +void compute () { + + int i=1, len=0; + + while (isalnum(t[i])){ + + if (t[i]==t[len]) + + pi[i++]=++len; + + else + + if (len) + + len=pi[len-1]; + + else + + pi[i++]=0; + + } + +} + +int main (void) { + + FILE *fin=fopen ("strmatch.in", "r"), + + *fout=fopen ("strmatch.out", "w"); + + fgets(t, sizeof t, fin); + + fgets(s, sizeof s, fin); + + + + fprintf(fout, " \n"); + + compute(); + + int i=0, j=0, ct=0; + + while (isalnum(s[i])) + + if (s[i]==t[j]) { + + ++i, ++j; + + if (!isalnum(t[j])) { + + ++ct; + + if (ct<=1000) + + fprintf (fout, "%d ", i-j); + + j=pi[j-1]; + + } + + } + + else + + if (j) + + j=pi[j-1]; + + else + + ++i; + + fseek(fout, 0, SEEK_SET); + + fprintf(fout, "%d", ct); + + return 0; + +} diff --git a/strmatch/kmp/5ee8dad1-c24d-45ab-9a9f-4f99c14889dd.cpp b/strmatch/kmp/5ee8dad1-c24d-45ab-9a9f-4f99c14889dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..322707b342a2b6b70afe334b5238d97e03999a82 --- /dev/null +++ b/strmatch/kmp/5ee8dad1-c24d-45ab-9a9f-4f99c14889dd.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000010],b[2000010]; + +int n,m,i,p[2000010],k,sol,s[2000010]; + +int main() + +{ + + fin>>a+1>>b+1; + + n=strlen(a+1);m=strlen(b+1); + + for(i=2;i<=n;i++) + + { + + while(k!=0&&a[i]!=a[k+1]) k=p[k]; + + if(a[i]==a[k+1]) k++; + + p[i]=k; + + }k=0; + + for(i=1;i<=m;i++) + + { + + while(k!=0&&b[i]!=a[k+1]) k=p[k]; + + if(b[i]==a[k+1]) k++; + + if(k==n) s[++sol]=i-n; + + } + + fout< + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +string a, b; + +int lps[4000002]; + + + + + +int main() + +{ + + fin >> a >> b; + + int m = a.size(); + + a = a + '#' + b; + + int n = a.size(); + + int i = 1, l = 0; + + while (i < n) { + + if (a[i] == a[l]) { + + l++; + + lps[i] = l; + + i++; + + } + + else { + + if (l > 0) + + l = lps[l - 1]; + + else { + + l = 0; + + i++; + + } + + } + + } + + vectorans; + + for (int i = 2 * m; i < n; i++) + + if (lps[i] == m) + + ans.push_back(i); + + fout << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fout << ans[i] - 2 * m << " "; + + return 0; + +} diff --git a/strmatch/kmp/5ffaf8c3-1350-418f-8c0f-6871210f77c9.cpp b/strmatch/kmp/5ffaf8c3-1350-418f-8c0f-6871210f77c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87caf7147358b71b7b94b571917a1a86feedf6dd --- /dev/null +++ b/strmatch/kmp/5ffaf8c3-1350-418f-8c0f-6871210f77c9.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; +int Pi[4000005]; +char s[4000005]; +ofstream fout ("strmatch.out"); +void KMP (int n, int l) +{ + int count = 0; + Pi[1] = 0; + for (int i = 2; i<=n; ++i) + { + int k = i - 1; + while (k && s[i] != s[Pi[k] + 1]) { + k = Pi[k]; + } + if (s[i] == s[Pi[k] + 1]) { + Pi[i] = Pi[k] + 1; + } else { + Pi[i] = 0; + } + if (Pi[i] == l) { + count += 1; + } + } + fout << count << '\n'; + count = 0; + for (int i = 2; i <= n; ++i) { + if (Pi[i] == l) + { + ++count; + if (count<=1000) + fout << i-l-l-1 << ' '; + } + } +} +int main() +{ + ifstream fin ("strmatch.in"); + fin >> (s+1); + int n = strlen(s+1), l; + l = n; + s[n+1] = '%'; + fin >> (s+n+2); + n = strlen(s+1); + KMP(n, l); + return 0; +} diff --git a/strmatch/kmp/612be3ed-1875-4cb3-a519-291d3925dde9.cpp b/strmatch/kmp/612be3ed-1875-4cb3-a519-291d3925dde9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95d008af8a67a10b40c800b24b12894775bb4007 --- /dev/null +++ b/strmatch/kmp/612be3ed-1875-4cb3-a519-291d3925dde9.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + + + +char A[2000001], B[2000001]; + +int pi[2000001]; + +void MakePi(char* A, int leng, int pi[]) { + + pi[0] = 0; + + int lasti = 0; + + int i = 1; + + + + while (i < leng) { + + if (A[lasti] == A[i]) { + + pi[i++] = ++lasti; + + } + + else { + + if (lasti != 0) + + lasti = pi[lasti - 1]; + + else + + pi[i++] = 0; + + } + + } + +} + + + +int main() + +{ + + int nr = 0; + + + + vector ind; + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + int M = strlen(A); + + int N = strlen(B); + + MakePi(A, M, pi); + + if (M > N) + + { + + { + + printf("0\n"); + + return 0; + + } + + } + + int j = 0; + + + + int i = 0; + + while (i < N) { + + if (A[j] == B[i]) { + + j++; + + i++; + + } + + if (j == M) { + + nr++; + + ind.push_back(i - M); + + j = pi[j - 1]; + + } + + else if (i < N && A[j] != B[i]) { + + if (j != 0) + + j = pi[j - 1]; + + else + + i++; + + } + + } + + printf("%d\n", nr); + + for (int i = 0; i < ((ind.size() < 1000) ? ind.size() : 1000); i++) { + + printf("%d ", ind[i]); + + } + + return 0; + +} diff --git a/strmatch/kmp/63f1c7fb-6465-4a47-a9d1-4a5871b1bb2a.cpp b/strmatch/kmp/63f1c7fb-6465-4a47-a9d1-4a5871b1bb2a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9f5e4c539f26a38f1934d4e20a1d73524e0af80 --- /dev/null +++ b/strmatch/kmp/63f1c7fb-6465-4a47-a9d1-4a5871b1bb2a.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; +string pattern, text; + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + cin.tie(NULL); + ios_base::sync_with_stdio(false); + + cin >> pattern >> text; + int N = pattern.size(), M = text.size(); + + vector pi(N, 0); + for (int idx = 1; idx < N; idx++) { + int len = pi[idx - 1]; + while (len > 0 && pattern[idx] != pattern[len]) { + len = pi[len - 1]; + } + if (pattern[len] == pattern[idx]) { + len++; + } + pi[idx] = len; + } + + vector sol; + sol.reserve(1000); + int totalSolutions = 0, len = 0; + for (int idx = 0; idx < M; idx++) { + while (len > 0 && text[idx] != pattern[len]) { + len = pi[len - 1]; + } + if (text[idx] == pattern[len]) { + len++; + } + if (len == N) { + totalSolutions++; + if (totalSolutions <= 1000) { + sol.push_back(idx - N + 1); + } + len = pi[len - 1]; + } + } + + cout << totalSolutions << "\n"; + for (auto idx : sol) { + cout << idx << " "; + } + cout << "\n"; + return 0; +} diff --git a/strmatch/kmp/662d1c71-fd32-4b70-b4be-c76abd223244.cpp b/strmatch/kmp/662d1c71-fd32-4b70-b4be-c76abd223244.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b63fe3798efca78d167feafc03369a5edef2d136 --- /dev/null +++ b/strmatch/kmp/662d1c71-fd32-4b70-b4be-c76abd223244.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int LMAX = 2000000; + + + +char p[LMAX + 5], s[LMAX + 5]; + +int urm[LMAX + 5]; + + + +void prefix(char *pattern, int *urmp) { + + int l = strlen(pattern); + + + + urmp[1] = 0; + + for(int i = 2; i <= l; i++) { + + int k = urmp[i - 1]; + + + + while(k > 0 && pattern[i - 1] != pattern[k]) + + k = urmp[k]; + + + + if(pattern[i - 1] == pattern[k]) + + k++; + + urmp[i] = k; + + } + +} + + + +void findp(char *pattern, int *urmp, char *str) { + + int crt_match = 0; + + int lp = strlen(pattern), ls = strlen(str); + + vector sol; + + + + for(int i = 1; i <= ls; i++) { + + while(crt_match > 0 && str[i - 1] != pattern[crt_match]) + + crt_match = urm[crt_match]; + + + + if(str[i - 1] == pattern[crt_match]) + + crt_match++; + + if(crt_match == lp) { + + sol.push_back(i - lp); + + crt_match = urm[crt_match]; + + } + + } + + + + printf("%d\n", sol.size()); + + int cnt = 0; + + for(int match: sol) { + + printf("%d ", match); + + cnt++; + + if(cnt == 1000) + + break; + + } + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s%s", p, s); + + + + prefix(p, urm); + + findp(p, urm, s); + + + + return 0; + +} diff --git a/strmatch/kmp/667747ea-b19d-429b-859d-40cdca814bc7.cpp b/strmatch/kmp/667747ea-b19d-429b-859d-40cdca814bc7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0c9c6a7fada927b772806f6d91223b3c808d624 --- /dev/null +++ b/strmatch/kmp/667747ea-b19d-429b-859d-40cdca814bc7.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char a[2000001]; + +char b[2000001]; + +int n; + +int pi[2000001]; + +int sol[1000]; + +int nrsol; +int main() + +{ + + f>>a>>b; + + f.close(); + + int i,j; + + pi[0]=0; + + j=0; + + i=1; + + while(a[i]) + + { + + if(a[i]==a[j]) + + { + + pi[i]=j+1; + + i++; + + j++; + + } + + else if(j==0) + + pi[i++]=0; + + else + + j=pi[j-1]; + + } + + n=i; + + i=j=0; + while(b[j]) + + { + + if(a[i]==b[j]) + + { + + i++; + + j++; + + if(!a[i]) + + { + + + + if(nrsol<1000) + + sol[nrsol]=j-n; + + nrsol++; + } + + } + + else if(i==0) + + j++; + + else + + i=pi[i-1]; + + } + + g< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 2000005; + + + +int lps[nmax]; + + + +int c; + + + +string t, p; + + + +void CreateLPS() + +{ + + int k = 1, len = 0, s = p.length(); + + + + while (k < s) + + { + + if (p[k] == p[len]) + + lps[k++] = ++len; + + else + + { + + if (len) + + len = lps[len - 1]; + + else + + k++; + + } + + } + +} + + + +int main() + +{ + + fout << " \n"; + + fin >> p >> t; + + + + CreateLPS(); + + + + int i = 0, j = 0, s = t.length(); + + + + while (i < s) + + { + + if (t[i] == p[j]) + + { + + i++; + + j++; + + } + + + + if (j == p.length()) + + { + + if (c < 1000) + + fout << i - j << ' '; + + + + c++; + + + + j = lps[j - 1]; + + } + + else if (t[i] != p[j]) + + { + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + } + + + + fout.close(); + + fout.open("strmatch.out", ios::in|ios::out); + + + + fout << c; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/68553677-cfe9-4558-8808-548dab3806eb.cpp b/strmatch/kmp/68553677-cfe9-4558-8808-548dab3806eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e076d422ba2345aa8fde3935c5041a704df3e43f --- /dev/null +++ b/strmatch/kmp/68553677-cfe9-4558-8808-548dab3806eb.cpp @@ -0,0 +1,68 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int n,m,k,i,p[2000010],cnt,sol[1010]; + +char A[2000010],B[2000010]; + +int main() { + + fin>>(A+1); + + fin>>(B+1); + + n=strlen(A+1), m=strlen(B+1); + + for (i=2;i<=n;i++) { + + while (k&&A[k+1]!=A[i]) + + k=p[k]; + + if (A[k+1]==A[i]) + + k++; + + p[i]=k; + + } + + k=0; + + for (i=1;i<=m;i++) { + + while (k&&A[k+1]!=B[i]) + + k=p[k]; + + if (A[k+1]==B[i]) + + k++; + + if (k==n) { + + cnt++; + + if (cnt<=1000) + + sol[cnt]=i-n; + + } + + } + + fout< + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char t[2000005],p[2000005]; + +int a[2000005],urm[2000005]; + +int n,m,k; + + + +void citire() + +{ + + fin.getline(p,2000005); + + fin.getline(t,2000005); + + n=strlen(p);m=strlen(t); + + fin.close(); + +} + +void prefix() + +{ + + int j=0; + + for(int i=1;i0&&p[i]!=p[j]) + + j=urm[j-1]; + + if(p[i]==p[j]) j++; + + urm[i]=j; + + } + +} + +void kmp() + +{ + + int q=0; + + for(int i=0;i0&&p[q]!=t[i]) + + q=urm[q-1]; + + if(p[q]==t[i])q++; + + if(q==n) + + a[k++]=i-n+1; + + } + +} + +int main() + +{ + + citire(); + + prefix(); + + kmp(); + + fout<1000) k=1000; + + for(int i=0;i +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char subsecventa[2000000]; + +int lung_subseventa; + +int lung_sir; + +char sir[2000000]; + + + +int LPS[2000000]; + + + +vector sol; + + + +void construct_LPS() + +{ + + int i = 1; + + int j = 0; + + while(i < lung_subseventa) + + { + + if(subsecventa[i] == subsecventa[j]) + + LPS[i] = j + 1, j++; + + else if(j != 0) + + j = LPS[j - 1], i--; + + else + + LPS[i] = 0; + + i++; + + } + +} + + + +void KMP() + +{ + + int i = 0; + + int j = 0; + + while(j < lung_sir) + + { + + if(subsecventa[i] != sir[j]) + + { + + if(i == 0) + + j++; + + else + + i = LPS[i - 1]; + + } + + else + + { + + i++; + + j++; + + if(i == lung_subseventa) + + { + + sol.push_back(j - i); + + i = LPS[i-1]; + + } + + } + + } + +} + + + +void citire() + +{ + + fin.getline(subsecventa, sizeof(subsecventa) + 1); + + fin.getline(sir, sizeof(sir) + 1); + + lung_sir = strlen(sir); + + lung_subseventa = strlen(subsecventa); + +} + + + +void afisare() + +{ + + if(sol.size() > 1000) + + { + + fout << sol.size() << '\n'; + + for(int i = 0; i < 1000; i++) + + fout << sol.at(i) << ' '; + + } + + else + + { fout << sol.size() << '\n'; + + for(unsigned int i = 0; i < sol.size(); i++) + + fout << sol.at(i) << ' '; + + } + +} + + + +int main() + +{ + + citire(); + + construct_LPS(); + + KMP(); + + afisare(); + + return 0; + +} diff --git a/strmatch/kmp/6a405d2c-0120-4a59-9630-739a1d33dfa1.cpp b/strmatch/kmp/6a405d2c-0120-4a59-9630-739a1d33dfa1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2e3d3525b885228db23245bb4b102b0f6f80f22 --- /dev/null +++ b/strmatch/kmp/6a405d2c-0120-4a59-9630-739a1d33dfa1.cpp @@ -0,0 +1,78 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int v[4000005],n,i,k,numar,urm[4000005],valoare,lim; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + for (i=1;i0&&s[i]!=s[k]) + + { + + k=urm[k-1]; + + } + + if (s[i]==s[k]) + + { + + k++; + + } + + urm[i]=k; + + if (k==n) + + { + + numar++; + + sol.push_back(i-2*n); + + } + + } + + g< +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int p[2000005]; + +char a[2000005], b[2000005]; + +int sol[1005]; + + + +void citire(){ + + f.getline(a+1, 2000005); + + f.getline(b+1, 2000005); + +} + + + +void form_prefix(){ + + int k=0; + + int m=strlen(a+1); + + p[1] = 0; + + for(int j=2; j<=m; j++){ + + while(k>0 && a[k+1] != a[j]) + + k = p[k]; + + if(a[k+1] == a[j]){ + + k++; + + } + + p[j] = k; + + } + +} + + + +int match_strings(){ + + int k=0; + + int nr=0; + + int n=strlen(b+1); + + int m=strlen(a+1); + + for(int i=1; i<=n; i++){ + + while(k>0 && a[k+1] != b[i]) + + k = p[k]; + + if(b[i] == a[k+1]) + + k++; + + if(k==m){ + + nr++; + + if(nr<=1000) + + sol[nr] = i-m; + + k=p[k]; + + } + + } + + return nr; + +} + + + +void afisare(int nr){ + + g< +#include +#include + + +using namespace std; + + + +int n, m, nro; + +int o[2000005], P[2000005]; + +char a[2000005], b[2000005]; + + + + + +void form_pref() + +{ + + int i = 1, j = 0; + + P[0] = -1; + + while(i <= n) + + { + + while(j > 0 && a[i] != a[j]) + + j = P[j-1] + 1; + + P[i] = j; + + ++i, ++j; + + } + +} + + + + + +void kmp() + +{ + + int i = 1, j = 1; + + while(i <= m) + + { + + while(j > 0 && b[i] != a[j]) + + j = P[j-1] + 1; + + ++i, ++j; + + if(j == n+1) + + { + + j = P[j-1]+1; + + nro++; + + if(nro <= 1000) + + o[nro] = i-n-1; + + } + + } + +} + + + +void afis() + +{ + + printf("%d\n", nro); + + int vmin = min(nro, 1000); + + for(int i=1; i<=vmin; ++i) + + printf("%d ", o[i]); + +} +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n%s\n", (a+1), (b+1)); + + n = strlen(a+1); + + m = strlen(b+1); + + form_pref(); + + kmp(); + + afis(); + + return 0; + +} diff --git a/strmatch/kmp/6e18d404-50ad-4114-a582-aba32e438b02.cpp b/strmatch/kmp/6e18d404-50ad-4114-a582-aba32e438b02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..401a74c10107be9c4705319a13bb3402b02615eb --- /dev/null +++ b/strmatch/kmp/6e18d404-50ad-4114-a582-aba32e438b02.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +using namespace std; + + +int pi[2000001]; +void prefix(char *A, int *pi){ + int k = 0, size = strlen(A); + + for(int i = 2; i <= size; i++){ + while(k && A[k + 1] != A[i]) + k = pi[k]; + + if(A[k + 1] == A[i]) k++; + + pi[i] = k; + } + +} + +void print(vector ans, FILE *fout){ + fprintf(fout, "%ld\n", ans.size()); + + for(int i = 0; i < ans.size() && i < 1000; i++) + fprintf(fout, "%d ", ans[i]); + +} + +void solve(char *A, char *B, int *pi, FILE *fout){ + int k = 0, size = strlen(B), sizeA = strlen(A); + vector ans; + + for(int i = 0; i < size; i++){ + while(k > 0 && A[k + 1] != B[i]) + k = pi[k]; + + if(A[k + 1] == B[i]) + k++; + + if(k == sizeA - 1){ + ans.push_back(i - sizeA + 2); + k = pi[k]; + } + + } + + print(ans, fout); +} + +int main(){ + FILE *fin = fopen("strmatch.in", "r"); + FILE *fout = fopen("strmatch.out", "w"); + char A[2000001], B[2000001]; + + fscanf(fin, "%s%s", A, B); + + for(int i = strlen(A); i > 0; i--) + A[i] = A[i - 1]; + + prefix(A, pi); + solve(A, B, pi, fout); + + fclose(fin); + fclose(fout); + +} diff --git a/strmatch/kmp/6e4e5abb-15ba-4421-9159-5be293c45fde.cpp b/strmatch/kmp/6e4e5abb-15ba-4421-9159-5be293c45fde.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffc16c7767d601ef5516a92adb88ff7d2c82ae2a --- /dev/null +++ b/strmatch/kmp/6e4e5abb-15ba-4421-9159-5be293c45fde.cpp @@ -0,0 +1,102 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int n, m; + +string a, b; + +vectorans; + +int q; + +int pi[2000011]; + +void prefix() + +{ + + for(int i=2; i>a; + + f>>b; + + a=' '+a; + + b=' '+b; + + n=a.size(); + + m=b.size(); + + prefix(); + + q=0; + + for(int i=1; i +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int n,m,i,j,k,urm[2000005],nr,poz[1001]; + +char vn[2000005],vm[2000005]; + +int main() + +{ + + f.getline(vn,2000005); + + f.getline(vm,2000005); + + k=0; + + n=strlen(vn); + + m=strlen(vm); + + urm[1]=0; + + for(i=2;i<=n;i++) + + { + + while(k>0 && vn[k]!=vn[i-1]) k=urm[k]; + + if(vn[k]==vn[i-1]) k++; + + urm[i]=k; + + } + + k=0; + + for(i=0;i0 && vn[k]!=vm[i]) k=urm[k]; + + if(vn[k]==vm[i]) k++; + + if(k==n) + + { + + nr++; + + if(nr<=1000)poz[nr]=i-n+1; + + } + + } + + g<1000) nr=1000; + + for(i=1;i<=nr;i++) + + g< +using namespace std; + + + +const int mod=1e9+7; + +const int modu=1999999973; + +const int modul=998244353; + + + +int pref[4000005]; + +vectorindx; + +string s,t; + + + +void kmp(){ + + t=(s+"#"+t); + + int j=0; + + for(int i=1;i=s.size()) indx.push_back(i-1); + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + cin >> s >> t; + + kmp(); + + int sz=indx.size(); + + cout << sz << '\n'; + + for(int i=0;i + + + + + + +const int lMax = 2000005; + + + +using namespace std; + + + +char a[lMax], b[lMax]; + +int lga, k, lgb, ans1; + +int pi[lMax]; + +vector ans2; + + + +main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + + + cin >> a + 1 >> b + 1; + + lga = strlen(a + 1); + + lgb = strlen(b + 1); + + + + k = 0; + + + + for (int i = 2; i <= lga; ++i) + + { + + k = pi[i - 1]; + + while (k && a[k + 1] != a[i]) + + k = pi[k]; + + + + if (a[k + 1] == a[i]) + + ++k; + + pi[i] = k; + + } + + + + k = 0; + + + + for (int i = 1; i <= lgb; ++i) + + { + + while (k && a[k + 1] != b[i]) + + k = pi[k]; + + if (a[k + 1] == b[i]) + + ++k; + + if (k == lga) + + { + + ++ans1; + + if (ans2.size() < 1000) + + ans2.emplace_back(i - lga); + + } + + } + + + + cout << ans1 << "\n"; + + for (auto i : ans2) + + cout << i << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/70c97701-d3ef-47e4-84ea-d5efcbd02818.cpp b/strmatch/kmp/70c97701-d3ef-47e4-84ea-d5efcbd02818.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d64770ba0d8b0b9bfb106769b43961b303bb629 --- /dev/null +++ b/strmatch/kmp/70c97701-d3ef-47e4-84ea-d5efcbd02818.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int n,m,p[20000005],i,q,db; + +vectormegold; + +string A,B; + + + +void prefix() + +{ + + int i=0,q=0; + + p[1]=0; + + + + for(i=2;i0 && A[q+1]!=A[i]) + + q=p[q]; + + + + if(A[q+1]==A[i]) ++q; + + p[i]=q; + + } + +} + + + +int main() + +{ + + cin>>A>>B; + + A=" "+A; + + B=" "+B; + + + + prefix(); + + + + for(i=1;i +#include +#include + + +using namespace std; + + + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + + + +constexpr int NMAX = 2e6 + 5; + + + +char a[2*NMAX+5]; + +char b[NMAX+5]; + + + +int phi[2*NMAX+5]; + + + +int K, N; + + + +void Read () { + + f.getline(a, NMAX); + + f.getline(b, NMAX); + + K = strlen(a); + + strcat(a, "#"); + + strcat(a, b); + + N = strlen(a); + +} + + + +void Construiesc_KMP () { + + int rez = 0; + + for (int i = 1; i < N; ++ i ) { + + while (rez > 0 && a[ rez ] != a[ i ]) { + + rez = phi[ rez - 1 ]; + + } + + if (a[ rez ] == a[ i ]) ++ rez; + + phi[ i ] = rez; + + } + +} + + + +int main () + +{ + + Read(); + + Construiesc_KMP(); + + + + vector ind; + + for (int i = K + 1; i < N; ++ i ) { + + if (phi[ i ] == K) ind.push_back(i-K-K); + + } + + + + g << ind.size() << '\n'; + + for (int i = 0; i < min((int)ind.size(), 1000); ++i) + + g << ind[ i ] << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/71fd67ca-e635-4e83-a89f-1e756f940c14.cpp b/strmatch/kmp/71fd67ca-e635-4e83-a89f-1e756f940c14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3239aa9592cff8dd0f4c8b12e64ea6f0a0b9562 --- /dev/null +++ b/strmatch/kmp/71fd67ca-e635-4e83-a89f-1e756f940c14.cpp @@ -0,0 +1,123 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char sir[2000005],s[2000005]; + +int l[2000005],v[1010]; + +int main() + +{ + + int i,j,n,numar=0,poz,m; + + fin>>s>>sir; + + n=strlen(s)-1; + + i=1; + + j=0; + + while(i<=n) + + { + + if(s[i]==s[j]) + + l[i]=j+1,i++,j++; + + else + + { + + while(j>0&&s[j]!=s[i]) + + j=l[j-1]; + + if(s[j]==s[i]) + + l[i]=j+1,i++,j++; + + else + + if(s[i]!=s[j]&&j==0) + + l[i]=0,i++; + + } + + } + + i=0; + + j=0; + + m=strlen(sir)-1; + + while(j<=m) + + { + + if(s[i]==sir[j]) + + { + + poz=j-i; + + while(s[i]==sir[j]&&i<=n) + + i++,j++; + + if(i==n+1) + + if(numar>=1000) + + numar++; + + else + + v[++numar]=poz; + + i=l[i-1]; + + } + + else + + { + + while(s[i]!=sir[j]&&i>0) + + i=l[i-1]; + + if(i==0&&s[i]!=sir[j]) + + j++; + + } + + } + + fout<1000) + + for(i=1;i<=1000;i++) + + fout< +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout("strmatch.out"); + +int n,prefix[2000006]; + +char s[2000006],patern[2000006]; + +vectorsol; + + + +int main() + +{ + + fin.getline(patern,sizeof patern); + + fin.getline(s,sizeof s); + + int n=strlen(patern),m=strlen(s),i,j=0; + + for (i=1;i + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000004],b[2000004]; + +int la,lb,lun,t[2000004],i,nrsol; + +vector sol; + +int main() + +{ + + fin>>(a+1)>>(b+1); + + la=strlen(a+1); + + lb=strlen(b+1); + + lun=0; + + for(i=2; i<=la; i++) + + { + + while(lun!=0&&a[lun+1]!=a[i]) + + lun=t[lun]; + + if(a[lun+1]==a[i]) + + lun++; + + t[i]=lun; + + } + + lun=0; + + for(i=1; i<=lb; i++) + + { + + while(lun!=0&&a[lun+1]!=b[i]) + + lun=t[lun]; + + if(a[lun+1]==b[i]) + + lun++; + + if(lun==la) + + { + + nrsol++; + + if(nrsol<=1000) + + sol.push_back(i-la); + + lun=t[lun]; + + } + + + + } + + fout< +#include +#include + + +using namespace std; + + + + + + + +char text[2000001], pattern[2000001]; + +int n, lps[2000001], resultext[2000001]; + +int lenT, lenP; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void calculateLPSArray() { + + int i=1; + + int j=0; + + lps[0] = 0; + + while (i < lenP) { + + if (pattern[i] == pattern[j]) { + + lps[i] = j+1; + + i++; + + j++; + + } + + else if (j != 0) { + + j=lps[j-1]; + + } + + else { + + lps[i] = 0; + + i++; + + } + + } + +} + + + +void KMP() { + + int i=0; + + int j=0; + + + + while (i < lenT) { + + if(text[i] == pattern[j]) { + + i++; + + j++; + + } else if (j != 0) { + + j=lps[j-1]; + + } else { + + i++; + + } + + if (j == lenP) { + + n++; + + if (n <= 1000) + + resultext[n] = i-j; + + j = lps[j-1]; + + } + + } + +} + + + +void printResult() { + + fout< 1000) { + + n = 1000; + + } + + for (int i=1; i<=n; i++) { + + fout< +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string txt, pat; + + + +int lps[2000005]; + +int rez[1000]; + +void computeLps() + +{ + + int i = 1, len = 0; + + + + while(i < pat.size()) + + { + + if(pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if(len) + + { + + len = lps[len-1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + + + +} + + + +int nr_rez; + +void kmp() + +{ + + computeLps(); + + int n = txt.size(), m = pat.size(), i=0, j=0; + + while(i < n) + + { + + if(txt[i] == pat[j]) + + { + + i++; + + j++; + + } + + else + + { + + if(j == 0) + + { + + i++; + + } + + else + + { + + j = lps[j-1]; + + } + + } + + if(j == m) + + { + + if(nr_rez < 1000) + + { + + rez[nr_rez] = i-j; + + } + + nr_rez++; + + j = lps[j-1]; + + } + + } + +} + + + +int main() + +{ + + fin>>pat>>txt; + + kmp(); + + fout< + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void debug_out() { cerr << '\n'; } + +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +typedef pair pii; + +typedef long long int ll; + +typedef long double ld; + + + +const int DMAX = 2e6+10; + + + +vector sol; + + + +char a[DMAX]; + +char b[DMAX]; + + + +int dp[DMAX]; + + + +int ans; + + + +int main(){ + + ios_base::sync_with_stdio(false); + + cin.tie(0); + + + + int t,i,j; + + int lga,lgb,k; + + + + fin>>(a+1); + + fin>>(b+1); + + + + lga=strlen(a+1); + + lgb=strlen(b+1); + + + + k=0; + + for(i=2;i<=lga;i++){ + + while(a[k+1] != a[i] && k) + + k=dp[k]; + + if(a[k+1] == a[i]) + + k++; + + dp[i]=k; + + } + + + + k=0; + + if(a[1] == b[1]) + + k=1; + + for(i=2;i<=lgb;i++){ + + while(a[k+1] != b[i] && k) + + k=dp[k]; + + if(a[k+1] == b[i]) + + k++; + + if(k == lga){ + + ans++; + + if(sol.size() < 1000) + + sol.push_back(i-lga); + + } + + } + + fout< +#include +#include +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +vector v; + +int pi[2000005]; + +int main() + +{ + + char a[2000005],b[2000005]; + + fin>>a>>b; + + int n=strlen(a),m=strlen(b),i,j,k=0; + + pi[1]=0; + + for(i=2;i<=n;++i) + + { + + while(a[i-1]!=a[k] && k)k=pi[k]; + + if(a[i-1]==a[k])++k; + + pi[i]=k; + + } + + k=0; + + for(i=1;i<=m;++i) + + { + + while(b[i-1]!=a[k] && k)k=pi[k]; + + if(b[i-1]==a[k])++k; + + if(k==n)v.push_back(i-k); + + } + + fout<1000)v.pop_back(); + + for(auto i : v) + + fout< +using namespace std; + +ifstream fin( "strmatch.in" ); + +ofstream fout( "strmatch.out" ); + +void fast() + +{ + + cin.tie( 0 ); + + ios_base::sync_with_stdio( 0 ); + +} + +vectorsol; + +string a, b; + +long long lps[2000005]; + +void calc_lps() + +{ + + long long len = 0; + + int n = a.size(); + + + + for( int i = 1; i < n; i++ ) + + { + + while( a[i] != a[len] && len ) + + len = lps[len - 1]; + + + + if( a[len] == a[i] ) + + len++; + + + + lps[i] = len; + + } + +} + +int main() + +{ + + fast(); + + fin >> a >> b; + + calc_lps(); + long long len = 0; + + int n = b.size(); + + + + for( int i = 0; i < n; i++ ) + + { + + while( a[len] != b[i] && len ) + + len = lps[len - 1]; + + + + if( a[len] == b[i] ) + + len++; + + + + if( len == a.size() ) + + sol.push_back( i - len + 1 ); + + + + } + + + + fout << sol.size() << '\n'; + + + + for( int i = 0; i < min( 1000,(int)(sol.size()) ); i++ ) + + fout << sol[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/kmp/77d61ec6-cfb5-4ee5-b0dd-31224107f9cf.cpp b/strmatch/kmp/77d61ec6-cfb5-4ee5-b0dd-31224107f9cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e0e4215f2287b737844499da2c0962ed3bf4133b --- /dev/null +++ b/strmatch/kmp/77d61ec6-cfb5-4ee5-b0dd-31224107f9cf.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000009]; + +char t[2000009]; + +int n,m; + +int pref[2000009]; + + + +void Read() + +{ + + int i,j = 0; + + fin>>s; + + fin>>t; + + m = strlen(s); + + n = strlen(t); + + for(int i = 1; i < m; ) + + { + + if(s[i] == s[j]) + + { + + pref[i] = j+1; + + ++i; + + ++j; + + } + + else + + { + + if(j != 0) + + { + + j = pref[j-1]; + + } + + else + + { + + + + ++i; + + } + + } + + } + +} + + + +void Do() + +{ + + int ct = 0; + + int sol[1005]; + + int i,j = 0; + + i = 0; + + while( i < n) + + { + + if(t[i] == s[j]) + + { + + ++i; + + ++j; + + } + + if( j == m) + + { + + if(ct <= 1000) + + sol[++ct] = i-j; + + else ct++; + + j = pref[j-1]; + + } + + else if(i < n && s[j] != t[i]) + + { + + if(j!=0) + + j = pref[j-1]; + + else ++i; + + } + + } + + fout< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 2000005; + + + +int lps[nmax]; + + + +int c; + + + +string t, p; + + + +void CreateLPS() + +{ + + int k = 1, len = 0, s = p.length(); + + + + while (k < s) + + { + + if (p[k] == p[len]) + + lps[k++] = ++len; + + else + + { + + if (len) + + len = lps[len - 1]; + + else + + k++; + + } + + } + +} + + + +int main() + +{ + + fout << " \n"; + + fin >> p >> t; + + + + CreateLPS(); + + + + int i = 0, j = 0, s = t.length(); + + + + while (i < s) + + { + + if (t[i] == p[j]) + + { + + i++; + + j++; + + } + + else + + { + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + + + if (j == p.length()) + + { + + if (c < 1000) + + fout << i - j << ' '; + + + + c++; + + + + j = lps[j - 1]; + + } + + } + + + + fout.close(); + + fout.open("strmatch.out", ios::in|ios::out); + + + + fout << c; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/7860fb4c-d5b3-43fc-9064-cd04a46cc402.cpp b/strmatch/kmp/7860fb4c-d5b3-43fc-9064-cd04a46cc402.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21318f1bf0165fcf2255017b4a08dbaa7c9b39fd --- /dev/null +++ b/strmatch/kmp/7860fb4c-d5b3-43fc-9064-cd04a46cc402.cpp @@ -0,0 +1,120 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void buildPi(vector &pi, const string &pattern) { + + + + int jIdx = -1; + + pi[0] = -1; + + + + for (int idx = 1; idx < (int)pattern.size(); ++idx) { + + + + while (jIdx != -1 and pattern[jIdx + 1] != pattern[idx]) + + jIdx = pi[jIdx]; + + + + if (pattern[jIdx + 1] == pattern[idx]) + + ++jIdx; + + pi[idx] = jIdx; + + } + +} + + + +int main() { + + + + string pattern, String; + + fin >> pattern >> String; + + + + vector pi(pattern.size()); + + buildPi(pi, pattern); + + + + vector index; + + index.reserve(1000); + + + + int jIdx = -1; + + int matches = 0; + + + + for (int idx = 0; idx < (int)String.size(); ++idx) { + + + + while (jIdx != -1 and String[idx] != pattern[jIdx + 1]) + + jIdx = pi[jIdx]; + + + + if (String[idx] == pattern[jIdx + 1]) + + ++jIdx; + + + + if (jIdx + 1 == (int)pattern.size()) { + + + + ++matches; + + jIdx = pi[jIdx]; + + + + if (matches <= 1000) + + index.push_back(idx - pattern.size() + 1); + + } + + } + + + + fout << matches << '\n'; + + for (const int &itm : index) + + fout << itm << ' '; + +} diff --git a/strmatch/kmp/78ff1425-5191-49f9-a981-75e917cc01c2.cpp b/strmatch/kmp/78ff1425-5191-49f9-a981-75e917cc01c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cdfd09d6b9ec64b902af8bcd47461083e5c44094 --- /dev/null +++ b/strmatch/kmp/78ff1425-5191-49f9-a981-75e917cc01c2.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a, b, c; + +int l1, l2; + +vector aparitii; + +int pi[2000005]; + + + +int main() { + + a = b = " "; + + fin >> c; + + a += c; + + fin >> c; + + b += c; + + l1 = a.length()-1, l2 = b.length()-1; + + + + int k = 0; + + for(int i = 2; i <= l1; i++) { + + while(k > 0 && a[k+1] != a[i]) + + k = pi[k]; + + if(a[k+1] == a[i]) + + k++; + + pi[i] = k; + + } + + + + int q = 0; + + for(int i = 1; i <= l2; i++) { + + while(q > 0 && (a[q+1] != b[i])) + + q = pi[q]; + + if(a[q+1] == b[i]) + + q++; + + if(q == l1) + + aparitii.push_back(i-l1); + + } + + + + fout << aparitii.size() << '\n'; + + if(aparitii.size() > 1000) + + for(int i = 0; i < 1000; i++) + + fout << aparitii[i] << ' '; + + else + + for(auto nr: aparitii) + + fout << nr << ' '; + +} diff --git a/strmatch/kmp/7908886d-5f2f-4af6-885c-75cba70862d1.cpp b/strmatch/kmp/7908886d-5f2f-4af6-885c-75cba70862d1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea753cffb08a3d1f3580dc18a87f3a4724e63d68 --- /dev/null +++ b/strmatch/kmp/7908886d-5f2f-4af6-885c-75cba70862d1.cpp @@ -0,0 +1,82 @@ +#include + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +const int DIM = 2000000 + 5; + + + +char s[DIM], t[DIM]; + + + +int pref[DIM], sol[DIM]; + + + +void prefix(int n) + +{ + + pref[1] = 0; + + int k = 0; + + for(int i = 2; i <= n; ++i) { + + while(k > 0 && s[i] != s[k + 1]) k = pref[k]; + + if(s[i] == s[k + 1]) ++k; + + pref[i] = k; + + } + +} + + + +int main() + +{ + + fin >> (s + 1) >> (t + 1); + + int szs = strlen(s + 1), szt = strlen(t + 1); + + prefix(szs); + + int cnt = 0; + + for(int i = 1; i <= szt; ++i) { + + while(cnt > 0 && t[i] != s[cnt + 1]) cnt = pref[cnt]; + + if(t[i] == s[cnt + 1]) ++cnt; + + if(cnt >= szs) { + + if(sol[0] < 1000) sol[++sol[0]] = i - szs; + + else ++sol[0]; + + } + + } + + fout << sol[0] << "\n"; + + for(int i = 1; i <= min(sol[0], 1000); ++i) fout << sol[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/7b07622d-65a3-4fbb-8cbd-bf6c82443b6e.cpp b/strmatch/kmp/7b07622d-65a3-4fbb-8cbd-bf6c82443b6e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2dbeb4e45494c46ee439e93dd721115b4053c319 --- /dev/null +++ b/strmatch/kmp/7b07622d-65a3-4fbb-8cbd-bf6c82443b6e.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = " " + b; + + int n = a.size() - 1, m = b.size() - 1; + + + + vector kmp(n + 1, 0); + + int k = 0; + + for(int i = 2; i <= n; i ++) { + + while(k > 0 && a[k + 1] != a[i]) + + k = kmp[k]; + + if(a[k + 1] == a[i]) + + k ++; + + kmp[i] = k; + + } + + + + int cnt = 0; + + vector ans; + + for(int i = 1; i <= m; i ++) { + + while(k > 0 && a[k + 1] != b[i]) + + k = kmp[k]; + + if(a[k + 1] == b[i]) + + k ++; + + if(k == n) { + + k = kmp[k]; + + if(i >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - n); + + } + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/7b7ce6ce-6be1-485e-b6d2-9c6cf2d001a0.cpp b/strmatch/kmp/7b7ce6ce-6be1-485e-b6d2-9c6cf2d001a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4c0148a9b91600c36c3c20592af439b73c9c24e9 --- /dev/null +++ b/strmatch/kmp/7b7ce6ce-6be1-485e-b6d2-9c6cf2d001a0.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +#include + + +using namespace std; + + + +int main() { + + string minta, str; + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + getline(fin, minta); + + getline(fin, str); + + int minta_hossz = minta.size(); + + int str_hossz = str.size(); + + int db = 0; + + vector a; + int* prefix; + + prefix = new int[minta_hossz + 1]; + + prefix[0] = 0; + + prefix[1] = 0; + + int k = 0; + + for (int i = 1; i < minta_hossz; i++) { + + while (k > 0 && minta[k] != minta[i]) + + k = prefix[k]; + + + + if (minta[k] == minta[i]) + + k++; + + + + prefix[i + 1] = k; + + } + + k = 0; + + db = 0; + + for (int i = 0; i < str_hossz; i++) { + + while (k > 0 && minta[k] != str[i]) + + k = prefix[k]; + + if (minta[k] == str[i]) + + k++; + + if (k == minta_hossz) { + + a.push_back(i - minta_hossz + 1); + + db++; + + } + + } + + fout << db << "\n"; + + if (db > 1000) { + + for (int i = 0; i <= 999; i++) { + + fout << a[i] << " "; + + } + + } + + else { + + for (auto& e : a) { + + fout << e << " "; + + } + + } + +} diff --git a/strmatch/kmp/7b921461-0baa-4b14-8887-c830f17f98ef.cpp b/strmatch/kmp/7b921461-0baa-4b14-8887-c830f17f98ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..34d14f8d4a99fe3a39745688d89771df5fef2344 --- /dev/null +++ b/strmatch/kmp/7b921461-0baa-4b14-8887-c830f17f98ef.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char s1[2000005],s2[2000005]; + +int prefix[2000005],ans[1009],k,j; + +int main() + +{ + + fin.getline(s1,2000005); + + fin.getline(s2,2000005); + + int n=strlen(s1); + + int m=strlen(s2); + + j=0; + + for(int i=1;i0&&s1[j]!=s1[i]) + + j=prefix[j-1]; + + if(s1[i]==s1[j]) + + j++; + + prefix[i]=j; + + } + + j=0; + + for(int i=0;i0&&s1[j]!=s2[i]) + + j=prefix[j-1]; + + if(s1[j]==s2[i]) + + j++; + + if(j==n) + + { + + k++; + + if(k<=1000) + + ans[k]=i-j+1; + + j=prefix[j-1]; + + } + + } + + fout<1000) + + k=1000; + + for(int i=1;i<=k;i++) + + fout< +#include +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +string s, w; + + +int t[2000001], n, m; + + +void precalc() +{ + int i, k = 0; + t[0] = -1; + for (i = 1; i= 0 && w[i] != w[k]) + k = t[k]; + } + } + t[w.size()] = k; +} + +int ap[1001]; + +int main() +{ + fin >> w >> s; + precalc(); + int i, k = -1; + for (i = 0; i= 0 && w[k] != s[i]) + k = t[k]; + if (k < 0) + k = 0; + if (w[k] == s[i]) + { + k++; + if (k == w.size()) + { + ap[0]++; + if (ap[0] <= 1000) + ap[ap[0]] = i-k+1; + k = t[k]; + } + } + } + fout << ap[0] << '\n'; + for (i = 1; i<=min(ap[0], 1000); i++) + fout << ap[i] << ' '; + return 0; +} diff --git a/strmatch/kmp/7e6353b3-9a09-4247-a466-2d6c43817fbd.cpp b/strmatch/kmp/7e6353b3-9a09-4247-a466-2d6c43817fbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cac58e47466da42b56204a35043ee7877a51dc4 --- /dev/null +++ b/strmatch/kmp/7e6353b3-9a09-4247-a466-2d6c43817fbd.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int phi[2*2000000 +10], ans; + +vector sol; + +string s1, s2, s; + + + +int main() + +{ + + f >> s1 >> s2; + + s = s1 + '%' + s2; + + int n1 = s1.size(), n = s.size(), j = 0, i = 1; + + while(i < n) + + { if(s[j] == s[i]) phi[i] = j + 1, i++, j++; + + else + + { if(j > 0) j = phi[j-1]; + + else i++; + + } + + } + + for(int i=0; i + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +char t[2000005], p[2000005]; + +int a[2000005]; + +int urm[2000005]; + +int n, m, k; + + + +void Prefix() + +{ + + int j=0; + + for (int i=1; i0 && p[i]!=p[j]) + + j=urm[j-1]; + + if (p[i]==p[j]) j++; + + urm[i]=j; + + } + +} + + + +void KMP() + +{ + + int q=0; + + for (int i=0; i0 && p[q]!=t[i]) + + q=urm[q-1]; + + if (p[q]==t[i]) q++; + + if (q==n) + + { + + a[++k]=i-n+1; + + } + + } + +} + + + +void Afisare() + +{ + + fout<1000) k=1000; + + for (int i=1; i<=k; ++i) + + fout< + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +string sir, cuv; + +int pref[2000001], sol; + +int pozitii[1001], vf; + + + +int main() + +{ + + in >> cuv >> sir; + + cuv = " " + cuv; + + sir = " " + sir; + + + + for(int i = 2, lung = 0; i < cuv.size(); i++) + + { + + while(lung && cuv[lung+1] != cuv[i]) + + lung = pref[lung]; + + + + if(cuv[lung+1] == cuv[i]) + + lung++; + + + + pref[i] = lung; + + } + + + + for(int i = 1, lung = 0; i < sir.size(); i++) + + { + + while(lung && cuv[lung+1] != sir[i]) + + lung = pref[lung]; + + + + if(cuv[lung+1] == sir[i]) + + lung++; + + + + if(lung == cuv.size() - 1) + + { + + sol++; + + if(vf < 1000) + + pozitii[++vf] = i - lung; + + lung = pref[lung]; + + } + + } + + + + out << sol << '\n'; + + + + for(int i = 1; i <= min(1000, sol); i++) + + out << pozitii[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/kmp/800fd794-b305-4ae1-b927-6f90073c1e38.cpp b/strmatch/kmp/800fd794-b305-4ae1-b927-6f90073c1e38.cpp new file mode 100644 index 0000000000000000000000000000000000000000..197c5623cd4555f7368ffe10c95110913ec969ba --- /dev/null +++ b/strmatch/kmp/800fd794-b305-4ae1-b927-6f90073c1e38.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + + + +const int maxN=(2e6)+5; + + + +char a[maxN],b[maxN]; + +int n,m,pref[maxN],k; + +vector pos; + + + +int main() + +{ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + + + scanf("%s",a+1); + + scanf("\n"); + + scanf("%s",b+1); + + + + n=strlen(a+1); + + m=strlen(b+1); + + + + + + pref[1]=0; + + + + k=0; + + for(int i=2;i<=n;i++) + + { + + while(k && a[k+1]!=a[i]) k=pref[k]; + + if(a[k+1]==a[i]) k++; + + + + pref[i]=k; + + } + + + + + + k=0; + + for(int i=1;i<=m;i++) + + { + + while(k && a[k+1]!=b[i]) k=pref[k]; + + if(a[k+1]==b[i]) k++; + + if(k==n) pos.push_back(i-n); + + } + + + + + + int sz=(int)pos.size(); + + printf("%d\n",sz); + + + + if(sz>1000) pos.resize(1000); + + + + for(auto it:pos) + + printf("%d ",it); + + + + printf("\n"); + + return 0; + +} diff --git a/strmatch/kmp/815e8f2f-c17a-4ff7-8e83-7e1b2c0146cb.cpp b/strmatch/kmp/815e8f2f-c17a-4ff7-8e83-7e1b2c0146cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdd7d7fc712874214c98094f1d6bce9a59dae8ea --- /dev/null +++ b/strmatch/kmp/815e8f2f-c17a-4ff7-8e83-7e1b2c0146cb.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +const int NMAX = 2000000; + +const int MMAX = 2000000; + +int urm[MMAX + 5]; + +char t[NMAX + 5] , p[MMAX + 5]; + +vector ans; + +int main() + +{ + + freopen("strmatch.in" , "r" , stdin); + + freopen("strmatch.out" , "w" , stdout); + + int n , m , i , k , nr; + + scanf("%s\n%s" , (p + 1) , (t + 1)); + + n = strlen(t + 1); + + m = strlen(p + 1); + + k = 0; + + urm[1] = 0; + + for(i = 2 ; i <= m ; i ++) + + { + + while(k > 0 && p[k + 1] != p[i]) + + k = urm[k]; + + if(p[k + 1] == p[i]) + + k ++; + + urm[i] = k; + + } + + k = nr = 0; + + for(i = 1 ; i <= n ; i ++) + + { + + while(k > 0 && p[k + 1] != t[i]) + + k = urm[k]; + + if(p[k + 1] == t[i]) + + k ++; + + if(k == m) + + { + + nr ++; + + if(ans.size() < 1000) + + ans.push_back(i - m); + + k = urm[k]; + + } + + } + + printf("%d\n" , nr); + + for(i = 0 ; i < ans.size() ; i ++) + + printf("%d " , ans[i]); + + return 0; + +} diff --git a/strmatch/kmp/8388a0cb-62aa-4191-af28-0b7847308486.cpp b/strmatch/kmp/8388a0cb-62aa-4191-af28-0b7847308486.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07be4b82bd3ece57119a5a491ebc93dca6886480 --- /dev/null +++ b/strmatch/kmp/8388a0cb-62aa-4191-af28-0b7847308486.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n, m, k, sol[2000050], d[2000050]; + +char patern[2000050], sir[2000050]; +void lps(){ + + int l=0; + + d[0]=0; + + int i=1; + + while(i>patern; + + fin>>sir; + + m=strlen(patern); + + n=strlen(sir); + + kmp(); + + fout< +#include +#include + + +using namespace std; + + + +char s[2000005], p[2000005]; + +int nr, poz[2000005], lps[2000005], n, m; + + + +void citire() + +{ + + scanf("%s\n", p); + + scanf("%s\n", s); + + n=strlen(p); + + m=strlen(s); + +} + + + +void lps_make() + +{ + + lps[0]=0; + + int len=0; + + for(int i=1;i1000) + + nr=1000; + + for(int i=0;i + + +using namespace std; + +ifstream r("strmatch.in"); + +ofstream w("strmatch.out"); + +string a, b; + +int p[2000005], n, m, rez; + +vectorf; + +void pref() + +{ + + p[0]=0; + + int cnt=0, ans=1; + + while (ans>a; + + r>>b; + + n=a.size(), m=b.size(); + + pref(); + + kmp(); + + w< +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Dim=2e6+2; + +int dp[Dim],dp2[Dim],K,cnt; + + + +vector < int > V; + + + +int main() + +{ + + string A,B; + + f>>A>>B; + + + + int lgA=A.size(),lgB=B.size(); + + for(int i=lgA;i>=1;i--) A[i]=A[i-1]; + + for(int i=lgB;i>=1;i--) B[i]=B[i-1]; + + + + dp[1]=0; + + for(int i=2;i<=lgA;i++) + + { + + K=dp[i-1]; + + while( K>0 && A[K+1]!=A[i] ) K=dp[K]; + + + + dp[i]=K+(A[K+1]==A[i]); + + } + + + + dp2[0]=0; + + for(int i=1;i<=lgB;i++) + + { + + K=dp2[i-1]; + + while( K>0 && A[K+1]!=B[i] ) K=dp[K]; + + + + dp2[i]=K+(A[K+1]==B[i]); + + + + if(dp2[i]==lgA) + + { + + cnt++; + + if( cnt <= 1000 ) V.push_back(i-lgA); + + } + + } + + + + g< +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + + + +int N, M, Size, Z[(NMAX << 1)]; + + + +char A[NMAX], B[NMAX], S[(NMAX << 1)]; + + + +int V[NMAX], cnt = 0; + + + +static inline void Read () + +{ + + f.tie(nullptr); + + + + f >> (A + 1) >> (B + 1); + + + + N = (int)strlen(A + 1), M = (int)strlen(B + 1); + + + + return; + +} + + + +static inline void Build () + +{ + + for(int i = 1; i <= N; ++i) + + S[++Size] = A[i]; + + + + S[++Size] = '#'; + + + + for(int i = 1; i <= M; ++i) + + S[++Size] = B[i]; + + + + return; + +} + + + +static inline void Precalculation () + +{ + + Build(); + + + + Z[1] = Size; + + + + int Left = 0, Right = 0; + + + + for(int i = 2; i <= Size; ++i) + + { + + if(i <= Right) + + Z[i] = min(Z[(i - Left + 1)], Right - i + 1); + + else + + Z[i] = 0; + + + + while(i + Z[i] <= Size && S[(i + Z[i])] == S[(Z[i] + 1)]) + + ++Z[i]; + + + + if(i + Z[i] - 1 >= Right) + + Left = i, Right = i + Z[i] - 1; + + } + + + + return; + +} + + + +static inline void Solve () + +{ + + for(int i = N + 2; i <= Size; ++i) + + if(Z[i] == N) + + V[++cnt] = i - (N + 2) + 1; + + + + return; + +} + + + +static inline void Write () + +{ + + g << cnt << '\n'; + + cnt = min(cnt, 1000); + + + + for(int i = 1; i <= cnt; ++i) + + { + + g << (--V[i]); + + + + if(i != cnt) + + g << ' '; + + } + + + + g << '\n'; + + + + return; + +} + + + +int main() + +{ + + Read(); + + + + Precalculation(); + + + + Solve(); + + + + Write(); + + + + return 0; + +} diff --git a/strmatch/kmp/884f3d75-e4b6-43f0-8e9d-ab8d5d6fd156.cpp b/strmatch/kmp/884f3d75-e4b6-43f0-8e9d-ab8d5d6fd156.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a33fc021b398f1e34bc68b0c9818ca10a6776837 --- /dev/null +++ b/strmatch/kmp/884f3d75-e4b6-43f0-8e9d-ab8d5d6fd156.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + +int pi[2000005],res[1005]; + +stack s; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int l=0,n,m,cnt=0,r=0,c2=0,i; + + string a,b; + + cin >> a; + + cin >> b; + + n=a.size(); + + m=b.size(); + + for(int i=1;i0 && a[l-1]!=a[i]) + + l=pi[l-1]; + + pi[i]=l; + + } + + l=0; + + i=0; + + while (i < m) + + { + + if (a[l] == b[i]) + + { + + l++; + + i++; + + } + + if (l == n) + + { + + if(cnt<1000) + + { + + s.push(i-l); + + cnt++; + + } + + r++; + + l = pi[l - 1]; + + } + + else + + { + + if (a[l] != b[i]) + + { + + if (l != 0) + + l = pi[l - 1]; + + else + + i = i + 1; + + } + + } + + } + + cout << r << '\n'; + + while(s.size()>0) + + { + + res[c2]=s.top(); + + c2++; + + s.pop(); + + } + + for(int i=c2-1;i>=0;i--) + + cout << res[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/8935cd31-5c66-4886-87d1-6107633f8232.cpp b/strmatch/kmp/8935cd31-5c66-4886-87d1-6107633f8232.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0322821e3e461a77cc4b29bf8c04cab42a2e06d2 --- /dev/null +++ b/strmatch/kmp/8935cd31-5c66-4886-87d1-6107633f8232.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; +ifstream f("strmatch.in"); +ofstream g("strmatch.out"); + +string a, b; +int p[2000004], j, s; +queue q; + +void getP() { + for (int i = 2; i < a.length(); i++) { + while (j && a[j + 1] != a[i]) + j = p[j]; + if (a[j + 1] == a[i]) + j++; + p[i] = j; + } +} + +int main() { + f >> a >> b; + a = "0" + a; + b = "0" + b; + + getP(); + + j = 0; + for (int i = 1; i < b.length(); i++) { + while (j && a[j + 1] != b[i]) + j = p[j]; + if (a[j + 1] == b[i]) + j++; + if (j == a.length()-1) { + q.push(i-a.length()+1); + j = p[j]; + } + } + + g << q.size() << '\n'; + s = min((int)q.size(), 1000); + for (int i = 1; i <= s; i++) { + g << q.front() << ' '; + q.pop(); + } + + f.close(); + g.close(); + return 0; +} diff --git a/strmatch/kmp/8a2c7aba-7fed-4ecd-aa32-0a0258b4f398.cpp b/strmatch/kmp/8a2c7aba-7fed-4ecd-aa32-0a0258b4f398.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9da8dc267a101d46cc11a42e2afef492f0a229d5 --- /dev/null +++ b/strmatch/kmp/8a2c7aba-7fed-4ecd-aa32-0a0258b4f398.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include + + + +using namespace std; + + + + + + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +char A[2000005], B[2000005]; +int pi[2000005], v[1004], nr, a, b; + +void kmp1(){ + int k = 0; + for(int i = 1; i <= b; ++i){ + while(k && A[k + 1] != B[i]) + k = pi[k]; + if(A[k + 1] == B[i]) + ++k; + if(k == a){ + ++nr; + if(nr <= 1000) + v[nr] = i; + } + } + +} + +void kmp(){ + int k = 0; + for(int i = 2; i <= a; ++i){ + while(k && A[k + 1] != A[i]) + k = pi[k]; + if( A[k + 1] == A[i]) + ++k; + pi[i] = k; + } +} + +int main(){ + in >> (A+1); + in >> (B+1); + a = strlen(A+1); + b = strlen(B+1); + + kmp(); + kmp1(); + + out << nr << '\n'; + if(nr>1000) nr=1000; + for(int i = 1; i <= nr; ++i) + out << v[i] - a << ' '; + return 0; + + +} diff --git a/strmatch/kmp/8a41922c-92b6-4267-b9f4-4acf58b9e815.cpp b/strmatch/kmp/8a41922c-92b6-4267-b9f4-4acf58b9e815.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb53b08d4394b78d262b1c67536bdd9cc08e77e0 --- /dev/null +++ b/strmatch/kmp/8a41922c-92b6-4267-b9f4-4acf58b9e815.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int main() { + + string s, t; fin >> s >> t; + + string str = s + '#' + t; + + + + vector pi(str.size()); + + int k = 0; + + for (int i = 1; i < (int) str.size(); i++) { + + while (k && str[i] != str[k]) + + k = pi[k - 1]; + + if (str[i] == str[k]) + + k++; + + pi[i] = k; + + } + + + + int cnt = 0; + + vector ans; + + for (int i = s.size() + 1; i < (int) str.size(); i++) + + if (pi[i] == (int) s.size() && ++cnt <= 1000) + + ans.push_back(i - 2 * s.size()); + + + + fout << cnt << '\n'; + + for (int pos : ans) + + fout << pos << ' '; + + fout << '\n'; + + + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/8b163a5e-b3ba-4715-997c-2a46e6968526.cpp b/strmatch/kmp/8b163a5e-b3ba-4715-997c-2a46e6968526.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f500f45c8e7d81b0bc7877cf7cac56749c5311f6 --- /dev/null +++ b/strmatch/kmp/8b163a5e-b3ba-4715-997c-2a46e6968526.cpp @@ -0,0 +1,86 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 4000005; + +string a, b; + +int n, m, pi[nmax]; + + + +int main(){ + + fin >> a >> b; + + n = a.size(); + + m = b.size(); + + a += '#'; + + for (int i = 0; i < m; ++i){ + + a += b[i]; + + } + + vector ans; + + for (int i = 1; i < m + n + 1; ++i){ + + int l = pi[i - 1]; + + while (l > 0 && a[l] != a[i]) l = pi[l - 1]; + + if (a[l] == a[i]) ++l; + + pi[i] = l; + + if (i >= n + 1){ + + if (pi[i] == n){ + + ans.push_back(i - n - n); + + } + + } + + } + + fout << ans.size() << "\n"; + + int cnt = 0; + + for (auto it : ans){ + + fout << it << " "; + + ++cnt; + + if (cnt == 1000){ + + break; + + } + + } + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/8b329b44-3e34-461f-bced-a2cc15386f76.cpp b/strmatch/kmp/8b329b44-3e34-461f-bced-a2cc15386f76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3ee0b7b4c73db687ffb8e4e638b58c6cb3a2b3d --- /dev/null +++ b/strmatch/kmp/8b329b44-3e34-461f-bced-a2cc15386f76.cpp @@ -0,0 +1,118 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + + + + + +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q + 1] != A[i]) + + q = pi[q]; + + if (A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void){ + + int i, q = 0, n = 0; + + fin.getline(A, 2000005); + + fin.getline(B, 2000005); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i - 1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i - 1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q + 1] != B[i]) + + q = pi[q]; + + if (A[q + 1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i - M; + + } + + } + + + + fout << n << "\n"; + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + fout << pos[i] << " "; + + fout << "\n"; + + + + return 0; + +} diff --git a/strmatch/kmp/8cdcb66b-de3f-48d1-8b91-42546783f4a5.cpp b/strmatch/kmp/8cdcb66b-de3f-48d1-8b91-42546783f4a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2317498c31cde960871f4450b653a87491769756 --- /dev/null +++ b/strmatch/kmp/8cdcb66b-de3f-48d1-8b91-42546783f4a5.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +vector build_lps(const string &pattern, const string &text) { + vector lps(pattern.size()); + + for (int i = 1, m = 0; i < pattern.size(); ++i) { + if (pattern[i] == pattern[m]) + lps[i] = ++m; + else if (m != 0) + m = lps[m - 1], --i; + else + lps[i] = 0; + } + + return lps; +} + +vector kmp(const string &pattern, const string &text) { + const auto lps = build_lps(pattern, text); + + vector matches; + + for (int i = 0, j = 0; j < text.size();) { + if (pattern[i] != text[j]) { + if (i == 0) + ++j; + else + i = lps[i - 1]; + } else { + ++i, ++j; + + if (i == pattern.size()) { + matches.push_back(j - i); + i = lps[i - 1]; + } + } + + } + + return matches; +} + +int main() { + ifstream in("strmatch.in"); + ofstream out("strmatch.out"); + + string pattern, text; + in >> pattern >> text; + + auto matches = kmp(pattern, text); + + out << matches.size() << '\n'; + int k = min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + out << matches[i] << ' '; +} diff --git a/strmatch/kmp/8d2b7cb2-ec76-43bc-a22a-48ffdefdc555.cpp b/strmatch/kmp/8d2b7cb2-ec76-43bc-a22a-48ffdefdc555.cpp new file mode 100644 index 0000000000000000000000000000000000000000..946be1c24d7f1ff4a862d20243d7840e75448288 --- /dev/null +++ b/strmatch/kmp/8d2b7cb2-ec76-43bc-a22a-48ffdefdc555.cpp @@ -0,0 +1,202 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +queue answers; + +int nr=0; + +const int maxSize=2000005; + + + +char A[maxSize],B[maxSize]; + + + +void read() + +{ + + fin>>A>>B; + +} + + + +void LPSarray(char* pat,int M,int* lps) + +{ + + int len=0,i=1; + + + + lps[0]=0; + + + + while(i0) + + { + + fout< +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +int main() { + string a; fin >> a; int m = a.size(); + string b; fin >> b; int n = b.size(); + + int k = 0; + vector pi(m); + for (int i = 1; i < m; i++) { + while (k && a[k] != a[i]) + k = pi[k - 1]; + if (a[k] == a[i]) + k++; + pi[i] = k; + } + + int cnt = 0; + vector sol; + + k = 0; + for (int i = 0; i < n; i++) { + while (k && a[k] != b[i]) + k = pi[k - 1]; + if (a[k] == b[i]) + k++; + if (k == m) { + cnt++; + if ((int) sol.size() < 1000) + sol.push_back(i - m + 1); + k = pi[k - 1]; + } + } + + fout << cnt << '\n'; + for (int pos : sol) + fout << pos << ' '; + fout << '\n'; + + fout.close(); + return 0; +} diff --git a/strmatch/kmp/8e896c60-7cd4-4b74-ba3a-6cf0ff9adb63.cpp b/strmatch/kmp/8e896c60-7cd4-4b74-ba3a-6cf0ff9adb63.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb3a00ce37a77284014bd0b3065c50eefed0ec8f --- /dev/null +++ b/strmatch/kmp/8e896c60-7cd4-4b74-ba3a-6cf0ff9adb63.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +int sufix[2000005], ans[1005], k, i, j, n, m; + +char s[2000005], t[2000005]; + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + + + f.getline(t, sizeof(t)); + + f.getline(s, sizeof(s)); + + + + n = strlen(s); + + m = strlen(t); + + j = 0; + + i = 1; + + + + while(i < m) + + { + + if(t[j] == t[i]) + + { + + sufix[i] = j + 1; + + j ++; + + i ++; + + } + + else if(j == 0) + + { + + sufix[i] = 0; + + i ++; + + } + + else j = sufix[j - 1]; + + + + } + + + + j = 0; + + for(i = 0; i < n; i ++) + + if(s[i] == t[j]) + + { + + j ++; + + if(j == m) + + { + + if(k <= 1002) + + { + + ans[++ k] = i - m + 1; + + j = sufix[j - 1]; + + } + + else k ++; + + } + + } + + else if(j == 0) continue; + + else + + { + + j = sufix[j - 1]; + + i --; + + } + + + + g << k << "\n"; + + for(i = 1; i <= min(k, 1000); i ++) + + g << ans[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/8f2eb136-bc8a-44cb-ac96-09c68ca470dd.cpp b/strmatch/kmp/8f2eb136-bc8a-44cb-ac96-09c68ca470dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d51574e87650125460fad1d020233e7aa72bb84c --- /dev/null +++ b/strmatch/kmp/8f2eb136-bc8a-44cb-ac96-09c68ca470dd.cpp @@ -0,0 +1,136 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int pattern[2000001]; + + + +void Pattern( string s1, int n) + +{ + + int i = 1, j = 0; + + while(i < n) + + { + + if(s1[i] == s1[j]) + + { + + j++; + + pattern[i] = j; + + i++; + + } + + else + + { + + if(j != 0) + + j = pattern[j - 1]; + + else + + { + + pattern[i] = 0; + + i++; + + } + + } + + + + } + + + +} + + + +int main() + +{ + + string s1, s2; + + fin >> s1 >> s2; + + int l1 = s1.length(), l2 = s2.length(), sol[1001]; + + Pattern(s1, l1); + + int i = 0, j = 0, k = 0, ct = 0; + + while(i < l2) + + { + + if(s2[i] == s1[j]) + + { + + i++; + + j++; + + } + + if(j == l1) + + { + + k++; + + if(ct < 1000) + + sol[ct++] = i - l1; + + j = pattern[j - 1]; + + } + + else if(s1[j] != s2[i] && i < l2) + + { + + if(j == 0) + + i++; + + else + + j = pattern[j - 1]; + + } + + } + + fout << k << '\n'; + + for(i = 0; i < ct; i++) + + fout << sol[i] <<' '; + +} diff --git a/strmatch/kmp/909df999-4f79-4118-ab62-650ae03d55bf.cpp b/strmatch/kmp/909df999-4f79-4118-ab62-650ae03d55bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7becc8d243e5f79a2aed3b3c11a94f0784e23a99 --- /dev/null +++ b/strmatch/kmp/909df999-4f79-4118-ab62-650ae03d55bf.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +using namespace std; + +using VI = vector; + +const int MaxN = 2000010; +int n, m; +VI pi, res; +char prop[MaxN], word[MaxN]; + +void Read(); +void KMP(); +void Write(); + +int main() +{ + Read(); + KMP(); + Write(); +} + +void KMP() +{ + pi = VI(n); + int q{0}; + for (int i = 1; i < n; i++) + { + while (q > 0 && word[q] != word[i]) + q = pi[q - 1]; + if (word[q] == word[i]) + q++; + pi[i] = q; + } + + q = 0; + for (int i = 0; i < m; i++) + { + while (q > 0 && word[q] != prop[i]) + q = pi[q - 1]; + if (word[q] == prop[i]) + q++; + if (q == n) + res.emplace_back(i - n + 1); + } +} + +void Write() +{ + ofstream fout("strmatch.out"); + fout << res.size() << '\n'; + for (int i = 0; i < min(1000, (int)res.size()); i++) + fout << res[i] << ' '; +} + +void Read() +{ + ifstream fin("strmatch.in"); + fin.getline(word, MaxN + 1); + fin.getline(prop, MaxN + 1); + n = strlen(word); + m = strlen(prop); +} diff --git a/strmatch/kmp/936ec83d-c03c-4a49-a419-ff6c7992fe23.cpp b/strmatch/kmp/936ec83d-c03c-4a49-a419-ff6c7992fe23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5cab509edda02c664c3d9fecbd7254cd92ed9f0a --- /dev/null +++ b/strmatch/kmp/936ec83d-c03c-4a49-a419-ff6c7992fe23.cpp @@ -0,0 +1,93 @@ +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char sir[2000005], pattern[2000005]; + +int prefix[2000005] , rez[1005]; + + + +int main() { + + + + int lsir = 0, lpattern = 0, i = 0, j = 0, n = 0; + + fin>>pattern>>sir; + + + + lsir = strlen(sir); + + lpattern = strlen(pattern); + + + + for(i = lsir; i >= 1; --i) + + sir[i] = sir[i-1]; + + for(i = lpattern; i >= 1; --i) + + pattern[i] = pattern[i-1]; + + + + for(i = 2; i <= lpattern; ++i) { + + while(j && pattern[j + 1] != pattern[i]) + + j = prefix[j]; + + if(pattern[i] == pattern[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + j = 0; + + for(i = 1; i <= lsir; ++i) { + + while(j && sir[i] != pattern[j+1]) + + j = prefix[j]; + + if(sir[i] == pattern[j+1]) + + ++j; + + if(j == lpattern) { + + j = prefix[lpattern]; + + ++n; + + if(n<=1000) + + rez[n]=i-lpattern; + + } + + } + + fout< +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000001], p[2000001]; + + + +int sol[1001], lps[2000001], cnt; + + + +void precalculare() + +{ + + int len = 0; + + int lg_p = strlen(p); + + lps[0] = 0; + + int i = 1; + + while (i < lg_p) { + + if (p[i] == p[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if (len != 0) { + + len = lps[len - 1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +void KMP(char* p, char* s) + +{ + + int lg_p = strlen(p); + + int lg_s = strlen(s); + + precalculare(); + + int i = 0, j = 0; + + while (i < lg_s) { + + if (s[i] == p[j]) + + { + + i++; + + j++; + + } + + if (j == lg_p) + + { + + cnt++; + + if (cnt <= 1000) + + sol[cnt] = i - j; + + j = lps[j - 1]; + + } + + else if (i < lg_s && s[i] != p[j]) + + { + + if (j != 0) + + j = lps[j - 1]; + + else + + i++; + + } + + } + +} + + + +int main() + +{ + + fin >> p; + + fin >> s; + + KMP(p, s); + + fout << cnt << '\n'; + + for (int i = 1; i <= min(cnt,1000); i++) { + + fout << sol[i] << ' '; + + } + +} diff --git a/strmatch/kmp/94a231d9-64c1-4a55-9089-b41daa52a5e5.cpp b/strmatch/kmp/94a231d9-64c1-4a55-9089-b41daa52a5e5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e4239a03d73addf7a10aa621d940696a1028352 --- /dev/null +++ b/strmatch/kmp/94a231d9-64c1-4a55-9089-b41daa52a5e5.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX = 2000005; + +int phi[2 * NMAX]; + +vectorsol; + + + +void kmp(string &s) + +{ + + phi[0] = -1; + + for (int i = 1; i < s.size(); i++) + + { + + int k = i - 1; + + while (phi[k] != -1 && s[phi[k] + 1] != s[i]) + + k = phi[k]; + + if (s[phi[k] + 1] == s[i]) + + phi[i] = phi[k] + 1; + + else + + phi[i] = -1; + + } + +} + + + +int main() + +{ + + string a, b; + + fin >> a; + + fin >> b; + + string s = a + "#" + b; + + int na = a.size(); + + int ns = s.size(); + + kmp(s); + + for (int i = na + 1; i < ns; i++) + + { + + if (phi[i] == na-1) + + sol.push_back(i - 2*na); + + } + + int mi = min((int)sol.size(), 1000); + + fout << sol.size() << "\n"; + + for (int i = 0; i < mi; i++) + + fout << sol[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/96c7b6ea-2cb8-40e5-969d-4f3979abb0ec.cpp b/strmatch/kmp/96c7b6ea-2cb8-40e5-969d-4f3979abb0ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a40ff3dcc4417c0f5e99c3ab34772d8a593139af --- /dev/null +++ b/strmatch/kmp/96c7b6ea-2cb8-40e5-969d-4f3979abb0ec.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +const int MAXN = 2000005; + +const int MAXA = 1000; + + + +char a[MAXN], b[MAXN]; + +int pi[MAXN]; + +int pos[MAXA + 5]; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", a + 1); + + scanf("%s", b + 1); + + int n = strlen(a + 1); + + int m = strlen(b + 1); + + int x = 0; + + for (int i = 2; i <= n; ++i) { + + while (x && a[x + 1] != a[i]) + + x = pi[x]; + + if (a[x + 1] == a[i]) + + x++; + + pi[i] = x; + + } + + + + int k = 0; + + x = 0; + + for (int i = 1; i <= m; ++i) { + + while (x && b[i] != a[x + 1]) + + x = pi[x]; + + if (b[i] == a[x + 1]) + + x++; + + if (x == n) { + + k++; + + if (k <= MAXA) + + pos[k] = i - n; + + } + + } + + + + printf("%d\n", k); + + for (int i = 1; i <= min(k, MAXA); ++i) + + printf("%d ", pos[i]); + + + + return 0; + +} diff --git a/strmatch/kmp/97208c16-16f0-4037-8612-ce766a605b72.cpp b/strmatch/kmp/97208c16-16f0-4037-8612-ce766a605b72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2eda88b1a8f6ad5ca70b806077945f72782fc147 --- /dev/null +++ b/strmatch/kmp/97208c16-16f0-4037-8612-ce766a605b72.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_LENGTH = 2000000; + + + +char a[MAX_LENGTH + 1]; + +char b[MAX_LENGTH + 1]; + +char united[2 * MAX_LENGTH + 1]; + +int pi[2 * MAX_LENGTH]; + + + +void calc_pi(char s[]) { + + int n; + + + + n = strlen(s); + + pi[0] = 0; + + for (int i = 1; i < n; i++) { + + int index = i - 1; + + + + while (s[i] != s[pi[index]] && index > 0) + + index = pi[index] - 1; + + + + if (index >= 0 && s[i] == s[pi[index]]) + + pi[i] = pi[index] + 1; + + else + + pi[i] = 0; + + } + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s\n%s", a, b); + + + + int n1, n2, n; + + + + n1 = strlen(a); + + n2 = strlen(b); + + n = n1 + 1 + n2; + + + + for (int i = 0; i < n1; i++) + + united[i] = a[i]; + + + + united[n1] = '*'; + + + + for (int i = 0; i < n2; i++) + + united[n1 + 1 + i] = b[i]; + + + + + + calc_pi(united); + + + + vector sol; + + for (int i = 2 * n1; i < n; i++) { + + if (pi[i] == n1) + + sol.push_back(i - 2 * n1); + + } + + + + printf("%d\n", sol.size()); + + for (int i = 0; i < min(1000, (int)sol.size()); i++) + + printf("%d ", sol[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/97577afb-3761-4efe-9410-1b118626211b.cpp b/strmatch/kmp/97577afb-3761-4efe-9410-1b118626211b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fdc08e593bb34fb5b42602009dec1e3f628e0b7 --- /dev/null +++ b/strmatch/kmp/97577afb-3761-4efe-9410-1b118626211b.cpp @@ -0,0 +1,119 @@ +#include +#include + + + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char model[2000000 +5], s[2000000 +5]; + +int pr[2000000 +5], cod[1005]; + +int n,m,cnt; + + + +inline void prefix() + +{ + + int i, q; + + q=-1; + + pr[0]=-1; + + for(i=1;i>model; in.get(); + + in>>s; + + m=strlen(model); + + n=strlen(s); + + + + prefix(); + + solve(); + + + + out<1000) cnt=1000; + + for(int i=1;i<=cnt;i++) out< +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q+1] != A[i]) + + q = pi[q]; + + if (A[q+1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void) + +{ + + int i, q = 0, n = 0; + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + fgets(A, sizeof(A), stdin); + + fgets(B, sizeof(B), stdin); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i-1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i-1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q+1] != B[i]) + + q = pi[q]; + + if (A[q+1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i-M; + + } + + } + + + + printf("%d\n", n); + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + printf("%d ", pos[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/98f41291-132c-422c-9e1f-6c2051082704.cpp b/strmatch/kmp/98f41291-132c-422c-9e1f-6c2051082704.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2eda88b1a8f6ad5ca70b806077945f72782fc147 --- /dev/null +++ b/strmatch/kmp/98f41291-132c-422c-9e1f-6c2051082704.cpp @@ -0,0 +1,131 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_LENGTH = 2000000; + + + +char a[MAX_LENGTH + 1]; + +char b[MAX_LENGTH + 1]; + +char united[2 * MAX_LENGTH + 1]; + +int pi[2 * MAX_LENGTH]; + + + +void calc_pi(char s[]) { + + int n; + + + + n = strlen(s); + + pi[0] = 0; + + for (int i = 1; i < n; i++) { + + int index = i - 1; + + + + while (s[i] != s[pi[index]] && index > 0) + + index = pi[index] - 1; + + + + if (index >= 0 && s[i] == s[pi[index]]) + + pi[i] = pi[index] + 1; + + else + + pi[i] = 0; + + } + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s\n%s", a, b); + + + + int n1, n2, n; + + + + n1 = strlen(a); + + n2 = strlen(b); + + n = n1 + 1 + n2; + + + + for (int i = 0; i < n1; i++) + + united[i] = a[i]; + + + + united[n1] = '*'; + + + + for (int i = 0; i < n2; i++) + + united[n1 + 1 + i] = b[i]; + + + + + + calc_pi(united); + + + + vector sol; + + for (int i = 2 * n1; i < n; i++) { + + if (pi[i] == n1) + + sol.push_back(i - 2 * n1); + + } + + + + printf("%d\n", sol.size()); + + for (int i = 0; i < min(1000, (int)sol.size()); i++) + + printf("%d ", sol[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/99752a07-994b-43c6-8319-73f4e0e19326.cpp b/strmatch/kmp/99752a07-994b-43c6-8319-73f4e0e19326.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b943d98e9ee77d1ef0bf0f66dc69b80dfe79527 --- /dev/null +++ b/strmatch/kmp/99752a07-994b-43c6-8319-73f4e0e19326.cpp @@ -0,0 +1,91 @@ +#include +#include + + + + +char s[2000001], t[2000001]; + +int pi[2000001]; + +void compute () { + + int i=1, len=0; + + while (t[i]) + + if (t[i]==t[len]) + + pi[i++]=++len; + + else + + if (len) + + len=pi[len-1]; + + else + + pi[i++]=0; + +} + + + +int main () { + + FILE *fin=fopen("strmatch.in", "r"), + + *fout=fopen("strmatch.out", "w"); + + fgets(t, sizeof t, fin); + + fgets(s, sizeof s, fin); + + compute(); + + + + fprintf (fout, " \n"); + + int i=0, j=0, ct=0; + + while (s[i]) { + + if (s[i]==t[j]) { + + ++i, ++j; + + if (!t[j] || isspace(t[j])) { + + if (ct<1000) + + fprintf (fout, "%d ", i-j); + + ct++; + + j=pi[j-1]; + + } + + } + + else + + if (j) + + j=pi[j-1]; + + else + + ++i; + + } + + fseek(fout, 0, SEEK_SET); + + fprintf(fout, "%d", ct); + + return 0; + +} diff --git a/strmatch/kmp/9a663731-1943-4481-a910-553ea14a4843.cpp b/strmatch/kmp/9a663731-1943-4481-a910-553ea14a4843.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea24b0e8af3e69b6881d9eef86fa7fa72b378800 --- /dev/null +++ b/strmatch/kmp/9a663731-1943-4481-a910-553ea14a4843.cpp @@ -0,0 +1,198 @@ +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Dim=2e6+5; + +const int Mod1=1e6+7; + +const int Mod2=1e6+21; + +const int d=91; + + + +void P1() + +{ + + int ans=0; + + string A,B; + + f>>A>>B; + + int N=A.size(),M=B.size(); + + for(int i=N;i>=1;i--) A[i]=A[i-1]; + + for(int i=M;i>=1;i--) B[i]=B[i-1]; + + + + vector < int > dp1(N+1),dp2(M+1),V; + + dp1[1]=0; + + for(int i=2;i<=N;i++) + + { + + int K=dp1[i-1]; + + while( K>0 && A[K+1]!=A[i] ) K=dp1[K]; + + + + dp1[i]=K + (A[K+1]==A[i]); + + } + + dp2[0]=0; + + for(int i=1;i<=M;i++) + + { + + int K=dp2[i-1]; + + while( K>0 && A[K+1]!=B[i] ) K=dp1[K]; + + + + dp2[i]=K+(A[K+1]==B[i]); + + + + if(dp2[i]==N) + + { + + ans++; + + if(ans<=1000) V.push_back(i-N); + + } + + } + + cout<>A>>B; + + int ans=0; + + int hashP1=0,hashP2=0,hashS1=0,hashS2=0,cos1=1,cos2=1; + + int N=A.size(),M=B.size(); + + + + vector < int > V; + + + + for(int i=0;i +#include +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +string s, w; + + +int t[2000001], n, m; + + +void precalc() +{ + int i, k = 0; + t[0] = -1; + for (i = 1; i= 0 && w[i] != w[k]) + k = t[k]; + } + } + t[w.size()] = k; +} + +int ap[1001]; + +int main() +{ + fin >> w >> s; + precalc(); + int i, k = -1; + for (i = 0; i= 0 && w[k] != s[i]) + k = t[k]; + if (k < 0) + k = 0; + if (w[k] == s[i]) + { + k++; + if (k == w.size()) + { + ap[0]++; + if (ap[0] <= 1000) + ap[ap[0]] = i-k+1; + k = t[k]; + } + } + } + fout << ap[0] << '\n'; + for (i = 1; i<=min(ap[0], 1000); i++) + fout << ap[i] << ' '; + return 0; +} diff --git a/strmatch/kmp/9b4db06a-2a47-4bdb-a51b-11fabebd0584.cpp b/strmatch/kmp/9b4db06a-2a47-4bdb-a51b-11fabebd0584.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad9d019f7f2172d941150d16c437ea822a8af1a3 --- /dev/null +++ b/strmatch/kmp/9b4db06a-2a47-4bdb-a51b-11fabebd0584.cpp @@ -0,0 +1,94 @@ +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int N,M,k,nr,i,p[2000010],sol[1010]; + +char c[2000010],s[2000010]; + + + +int main() + +{ + + f>>(c+1); + + f>>(s+1); + + + + N=strlen(c+1); + + M=strlen(s+1); + + + + p[1]=0; + + k=0; + + + + for(i=2;i<=N;i++) + + { + + while(k!=0 && c[k+1]!=c[i])k=p[k]; + + if(c[k+1]==c[i])k++; + + p[i]=k; + + } + + + + k=0; + + for(i=1;i<=M;i++) + + { + + while(k!=0 && c[k+1]!=s[i])k=p[k]; + + if(c[k+1]==s[i])k++; + + + + if(k==N) + + { + + nr++; + + if(nr<=1000) + + { + + sol[nr]=i-N; + + } + + } + + } + + + + g< + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +char a[2000005],b[2000005]; + +int n,m,p[2000005],ans[2000005],k; + +int main() + +{ + + in>>(a+1); + + in>>(b+1); + + n=strlen(a+1); + + m=strlen(b+1); + + int i=2,j=0; + + while(i<=n) + + { + + while(a[i]!=a[j+1]&&j>0) j=p[j]; + + if(a[i]==a[j+1]) j++; + + p[i]=j; + + i++; + + } + + j=0; + + i=1; + + while(i<=m) + + { + + while(b[i]!=a[j+1]&&j>0) j=p[j]; + + if(b[i]==a[j+1]) j++; + + if(j==n) + + { + + ans[++k]=i-n; + + j=p[n]; + + } + + i++; + + } + + out< +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string s1, s2; + + + +int prefix[2000005], sol[1005]; + + + + + +int main() + +{ + + fin >> s1 >> s2; + + + + int poz = 0; + + + + int j = 0; + + + + for(int i = 1; i < s1.size(); i++) + + { + + while(s1[i] != s1[j] && j != 0) + + j = prefix[j - 1]; + + + + if(s1[i] == s1[j]) + + j++; + + + + prefix[i] = j; + + } + + + + j = 0; + + + + int k = 0; + + + + for(int i = 0 ; i < s2.size(); i++) + + { + + while(s2[i] != s1[j] && j != 0) + + j = prefix[j - 1]; + + + + if(s2[i] == s1[j]) + + { + + j++; + + if(j == s1.size()) + + { + + k++; + + + + if(k <= 1000) + + sol[k] = i - j + 1; + + j = prefix[j - 1]; + + } + + } + + } + + fout << k << '\n'; + + + + for(int i = 1; i <= min(1000, k); i++) + + fout << sol[i] << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/9d01c4c1-e860-4fc9-a28f-f51c4706d7d5.cpp b/strmatch/kmp/9d01c4c1-e860-4fc9-a28f-f51c4706d7d5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3c9083cb6803a5047b2cd36623ce9600b1a84c0 --- /dev/null +++ b/strmatch/kmp/9d01c4c1-e860-4fc9-a28f-f51c4706d7d5.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +constexpr auto NMAX = 1000; + + + + + +std::ifstream fin ("strmatch.in"); + +std::ofstream fout ("strmatch.out"); + + + +int n, m, cnt; + +std::string text, pattern; + +std::vector lps; + +std::set Store; + + + + + +inline void Compute_LPS () { + + int len = 0, i = 1; + + + + while (i < m) { + + if (pattern[i] == pattern[len]) + + ++ len, lps[i] = len, ++ i; + + else { + + if (len) len = lps[len - 1]; + + else lps[i] = 0, ++ i; + + } + + } + +} + + + +inline void KMP_Search () { + + int i = 0, j = 0; + + n = (int)text.size (), m = (int)pattern.size (); + + lps.assign (m, 0); + + if (n < m) goto end; + + + + Compute_LPS (); + + while (i < n) { + + if (text[i] == pattern[j]) + + ++ i, ++ j; + + if (j == m) { + + ++ cnt; + + if (n > NMAX) { + + if (cnt - 1 < NMAX) + + Store.insert (i - j); + + } + + else Store.insert (i - j); + + j = lps[j - 1]; + + } + + else if (i < n && text[i] != pattern[j]) { + + if (j) j = lps[j - 1]; + + else ++ i; + + } + + } + + end : ; + +} + + + + + +int main () { + + fin >> pattern >> text; + + KMP_Search (); + + fout << cnt << "\n"; + + std::copy (Store.begin (), Store.end (), std::ostream_iterator (fout, " ")); + + + + fin.close (), fout.close (); + + return 0; + +} diff --git a/strmatch/kmp/9d04e704-4b19-44db-9293-8de3a0f92063.cpp b/strmatch/kmp/9d04e704-4b19-44db-9293-8de3a0f92063.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0fcf38810ef2b43685eedca084848dd478cd84 --- /dev/null +++ b/strmatch/kmp/9d04e704-4b19-44db-9293-8de3a0f92063.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + + + +int main() + +{ + + string pattern; + + string text; + + + + fin >> pattern >> text; + + + + vector PI(pattern.size() + 1); + + + + for(int i = 1, k = 0; i < (int)pattern.size(); ++i) + + { + + while(pattern[i] != pattern[k] && k > 0) k = PI[k - 1]; + + + + if(pattern[i] == pattern[k]) ++k; + + + + PI[i] = k; + + } + + + + vector answer; + + + + for(int i = 0, k = 0; i < (int)text.size(); ++i) + + { + + while(text[i] != pattern[k] && k > 0) k = PI[k - 1]; + + + + if(text[i] == pattern[k]) ++k; + + + + if(k == (int)pattern.size()) + + { + + answer.push_back(i - pattern.size() + 1); + + } + + } + + + + fout << answer.size() << '\n'; + + + + for(int i = 0; i < min(1000, (int)answer.size()); ++i) + + fout << answer[i] << ' '; + +} diff --git a/strmatch/kmp/9d59527e-96bb-4412-8252-49188997bddd.cpp b/strmatch/kmp/9d59527e-96bb-4412-8252-49188997bddd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1621d5ae6837e65ba5f5eb9a188777ffad781aac --- /dev/null +++ b/strmatch/kmp/9d59527e-96bb-4412-8252-49188997bddd.cpp @@ -0,0 +1,90 @@ +#include + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int n, m, i, p, nr; + +int sol[2000005], l[2000005]; + + + +char a[2000005], b[2000005]; + + + +int main(){ + + fin >> a + 1; + + fin >> b + 1; + + n = strlen (a + 1); + + m = strlen (b + 1); + + for (i=2; i<=n; i++){ + + while (a[i] != a[p+1] && p != 0){ + + p = l[p]; + + } + + if (a[i] == a[p+1]){ + + p++; + + } + + l[i] = p; + + } + + p = 0; + + for (i=1; i<=m; i++){ + + while (b[i] != a[p+1] && p != 0){ + + p = l[p]; + + } + + if (b[i] == a[p+1]){ + + p++; + + } + + if (p == n){ + + sol[++nr] = i - p; + + p = l[p]; + + } + + } + + fout << nr << "\n"; + + nr = min (nr, 1000); + + for (i=1; i<=nr; i++){ + + fout << sol[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/kmp/9fddd4f2-082c-4fa1-8586-ae919b82f3a1.cpp b/strmatch/kmp/9fddd4f2-082c-4fa1-8586-ae919b82f3a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de32c3bb577b1f623bb0ed50a1d43a9c43ea4e17 --- /dev/null +++ b/strmatch/kmp/9fddd4f2-082c-4fa1-8586-ae919b82f3a1.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + + + +const int mod=1e9+7; + +const int modu=1999999973; + +const int modul=998244353; + + + +int ans,pref[6000005]; + +vectorindx; + +string s,t; + + + +void kmp(){ + + t=(s+"#"+t); + + int j=0; + + for(int i=1;i=s.size()) indx.push_back(i); + + } + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + cin >> s >> t; + + kmp(); + + cout << indx.size() << '\n'; + + int g=indx.size(); + + for(int i=0;i +#include +#include +#include + + +using namespace std; + + + +const int MAX_LENGTH = 2000000; + + + +char a[MAX_LENGTH + 1]; + +char b[MAX_LENGTH + 1]; + +char united[2 * MAX_LENGTH + 1]; + +int pi[2 * MAX_LENGTH]; + + + +void calc_pi(char s[]) { + + int n; + + + + n = strlen(s); + + pi[0] = 0; + + for (int i = 1; i < n; i++) { + + int index = i - 1; + + + + while (s[i] != s[pi[index]] && index > 0) + + index = pi[index] - 1; + + + + if (index >= 0 && s[i] == s[pi[index]]) + + pi[i] = pi[index] + 1; + + else + + pi[i] = 0; + + } + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s\n%s", a, b); + + + + int n1, n2, n; + + + + n1 = strlen(a); + + n2 = strlen(b); + + n = n1 + 1 + n2; + + + + for (int i = 0; i < n1; i++) + + united[i] = a[i]; + + + + united[n1] = '*'; + + + + for (int i = 0; i < n2; i++) + + united[n1 + 1 + i] = b[i]; + + + + + + calc_pi(united); + + + + vector sol; + + for (int i = 2 * n1; i < n; i++) { + + if (pi[i] == n1) + + sol.push_back(i - 2 * n1); + + } + + + + printf("%d\n", sol.size()); + + for (int i = 0; i < min(1000, (int)sol.size()); i++) + + printf("%d ", sol[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/a0ba263b-f98a-40f6-a119-8e2b91bc1caa.cpp b/strmatch/kmp/a0ba263b-f98a-40f6-a119-8e2b91bc1caa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b5074f9f7247e2065523fd1f1c4d5a38093abf31 --- /dev/null +++ b/strmatch/kmp/a0ba263b-f98a-40f6-a119-8e2b91bc1caa.cpp @@ -0,0 +1,136 @@ +#include + + +using namespace std; + +ifstream in ("strmatch.in") ; + +ofstream out ("strmatch.out") ; + +char caut[2000001], text[2000001]; + +int nr_r, r[2000001], l[2000001]; + + + +void make_pattern() + +{ + + int lp=strlen(caut); + + int i, j; + + l[0]=0; + + i=1; + + j=0; + + while(i1000) + + nr_r=1000; + + for(int i=0; i + + +using namespace std; + + + +const int NMAX = 4000005; + +int pi[NMAX]; + +string s, t; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + for (int i = 1, k = 0;i < s.length();++i) + + { + + for (;k > 0 && s[k] != s[i];k = pi[k - 1]); + + if (s[k] == s[i]) + + ++k; + + pi[i] = k; + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + { + + if (pi[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - 2 * sz); + + } + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/a25b8edd-2b72-48b9-a706-6aec12c32e54.cpp b/strmatch/kmp/a25b8edd-2b72-48b9-a706-6aec12c32e54.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e04ad006cc0c7fc5a9f5cd3a4de08f0b9d059f3c --- /dev/null +++ b/strmatch/kmp/a25b8edd-2b72-48b9-a706-6aec12c32e54.cpp @@ -0,0 +1,196 @@ +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string pat, txt; + +vector rez; + +void computeLPSArray(int M, int *lps); + + + + + +void KMPSearch() + +{ + + int M = pat.size(); + + int N = txt.size(); + + + + + + + + int lps[M]; + + + + + + computeLPSArray(M, lps); + + + + int i = 0; + + int j = 0; + + while (i < N) + + { + + if (pat[j] == txt[i]) + + { + + j++; + + i++; + + } + + + + if (j == M) + + { + + rez.push_back(i - j); + + j = lps[j - 1]; + + } + + + + + + else if (i < N && pat[j] != txt[i]) + + { + + + + + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + + } + +} + + + + + +void computeLPSArray(int M, int *lps) + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + + + int i = 1; + + while (i < M) + + { + + if (pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + + + + + + + if (len != 0) + + { + + len = lps[len - 1]; + + + + + + + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + + + +int main() + +{ + + f >> pat >> txt; + + KMPSearch(); + + g << rez.size() << '\n'; + + for (int i = 0; i < min(int(rez.size()), 1000); i++) + + g << rez[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/a2d06107-5f81-4a22-a572-3d9e87511b04.cpp b/strmatch/kmp/a2d06107-5f81-4a22-a572-3d9e87511b04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fdd7d7fc712874214c98094f1d6bce9a59dae8ea --- /dev/null +++ b/strmatch/kmp/a2d06107-5f81-4a22-a572-3d9e87511b04.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +using namespace std; + +const int NMAX = 2000000; + +const int MMAX = 2000000; + +int urm[MMAX + 5]; + +char t[NMAX + 5] , p[MMAX + 5]; + +vector ans; + +int main() + +{ + + freopen("strmatch.in" , "r" , stdin); + + freopen("strmatch.out" , "w" , stdout); + + int n , m , i , k , nr; + + scanf("%s\n%s" , (p + 1) , (t + 1)); + + n = strlen(t + 1); + + m = strlen(p + 1); + + k = 0; + + urm[1] = 0; + + for(i = 2 ; i <= m ; i ++) + + { + + while(k > 0 && p[k + 1] != p[i]) + + k = urm[k]; + + if(p[k + 1] == p[i]) + + k ++; + + urm[i] = k; + + } + + k = nr = 0; + + for(i = 1 ; i <= n ; i ++) + + { + + while(k > 0 && p[k + 1] != t[i]) + + k = urm[k]; + + if(p[k + 1] == t[i]) + + k ++; + + if(k == m) + + { + + nr ++; + + if(ans.size() < 1000) + + ans.push_back(i - m); + + k = urm[k]; + + } + + } + + printf("%d\n" , nr); + + for(i = 0 ; i < ans.size() ; i ++) + + printf("%d " , ans[i]); + + return 0; + +} diff --git a/strmatch/kmp/a2de6490-d5a7-4327-b269-678f6c01dce6.cpp b/strmatch/kmp/a2de6490-d5a7-4327-b269-678f6c01dce6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f22d57d10bf5325dcc4c3e7f3e2282b6534ee6f --- /dev/null +++ b/strmatch/kmp/a2de6490-d5a7-4327-b269-678f6c01dce6.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 2000005; + + + +string txt, pattern; + + + +int lps[nmax]; + + + +void CreateLPS() + +{ + + int k = 1, len = 0, stop = pattern.length(); + + + + while (k < stop) + + { + + if (pattern[k] == pattern[len]) + + lps[k++] = ++len; + + else + + { + + if (len) + + len = lps[len - 1]; + + else + + k++; + + } + + } + +} + + + +int main() + +{ + + fin >> pattern >> txt; + + + + CreateLPS(); + + + + fout << " \n"; + + + + int i = 0, j = 0, stop = txt.length(), c = 0; + + + + while (i < stop) + + { + + if (pattern[j] == txt[i]) + + { + + i++; + + j++; + + } + + + + if (j == pattern.length()) + + { + + if (c < 1000) + + fout << i - j << ' '; + + + + c++; + + + + j = lps[j - 1]; + + } + + else if (j < pattern.length() && pattern[j] != txt[i]) + + { + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + + + + + } + + + + fout.close(); + + fout.open("strmatch.out", ios::in|ios::out); + + + + fout << c; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/a321b01b-cda1-4e8d-88bf-cbaebed66c46.cpp b/strmatch/kmp/a321b01b-cda1-4e8d-88bf-cbaebed66c46.cpp new file mode 100644 index 0000000000000000000000000000000000000000..632751abb02f9e407594bfcb03aa31816af09ad5 --- /dev/null +++ b/strmatch/kmp/a321b01b-cda1-4e8d-88bf-cbaebed66c46.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string pat, txt; + +vector rez; + +void computeLPSArray(int M, int *lps) + +{ + + int len = 0, i = 1; + + lps[0] = 0; + + while (i < M) + + if (pat[i] == pat[len]) + + len++, lps[i] = len, i++; + + else if (len != 0) + + len = lps[len - 1]; + + else + + lps[i] = 0, i++; + +} + +void KMPSearch() + +{ + + int M = pat.size(), N = txt.size(), lps[M], i = 0, j = 0; + + computeLPSArray(M, lps); + + while (i < N) + + { + + if (pat[j] == txt[i]) + + j++, i++; + + if (j == M) + + rez.push_back(i - j), j = lps[j - 1]; + + else if (i < N && pat[j] != txt[i]) + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + +} + +int main() + +{ + + f >> pat >> txt; + + KMPSearch(); + + g << rez.size() << '\n'; + + for (int i = 0; i < min(int(rez.size()), 1000); i++) + + g << rez[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/a4388464-21a1-44ca-8379-fce46eb1fe9d.cpp b/strmatch/kmp/a4388464-21a1-44ca-8379-fce46eb1fe9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3d5ee71ff1202c184c518dadf7cac0719cc081a --- /dev/null +++ b/strmatch/kmp/a4388464-21a1-44ca-8379-fce46eb1fe9d.cpp @@ -0,0 +1,100 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string a, b; + +int p[2000004], j, s; + +queue q; + + + +void getP() { + + for (int i = 2; i < a.length(); i++) { + + while (j && a[j + 1] != a[i]) + + j = p[j]; + + if (a[j + 1] == a[i]) + + j++; + + p[i] = j; + + } + +} + + + +int main() { + + f >> a >> b; + + a = "0" + a; + + b = "0" + b; + + + + getP(); + + + + j = 0; + + for (int i = 1; i < b.length(); i++) { + + while (j && a[j + 1] != b[i]) + + j = p[j]; + + if (a[j + 1] == b[i]) + + j++; + + if (j == a.length()-1) { + + q.push(i-a.length()+1); + + j = p[j]; + + } + + } + + + + g << q.size() << '\n'; + + s = min((int)q.size(), 1000); + + for (int i = 1; i <= s; i++) { + + g << q.front() << ' '; + + q.pop(); + + } + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/strmatch/kmp/a4832e5b-9d2a-4800-b6e9-91196686997a.cpp b/strmatch/kmp/a4832e5b-9d2a-4800-b6e9-91196686997a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b6ef8b080676fd775570dffe3fd4ec2bcfe4079 --- /dev/null +++ b/strmatch/kmp/a4832e5b-9d2a-4800-b6e9-91196686997a.cpp @@ -0,0 +1,138 @@ +#include + + +using namespace std; + + + +ifstream fin( "strmatch.in" ); + +ofstream fout( "strmatch.out" ); + + + +const int NMAX = 2000005; + + + +string A, pat; + +int lps[NMAX]; + + + +void Read() { + + fin >> pat >> A; + +} + + + +void Lps() + +{ + + int lg = 0; + + + + for( int i = 1; i < pat.size(); ++i ) + + if( pat[i] == pat[lg] ) lps[i] = ++lg; + + else + + if( lg > 0 ) { + + lg = lps[lg - 1]; + + --i; + + } + +} + + + +void Do() + +{ + + Lps(); + + + + int nr = 0; + + int i, j; + + vector pos; + + + + i = j = 0; + + + + while( i < A.size() ) { + + if( A[i] == pat[j] ) { + + ++i; + + ++j; + + } + + + + if( j == pat.size() ) { + + ++nr; + + if( pos.size() < 1000 ) pos.push_back( i - j ); + + + + j = lps[j - 1]; + + } + + else + + if( i < A.size() && pat[j] != A[i] ) + + if( j > 0 ) j = lps[j - 1]; + + else ++i; + + } + + + + fout << nr << '\n'; + + + + for( int i = 0; i < pos.size(); ++i ) + + fout << pos[i] << ' '; + +} + + + +int main() + +{ + + Read(); + + Do(); + + + + return 0; + +} diff --git a/strmatch/kmp/a4f7f3bf-4b9b-4572-a3f7-18023728814e.cpp b/strmatch/kmp/a4f7f3bf-4b9b-4572-a3f7-18023728814e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..481552963be8f6c992c51fc8d70aa2786fa4c7b5 --- /dev/null +++ b/strmatch/kmp/a4f7f3bf-4b9b-4572-a3f7-18023728814e.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char A[2000002],B[2000002]; + +int pi[2000002],a,b,nr=0,sol[1002]; + + + +void kmp() + +{ + + int k=0; + + for(int i=1;i<=b;++i) + + { + + while(k && A[k+1]!=B[i]) k=pi[k]; + + if(A[k+1]==B[i]) ++k; + + if(k==a) + + { + + ++nr; + + if(nr<=1000) + + sol[nr]=i; + + } + + } + +} + +void kmp1() + +{ + + int k=0; + + for(int i=2;i<=a;++i) + + { + + while(k && A[k+1]!=A[i]) k=pi[k]; + + if(A[k+1]==A[i]) ++k; + + pi[i]=k; + + } + +} + +int main() + +{ + + f>>A+1; + + f>>B+1; + + a=strlen(A+1); + + b=strlen(B+1); + + kmp1(); + + kmp(); + + g< +#include +#include +#include + + +using namespace std; + + + +string w, t; + +vector < int > sol, aux; + + + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + getline(f, w); + + getline(f, t); + + aux.resize(w.size() + 2, 0); + + + + int p = 1, m = 0; + + while(p < w.size()) + + { + + if(w[p] == w[m]) + + { + + m++; + + aux[p] = m; + + p++; + + } + + else + + { + + if(m) + + { + + m = aux[m - 1]; + + } + + else + + { + + aux[p] = 0; + + p++; + + } + + } + + } + + + + int i = 0, j = 0, nr = 0; + + while(j < t.size()) + + { + + if(t[j] != w[i]) + + { + + if(!i) j++; + + else i = aux[i - 1]; + + } + + else + + { + + i++; + + j++; + + if(i == w.size()) + + { + + nr++; + + if(nr <= 1000) + + sol.push_back(j - i); + + i = aux[i - 1]; + + } + + } + + } + + + + g << nr << '\n'; + + for(int x: sol) + + g << x << ' '; + + g << '\n'; + + f.close(); + + g.close(); + + return 0; + +} diff --git a/strmatch/kmp/a7034ea1-8cdd-401f-9571-39bc56faf062.cpp b/strmatch/kmp/a7034ea1-8cdd-401f-9571-39bc56faf062.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1c05f0f971a590417e248d32c326803b5954274 --- /dev/null +++ b/strmatch/kmp/a7034ea1-8cdd-401f-9571-39bc56faf062.cpp @@ -0,0 +1,62 @@ +#include + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +string cuv,sir; +int pref[2000001]; +int sol[1001],t; + +void createPref() +{ + int lung=0; + + for(int i=2;i0 && cuv[i]!=cuv[lung+1]) + lung=pref[lung]; + + if(cuv[i]==cuv[lung+1]) + lung++; + + pref[i]=lung; + } + +} + +int main() +{ + in>>cuv>>sir; + cuv.insert(cuv.begin(),' '); + sir.insert(sir.begin(),' '); + + createPref(); + + int lung=0; + for(int i=1;i0 && sir[i]!=cuv[lung+1]) + lung=pref[lung]; + + if(sir[i]==cuv[lung+1]) + lung++; + + if(lung==cuv.size()-1) + { + t++; + if(t<=1000) + sol[t]=i-cuv.size()+1; + + lung=pref[lung]; + } + } + + out< +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char a[2000005],b[2000005]; + +int i,j,n,m,PI[2000005],k,sol[2000005]; + +int main() + +{ + + f>>b+1>>a+1; + + n=strlen(a+1); + + m=strlen(b+1); + + i=2; + + j=0; + + while(i<=m) + + { + + while (j&&b[j+1]!=b[i]) + + j=PI[j]; + + if (b[j+1]==b[i]) + + j++; + + PI[i]=j; + + i++; + + } + + i=1; + + j=0; + + while(i<=n) + + { + + while(a[i]!=b[j+1]&&j!=0) + + j=PI[j]; + + if(a[i]==b[j+1]) + + j++; + + if(j==m) + + { + + sol[++k]=i-m; + + j=PI[j]; + + } + + i++; + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000001], p[2000001]; + + + +int sol[1001], lps[2000001], cnt,ccnt; + + + +void computeLPSArray(char* pat, int M, int* lps); + + + +void KMPSearch(char* pat, char* txt) + +{ + + int M = strlen(pat); + + int N = strlen(txt); + computeLPSArray(pat, M, lps); + + + + int i = 0; + + int j = 0; + + while (i < N) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + } + + + + if (j == M) { + + + + + + if (cnt <= 999) { + + cnt++; + + ccnt++; + + sol[cnt] = i - j; + + } + + else + + { + + ccnt++; + + } + + j = lps[j - 1]; + + } + + + + + + else if (i < N && pat[j] != txt[i]) { + + + + + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + + } + +} + + + + + +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + + + int i = 1; + + while (i < M) { + + if (pat[i] == pat[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + + + + + + + if (len != 0) { + + len = lps[len - 1]; + + + + + + + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +int main() + +{ + + fin >> p; + + fin >> s; + + KMPSearch(p, s); + + fout << ccnt << '\n'; + + for (int i = 1; i <= cnt; i++) { + + fout << sol[i] << ' '; + + } + +} diff --git a/strmatch/kmp/a91860f9-a690-4383-a918-e7ea712f8838.cpp b/strmatch/kmp/a91860f9-a690-4383-a918-e7ea712f8838.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9da8dc267a101d46cc11a42e2afef492f0a229d5 --- /dev/null +++ b/strmatch/kmp/a91860f9-a690-4383-a918-e7ea712f8838.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include + + + +using namespace std; + + + + + + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +char A[2000005], B[2000005]; +int pi[2000005], v[1004], nr, a, b; + +void kmp1(){ + int k = 0; + for(int i = 1; i <= b; ++i){ + while(k && A[k + 1] != B[i]) + k = pi[k]; + if(A[k + 1] == B[i]) + ++k; + if(k == a){ + ++nr; + if(nr <= 1000) + v[nr] = i; + } + } + +} + +void kmp(){ + int k = 0; + for(int i = 2; i <= a; ++i){ + while(k && A[k + 1] != A[i]) + k = pi[k]; + if( A[k + 1] == A[i]) + ++k; + pi[i] = k; + } +} + +int main(){ + in >> (A+1); + in >> (B+1); + a = strlen(A+1); + b = strlen(B+1); + + kmp(); + kmp1(); + + out << nr << '\n'; + if(nr>1000) nr=1000; + for(int i = 1; i <= nr; ++i) + out << v[i] - a << ' '; + return 0; + + +} diff --git a/strmatch/kmp/a9d7d4a8-28eb-4aeb-a6b9-168984018d96.cpp b/strmatch/kmp/a9d7d4a8-28eb-4aeb-a6b9-168984018d96.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0fcf38810ef2b43685eedca084848dd478cd84 --- /dev/null +++ b/strmatch/kmp/a9d7d4a8-28eb-4aeb-a6b9-168984018d96.cpp @@ -0,0 +1,88 @@ +#include +using namespace std; + + + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + + + +int main() + +{ + + string pattern; + + string text; + + + + fin >> pattern >> text; + + + + vector PI(pattern.size() + 1); + + + + for(int i = 1, k = 0; i < (int)pattern.size(); ++i) + + { + + while(pattern[i] != pattern[k] && k > 0) k = PI[k - 1]; + + + + if(pattern[i] == pattern[k]) ++k; + + + + PI[i] = k; + + } + + + + vector answer; + + + + for(int i = 0, k = 0; i < (int)text.size(); ++i) + + { + + while(text[i] != pattern[k] && k > 0) k = PI[k - 1]; + + + + if(text[i] == pattern[k]) ++k; + + + + if(k == (int)pattern.size()) + + { + + answer.push_back(i - pattern.size() + 1); + + } + + } + + + + fout << answer.size() << '\n'; + + + + for(int i = 0; i < min(1000, (int)answer.size()); ++i) + + fout << answer[i] << ' '; + +} diff --git a/strmatch/kmp/aa07f052-719c-48fd-8bf7-9de99bebd137.cpp b/strmatch/kmp/aa07f052-719c-48fd-8bf7-9de99bebd137.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4750762ea2f6b020fc28698e3ec2a260dbd601fa --- /dev/null +++ b/strmatch/kmp/aa07f052-719c-48fd-8bf7-9de99bebd137.cpp @@ -0,0 +1,93 @@ +#include +#include +using namespace std; + +string zero=" "; + +int pi[2000005],s[1005]; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int k,la,lb,l=0,cnt=0,res=0; + + string a,b; + + cin >> a; + + cin >> b; + + la=a.size(); + + lb=b.size(); + + a=zero+a; + + a+=zero; + + b=zero+b; + + for(int i=2;i<=la;i++) + + { + + while(l>0 && a[l+1]!=a[i]) + + l=pi[l]; + + if(a[l+1]==a[i]) + + l++; + + pi[i]=l; + + } + + l=0; + + for(int i=1;i<=lb;i++) + + { + + while(l>0 && a[l+1]!=b[i]) + + l=pi[l]; + + if(a[l+1]==b[i]) + + l++; + + if(l==la) + + { + + if(cnt<1000) + + { + + s[cnt]=i-la; + + cnt++; + + } + + res++; + + } + + } + + cout << res << '\n'; + + for(int i=0;i +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + + + +const int Base_1 = 131, Mod_1 = 1e9 + 7; + +const int Base_2 = 137, Mod_2 = 1e9 + 9; + + + +int N, M; + +char A[NMAX], B[NMAX]; + + + +int p_1[NMAX], p_2[NMAX]; + +int H_1, H_2; + + + +int Sol[NMAX]; + + + +char Const; + + + +static inline void Read () + +{ + + f.tie(nullptr); + + + + f >> (A + 1) >> (B + 1); + + + + N = (int)strlen(A + 1), M = (int)strlen(B + 1); + + + + for(int i = 1; i <= N; ++i) + + Const = min(Const, A[i]); + + for(int i = 1; i <= M; ++i) + + Const = min(Const, B[i]); + + + + return; + +} + + + +static inline void Precalculation () + +{ + + p_1[0] = p_2[0] = 1; + + + + for(int i = 1; i < N; ++i) + + { + + p_1[i] = (1LL * p_1[i - 1] * Base_1) % (1LL * Mod_1); + + + + p_2[i] = (1LL * p_2[i - 1] * Base_2) % (1LL * Mod_2); + + } + + + + return; + +} + + + +static inline void Find_Key () + +{ + + for(int i = 1; i <= N; ++i) + + { + + int Now = (int)(A[i] - Const); + + + + H_1 = ((1LL * H_1 * Base_1) % (1LL * Mod_1) + Now) % Mod_1; + + H_2 = ((1LL * H_2 * Base_2) % (1LL * Mod_2) + Now) % Mod_2; + + } + + + + return; + +} + + + +auto Ok = [] (int h_1, int h_2) + +{ + + if(h_1 == H_1 && h_2 == H_2) + + return 1; + + + + return 0; + +}; + + + +static inline void Solve () + +{ + + int h_1 = 0, h_2 = 0; + + + + for(int i = 1; i <= N; ++i) + + { + + int Now = (int)(B[i] - Const); + + + + h_1 = ((1LL * h_1 * Base_1) % (1LL * Mod_1) + Now) % Mod_1; + + h_2 = ((1LL * h_2 * Base_2) % (1LL * Mod_2) + Now) % Mod_2; + + } + + + + if(Ok(h_1, h_2)) + + Sol[++Sol[0]] = 0; + + + + for(int i = (N + 1); i <= M; ++i) + + { + + int Last = (int)(B[i - N] - Const), Now = (int)(B[i] - Const); + + + + h_1 = (h_1 - (1LL * Last * p_1[(N - 1)]) % (1LL * Mod_1)); + + if(h_1 < 0) + + h_1 += Mod_1; + + h_1 = ((1LL * h_1 * Base_1) % (1LL * Mod_1) + Now) % Mod_1; + + + + h_2 = (h_2 - (1LL * Last * p_2[(N - 1)]) % (1LL * Mod_2)); + + if(h_2 < 0) + + h_2 += Mod_2; + + h_2 = ((1LL * h_2 * Base_2) % (1LL * Mod_2) + Now) % Mod_2; + + + + if(Ok(h_1, h_2)) + + Sol[++Sol[0]] = i - N; + + } + + + + return; + +} + + + +static inline void Write () + +{ + + g << Sol[0] << '\n'; + + + + int n = min(Sol[0], 1000); + + + + for(int i = 1; i <= n; ++i) + + { + + g << Sol[i]; + + + + if(i != n) + + g << ' '; + + } + + + + g << '\n'; + + + + return; + +} + + + +int main() + +{ + + Read(); + + + + Precalculation(); + + + + Find_Key(); + + + + Solve(); + + + + Write(); + + + + return 0; + +} diff --git a/strmatch/kmp/ac575468-5e6b-4d9c-bde4-18a5418fd7ea.cpp b/strmatch/kmp/ac575468-5e6b-4d9c-bde4-18a5418fd7ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e46ebc5b5bed39f5da578af5df7c288c5c8c31c --- /dev/null +++ b/strmatch/kmp/ac575468-5e6b-4d9c-bde4-18a5418fd7ea.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + + + +using namespace std; + + + +int la,lb,i,j,rez; + +char a[2000005],b[2000005]; + +vector urm; + +vector poz; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void citire(){ + + fin>>a>>b; + + la=strlen(a); + + lb=strlen(b); + + urm.assign (la+5,0); + + poz.assign (1005,0); + +} + + + +void prefix(){ + + int k=0; + + for (i=1; i0 && a[k]!=a[i]) k=urm[k-1]; + + if (a[k]==a[i]) k++; + + urm[i]=k; + + } + +}; + + + +void rezolvare(){ + + prefix(); + + j=0; + + for (i=0;i0 && a[j]!=b[i]) j=urm[j-1]; + + if (a[j]==b[i]) j++; + + if (j==la){ + + if (rez<1000) poz[++rez]=i-la+1; + + else rez++; + + j=urm[j-1]; + + } + + } + +} + + + +void scriere(){ + + fout<1000) rez=1000; + + for (i=1;i<=rez;i++) + + fout< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char p[2000001], t[2000001]; + +int f[2000001]; + +vector < int > poz; + + + +int kmp(char p[], char t[], vector < int > &poz) + +{ + + int n, m, i, j, nr; + + n = strlen(t); + + m = strlen(p); + + + + f[0] = 0, j = 0; + + for (i=1; i 0) + + j = f[j-1]; + + else + + i++; + + } + + + + return nr; + +} + + + +int main() + +{ + + fin >> p >> t; + + fout << kmp(p, t, poz) << "\n"; + + for (int i=0; i +#include +std::fstream in ("strmatch.in", std::ios::in) ; +std::fstream out ("strmatch.out", std::ios::out) ; + +const int MV = 2e6 ; +int sol[MV + 1] ; +int pi[2 * MV + 1] ; + +int main(int argc, const char * argv[]) { + std::string A, B ; + in >> A >> B ; + std::string tot = '+' + A + '!' + B ; + int len(tot.size()) ; + int nr(0), i ; + for (i = 2 ; i < len ; ++ i) { + int prev = pi[i - 1] ; + while (prev && tot[prev + 1] != tot[i]) { + prev = pi[prev] ; + } + prev += tot[prev + 1] == tot[i] ; + pi[i] = prev ; + if (pi[i] == A.size()) { + sol[++ nr] = i - 2 * A.size() - 1 ; + } + } + out << nr << '\n' ; + for (i = 1 ; i <= nr && i <= 1000 ; ++ i) { + out << sol[i] << ' ' ; + } +} diff --git a/strmatch/kmp/adcc15ac-2f3d-4ca1-b38a-7e4b1781ddcb.cpp b/strmatch/kmp/adcc15ac-2f3d-4ca1-b38a-7e4b1781ddcb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f3518c5b27920f34910b7813865f2ae860c771ff --- /dev/null +++ b/strmatch/kmp/adcc15ac-2f3d-4ca1-b38a-7e4b1781ddcb.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +const int NMAX=2000005; + +int T[NMAX]; + + + +queue sol; + +string a,b; + + + +void preComp(){ + + int i=1,j=0; + + while(i<(int)b.size()){ + + if(b[i]==b[j]){ + + T[i]=j+1; + + i++,j++; + + } + + else if(j!=0){ + + while(j!=0 && b[i]!=b[j]) + + j=T[j-1]; + + } + + else i++; + + } + +} + + + +int main(){ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + int i=0,j=0,cnt=0; + + cin>>b>>a; + + a.push_back('.'); + + preComp(); + + while(i<=(int)a.size()){ + + if(j>=(int)b.size()){ + + if((int)sol.size()<1000) + + sol.push(i-(int)b.size()); + + cnt++; + + j=T[j-1]; + + } + + if(a[i]==b[j]) + + i++,j++; + + else if(j!=0) + + j=T[j-1]; + + else i++; + + } + + printf("%d\n", cnt); + + while(!sol.empty()){ + + printf("%d ", sol.front()); + + sol.pop(); + + } + + return 0; + +} diff --git a/strmatch/kmp/aebadfae-6fe9-4919-8229-eeda00178036.cpp b/strmatch/kmp/aebadfae-6fe9-4919-8229-eeda00178036.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6a0640b55a9a30b7395fd7f823bb187696f6df39 --- /dev/null +++ b/strmatch/kmp/aebadfae-6fe9-4919-8229-eeda00178036.cpp @@ -0,0 +1,104 @@ +#include +#include +#include + + +const int L_MAX = 2000000; + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char P[L_MAX + 5], T[L_MAX + 5]; + +int lps[L_MAX + 5]; + +int cnt; + + + +vector pos; + + + +int main() { + + fin >> (P + 1); + + P[0] = '\0'; + + int sizeP = strlen(P + 1); + + + + lps[0] = lps[1] = 0; + + for (int i = 2; i <= sizeP; i++) { + + int x = lps[i - 1]; + + while (x > 0 && P[x + 1] != P[i]) + + x = lps[x]; + + if(P[x + 1] == P[i]) + + lps[i] = x + 1; + + else lps[i] = 0; + + + + } + + + + fin >> T; + + int sizeT = strlen(T); + + int x = 0; + + for (int i = 0; i < sizeT; i++) { + + while(x > 0 && P[x + 1] != T[i]) + + x = lps[x]; + + if (P[x + 1] == T[i]) + + x++; + + else x = 0; + + + + if(x == sizeP) { + + cnt++; + + pos.push_back(i - x + 1); + + } + + } + + + + fout << cnt << '\n'; + + for (int i = 0; i < min(cnt, 1000); i++) + + fout << pos[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/af1da64f-662d-4e9b-94f2-643f1f54fd1c.cpp b/strmatch/kmp/af1da64f-662d-4e9b-94f2-643f1f54fd1c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a00684d6b929f92f2e3a94bf63ead3cd413dfa5a --- /dev/null +++ b/strmatch/kmp/af1da64f-662d-4e9b-94f2-643f1f54fd1c.cpp @@ -0,0 +1,185 @@ +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string txt, pat; + + + +int v[1005]; + +int lps[2000005]; + + + +void computeLPS() + +{ + + int len = 0, i = 1; + + + + while(i < pat.size()) + + { + + if(pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if(len) + + { + + len = lps[len-1]; + + } + + else + + { + + i++; + + } + + } + + } + +} + + + +int nr; + + + +void KMP() + +{ + + computeLPS(); + + + + int i, j; + + i = j = 0; + + while(i < txt.size()) + + { + + if(txt[i] == pat[j]) + + { + + i++; + + j++; + + } + + else + + { + + if(j) + + { + + j = lps[j-1]; + + } + + else + + { + + i++; + + } + + } + + if(j == pat.size()) + + { + + if(nr < 1000) + + { + + v[nr] = i-j; + + } + + nr++; + + j = lps[j-1]; + + } + + } + + + + + +} + + + +int main() + +{ + + fin>>pat>>txt; + + + + KMP(); + + + + fout< +#include +#include + + +using namespace std; + + + +const int N = 2000000 + 7; + +string pat, s; + +int m, n, l[N]; + + + +int main() { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + + + cin >> pat >> s; + + m = (int) pat.size(); + + n = (int) s.size(); + + l[0] = 0; + + int p = 0; + + for (int i = 1; i < m; i++) { + + while (p && pat[i] != pat[p]) { + + p = l[p - 1]; + + } + + if (pat[i] == pat[p]) { + + p++; + + } + + l[i] = p; + + } + + p = 0; + + vector sol; + + for (int i = 0; i < n; i++) { + + while (p && s[i] != pat[p]) { + + p = l[p - 1]; + + } + + if (s[i] == pat[p]) { + + p++; + + } + + if (p == m) { + + sol.push_back(i - m + 1); + + p = l[p - 1]; + + } + + } + + cout << (int) sol.size() << "\n"; + + if ((int) sol.size() > 1000) { + + sol.resize(1000); + + } + + for (auto &i : sol) { + + cout << i << " "; + + } + + cout << "\n"; + +} diff --git a/strmatch/kmp/afa525a8-f218-4e85-a449-c012be27b929.cpp b/strmatch/kmp/afa525a8-f218-4e85-a449-c012be27b929.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f894a0f9e940799dfc37fcba07f15a66a0309d9e --- /dev/null +++ b/strmatch/kmp/afa525a8-f218-4e85-a449-c012be27b929.cpp @@ -0,0 +1,152 @@ +#include +#include +#include +using namespace std; + + + +int prefix[2000010]; + +int nr = 0; + +vector v; + + + +void make_prefix(string match) + +{ + + prefix[0] = 0; + + + + for (int i = 1; i < match.size(); ++i) + + { + + int s = prefix[i - 1]; + + + + while (s != 0 && match[s] != match[i]) + + { + + s = prefix[s - 1]; + + } + + + + if (match[s] == match[i]) + + { + + prefix[i] = s + 1; + + } + + } + +} + + + +void print_matches(string search, string match) + +{ + + int last_state = 0; + + for (int i = 0; i < search.size(); ++i) + + { + + while (last_state != 0 && search[i] != match[last_state]) + + { + + last_state = prefix[last_state - 1]; + + } + + + + if (search[i] == match[last_state]) + + { + + last_state++; + + + + if (last_state == match.size()) + + { + + last_state = prefix[last_state - 1]; + + nr++; + + if (v.size() < 1000) + + { + + + + v.push_back(i - match.size() + 1); + + } + + } + + } + + } + +} + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +int main() + +{ + + string match; + + string search; + + + + in >> match >> search; + + + + make_prefix(match); + + + + print_matches(search, match); + + + + out << nr<< "\n"; + + + + for (int i = 0; i < v.size(); ++i) + + { + + out << v[i] << " "; + + } + +} diff --git a/strmatch/kmp/b0947952-e9a6-4d6d-9dea-bb457862302b.cpp b/strmatch/kmp/b0947952-e9a6-4d6d-9dea-bb457862302b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..893559cda25989e334de465a6a7a46e62692c5a3 --- /dev/null +++ b/strmatch/kmp/b0947952-e9a6-4d6d-9dea-bb457862302b.cpp @@ -0,0 +1,95 @@ +#include +#include +using namespace std; + + + +char sir[2000005], pattern[2000005]; + +int prefix[2000005] {}, rez[1005]; + + + +int main() { + + int lsir = 0, lpattern = 0, i = 0, j = 0, n = 0; + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + + + fin >> pattern >> sir; + + lsir = strlen(sir); + + lpattern = strlen(pattern); + + + + for(i = lsir; i >= 1; --i) + + sir[i] = sir[i-1]; + + for(i = lpattern; i >= 1; --i) + + pattern[i] = pattern[i-1]; + + + + for(i = 2; i <= lpattern; ++i) { + + while(j && pattern[j + 1] != pattern[i]) + + j = prefix[j]; + + if(pattern[i] == pattern[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + j = 0; + + for(i = 1; i <= lsir; ++i) { + + while(j && sir[i] != pattern[j+1]) + + j = prefix[j]; + + if(sir[i] == pattern[j+1]) + + ++j; + + if(j == lpattern) { + + j = prefix[j]; + + ++n; + + if(n <= 1000) + + rez[n] = i - lpattern; + + } + + } + + + + fout << n << '\n'; + + n = (n < 1000 ? n : 1000); + + for(i = 1; i <= n; ++i) + + fout << rez[i] << ' '; + + fout << '\n'; + +} diff --git a/strmatch/kmp/b0c3b944-f00d-4b60-aa20-b2bb332751c6.cpp b/strmatch/kmp/b0c3b944-f00d-4b60-aa20-b2bb332751c6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..076cd73ab64564096f312e7d8ff45cdefdb8d19e --- /dev/null +++ b/strmatch/kmp/b0c3b944-f00d-4b60-aa20-b2bb332751c6.cpp @@ -0,0 +1,152 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n, m; + +char pattern[2000005], s[2000005]; + +int lps[2000005]; + +vector sol; + + + +void process(); + +void kmp(); + +int main() + +{ + + fin>>pattern>>s; + + n=strlen(s); + + m=strlen(pattern); + + + + process(); + + kmp(); + + + + int temp=sol.size(); + + fout<0) + + j=lps[j-1]; + + else ++i; + + } + + } + +} + +void process() + +{ + + int len=0; + + lps[0]=0; + + int i=1; + + + + while(i +#include + +using namespace std; + +ifstream cin("strmatch.in"); +ofstream cout("strmatch.out"); + +char a[2000001], b[2000001]; +int prefix[2000001], n, m; +int poz[1001], k; + +void crp(){ + int i, j=0; + prefix[1]=0; + for (i=2;i<=n;i++){ + while (j>0&&a[j+1]!=a[i]) + j=prefix[j]; + if (a[j+1]==a[i]) + ++j; + prefix[i]=j; + } +} + +void kmp(){ + int i, j=0; + for (i=1;i<=m;i++){ + while (j>0&&a[j+1]!=b[i]) + j=prefix[j]; + if (a[j+1]==b[i]) + ++j; + if (j==n){ + ++k; + if (k<=1000) poz[k]=i-j; + j=prefix[j]; + } + } + cout<>(a+1); + cin.get(); + cin>>(b+1); + a[0]=b[0]=' '; + int i; + n=strlen(a)-1, m=strlen(b)-1; + crp(); + kmp(); + cin.close(); + cout.close(); + return 0; +} diff --git a/strmatch/kmp/b290f36e-702d-4d42-8c6b-00df48265cf3.cpp b/strmatch/kmp/b290f36e-702d-4d42-8c6b-00df48265cf3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8ae0688e63c6b99a63edbed66a5eac0f8b0c77ff --- /dev/null +++ b/strmatch/kmp/b290f36e-702d-4d42-8c6b-00df48265cf3.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +using namespace std; + +ifstream cin("strmatch.in"); +ofstream cout("strmatch.out"); + +const int NMAX = 2e6; + +int M, N; +char a[NMAX + 2], b[NMAX + 2]; +int pi[NMAX + 2]; + +void ComputePi() +{ + int j = 0; + + for(int i = 2; i <= M; i++) + { + while(j && a[i] != a[j + 1]) + j = pi[j]; + + if(a[i] == a[j + 1]) + j++; + + pi[i] = j; + } +} + +int nrm; +vector m; + +void Kmp() +{ + int j = 0; + + for(int i = 1; i <= N; i++) + { + while(j && b[i] != a[j + 1]) + j = pi[j]; + + if(b[i] == a[j + 1]) + j++; + + if(j == M) + { + nrm++; + if(m.size() < 1000) + m.push_back(i - M); + j = pi[j]; + } + } +} + +int main() +{ + cin >> (a + 1) >> (b + 1); + M = strlen(a + 1); + N = strlen(b + 1); + + ComputePi(); + + Kmp(); + + cout << nrm << '\n'; + for(auto it : m) + cout << it << ' '; + + return 0; +} diff --git a/strmatch/kmp/b2ceabf9-ebcf-4aa6-af5e-0f0db9a5a26f.cpp b/strmatch/kmp/b2ceabf9-ebcf-4aa6-af5e-0f0db9a5a26f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58f401f279a52dd823187b53f06453e31291e6b6 --- /dev/null +++ b/strmatch/kmp/b2ceabf9-ebcf-4aa6-af5e-0f0db9a5a26f.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +string pat, txt; + +int n, m; + +int lps[2000000 +5]; + +vector ans; + + + +int main () { + + fin >> pat >> txt; + + m = (int)(pat).size(); n = (int)(txt).size(); + + int i, j, z, len = 0; + + lps[0] = 0; i = 1; + + while (i < m) { + + if (pat[i] == pat[len]) lps[i++] = ++len; + + else if (len > 0) len = lps[len-1]; + + else lps[i++] = 0; + + } + + i = j = 0; + + while (i < n) { + + if (txt[i] == pat[j]) i++, j++; + + if (j == m) { + + ans.push_back(i-m); + + j = lps[m-1]; + + } else if (i < n && pat[j] != txt[i]) { + + if (j > 0) j = lps[j-1]; + + else i++; + + } + + } + + fout << (int)(ans).size() << '\n'; + + for (i = 0; i < min(1000, (int)(ans).size()); i++) fout << ans[i] << ' '; + + fin.close(); fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/b325ec0f-67b6-4d40-9cf4-62235949a8bb.cpp b/strmatch/kmp/b325ec0f-67b6-4d40-9cf4-62235949a8bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea24b0e8af3e69b6881d9eef86fa7fa72b378800 --- /dev/null +++ b/strmatch/kmp/b325ec0f-67b6-4d40-9cf4-62235949a8bb.cpp @@ -0,0 +1,198 @@ +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Dim=2e6+5; + +const int Mod1=1e6+7; + +const int Mod2=1e6+21; + +const int d=91; + + + +void P1() + +{ + + int ans=0; + + string A,B; + + f>>A>>B; + + int N=A.size(),M=B.size(); + + for(int i=N;i>=1;i--) A[i]=A[i-1]; + + for(int i=M;i>=1;i--) B[i]=B[i-1]; + + + + vector < int > dp1(N+1),dp2(M+1),V; + + dp1[1]=0; + + for(int i=2;i<=N;i++) + + { + + int K=dp1[i-1]; + + while( K>0 && A[K+1]!=A[i] ) K=dp1[K]; + + + + dp1[i]=K + (A[K+1]==A[i]); + + } + + dp2[0]=0; + + for(int i=1;i<=M;i++) + + { + + int K=dp2[i-1]; + + while( K>0 && A[K+1]!=B[i] ) K=dp1[K]; + + + + dp2[i]=K+(A[K+1]==B[i]); + + + + if(dp2[i]==N) + + { + + ans++; + + if(ans<=1000) V.push_back(i-N); + + } + + } + + cout<>A>>B; + + int ans=0; + + int hashP1=0,hashP2=0,hashS1=0,hashS2=0,cos1=1,cos2=1; + + int N=A.size(),M=B.size(); + + + + vector < int > V; + + + + for(int i=0;i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a, b; + + + +unsigned int p[4000000 + 7]; + +unsigned int sol[4000000 + 7]; + + + +int main() + +{ + + in >> a >> b; + + s.resize(2 + a.size() + b.size()); + + + + s = '*' + a; + + + + unsigned int k = 0; + + for(unsigned int i = 2; i < s.size(); i++) + + { + + while(k > 0 && s[k+1] != s[i]) + + k = p[k]; + + if(s[k+1] == s[i]) + + k++; + + p[i] = k; + + } + + + + k = 0; + + int nr_ap = 0; + + for(unsigned int i = 0; i < b.size(); i++) + + { + + while(k > 0 && s[k+1] != b[i]) + + k = p[k]; + + if(s[k+1] == b[i]) + + k++; + + + + sol[i] = k; + + + + if(k == a.size()) + + nr_ap++; + + } + + + + out << nr_ap << endl; + + if(nr_ap > 1000) nr_ap = 1000; + + for(unsigned int i = 0; i < b.size(); i++) + + { + + if(sol[i] == a.size() && nr_ap) + + { + + out << i - a.size() +1 << " "; + + nr_ap--; + + } + + } + +} diff --git a/strmatch/kmp/b4c9baff-9350-4455-abd4-564300a820aa.cpp b/strmatch/kmp/b4c9baff-9350-4455-abd4-564300a820aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43b3c86518155bbff67b0a38acbed8620c435e7b --- /dev/null +++ b/strmatch/kmp/b4c9baff-9350-4455-abd4-564300a820aa.cpp @@ -0,0 +1,65 @@ +#include + +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +const int DIM = 2e6 + 1; + +int phi[DIM]; + +main() +{ + string v, t; + fin >> v >> t; + + int n = v.size(); + int m = t.size(); + + v = ' ' + v; + t = ' ' + t; + + int k = 0; + + for(int i = 2; i <= n; ++i) + { + while(k && v[k + 1] != v[i]) + k = phi[k]; + + if(v[k + 1] == v[i]) + ++k; + + phi[i] = k; + } + + k = 0; + int cnt = 0; + + vector ans; + + for(int i = 1; i <= m; ++i) + { + while(k && v[k + 1] != t[i]) + k = phi[k]; + + if(v[k + 1] == t[i]) + ++k; + + if(k == n) + { + k = phi[k]; + ++cnt; + + if(ans.size() < 1000) + { + ans.emplace_back(i - n); + } + } + } + + fout << cnt << '\n'; + + for(auto i : ans) + fout << i << ' '; +} diff --git a/strmatch/kmp/b4ead521-3988-4081-ac2c-0e233418b553.cpp b/strmatch/kmp/b4ead521-3988-4081-ac2c-0e233418b553.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64bc82741b650171a20ef759edaa0dbfd71eccdb --- /dev/null +++ b/strmatch/kmp/b4ead521-3988-4081-ac2c-0e233418b553.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A,B; + +int n, m; + +int pi[20000005]; + +int nr; + +vector res; + + + +void Read() + +{ + + f>>A; + + f>>B; + + + + for(;(A[m] >= 'A' && A[m] <= 'Z') || (A[m] >= 'a' && A[m] <= 'z') || (A[m] >= '0' && A[m] <= '9');++m); + + for(;(B[n] >= 'A' && B[n] <= 'Z') || (B[n] >= 'a' && B[n] <= 'z') || (B[n] >= '0' && B[n] <= '9');++n); + + + + A.insert(0, " "); + + B.insert(0, " "); + + + + f.close(); + +} + + + + + +void compute() + +{ + + int q = 0; + + + + for(int i = 2;i <= m;++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +void KMP() + +{ + + int q = 0; + + for(int i = 1;i <= n;++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == m) + + { + + ++nr; + + if(res.size() < 1000) + + res.push_back(i - m); + + q = pi[q]; + + } + + } + +} + + + +void Print() + +{ + + g<::iterator it = res.begin();it != res.end();++it) + + g<<*it<<" "; + + g.close(); + +} + + + +int main() + +{ + + Read(); + + compute(); + + KMP(); + + Print(); + + return 0; + +} diff --git a/strmatch/kmp/b5f54b01-2b72-45e2-91b0-91e331e97b9b.cpp b/strmatch/kmp/b5f54b01-2b72-45e2-91b0-91e331e97b9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b5b98ce3f636e8a95ec4015c0b3cb2258c8d81e --- /dev/null +++ b/strmatch/kmp/b5f54b01-2b72-45e2-91b0-91e331e97b9b.cpp @@ -0,0 +1,83 @@ +#include +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +string s,s1; + +int stare[2000005],nr; + +vector v; + +int main() + +{ + + int i; + + fin>>s>>s1; + + int st=0; + + int x=s.size(); + + for (i=2;i<=x;i++) + + { + + while (st>0 && s[i-1]!=s[st]) + + st=stare[st]; + + if (s[i-1]==s[st]) + + st++; + + stare[i]=st; + + } + + int x1=s1.size(); + + st=0; + + for (i=1;i<=x1;i++) + + { + + while (st>0 && s1[i-1]!=s[st]) + + st=stare[st]; + + if (s1[i-1]==s[st]) + + st++; + + if (st>=x) + + { + + nr++; + + if (nr<=1000) + + v.push_back(i-st); + + } + + } + + fout< +using namespace std; +ifstream fin ("strmatch.in"); + +ofstream fout("strmatch.out"); +int n, m; + +string a, b; + +int dp[2000005]; + +vector < int > sol; + + + +void init() { + + int j = 0; + + dp[1] = 0; + + for(int i = 2; i <= n; i++) { + + while(j && a[j + 1] != a[i]) { + + j = dp[j]; + + } + + if(a[j + 1] == a[i]) { + + j++; + + } + + dp[i] = j; + + } + +} + + + +void fao() { + + int j = 0; + + for(int i = 1; i <= m; i++) { + + while(j && a[j + 1] != b[i]) { + + j = dp[j]; + + } + + if(a[j + 1] == b[i]) { + + j++; + + } + + if(j == n) { + + j = dp[n]; + + sol.push_back(i - n); + + } + + } + +} + + + +void solve() { + + fin >> a >> b; + + n = a.size(); + + m = b.size(); + + + + a = '.' + a; + + b = '.' + b; + + + + init(); + + fao(); + + + + fout << sol.size() << "\n"; + + if(sol.size() <= 1000) { + + for(auto it : sol) { + + fout << it << " "; + + } + + } else { + + for(int i = 0; i < 1000; i++) { + + fout << sol[i] << " "; + + } + + } + + + +} + + + +int main() { + + ios_base::sync_with_stdio(0); + + fin .tie(0); + + fout.tie(0); + + + + int testCases = 1; + + + + while(testCases--) { + + solve(); + + } + +} diff --git a/strmatch/kmp/b6d593bc-3e64-43c8-81b6-23878abcba9a.cpp b/strmatch/kmp/b6d593bc-3e64-43c8-81b6-23878abcba9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..172ff183668b91ca74886b29865d0d3e24b9ff16 --- /dev/null +++ b/strmatch/kmp/b6d593bc-3e64-43c8-81b6-23878abcba9a.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMAX = 1e6 + 1; + + + +char txt[2 * NMAX + 1], pat[2 * NMAX + 1]; + +int lps[2 * NMAX + 1]; + + + +void buildlps( int m ) { + + lps[0] = 0; + + int i, len; + + len = 0; + + i = 1; + + while( i < m ) { + + if( pat[len] == pat[i] ) { + + len ++; + + lps[i] = len; + + i ++; + + } + + else { + + if( len == 0 ) { + + lps[i] = 0; + + i ++; + + } + + else + + len = lps[len - 1]; + + } + + } + +} + + + +int main() { + + int n, m; + + fin>>pat; + + fin>>txt; + + m = strlen(pat); + + n = strlen(txt); + + buildlps(m); + + int i = 0, j = 0; + + vector ans; + + while( i < n ) { + + if( txt[i] == pat[j] ) { + + i ++; + + j ++; + + } + + if( j == m ) { + + ans.push_back(i); + + j = lps[j - 1]; + + } + + else if( i < n && txt[i] != pat[j] ) { + + if( j == 0 ) + + i ++; + + else + + j = lps[j - 1]; + + } + + } + + fout< +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000001], p[2000001]; + + + +int sol[1001], lps[2000001], cnt, lg_p, lg_s; + + + +void precalculare() + +{ + + lps[0] = 0; + + int i = 1, len = 0; + + while (i < lg_p) { + + if (p[i] == p[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if (len != 0) { + + len = lps[len - 1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +void KMP() + +{ + + precalculare(); + + int i = 0, j = 0; + + while (i < lg_s) { + + if (s[i] == p[j]) + + { + + i++; + + j++; + + } + + if (j == lg_p) + + { + + cnt++; + + if (cnt <= 1000) + + sol[cnt] = i - j; + + j = lps[j - 1]; + + } + + else if (i < lg_s && s[i] != p[j]) + + { + + if (j != 0) + + j = lps[j - 1]; + + else + + i++; + + } + + } + +} + + + +int main() + +{ + + fin >> p; + + fin >> s; + + lg_p = strlen(p); + + lg_s = strlen(s); + + KMP(); + + fout << cnt << '\n'; + + for (int i = 1; i <= min(cnt,1000); i++) { + + fout << sol[i] << ' '; + + } + +} diff --git a/strmatch/kmp/b7e3a447-3624-4804-8d76-d2df3492c095.cpp b/strmatch/kmp/b7e3a447-3624-4804-8d76-d2df3492c095.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8f1ab57d8ea2654c92194626e067216a58703c8 --- /dev/null +++ b/strmatch/kmp/b7e3a447-3624-4804-8d76-d2df3492c095.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +char A[2000001 +10], B[2000001 +10]; + +int nA,nB,P1,P2,hashA1,hashA2,hashB1,hashB2, pM1,pM2; + +vector match; + +int main() + +{ + + cin.getline(A,2000001); + + cin.getline(B,2000001); + + nA=strlen(A); + + nB=strlen(B); + + if(nA>nB){ + + cout<<0<<'\n'; + + return 0; + + } + + + + P1=P2=1; + + for(int i=0; i0) + + { + + P1=(P1*73)%100007; + + P2=(P2*73)%100021; + + } + + } + + if(hashA1==hashB1 && hashA2==hashB2) + + match.push_back(0); + + for(int i=nA; i +int M, N; + +char A[2000005], B[2000005]; + +int pi[2000005], pos[1024]; + + + +void make_prefix(void) + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= M; ++i) + + { + + while (q && A[q + 1] != A[i]) + + q = pi[q]; + + if (A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main(void) + +{ + + int i, q = 0, n = 0; + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + fgets(A, sizeof(A), stdin); + + fgets(B, sizeof(B), stdin); + + for (; (A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') + + || (A[M] >= '0' && A[M] <= '9'); ++M); + + for (; (B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') + + || (B[N] >= '0' && B[N] <= '9'); ++N); + + for (i = M; i; --i) A[i] = A[i - 1]; A[0] = ' '; + + for (i = N; i; --i) B[i] = B[i - 1]; B[0] = ' '; + + + + make_prefix(); + + + + for (i = 1; i <= N; ++i) + + { + + while (q && A[q + 1] != B[i]) + + q = pi[q]; + + if (A[q + 1] == B[i]) + + ++q; + + if (q == M) + + { + + q = pi[M]; + + ++n; + + if (n <= 1000) + + pos[n] = i - M; + + } + + } + + + + printf("%d\n", n); + + for (i = 1; i <= ((n < 1000) ? n : 1000); ++i) + + printf("%d ", pos[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/b9e97cae-7a90-4a28-aeae-0709347b3a2c.cpp b/strmatch/kmp/b9e97cae-7a90-4a28-aeae-0709347b3a2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b39c2b298900324aa41fecb08ca92dc0a918b21 --- /dev/null +++ b/strmatch/kmp/b9e97cae-7a90-4a28-aeae-0709347b3a2c.cpp @@ -0,0 +1,75 @@ +#include + +using namespace std; + +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + + +void Citire(); +void Pref(); +void KMP(); + +char T[2000005],P[2000005]; +int n,m,k; +int a[2000005],urm[2000005]; + +int main() +{ + Citire(); + KMP(); + fout<1000) k=1000; + for(int i=1;i<=k;++i) + fout<0 && P[q]!=T[i]) q=urm[q-1]; + + if(P[q]==T[i]) ++q; + + if(q==n) a[++k]=i-n+1; + + } + +} diff --git a/strmatch/kmp/ba08fce9-56aa-4435-83fa-b336f6ca267b.cpp b/strmatch/kmp/ba08fce9-56aa-4435-83fa-b336f6ca267b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3497af4a036ae88bf782305e831c1a365bc6a6dd --- /dev/null +++ b/strmatch/kmp/ba08fce9-56aa-4435-83fa-b336f6ca267b.cpp @@ -0,0 +1,103 @@ +#include +#include +using namespace std; + + + +char sir[2000005], pattern[2000005]; + +int prefix[2000005] {}, rez[1005]; + + + +int main() { + + int lsir = 0, lpattern = 0, i = 0, j = 0, n = 0; + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + + + scanf("%s%s", pattern, sir); + + lsir = strlen(sir); + + lpattern = strlen(pattern); + + + + for(i = lsir; i >= 1; --i) + + sir[i] = sir[i-1]; + + for(i = lpattern; i >= 1; --i) + + pattern[i] = pattern[i-1]; + + + + for(i = 2; i <= lpattern; ++i) { + + while(j && pattern[j + 1] != pattern[i]) + + j = prefix[j]; + + if(pattern[i] == pattern[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + j = 0; + + for(i = 1; i <= lsir; ++i) { + + while(j && sir[i] != pattern[j+1]) + + j = prefix[j]; + + if(sir[i] == pattern[j+1]) + + ++j; + + if(j == lpattern) { + + j = prefix[j]; + + ++n; + + if(n <= 1000) + + rez[n] = i - lpattern; + + } + + } + + + + + + printf("%d\n", n); + + n = (n < 1000 ? n : 1000); + + for(i = 1; i <= n; ++i) + + + + printf("%d ", rez[i]); + + + + printf("\n"); + +} diff --git a/strmatch/kmp/bbac918f-46d3-4985-ba91-bb68c3ca25ee.cpp b/strmatch/kmp/bbac918f-46d3-4985-ba91-bb68c3ca25ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..722920af671059dc59b5d67e5aa4053614c0c0bb --- /dev/null +++ b/strmatch/kmp/bbac918f-46d3-4985-ba91-bb68c3ca25ee.cpp @@ -0,0 +1,58 @@ +#include + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +const int dim = (int) (1e6)* 2 + 1; + +char a[dim], b[dim]; +int pi[dim], sol[dim], nr; + +void precalc() +{ + int m = strlen(a + 1); + pi[1] = 0; + int k = 0; + for(int q = 2;q <= m;q++) + { + while(k > 0 && a[k + 1] != a[q]) + k = pi[k]; + if(a[k + 1] == a[q]) + k++; + pi[q] = k; + } +} + +void match() +{ + int n = strlen(b + 1); + int m = strlen(a + 1); + precalc(); + int q = 0; + for(int i = 1;i <= n;i++) + { + while(q > 0 && a[q + 1] != b[i]) + q = pi[q]; + if(a[q + 1] ==b[i]) + q++; + if(q == m) + { + ++nr; + if(nr <= 1000) + sol[nr] = i - m; + q = pi[q]; + } + } +} + +int main() +{ + in>>(a + 1)>>(b + 1); + match(); + out< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int n,m,l,i,j; + +int nr[2000002],urm[2000002]; + +string p,t; + +void prefix(int n,string p){ + + int i,j; + + urm[0]=0; j=0; + + for(i=1;i0&&p[i]!=p[j]) + + j=urm[j-1]; + + if(p[j]==p[i]) + + j++; + + urm[i]=j; + + } + +} + +int main() + +{ + + f>>p>>t; + + n=p.size(); m=t.size(); + + prefix(n,p); + + j=0; l=0; + + for(i=0;i0&&p[j]!=t[i]) + + j=urm[j-1]; + + if(p[j]==t[i]) + + j++; + + if(j==n){ + + l++; + + nr[l]=i-n+1; + + } + + } + + g< +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("strmatch.in"); +ofstream fout ("strmatch.out"); + +const int Nmax = 2000666; +string A, B; + + + + + + + +int pi[Nmax]; + + +void build_prefix() { + for(int i = 0; i < A.size(); i++) { + + + int k = pi[i]; + while(k > 0 && A[i] != A[k]) { + k = pi[k]; + } + if (i == 0 || A[i] != A[k]) { + pi[i+1] = 0; + } else { + pi[i+1] = k+1; + } + } +} + +int main(void) { + fin >> A >> B; + int N = A.size(), M = B.size(); + if (N > M) { + fout << "0\n"; + return 0; + } + + build_prefix(); + int count = 0, k = 0; + vi res; + for(int i = 0; i < M; i++) { + while(k > 0 && B[i] != A[k]) { + k = pi[k]; + } + if (B[i] == A[k]) { + k++; + } + if (k == N) { + count++; + if (count <= 1000) { res.push_back(i-(N-1));} + k = pi[k]; + } + } + + fout << count << '\n'; + for(auto pos: res) { + fout << pos << ' '; + } + fout << endl; + + return 0; +} diff --git a/strmatch/kmp/bd1394c1-6e2d-488e-9ba8-6586a5f6b6a0.cpp b/strmatch/kmp/bd1394c1-6e2d-488e-9ba8-6586a5f6b6a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f8f7ea2bf829a0ba9f0f7299cbb64538bd7507 --- /dev/null +++ b/strmatch/kmp/bd1394c1-6e2d-488e-9ba8-6586a5f6b6a0.cpp @@ -0,0 +1,144 @@ +#include + + + + +using namespace std; + + + +int m,n; + +char a[2000001], b[2000001]; + +int pi[2000001], pos[1024]; + +int ct; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void Peg(); + +void Read(); + +void Rez(); + +void Afis(); + + + +int main() + +{ + + Read(); + + Peg(); + + Rez(); + + Afis(); + + return 0; + +} + + + +void Read() + +{ + + fin.getline(a,2000001); + + fin.getline(b,2000001); + + m=strlen(a); + + n=strlen(b); + + for(int i=n;i;i--) b[i]=b[i-1]; + + for(int i=m;i;i--) a[i]=a[i-1]; + + b[0]=a[0]=' '; + +} + + + +void Peg() + +{ + + pi[1]=0; + + int q=0; + + for(int i=2;i<=m;i++) + + { + + while(q && a[q+1]!=a[i]) q=pi[q]; + + if(a[q+1]==a[i]) q++; + + pi[i]=q; + + } + +} + + + +void Rez() + +{ + + int q=0; + + for(int i=1;i<=n;i++) + + { + + while(q && a[q+1]!=b[i]) q=pi[q]; + + if(a[q+1]==b[i]) q++; + + if(q==m) + + { + + q=pi[m]; + + ct++; + + if(ct<=1000) + + pos[ct]=i-m; + + } + + } + +} + + + +void Afis() + +{ + + fout< + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +char a[2000005], b[2000005]; + +int pi[2000005], n, m, k, sol[2000005], nr; + + + +int main() + +{ + + fin.getline(a+1, 2000005); + + fin.getline(b+1, 2000005); + + a[0]='1'; + + b[0]='1'; + + n=strlen(a)-1; + + m=strlen(b)-1; + + for(int i=2;i<=n;i++) + + { + + while(k>0 && a[k+1]!=a[i]) + + k=pi[k]; + + if(a[k+1]==a[i]) + + k++; + + pi[i]=k; + + } + + k=0; + + for(int i=1;i<=m;i++) + + { + + while(k>0 && a[k+1]!=b[i]) + + k=pi[k]; + + if(a[k+1]==b[i]) + + k++; + + if(k==n) + + sol[++nr]=i-n; + + } + + fout << nr << '\n'; + + for(int i=1;i<=min(nr, 1000);i++) + + fout << sol[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/bdb156ad-d706-4483-b694-4e88f2e19e9d.cpp b/strmatch/kmp/bdb156ad-d706-4483-b694-4e88f2e19e9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cba3a64dc0f719fa8a6f84a8eb57bf4bd5c5ea17 --- /dev/null +++ b/strmatch/kmp/bdb156ad-d706-4483-b694-4e88f2e19e9d.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int MAX_N = 2e6 + 5; + + + +int n, m; + + + +char a[MAX_N], b[MAX_N]; + + + +int kmp[MAX_N]; + + + +vector ans; + + + +int main() { + + int k; + + fin >> (a + 1) >> (b + 1); + + n = strlen(a + 1); + + m = strlen(b + 1); + + a[0] = b[0] = '\0'; + + kmp[0] = kmp[1] = 0; + + for (int i = 2; i <= n; ++i) { + + k = kmp[i - 1]; + + while (k > 0 && a[k + 1] != a[i]) { + + k = kmp[k]; + + } + + if (a[i] == a[1 + k]) { + + kmp[i] = 1 + k; + + } else { + + kmp[i] = 0; + + } + + } + + k = 0; + + for (int i = 1; i <= m; ++i) { + + while (k > 0 && b[i] != a[1 + k]) { + + k = kmp[k]; + + } + + if (b[i] == a[1 + k]) { + + ++k; + + } else { + + k = 0; + + } + + if (k == n) { + + ans.push_back(i - n); + + } + + } + + fout << ans.size() << "\n"; + + for (int i = 0; i < ans.size() && i < 1000; ++i) { + + fout << ans[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/kmp/be25300a-f088-469a-87f8-b45d3826d819.cpp b/strmatch/kmp/be25300a-f088-469a-87f8-b45d3826d819.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4b5f1fb0040a3a6977546062db224fe89632b8a --- /dev/null +++ b/strmatch/kmp/be25300a-f088-469a-87f8-b45d3826d819.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int pi[4000005]; + +void kmp(string s) + +{ + + pi[0]=0; + + int j=0; + + for(int i=1;i rez; + + for(int i=a.size()+1;i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a, b; + +int pr[2000041]; + +void precalc(){ + + int q = 0; + + for(int i = 1; i < a.size(); ++i){ + + while(a[i] != a[q] && q>0){ + + q = pr[q-1]; + + } + + if(a[i] == a[q]){ + + q++; + + } + + pr[i] = q; + + } + +} + + + +int cer=0; + +vector sol; + +void postcalc(){ + + int i = 0; + + for(int j = 0; j < b.size(); ++j){ + + while(a[i] != b[j] && i>0){ + + i = pr[i-1]; + + } + + if(a[i] == b[j]){ + + i++; + + } + + if(i == a.size()){ + + cer++; + + if(sol.size() < 1000){ + + sol.push_back(j-a.size()+1); + + } + + i = pr[i-1]; + + } + + } + +} + + + +int main(){ + + fin >> a >> b; + + precalc(); + + postcalc(); + + fout << cer << "\n"; + + for(auto a : sol){ + + fout << a << " "; + + } + + return 0; + +} diff --git a/strmatch/kmp/c19577eb-d085-4445-9981-a347448d73c0.cpp b/strmatch/kmp/c19577eb-d085-4445-9981-a347448d73c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a38e1bb9ec5bceace41816bd62e253d64bdd270e --- /dev/null +++ b/strmatch/kmp/c19577eb-d085-4445-9981-a347448d73c0.cpp @@ -0,0 +1,171 @@ +# include +# include +# include + + +using namespace std; + + + + + + + +char A[2000001], B[2000001]; + + + +int lps[2000001]; + + + +void computeLPSArray(char* pat, int M, int lps[]) + +{ + + int len = 0; + + lps[0] = 0; + + + + int i = 1; + + + + while (i < M) + + { + + if (pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if (len != 0) + + { + + len = lps[len - 1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + int nr = 0; + + vector sir; + + int M = strlen(A); + + int N = strlen(B); + + computeLPSArray(A, M, lps); + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int j = 0; + + int i = 0; + + while (i < N) + + { + + if (A[j] == B[i]) + + { + + j++; + + i++; + + } + + if (j == M) + + { + + j = lps[j - 1]; + + sir.push_back(i - M); + + nr++; + + } + + else if (i < N && A[j] != B[i]) + + { + + if (j != 0) + + j = lps[j - 1]; + + else + + i++; + + } + + } + + printf("%d\n", nr); + + for (int i = 0; i < ((sir.size() < 1000) ? sir.size() : 1000); i++) + + { + + printf("%d ", sir[i]); + + } + + return 0; + +} diff --git a/strmatch/kmp/c1a534c3-d14e-442b-a688-d58420825a93.cpp b/strmatch/kmp/c1a534c3-d14e-442b-a688-d58420825a93.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a899416974be07b21fa34c5c4a7623eac361e491 --- /dev/null +++ b/strmatch/kmp/c1a534c3-d14e-442b-a688-d58420825a93.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string sir,cuv; + +int sol[1001],t; + +int pref[2000001]; + + + +void adauga(int poz) + +{ + + t++; + + if(t<=1000) + + sol[t]=poz-cuv.size(); + +} + + + +void buildPref(string cuv) + +{ + + int lung=0; + + + + for(int i=2;i>cuv>>sir; + + + + KMP(cuv,sir); + + + + out< +#include +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out("strmatch.out"); + +char a[2000003], b[2000003]; + +int lps[2000003], sizeb; + +vector found; + +void calclps() + +{ + + lps[0]=0; + + int poz1=0; + + for(int i=1; b[i]!='\0'; i++) + + { + + while(b[poz1]!=b[i]&&poz1!=0) poz1=lps[poz1-1]; + + if(b[poz1]==b[i]) poz1++; + + lps[i]=poz1; + + } + + + + + +} + +int main() + +{ + + in.getline(b, 2000000001); + + in.getline(a, 2000000001); + + sizeb=strlen(b); + + calclps(); + + int poz1=0; + + for(int i=0; a[i]!='\0';i++) + + { + + while(b[poz1]!=a[i]&&poz1!=0) poz1=lps[poz1-1]; + + if(b[poz1]==a[i]) poz1++; + + if(poz1==sizeb) found.push_back(i-sizeb+1) ; + + } + + out<<(int)found.size()<<"\n"; + + for(int i=0; i + + + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int nxt[2000005], ocr[2000005]; + +string s, c; + + + +int main() + +{ + + fin >> s >> c; + + int n=s.size(), m=c.size(); + + int j=0; + + nxt[0]=0; + + for(int i=1; i0 && s[i]!=s[j]) + + j=nxt[j-1]; + + if(s[i]==s[j]) + + j++; + + nxt[i]=j; + + } + + int nr=0; + + j=0; + + for(int i=0; i0 && c[i]!=s[j]) + + j=nxt[j-1]; + + if(c[i]==s[j]) + + j++; + + if(j==n) + + ocr[++nr]=i-n+1; + + } + + fout << nr << "\n"; + + for(int i=1; i<=min(nr, 1000); i++) + + fout << ocr[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/c4846943-5a50-46be-ad3a-4a6097edc6ac.cpp b/strmatch/kmp/c4846943-5a50-46be-ad3a-4a6097edc6ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7efe96baf3cad3036fa0ef86a5311dc640d71c6d --- /dev/null +++ b/strmatch/kmp/c4846943-5a50-46be-ad3a-4a6097edc6ac.cpp @@ -0,0 +1,89 @@ +#include +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int p[2*2000001]; + +char sir1[2000001*2], sir2[2000001]; + + + +void f_pref(char s[], int pref[], int n) { + + pref[0] = 0; + + for (int i = 1; i < n; i++) { + + int j = i; + + while (j > 0 && s[pref[j - 1]] != s[i]) { + + j = pref[j - 1]; + + } + + if (j == 0) + + pref[i] = 0; + + else + + pref[i] = pref[j - 1] + 1; + + } + +} + +int main() + +{ + + fin >> sir1 >> sir2; + + int lg1 = strlen(sir1); + + int lg2 = strlen(sir2); + + sir1[lg1] = '#'; + + for (int i = lg1 + 1; i <= lg1 + lg2; ++i) + + sir1[i] = sir2[i-lg1-1]; + + f_pref(sir1, p, lg1+lg2+1); + + int ct = 0; + + for (int i = 0; i <= lg1+lg2; ++i) + + if (p[i] == lg1) + + ct++; + + fout << ct << '\n'; + + ct = 0; + + for (int i = 0; i <= lg1 + lg2; ++i) + + if (p[i] == lg1 && ct < 1000) + + { + + ct++; + + fout << i - lg1*2 << ' '; + + } + + return 0; + +} diff --git a/strmatch/kmp/c4dea759-ce8b-4d78-9df9-5513b48b38c4.cpp b/strmatch/kmp/c4dea759-ce8b-4d78-9df9-5513b48b38c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b3c1ddbddd71a24096b31d1b8d4bb476e208ebc1 --- /dev/null +++ b/strmatch/kmp/c4dea759-ce8b-4d78-9df9-5513b48b38c4.cpp @@ -0,0 +1,205 @@ +#include +#include + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + + + +ofstream g("strmatch.out"); + + + + + + + +char a[2000002],b[2000002]; + + + +int n,m,p[2000002],sol[1003],nr; +void prefix(char s[],int n){ + + + + p[1]=0; + + + + int k=0; + + + + for(int i=2;i<=n;i++){ + + + + while(k && s[i]!=s[k+1]) + + + + k=p[k]; + + + + if(s[i]==s[k+1]) + + + + k++; + + + + p[i]=k; + + + + } + + + +} + + + + + + + +void ad(char a[],int n){ + + + + for(int i=n;i>0;i--)a[i]=a[i-1]; + + + + a[0]=' '; + + + +} + + + + + + + +void kmp(char a[],int n,char b[],int m){ + + + + if(n>m)return; + + + + if(n==m){if(strcmp(a,b)==0){g<<"1\n";return;} + + + + else return;} + + + + ad(a,n); + + + + ad(b,m); + + + + prefix(a,n); + + + + int k=0; + + + + for(int i=1;i<=m;i++){ + + + + while(k && a[k+1]!=b[i]) + + + + k=p[k]; + + + + if(a[k+1]==b[i])k++; + + + + if(k==n){ + + + + k=p[n]; + + + + ++nr; + + + + if(nr<=1000) + + + + sol[nr]=i-n; + + + + } + + + + } + + + +} +int main(){ + + + + f.getline(a,2000001); + + + + f.getline(b,2000001); + + + + kmp(a,strlen(a),b,strlen(b)); + + + + g<1000)nr=1000; + + + + for(int i=1;i<=nr;i++) + + + + g< +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +string a,b,t1,t2; + +int poz,i,pozitiea,pozitieb,pozitie,l1,l2,numar,ok,j; + +int v[1010],p[2000010]; + +void precalc() + +{ + + p[0]=-1; + + poz=-1; + + for(i=1;i>a>>b; + + l1=a.size(); + + l2=b.size(); + + if(l1>l2) + + { + + fout<<0; + + return 0; + + } + + poz=-1; + + precalc(); + + + + poz=-1; + + for(i=0; i + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +char s[4000005]; + +int kmp[4000005]; + + + +void KMP(int n, int l) + +{ + + int cont = 0; + + kmp[1] = 0; + + for(int i = 2 ;i <= n; ++ i) + + { + + int k = i - 1; + + while(k && s[i] != s[kmp[k] + 1]) + + k = kmp[k]; + + if(s[i] == s[kmp[k] + 1]) + + kmp[i] = kmp[k] + 1; + + else kmp[i] = 0; + + if(kmp[i] == l) + + cont++; + + } + + fout << cont << "\n"; + + cont = 0; + + for(int i = 2;i <= n; ++ i) + + if(kmp[i] == l) + + { + + ++cont; + + if(cont <= 1000) + + fout<> (s+1); + + int n, l = strlen(s)-1; + + s[l+1]= '&'; + + fin >> (s + l + 2); + + n = strlen(s)-1; + + KMP(n,l); + + return 0; + +} diff --git a/strmatch/kmp/c66956af-7415-4a7b-9187-88c36054531e.cpp b/strmatch/kmp/c66956af-7415-4a7b-9187-88c36054531e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28617195c47d4d7a4ddf1cd0ad9fe6e72ca74e48 --- /dev/null +++ b/strmatch/kmp/c66956af-7415-4a7b-9187-88c36054531e.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int n,m,l,i,cnt,sol[1010],p[2000010]; + +char a[2000010],b[2000010]; + +int main() { + + fin>>a+1>>b+1; + + n=strlen(a+1); + + m=strlen(b+1); + + + + for(i=2;i<=n;i++){ + + while(l && a[l+1]!=a[i]) + + l=p[l]; + + + + if(a[i]==a[l+1]) + + l++; + + + + p[i]=l; + + } + + l=0; + + + + for(i=1;i<=m;i++){ + + while(l && a[l+1]!=b[i]) + + l=p[l]; + + + + if(b[i]==a[l+1]) + + l++; + + + + if(l==n){ + + if(++cnt<=1000) + + sol[cnt]=i-l; + + l=p[l]; + + } + + } + + + + fout< +#include +#include + + +using namespace std; + + + +string s; + +string t; + +int n; + +int m; + +vector bk; + + + +void build_automat() { + + bk.resize(m, 0); + + int j = 0; + + for (int i = 1; i < m; i++) { + + while (j && t[i] != t[j]) { + + j = bk[j - 1]; + + } + + if (t[i] == t[j]) { + + j++; + + bk[i] = j; + + } + + } + +} + + + +vector find_matches() { + + vector sol; + + int j = 0; + + for (int i = 0; i < n; i++) { + + while (j && s[i] != t[j]) { + + j = bk[j - 1]; + + } + + if (s[i] == t[j]) { + + j++; + + } + + if (j == m) { + + sol.push_back(i - m + 1); + + j = bk[j - 1]; + + } + + } + + return sol; + +} + + + +int main() { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + cin >> t >> s; + + n = (int) s.size(); + + m = (int) t.size(); + + build_automat(); + + vector s = find_matches(); + + cout << (int) s.size() << "\n"; + + if ((int) s.size() > 1000) { + + s.resize(1000); + + } + + for (auto &i : s) { + + cout << i << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/kmp/c700da19-f69b-4169-a9b3-a5aee9edec1b.cpp b/strmatch/kmp/c700da19-f69b-4169-a9b3-a5aee9edec1b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..255ca76cd2d2d0f5bd1eb66a0d0493f0b2181519 --- /dev/null +++ b/strmatch/kmp/c700da19-f69b-4169-a9b3-a5aee9edec1b.cpp @@ -0,0 +1,104 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n,m,i,q,j,nr; + +string s,t; + +vector ans; + +int pi[2000005]; + +int main() + +{ + + fin>>t; + + n=t.size(); + + + + pi[0]=0; + + for(i=1;i0 && t[i]!=t[j]) + + j=pi[j-1]; + + + + if(t[j]==t[i]) + + j++; + + + + pi[i]=j; + + } + + + + fin>>s; + + m=s.size(); + + q=0; + + + + for(i=0;i0 && t[q]!=s[i]) + + q=pi[q-1]; + + + + if(t[q]==s[i]) + + q++; + + + + if(q==n) + + { + + nr++; + + if(ans.size()<1000) + + ans.push_back(i-n+1); + + } + + + + } + + + + fout< +using namespace std; +using ull = unsigned long long; + +using ll = long long; + +using pii = pair; + +using pll = pair; + + + +template + +ostream& operator<<(ostream &os, const vector &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } + +template + +ostream& operator<<(ostream &os, const pair &p) { return os << '(' << p.first << ", " << p.second << ')'; } + + + +const int INF = INT_MAX; + +const int MOD = 1e9 + 7; + +const double EPS = 1e-9; + +const double PI = acos(-1); + + + +ll n, ans, t; +namespace KMP { + + vector pattern_prefix(string pattern, int n) { + + vector res(n, 0); + + + + int i = 0, j = 1; + + while (j < n) { + + if (pattern[i] == pattern[j]) { + + res[j] = i + 1; + + i++, j++; + + } else { + + if (i != 0) { + + i = res[i - 1]; + + } else { + + res[j] = 0; + + j++; + + } + + } + + } + + + + return res; + + } + + + + + + bool check(string pattern, string text) { + + if (pattern.length() > text.length() or (pattern.length() == text.length() and pattern != text)) + + return false; + + return true; + + } + + + + + + vector match(string pattern, int n, string text, int m) { + + vector res; + + + + vector pref_suf = pattern_prefix(pattern, n); + + int i = 0, j = 0; + + while (j < m) { + + if (pattern[i] == text[j]) { + + i++, j++; + + if (i == n) { + + res.push_back(j - n); + + i = pref_suf[i - 1]; + + } + + } else { + + if (i != 0) { + + i = pref_suf[i - 1]; + + } else { + + j++; + + } + + } + + } + + + + return res; + + } + +} + + + +int main(){ + + ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); + + mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + + + string a, b; + + cin >> a >> b; + + int n = (a).length(), m = (b).length(); + + + + if (not KMP::check(a, b)) { + + cout << 0 << "\n"; + + return 0; + + } + + + + vector ans = KMP::match(a, n, b, m); + + + + cout << ans.size() << "\n"; + + for (int i = 0; i < min(1000, (int) ans.size()); i++) + + cout << ans[i] << " "; + + + + return 0; + +} diff --git a/strmatch/kmp/c7d08881-ef30-4d3e-bd8e-f2222e3ba167.cpp b/strmatch/kmp/c7d08881-ef30-4d3e-bd8e-f2222e3ba167.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edb9ae8e2771c26cea1563f0215a96eaeaa62bf4 --- /dev/null +++ b/strmatch/kmp/c7d08881-ef30-4d3e-bd8e-f2222e3ba167.cpp @@ -0,0 +1,108 @@ +#include + + +const int MAX_N = 2000005; + +const unsigned int MAX_ANS = 1000; + + + +int n, m; + + + +int lps[MAX_N]; + +char p[MAX_N], t[MAX_N]; + + + +std::vector ans; + + + +int main() { + + int curr; + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + std::cin >> (p + 1) >> (t + 1); + + n = strlen(p + 1); + + m = strlen(t + 1); + + t[0] = p[0] = '\0'; + + lps[0] = lps[1] = 0; + + for (int i = 2; i <= n; ++i) { + + curr = lps[i - 1]; + + while (curr > 0 && p[curr + 1] != p[i]) { + + curr = lps[curr]; + + } + + if (p[i] == p[curr + 1]) { + + lps[i] = curr + 1; + + } else { + + lps[i] = 0; + + } + + } + + curr = 0; + + for (int i = 1; i <= m; ++i) { + + while (curr > 0 && p[curr + 1] != t[i]) { + + curr = lps[curr]; + + } + + if (p[curr + 1] == t[i]) { + + ++curr; + + } else { + + curr = 0; + + } + + if (curr == n) { + + ans.push_back(i - curr + 1); + + } + + } + + std::cout << ans.size() << "\n"; + + for (int i = 0; i < ans.size(); ++i) { + + std::cout << ans[i] - 1 << " "; + + if (i >= 999) { + + return 0; + + } + + } + + return 0; + +} diff --git a/strmatch/kmp/c8047cbf-3e47-4dc4-9b0b-692573d166d4.cpp b/strmatch/kmp/c8047cbf-3e47-4dc4-9b0b-692573d166d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..890a1499a24032fcdaf3fead352afd285be3c5d1 --- /dev/null +++ b/strmatch/kmp/c8047cbf-3e47-4dc4-9b0b-692573d166d4.cpp @@ -0,0 +1,75 @@ +#include +using namespace std; + + +int M, N; +char A[2000005], B[2000005]; +int pi[2000005], pos[1024]; + + +void make_prefix(void) +{ + int i, q = 0; + pi[1] = 0; + for (i = 2, pi[1] = 0; i <= M; ++i) { + while (q && A[q+1] != A[i]) + q = pi[q]; + + if (A[q+1] == A[i]) + ++q; + + pi[i] = q; + } +} + + + +int main() { + int i, q = 0, count = 0; + + ifstream fin("strmatch.in"); + ofstream fout("strmatch.out"); + + fin.getline(A, 2000005); + fin.getline(B, 2000005); + + M = strlen(A); + N = strlen(B); + + for (i = M; i; i--) + A[i] = A[i-1]; + A[0] = ' '; + + for (i = N; i; i--) + B[i] = B[i-1]; + B[0] = ' '; + + make_prefix(); + + for (i = 1; i <= N; i++) { + while (q && A[q+1] != B[i]) + q = pi[q]; + + if (A[q+1] == B[i]) + ++q; + + if (q == M) { + q = pi[M]; + count++; + if (count <= 1000) + pos[count] = i - M; + } + } + + fout << count << "\n"; + + for (i = 1; i <= min(count, 1000); i++) + fout << pos[i] << " "; + + fout << "\n"; + + + + return 0; + +} diff --git a/strmatch/kmp/c9b9bbcd-51fb-49cf-9154-de134cde1528.cpp b/strmatch/kmp/c9b9bbcd-51fb-49cf-9154-de134cde1528.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b0b181cf6d7809c73fcc4cf38ceac14cbd9e7a5 --- /dev/null +++ b/strmatch/kmp/c9b9bbcd-51fb-49cf-9154-de134cde1528.cpp @@ -0,0 +1,96 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n, m, nsol; + +int pi[2000001], sol[1001]; + +char pat[2000001], str[2000001]; + + + +void make_prefix() + +{ + + pi[1] = 0; + + int k = 0; + + for(int q = 2; q <= m; ++q) + + { + + while(k > 0 && pat[k + 1] != pat[q]) k = pi[k]; + + if(pat[k + 1] == pat[q]) ++k; + + pi[q] = k; + + } + +} + + + +int main() + +{ + + fin >> (pat + 1); + + fin >> (str + 1); + + m = strlen(pat + 1); + + n = strlen(str + 1); + + + + make_prefix(); + + + + int q = 0; + + for(int i = 1; i <= n; ++i) + + { + + while(q > 0 && pat[q + 1] != str[i]) q = pi[q]; + + if(pat[q + 1] == str[i]) ++q; + + if(q == m) + + { + + ++nsol; + + q = pi[m]; + + if(nsol <= 1000) sol[nsol] = i - m; + + } + + } + + + + fout << nsol << "\n"; + + for(int i = 1; i <= min(nsol, 1000); ++i) fout << sol[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/c9fdaee3-73d1-4fe2-8909-9327b2114348.cpp b/strmatch/kmp/c9fdaee3-73d1-4fe2-8909-9327b2114348.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbba32ba64c739b2798dcd56cf8f77bea8fc8c3a --- /dev/null +++ b/strmatch/kmp/c9fdaee3-73d1-4fe2-8909-9327b2114348.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char a[2000005]; + +char b[2000005]; + +int table[2000005]; + +vector < int > v; + +int ans; + + + +int main() + +{ + + f >> a >> b; + + int m = strlen(a); + + int n = strlen(b); + + int len = 0; + + table[0] = 0; + + for (int i=1;i +#include + + +char s[2000001],u[2000001]; + +int v[1000],i,t,k,m,n,j,p[2000001],q; + +int main() + +{ + + freopen("strmatch.in","r",stdin),freopen("strmatch.out","w",stdout),scanf("%s\n%s",s,u),m=strlen(s),n=strlen(u); + + for(i=2;i1000?1000:t,i=0;i +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n,m,p[2000010],i,j,l,cnt,sol[1010]; + +char a[2000010],b[2000010]; + + + +int main(){ + + fin>>a+1>>b+1; + + n=strlen(a+1); + + m=strlen(b+1); + + + + l=0; + + for(i=2;i<=n;i++){ + + while(l && a[l+1]!=a[i]) + + l=p[l]; + + + + if(a[l+1]==a[i]) + + l++; + + + + p[i]=l; + + } + + + + l=0; + + for(i=1;i<=m;i++){ + + while(l && a[l+1]!=b[i]) + + l=p[l]; + + + + if(a[l+1]==b[i]) + + l++; + + + + if(l==n){ + + cnt++; + + if(cnt<=1000) + + sol[cnt]=i-n; + + l=p[l]; + + } + + } + + + + fout< + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n,m,k,i,nr,sol[1010],p[2000010]; + +char s[2000010],c[2000010]; + + + +int main() + +{ + + f>>(c+1);n=strlen(c+1); + + f>>(s+1);m=strlen(s+1); + + + + for(i=2;i<=n;i++) + + { + + while(k!=0 && c[k+1]!=c[i])k=p[k]; + + if(c[k+1]==c[i])k++; + + p[i]=k; + + } + + + + k=0; + + for(i=1;i<=m;i++) + + { + + while(k!=0 && c[k+1]!=s[i])k=p[k]; + + if(c[k+1]==s[i])k++; + + + + if(k==n) + + { + + nr++; + + if(nr<=1000)sol[nr]=i-n; + + } + + } + + + + g< +#include +#include + + + + +using namespace std; + + + +int la,lb,i,j,rez; + +char a[2000005],b[2000005]; + +vector urm; + +vector poz; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void citire(){ + + fin>>a>>b; + + la=strlen(a); + + lb=strlen(b); + + urm.assign (la+5,0); + + poz.assign (1005,0); + +} + + + +void prefix(){ + + int k=0; + + for (i=1; i0 && a[k]!=a[i]) k=urm[k-1]; + + if (a[k]==a[i]) k++; + + urm[i]=k; + + } + +}; + + + +void rezolvare(){ + + prefix(); + + j=0; + + for (i=0;i0 && a[j]!=b[i]) j=urm[j-1]; + + if (a[j]==b[i]) j++; + + if (j==la){ + + if (rez<1000) poz[++rez]=i-la+1; + + else rez++; + + j=urm[j-1]; + + } + + } + +} + + + +void scriere(){ + + fout<1000) rez=1000; + + for (i=1;i<=rez;i++) + + fout< + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A,B; + +int n, m; + +int pi[20000005]; + +int nr; + +vector res; + + + +void Read() + +{ + + f>>A; + + f>>B; + + + + m = A.length(); + + n = B.length(); + + + + A.insert(0, " "); + + B.insert(0, " "); + + + + f.close(); + +} + + + + + +void compute() + +{ + + int q = 0; + + + + for(int i = 2;i <= m;++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +void KMP() + +{ + + int q = 0; + + for(int i = 1;i <= n;++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == m) + + { + + ++nr; + + if(res.size() < 1000) + + res.push_back(i - m); + + q = pi[q]; + + } + + } + +} + + + +void Print() + +{ + + g<::iterator it = res.begin();it != res.end();++it) + + g<<*it<<" "; + + g.close(); + +} + + + +int main() + +{ + + Read(); + + compute(); + + KMP(); + + Print(); + + return 0; + +} diff --git a/strmatch/kmp/cd4fd002-1449-48bf-865c-e1430fcefb76.cpp b/strmatch/kmp/cd4fd002-1449-48bf-865c-e1430fcefb76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64bc82741b650171a20ef759edaa0dbfd71eccdb --- /dev/null +++ b/strmatch/kmp/cd4fd002-1449-48bf-865c-e1430fcefb76.cpp @@ -0,0 +1,152 @@ +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A,B; + +int n, m; + +int pi[20000005]; + +int nr; + +vector res; + + + +void Read() + +{ + + f>>A; + + f>>B; + + + + for(;(A[m] >= 'A' && A[m] <= 'Z') || (A[m] >= 'a' && A[m] <= 'z') || (A[m] >= '0' && A[m] <= '9');++m); + + for(;(B[n] >= 'A' && B[n] <= 'Z') || (B[n] >= 'a' && B[n] <= 'z') || (B[n] >= '0' && B[n] <= '9');++n); + + + + A.insert(0, " "); + + B.insert(0, " "); + + + + f.close(); + +} + + + + + +void compute() + +{ + + int q = 0; + + + + for(int i = 2;i <= m;++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +void KMP() + +{ + + int q = 0; + + for(int i = 1;i <= n;++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == m) + + { + + ++nr; + + if(res.size() < 1000) + + res.push_back(i - m); + + q = pi[q]; + + } + + } + +} + + + +void Print() + +{ + + g<::iterator it = res.begin();it != res.end();++it) + + g<<*it<<" "; + + g.close(); + +} + + + +int main() + +{ + + Read(); + + compute(); + + KMP(); + + Print(); + + return 0; + +} diff --git a/strmatch/kmp/cd76e4a8-515a-4a7d-a4f6-38b4fec2a5e3.cpp b/strmatch/kmp/cd76e4a8-515a-4a7d-a4f6-38b4fec2a5e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f8f7ea2bf829a0ba9f0f7299cbb64538bd7507 --- /dev/null +++ b/strmatch/kmp/cd76e4a8-515a-4a7d-a4f6-38b4fec2a5e3.cpp @@ -0,0 +1,144 @@ +#include + + + + +using namespace std; + + + +int m,n; + +char a[2000001], b[2000001]; + +int pi[2000001], pos[1024]; + +int ct; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void Peg(); + +void Read(); + +void Rez(); + +void Afis(); + + + +int main() + +{ + + Read(); + + Peg(); + + Rez(); + + Afis(); + + return 0; + +} + + + +void Read() + +{ + + fin.getline(a,2000001); + + fin.getline(b,2000001); + + m=strlen(a); + + n=strlen(b); + + for(int i=n;i;i--) b[i]=b[i-1]; + + for(int i=m;i;i--) a[i]=a[i-1]; + + b[0]=a[0]=' '; + +} + + + +void Peg() + +{ + + pi[1]=0; + + int q=0; + + for(int i=2;i<=m;i++) + + { + + while(q && a[q+1]!=a[i]) q=pi[q]; + + if(a[q+1]==a[i]) q++; + + pi[i]=q; + + } + +} + + + +void Rez() + +{ + + int q=0; + + for(int i=1;i<=n;i++) + + { + + while(q && a[q+1]!=b[i]) q=pi[q]; + + if(a[q+1]==b[i]) q++; + + if(q==m) + + { + + q=pi[m]; + + ct++; + + if(ct<=1000) + + pos[ct]=i-m; + + } + + } + +} + + + +void Afis() + +{ + + fout< + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +void usain_bolt() + +{ + + ios::sync_with_stdio(false); + + fin.tie(0); + +} + + + +const int N = 2e6 + 5; + + + +char s[N], t[N]; + +int pref[N], sol[N]; + + + +void prefix(int n) + +{ + + pref[1] = 0; + + int k = 0; + + for(int i = 2; i <= n; ++i) { + + while(k > 0 && s[i] != s[k + 1]) k = pref[k]; + + if(s[i] == s[k + 1]) ++k; + + pref[i] = k; + + } + +} + + + +int main() + +{ + + usain_bolt(); + + + + fin >> (s + 1) >> (t + 1); + + int szs = strlen(s + 1); + + int szt = strlen(t + 1); + + prefix(szs); + + int cnt = 0; + + for(int i = 1; i <= szt; ++i) { + + while(cnt > 0 && t[i] != s[cnt + 1]) cnt = pref[cnt]; + + if(t[i] == s[cnt + 1]) ++cnt; + + if(cnt >= szs) { + + if(sol[0] < 1000) sol[++sol[0]] = i - szs; + + else ++sol[0]; + + } + + } + + fout << sol[0] << "\n"; + + for(int i = 1; i <= min(sol[0], 1000); ++i) fout << sol[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/ceef7c68-ea80-4574-a984-1237594ba4cc.cpp b/strmatch/kmp/ceef7c68-ea80-4574-a984-1237594ba4cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e2c137c71514b5efbc60a9d809e2e6e39dee5b1 --- /dev/null +++ b/strmatch/kmp/ceef7c68-ea80-4574-a984-1237594ba4cc.cpp @@ -0,0 +1,89 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char pat[MAXN + 1], str[MAXN + 1]; + +int pi[MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (pat + 1) >> (str + 1); + + int n = strlen(pat + 1), m = strlen(str + 1); + + int k = 0; + + for(i = 2; i <= n; i++) { + + while(k > 0 && pat[k + 1] != pat[i]) { + + k = pi[k]; + + } + + k += (pat[k + 1] == pat[i]); + + pi[i] = k; + + } + + vector sol; + + int ans = 0; + + k = 0; + + for(i = 1; i <= m; i++) { + + while(k > 0 && pat[k + 1] != str[i]) { + + k = pi[k]; + + } + + k += (pat[k + 1] == str[i]); + + if(k == n) { + + ans++; + + if(sol.size() < 1000) + + sol.push_back(i - n); + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/kmp/d08262ee-ca3c-416f-976f-27dd75525125.cpp b/strmatch/kmp/d08262ee-ca3c-416f-976f-27dd75525125.cpp new file mode 100644 index 0000000000000000000000000000000000000000..449f94109e567ce5212116fd1792aa3b27fb82b1 --- /dev/null +++ b/strmatch/kmp/d08262ee-ca3c-416f-976f-27dd75525125.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +char a[2000002], b[2000002]; + +int p[2000002], afis[2000002]; + +int main() + +{ + + int n, m, i, j, nr_afis=0; + + fin.getline(a, 2000002); n=strlen(a); + + fin.getline(b, 2000002); m=strlen(b); + + j=0; p[1]=0; + + for(i=1; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +typedef pair pii; + +typedef long long int ll; + + + +int n, m, len; + +int z[2 * 2000010]; + +char a[2000010], b[2000010], c[2 * 2000010]; + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", a + 1); + + scanf("%s", b + 1); + + + + int n = strlen(a + 1); + + int m = strlen(b + 1); + + + + len = n; + + + + for (int i = 1; i <= n; i++) c[i] = a[i]; + + + + c[++len] = '@'; + + + + for (int i = 1; i <= m; i++) c[++len] = b[i]; + + + + z[1] = 0; + + int l = 0, r = 0; + + + + for (int i = 2; i <= len; i++) { + + + + if (i > r) { + + + + l = r = i; + + + + while (r <= len && c[r] == c[r - l + 1]) r++; + + + + z[i] = r - l; r--; + + } + + else + + { + + int pos = i - l + 1; + + + + if (i + z[pos] <= r) z[i] = z[pos]; else + + { + + l = i; + + + + while (r <= len && c[r] == c[r - l + 1]) r++; + + + + z[i] = r - l; r--; + + } + + } + + } + + + + vector vc; + + + + for (int i = n + 1; i <= len; i++) + + if (z[i] == n) vc.push_back(i - n - 2); + + + + printf("%d\n", ((int)(vc.size()))); + + + + for (int i = 0; i < min(((int)(vc.size())), 1000); i++) printf("%d ", vc[i]); + return 0; + +} diff --git a/strmatch/kmp/d148d1fc-c227-472f-9f35-1617c4d179ef.cpp b/strmatch/kmp/d148d1fc-c227-472f-9f35-1617c4d179ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76126dd128f11c54d43b9b4c51b1da0eacb40961 --- /dev/null +++ b/strmatch/kmp/d148d1fc-c227-472f-9f35-1617c4d179ef.cpp @@ -0,0 +1,94 @@ +#include +using namespace std; + + + +const int mod=1e9+7; + +const int modu=1999999973; + +const int modul=998244353; + + + +vectorindx; + +int pref[4000005]; + +string s,t; + + + +void kmp(){ + + t=(s+"#"+t); + + int j=0; + + for(int i=1;i=s.size()) indx.push_back(i); + + } + +} + + + +int32_t main(){ + + ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0); + + srand(chrono::steady_clock::now().time_since_epoch().count()); + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + cin >> s >> t; + + kmp(); + + int sz=indx.size(); + + cout << sz << '\n'; + + for(int i=0;i +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Dim=2e6+5; + +const int Mod1=1e6+7; + +const int Mod2=1e6+21; + +const int d=91; + + + +void P1() + +{ + + int ans=0; + + string A,B; + + f>>A>>B; + + int N=A.size(),M=B.size(); + + for(int i=N;i>=1;i--) A[i]=A[i-1]; + + for(int i=M;i>=1;i--) B[i]=B[i-1]; + + + + vector < int > dp1(N+1),dp2(M+1),V; + + dp1[1]=0; + + for(int i=2;i<=N;i++) + + { + + int K=dp1[i-1]; + + while( K>0 && A[K+1]!=A[i] ) K=dp1[K]; + + + + dp1[i]=K + (A[K+1]==A[i]); + + } + + dp2[0]=0; + + for(int i=1;i<=M;i++) + + { + + int K=dp2[i-1]; + + while( K>0 && A[K+1]!=B[i] ) K=dp1[K]; + + + + dp2[i]=K+(A[K+1]==B[i]); + + + + if(dp2[i]==N) + + { + + ans++; + + if(ans<=1000) V.push_back(i-N); + + } + + } + + g<>A>>B; + + int ans=0; + + int hashP1=0,hashP2=0,hashS1=0,hashS2=0,cos1=1,cos2=1; + + int N=A.size(),M=B.size(); + + + + vector < int > V; + + + + for(int i=0;i +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char p[2000005],t[2000005]; + +int m,n,LPS[2000005],poz[2000005],k; + +void lps() + +{ + + for (int i = 1, j = 0; i < m; ++i) { + + if (p[i] == p[j]) + + LPS[i] = ++j; + + else if (j != 0) + + j = LPS[j - 1], --i; + + else + + LPS[i] = 0; + + } + +} + +void match() + +{ + + int i = 0, j = 0; + + while(j < n) + + { + + if(p[i] != t[j]) + + { + + if(i == 0) + + ++j; + + else + + i = LPS[i - 1]; + + } + + else + + { + + ++i; + + ++j; + + if(i == m) + + { + + poz[k++] = j - i; + + i = LPS[i - 1]; + + + + + + } + + } + + } + +} + + + +int main() + +{ + + fin.getline(p,2000005); + + fin.getline(t,2000005); + + m = strlen(p); + + n = strlen(t); + + lps(); + + match(); + + fout << k << "\n"; + + if(k > 1000) + + k = 1000; + + for(int i = 0; i < k; i++) + + fout << poz[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/d44e5815-cce1-4a0b-8976-37596ba16e37.cpp b/strmatch/kmp/d44e5815-cce1-4a0b-8976-37596ba16e37.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd7c82d48ad314868875ab04a5806ac1cc8023eb --- /dev/null +++ b/strmatch/kmp/d44e5815-cce1-4a0b-8976-37596ba16e37.cpp @@ -0,0 +1,98 @@ +#include + + + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +string A, B; + +int M, N, PI[2000002], sol[1 << 10], cnt, q; + + + +void Make_Prefix () { + + for (int i = 2, q = 0; i <= M; i ++) { + + while (q && A[q + 1] != A[i]) + + q = PI[q]; + + if (A[q + 1] == A[i]) + + q ++; + + PI[i] = q; + + } + +} + + + +int main () { + + fin >> A >> B; + + M = A.size(), N = B.size(); + + for (int i = M; i > 0; i --) + + A[i] = A[i - 1]; + + A[0] = ' '; + + for (int i = N; i > 0; i --) + + B[i] = B[i - 1]; + + B[0] = ' '; + + Make_Prefix(); + + for (int i = 1; i <= N; i ++) { + + while (q && A[q + 1] != B[i]) + + q = PI[q]; + + if (A[q + 1] == B[i]) + + q ++; + + if (q == M) { + + q = PI[M]; + + cnt ++; + + if (cnt <= 1000) + + sol[cnt] = i - M; + + } + + } + + fout << cnt << '\n'; + + for (int i = 1; i <= min (cnt, 1000); i ++) + + fout << sol[i] << ' '; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/d4512c50-6053-4d5f-be98-a2c3d2252419.cpp b/strmatch/kmp/d4512c50-6053-4d5f-be98-a2c3d2252419.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa842faf9e21171a37f3170241ae322e554193db --- /dev/null +++ b/strmatch/kmp/d4512c50-6053-4d5f-be98-a2c3d2252419.cpp @@ -0,0 +1,88 @@ +#include + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int i, j, n, m, cnt; + +int p[2000005], sol[2000005]; + + + +char a[2000005], b[2000005]; + + + +int main(){ + + fin >> a + 1; + + fin >> b + 1; + + n = strlen (a + 1), m = strlen (b + 1); + + for (i=2; i<=n; i++){ + + while (j > 0 && a[j+1] != a[i]){ + + j = p[j]; + + } + + if (a[j+1] == a[i]){ + + j++; + + } + + p[i] = j; + + } + + j = 0; + + for (i=1; i<=m; i++){ + + while (j > 0 && a[j+1] != b[i]){ + + j = p[j]; + + } + + if (a[j+1] == b[i]){ + + j++; + + } + + if (j == n){ + + sol[++cnt] = i - j; + + j = p[j]; + + } + + } + + fout << cnt << "\n"; + + cnt = min (cnt, 1000); + + for (i=1; i<=cnt; i++){ + + fout << sol[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/kmp/d5d99b9f-d647-450a-bddc-429ffc92c53b.cpp b/strmatch/kmp/d5d99b9f-d647-450a-bddc-429ffc92c53b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27f1de5b688fa8bbbb4ee70a96a5624fd2659059 --- /dev/null +++ b/strmatch/kmp/d5d99b9f-d647-450a-bddc-429ffc92c53b.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; + +ifstream f("strmatch.in"); +ofstream g("strmatch.out"); + +int pi[2000001]; +int noMatches = 0; +int matchBeginnings[2000001]; + +void buildAutomaton(string s) { + int matchingLength = 0; + pi[0] = 0; + for (int i = 1; i < s.length(); i++) { + while (matchingLength > 0 and s[matchingLength] != s[i]) { + matchingLength = pi[matchingLength - 1]; + } + if (s[matchingLength] == s[i]) { + matchingLength++; + } + pi[i] = matchingLength; + } +} + +void findMatches(string a, string b) { + int aPrefixLength = 0; + for (int i = 0; i < b.length(); i++) { + while (aPrefixLength > 0 && a[aPrefixLength] != b[i]) { + aPrefixLength = pi[aPrefixLength - 1]; + } + if (a[aPrefixLength] == b[i]) { + aPrefixLength++; + } + if (aPrefixLength == a.length()) { + matchBeginnings[noMatches] = i - a.length() + 1; + noMatches++; + } + } +} + +void printMatches() { + g << noMatches << endl; + for (int i = 0; i < min(noMatches, 1000); i++) { + g << matchBeginnings[i] << " "; + } + g << endl; +} + +int main() { + string a, b; + f >> a >> b; + buildAutomaton(a); + findMatches(a, b); + printMatches(); + return 0; +} diff --git a/strmatch/kmp/d6b1dabe-7603-4448-adc6-8d942d4d5b57.cpp b/strmatch/kmp/d6b1dabe-7603-4448-adc6-8d942d4d5b57.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e75e6039ff6ff688ee3c125930ccf600266bd021 --- /dev/null +++ b/strmatch/kmp/d6b1dabe-7603-4448-adc6-8d942d4d5b57.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char pattern[2000005], text[2000005]; + +int prefix[2000005], n, m, k; + +vector sol; + + + +void read() + +{ + + f.getline(pattern+1, 2000005); + + f.getline(text+1, 2000005); + + n = strlen(pattern+1); + + m = strlen(text+1); + +} + + + +void calcPrefix() + +{ + + int j = 0; + + for(int i = 2; i <= n; i++) + + { + + while(j > 0 && pattern[i] != pattern[j+1]) + + { + + j = prefix[j]; + + } + + if(pattern[i] == pattern[j+1]) + + { + + prefix[i] = j+1; + + j++; + + } + + } + +} + + + +void calcMatch() + +{ + + int i = 0; + + for(int j = 1; j<=m; j++) + + { + + while(i > 0 && pattern[i+1] != text[j]) + + { + + i = prefix[i]; + + } + + if(pattern[i+1] == text[j]) + + { + + i++; + + if(i == n) + + { + + k++; + + if(sol.size() < 1000) + + sol.push_back(j - i); + + i = prefix[i]; + + } + + } + + } + +} + + + +void afisare() + +{ + + g< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A, B; + +int M, N, nr; + +int pi[20000005]; + +int pos[20000005]; + + + + + +void Read() + +{ + + f>>A; + + f>>B; + +} + + + +void make_prefix() + +{ + + int q = 0; + + for(int i = 2;pi[0] = 1,i <= M;++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +void Solve() + +{ + + int q = 0; + + for(;(A[M] >= 'A' && A[M] <= 'Z') || (A[M] >= 'a' && A[M] <= 'z') || (A[M] >= '0' && A[M] <= '9');++M); + + for(;(B[N] >= 'A' && B[N] <= 'Z') || (B[N] >= 'a' && B[N] <= 'z') || (B[N] >= '0' && B[N] <= '9');++N); + + A.insert(0, " "); + + B.insert(0, " "); + + + + make_prefix(); + + + + for(int i = 1;i <= N;++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == M) + + { + + ++nr; + + q = pi[M]; + + if(nr <= 1000) + + pos[nr] = i - M; + + } + + } + + g< + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string a,b; + +int pi[2000005], poz[2000],nr,n,m; + +void prefix() + +{ + + int q = 0; + + pi[1] = 0; + + for(int i = 2; i <= n; ++i) + + { + + while(q && a[q+1] != a[i]) + + q = pi[q]; + + if(a[q+1] == a[i]) + + ++q; + + pi[i] = q; + + } + +} + +void pattern() + +{ + + int q = 0; + + for(int i = 1; i <= m; ++i) + + { + + while(q && a[q+1] != b[i]) + + q = pi[q]; + + if(a[q+1] == b[i]) + + ++q; + + if(q == n) + + { + + q = pi[n]; + + ++nr; + + if(nr <= 1000) + + poz[nr] = i - n; + + } + + } + +} + +int main() + +{ + + in >> a >> b; + + n = a.size(); + + m = b.size(); + + for(int i = n; i > 0; --i) a[i] = a[i-1]; + + for(int i = m; i > 0; --i) b[i] = b[i-1]; + + a[0] = ' '; + + b[0] = ' '; + + prefix(); + + pattern(); + + out << nr << '\n'; + + for(int i = 1; i <= min(nr,1000); ++i) + + out << poz[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/d919e0e6-660b-4053-b9d6-7e8566d35745.cpp b/strmatch/kmp/d919e0e6-660b-4053-b9d6-7e8566d35745.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd98a9848fed677cdb74c3c5537713b5af57676c --- /dev/null +++ b/strmatch/kmp/d919e0e6-660b-4053-b9d6-7e8566d35745.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +using namespace std; + +const int lmax = 2000001; +const int nmax = 1000; +char A[lmax], B[lmax]; +int lcp[lmax]; + +vector sol; + +void preProcessKMP(){ + int i,j,n; + n = strlen(A); + j = 0, lcp[0] = 0; + for(i=1; i +#include +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string a, b; + +int c[2000004], j; + +queue q; + + + +int main() { + + f>>a>>b; + + a="0"+a; + + b="0"+b; + + + + for(int i=2;i +#include + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int k,q,nr,n,m,urm[2000005],v[1005]; + +char N[2000005],M[2000005]; + + + +int main() + +{ + + int i; + + f.getline(N,2000005); + + f.getline(M,2000005); + + k=0;n=strlen(N);urm[1]=0; + + for (i=2;i<=n;i++) + + { + + while (k>0 && N[k]!=N[i-1]) k=urm[k]; + + if (N[k]==N[i-1]) k++; + + urm[i]=k; + + } + + q=0;nr=0;m=strlen(M); + + for (i=0;i0 && N[q]!=M[i]) q=urm[q]; + + if (N[q]==M[i]) q++; + + if (q==n) + + { + + nr++; + + if (nr<=1000) v[nr]=i-n+1; + + } + + } + + g<=1000) nr=1000; + + for (i=1;i<=nr;i++) g< +#include +using namespace std; + +ifstream cin ("strmatch.in"); + +ofstream cout ("strmatch.out"); + +string s, s1, s2; + +int pi[4000005]; + +int sol[1005]; + +int main() + +{ + + int n, m, i, cnt = 0; + + cin >> s1; + + cin >> s2; + + s = s1 + '#' + s2; + + for (i = 1; i < s.size(); i++) + + { + + int now = pi[i - 1]; + + while (now && s[i] != s[now]) + + now = pi[now - 1]; + + if (s[i] == s[now]) + + now++; + + pi[i] = now; + + if (pi[i] == s1.size()) + + { + + cnt++; + + if (cnt < 1001) + + sol[cnt] = i - 2 * s1.size(); + + } + + } + + cout << cnt << '\n'; + + for (i = 1; i <= min(cnt, 1000); i++) + + cout << sol[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/da36a5f6-2578-468f-b2a3-194fc7b805f1.cpp b/strmatch/kmp/da36a5f6-2578-468f-b2a3-194fc7b805f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf8dc54b969d2daca5cc926ef64fcf829e3a5785 --- /dev/null +++ b/strmatch/kmp/da36a5f6-2578-468f-b2a3-194fc7b805f1.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +vector kmp(string s, string pat) { + + int m = (int) pat.size(); + + int n = (int) s.size(); + + vector ps(m, 0); + + int j = 0; + + for (int i = 1; i < m; i++) { + + while (j && pat[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[i] == pat[j]) { + + j++; + + } + + ps[i] = j; + + } + + j = 0; + + vector ret; + + for (int i = 0; i < n; i++) { + + while (j && s[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[j] == s[i]) { + + j++; + + } + + if (j == m) { + + j = ps[m - 1]; + + ret.push_back(i - m + 1); + + } + + } + + return ret; + +} + + + +int main() { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + string pat, s; + + cin >> pat >> s; + + auto ret = kmp(s, pat); + + cout << (int) ret.size() << "\n"; + + while ((int) ret.size() > 1000) { + + ret.pop_back(); + + } + + for (auto &i : ret) { + + cout << i << " "; + + } + + cout << "\n"; + +} diff --git a/strmatch/kmp/db63e3ea-7ebe-468d-9acf-ce92153d98a0.cpp b/strmatch/kmp/db63e3ea-7ebe-468d-9acf-ce92153d98a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a138ab5ac4ddc97e51d556eb05e4bde215351f17 --- /dev/null +++ b/strmatch/kmp/db63e3ea-7ebe-468d-9acf-ce92153d98a0.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + + + +char sir[2000005], pattern[2000005]; + +int prefix[2000005] , rez[1005]; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + int lsir = 0, lpattern = 0, i = 0, j = 0, n = 0; + + fgets(pattern, sizeof(pattern), stdin); + + fgets(sir, sizeof(sir), stdin); + + + + lsir = 0; + + lpattern = 0; + + for (; (pattern[lpattern] >= 'A' && pattern[lpattern] <= 'Z') || (pattern[lpattern] >= 'a' && pattern[lpattern] <= 'z') + + || (pattern[lpattern] >= '0' && pattern[lpattern] <= '9'); ++lpattern); + + for (; (sir[lsir] >= 'A' && sir[lsir] <= 'Z') || (sir[lsir] >= 'a' && sir[lsir] <= 'z') + + || (sir[lsir] >= '0' && sir[lsir] <= '9'); ++lsir); + + + + for(i = lsir; i >= 1; --i) + + sir[i] = sir[i-1]; + + for(i = lpattern; i >= 1; --i) + + pattern[i] = pattern[i-1]; + + + + for(i = 2; i <= lpattern; ++i) { + + while(j && pattern[j + 1] != pattern[i]) + + j = prefix[j]; + + if(pattern[i] == pattern[j+1]) + + ++j; + + prefix[i] = j; + + } + + + + j = 0; + + for(i = 1; i <= lsir; ++i) { + + while(j && sir[i] != pattern[j+1]) + + j = prefix[j]; + + if(sir[i] == pattern[j+1]) + + ++j; + + if(j == lpattern) { + + j = prefix[lpattern]; + + ++n; + + if(n<=1000) + + rez[n]=i-lpattern; + + } + + } + + printf("%d\n", n); + + n = (n < 1000 ? n : 1000); + + for(i = 1; i <= n; ++i) + + printf("%d ", rez[i]); + + printf("\n"); + +} diff --git a/strmatch/kmp/db8fb1f0-e27c-4851-8f2a-f09d8d4e674b.cpp b/strmatch/kmp/db8fb1f0-e27c-4851-8f2a-f09d8d4e674b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f630849bb0de733fb03907c507598274ed2cece4 --- /dev/null +++ b/strmatch/kmp/db8fb1f0-e27c-4851-8f2a-f09d8d4e674b.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string a, b; + +vector fin; + +int aux[2000001]; + +int nrpos; + + + +void pref() + +{ + + int q=0; + + for(int i=2; i<=a.size(); i++) + + { + + while(q && a[q+1]!=a[i]) q=aux[q]; + + if(a[q+1]==a[i]) ++q; + + aux[i]=q; + + } + +} + + + +int main() + +{ + + getline(in, a); + + getline(in, b); + + a+=' ', b+=' '; + + for(int i=a.size()-1; i; i--) a[i]=a[i-1]; + + for(int i=b.size()-1; i; i--) b[i]=b[i-1]; + + a[0]=' ', b[0]=' '; + + pref(); + + int q=0; + + for(int i=1; i +#include +#include + +using namespace std; + +const int SMAX = 2000005; + +char a[SMAX],b[SMAX]; + +int pi[SMAX]; + +int n,m; + +void make_prefix(){ + pi[1]=0; + int q=0; + for(int i=2;i<=m;++i){ + while(q && a[q+1]!=a[i]) + q=pi[q]; + if(a[q+1]==a[i]) + q++; + pi[i]=q; + } +} + +int cnt; +int poz[1024]; + +void kmp(){ + int q=0; + for(int i=1;i<=n;++i){ + while(q && a[q+1]!=b[i]) + q=pi[q]; + if(a[q+1]==b[i]) + q++; + + if(q==m){ + q=pi[m]; + cnt++; + if(cnt<=1000) + poz[cnt]=i-m; + } + } +} + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + scanf("%s\n%s\n",a+1,b+1); + + a[0]=' '; + b[0]=' '; + + n=strlen(b)-1; + m=strlen(a)-1; + + make_prefix(); + + kmp(); + + printf("%d\n",cnt); + + for(int i=1;i<=min(cnt,1000);++i) + printf("%d ",poz[i]); + + return 0; +} diff --git a/strmatch/kmp/dcb98619-1222-4695-a5a2-78bb1054de9c.cpp b/strmatch/kmp/dcb98619-1222-4695-a5a2-78bb1054de9c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9634b97bf7aae1f63a2beee2a29eaca9a0cfafa --- /dev/null +++ b/strmatch/kmp/dcb98619-1222-4695-a5a2-78bb1054de9c.cpp @@ -0,0 +1,154 @@ +#include + + +using namespace std; + + + +ifstream in; + +ofstream out; + + + +string pat, s; + +vector sec,sol; + + + +void pattern() + +{ + + sec.push_back(0); + + int z = 1; + + while (z> pat >> s; + + + + pattern(); + + kmp(); + + int k; + + if (1000 > sol.size()) + + k = sol.size(); + + else + + k = 1000; + + out << sol.size() << endl; + + for (int i = 0; i < k; i++) + + out << sol[i] << " "; + +} diff --git a/strmatch/kmp/dd173555-6871-4010-a44c-ef9de45c2c31.cpp b/strmatch/kmp/dd173555-6871-4010-a44c-ef9de45c2c31.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b9306b5c22cb82797181aecab2c0aba3dd79a63 --- /dev/null +++ b/strmatch/kmp/dd173555-6871-4010-a44c-ef9de45c2c31.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; +const int NMAX = 1e6; + +vector ans; + +int pi[NMAX * 2 + 1]; +char a[NMAX * 2 + 2], b[NMAX * 2 + 1]; + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + int n, m, nr = 0; + scanf("%s\n", (a + 1)); + n = strlen(a + 1); + a[++n] = '#'; + scanf("%s", (b + 1)); + m = strlen(b + 1); + + + pi[1] = 0; + int k = 0; + for (int i = 2; i <= n; i++) { + while (k!= 0 && a[ i ] != a[k + 1]) { + k = pi[k]; + } + if (a[ i ] == a[k + 1]) { + k++; + } + pi[ i ] = k; + } + + k = 0; + for (int i = 1; i <= m; i++) { + while (k != 0 && b[ i ] != a[k + 1]) { + k = pi[ k ]; + } + if (b[ i ] == a[k + 1]) { + k++; + } + if (k == n-1) { + nr++; + if (ans.size() < 1000) { + ans.push_back(i - n + 1); + } + } + } + + cout << nr << '\n'; + for (auto idx: ans) { + cout << idx << " "; + } + +} diff --git a/strmatch/kmp/ddcaf856-310b-4fbf-a65d-17b2331f87da.cpp b/strmatch/kmp/ddcaf856-310b-4fbf-a65d-17b2331f87da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..423e06171f87e51b954e72a79ab0fdbeae1e123b --- /dev/null +++ b/strmatch/kmp/ddcaf856-310b-4fbf-a65d-17b2331f87da.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +using namespace std; + +ifstream f("strmatch.in"); +ofstream g("strmatch.out"); + +int p[2000005]; +char a[2000005], b[2000005]; +int sol[1005]; + + +void rez() { + int k = 0, m = strlen(a + 1); + p[1] = 0; + for (int j = 2; j <= m; j++) { + while (k > 0 && a[k + 1] != a[j]) + k = p[k]; + if (a[k + 1] == a[j]) { + k++; + } + p[j] = k; + } +} + +void afisare(int nr) { + g << nr << '\n'; + nr = min(nr, 1000); + for (int i = 1; i <= nr; i++) + g << sol[i] << ' '; +} + +int match() { + int k = 0, nr = 0, n = strlen(b + 1), m = strlen(a + 1); + + for (int i = 1; i <= n; i++) { + while (k > 0 && a[k + 1] != b[i]) + k = p[k]; + if (b[i] == a[k + 1]) + k++; + if (k == m) { + nr++; + if (nr <= 1000) + sol[nr] = i - m; + k = p[k]; + } + } + return nr; +} + +int main() { + f.getline(a + 1, 2000005); + f.getline(b + 1, 2000005); + rez(); + afisare(match()); + return 0; +} diff --git a/strmatch/kmp/de9d02f8-76b3-403d-b83a-bcbdb705895d.cpp b/strmatch/kmp/de9d02f8-76b3-403d-b83a-bcbdb705895d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e81959b9adffde832b18394a888adfbd90bd111d --- /dev/null +++ b/strmatch/kmp/de9d02f8-76b3-403d-b83a-bcbdb705895d.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +const int MAX = 2e6 + 10; + + + +char A[MAX], B[MAX]; + +int pi[MAX], ans[1010], a, b, sol; + + + +void PI() + +{ + + int q = 0; + + for(int i = 2; i <= a; ++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +int main() + +{ + + f >> (A + 1); a = strlen(A + 1); + + f >> (B + 1); b = strlen(B + 1); + + + + PI(); + + + + int q = 0; + + for(int i = 1; i <= b; ++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == a) + + { + + ++sol; + + q = pi[q]; + + if(sol <= 1000) + + ans[sol] = i - a; + + } + + } + + + + g << sol << "\n"; + + for(int i = 1; i <= min(sol, 1000); ++i) + + g << ans[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/df1928f2-7137-4f8b-bddb-4e8acf80a599.cpp b/strmatch/kmp/df1928f2-7137-4f8b-bddb-4e8acf80a599.cpp new file mode 100644 index 0000000000000000000000000000000000000000..471ec6e7480a1c09ccd60043f168b4ae41301031 --- /dev/null +++ b/strmatch/kmp/df1928f2-7137-4f8b-bddb-4e8acf80a599.cpp @@ -0,0 +1,141 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char string_array[2000005], pattern[2000005]; +void lps(int lps_array[], char pattern[]){ + + lps_array[0] = 0; + + int i = 1, n = strlen(pattern), len = 0; + + while(i < n){ + + if(pattern[i] == pattern[len]){ + + len++; + + lps_array[i] = len; + + i++; + + } + + else{ + + if(len != 0){ + + len = lps_array[len - 1]; + + } + + else{ + + lps_array[i] = 0; + + i++; + + } + + } + + } + +} + + + +void kmp(char sequence[], char pattern[]){ + + int i = 0, j = 0, n = strlen(sequence), m = strlen(pattern); + + int lps_array[m], cnt = 0, matches[1000]; + + + + lps(lps_array, pattern); + + while(i < n){ + + if(sequence[i] == pattern[j]){ + + i++; + + j++; + + if(j == m){ + + if(cnt < 1000){ + + matches[cnt] = i - j; + + } + + cnt++; + + j = lps_array[j - 1]; + + } + + } + + else if((i < n) && (pattern[j] != sequence[i])){ + + if(j > 0){ + + j = lps_array[j - 1]; + + } + + else{ + + i++; + + } + + + + } + + } + + fout << cnt << "\n"; + + for(int i = 0; i < ((cnt)<(1000)?(cnt):(1000)); ++i){ + + fout << matches[i] << " "; + + } + +} + + + +int main(){ + + fin >> pattern; + + fin >> string_array; + + kmp(string_array, pattern); + + + + return 0; + +} diff --git a/strmatch/kmp/dfc0d057-d964-4c83-a1a8-be4f6915597e.cpp b/strmatch/kmp/dfc0d057-d964-4c83-a1a8-be4f6915597e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d9da47257bcff697a94c3254641497adb0bef398 --- /dev/null +++ b/strmatch/kmp/dfc0d057-d964-4c83-a1a8-be4f6915597e.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int DIM = 2e6; + + + +int N, M; + +char p[DIM + 5], text[DIM + 5]; + + + +int pi[DIM + 5]; + +int ans; + +vector kmp; + + + +void Pi() + +{ + + int j = 0; + + + + for(int i = 2; i <= N; i++) { + + + + while(p[i] != p[j + 1] && j != 0) + + j = pi[j]; + + + + if(p[i] == p[j + 1]) + + j++; + + + + pi[i] = j; + + + + } + +} + + + +void Kmp() + +{ + + int j = 0; + + + + for(int i = 1; i <= M; i++) { + + + + while(text[i] != p[j + 1] && j != 0) + + j = pi[j]; + + + + if(text[i] == p[j + 1]) + + j++; + + + + if(j == N) { + + + + ans++; + + if(kmp.size() < 1000) + + kmp.push_back(i - N); + + + + j = pi[j]; + + } + + + + } + +} + + + +int main() + +{ + + fin >> (p + 1); N = strlen(p + 1); + + fin >> (text + 1); M = strlen(text + 1); + + + + Pi(); + + + + Kmp(); + + + + fout << ans << '\n'; + + for(auto it : kmp) + + fout << it << ' '; + + + + return 0; + +} diff --git a/strmatch/kmp/dfcca7e2-341c-4b72-a37c-8cf06855469f.cpp b/strmatch/kmp/dfcca7e2-341c-4b72-a37c-8cf06855469f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed18a768eec284c920f8fc67f35e291af514044e --- /dev/null +++ b/strmatch/kmp/dfcca7e2-341c-4b72-a37c-8cf06855469f.cpp @@ -0,0 +1,107 @@ +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s1[4000002], s2[4000002]; + +int pref[4000002], n; + + + +void f_pref(char s[4000002], int pref[4000002], int n) { + + pref[0] = 0; + + for (int i = 1; i < n; i++) { + + int j = i; + + while (j > 0 && s[pref[j - 1]] != s[i]) { + + j = pref[j - 1]; + + } + + if (j == 0) + + pref[i] = 0; + + else + + pref[i] = pref[j - 1] + 1; + + } + +} + + + +int main() + +{ + + int ct=0, lg1, i; + + + + fin.getline(s1, 4000002); + + lg1=strlen(s1); + + fin.getline(s2, 4000002); + + strcat(s1, "$"); + + strcat(s1, s2); + + n=strlen(s1); + + f_pref(s1, pref, n); + + for(i=0; i + + +using namespace std; + +string A, B; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +int n, m; + +int pi[2000005]; + +vector res; + +int nr; + + + +void Read() + +{ + + f>>A>>B; + + m = A.length(); + + n = B.length(); + + + + A.insert(0, " "); + + B.insert(0, " "); + +} + + + +void compute() + +{ + + int q = 0; + + for(int i = 2;i <= m;++i) + + { + + while(q && A[q + 1] != A[i]) + + q = pi[q]; + + if(A[q + 1] == A[i]) + + ++q; + + pi[i] = q; + + } + +} + + + +void KMP() + +{ + + int q = 0; + + for(int i = 1;i <= n;++i) + + { + + while(q && A[q + 1] != B[i]) + + q = pi[q]; + + if(A[q + 1] == B[i]) + + ++q; + + if(q == m) + + { + + ++nr; + + q = pi[q]; + + if(nr <= 1000) + + res.push_back(i - m); + + } + + } + +} + + + +void Print() + +{ + + g<::iterator it = res.begin();it != res.end();++it) + + g<<*it<<" "; + +} + +int main() + +{ + + Read(); + + compute(); + + KMP(); + + Print(); + + return 0; + +} diff --git a/strmatch/kmp/e1e6b532-6084-450a-af25-3ac5a9f9ce45.cpp b/strmatch/kmp/e1e6b532-6084-450a-af25-3ac5a9f9ce45.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27182ec8c693941a356b8228964c3cbbb2bc9d86 --- /dev/null +++ b/strmatch/kmp/e1e6b532-6084-450a-af25-3ac5a9f9ce45.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char A[2000001], B[2000001]; + +int t[2000001]; + + + +void init() { + + t[0] = 0; + + int i = 1, j = 0; + + while (A[i] != '\0') { + + if (A[i] == A[j]) { + + t[i] = j + 1; + + ++i, ++j; + + } + + else { + + if (j == 0) + + t[i] = 0, ++i; + + else j = t[j - 1]; + + } + + } + +} + + + +void KMP() { + + int nr = 0; + + int i = 0, j = 0; + + vector vec; + + while (B[j] != '\0') { + + if (A[i] == B[j]) { + + ++i, ++j; + + if (A[i] == '\0') { ++nr; if(nr<=1000) vec.push_back(j - i); i = t[i - 1]; } + + } + + else + + if (i == 0) + + ++j; + + else i = t[i - 1]; + + } + + fout << nr << '\n'; + + for (int x : vec) + + fout << x << ' '; + +} + + + +int main() + +{ + + fin >> A >> B; + + init(); + + KMP(); + + + + return 0; + +} diff --git a/strmatch/kmp/e364864b-733a-461a-8ab8-c22e7caeb0ad.cpp b/strmatch/kmp/e364864b-733a-461a-8ab8-c22e7caeb0ad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22bf62456484c2cf7019083ae61996e829ffe80d --- /dev/null +++ b/strmatch/kmp/e364864b-733a-461a-8ab8-c22e7caeb0ad.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_NO_POS = 1000; + + + +void buildSuffixTable(string & pattern, vector & suffixTable) { + + int len = 0; + + int i = 1; + + while (i < pattern.size()) { + + if (pattern[i] == pattern[len]) { + + len++; + + suffixTable[i] = len; + + i++; + + } else if (len > 0) { + + len = suffixTable[len - 1]; + + } else { + + suffixTable[i] = 0; + + i++; + + } + + } + +} + + + +void kmp(string & A, string & B, vector & suffixTable, int & n, list & ans) { + + if (A.size() > B.size()) { + + return; + + } + + int len = 0; + + int i = 0; + + while (i < B.size()) { + + if (B[i] == A[len]) { + + len++; + + if (len == A.size()) { + + n++; + + if (n <= MAX_NO_POS) { + + ans.push_back(i - len + 1); + + } + + len = suffixTable[len - 1]; + + } + + i++; + + } else if (len > 0) { + + len = suffixTable[len - 1]; + + } else { + + len = 0; + + i++; + + } + + } + +} + + + +int main() { + + ifstream fin; + + ofstream fout; + + string A, B; + + + + fin.open("strmatch.in"); + + fin >> A >> B; + + fin.close(); + + + + vector suffixTable(A.size(), 0); + + buildSuffixTable(A, suffixTable); + + + + int n = 0; + + list ans; + + kmp(A, B, suffixTable, n, ans); + + + + fout.open("strmatch.out"); + + fout << n << endl; + + for (auto it = ans.begin(); it != ans.end(); ++it) { + + fout << *it << " "; + + } + + fout.close(); + + + + ans.clear(); + + + + return 0; + +} diff --git a/strmatch/kmp/e37bbaaa-740a-4314-b7fa-e192959b0128.cpp b/strmatch/kmp/e37bbaaa-740a-4314-b7fa-e192959b0128.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68432bb6519507ac006df793cbf75ec35b0835dd --- /dev/null +++ b/strmatch/kmp/e37bbaaa-740a-4314-b7fa-e192959b0128.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +FILE *fin = fopen("strmatch.in", "r"); + +FILE *fout = fopen("strmatch.out", "w"); + + + +char patt[2000005],txt[2000005]; + +int p,t,i,lg,lps[2000005],ap[1005],nrap; + + + +int main() + +{ + + fgets(patt+1, 2000005 -2, fin); + + fgets(txt+1, 2000005 -2, fin); + + patt[0] = txt[0] = '.'; + + p = strlen(patt)-2; + + t = strlen(txt)-2; + + + + for(i=2; i<=p; ++i) + + { + + while(patt[i] != patt[lg+1] and lg>0) + + lg = lps[lg]; + + if(patt[i] == patt[lg+1]) + + lg++; + + lps[i] = lg; + + } + + + + lg = 0; + + for(i=1; i<=t; ++i) + + { + + while(txt[i] != patt[lg+1] and lg>0) + + lg = lps[lg]; + + + + if(txt[i] == patt[lg+1]) + + ++lg; + + + + if(lg == p) + + { + + ++nrap; + + if(nrap <= 1000) + + ap[nrap] = i-p; + + lg = lps[lg]; + + } + + } + + + + fprintf(fout, "%d\n", nrap); + + nrap = min(nrap, 1000); + + for(i=1; i<=nrap; ++i) + + fprintf(fout, "%d ", ap[i]); + + + + fclose(fin); + + fclose(fout); + + return 0; + +} diff --git a/strmatch/kmp/e41d9d48-6b3b-4591-bb19-1e303fa06f20.cpp b/strmatch/kmp/e41d9d48-6b3b-4591-bb19-1e303fa06f20.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a462da0e8308e08d8a25506cae6f6c39e189ed61 --- /dev/null +++ b/strmatch/kmp/e41d9d48-6b3b-4591-bb19-1e303fa06f20.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string s, t; + + + +int main() + +{ + + fin>>s>>t; + + int n=s.size(), m=t.size(); + + string p=s+'$'+t; + + int l=p.size(); + + vector pi(l+1, 0), sol; + + for(int i=1;i + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +string a,b; +int n,m,kmp[2000005]; +vector ans; +void prefix() +{ + int m=0; + for(int i=1;i>a>>b; + prefix(); + KMP(); + out< +#include + + +using namespace std; + + + +int l[2000005]; + +char a[2000005], b[2000005]; + +int sir[2000005]; + +int contor=0; + +int la, lb; + + + +int main() + +{ + + ifstream fin ("strmatch.in"); + + ofstream fout ("strmatch.out"); + + + + fin >> a >> b; + + la = strlen(a); + + lb = strlen(b); + + + + l[0] = 0; + + int i=1, j=0; + + + + while(i +#include +#include + +const int LMAX = 2000005; + +char p[LMAX], t[LMAX]; +int pl, tl; +int pref[LMAX]; + +void build_pref() { + int m = 0, i = 1; + + while (i < pl) { + if (p[i] == p[m]) { + m += 1; + pref[i] = m; + i += 1; + } else if (m != 0) { + m = pref[m - 1]; + } else { + pref[i] = 0; + i += 1; + } + } +} + +void kmp() { + std::vector matches; + + int i = 0, j = 0; + + while (j < tl) { + if (p[i] != t[j]) { + if (i == 0) + j++; + else + i = pref[i - 1]; + } else { + i++; + j++; + + if (i == pl) { + matches.push_back(j - i); + i = pref[i - 1]; + } + } + } + + printf("%ld\n", matches.size()); + for (int i = 0; i < 1000 && i < matches.size(); i++) + printf("%d ", matches[i]); +} + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + scanf("%s %s", p, t); + + pl = strlen(p); + tl = strlen(t); + + build_pref(); + kmp(); +} diff --git a/strmatch/kmp/e49b7a2a-e2da-411b-be5e-c16c469bfdc2.cpp b/strmatch/kmp/e49b7a2a-e2da-411b-be5e-c16c469bfdc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e49703c6ee57f144510d2af6bb32067f41ee1575 --- /dev/null +++ b/strmatch/kmp/e49b7a2a-e2da-411b-be5e-c16c469bfdc2.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int pi[2000005],poz[1024],m,n,cnt; + +char a[2000005],b[2000005]; + + + +void make_prefix() + +{ + + int i, q = 0; + + + + for (i = 2, pi[1] = 0; i <= m; ++i) + + { + + while (q && a[q+1] != a[i]) + + { + + q = pi[q]; + + } + + if (a[q+1] == a[i]) + + { + + ++q; + + } + + pi[i] = q; + + } + +} + +int main() + +{ + + char car; + + fin>>noskipws>>car; + + while(car!='\n') + + { + + a[++n]=car; + + fin>>noskipws>>car; + + } + + fin>>noskipws>>car; + + while(car!='\n') + + { + + b[++m]=car; + + fin>>noskipws>>car; + + } + + + + make_prefix(); + + + + int q=0; + + + + for (int i = 1; i <= m; ++i) + + { + + while (q && a[q+1] != b[i]) + + { + + q = pi[q]; + + } + + if (a[q+1] == b[i]) q++; + + + + if (q == n) + + { + + q = pi[n]; + + cnt++; + + if (cnt <= 1000) poz[cnt] = i-n; + + } + + } + + fout< +#include + + +char s[2000001],u[2000001]; + +int v[1001],i,t,k,m,n,j,p[2000001],q; + +int main() + +{ + + freopen("strmatch.in","r",stdin),freopen("strmatch.out","w",stdout),scanf("%s\n%s",s,u),m=strlen(s),n=strlen(u); + + for(i=2;i1000?1000:t);i++) + + printf("%d ",v[i]); + +} diff --git a/strmatch/kmp/e8ddf1c8-e70e-40cc-9d45-5ed2a8fe5018.cpp b/strmatch/kmp/e8ddf1c8-e70e-40cc-9d45-5ed2a8fe5018.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf8dc54b969d2daca5cc926ef64fcf829e3a5785 --- /dev/null +++ b/strmatch/kmp/e8ddf1c8-e70e-40cc-9d45-5ed2a8fe5018.cpp @@ -0,0 +1,98 @@ +#include + + +using namespace std; + + + +vector kmp(string s, string pat) { + + int m = (int) pat.size(); + + int n = (int) s.size(); + + vector ps(m, 0); + + int j = 0; + + for (int i = 1; i < m; i++) { + + while (j && pat[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[i] == pat[j]) { + + j++; + + } + + ps[i] = j; + + } + + j = 0; + + vector ret; + + for (int i = 0; i < n; i++) { + + while (j && s[i] != pat[j]) { + + j = ps[j - 1]; + + } + + if (pat[j] == s[i]) { + + j++; + + } + + if (j == m) { + + j = ps[m - 1]; + + ret.push_back(i - m + 1); + + } + + } + + return ret; + +} + + + +int main() { + + freopen ("strmatch.in", "r", stdin); + + freopen ("strmatch.out", "w", stdout); + + string pat, s; + + cin >> pat >> s; + + auto ret = kmp(s, pat); + + cout << (int) ret.size() << "\n"; + + while ((int) ret.size() > 1000) { + + ret.pop_back(); + + } + + for (auto &i : ret) { + + cout << i << " "; + + } + + cout << "\n"; + +} diff --git a/strmatch/kmp/e955fcd2-2879-4e58-a4c4-24bf28649654.cpp b/strmatch/kmp/e955fcd2-2879-4e58-a4c4-24bf28649654.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e0e063e07cb9eb9e712c4ad13efc2ad9c270ea --- /dev/null +++ b/strmatch/kmp/e955fcd2-2879-4e58-a4c4-24bf28649654.cpp @@ -0,0 +1,91 @@ +#include +#include + + +using namespace std; + + + +int pi[4000009]; + +char s[4000009],a[2000009]; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +void fpi(int n){ + + int now=0; + + for(int i=2;i<=n;i++){ + + while(s[now+1]!=s[i]&&now>0) + + now=pi[now]; + + if(s[now+1]==s[i]) + + now++; + + pi[i]=now; + + } + +} + + + +int main () { + +int l1,l2,n,nrap=0; + +cin.getline(s+1,2000010); + + + +cin.getline(a,2000009); + +l1=strlen(s+1); + +l2=strlen(a); + +strcat(s+1,"#"); + +strcat(s+1,a); + +n=l1+l2+1; + + + +fpi(n); + + + +for(int i=1;i<=n;i++) + + if(pi[i]==l1) + + nrap++; + +cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string pat, s; + +vector lps; + +void preproc() + +{ + + lps.resize(pat.size(), 0); + + for (int j = 1, len = 0; j < pat.size();) + + if (pat[j] == pat[len]) + + len++, lps[j] = len, j++; + + else if (len) + + len = lps[len - 1]; + + else + + j++; + +} + +int main() + +{ + + f >> pat >> s; + + preproc(); + + vector rez; + + for (int i = 0, j = 0; i < s.size();) + + { + + if (s[i] == pat[j]) + + i++, j++; + + if (j == pat.size()) + + rez.push_back(i - j), j = lps[j - 1]; + + else if (i < s.size() && pat[j] != s[i]) + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + g << rez.size() << '\n'; + + if (rez.size() > 1000) + + rez.resize(1000); + + for (auto i : rez) + + g << i << " "; + + return 0; + +} diff --git a/strmatch/kmp/ebb09f8d-d1a5-44ba-b619-139af7d3029c.cpp b/strmatch/kmp/ebb09f8d-d1a5-44ba-b619-139af7d3029c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b67e907cc41d9c71c4ff88a14eaad31284c96e41 --- /dev/null +++ b/strmatch/kmp/ebb09f8d-d1a5-44ba-b619-139af7d3029c.cpp @@ -0,0 +1,78 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char a[2000010],b[2000010]; + +int ans[1010],i,n,m,p[2000010],nr; + +int main() + +{ + + f>>(a+1);n=strlen(a+1); + + f>>(b+1);m=strlen(b+1); + + int k=0; + + for(i=2;i<=n;i++) + + { + + while(k!=0&&a[k+1]!=a[i])k=p[k]; + + if(a[k+1]==a[i])k++; + + p[i]=k; + + } + + k=0; + + for(i=1;i<=m;i++) + + { + + while(k!=0&&a[k+1]!=b[i])k=p[k]; + + if(a[k+1]==b[i])k++; + + if(k==n) + + { + + k=p[k]; + + nr++; + + if(nr<=1000) + + { + + ans[nr]=i-n; + + } + + } + + } + + g< + + +using namespace std; + +ifstream fin ( "strmatch.in" ); + +ofstream fout ( "strmatch.out" ); + + + +void read ( ); + +void preprocess ( ); + +void solve ( ); + +void afis ( ); + + + +string pat; + +string cuvant; + + + +vector < int > v; + +vector < int > sol; + + + +int k; + + + +int main ( ) + +{ + + read ( ); + + + + preprocess ( ); + + + + solve ( ); + + + + afis ( ); + +} + + + +void afis ( ) + +{ + + fout << k << '\n'; + + + + int lng = sol.size(); + + for ( int i = 0; i < lng; i++ ) + + fout << sol[i] << ' '; + + + + exit ( 0 ); + +} + + + +void solve ( ) + +{ + + int lng = cuvant.size(); + + int lg = pat.size(); + + + + int j = 0; + + + + for ( int i = 0; i < lng; i++ ) + + { + + if ( cuvant[i] == pat[j] ) + + { + + if ( j == lg-1 ) + + { + + k++; + + if ( k <= 1000) + + sol.push_back(i-j); + + + + j = v[j]; + + } + + + + else + + j++; + + } + + + + else + + { + + while ( j && pat[j]!= cuvant[i] ) + + j = v[j-1]; + + + + if ( pat[j] == cuvant[i] ) + + j++; + + + + } + + } + +} + + + +void preprocess ( ) + +{ + + int lng = pat.size(); + + v.reserve ( lng+1 ); + + + + v[0] = 0; + + + + int j = 0; + + + + for ( int i = 1; i < lng; i++ ) + + { + + if ( pat[j] == pat[i] ) + + { + + v[i] = j + 1; + + j++; + + } + + + + else + + { + + while ( j && pat[j] != pat[i] ) + + j = v[j-1]; + + + + if ( pat[j] == pat[i] ) + + { + + v[i] = j + 1 ; + + j++; + + } + + + + else + + v[i] = 0; + + } + + } + +} + + + +void read ( ) + +{ + + getline ( fin, pat ); + + getline ( fin, cuvant ); + +} diff --git a/strmatch/kmp/ed9f5c95-be05-4743-bb6f-ffa08101e192.cpp b/strmatch/kmp/ed9f5c95-be05-4743-bb6f-ffa08101e192.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b77f4ef3fce0257698b4ecf98e57bfc8a9df6f9 --- /dev/null +++ b/strmatch/kmp/ed9f5c95-be05-4743-bb6f-ffa08101e192.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_NO_POS = 1000; + + + +void buildSuffixTable(string & pattern, vector & suffixTable) { + + int len = 0; + + int i = 1; + + while (i < pattern.size()) { + + if (pattern[i] == pattern[len]) { + + len++; + + suffixTable[i] = len; + + i++; + + } else if (len > 0) { + + len = suffixTable[len - 1]; + + } else { + + suffixTable[i] = 0; + + i++; + + } + + } + +} + + + +void kmp(string & A, string & B, vector & suffixTable, int & n, list & ans) { + + if (A.size() > B.size()) { + + return; + + } + + int len = 0; + + int i = 0; + + while (i < B.size()) { + + if (B[i] == A[len]) { + + len++; + + if (len == A.size()) { + + n++; + + if (n <= MAX_NO_POS) { + + ans.push_back(i - len + 1); + + } + + len = suffixTable[len - 1]; + + } + + i++; + + } else if (len > 0) { + + len = suffixTable[len - 1]; + + } else { + + len = 0; + + i++; + + } + + } + +} + + + +int main() { + + ifstream fin; + + ofstream fout; + + string A, B; + + + + fin.open("strmatch.in"); + + fin >> A >> B; + + fin.close(); + + + + vector suffixTable(A.size(), 0); + + buildSuffixTable(A, suffixTable); + + + + int n = 0; + + list ans; + + kmp(A, B, suffixTable, n, ans); + + + + fout.open("strmatch.out"); + + fout << n << endl; + + for (auto it = ans.begin(); it != ans.end(); ++it) { + + fout << *it << " "; + + } + + fout.close(); + + + + ans.clear(); + + + + return 0; + +} diff --git a/strmatch/kmp/ee2c0ecc-356c-4a48-bdf8-7969f510a6ef.cpp b/strmatch/kmp/ee2c0ecc-356c-4a48-bdf8-7969f510a6ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..da2e1157d36f6232ce71e66a0aa0d4f79a8e6261 --- /dev/null +++ b/strmatch/kmp/ee2c0ecc-356c-4a48-bdf8-7969f510a6ef.cpp @@ -0,0 +1,201 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char s[2000001], p[2000001]; + + + +int sol[1001], lps[2000001], cnt, ccnt; + + + +void computeLPSArray(char* pat, int M, int* lps); + + + +void KMPSearch(char* pat, char* txt) + +{ + + int M = strlen(pat); + + int N = strlen(txt); + computeLPSArray(pat, M, lps); + + + + int i = 0; + + int j = 0; + + while (i < N) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + } + + + + if (j == M) { + + + + + + if (cnt <= 999) { + + cnt++; + + ccnt++; + + sol[cnt] = i - j; + + } + + else + + { + + ccnt++; + + } + + j = lps[j - 1]; + + } + + + + + + else if (i < N && pat[j] != txt[i]) { + + + + + + if (j != 0) + + j = lps[j - 1]; + + else + + i = i + 1; + + } + + } + +} + + + + + +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int len = 0; + + + + lps[0] = 0; + + + + + + int i = 1; + + while (i < M) { + + if (pat[i] == pat[len]) { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + + + + + + + if (len != 0) { + + len = lps[len - 1]; + + + + + + + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +int main() + +{ + + fin.getline(p, 2000001); + + fin.getline(s, 2000001); + + KMPSearch(p, s); + + fout << ccnt << '\n'; + + for (int i = 1; i <= cnt; i++) { + + fout << sol[i] << ' '; + + } + +} diff --git a/strmatch/kmp/ef3eeca3-f2d8-482f-aac3-21c70d78f70e.cpp b/strmatch/kmp/ef3eeca3-f2d8-482f-aac3-21c70d78f70e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1ab227551fc6bbd69d76de71461e46a8e52d368 --- /dev/null +++ b/strmatch/kmp/ef3eeca3-f2d8-482f-aac3-21c70d78f70e.cpp @@ -0,0 +1,62 @@ +#include + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +const int NMAX = 4000003; + +string s,s1,s2; + +int Pi[NMAX],afis[1001]; + +int i,n,now,r,q,j,cnt; + +int main() { + + in>>s1>>s2; + + s=" "+s1+"#"+s2; + + n=s.size(); + + r=0; + + for(i=2;i<=n;i++) { + + now=Pi[i-1]; + + while(now>0&&s[now+1]!=s[i]) + + now=Pi[now]; + + if(s[now+1]==s[i]) + + now++; + + Pi[i]=now; + + if(i>s1.size()+1&&Pi[i]==s1.size()){ + + cnt++; + + if(cnt<=1000) + + afis[cnt]=i-2*s1.size()-1; + + } + + } + + out< +using namespace std; +void debug_out() { cerr << endl; } +template ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;} +template ostream& operator<<(ostream& out, vector v) { return prnt(out, v); } +template ostream& operator<<(ostream& out, set v) { return prnt(out, v); } +template ostream& operator<<(ostream& out, map v) { return prnt(out, v); } +template ostream& operator<<(ostream& out, pair p) { return out << '(' << p.first << ' ' << p.second << ')'; } +template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);} +const long double PI = acos(-1); + +vector prefix(string const &A){ + vector pr(A.size()+1, 0); + int k = 0; + + for (int i=2;i<=A.size();i++){ + while (k && A[k] != A[i-1]) k = pr[k]; + if (A[k] == A[i-1]) k++; + pr[i] = k; + } + return pr; +} + +vector KMP(string const &A, string const &B){ + vector pr = prefix(B), ans; + int k = 0; + for (int i=1;i<=A.size();i++){ + while (k && B[k] != A[i-1]) k = pr[k]; + if (B[k] == A[i-1]) k++; + if (k == B.size()){ + ans.push_back(i - k); + } + } + + return ans; +} + + +int main(){ + ios::sync_with_stdio(false); + + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + string A,B; + cin >> B >> A; + vector ans = KMP(A,B); + cout << ans.size() << '\n'; + while (ans.size() > 1000) ans.pop_back(); + for (auto it : ans) cout << it << ' '; + cout << '\n'; + return 0; +} diff --git a/strmatch/kmp/f150b7d1-5bf9-442e-a3d0-599e6406468b.cpp b/strmatch/kmp/f150b7d1-5bf9-442e-a3d0-599e6406468b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8f2741f6e64639a405d6c4f72f81d80e88de8bd --- /dev/null +++ b/strmatch/kmp/f150b7d1-5bf9-442e-a3d0-599e6406468b.cpp @@ -0,0 +1,171 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +char A[2000001], B[2000001]; + + + +int lps[2000001]; + + + +void computeLPSArray(char* pat, int M, int lps[]) + +{ + + int len = 0; + + lps[0] = 0; + + + + int i = 1; + + + + while (i < M) + + { + + if (pat[i] == pat[len]) + + { + + len++; + + lps[i] = len; + + i++; + + } + + else + + { + + if (len != 0) + + { + + len = lps[len - 1]; + + } + + else + + { + + lps[i] = 0; + + i++; + + } + + } + + } + +} + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + int nr = 0; + + vector sir; + + int M = strlen(A); + + int N = strlen(B); + + computeLPSArray(A, M, lps); + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int j = 0; + + int i = 0; + + while (i < N) + + { + + if (A[j] == B[i]) + + { + + j++; + + i++; + + } + + if (j == M && j != 0) + + { + + j = lps[j - 1]; + + sir.push_back(i - M); + + nr++; + + } + + else if (i < N && A[j] != B[i]) + + { + + if (j != 0) + + j = lps[j - 1]; + + else + + i++; + + } + + } + + printf("%d\n", nr); + + for (int i = 0; i < ((sir.size() < 1000) ? sir.size() : 1000); i++) + + { + + printf("%d ", sir[i]); + + } + + return 0; + +} diff --git a/strmatch/kmp/f22ccbcd-5d92-469c-93fe-1cf8c897ffc5.cpp b/strmatch/kmp/f22ccbcd-5d92-469c-93fe-1cf8c897ffc5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04d07812b5d2fe11c8609f721e26773dd9e361d0 --- /dev/null +++ b/strmatch/kmp/f22ccbcd-5d92-469c-93fe-1cf8c897ffc5.cpp @@ -0,0 +1,122 @@ +#include +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char a[2000005]; + +char b[2000005]; + +int table[2000005]; + +vector < int > v; + +int ans; + + + +int main() + +{ + + f >> a >> b; + + int m = strlen(a); + + int n = strlen(b); + + int len = 0; + + table[0] = 0; + + for (int i=1;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 2000005; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char P[NMAX], S[NMAX]; + +int LPS[NMAX]; + +vector < int > Sol; + + + +void lps() + +{ + + LPS[0]=0; + + + + for( int i = 1; P[i]; ++i ) + + if( P[i] == P[LPS[i-1]] ) + + LPS[i] = LPS[i-1]+1; + + else + + { + + int p = LPS[i-1]; + + + + while( p!= 0 && P[i] != P[p] ) + + p = LPS[p-1]; + + + + if( P[i] == P[p] )LPS[i] = p+1; + + } + +} + +void Solve() + +{ + + fin >> P >> S; + + int np = strlen( P ), k = 0; + + lps(); + + + + + + + + for( int i = 0; S[i]; ++i ) + + { + + + + if( S[i] == P[k] ) + + { + + k++; + + if( k == np ) + + { + + Sol.push_back( i - np + 1 ); + + k = LPS[k-1]; + + } + + } + + else + + { + + if( k ) + + { + + k = LPS[k-1]; + + i--; + + } + + } + + } + + + + fout << Sol.size() << '\n'; + + + + for( int i = 0; i < Sol.size() && i < 1000; ++i ) + + fout << Sol[i] << ' '; + +} + +int main() + +{ + + Solve(); + + + + return 0; + +} diff --git a/strmatch/kmp/f2f9624e-f661-4061-8c30-1499f9a04317.cpp b/strmatch/kmp/f2f9624e-f661-4061-8c30-1499f9a04317.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fd02d9e184c30c72f15f98c78ba7cd0a0e33669 --- /dev/null +++ b/strmatch/kmp/f2f9624e-f661-4061-8c30-1499f9a04317.cpp @@ -0,0 +1,144 @@ +#include + + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[2000002], B[2000002]; + +int lps[2000002],cnt; + + + +vectorrasp; + + + +int main() + +{ + + in.getline(A,2000002); + + in.getline(B,2000002); + + int n=strlen(A), m=strlen(B); + + if(n>m) + + { + + out<<'0'; + + return 0; + + } + + lps[0]=0; + + int l=0; + + for(int i=1;i + + +using namespace std; + + + +int m,n; + +char a[2000001], b[2000001]; + +int pi[2000001], pos[1024]; + +int ct; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +void Peg(); + +void Read(); + +void Rez(); + +void Afis(); + + + +int main() + +{ + + Read(); + + Peg(); + + Rez(); + + Afis(); + + return 0; + +} + + + +void Read() + +{ + + fin.getline(a,2000001); + + fin.getline(b,2000001); + + m=strlen(a); + + n=strlen(b); + + for(int i=n;i;i--) b[i]=b[i-1]; + + for(int i=m;i;i--) a[i]=a[i-1]; + + b[0]=a[0]=' '; + +} + + + +void Peg() + +{ + + pi[1]=0; + + int q=0; + + for(int i=2;i<=m;i++) + + { + + while(q && a[q+1]!=a[i]) q=pi[q]; + + if(a[q+1]==a[i]) q++; + + pi[i]=q; + + } + +} + + + +void Rez() + +{ + + int q=0; + + for(int i=1;i<=n;i++) + + { + + while(q && a[q+1]!=b[i]) q=pi[q]; + + if(a[q+1]==b[i]) q++; + + if(q==m) + + { + + q=pi[m]; + + ct++; + + if(ct<=1000) + + pos[ct]=i-m; + + } + + } + +} + + + +void Afis() + +{ + + fout< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int m, n, k, c, urm[2000005]; + +char P[2000005], T[2000005]; + +int a[2000005]; + +void Prefix() + +{ + + int j = 0; + + for (int i = 1; i < n; ++i) + + { + + while (j > 0 && P[i] != P[j]) + + j = urm[j - 1]; + + if (P[i] == P[j]) + + j++; + + urm[i] = j; + + } + +} + + + +void KMP() + +{ + + int q = 0; + + for (int i = 0; i < m; ++i) + + { + + while (q > 0 && P[q] != T[i]) + + q = urm[q - 1]; + + if (P[q] == T[i]) ++q; + + if (q == n) + + { + + a[c++] = i - n + 1; + + } + + } + +} + + + +void Citire() + +{ + + fin.getline(P, 2000005); + + fin.getline(T, 2000005); + + n = strlen(P); + + m = strlen(T); + +} + + + +int main() + +{ + + Citire(); + + Prefix(); + + KMP(); + + fout << c << '\n'; + + if (c > 1000) + + c = 1000; + + for (int i = 0; i < c; ++i) + + { + + fout << a[i] << ' '; + + } + +} diff --git a/strmatch/kmp/f3aa33c7-e001-4ac7-8222-fdb0015e2409.cpp b/strmatch/kmp/f3aa33c7-e001-4ac7-8222-fdb0015e2409.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ccba735373f36f3c10c9eb643bf94bfc042d0160 --- /dev/null +++ b/strmatch/kmp/f3aa33c7-e001-4ac7-8222-fdb0015e2409.cpp @@ -0,0 +1,91 @@ +#include + + +using namespace std; +char sir[2000005],pat[2000005]; + +int pref[2000005],sol[2000005]; + + + +void calculare_prefix_table(int n){ + + int k=0,i; + + pref[1]=0; + + for(i=2;i<=n;i++){ + + while(k>0 && (pat[k+1]!=pat[i])) + + k=pref[k]; + + if(pat[k+1]==pat[i]) + + k++; + + pref[i]=k; + + } + +}; + + + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int n,m,i,s,k; + + cin>>pat+1; + + n=strlen(pat+1); + + cin>>sir+1; + + m=strlen(sir+1); + + calculare_prefix_table(n); + + k=0; + + s=0; + + for(i=1;i<=m;i++){ + + while(k>0 && (pat[k+1]!=sir[i])) + + k=pref[k]; + + if(pat[k+1]==sir[i]) + + k++; + + if(k==n){ + + k=pref[k]; + + s++; + + if(s<=1000) + + sol[s]=i-n; + + } + + } + + cout< + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char a[2000001],b[2000001]; + +int pi[2000001],n,m,nr,poz[2000001]; + +void make_prefix() + +{ + + int q=0; + + for (int i=2;i<=m;i++) + + { + + while (q && a[q+1]!=a[i]) + + q=pi[q]; + + if (a[q+1]==a[i]) + + q++; + + pi[i]=q; + + } + + + + + + + +} + + + +int main() + +{ + + fin>>a; + + fin>>b; + + for (;a[m];m++); + + for (;b[n];n++); + + for (int i=m;i>=0;i--) + + a[i]=a[i-1]; + + for (int i=n;i>=0;i--) + + b[i]=b[i-1]; + + a[0]=b[0]=' '; + + + + make_prefix(); + + int q=0; + + for (int i=1;i<=n;i++) + + { + + while (q && a[q+1]!=b[i]) + + q=pi[q]; + + if (a[q+1]==b[i]) + + q++; + + if (q==m) + + { + + q=pi[m]; + + nr++; + + if (nr<=1000) + + poz[nr]=i-m; + + } + + } + + fout< +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int DIM = 2e6 + 7; + + + +int N, M, P[DIM], L, Ans[DIM], sol; + +char A[DIM], B[DIM]; + +int main(){ + + + + fin.getline( A + 1, DIM - 5 ); + + fin.getline( B + 1, DIM - 5 ); + + N = strlen( A + 1 ); + + M = strlen( B + 1 ); + + + + L = 0; P[1] = 0; + + for( int i = 2; i <= N; i++ ){ + + while( L != 0 && A[i] != A[L + 1] ) + + L = P[L]; + + if( A[i] == A[L + 1] ) + + L++; + + P[i] = L; + + } + + + + L = 0; + + for( int i = 1; i <= M; i++ ){ + + while( L != 0 && B[i] != A[L + 1] ) + + L = P[L]; + + if( B[i] == A[L + 1] ) + + L++; + + if( L == N ){ + + Ans[++sol] = i - N; + + L = P[L]; + + } + + } + + + + fout << sol << "\n"; + + for( int i = 1; i <= min( sol, 1000 ); i++ ) + + fout << Ans[i] << " "; + + return 0; + +} diff --git a/strmatch/kmp/f483c618-d956-4095-a731-6060bcebbf9a.cpp b/strmatch/kmp/f483c618-d956-4095-a731-6060bcebbf9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9cc5e30be2418ef706ba59c54fb0c8cd412d3a6a --- /dev/null +++ b/strmatch/kmp/f483c618-d956-4095-a731-6060bcebbf9a.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include + + +using namespace std; + + + +int solCnt; + +int n, m; + +int pos[1005], lps[2000005]; + +char pat[2000005], txt[2000005]; +void computeLPSArray(char* pat, int M, int* lps) + +{ + + + + int cnd = 0; + + int pos = 1; + + + + lps[0] = -1; + + while (pos < M) { + + if (pat[pos] == pat[cnd]) { + + lps[pos] = lps[cnd]; + + } + + else + + { + + lps[pos] = cnd; + + cnd = lps[cnd]; + + while (cnd >= 0 && pat[pos] != pat[cnd]) + + cnd = lps[cnd]; + + } + + pos++; + + cnd++; + + } + + lps[pos] = cnd; + +} + + + +void KMPSearch(char* pat, char* txt) + +{ + + m = strlen(pat); + + n = strlen(txt); + + + + + + computeLPSArray(pat, m, lps); + + + + int i = 0; + + int j = 0; + + while (i < n) { + + if (pat[j] == txt[i]) { + + j++; + + i++; + + if (j == m) { + + if (solCnt < 1000) + + pos[solCnt++] = i - j; + + else + + solCnt++; + + j = lps[j]; + + } + + } + + else { + + j = lps[j]; + + if (j < 0) + + { + + j++; + + i++; + + } + + } + + } + +} + + + + + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", pat); + + scanf("%s", txt); + + + + KMPSearch(pat, txt); + + + + printf("%d\n", solCnt); + + for (int i = 0; i < std::min(solCnt, 1000); ++i) + + printf("%d ", pos[i]); + + return 0; + +} diff --git a/strmatch/kmp/f4970dae-cbdd-4b1e-aeb5-d5bc793bc0f0.cpp b/strmatch/kmp/f4970dae-cbdd-4b1e-aeb5-d5bc793bc0f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9ae38fc20af76087bb8111b447c6a892357cfb9 --- /dev/null +++ b/strmatch/kmp/f4970dae-cbdd-4b1e-aeb5-d5bc793bc0f0.cpp @@ -0,0 +1,88 @@ +#include +std::ifstream input (std::string("strmatch")+".in"); + +std::ofstream output(std::string("strmatch")+".out"); + + + +int N, M; + +char A[2000005], B[2000005]; + +int count; + +std::vector sol; + +void addToSol(int idx) { + + ++ count; + + if (sol.size() < 1000) sol.push_back(idx); + +} + + + +int LPS[2000005]; + +void computeLPS(char S[], int N) { + + int len = 0; + + for (int i=2; i<=N; ++i) { + + while (len != 0 && S[len+1] != S[i]) + + len = LPS[len]; + + if (S[len+1] == S[i]) ++ len; + + LPS[i] = len; + + } + +} + +void KMP() { + + computeLPS(A, N); + + int len = 0; + + for (int i=1; i<=M; ++i) { + + while (len != 0 && A[len+1] != B[i]) + + len = LPS[len]; + + if (A[len+1] == B[i]) ++ len; + + if (len == N) addToSol(i-len); + + } + +} + + + +int main() + +{ + + input >> (A+1) >> (B+1); + + N = strlen(A+1); + + M = strlen(B+1); + + KMP(); + + output << count << '\n'; + + for (auto &it:sol) output << it << ' '; + + + + return 0; + +} diff --git a/strmatch/kmp/f49b1ae3-7038-4831-bd13-e873102c28ac.cpp b/strmatch/kmp/f49b1ae3-7038-4831-bd13-e873102c28ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6d01506446ae404d4af23a643f1b14230fe3345 --- /dev/null +++ b/strmatch/kmp/f49b1ae3-7038-4831-bd13-e873102c28ac.cpp @@ -0,0 +1,48 @@ +#include + + + +using namespace std; + +const int NMAX = (int)2e6; + +int pi[NMAX], ans[1000]; + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string pattern, text; + getline(cin, pattern); + getline(cin, text); + + pi[0] = 0; + int n = pattern.size(); + for (int i = 1; i < n; ++i) { + int k = pi[i - 1]; + while (k > 0 && pattern[k] != pattern[i]) + k = pi[k - 1]; + if (pattern[k] == pattern[i]) + ++k; + pi[i] = k; + } + + int m = text.size(); + int k = 0, res = 0; + for (int i = 0; i < m; ++i) { + while (k > 0 && pattern[k] != text[i]) + k = pi[k - 1]; + if (pattern[k] == text[i]) + ++k; + if (k == n) { + if (res < 1000) ans[res] = i - n + 1; + ++res; + k = pi[k - 1]; + } + } + + cout << res << endl; + for (int i = 0; i < min(1000, res); ++i) + cout << ans[i] << ' '; + cout << endl; +} diff --git a/strmatch/kmp/f4a53b18-cdc0-48ed-9f70-d435ebbee8b9.cpp b/strmatch/kmp/f4a53b18-cdc0-48ed-9f70-d435ebbee8b9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6cc16769d0ec800f6d8e6a24f43a95d9f8f9cd3a --- /dev/null +++ b/strmatch/kmp/f4a53b18-cdc0-48ed-9f70-d435ebbee8b9.cpp @@ -0,0 +1,85 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +int n, m, i, p[2000002], L, nr, sol[1200]; + +char a[2000002], b[2000002]; + + + +int main(){ + + fin>>(a+1); + + L = 0; + + p[1] = 0; + + n = strlen(a+1); + + for(i=2;i<=n;i++){ + + while(a[L+1] != a[i] && L) + + L = p[L]; + + if(a[L+1] == a[i]) + + L++; + + p[i] = L; + + } + + fin>>(b+1); + + m = strlen(b+1); + + L = 0; + + for(i=1;i<=m;i++){ + + while(a[L+1] != b[i] && L) + + L = p[L]; + + if(a[L+1] == b[i]) + + L++; + + if(L == n){ + + nr++; + + if(nr <= 1000) + + sol[nr] = i-n; + + } + + } + + fout< 1000) + + nr = 1000; + + for(i=1;i<=nr;i++) + + fout< +#include +#include +using namespace std; + + + + + +ifstream fin("strmatch.in"); + + + +ofstream fout("strmatch.out"); + +const int DIM = 2e6 + 7; + +int p,t,i,j,lps[2000005],nrap,ap[DIM],k,lg; + +char patt[2000005],txt[2000005]; + +int main() + + + +{ + + + + + + fin.getline( patt + 1, DIM - 5 ); + + fin.getline( txt + 1, DIM - 5 ); + + p = strlen( patt + 1 ); + + t = strlen( txt + 1 ); + + + + + + + + lps[0] = -1;lps[1]=0; + + lg=0; + + + + for(i=2; i<=p; ++i){ + + while(lg>0 and patt[i]!=patt[lg+1]) + + lg=lps[lg]; + + if(patt[i]==patt[lg+1]) + + lg++; + + lps[i]=lg; + + } + + + + i = 1; j = 1;lg=0; + + + + while(i<=t ) + + { + + while(lg>0 and txt[i]!=patt[lg+1]) + + lg=lps[lg]; + + if(txt[i]==patt[lg+1]) + + lg++; + + if(lg==p) + + { + + ap[++nrap]=i-p; + + lg=lps[p]; + + } + + i++; + + } + + + + + + fout<1000) nrap=1000; + + for(i=1; i<=nrap; ++i) + + fout< + + +using namespace std; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + + + string pattern , text; + + fin >> pattern >> text; + + + + vector PI(pattern.size()); + + + + for(int k = 0, i = 1; i < (int)pattern.size(); ++i) + + { + + while(k > 0 && pattern[i] != pattern[k]) k = PI[k - 1]; + + if(pattern[i] == pattern[k]) ++k; + + PI[i] = k; + + } + + + + vector answer; + + + + for(int k = 0, i = 0; i < (int)text.size(); ++i) + + { + + while(k > 0 && text[i] != pattern[k]) k = PI[k - 1]; + + if(text[i] == pattern[k]) ++k; + + + + if(k == (int)pattern.size()) + + { + + answer.push_back(i - k + 1); + + } + + } + + + + fout << answer.size() << '\n'; + + + + for(int i = 0; i < min(1000, (int)answer.size()); ++i) fout << answer[i] << ' '; + +} diff --git a/strmatch/kmp/f6b09689-1066-4d69-9256-1b27fd5afe5c.cpp b/strmatch/kmp/f6b09689-1066-4d69-9256-1b27fd5afe5c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e1190ae305006f94a7377c624e515e711a36841 --- /dev/null +++ b/strmatch/kmp/f6b09689-1066-4d69-9256-1b27fd5afe5c.cpp @@ -0,0 +1,98 @@ +#include +#include +#include + + +using namespace std; + + + +int main() + +{ + + ifstream f("strmatch.in"); + + ofstream g("strmatch.out"); + + char A[2000000], ch; + + int N, A_index = -1, B_index = -1, CONTOR = 0; + + vector prefix, SOL; + + + + f.getline(A, 2000001); + + N = strlen(A); + + + + prefix.push_back(-1); + + int j = -1; + + for(int i = 1; i < N; i++) + + { + + while(j > -1 && A[j + 1] != A[i]) + + j = prefix[j]; + + if(A[j + 1] == A[i]) + + j++; + + prefix.push_back(j); + + } + + + + while(f >> ch) + + { + + B_index++; + + while(A_index > -1 && A[A_index + 1] != ch) + + A_index = prefix[A_index]; + + if(A[A_index + 1] == ch) + + A_index++; + + if(A_index == N - 1) + + { + + if(CONTOR < 1000) + + SOL.push_back(B_index - N + 1); + + CONTOR++; + + A_index = prefix[A_index]; + + } + + } + + + + g << CONTOR << '\n'; + + if(CONTOR > 1000) + + CONTOR = 1000; + + for(int i = 0; i < CONTOR; i++) + + g << SOL[i] << ' '; + + return 0; + +} diff --git a/strmatch/kmp/f7334ee8-4ed2-43e9-a582-391d510cc700.cpp b/strmatch/kmp/f7334ee8-4ed2-43e9-a582-391d510cc700.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99e8df6f19f2106cc80f03a7a0ede05c58e47eba --- /dev/null +++ b/strmatch/kmp/f7334ee8-4ed2-43e9-a582-391d510cc700.cpp @@ -0,0 +1,59 @@ +#include + +const int LMAX = 2000005; + +char p[LMAX], t[LMAX]; +int lenp, lent; +int lps[LMAX]; + +void read() { + scanf("%s %s", p, t); + + lenp = strlen(p); + lent = strlen(t); +} + +void build_lps() { + int j = 0; + + for (int i = 1; i < lenp; ++i) { + while (j > 0 && p[i] != p[j]) + j = lps[j - 1]; + + if (p[i] == p[j]) + ++j; + + lps[i] = j; + } +} + +void kmp() { + std::vector matches; + + int j = 0; + + for (int i = 0; i < lent; ++i) { + while (j > 0 && t[i] != p[j]) + j = lps[j - 1]; + + if (t[i] == p[j]) + ++j; + + if (j == lenp) + matches.push_back(i - j + 1); + } + + printf("%ld\n", matches.size()); + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + printf("%d ", matches[i]); +} + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + read(); + build_lps(); + kmp(); +} diff --git a/strmatch/kmp/f73c1dda-ff0e-4fb0-820a-0ae7759ea3a5.cpp b/strmatch/kmp/f73c1dda-ff0e-4fb0-820a-0ae7759ea3a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f9806f43bb6aa6fb4b8503f88de455816c79e94 --- /dev/null +++ b/strmatch/kmp/f73c1dda-ff0e-4fb0-820a-0ae7759ea3a5.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +int pr[4000003]; + +int main() +{ + ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + string s; + cin>>s; + int a=s.size(); + string aux(1,'#'); + s+='#'; + char c; + while(cin>>c) + { + s+=c; + } + int ans=0; + vectorv; + pr[0]=-1; + for(int i=1;i<=s.size();i++) + { + int k=pr[i-1]; + while(k>=0) + { + if(s[k]==s[i-1]) + { + pr[i]=k+1; + if(pr[i]==a) ans++,v.push_back(i-a*2-1); + break; + } + k=pr[k]; + } + } + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int INF = 2e9; + +const int N = 2e6; + + + +char A[5 + N], B[5 + N], s[5 + 2 * N]; + +int pi[5 + 2 * N]; + + + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + int lenA, lenB, cnt(0); + + fgets(A, 3 + N, stdin); + + fgets(B, 3 + N, stdin); + + + + lenA = strlen(A) - 1; + + lenB = strlen(B) - 1; + + for(int i = 0; i < lenA; i++) + + s[i] = A[i]; + + s[lenA] = '#'; + + for(int i = 0; i < lenB; i++) + + s[i + lenA + 1] = B[i]; + + + + for(int i = 1; i < lenA + lenB + 1; i++) { + + int k = pi[i - 1]; + + while(k > 0 && s[i] != s[k]) + + k = pi[k - 1]; + + + + if(s[i] == s[k]) k++; + + pi[i] = k; + + } + + + + for(int i = 0; i < lenA + lenB + 1; i++) { + + if(pi[i] == lenA) { + + if(sol.size() < 1000) { + + sol.push_back(i - lenA + 1); + + + + } + + cnt++; + + } + + } + + + + printf("%d\n", cnt); + + for(auto it : sol) printf("%d ", it - lenA - 1); + + return 0; + +} diff --git a/strmatch/kmp/f8850648-96d5-43d2-815e-e03a1aef016b.cpp b/strmatch/kmp/f8850648-96d5-43d2-815e-e03a1aef016b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..449f94109e567ce5212116fd1792aa3b27fb82b1 --- /dev/null +++ b/strmatch/kmp/f8850648-96d5-43d2-815e-e03a1aef016b.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +char a[2000002], b[2000002]; + +int p[2000002], afis[2000002]; + +int main() + +{ + + int n, m, i, j, nr_afis=0; + + fin.getline(a, 2000002); n=strlen(a); + + fin.getline(b, 2000002); m=strlen(b); + + j=0; p[1]=0; + + for(i=1; i +#include +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); + +int main() { + string s, t; + fin >> s >> t; + string aux = s + '#' + t; + vector pi(aux.size()); + int k = 0; + for (int i = 1; i < aux.size(); ++i) { + while (k && aux[i] != aux[k]) k = pi[k - 1]; + if (aux[i] == aux[k]) k++; + pi[i] = k; + } + int cnt = 0; + vector ans; + for (int i = s.size() + 1; i < aux.size(); ++i) + if (pi[i] == s.size() && ++cnt <= 1000) ans.push_back(i - 2 * s.size()); + fout << cnt << '\n'; + for (int pos : ans) fout << pos << ' '; + return 0; +} diff --git a/strmatch/kmp/f9cc9287-e14a-4a9c-8049-8b157f8910a5.cpp b/strmatch/kmp/f9cc9287-e14a-4a9c-8049-8b157f8910a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..748401b98de7637e874d4a3ee683b0967cdad224 --- /dev/null +++ b/strmatch/kmp/f9cc9287-e14a-4a9c-8049-8b157f8910a5.cpp @@ -0,0 +1,108 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +char T[2000005],P[2000005]; + +int a[2000005]; + +int urm[2000005]; + +int n,m,k; + + + +void citire() + +{fin>>P>>T; + + n=strlen(P); + + m=strlen(T); + +} + + + + + +void prefix() + +{int j=0; + + for(int i=1;i0 && P[i]!=P[j]) + + j=urm[j-1]; + + if(P[i]==P[j]) + + j++; + + urm[i]=j; + + } + +} + + + +void kmp() + +{int q=0; + + for(int i=0;i0 && P[q]!=T[i]) + + q=urm[q-1]; + + if(T[i]==P[q]) + + q++; + + if(q==n) + + a[k++]=i-n+1; + + } + + + +} + + + + + +int main() + +{citire(); + + prefix(); + + kmp(); + + fout<1000) k=1000; + + for(int i=0;i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[2000002], B[2000002]; + +char C[2000002 +2000002]; + +int Z[2000002 +2000002],cnt; + + + +int main() + +{ + + in.getline(A,2000002); + + in.getline(B,2000002); + + int n=strlen(A),m=strlen(B); + + if(n>m) + + { + + out<<"0"; + + return 0; + + } + + strcpy(C,A); + + strcat(C,"+"); + + strcat(C,B); + + int L=0,R=0; + + for(int i=1;iR) + + { + + L=i; + + R=i; + + while(RZ[k]) + + Z[i]=Z[k]; + + else + + { + + L=i; + + while(R +#include +#include +#include + + +using namespace std; + + + +const int MAX_LENGTH = 2000000; + + + +char a[MAX_LENGTH + 1]; + +char b[MAX_LENGTH + 1]; + +char united[2 * MAX_LENGTH + 1]; + +int pi[2 * MAX_LENGTH]; + + + +void calc_pi(char s[]) { + + int n; + + + + n = strlen(s); + + pi[0] = 0; + + for (int i = 1; i < n; i++) { + + int index = i - 1; + + + + while (s[i] != s[pi[index]] && index > 0) + + index = pi[index] - 1; + + + + if (index >= 0 && s[i] == s[pi[index]]) + + pi[i] = pi[index] + 1; + + else + + pi[i] = 0; + + } + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s\n%s", a, b); + + + + int n1, n2, n; + + + + n1 = strlen(a); + + n2 = strlen(b); + + n = n1 + 1 + n2; + + + + for (int i = 0; i < n1; i++) + + united[i] = a[i]; + + + + united[n1] = '*'; + + + + for (int i = 0; i < n2; i++) + + united[n1 + 1 + i] = b[i]; + + + + + + calc_pi(united); + + + + vector sol; + + for (int i = 2 * n1; i < n; i++) { + + if (pi[i] == n1) + + sol.push_back(i - 2 * n1); + + } + + + + printf("%d\n", sol.size()); + + for (int i = 0; i < min(1000, (int)sol.size()); i++) + + printf("%d ", sol[i]); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/kmp/fc05813f-79ba-4d13-9698-febf318e24a6.cpp b/strmatch/kmp/fc05813f-79ba-4d13-9698-febf318e24a6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c5dbbb2c003f46a6933ab80c826ad8385550e5b3 --- /dev/null +++ b/strmatch/kmp/fc05813f-79ba-4d13-9698-febf318e24a6.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s1[2000002],s2[2000002]; + +int lps[2000002],sol[2000002]; + +int main() + +{ + + int n1,n2,i,x,k=0; + + f.getline(s1+1,2000001); + + f.getline(s2+1,2000001); + + n1=strlen(s1+1); + + n2=strlen(s2+1); + + for(i=2;i<=n1;i++) + + { + + x=lps[i-1]; + + while(x>0&&s1[x+1]!=s1[i]) + + x=lps[x]; + + if(s1[x+1]==s1[i]) + + x++; + + lps[i]=x; + + } + + x=0; + + for(i=1;i<=n2;i++) + + { + + while(x>0&&s1[x+1]!=s2[i]) + + x=lps[x]; + + if(s1[x+1]==s2[i]) + + x++; + + if(x==n1) + + sol[++k]=i-n1; + + } + + g< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int nmax = 2000005; + + + +string p, t; + + + +int lps[nmax]; + + + +void CreateLPS() + +{ + + int len = 0, k = 1, s = p.length(); + + + + while (k < s) + + { + + if (p[k] == p[len]) + + lps[k++] = ++len; + + else + + { + + if (len) + + len = lps[len - 1]; + + else + + k++; + + } + + } + +} + + + + + +int main() + +{ + + fin >> p >> t; + + + + fout << " \n"; + + + + CreateLPS(); + + + + int i = 0, j = 0, s = t.length(), c = 0; + + + + + + while (i < s) + + { + + if (p[j] == t[i]) + + { + + i++; + + j++; + + } + + else + + { + + if (j) + + j = lps[j - 1]; + + else + + i++; + + } + + + + if (j == p.length()) + + { + + if (c < 1000) + + fout << i - j << ' '; + + + + c++; + + } + + } + + + + fout.close(); + + fout.open("strmatch.out", ios::in|ios::out); + + + + fout << c; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/kmp/fd9da47f-0dc3-480b-aa4f-bfa959cdde92.cpp b/strmatch/kmp/fd9da47f-0dc3-480b-aa4f-bfa959cdde92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..216eb099f22e94934f9d3e26436449558689e5e6 --- /dev/null +++ b/strmatch/kmp/fd9da47f-0dc3-480b-aa4f-bfa959cdde92.cpp @@ -0,0 +1,100 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char a[2000004],b[2000004]; + +int pi[2000004],v[1004],nr,aa,bb; + + + +void kmp1() + +{ + + int k=0; + + for(int i=1;i<=bb;++i) + + { + + while(k && a[k+1]!=b[i]) + + k=pi[k]; + + if(a[k+1]==b[i]) ++k; + + if(k==aa) + + { + + ++nr; + + if(nr<=1000) + + v[nr]=i; + + } + + } + +} + +void kmp() + +{ + + int k=0; + + for(int i=2;i<=aa;++i) + + { + + while(k && a[k+1]!=a[i]) + + k=pi[k]; + + if(a[k+1]==a[i])++k; + + pi[i]=k; + + } + +} + +int main() + +{ + + f>>(a+1); + + f>>(b+1); + + aa=strlen(a+1); + + bb=strlen(b+1); + + kmp(); + + kmp1(); + + g<1000) nr=1000; + + for(int i=1;i<=nr;++i) + + g< +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +ifstream fin ("strmatch.in"); +ofstream fout ("strmatch.out"); + +const int Nmax = 2000666; + + + + + + + +int pi[Nmax]; +int N, M; +string A,B; + +void build_prefix() { + pi[0] = -1; + int k = -1; + + for(uint i = 1; i < A.size(); i++) { + while(k > -1 && A[i] != A[k+1]) { k = pi[k]; } + + if (A[i] == A[k+1]) { k++; } + + pi[i] = k; + } +} + +int main(void) { + fin >> A >> B; + N = A.size(); + M = B.size(); + if (N > M) { + fout << "0\n"; + return 0; + } + + build_prefix(); + int k = -1; + int count = 0; + vi res; + for(int i = 0; i < M; i++) { + while(k > -1 && B[i] != A[k+1]) { k = pi[k]; } + if (B[i] == A[k+1]) { k++; } + if (k == N-1) { + count++; + if (count <= 1000) { + res.push_back(i-(N-1)); + } + k = pi[k]; + } + } + + fout << count << '\n'; + for(auto pos: res) { + fout << pos << ' '; + } + fout << endl; + + return 0; +} diff --git a/strmatch/rk/0082ebea-3931-47b2-b916-ee40670b305d.cpp b/strmatch/rk/0082ebea-3931-47b2-b916-ee40670b305d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..896d93b0e6372972ac7521c98f439f0c68019151 --- /dev/null +++ b/strmatch/rk/0082ebea-3931-47b2-b916-ee40670b305d.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 6; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/008a1159-0d2f-49fd-9ee5-2f17e7d4069e.cpp b/strmatch/rk/008a1159-0d2f-49fd-9ee5-2f17e7d4069e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb0e543ff78afa88cd1f2736a5b189c842a857c4 --- /dev/null +++ b/strmatch/rk/008a1159-0d2f-49fd-9ee5-2f17e7d4069e.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int i, nr=0, H, H2, has, has2, v[1005]; + +char m[2000003], n[2000004]; + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + cin>>m>>n; + + int l1= strlen(m); + + int l2= strlen(n); + + for(int i=0; i +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n,m,nr1,nr2,h1,h2,p1,p2,nrap; + +bool fr[2000005]; + +string s,t; + + + +void Read(); + +void RabinKarp(); + + + +int main() + +{ Read(); + + RabinKarp(); + + return 0; + +} + + + +void Read() + +{ f>>s>>t; + + n=s.size(); + + m=t.size(); + + p1=p2=1; + + for(int i=0; im) + + { g<<0; + + return; + + } + + for(int i=0; i +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +vectorpoz; + +const long long MOD=1e9+7; + +const long long p1=103; + +long long cc(char c) + +{ + + if(c>='a'&&c<='z') + + return c-'a'+1; + + if(c>='A'&&c<='Z') + + return c-'A'+27; + + return c-'0'+53; + +} + +long long hsh(string s) + +{ + + long long mod=0,val=1; + + for(long long poz=(long long)s.size()-1;poz>=0;poz--) + + { + + mod=(mod+1LL*val*cc(s[poz]))%MOD; + + val=(1LL*val*p1)%MOD; + + } + + return mod; + +} + +int main() + +{ + + string a,b; + + fin>>a>>b; + + long long nr=0,hsa=hsh(a); + + string s=b.substr(0,a.size()); + + long long hsb=hsh(s); + + long long put=1; + + for(long long i=1;i<=(long long)a.size();i++) + + { + + put=(1LL*put*p1)%MOD; + + + + } + + + + for(long long i=a.size();i<(long long)b.size();i++) + + { + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(i); + + } + + + + hsb=(1LL*hsb*p1+cc(b[i]))%MOD; + + hsb-=(1LL*put*(cc(b[i-a.size()])))%MOD; + + if(hsb<0) + + hsb+=MOD; + + } + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(b.size()); + + } + + fout< +#include + + + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +const long long lim=2e6+3; + +vector sol; + +int main() + +{ + + string a,b; + + long long na,nb,ha=0,hb=0,cnt=0,p=1; + + cin>>a>>b; + + na=a.size(),nb=b.size(); + + if(na>nb) + + { + + cout<<0; + + return 0; + + } + + for(long long i=0;i +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int hash1a,hash2a,hash1b,hash2b; + +char a[2000007],b[2000007]; + +vector v; + +int put1,put2; + +int main() + +{int lg1,i,lg2; + + fin.getline(a,2000007); + + fin.getline(b,2000007); + + lg1=strlen(a); + + lg2=strlen(b); + + put1=put2=1; + + if(lg1>lg2) + + {fout<<0;return 0;} + + for(i=0;i +std::pair hash(const std::string &str) { + + std::pair ans(0, 0); + + int p1 = 1, p2 = 1; + + for (int i=str.size()-1; i>=0; --i) + + ans.first = (ans.first + str[i]*p1)%100021, p1 = (p1*73)%100021, + + ans.second = (ans.second + str[i]*p2)%100007, p2 = (p2*73)%100007; + + return ans; + +} + + + +std::string t, s; + +std::pair hash_t; + + + +std::ifstream input ("strmatch.in"); + +std::ofstream output("strmatch.out"); + + + +int main() + +{ + + input >> t >> s; + + int len_t = t.size(); + + hash_t = hash(t); + + + + if (t.size() > s.size()) { + + output << 0; + + return 0; + + } + + + + int pp1 = 1, pp2 = 1; + + for (int i=0; i sol; + + std::pair val = hash(s.substr(0, len_t)); + + if (val == hash_t) sol.push_back(0); + + for (int i=len_t; i + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int Nmax = 2e6 + 5; + +const int p = 73; + +const int Mod1 = 1e5 + 21; + +const int Mod2 = 1e5 + 7; + + + +bitset viz; + +char a[Nmax], b[Nmax]; + + + +int main() + +{ + + fin >> a; + + fin >> b; + + + + int n, m, contor = 0; + + n = strlen(a); + + m = strlen(b); + + + + int hash_a1 = 0, hash_a2 = 0, pLg1 = 1, pLg2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + hash_a1 = (hash_a1 * p + a[i]) % Mod1; + + hash_a2 = (hash_a2 * p + a[i]) % Mod2; + + if(i != 0) + + { + + pLg1 = pLg1 * p % Mod1; + + pLg2 = pLg2 * p % Mod2; + + } + + } + + + + int hash_b1 = 0, hash_b2 = 0; + + + + for(int i = 0; i < n; i++) + + { + + hash_b1 = (hash_b1 * p + b[i]) % Mod1; + + hash_b2 = (hash_b2 * p + b[i]) % Mod2; + + } + + + + if(hash_a1 == hash_b1 && hash_a2 == hash_b2) + + { + + contor ++; + + viz[0] = 1; + + } + + + + for(int i = n; i < m; i++) + + { + + + + hash_b1 = ((hash_b1 - (pLg1 * b[i - n]) % Mod1 + Mod1) * p + b[i]) % Mod1; + + hash_b2 = ((hash_b2 - (pLg2 * b[i - n]) % Mod2 + Mod2) * p + b[i]) % Mod2; + + + + if(hash_a1 == hash_b1 && hash_a2 == hash_b2) + + { + + contor++; + + viz[i - n + 1] = 1; + + } + + } + + + + fout << contor << "\n"; + + + + contor = 0; + + for(int i = 0; i < m && contor < 1000; i++) + + if(viz[i]) + + { + + fout << i << " "; + + contor++; + + } + + + + fout << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/rk/0a16a3a1-cfd1-4264-ac5f-9ffdb301f945.cpp b/strmatch/rk/0a16a3a1-cfd1-4264-ac5f-9ffdb301f945.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7d460935b38d79f3af4c7dfb8eaded320fbfb31 --- /dev/null +++ b/strmatch/rk/0a16a3a1-cfd1-4264-ac5f-9ffdb301f945.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/0a1bb64e-b90d-46b8-9afb-7c42538080ce.cpp b/strmatch/rk/0a1bb64e-b90d-46b8-9afb-7c42538080ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1721ad3b4d428822de4943742699c5ea636c813c --- /dev/null +++ b/strmatch/rk/0a1bb64e-b90d-46b8-9afb-7c42538080ce.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/0ae17488-f96d-4cbc-88da-60c7dc65580d.cpp b/strmatch/rk/0ae17488-f96d-4cbc-88da-60c7dc65580d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..630179dceb1eccebd5b9abc6a88289870a222ddd --- /dev/null +++ b/strmatch/rk/0ae17488-f96d-4cbc-88da-60c7dc65580d.cpp @@ -0,0 +1,114 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +int val1,val2,rhash1,rhash2,lgt,lgp,putere1=1,putere2=1; + +char A[2000000 +1],B[2000000 +1]; + +vector V; + + + +void Rabin_Karp(char T[], char P[]) + +{ + + lgt=strlen(T); + + lgp=strlen(P); + + for(int i=1; i<=lgp-1; i++) + + { + + putere1*=75; + + putere2*=75; + + putere1%=100007; + + putere2%=100021; + + } + + for(int i=0; i>A; + + fi>>B; + + Rabin_Karp(B,A); + + afisare(); + + fi.close(); + + fo.close(); + + return 0; + +} diff --git a/strmatch/rk/0b416c50-58f5-44a1-a2b9-0b8bbf2e62e9.cpp b/strmatch/rk/0b416c50-58f5-44a1-a2b9-0b8bbf2e62e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fe40b342aa17f352c4874f2c9bb36d2525871b8 --- /dev/null +++ b/strmatch/rk/0b416c50-58f5-44a1-a2b9-0b8bbf2e62e9.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + char a[2000005], b[2000005]; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + ( a[i] - '0' + 1 ) ) % 45007; + + hash2a = ( hash2a * 80 + ( a[i] - '0' + 1 ) ) % 45013; + + + + hash1 = ( hash1 * 80 + ( b[i] - '0' + 1 ) ) % 45007; + + hash2 = ( hash2 * 80 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 ) % 45007 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 ) % 45013 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/0dbdaa36-fb57-4986-a607-cf89390c8880.cpp b/strmatch/rk/0dbdaa36-fb57-4986-a607-cf89390c8880.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9587163d4ce727c5b22392d7e7e7590e1497b308 --- /dev/null +++ b/strmatch/rk/0dbdaa36-fb57-4986-a607-cf89390c8880.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream si("strmatch.in"); + +ofstream so("strmatch.out"); + + + +string s,s2; + +int n,b=101,m1=100007,m2=100021,rs,h1,h2,m,i,hash1,hash2,p1,p2,v[1001]; + + + +int main() + +{ + + si>>s>>s2; + + n=s.length(); + + m=s2.length(); + + p1=1; + + p2=1; + + for(i=0; im) + + { + + so<<0<<'\n'; + + return 0; + + } + + for(i=0; i +std::pair hash(const std::string &str) { + + std::pair ans(0, 0); + + int p1 = 1, p2 = 1; + + for (int i=str.size()-1; i>=0; --i) + + ans.first = (ans.first + str[i]*p1)%100021, p1 = (p1*73)%100021, + + ans.second = (ans.second + str[i]*p2)%100007, p2 = (p2*73)%100007; + + return ans; + +} + + + +std::string t, s; + +std::pair hash_t; + + + +std::ifstream input ("strmatch.in"); + +std::ofstream output("strmatch.out"); + + + +int main() + +{ + + input >> t >> s; + + int len_t = t.size(); + + hash_t = hash(t); + + + + if (t.size() > s.size()) { + + output << 0; + + return 0; + + } + + + + int pp1 = 1, pp2 = 1; + + for (int i=0; i sol; + + std::pair val = hash(s.substr(0, len_t)); + + if (val == hash_t) sol.push_back(0); + + for (int i=len_t; i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Mod1 = 1e9+7; + +const int Mod2 = 1e9+9; + +const int p = 131; + +int main() + +{ + + string a,b; + + getline(f,a); + + getline(f,b); + + long long hi1=a[0]-'0',hi2=a[0]-'0',pt1=1,pt2=1,h1=0,h2=0; + + for(int i=1; i rez; + + if(h1==hi1 && h2==hi2) + + { + + rez.push_back(0); + + } + + for(int i=a.size(); i +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + +char a[2000005], b[2000005]; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + a[i] - '0' + 1 ) % 45007; + + hash2a = ( hash2a * 80 + a[i] - '0' + 1 ) % 45013; + + + + hash1 = ( hash1 * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( hash2 * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/0f0cd826-83c7-44de-a8bb-345f41d10524.cpp b/strmatch/rk/0f0cd826-83c7-44de-a8bb-345f41d10524.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9678ac342f41e52cac56881b406e120d9b70a355 --- /dev/null +++ b/strmatch/rk/0f0cd826-83c7-44de-a8bb-345f41d10524.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +const unsigned long long baza = 1010012383; + +const unsigned long long m = 1010012359; + +int v[ 1005 ], poz; + +unsigned long long sol; + +string a, b; + + + +int main() + +{ + + ifstream fin( "strmatch.in" ); + + fin >> a >> b; + + fin.close(); + + if( a.size() > b.size() ){ + + FILE *fout = fopen( "strmatch.out", "w" ); + + fprintf( fout, "0\n" ); + + fclose( fout ); + + } else { + + unsigned long long vala = 0; + + unsigned long long valb = 0; + + unsigned long long put = 1, i; + + for( i = 0; i < a.size(); i++ ){ + + vala = ( vala * baza + a[ i ] ) % m; + + put = ( put * baza ) % m; + + } + + for( i = 0; i < a.size(); i++ ) + + valb = ( valb * baza + b[ i ] ) % m; + + if( vala == valb ){ + + ++sol; + + v[ poz++ ] = 0; + + } + + for( i = 1; i <= b.size() - a.size(); i++ ){ + + valb = ( valb * baza + b[ i + a.size() - 1 ] ) % m; + + unsigned long long ceva = ( put * b[ i - 1 ] ) % m; + + valb = ( valb - ceva + m ) % m; + + if( vala == valb ){ + + ++sol; + + if( poz < 1000 ) + + v[ poz++ ] = i; + + } + + } + + FILE *fout = fopen( "strmatch.out", "w" ); + + fprintf( fout, "%lld\n", sol ); + + for( i = 0; i < poz; i++ ) + + fprintf( fout, "%lld ", v[ i ] ); + + fprintf( fout, "\n" ); + + fclose( fout ); + + } + + return 0; + +} diff --git a/strmatch/rk/1067890d-5c90-4b73-a1c7-393df29fba9e.cpp b/strmatch/rk/1067890d-5c90-4b73-a1c7-393df29fba9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0deb34c6ebdad6737790af11716601f6598f7d30 --- /dev/null +++ b/strmatch/rk/1067890d-5c90-4b73-a1c7-393df29fba9e.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +#include +#include + + + + +const int MOD1 = 1e9 + 7; + +const int MOD2 = 666013; + +const int baza = 74; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +long long amod1,amod2,bmod1,bmod2; + +int n,m; + +long long pown_MOD1,pown_MOD2; + +int ans[2000005]; + + + +int asci(char c){ + + return (int)c + 1; + +} + +string str(int nr){ + + string ans; + + while(nr){ + + ans += (nr % baza + 'A' - 1); + + nr /= baza; + + } + + reverse(ans.begin(),ans.end()); + + return ans; + +} + + + +int main() + +{ + + string a,b; + + in>>a>>b; + + if(a.size() > b.size()) + + swap(a,b); + + int n = a.size(); + + int m = b.size(); + + pown_MOD1 = pown_MOD2 = 1; + + for(int i = 1; i <= n - 1; i++){ + + pown_MOD1 = pown_MOD1 * baza % MOD1; + + pown_MOD2 = pown_MOD2 * baza % MOD2; + + } + + for(int i = 0; i < n; i++){ + + amod1 = (amod1 * baza + asci(a[i])) % MOD1; + + amod2 = (amod2 * baza + asci(a[i])) % MOD2; + + } + + for(int i = 0; i < n; i++){ + + bmod1 = (bmod1 * baza + asci(b[i])) % MOD1; + + bmod2 = (bmod2 * baza + asci(b[i])) % MOD2; + + } + + int index = 0; + + if(amod1 == bmod1 && amod2 == bmod2) + + ans[++index] = 0; + + for(int i = n; i < m; ++i){ + + bmod1 = ((bmod1 - asci(b[i - n]) * pown_MOD1) * baza + asci(b[i])) % MOD1; + + bmod1 = (bmod1 + MOD1) % MOD1; + + bmod2 = ((bmod2 - asci(b[i - n]) * pown_MOD2) * baza + asci(b[i])) % MOD2; + + bmod2 = (bmod2 + MOD2) % MOD2; + + if(amod1 == bmod1 && amod2 == bmod2) + + ans[++index] = i - n + 1; + + } + + + + out< +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/12f8ec01-55ba-41ee-a741-8ba15ffa21a2.cpp b/strmatch/rk/12f8ec01-55ba-41ee-a741-8ba15ffa21a2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c975214d9827df1821f5586466a3a721fab488a8 --- /dev/null +++ b/strmatch/rk/12f8ec01-55ba-41ee-a741-8ba15ffa21a2.cpp @@ -0,0 +1,127 @@ +#include +using namespace std; +ifstream fin("strmatch.in"); ofstream fout("strmatch.out"); + + + + + + + +long long n ,m; + +string a, b; + +long long h1=0, h2=0, hb=0; + +long long ord[300]; + +vector pos; + + + + + +int32_t main(){ + +ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +getline(fin, b); + +getline(fin, a); + +n=a.length(); m=b.length(); + +if(n +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +const int MOD = 1e9; + +const int BASE = 997; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = " " + b; + + int n = a.size() - 1, m = b.size() - 1, needle_hash = 0; + + for(int i = 1; i <= n; i ++) + + needle_hash = (1LL * needle_hash * BASE + 1LL * (a[i] - '0')) % MOD; + + + + vector p(m + 1, 1); + + for(int i = 1; i <= m; i ++) + + p[i] = (1LL * BASE * p[i - 1]) % MOD; + + + + vector hs(m + 1, 0); + + int cnt = 0; + + vector ans; + + for(int i = 1; i <= m; i ++) { + + hs[i] = (1LL * hs[i - 1] * BASE + 1LL * (b[i] - '0')) % MOD; + + if(i < n) + + continue; + + int pretender = (MOD + hs[i] - ((1LL * hs[i - n] * p[n]) % MOD)) % MOD; + + if(pretender == needle_hash) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/rk/13a6426d-7839-40c8-8fa1-9f2e8251cd04.cpp b/strmatch/rk/13a6426d-7839-40c8-8fa1-9f2e8251cd04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be32cbb96cff1319be7e8b1004d5e07f05aef2bc --- /dev/null +++ b/strmatch/rk/13a6426d-7839-40c8-8fa1-9f2e8251cd04.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; + +const int P = 191; + +const int MOD1 = 10000007; + +const int MOD2 = 10000021; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +string a, b; + +int P1 = 1, P2 = 1, hash_a1, hash_a2, hash_b1, hash_b2, cnt, len_a, len_b; + +vector sol; + + + +int main() { + + fin >> a >> b; + + + + len_a = a.length(); + + len_b = b.length(); + + + + if (len_a > len_b) { + + fout << 0; + + return 0; + + } + + + + for (int i = 0; i < len_a; i++) { + + hash_a1 = (hash_a1 * P + a[i] - '0' + 1) % MOD1; + + hash_a2 = (hash_a2 * P + a[i] - '0' + 1) % MOD2; + + if (i) { + + P1 = P1 * P % MOD1; + + P2 = P2 * P % MOD2; + + } + + } + + + + for (int i = 0; i < len_a; i++) { + + hash_b1 = (hash_b1 * P + b[i] - '0' + 1) % MOD1; + + hash_b2 = (hash_b2 * P + b[i] - '0' + 1) % MOD2; + + } + + + + if (hash_a1 == hash_b1 && hash_a2 == hash_b2) { + + cnt++; + + sol.push_back(0); + + } + + + + for (int i = len_a; i < len_b; i++) { + + hash_b1 = ((hash_b1 - ((b[i - len_a] - '0' + 1) * P1 % MOD1) + MOD1) % MOD1 * P + b[i] - '0' + 1) % MOD1; + + hash_b2 = ((hash_b2 - ((b[i - len_a] - '0' + 1) * P2 % MOD2) + MOD2) % MOD2 * P + b[i] - '0' + 1) % MOD2; + + + + if (hash_a1 == hash_b1 && hash_a2 == hash_b2) { + + cnt++; + + if (cnt <= 1000) + + sol.push_back(i - len_a + 1); + + } + + } + + + + fout << cnt << '\n'; + + for (int i : sol) + + fout << i << ' '; + + return 0; + +} diff --git a/strmatch/rk/1610aabf-a7ef-4f28-a83b-8c5ff4224ea9.cpp b/strmatch/rk/1610aabf-a7ef-4f28-a83b-8c5ff4224ea9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ba498bb6c4796b83583997fda038a93f5e2b68 --- /dev/null +++ b/strmatch/rk/1610aabf-a7ef-4f28-a83b-8c5ff4224ea9.cpp @@ -0,0 +1,212 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + + + +ofstream fout("strmatch.out"); + + + +vectorpoz; + + + +const int MOD=1e9+7; + + + +const int p1=131; + + + + + + + +int hsh(string s) + + + +{ + + + + int mod=0,val=1; + + + + for(int poz=(int)s.size()-1;poz>=0;poz--) + + + + { + + + + mod=(mod+1LL*val*(s[poz]-'0'))%MOD; + + + + val=(1LL*val*p1)%MOD; + + + + } + + + + return mod; + + + +} + + + + + + + +int main() + + + +{ + + + + string a,b; + + + + fin>>a>>b; + + + + int nr=0,hsa=hsh(a); + + + + string s=b.substr(0,a.size()); + + + + int hsb=hsh(s); + + + + int put=1; + + + + for(int i=1;i<=(int)a.size();i++) + + + + { + + + + put=(1LL*put*p1)%MOD; + + + + } + + + + for(int i=a.size();i<(int)b.size();i++) + + + + { + + + + if(hsa==hsb) + + + + { + + + + nr++; + + + + if(nr<=1000) + + + + poz.push_back(i); + + + + } + + + + hsb=(1LL*hsb*p1+b[i]-'0')%MOD; + + + + hsb-=(1LL*put*(b[i-a.size()]-'0'))%MOD; + + + + if(hsb<0) + + + + hsb+=MOD; + + + + } + + + + if(hsa==hsb) + + + + { + + + + nr++; + + + + if(nr<=1000) + + + + poz.push_back(b.size()); + + + + } + + + + fout< +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s[2000001],s2[2000001]; + +int n,b=101,mod1=100007,mod2=100021,h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + +int main() { + + f>>s>>s2; + + n=strlen(s); + + m=strlen(s2); + + p1=1; + + p2=1; + + for(i=0; im) { + + g<<0<<'\n'; + + return 0; + + } + + for(i=0; i +#include +#include +using namespace std; + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + + + + + + + +char s1[2000009],s2[2000009]; + +int p1=1,p2=1; + +int ans[1009],k,n,m; + +int HASH1,HASH2 ,Hash1,Hash2; + +int main(){ + + f>>s1>>s2; + + n=strlen(s1); + + m=strlen(s2); + + for(int i=0;i + + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string cuv,sir; + +int cuvSize,sirSize; + +int sol[1001],t; + + + +unsigned long long cuvP,A=915358133,B=974328597; + +unsigned long long h[2000001],p[2000001]; + + + +void build() + +{ + + for(auto lit:cuv) + + cuvP=(cuvP*A+lit)%B; + + + + p[0]=1; + + + + for(int i=1;i<=sirSize;i++) + + { + + h[i]=(h[i-1]*A+sir[i-1])%B; + + p[i]=(p[i-1]*A)%B; + + } + +} + + + +void rabinKarp() + +{ + + for(int i=cuvSize;i<=sirSize;i++) + + { + + if( (h[i]-h[i-cuvSize]*p[cuvSize]+B*B)%B==cuvP ) + + { + + t++; + + if(t<=1000) + + sol[t]=i-cuvSize; + + } + + } + +} + + + +int main() + +{ + + in>>cuv>>sir; + + cuvSize=cuv.size(); + + sirSize=sir.size(); + + + + build(); + + rabinKarp(); + + + + out< +using namespace std; + +char s1[2000005],s2[2000005]; + +int ap[2000005]; + +const int mod1=1e9+7; + +const int mod2=1e9+9; + +const int baza=97; + +int main() + +{ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + fgets(s1+1,2000005,stdin); + + fgets(s2+1,2000005,stdin); + + int n=strlen(s1+1),m=strlen(s2+1); + + long long put1=1,put2=1,rez1=0,rez2=0; + + --n;--m; + + int i,j,cnt=0; + + for(i=1;i=n) + + ap[++cnt]=i; + + } + + printf("%d\n",cnt); + + for(i=1;i<=min(1000,cnt);++i)printf("%d ",ap[i]-n); + + return 0; + +} diff --git a/strmatch/rk/1bfc8289-bb83-44a9-b852-59bd9472fefe.cpp b/strmatch/rk/1bfc8289-bb83-44a9-b852-59bd9472fefe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9587163d4ce727c5b22392d7e7e7590e1497b308 --- /dev/null +++ b/strmatch/rk/1bfc8289-bb83-44a9-b852-59bd9472fefe.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + + + +ifstream si("strmatch.in"); + +ofstream so("strmatch.out"); + + + +string s,s2; + +int n,b=101,m1=100007,m2=100021,rs,h1,h2,m,i,hash1,hash2,p1,p2,v[1001]; + + + +int main() + +{ + + si>>s>>s2; + + n=s.length(); + + m=s2.length(); + + p1=1; + + p2=1; + + for(i=0; im) + + { + + so<<0<<'\n'; + + return 0; + + } + + for(i=0; i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +const int h = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +string a; + +string b; + + + +vector rez; + + + +int main() + +{ + + in >> a; + + in >> b; + + + + int cnt = 0; + + + + + + int n = a.size(); + + int m = b.size(); + + + + if(n > m) + + { + + out << 0 <<'\n'; + + return 0; + + } + + + + int Sm1 = 0; + + int Sm2 = 0; + + int Sc1 = 0; + + int Sc2 = 0; + + + + int p1 = 1; + + int p2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + int nr1 = a[i]; + + int nr2 = b[i]; + + + + Sm1 = (Sm1 * h + nr1) % mod1; + + Sm2 = (Sm2 * h + nr1) % mod2; + + Sc1 = (Sc1 * h + nr2) % mod1; + + Sc2 = (Sc2 * h + nr2) % mod2; + + + + + + if(i != 0) + + {p1 = (p1 * h) % mod1; + + p2 = (p2 * h) % mod2; + + } + + + + } + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + rez.push_back(0); + + } + + + + + + for(int i = n; i < m; i++) + + { + + int nr1 = b[i - n]; + + int nr2 = b[i]; + + + + + + Sc1 = ((Sc1 - (nr1 * p1) % mod1 + mod1) * h + nr2) % mod1; + + Sc2 = ((Sc2 - (nr1 * p2) % mod2 + mod2) * h + nr2) % mod2; + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + if(cnt <= 1000) + + rez.push_back(i - n + 1); + + } + + + + } + + + + out << cnt<<'\n'; + + + + for(int i = 0; i < rez.size(); i++) + + out << rez[i]<<" "; + + + + + + return 0; + +} diff --git a/strmatch/rk/1f262d48-0cce-4116-af0c-2088b6b3cc66.cpp b/strmatch/rk/1f262d48-0cce-4116-af0c-2088b6b3cc66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e594c92ff0bda9a0edeb021056ea997d77512d8 --- /dev/null +++ b/strmatch/rk/1f262d48-0cce-4116-af0c-2088b6b3cc66.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + + scanf("%s%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + if(m>n){ + printf("0\n"); + return 0; + } + + int hashw1=0,hashw2=0; + int hashs1=0,hashs2=0; + + int p1=1,p2=1; + + + for(int i=0;i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +const int h = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +string a; + +string b; + + + +vector rez; + + + +int main() + +{ + + in >> a; + + in >> b; + + + + int cnt = 0; + + + + + + int n = a.size(); + + int m = b.size(); + + + + if(n > m) + + { + + out << 0 <<'\n'; + + return 0; + + } + + + + int Sm1 = 0; + + int Sm2 = 0; + + int Sc1 = 0; + + int Sc2 = 0; + + + + int p1 = 1; + + int p2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + int nr1 = a[i]; + + int nr2 = b[i]; + + + + Sm1 = (Sm1 * h + nr1) % mod1; + + Sm2 = (Sm2 * h + nr1) % mod2; + + Sc1 = (Sc1 * h + nr2) % mod1; + + Sc2 = (Sc2 * h + nr2) % mod2; + + + + + + if(i != 0) + + {p1 = (p1 * h) % mod1; + + p2 = (p2 * h) % mod2; + + } + + + + } + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + rez.push_back(0); + + } + + + + + + for(int i = n; i < m; i++) + + { + + int nr1 = b[i - n]; + + int nr2 = b[i]; + + + + + + Sc1 = ((Sc1 - (nr1 * p1) % mod1 + mod1) * h + nr2) % mod1; + + Sc2 = ((Sc2 - (nr1 * p2) % mod2 + mod2) * h + nr2) % mod2; + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + if(cnt <= 1000) + + rez.push_back(i - n + 1); + + } + + + + } + + + + out << cnt<<'\n'; + + + + for(int i = 0; i < rez.size(); i++) + + out << rez[i]<<" "; + + + + + + return 0; + +} diff --git a/strmatch/rk/22c74efc-8c92-42e2-bf43-b1448609b5b1.cpp b/strmatch/rk/22c74efc-8c92-42e2-bf43-b1448609b5b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf2753a5678fb06a0fed6cfb8418d5be45edbb4b --- /dev/null +++ b/strmatch/rk/22c74efc-8c92-42e2-bf43-b1448609b5b1.cpp @@ -0,0 +1,112 @@ +#include + + + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +long long pro1,pro2,val1,val2,val3,val4,n,n2,i,nrr,nrr2; + +char c,a[2000005]; + +int main() + +{ + + pro1=pro2=1; + +while (true) + +{ + + in.get(c); + + if (c=='\n') break; + + val1=(67*val1+c)%(85636124); + + val2=(67*val2+c)%(93579235); + + if (n!=0){ + + pro1=67*pro1%(85636124); + + pro2=67*pro2%(93579235);} + + n++; + +} + +while (true) + +{ + + in.get(a[i++]); + + if (a[i-1]=='\n') break; + + if (i<=n) {val3=(67*val3+a[i-1])%(85636124); + + val4=(67*val4+a[i-1])%(93579235); + + } + + else {if (val1==val3&&val2==val4) nrr++; + + val3=(67*(val3-pro1*a[i-n-1])%(85636124)+(85636124)+a[i-1])%(85636124); + + val4=(67*(val4-pro2*a[i-n-1])%(93579235)+(93579235)+a[i-1])%(93579235);} + +} + +if (val1==val3&&val2==val4) nrr++; + +out<1000) return 0; + + out<1000) return 0; + + out< +#include +#include +using namespace std; + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + + + + + + + +char s1[2000009],s2[2000009]; + +int p1=1,p2=1; + +int ans[1009],k,n,m; + +int HASH1,HASH2 ,Hash1,Hash2; + +int main(){ + + f>>s1>>s2; + + n=strlen(s1); + + m=strlen(s2); + + for(int i=0;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string pat, txt; + + + +const int d = 73, mod1 = 100007, mod2 = 100021; + + + +vector sol; + + + +void rabin_karp(string pat, string txt, int m1, int m2) { + + int p1 = 0, t1 = 0, p2 = 0, t2 = 0, h1 = 1, h2 = 1; + + int m = pat.size(); + + int n = txt.size(); + + for(int i = 0; i < m; ++i) { + + p1 = (d * p1 + pat[i]) % m1; + + p2 = (d * p2 + pat[i]) % m2; + + + + if(i != 0) { + + h1 = (h1 * d) % m1; + + h2 = (h2 * d) % m2; + + } + + } + + for(int i = 0; i < m; ++i) { + + t1 = (d * t1 + txt[i]) % m1; + + t2 = (d * t2 + txt[i]) % m2; + + } + + if(p1 == t1 && p2 == t2) { + + sol.push_back(0); + + } + + for(int i = m; i < n; ++i) { + + t1 = ((t1 - (txt[i - m] * h1) % m1 + m1) * d + txt[i]) % m1; + + t2 = ((t2 - (txt[i - m] * h2) % m2 + m2) * d + txt[i]) % m2; + + if(p1 == t1 && p2 == t2) { + + sol.push_back(i - m + 1); + + } + + } + +} + + + +void read() { + + getline(f, pat); + + + + getline(f, txt); + +} + + + +void solve() { + + read(); + + rabin_karp(pat, txt, mod1, mod2); + + int nr = sol.size(); + + g << nr << '\n'; + + for(int i = 0; i < min(nr, 1000); ++i) { + + g << sol[i] << ' '; + + } + +} + + + +int main(void) { + + solve(); + +} diff --git a/strmatch/rk/25720f3a-5174-4334-873d-92e00d239c7a.cpp b/strmatch/rk/25720f3a-5174-4334-873d-92e00d239c7a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66f14ed591310b6755b81d254607e11fa761667f --- /dev/null +++ b/strmatch/rk/25720f3a-5174-4334-873d-92e00d239c7a.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a,b; + +vector sol; + + + +int main() + +{ + + fin >> a >> b; + + + + int cod1=0, cod2=0, p1, p2, c1=0, c2=0, nr = 0, n= a.size(); + + cod1 = cod2 = 0; + + p1 = p2 = 1; + + for(unsigned int i = 0; i < a.size(); i++) + + { + + cod1 = (cod1*73 % 100007 + a[i]%100007)%100007; + + cod2 = (cod2*73 % 100021 + a[i]%100021)%100021; + + if(i != 0) + + p1 = p1*73 %100007, p2 = p2*73 %100021; + + } + + + + for(unsigned int i = 0 ; i < b.size(); i++) + + if(i < a.size()) + + { + + c1 = (c1*73 %100007 + b[i]%100007)%100007; + + c2 = (c2*73 %100021 + b[i]%100021)%100021; + + + + } + + else{ + + if(c1 == cod1 && c2 == cod2) + + {nr++; if(nr <= 1000) sol.push_back(i-n);} + + + + c1 = ((c1 + 100007 - b[i-n]*p1%100007)*73%100007 + b[i])%100007; + + c2 = ((c2 + 100021 - b[i-n]*p2%100021)*73%100021 + b[i])%100021; + + } + + + + if(c1 == cod1 && c2 == cod2) + + { + + nr++; + + if(nr <= 1000) sol.push_back(b.size()-n); + + } + + + + fout << nr <<'\n'; + + for(unsigned int i = 0; i < sol.size(); i++) + + fout << sol[i] << " "; + + + + return 0; + + + +} diff --git a/strmatch/rk/261ff525-5dfa-4861-af38-6bc79437f559.cpp b/strmatch/rk/261ff525-5dfa-4861-af38-6bc79437f559.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f315b727f1cf2f423d9ddcc113838973be70027f --- /dev/null +++ b/strmatch/rk/261ff525-5dfa-4861-af38-6bc79437f559.cpp @@ -0,0 +1,93 @@ +#include +#include + + + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +const long long lim=2e6+3; + +vector sol; + +int main() + +{ + + string a,b; + + long long na,nb,ha=0,hb=0,cnt=0,p=1; + + cin>>a>>b; + + na=a.size(),nb=b.size(); + + if(na>nb) + + { + + cout<<0; + + return 0; + + } + + for(long long i=0;i +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/29a28e5a-afc9-416a-95c6-31a3aed736b1.cpp b/strmatch/rk/29a28e5a-afc9-416a-95c6-31a3aed736b1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..edb73e7f9e3fc9ee1a407e2b9d69ac99f570f450 --- /dev/null +++ b/strmatch/rk/29a28e5a-afc9-416a-95c6-31a3aed736b1.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +vectorpoz; + +const long long MOD=1e9+7; + +const long long p1=103; + +long long cc(char c) + +{ + + if(c>='a'&&c<='z') + + return c-'a'+1; + + if(c>='A'&&c<='Z') + + return c-'A'+27; + + return c-'0'+53; + +} + +long long hsh(string s) + +{ + + long long mod=0,val=1; + + for(long long poz=(long long)s.size()-1;poz>=0;poz--) + + { + + mod=(mod+1LL*val*cc(s[poz]))%MOD; + + val=(1LL*val*p1)%MOD; + + } + + return mod; + +} + +int main() + +{ + + string a,b; + + fin>>a>>b; + + long long nr=0,hsa=hsh(a); + + string s=b.substr(0,a.size()); + + long long hsb=hsh(s); + + long long put=1; + + for(long long i=1;i<=(long long)a.size();i++) + + { + + put=(1LL*put*p1)%MOD; + + + + } + + + + for(long long i=a.size();i<(long long)b.size();i++) + + { + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(i); + + } + + + + hsb=(1LL*hsb*p1+cc(b[i]))%MOD; + + hsb-=(1LL*put*(cc(b[i-a.size()])))%MOD; + + if(hsb<0) + + hsb+=MOD; + + } + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(b.size()); + + } + + fout< + + +const int MAX_N = 2000005; + +const int P = 57; + +const int MOD1 = 1000000009; + +const int MOD2 = 1000000007; + + + +int n, m; + + + +std::vector ans; + + + +char p[MAX_N], t[MAX_N]; + + + +long long a1, a2, b1, b2, p1, p2; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n%s", p + 1, t + 1); + + t[0] = p[0] = '\0'; + + n = strlen(p + 1); + + m = strlen(t + 1); + + p1 = p2 = 1; + + for (int i = 1; i <= n; ++i) { + + a1 = (1LL * a1 * P + p[i]) % MOD1; + + a2 = (1LL * a2 * P + p[i]) % MOD2; + + if (i != 1) { + + p1 = (1LL * p1 * P) % MOD1; + + p2 = (1LL * p2 * P) % MOD2; + + } + + } + + for (int i = 1; i <= n; ++i) { + + b1 = (1LL * b1 * P + t[i]) % MOD1; + + b2 = (1LL * b2 * P + t[i]) % MOD2; + + } + + if (a1 == b1 && a2 == b2) { + + ans.push_back(0); + + } + + for (int i = n + 1; i <= m; ++i) { + + b1 = ((1LL * b1 - (p1 * t[i - n]) % MOD1 + MOD1) % MOD1 * P + t[i]) % MOD1; + + b2 = ((1LL * b2 - (p2 * t[i - n]) % MOD2 + MOD2) % MOD2 * P + t[i]) % MOD2; + + if (a1 == b1 && a2 == b2) { + + ans.push_back(i - n); + + } + + } + + std::cout << ans.size() << "\n"; + + for (int i = 0; i < ans.size() && i < 1000; ++i) { + + std::cout << ans[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/rk/2abdada2-fe67-4987-b0fa-ac4ac586ebb4.cpp b/strmatch/rk/2abdada2-fe67-4987-b0fa-ac4ac586ebb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e015649abde83c859ca181911981d8720dc3f6a --- /dev/null +++ b/strmatch/rk/2abdada2-fe67-4987-b0fa-ac4ac586ebb4.cpp @@ -0,0 +1,115 @@ +#include +#include + + +int min(int a, int b){ + + if(a <= b) + + return a; + + return b; + +} + + + +long long hash[2], base; + +char word[2000001], text[2000001]; + +int sol[1001]; + +int main(){ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + + + int wLen, tLen, nr = 0; + + long long whash[2], rhash[2], basePow[2]; + + + + scanf("%s%s",&word, &text); + + wLen = strlen(word); + + tLen = strlen(text); + + + + hash[0] = 666013; + + hash[1] = 1000000007; + + base = 97; + + + + for(int j = 0; j < 2; j++){ + + rhash[j] = 0; + + whash[j] = 0; + + basePow[j] = 1; + + } + + + + for(int i = 0; i < wLen; i++){ + + for(int j = 0; j < 2; j++){ + + whash[j] = ( (whash[j] * base) % hash[j] + word[i] ) % hash[j]; + + basePow[j] = (basePow[j] * base) % hash[j]; + + } + + } + + + + for(int i = 0; i < tLen; i++){ + + for(int j = 0; j < 2; j++) + + rhash[j] = ( (rhash[j] * base) % hash[j] + text[i] ) % hash[j]; + + if(i >= wLen){ + + for(int j = 0; j < 2; j++){ + + rhash[j] = ((rhash[j] - basePow[j] * text[i - wLen]) % hash[j] + hash[j]) % hash[j]; + + } + + } + + if(i >= wLen - 1 && whash[0] == rhash[0] && whash[1] == rhash[1]){ + + nr++; + + if(nr <= 1000) + + sol[nr - 1] = i - wLen + 1; + + } + + } + + printf("%d\n",nr); + + for(int i = 0; i < min(nr, 1000); i++) + + printf("%d ",sol[i]); + + return 0; + +} diff --git a/strmatch/rk/2b527f77-7d8c-44f2-a935-6eab8ea57a52.cpp b/strmatch/rk/2b527f77-7d8c-44f2-a935-6eab8ea57a52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28110248183f7ad5292b82fd568a7ba17ea1e857 --- /dev/null +++ b/strmatch/rk/2b527f77-7d8c-44f2-a935-6eab8ea57a52.cpp @@ -0,0 +1,146 @@ +#include + + +const int mod=1000000007; + + + +const int prim=666013; + + + +using namespace std; + + + +string str1,str2; + + + +unsigned int nr; + + + +queue ans; + + + +int main() + +{ + + ifstream fin ("strmatch.in"); + + ofstream fout ("strmatch.out"); + + fin>>str1>>str2; + + if (str1.length()>str2.length()) + + { + + fout<<0; + + return 0; + + } + + int_fast64_t ct=1,hash1=0,chash=0; + + for (int i=1;i +#include +#include + + + + + + +using namespace std; + + + +int i, nr=0, H, H2, has, has2, v[1005]; + +char m[2000003], n[2000004]; + +int main() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + cin>>m>>n; + + int l1= strlen(m); + + int l2= strlen(n); + + for(int i=0; i + +using namespace std; + +class RollingHash { +private: + int a{}, mod{}; + int max_power{1}, value{0}; + +public: + RollingHash() = default; + + RollingHash(int a, int mod) : a(a), mod(mod) {} + + void init(const string &s) { + for (int i = 0; i < s.length(); i++) { + value = (value * a + s[i]) % mod; + + if (i) max_power = (max_power * a) % mod; + } + } + + void roll(char out, char in) { + value = ((value - (out * max_power) % mod + mod) * a + in) % mod; + } + + bool operator==(const RollingHash &other) const { + return value == other.value; + } +}; + +tuple> rabin_karp(const string &pattern, const string &text) { + const int a = 31; + const int n1 = 100003, n2 = 100109; + + array hp{RollingHash(a, n1), RollingHash(a, n2)}; + array ht{RollingHash(a, n1), RollingHash(a, n2)}; + + if (pattern.length() > text.length()) { + return {false, {}}; + } + + for (int i = 0; i < 2; i++) { + hp[i].init(pattern); + ht[i].init(text.substr(0, pattern.length())); + } + + vector matches; + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(0); + + for (int i = pattern.length(); i < text.length(); i++) { + for (int k = 0; k < 2; k++) + ht[k].roll(text[i - pattern.length()], text[i]); + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(i - pattern.length() + 1); + } + + return {true, matches}; +} + +int main() { + ifstream in("strmatch.in"); + ofstream out("strmatch.out"); + + string pattern, text; + + in >> pattern >> text; + + vector matches; + bool okay; + + tie(okay, matches) = rabin_karp(pattern, text); + + if (!okay) { + out << 0; + return 0; + } + + out << matches.size() << '\n'; + + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + out << matches[i] << ' '; +} diff --git a/strmatch/rk/2d879855-1816-48b0-9c49-8ffcc6ffa984.cpp b/strmatch/rk/2d879855-1816-48b0-9c49-8ffcc6ffa984.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa6b685c8d676b8a1c8a00ae1667fc07efab6c0b --- /dev/null +++ b/strmatch/rk/2d879855-1816-48b0-9c49-8ffcc6ffa984.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include + + + + + +using namespace std; + +class rollingHash{ +private: long long key; + long long maxpower; + long long n; +public: + long long val; + bool operator== (const rollingHash &other) const { + return (this->val == other.val && this->key == other.key && this->n == other.n); + } + void assignKeys(long long key, long long n){ + this->key = key; + this->n = n; + } + void buildInitialHash(string x){ + this->val = 0; + long long p = 1; + for (int i = x.length() - 1; i >= 0; --i) { + this->val = (this->val + ((p * x[i]) % this->n)) % this->n; + this->maxpower = p; + p = (p * this->key) % this->n; + } + } + void rollHash(char out, char in){ + this->val = ((this->val - (out * this->maxpower) % this->n + this->n) * this->key + in) % this->n; + } +}; + +int main() { + ifstream f("strmatch.in"); + ofstream g("strmatch.out"); + string a, b; + f>>a>>b; + rollingHash h1a, h2a; + h1a.assignKeys(31, 9153637); + h2a.assignKeys(37, 9231329); + h1a.buildInitialHash(a); + h2a.buildInitialHash(a); + vector matches; + rollingHash h1b, h2b; + h1b.assignKeys(31, 9153637); + h2b.assignKeys(37, 9231329); + h1b.buildInitialHash(b.substr(0, a.length())); + h2b.buildInitialHash(b.substr(0, a.length())); + for (int i = a.length(); i < b.length(); ++i) { + if(h1a == h1b && h2a == h2b) matches.push_back(i - a.length()); + h1b.rollHash(b[i - a.length()], b[i]); + h2b.rollHash(b[i - a.length()], b[i]); + } + if(h1a == h1b && h2a == h2b) matches.push_back(b.length() - a.length()); + g< +#include +#include +using namespace std; + + + +char A[2000001], B[2000001]; +int main() + +{ + + vector result; + + + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + + + int M = strlen(A); + + int N = strlen(B); + + + + + + + + int p = 1, p2 = 1, n = 0; + + int aVal = 0; + + int aVal2 = 0; + + + + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + + + for (int i = 0; i < M; i++) + + { + + aVal = (aVal * 73 + A[i]) % 100007; + + aVal2 = (aVal2 * 73 + A[i]) % 100011; + + hash1 = (hash1 * 73 + B[i]) % 100007; + + hash2 = (hash2 * 73 + B[i]) % 100011; + + if (i != 0) + + { + + p = (p * 73) % 100007; + + p2 = (p2 * 73) % 100011; + + } + + } + + + + if (hash1 == aVal && hash2 == aVal2) + + { + + n++; + + result.push_back(0); + + } + + + + for (int i = M; i < N; i++) + + { + + hash1 = ((hash1 - (B[i - M] * p) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - M] * p2) % 100011 + 100011) * 73 + B[i]) % 100011; + + if (hash1 == aVal && hash2 == aVal2) + + { + + if(n < 1000) + + result.push_back(i - M + 1); + + n++; + + } + + } + + + + printf("%d\n", n); + + for (int i = 0; i < result.size(); i++) + + { + + printf("%d ", result[i]); + + } + + + + return 0; + +} diff --git a/strmatch/rk/2febf269-0ede-41b5-86d2-6e7c59e4e819.cpp b/strmatch/rk/2febf269-0ede-41b5-86d2-6e7c59e4e819.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5290a207af815cabdb1a6cd272e970b29560f3e6 --- /dev/null +++ b/strmatch/rk/2febf269-0ede-41b5-86d2-6e7c59e4e819.cpp @@ -0,0 +1,164 @@ +#include +#include + + +using namespace std; +char a[2000000 +1],b[2000000 +1]; + +int v[2000000 +1]; + + + +int deletecif(int cif,int pow){ + + return cif*pow; + +} + + + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("strmatch.in","r"); + + fout=fopen("strmatch.out","w"); + + + + int i,j,nra,sizea,sirurimatch,nrb,inceput,sfarsit,pow,sizeb,sizev,nrb2,nra2,pow2; + + + + fscanf(fin,"%s",a); + + fscanf(fin,"%s",b); + + + + sizea=strlen(a); + + nra=0; + + nra2=0; + + i=0; + + + + while(a[i]!='\0'){ + + nra=((nra*128)%666013 +a[i]%666013)%666013; + + nra2=((nra2*128)%666019 +a[i]%666019)%666019; + + i++; + + } + + + + nrb=0; + + nrb2=0; + + sizeb=0; + + j=0; + + + + while(sizeb +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const long long A = 1763922631; + +const long long B = 1617924853; + +const long long C = 666013; + + + +vectorsol; + + + +int main() + +{ + + long long rasp = 0, put, x, y; + + string a, b; + + fin >> a >> b; + + if (a.size() > b.size()) + + { + + fout << 0; + + return 0; + + } + + + + long long n = a.size(), i, j, m = b.size(); + + long long valb = 0, vala = 0; + + long long val1a = 0, val1b = 0; + + put = 1; + + long long put1 = 1; + + + + for (i = 0; i < n; ++i) + + { + + vala = (vala * A + a[i]) % B; + + val1a = (val1a * A + a[i]) % C; + + put = (put * A) % B; + + put1 = (put1 * A) % C; + + } + + for (i = 0; i < n; ++i) { + + valb = (valb * A + b[i]) % B; + + val1b = (val1b * A + b[i]) % C; + + } + + + + if (vala == valb && val1a == val1b) + + ++rasp, sol.push_back(0); + + + + for (i = 1; i <= m - n; ++i) + + { + + valb = (valb * A + b[i + n - 1]) % B; + + val1b = (val1b * A + b[i + n - 1]) % C; + + x = put * b[i - 1]; + + y = put1 * b[i - 1]; + + valb = (((valb - x) % B) + B) % B; + + val1b = (((val1b - y) % C) + C) % C; + + if (vala == valb && val1a == val1b) + + { + + ++rasp; + + sol.push_back(i); + + } + + } + + + + fout << rasp << endl; + + for (int i = 0; i < min((int)sol.size(), 1000); i++) + + fout << sol[i] << " "; + + fout << "\n"; + + + + return 0; + +} diff --git a/strmatch/rk/3260176c-e428-45bd-a52f-4dae0e0f66dd.cpp b/strmatch/rk/3260176c-e428-45bd-a52f-4dae0e0f66dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1da6748c63e638bd29775cfcf2a18756e6aa2be --- /dev/null +++ b/strmatch/rk/3260176c-e428-45bd-a52f-4dae0e0f66dd.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + + + + + + +int main() +{ + FILE *in = freopen("strmatch.in", "r", stdin); + FILE *out = freopen("strmatch.out", "w", stdout); + char *subsir = (char *)malloc(2000010 * sizeof(char)); + char *sir = (char *)malloc(2000010 * sizeof(char)); + char c; + int n = 0; + int m = 0; + while ((c = getchar()) != '\n') + { + subsir[m++] = c; + } + subsir[m] = 0; + while ((c = getchar()) != '\n' && c != EOF) + { + sir[n++] = c; + } + sir[n] = 0; + int h_sir1 = 0; + int h_subsir1 = 0; + int h_sir2 = 0; + int h_subsir2 = 0; + int h1 = 1; + int h2 = 1; + int nr = 0; + int aparitii[1000]; + for (int i = 0; i < m - 1; i++) + { + h1 = (73 * h1) % 100007; + h2 = (73 * h2) % 100021; + h_sir1 = (73 * h_sir1 + sir[i]) % 100007; + h_subsir1 = (73 * h_subsir1 + subsir[i]) % 100007; + h_sir2 = (73 * h_sir2 + sir[i]) % 100021; + h_subsir2 = (73 * h_subsir2 + subsir[i]) % 100021; + } + h_sir1 = (73 * h_sir1 + sir[m - 1]) % 100007; + h_subsir1 = (73 * h_subsir1 + subsir[m - 1]) % 100007; + h_sir2 = (73 * h_sir2 + sir[m - 1]) % 100021; + h_subsir2 = (73 * h_subsir2 + subsir[m - 1]) % 100021; + for (int i = 0; i <= n - m; i++) + { + if (h_sir1 == h_subsir1 && h_sir2 == h_subsir2) + { + if (nr < 1000) + aparitii[nr] = i; + nr++; + } + if (i != n - m) + { + h_sir1 = (73 * (h_sir1 - h1 * sir[i]) + sir[i + m]) % 100007; + h_sir1 = h_sir1 < 0 ? h_sir1 + 100007 : h_sir1; + h_sir2 = (73 * (h_sir2 - h2 * sir[i]) + sir[i + m]) % 100021; + h_sir2 = h_sir2 < 0 ? h_sir2 + 100021 : h_sir2; + } + } + printf("%d\n", nr); + for (int i = 0; i < nr && i < 1000; i++) + { + printf("%d ", aparitii[i]); + } + fclose(in); + fclose(out); + return 0; +} diff --git a/strmatch/rk/32d2365b-a57f-44e6-b533-f92a7686cbc2.cpp b/strmatch/rk/32d2365b-a57f-44e6-b533-f92a7686cbc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1721ad3b4d428822de4943742699c5ea636c813c --- /dev/null +++ b/strmatch/rk/32d2365b-a57f-44e6-b533-f92a7686cbc2.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/35e9c706-c5c0-486c-9f6c-f1ea64245d38.cpp b/strmatch/rk/35e9c706-c5c0-486c-9f6c-f1ea64245d38.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6866a8725554432880097e90b072a8918648a40 --- /dev/null +++ b/strmatch/rk/35e9c706-c5c0-486c-9f6c-f1ea64245d38.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +int hash1a,hash2a,hash1b,hash2b; + +char a[2000007],b[2000007]; + +vector v; + +int put1,put2; + +int main() + +{int lg1,i,lg2; + + fin.getline(a,2000007); + + fin.getline(b,2000007); + + lg1=strlen(a); + + lg2=strlen(b); + + put1=put2=1; + + if(lg1>lg2) + + {fout<<0;return 0;} + + for(i=0;i +using namespace std; + + + + + + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +string text,pattern; + +void read() +{ + in>>pattern>>text; +} + + +void rabin_karp() +{ + int base1=1,base2=1; + long long basemod1=0,basemod2=0; + + vector results; + + for(int i=0; itext.length()) + out<<0; + else + rabin_karp(); +} diff --git a/strmatch/rk/38539f86-fd5b-4c52-b5cb-6292d26183d8.cpp b/strmatch/rk/38539f86-fd5b-4c52-b5cb-6292d26183d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e364c841715233b46a7f4374aa64fa36b8e8aa6 --- /dev/null +++ b/strmatch/rk/38539f86-fd5b-4c52-b5cb-6292d26183d8.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i0)? h2[i-1]: 0) + 1LL*p2[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9)) && + + ((1LL*hs2*p2[i-m+1])%int(1e9+9)) == (1LL*(h2[i]- ((i>=m)?h2[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/38db473d-76ec-459b-a3e2-70d6fd1a8c42.cpp b/strmatch/rk/38db473d-76ec-459b-a3e2-70d6fd1a8c42.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0deb34c6ebdad6737790af11716601f6598f7d30 --- /dev/null +++ b/strmatch/rk/38db473d-76ec-459b-a3e2-70d6fd1a8c42.cpp @@ -0,0 +1,134 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i + + +using namespace std; + + + +const int m1=100007; + +const int m2=100021; + +const int p=101; + + + +string A; + +string B; + + + +int nA; + +int nB; + + + +int hash1,hash2,h1,h2; + + + +int rasp; + + + +int p1=1,p2=1; + + + +bool nuVerifica=false; + + + +int v[2000001]; + + + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int main() + +{ + + f>>A>>B; + + nA=A.length(); + + nB=B.length(); + + + + for(int i=0;inB) + + { + + g<<"0"<<"\n"; + + return 0; + + } + + + + for(int i=0;i +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/3cad7ec5-9c85-49da-9e3c-5683f4bc6e1e.cpp b/strmatch/rk/3cad7ec5-9c85-49da-9e3c-5683f4bc6e1e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bf296ac70d3ee8f413b5302b620dd7ca8d0dd8c9 --- /dev/null +++ b/strmatch/rk/3cad7ec5-9c85-49da-9e3c-5683f4bc6e1e.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/3e5d0bd8-2b35-406d-85cc-ce1a40baec4b.cpp b/strmatch/rk/3e5d0bd8-2b35-406d-85cc-ce1a40baec4b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49e173356e599ee671f5101a206f52bcba8fbeba --- /dev/null +++ b/strmatch/rk/3e5d0bd8-2b35-406d-85cc-ce1a40baec4b.cpp @@ -0,0 +1,78 @@ +#include + +const int LMAX = 2000005; +const int a = 31; + +char p[LMAX], t[LMAX]; +int lenp, lent; + +void read() { + scanf("%s %s", p, t); + + lenp = strlen(p); + lent = strlen(t); +} + +struct RollingHash { + int a, n; + int maxpw{1}, value{0}; + + void init(char *s, int len) { + for (int i = 0; i < len; i++) { + value = (value * a + s[i]) % n; + + if (i) maxpw = (maxpw * a) % n; + } + } + + void roll(char out, char in) { + value = ((value - (out * maxpw) % n + n) * a + in) % n; + } + + bool operator==(const RollingHash& other) const { + return value == other.value; + } +}; + +void rabin_karp() { + std::vector matches; + + int n1 = 100003, n2 = 100109; + + RollingHash hp[]{{a, n1}, {a, n2}}; + RollingHash ht[]{{a, n1}, {a, n2}}; + + for (int i = 0; i < 2; i++) { + hp[i].init(p, lenp); + ht[i].init(t, lenp); + } + + if (lenp > lent) { + printf("0\n"); + return; + } + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(0); + + for (int i = lenp; i < lent; i++) { + ht[0].roll(t[i - lenp], t[i]); + ht[1].roll(t[i - lenp], t[i]); + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(i - lenp + 1); + } + + printf("%ld\n", matches.size()); + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + printf("%d ", matches[i]); +} + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + read(); + rabin_karp(); +} diff --git a/strmatch/rk/40ea2079-1d74-4e32-ac3d-13c60ae3b1dd.cpp b/strmatch/rk/40ea2079-1d74-4e32-ac3d-13c60ae3b1dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1da6748c63e638bd29775cfcf2a18756e6aa2be --- /dev/null +++ b/strmatch/rk/40ea2079-1d74-4e32-ac3d-13c60ae3b1dd.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + + + + + + +int main() +{ + FILE *in = freopen("strmatch.in", "r", stdin); + FILE *out = freopen("strmatch.out", "w", stdout); + char *subsir = (char *)malloc(2000010 * sizeof(char)); + char *sir = (char *)malloc(2000010 * sizeof(char)); + char c; + int n = 0; + int m = 0; + while ((c = getchar()) != '\n') + { + subsir[m++] = c; + } + subsir[m] = 0; + while ((c = getchar()) != '\n' && c != EOF) + { + sir[n++] = c; + } + sir[n] = 0; + int h_sir1 = 0; + int h_subsir1 = 0; + int h_sir2 = 0; + int h_subsir2 = 0; + int h1 = 1; + int h2 = 1; + int nr = 0; + int aparitii[1000]; + for (int i = 0; i < m - 1; i++) + { + h1 = (73 * h1) % 100007; + h2 = (73 * h2) % 100021; + h_sir1 = (73 * h_sir1 + sir[i]) % 100007; + h_subsir1 = (73 * h_subsir1 + subsir[i]) % 100007; + h_sir2 = (73 * h_sir2 + sir[i]) % 100021; + h_subsir2 = (73 * h_subsir2 + subsir[i]) % 100021; + } + h_sir1 = (73 * h_sir1 + sir[m - 1]) % 100007; + h_subsir1 = (73 * h_subsir1 + subsir[m - 1]) % 100007; + h_sir2 = (73 * h_sir2 + sir[m - 1]) % 100021; + h_subsir2 = (73 * h_subsir2 + subsir[m - 1]) % 100021; + for (int i = 0; i <= n - m; i++) + { + if (h_sir1 == h_subsir1 && h_sir2 == h_subsir2) + { + if (nr < 1000) + aparitii[nr] = i; + nr++; + } + if (i != n - m) + { + h_sir1 = (73 * (h_sir1 - h1 * sir[i]) + sir[i + m]) % 100007; + h_sir1 = h_sir1 < 0 ? h_sir1 + 100007 : h_sir1; + h_sir2 = (73 * (h_sir2 - h2 * sir[i]) + sir[i + m]) % 100021; + h_sir2 = h_sir2 < 0 ? h_sir2 + 100021 : h_sir2; + } + } + printf("%d\n", nr); + for (int i = 0; i < nr && i < 1000; i++) + { + printf("%d ", aparitii[i]); + } + fclose(in); + fclose(out); + return 0; +} diff --git a/strmatch/rk/41718688-024e-484a-9dea-6222d8deaa47.cpp b/strmatch/rk/41718688-024e-484a-9dea-6222d8deaa47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ebca98066f0401516a6df847e27a9d77fa9283e8 --- /dev/null +++ b/strmatch/rk/41718688-024e-484a-9dea-6222d8deaa47.cpp @@ -0,0 +1,83 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +const long long A=914530549; + +const long long B=914531357; + +int main() + +{ + + long long rasp=0; + + string a,b; + + cin>>a>>b; + + if(a.size()>b.size()) + + { + + cout<<0<<'\n'; + + return 0; + + } + + long long n=a.size(),ka=0,kb=0,power=1; + + for(long long i=0;i + + + + +using namespace std; + + + + + + + +const long long p1=999727999, b1=137; + +const long long p2=999686999, b2=139; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[5+2000000], B[5+2000000]; + + + +vectorrasp; + + + +int main() + +{ + + int n,m; + + in.getline(A,2000000 +2); + + in.getline(B,2000000 +2); + + n=strlen(A); + + m=strlen(B); + + if(n>m) + + { + + out<<"0"; + + return 0; + + } + + long long exp1=1,exp2=1,hash1B=0,hash2B=0,hash1A=0,hash2A=0,cnt=0; + + for(int i=0;i +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int n,m,nr1,nr2,h1,h2,p1,p2,nrap; + +bool fr[2000005]; + +string s,t; + + + +void Read(); + +void RabinKarp(); + + + +int main() + +{ Read(); + + RabinKarp(); + + return 0; + +} + + + +void Read() + +{ f>>s>>t; + + n=s.size(); + + m=t.size(); + + p1=p2=1; + + for(int i=0; im) + + { g<<0; + + return; + + } + + for(int i=0; i + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/46738a32-4ee8-42e8-96cb-cf43d3900b1d.cpp b/strmatch/rk/46738a32-4ee8-42e8-96cb-cf43d3900b1d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6573d6583175849fdddfef776e0b36e29dbaf5cf --- /dev/null +++ b/strmatch/rk/46738a32-4ee8-42e8-96cb-cf43d3900b1d.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int P = 62; + +const int NMAX = 2e6 + 7; + +const int m1 = 1e5 + 7; + +const int m2 = 1e5 + 21; + + + +int N,M,P1,P2,hashA1,hashA2,hash1,hash2; + +vector a; + +char A[NMAX],B[NMAX]; + + + +int main() + +{ + + + + fin >> A >> B; + + N=strlen(A), M=strlen(B); + + + + P1=1, P2=1; + + for(int i=0;i +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +char a[2000001],b[2000001]; + +int v[2000001]; + +int main(){ + + FILE *fin=fopen("strmatch.in","r"); + + FILE *fout=fopen("strmatch.out","w"); + + int n,m,i,am1,am2,bm1,bm2,p1,p2,sol=0; + + + + fscanf(fin,"%s",&a); + + fscanf(fin,"%s",&b); + + n=strlen(a); + + m=strlen(b); + + if(n>m) + + sol=0; + + else{ + + am1=am2=bm1=bm2=0; + + for(i=0; i +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +string A, B; + +int NA, NB; + +int hashA1, hashA2, P1, P2; + +char match[2000001]; + + + +int main() { + + fin >> A >> B; + + NA = A.size(), NB = B.size(); + + if (NA > NB) { + + fout << "0\n"; + + return 0; + + } + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i ++) { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + if (i != 0) + + P1 = (P1 * 73) % 100007, P2 = (P2 * 73) % 100021; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr ++; + + + + for (int i = NA; i < NB; i ++) { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr ++; + + } + + fout << Nr << '\n'; + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i ++) + + if (match[i]) + + Nr ++, fout << i << ' '; + + return 0; + +} diff --git a/strmatch/rk/49c8fdc7-b7b9-43dc-b762-fac44410c6f5.cpp b/strmatch/rk/49c8fdc7-b7b9-43dc-b762-fac44410c6f5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..828a624369b34f8a54f2254c72b7e1c519cc8856 --- /dev/null +++ b/strmatch/rk/49c8fdc7-b7b9-43dc-b762-fac44410c6f5.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + + scanf("%s%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + if(m>n){ + printf("0\n"); + return 0; + } + + int hashw1=0,hashw2=0; + int hashs1=0,hashs2=0; + + int p1=1,p2=1; + + + for(int i=0;i + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +unsigned long long B=127; + +string a,b; +unsigned long long hashA,hashB,powe; +unsigned long long sol; +vector ans; +void make_hash() +{ + powe=1; + for(unsigned long long i=0;i>a>>b; + if(a.size()>b.size()) + { + out<<"0"; + return 0; + } + make_hash(); + if(hashA == hashB) + { + sol++; + ans.push_back(0); + } + for(unsigned int i=1;i<=b.size()-a.size();i++) + { + hashB=(hashB * B+b[i+a.size()-1]); + unsigned long long aux=(powe * b[i-1]); + hashB=(hashB-aux); + if(hashA==hashB) + { + sol++; + if(ans.size()<=1000) + ans.push_back(i); + } + } + out< +#include +#include +using namespace std; + +string a,b; + +unsigned int baz=71; + +unsigned int ha[2000001],puteri[2000001],hasha; + +int rez[2000001],cont; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + getline(cin,a); + + getline(cin,b); + + for(int i=a.size()-1; i>=0; --i) + + hasha=(hasha*baz+a[i]); + + for(int i=b.size()-1; i>=0; --i) + + ha[i]=(ha[i+1]*baz+b[i]); + + puteri[0]=1; + + for(int i=1; i<=b.size(); ++i) + + puteri[i]=(puteri[i-1]*baz); + + for(int i=a.size()-1; i +#include +#include + +using namespace std; + +const int NMAX = 2000005; + +const int P = 73; +const int MOD1 = 1e5+7; +const int MOD2 = 1e5+21; + +char a[NMAX],b[NMAX]; +int n,m; + +int cnt; +int poz[1024]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + scanf("%s\n%s",a+1,b+1); + + a[0]=' '; + b[0]=' '; + + m=strlen(a)-1; + n=strlen(b)-1; + + int A1=0,A2=0; + int PA1=1,PA2=1; + + for(int i=1;i<=m;++i){ + A1=(A1*P+(a[i]-'0'))%MOD1; + + A2=(A2*P+(a[i]-'0'))%MOD2; + } + + int B1=0,B2=0; + + for(int i=1;i<=m;++i){ + B1=(B1*P+(b[i]-'0'))%MOD1; + if(i!=m) + PA1=(PA1*P)%MOD1; + + B2=(B2*P+(b[i]-'0'))%MOD2; + if(i!=m) + PA2=(PA2*P)%MOD2; + } + + if(B1==A1 && B2==A2){ + ++cnt; + poz[cnt]=0; + } + + for(int i=m+1;i<=n;++i){ + B1=((B1-((b[i-m]-'0')*PA1%MOD1)+MOD1)*P+(b[i]-'0'))%MOD1; + + B2=((B2-((b[i-m]-'0')*PA2%MOD2)+MOD2)*P+(b[i]-'0'))%MOD2; + + if(B1==A1 && B2==A2){ + ++cnt; + if(cnt<=1000) + poz[cnt]=i-m; + + } + } + + printf("%d\n",cnt); + + for(int i=1;i<=min(cnt,1000);++i) + printf("%d ",poz[i]); + + return 0; +} diff --git a/strmatch/rk/4dba0eac-c625-448b-b9db-e1515196888f.cpp b/strmatch/rk/4dba0eac-c625-448b-b9db-e1515196888f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7cb10c56f56cf5aeced3f40865da8ef08ab34e7 --- /dev/null +++ b/strmatch/rk/4dba0eac-c625-448b-b9db-e1515196888f.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 2e6; + +const int MOD1 = 666013; + +const int MOD2 = 269696969; + +const int BASE = 67; + + + +char A[5 + N], B[5 + N]; + +bool sol[5 + N]; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + long long lenA, lenB, hashA1, hashA2, hashB1, hashB2, pow1, pow2; + + fgets(A, 3 + N, stdin); + + fgets(B, 3 + N, stdin); + + lenA = strlen(A) - 1; + + lenB = strlen(B) - 1; + + + + if(lenA > lenB) { + + printf("0\n"); + + return 0; + + } + + + + hashA1 = hashA2 = 0; + + pow1 = pow2 = 1; + + + + for(int i = 0; i < lenA; i++) { + + hashA1 = (hashA1 * BASE + A[i]) % MOD1; + + hashA2 = (hashA2 * BASE + A[i]) % MOD2; + + if(i > 0) { + + pow1 = (pow1 * BASE) % MOD1; + + pow2 = (pow2 * BASE) % MOD2; + + } + + } + + + + hashB1 = hashB2 = 0; + + for(int i = 0; i < lenA; i++) { + + hashB1 = (hashB1 * BASE + B[i]) % MOD1; + + hashB2 = (hashB2 * BASE + B[i]) % MOD2; + + } + + int k = 0; + + + + if(hashA1 == hashB1 && hashA2 == hashB2) { + + sol[0] = true; + + k++; + + } + + for(int i = lenA; i < lenB; i++) { + + hashB1 = ((hashB1 - (B[i - lenA] * pow1) % MOD1 + MOD1) * BASE + B[i]) % MOD1; + + hashB2 = ((hashB2 - (B[i - lenA] * pow2) % MOD2 + MOD2) * BASE + B[i]) % MOD2; + + + + if(hashA1 == hashB1 && hashA2 == hashB2) { + + k++; + + sol[i - lenA + 1] = true; + + } + + } + + + + printf("%d\n", k); + + k = 0; + + for(long long i = 0; i < lenB && k < 1000; i++) { + + if(sol[i] == true) { + + k++; + + printf("%lld ", i); + + } + + } + + printf("\n"); + + return 0; + +} diff --git a/strmatch/rk/4fde0257-0edf-4b43-9ae6-31ab6358bcc2.cpp b/strmatch/rk/4fde0257-0edf-4b43-9ae6-31ab6358bcc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d1a5ba7eb83621ff1a59c3c04f862e418dca063 --- /dev/null +++ b/strmatch/rk/4fde0257-0edf-4b43-9ae6-31ab6358bcc2.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int nmax = 2000005; + +const int p = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +char a[nmax], b[nmax]; + +int na, nb; + +int hasha1, hasha2, p1, p2; + +char match[nmax]; + +int main() + +{ + + fin >> a >> b; + + na = strlen(a); + + nb = strlen(b); + + p1 = p2 = 1; + + hasha1 = hasha2 = 0; + + for(int i = 0; i < na; ++i) + + { + + hasha1 = (hasha1 * p + a[i]) % mod1; + + hasha2 = (hasha2 * p + a[i]) % mod2; + + if(i != 0) + + { + + p1 = (p1 * p) % mod1; + + p2 = (p2 * p) % mod2; + + } + + } + + if(na > nb) + + { + + fout << "0\n"; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for(int i = 0; i < na; ++i) + + { + + hash1 = (hash1 * p + b[i]) % mod1; + + hash2 = (hash2 * p + b[i]) % mod2; + + } + + int nr = 0; + + if(hash1 == hasha1 && hash2 == hasha2) + + match[0] = 1, nr++; + + for(int i = na; i < nb; ++i) + + { + + hash1 = ((hash1 - (b[i - na] * p1) % mod1 + mod1) * p + b[i]) % mod1; + + hash2 = ((hash2 - (b[i - na] * p2) % mod2 + mod2) * p + b[i]) % mod2; + + if(hash1 == hasha1 && hash2 == hasha2) + + match[i - na + 1] = 1, nr++; + + } + + fout << nr << "\n"; + + nr = 0; + + for(int i = 0; i < nb && nr < 1000; ++i) + + if(match[i]) + + nr++, fout << i << " "; + + + + return 0; + +} diff --git a/strmatch/rk/51064aca-77d4-4843-a916-c3f4dc1ea70a.cpp b/strmatch/rk/51064aca-77d4-4843-a916-c3f4dc1ea70a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b7f9279c348938176eeb3be71c409d3a8f6ce8e --- /dev/null +++ b/strmatch/rk/51064aca-77d4-4843-a916-c3f4dc1ea70a.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char s1[2000005], s2[2000005]; + +int nr=0, sol[2000005]; + + + +struct Hash{ + + long long n, m, power, hashh; + + void init(char *s, long long len) + + { + + power=1; + + hashh=0; + + for(long long i=len-1; i>=0; i--) + + { + + hashh=(hashh+(1LL*power*s[i])%m)%m; + + if(i) + + power=(power*n)%m; + + } + + } + + + + void roll(char toRemove, char toAdd) + + { + + hashh=(((hashh-(1LL*toRemove*power)%m+m)*n)%m+toAdd)%m; + + } + + + + + +}; + + + +int main() + +{ + + int l1, l2; + + f.getline(s1, 2000005); + + f.getline(s2, 2000005); + + l1=strlen(s1); + + l2=strlen(s2); + + Hash pHash{31,40099},tHash{31,40099}; + + Hash hHash{53,319993},oHash{53,319993}; + + long long nr=0; + + pHash.init(s1,l1); + + tHash.init(s2,l1); + + hHash.init(s1,l1); + + oHash.init(s2,l1); + + if(pHash.hashh==tHash.hashh && hHash.hashh==oHash.hashh) + + { + + sol[++nr]=0; + + } + + for(long long i=l1;i=1000) + + nr=1000; + + for(int i=1; i<=nr;i++) + + g< +using namespace std; +ifstream fin("strmatch.in"); ofstream fout("strmatch.out"); + + + + + + + +long long n ,m; + +string a, b; + +long long h1=0, h2=0, hb=0; + +long long ord[300]; + +vector pos; + + + + + +int32_t main(){ + +ios_base :: sync_with_stdio(false); fin.tie(NULL); fout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +getline(fin, b); + +getline(fin, a); + +n=a.length(); m=b.length(); + +if(n + + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string cuv,sir; + +int cuvSize,sirSize; + +int sol[1001],t; + + + +unsigned long long cuvP,A=915358133,B=974328597; + +unsigned long long h[2000001],p[2000001]; + + + +void build() + +{ + + for(auto lit:cuv) + + cuvP=(cuvP*A+lit)%B; + + + + p[0]=1; + + + + for(int i=1;i<=sirSize;i++) + + { + + h[i]=(h[i-1]*A+sir[i-1])%B; + + p[i]=(p[i-1]*A)%B; + + } + +} + + + +void rabinKarp() + +{ + + for(int i=cuvSize;i<=sirSize;i++) + + { + + if( (h[i]-h[i-cuvSize]*p[cuvSize]+B*B)%B==cuvP ) + + { + + t++; + + if(t<=1000) + + sol[t]=i-cuvSize; + + } + + } + +} + + + +int main() + +{ + + in>>cuv>>sir; + + cuvSize=cuv.size(); + + sirSize=sir.size(); + + + + build(); + + rabinKarp(); + + + + out< +#include +char a[2000001],b[2000001]; + +int v[2000001]; + +int main() + +{ + + FILE *fin=fopen("strmatch.in","r"); + + FILE *fout=fopen("strmatch.out","w"); + + int n,m,i,am1,am2,bm1,bm2,p1,p2,sol=0; + + + + fscanf(fin,"%s",&a); + + fscanf(fin,"%s",&b); + + n=strlen(a); + + m=strlen(b); + + if(n>m) + + sol=0; + + else + + { + + am1=am2=bm1=bm2=0; + + for(i=0; i +#include + + + + + + + +char a[2000005], b[2000005]; +int nrla, nrlb, hashA1, hashA2, P1 = 1, P2 = 1, hash1, hash2, nr_pot; +bool match[2000005]; + +void citire() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + scanf("%s\n%s", a, b); + nrla = strlen(a); + nrlb = strlen(b); +} + +void initializare() { + for (int i = 0; i < nrla; ++i) { + hashA1 = (hashA1 * 73 + a[i]) % 100007; + hashA2 = (hashA2 * 73 + a[i]) % 100021; + + if (i != 0) { + P1 = (P1 * 73) % 100007; + P2 = (P2 * 73) % 100021; + } + } + + for (int i = 0; i < nrla; ++i) { + hash1 = (hash1 * 73 + b[i]) % 100007; + hash2 = (hash2 * 73 + b[i]) % 100021; + } +} + +void cautare_potriviri() { + if (hash1 == hashA1 && hash2 == hashA2) { + match[0] = true; + nr_pot++; + } + + for (int i = nrla; i < nrlb; ++i) { + hash1 = ((hash1 - (b[i - nrla] * P1) % 100007 + 100007) * 73 + b[i]) % 100007; + hash2 = ((hash2 - (b[i - nrla] * P2) % 100021 + 100021) * 73 + b[i]) % 100021; + + if (hash1 == hashA1 && hash2 == hashA2) { + match[i - nrla + 1] = true; + nr_pot++; + } + } +} + +void afisare() { + printf("%d\n", nr_pot); + nr_pot = 0; + for (int i = 0; i < nrlb && nr_pot < 1000; ++i) + if (match[i]) { + nr_pot++; + printf("%d ", i); + } +} + +int main() { + citire(); + if (nrla > nrlb) { + printf("0"); + return 0; + } + initializare(); + cautare_potriviri(); + afisare(); + return 0; +} diff --git a/strmatch/rk/5755080f-92d0-4385-8537-39c8029859d6.cpp b/strmatch/rk/5755080f-92d0-4385-8537-39c8029859d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f4cc0ac205e34bd530a989d15b6fcfc98e652ce --- /dev/null +++ b/strmatch/rk/5755080f-92d0-4385-8537-39c8029859d6.cpp @@ -0,0 +1,134 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +inline int getMod(long long x) { + + return x - x / MOD1 * MOD1; + +} + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = getMod(1LL * codPat * SIGMA + codCaracter(pat[i])); + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = getMod(1LL * codSir * SIGMA + codCaracter(A[i])); + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = getMod(1LL * putere * SIGMA); + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = getMod((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1); + + + + codSir = getMod(1LL * codSir * SIGMA + codCaracter(A[i])); + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/58acbe83-a3df-478b-aad4-9a653fa8e89b.cpp b/strmatch/rk/58acbe83-a3df-478b-aad4-9a653fa8e89b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86cc26011e3f4dcf5383992c0ce2f1a854b4a911 --- /dev/null +++ b/strmatch/rk/58acbe83-a3df-478b-aad4-9a653fa8e89b.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +using namespace std; + + + +char a[2000001], b[2000001]; + +int n, m, nr; + +int hash1, hash2, p1, p2, h1, h2; + +char match[2000001]; + + + +void citire() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n %s", a, b); + + n=strlen(a); + + m=strlen(b); + +} + + + +void comparare() + +{ + + p1=p2=1; + + hash1=hash2=0; + + for (int i=0;i m) + + { + + cout<<0; + + return 0; + + } + + comparare(); + + afisare(); + + return 0; + +} diff --git a/strmatch/rk/594e53bb-592b-402f-8ba0-6e8ddd29c31e.cpp b/strmatch/rk/594e53bb-592b-402f-8ba0-6e8ddd29c31e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1760ca0115d0c08161d1392ce3ed189aec3d6f4e --- /dev/null +++ b/strmatch/rk/594e53bb-592b-402f-8ba0-6e8ddd29c31e.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +vectorpoz; + +const int MOD1=1e9+7; + +const int p1=131; + +const int MOD2=1e9+9; + +const int p2=135; + +pair < int, int > hsh(string s) + +{ + + int mod1=0,mod2=0,val1=1,val2=1; + + for(int poz=(int)s.size()-1;poz>=0;poz--) + + { + + mod1=(mod1+1LL*val1*(s[poz]-'0'))%MOD1; + + mod2=(mod2+1LL*val2*(s[poz]-'0'))%MOD2; + + val1=(1LL*val1*p1)%MOD1; + + val2=(1LL*val2*p2)%MOD2; + + } + + return {mod1, mod2}; + +} + + + +int main() + +{ + + string a,b; + + fin>>a>>b; + + int nr=0; + + pair < int, int > hsa=hsh(a); + + string s=b.substr(0,a.size()); + + pair < int, int > hsb=hsh(s); + + int put1=1, put2=1; + + for(int i=1;i<=(int)a.size();i++) + + { + + put1=(1LL*put1*p1)%MOD1; + + put2=(1LL*put2*p2)%MOD2; + + } + + for(int i=a.size();i<(int)b.size();i++) + + { + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(i); + + } + + hsb.first=(1LL*hsb.first*p1+b[i]-'0')%MOD1; + + hsb.second=(1LL*hsb.second*p2+b[i]-'0')%MOD2; + + hsb.first-=(1LL*put1*(b[i-a.size()]-'0'))%MOD1; + + hsb.second-=(1LL*put2*(b[i-a.size()]-'0'))%MOD2; + + if(hsb.first<0) + + hsb.first+=MOD1; + + if(hsb.second<0) + + hsb.second+=MOD2; + + } + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(b.size()); + + } + + fout< +#include +#include + + +using namespace std; + +char patt[2000002],s[2000002]; + +int poz[1010]; + + + +int is_prime(int x) + +{ + + if(x<2||x>2&&x%2==0)return 0; + + for(int d=3;d*d<=x;d+=2) + + if(x%d==0)return 0; + + return 1; + +} + + + +int next_prime(int x) + +{ + + x++; + + while(!is_prime(x))x++; + + return x; + +} + + + +int main() + +{ + + FILE *f=fopen("strmatch.in","r"); + + fgets(patt,2000002,f); + + fgets(s,2000002,f); + + f=fopen("strmatch.out","w"); + + int lp=strlen(patt),i; + + if(patt[lp-1]=='\n')patt[--lp]=0; + + int ls=strlen(s); + + if(s[ls-1]=='\n')s[--ls]=0; + + if(ls +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int N = 2e6; + +const int MOD1 = 666013; + +const int MOD2 = 269696969; + +const int BASE = 67; + + + +char A[5 + N], B[5 + N]; + +bool sol[5 + N]; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + long long lenA, lenB, hashA1, hashA2, hashB1, hashB2, pow1, pow2; + + fgets(A, 3 + N, stdin); + + fgets(B, 3 + N, stdin); + + lenA = strlen(A) - 1; + + lenB = strlen(B) - 1; + + + + if(lenA > lenB) { + + printf("0\n"); + + return 0; + + } + + + + hashA1 = hashA2 = 0; + + pow1 = pow2 = 1; + + + + for(int i = 0; i < lenA; i++) { + + hashA1 = (hashA1 * BASE + A[i]) % MOD1; + + hashA2 = (hashA2 * BASE + A[i]) % MOD2; + + if(i > 0) { + + pow1 = (pow1 * BASE) % MOD1; + + pow2 = (pow2 * BASE) % MOD2; + + } + + } + + + + hashB1 = hashB2 = 0; + + for(int i = 0; i < lenA; i++) { + + hashB1 = (hashB1 * BASE + B[i]) % MOD1; + + hashB2 = (hashB2 * BASE + B[i]) % MOD2; + + } + + int k = 0; + + + + if(hashA1 == hashB1 && hashA2 == hashB2) { + + sol[0] = true; + + k++; + + } + + for(int i = lenA; i < lenB; i++) { + + hashB1 = ((hashB1 - (B[i - lenA] * pow1) % MOD1 + MOD1) * BASE + B[i]) % MOD1; + + hashB2 = ((hashB2 - (B[i - lenA] * pow2) % MOD2 + MOD2) * BASE + B[i]) % MOD2; + + + + if(hashA1 == hashB1 && hashA2 == hashB2) { + + k++; + + sol[i - lenA + 1] = true; + + } + + } + + + + printf("%d\n", k); + + k = 0; + + for(long long i = 0; i < lenB && k < 1000; i++) { + + if(sol[i] == true) { + + k++; + + printf("%lld ", i); + + } + + } + + printf("\n"); + + return 0; + +} diff --git a/strmatch/rk/5cade3ea-1a0a-43b8-b982-74834ca974b8.cpp b/strmatch/rk/5cade3ea-1a0a-43b8-b982-74834ca974b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fb45b2687a8ceb48b1a7e9e850dc1df691722de --- /dev/null +++ b/strmatch/rk/5cade3ea-1a0a-43b8-b982-74834ca974b8.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_NO_POS = 1000; + +const int BASE = 62; + +const int MOD = 10007; + +const int MOD2 = 10009; + + + +void rabinKarp(string & A, string & B, int & n, list & ans) { + + if (A.size() > B.size()) { + + return; + + } + + + + int hashWindow = 0; + + int hashWindow2 = 0; + + int hashPattern = 0; + + int hashPattern2 = 0; + + int pow = 1; + + int pow2 = 1; + + for (int i = 0; i < A.size() - 1; i++) { + + pow = (pow * BASE) % MOD; + + pow2 = (pow2 * BASE) % MOD2; + + } + + for (int i = 0; i < A.size(); i++) { + + hashWindow = (BASE * hashWindow + B[i]) % MOD; + + hashWindow2 = (BASE * hashWindow2 + B[i]) % MOD2; + + hashPattern = (BASE * hashPattern + A[i]) % MOD; + + hashPattern2 = (BASE * hashPattern2 + A[i]) % MOD2; + + } + + if (hashWindow == hashPattern && hashWindow2 == hashPattern2) { + + n++; + + ans.push_back(0); + + } + + + + for (int i = A.size(); i < B.size(); i++) { + + hashWindow = (BASE * (hashWindow - (pow * B[i - A.size()])) + B[i]) % MOD; + + hashWindow2 = (BASE * (hashWindow2 - (pow2 * B[i - A.size()])) + B[i]) % MOD2; + + hashWindow += (hashWindow < 0) ? MOD : 0; + + hashWindow2 += (hashWindow2 < 0) ? MOD2 : 0; + + if (hashWindow == hashPattern && hashWindow2 == hashPattern2) { + + n++; + + if (n <= MAX_NO_POS) { + + ans.push_back(i - A.size() + 1); + + } + + } + + } + +} + + + +int main() { + + ifstream fin; + + ofstream fout; + + string A, B; + + + + fin.open("strmatch.in"); + + fin >> A >> B; + + fin.close(); + + + + int n = 0; + + list ans; + + rabinKarp(A, B, n, ans); + + + + fout.open("strmatch.out"); + + fout << n << endl; + + for (auto it = ans.begin(); it != ans.end(); ++it) { + + fout << *it << " "; + + } + + fout.close(); + + + + ans.clear(); + + + + return 0; + +} diff --git a/strmatch/rk/5d98531a-75ec-4fcd-8fdb-1fede678316d.cpp b/strmatch/rk/5d98531a-75ec-4fcd-8fdb-1fede678316d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5c04ee28efc90ac4dd1e117011a945f680e107e --- /dev/null +++ b/strmatch/rk/5d98531a-75ec-4fcd-8fdb-1fede678316d.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +using namespace std; + +const int NMAX = 2000005; + +const int P = 73; +const int MOD1 = 1e5+7; +const int MOD2 = 1e5+21; + +char a[NMAX],b[NMAX]; +int n,m; + +int cnt; +int poz[1024]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + scanf("%s\n%s",a+1,b+1); + + a[0]=' '; + b[0]=' '; + + m=strlen(a)-1; + n=strlen(b)-1; + + int A1=0,A2=0; + int PA1=1,PA2=1; + + for(int i=1;i<=m;++i){ + A1=(A1*P+(a[i]-'0'))%MOD1; + + A2=(A2*P+(a[i]-'0'))%MOD2; + } + + int B1=0,B2=0; + + for(int i=1;i<=m;++i){ + B1=(B1*P+(b[i]-'0'))%MOD1; + if(i!=m) + PA1=(PA1*P)%MOD1; + + B2=(B2*P+(b[i]-'0'))%MOD2; + if(i!=m) + PA2=(PA2*P)%MOD2; + } + + if(B1==A1 && B2==A2){ + ++cnt; + poz[cnt]=0; + } + + for(int i=m+1;i<=n;++i){ + B1=((B1-((b[i-m]-'0')*PA1%MOD1)+MOD1)*P+(b[i]-'0'))%MOD1; + + B2=((B2-((b[i-m]-'0')*PA2%MOD2)+MOD2)*P+(b[i]-'0'))%MOD2; + + if(B1==A1 && B2==A2){ + ++cnt; + if(cnt<=1000) + poz[cnt]=i-m; + + } + } + + printf("%d\n",cnt); + + for(int i=1;i<=min(cnt,1000);++i) + printf("%d ",poz[i]); + + return 0; +} diff --git a/strmatch/rk/5e48c9b9-5832-4526-af6f-8aaf0138d3a8.cpp b/strmatch/rk/5e48c9b9-5832-4526-af6f-8aaf0138d3a8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b615bdc484de85394a379f1ff4e7e2ad5c97169 --- /dev/null +++ b/strmatch/rk/5e48c9b9-5832-4526-af6f-8aaf0138d3a8.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +using namespace std; +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string a,b; + +vector sol; + + + +int main() + +{ + + fin >> a >> b; + + + + int cod1=0, cod2=0, p1, p2, c1=0, c2=0, nr = 0, n= a.size(); + + cod1 = cod2 = 0; + + p1 = p2 = 1; + + for(unsigned int i = 0; i < a.size(); i++) + + { + + cod1 = (cod1*73 % 100007 + a[i]%100007)%100007; + + cod2 = (cod2*73 % 100021 + a[i]%100021)%100021; + + if(i != 0) + + p1 = p1*73 %100007, p2 = p2*73 %100021; + + } + + + + for(unsigned int i = 0 ; i < b.size(); i++) + + if(i < a.size()) + + { + + c1 = (c1*73 %100007 + b[i]%100007)%100007; + + c2 = (c2*73 %100021 + b[i]%100021)%100021; + + + + } + + else{ + + if(c1 == cod1 && c2 == cod2) + + {nr++; if(nr <= 1000) sol.push_back(i-n);} + + + + c1 = ((c1 + 100007 - b[i-n]*p1%100007)*73%100007 + b[i])%100007; + + c2 = ((c2 + 100021 - b[i-n]*p2%100021)*73%100021 + b[i])%100021; + + } + + + + if(c1 == cod1 && c2 == cod2) + + { + + nr++; + + if(nr <= 1000) sol.push_back(b.size()-n); + + } + + + + fout << nr <<'\n'; + + for(unsigned int i = 0; i < sol.size(); i++) + + fout << sol[i] << " "; + + + + return 0; + + + +} diff --git a/strmatch/rk/620df41d-babb-42b6-855b-c08c9f46050a.cpp b/strmatch/rk/620df41d-babb-42b6-855b-c08c9f46050a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3e594c92ff0bda9a0edeb021056ea997d77512d8 --- /dev/null +++ b/strmatch/rk/620df41d-babb-42b6-855b-c08c9f46050a.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + + scanf("%s%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + if(m>n){ + printf("0\n"); + return 0; + } + + int hashw1=0,hashw2=0; + int hashs1=0,hashs2=0; + + int p1=1,p2=1; + + + for(int i=0;i +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +string A, B; + +int NA, NB; + +int hashA1, hashA2, P1, P2; + +char match[2000001]; + + + +int main() { + + fin >> A >> B; + + NA = A.size(), NB = B.size(); + + if (NA > NB) { + + fout << "0\n"; + + return 0; + + } + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i ++) { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + if (i != 0) + + P1 = (P1 * 73) % 100007, P2 = (P2 * 73) % 100021; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr ++; + + + + for (int i = NA; i < NB; i ++) { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr ++; + + } + + fout << Nr << '\n'; + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i ++) + + if (match[i]) + + Nr ++, fout << i << ' '; + + return 0; + +} diff --git a/strmatch/rk/63f70d83-365a-4b14-8d7f-237b1b0718c7.cpp b/strmatch/rk/63f70d83-365a-4b14-8d7f-237b1b0718c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..896d93b0e6372972ac7521c98f439f0c68019151 --- /dev/null +++ b/strmatch/rk/63f70d83-365a-4b14-8d7f-237b1b0718c7.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 6; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/64c75e39-09aa-4735-afb2-31cfb9ce5c55.cpp b/strmatch/rk/64c75e39-09aa-4735-afb2-31cfb9ce5c55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55587c4e60085c50f6ea28242dceb9d822be66c4 --- /dev/null +++ b/strmatch/rk/64c75e39-09aa-4735-afb2-31cfb9ce5c55.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +using namespace std; + + + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + + + +vector s; + + + +int N, M, x1, x2, y1, y2, power1, power2; + + + +char A[2000002]; + +char B[2000002]; + + + +int main() + +{ + + f.getline(A,2000002); + + f.getline(B,2000002); + + + + N = strlen(A); + + M = strlen(B); + + + + if (N > M) + + { + + g << 0 << '\n'; + + return 0; + + } + + + + for (int i = 0; i < N; ++ i ) + + { + + x1 = ((x1 * 127) % 10003 + (A[ i ] - '0')) % 10003; + + x2 = ((x2 * 131) % 666013 + (A[ i ] - '0')) % 666013; + + y1 = ((y1 * 127) % 10003 + (B[ i ] - '0')) % 10003; + + y2 = ((y2 * 131) % 666013 + (B[ i ] - '0')) % 666013; + + } + + + + power1 = power2 = 1; + + for (int i = 1; i < N; ++ i ) + + { + + power1 = (power1 * 127) % 10003; + + power2 = (power2 * 131) % 666013; + + } + + + + if (x1 == y1 && x2 == y2) + + { + + s.push_back(0); + + } + + + + for (int i = N; i < M; ++ i ) + + { + + y1 = ((y1 - (power1 * (B[i - N] - '0')) % 10003 + 10003) * 127 + (B[i] - '0')) % 10003; + + y2 = ((y2 - (power2 * (B[i - N] - '0')) % 666013 + 666013) * 131 + (B[i] - '0')) % 666013; + + + + if (y1 == x1 && y2 == x2) + + { + + s.push_back(i - N + 1); + + } + + } + + + + g << s.size() << '\n'; + + + + for (int i = 0; i < 1000 && i < s.size(); ++ i ) + + g << s[i] << " "; + + + + return 0; + +} diff --git a/strmatch/rk/65d800f2-964e-4497-9d2a-1e0fe6503f5f.cpp b/strmatch/rk/65d800f2-964e-4497-9d2a-1e0fe6503f5f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63d27cf17c81896076528448388d02214b7a4715 --- /dev/null +++ b/strmatch/rk/65d800f2-964e-4497-9d2a-1e0fe6503f5f.cpp @@ -0,0 +1,136 @@ +#include + + + + +using namespace std; + + + + + + + +const long long p1=999727999, b1=137; + +const long long p2=999686999, b2=139; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[5+2000000], B[5+2000000]; + + + +vectorrasp; + + + +int main() + +{ + + int n,m; + + in.getline(A,2000000 +2); + + in.getline(B,2000000 +2); + + n=strlen(A); + + m=strlen(B); + + if(n>m) + + { + + out<<"0"; + + return 0; + + } + + long long exp1=1,exp2=1,hash1B=0,hash2B=0,hash1A=0,hash2A=0,cnt=0; + + for(int i=0;i +#include +#include +using namespace std; + +string a,b; + +unsigned int baz=71; + +unsigned int ha[2000001],puteri[2000001],hasha; + +int rez[2000001],cont; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + getline(cin,a); + + getline(cin,b); + + for(int i=a.size()-1; i>=0; --i) + + hasha=(hasha*baz+a[i]); + + for(int i=b.size()-1; i>=0; --i) + + ha[i]=(ha[i+1]*baz+b[i]); + + puteri[0]=1; + + for(int i=1; i<=b.size(); ++i) + + puteri[i]=(puteri[i-1]*baz); + + for(int i=a.size()-1; i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef string::const_iterator sit; + + + +class Roller{ + +private: + + int a, n; + + int ap = 1; + + int q = 0; + +public: + + Roller(); + + Roller(int a, int n, sit lt, sit rt) : a(a), n(n){ + + for(auto it = lt; it != rt; ++it){ + + q *= a; + + q += *it; + + q %= n; + + + + if(it!=lt)ap *= a; + + ap %= n; + + } + + } + + Roller(int a, int n, const string &str) : Roller(a,n,str.begin(),str.end()){} + + Roller(int a, int n, const string &str, int len) : Roller(a,n,str.begin(),str.begin()+len){} + + void roll(char out, char in){ + + q -= out*ap; + + q += n*128; + + q %= n; + + q *= a; + + q += in; + + q %= n; + + } + + bool operator==(const Roller &rhs){ + + return q == rhs.q; + + } + +}; + + + +const int a = 31; + +const int n1 = 1000003, n2 = 1000033; + + + +string pattern, text; + + + +int ans1 = 0; + +vector ans2; + + + +int main(){ + + + + fin >> pattern >> text; + + Roller rpattern[] = {Roller(a,n1,pattern),Roller(a,n2,pattern)}; + + Roller rtext[] = {Roller(a,n1,text,pattern.size()),Roller(a,n2,text,pattern.size())}; + + + + if(rpattern[0] == rtext[0] && rpattern[1] == rtext[1]){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(0); + + } + + + + for(int i = pattern.size(); i < text.size(); ++i){ + + rtext[0].roll(text[i-pattern.size()], text[i]); + + rtext[1].roll(text[i-pattern.size()], text[i]); + + if(rpattern[0] == rtext[0] && rpattern[1] == rtext[1]){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-pattern.size()+1); + + } + + } + + + + fout << ans1 << "\n"; + + for(auto a : ans2)fout << a << " "; + + return 0; + +} diff --git a/strmatch/rk/666df061-cc40-4d0d-9423-1b9640de756e.cpp b/strmatch/rk/666df061-cc40-4d0d-9423-1b9640de756e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4172ea69b11a2017283f58233f81664006fa27 --- /dev/null +++ b/strmatch/rk/666df061-cc40-4d0d-9423-1b9640de756e.cpp @@ -0,0 +1,125 @@ +#include +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/68bed9c8-d828-4bf0-8c56-fa309fb96b4d.cpp b/strmatch/rk/68bed9c8-d828-4bf0-8c56-fa309fb96b4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68d8799ced931d3204adc18881fe735abe3ee81b --- /dev/null +++ b/strmatch/rk/68bed9c8-d828-4bf0-8c56-fa309fb96b4d.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out" ); + + + +vector searchh ( string pat, string s ) + +{ + + int mod1 = 100007, mod2 = 100021, d = 73, h1 = 1, h2 = 1, p1 = 0, p2 = 0, t1 = 0, t2 = 0, i; + + + + vector < int > sol; + + + + int m = pat.size(); + + int n = s.size(); + + + + for ( i = 0 ; i < m-1 ; i++ ) + + { + + h1 = (h1*d) % mod1; + + h2 = (h2*d) % mod2; + + } + + + + for ( i = 0 ; i < m; i++ ) + + { + + p1 = (p1 * d + pat[i]) % mod1; + + p2 = (p2 * d + pat[i]) % mod2; + + t1 = (t1 * d + s[i]) % mod1; + + t2 = (t2 * d + s[i]) % mod2; + + } + + + + + + if ( p1 == t1 && p2 == t2 ) + + sol.push_back(0); + + + + for ( i = m ; i < n ; i++ ) + + { + + t1 = ((t1 - (s[i-m]*h1)%mod1 +mod1 ) * d + s[i]) % mod1; + + t2 = ((t2 - (s[i-m]*h2)%mod2 +mod2 ) * d + s[i]) % mod2; + + + + if ( t1 == p1 && t2 == p2 ) + + sol.push_back(i-m+1); + + } + + + + + + + + return sol; + + + + + +} + + + +int main () + +{ + + + + string a, b; + + + + vector sol; + + + + f >> a >> b; + + + + if (a.size() > b.size()) + + { + + g<<0; + + return 0; + + } + + + + sol = searchh ( a, b ); + + + + g << sol.size() << '\n'; + + + + for ( int i = 0 ; i < sol.size() && i < 1000 ; i++ ) + + g< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef long long ll; + + + +const int baza = 543217; + +const int mod = 100000007; + + + +string n, m; + +int N, M; + +vector ans; + + + +void read(){ + + fin >> n >> m; + + N = n.length(); + + M = m.length(); + +} + + + +void solve(){ + + ll hashN = 0, hashM = 0, bazaN = 1; + + + + for(int i = 1; i < N; i++) + + bazaN = (1LL * bazaN * baza) % mod; + + + + for(int i = 0; i < N; i++){ + + hashN = (hashN * baza * 1LL + n[i] - '0')%mod; + + hashM = (hashM * baza * 1LL + m[i] - '0')%mod; + + } + + + + if(hashN == hashM) + + ans.push_back(0); + + + + for(int i = N; i < M; i++){ + + hashM = hashM - (m[i - N] - '0') * bazaN * 1LL; + + hashM %= mod; + + + + hashM = hashM * baza * 1LL + (m[i] - '0'); + + hashM %= mod; + + + + if(hashM < 0) + + hashM += mod; + + + + if(hashM == hashN) + + ans.push_back(i - N + 1); + + } + + + + fout << ans.size() << "\n"; + + + + for(int i = 0; i < ans.size(); i++){ + + fout << ans[i] << " "; + + + + if(i == 999) + + break; + + } + +} + + + +int main() + +{ + + read(); + + solve(); + + + + return 0; + +} diff --git a/strmatch/rk/699501c7-ef91-44a1-a3dd-93f994b838e7.cpp b/strmatch/rk/699501c7-ef91-44a1-a3dd-93f994b838e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9acb12b6677d24ae1e2eb671899e88011046ecb5 --- /dev/null +++ b/strmatch/rk/699501c7-ef91-44a1-a3dd-93f994b838e7.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +const int Mod1 = 1e9+7; + +const int Mod2 = 1e9+9; + +const int p = 131; + +int main() + +{ + + string a,b; + + getline(f,a); + + getline(f,b); + + long long hi1=a[0]-'0',hi2=a[0]-'0',pt1=1,pt2=1,h1=0,h2=0; + + for(int i=1; i rez; + + if(h1==hi1 && h2==hi2) + + { + + rez.push_back(0); + + } + + for(int i=a.size(); i +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s[2000001],s2[2000001]; + +int n,b=101,mod1=100007,mod2=100021,h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + +int main() { + + f>>s>>s2; + + n=strlen(s); + + m=strlen(s2); + + p1=1; + + p2=1; + + for(i=0; im) { + + g<<0<<'\n'; + + return 0; + + } + + for(i=0; i +#include +#include + + +using namespace std; + + + +const int MOD = 1000000007; + +const int MAX_LENGTH = 2000000; + +const int BASE = 62; + + + +char s1[MAX_LENGTH + 5]; + +char s2[MAX_LENGTH + 5]; + +int p; + + + +int code(char ch) { + + if (ch >= 'a' && ch <= 'z') { + + return ch - 'a' + 1; + + } else if (ch >= 'A' && ch <= 'Z') { + + return 26 + ch - 'A' + 1; + + } else { + + return 52 + ch - '0' + 1; + + } + +} + + + +int remove_first(int h, char ch) { + + int newH; + + newH = h - (1LL * p * code(ch)) % MOD; + + if (newH < 0) + + newH += MOD; + + return newH; + +} + + + +int add(int h, char ch) { + + return (1LL * h * BASE + code(ch)) % MOD; + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + int n, m; + + scanf("%s\n%s", s1, s2); + + n = strlen(s1); + + m = strlen(s2); + + + + if (n > m) { + + printf("0\n"); + + return 0; + + } + + + + int h1; + + h1 = 0; + + for (int i = 0; i < n; i++) + + h1 = add(h1, s1[i]); + + + + int h2; + + h2 = 0; + + for (int i = 0; i < n; i++) + + h2 = add(h2, s2[i]); + + + + p = 1; + + for (int i = 1; i <= n - 1; i++) + + p = (1LL * p * BASE) % MOD; + + + + int ans; + + vector sol; + + ans = 0; + + if (h1 == h2) { + + sol.push_back(0); + + ans++; + + } + + for (int i = n; i < m; i++) { + + h2 = remove_first(h2, s2[i - n]); + + h2 = add(h2, s2[i]); + + if (h1 == h2) { + + ans++; + + if (sol.size() < 1000) + + sol.push_back(i - n + 1); + + } + + } + + + + printf("%d\n", ans); + + for (auto it : sol) + + printf("%d ", it); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/6d182f84-f97c-4675-9f33-499cdfb96748.cpp b/strmatch/rk/6d182f84-f97c-4675-9f33-499cdfb96748.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22709543639b3803574d88085345a77cc4f766ef --- /dev/null +++ b/strmatch/rk/6d182f84-f97c-4675-9f33-499cdfb96748.cpp @@ -0,0 +1,247 @@ +#include +#include +char A[2000001], B[2000001]; + + + +int NA, NB; + + + + + + + +int hashA1, hashA2, P1, P2; + + + + + + + +char match[2000001]; + + + + + + + +int main() + + + +{ + + + + freopen("strmatch.in", "rt", stdin); + + + + freopen("strmatch.out", "wt", stdout); + + + + + + + + scanf("%s %s", A, B); + + + + NA = strlen(A); + + + + NB = strlen(B); + + + + + + + + P1 = P2 = 1; + + + + hashA1 = hashA2 = 0; + + + + for (int i = 0; i < NA; i++) + + + + { + + + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + + + + + if (i != 0) + + + + P1 = (P1 * 73) % 100007, + + + + P2 = (P2 * 73) % 100021; + + + + } + + + + + + + + if (NA > NB) + + + + { + + + + printf("0\n"); + + + + return 0; + + + + } + + + + + + + + int hash1 = 0, hash2 = 0; + + + + for (int i = 0; i < NA; i++) + + + + hash1 = (hash1 * 73 + B[i]) % 100007, + + + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + + + + + int Nr = 0; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + + + match[0] = 1, Nr++; + + + + + + + + for (int i = NA; i < NB; i++) + + + + { + + + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + + + + + if (hash1 == hashA1 && hash2 == hashA2) + + + + match[ i - NA + 1 ] = 1, Nr++; + + + + } + + + + printf("%d\n", Nr); + + + + + + + + Nr = 0; + + + + for (int i = 0; i < NB && Nr < 1000; i++) + + + + if (match[i]) + + + + Nr++, + + + + printf("%d ", i); + + + + printf("\n"); + + + + + + + + return 0; + + + +} diff --git a/strmatch/rk/6d8a2372-50f4-4315-b5c6-7576454a363d.cpp b/strmatch/rk/6d8a2372-50f4-4315-b5c6-7576454a363d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..66ba498bb6c4796b83583997fda038a93f5e2b68 --- /dev/null +++ b/strmatch/rk/6d8a2372-50f4-4315-b5c6-7576454a363d.cpp @@ -0,0 +1,212 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + + + +ofstream fout("strmatch.out"); + + + +vectorpoz; + + + +const int MOD=1e9+7; + + + +const int p1=131; + + + + + + + +int hsh(string s) + + + +{ + + + + int mod=0,val=1; + + + + for(int poz=(int)s.size()-1;poz>=0;poz--) + + + + { + + + + mod=(mod+1LL*val*(s[poz]-'0'))%MOD; + + + + val=(1LL*val*p1)%MOD; + + + + } + + + + return mod; + + + +} + + + + + + + +int main() + + + +{ + + + + string a,b; + + + + fin>>a>>b; + + + + int nr=0,hsa=hsh(a); + + + + string s=b.substr(0,a.size()); + + + + int hsb=hsh(s); + + + + int put=1; + + + + for(int i=1;i<=(int)a.size();i++) + + + + { + + + + put=(1LL*put*p1)%MOD; + + + + } + + + + for(int i=a.size();i<(int)b.size();i++) + + + + { + + + + if(hsa==hsb) + + + + { + + + + nr++; + + + + if(nr<=1000) + + + + poz.push_back(i); + + + + } + + + + hsb=(1LL*hsb*p1+b[i]-'0')%MOD; + + + + hsb-=(1LL*put*(b[i-a.size()]-'0'))%MOD; + + + + if(hsb<0) + + + + hsb+=MOD; + + + + } + + + + if(hsa==hsb) + + + + { + + + + nr++; + + + + if(nr<=1000) + + + + poz.push_back(b.size()); + + + + } + + + + fout< +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + +char a[2000005], b[2000005]; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + a[i] - '0' + 1 ) % 45007; + + hash2a = ( hash2a * 80 + a[i] - '0' + 1 ) % 45013; + + + + hash1 = ( hash1 * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( hash2 * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/6f33a354-75c2-427b-b52f-c865126a9603.cpp b/strmatch/rk/6f33a354-75c2-427b-b52f-c865126a9603.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e364c841715233b46a7f4374aa64fa36b8e8aa6 --- /dev/null +++ b/strmatch/rk/6f33a354-75c2-427b-b52f-c865126a9603.cpp @@ -0,0 +1,90 @@ +#include + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i0)? h2[i-1]: 0) + 1LL*p2[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9)) && + + ((1LL*hs2*p2[i-m+1])%int(1e9+9)) == (1LL*(h2[i]- ((i>=m)?h2[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/6fbbac4b-8be5-42be-90f2-8ae0ee22ab8a.cpp b/strmatch/rk/6fbbac4b-8be5-42be-90f2-8ae0ee22ab8a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6059933fb93be1f2d701338cdb07c2152496fcde --- /dev/null +++ b/strmatch/rk/6fbbac4b-8be5-42be-90f2-8ae0ee22ab8a.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int MAXN = 2000005, BASE = 256, MOD[2] = {2000003, 2000029}, MAX = 1000; + + + +char sub[MAXN], str[MAXN]; + +int res[MAX + 5], subH[2], strH[2], s, n, power[2], nr; + + + +void getHash() + +{ + + power[0] = power[1] = 1; + + for (int i = 0; i < s; ++i) { + + for (int k = 0; k < 2; ++k) + + subH[k] = (subH[k] * BASE + sub[i]) % MOD[k], + + strH[k] = (strH[k] * BASE + str[i]) % MOD[k]; + + if (i) + + for (int k = 0; k < 2; ++k) + + power[k] = (power[k] * BASE) % MOD[k]; + + } + +} + + + +void solve() + +{ + + if (subH[0] == strH[0] && subH[1] == strH[1]) + + res[nr++] = 0; + + for (int i = s; i < n; ++i) { + + strH[0] = ((strH[0] - (power[0] * str[i - s]) % MOD[0] + MOD[0]) * BASE + str[i]) % MOD[0]; + + strH[1] = ((strH[1] - (power[1] * str[i - s]) % MOD[1] + MOD[1]) * BASE + str[i]) % MOD[1]; + + if (subH[0] == strH[0] && subH[1] == strH[1]) { + + if (nr >= MAX) + + ++nr; + + else + + res[nr++] = i - s + 1; + + } + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + nr = min(nr, MAX); + + for (int i = 0; i < nr; ++i) + + fout << res[i] << ' '; + +} + + + +int main() + +{ + + fin >> sub >> str; + + n = strlen(str); + + s = strlen(sub); + + getHash(); + + solve(); + + print(); + + return 0; + +} diff --git a/strmatch/rk/7065f836-6136-4979-9b51-3d08eacd3345.cpp b/strmatch/rk/7065f836-6136-4979-9b51-3d08eacd3345.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1721ad3b4d428822de4943742699c5ea636c813c --- /dev/null +++ b/strmatch/rk/7065f836-6136-4979-9b51-3d08eacd3345.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/7078f264-0844-4491-a32d-d4c249811904.cpp b/strmatch/rk/7078f264-0844-4491-a32d-d4c249811904.cpp new file mode 100644 index 0000000000000000000000000000000000000000..120612ca9d0e088eefb413da888e2caeff82018a --- /dev/null +++ b/strmatch/rk/7078f264-0844-4491-a32d-d4c249811904.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int i, lg_a, lg_b, r[2000003]; + +int Hash_a1, Hash_a2, Hash1, Hash2, P1 = 1, P2 = 1; + +char a[2000003], b[2000003]; + + + +int main() + +{ + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + Hash_a1 = ( Hash_a1 * 80 + ( a[i] - '0' + 1 ) ) % 100003; + + Hash_a2 = ( Hash_a2 * 80 + ( a[i] - '0' + 1 ) ) % 100019; + + + + Hash1 = ( Hash1 * 80 + ( b[i] - '0' + 1 ) ) % 100003; + + Hash2 = ( Hash2 * 80 + ( b[i] - '0' + 1 ) ) % 100019; + + + + if ( i != 0 ) + + { + + P1 = ( P1 * 80 ) % 100003; + + P2 = ( P2 * 80 ) % 100019; + + } + + } + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = 0; + + + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + Hash1 = ( ( ( Hash1 - ( ( b[i-lg_a] - '0' + 1 ) * P1 ) % 100003 + 100003 ) * 80 ) % 100003 + ( b[i] - '0' + 1 ) ) % 100003; + + Hash2 = ( ( ( Hash2 - ( ( b[i-lg_a] - '0' + 1 ) * P2 ) % 100019 + 100019 ) * 80 ) % 100019 + ( b[i] - '0' + 1 ) ) % 100019; + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = i - lg_a + 1; + + } + + + + fout << r[0] << '\n'; + + r[0] = min ( 1000, r[0] ); + + for ( i = 1 ; i <= r[0] ; i++ ) fout << r[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/71038d7a-af8c-441e-a74f-db1393d246d0.cpp b/strmatch/rk/71038d7a-af8c-441e-a74f-db1393d246d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..86cc26011e3f4dcf5383992c0ce2f1a854b4a911 --- /dev/null +++ b/strmatch/rk/71038d7a-af8c-441e-a74f-db1393d246d0.cpp @@ -0,0 +1,134 @@ +#include +#include +#include +using namespace std; + + + +char a[2000001], b[2000001]; + +int n, m, nr; + +int hash1, hash2, p1, p2, h1, h2; + +char match[2000001]; + + + +void citire() + +{ + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n %s", a, b); + + n=strlen(a); + + m=strlen(b); + +} + + + +void comparare() + +{ + + p1=p2=1; + + hash1=hash2=0; + + for (int i=0;i m) + + { + + cout<<0; + + return 0; + + } + + comparare(); + + afisare(); + + return 0; + +} diff --git a/strmatch/rk/7128aa3b-1b35-4178-82ad-f6dbf3caf922.cpp b/strmatch/rk/7128aa3b-1b35-4178-82ad-f6dbf3caf922.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e021bad38083eec8aa98951b79ffce769e7b47c8 --- /dev/null +++ b/strmatch/rk/7128aa3b-1b35-4178-82ad-f6dbf3caf922.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream gout("strmatch.out"); + + + +typedef unsigned long long LL; + + + + + +const unsigned long long baza = 1010012383; + +const unsigned long long m = 1010012359; + + + + + + + +LL rasp; + + + +vectorv; + + + +int main() + +{ + + string a, b; + + + + fin >> a >> b; + + if( a .size() > b.size() ) + + { + + gout <<0; + + return 0; + + } + + LL n = a.size(), i; + + LL vala = 0, valb = 0, put = 1; + + + + + + for( i = 0; i < n; ++i ) + + { + + vala = ( vala * baza + a[i]) % m; + + put = (put * baza )%m; + + } + + + + + + for( i = 0; i < n; ++i ) + + valb = ( valb * baza + b[i]) % m; + + + + + + if( vala == valb ) + + { + + ++rasp; + + v.push_back(0); + + } + for(i = 1; i <= b.size() - n; ++i ) + + { + + valb = ( valb * baza + b[i+n-1] ) % m; + + + + + + + + LL ceva = (put * b[i-1]) % m; + + valb = ( valb - ceva +m) % m; + + + + + + if( vala == valb ) + + { + + ++rasp; + + + + if(v.size() < 1000) + + v.push_back(i); + + + + } + + } + + gout < +#include +#include +#include + + + + +using namespace std ; + + + +const long long MOD1 = 1e9 + 7 ; + +const long long MOD2 = 1e9 + 9 ; + +const long long BASE = 91 ; + +vector < int > v ; + + + +int main () + +{ + + ifstream in ( "strmatch.in" ) ; + + ofstream out ( "strmatch.out" ) ; + + + + string inp1 , inp2 ; + + long long hash11 , hash12 , hash21 , hash22, n , m , nr = 0 ; + + long long p1 , p2 , i ; + + + + in >> inp1 >> inp2 ; + + n = inp1.size () ; + + m = inp2.size () ; + + + + if ( n > m ) + + { + + out << 0 ; + + return 0 ; + + } + + + + hash11 = hash12 = hash21 = hash22 = 0 ; + + p1 = p2 = 1 ; + + + + for ( i = 0 ; i < n ; ++ i ) + + { + + hash11 = ( hash11 * BASE + inp1 [ i ] ) % MOD1 ; + + hash12 = ( hash12 * BASE + inp1 [ i ] ) % MOD2 ; + + + + hash21 = ( hash21 * BASE + inp2 [ i ] ) % MOD1 ; + + hash22 = ( hash22 * BASE + inp2 [ i ] ) % MOD2 ; + + + + if ( i ) + + { + + p1 = ( p1 * BASE ) % MOD1 ; + + p2 = ( p2 * BASE ) % MOD2 ; + + } + + } + + + + if ( hash11 == hash21 && hash12 == hash22 ) + + v.push_back ( 0 ) , ++ nr ; + + + + for ( i = n ; i < m ; ++ i ) + + { + + hash21 = ( ( hash21 - ( 1LL * inp2 [ i - n ] * p1 ) % MOD1 + MOD1 ) * BASE + inp2 [ i ] ) % MOD1 ; + + hash22 = ( ( hash22 - ( 1LL * inp2 [ i - n ] * p2 ) % MOD2 + MOD2 ) * BASE + inp2 [ i ] ) % MOD2 ; + + + + if ( hash11 == hash21 && hash12 == hash22 ) + + { + + ++ nr ; + + if ( v.size () < 1000 ) + + v.push_back ( i - n + 1 ) ; + + } + + } + + + + out << nr << '\n' ; + + for ( i = 0 ; i < v.size () ; ++ i ) + + out << v [ i ] << ' ' ; + + + + return 0 ; + +} diff --git a/strmatch/rk/769f5d92-1eb2-4ea9-b72d-bf12c905770c.cpp b/strmatch/rk/769f5d92-1eb2-4ea9-b72d-bf12c905770c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fc97ca6b303c8ea9e64f996ee171393d1d08155 --- /dev/null +++ b/strmatch/rk/769f5d92-1eb2-4ea9-b72d-bf12c905770c.cpp @@ -0,0 +1,97 @@ +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s[2000001],s2[2000001]; + +int n,b=101,mod1=100007,mod2=100021,h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + +int main() { + + f>>s>>s2; + + n=strlen(s); + + m=strlen(s2); + + p1=1; + + p2=1; + + for(i=0; im) { + + g<<0<<'\n'; + + return 0; + + } + + for(i=0; i +#include +#include + +using namespace std; +const int NMAX = 2000003; + +char s[NMAX]; +char w[NMAX]; + +int mch=0; +bool poz[NMAX]; + +int main(){ + freopen("strmatch.in","r",stdin); + freopen("strmatch.out","w",stdout); + + + scanf("%s%s",w,s); + + int n=strlen(s); + int m=strlen(w); + + if(m>n){ + printf("0\n"); + return 0; + } + + int hashw1=0,hashw2=0; + int hashs1=0,hashs2=0; + + int p1=1,p2=1; + + + for(int i=0;i +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/7afca07b-27eb-4547-bda1-df8fe8e555a4.cpp b/strmatch/rk/7afca07b-27eb-4547-bda1-df8fe8e555a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95620a46ad3b4c46bdd8307dcb4f1acc28ba2d1c --- /dev/null +++ b/strmatch/rk/7afca07b-27eb-4547-bda1-df8fe8e555a4.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +using namespace std; +const int LMAX = 2000001; +const int MOD1 = 100007; +const int MOD2 = 100021; +const int p = 73; + +char a[LMAX], b[LMAX]; +bool positions[LMAX]; + +int main() +{ + FILE *fin, *fout; + int na,nb,i,hasha1,hasha2,p1,p2,hash1,hash2,nr; + fin = fopen("strmatch.in","r"); + fout = fopen("strmatch.out","w"); + fscanf(fin,"%s\n%s\n",a,b); + + na = strlen(a), nb = strlen(b); + hasha1 = hasha2 = 0; + p1 = p2 = 1; + for(i=0; i 0){ + p1 = (p1*p)%MOD1; + p2 = (p2*p)%MOD2; + } + } + + if(na > nb) + fprintf(fout,"0\n"); + else{ + hash1 = hash2 = 0; + for(i=0; i +#include + + +using namespace std; +char a[2000000 +1],b[2000000 +1]; + +int v[2000000 +1]; + + + +int deletecif(int cif,int pow){ + + return cif*pow; + +} + + + +int main() + +{ + + FILE *fin,*fout; + + fin=fopen("strmatch.in","r"); + + fout=fopen("strmatch.out","w"); + + + + int i,j,nra,sizea,sirurimatch,nrb,inceput,sfarsit,pow,sizeb,sizev,nrb2,nra2,pow2; + + + + fscanf(fin,"%s",a); + + fscanf(fin,"%s",b); + + + + sizea=strlen(a); + + nra=0; + + nra2=0; + + i=0; + + + + while(a[i]!='\0'){ + + nra=((nra*128)%666013 +a[i]%666013)%666013; + + nra2=((nra2*128)%666019 +a[i]%666019)%666019; + + i++; + + } + + + + nrb=0; + + nrb2=0; + + sizeb=0; + + j=0; + + + + while(sizeb + + + + + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000010],b[2000010]; + +int poz,i,pozitiea,pozitieb,pozitie,l1,l2,numar,p1,p2,hasha1,hashb1,hasha2,hashb2; + +int v[1010]; + +int main() + +{ + + fin>>a>>b; + + l1=strlen(a); + + l2=strlen(b); + + if(l1>l2) + + { + + fout<<0; + + return 0; + + } + + p1=p2=1; + + for(i=0;i0) + + { + + p1=(p1*73)%100007; + + p2=(p2*73)%100021; + + } + + } + + if(hasha1==hashb1 && hasha2==hashb2) + + v[++numar]=0; + + for(i=l1;i +using namespace std; + +const int P = 191; + +const int MOD1 = 10000007; + +const int MOD2 = 10000021; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +string a, b; + +int P1 = 1, P2 = 1, hash_a1, hash_a2, hash_b1, hash_b2, cnt, len_a, len_b; + +vector sol; + + + +int main() { + + fin >> a >> b; + + + + len_a = a.length(); + + len_b = b.length(); + + + + if (len_a > len_b) { + + fout << 0; + + return 0; + + } + + + + for (int i = 0; i < len_a; i++) { + + hash_a1 = (hash_a1 * P + a[i] - '0' + 1) % MOD1; + + hash_a2 = (hash_a2 * P + a[i] - '0' + 1) % MOD2; + + if (i) { + + P1 = P1 * P % MOD1; + + P2 = P2 * P % MOD2; + + } + + } + + + + for (int i = 0; i < len_a; i++) { + + hash_b1 = (hash_b1 * P + b[i] - '0' + 1) % MOD1; + + hash_b2 = (hash_b2 * P + b[i] - '0' + 1) % MOD2; + + } + + + + if (hash_a1 == hash_b1 && hash_a2 == hash_b2) { + + cnt++; + + sol.push_back(0); + + } + + + + for (int i = len_a; i < len_b; i++) { + + hash_b1 = ((hash_b1 - ((b[i - len_a] - '0' + 1) * P1 % MOD1) + MOD1) % MOD1 * P + b[i] - '0' + 1) % MOD1; + + hash_b2 = ((hash_b2 - ((b[i - len_a] - '0' + 1) * P2 % MOD2) + MOD2) % MOD2 * P + b[i] - '0' + 1) % MOD2; + + + + if (hash_a1 == hash_b1 && hash_a2 == hash_b2) { + + cnt++; + + if (cnt <= 1000) + + sol.push_back(i - len_a + 1); + + } + + } + + + + fout << cnt << '\n'; + + for (int i : sol) + + fout << i << ' '; + + return 0; + +} diff --git a/strmatch/rk/807c70e1-e90e-44a3-a2f8-ff756e66c09f.cpp b/strmatch/rk/807c70e1-e90e-44a3-a2f8-ff756e66c09f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cf2753a5678fb06a0fed6cfb8418d5be45edbb4b --- /dev/null +++ b/strmatch/rk/807c70e1-e90e-44a3-a2f8-ff756e66c09f.cpp @@ -0,0 +1,112 @@ +#include + + + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +long long pro1,pro2,val1,val2,val3,val4,n,n2,i,nrr,nrr2; + +char c,a[2000005]; + +int main() + +{ + + pro1=pro2=1; + +while (true) + +{ + + in.get(c); + + if (c=='\n') break; + + val1=(67*val1+c)%(85636124); + + val2=(67*val2+c)%(93579235); + + if (n!=0){ + + pro1=67*pro1%(85636124); + + pro2=67*pro2%(93579235);} + + n++; + +} + +while (true) + +{ + + in.get(a[i++]); + + if (a[i-1]=='\n') break; + + if (i<=n) {val3=(67*val3+a[i-1])%(85636124); + + val4=(67*val4+a[i-1])%(93579235); + + } + + else {if (val1==val3&&val2==val4) nrr++; + + val3=(67*(val3-pro1*a[i-n-1])%(85636124)+(85636124)+a[i-1])%(85636124); + + val4=(67*(val4-pro2*a[i-n-1])%(93579235)+(93579235)+a[i-1])%(93579235);} + +} + +if (val1==val3&&val2==val4) nrr++; + +out<1000) return 0; + + out<1000) return 0; + + out< +#include +#include +#include + + + + + + +using namespace std; + + + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +char s1[2000005], s2[2000005]; + +int nr=0, sol[2000005]; + + + +struct Hash{ + + long long n, m, power, hashh; + + void init(char *s, long long len) + + { + + power=1; + + hashh=0; + + for(long long i=len-1; i>=0; i--) + + { + + hashh=(hashh+(1LL*power*s[i])%m)%m; + + if(i) + + power=(power*n)%m; + + } + + } + + + + void roll(char toRemove, char toAdd) + + { + + hashh=(((hashh-(1LL*toRemove*power)%m+m)*n)%m+toAdd)%m; + + } + + + + + +}; + + + +int main() + +{ + + int l1, l2; + + f.getline(s1, 2000005); + + f.getline(s2, 2000005); + + l1=strlen(s1); + + l2=strlen(s2); + + Hash pHash{31,40099},tHash{31,40099}; + + Hash hHash{53,319993},oHash{53,319993}; + + long long nr=0; + + pHash.init(s1,l1); + + tHash.init(s2,l1); + + hHash.init(s1,l1); + + oHash.init(s2,l1); + + if(pHash.hashh==tHash.hashh && hHash.hashh==oHash.hashh) + + { + + sol[++nr]=0; + + } + + for(long long i=l1;i=1000) + + nr=1000; + + for(int i=1; i<=nr;i++) + + g< +#include +#include +char a[2000001], b[2000001], ok[2000001]; + + + +int i, j, na, nb, ns1, ns2, nsa1, nsa2, p1, p2, nr; + + + +using namespace std; + + + +int main() { + + + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin.getline(a, 2000001); + + fin.getline(b, 2000001); + + na=strlen(a); + + nb=strlen(b); + + p1=p2=1; + + nsa1=nsa2=0; + + for(i=0;inb) { + + fout<<"0"; + + } + + else { + + ns1=ns2=0; + + for(i=0;i +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int i, lg_a, lg_b, r[2000003]; + +int Hash_a1, Hash_a2, Hash1, Hash2, P1 = 1, P2 = 1; + +char a[2000003], b[2000003]; + + + +int main() + +{ + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + Hash_a1 = ( Hash_a1 * 80 + ( a[i] - '0' + 1 ) ) % 100003; + + Hash_a2 = ( Hash_a2 * 80 + ( a[i] - '0' + 1 ) ) % 100019; + + + + Hash1 = ( Hash1 * 80 + ( b[i] - '0' + 1 ) ) % 100003; + + Hash2 = ( Hash2 * 80 + ( b[i] - '0' + 1 ) ) % 100019; + + + + if ( i != 0 ) + + { + + P1 = ( P1 * 80 ) % 100003; + + P2 = ( P2 * 80 ) % 100019; + + } + + } + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = 0; + + + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + Hash1 = ( ( ( Hash1 - ( ( b[i-lg_a] - '0' + 1 ) * P1 ) % 100003 + 100003 ) * 80 ) % 100003 + ( b[i] - '0' + 1 ) ) % 100003; + + Hash2 = ( ( ( Hash2 - ( ( b[i-lg_a] - '0' + 1 ) * P2 ) % 100019 + 100019 ) * 80 ) % 100019 + ( b[i] - '0' + 1 ) ) % 100019; + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = i - lg_a + 1; + + } + + + + fout << r[0] << '\n'; + + r[0] = min ( 1000, r[0] ); + + for ( i = 1 ; i <= r[0] ; i++ ) fout << r[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/81180745-df4b-41f3-aa8f-5fa6f61efa3a.cpp b/strmatch/rk/81180745-df4b-41f3-aa8f-5fa6f61efa3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..296a729f304ec63e1e1d70ae488ecdbbba112428 --- /dev/null +++ b/strmatch/rk/81180745-df4b-41f3-aa8f-5fa6f61efa3a.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + + + + + +char pat[2000001],sir[2000001]; + +int poz[2000001]; + +int main() + +{ + + int m1=666013; + + int m2=800057; + + int p=72,p1,p2,h1,h2,la,lb,i,s,H1,H2; + + cin>>pat; + + cin>>sir; + + la=strlen(pat); + + lb=strlen(sir); + + p1=p2=1; + + h1=h2=0; + + for(i=0;i=1){ + + p1=(p1*p)%m1; + + p2=(p2*p)%m2; + + } + + } + + if(la>lb){ + + cout<<0<<'\n'; + + return 0; + + } + + H1=H2=0; + + for(i=0;i + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int MAXN = 2000005, BASE = 256, MOD[2] = {2000003, 2000029}, MAX = 1000; + + + +char sub[MAXN], str[MAXN]; + +int res[MAX + 5], subH[2], strH[2], s, n, power[2], nr; + + + +void getHash() + +{ + + power[0] = power[1] = 1; + + for (int i = 0; i < s; ++i) { + + for (int k = 0; k < 2; ++k) + + subH[k] = (subH[k] * BASE + sub[i]) % MOD[k], + + strH[k] = (strH[k] * BASE + str[i]) % MOD[k]; + + if (i) + + for (int k = 0; k < 2; ++k) + + power[k] = (power[k] * BASE) % MOD[k]; + + } + +} + + + +void solve() + +{ + + if (subH[0] == strH[0] && subH[1] == strH[1]) + + res[nr++] = 0; + + for (int i = s; i < n; ++i) { + + for (int k = 0; k < 2; ++k) + + strH[k] = ((strH[k] - (power[k] * str[i - s]) % MOD[k] + MOD[k]) * BASE + str[i]) % MOD[k]; + + if (subH[0] == strH[0] && subH[1] == strH[1]) { + + if (nr >= MAX) + + ++nr; + + else + + res[nr++] = i - s + 1; + + } + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + nr = min(nr, MAX); + + for (int i = 0; i < nr; ++i) + + fout << res[i] << ' '; + +} + + + +int main() + +{ + + fin >> sub >> str; + + n = strlen(str); + + s = strlen(sub); + + getHash(); + + solve(); + + print(); + + return 0; + +} diff --git a/strmatch/rk/81e0c3ec-8e79-4c07-955f-87db6b4bb6ce.cpp b/strmatch/rk/81e0c3ec-8e79-4c07-955f-87db6b4bb6ce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ceb4356cb09d8a8dd89c82c16601b2b80c27165 --- /dev/null +++ b/strmatch/rk/81e0c3ec-8e79-4c07-955f-87db6b4bb6ce.cpp @@ -0,0 +1,161 @@ +#include + + + + + + + +using namespace std; + + + +ifstream f("strmatch" ".in"); + +ofstream g("strmatch" ".out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef pair pii; + +typedef pair pct; + + + +const ll mod = 1e9 + 7; + +const ld eps = 1e-9; + + + + + +void add(ll &a , ll b) + +{ + + a += b; + + a %= mod; + +} + + + +void sub(ll &a, ll b) + +{ + + a = (a - b + mod) % mod; + +} + + + +string txt,pat; + +int pi[1000010], d[1000010],ans,cat; + +vector sol; + +void compute_pattern(string a,int pi[]) + +{ + + int k = 0; + + int n = a.size() - 1; + + pi[1] = 0; + + for(int i = 2; i <= n; ++i) + + { + + while( k > 0 && a[i] != a[k + 1] ) k = pi[k]; + + if(a[i] == a[k + 1]) k++; + + pi[i] = k; + + } + +} + + + +void find_pattern(string y, string pat,int pi[], int d[]) + +{ + + compute_pattern(pat, pi); + + int m = y.size() - 1; + + int n = pat.size() - 1; + + int k = 0; + + for(int i = 1; i <= m ; ++i) + + { + + while( (k > 0) && (y[i] != pat[k + 1]) ) k = pi[k]; + + if(y[i] == pat[k+1]) k++; + + if(k == pat.size() - 1) + + { + + cat++; + + if(cat <= 1000) + + sol.push_back(i - n); + + } + + } + +} + +int main() + +{ + + f >> pat >> txt ; + + txt = ' ' + txt; + + pat = ' ' + pat; + + find_pattern(txt, pat, pi, d); + + g << cat << '\n'; + + for(auto it : sol) + + { + + g << it << ' '; + + cat--; + + if(cat == 0) + + break; + + } + + f.close(); + + g.close(); + + return 0; + +} diff --git a/strmatch/rk/822a0bea-5c5e-468b-a5df-71c239c2485b.cpp b/strmatch/rk/822a0bea-5c5e-468b-a5df-71c239c2485b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c52e09050ea80af1ba86fc78ac90567179e5b3f1 --- /dev/null +++ b/strmatch/rk/822a0bea-5c5e-468b-a5df-71c239c2485b.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +ifstream r("strmatch.in"); + +ofstream w("strmatch.out"); + +const int base=73, mod1=100007, mod2=100021; + +string a, b; + +int n, m, h1, h2, x, y; + +char match[2000001]; + +int main() + +{ + + r>>a>>b; + + n = a.size(); + + m = b.size(); + + x = y = 1; + + h1 = h2 = 0; + + for (int i = 0; i < n; i++) + + { + + h1 = (h1 * base + a[i]) % mod1; + + h2 = (h2 * base + a[i]) % mod2; + + if (i != 0) + + { + + x = (x * base) % mod1, + + y = (y * base) % mod2; + + } + + } + + + + if (n > m) + + { + + w<<0; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < n; i++) + + { + + hash1 = (hash1 * base + b[i]) % mod1; + + hash2 = (hash2 * base + b[i]) % mod2; + + } + + int nr = 0; + + if (hash1 == h1 && hash2 == h2) + + match[0] = 1, nr++; + + + + for (int i = n; i < m; i++) + + { + + hash1 = ((hash1 - (b[i - n] * x) % mod1 + mod1) * base + b[i]) % mod1; + + hash2 = ((hash2 - (b[i - n] * y) % mod2 + mod2) * base + b[i]) % mod2; + + + + if (hash1 == h1 && hash2 == h2) + + { + + match[ i - n + 1 ] = 1; + + nr++; + + } + + } + + w< +#include +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int MOD=1000000007; + +const int baza=597813; + + + +string n,m; + +int nrN,nrM; + +vector ind; + + + +void read() + +{ + + fin>>n>>m; + + nrN=n.length(); + + nrM=m.length(); + +} + + + +void solve() + +{ + + int_fast64_t hashN=0,hashM=0,baseNrN=1; + + + + for(int i=1;inrM) + + { + + fout<<0; + + return 0; + + } + + + + solve(); + + + + return 0; + +} diff --git a/strmatch/rk/83d9143e-d4fe-481b-8ee6-753c69e26627.cpp b/strmatch/rk/83d9143e-d4fe-481b-8ee6-753c69e26627.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4172ea69b11a2017283f58233f81664006fa27 --- /dev/null +++ b/strmatch/rk/83d9143e-d4fe-481b-8ee6-753c69e26627.cpp @@ -0,0 +1,125 @@ +#include +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/841af5d5-a6c2-437b-a17c-a2e0ce1c414e.cpp b/strmatch/rk/841af5d5-a6c2-437b-a17c-a2e0ce1c414e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28110248183f7ad5292b82fd568a7ba17ea1e857 --- /dev/null +++ b/strmatch/rk/841af5d5-a6c2-437b-a17c-a2e0ce1c414e.cpp @@ -0,0 +1,146 @@ +#include + + +const int mod=1000000007; + + + +const int prim=666013; + + + +using namespace std; + + + +string str1,str2; + + + +unsigned int nr; + + + +queue ans; + + + +int main() + +{ + + ifstream fin ("strmatch.in"); + + ofstream fout ("strmatch.out"); + + fin>>str1>>str2; + + if (str1.length()>str2.length()) + + { + + fout<<0; + + return 0; + + } + + int_fast64_t ct=1,hash1=0,chash=0; + + for (int i=1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +const long long A=914530549; + +const long long B=914531357; + +int main() + +{ + + long long rasp=0; + + string a,b; + + cin>>a>>b; + + if(a.size()>b.size()) + + { + + cout<<0<<'\n'; + + return 0; + + } + + long long n=a.size(),ka=0,kb=0,power=1; + + for(long long i=0;i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int P = 62; + +const int NMAX = 2e6 + 7; + +const int m1 = 1e5 + 7; + +const int m2 = 1e5 + 21; + + + +int N,M,P1,P2,hashA1,hashA2,hash1,hash2; + +vector a; + +char A[NMAX],B[NMAX]; + + + +int main() + +{ + + + + fin >> A >> B; + + N=strlen(A), M=strlen(B); + + + + P1=1, P2=1; + + for(int i=0;i +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + +char a[2000005], b[2000005]; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + a[i] - '0' + 1 ) % 45007; + + hash2a = ( hash2a * 80 + a[i] - '0' + 1 ) % 45013; + + + + hash1 = ( hash1 * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( hash2 * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/88b86e0e-75bf-46de-8f83-a22f02a5aace.cpp b/strmatch/rk/88b86e0e-75bf-46de-8f83-a22f02a5aace.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e89a42e52dabf1fba638e657c604dca4c3526a38 --- /dev/null +++ b/strmatch/rk/88b86e0e-75bf-46de-8f83-a22f02a5aace.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + +char a[2000005], b[2000005]; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + a[i] - '0' + 1 ) % 45007; + + hash2a = ( hash2a * 80 + a[i] - '0' + 1 ) % 45013; + + + + hash1 = ( hash1 * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( hash2 * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/8922d93a-d58d-4633-a3dd-39d3fc232367.cpp b/strmatch/rk/8922d93a-d58d-4633-a3dd-39d3fc232367.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e56745bd95345bf2423fbe6d3ac56d0712546081 --- /dev/null +++ b/strmatch/rk/8922d93a-d58d-4633-a3dd-39d3fc232367.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +string a,b; + +unsigned int baz=71; + +unsigned int ha[2000001],puteri[2000001],hasha; + +int rez[2000001],cont; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + getline(cin,a); + + getline(cin,b); + + for(int i=a.size()-1; i>=0; --i) + + hasha=(hasha*baz+a[i]); + + for(int i=b.size()-1; i>=0; --i) + + ha[i]=(ha[i+1]*baz+b[i]); + + puteri[0]=1; + + for(int i=1; i<=b.size(); ++i) + + puteri[i]=(puteri[i-1]*baz); + + for(int i=a.size()-1; i + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/8cc11600-6e7a-40d7-97cf-5257f2777c4e.cpp b/strmatch/rk/8cc11600-6e7a-40d7-97cf-5257f2777c4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4172ea69b11a2017283f58233f81664006fa27 --- /dev/null +++ b/strmatch/rk/8cc11600-6e7a-40d7-97cf-5257f2777c4e.cpp @@ -0,0 +1,125 @@ +#include +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/9070d901-2d8e-4f81-9add-d1e1844fcac1.cpp b/strmatch/rk/9070d901-2d8e-4f81-9add-d1e1844fcac1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc107078cdcdca7bef63e7ad137e314163758ab6 --- /dev/null +++ b/strmatch/rk/9070d901-2d8e-4f81-9add-d1e1844fcac1.cpp @@ -0,0 +1,56 @@ +#include + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +unsigned long long B=127; + +string a,b; +unsigned long long hashA,hashB,powe; +unsigned long long sol; +vector ans; +void make_hash() +{ + powe=1; + for(unsigned long long i=0;i>a>>b; + if(a.size()>b.size()) + { + out<<"0"; + return 0; + } + make_hash(); + if(hashA == hashB) + { + sol++; + ans.push_back(0); + } + for(unsigned int i=1;i<=b.size()-a.size();i++) + { + hashB=(hashB * B+b[i+a.size()-1]); + unsigned long long aux=(powe * b[i-1]); + hashB=(hashB-aux); + if(hashA==hashB) + { + sol++; + if(ans.size()<=1000) + ans.push_back(i); + } + } + out< +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +char A[2000001], B[2000001]; + +int NA, NB; + +int hashA1, hashA2, P1, P2; + +char match[2000001]; + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/91e75fe3-65bc-4990-a2af-baab6ecaddbd.cpp b/strmatch/rk/91e75fe3-65bc-4990-a2af-baab6ecaddbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4f8d088b9cb37e1187d98de873a59f8406cc390 --- /dev/null +++ b/strmatch/rk/91e75fe3-65bc-4990-a2af-baab6ecaddbd.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +char a[2000001], b[2000001], ok[2000001]; + + + +int i, j, na, nb, ns1, ns2, nsa1, nsa2, p1, p2, nr; + + + +using namespace std; + + + +int main() { + + + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin.getline(a, 2000001); + + fin.getline(b, 2000001); + + na=strlen(a); + + nb=strlen(b); + + p1=p2=1; + + nsa1=nsa2=0; + + for(i=0;inb) { + + fout<<"0"; + + } + + else { + + ns1=ns2=0; + + for(i=0;i +#include +#include + + + + + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +long long int x1=1,x2=1,mod,H1,H2,h1,h2; + +vectorv; + +string a,b; + +void logpow(long long int &oof,long long int exp,long long int mod) + +{ + + long long int rez=1,okei=oof; + + while(exp) + + { + + if(exp%2==1) + + rez*=okei; + + rez%=mod; + + okei*=okei; + + okei%=mod; + + exp/=2; + + } + + oof=rez; + +} + +void afis(vector v) + +{ + + cout<>a>>b; + + for(int i=0;i +#include +#include +#include +#include + + + + + +using namespace std; + +class rollingHash{ +private: long long key; + long long maxpower; + long long n; +public: + long long val; + bool operator== (const rollingHash &other) const { + return (this->val == other.val && this->key == other.key && this->n == other.n); + } + void assignKeys(long long key, long long n){ + this->key = key; + this->n = n; + } + void buildInitialHash(string x){ + this->val = 0; + long long p = 1; + for (int i = x.length() - 1; i >= 0; --i) { + this->val = (this->val + ((p * x[i]) % this->n)) % this->n; + this->maxpower = p; + p = (p * this->key) % this->n; + } + } + void rollHash(char out, char in){ + this->val = ((this->val - (out * this->maxpower) % this->n + this->n) * this->key + in) % this->n; + } +}; + +int main() { + ifstream f("strmatch.in"); + ofstream g("strmatch.out"); + string a, b; + f>>a>>b; + rollingHash h1a, h2a; + h1a.assignKeys(31, 9153637); + h2a.assignKeys(37, 9231329); + h1a.buildInitialHash(a); + h2a.buildInitialHash(a); + vector matches; + rollingHash h1b, h2b; + h1b.assignKeys(31, 9153637); + h2b.assignKeys(37, 9231329); + h1b.buildInitialHash(b.substr(0, a.length())); + h2b.buildInitialHash(b.substr(0, a.length())); + for (int i = a.length(); i < b.length(); ++i) { + if(h1a == h1b && h2a == h2b) matches.push_back(i - a.length()); + h1b.rollHash(b[i - a.length()], b[i]); + h2b.rollHash(b[i - a.length()], b[i]); + } + if(h1a == h1b && h2a == h2b) matches.push_back(b.length() - a.length()); + g< +using namespace std; + +const int M1 = 1e9 + 7; + +const int M3 = 1e9 + 21; + +const int P = 73; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +struct hashh { + + long long h1, h3; + +}; + +hashh hashA, hashB; + +bool verif() { + + if (hashA.h1 == hashB.h1 && hashA.h3 == hashB.h3) { + + return true; + + } + + return false; + +} + +vector < int > ans; + +int main() { + + string a, b; + + long long P1 = 1, P2 = 1, P3 = 1; + + hashA.h1 = hashA.h3 = hashB.h1 = hashB.h3 = 0; + + in >> a >> b; + + for (int i = 1; i <= a.size() - 1; i++) { + + P1 = (P1 * P) % M1; + + P3 = (P3 * P) % M3; + + } + + if (a.size() > b.size()) { + + out << "0\n"; + + return 0; + + } + + for (int i = 0; i < a.size(); i++) { + + hashA.h1 = (hashA.h1 * P + a[i]) % M1; + + hashA.h3 = (hashA.h3 * P + a[i]) % M3; + + hashB.h1 = (hashB.h1 * P + b[i]) % M1; + + hashB.h3 = (hashB.h3 * P + b[i]) % M3; + + } + + if (verif()) { + + ans.push_back(0); + + } + + for (int i = a.size(); i < b.size(); i++) { + + hashB.h1 = (hashB.h1 - ((b[i - a.size()] * P1) % M1) + M1); + + hashB.h3 = (hashB.h3 - ((b[i - a.size()] * P3) % M3) + M3); + + hashB.h1 = (hashB.h1 * P + b[i]) % M1; + + hashB.h3 = (hashB.h3 * P + b[i]) % M3; + + if (verif()) { + + ans.push_back(i - a.size() + 1); + + } + + } + + out << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + out << ans[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/rk/96dd0aa4-f980-47b9-a940-ef971d0d7671.cpp b/strmatch/rk/96dd0aa4-f980-47b9-a940-ef971d0d7671.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0701463dedff1d5a9b8eab5916ce0b9874ba3333 --- /dev/null +++ b/strmatch/rk/96dd0aa4-f980-47b9-a940-ef971d0d7671.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const long long A = 1763922631; + +const long long B = 1617924853; + +const long long C = 666013; + + + +vectorsol; + + + +int main() + +{ + + long long rasp = 0, put, x, y; + + string a, b; + + fin >> a >> b; + + if (a.size() > b.size()) + + { + + fout << 0; + + return 0; + + } + + + + long long n = a.size(), i, j, m = b.size(); + + long long valb = 0, vala = 0; + + long long val1a = 0, val1b = 0; + + put = 1; + + long long put1 = 1; + + + + for (i = 0; i < n; ++i) + + { + + vala = (vala * A + a[i]) % B; + + val1a = (val1a * A + a[i]) % C; + + put = (put * A) % B; + + put1 = (put1 * A) % C; + + } + + for (i = 0; i < n; ++i) { + + valb = (valb * A + b[i]) % B; + + val1b = (val1b * A + b[i]) % C; + + } + + + + if (vala == valb && val1a == val1b) + + ++rasp, sol.push_back(0); + + + + for (i = 1; i <= m - n; ++i) + + { + + valb = (valb * A + b[i + n - 1]) % B; + + val1b = (val1b * A + b[i + n - 1]) % C; + + x = put * b[i - 1]; + + y = put1 * b[i - 1]; + + valb = (((valb - x) % B) + B) % B; + + val1b = (((val1b - y) % C) + C) % C; + + if (vala == valb && val1a == val1b) + + { + + ++rasp; + + sol.push_back(i); + + } + + } + + + + fout << rasp << endl; + + for (int i = 0; i < min((int)sol.size(), 1000); i++) + + fout << sol[i] << " "; + + fout << "\n"; + + + + return 0; + +} diff --git a/strmatch/rk/9b76a081-f12e-42cb-84a9-5bcf425440d9.cpp b/strmatch/rk/9b76a081-f12e-42cb-84a9-5bcf425440d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41077150738f1b1b4c735675ba0b9262f4d700cc --- /dev/null +++ b/strmatch/rk/9b76a081-f12e-42cb-84a9-5bcf425440d9.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 2e6 + 5; + +const long long MOD = 1000000123; + +const long long BASE = 62; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +long long h2[NMAX]; + + + +long long key[150]; + + + +void FormKey() + +{ + + int cnt = 0; + + for(char c = '0'; c <= '9'; ++c) { + + key[c] = ++cnt; + + } + + for(char c = 'a'; c <= 'z'; ++c) { + + key[c] = ++cnt; + + } + + for(char c = 'A'; c <= 'Z'; ++c) { + + key[c] = ++cnt; + + } + + + +} + +int main() + +{ + + FormKey(); + + string a, b; + + cin >> a >> b; + + long long h1 = 0, h2 = 0; + + long long p = 1, pk = 1; + + for(auto x: a) { + + + + h1 = (h1 * BASE + key[x]) % MOD; + + pk = p; + + p = (p * BASE) % MOD; + + } + + p = 1; + + int cnt = 0; + + vector sol; + + for(int i = 0; i < a.size(); ++i) { + + h2 = (h2 * BASE + key[b[i]]) % MOD; + + } + + if(h1 == h2) { + + cnt++; + + sol.emplace_back(0); + + } + + for(int i = a.size(); i < b.size(); ++i) { + + h2 = h2 - key[b[i - a.size()]] * pk % MOD; + + if(h2 < 0) + + h2 += MOD; + + h2 = (h2 * BASE % MOD + key[b[i]]) % MOD; + + if(h1 == h2) { + + cnt++; + + if(cnt <= 1e3) { + + sol.emplace_back(i - a.size() + 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) + + cout << x << " "; + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/rk/9d78f61e-447c-40a0-8d49-ca248bb78d94.cpp b/strmatch/rk/9d78f61e-447c-40a0-8d49-ca248bb78d94.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31b51d4f2204f929320c06528ed2815c0e7c905e --- /dev/null +++ b/strmatch/rk/9d78f61e-447c-40a0-8d49-ca248bb78d94.cpp @@ -0,0 +1,136 @@ +#include +#include +using namespace std; + + + +char A[2000001], B[2000001]; +int main() + +{ + + int result[1000]; + + + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + + + int M = strlen(A); + + int N = strlen(B); + + + + + + + + int p = 1, p2 = 1, n = 0; + + int aVal = 0; + + int aVal2 = 0; + + + + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + + + for (int i = 0; i < M; i++) + + { + + aVal = (aVal * 73 + A[i]) % 100007; + + aVal2 = (aVal2 * 73 + A[i]) % 100011; + + hash1 = (hash1 * 73 + B[i]) % 100007; + + hash2 = (hash2 * 73 + B[i]) % 100011; + + if (i != 0) + + { + + p = (p * 73) % 100007; + + p2 = (p2 * 73) % 100011; + + } + + } + + + + if (hash1 == aVal && hash2 == aVal2) + + { + + n++; + + result[0] = 0; + + } + + + + for (int i = M; i < N; i++) + + { + + hash1 = ((hash1 - (B[i - M] * p) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - M] * p2) % 100011 + 100011) * 73 + B[i]) % 100011; + + if (hash1 == aVal && hash2 == aVal2) + + { + + if(n < 1000) + + result[n] = (i - M + 1); + + n++; + + } + + } + + + + printf("%d\n", n); + + for (int i = 0; i < ((n < 1000) ? n : 1000); i++) + + { + + printf("%d ", result[i]); + + } + + + + return 0; + +} diff --git a/strmatch/rk/9eb41065-42af-433b-b3e1-d552c4067321.cpp b/strmatch/rk/9eb41065-42af-433b-b3e1-d552c4067321.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22709543639b3803574d88085345a77cc4f766ef --- /dev/null +++ b/strmatch/rk/9eb41065-42af-433b-b3e1-d552c4067321.cpp @@ -0,0 +1,247 @@ +#include +#include +char A[2000001], B[2000001]; + + + +int NA, NB; + + + + + + + +int hashA1, hashA2, P1, P2; + + + + + + + +char match[2000001]; + + + + + + + +int main() + + + +{ + + + + freopen("strmatch.in", "rt", stdin); + + + + freopen("strmatch.out", "wt", stdout); + + + + + + + + scanf("%s %s", A, B); + + + + NA = strlen(A); + + + + NB = strlen(B); + + + + + + + + P1 = P2 = 1; + + + + hashA1 = hashA2 = 0; + + + + for (int i = 0; i < NA; i++) + + + + { + + + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + + + + + if (i != 0) + + + + P1 = (P1 * 73) % 100007, + + + + P2 = (P2 * 73) % 100021; + + + + } + + + + + + + + if (NA > NB) + + + + { + + + + printf("0\n"); + + + + return 0; + + + + } + + + + + + + + int hash1 = 0, hash2 = 0; + + + + for (int i = 0; i < NA; i++) + + + + hash1 = (hash1 * 73 + B[i]) % 100007, + + + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + + + + + int Nr = 0; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + + + match[0] = 1, Nr++; + + + + + + + + for (int i = NA; i < NB; i++) + + + + { + + + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + + + + + if (hash1 == hashA1 && hash2 == hashA2) + + + + match[ i - NA + 1 ] = 1, Nr++; + + + + } + + + + printf("%d\n", Nr); + + + + + + + + Nr = 0; + + + + for (int i = 0; i < NB && Nr < 1000; i++) + + + + if (match[i]) + + + + Nr++, + + + + printf("%d ", i); + + + + printf("\n"); + + + + + + + + return 0; + + + +} diff --git a/strmatch/rk/a2463a16-6148-4f0e-92d4-59a55a189d95.cpp b/strmatch/rk/a2463a16-6148-4f0e-92d4-59a55a189d95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..198d6d6a4b71af8199f633418f5b9f898d048cdf --- /dev/null +++ b/strmatch/rk/a2463a16-6148-4f0e-92d4-59a55a189d95.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); +vector ans; + + + + + + + +void rabin_karp(string const& s, string const& t) { + + const long long p = 167; + + int S = s.size(), T = t.size(); + + + + unsigned long long pow = 1; + + for (auto x : s) pow *= p; + + + + vector h(T + 1); + + for (int i = 0; i < T; i++) + + h[i + 1] = h[i] * p + t[i]; + + long long h_s = 0; + + for (int i = 0; i < S; i++) + + h_s = h_s * p + s[i]; + + + + vector occurences; + + for (int i = 0; i + S - 1 < T; i++) { + + long long cur_h = h[i + S] - h[i] * pow; + + if (cur_h == h_s) + + ans.push_back(i); + + } + +} + + + + + + + +int main() { + + + + string a, b; cin >> a >> b; + + + + + + rabin_karp(a, b); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + + + return 0; + +} diff --git a/strmatch/rk/a2ded24f-05c7-4937-a781-7a6300d00568.cpp b/strmatch/rk/a2ded24f-05c7-4937-a781-7a6300d00568.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68d8799ced931d3204adc18881fe735abe3ee81b --- /dev/null +++ b/strmatch/rk/a2ded24f-05c7-4937-a781-7a6300d00568.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + + + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out" ); + + + +vector searchh ( string pat, string s ) + +{ + + int mod1 = 100007, mod2 = 100021, d = 73, h1 = 1, h2 = 1, p1 = 0, p2 = 0, t1 = 0, t2 = 0, i; + + + + vector < int > sol; + + + + int m = pat.size(); + + int n = s.size(); + + + + for ( i = 0 ; i < m-1 ; i++ ) + + { + + h1 = (h1*d) % mod1; + + h2 = (h2*d) % mod2; + + } + + + + for ( i = 0 ; i < m; i++ ) + + { + + p1 = (p1 * d + pat[i]) % mod1; + + p2 = (p2 * d + pat[i]) % mod2; + + t1 = (t1 * d + s[i]) % mod1; + + t2 = (t2 * d + s[i]) % mod2; + + } + + + + + + if ( p1 == t1 && p2 == t2 ) + + sol.push_back(0); + + + + for ( i = m ; i < n ; i++ ) + + { + + t1 = ((t1 - (s[i-m]*h1)%mod1 +mod1 ) * d + s[i]) % mod1; + + t2 = ((t2 - (s[i-m]*h2)%mod2 +mod2 ) * d + s[i]) % mod2; + + + + if ( t1 == p1 && t2 == p2 ) + + sol.push_back(i-m+1); + + } + + + + + + + + return sol; + + + + + +} + + + +int main () + +{ + + + + string a, b; + + + + vector sol; + + + + f >> a >> b; + + + + if (a.size() > b.size()) + + { + + g<<0; + + return 0; + + } + + + + sol = searchh ( a, b ); + + + + g << sol.size() << '\n'; + + + + for ( int i = 0 ; i < sol.size() && i < 1000 ; i++ ) + + g< +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/a6f75857-6f45-468e-8b5f-a1c44ba638b4.cpp b/strmatch/rk/a6f75857-6f45-468e-8b5f-a1c44ba638b4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae31415f4ce6eb687d4441d194b12cea84798c70 --- /dev/null +++ b/strmatch/rk/a6f75857-6f45-468e-8b5f-a1c44ba638b4.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string cuv,sir; + +unsigned long long Hcuv,A=915358133,B=974328597; + +unsigned long long h[2000001],putere[2000001]; + +int sol[1001],nr; + + + +int main() + +{ + + in>>cuv>>sir; + + + + for(auto lit:cuv) + + Hcuv=( Hcuv*A+lit )%B; + + + + h[0]=sir[0]%B; + + putere[0]=1; + + + + for(int i=1;i +#include +#include + + + + + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +long long int x1=1,x2=1,mod,H1,H2,h1,h2; + +vectorv; + +string a,b; + +void logpow(long long int &oof,long long int exp,long long int mod) + +{ + + long long int rez=1,okei=oof; + + while(exp) + + { + + if(exp%2==1) + + rez*=okei; + + rez%=mod; + + okei*=okei; + + okei%=mod; + + exp/=2; + + } + + oof=rez; + +} + +void afis(vector v) + +{ + + cout<>a>>b; + + for(int i=0;i +#include + + +int min(int a, int b){ + + if(a <= b) + + return a; + + return b; + +} + + + +long long hash[2], base; + +char word[2000001], text[2000001]; + +int sol[1001]; + +int main(){ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + + + int wLen, tLen, nr = 0; + + long long whash[2], rhash[2], basePow[2]; + + + + scanf("%s%s",&word, &text); + + wLen = strlen(word); + + tLen = strlen(text); + + + + hash[0] = 666013; + + hash[1] = 1000000007; + + base = 97; + + + + for(int j = 0; j < 2; j++){ + + rhash[j] = 0; + + whash[j] = 0; + + basePow[j] = 1; + + } + + + + for(int i = 0; i < wLen; i++){ + + for(int j = 0; j < 2; j++){ + + whash[j] = ( (whash[j] * base) % hash[j] + word[i] ) % hash[j]; + + basePow[j] = (basePow[j] * base) % hash[j]; + + } + + } + + + + for(int i = 0; i < tLen; i++){ + + for(int j = 0; j < 2; j++) + + rhash[j] = ( (rhash[j] * base) % hash[j] + text[i] ) % hash[j]; + + if(i >= wLen){ + + for(int j = 0; j < 2; j++){ + + rhash[j] = ((rhash[j] - basePow[j] * text[i - wLen]) % hash[j] + hash[j]) % hash[j]; + + } + + } + + if(i >= wLen - 1 && whash[0] == rhash[0] && whash[1] == rhash[1]){ + + nr++; + + if(nr <= 1000) + + sol[nr - 1] = i - wLen + 1; + + } + + } + + printf("%d\n",nr); + + for(int i = 0; i < min(nr, 1000); i++) + + printf("%d ",sol[i]); + + return 0; + +} diff --git a/strmatch/rk/a8d77232-9ed3-4890-b8bc-a1bf3863f600.cpp b/strmatch/rk/a8d77232-9ed3-4890-b8bc-a1bf3863f600.cpp new file mode 100644 index 0000000000000000000000000000000000000000..84d12e410fa9ec1679ace7a0ac445ec7a8a24e3f --- /dev/null +++ b/strmatch/rk/a8d77232-9ed3-4890-b8bc-a1bf3863f600.cpp @@ -0,0 +1,80 @@ +#include + + + + + + +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + +int n, m; + +int p[2000005], h[2000005], h2[2000005], p2[2000005]; + +long long hs, hs2; + +char s[2000005], t[2000005]; + +vector rez; + +int main() + +{ + + fin.getline(s, 2000005); + + fin.getline(t, 2000005); + + n = strlen(t); + + m = strlen(s); + + p[0] = p2[0] = 1; + + for(int i=1; i<=n; i++) + + p[i] = (1LL*p[i-1]*31)%int(1e9+9), p2[i] = (1LL*p2[i-1]*53)%int(1e9+9); + + for(int i=0; i0)? h[i-1]: 0) + 1LL*p[i]*(t[i]-'0'+1) %int(1e9+9))%int(1e9+9); + + for(int i=0; i=m)?h[i-m]:0)+int(1e9+9))%int(1e9+9))) + + { + + if(nr<1000) + + rez.push_back(i-m+1); + + nr++; + + } + + fout << nr << '\n'; + + nr = 0; + + for(auto it:rez) + + if(nr > 1000) + + break; + + else fout << it << ' ', nr++; + + return 0; + +} diff --git a/strmatch/rk/a92522cb-58ad-4f15-8307-b46d9c0baa13.cpp b/strmatch/rk/a92522cb-58ad-4f15-8307-b46d9c0baa13.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5346b51356d7b37e4a59661de59ac2505e8b98c2 --- /dev/null +++ b/strmatch/rk/a92522cb-58ad-4f15-8307-b46d9c0baa13.cpp @@ -0,0 +1,105 @@ +#include +#include +char A[2000001], B[2000001]; + +int NA, NB; + +int hashA1, hashA2, P1, P2; + +char match[2000001]; + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/aabc8f6d-9e42-4e39-a272-4e9130ced98b.cpp b/strmatch/rk/aabc8f6d-9e42-4e39-a272-4e9130ced98b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..630179dceb1eccebd5b9abc6a88289870a222ddd --- /dev/null +++ b/strmatch/rk/aabc8f6d-9e42-4e39-a272-4e9130ced98b.cpp @@ -0,0 +1,114 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +int val1,val2,rhash1,rhash2,lgt,lgp,putere1=1,putere2=1; + +char A[2000000 +1],B[2000000 +1]; + +vector V; + + + +void Rabin_Karp(char T[], char P[]) + +{ + + lgt=strlen(T); + + lgp=strlen(P); + + for(int i=1; i<=lgp-1; i++) + + { + + putere1*=75; + + putere2*=75; + + putere1%=100007; + + putere2%=100021; + + } + + for(int i=0; i>A; + + fi>>B; + + Rabin_Karp(B,A); + + afisare(); + + fi.close(); + + fo.close(); + + return 0; + +} diff --git a/strmatch/rk/ac920699-3768-4836-8d70-fbe33ce418c0.cpp b/strmatch/rk/ac920699-3768-4836-8d70-fbe33ce418c0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1eee2bda0804b5b505903f43098e9a1224f86ca --- /dev/null +++ b/strmatch/rk/ac920699-3768-4836-8d70-fbe33ce418c0.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string pat, txt; + + + +const int d = 73, mod1 = 100007, mod2 = 100021; + + + +vector sol; + + + +void rabin_karp(string pat, string txt, int m1, int m2) { + + int p1 = 0, t1 = 0, p2 = 0, t2 = 0, h1 = 1, h2 = 1; + + int m = pat.size(); + + int n = txt.size(); + + for(int i = 0; i < m; ++i) { + + p1 = (d * p1 + pat[i]) % m1; + + p2 = (d * p2 + pat[i]) % m2; + + + + if(i != 0) { + + h1 = (h1 * d) % m1; + + h2 = (h2 * d) % m2; + + } + + } + + for(int i = 0; i < m; ++i) { + + t1 = (d * t1 + txt[i]) % m1; + + t2 = (d * t2 + txt[i]) % m2; + + } + + if(p1 == t1 && p2 == t2) { + + sol.push_back(0); + + } + + for(int i = m; i < n; ++i) { + + t1 = ((t1 - (txt[i - m] * h1) % m1 + m1) * d + txt[i]) % m1; + + t2 = ((t2 - (txt[i - m] * h2) % m2 + m2) * d + txt[i]) % m2; + + if(p1 == t1 && p2 == t2) { + + sol.push_back(i - m + 1); + + } + + } + +} + + + +void read() { + + getline(f, pat); + + + + getline(f, txt); + +} + + + +void solve() { + + read(); + + rabin_karp(pat, txt, mod1, mod2); + + int nr = sol.size(); + + g << nr << '\n'; + + for(int i = 0; i < min(nr, 1000); ++i) { + + g << sol[i] << ' '; + + } + +} + + + +int main(void) { + + solve(); + +} diff --git a/strmatch/rk/adb09edb-779f-4773-bff0-de26d75ae8c5.cpp b/strmatch/rk/adb09edb-779f-4773-bff0-de26d75ae8c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d318cc734f0ca0378aad3985b7d4eb79eed3bc37 --- /dev/null +++ b/strmatch/rk/adb09edb-779f-4773-bff0-de26d75ae8c5.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +const int DIM = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int MOD2 = 1e9 + 9; + +const int P = 66; + + + +int n, m; + +char A[DIM], B[DIM]; + +int f[260]; + + + +void add(int &H1, int &H2, int cif) { + + H1 = (1LL * H1 * P + cif) % MOD1; + + H2 = (1LL * H2 * P + cif) % MOD2; + +} + + + +int P1, P2; + +void scoate(int &H1, int &H2, int cif) { + + H1 = (H1 - 1LL * cif * P1) % MOD1; + + if (H1 < 0) H1 += MOD1; + + + + H2 = (H2 - 1LL * cif * P2) % MOD2; + + if (H2 < 0) H2 += MOD2; + +} + + + +bool checkEqual(int H1, int H2, int h1, int h2) { + + if (H1 == h1 && H2 == h2) return true; + + return false; + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", A + 1); + + scanf("%s", B + 1); + + + + n = strlen(A + 1); + + m = strlen(B + 1); + + + + int nr = 0; + + for (char i = 'a'; i <= 'z' ; ++i) f[i] = ++nr; + + for (char i = 'A'; i <= 'Z' ; ++i) f[i] = ++nr; + + for (char i = '0'; i <= '9' ; ++i) f[i] = ++nr; + + + + int hashA1 = 0, hashA2 = 0; + + for (int i = 1; i <= n ; ++i) + + add(hashA1, hashA2, f[A[i]]); + + + + int hashB1 = 0, hashB2 = 0; + + for (int i = 1; i <= n ; ++i) + + add(hashB1, hashB2, f[B[i]]); + + + + vector v; + + if (checkEqual(hashA1, hashA2, hashB1, hashB2)) v.push_back(0); + + + + P1 = P2 = 1; + + for (int i = 1; i <= n - 1 ; ++i) { + + P1 = 1LL * P1 * P % MOD1; + + P2 = 1LL * P2 * P % MOD2; + + } + + + + for (int i = n + 1; i <= m ; ++i) { + + scoate(hashB1, hashB2, f[B[i - n]]); + + add(hashB1, hashB2, f[B[i]]); + + + + if (checkEqual(hashA1, hashA2, hashB1, hashB2)) v.push_back(i - n); + + } + + + + printf("%d\n", (int)v.size()); + + while ((int)v.size() > 1000) v.pop_back(); + + for (auto it : v) printf("%d ", it); + + + + return 0; + +} diff --git a/strmatch/rk/ae3fabf2-9cd3-44ba-91f8-f6fe0f0034de.cpp b/strmatch/rk/ae3fabf2-9cd3-44ba-91f8-f6fe0f0034de.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c8b26217de0b86083913a6818696dfcbd79032f --- /dev/null +++ b/strmatch/rk/ae3fabf2-9cd3-44ba-91f8-f6fe0f0034de.cpp @@ -0,0 +1,204 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +struct RollingHash + +{ + + int a, mod, aMax; + + int value; + + + + RollingHash(int a, int mod) + + { + + this->a = a; + + this->mod = mod; + + aMax = 1; + + value = 0; + + } + + + + void initHash(const string &s) + + { + + for(int i = 0; i sol; + +int k; + + + +void debug(RollingHash r1, RollingHash r2, RollingHash r3, RollingHash r4) + +{ + + g<>pattern>>text; + + if(pattern.size() > text.size()) + + { + + return; + + } + + + + pattern1.initHash(pattern); + + pattern2.initHash(pattern); + + text1.initHash(text.substr(0, pattern.size())); + + text2.initHash(text.substr(0, pattern.size())); + + + + if(pattern1 == text1 && pattern2 == text2) + + { + + k++; + + sol.push_back(0); + + } + + for(int i = pattern.size(); i < text.size(); i++) + + { + + text1.roll(text[i], text[i - pattern.size()]); + + text2.roll(text[i], text[i - pattern.size()]); + + if(pattern1 == text1 && pattern2 == text2) + + { + + k++; + + if(sol.size() < 1000) + + sol.push_back(i - pattern.size() + 1); + + } + + } + +} + + + + + +void afisare() + +{ + + g< + +const int LMAX = 2000005; +const int a = 31; + +char p[LMAX], t[LMAX]; +int lenp, lent; + +void read() { + scanf("%s %s", p, t); + + lenp = strlen(p); + lent = strlen(t); +} + +struct RollingHash { + int a, n; + int maxpw{1}, value{0}; + + void init(char *s, int len) { + for (int i = 0; i < len; i++) { + value = (value * a + s[i]) % n; + + if (i) maxpw = (maxpw * a) % n; + } + } + + void roll(char out, char in) { + value = ((value - (out * maxpw) % n + n) * a + in) % n; + } + + bool operator==(const RollingHash& other) const { + return value == other.value; + } +}; + +void rabin_karp() { + std::vector matches; + + int n1 = 100003, n2 = 100109; + + RollingHash hp[]{{a, n1}, {a, n2}}; + RollingHash ht[]{{a, n1}, {a, n2}}; + + for (int i = 0; i < 2; i++) { + hp[i].init(p, lenp); + ht[i].init(t, lenp); + } + + if (lenp > lent) { + printf("0\n"); + return; + } + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(0); + + for (int i = lenp; i < lent; i++) { + ht[0].roll(t[i - lenp], t[i]); + ht[1].roll(t[i - lenp], t[i]); + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(i - lenp + 1); + } + + printf("%ld\n", matches.size()); + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + printf("%d ", matches[i]); +} + +int main() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + + read(); + rabin_karp(); +} diff --git a/strmatch/rk/afa6554c-ea84-4b86-b670-3843baf0180b.cpp b/strmatch/rk/afa6554c-ea84-4b86-b670-3843baf0180b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3ee3a33cfa06806705606d0615274bb8f925af --- /dev/null +++ b/strmatch/rk/afa6554c-ea84-4b86-b670-3843baf0180b.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string s,s2; + +int n,b=101,mod1=100007,mod2=100021; + +int h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + + + +int main() + +{ + + fin>>s>>s2; + + n=s.length(); + + m=s2.length(); + + p1=1; + + p2=1; + + for(i=0; im) + + { + + fout<<0<<'\n'; + + return 0; + + } + + for(i=0; i +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char a[2000010],b[2000010]; + +const long long p1=31,p2=101,mod1=423957231,mod2=298892145; + +long long stra,strb; + + + +void citire() + +{ + + in>>a>>b; + + stra=strlen(a); + + strb=strlen(b); + +} + +void solv() + +{ + + long long hasha1=0,hasha2=0,hashb1=0,hashb2=0,c1=1,c2=1; + + vectorsol; + + for(int i=0;istrb) + + { + + out<<0; + + return 0; + + } + + solv(); + + return 0; + +} diff --git a/strmatch/rk/b3c6c659-3110-41c6-92ee-63821f4df4e7.cpp b/strmatch/rk/b3c6c659-3110-41c6-92ee-63821f4df4e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41077150738f1b1b4c735675ba0b9262f4d700cc --- /dev/null +++ b/strmatch/rk/b3c6c659-3110-41c6-92ee-63821f4df4e7.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 2e6 + 5; + +const long long MOD = 1000000123; + +const long long BASE = 62; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +long long h2[NMAX]; + + + +long long key[150]; + + + +void FormKey() + +{ + + int cnt = 0; + + for(char c = '0'; c <= '9'; ++c) { + + key[c] = ++cnt; + + } + + for(char c = 'a'; c <= 'z'; ++c) { + + key[c] = ++cnt; + + } + + for(char c = 'A'; c <= 'Z'; ++c) { + + key[c] = ++cnt; + + } + + + +} + +int main() + +{ + + FormKey(); + + string a, b; + + cin >> a >> b; + + long long h1 = 0, h2 = 0; + + long long p = 1, pk = 1; + + for(auto x: a) { + + + + h1 = (h1 * BASE + key[x]) % MOD; + + pk = p; + + p = (p * BASE) % MOD; + + } + + p = 1; + + int cnt = 0; + + vector sol; + + for(int i = 0; i < a.size(); ++i) { + + h2 = (h2 * BASE + key[b[i]]) % MOD; + + } + + if(h1 == h2) { + + cnt++; + + sol.emplace_back(0); + + } + + for(int i = a.size(); i < b.size(); ++i) { + + h2 = h2 - key[b[i - a.size()]] * pk % MOD; + + if(h2 < 0) + + h2 += MOD; + + h2 = (h2 * BASE % MOD + key[b[i]]) % MOD; + + if(h1 == h2) { + + cnt++; + + if(cnt <= 1e3) { + + sol.emplace_back(i - a.size() + 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) + + cout << x << " "; + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/rk/b3f055cb-133e-4f40-9ec9-696a30da4fce.cpp b/strmatch/rk/b3f055cb-133e-4f40-9ec9-696a30da4fce.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b3abf08697d99edd99af520bbbaefc5e117c60d --- /dev/null +++ b/strmatch/rk/b3f055cb-133e-4f40-9ec9-696a30da4fce.cpp @@ -0,0 +1,172 @@ +#include + + +using namespace std; + + + +const int m1=100007; + +const int m2=100021; + +const int p=101; + + + +string A; + +string B; + + + +int nA; + +int nB; + + + +int hash1,hash2,h1,h2; + + + +int rasp; + + + +int p1=1,p2=1; + + + +bool nuVerifica=false; + + + +int v[2000001]; + + + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +int main() + +{ + + f>>A>>B; + + nA=A.length(); + + nB=B.length(); + + + + for(int i=0;inB) + + { + + g<<"0"<<"\n"; + + return 0; + + } + + + + for(int i=0;i +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/b63bb89e-acbb-41c5-bd62-1556abbc79ba.cpp b/strmatch/rk/b63bb89e-acbb-41c5-bd62-1556abbc79ba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b615bdc484de85394a379f1ff4e7e2ad5c97169 --- /dev/null +++ b/strmatch/rk/b63bb89e-acbb-41c5-bd62-1556abbc79ba.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef long long ll; + + + +const ll p = 256, MOD1 = 100007, MOD2 = 100021; + + + +ll p1 = 1, p2 = 1, hasha1, hasha2, hash1, hash2; + +string txt, pat; + +vector v; + + + +int main() + +{ + + fin>>pat>>txt; + + for(int i = 0; i < pat.length(); ++i) { + + hasha1 = (hasha1 * p + pat[i]) % MOD1, + + hasha2 = (hasha2 * p + pat[i]) % MOD2; + + + + if(i) p1 = (p1 * p) % MOD1, p2 = (p2 * p) % MOD2; + + } + + + + for(int i = 0; i < pat.length(); ++i) + + hash1 = (hash1 * p + txt[i]) % MOD1, + + hash2 = (hash2 * p + txt[i]) % MOD2; + + + + if(hash1 == hasha1 && hash2 == hasha2) v.push_back(0); + + + + for(int i = pat.length(); i < txt.length(); ++i) { + + hash1 = ((hash1 - (txt[i - pat.length()] * p1) % MOD1 + MOD1) * p + txt[i]) % MOD1, + + hash2 = ((hash2 - (txt[i - pat.length()] * p2) % MOD2 + MOD2) * p + txt[i]) % MOD2; + + + + if(hash1 == hasha1 && hash2 == hasha2) v.push_back(i - pat.length() + 1); + + } + + + + fout< +#include +#include +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream gout("strmatch.out"); + + + +typedef unsigned long long LL; + + + + + +const unsigned long long baza = 1010012383; + +const unsigned long long m = 1010012359; + + + + + + + +LL rasp; + + + +vectorv; + + + +int main() + +{ + + string a, b; + + + + fin >> a >> b; + + if( a .size() > b.size() ) + + { + + gout <<0; + + return 0; + + } + + LL n = a.size(), i; + + LL vala = 0, valb = 0, put = 1; + + + + + + for( i = 0; i < n; ++i ) + + { + + vala = ( vala * baza + a[i]) % m; + + put = (put * baza )%m; + + } + + + + + + for( i = 0; i < n; ++i ) + + valb = ( valb * baza + b[i]) % m; + + + + + + if( vala == valb ) + + { + + ++rasp; + + v.push_back(0); + + } + for(i = 1; i <= b.size() - n; ++i ) + + { + + valb = ( valb * baza + b[i+n-1] ) % m; + + + + + + + + LL ceva = (put * b[i-1]) % m; + + valb = ( valb - ceva +m) % m; + + + + + + if( vala == valb ) + + { + + ++rasp; + + + + if(v.size() < 1000) + + v.push_back(i); + + + + } + + } + + gout < +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +int P = 97; + +const int mod1 = 100103; +const int mod2 = 100109; + +ifstream fin ("strmatch.in"); +ofstream fout ("strmatch.out"); + +string A,B; + +int main(void) { + fin >> A >> B; + int N = A.size(), M = B.size(); + + if(M < N) { + fout << 0 << endl; + return 0; + } + + int p1 = 1; + int p2 = 1; + + int h1 = 0; + int h2 = 0; + for(auto a: A) { + h1 = (h1*P + a) % mod1; + h2 = (h2*P + a) % mod2; + + p1 = (p1*P) % mod1; + p2 = (p2*P) % mod2; + } + + + int hb1 = 0; + int hb2 = 0; + for(int i = 0; i < N; i++) { + hb1 = (hb1*P + B[i]) % mod1; + hb2 = (hb2*P + B[i]) % mod2; + } + + vi res; + int result = 0; + if (hb1 == h1 && hb2 == h2) { res.push_back(0); result++; } + + for(int i = N; i < M; i++) { + hb1 = ((hb1*P + B[i]) % mod1 - (B[i-N]*p1) % mod1 + mod1) % mod1; + hb2 = ((hb2*P + B[i]) % mod2 - (B[i-N]*p2) % mod2 + mod2) % mod2; + if (hb1 == h1 && hb2 == h2) { + result++; + if (res.size() < 1000) { res.push_back(i - (N-1)); } + } + } + + fout << result << '\n'; + for(auto pos: res) { + fout << pos << ' '; + } + fout << '\n'; + + return 0; +} diff --git a/strmatch/rk/b99bd243-4fcf-415c-8156-93bf30c4e2c1.cpp b/strmatch/rk/b99bd243-4fcf-415c-8156-93bf30c4e2c1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7d460935b38d79f3af4c7dfb8eaded320fbfb31 --- /dev/null +++ b/strmatch/rk/b99bd243-4fcf-415c-8156-93bf30c4e2c1.cpp @@ -0,0 +1,126 @@ +#include +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = (1LL * codPat * SIGMA + codCaracter(pat[i])) % MOD1; + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = 1LL * putere * SIGMA % MOD1; + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = ((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1) % MOD1; + + + + codSir = (1LL * codSir * SIGMA + codCaracter(A[i])) % MOD1; + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/baf0c0de-2043-41d0-b3de-716ccea99da0.cpp b/strmatch/rk/baf0c0de-2043-41d0-b3de-716ccea99da0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e56745bd95345bf2423fbe6d3ac56d0712546081 --- /dev/null +++ b/strmatch/rk/baf0c0de-2043-41d0-b3de-716ccea99da0.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +using namespace std; + +string a,b; + +unsigned int baz=71; + +unsigned int ha[2000001],puteri[2000001],hasha; + +int rez[2000001],cont; + +int main() + +{ + + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + getline(cin,a); + + getline(cin,b); + + for(int i=a.size()-1; i>=0; --i) + + hasha=(hasha*baz+a[i]); + + for(int i=b.size()-1; i>=0; --i) + + ha[i]=(ha[i+1]*baz+b[i]); + + puteri[0]=1; + + for(int i=1; i<=b.size(); ++i) + + puteri[i]=(puteri[i-1]*baz); + + for(int i=a.size()-1; i +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A,B; + +int hashA1,hashA2,P1,P2; + +char match[2000001]; + + + +void Read() + +{ + + f>>A; + + f>>B; + + f.close(); + +} + + + +void Solve() + +{ + + int lenA,lenB; + + lenA = A.length(); + + lenB = B.length(); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for(int i=0;i lenB) + + { + + g<<0<<'\n'; + + g.close(); + + return; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +unsigned long long H[2000005]; + +vector ans; + + + + + + + +void rabin_karp(string const& a, string const& b) { + + const unsigned long long base = 167; + + + + unsigned long long hsrch = 0; + + for (auto& x : a) { + + hsrch *= base; + + hsrch += x; + + } + + + + unsigned long long pow = 1; + + for (int i = 1; i <= a.size(); i++) pow *= base; + + + + for (int i = 1; i <= b.size(); i++) { + + H[i] = H[i - 1] * base; + + H[i] += b[i - 1]; + + + + if (i < a.size()) continue; + + + + unsigned long long hash = H[i] - H[i - a.size()] * pow; + + + + if (hash == hsrch) ans.push_back(i - a.size()); + + } + + + +} + + + + + + + +int main() { + + + + string a, b; cin >> a >> b; + + + + rabin_karp(a, b); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + + + + + return 0; + +} diff --git a/strmatch/rk/bd8c3180-2618-414b-8a5f-2da755869ed2.cpp b/strmatch/rk/bd8c3180-2618-414b-8a5f-2da755869ed2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f292dbe676e1213d49f5d21e0b49880ab0b3644 --- /dev/null +++ b/strmatch/rk/bd8c3180-2618-414b-8a5f-2da755869ed2.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[2000001], B[2000001]; + +int NA, NB; + +int P = 73; + + + +int MOD1 = 100007; + +int MOD2 = 100021; + +int hashA1, hashA2, P1, P2; + + + +int sol[200001]; + + + +int main() + +{ + + + + in >> A >> B; + + NA = strlen(A); + + NB = strlen(B); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * P + A[i]) % MOD1; + + hashA2 = (hashA2 * P + A[i]) % MOD2; + + + + if (i != 0) + + P1 = (P1 * P) % MOD1, + + P2 = (P2 * P) % MOD2; + + } + + + + if (NA > NB) + + { + + out << 0; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * P + B[i]) % MOD1, + + hash2 = (hash2 * P + B[i]) % MOD2; + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + sol[Nr] = 0, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % MOD1 + MOD1) * P + B[i]) % MOD1; + + hash2 = ((hash2 - (B[i - NA] * P2) % MOD2 + MOD2) * P + B[i]) % MOD2; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + { + + if(Nr <= 1000) + + sol[Nr] = i-NA+1; + + Nr++; + + } + + } + + if (NA>NB) + + out << "0"; + + else { + + out < +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +string A,B; + +int hashA1,hashA2,P1,P2; + +char match[2000001]; + + + +void Read() + +{ + + f>>A; + + f>>B; + + f.close(); + +} + + + +void Solve() + +{ + + int lenA,lenB; + + lenA = A.length(); + + lenB = B.length(); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for(int i=0;i lenB) + + { + + g<<0<<'\n'; + + g.close(); + + return; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + for(int i=0;i + + +const int MAX_N = 2000005; + +const int P = 57; + +const int MOD1 = 1000000009; + +const int MOD2 = 1000000007; + + + +int n, m; + + + +std::vector ans; + + + +char p[MAX_N], t[MAX_N]; + + + +long long a1, a2, b1, b2, p1, p2; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s\n%s", p + 1, t + 1); + + t[0] = p[0] = '\0'; + + n = strlen(p + 1); + + m = strlen(t + 1); + + p1 = p2 = 1; + + for (int i = 1; i <= n; ++i) { + + a1 = (1LL * a1 * P + p[i]) % MOD1; + + a2 = (1LL * a2 * P + p[i]) % MOD2; + + if (i != 1) { + + p1 = (1LL * p1 * P) % MOD1; + + p2 = (1LL * p2 * P) % MOD2; + + } + + } + + for (int i = 1; i <= n; ++i) { + + b1 = (1LL * b1 * P + t[i]) % MOD1; + + b2 = (1LL * b2 * P + t[i]) % MOD2; + + } + + if (a1 == b1 && a2 == b2) { + + ans.push_back(0); + + } + + for (int i = n + 1; i <= m; ++i) { + + b1 = ((1LL * b1 - (p1 * t[i - n]) % MOD1 + MOD1) % MOD1 * P + t[i]) % MOD1; + + b2 = ((1LL * b2 - (p2 * t[i - n]) % MOD2 + MOD2) % MOD2 * P + t[i]) % MOD2; + + if (a1 == b1 && a2 == b2) { + + ans.push_back(i - n); + + } + + } + + std::cout << ans.size() << "\n"; + + for (int i = 0; i < ans.size() && i < 1000; ++i) { + + std::cout << ans[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/rk/bf046da9-87ce-44a4-811b-709cd77aa6fb.cpp b/strmatch/rk/bf046da9-87ce-44a4-811b-709cd77aa6fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..55587c4e60085c50f6ea28242dceb9d822be66c4 --- /dev/null +++ b/strmatch/rk/bf046da9-87ce-44a4-811b-709cd77aa6fb.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +using namespace std; + + + +ifstream f ("strmatch.in"); + +ofstream g ("strmatch.out"); + + + +vector s; + + + +int N, M, x1, x2, y1, y2, power1, power2; + + + +char A[2000002]; + +char B[2000002]; + + + +int main() + +{ + + f.getline(A,2000002); + + f.getline(B,2000002); + + + + N = strlen(A); + + M = strlen(B); + + + + if (N > M) + + { + + g << 0 << '\n'; + + return 0; + + } + + + + for (int i = 0; i < N; ++ i ) + + { + + x1 = ((x1 * 127) % 10003 + (A[ i ] - '0')) % 10003; + + x2 = ((x2 * 131) % 666013 + (A[ i ] - '0')) % 666013; + + y1 = ((y1 * 127) % 10003 + (B[ i ] - '0')) % 10003; + + y2 = ((y2 * 131) % 666013 + (B[ i ] - '0')) % 666013; + + } + + + + power1 = power2 = 1; + + for (int i = 1; i < N; ++ i ) + + { + + power1 = (power1 * 127) % 10003; + + power2 = (power2 * 131) % 666013; + + } + + + + if (x1 == y1 && x2 == y2) + + { + + s.push_back(0); + + } + + + + for (int i = N; i < M; ++ i ) + + { + + y1 = ((y1 - (power1 * (B[i - N] - '0')) % 10003 + 10003) * 127 + (B[i] - '0')) % 10003; + + y2 = ((y2 - (power2 * (B[i - N] - '0')) % 666013 + 666013) * 131 + (B[i] - '0')) % 666013; + + + + if (y1 == x1 && y2 == x2) + + { + + s.push_back(i - N + 1); + + } + + } + + + + g << s.size() << '\n'; + + + + for (int i = 0; i < 1000 && i < s.size(); ++ i ) + + g << s[i] << " "; + + + + return 0; + +} diff --git a/strmatch/rk/bf667781-5c65-4685-8b54-a4701b34b284.cpp b/strmatch/rk/bf667781-5c65-4685-8b54-a4701b34b284.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc811bea03d688e6c62b9823387bb708774e52ae --- /dev/null +++ b/strmatch/rk/bf667781-5c65-4685-8b54-a4701b34b284.cpp @@ -0,0 +1,104 @@ +#include + + + + + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +char a[2000010],b[2000010]; + +int poz,i,pozitiea,pozitieb,pozitie,l1,l2,numar,p1,p2,hasha1,hashb1,hasha2,hashb2; + +int v[1010]; + +int main() + +{ + + fin>>a>>b; + + l1=strlen(a); + + l2=strlen(b); + + if(l1>l2) + + { + + fout<<0; + + return 0; + + } + + p1=p2=1; + + for(i=0;i0) + + { + + p1=(p1*73)%100007; + + p2=(p2*73)%100021; + + } + + } + + if(hasha1==hashb1 && hasha2==hashb2) + + v[++numar]=0; + + for(i=l1;i///Rabin-Karp +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout("strmatch.out"); + + + + + + + +char s1[2000009],s2[2000009]; + +int p1=1,p2=1; + +int ans[1009],k,n,m; + +int HASH1,HASH2 ,Hash1,Hash2; + +int main() + +{ + + fin>>s1>>s2; + + n=strlen(s1); + + m=strlen(s2); + + for(int i=0;i +using namespace std; + +const int M1 = 1e9 + 7; + +const int M3 = 1e9 + 21; + +const int P = 73; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +struct hashh { + + long long h1, h3; + +}; + +hashh hashA, hashB; + +bool verif() { + + if (hashA.h1 == hashB.h1 && hashA.h3 == hashB.h3) { + + return true; + + } + + return false; + +} + +vector < int > ans; + +int main() { + + string a, b; + + long long P1 = 1, P2 = 1, P3 = 1; + + hashA.h1 = hashA.h3 = hashB.h1 = hashB.h3 = 0; + + in >> a >> b; + + for (int i = 1; i <= a.size() - 1; i++) { + + P1 = (P1 * P) % M1; + + P3 = (P3 * P) % M3; + + } + + if (a.size() > b.size()) { + + out << "0\n"; + + return 0; + + } + + for (int i = 0; i < a.size(); i++) { + + hashA.h1 = (hashA.h1 * P + a[i]) % M1; + + hashA.h3 = (hashA.h3 * P + a[i]) % M3; + + hashB.h1 = (hashB.h1 * P + b[i]) % M1; + + hashB.h3 = (hashB.h3 * P + b[i]) % M3; + + } + + if (verif()) { + + ans.push_back(0); + + } + + for (int i = a.size(); i < b.size(); i++) { + + hashB.h1 = (hashB.h1 - ((b[i - a.size()] * P1) % M1) + M1); + + hashB.h3 = (hashB.h3 - ((b[i - a.size()] * P3) % M3) + M3); + + hashB.h1 = (hashB.h1 * P + b[i]) % M1; + + hashB.h3 = (hashB.h3 * P + b[i]) % M3; + + if (verif()) { + + ans.push_back(i - a.size() + 1); + + } + + } + + out << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + out << ans[i] << " "; + + } + + return 0; + +} diff --git a/strmatch/rk/c06d6fb1-f640-4ab7-aaa7-8de7116cc69a.cpp b/strmatch/rk/c06d6fb1-f640-4ab7-aaa7-8de7116cc69a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d318cc734f0ca0378aad3985b7d4eb79eed3bc37 --- /dev/null +++ b/strmatch/rk/c06d6fb1-f640-4ab7-aaa7-8de7116cc69a.cpp @@ -0,0 +1,150 @@ +#include +using namespace std; + + + +const int DIM = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int MOD2 = 1e9 + 9; + +const int P = 66; + + + +int n, m; + +char A[DIM], B[DIM]; + +int f[260]; + + + +void add(int &H1, int &H2, int cif) { + + H1 = (1LL * H1 * P + cif) % MOD1; + + H2 = (1LL * H2 * P + cif) % MOD2; + +} + + + +int P1, P2; + +void scoate(int &H1, int &H2, int cif) { + + H1 = (H1 - 1LL * cif * P1) % MOD1; + + if (H1 < 0) H1 += MOD1; + + + + H2 = (H2 - 1LL * cif * P2) % MOD2; + + if (H2 < 0) H2 += MOD2; + +} + + + +bool checkEqual(int H1, int H2, int h1, int h2) { + + if (H1 == h1 && H2 == h2) return true; + + return false; + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + scanf("%s", A + 1); + + scanf("%s", B + 1); + + + + n = strlen(A + 1); + + m = strlen(B + 1); + + + + int nr = 0; + + for (char i = 'a'; i <= 'z' ; ++i) f[i] = ++nr; + + for (char i = 'A'; i <= 'Z' ; ++i) f[i] = ++nr; + + for (char i = '0'; i <= '9' ; ++i) f[i] = ++nr; + + + + int hashA1 = 0, hashA2 = 0; + + for (int i = 1; i <= n ; ++i) + + add(hashA1, hashA2, f[A[i]]); + + + + int hashB1 = 0, hashB2 = 0; + + for (int i = 1; i <= n ; ++i) + + add(hashB1, hashB2, f[B[i]]); + + + + vector v; + + if (checkEqual(hashA1, hashA2, hashB1, hashB2)) v.push_back(0); + + + + P1 = P2 = 1; + + for (int i = 1; i <= n - 1 ; ++i) { + + P1 = 1LL * P1 * P % MOD1; + + P2 = 1LL * P2 * P % MOD2; + + } + + + + for (int i = n + 1; i <= m ; ++i) { + + scoate(hashB1, hashB2, f[B[i - n]]); + + add(hashB1, hashB2, f[B[i]]); + + + + if (checkEqual(hashA1, hashA2, hashB1, hashB2)) v.push_back(i - n); + + } + + + + printf("%d\n", (int)v.size()); + + while ((int)v.size() > 1000) v.pop_back(); + + for (auto it : v) printf("%d ", it); + + + + return 0; + +} diff --git a/strmatch/rk/c266c2aa-e49c-45ea-baf9-ca8a0b9f2a41.cpp b/strmatch/rk/c266c2aa-e49c-45ea-baf9-ca8a0b9f2a41.cpp new file mode 100644 index 0000000000000000000000000000000000000000..37aebdf56fdcf5f91348f67a1cd83c5f7c345499 --- /dev/null +++ b/strmatch/rk/c266c2aa-e49c-45ea-baf9-ca8a0b9f2a41.cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + + + + + + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +string text,pattern; + +void read() +{ + in>>pattern>>text; +} + + +void rabin_karp() +{ + int base1=1,base2=1; + long long basemod1=0,basemod2=0; + + vector results; + + for(int i=0; itext.length()) + out<<0; + else + rabin_karp(); +} diff --git a/strmatch/rk/c2753160-75cd-42d3-9c84-16dc30e11a47.cpp b/strmatch/rk/c2753160-75cd-42d3-9c84-16dc30e11a47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fe40b342aa17f352c4874f2c9bb36d2525871b8 --- /dev/null +++ b/strmatch/rk/c2753160-75cd-42d3-9c84-16dc30e11a47.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +vector < int > R; + + + +int main() + +{ + + int lg_a, lg_b, i, hash1a = 0, hash2a = 0, hash1 = 0, hash2 = 0, P1 = 1, P2 = 1, nr; + + char a[2000005], b[2000005]; + + + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + hash1a = ( hash1a * 80 + ( a[i] - '0' + 1 ) ) % 45007; + + hash2a = ( hash2a * 80 + ( a[i] - '0' + 1 ) ) % 45013; + + + + hash1 = ( hash1 * 80 + ( b[i] - '0' + 1 ) ) % 45007; + + hash2 = ( hash2 * 80 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( i != 0 ) P1 = ( P1 * 80 ) % 45007, P2 = ( P2 * 80 ) % 45013; + + } + + + + if ( hash1a == hash1 && hash2a == hash2 ) R.push_back ( 0 ); + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + hash1 = ( ( ( hash1 - ( P1 * ( b[i-lg_a] - '0' + 1 ) ) % 45007 + 45007 ) * 80 ) % 45007 + b[i] - '0' + 1 ) % 45007; + + hash2 = ( ( ( hash2 - ( P2 * ( b[i-lg_a] - '0' + 1 ) ) % 45013 + 45013 ) * 80 ) % 45013 + b[i] - '0' + 1 ) % 45013; + + + + if ( hash1 == hash1a && hash2 == hash2a ) R.push_back ( i - lg_a + 1 ); + + } + + + + fout << R.size() << '\n'; + + nr = R.size(); + + nr = min ( nr, 1000 ); + + for ( i = 0 ; i < nr ; i++ ) fout << R[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/c27c464f-c29d-4f39-ae39-634dbdb330e1.cpp b/strmatch/rk/c27c464f-c29d-4f39-ae39-634dbdb330e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10cafc0414a5ea99a43639d393bce5d95c200034 --- /dev/null +++ b/strmatch/rk/c27c464f-c29d-4f39-ae39-634dbdb330e1.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + +const int NMAX = 2000005; + +const long long MOD1 = 1000000007; + +const long long MOD2 = 1000000009; + +const long long BAZA = 62; + +char a[NMAX], b[NMAX]; + +long long kkt(char ch) { + + int ad = 0; + + if('0' <= ch && ch <= '9') { + + return ch - '0' + 1; + + } + + ad += ('9' - '0' + 1); + + if('A' <= ch && ch <= 'Z') { + + return ad + ch - 'A' + 1; + + } + + ad += ('Z' - 'A' + 1); + + return ad + ch - 'a' + 1; + +} + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", a + 1); + + int n = (int)strlen(a + 1); + + long long c1 = 0, c2 = 0, p1 = 1, p2 = 1; + + for(int i = 1; i <= n; i++) { + + c1 = ((c1 * BAZA) + kkt(a[i])) % MOD1; + + c2 = ((c2 * BAZA) + kkt(a[i])) % MOD2; + + if(i < n) { + + p1 = (p1 * BAZA) % MOD1; + + p2 = (p2 * BAZA) % MOD2; + + } + + } + + scanf("%s", b + 1); + + int m = (int)strlen(b + 1); + + long long a1 = 0, a2 = 0; + + for(int i = 1; i <= m; i++) { + + if(i > n) { + + a1 = (a1 - (kkt(b[i - n]) * p1) % MOD1 + MOD1) % MOD1; + + a2 = (a2 - (kkt(b[i - n]) * p2) % MOD2 + MOD2) % MOD2; + + } + + a1 = ((a1 * BAZA) + kkt(b[i])) % MOD1; + + a2 = ((a2 * BAZA) + kkt(b[i])) % MOD2; + + if(i >= n) { + + if(a1 == c1 && a2 == c2) { + + sol.push_back(i - n); + + } + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 1; i <= 1000 && i <= sol.size(); i++) { + + printf("%d ", sol[i - 1]); + + } + + printf("\n"); + + return 0; + +} diff --git a/strmatch/rk/c3372879-cb22-4b17-ab3d-9d6152b4a66c.cpp b/strmatch/rk/c3372879-cb22-4b17-ab3d-9d6152b4a66c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95620a46ad3b4c46bdd8307dcb4f1acc28ba2d1c --- /dev/null +++ b/strmatch/rk/c3372879-cb22-4b17-ab3d-9d6152b4a66c.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +using namespace std; +const int LMAX = 2000001; +const int MOD1 = 100007; +const int MOD2 = 100021; +const int p = 73; + +char a[LMAX], b[LMAX]; +bool positions[LMAX]; + +int main() +{ + FILE *fin, *fout; + int na,nb,i,hasha1,hasha2,p1,p2,hash1,hash2,nr; + fin = fopen("strmatch.in","r"); + fout = fopen("strmatch.out","w"); + fscanf(fin,"%s\n%s\n",a,b); + + na = strlen(a), nb = strlen(b); + hasha1 = hasha2 = 0; + p1 = p2 = 1; + for(i=0; i 0){ + p1 = (p1*p)%MOD1; + p2 = (p2*p)%MOD2; + } + } + + if(na > nb) + fprintf(fout,"0\n"); + else{ + hash1 = hash2 = 0; + for(i=0; i + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +const int h = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +string a; + +string b; + + + +vector rez; + + + +int main() + +{ + + in >> a; + + in >> b; + + + + int cnt = 0; + + + + + + int n = a.size(); + + int m = b.size(); + + + + if(n > m) + + { + + out << 0 <<'\n'; + + return 0; + + } + + + + int Sm1 = 0; + + int Sm2 = 0; + + int Sc1 = 0; + + int Sc2 = 0; + + + + int p1 = 1; + + int p2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + int nr1 = a[i]; + + int nr2 = b[i]; + + + + Sm1 = (Sm1 * h + nr1) % mod1; + + Sm2 = (Sm2 * h + nr1) % mod2; + + Sc1 = (Sc1 * h + nr2) % mod1; + + Sc2 = (Sc2 * h + nr2) % mod2; + + + + + + if(i != 0) + + {p1 = (p1 * h) % mod1; + + p2 = (p2 * h) % mod2; + + } + + + + } + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + rez.push_back(0); + + } + + + + + + for(int i = n; i < m; i++) + + { + + int nr1 = b[i - n]; + + int nr2 = b[i]; + + + + + + Sc1 = ((Sc1 - (nr1 * p1) % mod1 + mod1) * h + nr2) % mod1; + + Sc2 = ((Sc2 - (nr1 * p2) % mod2 + mod2) * h + nr2) % mod2; + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + if(cnt <= 1000) + + rez.push_back(i - n + 1); + + } + + + + } + + + + out << cnt<<'\n'; + + + + for(int i = 0; i < rez.size(); i++) + + out << rez[i]<<" "; + + + + + + return 0; + +} diff --git a/strmatch/rk/c58e3e12-4bf8-4553-a2c2-2895ee31778d.cpp b/strmatch/rk/c58e3e12-4bf8-4553-a2c2-2895ee31778d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4f8d088b9cb37e1187d98de873a59f8406cc390 --- /dev/null +++ b/strmatch/rk/c58e3e12-4bf8-4553-a2c2-2895ee31778d.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +char a[2000001], b[2000001], ok[2000001]; + + + +int i, j, na, nb, ns1, ns2, nsa1, nsa2, p1, p2, nr; + + + +using namespace std; + + + +int main() { + + + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin.getline(a, 2000001); + + fin.getline(b, 2000001); + + na=strlen(a); + + nb=strlen(b); + + p1=p2=1; + + nsa1=nsa2=0; + + for(i=0;inb) { + + fout<<"0"; + + } + + else { + + ns1=ns2=0; + + for(i=0;i +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + + + +const int NMAX = 2e6 + 5; + +const int MOD1 = 1e9 + 7; + +const int SIGMA = 63; + + + +char pat[NMAX], A[NMAX]; + +int m, n; + +int codPat, codSir; + + + +inline int getMod(long long x) { + + return x - x / MOD1 * MOD1; + +} + + + +int codCaracter(char x) { + + if ('A' <= x && x <= 'Z') + + return x - 'A' + 1; + + else if ('a' <= x && x <= 'z') + + return x - 'a' + 26 + 1; + + else + + return x - '0' + 26 + 26 + 1; + +} + + + +int main() + +{ + + fi >> pat + 1 >> A + 1; + + m = strlen(pat + 1); + + n = strlen(A + 1); + + + + codPat = 0; + + for (int i = 1; i <= m; i++) { + + codPat = getMod(1LL * codPat * SIGMA + codCaracter(pat[i])); + + } + + + + vector ans; + + codSir = 0; + + for (int i = 1; i <= m; i++) { + + codSir = getMod(1LL * codSir * SIGMA + codCaracter(A[i])); + + } + + + + int putere = 1; + + for (int i = 1; i <= m - 1; i++) + + putere = getMod(1LL * putere * SIGMA); + + + + for (int i = m + 1; i <= n; i++) { + + + + if (codPat == codSir) { + + ans.push_back(i - m - 1); + + } + + codSir = getMod((long long)codSir - 1LL * putere * codCaracter(A[i - m]) + 100LL * MOD1); + + + + codSir = getMod(1LL * codSir * SIGMA + codCaracter(A[i])); + + + + } + + if (codPat == codSir) { + + ans.push_back(n - m); + + } + + + + fo << ans.size() << "\n"; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) + + fo << ans[i] << " "; + + + + + + return 0; + +} diff --git a/strmatch/rk/c77083d7-eb79-4281-ba9c-71b9d8292e61.cpp b/strmatch/rk/c77083d7-eb79-4281-ba9c-71b9d8292e61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7626099ffe7e321cfb157e06bde8c3896f1110e5 --- /dev/null +++ b/strmatch/rk/c77083d7-eb79-4281-ba9c-71b9d8292e61.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + + + +const int MOD1 = 1e9 + 7; + +const int MOD2 = 666013; + +const int baza = 74; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +long long amod1,amod2,bmod1,bmod2; + +int n,m; + +long long pown_MOD1,pown_MOD2; + +int ans[2000005]; + + + +int asci(char c){ + + return (int)c + 1; + +} + +string str(int nr){ + + string ans; + + while(nr){ + + ans += (nr % baza + 'A' - 1); + + nr /= baza; + + } + + reverse(ans.begin(),ans.end()); + + return ans; + +} + + + +int main() + +{ + + string a,b; + + in>>a>>b; + + if(a.size() > b.size()) + + swap(a,b); + + int n = a.size(); + + int m = b.size(); + + pown_MOD1 = pown_MOD2 = 1; + + for(int i = 1; i <= n - 1; i++){ + + pown_MOD1 = pown_MOD1 * baza % MOD1; + + pown_MOD2 = pown_MOD2 * baza % MOD2; + + } + + for(int i = 0; i < n; i++){ + + amod1 = (amod1 * baza + asci(a[i])) % MOD1; + + amod2 = (amod2 * baza + asci(a[i])) % MOD2; + + } + + for(int i = 0; i < n; i++){ + + bmod1 = (bmod1 * baza + asci(b[i])) % MOD1; + + bmod2 = (bmod2 * baza + asci(b[i])) % MOD2; + + } + + int index = 0; + + if(amod1 == bmod1 && amod2 == bmod2) + + ans[++index] = 0; + + for(int i = n; i < m; ++i){ + + bmod1 = ((bmod1 - asci(b[i - n]) * pown_MOD1) * baza + asci(b[i])) % MOD1; + + bmod1 = (bmod1 + MOD1) % MOD1; + + bmod2 = ((bmod2 - asci(b[i - n]) * pown_MOD2) * baza + asci(b[i])) % MOD2; + + bmod2 = (bmod2 + MOD2) % MOD2; + + if(amod1 == bmod1 && amod2 == bmod2) + + ans[++index] = i - n + 1; + + } + + + + out< + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +const int NMAX=2000004; + +const int P=73; + +const int mod1=100007; + +const int mod2=100021; + +char a[NMAX],b[NMAX]; + +long long amod1,bmod1,amod2,bmod2,powm1=1,powm2=1,cnt,rasp[NMAX],la,lb; + +int main() + +{ + + in>>a>>b; + + la=strlen(a); + + lb=strlen(b); + + if(la>lb){ + + out<<0; + + return 0; + + } + + for(int i=0;i +#include +#include +using namespace std; + + + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2; + +int hasht1,hasht2; + + + + + +void Rabin_Karp() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1, hashp1=hashp2=0; + + for(int i=0; i m) + + { + + printf("0"); + + return 0; + + } + + Rabin_Karp(); + + + + return 0; + + + +} diff --git a/strmatch/rk/c960c6b5-aa48-469f-bdb9-160cec04d199.cpp b/strmatch/rk/c960c6b5-aa48-469f-bdb9-160cec04d199.cpp new file mode 100644 index 0000000000000000000000000000000000000000..183afbfa917589b3a303d095fcf39faddac4c700 --- /dev/null +++ b/strmatch/rk/c960c6b5-aa48-469f-bdb9-160cec04d199.cpp @@ -0,0 +1,132 @@ +#include +#include +#include + + +using namespace std; + +char patt[2000002],s[2000002]; + +int poz[1010]; + + + +int is_prime(int x) + +{ + + if(x<2||x>2&&x%2==0)return 0; + + for(int d=3;d*d<=x;d+=2) + + if(x%d==0)return 0; + + return 1; + +} + + + +int next_prime(int x) + +{ + + x++; + + while(!is_prime(x))x++; + + return x; + +} + + + +int main() + +{ + + FILE *f=fopen("strmatch.in","r"); + + fgets(patt,2000002,f); + + fgets(s,2000002,f); + + f=fopen("strmatch.out","w"); + + int lp=strlen(patt),i; + + if(patt[lp-1]=='\n')patt[--lp]=0; + + int ls=strlen(s); + + if(s[ls-1]=='\n')s[--ls]=0; + + if(ls +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp-1; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); +vector ans; + + + + + + + +void rabin_karp(string const& s, string const& t) { + + const long long p = 167; + + int S = s.size(), T = t.size(); + + + + unsigned long long pow = 1; + + for (auto x : s) pow *= p; + + + + vector h(T + 1); + + for (int i = 0; i < T; i++) + + h[i + 1] = h[i] * p + t[i]; + + long long h_s = 0; + + for (int i = 0; i < S; i++) + + h_s = h_s * p + s[i]; + + + + vector occurences; + + for (int i = 0; i + S - 1 < T; i++) { + + long long cur_h = h[i + S] - h[i] * pow; + + if (cur_h == h_s) + + ans.push_back(i); + + } + +} + + + + + + + +int main() { + + + + string a, b; cin >> a >> b; + + + + + + rabin_karp(a, b); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + + + return 0; + +} diff --git a/strmatch/rk/c9d568a5-0323-4cf1-a602-15e403b3b753.cpp b/strmatch/rk/c9d568a5-0323-4cf1-a602-15e403b3b753.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ceb4356cb09d8a8dd89c82c16601b2b80c27165 --- /dev/null +++ b/strmatch/rk/c9d568a5-0323-4cf1-a602-15e403b3b753.cpp @@ -0,0 +1,161 @@ +#include + + + + + + + +using namespace std; + + + +ifstream f("strmatch" ".in"); + +ofstream g("strmatch" ".out"); + + + +typedef long long ll; + +typedef long double ld; + +typedef pair pii; + +typedef pair pct; + + + +const ll mod = 1e9 + 7; + +const ld eps = 1e-9; + + + + + +void add(ll &a , ll b) + +{ + + a += b; + + a %= mod; + +} + + + +void sub(ll &a, ll b) + +{ + + a = (a - b + mod) % mod; + +} + + + +string txt,pat; + +int pi[1000010], d[1000010],ans,cat; + +vector sol; + +void compute_pattern(string a,int pi[]) + +{ + + int k = 0; + + int n = a.size() - 1; + + pi[1] = 0; + + for(int i = 2; i <= n; ++i) + + { + + while( k > 0 && a[i] != a[k + 1] ) k = pi[k]; + + if(a[i] == a[k + 1]) k++; + + pi[i] = k; + + } + +} + + + +void find_pattern(string y, string pat,int pi[], int d[]) + +{ + + compute_pattern(pat, pi); + + int m = y.size() - 1; + + int n = pat.size() - 1; + + int k = 0; + + for(int i = 1; i <= m ; ++i) + + { + + while( (k > 0) && (y[i] != pat[k + 1]) ) k = pi[k]; + + if(y[i] == pat[k+1]) k++; + + if(k == pat.size() - 1) + + { + + cat++; + + if(cat <= 1000) + + sol.push_back(i - n); + + } + + } + +} + +int main() + +{ + + f >> pat >> txt ; + + txt = ' ' + txt; + + pat = ' ' + pat; + + find_pattern(txt, pat, pi, d); + + g << cat << '\n'; + + for(auto it : sol) + + { + + g << it << ' '; + + cat--; + + if(cat == 0) + + break; + + } + + f.close(); + + g.close(); + + return 0; + +} diff --git a/strmatch/rk/cabf044f-5bd3-405f-ae72-d0c12050ccc2.cpp b/strmatch/rk/cabf044f-5bd3-405f-ae72-d0c12050ccc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fc97ca6b303c8ea9e64f996ee171393d1d08155 --- /dev/null +++ b/strmatch/rk/cabf044f-5bd3-405f-ae72-d0c12050ccc2.cpp @@ -0,0 +1,97 @@ +#include +#include +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +char s[2000001],s2[2000001]; + +int n,b=101,mod1=100007,mod2=100021,h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + +int main() { + + f>>s>>s2; + + n=strlen(s); + + m=strlen(s2); + + p1=1; + + p2=1; + + for(i=0; im) { + + g<<0<<'\n'; + + return 0; + + } + + for(i=0; i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +const unsigned long long base = 167; + +vector vans; + +ll ans; + +unsigned long long H[2000005]; +int main() { + + + + string a, b; cin >> a >> b; + + + + unsigned long long hsrch = 0; + + for (auto& x : a) { + + hsrch *= base; + + hsrch += x; + + } + + + + unsigned long long pow = 1; + + for (int i = 1; i <= a.size(); i++) { + + pow *= base; + + } + + + + for (int i = 1; i <= b.size(); i++) { + + H[i] = H[i - 1] * base; + + H[i] += b[i - 1]; + + + + if (i < a.size()) { + + continue; + + } + + + + unsigned long long hash = H[i] - H[i - a.size()] * pow; + + + + if (hash == hsrch) { + + ans++; + + if (vans.size() < 1000) vans.push_back(i - a.size()); + + } + + } + + + + cout << ans << '\n'; + + for (auto& x : vans) { + + cout << x << ' '; + + } + + + + + + return 0; + +} diff --git a/strmatch/rk/cb906716-e8b7-4c4b-b371-e0ab2da7213a.cpp b/strmatch/rk/cb906716-e8b7-4c4b-b371-e0ab2da7213a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31b51d4f2204f929320c06528ed2815c0e7c905e --- /dev/null +++ b/strmatch/rk/cb906716-e8b7-4c4b-b371-e0ab2da7213a.cpp @@ -0,0 +1,136 @@ +#include +#include +using namespace std; + + + +char A[2000001], B[2000001]; +int main() + +{ + + int result[1000]; + + + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + + + int M = strlen(A); + + int N = strlen(B); + + + + + + + + int p = 1, p2 = 1, n = 0; + + int aVal = 0; + + int aVal2 = 0; + + + + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + + + for (int i = 0; i < M; i++) + + { + + aVal = (aVal * 73 + A[i]) % 100007; + + aVal2 = (aVal2 * 73 + A[i]) % 100011; + + hash1 = (hash1 * 73 + B[i]) % 100007; + + hash2 = (hash2 * 73 + B[i]) % 100011; + + if (i != 0) + + { + + p = (p * 73) % 100007; + + p2 = (p2 * 73) % 100011; + + } + + } + + + + if (hash1 == aVal && hash2 == aVal2) + + { + + n++; + + result[0] = 0; + + } + + + + for (int i = M; i < N; i++) + + { + + hash1 = ((hash1 - (B[i - M] * p) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - M] * p2) % 100011 + 100011) * 73 + B[i]) % 100011; + + if (hash1 == aVal && hash2 == aVal2) + + { + + if(n < 1000) + + result[n] = (i - M + 1); + + n++; + + } + + } + + + + printf("%d\n", n); + + for (int i = 0; i < ((n < 1000) ? n : 1000); i++) + + { + + printf("%d ", result[i]); + + } + + + + return 0; + +} diff --git a/strmatch/rk/cc260c3c-a32b-4854-ba3b-2193e7b0471f.cpp b/strmatch/rk/cc260c3c-a32b-4854-ba3b-2193e7b0471f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97aebbb3ef2ff6c2d282f20c8e495e899cba398c --- /dev/null +++ b/strmatch/rk/cc260c3c-a32b-4854-ba3b-2193e7b0471f.cpp @@ -0,0 +1,122 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'+1); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include + + + + +using namespace std; +typedef long long ll; +typedef pair pii; +typedef vector vi; +const int INF = 0x3f3f3f3f; + +int P = 97; + +const int mod1 = 100103; +const int mod2 = 100109; + +ifstream fin ("strmatch.in"); +ofstream fout ("strmatch.out"); + +string A,B; + +int main(void) { + fin >> A >> B; + int N = A.size(), M = B.size(); + + if(M < N) { + fout << 0 << endl; + return 0; + } + + int p1 = 1; + int p2 = 1; + + int h1 = 0; + int h2 = 0; + for(auto a: A) { + h1 = (h1*P + a) % mod1; + h2 = (h2*P + a) % mod2; + + p1 = (p1*P) % mod1; + p2 = (p2*P) % mod2; + } + + + int hb1 = 0; + int hb2 = 0; + for(int i = 0; i < N; i++) { + hb1 = (hb1*P + B[i]) % mod1; + hb2 = (hb2*P + B[i]) % mod2; + } + + vi res; + int result = 0; + if (hb1 == h1 && hb2 == h2) { res.push_back(0); result++; } + + for(int i = N; i < M; i++) { + hb1 = ((hb1*P + B[i]) % mod1 - (B[i-N]*p1) % mod1 + mod1) % mod1; + hb2 = ((hb2*P + B[i]) % mod2 - (B[i-N]*p2) % mod2 + mod2) % mod2; + if (hb1 == h1 && hb2 == h2) { + result++; + if (res.size() < 1000) { res.push_back(i - (N-1)); } + } + } + + fout << result << '\n'; + for(auto pos: res) { + fout << pos << ' '; + } + fout << '\n'; + + return 0; +} diff --git a/strmatch/rk/d019aaf8-31ee-4ae1-8827-30c0b1cfa5e3.cpp b/strmatch/rk/d019aaf8-31ee-4ae1-8827-30c0b1cfa5e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd56f9fcad335fd8b58faeefee4bc88090cf414c --- /dev/null +++ b/strmatch/rk/d019aaf8-31ee-4ae1-8827-30c0b1cfa5e3.cpp @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef string::iterator sit; + + + +class Roller{ + +public: + + int a, n; + + int ap = 1; + + int q = 0; + + Roller(); + + Roller(int a, int n, sit lt, sit rt) : a(a), n(n){ + + for(auto it = lt; it != rt; ++it){ + + q *= a; + + q += *it; + + q %= n; + + + + if(it!=lt)ap *= a; + + ap %= n; + + } + + } + + void roll(char out, char in){ + + q -= out*ap; + + q += n*128; + + q %= n; + + q *= a; + + q += in; + + q %= n; + + } + +}; + + + +const int a = 31; + +const int n1 = 1000003, n2 = 1000033; + + + +string pattern, text; + + + +int ans1 = 0; + +vector ans2; + + + +int main(){ + + + + fin >> pattern >> text; + + Roller rpattern[] = {Roller(a,n1,pattern.begin(),pattern.end()),Roller(a,n2,pattern.begin(),pattern.end())}; + + Roller rtext[] = {Roller(a,n1,text.begin(),text.begin()+pattern.size()),Roller(a,n2,text.begin(),text.begin()+pattern.size())}; + + + + if(rpattern[0].q == rtext[0].q && rpattern[1].q == rtext[1].q){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(0); + + } + + + + for(int i = pattern.size(); i < text.size(); ++i){ + + rtext[0].roll(text[i-pattern.size()], text[i]); + + rtext[1].roll(text[i-pattern.size()], text[i]); + + if(rpattern[0].q == rtext[0].q && rpattern[1].q == rtext[1].q){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-pattern.size()+1); + + } + + } + + + + fout << ans1 << "\n"; + + for(auto a : ans2)fout << a << " "; + + return 0; + +} diff --git a/strmatch/rk/d0c1454c-7a9c-4285-b742-11284f77332e.cpp b/strmatch/rk/d0c1454c-7a9c-4285-b742-11284f77332e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f4172ea69b11a2017283f58233f81664006fa27 --- /dev/null +++ b/strmatch/rk/d0c1454c-7a9c-4285-b742-11284f77332e.cpp @@ -0,0 +1,125 @@ +#include +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/d1efccad-8af5-411d-846b-54bbad7eaf49.cpp b/strmatch/rk/d1efccad-8af5-411d-846b-54bbad7eaf49.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f292dbe676e1213d49f5d21e0b49880ab0b3644 --- /dev/null +++ b/strmatch/rk/d1efccad-8af5-411d-846b-54bbad7eaf49.cpp @@ -0,0 +1,135 @@ +#include +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char A[2000001], B[2000001]; + +int NA, NB; + +int P = 73; + + + +int MOD1 = 100007; + +int MOD2 = 100021; + +int hashA1, hashA2, P1, P2; + + + +int sol[200001]; + + + +int main() + +{ + + + + in >> A >> B; + + NA = strlen(A); + + NB = strlen(B); + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * P + A[i]) % MOD1; + + hashA2 = (hashA2 * P + A[i]) % MOD2; + + + + if (i != 0) + + P1 = (P1 * P) % MOD1, + + P2 = (P2 * P) % MOD2; + + } + + + + if (NA > NB) + + { + + out << 0; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * P + B[i]) % MOD1, + + hash2 = (hash2 * P + B[i]) % MOD2; + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + sol[Nr] = 0, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % MOD1 + MOD1) * P + B[i]) % MOD1; + + hash2 = ((hash2 - (B[i - NA] * P2) % MOD2 + MOD2) * P + B[i]) % MOD2; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + { + + if(Nr <= 1000) + + sol[Nr] = i-NA+1; + + Nr++; + + } + + } + + if (NA>NB) + + out << "0"; + + else { + + out < +#include +#include +using namespace std; + + + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2; + +int hasht1,hasht2; + + + + + +void Rabin_Karp() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1, hashp1=hashp2=0; + + for(int i=0; i m) + + { + + printf("0"); + + return 0; + + } + + Rabin_Karp(); + + + + return 0; + + + +} diff --git a/strmatch/rk/d2e2be5d-b09b-4c82-9deb-83f98ea52380.cpp b/strmatch/rk/d2e2be5d-b09b-4c82-9deb-83f98ea52380.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9678ac342f41e52cac56881b406e120d9b70a355 --- /dev/null +++ b/strmatch/rk/d2e2be5d-b09b-4c82-9deb-83f98ea52380.cpp @@ -0,0 +1,98 @@ +#include +using namespace std; + +const unsigned long long baza = 1010012383; + +const unsigned long long m = 1010012359; + +int v[ 1005 ], poz; + +unsigned long long sol; + +string a, b; + + + +int main() + +{ + + ifstream fin( "strmatch.in" ); + + fin >> a >> b; + + fin.close(); + + if( a.size() > b.size() ){ + + FILE *fout = fopen( "strmatch.out", "w" ); + + fprintf( fout, "0\n" ); + + fclose( fout ); + + } else { + + unsigned long long vala = 0; + + unsigned long long valb = 0; + + unsigned long long put = 1, i; + + for( i = 0; i < a.size(); i++ ){ + + vala = ( vala * baza + a[ i ] ) % m; + + put = ( put * baza ) % m; + + } + + for( i = 0; i < a.size(); i++ ) + + valb = ( valb * baza + b[ i ] ) % m; + + if( vala == valb ){ + + ++sol; + + v[ poz++ ] = 0; + + } + + for( i = 1; i <= b.size() - a.size(); i++ ){ + + valb = ( valb * baza + b[ i + a.size() - 1 ] ) % m; + + unsigned long long ceva = ( put * b[ i - 1 ] ) % m; + + valb = ( valb - ceva + m ) % m; + + if( vala == valb ){ + + ++sol; + + if( poz < 1000 ) + + v[ poz++ ] = i; + + } + + } + + FILE *fout = fopen( "strmatch.out", "w" ); + + fprintf( fout, "%lld\n", sol ); + + for( i = 0; i < poz; i++ ) + + fprintf( fout, "%lld ", v[ i ] ); + + fprintf( fout, "\n" ); + + fclose( fout ); + + } + + return 0; + +} diff --git a/strmatch/rk/d2e85542-1236-4384-ae57-47bacc6d32a0.cpp b/strmatch/rk/d2e85542-1236-4384-ae57-47bacc6d32a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dccb1b03eb18929f6491fb2c16626ed7584fac03 --- /dev/null +++ b/strmatch/rk/d2e85542-1236-4384-ae57-47bacc6d32a0.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + + + +using namespace std ; + + + +const long long MOD1 = 1e9 + 7 ; + +const long long MOD2 = 1e9 + 9 ; + +const long long BASE = 91 ; + +vector < int > v ; + + + +int main () + +{ + + ifstream in ( "strmatch.in" ) ; + + ofstream out ( "strmatch.out" ) ; + + + + string inp1 , inp2 ; + + long long hash11 , hash12 , hash21 , hash22, n , m , nr = 0 ; + + long long p1 , p2 , i ; + + + + in >> inp1 >> inp2 ; + + n = inp1.size () ; + + m = inp2.size () ; + + + + if ( n > m ) + + { + + out << 0 ; + + return 0 ; + + } + + + + hash11 = hash12 = hash21 = hash22 = 0 ; + + p1 = p2 = 1 ; + + + + for ( i = 0 ; i < n ; ++ i ) + + { + + hash11 = ( hash11 * BASE + inp1 [ i ] ) % MOD1 ; + + hash12 = ( hash12 * BASE + inp1 [ i ] ) % MOD2 ; + + + + hash21 = ( hash21 * BASE + inp2 [ i ] ) % MOD1 ; + + hash22 = ( hash22 * BASE + inp2 [ i ] ) % MOD2 ; + + + + if ( i ) + + { + + p1 = ( p1 * BASE ) % MOD1 ; + + p2 = ( p2 * BASE ) % MOD2 ; + + } + + } + + + + if ( hash11 == hash21 && hash12 == hash22 ) + + v.push_back ( 0 ) , ++ nr ; + + + + for ( i = n ; i < m ; ++ i ) + + { + + hash21 = ( ( hash21 - ( 1LL * inp2 [ i - n ] * p1 ) % MOD1 + MOD1 ) * BASE + inp2 [ i ] ) % MOD1 ; + + hash22 = ( ( hash22 - ( 1LL * inp2 [ i - n ] * p2 ) % MOD2 + MOD2 ) * BASE + inp2 [ i ] ) % MOD2 ; + + + + if ( hash11 == hash21 && hash12 == hash22 ) + + { + + ++ nr ; + + if ( v.size () < 1000 ) + + v.push_back ( i - n + 1 ) ; + + } + + } + + + + out << nr << '\n' ; + + for ( i = 0 ; i < v.size () ; ++ i ) + + out << v [ i ] << ' ' ; + + + + return 0 ; + +} diff --git a/strmatch/rk/d6098081-e9a8-4dce-8e76-48591185b77a.cpp b/strmatch/rk/d6098081-e9a8-4dce-8e76-48591185b77a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8a0b35807430176be0809193ec014373a8beca02 --- /dev/null +++ b/strmatch/rk/d6098081-e9a8-4dce-8e76-48591185b77a.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +const int NMAX=2000004; + +const int P=73; + +const int mod1=100007; + +const int mod2=100021; + +char a[NMAX],b[NMAX]; + +long long amod1,bmod1,amod2,bmod2,powm1=1,powm2=1,cnt,rasp[NMAX],la,lb; + +int main() + +{ + + in>>a>>b; + + la=strlen(a); + + lb=strlen(b); + + if(la>lb){ + + out<<0; + + return 0; + + } + + for(int i=0;i +#include +#include + + + + + + +using namespace std; + + + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + + + +struct RollingHash + +{ + + int a, mod, aMax; + + int value; + + + + RollingHash(int a, int mod) + + { + + this->a = a; + + this->mod = mod; + + aMax = 1; + + value = 0; + + } + + + + void initHash(const string &s) + + { + + for(int i = 0; i sol; + +int k; + + + +void debug(RollingHash r1, RollingHash r2, RollingHash r3, RollingHash r4) + +{ + + g<>pattern>>text; + + if(pattern.size() > text.size()) + + { + + return; + + } + + + + pattern1.initHash(pattern); + + pattern2.initHash(pattern); + + text1.initHash(text.substr(0, pattern.size())); + + text2.initHash(text.substr(0, pattern.size())); + + + + if(pattern1 == text1 && pattern2 == text2) + + { + + k++; + + sol.push_back(0); + + } + + for(int i = pattern.size(); i < text.size(); i++) + + { + + text1.roll(text[i], text[i - pattern.size()]); + + text2.roll(text[i], text[i - pattern.size()]); + + if(pattern1 == text1 && pattern2 == text2) + + { + + k++; + + if(sol.size() < 1000) + + sol.push_back(i - pattern.size() + 1); + + } + + } + +} + + + + + +void afisare() + +{ + + g< + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int MAXN = 2000005, BASE = 256, MOD[2] = {2000003, 2000029}, MAX = 1000; + + + +char sub[MAXN], str[MAXN]; + +int res[MAX + 5], subH[2], strH[2], s, n, power[2], nr; + + + +void getHash() + +{ + + power[0] = power[1] = 1; + + for (int i = 0; i < s; ++i) { + + for (int k = 0; k < 2; ++k) + + subH[k] = (subH[k] * BASE + sub[i]) % MOD[k], + + strH[k] = (strH[k] * BASE + str[i]) % MOD[k]; + + if (i) + + for (int k = 0; k < 2; ++k) + + power[k] = (power[k] * BASE) % MOD[k]; + + } + +} + + + +void solve() + +{ + + if (subH[0] == strH[0] && subH[1] == strH[1]) + + res[nr++] = 0; + + for (int i = s; i < n; ++i) { + + for (int k = 0; k < 2; ++k) + + strH[k] = ((strH[k] - (power[k] * str[i - s]) % MOD[k] + MOD[k]) * BASE + str[i]) % MOD[k]; + + if (subH[0] == strH[0] && subH[1] == strH[1]) { + + if (nr >= MAX) + + ++nr; + + else + + res[nr++] = i - s + 1; + + } + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + nr = min(nr, MAX); + + for (int i = 0; i < nr; ++i) + + fout << res[i] << ' '; + +} + + + +int main() + +{ + + fin >> sub >> str; + + n = strlen(str); + + s = strlen(sub); + + getHash(); + + solve(); + + print(); + + return 0; + +} diff --git a/strmatch/rk/d8098386-86e2-4be1-972e-ea400b8f828e.cpp b/strmatch/rk/d8098386-86e2-4be1-972e-ea400b8f828e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae31415f4ce6eb687d4441d194b12cea84798c70 --- /dev/null +++ b/strmatch/rk/d8098386-86e2-4be1-972e-ea400b8f828e.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string cuv,sir; + +unsigned long long Hcuv,A=915358133,B=974328597; + +unsigned long long h[2000001],putere[2000001]; + +int sol[1001],nr; + + + +int main() + +{ + + in>>cuv>>sir; + + + + for(auto lit:cuv) + + Hcuv=( Hcuv*A+lit )%B; + + + + h[0]=sir[0]%B; + + putere[0]=1; + + + + for(int i=1;i +#include +#include +using namespace std; + +ifstream in ("strmatch.in"); + +ofstream out ("strmatch.out"); + +char pattern[2000001], text[2000001]; + +bool match[2000001]; + +int m,n,nr,hashp1,hashp2,d1,d2,i; + +int hasht1,hasht2; + + + +void f() + +{ + + n=strlen(pattern); + + m=strlen(text); + + d1=d2=1; + + hashp1=hashp2=0; + + for(i=0;i>pattern>>text; + + if (n > m) + + { + + printf("0"); + + return 0; + + } + + f(); + + return 0; + + } diff --git a/strmatch/rk/d90de17c-80b3-49de-9b4c-1bcd482f352a.cpp b/strmatch/rk/d90de17c-80b3-49de-9b4c-1bcd482f352a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01ea993aad746293a96745fbd81937c61a38de3d --- /dev/null +++ b/strmatch/rk/d90de17c-80b3-49de-9b4c-1bcd482f352a.cpp @@ -0,0 +1,87 @@ +#include + +using namespace std; + +class RollingHash { +private: + int a{}, mod{}; + int max_power{1}, value{0}; + +public: + RollingHash() = default; + + RollingHash(int a, int mod) : a(a), mod(mod) {} + + void init(const string &s) { + for (int i = 0; i < s.length(); i++) { + value = (value * a + s[i]) % mod; + + if (i) max_power = (max_power * a) % mod; + } + } + + void roll(char out, char in) { + value = ((value - (out * max_power) % mod + mod) * a + in) % mod; + } + + bool operator==(const RollingHash &other) const { + return value == other.value; + } +}; + +tuple> rabin_karp(const string &pattern, const string &text) { + const int a = 31; + const int n1 = 100003, n2 = 100109; + + array hp{RollingHash(a, n1), RollingHash(a, n2)}; + array ht{RollingHash(a, n1), RollingHash(a, n2)}; + + if (pattern.length() > text.length()) { + return {false, {}}; + } + + for (int i = 0; i < 2; i++) { + hp[i].init(pattern); + ht[i].init(text.substr(0, pattern.length())); + } + + vector matches; + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(0); + + for (int i = pattern.length(); i < text.length(); i++) { + for (int k = 0; k < 2; k++) + ht[k].roll(text[i - pattern.length()], text[i]); + + if (hp[0] == ht[0] && hp[1] == ht[1]) + matches.push_back(i - pattern.length() + 1); + } + + return {true, matches}; +} + +int main() { + ifstream in("strmatch.in"); + ofstream out("strmatch.out"); + + string pattern, text; + + in >> pattern >> text; + + vector matches; + bool okay; + + tie(okay, matches) = rabin_karp(pattern, text); + + if (!okay) { + out << 0; + return 0; + } + + out << matches.size() << '\n'; + + int k = std::min(1000ul, matches.size()); + for (int i = 0; i < k; ++i) + out << matches[i] << ' '; +} diff --git a/strmatch/rk/dac973c6-a24b-4544-b806-db4504ea1226.cpp b/strmatch/rk/dac973c6-a24b-4544-b806-db4504ea1226.cpp new file mode 100644 index 0000000000000000000000000000000000000000..266dd3eafe81d05549f6d0b1a0cb713c1927d178 --- /dev/null +++ b/strmatch/rk/dac973c6-a24b-4544-b806-db4504ea1226.cpp @@ -0,0 +1,76 @@ +#include +#include + + + + + + + +char a[2000005], b[2000005]; +int nrla, nrlb, hashA1, hashA2, P1 = 1, P2 = 1, hash1, hash2, nr_pot; +bool match[2000005]; + +void citire() { + freopen("strmatch.in", "r", stdin); + freopen("strmatch.out", "w", stdout); + scanf("%s\n%s", a, b); + nrla = strlen(a); + nrlb = strlen(b); +} + +void initializare() { + for (int i = 0; i < nrla; ++i) { + hashA1 = (hashA1 * 73 + a[i]) % 100007; + hashA2 = (hashA2 * 73 + a[i]) % 100021; + + if (i != 0) { + P1 = (P1 * 73) % 100007; + P2 = (P2 * 73) % 100021; + } + } + + for (int i = 0; i < nrla; ++i) { + hash1 = (hash1 * 73 + b[i]) % 100007; + hash2 = (hash2 * 73 + b[i]) % 100021; + } +} + +void cautare_potriviri() { + if (hash1 == hashA1 && hash2 == hashA2) { + match[0] = true; + nr_pot++; + } + + for (int i = nrla; i < nrlb; ++i) { + hash1 = ((hash1 - (b[i - nrla] * P1) % 100007 + 100007) * 73 + b[i]) % 100007; + hash2 = ((hash2 - (b[i - nrla] * P2) % 100021 + 100021) * 73 + b[i]) % 100021; + + if (hash1 == hashA1 && hash2 == hashA2) { + match[i - nrla + 1] = true; + nr_pot++; + } + } +} + +void afisare() { + printf("%d\n", nr_pot); + nr_pot = 0; + for (int i = 0; i < nrlb && nr_pot < 1000; ++i) + if (match[i]) { + nr_pot++; + printf("%d ", i); + } +} + +int main() { + citire(); + if (nrla > nrlb) { + printf("0"); + return 0; + } + initializare(); + cautare_potriviri(); + afisare(); + return 0; +} diff --git a/strmatch/rk/db801128-a710-4c53-a2c1-eed0d78e8c90.cpp b/strmatch/rk/db801128-a710-4c53-a2c1-eed0d78e8c90.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3ee3a33cfa06806705606d0615274bb8f925af --- /dev/null +++ b/strmatch/rk/db801128-a710-4c53-a2c1-eed0d78e8c90.cpp @@ -0,0 +1,124 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +string s,s2; + +int n,b=101,mod1=100007,mod2=100021; + +int h1,h2,m,i,hash1,hash2,p1,p2,rasp,v[1001]; + + + +int main() + +{ + + fin>>s>>s2; + + n=s.length(); + + m=s2.length(); + + p1=1; + + p2=1; + + for(i=0; im) + + { + + fout<<0<<'\n'; + + return 0; + + } + + for(i=0; i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef string::const_iterator sit; + + + +class Roller{ + +private: + + int a, n; + + int ap = 1; + + int q = 0; + +public: + + Roller(); + + Roller(int a, int n, sit lt, sit rt) : a(a), n(n){ + + for(auto it = lt; it != rt; ++it){ + + q *= a; + + q += *it; + + q %= n; + + + + if(it!=lt)ap *= a; + + ap %= n; + + } + + } + + Roller(int a, int n, const string &str) : Roller(a,n,str.begin(),str.end()){} + + Roller(int a, int n, const string &str, int len) : Roller(a,n,str.begin(),str.begin()+len){} + + void roll(char out, char in){ + + q -= out*ap; + + q += n*128; + + q %= n; + + q *= a; + + q += in; + + q %= n; + + } + + bool operator==(const Roller &rhs){ + + return q == rhs.q; + + } + +}; + + + +const int a = 31; + +const int n1 = 1000003, n2 = 1000033; + + + +string pattern, text; + + + +int ans1 = 0; + +vector ans2; + + + +int main(){ + + + + fin >> pattern >> text; + + Roller rpattern[] = {Roller(a,n1,pattern),Roller(a,n2,pattern)}; + + Roller rtext[] = {Roller(a,n1,text,pattern.size()),Roller(a,n2,text,pattern.size())}; + + + + if(rpattern[0] == rtext[0] && rpattern[1] == rtext[1]){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(0); + + } + + + + for(int i = pattern.size(); i < text.size(); ++i){ + + rtext[0].roll(text[i-pattern.size()], text[i]); + + rtext[1].roll(text[i-pattern.size()], text[i]); + + if(rpattern[0] == rtext[0] && rpattern[1] == rtext[1]){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-pattern.size()+1); + + } + + } + + + + fout << ans1 << "\n"; + + for(auto a : ans2)fout << a << " "; + + return 0; + +} diff --git a/strmatch/rk/de37c432-c8cd-49f4-bc5a-cf431b89d00f.cpp b/strmatch/rk/de37c432-c8cd-49f4-bc5a-cf431b89d00f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a9c8ebaf021d8021498c37b6c9c6eae9d367e243 --- /dev/null +++ b/strmatch/rk/de37c432-c8cd-49f4-bc5a-cf431b89d00f.cpp @@ -0,0 +1,182 @@ +#include + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +const int h = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +string a; + +string b; + + + +vector rez; + + + +int main() + +{ + + in >> a; + + in >> b; + + + + int cnt = 0; + + + + + + int n = a.size(); + + int m = b.size(); + + + + if(n > m) + + { + + out << 0 <<'\n'; + + return 0; + + } + + + + int Sm1 = 0; + + int Sm2 = 0; + + int Sc1 = 0; + + int Sc2 = 0; + + + + int p1 = 1; + + int p2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + int nr1 = a[i]; + + int nr2 = b[i]; + + + + Sm1 = (Sm1 * h + nr1) % mod1; + + Sm2 = (Sm2 * h + nr1) % mod2; + + Sc1 = (Sc1 * h + nr2) % mod1; + + Sc2 = (Sc2 * h + nr2) % mod2; + + + + + + if(i != 0) + + {p1 = (p1 * h) % mod1; + + p2 = (p2 * h) % mod2; + + } + + + + } + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + rez.push_back(0); + + } + + + + + + for(int i = n; i < m; i++) + + { + + int nr1 = b[i - n]; + + int nr2 = b[i]; + + + + + + Sc1 = ((Sc1 - (nr1 * p1) % mod1 + mod1) * h + nr2) % mod1; + + Sc2 = ((Sc2 - (nr1 * p2) % mod2 + mod2) * h + nr2) % mod2; + + + + if(Sc1 == Sm1 && Sc2 == Sm2) + + { + + cnt++; + + if(cnt <= 1000) + + rez.push_back(i - n + 1); + + } + + + + } + + + + out << cnt<<'\n'; + + + + for(int i = 0; i < rez.size(); i++) + + out << rez[i]<<" "; + + + + + + return 0; + +} diff --git a/strmatch/rk/def2d5ac-db20-43d8-9cf4-f36dead44052.cpp b/strmatch/rk/def2d5ac-db20-43d8-9cf4-f36dead44052.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6f7417ce76abbe3a8a6ebca140e3cfbd442cef6 --- /dev/null +++ b/strmatch/rk/def2d5ac-db20-43d8-9cf4-f36dead44052.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int i, lg_a, lg_b, r[2000003]; + +int Hash_a1, Hash_a2, Hash1, Hash2, P1 = 1, P2 = 1; + +char a[2000003], b[2000003]; + + + +int main() + +{ + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + Hash_a1 = ( Hash_a1 * 80 + ( a[i] - '0' + 1 ) ) % 45007; + + Hash_a2 = ( Hash_a2 * 80 + ( a[i] - '0' + 1 ) ) % 45013; + + + + Hash1 = ( Hash1 * 80 + ( b[i] - '0' + 1 ) ) % 45007; + + Hash2 = ( Hash2 * 80 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( i != 0 ) + + { + + P1 = ( P1 * 80 ) % 45007; + + P2 = ( P2 * 80 ) % 45013; + + } + + } + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = 0; + + + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + Hash1 = ( ( ( Hash1 - ( ( b[i-lg_a] - '0' + 1 ) * P1 ) % 45007 + 45007 ) * 80 ) % 45007 + ( b[i] - '0' + 1 ) ) % 45007; + + Hash2 = ( ( ( Hash2 - ( ( b[i-lg_a] - '0' + 1 ) * P2 ) % 45013 + 45013 ) * 80 ) % 45013 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = i - lg_a + 1; + + } + + + + fout << r[0] << '\n'; + + r[0] = min ( 1000, r[0] ); + + for ( i = 1 ; i <= r[0] ; i++ ) fout << r[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/rk/e1f3159b-261c-4619-a62e-1d75d779b10e.cpp b/strmatch/rk/e1f3159b-261c-4619-a62e-1d75d779b10e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1760ca0115d0c08161d1392ce3ed189aec3d6f4e --- /dev/null +++ b/strmatch/rk/e1f3159b-261c-4619-a62e-1d75d779b10e.cpp @@ -0,0 +1,124 @@ +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +vectorpoz; + +const int MOD1=1e9+7; + +const int p1=131; + +const int MOD2=1e9+9; + +const int p2=135; + +pair < int, int > hsh(string s) + +{ + + int mod1=0,mod2=0,val1=1,val2=1; + + for(int poz=(int)s.size()-1;poz>=0;poz--) + + { + + mod1=(mod1+1LL*val1*(s[poz]-'0'))%MOD1; + + mod2=(mod2+1LL*val2*(s[poz]-'0'))%MOD2; + + val1=(1LL*val1*p1)%MOD1; + + val2=(1LL*val2*p2)%MOD2; + + } + + return {mod1, mod2}; + +} + + + +int main() + +{ + + string a,b; + + fin>>a>>b; + + int nr=0; + + pair < int, int > hsa=hsh(a); + + string s=b.substr(0,a.size()); + + pair < int, int > hsb=hsh(s); + + int put1=1, put2=1; + + for(int i=1;i<=(int)a.size();i++) + + { + + put1=(1LL*put1*p1)%MOD1; + + put2=(1LL*put2*p2)%MOD2; + + } + + for(int i=a.size();i<(int)b.size();i++) + + { + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(i); + + } + + hsb.first=(1LL*hsb.first*p1+b[i]-'0')%MOD1; + + hsb.second=(1LL*hsb.second*p2+b[i]-'0')%MOD2; + + hsb.first-=(1LL*put1*(b[i-a.size()]-'0'))%MOD1; + + hsb.second-=(1LL*put2*(b[i-a.size()]-'0'))%MOD2; + + if(hsb.first<0) + + hsb.first+=MOD1; + + if(hsb.second<0) + + hsb.second+=MOD2; + + } + + if(hsa==hsb) + + { + + nr++; + + if(nr<=1000) + + poz.push_back(b.size()); + + } + + fout< +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +char a[2000010],b[2000010]; + +const long long p1=31,p2=101,mod1=423957231,mod2=298892145; + +long long stra,strb; + + + +void citire() + +{ + + in>>a>>b; + + stra=strlen(a); + + strb=strlen(b); + +} + +void solv() + +{ + + long long hasha1=0,hasha2=0,hashb1=0,hashb2=0,c1=1,c2=1; + + vectorsol; + + for(int i=0;istrb) + + { + + out<<0; + + return 0; + + } + + solv(); + + return 0; + +} diff --git a/strmatch/rk/e4a9be42-00a3-4eb1-9b30-7ed956b8eb60.cpp b/strmatch/rk/e4a9be42-00a3-4eb1-9b30-7ed956b8eb60.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a599479692bf59e0aa520c50869129523484728c --- /dev/null +++ b/strmatch/rk/e4a9be42-00a3-4eb1-9b30-7ed956b8eb60.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +unsigned long long H[2000005]; + +vector ans; + + + + + + + +void rabin_karp(string const& a, string const& b) { + + const unsigned long long base = 167; + + + + unsigned long long hsrch = 0; + + for (auto& x : a) { + + hsrch *= base; + + hsrch += x; + + } + + + + unsigned long long pow = 1; + + for (int i = 1; i <= a.size(); i++) pow *= base; + + + + for (int i = 1; i <= b.size(); i++) { + + H[i] = H[i - 1] * base; + + H[i] += b[i - 1]; + + + + if (i < a.size()) continue; + + + + unsigned long long hash = H[i] - H[i - a.size()] * pow; + + + + if (hash == hsrch) ans.push_back(i - a.size()); + + } + + + +} + + + + + + + +int main() { + + + + string a, b; cin >> a >> b; + + + + rabin_karp(a, b); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + + + + + return 0; + +} diff --git a/strmatch/rk/e4d44a40-3773-463f-a5fd-510c0555aa8b.cpp b/strmatch/rk/e4d44a40-3773-463f-a5fd-510c0555aa8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f79243258c2526f1a4c5c37a853804a7be28b20f --- /dev/null +++ b/strmatch/rk/e4d44a40-3773-463f-a5fd-510c0555aa8b.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef long long ll; + + + +const ll p = 256, MOD1 = 100007, MOD2 = 100021; + + + +ll p1 = 1, p2 = 1, hasha1, hasha2, hash1, hash2; + +string txt, pat; + +vector v; + + + +int main() + +{ + + fin>>pat>>txt; + + for(int i = 0; i < pat.length(); ++i) { + + hasha1 = (hasha1 * p + pat[i]) % MOD1, + + hasha2 = (hasha2 * p + pat[i]) % MOD2; + + + + if(i) p1 = (p1 * p) % MOD1, p2 = (p2 * p) % MOD2; + + } + + + + for(int i = 0; i < pat.length(); ++i) + + hash1 = (hash1 * p + txt[i]) % MOD1, + + hash2 = (hash2 * p + txt[i]) % MOD2; + + + + if(hash1 == hasha1 && hash2 == hasha2) v.push_back(0); + + + + for(int i = pat.length(); i < txt.length(); ++i) { + + hash1 = ((hash1 - (txt[i - pat.length()] * p1) % MOD1 + MOD1) * p + txt[i]) % MOD1, + + hash2 = ((hash2 - (txt[i - pat.length()] * p2) % MOD2 + MOD2) * p + txt[i]) % MOD2; + + + + if(hash1 == hasha1 && hash2 == hasha2) v.push_back(i - pat.length() + 1); + + } + + + + fout< + + +using namespace std; + +ifstream r("strmatch.in"); + +ofstream w("strmatch.out"); + +const int base=73, mod1=100007, mod2=100021; + +string a, b; + +int n, m, h1, h2, x, y; + +char match[2000001]; + +int main() + +{ + + r>>a>>b; + + n = a.size(); + + m = b.size(); + + x = y = 1; + + h1 = h2 = 0; + + for (int i = 0; i < n; i++) + + { + + h1 = (h1 * base + a[i]) % mod1; + + h2 = (h2 * base + a[i]) % mod2; + + if (i != 0) + + { + + x = (x * base) % mod1, + + y = (y * base) % mod2; + + } + + } + + + + if (n > m) + + { + + w<<0; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < n; i++) + + { + + hash1 = (hash1 * base + b[i]) % mod1; + + hash2 = (hash2 * base + b[i]) % mod2; + + } + + int nr = 0; + + if (hash1 == h1 && hash2 == h2) + + match[0] = 1, nr++; + + + + for (int i = n; i < m; i++) + + { + + hash1 = ((hash1 - (b[i - n] * x) % mod1 + mod1) * base + b[i]) % mod1; + + hash2 = ((hash2 - (b[i - n] * y) % mod2 + mod2) * base + b[i]) % mod2; + + + + if (hash1 == h1 && hash2 == h2) + + { + + match[ i - n + 1 ] = 1; + + nr++; + + } + + } + + w< +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/e99b83b0-5cb3-4dff-8eb2-f05961f7714e.cpp b/strmatch/rk/e99b83b0-5cb3-4dff-8eb2-f05961f7714e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..85abecc38c0ae9f6ea300d613ac2feb0f4612a7b --- /dev/null +++ b/strmatch/rk/e99b83b0-5cb3-4dff-8eb2-f05961f7714e.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + + +using namespace std; + + + +const int MOD = 1000000007; + +const int MAX_LENGTH = 2000000; + +const int BASE = 62; + + + +char s1[MAX_LENGTH + 5]; + +char s2[MAX_LENGTH + 5]; + +int p; + + + +int code(char ch) { + + if (ch >= 'a' && ch <= 'z') { + + return ch - 'a' + 1; + + } else if (ch >= 'A' && ch <= 'Z') { + + return 26 + ch - 'A' + 1; + + } else { + + return 52 + ch - '0' + 1; + + } + +} + + + +int remove_first(int h, char ch) { + + int newH; + + newH = h - (1LL * p * code(ch)) % MOD; + + if (newH < 0) + + newH += MOD; + + return newH; + +} + + + +int add(int h, char ch) { + + return (1LL * h * BASE + code(ch)) % MOD; + +} + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + int n, m; + + scanf("%s\n%s", s1, s2); + + n = strlen(s1); + + m = strlen(s2); + + + + if (n > m) { + + printf("0\n"); + + return 0; + + } + + + + int h1; + + h1 = 0; + + for (int i = 0; i < n; i++) + + h1 = add(h1, s1[i]); + + + + int h2; + + h2 = 0; + + for (int i = 0; i < n; i++) + + h2 = add(h2, s2[i]); + + + + p = 1; + + for (int i = 1; i <= n - 1; i++) + + p = (1LL * p * BASE) % MOD; + + + + int ans; + + vector sol; + + ans = 0; + + if (h1 == h2) { + + sol.push_back(0); + + ans++; + + } + + for (int i = n; i < m; i++) { + + h2 = remove_first(h2, s2[i - n]); + + h2 = add(h2, s2[i]); + + if (h1 == h2) { + + ans++; + + if (sol.size() < 1000) + + sol.push_back(i - n + 1); + + } + + } + + + + printf("%d\n", ans); + + for (auto it : sol) + + printf("%d ", it); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/e9e6188f-3ead-46e9-9f90-741aa68cb925.cpp b/strmatch/rk/e9e6188f-3ead-46e9-9f90-741aa68cb925.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10cafc0414a5ea99a43639d393bce5d95c200034 --- /dev/null +++ b/strmatch/rk/e9e6188f-3ead-46e9-9f90-741aa68cb925.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +using namespace std; + +const int NMAX = 2000005; + +const long long MOD1 = 1000000007; + +const long long MOD2 = 1000000009; + +const long long BAZA = 62; + +char a[NMAX], b[NMAX]; + +long long kkt(char ch) { + + int ad = 0; + + if('0' <= ch && ch <= '9') { + + return ch - '0' + 1; + + } + + ad += ('9' - '0' + 1); + + if('A' <= ch && ch <= 'Z') { + + return ad + ch - 'A' + 1; + + } + + ad += ('Z' - 'A' + 1); + + return ad + ch - 'a' + 1; + +} + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", a + 1); + + int n = (int)strlen(a + 1); + + long long c1 = 0, c2 = 0, p1 = 1, p2 = 1; + + for(int i = 1; i <= n; i++) { + + c1 = ((c1 * BAZA) + kkt(a[i])) % MOD1; + + c2 = ((c2 * BAZA) + kkt(a[i])) % MOD2; + + if(i < n) { + + p1 = (p1 * BAZA) % MOD1; + + p2 = (p2 * BAZA) % MOD2; + + } + + } + + scanf("%s", b + 1); + + int m = (int)strlen(b + 1); + + long long a1 = 0, a2 = 0; + + for(int i = 1; i <= m; i++) { + + if(i > n) { + + a1 = (a1 - (kkt(b[i - n]) * p1) % MOD1 + MOD1) % MOD1; + + a2 = (a2 - (kkt(b[i - n]) * p2) % MOD2 + MOD2) % MOD2; + + } + + a1 = ((a1 * BAZA) + kkt(b[i])) % MOD1; + + a2 = ((a2 * BAZA) + kkt(b[i])) % MOD2; + + if(i >= n) { + + if(a1 == c1 && a2 == c2) { + + sol.push_back(i - n); + + } + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 1; i <= 1000 && i <= sol.size(); i++) { + + printf("%d ", sol[i - 1]); + + } + + printf("\n"); + + return 0; + +} diff --git a/strmatch/rk/eafcec00-6e4f-4807-a733-8cb856a9e037.cpp b/strmatch/rk/eafcec00-6e4f-4807-a733-8cb856a9e037.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ffac29f869a14457d315affa94fd0eb55d6514e3 --- /dev/null +++ b/strmatch/rk/eafcec00-6e4f-4807-a733-8cb856a9e037.cpp @@ -0,0 +1,88 @@ +#include ///Rabin-Karp +using namespace std; + +ifstream fin ("strmatch.in"); + +ofstream fout("strmatch.out"); + + + + + + + +char s1[2000009],s2[2000009]; + +int p1=1,p2=1; + +int ans[1009],k,n,m; + +int HASH1,HASH2 ,Hash1,Hash2; + +int main() + +{ + + fin>>s1>>s2; + + n=strlen(s1); + + m=strlen(s2); + + for(int i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef string::iterator sit; + + + +class Roller{ + +public: + + int a, n; + + int ap = 1; + + int q = 0; + + Roller(); + + Roller(int a, int n, sit lt, sit rt) : a(a), n(n){ + + for(auto it = lt; it != rt; ++it){ + + q *= a; + + q += *it; + + q %= n; + + + + if(it!=lt)ap *= a; + + ap %= n; + + } + + } + + void roll(char out, char in){ + + q -= out*ap; + + q += n*128; + + q %= n; + + q *= a; + + q += in; + + q %= n; + + } + +}; + + + +const int a = 31; + +const int n1 = 1000003, n2 = 1000033; + + + +string pattern, text; + + + +int ans1 = 0; + +vector ans2; + + + +int main(){ + + + + fin >> pattern >> text; + + Roller rpattern[] = {Roller(a,n1,pattern.begin(),pattern.end()),Roller(a,n2,pattern.begin(),pattern.end())}; + + Roller rtext[] = {Roller(a,n1,text.begin(),text.begin()+pattern.size()),Roller(a,n2,text.begin(),text.begin()+pattern.size())}; + + + + if(rpattern[0].q == rtext[0].q && rpattern[1].q == rtext[1].q){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(0); + + } + + + + for(int i = pattern.size(); i < text.size(); ++i){ + + rtext[0].roll(text[i-pattern.size()], text[i]); + + rtext[1].roll(text[i-pattern.size()], text[i]); + + if(rpattern[0].q == rtext[0].q && rpattern[1].q == rtext[1].q){ + + ans1++; + + if(ans1 <= 1000)ans2.push_back(i-pattern.size()+1); + + } + + } + + + + fout << ans1 << "\n"; + + for(auto a : ans2)fout << a << " "; + + return 0; + +} diff --git a/strmatch/rk/eb87c148-ea1f-4f0d-82ab-2db0218e421e.cpp b/strmatch/rk/eb87c148-ea1f-4f0d-82ab-2db0218e421e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d1a5ba7eb83621ff1a59c3c04f862e418dca063 --- /dev/null +++ b/strmatch/rk/eb87c148-ea1f-4f0d-82ab-2db0218e421e.cpp @@ -0,0 +1,118 @@ +#include + + +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int nmax = 2000005; + +const int p = 73; + +const int mod1 = 100007; + +const int mod2 = 100021; + + + +char a[nmax], b[nmax]; + +int na, nb; + +int hasha1, hasha2, p1, p2; + +char match[nmax]; + +int main() + +{ + + fin >> a >> b; + + na = strlen(a); + + nb = strlen(b); + + p1 = p2 = 1; + + hasha1 = hasha2 = 0; + + for(int i = 0; i < na; ++i) + + { + + hasha1 = (hasha1 * p + a[i]) % mod1; + + hasha2 = (hasha2 * p + a[i]) % mod2; + + if(i != 0) + + { + + p1 = (p1 * p) % mod1; + + p2 = (p2 * p) % mod2; + + } + + } + + if(na > nb) + + { + + fout << "0\n"; + + return 0; + + } + + int hash1 = 0, hash2 = 0; + + for(int i = 0; i < na; ++i) + + { + + hash1 = (hash1 * p + b[i]) % mod1; + + hash2 = (hash2 * p + b[i]) % mod2; + + } + + int nr = 0; + + if(hash1 == hasha1 && hash2 == hasha2) + + match[0] = 1, nr++; + + for(int i = na; i < nb; ++i) + + { + + hash1 = ((hash1 - (b[i - na] * p1) % mod1 + mod1) * p + b[i]) % mod1; + + hash2 = ((hash2 - (b[i - na] * p2) % mod2 + mod2) * p + b[i]) % mod2; + + if(hash1 == hasha1 && hash2 == hasha2) + + match[i - na + 1] = 1, nr++; + + } + + fout << nr << "\n"; + + nr = 0; + + for(int i = 0; i < nb && nr < 1000; ++i) + + if(match[i]) + + nr++, fout << i << " "; + + + + return 0; + +} diff --git a/strmatch/rk/ec2a8ff3-a36a-4087-82d9-51df5df08a6c.cpp b/strmatch/rk/ec2a8ff3-a36a-4087-82d9-51df5df08a6c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3fb45b2687a8ceb48b1a7e9e850dc1df691722de --- /dev/null +++ b/strmatch/rk/ec2a8ff3-a36a-4087-82d9-51df5df08a6c.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int MAX_NO_POS = 1000; + +const int BASE = 62; + +const int MOD = 10007; + +const int MOD2 = 10009; + + + +void rabinKarp(string & A, string & B, int & n, list & ans) { + + if (A.size() > B.size()) { + + return; + + } + + + + int hashWindow = 0; + + int hashWindow2 = 0; + + int hashPattern = 0; + + int hashPattern2 = 0; + + int pow = 1; + + int pow2 = 1; + + for (int i = 0; i < A.size() - 1; i++) { + + pow = (pow * BASE) % MOD; + + pow2 = (pow2 * BASE) % MOD2; + + } + + for (int i = 0; i < A.size(); i++) { + + hashWindow = (BASE * hashWindow + B[i]) % MOD; + + hashWindow2 = (BASE * hashWindow2 + B[i]) % MOD2; + + hashPattern = (BASE * hashPattern + A[i]) % MOD; + + hashPattern2 = (BASE * hashPattern2 + A[i]) % MOD2; + + } + + if (hashWindow == hashPattern && hashWindow2 == hashPattern2) { + + n++; + + ans.push_back(0); + + } + + + + for (int i = A.size(); i < B.size(); i++) { + + hashWindow = (BASE * (hashWindow - (pow * B[i - A.size()])) + B[i]) % MOD; + + hashWindow2 = (BASE * (hashWindow2 - (pow2 * B[i - A.size()])) + B[i]) % MOD2; + + hashWindow += (hashWindow < 0) ? MOD : 0; + + hashWindow2 += (hashWindow2 < 0) ? MOD2 : 0; + + if (hashWindow == hashPattern && hashWindow2 == hashPattern2) { + + n++; + + if (n <= MAX_NO_POS) { + + ans.push_back(i - A.size() + 1); + + } + + } + + } + +} + + + +int main() { + + ifstream fin; + + ofstream fout; + + string A, B; + + + + fin.open("strmatch.in"); + + fin >> A >> B; + + fin.close(); + + + + int n = 0; + + list ans; + + rabinKarp(A, B, n, ans); + + + + fout.open("strmatch.out"); + + fout << n << endl; + + for (auto it = ans.begin(); it != ans.end(); ++it) { + + fout << *it << " "; + + } + + fout.close(); + + + + ans.clear(); + + + + return 0; + +} diff --git a/strmatch/rk/ec5176da-3b02-4f55-84e5-afa4c0e025fd.cpp b/strmatch/rk/ec5176da-3b02-4f55-84e5-afa4c0e025fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..43683058ec261cf21fcb49680adb6513e4f00388 --- /dev/null +++ b/strmatch/rk/ec5176da-3b02-4f55-84e5-afa4c0e025fd.cpp @@ -0,0 +1,179 @@ +#include +#include +#include +#include +using namespace std; + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int MOD=1000000007; + +const int baza=597813; + + + +string n,m; + +int nrN,nrM; + +vector ind; + + + +void read() + +{ + + fin>>n>>m; + + nrN=n.length(); + + nrM=m.length(); + +} + + + +void solve() + +{ + + int_fast64_t hashN=0,hashM=0,baseNrN=1; + + + + for(int i=1;inrM) + + { + + fout<<0; + + return 0; + + } + + + + solve(); + + + + return 0; + +} diff --git a/strmatch/rk/ed0ce1ce-7f85-4bec-b773-5cfac2845e7d.cpp b/strmatch/rk/ed0ce1ce-7f85-4bec-b773-5cfac2845e7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..90489bee1f76f2879cd43426a442a11a1f9fe5f0 --- /dev/null +++ b/strmatch/rk/ed0ce1ce-7f85-4bec-b773-5cfac2845e7d.cpp @@ -0,0 +1,87 @@ +#include +#include +char a[2000001],b[2000001]; + +int v[2000001]; + +int main(){ + + FILE *fin=fopen("strmatch.in","r"); + + FILE *fout=fopen("strmatch.out","w"); + + int n,m,i,am1,am2,bm1,bm2,p1,p2,sol=0; + + + + fscanf(fin,"%s",&a); + + fscanf(fin,"%s",&b); + + n=strlen(a); + + m=strlen(b); + + if(n>m) + + sol=0; + + else{ + + am1=am2=bm1=bm2=0; + + for(i=0; i +#include +char A[2000001], B[2000001]; + +int NA, NB; + + + +int hashA1, hashA2, P1, P2; + + + +char match[2000001]; + + + +int main() + +{ + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + + + scanf("%s %s", A, B); + + NA = strlen(A); + + NB = strlen(B); + + + + P1 = P2 = 1; + + hashA1 = hashA2 = 0; + + for (int i = 0; i < NA; i++) + + { + + hashA1 = (hashA1 * 73 + A[i]) % 100007; + + hashA2 = (hashA2 * 73 + A[i]) % 100021; + + + + if (i != 0) + + P1 = (P1 * 73) % 100007, + + P2 = (P2 * 73) % 100021; + + } + + + + if (NA > NB) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0, hash2 = 0; + + for (int i = 0; i < NA; i++) + + hash1 = (hash1 * 73 + B[i]) % 100007, + + hash2 = (hash2 * 73 + B[i]) % 100021; + + + + int Nr = 0; + + if (hash1 == hashA1 && hash2 == hashA2) + + match[0] = 1, Nr++; + + + + for (int i = NA; i < NB; i++) + + { + + hash1 = ((hash1 - (B[i - NA] * P1) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - NA] * P2) % 100021 + 100021) * 73 + B[i]) % 100021; + + + + if (hash1 == hashA1 && hash2 == hashA2) + + match[ i - NA + 1 ] = 1, Nr++; + + } + + printf("%d\n", Nr); + + + + Nr = 0; + + for (int i = 0; i < NB && Nr < 1000; i++) + + if (match[i]) + + Nr++, + + printf("%d ", i); + + printf("\n"); + + + + return 0; + +} diff --git a/strmatch/rk/ee4d2bd7-4821-48a3-9e82-43f7650f6e88.cpp b/strmatch/rk/ee4d2bd7-4821-48a3-9e82-43f7650f6e88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4f8d088b9cb37e1187d98de873a59f8406cc390 --- /dev/null +++ b/strmatch/rk/ee4d2bd7-4821-48a3-9e82-43f7650f6e88.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +char a[2000001], b[2000001], ok[2000001]; + + + +int i, j, na, nb, ns1, ns2, nsa1, nsa2, p1, p2, nr; + + + +using namespace std; + + + +int main() { + + + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin.getline(a, 2000001); + + fin.getline(b, 2000001); + + na=strlen(a); + + nb=strlen(b); + + p1=p2=1; + + nsa1=nsa2=0; + + for(i=0;inb) { + + fout<<"0"; + + } + + else { + + ns1=ns2=0; + + for(i=0;i + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +typedef long long ll; + + + +const int baza = 543217; + +const int mod = 100000007; + + + +string n, m; + +int N, M; + +vector ans; + + + +void read(){ + + fin >> n >> m; + + N = n.length(); + + M = m.length(); + +} + + + +void solve(){ + + ll hashN = 0, hashM = 0, bazaN = 1; + + + + for(int i = 1; i < N; i++) + + bazaN = (1LL * bazaN * baza) % mod; + + + + for(int i = 0; i < N; i++){ + + hashN = (hashN * baza * 1LL + n[i] - '0')%mod; + + hashM = (hashM * baza * 1LL + m[i] - '0')%mod; + + } + + + + if(hashN == hashM) + + ans.push_back(0); + + + + for(int i = N; i < M; i++){ + + hashM = hashM - (m[i - N] - '0') * bazaN * 1LL; + + hashM %= mod; + + + + hashM = hashM * baza * 1LL + (m[i] - '0'); + + hashM %= mod; + + + + if(hashM < 0) + + hashM += mod; + + + + if(hashM == hashN) + + ans.push_back(i - N + 1); + + } + + + + fout << ans.size() << "\n"; + + + + for(int i = 0; i < ans.size(); i++){ + + fout << ans[i] << " "; + + + + if(i == 999) + + break; + + } + +} + + + +int main() + +{ + + read(); + + solve(); + + + + return 0; + +} diff --git a/strmatch/rk/ef81dc57-af97-4eed-9bfd-36dad4df7820.cpp b/strmatch/rk/ef81dc57-af97-4eed-9bfd-36dad4df7820.cpp new file mode 100644 index 0000000000000000000000000000000000000000..296a729f304ec63e1e1d70ae488ecdbbba112428 --- /dev/null +++ b/strmatch/rk/ef81dc57-af97-4eed-9bfd-36dad4df7820.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + + + + + +char pat[2000001],sir[2000001]; + +int poz[2000001]; + +int main() + +{ + + int m1=666013; + + int m2=800057; + + int p=72,p1,p2,h1,h2,la,lb,i,s,H1,H2; + + cin>>pat; + + cin>>sir; + + la=strlen(pat); + + lb=strlen(sir); + + p1=p2=1; + + h1=h2=0; + + for(i=0;i=1){ + + p1=(p1*p)%m1; + + p2=(p2*p)%m2; + + } + + } + + if(la>lb){ + + cout<<0<<'\n'; + + return 0; + + } + + H1=H2=0; + + for(i=0;i +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +const int MOD = 1e9; + +const int BASE = 997; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = " " + b; + + int n = a.size() - 1, m = b.size() - 1, needle_hash = 0; + + for(int i = 1; i <= n; i ++) + + needle_hash = (1LL * needle_hash * BASE + 1LL * (a[i] - '0')) % MOD; + + + + vector p(m + 1, 1); + + for(int i = 1; i <= m; i ++) + + p[i] = (1LL * BASE * p[i - 1]) % MOD; + + + + vector hs(m + 1, 0); + + int cnt = 0; + + vector ans; + + for(int i = 1; i <= m; i ++) { + + hs[i] = (1LL * hs[i - 1] * BASE + 1LL * (b[i] - '0')) % MOD; + + if(i < n) + + continue; + + int pretender = (MOD + hs[i] - ((1LL * hs[i - n] * p[n]) % MOD)) % MOD; + + if(pretender == needle_hash) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/rk/f0b3c87a-7d83-4868-80d2-b61155d928f3.cpp b/strmatch/rk/f0b3c87a-7d83-4868-80d2-b61155d928f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..07ba1af3d341164191ce2c178df970440cd9338e --- /dev/null +++ b/strmatch/rk/f0b3c87a-7d83-4868-80d2-b61155d928f3.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +char s1[2000005],s2[2000005]; + +int ap[2000005]; + +const int mod1=1e9+7; + +const int mod2=1e9+9; + +const int baza=97; + +int main() + +{ + + freopen("strmatch.in","r",stdin); + + freopen("strmatch.out","w",stdout); + + fgets(s1+1,2000005,stdin); + + fgets(s2+1,2000005,stdin); + + int n=strlen(s1+1),m=strlen(s2+1); + + long long put1=1,put2=1,rez1=0,rez2=0; + + --n;--m; + + int i,j,cnt=0; + + for(i=1;i=n) + + ap[++cnt]=i; + + } + + printf("%d\n",cnt); + + for(i=1;i<=min(1000,cnt);++i)printf("%d ",ap[i]-n); + + return 0; + +} diff --git a/strmatch/rk/f1a37b2a-a032-4ed1-ad5e-3a5980b7aeb1.cpp b/strmatch/rk/f1a37b2a-a032-4ed1-ad5e-3a5980b7aeb1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..389952dccb756bbb1fbebb18c221ab92e16f26e2 --- /dev/null +++ b/strmatch/rk/f1a37b2a-a032-4ed1-ad5e-3a5980b7aeb1.cpp @@ -0,0 +1,127 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +const unsigned long long base = 167; + +vector vans; + +ll ans; + +unsigned long long H[2000005]; +int main() { + + + + string a, b; cin >> a >> b; + + + + unsigned long long hsrch = 0; + + for (auto& x : a) { + + hsrch *= base; + + hsrch += x; + + } + + + + unsigned long long pow = 1; + + for (int i = 1; i <= a.size(); i++) { + + pow *= base; + + } + + + + for (int i = 1; i <= b.size(); i++) { + + H[i] = H[i - 1] * base; + + H[i] += b[i - 1]; + + + + if (i < a.size()) { + + continue; + + } + + + + unsigned long long hash = H[i] - H[i - a.size()] * pow; + + + + if (hash == hsrch) { + + ans++; + + if (vans.size() < 1000) vans.push_back(i - a.size()); + + } + + } + + + + cout << ans << '\n'; + + for (auto& x : vans) { + + cout << x << ' '; + + } + + + + + + return 0; + +} diff --git a/strmatch/rk/f7410549-26b8-4055-b1e6-50df3e3796c4.cpp b/strmatch/rk/f7410549-26b8-4055-b1e6-50df3e3796c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6059933fb93be1f2d701338cdb07c2152496fcde --- /dev/null +++ b/strmatch/rk/f7410549-26b8-4055-b1e6-50df3e3796c4.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + +const int MAXN = 2000005, BASE = 256, MOD[2] = {2000003, 2000029}, MAX = 1000; + + + +char sub[MAXN], str[MAXN]; + +int res[MAX + 5], subH[2], strH[2], s, n, power[2], nr; + + + +void getHash() + +{ + + power[0] = power[1] = 1; + + for (int i = 0; i < s; ++i) { + + for (int k = 0; k < 2; ++k) + + subH[k] = (subH[k] * BASE + sub[i]) % MOD[k], + + strH[k] = (strH[k] * BASE + str[i]) % MOD[k]; + + if (i) + + for (int k = 0; k < 2; ++k) + + power[k] = (power[k] * BASE) % MOD[k]; + + } + +} + + + +void solve() + +{ + + if (subH[0] == strH[0] && subH[1] == strH[1]) + + res[nr++] = 0; + + for (int i = s; i < n; ++i) { + + strH[0] = ((strH[0] - (power[0] * str[i - s]) % MOD[0] + MOD[0]) * BASE + str[i]) % MOD[0]; + + strH[1] = ((strH[1] - (power[1] * str[i - s]) % MOD[1] + MOD[1]) * BASE + str[i]) % MOD[1]; + + if (subH[0] == strH[0] && subH[1] == strH[1]) { + + if (nr >= MAX) + + ++nr; + + else + + res[nr++] = i - s + 1; + + } + + } + +} + + + +void print() + +{ + + fout << nr << '\n'; + + nr = min(nr, MAX); + + for (int i = 0; i < nr; ++i) + + fout << res[i] << ' '; + +} + + + +int main() + +{ + + fin >> sub >> str; + + n = strlen(str); + + s = strlen(sub); + + getHash(); + + solve(); + + print(); + + return 0; + +} diff --git a/strmatch/rk/f7e443df-8d84-4b08-a4e2-3f4c94c64d10.cpp b/strmatch/rk/f7e443df-8d84-4b08-a4e2-3f4c94c64d10.cpp new file mode 100644 index 0000000000000000000000000000000000000000..97aebbb3ef2ff6c2d282f20c8e495e899cba398c --- /dev/null +++ b/strmatch/rk/f7e443df-8d84-4b08-a4e2-3f4c94c64d10.cpp @@ -0,0 +1,122 @@ +#include +#include +#include + + +using namespace std; + +ifstream fi("strmatch.in"); + +ofstream fo("strmatch.out"); + +const int lmax=2e6, mod1=1e5+7, mod2=1e5+21, baza=75; + +char T[lmax+5], P[lmax+5]; + +int lgt, lgp, putere1=1, putere2=1, codp1, codp2, rhash1, rhash2, cnt; + +vector sol; + + + +int cod(char c) + +{ + + return (c-'0'+1); + +} + + + +int main() + +{ + + fi>>P>>T; + + lgp=strlen(P); + + lgt=strlen(T); + + + + for(int i=1; i<=lgp; i++) + + { + + putere1=(putere1*baza)%mod1; + + putere2=(putere2*baza)%mod2; + + } + + + + for(int i=0; i +#include +#include +using namespace std; + + + +char A[2000001], B[2000001]; +int main() + +{ + + vector result; + + + + freopen("strmatch.in", "rt", stdin); + + freopen("strmatch.out", "wt", stdout); + + scanf("%s %s", A, B); + + + + int M = strlen(A); + + int N = strlen(B); + + + + + + + + int p = 1, p2 = 1, n = 0; + + int aVal = 0; + + int aVal2 = 0; + + + + if (M > N) + + { + + printf("0\n"); + + return 0; + + } + + + + int hash1 = 0; + + int hash2 = 0; + + + + for (int i = 0; i < M; i++) + + { + + aVal = (aVal * 73 + A[i]) % 100007; + + aVal2 = (aVal2 * 73 + A[i]) % 100011; + + hash1 = (hash1 * 73 + B[i]) % 100007; + + hash2 = (hash2 * 73 + B[i]) % 100011; + + if (i != 0) + + { + + p = (p * 73) % 100007; + + p2 = (p2 * 73) % 100011; + + } + + } + + + + if (hash1 == aVal && hash2 == aVal2) + + { + + n++; + + result.push_back(0); + + } + + + + for (int i = M; i < N; i++) + + { + + hash1 = ((hash1 - (B[i - M] * p) % 100007 + 100007) * 73 + B[i]) % 100007; + + hash2 = ((hash2 - (B[i - M] * p2) % 100011 + 100011) * 73 + B[i]) % 100011; + + if (hash1 == aVal && hash2 == aVal2) + + { + + if(n < 1000) + + result.push_back(i - M + 1); + + n++; + + } + + } + + + + printf("%d\n", n); + + for (int i = 0; i < result.size(); i++) + + { + + printf("%d ", result[i]); + + } + + + + return 0; + +} diff --git a/strmatch/rk/fdc3c9a8-692f-4c1d-9312-694cda90f590.cpp b/strmatch/rk/fdc3c9a8-692f-4c1d-9312-694cda90f590.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc42afff3f40577b695c94e0c5ddc207edfb1aab --- /dev/null +++ b/strmatch/rk/fdc3c9a8-692f-4c1d-9312-694cda90f590.cpp @@ -0,0 +1,158 @@ +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int Nmax = 2e6 + 5; + +const int p = 73; + +const int Mod1 = 1e5 + 21; + +const int Mod2 = 1e5 + 7; + + + +bitset viz; + +char a[Nmax], b[Nmax]; + + + +int main() + +{ + + fin >> a; + + fin >> b; + + + + int n, m, contor = 0; + + n = strlen(a); + + m = strlen(b); + + + + int hash_a1 = 0, hash_a2 = 0, pLg1 = 1, pLg2 = 1; + + + + for(int i = 0; i < n; i++) + + { + + hash_a1 = (hash_a1 * p + a[i]) % Mod1; + + hash_a2 = (hash_a2 * p + a[i]) % Mod2; + + if(i != 0) + + { + + pLg1 = pLg1 * p % Mod1; + + pLg2 = pLg2 * p % Mod2; + + } + + } + + + + int hash_b1 = 0, hash_b2 = 0; + + + + for(int i = 0; i < n; i++) + + { + + hash_b1 = (hash_b1 * p + b[i]) % Mod1; + + hash_b2 = (hash_b2 * p + b[i]) % Mod2; + + } + + + + if(hash_a1 == hash_b1 && hash_a2 == hash_b2) + + { + + contor ++; + + viz[0] = 1; + + } + + + + for(int i = n; i < m; i++) + + { + + + + hash_b1 = ((hash_b1 - (pLg1 * b[i - n]) % Mod1 + Mod1) * p + b[i]) % Mod1; + + hash_b2 = ((hash_b2 - (pLg2 * b[i - n]) % Mod2 + Mod2) * p + b[i]) % Mod2; + + + + if(hash_a1 == hash_b1 && hash_a2 == hash_b2) + + { + + contor++; + + viz[i - n + 1] = 1; + + } + + } + + + + fout << contor << "\n"; + + + + contor = 0; + + for(int i = 0; i < m && contor < 1000; i++) + + if(viz[i]) + + { + + fout << i << " "; + + contor++; + + } + + + + fout << "\n"; + + + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/rk/feb0087c-b086-4398-b8f8-fe5c2486ef2f.cpp b/strmatch/rk/feb0087c-b086-4398-b8f8-fe5c2486ef2f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..01811ed980327e014dfaf21e76c958ee2a0bb3c2 --- /dev/null +++ b/strmatch/rk/feb0087c-b086-4398-b8f8-fe5c2486ef2f.cpp @@ -0,0 +1,97 @@ +#include +#include +char a[2000001],b[2000001]; + +int v[2000001]; + +int main() + +{ + + FILE *fin=fopen("strmatch.in","r"); + + FILE *fout=fopen("strmatch.out","w"); + + int n,m,i,am1,am2,bm1,bm2,p1,p2,sol=0; + + + + fscanf(fin,"%s",&a); + + fscanf(fin,"%s",&b); + + n=strlen(a); + + m=strlen(b); + + if(n>m) + + sol=0; + + else + + { + + am1=am2=bm1=bm2=0; + + for(i=0; i +#include +using namespace std; + + + +ifstream fin ("strmatch.in"); + +ofstream fout ("strmatch.out"); + + + +int i, lg_a, lg_b, r[2000003]; + +int Hash_a1, Hash_a2, Hash1, Hash2, P1 = 1, P2 = 1; + +char a[2000003], b[2000003]; + + + +int main() + +{ + + fin.getline ( a, 2000005 ); + + fin.getline ( b, 2000005 ); + + + + lg_a = strlen ( a ); + + lg_b = strlen ( b ); + + if ( lg_a > lg_b ) + + { + + fout << 0; + + return 0; + + } + + + + for ( i = 0 ; i < lg_a ; i++ ) + + { + + Hash_a1 = ( Hash_a1 * 80 + ( a[i] - '0' + 1 ) ) % 45007; + + Hash_a2 = ( Hash_a2 * 80 + ( a[i] - '0' + 1 ) ) % 45013; + + + + Hash1 = ( Hash1 * 80 + ( b[i] - '0' + 1 ) ) % 45007; + + Hash2 = ( Hash2 * 80 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( i != 0 ) + + { + + P1 = ( P1 * 80 ) % 45007; + + P2 = ( P2 * 80 ) % 45013; + + } + + } + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = 0; + + + + for ( i = lg_a ; i < lg_b ; i++ ) + + { + + Hash1 = ( ( ( Hash1 - ( ( b[i-lg_a] - '0' + 1 ) * P1 ) % 45007 + 45007 ) * 80 ) % 45007 + ( b[i] - '0' + 1 ) ) % 45007; + + Hash2 = ( ( ( Hash2 - ( ( b[i-lg_a] - '0' + 1 ) * P2 ) % 45013 + 45013 ) * 80 ) % 45013 + ( b[i] - '0' + 1 ) ) % 45013; + + + + if ( Hash_a1 == Hash1 && Hash_a2 == Hash2 ) r[++r[0]] = i - lg_a + 1; + + } + + + + fout << r[0] << '\n'; + + r[0] = min ( 1000, r[0] ); + + for ( i = 1 ; i <= r[0] ; i++ ) fout << r[i] << ' '; + + + + return 0; + +} diff --git a/strmatch/z-algo/0026f943-e20f-4f1c-b36a-d1c8e1bf8879.cpp b/strmatch/z-algo/0026f943-e20f-4f1c-b36a-d1c8e1bf8879.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53355b04d6dbabf4ba7e49519b721cd82144d85 --- /dev/null +++ b/strmatch/z-algo/0026f943-e20f-4f1c-b36a-d1c8e1bf8879.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/0363b3f2-9c67-446e-96ab-3d9b9ae0eb69.cpp b/strmatch/z-algo/0363b3f2-9c67-446e-96ab-3d9b9ae0eb69.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8698b26bdaef364ffa5f1aaf54fa07b3ab0be209 --- /dev/null +++ b/strmatch/z-algo/0363b3f2-9c67-446e-96ab-3d9b9ae0eb69.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/050da38d-9976-4bb4-90bd-dac3dc928552.cpp b/strmatch/z-algo/050da38d-9976-4bb4-90bd-dac3dc928552.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83ac1c6f9fd8b5c2f9b614a8c6d551ba6b2e8c05 --- /dev/null +++ b/strmatch/z-algo/050da38d-9976-4bb4-90bd-dac3dc928552.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/06d15335-ba46-44cd-9d4c-0800a3396cff.cpp b/strmatch/z-algo/06d15335-ba46-44cd-9d4c-0800a3396cff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/06d15335-ba46-44cd-9d4c-0800a3396cff.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/085e8580-8154-41ce-87bb-af63697b768f.cpp b/strmatch/z-algo/085e8580-8154-41ce-87bb-af63697b768f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/085e8580-8154-41ce-87bb-af63697b768f.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/0892e7ab-89a4-4fc0-a491-4e8d71767b0c.cpp b/strmatch/z-algo/0892e7ab-89a4-4fc0-a491-4e8d71767b0c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/0892e7ab-89a4-4fc0-a491-4e8d71767b0c.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/0d400ae7-3891-45a8-bcf6-25031904fadd.cpp b/strmatch/z-algo/0d400ae7-3891-45a8-bcf6-25031904fadd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/0d400ae7-3891-45a8-bcf6-25031904fadd.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/0e62801b-43ac-4f85-8177-a3c279d741cc.cpp b/strmatch/z-algo/0e62801b-43ac-4f85-8177-a3c279d741cc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/0e62801b-43ac-4f85-8177-a3c279d741cc.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/0ed5c5e2-7d48-4c39-b6b7-6a7e701633b6.cpp b/strmatch/z-algo/0ed5c5e2-7d48-4c39-b6b7-6a7e701633b6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/0ed5c5e2-7d48-4c39-b6b7-6a7e701633b6.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/147f8e8b-c852-4062-8c1f-914d93270c72.cpp b/strmatch/z-algo/147f8e8b-c852-4062-8c1f-914d93270c72.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/147f8e8b-c852-4062-8c1f-914d93270c72.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/1744a2bd-f27c-4064-b4a5-c52181819264.cpp b/strmatch/z-algo/1744a2bd-f27c-4064-b4a5-c52181819264.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d5fe12390dbc06de4da517eab98572389098c3b --- /dev/null +++ b/strmatch/z-algo/1744a2bd-f27c-4064-b4a5-c52181819264.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/18872acf-b0b1-4ede-89a5-08dc876ab31b.cpp b/strmatch/z-algo/18872acf-b0b1-4ede-89a5-08dc876ab31b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278e75b2c3bab968959d9f8045fd8963bc76309b --- /dev/null +++ b/strmatch/z-algo/18872acf-b0b1-4ede-89a5-08dc876ab31b.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/20a066d0-1a46-4ca4-b8d3-70cecb6f6bc0.cpp b/strmatch/z-algo/20a066d0-1a46-4ca4-b8d3-70cecb6f6bc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/20a066d0-1a46-4ca4-b8d3-70cecb6f6bc0.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/223e64ec-354b-4360-befb-4a1e648a51e9.cpp b/strmatch/z-algo/223e64ec-354b-4360-befb-4a1e648a51e9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64100984b9624ec471499b3a421c0b0e0bf9eb66 --- /dev/null +++ b/strmatch/z-algo/223e64ec-354b-4360-befb-4a1e648a51e9.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/2525bd57-cd7e-4607-924b-9bcf7caad91f.cpp b/strmatch/z-algo/2525bd57-cd7e-4607-924b-9bcf7caad91f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83ac1c6f9fd8b5c2f9b614a8c6d551ba6b2e8c05 --- /dev/null +++ b/strmatch/z-algo/2525bd57-cd7e-4607-924b-9bcf7caad91f.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/261dd59c-cd79-4e31-a548-250ba626b790.cpp b/strmatch/z-algo/261dd59c-cd79-4e31-a548-250ba626b790.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd95988b26aa7f69438a80d95dcfab4b0028ba9d --- /dev/null +++ b/strmatch/z-algo/261dd59c-cd79-4e31-a548-250ba626b790.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/2964b446-0a26-4909-9191-9294296c1a3a.cpp b/strmatch/z-algo/2964b446-0a26-4909-9191-9294296c1a3a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53b174cd0de4016f195b38aea2565b07d2f156c --- /dev/null +++ b/strmatch/z-algo/2964b446-0a26-4909-9191-9294296c1a3a.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/2d8ee575-4fcc-415e-b0ad-ba4fc38cde5a.cpp b/strmatch/z-algo/2d8ee575-4fcc-415e-b0ad-ba4fc38cde5a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2920f59e1539a06a08665c1e63544d68fa42f84a --- /dev/null +++ b/strmatch/z-algo/2d8ee575-4fcc-415e-b0ad-ba4fc38cde5a.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/2df95225-70d0-4af4-83f9-e2b78c95e331.cpp b/strmatch/z-algo/2df95225-70d0-4af4-83f9-e2b78c95e331.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/2df95225-70d0-4af4-83f9-e2b78c95e331.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/31070d19-8cac-4a69-9bc0-56ba614173a3.cpp b/strmatch/z-algo/31070d19-8cac-4a69-9bc0-56ba614173a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53b174cd0de4016f195b38aea2565b07d2f156c --- /dev/null +++ b/strmatch/z-algo/31070d19-8cac-4a69-9bc0-56ba614173a3.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/32ede206-934b-4ac6-a7bb-52d4faabbc73.cpp b/strmatch/z-algo/32ede206-934b-4ac6-a7bb-52d4faabbc73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fdd2c1b377ec22b358ffcd0580a857c8adf46d --- /dev/null +++ b/strmatch/z-algo/32ede206-934b-4ac6-a7bb-52d4faabbc73.cpp @@ -0,0 +1,131 @@ +#include +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/3446bbc1-6ae1-41b0-9e4d-bf51b0d9f389.cpp b/strmatch/z-algo/3446bbc1-6ae1-41b0-9e4d-bf51b0d9f389.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/3446bbc1-6ae1-41b0-9e4d-bf51b0d9f389.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/3969566e-f32e-4ea5-b1d9-0756fd93dfdb.cpp b/strmatch/z-algo/3969566e-f32e-4ea5-b1d9-0756fd93dfdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fdd2c1b377ec22b358ffcd0580a857c8adf46d --- /dev/null +++ b/strmatch/z-algo/3969566e-f32e-4ea5-b1d9-0756fd93dfdb.cpp @@ -0,0 +1,131 @@ +#include +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/39d15647-4ad6-4163-afa1-5a2782bf9fc7.cpp b/strmatch/z-algo/39d15647-4ad6-4163-afa1-5a2782bf9fc7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83ac1c6f9fd8b5c2f9b614a8c6d551ba6b2e8c05 --- /dev/null +++ b/strmatch/z-algo/39d15647-4ad6-4163-afa1-5a2782bf9fc7.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/3c8ab4ba-d907-4ae8-ba43-2e12f93808bf.cpp b/strmatch/z-algo/3c8ab4ba-d907-4ae8-ba43-2e12f93808bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1183c3cca0bea9378d1cc72678e860396a19b29e --- /dev/null +++ b/strmatch/z-algo/3c8ab4ba-d907-4ae8-ba43-2e12f93808bf.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/3cc282b1-5a32-4346-b1e0-c6c74d9101f8.cpp b/strmatch/z-algo/3cc282b1-5a32-4346-b1e0-c6c74d9101f8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278e75b2c3bab968959d9f8045fd8963bc76309b --- /dev/null +++ b/strmatch/z-algo/3cc282b1-5a32-4346-b1e0-c6c74d9101f8.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/491d69dc-2d0c-4092-bb3c-0347d572f9eb.cpp b/strmatch/z-algo/491d69dc-2d0c-4092-bb3c-0347d572f9eb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53b174cd0de4016f195b38aea2565b07d2f156c --- /dev/null +++ b/strmatch/z-algo/491d69dc-2d0c-4092-bb3c-0347d572f9eb.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/4bcfd40a-9dba-4725-91f8-459b079056b5.cpp b/strmatch/z-algo/4bcfd40a-9dba-4725-91f8-459b079056b5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a5e55900d1aa618751a5545eba5cf516e6cc39 --- /dev/null +++ b/strmatch/z-algo/4bcfd40a-9dba-4725-91f8-459b079056b5.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/4c351c1b-6c30-41e4-b9d1-154649e49121.cpp b/strmatch/z-algo/4c351c1b-6c30-41e4-b9d1-154649e49121.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53b174cd0de4016f195b38aea2565b07d2f156c --- /dev/null +++ b/strmatch/z-algo/4c351c1b-6c30-41e4-b9d1-154649e49121.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/544732fc-9eeb-4762-878a-add138ec4a96.cpp b/strmatch/z-algo/544732fc-9eeb-4762-878a-add138ec4a96.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/544732fc-9eeb-4762-878a-add138ec4a96.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/56d06ce3-9a4c-4d55-a37b-f2e63a27a293.cpp b/strmatch/z-algo/56d06ce3-9a4c-4d55-a37b-f2e63a27a293.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53355b04d6dbabf4ba7e49519b721cd82144d85 --- /dev/null +++ b/strmatch/z-algo/56d06ce3-9a4c-4d55-a37b-f2e63a27a293.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/570a213c-14a1-4852-ae10-c5b5aa2585e8.cpp b/strmatch/z-algo/570a213c-14a1-4852-ae10-c5b5aa2585e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2920f59e1539a06a08665c1e63544d68fa42f84a --- /dev/null +++ b/strmatch/z-algo/570a213c-14a1-4852-ae10-c5b5aa2585e8.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/5769fc59-3a5b-4179-bc25-25c9f5dafb9e.cpp b/strmatch/z-algo/5769fc59-3a5b-4179-bc25-25c9f5dafb9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53355b04d6dbabf4ba7e49519b721cd82144d85 --- /dev/null +++ b/strmatch/z-algo/5769fc59-3a5b-4179-bc25-25c9f5dafb9e.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/5785673c-39a4-4729-b378-775b17ad36e8.cpp b/strmatch/z-algo/5785673c-39a4-4729-b378-775b17ad36e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/5785673c-39a4-4729-b378-775b17ad36e8.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/5b2c5247-bdb1-4756-baab-79df67a4993e.cpp b/strmatch/z-algo/5b2c5247-bdb1-4756-baab-79df67a4993e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/5b2c5247-bdb1-4756-baab-79df67a4993e.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/64e56210-2903-4382-8650-37036580e10d.cpp b/strmatch/z-algo/64e56210-2903-4382-8650-37036580e10d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64100984b9624ec471499b3a421c0b0e0bf9eb66 --- /dev/null +++ b/strmatch/z-algo/64e56210-2903-4382-8650-37036580e10d.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/6a6599c7-a731-41c8-8f1d-95a4ef8b0bc6.cpp b/strmatch/z-algo/6a6599c7-a731-41c8-8f1d-95a4ef8b0bc6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea687799c15524eac6dd9771863eee3751cd41db --- /dev/null +++ b/strmatch/z-algo/6a6599c7-a731-41c8-8f1d-95a4ef8b0bc6.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/6c0df68e-093a-4134-8ea0-78b49b76cf7f.cpp b/strmatch/z-algo/6c0df68e-093a-4134-8ea0-78b49b76cf7f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45c1789e76dc34bc52b18ada58007b8682dace7f --- /dev/null +++ b/strmatch/z-algo/6c0df68e-093a-4134-8ea0-78b49b76cf7f.cpp @@ -0,0 +1,48 @@ +#include + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/6c807157-b9de-412e-ab85-839971f5e60c.cpp b/strmatch/z-algo/6c807157-b9de-412e-ab85-839971f5e60c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d5fe12390dbc06de4da517eab98572389098c3b --- /dev/null +++ b/strmatch/z-algo/6c807157-b9de-412e-ab85-839971f5e60c.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/6f34756b-7174-4df2-82bf-1ffa79beedff.cpp b/strmatch/z-algo/6f34756b-7174-4df2-82bf-1ffa79beedff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/6f34756b-7174-4df2-82bf-1ffa79beedff.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/6f6fe24c-2fd8-4d67-b616-3ae7d1c440cb.cpp b/strmatch/z-algo/6f6fe24c-2fd8-4d67-b616-3ae7d1c440cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/6f6fe24c-2fd8-4d67-b616-3ae7d1c440cb.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/71029238-2c5f-4221-bee2-90147555d132.cpp b/strmatch/z-algo/71029238-2c5f-4221-bee2-90147555d132.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaa804d97a099a40e32f64ba8e4c751dc2fb136 --- /dev/null +++ b/strmatch/z-algo/71029238-2c5f-4221-bee2-90147555d132.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/7102d005-756a-4bfc-8063-26591d0ce761.cpp b/strmatch/z-algo/7102d005-756a-4bfc-8063-26591d0ce761.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45c1789e76dc34bc52b18ada58007b8682dace7f --- /dev/null +++ b/strmatch/z-algo/7102d005-756a-4bfc-8063-26591d0ce761.cpp @@ -0,0 +1,48 @@ +#include + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/7228adf1-40fc-441f-bfe2-74d456d88642.cpp b/strmatch/z-algo/7228adf1-40fc-441f-bfe2-74d456d88642.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/7228adf1-40fc-441f-bfe2-74d456d88642.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/72ff93b4-df2c-47fa-9c3a-ced3d12fb52b.cpp b/strmatch/z-algo/72ff93b4-df2c-47fa-9c3a-ced3d12fb52b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aca50ace5c43cd8de09108fca7259b5d453209c --- /dev/null +++ b/strmatch/z-algo/72ff93b4-df2c-47fa-9c3a-ced3d12fb52b.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/76a5b361-f986-4c22-9dab-6b07a597ca00.cpp b/strmatch/z-algo/76a5b361-f986-4c22-9dab-6b07a597ca00.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1183c3cca0bea9378d1cc72678e860396a19b29e --- /dev/null +++ b/strmatch/z-algo/76a5b361-f986-4c22-9dab-6b07a597ca00.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/76eb6875-5f4c-4359-9155-9cc8ab50806f.cpp b/strmatch/z-algo/76eb6875-5f4c-4359-9155-9cc8ab50806f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/76eb6875-5f4c-4359-9155-9cc8ab50806f.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/793d745a-3a32-4e31-af93-69b21f722156.cpp b/strmatch/z-algo/793d745a-3a32-4e31-af93-69b21f722156.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/793d745a-3a32-4e31-af93-69b21f722156.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/7a676bd6-d4be-46d9-a6a2-0e41979a556a.cpp b/strmatch/z-algo/7a676bd6-d4be-46d9-a6a2-0e41979a556a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278e75b2c3bab968959d9f8045fd8963bc76309b --- /dev/null +++ b/strmatch/z-algo/7a676bd6-d4be-46d9-a6a2-0e41979a556a.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/7ba050a2-bd0d-402f-b6ad-34c48ca782fb.cpp b/strmatch/z-algo/7ba050a2-bd0d-402f-b6ad-34c48ca782fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64100984b9624ec471499b3a421c0b0e0bf9eb66 --- /dev/null +++ b/strmatch/z-algo/7ba050a2-bd0d-402f-b6ad-34c48ca782fb.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/7c8f9ef0-2f34-4d20-9260-0d3ac2b22bd1.cpp b/strmatch/z-algo/7c8f9ef0-2f34-4d20-9260-0d3ac2b22bd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/7c8f9ef0-2f34-4d20-9260-0d3ac2b22bd1.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/81f2d6f0-66d5-4d72-bcfe-4c1c98c46066.cpp b/strmatch/z-algo/81f2d6f0-66d5-4d72-bcfe-4c1c98c46066.cpp new file mode 100644 index 0000000000000000000000000000000000000000..83ac1c6f9fd8b5c2f9b614a8c6d551ba6b2e8c05 --- /dev/null +++ b/strmatch/z-algo/81f2d6f0-66d5-4d72-bcfe-4c1c98c46066.cpp @@ -0,0 +1,71 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/8562a89f-9ed0-4b35-b856-89181f0e1174.cpp b/strmatch/z-algo/8562a89f-9ed0-4b35-b856-89181f0e1174.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/8562a89f-9ed0-4b35-b856-89181f0e1174.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/875cdb3a-58f8-4559-9c8f-ddb8be19945d.cpp b/strmatch/z-algo/875cdb3a-58f8-4559-9c8f-ddb8be19945d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8698b26bdaef364ffa5f1aaf54fa07b3ab0be209 --- /dev/null +++ b/strmatch/z-algo/875cdb3a-58f8-4559-9c8f-ddb8be19945d.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/87a12392-74f9-48ed-a29b-20a03714f4ec.cpp b/strmatch/z-algo/87a12392-74f9-48ed-a29b-20a03714f4ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278e75b2c3bab968959d9f8045fd8963bc76309b --- /dev/null +++ b/strmatch/z-algo/87a12392-74f9-48ed-a29b-20a03714f4ec.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/8aacf192-d502-4210-a075-03bb7088ca62.cpp b/strmatch/z-algo/8aacf192-d502-4210-a075-03bb7088ca62.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/8aacf192-d502-4210-a075-03bb7088ca62.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/8c13bb94-d46b-4bf7-a113-c4ca82deb6fe.cpp b/strmatch/z-algo/8c13bb94-d46b-4bf7-a113-c4ca82deb6fe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45c1789e76dc34bc52b18ada58007b8682dace7f --- /dev/null +++ b/strmatch/z-algo/8c13bb94-d46b-4bf7-a113-c4ca82deb6fe.cpp @@ -0,0 +1,48 @@ +#include + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/8db2b3e8-9627-4b89-a4b4-401fbbe755fc.cpp b/strmatch/z-algo/8db2b3e8-9627-4b89-a4b4-401fbbe755fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17574244340f4c6aeef874723a139cc0247831d0 --- /dev/null +++ b/strmatch/z-algo/8db2b3e8-9627-4b89-a4b4-401fbbe755fc.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/91564514-f9d8-4204-bcac-5b92002d997e.cpp b/strmatch/z-algo/91564514-f9d8-4204-bcac-5b92002d997e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed3395e2c91f5e00ff7772c35812a1edd4a4d273 --- /dev/null +++ b/strmatch/z-algo/91564514-f9d8-4204-bcac-5b92002d997e.cpp @@ -0,0 +1,114 @@ +#include + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/9207a830-7c7b-42d2-8481-060bd0e69dbd.cpp b/strmatch/z-algo/9207a830-7c7b-42d2-8481-060bd0e69dbd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaa804d97a099a40e32f64ba8e4c751dc2fb136 --- /dev/null +++ b/strmatch/z-algo/9207a830-7c7b-42d2-8481-060bd0e69dbd.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/9248d52e-8ec3-4bd6-9ef9-709cf1fff8e1.cpp b/strmatch/z-algo/9248d52e-8ec3-4bd6-9ef9-709cf1fff8e1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fdd2c1b377ec22b358ffcd0580a857c8adf46d --- /dev/null +++ b/strmatch/z-algo/9248d52e-8ec3-4bd6-9ef9-709cf1fff8e1.cpp @@ -0,0 +1,131 @@ +#include +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/941dff74-e8da-440a-b0d8-76ef5b6c7dbe.cpp b/strmatch/z-algo/941dff74-e8da-440a-b0d8-76ef5b6c7dbe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/941dff74-e8da-440a-b0d8-76ef5b6c7dbe.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/9db03d1e-1a01-459d-9473-894e4326b6f7.cpp b/strmatch/z-algo/9db03d1e-1a01-459d-9473-894e4326b6f7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/9db03d1e-1a01-459d-9473-894e4326b6f7.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/9e04a6b2-57b6-49fd-804f-b1eb1b1b91a3.cpp b/strmatch/z-algo/9e04a6b2-57b6-49fd-804f-b1eb1b1b91a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64100984b9624ec471499b3a421c0b0e0bf9eb66 --- /dev/null +++ b/strmatch/z-algo/9e04a6b2-57b6-49fd-804f-b1eb1b1b91a3.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/a10ce8ed-9ead-4c47-bc4d-ee23e13f3d40.cpp b/strmatch/z-algo/a10ce8ed-9ead-4c47-bc4d-ee23e13f3d40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd95988b26aa7f69438a80d95dcfab4b0028ba9d --- /dev/null +++ b/strmatch/z-algo/a10ce8ed-9ead-4c47-bc4d-ee23e13f3d40.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/a2f82018-0ff3-4e15-ae7f-fb25eccd2366.cpp b/strmatch/z-algo/a2f82018-0ff3-4e15-ae7f-fb25eccd2366.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1183c3cca0bea9378d1cc72678e860396a19b29e --- /dev/null +++ b/strmatch/z-algo/a2f82018-0ff3-4e15-ae7f-fb25eccd2366.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/a331b4ff-5239-4ed6-a22f-3cd1765bd318.cpp b/strmatch/z-algo/a331b4ff-5239-4ed6-a22f-3cd1765bd318.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53355b04d6dbabf4ba7e49519b721cd82144d85 --- /dev/null +++ b/strmatch/z-algo/a331b4ff-5239-4ed6-a22f-3cd1765bd318.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/a3f5fb11-bc6f-473b-afbd-8698ce0a2fa6.cpp b/strmatch/z-algo/a3f5fb11-bc6f-473b-afbd-8698ce0a2fa6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..278e75b2c3bab968959d9f8045fd8963bc76309b --- /dev/null +++ b/strmatch/z-algo/a3f5fb11-bc6f-473b-afbd-8698ce0a2fa6.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/a7253715-7e49-4b35-b91e-09e1f30f66a0.cpp b/strmatch/z-algo/a7253715-7e49-4b35-b91e-09e1f30f66a0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaa804d97a099a40e32f64ba8e4c751dc2fb136 --- /dev/null +++ b/strmatch/z-algo/a7253715-7e49-4b35-b91e-09e1f30f66a0.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/a811fcbc-f7b6-4e28-b869-36c000bed914.cpp b/strmatch/z-algo/a811fcbc-f7b6-4e28-b869-36c000bed914.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fdd2c1b377ec22b358ffcd0580a857c8adf46d --- /dev/null +++ b/strmatch/z-algo/a811fcbc-f7b6-4e28-b869-36c000bed914.cpp @@ -0,0 +1,131 @@ +#include +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/a899938a-876d-402c-9832-7472dd1ddb64.cpp b/strmatch/z-algo/a899938a-876d-402c-9832-7472dd1ddb64.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53355b04d6dbabf4ba7e49519b721cd82144d85 --- /dev/null +++ b/strmatch/z-algo/a899938a-876d-402c-9832-7472dd1ddb64.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/a99a11de-cb16-4ad4-a13d-45564aafacc0.cpp b/strmatch/z-algo/a99a11de-cb16-4ad4-a13d-45564aafacc0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1183c3cca0bea9378d1cc72678e860396a19b29e --- /dev/null +++ b/strmatch/z-algo/a99a11de-cb16-4ad4-a13d-45564aafacc0.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/aa7380fe-296a-4cab-98a8-2b94cde0ea40.cpp b/strmatch/z-algo/aa7380fe-296a-4cab-98a8-2b94cde0ea40.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45c1789e76dc34bc52b18ada58007b8682dace7f --- /dev/null +++ b/strmatch/z-algo/aa7380fe-296a-4cab-98a8-2b94cde0ea40.cpp @@ -0,0 +1,48 @@ +#include + + + + + +using namespace std; + +vector GetZ(string s) { + int n = s.length(); + vector z(n); + int l = 0, r = 0; + for (int i = 1; i < n; ++i) { + z[i] = min(r - i + 1, z[i - l]); + if (z[i] < 0) + z[i] = 0; + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) + ++z[i]; + if (i + z[i] - 1 > r) { + l = i; + r = i + z[i] - 1; + } + } + return z; +} + +int main() { + ifstream cin("strmatch.in"); + ofstream cout("strmatch.out"); + + string patt, text; + getline(cin, patt); + getline(cin, text); + string s = patt + "#" + text; + auto z = GetZ(s); + vector ans; + int res = 0; + for (int i = patt.size() + 1; i < (int)s.size(); ++i) { + if (z[i] == (int)patt.size()) { + if ((int)ans.size() < 1000) + ans.emplace_back(i - patt.size() - 1); + ++res; + } + } + cout << res << '\n'; + for (int x : ans) cout << x << ' '; + cout << endl; +} diff --git a/strmatch/z-algo/ab6f534a-5e81-4e91-ac8c-240cc6857974.cpp b/strmatch/z-algo/ab6f534a-5e81-4e91-ac8c-240cc6857974.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea687799c15524eac6dd9771863eee3751cd41db --- /dev/null +++ b/strmatch/z-algo/ab6f534a-5e81-4e91-ac8c-240cc6857974.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/abc0c008-9480-4e6e-9ac3-8c9757e8a593.cpp b/strmatch/z-algo/abc0c008-9480-4e6e-9ac3-8c9757e8a593.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/abc0c008-9480-4e6e-9ac3-8c9757e8a593.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/b1c94c85-30d9-4656-a7b9-fbebbed810f0.cpp b/strmatch/z-algo/b1c94c85-30d9-4656-a7b9-fbebbed810f0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bdaa804d97a099a40e32f64ba8e4c751dc2fb136 --- /dev/null +++ b/strmatch/z-algo/b1c94c85-30d9-4656-a7b9-fbebbed810f0.cpp @@ -0,0 +1,62 @@ +#include +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/b29d532e-7dc4-4cd1-9e6d-ff1c67cbcf88.cpp b/strmatch/z-algo/b29d532e-7dc4-4cd1-9e6d-ff1c67cbcf88.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7aca50ace5c43cd8de09108fca7259b5d453209c --- /dev/null +++ b/strmatch/z-algo/b29d532e-7dc4-4cd1-9e6d-ff1c67cbcf88.cpp @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/b29e2858-b887-4e13-bf02-b1788f802b8d.cpp b/strmatch/z-algo/b29e2858-b887-4e13-bf02-b1788f802b8d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d53b174cd0de4016f195b38aea2565b07d2f156c --- /dev/null +++ b/strmatch/z-algo/b29e2858-b887-4e13-bf02-b1788f802b8d.cpp @@ -0,0 +1,109 @@ +#include +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/b5c15cae-d1b8-471a-b451-cbabfc65fec5.cpp b/strmatch/z-algo/b5c15cae-d1b8-471a-b451-cbabfc65fec5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4fc45faabaf0af46d59f1541d5cdf75e89b964f8 --- /dev/null +++ b/strmatch/z-algo/b5c15cae-d1b8-471a-b451-cbabfc65fec5.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/bd73715c-bf2a-429d-81fe-a4698e570003.cpp b/strmatch/z-algo/bd73715c-bf2a-429d-81fe-a4698e570003.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a5e55900d1aa618751a5545eba5cf516e6cc39 --- /dev/null +++ b/strmatch/z-algo/bd73715c-bf2a-429d-81fe-a4698e570003.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< + + +using namespace std; + + + +int z[4000005]; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +int main() + +{ + + string s1,s2; + + cin>>s1>>s2; + + int n=s1.size(),m=s2.size(); + + s1+='#'; + + s1+=s2; + + + + int st=0,dr=0; + + for(int i=1;idr){ + + dr=i; + + st=i; + + while(s1[dr]==s1[dr-st] and dr +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/be0e910b-3c77-4310-92ce-f8d1c4bc54a4.cpp b/strmatch/z-algo/be0e910b-3c77-4310-92ce-f8d1c4bc54a4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea687799c15524eac6dd9771863eee3751cd41db --- /dev/null +++ b/strmatch/z-algo/be0e910b-3c77-4310-92ce-f8d1c4bc54a4.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/bf77cca2-2335-4121-8e0b-b360f005ebad.cpp b/strmatch/z-algo/bf77cca2-2335-4121-8e0b-b360f005ebad.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ea687799c15524eac6dd9771863eee3751cd41db --- /dev/null +++ b/strmatch/z-algo/bf77cca2-2335-4121-8e0b-b360f005ebad.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/bfb6981b-6c16-494c-a6f5-c8a3bd2a10cb.cpp b/strmatch/z-algo/bfb6981b-6c16-494c-a6f5-c8a3bd2a10cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a282c6a7baaea4002fd816b13337522258402d37 --- /dev/null +++ b/strmatch/z-algo/bfb6981b-6c16-494c-a6f5-c8a3bd2a10cb.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/c1aad038-3b36-4833-bf51-92f3e79c0226.cpp b/strmatch/z-algo/c1aad038-3b36-4833-bf51-92f3e79c0226.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17574244340f4c6aeef874723a139cc0247831d0 --- /dev/null +++ b/strmatch/z-algo/c1aad038-3b36-4833-bf51-92f3e79c0226.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/c39c4c5d-00c9-4e64-b6e3-4e953fbdc1d0.cpp b/strmatch/z-algo/c39c4c5d-00c9-4e64-b6e3-4e953fbdc1d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17574244340f4c6aeef874723a139cc0247831d0 --- /dev/null +++ b/strmatch/z-algo/c39c4c5d-00c9-4e64-b6e3-4e953fbdc1d0.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include + +using namespace std; +ifstream fin("strmatch.in"); +ofstream fout("strmatch.out"); +int z[2 * 2000002], N; +void makeZarr(string str) { + N = str.length(); + long l, r; + l = r = 0; + for (long i = 1; i < N; ++i) { + if (i > r) { + long j = 0; + r = l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + else { + long k = i - l; + if (z[k] < r - i) { + z[i] = z[k]; + } + else { + long j = r - i; + l = i; + while (r < N && str[j] == str[r]) { + ++j; + ++r; + } + r--; + z[i] = r - l + 1; + } + } + } +} +int main() +{ + string word, s; + fin >> word >> s; + long len = word.length(), con = 0, nr = 0; + makeZarr(word + "?" + s); + for (long i = len; i < N; ++i) { + if (z[i] == len) { + ++con; + } + } + fout << con << "\n"; + for (long i = len; i < N; ++i) { + if (z[i] == len) { + fout << i - len - 1 << " "; + ++nr; + } + if (nr >= 1000) + break; + } + return 0; +} diff --git a/strmatch/z-algo/c69a1b14-3dff-445d-9016-98b534c8cd9d.cpp b/strmatch/z-algo/c69a1b14-3dff-445d-9016-98b534c8cd9d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/c69a1b14-3dff-445d-9016-98b534c8cd9d.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/c880b1e7-98ba-4a81-a8d3-02a7257e315c.cpp b/strmatch/z-algo/c880b1e7-98ba-4a81-a8d3-02a7257e315c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/c880b1e7-98ba-4a81-a8d3-02a7257e315c.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +using namespace std; + +string a,c; + +unsigned int z[3000001]; + +int rez[2000001],cont,l,lef,righ; + +void Z() + +{ + + int n=c.size(); + + for(int i=1; irigh) + + { + + while(z[i]+i>a; + + c+=a; + + Z(); + + cout< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector z(string s) { + +int n = s.size(); + +vector z(n); + +int x = 0, y = 0; + +for (int i = 1; i < n; i++) { + +z[i] = max(0,min(z[i-x],y-i+1)); + +while (i+z[i] < n && s[z[i]] == s[i+z[i]]) { + +x = i; y = i+z[i]; z[i]++; + +} + +} + +return z; + +} + +int main() + +{ + + string a,b; + + cin>>a>>b; + + b=a+'#'+b; + + vector v=z(b); + + int rasp=0; + + for(int i=0;i +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/d3194180-6849-47fc-8f43-76ef9197cfa3.cpp b/strmatch/z-algo/d3194180-6849-47fc-8f43-76ef9197cfa3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17574244340f4c6aeef874723a139cc0247831d0 --- /dev/null +++ b/strmatch/z-algo/d3194180-6849-47fc-8f43-76ef9197cfa3.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/d6b41b14-147f-428c-a5fa-058702206f5b.cpp b/strmatch/z-algo/d6b41b14-147f-428c-a5fa-058702206f5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9e6f92c5d280f52b4f60f9837b7134615210f63 --- /dev/null +++ b/strmatch/z-algo/d6b41b14-147f-428c-a5fa-058702206f5b.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int NMAX = 4e6 + 5; + + + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + + + +int z[NMAX]; + + + +int main() + +{ + + string a, b; + + cin >> a >> b; + + string s = a; + + s += "#"; + + s += b; + + int r = 0, pozr = 0; + + for(int i = 1; i < s.size(); ++i) { + + if(i > r) { + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } else { + + int rest = min(z[i - pozr], r - i); + + z[i] = rest; + + while(i + z[i] < s.size() && s[i + z[i]] == s[z[i]]) { + + z[i]++; + + } + + } + + if(i + z[i] > r) { + + r = i + z[i]; + + pozr = i; + + } + + } + + int cnt = 0; + + vector sol; + + for(int i = 1; i < s.size(); ++i) { + + if(z[i] == a.size()) { + + cnt++; + + if(cnt <= 1000) { + + sol.push_back(i - a.size() - 1); + + } + + } + + } + + cout << cnt << "\n"; + + for(auto x: sol) { + + cout << x << " "; + + } + + cout << "\n"; + + return 0; + +} diff --git a/strmatch/z-algo/d8430c2c-3e57-4121-bc12-187f230d2acb.cpp b/strmatch/z-algo/d8430c2c-3e57-4121-bc12-187f230d2acb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2920f59e1539a06a08665c1e63544d68fa42f84a --- /dev/null +++ b/strmatch/z-algo/d8430c2c-3e57-4121-bc12-187f230d2acb.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/d8df9ce9-fb1c-4b68-9e94-5653890f1df6.cpp b/strmatch/z-algo/d8df9ce9-fb1c-4b68-9e94-5653890f1df6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2920f59e1539a06a08665c1e63544d68fa42f84a --- /dev/null +++ b/strmatch/z-algo/d8df9ce9-fb1c-4b68-9e94-5653890f1df6.cpp @@ -0,0 +1,138 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("strmatch.in"); + +ofstream fout("strmatch.out"); + + + +const int NMax = 4e6; + + + +char S[NMax + 5], B[NMax + 5]; + +int L, R, Z[NMax + 5], N, M, Nr; + +vector Sol; + + + +int Advance(int j, int i) + +{ + + while(i < N && S[j] == S[i]) + + i++, j++; + + + + return j; + +} + + + +int main() + +{ + + fin >> S >> B; + + + + M = strlen(S); + + strcat(S, B); + + N = strlen(S); + + + + for(int i = 1; i < N; i++) + + { + + if(i > R) + + { + + Z[i] = Advance(0, i); + + L = i, R = i + Z[i] - 1; + + } + + else + + { + + int Ls = 0, Rs = R - L, is = i - L; + + + + + + if(is + Z[is] - 1 < Rs) + + Z[i] = Z[is]; + + else + + { + + Z[i] = Advance(R - i + 1, R + 1); + + L = i, R = i + Z[i] - 1; + + } + + } + + } + + + + for(int i = M; i < N; i++) + + { + + if(Z[i] < M) continue; + + Nr++; + + + + if(Sol.size() < 1000) + + Sol.push_back(i - M); + + } + + fout << Nr << '\n'; + + + + for(auto x : Sol) + + fout << x << " "; + + + + fin.close(); + + fout.close(); + + + + return 0; + +} diff --git a/strmatch/z-algo/d9266809-21cc-4b0d-b5b3-6f0814c531ca.cpp b/strmatch/z-algo/d9266809-21cc-4b0d-b5b3-6f0814c531ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a5e55900d1aa618751a5545eba5cf516e6cc39 --- /dev/null +++ b/strmatch/z-algo/d9266809-21cc-4b0d-b5b3-6f0814c531ca.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/d9b71f29-d550-40d0-a0a0-0f62e19f562b.cpp b/strmatch/z-algo/d9b71f29-d550-40d0-a0a0-0f62e19f562b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1183c3cca0bea9378d1cc72678e860396a19b29e --- /dev/null +++ b/strmatch/z-algo/d9b71f29-d550-40d0-a0a0-0f62e19f562b.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/daa9505f-4075-4bed-a475-7c0dc6f2185b.cpp b/strmatch/z-algo/daa9505f-4075-4bed-a475-7c0dc6f2185b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/daa9505f-4075-4bed-a475-7c0dc6f2185b.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/strmatch/z-algo/dbbd0283-c4ec-42ea-aeb7-e4598606181f.cpp b/strmatch/z-algo/dbbd0283-c4ec-42ea-aeb7-e4598606181f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b4db3d1a88d08990351b1631ee56ff4e3e189722 --- /dev/null +++ b/strmatch/z-algo/dbbd0283-c4ec-42ea-aeb7-e4598606181f.cpp @@ -0,0 +1,81 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector z_algo(string s) + +{ + + int n=s.size(); + + vector z(n); + + int x=0,y=0; + + for(int i=1;i sol; + +int main() + +{ + + string a,b,s; + + cin>>a>>b; + + s=a+'#'+b; + + vector v=z_algo(s); + + int cnt=0; + + for(int i=a.size()+1;i + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/def53a0d-8e0a-4db2-be23-4ff1ed7b4f83.cpp b/strmatch/z-algo/def53a0d-8e0a-4db2-be23-4ff1ed7b4f83.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b2212d07041c0e454ad43d7f07b69590f68bcdb --- /dev/null +++ b/strmatch/z-algo/def53a0d-8e0a-4db2-be23-4ff1ed7b4f83.cpp @@ -0,0 +1,105 @@ +#include +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/e0811f76-775b-4fd7-8095-6fc4f07bccd5.cpp b/strmatch/z-algo/e0811f76-775b-4fd7-8095-6fc4f07bccd5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d5fe12390dbc06de4da517eab98572389098c3b --- /dev/null +++ b/strmatch/z-algo/e0811f76-775b-4fd7-8095-6fc4f07bccd5.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + + + + +using namespace std; + +using ll = long long; + + + + + +ifstream cin("strmatch.in"); ofstream cout("strmatch.out"); + + + + + + + +string a, b; + +vector ans; + + + + + + + +void z(string s) { + + int n = (int)s.length(); + + vector z(n); + + for (int i = 1, l = 0, r = 0; i < n; ++i) { + + if (i <= r) z[i] = max(min(z[i - l], r - i + 1), 0); + + while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; + + if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; + + } + + + + for (int i = 1; i < n; i++) { + + if (z[i] == a.size()) ans.push_back(i - a.size() - 1); + + } + +} + + + + + + + +int main() { + + + + + + cin >> a >> b; + + + + string s = a + '#' + b; + + + + z(s); + + + + cout << ans.size() << '\n'; + + for (int i = 0; i < min((int)ans.size(), 1000); i++) { + + cout << ans[i] << ' '; + + } + + return 0; + +} diff --git a/strmatch/z-algo/e1b6aa71-bd14-45c3-b006-6aad5a484fc1.cpp b/strmatch/z-algo/e1b6aa71-bd14-45c3-b006-6aad5a484fc1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd95988b26aa7f69438a80d95dcfab4b0028ba9d --- /dev/null +++ b/strmatch/z-algo/e1b6aa71-bd14-45c3-b006-6aad5a484fc1.cpp @@ -0,0 +1,92 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +#include +#include +#include +using namespace std; + +const int NMAX = 5000005; + +char s[NMAX]; + +int z[NMAX]; + +vector sol; + + + +int main() { + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + scanf("%s", s + 1); + + int n = (int)strlen(s + 1); + + s[n + 1] = '#'; + + scanf("%s", s + n + 2); + + int m = (int)strlen(s + 1); + + int st = 1, dr = 1; + + z[1] = 1; + + for(int k = 2; k <= m; k++) { + + if(s[k] != s[1]) { + + z[k] = 0; + + } + + else { + + int poz = k; + + if(k <= dr) { + + poz = min(dr, k + z[k - st + 1] - 1); + + } + + while(poz <= m && s[poz] == s[poz - k + 1]) { + + poz++; + + } + + poz--; + + if(dr < poz) { + + dr = poz; + + st = k; + + } + + z[k] = poz - k + 1; + + } + + } + + for(int k = n + 1; k <= m; k++) { + + if(z[k] == n) { + + sol.push_back(k - n); + + } + + } + + printf("%d\n", sol.size()); + + for(int i = 0; i < sol.size() && i < 1000; i++) { + + printf("%d ", sol[i] - 2); + + } + + return 0; + +} diff --git a/strmatch/z-algo/e4578419-2d64-43af-a860-3bc929dfd2a3.cpp b/strmatch/z-algo/e4578419-2d64-43af-a860-3bc929dfd2a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d5fe12390dbc06de4da517eab98572389098c3b --- /dev/null +++ b/strmatch/z-algo/e4578419-2d64-43af-a860-3bc929dfd2a3.cpp @@ -0,0 +1,110 @@ +#include + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +vector sol; + +vector zzz(string s) + +{ + + int l,r,n=s.size(); + + vector z(n); + + l=r=0; + + for(int i=1;ir) + + { + + l=r=i; + + while(r>pat>>txt; + + txt=pat+'#'+txt; + + vector poz=zzz(txt); + + for(int i=pat.size()+1;i +#include +#include +#include +using namespace std; +string a,c; +unsigned int z[3000001]; +int rez[2000001],cont,l,lef,righ; +void Z() +{ + int n=c.size(); + for(int i=1; irigh) + { + while(z[i]+i>a; + c+=a; + Z(); + cout< + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string a,b; + +int z[4000005]; + +void z_algorithm(string s) + +{ + + for(int i=1,l=0,r=0;ir) + + { + + l=i; + + r=i+z[i]-1; + + } + + } + + vector rez; + + for(int i=a.size()+1;i +using namespace std; + + + +const int MAXN = (int) 2e6 + 5; + + + +char str[2 * MAXN + 1]; + +int z[2 * MAXN + 1]; + + + +int main() { + ifstream cin("strmatch.in"); + + ofstream cout("strmatch.out"); + + int i; + + ios::sync_with_stdio(false); + + cin.tie(0), cout.tie(0); + + + + cin >> (str + 1); + + int n = strlen(str + 1); + + str[n + 1] = '@'; + + cin >> (str + n + 2); + + int m = strlen(str + n + 2); + + + + int sz = n + m + 1, pos = 0; + + z[1] = sz; + + for(i = 2; i <= sz; i++) { + + if(pos + z[pos] >= i) { + + z[i] = min(pos + z[pos] - i, z[i - pos + 1]); + + } + + while(i + z[i] <= sz && str[i + z[i]] == str[z[i] + 1]) { + + z[i]++; + + } + + if(i + z[i] > pos + z[pos]) { + + pos = i; + + } + + } + + vector sol; + + int ans = 0; + + for(i = n + 2; i <= sz; i++) { + + if(z[i] == n) { + + ans++; + + if(sol.size() < 1000) { + + sol.push_back(i - n - 2); + + } + + } + + } + + cout << ans << "\n"; + + for(auto it : sol) { + + cout << it << " "; + + } + + + + return 0; + +} diff --git a/strmatch/z-algo/efaab13b-aa1c-40e7-b40a-1682b6c1dc85.cpp b/strmatch/z-algo/efaab13b-aa1c-40e7-b40a-1682b6c1dc85.cpp new file mode 100644 index 0000000000000000000000000000000000000000..64100984b9624ec471499b3a421c0b0e0bf9eb66 --- /dev/null +++ b/strmatch/z-algo/efaab13b-aa1c-40e7-b40a-1682b6c1dc85.cpp @@ -0,0 +1,63 @@ +#include + + + +using namespace std; + +ifstream in("strmatch.in"); +ofstream out("strmatch.out"); + +int z[4000010]; + + +int main() +{ + string a,b; + in>>a>>b; + int n=a.size(); + string rez=a+"#"+b; + int lung=rez.length(); + lung++; + int st=0,dr=0; + for(int i=1;idr) + { + st=i; + dr=i; + while(rez[dr]==rez[dr-st] && dr<=lung) + dr++; + z[i]=dr-st; + dr--; + } + else + { + if(z[i-st]+i-1 + + +using namespace std; + +ifstream cin("strmatch.in"); + +ofstream cout("strmatch.out"); + +string s1,s2; + +int z[4000008]; + +int main() + +{ + + cin>>s1>>s2; + + int n=s1.size(); + + int m=s2.size(); + + s1+='$'; + + s1+=s2; + + int st=0,dr=0; + + for(int i=1;idr){ + + st=i; + + dr=i; + + while(s1[dr]==s1[dr-st] and dr<=n+m+1){ + + dr++; + + } + + z[i]=dr-st; + + dr--; + + } + + else{ + + if(z[i-st]+i-11000) + + continue; + + cout< +#include +#include +#include +#include + + + + +unsigned int z[4000002]; + + + +std::string a, b; + + + +void compute_z_func(std::string& a) + +{ + + unsigned int r = 0, l = 0; + + for (unsigned int i = 1; i < a.length(); ++i) + + { + + if (i <= r) + + { + + z[i] = std::min(r - i + 1, z[i - l]); + + } + + while (i + z[i] < a.length() && a[z[i]] == a[i + z[i]]) + + ++z[i]; + + if (i + z[i] - 1 > r) + + { + + l = i; + + r = i + z[i] - 1; + + } + + } + +} + + + + + + + +int main(){ + + + + freopen("strmatch.in", "r", stdin); + + freopen("strmatch.out", "w", stdout); + + + + std::cin >> a >> b; + + int n = a.length(); + + int m = b.length(); + + a += "#" + b; + + compute_z_func(a); + + + + int res = 0; + + for (int i = n + 1; i < a.length(); ++i) + + if (z[i] == n) + + ++res; + + + + std::cout << res << "\n"; + + + + int cnt = 0; + + + + for (int i = n + 1; cnt < 1000 && i < a.length() + 1; ++i) + + if (z[i] == n) + + { + + std::cout << i - n - 1 << " "; + + ++cnt; + + } + + + + + + return 0; + +} diff --git a/strmatch/z-algo/f5c6657a-3d70-4a62-b56b-bb926fd5a455.cpp b/strmatch/z-algo/f5c6657a-3d70-4a62-b56b-bb926fd5a455.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a5e55900d1aa618751a5545eba5cf516e6cc39 --- /dev/null +++ b/strmatch/z-algo/f5c6657a-3d70-4a62-b56b-bb926fd5a455.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/f7375680-3d4e-4a67-8680-152e925d627f.cpp b/strmatch/z-algo/f7375680-3d4e-4a67-8680-152e925d627f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8698b26bdaef364ffa5f1aaf54fa07b3ab0be209 --- /dev/null +++ b/strmatch/z-algo/f7375680-3d4e-4a67-8680-152e925d627f.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +int main() { + + + + ifstream in("strmatch.in"); + + ofstream out("strmatch.out"); + + + + string a, b; + + in >> a >> b; + + a = " " + a; + + b = a + b; + + int m = b.size() - 1, n = a.size() - 1; + + + + vector z(m + 1, 0); + + int r = 0, l = 0; + + int cnt = 0; + + vector ans; + + for(int i = 2; i <= m; i ++) { + + if(i <= r) + + z[i] = min(r - i + 1, z[i - l + 1]); + + while(z[i] + i <= m && b[z[i] + 1] == b[i + z[i]]) + + z[i] ++; + + + + if(z[i] + i - 1 > r) { + + r = z[i] + i - 1; + + l = i; + + } + + + + if(i > n && z[i] >= n) { + + cnt ++; + + if(ans.size() < 1000) + + ans.push_back(i - 1 - n); + + } + + } + + out << cnt << "\n"; + + for(auto it : ans) + + out << it << " "; + + + + return 0; + +} diff --git a/strmatch/z-algo/f8d55690-f476-4110-aa24-07709a85d282.cpp b/strmatch/z-algo/f8d55690-f476-4110-aa24-07709a85d282.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2fdd2c1b377ec22b358ffcd0580a857c8adf46d --- /dev/null +++ b/strmatch/z-algo/f8d55690-f476-4110-aa24-07709a85d282.cpp @@ -0,0 +1,131 @@ +#include +#include + + +const int MAXN = 4e6 + 4; + + + +using namespace std; + + + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + + + +string s; + +string a,b; + +int z[MAXN]; + + + +void Z(){ + + int n = s.size(); + + int l = 0,r = 0; + + int cate_sunt = 0; + + z[0] = s.size(); + + for(int i = 1; i < n; i++){ + + if(i > r){ + + int cnt = 0; + + while(i + cnt < n && s[i + cnt] == s[cnt]) + + cnt++; + + z[i] = cnt; + + l = i; + + r = i + cnt - 1; + + }else{ + + if(z[i - l] + i < r){ + + z[i] = z[i - l]; + + }else{ + + z[i] = min(z[i - l],r - i + 1); + + int capat = i + z[i],cnt = 0; + + int inceput = z[i]; + + + + while(capat + cnt < n && s[capat + cnt] == s[cnt + inceput]) + + cnt++; + + z[i] += cnt; + + l = i; + + r = capat + cnt - 1; + + } + + } + + if(z[i] == a.size()){ + + cate_sunt++; + + } + + } + + out<>a>>b; + + + + + + + + s = a + '#' + b; + + Z(); + + return 0; + +} diff --git a/strmatch/z-algo/f8f41bcf-6a87-4332-8daa-45f721ab00c7.cpp b/strmatch/z-algo/f8f41bcf-6a87-4332-8daa-45f721ab00c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99a5e55900d1aa618751a5545eba5cf516e6cc39 --- /dev/null +++ b/strmatch/z-algo/f8f41bcf-6a87-4332-8daa-45f721ab00c7.cpp @@ -0,0 +1,84 @@ +#include + + +using namespace std; + +ifstream f("strmatch.in"); + +ofstream g("strmatch.out"); + +string s,s1; + +int n,i,k,numar,valoare,lim,l,r,z[4000005]; + +vector sol; + +int main() + +{ + + f>>s; + + n=s.size(); + + f>>s1; + + s=s+'#'+s1; + + l=r=0; + + for (i=1;ir) + + { + + r=i+z[i]-1; + + l=i; + + } + + if (z[i]==n) + + { + + numar++; + + sol.push_back(i-n-1); + + } + + } + + g< +using namespace std; + +ifstream in("strmatch.in"); + +ofstream out("strmatch.out"); + +int main() + +{ + + string a,b; + + in>>a>>b; + + int n,k; + + k=a.size(); + + a+='#'+b; + + n=a.size(); + + vector z(n),sol; + + int x=0,y=0,ans=0; + + for(int i=1;ik and z[i]==k) + + { + + ++ans; + + if(ans<=1000) + + sol.push_back(i-k-1); + + } + + } + + out< + + +using namespace std; + + + +const int NMAX = 4000005; + +string s, t; + +int z[NMAX]; + + + +int main() + +{ + + ifstream fin("strmatch.in"); + + ofstream fout("strmatch.out"); + + fin >> s >> t; + + int sz = s.length(); + + s = s + "#" + t; + + int left = 0, right = 0; + + for (int i = 1;i < s.length();++i) + + { + + if (i > right) + + { + + left = right = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + else + + { + + if (i + z[i - left] - 1 < right) + + z[i] = z[i - left]; + + else + + { + + left = i; + + while (right < s.length() && s[right] == s[right - left]) + + ++right; + + z[i] = right - left; + + --right; + + } + + } + + } + + int cnt = 0; + + vector answer; + + for (int i = sz;i < s.length();++i) + + if (z[i] == sz) + + { + + ++cnt; + + if (cnt <= 1000) + + answer.push_back(i - sz - 1); + + } + + fout << cnt << "\n"; + + for (auto& i : answer) + + fout << i << " "; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/aib/003e4d49-0b67-11ec-b186-2cf05d84bfc2.cpp b/swap/aib/003e4d49-0b67-11ec-b186-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8960821f6827273bd1040e985ee8427c6b4fb8f9 --- /dev/null +++ b/swap/aib/003e4d49-0b67-11ec-b186-2cf05d84bfc2.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 5 * 1e4; + +const int LIM = 1e5; + +const int AMAX = 30; + + + +string a, b; + +long long res; + +int aint[1 + 4 * NMAX]; + + + +queue < int > q[AMAX]; + + + +void add(int x) { + + for(int i = x; i <= LIM; i += (i & (-i))) + + aint[i]++; + +} + + + +int query(int x) { + + int res = 0; + + for(int i = x; i != 0; i -= (i & (-i))) + + res += aint[i]; + + return res; + +} + + + +int main() + +{ + + in >> a >> b; + + + + for(int i = 0; i < a.size(); i++) + + q[a[i] - 'a'].push(i + 1); + + for(int i = 0; i < b.size(); i++) { + + if(q[b[i] - 'a'].empty()) { + + out << "-1\n"; + + exit(EXIT_SUCCESS); + + } else { + + int from = q[b[i] - 'a'].front(); + + q[b[i] - 'a'].pop(); + + + + res += from - i - 1 + query(b.size()) - query(from); + + add(from); + + } + + } + + + + out << res << '\n'; + + return 0; + +} diff --git a/swap/aib/0056bae2-e972-43f6-8e28-b97d7752d4ec.cpp b/swap/aib/0056bae2-e972-43f6-8e28-b97d7752d4ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d946c6c2f49f217b69929701aa702a4058bbaba7 --- /dev/null +++ b/swap/aib/0056bae2-e972-43f6-8e28-b97d7752d4ec.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() +{ + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n=0; + while(a[n]>='a' && a[n]<='z') ++n; + + fgets(b, 50001, stdin); + + for(i=0;i +#include +#include +using namespace std; + +const int nmax=5e4+3; + +int n,v[nmax],p[nmax],sol,q[30][nmax],poz[30]; + +char a[nmax],b[nmax]; + +inline int usu(int x) + +{ + + return x&(-x); + +} + +void update(int x) + +{ + + while(x<=n) + + { + + ++v[x]; + + x+=usu(x); + + } + +} + +int query(int x) + +{ + + int sol=0; + + while(x) + + { + + sol+=v[x]; + + x-=usu(x); + + } + + return sol; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",(a+1)); + + scanf("%s",(b+1)); + + n=strlen(a+1); + + for(int i=1;i<=n;++i) q[a[i]-'a'][++q[a[i]-'a'][0]]=i; + + for(int i=0;i<=26;++i) poz[i]=1; + + for(int i=1;i<=n;++i) + + { + + if(q[b[i]-'a'][0]+1==poz[b[i]-'a']) + + { + + printf("-1"); + + return 0; + + } + + p[i]=q[b[i]-'a'][poz[b[i]-'a']]; + + ++poz[b[i]-'a']; + + } + + for(int i=1;i<=n;++i) + + { + + sol=sol+i-query(p[i])-1; + + update(p[i]); + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/007fc9c0-0b67-11ec-b6e9-2cf05d84bfc2.cpp b/swap/aib/007fc9c0-0b67-11ec-b6e9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ca33949992d1810698f2a16d410de36abb8c532 --- /dev/null +++ b/swap/aib/007fc9c0-0b67-11ec-b6e9-2cf05d84bfc2.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char c1[50002],c2[50002]; + +bool wtaken[50002],ok[50002],good=1; + +dequev[30]; + +int AIB[50002],L,sol; + +int fr1[30],fr2[30]; + +void intro() + +{ + + f>>c1+1; + + f>>c2+1; + + L=strlen(c1+1); + + for(int i=1;i<=L;++i){ + + fr1[c1[i]-'a']++,fr2[c2[i]-'a']++; + + v[c1[i]-'a'].push_back(i); + + } + + for(int i=0;i<=25;++i) + + if(fr1[i]!=fr2[i]) + + good=0; + +} + +void Add(int nod) + +{ + + for(;nod<=L;nod+=(nod&(-nod))) + + AIB[nod]++; + +} + +int compute(int nod) + +{ + + int s=0; + + for(;nod;nod-=(nod&(-nod))) + + s+=AIB[nod]; + + return s; + +} + +void finito() + +{ + + for(int i=1;i<=L;++i) + + { + + sol+=v[c2[i]-'a'][0]+compute(L-v[c2[i]-'a'][0]+1)-i; + + + + Add(L-v[c2[i]-'a'][0]+1); + + v[c2[i]-'a'].pop_front(); + + } + + g< +#include +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int i,n,sol,k; + +int v[50005],aib[50005]; + +char c[50005],d[50005]; + +dequeH[27]; + +void update(int p){ + + for(;p<=n;p+=(p&-p)){ + + aib[p]++; + + } + +} + +int querry(int p){ + + int r=0; + + for(;p;p-=(p&-p)){ + + r+=aib[p]; + + } + + return r; + +} + +int main(){ + + fin>>c+1>>d+1; + + n=strlen(c+1); + + for(i=1;c[i]!=0;i++){ + + H[c[i]-'a'+1].push_back(i); + + } + + for(i=1;i<=n;i++){ + + if(H[d[i]-'a'+1].empty()){ + + fout<<"-1"; + + return 0; + + } + + k=H[d[i]-'a'+1].front(); + + H[d[i]-'a'+1].pop_front(); + + sol+=k-querry(k)-1; + + update(k); + + } + + fout< +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + + + +int n,m,ok,i,x,v[50001],aib[50001],f[50001]; + +int sol; + +char a[50001],b[50001]; + +queue L[27]; + + + +void update(int p){ + + for(int i=p;i<=n;i+=(i&-i)) + + aib[i]++; + +} + +int query(int p){ + + int sol=0; + + for(int i=p;i>=1;i-=(i&-i)) + + sol += aib[i]; + + return sol; + +} + + + +int main (){ + + + + fin>>a+1>>b+1; + n = strlen (b+1); + + for (i=1;i<=n;i++) + + L[b[i]-'a'+1].push (i); + + + + for (i=1;i<=n;i++){ + + if (L[a[i]-'a'+1].size () == 0){ + + + + fout<<-1; + + return 0; + + } + + x = L[a[i]-'a'+1].front(); + + sol += x - 1- query (x); + + update (x); + + L[a[i]-'a'+1].pop(); + + } + + + + + + fout< +#include +#include +using namespace std; + +const int nmax=5e4+3; + +int n,v[nmax],p[nmax],sol,q[30][nmax],poz[30]; + +char a[nmax],b[nmax]; + +inline int usu(int x) + +{ + + return x&(-x); + +} + +void update(int x) + +{ + + while(x<=n) + + { + + ++v[x]; + + x+=usu(x); + + } + +} + +int query(int x) + +{ + + int sol=0; + + while(x) + + { + + sol+=v[x]; + + x-=usu(x); + + } + + return sol; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",(a+1)); + + scanf("%s",(b+1)); + + n=strlen(a+1); + + for(int i=1;i<=n;++i) q[a[i]-'a'][++q[a[i]-'a'][0]]=i; + + for(int i=0;i<=26;++i) poz[i]=1; + + for(int i=1;i<=n;++i) + + { + + if(q[b[i]-'a'][0]+1==poz[b[i]-'a']) + + { + + printf("-1"); + + return 0; + + } + + p[i]=q[b[i]-'a'][poz[b[i]-'a']]; + + ++poz[b[i]-'a']; + + } + + for(int i=1;i<=n;++i) + + { + + sol=sol+i-query(p[i])-1; + + update(p[i]); + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp b/swap/aib/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/aib/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/aib/011d806c-0b67-11ec-9bdc-2cf05d84bfc2.cpp b/swap/aib/011d806c-0b67-11ec-9bdc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a79a313302e38066f84bc08b942721322ba896e --- /dev/null +++ b/swap/aib/011d806c-0b67-11ec-9bdc-2cf05d84bfc2.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + + + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int N, i, j, aib[50005]; +int ind[135], poz[50005]; +long long ANS; +string s1, s2, cop1, cop2; +vector V[135]; + +int zero(int nr) +{ + return nr & (-nr); +} + +void Update(int nr) +{ + while (nr<=N) + { + aib[nr]++; + nr+=zero(nr); + } +} + +int Query(int nr) +{ + int ans=0; + while (nr>0) + { + ans+=aib[nr]; + nr-=zero(nr); + } + return ans; +} + +int main() +{ + fin >> s1 >> s2; + cop1=s1; + cop2=s2; + sort(cop1.begin(), cop1.end()); + sort(cop2.begin(), cop2.end()); + if (cop1!=cop2) + { + fout << -1; + fin.close(); + fout.close(); + return 0; + } + N=s1.size(); + s1='*'+s1; + s2='*'+s2; + for (i=1; i<=N; i++) + V[s2[i]].push_back(i); + for (i=1; i<=N; i++) + { + poz[V[s1[i]][ind[s1[i]]]]=i; + ind[s1[i]]++; + } + for (i=1; i<=N; i++) + { + ANS+=poz[i]-Query(poz[i])-1; + Update(poz[i]); + } + fout << ANS << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/swap/aib/013c377b-0b67-11ec-af12-2cf05d84bfc2.cpp b/swap/aib/013c377b-0b67-11ec-af12-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93753c054b0f07ac119f41d070821c119748ac3e --- /dev/null +++ b/swap/aib/013c377b-0b67-11ec-af12-2cf05d84bfc2.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include + + + +const int MAX_LENGTH = 50000; +const int SIGMA = 26; + +int n; + +class FenwickTree { +public: + int a[1 + MAX_LENGTH]; + + FenwickTree() { + memset(a, 0, sizeof(a)); + } + + void update(int poz, int val) { + for (int i = poz; i <= n; i += i & -i) { + a[i] += val; + } + } + + int query(int poz) { + int ans = 0; + for (int i = poz; i >= 1; i -= i & -i) { + ans += a[i]; + } + return ans; + } + + int query(int st, int dr) { + return query(dr) - query(st - 1); + } +}; + +int f1[SIGMA], f2[SIGMA]; + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + char s1[MAX_LENGTH + 1], s2[MAX_LENGTH + 1]; + scanf("%s%s", s1, s2); + if (strlen(s1) != strlen(s2)) { + printf("-1"); + return 0; + } + n = (int)strlen(s1); + std::queue poz[SIGMA]; + for (int i = 0; i < n; i++) { + f1[s1[i] - 'a']++; + f2[s2[i] - 'a']++; + poz[s1[i] - 'a'].push(i + 1); + } + for (char alph = 'a'; alph <= 'z'; alph++) { + if (f1[alph - 'a'] != f2[alph - 'a']) { + printf("-1"); + return 0; + } + } + FenwickTree t; + int ans = 0; + for (int i = 0; i < n; i++) { + int index = poz[s2[i] - 'a'].front(); + poz[s2[i] - 'a'].pop(); + ans += index - i - 1 + t.query(index + 1, n); + t.update(index, 1); + } + printf("%d\n", ans); + return 0; +} diff --git a/swap/aib/015b0600-0b67-11ec-a440-2cf05d84bfc2.cpp b/swap/aib/015b0600-0b67-11ec-a440-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d9ce4616fcd1981ab50ce9594fbaea4e69164d --- /dev/null +++ b/swap/aib/015b0600-0b67-11ec-a440-2cf05d84bfc2.cpp @@ -0,0 +1,92 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int i,sol,n,A[50001]; + +char s[50001],t[50001]; + +queue L[30]; + + + +void update(int p) + +{ + + for (int i=p; i<=n; i+=(i&-i)) + + A[i]++; + +} + + + +int query(int p) + +{ + + int sol = 0; + + for (int i=p; i>=1; i-=(i&-i)) + + sol += A[i]; + + return sol; + +} + + + +int main() + +{ + + fin >> s+1 >> t+1; + + n = strlen(t+1); + + for (i=1; t[i]!=0; i++) + + L[t[i]-'a'+1].push(i); + + for (i=1; s[i]!=0; i++) + + { + + if (L[s[i]-'a'+1].size() == 0) + + { + + fout << -1; + + return 0; + + } + + int x = L[s[i]-'a'+1].front(); + + sol += x-1-query(x); + + update(x); + + L[s[i]-'a'+1].pop(); + + } + + fout << sol; + + return 0; + +} diff --git a/swap/aib/01794d97-ca58-4f22-a31e-513860c26620.cpp b/swap/aib/01794d97-ca58-4f22-a31e-513860c26620.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8960821f6827273bd1040e985ee8427c6b4fb8f9 --- /dev/null +++ b/swap/aib/01794d97-ca58-4f22-a31e-513860c26620.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include + + + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 5 * 1e4; + +const int LIM = 1e5; + +const int AMAX = 30; + + + +string a, b; + +long long res; + +int aint[1 + 4 * NMAX]; + + + +queue < int > q[AMAX]; + + + +void add(int x) { + + for(int i = x; i <= LIM; i += (i & (-i))) + + aint[i]++; + +} + + + +int query(int x) { + + int res = 0; + + for(int i = x; i != 0; i -= (i & (-i))) + + res += aint[i]; + + return res; + +} + + + +int main() + +{ + + in >> a >> b; + + + + for(int i = 0; i < a.size(); i++) + + q[a[i] - 'a'].push(i + 1); + + for(int i = 0; i < b.size(); i++) { + + if(q[b[i] - 'a'].empty()) { + + out << "-1\n"; + + exit(EXIT_SUCCESS); + + } else { + + int from = q[b[i] - 'a'].front(); + + q[b[i] - 'a'].pop(); + + + + res += from - i - 1 + query(b.size()) - query(from); + + add(from); + + } + + } + + + + out << res << '\n'; + + return 0; + +} diff --git a/swap/aib/01a12deb-c519-42dc-93a8-0557bb7690f4.cpp b/swap/aib/01a12deb-c519-42dc-93a8-0557bb7690f4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93c44087f6ac1e1475ab3d86ac3ab1b7781be60e --- /dev/null +++ b/swap/aib/01a12deb-c519-42dc-93a8-0557bb7690f4.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +vectorpoz[30]; + +int f[30],f2[30]; + +int v[50005]; + +char a[50005]; + +int aib[50005]; + +int update(int poz,int nr){ + +int i; + +for(i=poz;i<=50000;i=i+(i&(-i))) + +aib[i]+=nr;} + +int query(int poz){ + +int i,s=0; + +for(i=poz;i>0;i=i-(i&(-i))) + +s=s+aib[i]; + +return s;} + +int main(){ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +int n,rez=0,i; + +char ch; + +fgets(a+1,50003,stdin); + +n=strlen(a+1); + +n--; + +for(i=1;i<=n;i++) + +f[a[i]-96]++; + +for(i=1;i<=n;i++) + +scanf("%c",&ch),f2[ch-96]++,poz[ch-96].push_back(i); + +for(i=1;i<=26;i++) + +if (f[i]!=f2[i]){ + +printf("-1\n"); + +return 0;} + +for(i=n;i>=1;i--) + +v[i]=poz[a[i]-96].back(),poz[a[i]-96].pop_back(); + +for(i=1;i<=n;i++) + +update(i,1); + +for(i=1;i<=n;i++){ + +rez=rez+query(v[i]-1); + +update(v[i],-1);} + +printf("%d\n",rez); + +return 0;} diff --git a/swap/aib/01a59b45-0b67-11ec-84fe-2cf05d84bfc2.cpp b/swap/aib/01a59b45-0b67-11ec-84fe-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b792cfc55cd2741f09de70d163eeb0f0f56de44 --- /dev/null +++ b/swap/aib/01a59b45-0b67-11ec-84fe-2cf05d84bfc2.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +deque A[28]; + +deque B[28]; + +int n,i,sol,p,aib[50001]; + +char s1[50001], s2[50001]; + +int query (int p){ + + int r=0; + + for(;p;p-=p&-p){ + + r+=aib[p]; + + } + + return r; + +} + + + +void update (int p){ + + for(;p<=n;p+=p&-p) + + aib[p]++; + +} + + + + + +int main(){ + + fin>>s1+1>>s2+1; + + for(i=1;s1[i]!=0;i++) { + + A[s1[i]-'a'+1].push_back(i); + + B[s2[i]-'a'+1].push_back(i); + + ++n; + + } + + for(i=1;i<=26;i++) + + if(A[i].size()!=B[i].size()){ + + fout<<-1; + + return 0; + + } + + for(i=1;i<=n;i++){ + + p=A[s2[i] - 'a'+1].front(); + + A[s2[i] - 'a'+1].pop_front(); + + sol+=p-query(p)-1; + + update(p); + + } + + fout< +#include +#include +using namespace std; + +ifstream in("swap.in"); + +ofstream out("swap.out"); + +char sir1[50001], sir2[50001]; + +unsigned short hz[50001],hz2[50001],aib[50001]; + +unsigned int hashuri[27][50001]; + +int n; + +void update_aib (int pozitie) { + + for (int i = pozitie; i <= n; i += (i&(-i))) { + + aib[i] ++; + + } + + return; + +} + +int query_aib(int pozitie) { + + int rezultat = 0; + + for (int i = n; i >= 1; i -=(i&(-i))) { + + rezultat += aib[i]; + + } + + for (int i = pozitie; i >= 1; i -= (i&(-i))) { + + rezultat -= aib[i]; + + } + + return rezultat; + +} + +int rezultat; + +int main(void) { + + in >> sir1 + 1; + + in >> sir2 + 1; + + n = strlen(sir1+1); + + for (int i = 1; i <= n; i ++) { + + hz[sir1[i]-'a'+1] ++; + + hz2[sir2[i]-'a'+1]++; + + hashuri[sir1[i]-'a'+1][hz[sir1[i]-'a'+1]] = i; + + } + + for (int i = 1; i <= 26; i ++) { + + if (hz[i] != hz2[i]) { + + out<<-1; + + return 0; + + } + + hz2[i] = 0; + + } + + for (int i = 1; i <= n; i ++) { + + hz2[sir2[i]-'a'+1] ++; + + int x = hashuri[sir2[i]-'a'+1][hz2[sir2[i]-'a'+1]]; + + update_aib (x); + + rezultat += query_aib(x); + + } + + out << rezultat; + + return 0; + +} diff --git a/swap/aib/01e4771c-0b67-11ec-a8e4-2cf05d84bfc2.cpp b/swap/aib/01e4771c-0b67-11ec-a8e4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d6d012b783c2678e86cf6976145207679bdce4f --- /dev/null +++ b/swap/aib/01e4771c-0b67-11ec-a8e4-2cf05d84bfc2.cpp @@ -0,0 +1,88 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +deque < int > H[30]; + + + +int v[50001], n, s, A[50001]; + + + +char s1[50001], s2[50001]; + + + +void update (int p, int x) { + + for (; p <= n; p += (p & -p)) + + A[p] += x; + +} + + + +int querry (int p) { + + int r = 0; + + for (; p >= 1; p -= (p & -p)) + + r += A[p]; + + return r; + +} + + + +int main () { + + fin >> s1 + 1 >> s2 + 1; + + for (int i = 1; s1[i] != 0; ++i, ++n) { + + H[s1[i] - 'a' + 1].push_back (i); + + } + + for (int i = 1; i <= n; ++i) { + + if (H[s2[i] - 'a' + 1].empty ()) { + + fout << "-1"; + + return 0; + + } + + int nod = H[s2[i] - 'a' + 1].front (); + + H[s2[i] - 'a' + 1].pop_front (); + + s += nod - querry (nod) - 1; + + update (nod, 1); + + } + + fout << s; + + return 0; + +} diff --git a/swap/aib/0205896c-0b67-11ec-ab2c-2cf05d84bfc2.cpp b/swap/aib/0205896c-0b67-11ec-ab2c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94181d4d56b451c97f988a1b3d9c436722598515 --- /dev/null +++ b/swap/aib/0205896c-0b67-11ec-ab2c-2cf05d84bfc2.cpp @@ -0,0 +1,101 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int n, v[50002], viz1[30], viz2[30], n2; + + + +deque viz[30]; + + + +long long s; + + + +char sir1[50002], sir2[50002]; + + + +int aib[50002]; + + + +int query(int nod){ + + int s = 0; + + for( ; nod >= 1; nod -= (nod & -nod)) + + s += v[nod]; + + return s; + +} + + + +void update(int nod){ + + for( ; nod <= n; nod += (nod & -nod)) + + ++ v[nod]; + +} + + + +int main(){ + + + + f>>sir1 + 1>>sir2 + 1; + for(int i = 1; sir1[i] != 0; ++ i, ++ n) + + viz[sir1[i] - 'a' + 1].push_back(i); + + + + for(int i = 1; i <= n; ++ i){ + + if(viz[sir2[i] - 'a' + 1].empty()){ + + g<<-1; + + return 0; + + } + + int nod = viz[sir2[i] - 'a' + 1].front(); + + viz[sir2[i] - 'a' + 1].pop_front(); + + s += nod - query(nod) - 1; + + update(nod); + + } + + + + g< + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int N; + +vector < int > BIT; + + + +void update(int index) { + + for(int i = index; i <= N; i += i & -i) + + ++BIT[i]; + +} + + + +int query(int index) { + + int ans = 0; + + for(int i = index; i > 0; i -= i & -i) + + ans += BIT[i]; + + return ans; + +} + + + +int main() { + + string s, t; + + fin >> s >> t; + + vector < int > fs(26), ft(26); + + for(char x : s) + + ++fs[x - 'a']; + + for(char x : t) + + ++ft[x - 'a']; + + bool ok = true; + + for(int i = 0; i < 26 && ok; ++i) + + if(fs[i] != ft[i]) + + ok = false; + + if(!ok) { + + fout << -1; + + return 0; + + } + + vector < int > from_s[26], from_t[26]; + + N = s.size(); + + s = '0' + s, t = '0' + t; + + for(int i = 1; i <= N; ++i) + + from_s[s[i] - 'a'].emplace_back(i); + + for(int i = 1; i <= N; ++i) + + from_t[t[i] - 'a'].emplace_back(i); + + vector < int > perm(N + 1); + + for(int i = 0; i < 26; ++i) + + for(int j = 0; j < (int)from_s[i].size(); ++j) { + + int poz = from_s[i][j]; + + perm[poz] = from_t[i][j]; + + } + + int ans = 0; + + BIT.resize(N + 1); + + for(int i = N; i > 0; --i) { + + ans += query(perm[i]); + + update(perm[i]); + + } + + fout << ans; + +} diff --git a/swap/aib/0253758d-0b67-11ec-b3fe-2cf05d84bfc2.cpp b/swap/aib/0253758d-0b67-11ec-b3fe-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e680c928aed67a2e1f29294101ae3b5f84c16130 --- /dev/null +++ b/swap/aib/0253758d-0b67-11ec-b3fe-2cf05d84bfc2.cpp @@ -0,0 +1,71 @@ +# include +# include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +queue d[27]; + +char a[50010],b[50010]; + +int v[50010],f[27],n,i; + +long long s; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(i&(-i))) + + v[i]++; + +} + +int query(int poz){ + + int s=0; + + for(int i=poz;i>=1;i-=(i&(-i))) + + s+=v[i]; + + return s; + +} + +int main () { + + fin>>a+1>>b+1; + + for(i=1;b[i]!=0;i++,n++) + + d[b[i]-'a'+1].push(i); + + for(i=1;i<=n;i++){ + + if(d[a[i]-'a'+1].size()==0){ + + fout<<"-1\n"; + + return 0; + + } + + int el=d[a[i]-'a'+1].front(); + + s+=el-1-query(el); + + update(el); + + d[a[i]-'a'+1].pop(); + + } + + fout< +# include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +queue d[27]; + +char a[50010],b[50010]; + +int v[50010],f[27],n,i; + +long long s; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(i&(-i))) + + v[i]++; + +} + +int query(int poz){ + + int s=0; + + for(int i=poz;i>=1;i-=(i&(-i))) + + s+=v[i]; + + return s; + +} + +int main () { + + fin>>a+1>>b+1; + + for(i=1;b[i]!=0;i++,n++) + + d[b[i]-'a'+1].push(i); + + for(i=1;i<=n;i++){ + + if(d[a[i]-'a'+1].size()==0){ + + fout<<"-1\n"; + + return 0; + + } + + int el=d[a[i]-'a'+1].front(); + + s+=el-1-query(el); + + update(el); + + d[a[i]-'a'+1].pop(); + + } + + fout< +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +deque aux[300]; + +int i, n, v[50003], ok, z[50003]; + +char c1[50003], c2[50003]; + +long long sol; + + + +void update(int p, int val) + +{ + + for(;p<=n;p+=p&(-p)) + + z[p]+=val; + +} + + + +int quarry(int p) + +{ + + int r=0; + + for(;p>=1;p-=p&(-p)) + + r+=z[p]; + + return r; + +} + + + +int verif(int a) + +{ + + if(a<0) + + a=-a; + + return a; + +} + + + +int main() + +{ + + fin>>c1; + + fin>>c2; + + for(i=0;c2[i]!=0;i++) + + aux[c2[i]].push_back(i+1); + + for(i=0;c1[i]!=0;i++) + + { + + n++; + + if(aux[c1[i]].size()>0) + + { + + v[i+1]=aux[c1[i]][0]; + + aux[c1[i]].pop_front(); + + } + + else + + { + + ok=1; + + } + + } + + if(ok==1) + + { + + fout<<-1; + + return 0; + + } + + for(i=1;i<=n;i++) + + { + + sol+=v[i]-1-quarry(v[i]); + + update(v[i], 1); + + } + + fout< +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 -= p2 & (-p2); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/02f99d3c-0b67-11ec-a5f3-2cf05d84bfc2.cpp b/swap/aib/02f99d3c-0b67-11ec-a5f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be6c96a90daf4ca6f67cf784583b82e73ae1d44c --- /dev/null +++ b/swap/aib/02f99d3c-0b67-11ec-a5f3-2cf05d84bfc2.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n, i, s, aib[50003], vec[50003]; + +deque a[30]; + +char s1[50003], s2[50003]; + +int query(int p) + +{ + + int r=0; + + for(;p;p-=(p & -p)) + + r+=aib[p]; + + return r; + +} + +void update(int p) + +{ + + for(;p<=n;p+=(p & -p)) + + aib[p]++; + +} + +int main() + +{ + + f>>s1>>s2; + + for(i=0;s1[i]!=0;i++) + + a[s1[i]-'a'].push_back(i+1); + + for(i=0;s2[i]!=0;i++) + + { + + if(a[s2[i]-'a'].size()==0) + + { + + g<<-1; + + return 0; + + } + + n++; + + vec[i+1]=a[s2[i]-'a'][0]; + + a[s2[i]-'a'].pop_front(); + + } + + for(i=n;i>0;i--) + + { + + s=s+query(vec[i]); + + update(vec[i]); + + } + + g< +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + + + aib[i] = 0; + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 -= p2 & (-p2); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/03378594-0b67-11ec-a77d-2cf05d84bfc2.cpp b/swap/aib/03378594-0b67-11ec-a77d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efbeee7311eb9520ca3960f6094e5b3532669db9 --- /dev/null +++ b/swap/aib/03378594-0b67-11ec-a77d-2cf05d84bfc2.cpp @@ -0,0 +1,296 @@ +#include +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 = p2 & (p2 - 1); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/037dfefb-0b67-11ec-abf1-2cf05d84bfc2.cpp b/swap/aib/037dfefb-0b67-11ec-abf1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d104f1307e001ccd21544683243e37cc6ada6903 --- /dev/null +++ b/swap/aib/037dfefb-0b67-11ec-abf1-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string s1, s2; long long ans; int arb[100100]; + +queue q[30]; + + + +void add(int i){ + + for(; i <= 100000; i += (i & (-i))) arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) ans += arb[i]; + + return ans; + +} + + + +int main(){ + + getline(in, s1); + + getline(in, s2); + + for(int i = 0; i < s1.size(); i++) q[s1[i] - 'a'].push(i+1); + + for(int i = 0; i < s2.size(); i++){ + + if(q[s2[i] - 'a'].empty()) return out << -1, 0; + + int pos = q[s2[i] - 'a'].front(); + + q[s2[i] - 'a'].pop(); + + ans += pos - i - 1 + get(s2.size()) - get(pos); + + add(pos); + + } + + out << ans; + + return 0; + +} diff --git a/swap/aib/03d257d2-0b67-11ec-a20a-2cf05d84bfc2.cpp b/swap/aib/03d257d2-0b67-11ec-a20a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c8ab11095c2e789d53a39e81f8c91a83bcc67e6 --- /dev/null +++ b/swap/aib/03d257d2-0b67-11ec-a20a-2cf05d84bfc2.cpp @@ -0,0 +1,188 @@ +#include +using namespace std; + + + +const int SPQR = 50005, + +SIGM = 26; + + + +int n; + + + +vector aps[SIGM]; + +char sr1[SPQR], + +sr2[SPQR]; + +int aib[SPQR], + +gnm[SIGM]; + + + +inline int lsb(int arg) { + + return arg&-arg; + +} + + + +inline int cod(int arg) { + + return arg - 'a'; + +} + + + +inline void update(int pos, int val) { + + while(pos < SPQR) { + + aib[pos]+= val; + + pos+= lsb(pos); + + } + +} + + + +inline int query(int pos) { + + int ant = 0; + + + + while(pos > 0) { + + ant+= aib[pos]; + + pos-= lsb(pos); + + } + + + + return ant; + +} + + + +void ave(void) { + + int lst[SIGM]; + + + + memset(lst, 0xFF, sizeof(lst)); + + for(int i=1; i<=n; ++i) + + aps[cod(sr2[i])].push_back(i); + +} + + + +void caesar(int &ant) { + + int f1[SIGM], + + f2[SIGM]; + + int pos; + + + + ant = 0; + + memset(f1, 0x00, sizeof(f1)); + + memset(f2, 0x00, sizeof(f2)); + + + + for(int i=1; i<=n; ++i) { + + ++ f1[ cod(sr1[i]) ]; + + ++ f2[ cod(sr2[i]) ]; + + } + + + + for(int i=0; i>(sr1 + 1)>>(sr2 + 1); + + n = strlen(sr1 + 1); + + + + ave(); + + caesar(ant); + + + + fo< +#include +#include +#include + +using namespace std; + +ifstream f("swap.in"); +ofstream g("swap.out"); +const int LETTERS = 26; + +int N; +vector < int > frA(LETTERS, 0), frB(LETTERS, 0); +vector < int > perm, bit; +deque < int > fromA[LETTERS], fromB[LETTERS]; + +inline int lsb(int nr) { + return (nr & -nr); +} + +int query(int index) { + int ret{}; + + for(;index;index -= lsb(index)) + ret += bit[index]; + + return ret; +} + +void update(int index) { + for(;index <= N;index += lsb(index)) + bit[index]++; +} + +int main() { + + string a, b; + f >> a >> b; + + for (char &c : a) + frA[c - 'a']++; + + for (char &c : b) + frB[c - 'a']++; + + for (int i = 0; i < LETTERS; ++i) { + if (frA[i] != frB[i]) { + g << "-1"; + return 0; + } + } + + a = " " + a; + b = " " + b; + + N = a.size() - 1; + + perm.resize(N + 1, 0); + + for(int i = 1;i <= N;++i) + fromA[ a[i] - 'a' ].push_back(i); + + for(int i = 1;i <= N;++i) + fromB[ b[i] - 'a' ].push_back(i); + + for(int i = 1;i <= N;++i) { + int pos = fromA[a[i] - 'a'].front(); + fromA[ a[i] - 'a' ].pop_front(); + + perm[pos] = fromB[ a[i] - 'a' ].front(); + fromB[ a[i] - 'a' ].pop_front(); + } + + int ans{}; + bit.resize(N + 1, 0); + + for(int i = N;i;--i) { + ans += query(perm[i] - 1); + update(perm[i]); + } + + g << ans; + + return 0; +} diff --git a/swap/aib/03f2b76f-0b67-11ec-9a88-2cf05d84bfc2.cpp b/swap/aib/03f2b76f-0b67-11ec-9a88-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..022c07e3551a51f7aaf52bfbeddb4f4596d6a302 --- /dev/null +++ b/swap/aib/03f2b76f-0b67-11ec-9a88-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string s1, s2; long long ans; int arb[100100]; + +queue q[30]; + + + +void add(int i){ + + for(; i <= 100000; i += (i & (-i))) arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) ans += arb[i]; + + return ans; + +} + + + +int main(){ + + getline(in, s1); + + getline(in, s2); + + for(int i = 0; i < s1.size(); i++) q[s1[i] - 'a'].push(i+1); + + for(int i = 0; i < s2.size(); i++){ + + if(q[s2[i] - 'a'].empty()) return out << -1, 0; + + int pos = q[s2[i] - 'a'].front(); + + q[s2[i] - 'a'].pop(); + + ans += pos - i - 1 + get(s2.size()) - get(pos); + + add(pos); + + } + + out << ans; + + return 0; + +} diff --git a/swap/aib/041247bb-0b67-11ec-97be-2cf05d84bfc2.cpp b/swap/aib/041247bb-0b67-11ec-97be-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca9bd46abffd073b4a2c1dde1a8c54a9e406e7fb --- /dev/null +++ b/swap/aib/041247bb-0b67-11ec-97be-2cf05d84bfc2.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +string s,s1; + +queue q[30]; + +int z(int i) + +{ + + return i&(-i); + +} + +int arb[100100],rez; + +void add(int i) + +{ + + for(; i <= 100000; i += z(i)) + + arb[i]++; + +} + +int get(int i) + +{ + + int ans = 0; + + for (;i; i-= z(i)) + + ans+= arb[i]; + + return ans; + +} + +int main() + +{ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> s1; + + for (int i = 0 ; i < s.length(); i++) + + { + + q[int(s[i] - 'a')].push(i+1); + + } + + for (int i = 0 ; i < s1.length();i++) + + { + + if (q[s1[i] - 'a'].size() == 0) return cout<<-1,0; + + int poz = q[s1[i] - 'a'].front(); + + q[s1[i] - 'a'].pop(); + + rez += poz-i-1 + get(s.length()) - get(poz); + + add(poz); + + } + + cout << rez; + +} diff --git a/swap/aib/0423e49a-ebfe-4199-9f0f-e5aefcbd080f.cpp b/swap/aib/0423e49a-ebfe-4199-9f0f-e5aefcbd080f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d104f1307e001ccd21544683243e37cc6ada6903 --- /dev/null +++ b/swap/aib/0423e49a-ebfe-4199-9f0f-e5aefcbd080f.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string s1, s2; long long ans; int arb[100100]; + +queue q[30]; + + + +void add(int i){ + + for(; i <= 100000; i += (i & (-i))) arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) ans += arb[i]; + + return ans; + +} + + + +int main(){ + + getline(in, s1); + + getline(in, s2); + + for(int i = 0; i < s1.size(); i++) q[s1[i] - 'a'].push(i+1); + + for(int i = 0; i < s2.size(); i++){ + + if(q[s2[i] - 'a'].empty()) return out << -1, 0; + + int pos = q[s2[i] - 'a'].front(); + + q[s2[i] - 'a'].pop(); + + ans += pos - i - 1 + get(s2.size()) - get(pos); + + add(pos); + + } + + out << ans; + + return 0; + +} diff --git a/swap/aib/04508428-0b67-11ec-98e2-2cf05d84bfc2.cpp b/swap/aib/04508428-0b67-11ec-98e2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..db5de9e76830a70861aa23d360360794b05dcac9 --- /dev/null +++ b/swap/aib/04508428-0b67-11ec-98e2-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +vector > Veq; + +string sir1, sir2; + +vector freq, aib; + + + +void update(int pos) { + + for(;pos < 50005; pos += pos & (-pos)) { + + aib[pos]++; + + } + +} + + + +int query(int pos) { + + int ans = 0; + + + + for(;pos;pos -= pos & (-pos)) { + + ans += aib[pos]; + + } + + + + return ans; + +} + + + +int main() { + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + cin >> sir1; + + cin >> sir2; + + + + int lg1 = sir1.length(); + + int lg2 = sir2.length(); + + + + if(lg1 != lg2) { + + cout << -1 << '\n'; + + return 0; + + } + + + + freq.resize(30); + + for(int i = 0; i < lg1; ++i) { + + freq[sir1[i] - 'a']++; + + freq[sir2[i] - 'a']--; + + } + + + + for(int i = 0; i < 26; ++i) { + + if(freq[i] != 0) { + + cout << -1 << '\n'; + + return 0; + + } + + } + + + + Veq.resize(30); + + aib.resize(50005); + + + + for(int i = 0; i < lg1; ++i) { + + Veq[sir1[i] - 'a'].push_back(i); + + } + + + + int ans = 0; + + for(int i = 0; i < lg2; ++i) { + + ans += max(0, Veq[sir2[i] - 'a'].front() - query(1 + Veq[sir2[i] - 'a'].front())); + + update(Veq[sir2[i] - 'a'].front() + 1); + + Veq[sir2[i] - 'a'].pop_front(); + + } + + + + cout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/048116ca-0b67-11ec-bd59-2cf05d84bfc2.cpp b/swap/aib/048116ca-0b67-11ec-bd59-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ad22ab7eb1630c3d53ac5120963b74a2daf6030 --- /dev/null +++ b/swap/aib/048116ca-0b67-11ec-bd59-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int N; +string A, B; + +vector freq[26]; + +inline int lsb(int node) { + return node & (-node); +} + +const int NMAX = 50000 + 5; +int aib[NMAX]; + +void init() { + for (int i = 1; i <= N; ++ i) + aib[i] = lsb(i); +} + +void update(int where) { + for (; where <= N; where += lsb(where)) + aib[where] --; +} + +int query(int where) { + int ans = 0; + for (; where; where -= lsb(where)) + ans += aib[where]; + return ans; +} + +int main() +{ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + + cin >> A >> B; + N = A.size(); + A = " " + A; + B = " " + B; + + for (int i = N; i; -- i) + freq[B[i] - 'a'].push_back(i); + init(); + + int ans = 0; + for (int i = 1; i <= N; ++ i) + if (freq[A[i] - 'a'].empty()) { + ans = -1; + break; + } + else { + int pos = freq[A[i] - 'a'].back(); + freq[A[i] - 'a'].pop_back(); + update(pos); + ans += query(pos); + } + + if (ans == -1) + cout << "-1\n"; + else + cout << ans << '\n'; + return 0; +} diff --git a/swap/aib/04d60707-0b67-11ec-ba67-2cf05d84bfc2.cpp b/swap/aib/04d60707-0b67-11ec-ba67-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13edb8c8996b20dfa265d89c9220d4c8de6c373c --- /dev/null +++ b/swap/aib/04d60707-0b67-11ec-ba67-2cf05d84bfc2.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + +int arb[100020]; + +queue q[26]; + +void add(int i){ + + for(;i<100000;i+=(i&(-i))) arb[i]++; + +} + +int get(int i){ + + int ans =0; + + for(;i;i-=(i&(-i))) ans+=arb[i]; + + return ans; + +} + +int main(){ + + string a, b; + + int rs=0; + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>a>>b; + + int n=a.length(); + + int m=b.length(); + + for(int i=0;i + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +string s1,s2; +queue Q[27]; +int N; +int aib [110000]; + +int prev(int x){ + return x-(x&(-x)); +} + +int next(int x){ + return x+(x&(-x)); +} +int zer(int x){ + return x&(-x); +} + +void update(int k,int newv){ + for (;k<=100000;k=next(k)) aib[k]+=newv; +} + +int query(int k){ + int rs=0; + for (;k;k=prev(k)) rs+=aib[k]; + return rs; +} + +int main(){ + fin >>s1>>s2; + N=s1.length(); + for (int i=0;i +#include +#include +#include + + + +std::ifstream in("swap.in"); +std::ofstream out("swap.out"); + +int AIB[50005]; +std::queue positions[256]; + +void update(int x, int k) { + + while (x < 50005) { + AIB[x] += k; + x += (x & (-x)); + } +} + +int query(int x) { + + int sum = 0; + + while (x) { + sum += (AIB[x]); + x -= (x & (-x)); + } + + return sum; +} + +int main() { + std::string s1, s2; + getline(in, s1); + getline(in, s2); + + for (int i = 0; i < s1.size(); i++) + { + positions[s1[i]].push(i); + update(i + 1, 1); + } + + int total = 0; + for (int i = 0; i < s2.size(); i++) { + char ch = s2[i]; + + if (positions[ch].empty()) { + out << "-1\n"; + return 0; + } + + int position = positions[ch].front(); + positions[ch].pop(); + + total += query(position); + update(position + 1, -1); + } + + out << total << "\n"; +} diff --git a/swap/aib/0519667b-0b67-11ec-8306-2cf05d84bfc2.cpp b/swap/aib/0519667b-0b67-11ec-8306-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c66340975b75f31f9e9467f424167280bfa27156 --- /dev/null +++ b/swap/aib/0519667b-0b67-11ec-8306-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout("swap.out"); + + + +int arb[100100]; + + + +void add(int i){ + + for(; i < 100000; i+=(i & (-i))) + + arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) + + ans +=arb[i]; + + return ans; + +} + + + + + +string s, t; + + + +queue q[30]; + + + +int main(){ + + + + getline(cin,s); + + getline(cin,t); + + + + for(int i = 0; i < s.length(); i++) + + q[s[i]-'a'].push(i+1); + + + + int rs = 0; + + + + for(int i = 0; i < t.length(); i++) + + { + + if(q[t[i]-'a'].empty()) return(cout << -1, 0); + + int pos = q[t[i]-'a'].front(); q[t[i]-'a'].pop(); + + rs += pos - i - 1 + get(s.length()) - get(pos); + + add(pos); + + } + + + + cout << rs; + + return (0); + +} diff --git a/swap/aib/0538758b-0b67-11ec-93ca-2cf05d84bfc2.cpp b/swap/aib/0538758b-0b67-11ec-93ca-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7faf27c258f15b7fe55816d87a871cc45f082ea7 --- /dev/null +++ b/swap/aib/0538758b-0b67-11ec-93ca-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +int ARB[105000], rs; + +string s, S; + +void add(int i) + +{ + + for (; i < 100000; i += (i & (-i))) ARB[i]++; + +} + +int get (int i) + +{ + + int rs = 0; + + for (;i;i-=(i & (-i))) rs+=ARB[i]; + + return rs; + +} + +queue < int > Q[40]; + +int main(){ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> S; + + for (int i = 0; i < s.size(); i++) Q[s[i]-'a'].push(i+1); + + for (int i = 0; i < S.size(); i++) + + { + + if(Q[S[i]-'a'].empty()) return cout << -1, 0; + + int curr = Q[S[i]-'a'].front(); + + Q[S[i]-'a'].pop(); + + rs += curr - i - 1 + get(s.size()) - get(curr); + + add(curr); + + } + + cout << rs; + + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + + return 0; + +} diff --git a/swap/aib/05584b18-0b67-11ec-a5ae-2cf05d84bfc2.cpp b/swap/aib/05584b18-0b67-11ec-a5ae-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aaea8979118e7ed5b3465dad763698e493a79ee --- /dev/null +++ b/swap/aib/05584b18-0b67-11ec-a5ae-2cf05d84bfc2.cpp @@ -0,0 +1,27 @@ +#include + +int arb[100100],rs; +using namespace std; +int get(int i){ + int ans=0; + for(;i;i-=(i & (-i))) ans+=arb[i]; + return ans; +} +void add(int i){ + for(;i<100000;i+=(i & (-i))) arb[i]++; +} +queue Q[30]; +string A,B; +int main(){ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + cin >> A >> B; + for(int i = 0;i + + +using namespace std; + +int arb[100020]; + +queue q[26]; + +void add(int i){ + + for(;i<100000;i+=(i&(-i))) arb[i]++; + +} + +int get(int i){ + + int ans =0; + + for(;i;i-=(i&(-i))) ans+=arb[i]; + + return ans; + +} + +int main(){ + + string a, b; + + int rs=0; + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>a>>b; + + int n=a.length(); + + int m=b.length(); + + for(int i=0;i + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +string s1,s2; +queue Q[27]; +int N; +int aib [110000]; + +int prev(int x){ + return x-(x&(-x)); +} + +int next(int x){ + return x+(x&(-x)); +} +int zer(int x){ + return x&(-x); +} + +void update(int k,int newv){ + for (;k<=100000;k=next(k)) aib[k]+=newv; +} + +int query(int k){ + int rs=0; + for (;k;k=prev(k)) rs+=aib[k]; + return rs; +} + +int main(){ + fin >>s1>>s2; + N=s1.length(); + for (int i=0;i +#include +#include + + + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout("swap.out"); + + + +int arb[100100]; + + + +void add(int i){ + + for(; i < 100000; i+=(i & (-i))) + + arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) + + ans +=arb[i]; + + return ans; + +} + + + + + +string s, t; + + + +queue q[30]; + + + +int main(){ + + + + getline(cin,s); + + getline(cin,t); + + + + for(int i = 0; i < s.length(); i++) + + q[s[i]-'a'].push(i+1); + + + + int rs = 0; + + + + for(int i = 0; i < t.length(); i++) + + { + + if(q[t[i]-'a'].empty()) return(cout << -1, 0); + + int pos = q[t[i]-'a'].front(); q[t[i]-'a'].pop(); + + rs += pos - i - 1 + get(s.length()) - get(pos); + + add(pos); + + } + + + + cout << rs; + + return (0); + +} diff --git a/swap/aib/05d6c461-0b67-11ec-b486-2cf05d84bfc2.cpp b/swap/aib/05d6c461-0b67-11ec-b486-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7faf27c258f15b7fe55816d87a871cc45f082ea7 --- /dev/null +++ b/swap/aib/05d6c461-0b67-11ec-b486-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +int ARB[105000], rs; + +string s, S; + +void add(int i) + +{ + + for (; i < 100000; i += (i & (-i))) ARB[i]++; + +} + +int get (int i) + +{ + + int rs = 0; + + for (;i;i-=(i & (-i))) rs+=ARB[i]; + + return rs; + +} + +queue < int > Q[40]; + +int main(){ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> S; + + for (int i = 0; i < s.size(); i++) Q[s[i]-'a'].push(i+1); + + for (int i = 0; i < S.size(); i++) + + { + + if(Q[S[i]-'a'].empty()) return cout << -1, 0; + + int curr = Q[S[i]-'a'].front(); + + Q[S[i]-'a'].pop(); + + rs += curr - i - 1 + get(s.size()) - get(curr); + + add(curr); + + } + + cout << rs; + + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + + return 0; + +} diff --git a/swap/aib/05f6f1f7-0b67-11ec-907d-2cf05d84bfc2.cpp b/swap/aib/05f6f1f7-0b67-11ec-907d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aaea8979118e7ed5b3465dad763698e493a79ee --- /dev/null +++ b/swap/aib/05f6f1f7-0b67-11ec-907d-2cf05d84bfc2.cpp @@ -0,0 +1,27 @@ +#include + +int arb[100100],rs; +using namespace std; +int get(int i){ + int ans=0; + for(;i;i-=(i & (-i))) ans+=arb[i]; + return ans; +} +void add(int i){ + for(;i<100000;i+=(i & (-i))) arb[i]++; +} +queue Q[30]; +string A,B; +int main(){ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + cin >> A >> B; + for(int i = 0;i +#include +#include +#include + + + + + + +char buf[16384]; + +int pbuf=16384; + +FILE*fi,*fo; + +inline char nextch(){ + + if(pbuf==16384){ + + fread(buf, 16384, 1, fi); + + pbuf=0; + + } + + return buf[pbuf++]; + +} + +inline long long nextnum(){ + + long long a=0; + + char c=nextch(); + + while((c<'0' || c>'9') && c!='-') + + c=nextch(); + + int semn=1; + + if(c=='-'){ + + semn=-1; + + c=nextch(); + + } + + while('0'<=c && c<='9'){ + + a=a*10+c-'0'; + + c=nextch(); + + } + + return a*semn; + +} + + + + + +int aib[50000 +1], n; + +inline void add(int poz,int nr){ + + for(int i=poz;i<=n;i+=(((i-1)^i)&i)) + + aib[i]+=nr; + +} + +inline int sum(int poz){ + + int s=0; + + for(int i=poz;i>0;i-=(((i-1)^i)&i)) + + s=s+aib[i]; + + return s; + +} + + + +char v1[50000 +1], v2[50000 +1]; + +int f1[27], f2[27]; + +int p[50000 +1]; + +std::vector poz[27]; + +int last[27]; + +int main(){ + + fi=fopen("swap.in","r"); + + fo=fopen("swap.out","w"); + + char c=nextch(); + + int n1=0; + + while('a'<=c && c<='z'){ + + v1[++n1]=c-'a'+1; + + f1[c-'a'+1]++; + + c=nextch(); + + } + + + + c=nextch(); + + int n2=0; + + while('a'<=c && c<='z'){ + + v2[++n2]=c-'a'+1; + + poz[c-'a'+1].push_back(n2); + + f2[c-'a'+1]++; + + c=nextch(); + + } + + + + int z=1; + + while(z<27 && f1[z]==f2[z]) + + z++; + + if(z!=27){ + + fprintf(fo,"-1"); + + return 0; + + } + + for(int i=1;i<=n1;i++) + + p[i]=poz[v1[i]][last[v1[i]]++]; + + int ans=0; + + n=n1; + + for(int i=n1;i>0;i--){ + + ans+=sum(p[i]); + + + + add(p[i], 1); + + } + + fprintf(fo,"%d", ans); + + fclose(fi); + + fclose(fo); + + return 0; + +} diff --git a/swap/aib/06b56749-0b67-11ec-ba33-2cf05d84bfc2.cpp b/swap/aib/06b56749-0b67-11ec-ba33-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796a1357148cc5ce8b495af7cbbc2122e297762e --- /dev/null +++ b/swap/aib/06b56749-0b67-11ec-ba33-2cf05d84bfc2.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + + + + + +char buf[16384]; + +int pbuf=16384; + +FILE*fi,*fo; + +inline char nextch(){ + + if(pbuf==16384){ + + fread(buf, 16384, 1, fi); + + pbuf=0; + + } + + return buf[pbuf++]; + +} + +inline long long nextnum(){ + + long long a=0; + + char c=nextch(); + + while((c<'0' || c>'9') && c!='-') + + c=nextch(); + + int semn=1; + + if(c=='-'){ + + semn=-1; + + c=nextch(); + + } + + while('0'<=c && c<='9'){ + + a=a*10+c-'0'; + + c=nextch(); + + } + + return a*semn; + +} + + + + + +int aib[50000 +1], n; + +inline void add(int poz,int nr){ + + for(int i=poz;i<=n;i+=(((i-1)^i)&i)) + + aib[i]+=nr; + +} + +inline int sum(int poz){ + + int s=0; + + for(int i=poz;i>0;i-=(((i-1)^i)&i)) + + s=s+aib[i]; + + return s; + +} + + + +char v1[50000 +1], v2[50000 +1]; + +int f1[27], f2[27]; + +int p[50000 +1]; + +std::vector poz[27]; + +int last[27]; + +int main(){ + + fi=fopen("swap.in","r"); + + fo=fopen("swap.out","w"); + + char c=nextch(); + + int n1=0; + + while('a'<=c && c<='z'){ + + v1[++n1]=c-'a'+1; + + f1[c-'a'+1]++; + + c=nextch(); + + } + + + + c=nextch(); + + int n2=0; + + while('a'<=c && c<='z'){ + + v2[++n2]=c-'a'+1; + + poz[c-'a'+1].push_back(n2); + + f2[c-'a'+1]++; + + c=nextch(); + + } + + + + int z=1; + + while(z<27 && f1[z]==f2[z]) + + z++; + + if(z!=27){ + + fprintf(fo,"-1"); + + return 0; + + } + + for(int i=1;i<=n1;i++) + + p[i]=poz[v1[i]][last[v1[i]]++]; + + int ans=0; + + n=n1; + + for(int i=n1;i>0;i--){ + + ans+=sum(p[i]); + + + + add(p[i], 1); + + } + + fprintf(fo,"%d", ans); + + fclose(fi); + + fclose(fo); + + return 0; + +} diff --git a/swap/aib/06d643dd-0b67-11ec-a717-2cf05d84bfc2.cpp b/swap/aib/06d643dd-0b67-11ec-a717-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f603bf27e4a5dad5ff72f4ee749b751c0b58599 --- /dev/null +++ b/swap/aib/06d643dd-0b67-11ec-a717-2cf05d84bfc2.cpp @@ -0,0 +1,188 @@ +#include +using namespace std; + + + +const int SPQR = 50005, + + SIGM = 26; + + + +int n; + + + +vector aps[SIGM]; + +char sr1[SPQR], + + sr2[SPQR]; + +int aib[SPQR], + + gnm[SIGM]; + + + +inline int lsb(int arg) { + + return arg&-arg; + +} + + + +inline int cod(int arg) { + + return arg - 'a'; + +} + + + +inline void update(int pos, int val) { + + while(pos < SPQR) { + + aib[pos]+= val; + + pos+= lsb(pos); + + } + +} + + + +inline int query(int pos) { + + int ant = 0; + + + + while(pos > 0) { + + ant+= aib[pos]; + + pos-= lsb(pos); + + } + + + + return ant; + +} + + + +void ave(void) { + + int lst[SIGM]; + + + + memset(lst, 0xFF, sizeof(lst)); + + for(int i=1; i<=n; ++i) + + aps[cod(sr2[i])].push_back(i); + +} + + + +void caesar(int &ant) { + + int f1[SIGM], + + f2[SIGM]; + + int pos; + + + + ant = 0; + + memset(f1, 0x00, sizeof(f1)); + + memset(f2, 0x00, sizeof(f2)); + + + + for(int i=1; i<=n; ++i) { + + ++ f1[ cod(sr1[i]) ]; + + ++ f2[ cod(sr2[i]) ]; + + } + + + + for(int i=0; i>(sr1 + 1)>>(sr2 + 1); + + n = strlen(sr1 + 1); + + + + ave(); + + caesar(ant); + + + + fo< + + +using namespace std; + +int m[200][50005],v[200],nr[50005],f[200],perm[50005]; + +char s1[50005],s2[50005]; + +int AIB[50005],l; + +long long sol; + +void update(int pos) + +{ + + for(int i=pos;i<=l;i+=(i&(-i))) + + AIB[i]++; + +} + +int query(int pos) + +{ + + int s=0; + + for(int i=pos;i>=1;i-=(i&(-i))) + + { + + s+=AIB[i]; + + } + + return s; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",&s1); + + scanf("\n"); + + l=strlen(s1); + + fread(s2,1,l,stdin); + + + + + + for(int i=0;i + + + + + + +using namespace std; + +int n, m, v[50002], aib[50002], occ[50002], ans, p[26 + 2]; + +char s1[50002], s2[50002]; + +short int sum(int x) + +{ + + int i; + + short int s = 0; + + for (int i = x; i >= 1; i -= i & (-i)) + + s += aib[i]; + + return s; + +} + +void add(int x) + +{ + + for (int i = x; i <= n; i += i & (-i)) + + ++ aib[i]; + +} + +void read() + +{ + + freopen("swap.in", "r", stdin); + + scanf("%s\n%s\n", s1 + 1, s2 + 1); + + n = strlen(s1 + 1); + +} + +void solve() + +{ + + int i; + + for (i = 1; i <= n; ++ i) + + { + + ++ occ[s1[i] - 'a']; + + -- occ[s2[i] - 'a']; + + } + + for (i = 0; i < 26; ++ i) + + if (occ[i]) + + { + + ans = -1; + + return ; + + } + + for (i = 1; i <= n; ++ i) + + { + + while (s2[p[s1[i] - 'a']] != s1[i]) + + ++ p[s1[i] - 'a']; + + v[i] = p[s1[i] - 'a'] ++; + + } + + for (i = 1; i <= n; ++ i) + + { + + ans += i - sum(v[i]) - 1; + + add(v[i]); + + } + +} + +void write() + +{ + + freopen("swap.out", "w", stdout); + + printf("%d\n", ans); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/swap/aib/0732cfb2-0b67-11ec-aeec-2cf05d84bfc2.cpp b/swap/aib/0732cfb2-0b67-11ec-aeec-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4196fad4212d41a961210c895318ce1bdd04949 --- /dev/null +++ b/swap/aib/0732cfb2-0b67-11ec-aeec-2cf05d84bfc2.cpp @@ -0,0 +1,65 @@ +#include +#include +using namespace std; + + + +int vf1[26],vf2[26]; +char v1[50000 +1],v2[50000 +1]; +int aib[50000 +1]; +int last[26]; +vector P[26]; +inline void update(int poz){ + int i; + for(i=poz;i<=50000;i+=(i&(-i))) + aib[i]++; +} +inline int query(int poz){ + int i,sum; + sum=0; + for(i=poz;i>0;i-=(i&(-i))) + sum+=aib[i]; + return sum; +} +int main(){ + FILE*fi,*fout; + int n1,n2,poz,i,ans; + char a; + fi=fopen("swap.in" ,"r"); + fout=fopen("swap.out" ,"w"); + a=fgetc(fi); + n1=0; + while(a!='\n'){ + a-='a'; + vf1[a]++; + v1[++n1]=a; + a=fgetc(fi); + } + a=fgetc(fi); + n2=0; + while(a!='\n'){ + a-='a'; + vf2[a]++; + v2[++n2]=a; + P[a].push_back(n2); + a=fgetc(fi); + } + i=0; + while(i<26&&vf1[i]==vf2[i]) + i++; + if(i<26) + fprintf(fout,"-1"); + else{ + ans=0; + for(i=1;i<=n1;i++){ + poz=P[v1[i]][last[v1[i]]]; + ans+=(poz+i-1-query(poz-1))-i; + last[v1[i]]++; + update(poz); + } + fprintf(fout,"%d" ,ans); + } + fclose(fi); + fclose(fout); + return 0; +} diff --git a/swap/aib/074079ed-7b7e-43ed-bc3b-cebfbf4f920f.cpp b/swap/aib/074079ed-7b7e-43ed-bc3b-cebfbf4f920f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d18bc253eb8276168ebfbe0a129828708386398 --- /dev/null +++ b/swap/aib/074079ed-7b7e-43ed-bc3b-cebfbf4f920f.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + + + +int main(){ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +char si[50000 +1],sf[50000 +1]; + +int i,j,k,k2,f[26],f1[26],l,ok,pi,pf,n0, + +ud,poz[26]={0},a[26][50000/4],p,uc[26]={0}; + +long long nr; + +scanf("%s%s",si,sf); + +l=strlen(si); + +for(i=0;i<26;i++) f[i]=f1[i]=0; + +for(i=0;si[i];++i){ + + a[si[i]-'a'][f[si[i]-'a']]=i; + + f[si[i]-'a']++; + + f1[sf[i]-'a']++; + + } + +ok=1; + +for(i=0;i<26&&ok;++i) + + ok=f[i]==f1[i]; + +if(!ok) {nr=-1;goto finish;} + +nr=0; + +pi=0;pf=l-1;ud=0; + +for(i=0;i=j) uc[k]--; + + + + for(k2=uc[k];a[k][k2] +#include + + + + +using namespace std; + +int n; + +int aib[50010],v[50010]; + +int countA[30],countB[30],first[30][50010]; + +char a[50010],b[50010]; + +void Update(int x){ + + for(x;x<=n;x=x+((x&(x-1))^x)) + + aib[x]++; + +} + +int Query(int x){ + + int answer=0; + + for(x;x>0;x=x-((x&(x-1))^x)) + + answer+=aib[x]; + + return answer; + +} + +int main(){ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i; + + long long answer=0; + + scanf("%s%s",a,b); + + n=strlen(a); + + for(i=0;i=0;i--){ + + answer+=Query(v[i]); + + Update(v[i]); + + } + + printf("%lld",answer); + + return 0; + +} diff --git a/swap/aib/075e5c2c-d01c-45c4-9653-04a6a0849f5b.cpp b/swap/aib/075e5c2c-d01c-45c4-9653-04a6a0849f5b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74ec581c8251e7e984afd469537387924c351662 --- /dev/null +++ b/swap/aib/075e5c2c-d01c-45c4-9653-04a6a0849f5b.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +vector v[50]; + +vector v1[50]; + +char a[50005],b[50005]; + +int n,n1,aib[50005],pozi[50]; + +int ub (int x) + +{ + + return x&(-x); + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz;i<=n;i+=ub(i)) + + { + + aib[i]+=val; + + } + +} + +int suma (int poz) + +{ + + int i,sum=0; + + for (i=poz;i>0;i-=ub(i)) + + { + + sum=sum+aib[i]; + + } + + return sum; + +} + +int main() + +{ + + f>>a>>b; + + n=strlen(a); + + for (int i=0;i +#include +#include +using namespace std; + +int aib[50005],sum,poz,i,n,x,nr,ind[28]; + +long long tot,summ; + +vector v[29]; + +string s1,s2; + +inline int lbit(int x) + +{ + + return ((x^(x-1))&x); + +} + +void update(int y,int val) + +{ + + for(poz=y;poz<=n;poz+=lbit(poz)) + + aib[poz]+=val; + +} + +int compute(int y) + +{ + + sum=0; + + for(poz=y;poz>0;poz-=lbit(poz)) + + sum+=aib[poz]; + + return sum; + +} + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>s1>>s2; + + if(s1.size()!=s2.size()) {g<<"-1";return 0;} + + n=s1.size(); + + for(i=0;iv[nr].size()) {g<<"-1";return 0;} + + else + + { + + x=v[nr][ind[nr]]; + + summ=compute(x+1)-(i+1); + + tot+=summ; + + update(1,1); + + update(x+1,-1); + + ind[nr]++; + + } + + } + + g< + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int NMAX = 50005; + +int n; + +char s1[NMAX]; char s2[NMAX]; +int cnt1[127]; int cnt2[127]; + +int perm[NMAX]; +int aib[NMAX]; + +vector a[127]; +long long swaps = 0; + +int bit(int x) { return x & -x; } + +void ins(int pos) { + + for( ; pos <= n; pos += bit(pos)) + aib[pos]++; +} + +int query(int pos) { + + int sum = 0; + for( ; pos > 0 ; pos -= bit(pos)) + sum += aib[pos]; + return sum; +} + +int main() { + + fin >> s1 + 1 >> s2 + 1; + n = strlen(s1 + 1); + + for(int i = n ; i >= 1; --i) + a[s2[i]].push_back(i), cnt2[s2[i]]++; + + for(int i = 1; i <= n ; ++i) + cnt1[s1[i]]++; + + for(int i = 'a' ; i <= 'z'; ++i) { + a[i].reserve(n / 21); + if(cnt1[i] != cnt2[i]) { + fout << -1 << '\n'; + exit(0); + } + } + + for(int i = 1; i <= n ; ++i) { + + perm[i] = a[s1[i]][ a[s1[i]].size() - 1 ]; + a[s1[i]].pop_back(); + } + + for(int i = 1; i <= n ; ++i) { + swaps += i - 1 - query(perm[i] - 1) ; + ins(perm[i]); + } + + fout << swaps << '\n'; + + return 0; +} diff --git a/swap/aib/07f90295-0b67-11ec-a937-2cf05d84bfc2.cpp b/swap/aib/07f90295-0b67-11ec-a937-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b97e846d0ed29860b0f676059d30227cc53ab91 --- /dev/null +++ b/swap/aib/07f90295-0b67-11ec-a937-2cf05d84bfc2.cpp @@ -0,0 +1,93 @@ +#include +#include + + + + +using namespace std; + +int n; + +int aib[50010],v[50010]; + +int countA[30],countB[30],first[30][50010]; + +char a[50010],b[50010]; + +void Update(int x){ + + for(x;x<=n;x=x+((x&(x-1))^x)) + + aib[x]++; + +} + +int Query(int x){ + + int answer=0; + + for(x;x>0;x=x-((x&(x-1))^x)) + + answer+=aib[x]; + + return answer; + +} + +int main(){ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i; + + long long answer=0; + + scanf("%s%s",a,b); + + n=strlen(a); + + for(i=0;i=0;i--){ + + answer+=Query(v[i]); + + Update(v[i]); + + } + + printf("%lld",answer); + + return 0; + +} diff --git a/swap/aib/083996fb-0b67-11ec-b676-2cf05d84bfc2.cpp b/swap/aib/083996fb-0b67-11ec-b676-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9ea233807b70cd9b4bea1d3b7d02eee9275d125 --- /dev/null +++ b/swap/aib/083996fb-0b67-11ec-b676-2cf05d84bfc2.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +char s1[50000 + 2], s2[50000 + 2]; + +int aib[50000 + 1], next[50000], ut[26]; + +int n; + +int fr1[26], fr2[26]; + + + +inline int ultb(int x){ + + return x & (-x); + +} + + + +int suma(int x){ + + if(x == 0) + + return 0; + + return aib[x] + suma(x - ultb(x)); + +} + + + +void add(int x, int d){ + + if(x <= n){ + + aib[x] += d; + + add(x + ultb(x), d); + + } + +} + + + +int main(){ + + FILE *in = fopen("swap.in", "r"); + + fgets(s1, 50000 + 2, in); + + fgets(s2, 50000 + 2, in); + + n = 0; + + while(s1[n] >= 'a' && s1[n] <= 'z'){ + + next[n] = ut[s1[n] - 'a']; + + ut[s1[n] - 'a'] = n; + + n++; + + } + + fclose(in); + + int i, p, rez = 0; + + for(i = 0; i < n; i++){ + + s1[i] -= 'a'; + + s2[i] -= 'a'; + + fr1[s1[i]]++; + + fr2[s2[i]]++; + + } + + bool cnt = 1; + + for(i = 0; i < 26; i++) + + if(fr1[i] != fr2[i]) + + cnt = 0; + + FILE *out = fopen("swap.out", "w"); + + if(cnt == 0) + + fprintf(out, "-1"); + + else{ + + for(i = 1; i <= n; i++) + + aib[i] = ultb(i); + + for(i = n - 1; i >= 0; i--){ + + p = ut[s2[i]]; + + ut[s2[i]] = next[ut[s2[i]]]; + + rez += suma(n) - suma(p + 1); + + add(p + 1, -1); + + } + + fprintf(out, "%d", rez); + + } + + fclose(out); + + return 0; + +} diff --git a/swap/aib/085914ea-0b67-11ec-86b5-2cf05d84bfc2.cpp b/swap/aib/085914ea-0b67-11ec-86b5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..728ff749f537095b7ac68e133cf02a708929a97b --- /dev/null +++ b/swap/aib/085914ea-0b67-11ec-86b5-2cf05d84bfc2.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int NMAX = 50005; + +int n; + +char s1[NMAX]; char s2[NMAX]; +int cnt1[127]; int cnt2[127]; + +int perm[NMAX]; +int aib[NMAX]; + +vector a[127]; +long long swaps = 0; + +int bit(int x) { return x & -x; } + +void ins(int pos) { + + for( ; pos <= n; pos += bit(pos)) + aib[pos]++; +} + +int query(int pos) { + + int sum = 0; + for( ; pos > 0 ; pos -= bit(pos)) + sum += aib[pos]; + return sum; +} + +int main() { + + fin >> s1 + 1 >> s2 + 1; + n = strlen(s1 + 1); + + for(int i = n ; i >= 1; --i) + a[s2[i]].push_back(i), cnt2[s2[i]]++; + + for(int i = 1; i <= n ; ++i) + cnt1[s1[i]]++; + + for(int i = 'a' ; i <= 'z'; ++i) { + + if(cnt1[i] != cnt2[i]) { + fout << -1 << '\n'; + exit(0); + } + } + + for(int i = 1; i <= n ; ++i) { + + perm[i] = a[s1[i]][ a[s1[i]].size() - 1 ]; + a[s1[i]].pop_back(); + } + + for(int i = 1; i <= n ; ++i) { + swaps += i - 1 - query(perm[i] - 1) ; + ins(perm[i]); + } + + fout << swaps << '\n'; + + return 0; +} diff --git a/swap/aib/08781010-0b67-11ec-bf01-2cf05d84bfc2.cpp b/swap/aib/08781010-0b67-11ec-bf01-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4196fad4212d41a961210c895318ce1bdd04949 --- /dev/null +++ b/swap/aib/08781010-0b67-11ec-bf01-2cf05d84bfc2.cpp @@ -0,0 +1,65 @@ +#include +#include +using namespace std; + + + +int vf1[26],vf2[26]; +char v1[50000 +1],v2[50000 +1]; +int aib[50000 +1]; +int last[26]; +vector P[26]; +inline void update(int poz){ + int i; + for(i=poz;i<=50000;i+=(i&(-i))) + aib[i]++; +} +inline int query(int poz){ + int i,sum; + sum=0; + for(i=poz;i>0;i-=(i&(-i))) + sum+=aib[i]; + return sum; +} +int main(){ + FILE*fi,*fout; + int n1,n2,poz,i,ans; + char a; + fi=fopen("swap.in" ,"r"); + fout=fopen("swap.out" ,"w"); + a=fgetc(fi); + n1=0; + while(a!='\n'){ + a-='a'; + vf1[a]++; + v1[++n1]=a; + a=fgetc(fi); + } + a=fgetc(fi); + n2=0; + while(a!='\n'){ + a-='a'; + vf2[a]++; + v2[++n2]=a; + P[a].push_back(n2); + a=fgetc(fi); + } + i=0; + while(i<26&&vf1[i]==vf2[i]) + i++; + if(i<26) + fprintf(fout,"-1"); + else{ + ans=0; + for(i=1;i<=n1;i++){ + poz=P[v1[i]][last[v1[i]]]; + ans+=(poz+i-1-query(poz-1))-i; + last[v1[i]]++; + update(poz); + } + fprintf(fout,"%d" ,ans); + } + fclose(fi); + fclose(fout); + return 0; +} diff --git a/swap/aib/08978378-0b67-11ec-a6cf-2cf05d84bfc2.cpp b/swap/aib/08978378-0b67-11ec-a6cf-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e499387fd782468a2c40d35f58531e1f31179681 --- /dev/null +++ b/swap/aib/08978378-0b67-11ec-a6cf-2cf05d84bfc2.cpp @@ -0,0 +1,111 @@ +#include +#include + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int mat[30][50005] , aib[50005] , nr[30] , c1[30] , c2[30]; + +int sol , n; + + + +char sir1[50005] , sir2[50005]; + + + +void update(int poz , int val) { + + while(poz <= n) { + + aib[poz] += val; + + poz += poz & (poz ^ (poz - 1)); + + } + +} + + + +int query(int poz) { + + int sum = 0; + + while(poz >= 1) { + + sum += aib[poz]; + + poz -= poz & (poz ^ (poz - 1)); + + } + + + + return sum ; + +} + + + +int main() { + + f >> sir1 + 1 >> sir2 + 1; + + + + n = strlen(sir1 + 1); + + for(int i = 1 ; i <= n ; ++i) { + + mat[sir1[i] - 'a'][++mat[sir1[i] - 'a'][0]] = i; + + ++c1[sir1[i] - 'a']; + + ++c2[sir2[i] - 'a']; + + } + + + + for(char ch = 'a' ; ch <= 'z' ; ++ch) { + + if(c1[ch - 'a'] != c2[ch - 'a']) { + + g << -1; + + return 0; + + } + + } + + + + for(int i = 1 ; i <= n ; ++i) { + + sol += mat[sir2[i] - 'a'][++nr[sir2[i] - 'a']] - 1 - query(mat[sir2[i] - 'a'][nr[sir2[i] - 'a']] - 1); + + update(mat[sir2[i] - 'a'][nr[sir2[i] - 'a']] , 1); + + } + + + + g << sol; + + return 0; + +} diff --git a/swap/aib/08e59535-0b67-11ec-bf9e-2cf05d84bfc2.cpp b/swap/aib/08e59535-0b67-11ec-bf9e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..805f521c49b5fc9929de8b9a9fe31a4d16f32533 --- /dev/null +++ b/swap/aib/08e59535-0b67-11ec-bf9e-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +using namespace std; + +int n,i,j,sol,aux,l; + +vector poz[30]; + +int p[30]; + +int aib[50005]; + +char s1[50005],s2[50005]; + +void update(int pos) + +{ + + int i; + + for(i=pos;i<=n;i+=((i)&(-i))) + + aib[i]++; + +} + +int query(int pos) + +{ + + int i; + + int sum=0; + + for(i=pos;i>0;i-=((i)&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s\n%s",s1+1,s2+1); + + n=strlen(s1+1); + + if(strlen(s1+1)!=strlen(s2+1)) + + { + + printf("-1"); + + return 0; + + } + + for(i=1;i<=n;i++) + + poz[((s1[i])-'a')].push_back(i); + + for(i=1;i<=n;i++) + + { + + l=((s2[i])-'a'); + + if(p[l]>=(int)poz[l].size()) + + { + + printf("-1"); + + return 0; + + } + + sol+=abs(i-(poz[l][p[l]]+query(n)-query(poz[l][p[l]]))); + + update(poz[l][p[l]]); + + p[l]++; + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/09721284-0b67-11ec-a4f3-2cf05d84bfc2.cpp b/swap/aib/09721284-0b67-11ec-a4f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82a2b62d75358f273bc244c6cb8a5ed77e33024e --- /dev/null +++ b/swap/aib/09721284-0b67-11ec-a4f3-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i,l,sol; + +int fr_a[30],fr_b[30],AIB[1<<16],v[26][1<<16]; + +char a[1<<16],b[1<<16]; + +inline int zeros(int x) + +{ + + return ((x^(x-1))&x); + +} + +inline void bagval(int poz,int val) + +{ + + while(poz<=l) + + { + + AIB[poz]+=val; + + poz+=zeros(poz); + + } + +} + +inline int Query(int poz) + +{ + + int suma=0; + + while(poz) + + { + + suma+=AIB[poz]; + + poz-=zeros(poz); + + } + + return suma; + +} + +int main() + +{ + + f>>(a+1)>>(b+1); + + l=strlen(a+1); + + for(i=l;i;--i) + + { + + short w=b[i]-'a'; + + v[w][++v[w][0]]=i; + + bagval(i,1); + + } + + for(i=1;i<=l;++i) + + { + + short w=a[i]-'a'; + + if(!v[w][0]) + + { + + g<<-1; + + return 0; + + } + + sol+=Query(v[w][v[w][0]]-1); + + bagval(v[w][v[w][0]],-1); + + --v[w][0]; + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +char s1[50003]; + +char s2[50003]; + +int pr[50003]; + +long long aib[50003]; + +queue cr[30]; + +int i; + +int ln; + + + +inline void update(int val) { + + for(int i = val; i <= ln; i += i&-i) + + aib[i]++; + +} + + + +inline long long query(int val) { + + long long rez = 0; + + for(int i = val; i > 0; i -= i&-i) { + + rez += aib[i]; + + } + + return rez; + +} + + + +int main() { + + + + in.getline(s1, 50003); + + in.getline(s2, 50003); + + + + for(i = 0; s2[i]; i++) + + cr[s2[i]-'a'].push(i); + + + + for(i = 0; s1[i]; i++) { + + if(cr[s1[i]-'a'].empty()) { + + out << "-1"; + + return 0; + + } + + pr[i] = cr[s1[i]-'a'].front()+1; + + cr[s1[i]-'a'].pop(); + + } + + + + ln = i; + + + + for(i = 0; i < 30; i++) { + + if(!cr[i].empty()) { + + out << "-1"; + + return 0; + + } + + } + + + + long long rez = 0; + + + + for(i = ln-1; i >= 0; i--) { + + rez += query(pr[i]); + + update(pr[i]); + + } + + + + out << rez; + + + + return 0; + +} diff --git a/swap/aib/0a3b10fb-0b67-11ec-a92b-2cf05d84bfc2.cpp b/swap/aib/0a3b10fb-0b67-11ec-a92b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b58f7e14eb43a13ac1a70c027cd79dcba4a3827 --- /dev/null +++ b/swap/aib/0a3b10fb-0b67-11ec-a92b-2cf05d84bfc2.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +int n, i; +char a[50011], b[50011]; +int perm[50011]; +int id[26][50011]; +int pos[26], act[26]; + +int aib[50011]; +long long ans; + +int zrs(int x) { + return (x ^ (x - 1)) & x; +} + +void add(int pos) { + while (pos <= n) { + aib[pos]++; + pos += zrs(pos); + } +} + +int sum(int pos) { + int ans = 0; + while (pos) { + ans += aib[pos]; + pos -= zrs(pos); + } + return ans; +} + +int main() +{ + freopen("swap.in","r",stdin); + freopen("swap.out","w",stdout); + + scanf("%s\n%s", a + 1, b + 1); + n = strlen(a + 1); + + for (i = 1; i <= n; i++) + id[ b[i] - 'a' ][pos[ b[i] - 'a' ]++] = i; + + for (i = 1; i <= n; i++) { + int go = a[i] - 'a'; + if (pos[go] == act[go]) { + printf("-1"); + return 0; + } + perm[i] = id[go][ act[go]++ ]; + } + + ans = 1LL * n * (n - 1); + ans /= 2; + + for (i = 1; i <= n; i++) { + ans -= sum(perm[i]); + add(perm[i]); + } + + printf("%lld", ans); + + return 0; +} diff --git a/swap/aib/0a5c23e2-0b67-11ec-84d2-2cf05d84bfc2.cpp b/swap/aib/0a5c23e2-0b67-11ec-84d2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..261e380716b9459d6abd89894587013059fe46a1 --- /dev/null +++ b/swap/aib/0a5c23e2-0b67-11ec-84d2-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +# include + + + + +using namespace std; + + + +const int Nmax = 50000 + 5; + + + +queue Q[32]; + +char a[Nmax], b[Nmax]; + +int N, i, val, aib[Nmax]; + +long long sol = 0; + + + +void add(int x) + +{ + + int i; + + for (i = x; i <= N; i += (i&(-i))) aib[i]++; + +} + +int sum (int x) + +{ + + int i, s = 0; + + + + for (i = x - 1; i; i -= (i&(-i))) s += aib[i]; + + + + return s; + +} + + + +int main () + + + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + + + + N = strlen(a); + + + + for (i = 0; i < N; ++i) + + Q[ b[i] - 'a' ].push(i + 1); + + + + for (i = 0; i < N; ++i) + + { + + if (Q[ a[i] - 'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + + + val = Q[a[i] - 'a'].front(); + + Q[a[i] - 'a'].pop(); + + + + sol += sum(val); + + add(val); + + } + + + + printf("%lld\n", 1LL*N * (N - 1) / 2 - sol); + + + + return 0; + +} diff --git a/swap/aib/0a7b9aba-0b67-11ec-b6d7-2cf05d84bfc2.cpp b/swap/aib/0a7b9aba-0b67-11ec-b6d7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b5862d7455ebf054313ed6e76074f8d50f8251f --- /dev/null +++ b/swap/aib/0a7b9aba-0b67-11ec-b6d7-2cf05d84bfc2.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; + + + +int n, i, j, sol, p, x; + +int f[27]; + +char s[50002], a[50002]; + +int aib[50002]; + +int pos[27][50002]; + +int ind[27]; + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50001, stdin); + + fgets(a, 50001, stdin); + + n = strlen(a + 1); + + + + for(i = 1; i <= n; i++) + + { + + f[ s[i] - 'a' ]++; + + f[ a[i] - 'a' ]--; + + } + + + + for(i = 0; i < 26; i++) + + if(f[i] != 0) + + { + + printf("-1"); + + return 0; + + } + + + + for(i = 1; i <= n; i++) + + pos[ s[i] - 'a' ][ ++ind[ s[i] - 'a' ] ] = i; + + + + for(i = 0; i < 26; i++) + + ind[i] = 1; + + + + for(i = 1; i <= n; i++) + + { + + p = pos[ a[i] - 'a' ][ ind[ a[i] - 'a' ]++ ]; + + sol += p - 1; + + for(j = p - 1; j >= 1; j -= (j & (-j))) + + sol -= aib[j]; + + for(j = p; j <= n; j += (j & (-j))) + + aib[j]++; + + } + + + + printf("%d", sol); + + + + return 0; + +} diff --git a/swap/aib/0a9d9609-0b67-11ec-b05c-2cf05d84bfc2.cpp b/swap/aib/0a9d9609-0b67-11ec-b05c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c90d83b9061a5eb32ed99b729b3ec6d37566a9c3 --- /dev/null +++ b/swap/aib/0a9d9609-0b67-11ec-b05c-2cf05d84bfc2.cpp @@ -0,0 +1,176 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int n, sol, sum, Nrc[30], P[50010], ARBI[50010 * 3]; + +string S1, S2; + +queue < int > V[30]; + + + +void Update(int nod, int left, int right, int poz, int value) + +{ + + if (left == right) + + { + + ARBI[nod] = value; + + return; + + } + + + + int mid = (left + right) >> 1; + + if (poz <= mid) + + { + + Update(nod * 2, left, mid, poz, value); + + } + + else + + { + + Update(nod * 2 + 1, mid + 1, right, poz, value); + + } + + ARBI[nod] = ARBI[nod * 2] + ARBI[nod * 2 + 1]; + +} + + + +void Query(int nod, int left, int right, int st, int dr) + +{ + + if (st <= left && right <= dr) + + { + + sum += ARBI[nod]; + + return; + + } + + + + int mid = (left + right) >> 1; + + if (st <= mid) + + { + + Query(nod * 2, left, mid, st, dr); + + } + + if (dr > mid) + + { + + Query(nod * 2 + 1, mid + 1, right, st, dr); + + } + +} + + + +int main() + +{ + + fin >> S1 >> S2; + + n = S1.size(); + + for (int i = 0; i < n; i ++) + + { + + Nrc[S1[i] - 'a'] ++; + + Nrc[S2[i] - 'a'] --; + + V[S2[i] - 'a'].push(i); + + } + + for (int i = 0; i < 30; i ++) + + { + + if (Nrc[i] != 0) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + } + + for (int i = 0; i < n; i ++) + + { + + P[i + 1] = V[S1[i] - 'a'].front() + 1; + + V[S1[i] - 'a'].pop(); + + } + + + + Update(1, 1, n, P[n], 1); + + for (int i = n - 1; i >= 1; i --) + + { + + sum = 0; + + Query(1, 1, n, 1, P[i]); + + sol += sum; + + Update(1, 1, n, P[i], 1); + + } + + + + fout << sol << '\n'; + + fout.close(); + + return 0; + +} diff --git a/swap/aib/0adc7a16-0b67-11ec-95af-2cf05d84bfc2.cpp b/swap/aib/0adc7a16-0b67-11ec-95af-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e4e99da90755ed6404658eb0bcbd3e24272c7b3 --- /dev/null +++ b/swap/aib/0adc7a16-0b67-11ec-95af-2cf05d84bfc2.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +bool check_possible(const string& from, const string& to){ + + array cnt_from = {}, cnt_to = {}; + + for(const auto x : from){ + + ++cnt_from[x-'a']; } + + for(const auto x : to){ + + ++cnt_to[x-'a']; } + + return cnt_from == cnt_to; } + + + +void mk_permut(const string& from, const string& to, vector& permut){ + + array, 26> pozitii; + + for(int i = 0; i < from.size(); ++i){ + + pozitii[from[i]-'a'].push_back(i); } + + for(auto& x : pozitii){ + + reverse(begin(x), end(x)); } + + + + for(int i = 0; i < to.size(); ++i){ + + permut[pozitii[to[i]-'a'].back()] = i; + + pozitii[to[i]-'a'].pop_back(); } } + + + +class aib{ + + int n; + + vector buf; + +public: + + aib(const int N): n(N), buf(n, 0){} + + void update(int poz){ + + for( ; poz < n; poz += (poz+1)&(-poz-1)){ + + ++buf[poz]; } } + + int query(int poz){ + + int rez = 0; + + for( ; poz >= 0; poz -= (poz+1)&(-poz-1)){ + + rez += buf[poz]; } + + return rez; } }; + + + +int cnt_inversiuni(const vector& permut){ + + aib a(permut.size()); + + int rez = 0; + + for(int i = permut.size()-1; i >= 0; --i){ + + rez += a.query(permut[i]); + + a.update(permut[i]); } + + return rez; } + + + +int main(){ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + string from, to; + + f >> from >> ws >> to; + + + + if(!check_possible(from, to)){ + + g << -1; + + return 0; } + + + + vector permut(from.size()); + + mk_permut(from, to, permut); + + g << cnt_inversiuni(permut); + + return 0; } diff --git a/swap/aib/0afdca11-0b67-11ec-ab7e-2cf05d84bfc2.cpp b/swap/aib/0afdca11-0b67-11ec-ab7e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..064ee247aec7482cd0792576f4e859347befe78e --- /dev/null +++ b/swap/aib/0afdca11-0b67-11ec-ab7e-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int n, sol, Nrc[30], P[50010], AIB[50010]; + +string S1, S2; + +queue < int > V[30]; + + + +inline int zeros(int x) + +{ + + return (x ^ (x - 1) & x); + +} + + + +void Update(int poz, int value) + +{ + + while (poz <= n) + + { + + AIB[poz] += value; + + poz += zeros(poz); + + } + +} + + + +int Query(int poz, int sum = 0) + +{ + + while (poz) + + { + + sum += AIB[poz]; + + poz -= zeros(poz); + + } + + + + return sum; + +} + + + +int main() + +{ + + fin >> S1 >> S2; + + n = S1.size(); + + for (int i = 0; i < n; i ++) + + { + + Nrc[S1[i] - 'a'] ++; + + Nrc[S2[i] - 'a'] --; + + V[S2[i] - 'a'].push(i); + + } + + for (int i = 0; i < 30; i ++) + + { + + if (Nrc[i] != 0) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + } + + for (int i = 0; i < n; i ++) + + { + + P[i + 1] = V[S1[i] - 'a'].front() + 1; + + V[S1[i] - 'a'].pop(); + + } + + + + Update(P[n], 1); + + for (int i = n - 1; i >= 1; i --) + + { + + sol += Query(P[i]); + + Update(P[i], 1); + + } + + + + fout << sol << '\n'; + + fout.close(); + + return 0; + +} diff --git a/swap/aib/0b20c944-26bf-4e61-9979-bb3838341be1.cpp b/swap/aib/0b20c944-26bf-4e61-9979-bb3838341be1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d1715bd48914a9d22c3d415cc484dab48c82b2b --- /dev/null +++ b/swap/aib/0b20c944-26bf-4e61-9979-bb3838341be1.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int NMAX = 50005; + +int n; + +char s1[NMAX]; char s2[NMAX]; +int cnt1[127]; int cnt2[127]; + +int perm[NMAX]; +int aib[NMAX]; + +vector a[127]; +long long swaps = 0; + +int bit(int x) { return x & -x; } + +void ins(int pos) { + + for( ; pos <= n; pos += bit(pos)) + aib[pos]++; +} + +int query(int pos) { + + int sum = 0; + for( ; pos > 0 ; pos -= bit(pos)) + sum += aib[pos]; + return sum; +} + +int main() { + + fin >> s1 + 1 >> s2 + 1; + n = strlen(s1 + 1); + + for(int i = n ; i >= 1; --i) + a[s2[i]].push_back(i), cnt2[s2[i]]++; + + for(int i = 1; i <= n ; ++i) + cnt1[s1[i]]++; + + for(int i = 'a' ; i <= 'z'; ++i) { + a[i].reserve(n / 21); + if(cnt1[i] != cnt2[i]) { + fout << -1 << '\n'; + exit(0); + } + } + + for(int i = 1; i <= n ; ++i) { + + perm[i] = a[s1[i]][ a[s1[i]].size() - 1 ]; + a[s1[i]].pop_back(); + } + + for(int i = 1; i <= n ; ++i) { + swaps += i - 1 - query(perm[i] - 1) ; + ins(perm[i]); + } + + fout << swaps << '\n'; + + return 0; +} diff --git a/swap/aib/0b5d096a-0b67-11ec-b714-2cf05d84bfc2.cpp b/swap/aib/0b5d096a-0b67-11ec-b714-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46735554291d94cc7272584104355560b42a6166 --- /dev/null +++ b/swap/aib/0b5d096a-0b67-11ec-b714-2cf05d84bfc2.cpp @@ -0,0 +1,135 @@ +#include +#include +using namespace std; + + + + + + + +char a[51002], b[51002]; + +int aib[51002], x[51002], nx, n; + +deque< int > v[30]; + + + +void update(int, int) ; + +int query(int) ; + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + int i, p, ch, rez; + + + + fgets(a , 50001, stdin); + + fgets(b , 50001, stdin); + + + + for(n = 0, i = 1; a[i] != '\0'; ++i, ++n) + + { + + ch = a[i] - 'a'; + + v[ch].push_back(i); + + } + + + + for(i = 1; i <= n; ++i) + + { + + ch = b[i] - 'a'; + + if(v[ch].empty()) + + { + + printf("-1\n"); + + return 0; + + } + + + + x[++nx] = v[ch].front(); + + v[ch].pop_front(); + + } + + + + for (i = 1;i <= n;++i) + + update(i, 1); + + + + for (rez=0,i = 1;i <= n;++i) + + { + + update(x[i], -1); + + rez += query(x[i]); + + } + + + + printf("%d\n", rez); + + + + return 0; + +} + + + +void update(int p, int val) + +{ + + for(; p <= n; p += p & -p) aib[p] += val; + +} + + + +int query(int p) + +{ + + int rez; + + + + for(rez = 0; p > 0; p -= p & -p) rez += aib[p]; + + + + return rez; + +} diff --git a/swap/aib/0b6c00a9-b047-4af9-ac48-dc4aa1a1acb4.cpp b/swap/aib/0b6c00a9-b047-4af9-ac48-dc4aa1a1acb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a14fce94f3cbaaff8632107898057c0e2bc0b42a --- /dev/null +++ b/swap/aib/0b6c00a9-b047-4af9-ac48-dc4aa1a1acb4.cpp @@ -0,0 +1,112 @@ +#include + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int N, M; + +int bit[50005], a[50005], b[50005]; + +char S[50005]; + +queue Q[26]; + + + +void update(int pos) { + + for (int i = pos; i <= N; i += i & (-i)) + + bit[i]++; + +} + +int calc(int pos) { + + int sum = 0 ; + + for (int i = pos; i > 0; i -= i & (-i)) + + sum += bit[i]; + + return sum; + +} + +int main() + +{ + + f.get(S, 50005), f.get(); + + N = strlen(S); + + for (int i = 1; i <= N; ++i) + + a[i] = S[i - 1] - 'a', Q[a[i]].push(i); + + + + f.get(S, 50005), f.get(); + + M = strlen(S); + + for (int i = 1; i <= N; ++i) + + b[i] = S[i - 1] - 'a'; + + if (N != M) { + + g << -1; + + return 0; + + } + + + + int ans = 0; + + for (int i = 1; i <= N; ++i) + + if (Q[b[i]].empty()) { + + g << -1; + + return 0; + + } + + else { + + int j = Q[b[i]].front(); + + Q[b[i]].pop(); + + ans += j - i + calc(N) - calc(j); + + update(j); + + } + + + + g << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/0b7f0aa7-0b67-11ec-bb2f-2cf05d84bfc2.cpp b/swap/aib/0b7f0aa7-0b67-11ec-bb2f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a194c09af0e12d76f9a02368afd63418481452fa --- /dev/null +++ b/swap/aib/0b7f0aa7-0b67-11ec-bb2f-2cf05d84bfc2.cpp @@ -0,0 +1,117 @@ +#include +#include +#include +#include +using namespace std; + + + +char s1[50010], s2[50010]; + +int aib[50010], n; + +queue poz[32]; + + + +inline void add (int x) + +{ + + for (int i = x; i <= n; i += i & (-i)) + + ++aib[i]; + +} + + + +inline int searchh (int x) + +{ + + int rez = 0; + + for (int i = x - 1; i; i -= i & (-i)) + + rez += aib[i]; + + + + return rez; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (s1, 50001, stdin); + + fgets (s2, 50001, stdin); + + + + n = strlen (s1); + + for (int i = 0; i < n; ++i) + + poz[s2[i] - 'a'].push (i + 1); + + + + int nr = 0; + + for (int i = 0; i < n; ++i) + + { + + if (!poz[s1[i] - 'a'].size ()) + + { + + printf ("-1\n"); + + return 0; + + } + + + + int x = poz[s1[i] - 'a'].front (); + + poz[s1[i] - 'a'].pop (); + + + + nr += searchh (x); + + add (x); + + } + + + + long long rez = 1LL * n * (n - 1) / 2LL; + + rez -= 1LL * nr; + + + + nr = (int) rez; + + printf ("%d\n", nr); + + + + return 0; + +} diff --git a/swap/aib/0b9d81a3-0b67-11ec-bff2-2cf05d84bfc2.cpp b/swap/aib/0b9d81a3-0b67-11ec-bff2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..308d6c2406c4da6ed3e7321efa28f3a672319048 --- /dev/null +++ b/swap/aib/0b9d81a3-0b67-11ec-bff2-2cf05d84bfc2.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +using namespace std; +FILE *f = fopen ( "swap.in", "r" ); + +FILE *g = fopen ( "swap.out", "w" ); + + + +int N, AIB[50002], where[30]; + +char A[50002], B[50002]; + +vector < int > G[30]; + + + +void Update ( int poz ){ + + + + for ( int i = poz; i <= N; i += ( (i) & (-i) ) ) + + AIB[i] ++; + + + +} + + + +int Suma ( int N ){ + + + + int sum = 0; + + + + for ( int i = N; i > 0; i -= ( (i) & (-i) ) ) + + sum += AIB[i]; + + + + return sum; + +} + + + +int SumaInt ( int st, int dr ){ + + return dr - Suma(st-1); + +} + + + +int main(){ + + + + int i, poz, lp, ch, moves = 0; + + + + fscanf ( f, "%s%*c%s", A + 1, B + 1 ); + + + + N = strlen ( A + 1 ); + + + + for ( i = 1; i <= N; ++i ) + + G[A[i]-'a'].push_back ( i ); + + for ( i = 0; i < 'z' - 'a'; ++i ) + + G[i].resize ( G[i].size() ); + + + + + + + + for ( i = 1; i <= N; ++i ){ + + ch = B[i] - 'a'; + + + + if ( where[ch] == G[ch].size() ){ + + fprintf ( g, "-1" ); + + return 0; + + } + + + + poz = G[ch][where[ch]]; + + where[ch]++; + + + + lp = poz; + + poz += SumaInt( poz, i-1 ); + + + + Update ( lp ); + + moves += poz - i; + + } + + + + fprintf ( g, "%d", moves ); + + + + return 0; + +} diff --git a/swap/aib/0bbc9476-0b67-11ec-83ce-2cf05d84bfc2.cpp b/swap/aib/0bbc9476-0b67-11ec-83ce-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa9f2d82e1996d1c5289468dbb00be763aba59c --- /dev/null +++ b/swap/aib/0bbc9476-0b67-11ec-83ce-2cf05d84bfc2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + + + +using namespace std; + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() { + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n = 0; + while (a[n] >= 'a' && a[n] <= 'z') + ++n; + + fgets(b, 50001, stdin); + + for (i = 0;i < n;++i) + ax[ (int)a[i] ].push_back(i + 1); + + for (i = 0;i < n;++i) { + x[++nx] = ax[ b[i] ][0]; + ax[b[i]].pop_front(); + } +} + +inline void update(int x, int v) { + for (int i = x; i <= n; i += (i & -i)) + H[i] += v; +} + +inline int query(int x) { + int s = 0; + for (int i = x; i ; i -= (i & -i)) + s += H[i]; + return s; +} + +void solve() { + int i, j; + for (i = 1;i <= n;++i) + update(i, 1); + int nr = 0; + for (i = 1;i <= n;++i) { + update(x[i], -1); + nr += query(x[i]); + } + + printf("%d\n", nr); +} + +int main() { + freopen("swap.out","w",stdout); + read(); + int i; + for (i = 0;i < n;++i) + ++u1[ a[i] ], + ++u2[ b[i] ]; + + for (i = 0;i <= 255;++i) + if (u1[i] != u2[i]){ + printf("-1\n"); + return 0; + } + + solve(); + return 0; +} diff --git a/swap/aib/0bdb7faa-0b67-11ec-9e41-2cf05d84bfc2.cpp b/swap/aib/0bdb7faa-0b67-11ec-9e41-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa9f2d82e1996d1c5289468dbb00be763aba59c --- /dev/null +++ b/swap/aib/0bdb7faa-0b67-11ec-9e41-2cf05d84bfc2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + + + +using namespace std; + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() { + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n = 0; + while (a[n] >= 'a' && a[n] <= 'z') + ++n; + + fgets(b, 50001, stdin); + + for (i = 0;i < n;++i) + ax[ (int)a[i] ].push_back(i + 1); + + for (i = 0;i < n;++i) { + x[++nx] = ax[ b[i] ][0]; + ax[b[i]].pop_front(); + } +} + +inline void update(int x, int v) { + for (int i = x; i <= n; i += (i & -i)) + H[i] += v; +} + +inline int query(int x) { + int s = 0; + for (int i = x; i ; i -= (i & -i)) + s += H[i]; + return s; +} + +void solve() { + int i, j; + for (i = 1;i <= n;++i) + update(i, 1); + int nr = 0; + for (i = 1;i <= n;++i) { + update(x[i], -1); + nr += query(x[i]); + } + + printf("%d\n", nr); +} + +int main() { + freopen("swap.out","w",stdout); + read(); + int i; + for (i = 0;i < n;++i) + ++u1[ a[i] ], + ++u2[ b[i] ]; + + for (i = 0;i <= 255;++i) + if (u1[i] != u2[i]){ + printf("-1\n"); + return 0; + } + + solve(); + return 0; +} diff --git a/swap/aib/0c8e1573-0b67-11ec-a6ef-2cf05d84bfc2.cpp b/swap/aib/0c8e1573-0b67-11ec-a6ef-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..232e50512fb2c83e3da945ddb311723ac5bf0369 --- /dev/null +++ b/swap/aib/0c8e1573-0b67-11ec-a6ef-2cf05d84bfc2.cpp @@ -0,0 +1,119 @@ +#include +#include + + +using namespace std; + +char a[50010],b[50010]; + +int i,x,AIB[50010],n,c[50010]; + +int inv,nxt[50010],crt[30],lst[30]; + +int sum(int p) + +{ + + int s=0; + + int i; + + for(i=p; i; i-=(i&(-i))) + + s+=AIB[i]; + + return s; + +} + + + +void add(int p) + +{ + + int i; + + for(i=p; i<=n; i+=(i&(-i))) + + ++AIB[i]; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + + n=strlen(a); + + for(i=0; i=1; --i) + + { + + x=c[i]; + + inv+=sum(x); + + add(x); + + } + + printf("%d\n",inv); + + return 0; + +} diff --git a/swap/aib/0cae3341-0b67-11ec-8e36-2cf05d84bfc2.cpp b/swap/aib/0cae3341-0b67-11ec-8e36-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c695ba649cf6d3aeb6f1a710e77c5c87b4a79b90 --- /dev/null +++ b/swap/aib/0cae3341-0b67-11ec-8e36-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int p[Nmax] , aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/0cce9265-0b67-11ec-ac7a-2cf05d84bfc2.cpp b/swap/aib/0cce9265-0b67-11ec-ac7a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a15aed548b929420c2fc0ed4ffcffd347783e3b8 --- /dev/null +++ b/swap/aib/0cce9265-0b67-11ec-ac7a-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +deque < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin);; n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/0cee85a4-0b67-11ec-ac3a-2cf05d84bfc2.cpp b/swap/aib/0cee85a4-0b67-11ec-ac3a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..20d207b3eb885f45fcd7a435887add92d3eb95b1 --- /dev/null +++ b/swap/aib/0cee85a4-0b67-11ec-ac3a-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +deque < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> a >> b; n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + fout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/0d372623-0b67-11ec-8596-2cf05d84bfc2.cpp b/swap/aib/0d372623-0b67-11ec-8596-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8687e8e5ad7553ef21b5c7affc5f175b1917368 --- /dev/null +++ b/swap/aib/0d372623-0b67-11ec-8596-2cf05d84bfc2.cpp @@ -0,0 +1,124 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int p[Nmax] , aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin); n = strlen(a+1); + + + + for (i = 1; i <= n; ++i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = 1; i <= n; ++i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = crt[a[i]-'a']; + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + for (i = n ; i ; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/0d5c885f-0b67-11ec-9271-2cf05d84bfc2.cpp b/swap/aib/0d5c885f-0b67-11ec-9271-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48c188d7929bf20b88f792301582d1c97a59f5cb --- /dev/null +++ b/swap/aib/0d5c885f-0b67-11ec-9271-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +vector < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin); n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/0d7c6eb7-0b67-11ec-9e86-2cf05d84bfc2.cpp b/swap/aib/0d7c6eb7-0b67-11ec-9e86-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e701e2b5c087da37bc38a2d175d0ac61453957d3 --- /dev/null +++ b/swap/aib/0d7c6eb7-0b67-11ec-9e86-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +vector < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> a >> b; n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + fout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/0ea6c2d7-0b67-11ec-8b52-2cf05d84bfc2.cpp b/swap/aib/0ea6c2d7-0b67-11ec-8b52-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f71c479db4a65788d2ae7cef6f0735a550aaaf98 --- /dev/null +++ b/swap/aib/0ea6c2d7-0b67-11ec-8b52-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int NMAX=50005; + + + +int n,m,len,fra[30],frb[30],AIB[NMAX]; + +char a[NMAX],b[NMAX]; + +long long sol; + +queueQ[30]; + + + +inline int zeros(int x) + +{ + + return x^(x&(x-1)); + +} + + + +inline void Update(int poz,int val) + +{ + + for (int i=poz;i<=n;i+=zeros(i)) AIB[i]+=val; + +} + + + +inline int Query(int poz) + +{ + + int i,rez=0; + + for (i=poz;i>=1;i-=zeros(i)) rez+=AIB[i]; + + return rez; + +} + + + +int main() + +{ + + int i,j,aux,poz; + + int C; + + bool ok; + + fin>>(a+1)>>(b+1); + + n=strlen(a+1);m=strlen(b+1); + + for (i=1;i<=n;i++) fra[a[i]-'a'+1]++; + + for (i=1;i<=m;i++) frb[b[i]-'a'+1]++; + + ok=1; + + for (i=1;i<=26;i++) + + if (fra[i]!=frb[i]) + + ok=0; + + if (ok==0) {fout<<"-1";return 0;} + + for (i=1;i<=n;i++) Q[a[i]-'a'+1].push(i); + + for (i=1;i<=m;i++) + + { + + C=b[i]-'a'+1;poz=Q[C].front(); + + aux=Query(n)-Query(poz-1); + + sol+=aux+poz-i; + + Update(poz,1); + + Q[C].pop(); + + } + + fout< +#include +#include +#include +using namespace std; + + + +char s1[50010], s2[50010]; + +int aib[50010], n; + +queue poz[32]; + + + +inline void add (int x) + +{ + + for (int i = x; i <= n; i += i & (-i)) + + ++aib[i]; + +} + + + +inline int searchh (int x) + +{ + + int rez = 0; + + for (int i = x - 1; i; i -= i & (-i)) + + rez += aib[i]; + + + + return rez; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (s1, 50001, stdin); + + fgets (s2, 50001, stdin); + + + + n = strlen (s1); + + for (int i = 0; i < n; ++i) + + poz[s2[i] - 'a'].push (i + 1); + + + + int nr = 0; + + for (int i = 0; i < n; ++i) + + { + + if (!poz[s1[i] - 'a'].size ()) + + { + + printf ("-1\n"); + + return 0; + + } + + + + int x = poz[s1[i] - 'a'].front (); + + poz[s1[i] - 'a'].pop (); + + + + nr += searchh (x); + + add (x); + + } + + + + long long rez = 1LL * n * (n - 1) / 2LL; + + rez -= 1LL * nr; + + + + nr = (int) rez; + + printf ("%d\n", nr); + + + + return 0; + +} diff --git a/swap/aib/0ec6f3ee-0b67-11ec-a87e-2cf05d84bfc2.cpp b/swap/aib/0ec6f3ee-0b67-11ec-a87e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..58446a044d3cad9aa2f59bc97cf4399c053ea6b9 --- /dev/null +++ b/swap/aib/0ec6f3ee-0b67-11ec-a87e-2cf05d84bfc2.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + + + +typedef struct lnod { + + int nod; + + lnod *next; + +}*nod; + + + +int i,n,aib[50005],ar[50005],rs; + +nod lda[30]; + +string a,b; + + + +void update(int poz) { + + while(poz<=n) ++aib[poz],poz+=-poz&poz; + +} + + + +int query(int poz) { + + int aux=0; + + while(poz>=1) aux+=aib[poz],poz-=-poz&poz; + + return aux; + +} + + + +void add(int x,nod &y) { + + nod p=new lnod; + + p->nod=x; + + p->next=y; + + y=p; + +} + + + +int main() + +{ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + + + ios_base::sync_with_stdio(0); cin.tie(0); + + + + getline(cin,a); n=a.length(); + + getline(cin,b); + + + + for(i=0;i=0;--i) + + { + + nod p=lda[a[i]-'a']; + + if(!p) return cout<<"-1\n",0; + + ar[i+1]=p->nod; + + lda[a[i]-'a']=p->next; + + } + + + + for(i=1;i<=n;++i) rs+=i-1-query(ar[i]),update(ar[i]); + + + + cout< +#include +#include +#include + + + +std::ifstream in("swap.in"); +std::ofstream out("swap.out"); + +int AIB[50005]; +std::queue positions[256]; + +void update(int x, int k) { + + while (x < 50005) { + AIB[x] += k; + x += (x & (-x)); + } +} + +int query(int x) { + + int sum = 0; + + while (x) { + sum += (AIB[x]); + x -= (x & (-x)); + } + + return sum; +} + +int main() { + std::string s1, s2; + getline(in, s1); + getline(in, s2); + + for (int i = 0; i < s1.size(); i++) + { + positions[s1[i]].push(i); + update(i + 1, 1); + } + + int total = 0; + for (int i = 0; i < s2.size(); i++) { + char ch = s2[i]; + + if (positions[ch].empty()) { + out << "-1\n"; + return 0; + } + + int position = positions[ch].front(); + positions[ch].pop(); + + total += query(position); + update(position + 1, -1); + } + + out << total << "\n"; +} diff --git a/swap/aib/0f73f04c-0b67-11ec-9798-2cf05d84bfc2.cpp b/swap/aib/0f73f04c-0b67-11ec-9798-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..209ee2456eb0f262c8d3550b66ae7a1bda144779 --- /dev/null +++ b/swap/aib/0f73f04c-0b67-11ec-9798-2cf05d84bfc2.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + + + + + + + +using namespace std; +FILE *fin=freopen("swap.in","r",stdin); +FILE *fout=freopen("swap.out","w",stdout); + +vector pos[26 + 1]; +int p[26 + 1], n, AIB[50050]; +char S1[50050], S2[50050]; + +inline void Add(int pos, int val) +{ + int i; + for(i = pos; i <= n; i += ((i ^ (i - 1)) & i)) + AIB[i] += val; +} + +inline int Query(int pos) +{ + int i, ret = 0; + for(i = pos; i; i -= ((i ^ (i - 1)) & i)) + ret += AIB[i]; + return ret; +} + +void Set() +{ + fgets(S1 , 50001, stdin); + fgets(S2 , 50001, stdin); + n = strlen(S1 + 1); + for(int i = 1; i <= n; ++i) + { + pos[ S1[i] - 'a' ].push_back( i ); + } +} + +void Solve() +{ + int ans = 0, place; + int i, l; + + for(i = 1; i <= n; ++i) + { + l = S2[i] - 'a'; + + if( p[l] >= pos[ l ].size() ) + { + printf("-1"); return; + } + place = pos[ l ][ p[l] ] + Query( n ) - Query( pos[ l ][ p[l] ] ); + ans += abs( i - place ); + Add( pos[ l ][ p[l] ], 1 ); + ++p[l]; + } + printf("%d", ans); +} + +int main() +{ + Set(); + Solve(); + return 0; +} diff --git a/swap/aib/0f94e6cb-0b67-11ec-9b2b-2cf05d84bfc2.cpp b/swap/aib/0f94e6cb-0b67-11ec-9b2b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e3adbcf63d719a86340a9233a520d38ca029362 --- /dev/null +++ b/swap/aib/0f94e6cb-0b67-11ec-9b2b-2cf05d84bfc2.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; + +listL[27]; + +long long n,i,j,b,l,a[50100]; + +long long nr; + +char s[50100],s1[50100]; + +FILE *f,*g; + +void upd(int p,int x){ + + for(int i=p;i<=l+1;i+=(i&(-i))){ + + a[i]+=x; + + } + +} + +int query(int p){ + + int s=0; + + for(int i=p;i>0;i-=(i&(-i))){ + + s+=a[i]; + + } + + return s; + +} + +int main(){ + + f=fopen("swap.in","r"); + + g=fopen("swap.out","w"); + + fscanf(f,"%s%s",s,s1); + + l=strlen(s); + + for(i=0;i +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,m; + +int fr1[29],fr2[29],v[50011],aib[50011]; + +char A[50011],B[50011]; + + + +vector L[36]; + +vector::iterator it[36]; + + + +int query(int p){ + + int r=0; + + for(;p;p-=(p&(-p))) r+=aib[p]; + + return r; + +} + + + +void update(int p){ + + for(;p<=n;p+=(p&(-p))) aib[p]++; + +} + + + +int main(void){ + + register int i,j; + + + + + + f>>B+1>>A+1; + + n=strlen(A+1),m=strlen(B+1); + + if(n!=m){ + + g<<"-1"; + + f.close(); + + g.close(); + + return 0; + + } + + for(i=1;i<=n;i++) fr1[A[i]-'a']++,L[A[i]-'a'].push_back(i); + + for(i=0;i<=27;i++) it[i]=L[i].begin(); + + for(i=1;i<=m;i++){ + + fr2[B[i]-'a']++; + + v[i]=*it[B[i]-'a']; + + it[B[i]-'a']++; + + } + + for(i=0;i<=26;i++) + + if(fr1[i]!=fr2[i]){ + + g<<"-1"; + + f.close(); + + g.close(); + + return 0; + + } + + long long sol=0; + + for(i=1;i<=n;i++){ + + sol-=query(v[i]); + + update(v[i]); + + } + + sol+=1LL*n*(n-1)/2; + + g< +#include + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int n, i, lga, lgb, s, ok; + +int qqa[303], qqb[303], order[50050], position[303][50050], aib[50050]; + +char a[50050]; + +char b[50050]; + +int p[50050]; + + + +void tree_update(int value, int pos) + +{ + + int j; + + for(j=pos; j<=n; j+=(j&(-j))) + + aib[j]+=value; + +} + + + +int tree_query(int position) + +{ + + int s=0; + + for(i=position; i>=1; i-=(i&(-i))) s+=aib[i]; + + return s; + +} + + + +int main() + +{ + + f.getline(a, 50050); + + f.getline(b, 50050); + + + + lga=strlen(a); + + for(i=0;i=1; --j) + + { + + s+=tree_query(p[j]); + + tree_update(1, p[j]); + + } + + g< +#include +#include +#include + + + +std::ifstream in("swap.in"); +std::ofstream out("swap.out"); + +int AIB[50005]; +std::queue positions[256]; + +void update(int x, int k) { + + while (x < 50005) { + AIB[x] += k; + x += (x & (-x)); + } +} + +int query(int x) { + + int sum = 0; + + while (x) { + sum += (AIB[x]); + x -= (x & (-x)); + } + + return sum; +} + +int main() { + std::string s1, s2; + getline(in, s1); + getline(in, s2); + + for (int i = 0; i < s1.size(); i++) + { + positions[s1[i]].push(i); + update(i + 1, 1); + } + + int total = 0; + for (int i = 0; i < s2.size(); i++) { + char ch = s2[i]; + + if (positions[ch].empty()) { + out << "-1\n"; + return 0; + } + + int position = positions[ch].front(); + positions[ch].pop(); + + total += query(position); + update(position + 1, -1); + } + + out << total << "\n"; +} diff --git a/swap/aib/1034d1c3-0b67-11ec-b729-2cf05d84bfc2.cpp b/swap/aib/1034d1c3-0b67-11ec-b729-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc2d9e4e3d8a0fe71a4d3396eef060f049037f32 --- /dev/null +++ b/swap/aib/1034d1c3-0b67-11ec-b729-2cf05d84bfc2.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int sa[26],sb[26]; + +int x[26],lit,n,m; + +char s[50050],t[50050],r[50050]; + +int v[50050],aib[50050]; + +long long sol; + + + +int query(int poz) + +{ + + int sum=0; + + for (;poz>=1;poz-=poz&(-poz)) + + sum+=aib[poz]; + + return sum; + +} + +void update(int poz,int val) + +{ + + for (;poz<=n+2;poz+=poz&(-poz)) + + aib[poz]+=val; + +} + + + +int main() + +{ + + int i,j; + + f.getline(s,50050); + + f.getline(t,50050); + + + + memcpy(r,s,sizeof(r)); + + memset(s,0,sizeof(s)); + + memcpy(s,t,sizeof(r)); + + memset(t,0,sizeof(s)); + + memcpy(t,r,sizeof(r)); + + + + n=strlen(s); + + m=strlen(t); + + if (n!=m) { + + g<<-1<<'\n'; + + return 0; + + } + + + + + + for (i=n-1;i>=0;i--) + + sa[s[i]-'a']++; + + for (i=m-1;i>=0;i--) + + sb[t[i]-'a']++; + + for (i=0;i<26;i++) + + if (sa[i]!=sb[i]) { + + g<<-1<<'\n'; + + return 0; + + } + + for (i=0;t[i];i++) { + + lit=t[i]-'a'; + + while (s[x[lit]]!=t[i]) + + x[lit]++; + + v[i+1]=x[lit]+1; + + x[lit]++; + + } + + sol=1LL*n*(n-1)/2; + + for (i=1;i<=n;i++) { + + sol-=query(v[i]-1); + + update(v[i],1); + + } + + g< +#include +#include + + +using namespace std; + +int len,h,n; + +char ch[50005]; + +int a[50005]; + +deque dq[28]; + + + + + + + +void add(int pos,int val){ + + for(;pos<=len;pos+=pos&-pos) a[pos] += val; + +} + +int query(int pos){ + + int sum = 0; + + for(;pos;pos-=pos&-pos) sum += a[pos]; + + return sum; + +} + + + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + f >> ch; + + len = strlen(ch), h = 0; + + for(int i=0; i < len; ++i){ + + dq[ ch[i] - 'a' ] . push_back( ++h ); + + add( h , 1 ); + + } + + int sum = 0; + + n = len; + + f >> ch; + + len = strlen(ch); + + + + if( len != n ){ + + g << "-1"; + + return 0; + + } + + + + for(int i=0;i +#include +#include +#include + + + + + + +using namespace std; + +typedef int var; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +deque IND['z'-'a' + 1]; + +var n; + +var A[50001]; + + + +inline var zeros(var v) { + + return (v&(-v)); + +} + + + +void adauga(var val, var ind) { + + for(;ind<=n; ind += zeros(ind)) { + + A[ind] += val; + + } + +} + + + + + +var afla(var ind) { + + var s = 0; + + for(;ind; ind -= zeros(ind)) { + + s += A[ind]; + + } + + return s; + +} + + + +var aflaelem(var ind) { + + return afla(ind) - afla(ind-1); + +} + + + +char SIR[50001]; + +void citire() { + + var poz = 0; + + fin>>SIR; + + n = strlen(SIR); + + for(var i=0; i>SIR; + + if(strlen(SIR) != n) {fout<<-1; return;} + + for(var i=0; i +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +char c1[50001],c2[50001]; + +int n,f1[30],f2[30],i,v[50001]; + +int a[30][50001]; + +long long sol; + + + +int q(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=v[p]; + + + + return sol; + +} + + + +void u(int p){ + + for(;p<=n;p+=(p&-p)) + + v[p]++; + +} + + + +int main(){ + + fin>>c1+1>>c2+1; + + n=strlen(c1+1); + + + + for(i=1;i<=n;i++){ + + f1[ c1[i]-'a'+1 ]++; + + f2[ c2[i]-'a'+1 ]++; + + + + a[c1[i]-'a'+1][ f1[ c1[i]-'a'+1 ] ]=i; + + } + + + + for(i=1;i<=26;i++){ + + if(f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for(i=1;i<=n;i++){ + + f2[c2[i]-'a'+1]++; + + sol+=i-1-q(a[c2[i]-'a'+1][f2[c2[i]-'a'+1]]); + + u(a[c2[i]-'a'+1][f2[c2[i]-'a'+1]]); + + } + + + + fout< +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,i,lga,lgb,s,ok; + +int APa[300]; + +int APb[300]; + +int ord[50005]; + +int poz[300][50005]; + +int c[50005]; + +char a[50005]; + +char b[50005]; + +int p[50005]; + +void update(int poz, int val) + +{ + + int i; + + for(i=poz;i<=n;i+=( (-i)&i )) + + { + + c[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int i,s; + + s=0; + + for(i=poz;i>=1;i-=( (-i)&i )) + + { + + s+=c[i]; + + } + + return s; + +} + +int main() + +{ + + f.getline(a,50005); + + f.getline(b,50005); + + + + lga=strlen(a); + + for(i=0;i=1;--i) + + { + + s+=sum(p[i]); + + update(p[i],1); + + } + + g< +# include +# include + + +using namespace std; + +vector v[150]; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i,j,n,m,x,y,VV; + +int AIB[50005], poz[50005], nr[125], NEXT[125]; + +char a[50005], b[50005]; + +int suma (int k) + +{ + + int S=0; + + for (int i=k; i>=1; i-=((-i) & i)) + + S+=AIB[i]; + + return S; + +} + +void actualizare (int k) + +{ + + for (int i=k; i<=n; i+=((-i) & i)) + + ++AIB[i]; + +} + +int main () + +{ + + f.getline(a+1, 50005); n=strlen(a+1); + + for (i=1; i<=n; ++i) + + { + + ++nr[a[i]]; + + v[a[i]].push_back(i); + + } + + f.getline(b+1, 50005); m=strlen(b+1); + + if (n!=m) {g<<"-1\n"; return 0;} + + + + for (i=1; i<=n; ++i) + + { + + poz[i]=v[b[i]][NEXT[b[i]]]; + + ++NEXT[b[i]]; + + } + + + + for (i=1; i<=120; ++i) + + if (nr[i]!=NEXT[i]) {g<<"-1\n"; return 0;} + + + + for (i=n; i>=1; --i) + + { + + VV+=suma(poz[i]-1); + + actualizare (poz[i]); + + } + + g< +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,i,lga,lgb,s,ok; + +int APa[300]; + +int APb[300]; + +int ord[50005]; + +int poz[300][50005]; + +int c[50005]; + +char a[50005]; + +char b[50005]; + +int p[50005]; + +void update(int poz, int val) + +{ + + int i; + + for(i=poz;i<=n;i+=( (-i)&i )) + + { + + c[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int i,s; + + s=0; + + for(i=poz;i>=1;i-=( (-i)&i )) + + { + + s+=c[i]; + + } + + return s; + +} + +int main() + +{ + + f.getline(a,50005); + + f.getline(b,50005); + + + + lga=strlen(a); + + for(i=0;i=1;--i) + + { + + s+=sum(p[i]); + + update(p[i],1); + + } + + g< +#include + + + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +char a[nmax+2], b[nmax+2]; + +int lc[sigma]; + + + +queue q[sigma]; + +int t[nmax+1]; + + + +void bt_upd(int x, int n){ + + for (; x<=n; x= 2*x-(x&(x-1))){ + + ++t[x]; + + } + +} + + + +int bt_que(int x){ + + int sol= 0; + + for (; x>0; x&= (x-1)){ + + sol+= t[x]; + + } + + return sol; + +} + + + +int main(){ + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + int n; + + for (n= 0; n<=nmax&& a[n]>='a'&& a[n]<='z'; ++n){ + + ++lc[a[n]-'a']; --lc[b[n]-'a']; + + } + + for (int i= 0; i +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +int ft[nmax+1], f1[sigma]; + + + +char a[nmax+2], b[nmax+2]; + +queue v[sigma]; + + + +void update( int p, int n ) { + + for ( ; p<=n; p= 2*p-(p&(p-1)) ){ + + ++ft[p]; + + } + +} + + + +int query( int p ) { + + int sol= 0; + + for ( ; p>0; p&= (p-1) ) { + + sol+= ft[p]; + + } + + return sol; + +} + + + +int main( ) { + + int ans= 0; + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + + + int n; + + for ( n= 0; a[n]>='a' && a[n]<='z'; ++n ) { + + ++f1[a[n]-'a'], --f1[b[n]-'a']; + + } + + for ( int i= 0; i +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +int ft[nmax+1], f1[sigma]; + + + +char a[nmax+2], b[nmax+2]; + +queue v[sigma]; + + + +void update( int p, int n ) { + + for ( ; p<=n; p= 2*p-(p&(p-1)) ){ + + ++ft[p]; + + } + +} + + + +int query( int p ) { + + int sol= 0; + + for ( ; p>0; p&= (p-1) ) { + + sol+= ft[p]; + + } + + return sol; + +} + + + +int main( ) { + + int ans= 0; + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + + + int n; + + for ( n= 0; a[n]>='a' && a[n]<='z'; ++n ) { + + ++f1[a[n]-'a'], --f1[b[n]-'a']; + + } + + for ( int i= 0; i + + + + + + +using namespace std; + +int n, m, v[50002], aib[50002], occ[50002], ans, p[26 + 2]; + +char s1[50002], s2[50002]; + +short int sum(int x) + +{ + + int i; + + short int s = 0; + + for (int i = x; i >= 1; i -= i & (-i)) + + s += aib[i]; + + return s; + +} + +void add(int x) + +{ + + for (int i = x; i <= n; i += i & (-i)) + + ++ aib[i]; + +} + +void read() + +{ + + freopen("swap.in", "r", stdin); + + scanf("%s\n%s\n", s1 + 1, s2 + 1); + + n = strlen(s1 + 1); + +} + +void solve() + +{ + + int i; + + for (i = 1; i <= n; ++ i) + + { + + ++ occ[s1[i] - 'a']; + + -- occ[s2[i] - 'a']; + + } + + for (i = 0; i < 26; ++ i) + + if (occ[i]) + + { + + ans = -1; + + return ; + + } + + for (i = 1; i <= n; ++ i) + + { + + while (s2[p[s1[i] - 'a']] != s1[i]) + + ++ p[s1[i] - 'a']; + + v[i] = p[s1[i] - 'a'] ++; + + } + + for (i = 1; i <= n; ++ i) + + { + + ans += i - sum(v[i]) - 1; + + add(v[i]); + + } + +} + +void write() + +{ + + freopen("swap.out", "w", stdout); + + printf("%d\n", ans); + +} + +int main() + +{ + + read(); + + solve(); + + write(); + + return 0; + +} diff --git a/swap/aib/1211e689-0b67-11ec-9fad-2cf05d84bfc2.cpp b/swap/aib/1211e689-0b67-11ec-9fad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cec99beb97527e66004a778081b289d97a6a155 --- /dev/null +++ b/swap/aib/1211e689-0b67-11ec-9fad-2cf05d84bfc2.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int nmax = 50005; + +const int alpha = 30; + + + +int n, i, sol, a[nmax], aib[nmax]; + +int p[alpha], f1[alpha], f2[alpha]; + +string s, t; + + + +vector v[alpha]; + + + +void update(int x) + +{ + + for(int i = x; i <= n; i += i & (-i)) + + aib[i]++; + +} + + + +int query(int x) + +{ + + int r = 0; + + for(int i = x; i; i -= i & (-i)) + + r += aib[i]; + + return r; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> s; + + fin >> t; + + + + n = s.size(); + + + + for(i = 0; i < n; i++) + + { + + f1[s[i] - 'a']++; + + f2[t[i] - 'a']++; + + } + + + + for(i = 0; i < 26; i++) + + if(f1[i] != f2[i]) + + { + + fout << "-1\n"; + + return 0; + + } + + + + for(i = 0; i < n; i++) + + v[s[i] - 'a'].push_back(i + 1); + + + + for(i = 0; i < n; i++) + + a[i] = v[t[i] - 'a'][p[t[i] - 'a']++]; + + + + for(i = n - 1; i >= 0; i--) + + { + + sol += query(a[i] - 1); + + update(a[i]); + + } + + + + fout << sol; + + + + return 0; + +} diff --git a/swap/aib/1250c4a1-0b67-11ec-b982-2cf05d84bfc2.cpp b/swap/aib/1250c4a1-0b67-11ec-b982-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afb64cf52e1d07601df8cf489e238a1f5b5e2b81 --- /dev/null +++ b/swap/aib/1250c4a1-0b67-11ec-b982-2cf05d84bfc2.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int L[26][50010]; + +int F[26], G[26], P[50010]; + +int AIB[50010]; + +char A[50010], B[50010]; + + + +int n, i, j; + +long long sol; + + + +int query(int p) { + + int r = 0; + + for (;p;p -= (p&-p)) + + r += AIB[p]; + + return r; + +} + + + +void update(int p) { + + for (;p<=n;p += (p&-p)) + + AIB[p]++; + +} + + + +int main() { + + fin>>A+1; + + fin>>B+1; + + n = strlen(A+1); + + for (i=1;i<=n;i++) { + + A[i] -= 'a'; + + B[i] -= 'a'; + + } + + + + for (i=1;i<=n;i++) { + + F[A[i]]++; + + L[ A[i] ][ F[A[i]] ] = i; + + } + + + + for (i=1;i<=n;i++) { + + G[ B[i] ]++; + + P[ L[ B[i] ][ G[ B[i] ] ] ] = i; + + } + + + + for (i=0;i<26;i++) { + + if (F[i] != G[i]) { + + fout<<-1; + + return 0; + + } + + } + + + + for (i=1;i<=n;i++) + + P[i] = n-P[i]+1; + + + + for (i=1;i<=n;i++) { + + sol += query(P[i]); + + update(P[i]); + + } + + + + fout< +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +list v[26]; + +char a[50005],b[50005]; + +int n,ch,sol,ap1[26],ap2[26],A[50005],V[50005]; + +inline void update(int p) + +{ + + int i; + + for (i=p;i<=n;i+=(i&-i)) + + ++V[i]; + +} + +inline int query(int p) + +{ + + int i,sum=0; + + for (i=p;i>0;i-=(i&-i)) + + sum+=V[i]; + + return sum; + +} + +int main() + +{ + + int i; + + fin.getline(a+1,50005); + + fin.getline(b+1,50005); + + n=strlen(a+1); + + for (i=1;i<=n;++i) + + { + + ++ap1[a[i]-'a'], ++ap2[b[i]-'a']; + + v[a[i]-'a'].push_back(i); + + } + + for (i=0;i<26;++i) + + if (ap1[i]!=ap2[i]) + + { + + fout<<"-1\n"; + + return 0; + + } + + for (i=1;i<=n;++i) + + { + + ch=b[i]-'a'; + + A[i]=v[ch].front(); + + v[ch].pop_front(); + + } + + for (i=1;i<=n;++i) + + { + + sol+=query(A[i]-1); + + update(A[i]); + + } + + if (n&1) + + sol=((n-1)>>1)*n-sol; + + else + + sol=(n>>1)*(n-1)-sol; + + fout< +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,m; + +int fr1[29],fr2[29],v[50011],aib[50011]; + +char A[50011],B[50011]; + + + +vector L[36]; + +vector::iterator it[36]; + + + +int query(int p){ + + int r=0; + + for(;p;p-=(p&(-p))) r+=aib[p]; + + return r; + +} + + + +void update(int p){ + + for(;p<=n;p+=(p&(-p))) aib[p]++; + +} + + + +int main(void){ + + register int i,j; + + + + + + f>>B+1>>A+1; + + n=strlen(A+1),m=strlen(B+1); + + if(n!=m){ + + g<<"-1"; + + f.close(); + + g.close(); + + return 0; + + } + + for(i=1;i<=n;i++) fr1[A[i]-'a']++,L[A[i]-'a'].push_back(i); + + for(i=0;i<=27;i++) it[i]=L[i].begin(); + + for(i=1;i<=m;i++){ + + fr2[B[i]-'a']++; + + v[i]=*it[B[i]-'a']; + + it[B[i]-'a']++; + + } + + for(i=0;i<=26;i++) + + if(fr1[i]!=fr2[i]){ + + g<<"-1"; + + f.close(); + + g.close(); + + return 0; + + } + + long long sol=0; + + for(i=1;i<=n;i++){ + + sol-=query(v[i]); + + update(v[i]); + + } + + sol+=1LL*n*(n-1)/2; + + g< +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int L[26][50010]; + +int F[26], G[26], P[50010]; + +int AIB[50010]; + +char A[50010], B[50010]; + + + +int n, i, j; + +long long sol; + + + +int query(int p) { + + int r = 0; + + for (;p;p -= (p&-p)) + + r += AIB[p]; + + return r; + +} + + + +void update(int p) { + + for (;p<=n;p += (p&-p)) + + AIB[p]++; + +} + + + +int main() { + + fin>>A+1; + + fin>>B+1; + + n = strlen(A+1); + + for (i=1;i<=n;i++) { + + A[i] -= 'a'; + + B[i] -= 'a'; + + } + + + + for (i=1;i<=n;i++) { + + F[A[i]]++; + + L[ A[i] ][ F[A[i]] ] = i; + + } + + + + for (i=1;i<=n;i++) { + + G[ B[i] ]++; + + P[ L[ B[i] ][ G[ B[i] ] ] ] = i; + + } + + + + for (i=0;i<26;i++) { + + if (F[i] != G[i]) { + + fout<<-1; + + return 0; + + } + + } + + + + for (i=1;i<=n;i++) + + P[i] = n-P[i]+1; + + + + for (i=1;i<=n;i++) { + + sol += query(P[i]); + + update(P[i]); + + } + + + + fout< +#include +#include +using namespace std; + +listL[27]; + +long long n,m,i,j,l,a[50100],x,sum,w[50100]; + +char s[50100],s1[50100]; + +FILE *f,*g; + +int query(int p){ + + int s=0; + + for(int i=p;i>0;i-=(i&-i)){ + + s+=a[i]; + + } + + return s; + +} + +void update(int p,int x){ + + for(int i=p;i<=l+1;i+=(i&-i)){ + + a[i]+=x; + + } + +} + +int main(){ + + f=fopen("swap.in","r"); + + g=fopen("swap.out","w"); + + fscanf(f,"%s\n%s",s,s1); + + l=strlen(s); + + for(i=0;i +#include +#include +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +list L[26]; + + + +int n; + + + +int sol; + + + +int A[50005]; + + + +char s1[50005], s2[50005]; + + + +void update(int p) { + + for (int i=p; i<=n; i+=(i&(-i))) { + + ++A[i]; + + } + +} + + + +void query(int p) { + + for (int i=p; i>0; i-=(i&(-i))) { + + sol += A[i]; + + } + +} + + + +int main () { + + f >> s1+1; + + f >> s2+1; + + n = strlen(s1+1); + + for (int i=1; i<=n; ++i) { + + L[s1[i] - 'a'].push_back(i); + + } + + for (int i=1; i<=n; ++i) { + + if (L[s2[i]-'a'].empty()) { + + g <<-1; + + return 0; + + } + + int p = *L[s2[i] - 'a'].begin(); + + L[s2[i] - 'a'].pop_front(); + + query(p); + + update(p); + + } + + g << 1LL*n*(n-1)/2 - sol; + + return 0; + +} diff --git a/swap/aib/1329d5bb-0b67-11ec-bc9b-2cf05d84bfc2.cpp b/swap/aib/1329d5bb-0b67-11ec-bc9b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8860233aa2845d0a3f6e90718c2493dbfc755098 --- /dev/null +++ b/swap/aib/1329d5bb-0b67-11ec-bc9b-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +list l[30]; + + + +int A[50010],v[50010],i,j,n,m,rez; + + + +char a[50010],b[50010]; + + + +void query (int x) { + + + + for (int i=x;i>0;i-=(i&(-i))) + + rez+=A[i]; + + + +} + + + +void update (int x) { + + + + for (int i=x;i<=n;i+=(i&(-i))) + + A[i]++; + +} + + + + + +int main () { + + + + fin>>a; + + fin>>b; + + n=strlen(a); + + for (i=0;i +#include +#include +using namespace std; + + + +const char infile[] = "swap.in"; + +const char outfile[] = "swap.out"; + + + +ifstream fin(infile); + +ofstream fout(outfile); + + + +const int MAXN = 50005; + +const int oo = 0x3f3f3f3f; + +const int SIGMA = 26; + + + +char a[MAXN], b[MAXN]; + +int N, p[MAXN], ind[MAXN], pos[SIGMA]; + +vector fr[SIGMA]; + +int aib[MAXN]; + + + +struct classComp { + + inline bool operator () (const int &a, const int &b) const { + + if(p[a] == p[b]) + + return a > b; + + return p[a] > p[b]; + + } + +}; + + + +inline int lsb(const int &conf) { + + return conf & (-conf); + +} + + + +inline void Update(const int &Node) { + + for(int _newNode = Node ; _newNode <= N ; _newNode += lsb(_newNode)) + + ++ aib[_newNode]; + +} + + + +inline int Query(const int &Node) { + + int sum = 0; + + for(int _newNode = Node ; _newNode > 0 ; _newNode -= lsb(_newNode)) + + sum += aib[_newNode]; + + return sum; + +} + + + +int main() { + + + + + + fin >> (a + 1) >> (b + 1); + + for(N = 1 ; b[N] ; ++ N) + + fr[b[N] - 'a'].push_back(N); + + -- N; + + for(int i = 1 ; i <= N ; ++ i) { + + ind[i] = i; + + if(pos[a[i] - 'a'] >= fr[a[i]-'a'].size()) { + + fout << "-1\n"; + + return 0; + + } + + p[i] = fr[a[i] - 'a'][pos[a[i] - 'a']]; + + ++ pos[a[i] - 'a']; + + } + + sort(ind + 1, ind + N + 1, classComp()); + + int Ans = 0; + + for(int i = 1 ; i <= N ; ++ i) { + + Ans += Query(ind[i]); + + Update(ind[i]); + + } + + fout << Ans << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/aib/13fb39dc-dfb2-42a8-bd5a-b91f9bb24240.cpp b/swap/aib/13fb39dc-dfb2-42a8-bd5a-b91f9bb24240.cpp new file mode 100644 index 0000000000000000000000000000000000000000..249ed38e384a839c3fbc4d32df358625881d3888 --- /dev/null +++ b/swap/aib/13fb39dc-dfb2-42a8-bd5a-b91f9bb24240.cpp @@ -0,0 +1,98 @@ +# include +# include +# include + + +using namespace std; + +vector v[150]; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i,j,n,m,x,y,VV; + +int AIB[50005], poz[50005], nr[125], NEXT[125]; + +char a[50005], b[50005]; + +int suma (int k) + +{ + + int S=0; + + for (int i=k; i>=1; i-=((-i) & i)) + + S+=AIB[i]; + + return S; + +} + +void actualizare (int k) + +{ + + for (int i=k; i<=n; i+=((-i) & i)) + + ++AIB[i]; + +} + +int main () + +{ + + f.getline(a+1, 50005); n=strlen(a+1); + + for (i=1; i<=n; ++i) + + { + + ++nr[a[i]]; + + v[a[i]].push_back(i); + + } + + f.getline(b+1, 50005); m=strlen(b+1); + + if (n!=m) {g<<"-1\n"; return 0;} + + + + for (i=1; i<=n; ++i) + + { + + poz[i]=v[b[i]][NEXT[b[i]]]; + + ++NEXT[b[i]]; + + } + + + + for (i=1; i<=120; ++i) + + if (nr[i]!=NEXT[i]) {g<<"-1\n"; return 0;} + + + + for (i=n; i>=1; --i) + + { + + VV+=suma(poz[i]-1); + + actualizare (poz[i]); + + } + + g< +#include +using namespace std; + +int i,j,nr,n,ap[300],pr[50009][30],aib[50009],cr[50009],dr[50009]; + +char A[50009],B[50009]; + +void U(int p,int v){while(p<=n){aib[p]+=v;p+=p-(p&(p-1));}} + +int Q(int p){int s=0;while(p){s+=aib[p];p&=p-1;}return s;} + +int main() + +{ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +fgets(A, 50001, stdin); + +fgets(B, 50001, stdin); + +n=strlen(A+1); + +for(i=1;i<=n;i++) + + ap[A[i]]++; + +for(i=1;i<=n;i++) + + ap[B[i]]--; + +for(i='a';i<='z';i++) + + if(ap[i]!=0) + + { + + printf("-1\n"); + + return 0; + + } + +for(i=n;i>=1;i--) + +{ + + for(j='a';j<='z';j++) + + pr[i][j-'a']=pr[i+1][j-'a']; + + pr[i][A[i]-'a']=i; + +} + +for(i=1;i<=n;i++) + +{ + + cr[B[i]]=pr[cr[B[i]]+1][B[i]-'a']; + + dr[cr[B[i]]]=i; + +} + +for(i=1;i<=n;i++) + +{ + + nr+=dr[i]-i+(i-1-Q(dr[i])); + + U(dr[i],1); + +} + +printf("%d\n",nr); + +return 0; + +} diff --git a/swap/aib/14858f1e-0b67-11ec-9a48-2cf05d84bfc2.cpp b/swap/aib/14858f1e-0b67-11ec-9a48-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8723a86447fbdade7083c8019fa5044d181ebf8 --- /dev/null +++ b/swap/aib/14858f1e-0b67-11ec-9a48-2cf05d84bfc2.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include + +using namespace std; + +const int MAX_N = 50100; + +char fst[MAX_N]; +char lst[MAX_N]; + +int n; + +int pos['z' + 5][MAX_N]; + +int ap['z' + 10]; + +int AIB[MAX_N]; + +inline int lsb(const int x){return x & (-x);} + +void update(const int pos, const int delta){ + for(int i = pos ; i <= n ; i += lsb(i)) + AIB[i] += delta; +} + +int query(const int pos){ + int ret = 0; + for(int i = pos ; i > 0 ; i -= lsb(i)) + ret += AIB[i]; + return ret; +} + +int organize() { + + int total = 0; + + for(int i = n ; i >= 1 ; --i) { + + const int next = pos[lst[i]][pos[lst[i]][0]--]; + + const int shift = query(next); + const int actual = next - shift; + + total += i - actual; + + update(next, 1); + } + + return total; +} + +int operations() { + int i; + + for(i = 1 ; fst[i] && lst[i] ; ++i){ + + pos[fst[i]][++pos[fst[i]][0]] = i; + + ap[fst[i]]++; + ap[lst[i]]--; + } + + n = i - 1; + + bool bad = 0; + + if(fst[i] || lst[i]) + bad = 1; + + for(char it = 'a' ; it <= 'z' && !bad ; ++it) + if( ap[it] != 0 ) + bad = 1; + + + if(bad) + return -1; + + return organize(); +} + +int main() { + ifstream in("swap.in"); + freopen("swap.out", "w", stdout); + + in >> (fst+1) >> (lst+1); + + printf("%d\n", operations()); + + return 0; +} diff --git a/swap/aib/15103a75-256a-48a9-8758-8fd68a159e67.cpp b/swap/aib/15103a75-256a-48a9-8758-8fd68a159e67.cpp new file mode 100644 index 0000000000000000000000000000000000000000..209ee2456eb0f262c8d3550b66ae7a1bda144779 --- /dev/null +++ b/swap/aib/15103a75-256a-48a9-8758-8fd68a159e67.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include + + + + + + + +using namespace std; +FILE *fin=freopen("swap.in","r",stdin); +FILE *fout=freopen("swap.out","w",stdout); + +vector pos[26 + 1]; +int p[26 + 1], n, AIB[50050]; +char S1[50050], S2[50050]; + +inline void Add(int pos, int val) +{ + int i; + for(i = pos; i <= n; i += ((i ^ (i - 1)) & i)) + AIB[i] += val; +} + +inline int Query(int pos) +{ + int i, ret = 0; + for(i = pos; i; i -= ((i ^ (i - 1)) & i)) + ret += AIB[i]; + return ret; +} + +void Set() +{ + fgets(S1 , 50001, stdin); + fgets(S2 , 50001, stdin); + n = strlen(S1 + 1); + for(int i = 1; i <= n; ++i) + { + pos[ S1[i] - 'a' ].push_back( i ); + } +} + +void Solve() +{ + int ans = 0, place; + int i, l; + + for(i = 1; i <= n; ++i) + { + l = S2[i] - 'a'; + + if( p[l] >= pos[ l ].size() ) + { + printf("-1"); return; + } + place = pos[ l ][ p[l] ] + Query( n ) - Query( pos[ l ][ p[l] ] ); + ans += abs( i - place ); + Add( pos[ l ][ p[l] ], 1 ); + ++p[l]; + } + printf("%d", ans); +} + +int main() +{ + Set(); + Solve(); + return 0; +} diff --git a/swap/aib/152a8176-0b67-11ec-ab3a-2cf05d84bfc2.cpp b/swap/aib/152a8176-0b67-11ec-ab3a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c86a16466ab396965b04e7d65f0ce7d168d81947 --- /dev/null +++ b/swap/aib/152a8176-0b67-11ec-ab3a-2cf05d84bfc2.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); +ofstream g("swap.out"); + + + + +string s1,s2; +int Aib[100666],i,N; +int a[100666],b[100666]; +vector H[666]; +int fr1[666],fr2[666]; +int grad[777],result; + +int query(int pos) +{ + int res=0; + for(; pos; pos-=(pos&(pos^(pos-1)))) res+=Aib[pos]; + return res; +} + +void update(int pos,int value) +{ + for(; pos<=N; pos+=(pos&(pos^(pos-1)))) + Aib[pos]+=value; +} + +int main() +{ + f>>s1>>s2; + N=s1.length(); + + for(i=0; i +#include +#include +#include +using namespace std; + + + +const int NMAX = 50005, ALPHA = 26; + + + +int N, FreqA[ALPHA], FreqB[ALPHA], FT[NMAX], Ptr[ALPHA], Ans; + +vector Pos[ALPHA]; + +char A[NMAX], B[NMAX]; + + + +int LSB(int X) + +{ + + return ((X & (X - 1)) ^ X); + +} + + + +void Update(int Pos) + +{ + + for(; Pos <= N; Pos += LSB(Pos)) + + FT[Pos] ++; + +} + + + +int Ask(int Pos) + +{ + + int Ans = 0; + + for(; Pos; Pos -= LSB(Pos)) + + Ans += FT[Pos]; + + return Ans; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(A , 50001, stdin); + + fgets(B , 50001, stdin); + + N = strlen(A + 1); + + + + for(int i = 1; i <= N; ++ i) + + { + + FreqA[A[i] - 'a'] ++; + + FreqB[B[i] - 'a'] ++; + + Pos[A[i] - 'a'].push_back(i); + + } + + + + for(int i = 0; i < ALPHA; ++ i) + + if(FreqA[i] != FreqB[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(int i = 1; i <= N; ++ i) + + { + + int Ch = B[i] - 'a', NextPos = Pos[Ch][ Ptr[Ch] ]; + + Ptr[Ch] ++; + + + + Ans += NextPos - i + Ask(N - NextPos + 1); + + Update(N - NextPos + 1); + + } + + + + printf("%i\n", Ans); + +} diff --git a/swap/aib/155640fb-b83e-4aa3-92f3-1e30fc37f967.cpp b/swap/aib/155640fb-b83e-4aa3-92f3-1e30fc37f967.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b97e846d0ed29860b0f676059d30227cc53ab91 --- /dev/null +++ b/swap/aib/155640fb-b83e-4aa3-92f3-1e30fc37f967.cpp @@ -0,0 +1,93 @@ +#include +#include + + + + +using namespace std; + +int n; + +int aib[50010],v[50010]; + +int countA[30],countB[30],first[30][50010]; + +char a[50010],b[50010]; + +void Update(int x){ + + for(x;x<=n;x=x+((x&(x-1))^x)) + + aib[x]++; + +} + +int Query(int x){ + + int answer=0; + + for(x;x>0;x=x-((x&(x-1))^x)) + + answer+=aib[x]; + + return answer; + +} + +int main(){ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i; + + long long answer=0; + + scanf("%s%s",a,b); + + n=strlen(a); + + for(i=0;i=0;i--){ + + answer+=Query(v[i]); + + Update(v[i]); + + } + + printf("%lld",answer); + + return 0; + +} diff --git a/swap/aib/158a1fc7-0b67-11ec-be53-2cf05d84bfc2.cpp b/swap/aib/158a1fc7-0b67-11ec-be53-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d5f66874a7f5300eec960aabac8772f3426db6d --- /dev/null +++ b/swap/aib/158a1fc7-0b67-11ec-be53-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + + + +char s[50100], sfin[50100]; + +int ind[26], perm[50100], AIB[50100]; + + + +inline int lsb(int X) + +{ + + return X & -X; + +} + + + +int query(int x) + +{ + + int cnt = 0; + + + + while (x) + + { + + cnt += AIB[x]; + + x = x - lsb(x); + + } + + + + return cnt; + +} + + + +void update(int x, int N) + +{ + + while (x <= N) + + { + + AIB[x] ++; + + x += lsb(x); + + } + +} + + + +int main() + +{ + + int i, N; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s , 50001, stdin); fgets(sfin , 50001, stdin); + + N = strlen(s + 1); + + + + for (i = 1; i <= N; i ++) + + { + + int now = s[i] - 'a'; + + while (ind[now] <= N) + + { + + ++ ind[now]; + + if (sfin[ind[now]] == s[i]) + + break; + + } + + if (ind[now] == N + 1) + + { + + printf("-1"); + + return 0; + + } + + perm[i] = ind[now]; + + } + + + + int sol = 0; + + for (i = 1; i <= N; i ++) + + { + + sol += i - 1 - query(perm[i]); + + update(perm[i], N); + + } + + + + printf("%d", sol); + + return 0; + +} diff --git a/swap/aib/15aafc8e-0b67-11ec-9386-2cf05d84bfc2.cpp b/swap/aib/15aafc8e-0b67-11ec-9386-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61c48c7646a4dfce0e3f4ae3a99bd162d914ce5a --- /dev/null +++ b/swap/aib/15aafc8e-0b67-11ec-9386-2cf05d84bfc2.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int N,AIB[50100],Sol; + +char S[50100],S2[50100]; + +queue Index[26]; + + + +int Query(int nod) { + + + + int Rez=0; + + + + while(nod>0) { + + Rez+=AIB[nod]; + + nod-=(nod&-nod); + + } + + + + return Rez; + + + +} + +void Update(int nod) { + + + + while(nod<=N) { + + AIB[nod]++; + + nod+=(nod&-nod); + + } + + + +} + +void Afis() { + + + + ofstream out("swap.out"); + + out< +#include +#include + + +using namespace std; + + + +int cont[27], cont2[27]; + +int P[50005]; + +int M[27][50005]; + +int N; + + + +int BIT[50005]; + + + +string a, b; + + + +inline int lsb( int x ) + +{ + + return ( x & ( -x ) ); + +} + + + +void update( int x, int val ) + +{ + + for ( int i = x; i <= N; i += lsb( i ) ) + + BIT[i] += val; + +} + + + +int query( int x ) + +{ + + int s = 0; + + + + for ( int i = x; i >= 1; i -= lsb( i ) ) + + s += BIT[i]; + + + + return s; + +} + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline( f, a ); + + getline( f, b ); + + + + N = a.size(); + + + + for ( int i = 0; i < N; ++i ) + + { + + M[ b[i] - 97 ][ ++cont2[ b[i] - 97 ] ] = i + 1; + + } + + + + for ( int i = 0; i < N; ++i ) + + { + + ++cont[ a[i] - 97 ]; + + + + P[i + 1] = M[ a[i] - 97 ][ cont[ a[i] - 97 ] ]; + + } + + + + int sol = 0; + + + + for ( int i = 1; i <= N; ++i ) + + { + + if ( P[i] == 0 ) + + { + + g << "-1\n"; + + return 0; + + } + + } + + + + for ( int i = 1; i <= N; ++i ) + + { + + sol += i - 1 - query( P[i] - 1 ); + + update( P[i], 1 ); + + } + + + + g << sol << "\n"; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/swap/aib/16412734-7341-4afd-acfe-d06606b21333.cpp b/swap/aib/16412734-7341-4afd-acfe-d06606b21333.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ca33949992d1810698f2a16d410de36abb8c532 --- /dev/null +++ b/swap/aib/16412734-7341-4afd-acfe-d06606b21333.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char c1[50002],c2[50002]; + +bool wtaken[50002],ok[50002],good=1; + +dequev[30]; + +int AIB[50002],L,sol; + +int fr1[30],fr2[30]; + +void intro() + +{ + + f>>c1+1; + + f>>c2+1; + + L=strlen(c1+1); + + for(int i=1;i<=L;++i){ + + fr1[c1[i]-'a']++,fr2[c2[i]-'a']++; + + v[c1[i]-'a'].push_back(i); + + } + + for(int i=0;i<=25;++i) + + if(fr1[i]!=fr2[i]) + + good=0; + +} + +void Add(int nod) + +{ + + for(;nod<=L;nod+=(nod&(-nod))) + + AIB[nod]++; + +} + +int compute(int nod) + +{ + + int s=0; + + for(;nod;nod-=(nod&(-nod))) + + s+=AIB[nod]; + + return s; + +} + +void finito() + +{ + + for(int i=1;i<=L;++i) + + { + + sol+=v[c2[i]-'a'][0]+compute(L-v[c2[i]-'a'][0]+1)-i; + + + + Add(L-v[c2[i]-'a'][0]+1); + + v[c2[i]-'a'].pop_front(); + + } + + g< +#include +#include +#include + + +using namespace std; + +deque poz[100]; + +char unu[50010],doi[50010]; + +unsigned short int n,a[50010],aib[50010],funu[30],fdoi[30]; + +void update (unsigned short int pos, unsigned short int val) + +{ + + for(;pos<=n && pos;pos=pos+(pos&-pos)) + + aib[pos]+=val; + +} + +int query (unsigned short int pos) + +{ + + int ans=0; + + for(;pos;pos=pos-(pos&-pos)) + + ans+=aib[pos]; + + return ans; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i,nswap; + + fgets(unu, 50001, stdin); fgets(doi, 50001, stdin); + + n=strlen(doi); + + for(i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in ("swap.in"); + +ofstream out ("swap.out"); + + + +const int MAXN = 50010; + + + +int N; + +char S1[MAXN], S2[MAXN]; + +int V[MAXN], AIB[MAXN]; + +vector Ap1[30], Ap2[30]; + +int A1[30], A2[30]; + + + +inline int lsb (const int &x) + +{ + + return x & (-x); + +} + + + +inline void update (int poz) + +{ + + for ( ; poz <= N; poz += lsb (poz)) + + AIB[poz] ++; + +} + + + +inline int query (int poz) + +{ + + int ret = 0; + + + + for ( ; poz; poz -= lsb (poz)) + + ret += AIB[poz]; + + + + return ret; + +} + + + +int main() + +{ + + int i, j, Ans = 0; + + + + in >> (S1 + 1) >> (S2 + 1); + + N = strlen (S1 + 1); + + + + for (i = 1; i <= N; i ++){ + + Ap1[S1[i] - 'a'].push_back (i); + + Ap2[S2[i] - 'a'].push_back (i); + + ++ A1[S1[i] - 'a']; + + ++ A2[S2[i] - 'a']; + + } + + + + for (i = 0; i < 26; i ++) + + if (A1[i] != A2[i]){ + + out << -1; + + + + return 0; + + } + + + + for (i = 0; i < 26; i ++) + + for (j = 0; j < (int) Ap1[i].size (); j ++) + + V[Ap1[i][j]] = Ap2[i][j]; + + + + for (i = N; i; i --){ + + Ans += query (V[i]); + + update (V[i]); + + } + + + + out << Ans; + + + + return 0; + +} diff --git a/swap/aib/16a0656c-0b67-11ec-938e-2cf05d84bfc2.cpp b/swap/aib/16a0656c-0b67-11ec-938e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04032349a2225454b2d947a504f3ab357b7b3ee7 --- /dev/null +++ b/swap/aib/16a0656c-0b67-11ec-938e-2cf05d84bfc2.cpp @@ -0,0 +1,60 @@ +#include +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int sigma= 26; +const int nmax= 50000; + +char a[nmax+2], b[nmax+2]; +int lc[sigma]; + +queue q[sigma]; +int t[nmax+1]; + +void bt_upd(int x, int n){ + for (; x<=n; x= 2*x-(x&(x-1))){ + ++t[x]; + } +} + +int bt_que(int x){ + int sol= 0; + for (; x>0; x&= (x-1)){ + sol+= t[x]; + } + return sol; +} + +int main(){ + fin.getline(a, nmax+2); + fin.getline(b, nmax+2); + int n; + for (n= 0; n<=nmax&& a[n]>='a'&& a[n]<='z'; ++n){ + ++lc[a[n]-'a']; --lc[b[n]-'a']; + } + for (int i= 0; i +#include +#include +#include +using namespace std; + + + +const int NMAX = 50003; + + + +queue q[26]; + +int N, aib[NMAX]; + +char s[NMAX]; + + + +inline void AIB_build () { + + + + int i, j; + + for (i = 1; i <= N; ++i) + + for (j = i; j <= N; j += j & -j) + + ++aib[j]; + + + +} + + + +inline void AIB_update (int k) { + + + + for (; k <= N; k += k & -k) + + --aib[k]; + + + +} + + + +inline int AIB_query (int k) { + + + + int r = 0; + + for (; k; k -= k & -k) + + r += aib[k]; + + return r; + + + +} + + + +int main () { + + + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + int i, r = 0; + + scanf ("%s", s + 1); + + N = strlen (s + 1); + + for (i = 1; i <= N; ++i) + + q[s[i] - 'a'].push (i); + + scanf ("%s", s + 1); + + if (strlen (s + 1) != N) + + printf ("-1\n"), + + exit (EXIT_SUCCESS); + + AIB_build (); + + for (i = 1; i <= N; ++i) { + + if (q[s[i] - 'a'].empty ()) + + printf ("-1\n"), + + exit (EXIT_SUCCESS); + + r += AIB_query (q[s[i] - 'a'].front ()) - 1; + + AIB_update (q[s[i] - 'a'].front ()); + + q[s[i] - 'a'].pop (); + + } + + printf ("%d", r); + + + +} diff --git a/swap/aib/1746f913-0b67-11ec-a83e-2cf05d84bfc2.cpp b/swap/aib/1746f913-0b67-11ec-a83e-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2ca6158874dc16a3dbe3f8ea4bc6f6471a6ef99a --- /dev/null +++ b/swap/aib/1746f913-0b67-11ec-a83e-2cf05d84bfc2.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +using namespace std; + + + +int N, aib[60000]; +string s1, s2; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +void insert_aib(int poz, int elem) { + while (poz <= N) { + aib[poz] += elem; + poz += poz & (-poz); + } +} + +int query (int poz) { + int sum = 0; + while (poz > 0) { + sum += aib[poz]; + poz -= poz & (-poz); + } + return sum; +} + +void doSol(int perm[60000]) { + int rez = 0; + + for (int i = 1; i <= N; ++i) { + rez += query (N) - query (perm[i]); + insert_aib(perm[i], 1); + } + fout << rez; +} + +int sol[30][60000], curPoz[30]; +int perm[60000]; + +int main() { + getline(fin, s1); + getline(fin, s2); + + N = s1.size(); + + for (int i = 0; i < s2.size(); ++i) { + int v = s2[i] - 'a'; + sol[v][++sol[v][0]] = i + 1; + } + + for (int i = 0; i < s1.size(); ++i) { + int v = s1[i] - 'a'; + perm[i + 1] = sol[v][++curPoz[v]]; + } + + + for (int i = 0; i < 30; ++i) + if (curPoz[i] != sol[i][0]) { + fout << -1; + return 0; + } + + doSol (perm); + + return 0; +} diff --git a/swap/aib/17667210-0b67-11ec-b4f8-2cf05d84bfc2.cpp b/swap/aib/17667210-0b67-11ec-b4f8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..effaf71ccf5e6243d1cbeb78f5c37ec37ee4e351 --- /dev/null +++ b/swap/aib/17667210-0b67-11ec-b4f8-2cf05d84bfc2.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +using namespace std; + + + +char s[50005], S[50005]; + +int lgt, sol, aib[50005], P[50005], poz[26]; + +vector v[26]; + + + +inline int abs(int a) + +{ + + if(a < 0) return -a; + + return a; + +} + + + +int query(int poz) + +{ + + int sol = 0; + + for(int x = poz; x; x -= (x & (-x))) + + sol += aib[x]; + + return sol; + +} + + + +void update(int poz) + +{ + + for(int x = poz; x <= lgt; x += (x & (-x))) + + aib[x]++; + +} + + + +int main() + +{ + + ifstream in("swap.in"); in.getline(s + 1, 50005); in.getline(S + 1, 50005); in.close(); + + lgt = strlen(s + 1); + + for(int i = 1; i <= lgt; i++) v[s[i] - 'a'].push_back(i); + + ofstream out("swap.out"); + + for(int i = 1; i <= lgt; i++) + + { + + if(v[S[i] - 'a'].size() != poz[S[i] - 'a']) + + P[v[S[i] - 'a'][poz[S[i] - 'a']++]] = i; + + else + + { + + out<<"-1"; + + out.close(); + + return 0; + + } + + } + + for(int i = 1; i <= lgt; i++) + + { + + sol += query(lgt) - query(P[i]); + + update(P[i]); + + } + + out< +#include +#include +#include +using std::multimap; +using std::make_pair; +using std::sort; + + +int t[50005]; + +void u (int i,int v) +{ + while(i<50005) + t[i]+=v,i+=(i&-i); +} + +int g (int i) +{ + int s=0; + while(i) + s+=t[i],i-=(i&-i); + return s; +} + +int o[50005]; +char a[50005]; +char b[50005]; + +multimap m; + +long long r=0; + +int main (void) +{ + freopen ("swap.in","r",stdin); + + + + + fgets (a, 50001, stdin); + fgets (b, 50001, stdin); + + int n=strlen (a); + for(int i=0;i::iterator it=m.lower_bound (a[i]); + if(a[i]!=it->first){ + printf ("-1"); + return 0; + } + o[i]=it->second; + m.erase (it); + } + + for(int i=0;i +#include + + + + +char s[50100], sfin[50100]; + +int ind[26], perm[50100], AIB[50100]; + + + +inline int lsb(int X) + +{ + + return X & -X; + +} + + + +int query(int x) + +{ + + int cnt = 0; + + + + while (x) + + { + + cnt += AIB[x]; + + x = x - lsb(x); + + } + + + + return cnt; + +} + + + +void update(int x, int N) + +{ + + while (x <= N) + + { + + AIB[x] ++; + + x += lsb(x); + + } + +} + + + +int main() + +{ + + int i, N; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50001, stdin); fgets(sfin, 50001, stdin); + + N = strlen(s + 1); + + + + for (i = 1; i <= N; i ++) + + { + + int now = s[i] - 'a'; + + while (ind[now] <= N) + + { + + ++ ind[now]; + + if (sfin[ind[now]] == s[i]) + + break; + + } + + if (ind[now] == N + 1) + + { + + printf("-1"); + + return 0; + + } + + perm[i] = ind[now]; + + } + + + + int sol = 0; + + for (i = 1; i <= N; i ++) + + { + + sol += i - 1 - query(perm[i]); + + update(perm[i], N); + + } + + + + printf("%d", sol); + + return 0; + +} diff --git a/swap/aib/17cd73b0-0b67-11ec-9ff4-2cf05d84bfc2.cpp b/swap/aib/17cd73b0-0b67-11ec-9ff4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec51c6c080fa5ed1cca96242b03f939742fbc33a --- /dev/null +++ b/swap/aib/17cd73b0-0b67-11ec-9ff4-2cf05d84bfc2.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, fr1[30], fr2[30], ANS, W[50010], AIB[50010]; + + + +struct nr + +{ + + int P, V; + +} VV[50010]; + + + +char A[50010], B[50010]; + + + +bool cmp(nr a, nr b) + +{ + + return a.V > b.V; + +} + + + +void Update(int P) + +{ + + for(; P <= N; P += (P & (-P))) + + AIB[P] ++; + +} + + + +int Query(int P) + +{ + + int V = 0; + + for(; P; P -= (P & (-P))) + + V += AIB[P]; + + return V; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(A, 50001, stdin); + + fgets(B, 50001, stdin); + + N = strlen(A); + + + + for(i = 0; i < N; i++) + + { + + fr1[A[i] - 'a'] ++; + + fr2[B[i] - 'a'] ++; + + V[B[i] - 'a'].push_back(i + 1); + + W[i] = fr1[A[i] - 'a']; + + } + + + + for(i = 0; i < 30; i++) + + if(fr1[i] != fr2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(i = 0; i < N; i++) + + { + + VV[i].P = i + 1; + + VV[i].V = V[A[i] - 'a'][W[i] - 1]; + + } + + + + sort(VV, VV + N, cmp); + + + + for(i = 0; i < N; i++) + + { + + ANS += Query(VV[i].P); + + Update(VV[i].P); + + } + + + + printf("%i\n", ANS); + + return 0; + +} diff --git a/swap/aib/17efed76-0b67-11ec-b258-2cf05d84bfc2.cpp b/swap/aib/17efed76-0b67-11ec-b258-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2779a0089c0beebcb151611de5b37f68574d2ab --- /dev/null +++ b/swap/aib/17efed76-0b67-11ec-b258-2cf05d84bfc2.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + + + +string s,t; + +vector v[30]; + +int a[50001],aib[50001],i,n; + +long long sum; + + + +void update(int k,int val) + +{ + + int i; + + for (i=k;i<=n;i=(i | (i-1))+1) + + aib[i]+=val; + +} + + + +int query(int k) + +{ + + int i,s=0; + + for (i=k;i>0;i=i & (i-1)) + + s+=aib[i]; + + return s; + +} + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f >> s >> t; + + n=s.length(); + + for (i=1;i<=26;i++) + + v[i].push_back(0); + + for (i=0;i=1;i--) + + { + + sum+=query(a[i]); + + update(a[i],1); + + } + + g << sum; + + return 0; + +} diff --git a/swap/aib/180fd3c6-0b67-11ec-b4ee-2cf05d84bfc2.cpp b/swap/aib/180fd3c6-0b67-11ec-b4ee-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef8dc0912bb798c01584e356a91541cca4b2f997 --- /dev/null +++ b/swap/aib/180fd3c6-0b67-11ec-b4ee-2cf05d84bfc2.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + + + + + char A[55005],B[55005]; + vector Poz[30]; + int T[55005],Fi[55005],Nr[55005]; + int Aib[55005]; + int rez; + int i,n; + +void add ( int ind ){ + for ( ; ind<=n; ind+=((ind)&(-ind)) ) + Aib[ind]+=1; +} +int query ( int ind ){ + int rez=0; + for ( ; ind; ind-=((ind)&(-ind)) ){ + rez+=Aib[ind]; + } + return rez; +} + +int main(){ + + freopen ("swap.in","r",stdin); + freopen ("swap.out","w",stdout); + + scanf ("%s", &A ); + scanf ("%s", &B ); + + n=strlen(A); + for ( i=0; i Fi[ B[i]-'a' ] ){ + printf("-1\n"); + return 0; + } + T[ i ] = Poz[ B[i]-'a' ][ Nr[ B[i]-'a'] ]; + T[i]++; + Nr[ B[i]-'a' ]++; + } + + for ( i=n-1; i>=0; i-- ){ + rez += query ( T[i] ); + add ( T[i] ); + } + printf("%d\n",rez); + return 0; +} diff --git a/swap/aib/187876a2-0b67-11ec-a447-2cf05d84bfc2.cpp b/swap/aib/187876a2-0b67-11ec-a447-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b83b2dec0886852cde41853a151ee55a9f2477c9 --- /dev/null +++ b/swap/aib/187876a2-0b67-11ec-a447-2cf05d84bfc2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + + +using namespace std; + + +string a,b; +vector se[26]; + +int n,s[50005],aib[50005],ind[50005],rez,poz[26]; + +bool cmp(const int &a,const int &b) { + if(s[a]==s[b]) return a>b; + return s[a]>s[b]; +} + +int lsb(int n) { + return (n^(n-1))&n; +} + +void ins(int x) { + for(int i=x; i<=n;i+=lsb(i)) ++aib[i]; +} + +int query(int x) { + int ret=0; + for(int i=x; i>0; i-=lsb(i)) ret+=aib[i]; + return ret; +} + + +int main() + +{ + + ifstream f("swap.in"); + ofstream g("swap.out"); + f>>a>>b; + for(int i=0; i=se[a[i]-'a'].size()) { + g<<"-1"; + return 0; + } + s[i+1]=se[a[i]-'a'][poz[a[i]-'a']]; + ++poz[a[i]-'a']; + } + n=a.size(); + sort(ind+1,ind+n+1,cmp); + for(int i=1; i<=n; ++i) { + rez+=query(ind[i]); + ins(ind[i]); + } + g< +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, fr1[30], fr2[30], ANS, W[50010], AIB[50010]; + + + +struct nr + +{ + + int P, V; + +} VV[50010]; + + + +char A[50010], B[50010]; + + + +bool cmp(nr a, nr b) + +{ + + return a.V > b.V; + +} + + + +void Update(int P) + +{ + + for(; P <= N; P += (P & (-P))) + + AIB[P] ++; + +} + + + +int Query(int P) + +{ + + int V = 0; + + for(; P; P -= (P & (-P))) + + V += AIB[P]; + + return V; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(A, 50001, stdin); + + fgets(B, 50001, stdin); + + N = strlen(A); + + + + for(i = 0; i < N; i++) + + { + + fr1[A[i] - 'a'] ++; + + fr2[B[i] - 'a'] ++; + + V[B[i] - 'a'].push_back(i + 1); + + W[i] = fr1[A[i] - 'a']; + + } + + + + for(i = 0; i < 30; i++) + + if(fr1[i] != fr2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(i = 0; i < N; i++) + + { + + VV[i].P = i + 1; + + VV[i].V = V[A[i] - 'a'][W[i] - 1]; + + } + + + + sort(VV, VV + N, cmp); + + + + for(i = 0; i < N; i++) + + { + + ANS += Query(VV[i].P); + + Update(VV[i].P); + + } + + + + printf("%i\n", ANS); + + return 0; + +} diff --git a/swap/aib/18a4dfa5-d71d-4dc4-b957-27a54205ea18.cpp b/swap/aib/18a4dfa5-d71d-4dc4-b957-27a54205ea18.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45836601e2e3585b10bba92a4316fe2c49d849d7 --- /dev/null +++ b/swap/aib/18a4dfa5-d71d-4dc4-b957-27a54205ea18.cpp @@ -0,0 +1,300 @@ +#include +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + + + aib[i] = 0; + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 -= p2 & (-p2); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/18b78b20-0b67-11ec-bed2-2cf05d84bfc2.cpp b/swap/aib/18b78b20-0b67-11ec-bed2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed138749398ca5aed7289aa4e79c79e5a1027157 --- /dev/null +++ b/swap/aib/18b78b20-0b67-11ec-bed2-2cf05d84bfc2.cpp @@ -0,0 +1,130 @@ +#include +#include +#include #include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, ap1[30], ap2[30], ans, W[50010], Aib[50010]; + +struct data + +{ + + int pos, val; + +}VV[50010]; + +char S1[50010], S2[50010]; + + + +bool cmp(data one, data two) + +{ + + return one.val > two.val; + +} + + + +void Update(int pos) + +{ + + for(; pos <= N; pos += (pos & (-pos))) + + Aib[pos] ++; + +} + + + +int Query(int pos) + +{ + + int rez = 0; + + for(; pos; pos -= (pos & (-pos))) + + rez += Aib[pos]; + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + N = strlen(S1); + + for(i = 0; i < N; i++) + + { + + ap1[S1[i] - 'a'] ++; + + ap2[S2[i] - 'a'] ++; + + V[S2[i] - 'a'].push_back(i + 1); + + W[i] = ap1[S1[i] - 'a']; + + } + + for(i = 0; i < 30; i++) + + if(ap1[i] != ap2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + for(i = 0; i < N; i++) + + { + + VV[i].pos = i + 1; + + VV[i].val = V[S1[i] - 'a'][W[i] - 1]; + + } + + sort(VV, VV + N, cmp); + + for(i = 0; i < N; i++) + + { + + ans += Query(VV[i].pos); + + Update(VV[i].pos); + + } + + printf("%i\n", ans); + + return 0; + +} diff --git a/swap/aib/18d78bae-0b67-11ec-8b0c-2cf05d84bfc2.cpp b/swap/aib/18d78bae-0b67-11ec-8b0c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b83b2dec0886852cde41853a151ee55a9f2477c9 --- /dev/null +++ b/swap/aib/18d78bae-0b67-11ec-8b0c-2cf05d84bfc2.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + + +using namespace std; + + +string a,b; +vector se[26]; + +int n,s[50005],aib[50005],ind[50005],rez,poz[26]; + +bool cmp(const int &a,const int &b) { + if(s[a]==s[b]) return a>b; + return s[a]>s[b]; +} + +int lsb(int n) { + return (n^(n-1))&n; +} + +void ins(int x) { + for(int i=x; i<=n;i+=lsb(i)) ++aib[i]; +} + +int query(int x) { + int ret=0; + for(int i=x; i>0; i-=lsb(i)) ret+=aib[i]; + return ret; +} + + +int main() + +{ + + ifstream f("swap.in"); + ofstream g("swap.out"); + f>>a>>b; + for(int i=0; i=se[a[i]-'a'].size()) { + g<<"-1"; + return 0; + } + s[i+1]=se[a[i]-'a'][poz[a[i]-'a']]; + ++poz[a[i]-'a']; + } + n=a.size(); + sort(ind+1,ind+n+1,cmp); + for(int i=1; i<=n; ++i) { + rez+=query(ind[i]); + ins(ind[i]); + } + g< + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int bit[50005]; + +void update(int pozitie,int val) + +{ + + while(pozitie<50005) + + { + + bit[pozitie]+=val; + + pozitie=(pozitie|(pozitie+1)); + + } + +} + +int query(int pozitie) + +{ + + int ans=0; + + while(pozitie>=0) + + { + + ans=ans+bit[pozitie]; + + pozitie=(pozitie&(pozitie+1))-1; + + } + + return ans; + +} + +int main() + +{ + + string s1,s2; + + deque pozitie[26]; + + f>>s1>>s2; + + int mutari=0,i; + + for(i=0; i +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, ap1[30], ap2[30], ans, W[50010], Aib[50010]; + +struct data + +{ + + int pos, val; + +}VV[50010]; + +char S1[50010], S2[50010]; + + + +bool cmp(data one, data two) + +{ + + return one.val > two.val; + +} + + + +void Update(int pos) + +{ + + for(; pos <= N; pos += (pos & (-pos))) + + Aib[pos] ++; + +} + + + +int Query(int pos) + +{ + + int rez = 0; + + for(; pos; pos -= (pos & (-pos))) + + rez += Aib[pos]; + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + N = strlen(S1); + + for(i = 0; i < N; i++) + + { + + ap1[S1[i] - 'a'] ++; + + ap2[S2[i] - 'a'] ++; + + V[S2[i] - 'a'].push_back(i + 1); + + W[i] = ap1[S1[i] - 'a']; + + } + + for(i = 0; i < 30; i++) + + if(ap1[i] != ap2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + for(i = 0; i < N; i++) + + { + + VV[i].pos = i + 1; + + VV[i].val = V[S1[i] - 'a'][W[i] - 1]; + + } + + sort(VV, VV + N, cmp); + + for(i = 0; i < N; i++) + + { + + ans += Query(VV[i].pos); + + Update(VV[i].pos); + + } + + printf("%i\n", ans); + + return 0; + +} diff --git a/swap/aib/191733e3-0327-429e-a454-c4aa5df006c5.cpp b/swap/aib/191733e3-0327-429e-a454-c4aa5df006c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0e3adbcf63d719a86340a9233a520d38ca029362 --- /dev/null +++ b/swap/aib/191733e3-0327-429e-a454-c4aa5df006c5.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; + +listL[27]; + +long long n,i,j,b,l,a[50100]; + +long long nr; + +char s[50100],s1[50100]; + +FILE *f,*g; + +void upd(int p,int x){ + + for(int i=p;i<=l+1;i+=(i&(-i))){ + + a[i]+=x; + + } + +} + +int query(int p){ + + int s=0; + + for(int i=p;i>0;i-=(i&(-i))){ + + s+=a[i]; + + } + + return s; + +} + +int main(){ + + f=fopen("swap.in","r"); + + g=fopen("swap.out","w"); + + fscanf(f,"%s%s",s,s1); + + l=strlen(s); + + for(i=0;i +#include +using namespace std; + +int n,perm[50100],lit[50100][30],nr[2][30],v[50100],aib[150000]; + +char A[50100],B[50100]; + +long long sol; + + + +inline void Aduna(int poz,int val) + +{ + + while(poz<=n) + + { + + aib[poz]+=val; + + poz+=(poz & -poz); + + } + +} + + + +inline int Suma(int poz) + +{ + + int sum=0; + + while(poz) + + { + + sum+=aib[poz]; + + poz-=(poz & -poz); + + } + + return sum; + +} + + + +int main() + +{ + + int i; + + ifstream fin("swap.in"); + + fin>>(A+1); + + fin>>(B+1); + + fin.close(); + + + + n=strlen(A+1); + + for(i=1;i<=n;i++) + + { + + v[i]=++nr[0][A[i]-'a']; + + lit[++nr[1][B[i]-'a']][B[i]-'a']=i; + + } + + for(i=0;i<30;i++) + + { + + if(nr[0][i]!=nr[1][i]) + + sol=-1; + + } + + if(sol==0) + + { + + for(i=1;i<=n;i++) + + perm[i]=lit[v[i]][A[i]-'a']; + + Aduna(perm[1],1); + + for(i=2;i<=n;i++) + + { + + sol+=(i-1-Suma(perm[i])); + + Aduna(perm[i],1); + + } + + } + + + + ofstream fout("swap.out"); + + fout< +#include +#include +using namespace std; + + + +FILE*f=fopen("swap.in","r"); + +FILE*g=fopen("swap.out","w"); + + + +int n; + +int child[30],aib[50005],fr1[30],fr2[30]; + +char A[50005],B[50005]; + +vectorP[30]; + + + +inline int lsb ( int x ){ + + return x & -x; + +} + + + +inline void update_aib ( int poz ){ + + + + while ( poz <= n ){ + + ++aib[poz]; + + poz += lsb(poz); + + } + +} + + + +inline int query_aib ( int poz ){ + + int s = 0; + + + + while ( poz > 0 ){ + + s += aib[poz]; + + poz -= lsb(poz); + + } + + + + return s; + +} + + + +int main () { + + + + fscanf(f,"%s%s",A+1,B+1); + + n = strlen(A+1); + + + + for ( int i = 1 ; i <= n ; ++i ){ + + P[ A[i]-'a'+1 ].push_back(i); + + ++fr1[A[i]-'a'+1]; + + ++fr2[B[i]-'a'+1]; + + } + + + + for ( int i = 1 ; i <= 26 ; ++i ){ + + if ( fr1[i] != fr2[i] ){ + + fprintf(g,"-1\n"); + + fclose(f); + + fclose(g); + + return 0; + + } + + } + + + + long long sol = 0; + + for ( int i = 1 ; i <= n ; ++i ){ + + int pos = P[ B[i]-'a'+1 ][child[B[i]-'a'+1]++]; + + int real_pos = pos + i - 1 - query_aib(pos-1); + + + + sol += real_pos - i; + + update_aib(pos); + + } + + + + fprintf(g,"%lld\n",sol); + + + + fclose(f); + + fclose(g); + + + + return 0; + +} diff --git a/swap/aib/19c2df94-0b67-11ec-89ae-2cf05d84bfc2.cpp b/swap/aib/19c2df94-0b67-11ec-89ae-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d95c3fea82288256b82dfb73256787498b4d86f3 --- /dev/null +++ b/swap/aib/19c2df94-0b67-11ec-89ae-2cf05d84bfc2.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include + + +using namespace std; + + + +typedef long long LL; + + + +const int kMaxN = 50005; + + + +deque C[2][26]; + +int N, P[kMaxN], BIT[kMaxN], I; + +char S[2][kMaxN]; + + + +void BuildP() + +{ + + for (int i = 1; i <= N; ++i) + + for (int s = 0; s < 2; ++s) + + C[s][S[s][i]-'a'].push_back(i); + + for (int c = 0; c < 26; ++c) + + for (; !C[0][c].empty(); C[0][c].pop_front(), C[1][c].pop_front()) + + P[C[0][c].front()] = C[1][c].front(); + +} + + + +bool CheckS() + +{ + + vector F(26, 0); + + for (int i = 1; i <= N; ++i) + + ++F[S[0][i]-'a'], --F[S[1][i]-'a']; + + for (int c = 0; c < 26; ++c) + + if (F[c] != 0) return false; + + return true; + +} + + + +inline int LSB(const int X) + +{ + + return X&(-X); + +} + + + +inline void Update(int X, const int V) + +{ + + for (; X <= N; X += LSB(X)) BIT[X] += V; + +} + + + +inline int Query(int X) + +{ + + int Q = 0; + + for (; X; X -= LSB(X)) Q += BIT[X]; + + return Q; + +} + + + +void Solve() + +{ + + if (!CheckS()) + + { + + I = -1; return; + + } + + BuildP(); + + for (int i = 1; i <= N; ++i) + + I += Query(N) - Query(P[i]), Update (P[i], 1); + +} + + + +void Read() + +{ + + freopen("swap.in", "r", stdin); + + scanf("%s\n%s", S[0]+1, S[1]+1); + + N = strlen (S[0]+1); + +} + + + +void Print() + +{ + + freopen("swap.out", "w", stdout); + + printf("%d\n", I); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/swap/aib/1a12a774-0b67-11ec-87fa-2cf05d84bfc2.cpp b/swap/aib/1a12a774-0b67-11ec-87fa-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c7c396c3f18356933f6e48e1a4697d9e5297d058 --- /dev/null +++ b/swap/aib/1a12a774-0b67-11ec-87fa-2cf05d84bfc2.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int N,AIB[50100],Sol; + +char S[50100],S2[50100]; + +queue Index[26]; + + + +int Query(int nod) { + + + + int Rez=0; + + + + while(nod>0) { + + Rez+=AIB[nod]; + + nod-=(nod&-nod); + + } + + + + return Rez; + + + +} + +void Update(int nod) { + + + + while(nod<=N) { + + AIB[nod]++; + + nod+=(nod&-nod); + + } + + + +} + +void Afis() { + + + + ofstream out("swap.out"); + + out< +#include + + +int n, sol; + +int aib[50002], f[155]; + +int nr[27][50002]; + +char s1[50002], s2[50002]; + + + +int query (int p) + +{ + + int x = 0; + + while (p) + + { + + x += aib[p]; + + p -= p & -p; + + } + + return x; + +} + + + +void update (int p) + +{ + + while (p <= n) + + { + + aib[p] ++; + + p += p & -p; + + } + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (s1, 50001, stdin); + + fgets (s2, 50001, stdin); + + n = strlen (s1 + 1); + + + + int i, p; + + + + for (i = 1; i <= n; i ++) + + { + + f[s1[i] - 'a'] ++; + + nr[s1[i] - 'a'][++ nr[s1[i] - 'a'][0]] = i; + + f[s2[i] - 'a'] --; + + } + + for (i = 0; i <= 26;nr[i][0] = 0, i ++) + + if (f[i]) + + { + + printf ("-1\n"); + + return 0; + + } + + + + for (i = 1; i <= n; i ++) + + { + + p = nr[s2[i] - 'a'][++ nr[s2[i] - 'a'][0]]; + + sol = sol + i - 1 - query (p); + + update (p); + + } + + printf ("%d\n", sol); + + return 0; + +} diff --git a/swap/aib/1a91008c-2e99-4f99-9896-d4292854bc97.cpp b/swap/aib/1a91008c-2e99-4f99-9896-d4292854bc97.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be6c96a90daf4ca6f67cf784583b82e73ae1d44c --- /dev/null +++ b/swap/aib/1a91008c-2e99-4f99-9896-d4292854bc97.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n, i, s, aib[50003], vec[50003]; + +deque a[30]; + +char s1[50003], s2[50003]; + +int query(int p) + +{ + + int r=0; + + for(;p;p-=(p & -p)) + + r+=aib[p]; + + return r; + +} + +void update(int p) + +{ + + for(;p<=n;p+=(p & -p)) + + aib[p]++; + +} + +int main() + +{ + + f>>s1>>s2; + + for(i=0;s1[i]!=0;i++) + + a[s1[i]-'a'].push_back(i+1); + + for(i=0;s2[i]!=0;i++) + + { + + if(a[s2[i]-'a'].size()==0) + + { + + g<<-1; + + return 0; + + } + + n++; + + vec[i+1]=a[s2[i]-'a'][0]; + + a[s2[i]-'a'].pop_front(); + + } + + for(i=n;i>0;i--) + + { + + s=s+query(vec[i]); + + update(vec[i]); + + } + + g< +#include +#include + + +using namespace std; + + + +char a[50002],b[50002]; + +int v[50001],aux[26],aib[50001]; + +queue ind[26]; + + + +int main() + +{ + + int n,i,j,sol=0; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a,sizeof(a),stdin); + + fgets(b,sizeof(b),stdin); + + n=strlen(a)-1; + + for (i=0;i +#include +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +string a, b; + +queue q[200]; + +int aib[50005], rez; + +int ap_a[200], ap_b[200]; + +typedef struct{ + + int val, poz; + +}camp; + +camp v[50005]; + + + +bool cmp(camp a, camp b){ + + + + return a.val > b.val; + + + +} + + + +void citeste(){ + + + + f >> a; + + f >> b; + + + +} + + + +int query(int poz){ + + + + int sum = 0; + + + + int bit = 0; + + + + for(; poz; ){ + + sum += aib[poz]; + + while( (poz & (1< +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int i,ok=1,n,f[130],f1[130],sol,v[50010],poz[50010],w[50010],nr,cnt,k,a[130][50010]; + +char s[50010],c[50010]; + +int query(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=v[p]; + + return sol; + +} + +void update(int p){ + + for(;p<=n;p+=(p&-p)) + + v[p]++; + +} + +int main(){ + + fin>>(s+1); + + for(i=1;s[i]!=0;i++) + + f[s[i]]++; + + fin>>(c+1); + + for(i=1;c[i]!=0;i++) + + f1[c[i]]++; + + for(char t='a';t<='z';t++) + + if(f[t]!=f1[t]){ + + ok=0; + + break; + + } + + if(!ok){ + + fout<<-1; + + return 0; + + } + + n=strlen(s+1); + + for(i=1;i<=n;i++) + + f[s[i]]=f1[s[i]]=0; + + + + for(i=1;i<=n;i++) + + a[c[i]-'0'][++f[c[i]]]=i; + + + + for(i=1;i<=n;i++){ + + f1[s[i]]++; + + w[i]=a[s[i]-'0'][f1[s[i]]]; + + + + } + + for(i=1;i<=n;i++){ + + sol+=(query(n)-query(w[i])); + + update(w[i]); + + } + + fout< +#include +#include +using namespace std; + + + + + +char s[50010]; + +int a[50010], b[50010], v[50010], d[30], n, aib[50010], sol; + +vector c[30]; + + + +void update(int p) + +{ + + do + + { + + aib[p]++; + + p += (p&(p-1))^p; + + } + + while (p<=n); + +} + + + +int query(int p) + +{ + + int s=0; + + do + + { + + s+=aib[p]; + + p=p&(p-1); + + } + + while (p); + + return s; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(s,50010,stdin); + + n=strlen(s); + + n--; + + int i, ok=0; + + for (i=0; i +#include +#include +using namespace std; + +ifstream f("swap.in"); +ofstream g("swap.out"); + + + + +string s1,s2; +int Aib[100666],i,N; +int a[100666],b[100666]; +vector H[666]; +int fr1[666],fr2[666]; +int grad[777],result; + +int query(int pos) +{ + int res=0; + for(; pos; pos-=(pos&(pos^(pos-1)))) res+=Aib[pos]; + return res; +} + +void update(int pos,int value) +{ + for(; pos<=N; pos+=(pos&(pos^(pos-1)))) + Aib[pos]+=value; +} + +int main() +{ + f>>s1>>s2; + N=s1.length(); + + for(i=0; i +#include +#include +#include +#include +#include +using namespace std; + +struct nod{ int poz; + +char c;}; + +ofstream fout("swap.out"); + +nod s1[50005],s2[50005]; + +char aux1[50005],aux2[50005]; + +int P[50005],M[1000],aib[50005],M1[1000],p=1,N1,N2; + +long long S; + +vectorm[50005]; + +void cit() + +{int i,j; + + ifstream fin("swap.in"); + + fin>>aux1>>aux2; + + N1=strlen(aux1),N2=strlen(aux2); + + for(i=0;aux1[i];i++) + + {s1[i].c=aux1[i]; + + if(i==0||M[(int)s1[i].c]==0) + + {s1[i].poz=1; + + M[(int)s1[i].c]=1; + + } + + else s1[i].poz=M[(int)s1[i].c]+1,M[(int)s1[i].c]++; + + } + + for(i=0;aux2[i];i++) + + { s2[i].c=aux2[i]; + + M1[(int)s2[i].c]++; + + } + + if(N1==N2) + + for(i=1;i<=500;i++) + + if(M[i]!=M1[i]) + + {p=0; return;} + + + + + + cout<<1; + + for(i=0;s2[i].c;i++) + + { + + m[s2[i].c-'a'+1].push_back(i+1); + + } + + for(i=0;s1[i].c;i++) + + P[i+1]=m[s1[i].c-'a'+1][s1[i].poz-1]; + + + + fin.close(); + +} + + + +long long query(int p) + +{long long s=0,i; + +for(i=p;i;i-=i&-i) + + s+=aib[i]; + +return s; + +} + +void update (int p,int x) + + {long i; + + for(i=p;i<=N1;i+=i&-i) + + aib[i]+=x; + + + + } + +void solve() + +{int i; + + for(i=1;i<=N1;i++) + + {S+=(query(N1)-query(P[i])); + + + + update(P[i],1); + + } + + fout< +#include +#include +#include +#include +#include +using namespace std; + +struct nod{ int poz; + +char c;}; + +ofstream fout("swap.out"); + +nod s1[50005],s2[50005]; + +char aux1[50005],aux2[50005]; + +int P[50005],M[1000],aib[50005],M1[1000],p=1,N1,N2; + +long long S; + +vectorm[50005]; + +void cit() + +{int i,j; + + ifstream fin("swap.in"); + + fin>>aux1>>aux2; + + N1=strlen(aux1),N2=strlen(aux2); + + for(i=0;aux1[i];i++) + + {s1[i].c=aux1[i]; + + if(i==0||M[(int)s1[i].c]==0) + + {s1[i].poz=1; + + M[(int)s1[i].c]=1; + + } + + else s1[i].poz=M[(int)s1[i].c]+1,M[(int)s1[i].c]++; + + } + + for(i=0;aux2[i];i++) + + { s2[i].c=aux2[i]; + + M1[(int)s2[i].c]++; + + } + + if(N1==N2) + + for(i=1;i<=500;i++) + + if(M[i]!=M1[i]) + + {p=0; return;} + + + + + + cout<<1; + + for(i=0;s2[i].c;i++) + + { + + m[s2[i].c-'a'+1].push_back(i+1); + + } + + for(i=0;s1[i].c;i++) + + P[i+1]=m[s1[i].c-'a'+1][s1[i].poz-1]; + + + + fin.close(); + +} + + + +long long query(int p) + +{long long s=0,i; + +for(i=p;i;i-=i&-i) + + s+=aib[i]; + +return s; + +} + +void update (int p,int x) + + {long i; + + for(i=p;i<=N1;i+=i&-i) + + aib[i]+=x; + + + + } + +void solve() + +{int i; + + for(i=1;i<=N1;i++) + + {S+=(query(N1)-query(P[i])); + + + + update(P[i],1); + + } + + fout< +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +#include + + + + + + +using namespace std; + +char A[50005],B[50005]; + +vector C[26]; + +inline int lit(char x) + +{ + + return x>='a' && x<='z'; + +} + +int a,b,nr1[26],nr2[26],act[26],D[50005],aib[50005],rez; + +void read() + +{ + + fgets(A+1,50005,stdin); + + fgets(B+1,50005,stdin); + + while (lit(A[a+1])) + + { + + a++; + + C[A[a]-'a'].push_back(a); + + nr1[A[a]-'a']++; + + } + + while (lit(B[b+1])) + + { + + b++; + + nr2[B[b]-'a']++; + + } + +} + +inline int verif() + +{ + + int i; + + for (i=0; i<26; i++) + + if (nr1[i]!=nr2[i]) + + return 0; + + return 1; + +} + +int lsb(int x) + +{ + + return x & -x; + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz; i<=b; i+=lsb(i)) + + aib[i]+=val; + +} + +int query(int poz) + +{ + + int i,sum=0; + + for (i=poz; i>0; i-=lsb(i)) + + sum+=aib[i]; + + return sum; + +} + +void solve() + +{ + + int i,l; + + for (i=1; i<=b; i++) + + { + + l=B[i]-'a'; + + D[i]=C[l][act[l]++]; + + } + + for (i=b; i>=1; i--) + + { + + rez+=query(D[i]-1); + + update(D[i],1); + + } + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + read(); + + if (!verif()) + + { + + printf("-1\n"); + + return 0; + + } + + solve(); + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/1cfd21b2-0b67-11ec-948f-2cf05d84bfc2.cpp b/swap/aib/1cfd21b2-0b67-11ec-948f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..036d52e826080aa154279f183d0a7dd69d9529f2 --- /dev/null +++ b/swap/aib/1cfd21b2-0b67-11ec-948f-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +#include +#include +#include + + + + + +using namespace std; + +char a[50005], b[50005]; +int n, m, FF[40], FS[40], i, LG [40], P[50005], j; +int aib [50005], sol; +vector V[40]; +inline int C (char a) {return int(a) - 97;} +inline bool check () +{ + for (i = C ('a'); i <= C ('z'); i++) + if (FF[i] != FS[i]) return 1; + return 0; + +} +inline void update (int x) { + for ( ; x <= n; x += x & -x) ++aib[x]; +} +inline int query (int x) { + int S = 0; + for ( ; x >= 1; x -= x & -x) S += aib [x]; + return S; +} +int main () +{ + freopen ("swap.in", "r", stdin); + freopen ("swap.out", "w", stdout); + scanf ("%s%s\n", a, b); + n = strlen (a); m = strlen (b); + if (n != m) printf ("-1\n"); + for (i = 0; i < n; i++) { + ++FF[C (a[i])]; + ++FS[C (b[i])]; + V[C (b[i])].push_back (i + 1); + } + if (check ()) { + printf ("-1\n"); + return 0; + } + for (i = 0; i < n; i++) { + P[i + 1] = V[C (a[i])][LG[C (a[i])]]; + LG[C (a[i])]++; + } + for (i = 1; i <= n; i++) { + sol += i - 1 - query (P[i]); + update (P[i]); + } + printf ("%d\n", sol); + + +} diff --git a/swap/aib/1d3bc073-0b67-11ec-ac71-2cf05d84bfc2.cpp b/swap/aib/1d3bc073-0b67-11ec-ac71-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69fc8577a809762b7fdc2d0782f5c81511ecbfb7 --- /dev/null +++ b/swap/aib/1d3bc073-0b67-11ec-ac71-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +#include #include +#include +using namespace std; + +const int MAX_N = 50007; + +vector B[27]; + +int AIB[MAX_N], A[27], N; + +char S[MAX_N], T[MAX_N]; + +void update(int x) + +{ + + for(;x<=N;x+=(x&-x)) AIB[x]++; + +} + +inline int query(int x) + +{ + + int sol = 0; + + for(;x;x-=(x&-x)) sol += AIB[x]; + + return sol; + +} + +inline int getP(char x, int y) + +{ + + int c = x - 96; + + if( B[c].size() <= y-1) return -1; + + return B[c][y-1]; + +} + +int sol; + +int main() + +{ + + ifstream in("swap.in"); ofstream out("swap.out"); + + in>>S+1>>T+1; + + int i, P; + + N = strlen(S+1); + + for(i = 1; i <= N; ++i) + + B[ T[i] - 96 ].push_back(i); + + for(i = 1; i <= N; ++i) + + { + + A[ S[i] - 96 ]++; + + P = getP( S[i], A[ S[i] - 96 ] ); + + if(P == -1) break; + + sol += query(N) - query(P); + + update(P); + + } + + if(P != - 1) out< #include #include +#include +using namespace std; +vector p[26]; + +int aib[50005],f[26]; + +char a[50005],b[50005]; + +int n,nrt; + + + +int lsb (int x) + +{ + + return x^(x-1)&x; + +} + + + +void update (int poz,int val) + +{ + + for ( ; poz<=n; poz+=lsb (poz)) + + aib[poz]+=val; + +} + + + +int query (int poz) + +{ + + int sum; + + + + for (sum=0; poz; poz-=lsb (poz)) + + sum+=aib[poz]; + + return sum; + +} + + + +void solve () + +{ + + int i; + + + + n=strlen (a+1)-1; + + for (i=1; i<=n; ++i) + + p[a[i]-'a'].push_back (i); + + for (i=1; i<=n; ++i) + + update (i,1); + + for (i=1; i<=n; ++i) + + { + + if (f[b[i]-'a']>=(int)p[b[i]-'a'].size ()) + + { + + printf ("-1"); + + return ; + + } + + nrt+=query (p[b[i]-'a'][f[b[i]-'a']]-1); + + update (p[b[i]-'a'][f[b[i]-'a']],-1); + + ++f[b[i]-'a']; + + } + + printf ("%d",nrt); + +} + + + +int main () + +{ + + freopen ("swap.in","r",stdin); + + freopen ("swap.out","w",stdout); + + + + fgets (a+1,50005,stdin); + + fgets (b+1,50005,stdin); + + solve (); + + + + return 0; + +} diff --git a/swap/aib/1d683bc4-1f29-4a45-a29a-eccd708d7bba.cpp b/swap/aib/1d683bc4-1f29-4a45-a29a-eccd708d7bba.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eedfad199b0077d5ed4c2cabac4e5313d8118dbf --- /dev/null +++ b/swap/aib/1d683bc4-1f29-4a45-a29a-eccd708d7bba.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +const int maxN = 1 << 16; + + + +vector A[26], B[26]; + + + +int N, Perm[maxN], Aib[maxN]; + +char a[maxN], b[maxN]; + + + +int Sol; + + + +inline int lsb(int x){ + + return (x ^ (x - 1)) & x; + +} + + + +void Update(int poz, int val){ + + while (poz <= N){ + + Aib[poz] += val; + + poz += lsb(poz); + + } + +} + + + +int Query(int poz){ + + int S = 0; + + while (poz){ + + S += Aib[poz]; + + poz -= lsb(poz); + + } + + + + return S; + +} + + + +int main(){ + + int i, j; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + + + + for (N = 0; a[N] != 0; ++N); + + + + for (i = 0; i < N; ++i) A[a[i] - 'a'].push_back(i); + + + + for (i = 0; i < N; ++i) B[b[i] - 'a'].push_back(i); + + + + for (i = 0; i <= 25; ++i){ + + if (A[i].size() != B[i].size()){ + + printf("-1\n"); + + return 0; + + } + + for (j = 0; j < A[i].size(); ++j) Perm[A[i][j] + 1] = B[i][j] + 1; + + } + + + + Update(Perm[1], 1); + + for (i = 2; i <= N; ++i){ + + Sol += Query(N) - Query(Perm[i]); + + Update(Perm[i], 1); + + } + + + + printf("%d\n", Sol); + + + +} diff --git a/swap/aib/1d9c7c76-0b67-11ec-a506-2cf05d84bfc2.cpp b/swap/aib/1d9c7c76-0b67-11ec-a506-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f1a0a7b3ba68f0e105091dbfcebde58b968da3e --- /dev/null +++ b/swap/aib/1d9c7c76-0b67-11ec-a506-2cf05d84bfc2.cpp @@ -0,0 +1,161 @@ +#include +#include + + + + + + +using namespace std; + +char A[50005],B[50005]; + +vector C[26]; + +inline int lit(char x) + +{ + + return x>='a' && x<='z'; + +} + +int a,b,nr1[26],nr2[26],act[26],D[50005],aib[50005],rez; + +void read() + +{ + + fgets(A+1,50005,stdin); + + fgets(B+1,50005,stdin); + + while (lit(A[a+1])) + + { + + a++; + + C[A[a]-'a'].push_back(a); + + nr1[A[a]-'a']++; + + } + + while (lit(B[b+1])) + + { + + b++; + + nr2[B[b]-'a']++; + + } + +} + +inline int verif() + +{ + + int i; + + for (i=0; i<26; i++) + + if (nr1[i]!=nr2[i]) + + return 0; + + return 1; + +} + +int lsb(int x) + +{ + + return x & -x; + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz; i<=b; i+=lsb(i)) + + aib[i]+=val; + +} + +int query(int poz) + +{ + + int i,sum=0; + + for (i=poz; i>0; i-=lsb(i)) + + sum+=aib[i]; + + return sum; + +} + +void solve() + +{ + + int i,l; + + for (i=1; i<=b; i++) + + { + + l=B[i]-'a'; + + D[i]=C[l][act[l]++]; + + } + + for (i=b; i>=1; i--) + + { + + rez+=query(D[i]-1); + + update(D[i],1); + + } + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + read(); + + if (!verif()) + + { + + printf("-1\n"); + + return 0; + + } + + solve(); + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/1dbbb1bd-0b67-11ec-90bc-2cf05d84bfc2.cpp b/swap/aib/1dbbb1bd-0b67-11ec-90bc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..036d52e826080aa154279f183d0a7dd69d9529f2 --- /dev/null +++ b/swap/aib/1dbbb1bd-0b67-11ec-90bc-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i #include +#include +using namespace std; + +const int MAX_N = 50007; + +vector B[27]; + +int AIB[MAX_N], A[27], N; + +char S[MAX_N], T[MAX_N]; + +void update(int x) + +{ + + for(;x<=N;x+=(x&-x)) AIB[x]++; + +} + +inline int query(int x) + +{ + + int sol = 0; + + for(;x;x-=(x&-x)) sol += AIB[x]; + + return sol; + +} + +inline int getP(char x, int y) + +{ + + int c = x - 96; + + if( B[c].size() <= y-1) return -1; + + return B[c][y-1]; + +} + +int sol; + +int main() + +{ + + ifstream in("swap.in"); ofstream out("swap.out"); + + in>>S+1>>T+1; + + int i, P; + + N = strlen(S+1); + + for(i = 1; i <= N; ++i) + + B[ T[i] - 96 ].push_back(i); + + for(i = 1; i <= N; ++i) + + { + + A[ S[i] - 96 ]++; + + P = getP( S[i], A[ S[i] - 96 ] ); + + if(P == -1) break; + + sol += query(N) - query(P); + + update(P); + + } + + if(P != - 1) out< #include +#include +#include +using namespace std; + + + +int sol; + +char strInit[50024], strMod[50024]; + +int aibAp[50024]; + +vector vctPoz[26]; + + + +inline int aibSum(int x) + +{ + + int sum = 0; + + + + for (int i = x; i; i -= (i ^ (i & (i - 1)))) + + sum += aibAp[i]; + + + + return sum; + +} + + + +inline void aibAdd(int x, int val) + +{ + + for (int i = x; i < 50024; i += (i ^ (i & (i - 1)))) + + aibAp[i] += val; + +} + + + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(strInit + 1, 50024, stdin); + + fgets(strMod + 1, 50024, stdin); + + + + int n = strlen(strInit + 1) - 1; + + for (int i = 1; i <= n; i++) + + vctPoz[strInit[i] - 'a'].push_back(i); + + + + for (int i = n; i; i--) + + { + + int loc = vctPoz[strMod[i] -'a'].size(); + + if (!loc) + + { + + sol = -1; + + + + break; + + } + + int poz = vctPoz[strMod[i] - 'a'][loc - 1]; + + vctPoz[strMod[i] - 'a'].pop_back(); + + + + sol += aibSum(poz); + + aibAdd(poz, 1); + + } + + + + printf("%d\n", sol); + + + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/swap/aib/1f1c2ff2-0b67-11ec-b63c-2cf05d84bfc2.cpp b/swap/aib/1f1c2ff2-0b67-11ec-b63c-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e12f33fc69b83568bcf2f4b3f9b561726d7660a --- /dev/null +++ b/swap/aib/1f1c2ff2-0b67-11ec-b63c-2cf05d84bfc2.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + + + + + +int t[50010],n; + +char a[50010],b[50010]; + +int lac[50010],cate[50010]; + +int rez; + +vector unde[50010]; + +inline int make_hash(char c) + +{ + + return c-'a'; + +} + +inline int bit(int x) + +{ + + return (x&(x-1))^x; + +} + +void update(int poz) + +{ + + while(poz>0) + + { + + ++t[poz]; + + poz-=bit(poz); + + } + +} + +int querry(int x) + +{ + + int cat=0; + + while(x<=n) + + { + + cat+=t[x]; + + x+=bit(x); + + } + + return cat; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hash(a[n]); + + ++cate[aux]; + + unde[aux].push_back(n); + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hash(b[i]); + + if(lac[aux]==cate[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + int x=unde[aux][lac[aux]++]; + + rez+=x-i+querry(x); + + update(x-1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/1f4b611c-edde-4e64-a204-a0c55ca7e8a9.cpp b/swap/aib/1f4b611c-edde-4e64-a204-a0c55ca7e8a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48c188d7929bf20b88f792301582d1c97a59f5cb --- /dev/null +++ b/swap/aib/1f4b611c-edde-4e64-a204-a0c55ca7e8a9.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +vector < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin); n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/1f5e2a6b-0b67-11ec-ae72-2cf05d84bfc2.cpp b/swap/aib/1f5e2a6b-0b67-11ec-ae72-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42a853c99f93b94da74865f37e6019678c41ce4a --- /dev/null +++ b/swap/aib/1f5e2a6b-0b67-11ec-ae72-2cf05d84bfc2.cpp @@ -0,0 +1,110 @@ +#include +#include + + +using namespace std; +char A[50005]; + +char B[50005]; + + + +int P[50005], C[50005]; + +int M[50005][30], L[30]; + +int D[50005]; + +int N, Res; + + + + void add (int p, int e) + + { + + while (p <= N) D[p] += e, p += p ^(p&(p - 1)); + + } + + + + int query (int p) + + { + + int ret = 0; + + for (int i = p; i;) ret += D[i], i -= i ^(i&(i - 1)); + + return ret; + + } + + + + void solve () + + { + + int i, ok = 0; + + for (i = 1; i <= N; ++i) C[i] = ++L[A[i] - 'a']; + + for (i = 1; i <= N; ++i) M[++M[0][B[i] - 'a']][B[i] - 'a'] = i; + + for (i = 1; i <= N; ++i) P[i] = M[C[i]][A[i] - 'a']; + + for (i = 1; i <= N; ++i) + + if (!P[i]) { + + printf ("-1\n"); + + return; + + } + + + + for (i = 1; i <= N; ++i) add(i, 1); + + for (i = 1; i <= N; ++i) + + { + + add (P[i], -1); + + Res += query(P[i] - 1); + + } + + printf ("%d\n", Res); + + } + + + + int main () + + { + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (A , 50001, stdin); + + fgets (B , 50001, stdin); + + N = strlen (A + 1); + + solve (); + + + + return 0; + + } diff --git a/swap/aib/1f9f179e-0b67-11ec-bc43-2cf05d84bfc2.cpp b/swap/aib/1f9f179e-0b67-11ec-bc43-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc92ee73c61a912686602efce9f4e14d9034a5b0 --- /dev/null +++ b/swap/aib/1f9f179e-0b67-11ec-bc43-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + + +using namespace std; +list v[51001]; + +char a[51001],b[51001]; + +int p[51001],aib[51001]; + +int n; + + + +void citire() + +{ + + int i,j; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + scanf("%s\n", a+1); + + n=strlen(a+1); + + scanf("%s\n", b+1); + +} + + + +void add(int x, int val) + +{ + + for (;x<=n;x+=((x^(x-1))&x)) + + aib[x]+=val; + +} + + + +int compute(int x) + +{ + + int ret=0; + + for (;x>0;x-=((x^(x-1))&x)) + + ret+=aib[x]; + + return ret; + +} + + + +bool verif() + +{ + + int i; + + int v1[26]; + + memset(v1,0,sizeof(v1)); + + for (i=1;i<=n;++i) + + v1[a[i]-'a']++; + + for (i=1;i<=n;++i) + + v1[b[i]-'a']--; + + for (i=0;i<26;++i) + + if (v1[i]!=0) + + return false; + + return true; + +} + + + +void solve() + +{ + + int i,rez=0,j,ok; + + + + for (i=1;i<=n;++i) + + a[i]=a[i]-'a'; + + for (i=1;i<=n;++i) + + b[i]=b[i]-'a'; + + memset(p,0,sizeof(p)); + + int nr=0; + + for (i=1;i<=n;++i) + + v[a[i]].push_back(i); + + for (i=1;i<=n;++i) + + { + + p[i]=*(v[b[i]].begin()); + + v[b[i]].pop_front(); + + } + + for (i=1;i<=n;++i) + + add(i,1); + + for (i=1;i<=n;++i) + + { + + rez+=compute(p[i]); + + rez-=1; + + add(p[i],-1); + + } + + + + printf("%d", rez); + +} + + + + + +int main() + +{ + + citire(); + + if (!verif()) + + printf("-1\n"); + + else + + solve(); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/aib/1fdfc114-0b67-11ec-af06-2cf05d84bfc2.cpp b/swap/aib/1fdfc114-0b67-11ec-af06-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5acebf941a911abd9828e0f12cef68145f39878d --- /dev/null +++ b/swap/aib/1fdfc114-0b67-11ec-af06-2cf05d84bfc2.cpp @@ -0,0 +1,68 @@ +#include +#include + + + + +char A[50005], B[50005]; +int P[26][50005], N, App[26], V[50005], AIB[50005]; + +void citire() +{ + fgets(A, 50005, stdin); + fgets(B, 50005, stdin); + N = strlen(A) - 1; +} + +void pre() +{ + for(int i = 0; i < N; ++i) + P[A[i] - 'a'][++P[A[i] - 'a'][0]] = i+1; +} + +void update(int poz) +{ + for(; poz < 50005; poz += poz & -poz) + ++AIB[poz]; +} + +int sum(int poz) +{ + int rez = 0; + for(; poz > 0; poz -= poz & -poz) + rez += AIB[poz]; + return rez; +} + +void solve() +{ + for(int i = 0; i < N; ++i) + { + int x = ++App[B[i]-'a'], ord = B[i] - 'a'; + + int ind = P[ord][x]; + if(ind == 0) + { + printf("-1\n"); + return; + } + V[ind] = i+1; + } + long long Rez = 0; + for(int i = 1; i <= N; ++i) + { + Rez += sum(50005 -1) - sum(V[i]); + update(V[i]); + } + printf("%lld\n",Rez); +} + +int main() +{ + freopen("swap.in","rt",stdin); + freopen("swap.out","wt",stdout); + + citire(); + pre(); + solve(); +} diff --git a/swap/aib/2055e1a0-0b67-11ec-a75f-2cf05d84bfc2.cpp b/swap/aib/2055e1a0-0b67-11ec-a75f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eedfad199b0077d5ed4c2cabac4e5313d8118dbf --- /dev/null +++ b/swap/aib/2055e1a0-0b67-11ec-a75f-2cf05d84bfc2.cpp @@ -0,0 +1,131 @@ +#include +#include + + +using namespace std; + + + +const int maxN = 1 << 16; + + + +vector A[26], B[26]; + + + +int N, Perm[maxN], Aib[maxN]; + +char a[maxN], b[maxN]; + + + +int Sol; + + + +inline int lsb(int x){ + + return (x ^ (x - 1)) & x; + +} + + + +void Update(int poz, int val){ + + while (poz <= N){ + + Aib[poz] += val; + + poz += lsb(poz); + + } + +} + + + +int Query(int poz){ + + int S = 0; + + while (poz){ + + S += Aib[poz]; + + poz -= lsb(poz); + + } + + + + return S; + +} + + + +int main(){ + + int i, j; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + + + + for (N = 0; a[N] != 0; ++N); + + + + for (i = 0; i < N; ++i) A[a[i] - 'a'].push_back(i); + + + + for (i = 0; i < N; ++i) B[b[i] - 'a'].push_back(i); + + + + for (i = 0; i <= 25; ++i){ + + if (A[i].size() != B[i].size()){ + + printf("-1\n"); + + return 0; + + } + + for (j = 0; j < A[i].size(); ++j) Perm[A[i][j] + 1] = B[i][j] + 1; + + } + + + + Update(Perm[1], 1); + + for (i = 2; i <= N; ++i){ + + Sol += Query(N) - Query(Perm[i]); + + Update(Perm[i], 1); + + } + + + + printf("%d\n", Sol); + + + +} diff --git a/swap/aib/2100dc01-0b67-11ec-83c3-2cf05d84bfc2.cpp b/swap/aib/2100dc01-0b67-11ec-83c3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d18bc253eb8276168ebfbe0a129828708386398 --- /dev/null +++ b/swap/aib/2100dc01-0b67-11ec-83c3-2cf05d84bfc2.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + + + +int main(){ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +char si[50000 +1],sf[50000 +1]; + +int i,j,k,k2,f[26],f1[26],l,ok,pi,pf,n0, + +ud,poz[26]={0},a[26][50000/4],p,uc[26]={0}; + +long long nr; + +scanf("%s%s",si,sf); + +l=strlen(si); + +for(i=0;i<26;i++) f[i]=f1[i]=0; + +for(i=0;si[i];++i){ + + a[si[i]-'a'][f[si[i]-'a']]=i; + + f[si[i]-'a']++; + + f1[sf[i]-'a']++; + + } + +ok=1; + +for(i=0;i<26&&ok;++i) + + ok=f[i]==f1[i]; + +if(!ok) {nr=-1;goto finish;} + +nr=0; + +pi=0;pf=l-1;ud=0; + +for(i=0;i=j) uc[k]--; + + + + for(k2=uc[k];a[k][k2] #include +#include + + +using namespace std; + +int n,i,nr,sol,u[27],a[50005],AI[200000]; + +char ch,s1[50005],s2[50005]; + +vector f[27]; + + + +void update(int nod,int l,int r,int x){ + + if (l==r){AI[nod]=1;return;} + + int m=(l+r)>>1; + + if (x<=m)update(nod<<1,l,m,x); + + else update((nod<<1)|1,m+1,r,x); + + AI[nod]=AI[nod<<1]+AI[(nod<<1)|1]; + +} + +void query(int nod,int l,int r,int x){ + + if (l>0&&r<=x){nr+=AI[nod];return;} + + int m=(l+r)>>1; + + if (m)query(nod<<1,l,m,x); + + if (m0) + + a[i]=f[ch][u[ch]++]; + + else {printf("-1\n");return 0;} + + } + + for (i=1;i<=n;++i){ + + update(1,1,n,a[i]); + + nr=0; + + query(1,1,n,a[i]); + + sol+=a[i]-nr; + + } + + printf("%d\n",sol); + +return 0; + +} diff --git a/swap/aib/215dc058-0b67-11ec-96ec-2cf05d84bfc2.cpp b/swap/aib/215dc058-0b67-11ec-96ec-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..848084c1c45674fdaf80e91d6ef37cc27f32752c --- /dev/null +++ b/swap/aib/215dc058-0b67-11ec-96ec-2cf05d84bfc2.cpp @@ -0,0 +1,151 @@ +#include +#include + + + + + + +using namespace std; + + + +long n, i, j, k, l, sol; + +char s1[51000], s2[51000]; + +long p[29], v[51000], aib[51000]; + +vector poz[29]; + + + +int lsb(int x) + +{ + + return (x&(x-1))^x; + +} + + + +void update(int x) + +{ + + while(x<=l) + + { + + aib[x]++; + + x=x+lsb(x); + + } + +} + + + +int query(int x) + +{ + + int rez=0; + + while(x) + + { + + rez+=aib[x]; + + x=x-lsb(x); + + } + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + l=1; + + scanf("%c", &s1[l]); + + while(s1[l]>='a') + + { + + l++; + + scanf("%c", &s1[l]); + + } + + l--; + + for(i=1; i<=l; i++) + + { + + scanf("%c", &s2[i]); + + poz[s2[i]-'a'].push_back(i); + + aib[i]=0; + + } + + for(i=1; i<=l; i++) + + { + + k=s1[i]-'a'; + + if(p[k]==poz[k].size()) + + { + + printf("-1\n"); + + return 0; + + } + + else + + { + + v[i]=poz[k][p[k]]; + + p[k]++; + + } + + } + + for(i=1; i<=l; i++) + + { + + sol+=(i-1-query(v[i])); + + update(v[i]); + + } + + printf("%d\n", sol); + + return 0; + +} diff --git a/swap/aib/22085867-0b67-11ec-8c87-2cf05d84bfc2.cpp b/swap/aib/22085867-0b67-11ec-8c87-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d946c6c2f49f217b69929701aa702a4058bbaba7 --- /dev/null +++ b/swap/aib/22085867-0b67-11ec-8c87-2cf05d84bfc2.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() +{ + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n=0; + while(a[n]>='a' && a[n]<='z') ++n; + + fgets(b, 50001, stdin); + + for(i=0;i +#include +int n,p[26][50001],s[50001],v[50001],i,j,k,nr; + +char a[50001],b[50001]; + +void add(int x){ + + k=1; + + while(!(x&k)) k<<=1; + + for(j=x;j<=n;){ + + v[j]++; + + while(!(j&k)) k<<=1; + + j=j+k;} + +} + + + +int get(int x){ + + k=1; + + while(!(x&k))k<<=1; + + int s=0; + + for(j=x;j>=1;){ + + s=s+v[j]; + + while(!(j&k)) k<<=1; + + j=j-k;} + + return s;} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf(" %s %s ",a,b); + + n=strlen(a); + + if(n!=strlen(b)) {printf("-1");fclose(stdout);return 0;} + + for(i=0;i +#include +#include +using namespace std; + + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() +{ + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n=0; + while(a[n]>='a' && a[n]<='z') ++n; + + fgets(b, 50001, stdin); + + for(i=0;i +#include + + + + + + +FILE *fin,*fout; + + + +typedef long long LL; + + + +char sir[2][50001 +1]; + + + +int nr[28],cp[28]; + +int *poz[28],scade[28],pos[28]; + + + +int scos[50001 +1]; + + + +LL num; + + + +void proces() + +{int i,j,l; + + l=strlen(sir[0]); + + for (i=1;i<='z'-'a'+1;++i) nr[i]=0; + + for (j=0;jnr[val]) + + {num=-1;break;} + + else {p=poz[val][cp[val]]; + + num=num+p-scade[val]; + + scos[p]=1; + + for (j=1;j<='z'-'a'+1;++j) if (pos[j]>p) ++scade[j]; + + ++cp[val]; + + if (cp[val]<=nr[val]) + + for (;pos[val]<=poz[val][cp[val]];++pos[val]) + + scade[val]+=scos[pos[val]]; + + } + + } + + fout=fopen("swap.out","w"); + + fprintf(fout,"%lld\n",num); + + fclose(fout); + + return 0; + +} diff --git a/swap/aib/23296940-0b67-11ec-89d3-2cf05d84bfc2.cpp b/swap/aib/23296940-0b67-11ec-89d3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..231a3c5ce240db9b4600655ee6b54e5ff2a4f9f8 --- /dev/null +++ b/swap/aib/23296940-0b67-11ec-89d3-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +char a[50100],b[50100]; + +vector p[26]; + +int x[26], v[50100], n; + + + +void update(int x,int val) + + { + + for(;x<=n;x+=x^(x-1)&x) + + v[x] += val; + + } + + + +int query(int x) + + { + + int r = 0; + + for(;x;x-=x^(x-1)&x) + + r += v[x]; + + return r; + + } + + + +int main() + + { + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + int i, tmp, S = 0; + + + + fgets(a,50100,stdin); + + fgets(b,50100,stdin); + + + + for(i=0;a[i]!='\n';++i) + + p[a[i]-'a'].push_back(i+1); + + n = i; + + + + for(i=1;i<=n;++i) + + update(i,1); + + + + for(i=0;b[i]!='\n';++i) + + { + + if(x[b[i]-'a'] == p[b[i]-'a'].size()) + + { + + printf("-1\n"); + + return 0; + + } + + tmp = p[b[i]-'a'][x[b[i]-'a']]; + + ++x[b[i]-'a']; + + S += query(tmp-1); + + update(tmp,-1); + + } + + + + printf("%d\n",S); + + + + return 0; + + } diff --git a/swap/aib/236fdd3d-0b67-11ec-801d-2cf05d84bfc2.cpp b/swap/aib/236fdd3d-0b67-11ec-801d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..678619cce45af46a0fca23f5c4385cc6848d2fbe --- /dev/null +++ b/swap/aib/236fdd3d-0b67-11ec-801d-2cf05d84bfc2.cpp @@ -0,0 +1,160 @@ +#include #include +#include +#include +using namespace std; +int s[28][50001], L1, L2, ind[28]; + +int go[50001], c[50001], place[50001]; + +char sir1[50001], sir2[50001]; + + + +void read () + +{ + + freopen ("swap.in", "rt", stdin); + + freopen ("swap.out", "wt", stdout); + + + + fgets(sir1, 50001, stdin); + + fgets(sir2, 50001, stdin); + + + + L1 = strlen (sir1); + + L2 = strlen (sir2); + +} + + + +int LSB (int x) + +{ + + return x ^ (x-1) & x; + +} + + + +void update (int x) + +{ + + for (; x <= L1 ; x += LSB(x)) + + c[x] += 1; + +} + + + +int query (int x) + +{ + + int rez = 0; + + + + for (; x > 0; x -= LSB(x)) + + rez += c[x]; + + return rez; + +} + + + +void solve () + +{ + + int sol = 0; + + + + if (L1 != L2) + + { + + printf ("-1\n"); + + return; + + } + + + + for (int i = 0; i < L2; ++ i) + + s[sir2[i] - 'a'][ind[sir2[i] - 'a']++] = i + 1; + + + + memset (ind, 0, sizeof(ind)); + + for (int i = 0; i < L1; ++ i) + + { + + go[i + 1] = s[sir1[i] - 'a'][ind[sir1[i] - 'a']++]; + + if (!go[i + 1]) + + { + + printf ("-1\n"); + + return; + + } + + place[go[i + 1]] = i + 1; + + } + int ii; + + + + for (int i = 0; i < L1; ++ i) + + { + + ii = place[i+1]; + + sol += ii - go[ii] + query (L1) - query (ii); + + update (ii); + + } + + + + printf ("%d\n", sol); + +} + + + +int + + main () + +{ + + read (); + + solve (); + + return 0; + +} diff --git a/swap/aib/246ee781-0441-475a-b86d-0f928ddde1b7.cpp b/swap/aib/246ee781-0441-475a-b86d-0f928ddde1b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ef8dc0912bb798c01584e356a91541cca4b2f997 --- /dev/null +++ b/swap/aib/246ee781-0441-475a-b86d-0f928ddde1b7.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +using namespace std; + + + + + char A[55005],B[55005]; + vector Poz[30]; + int T[55005],Fi[55005],Nr[55005]; + int Aib[55005]; + int rez; + int i,n; + +void add ( int ind ){ + for ( ; ind<=n; ind+=((ind)&(-ind)) ) + Aib[ind]+=1; +} +int query ( int ind ){ + int rez=0; + for ( ; ind; ind-=((ind)&(-ind)) ){ + rez+=Aib[ind]; + } + return rez; +} + +int main(){ + + freopen ("swap.in","r",stdin); + freopen ("swap.out","w",stdout); + + scanf ("%s", &A ); + scanf ("%s", &B ); + + n=strlen(A); + for ( i=0; i Fi[ B[i]-'a' ] ){ + printf("-1\n"); + return 0; + } + T[ i ] = Poz[ B[i]-'a' ][ Nr[ B[i]-'a'] ]; + T[i]++; + Nr[ B[i]-'a' ]++; + } + + for ( i=n-1; i>=0; i-- ){ + rez += query ( T[i] ); + add ( T[i] ); + } + printf("%d\n",rez); + return 0; +} diff --git a/swap/aib/252a2bc7-70b8-4b5d-b87e-765f5308fe95.cpp b/swap/aib/252a2bc7-70b8-4b5d-b87e-765f5308fe95.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce4775f643d1d93203a291e254a224150c9e02ea --- /dev/null +++ b/swap/aib/252a2bc7-70b8-4b5d-b87e-765f5308fe95.cpp @@ -0,0 +1,107 @@ +#include +#include + + +using namespace std; + +ifstream f ("swap.in"); + +ofstream g ("swap.out"); + +char s[50010],sir[50010]; + +int viz[50],viz2[50],mat[30][50010],v[50010],arb[50010]; + +int n,m,i,total,nr; + +char *p; + +char aux[50010]; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(-i)&i){ + + arb[i]+=1; + + } + +} + +int sum(int poz){ + + int suma=0; + + for(int i=poz;i>0;i-=(-i)&i){ + + suma+=arb[i]; + + } + + return suma; + +} + + + +int main() + +{ + + f.getline(s,50010); + + f.getline(sir,50010); + + n=strlen(s); + + m=strlen(sir); + + if(n!=m){ + + g<<-1; + + return 0; + + } + + for(i=0;i0;--i){ + + total+=sum(v[i]); + + update(v[i]); + + } + + g< +#include +#include +#include + + + + + +using namespace std; + +char a[50005], b[50005]; +int n, m, FF[40], FS[40], i, LG [40], P[50005], j; +int aib [50005], sol; +vector V[40]; +inline int C (char a) {return int(a) - 97;} +inline bool check () +{ + for (i = C ('a'); i <= C ('z'); i++) + if (FF[i] != FS[i]) return 1; + return 0; + +} +inline void update (int x) { + for ( ; x <= n; x += x & -x) ++aib[x]; +} +inline int query (int x) { + int S = 0; + for ( ; x >= 1; x -= x & -x) S += aib [x]; + return S; +} +int main () +{ + freopen ("swap.in", "r", stdin); + freopen ("swap.out", "w", stdout); + scanf ("%s%s\n", a, b); + n = strlen (a); m = strlen (b); + if (n != m) printf ("-1\n"); + for (i = 0; i < n; i++) { + ++FF[C (a[i])]; + ++FS[C (b[i])]; + V[C (b[i])].push_back (i + 1); + } + if (check ()) { + printf ("-1\n"); + return 0; + } + for (i = 0; i < n; i++) { + P[i + 1] = V[C (a[i])][LG[C (a[i])]]; + LG[C (a[i])]++; + } + for (i = 1; i <= n; i++) { + sol += i - 1 - query (P[i]); + update (P[i]); + } + printf ("%d\n", sol); + + +} diff --git a/swap/aib/25b40225-d992-4ed6-8534-ba11a95f677c.cpp b/swap/aib/25b40225-d992-4ed6-8534-ba11a95f677c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e4e99da90755ed6404658eb0bcbd3e24272c7b3 --- /dev/null +++ b/swap/aib/25b40225-d992-4ed6-8534-ba11a95f677c.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + + + +bool check_possible(const string& from, const string& to){ + + array cnt_from = {}, cnt_to = {}; + + for(const auto x : from){ + + ++cnt_from[x-'a']; } + + for(const auto x : to){ + + ++cnt_to[x-'a']; } + + return cnt_from == cnt_to; } + + + +void mk_permut(const string& from, const string& to, vector& permut){ + + array, 26> pozitii; + + for(int i = 0; i < from.size(); ++i){ + + pozitii[from[i]-'a'].push_back(i); } + + for(auto& x : pozitii){ + + reverse(begin(x), end(x)); } + + + + for(int i = 0; i < to.size(); ++i){ + + permut[pozitii[to[i]-'a'].back()] = i; + + pozitii[to[i]-'a'].pop_back(); } } + + + +class aib{ + + int n; + + vector buf; + +public: + + aib(const int N): n(N), buf(n, 0){} + + void update(int poz){ + + for( ; poz < n; poz += (poz+1)&(-poz-1)){ + + ++buf[poz]; } } + + int query(int poz){ + + int rez = 0; + + for( ; poz >= 0; poz -= (poz+1)&(-poz-1)){ + + rez += buf[poz]; } + + return rez; } }; + + + +int cnt_inversiuni(const vector& permut){ + + aib a(permut.size()); + + int rez = 0; + + for(int i = permut.size()-1; i >= 0; --i){ + + rez += a.query(permut[i]); + + a.update(permut[i]); } + + return rez; } + + + +int main(){ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + string from, to; + + f >> from >> ws >> to; + + + + if(!check_possible(from, to)){ + + g << -1; + + return 0; } + + + + vector permut(from.size()); + + mk_permut(from, to, permut); + + g << cnt_inversiuni(permut); + + return 0; } diff --git a/swap/aib/2757c5db-da13-404d-a514-df88a473a000.cpp b/swap/aib/2757c5db-da13-404d-a514-df88a473a000.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e997181356e67ed2cb42473a4262609d38e5a28c --- /dev/null +++ b/swap/aib/2757c5db-da13-404d-a514-df88a473a000.cpp @@ -0,0 +1,120 @@ +#include #include #include +#include +using namespace std; +vector p[26]; + +int aib[50005],f[26]; + +char a[50005],b[50005]; + +int n,nrt; + + + +int lsb (int x) + +{ + + return x^(x-1)&x; + +} + + + +void update (int poz,int val) + +{ + + for ( ; poz<=n; poz+=lsb (poz)) + + aib[poz]+=val; + +} + + + +int query (int poz) + +{ + + int sum; + + + + for (sum=0; poz; poz-=lsb (poz)) + + sum+=aib[poz]; + + return sum; + +} + + + +void solve () + +{ + + int i; + + + + n=strlen (a+1)-1; + + for (i=1; i<=n; ++i) + + p[a[i]-'a'].push_back (i); + + for (i=1; i<=n; ++i) + + update (i,1); + + for (i=1; i<=n; ++i) + + { + + if (f[b[i]-'a']>=(int)p[b[i]-'a'].size ()) + + { + + printf ("-1"); + + return ; + + } + + nrt+=query (p[b[i]-'a'][f[b[i]-'a']]-1); + + update (p[b[i]-'a'][f[b[i]-'a']],-1); + + ++f[b[i]-'a']; + + } + + printf ("%d",nrt); + +} + + + +int main () + +{ + + freopen ("swap.in","r",stdin); + + freopen ("swap.out","w",stdout); + + + + fgets (a+1,50005,stdin); + + fgets (b+1,50005,stdin); + + solve (); + + + + return 0; + +} diff --git a/swap/aib/27a767d2-64ea-4cec-b0e6-d76048c622bf.cpp b/swap/aib/27a767d2-64ea-4cec-b0e6-d76048c622bf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5acebf941a911abd9828e0f12cef68145f39878d --- /dev/null +++ b/swap/aib/27a767d2-64ea-4cec-b0e6-d76048c622bf.cpp @@ -0,0 +1,68 @@ +#include +#include + + + + +char A[50005], B[50005]; +int P[26][50005], N, App[26], V[50005], AIB[50005]; + +void citire() +{ + fgets(A, 50005, stdin); + fgets(B, 50005, stdin); + N = strlen(A) - 1; +} + +void pre() +{ + for(int i = 0; i < N; ++i) + P[A[i] - 'a'][++P[A[i] - 'a'][0]] = i+1; +} + +void update(int poz) +{ + for(; poz < 50005; poz += poz & -poz) + ++AIB[poz]; +} + +int sum(int poz) +{ + int rez = 0; + for(; poz > 0; poz -= poz & -poz) + rez += AIB[poz]; + return rez; +} + +void solve() +{ + for(int i = 0; i < N; ++i) + { + int x = ++App[B[i]-'a'], ord = B[i] - 'a'; + + int ind = P[ord][x]; + if(ind == 0) + { + printf("-1\n"); + return; + } + V[ind] = i+1; + } + long long Rez = 0; + for(int i = 1; i <= N; ++i) + { + Rez += sum(50005 -1) - sum(V[i]); + update(V[i]); + } + printf("%lld\n",Rez); +} + +int main() +{ + freopen("swap.in","rt",stdin); + freopen("swap.out","wt",stdout); + + citire(); + pre(); + solve(); +} diff --git a/swap/aib/281f9ead-6732-4d35-8a62-ab0a5eaafe94.cpp b/swap/aib/281f9ead-6732-4d35-8a62-ab0a5eaafe94.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c695ba649cf6d3aeb6f1a710e77c5c87b4a79b90 --- /dev/null +++ b/swap/aib/281f9ead-6732-4d35-8a62-ab0a5eaafe94.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int p[Nmax] , aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/2b6ee05c-25cd-4481-8455-2c24b69d8056.cpp b/swap/aib/2b6ee05c-25cd-4481-8455-2c24b69d8056.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e52a0ed23a238568c4a357bdf4df6479b7da0a9 --- /dev/null +++ b/swap/aib/2b6ee05c-25cd-4481-8455-2c24b69d8056.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 50005, ALPHA = 26; + + + +int N, FreqA[ALPHA], FreqB[ALPHA], FT[NMAX], Ptr[ALPHA], Ans; + +vector Pos[ALPHA]; + +char A[NMAX], B[NMAX]; + + + +int LSB(int X) + +{ + + return ((X & (X - 1)) ^ X); + +} + + + +void Update(int Pos) + +{ + + for(; Pos <= N; Pos += LSB(Pos)) + + FT[Pos] ++; + +} + + + +int Ask(int Pos) + +{ + + int Ans = 0; + + for(; Pos; Pos -= LSB(Pos)) + + Ans += FT[Pos]; + + return Ans; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(A , 50001, stdin); + + fgets(B , 50001, stdin); + + N = strlen(A + 1); + + + + for(int i = 1; i <= N; ++ i) + + { + + FreqA[A[i] - 'a'] ++; + + FreqB[B[i] - 'a'] ++; + + Pos[A[i] - 'a'].push_back(i); + + } + + + + for(int i = 0; i < ALPHA; ++ i) + + if(FreqA[i] != FreqB[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(int i = 1; i <= N; ++ i) + + { + + int Ch = B[i] - 'a', NextPos = Pos[Ch][ Ptr[Ch] ]; + + Ptr[Ch] ++; + + + + Ans += NextPos - i + Ask(N - NextPos + 1); + + Update(N - NextPos + 1); + + } + + + + printf("%i\n", Ans); + +} diff --git a/swap/aib/2bd5b6e3-df08-4f07-a2be-450c0056a089.cpp b/swap/aib/2bd5b6e3-df08-4f07-a2be-450c0056a089.cpp new file mode 100644 index 0000000000000000000000000000000000000000..243db72bbb76e20aafd38542573b712253762165 --- /dev/null +++ b/swap/aib/2bd5b6e3-df08-4f07-a2be-450c0056a089.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include + + + +std::ifstream in("swap.in"); +std::ofstream out("swap.out"); + +int AIB[50005]; +std::queue positions[256]; + +void update(int x, int k) { + + while (x < 50005) { + AIB[x] += k; + x += (x & (-x)); + } +} + +int query(int x) { + + int sum = 0; + + while (x) { + sum += (AIB[x]); + x -= (x & (-x)); + } + + return sum; +} + +int main() { + std::string s1, s2; + getline(in, s1); + getline(in, s2); + + for (int i = 0; i < s1.size(); i++) + { + positions[s1[i]].push(i); + update(i + 1, 1); + } + + int total = 0; + for (int i = 0; i < s2.size(); i++) { + char ch = s2[i]; + + if (positions[ch].empty()) { + out << "-1\n"; + return 0; + } + + int position = positions[ch].front(); + positions[ch].pop(); + + total += query(position); + update(position + 1, -1); + } + + out << total << "\n"; +} diff --git a/swap/aib/2e97eaca-740e-4632-89eb-3d2705e6f2e4.cpp b/swap/aib/2e97eaca-740e-4632-89eb-3d2705e6f2e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2fd87d022e30cacc180a0b7c493159c46197a43 --- /dev/null +++ b/swap/aib/2e97eaca-740e-4632-89eb-3d2705e6f2e4.cpp @@ -0,0 +1,140 @@ +#include +#include +#include + + + + +char s[50100], sfin[50100]; + +int ind[26], perm[50100], AIB[50100]; + + + +inline int lsb(int X) + +{ + + return X & -X; + +} + + + +int query(int x) + +{ + + int cnt = 0; + + + + while (x) + + { + + cnt += AIB[x]; + + x = x - lsb(x); + + } + + + + return cnt; + +} + + + +void update(int x, int N) + +{ + + while (x <= N) + + { + + AIB[x] ++; + + x += lsb(x); + + } + +} + + + +int main() + +{ + + int i, N; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s , 50001, stdin); fgets(sfin, 50001, stdin); + + N = strlen(s + 1); + + + + for (i = 1; i <= N; i ++) + + { + + int now = s[i] - 'a'; + + while (ind[now] <= N) + + { + + ++ ind[now]; + + if (sfin[ind[now]] == s[i]) + + break; + + } + + if (ind[now] == N + 1) + + { + + printf("-1"); + + return 0; + + } + + perm[i] = ind[now]; + + } + + + + int sol = 0; + + for (i = 1; i <= N; i ++) + + { + + sol += i - 1 - query(perm[i]); + + update(perm[i], N); + + } + + + + printf("%d", sol); + + return 0; + +} diff --git a/swap/aib/31266b72-efab-4827-a1c6-a2074ba59bd1.cpp b/swap/aib/31266b72-efab-4827-a1c6-a2074ba59bd1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0538336af27adb4c5238b8d80b850a8faeec2b28 --- /dev/null +++ b/swap/aib/31266b72-efab-4827-a1c6-a2074ba59bd1.cpp @@ -0,0 +1,139 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int N = 50000 + 7; + +int n; + +string s; + +string t; + +int aib[N]; + +vector p[26]; + + + +void add(int i, int x) + +{ + + while (i <= n) + + { + + aib[i] += x; + + i += i & (-i); + + } + +} + + + +int get(int i) + +{ + + int ans = 0; + + while (i) + + { + + ans += aib[i]; + + i -= i & (-i); + + } + + return ans; + +} + + + +int main() + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + cin >> s >> t; + + n = (int) s.size(); + + s = "." + s; + + t = "." + t; + + for (int i = 1; i <= n; i++) + + { + + int c = s[i] - 'a'; + + p[c].push_back(i); + + } + + for (int i = 1; i <= n; i++) + + { + + add(i, +1); + + } + + for (int c = 0; c < 26; c++) + + { + + reverse(p[c].begin(), p[c].end()); + + } + + int cost = 0; + + for (int i = 1; i <= n; i++) + + { + + int c = t[i] - 'a'; + + if (p[c].empty()) + + { + + cout << "-1\n"; + + return 0; + + } + + int j = p[c].back(); + + p[c].pop_back(); + + cost += get(j) - 1; + + add(j, -1); + + } + + cout << cost << "\n"; + +} diff --git a/swap/aib/318a8627-46c3-4301-874e-c660e7bb0a8b.cpp b/swap/aib/318a8627-46c3-4301-874e-c660e7bb0a8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..036d52e826080aa154279f183d0a7dd69d9529f2 --- /dev/null +++ b/swap/aib/318a8627-46c3-4301-874e-c660e7bb0a8b.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +vector > Veq; + +string sir1, sir2; + +vector freq, aib; + + + +void update(int pos) { + + for(;pos < 50005; pos += pos & (-pos)) { + + aib[pos]++; + + } + +} + + + +int query(int pos) { + + int ans = 0; + + + + for(;pos;pos -= pos & (-pos)) { + + ans += aib[pos]; + + } + + + + return ans; + +} + + + +int main() { + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + cin >> sir1; + + cin >> sir2; + + + + int lg1 = sir1.length(); + + int lg2 = sir2.length(); + + + + if(lg1 != lg2) { + + cout << -1 << '\n'; + + return 0; + + } + + + + freq.resize(30); + + for(int i = 0; i < lg1; ++i) { + + freq[sir1[i] - 'a']++; + + freq[sir2[i] - 'a']--; + + } + + + + for(int i = 0; i < 26; ++i) { + + if(freq[i] != 0) { + + cout << -1 << '\n'; + + return 0; + + } + + } + + + + Veq.resize(30); + + aib.resize(50005); + + + + for(int i = 0; i < lg1; ++i) { + + Veq[sir1[i] - 'a'].push_back(i); + + } + + + + int ans = 0; + + for(int i = 0; i < lg2; ++i) { + + ans += max(0, Veq[sir2[i] - 'a'].front() - query(1 + Veq[sir2[i] - 'a'].front())); + + update(Veq[sir2[i] - 'a'].front() + 1); + + Veq[sir2[i] - 'a'].pop_front(); + + } + + + + cout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/342c93d4-f478-46b3-b979-918cdfd62819.cpp b/swap/aib/342c93d4-f478-46b3-b979-918cdfd62819.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7081be7d2c7ec2410d0295822e5496792f848fc5 --- /dev/null +++ b/swap/aib/342c93d4-f478-46b3-b979-918cdfd62819.cpp @@ -0,0 +1,160 @@ +#include +#include #include +#include +using namespace std; +int s[28][50001], L1, L2, ind[28]; + +int go[50001], c[50001], place[50001]; + +char sir1[50001], sir2[50001]; + + + +void read () + +{ + + freopen ("swap.in", "rt", stdin); + + freopen ("swap.out", "wt", stdout); + + + + fgets(sir1, 50001, stdin); + + fgets(sir2, 50001, stdin); + + + + L1 = strlen (sir1); + + L2 = strlen (sir2); + +} + + + +int LSB (int x) + +{ + + return x ^ (x-1) & x; + +} + + + +void update (int x) + +{ + + for (; x <= L1 ; x += LSB(x)) + + c[x] += 1; + +} + + + +int query (int x) + +{ + + int rez = 0; + + + + for (; x > 0; x -= LSB(x)) + + rez += c[x]; + + return rez; + +} + + + +void solve () + +{ + + int sol = 0; + + + + if (L1 != L2) + + { + + printf ("-1\n"); + + return; + + } + + + + for (int i = 0; i < L2; ++ i) + + s[sir2[i] - 'a'][ind[sir2[i] - 'a']++] = i + 1; + + + + memset (ind, 0, sizeof(ind)); + + for (int i = 0; i < L1; ++ i) + + { + + go[i + 1] = s[sir1[i] - 'a'][ind[sir1[i] - 'a']++]; + + if (!go[i + 1]) + + { + + printf ("-1\n"); + + return; + + } + + place[go[i + 1]] = i + 1; + + } + int ii; + + + + for (int i = 0; i < L1; ++ i) + + { + + ii = place[i+1]; + + sol += ii - go[ii] + query (L1) - query (ii); + + update (ii); + + } + + + + printf ("%d\n", sol); + +} + + + +int + + main () + +{ + + read (); + + solve (); + + return 0; + +} diff --git a/swap/aib/34e7de08-fce6-4a37-825f-fbedd2a54027.cpp b/swap/aib/34e7de08-fce6-4a37-825f-fbedd2a54027.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc61cdf887e7ff0341af2bab20e5d0e2808d5897 --- /dev/null +++ b/swap/aib/34e7de08-fce6-4a37-825f-fbedd2a54027.cpp @@ -0,0 +1,143 @@ +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,i,lga,lgb,s,ok; + +int APa[300]; + +int APb[300]; + +int ord[50005]; + +int poz[300][50005]; + +int c[50005]; + +char a[50005]; + +char b[50005]; + +int p[50005]; + +void update(int poz, int val) + +{ + + int i; + + for(i=poz;i<=n;i+=( (-i)&i )) + + { + + c[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int i,s; + + s=0; + + for(i=poz;i>=1;i-=( (-i)&i )) + + { + + s+=c[i]; + + } + + return s; + +} + +int main() + +{ + + f.getline(a,50005); + + f.getline(b,50005); + + + + lga=strlen(a); + + for(i=0;i=1;--i) + + { + + s+=sum(p[i]); + + update(p[i],1); + + } + + g< +#include +using namespace std; + +int n,perm[50100],lit[50100][30],nr[2][30],v[50100],aib[150000]; + +char A[50100],B[50100]; + +long long sol; + + + +inline void Aduna(int poz,int val) + +{ + + while(poz<=n) + + { + + aib[poz]+=val; + + poz+=(poz & -poz); + + } + +} + + + +inline int Suma(int poz) + +{ + + int sum=0; + + while(poz) + + { + + sum+=aib[poz]; + + poz-=(poz & -poz); + + } + + return sum; + +} + + + +int main() + +{ + + int i; + + ifstream fin("swap.in"); + + fin>>(A+1); + + fin>>(B+1); + + fin.close(); + + + + n=strlen(A+1); + + for(i=1;i<=n;i++) + + { + + v[i]=++nr[0][A[i]-'a']; + + lit[++nr[1][B[i]-'a']][B[i]-'a']=i; + + } + + for(i=0;i<30;i++) + + { + + if(nr[0][i]!=nr[1][i]) + + sol=-1; + + } + + if(sol==0) + + { + + for(i=1;i<=n;i++) + + perm[i]=lit[v[i]][A[i]-'a']; + + Aduna(perm[1],1); + + for(i=2;i<=n;i++) + + { + + sol+=(i-1-Suma(perm[i])); + + Aduna(perm[i],1); + + } + + } + + + + ofstream fout("swap.out"); + + fout< +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, fr1[30], fr2[30], ANS, W[50010], AIB[50010]; + + + +struct nr + +{ + + int P, V; + +} VV[50010]; + + + +char A[50010], B[50010]; + + + +bool cmp(nr a, nr b) + +{ + + return a.V > b.V; + +} + + + +void Update(int P) + +{ + + for(; P <= N; P += (P & (-P))) + + AIB[P] ++; + +} + + + +int Query(int P) + +{ + + int V = 0; + + for(; P; P -= (P & (-P))) + + V += AIB[P]; + + return V; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(A, 50001, stdin); + + fgets(B, 50001, stdin); + + N = strlen(A); + + + + for(i = 0; i < N; i++) + + { + + fr1[A[i] - 'a'] ++; + + fr2[B[i] - 'a'] ++; + + V[B[i] - 'a'].push_back(i + 1); + + W[i] = fr1[A[i] - 'a']; + + } + + + + for(i = 0; i < 30; i++) + + if(fr1[i] != fr2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(i = 0; i < N; i++) + + { + + VV[i].P = i + 1; + + VV[i].V = V[A[i] - 'a'][W[i] - 1]; + + } + + + + sort(VV, VV + N, cmp); + + + + for(i = 0; i < N; i++) + + { + + ANS += Query(VV[i].P); + + Update(VV[i].P); + + } + + + + printf("%i\n", ANS); + + return 0; + +} diff --git a/swap/aib/3af5e7a6-ebcc-402b-8abb-7fb2062111dc.cpp b/swap/aib/3af5e7a6-ebcc-402b-8abb-7fb2062111dc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..179d6797d210e07d3acd42cf95c769e8ea68af81 --- /dev/null +++ b/swap/aib/3af5e7a6-ebcc-402b-8abb-7fb2062111dc.cpp @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +string a, b; + +queue q[200]; + +int aib[50005], rez; + +int ap_a[200], ap_b[200]; + +typedef struct{ + + int val, poz; + +}camp; + +camp v[50005]; + + + +bool cmp(camp a, camp b){ + + + + return a.val > b.val; + + + +} + + + +void citeste(){ + + + + f >> a; + + f >> b; + + + +} + + + +int query(int poz){ + + + + int sum = 0; + + + + int bit = 0; + + + + for(; poz; ){ + + sum += aib[poz]; + + while( (poz & (1< +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int n,i,lga,lgb,s,ok; + +int APa[300]; + +int APb[300]; + +int ord[50005]; + +int poz[300][50005]; + +int c[50005]; + +char a[50005]; + +char b[50005]; + +int p[50005]; + +void update(int poz, int val) + +{ + + int i; + + for(i=poz;i<=n;i+=( (-i)&i )) + + { + + c[i]+=val; + + } + +} + +int sum(int poz) + +{ + + int i,s; + + s=0; + + for(i=poz;i>=1;i-=( (-i)&i )) + + { + + s+=c[i]; + + } + + return s; + +} + +int main() + +{ + + f.getline(a,50005); + + f.getline(b,50005); + + + + lga=strlen(a); + + for(i=0;i=1;--i) + + { + + s+=sum(p[i]); + + update(p[i],1); + + } + + g< +#include +#include + + + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +deque < int > H[30]; + + + +int v[50001], n, s, A[50001]; + + + +char s1[50001], s2[50001]; + + + +void update (int p, int x) { + + for (; p <= n; p += (p & -p)) + + A[p] += x; + +} + + + +int querry (int p) { + + int r = 0; + + for (; p >= 1; p -= (p & -p)) + + r += A[p]; + + return r; + +} + + + +int main () { + + fin >> s1 + 1 >> s2 + 1; + + for (int i = 1; s1[i] != 0; ++i, ++n) { + + H[s1[i] - 'a' + 1].push_back (i); + + } + + for (int i = 1; i <= n; ++i) { + + if (H[s2[i] - 'a' + 1].empty ()) { + + fout << "-1"; + + return 0; + + } + + int nod = H[s2[i] - 'a' + 1].front (); + + H[s2[i] - 'a' + 1].pop_front (); + + s += nod - querry (nod) - 1; + + update (nod, 1); + + } + + fout << s; + + return 0; + +} diff --git a/swap/aib/4152848f-c12d-4b32-9951-dfd438f187ab.cpp b/swap/aib/4152848f-c12d-4b32-9951-dfd438f187ab.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13edb8c8996b20dfa265d89c9220d4c8de6c373c --- /dev/null +++ b/swap/aib/4152848f-c12d-4b32-9951-dfd438f187ab.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + +int arb[100020]; + +queue q[26]; + +void add(int i){ + + for(;i<100000;i+=(i&(-i))) arb[i]++; + +} + +int get(int i){ + + int ans =0; + + for(;i;i-=(i&(-i))) ans+=arb[i]; + + return ans; + +} + +int main(){ + + string a, b; + + int rs=0; + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>a>>b; + + int n=a.length(); + + int m=b.length(); + + for(int i=0;i +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 50005; + + + +int aib[NMAX]; + + + +void update(int toadd, int pos, int n) { + + for(; pos <= n; pos += pos & -pos) + + aib[pos] += toadd; + +} + + + +int query(int pos) { + + int sum = 0; + + for(; pos; pos -= pos & -pos) + + sum += aib[pos]; + + return sum; + +} + + + +int main() { + + + + string a, b; + + in >> a >> b; + + + + vector fr(30, 0); + + for(auto it : a) + + fr[it - 'a'] ++; + + for(auto it : b) + + fr[it - 'a'] --; + + bool havesol = 1; + + for(int i = 0; i < 30; i ++) + + if(fr[i] != 0) + + havesol = 0; + + if(!havesol) + + out << -1; + + else { + + a = " " + a; + + b = " " + b; + + vector> pos(30); + + for(int i = 1; i < a.size(); i ++) + + pos[a[i] - 'a'].push_back(i); + + int ans = 0; + + for(int i = 1; i < a.size(); i ++) { + + int j = pos[b[i] - 'a'].front(); + + pos[b[i] - 'a'].pop_front(); + + + + ans += (j - 1 - query(j)); + + update(1, j, a.size()); + + } + + out << ans; + + } + + + + return 0; + +} diff --git a/swap/aib/44556c35-c398-4e7a-a6c2-1480e2cb1947.cpp b/swap/aib/44556c35-c398-4e7a-a6c2-1480e2cb1947.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b58f7e14eb43a13ac1a70c027cd79dcba4a3827 --- /dev/null +++ b/swap/aib/44556c35-c398-4e7a-a6c2-1480e2cb1947.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + + + + + + +int n, i; +char a[50011], b[50011]; +int perm[50011]; +int id[26][50011]; +int pos[26], act[26]; + +int aib[50011]; +long long ans; + +int zrs(int x) { + return (x ^ (x - 1)) & x; +} + +void add(int pos) { + while (pos <= n) { + aib[pos]++; + pos += zrs(pos); + } +} + +int sum(int pos) { + int ans = 0; + while (pos) { + ans += aib[pos]; + pos -= zrs(pos); + } + return ans; +} + +int main() +{ + freopen("swap.in","r",stdin); + freopen("swap.out","w",stdout); + + scanf("%s\n%s", a + 1, b + 1); + n = strlen(a + 1); + + for (i = 1; i <= n; i++) + id[ b[i] - 'a' ][pos[ b[i] - 'a' ]++] = i; + + for (i = 1; i <= n; i++) { + int go = a[i] - 'a'; + if (pos[go] == act[go]) { + printf("-1"); + return 0; + } + perm[i] = id[go][ act[go]++ ]; + } + + ans = 1LL * n * (n - 1); + ans /= 2; + + for (i = 1; i <= n; i++) { + ans -= sum(perm[i]); + add(perm[i]); + } + + printf("%lld", ans); + + return 0; +} diff --git a/swap/aib/457b24f5-73be-4a62-83c4-447e55c4db02.cpp b/swap/aib/457b24f5-73be-4a62-83c4-447e55c4db02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..13edb8c8996b20dfa265d89c9220d4c8de6c373c --- /dev/null +++ b/swap/aib/457b24f5-73be-4a62-83c4-447e55c4db02.cpp @@ -0,0 +1,70 @@ +#include + + +using namespace std; + +int arb[100020]; + +queue q[26]; + +void add(int i){ + + for(;i<100000;i+=(i&(-i))) arb[i]++; + +} + +int get(int i){ + + int ans =0; + + for(;i;i-=(i&(-i))) ans+=arb[i]; + + return ans; + +} + +int main(){ + + string a, b; + + int rs=0; + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>a>>b; + + int n=a.length(); + + int m=b.length(); + + for(int i=0;i +#include +#include +using namespace std; + + + +int N, aib[60000]; +string s1, s2; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +void insert_aib(int poz, int elem) { + while (poz <= N) { + aib[poz] += elem; + poz += poz & (-poz); + } +} + +int query (int poz) { + int sum = 0; + while (poz > 0) { + sum += aib[poz]; + poz -= poz & (-poz); + } + return sum; +} + +void doSol(int perm[60000]) { + int rez = 0; + + for (int i = 1; i <= N; ++i) { + rez += query (N) - query (perm[i]); + insert_aib(perm[i], 1); + } + fout << rez; +} + +int sol[30][60000], curPoz[30]; +int perm[60000]; + +int main() { + getline(fin, s1); + getline(fin, s2); + + N = s1.size(); + + for (int i = 0; i < s2.size(); ++i) { + int v = s2[i] - 'a'; + sol[v][++sol[v][0]] = i + 1; + } + + for (int i = 0; i < s1.size(); ++i) { + int v = s1[i] - 'a'; + perm[i + 1] = sol[v][++curPoz[v]]; + } + + + for (int i = 0; i < 30; ++i) + if (curPoz[i] != sol[i][0]) { + fout << -1; + return 0; + } + + doSol (perm); + + return 0; +} diff --git a/swap/aib/484acb5c-af5e-4196-909d-15eaa696604d.cpp b/swap/aib/484acb5c-af5e-4196-909d-15eaa696604d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec51c6c080fa5ed1cca96242b03f939742fbc33a --- /dev/null +++ b/swap/aib/484acb5c-af5e-4196-909d-15eaa696604d.cpp @@ -0,0 +1,146 @@ +#include +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, fr1[30], fr2[30], ANS, W[50010], AIB[50010]; + + + +struct nr + +{ + + int P, V; + +} VV[50010]; + + + +char A[50010], B[50010]; + + + +bool cmp(nr a, nr b) + +{ + + return a.V > b.V; + +} + + + +void Update(int P) + +{ + + for(; P <= N; P += (P & (-P))) + + AIB[P] ++; + +} + + + +int Query(int P) + +{ + + int V = 0; + + for(; P; P -= (P & (-P))) + + V += AIB[P]; + + return V; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(A, 50001, stdin); + + fgets(B, 50001, stdin); + + N = strlen(A); + + + + for(i = 0; i < N; i++) + + { + + fr1[A[i] - 'a'] ++; + + fr2[B[i] - 'a'] ++; + + V[B[i] - 'a'].push_back(i + 1); + + W[i] = fr1[A[i] - 'a']; + + } + + + + for(i = 0; i < 30; i++) + + if(fr1[i] != fr2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + + + for(i = 0; i < N; i++) + + { + + VV[i].P = i + 1; + + VV[i].V = V[A[i] - 'a'][W[i] - 1]; + + } + + + + sort(VV, VV + N, cmp); + + + + for(i = 0; i < N; i++) + + { + + ANS += Query(VV[i].P); + + Update(VV[i].P); + + } + + + + printf("%i\n", ANS); + + return 0; + +} diff --git a/swap/aib/4871b053-7171-4f03-9658-dd1f787ed120.cpp b/swap/aib/4871b053-7171-4f03-9658-dd1f787ed120.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87bcd0a7f0e81a29285f6f7ea661ba437260824c --- /dev/null +++ b/swap/aib/4871b053-7171-4f03-9658-dd1f787ed120.cpp @@ -0,0 +1,112 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +list v[26]; + +char a[50005],b[50005]; + +int n,ch,sol,ap1[26],ap2[26],A[50005],V[50005]; + +inline void update(int p) + +{ + + int i; + + for (i=p;i<=n;i+=(i&-i)) + + ++V[i]; + +} + +inline int query(int p) + +{ + + int i,sum=0; + + for (i=p;i>0;i-=(i&-i)) + + sum+=V[i]; + + return sum; + +} + +int main() + +{ + + int i; + + fin.getline(a+1,50005); + + fin.getline(b+1,50005); + + n=strlen(a+1); + + for (i=1;i<=n;++i) + + { + + ++ap1[a[i]-'a'], ++ap2[b[i]-'a']; + + v[a[i]-'a'].push_back(i); + + } + + for (i=0;i<26;++i) + + if (ap1[i]!=ap2[i]) + + { + + fout<<"-1\n"; + + return 0; + + } + + for (i=1;i<=n;++i) + + { + + ch=b[i]-'a'; + + A[i]=v[ch].front(); + + v[ch].pop_front(); + + } + + for (i=1;i<=n;++i) + + { + + sol+=query(A[i]-1); + + update(A[i]); + + } + + if (n&1) + + sol=((n-1)>>1)*n-sol; + + else + + sol=(n>>1)*(n-1)-sol; + + fout< +#include +#include +#include +#include +#include +using namespace std; + +struct nod{ int poz; + +char c;}; + +ofstream fout("swap.out"); + +nod s1[50005],s2[50005]; + +char aux1[50005],aux2[50005]; + +int P[50005],M[1000],aib[50005],M1[1000],p=1,N1,N2; + +long long S; + +vectorm[50005]; + +void cit() + +{int i,j; + + ifstream fin("swap.in"); + + fin>>aux1>>aux2; + + N1=strlen(aux1),N2=strlen(aux2); + + for(i=0;aux1[i];i++) + + {s1[i].c=aux1[i]; + + if(i==0||M[(int)s1[i].c]==0) + + {s1[i].poz=1; + + M[(int)s1[i].c]=1; + + } + + else s1[i].poz=M[(int)s1[i].c]+1,M[(int)s1[i].c]++; + + } + + for(i=0;aux2[i];i++) + + { s2[i].c=aux2[i]; + + M1[(int)s2[i].c]++; + + } + + if(N1==N2) + + for(i=1;i<=500;i++) + + if(M[i]!=M1[i]) + + {p=0; return;} + + + + + + cout<<1; + + for(i=0;s2[i].c;i++) + + { + + m[s2[i].c-'a'+1].push_back(i+1); + + } + + for(i=0;s1[i].c;i++) + + P[i+1]=m[s1[i].c-'a'+1][s1[i].poz-1]; + + + + fin.close(); + +} + + + +long long query(int p) + +{long long s=0,i; + +for(i=p;i;i-=i&-i) + + s+=aib[i]; + +return s; + +} + +void update (int p,int x) + + {long i; + + for(i=p;i<=N1;i+=i&-i) + + aib[i]+=x; + + + + } + +void solve() + +{int i; + + for(i=1;i<=N1;i++) + + {S+=(query(N1)-query(P[i])); + + + + update(P[i],1); + + } + + fout< +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +# include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +queue d[27]; + +char a[50010],b[50010]; + +int v[50010],f[27],n,i; + +long long s; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(i&(-i))) + + v[i]++; + +} + +int query(int poz){ + + int s=0; + + for(int i=poz;i>=1;i-=(i&(-i))) + + s+=v[i]; + + return s; + +} + +int main () { + + fin>>a+1>>b+1; + + for(i=1;b[i]!=0;i++,n++) + + d[b[i]-'a'+1].push(i); + + for(i=1;i<=n;i++){ + + if(d[a[i]-'a'+1].size()==0){ + + fout<<"-1\n"; + + return 0; + + } + + int el=d[a[i]-'a'+1].front(); + + s+=el-1-query(el); + + update(el); + + d[a[i]-'a'+1].pop(); + + } + + fout< +#include +#include +using namespace std; + +listL[27]; + +long long n,m,i,j,l,a[50100],x,sum,w[50100]; + +char s[50100],s1[50100]; + +FILE *f,*g; + +int query(int p){ + + int s=0; + + for(int i=p;i>0;i-=(i&-i)){ + + s+=a[i]; + + } + + return s; + +} + +void update(int p,int x){ + + for(int i=p;i<=l+1;i+=(i&-i)){ + + a[i]+=x; + + } + +} + +int main(){ + + f=fopen("swap.in","r"); + + g=fopen("swap.out","w"); + + fscanf(f,"%s\n%s",s,s1); + + l=strlen(s); + + for(i=0;i + + + + +using namespace std; + + + +int ARB[105000], rs; + +string s, S; + +void add(int i) + +{ + + for (; i < 100000; i += (i & (-i))) ARB[i]++; + +} + +int get (int i) + +{ + + int rs = 0; + + for (;i;i-=(i & (-i))) rs+=ARB[i]; + + return rs; + +} + +queue < int > Q[40]; + +int main(){ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> S; + + for (int i = 0; i < s.size(); i++) Q[s[i]-'a'].push(i+1); + + for (int i = 0; i < S.size(); i++) + + { + + if(Q[S[i]-'a'].empty()) return cout << -1, 0; + + int curr = Q[S[i]-'a'].front(); + + Q[S[i]-'a'].pop(); + + rs += curr - i - 1 + get(s.size()) - get(curr); + + add(curr); + + } + + cout << rs; + + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + + return 0; + +} diff --git a/swap/aib/50b86258-1f25-4da2-a752-94bf8dd55da9.cpp b/swap/aib/50b86258-1f25-4da2-a752-94bf8dd55da9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6e12f33fc69b83568bcf2f4b3f9b561726d7660a --- /dev/null +++ b/swap/aib/50b86258-1f25-4da2-a752-94bf8dd55da9.cpp @@ -0,0 +1,125 @@ +#include +#include +using namespace std; + + + + + +int t[50010],n; + +char a[50010],b[50010]; + +int lac[50010],cate[50010]; + +int rez; + +vector unde[50010]; + +inline int make_hash(char c) + +{ + + return c-'a'; + +} + +inline int bit(int x) + +{ + + return (x&(x-1))^x; + +} + +void update(int poz) + +{ + + while(poz>0) + + { + + ++t[poz]; + + poz-=bit(poz); + + } + +} + +int querry(int x) + +{ + + int cat=0; + + while(x<=n) + + { + + cat+=t[x]; + + x+=bit(x); + + } + + return cat; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hash(a[n]); + + ++cate[aux]; + + unde[aux].push_back(n); + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hash(b[i]); + + if(lac[aux]==cate[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + int x=unde[aux][lac[aux]++]; + + rez+=x-i+querry(x); + + update(x-1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/5465b640-86dc-405c-a9f7-d329917e3ec5.cpp b/swap/aib/5465b640-86dc-405c-a9f7-d329917e3ec5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b97e846d0ed29860b0f676059d30227cc53ab91 --- /dev/null +++ b/swap/aib/5465b640-86dc-405c-a9f7-d329917e3ec5.cpp @@ -0,0 +1,93 @@ +#include +#include + + + + +using namespace std; + +int n; + +int aib[50010],v[50010]; + +int countA[30],countB[30],first[30][50010]; + +char a[50010],b[50010]; + +void Update(int x){ + + for(x;x<=n;x=x+((x&(x-1))^x)) + + aib[x]++; + +} + +int Query(int x){ + + int answer=0; + + for(x;x>0;x=x-((x&(x-1))^x)) + + answer+=aib[x]; + + return answer; + +} + +int main(){ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i; + + long long answer=0; + + scanf("%s%s",a,b); + + n=strlen(a); + + for(i=0;i=0;i--){ + + answer+=Query(v[i]); + + Update(v[i]); + + } + + printf("%lld",answer); + + return 0; + +} diff --git a/swap/aib/561ccb89-a74d-4305-abda-53165f25a6c8.cpp b/swap/aib/561ccb89-a74d-4305-abda-53165f25a6c8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b88ee41cced0eda4137ce27972fb37ed8f6f5e1b --- /dev/null +++ b/swap/aib/561ccb89-a74d-4305-abda-53165f25a6c8.cpp @@ -0,0 +1,128 @@ +#include + + +using namespace std; + +int m[200][50005],v[200],nr[50005],f[200],perm[50005]; + +char s1[50005],s2[50005]; + +int AIB[50005],l; + +long long sol; + +void update(int pos) + +{ + + for(int i=pos;i<=l;i+=(i&(-i))) + + AIB[i]++; + +} + +int query(int pos) + +{ + + int s=0; + + for(int i=pos;i>=1;i-=(i&(-i))) + + { + + s+=AIB[i]; + + } + + return s; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",&s1); + + scanf("\n"); + + l=strlen(s1); + + fread(s2,1,l,stdin); + + + + + + for(int i=0;i #include +#include +using namespace std; + +const int MAX_N = 50007; + +vector B[27]; + +int AIB[MAX_N], A[27], N; + +char S[MAX_N], T[MAX_N]; + +void update(int x) + +{ + + for(;x<=N;x+=(x&-x)) AIB[x]++; + +} + +inline int query(int x) + +{ + + int sol = 0; + + for(;x;x-=(x&-x)) sol += AIB[x]; + + return sol; + +} + +inline int getP(char x, int y) + +{ + + int c = x - 96; + + if( B[c].size() <= y-1) return -1; + + return B[c][y-1]; + +} + +int sol; + +int main() + +{ + + ifstream in("swap.in"); ofstream out("swap.out"); + + in>>S+1>>T+1; + + int i, P; + + N = strlen(S+1); + + for(i = 1; i <= N; ++i) + + B[ T[i] - 96 ].push_back(i); + + for(i = 1; i <= N; ++i) + + { + + A[ S[i] - 96 ]++; + + P = getP( S[i], A[ S[i] - 96 ] ); + + if(P == -1) break; + + sol += query(N) - query(P); + + update(P); + + } + + if(P != - 1) out< +#include +using namespace std; + + + + + + + +char a[51002], b[51002]; + +int aib[51002], x[51002], nx, n; + +deque< int > v[30]; + + + +void update(int, int) ; + +int query(int) ; + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + int i, p, ch, rez; + + + + fgets(a , 50001, stdin); + + fgets(b , 50001, stdin); + + + + for(n = 0, i = 1; a[i] != '\0'; ++i, ++n) + + { + + ch = a[i] - 'a'; + + v[ch].push_back(i); + + } + + + + for(i = 1; i <= n; ++i) + + { + + ch = b[i] - 'a'; + + if(v[ch].empty()) + + { + + printf("-1\n"); + + return 0; + + } + + + + x[++nx] = v[ch].front(); + + v[ch].pop_front(); + + } + + + + for (i = 1;i <= n;++i) + + update(i, 1); + + + + for (rez=0,i = 1;i <= n;++i) + + { + + update(x[i], -1); + + rez += query(x[i]); + + } + + + + printf("%d\n", rez); + + + + return 0; + +} + + + +void update(int p, int val) + +{ + + for(; p <= n; p += p & -p) aib[p] += val; + +} + + + +int query(int p) + +{ + + int rez; + + + + for(rez = 0; p > 0; p -= p & -p) rez += aib[p]; + + + + return rez; + +} diff --git a/swap/aib/5de66455-ab1b-4309-98da-c62b1a11543d.cpp b/swap/aib/5de66455-ab1b-4309-98da-c62b1a11543d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6873b9389afd426ef4004f31db66dc66ad4c9dde --- /dev/null +++ b/swap/aib/5de66455-ab1b-4309-98da-c62b1a11543d.cpp @@ -0,0 +1,123 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +string a, b; + +int fr[30]; + +int aib[50003], n; + + + +void update(int p, int val) + +{ + + for (int i = p ; i <= n; i += i & -i) + + aib[i] += val; + +} + + + +int query(int p) + +{ + + int suma = 0; + + for (int i = p; i; i -= i & -i) + + suma += aib[i]; + + return suma; + +} + + + +int main() + +{ + + fin >> a >> b; + + n = a.size(); + + + + for (int i = 0; i < n; ++i) + + { + + ++fr[a[i] - 'a']; + + --fr[b[i] - 'a']; + + } + + + + for (int i = 0; i <= 26; ++i) + + if (fr[i]) + + { + + fout << "-1\n"; + + return 0; + + } + + a = " " + a; + + b = " " + b; + + int cnt = 0; + + + + vector < deque > poz(30); + + + + for (int i = 1; i <= n; ++i) + + poz[a[i] - 'a'].push_back(i); + + + + for (int i = 1; i <= n; ++i) + + { + + int p = poz[b[i] - 'a'].front(); + + poz[b[i] - 'a'].pop_front(); + + cnt += p - 1 - query(p); + + update(p, 1); + + } + + + + fout << cnt << "\n"; + + return 0; + +} diff --git a/swap/aib/5df43e58-2ba3-4651-8bc0-34f604491cff.cpp b/swap/aib/5df43e58-2ba3-4651-8bc0-34f604491cff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e701e2b5c087da37bc38a2d175d0ac61453957d3 --- /dev/null +++ b/swap/aib/5df43e58-2ba3-4651-8bc0-34f604491cff.cpp @@ -0,0 +1,130 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +vector < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> a >> b; n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + fout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/5e56f888-1783-4d8d-98af-4571dd48781a.cpp b/swap/aib/5e56f888-1783-4d8d-98af-4571dd48781a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7faf27c258f15b7fe55816d87a871cc45f082ea7 --- /dev/null +++ b/swap/aib/5e56f888-1783-4d8d-98af-4571dd48781a.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +int ARB[105000], rs; + +string s, S; + +void add(int i) + +{ + + for (; i < 100000; i += (i & (-i))) ARB[i]++; + +} + +int get (int i) + +{ + + int rs = 0; + + for (;i;i-=(i & (-i))) rs+=ARB[i]; + + return rs; + +} + +queue < int > Q[40]; + +int main(){ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> S; + + for (int i = 0; i < s.size(); i++) Q[s[i]-'a'].push(i+1); + + for (int i = 0; i < S.size(); i++) + + { + + if(Q[S[i]-'a'].empty()) return cout << -1, 0; + + int curr = Q[S[i]-'a'].front(); + + Q[S[i]-'a'].pop(); + + rs += curr - i - 1 + get(s.size()) - get(curr); + + add(curr); + + } + + cout << rs; + + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + + return 0; + +} diff --git a/swap/aib/5ed30361-edaa-4da9-87ce-53a9e4006748.cpp b/swap/aib/5ed30361-edaa-4da9-87ce-53a9e4006748.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eb8bbe38f5a3ee5bbb066f6d52239da39526212e --- /dev/null +++ b/swap/aib/5ed30361-edaa-4da9-87ce-53a9e4006748.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + +char s[50100], sfin[50100]; + +int ind[26], perm[50100], AIB[50100]; + + + +inline int lsb(int X) + +{ + + return X & -X; + +} + + + +int query(int x) + +{ + + int cnt = 0; + + + + while (x) + + { + + cnt += AIB[x]; + + x = x - lsb(x); + + } + + + + return cnt; + +} + + + +void update(int x, int N) + +{ + + while (x <= N) + + { + + AIB[x] ++; + + x += lsb(x); + + } + +} + + + +int main() + +{ + + int i, N; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50001, stdin); fgets(sfin, 50001, stdin); + + N = strlen(s + 1); + + + + for (i = 1; i <= N; i ++) + + { + + int now = s[i] - 'a'; + + while (ind[now] <= N) + + { + + ++ ind[now]; + + if (sfin[ind[now]] == s[i]) + + break; + + } + + if (ind[now] == N + 1) + + { + + printf("-1"); + + return 0; + + } + + perm[i] = ind[now]; + + } + + + + int sol = 0; + + for (i = 1; i <= N; i ++) + + { + + sol += i - 1 - query(perm[i]); + + update(perm[i], N); + + } + + + + printf("%d", sol); + + return 0; + +} diff --git a/swap/aib/6d2960f8-4c1a-4a37-81fa-a1ebf6d3f80a.cpp b/swap/aib/6d2960f8-4c1a-4a37-81fa-a1ebf6d3f80a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..caf414e681210dddde7341e44de704dd4a542645 --- /dev/null +++ b/swap/aib/6d2960f8-4c1a-4a37-81fa-a1ebf6d3f80a.cpp @@ -0,0 +1,103 @@ +#include +#include + + + + +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +char s1[50006],s2[50006]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[30][50001]; + +int n,sol; + +int query (int p){ + + int s=0; + + for (int i=n;i>0;i-=(i&(-i))) + + s=s+a[i]; + + for (int i=p;i>0;i-=(i&(-i))) + + s=s-a[i]; + + return s; + +} + +void update (int p){ + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int main() + +{ + + fin>>s1+1; + + fin>>s2+1; + + n=strlen(s1+1); + + for (int i=1;i<=n;i++){ + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26;i++){ + + if (f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for (int i=1;i<=n;i++){ + + f2[s2[i]-'a'+1]++; + + int x=m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + + + + + update (x); + + sol+=query(x); + + } + + fout< +#include +using namespace std; + + + +int vf1[26],vf2[26]; +char v1[50000 +1],v2[50000 +1]; +int aib[50000 +1]; +int last[26]; +vector P[26]; +inline void update(int poz){ + int i; + for(i=poz;i<=50000;i+=(i&(-i))) + aib[i]++; +} +inline int query(int poz){ + int i,sum; + sum=0; + for(i=poz;i>0;i-=(i&(-i))) + sum+=aib[i]; + return sum; +} +int main(){ + FILE*fi,*fout; + int n1,n2,poz,i,ans; + char a; + fi=fopen("swap.in" ,"r"); + fout=fopen("swap.out" ,"w"); + a=fgetc(fi); + n1=0; + while(a!='\n'){ + a-='a'; + vf1[a]++; + v1[++n1]=a; + a=fgetc(fi); + } + a=fgetc(fi); + n2=0; + while(a!='\n'){ + a-='a'; + vf2[a]++; + v2[++n2]=a; + P[a].push_back(n2); + a=fgetc(fi); + } + i=0; + while(i<26&&vf1[i]==vf2[i]) + i++; + if(i<26) + fprintf(fout,"-1"); + else{ + ans=0; + for(i=1;i<=n1;i++){ + poz=P[v1[i]][last[v1[i]]]; + ans+=(poz+i-1-query(poz-1))-i; + last[v1[i]]++; + update(poz); + } + fprintf(fout,"%d" ,ans); + } + fclose(fi); + fclose(fout); + return 0; +} diff --git a/swap/aib/70a97f65-0f03-4366-a3d2-8802ad072183.cpp b/swap/aib/70a97f65-0f03-4366-a3d2-8802ad072183.cpp new file mode 100644 index 0000000000000000000000000000000000000000..effaf71ccf5e6243d1cbeb78f5c37ec37ee4e351 --- /dev/null +++ b/swap/aib/70a97f65-0f03-4366-a3d2-8802ad072183.cpp @@ -0,0 +1,104 @@ +#include +#include +#include +using namespace std; + + + +char s[50005], S[50005]; + +int lgt, sol, aib[50005], P[50005], poz[26]; + +vector v[26]; + + + +inline int abs(int a) + +{ + + if(a < 0) return -a; + + return a; + +} + + + +int query(int poz) + +{ + + int sol = 0; + + for(int x = poz; x; x -= (x & (-x))) + + sol += aib[x]; + + return sol; + +} + + + +void update(int poz) + +{ + + for(int x = poz; x <= lgt; x += (x & (-x))) + + aib[x]++; + +} + + + +int main() + +{ + + ifstream in("swap.in"); in.getline(s + 1, 50005); in.getline(S + 1, 50005); in.close(); + + lgt = strlen(s + 1); + + for(int i = 1; i <= lgt; i++) v[s[i] - 'a'].push_back(i); + + ofstream out("swap.out"); + + for(int i = 1; i <= lgt; i++) + + { + + if(v[S[i] - 'a'].size() != poz[S[i] - 'a']) + + P[v[S[i] - 'a'][poz[S[i] - 'a']++]] = i; + + else + + { + + out<<"-1"; + + out.close(); + + return 0; + + } + + } + + for(int i = 1; i <= lgt; i++) + + { + + sol += query(lgt) - query(P[i]); + + update(P[i]); + + } + + out< + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s1[50005]; + +char s2[50005]; + +int fr1[26]; + +int fr2[26]; + +int v[50005]; + +int app[26][50005]; + +int p[50005]; + +int AIB[50005]; + +int n; + +void update(int i) + +{ + + while(i<=n) + + { + + AIB[i]++; + + i+=(i&(-i)); + + } + +} + +int query(int i) + +{ + + int s=0; + + while(i>0) + + { + + s+=AIB[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + f>>(s1+1); + + int i; + + for(i=1;s1[i];i++) + + { + + ++n; + + v[i]=++fr1[s1[i]-'a']; + + } + + f>>(s2+1); + + for(i=1;s2[i];i++) + + app[s2[i]-'a'][++fr2[s2[i]-'a']]=i; + + for(i=0;i<26;i++) + + if(fr1[i]!=fr2[i]) + + { + + g<<-1; + + return 0; + + } + + for(i=1;i<=n;i++) + + p[i]=app[s1[i]-'a'][v[i]]; + + int ans=0; + + for(i=n;i;i--) + + { + + ans+=query(p[i]); + + update(p[i]); + + } + + g< +using namespace std; + +int n,i,j,sol,aux,l; + +vector poz[30]; + +int p[30]; + +int aib[50005]; + +char s1[50005],s2[50005]; + +void update(int pos) + +{ + + int i; + + for(i=pos;i<=n;i+=((i)&(-i))) + + aib[i]++; + +} + +int query(int pos) + +{ + + int i; + + int sum=0; + + for(i=pos;i>0;i-=((i)&(-i))) + + sum+=aib[i]; + + return sum; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s\n%s",s1+1,s2+1); + + n=strlen(s1+1); + + if(strlen(s1+1)!=strlen(s2+1)) + + { + + printf("-1"); + + return 0; + + } + + for(i=1;i<=n;i++) + + poz[((s1[i])-'a')].push_back(i); + + for(i=1;i<=n;i++) + + { + + l=((s2[i])-'a'); + + if(p[l]>=(int)poz[l].size()) + + { + + printf("-1"); + + return 0; + + } + + sol+=abs(i-(poz[l][p[l]]+query(n)-query(poz[l][p[l]]))); + + update(poz[l][p[l]]); + + p[l]++; + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/753e5b3c-18b1-4c4a-9bb0-81f1aafd5164.cpp b/swap/aib/753e5b3c-18b1-4c4a-9bb0-81f1aafd5164.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc474d14bd0d13bbde5f8dcf6089ff8236fbc951 --- /dev/null +++ b/swap/aib/753e5b3c-18b1-4c4a-9bb0-81f1aafd5164.cpp @@ -0,0 +1,108 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +vector v1[30], v2[30]; + +int vec[50005], n, AIB[50005]; + + + +void xAdd(int poz){ + + for(int i = poz; i <= n; i += i & -i) + + AIB[i] ++; + +} + + + +int xCount(int poz){ + + int rez = 0; + + for(int i = poz; i >= 1; i -= i & -i) + + rez += AIB[i]; + + return rez; + +} + + + +int main() + +{ + + string a, b; + + fin >> a >> b; + + + + n = a.size(); + + for(int i = 0; i < n; ++i) + + v1[a[i] - 'a'].push_back(i); + + for(int i = 0; i < n; ++i) + + v2[b[i] - 'a'].push_back(i); + + for(int i = 0; i < 26; ++i) + + if(v1[i].size() > 0) + + { + + if(v1[i].size() != v2[i].size()) + + { + + fout << -1 << '\n'; + + return 0; + + } + + for(int j = 0; j < v1[i].size(); ++j) + + vec[v1[i][j]] = v2[i][j]; + + } + + for(int i = 0; i < n; ++i) + + vec[i] ++; + + int rez = 0; + + for(int i = n - 1; i >= 0; --i){ + + rez += xCount(vec[i] - 1); + + xAdd(vec[i]); + + } + + fout << rez << '\n'; + + return 0; + +} diff --git a/swap/aib/7613a717-5462-4f36-9f91-e38785b4c8ef.cpp b/swap/aib/7613a717-5462-4f36-9f91-e38785b4c8ef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47d9ce4616fcd1981ab50ce9594fbaea4e69164d --- /dev/null +++ b/swap/aib/7613a717-5462-4f36-9f91-e38785b4c8ef.cpp @@ -0,0 +1,92 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int i,sol,n,A[50001]; + +char s[50001],t[50001]; + +queue L[30]; + + + +void update(int p) + +{ + + for (int i=p; i<=n; i+=(i&-i)) + + A[i]++; + +} + + + +int query(int p) + +{ + + int sol = 0; + + for (int i=p; i>=1; i-=(i&-i)) + + sol += A[i]; + + return sol; + +} + + + +int main() + +{ + + fin >> s+1 >> t+1; + + n = strlen(t+1); + + for (i=1; t[i]!=0; i++) + + L[t[i]-'a'+1].push(i); + + for (i=1; s[i]!=0; i++) + + { + + if (L[s[i]-'a'+1].size() == 0) + + { + + fout << -1; + + return 0; + + } + + int x = L[s[i]-'a'+1].front(); + + sol += x-1-query(x); + + update(x); + + L[s[i]-'a'+1].pop(); + + } + + fout << sol; + + return 0; + +} diff --git a/swap/aib/771ab843-9a7b-4ec1-9f5b-db09e274e28c.cpp b/swap/aib/771ab843-9a7b-4ec1-9f5b-db09e274e28c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7cec99beb97527e66004a778081b289d97a6a155 --- /dev/null +++ b/swap/aib/771ab843-9a7b-4ec1-9f5b-db09e274e28c.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + + + +const int nmax = 50005; + +const int alpha = 30; + + + +int n, i, sol, a[nmax], aib[nmax]; + +int p[alpha], f1[alpha], f2[alpha]; + +string s, t; + + + +vector v[alpha]; + + + +void update(int x) + +{ + + for(int i = x; i <= n; i += i & (-i)) + + aib[i]++; + +} + + + +int query(int x) + +{ + + int r = 0; + + for(int i = x; i; i -= i & (-i)) + + r += aib[i]; + + return r; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> s; + + fin >> t; + + + + n = s.size(); + + + + for(i = 0; i < n; i++) + + { + + f1[s[i] - 'a']++; + + f2[t[i] - 'a']++; + + } + + + + for(i = 0; i < 26; i++) + + if(f1[i] != f2[i]) + + { + + fout << "-1\n"; + + return 0; + + } + + + + for(i = 0; i < n; i++) + + v[s[i] - 'a'].push_back(i + 1); + + + + for(i = 0; i < n; i++) + + a[i] = v[t[i] - 'a'][p[t[i] - 'a']++]; + + + + for(i = n - 1; i >= 0; i--) + + { + + sol += query(a[i] - 1); + + update(a[i]); + + } + + + + fout << sol; + + + + return 0; + +} diff --git a/swap/aib/7746cc70-35c5-4fa2-95fa-aec0c2fc0e99.cpp b/swap/aib/7746cc70-35c5-4fa2-95fa-aec0c2fc0e99.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796a1357148cc5ce8b495af7cbbc2122e297762e --- /dev/null +++ b/swap/aib/7746cc70-35c5-4fa2-95fa-aec0c2fc0e99.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + + + + + +char buf[16384]; + +int pbuf=16384; + +FILE*fi,*fo; + +inline char nextch(){ + + if(pbuf==16384){ + + fread(buf, 16384, 1, fi); + + pbuf=0; + + } + + return buf[pbuf++]; + +} + +inline long long nextnum(){ + + long long a=0; + + char c=nextch(); + + while((c<'0' || c>'9') && c!='-') + + c=nextch(); + + int semn=1; + + if(c=='-'){ + + semn=-1; + + c=nextch(); + + } + + while('0'<=c && c<='9'){ + + a=a*10+c-'0'; + + c=nextch(); + + } + + return a*semn; + +} + + + + + +int aib[50000 +1], n; + +inline void add(int poz,int nr){ + + for(int i=poz;i<=n;i+=(((i-1)^i)&i)) + + aib[i]+=nr; + +} + +inline int sum(int poz){ + + int s=0; + + for(int i=poz;i>0;i-=(((i-1)^i)&i)) + + s=s+aib[i]; + + return s; + +} + + + +char v1[50000 +1], v2[50000 +1]; + +int f1[27], f2[27]; + +int p[50000 +1]; + +std::vector poz[27]; + +int last[27]; + +int main(){ + + fi=fopen("swap.in","r"); + + fo=fopen("swap.out","w"); + + char c=nextch(); + + int n1=0; + + while('a'<=c && c<='z'){ + + v1[++n1]=c-'a'+1; + + f1[c-'a'+1]++; + + c=nextch(); + + } + + + + c=nextch(); + + int n2=0; + + while('a'<=c && c<='z'){ + + v2[++n2]=c-'a'+1; + + poz[c-'a'+1].push_back(n2); + + f2[c-'a'+1]++; + + c=nextch(); + + } + + + + int z=1; + + while(z<27 && f1[z]==f2[z]) + + z++; + + if(z!=27){ + + fprintf(fo,"-1"); + + return 0; + + } + + for(int i=1;i<=n1;i++) + + p[i]=poz[v1[i]][last[v1[i]]++]; + + int ans=0; + + n=n1; + + for(int i=n1;i>0;i--){ + + ans+=sum(p[i]); + + + + add(p[i], 1); + + } + + fprintf(fo,"%d", ans); + + fclose(fi); + + fclose(fo); + + return 0; + +} diff --git a/swap/aib/775e4aac-4099-4829-a1c5-933ad5ddb1a9.cpp b/swap/aib/775e4aac-4099-4829-a1c5-933ad5ddb1a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..231a3c5ce240db9b4600655ee6b54e5ff2a4f9f8 --- /dev/null +++ b/swap/aib/775e4aac-4099-4829-a1c5-933ad5ddb1a9.cpp @@ -0,0 +1,116 @@ +#include +#include +#include + + +using namespace std; + + + + + + + +char a[50100],b[50100]; + +vector p[26]; + +int x[26], v[50100], n; + + + +void update(int x,int val) + + { + + for(;x<=n;x+=x^(x-1)&x) + + v[x] += val; + + } + + + +int query(int x) + + { + + int r = 0; + + for(;x;x-=x^(x-1)&x) + + r += v[x]; + + return r; + + } + + + +int main() + + { + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + int i, tmp, S = 0; + + + + fgets(a,50100,stdin); + + fgets(b,50100,stdin); + + + + for(i=0;a[i]!='\n';++i) + + p[a[i]-'a'].push_back(i+1); + + n = i; + + + + for(i=1;i<=n;++i) + + update(i,1); + + + + for(i=0;b[i]!='\n';++i) + + { + + if(x[b[i]-'a'] == p[b[i]-'a'].size()) + + { + + printf("-1\n"); + + return 0; + + } + + tmp = p[b[i]-'a'][x[b[i]-'a']]; + + ++x[b[i]-'a']; + + S += query(tmp-1); + + update(tmp,-1); + + } + + + + printf("%d\n",S); + + + + return 0; + + } diff --git a/swap/aib/7a7e5332-8677-417c-a52a-1564484ec1bb.cpp b/swap/aib/7a7e5332-8677-417c-a52a-1564484ec1bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc92ee73c61a912686602efce9f4e14d9034a5b0 --- /dev/null +++ b/swap/aib/7a7e5332-8677-417c-a52a-1564484ec1bb.cpp @@ -0,0 +1,183 @@ +#include +#include +#include +#include +#include + + +using namespace std; +list v[51001]; + +char a[51001],b[51001]; + +int p[51001],aib[51001]; + +int n; + + + +void citire() + +{ + + int i,j; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + scanf("%s\n", a+1); + + n=strlen(a+1); + + scanf("%s\n", b+1); + +} + + + +void add(int x, int val) + +{ + + for (;x<=n;x+=((x^(x-1))&x)) + + aib[x]+=val; + +} + + + +int compute(int x) + +{ + + int ret=0; + + for (;x>0;x-=((x^(x-1))&x)) + + ret+=aib[x]; + + return ret; + +} + + + +bool verif() + +{ + + int i; + + int v1[26]; + + memset(v1,0,sizeof(v1)); + + for (i=1;i<=n;++i) + + v1[a[i]-'a']++; + + for (i=1;i<=n;++i) + + v1[b[i]-'a']--; + + for (i=0;i<26;++i) + + if (v1[i]!=0) + + return false; + + return true; + +} + + + +void solve() + +{ + + int i,rez=0,j,ok; + + + + for (i=1;i<=n;++i) + + a[i]=a[i]-'a'; + + for (i=1;i<=n;++i) + + b[i]=b[i]-'a'; + + memset(p,0,sizeof(p)); + + int nr=0; + + for (i=1;i<=n;++i) + + v[a[i]].push_back(i); + + for (i=1;i<=n;++i) + + { + + p[i]=*(v[b[i]].begin()); + + v[b[i]].pop_front(); + + } + + for (i=1;i<=n;++i) + + add(i,1); + + for (i=1;i<=n;++i) + + { + + rez+=compute(p[i]); + + rez-=1; + + add(p[i],-1); + + } + + + + printf("%d", rez); + +} + + + + + +int main() + +{ + + citire(); + + if (!verif()) + + printf("-1\n"); + + else + + solve(); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/aib/7a9b6ff7-188b-4cd0-9cb1-6ca654df913e.cpp b/swap/aib/7a9b6ff7-188b-4cd0-9cb1-6ca654df913e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ca9bd46abffd073b4a2c1dde1a8c54a9e406e7fb --- /dev/null +++ b/swap/aib/7a9b6ff7-188b-4cd0-9cb1-6ca654df913e.cpp @@ -0,0 +1,78 @@ +#include +using namespace std; + +string s,s1; + +queue q[30]; + +int z(int i) + +{ + + return i&(-i); + +} + +int arb[100100],rez; + +void add(int i) + +{ + + for(; i <= 100000; i += z(i)) + + arb[i]++; + +} + +int get(int i) + +{ + + int ans = 0; + + for (;i; i-= z(i)) + + ans+= arb[i]; + + return ans; + +} + +int main() + +{ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + cin >> s >> s1; + + for (int i = 0 ; i < s.length(); i++) + + { + + q[int(s[i] - 'a')].push(i+1); + + } + + for (int i = 0 ; i < s1.length();i++) + + { + + if (q[s1[i] - 'a'].size() == 0) return cout<<-1,0; + + int poz = q[s1[i] - 'a'].front(); + + q[s1[i] - 'a'].pop(); + + rez += poz-i-1 + get(s.length()) - get(poz); + + add(poz); + + } + + cout << rez; + +} diff --git a/swap/aib/7c9e9097-2c5d-4a6c-af76-fdfb462b93b7.cpp b/swap/aib/7c9e9097-2c5d-4a6c-af76-fdfb462b93b7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4196fad4212d41a961210c895318ce1bdd04949 --- /dev/null +++ b/swap/aib/7c9e9097-2c5d-4a6c-af76-fdfb462b93b7.cpp @@ -0,0 +1,65 @@ +#include +#include +using namespace std; + + + +int vf1[26],vf2[26]; +char v1[50000 +1],v2[50000 +1]; +int aib[50000 +1]; +int last[26]; +vector P[26]; +inline void update(int poz){ + int i; + for(i=poz;i<=50000;i+=(i&(-i))) + aib[i]++; +} +inline int query(int poz){ + int i,sum; + sum=0; + for(i=poz;i>0;i-=(i&(-i))) + sum+=aib[i]; + return sum; +} +int main(){ + FILE*fi,*fout; + int n1,n2,poz,i,ans; + char a; + fi=fopen("swap.in" ,"r"); + fout=fopen("swap.out" ,"w"); + a=fgetc(fi); + n1=0; + while(a!='\n'){ + a-='a'; + vf1[a]++; + v1[++n1]=a; + a=fgetc(fi); + } + a=fgetc(fi); + n2=0; + while(a!='\n'){ + a-='a'; + vf2[a]++; + v2[++n2]=a; + P[a].push_back(n2); + a=fgetc(fi); + } + i=0; + while(i<26&&vf1[i]==vf2[i]) + i++; + if(i<26) + fprintf(fout,"-1"); + else{ + ans=0; + for(i=1;i<=n1;i++){ + poz=P[v1[i]][last[v1[i]]]; + ans+=(poz+i-1-query(poz-1))-i; + last[v1[i]]++; + update(poz); + } + fprintf(fout,"%d" ,ans); + } + fclose(fi); + fclose(fout); + return 0; +} diff --git a/swap/aib/7d4502ad-fe19-462d-8ce8-f81f1c98cdd4.cpp b/swap/aib/7d4502ad-fe19-462d-8ce8-f81f1c98cdd4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c66340975b75f31f9e9467f424167280bfa27156 --- /dev/null +++ b/swap/aib/7d4502ad-fe19-462d-8ce8-f81f1c98cdd4.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout("swap.out"); + + + +int arb[100100]; + + + +void add(int i){ + + for(; i < 100000; i+=(i & (-i))) + + arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) + + ans +=arb[i]; + + return ans; + +} + + + + + +string s, t; + + + +queue q[30]; + + + +int main(){ + + + + getline(cin,s); + + getline(cin,t); + + + + for(int i = 0; i < s.length(); i++) + + q[s[i]-'a'].push(i+1); + + + + int rs = 0; + + + + for(int i = 0; i < t.length(); i++) + + { + + if(q[t[i]-'a'].empty()) return(cout << -1, 0); + + int pos = q[t[i]-'a'].front(); q[t[i]-'a'].pop(); + + rs += pos - i - 1 + get(s.length()) - get(pos); + + add(pos); + + } + + + + cout << rs; + + return (0); + +} diff --git a/swap/aib/7ebe6c28-e50e-4922-b4d4-47d59116566b.cpp b/swap/aib/7ebe6c28-e50e-4922-b4d4-47d59116566b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..efbeee7311eb9520ca3960f6094e5b3532669db9 --- /dev/null +++ b/swap/aib/7ebe6c28-e50e-4922-b4d4-47d59116566b.cpp @@ -0,0 +1,296 @@ +#include +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 = p2 & (p2 - 1); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/7febb286-500a-4b7d-94c7-975069b31825.cpp b/swap/aib/7febb286-500a-4b7d-94c7-975069b31825.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb564cffd3b2b4d3bf28b79c22f57c81c7257c41 --- /dev/null +++ b/swap/aib/7febb286-500a-4b7d-94c7-975069b31825.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +using namespace std; + + + +const int NMAX = 50003; + + + +queue q[26]; + +int N, aib[NMAX]; + +char s[NMAX]; + + + +inline void AIB_build () { + + + + int i, j; + + for (i = 1; i <= N; ++i) + + for (j = i; j <= N; j += j & -j) + + ++aib[j]; + + + +} + + + +inline void AIB_update (int k) { + + + + for (; k <= N; k += k & -k) + + --aib[k]; + + + +} + + + +inline int AIB_query (int k) { + + + + int r = 0; + + for (; k; k -= k & -k) + + r += aib[k]; + + return r; + + + +} + + + +int main () { + + + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + int i, r = 0; + + scanf ("%s", s + 1); + + N = strlen (s + 1); + + for (i = 1; i <= N; ++i) + + q[s[i] - 'a'].push (i); + + scanf ("%s", s + 1); + + if (strlen (s + 1) != N) + + printf ("-1\n"), + + exit (EXIT_SUCCESS); + + AIB_build (); + + for (i = 1; i <= N; ++i) { + + if (q[s[i] - 'a'].empty ()) + + printf ("-1\n"), + + exit (EXIT_SUCCESS); + + r += AIB_query (q[s[i] - 'a'].front ()) - 1; + + AIB_update (q[s[i] - 'a'].front ()); + + q[s[i] - 'a'].pop (); + + } + + printf ("%d", r); + + + +} diff --git a/swap/aib/7ffa5c69-03ca-49b3-a1a7-efaae8cca7d1.cpp b/swap/aib/7ffa5c69-03ca-49b3-a1a7-efaae8cca7d1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..848084c1c45674fdaf80e91d6ef37cc27f32752c --- /dev/null +++ b/swap/aib/7ffa5c69-03ca-49b3-a1a7-efaae8cca7d1.cpp @@ -0,0 +1,151 @@ +#include +#include + + + + + + +using namespace std; + + + +long n, i, j, k, l, sol; + +char s1[51000], s2[51000]; + +long p[29], v[51000], aib[51000]; + +vector poz[29]; + + + +int lsb(int x) + +{ + + return (x&(x-1))^x; + +} + + + +void update(int x) + +{ + + while(x<=l) + + { + + aib[x]++; + + x=x+lsb(x); + + } + +} + + + +int query(int x) + +{ + + int rez=0; + + while(x) + + { + + rez+=aib[x]; + + x=x-lsb(x); + + } + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + l=1; + + scanf("%c", &s1[l]); + + while(s1[l]>='a') + + { + + l++; + + scanf("%c", &s1[l]); + + } + + l--; + + for(i=1; i<=l; i++) + + { + + scanf("%c", &s2[i]); + + poz[s2[i]-'a'].push_back(i); + + aib[i]=0; + + } + + for(i=1; i<=l; i++) + + { + + k=s1[i]-'a'; + + if(p[k]==poz[k].size()) + + { + + printf("-1\n"); + + return 0; + + } + + else + + { + + v[i]=poz[k][p[k]]; + + p[k]++; + + } + + } + + for(i=1; i<=l; i++) + + { + + sol+=(i-1-query(v[i])); + + update(v[i]); + + } + + printf("%d\n", sol); + + return 0; + +} diff --git a/swap/aib/801c0f4e-f841-4661-8381-ef49250dbe16.cpp b/swap/aib/801c0f4e-f841-4661-8381-ef49250dbe16.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e252a24faf50ca3a748182a98fdc2b9802c59388 --- /dev/null +++ b/swap/aib/801c0f4e-f841-4661-8381-ef49250dbe16.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int i,n,sol,k; + +int v[50005],aib[50005]; + +char c[50005],d[50005]; + +dequeH[27]; + +void update(int p){ + + for(;p<=n;p+=(p&-p)){ + + aib[p]++; + + } + +} + +int querry(int p){ + + int r=0; + + for(;p;p-=(p&-p)){ + + r+=aib[p]; + + } + + return r; + +} + +int main(){ + + fin>>c+1>>d+1; + + n=strlen(c+1); + + for(i=1;c[i]!=0;i++){ + + H[c[i]-'a'+1].push_back(i); + + } + + for(i=1;i<=n;i++){ + + if(H[d[i]-'a'+1].empty()){ + + fout<<"-1"; + + return 0; + + } + + k=H[d[i]-'a'+1].front(); + + H[d[i]-'a'+1].pop_front(); + + sol+=k-querry(k)-1; + + update(k); + + } + + fout< +#include +#include +#include + + + +const int MAX_LENGTH = 50000; +const int SIGMA = 26; + +int n; + +class FenwickTree { +public: + int a[1 + MAX_LENGTH]; + + FenwickTree() { + memset(a, 0, sizeof(a)); + } + + void update(int poz, int val) { + for (int i = poz; i <= n; i += i & -i) { + a[i] += val; + } + } + + int query(int poz) { + int ans = 0; + for (int i = poz; i >= 1; i -= i & -i) { + ans += a[i]; + } + return ans; + } + + int query(int st, int dr) { + return query(dr) - query(st - 1); + } +}; + +int f1[SIGMA], f2[SIGMA]; + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + char s1[MAX_LENGTH + 1], s2[MAX_LENGTH + 1]; + scanf("%s%s", s1, s2); + if (strlen(s1) != strlen(s2)) { + printf("-1"); + return 0; + } + n = (int)strlen(s1); + std::queue poz[SIGMA]; + for (int i = 0; i < n; i++) { + f1[s1[i] - 'a']++; + f2[s2[i] - 'a']++; + poz[s1[i] - 'a'].push(i + 1); + } + for (char alph = 'a'; alph <= 'z'; alph++) { + if (f1[alph - 'a'] != f2[alph - 'a']) { + printf("-1"); + return 0; + } + } + FenwickTree t; + int ans = 0; + for (int i = 0; i < n; i++) { + int index = poz[s2[i] - 'a'].front(); + poz[s2[i] - 'a'].pop(); + ans += index - i - 1 + t.query(index + 1, n); + t.update(index, 1); + } + printf("%d\n", ans); + return 0; +} diff --git a/swap/aib/81fefd6c-0942-46b6-803e-90233b3f71d0.cpp b/swap/aib/81fefd6c-0942-46b6-803e-90233b3f71d0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c90d83b9061a5eb32ed99b729b3ec6d37566a9c3 --- /dev/null +++ b/swap/aib/81fefd6c-0942-46b6-803e-90233b3f71d0.cpp @@ -0,0 +1,176 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int n, sol, sum, Nrc[30], P[50010], ARBI[50010 * 3]; + +string S1, S2; + +queue < int > V[30]; + + + +void Update(int nod, int left, int right, int poz, int value) + +{ + + if (left == right) + + { + + ARBI[nod] = value; + + return; + + } + + + + int mid = (left + right) >> 1; + + if (poz <= mid) + + { + + Update(nod * 2, left, mid, poz, value); + + } + + else + + { + + Update(nod * 2 + 1, mid + 1, right, poz, value); + + } + + ARBI[nod] = ARBI[nod * 2] + ARBI[nod * 2 + 1]; + +} + + + +void Query(int nod, int left, int right, int st, int dr) + +{ + + if (st <= left && right <= dr) + + { + + sum += ARBI[nod]; + + return; + + } + + + + int mid = (left + right) >> 1; + + if (st <= mid) + + { + + Query(nod * 2, left, mid, st, dr); + + } + + if (dr > mid) + + { + + Query(nod * 2 + 1, mid + 1, right, st, dr); + + } + +} + + + +int main() + +{ + + fin >> S1 >> S2; + + n = S1.size(); + + for (int i = 0; i < n; i ++) + + { + + Nrc[S1[i] - 'a'] ++; + + Nrc[S2[i] - 'a'] --; + + V[S2[i] - 'a'].push(i); + + } + + for (int i = 0; i < 30; i ++) + + { + + if (Nrc[i] != 0) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + } + + for (int i = 0; i < n; i ++) + + { + + P[i + 1] = V[S1[i] - 'a'].front() + 1; + + V[S1[i] - 'a'].pop(); + + } + + + + Update(1, 1, n, P[n], 1); + + for (int i = n - 1; i >= 1; i --) + + { + + sum = 0; + + Query(1, 1, n, 1, P[i]); + + sol += sum; + + Update(1, 1, n, P[i], 1); + + } + + + + fout << sol << '\n'; + + fout.close(); + + return 0; + +} diff --git a/swap/aib/83c3e3fd-0762-4f11-9d62-123885da1464.cpp b/swap/aib/83c3e3fd-0762-4f11-9d62-123885da1464.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eed703c9aac2205939871e7003c41fc37b53caed --- /dev/null +++ b/swap/aib/83c3e3fd-0762-4f11-9d62-123885da1464.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +string s1,s2; +queue Q[27]; +int N; +int aib [110000]; + +int prev(int x){ + return x-(x&(-x)); +} + +int next(int x){ + return x+(x&(-x)); +} +int zer(int x){ + return x&(-x); +} + +void update(int k,int newv){ + for (;k<=100000;k=next(k)) aib[k]+=newv; +} + +int query(int k){ + int rs=0; + for (;k;k=prev(k)) rs+=aib[k]; + return rs; +} + +int main(){ + fin >>s1>>s2; + N=s1.length(); + for (int i=0;i +#include +#include +using namespace std; +FILE *f = fopen ( "swap.in", "r" ); + +FILE *g = fopen ( "swap.out", "w" ); + + + +int N, AIB[50002], where[30]; + +char A[50002], B[50002]; + +vector < int > G[30]; + + + +void Update ( int poz ){ + + + + for ( int i = poz; i <= N; i += ( (i) & (-i) ) ) + + AIB[i] ++; + + + +} + + + +int Suma ( int N ){ + + + + int sum = 0; + + + + for ( int i = N; i > 0; i -= ( (i) & (-i) ) ) + + sum += AIB[i]; + + + + return sum; + +} + + + +int SumaInt ( int st, int dr ){ + + return dr - Suma(st-1); + +} + + + +int main(){ + + + + int i, poz, lp, ch, moves = 0; + + + + fscanf ( f, "%s%*c%s", A + 1, B + 1 ); + + + + N = strlen ( A + 1 ); + + + + for ( i = 1; i <= N; ++i ) + + G[A[i]-'a'].push_back ( i ); + + for ( i = 0; i < 'z' - 'a'; ++i ) + + G[i].resize ( G[i].size() ); + + + + + + + + for ( i = 1; i <= N; ++i ){ + + ch = B[i] - 'a'; + + + + if ( where[ch] == G[ch].size() ){ + + fprintf ( g, "-1" ); + + return 0; + + } + + + + poz = G[ch][where[ch]]; + + where[ch]++; + + + + lp = poz; + + poz += SumaInt( poz, i-1 ); + + + + Update ( lp ); + + moves += poz - i; + + } + + + + fprintf ( g, "%d", moves ); + + + + return 0; + +} diff --git a/swap/aib/85587283-bcbe-4a0a-bc3d-a8721e3f8844.cpp b/swap/aib/85587283-bcbe-4a0a-bc3d-a8721e3f8844.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796a1357148cc5ce8b495af7cbbc2122e297762e --- /dev/null +++ b/swap/aib/85587283-bcbe-4a0a-bc3d-a8721e3f8844.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +#include + + + + + + +char buf[16384]; + +int pbuf=16384; + +FILE*fi,*fo; + +inline char nextch(){ + + if(pbuf==16384){ + + fread(buf, 16384, 1, fi); + + pbuf=0; + + } + + return buf[pbuf++]; + +} + +inline long long nextnum(){ + + long long a=0; + + char c=nextch(); + + while((c<'0' || c>'9') && c!='-') + + c=nextch(); + + int semn=1; + + if(c=='-'){ + + semn=-1; + + c=nextch(); + + } + + while('0'<=c && c<='9'){ + + a=a*10+c-'0'; + + c=nextch(); + + } + + return a*semn; + +} + + + + + +int aib[50000 +1], n; + +inline void add(int poz,int nr){ + + for(int i=poz;i<=n;i+=(((i-1)^i)&i)) + + aib[i]+=nr; + +} + +inline int sum(int poz){ + + int s=0; + + for(int i=poz;i>0;i-=(((i-1)^i)&i)) + + s=s+aib[i]; + + return s; + +} + + + +char v1[50000 +1], v2[50000 +1]; + +int f1[27], f2[27]; + +int p[50000 +1]; + +std::vector poz[27]; + +int last[27]; + +int main(){ + + fi=fopen("swap.in","r"); + + fo=fopen("swap.out","w"); + + char c=nextch(); + + int n1=0; + + while('a'<=c && c<='z'){ + + v1[++n1]=c-'a'+1; + + f1[c-'a'+1]++; + + c=nextch(); + + } + + + + c=nextch(); + + int n2=0; + + while('a'<=c && c<='z'){ + + v2[++n2]=c-'a'+1; + + poz[c-'a'+1].push_back(n2); + + f2[c-'a'+1]++; + + c=nextch(); + + } + + + + int z=1; + + while(z<27 && f1[z]==f2[z]) + + z++; + + if(z!=27){ + + fprintf(fo,"-1"); + + return 0; + + } + + for(int i=1;i<=n1;i++) + + p[i]=poz[v1[i]][last[v1[i]]++]; + + int ans=0; + + n=n1; + + for(int i=n1;i>0;i--){ + + ans+=sum(p[i]); + + + + add(p[i], 1); + + } + + fprintf(fo,"%d", ans); + + fclose(fi); + + fclose(fo); + + return 0; + +} diff --git a/swap/aib/8588c5d4-d662-48f5-8bfc-2f9c7a04671d.cpp b/swap/aib/8588c5d4-d662-48f5-8bfc-2f9c7a04671d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99e1784797bf4e5452b2e2e76cda531022fb3a6a --- /dev/null +++ b/swap/aib/8588c5d4-d662-48f5-8bfc-2f9c7a04671d.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +queue que[51]; + +char S[50001], S2[50001]; + +int i, x[50001], S_fr[51], S2_fr[51], sol, n, aib[50001]; + +void update(int pos){ + + while(pos <= n){ + + aib[pos]++; + + pos = pos + (pos & (-pos)); + + } + +} + +int quary(int pos){ + + int s = 0; + + while(pos >= 1){ + + s = s + aib[pos]; + + pos = pos - (pos & (-pos)); + + } + + return s; + +} + +int main() + +{ f >> (S + 1); + + f >> (S2 + 1); + + for(i = 1; S[i] != 0; i++){ + + S_fr[S[i] - 'a' + 1]++; + + S2_fr[S2[i] - 'a' + 1]++; + + } + + for(i = 1; i <= 'z' - 'a' + 1; i++) + + if(S_fr[i] != S2_fr[i]){ + + g << -1 ; + + return 0; + + } + + for(i = 1; S2[i] != 0; i++) + + que[S2[i] - 'a' + 1].push(i); + + for(i = 1; S[i] != 0; i++){ + + x[i] = que[S[i] - 'a' + 1].front(); + + que[S[i] - 'a' + 1].pop(); + + } + + n = strlen(S + 1); + + for(i = 1; i <= n; i++){ + + sol = sol + quary(n) - quary(x[i]); + + update(x[i]); + + } + + g << sol; + + return 0; + +} diff --git a/swap/aib/86f495bb-50b0-4b58-a1eb-33a6aa48d3c2.cpp b/swap/aib/86f495bb-50b0-4b58-a1eb-33a6aa48d3c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8991a34ca655408877a07058b3cca283b126ed63 --- /dev/null +++ b/swap/aib/86f495bb-50b0-4b58-a1eb-33a6aa48d3c2.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int N; + +vector < int > BIT; + + + +void update(int index) { + + for(int i = index; i <= N; i += i & -i) + + ++BIT[i]; + +} + + + +int query(int index) { + + int ans = 0; + + for(int i = index; i > 0; i -= i & -i) + + ans += BIT[i]; + + return ans; + +} + + + +int main() { + + string s, t; + + fin >> s >> t; + + vector < int > fs(26), ft(26); + + for(char x : s) + + ++fs[x - 'a']; + + for(char x : t) + + ++ft[x - 'a']; + + bool ok = true; + + for(int i = 0; i < 26 && ok; ++i) + + if(fs[i] != ft[i]) + + ok = false; + + if(!ok) { + + fout << -1; + + return 0; + + } + + vector < int > from_s[26], from_t[26]; + + N = s.size(); + + s = '0' + s, t = '0' + t; + + for(int i = 1; i <= N; ++i) + + from_s[s[i] - 'a'].emplace_back(i); + + for(int i = 1; i <= N; ++i) + + from_t[t[i] - 'a'].emplace_back(i); + + vector < int > perm(N + 1); + + for(int i = 0; i < 26; ++i) + + for(int j = 0; j < (int)from_t[i].size(); ++j) { + + int poz = from_t[i][j]; + + perm[poz] = from_s[i][j]; + + } + + int ans = 0; + + BIT.resize(N + 1); + + for(int i = N; i > 0; --i) { + + ans += query(perm[i]); + + update(perm[i]); + + } + + fout << ans; + +} diff --git a/swap/aib/87a67e27-cdf2-47f5-bb71-ab977873cd59.cpp b/swap/aib/87a67e27-cdf2-47f5-bb71-ab977873cd59.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8687e8e5ad7553ef21b5c7affc5f175b1917368 --- /dev/null +++ b/swap/aib/87a67e27-cdf2-47f5-bb71-ab977873cd59.cpp @@ -0,0 +1,124 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int p[Nmax] , aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin); n = strlen(a+1); + + + + for (i = 1; i <= n; ++i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = 1; i <= n; ++i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = crt[a[i]-'a']; + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + for (i = n ; i ; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/8841f5b3-015a-42dc-876f-0912a46259da.cpp b/swap/aib/8841f5b3-015a-42dc-876f-0912a46259da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aaea8979118e7ed5b3465dad763698e493a79ee --- /dev/null +++ b/swap/aib/8841f5b3-015a-42dc-876f-0912a46259da.cpp @@ -0,0 +1,27 @@ +#include + +int arb[100100],rs; +using namespace std; +int get(int i){ + int ans=0; + for(;i;i-=(i & (-i))) ans+=arb[i]; + return ans; +} +void add(int i){ + for(;i<100000;i+=(i & (-i))) arb[i]++; +} +queue Q[30]; +string A,B; +int main(){ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + cin >> A >> B; + for(int i = 0;i +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +using namespace std; + + + +int n, i, j, sol, p, x; + +int f[27]; + +char s[50002], a[50002]; + +int aib[50002]; + +int pos[27][50002]; + +int ind[27]; + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50001, stdin); + + fgets(a, 50001, stdin); + + n = strlen(a + 1); + + + + for(i = 1; i <= n; i++) + + { + + f[ s[i] - 'a' ]++; + + f[ a[i] - 'a' ]--; + + } + + + + for(i = 0; i < 26; i++) + + if(f[i] != 0) + + { + + printf("-1"); + + return 0; + + } + + + + for(i = 1; i <= n; i++) + + pos[ s[i] - 'a' ][ ++ind[ s[i] - 'a' ] ] = i; + + + + for(i = 0; i < 26; i++) + + ind[i] = 1; + + + + for(i = 1; i <= n; i++) + + { + + p = pos[ a[i] - 'a' ][ ind[ a[i] - 'a' ]++ ]; + + sol += p - 1; + + for(j = p - 1; j >= 1; j -= (j & (-j))) + + sol -= aib[j]; + + for(j = p; j <= n; j += (j & (-j))) + + aib[j]++; + + } + + + + printf("%d", sol); + + + + return 0; + +} diff --git a/swap/aib/89a24027-ac93-4eb1-b17e-c2cdf8e3bf01.cpp b/swap/aib/89a24027-ac93-4eb1-b17e-c2cdf8e3bf01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afb64cf52e1d07601df8cf489e238a1f5b5e2b81 --- /dev/null +++ b/swap/aib/89a24027-ac93-4eb1-b17e-c2cdf8e3bf01.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int L[26][50010]; + +int F[26], G[26], P[50010]; + +int AIB[50010]; + +char A[50010], B[50010]; + + + +int n, i, j; + +long long sol; + + + +int query(int p) { + + int r = 0; + + for (;p;p -= (p&-p)) + + r += AIB[p]; + + return r; + +} + + + +void update(int p) { + + for (;p<=n;p += (p&-p)) + + AIB[p]++; + +} + + + +int main() { + + fin>>A+1; + + fin>>B+1; + + n = strlen(A+1); + + for (i=1;i<=n;i++) { + + A[i] -= 'a'; + + B[i] -= 'a'; + + } + + + + for (i=1;i<=n;i++) { + + F[A[i]]++; + + L[ A[i] ][ F[A[i]] ] = i; + + } + + + + for (i=1;i<=n;i++) { + + G[ B[i] ]++; + + P[ L[ B[i] ][ G[ B[i] ] ] ] = i; + + } + + + + for (i=0;i<26;i++) { + + if (F[i] != G[i]) { + + fout<<-1; + + return 0; + + } + + } + + + + for (i=1;i<=n;i++) + + P[i] = n-P[i]+1; + + + + for (i=1;i<=n;i++) { + + sol += query(P[i]); + + update(P[i]); + + } + + + + fout< +#include +#include +#include + + + + + + +using namespace std; + +typedef int var; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +deque IND['z'-'a' + 1]; + +var n; + +var A[50001]; + + + +inline var zeros(var v) { + + return (v&(-v)); + +} + + + +void adauga(var val, var ind) { + + for(;ind<=n; ind += zeros(ind)) { + + A[ind] += val; + + } + +} + + + + + +var afla(var ind) { + + var s = 0; + + for(;ind; ind -= zeros(ind)) { + + s += A[ind]; + + } + + return s; + +} + + + +var aflaelem(var ind) { + + return afla(ind) - afla(ind-1); + +} + + + +char SIR[50001]; + +void citire() { + + var poz = 0; + + fin>>SIR; + + n = strlen(SIR); + + for(var i=0; i>SIR; + + if(strlen(SIR) != n) {fout<<-1; return;} + + for(var i=0; i +#include +#include +#include +using std::multimap; +using std::make_pair; +using std::sort; + + +int t[50005]; + +void u (int i,int v) +{ + while(i<50005) + t[i]+=v,i+=(i&-i); +} + +int g (int i) +{ + int s=0; + while(i) + s+=t[i],i-=(i&-i); + return s; +} + +int o[50005]; +char a[50005]; +char b[50005]; + +multimap m; + +long long r=0; + +int main (void) +{ + freopen ("swap.in","r",stdin); + + + + + fgets (a, 50001, stdin); + fgets (b, 50001, stdin); + + int n=strlen (a); + for(int i=0;i::iterator it=m.lower_bound (a[i]); + if(a[i]!=it->first){ + printf ("-1"); + return 0; + } + o[i]=it->second; + m.erase (it); + } + + for(int i=0;i + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int nmax = 50005; + +int n, m, aib[nmax]; + +string a, b; + +vector G[30]; + + + +int Query(int i){ + + int sum = 0; + + for (; i > 0; i -= ( (i ^ (i - 1)) & i )){ + + sum += aib[i]; + + } + + return sum; + +} + + + +void Update(int i){ + + for (; i <= n; i += ( (i ^ (i - 1)) & i )){ + + aib[i] += 1; + + } + +} + + + +int main(){ + + fin >> a >> b; + + n = a.size(); + + m = b.size(); + + if (n != m){ + + fout << -1; + + return 0; + + } + + for (int i = n - 1; i >= 0; --i){ + + G[a[i] - 'a'].push_back(i + 1); + + } + + int ans = 0; + + for (int i = 0; i < m; ++i){ + + if (G[b[i] - 'a'].size() == 0){ + + fout << -1; + + return 0; + + } + + int index = G[b[i] - 'a'].back(); + + G[b[i] - 'a'].pop_back(); + + int index2 = index + Query(n) - Query(index - 1); + + ans += (index2 - (i + 1)); + + Update(index); + + } + + fout << ans; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/aib/8f5eeec9-9a45-49a9-a76c-5c8cc797548b.cpp b/swap/aib/8f5eeec9-9a45-49a9-a76c-5c8cc797548b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f382c5ab1b47ce902912e9adbc371be9c79ab3d7 --- /dev/null +++ b/swap/aib/8f5eeec9-9a45-49a9-a76c-5c8cc797548b.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 50005; + +int aib[NMAX]; + + + +void update(int toadd, int pos, int n) { + + for(; pos <= n; pos += pos & -pos) + + aib[pos] += toadd; + +} + + + +int query(int pos) { + + int ans = 0; + + for(; pos; pos -= pos & -pos) + + ans += aib[pos]; + + return ans; + +} + + + +int main() { + + string a, b; + + in >> a >> b; + + + + vector fr(30, 0); + + for(int i = 0; i < a.size(); i ++) + + fr[a[i] - 'a'] ++; + + for(int i = 0; i < b.size(); i ++) + + fr[b[i] - 'a'] --; + + bool havesol = 1; + + for(int i = 0; i < 30; i ++) + + if(fr[i] != 0) + + havesol = 0; + + + + if(havesol) { + + vector> pos(30); + + a = " " + a; + + b = " " + b; + + for(int i = 1; i < a.size(); i ++) { + + pos[a[i] - 'a'].push_back(i); + + update(1, i, a.size()); + + } + + + + int ans = 0; + + for(int i = 1; i < a.size(); i ++) { + + int x = pos[b[i] - 'a'].front(); + + pos[b[i] - 'a'].pop_front(); + + + + ans += (query(x) - 1); + + update(-1, x, a.size()); + + } + + out << ans; + + + + } else { + + out << -1; + + } + + + + return 0; + +} diff --git a/swap/aib/8f9000d0-e089-462b-9b1e-4002d9273277.cpp b/swap/aib/8f9000d0-e089-462b-9b1e-4002d9273277.cpp new file mode 100644 index 0000000000000000000000000000000000000000..064ee247aec7482cd0792576f4e859347befe78e --- /dev/null +++ b/swap/aib/8f9000d0-e089-462b-9b1e-4002d9273277.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int n, sol, Nrc[30], P[50010], AIB[50010]; + +string S1, S2; + +queue < int > V[30]; + + + +inline int zeros(int x) + +{ + + return (x ^ (x - 1) & x); + +} + + + +void Update(int poz, int value) + +{ + + while (poz <= n) + + { + + AIB[poz] += value; + + poz += zeros(poz); + + } + +} + + + +int Query(int poz, int sum = 0) + +{ + + while (poz) + + { + + sum += AIB[poz]; + + poz -= zeros(poz); + + } + + + + return sum; + +} + + + +int main() + +{ + + fin >> S1 >> S2; + + n = S1.size(); + + for (int i = 0; i < n; i ++) + + { + + Nrc[S1[i] - 'a'] ++; + + Nrc[S2[i] - 'a'] --; + + V[S2[i] - 'a'].push(i); + + } + + for (int i = 0; i < 30; i ++) + + { + + if (Nrc[i] != 0) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + } + + for (int i = 0; i < n; i ++) + + { + + P[i + 1] = V[S1[i] - 'a'].front() + 1; + + V[S1[i] - 'a'].pop(); + + } + + + + Update(P[n], 1); + + for (int i = n - 1; i >= 1; i --) + + { + + sol += Query(P[i]); + + Update(P[i], 1); + + } + + + + fout << sol << '\n'; + + fout.close(); + + return 0; + +} diff --git a/swap/aib/90f13b0e-8b33-4193-bdcd-5df06ecf08d6.cpp b/swap/aib/90f13b0e-8b33-4193-bdcd-5df06ecf08d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5d9642d486d640032ae043002cbd213cdb5b3065 --- /dev/null +++ b/swap/aib/90f13b0e-8b33-4193-bdcd-5df06ecf08d6.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +list L[26]; + + + +int n; + + + +int sol; + + + +int A[50005]; + + + +char s1[50005], s2[50005]; + + + +void update(int p) { + + for (int i=p; i<=n; i+=(i&(-i))) { + + ++A[i]; + + } + +} + + + +void query(int p) { + + for (int i=p; i>0; i-=(i&(-i))) { + + sol += A[i]; + + } + +} + + + +int main () { + + f >> s1+1; + + f >> s2+1; + + n = strlen(s1+1); + + for (int i=1; i<=n; ++i) { + + L[s1[i] - 'a'].push_back(i); + + } + + for (int i=1; i<=n; ++i) { + + if (L[s2[i]-'a'].empty()) { + + g <<-1; + + return 0; + + } + + int p = *L[s2[i] - 'a'].begin(); + + L[s2[i] - 'a'].pop_front(); + + query(p); + + update(p); + + } + + g << 1LL*n*(n-1)/2 - sol; + + return 0; + +} diff --git a/swap/aib/943f97f8-bced-4806-95b7-25ceffe6a029.cpp b/swap/aib/943f97f8-bced-4806-95b7-25ceffe6a029.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ea26e69e37a21aca0901fcd31e46a0c8a4a4e93 --- /dev/null +++ b/swap/aib/943f97f8-bced-4806-95b7-25ceffe6a029.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + +const int nmax=5e4+3; + +int n,v[nmax],p[nmax],sol,q[30][nmax],poz[30]; + +char a[nmax],b[nmax]; + +inline int usu(int x) + +{ + + return x&(-x); + +} + +void update(int x) + +{ + + while(x<=n) + + { + + ++v[x]; + + x+=usu(x); + + } + +} + +int query(int x) + +{ + + int sol=0; + + while(x) + + { + + sol+=v[x]; + + x-=usu(x); + + } + + return sol; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",(a+1)); + + scanf("%s",(b+1)); + + n=strlen(a+1); + + for(int i=1;i<=n;++i) q[a[i]-'a'][++q[a[i]-'a'][0]]=i; + + for(int i=0;i<=26;++i) poz[i]=1; + + for(int i=1;i<=n;++i) + + { + + if(q[b[i]-'a'][0]+1==poz[b[i]-'a']) + + { + + printf("-1"); + + return 0; + + } + + p[i]=q[b[i]-'a'][poz[b[i]-'a']]; + + ++poz[b[i]-'a']; + + } + + for(int i=1;i<=n;++i) + + { + + sol=sol+i-query(p[i])-1; + + update(p[i]); + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/95c2a5b6-de32-4b46-b67e-5fd76cbe167e.cpp b/swap/aib/95c2a5b6-de32-4b46-b67e-5fd76cbe167e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f1a0a7b3ba68f0e105091dbfcebde58b968da3e --- /dev/null +++ b/swap/aib/95c2a5b6-de32-4b46-b67e-5fd76cbe167e.cpp @@ -0,0 +1,161 @@ +#include +#include + + + + + + +using namespace std; + +char A[50005],B[50005]; + +vector C[26]; + +inline int lit(char x) + +{ + + return x>='a' && x<='z'; + +} + +int a,b,nr1[26],nr2[26],act[26],D[50005],aib[50005],rez; + +void read() + +{ + + fgets(A+1,50005,stdin); + + fgets(B+1,50005,stdin); + + while (lit(A[a+1])) + + { + + a++; + + C[A[a]-'a'].push_back(a); + + nr1[A[a]-'a']++; + + } + + while (lit(B[b+1])) + + { + + b++; + + nr2[B[b]-'a']++; + + } + +} + +inline int verif() + +{ + + int i; + + for (i=0; i<26; i++) + + if (nr1[i]!=nr2[i]) + + return 0; + + return 1; + +} + +int lsb(int x) + +{ + + return x & -x; + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz; i<=b; i+=lsb(i)) + + aib[i]+=val; + +} + +int query(int poz) + +{ + + int i,sum=0; + + for (i=poz; i>0; i-=lsb(i)) + + sum+=aib[i]; + + return sum; + +} + +void solve() + +{ + + int i,l; + + for (i=1; i<=b; i++) + + { + + l=B[i]-'a'; + + D[i]=C[l][act[l]++]; + + } + + for (i=b; i>=1; i--) + + { + + rez+=query(D[i]-1); + + update(D[i],1); + + } + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + read(); + + if (!verif()) + + { + + printf("-1\n"); + + return 0; + + } + + solve(); + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/964fbc28-0f6f-4727-ab45-cb12bf32e322.cpp b/swap/aib/964fbc28-0f6f-4727-ab45-cb12bf32e322.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d70603ea77f196b09ea14486fa75d0674af6775 --- /dev/null +++ b/swap/aib/964fbc28-0f6f-4727-ab45-cb12bf32e322.cpp @@ -0,0 +1,87 @@ +#include +#include + + + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +long long n,m,i,s[125][50010],fa[125],fb[125],f[50010],k,sol; + +char a[50010],b[50010]; + +void update(int x) { + + for (;x<=n;x+=(x&-x)) + + f[x]++; + +} + +int query(int x) { + + int val=0; + + for (;x;x-=(x&-x)) + + val+=f[x]; + + return val; + +} + +int main() { + + fin>>a+1>>b+1; + + n=strlen(a+1), m=strlen(b+1); + + if (n!=m) { + + fout<<"-1"; + + return 0; + + } + + for (i=1;i<=n;i++) { + + s[a[i]][++fa[a[i]]]=i; + + fb[b[i]]++; + + } + + for (i='a';i<='z';i++) { + + if (fa[i]!=fb[i]) { + + fout<<"-1"; + + return 0; + + } + + fb[i]=0; + + } + + for (i=1;i<=n;i++) { + + k=query(s[b[i]][++fb[b[i]]]); + + sol+=(i-1-k); + + update(s[b[i]][fb[b[i]]]); + + } + + fout< #include +#include + + +using namespace std; + +int n,i,nr,sol,u[27],a[50005],AI[200000]; + +char ch,s1[50005],s2[50005]; + +vector f[27]; + + + +void update(int nod,int l,int r,int x){ + + if (l==r){AI[nod]=1;return;} + + int m=(l+r)>>1; + + if (x<=m)update(nod<<1,l,m,x); + + else update((nod<<1)|1,m+1,r,x); + + AI[nod]=AI[nod<<1]+AI[(nod<<1)|1]; + +} + +void query(int nod,int l,int r,int x){ + + if (l>0&&r<=x){nr+=AI[nod];return;} + + int m=(l+r)>>1; + + if (m)query(nod<<1,l,m,x); + + if (m0) + + a[i]=f[ch][u[ch]++]; + + else {printf("-1\n");return 0;} + + } + + for (i=1;i<=n;++i){ + + update(1,1,n,a[i]); + + nr=0; + + query(1,1,n,a[i]); + + sol+=a[i]-nr; + + } + + printf("%d\n",sol); + +return 0; + +} diff --git a/swap/aib/973a37a1-c83f-467b-8474-7207adf82fe0.cpp b/swap/aib/973a37a1-c83f-467b-8474-7207adf82fe0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..728ff749f537095b7ac68e133cf02a708929a97b --- /dev/null +++ b/swap/aib/973a37a1-c83f-467b-8474-7207adf82fe0.cpp @@ -0,0 +1,70 @@ +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int NMAX = 50005; + +int n; + +char s1[NMAX]; char s2[NMAX]; +int cnt1[127]; int cnt2[127]; + +int perm[NMAX]; +int aib[NMAX]; + +vector a[127]; +long long swaps = 0; + +int bit(int x) { return x & -x; } + +void ins(int pos) { + + for( ; pos <= n; pos += bit(pos)) + aib[pos]++; +} + +int query(int pos) { + + int sum = 0; + for( ; pos > 0 ; pos -= bit(pos)) + sum += aib[pos]; + return sum; +} + +int main() { + + fin >> s1 + 1 >> s2 + 1; + n = strlen(s1 + 1); + + for(int i = n ; i >= 1; --i) + a[s2[i]].push_back(i), cnt2[s2[i]]++; + + for(int i = 1; i <= n ; ++i) + cnt1[s1[i]]++; + + for(int i = 'a' ; i <= 'z'; ++i) { + + if(cnt1[i] != cnt2[i]) { + fout << -1 << '\n'; + exit(0); + } + } + + for(int i = 1; i <= n ; ++i) { + + perm[i] = a[s1[i]][ a[s1[i]].size() - 1 ]; + a[s1[i]].pop_back(); + } + + for(int i = 1; i <= n ; ++i) { + swaps += i - 1 - query(perm[i] - 1) ; + ins(perm[i]); + } + + fout << swaps << '\n'; + + return 0; +} diff --git a/swap/aib/98e32de9-6f43-4fa6-9f4f-cf7943afd3b2.cpp b/swap/aib/98e32de9-6f43-4fa6-9f4f-cf7943afd3b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..82a2b62d75358f273bc244c6cb8a5ed77e33024e --- /dev/null +++ b/swap/aib/98e32de9-6f43-4fa6-9f4f-cf7943afd3b2.cpp @@ -0,0 +1,107 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i,l,sol; + +int fr_a[30],fr_b[30],AIB[1<<16],v[26][1<<16]; + +char a[1<<16],b[1<<16]; + +inline int zeros(int x) + +{ + + return ((x^(x-1))&x); + +} + +inline void bagval(int poz,int val) + +{ + + while(poz<=l) + + { + + AIB[poz]+=val; + + poz+=zeros(poz); + + } + +} + +inline int Query(int poz) + +{ + + int suma=0; + + while(poz) + + { + + suma+=AIB[poz]; + + poz-=zeros(poz); + + } + + return suma; + +} + +int main() + +{ + + f>>(a+1)>>(b+1); + + l=strlen(a+1); + + for(i=l;i;--i) + + { + + short w=b[i]-'a'; + + v[w][++v[w][0]]=i; + + bagval(i,1); + + } + + for(i=1;i<=l;++i) + + { + + short w=a[i]-'a'; + + if(!v[w][0]) + + { + + g<<-1; + + return 0; + + } + + sol+=Query(v[w][v[w][0]]-1); + + bagval(v[w][v[w][0]],-1); + + --v[w][0]; + + } + + g< +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +int ft[nmax+1], f1[sigma]; + + + +char a[nmax+2], b[nmax+2]; + +queue v[sigma]; + + + +void update( int p, int n ) { + + for ( ; p<=n; p= 2*p-(p&(p-1)) ){ + + ++ft[p]; + + } + +} + + + +int query( int p ) { + + int sol= 0; + + for ( ; p>0; p&= (p-1) ) { + + sol+= ft[p]; + + } + + return sol; + +} + + + +int main( ) { + + int ans= 0; + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + + + int n; + + for ( n= 0; a[n]>='a' && a[n]<='z'; ++n ) { + + ++f1[a[n]-'a'], --f1[b[n]-'a']; + + } + + for ( int i= 0; i #include +#include +using namespace std; + +const int MAX_N = 50007; + +vector B[27]; + +int AIB[MAX_N], A[27], N; + +char S[MAX_N], T[MAX_N]; + +void update(int x) + +{ + + for(;x<=N;x+=(x&-x)) AIB[x]++; + +} + +inline int query(int x) + +{ + + int sol = 0; + + for(;x;x-=(x&-x)) sol += AIB[x]; + + return sol; + +} + +inline int getP(char x, int y) + +{ + + int c = x - 96; + + if( B[c].size() <= y-1) return -1; + + return B[c][y-1]; + +} + +int sol; + +int main() + +{ + + ifstream in("swap.in"); ofstream out("swap.out"); + + in>>S+1>>T+1; + + int i, P; + + N = strlen(S+1); + + for(i = 1; i <= N; ++i) + + B[ T[i] - 96 ].push_back(i); + + for(i = 1; i <= N; ++i) + + { + + A[ S[i] - 96 ]++; + + P = getP( S[i], A[ S[i] - 96 ] ); + + if(P == -1) break; + + sol += query(N) - query(P); + + update(P); + + } + + if(P != - 1) out< +#include + + + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +char a[nmax+2], b[nmax+2]; + +int lc[sigma]; + + + +queue q[sigma]; + +int t[nmax+1]; + + + +void bt_upd(int x, int n){ + + for (; x<=n; x= 2*x-(x&(x-1))){ + + ++t[x]; + + } + +} + + + +int bt_que(int x){ + + int sol= 0; + + for (; x>0; x&= (x-1)){ + + sol+= t[x]; + + } + + return sol; + +} + + + +int main(){ + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + int n; + + for (n= 0; n<=nmax&& a[n]>='a'&& a[n]<='z'; ++n){ + + ++lc[a[n]-'a']; --lc[b[n]-'a']; + + } + + for (int i= 0; i +#include + + +int n, sol; + +int aib[50002], f[155]; + +int nr[27][50002]; + +char s1[50002], s2[50002]; + + + +int query (int p) + +{ + + int x = 0; + + while (p) + + { + + x += aib[p]; + + p -= p & -p; + + } + + return x; + +} + + + +void update (int p) + +{ + + while (p <= n) + + { + + aib[p] ++; + + p += p & -p; + + } + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (s1, 50001, stdin); + + fgets (s2, 50001, stdin); + + n = strlen (s1 + 1); + + + + int i, p; + + + + for (i = 1; i <= n; i ++) + + { + + f[s1[i] - 'a'] ++; + + nr[s1[i] - 'a'][++ nr[s1[i] - 'a'][0]] = i; + + f[s2[i] - 'a'] --; + + } + + for (i = 0; i <= 26;nr[i][0] = 0, i ++) + + if (f[i]) + + { + + printf ("-1\n"); + + return 0; + + } + + + + for (i = 1; i <= n; i ++) + + { + + p = nr[s2[i] - 'a'][++ nr[s2[i] - 'a'][0]]; + + sol = sol + i - 1 - query (p); + + update (p); + + } + + printf ("%d\n", sol); + + return 0; + +} diff --git a/swap/aib/9ecf85a5-25bc-4072-95ca-e0e3374ff318.cpp b/swap/aib/9ecf85a5-25bc-4072-95ca-e0e3374ff318.cpp new file mode 100644 index 0000000000000000000000000000000000000000..022c07e3551a51f7aaf52bfbeddb4f4596d6a302 --- /dev/null +++ b/swap/aib/9ecf85a5-25bc-4072-95ca-e0e3374ff318.cpp @@ -0,0 +1,68 @@ +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string s1, s2; long long ans; int arb[100100]; + +queue q[30]; + + + +void add(int i){ + + for(; i <= 100000; i += (i & (-i))) arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) ans += arb[i]; + + return ans; + +} + + + +int main(){ + + getline(in, s1); + + getline(in, s2); + + for(int i = 0; i < s1.size(); i++) q[s1[i] - 'a'].push(i+1); + + for(int i = 0; i < s2.size(); i++){ + + if(q[s2[i] - 'a'].empty()) return out << -1, 0; + + int pos = q[s2[i] - 'a'].front(); + + q[s2[i] - 'a'].pop(); + + ans += pos - i - 1 + get(s2.size()) - get(pos); + + add(pos); + + } + + out << ans; + + return 0; + +} diff --git a/swap/aib/9ed21eca-1001-4542-81bd-04113a944bf4.cpp b/swap/aib/9ed21eca-1001-4542-81bd-04113a944bf4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/aib/9ed21eca-1001-4542-81bd-04113a944bf4.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/aib/9f424008-1718-4c9d-80ce-4dbe25172cf1.cpp b/swap/aib/9f424008-1718-4c9d-80ce-4dbe25172cf1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..94181d4d56b451c97f988a1b3d9c436722598515 --- /dev/null +++ b/swap/aib/9f424008-1718-4c9d-80ce-4dbe25172cf1.cpp @@ -0,0 +1,101 @@ +#include +#include +#include + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int n, v[50002], viz1[30], viz2[30], n2; + + + +deque viz[30]; + + + +long long s; + + + +char sir1[50002], sir2[50002]; + + + +int aib[50002]; + + + +int query(int nod){ + + int s = 0; + + for( ; nod >= 1; nod -= (nod & -nod)) + + s += v[nod]; + + return s; + +} + + + +void update(int nod){ + + for( ; nod <= n; nod += (nod & -nod)) + + ++ v[nod]; + +} + + + +int main(){ + + + + f>>sir1 + 1>>sir2 + 1; + for(int i = 1; sir1[i] != 0; ++ i, ++ n) + + viz[sir1[i] - 'a' + 1].push_back(i); + + + + for(int i = 1; i <= n; ++ i){ + + if(viz[sir2[i] - 'a' + 1].empty()){ + + g<<-1; + + return 0; + + } + + int nod = viz[sir2[i] - 'a' + 1].front(); + + viz[sir2[i] - 'a' + 1].pop_front(); + + s += nod - query(nod) - 1; + + update(nod); + + } + + + + g< +#include +#include + + + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout("swap.out"); + + + +int arb[100100]; + + + +void add(int i){ + + for(; i < 100000; i+=(i & (-i))) + + arb[i]++; + +} + + + +int get(int i){ + + int ans = 0; + + for(; i; i -= (i & (-i))) + + ans +=arb[i]; + + return ans; + +} + + + + + +string s, t; + + + +queue q[30]; + + + +int main(){ + + + + getline(cin,s); + + getline(cin,t); + + + + for(int i = 0; i < s.length(); i++) + + q[s[i]-'a'].push(i+1); + + + + int rs = 0; + + + + for(int i = 0; i < t.length(); i++) + + { + + if(q[t[i]-'a'].empty()) return(cout << -1, 0); + + int pos = q[t[i]-'a'].front(); q[t[i]-'a'].pop(); + + rs += pos - i - 1 + get(s.length()) - get(pos); + + add(pos); + + } + + + + cout << rs; + + return (0); + +} diff --git a/swap/aib/a23b2069-9c93-40cf-9267-5ac46bde0ba7.cpp b/swap/aib/a23b2069-9c93-40cf-9267-5ac46bde0ba7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aea2a90354f8530f07c25e092bf196779efee3ac --- /dev/null +++ b/swap/aib/a23b2069-9c93-40cf-9267-5ac46bde0ba7.cpp @@ -0,0 +1,95 @@ +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char s1[50001], s2[50001]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[27][50001]; + +int n; + +void update (int p) { + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int query(int p) { + + int s=0; + + for (int i=n;i>=1;i-=(i&(-i))) + + s+=a[i]; + + for (int i=p;i>=1;i-=(i&(-i))) + + s-=a[i]; + + return s; + +} + +int sol; + +int main() { + + fin >> s1+1; + + fin >> s2+1; + + n = strlen(s1+1); + + for (int i=1; i<=n; i++) { + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26; i++) { + + if (f1[i]!=f2[i]) { + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + for (int i=1; i<=n; i++) { + + f2[s2[i]-'a'+1]++; + + int x = m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + update (x); + + sol+=query(x); + + } + + fout << sol; + + return 0; + +} diff --git a/swap/aib/a277e040-927e-4fe3-82b1-7e9df2da238c.cpp b/swap/aib/a277e040-927e-4fe3-82b1-7e9df2da238c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fc2d9e4e3d8a0fe71a4d3396eef060f049037f32 --- /dev/null +++ b/swap/aib/a277e040-927e-4fe3-82b1-7e9df2da238c.cpp @@ -0,0 +1,139 @@ +#include +#include + + + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int sa[26],sb[26]; + +int x[26],lit,n,m; + +char s[50050],t[50050],r[50050]; + +int v[50050],aib[50050]; + +long long sol; + + + +int query(int poz) + +{ + + int sum=0; + + for (;poz>=1;poz-=poz&(-poz)) + + sum+=aib[poz]; + + return sum; + +} + +void update(int poz,int val) + +{ + + for (;poz<=n+2;poz+=poz&(-poz)) + + aib[poz]+=val; + +} + + + +int main() + +{ + + int i,j; + + f.getline(s,50050); + + f.getline(t,50050); + + + + memcpy(r,s,sizeof(r)); + + memset(s,0,sizeof(s)); + + memcpy(s,t,sizeof(r)); + + memset(t,0,sizeof(s)); + + memcpy(t,r,sizeof(r)); + + + + n=strlen(s); + + m=strlen(t); + + if (n!=m) { + + g<<-1<<'\n'; + + return 0; + + } + + + + + + for (i=n-1;i>=0;i--) + + sa[s[i]-'a']++; + + for (i=m-1;i>=0;i--) + + sb[t[i]-'a']++; + + for (i=0;i<26;i++) + + if (sa[i]!=sb[i]) { + + g<<-1<<'\n'; + + return 0; + + } + + for (i=0;t[i];i++) { + + lit=t[i]-'a'; + + while (s[x[lit]]!=t[i]) + + x[lit]++; + + v[i+1]=x[lit]+1; + + x[lit]++; + + } + + sol=1LL*n*(n-1)/2; + + for (i=1;i<=n;i++) { + + sol-=query(v[i]-1); + + update(v[i],1); + + } + + g< +#include +#include +using namespace std; + + + +FILE *f, *g; + + + +int rez; + + + +unsigned short apa[26 + 1]; + +unsigned short apb[26 + 1]; + + + +int n; + +char a[50009 + 2]; + +char b[50009 + 2]; + + + +unsigned short k; + +unsigned short lst[26 + 1]; + +unsigned short nod[50009 + 1]; + +unsigned short urm[50009 + 1]; + + + +unsigned short aib[50009 + 1]; + + + +int getLen() + +{ + + int rez = 1; + + while(a[rez] >= 'a' && a[rez] <= 'z') + + rez ++; + + + + return rez; + +} + + + +void modif(char a[]) + +{ + + int i; + + for(i = n; i >= 1; i --) + + a[i] = a[i - 1]; + +} + + + +void readFile() + +{ + + f = fopen("swap.in", "r"); + + + + fgets(a, 50009 + 2, f); + + fgets(b, 50009 + 2, f); + + + + n = getLen(); + + + + modif(a); + + modif(b); + + + + fclose(f); + +} + + + +int pos() + +{ + + int i; + + int rez = 1; + + for(i = 1; i <= n; i ++) + + { + + apa[a[i] - 'a' + 1] ++; + + apb[b[i] - 'a' + 1] ++; + + } + + + + i = 1; + + while(i <= 26 && rez == 1) + + { + + rez = rez && (apa[i] == apb[i]); + + + + i ++; + + } + + + + return rez; + +} + + + +void addChar(int a, int b) + +{ + + k ++; + + + + nod[k] = b; + + urm[k] = lst[a]; + + + + lst[a] = k; + +} +void getRez() + +{ + + int i; + + for(i = 1; i <= n; i ++) + + { + + addChar(a[i] - 'a' + 1, i); + + } + + + + int p2, s; + + for(i = n; i >= 1; i --) + + { + + int nr = b[i] - 'a' + 1; + + int poz = nod[lst[nr]]; + + lst[nr] = urm[lst[nr]]; + + + + int p2 = poz + 1; + + while(p2 <= n) + + { + + aib[p2] += 1; + + + + p2 += p2 & (-p2); + + } + + + + s = 0; + + p2 = poz; + + while(p2 > 0) + + { + + s += aib[p2]; + + + + p2 -= p2 & (-p2); + + } + + poz -= s; + + + + rez += i - poz; + + } + +} + + + +void solve() + +{ + + if(pos() == 0) + + { + + rez = -1; + + + + return; + + } + + + + getRez(); + +} + + + +void printFile() + +{ + + g = fopen("swap.out", "w"); + + + + fprintf(g, "%d\n", rez); + fclose(g); + +} + + + +int main() + +{ + + readFile(); + + + + solve(); + + + + printFile(); + + + + return 0; + +} diff --git a/swap/aib/a7f3b49b-91ac-4aad-978a-fb2fba37c130.cpp b/swap/aib/a7f3b49b-91ac-4aad-978a-fb2fba37c130.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c8ab11095c2e789d53a39e81f8c91a83bcc67e6 --- /dev/null +++ b/swap/aib/a7f3b49b-91ac-4aad-978a-fb2fba37c130.cpp @@ -0,0 +1,188 @@ +#include +using namespace std; + + + +const int SPQR = 50005, + +SIGM = 26; + + + +int n; + + + +vector aps[SIGM]; + +char sr1[SPQR], + +sr2[SPQR]; + +int aib[SPQR], + +gnm[SIGM]; + + + +inline int lsb(int arg) { + + return arg&-arg; + +} + + + +inline int cod(int arg) { + + return arg - 'a'; + +} + + + +inline void update(int pos, int val) { + + while(pos < SPQR) { + + aib[pos]+= val; + + pos+= lsb(pos); + + } + +} + + + +inline int query(int pos) { + + int ant = 0; + + + + while(pos > 0) { + + ant+= aib[pos]; + + pos-= lsb(pos); + + } + + + + return ant; + +} + + + +void ave(void) { + + int lst[SIGM]; + + + + memset(lst, 0xFF, sizeof(lst)); + + for(int i=1; i<=n; ++i) + + aps[cod(sr2[i])].push_back(i); + +} + + + +void caesar(int &ant) { + + int f1[SIGM], + + f2[SIGM]; + + int pos; + + + + ant = 0; + + memset(f1, 0x00, sizeof(f1)); + + memset(f2, 0x00, sizeof(f2)); + + + + for(int i=1; i<=n; ++i) { + + ++ f1[ cod(sr1[i]) ]; + + ++ f2[ cod(sr2[i]) ]; + + } + + + + for(int i=0; i>(sr1 + 1)>>(sr2 + 1); + + n = strlen(sr1 + 1); + + + + ave(); + + caesar(ant); + + + + fo< +#include +#include +using namespace std; + +ifstream in("swap.in"); + +ofstream out("swap.out"); + +char sir1[50001], sir2[50001]; + +unsigned short hz[50001],hz2[50001],aib[50001]; + +unsigned int hashuri[27][50001]; + +int n; + +void update_aib (int pozitie) { + + for (int i = pozitie; i <= n; i += (i&(-i))) { + + aib[i] ++; + + } + + return; + +} + +int query_aib(int pozitie) { + + int rezultat = 0; + + for (int i = n; i >= 1; i -=(i&(-i))) { + + rezultat += aib[i]; + + } + + for (int i = pozitie; i >= 1; i -= (i&(-i))) { + + rezultat -= aib[i]; + + } + + return rezultat; + +} + +int rezultat; + +int main(void) { + + in >> sir1 + 1; + + in >> sir2 + 1; + + n = strlen(sir1+1); + + for (int i = 1; i <= n; i ++) { + + hz[sir1[i]-'a'+1] ++; + + hz2[sir2[i]-'a'+1]++; + + hashuri[sir1[i]-'a'+1][hz[sir1[i]-'a'+1]] = i; + + } + + for (int i = 1; i <= 26; i ++) { + + if (hz[i] != hz2[i]) { + + out<<-1; + + return 0; + + } + + hz2[i] = 0; + + } + + for (int i = 1; i <= n; i ++) { + + hz2[sir2[i]-'a'+1] ++; + + int x = hashuri[sir2[i]-'a'+1][hz2[sir2[i]-'a'+1]]; + + update_aib (x); + + rezultat += query_aib(x); + + } + + out << rezultat; + + return 0; + +} diff --git a/swap/aib/a907d35a-d645-4add-a3ae-48e5db6f1f9b.cpp b/swap/aib/a907d35a-d645-4add-a3ae-48e5db6f1f9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2af125ba003524dabe4b76e7168118c233562101 --- /dev/null +++ b/swap/aib/a907d35a-d645-4add-a3ae-48e5db6f1f9b.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include + + +using namespace std; + +deque poz[100]; + +char unu[50010],doi[50010]; + +unsigned short int n,a[50010],aib[50010],funu[30],fdoi[30]; + +void update (unsigned short int pos, unsigned short int val) + +{ + + for(;pos<=n && pos;pos=pos+(pos&-pos)) + + aib[pos]+=val; + +} + +int query (unsigned short int pos) + +{ + + int ans=0; + + for(;pos;pos=pos-(pos&-pos)) + + ans+=aib[pos]; + + return ans; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + int i,nswap; + + fgets(unu, 50001, stdin); fgets(doi, 50001, stdin); + + n=strlen(doi); + + for(i=0;i +#include +#include +#include +#include +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +deque A[28]; + +deque B[28]; + +int n,i,sol,p,aib[50001]; + +char s1[50001], s2[50001]; + +int query (int p){ + + int r=0; + + for(;p;p-=p&-p){ + + r+=aib[p]; + + } + + return r; + +} + + + +void update (int p){ + + for(;p<=n;p+=p&-p) + + aib[p]++; + +} + + + + + +int main(){ + + fin>>s1+1>>s2+1; + + for(i=1;s1[i]!=0;i++) { + + A[s1[i]-'a'+1].push_back(i); + + B[s2[i]-'a'+1].push_back(i); + + ++n; + + } + + for(i=1;i<=26;i++) + + if(A[i].size()!=B[i].size()){ + + fout<<-1; + + return 0; + + } + + for(i=1;i<=n;i++){ + + p=A[s2[i] - 'a'+1].front(); + + A[s2[i] - 'a'+1].pop_front(); + + sol+=p-query(p)-1; + + update(p); + + } + + fout< +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int sigma= 26; + +const int nmax= 50000; + + + +int ft[nmax+1], f1[sigma]; + + + +char a[nmax+2], b[nmax+2]; + +queue v[sigma]; + + + +void update( int p, int n ) { + + for ( ; p<=n; p= 2*p-(p&(p-1)) ){ + + ++ft[p]; + + } + +} + + + +int query( int p ) { + + int sol= 0; + + for ( ; p>0; p&= (p-1) ) { + + sol+= ft[p]; + + } + + return sol; + +} + + + +int main( ) { + + int ans= 0; + + fin.getline(a, nmax+2); + + fin.getline(b, nmax+2); + + + + int n; + + for ( n= 0; a[n]>='a' && a[n]<='z'; ++n ) { + + ++f1[a[n]-'a'], --f1[b[n]-'a']; + + } + + for ( int i= 0; i +#include +#include +#include +#include +#include +using namespace std; + +struct nod{ int poz; + +char c;}; + +ofstream fout("swap.out"); + +nod s1[50005],s2[50005]; + +char aux1[50005],aux2[50005]; + +int P[50005],M[1000],aib[50005],M1[1000],p=1,N1,N2; + +long long S; + +vectorm[50005]; + +void cit() + +{int i,j; + + ifstream fin("swap.in"); + + fin>>aux1>>aux2; + + N1=strlen(aux1),N2=strlen(aux2); + + for(i=0;aux1[i];i++) + + {s1[i].c=aux1[i]; + + if(i==0||M[(int)s1[i].c]==0) + + {s1[i].poz=1; + + M[(int)s1[i].c]=1; + + } + + else s1[i].poz=M[(int)s1[i].c]+1,M[(int)s1[i].c]++; + + } + + for(i=0;aux2[i];i++) + + { s2[i].c=aux2[i]; + + M1[(int)s2[i].c]++; + + } + + if(N1==N2) + + for(i=1;i<=500;i++) + + if(M[i]!=M1[i]) + + {p=0; return;} + + + + + + cout<<1; + + for(i=0;s2[i].c;i++) + + { + + m[s2[i].c-'a'+1].push_back(i+1); + + } + + for(i=0;s1[i].c;i++) + + P[i+1]=m[s1[i].c-'a'+1][s1[i].poz-1]; + + + + fin.close(); + +} + + + +long long query(int p) + +{long long s=0,i; + +for(i=p;i;i-=i&-i) + + s+=aib[i]; + +return s; + +} + +void update (int p,int x) + + {long i; + + for(i=p;i<=N1;i+=i&-i) + + aib[i]+=x; + + + + } + +void solve() + +{int i; + + for(i=1;i<=N1;i++) + + {S+=(query(N1)-query(P[i])); + + + + update(P[i],1); + + } + + fout< +#include +using namespace std; + +int i,j,nr,n,ap[300],pr[50009][30],aib[50009],cr[50009],dr[50009]; + +char A[50009],B[50009]; + +void U(int p,int v){while(p<=n){aib[p]+=v;p+=p-(p&(p-1));}} + +int Q(int p){int s=0;while(p){s+=aib[p];p&=p-1;}return s;} + +int main() + +{ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +fgets(A, 50001, stdin); + +fgets(B, 50001, stdin); + +n=strlen(A+1); + +for(i=1;i<=n;i++) + + ap[A[i]]++; + +for(i=1;i<=n;i++) + + ap[B[i]]--; + +for(i='a';i<='z';i++) + + if(ap[i]!=0) + + { + + printf("-1\n"); + + return 0; + + } + +for(i=n;i>=1;i--) + +{ + + for(j='a';j<='z';j++) + + pr[i][j-'a']=pr[i+1][j-'a']; + + pr[i][A[i]-'a']=i; + +} + +for(i=1;i<=n;i++) + +{ + + cr[B[i]]=pr[cr[B[i]]+1][B[i]-'a']; + + dr[cr[B[i]]]=i; + +} + +for(i=1;i<=n;i++) + +{ + + nr+=dr[i]-i+(i-1-Q(dr[i])); + + U(dr[i],1); + +} + +printf("%d\n",nr); + +return 0; + +} diff --git a/swap/aib/ad30b8ac-4c6d-4da9-85b3-91451bb95472.cpp b/swap/aib/ad30b8ac-4c6d-4da9-85b3-91451bb95472.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e680c928aed67a2e1f29294101ae3b5f84c16130 --- /dev/null +++ b/swap/aib/ad30b8ac-4c6d-4da9-85b3-91451bb95472.cpp @@ -0,0 +1,71 @@ +# include +# include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +queue d[27]; + +char a[50010],b[50010]; + +int v[50010],f[27],n,i; + +long long s; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(i&(-i))) + + v[i]++; + +} + +int query(int poz){ + + int s=0; + + for(int i=poz;i>=1;i-=(i&(-i))) + + s+=v[i]; + + return s; + +} + +int main () { + + fin>>a+1>>b+1; + + for(i=1;b[i]!=0;i++,n++) + + d[b[i]-'a'+1].push(i); + + for(i=1;i<=n;i++){ + + if(d[a[i]-'a'+1].size()==0){ + + fout<<"-1\n"; + + return 0; + + } + + int el=d[a[i]-'a'+1].front(); + + s+=el-1-query(el); + + update(el); + + d[a[i]-'a'+1].pop(); + + } + + fout< +#include +#include +#include +#include + +using namespace std; + +const int MAX_N = 50100; + +char fst[MAX_N]; +char lst[MAX_N]; + +int n; + +int pos['z' + 5][MAX_N]; + +int ap['z' + 10]; + +int AIB[MAX_N]; + +inline int lsb(const int x){return x & (-x);} + +void update(const int pos, const int delta){ + for(int i = pos ; i <= n ; i += lsb(i)) + AIB[i] += delta; +} + +int query(const int pos){ + int ret = 0; + for(int i = pos ; i > 0 ; i -= lsb(i)) + ret += AIB[i]; + return ret; +} + +int organize() { + + int total = 0; + + for(int i = n ; i >= 1 ; --i) { + + const int next = pos[lst[i]][pos[lst[i]][0]--]; + + const int shift = query(next); + const int actual = next - shift; + + total += i - actual; + + update(next, 1); + } + + return total; +} + +int operations() { + int i; + + for(i = 1 ; fst[i] && lst[i] ; ++i){ + + pos[fst[i]][++pos[fst[i]][0]] = i; + + ap[fst[i]]++; + ap[lst[i]]--; + } + + n = i - 1; + + bool bad = 0; + + if(fst[i] || lst[i]) + bad = 1; + + for(char it = 'a' ; it <= 'z' && !bad ; ++it) + if( ap[it] != 0 ) + bad = 1; + + + if(bad) + return -1; + + return organize(); +} + +int main() { + ifstream in("swap.in"); + freopen("swap.out", "w", stdout); + + in >> (fst+1) >> (lst+1); + + printf("%d\n", operations()); + + return 0; +} diff --git a/swap/aib/b211a980-7855-4547-9b30-7a6051d26560.cpp b/swap/aib/b211a980-7855-4547-9b30-7a6051d26560.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b834d940b19a8a444d66a73cdf636dc1a56de5f --- /dev/null +++ b/swap/aib/b211a980-7855-4547-9b30-7a6051d26560.cpp @@ -0,0 +1,123 @@ +#include +#include + + + + + + +FILE *fin,*fout; + + + +typedef long long LL; + + + +char sir[2][50001 +1]; + + + +int nr[28],cp[28]; + +int *poz[28],scade[28],pos[28]; + + + +int scos[50001 +1]; + + + +LL num; + + + +void proces() + +{int i,j,l; + + l=strlen(sir[0]); + + for (i=1;i<='z'-'a'+1;++i) nr[i]=0; + + for (j=0;jnr[val]) + + {num=-1;break;} + + else {p=poz[val][cp[val]]; + + num=num+p-scade[val]; + + scos[p]=1; + + for (j=1;j<='z'-'a'+1;++j) if (pos[j]>p) ++scade[j]; + + ++cp[val]; + + if (cp[val]<=nr[val]) + + for (;pos[val]<=poz[val][cp[val]];++pos[val]) + + scade[val]+=scos[pos[val]]; + + } + + } + + fout=fopen("swap.out","w"); + + fprintf(fout,"%lld\n",num); + + fclose(fout); + + return 0; + +} diff --git a/swap/aib/b31e8210-d8fa-460f-bb5c-2e0bf4187b31.cpp b/swap/aib/b31e8210-d8fa-460f-bb5c-2e0bf4187b31.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61c48c7646a4dfce0e3f4ae3a99bd162d914ce5a --- /dev/null +++ b/swap/aib/b31e8210-d8fa-460f-bb5c-2e0bf4187b31.cpp @@ -0,0 +1,158 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int N,AIB[50100],Sol; + +char S[50100],S2[50100]; + +queue Index[26]; + + + +int Query(int nod) { + + + + int Rez=0; + + + + while(nod>0) { + + Rez+=AIB[nod]; + + nod-=(nod&-nod); + + } + + + + return Rez; + + + +} + +void Update(int nod) { + + + + while(nod<=N) { + + AIB[nod]++; + + nod+=(nod&-nod); + + } + + + +} + +void Afis() { + + + + ofstream out("swap.out"); + + out< +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +deque aux[300]; + +int i, n, v[50003], ok, z[50003]; + +char c1[50003], c2[50003]; + +long long sol; + + + +void update(int p, int val) + +{ + + for(;p<=n;p+=p&(-p)) + + z[p]+=val; + +} + + + +int quarry(int p) + +{ + + int r=0; + + for(;p>=1;p-=p&(-p)) + + r+=z[p]; + + return r; + +} + + + +int verif(int a) + +{ + + if(a<0) + + a=-a; + + return a; + +} + + + +int main() + +{ + + fin>>c1; + + fin>>c2; + + for(i=0;c2[i]!=0;i++) + + aux[c2[i]].push_back(i+1); + + for(i=0;c1[i]!=0;i++) + + { + + n++; + + if(aux[c1[i]].size()>0) + + { + + v[i+1]=aux[c1[i]][0]; + + aux[c1[i]].pop_front(); + + } + + else + + { + + ok=1; + + } + + } + + if(ok==1) + + { + + fout<<-1; + + return 0; + + } + + for(i=1;i<=n;i++) + + { + + sol+=v[i]-1-quarry(v[i]); + + update(v[i], 1); + + } + + fout< +#include + +using namespace std; +queue q[30]; +int f1[30],f2[30]; +int aib[50005],n; +void upd(int a){ + for(;a<=n;a+=(a&(-a))){ + aib[a]++; + } +} +int que(int a){ + int sol=0; + for(;a>=1;a-=(a&(-a))){ + sol+=aib[a]; + } + return sol; +} +ifstream cin("swap.in"); +ofstream cout("swap.out"); +int main() +{ + string s1,s2; + cin>>s1; + cin>>s2; + n=s2.size(); + if(n!=s2.size()){ + cout<<-1; + return 0; + } + for(int i=0;i +#include +#include + + +using namespace std; + + + +char a[50002],b[50002]; + +int v[50001],aux[26],aib[50001]; + +queue ind[26]; + + + +int main() + +{ + + int n,i,j,sol=0; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a,sizeof(a),stdin); + + fgets(b,sizeof(b),stdin); + + n=strlen(a)-1; + + for (i=0;i +#include +#include +using namespace std; + + + +const char infile[] = "swap.in"; + +const char outfile[] = "swap.out"; + + + +ifstream fin(infile); + +ofstream fout(outfile); + + + +const int MAXN = 50005; + +const int oo = 0x3f3f3f3f; + +const int SIGMA = 26; + + + +char a[MAXN], b[MAXN]; + +int N, p[MAXN], ind[MAXN], pos[SIGMA]; + +vector fr[SIGMA]; + +int aib[MAXN]; + + + +struct classComp { + + inline bool operator () (const int &a, const int &b) const { + + if(p[a] == p[b]) + + return a > b; + + return p[a] > p[b]; + + } + +}; + + + +inline int lsb(const int &conf) { + + return conf & (-conf); + +} + + + +inline void Update(const int &Node) { + + for(int _newNode = Node ; _newNode <= N ; _newNode += lsb(_newNode)) + + ++ aib[_newNode]; + +} + + + +inline int Query(const int &Node) { + + int sum = 0; + + for(int _newNode = Node ; _newNode > 0 ; _newNode -= lsb(_newNode)) + + sum += aib[_newNode]; + + return sum; + +} + + + +int main() { + + + + + + fin >> (a + 1) >> (b + 1); + + for(N = 1 ; b[N] ; ++ N) + + fr[b[N] - 'a'].push_back(N); + + -- N; + + for(int i = 1 ; i <= N ; ++ i) { + + ind[i] = i; + + if(pos[a[i] - 'a'] >= fr[a[i]-'a'].size()) { + + fout << "-1\n"; + + return 0; + + } + + p[i] = fr[a[i] - 'a'][pos[a[i] - 'a']]; + + ++ pos[a[i] - 'a']; + + } + + sort(ind + 1, ind + N + 1, classComp()); + + int Ans = 0; + + for(int i = 1 ; i <= N ; ++ i) { + + Ans += Query(ind[i]); + + Update(ind[i]); + + } + + fout << Ans << '\n'; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/aib/bb5c212c-0cbe-481c-b325-9c4739e94795.cpp b/swap/aib/bb5c212c-0cbe-481c-b325-9c4739e94795.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b83b2dec0886852cde41853a151ee55a9f2477c9 --- /dev/null +++ b/swap/aib/bb5c212c-0cbe-481c-b325-9c4739e94795.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + + +using namespace std; + + +string a,b; +vector se[26]; + +int n,s[50005],aib[50005],ind[50005],rez,poz[26]; + +bool cmp(const int &a,const int &b) { + if(s[a]==s[b]) return a>b; + return s[a]>s[b]; +} + +int lsb(int n) { + return (n^(n-1))&n; +} + +void ins(int x) { + for(int i=x; i<=n;i+=lsb(i)) ++aib[i]; +} + +int query(int x) { + int ret=0; + for(int i=x; i>0; i-=lsb(i)) ret+=aib[i]; + return ret; +} + + +int main() + +{ + + ifstream f("swap.in"); + ofstream g("swap.out"); + f>>a>>b; + for(int i=0; i=se[a[i]-'a'].size()) { + g<<"-1"; + return 0; + } + s[i+1]=se[a[i]-'a'][poz[a[i]-'a']]; + ++poz[a[i]-'a']; + } + n=a.size(); + sort(ind+1,ind+n+1,cmp); + for(int i=1; i<=n; ++i) { + rez+=query(ind[i]); + ins(ind[i]); + } + g< +#include +#include +using namespace std; + +int aib[50005],sum,poz,i,n,x,nr,ind[28]; + +long long tot,summ; + +vector v[29]; + +string s1,s2; + +inline int lbit(int x) + +{ + + return ((x^(x-1))&x); + +} + +void update(int y,int val) + +{ + + for(poz=y;poz<=n;poz+=lbit(poz)) + + aib[poz]+=val; + +} + +int compute(int y) + +{ + + sum=0; + + for(poz=y;poz>0;poz-=lbit(poz)) + + sum+=aib[poz]; + + return sum; + +} + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f>>s1>>s2; + + if(s1.size()!=s2.size()) {g<<"-1";return 0;} + + n=s1.size(); + + for(i=0;iv[nr].size()) {g<<"-1";return 0;} + + else + + { + + x=v[nr][ind[nr]]; + + summ=compute(x+1)-(i+1); + + tot+=summ; + + update(1,1); + + update(x+1,-1); + + ind[nr]++; + + } + + } + + g< +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int i, n, sol, x; + +int fa[205], fb[205], f[50005]; + +int d[205][50005]; + + + +char a[50005], b[50005]; + + + +inline int query (int x){ + + int sol = 0; + + while (x){ + + sol += f[x]; + + x -= (x&-x); + + } + + return sol; + +} + + + +inline void update (int x){ + + while (x <= n){ + + f[x]++; + + x += (x&-x); + + } + +} + + + +int main(){ + + fin >> a; + + fin >> b; + + n = strlen(a); + + for (i=0; i +#include + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int n, i, lga, lgb, s, ok; + +int qqa[303], qqb[303], order[50050], position[303][50050], aib[50050]; + +char a[50050]; + +char b[50050]; + +int p[50050]; + + + +void tree_update(int value, int pos) + +{ + + int j; + + for(j=pos; j<=n; j+=(j&(-j))) + + aib[j]+=value; + +} + + + +int tree_query(int position) + +{ + + int s=0; + + for(i=position; i>=1; i-=(i&(-i))) s+=aib[i]; + + return s; + +} + + + +int main() + +{ + + f.getline(a, 50050); + + f.getline(b, 50050); + + + + lga=strlen(a); + + for(i=0;i=1; --j) + + { + + s+=tree_query(p[j]); + + tree_update(1, p[j]); + + } + + g< + + + + +std::ifstream fin ("swap.in"); + +std::ofstream fout ("swap.out"); + + + +int bit[50005]; + +void update (int pos, int val){ + + while (pos < 50005){ + + bit[pos] += val; + + pos = (pos | (pos+1)); + + } + +} + + + +int query (int pos){ + + int ans = 0; + + while (pos >= 0){ + + ans = ans + bit[pos]; + + pos = (pos & (pos+1)) - 1; + + } + + return ans; + +} + + + +int main() + +{ + + std::string s1, s2; + + std::deque pos[26]; + + fin >> s1 >> s2; + + int moves = 0, i; + + for (i=0; i +#include +#include +using namespace std; + + + + + +char s[50010]; + +int a[50010], b[50010], v[50010], d[30], n, aib[50010], sol; + +vector c[30]; + + + +void update(int p) + +{ + + do + + { + + aib[p]++; + + p += (p&(p-1))^p; + + } + + while (p<=n); + +} + + + +int query(int p) + +{ + + int s=0; + + do + + { + + s+=aib[p]; + + p=p&(p-1); + + } + + while (p); + + return s; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(s,50010,stdin); + + n=strlen(s); + + n--; + + int i, ok=0; + + for (i=0; i +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int NMAX=50005; + + + +int n,m,len,fra[30],frb[30],AIB[NMAX]; + +char a[NMAX],b[NMAX]; + +long long sol; + +queueQ[30]; + + + +inline int zeros(int x) + +{ + + return x^(x&(x-1)); + +} + + + +inline void Update(int poz,int val) + +{ + + for (int i=poz;i<=n;i+=zeros(i)) AIB[i]+=val; + +} + + + +inline int Query(int poz) + +{ + + int i,rez=0; + + for (i=poz;i>=1;i-=zeros(i)) rez+=AIB[i]; + + return rez; + +} + + + +int main() + +{ + + int i,j,aux,poz; + + int C; + + bool ok; + + fin>>(a+1)>>(b+1); + + n=strlen(a+1);m=strlen(b+1); + + for (i=1;i<=n;i++) fra[a[i]-'a'+1]++; + + for (i=1;i<=m;i++) frb[b[i]-'a'+1]++; + + ok=1; + + for (i=1;i<=26;i++) + + if (fra[i]!=frb[i]) + + ok=0; + + if (ok==0) {fout<<"-1";return 0;} + + for (i=1;i<=n;i++) Q[a[i]-'a'+1].push(i); + + for (i=1;i<=m;i++) + + { + + C=b[i]-'a'+1;poz=Q[C].front(); + + aux=Query(n)-Query(poz-1); + + sol+=aux+poz-i; + + Update(poz,1); + + Q[C].pop(); + + } + + fout< +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int sigma= 26; +const int nmax= 50000; + +char a[nmax+2], b[nmax+2]; +int lc[sigma]; + +queue q[sigma]; +int t[nmax+1]; + +void bt_upd(int x, int n){ + for (; x<=n; x= 2*x-(x&(x-1))){ + ++t[x]; + } +} + +int bt_que(int x){ + int sol= 0; + for (; x>0; x&= (x-1)){ + sol+= t[x]; + } + return sol; +} + +int main(){ + fin.getline(a, nmax+2); + fin.getline(b, nmax+2); + int n; + for (n= 0; n<=nmax&& a[n]>='a'&& a[n]<='z'; ++n){ + ++lc[a[n]-'a']; --lc[b[n]-'a']; + } + for (int i= 0; i +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +list l[30]; + + + +int A[50010],v[50010],i,j,n,m,rez; + + + +char a[50010],b[50010]; + + + +void query (int x) { + + + + for (int i=x;i>0;i-=(i&(-i))) + + rez+=A[i]; + + + +} + + + +void update (int x) { + + + + for (int i=x;i<=n;i+=(i&(-i))) + + A[i]++; + +} + + + + + +int main () { + + + + fin>>a; + + fin>>b; + + n=strlen(a); + + for (i=0;i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream in ("swap.in"); + +ofstream out ("swap.out"); + + + +const int MAXN = 50010; + + + +int N; + +char S1[MAXN], S2[MAXN]; + +int V[MAXN], AIB[MAXN]; + +vector Ap1[30], Ap2[30]; + +int A1[30], A2[30]; + + + +inline int lsb (const int &x) + +{ + + return x & (-x); + +} + + + +inline void update (int poz) + +{ + + for ( ; poz <= N; poz += lsb (poz)) + + AIB[poz] ++; + +} + + + +inline int query (int poz) + +{ + + int ret = 0; + + + + for ( ; poz; poz -= lsb (poz)) + + ret += AIB[poz]; + + + + return ret; + +} + + + +int main() + +{ + + int i, j, Ans = 0; + + + + in >> (S1 + 1) >> (S2 + 1); + + N = strlen (S1 + 1); + + + + for (i = 1; i <= N; i ++){ + + Ap1[S1[i] - 'a'].push_back (i); + + Ap2[S2[i] - 'a'].push_back (i); + + ++ A1[S1[i] - 'a']; + + ++ A2[S2[i] - 'a']; + + } + + + + for (i = 0; i < 26; i ++) + + if (A1[i] != A2[i]){ + + out << -1; + + + + return 0; + + } + + + + for (i = 0; i < 26; i ++) + + for (j = 0; j < (int) Ap1[i].size (); j ++) + + V[Ap1[i][j]] = Ap2[i][j]; + + + + for (i = N; i; i --){ + + Ans += query (V[i]); + + update (V[i]); + + } + + + + out << Ans; + + + + return 0; + +} diff --git a/swap/aib/caaef3ca-2e95-45ca-bcae-393a6aa50ecb.cpp b/swap/aib/caaef3ca-2e95-45ca-bcae-393a6aa50ecb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ad22ab7eb1630c3d53ac5120963b74a2daf6030 --- /dev/null +++ b/swap/aib/caaef3ca-2e95-45ca-bcae-393a6aa50ecb.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +using namespace std; + +int N; +string A, B; + +vector freq[26]; + +inline int lsb(int node) { + return node & (-node); +} + +const int NMAX = 50000 + 5; +int aib[NMAX]; + +void init() { + for (int i = 1; i <= N; ++ i) + aib[i] = lsb(i); +} + +void update(int where) { + for (; where <= N; where += lsb(where)) + aib[where] --; +} + +int query(int where) { + int ans = 0; + for (; where; where -= lsb(where)) + ans += aib[where]; + return ans; +} + +int main() +{ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + + cin >> A >> B; + N = A.size(); + A = " " + A; + B = " " + B; + + for (int i = N; i; -- i) + freq[B[i] - 'a'].push_back(i); + init(); + + int ans = 0; + for (int i = 1; i <= N; ++ i) + if (freq[A[i] - 'a'].empty()) { + ans = -1; + break; + } + else { + int pos = freq[A[i] - 'a'].back(); + freq[A[i] - 'a'].pop_back(); + update(pos); + ans += query(pos); + } + + if (ans == -1) + cout << "-1\n"; + else + cout << ans << '\n'; + return 0; +} diff --git a/swap/aib/cac757f7-f620-4aad-9017-3c1aa0ea9d77.cpp b/swap/aib/cac757f7-f620-4aad-9017-3c1aa0ea9d77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..678a782f2c97e0e56b5d4b343d4d322e32127f7c --- /dev/null +++ b/swap/aib/cac757f7-f620-4aad-9017-3c1aa0ea9d77.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +ifstream fin ("swap.in"); +ofstream fout ("swap.out"); + +int sp[2][50001][26], n, x, ans; +queue urm[26]; +string s1, s2; + +int main() { + fin >> s1 >> s2; + int n = s1.size(); + if (n != s2.size()) { + fout << -1; + return 0; + } + for (int i = 1; i <= n; ++i) { + ++sp[0][i][s1[i - 1] - 'a']; + ++sp[1][i][s2[i - 1] - 'a']; + urm[s1[i - 1] - 'a'].push(i); + for (int j = 0; j < 26; ++j) { + sp[0][i][j] += sp[0][i - 1][j]; + sp[1][i][j] += sp[1][i - 1][j]; + } + } + for (int j = 0; j < 26; ++j) { + if (sp[0][n][j] != sp[1][n][j]) { + fout << -1; + return 0; + } + } + for (int i = 1; i <= n; ++i) { + x = urm[s2[i - 1] - 'a'].front(); + urm[s2[i - 1] - 'a'].pop(); + for (int j = 0; j < 26; ++j) { + ans += max(0, sp[0][x][j] - sp[1][i][j]); + } + } + fout << ans; + return 0; +} diff --git a/swap/aib/ceae3296-215f-49a9-aede-7389ceda9f35.cpp b/swap/aib/ceae3296-215f-49a9-aede-7389ceda9f35.cpp new file mode 100644 index 0000000000000000000000000000000000000000..893024a7691c78cd67682f3e773a5b62c80a971b --- /dev/null +++ b/swap/aib/ceae3296-215f-49a9-aede-7389ceda9f35.cpp @@ -0,0 +1,110 @@ +#include +#include +#include + + +using namespace std; + +int len,h,n; + +char ch[50005]; + +int a[50005]; + +deque dq[28]; + + + + + + + +void add(int pos,int val){ + + for(;pos<=len;pos+=pos&-pos) a[pos] += val; + +} + +int query(int pos){ + + int sum = 0; + + for(;pos;pos-=pos&-pos) sum += a[pos]; + + return sum; + +} + + + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + f >> ch; + + len = strlen(ch), h = 0; + + for(int i=0; i < len; ++i){ + + dq[ ch[i] - 'a' ] . push_back( ++h ); + + add( h , 1 ); + + } + + int sum = 0; + + n = len; + + f >> ch; + + len = strlen(ch); + + + + if( len != n ){ + + g << "-1"; + + return 0; + + } + + + + for(int i=0;i +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, ap1[30], ap2[30], ans, W[50010], Aib[50010]; + +struct data + +{ + + int pos, val; + +}VV[50010]; + +char S1[50010], S2[50010]; + + + +bool cmp(data one, data two) + +{ + + return one.val > two.val; + +} + + + +void Update(int pos) + +{ + + for(; pos <= N; pos += (pos & (-pos))) + + Aib[pos] ++; + +} + + + +int Query(int pos) + +{ + + int rez = 0; + + for(; pos; pos -= (pos & (-pos))) + + rez += Aib[pos]; + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + N = strlen(S1); + + for(i = 0; i < N; i++) + + { + + ap1[S1[i] - 'a'] ++; + + ap2[S2[i] - 'a'] ++; + + V[S2[i] - 'a'].push_back(i + 1); + + W[i] = ap1[S1[i] - 'a']; + + } + + for(i = 0; i < 30; i++) + + if(ap1[i] != ap2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + for(i = 0; i < N; i++) + + { + + VV[i].pos = i + 1; + + VV[i].val = V[S1[i] - 'a'][W[i] - 1]; + + } + + sort(VV, VV + N, cmp); + + for(i = 0; i < N; i++) + + { + + ans += Query(VV[i].pos); + + Update(VV[i].pos); + + } + + printf("%i\n", ans); + + return 0; + +} diff --git a/swap/aib/d03b72d4-430c-4d86-ae1e-9bd68f68ca14.cpp b/swap/aib/d03b72d4-430c-4d86-ae1e-9bd68f68ca14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7066d90ff8854febdff14b8986ede84810b617d2 --- /dev/null +++ b/swap/aib/d03b72d4-430c-4d86-ae1e-9bd68f68ca14.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + +FILE*f=fopen("swap.in","r"); + +FILE*g=fopen("swap.out","w"); + + + +int n; + +int child[30],aib[50005],fr1[30],fr2[30]; + +char A[50005],B[50005]; + +vectorP[30]; + + + +inline int lsb ( int x ){ + + return x & -x; + +} + + + +inline void update_aib ( int poz ){ + + + + while ( poz <= n ){ + + ++aib[poz]; + + poz += lsb(poz); + + } + +} + + + +inline int query_aib ( int poz ){ + + int s = 0; + + + + while ( poz > 0 ){ + + s += aib[poz]; + + poz -= lsb(poz); + + } + + + + return s; + +} + + + +int main () { + + + + fscanf(f,"%s%s",A+1,B+1); + + n = strlen(A+1); + + + + for ( int i = 1 ; i <= n ; ++i ){ + + P[ A[i]-'a'+1 ].push_back(i); + + ++fr1[A[i]-'a'+1]; + + ++fr2[B[i]-'a'+1]; + + } + + + + for ( int i = 1 ; i <= 26 ; ++i ){ + + if ( fr1[i] != fr2[i] ){ + + fprintf(g,"-1\n"); + + fclose(f); + + fclose(g); + + return 0; + + } + + } + + + + long long sol = 0; + + for ( int i = 1 ; i <= n ; ++i ){ + + int pos = P[ B[i]-'a'+1 ][child[B[i]-'a'+1]++]; + + int real_pos = pos + i - 1 - query_aib(pos-1); + + + + sol += real_pos - i; + + update_aib(pos); + + } + + + + fprintf(g,"%lld\n",sol); + + + + fclose(f); + + fclose(g); + + + + return 0; + +} diff --git a/swap/aib/d16dc9bb-701d-4052-bfbc-e74992e4d6fc.cpp b/swap/aib/d16dc9bb-701d-4052-bfbc-e74992e4d6fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38ba9e6f6325767b938079eaefea3bd3ffe483ba --- /dev/null +++ b/swap/aib/d16dc9bb-701d-4052-bfbc-e74992e4d6fc.cpp @@ -0,0 +1,118 @@ +#include +#include + + +using namespace std; + +char a[50010],b[50010]; + +int i,x,AIB[50010],n,c[50010]; + +int inv,nxt[50010],crt[30],lst[30]; + +int sum(int p) + +{ + + int s=0; + + int i; + + for(i=p; i; i-=(i&(-i))) + + s+=AIB[i]; + + return s; + +} + + + +void add(int p) + +{ + + int i; + + for(i=p; i<=n; i+=(i&(-i))) + + ++AIB[i]; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + n=strlen(a); + + for(i=0; i=1; --i) + + { + + x=c[i]; + + inv+=sum(x); + + add(x); + + } + + printf("%d\n",inv); + + return 0; + +} diff --git a/swap/aib/d20ed932-6ba9-4ec3-a614-96832123e52b.cpp b/swap/aib/d20ed932-6ba9-4ec3-a614-96832123e52b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b83b2dec0886852cde41853a151ee55a9f2477c9 --- /dev/null +++ b/swap/aib/d20ed932-6ba9-4ec3-a614-96832123e52b.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + + +using namespace std; + + +string a,b; +vector se[26]; + +int n,s[50005],aib[50005],ind[50005],rez,poz[26]; + +bool cmp(const int &a,const int &b) { + if(s[a]==s[b]) return a>b; + return s[a]>s[b]; +} + +int lsb(int n) { + return (n^(n-1))&n; +} + +void ins(int x) { + for(int i=x; i<=n;i+=lsb(i)) ++aib[i]; +} + +int query(int x) { + int ret=0; + for(int i=x; i>0; i-=lsb(i)) ret+=aib[i]; + return ret; +} + + +int main() + +{ + + ifstream f("swap.in"); + ofstream g("swap.out"); + f>>a>>b; + for(int i=0; i=se[a[i]-'a'].size()) { + g<<"-1"; + return 0; + } + s[i+1]=se[a[i]-'a'][poz[a[i]-'a']]; + ++poz[a[i]-'a']; + } + n=a.size(); + sort(ind+1,ind+n+1,cmp); + for(int i=1; i<=n; ++i) { + rez+=query(ind[i]); + ins(ind[i]); + } + g< +#include + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int mat[30][50005] , aib[50005] , nr[30] , c1[30] , c2[30]; + +int sol , n; + + + +char sir1[50005] , sir2[50005]; + + + +void update(int poz , int val) { + + while(poz <= n) { + + aib[poz] += val; + + poz += poz & (poz ^ (poz - 1)); + + } + +} + + + +int query(int poz) { + + int sum = 0; + + while(poz >= 1) { + + sum += aib[poz]; + + poz -= poz & (poz ^ (poz - 1)); + + } + + + + return sum ; + +} + + + +int main() { + + f >> sir1 + 1 >> sir2 + 1; + + + + n = strlen(sir1 + 1); + + for(int i = 1 ; i <= n ; ++i) { + + mat[sir1[i] - 'a'][++mat[sir1[i] - 'a'][0]] = i; + + ++c1[sir1[i] - 'a']; + + ++c2[sir2[i] - 'a']; + + } + + + + for(char ch = 'a' ; ch <= 'z' ; ++ch) { + + if(c1[ch - 'a'] != c2[ch - 'a']) { + + g << -1; + + return 0; + + } + + } + + + + for(int i = 1 ; i <= n ; ++i) { + + sol += mat[sir2[i] - 'a'][++nr[sir2[i] - 'a']] - 1 - query(mat[sir2[i] - 'a'][nr[sir2[i] - 'a']] - 1); + + update(mat[sir2[i] - 'a'][nr[sir2[i] - 'a']] , 1); + + } + + + + g << sol; + + return 0; + +} diff --git a/swap/aib/d33d11db-9e75-45df-8d40-6b150dcec491.cpp b/swap/aib/d33d11db-9e75-45df-8d40-6b150dcec491.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9fb151e4042c3968e8aa0940688989ce5d91f902 --- /dev/null +++ b/swap/aib/d33d11db-9e75-45df-8d40-6b150dcec491.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + + +using namespace std; + + + +int cont[27], cont2[27]; + +int P[50005]; + +int M[27][50005]; + +int N; + + + +int BIT[50005]; + + + +string a, b; + + + +inline int lsb( int x ) + +{ + + return ( x & ( -x ) ); + +} + + + +void update( int x, int val ) + +{ + + for ( int i = x; i <= N; i += lsb( i ) ) + + BIT[i] += val; + +} + + + +int query( int x ) + +{ + + int s = 0; + + + + for ( int i = x; i >= 1; i -= lsb( i ) ) + + s += BIT[i]; + + + + return s; + +} + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline( f, a ); + + getline( f, b ); + + + + N = a.size(); + + + + for ( int i = 0; i < N; ++i ) + + { + + M[ b[i] - 97 ][ ++cont2[ b[i] - 97 ] ] = i + 1; + + } + + + + for ( int i = 0; i < N; ++i ) + + { + + ++cont[ a[i] - 97 ]; + + + + P[i + 1] = M[ a[i] - 97 ][ cont[ a[i] - 97 ] ]; + + } + + + + int sol = 0; + + + + for ( int i = 1; i <= N; ++i ) + + { + + if ( P[i] == 0 ) + + { + + g << "-1\n"; + + return 0; + + } + + } + + + + for ( int i = 1; i <= N; ++i ) + + { + + sol += i - 1 - query( P[i] - 1 ); + + update( P[i], 1 ); + + } + + + + g << sol << "\n"; + + + + f.close(); + + g.close(); + + + + return 0; + +} diff --git a/swap/aib/d53defc3-48be-49f8-bb30-bc6181caca73.cpp b/swap/aib/d53defc3-48be-49f8-bb30-bc6181caca73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa9f2d82e1996d1c5289468dbb00be763aba59c --- /dev/null +++ b/swap/aib/d53defc3-48be-49f8-bb30-bc6181caca73.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + + + +using namespace std; + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() { + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n = 0; + while (a[n] >= 'a' && a[n] <= 'z') + ++n; + + fgets(b, 50001, stdin); + + for (i = 0;i < n;++i) + ax[ (int)a[i] ].push_back(i + 1); + + for (i = 0;i < n;++i) { + x[++nx] = ax[ b[i] ][0]; + ax[b[i]].pop_front(); + } +} + +inline void update(int x, int v) { + for (int i = x; i <= n; i += (i & -i)) + H[i] += v; +} + +inline int query(int x) { + int s = 0; + for (int i = x; i ; i -= (i & -i)) + s += H[i]; + return s; +} + +void solve() { + int i, j; + for (i = 1;i <= n;++i) + update(i, 1); + int nr = 0; + for (i = 1;i <= n;++i) { + update(x[i], -1); + nr += query(x[i]); + } + + printf("%d\n", nr); +} + +int main() { + freopen("swap.out","w",stdout); + read(); + int i; + for (i = 0;i < n;++i) + ++u1[ a[i] ], + ++u2[ b[i] ]; + + for (i = 0;i <= 255;++i) + if (u1[i] != u2[i]){ + printf("-1\n"); + return 0; + } + + solve(); + return 0; +} diff --git a/swap/aib/d5521238-667c-47e7-9733-2eb2f840ac76.cpp b/swap/aib/d5521238-667c-47e7-9733-2eb2f840ac76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a79a313302e38066f84bc08b942721322ba896e --- /dev/null +++ b/swap/aib/d5521238-667c-47e7-9733-2eb2f840ac76.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + + + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int N, i, j, aib[50005]; +int ind[135], poz[50005]; +long long ANS; +string s1, s2, cop1, cop2; +vector V[135]; + +int zero(int nr) +{ + return nr & (-nr); +} + +void Update(int nr) +{ + while (nr<=N) + { + aib[nr]++; + nr+=zero(nr); + } +} + +int Query(int nr) +{ + int ans=0; + while (nr>0) + { + ans+=aib[nr]; + nr-=zero(nr); + } + return ans; +} + +int main() +{ + fin >> s1 >> s2; + cop1=s1; + cop2=s2; + sort(cop1.begin(), cop1.end()); + sort(cop2.begin(), cop2.end()); + if (cop1!=cop2) + { + fout << -1; + fin.close(); + fout.close(); + return 0; + } + N=s1.size(); + s1='*'+s1; + s2='*'+s2; + for (i=1; i<=N; i++) + V[s2[i]].push_back(i); + for (i=1; i<=N; i++) + { + poz[V[s1[i]][ind[s1[i]]]]=i; + ind[s1[i]]++; + } + for (i=1; i<=N; i++) + { + ANS+=poz[i]-Query(poz[i])-1; + Update(poz[i]); + } + fout << ANS << '\n'; + fin.close(); + fout.close(); + return 0; +} diff --git a/swap/aib/d8d36db6-dd60-4b6e-9329-1a6344e18bdf.cpp b/swap/aib/d8d36db6-dd60-4b6e-9329-1a6344e18bdf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4f1a0a7b3ba68f0e105091dbfcebde58b968da3e --- /dev/null +++ b/swap/aib/d8d36db6-dd60-4b6e-9329-1a6344e18bdf.cpp @@ -0,0 +1,161 @@ +#include +#include + + + + + + +using namespace std; + +char A[50005],B[50005]; + +vector C[26]; + +inline int lit(char x) + +{ + + return x>='a' && x<='z'; + +} + +int a,b,nr1[26],nr2[26],act[26],D[50005],aib[50005],rez; + +void read() + +{ + + fgets(A+1,50005,stdin); + + fgets(B+1,50005,stdin); + + while (lit(A[a+1])) + + { + + a++; + + C[A[a]-'a'].push_back(a); + + nr1[A[a]-'a']++; + + } + + while (lit(B[b+1])) + + { + + b++; + + nr2[B[b]-'a']++; + + } + +} + +inline int verif() + +{ + + int i; + + for (i=0; i<26; i++) + + if (nr1[i]!=nr2[i]) + + return 0; + + return 1; + +} + +int lsb(int x) + +{ + + return x & -x; + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz; i<=b; i+=lsb(i)) + + aib[i]+=val; + +} + +int query(int poz) + +{ + + int i,sum=0; + + for (i=poz; i>0; i-=lsb(i)) + + sum+=aib[i]; + + return sum; + +} + +void solve() + +{ + + int i,l; + + for (i=1; i<=b; i++) + + { + + l=B[i]-'a'; + + D[i]=C[l][act[l]++]; + + } + + for (i=b; i>=1; i--) + + { + + rez+=query(D[i]-1); + + update(D[i],1); + + } + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + read(); + + if (!verif()) + + { + + printf("-1\n"); + + return 0; + + } + + solve(); + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/aib/e101c084-3565-4042-bee9-caab9b149f5e.cpp b/swap/aib/e101c084-3565-4042-bee9-caab9b149f5e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..672628dc02f13a4881d6a2a7bde4c94d7e3a2530 --- /dev/null +++ b/swap/aib/e101c084-3565-4042-bee9-caab9b149f5e.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + + + +int n,m,ok,i,x,v[50001],aib[50001],f[50001]; + +int sol; + +char a[50001],b[50001]; + +queue L[27]; + + + +void update(int p){ + + for(int i=p;i<=n;i+=(i&-i)) + + aib[i]++; + +} + +int query(int p){ + + int sol=0; + + for(int i=p;i>=1;i-=(i&-i)) + + sol += aib[i]; + + return sol; + +} + + + +int main (){ + + + + fin>>a+1>>b+1; + n = strlen (b+1); + + for (i=1;i<=n;i++) + + L[b[i]-'a'+1].push (i); + + + + for (i=1;i<=n;i++){ + + if (L[a[i]-'a'+1].size () == 0){ + + + + fout<<-1; + + return 0; + + } + + x = L[a[i]-'a'+1].front(); + + sol += x - 1- query (x); + + update (x); + + L[a[i]-'a'+1].pop(); + + } + + + + + + fout< +#include +#include +#include + + +using namespace std; + + + +typedef long long LL; + + + +const int kMaxN = 50005; + + + +deque C[2][26]; + +int N, P[kMaxN], BIT[kMaxN], I; + +char S[2][kMaxN]; + + + +void BuildP() + +{ + + for (int i = 1; i <= N; ++i) + + for (int s = 0; s < 2; ++s) + + C[s][S[s][i]-'a'].push_back(i); + + for (int c = 0; c < 26; ++c) + + for (; !C[0][c].empty(); C[0][c].pop_front(), C[1][c].pop_front()) + + P[C[0][c].front()] = C[1][c].front(); + +} + + + +bool CheckS() + +{ + + vector F(26, 0); + + for (int i = 1; i <= N; ++i) + + ++F[S[0][i]-'a'], --F[S[1][i]-'a']; + + for (int c = 0; c < 26; ++c) + + if (F[c] != 0) return false; + + return true; + +} + + + +inline int LSB(const int X) + +{ + + return X&(-X); + +} + + + +inline void Update(int X, const int V) + +{ + + for (; X <= N; X += LSB(X)) BIT[X] += V; + +} + + + +inline int Query(int X) + +{ + + int Q = 0; + + for (; X; X -= LSB(X)) Q += BIT[X]; + + return Q; + +} + + + +void Solve() + +{ + + if (!CheckS()) + + { + + I = -1; return; + + } + + BuildP(); + + for (int i = 1; i <= N; ++i) + + I += Query(N) - Query(P[i]), Update (P[i], 1); + +} + + + +void Read() + +{ + + freopen("swap.in", "r", stdin); + + scanf("%s\n%s", S[0]+1, S[1]+1); + + N = strlen (S[0]+1); + +} + + + +void Print() + +{ + + freopen("swap.out", "w", stdout); + + printf("%d\n", I); + +} + + + +int main() + +{ + + Read(); + + Solve(); + + Print(); + + return 0; + +} diff --git a/swap/aib/e234e682-79fa-4ee8-9078-2bce49175295.cpp b/swap/aib/e234e682-79fa-4ee8-9078-2bce49175295.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02a3821662a60a1912a381fedb11670bff0e1470 --- /dev/null +++ b/swap/aib/e234e682-79fa-4ee8-9078-2bce49175295.cpp @@ -0,0 +1,165 @@ +#include +#include +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int Lmax = 5 + 50000; + + + +deque poz[30]; + + + +string a, b; + + + +int aib[Lmax]; + +int n; + + + +bool verif = true; + + + +void Solve(); + +void Update(int p, int x); + +int Query(int p); + + + +int main() + +{ + + fin >> a >> b; + + + + a = ' ' + a; + + b = ' ' + b; + + + + n = a.size() - 1; + + + + + + Solve(); + + + + if (verif == false) + + { + + fout << -1; + + } + + else + + { + + int ans = 0; + + + + for (int i = 1; i <= n; ++i) + + { + + int p = poz[b[i] - 'a'].front(); + + poz[b[i] - 'a'].pop_front(); + + + + ans += p - 1 - Query(p); + + Update(p, 1); + + } + + + + fout << ans; + + } + +} + + + +void Solve() + +{ + + int f[30] = {0}; + + + + for (int i = 1; i <= n; ++i) + + { + + poz[a[i] - 'a'].push_back(i); + + ++f[b[i]- 'a']; + + } + + + + for (int i = 0; i < 26; ++i) + + if (poz[i].size() != f[i]) + + verif = false; + +} + + + +void Update(int p, int x) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] += x; + +} + + + +int Query(int p) + +{ + + int sum = 0; + + for (int i = p; i >= 1; i -= i & -i) + + sum += aib[i]; + + return sum; + +} diff --git a/swap/aib/e23a8cc7-fda9-4b5e-8a51-33a8e706e16d.cpp b/swap/aib/e23a8cc7-fda9-4b5e-8a51-33a8e706e16d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..580f97fd4b4adc66efa18b427ddc90540d80910d --- /dev/null +++ b/swap/aib/e23a8cc7-fda9-4b5e-8a51-33a8e706e16d.cpp @@ -0,0 +1,81 @@ +#include +#include +int n,p[26][50001],s[50001],v[50001],i,j,k,nr; + +char a[50001],b[50001]; + +void add(int x){ + + k=1; + + while(!(x&k)) k<<=1; + + for(j=x;j<=n;){ + + v[j]++; + + while(!(j&k)) k<<=1; + + j=j+k;} + +} + + + +int get(int x){ + + k=1; + + while(!(x&k))k<<=1; + + int s=0; + + for(j=x;j>=1;){ + + s=s+v[j]; + + while(!(j&k)) k<<=1; + + j=j-k;} + + return s;} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf(" %s %s ",a,b); + + n=strlen(a); + + if(n!=strlen(b)) {printf("-1");fclose(stdout);return 0;} + + for(i=0;i +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +char s1[50003]; + +char s2[50003]; + +int pr[50003]; + +long long aib[50003]; + +queue cr[30]; + +int i; + +int ln; + + + +inline void update(int val) { + + for(int i = val; i <= ln; i += i&-i) + + aib[i]++; + +} + + + +inline long long query(int val) { + + long long rez = 0; + + for(int i = val; i > 0; i -= i&-i) { + + rez += aib[i]; + + } + + return rez; + +} + + + +int main() { + + + + in.getline(s1, 50003); + + in.getline(s2, 50003); + + + + for(i = 0; s2[i]; i++) + + cr[s2[i]-'a'].push(i); + + + + for(i = 0; s1[i]; i++) { + + if(cr[s1[i]-'a'].empty()) { + + out << "-1"; + + return 0; + + } + + pr[i] = cr[s1[i]-'a'].front()+1; + + cr[s1[i]-'a'].pop(); + + } + + + + ln = i; + + + + for(i = 0; i < 30; i++) { + + if(!cr[i].empty()) { + + out << "-1"; + + return 0; + + } + + } + + + + long long rez = 0; + + + + for(i = ln-1; i >= 0; i--) { + + rez += query(pr[i]); + + update(pr[i]); + + } + + + + out << rez; + + + + return 0; + +} diff --git a/swap/aib/e7332ea7-3970-447c-8ec8-ced119b25130.cpp b/swap/aib/e7332ea7-3970-447c-8ec8-ced119b25130.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70aa60a8839a1cd65d980fa2625db44c4ca304f9 --- /dev/null +++ b/swap/aib/e7332ea7-3970-447c-8ec8-ced119b25130.cpp @@ -0,0 +1,99 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +short r[50005]; + +int rez,n,f,g; + +char a[50005],b[50005]; + +priority_queueq[35]; + +void update(int poz) + +{ + + while(poz<=n) + + { + + r[poz]--; + + poz+=(poz&(-poz)); + + } + +} + +int sum(int poz) + +{ + + int sum=0; + + while(poz) + + { + + sum+=r[poz]; + + poz-=(poz&(-poz)); + + } + + return sum; + +} + +int main() + +{ + + fin.getline(a+1,50005); + + fin.getline(b+1,50005); + + n=strlen(a+1); + + for(int i=1;i<=n;i++) + + q[a[i]-'a'].push(i); + + for(int i=n;i>0;i--) + + { + + if(q[b[i]-'a'].empty()) + + { + + fout<<-1; + + return 0; + + } + + f=q[b[i]-'a'].top(); + + q[b[i]-'a'].pop(); + + g=f+sum(f); + + update(f); + + rez+=i-g; + + } + + fout< +using namespace std; + + + +const int SPQR = 50005, + + SIGM = 26; + + + +int n; + + + +vector aps[SIGM]; + +char sr1[SPQR], + + sr2[SPQR]; + +int aib[SPQR], + + gnm[SIGM]; + + + +inline int lsb(int arg) { + + return arg&-arg; + +} + + + +inline int cod(int arg) { + + return arg - 'a'; + +} + + + +inline void update(int pos, int val) { + + while(pos < SPQR) { + + aib[pos]+= val; + + pos+= lsb(pos); + + } + +} + + + +inline int query(int pos) { + + int ant = 0; + + + + while(pos > 0) { + + ant+= aib[pos]; + + pos-= lsb(pos); + + } + + + + return ant; + +} + + + +void ave(void) { + + int lst[SIGM]; + + + + memset(lst, 0xFF, sizeof(lst)); + + for(int i=1; i<=n; ++i) + + aps[cod(sr2[i])].push_back(i); + +} + + + +void caesar(int &ant) { + + int f1[SIGM], + + f2[SIGM]; + + int pos; + + + + ant = 0; + + memset(f1, 0x00, sizeof(f1)); + + memset(f2, 0x00, sizeof(f2)); + + + + for(int i=1; i<=n; ++i) { + + ++ f1[ cod(sr1[i]) ]; + + ++ f2[ cod(sr2[i]) ]; + + } + + + + for(int i=0; i>(sr1 + 1)>>(sr2 + 1); + + n = strlen(sr1 + 1); + + + + ave(); + + caesar(ant); + + + + fo< +#include +#include +using namespace std; + + + +string s,t; + +vector v[30]; + +int a[50001],aib[50001],i,n; + +long long sum; + + + +void update(int k,int val) + +{ + + int i; + + for (i=k;i<=n;i=(i | (i-1))+1) + + aib[i]+=val; + +} + + + +int query(int k) + +{ + + int i,s=0; + + for (i=k;i>0;i=i & (i-1)) + + s+=aib[i]; + + return s; + +} + + + +int main() + +{ + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + f >> s >> t; + + n=s.length(); + + for (i=1;i<=26;i++) + + v[i].push_back(0); + + for (i=0;i=1;i--) + + { + + sum+=query(a[i]); + + update(a[i],1); + + } + + g << sum; + + return 0; + +} diff --git a/swap/aib/eac308e9-db3d-46ad-848d-1fdae6d066e6.cpp b/swap/aib/eac308e9-db3d-46ad-848d-1fdae6d066e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..42a853c99f93b94da74865f37e6019678c41ce4a --- /dev/null +++ b/swap/aib/eac308e9-db3d-46ad-848d-1fdae6d066e6.cpp @@ -0,0 +1,110 @@ +#include +#include + + +using namespace std; +char A[50005]; + +char B[50005]; + + + +int P[50005], C[50005]; + +int M[50005][30], L[30]; + +int D[50005]; + +int N, Res; + + + + void add (int p, int e) + + { + + while (p <= N) D[p] += e, p += p ^(p&(p - 1)); + + } + + + + int query (int p) + + { + + int ret = 0; + + for (int i = p; i;) ret += D[i], i -= i ^(i&(i - 1)); + + return ret; + + } + + + + void solve () + + { + + int i, ok = 0; + + for (i = 1; i <= N; ++i) C[i] = ++L[A[i] - 'a']; + + for (i = 1; i <= N; ++i) M[++M[0][B[i] - 'a']][B[i] - 'a'] = i; + + for (i = 1; i <= N; ++i) P[i] = M[C[i]][A[i] - 'a']; + + for (i = 1; i <= N; ++i) + + if (!P[i]) { + + printf ("-1\n"); + + return; + + } + + + + for (i = 1; i <= N; ++i) add(i, 1); + + for (i = 1; i <= N; ++i) + + { + + add (P[i], -1); + + Res += query(P[i] - 1); + + } + + printf ("%d\n", Res); + + } + + + + int main () + + { + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + fgets (A , 50001, stdin); + + fgets (B , 50001, stdin); + + N = strlen (A + 1); + + solve (); + + + + return 0; + + } diff --git a/swap/aib/ebb84f09-1be8-4b06-9bad-1cff17497522.cpp b/swap/aib/ebb84f09-1be8-4b06-9bad-1cff17497522.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cb0f015812a968405946aef82817eeeffdf5503 --- /dev/null +++ b/swap/aib/ebb84f09-1be8-4b06-9bad-1cff17497522.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +using VI = vector; + +using VVI = vector; + +using VB = vector; + + + +const int NrLit = 26; + + + +int N; + +string a, b; + +VI p[NrLit]; + +VI aib; + + + +void Read(); + +void Solve(); + +void Update(int pos, int val); + +int Query(int pos); + + + +int main() + +{ + + Read(); + + Solve(); + + + + fin.close(); + + fout.close(); + + return 0; + +} + + + +void Read() + +{ + + fin >> a >> b; + + N = (int)a.size(); + + + +} + + + +void Solve() + +{ + + for (int i = N - 1; i >= 0; --i) + + p[a[i] - 'a'].push_back(i); + + + + aib = VI(N + 1); + + + + int ans{0}; + + for (int i = 0; i < N; ++i) + + { + + if (p[b[i] - 'a'].empty()) + + { + + fout << "-1\n"; + + return; + + } + + + + int pos = p[b[i] - 'a'].back(); + + Update(pos + 1, +1); + + p[b[i] - 'a'].pop_back(); + + + + pos = pos + (Query(N) - Query(pos + 1)); + + + + ans += pos - i; + + } + + + + fout << ans << '\n'; + +} + + + +void Update(int pos, int val) + +{ + + for (int i = pos; i <= N; i += i & -i) + + aib[i] += val; + +} + + + +int Query(int pos) + +{ + + int sum{0}; + + + + for (int i = pos; i > 0; i -= i & -i) + + sum += aib[i]; + + + + return sum; + +} diff --git a/swap/aib/ecfb3b8c-bc5e-4b9b-b9b2-66c9d102672d.cpp b/swap/aib/ecfb3b8c-bc5e-4b9b-b9b2-66c9d102672d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d946c6c2f49f217b69929701aa702a4058bbaba7 --- /dev/null +++ b/swap/aib/ecfb3b8c-bc5e-4b9b-b9b2-66c9d102672d.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +using namespace std; + + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() +{ + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n=0; + while(a[n]>='a' && a[n]<='z') ++n; + + fgets(b, 50001, stdin); + + for(i=0;i + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +deque < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + + + fin >> a >> b; n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + fout << "-1" << '\n'; + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + fout << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/f1b5cfc1-e2f2-43b4-8610-d287f6189e77.cpp b/swap/aib/f1b5cfc1-e2f2-43b4-8610-d287f6189e77.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afb64cf52e1d07601df8cf489e238a1f5b5e2b81 --- /dev/null +++ b/swap/aib/f1b5cfc1-e2f2-43b4-8610-d287f6189e77.cpp @@ -0,0 +1,127 @@ +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int L[26][50010]; + +int F[26], G[26], P[50010]; + +int AIB[50010]; + +char A[50010], B[50010]; + + + +int n, i, j; + +long long sol; + + + +int query(int p) { + + int r = 0; + + for (;p;p -= (p&-p)) + + r += AIB[p]; + + return r; + +} + + + +void update(int p) { + + for (;p<=n;p += (p&-p)) + + AIB[p]++; + +} + + + +int main() { + + fin>>A+1; + + fin>>B+1; + + n = strlen(A+1); + + for (i=1;i<=n;i++) { + + A[i] -= 'a'; + + B[i] -= 'a'; + + } + + + + for (i=1;i<=n;i++) { + + F[A[i]]++; + + L[ A[i] ][ F[A[i]] ] = i; + + } + + + + for (i=1;i<=n;i++) { + + G[ B[i] ]++; + + P[ L[ B[i] ][ G[ B[i] ] ] ] = i; + + } + + + + for (i=0;i<26;i++) { + + if (F[i] != G[i]) { + + fout<<-1; + + return 0; + + } + + } + + + + for (i=1;i<=n;i++) + + P[i] = n-P[i]+1; + + + + for (i=1;i<=n;i++) { + + sol += query(P[i]); + + update(P[i]); + + } + + + + fout< + +int arb[100100],rs; +using namespace std; +int get(int i){ + int ans=0; + for(;i;i-=(i & (-i))) ans+=arb[i]; + return ans; +} +void add(int i){ + for(;i<100000;i+=(i & (-i))) arb[i]++; +} +queue Q[30]; +string A,B; +int main(){ + ifstream cin("swap.in"); + ofstream cout("swap.out"); + cin >> A >> B; + for(int i = 0;i + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +string s1,s2; +queue Q[27]; +int N; +int aib [110000]; + +int prev(int x){ + return x-(x&(-x)); +} + +int next(int x){ + return x+(x&(-x)); +} +int zer(int x){ + return x&(-x); +} + +void update(int k,int newv){ + for (;k<=100000;k=next(k)) aib[k]+=newv; +} + +int query(int k){ + int rs=0; + for (;k;k=prev(k)) rs+=aib[k]; + return rs; +} + +int main(){ + fin >>s1>>s2; + N=s1.length(); + for (int i=0;i + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , m , ans; + +int p[Nmax] , aib[Nmax]; + + + +deque < int > g[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); fgets(b, 50001, stdin); n = strlen(a); m = strlen(b); + + + + if (n != m) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (i = n - 1; i >= 0; --i) + + g[b[i]-'a'].push_back(i); + + + + for (i = 0; i < n; ++i) + + { + + if (g[a[i]-'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + p[i] = g[a[i]-'a'].back() + 1; + + g[a[i]-'a'].pop_back(); + + } + + + + for (i = n - 1; i >= 0; --i) + + { + + ans += aibQuery(p[i]); + + aibUpdate(p[i]); + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/aib/f4d73448-9a68-4719-90a2-46629e787e80.cpp b/swap/aib/f4d73448-9a68-4719-90a2-46629e787e80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f9ea233807b70cd9b4bea1d3b7d02eee9275d125 --- /dev/null +++ b/swap/aib/f4d73448-9a68-4719-90a2-46629e787e80.cpp @@ -0,0 +1,126 @@ +#include + + +using namespace std; + +char s1[50000 + 2], s2[50000 + 2]; + +int aib[50000 + 1], next[50000], ut[26]; + +int n; + +int fr1[26], fr2[26]; + + + +inline int ultb(int x){ + + return x & (-x); + +} + + + +int suma(int x){ + + if(x == 0) + + return 0; + + return aib[x] + suma(x - ultb(x)); + +} + + + +void add(int x, int d){ + + if(x <= n){ + + aib[x] += d; + + add(x + ultb(x), d); + + } + +} + + + +int main(){ + + FILE *in = fopen("swap.in", "r"); + + fgets(s1, 50000 + 2, in); + + fgets(s2, 50000 + 2, in); + + n = 0; + + while(s1[n] >= 'a' && s1[n] <= 'z'){ + + next[n] = ut[s1[n] - 'a']; + + ut[s1[n] - 'a'] = n; + + n++; + + } + + fclose(in); + + int i, p, rez = 0; + + for(i = 0; i < n; i++){ + + s1[i] -= 'a'; + + s2[i] -= 'a'; + + fr1[s1[i]]++; + + fr2[s2[i]]++; + + } + + bool cnt = 1; + + for(i = 0; i < 26; i++) + + if(fr1[i] != fr2[i]) + + cnt = 0; + + FILE *out = fopen("swap.out", "w"); + + if(cnt == 0) + + fprintf(out, "-1"); + + else{ + + for(i = 1; i <= n; i++) + + aib[i] = ultb(i); + + for(i = n - 1; i >= 0; i--){ + + p = ut[s2[i]]; + + ut[s2[i]] = next[ut[s2[i]]]; + + rez += suma(n) - suma(p + 1); + + add(p + 1, -1); + + } + + fprintf(out, "%d", rez); + + } + + fclose(out); + + return 0; + +} diff --git a/swap/aib/f5396922-af92-4ba3-a59e-a920aa3e851a.cpp b/swap/aib/f5396922-af92-4ba3-a59e-a920aa3e851a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ea26e69e37a21aca0901fcd31e46a0c8a4a4e93 --- /dev/null +++ b/swap/aib/f5396922-af92-4ba3-a59e-a920aa3e851a.cpp @@ -0,0 +1,108 @@ +#include +#include +#include +using namespace std; + +const int nmax=5e4+3; + +int n,v[nmax],p[nmax],sol,q[30][nmax],poz[30]; + +char a[nmax],b[nmax]; + +inline int usu(int x) + +{ + + return x&(-x); + +} + +void update(int x) + +{ + + while(x<=n) + + { + + ++v[x]; + + x+=usu(x); + + } + +} + +int query(int x) + +{ + + int sol=0; + + while(x) + + { + + sol+=v[x]; + + x-=usu(x); + + } + + return sol; + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s",(a+1)); + + scanf("%s",(b+1)); + + n=strlen(a+1); + + for(int i=1;i<=n;++i) q[a[i]-'a'][++q[a[i]-'a'][0]]=i; + + for(int i=0;i<=26;++i) poz[i]=1; + + for(int i=1;i<=n;++i) + + { + + if(q[b[i]-'a'][0]+1==poz[b[i]-'a']) + + { + + printf("-1"); + + return 0; + + } + + p[i]=q[b[i]-'a'][poz[b[i]-'a']]; + + ++poz[b[i]-'a']; + + } + + for(int i=1;i<=n;++i) + + { + + sol=sol+i-query(p[i])-1; + + update(p[i]); + + } + + printf("%d",sol); + + return 0; + +} diff --git a/swap/aib/f5741378-82c1-45b2-a267-5273bd6411b3.cpp b/swap/aib/f5741378-82c1-45b2-a267-5273bd6411b3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f8c8590e68463d50e0712db7bf056b6906a16194 --- /dev/null +++ b/swap/aib/f5741378-82c1-45b2-a267-5273bd6411b3.cpp @@ -0,0 +1,101 @@ +#include +#include + + +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +char s1[50006],s2[50006]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[30][50001]; + +int n,sol; + +int query (int p){ + + int s=0; + + for (int i=n;i>0;i-=(i&(-i))) + + s=s+a[i]; + + for (int i=p;i>0;i-=(i&(-i))) + + s=s-a[i]; + + return s; + +} + +void update (int p){ + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int main() + +{ + + fin>>s1+1; + + fin>>s2+1; + + n=strlen(s1+1); + + for (int i=1;i<=n;i++){ + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26;i++){ + + if (f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for (int i=1;i<=n;i++){ + + f2[s2[i]-'a'+1]++; + + int x=m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + + + + + update (x); + + sol+=query(x); + + } + + fout< #include +#include +#include +using namespace std; + + + +int sol; + +char strInit[50024], strMod[50024]; + +int aibAp[50024]; + +vector vctPoz[26]; + + + +inline int aibSum(int x) + +{ + + int sum = 0; + + + + for (int i = x; i; i -= (i ^ (i & (i - 1)))) + + sum += aibAp[i]; + + + + return sum; + +} + + + +inline void aibAdd(int x, int val) + +{ + + for (int i = x; i < 50024; i += (i ^ (i & (i - 1)))) + + aibAp[i] += val; + +} + + + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(strInit + 1, 50024, stdin); + + fgets(strMod + 1, 50024, stdin); + + + + int n = strlen(strInit + 1) - 1; + + for (int i = 1; i <= n; i++) + + vctPoz[strInit[i] - 'a'].push_back(i); + + + + for (int i = n; i; i--) + + { + + int loc = vctPoz[strMod[i] -'a'].size(); + + if (!loc) + + { + + sol = -1; + + + + break; + + } + + int poz = vctPoz[strMod[i] - 'a'][loc - 1]; + + vctPoz[strMod[i] - 'a'].pop_back(); + + + + sol += aibSum(poz); + + aibAdd(poz, 1); + + } + + + + printf("%d\n", sol); + + + + fclose(stdin); + + fclose(stdout); + + return 0; + +} diff --git a/swap/aib/f9016136-0b66-11ec-a670-2cf05d84bfc2.cpp b/swap/aib/f9016136-0b66-11ec-a670-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8991a34ca655408877a07058b3cca283b126ed63 --- /dev/null +++ b/swap/aib/f9016136-0b66-11ec-a670-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int N; + +vector < int > BIT; + + + +void update(int index) { + + for(int i = index; i <= N; i += i & -i) + + ++BIT[i]; + +} + + + +int query(int index) { + + int ans = 0; + + for(int i = index; i > 0; i -= i & -i) + + ans += BIT[i]; + + return ans; + +} + + + +int main() { + + string s, t; + + fin >> s >> t; + + vector < int > fs(26), ft(26); + + for(char x : s) + + ++fs[x - 'a']; + + for(char x : t) + + ++ft[x - 'a']; + + bool ok = true; + + for(int i = 0; i < 26 && ok; ++i) + + if(fs[i] != ft[i]) + + ok = false; + + if(!ok) { + + fout << -1; + + return 0; + + } + + vector < int > from_s[26], from_t[26]; + + N = s.size(); + + s = '0' + s, t = '0' + t; + + for(int i = 1; i <= N; ++i) + + from_s[s[i] - 'a'].emplace_back(i); + + for(int i = 1; i <= N; ++i) + + from_t[t[i] - 'a'].emplace_back(i); + + vector < int > perm(N + 1); + + for(int i = 0; i < 26; ++i) + + for(int j = 0; j < (int)from_t[i].size(); ++j) { + + int poz = from_t[i][j]; + + perm[poz] = from_s[i][j]; + + } + + int ans = 0; + + BIT.resize(N + 1); + + for(int i = N; i > 0; --i) { + + ans += query(perm[i]); + + update(perm[i]); + + } + + fout << ans; + +} diff --git a/swap/aib/f9208ba5-0b66-11ec-84d9-2cf05d84bfc2.cpp b/swap/aib/f9208ba5-0b66-11ec-84d9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19c578be685cc7d9063a56770909ef48b02ed001 --- /dev/null +++ b/swap/aib/f9208ba5-0b66-11ec-84d9-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream f("swap.in"); +ofstream g("swap.out"); +const int LETTERS = 26; + +int N; +vector < int > frA(LETTERS, 0), frB(LETTERS, 0); +vector < int > perm, bit; +deque < int > fromA[LETTERS], fromB[LETTERS]; + +inline int lsb(int nr) { + return (nr & -nr); +} + +int query(int index) { + int ret{}; + + for(;index;index -= lsb(index)) + ret += bit[index]; + + return ret; +} + +void update(int index) { + for(;index <= N;index += lsb(index)) + bit[index]++; +} + +int main() { + + string a, b; + f >> a >> b; + + for (char &c : a) + frA[c - 'a']++; + + for (char &c : b) + frB[c - 'a']++; + + for (int i = 0; i < LETTERS; ++i) { + if (frA[i] != frB[i]) { + g << "-1"; + return 0; + } + } + + a = " " + a; + b = " " + b; + + N = a.size() - 1; + + perm.resize(N + 1, 0); + + for(int i = 1;i <= N;++i) + fromA[ a[i] - 'a' ].push_back(i); + + for(int i = 1;i <= N;++i) + fromB[ b[i] - 'a' ].push_back(i); + + for(int i = 1;i <= N;++i) { + int pos = fromA[a[i] - 'a'].front(); + fromA[ a[i] - 'a' ].pop_front(); + + perm[pos] = fromB[ a[i] - 'a' ].front(); + fromB[ a[i] - 'a' ].pop_front(); + } + + int ans{}; + bit.resize(N + 1, 0); + + for(int i = N;i;--i) { + ans += query(perm[i] - 1); + update(perm[i]); + } + + g << ans; + + return 0; +} diff --git a/swap/aib/f953fd27-0b66-11ec-8b4a-2cf05d84bfc2.cpp b/swap/aib/f953fd27-0b66-11ec-8b4a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bc474d14bd0d13bbde5f8dcf6089ff8236fbc951 --- /dev/null +++ b/swap/aib/f953fd27-0b66-11ec-8b4a-2cf05d84bfc2.cpp @@ -0,0 +1,108 @@ +#include + + + + + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +vector v1[30], v2[30]; + +int vec[50005], n, AIB[50005]; + + + +void xAdd(int poz){ + + for(int i = poz; i <= n; i += i & -i) + + AIB[i] ++; + +} + + + +int xCount(int poz){ + + int rez = 0; + + for(int i = poz; i >= 1; i -= i & -i) + + rez += AIB[i]; + + return rez; + +} + + + +int main() + +{ + + string a, b; + + fin >> a >> b; + + + + n = a.size(); + + for(int i = 0; i < n; ++i) + + v1[a[i] - 'a'].push_back(i); + + for(int i = 0; i < n; ++i) + + v2[b[i] - 'a'].push_back(i); + + for(int i = 0; i < 26; ++i) + + if(v1[i].size() > 0) + + { + + if(v1[i].size() != v2[i].size()) + + { + + fout << -1 << '\n'; + + return 0; + + } + + for(int j = 0; j < v1[i].size(); ++j) + + vec[v1[i][j]] = v2[i][j]; + + } + + for(int i = 0; i < n; ++i) + + vec[i] ++; + + int rez = 0; + + for(int i = n - 1; i >= 0; --i){ + + rez += xCount(vec[i] - 1); + + xAdd(vec[i]); + + } + + fout << rez << '\n'; + + return 0; + +} diff --git a/swap/aib/f974999a-0b66-11ec-9080-2cf05d84bfc2.cpp b/swap/aib/f974999a-0b66-11ec-9080-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..760e929de8fe495baef74366682db2c46b0dc64d --- /dev/null +++ b/swap/aib/f974999a-0b66-11ec-9080-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int N; + +vector < int > BIT; + + + +void update(int index) { + + for(int i = index; i <= N; i += i & -i) + + ++BIT[i]; + +} + + + +int query(int index) { + + int ans = 0; + + for(int i = index; i > 0; i -= i & -i) + + ans += BIT[i]; + + return ans; + +} + + + +int main() { + + string s, t; + + fin >> s >> t; + + vector < int > fs(26), ft(26); + + for(char x : s) + + ++fs[x - 'a']; + + for(char x : t) + + ++ft[x - 'a']; + + bool ok = true; + + for(int i = 0; i < 26 && ok; ++i) + + if(fs[i] != ft[i]) + + ok = false; + + if(!ok) { + + fout << -1; + + return 0; + + } + + vector < int > from_s[26], from_t[26]; + + N = s.size(); + + s = '0' + s, t = '0' + t; + + for(int i = 1; i <= N; ++i) + + from_s[s[i] - 'a'].emplace_back(i); + + for(int i = 1; i <= N; ++i) + + from_t[t[i] - 'a'].emplace_back(i); + + vector < int > perm(N + 1); + + for(int i = 0; i < 26; ++i) + + for(int j = 0; j < (int)from_s[i].size(); ++j) { + + int poz = from_s[i][j]; + + perm[poz] = from_t[i][j]; + + } + + int ans = 0; + + BIT.resize(N + 1); + + for(int i = N; i > 0; --i) { + + ans += query(perm[i]); + + update(perm[i]); + + } + + fout << ans; + +} diff --git a/swap/aib/f9b60fe6-0b66-11ec-a6be-2cf05d84bfc2.cpp b/swap/aib/f9b60fe6-0b66-11ec-a6be-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..118417aaa7d2d748833222a2d424a5b54f03c059 --- /dev/null +++ b/swap/aib/f9b60fe6-0b66-11ec-a6be-2cf05d84bfc2.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; +queue q[30]; +int f1[30],f2[30]; +int aib[50005],n; +void upd(int a){ + for(;a<=n;a+=(a&(-a))){ + aib[a]++; + } +} +int que(int a){ + int sol=0; + for(;a>=1;a-=(a&(-a))){ + sol+=aib[a]; + } + return sol; +} +ifstream cin("swap.in"); +ofstream cout("swap.out"); +int main() +{ + string s1,s2; + cin>>s1; + cin>>s2; + n=s2.size(); + if(n!=s2.size()){ + cout<<-1; + return 0; + } + for(int i=0;i + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int nmax = 50005; + +int n, m, aib[nmax]; + +string a, b; + +vector G[30]; + + + +int Query(int i){ + + int sum = 0; + + for (; i > 0; i -= ( (i ^ (i - 1)) & i )){ + + sum += aib[i]; + + } + + return sum; + +} + + + +void Update(int i){ + + for (; i <= n; i += ( (i ^ (i - 1)) & i )){ + + aib[i] += 1; + + } + +} + + + +int main(){ + + fin >> a >> b; + + n = a.size(); + + m = b.size(); + + if (n != m){ + + fout << -1; + + return 0; + + } + + for (int i = n - 1; i >= 0; --i){ + + G[a[i] - 'a'].push_back(i + 1); + + } + + int ans = 0; + + for (int i = 0; i < m; ++i){ + + if (G[b[i] - 'a'].size() == 0){ + + fout << -1; + + return 0; + + } + + int index = G[b[i] - 'a'].back(); + + G[b[i] - 'a'].pop_back(); + + int index2 = index + Query(n) - Query(index - 1); + + ans += (index2 - (i + 1)); + + Update(index); + + } + + fout << ans; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/aib/f9f65b82-0b66-11ec-bbf2-2cf05d84bfc2.cpp b/swap/aib/f9f65b82-0b66-11ec-bbf2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8163512aa1180eddf56a1bdd48245f9f60e9c99f --- /dev/null +++ b/swap/aib/f9f65b82-0b66-11ec-bbf2-2cf05d84bfc2.cpp @@ -0,0 +1,100 @@ +#include + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int bit[50005]; + +void update(int pozitie,int val) + +{ + + while(pozitie<50005) + + { + + bit[pozitie]+=val; + + pozitie=(pozitie|(pozitie+1)); + + } + +} + +int query(int pozitie) + +{ + + int ans=0; + + while(pozitie>=0) + + { + + ans=ans+bit[pozitie]; + + pozitie=(pozitie&(pozitie+1))-1; + + } + + return ans; + +} + +int main() + +{ + + string s1,s2; + + deque pozitie[26]; + + f>>s1>>s2; + + int mutari=0,i; + + for(i=0; i + + + + +std::ifstream fin ("swap.in"); + +std::ofstream fout ("swap.out"); + + + +int bit[50005]; + +void update (int pos, int val){ + + while (pos < 50005){ + + bit[pos] += val; + + pos = (pos | (pos+1)); + + } + +} + + + +int query (int pos){ + + int ans = 0; + + while (pos >= 0){ + + ans = ans + bit[pos]; + + pos = (pos & (pos+1)) - 1; + + } + + return ans; + +} + + + +int main() + +{ + + std::string s1, s2; + + std::deque pos[26]; + + fin >> s1 >> s2; + + int moves = 0, i; + + for (i=0; i +using namespace std; + + + +typedef struct lnod { + + int nod; + + lnod *next; + +}*nod; + + + +int i,n,aib[50005],ar[50005],rs; + +nod lda[30]; + +string a,b; + + + +void update(int poz) { + + while(poz<=n) ++aib[poz],poz+=-poz&poz; + +} + + + +int query(int poz) { + + int aux=0; + + while(poz>=1) aux+=aib[poz],poz-=-poz&poz; + + return aux; + +} + + + +void add(int x,nod &y) { + + nod p=new lnod; + + p->nod=x; + + p->next=y; + + y=p; + +} + + + +int main() + +{ + + ifstream cin("swap.in"); + + ofstream cout("swap.out"); + + + + ios_base::sync_with_stdio(0); cin.tie(0); + + + + getline(cin,a); n=a.length(); + + getline(cin,b); + + + + for(i=0;i=0;--i) + + { + + nod p=lda[a[i]-'a']; + + if(!p) return cout<<"-1\n",0; + + ar[i+1]=p->nod; + + lda[a[i]-'a']=p->next; + + } + + + + for(i=1;i<=n;++i) rs+=i-1-query(ar[i]),update(ar[i]); + + + + cout< + + + + + + +using namespace std; + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +int N, M; + +int bit[50005], a[50005], b[50005]; + +char S[50005]; + +queue Q[26]; + + + +void update(int pos) { + + for (int i = pos; i <= N; i += i & (-i)) + + bit[i]++; + +} + +int calc(int pos) { + + int sum = 0 ; + + for (int i = pos; i > 0; i -= i & (-i)) + + sum += bit[i]; + + return sum; + +} + +int main() + +{ + + f.get(S, 50005), f.get(); + + N = strlen(S); + + for (int i = 1; i <= N; ++i) + + a[i] = S[i - 1] - 'a', Q[a[i]].push(i); + + + + f.get(S, 50005), f.get(); + + M = strlen(S); + + for (int i = 1; i <= N; ++i) + + b[i] = S[i - 1] - 'a'; + + if (N != M) { + + g << -1; + + return 0; + + } + + + + int ans = 0; + + for (int i = 1; i <= N; ++i) + + if (Q[b[i]].empty()) { + + g << -1; + + return 0; + + } + + else { + + int j = Q[b[i]].front(); + + Q[b[i]].pop(); + + ans += j - i + calc(N) - calc(j); + + update(j); + + } + + + + g << ans << '\n'; + + + + return 0; + +} diff --git a/swap/aib/fa8e21a1-0b66-11ec-9dad-2cf05d84bfc2.cpp b/swap/aib/fa8e21a1-0b66-11ec-9dad-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74ec581c8251e7e984afd469537387924c351662 --- /dev/null +++ b/swap/aib/fa8e21a1-0b66-11ec-9dad-2cf05d84bfc2.cpp @@ -0,0 +1,142 @@ +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +vector v[50]; + +vector v1[50]; + +char a[50005],b[50005]; + +int n,n1,aib[50005],pozi[50]; + +int ub (int x) + +{ + + return x&(-x); + +} + +void update(int poz,int val) + +{ + + int i; + + for (i=poz;i<=n;i+=ub(i)) + + { + + aib[i]+=val; + + } + +} + +int suma (int poz) + +{ + + int i,sum=0; + + for (i=poz;i>0;i-=ub(i)) + + { + + sum=sum+aib[i]; + + } + + return sum; + +} + +int main() + +{ + + f>>a>>b; + + n=strlen(a); + + for (int i=0;i +#include +#include + + + + + + +using namespace std; + + + +int N,AIB[50100],Sol; + +char S[50100],S2[50100]; + +queue Index[26]; + + + +int Query(int nod) { + + + + int Rez=0; + + + + while(nod>0) { + + Rez+=AIB[nod]; + + nod-=(nod&-nod); + + } + + + + return Rez; + + + +} + +void Update(int nod) { + + + + while(nod<=N) { + + AIB[nod]++; + + nod+=(nod&-nod); + + } + + + +} + +void Afis() { + + + + ofstream out("swap.out"); + + out< +#include +#include +#include + + +using namespace std; + + + +const int N = 50000 + 7; + +int n; + +string s; + +string t; + +int aib[N]; + +vector p[26]; + + + +void add(int i, int x) + +{ + + while (i <= n) + + { + + aib[i] += x; + + i += i & (-i); + + } + +} + + + +int get(int i) + +{ + + int ans = 0; + + while (i) + + { + + ans += aib[i]; + + i -= i & (-i); + + } + + return ans; + +} + + + +int main() + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + + + cin >> s >> t; + + n = (int) s.size(); + + s = "." + s; + + t = "." + t; + + for (int i = 1; i <= n; i++) + + { + + int c = s[i] - 'a'; + + p[c].push_back(i); + + } + + for (int i = 1; i <= n; i++) + + { + + add(i, +1); + + } + + for (int c = 0; c < 26; c++) + + { + + reverse(p[c].begin(), p[c].end()); + + } + + int cost = 0; + + for (int i = 1; i <= n; i++) + + { + + int c = t[i] - 'a'; + + if (p[c].empty()) + + { + + cout << "-1\n"; + + return 0; + + } + + int j = p[c].back(); + + p[c].pop_back(); + + cost += get(j) - 1; + + add(j, -1); + + } + + cout << cost << "\n"; + +} diff --git a/swap/aib/fb2db1a2-0b66-11ec-957f-2cf05d84bfc2.cpp b/swap/aib/fb2db1a2-0b66-11ec-957f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6873b9389afd426ef4004f31db66dc66ad4c9dde --- /dev/null +++ b/swap/aib/fb2db1a2-0b66-11ec-957f-2cf05d84bfc2.cpp @@ -0,0 +1,123 @@ +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +string a, b; + +int fr[30]; + +int aib[50003], n; + + + +void update(int p, int val) + +{ + + for (int i = p ; i <= n; i += i & -i) + + aib[i] += val; + +} + + + +int query(int p) + +{ + + int suma = 0; + + for (int i = p; i; i -= i & -i) + + suma += aib[i]; + + return suma; + +} + + + +int main() + +{ + + fin >> a >> b; + + n = a.size(); + + + + for (int i = 0; i < n; ++i) + + { + + ++fr[a[i] - 'a']; + + --fr[b[i] - 'a']; + + } + + + + for (int i = 0; i <= 26; ++i) + + if (fr[i]) + + { + + fout << "-1\n"; + + return 0; + + } + + a = " " + a; + + b = " " + b; + + int cnt = 0; + + + + vector < deque > poz(30); + + + + for (int i = 1; i <= n; ++i) + + poz[a[i] - 'a'].push_back(i); + + + + for (int i = 1; i <= n; ++i) + + { + + int p = poz[b[i] - 'a'].front(); + + poz[b[i] - 'a'].pop_front(); + + cnt += p - 1 - query(p); + + update(p, 1); + + } + + + + fout << cnt << "\n"; + + return 0; + +} diff --git a/swap/aib/fbd0c0b5-0b66-11ec-8c12-2cf05d84bfc2.cpp b/swap/aib/fbd0c0b5-0b66-11ec-8c12-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99e1784797bf4e5452b2e2e76cda531022fb3a6a --- /dev/null +++ b/swap/aib/fbd0c0b5-0b66-11ec-8c12-2cf05d84bfc2.cpp @@ -0,0 +1,96 @@ +#include +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +queue que[51]; + +char S[50001], S2[50001]; + +int i, x[50001], S_fr[51], S2_fr[51], sol, n, aib[50001]; + +void update(int pos){ + + while(pos <= n){ + + aib[pos]++; + + pos = pos + (pos & (-pos)); + + } + +} + +int quary(int pos){ + + int s = 0; + + while(pos >= 1){ + + s = s + aib[pos]; + + pos = pos - (pos & (-pos)); + + } + + return s; + +} + +int main() + +{ f >> (S + 1); + + f >> (S2 + 1); + + for(i = 1; S[i] != 0; i++){ + + S_fr[S[i] - 'a' + 1]++; + + S2_fr[S2[i] - 'a' + 1]++; + + } + + for(i = 1; i <= 'z' - 'a' + 1; i++) + + if(S_fr[i] != S2_fr[i]){ + + g << -1 ; + + return 0; + + } + + for(i = 1; S2[i] != 0; i++) + + que[S2[i] - 'a' + 1].push(i); + + for(i = 1; S[i] != 0; i++){ + + x[i] = que[S[i] - 'a' + 1].front(); + + que[S[i] - 'a' + 1].pop(); + + } + + n = strlen(S + 1); + + for(i = 1; i <= n; i++){ + + sol = sol + quary(n) - quary(x[i]); + + update(x[i]); + + } + + g << sol; + + return 0; + +} diff --git a/swap/aib/fc0f8e4b-0b66-11ec-b630-2cf05d84bfc2.cpp b/swap/aib/fc0f8e4b-0b66-11ec-b630-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1952ba11b6fb3e880160baae7cb0dc74b5f8c986 --- /dev/null +++ b/swap/aib/fc0f8e4b-0b66-11ec-b630-2cf05d84bfc2.cpp @@ -0,0 +1,108 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +int i, n, sol, x; + +int fa[205], fb[205], f[50005]; + +int d[205][50005]; + + + +char a[50005], b[50005]; + + + +inline int query (int x){ + + int sol = 0; + + while (x){ + + sol += f[x]; + + x -= (x&-x); + + } + + return sol; + +} + + + +inline void update (int x){ + + while (x <= n){ + + f[x]++; + + x += (x&-x); + + } + +} + + + +int main(){ + + fin >> a; + + fin >> b; + + n = strlen(a); + + for (i=0; i +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +char c1[50001],c2[50001]; + +int n,f1[30],f2[30],i,v[50001]; + +int a[30][50001]; + +long long sol; + + + +int q(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=v[p]; + + + + return sol; + +} + + + +void u(int p){ + + for(;p<=n;p+=(p&-p)) + + v[p]++; + +} + + + +int main(){ + + fin>>c1+1>>c2+1; + + n=strlen(c1+1); + + + + for(i=1;i<=n;i++){ + + f1[ c1[i]-'a'+1 ]++; + + f2[ c2[i]-'a'+1 ]++; + + + + a[c1[i]-'a'+1][ f1[ c1[i]-'a'+1 ] ]=i; + + } + + + + for(i=1;i<=26;i++){ + + if(f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for(i=1;i<=n;i++){ + + f2[c2[i]-'a'+1]++; + + sol+=i-1-q(a[c2[i]-'a'+1][f2[c2[i]-'a'+1]]); + + u(a[c2[i]-'a'+1][f2[c2[i]-'a'+1]]); + + } + + + + fout< +#include + + + + +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +char s1[50006],s2[50006]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[30][50001]; + +int n,sol; + +int query (int p){ + + int s=0; + + for (int i=n;i>0;i-=(i&(-i))) + + s=s+a[i]; + + for (int i=p;i>0;i-=(i&(-i))) + + s=s-a[i]; + + return s; + +} + +void update (int p){ + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int main() + +{ + + fin>>s1+1; + + fin>>s2+1; + + n=strlen(s1+1); + + for (int i=1;i<=n;i++){ + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26;i++){ + + if (f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for (int i=1;i<=n;i++){ + + f2[s2[i]-'a'+1]++; + + int x=m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + + + + + update (x); + + sol+=query(x); + + } + + fout< +#include + + +using namespace std; + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + +char s1[50006],s2[50006]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[30][50001]; + +int n,sol; + +int query (int p){ + + int s=0; + + for (int i=n;i>0;i-=(i&(-i))) + + s=s+a[i]; + + for (int i=p;i>0;i-=(i&(-i))) + + s=s-a[i]; + + return s; + +} + +void update (int p){ + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int main() + +{ + + fin>>s1+1; + + fin>>s2+1; + + n=strlen(s1+1); + + for (int i=1;i<=n;i++){ + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26;i++){ + + if (f1[i]!=f2[i]){ + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + + + for (int i=1;i<=n;i++){ + + f2[s2[i]-'a'+1]++; + + int x=m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + + + + + update (x); + + sol+=query(x); + + } + + fout< +#include +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +const int Lmax = 5 + 50000; + + + +deque poz[30]; + + + +string a, b; + + + +int aib[Lmax]; + +int n; + + + +bool verif = true; + + + +void Solve(); + +void Update(int p, int x); + +int Query(int p); + + + +int main() + +{ + + fin >> a >> b; + + + + a = ' ' + a; + + b = ' ' + b; + + + + n = a.size() - 1; + + + + + + Solve(); + + + + if (verif == false) + + { + + fout << -1; + + } + + else + + { + + int ans = 0; + + + + for (int i = 1; i <= n; ++i) + + { + + int p = poz[b[i] - 'a'].front(); + + poz[b[i] - 'a'].pop_front(); + + + + ans += p - 1 - Query(p); + + Update(p, 1); + + } + + + + fout << ans; + + } + +} + + + +void Solve() + +{ + + int f[30] = {0}; + + + + for (int i = 1; i <= n; ++i) + + { + + poz[a[i] - 'a'].push_back(i); + + ++f[b[i]- 'a']; + + } + + + + for (int i = 0; i < 26; ++i) + + if (poz[i].size() != f[i]) + + verif = false; + +} + + + +void Update(int p, int x) + +{ + + for (int i = p; i <= n; i += i & -i) + + aib[i] += x; + +} + + + +int Query(int p) + +{ + + int sum = 0; + + for (int i = p; i >= 1; i -= i & -i) + + sum += aib[i]; + + return sum; + +} diff --git a/swap/aib/fcaf7cc5-0b66-11ec-9258-2cf05d84bfc2.cpp b/swap/aib/fcaf7cc5-0b66-11ec-9258-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cb0f015812a968405946aef82817eeeffdf5503 --- /dev/null +++ b/swap/aib/fcaf7cc5-0b66-11ec-9258-2cf05d84bfc2.cpp @@ -0,0 +1,164 @@ +#include +#include +#include +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +using VI = vector; + +using VVI = vector; + +using VB = vector; + + + +const int NrLit = 26; + + + +int N; + +string a, b; + +VI p[NrLit]; + +VI aib; + + + +void Read(); + +void Solve(); + +void Update(int pos, int val); + +int Query(int pos); + + + +int main() + +{ + + Read(); + + Solve(); + + + + fin.close(); + + fout.close(); + + return 0; + +} + + + +void Read() + +{ + + fin >> a >> b; + + N = (int)a.size(); + + + +} + + + +void Solve() + +{ + + for (int i = N - 1; i >= 0; --i) + + p[a[i] - 'a'].push_back(i); + + + + aib = VI(N + 1); + + + + int ans{0}; + + for (int i = 0; i < N; ++i) + + { + + if (p[b[i] - 'a'].empty()) + + { + + fout << "-1\n"; + + return; + + } + + + + int pos = p[b[i] - 'a'].back(); + + Update(pos + 1, +1); + + p[b[i] - 'a'].pop_back(); + + + + pos = pos + (Query(N) - Query(pos + 1)); + + + + ans += pos - i; + + } + + + + fout << ans << '\n'; + +} + + + +void Update(int pos, int val) + +{ + + for (int i = pos; i <= N; i += i & -i) + + aib[i] += val; + +} + + + +int Query(int pos) + +{ + + int sum{0}; + + + + for (int i = pos; i > 0; i -= i & -i) + + sum += aib[i]; + + + + return sum; + +} diff --git a/swap/aib/fcb70003-8801-4db5-a301-eaf45fd4e70e.cpp b/swap/aib/fcb70003-8801-4db5-a301-eaf45fd4e70e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..95eeb303d6e2b86f2fbb078c5745a1360d6f9eca --- /dev/null +++ b/swap/aib/fcb70003-8801-4db5-a301-eaf45fd4e70e.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + + + +char a[50001], b[50001]; + +int H[50001]; + +int n; + +deque ax[256]; + + + +int x[50001], nx; + +int u1[256], u2[256]; + + + +void read() + +{ + + int i; + + freopen("swap.in","r",stdin); + + fgets(a, 50001, stdin); + + n=0; + + while(a[n]>='a' && a[n]<='z') ++n; + + + + fgets(b, 50001, stdin); + + + + for(i=0;i +#include + + + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +long long n,m,i,s[125][50010],fa[125],fb[125],f[50010],k,sol; + +char a[50010],b[50010]; + +void update(int x) { + + for (;x<=n;x+=(x&-x)) + + f[x]++; + +} + +int query(int x) { + + int val=0; + + for (;x;x-=(x&-x)) + + val+=f[x]; + + return val; + +} + +int main() { + + fin>>a+1>>b+1; + + n=strlen(a+1), m=strlen(b+1); + + if (n!=m) { + + fout<<"-1"; + + return 0; + + } + + for (i=1;i<=n;i++) { + + s[a[i]][++fa[a[i]]]=i; + + fb[b[i]]++; + + } + + for (i='a';i<='z';i++) { + + if (fa[i]!=fb[i]) { + + fout<<"-1"; + + return 0; + + } + + fb[i]=0; + + } + + for (i=1;i<=n;i++) { + + k=query(s[b[i]][++fb[b[i]]]); + + sol+=(i-1-k); + + update(s[b[i]][fb[b[i]]]); + + } + + fout< +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int i,ok=1,n,f[130],f1[130],sol,v[50010],poz[50010],w[50010],nr,cnt,k,a[130][50010]; + +char s[50010],c[50010]; + +int query(int p){ + + int sol=0; + + for(;p;p-=(p&-p)) + + sol+=v[p]; + + return sol; + +} + +void update(int p){ + + for(;p<=n;p+=(p&-p)) + + v[p]++; + +} + +int main(){ + + fin>>(s+1); + + for(i=1;s[i]!=0;i++) + + f[s[i]]++; + + fin>>(c+1); + + for(i=1;c[i]!=0;i++) + + f1[c[i]]++; + + for(char t='a';t<='z';t++) + + if(f[t]!=f1[t]){ + + ok=0; + + break; + + } + + if(!ok){ + + fout<<-1; + + return 0; + + } + + n=strlen(s+1); + + for(i=1;i<=n;i++) + + f[s[i]]=f1[s[i]]=0; + + + + for(i=1;i<=n;i++) + + a[c[i]-'0'][++f[c[i]]]=i; + + + + for(i=1;i<=n;i++){ + + f1[s[i]]++; + + w[i]=a[s[i]-'0'][f1[s[i]]]; + + + + } + + for(i=1;i<=n;i++){ + + sol+=(query(n)-query(w[i])); + + update(w[i]); + + } + + fout< +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +short r[50005]; + +int rez,n,f,g; + +char a[50005],b[50005]; + +priority_queueq[35]; + +void update(int poz) + +{ + + while(poz<=n) + + { + + r[poz]--; + + poz+=(poz&(-poz)); + + } + +} + +int sum(int poz) + +{ + + int sum=0; + + while(poz) + + { + + sum+=r[poz]; + + poz-=(poz&(-poz)); + + } + + return sum; + +} + +int main() + +{ + + fin.getline(a+1,50005); + + fin.getline(b+1,50005); + + n=strlen(a+1); + + for(int i=1;i<=n;i++) + + q[a[i]-'a'].push(i); + + for(int i=n;i>0;i--) + + { + + if(q[b[i]-'a'].empty()) + + { + + fout<<-1; + + return 0; + + } + + f=q[b[i]-'a'].top(); + + q[b[i]-'a'].pop(); + + g=f+sum(f); + + update(f); + + rez+=i-g; + + } + + fout< +#include +#include + +using namespace std; + +ifstream fin ("swap.in"); +ofstream fout ("swap.out"); + +int sp[2][50001][26], n, x, ans; +queue urm[26]; +string s1, s2; + +int main() { + fin >> s1 >> s2; + int n = s1.size(); + if (n != s2.size()) { + fout << -1; + return 0; + } + for (int i = 1; i <= n; ++i) { + ++sp[0][i][s1[i - 1] - 'a']; + ++sp[1][i][s2[i - 1] - 'a']; + urm[s1[i - 1] - 'a'].push(i); + for (int j = 0; j < 26; ++j) { + sp[0][i][j] += sp[0][i - 1][j]; + sp[1][i][j] += sp[1][i - 1][j]; + } + } + for (int j = 0; j < 26; ++j) { + if (sp[0][n][j] != sp[1][n][j]) { + fout << -1; + return 0; + } + } + for (int i = 1; i <= n; ++i) { + x = urm[s2[i - 1] - 'a'].front(); + urm[s2[i - 1] - 'a'].pop(); + for (int j = 0; j < 26; ++j) { + ans += max(0, sp[0][x][j] - sp[1][i][j]); + } + } + fout << ans; + return 0; +} diff --git a/swap/aib/fe82ad63-0b66-11ec-a24f-2cf05d84bfc2.cpp b/swap/aib/fe82ad63-0b66-11ec-a24f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78e1fba299e52d5b25a6eccd7ba87785fe21dacf --- /dev/null +++ b/swap/aib/fe82ad63-0b66-11ec-a24f-2cf05d84bfc2.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 50005; + + + +int aib[NMAX]; + + + +void update(int toadd, int pos, int n) { + + for(; pos <= n; pos += pos & -pos) + + aib[pos] += toadd; + +} + + + +int query(int pos) { + + int sum = 0; + + for(; pos; pos -= pos & -pos) + + sum += aib[pos]; + + return sum; + +} + + + +int main() { + + + + string a, b; + + in >> a >> b; + + + + vector fr(30, 0); + + for(auto it : a) + + fr[it - 'a'] ++; + + for(auto it : b) + + fr[it - 'a'] --; + + bool havesol = 1; + + for(int i = 0; i < 30; i ++) + + if(fr[i] != 0) + + havesol = 0; + + if(!havesol) + + out << -1; + + else { + + a = " " + a; + + b = " " + b; + + vector> pos(30); + + for(int i = 1; i < a.size(); i ++) + + pos[a[i] - 'a'].push_back(i); + + int ans = 0; + + for(int i = 1; i < a.size(); i ++) { + + int j = pos[b[i] - 'a'].front(); + + pos[b[i] - 'a'].pop_front(); + + + + ans += (j - 1 - query(j)); + + update(1, j, a.size()); + + } + + out << ans; + + } + + + + return 0; + +} diff --git a/swap/aib/fea17fff-0b66-11ec-a4a1-2cf05d84bfc2.cpp b/swap/aib/fea17fff-0b66-11ec-a4a1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f382c5ab1b47ce902912e9adbc371be9c79ab3d7 --- /dev/null +++ b/swap/aib/fea17fff-0b66-11ec-a4a1-2cf05d84bfc2.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include +#include + + + + +using namespace std; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +const int NMAX = 50005; + +int aib[NMAX]; + + + +void update(int toadd, int pos, int n) { + + for(; pos <= n; pos += pos & -pos) + + aib[pos] += toadd; + +} + + + +int query(int pos) { + + int ans = 0; + + for(; pos; pos -= pos & -pos) + + ans += aib[pos]; + + return ans; + +} + + + +int main() { + + string a, b; + + in >> a >> b; + + + + vector fr(30, 0); + + for(int i = 0; i < a.size(); i ++) + + fr[a[i] - 'a'] ++; + + for(int i = 0; i < b.size(); i ++) + + fr[b[i] - 'a'] --; + + bool havesol = 1; + + for(int i = 0; i < 30; i ++) + + if(fr[i] != 0) + + havesol = 0; + + + + if(havesol) { + + vector> pos(30); + + a = " " + a; + + b = " " + b; + + for(int i = 1; i < a.size(); i ++) { + + pos[a[i] - 'a'].push_back(i); + + update(1, i, a.size()); + + } + + + + int ans = 0; + + for(int i = 1; i < a.size(); i ++) { + + int x = pos[b[i] - 'a'].front(); + + pos[b[i] - 'a'].pop_front(); + + + + ans += (query(x) - 1); + + update(-1, x, a.size()); + + } + + out << ans; + + + + } else { + + out << -1; + + } + + + + return 0; + +} diff --git a/swap/aib/ff66bc37-42a8-4c87-a724-231fd62b9a87.cpp b/swap/aib/ff66bc37-42a8-4c87-a724-231fd62b9a87.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5fa9f2d82e1996d1c5289468dbb00be763aba59c --- /dev/null +++ b/swap/aib/ff66bc37-42a8-4c87-a724-231fd62b9a87.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + + + +using namespace std; + +char a[50001], b[50001]; +int H[50001]; +int n; +deque ax[256]; + +int x[50001], nx; +int u1[256], u2[256]; + +void read() { + int i; + freopen("swap.in","r",stdin); + fgets(a, 50001, stdin); + n = 0; + while (a[n] >= 'a' && a[n] <= 'z') + ++n; + + fgets(b, 50001, stdin); + + for (i = 0;i < n;++i) + ax[ (int)a[i] ].push_back(i + 1); + + for (i = 0;i < n;++i) { + x[++nx] = ax[ b[i] ][0]; + ax[b[i]].pop_front(); + } +} + +inline void update(int x, int v) { + for (int i = x; i <= n; i += (i & -i)) + H[i] += v; +} + +inline int query(int x) { + int s = 0; + for (int i = x; i ; i -= (i & -i)) + s += H[i]; + return s; +} + +void solve() { + int i, j; + for (i = 1;i <= n;++i) + update(i, 1); + int nr = 0; + for (i = 1;i <= n;++i) { + update(x[i], -1); + nr += query(x[i]); + } + + printf("%d\n", nr); +} + +int main() { + freopen("swap.out","w",stdout); + read(); + int i; + for (i = 0;i < n;++i) + ++u1[ a[i] ], + ++u2[ b[i] ]; + + for (i = 0;i <= 255;++i) + if (u1[i] != u2[i]){ + printf("-1\n"); + return 0; + } + + solve(); + return 0; +} diff --git a/swap/aib/ff75cf63-0b66-11ec-aff5-2cf05d84bfc2.cpp b/swap/aib/ff75cf63-0b66-11ec-aff5-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..de4776f328533cc87c43c11a68e2f96f9635a2fb --- /dev/null +++ b/swap/aib/ff75cf63-0b66-11ec-aff5-2cf05d84bfc2.cpp @@ -0,0 +1,124 @@ +#include + + + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s1[50005]; + +char s2[50005]; + +int fr1[26]; + +int fr2[26]; + +int v[50005]; + +int app[26][50005]; + +int p[50005]; + +int AIB[50005]; + +int n; + +void update(int i) + +{ + + while(i<=n) + + { + + AIB[i]++; + + i+=(i&(-i)); + + } + +} + +int query(int i) + +{ + + int s=0; + + while(i>0) + + { + + s+=AIB[i]; + + i-=(i&(-i)); + + } + + return s; + +} + +int main() + +{ + + f>>(s1+1); + + int i; + + for(i=1;s1[i];i++) + + { + + ++n; + + v[i]=++fr1[s1[i]-'a']; + + } + + f>>(s2+1); + + for(i=1;s2[i];i++) + + app[s2[i]-'a'][++fr2[s2[i]-'a']]=i; + + for(i=0;i<26;i++) + + if(fr1[i]!=fr2[i]) + + { + + g<<-1; + + return 0; + + } + + for(i=1;i<=n;i++) + + p[i]=app[s1[i]-'a'][v[i]]; + + int ans=0; + + for(i=n;i;i--) + + { + + ans+=query(p[i]); + + update(p[i]); + + } + + g< + + + + +using namespace std; + + + +const int Nmax = 50000 + 5; + + + +queue Q[32]; + +char a[Nmax], b[Nmax]; + +int N, i, val, aib[Nmax]; + +long long sol = 0; + + + +void add(int x) + +{ + + int i; + + for (i = x; i <= N; i += (i&(-i))) aib[i]++; + +} + +int sum (int x) + +{ + + int i, s = 0; + + + + for (i = x - 1; i; i -= (i&(-i))) s += aib[i]; + + + + return s; + +} + + + +int main () + + + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a, 50001, stdin); + + fgets(b, 50001, stdin); + + + + N = strlen(a); + + + + for (i = 0; i < N; ++i) + + Q[ b[i] - 'a' ].push(i + 1); + + + + for (i = 0; i < N; ++i) + + { + + if (Q[ a[i] - 'a'].size() == 0) + + { + + printf("-1\n"); + + return 0; + + } + + + + val = Q[a[i] - 'a'].front(); + + Q[a[i] - 'a'].pop(); + + + + sol += sum(val); + + add(val); + + } + + + + printf("%lld\n", 1LL*N * (N - 1) / 2 - sol); + + + + return 0; + +} diff --git a/swap/aib/ff950cfd-0b66-11ec-b739-2cf05d84bfc2.cpp b/swap/aib/ff950cfd-0b66-11ec-b739-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..93c44087f6ac1e1475ab3d86ac3ab1b7781be60e --- /dev/null +++ b/swap/aib/ff950cfd-0b66-11ec-b739-2cf05d84bfc2.cpp @@ -0,0 +1,82 @@ +#include +#include +#include +using namespace std; + +vectorpoz[30]; + +int f[30],f2[30]; + +int v[50005]; + +char a[50005]; + +int aib[50005]; + +int update(int poz,int nr){ + +int i; + +for(i=poz;i<=50000;i=i+(i&(-i))) + +aib[i]+=nr;} + +int query(int poz){ + +int i,s=0; + +for(i=poz;i>0;i=i-(i&(-i))) + +s=s+aib[i]; + +return s;} + +int main(){ + +freopen("swap.in","r",stdin); + +freopen("swap.out","w",stdout); + +int n,rez=0,i; + +char ch; + +fgets(a+1,50003,stdin); + +n=strlen(a+1); + +n--; + +for(i=1;i<=n;i++) + +f[a[i]-96]++; + +for(i=1;i<=n;i++) + +scanf("%c",&ch),f2[ch-96]++,poz[ch-96].push_back(i); + +for(i=1;i<=26;i++) + +if (f[i]!=f2[i]){ + +printf("-1\n"); + +return 0;} + +for(i=n;i>=1;i--) + +v[i]=poz[a[i]-96].back(),poz[a[i]-96].pop_back(); + +for(i=1;i<=n;i++) + +update(i,1); + +for(i=1;i<=n;i++){ + +rez=rez+query(v[i]-1); + +update(v[i],-1);} + +printf("%d\n",rez); + +return 0;} diff --git a/swap/aib/ffb3321c-0b66-11ec-b476-2cf05d84bfc2.cpp b/swap/aib/ffb3321c-0b66-11ec-b476-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce4775f643d1d93203a291e254a224150c9e02ea --- /dev/null +++ b/swap/aib/ffb3321c-0b66-11ec-b476-2cf05d84bfc2.cpp @@ -0,0 +1,107 @@ +#include +#include + + +using namespace std; + +ifstream f ("swap.in"); + +ofstream g ("swap.out"); + +char s[50010],sir[50010]; + +int viz[50],viz2[50],mat[30][50010],v[50010],arb[50010]; + +int n,m,i,total,nr; + +char *p; + +char aux[50010]; + +void update(int poz){ + + for(int i=poz;i<=n;i+=(-i)&i){ + + arb[i]+=1; + + } + +} + +int sum(int poz){ + + int suma=0; + + for(int i=poz;i>0;i-=(-i)&i){ + + suma+=arb[i]; + + } + + return suma; + +} + + + +int main() + +{ + + f.getline(s,50010); + + f.getline(sir,50010); + + n=strlen(s); + + m=strlen(sir); + + if(n!=m){ + + g<<-1; + + return 0; + + } + + for(i=0;i0;--i){ + + total+=sum(v[i]); + + update(v[i]); + + } + + g< +#include +#include +#include +#include +using namespace std; + + + +vector > V(30); + +int N, i, ap1[30], ap2[30], ans, W[50010], Aib[50010]; + +struct data + +{ + + int pos, val; + +}VV[50010]; + +char S1[50010], S2[50010]; + + + +bool cmp(data one, data two) + +{ + + return one.val > two.val; + +} + + + +void Update(int pos) + +{ + + for(; pos <= N; pos += (pos & (-pos))) + + Aib[pos] ++; + +} + + + +int Query(int pos) + +{ + + int rez = 0; + + for(; pos; pos -= (pos & (-pos))) + + rez += Aib[pos]; + + return rez; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + N = strlen(S1); + + for(i = 0; i < N; i++) + + { + + ap1[S1[i] - 'a'] ++; + + ap2[S2[i] - 'a'] ++; + + V[S2[i] - 'a'].push_back(i + 1); + + W[i] = ap1[S1[i] - 'a']; + + } + + for(i = 0; i < 30; i++) + + if(ap1[i] != ap2[i]) + + { + + printf("-1\n"); + + return 0; + + } + + for(i = 0; i < N; i++) + + { + + VV[i].pos = i + 1; + + VV[i].val = V[S1[i] - 'a'][W[i] - 1]; + + } + + sort(VV, VV + N, cmp); + + for(i = 0; i < N; i++) + + { + + ans += Query(VV[i].pos); + + Update(VV[i].pos); + + } + + printf("%i\n", ans); + + return 0; + +} diff --git a/swap/aib/fff23a67-0b66-11ec-9b3d-2cf05d84bfc2.cpp b/swap/aib/fff23a67-0b66-11ec-9b3d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aea2a90354f8530f07c25e092bf196779efee3ac --- /dev/null +++ b/swap/aib/fff23a67-0b66-11ec-9b3d-2cf05d84bfc2.cpp @@ -0,0 +1,95 @@ +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char s1[50001], s2[50001]; + +unsigned short f1[50001],f2[50001],a[50001]; + +unsigned int m[27][50001]; + +int n; + +void update (int p) { + + for (int i=p;i<=n;i+=(i&(-i))) + + a[i]++; + + return; + +} + +int query(int p) { + + int s=0; + + for (int i=n;i>=1;i-=(i&(-i))) + + s+=a[i]; + + for (int i=p;i>=1;i-=(i&(-i))) + + s-=a[i]; + + return s; + +} + +int sol; + +int main() { + + fin >> s1+1; + + fin >> s2+1; + + n = strlen(s1+1); + + for (int i=1; i<=n; i++) { + + f1[s1[i]-'a'+1]++; + + f2[s2[i]-'a'+1]++; + + m[s1[i]-'a'+1][f1[s1[i]-'a'+1]]=i; + + } + + for (int i=1;i<=26; i++) { + + if (f1[i]!=f2[i]) { + + fout<<-1; + + return 0; + + } + + f2[i]=0; + + } + + for (int i=1; i<=n; i++) { + + f2[s2[i]-'a'+1]++; + + int x = m[s2[i]-'a'+1][f2[s2[i]-'a'+1]]; + + update (x); + + sol+=query(x); + + } + + fout << sol; + + return 0; + +} diff --git a/swap/merge/0065f539-2cdc-472f-9c49-235ef56dc76e.cpp b/swap/merge/0065f539-2cdc-472f-9c49-235ef56dc76e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46539ab3b9985d1f1b0331b97d76af6ea9488513 --- /dev/null +++ b/swap/merge/0065f539-2cdc-472f-9c49-235ef56dc76e.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +using namespace std; +const int MAX_N = 50100; +ifstream fin("swap.in"); +ofstream fout("swap.out"); +int alf[27][MAX_N]; +int alfIt[27]; +int v[MAX_N]; +int merge_sort(int, int); +int main() { + string a1, a2; + getline(fin, a1); + getline(fin, a2); + for (unsigned int i = 0; i < a2.size(); ++i) { + int ch = a2[i] - 'a'; + alf[ch][++alf[ch][0]] = i + 1; + } + for (unsigned int i = 0; i < a1.size(); ++i) { + int ch = a1[i] - 'a'; + ++alfIt[ch]; + if (alfIt[ch] > alf[ch][0]) { + fout << -1; + return 0; + } + v[i+1] = alf[ch][alfIt[ch]]; + } + int N = a1.size(); + fout << merge_sort(1, N); + return 0; +} + +int merge_sort(int lo, int hi) { + if (lo == hi) return 0; + int mid = lo + (hi - lo) / 2, numInv; + numInv = merge_sort(lo, mid) + merge_sort(mid + 1, hi); + int aux[MAX_N]; + for (int i = lo, j = mid + 1, k = lo; i <= mid || j <= hi; ++k) { + if ((i <= mid && v[i] < v[j]) || j > hi) { + aux[k] = v[i++]; + } else { + aux[k] = v[j++]; + numInv += (mid - i + 1); + } + } + for (int k = lo; k <= hi; ++k) { + v[k] = aux[k]; + } + return numInv; +} diff --git a/swap/merge/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp b/swap/merge/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/merge/00fd392b-0b67-11ec-8c29-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/01082f74-969c-46ce-87e1-b3ca331823bb.cpp b/swap/merge/01082f74-969c-46ce-87e1-b3ca331823bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cf8987f1eb3d0e107fde86ac4896ef0c89d6e52 --- /dev/null +++ b/swap/merge/01082f74-969c-46ce-87e1-b3ca331823bb.cpp @@ -0,0 +1,178 @@ +#include +using namespace std; + + + +ifstream f("swap.in"); + + + +ofstream g("swap.out"); + + + + + + + +char a[50005], b[50005]; + + + +int u1[27], u2[27], fr[27], p[50005], H[3*50005], sol; + + + +void update(int n, int l, int r, int p, int v) { + + + + if (l >= r) {H[n] += v; return;} + + + + int m = (l + r) >> 1; + + + + if (m >= p) update(n<<1,l,m,p,v); + + + + else update((n<<1)|1,m+1,r,p,v); + + + + H[n] = H[n<<1] + H[(n<<1)|1]; + + + +} + + + +void query(int n, int l, int r, int a, int b) { + + + + if (a <= l && r <= b) {sol += H[n]; return;} + + + + int m = (l + r) >> 1; + + + + if (a <= m) query(n<<1,l,m,a,b); + + + + if (b > m) query((n<<1)|1,m+1,r,a,b); + + + +} + + + + + + + +int main() + + + +{ + + + + for (int i = 0; i < 27; ++i) fr[i] = -1; + + + + f >> a >> b; + + + + int i; + + + + for (i = 0; a[i]; ++i) ++u1[a[i] - 'a']; + + + + int n = i; + + + + for (i = 0; b[i]; ++i) {++u2[b[i] - 'a']; if (fr[b[i]-'a'] == -1) fr[b[i]-'a'] = i;} + + + + for (i = 0; i < 27; ++i) if (u1[i] != u2[i]) {g << "-1\n"; return 0;} + + + + for (i = 0; a[i]; ++i) { + + + + p[i + 1] = fr[a[i]-'a'] + 1; + + + + ++fr[a[i]-'a']; + + + + while (b[fr[a[i]-'a']] != a[i]) { + + + + ++fr[a[i]-'a']; + + + + if (!b[fr[a[i]-'a']]) fr[a[i]-'a'] = 0; + + + + } + + + + } + + + + for (i = 1; i <= n; ++i) update(1,1,n,p[i],1); + + + + for (i = 1; i <= n; ++i) { + + + + if (p[i] > 1) query(1,1,n,1,p[i] - 1); + + + + update(1,1,n,p[i],-1); + + + + } + + + + g << sol << '\n'; + + + + return 0; + + + +} diff --git a/swap/merge/0297081e-0b67-11ec-b2f4-2cf05d84bfc2.cpp b/swap/merge/0297081e-0b67-11ec-b2f4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61615118c4ea6fd2c6d349530db03bb47f802d62 --- /dev/null +++ b/swap/merge/0297081e-0b67-11ec-b2f4-2cf05d84bfc2.cpp @@ -0,0 +1,172 @@ +#include +#include +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int v[50005], n, sol; + +char s[50005]; + +struct aint{ + + int val; + + int lazy; + +} A[200005]; + +queue q[30]; + +void build( int nod, int st, int dr ){ + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( st > dr ) + + return; + + if( st == dr ){ + + A[nod].val = st; + + A[nod].lazy = 0; + + }else{ + + int mid = ( st + dr ) >> 1; + + build( Nst, st, mid ); + + build( Ndr, mid + 1, dr ); + + } + +} + +void update( int nod, int st, int dr, int a, int b ){ + + if( a > b ) + + return; + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( st > dr ) + + return; + + if( a <= st && dr <= b ){ + + A[nod].lazy++; + + }else{ + + int mid = ( st + dr ) >> 1; + + if( a <= mid ) + + update( Nst, st, mid, a, b ); + + if( b > mid ) + + update( Ndr, mid + 1, dr, a, b ); + + } + +} + +int query( int nod, int st, int dr, int pos ){ + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( A[nod].lazy != 0 ){ + + A[nod].val += A[nod].lazy; + + if( st != dr ){ + + A[Nst].lazy += A[nod].lazy; + + A[Ndr].lazy += A[nod].lazy; + + } + + A[nod].lazy = 0; + + } + + if( st == dr ){ + + return A[nod].val; + + }else{ + + int mid = ( st + dr ) >> 1; + + if( pos <= mid ) + + return query( Nst, st, mid, pos ); + + else + + return query( Ndr, mid + 1, dr, pos ); + + } + +} + +int main(){ + + fin >> s; + + n = strlen( s ); + + for( int i = 0; i < n; i++ ){ + + q[ s[i] - 'a' ].push( i + 1 ); + + } + + fin >> s; + + for( int i = 0; i < n; i++ ){ + + if( q[ s[i] - 'a' ].empty() ){ + + fout << "-1\n"; + + return 0; + + } + + v[i + 1] = q[ s[i] - 'a' ].front(); + + q[ s[i] - 'a' ].pop(); + + } + + build( 1, 1, n ); + + for( int i = 1; i <= n; i++ ){ + + sol += query( 1, 1, n, v[i] ) - i; + + update( 1, 1, n, 1, v[i] - 1 ); + + } + + fout << sol << "\n"; + + return 0; + +} diff --git a/swap/merge/02abf05e-57ef-439f-82cc-6453cf87f245.cpp b/swap/merge/02abf05e-57ef-439f-82cc-6453cf87f245.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d769a5a6f93e6aa37dea4ad115183666d155ef24 --- /dev/null +++ b/swap/merge/02abf05e-57ef-439f-82cc-6453cf87f245.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/02d09cfa-14bb-470f-a964-cd5578e345e6.cpp b/swap/merge/02d09cfa-14bb-470f-a964-cd5578e345e6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8633712b7e1d6c8e805ab7c82e7b6f8db8cf46cf --- /dev/null +++ b/swap/merge/02d09cfa-14bb-470f-a964-cd5578e345e6.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int n,i,j,u,nrn,rez,pfrz[250000],vs[250000]; +string s; +vector pos[300]; + +void make(int nd,int st,int dr) +{ + if(st==dr) + { + pfrz[++nrn]=nd; + return; + } + int md=st+(dr-st)/2; + make(2*nd,st,md); + make(2*nd+1,md+1,dr); +} +int take(int nd,int sum) +{ + if(nd==1) return sum; + if(nd/2*2+1!=nd) vs[nd/2*2+1]++; + return take(nd/2,sum+vs[nd]); +} +int main() { + + getline(fin,s); + n=(int)s.size(); + for(i=n-1;i>=0;i--) + pos[s[i]-96].push_back(i); + + make(1,1,n); + rez=-n; + getline(fin,s); + for(i=0;i +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + + + for(j = 0; j < frecB[i].size(); j++) + + { + + + + } + + + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + for(i = 1; i <= n; i++) + + { + + + + } + + + + } + + + +} diff --git a/swap/merge/03ffab01-7aab-4acd-9054-1340cb2a827b.cpp b/swap/merge/03ffab01-7aab-4acd-9054-1340cb2a827b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..539f5169dc1d55a57cc829c8a3a29309b00e851a --- /dev/null +++ b/swap/merge/03ffab01-7aab-4acd-9054-1340cb2a827b.cpp @@ -0,0 +1,90 @@ +#include + + + +int a[50001],b[50001],poz[50001],apar[27][50001],segm[128*50001]; +int a1,b1,sol,cost,value; +FILE *in,*out; + +void ins_del(int nod,int st,int dr) { +int m; + if (a1==st && dr==a1) segm[nod]=value; + + else { + m=(st+dr)/2; + if (a1<=m) ins_del(2*nod,st,m); + else ins_del(2*nod+1,m+1,dr); + segm[nod]=segm[2*nod]+segm[2*nod+1]; + } +} + +void query(int nod,int st,int dr) { +int m; + if (a1<=st && dr<=b1) cost+=segm[nod]; + else { + m=(st+dr)/2; + if (a1<=m) query(2*nod,st,m); + if (b1>m) query(2*nod+1,m+1,dr); + } +} + +int main() { +int i,good=1; +char cache; + in=fopen("swap.in","r"); out=fopen("swap.out","w"); + + cache=fgetc(in); + while (cache!='\n') { + a[++a[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + cache=fgetc(in); + while (cache!='\n' && cache!=EOF) { + b[++b[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + + + for (i=1;i<=a[0];++i) apar[a[i]][++apar[a[i]][0]]=i; + + for (i=0;i<27;++i) apar[i][0]=1; + + value=1; + + for (i=1;i<=b[0];++i) { + if (!apar[b[i]][apar[b[i]][0]]) { good=0; break; } + + else + poz[i]=apar[b[i]][apar[b[i]][0]++]; + a1=i; + ins_del(1,1,a[0]); + } + + value=0; + + if (!good) fprintf(out,"-1\n"); + + else { + + for (i=1;i<=b[0];++i) { + + a1=1; b1=poz[i]; + cost=0; + query(1,1,a[0]); + + sol+=cost-1; + + a1=poz[i]; + ins_del(1,1,a[0]); + + } + + fprintf(out,"%i\n",sol); + } + + fclose(in); fclose(out); + + return 0; +} diff --git a/swap/merge/04314056-0b67-11ec-93dd-2cf05d84bfc2.cpp b/swap/merge/04314056-0b67-11ec-93dd-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4881dd5eb4393cc5cec2d3c644aa9f8a22a0af92 --- /dev/null +++ b/swap/merge/04314056-0b67-11ec-93dd-2cf05d84bfc2.cpp @@ -0,0 +1,260 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + } + + + +} diff --git a/swap/merge/04429ead-e79f-460b-9615-d03af357b8a3.cpp b/swap/merge/04429ead-e79f-460b-9615-d03af357b8a3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0928a89f2b945c3c77724c638aac15e14a1ba641 --- /dev/null +++ b/swap/merge/04429ead-e79f-460b-9615-d03af357b8a3.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +using namespace std; + + + +char a[50111],b[50111]; + +int q,nr[111],nr2[111],*l[111],S,sol,v[50111],A[50111 * 4],i,n,poz[50111]; + + + +void query(int nod,int p,int u,int a,int b){ + + int mij=(p+u)>>1; + + + + if(a<=p && u<=b){ + + S+=A[nod]; + + return; + + } + + + + if(a<= mij) + + query(nod<<1,p,mij,a,b); + + if(b > mij) + + query((nod<<1)+1,mij+1,u,a,b); + + + +} + + + + + +void update(int nod,int p,int u,int poz,int val){ + + int mij=(p+u)>>1; + + + + if(p == u){ + + A[nod] = val; + + return ; + + } + + + + if(poz<=mij) + + update(nod<<1,p,mij,poz,val); + + else + + update((nod<<1)+1,mij+1,u,poz,val); + + + + + + A[nod] = A[nod<<1]+A[(nod<<1)+1]; + +} + + + + + +void afis(){ + + + + FILE *g=fopen("swap.out","w"); + + fprintf(g,"%d",sol); + + fclose(g); + + + +} + + + +int main(){ + + + + FILE *f=fopen("swap.in","r"); + + a[0]=b[0]=' '; + + fscanf(f,"%s",a+1); + + fscanf(f,"%s",b+1); + + + + n=strlen(a); + + + + if(strlen(a) != strlen(b)){ + + sol=-1; + + afis(); + + return 0; + + } + + + + n--; + + + + for(i=1;i<=n;i++) + + nr[a[i]-96]++; + + + + for(i=1;i<=n;i++) + + nr2[b[i]-96]++; + + + + + + for(i=0;i<=28;i++) + + if(nr[i] != nr2[i]){ + + sol=-1; + + afis(); + + return 0; + + } + + + + + + for(i=0;i<=28;i++){ + + l[i]=new int [nr[i]+5]; + + l[i][0]=0; + + nr[i]=0; + + } + + + + for(i=1;i<=n;i++){ + + q=a[i]-96; + + l[q][++l[q][0]] = i; + + } + + + + for(i=1;i<=n;i++){ + + q=b[i] - 96; + + v[i] = l[q][++nr[q]]; + + } + + + +fclose(f); + + + + for(i=1;i<=n;i++) + + poz[v[i]] = i; + + + + for(i=1;i<=n;i++) + + if(v[i] == 1) + + update(1,1,n,i,0); + + else + + update(1,1,n,i,1); + + + + query(1,1,n,1,poz[1]); + + sol=S; + + + + for(i=2;i<=n;i++){ + + update(1,1,n,poz[i-1],0); + + update(1,1,n,poz[i],0); + + S=0; + + query(1,1,n,1,poz[i]); + + sol+=S; + + } + + + +afis(); + + + +return 0; + +} diff --git a/swap/merge/052f9395-2cf4-4d75-a79d-6ac6a1dd68cf.cpp b/swap/merge/052f9395-2cf4-4d75-a79d-6ac6a1dd68cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0658b62970cf652cb4b2bbf6e0cd86eb8583fbc --- /dev/null +++ b/swap/merge/052f9395-2cf4-4d75-a79d-6ac6a1dd68cf.cpp @@ -0,0 +1,92 @@ +# include +char a[50005]; + +char b[50005]; + +int r[26]; + +std :: queue < int > v[26]; + +int s[200005],p[50005]; + +int n; + +void update(int p,int u,int v,int nod) + +{ + + if (p == u) s[nod] = 1; + + else + + { + + int m = (p + u) / 2; + + if (v <= m) update(p,m,v,nod*2); + + else update(m+1,u,v,nod*2+1); + + s[nod] = s[nod*2] + s[nod*2+1]; + + } + +} + +int query(int p,int u,int r,int nod) + +{ + + if (u <= r) return s[nod]; + + int ans = 0; + + int m = (p + u) / 2; + + ans += query(p,m,r,nod*2); + + if (m+1<=r) ans += query(m+1,u,r,nod*2+1); + + return ans; + +} + +int main(void) + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1, 50001, stdin);fgets(b+1, 50001, stdin); + + n = strlen(a+1); + + for (int i = 1;i <= n;++i) a[i] -= 'a',b[i] -= 'a'; + + for (int i = 1;i <= n;++i) ++r[a[i]],--r[b[i]]; + + for (int i = 0;i < 26;++i) if (r[i]) + + return printf("-1\n"),0; + + for (int i = 1;i <= n;++i) + + v[b[i]].push(i); + + int ans = 0; + + for (int i = 1;i <= n;++i) + + { + + p[i] = n - v[a[i]].front() + 1;v[a[i]].pop(); + + } + + for (int i = 1;i <= n;++i) ans += query(1,n,p[i],1),update(1,n,p[i],1); + + return printf("%d\n",ans),0; + +} diff --git a/swap/merge/054fa26d-6d41-48f3-b042-ae6bbd41925b.cpp b/swap/merge/054fa26d-6d41-48f3-b042-ae6bbd41925b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4881dd5eb4393cc5cec2d3c644aa9f8a22a0af92 --- /dev/null +++ b/swap/merge/054fa26d-6d41-48f3-b042-ae6bbd41925b.cpp @@ -0,0 +1,260 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + } + + + +} diff --git a/swap/merge/0653c159-0b67-11ec-b429-2cf05d84bfc2.cpp b/swap/merge/0653c159-0b67-11ec-b429-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec4b1e6d498fe7ec83c8a619c8429332d614f791 --- /dev/null +++ b/swap/merge/0653c159-0b67-11ec-b429-2cf05d84bfc2.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + + + +int f[1001], INV1 = 0, INV2 = 0, pos[201][50001], d[50001], LE[50001], C[50001]; + +char a[50002], b[50002]; + +int c[50002]; + +inline void Merge(int st, int mij, int dr){ + + int i = st, j = mij + 1, NR = 0; + + while(i <= mij && j <= dr){ + + if(d[i] > d[j]){ + + c[++NR] = d[j++]; + + INV1 = INV1 + mij - i + 1; + + } + + else + + c[++NR] = d[i++]; + + } + + while(j <= dr) + + c[++NR] = d[j++]; + + while(i <= mij) + + c[++NR] = d[i++]; + + for(i = st , j = 1; i <= dr ; ++i, ++j) + + d[i] = c[j]; + +} + +inline void MergeSort(int st, int dr){ + + if(st >= dr) return ; + + MergeSort(st, (st + dr) / 2); + + MergeSort((st + dr) / 2 + 1, dr); + + Merge(st, (st + dr) / 2, dr); + +} + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + scanf("%s", a); + + scanf("%s", b); + + int L = strlen(a); + + for(int i = 0 ; i < L ; ++i) + + ++f[a[i]]; + + for(int i = 0 ; i < L ; ++i){ + + if(f[b[i]] == 0) + + {printf("-1"); return 0;} + + --f[b[i]]; + + } + + for(int i = 0 ; i < L ; ++i) + + pos[a[i]][++LE[a[i]]] = i + 1; + + for(int i = 0 ; i < L ; ++i) + + d[i] = pos[b[i]][++C[b[i]]]; + + MergeSort(0, L - 1); + + printf("%d", INV1); + + return 0; + +} diff --git a/swap/merge/0694a9d1-0b67-11ec-92f3-2cf05d84bfc2.cpp b/swap/merge/0694a9d1-0b67-11ec-92f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48ab10d62d7df2146cd5fd460177024a0e9b1d2b --- /dev/null +++ b/swap/merge/0694a9d1-0b67-11ec-92f3-2cf05d84bfc2.cpp @@ -0,0 +1,271 @@ +#include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/074a746c-6c41-4bc8-98c8-2bdbabba7ac0.cpp b/swap/merge/074a746c-6c41-4bc8-98c8-2bdbabba7ac0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365de5cf7c67d0626c61fb8054d15be54b13f83f --- /dev/null +++ b/swap/merge/074a746c-6c41-4bc8-98c8-2bdbabba7ac0.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +long nr[50100],NR[50100],i,n,num,P[50100],B[50100],a[50100][30],A[50100]; + +char s[50100],t[50100]; + +void merge (int st,int dr ) { + + + + if(st==dr) + + return ; + + int m=(st+dr)>>1; + + + + int i,j,p; + + merge(st,m); + + merge(m+1,dr); + + i=st;j=m+1;p=0; + + while(i<=m || j<=dr ){ + + if((P[j]m){ + + B[st+p++]=P[j++]; + + num+=m-i+1; + + } + + else + + B[st+p++]=P[i++]; + + } + + for(int u=st;u<=dr;++u){ + + P[u]=B[u]; + + B[u]=0; + + } + +} + +int main (){ + + f>>(s+1); + + f.get();f>>(t+1); + + f.get(); + + n=strlen(s+1); + + for(i=1;i<=n;++i){ + + a[++nr[s[i]-96]][s[i]-96]=i; + + + + } + + + + for(i=1;i<=n;i++){ + + + + P[a[++NR[t[i]-96]][t[i]-96]]=i; + + + + } + + for(i=0;i<=26;i++) + + if(NR[i]!=nr[i]){ + + g<<-1<<"\n"; + + + + return 0; + + } + + merge(1,n); + + g< +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/07c628e4-fbd4-4f22-b7d0-2391d763bc3d.cpp b/swap/merge/07c628e4-fbd4-4f22-b7d0-2391d763bc3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27fa7cfd0360f54c2fd1e145b8912faa5ca01c2d --- /dev/null +++ b/swap/merge/07c628e4-fbd4-4f22-b7d0-2391d763bc3d.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/081a1b2e-0b67-11ec-b543-2cf05d84bfc2.cpp b/swap/merge/081a1b2e-0b67-11ec-b543-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..234dcf008b6afeae6145f8d9813f2b2abf79fdcd --- /dev/null +++ b/swap/merge/081a1b2e-0b67-11ec-b543-2cf05d84bfc2.cpp @@ -0,0 +1,138 @@ +#include + + + + + + +using namespace std; + +char a[50005],b[50005]; + +int n,i,j,aux; + +int x[50005],y[50005]; + +int cat1[150][50005]; + +int ap[150]; + +int A[150][50005]; + +pair v[50005]; + +int sol; + +char c; + +void Merge(int st,int mij,int dr) + +{ + + int k=0; + + int i=st,j=mij+1; + + while(i<=mij && j<=dr) + + if(x[i]<=x[j]) + + y[++k]=x[i++]; + + else + + y[++k]=x[j++],sol+=mij+1-i; + + while(i<=mij) + + y[++k]=x[i++]; + + while(j<=dr) + + y[++k]=x[j++]; + + for(k=1,i=st;i<=dr;i++,k++) + + x[i]=y[k]; + +} + +void MergeSort(int st,int dr) + +{ + + int mij; + + if(st + + + + +using namespace std; + + + +ifstream fin("swap.in") ; + +ofstream fout("swap.out") ; + + + +string sir1,sir2 ; + +int ax[50005] , nrpr[50005] ; + +int n , p[50005] , t[50005] ,inv , v[50005] ; + +vector graf[50005] , graf2[50005] ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft == rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( p[x] <= p[y] ) + + t[i++] = p[x++] ; + + else + + { + + t[i++] = p[y++] ; + + inv = inv +mij-x+1 ; + + } + + } + + while ( x <= mij ) + + t[i++] = p[x++] ; + + while ( y <= rght ) + + t[i++] = p[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + p[i] = t[i] ; + +} + + + +int main() + +{ + + int i , j ,m; + + fin >> sir1 ; + + fin >> sir2 ; + + sir1 = '#' + sir1 ; + + sir2 = '#' + sir2 ; + + n = sir1.size()-1 ; + + m = sir2.size()-1 ; + + if ( n!= m ) + + { + + fout << "-1" ; + + return 0 ; + + } + + for ( i = 1 ; i <= n ; i++ ) + + nrpr[sir1[i]-'a'+1]++ , nrpr[sir2[i]-'a'+1]-- ; + + for ( i = 1 ; i <= n ; i++ ) + + { + + if ( nrpr[i] != 0 ) + + { + + fout << "-1" ; + + return 0 ; + + } + + } + + for ( i = 1 ; i <= n ; i++ ) + + { + + graf[sir1[i]-'a'+1].push_back(i) ; + + graf2[sir2[i]-'a'+1].push_back(i) ; + + } + + for ( i = 1 ; i <= 27 ; i++ ) + + for ( j = 0 ; j < graf[i].size() ; j++ ) + + p[graf[i][j]] = graf2[i][j] ; + + mergemerge(1,n) ; + + fout << inv ; + +} diff --git a/swap/merge/090529fe-0b67-11ec-90ab-2cf05d84bfc2.cpp b/swap/merge/090529fe-0b67-11ec-90ab-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e9a019c44fb7c03d6be7223145f796da5415fd --- /dev/null +++ b/swap/merge/090529fe-0b67-11ec-90ab-2cf05d84bfc2.cpp @@ -0,0 +1,145 @@ +#include + + + + +using namespace std; + +int aib[50005]; + +char a[50005],b[50005]; + +int n,i,j,aux; + +int x[50005],y[50005]; + +int cat1[150][50005]; + +int cat2[150][50005]; + +int A[150][50005]; + +pair v[50005]; + +long long sol; + +bool ok; + +void interclas(int st,int m,int dr) + +{ + + int k=0,i=st,j=m+1; + + while(i<=m &&j<=dr) + + if(x[i]<=x[j])y[++k]=x[i++]; + + else {y[++k]=x[j++];sol=(sol+m-i+1);} + + while(i<=m)y[++k]=x[i++]; + + while(j<=dr)y[++k]=x[j++]; + + for(k=1,i=st;i<=dr;i++,k++)x[i]=y[k]; + +} + +void sortt(int st,int dr) + +{ + + int m; + + if(st +#include +#include +using namespace std; + +const int Q_sze=30; + +const int Maxx=5e4+1; + +deque Q[Q_sze]; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int aint[4*Maxx],vis[4*Maxx]; + +int i,n,ans,len,m; + +string s; + +void build(int node,int left,int right); + +int query(int node,int tot); + +int main() + +{ + + fin>>s; + + n=s.size(); + + for (i=n-1;i>=0;--i) Q[s[i]-96].push_back(i); + + build(1,1,n); + + fin>>s; + + for (i=0;i +#include +#include +#include + + +const int DIM = 50100; + +const int EXP = 26; + +using namespace std; + + + +int N, ok, Aux[DIM]; long long sol; char A[DIM], B[DIM]; + +vector L[EXP]; int P[DIM], S1[EXP], S2[EXP], V[DIM]; + + + +void merge_sort( int left, int right ) { + + if( left == right ) return; + + + + int middle = left + ( right - left ) / 2; + + int index1, index2 = middle, pos = left; + + + + merge_sort( left, middle ); + + merge_sort( middle + 1, right ); + + + + for( index1 = left; index1 <= middle; index1 ++ ) { + + while( index2 < right && P[index1] > P[index2 + 1] ) + + index2 ++; + + sol += index2 - middle; + + } + + + + index1 = left; index2 = middle + 1; + + while( index1 <= middle || index2 <= right ) { + + if( index1 > middle ) Aux[pos++] = P[index2++]; else + + if( index2 > right ) Aux[pos++] = P[index1++]; else + + if( P[index1] <= P[index2] ) + + Aux[pos++] = P[index1++]; + + else + + Aux[pos++] = P[index2++]; + + } + + + + for( int i = left; i <= right; i ++ ) + + P[i] = Aux[i]; + + + + return; + +} + + + +int main () { + + + + freopen( "swap.in" , "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + scanf( "%s %s", A + 1, B + 1 ); + + N = strlen( A + 1 ); + + + + for( int i = 1; i <= N; i ++ ) { + + S1[A[i] - 'a'] ++; + + S2[B[i] - 'a'] ++; + + V[i] = S1[A[i] - 'a']; + + L[B[i] - 'a'].push_back( i ); + + } + + + + ok = 1; + + for( int i = 0; i < 26; i ++ ) + + if( S1[i] != S2[i] ) ok = 0; + + + + if( ok == 0 ) + + printf( "-1\n" ); + + else { + + for( int i = 1; i <= N; i ++ ) + + P[i] = L[A[i] - 'a'][V[i] - 1]; + + + + merge_sort( 1, N ); + + printf( "%lld\n", sol ); + + } + + + + return 0; + +} diff --git a/swap/merge/09b3f04f-558a-4406-9410-ca2a4678e660.cpp b/swap/merge/09b3f04f-558a-4406-9410-ca2a4678e660.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e54c2a77c6d484e9feee6e0aabd2b78f3021a613 --- /dev/null +++ b/swap/merge/09b3f04f-558a-4406-9410-ca2a4678e660.cpp @@ -0,0 +1,208 @@ +#include + + + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int perm[50000], a[4*50000]; + +char s[50000], pattern[50000]; + +int freq[27], evan[50000]; + +vector< int > pos[27]; + +pair v[50000]; + + + +void update(int nod){ + + while(nod!=1){ + + nod/=2; + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + +long long query(int lo, int hi){ + + long long left=0, right=0; + + if(lo==hi){ + + return a[lo]; + + } + + else if(lo a, pair b){ + + return a.second>s>>pattern; + + n=strlen(s); + + while(start +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +vector h[27]; + +vector L[27]; + +int P[50010], n, cnt; + +char a[50010], b[50010]; + + + +void Citire() + +{ + + int i, j, ok; + + fin >> a >> b; + + n = strlen(a); + + + + for (i=n; i>=1; i--) + + { + + a[i] = a[i-1]; + + b[i] = b[i-1]; + + } + + + + for (i=0; i<=26; i++) + + { + + for (j=1; j<=n; j++) + + { + + if (b[j] - 'a' == i) + + h[i].push_back(j); + + if (a[j] - 'a' == i) + + L[i].push_back(j); + + } + + } + + + + ok = 1; + + for (i=0; i<=26 && ok==1; i++) + + if (h[i].size() != L[i].size()) + + ok = 0; + + + + if (ok == 0) + + { + + fout << "-1\n"; + + exit(0); + + } + + + + for (i=n; i>=1; i--) + + { j = a[i]-'a'; + + P[i] = h[j][h[j].size()-1]; + + h[j].pop_back(); + + } + +} + + + +void Interclasare(int st, int m, int dr) + +{ + + int Q[50009], i, j, k; + + i = st; + + j = m+1; + + k = 0; + + while (i <= m && j <= dr) + + { + + if (P[i] <= P[j]) + + Q[++k] = P[i++]; + + else + + { + + Q[++k] = P[j++]; + + cnt += (m - i + 1); + + } + + } + + while (i <= m) + + Q[++k] = P[i++]; + + while (j <= dr) + + Q[++k] = P[j++]; + + + + j = st; + + for (i = 1; i <= k; ++i) + + P[j++] = Q[i]; + +} + + + +void Interschimba(int i, int j) + +{ + + if (P[i] > P[j]) + + { + + cnt++; + + swap(P[i], P[j]); + + } + +} + + + +void MergeSort(int st, int dr) + +{ + + int m, i; + + if (dr - st <= 1) Interschimba(st, dr); + + else + + { + + m = (st + dr) / 2; + + MergeSort(st, m); + + MergeSort(m + 1, dr); + + Interclasare(st, m, dr); + + } + +} + + + +int main () + +{ + + Citire(); + + MergeSort(1,n); + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/merge/0abc1d86-0b67-11ec-be38-2cf05d84bfc2.cpp b/swap/merge/0abc1d86-0b67-11ec-be38-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/0abc1d86-0b67-11ec-be38-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/0ace7224-2724-4348-af1c-fc17b8d753ac.cpp b/swap/merge/0ace7224-2724-4348-af1c-fc17b8d753ac.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9a05aa28c4a48148f0ad05ced519cb79fcfa4d2 --- /dev/null +++ b/swap/merge/0ace7224-2724-4348-af1c-fc17b8d753ac.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +using namespace std; + +const int MAX_N = 50002; +const int SIGMA = 27; + +int N; +int A[5*MAX_N], cnt_s1[SIGMA], cnt_s2[SIGMA]; +queue < int > pos[SIGMA]; +char s1[MAX_N], s2[MAX_N]; + +void update(int node, int left, int right, int x, int val) { + if(left == right) + A[node] += val; + else { + int ls = 2*node, rs = 2*node + 1, m = (left + right)/2; + + if(x <= m) + update(ls, left, m, x, val); + else + update(rs, m + 1, right, x, val); + A[node] = A[ls] + A[rs]; + } +} + +int query(int node, int left, int right, int x, int y) { + if(x <= left && right <= y) + return A[node]; + else { + int ls = 2*node, rs = 2*node + 1, m = (left + right)/2; + + int ans = 0; + if(x <= m) + ans += query(ls, left, m, x, y); + if(y > m) + ans += query(rs, m + 1, right, x, y); + + return ans; + } +} + +int main() { + ifstream f("swap.in"); + ofstream g("swap.out"); + + f >> s1; + f >> s2; + + int len = strlen(s1); + for(int i = 0; i < len; ++i) + ++cnt_s1[s1[i] - 'a']; + for(int i = 0; i < len; ++i) + ++cnt_s2[s2[i] - 'a']; + + bool ok = 1; + for(int i = 0; i < SIGMA; ++i) + if(cnt_s1[i] != cnt_s2[i]) + ok = 0; + int ans = 0; + if(ok) { + for(int i = len; i >= 1; --i) + s1[i] = s1[i-1]; + for(int i = len; i >= 1; --i) + s2[i] = s2[i-1]; + for(int i = 1; i <= len; ++i) + pos[s1[i] - 'a'].push(i); + for(int i = 1; i <= len; ++i) { + int p = pos[s2[i] - 'a'].front(); + pos[s2[i] - 'a'].pop(); + int temp; + if(p < len) + temp = query(1, 1, len, p + 1, len); + else temp = 0; + ans += p + temp - i; + update(1, 1, len, p, 1); + } + } + else ans = -1; + + g << ans << "\n"; + + f.close(); + g.close(); + + return 0; +} diff --git a/swap/merge/0b5f4892-632a-48e9-8275-e6a76861c769.cpp b/swap/merge/0b5f4892-632a-48e9-8275-e6a76861c769.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6362261faf1c7814fc9eed488117d70815fe9ca --- /dev/null +++ b/swap/merge/0b5f4892-632a-48e9-8275-e6a76861c769.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + + + + +char buffer[2 * 50000 + 2]; +std::vector position[26]; +int start[26]; + +int P[50000]; +int tmp[50000]; + +int n; + +inline int mergeCount(int left, int mid, int right) { + int invCount = 0; + int i, j, k; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] <= P[j]) { + tmp[k++] = P[i++]; + } else { + tmp[k++] = P[j++]; + invCount += (mid - i + 1); + } + } + while (i <= mid) { + tmp[k++] = P[i++]; + } + while (j <= right) { + tmp[k++] = P[j++]; + } + for (i = left; i <= right; i++) + P[i] = tmp[i]; + return invCount; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) / 2; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, mid, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int ptr = 0; + int character; + + fread(buffer, 1, 2 * 50000 + 2, f); + fclose(f); + + do { + character = buffer[ptr] - 'a'; + position[character].emplace_back(ptr); + } while (isalpha(buffer[++ptr])); + + ptr++; + n = 0; + + do { + character = buffer[ptr] - 'a'; + if (start[character] < (int) position[character].size()) { + P[n++] = position[character][start[character]++]; + } else { + n = -1; + } + } while ((~n) && (isalpha(buffer[++ptr]))); + + f = fopen("swap.out", "w"); + if (~n) { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } else { + fputs("-1\n", f); + } + fclose(f); + return 0; +} diff --git a/swap/merge/0c212206-0b67-11ec-a6c7-2cf05d84bfc2.cpp b/swap/merge/0c212206-0b67-11ec-a6c7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88ca8ccdf25aa83e310951d6097b821c6c3a811b --- /dev/null +++ b/swap/merge/0c212206-0b67-11ec-a6c7-2cf05d84bfc2.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + + +char A[50000 + 2], B[50000 + 2]; + +int next[50000]; +int c[26]; + +int P[50000]; +int tmp[50000]; +int n; + + +inline int mergeCount(int left, int right) { + int i, j, k; + int mid = (left + right) / 2; + int cnt = 0; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] < P[j]) { + tmp[k] = P[i]; + i++; + } else { + cnt += (mid - i + 1); + tmp[k] = P[j]; + j++; + } + k++; + } + while (i <= mid) { + tmp[k++] = P[i]; + i++; + } + while (j <= right) { + tmp[k++] = P[j]; + j++; + } + memcpy(P + left, tmp + left, (right - left + 1) << 2); + return cnt; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) >> 1; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int pos, character; + + fgets(A, 50000 + 2, f); + fgets(B, 50000 + 2, f); + fclose(f); + + n = strlen(A) - 1; + + + for (int i = 0; i < 26; i++) { + c[i] = -1; + } + + for (int i = n - 1; i >= 0; i--) { + character = A[i] - 'a'; + next[i] = c[character]; + c[character] = i; + } + + + + + pos = 0; + do { + character = B[pos] - 'a'; + P[pos] = c[character]; + c[character] = next[c[character]]; + pos++; + } while ((pos < n) && (P[pos - 1] != -1)); + + f = fopen("swap.out", "w"); + if (pos != n || P[n - 1] == -1) { + fputs("-1\n", f); + } else { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } + fclose(f); + return 0; +} diff --git a/swap/merge/0c38e413-23dc-4b9d-940b-c039a5159ac3.cpp b/swap/merge/0c38e413-23dc-4b9d-940b-c039a5159ac3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..545356933bcf100511c6769371b374e0cf97ad2b --- /dev/null +++ b/swap/merge/0c38e413-23dc-4b9d-940b-c039a5159ac3.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50005; + +int n,nr,i,j,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); + + sort(m+1,dr); + + merge(st,dr,m); + + } + +} + +int main() + +{ f>>a+1>>b+1; + + n=strlen(a+1); + + for(i=1;i<=n;++i) + + { ++x[a[i]-'a']; ++y[b[i]-'a']; + + L[b[i]-'a'].push_back(i); + + } + + int e=0; + + for(i=0;i<26;++i) + + { if(x[i]!=y[i]) e=1; + + x[i]=0; + + } + + if(e) g<<"-1\n"; else + + { + + for(i=1;i<=n;++i) + + { + + j=x[a[i]-'a']++; + + p[i]=L[a[i]-'a'][j]; + + } + + sort(1,n); + + g< +#include +#include +#include + + + + +char buffer[2 * 50000 + 2]; +std::vector position[26]; +int start[26]; + +int P[50000]; +int tmp[50000]; + +int n; + +inline int mergeCount(int left, int mid, int right) { + int invCount = 0; + int i, j, k; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] <= P[j]) { + tmp[k++] = P[i++]; + } else { + tmp[k++] = P[j++]; + invCount += (mid - i + 1); + } + } + while (i <= mid) { + tmp[k++] = P[i++]; + } + while (j <= right) { + tmp[k++] = P[j++]; + } + for (i = left; i <= right; i++) + P[i] = tmp[i]; + return invCount; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) / 2; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, mid, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int ptr = 0; + int character; + + fread(buffer, 1, 2 * 50000 + 2, f); + fclose(f); + + do { + character = buffer[ptr] - 'a'; + position[character].emplace_back(ptr); + } while (isalpha(buffer[++ptr])); + + ptr++; + n = 0; + + do { + character = buffer[ptr] - 'a'; + if (start[character] < (int) position[character].size()) { + P[n++] = position[character][start[character]++]; + } else { + n = -1; + } + } while ((~n) && (isalpha(buffer[++ptr]))); + + f = fopen("swap.out", "w"); + if (~n) { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } else { + fputs("-1\n", f); + } + fclose(f); + return 0; +} diff --git a/swap/merge/0cca3a27-7a71-4534-85ca-56b6dca717ff.cpp b/swap/merge/0cca3a27-7a71-4534-85ca-56b6dca717ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0a14251742e1fa14a92527d1f9c20d09da8b66 --- /dev/null +++ b/swap/merge/0cca3a27-7a71-4534-85ca-56b6dca717ff.cpp @@ -0,0 +1,284 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + + + for(j = 0; j < frecB[i].size(); j++) + + { + + + + } + + + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + for(i = 1; i <= n; i++) + + { + + + + } + + + + } + + + +} diff --git a/swap/merge/0cf0b645-9314-4b3e-a561-9c5f18748507.cpp b/swap/merge/0cf0b645-9314-4b3e-a561-9c5f18748507.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/merge/0cf0b645-9314-4b3e-a561-9c5f18748507.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/0d177066-0b67-11ec-b867-2cf05d84bfc2.cpp b/swap/merge/0d177066-0b67-11ec-b867-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6199e366d57b479e3822e8dc4d4275fdc7c67cbe --- /dev/null +++ b/swap/merge/0d177066-0b67-11ec-b867-2cf05d84bfc2.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/merge/0de29120-32be-459a-aff2-35bff7dda791.cpp b/swap/merge/0de29120-32be-459a-aff2-35bff7dda791.cpp new file mode 100644 index 0000000000000000000000000000000000000000..89cf5189aee3b82bdda61790c8d1d55cdcccbf5e --- /dev/null +++ b/swap/merge/0de29120-32be-459a-aff2-35bff7dda791.cpp @@ -0,0 +1,200 @@ +#include +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +ifstream f ("swap.in"); + +ofstream g ("swap.out"); + + + +const int NMAX = 50005; + +const int SIGMA = 30; + +int v[NMAX], ans; + +string s; + +queue q[SIGMA]; + + + +struct Node { + + int val, lazy; + + Node() { + + val = lazy = 0; + + } + +}segmTree[4 * NMAX]; + + + +void build (int node, int left, int right) { + + if (left == right) { + + segmTree[node].val = left; + + return; + + } + + int mid = left + (right - left) / 2; + + build (2 * node + 1, left, mid); + + build (2 * node + 2, mid + 1, right); + +} + + + +void update (int node, int left, int right, int x, int y) { + + if (x <= left && right <= y) { + + ++segmTree[node].lazy; + + return; + + } + + int mid = left + (right - left) / 2; + + if (x <= mid) { + + update (2 * node + 1, left, mid, x, y); + + } + + if (mid < y) { + + update (2 * node + 2, mid + 1, right, x, y); + + } + +} + + + +void propagate (int node, int left, int right) { + + if (segmTree[node].lazy) { + + segmTree[node].val += segmTree[node].lazy; + + if (left != right) { + + segmTree[2 * node + 1].lazy += segmTree[node].lazy; + + segmTree[2 * node + 2].lazy += segmTree[node].lazy; + + } + + segmTree[node].lazy = 0; + + } + +} + + + +int query (int node, int left, int right, int pos) { + + propagate (node, left, right); + + + + if (left == right) { + + return segmTree[node].val; + + } + + int mid = left + (right - left) / 2; + + if (pos <= mid) { + + return query (2 * node + 1, left, mid, pos); + + } else { + + return query (2 * node + 2, mid + 1, right, pos); + + } + +} + + + +int main() { + + f >> s; + + int n = s.size(); + + for (int i = 0; i < n; ++i) { + + q[s[i] - 'a'].push (i); + + } + + f >> s; + + for (int i = 0; i < n; ++i) { + + if (q[s[i] - 'a'].empty()) { + + g << "-1\n"; + + return 0; + + } + + v[i] = q[s[i] - 'a'].front(); + + q[s[i] - 'a'].pop(); + + } + + build (0, 0, n - 1); + + for (int i = 0; i < n; ++i) { + + ans += query (0, 0, n - 1, v[i]) - i; + + if (v[i] == 0) continue; + + update (0, 0, n - 1, 0, v[i] - 1); + + } + + + + g << ans << '\n'; + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/swap/merge/0df4533f-0b67-11ec-87f1-2cf05d84bfc2.cpp b/swap/merge/0df4533f-0b67-11ec-87f1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/0df4533f-0b67-11ec-87f1-2cf05d84bfc2.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +char a[50005]; + +char b[50005]; + +int r[26]; + +std :: queue < int > v[26]; + +int s[200005],p[50005]; + +int n; + +void update(int p,int u,int v,int nod) + +{ + + if (p == u) s[nod] = 1; + + else + + { + + int m = (p + u) / 2; + + if (v <= m) update(p,m,v,nod*2); + + else update(m+1,u,v,nod*2+1); + + s[nod] = s[nod*2] + s[nod*2+1]; + + } + +} + +int query(int p,int u,int r,int nod) + +{ + + if (u <= r) return s[nod]; + + int ans = 0; + + int m = (p + u) / 2; + + ans += query(p,m,r,nod*2); + + if (m+1<=r) ans += query(m+1,u,r,nod*2+1); + + return ans; + +} + +int main(void) + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1, 50001, stdin);fgets(b+1, 50001, stdin); + + n = strlen(a+1); + + for (int i = 1;i <= n;++i) a[i] -= 'a',b[i] -= 'a'; + + for (int i = 1;i <= n;++i) ++r[a[i]],--r[b[i]]; + + for (int i = 0;i < 26;++i) if (r[i]) + + return printf("-1\n"),0; + + for (int i = 1;i <= n;++i) + + v[b[i]].push(i); + + int ans = 0; + + for (int i = 1;i <= n;++i) + + { + + p[i] = n - v[a[i]].front() + 1;v[a[i]].pop(); + + } + + for (int i = 1;i <= n;++i) ans += query(1,n,p[i],1),update(1,n,p[i],1); + + return printf("%d\n",ans),0; + +} diff --git a/swap/merge/0fcb6edf-4f89-405a-bba9-4ba5eb1fabf3.cpp b/swap/merge/0fcb6edf-4f89-405a-bba9-4ba5eb1fabf3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91e5980b883af7ab699546a07df69b09f2508383 --- /dev/null +++ b/swap/merge/0fcb6edf-4f89-405a-bba9-4ba5eb1fabf3.cpp @@ -0,0 +1,117 @@ +# include +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/0ff75c9c-0b67-11ec-8ad3-2cf05d84bfc2.cpp b/swap/merge/0ff75c9c-0b67-11ec-8ad3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4ba596157bc3eba5af4ca2864e897707820540d --- /dev/null +++ b/swap/merge/0ff75c9c-0b67-11ec-8ad3-2cf05d84bfc2.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char beg[50001]; + +int sir[50001],arb[3*50001],viz[50001],i,n,poz,pozst,pozdr,ch,nr; + +long long sum; + +vector G[50]; + +void upDate(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + arb[nod]=1; + + return; + + } + + int mijl=(st+dr)/2; + + if(poz<=mijl) + + upDate(2*nod,st,mijl); + + else + + upDate(2*nod+1,mijl+1,dr); + + arb[nod]=arb[2*nod]+arb[2*nod+1]; + +} + +void query(int nod, int st, int dr) + +{ + + if(st>=pozst&&dr<=pozdr) + + { + + sum+=arb[nod]; + + return; + + } + + int mijl=(st+dr)/2; + + if(pozst<=mijl) + + query(2*nod,st,mijl); + + if(pozdr>mijl) + + query(2*nod+1,mijl+1,dr); + +} + +int main() + +{ + + fin>>(beg+1); + + n=strlen(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + G[ch].push_back(i); + + } + + fin>>(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + if(viz[ch] +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +#include +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i, n, perm[50010], lastap[50010], poz[27][50010], ind[27], vtmp[50010], nr, k, ap1[27], ap2[27]; + +char a[50010], b[50010]; + + + +void interc(int st, int mid, int dr) + +{ + + int i=0, j=0, k=0; + + for (i=st, j=mid+1; i<=mid && j<=dr; ) { + + if (perm[i] > perm[j]) { + + vtmp[++k]=perm[j]; + + j++; + + nr=nr+mid-i+1; + + } + + else { + + vtmp[++k]=perm[i]; + + i++; + + } + + } + + for (; i<=mid; i++) + + vtmp[++k]=perm[i]; + + for (; j<=dr; j++) + + vtmp[++k]=perm[j]; + + k=0; + + for (i=st; i<=dr; i++) + + perm[i]=vtmp[++k]; + +} + +void divimp(int st, int dr) + +{ + + if (dr-st<=1) { + + if (perm[st] > perm[dr]) { + + nr++; + + int tmp=perm[st]; + + perm[st]=perm[dr]; + + perm[dr]=tmp; + + } + + } + + else { + + int mid=(st+dr)/2; + + divimp(st, mid); + + divimp(mid+1, dr); + + interc(st, mid, dr); + + } + +} + + + +int main() + +{ + + f.get(a, 50001); + + f.get(); + + f.get(b, 50001); + + n=strlen(a); + + for (i=0; i +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +vector h[27]; + +vector L[27]; + +int P[50010], n, cnt; + +char a[50010], b[50010]; + + + +void Citire() + +{ + + int i, j, ok; + + fin >> a >> b; + + n = strlen(a); + + + + for (i=n; i>=1; i--) + + { + + a[i] = a[i-1]; + + b[i] = b[i-1]; + + } + + + + for (i=0; i<=26; i++) + + { + + for (j=1; j<=n; j++) + + { + + if (b[j] - 'a' == i) + + h[i].push_back(j); + + if (a[j] - 'a' == i) + + L[i].push_back(j); + + } + + } + + + + ok = 1; + + for (i=0; i<=26 && ok==1; i++) + + if (h[i].size() != L[i].size()) + + ok = 0; + + + + if (ok == 0) + + { + + fout << "-1\n"; + + exit(0); + + } + + + + for (i=n; i>=1; i--) + + { j = a[i]-'a'; + + P[i] = h[j][h[j].size()-1]; + + h[j].pop_back(); + + } + +} + + + +void Interclasare(int st, int m, int dr) + +{ + + int Q[50009], i, j, k; + + i = st; + + j = m+1; + + k = 0; + + while (i <= m && j <= dr) + + { + + if (P[i] <= P[j]) + + Q[++k] = P[i++]; + + else + + { + + Q[++k] = P[j++]; + + cnt += (m - i + 1); + + } + + } + + while (i <= m) + + Q[++k] = P[i++]; + + while (j <= dr) + + Q[++k] = P[j++]; + + + + j = st; + + for (i = 1; i <= k; ++i) + + P[j++] = Q[i]; + +} + + + +void Interschimba(int i, int j) + +{ + + if (P[i] > P[j]) + + { + + cnt++; + + swap(P[i], P[j]); + + } + +} + + + +void MergeSort(int st, int dr) + +{ + + int m, i; + + if (dr - st <= 1) Interschimba(st, dr); + + else + + { + + m = (st + dr) / 2; + + MergeSort(st, m); + + MergeSort(m + 1, dr); + + Interclasare(st, m, dr); + + } + +} + + + +int main () + +{ + + Citire(); + + MergeSort(1,n); + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/merge/12821c48-8dee-41a5-a38e-5a8aca960b4e.cpp b/swap/merge/12821c48-8dee-41a5-a38e-5a8aca960b4e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..acb24f4ba6c551f815c0b9c9e5f0662c109608c5 --- /dev/null +++ b/swap/merge/12821c48-8dee-41a5-a38e-5a8aca960b4e.cpp @@ -0,0 +1,85 @@ +#include #include #include using namespace std; + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50005; + +int n,nr,i,j,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); + + sort(m+1,dr); + + merge(st,dr,m); + + } + +} + +int main() + +{ f>>a+1>>b+1; + + n=strlen(a+1); + + for(i=1;i<=n;++i) + + { ++x[a[i]-'a']; ++y[b[i]-'a']; + + L[b[i]-'a'].push_back(i); + + } + + int e=0; + + for(i=0;i<26;++i) + + { if(x[i]!=y[i]) e=1; + + x[i]=0; + + } + + if(e) g<<"-1\n"; else + + { + + for(i=1;i<=n;++i) + + { + + j=x[a[i]-'a']++; + + p[i]=L[a[i]-'a'][j]; + + } + + sort(1,n); + + g< +#include + + +using namespace std; + + + +int N,v[50005],b[50005],c[50005],sol,L[30][50000]; + +char s1[50005],s2[50005]; + + + +inline void Read() + +{ + + int i,n; + + ifstream fin("swap.in"); + + fin.getline(s1,50005);fin.getline(s2,50005); + + n=strlen(s2); + + for(i=n-1;i>=0;i--) + + L[s2[i]-'a'][++L[s2[i]-'a'][0]]=i; + + + + n=strlen(s1); + + for(i=0;i=0) + + MergeSort(v,1,N); + + Write(); + + return 0; + +} diff --git a/swap/merge/13fc34b6-0b67-11ec-94bc-2cf05d84bfc2.cpp b/swap/merge/13fc34b6-0b67-11ec-94bc-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3bd13450f873014840b5bf9cad4224cd9160c5 --- /dev/null +++ b/swap/merge/13fc34b6-0b67-11ec-94bc-2cf05d84bfc2.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50001; + +int n,nr,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); sort(m+1,dr); merge(st,dr,m); + + } + +} + +int main() + +{ f>>a>>b; n=strlen(a); + + int i,e=0; + + for(i=0;i +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/14a4a455-0b67-11ec-bfef-2cf05d84bfc2.cpp b/swap/merge/14a4a455-0b67-11ec-bfef-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04177cb97cb34ade0f74448185950f06862d4b61 --- /dev/null +++ b/swap/merge/14a4a455-0b67-11ec-bfef-2cf05d84bfc2.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include + + +using namespace std; + +int lit[50001], poz[30][50001],l,nr2[30],nr1[30],v[50001],x,u,ok; + +char s1[50001],s2[50001]; + +long long s; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +void citire() + +{ + + fin>>s1>>s2; + +} + + + +void permutare() + +{ + + l=strlen(s2); + + for(int i=0;i +#include + + +using namespace std; + + + +int N, v[50005]; + +string A, B; + + + +void Citire () { + + ifstream fin ("swap.in"); + + fin >> A >> B; + + fin.close (); + + N = A.size (); + +} + + + +int alfa[30], beta[30]; + + + +int Good () { + + for (int i = 0; i < N; i++) + + { + + alfa[A[i] - 'a']++; + + beta[B[i] - 'a']++; + + } + + for (int i = 0; i <= 30; i++) + + if (!(alfa[i] == beta[i])) return 0; + + return 1; + +} + + + +void Initializare () { + + int k; + + for (char i = 'a'; i <= 'z'; i++) + + { + + k = -1; + + for (int j = 0; j < N; j++) + + { + + if (A[j] == i) + + { + + for (++k; k < N; k++) + + { + + if (B[k] == i) + + { + + v[k] = j; + + break; + + } + + } + + } + + } + + } + +} + + + +int aux[50005]; + + + +int Business (int L, int R) { + + int cnt = 0, mij = (L + R) >> 1, k = L - 1, i, j; + + if (L >= R) return 0; + + cnt += Business (L, mij); + + cnt += Business (mij + 1, R); + + for (i = L, j = mij + 1; i <= mij && j <= R;) + + { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else + + { + + aux[++k] = v[j++]; + + cnt += mij - i + 1; + + } + + } + + while (i <= mij) + + { + + aux[++k] = v[i++]; + + } + + while (j <= R) + + { + + aux[++k] = v[j++]; + + } + + for (int i = L; i <= R; i++) + + v[i] = aux[i]; + + return cnt; + +} + + + +int main () { + + Citire (); + + ofstream fout ("swap.out"); + + if (!Good ()) + + { + + fout << "-1"; + + fout.close (); + + return 0; + + } + + Initializare (); + + fout << Business (0, N - 1); + + fout.close (); + + return 0; + +} diff --git a/swap/merge/150c82a3-0b67-11ec-bc32-2cf05d84bfc2.cpp b/swap/merge/150c82a3-0b67-11ec-bc32-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e9a05aa28c4a48148f0ad05ced519cb79fcfa4d2 --- /dev/null +++ b/swap/merge/150c82a3-0b67-11ec-bc32-2cf05d84bfc2.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +using namespace std; + +const int MAX_N = 50002; +const int SIGMA = 27; + +int N; +int A[5*MAX_N], cnt_s1[SIGMA], cnt_s2[SIGMA]; +queue < int > pos[SIGMA]; +char s1[MAX_N], s2[MAX_N]; + +void update(int node, int left, int right, int x, int val) { + if(left == right) + A[node] += val; + else { + int ls = 2*node, rs = 2*node + 1, m = (left + right)/2; + + if(x <= m) + update(ls, left, m, x, val); + else + update(rs, m + 1, right, x, val); + A[node] = A[ls] + A[rs]; + } +} + +int query(int node, int left, int right, int x, int y) { + if(x <= left && right <= y) + return A[node]; + else { + int ls = 2*node, rs = 2*node + 1, m = (left + right)/2; + + int ans = 0; + if(x <= m) + ans += query(ls, left, m, x, y); + if(y > m) + ans += query(rs, m + 1, right, x, y); + + return ans; + } +} + +int main() { + ifstream f("swap.in"); + ofstream g("swap.out"); + + f >> s1; + f >> s2; + + int len = strlen(s1); + for(int i = 0; i < len; ++i) + ++cnt_s1[s1[i] - 'a']; + for(int i = 0; i < len; ++i) + ++cnt_s2[s2[i] - 'a']; + + bool ok = 1; + for(int i = 0; i < SIGMA; ++i) + if(cnt_s1[i] != cnt_s2[i]) + ok = 0; + int ans = 0; + if(ok) { + for(int i = len; i >= 1; --i) + s1[i] = s1[i-1]; + for(int i = len; i >= 1; --i) + s2[i] = s2[i-1]; + for(int i = 1; i <= len; ++i) + pos[s1[i] - 'a'].push(i); + for(int i = 1; i <= len; ++i) { + int p = pos[s2[i] - 'a'].front(); + pos[s2[i] - 'a'].pop(); + int temp; + if(p < len) + temp = query(1, 1, len, p + 1, len); + else temp = 0; + ans += p + temp - i; + update(1, 1, len, p, 1); + } + } + else ans = -1; + + g << ans << "\n"; + + f.close(); + g.close(); + + return 0; +} diff --git a/swap/merge/152f303a-69dd-47bf-8105-7fa509c019af.cpp b/swap/merge/152f303a-69dd-47bf-8105-7fa509c019af.cpp new file mode 100644 index 0000000000000000000000000000000000000000..050bff30275a305e99e532027f7890a516a31cb5 --- /dev/null +++ b/swap/merge/152f303a-69dd-47bf-8105-7fa509c019af.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; + +vector v[30]; + +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + + j = mid + 1; + + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/156c42cd-0b67-11ec-a6e6-2cf05d84bfc2.cpp b/swap/merge/156c42cd-0b67-11ec-a6e6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2f4ac639305cc0103407201cfac6d32a3a5ac9c7 --- /dev/null +++ b/swap/merge/156c42cd-0b67-11ec-a6e6-2cf05d84bfc2.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +using namespace std; +char s[50001], c[50001]; + +deque list[256]; + + +int perm[50001], w[50001]; +int n, m, nr; + +void merge(int v[], int l, int r){ + int m = (l + r) / 2, k = l - 1, i = 0, j = 0; + for(i = l, j = m + 1; i <= m && j <= r;){ + if(v[i] <= v[j]) + w[++k] = v[i++]; + else{ + w[++k] = v[j++]; + nr += m - i + 1; + } + } + for(; i <= m;) + w[++k] = v[i++]; + for(; j <= r;) + w[++k] = v[j++]; + for(i = l; i <= r; ++i) + v[i] = w[i]; +} +void sortare(int v[], int l, int r){ + if(l>=r) + return; + int m = (l + r) / 2; + sortare(v, l, m); + sortare(v, m + 1, r); + merge(v, l, r); +} +void read(){ + freopen("swap.in", "r", stdin); + scanf("%s %s", &s, &c); + n = strlen(s); + m = strlen(c); +} + +int cnt[256]; + +void solve(){ + int i, j; + for (i = 0; i < m ; ++i) + cnt[ int(c[i]) ]++; + + for (i = 0; i < n; ++i) + list[ int(s[i]) ].push_back(i + 1); + + for (i = 0; i < 256; ++i) + if (cnt[i] != list[i].size()) { + printf ("-1\n"); + return ; + } + +int nperm; + + + for (i = 0; i < m; ++i) { + perm[i + 1] = list[ int(c[i]) ][0]; + list[ int(c[i]) ].pop_front(); + } + + sortare(perm, 1, m); + printf("%d\n",nr); +} +int main(){ + freopen("swap.out", "w", stdout); + read(); + solve(); +} diff --git a/swap/merge/15eb2d89-0b67-11ec-8041-2cf05d84bfc2.cpp b/swap/merge/15eb2d89-0b67-11ec-8041-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce36f5e3dc56a2f21d90c0f2a99e4547dc825986 --- /dev/null +++ b/swap/merge/15eb2d89-0b67-11ec-8041-2cf05d84bfc2.cpp @@ -0,0 +1,85 @@ +#include +#include + + + + + +using namespace std ; +int n,sol; +int aint[4*50004]; +inline void Update(const int node,const int Left,const int Right,const int pos) +{ + if(Left==Right) + { + aint[node] = 1; + return ; + } + int Middle = ( Left + Right ) >>1; + if(pos<=Middle) + Update(2*node,Left,Middle,pos); + else + Update(2*node+1,Middle+1,Right,pos); + aint[node] = aint[2*node] + aint[2*node+1]; +} +inline void Query(const int node,const int Left,const int Right,const int A,const int B) +{ + if(A <= Left && Right <= B) + { + sol += aint[node]; + return; + } + int Middle = ( Left + Right ) >>1; + if(A<=Middle) + Query(2*node,Left,Middle,A,B); + if(Middle < B) + Query(2*node+1,Middle+1,Right,A,B); +} +int fr0[300],fr1[300], ind[300] , a[50004]; +vector < int > pos[300]; +char s0[50004],s1[50004]; +inline void Read() +{ + ifstream f("swap.in"); + f>> (s0+1) >> (s1+1); + f.close(); +} +inline void Solve() +{ + int i; + for(i = 1;s0[i] ;++i) + ++fr0[s0[i]]; + for(i = 1;s1[i] ;++i) + ++fr1[s1[i]], + pos[s1[i]].push_back(i); + n = i-1; + for(i = 'a';i <= 'z'; ++i) + if(fr0[i]!=fr1[i]) + { + sol = -1; + return ; + } + for(i = 1;i <= n; ++i) + a[i] = pos[s0[i]][ind[s0[i]]++]; + for(i = 1;i <= n; ++i) + { + if(a[i]!=n) + Query(1,1,n,a[i]+1,n); + Update(1,1,n,a[i]); + } +} + +inline void Write() +{ + ofstream g("swap.out"); + g< +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/16348384-0b67-11ec-9515-2cf05d84bfc2.cpp b/swap/merge/16348384-0b67-11ec-9515-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad691a5c4991cb1f390336b5ae546164c43141fa --- /dev/null +++ b/swap/merge/16348384-0b67-11ec-9515-2cf05d84bfc2.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i #include +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/16bfee8b-0b67-11ec-b7db-2cf05d84bfc2.cpp b/swap/merge/16bfee8b-0b67-11ec-b7db-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87331dc8ccc6e2ccf8f5f2e9f13080dd44cde29c --- /dev/null +++ b/swap/merge/16bfee8b-0b67-11ec-b7db-2cf05d84bfc2.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/16f5b8e5-9919-42a1-92ac-4d9d387b18fa.cpp b/swap/merge/16f5b8e5-9919-42a1-92ac-4d9d387b18fa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31b8ecd0a945bc196f509a4b830bb4362c700dfb --- /dev/null +++ b/swap/merge/16f5b8e5-9919-42a1-92ac-4d9d387b18fa.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/1701e815-0b67-11ec-afc3-2cf05d84bfc2.cpp b/swap/merge/1701e815-0b67-11ec-afc3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c9cc08649d1169f7e8f1dad0a3ac4846773bf49 --- /dev/null +++ b/swap/merge/1701e815-0b67-11ec-afc3-2cf05d84bfc2.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +using namespace std; + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + +ifstream fin(iname); + +ofstream fout(oname); + +deque Q[300]; + +int i, lg, lg2, n, ANS, sol; + +int j_s1[300], j_s2[300], P[50004]; + +int H[50004 * 3]; + +char s1[50004], s2[50004]; + +inline void update (int n, int l, int r, int p, int v){ + + if (l == r){H[n] = v; return;} + + int m = (l + r) / 2, L = n << 1, R = L | 1; + + if (p <= m) update (L, l, m, p, v); + + else + + update (R, m + 1, r, p, v); + + H[n] = H[L] + H[R]; + +} + +inline int query (int n, int l, int r, int i, int j){ + + if (i <= l && r <= j) return H[n]; + + int m = (l + r) / 2; int sol = 0; + + if (i <= m) sol += query(2 * n, l, m, i, j); + + if (j > m) sol += query(2 * n + 1, m + 1, r, i, j); + + return sol; + +} + +int main() + +{ + + fin >> s1; lg = strlen(s1); + + for (i = 0; i < lg; ++i) + + ++j_s1[(int)s1[i]], + + Q[(int)s1[i]].push_back(i + 1); + + fin >> s2; lg2 = strlen(s2); + + if (lg != lg2){fout << -1 << '\n'; return 0;} + + for (i = 0; i < lg; ++i) + + { + + ++j_s2[(int)s2[i]]; + + P[++n] = Q[s2[i]][0]; + + Q[s2[i]].pop_front(); + + } + + for (i = 0; i < lg; ++i) + + { + + if (j_s1[(int)s1[i]] != j_s2[(int)s1[i]]){ + + fout << -1 << '\n'; return 0; + + } + + } + + update (1, 1, n, P[1], 1); + + for (i = 2; i <= n; ++i) + + { + + update (1, 1, n, P[i], 1); + + ANS += query (1, 1, n, P[i] + 1, n); + + } + + fout << ANS << '\n'; + + return 0; + +} diff --git a/swap/merge/1752a7c4-81ba-441c-a803-0a8eab5fb0c2.cpp b/swap/merge/1752a7c4-81ba-441c-a803-0a8eab5fb0c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec4b1e6d498fe7ec83c8a619c8429332d614f791 --- /dev/null +++ b/swap/merge/1752a7c4-81ba-441c-a803-0a8eab5fb0c2.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + + + +int f[1001], INV1 = 0, INV2 = 0, pos[201][50001], d[50001], LE[50001], C[50001]; + +char a[50002], b[50002]; + +int c[50002]; + +inline void Merge(int st, int mij, int dr){ + + int i = st, j = mij + 1, NR = 0; + + while(i <= mij && j <= dr){ + + if(d[i] > d[j]){ + + c[++NR] = d[j++]; + + INV1 = INV1 + mij - i + 1; + + } + + else + + c[++NR] = d[i++]; + + } + + while(j <= dr) + + c[++NR] = d[j++]; + + while(i <= mij) + + c[++NR] = d[i++]; + + for(i = st , j = 1; i <= dr ; ++i, ++j) + + d[i] = c[j]; + +} + +inline void MergeSort(int st, int dr){ + + if(st >= dr) return ; + + MergeSort(st, (st + dr) / 2); + + MergeSort((st + dr) / 2 + 1, dr); + + Merge(st, (st + dr) / 2, dr); + +} + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + scanf("%s", a); + + scanf("%s", b); + + int L = strlen(a); + + for(int i = 0 ; i < L ; ++i) + + ++f[a[i]]; + + for(int i = 0 ; i < L ; ++i){ + + if(f[b[i]] == 0) + + {printf("-1"); return 0;} + + --f[b[i]]; + + } + + for(int i = 0 ; i < L ; ++i) + + pos[a[i]][++LE[a[i]]] = i + 1; + + for(int i = 0 ; i < L ; ++i) + + d[i] = pos[b[i]][++C[b[i]]]; + + MergeSort(0, L - 1); + + printf("%d", INV1); + + return 0; + +} diff --git a/swap/merge/17d2de01-3fcd-406c-a839-1c388146cbb3.cpp b/swap/merge/17d2de01-3fcd-406c-a839-1c388146cbb3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d710932b647c4881758b8da44d90d23734a4ae6 --- /dev/null +++ b/swap/merge/17d2de01-3fcd-406c-a839-1c388146cbb3.cpp @@ -0,0 +1,137 @@ +#include +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] +#include +#include +#include +using namespace std; +const int MAX_N = 50100; +ifstream fin("swap.in"); +ofstream fout("swap.out"); +int alf[27][MAX_N]; +int alfIt[27]; +int v[MAX_N]; +int merge_sort(int, int); +int main() { + string a1, a2; + getline(fin, a1); + getline(fin, a2); + for (unsigned int i = 0; i < a2.size(); ++i) { + int ch = a2[i] - 'a'; + alf[ch][++alf[ch][0]] = i + 1; + } + for (unsigned int i = 0; i < a1.size(); ++i) { + int ch = a1[i] - 'a'; + ++alfIt[ch]; + if (alfIt[ch] > alf[ch][0]) { + fout << -1; + return 0; + } + v[i+1] = alf[ch][alfIt[ch]]; + } + int N = a1.size(); + fout << merge_sort(1, N); + return 0; +} + +int merge_sort(int lo, int hi) { + if (lo == hi) return 0; + int mid = lo + (hi - lo) / 2, numInv; + numInv = merge_sort(lo, mid) + merge_sort(mid + 1, hi); + int aux[MAX_N]; + for (int i = lo, j = mid + 1, k = lo; i <= mid || j <= hi; ++k) { + if ((i <= mid && v[i] < v[j]) || j > hi) { + aux[k] = v[i++]; + } else { + aux[k] = v[j++]; + numInv += (mid - i + 1); + } + } + for (int k = lo; k <= hi; ++k) { + v[k] = aux[k]; + } + return numInv; +} diff --git a/swap/merge/192bb16d-048f-4c0f-bdeb-a43decfc9f9a.cpp b/swap/merge/192bb16d-048f-4c0f-bdeb-a43decfc9f9a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e118cf82d09be8fa8f3df499568fdd011f6b29 --- /dev/null +++ b/swap/merge/192bb16d-048f-4c0f-bdeb-a43decfc9f9a.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include + + +using namespace std; + + + +int N, v[50005]; + +string A, B; + + + +void Citire () { + + ifstream fin ("swap.in"); + + fin >> A >> B; + + fin.close (); + + N = A.size (); + +} + + + +int alfa[30], beta[30]; + + + +int Good () { + + for (int i = 0; i < N; i++) + + { + + alfa[A[i] - 'a']++; + + beta[B[i] - 'a']++; + + } + + for (int i = 0; i <= 30; i++) + + if (!(alfa[i] == beta[i])) return 0; + + return 1; + +} + + + +void Initializare () { + + int k; + + for (char i = 'a'; i <= 'z'; i++) + + { + + k = -1; + + for (int j = 0; j < N; j++) + + { + + if (A[j] == i) + + { + + for (++k; k < N; k++) + + { + + if (B[k] == i) + + { + + v[k] = j; + + break; + + } + + } + + } + + } + + } + +} + + + +int aux[50005]; + + + +int Business (int L, int R) { + + int cnt = 0, mij = (L + R) >> 1, k = L - 1, i, j; + + if (L >= R) return 0; + + cnt += Business (L, mij); + + cnt += Business (mij + 1, R); + + for (i = L, j = mij + 1; i <= mij && j <= R;) + + { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else + + { + + aux[++k] = v[j++]; + + cnt += mij - i + 1; + + } + + } + + while (i <= mij) + + { + + aux[++k] = v[i++]; + + } + + while (j <= R) + + { + + aux[++k] = v[j++]; + + } + + for (int i = L; i <= R; i++) + + v[i] = aux[i]; + + return cnt; + +} + + + +int main () { + + Citire (); + + ofstream fout ("swap.out"); + + if (!Good ()) + + { + + fout << "-1"; + + fout.close (); + + return 0; + + } + + Initializare (); + + fout << Business (0, N - 1); + + fout.close (); + + return 0; + +} diff --git a/swap/merge/19818a83-0b67-11ec-b502-2cf05d84bfc2.cpp b/swap/merge/19818a83-0b67-11ec-b502-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365de5cf7c67d0626c61fb8054d15be54b13f83f --- /dev/null +++ b/swap/merge/19818a83-0b67-11ec-b502-2cf05d84bfc2.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +long nr[50100],NR[50100],i,n,num,P[50100],B[50100],a[50100][30],A[50100]; + +char s[50100],t[50100]; + +void merge (int st,int dr ) { + + + + if(st==dr) + + return ; + + int m=(st+dr)>>1; + + + + int i,j,p; + + merge(st,m); + + merge(m+1,dr); + + i=st;j=m+1;p=0; + + while(i<=m || j<=dr ){ + + if((P[j]m){ + + B[st+p++]=P[j++]; + + num+=m-i+1; + + } + + else + + B[st+p++]=P[i++]; + + } + + for(int u=st;u<=dr;++u){ + + P[u]=B[u]; + + B[u]=0; + + } + +} + +int main (){ + + f>>(s+1); + + f.get();f>>(t+1); + + f.get(); + + n=strlen(s+1); + + for(i=1;i<=n;++i){ + + a[++nr[s[i]-96]][s[i]-96]=i; + + + + } + + + + for(i=1;i<=n;i++){ + + + + P[a[++NR[t[i]-96]][t[i]-96]]=i; + + + + } + + for(i=0;i<=26;i++) + + if(NR[i]!=nr[i]){ + + g<<-1<<"\n"; + + + + return 0; + + } + + merge(1,n); + + g< +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/1a84b069-ff2e-4bb0-9d67-0a90243bf370.cpp b/swap/merge/1a84b069-ff2e-4bb0-9d67-0a90243bf370.cpp new file mode 100644 index 0000000000000000000000000000000000000000..99eea143b186f834e4c31faa9c3361a6ab549f08 --- /dev/null +++ b/swap/merge/1a84b069-ff2e-4bb0-9d67-0a90243bf370.cpp @@ -0,0 +1,163 @@ +#include +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +queuev2[27]; + +int n,i,ap[27],v[50001],rasp,v3[50001]; + +char s[50001],s2[50001]; + +void merge_sort(int in,int sf) + +{ + + if(in==sf) + + { + + return; + + } + + int mij=(in+sf)/2; + + merge_sort(in,mij); + + merge_sort(mij+1,sf); + + int i=in; + + int j=mij+1; + + int poz=in; + + while(i<=mij && j<=sf) + + { + + if(v[i]<=v[j]) + + { + + v3[poz]=v[i]; + + i++; + + } + + else + + { + + v3[poz]=v[j]; + + rasp+=j-poz; + + j++; + + } + + poz++; + + } + + while(i<=mij) + + { + + v3[poz]=v[i]; + + i++; + + poz++; + + } + + while(j<=sf) + + { + + v3[poz]=v[j]; + + rasp+=j-poz; + + j++; + + poz++; + + } + + for(i=in;i<=sf;i++) + + { + + v[i]=v3[i]; + + } + + return; + +} + +int main() + +{ + + f>>s; + + n=strlen(s); + + f>>s2; + + for(i=0;i +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/1b034571-0b67-11ec-965b-2cf05d84bfc2.cpp b/swap/merge/1b034571-0b67-11ec-965b-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81926a8a9ff03612cb5b2bb92246c601f56adbfd --- /dev/null +++ b/swap/merge/1b034571-0b67-11ec-965b-2cf05d84bfc2.cpp @@ -0,0 +1,86 @@ +#include #include +#include using namespace std; + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/1ba4fa34-0b67-11ec-9094-2cf05d84bfc2.cpp b/swap/merge/1ba4fa34-0b67-11ec-9094-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d769a5a6f93e6aa37dea4ad115183666d155ef24 --- /dev/null +++ b/swap/merge/1ba4fa34-0b67-11ec-9094-2cf05d84bfc2.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/1bad307d-f685-43dc-9e12-da114b9a7eb4.cpp b/swap/merge/1bad307d-f685-43dc-9e12-da114b9a7eb4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4ba596157bc3eba5af4ca2864e897707820540d --- /dev/null +++ b/swap/merge/1bad307d-f685-43dc-9e12-da114b9a7eb4.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char beg[50001]; + +int sir[50001],arb[3*50001],viz[50001],i,n,poz,pozst,pozdr,ch,nr; + +long long sum; + +vector G[50]; + +void upDate(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + arb[nod]=1; + + return; + + } + + int mijl=(st+dr)/2; + + if(poz<=mijl) + + upDate(2*nod,st,mijl); + + else + + upDate(2*nod+1,mijl+1,dr); + + arb[nod]=arb[2*nod]+arb[2*nod+1]; + +} + +void query(int nod, int st, int dr) + +{ + + if(st>=pozst&&dr<=pozdr) + + { + + sum+=arb[nod]; + + return; + + } + + int mijl=(st+dr)/2; + + if(pozst<=mijl) + + query(2*nod,st,mijl); + + if(pozdr>mijl) + + query(2*nod+1,mijl+1,dr); + +} + +int main() + +{ + + fin>>(beg+1); + + n=strlen(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + G[ch].push_back(i); + + } + + fin>>(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + if(viz[ch] +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/1be62e3b-3f81-4da2-beca-6aea05885c7d.cpp b/swap/merge/1be62e3b-3f81-4da2-beca-6aea05885c7d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87331dc8ccc6e2ccf8f5f2e9f13080dd44cde29c --- /dev/null +++ b/swap/merge/1be62e3b-3f81-4da2-beca-6aea05885c7d.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i + + + + + + +char a[50002],b[50002]; + +int lita[27][50002],litb[27][50002]; + +int P[50002],B[50002]; + +int N,nr,ok; + + + +void merge(int l,int r){ + + int m=l+(r-l)/2,aux; + + if( l==r ) return; + + if( l==r-1 ){ + + if( P[l]>P[r] ) ++nr, aux=P[l],P[l]=P[r],P[r]=aux; + + return; + + } + + + + merge(l,m); + + merge(m+1,r); + + + + int i,j; + + B[0]=0; + + for(i=l,j=m+1; i<=m && j<=r; ) + + if( P[i] <= P[j] ) B[++B[0]]=P[i++]; + + else B[++B[0]]=P[j++], nr+=m-i+1; + + for(; i<=m; ++i) B[++B[0]]=P[i]; + + for(; j<=r; ++j) B[++B[0]]=P[j]; + + + + for(i=l;i<=r;++i) P[i]=B[i-l+1]; + + + +} + + + +void precal_p(){ + + int i,j; + + for(i=0;i<27;++i) + + if( lita[i][0] != litb[i][0] ){ + + ok=0; return; + + } + + else + + for(j=1;j<=lita[i][0];++j) + + P[lita[i][j]]=litb[i][j]; + +} + + + +int main(){ + + int i; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s%s",a+1,b+1); + + for(i=1; a[i]!='\0' && a[i]!='\n'; ) ++i; + + N=i-1; + + + + for(i=1;i<=N;++i){ + + lita[a[i]-'a'][++lita[a[i]-'a'][0]]=i; + + litb[b[i]-'a'][++litb[b[i]-'a'][0]]=i; + + } + + + + ok=1; + + precal_p(); + + if( ok ) merge(1,N); + + else nr=-1; + + + + printf("%d\n",nr); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/swap/merge/1c1e2329-e666-4fd7-9c05-740b72a5fa38.cpp b/swap/merge/1c1e2329-e666-4fd7-9c05-740b72a5fa38.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c4f046d36d25fc8e593d379ea11d05548a330b9d --- /dev/null +++ b/swap/merge/1c1e2329-e666-4fd7-9c05-740b72a5fa38.cpp @@ -0,0 +1,106 @@ +#include + + + + +std::ifstream fin ("swap.in"); + +std::ofstream fout ("swap.out"); + + + +int moves=0; + + + +void merge (std::vector &arr, int left, int right){ + + if (left == right) + + return; + + int mid = (left + right) / 2; + + merge (arr, left, mid); + + merge (arr, mid+1, right); + + + + std::vector aux; + + int i=left, j=mid+1; + + while (i <= mid or j <=right){ + + if (i > mid) + + aux.push_back (arr[j++]), moves = moves + (mid - i + 1); + + else if (j > right) + + aux.push_back (arr[i++]); + + else if (arr[i] <= arr[j]) + + aux.push_back (arr[i++]); + + else + + aux.push_back (arr[j++]), moves = moves + (mid - i + 1); + + } + + for (i=left; i<=right; i++) + + arr[i] = aux[i-left]; + +} + + + +int main() + +{ + + std::string s1, s2; + + + + fin >> s1 >> s2; + + std::vector v(s1.size()); + + std::deque pos[26]; + + for (int i=0; i +#include +#include + + + + + + +using namespace std; + + + + + + + +char S[2][50666]; + +vector v[30]; + +int cnt[2][30], P[50666], Paux[50666]; + +int i, j, k, N, ninv; + + + +void compute_ninv(int li, int ls) + +{ + + int m; + + + + if (li >= ls) + + return; + + + + m = (li + ls) >> 1; + + + + compute_ninv(li, m); + + compute_ninv(m + 1, ls); + + + + i = li, j = m + 1, k = li - 1; + + + + while (i <= m && j <= ls) + + { + + if (P[i] < P[j]) + + Paux[++k] = P[i++]; + + else + + { + + ninv += (m - i + 1); + + Paux[++k] = P[j++]; + + } + + } + + + + if (i <= m) + + { + + for (j = i; j <= m; j++) + + Paux[++k] = P[j]; + + } + + else + + { + + for (i = j; i <= ls; i++) + + Paux[++k] = P[i]; + + } + + + + for (i = li; i <= ls; i++) + + P[i] = Paux[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S[0] + 1, 50666, stdin); + + fgets(S[1] + 1, 50666, stdin); + + + + N = strlen(S[0] + 1) - 1; + + + + memset(cnt, 0, sizeof(cnt)); + + for (i = 1; i <= N; i++) + + { + + cnt[1][S[1][i] - 'a']++; + + v[S[1][i] - 'a'].push_back(i); + + } + + + + for (i = 1; i <= N; i++) + + { + + cnt[0][S[0][i] - 'a']++; + + + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + + } + + + + ninv = 0; + + compute_ninv(1, N); + + + + printf("%d\n", ninv); + + + + return 0; + +} diff --git a/swap/merge/1d7d7453-0b67-11ec-8ac7-2cf05d84bfc2.cpp b/swap/merge/1d7d7453-0b67-11ec-8ac7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c110dfc220c299529601bcf56bb693e48fc30a --- /dev/null +++ b/swap/merge/1d7d7453-0b67-11ec-8ac7-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + for(i=0;i<26;i++) + + ind[i]=0; + + + + for(pos=0;posv[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/1da3f345-4284-4e3e-89fc-53a3a1991222.cpp b/swap/merge/1da3f345-4284-4e3e-89fc-53a3a1991222.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365de5cf7c67d0626c61fb8054d15be54b13f83f --- /dev/null +++ b/swap/merge/1da3f345-4284-4e3e-89fc-53a3a1991222.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +long nr[50100],NR[50100],i,n,num,P[50100],B[50100],a[50100][30],A[50100]; + +char s[50100],t[50100]; + +void merge (int st,int dr ) { + + + + if(st==dr) + + return ; + + int m=(st+dr)>>1; + + + + int i,j,p; + + merge(st,m); + + merge(m+1,dr); + + i=st;j=m+1;p=0; + + while(i<=m || j<=dr ){ + + if((P[j]m){ + + B[st+p++]=P[j++]; + + num+=m-i+1; + + } + + else + + B[st+p++]=P[i++]; + + } + + for(int u=st;u<=dr;++u){ + + P[u]=B[u]; + + B[u]=0; + + } + +} + +int main (){ + + f>>(s+1); + + f.get();f>>(t+1); + + f.get(); + + n=strlen(s+1); + + for(i=1;i<=n;++i){ + + a[++nr[s[i]-96]][s[i]-96]=i; + + + + } + + + + for(i=1;i<=n;i++){ + + + + P[a[++NR[t[i]-96]][t[i]-96]]=i; + + + + } + + for(i=0;i<=26;i++) + + if(NR[i]!=nr[i]){ + + g<<-1<<"\n"; + + + + return 0; + + } + + merge(1,n); + + g< #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A, 50001, stdin); + + fgets( B, 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/1e1a37a5-0b67-11ec-b011-2cf05d84bfc2.cpp b/swap/merge/1e1a37a5-0b67-11ec-b011-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/1e1a37a5-0b67-11ec-b011-2cf05d84bfc2.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/1e392142-0b67-11ec-9532-2cf05d84bfc2.cpp b/swap/merge/1e392142-0b67-11ec-9532-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3145e2bba904877ed99571657e3ddc4a79c8429 --- /dev/null +++ b/swap/merge/1e392142-0b67-11ec-9532-2cf05d84bfc2.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/1e5963fb-0b67-11ec-8e20-2cf05d84bfc2.cpp b/swap/merge/1e5963fb-0b67-11ec-8e20-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27fa7cfd0360f54c2fd1e145b8912faa5ca01c2d --- /dev/null +++ b/swap/merge/1e5963fb-0b67-11ec-8e20-2cf05d84bfc2.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/1ed11fb9-7282-45ad-b92f-61b8622fe849.cpp b/swap/merge/1ed11fb9-7282-45ad-b92f-61b8622fe849.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4ba596157bc3eba5af4ca2864e897707820540d --- /dev/null +++ b/swap/merge/1ed11fb9-7282-45ad-b92f-61b8622fe849.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char beg[50001]; + +int sir[50001],arb[3*50001],viz[50001],i,n,poz,pozst,pozdr,ch,nr; + +long long sum; + +vector G[50]; + +void upDate(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + arb[nod]=1; + + return; + + } + + int mijl=(st+dr)/2; + + if(poz<=mijl) + + upDate(2*nod,st,mijl); + + else + + upDate(2*nod+1,mijl+1,dr); + + arb[nod]=arb[2*nod]+arb[2*nod+1]; + +} + +void query(int nod, int st, int dr) + +{ + + if(st>=pozst&&dr<=pozdr) + + { + + sum+=arb[nod]; + + return; + + } + + int mijl=(st+dr)/2; + + if(pozst<=mijl) + + query(2*nod,st,mijl); + + if(pozdr>mijl) + + query(2*nod+1,mijl+1,dr); + +} + +int main() + +{ + + fin>>(beg+1); + + n=strlen(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + G[ch].push_back(i); + + } + + fin>>(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + if(viz[ch] +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/1efbcb7c-0b67-11ec-a1d7-2cf05d84bfc2.cpp b/swap/merge/1efbcb7c-0b67-11ec-a1d7-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f5d9ae25cda0936322d2f1155ad2d13ad115d34 --- /dev/null +++ b/swap/merge/1efbcb7c-0b67-11ec-a1d7-2cf05d84bfc2.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/1f3e6613-0b67-11ec-91b4-2cf05d84bfc2.cpp b/swap/merge/1f3e6613-0b67-11ec-91b4-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d710932b647c4881758b8da44d90d23734a4ae6 --- /dev/null +++ b/swap/merge/1f3e6613-0b67-11ec-91b4-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/20360d7d-0b67-11ec-ac19-2cf05d84bfc2.cpp b/swap/merge/20360d7d-0b67-11ec-ac19-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d67da457069e37ba2137a23a4d659274bcbb2dc --- /dev/null +++ b/swap/merge/20360d7d-0b67-11ec-ac19-2cf05d84bfc2.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + +const int MAX_N = 50010; + +const int MAX_L = 28; + + + +int f[2][MAX_L], d[MAX_N], c[MAX_N], sol; + +char a[MAX_N], b[MAX_N]; + +vector v[MAX_L]; + + + +void merge(int st, int dr) + +{ + + int i, j, k; + + int mid = st + (dr - st) / 2; + + if (st >= dr) return; + + + + merge(st, mid); + + merge(mid + 1, dr); + + + + i = st; + + j = mid + 1; + + k = st - 1; + + + + while (i <= mid && j <= dr) + + { + + if (d[i] < d[j]) c[++k] = d[i++]; + + else + + { + + sol += (mid - i + 1); + + c[++k] = d[j++]; + + } + + } + + + + if (i <= mid) + + for (; i <= mid; ++i) c[++k] = d[i]; + + else for (i = j; i <= dr; ++i) c[++k] = d[i]; + + + + for (i = st; i <= dr; ++i) d[i] = c[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s\n", a + 1); + + scanf("%s\n", b + 1); + + int l = strlen(a + 1); + + + + for (int i = 1; i <= l; ++i) + + { + + ++f[1][b[i] - 'a']; + + v[b[i] - 'a'].push_back(i); + + } + + + + for (int i = 1; i <= l; i++) + + { + + ++f[0][a[i] - 'a']; + + if (f[0][a[i] - 'a'] > f[1][a[i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + d[i] = v[a[i] - 'a'][f[0][a[i] - 'a'] - 1]; + + } + + + + merge(1, l); + + + + printf("%d\n", sol); + +} diff --git a/swap/merge/20756b81-0b67-11ec-a580-2cf05d84bfc2.cpp b/swap/merge/20756b81-0b67-11ec-a580-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6474d92f61de8e377f818314ae28fe87b3bf9c2 --- /dev/null +++ b/swap/merge/20756b81-0b67-11ec-a580-2cf05d84bfc2.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; +vector v[30]; +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + j = mid + 1; + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/20c15e64-0b67-11ec-9ae1-2cf05d84bfc2.cpp b/swap/merge/20c15e64-0b67-11ec-9ae1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..050bff30275a305e99e532027f7890a516a31cb5 --- /dev/null +++ b/swap/merge/20c15e64-0b67-11ec-9ae1-2cf05d84bfc2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; + +vector v[30]; + +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + + j = mid + 1; + + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/20c1b2af-fe62-4878-9ce1-1cc26eaa61a9.cpp b/swap/merge/20c1b2af-fe62-4878-9ce1-1cc26eaa61a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5aa11a6ca467c890d9fcc4ab6bc042f419ef91a --- /dev/null +++ b/swap/merge/20c1b2af-fe62-4878-9ce1-1cc26eaa61a9.cpp @@ -0,0 +1,191 @@ +#include +#include + + +using namespace std; + +int lit[50001], poz[30][50001],l,nr2[30],nr1[30],v[50001],x,u,ok; + +char s1[50001],s2[50001]; + +long long s; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +void citire() + +{ + + fin>>s1>>s2; + +} + + + +void permutare() + +{ + + l=strlen(s2); + + for(int i=0;i +#include + + +using namespace std; + + + +int n, i, j, poz, sol, q; + +vector t[30]; + +int ind[30]; + +char s[50100]; + +int v[50100], x[50100], st[130100]; + +int t1[30], t2[30]; + + + +void st_update(int nod, int l, int r, int a, int b, int u) { + + int m = (l + r) / 2; + + if (a <= l && r <= b) { + + st[nod] += u; + + return; + + } + + if (a <= m) + + st_update(2 * nod, l, m, a, b, u); + + if (b > m) + + st_update(2 * nod + 1, m + 1, r, a, b, u); + +} + + + +void st_query(int nod, int l, int r, int poz) { + + int m = (l + r) / 2; + + q += st[nod]; + + if (m == poz && l == r) + + return; + + if (poz <= m) + + st_query(2 * nod, l, m, poz); + + else + + st_query(2 * nod + 1, m + 1, r, poz); + + + +} + + + +int main() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50100, stdin); + + for (i = 0; s[i] != 0; i++) { + + n++; + + v[n] = s[i] - 'a' + 1; + + t1[v[n]]++; + + t[v[n]].push_back(n); + + } + + + + fgets(s, 50100, stdin); + + for (i = 0; s[i] != 0; i++) { + + x[i + 1] = s[i] - 'a' + 1; + + t1[x[i + 1]]--; + + } + + + + for (i = 1; i <= 30; i++) + + if (t1[i] != 0) { + + printf("-1\n"); + + return 0; + + } + + + + + + for (i = 1; i < n; i++) { + + + + q = 0; + + st_query(1, 1, n, t[x[i]][ind[x[i]]]); + + poz = t[x[i]][ind[x[i]]] + q; + + sol += (poz - i); + + st_update(1, 1, n, 1, poz - q, 1); + + ind[x[i]]++; + + if (ind[x[i]] > t[x[i]].size()) { + + printf("-1\n"); + + return 0; + + } + + } + + + + printf("%d\n", sol); + + + + return 0; + +} diff --git a/swap/merge/211f4708-0b67-11ec-8b35-2cf05d84bfc2.cpp b/swap/merge/211f4708-0b67-11ec-8b35-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0928a89f2b945c3c77724c638aac15e14a1ba641 --- /dev/null +++ b/swap/merge/211f4708-0b67-11ec-8b35-2cf05d84bfc2.cpp @@ -0,0 +1,244 @@ +#include +#include +#include +using namespace std; + + + +char a[50111],b[50111]; + +int q,nr[111],nr2[111],*l[111],S,sol,v[50111],A[50111 * 4],i,n,poz[50111]; + + + +void query(int nod,int p,int u,int a,int b){ + + int mij=(p+u)>>1; + + + + if(a<=p && u<=b){ + + S+=A[nod]; + + return; + + } + + + + if(a<= mij) + + query(nod<<1,p,mij,a,b); + + if(b > mij) + + query((nod<<1)+1,mij+1,u,a,b); + + + +} + + + + + +void update(int nod,int p,int u,int poz,int val){ + + int mij=(p+u)>>1; + + + + if(p == u){ + + A[nod] = val; + + return ; + + } + + + + if(poz<=mij) + + update(nod<<1,p,mij,poz,val); + + else + + update((nod<<1)+1,mij+1,u,poz,val); + + + + + + A[nod] = A[nod<<1]+A[(nod<<1)+1]; + +} + + + + + +void afis(){ + + + + FILE *g=fopen("swap.out","w"); + + fprintf(g,"%d",sol); + + fclose(g); + + + +} + + + +int main(){ + + + + FILE *f=fopen("swap.in","r"); + + a[0]=b[0]=' '; + + fscanf(f,"%s",a+1); + + fscanf(f,"%s",b+1); + + + + n=strlen(a); + + + + if(strlen(a) != strlen(b)){ + + sol=-1; + + afis(); + + return 0; + + } + + + + n--; + + + + for(i=1;i<=n;i++) + + nr[a[i]-96]++; + + + + for(i=1;i<=n;i++) + + nr2[b[i]-96]++; + + + + + + for(i=0;i<=28;i++) + + if(nr[i] != nr2[i]){ + + sol=-1; + + afis(); + + return 0; + + } + + + + + + for(i=0;i<=28;i++){ + + l[i]=new int [nr[i]+5]; + + l[i][0]=0; + + nr[i]=0; + + } + + + + for(i=1;i<=n;i++){ + + q=a[i]-96; + + l[q][++l[q][0]] = i; + + } + + + + for(i=1;i<=n;i++){ + + q=b[i] - 96; + + v[i] = l[q][++nr[q]]; + + } + + + +fclose(f); + + + + for(i=1;i<=n;i++) + + poz[v[i]] = i; + + + + for(i=1;i<=n;i++) + + if(v[i] == 1) + + update(1,1,n,i,0); + + else + + update(1,1,n,i,1); + + + + query(1,1,n,1,poz[1]); + + sol=S; + + + + for(i=2;i<=n;i++){ + + update(1,1,n,poz[i-1],0); + + update(1,1,n,poz[i],0); + + S=0; + + query(1,1,n,1,poz[i]); + + sol+=S; + + } + + + +afis(); + + + +return 0; + +} diff --git a/swap/merge/217c67a4-0b67-11ec-9767-2cf05d84bfc2.cpp b/swap/merge/217c67a4-0b67-11ec-9767-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..050bff30275a305e99e532027f7890a516a31cb5 --- /dev/null +++ b/swap/merge/217c67a4-0b67-11ec-9767-2cf05d84bfc2.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; + +vector v[30]; + +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + + j = mid + 1; + + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/219e4c77-0b67-11ec-8218-2cf05d84bfc2.cpp b/swap/merge/219e4c77-0b67-11ec-8218-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0480c8c88a397ed07d1731f22be4c58ada83d358 --- /dev/null +++ b/swap/merge/219e4c77-0b67-11ec-8218-2cf05d84bfc2.cpp @@ -0,0 +1,177 @@ +#include #include +#include + + +using namespace std; + + + + + + + +int n, m, i, j, k, sol, p2; + +int cor[50010]; + +int arb[3 * 50010]; + + + +char a[50010], b[50010]; + +vector s1[30], s2[30]; + + + +void cit() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s", a); + + scanf("%s", b); + +} + + + +inline int query(int nod, int st, int dr, int ask) { + + if (st == dr) return arb[nod]; + + + + int r = (st + dr) / 2; + + + + if (ask <= r) return arb[nod] + query(nod * 2, st, r, ask); + + else return arb[nod] + query(nod * 2 + 1, r + 1, dr, ask); + +} + + + +void update(int nod, int st, int dr, int left, int right) { + + if (left > dr || right < st) return; + + + + if (left <= st && dr <= right) { + + arb[nod]++; + + return; + + } + + + + int r = (st + dr) / 2; + + update(nod * 2, st, r, left, right); + + update(nod * 2 + 1, r + 1, dr, left, right); + +} + + + +int cost(int poz) { + + int pozitie = cor[poz] + query(1, 1, p2, cor[poz]); + + + + update(1, 1, p2, 1, cor[poz] - 1); + + + + return pozitie - i; + +} + + + +void solve() { + + + + n = strlen(a); + + m = strlen(b); + + for (i = 0; i < n; i++) + + s1[a[i] - 'a'].push_back(i); + + for (j = 0; j < m; j++) + + s2[b[j] - 'a'].push_back(j); + + + + + + for (i = 0; i < 30; i++) + + if (s1[i].size() != s2[i].size()) { + + printf("-1\n"); + + return; + + } + + + + + + for (i = 0; i < 30; i++) { + + k = s2[i].size(); + + for (j = 0; j < k; j++) + + cor[s2[i][j]] = s1[i][j]; + + } + + + + + + p2 = 1; + + while (p2 < n) p2 *= 2; + + for (i = 0; i < n; i++) + + sol += cost(i); + + printf("%d\n", sol); + +} + + + +int main() { + + + + cit(); + + solve(); + + + + return 0; + +} diff --git a/swap/merge/21caf728-5cec-4dda-9d80-6bc93899e0ee.cpp b/swap/merge/21caf728-5cec-4dda-9d80-6bc93899e0ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4343fb6afc4dd5044ae6002fe8d098abb851a108 --- /dev/null +++ b/swap/merge/21caf728-5cec-4dda-9d80-6bc93899e0ee.cpp @@ -0,0 +1,271 @@ +#include #include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/21e83e1d-0b67-11ec-983a-2cf05d84bfc2.cpp b/swap/merge/21e83e1d-0b67-11ec-983a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75a53c89789ca1de8ca40b84b0a955f2b3d9b9e9 --- /dev/null +++ b/swap/merge/21e83e1d-0b67-11ec-983a-2cf05d84bfc2.cpp @@ -0,0 +1,137 @@ +#include +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] +# include +# include + + +const long int MAXN=50000; + + + +typedef struct NOD {long int pos; NOD *next;}; + +typedef struct NODA {long int inf; NODA *stga,*drta;}; + +NODA *rad; + + + +struct {NOD *prim,*ultim;} list[30]; + +long int n; + +char s1[MAXN+10],s2[MAXN+10]; + + + +void add(long int i, long int pos) + +{ + +NOD *p=(NOD*) malloc (sizeof(NOD)); + +(*p).pos=pos;(*p).next=NULL; + +if (list[i].prim) + + { + + (*list[i].ultim).next=p; + + list[i].ultim=p; + + } + +else + + { + + list[i].prim=p; + + list[i].ultim=p; + + } + +} + + + +void citire() + +{ + +FILE *f=fopen("swap.in","r"); + +fgets(s1+1,MAXN+10,f); + +fgets(s2+1,MAXN+10,f); + +n=strlen(s1+1)-1; + +fclose(f); + +} + + + +long int parity() + +{ + +long int count[30]={0}; + +long int i; + +for (i=1;i<=n;i++) + + { + + count[s1[i]-'a'+1]++; + + count[s2[i]-'a'+1]--; + + } + +for (i=1;i<='z'-'a'+1;i++) if (count[i]!=0) return 0; + +return 1; + +} + + + +void create() + +{ + +long int i; + +for (i=1;i<=n;i++) + + add(s1[i]-'a'+1,i); + +} + + + +void update(NODA *p, long int li, long int lf, long int a) + +{ + +long int m=(li+lf)/2; + +if (lf>=li) + + if (li==lf) (*p).inf++; + + else + + { + + if (a<=m) + + { + + (*p).inf++; + + update((*p).stga,li,m,a); + + } + + else + + { + + (*p).inf++; + + update((*p).drta,m+1,lf,a); + + } + + } + +} + + + +long int query(NODA *p,long int li, long int lf, long int a, long int b) + +{ + +long int m=(li+lf)/2; + +if (b>=a) + + { + + if (li==a&&lf==b) return (*p).inf; + + if (b<=m) return query((*p).stga,li,m,a,b); + + if (a>m ) return query((*p).drta,m+1,lf,a,b); + + return query((*p).stga,li,m,a,m)+query((*p).drta,m+1,lf,m+1,b); + + } + +return 0; + +} + + + +long int calculeaza() + +{ + +long int sol=0; + +long int i; + +for (i=1;i<=n;i++) + + { + + + + sol+=(*list[s2[i]-'a'+1].prim).pos-i+query(rad,1,n,(*list[s2[i]-'a'+1].prim).pos+1,n); + + update(rad,1,n,(*list[s2[i]-'a'+1].prim).pos); + + list[s2[i]-'a'+1].prim=(*list[s2[i]-'a'+1].prim).next; + + } + +return sol; + +} + + + +void scrie(long int a) + +{ + +FILE *g=fopen("swap.out","w"); + +fprintf(g,"%ld\n",a); + +fcloseall(); + +} + + + +void aloca(NODA *rad, long int li, long int lf) + +{ + +NODA *p; + +long int m=(li+lf)/2; + +if (li +#include +#include + + + + + + +using namespace std; + + + + + + + +char S[2][50666]; + +vector v[30]; + +int cnt[2][30], P[50666], Paux[50666]; + +int i, j, k, N, ninv; + + + +void compute_ninv(int li, int ls) + +{ + + int m; + + + + if (li >= ls) + + return; + + + + m = (li + ls) >> 1; + + + + compute_ninv(li, m); + + compute_ninv(m + 1, ls); + + + + i = li, j = m + 1, k = li - 1; + + + + while (i <= m && j <= ls) + + { + + if (P[i] < P[j]) + + Paux[++k] = P[i++]; + + else + + { + + ninv += (m - i + 1); + + Paux[++k] = P[j++]; + + } + + } + + + + if (i <= m) + + { + + for (j = i; j <= m; j++) + + Paux[++k] = P[j]; + + } + + else + + { + + for (i = j; i <= ls; i++) + + Paux[++k] = P[i]; + + } + + + + for (i = li; i <= ls; i++) + + P[i] = Paux[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S[0] + 1, 50666, stdin); + + fgets(S[1] + 1, 50666, stdin); + + + + N = strlen(S[0] + 1) - 1; + + + + memset(cnt, 0, sizeof(cnt)); + + for (i = 1; i <= N; i++) + + { + + cnt[1][S[1][i] - 'a']++; + + v[S[1][i] - 'a'].push_back(i); + + } + + + + for (i = 1; i <= N; i++) + + { + + cnt[0][S[0][i] - 'a']++; + + + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + + } + + + + ninv = 0; + + compute_ninv(1, N); + + + + printf("%d\n", ninv); + + + + return 0; + +} diff --git a/swap/merge/2349cc51-0b67-11ec-a693-2cf05d84bfc2.cpp b/swap/merge/2349cc51-0b67-11ec-a693-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac02631102d4deaba49fe0f85e7b1ed6f19508dd --- /dev/null +++ b/swap/merge/2349cc51-0b67-11ec-a693-2cf05d84bfc2.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int T[2*(1<<16)], V[50666], N, X[30], Rez, P, Q, Cate; + +char S1[50666], S2[50666]; + +vector A[30]; + + + +void update(int p1, int p2, int nod) + +{ + + int mij; + + + + if (P <= p1 && p2 <= Q) + + { + + T[nod]++; + + return; + + } + + + + if ((p1 <= P && P <= p2) || (p1 <= Q && Q <= p2)) + + { + + mij = (p1+p2)/2; + + update(p1, mij, 2*nod); + + update(mij+1, p2, 2*nod+1); + + } + +} + + + +void query(int x) + +{ + + int nod = (1<<16)+x; + + + + while (nod > 0) + + { + + Cate += T[nod]; + + nod = nod>>1; + + } + +} + + + +int main() + +{ + + int i, n; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + + + n = strlen(S1); + + for (i = 0; i < n; i++) + + A[ S1[i]-'a' ].push_back(i); + + + + if (strlen(S1) != n) { printf("-1\n"); return 0; } + + + + for (i = 0; i < n; i++) + + { + + if ((A[ S2[i]-'a' ].size() == 0) || (A[ S2[i]-'a' ].size() == X[ S2[i]-'a' ])) + + { printf("-1\n"); return 0; } + + V[ A[ S2[i]-'a' ][ X[ S2[i]-'a' ]++ ] ] = i; + + i = i+1; i--; + + } + + + + for (i = 0; i < n; i++) + + { + + Cate = 0; + + query(V[i]); + + Rez += (V[i]-Cate); + + P = V[i]+1; Q = n; + + update(0, (1<<16)-1, 1); + + } + + + + printf("%d\n", Rez); + + return 0; + + + +} diff --git a/swap/merge/23906ff1-0b67-11ec-9c36-2cf05d84bfc2.cpp b/swap/merge/23906ff1-0b67-11ec-9c36-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ff0d5dae446ea2257e7a80835f24ce5a950d732 --- /dev/null +++ b/swap/merge/23906ff1-0b67-11ec-9c36-2cf05d84bfc2.cpp @@ -0,0 +1,308 @@ +#include +#include +#include + + +using namespace std; + + + +typedef struct punct {int s; int p; int s2; int p2;} point; + + + + + + + +int n, i, dp, k; + +long long sol; + +point x[60010], y[60010], z[60010]; + +int ai[4*60010], sl[60010]; + +char a[60010], b[60010]; + + + +int operator<(const point &c, const point &d) + + + +{ + + if (!dp) + + { + + if (c.s != d.s) + + return c.s < d.s; + + else + + return c.p < d.p; + + } + + else + + return c.p < d.p; + +} + + + +void ins(int nod, int nr, int l, int r) + + + +{ + + int mid = (l + r)/2; + + + + if (nr <=r && nr >=l && (l<=r)) + + { + + ai[nod]++; + + if (nr > mid) + + ins(2*nod+1, nr, mid+1, r); + + else + + if (l != r) + + ins(2*nod, nr, l, mid); + + } + + + +} + + + +int find(int nod, int a, int b, int l, int r) + + + +{ + + int mid = (l+r)/2; + + + + if (a==l && b==r) + + return ai[nod]; + + + + if (a >=l && b<=r) + + { + + if (a<=mid && b>=mid+1) + + { + + return (find(2*nod, a, mid, l, mid) + find(2*nod+1, mid+1, b, mid+1, r)); + + } + + else + + if (a<=mid && b<=mid) + + return (find(2*nod, a, b, l, mid)); + + else + + if (a>=mid+1 && b>=mid+1) + + return (find(2*nod+1, a, b, mid+1, r)); + + + + } + + + + + +} + + + + + +void read() + + + +{ + + + + + + scanf("%s\n%s ", a, b); + +} + + + + + +void solve() + + + +{ + + n = strlen(a); + for (i=0; i 0) + + ins(1, z[0].p2-1, 0, n-1); + + + + sol+=sl[0]; + + + + for (i=1; i0) + + ins(1, z[i].p2-1, 0, n-1); + + sol+=sl[i]; + + } + } + + + +} + + + + + +void write() + + + +{ + + printf("%lld\n", sol); + +} + + + + + +int main() + + + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out","w",stdout); + + + + read(); + + solve(); + + write(); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/23c20744-4f23-4b9d-8699-fb1d9b847f02.cpp b/swap/merge/23c20744-4f23-4b9d-8699-fb1d9b847f02.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2c612599b44de63fd7d36dcab78864515a3fe9 --- /dev/null +++ b/swap/merge/23c20744-4f23-4b9d-8699-fb1d9b847f02.cpp @@ -0,0 +1,152 @@ +#include + + + + +using namespace std; + + + +ifstream fin("swap.in") ; + +ofstream fout("swap.out") ; + + + +string sir1,sir2 ; + +int ax[50005] , nrpr[50005] ; + +int n , p[50005] , t[50005] ,inv , v[50005] ; + +vector graf[50005] , graf2[50005] ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft == rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( p[x] <= p[y] ) + + t[i++] = p[x++] ; + + else + + { + + t[i++] = p[y++] ; + + inv = inv +mij-x+1 ; + + } + + } + + while ( x <= mij ) + + t[i++] = p[x++] ; + + while ( y <= rght ) + + t[i++] = p[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + p[i] = t[i] ; + +} + + + +int main() + +{ + + int i , j ,m; + + fin >> sir1 ; + + fin >> sir2 ; + + sir1 = '#' + sir1 ; + + sir2 = '#' + sir2 ; + + n = sir1.size()-1 ; + + m = sir2.size()-1 ; + + if ( n!= m ) + + { + + fout << "-1" ; + + return 0 ; + + } + + for ( i = 1 ; i <= n ; i++ ) + + nrpr[sir1[i]-'a'+1]++ , nrpr[sir2[i]-'a'+1]-- ; + + for ( i = 1 ; i <= n ; i++ ) + + { + + if ( nrpr[i] != 0 ) + + { + + fout << "-1" ; + + return 0 ; + + } + + } + + for ( i = 1 ; i <= n ; i++ ) + + { + + graf[sir1[i]-'a'+1].push_back(i) ; + + graf2[sir2[i]-'a'+1].push_back(i) ; + + } + + for ( i = 1 ; i <= 27 ; i++ ) + + for ( j = 0 ; j < graf[i].size() ; j++ ) + + p[graf[i][j]] = graf2[i][j] ; + + mergemerge(1,n) ; + + fout << inv ; + +} diff --git a/swap/merge/23feb98a-0b67-11ec-b2f3-2cf05d84bfc2.cpp b/swap/merge/23feb98a-0b67-11ec-b2f3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f83fc9da6cb060dde03aa622903872cd83322ac --- /dev/null +++ b/swap/merge/23feb98a-0b67-11ec-b2f3-2cf05d84bfc2.cpp @@ -0,0 +1,88 @@ +#include + + + +int a[50001],b[50001],poz[50001],apar[27][50001],segm[128*50001]; +int a1,b1,sol,cost,value; +FILE *in,*out; + +void ins_del(int nod,int st,int dr) { +int m; + if (a1==st && dr==a1) segm[nod]=value; + + else { + m=(st+dr)/2; + if (a1<=m) ins_del(2*nod,st,m); + else ins_del(2*nod+1,m+1,dr); + segm[nod]=segm[2*nod]+segm[2*nod+1]; + } +} + +void query(int nod,int st,int dr) { +int m; + if (a1<=st && dr<=b1) cost+=segm[nod]; + else { + m=(st+dr)/2; + if (a1<=m) query(2*nod,st,m); + if (b1>m) query(2*nod+1,m+1,dr); + } +} + +int main() { +int i,good=1; +char cache; + in=fopen("swap.in","r"); out=fopen("swap.out","w"); + + cache=fgetc(in); + while (cache!='\n') { + a[++a[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + cache=fgetc(in); + while (cache!='\n' && cache!=EOF) { + b[++b[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + for (i=1;i<=a[0];++i) apar[a[i]][++apar[a[i]][0]]=i; + + for (i=0;i<27;++i) apar[i][0]=1; + + value=1; + + for (i=1;i<=b[0];++i) { + if (!apar[b[i]][apar[b[i]][0]]) { good=0; break; } + + else + poz[i]=apar[b[i]][apar[b[i]][0]++]; + a1=i; + ins_del(1,1,a[0]); + } + + value=0; + + if (!good) fprintf(out,"-1\n"); + + else { + + for (i=1;i<=b[0];++i) { + + a1=1; b1=poz[i]; + cost=0; + query(1,1,a[0]); + + sol+=cost-1; + + a1=poz[i]; + ins_del(1,1,a[0]); + + } + + fprintf(out,"%i\n",sol); + } + + fclose(in); fclose(out); + + return 0; +} diff --git a/swap/merge/2424a7d7-94ec-4e37-bf4a-3b6d7231de23.cpp b/swap/merge/2424a7d7-94ec-4e37-bf4a-3b6d7231de23.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7c3deb44ac4d6a594dd4e95f4fe8e2b0efaaed2f --- /dev/null +++ b/swap/merge/2424a7d7-94ec-4e37-bf4a-3b6d7231de23.cpp @@ -0,0 +1,86 @@ +#include #include #include + using namespace std; + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include +using namespace std; + +const int NO_LIT = 'z' - 'a' + 1; +const int LMAX = 50001; +vectorlitere[NO_LIT + 1]; +int left[NO_LIT + 1], aib[LMAX], l; + +char a[LMAX],b[LMAX]; + +int askAIB(int pos) { + int ret = 0; + while (pos > 0) { + ret += aib[pos]; + pos -= pos ^ (pos & (pos - 1)); + } + return ret; +} + +void incrAIB(int pos) { + while (pos <= l) { + aib[pos]++; + pos += pos ^ (pos & (pos - 1)); + } +} + +int solve() { + FILE *fin = fopen("swap.in", "r"); + fscanf (fin, "%s\n", &a); + fscanf (fin, "%s\n", &b); + l = strlen(a); + if (strlen(b) != l) return -1; + + int i; + for (i = 0; i < l; ++i) { + litere[a[i]-'a'].push_back(i); + } + int ret = 0; + for (i = 0; i < l; ++i) { + int litera = b[i] - 'a'; + if (left[litera] == litere[litera].size()) { + return -1; + } + int pos = litere[litera][left[litera]++]; + ret += pos - askAIB(pos); + incrAIB(pos + 1); + } + return ret; +} + +int main() { + fprintf (fopen("swap.out", "w"), "%d", solve()); + return 0; +} diff --git a/swap/merge/245ec100-0b67-11ec-b404-2cf05d84bfc2.cpp b/swap/merge/245ec100-0b67-11ec-b404-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9205f65561db012ea4daab80ba54d7ca05fbf541 --- /dev/null +++ b/swap/merge/245ec100-0b67-11ec-b404-2cf05d84bfc2.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +using namespace std; + + + +const int nmax = 51200; + +int seq[nmax], nseq[nmax]; + + + +int go(int lo, int hi) { + + if (lo >= hi) return 0; + + + + int mid = (lo + hi) >> 1; + + int res = go(lo, mid) + go(mid+1, hi); + + + + int i = lo, j = mid+1, k = lo; + + while (i <= mid && j <= hi) { + + if (seq[i] <= seq[j]) { + + nseq[k++] = seq[i++]; + + } else { + + nseq[k++] = seq[j++]; + + res += mid + 1 - i; + + } + + } + + while (i <= mid) { + + nseq[k++] = seq[i++]; + + } + + while (j <= hi) { + + nseq[k++] = seq[j++]; + + } + + + + for (i = lo; i <= hi; ++i) seq[i] = nseq[i]; + + return res; + +} + + + +int main() { + + freopen("swap.in", "rt", stdin); + + freopen("swap.out", "wt", stdout); + + + + char s1[nmax], s2[nmax]; + + fgets(s1, 50001, stdin); + + fgets(s2, 50001, stdin); + + + + int N = strlen(s1); + + if (strlen(s2) != N) { + + printf("-1\n"); + + return 0; + + } + + + + deque pos[32]; + + for (int i = 0; i < N; ++i) + + pos[s1[i]-'a'].push_back(i); + + + + for (int i = 0; i < N; ++i) { + + int ind = s2[i]-'a'; + + if (pos[ind].empty()) { + + printf("-1\n"); + + return 0; + + } + + seq[i] = pos[ind].front(); + + pos[ind].pop_front(); + + } + + + + printf("%d\n", go(0, N-1)); + + + + return 0; + +} diff --git a/swap/merge/24a0e252-0b67-11ec-8716-2cf05d84bfc2.cpp b/swap/merge/24a0e252-0b67-11ec-8716-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..539f5169dc1d55a57cc829c8a3a29309b00e851a --- /dev/null +++ b/swap/merge/24a0e252-0b67-11ec-8716-2cf05d84bfc2.cpp @@ -0,0 +1,90 @@ +#include + + + +int a[50001],b[50001],poz[50001],apar[27][50001],segm[128*50001]; +int a1,b1,sol,cost,value; +FILE *in,*out; + +void ins_del(int nod,int st,int dr) { +int m; + if (a1==st && dr==a1) segm[nod]=value; + + else { + m=(st+dr)/2; + if (a1<=m) ins_del(2*nod,st,m); + else ins_del(2*nod+1,m+1,dr); + segm[nod]=segm[2*nod]+segm[2*nod+1]; + } +} + +void query(int nod,int st,int dr) { +int m; + if (a1<=st && dr<=b1) cost+=segm[nod]; + else { + m=(st+dr)/2; + if (a1<=m) query(2*nod,st,m); + if (b1>m) query(2*nod+1,m+1,dr); + } +} + +int main() { +int i,good=1; +char cache; + in=fopen("swap.in","r"); out=fopen("swap.out","w"); + + cache=fgetc(in); + while (cache!='\n') { + a[++a[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + cache=fgetc(in); + while (cache!='\n' && cache!=EOF) { + b[++b[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + + + for (i=1;i<=a[0];++i) apar[a[i]][++apar[a[i]][0]]=i; + + for (i=0;i<27;++i) apar[i][0]=1; + + value=1; + + for (i=1;i<=b[0];++i) { + if (!apar[b[i]][apar[b[i]][0]]) { good=0; break; } + + else + poz[i]=apar[b[i]][apar[b[i]][0]++]; + a1=i; + ins_del(1,1,a[0]); + } + + value=0; + + if (!good) fprintf(out,"-1\n"); + + else { + + for (i=1;i<=b[0];++i) { + + a1=1; b1=poz[i]; + cost=0; + query(1,1,a[0]); + + sol+=cost-1; + + a1=poz[i]; + ins_del(1,1,a[0]); + + } + + fprintf(out,"%i\n",sol); + } + + fclose(in); fclose(out); + + return 0; +} diff --git a/swap/merge/29106a97-d2e3-4563-aba0-1183aa547c0d.cpp b/swap/merge/29106a97-d2e3-4563-aba0-1183aa547c0d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0ff0d5dae446ea2257e7a80835f24ce5a950d732 --- /dev/null +++ b/swap/merge/29106a97-d2e3-4563-aba0-1183aa547c0d.cpp @@ -0,0 +1,308 @@ +#include +#include +#include + + +using namespace std; + + + +typedef struct punct {int s; int p; int s2; int p2;} point; + + + + + + + +int n, i, dp, k; + +long long sol; + +point x[60010], y[60010], z[60010]; + +int ai[4*60010], sl[60010]; + +char a[60010], b[60010]; + + + +int operator<(const point &c, const point &d) + + + +{ + + if (!dp) + + { + + if (c.s != d.s) + + return c.s < d.s; + + else + + return c.p < d.p; + + } + + else + + return c.p < d.p; + +} + + + +void ins(int nod, int nr, int l, int r) + + + +{ + + int mid = (l + r)/2; + + + + if (nr <=r && nr >=l && (l<=r)) + + { + + ai[nod]++; + + if (nr > mid) + + ins(2*nod+1, nr, mid+1, r); + + else + + if (l != r) + + ins(2*nod, nr, l, mid); + + } + + + +} + + + +int find(int nod, int a, int b, int l, int r) + + + +{ + + int mid = (l+r)/2; + + + + if (a==l && b==r) + + return ai[nod]; + + + + if (a >=l && b<=r) + + { + + if (a<=mid && b>=mid+1) + + { + + return (find(2*nod, a, mid, l, mid) + find(2*nod+1, mid+1, b, mid+1, r)); + + } + + else + + if (a<=mid && b<=mid) + + return (find(2*nod, a, b, l, mid)); + + else + + if (a>=mid+1 && b>=mid+1) + + return (find(2*nod+1, a, b, mid+1, r)); + + + + } + + + + + +} + + + + + +void read() + + + +{ + + + + + + scanf("%s\n%s ", a, b); + +} + + + + + +void solve() + + + +{ + + n = strlen(a); + for (i=0; i 0) + + ins(1, z[0].p2-1, 0, n-1); + + + + sol+=sl[0]; + + + + for (i=1; i0) + + ins(1, z[i].p2-1, 0, n-1); + + sol+=sl[i]; + + } + } + + + +} + + + + + +void write() + + + +{ + + printf("%lld\n", sol); + +} + + + + + +int main() + + + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out","w",stdout); + + + + read(); + + solve(); + + write(); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/29ff7127-efb3-44cb-b66b-fb65ac0269fb.cpp b/swap/merge/29ff7127-efb3-44cb-b66b-fb65ac0269fb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f5d9ae25cda0936322d2f1155ad2d13ad115d34 --- /dev/null +++ b/swap/merge/29ff7127-efb3-44cb-b66b-fb65ac0269fb.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/2ae49cce-0c37-4032-9b15-55e68a9504f1.cpp b/swap/merge/2ae49cce-0c37-4032-9b15-55e68a9504f1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b92e7f70db762a65107659ab1fb642eb8632d11 --- /dev/null +++ b/swap/merge/2ae49cce-0c37-4032-9b15-55e68a9504f1.cpp @@ -0,0 +1,180 @@ +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +char S[2][50666]; + +vector v[30]; + +int cnt[2][30], P[50666], Paux[50666]; + +int i, j, k, N, ninv; + + + +void compute_ninv(int li, int ls) + +{ + + int m; + + + + if (li >= ls) + + return; + + + + m = (li + ls) >> 1; + + + + compute_ninv(li, m); + + compute_ninv(m + 1, ls); + + + + i = li, j = m + 1, k = li - 1; + + + + while (i <= m && j <= ls) + + { + + if (P[i] < P[j]) + + Paux[++k] = P[i++]; + + else + + { + + ninv += (m - i + 1); + + Paux[++k] = P[j++]; + + } + + } + + + + if (i <= m) + + { + + for (j = i; j <= m; j++) + + Paux[++k] = P[j]; + + } + + else + + { + + for (i = j; i <= ls; i++) + + Paux[++k] = P[i]; + + } + + + + for (i = li; i <= ls; i++) + + P[i] = Paux[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S[0] + 1, 50666, stdin); + + fgets(S[1] + 1, 50666, stdin); + + + + N = strlen(S[0] + 1) - 1; + + + + memset(cnt, 0, sizeof(cnt)); + + for (i = 1; i <= N; i++) + + { + + cnt[1][S[1][i] - 'a']++; + + v[S[1][i] - 'a'].push_back(i); + + } + + + + for (i = 1; i <= N; i++) + + { + + cnt[0][S[0][i] - 'a']++; + + + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + + } + + + + ninv = 0; + + compute_ninv(1, N); + + + + printf("%d\n", ninv); + + + + return 0; + +} diff --git a/swap/merge/2be854f8-5dbc-44e5-be19-dce6fe5e0048.cpp b/swap/merge/2be854f8-5dbc-44e5-be19-dce6fe5e0048.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8b8bbb9c145d7ad21a0a8141ec3fa02879b52a7c --- /dev/null +++ b/swap/merge/2be854f8-5dbc-44e5-be19-dce6fe5e0048.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +using namespace std; + +const int NO_LIT = 'z' - 'a' + 1; +const int LMAX = 50001; +vectorlitere[NO_LIT + 1]; +int left[NO_LIT + 1], aib[LMAX], l; + +char a[LMAX],b[LMAX]; + +int askAIB(int pos) { + int ret = 0; + while (pos > 0) { + ret += aib[pos]; + pos -= pos ^ (pos & (pos - 1)); + } + return ret; +} + +void incrAIB(int pos) { + while (pos <= l) { + aib[pos]++; + pos += pos ^ (pos & (pos - 1)); + } +} + +int solve() { + FILE *fin = fopen("swap.in", "r"); + fscanf (fin, "%s\n", &a); + fscanf (fin, "%s\n", &b); + l = strlen(a); + if (strlen(b) != l) return -1; + + int i; + for (i = 0; i < l; ++i) { + litere[a[i]-'a'].push_back(i); + } + int ret = 0; + for (i = 0; i < l; ++i) { + int litera = b[i] - 'a'; + if (left[litera] == litere[litera].size()) { + return -1; + } + int pos = litere[litera][left[litera]++]; + ret += pos - askAIB(pos); + incrAIB(pos + 1); + } + return ret; +} + +int main() { + fprintf (fopen("swap.out", "w"), "%d", solve()); + return 0; +} diff --git a/swap/merge/2d8a1268-ac16-423f-8f7c-275df8fa5c31.cpp b/swap/merge/2d8a1268-ac16-423f-8f7c-275df8fa5c31.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eede082f575bbd79eb9f1c56d01ac777518189a1 --- /dev/null +++ b/swap/merge/2d8a1268-ac16-423f-8f7c-275df8fa5c31.cpp @@ -0,0 +1,183 @@ +#include +#include + + +using namespace std; + + + +int N, v[50005]; + +string A, B; + + + +void Citire () { + + ifstream fin ("swap.in"); + + fin >> A >> B; + + fin.close (); + + N = A.size (); + +} + + + +int alfa[30], beta[30]; + + + +int Good () { + + for (int i = 0; i < N; i++) + + { + + alfa[A[i] - 'a']++; + + beta[B[i] - 'a']++; + + } + + for (int i = 0; i <= 30; i++) + + if (!(alfa[i] == beta[i])) return 0; + + return 1; + +} + + + +void Initializare () { + + int k; + + for (char i = 'a'; i <= 'z'; i++) + + { + + k = -1; + + for (int j = 0; j < N; j++) + + { + + if (A[j] == i) + + { + + for (++k; k < N; k++) + + { + + if (B[k] == i) + + { + + v[k] = j; + + break; + + } + + } + + } + + } + + } + +} + + + +int aux[50005]; + + + +int Business (int L, int R) { + + int cnt = 0, mij = (L + R) >> 1, k = L - 1, i, j; + + if (L >= R) return 0; + + cnt += Business (L, mij); + + cnt += Business (mij + 1, R); + + for (i = L, j = mij + 1; i <= mij && j <= R;) + + { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else + + { + + aux[++k] = v[j++]; + + cnt += mij - i + 1; + + } + + } + + while (i <= mij) + + { + + aux[++k] = v[i++]; + + } + + while (j <= R) + + { + + aux[++k] = v[j++]; + + } + + for (int i = L; i <= R; i++) + + v[i] = aux[i]; + + return cnt; + +} + + + +int main () { + + Citire (); + + ofstream fout ("swap.out"); + + if (!Good ()) + + { + + fout << "-1"; + + fout.close (); + + return 0; + + } + + Initializare (); + + fout << Business (0, N - 1); + + fout.close (); + + return 0; + +} diff --git a/swap/merge/2e7099c2-b89c-4a86-b284-28e9d1c0a092.cpp b/swap/merge/2e7099c2-b89c-4a86-b284-28e9d1c0a092.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88ca8ccdf25aa83e310951d6097b821c6c3a811b --- /dev/null +++ b/swap/merge/2e7099c2-b89c-4a86-b284-28e9d1c0a092.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + + +char A[50000 + 2], B[50000 + 2]; + +int next[50000]; +int c[26]; + +int P[50000]; +int tmp[50000]; +int n; + + +inline int mergeCount(int left, int right) { + int i, j, k; + int mid = (left + right) / 2; + int cnt = 0; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] < P[j]) { + tmp[k] = P[i]; + i++; + } else { + cnt += (mid - i + 1); + tmp[k] = P[j]; + j++; + } + k++; + } + while (i <= mid) { + tmp[k++] = P[i]; + i++; + } + while (j <= right) { + tmp[k++] = P[j]; + j++; + } + memcpy(P + left, tmp + left, (right - left + 1) << 2); + return cnt; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) >> 1; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int pos, character; + + fgets(A, 50000 + 2, f); + fgets(B, 50000 + 2, f); + fclose(f); + + n = strlen(A) - 1; + + + for (int i = 0; i < 26; i++) { + c[i] = -1; + } + + for (int i = n - 1; i >= 0; i--) { + character = A[i] - 'a'; + next[i] = c[character]; + c[character] = i; + } + + + + + pos = 0; + do { + character = B[pos] - 'a'; + P[pos] = c[character]; + c[character] = next[c[character]]; + pos++; + } while ((pos < n) && (P[pos - 1] != -1)); + + f = fopen("swap.out", "w"); + if (pos != n || P[n - 1] == -1) { + fputs("-1\n", f); + } else { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } + fclose(f); + return 0; +} diff --git a/swap/merge/2f5c5a6f-4953-46e2-b83f-df422f69989d.cpp b/swap/merge/2f5c5a6f-4953-46e2-b83f-df422f69989d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4281264594f093ddd264f439950bf4d32b3f56 --- /dev/null +++ b/swap/merge/2f5c5a6f-4953-46e2-b83f-df422f69989d.cpp @@ -0,0 +1,183 @@ +#include #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/301d8ef9-ed0c-44fe-921b-892d03a7018b.cpp b/swap/merge/301d8ef9-ed0c-44fe-921b-892d03a7018b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2a3b821a10274868c4dc02c37986b22243302d08 --- /dev/null +++ b/swap/merge/301d8ef9-ed0c-44fe-921b-892d03a7018b.cpp @@ -0,0 +1,86 @@ +#include #include #include + using namespace std; + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include +using namespace std; + + + +int f[1001], INV1 = 0, INV2 = 0, pos[201][50001], d[50001], LE[50001], C[50001]; + +char a[50002], b[50002]; + +int c[50002]; + +inline void Merge(int st, int mij, int dr){ + + int i = st, j = mij + 1, NR = 0; + + while(i <= mij && j <= dr){ + + if(d[i] > d[j]){ + + c[++NR] = d[j++]; + + INV1 = INV1 + mij - i + 1; + + } + + else + + c[++NR] = d[i++]; + + } + + while(j <= dr) + + c[++NR] = d[j++]; + + while(i <= mij) + + c[++NR] = d[i++]; + + for(i = st , j = 1; i <= dr ; ++i, ++j) + + d[i] = c[j]; + +} + +inline void MergeSort(int st, int dr){ + + if(st >= dr) return ; + + MergeSort(st, (st + dr) / 2); + + MergeSort((st + dr) / 2 + 1, dr); + + Merge(st, (st + dr) / 2, dr); + +} + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + scanf("%s", a); + + scanf("%s", b); + + int L = strlen(a); + + for(int i = 0 ; i < L ; ++i) + + ++f[a[i]]; + + for(int i = 0 ; i < L ; ++i){ + + if(f[b[i]] == 0) + + {printf("-1"); return 0;} + + --f[b[i]]; + + } + + for(int i = 0 ; i < L ; ++i) + + pos[a[i]][++LE[a[i]]] = i + 1; + + for(int i = 0 ; i < L ; ++i) + + d[i] = pos[b[i]][++C[b[i]]]; + + MergeSort(0, L - 1); + + printf("%d", INV1); + + return 0; + +} diff --git a/swap/merge/34a97cc9-694e-438b-9ab6-98301230a4e0.cpp b/swap/merge/34a97cc9-694e-438b-9ab6-98301230a4e0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6199e366d57b479e3822e8dc4d4275fdc7c67cbe --- /dev/null +++ b/swap/merge/34a97cc9-694e-438b-9ab6-98301230a4e0.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/merge/355467b2-dc1a-467b-9467-5804aafbce2d.cpp b/swap/merge/355467b2-dc1a-467b-9467-5804aafbce2d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/355467b2-dc1a-467b-9467-5804aafbce2d.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/36f4a61a-f9f1-4bc8-9fcb-69e558c84003.cpp b/swap/merge/36f4a61a-f9f1-4bc8-9fcb-69e558c84003.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0658b62970cf652cb4b2bbf6e0cd86eb8583fbc --- /dev/null +++ b/swap/merge/36f4a61a-f9f1-4bc8-9fcb-69e558c84003.cpp @@ -0,0 +1,92 @@ +# include +char a[50005]; + +char b[50005]; + +int r[26]; + +std :: queue < int > v[26]; + +int s[200005],p[50005]; + +int n; + +void update(int p,int u,int v,int nod) + +{ + + if (p == u) s[nod] = 1; + + else + + { + + int m = (p + u) / 2; + + if (v <= m) update(p,m,v,nod*2); + + else update(m+1,u,v,nod*2+1); + + s[nod] = s[nod*2] + s[nod*2+1]; + + } + +} + +int query(int p,int u,int r,int nod) + +{ + + if (u <= r) return s[nod]; + + int ans = 0; + + int m = (p + u) / 2; + + ans += query(p,m,r,nod*2); + + if (m+1<=r) ans += query(m+1,u,r,nod*2+1); + + return ans; + +} + +int main(void) + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1, 50001, stdin);fgets(b+1, 50001, stdin); + + n = strlen(a+1); + + for (int i = 1;i <= n;++i) a[i] -= 'a',b[i] -= 'a'; + + for (int i = 1;i <= n;++i) ++r[a[i]],--r[b[i]]; + + for (int i = 0;i < 26;++i) if (r[i]) + + return printf("-1\n"),0; + + for (int i = 1;i <= n;++i) + + v[b[i]].push(i); + + int ans = 0; + + for (int i = 1;i <= n;++i) + + { + + p[i] = n - v[a[i]].front() + 1;v[a[i]].pop(); + + } + + for (int i = 1;i <= n;++i) ans += query(1,n,p[i],1),update(1,n,p[i],1); + + return printf("%d\n",ans),0; + +} diff --git a/swap/merge/388ad243-eb43-4594-98c4-6565d5a48599.cpp b/swap/merge/388ad243-eb43-4594-98c4-6565d5a48599.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3bd13450f873014840b5bf9cad4224cd9160c5 --- /dev/null +++ b/swap/merge/388ad243-eb43-4594-98c4-6565d5a48599.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50001; + +int n,nr,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); sort(m+1,dr); merge(st,dr,m); + + } + +} + +int main() + +{ f>>a>>b; n=strlen(a); + + int i,e=0; + + for(i=0;i +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/399e5b57-e9e0-4f82-8af9-e09b0279d43f.cpp b/swap/merge/399e5b57-e9e0-4f82-8af9-e09b0279d43f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbf9ad120b351ee7dc3669ce1dcd5cc7409f4e84 --- /dev/null +++ b/swap/merge/399e5b57-e9e0-4f82-8af9-e09b0279d43f.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/3b3daf8a-ba1b-4658-91e7-0ce6468e8f01.cpp b/swap/merge/3b3daf8a-ba1b-4658-91e7-0ce6468e8f01.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb3bd13450f873014840b5bf9cad4224cd9160c5 --- /dev/null +++ b/swap/merge/3b3daf8a-ba1b-4658-91e7-0ce6468e8f01.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50001; + +int n,nr,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); sort(m+1,dr); merge(st,dr,m); + + } + +} + +int main() + +{ f>>a>>b; n=strlen(a); + + int i,e=0; + + for(i=0;i +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +vector h[27]; + +vector L[27]; + +int P[50010], n, cnt; + +char a[50010], b[50010]; + + + +void read() + +{ + + int i, j, ok; + + fin >> a >> b; + + n = strlen(a); + + + + for (i=n; i>=1; i--) + + { + + a[i] = a[i-1]; + + b[i] = b[i-1]; + + } + + + + for (i=0; i<=26; i++) + + { + + for (j=1; j<=n; j++) + + { + + if (b[j] - 'a' == i) + + h[i].push_back(j); + + if (a[j] - 'a' == i) + + L[i].push_back(j); + + } + + } + + + + ok = 1; + + for (i=0; i<=26 && ok==1; i++) + + if (h[i].size() != L[i].size()) + + ok = 0; + + + + if (ok == 0) + + { + + fout << "-1\n"; + + exit(0); + + } + + + + for (i=n; i>=1; i--) + + { j = a[i]-'a'; + + P[i] = h[j][h[j].size()-1]; + + h[j].pop_back(); + + } + +} + + + +void merge(int st, int m, int dr) + +{ + + int Q[50009], i, j, k; + + i = st; + + j = m+1; + + k = 0; + + while (i <= m && j <= dr) + + { + + if (P[i] <= P[j]) + + Q[++k] = P[i++]; + + else + + { + + Q[++k] = P[j++]; + + cnt += (m - i + 1); + + } + + } + + while (i <= m) + + Q[++k] = P[i++]; + + while (j <= dr) + + Q[++k] = P[j++]; + + + + j = st; + + for (i = 1; i <= k; ++i) + + P[j++] = Q[i]; + +} + + + +void Interschimba(int i, int j) + +{ + + if (P[i] > P[j]) + + { + + cnt++; + + swap(P[i], P[j]); + + } + +} + + + +void MergeSort(int st, int dr) + +{ + + int m, i; + + if (dr - st <= 1) Interschimba(st, dr); + + else + + { + + m = (st + dr) / 2; + + MergeSort(st, m); + + MergeSort(m + 1, dr); + + merge(st, m, dr); + + } + +} + + + +int main () + +{ + + read(); + + MergeSort(1,n); + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/merge/3d3ee8a6-0c19-11ec-ab68-2cf05d84bfc2.cpp b/swap/merge/3d3ee8a6-0c19-11ec-ab68-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22d652bb37a72bebbfd8db7d10f6d9ad4b0fea80 --- /dev/null +++ b/swap/merge/3d3ee8a6-0c19-11ec-ab68-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void do_merge(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + do_merge(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/3e4baded-7014-4d08-bb6e-489c03836fd5.cpp b/swap/merge/3e4baded-7014-4d08-bb6e-489c03836fd5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/merge/3e4baded-7014-4d08-bb6e-489c03836fd5.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/3e9d9314-4020-4005-a92e-adf410c1008b.cpp b/swap/merge/3e9d9314-4020-4005-a92e-adf410c1008b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbf9ad120b351ee7dc3669ce1dcd5cc7409f4e84 --- /dev/null +++ b/swap/merge/3e9d9314-4020-4005-a92e-adf410c1008b.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/4042267d-7757-4d92-b6a5-825b96c9f25e.cpp b/swap/merge/4042267d-7757-4d92-b6a5-825b96c9f25e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0396582c735c9e2c51829a58515d53c40aca8de7 --- /dev/null +++ b/swap/merge/4042267d-7757-4d92-b6a5-825b96c9f25e.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; + +const int Q_sze=30; + +const int Maxx=5e4+1; + +deque Q[Q_sze]; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int aint[4*Maxx],vis[4*Maxx]; + +int i,n,ans,len,m; + +string s; + +void build(int node,int left,int right); + +int query(int node,int tot); + +int main() + +{ + + fin>>s; + + n=s.size(); + + for (i=n-1;i>=0;--i) Q[s[i]-96].push_back(i); + + build(1,1,n); + + fin>>s; + + for (i=0;i #include +#include + + +using namespace std; + + + + + + + +int n, m, i, j, k, sol, p2; + +int cor[50010]; + +int arb[3 * 50010]; + + + +char a[50010], b[50010]; + +vector s1[30], s2[30]; + + + +void cit() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s", a); + + scanf("%s", b); + +} + + + +inline int query(int nod, int st, int dr, int ask) { + + if (st == dr) return arb[nod]; + + + + int r = (st + dr) / 2; + + + + if (ask <= r) return arb[nod] + query(nod * 2, st, r, ask); + + else return arb[nod] + query(nod * 2 + 1, r + 1, dr, ask); + +} + + + +void update(int nod, int st, int dr, int left, int right) { + + if (left > dr || right < st) return; + + + + if (left <= st && dr <= right) { + + arb[nod]++; + + return; + + } + + + + int r = (st + dr) / 2; + + update(nod * 2, st, r, left, right); + + update(nod * 2 + 1, r + 1, dr, left, right); + +} + + + +int cost(int poz) { + + int pozitie = cor[poz] + query(1, 1, p2, cor[poz]); + + + + update(1, 1, p2, 1, cor[poz] - 1); + + + + return pozitie - i; + +} + + + +void solve() { + + + + n = strlen(a); + + m = strlen(b); + + for (i = 0; i < n; i++) + + s1[a[i] - 'a'].push_back(i); + + for (j = 0; j < m; j++) + + s2[b[j] - 'a'].push_back(j); + + + + + + for (i = 0; i < 30; i++) + + if (s1[i].size() != s2[i].size()) { + + printf("-1\n"); + + return; + + } + + + + + + for (i = 0; i < 30; i++) { + + k = s2[i].size(); + + for (j = 0; j < k; j++) + + cor[s2[i][j]] = s1[i][j]; + + } + + + + + + p2 = 1; + + while (p2 < n) p2 *= 2; + + for (i = 0; i < n; i++) + + sol += cost(i); + + printf("%d\n", sol); + +} + + + +int main() { + + + + cit(); + + solve(); + + + + return 0; + +} diff --git a/swap/merge/42ae8b80-2a04-408a-8af7-8f7196b2aac3.cpp b/swap/merge/42ae8b80-2a04-408a-8af7-8f7196b2aac3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2c612599b44de63fd7d36dcab78864515a3fe9 --- /dev/null +++ b/swap/merge/42ae8b80-2a04-408a-8af7-8f7196b2aac3.cpp @@ -0,0 +1,152 @@ +#include + + + + +using namespace std; + + + +ifstream fin("swap.in") ; + +ofstream fout("swap.out") ; + + + +string sir1,sir2 ; + +int ax[50005] , nrpr[50005] ; + +int n , p[50005] , t[50005] ,inv , v[50005] ; + +vector graf[50005] , graf2[50005] ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft == rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( p[x] <= p[y] ) + + t[i++] = p[x++] ; + + else + + { + + t[i++] = p[y++] ; + + inv = inv +mij-x+1 ; + + } + + } + + while ( x <= mij ) + + t[i++] = p[x++] ; + + while ( y <= rght ) + + t[i++] = p[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + p[i] = t[i] ; + +} + + + +int main() + +{ + + int i , j ,m; + + fin >> sir1 ; + + fin >> sir2 ; + + sir1 = '#' + sir1 ; + + sir2 = '#' + sir2 ; + + n = sir1.size()-1 ; + + m = sir2.size()-1 ; + + if ( n!= m ) + + { + + fout << "-1" ; + + return 0 ; + + } + + for ( i = 1 ; i <= n ; i++ ) + + nrpr[sir1[i]-'a'+1]++ , nrpr[sir2[i]-'a'+1]-- ; + + for ( i = 1 ; i <= n ; i++ ) + + { + + if ( nrpr[i] != 0 ) + + { + + fout << "-1" ; + + return 0 ; + + } + + } + + for ( i = 1 ; i <= n ; i++ ) + + { + + graf[sir1[i]-'a'+1].push_back(i) ; + + graf2[sir2[i]-'a'+1].push_back(i) ; + + } + + for ( i = 1 ; i <= 27 ; i++ ) + + for ( j = 0 ; j < graf[i].size() ; j++ ) + + p[graf[i][j]] = graf2[i][j] ; + + mergemerge(1,n) ; + + fout << inv ; + +} diff --git a/swap/merge/44648991-9eb6-4b32-994d-b08b146aa02c.cpp b/swap/merge/44648991-9eb6-4b32-994d-b08b146aa02c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31b8ecd0a945bc196f509a4b830bb4362c700dfb --- /dev/null +++ b/swap/merge/44648991-9eb6-4b32-994d-b08b146aa02c.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/448cdd04-ebf4-492b-81ff-ac67f4307ab9.cpp b/swap/merge/448cdd04-ebf4-492b-81ff-ac67f4307ab9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/448cdd04-ebf4-492b-81ff-ac67f4307ab9.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< #include #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A+1, 50001, stdin); + + fgets( B+1, 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/455fc3f0-f7b2-445a-97c8-54446ece516b.cpp b/swap/merge/455fc3f0-f7b2-445a-97c8-54446ece516b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbf9ad120b351ee7dc3669ce1dcd5cc7409f4e84 --- /dev/null +++ b/swap/merge/455fc3f0-f7b2-445a-97c8-54446ece516b.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/46073182-155f-4d1b-a730-84261c1eff9f.cpp b/swap/merge/46073182-155f-4d1b-a730-84261c1eff9f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d67da457069e37ba2137a23a4d659274bcbb2dc --- /dev/null +++ b/swap/merge/46073182-155f-4d1b-a730-84261c1eff9f.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + +const int MAX_N = 50010; + +const int MAX_L = 28; + + + +int f[2][MAX_L], d[MAX_N], c[MAX_N], sol; + +char a[MAX_N], b[MAX_N]; + +vector v[MAX_L]; + + + +void merge(int st, int dr) + +{ + + int i, j, k; + + int mid = st + (dr - st) / 2; + + if (st >= dr) return; + + + + merge(st, mid); + + merge(mid + 1, dr); + + + + i = st; + + j = mid + 1; + + k = st - 1; + + + + while (i <= mid && j <= dr) + + { + + if (d[i] < d[j]) c[++k] = d[i++]; + + else + + { + + sol += (mid - i + 1); + + c[++k] = d[j++]; + + } + + } + + + + if (i <= mid) + + for (; i <= mid; ++i) c[++k] = d[i]; + + else for (i = j; i <= dr; ++i) c[++k] = d[i]; + + + + for (i = st; i <= dr; ++i) d[i] = c[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s\n", a + 1); + + scanf("%s\n", b + 1); + + int l = strlen(a + 1); + + + + for (int i = 1; i <= l; ++i) + + { + + ++f[1][b[i] - 'a']; + + v[b[i] - 'a'].push_back(i); + + } + + + + for (int i = 1; i <= l; i++) + + { + + ++f[0][a[i] - 'a']; + + if (f[0][a[i] - 'a'] > f[1][a[i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + d[i] = v[a[i] - 'a'][f[0][a[i] - 'a'] - 1]; + + } + + + + merge(1, l); + + + + printf("%d\n", sol); + +} diff --git a/swap/merge/48f8f7e2-4468-4370-a3fb-0f7224b3485e.cpp b/swap/merge/48f8f7e2-4468-4370-a3fb-0f7224b3485e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..31b8ecd0a945bc196f509a4b830bb4362c700dfb --- /dev/null +++ b/swap/merge/48f8f7e2-4468-4370-a3fb-0f7224b3485e.cpp @@ -0,0 +1,181 @@ +#include +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/49e4682c-a00c-423f-8282-502400a78aaf.cpp b/swap/merge/49e4682c-a00c-423f-8282-502400a78aaf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/49e4682c-a00c-423f-8282-502400a78aaf.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/49edcb43-f0c2-4e17-b06d-b92679ef0100.cpp b/swap/merge/49edcb43-f0c2-4e17-b06d-b92679ef0100.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c13c9a0e87d1af66a71d82bf642148d66d401e3 --- /dev/null +++ b/swap/merge/49edcb43-f0c2-4e17-b06d-b92679ef0100.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/4cc14656-63ab-42b0-bf93-044e21c6676e.cpp b/swap/merge/4cc14656-63ab-42b0-bf93-044e21c6676e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..75a53c89789ca1de8ca40b84b0a955f2b3d9b9e9 --- /dev/null +++ b/swap/merge/4cc14656-63ab-42b0-bf93-044e21c6676e.cpp @@ -0,0 +1,137 @@ +#include +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] + + + + + + +char a[50002],b[50002]; + +int lita[27][50002],litb[27][50002]; + +int P[50002],B[50002]; + +int N,nr,ok; + + + +void merge(int l,int r){ + + int m=l+(r-l)/2,aux; + + if( l==r ) return; + + if( l==r-1 ){ + + if( P[l]>P[r] ) ++nr, aux=P[l],P[l]=P[r],P[r]=aux; + + return; + + } + + + + merge(l,m); + + merge(m+1,r); + + + + int i,j; + + B[0]=0; + + for(i=l,j=m+1; i<=m && j<=r; ) + + if( P[i] <= P[j] ) B[++B[0]]=P[i++]; + + else B[++B[0]]=P[j++], nr+=m-i+1; + + for(; i<=m; ++i) B[++B[0]]=P[i]; + + for(; j<=r; ++j) B[++B[0]]=P[j]; + + + + for(i=l;i<=r;++i) P[i]=B[i-l+1]; + + + +} + + + +void precal_p(){ + + int i,j; + + for(i=0;i<27;++i) + + if( lita[i][0] != litb[i][0] ){ + + ok=0; return; + + } + + else + + for(j=1;j<=lita[i][0];++j) + + P[lita[i][j]]=litb[i][j]; + +} + + + +int main(){ + + int i; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s%s",a+1,b+1); + + for(i=1; a[i]!='\0' && a[i]!='\n'; ) ++i; + + N=i-1; + + + + for(i=1;i<=N;++i){ + + lita[a[i]-'a'][++lita[a[i]-'a'][0]]=i; + + litb[b[i]-'a'][++litb[b[i]-'a'][0]]=i; + + } + + + + ok=1; + + precal_p(); + + if( ok ) merge(1,N); + + else nr=-1; + + + + printf("%d\n",nr); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/swap/merge/4eb7c3d6-85e4-49bf-a013-9184328037d1.cpp b/swap/merge/4eb7c3d6-85e4-49bf-a013-9184328037d1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aaeba17fd72e9d45e03f19fb1a2b51b4dda55df --- /dev/null +++ b/swap/merge/4eb7c3d6-85e4-49bf-a013-9184328037d1.cpp @@ -0,0 +1,92 @@ +#include +#include +#include + + +using namespace std; + +ifstream si("swap.in"); + +ofstream so("swap.out"); + + + +int n,u,num,rez,p[250000],vs[250000]; + +string s; + +vector pos[300]; + +void make(int node,int st,int dr) { + + if(st==dr) { + + p[++num]=node; + + return; + + } + + int md=(st+dr)/2; + + make(2*node,st,md); + + make(2*node+1,md+1,dr); + +} + +int take(int node,int sum) { + + if(node==1) + + return sum; + + if(node/2*2+1!=node) + + vs[node/2*2+1]++; + + return take(node/2,sum+vs[node]); + +} + +int main() { + + getline(si,s); + + n=s.size(); + + for(int i=n-1; i>=0; i--) + + pos[s[i]-96].push_back(i); + + make(1,1,n); + + rez=-n; + + getline(si,s); + + for(int i=0; i +#include +#include +using namespace std; +char s[50001], c[50001]; + +deque list[256]; + + +int perm[50001], w[50001]; +int n, m, nr; + +void merge(int v[], int l, int r){ + int m = (l + r) / 2, k = l - 1, i = 0, j = 0; + for(i = l, j = m + 1; i <= m && j <= r;){ + if(v[i] <= v[j]) + w[++k] = v[i++]; + else{ + w[++k] = v[j++]; + nr += m - i + 1; + } + } + for(; i <= m;) + w[++k] = v[i++]; + for(; j <= r;) + w[++k] = v[j++]; + for(i = l; i <= r; ++i) + v[i] = w[i]; +} +void sortare(int v[], int l, int r){ + if(l>=r) + return; + int m = (l + r) / 2; + sortare(v, l, m); + sortare(v, m + 1, r); + merge(v, l, r); +} +void read(){ + freopen("swap.in", "r", stdin); + scanf("%s %s", &s, &c); + n = strlen(s); + m = strlen(c); +} + +int cnt[256]; + +void solve(){ + int i, j; + for (i = 0; i < m ; ++i) + cnt[ int(c[i]) ]++; + + for (i = 0; i < n; ++i) + list[ int(s[i]) ].push_back(i + 1); + + for (i = 0; i < 256; ++i) + if (cnt[i] != list[i].size()) { + printf ("-1\n"); + return ; + } + +int nperm; + + + for (i = 0; i < m; ++i) { + perm[i + 1] = list[ int(c[i]) ][0]; + list[ int(c[i]) ].pop_front(); + } + + sortare(perm, 1, m); + printf("%d\n",nr); +} +int main(){ + freopen("swap.out", "w", stdout); + read(); + solve(); +} diff --git a/swap/merge/50481125-3fab-4a73-a466-94ded89db70a.cpp b/swap/merge/50481125-3fab-4a73-a466-94ded89db70a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27fa7cfd0360f54c2fd1e145b8912faa5ca01c2d --- /dev/null +++ b/swap/merge/50481125-3fab-4a73-a466-94ded89db70a.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/5345ccf3-219a-4bfb-9860-ee32b2cb6cc9.cpp b/swap/merge/5345ccf3-219a-4bfb-9860-ee32b2cb6cc9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f5d9ae25cda0936322d2f1155ad2d13ad115d34 --- /dev/null +++ b/swap/merge/5345ccf3-219a-4bfb-9860-ee32b2cb6cc9.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/54c2a815-224b-48d0-957c-ce195ac747f6.cpp b/swap/merge/54c2a815-224b-48d0-957c-ce195ac747f6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ef395385b2857fa2ceedd42b3e6e28ebb0aef40 --- /dev/null +++ b/swap/merge/54c2a815-224b-48d0-957c-ce195ac747f6.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + +const int DIM = 50100; + +const int EXP = 26; + +using namespace std; + + + +int N, ok, Aux[DIM]; long long sol; char A[DIM], B[DIM]; + +vector L[EXP]; int P[DIM], S1[EXP], S2[EXP], V[DIM]; + + + +void merge_sort( int left, int right ) { + + if( left == right ) return; + + + + int middle = left + ( right - left ) / 2; + + int index1, index2 = middle, pos = left; + + + + merge_sort( left, middle ); + + merge_sort( middle + 1, right ); + + + + for( index1 = left; index1 <= middle; index1 ++ ) { + + while( index2 < right && P[index1] > P[index2 + 1] ) + + index2 ++; + + sol += index2 - middle; + + } + + + + index1 = left; index2 = middle + 1; + + while( index1 <= middle || index2 <= right ) { + + if( index1 > middle ) Aux[pos++] = P[index2++]; else + + if( index2 > right ) Aux[pos++] = P[index1++]; else + + if( P[index1] <= P[index2] ) + + Aux[pos++] = P[index1++]; + + else + + Aux[pos++] = P[index2++]; + + } + + + + for( int i = left; i <= right; i ++ ) + + P[i] = Aux[i]; + + + + return; + +} + + + +int main () { + + + + freopen( "swap.in" , "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + scanf( "%s %s", A + 1, B + 1 ); + + N = strlen( A + 1 ); + + + + for( int i = 1; i <= N; i ++ ) { + + S1[A[i] - 'a'] ++; + + S2[B[i] - 'a'] ++; + + V[i] = S1[A[i] - 'a']; + + L[B[i] - 'a'].push_back( i ); + + } + + + + ok = 1; + + for( int i = 0; i < 26; i ++ ) + + if( S1[i] != S2[i] ) ok = 0; + + + + if( ok == 0 ) + + printf( "-1\n" ); + + else { + + for( int i = 1; i <= N; i ++ ) + + P[i] = L[A[i] - 'a'][V[i] - 1]; + + + + merge_sort( 1, N ); + + printf( "%lld\n", sol ); + + } + + + + return 0; + +} diff --git a/swap/merge/55dc1ff5-6534-4602-b878-f81c0f29b494.cpp b/swap/merge/55dc1ff5-6534-4602-b878-f81c0f29b494.cpp new file mode 100644 index 0000000000000000000000000000000000000000..27fa7cfd0360f54c2fd1e145b8912faa5ca01c2d --- /dev/null +++ b/swap/merge/55dc1ff5-6534-4602-b878-f81c0f29b494.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/55dca4c4-67a8-4078-aa85-e058b2322183.cpp b/swap/merge/55dca4c4-67a8-4078-aa85-e058b2322183.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0a14251742e1fa14a92527d1f9c20d09da8b66 --- /dev/null +++ b/swap/merge/55dca4c4-67a8-4078-aa85-e058b2322183.cpp @@ -0,0 +1,284 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + + + for(j = 0; j < frecB[i].size(); j++) + + { + + + + } + + + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + for(i = 1; i <= n; i++) + + { + + + + } + + + + } + + + +} diff --git a/swap/merge/58a5cf33-2848-4696-9990-f31249bc2b78.cpp b/swap/merge/58a5cf33-2848-4696-9990-f31249bc2b78.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4ba596157bc3eba5af4ca2864e897707820540d --- /dev/null +++ b/swap/merge/58a5cf33-2848-4696-9990-f31249bc2b78.cpp @@ -0,0 +1,144 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +char beg[50001]; + +int sir[50001],arb[3*50001],viz[50001],i,n,poz,pozst,pozdr,ch,nr; + +long long sum; + +vector G[50]; + +void upDate(int nod, int st, int dr) + +{ + + if(st==dr) + + { + + arb[nod]=1; + + return; + + } + + int mijl=(st+dr)/2; + + if(poz<=mijl) + + upDate(2*nod,st,mijl); + + else + + upDate(2*nod+1,mijl+1,dr); + + arb[nod]=arb[2*nod]+arb[2*nod+1]; + +} + +void query(int nod, int st, int dr) + +{ + + if(st>=pozst&&dr<=pozdr) + + { + + sum+=arb[nod]; + + return; + + } + + int mijl=(st+dr)/2; + + if(pozst<=mijl) + + query(2*nod,st,mijl); + + if(pozdr>mijl) + + query(2*nod+1,mijl+1,dr); + +} + +int main() + +{ + + fin>>(beg+1); + + n=strlen(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + G[ch].push_back(i); + + } + + fin>>(beg+1); + + for(i=1;i<=n;i++) + + { + + ch=beg[i]-96; + + if(viz[ch] +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/60448c3c-fca7-483c-a9b1-9f24357498d2.cpp b/swap/merge/60448c3c-fca7-483c-a9b1-9f24357498d2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22d652bb37a72bebbfd8db7d10f6d9ad4b0fea80 --- /dev/null +++ b/swap/merge/60448c3c-fca7-483c-a9b1-9f24357498d2.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void do_merge(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + do_merge(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/61760d5d-0e1c-4a83-b4ce-3fb58db64373.cpp b/swap/merge/61760d5d-0e1c-4a83-b4ce-3fb58db64373.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a464859816f891b958463b2e8aec8484b2e9fc05 --- /dev/null +++ b/swap/merge/61760d5d-0e1c-4a83-b4ce-3fb58db64373.cpp @@ -0,0 +1,170 @@ +#include #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A+1, 50001, stdin); + + fgets( B+1, 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/62250831-215f-4d81-b085-2eba000b0e4c.cpp b/swap/merge/62250831-215f-4d81-b085-2eba000b0e4c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..365de5cf7c67d0626c61fb8054d15be54b13f83f --- /dev/null +++ b/swap/merge/62250831-215f-4d81-b085-2eba000b0e4c.cpp @@ -0,0 +1,113 @@ +#include +#include + + +using namespace std; + + + + + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +long nr[50100],NR[50100],i,n,num,P[50100],B[50100],a[50100][30],A[50100]; + +char s[50100],t[50100]; + +void merge (int st,int dr ) { + + + + if(st==dr) + + return ; + + int m=(st+dr)>>1; + + + + int i,j,p; + + merge(st,m); + + merge(m+1,dr); + + i=st;j=m+1;p=0; + + while(i<=m || j<=dr ){ + + if((P[j]m){ + + B[st+p++]=P[j++]; + + num+=m-i+1; + + } + + else + + B[st+p++]=P[i++]; + + } + + for(int u=st;u<=dr;++u){ + + P[u]=B[u]; + + B[u]=0; + + } + +} + +int main (){ + + f>>(s+1); + + f.get();f>>(t+1); + + f.get(); + + n=strlen(s+1); + + for(i=1;i<=n;++i){ + + a[++nr[s[i]-96]][s[i]-96]=i; + + + + } + + + + for(i=1;i<=n;i++){ + + + + P[a[++NR[t[i]-96]][t[i]-96]]=i; + + + + } + + for(i=0;i<=26;i++) + + if(NR[i]!=nr[i]){ + + g<<-1<<"\n"; + + + + return 0; + + } + + merge(1,n); + + g< +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A+1, 50001, stdin); + + fgets( B+1, 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/634073ab-774c-4bc3-a3c9-f12c0ac5038f.cpp b/swap/merge/634073ab-774c-4bc3-a3c9-f12c0ac5038f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0480c8c88a397ed07d1731f22be4c58ada83d358 --- /dev/null +++ b/swap/merge/634073ab-774c-4bc3-a3c9-f12c0ac5038f.cpp @@ -0,0 +1,177 @@ +#include #include +#include + + +using namespace std; + + + + + + + +int n, m, i, j, k, sol, p2; + +int cor[50010]; + +int arb[3 * 50010]; + + + +char a[50010], b[50010]; + +vector s1[30], s2[30]; + + + +void cit() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s", a); + + scanf("%s", b); + +} + + + +inline int query(int nod, int st, int dr, int ask) { + + if (st == dr) return arb[nod]; + + + + int r = (st + dr) / 2; + + + + if (ask <= r) return arb[nod] + query(nod * 2, st, r, ask); + + else return arb[nod] + query(nod * 2 + 1, r + 1, dr, ask); + +} + + + +void update(int nod, int st, int dr, int left, int right) { + + if (left > dr || right < st) return; + + + + if (left <= st && dr <= right) { + + arb[nod]++; + + return; + + } + + + + int r = (st + dr) / 2; + + update(nod * 2, st, r, left, right); + + update(nod * 2 + 1, r + 1, dr, left, right); + +} + + + +int cost(int poz) { + + int pozitie = cor[poz] + query(1, 1, p2, cor[poz]); + + + + update(1, 1, p2, 1, cor[poz] - 1); + + + + return pozitie - i; + +} + + + +void solve() { + + + + n = strlen(a); + + m = strlen(b); + + for (i = 0; i < n; i++) + + s1[a[i] - 'a'].push_back(i); + + for (j = 0; j < m; j++) + + s2[b[j] - 'a'].push_back(j); + + + + + + for (i = 0; i < 30; i++) + + if (s1[i].size() != s2[i].size()) { + + printf("-1\n"); + + return; + + } + + + + + + for (i = 0; i < 30; i++) { + + k = s2[i].size(); + + for (j = 0; j < k; j++) + + cor[s2[i][j]] = s1[i][j]; + + } + + + + + + p2 = 1; + + while (p2 < n) p2 *= 2; + + for (i = 0; i < n; i++) + + sol += cost(i); + + printf("%d\n", sol); + +} + + + +int main() { + + + + cit(); + + solve(); + + + + return 0; + +} diff --git a/swap/merge/65ae9c6a-9414-4f93-ad20-28620f176211.cpp b/swap/merge/65ae9c6a-9414-4f93-ad20-28620f176211.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bec3af33d438190dc199b11ad054da4aa13fb248 --- /dev/null +++ b/swap/merge/65ae9c6a-9414-4f93-ad20-28620f176211.cpp @@ -0,0 +1,86 @@ +#include #include +#include + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + +using namespace std; + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include +#include + + + + +char buffer[2 * 50000 + 2]; +std::vector position[26]; +int start[26]; + +int P[50000]; +int tmp[50000]; + +int n; + +inline int mergeCount(int left, int mid, int right) { + int invCount = 0; + int i, j, k; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] <= P[j]) { + tmp[k++] = P[i++]; + } else { + tmp[k++] = P[j++]; + invCount += (mid - i + 1); + } + } + while (i <= mid) { + tmp[k++] = P[i++]; + } + while (j <= right) { + tmp[k++] = P[j++]; + } + for (i = left; i <= right; i++) + P[i] = tmp[i]; + return invCount; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) / 2; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, mid, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int ptr = 0; + int character; + + fread(buffer, 1, 2 * 50000 + 2, f); + fclose(f); + + do { + character = buffer[ptr] - 'a'; + position[character].emplace_back(ptr); + } while (isalpha(buffer[++ptr])); + + ptr++; + n = 0; + + do { + character = buffer[ptr] - 'a'; + if (start[character] < (int) position[character].size()) { + P[n++] = position[character][start[character]++]; + } else { + n = -1; + } + } while ((~n) && (isalpha(buffer[++ptr]))); + + f = fopen("swap.out", "w"); + if (~n) { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } else { + fputs("-1\n", f); + } + fclose(f); + return 0; +} diff --git a/swap/merge/685637e5-201d-44fe-b188-9eb806b02cc7.cpp b/swap/merge/685637e5-201d-44fe-b188-9eb806b02cc7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c110dfc220c299529601bcf56bb693e48fc30a --- /dev/null +++ b/swap/merge/685637e5-201d-44fe-b188-9eb806b02cc7.cpp @@ -0,0 +1,178 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + for(i=0;i<26;i++) + + ind[i]=0; + + + + for(pos=0;posv[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/6964d282-4da5-43e2-9b9c-74e71fb19b0f.cpp b/swap/merge/6964d282-4da5-43e2-9b9c-74e71fb19b0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188dfe6520406b685fed012c8e6a0c8eae563c0b --- /dev/null +++ b/swap/merge/6964d282-4da5-43e2-9b9c-74e71fb19b0f.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/6b55f4bf-fdf5-4193-97fa-8bf5af978560.cpp b/swap/merge/6b55f4bf-fdf5-4193-97fa-8bf5af978560.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/6b55f4bf-fdf5-4193-97fa-8bf5af978560.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< +#include +#include + + +using namespace std; + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + +ifstream fin(iname); + +ofstream fout(oname); + +deque Q[300]; + +int i, lg, lg2, n, ANS, sol; + +int j_s1[300], j_s2[300], P[50004]; + +int H[50004 * 3]; + +char s1[50004], s2[50004]; + +inline void update (int n, int l, int r, int p, int v){ + + if (l == r){H[n] = v; return;} + + int m = (l + r) / 2, L = n << 1, R = L | 1; + + if (p <= m) update (L, l, m, p, v); + + else + + update (R, m + 1, r, p, v); + + H[n] = H[L] + H[R]; + +} + +inline int query (int n, int l, int r, int i, int j){ + + if (i <= l && r <= j) return H[n]; + + int m = (l + r) / 2; int sol = 0; + + if (i <= m) sol += query(2 * n, l, m, i, j); + + if (j > m) sol += query(2 * n + 1, m + 1, r, i, j); + + return sol; + +} + +int main() + +{ + + fin >> s1; lg = strlen(s1); + + for (i = 0; i < lg; ++i) + + ++j_s1[(int)s1[i]], + + Q[(int)s1[i]].push_back(i + 1); + + fin >> s2; lg2 = strlen(s2); + + if (lg != lg2){fout << -1 << '\n'; return 0;} + + for (i = 0; i < lg; ++i) + + { + + ++j_s2[(int)s2[i]]; + + P[++n] = Q[s2[i]][0]; + + Q[s2[i]].pop_front(); + + } + + for (i = 0; i < lg; ++i) + + { + + if (j_s1[(int)s1[i]] != j_s2[(int)s1[i]]){ + + fout << -1 << '\n'; return 0; + + } + + } + + update (1, 1, n, P[1], 1); + + for (i = 2; i <= n; ++i) + + { + + update (1, 1, n, P[i], 1); + + ANS += query (1, 1, n, P[i] + 1, n); + + } + + fout << ANS << '\n'; + + return 0; + +} diff --git a/swap/merge/70b173d1-e25c-483a-9cee-6bfc75efe1ea.cpp b/swap/merge/70b173d1-e25c-483a-9cee-6bfc75efe1ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3145e2bba904877ed99571657e3ddc4a79c8429 --- /dev/null +++ b/swap/merge/70b173d1-e25c-483a-9cee-6bfc75efe1ea.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/71f3a26c-d916-45a2-8da3-b6a7980ade9e.cpp b/swap/merge/71f3a26c-d916-45a2-8da3-b6a7980ade9e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38e9f7a399d6595846b50b7d687f81a93243aff5 --- /dev/null +++ b/swap/merge/71f3a26c-d916-45a2-8da3-b6a7980ade9e.cpp @@ -0,0 +1,278 @@ +# include +# include +# include + + +const long int MAXN=50000; + + + +typedef struct NOD {long int pos; NOD *next;}; + +typedef struct NODA {long int inf; NODA *stga,*drta;}; + +NODA *rad; + + + +struct {NOD *prim,*ultim;} list[30]; + +long int n; + +char s1[MAXN+10],s2[MAXN+10]; + + + +void add(long int i, long int pos) + +{ + +NOD *p=(NOD*) malloc (sizeof(NOD)); + +(*p).pos=pos;(*p).next=NULL; + +if (list[i].prim) + + { + + (*list[i].ultim).next=p; + + list[i].ultim=p; + + } + +else + + { + + list[i].prim=p; + + list[i].ultim=p; + + } + +} + + + +void citire() + +{ + +FILE *f=fopen("swap.in","r"); + +fgets(s1+1,MAXN+10,f); + +fgets(s2+1,MAXN+10,f); + +n=strlen(s1+1)-1; + +fclose(f); + +} + + + +long int parity() + +{ + +long int count[30]={0}; + +long int i; + +for (i=1;i<=n;i++) + + { + + count[s1[i]-'a'+1]++; + + count[s2[i]-'a'+1]--; + + } + +for (i=1;i<='z'-'a'+1;i++) if (count[i]!=0) return 0; + +return 1; + +} + + + +void create() + +{ + +long int i; + +for (i=1;i<=n;i++) + + add(s1[i]-'a'+1,i); + +} + + + +void update(NODA *p, long int li, long int lf, long int a) + +{ + +long int m=(li+lf)/2; + +if (lf>=li) + + if (li==lf) (*p).inf++; + + else + + { + + if (a<=m) + + { + + (*p).inf++; + + update((*p).stga,li,m,a); + + } + + else + + { + + (*p).inf++; + + update((*p).drta,m+1,lf,a); + + } + + } + +} + + + +long int query(NODA *p,long int li, long int lf, long int a, long int b) + +{ + +long int m=(li+lf)/2; + +if (b>=a) + + { + + if (li==a&&lf==b) return (*p).inf; + + if (b<=m) return query((*p).stga,li,m,a,b); + + if (a>m ) return query((*p).drta,m+1,lf,a,b); + + return query((*p).stga,li,m,a,m)+query((*p).drta,m+1,lf,m+1,b); + + } + +return 0; + +} + + + +long int calculeaza() + +{ + +long int sol=0; + +long int i; + +for (i=1;i<=n;i++) + + { + + + + sol+=(*list[s2[i]-'a'+1].prim).pos-i+query(rad,1,n,(*list[s2[i]-'a'+1].prim).pos+1,n); + + update(rad,1,n,(*list[s2[i]-'a'+1].prim).pos); + + list[s2[i]-'a'+1].prim=(*list[s2[i]-'a'+1].prim).next; + + } + +return sol; + +} + + + +void scrie(long int a) + +{ + +FILE *g=fopen("swap.out","w"); + +fprintf(g,"%ld\n",a); + +fcloseall(); + +} + + + +void aloca(NODA *rad, long int li, long int lf) + +{ + +NODA *p; + +long int m=(li+lf)/2; + +if (li +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] + + + +int a[50001],b[50001],poz[50001],apar[27][50001],segm[128*50001]; +int a1,b1,sol,cost,value; +FILE *in,*out; + +void ins_del(int nod,int st,int dr) { +int m; + if (a1==st && dr==a1) segm[nod]=value; + + else { + m=(st+dr)/2; + if (a1<=m) ins_del(2*nod,st,m); + else ins_del(2*nod+1,m+1,dr); + segm[nod]=segm[2*nod]+segm[2*nod+1]; + } +} + +void query(int nod,int st,int dr) { +int m; + if (a1<=st && dr<=b1) cost+=segm[nod]; + else { + m=(st+dr)/2; + if (a1<=m) query(2*nod,st,m); + if (b1>m) query(2*nod+1,m+1,dr); + } +} + +int main() { +int i,good=1; +char cache; + in=fopen("swap.in","r"); out=fopen("swap.out","w"); + + cache=fgetc(in); + while (cache!='\n') { + a[++a[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + cache=fgetc(in); + while (cache!='\n' && cache!=EOF) { + b[++b[0]]=(int)cache-'a'; + cache=fgetc(in); + } + + for (i=1;i<=a[0];++i) apar[a[i]][++apar[a[i]][0]]=i; + + for (i=0;i<27;++i) apar[i][0]=1; + + value=1; + + for (i=1;i<=b[0];++i) { + if (!apar[b[i]][apar[b[i]][0]]) { good=0; break; } + + else + poz[i]=apar[b[i]][apar[b[i]][0]++]; + a1=i; + ins_del(1,1,a[0]); + } + + value=0; + + if (!good) fprintf(out,"-1\n"); + + else { + + for (i=1;i<=b[0];++i) { + + a1=1; b1=poz[i]; + cost=0; + query(1,1,a[0]); + + sol+=cost-1; + + a1=poz[i]; + ins_del(1,1,a[0]); + + } + + fprintf(out,"%i\n",sol); + } + + fclose(in); fclose(out); + + return 0; +} diff --git a/swap/merge/76ad92ea-92bb-4450-9894-6b77fbb7f800.cpp b/swap/merge/76ad92ea-92bb-4450-9894-6b77fbb7f800.cpp new file mode 100644 index 0000000000000000000000000000000000000000..88ca8ccdf25aa83e310951d6097b821c6c3a811b --- /dev/null +++ b/swap/merge/76ad92ea-92bb-4450-9894-6b77fbb7f800.cpp @@ -0,0 +1,99 @@ +#include +#include + + + + + +char A[50000 + 2], B[50000 + 2]; + +int next[50000]; +int c[26]; + +int P[50000]; +int tmp[50000]; +int n; + + +inline int mergeCount(int left, int right) { + int i, j, k; + int mid = (left + right) / 2; + int cnt = 0; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] < P[j]) { + tmp[k] = P[i]; + i++; + } else { + cnt += (mid - i + 1); + tmp[k] = P[j]; + j++; + } + k++; + } + while (i <= mid) { + tmp[k++] = P[i]; + i++; + } + while (j <= right) { + tmp[k++] = P[j]; + j++; + } + memcpy(P + left, tmp + left, (right - left + 1) << 2); + return cnt; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) >> 1; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int pos, character; + + fgets(A, 50000 + 2, f); + fgets(B, 50000 + 2, f); + fclose(f); + + n = strlen(A) - 1; + + + for (int i = 0; i < 26; i++) { + c[i] = -1; + } + + for (int i = n - 1; i >= 0; i--) { + character = A[i] - 'a'; + next[i] = c[character]; + c[character] = i; + } + + + + + pos = 0; + do { + character = B[pos] - 'a'; + P[pos] = c[character]; + c[character] = next[c[character]]; + pos++; + } while ((pos < n) && (P[pos - 1] != -1)); + + f = fopen("swap.out", "w"); + if (pos != n || P[n - 1] == -1) { + fputs("-1\n", f); + } else { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } + fclose(f); + return 0; +} diff --git a/swap/merge/76e6098e-5a11-479a-a9e5-5fedecf7b9fc.cpp b/swap/merge/76e6098e-5a11-479a-a9e5-5fedecf7b9fc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/76e6098e-5a11-479a-a9e5-5fedecf7b9fc.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< +#include +#include +using namespace std; + + + +const int nmax = 51200; + +int seq[nmax], nseq[nmax]; + + + +int go(int lo, int hi) { + + if (lo >= hi) return 0; + + + + int mid = (lo + hi) >> 1; + + int res = go(lo, mid) + go(mid+1, hi); + + + + int i = lo, j = mid+1, k = lo; + + while (i <= mid && j <= hi) { + + if (seq[i] <= seq[j]) { + + nseq[k++] = seq[i++]; + + } else { + + nseq[k++] = seq[j++]; + + res += mid + 1 - i; + + } + + } + + while (i <= mid) { + + nseq[k++] = seq[i++]; + + } + + while (j <= hi) { + + nseq[k++] = seq[j++]; + + } + + + + for (i = lo; i <= hi; ++i) seq[i] = nseq[i]; + + return res; + +} + + + +int main() { + + freopen("swap.in", "rt", stdin); + + freopen("swap.out", "wt", stdout); + + + + char s1[nmax], s2[nmax]; + + fgets(s1, 50001, stdin); + + fgets(s2, 50001, stdin); + + + + int N = strlen(s1); + + if (strlen(s2) != N) { + + printf("-1\n"); + + return 0; + + } + + + + deque pos[32]; + + for (int i = 0; i < N; ++i) + + pos[s1[i]-'a'].push_back(i); + + + + for (int i = 0; i < N; ++i) { + + int ind = s2[i]-'a'; + + if (pos[ind].empty()) { + + printf("-1\n"); + + return 0; + + } + + seq[i] = pos[ind].front(); + + pos[ind].pop_front(); + + } + + + + printf("%d\n", go(0, N-1)); + + + + return 0; + +} diff --git a/swap/merge/7b2ea691-9917-49e2-9d15-241d8e5e5f33.cpp b/swap/merge/7b2ea691-9917-49e2-9d15-241d8e5e5f33.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188dfe6520406b685fed012c8e6a0c8eae563c0b --- /dev/null +++ b/swap/merge/7b2ea691-9917-49e2-9d15-241d8e5e5f33.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/7d7e968d-0849-4d51-a308-57755c72ca2b.cpp b/swap/merge/7d7e968d-0849-4d51-a308-57755c72ca2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3ec921aaff3bf2ff3fb569d0bd8051d434fd303c --- /dev/null +++ b/swap/merge/7d7e968d-0849-4d51-a308-57755c72ca2b.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +ifstream fin ("swap.in"); + +ofstream fout ("swap.out"); + + + +vector h[27]; + +vector L[27]; + +int P[50010], n, cnt; + +char a[50010], b[50010]; + + + +void read() + +{ + + int i, j, ok; + + fin >> a >> b; + + n = strlen(a); + + + + for (i=n; i>=1; i--) + + { + + a[i] = a[i-1]; + + b[i] = b[i-1]; + + } + + + + for (i=0; i<=26; i++) + + { + + for (j=1; j<=n; j++) + + { + + if (b[j] - 'a' == i) + + h[i].push_back(j); + + if (a[j] - 'a' == i) + + L[i].push_back(j); + + } + + } + + + + ok = 1; + + for (i=0; i<=26 && ok==1; i++) + + if (h[i].size() != L[i].size()) + + ok = 0; + + + + if (ok == 0) + + { + + fout << "-1\n"; + + exit(0); + + } + + + + for (i=n; i>=1; i--) + + { j = a[i]-'a'; + + P[i] = h[j][h[j].size()-1]; + + h[j].pop_back(); + + } + +} + + + +void merge(int st, int m, int dr) + +{ + + int Q[50009], i, j, k; + + i = st; + + j = m+1; + + k = 0; + + while (i <= m && j <= dr) + + { + + if (P[i] <= P[j]) + + Q[++k] = P[i++]; + + else + + { + + Q[++k] = P[j++]; + + cnt += (m - i + 1); + + } + + } + + while (i <= m) + + Q[++k] = P[i++]; + + while (j <= dr) + + Q[++k] = P[j++]; + + + + j = st; + + for (i = 1; i <= k; ++i) + + P[j++] = Q[i]; + +} + + + +void Interschimba(int i, int j) + +{ + + if (P[i] > P[j]) + + { + + cnt++; + + swap(P[i], P[j]); + + } + +} + + + +void MergeSort(int st, int dr) + +{ + + int m, i; + + if (dr - st <= 1) Interschimba(st, dr); + + else + + { + + m = (st + dr) / 2; + + MergeSort(st, m); + + MergeSort(m + 1, dr); + + merge(st, m, dr); + + } + +} + + + +int main () + +{ + + read(); + + MergeSort(1,n); + + fout << cnt << "\n"; + + fin.close(); + + fout.close(); + + return 0; + +} diff --git a/swap/merge/7eada339-6961-49dd-9ba8-2399b97c42b2.cpp b/swap/merge/7eada339-6961-49dd-9ba8-2399b97c42b2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28355501b11524b522d12c542f6188a90b4d22b6 --- /dev/null +++ b/swap/merge/7eada339-6961-49dd-9ba8-2399b97c42b2.cpp @@ -0,0 +1,217 @@ +#include +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/merge/82ce9e0a-0c19-11ec-bbf8-2cf05d84bfc2.cpp b/swap/merge/82ce9e0a-0c19-11ec-bbf8-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d5aa11a6ca467c890d9fcc4ab6bc042f419ef91a --- /dev/null +++ b/swap/merge/82ce9e0a-0c19-11ec-bbf8-2cf05d84bfc2.cpp @@ -0,0 +1,191 @@ +#include +#include + + +using namespace std; + +int lit[50001], poz[30][50001],l,nr2[30],nr1[30],v[50001],x,u,ok; + +char s1[50001],s2[50001]; + +long long s; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +void citire() + +{ + + fin>>s1>>s2; + +} + + + +void permutare() + +{ + + l=strlen(s2); + + for(int i=0;i +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50001; + +int n,nr,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); sort(m+1,dr); merge(st,dr,m); + + } + +} + +int main() + +{ f>>a>>b; n=strlen(a); + + int i,e=0; + + for(i=0;i +#include +#include +#include + + +const int DIM = 50100; + +const int EXP = 26; + +using namespace std; + + + +int N, ok, Aux[DIM]; long long sol; char A[DIM], B[DIM]; + +vector L[EXP]; int P[DIM], S1[EXP], S2[EXP], V[DIM]; + + + +void merge_sort( int left, int right ) { + + if( left == right ) return; + + + + int middle = left + ( right - left ) / 2; + + int index1, index2 = middle, pos = left; + + + + merge_sort( left, middle ); + + merge_sort( middle + 1, right ); + + + + for( index1 = left; index1 <= middle; index1 ++ ) { + + while( index2 < right && P[index1] > P[index2 + 1] ) + + index2 ++; + + sol += index2 - middle; + + } + + + + index1 = left; index2 = middle + 1; + + while( index1 <= middle || index2 <= right ) { + + if( index1 > middle ) Aux[pos++] = P[index2++]; else + + if( index2 > right ) Aux[pos++] = P[index1++]; else + + if( P[index1] <= P[index2] ) + + Aux[pos++] = P[index1++]; + + else + + Aux[pos++] = P[index2++]; + + } + + + + for( int i = left; i <= right; i ++ ) + + P[i] = Aux[i]; + + + + return; + +} + + + +int main () { + + + + freopen( "swap.in" , "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + scanf( "%s %s", A + 1, B + 1 ); + + N = strlen( A + 1 ); + + + + for( int i = 1; i <= N; i ++ ) { + + S1[A[i] - 'a'] ++; + + S2[B[i] - 'a'] ++; + + V[i] = S1[A[i] - 'a']; + + L[B[i] - 'a'].push_back( i ); + + } + + + + ok = 1; + + for( int i = 0; i < 26; i ++ ) + + if( S1[i] != S2[i] ) ok = 0; + + + + if( ok == 0 ) + + printf( "-1\n" ); + + else { + + for( int i = 1; i <= N; i ++ ) + + P[i] = L[A[i] - 'a'][V[i] - 1]; + + + + merge_sort( 1, N ); + + printf( "%lld\n", sol ); + + } + + + + return 0; + +} diff --git a/swap/merge/82ce9e0d-0c19-11ec-9352-2cf05d84bfc2.cpp b/swap/merge/82ce9e0d-0c19-11ec-9352-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa6466720d44c4b54151cff605d4e794b26b2d9 --- /dev/null +++ b/swap/merge/82ce9e0d-0c19-11ec-9352-2cf05d84bfc2.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + + + +int N,v[50005],b[50005],c[50005],sol,L[30][50000]; + +char s1[50005],s2[50005]; + + + +inline void Read() + +{ + + int i,n; + + ifstream fin("swap.in"); + + fin.getline(s1,50005);fin.getline(s2,50005); + + n=strlen(s2); + + for(i=n-1;i>=0;i--) + + L[s2[i]-'a'][++L[s2[i]-'a'][0]]=i; + + + + n=strlen(s1); + + for(i=0;i=0) + + MergeSort(v,1,N); + + Write(); + + return 0; + +} diff --git a/swap/merge/82ce9e0e-0c19-11ec-9ee2-2cf05d84bfc2.cpp b/swap/merge/82ce9e0e-0c19-11ec-9ee2-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..be0a14251742e1fa14a92527d1f9c20d09da8b66 --- /dev/null +++ b/swap/merge/82ce9e0e-0c19-11ec-9ee2-2cf05d84bfc2.cpp @@ -0,0 +1,284 @@ +#include +#include +#include + + +using namespace std; + + + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + + + +const int NMAX = 50005; + + + +int i, n, k, j,contor,st,dr,x,y,m; + +string a, b; + +vector frecB[30], frecA[30]; + +int v[NMAX]; + +long long int sol; + + + +void merge(int a[], int stanga, int mij, int dreapta) + +{ + + int b[500005]; + + int x , y , k; + + k = stanga; + + x = stanga; + + y = mij + 1; + + int contor = 0; + + while(x <= mij && y <= dreapta) + + { + + if(a[x] < a[y]) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + else if(a[y] < a[x]) + + { + + b[k++] = a[y]; + + y++; + + contor++; + + } + + else + + { + + sol = sol + contor; + + contor++; + + b[k++] = a[x]; + + b[k++] = a[y]; + + x++; + + y++; + + } + + } + + while(x <= mij) + + { + + b[k++] = a[x]; + + x++; + + sol = sol + contor; + + } + + while(y <= dreapta) + + { + + b[k++] = a[y]; + + y++; + + } + + for(i = stanga; i <= dreapta; i++) + + { + + a[i] = b[i]; + + } + + + + + +} + + + +void mergeSort(int a[], int stanga, int dreapta) + +{ + + if(stanga < dreapta) + + { + + int mij = (stanga + dreapta) / 2; + + mergeSort(a, stanga, mij); + + mergeSort(a, mij + 1, dreapta); + + merge(a, stanga, mij, dreapta); + + } + + + +} + + + + + +int main() + +{ + + fin >> a >> b; + + + + n = a.length(); + + a = ' ' + a; + + b = ' ' + b; + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + frecA[letter].push_back(i); + + + + letter = b[i] - 'a'; + + frecB[letter].push_back(i); + + + + } + + bool ok = true; + + for(i = 0; i <= 25; i++) + + { + + if(frecB[i].size() != frecA[i].size()) + + { + + ok = false; + + } + + + + for(j = 0; j < frecB[i].size(); j++) + + { + + + + } + + + + } + + if(ok == false) + + { + + fout <<"-1\n"; + + return 0; + + } + + else + + { + + for(i = 0; i <= 25; i++) + + { + + frecA[i].clear(); + + } + + for(i = 1; i <= n; i++) + + { + + int letter = a[i] - 'a'; + + int nr = frecA[letter].size(); + + v[i] = frecB[letter][nr]; + + frecA[letter].push_back(i); + + + + + + + + } + + sol = 0; + + mergeSort(v, 1, n); + + fout << sol << "\n"; + + for(i = 1; i <= n; i++) + + { + + + + } + + + + } + + + +} diff --git a/swap/merge/837bd2d0-744b-49de-ae52-8ae0871dcfaa.cpp b/swap/merge/837bd2d0-744b-49de-ae52-8ae0871dcfaa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0aa6466720d44c4b54151cff605d4e794b26b2d9 --- /dev/null +++ b/swap/merge/837bd2d0-744b-49de-ae52-8ae0871dcfaa.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + + + +int N,v[50005],b[50005],c[50005],sol,L[30][50000]; + +char s1[50005],s2[50005]; + + + +inline void Read() + +{ + + int i,n; + + ifstream fin("swap.in"); + + fin.getline(s1,50005);fin.getline(s2,50005); + + n=strlen(s2); + + for(i=n-1;i>=0;i--) + + L[s2[i]-'a'][++L[s2[i]-'a'][0]]=i; + + + + n=strlen(s1); + + for(i=0;i=0) + + MergeSort(v,1,N); + + Write(); + + return 0; + +} diff --git a/swap/merge/845aea0b-c5b5-43b6-aba9-94d20c818635.cpp b/swap/merge/845aea0b-c5b5-43b6-aba9-94d20c818635.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6362261faf1c7814fc9eed488117d70815fe9ca --- /dev/null +++ b/swap/merge/845aea0b-c5b5-43b6-aba9-94d20c818635.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include + + + + +char buffer[2 * 50000 + 2]; +std::vector position[26]; +int start[26]; + +int P[50000]; +int tmp[50000]; + +int n; + +inline int mergeCount(int left, int mid, int right) { + int invCount = 0; + int i, j, k; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] <= P[j]) { + tmp[k++] = P[i++]; + } else { + tmp[k++] = P[j++]; + invCount += (mid - i + 1); + } + } + while (i <= mid) { + tmp[k++] = P[i++]; + } + while (j <= right) { + tmp[k++] = P[j++]; + } + for (i = left; i <= right; i++) + P[i] = tmp[i]; + return invCount; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) / 2; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, mid, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int ptr = 0; + int character; + + fread(buffer, 1, 2 * 50000 + 2, f); + fclose(f); + + do { + character = buffer[ptr] - 'a'; + position[character].emplace_back(ptr); + } while (isalpha(buffer[++ptr])); + + ptr++; + n = 0; + + do { + character = buffer[ptr] - 'a'; + if (start[character] < (int) position[character].size()) { + P[n++] = position[character][start[character]++]; + } else { + n = -1; + } + } while ((~n) && (isalpha(buffer[++ptr]))); + + f = fopen("swap.out", "w"); + if (~n) { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } else { + fputs("-1\n", f); + } + fclose(f); + return 0; +} diff --git a/swap/merge/86bcde6b-c8e8-4f4b-9990-80da68222a4d.cpp b/swap/merge/86bcde6b-c8e8-4f4b-9990-80da68222a4d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04177cb97cb34ade0f74448185950f06862d4b61 --- /dev/null +++ b/swap/merge/86bcde6b-c8e8-4f4b-9990-80da68222a4d.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include + + +using namespace std; + + + +int N,v[50005],b[50005],c[50005],sol,L[30][50000]; + +char s1[50005],s2[50005]; + + + +inline void Read() + +{ + + int i,n; + + ifstream fin("swap.in"); + + fin.getline(s1,50005);fin.getline(s2,50005); + + n=strlen(s2); + + for(i=n-1;i>=0;i--) + + L[s2[i]-'a'][++L[s2[i]-'a'][0]]=i; + + + + n=strlen(s1); + + for(i=0;i=0) + + MergeSort(v,1,N); + + Write(); + + return 0; + +} diff --git a/swap/merge/8c4d7aaa-5169-4e1f-aec6-22fe65a623f3.cpp b/swap/merge/8c4d7aaa-5169-4e1f-aec6-22fe65a623f3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04177cb97cb34ade0f74448185950f06862d4b61 --- /dev/null +++ b/swap/merge/8c4d7aaa-5169-4e1f-aec6-22fe65a623f3.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/8f430629-2c40-452e-b202-2c66294f31be.cpp b/swap/merge/8f430629-2c40-452e-b202-2c66294f31be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8150da2685891f8fdbcf6687f76322383d8b094f --- /dev/null +++ b/swap/merge/8f430629-2c40-452e-b202-2c66294f31be.cpp @@ -0,0 +1,128 @@ +#include + + + + + + +char a[50002],b[50002]; + +int lita[27][50002],litb[27][50002]; + +int P[50002],B[50002]; + +int N,nr,ok; + + + +void merge(int l,int r){ + + int m=l+(r-l)/2,aux; + + if( l==r ) return; + + if( l==r-1 ){ + + if( P[l]>P[r] ) ++nr, aux=P[l],P[l]=P[r],P[r]=aux; + + return; + + } + + + + merge(l,m); + + merge(m+1,r); + + + + int i,j; + + B[0]=0; + + for(i=l,j=m+1; i<=m && j<=r; ) + + if( P[i] <= P[j] ) B[++B[0]]=P[i++]; + + else B[++B[0]]=P[j++], nr+=m-i+1; + + for(; i<=m; ++i) B[++B[0]]=P[i]; + + for(; j<=r; ++j) B[++B[0]]=P[j]; + + + + for(i=l;i<=r;++i) P[i]=B[i-l+1]; + + + +} + + + +void precal_p(){ + + int i,j; + + for(i=0;i<27;++i) + + if( lita[i][0] != litb[i][0] ){ + + ok=0; return; + + } + + else + + for(j=1;j<=lita[i][0];++j) + + P[lita[i][j]]=litb[i][j]; + +} + + + +int main(){ + + int i; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s%s",a+1,b+1); + + for(i=1; a[i]!='\0' && a[i]!='\n'; ) ++i; + + N=i-1; + + + + for(i=1;i<=N;++i){ + + lita[a[i]-'a'][++lita[a[i]-'a'][0]]=i; + + litb[b[i]-'a'][++litb[b[i]-'a'][0]]=i; + + } + + + + ok=1; + + precal_p(); + + if( ok ) merge(1,N); + + else nr=-1; + + + + printf("%d\n",nr); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/swap/merge/8f7a0751-1775-459b-9ae9-c25db14870ea.cpp b/swap/merge/8f7a0751-1775-459b-9ae9-c25db14870ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9205f65561db012ea4daab80ba54d7ca05fbf541 --- /dev/null +++ b/swap/merge/8f7a0751-1775-459b-9ae9-c25db14870ea.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +using namespace std; + + + +const int nmax = 51200; + +int seq[nmax], nseq[nmax]; + + + +int go(int lo, int hi) { + + if (lo >= hi) return 0; + + + + int mid = (lo + hi) >> 1; + + int res = go(lo, mid) + go(mid+1, hi); + + + + int i = lo, j = mid+1, k = lo; + + while (i <= mid && j <= hi) { + + if (seq[i] <= seq[j]) { + + nseq[k++] = seq[i++]; + + } else { + + nseq[k++] = seq[j++]; + + res += mid + 1 - i; + + } + + } + + while (i <= mid) { + + nseq[k++] = seq[i++]; + + } + + while (j <= hi) { + + nseq[k++] = seq[j++]; + + } + + + + for (i = lo; i <= hi; ++i) seq[i] = nseq[i]; + + return res; + +} + + + +int main() { + + freopen("swap.in", "rt", stdin); + + freopen("swap.out", "wt", stdout); + + + + char s1[nmax], s2[nmax]; + + fgets(s1, 50001, stdin); + + fgets(s2, 50001, stdin); + + + + int N = strlen(s1); + + if (strlen(s2) != N) { + + printf("-1\n"); + + return 0; + + } + + + + deque pos[32]; + + for (int i = 0; i < N; ++i) + + pos[s1[i]-'a'].push_back(i); + + + + for (int i = 0; i < N; ++i) { + + int ind = s2[i]-'a'; + + if (pos[ind].empty()) { + + printf("-1\n"); + + return 0; + + } + + seq[i] = pos[ind].front(); + + pos[ind].pop_front(); + + } + + + + printf("%d\n", go(0, N-1)); + + + + return 0; + +} diff --git a/swap/merge/8ff3dfd8-7a06-4f43-8a32-def7faad5b91.cpp b/swap/merge/8ff3dfd8-7a06-4f43-8a32-def7faad5b91.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec4b1e6d498fe7ec83c8a619c8429332d614f791 --- /dev/null +++ b/swap/merge/8ff3dfd8-7a06-4f43-8a32-def7faad5b91.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +using namespace std; + + + +int f[1001], INV1 = 0, INV2 = 0, pos[201][50001], d[50001], LE[50001], C[50001]; + +char a[50002], b[50002]; + +int c[50002]; + +inline void Merge(int st, int mij, int dr){ + + int i = st, j = mij + 1, NR = 0; + + while(i <= mij && j <= dr){ + + if(d[i] > d[j]){ + + c[++NR] = d[j++]; + + INV1 = INV1 + mij - i + 1; + + } + + else + + c[++NR] = d[i++]; + + } + + while(j <= dr) + + c[++NR] = d[j++]; + + while(i <= mij) + + c[++NR] = d[i++]; + + for(i = st , j = 1; i <= dr ; ++i, ++j) + + d[i] = c[j]; + +} + +inline void MergeSort(int st, int dr){ + + if(st >= dr) return ; + + MergeSort(st, (st + dr) / 2); + + MergeSort((st + dr) / 2 + 1, dr); + + Merge(st, (st + dr) / 2, dr); + +} + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + scanf("%s", a); + + scanf("%s", b); + + int L = strlen(a); + + for(int i = 0 ; i < L ; ++i) + + ++f[a[i]]; + + for(int i = 0 ; i < L ; ++i){ + + if(f[b[i]] == 0) + + {printf("-1"); return 0;} + + --f[b[i]]; + + } + + for(int i = 0 ; i < L ; ++i) + + pos[a[i]][++LE[a[i]]] = i + 1; + + for(int i = 0 ; i < L ; ++i) + + d[i] = pos[b[i]][++C[b[i]]]; + + MergeSort(0, L - 1); + + printf("%d", INV1); + + return 0; + +} diff --git a/swap/merge/916f3a7e-c3f7-4f29-bfc8-7313518edc80.cpp b/swap/merge/916f3a7e-c3f7-4f29-bfc8-7313518edc80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c13c9a0e87d1af66a71d82bf642148d66d401e3 --- /dev/null +++ b/swap/merge/916f3a7e-c3f7-4f29-bfc8-7313518edc80.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/9291b407-41d7-400f-ade9-6e6a8b5db40d.cpp b/swap/merge/9291b407-41d7-400f-ade9-6e6a8b5db40d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/9291b407-41d7-400f-ade9-6e6a8b5db40d.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/936b3586-dbe8-4d2f-ac49-81a2ba3d8ab8.cpp b/swap/merge/936b3586-dbe8-4d2f-ac49-81a2ba3d8ab8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e118cf82d09be8fa8f3df499568fdd011f6b29 --- /dev/null +++ b/swap/merge/936b3586-dbe8-4d2f-ac49-81a2ba3d8ab8.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; +vector v[30]; +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + j = mid + 1; + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/96826d96-6c2c-47ec-906c-b5e601a54d0f.cpp b/swap/merge/96826d96-6c2c-47ec-906c-b5e601a54d0f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c110dfc220c299529601bcf56bb693e48fc30a --- /dev/null +++ b/swap/merge/96826d96-6c2c-47ec-906c-b5e601a54d0f.cpp @@ -0,0 +1,178 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + for(i=0;i<26;i++) + + ind[i]=0; + + + + for(pos=0;posv[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/9723af1d-271e-4814-b502-bc10c8c4b39c.cpp b/swap/merge/9723af1d-271e-4814-b502-bc10c8c4b39c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9205f65561db012ea4daab80ba54d7ca05fbf541 --- /dev/null +++ b/swap/merge/9723af1d-271e-4814-b502-bc10c8c4b39c.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +using namespace std; + + + +const int nmax = 51200; + +int seq[nmax], nseq[nmax]; + + + +int go(int lo, int hi) { + + if (lo >= hi) return 0; + + + + int mid = (lo + hi) >> 1; + + int res = go(lo, mid) + go(mid+1, hi); + + + + int i = lo, j = mid+1, k = lo; + + while (i <= mid && j <= hi) { + + if (seq[i] <= seq[j]) { + + nseq[k++] = seq[i++]; + + } else { + + nseq[k++] = seq[j++]; + + res += mid + 1 - i; + + } + + } + + while (i <= mid) { + + nseq[k++] = seq[i++]; + + } + + while (j <= hi) { + + nseq[k++] = seq[j++]; + + } + + + + for (i = lo; i <= hi; ++i) seq[i] = nseq[i]; + + return res; + +} + + + +int main() { + + freopen("swap.in", "rt", stdin); + + freopen("swap.out", "wt", stdout); + + + + char s1[nmax], s2[nmax]; + + fgets(s1, 50001, stdin); + + fgets(s2, 50001, stdin); + + + + int N = strlen(s1); + + if (strlen(s2) != N) { + + printf("-1\n"); + + return 0; + + } + + + + deque pos[32]; + + for (int i = 0; i < N; ++i) + + pos[s1[i]-'a'].push_back(i); + + + + for (int i = 0; i < N; ++i) { + + int ind = s2[i]-'a'; + + if (pos[ind].empty()) { + + printf("-1\n"); + + return 0; + + } + + seq[i] = pos[ind].front(); + + pos[ind].pop_front(); + + } + + + + printf("%d\n", go(0, N-1)); + + + + return 0; + +} diff --git a/swap/merge/972eddbd-0a60-42b3-a8a2-7587717d6222.cpp b/swap/merge/972eddbd-0a60-42b3-a8a2-7587717d6222.cpp new file mode 100644 index 0000000000000000000000000000000000000000..28355501b11524b522d12c542f6188a90b4d22b6 --- /dev/null +++ b/swap/merge/972eddbd-0a60-42b3-a8a2-7587717d6222.cpp @@ -0,0 +1,217 @@ +#include +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/975bf592-4bf2-46cb-8412-1388ee5ffc21.cpp b/swap/merge/975bf592-4bf2-46cb-8412-1388ee5ffc21.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9ef395385b2857fa2ceedd42b3e6e28ebb0aef40 --- /dev/null +++ b/swap/merge/975bf592-4bf2-46cb-8412-1388ee5ffc21.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + + +const int DIM = 50100; + +const int EXP = 26; + +using namespace std; + + + +int N, ok, Aux[DIM]; long long sol; char A[DIM], B[DIM]; + +vector L[EXP]; int P[DIM], S1[EXP], S2[EXP], V[DIM]; + + + +void merge_sort( int left, int right ) { + + if( left == right ) return; + + + + int middle = left + ( right - left ) / 2; + + int index1, index2 = middle, pos = left; + + + + merge_sort( left, middle ); + + merge_sort( middle + 1, right ); + + + + for( index1 = left; index1 <= middle; index1 ++ ) { + + while( index2 < right && P[index1] > P[index2 + 1] ) + + index2 ++; + + sol += index2 - middle; + + } + + + + index1 = left; index2 = middle + 1; + + while( index1 <= middle || index2 <= right ) { + + if( index1 > middle ) Aux[pos++] = P[index2++]; else + + if( index2 > right ) Aux[pos++] = P[index1++]; else + + if( P[index1] <= P[index2] ) + + Aux[pos++] = P[index1++]; + + else + + Aux[pos++] = P[index2++]; + + } + + + + for( int i = left; i <= right; i ++ ) + + P[i] = Aux[i]; + + + + return; + +} + + + +int main () { + + + + freopen( "swap.in" , "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + scanf( "%s %s", A + 1, B + 1 ); + + N = strlen( A + 1 ); + + + + for( int i = 1; i <= N; i ++ ) { + + S1[A[i] - 'a'] ++; + + S2[B[i] - 'a'] ++; + + V[i] = S1[A[i] - 'a']; + + L[B[i] - 'a'].push_back( i ); + + } + + + + ok = 1; + + for( int i = 0; i < 26; i ++ ) + + if( S1[i] != S2[i] ) ok = 0; + + + + if( ok == 0 ) + + printf( "-1\n" ); + + else { + + for( int i = 1; i <= N; i ++ ) + + P[i] = L[A[i] - 'a'][V[i] - 1]; + + + + merge_sort( 1, N ); + + printf( "%lld\n", sol ); + + } + + + + return 0; + +} diff --git a/swap/merge/977a46ef-1ec0-42ff-8382-e23f8d8c3f94.cpp b/swap/merge/977a46ef-1ec0-42ff-8382-e23f8d8c3f94.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/977a46ef-1ec0-42ff-8382-e23f8d8c3f94.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/9882bb42-41fa-4b7f-b644-9e116f12a5c2.cpp b/swap/merge/9882bb42-41fa-4b7f-b644-9e116f12a5c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5c13c9a0e87d1af66a71d82bf642148d66d401e3 --- /dev/null +++ b/swap/merge/9882bb42-41fa-4b7f-b644-9e116f12a5c2.cpp @@ -0,0 +1,124 @@ +#include +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/990411bb-fcbc-4e03-972d-26810e565d65.cpp b/swap/merge/990411bb-fcbc-4e03-972d-26810e565d65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..526556a623ca8a4f385e60256165e35e7eb270b9 --- /dev/null +++ b/swap/merge/990411bb-fcbc-4e03-972d-26810e565d65.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int DIM = 5e4 + 7; +const int sigma = 26; + +queue pos_ch[sigma]; +int permutare[DIM]; +int arb[DIM * 4]; + +void build(int pos, int l, int r) +{ + arb[pos] = (r - l + 1); + + if(r != l) + { + int mid = (l + r) / 2; + + build(pos * 2, l, mid); + build(pos * 2 + 1, mid + 1, r); + } +} + +void update(int pos, int l, int r, int x) +{ + if(l == r) + { + arb[pos] = 0; + return ; + } + + int mid = (l + r) / 2; + + if(x <= mid) + update(pos * 2, l, mid, x); + else + update(pos * 2 + 1, mid + 1, r, x); + + arb[pos] = arb[pos * 2 + 1] + arb[pos * 2]; +} + +int query(int pos, int l, int r, int x, int y) +{ + if(x <= l && r <= y) + { + return arb[pos]; + } + + int mid = (l + r) / 2; + + int sum = 0; + + if(x <= mid) + sum += query(pos * 2, l, mid, x, y); + + if(y > mid) + sum += query(pos * 2 + 1, mid + 1, r, x, y); + + return sum; +} + +main() +{ + string s, t; + fin >> s >> t; + + int n = s.size(); + + for(int i = 0; i < n; i++) + pos_ch[s[i] - 'a'].push(i + 1); + + for(int i = 0; i < n ; i++) + { + int id = t[i] - 'a'; + + if(pos_ch[id].empty() == true) + { + fout << -1; + return 0; + } + + permutare[i + 1] = pos_ch[id].front(); + pos_ch[id].pop(); + } + + build(1, 1, n); + + int cost = 0; + + for(int i = 1; i <= n; i++) + { + cost += query(1, 1, n, 1, permutare[i]) - 1; + update(1, 1, n, permutare[i]); + } + + fout << cost; +} diff --git a/swap/merge/99490679-0c19-11ec-afb6-2cf05d84bfc2.cpp b/swap/merge/99490679-0c19-11ec-afb6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..04177cb97cb34ade0f74448185950f06862d4b61 --- /dev/null +++ b/swap/merge/99490679-0c19-11ec-afb6-2cf05d84bfc2.cpp @@ -0,0 +1,71 @@ +#include +#include +#include + + +using namespace std; + +int aint[4*50005], nxt[50005], lst[27]; +int n; +char a[50005]; + +int pozx, pozy; + +void update(int nod, int left, int right) +{ + if(left==right) + { + aint[nod]++; + return; + } + int mid=(left+right)/2; + if(pozx<=mid) update(2*nod, left, mid); + else update(2*nod+1, mid+1, right); + aint[nod]=aint[2*nod]+aint[2*nod+1]; +} + +int query(int nod, int left, int right) +{ + if(pozx<=left&&right<=pozy) + { + return aint[nod]; + } + int mid=(left+right)/2, ret=0; + if(pozx<=mid) ret+=query(2*nod, left, mid); + if(mid +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50005; + +int n,nr,i,j,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); + + sort(m+1,dr); + + merge(st,dr,m); + + } + +} + +int main() + +{ f>>a+1>>b+1; + + n=strlen(a+1); + + for(i=1;i<=n;++i) + + { ++x[a[i]-'a']; ++y[b[i]-'a']; + + L[b[i]-'a'].push_back(i); + + } + + int e=0; + + for(i=0;i<26;++i) + + { if(x[i]!=y[i]) e=1; + + x[i]=0; + + } + + if(e) g<<"-1\n"; else + + { + + for(i=1;i<=n;++i) + + { + + j=x[a[i]-'a']++; + + p[i]=L[a[i]-'a'][j]; + + } + + sort(1,n); + + g< +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/9949067d-0c19-11ec-8451-2cf05d84bfc2.cpp b/swap/merge/9949067d-0c19-11ec-8451-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4281264594f093ddd264f439950bf4d32b3f56 --- /dev/null +++ b/swap/merge/9949067d-0c19-11ec-8451-2cf05d84bfc2.cpp @@ -0,0 +1,183 @@ +#include #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i +#include +#include + + + + + + +using namespace std; + + + +int T[2*(1<<16)], V[50666], N, X[30], Rez, P, Q, Cate; + +char S1[50666], S2[50666]; + +vector A[30]; + + + +void update(int p1, int p2, int nod) + +{ + + int mij; + + + + if (P <= p1 && p2 <= Q) + + { + + T[nod]++; + + return; + + } + + + + if ((p1 <= P && P <= p2) || (p1 <= Q && Q <= p2)) + + { + + mij = (p1+p2)/2; + + update(p1, mij, 2*nod); + + update(mij+1, p2, 2*nod+1); + + } + +} + + + +void query(int x) + +{ + + int nod = (1<<16)+x; + + + + while (nod > 0) + + { + + Cate += T[nod]; + + nod = nod>>1; + + } + +} + + + +int main() + +{ + + int i, n; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + + + n = strlen(S1); + + for (i = 0; i < n; i++) + + A[ S1[i]-'a' ].push_back(i); + + + + if (strlen(S1) != n) { printf("-1\n"); return 0; } + + + + for (i = 0; i < n; i++) + + { + + if ((A[ S2[i]-'a' ].size() == 0) || (A[ S2[i]-'a' ].size() == X[ S2[i]-'a' ])) + + { printf("-1\n"); return 0; } + + V[ A[ S2[i]-'a' ][ X[ S2[i]-'a' ]++ ] ] = i; + + i = i+1; i--; + + } + + + + for (i = 0; i < n; i++) + + { + + Cate = 0; + + query(V[i]); + + Rez += (V[i]-Cate); + + P = V[i]+1; Q = n; + + update(0, (1<<16)-1, 1); + + } + + + + printf("%d\n", Rez); + + return 0; + + + +} diff --git a/swap/merge/9e15a9dc-5222-4901-9050-032339984c3d.cpp b/swap/merge/9e15a9dc-5222-4901-9050-032339984c3d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..664ad7f9256309677d19dfef7ecfcc764c183527 --- /dev/null +++ b/swap/merge/9e15a9dc-5222-4901-9050-032339984c3d.cpp @@ -0,0 +1,170 @@ +#include #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A+1, 50001, stdin); + + fgets( B+1, 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/9f5f9a7e-2f41-465b-8a0b-829c2f714813.cpp b/swap/merge/9f5f9a7e-2f41-465b-8a0b-829c2f714813.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d67da457069e37ba2137a23a4d659274bcbb2dc --- /dev/null +++ b/swap/merge/9f5f9a7e-2f41-465b-8a0b-829c2f714813.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +using namespace std; + + + +const int MAX_N = 50010; + +const int MAX_L = 28; + + + +int f[2][MAX_L], d[MAX_N], c[MAX_N], sol; + +char a[MAX_N], b[MAX_N]; + +vector v[MAX_L]; + + + +void merge(int st, int dr) + +{ + + int i, j, k; + + int mid = st + (dr - st) / 2; + + if (st >= dr) return; + + + + merge(st, mid); + + merge(mid + 1, dr); + + + + i = st; + + j = mid + 1; + + k = st - 1; + + + + while (i <= mid && j <= dr) + + { + + if (d[i] < d[j]) c[++k] = d[i++]; + + else + + { + + sol += (mid - i + 1); + + c[++k] = d[j++]; + + } + + } + + + + if (i <= mid) + + for (; i <= mid; ++i) c[++k] = d[i]; + + else for (i = j; i <= dr; ++i) c[++k] = d[i]; + + + + for (i = st; i <= dr; ++i) d[i] = c[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s\n", a + 1); + + scanf("%s\n", b + 1); + + int l = strlen(a + 1); + + + + for (int i = 1; i <= l; ++i) + + { + + ++f[1][b[i] - 'a']; + + v[b[i] - 'a'].push_back(i); + + } + + + + for (int i = 1; i <= l; i++) + + { + + ++f[0][a[i] - 'a']; + + if (f[0][a[i] - 'a'] > f[1][a[i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + d[i] = v[a[i] - 'a'][f[0][a[i] - 'a'] - 1]; + + } + + + + merge(1, l); + + + + printf("%d\n", sol); + +} diff --git a/swap/merge/9fb39eb0-b9c9-43e1-9105-035a4150d615.cpp b/swap/merge/9fb39eb0-b9c9-43e1-9105-035a4150d615.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2c110dfc220c299529601bcf56bb693e48fc30a --- /dev/null +++ b/swap/merge/9fb39eb0-b9c9-43e1-9105-035a4150d615.cpp @@ -0,0 +1,178 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[200000],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,200000*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + for(i=0;i<26;i++) + + ind[i]=0; + + + + for(pos=0;posv[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/a013070c-cddb-4bbc-8de7-f4d078a6e953.cpp b/swap/merge/a013070c-cddb-4bbc-8de7-f4d078a6e953.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cbf9ad120b351ee7dc3669ce1dcd5cc7409f4e84 --- /dev/null +++ b/swap/merge/a013070c-cddb-4bbc-8de7-f4d078a6e953.cpp @@ -0,0 +1,204 @@ +#include +#include +#include +#include + + +using namespace std; + + + +const int maxn = 50002; + +const int maxa = 27; + + + +ifstream in("swap.in"); + +ofstream out("swap.out"); + + + +string a, b; + + + +int poz1[maxa]; + +vector poz2[maxa]; + + + +int perm[maxn]; + + + +void read() + +{ + + in >> a >> b; + +} + + + +int v1[maxa]; + +int v2[maxa]; + +int valid() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + ++v1[ a[i] - 'a' ], ++v2[ b[i] - 'a' ]; + + + + for ( int i = 0; i <= 'z' - 'a'; ++i ) + + if ( v1[i] != v2[i] ) + + return 0; + + + + return 1; + +} + + + +void init() + +{ + + for ( int i = 0; i < a.size(); ++i ) + + poz2[ b[i] - 'a' ].push_back(i); + + + + for ( int i = 0; i < a.size(); ++i ) + + perm[i + 1] = 1 + poz2[ a[i] - 'a' ][ poz1[ a[i] - 'a' ]++ ]; + +} + + + +int answ; + + + +int tmp[maxn]; + +void merge(int st, int m, int dr) + +{ + + int i = st; + + int j = m+1; + + int k = 0; + + + + while ( i <= m && j <= dr ) + + if ( perm[i] <= perm[j] ) + + tmp[++k] = perm[i++]; + + else + + { + + answ += m - i + 1; + + tmp[++k] = perm[j++]; + + } + + + + while ( i <= m ) + + tmp[++k] = perm[i++]; + + while ( j <= dr ) + + tmp[++k] = perm[j++]; + + + + k = 0; + + for ( int i = st; i <= dr; ++i ) + + perm[i] = tmp[++k]; + +} + + + +void sort(int st, int dr) + +{ + + if ( st < dr ) + + { + + int m = (st + dr) >> 1; + + + + sort(st, m); + + sort(m+1, dr); + + + + merge(st, m, dr); + + } + +} + + + +int main() + +{ + + read(); + + + + if ( !valid() ) + + { + + out << -1 << endl; + + return 0; + + } + + + + init(); + sort(1, a.size()); + + out << answ << endl; + + + + return 0; + +} diff --git a/swap/merge/a0382481-80b7-4f29-ab16-c00cfd116de0.cpp b/swap/merge/a0382481-80b7-4f29-ab16-c00cfd116de0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2f8d78f5148a2fc2883e3e7689fdaa6fe1883e0 --- /dev/null +++ b/swap/merge/a0382481-80b7-4f29-ab16-c00cfd116de0.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i, n, perm[50010], lastap[50010], poz[27][50010], ind[27], vtmp[50010], nr, k, ap1[27], ap2[27]; + +char a[50010], b[50010]; + + + +void interc(int st, int mid, int dr) + +{ + + int i=0, j=0, k=0; + + for (i=st, j=mid+1; i<=mid && j<=dr; ) { + + if (perm[i] > perm[j]) { + + vtmp[++k]=perm[j]; + + j++; + + nr=nr+mid-i+1; + + } + + else { + + vtmp[++k]=perm[i]; + + i++; + + } + + } + + for (; i<=mid; i++) + + vtmp[++k]=perm[i]; + + for (; j<=dr; j++) + + vtmp[++k]=perm[j]; + + k=0; + + for (i=st; i<=dr; i++) + + perm[i]=vtmp[++k]; + +} + +void divimp(int st, int dr) + +{ + + if (dr-st<=1) { + + if (perm[st] > perm[dr]) { + + nr++; + + int tmp=perm[st]; + + perm[st]=perm[dr]; + + perm[dr]=tmp; + + } + + } + + else { + + int mid=(st+dr)/2; + + divimp(st, mid); + + divimp(mid+1, dr); + + interc(st, mid, dr); + + } + +} + + + +int main() + +{ + + f.get(a, 50001); + + f.get(); + + f.get(b, 50001); + + n=strlen(a); + + for (i=0; i +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/a29fda21-739c-4bb5-849c-fdf274b00fef.cpp b/swap/merge/a29fda21-739c-4bb5-849c-fdf274b00fef.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4d710932b647c4881758b8da44d90d23734a4ae6 --- /dev/null +++ b/swap/merge/a29fda21-739c-4bb5-849c-fdf274b00fef.cpp @@ -0,0 +1,137 @@ +#include +#include +struct nod{ + +long int info; + +nod *next; + +}; + +nod *prima[30],*ultima[30],*primb[30],*ultimb[30],*pa,*pb; + +long int la,lb,i,fra[30],frb[30],slot,p[50002],q[50002],sol; + +char a[50002],b[50002],*aa,*bb; + +void punea(long int ii,long int jj); + +void puneb(long int ii,long int jj); + +void mergesort(long int ll,long int rr); + +void merge(long int p1,long int p2,long int p3); + +int main() + +{ + + FILE *f,*g;f=fopen("swap.in","r");g=fopen("swap.out","w"); + + aa=&a[0];bb=&b[0]; + + fscanf(f,"%s",aa);fscanf(f,"%s",bb);la=strlen(aa);lb=strlen(b); + + if(la!=lb){fprintf(g,"-1\n");fcloseall();return 0;} + + for(i=0;iinfo]=pb->info;pa=pa->next;pb=pb->next;}} + + mergesort(1,la); + + fprintf(g,"%ld\n",sol); + + fcloseall(); + + return 0; + +} + +void punea(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!prima[ii]){prima[ii]=ploc;ultima[ii]=ploc;return;} + + ultima[ii]->next=ploc;ultima[ii]=ploc; + +} + +void puneb(long int ii,long int jj) + +{ + + nod *ploc; + + ploc=new nod; + + ploc->info=jj; + + ploc->next=0; + + if(!primb[ii]){primb[ii]=ploc;ultimb[ii]=ploc;return;} + + ultimb[ii]->next=ploc;ultimb[ii]=ploc; + +} + +void mergesort(long int ll,long int rr) + +{ long int mm,aux; + + if(rr==ll) return; + + if(rr==ll+1) + + { if(p[ll]>p[rr]) {sol++;aux=p[ll];p[ll]=p[rr];p[rr]=aux;} + + return; + + } + + mm=(ll+rr)/2; + + mergesort(ll,mm); + + mergesort(mm+1,rr); + + merge(ll,mm,rr); + +} + +void merge(long int p1,long int p2,long int p3) + +{ long int ii,pp,pp1,pp2,ramase; + + for(ii=p1;ii<=p3;ii++)q[ii]=p[ii]; + + pp=p1;pp1=p1;pp2=p2+1;ramase=p2-p1+1; + + while(pp1<=p2&&pp2<=p3) + + { if(q[pp1] +#include +#include + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int n,i,j,u,nrn,rez,pfrz[250000],vs[250000]; +string s; +vector pos[300]; + +void make(int nd,int st,int dr) +{ + if(st==dr) + { + pfrz[++nrn]=nd; + return; + } + int md=st+(dr-st)/2; + make(2*nd,st,md); + make(2*nd+1,md+1,dr); +} +int take(int nd,int sum) +{ + if(nd==1) return sum; + if(nd/2*2+1!=nd) vs[nd/2*2+1]++; + return take(nd/2,sum+vs[nd]); +} +int main() { + + getline(fin,s); + n=(int)s.size(); + for(i=n-1;i>=0;i--) + pos[s[i]-96].push_back(i); + + make(1,1,n); + rez=-n; + getline(fin,s); + for(i=0;i +#include + + +using namespace std; + +int lit[50001], poz[30][50001],l,nr2[30],nr1[30],v[50001],x,u,ok; + +char s1[50001],s2[50001]; + +long long s; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +void citire() + +{ + + fin>>s1>>s2; + +} + + + +void permutare() + +{ + + l=strlen(s2); + + for(int i=0;i +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/a94e5316-5a67-4317-a834-7afb560ce86a.cpp b/swap/merge/a94e5316-5a67-4317-a834-7afb560ce86a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6c6a1a5d715c26f037c645fd3f3152ce834baa44 --- /dev/null +++ b/swap/merge/a94e5316-5a67-4317-a834-7afb560ce86a.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout ("swap.out"); +vector< queue > x; + +vector arb; + + + +char a[50001]; + +int n, nr[50001], rez; + + + +void createarb(int node, int st, int dr) + +{ + + if (st == dr) + + { + + arb[node] = 1; + + return; + + } + + + + int mijl = (st + dr) / 2; + + createarb(2 * node, st, mijl); + + createarb(2 * node + 1, mijl + 1, dr); + + arb[node] = arb[2 * node] + arb[2 * node + 1]; + +} + + + +void setpos(int node,int st, int dr, int pos) + +{ + + if (st == dr) + + { + + arb[node] = 0; + + return; + + } + + + + int mijl = (st + dr) /2; + + if (pos <= mijl)setpos(2 * node, st, mijl, pos); + + else setpos(2 * node + 1, mijl + 1, dr, pos); + + arb[node] = arb[2 * node] + arb[2 * node + 1]; + +} + +int query(int node,int st, int dr, int L, int R) + +{ + + if (L <= st && dr <= R) return arb[node]; + + + + int mijl = (st + dr) / 2, s = 0; + + if (L <= mijl)s += query(2 * node, st, mijl, L, R); + + if (R > mijl) s += query(2 * node + 1, mijl + 1, dr, L, R); + + return s; + +} + +int main() + +{ + + cin >> a; + + n = strlen(a); + + + + x = vector< queue >(31); + + + + for (int i = 0; i < n; ++ i) + + x[a[i] - 'a'].push(i); + + + + cin >> a; + + n = strlen(a); + + for (int i = 0; i < n; ++ i) + + nr[a[i] - 'a'] ++; + + + + for (int i = 0; i < 31; ++ i) + + if (nr[i] != x[i].size()) + + { + + cout << -1; + + return 0; + + } + + + + arb = vector(4 * n + 1); + + + + createarb(1, 1, n); + + for (int i = 0, pos; i < n; ++ i) + + { + + pos = x[a[i] - 'a'].front() + 1; + + x[a[i] - 'a'].pop(); + + setpos(1, 1, n, pos); + + rez += query(1, 1, n, 1, pos); + + } + + + + cout << rez; + + return 0; + +} diff --git a/swap/merge/ab819737-3894-4f9c-b95d-2a0b195db4e4.cpp b/swap/merge/ab819737-3894-4f9c-b95d-2a0b195db4e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d7100397a01fdd21a295fcd2a69b822b406a942 --- /dev/null +++ b/swap/merge/ab819737-3894-4f9c-b95d-2a0b195db4e4.cpp @@ -0,0 +1,137 @@ +#include +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/ac9dd209-9642-4d98-988d-55ff7b6a8617.cpp b/swap/merge/ac9dd209-9642-4d98-988d-55ff7b6a8617.cpp new file mode 100644 index 0000000000000000000000000000000000000000..626def968c02a7b936ab34fafaa7abb6e853503a --- /dev/null +++ b/swap/merge/ac9dd209-9642-4d98-988d-55ff7b6a8617.cpp @@ -0,0 +1,157 @@ +#include +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/ae72892a-f5a7-408a-8c14-ed9b303ac0c7.cpp b/swap/merge/ae72892a-f5a7-408a-8c14-ed9b303ac0c7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e118cf82d09be8fa8f3df499568fdd011f6b29 --- /dev/null +++ b/swap/merge/ae72892a-f5a7-408a-8c14-ed9b303ac0c7.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +using namespace std; + + + + + +char a[50010],b[50010]; + +int t[3*50010]; + +int x,y,rez; + +int lac[26],cate[26],n; + +vector unde[26]; + +inline int make_hask(char c) + +{ + + return c-'a'; + +} + +void update(int p,int u,int i) + +{ + + if(p>u) + + return; + + if(x<=p && u<=y) + + { + + ++t[i]; + + return; + + } + + if(p==u) + + return; + + int mij=(p+u)>>1; + + if(x<=mij) + + update(p,mij,i<<1); + + if(miju) + + return; + + if(p==u && p==x) + + { + + y+=t[i]; + + return; + + } + + if(p<=x && x<=u) + + y+=t[i]; + + int mij=(p+u)>>1; + + if(x<=mij) + + querry(p,mij,i<<1); + + else + + querry(mij+1,u,(i<<1)+1); + +} + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1,50010,stdin); + + fgets(b+1,50010,stdin); + + for(n=1; a[n]!='\n'; ++n) + + { + + int aux=make_hask(a[n]); + + unde[aux].push_back(n); + + ++cate[aux]; + + } + + --n; + + for(int i=1; b[i]!='\n'; ++i) + + { + + int aux=make_hask(b[i]); + + y=0; + + if(cate[aux]==lac[aux]) + + { + + printf("-1\n"); + + return 0; + + } + + x=unde[aux][lac[aux]++]; + + querry(1,n,1); + + rez+=x-i+y; + + y=x-1; + + x=1; + + update(1,n,1); + + } + + printf("%d\n",rez); + + return 0; + +} diff --git a/swap/merge/b0895ba2-bb3c-4d8f-9ce2-226602ead9ff.cpp b/swap/merge/b0895ba2-bb3c-4d8f-9ce2-226602ead9ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac02631102d4deaba49fe0f85e7b1ed6f19508dd --- /dev/null +++ b/swap/merge/b0895ba2-bb3c-4d8f-9ce2-226602ead9ff.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int T[2*(1<<16)], V[50666], N, X[30], Rez, P, Q, Cate; + +char S1[50666], S2[50666]; + +vector A[30]; + + + +void update(int p1, int p2, int nod) + +{ + + int mij; + + + + if (P <= p1 && p2 <= Q) + + { + + T[nod]++; + + return; + + } + + + + if ((p1 <= P && P <= p2) || (p1 <= Q && Q <= p2)) + + { + + mij = (p1+p2)/2; + + update(p1, mij, 2*nod); + + update(mij+1, p2, 2*nod+1); + + } + +} + + + +void query(int x) + +{ + + int nod = (1<<16)+x; + + + + while (nod > 0) + + { + + Cate += T[nod]; + + nod = nod>>1; + + } + +} + + + +int main() + +{ + + int i, n; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + + + n = strlen(S1); + + for (i = 0; i < n; i++) + + A[ S1[i]-'a' ].push_back(i); + + + + if (strlen(S1) != n) { printf("-1\n"); return 0; } + + + + for (i = 0; i < n; i++) + + { + + if ((A[ S2[i]-'a' ].size() == 0) || (A[ S2[i]-'a' ].size() == X[ S2[i]-'a' ])) + + { printf("-1\n"); return 0; } + + V[ A[ S2[i]-'a' ][ X[ S2[i]-'a' ]++ ] ] = i; + + i = i+1; i--; + + } + + + + for (i = 0; i < n; i++) + + { + + Cate = 0; + + query(V[i]); + + Rez += (V[i]-Cate); + + P = V[i]+1; Q = n; + + update(0, (1<<16)-1, 1); + + } + + + + printf("%d\n", Rez); + + return 0; + + + +} diff --git a/swap/merge/b255b148-a7f8-4964-8087-eaa07d9870ee.cpp b/swap/merge/b255b148-a7f8-4964-8087-eaa07d9870ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad691a5c4991cb1f390336b5ae546164c43141fa --- /dev/null +++ b/swap/merge/b255b148-a7f8-4964-8087-eaa07d9870ee.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i +#include +#include +using namespace std; + + + +const int MAX_N = 50010; + +const int MAX_L = 28; + + + +int f[2][MAX_L], d[MAX_N], c[MAX_N], sol; + +char a[MAX_N], b[MAX_N]; + +vector v[MAX_L]; + + + +void merge(int st, int dr) + +{ + + int i, j, k; + + int mid = st + (dr - st) / 2; + + if (st >= dr) return; + + + + merge(st, mid); + + merge(mid + 1, dr); + + + + i = st; + + j = mid + 1; + + k = st - 1; + + + + while (i <= mid && j <= dr) + + { + + if (d[i] < d[j]) c[++k] = d[i++]; + + else + + { + + sol += (mid - i + 1); + + c[++k] = d[j++]; + + } + + } + + + + if (i <= mid) + + for (; i <= mid; ++i) c[++k] = d[i]; + + else for (i = j; i <= dr; ++i) c[++k] = d[i]; + + + + for (i = st; i <= dr; ++i) d[i] = c[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s\n", a + 1); + + scanf("%s\n", b + 1); + + int l = strlen(a + 1); + + + + for (int i = 1; i <= l; ++i) + + { + + ++f[1][b[i] - 'a']; + + v[b[i] - 'a'].push_back(i); + + } + + + + for (int i = 1; i <= l; i++) + + { + + ++f[0][a[i] - 'a']; + + if (f[0][a[i] - 'a'] > f[1][a[i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + d[i] = v[a[i] - 'a'][f[0][a[i] - 'a'] - 1]; + + } + + + + merge(1, l); + + + + printf("%d\n", sol); + +} diff --git a/swap/merge/b32bbf8f-69f4-4d69-a15a-a264593da8aa.cpp b/swap/merge/b32bbf8f-69f4-4d69-a15a-a264593da8aa.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2e118cf82d09be8fa8f3df499568fdd011f6b29 --- /dev/null +++ b/swap/merge/b32bbf8f-69f4-4d69-a15a-a264593da8aa.cpp @@ -0,0 +1,89 @@ +#include +#include +using namespace std; + + + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i + + +using namespace std; + + + + + + + +const int MAXN = 1e5; + + + +char s[MAXN + 1], t[MAXN + 1]; + +vector poz[100]; + +int code (char c) { + + return c - 'a'; + +} + +int nr; + +int p[4 * MAXN + 1], v[4 * MAXN + 1]; + +void build (int st, int dr, int nod) { + + if (st == dr) { + + p[++nr] = nod; + + return; + + } + + int mij = (st + dr) / 2; + + build (st, mij, nod * 2); + + build (mij + 1, dr, nod * 2 + 1); + +} + + + +int take (int nod, int sum) { + + if (nod == 1) + + return sum; + + if (nod % 2 == 0) + + v[nod + 1]++; + + return take (nod / 2, sum + v[nod]); + +} + + + +int main() { + + int n, i, x; + + long long swaps; + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + scanf ("%s", &s); + + n = strlen (s); + + for (i = n - 1; i >= 0; i--) + + poz[code (s[i])].push_back (i); + + build (1, n, 1); + + scanf ("%s", &t); + + swaps = 0; + + for (i = 0; i < n; i++) { + + if (poz[code(t[i])].size () == 0) { + + printf ("-1\n"); + + return 0; + + } + + x = poz[code(t[i])].back () + 1; + + swaps += x - take (p[x], 0); + + poz[code (t[i])].pop_back (); + + } + + printf ("%lld\n", swaps - n); + + return 0; + +} diff --git a/swap/merge/b577324a-d969-4a8f-a74f-c9dedefb965e.cpp b/swap/merge/b577324a-d969-4a8f-a74f-c9dedefb965e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d769a5a6f93e6aa37dea4ad115183666d155ef24 --- /dev/null +++ b/swap/merge/b577324a-d969-4a8f-a74f-c9dedefb965e.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/b659b34b-83d2-49bc-98a4-370e4423884c.cpp b/swap/merge/b659b34b-83d2-49bc-98a4-370e4423884c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2f8d78f5148a2fc2883e3e7689fdaa6fe1883e0 --- /dev/null +++ b/swap/merge/b659b34b-83d2-49bc-98a4-370e4423884c.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i, n, perm[50010], lastap[50010], poz[27][50010], ind[27], vtmp[50010], nr, k, ap1[27], ap2[27]; + +char a[50010], b[50010]; + + + +void interc(int st, int mid, int dr) + +{ + + int i=0, j=0, k=0; + + for (i=st, j=mid+1; i<=mid && j<=dr; ) { + + if (perm[i] > perm[j]) { + + vtmp[++k]=perm[j]; + + j++; + + nr=nr+mid-i+1; + + } + + else { + + vtmp[++k]=perm[i]; + + i++; + + } + + } + + for (; i<=mid; i++) + + vtmp[++k]=perm[i]; + + for (; j<=dr; j++) + + vtmp[++k]=perm[j]; + + k=0; + + for (i=st; i<=dr; i++) + + perm[i]=vtmp[++k]; + +} + +void divimp(int st, int dr) + +{ + + if (dr-st<=1) { + + if (perm[st] > perm[dr]) { + + nr++; + + int tmp=perm[st]; + + perm[st]=perm[dr]; + + perm[dr]=tmp; + + } + + } + + else { + + int mid=(st+dr)/2; + + divimp(st, mid); + + divimp(mid+1, dr); + + interc(st, mid, dr); + + } + +} + + + +int main() + +{ + + f.get(a, 50001); + + f.get(); + + f.get(b, 50001); + + n=strlen(a); + + for (i=0; i +#include +#include + + +using namespace std; + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + +ifstream fin(iname); + +ofstream fout(oname); + +deque Q[300]; + +int i, lg, lg2, n, ANS, sol; + +int j_s1[300], j_s2[300], P[50004]; + +int H[50004 * 3]; + +char s1[50004], s2[50004]; + +inline void update (int n, int l, int r, int p, int v){ + + if (l == r){H[n] = v; return;} + + int m = (l + r) / 2, L = n << 1, R = L | 1; + + if (p <= m) update (L, l, m, p, v); + + else + + update (R, m + 1, r, p, v); + + H[n] = H[L] + H[R]; + +} + +inline int query (int n, int l, int r, int i, int j){ + + if (i <= l && r <= j) return H[n]; + + int m = (l + r) / 2; int sol = 0; + + if (i <= m) sol += query(2 * n, l, m, i, j); + + if (j > m) sol += query(2 * n + 1, m + 1, r, i, j); + + return sol; + +} + +int main() + +{ + + fin >> s1; lg = strlen(s1); + + for (i = 0; i < lg; ++i) + + ++j_s1[(int)s1[i]], + + Q[(int)s1[i]].push_back(i + 1); + + fin >> s2; lg2 = strlen(s2); + + if (lg != lg2){fout << -1 << '\n'; return 0;} + + for (i = 0; i < lg; ++i) + + { + + ++j_s2[(int)s2[i]]; + + P[++n] = Q[s2[i]][0]; + + Q[s2[i]].pop_front(); + + } + + for (i = 0; i < lg; ++i) + + { + + if (j_s1[(int)s1[i]] != j_s2[(int)s1[i]]){ + + fout << -1 << '\n'; return 0; + + } + + } + + update (1, 1, n, P[1], 1); + + for (i = 2; i <= n; ++i) + + { + + update (1, 1, n, P[i], 1); + + ANS += query (1, 1, n, P[i] + 1, n); + + } + + fout << ANS << '\n'; + + return 0; + +} diff --git a/swap/merge/b96e5fef-80f6-43b2-992c-7652a4fb71b8.cpp b/swap/merge/b96e5fef-80f6-43b2-992c-7652a4fb71b8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3145e2bba904877ed99571657e3ddc4a79c8429 --- /dev/null +++ b/swap/merge/b96e5fef-80f6-43b2-992c-7652a4fb71b8.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/ba252e38-9110-4fa9-8c54-c3c67c059b3c.cpp b/swap/merge/ba252e38-9110-4fa9-8c54-c3c67c059b3c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/ba252e38-9110-4fa9-8c54-c3c67c059b3c.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/bb3fbb95-75d6-4b73-9c18-ee3991bdfc2b.cpp b/swap/merge/bb3fbb95-75d6-4b73-9c18-ee3991bdfc2b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad691a5c4991cb1f390336b5ae546164c43141fa --- /dev/null +++ b/swap/merge/bb3fbb95-75d6-4b73-9c18-ee3991bdfc2b.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include + + +using namespace std; + + + +vector > w; + + + +int H[3*50005], v[50005], freq[2][200]; + +int n, inv = 0, sol = 0; + + + +string a, b; + + + +void query(int node, int lo, int hi, int a, int b) { + + if(a > b) return; + + if(a <= lo && hi <= b) { + + inv += H[node]; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(a <= mid) query(2*node, lo, mid, a, b); + + if(b > mid) query(2*node+1, mid+1, hi, a, b); + +} + + + +void update(int node, int lo, int hi, int pos) { + + if(lo == hi) { + + H[node]++; + + return; + + } + + + + int mid = (lo + hi) >> 1; + + if(pos <= mid) update(2*node, lo, mid, pos); + + else update(2*node+1, mid+1, hi, pos); + + + + H[node] = H[2*node] + H[2*node+1]; + +} + + + + + +int main() { + + ifstream f("swap.in"); + + ofstream g("swap.out"); + + + + getline(f, a); + + getline(f, b); + + n = a.size(); + + + + for(int i=0; i +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/bd7a64bf-ffd6-4397-98f8-21775b17b868.cpp b/swap/merge/bd7a64bf-ffd6-4397-98f8-21775b17b868.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b171ed3342ad84342fba440df8339adb0bca7943 --- /dev/null +++ b/swap/merge/bd7a64bf-ffd6-4397-98f8-21775b17b868.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +using namespace std; + +ifstream fi("swap.in"); + +ofstream fo("swap.out"); + +char A[50002],B[50002]; + +int FA[26],FB[26]; + +deque POZB[26]; + +deque :: iterator it; + +int AIB[50002],naib=50000; + + + +long long rez; + + + +void update(int poz, int v) + + + +{ + + while (poz<=naib) + + { + + AIB[poz]+=v; + + poz=poz+(poz&(-poz)); + + } + +} + + + +int f(int poz) + +{ + + int rez; + + rez=0; + + while (poz>=1) + + { + + rez=rez+AIB[poz]; + + poz=poz-(poz&(-poz)); + + } + + return rez; + +} + + + +int query(int st, int dr) + +{ + + return f(dr)-f(st-1); + +} + + + +int main() + +{ + + fi>>A+1; + + fi>>B+1; + + for (int i=1;A[i]!='\0';i++) + + FA[A[i]-'a']++; + + for (int i=1;B[i]!='\0';i++) + + FB[B[i]-'a']++; + + int t=1; + + for (int i=0;i<=25;i++) + + if (FA[i]!=FB[i]) + + t=0; + + if (t==0) + + fo<<-1; + + else + + { + + + + + + for (int i=1;B[i]!='\0';i++) + + POZB[B[i]-'a'].push_back(i); + + int n=strlen(B+1); + + for (int i=1;i<=n;i++) + + update(i,1); + + rez=0; + + for (int i=1;i<=n;i++) + + { + + int c; + + c=A[i]; + + int poz; + + poz=POZB[c-'a'].front(); + + POZB[c-'a'].pop_front(); + + update(poz,-1); + + rez=rez+query(1,poz); + + } + + fo< +#include +#include +#include +#include +#include +using namespace std; + + + +char ch1[50005],ch2[50005]; + +int poz[26][21000]; + +int us2[26],us[26]; + +int p[50005]; + +int sol; + + + +int _mergeSort(int arr[], int temp[], int left, int right); + +int merge(int arr[], int temp[], int left, int mid, int right); + + + + + + + +int mergeSort(int arr[], int array_size) + +{ + + int *temp = (int *)malloc(sizeof(int)*array_size); + + return _mergeSort(arr, temp, 0, array_size - 1); + +} + + + + + + + +int _mergeSort(int arr[], int temp[], int left, int right) + +{ + + int mid, inv_count = 0; + + if (right > left) + + { + + + + + + mid = (right + left)/2; + + + + + + + + inv_count = _mergeSort(arr, temp, left, mid); + + inv_count += _mergeSort(arr, temp, mid+1, right); + + + + + + inv_count += merge(arr, temp, left, mid+1, right); + + } + + return inv_count; + +} + + + + + + + +int merge(int arr[], int temp[], int left, int mid, int right) + +{ + + int i, j, k; + + int inv_count = 0; + + + + i = left; + + j = mid; + + k = left; + + while ((i <= mid - 1) && (j <= right)) + + { + + if (arr[i] <= arr[j]) + + { + + temp[k++] = arr[i++]; + + } + + else + + { + + temp[k++] = arr[j++]; + + + + + + inv_count = inv_count + (mid - i); + + } + + } + + + + + + + + while (i <= mid - 1) + + temp[k++] = arr[i++]; + + + + + + + + while (j <= right) + + temp[k++] = arr[j++]; + + + + + + for (i=left; i <= right; i++) + + arr[i] = temp[i]; + + + + return inv_count; + +} + + + + + +int main() + +{ + + freopen("swap.in" ,"r" ,stdin); + + freopen("swap.out","w" ,stdout); + + scanf("%s\n%s\n",&ch1,&ch2); + + + + int len2 = strlen(ch2); + + for(int i= 0 ;i< len2; ++i){ + + ++us2[ ch2[i]-'a' ]; + + poz[ ch2[i]-'a' ][us2[ch2[i]-'a'] ] = i+1; + + } + + int len = strlen(ch1); + + if(len != len2){ + + printf("-1"); + + return 0; + + } + + for(int i=0;i +#include +#include +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int v[50005], n, sol; + +char s[50005]; + +struct aint{ + + int val; + + int lazy; + +} A[200005]; + +queue q[30]; + +void build( int nod, int st, int dr ){ + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( st > dr ) + + return; + + if( st == dr ){ + + A[nod].val = st; + + A[nod].lazy = 0; + + }else{ + + int mid = ( st + dr ) >> 1; + + build( Nst, st, mid ); + + build( Ndr, mid + 1, dr ); + + } + +} + +void update( int nod, int st, int dr, int a, int b ){ + + if( a > b ) + + return; + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( st > dr ) + + return; + + if( a <= st && dr <= b ){ + + A[nod].lazy++; + + }else{ + + int mid = ( st + dr ) >> 1; + + if( a <= mid ) + + update( Nst, st, mid, a, b ); + + if( b > mid ) + + update( Ndr, mid + 1, dr, a, b ); + + } + +} + +int query( int nod, int st, int dr, int pos ){ + + int Nst = (nod<<1); + + int Ndr = (nod<<1)|1; + + if( A[nod].lazy != 0 ){ + + A[nod].val += A[nod].lazy; + + if( st != dr ){ + + A[Nst].lazy += A[nod].lazy; + + A[Ndr].lazy += A[nod].lazy; + + } + + A[nod].lazy = 0; + + } + + if( st == dr ){ + + return A[nod].val; + + }else{ + + int mid = ( st + dr ) >> 1; + + if( pos <= mid ) + + return query( Nst, st, mid, pos ); + + else + + return query( Ndr, mid + 1, dr, pos ); + + } + +} + +int main(){ + + fin >> s; + + n = strlen( s ); + + for( int i = 0; i < n; i++ ){ + + q[ s[i] - 'a' ].push( i + 1 ); + + } + + fin >> s; + + for( int i = 0; i < n; i++ ){ + + if( q[ s[i] - 'a' ].empty() ){ + + fout << "-1\n"; + + return 0; + + } + + v[i + 1] = q[ s[i] - 'a' ].front(); + + q[ s[i] - 'a' ].pop(); + + } + + build( 1, 1, n ); + + for( int i = 1; i <= n; i++ ){ + + sol += query( 1, 1, n, v[i] ) - i; + + update( 1, 1, n, 1, v[i] - 1 ); + + } + + fout << sol << "\n"; + + return 0; + +} diff --git a/swap/merge/c34af14a-1d0d-4795-b735-de885da0ee3f.cpp b/swap/merge/c34af14a-1d0d-4795-b735-de885da0ee3f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2f8d78f5148a2fc2883e3e7689fdaa6fe1883e0 --- /dev/null +++ b/swap/merge/c34af14a-1d0d-4795-b735-de885da0ee3f.cpp @@ -0,0 +1,147 @@ +#include +#include + + +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +int i, n, perm[50010], lastap[50010], poz[27][50010], ind[27], vtmp[50010], nr, k, ap1[27], ap2[27]; + +char a[50010], b[50010]; + + + +void interc(int st, int mid, int dr) + +{ + + int i=0, j=0, k=0; + + for (i=st, j=mid+1; i<=mid && j<=dr; ) { + + if (perm[i] > perm[j]) { + + vtmp[++k]=perm[j]; + + j++; + + nr=nr+mid-i+1; + + } + + else { + + vtmp[++k]=perm[i]; + + i++; + + } + + } + + for (; i<=mid; i++) + + vtmp[++k]=perm[i]; + + for (; j<=dr; j++) + + vtmp[++k]=perm[j]; + + k=0; + + for (i=st; i<=dr; i++) + + perm[i]=vtmp[++k]; + +} + +void divimp(int st, int dr) + +{ + + if (dr-st<=1) { + + if (perm[st] > perm[dr]) { + + nr++; + + int tmp=perm[st]; + + perm[st]=perm[dr]; + + perm[dr]=tmp; + + } + + } + + else { + + int mid=(st+dr)/2; + + divimp(st, mid); + + divimp(mid+1, dr); + + interc(st, mid, dr); + + } + +} + + + +int main() + +{ + + f.get(a, 50001); + + f.get(); + + f.get(b, 50001); + + n=strlen(a); + + for (i=0; i +#include +#include + + +using namespace std; + + + +int ai[131072],poz,nr,start,finish; + + + +void update(int nod,int st,int dr) + +{ + + int div; + + + + if(st==dr) + + { + + ai[nod]=1; + + return; + + } + + div=(st+dr)/2; + + + + if(poz<=div) + + update(2*nod+1,st,div); + + else + + update(2*nod+2,div+1,dr); + + ai[nod]=ai[2*nod+1]+ai[2*nod+2]; + +} + + + +void query(int nod,int st,int dr) + +{ + + int div; + + + + if(start<=st && dr<=finish) + + { + + nr+=ai[nod]; + + return; + + } + + + + div=(st+dr)/2; + + if(start<=div) + + query(2*nod+1,st,div); + + if(finish>div) + + query(2*nod+2,div+1,dr); + +} + + + +int main() + +{ + + char s[50001],s1[50001],*p,c; + + int i,pos,l=0,nrmin=0,ind[26],pos1; + + vector v[26]; + + + + memset(ai,0,131072*sizeof(int)); + + FILE *f=fopen("swap.in","r"); + + fscanf(f,"%s\n",s); + + fscanf(f,"%s\n",s1); + + fclose(f); + + + + pos=0; + + p=s; + + while(*p!='\0') + + { + + v[*p-'a'].push_back(l); + + l++; + + p++; + + } + + + + for(i=0;i<26;i++) + + ind[i]=0; + + for(pos=0;pos(int) v[c-'a'].size()-1) + + { + + nrmin=-1; + + break; + + } + + pos1=v[c-'a'][ind[c-'a']]; + + ind[c-'a']++; + + nr=0; + + start=pos1+1; + + finish=l-1; + + query(0,0,l-1); + + nrmin+=pos1+nr-pos; + + poz=pos1; + + update(0,0,l-1); + + } + + f=fopen("swap.out","w"); + + fprintf(f,"%i\n",nrmin); + + fclose(f); + + return 0; + +} diff --git a/swap/merge/c56a09cc-22bd-49be-bba8-6bf44dd957cb.cpp b/swap/merge/c56a09cc-22bd-49be-bba8-6bf44dd957cb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/c56a09cc-22bd-49be-bba8-6bf44dd957cb.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/c5ae7828-fbee-46a7-8c9e-09d2a17ae55e.cpp b/swap/merge/c5ae7828-fbee-46a7-8c9e-09d2a17ae55e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/c5ae7828-fbee-46a7-8c9e-09d2a17ae55e.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/c8237431-89e6-4c40-baa9-fd03a1496d3f.cpp b/swap/merge/c8237431-89e6-4c40-baa9-fd03a1496d3f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4281264594f093ddd264f439950bf4d32b3f56 --- /dev/null +++ b/swap/merge/c8237431-89e6-4c40-baa9-fd03a1496d3f.cpp @@ -0,0 +1,183 @@ +#include #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/ca8807fd-3d48-47a3-959d-dc27b059a2e4.cpp b/swap/merge/ca8807fd-3d48-47a3-959d-dc27b059a2e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d769a5a6f93e6aa37dea4ad115183666d155ef24 --- /dev/null +++ b/swap/merge/ca8807fd-3d48-47a3-959d-dc27b059a2e4.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/cacd5edf-a2ef-4ea7-a448-a52de43cea61.cpp b/swap/merge/cacd5edf-a2ef-4ea7-a448-a52de43cea61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38e9f7a399d6595846b50b7d687f81a93243aff5 --- /dev/null +++ b/swap/merge/cacd5edf-a2ef-4ea7-a448-a52de43cea61.cpp @@ -0,0 +1,278 @@ +# include +# include +# include + + +const long int MAXN=50000; + + + +typedef struct NOD {long int pos; NOD *next;}; + +typedef struct NODA {long int inf; NODA *stga,*drta;}; + +NODA *rad; + + + +struct {NOD *prim,*ultim;} list[30]; + +long int n; + +char s1[MAXN+10],s2[MAXN+10]; + + + +void add(long int i, long int pos) + +{ + +NOD *p=(NOD*) malloc (sizeof(NOD)); + +(*p).pos=pos;(*p).next=NULL; + +if (list[i].prim) + + { + + (*list[i].ultim).next=p; + + list[i].ultim=p; + + } + +else + + { + + list[i].prim=p; + + list[i].ultim=p; + + } + +} + + + +void citire() + +{ + +FILE *f=fopen("swap.in","r"); + +fgets(s1+1,MAXN+10,f); + +fgets(s2+1,MAXN+10,f); + +n=strlen(s1+1)-1; + +fclose(f); + +} + + + +long int parity() + +{ + +long int count[30]={0}; + +long int i; + +for (i=1;i<=n;i++) + + { + + count[s1[i]-'a'+1]++; + + count[s2[i]-'a'+1]--; + + } + +for (i=1;i<='z'-'a'+1;i++) if (count[i]!=0) return 0; + +return 1; + +} + + + +void create() + +{ + +long int i; + +for (i=1;i<=n;i++) + + add(s1[i]-'a'+1,i); + +} + + + +void update(NODA *p, long int li, long int lf, long int a) + +{ + +long int m=(li+lf)/2; + +if (lf>=li) + + if (li==lf) (*p).inf++; + + else + + { + + if (a<=m) + + { + + (*p).inf++; + + update((*p).stga,li,m,a); + + } + + else + + { + + (*p).inf++; + + update((*p).drta,m+1,lf,a); + + } + + } + +} + + + +long int query(NODA *p,long int li, long int lf, long int a, long int b) + +{ + +long int m=(li+lf)/2; + +if (b>=a) + + { + + if (li==a&&lf==b) return (*p).inf; + + if (b<=m) return query((*p).stga,li,m,a,b); + + if (a>m ) return query((*p).drta,m+1,lf,a,b); + + return query((*p).stga,li,m,a,m)+query((*p).drta,m+1,lf,m+1,b); + + } + +return 0; + +} + + + +long int calculeaza() + +{ + +long int sol=0; + +long int i; + +for (i=1;i<=n;i++) + + { + + + + sol+=(*list[s2[i]-'a'+1].prim).pos-i+query(rad,1,n,(*list[s2[i]-'a'+1].prim).pos+1,n); + + update(rad,1,n,(*list[s2[i]-'a'+1].prim).pos); + + list[s2[i]-'a'+1].prim=(*list[s2[i]-'a'+1].prim).next; + + } + +return sol; + +} + + + +void scrie(long int a) + +{ + +FILE *g=fopen("swap.out","w"); + +fprintf(g,"%ld\n",a); + +fcloseall(); + +} + + + +void aloca(NODA *rad, long int li, long int lf) + +{ + +NODA *p; + +long int m=(li+lf)/2; + +if (li #include +#include + + +using namespace std; + + + + + + + +int n, m, i, j, k, sol, p2; + +int cor[50010]; + +int arb[3 * 50010]; + + + +char a[50010], b[50010]; + +vector s1[30], s2[30]; + + + +void cit() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + scanf("%s", a); + + scanf("%s", b); + +} + + + +inline int query(int nod, int st, int dr, int ask) { + + if (st == dr) return arb[nod]; + + + + int r = (st + dr) / 2; + + + + if (ask <= r) return arb[nod] + query(nod * 2, st, r, ask); + + else return arb[nod] + query(nod * 2 + 1, r + 1, dr, ask); + +} + + + +void update(int nod, int st, int dr, int left, int right) { + + if (left > dr || right < st) return; + + + + if (left <= st && dr <= right) { + + arb[nod]++; + + return; + + } + + + + int r = (st + dr) / 2; + + update(nod * 2, st, r, left, right); + + update(nod * 2 + 1, r + 1, dr, left, right); + +} + + + +int cost(int poz) { + + int pozitie = cor[poz] + query(1, 1, p2, cor[poz]); + + + + update(1, 1, p2, 1, cor[poz] - 1); + + + + return pozitie - i; + +} + + + +void solve() { + + + + n = strlen(a); + + m = strlen(b); + + for (i = 0; i < n; i++) + + s1[a[i] - 'a'].push_back(i); + + for (j = 0; j < m; j++) + + s2[b[j] - 'a'].push_back(j); + + + + + + for (i = 0; i < 30; i++) + + if (s1[i].size() != s2[i].size()) { + + printf("-1\n"); + + return; + + } + + + + + + for (i = 0; i < 30; i++) { + + k = s2[i].size(); + + for (j = 0; j < k; j++) + + cor[s2[i][j]] = s1[i][j]; + + } + + + + + + p2 = 1; + + while (p2 < n) p2 *= 2; + + for (i = 0; i < n; i++) + + sol += cost(i); + + printf("%d\n", sol); + +} + + + +int main() { + + + + cit(); + + solve(); + + + + return 0; + +} diff --git a/swap/merge/cd337bfa-25fc-42e9-92dd-f65681b92a3b.cpp b/swap/merge/cd337bfa-25fc-42e9-92dd-f65681b92a3b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8633712b7e1d6c8e805ab7c82e7b6f8db8cf46cf --- /dev/null +++ b/swap/merge/cd337bfa-25fc-42e9-92dd-f65681b92a3b.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int n,i,j,u,nrn,rez,pfrz[250000],vs[250000]; +string s; +vector pos[300]; + +void make(int nd,int st,int dr) +{ + if(st==dr) + { + pfrz[++nrn]=nd; + return; + } + int md=st+(dr-st)/2; + make(2*nd,st,md); + make(2*nd+1,md+1,dr); +} +int take(int nd,int sum) +{ + if(nd==1) return sum; + if(nd/2*2+1!=nd) vs[nd/2*2+1]++; + return take(nd/2,sum+vs[nd]); +} +int main() { + + getline(fin,s); + n=(int)s.size(); + for(i=n-1;i>=0;i--) + pos[s[i]-96].push_back(i); + + make(1,1,n); + rez=-n; + getline(fin,s); + for(i=0;i +#include + + + + + +char A[50000 + 2], B[50000 + 2]; + +int next[50000]; +int c[26]; + +int P[50000]; +int tmp[50000]; +int n; + + +inline int mergeCount(int left, int right) { + int i, j, k; + int mid = (left + right) / 2; + int cnt = 0; + + i = left; + j = mid + 1; + k = left; + while (i <= mid && j <= right) { + if (P[i] < P[j]) { + tmp[k] = P[i]; + i++; + } else { + cnt += (mid - i + 1); + tmp[k] = P[j]; + j++; + } + k++; + } + while (i <= mid) { + tmp[k++] = P[i]; + i++; + } + while (j <= right) { + tmp[k++] = P[j]; + j++; + } + memcpy(P + left, tmp + left, (right - left + 1) << 2); + return cnt; +} + +int inversionCount(int left, int right) { + if (left >= right) { + return 0; + } + int mid = (left + right) >> 1; + int invCount = inversionCount(left, mid) + inversionCount(mid + 1, right); + invCount += mergeCount(left, right); + return invCount; +} + +int main(void) { + FILE *f = fopen("swap.in", "r"); + int pos, character; + + fgets(A, 50000 + 2, f); + fgets(B, 50000 + 2, f); + fclose(f); + + n = strlen(A) - 1; + + + for (int i = 0; i < 26; i++) { + c[i] = -1; + } + + for (int i = n - 1; i >= 0; i--) { + character = A[i] - 'a'; + next[i] = c[character]; + c[character] = i; + } + + + + + pos = 0; + do { + character = B[pos] - 'a'; + P[pos] = c[character]; + c[character] = next[c[character]]; + pos++; + } while ((pos < n) && (P[pos - 1] != -1)); + + f = fopen("swap.out", "w"); + if (pos != n || P[n - 1] == -1) { + fputs("-1\n", f); + } else { + fprintf(f, "%d\n", inversionCount(0, n - 1)); + } + fclose(f); + return 0; +} diff --git a/swap/merge/ce2dcc3c-d3ce-419f-a313-af8cab0a3257.cpp b/swap/merge/ce2dcc3c-d3ce-419f-a313-af8cab0a3257.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eede082f575bbd79eb9f1c56d01ac777518189a1 --- /dev/null +++ b/swap/merge/ce2dcc3c-d3ce-419f-a313-af8cab0a3257.cpp @@ -0,0 +1,183 @@ +#include +#include + + +using namespace std; + + + +int N, v[50005]; + +string A, B; + + + +void Citire () { + + ifstream fin ("swap.in"); + + fin >> A >> B; + + fin.close (); + + N = A.size (); + +} + + + +int alfa[30], beta[30]; + + + +int Good () { + + for (int i = 0; i < N; i++) + + { + + alfa[A[i] - 'a']++; + + beta[B[i] - 'a']++; + + } + + for (int i = 0; i <= 30; i++) + + if (!(alfa[i] == beta[i])) return 0; + + return 1; + +} + + + +void Initializare () { + + int k; + + for (char i = 'a'; i <= 'z'; i++) + + { + + k = -1; + + for (int j = 0; j < N; j++) + + { + + if (A[j] == i) + + { + + for (++k; k < N; k++) + + { + + if (B[k] == i) + + { + + v[k] = j; + + break; + + } + + } + + } + + } + + } + +} + + + +int aux[50005]; + + + +int Business (int L, int R) { + + int cnt = 0, mij = (L + R) >> 1, k = L - 1, i, j; + + if (L >= R) return 0; + + cnt += Business (L, mij); + + cnt += Business (mij + 1, R); + + for (i = L, j = mij + 1; i <= mij && j <= R;) + + { + + if (v[i] <= v[j]) aux[++k] = v[i++]; + + else + + { + + aux[++k] = v[j++]; + + cnt += mij - i + 1; + + } + + } + + while (i <= mij) + + { + + aux[++k] = v[i++]; + + } + + while (j <= R) + + { + + aux[++k] = v[j++]; + + } + + for (int i = L; i <= R; i++) + + v[i] = aux[i]; + + return cnt; + +} + + + +int main () { + + Citire (); + + ofstream fout ("swap.out"); + + if (!Good ()) + + { + + fout << "-1"; + + fout.close (); + + return 0; + + } + + Initializare (); + + fout << Business (0, N - 1); + + fout.close (); + + return 0; + +} diff --git a/swap/merge/cf942eee-c061-44d0-ab16-f7d054d5d242.cpp b/swap/merge/cf942eee-c061-44d0-ab16-f7d054d5d242.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d3ba3d3c9ed3c821e98fa3dbc7ccb7e010d7631 --- /dev/null +++ b/swap/merge/cf942eee-c061-44d0-ab16-f7d054d5d242.cpp @@ -0,0 +1,240 @@ +#include +using namespace std; + + + +FILE *IN, *OUT; + +int N, Ans, Total, Start, End; + +int c1[26], c2[26]; + +int first[50005], apear[27], second[26][50005], Val; + + + +int pos, out, sign; + +char f[131072], Out[131072], str[10], c; + + + +struct _Tree{ + + int Lower, Pos; + +}T[132000]; + + + +inline void R_Ch(char &ch){ + + ch = f[pos++]; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + +} + + + +inline void Write_Ch(char ch){ + + Out[out++] = ch; + + if(out == 131072) + + fwrite(Out, 131072, 1, OUT), out = 0; + +} + + + +inline void Write_Int(int nr){ + + int x = 0; + + if(nr < 0) Write_Ch('-'), nr =- nr; + + do{ + + str[x++] = nr % 10 + '0'; + + nr /= 10; + + } + + while(nr); + + while(x > 0) + + Write_Ch(str[--x]); + +} + + + +inline int max2(int a,int b){ + + if (a > b) return a; + + else return b; + +} + + + +inline void Read(int &nr){ + + sign = 0; + + nr = 0; + + while(f[pos] < '0' || f[pos] > '9'){ + + if(f[pos] == '-') sign=1; + + pos++; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + while(f[pos] >= '0' && f[pos] <= '9'){ + + nr = 10 * nr + f[pos++] - '0'; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + if(sign) nr =- nr; + +} + + + +void Update(int node,int start,int end,int V){ + + if(start == end) + + T[node].Pos = V; + + else{ + + int mid = (start + end) >> 1; + + if(V <= mid) Update(2 * node, start, mid, V); + + else Update(2 * node + 1, mid + 1, end, V); + + } + +} + + + +void Query(int node,int start,int end){ + + if(start == end){ + + Ans += T[node].Pos + T[node].Lower; + + return; + + } else { + + Ans += T[node].Lower; + + int mid = (start+end)>>1; + + if(Val <= mid) Query(2 * node, start, mid); + + else{ + + T[2 * node].Lower++; + + Query(2 * node + 1, mid + 1, end); + + } + + } + +} + +int main(){ + + IN = fopen("swap.in", "r"); + + OUT = fopen("swap.out", "w"); + + + + fread(f, 1, 131072, IN); + + R_Ch(c); + + while(c != '\n'){ + + c1[c - 'a']++; + + first[++N] = c - 'a'; + + R_Ch(c); + + } + + for(int i = 1; i <= N; i++){ + + R_Ch(c); + + c2[c - 'a']++; + + second[c - 'a'][++apear[c - 'a']] = i; + + Update(1, 1, N, i); + + } + + bool possible = true; + + for(int i = 0; i <= 25 && possible; i++) + + if(c1[i] != c2[i]) possible = false; + + if(!possible){ + + Write_Ch('-'); + + Write_Ch('1'); + + } else { + + memset(apear, 0, sizeof apear); + + for(int i =1 ; i <= N; i++){ + + Ans = 0; + + Val = second[first[i]][++apear[first[i]]]; + + Query(1, 1, N); + + Total += Ans - i; + + } + + Write_Int(Total); + + } + + if(out > 0) fwrite(Out, 1, out, OUT); + + return 0; + +} diff --git a/swap/merge/d0360925-7d68-4ed8-8df6-720bfbe4d119.cpp b/swap/merge/d0360925-7d68-4ed8-8df6-720bfbe4d119.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91e5980b883af7ab699546a07df69b09f2508383 --- /dev/null +++ b/swap/merge/d0360925-7d68-4ed8-8df6-720bfbe4d119.cpp @@ -0,0 +1,117 @@ +# include +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/d13869cb-be2f-4129-8612-3bc3ed541950.cpp b/swap/merge/d13869cb-be2f-4129-8612-3bc3ed541950.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f5d9ae25cda0936322d2f1155ad2d13ad115d34 --- /dev/null +++ b/swap/merge/d13869cb-be2f-4129-8612-3bc3ed541950.cpp @@ -0,0 +1,186 @@ +#include +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/d15a7d0f-216c-4cb5-99d4-7a5e88402275.cpp b/swap/merge/d15a7d0f-216c-4cb5-99d4-7a5e88402275.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5edc102da31b7812fbffdde8bcfff51333bb50b4 --- /dev/null +++ b/swap/merge/d15a7d0f-216c-4cb5-99d4-7a5e88402275.cpp @@ -0,0 +1,174 @@ +#include +#include +#include + + +using namespace std; + + + +const char iname[]="swap.in"; + +const char oname[]="swap.out"; + +const int maxn=100005; + + + +vector a[300]; + + + +char s[maxn],m[maxn]; + + + +int n,i,j,k,v,many[300]; + + + +int aib[maxn*2]; + + + +void update(int nod,int left,int right,int pos,int value) + +{ + + if(left==right) + + { + + aib[nod]=value; + + return; + + } + + int mid=(left+right)>>1; + + if(pos<=mid) + + update(nod<<1,left,mid,pos,value); + + else + + update((nod<<1)+1,mid+1,right,pos,value); + + aib[nod]=aib[nod<<1]+aib[(nod<<1)+1]; + +} + + + +int query(int nod,int left,int right,int start,int finish) + +{ + + if(start<=left&&right<=finish) + + return aib[nod]; + + + + int mid=(left+right)>>1,k=0; + + if(start<=mid) + + k+=query(nod<<1,left,mid,start,finish); + + if(mid=0;--i) + + a[s[i]].push_back(i); + + + + for(i=0;i0) + + k+=query(1,0,n-1,0,v-1); + + update(1,0,n-1,v,0); + + } + + + + printf("%d\n",k); + + + + fclose(stdin); + + fclose(stdout); + + + + return 0; + +} diff --git a/swap/merge/d184dfb6-a5f1-48de-a22f-d479999eabcb.cpp b/swap/merge/d184dfb6-a5f1-48de-a22f-d479999eabcb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/d184dfb6-a5f1-48de-a22f-d479999eabcb.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/d1e1ffdf-bbb7-470d-b877-6c330f919efd.cpp b/swap/merge/d1e1ffdf-bbb7-470d-b877-6c330f919efd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46539ab3b9985d1f1b0331b97d76af6ea9488513 --- /dev/null +++ b/swap/merge/d1e1ffdf-bbb7-470d-b877-6c330f919efd.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +using namespace std; +const int MAX_N = 50100; +ifstream fin("swap.in"); +ofstream fout("swap.out"); +int alf[27][MAX_N]; +int alfIt[27]; +int v[MAX_N]; +int merge_sort(int, int); +int main() { + string a1, a2; + getline(fin, a1); + getline(fin, a2); + for (unsigned int i = 0; i < a2.size(); ++i) { + int ch = a2[i] - 'a'; + alf[ch][++alf[ch][0]] = i + 1; + } + for (unsigned int i = 0; i < a1.size(); ++i) { + int ch = a1[i] - 'a'; + ++alfIt[ch]; + if (alfIt[ch] > alf[ch][0]) { + fout << -1; + return 0; + } + v[i+1] = alf[ch][alfIt[ch]]; + } + int N = a1.size(); + fout << merge_sort(1, N); + return 0; +} + +int merge_sort(int lo, int hi) { + if (lo == hi) return 0; + int mid = lo + (hi - lo) / 2, numInv; + numInv = merge_sort(lo, mid) + merge_sort(mid + 1, hi); + int aux[MAX_N]; + for (int i = lo, j = mid + 1, k = lo; i <= mid || j <= hi; ++k) { + if ((i <= mid && v[i] < v[j]) || j > hi) { + aux[k] = v[i++]; + } else { + aux[k] = v[j++]; + numInv += (mid - i + 1); + } + } + for (int k = lo; k <= hi; ++k) { + v[k] = aux[k]; + } + return numInv; +} diff --git a/swap/merge/d3e12531-e2c7-4209-94c4-7f2e1cc56354.cpp b/swap/merge/d3e12531-e2c7-4209-94c4-7f2e1cc56354.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48ab10d62d7df2146cd5fd460177024a0e9b1d2b --- /dev/null +++ b/swap/merge/d3e12531-e2c7-4209-94c4-7f2e1cc56354.cpp @@ -0,0 +1,271 @@ +#include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/d54ebaba-62b4-4d49-9213-f78d3df1e639.cpp b/swap/merge/d54ebaba-62b4-4d49-9213-f78d3df1e639.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6199e366d57b479e3822e8dc4d4275fdc7c67cbe --- /dev/null +++ b/swap/merge/d54ebaba-62b4-4d49-9213-f78d3df1e639.cpp @@ -0,0 +1,116 @@ +#include + + + + + + +using namespace std; + + + +const int Nmax = 50000 + 10; + + + +int i , n , ans; + +int aib[Nmax]; + +int nxt[Nmax] , last[30] , crt[30]; + + + +char a[Nmax] , b[Nmax]; + + + +int aibQuery(int i) + +{ + + int ret = 0; + + for ( ; i ; i -= (i&(-i))) + + ret += aib[i]; + + return ret; + +} + + + +void aibUpdate(int i) + +{ + + for ( ; i <= n; i += (i&(-i))) + + aib[i]++; + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets(a+1, 50001, stdin); fgets(b+1, 50001, stdin); n = strlen(a+1); + + + + for (i = n; i ; --i) + + { + + if (!last[b[i]-'a']) crt[b[i]-'a'] = i; + + nxt[last[b[i]-'a']] = i; + + last[b[i]-'a'] = i; + + } + + + + for (i = n; i ; --i) + + { + + if (!crt[a[i]-'a']) + + { + + printf("-1\n"); + + return 0; + + } + + ans += aibQuery(crt[a[i]-'a']); + + aibUpdate(crt[a[i]-'a']); + + + + crt[a[i]-'a'] = nxt[crt[a[i]-'a']]; + + } + + + + printf("%d\n", ans); + + + + return 0; + +} diff --git a/swap/merge/d60de775-38ad-4311-ab28-d7fdce395e04.cpp b/swap/merge/d60de775-38ad-4311-ab28-d7fdce395e04.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7339bbf91c946c2a125bc8fbde98f2cdecafc65a --- /dev/null +++ b/swap/merge/d60de775-38ad-4311-ab28-d7fdce395e04.cpp @@ -0,0 +1,271 @@ +#include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/d61dc06d-4f5e-482b-8f8b-612bdd3ac663.cpp b/swap/merge/d61dc06d-4f5e-482b-8f8b-612bdd3ac663.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/d61dc06d-4f5e-482b-8f8b-612bdd3ac663.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< +#include +#include +using namespace std; + +const int NO_LIT = 'z' - 'a' + 1; +const int LMAX = 50001; +vectorlitere[NO_LIT + 1]; +int left[NO_LIT + 1], aib[LMAX], l; + +char a[LMAX],b[LMAX]; + +int askAIB(int pos) { + int ret = 0; + while (pos > 0) { + ret += aib[pos]; + pos -= pos ^ (pos & (pos - 1)); + } + return ret; +} + +void incrAIB(int pos) { + while (pos <= l) { + aib[pos]++; + pos += pos ^ (pos & (pos - 1)); + } +} + +int solve() { + FILE *fin = fopen("swap.in", "r"); + fscanf (fin, "%s\n", &a); + fscanf (fin, "%s\n", &b); + l = strlen(a); + if (strlen(b) != l) return -1; + + int i; + for (i = 0; i < l; ++i) { + litere[a[i]-'a'].push_back(i); + } + int ret = 0; + for (i = 0; i < l; ++i) { + int litera = b[i] - 'a'; + if (left[litera] == litere[litera].size()) { + return -1; + } + int pos = litere[litera][left[litera]++]; + ret += pos - askAIB(pos); + incrAIB(pos + 1); + } + return ret; +} + +int main() { + fprintf (fopen("swap.out", "w"), "%d", solve()); + return 0; +} diff --git a/swap/merge/d8c194f8-1ee6-43a8-b80a-c27c63394160.cpp b/swap/merge/d8c194f8-1ee6-43a8-b80a-c27c63394160.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e33d5a1cb61b2956ed5b7022efc4cbcdbce3a650 --- /dev/null +++ b/swap/merge/d8c194f8-1ee6-43a8-b80a-c27c63394160.cpp @@ -0,0 +1,86 @@ +#include #include #include +using namespace std; + +long n,m,i,j,r[50001],k,v[50001],x[30],y[30],a[30][50001],b[30][50001]; + +long long l; + +char s1[50001],s2[50001],c; + + + +void merge(long r[50001],long p,long q,long long *l) + +{long i,j,k,m=(p+q)/2; + +if(p==q) + + return; + +merge(r,p,m,l); + +merge(r,m+1,q,l); + +for(i=k=p,j=m+1;i<=m||j<=q;) + +if(j>q||(i<=m&&r[i]>s1; + +f>>s2; + +n=strlen(s1); + +for(i=0;i +#include +#include + + + +using namespace std; + + + +char S[2][50666]; +vector v[30]; +int cnt[2][30], P[50666], Paux[50666]; +int i, j, k, N, ninv; + +void compute_ninv(int li, int ls) +{ + int m; + + if (li >= ls) + return; + + m = (li + ls) >> 1; + + compute_ninv(li, m); + compute_ninv(m + 1, ls); + + i = li, j = m + 1, k = li - 1; + + while (i <= m && j <= ls) + { + if (P[i] < P[j]) + Paux[++k] = P[i++]; + else + { + ninv += (m - i + 1); + Paux[++k] = P[j++]; + } + } + + if (i <= m) + { + for (j = i; j <= m; j++) + Paux[++k] = P[j]; + } + else + { + for (i = j; i <= ls; i++) + Paux[++k] = P[i]; + } + + for (i = li; i <= ls; i++) + P[i] = Paux[i]; +} + + + + + +void merge_sort(int l, int r, int *A, int *B) + +{ + + int m = (l + r) >> 1, i, j, k; + + if (l == r) return; + + merge_sort(l, m, A, B); + + merge_sort(m + 1, r, A, B); + + for (i=l, j=m+1, k=l; i<=m || j<=r; ) + + if (j > r || (i <= m && A[i] < A[j])) + + B[k++] = A[i++]; + + else + + ninv += (m - i + 1), B[k++] = A[j++]; + + for (k = l; k <= r; k++) A[k] = B[k]; + +} + +int main() +{ + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + + fgets(S[0] + 1, 50666, stdin); + fgets(S[1] + 1, 50666, stdin); + + N = strlen(S[0] + 1) - 1; + + memset(cnt, 0, sizeof(cnt)); + for (i = 1; i <= N; i++) + { + cnt[1][S[1][i] - 'a']++; + v[S[1][i] - 'a'].push_back(i); + } + + for (i = 1; i <= N; i++) + { + cnt[0][S[0][i] - 'a']++; + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + { + printf("-1\n"); + return 0; + } + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + } + + ninv = 0; + + + merge_sort ( 1, N, P, Paux ) ; + + printf("%d\n", ninv); + + return 0; +} diff --git a/swap/merge/db3d4967-79f4-410d-8521-2ae04c1c1b2c.cpp b/swap/merge/db3d4967-79f4-410d-8521-2ae04c1c1b2c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b171ed3342ad84342fba440df8339adb0bca7943 --- /dev/null +++ b/swap/merge/db3d4967-79f4-410d-8521-2ae04c1c1b2c.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +using namespace std; + +ifstream fi("swap.in"); + +ofstream fo("swap.out"); + +char A[50002],B[50002]; + +int FA[26],FB[26]; + +deque POZB[26]; + +deque :: iterator it; + +int AIB[50002],naib=50000; + + + +long long rez; + + + +void update(int poz, int v) + + + +{ + + while (poz<=naib) + + { + + AIB[poz]+=v; + + poz=poz+(poz&(-poz)); + + } + +} + + + +int f(int poz) + +{ + + int rez; + + rez=0; + + while (poz>=1) + + { + + rez=rez+AIB[poz]; + + poz=poz-(poz&(-poz)); + + } + + return rez; + +} + + + +int query(int st, int dr) + +{ + + return f(dr)-f(st-1); + +} + + + +int main() + +{ + + fi>>A+1; + + fi>>B+1; + + for (int i=1;A[i]!='\0';i++) + + FA[A[i]-'a']++; + + for (int i=1;B[i]!='\0';i++) + + FB[B[i]-'a']++; + + int t=1; + + for (int i=0;i<=25;i++) + + if (FA[i]!=FB[i]) + + t=0; + + if (t==0) + + fo<<-1; + + else + + { + + + + + + for (int i=1;B[i]!='\0';i++) + + POZB[B[i]-'a'].push_back(i); + + int n=strlen(B+1); + + for (int i=1;i<=n;i++) + + update(i,1); + + rez=0; + + for (int i=1;i<=n;i++) + + { + + int c; + + c=A[i]; + + int poz; + + poz=POZB[c-'a'].front(); + + POZB[c-'a'].pop_front(); + + update(poz,-1); + + rez=rez+query(1,poz); + + } + + fo< +#include +#include +using namespace std; + + + +char a [50005], b [50005]; + +int x, n, s, fa [30], fb [30], arb [50005<<2]; + +queue l [30]; + + + +void frecv (char x [], int f []) + +{ + + int i; + + for (i=0; x [i]; ++i) + + ++f [x [i]-'a']; + +} + + + +bool egale () + +{ + + frecv (a, fa); + + frecv (b, fb); + + int i; + + for (i=0; i < 30; ++i) + + if (fa [i] != fb [i]) + + return false; + + return true; + +} + + + +void init () + +{ + + int i; + + for (i=0; a [i]; ++i) + + l [a [i]-'a'].push (i); + +} + + + +void update (int nod, int st, int dr) + +{ + + if (st == dr) + + { + + ++arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m >= x) + + update (nod<<1, st, m); + + else + + update ((nod<<1)+1, m+1, dr); + + arb [nod]=arb [nod<<1]+arb [(nod<<1)+1]; + +} + + + +void query (int nod, int st, int dr) + +{ + + if (dr <= x) + + { + + s+=arb [nod]; + + return ; + + } + + int m=(st+dr)/2; + + if (m < x) + + query ((nod<<1)+1, m+1, dr); + + query (nod<<1, st, m); + +} + + + +int res () + +{ + + n=strlen (a)-1; + + init (); + + int i, r=0; + + for (i=0; b [i]; ++i) + + { + + x=l [b [i]-'a'].front (); + + s=0; + + query (1, 0, n); + + r+=x-s; + + + + + + l [b [i]-'a'].pop (); + + update (1, 0, n); + + } + + return r; + +} + + + +int main () + +{ + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + fgets (a, 50001, stdin); + + fgets (b, 50001, stdin); + + if (!egale ()) + + { + + printf ("-1\n"); + + return 0; + + } + + printf ("%d\n", res ()); + + return 0; + +} diff --git a/swap/merge/dc7f3dec-b150-496f-9baf-efd8e0f480a9.cpp b/swap/merge/dc7f3dec-b150-496f-9baf-efd8e0f480a9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..91e5980b883af7ab699546a07df69b09f2508383 --- /dev/null +++ b/swap/merge/dc7f3dec-b150-496f-9baf-efd8e0f480a9.cpp @@ -0,0 +1,117 @@ +# include +# include + + + + + + +const char *FIN = "swap.in", *FOU = "swap.out" ; + +const int MAX = 50005 ; + + + +char sir[2][MAX]; + +int V[30][MAX], cnt[2][30], aux[30] ; + +int A[MAX], B[MAX]; + +int sol ; + + + +void solve ( int l, int r ) { + + int m = (l + r) >> 1 ; + + + + if (l == r) return ; + + + + solve ( l, m ) ; + + solve ( m + 1, r ) ; + + + + for ( int i = l, j = m + 1, k = l; i <= m || j <= r; ) + + if ( j > r || ( i <= m && A[i] < A[j] ) ) { + + B[k++] = A[i++]; + + } else { + + sol += ( m - i + 1 ), B[k++] = A[j++] ; + + } + + for ( int k = l; k <= r; ++k ) { + + A[k] = B[k] ; + + } + +} + + + +int main ( void ) { + + freopen ( FIN, "r", stdin ) ; + + + + fgets ( sir[0] + 1, MAX, stdin ) ; + + fgets ( sir[1] + 1, MAX, stdin ) ; + + + + int N = strlen ( sir[0] + 1 ) - 1 ; + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[1][ ( sir[1][i] - 'a' ) ] ; + + V[ ( sir[1][i] - 'a' ) ][ aux[ ( sir[1][i] - 'a' ) ]++ ] = i ; + + } + + + + for ( int i = 1; i <= N; ++i ) { + + ++cnt[0][ ( sir[0][i] - 'a' ) ] ; + + + + if ( cnt[0][ ( sir[0][i] - 'a' ) ] > cnt[1][ ( sir[0][i] - 'a' ) ] ) { + + fprintf ( fopen ( FOU, "w" ), "-1\n" ) ; + + return 0 ; + + } + + + + A[i] = V[ ( sir[0][i] - 'a' ) ][ cnt[0][ ( sir[0][i] - 'a' ) ] - 1 ] ; + + } + + + + solve ( 1, N ) ; + + + + fprintf ( fopen ( FOU, "w" ), "%d", sol ) ; + +} diff --git a/swap/merge/dd2554d2-c5f7-4dbf-8a46-fe63b3592ff2.cpp b/swap/merge/dd2554d2-c5f7-4dbf-8a46-fe63b3592ff2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b92e7f70db762a65107659ab1fb642eb8632d11 --- /dev/null +++ b/swap/merge/dd2554d2-c5f7-4dbf-8a46-fe63b3592ff2.cpp @@ -0,0 +1,180 @@ +#include +#include +#include + + + + + + +using namespace std; + + + + + + + +char S[2][50666]; + +vector v[30]; + +int cnt[2][30], P[50666], Paux[50666]; + +int i, j, k, N, ninv; + + + +void compute_ninv(int li, int ls) + +{ + + int m; + + + + if (li >= ls) + + return; + + + + m = (li + ls) >> 1; + + + + compute_ninv(li, m); + + compute_ninv(m + 1, ls); + + + + i = li, j = m + 1, k = li - 1; + + + + while (i <= m && j <= ls) + + { + + if (P[i] < P[j]) + + Paux[++k] = P[i++]; + + else + + { + + ninv += (m - i + 1); + + Paux[++k] = P[j++]; + + } + + } + + + + if (i <= m) + + { + + for (j = i; j <= m; j++) + + Paux[++k] = P[j]; + + } + + else + + { + + for (i = j; i <= ls; i++) + + Paux[++k] = P[i]; + + } + + + + for (i = li; i <= ls; i++) + + P[i] = Paux[i]; + +} + + + +int main() + +{ + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S[0] + 1, 50666, stdin); + + fgets(S[1] + 1, 50666, stdin); + + + + N = strlen(S[0] + 1) - 1; + + + + memset(cnt, 0, sizeof(cnt)); + + for (i = 1; i <= N; i++) + + { + + cnt[1][S[1][i] - 'a']++; + + v[S[1][i] - 'a'].push_back(i); + + } + + + + for (i = 1; i <= N; i++) + + { + + cnt[0][S[0][i] - 'a']++; + + + + if (cnt[0][S[0][i] - 'a'] > cnt[1][S[0][i] - 'a']) + + { + + printf("-1\n"); + + return 0; + + } + + + + P[i] = v[S[0][i] - 'a'][cnt[0][S[0][i] - 'a'] - 1]; + + } + + + + ninv = 0; + + compute_ninv(1, N); + + + + printf("%d\n", ninv); + + + + return 0; + +} diff --git a/swap/merge/dd708ea5-16dc-4cd8-8a21-27a49511cbc8.cpp b/swap/merge/dd708ea5-16dc-4cd8-8a21-27a49511cbc8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..050bff30275a305e99e532027f7890a516a31cb5 --- /dev/null +++ b/swap/merge/dd708ea5-16dc-4cd8-8a21-27a49511cbc8.cpp @@ -0,0 +1,77 @@ +#include +#include +#include + +using namespace std; + + + +long ind[2][30], a[50010], A[50010], i, j, k, len, sol; + +vector v[30]; + +char cuv[2][50010]; + +void baga_arbore(long li, long ls) { + long mid; + if (li >= ls) { + return; + + } + mid = (li + ls) >> 1; + baga_arbore(li, mid); + baga_arbore(mid + 1, ls); + i = li; + + j = mid + 1; + + k = li - 1; + while (i <= mid && j <= ls) { + if (a[i] < a[j]) { + A[++k] = a[i++]; + + } else { + sol += (mid - i + 1); + A[++k] = a[j++]; + } + } + if (i <= mid) { + for (j = i; j <= mid; ++j) { + A[++k] = a[j]; + + } + } else { + for (i = j; i <= ls; ++i) { + A[++k] = a[i]; + + } + } + for (i = li; i <= ls; ++i) { + a[i] = A[i]; + + } +} + +int main() { + freopen("swap.in", "r", stdin); + freopen("swap.out", "w", stdout); + fgets(cuv[0] + 1, 50010, stdin); + fgets(cuv[1] + 1, 50010, stdin); + len = strlen(cuv[0] + 1) - 1; + for (i = 1; i <= len; ++i) { + ++ind[1][cuv[1][i] - 'a']; + v[cuv[1][i] - 'a'].push_back(i); + } + for (i = 1; i <= len; i++) { + + ++ind[0][cuv[0][i] - 'a']; + if (ind[0][cuv[0][i] - 'a'] > ind[1][cuv[0][i] - 'a']) { + printf("-1\n"); + return 0; + } + a[i] = v[cuv[0][i] - 'a'][ind[0][cuv[0][i] - 'a'] - 1]; + } + baga_arbore(1, len); + printf("%ld\n", sol); + return 0; +} diff --git a/swap/merge/dd7d1846-2593-4bd7-a34a-99071c0ec04e.cpp b/swap/merge/dd7d1846-2593-4bd7-a34a-99071c0ec04e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c865f1ead6091e04c324807f93ac324bb88296e9 --- /dev/null +++ b/swap/merge/dd7d1846-2593-4bd7-a34a-99071c0ec04e.cpp @@ -0,0 +1,271 @@ +#include #include #include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/dfba18e3-b551-4765-bc38-c7860e05042c.cpp b/swap/merge/dfba18e3-b551-4765-bc38-c7860e05042c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4177e1c7b6b614684d0f52d27ea1014f94e73190 --- /dev/null +++ b/swap/merge/dfba18e3-b551-4765-bc38-c7860e05042c.cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +char a[50005], b[50005]; + +int u1[27], u2[27], fr[27], p[50005], H[3*50005], sol; + +void update(int n, int l, int r, int p, int v) { + + if (l >= r) {H[n] += v; return;} + + int m = (l + r) >> 1; + + if (m >= p) update(n<<1,l,m,p,v); + + else update((n<<1)|1,m+1,r,p,v); + + H[n] = H[n<<1] + H[(n<<1)|1]; + +} + +void query(int n, int l, int r, int a, int b) { + + if (a <= l && r <= b) {sol += H[n]; return;} + + int m = (l + r) >> 1; + + if (a <= m) query(n<<1,l,m,a,b); + + if (b > m) query((n<<1)|1,m+1,r,a,b); + +} + + + +int main() + +{ + + for (int i = 0; i < 27; ++i) fr[i] = -1; + + f >> a >> b; + + int i; + + for (i = 0; a[i]; ++i) ++u1[a[i] - 'a']; + + int n = i; + + for (i = 0; b[i]; ++i) {++u2[b[i] - 'a']; if (fr[b[i]-'a'] == -1) fr[b[i]-'a'] = i;} + + for (i = 0; i < 27; ++i) if (u1[i] != u2[i]) {g << "-1\n"; return 0;} + + for (i = 0; a[i]; ++i) { + + p[i + 1] = fr[a[i]-'a'] + 1; + + ++fr[a[i]-'a']; + + while (b[fr[a[i]-'a']] != a[i]) { + + ++fr[a[i]-'a']; + + if (!b[fr[a[i]-'a']]) fr[a[i]-'a'] = 0; + + } + + } + + for (i = 1; i <= n; ++i) update(1,1,n,p[i],1); + + for (i = 1; i <= n; ++i) { + + if (p[i] > 1) query(1,1,n,1,p[i] - 1); + + update(1,1,n,p[i],-1); + + } + + g << sol << '\n'; + + return 0; + +} diff --git a/swap/merge/e2ffd15f-4508-4460-bf0b-213f3395f153.cpp b/swap/merge/e2ffd15f-4508-4460-bf0b-213f3395f153.cpp new file mode 100644 index 0000000000000000000000000000000000000000..08f710d923d9830ebe5a061474eb1ce2a70d8e6b --- /dev/null +++ b/swap/merge/e2ffd15f-4508-4460-bf0b-213f3395f153.cpp @@ -0,0 +1,170 @@ +#include #include #include +#include +using namespace std; +char A[ 50001 ], B[ 50001 ]; + +int N, XXX, F[ 50001 ], P[ 50001 ]; + +int sum, arb[ 50001<<2 ]; + +vector poz[ 26 ]; + + + +inline void query( const int &nod, const int &st, const int &dr, const int &a, const int &b ) { + + + + int mij; + + + + if( a <= st && dr <= b ) { + + + + sum += arb[ nod ]; + + return; + + } + + mij = (st+dr) / 2; + + if( a <= mij ) + + query( nod<<1, st, mij, a, b ); + + if( mij < b ) + + query( (nod<<1) + 1, mij+1, dr, a, b ); + +} + + + +inline void update( const int &nod, const int &st, const int &dr, const int &poz ) { + + + + int mij; + + + + if( st == dr ) { + + + + arb[ nod ] = 1; + + return; + + } + + mij = (st+dr) / 2; + + if( poz <= mij ) + + update( nod<<1, st, mij, poz ); + + else + + update( (nod<<1) + 1, mij+1, dr, poz ); + + arb[ nod ] = arb[ nod<<1 ] + arb[ (nod<<1) + 1 ]; + +} + + + +int main() { + + + + freopen( "swap.in", "r", stdin ); + + freopen( "swap.out", "w", stdout ); + + + + int i, ind; + + + + fgets( A+1, 50001, stdin); + + fgets( B+1 , 50001, stdin); + + + + N = strlen( A+1 ); + + for( i = 1; i <= N; ++i ) { + + + + ++F[ A[ i ] - 'a' ]; + + --F[ B[ i ] - 'a' ]; + + } + + for( i = 0; i < 26; ++i ) + + if( F[ i ] ) { + + + + printf( "-1" ); + + return 0; + + } + + for( i = 1; i <= N; ++i ) { + + + + ++F[ B[ i ] - 'a' ]; + + poz[ B[ i ] - 'a' ].push_back( i ); + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + ind = A[ i ] - 'a'; + + ++F[ ind ]; + + P[ i ] = poz[ ind ][ F[ ind ] - 1 ]; + + } + memset( F, 0, sizeof( F ) ); + + for( i = 1; i <= N; ++i ) { + + + + update( 1, 1, N, P[ i ] ); + + sum = 0; + + query( 1, 1, N, 1, P[ i ] ); + + XXX += P[ i ] - sum; + + } + + + + printf( "%d", XXX ); + + + + return 0; + +} diff --git a/swap/merge/e32c0c34-9733-4554-8984-8e80d764dace.cpp b/swap/merge/e32c0c34-9733-4554-8984-8e80d764dace.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4281264594f093ddd264f439950bf4d32b3f56 --- /dev/null +++ b/swap/merge/e32c0c34-9733-4554-8984-8e80d764dace.cpp @@ -0,0 +1,183 @@ +#include #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i +#include + + +const int MAXN = 5e4; + + + + + +std::vector pos[26]; + +int f1[26], f2[26], last[26], + + aib[MAXN + 1], p[MAXN + 1]; + +char s1[MAXN], s2[MAXN]; + + + +void update(int x, int val, int n) { + + while (x <= n) { + + aib[x] += val; + + x += x & -x; + + } + +} + + + +int sum(int x) { + + int s = 0; + + while (x > 0) { + + s += aib[x]; + + x -= x & -x; + + } + + return s; + +} + + + +int main() { + + int n, imp, ans; + + char ch; + + FILE *f = fopen("swap.in", "r"); + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s1[n++] = ch; + + ++f1[ch]; + + ch = fgetc(f); + + } + + n = 0; + + ch = fgetc(f); + + while (ch != '\n') { + + ch -= 'a'; + + s2[n++] = ch; + + pos[ch].push_back(n); + + ++f2[ch]; + + ch = fgetc(f); + + } + + fclose(f); + + imp = 0; + + while (imp < 26 && f1[imp] == f2[imp]) { + + ++imp; + + } + + f = fopen("swap.out", "w"); + + if (imp != 26) { + + fprintf(f, "-1\n"); + + } else { + + for (int i = 0; i < n; ++i) { + + p[i] = pos[s1[i]][last[s1[i]]++]; + + } + + ans = 0; + + for (int i = n - 1; i >= 0; --i) { + + ans += sum(p[i]); + + update(p[i], 1, n); + + } + + fprintf(f, "%d\n", ans); + + } + + fclose(f); + + return 0; + +} diff --git a/swap/merge/e4c4321f-911a-4d3d-b17f-bc81f233bd29.cpp b/swap/merge/e4c4321f-911a-4d3d-b17f-bc81f233bd29.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ede756e64ffaabd0b9d62fb39ee5ed573a37a380 --- /dev/null +++ b/swap/merge/e4c4321f-911a-4d3d-b17f-bc81f233bd29.cpp @@ -0,0 +1,129 @@ +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +char s[50001],s1[50001]; + +int n,A[200][50001],viz[50001],v[50001],m,i,a,b,arb[200000],ok; + +long long sol; + +int query(int nod, int st,int dr){ + + int sol=0; + + if(a<=st&&b>=dr) + + return arb[nod]; + + int mij=(st+dr)/2; + + if(a<=mij) + + sol=query(2*nod,st,mij); + + if(mij>s+1; + +n=strlen(s+1); + + for(i=1;i<=n;i++){ + + A[s[i]][0]++; + + A[s[i]][A[s[i]][0]]=i; + + } + + f>>s1+1; + + m=strlen(s1+1); + + for(i=1;i<=m;i++){ + + viz[s1[i]]++; + + v[A[s1[i]][viz[s1[i]]]]=i;} + + for(i='a';i<='z';i++) + + { + + if(viz[i]!=A[i][0]){ + + ok=1; + + break; + + } + + } + + if(ok==1) + + g<<-1; + + else{ + +for(i=1;i<=n;i++){ + + + + a=v[i]+1; + + b=n; + + sol+=query(1,1,n); + + + + update(1,1,n);} + + g< +#include +#include +using namespace std; +queue A[28]; + +int Co[28]; + + + +char One[50011];int N; + +char Two[50011];int M; + + + +int Minus[50011*4],Rez,Semn; + +int Solve; + + + +void Update( int Nod,int St,int Dr,int Pos ) + +{ + + if ( St==Dr ) + + { + + Minus[Nod] += Semn; + + return; + + } + + + + int Mid=(St+Dr)/2; + + if ( Mid>=Pos ) + + Update( Nod*2,St,Mid,Pos ); + + else + + Update( Nod*2+1,Mid+1,Dr,Pos ); + + Minus[Nod]=Minus[Nod*2]+Minus[Nod*2+1]; + +} + + + +void Query( int Nod,int St,int Dr,int Begin,int Endin ) + +{ + + if ( St>=Begin && Dr<=Endin ) + + { + + Rez += Minus[Nod]; + + return; + + } + + + + if ( St==Dr ) return; + + + + int Mid=(St+Dr)/2; + + if ( Begin <=Mid ) + + Query( Nod*2, St, Mid, Begin , Endin ); + + if ( Mid < Endin ) + + Query( Nod*2+1 , Mid+1, Dr, Begin , Endin ); + +} + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( One , 50001, stdin);N=strlen( One ); + + fgets( Two , 50001, stdin);M=strlen( Two ); + + + + for (int i=N;i;--i) + + { + + One[i]=One[i-1],++Co[ One[i]-'a'+1 ]; + + Two[i]=Two[i-1],--Co[ Two[i]-'a'+1 ]; + + } + + + + if ( N-M ) + + { + + printf("-1\n"); + + return 0; + + } + + for (int i=1;i<27;++i) + + if ( Co[i] ) + + { + + printf("-1\n"); + + return 0; + + } + + + + for (int i=1;i<=N;++i) A[ One[i]-'a'+1 ].push( i ); + + + + for (int i=1;i<=N;++i) + + { + + Rez=0; + + int Last=A[Two[i]-'a'+1].front(); + + + + Query( 1,1,N,1,Last ); + + Solve += Last-i+Rez; + + + + Semn= 1; + + Update( 1,1,N,1 ); + + + + Semn= -1; + + Update( 1,1,N,Last+1 ); + + + + A[Two[i]-'a'+1].pop(); + + } + + + + printf("%d\n",Solve); + +} diff --git a/swap/merge/e6b74612-28cd-49bb-b099-e9d76e568241.cpp b/swap/merge/e6b74612-28cd-49bb-b099-e9d76e568241.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1c9cc08649d1169f7e8f1dad0a3ac4846773bf49 --- /dev/null +++ b/swap/merge/e6b74612-28cd-49bb-b099-e9d76e568241.cpp @@ -0,0 +1,112 @@ +#include +#include +#include + + +using namespace std; + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + +ifstream fin(iname); + +ofstream fout(oname); + +deque Q[300]; + +int i, lg, lg2, n, ANS, sol; + +int j_s1[300], j_s2[300], P[50004]; + +int H[50004 * 3]; + +char s1[50004], s2[50004]; + +inline void update (int n, int l, int r, int p, int v){ + + if (l == r){H[n] = v; return;} + + int m = (l + r) / 2, L = n << 1, R = L | 1; + + if (p <= m) update (L, l, m, p, v); + + else + + update (R, m + 1, r, p, v); + + H[n] = H[L] + H[R]; + +} + +inline int query (int n, int l, int r, int i, int j){ + + if (i <= l && r <= j) return H[n]; + + int m = (l + r) / 2; int sol = 0; + + if (i <= m) sol += query(2 * n, l, m, i, j); + + if (j > m) sol += query(2 * n + 1, m + 1, r, i, j); + + return sol; + +} + +int main() + +{ + + fin >> s1; lg = strlen(s1); + + for (i = 0; i < lg; ++i) + + ++j_s1[(int)s1[i]], + + Q[(int)s1[i]].push_back(i + 1); + + fin >> s2; lg2 = strlen(s2); + + if (lg != lg2){fout << -1 << '\n'; return 0;} + + for (i = 0; i < lg; ++i) + + { + + ++j_s2[(int)s2[i]]; + + P[++n] = Q[s2[i]][0]; + + Q[s2[i]].pop_front(); + + } + + for (i = 0; i < lg; ++i) + + { + + if (j_s1[(int)s1[i]] != j_s2[(int)s1[i]]){ + + fout << -1 << '\n'; return 0; + + } + + } + + update (1, 1, n, P[1], 1); + + for (i = 2; i <= n; ++i) + + { + + update (1, 1, n, P[i], 1); + + ANS += query (1, 1, n, P[i] + 1, n); + + } + + fout << ANS << '\n'; + + return 0; + +} diff --git a/swap/merge/e70c06c8-8e84-4117-87b3-6fb03a103d8b.cpp b/swap/merge/e70c06c8-8e84-4117-87b3-6fb03a103d8b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c4281264594f093ddd264f439950bf4d32b3f56 --- /dev/null +++ b/swap/merge/e70c06c8-8e84-4117-87b3-6fb03a103d8b.cpp @@ -0,0 +1,183 @@ +#include #include +#include +#include + + + + +using namespace std; + + + +int P[50002],P2[50002]; + + + +char S1[50002],S2[50002]; + +int C[32]; + + + +vector< int > V2[ 32 ]; + +bool viz[50002]; + + + +int N,ANS; + + + +bool check_up() + +{ + + int i; + + for(i=0; i= right) + return; + + int mij=(left+right)>>1; + + impera(left,mij); + impera(mij+1,right); + + int i1,i2,i3; + i1=left,i2=mij+1,i3=left-1; + + while( i1<=mij && i2<=right ) + { + if( P[i1]>P[i2] ) + { + ANS+=(mij-i1+1); + P2[++i3]=P[i2]; + ++i2; + } + else + P2[++i3]=P[i1++]; + } + + if(i1<=mij) + { + for(i2=i1; i2<=mij; ++i2) + P2[++i3]=P[i2]; + } + else + { + for(i1=i2; i1<=right; ++i1) + P2[++i3]=P[i1]; + } + + int i; + for(i=left; i<=right; ++i) + P[i]=P2[i]; +} + + + + + + + +int main() + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + + + fgets( S1, 50002, stdin ); + + fgets( S2, 50002, stdin ); + + + + N=strlen(S1)-1; + + + + if( !check_up() ) + + return 0; + + + + int i,aux; + + for(i=0; i + + + + + + +char a[50002],b[50002]; + +int lita[27][50002],litb[27][50002]; + +int P[50002],B[50002]; + +int N,nr,ok; + + + +void merge(int l,int r){ + + int m=l+(r-l)/2,aux; + + if( l==r ) return; + + if( l==r-1 ){ + + if( P[l]>P[r] ) ++nr, aux=P[l],P[l]=P[r],P[r]=aux; + + return; + + } + + + + merge(l,m); + + merge(m+1,r); + + + + int i,j; + + B[0]=0; + + for(i=l,j=m+1; i<=m && j<=r; ) + + if( P[i] <= P[j] ) B[++B[0]]=P[i++]; + + else B[++B[0]]=P[j++], nr+=m-i+1; + + for(; i<=m; ++i) B[++B[0]]=P[i]; + + for(; j<=r; ++j) B[++B[0]]=P[j]; + + + + for(i=l;i<=r;++i) P[i]=B[i-l+1]; + + + +} + + + +void precal_p(){ + + int i,j; + + for(i=0;i<27;++i) + + if( lita[i][0] != litb[i][0] ){ + + ok=0; return; + + } + + else + + for(j=1;j<=lita[i][0];++j) + + P[lita[i][j]]=litb[i][j]; + +} + + + +int main(){ + + int i; + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + scanf("%s%s",a+1,b+1); + + for(i=1; a[i]!='\0' && a[i]!='\n'; ) ++i; + + N=i-1; + + + + for(i=1;i<=N;++i){ + + lita[a[i]-'a'][++lita[a[i]-'a'][0]]=i; + + litb[b[i]-'a'][++litb[b[i]-'a'][0]]=i; + + } + + + + ok=1; + + precal_p(); + + if( ok ) merge(1,N); + + else nr=-1; + + + + printf("%d\n",nr); + + fclose(stdin); fclose(stdout); + + return 0; + +} diff --git a/swap/merge/e80a8d97-fa7b-47b0-8ac2-ce5caacd9560.cpp b/swap/merge/e80a8d97-fa7b-47b0-8ac2-ce5caacd9560.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9541f6bacc447c00697702d8c5bc777e03d37e9f --- /dev/null +++ b/swap/merge/e80a8d97-fa7b-47b0-8ac2-ce5caacd9560.cpp @@ -0,0 +1,155 @@ +#include +#include + + +using namespace std; + + + +int n, i, j, poz, sol, q; + +vector t[30]; + +int ind[30]; + +char s[50100]; + +int v[50100], x[50100], st[130100]; + +int t1[30], t2[30]; + + + +void st_update(int nod, int l, int r, int a, int b, int u) { + + int m = (l + r) / 2; + + if (a <= l && r <= b) { + + st[nod] += u; + + return; + + } + + if (a <= m) + + st_update(2 * nod, l, m, a, b, u); + + if (b > m) + + st_update(2 * nod + 1, m + 1, r, a, b, u); + +} + + + +void st_query(int nod, int l, int r, int poz) { + + int m = (l + r) / 2; + + q += st[nod]; + + if (m == poz && l == r) + + return; + + if (poz <= m) + + st_query(2 * nod, l, m, poz); + + else + + st_query(2 * nod + 1, m + 1, r, poz); + + + +} + + + +int main() { + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(s, 50100, stdin); + + for (i = 0; s[i] != 0; i++) { + + n++; + + v[n] = s[i] - 'a' + 1; + + t1[v[n]]++; + + t[v[n]].push_back(n); + + } + + + + fgets(s, 50100, stdin); + + for (i = 0; s[i] != 0; i++) { + + x[i + 1] = s[i] - 'a' + 1; + + t1[x[i + 1]]--; + + } + + + + for (i = 1; i <= 30; i++) + + if (t1[i] != 0) { + + printf("-1\n"); + + return 0; + + } + + + + + + for (i = 1; i < n; i++) { + + + + q = 0; + + st_query(1, 1, n, t[x[i]][ind[x[i]]]); + + poz = t[x[i]][ind[x[i]]] + q; + + sol += (poz - i); + + st_update(1, 1, n, 1, poz - q, 1); + + ind[x[i]]++; + + if (ind[x[i]] > t[x[i]].size()) { + + printf("-1\n"); + + return 0; + + } + + } + + + + printf("%d\n", sol); + + + + return 0; + +} diff --git a/swap/merge/ea383083-f017-461e-98a9-26bce3c5ef1a.cpp b/swap/merge/ea383083-f017-461e-98a9-26bce3c5ef1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..188dfe6520406b685fed012c8e6a0c8eae563c0b --- /dev/null +++ b/swap/merge/ea383083-f017-461e-98a9-26bce3c5ef1a.cpp @@ -0,0 +1,170 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/ea9e96cb-020d-4f60-bf23-879ccec590df.cpp b/swap/merge/ea9e96cb-020d-4f60-bf23-879ccec590df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..38e9f7a399d6595846b50b7d687f81a93243aff5 --- /dev/null +++ b/swap/merge/ea9e96cb-020d-4f60-bf23-879ccec590df.cpp @@ -0,0 +1,278 @@ +# include +# include +# include + + +const long int MAXN=50000; + + + +typedef struct NOD {long int pos; NOD *next;}; + +typedef struct NODA {long int inf; NODA *stga,*drta;}; + +NODA *rad; + + + +struct {NOD *prim,*ultim;} list[30]; + +long int n; + +char s1[MAXN+10],s2[MAXN+10]; + + + +void add(long int i, long int pos) + +{ + +NOD *p=(NOD*) malloc (sizeof(NOD)); + +(*p).pos=pos;(*p).next=NULL; + +if (list[i].prim) + + { + + (*list[i].ultim).next=p; + + list[i].ultim=p; + + } + +else + + { + + list[i].prim=p; + + list[i].ultim=p; + + } + +} + + + +void citire() + +{ + +FILE *f=fopen("swap.in","r"); + +fgets(s1+1,MAXN+10,f); + +fgets(s2+1,MAXN+10,f); + +n=strlen(s1+1)-1; + +fclose(f); + +} + + + +long int parity() + +{ + +long int count[30]={0}; + +long int i; + +for (i=1;i<=n;i++) + + { + + count[s1[i]-'a'+1]++; + + count[s2[i]-'a'+1]--; + + } + +for (i=1;i<='z'-'a'+1;i++) if (count[i]!=0) return 0; + +return 1; + +} + + + +void create() + +{ + +long int i; + +for (i=1;i<=n;i++) + + add(s1[i]-'a'+1,i); + +} + + + +void update(NODA *p, long int li, long int lf, long int a) + +{ + +long int m=(li+lf)/2; + +if (lf>=li) + + if (li==lf) (*p).inf++; + + else + + { + + if (a<=m) + + { + + (*p).inf++; + + update((*p).stga,li,m,a); + + } + + else + + { + + (*p).inf++; + + update((*p).drta,m+1,lf,a); + + } + + } + +} + + + +long int query(NODA *p,long int li, long int lf, long int a, long int b) + +{ + +long int m=(li+lf)/2; + +if (b>=a) + + { + + if (li==a&&lf==b) return (*p).inf; + + if (b<=m) return query((*p).stga,li,m,a,b); + + if (a>m ) return query((*p).drta,m+1,lf,a,b); + + return query((*p).stga,li,m,a,m)+query((*p).drta,m+1,lf,m+1,b); + + } + +return 0; + +} + + + +long int calculeaza() + +{ + +long int sol=0; + +long int i; + +for (i=1;i<=n;i++) + + { + + + + sol+=(*list[s2[i]-'a'+1].prim).pos-i+query(rad,1,n,(*list[s2[i]-'a'+1].prim).pos+1,n); + + update(rad,1,n,(*list[s2[i]-'a'+1].prim).pos); + + list[s2[i]-'a'+1].prim=(*list[s2[i]-'a'+1].prim).next; + + } + +return sol; + +} + + + +void scrie(long int a) + +{ + +FILE *g=fopen("swap.out","w"); + +fprintf(g,"%ld\n",a); + +fcloseall(); + +} + + + +void aloca(NODA *rad, long int li, long int lf) + +{ + +NODA *p; + +long int m=(li+lf)/2; + +if (li + + + + +using namespace std; + +int aib[50005]; + +char a[50005],b[50005]; + +int n,i,j,aux; + +int x[50005],y[50005]; + +int cat1[150][50005]; + +int cat2[150][50005]; + +int A[150][50005]; + +pair v[50005]; + +long long sol; + +bool ok; + +void interclas(int st,int m,int dr) + +{ + + int k=0,i=st,j=m+1; + + while(i<=m &&j<=dr) + + if(x[i]<=x[j])y[++k]=x[i++]; + + else {y[++k]=x[j++];sol=(sol+m-i+1);} + + while(i<=m)y[++k]=x[i++]; + + while(j<=dr)y[++k]=x[j++]; + + for(k=1,i=st;i<=dr;i++,k++)x[i]=y[k]; + +} + +void sortt(int st,int dr) + +{ + + int m; + + if(st +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + exit(0); + + } + + for(j = 0; j < last[i]; j ++) + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + return 0; + +} diff --git a/swap/merge/eef8ed24-489a-468b-89db-0534e61d6b17.cpp b/swap/merge/eef8ed24-489a-468b-89db-0534e61d6b17.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46539ab3b9985d1f1b0331b97d76af6ea9488513 --- /dev/null +++ b/swap/merge/eef8ed24-489a-468b-89db-0534e61d6b17.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +using namespace std; +const int MAX_N = 50100; +ifstream fin("swap.in"); +ofstream fout("swap.out"); +int alf[27][MAX_N]; +int alfIt[27]; +int v[MAX_N]; +int merge_sort(int, int); +int main() { + string a1, a2; + getline(fin, a1); + getline(fin, a2); + for (unsigned int i = 0; i < a2.size(); ++i) { + int ch = a2[i] - 'a'; + alf[ch][++alf[ch][0]] = i + 1; + } + for (unsigned int i = 0; i < a1.size(); ++i) { + int ch = a1[i] - 'a'; + ++alfIt[ch]; + if (alfIt[ch] > alf[ch][0]) { + fout << -1; + return 0; + } + v[i+1] = alf[ch][alfIt[ch]]; + } + int N = a1.size(); + fout << merge_sort(1, N); + return 0; +} + +int merge_sort(int lo, int hi) { + if (lo == hi) return 0; + int mid = lo + (hi - lo) / 2, numInv; + numInv = merge_sort(lo, mid) + merge_sort(mid + 1, hi); + int aux[MAX_N]; + for (int i = lo, j = mid + 1, k = lo; i <= mid || j <= hi; ++k) { + if ((i <= mid && v[i] < v[j]) || j > hi) { + aux[k] = v[i++]; + } else { + aux[k] = v[j++]; + numInv += (mid - i + 1); + } + } + for (int k = lo; k <= hi; ++k) { + v[k] = aux[k]; + } + return numInv; +} diff --git a/swap/merge/efbf4ddf-907e-4bde-834d-acfdd8d31a6f.cpp b/swap/merge/efbf4ddf-907e-4bde-834d-acfdd8d31a6f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a0658b62970cf652cb4b2bbf6e0cd86eb8583fbc --- /dev/null +++ b/swap/merge/efbf4ddf-907e-4bde-834d-acfdd8d31a6f.cpp @@ -0,0 +1,92 @@ +# include +char a[50005]; + +char b[50005]; + +int r[26]; + +std :: queue < int > v[26]; + +int s[200005],p[50005]; + +int n; + +void update(int p,int u,int v,int nod) + +{ + + if (p == u) s[nod] = 1; + + else + + { + + int m = (p + u) / 2; + + if (v <= m) update(p,m,v,nod*2); + + else update(m+1,u,v,nod*2+1); + + s[nod] = s[nod*2] + s[nod*2+1]; + + } + +} + +int query(int p,int u,int r,int nod) + +{ + + if (u <= r) return s[nod]; + + int ans = 0; + + int m = (p + u) / 2; + + ans += query(p,m,r,nod*2); + + if (m+1<=r) ans += query(m+1,u,r,nod*2+1); + + return ans; + +} + +int main(void) + +{ + + freopen("swap.in","r",stdin); + + freopen("swap.out","w",stdout); + + fgets(a+1, 50001, stdin);fgets(b+1, 50001, stdin); + + n = strlen(a+1); + + for (int i = 1;i <= n;++i) a[i] -= 'a',b[i] -= 'a'; + + for (int i = 1;i <= n;++i) ++r[a[i]],--r[b[i]]; + + for (int i = 0;i < 26;++i) if (r[i]) + + return printf("-1\n"),0; + + for (int i = 1;i <= n;++i) + + v[b[i]].push(i); + + int ans = 0; + + for (int i = 1;i <= n;++i) + + { + + p[i] = n - v[a[i]].front() + 1;v[a[i]].pop(); + + } + + for (int i = 1;i <= n;++i) ans += query(1,n,p[i],1),update(1,n,p[i],1); + + return printf("%d\n",ans),0; + +} diff --git a/swap/merge/f0b4a3e3-4f42-4a09-8cd3-878253947c65.cpp b/swap/merge/f0b4a3e3-4f42-4a09-8cd3-878253947c65.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ac02631102d4deaba49fe0f85e7b1ed6f19508dd --- /dev/null +++ b/swap/merge/f0b4a3e3-4f42-4a09-8cd3-878253947c65.cpp @@ -0,0 +1,152 @@ +#include +#include +#include + + + + + + +using namespace std; + + + +int T[2*(1<<16)], V[50666], N, X[30], Rez, P, Q, Cate; + +char S1[50666], S2[50666]; + +vector A[30]; + + + +void update(int p1, int p2, int nod) + +{ + + int mij; + + + + if (P <= p1 && p2 <= Q) + + { + + T[nod]++; + + return; + + } + + + + if ((p1 <= P && P <= p2) || (p1 <= Q && Q <= p2)) + + { + + mij = (p1+p2)/2; + + update(p1, mij, 2*nod); + + update(mij+1, p2, 2*nod+1); + + } + +} + + + +void query(int x) + +{ + + int nod = (1<<16)+x; + + + + while (nod > 0) + + { + + Cate += T[nod]; + + nod = nod>>1; + + } + +} + + + +int main() + +{ + + int i, n; + + + + freopen("swap.in", "r", stdin); + + freopen("swap.out", "w", stdout); + + + + fgets(S1, 50001, stdin); + + fgets(S2, 50001, stdin); + + + + n = strlen(S1); + + for (i = 0; i < n; i++) + + A[ S1[i]-'a' ].push_back(i); + + + + if (strlen(S1) != n) { printf("-1\n"); return 0; } + + + + for (i = 0; i < n; i++) + + { + + if ((A[ S2[i]-'a' ].size() == 0) || (A[ S2[i]-'a' ].size() == X[ S2[i]-'a' ])) + + { printf("-1\n"); return 0; } + + V[ A[ S2[i]-'a' ][ X[ S2[i]-'a' ]++ ] ] = i; + + i = i+1; i--; + + } + + + + for (i = 0; i < n; i++) + + { + + Cate = 0; + + query(V[i]); + + Rez += (V[i]-Cate); + + P = V[i]+1; Q = n; + + update(0, (1<<16)-1, 1); + + } + + + + printf("%d\n", Rez); + + return 0; + + + +} diff --git a/swap/merge/f0ed1040-87e2-4676-b0df-ab1cad324707.cpp b/swap/merge/f0ed1040-87e2-4676-b0df-ab1cad324707.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ce36f5e3dc56a2f21d90c0f2a99e4547dc825986 --- /dev/null +++ b/swap/merge/f0ed1040-87e2-4676-b0df-ab1cad324707.cpp @@ -0,0 +1,85 @@ +#include +#include + + + + + +using namespace std ; +int n,sol; +int aint[4*50004]; +inline void Update(const int node,const int Left,const int Right,const int pos) +{ + if(Left==Right) + { + aint[node] = 1; + return ; + } + int Middle = ( Left + Right ) >>1; + if(pos<=Middle) + Update(2*node,Left,Middle,pos); + else + Update(2*node+1,Middle+1,Right,pos); + aint[node] = aint[2*node] + aint[2*node+1]; +} +inline void Query(const int node,const int Left,const int Right,const int A,const int B) +{ + if(A <= Left && Right <= B) + { + sol += aint[node]; + return; + } + int Middle = ( Left + Right ) >>1; + if(A<=Middle) + Query(2*node,Left,Middle,A,B); + if(Middle < B) + Query(2*node+1,Middle+1,Right,A,B); +} +int fr0[300],fr1[300], ind[300] , a[50004]; +vector < int > pos[300]; +char s0[50004],s1[50004]; +inline void Read() +{ + ifstream f("swap.in"); + f>> (s0+1) >> (s1+1); + f.close(); +} +inline void Solve() +{ + int i; + for(i = 1;s0[i] ;++i) + ++fr0[s0[i]]; + for(i = 1;s1[i] ;++i) + ++fr1[s1[i]], + pos[s1[i]].push_back(i); + n = i-1; + for(i = 'a';i <= 'z'; ++i) + if(fr0[i]!=fr1[i]) + { + sol = -1; + return ; + } + for(i = 1;i <= n; ++i) + a[i] = pos[s0[i]][ind[s0[i]]++]; + for(i = 1;i <= n; ++i) + { + if(a[i]!=n) + Query(1,1,n,a[i]+1,n); + Update(1,1,n,a[i]); + } +} + +inline void Write() +{ + ofstream g("swap.out"); + g< + + + + + + +using namespace std; + +char a[50005],b[50005]; + +int n,i,j,aux; + +int x[50005],y[50005]; + +int cat1[150][50005]; + +int ap[150]; + +int A[150][50005]; + +pair v[50005]; + +int sol; + +char c; + +void Merge(int st,int mij,int dr) + +{ + + int k=0; + + int i=st,j=mij+1; + + while(i<=mij && j<=dr) + + if(x[i]<=x[j]) + + y[++k]=x[i++]; + + else + + y[++k]=x[j++],sol+=mij+1-i; + + while(i<=mij) + + y[++k]=x[i++]; + + while(j<=dr) + + y[++k]=x[j++]; + + for(k=1,i=st;i<=dr;i++,k++) + + x[i]=y[k]; + +} + +void MergeSort(int st,int dr) + +{ + + int mij; + + if(st +#include +#include +using namespace std; + +ifstream f("swap.in"); ofstream g("swap.out"); + +const int Nmax=50005; + +int n,nr,i,j,p[Nmax],v[Nmax],x[27],y[27]; + +char a[Nmax],b[Nmax]; + +vector L[27]; + +void merge(int st, int dr, int m) + +{ int i=st,j=m+1,k=st; + + while(i<=m && j<=dr) + + if(p[i]>1; + + sort(st,m); + + sort(m+1,dr); + + merge(st,dr,m); + + } + +} + +int main() + +{ f>>a+1>>b+1; + + n=strlen(a+1); + + for(i=1;i<=n;++i) + + { ++x[a[i]-'a']; ++y[b[i]-'a']; + + L[b[i]-'a'].push_back(i); + + } + + int e=0; + + for(i=0;i<26;++i) + + { if(x[i]!=y[i]) e=1; + + x[i]=0; + + } + + if(e) g<<"-1\n"; else + + { + + for(i=1;i<=n;++i) + + { + + j=x[a[i]-'a']++; + + p[i]=L[a[i]-'a'][j]; + + } + + sort(1,n); + + g< +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int i,n,a,b,nr,sol,poz; + +int f1[30],f2[30],v[50005],aint[3*50005]; + +char A[50005],B[50005]; + +deque d[30]; + + + +void update(int nod, int st, int dr){ + + if(st==dr){ + + aint[nod]++; + + return; + + } + + int mij=(st+dr)/2; + + if(poz<=mij) + + update(2*nod,st,mij); + + else + + update(2*nod+1,mij+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + + + +void query(int nod, int st, int dr){ + + if(a<=st && dr<=b){ + + nr+=aint[nod]; + + return; + + } + + int mij=(st+dr)/2; + + if(mij>=a) + + query(2*nod,st,mij); + + if(mij>(A+1)>>(B+1); + + n=strlen(A+1); + + for(i=1;i<=n;i++){ + + f1[A[i]-'a'+1]++; + + f2[B[i]-'a'+1]++; + + d[B[i]-'a'+1].push_back(i); + + } + + for(i=1;i<=26;i++) + + if(f1[i]!=f2[i]){ + + fout<<-1; + + return 0;} + + for(i=1;i<=n;i++){ + + v[i]=d[A[i]-'a'+1].front(); + + d[A[i]-'a'+1].pop_front(); + + } + + for(i=1;i<=n;i++){ + + nr=0; + + a=v[i]+1; b=n; + + query(1,1,n); + + sol+=nr; + + poz=v[i]; + + update(1,1,n); + + } + + fout< +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/f80b3a6d-9043-4a05-aca4-0d95b6186b66.cpp b/swap/merge/f80b3a6d-9043-4a05-aca4-0d95b6186b66.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48ab10d62d7df2146cd5fd460177024a0e9b1d2b --- /dev/null +++ b/swap/merge/f80b3a6d-9043-4a05-aca4-0d95b6186b66.cpp @@ -0,0 +1,271 @@ +#include +#include +#include +#include +using namespace std; + + + +FILE *IN,*OUT; + +int pos=0,out=0,sign,N,Ans=0,Total=0,Start,End,c1[26],c2[26]; + +int first[50005],apear[27],second[26][50005],Val; + +char f[131072],Out[131072],str[10],c; + + + +struct _Tree + +{ + + int Lower,Pos; + +}T[132000]; + +inline void R_Ch(char &ch) + +{ + + ch=f[pos++]; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + +} + +inline void Write_Ch(char ch) + +{ + + Out[out++]=ch; + + if(out==131072) + + fwrite(Out,131072,1,OUT),out=0; + +} + +inline void Write_Int(int nr) + +{ + + int x=0; + + if(nr<0)Write_Ch('-'),nr=-nr; + + do + + { + + str[x++]=nr%10+'0'; + + nr/=10; + + } + + while(nr); + + while(x>0) + + Write_Ch(str[--x]); + +} + +inline int max2(int a,int b) + +{ + + if (a>b) return a; + + else return b; + +} + +inline void Read(int &nr) + +{ + + sign=0; + + nr=0; + + while(f[pos]<'0'||f[pos]>'9') + + { + + if(f[pos]=='-')sign=1; + + pos++; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + while(f[pos]>='0'&&f[pos]<='9') + + { + + nr=10*nr+f[pos++]-'0'; + + if(pos==131072) + + fread(f,131072,1,IN),pos=0; + + } + + if(sign)nr=-nr; + +} + + + +void Update(int node,int start,int end,int V) + +{ + + if(start==end) + + T[node].Pos=V; + + else + + { + + int mid=(start+end)>>1; + + if(V<=mid)Update(2*node,start,mid,V); + + else Update(2*node+1,mid+1,end,V); + + } + +} + +void Query(int node,int start,int end) + +{ + + if(start==end) + + { + + Ans+=T[node].Pos+T[node].Lower; + + return; + + } + + else + + { + + Ans+=T[node].Lower; + + int mid=(start+end)>>1; + + if(Val<=mid)Query(2*node,start,mid); + + else + + { + + T[2*node].Lower++; + + Query(2*node+1,mid+1,end); + + } + + } + +} + +int main() + +{ + + IN=fopen("swap.in","r"); + + OUT=fopen("swap.out","w"); + + + + fread(f,1,131072,IN); + + R_Ch(c); + + while(c!='\n') + + { + + c1[c-'a']++; + + first[++N]=c-'a'; + + R_Ch(c); + + } + + for(int i=1;i<=N;i++) + + { + + R_Ch(c); + + c2[c-'a']++; + + second[c-'a'][++apear[c-'a']]=i; + + Update(1,1,N,i); + + } + + bool possible=true; + + for(int i=0;i<=25&&possible;i++) + + if(c1[i]!=c2[i])possible=false; + + if(!possible) + + { + + Write_Ch('-'); + + Write_Ch('1'); + + } + + else + + { + + memset(apear,0,sizeof apear); + + for(int i=1;i<=N;i++) + + { + + Ans=0; + + Val=second[first[i]][++apear[first[i]]]; + + Query(1,1,N); + + Total+=Ans-i; + + } + + Write_Int(Total); + + } + + if(out>0)fwrite(Out,1,out,OUT); + + return 0; + +} diff --git a/swap/merge/f8d4ecb3-0b66-11ec-b840-2cf05d84bfc2.cpp b/swap/merge/f8d4ecb3-0b66-11ec-b840-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e54c2a77c6d484e9feee6e0aabd2b78f3021a613 --- /dev/null +++ b/swap/merge/f8d4ecb3-0b66-11ec-b840-2cf05d84bfc2.cpp @@ -0,0 +1,208 @@ +#include + + + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int perm[50000], a[4*50000]; + +char s[50000], pattern[50000]; + +int freq[27], evan[50000]; + +vector< int > pos[27]; + +pair v[50000]; + + + +void update(int nod){ + + while(nod!=1){ + + nod/=2; + + a[nod]=a[2*nod]+a[2*nod+1]; + + } + +} + +long long query(int lo, int hi){ + + long long left=0, right=0; + + if(lo==hi){ + + return a[lo]; + + } + + else if(lo a, pair b){ + + return a.second>s>>pattern; + + n=strlen(s); + + while(start + + + + +std::ifstream fin ("swap.in"); + +std::ofstream fout ("swap.out"); + + + +int moves=0; + + + +void merge (std::vector &arr, int left, int right){ + + if (left == right) + + return; + + int mid = (left + right) / 2; + + merge (arr, left, mid); + + merge (arr, mid+1, right); + + + + std::vector aux; + + int i=left, j=mid+1; + + while (i <= mid or j <=right){ + + if (i > mid) + + aux.push_back (arr[j++]), moves = moves + (mid - i + 1); + + else if (j > right) + + aux.push_back (arr[i++]); + + else if (arr[i] <= arr[j]) + + aux.push_back (arr[i++]); + + else + + aux.push_back (arr[j++]), moves = moves + (mid - i + 1); + + } + + for (i=left; i<=right; i++) + + arr[i] = aux[i-left]; + +} + + + +int main() + +{ + + std::string s1, s2; + + + + fin >> s1 >> s2; + + std::vector v(s1.size()); + + std::deque pos[26]; + + for (int i=0; i +#include +#include +using namespace std; + +const int NO_LIT = 'z' - 'a' + 1; +const int LMAX = 50001; +vectorlitere[NO_LIT + 1]; +int left[NO_LIT + 1], aib[LMAX], l; + +char a[LMAX],b[LMAX]; + +int askAIB(int pos) { + int ret = 0; + while (pos > 0) { + ret += aib[pos]; + pos -= pos ^ (pos & (pos - 1)); + } + return ret; +} + +void incrAIB(int pos) { + while (pos <= l) { + aib[pos]++; + pos += pos ^ (pos & (pos - 1)); + } +} + +int solve() { + FILE *fin = fopen("swap.in", "r"); + fscanf (fin, "%s\n", &a); + fscanf (fin, "%s\n", &b); + l = strlen(a); + if (strlen(b) != l) return -1; + + int i; + for (i = 0; i < l; ++i) { + litere[a[i]-'a'].push_back(i); + } + int ret = 0; + for (i = 0; i < l; ++i) { + int litera = b[i] - 'a'; + if (left[litera] == litere[litera].size()) { + return -1; + } + int pos = litere[litera][left[litera]++]; + ret += pos - askAIB(pos); + incrAIB(pos + 1); + } + return ret; +} + +int main() { + fprintf (fopen("swap.out", "w"), "%d", solve()); + return 0; +} diff --git a/swap/merge/fb469337-439b-407d-abfc-9f98d8c2e0cd.cpp b/swap/merge/fb469337-439b-407d-abfc-9f98d8c2e0cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0396582c735c9e2c51829a58515d53c40aca8de7 --- /dev/null +++ b/swap/merge/fb469337-439b-407d-abfc-9f98d8c2e0cd.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; + +const int Q_sze=30; + +const int Maxx=5e4+1; + +deque Q[Q_sze]; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int aint[4*Maxx],vis[4*Maxx]; + +int i,n,ans,len,m; + +string s; + +void build(int node,int left,int right); + +int query(int node,int tot); + +int main() + +{ + + fin>>s; + + n=s.size(); + + for (i=n-1;i>=0;--i) Q[s[i]-96].push_back(i); + + build(1,1,n); + + fin>>s; + + for (i=0;i +#include +#include +#include + + +using namespace std; + + + +ifstream cin ("swap.in"); + +ofstream cout ("swap.out"); +vector< queue > x; + +vector arb; + + + +char a[50001]; + +int n, nr[50001], rez; + + + +void createarb(int node, int st, int dr) + +{ + + if (st == dr) + + { + + arb[node] = 1; + + return; + + } + + + + int mijl = (st + dr) / 2; + + createarb(2 * node, st, mijl); + + createarb(2 * node + 1, mijl + 1, dr); + + arb[node] = arb[2 * node] + arb[2 * node + 1]; + +} + + + +void setpos(int node,int st, int dr, int pos) + +{ + + if (st == dr) + + { + + arb[node] = 0; + + return; + + } + + + + int mijl = (st + dr) /2; + + if (pos <= mijl)setpos(2 * node, st, mijl, pos); + + else setpos(2 * node + 1, mijl + 1, dr, pos); + + arb[node] = arb[2 * node] + arb[2 * node + 1]; + +} + +int query(int node,int st, int dr, int L, int R) + +{ + + if (L <= st && dr <= R) return arb[node]; + + + + int mijl = (st + dr) / 2, s = 0; + + if (L <= mijl)s += query(2 * node, st, mijl, L, R); + + if (R > mijl) s += query(2 * node + 1, mijl + 1, dr, L, R); + + return s; + +} + +int main() + +{ + + cin >> a; + + n = strlen(a); + + + + x = vector< queue >(31); + + + + for (int i = 0; i < n; ++ i) + + x[a[i] - 'a'].push(i); + + + + cin >> a; + + n = strlen(a); + + for (int i = 0; i < n; ++ i) + + nr[a[i] - 'a'] ++; + + + + for (int i = 0; i < 31; ++ i) + + if (nr[i] != x[i].size()) + + { + + cout << -1; + + return 0; + + } + + + + arb = vector(4 * n + 1); + + + + createarb(1, 1, n); + + for (int i = 0, pos; i < n; ++ i) + + { + + pos = x[a[i] - 'a'].front() + 1; + + x[a[i] - 'a'].pop(); + + setpos(1, 1, n, pos); + + rez += query(1, 1, n, 1, pos); + + } + + + + cout << rez; + + return 0; + +} diff --git a/swap/merge/fb8594b3-0b66-11ec-bc08-2cf05d84bfc2.cpp b/swap/merge/fb8594b3-0b66-11ec-bc08-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..526556a623ca8a4f385e60256165e35e7eb270b9 --- /dev/null +++ b/swap/merge/fb8594b3-0b66-11ec-bc08-2cf05d84bfc2.cpp @@ -0,0 +1,101 @@ +#include + +using namespace std; + +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +const int DIM = 5e4 + 7; +const int sigma = 26; + +queue pos_ch[sigma]; +int permutare[DIM]; +int arb[DIM * 4]; + +void build(int pos, int l, int r) +{ + arb[pos] = (r - l + 1); + + if(r != l) + { + int mid = (l + r) / 2; + + build(pos * 2, l, mid); + build(pos * 2 + 1, mid + 1, r); + } +} + +void update(int pos, int l, int r, int x) +{ + if(l == r) + { + arb[pos] = 0; + return ; + } + + int mid = (l + r) / 2; + + if(x <= mid) + update(pos * 2, l, mid, x); + else + update(pos * 2 + 1, mid + 1, r, x); + + arb[pos] = arb[pos * 2 + 1] + arb[pos * 2]; +} + +int query(int pos, int l, int r, int x, int y) +{ + if(x <= l && r <= y) + { + return arb[pos]; + } + + int mid = (l + r) / 2; + + int sum = 0; + + if(x <= mid) + sum += query(pos * 2, l, mid, x, y); + + if(y > mid) + sum += query(pos * 2 + 1, mid + 1, r, x, y); + + return sum; +} + +main() +{ + string s, t; + fin >> s >> t; + + int n = s.size(); + + for(int i = 0; i < n; i++) + pos_ch[s[i] - 'a'].push(i + 1); + + for(int i = 0; i < n ; i++) + { + int id = t[i] - 'a'; + + if(pos_ch[id].empty() == true) + { + fout << -1; + return 0; + } + + permutare[i + 1] = pos_ch[id].front(); + pos_ch[id].pop(); + } + + build(1, 1, n); + + int cost = 0; + + for(int i = 1; i <= n; i++) + { + cost += query(1, 1, n, 1, permutare[i]) - 1; + update(1, 1, n, permutare[i]); + } + + fout << cost; +} diff --git a/swap/merge/fbaa761e-0b66-11ec-986d-2cf05d84bfc2.cpp b/swap/merge/fbaa761e-0b66-11ec-986d-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f33e04fc9c073045a1276ffa8c0e8bd91d7017a8 --- /dev/null +++ b/swap/merge/fbaa761e-0b66-11ec-986d-2cf05d84bfc2.cpp @@ -0,0 +1,128 @@ +#include +#include +#include + + +using namespace std; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + + + +int i,n,a,b,nr,sol,poz; + +int f1[30],f2[30],v[50005],aint[3*50005]; + +char A[50005],B[50005]; + +deque d[30]; + + + +void update(int nod, int st, int dr){ + + if(st==dr){ + + aint[nod]++; + + return; + + } + + int mij=(st+dr)/2; + + if(poz<=mij) + + update(2*nod,st,mij); + + else + + update(2*nod+1,mij+1,dr); + + aint[nod]=aint[2*nod]+aint[2*nod+1]; + +} + + + +void query(int nod, int st, int dr){ + + if(a<=st && dr<=b){ + + nr+=aint[nod]; + + return; + + } + + int mij=(st+dr)/2; + + if(mij>=a) + + query(2*nod,st,mij); + + if(mij>(A+1)>>(B+1); + + n=strlen(A+1); + + for(i=1;i<=n;i++){ + + f1[A[i]-'a'+1]++; + + f2[B[i]-'a'+1]++; + + d[B[i]-'a'+1].push_back(i); + + } + + for(i=1;i<=26;i++) + + if(f1[i]!=f2[i]){ + + fout<<-1; + + return 0;} + + for(i=1;i<=n;i++){ + + v[i]=d[A[i]-'a'+1].front(); + + d[A[i]-'a'+1].pop_front(); + + } + + for(i=1;i<=n;i++){ + + nr=0; + + a=v[i]+1; b=n; + + query(1,1,n); + + sol+=nr; + + poz=v[i]; + + update(1,1,n); + + } + + fout< +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + + + +char a[50005], b[50005]; + +int u1[27], u2[27], fr[27], p[50005], H[3*50005], sol; + +void update(int n, int l, int r, int p, int v) { + + if (l >= r) {H[n] += v; return;} + + int m = (l + r) >> 1; + + if (m >= p) update(n<<1,l,m,p,v); + + else update((n<<1)|1,m+1,r,p,v); + + H[n] = H[n<<1] + H[(n<<1)|1]; + +} + +void query(int n, int l, int r, int a, int b) { + + if (a <= l && r <= b) {sol += H[n]; return;} + + int m = (l + r) >> 1; + + if (a <= m) query(n<<1,l,m,a,b); + + if (b > m) query((n<<1)|1,m+1,r,a,b); + +} + + + +int main() + +{ + + for (int i = 0; i < 27; ++i) fr[i] = -1; + + f >> a >> b; + + int i; + + for (i = 0; a[i]; ++i) ++u1[a[i] - 'a']; + + int n = i; + + for (i = 0; b[i]; ++i) {++u2[b[i] - 'a']; if (fr[b[i]-'a'] == -1) fr[b[i]-'a'] = i;} + + for (i = 0; i < 27; ++i) if (u1[i] != u2[i]) {g << "-1\n"; return 0;} + + for (i = 0; a[i]; ++i) { + + p[i + 1] = fr[a[i]-'a'] + 1; + + ++fr[a[i]-'a']; + + while (b[fr[a[i]-'a']] != a[i]) { + + ++fr[a[i]-'a']; + + if (!b[fr[a[i]-'a']]) fr[a[i]-'a'] = 0; + + } + + } + + for (i = 1; i <= n; ++i) update(1,1,n,p[i],1); + + for (i = 1; i <= n; ++i) { + + if (p[i] > 1) query(1,1,n,1,p[i] - 1); + + update(1,1,n,p[i],-1); + + } + + g << sol << '\n'; + + return 0; + +} diff --git a/swap/merge/fcd0c2ff-0b66-11ec-89a1-2cf05d84bfc2.cpp b/swap/merge/fcd0c2ff-0b66-11ec-89a1-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d3ba3d3c9ed3c821e98fa3dbc7ccb7e010d7631 --- /dev/null +++ b/swap/merge/fcd0c2ff-0b66-11ec-89a1-2cf05d84bfc2.cpp @@ -0,0 +1,240 @@ +#include +using namespace std; + + + +FILE *IN, *OUT; + +int N, Ans, Total, Start, End; + +int c1[26], c2[26]; + +int first[50005], apear[27], second[26][50005], Val; + + + +int pos, out, sign; + +char f[131072], Out[131072], str[10], c; + + + +struct _Tree{ + + int Lower, Pos; + +}T[132000]; + + + +inline void R_Ch(char &ch){ + + ch = f[pos++]; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + +} + + + +inline void Write_Ch(char ch){ + + Out[out++] = ch; + + if(out == 131072) + + fwrite(Out, 131072, 1, OUT), out = 0; + +} + + + +inline void Write_Int(int nr){ + + int x = 0; + + if(nr < 0) Write_Ch('-'), nr =- nr; + + do{ + + str[x++] = nr % 10 + '0'; + + nr /= 10; + + } + + while(nr); + + while(x > 0) + + Write_Ch(str[--x]); + +} + + + +inline int max2(int a,int b){ + + if (a > b) return a; + + else return b; + +} + + + +inline void Read(int &nr){ + + sign = 0; + + nr = 0; + + while(f[pos] < '0' || f[pos] > '9'){ + + if(f[pos] == '-') sign=1; + + pos++; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + while(f[pos] >= '0' && f[pos] <= '9'){ + + nr = 10 * nr + f[pos++] - '0'; + + if(pos == 131072) + + fread(f, 131072, 1, IN), pos = 0; + + } + + if(sign) nr =- nr; + +} + + + +void Update(int node,int start,int end,int V){ + + if(start == end) + + T[node].Pos = V; + + else{ + + int mid = (start + end) >> 1; + + if(V <= mid) Update(2 * node, start, mid, V); + + else Update(2 * node + 1, mid + 1, end, V); + + } + +} + + + +void Query(int node,int start,int end){ + + if(start == end){ + + Ans += T[node].Pos + T[node].Lower; + + return; + + } else { + + Ans += T[node].Lower; + + int mid = (start+end)>>1; + + if(Val <= mid) Query(2 * node, start, mid); + + else{ + + T[2 * node].Lower++; + + Query(2 * node + 1, mid + 1, end); + + } + + } + +} + +int main(){ + + IN = fopen("swap.in", "r"); + + OUT = fopen("swap.out", "w"); + + + + fread(f, 1, 131072, IN); + + R_Ch(c); + + while(c != '\n'){ + + c1[c - 'a']++; + + first[++N] = c - 'a'; + + R_Ch(c); + + } + + for(int i = 1; i <= N; i++){ + + R_Ch(c); + + c2[c - 'a']++; + + second[c - 'a'][++apear[c - 'a']] = i; + + Update(1, 1, N, i); + + } + + bool possible = true; + + for(int i = 0; i <= 25 && possible; i++) + + if(c1[i] != c2[i]) possible = false; + + if(!possible){ + + Write_Ch('-'); + + Write_Ch('1'); + + } else { + + memset(apear, 0, sizeof apear); + + for(int i =1 ; i <= N; i++){ + + Ans = 0; + + Val = second[first[i]][++apear[first[i]]]; + + Query(1, 1, N); + + Total += Ans - i; + + } + + Write_Int(Total); + + } + + if(out > 0) fwrite(Out, 1, out, OUT); + + return 0; + +} diff --git a/swap/merge/fce1b883-33c4-4042-890e-bb01bcf6fc47.cpp b/swap/merge/fce1b883-33c4-4042-890e-bb01bcf6fc47.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d769a5a6f93e6aa37dea4ad115183666d155ef24 --- /dev/null +++ b/swap/merge/fce1b883-33c4-4042-890e-bb01bcf6fc47.cpp @@ -0,0 +1,196 @@ +#include +#include +#include + + + + +using namespace std; + + + +const char iname[] = "swap.in"; + +const char oname[] = "swap.out"; + + + +char s1[50006], s2[50006]; + +int i, j, k, m, n, P[50006], val, sol,oki; + +int sum[3 * 50006], Sigma[27][50006], last[50006], last2[50006], Sigma2[27][50006]; + + + +ifstream fin(iname); + +ofstream fout(oname); + + + +void update(int nod, int left, int right) + +{ + + if(left == right) + + { + + sum[nod] = 1; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(val <= middle) + + update(2 * nod, left, middle); + + else + + update(2 * nod + 1, middle + 1, right); + + } + + sum[nod] = sum[2 * nod] + sum[2 * nod + 1]; + +} + + + +void query(int nod, int left, int right, int lm, int rm) + +{ + + if(lm <= left && right <= rm) + + { + + sol = sol + sum[nod]; + + return; + + } + + else + + { + + int middle = (left + right) >> 1; + + if(lm <= middle) + + query(2 * nod, left, middle, lm, rm); + + if(middle < rm) + + query(2 * nod + 1, middle + 1, right, lm, rm); + + } + +} + + + +int main() + +{ + + oki = 1; + + fin.getline(s1, 50006); + + fin.getline(s2, 50006); + + n = strlen(s1); + + m = strlen(s2); + + m = strlen(s2); + + for(i = 0; i < n; i ++) + + Sigma[s1[i] - 'a'][last[s1[i] - 'a']++] = i + 1; + + + + for(i = 0; i < m; i ++) + + Sigma2[s2[i] - 'a'][last2[s2[i] - 'a']++] = i + 1; + + for(i = 0; i <= 29; i ++) + + { + + if(oki == 1) + + { + + for(j = 0; j < last[i]; j ++) + + { + + if(last[i] != last2[i]) + + { + + fout << "-1\n"; + + oki = 0; + + break; + + } + + P[Sigma[i][j]] = Sigma2[i][j]; + + } + + } + + else + + break; + + } + + + + if(oki == 1) + + { + + for(i = n; i >= 1; i --) + + { + + val = P[i]; + + update(1, 1, n); + + query(1, 1, n, 1, val); + + } + + + + if(sol == 1) + + fout << "1\n"; + + else + + fout << sol - n << "\n"; + + } + + return 0; + +} diff --git a/swap/merge/fcf0d822-0b66-11ec-a529-2cf05d84bfc2.cpp b/swap/merge/fcf0d822-0b66-11ec-a529-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0cf8987f1eb3d0e107fde86ac4896ef0c89d6e52 --- /dev/null +++ b/swap/merge/fcf0d822-0b66-11ec-a529-2cf05d84bfc2.cpp @@ -0,0 +1,178 @@ +#include +using namespace std; + + + +ifstream f("swap.in"); + + + +ofstream g("swap.out"); + + + + + + + +char a[50005], b[50005]; + + + +int u1[27], u2[27], fr[27], p[50005], H[3*50005], sol; + + + +void update(int n, int l, int r, int p, int v) { + + + + if (l >= r) {H[n] += v; return;} + + + + int m = (l + r) >> 1; + + + + if (m >= p) update(n<<1,l,m,p,v); + + + + else update((n<<1)|1,m+1,r,p,v); + + + + H[n] = H[n<<1] + H[(n<<1)|1]; + + + +} + + + +void query(int n, int l, int r, int a, int b) { + + + + if (a <= l && r <= b) {sol += H[n]; return;} + + + + int m = (l + r) >> 1; + + + + if (a <= m) query(n<<1,l,m,a,b); + + + + if (b > m) query((n<<1)|1,m+1,r,a,b); + + + +} + + + + + + + +int main() + + + +{ + + + + for (int i = 0; i < 27; ++i) fr[i] = -1; + + + + f >> a >> b; + + + + int i; + + + + for (i = 0; a[i]; ++i) ++u1[a[i] - 'a']; + + + + int n = i; + + + + for (i = 0; b[i]; ++i) {++u2[b[i] - 'a']; if (fr[b[i]-'a'] == -1) fr[b[i]-'a'] = i;} + + + + for (i = 0; i < 27; ++i) if (u1[i] != u2[i]) {g << "-1\n"; return 0;} + + + + for (i = 0; a[i]; ++i) { + + + + p[i + 1] = fr[a[i]-'a'] + 1; + + + + ++fr[a[i]-'a']; + + + + while (b[fr[a[i]-'a']] != a[i]) { + + + + ++fr[a[i]-'a']; + + + + if (!b[fr[a[i]-'a']]) fr[a[i]-'a'] = 0; + + + + } + + + + } + + + + for (i = 1; i <= n; ++i) update(1,1,n,p[i],1); + + + + for (i = 1; i <= n; ++i) { + + + + if (p[i] > 1) query(1,1,n,1,p[i] - 1); + + + + update(1,1,n,p[i],-1); + + + + } + + + + g << sol << '\n'; + + + + return 0; + + + +} diff --git a/swap/merge/fd7dedb1-0b66-11ec-927a-2cf05d84bfc2.cpp b/swap/merge/fd7dedb1-0b66-11ec-927a-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8cb1e6007b01b73e6a29c9ad64f38bfc064011b0 --- /dev/null +++ b/swap/merge/fd7dedb1-0b66-11ec-927a-2cf05d84bfc2.cpp @@ -0,0 +1,112 @@ +#include + + +using namespace std; + + + + + + + +const int MAXN = 1e5; + + + +char s[MAXN + 1], t[MAXN + 1]; + +vector poz[100]; + +int code (char c) { + + return c - 'a'; + +} + +int nr; + +int p[4 * MAXN + 1], v[4 * MAXN + 1]; + +void build (int st, int dr, int nod) { + + if (st == dr) { + + p[++nr] = nod; + + return; + + } + + int mij = (st + dr) / 2; + + build (st, mij, nod * 2); + + build (mij + 1, dr, nod * 2 + 1); + +} + + + +int take (int nod, int sum) { + + if (nod == 1) + + return sum; + + if (nod % 2 == 0) + + v[nod + 1]++; + + return take (nod / 2, sum + v[nod]); + +} + + + +int main() { + + int n, i, x; + + long long swaps; + + freopen ("swap.in", "r", stdin); + + freopen ("swap.out", "w", stdout); + + scanf ("%s", &s); + + n = strlen (s); + + for (i = n - 1; i >= 0; i--) + + poz[code (s[i])].push_back (i); + + build (1, n, 1); + + scanf ("%s", &t); + + swaps = 0; + + for (i = 0; i < n; i++) { + + if (poz[code(t[i])].size () == 0) { + + printf ("-1\n"); + + return 0; + + } + + x = poz[code(t[i])].back () + 1; + + swaps += x - take (p[x], 0); + + poz[code (t[i])].pop_back (); + + } + + printf ("%lld\n", swaps - n); + + return 0; + +} diff --git a/swap/merge/fd9e2fd2-0b66-11ec-aed6-2cf05d84bfc2.cpp b/swap/merge/fd9e2fd2-0b66-11ec-aed6-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9d2c612599b44de63fd7d36dcab78864515a3fe9 --- /dev/null +++ b/swap/merge/fd9e2fd2-0b66-11ec-aed6-2cf05d84bfc2.cpp @@ -0,0 +1,152 @@ +#include + + + + +using namespace std; + + + +ifstream fin("swap.in") ; + +ofstream fout("swap.out") ; + + + +string sir1,sir2 ; + +int ax[50005] , nrpr[50005] ; + +int n , p[50005] , t[50005] ,inv , v[50005] ; + +vector graf[50005] , graf2[50005] ; + + + +void mergemerge(int lft,int rght) + +{ + + int i , x , y ; + + if ( lft == rght ) + + return ; + + int mij = (lft+rght)/2 ; + + mergemerge(lft,mij) ; + + mergemerge(mij+1,rght) ; + + x = lft ; + + y = mij+1 ; + + i = lft ; + + while ( x <= mij && y <= rght ) + + { + + if ( p[x] <= p[y] ) + + t[i++] = p[x++] ; + + else + + { + + t[i++] = p[y++] ; + + inv = inv +mij-x+1 ; + + } + + } + + while ( x <= mij ) + + t[i++] = p[x++] ; + + while ( y <= rght ) + + t[i++] = p[y++] ; + + for ( i = lft ; i <= rght ; i++ ) + + p[i] = t[i] ; + +} + + + +int main() + +{ + + int i , j ,m; + + fin >> sir1 ; + + fin >> sir2 ; + + sir1 = '#' + sir1 ; + + sir2 = '#' + sir2 ; + + n = sir1.size()-1 ; + + m = sir2.size()-1 ; + + if ( n!= m ) + + { + + fout << "-1" ; + + return 0 ; + + } + + for ( i = 1 ; i <= n ; i++ ) + + nrpr[sir1[i]-'a'+1]++ , nrpr[sir2[i]-'a'+1]-- ; + + for ( i = 1 ; i <= n ; i++ ) + + { + + if ( nrpr[i] != 0 ) + + { + + fout << "-1" ; + + return 0 ; + + } + + } + + for ( i = 1 ; i <= n ; i++ ) + + { + + graf[sir1[i]-'a'+1].push_back(i) ; + + graf2[sir2[i]-'a'+1].push_back(i) ; + + } + + for ( i = 1 ; i <= 27 ; i++ ) + + for ( j = 0 ; j < graf[i].size() ; j++ ) + + p[graf[i][j]] = graf2[i][j] ; + + mergemerge(1,n) ; + + fout << inv ; + +} diff --git a/swap/merge/fdbcc552-0b66-11ec-a7d3-2cf05d84bfc2.cpp b/swap/merge/fdbcc552-0b66-11ec-a7d3-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e2724a16843dcce5c69d990160fd23c508aee3a --- /dev/null +++ b/swap/merge/fdbcc552-0b66-11ec-a7d3-2cf05d84bfc2.cpp @@ -0,0 +1,184 @@ +#include + + + + +using namespace std; + + + +vector h[26]; + +char a[50005], b[50005]; + +int P[50005], n, nrInv; + + + + + +void Interclasare(int p, int m, int q) + +{ + + int i, j, k; + + int b[50005]; + + i = p; j = m + 1; k = 0; + + while (i <= m && j <= q) + + if (P[i] <= P[j]) b[++k] = P[i++]; + + else + + { + + nrInv += (m - i + 1); + + b[++k] = P[j++]; + + } + + while (i <= m) + + b[++k] = P[i++]; + + while (j <= q) + + b[++k] = P[j++]; + + j = p; + + for (i = 1; i <= k; i++) + + P[j++] = b[i]; + +} + + + +void Sch(int i, int j) + +{ + + int aux; + + if (P[i] > P[j]) + + { + + nrInv++; + + aux = P[i]; + + P[i] = P[j]; + + P[j] = aux; + + } + +} + + + +void MergeSort(int p, int q) + +{ + + int m; + + if (q - p <= 1) Sch(p, q); + + else + + { + + m = (p + q) / 2; + + MergeSort(p, m); + + MergeSort(m + 1, q); + + Interclasare(p, m, q); + + } + +} + + + + + +int main() + +{ + + int i, j, n, m; + + ifstream fin("swap.in"); + + ofstream fout("swap.out"); + + fin >> (a + 1); + + fin >> (b + 1); + + fin.close(); + + n = strlen(a+1); + + m = strlen(b+1); + + if (n != m) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + for (i = 1; b[i]; ++i) + + h[b[i]-'a'].push_back(i); + + for (i = n; i >= 1; --i) + + { + + j = a[i] - 'a'; + + if (h[j].empty()) + + { + + fout << "-1\n"; + + fout.close(); + + return 0; + + } + + m = h[j].size(); + + P[i] = h[j][m-1]; + + h[j].pop_back(); + + } + + MergeSort(1, n); + + fout << nrInv << "\n"; + + fout.close(); + + return 0; + +} diff --git a/swap/merge/fde18b6f-0b66-11ec-afd9-2cf05d84bfc2.cpp b/swap/merge/fde18b6f-0b66-11ec-afd9-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0396582c735c9e2c51829a58515d53c40aca8de7 --- /dev/null +++ b/swap/merge/fde18b6f-0b66-11ec-afd9-2cf05d84bfc2.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +using namespace std; + +const int Q_sze=30; + +const int Maxx=5e4+1; + +deque Q[Q_sze]; + +ifstream fin("swap.in"); + +ofstream fout("swap.out"); + +int aint[4*Maxx],vis[4*Maxx]; + +int i,n,ans,len,m; + +string s; + +void build(int node,int left,int right); + +int query(int node,int tot); + +int main() + +{ + + fin>>s; + + n=s.size(); + + for (i=n-1;i>=0;--i) Q[s[i]-96].push_back(i); + + build(1,1,n); + + fin>>s; + + for (i=0;i +#include +#include + +using namespace std; +ifstream fin("swap.in"); +ofstream fout("swap.out"); + +int n,i,j,u,nrn,rez,pfrz[250000],vs[250000]; +string s; +vector pos[300]; + +void make(int nd,int st,int dr) +{ + if(st==dr) + { + pfrz[++nrn]=nd; + return; + } + int md=st+(dr-st)/2; + make(2*nd,st,md); + make(2*nd+1,md+1,dr); +} +int take(int nd,int sum) +{ + if(nd==1) return sum; + if(nd/2*2+1!=nd) vs[nd/2*2+1]++; + return take(nd/2,sum+vs[nd]); +} +int main() { + + getline(fin,s); + n=(int)s.size(); + for(i=n-1;i>=0;i--) + pos[s[i]-96].push_back(i); + + make(1,1,n); + rez=-n; + getline(fin,s); + for(i=0;i +#include +#include +#include +using namespace std; + +ifstream f("swap.in"); + +ofstream g("swap.out"); + +queuev2[27]; + +int n,i,ap[27],v[50001],rasp,v3[50001]; + +char s[50001],s2[50001]; + +void merge_sort(int in,int sf) + +{ + + if(in==sf) + + { + + return; + + } + + int mij=(in+sf)/2; + + merge_sort(in,mij); + + merge_sort(mij+1,sf); + + int i=in; + + int j=mij+1; + + int poz=in; + + while(i<=mij && j<=sf) + + { + + if(v[i]<=v[j]) + + { + + v3[poz]=v[i]; + + i++; + + } + + else + + { + + v3[poz]=v[j]; + + rasp+=j-poz; + + j++; + + } + + poz++; + + } + + while(i<=mij) + + { + + v3[poz]=v[i]; + + i++; + + poz++; + + } + + while(j<=sf) + + { + + v3[poz]=v[j]; + + rasp+=j-poz; + + j++; + + poz++; + + } + + for(i=in;i<=sf;i++) + + { + + v[i]=v3[i]; + + } + + return; + +} + +int main() + +{ + + f>>s; + + n=strlen(s); + + f>>s2; + + for(i=0;i +#include +#include + + +using namespace std; + +ifstream si("swap.in"); + +ofstream so("swap.out"); + + + +int n,u,num,rez,p[250000],vs[250000]; + +string s; + +vector pos[300]; + +void make(int node,int st,int dr) { + + if(st==dr) { + + p[++num]=node; + + return; + + } + + int md=(st+dr)/2; + + make(2*node,st,md); + + make(2*node+1,md+1,dr); + +} + +int take(int node,int sum) { + + if(node==1) + + return sum; + + if(node/2*2+1!=node) + + vs[node/2*2+1]++; + + return take(node/2,sum+vs[node]); + +} + +int main() { + + getline(si,s); + + n=s.size(); + + for(int i=n-1; i>=0; i--) + + pos[s[i]-96].push_back(i); + + make(1,1,n); + + rez=-n; + + getline(si,s); + + for(int i=0; i +using namespace std; + + + +typedef pair pii; + +typedef vector vi; + +typedef long long ll; + +typedef unsigned long long ull; + + + +ifstream f ("swap.in"); + +ofstream g ("swap.out"); + + + +const int NMAX = 50005; + +const int SIGMA = 30; + +int v[NMAX], ans; + +string s; + +queue q[SIGMA]; + + + +struct Node { + + int val, lazy; + + Node() { + + val = lazy = 0; + + } + +}segmTree[4 * NMAX]; + + + +void build (int node, int left, int right) { + + if (left == right) { + + segmTree[node].val = left; + + return; + + } + + int mid = left + (right - left) / 2; + + build (2 * node + 1, left, mid); + + build (2 * node + 2, mid + 1, right); + +} + + + +void update (int node, int left, int right, int x, int y) { + + if (x <= left && right <= y) { + + ++segmTree[node].lazy; + + return; + + } + + int mid = left + (right - left) / 2; + + if (x <= mid) { + + update (2 * node + 1, left, mid, x, y); + + } + + if (mid < y) { + + update (2 * node + 2, mid + 1, right, x, y); + + } + +} + + + +void propagate (int node, int left, int right) { + + if (segmTree[node].lazy) { + + segmTree[node].val += segmTree[node].lazy; + + if (left != right) { + + segmTree[2 * node + 1].lazy += segmTree[node].lazy; + + segmTree[2 * node + 2].lazy += segmTree[node].lazy; + + } + + segmTree[node].lazy = 0; + + } + +} + + + +int query (int node, int left, int right, int pos) { + + propagate (node, left, right); + + + + if (left == right) { + + return segmTree[node].val; + + } + + int mid = left + (right - left) / 2; + + if (pos <= mid) { + + return query (2 * node + 1, left, mid, pos); + + } else { + + return query (2 * node + 2, mid + 1, right, pos); + + } + +} + + + +int main() { + + f >> s; + + int n = s.size(); + + for (int i = 0; i < n; ++i) { + + q[s[i] - 'a'].push (i); + + } + + f >> s; + + for (int i = 0; i < n; ++i) { + + if (q[s[i] - 'a'].empty()) { + + g << "-1\n"; + + return 0; + + } + + v[i] = q[s[i] - 'a'].front(); + + q[s[i] - 'a'].pop(); + + } + + build (0, 0, n - 1); + + for (int i = 0; i < n; ++i) { + + ans += query (0, 0, n - 1, v[i]) - i; + + if (v[i] == 0) continue; + + update (0, 0, n - 1, 0, v[i] - 1); + + } + + + + g << ans << '\n'; + + + + f.close(); + + g.close(); + + return 0; + +} diff --git a/swap/merge/ff5587f6-0b66-11ec-ac6f-2cf05d84bfc2.cpp b/swap/merge/ff5587f6-0b66-11ec-ac6f-2cf05d84bfc2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b171ed3342ad84342fba440df8339adb0bca7943 --- /dev/null +++ b/swap/merge/ff5587f6-0b66-11ec-ac6f-2cf05d84bfc2.cpp @@ -0,0 +1,158 @@ +#include +#include +#include +using namespace std; + +ifstream fi("swap.in"); + +ofstream fo("swap.out"); + +char A[50002],B[50002]; + +int FA[26],FB[26]; + +deque POZB[26]; + +deque :: iterator it; + +int AIB[50002],naib=50000; + + + +long long rez; + + + +void update(int poz, int v) + + + +{ + + while (poz<=naib) + + { + + AIB[poz]+=v; + + poz=poz+(poz&(-poz)); + + } + +} + + + +int f(int poz) + +{ + + int rez; + + rez=0; + + while (poz>=1) + + { + + rez=rez+AIB[poz]; + + poz=poz-(poz&(-poz)); + + } + + return rez; + +} + + + +int query(int st, int dr) + +{ + + return f(dr)-f(st-1); + +} + + + +int main() + +{ + + fi>>A+1; + + fi>>B+1; + + for (int i=1;A[i]!='\0';i++) + + FA[A[i]-'a']++; + + for (int i=1;B[i]!='\0';i++) + + FB[B[i]-'a']++; + + int t=1; + + for (int i=0;i<=25;i++) + + if (FA[i]!=FB[i]) + + t=0; + + if (t==0) + + fo<<-1; + + else + + { + + + + + + for (int i=1;B[i]!='\0';i++) + + POZB[B[i]-'a'].push_back(i); + + int n=strlen(B+1); + + for (int i=1;i<=n;i++) + + update(i,1); + + rez=0; + + for (int i=1;i<=n;i++) + + { + + int c; + + c=A[i]; + + int poz; + + poz=POZB[c-'a'].front(); + + POZB[c-'a'].pop_front(); + + update(poz,-1); + + rez=rez+query(1,poz); + + } + + fo< +#include +#include +using namespace std; + +ifstream fi("swap.in"); + +ofstream fo("swap.out"); + +char A[50002],B[50002]; + +int FA[26],FB[26]; + +deque POZB[26]; + +deque :: iterator it; + +int AIB[50002],naib=50000; + + + +long long rez; + + + +void update(int poz, int v) + + + +{ + + while (poz<=naib) + + { + + AIB[poz]+=v; + + poz=poz+(poz&(-poz)); + + } + +} + + + +int f(int poz) + +{ + + int rez; + + rez=0; + + while (poz>=1) + + { + + rez=rez+AIB[poz]; + + poz=poz-(poz&(-poz)); + + } + + return rez; + +} + + + +int query(int st, int dr) + +{ + + return f(dr)-f(st-1); + +} + + + +int main() + +{ + + fi>>A+1; + + fi>>B+1; + + for (int i=1;A[i]!='\0';i++) + + FA[A[i]-'a']++; + + for (int i=1;B[i]!='\0';i++) + + FB[B[i]-'a']++; + + int t=1; + + for (int i=0;i<=25;i++) + + if (FA[i]!=FB[i]) + + t=0; + + if (t==0) + + fo<<-1; + + else + + { + + + + + + for (int i=1;B[i]!='\0';i++) + + POZB[B[i]-'a'].push_back(i); + + int n=strlen(B+1); + + for (int i=1;i<=n;i++) + + update(i,1); + + rez=0; + + for (int i=1;i<=n;i++) + + { + + int c; + + c=A[i]; + + int poz; + + poz=POZB[c-'a'].front(); + + POZB[c-'a'].pop_front(); + + update(poz,-1); + + rez=rez+query(1,poz); + + } + + fo< +#include +#include +#include + +class InParser { + private: + FILE *fin; + char *buff; + int sp; + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +}; + +const int MAX_N = 30000; +const int INF = 2e9; + +struct Node { + int u; + int d; +}; +std::vector G[1 + MAX_N]; +int dist[1 + MAX_N]; +bool inQ[1 + MAX_N]; + +int main() { + + InParser fin("sate.in"); + freopen("sate.out", "w", stdout); + + int n, m, x, y; + fin >> n >> m >> x >> y; + for (int i = 1; i <= m; i++) { + int u, v, d; + fin >> u >> v >> d; + G[u].push_back(Node{v, d}); + G[v].push_back(Node{u, -d}); + } + for (int i = 1; i <= n; i++) + dist[i] = INF; + dist[x] = 0; + std::queue bfQ; + bfQ.push(Node{x, 0}); + inQ[x] = true; + while (!bfQ.empty() && !inQ[y]) { + int a = bfQ.front().u; + int b = bfQ.front().d; + for (Node e : G[a]) { + if (!inQ[e.u]) { + dist[e.u] = b + e.d; + inQ[e.u] = true; + bfQ.push(Node{e.u, dist[e.u]}); + } + } + bfQ.pop(); + } + + printf("%d\n", dist[y]); + + fclose(stdin); + fclose(stdout); + + return 0; +} diff --git a/villages/bfs/0292394a-94cb-44ee-bd90-ae01f0d41148.cpp b/villages/bfs/0292394a-94cb-44ee-bd90-ae01f0d41148.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b5ec6f00d797062c42f4afe8055d0d6288387d --- /dev/null +++ b/villages/bfs/0292394a-94cb-44ee-bd90-ae01f0d41148.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< +#include +#include +#include +#define lim 100030 +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +vector > muc[lim]; +int cost[lim]; +queue q; +int n,m,x,y,a,b,d,nod; +long long viz[lim]; +int main() +{ + f>>n>>m>>x>>y; + for (int i=1;i<=m;i++) + { + f>>a>>b>>d; + muc[a].push_back({b,d}); + muc[b].push_back({a,(-1)*d}); + } + q.push(x); + while (!q.empty()) + { + nod=q.front(); + q.pop(); + for (int i=0;i + +using namespace std; +bitset<100030>ap; +int n,m,x,y,p1,p2,c; +vector >nod[30005]; +int main() +{ + ifstream fin("sate.in"); + ofstream fout("sate.out"); + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>p1>>p2>>c; + nod[p1].push_back({p2,c}); + nod[p2].push_back({p1,-1*c}); + } + priority_queue >pq; + pq.push({0,x}); + while(!pq.empty()) + { + int pos=pq.top().second; + int dist=pq.top().first; + pq.pop(); + if(ap[pos]) + continue; + ap[pos]=1; + if(pos==y) + { + fout< +#define maxLimit 300001 +using namespace std; + +vector >G[maxLimit]; + +queue coada; + + ifstream fin("sate.in"); + ofstream fout("sate.out"); + + int n, m, x, y, a, b, c, viz[maxLimit], cost[maxLimit]; + +void bfs(int plecare){ + coada.push(plecare); + cost[plecare]=0; + viz[plecare]=1; + int actualNod; + while(!coada.empty()){ + actualNod=coada.front(); + for(int i=0;i>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>a>>b>>c; + G[a].push_back({b,c}); + G[b].push_back({a,(-1)*c}); + } + bfs(x); + fout< + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > mv[30005]; +queue < pair > q; +int viz[30005]; +int val[30005]; +int n,m,x,y,nr; + +int BFS(int v){ + int i; + + viz[v]=1; + q.push(make_pair(v,0)); + + while(!q.empty()){ + v=q.front().first; + q.pop(); + + if(v==y) + return val[v]; + + for(i=0;iv) + val[mv[v][i].first]=val[v]+mv[v][i].second; + else + val[mv[v][i].first]=val[v]-mv[v][i].second; + + q.push(make_pair(mv[v][i].first,val[mv[v][i].first])); + } + } + } +} + +int main(){ + int i,a,b; + + fin>>n>>m>>x>>y; + + for(i=1;i<=m;i++){ + fin>>a>>b>>nr; + + mv[a].push_back(make_pair(b,nr)); + mv[b].push_back(make_pair(a,nr)); + } + + fout< +#include +#include + +std::vector> neighbours[2 + 30000]; +int dist[2 + 30000]; +bool reach[2 + 30000]; + +void bfs(int k, int Y) { + std::queue> q; + q.push({k, 1}); + dist[k] = 1; + while (!q.empty() && !reach[Y]) { + int e = q.front().first; + int d = q.front().second; + for (std::pair p : neighbours[e]) + if (!reach[p.first]) { + reach[p.first] = true; + q.push({p.first, d + p.second}); + dist[p.first] = d + p.second; + } + q.pop(); + } +} +int main() { + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + int n, m, X, Y; + scanf("%d%d%d%d", &n, &m, &X, &Y); + for (int i = 1; i <= m; i++) { + int x, y, d; + scanf("%d%d%d", &x, &y, &d); + neighbours[x].push_back({y, d}); + neighbours[y].push_back({x, -d}); + } + bfs(X, Y); + printf("%d", dist[Y] - 1); + return 0; +} diff --git a/villages/bfs/05ae2e0e-40ad-11ec-b1ad-dc7196033e92.cpp b/villages/bfs/05ae2e0e-40ad-11ec-b1ad-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb0e3ec2e875d3508ed7bb3f7eb8eba8a88ff553 --- /dev/null +++ b/villages/bfs/05ae2e0e-40ad-11ec-b1ad-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,S,F,p,u,x,y,cost; +int c[900010],dist[30010],v[30010]; +vector < pair > L[30010]; +int main(){ + fin>>n>>m>>S>>F; + for(int i=1;i<=m;i++){ + fin>>x>>y>>cost; + L[x].push_back(pair(y,cost)); + L[y].push_back(pair(x,cost)); + } + c[1]=S; + p=u=1; + v[S]=1; + while(p<=u){ + if(c[p]==F) + break; + for(int i=0;i +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int n,m,start,finish,ras; +vector< pair > G[30001]; +queue Q; +int dp[30001]; +void BFS() +{ + Q.push(start); + dp[start]=1; + while(!Q.empty()) + { + int node=Q.front(); + Q.pop(); + for(vector< pair >::iterator it=G[node].begin();it i=*it; + if(dp[i.first]==0) + { + dp[i.first]=dp[node]+i.second; + Q.push(i.first); + } + if(i.first==finish) + return; + } + } +} +int main() +{ + f>>n>>m>>start>>finish; + while(m--) + { + int a,b,v; + f>>a>>b>>v; + G[a].push_back(make_pair(b,v)); + G[b].push_back(make_pair(a,-v)); + } + BFS(); + g< +#define nr first +#define cost second +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y; + +int a, b, c; +vector< pair > l[30005]; +/// (vecin, cost) +bool vizitat[30005]; +long long d[30005]; + +void bfs( int start ) +{ + queue coada; + int nod; + pair vecin; + + coada.push(start); + vizitat[start] = true; + + while( !coada.empty() ) + { + nod = coada.front(); + coada.pop(); + + for( unsigned int i = 0; i < l[nod].size(); i++ ) + { + vecin = l[nod][i]; + + if( vizitat[ vecin.nr ] == false ) + { + coada.push( vecin.nr ); + vizitat[ vecin.nr ] = true; + d[ vecin.nr ] = d[nod] + vecin.cost * ( (vecin.nr > nod)? 1 : -1 ); + } + } + } +} + +int main() +{ + in>>n>>m>>x>>y; + for( int i = 1; i <= m; i++ ) + { + in>>a>>b>>c; + l[a].push_back( make_pair(b, c) ); + l[b].push_back( make_pair(a, c) ); + } + + bfs(x); + + out< +#include +#define DIM 30010 +#define X first +#define Y second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int c[DIM],f[DIM],p,u,i,j,nod,n,m,x,y,tot,d,w[DIM]; +vector< pair >L[DIM]; +int main(){ + fin>>n>>m>>x>>y; + while(fin>>i>>j>>d){ + L[i].push_back(make_pair(j,d)); + L[j].push_back(make_pair(i,-d)); + } + p=1; u=1; c[1]=x; f[x]=1; + while(p<=u){ + nod=c[p]; + for(i=0;i +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y,cnt,k,i,j,p,u,c[30001],f[30001],drum[30001]; + +vector < pair > l[30001]; + +int main(){ + fin>>n>>m>>x>>y; + for(cnt=1;cnt<=m;cnt++){ + fin>>i>>j>>k; + l[i].push_back(make_pair(j,k)); + l[j].push_back(make_pair(i,0-k)); + } + + c[1]=x; + f[x]=1; + p=1; u=1; + + while(f[y]==0 && p<=u){ + for(k=0;k +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< +#include +#include +#include + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y,i,j,nr; +int d[30005]; +vector> adia[30005]; +deque q; + +int main() { + + fin>>n>>m>>x>>y; + while(m--) + { + fin>>i>>j>>nr; + adia[i].push_back({j, nr}); + adia[j].push_back({i, nr}); + } + + d[x]=1; + q.push_back(x); + while(!q.empty()) + { + x=q.front(); + q.pop_front(); + for(auto i: adia[x]) + if(d[i.first]==0) + { + nr=(i.first + +#define MaximumNodes 30005 +#define mp std::make_pair +#define Distance second +#define Node first + +std::ifstream InFile("sate.in"); +std::ofstream OutFile("sate.out"); + + +int N, M, X, Y; +// Assuming no data duplicates +std::vector > Adj[MaximumNodes]; +// Distance[i] - distance from X to i +int Distance[MaximumNodes]; + +// We'll start a BFS from the leftmost given starting index (it'll be X), updating Distance[] on the way +// using the fact that if x = distance from i to j given in the data entry, +// Distance[i] = Distance[j] - distance (j>i) or Distance[j] + distance (i < j) +std::queue BFSQueue; + +void ComputeDistance() { + BFSQueue.push(X); + + while(!BFSQueue.empty()) { + int Index = BFSQueue.front(); + BFSQueue.pop(); + + for (auto Relationship : Adj[Index]) { + if(Distance[Relationship.Node] && Relationship.Node != X); //Distance is known already + else { + Distance[Relationship.Node] = Distance[Index] + Relationship.Distance; + if(Relationship.Node == Y) return; + + BFSQueue.push(Relationship.Node); + } + + } + } +} + +void Initialise() { + if(X > Y) std::swap(X, Y); +} + + +void Citire() { + InFile >> N >> M >> X >> Y; + X--, Y--; + + for (int i=0, x, y, dist; i> x >> y >> dist; + // Because I can + x--, y--; + Adj[x].push_back(mp(y, dist)); + Adj[y].push_back(mp(x, -dist)); + } +} + +void Rezolvare() { + Initialise(); + + ComputeDistance(); + + OutFile << Distance[Y]; +} + +int main() +{ + Citire(); + Rezolvare(); + + return 0; +} diff --git a/villages/bfs/0bf8931d-40ad-11ec-b95e-dc7196033e92.cpp b/villages/bfs/0bf8931d-40ad-11ec-b95e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c118bfdfd32b8074c281a6fec8831a1244228f6e --- /dev/null +++ b/villages/bfs/0bf8931d-40ad-11ec-b95e-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); + +const int NMAX = 30005; +vector > g[NMAX]; +queue > q; +bool visited[NMAX]; + +int main() { + ios::sync_with_stdio(false); + int n, m, x, y; + in >> n >> m >> x >> y; + for (int i = 1; i <= m; i ++) { + int a, b, c; + in >> a >> b >> c; + g[a].push_back({b, c}); + g[b].push_back({a, c}); + } + q.push({x, 0}); + visited[x] = 1; + while (q.size()) { + auto s = q.front(); + q.pop(); + if(s.first == y) { + out << s.second; + return 0; + } + for(auto i : g[s.first]) { + if(!visited[i.first]) { + visited[i.first] = 1; + int aux = s.second; + if(i.first < s.first) + aux -= i.second; + else + aux += i.second; + q.push({i.first, aux}); + } + } + } + return 0; +} diff --git a/villages/bfs/0c2aa389-40ad-11ec-bc86-dc7196033e92.cpp b/villages/bfs/0c2aa389-40ad-11ec-bc86-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e5742120c690a9c4baa16ebf2496e99748732a3a --- /dev/null +++ b/villages/bfs/0c2aa389-40ad-11ec-bc86-dc7196033e92.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#define MAXN 30005 + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n,m,sat_inceput,sat_sfarsit,dp[MAXN]; +bool viz[MAXN]; +vector< pair >graf[MAXN]; +queuestiva; + + +int bfs(){ + stiva.push(sat_inceput); + viz[sat_inceput] = true; + + while(stiva.front() != sat_sfarsit){ + int sat = stiva.front(); + stiva.pop(); + + for(int i = 0; i < graf[sat].size(); ++i){ + int sat_curent = graf[sat][i].first; + if(!viz[sat_curent]){ + stiva.push(sat_curent); + viz[sat_curent] = true; + if(sat_curent != sat_inceput){ + if(sat_curent >= sat) + dp[sat_curent] = dp[sat] + graf[sat][i].second; + else if(sat_curent < sat) + dp[sat_curent] = dp[sat] - graf[sat][i].second; + //cout<<"Distanta de la "<>n>>m>>sat_inceput>>sat_sfarsit; + + int sat1,sat2; + for(int i = 1; i <= m; ++i){ + int lung; + in>>sat1>>sat2>>lung; + graf[sat1].push_back({sat2,lung}); + graf[sat2].push_back({sat1,lung}); + + } + out< +#include +#define NMAX 30000 +#define MMAX 100024 + +using namespace std; + +int n, m, nr, lst[1 + MMAX], urm[1 + 2 * MMAX], viz[1 + 2 * NMAX], s, f, dist[1 + 2 * NMAX]; + +struct M { + int p, d; +}; + +M vf[1 + 2 * MMAX]; + +queue a; +void adauga ( int x, int y, int d ) { + ++nr; + vf[nr].p = y; + vf[nr].d = d; + urm[nr] = lst[x]; + lst[x] = nr; +} + +void bfs ( ) { + while ( !a.empty() ) { + M x; + x.p = a.front(); + a.pop(); + int p = lst[x.p]; + M y; + while ( p ) { + y = vf[p]; + if ( !viz[y.p] ) { + a.push ( y.p ); + if (y.p < x.p) + dist[y.p] = dist[x.p] - y.d; + else + dist[y.p] = dist[x.p] + y.d; + viz[y.p] = 1; + } + + p = urm[p]; + } + } +} + +int main() { + FILE *fin = fopen ( "sate.in", "r" ); + fscanf ( fin, "%d%d%d%d", &n, &m, &s, &f ); + int i, x, y, d; + for ( i = 1; i <= m; ++i ) { + fscanf ( fin, "%d%d%d", &x, &y, &d ); + adauga ( y, x, d ); + adauga ( x, y, d ); + } + fclose ( fin ); + a.push(s); + + bfs(); + + FILE *fout = fopen ( "sate.out", "w" ); + fprintf ( fout, "%d", dist[f]); + fclose ( fout ); + + return 0; +} diff --git a/villages/bfs/0c926c51-40ad-11ec-9340-dc7196033e92.cpp b/villages/bfs/0c926c51-40ad-11ec-9340-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6191c5f4478188175c0adc7a15782b29ea2a6b49 --- /dev/null +++ b/villages/bfs/0c926c51-40ad-11ec-9340-dc7196033e92.cpp @@ -0,0 +1,84 @@ +#include +#include + +#define NMAX 30000 +#define MMAX 100024 +#define CHUNK (1 << 21) + +static struct edge { + int v; + int d; + int next; +} edges[2*MMAX+1]; + +static int adj_list[NMAX+1], dist[NMAX+1], q[NMAX], visited[NMAX+1]; +static char buf[CHUNK]; +static size_t bpos; + +static inline int read_int(void) +{ + int x = 0; + + while ('0' <= buf[bpos] && buf[bpos] <= '9') { + x = x * 10 + (buf[bpos] - '0'); + bpos++; + } + + bpos++; + return x; +} + +int main(void) +{ + int i, n, m, x, y, u, v, s, qt, qh; + + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + read(STDIN_FILENO, buf, CHUNK); + + n = read_int(); + m = read_int(); + x = read_int(); + y = read_int(); + + for (i = 1; i <= m; i++) { + u = read_int(); + v = read_int(); + s = read_int(); + + edges[2*i].v = v; + edges[2*i].d = s; + edges[2*i].next = adj_list[u]; + adj_list[u] = 2*i; + + edges[2*i+1].v = u; + edges[2*i+1].d = s; + edges[2*i+1].next = adj_list[v]; + adj_list[v] = 2*i+1; + } + + qh = qt = 0; + q[qt++] = x; + visited[x] = 1; + + while (qh < qt) { + s = q[qh++]; + for (i = adj_list[s]; i != 0; i = edges[i].next) { + if (!visited[edges[i].v]) { + visited[edges[i].v] = 1; + q[qt++] = edges[i].v; + if (s < edges[i].v) { + dist[edges[i].v] = dist[s] + edges[i].d; + } else { + dist[edges[i].v] = dist[s] - edges[i].d; + } + + if (edges[i].v == y) { + printf("%d\n", dist[edges[i].v]); + return 0; + } + } + } + } +} diff --git a/villages/bfs/0d0a33a7-40ad-11ec-b183-dc7196033e92.cpp b/villages/bfs/0d0a33a7-40ad-11ec-b183-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f8d95c02328f98c77882b38e83f29007c29dcd3 --- /dev/null +++ b/villages/bfs/0d0a33a7-40ad-11ec-b183-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +ifstream in("sate.in"); + +ofstream out("sate.out"); + +int N, M, x, y, s[30003], a[100030], b[100030], d[100030]; + +int main() + +{ + + in >> N >> M >> x >> y; + + s[x] = 1; + + for (int i = 1; i <= M; i++) + + in >> a[i] >> b[i] >> d[i]; + + while (!s[y]) + + { + + for (int i = 1; i <= M; i++) + + { + + if (s[a[i]] && !s[b[i]]) + s[b[i]] = s[a[i]] + d[i]; + + else if (!s[a[i]] && s[b[i]]) + s[a[i]] = s[b[i]] - d[i]; + } + } + + out << s[y] - 1 << "\n"; + + return 0; +} diff --git a/villages/bfs/0d924f79-40ad-11ec-9f34-dc7196033e92.cpp b/villages/bfs/0d924f79-40ad-11ec-9f34-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..41bfac5c9b2727285342ecdac1baae159b547910 --- /dev/null +++ b/villages/bfs/0d924f79-40ad-11ec-9f34-dc7196033e92.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#define i64 long long +using namespace std; +char const in [] = "sate.in"; +char const out [] = "sate.out"; +int const NM = 1e7 + 7 , NM2 = 30007; +struct code +{ + int cost , node; +}; +vector v [NM2]; +queue q; +i64 n , m , x , y , dp [NM2]; +char buffer [NM + 7]; +bool mark [NM2]; +i64 best ; +int point ; +int parsebuff () +{ + int value = 0; + while(! isdigit (buffer [point])) + { + if(point == NM ) + { + point = 0; + fread (buffer , 1 , NM , stdin); + } + ++ point; + } + while(isdigit (buffer [point])) + { + value = value * 10 + (buffer [point] - '0'); + if(point == NM ) + { + point = 0; + fread (buffer , 1 , NM , stdin); + } + ++ point; + } + return value; +} +void buff () +{ + + freopen(in , "r" , stdin); + freopen(out , "w" , stdout); + fread (buffer , 1 , NM , stdin); + n = parsebuff (); + m = parsebuff (); + x = parsebuff (); + y = parsebuff (); + int i; + for(i = 1 ; i <= m ; ++ i) + { + int a , b , c; + code curent; + a = parsebuff (); + b = parsebuff (); + c = parsebuff (); + curent . cost = c; + curent . node = b; + v [a] . push_back (curent); + curent . cost = - c; + curent . node = a; + v [b] . push_back (curent); + } +} +void bfs () +{ + q . push (x); + mark [x] = 1; + while(q . size ()) + { + int curent = q . front () , i , sz = v [curent] . size (); + int minval = 1e9 + 1 , minadd; + bool r = 0; + int val = dp [curent]; + for(i = 0 ; i < sz ; ++ i) + if(! mark [v [curent][i] . node]) + { + q . push (v [curent][i] . node); + mark [v [curent][i] . node] = 1; + dp [v [curent][i] . node] += (v [curent][i] . cost + val); + } + q . pop (); + } + if(! mark [y]) + printf ("-1"); + else + printf ("%d" , dp [y]); +} +int main() +{ + buff (); + bfs (); + return 0; +} diff --git a/villages/bfs/0dd91fed-40ad-11ec-90db-dc7196033e92.cpp b/villages/bfs/0dd91fed-40ad-11ec-90db-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d20bab8496e381aadd1015dbadf2e8eb2535e46 --- /dev/null +++ b/villages/bfs/0dd91fed-40ad-11ec-90db-dc7196033e92.cpp @@ -0,0 +1,2 @@ +#include +using namespace std;ifstream f("sate.in");ofstream g("sate.out");int N,M,x,y,s[30003],a[100030],b[100030],d[100030];int main(){f>>N>>M>>x>>y;s[x]=1;for(int i=1;i<=M;i++)f>>a[i]>>b[i]>>d[i];while(!s[y]){for(int i=1;i<=M;i++){if(s[a[i]]&&!s[b[i]])s[b[i]]=s[a[i]]+d[i];else if(!s[a[i]]&&s[b[i]])s[a[i]]=s[b[i]]-d[i];}}g< +#include +#include +#include +#define NMAX 30000 + +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +int n, m, x, y, rez; +int VIZ[NMAX+5], PRE[NMAX+5]; +vector > VEC[NMAX+5]; +queue Q; + +void citire(void) +{ + int a, b, c; + fi>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + fi>>a>>b>>c; + VEC[a].push_back({b, c}); + VEC[b].push_back({a, -c}); + } +} + +void BFS(int src) +{ + int nod, nods; + VIZ[src]=1; + memset(PRE, -1, sizeof(PRE)); + Q.push(src); + while(!Q.empty()) + { + nod=Q.front(); + Q.pop(); + for(int i=0; i +#include +#include + +using namespace std; + +void citire( int &n, vector< pair >* &la, int &x, int &y, ifstream &f) +{ + int m, it, i, j, cost; + f >> n >> m >> x >> y; + + la = new vector< pair >[n+1]; + + + for(it = 1; it <= m; it++) + { + f >> i >> j >> cost; + la[i].push_back({j, cost}); + la[j].push_back({i, cost}); + } +} + + + +void Bf(const int &n, vector< pair > *la, const int &nod_start, const int &nod_end, int* &dist) +{ + int *viz = new int[n+1](); //initial cu zero + dist = new int[n+1](); + queue c; + + viz[nod_start] = 1; + dist[nod_start] = 1; + + c.push(nod_start); + + while( !c.empty() ) + { + int nod = c.front(); + c.pop(); + + for(unsigned i = 0; i < la[nod].size(); i++) + { + int nod_nou = la[nod][i].first; + int cost_nou = la[nod][i].second; + + if( viz[nod_nou] == 0) + { + viz[nod_nou] = 1; + + if( nod_nou > nod ) + dist[nod_nou] = dist[nod] + cost_nou; + else + dist[nod_nou] = dist[nod] - cost_nou; + + c.push(nod_nou); + } + } + } + +} + +int main() +{ + ifstream fin ("sate.in"); + int n, nod_start, nod_end; + vector< pair > *la; + citire(n, la, nod_start, nod_end, fin); + fin.close(); + + int *km; + Bf(n, la, nod_start, nod_end, km); + + ofstream fout ("sate.out"); + + fout << km[nod_end] - km[nod_start]; + + fout.close(); + + + return 0; +} diff --git a/villages/bfs/108bcc2b-34d0-4f17-a9a4-ba4be073a399.cpp b/villages/bfs/108bcc2b-34d0-4f17-a9a4-ba4be073a399.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b5ec6f00d797062c42f4afe8055d0d6288387d --- /dev/null +++ b/villages/bfs/108bcc2b-34d0-4f17-a9a4-ba4be073a399.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< +#include +#include +using namespace std; + +const int MaxNodesCnt = 3e4 + 5, buffSZ = 4e4 + 5; + +char buff[buffSZ]; +int buffPointer = buffSZ - 1; + +inline void read(int &no){ + + while(!isdigit(buff[buffPointer])) + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + no = 0; + while(isdigit(buff[buffPointer])){ + + no = no * 10 + buff[buffPointer] - '0'; + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + } +} + +struct Graph{ + + int newNode, cost; +}; + +list adjList[MaxNodesCnt]; +int dist[MaxNodesCnt]; +int nodesCnt, edgesCnt, startNode, endNode; + +inline void readData(){ + + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + read(nodesCnt); read(edgesCnt); read(startNode); read(endNode); + + int from, to, cost; + while(edgesCnt--){ + + read(from); read(to); read(cost); + adjList[from].push_back({to, cost}); + adjList[to].push_back({from, -cost}); + } +} + +inline void BFS(int node){ + + deque Queue; + Queue.push_back(node); + + int newNode, cost; + + while(!Queue.empty()){ + + node = Queue.front(); + Queue.pop_front(); + + for(const Graph &nextNode : adjList[node]){ + + newNode = nextNode.newNode; + cost = nextNode.cost; + if(dist[newNode] == 0 and newNode != startNode){ + + dist[newNode] = dist[node] + cost; + Queue.push_back(newNode); + } + } + } +} + +int main(){ + + readData(); + BFS(startNode); + printf("%d", dist[endNode]); +} diff --git a/villages/bfs/123ee163-40ad-11ec-b249-dc7196033e92.cpp b/villages/bfs/123ee163-40ad-11ec-b249-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1b1ddc4d5b50927af735d5dbe91c773bc155999c --- /dev/null +++ b/villages/bfs/123ee163-40ad-11ec-b249-dc7196033e92.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; + +const int MaxNodesCnt = 3e4 + 5, buffSZ = 4e4 + 5; + +char buff[buffSZ]; +int buffPointer = buffSZ - 1; + +inline void read(int &no){ + + while(!isdigit(buff[buffPointer])) + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + no = 0; + while(isdigit(buff[buffPointer])){ + + no = no * 10 + buff[buffPointer] - '0'; + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + } +} + +struct Graph{ + + int newNode, cost; +}; + +vector adjList[MaxNodesCnt]; +int dist[MaxNodesCnt]; +int nodesCnt, edgesCnt, startNode, endNode; + +inline void readData(){ + + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + read(nodesCnt); read(edgesCnt); read(startNode); read(endNode); + + int from, to, cost; + while(edgesCnt--){ + + read(from); read(to); read(cost); + adjList[from].push_back({to, cost}); + adjList[to].push_back({from, -cost}); + } +} + +inline void BFS(int node){ + + deque Queue; + Queue.push_back(node); + + int newNode, cost; + + while(!Queue.empty()){ + + node = Queue.front(); + Queue.pop_front(); + + for(const Graph &nextNode : adjList[node]){ + + newNode = nextNode.newNode; + cost = nextNode.cost; + if(dist[newNode] == 0 and newNode != startNode){ + + dist[newNode] = dist[node] + cost; + Queue.push_back(newNode); + } + } + } +} + +int main(){ + + readData(); + BFS(startNode); + printf("%d", dist[endNode]); +} diff --git a/villages/bfs/12d29f95-40ad-11ec-939c-dc7196033e92.cpp b/villages/bfs/12d29f95-40ad-11ec-939c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afc4f326b550e14fca10c200f631f76c719dc22b --- /dev/null +++ b/villages/bfs/12d29f95-40ad-11ec-939c-dc7196033e92.cpp @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#define Nmax 30005 +#define INF 0x3f3f3f3f + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +queue Q; +vector > v[Nmax]; +int d[Nmax]; +bool seen[Nmax]; +int n, m, X, Y; + +void read() +{ + f >> n >> m >> X >> Y;int x,y,cost; + for ( int i = 1 ; i <= m ; i ++ ) + { + f >> x >> y >> cost; + v[x].push_back(make_pair(cost,y)); + v[y].push_back(make_pair(cost,x)); + } +} + +void BFS() +{ + Q.push(X); + seen[X]=true; + d[X]=1; + while(!Q.empty()) + { + int old_node=Q.front(); + Q.pop(); + for ( int k = 0 ; k < v[old_node].size() ; k ++ ) + { + int new_cost=v[old_node][k].first; + int new_node=v[old_node][k].second; + if ( seen[new_node] == false ) + { + seen[new_node]=true; + if ( new_node > old_node ) + d[new_node]=d[old_node]+new_cost; + else + d[new_node]=d[old_node]-new_cost; + Q.push(new_node); + } + } + } + /*for ( int i = 1 ; i <= n ; i ++ ) + g << d[i] << " "; + g << '\n';*/ +} +void print() +{ + g << d[Y]-d[X]; +} + +int main() +{ + read(); + BFS(); + print(); + return 0; +} diff --git a/villages/bfs/130c01b7-40ad-11ec-b63c-dc7196033e92.cpp b/villages/bfs/130c01b7-40ad-11ec-b63c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed26b18d5e096aaa2d165bb8e4a876eb825cb969 --- /dev/null +++ b/villages/bfs/130c01b7-40ad-11ec-b63c-dc7196033e92.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; +const int maxn=30001; +const int maxm=100025; +queue q; +bool viz[maxn]; +int d[maxn],cap[maxn],nxt[2*maxm],nr; +struct muchie +{ + int nod,cost; +}val[2*maxm]; +inline void add(int a,int b,int c) +{ + val[++nr].nod=b;val[nr].cost=c; + nxt[nr]=cap[a]; + cap[a]=nr; +} +inline void bfs(int x,int y) +{ + int a,b,c,p; + q.push(x);viz[x]=true; + while(!viz[y]) + { + a=q.front();q.pop(); + p=cap[a]; + while(p) + { + b=val[p].nod;c=val[p].cost; + if(!viz[b]) + { + viz[b]=true; + d[b]=d[a]+c; + q.push(b); + } + p=nxt[p]; + } + } +} +int main() +{ + FILE *fin,*fout; + fin=fopen("sate.in","r"); + fout=fopen("sate.out","w"); + int n,m,x,y,a,b,c; + fscanf(fin,"%d%d%d%d",&n,&m,&x,&y); + for(int i=0;i + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +struct muchie +{ + int d; + int c; +}; +const int nx= 30002; +vector < muchie > v[nx]; +int n,m,start,stop; +int dist[nx]; +void bfs () +{ + queue < int > q; + dist[start]=0; + q.push(start); + while(!q.empty()) + { + int i=q.front(); + int d=dist[i]; + q.pop(); + for(vector < muchie > :: iterator it=v[i].begin(); it!=v[i].end(); it++) + if(dist[it->d]==0) + { + if(it->d < i) + dist[it->d]=d-(it->c); + else dist[it->d]=d+(it->c); + if(it->d==stop) + { + out<d); + } + } +} +int main() +{ + in>>n>>m>>start>>stop; + for(;m;m--) + { + int i,j,c; + in>>i>>j>>c; + v[i].push_back({j,c}); + v[j].push_back({i,c}); + } + bfs(); + return 0; +} diff --git a/villages/bfs/13c50d52-40ad-11ec-8383-dc7196033e92.cpp b/villages/bfs/13c50d52-40ad-11ec-8383-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49e92ec65b3a1ea803612128df274325f07f932e --- /dev/null +++ b/villages/bfs/13c50d52-40ad-11ec-8383-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#define nMax 30010 +#define pereche pair +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vectorv[30010]; +queueq; + +int dist[nMax]; +int n,m,xs,ys,a,b,c; +int main() +{ + fin>>n>>m>>xs>>ys; + for(; m; m--) + { + fin>>a>>b>>c; + v[a].push_back({b,c}); + v[b].push_back({a,c}); + } + q.push({dist[xs],xs}); + dist[xs]=-1; + while(q.size()) + { + tie(c,a)=q.front(); + q.pop(); + dist[a]+=c; + if(a==ys)break; + //fout<a) + q.push({dist[a]+vec.second,vec.first}); + else + q.push({dist[a]-vec.second,vec.first}); + } + } + } + fout< + +using namespace std; + + +ifstream f("sate.in"); +ofstream g("sate.out"); + +const int nmax = 30001; + +int n,m,x,y; +vector > adj[nmax]; +vector visited(nmax); +queue q; + +void addedge (vector > adj[], int a, int b, int c) +{ + adj[a].push_back(make_pair(b,c)); + adj[b].push_back(make_pair(a,c)); +} + + + +void citire() +{ + f>>n>>m>>x>>y; + int a,b,c; + int i; + for(i=1;i<=m;i++) + { + f>>a>>b>>c; + addedge(adj,a,b,c); + } +} + +int bfs() +{ + int c = x; + int i; + q.push(c); + visited[c] = 0; + while( !q.empty()) + { + if(visited[y]) + return visited[y]; + c = q.front(); + q.pop(); + for( i = 0; i< adj[c].size(); i++) + { + if(!visited[adj[c][i].first]) + { + if(c < adj[c][i].first) + visited[adj[c][i].first] = visited[c]+adj[c][i].second; + else + visited[adj[c][i].first] = visited[c]-adj[c][i].second; + q.push(adj[c][i].first); + } + + + } + } +} + + + + + +int main() +{ + citire(); + g< +#include +#include +#include + +using namespace std; + +vector > graph[30005]; +queue q; +int dst[30005]; + +int BFS(int s,int d) { + int u; + + dst[s]=0; + q.push(s); + + while (!q.empty()) { + if (dst[d]) + return dst[d]; + + u=q.front(); + for (int v=0;v>n>>m>>s>>d; + + for (int i=0;i>x>>y>>c; + graph[x].push_back(make_pair(y,c)); + graph[y].push_back(make_pair(x,c)); + } + + memset(dst,0,sizeof(dst)); + + out< +#include +#include +#define NMAX 30001 +using namespace std; +vector < pair > v[NMAX]; +long long viz[NMAX]; +int stiv [NMAX]; +int main (){ + FILE *in,*out; + in = fopen ("sate.in","r"); + out = fopen ("sate.out","w"); + int n,m,i,nods,nodf,inc,sf,pp,a,b,l,nod,DIM; + fscanf(in,"%d%d%d%d",&n,&m,&nods,&nodf); + for (i=1;i<=m;i++){ + fscanf(in,"%d%d%d",&a,&b,&l); + v[a].push_back(make_pair(b,l)); + v[b].push_back(make_pair(a,l));/// reciproca + } + + for (i=1;i<=n;i++) + viz[i] = -1LL; + inc = sf = 1; + stiv[inc] = nods; + viz[nods] = 0LL; + pp = 0; + while (inc <= sf && pp == 0){ + nod = stiv[inc]; + DIM = v[nod].size(); + for (i = 0; i < DIM;i ++){ + if (viz[v[nod][i].first] == -1){ + if (v[nod][i].first > nod) + viz[v[nod][i].first] = viz[nod] + v[nod][i].second; + else + viz[v[nod][i].first] = viz[nod] - v[nod][i].second; + stiv[++sf] = v[nod][i].first; + ///if (v[nod][i].first == nodf) + /// pp = 1; + } + } + inc ++; + } + + fprintf(out,"%d",viz[nodf]); + + + + fclose (in); + fclose (out); + return 0; +} + + diff --git a/villages/bfs/15acc4a6-40ad-11ec-8d22-dc7196033e92.cpp b/villages/bfs/15acc4a6-40ad-11ec-8d22-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..681db6f55f9b2534a72166f7e1e4c86436a39a3b --- /dev/null +++ b/villages/bfs/15acc4a6-40ad-11ec-8d22-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +vector< pair< int, int > > G[30010]; +int distante[30010], n, m, sat1, sat2; + +int BFS() { + queue< int > q; q.push(sat1); + + distante[sat1] = 0; + while(!q.empty()) { + if(distante[sat2] != 0) { + return distante[sat2]; + } + + int tempNode = q.front(); q.pop(); + + for(auto vecin: G[tempNode]) { + if(distante[vecin.first] == 0) { + if(tempNode < vecin.first) { + distante[vecin.first] = distante[tempNode] + vecin.second; + } else { + distante[vecin.first] = distante[tempNode] - vecin.second; + } + q.push(vecin.first); + } + } + } + return 0; +} + +int main() { + + in >> n >> m >> sat1 >> sat2; + + for(int i = 1; i <= m; ++i) { + int x, y, d; in >> x >> y >> d; + G[x].push_back({y, d}); + G[y].push_back({x, d}); + } + out << BFS(); + + in.close(); out.close(); + + return 0; +} \ No newline at end of file diff --git a/villages/bfs/15e3b6a0-40ad-11ec-bfec-dc7196033e92.cpp b/villages/bfs/15e3b6a0-40ad-11ec-bfec-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b81d7c469b5d6886f5b92b8ae9efd017b7f0d625 --- /dev/null +++ b/villages/bfs/15e3b6a0-40ad-11ec-bfec-dc7196033e92.cpp @@ -0,0 +1,72 @@ +#include + +using namespace std; + + +ifstream f("sate.in"); +ofstream g("sate.out"); + +const int nmax = 30001; + +int n,m,x,y; +vector > adj[nmax]; +vector visited(nmax); +queue q; + +void addedge (vector > adj[], int a, int b, int c) +{ + adj[a].push_back(make_pair(b,c)); + adj[b].push_back(make_pair(a,c)); +} + + + +void citire() +{ + f>>n>>m>>x>>y; + int a,b,c; + int i; + for(i=1;i<=m;i++) + { + f>>a>>b>>c; + addedge(adj,a,b,c); + } +} + +int bfs() +{ + int c = x; + int i; + q.push(c); + visited[c] = 0; + while( !q.empty()) + { + if(visited[y]) + return visited[y]; + c = q.front(); + q.pop(); + for( i = 0; i< adj[c].size(); i++) + { + if(!visited[adj[c][i].first]) + { + if(c < adj[c][i].first) + visited[adj[c][i].first] = visited[c]+adj[c][i].second; + else + visited[adj[c][i].first] = visited[c]-adj[c][i].second; + q.push(adj[c][i].first); + } + + + } + } +} + + + + + +int main() +{ + citire(); + g< + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +const int N = 30010, M = 100025; + +int n, m, a, b, v[N], x[M], y[M], z[M], i; + +int main() { + fin >> n >> m >> a >> b; + v[a] = 1; + for (i = 1; i <= m; ++i) { + fin >> x[i] >> y[i] >> z[i]; + } + while (!v[b]) { + for (i = 1; i <= m; ++i) { + if (v[ x[i] ] && !v[ y[i] ]) { + v[ y[i] ] = v[ x[i] ] + z[i]; + } + else if (!v[ x[i] ] && v[ y[i] ]) { + v[ x[i] ] = v[ y[i] ] - z[i]; + } + } + } + fout << v[b] - 1; +} diff --git a/villages/bfs/16b115b8-1d23-4239-8c60-ab3eadacf9be.cpp b/villages/bfs/16b115b8-1d23-4239-8c60-ab3eadacf9be.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e88fad93a13f1bba4f7b1168dfccad42dc58b2a8 --- /dev/null +++ b/villages/bfs/16b115b8-1d23-4239-8c60-ab3eadacf9be.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +struct muchie +{ + int d; + int c; +}; +const int nx= 30002; +vector < muchie > v[nx]; +int n,m,start,stop; +int dist[nx]; +void bfs () +{ + queue < int > q; + dist[start]=0; + q.push(start); + while(!q.empty()) + { + int i=q.front(); + int d=dist[i]; + q.pop(); + for(vector < muchie > :: iterator it=v[i].begin(); it!=v[i].end(); it++) + if(dist[it->d]==0) + { + if(it->d < i) + dist[it->d]=d-(it->c); + else dist[it->d]=d+(it->c); + if(it->d==stop) + { + out<d); + } + } +} +int main() +{ + in>>n>>m>>start>>stop; + for(;m;m--) + { + int i,j,c; + in>>i>>j>>c; + v[i].push_back({j,c}); + v[j].push_back({i,c}); + } + bfs(); + return 0; +} diff --git a/villages/bfs/170a9afb-40ad-11ec-9dc2-dc7196033e92.cpp b/villages/bfs/170a9afb-40ad-11ec-9dc2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aba47a33226a2f4002e64f75384a5af4d32b2335 --- /dev/null +++ b/villages/bfs/170a9afb-40ad-11ec-9dc2-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > graf[100100]; +queue Q; +int n,m,x1,ye,x,y,d,viz[30100],dist[30100]; + +void bfs(int q){ + viz[q] = 1; + Q.push(q); + while(!Q.empty()){ + int t = Q.front(); + Q.pop(); + viz[t] = 1; + for (int i=0; i nodc) //daca satul e mai la stanga de precedent, scadem dist + dist[nodc] = dist[t] - dis; + else + dist[nodc] = dist[t] + dis; + Q.push(nodc); + } + if (nodc == ye){ + fout << dist[nodc]; + return; + } + } + } +} + +int main() +{ + fin >> n >> m >> x1 >> ye; + for (int i=0; i> x >> y >> d; + graf[y].push_back(make_pair(x,d)); + graf[x].push_back(make_pair(y,d)); + } + + bfs(x1); + + + return 0; +} diff --git a/villages/bfs/173f127a-40ad-11ec-803b-dc7196033e92.cpp b/villages/bfs/173f127a-40ad-11ec-803b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a637ef5941be14abd3d48c212a06a147f39fd6f --- /dev/null +++ b/villages/bfs/173f127a-40ad-11ec-803b-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +#define DEF 100100 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, start, dest, p, u, viz[DEF]; + +pair < int, int > c[DEF]; + +vector < pair < int, int > > D[DEF]; + +int main () { + fin >> n >> m >> start >> dest; + + if (start > dest) + swap (start, dest); + + for (int i = 1; i <= m; ++ i) { + int x, y, c; + fin >> x >> y >> c; + D[x].push_back (make_pair (y, c)); + D[y].push_back (make_pair (x, - c)); + } + + c[++ u] = make_pair (start, 0); + viz[start] = 1; + while (p <= u) { + int nod = c[p].first; + int cost = c[p].second; + + for (int i = 0; i < D[nod].size (); ++ i) { + if (viz[D[nod][i].first] == 0) { + viz[D[nod][i].first] = 1; + int new_cost = cost + D[nod][i].second; + + c[++ u] = make_pair (D[nod][i].first, new_cost); + + if (D[nod][i].first == dest) { + fout << new_cost; + return 0; + } + } + } + ++ p; + } + + return 0; +} diff --git a/villages/bfs/189bb3b3-40ad-11ec-94cd-dc7196033e92.cpp b/villages/bfs/189bb3b3-40ad-11ec-94cd-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..eebc05f57764f298e00a963b94300828ee182b73 --- /dev/null +++ b/villages/bfs/189bb3b3-40ad-11ec-94cd-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int n, m, A, B, dist[30002], c[30002]; +bool viz[30002]; +vector > l[100030]; +void bfs(int nod) +{ + viz[nod]=1; + int p=1; + int u=1; + c[1]=nod; + while(p<=u) + { + int nodc=c[p]; + for(int i=0;i>n>>m>>A>>B; + for(int i=1;i<=m;i++) + { + int x, y, c; + f>>x>>y>>c; + l[x].push_back(make_pair(y, c)); + l[y].push_back(make_pair(x, c)); + } + bfs(A); + g< + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > graf[100100]; +queue Q; +int n,m,x1,ye,x,y,d,viz[30100],dist[30100]; + +void bfs(int q){ + viz[q] = 1; + Q.push(q); + while(!Q.empty()){ + int t = Q.front(); + Q.pop(); + viz[t] = 1; + for (int i=0; i nodc) //daca satul e mai la stanga de precedent, scadem dist + dist[nodc] = dist[t] - dis; + else + dist[nodc] = dist[t] + dis; + Q.push(nodc); + } + if (nodc == ye){ + fout << dist[nodc]; + return; + } + } + } +} + +int main() +{ + fin >> n >> m >> x1 >> ye; + for (int i=0; i> x >> y >> d; + graf[y].push_back(make_pair(x,d)); + graf[x].push_back(make_pair(y,d)); + } + + bfs(x1); + + + return 0; +} diff --git a/villages/bfs/19a64db6-40ad-11ec-a4bc-dc7196033e92.cpp b/villages/bfs/19a64db6-40ad-11ec-a4bc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e740098bbe1677dba4d42298d66e391e47001d6f --- /dev/null +++ b/villages/bfs/19a64db6-40ad-11ec-a4bc-dc7196033e92.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#define mp make_pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +const int Nmax = 30005; + +queue Q; +vector > A[Nmax]; + +void BFS (int x, int&n, vector&d) +{ + for (int i = 1; i <= n; i++) + d[i] = -1; + d[x] = 0; + + Q.push(x); + + while (!Q.empty()) + { + int k = Q.front(); + Q.pop(); + + for (auto i = 0; i < A[k].size(); i++) + { + int nod = A[k][i].first; + int cost = A[k][i].second; + + if (d[nod] == -1) + { + if (nod > k) + d[nod] = d[k] + cost; + else + d[nod] = d[k] - cost; + Q.push(nod); + } + } + } +} + +int main() +{ + int n, m, start, finish; + vector d(Nmax); + + in >> n >> m >> start >> finish; + + for (int i = 1; i <= m; i++) + { + int x, y, c; + in >> x >> y >> c; + + A[x].push_back(mp(y, c)); + A[y].push_back(mp(x, c)); + } + + BFS(start, n, d); + + out << d[finish]; + return 0; +} diff --git a/villages/bfs/19fa8da9-40ad-11ec-a25c-dc7196033e92.cpp b/villages/bfs/19fa8da9-40ad-11ec-a25c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a4d3e284498d8e0312d845140ddc5c927e0bc32 --- /dev/null +++ b/villages/bfs/19fa8da9-40ad-11ec-a25c-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +#define limit 30005 + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + + +vector < pair< int, int> > G[limit]; +vector < int > d(limit, -1); +queue < int > q; +int N, M, X, Y, Z; +int A, B, C; + +void bfs() +{ + q.push(X); + d[X]=0; + for( ; !q.empty() ; q.pop()) + { + int aux=q.front(); + for(int i = 0 ; i < G[aux].size(); ++ i) + { + int nod = G[aux][i].first; + int cost= G[aux][i].second; + if(d[nod] == -1) + { + d[nod]=d[aux]+cost; + q.push(nod); + } + } + } + +} + +int main() +{ fin>>N>>M>>X>>Y; + + for(int i = 1 ; i <= M ; ++ i) + { + fin>>A>>B>>C; + G[A].push_back(make_pair(B, C)); + G[B].push_back(make_pair(A, -C)); + } + bfs(); + fout< +#define maxLimit 300001 +using namespace std; + +vector >G[maxLimit]; + +queue coada; + + ifstream fin("sate.in"); + ofstream fout("sate.out"); + + int n, m, x, y, a, b, c, viz[maxLimit], cost[maxLimit]; + +void bfs(int plecare){ + coada.push(plecare); + cost[plecare]=0; + viz[plecare]=1; + int actualNod; + while(!coada.empty()){ + actualNod=coada.front(); + for(int i=0;i>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>a>>b>>c; + G[a].push_back({b,c}); + G[b].push_back({a,(-1)*c}); + } + bfs(x); + fout< +#include +#include +#define mp make_pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +const int Nmax = 30005; + +vector > A[Nmax]; + +void BFS (int x, int&n, vector&d, queue&Q) +{ + for (int i = 1; i <= n; i++) + d[i] = -1; + d[x] = 0; + + Q.push(x); + + while (!Q.empty()) + { + int k = Q.front(); + Q.pop(); + + for (auto i = 0; i < A[k].size(); i++) + { + int nod = A[k][i].first; + int cost = A[k][i].second; + + if (d[nod] == -1) + { + if (nod > k) + d[nod] = d[k] + cost; + else + d[nod] = d[k] - cost; + Q.push(nod); + } + } + } +} + +int main() +{ + int n, m, start, finish; + vector d(Nmax); + queue Q; + + in >> n >> m >> start >> finish; + + for (int i = 1; i <= m; i++) + { + int x, y, c; + in >> x >> y >> c; + + A[x].push_back(mp(y, c)); + A[y].push_back(mp(x, c)); + } + + BFS(start, n, d, Q); + + out << d[finish]; + return 0; +} diff --git a/villages/bfs/1a680afb-40ad-11ec-88e3-dc7196033e92.cpp b/villages/bfs/1a680afb-40ad-11ec-88e3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1fd167ba866e87f539172c378a5a9716bf522e81 --- /dev/null +++ b/villages/bfs/1a680afb-40ad-11ec-88e3-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAXN = 30005; +int n, m; +int c1, c2; +vector< pair > v[MAXN]; +queue q; +int s[MAXN]; + +int main() { + + fin >> n >> m; + fin >> c1 >> c2; + for (int i = 1; i <= m; ++i) { + int a, b, c; + fin >> a >> b >> c; + v[a].push_back(make_pair(b, c)); + v[b].push_back(make_pair(a, -c)); + + } + + q.push(c1); + while (!q.empty()) { + int k = q.front(); + for (int i = 0; i < v[k].size(); ++i) { + pair p = v[k][i]; + if (s[p.first] == 0 || s[p.first] > s[k] + p.second) { + s[p.first] = s[k] + p.second; + q.push(p.first); + } + } + q.pop(); + } + + fout << s[c2]; + + + fout.close(); + return 0; +} diff --git a/villages/bfs/1aa020b6-40ad-11ec-a199-dc7196033e92.cpp b/villages/bfs/1aa020b6-40ad-11ec-a199-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47dce23b3c1b1e2f046b6457e54f7fc22bd33fa4 --- /dev/null +++ b/villages/bfs/1aa020b6-40ad-11ec-a199-dc7196033e92.cpp @@ -0,0 +1,82 @@ +#include +#define in "sate.in" +#define out "sate.out" +#define Nmax 30003 +using namespace std; +ifstream fin(in); +ofstream fout(out); + +struct sate +{ + int nod; + int dist; +}; + +int n,m; +int X,Y; +int d[Nmax]; +vector L[Nmax]; +queue q; + +void Read() +{ + int x,y,cost,i; + sate s; + + fin >> n >> m >> X >> Y; + for (i = 1; i <= m; i++) + { + fin >> x >> y >> cost; + + s.nod = y; + s.dist = cost; + L[x].push_back(s); + + s.nod = x; + L[y].push_back(s); + } +} + +void Init() +{ + int i; + + for (i = 1; i <= n; i++) + d[i] = -1; +} + +void BFS(int k) +{ + int nod,cost; + + q.push(k); + d[k] = 0; + while(!q.empty()) + { + k = q.front(); + q.pop(); + for (auto i : L[k]) + { + nod = i.nod; + cost = i.dist; + if (d[nod] == -1) + { + if (nod > k) d[nod] = d[k] + cost; + else d[nod] = d[k] - cost; + q.push(nod); + } + } + } +} + +int main() +{ + Read(); + Init(); + BFS(X); + fout << d[Y] << "\n"; + + fin.close(); + fout.close(); + return 0; +} diff --git a/villages/bfs/1ad5d9e8-40ad-11ec-912f-dc7196033e92.cpp b/villages/bfs/1ad5d9e8-40ad-11ec-912f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ff6b57de7e05751d26d62026d41e6d825a04c93 --- /dev/null +++ b/villages/bfs/1ad5d9e8-40ad-11ec-912f-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#define limit 30005 + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + + +vector < pair< int, int> > G[limit]; +vector < int > d(limit, -1); +queue < int > q; +int N, M, X, Y, Z; +int A, B, C; + +void bfs() +{ + q.push(X); + d[X]=0; + for( ; !q.empty() ; q.pop()) + { + int aux=q.front(); + for(int i = 0 ; i < G[aux].size(); ++ i) + { + int nod = G[aux][i].first; + int cost= G[aux][i].second; + if(d[nod] == -1) + { + d[nod]=d[aux]+cost; + q.push(nod); + } + } + } + +} + +int main() +{ fin>>N>>M>>X>>Y; + + for(int i = 1 ; i <= M ; ++ i) + { + fin>>A>>B>>C; + G[A].push_back(make_pair(B, C)); + G[B].push_back(make_pair(A, -C)); + } + bfs(); + fout< +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,p,y; + +vector > g[30100]; +int v[30100]; + + +void bfs(int x){ + + list q; + + q.push_back(x); + v[x] = 0; + + while(!q.empty()){ + + x = q.front(); + q.pop_front(); + + for(int i = 0; i < g[x].size(); i++){ + if(v[(g[x][i]).first] == -1){ + q.push_back((g[x][i]).first); + + if((g[x][i]).first > x) + v[(g[x][i]).first] = v[x] + (g[x][i]).second; + else + v[(g[x][i]).first] = v[x] - (g[x][i]).second; + } + } + + } + +} + + +int main(){ + fin >> n>>m >>p>>y; + int t1,t2, d; + for(int i = 0; i>t1>>t2>>d; + g[t1].push_back(make_pair(t2, d)); + g[t2].push_back(make_pair(t1, d)); + } + memset(v, -1, sizeof(v)); + bfs(p); + fout << v[y]; +} diff --git a/villages/bfs/1bbe65ca-62d0-40e9-8bff-9420e66dfc9b.cpp b/villages/bfs/1bbe65ca-62d0-40e9-8bff-9420e66dfc9b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c94ca2c6c246987618752c8916e3c88f37d8da3 --- /dev/null +++ b/villages/bfs/1bbe65ca-62d0-40e9-8bff-9420e66dfc9b.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + + + +int x,y,a[100050],b[100051],dist[30002],c[100051],n,m; +int main(){ + + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + scanf("%d%d%d%d",&n,&m,&x,&y); + + for(int i=1;i<=m;i++) + scanf("%d%d%d",&a[i],&b[i],&c[i]); + + dist[x]=1; + while(!dist[y]){ + for(int i=1;i<=m;i++) + { + if(dist[a[i]] && !dist[b[i]]) + dist[b[i]]=dist[a[i]]+c[i]; + else if(!dist[a[i]] && dist[b[i]]) + dist[a[i]]=dist[b[i]]-c[i]; + + + } + } + printf("%d\n",abs(dist[y])-1); + return 0; +} diff --git a/villages/bfs/1be77f67-40ad-11ec-8dab-dc7196033e92.cpp b/villages/bfs/1be77f67-40ad-11ec-8dab-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c695fcc6717aea006d2d521361da06d428d614c4 --- /dev/null +++ b/villages/bfs/1be77f67-40ad-11ec-8dab-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int nodes, edges, X, Y, u, v, cost, visited[30001]; + +vector > adj[30001]; + +int BFS(int start) +{ + queue > q; q.push({start, 0}); + + while(!q.empty()) + { + int current = q.front().first, + cost = q.front().second; + + if(current == Y) return cost; + + q.pop(); + + for(auto child : adj[current]) + { + if(!visited[child.first]) + { + q.push({child.first, child.second + cost}); + + visited[child.first] = 1; + } + } + } +} + +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + scanf("%d %d %d %d", &nodes, &edges, &X, &Y); + + for(int i = 1; i <= edges; i++) + { + scanf("%d %d %d", &u, &v, &cost); + + adj[u].push_back({v, cost}); + + adj[v].push_back({u, -cost}); + } + + printf("%d", BFS(X)); + + return 0; +} diff --git a/villages/bfs/1c1bfefd-40ad-11ec-81d6-dc7196033e92.cpp b/villages/bfs/1c1bfefd-40ad-11ec-81d6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3d8c92901373b463f415a66e594bf9811f5d208 --- /dev/null +++ b/villages/bfs/1c1bfefd-40ad-11ec-81d6-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +typedef pair p; +const int NMAX = 30005; +vector

G[NMAX]; +int N, M, X, Y; +bool use[NMAX]; +queue

q; + +void Read(){ + in >> N >> M >> X >> Y; + for(int i = 1; i <= M; ++i){ + int a, b, d; + in >> a >> b >> d; + G[a].push_back(make_pair(b, d)); + G[b].push_back(make_pair(a, d)); + } + q.push(make_pair(X, 0)); + use[X] = true; +} + +void SolveAndPrint(){ + while(!q.empty()){ + int node = q.front().first; + int cost = q.front().second; + if(node == Y){ + out << cost << "\n"; + break; + } + q.pop(); + for(int i = 0; i < G[node].size(); ++i){ + p neighbour = G[node][i]; + if(!use[neighbour.first]){ + use[neighbour.first] = true; + q.push(make_pair(neighbour.first, + cost + (neighbour.second * (node < neighbour.first ? + 1 : -1)))); + } + } + } +} + +int main(){ + Read(); + SolveAndPrint(); +} diff --git a/villages/bfs/1c542985-40ad-11ec-ad78-dc7196033e92.cpp b/villages/bfs/1c542985-40ad-11ec-ad78-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0c94ca2c6c246987618752c8916e3c88f37d8da3 --- /dev/null +++ b/villages/bfs/1c542985-40ad-11ec-ad78-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + + + +int x,y,a[100050],b[100051],dist[30002],c[100051],n,m; +int main(){ + + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + scanf("%d%d%d%d",&n,&m,&x,&y); + + for(int i=1;i<=m;i++) + scanf("%d%d%d",&a[i],&b[i],&c[i]); + + dist[x]=1; + while(!dist[y]){ + for(int i=1;i<=m;i++) + { + if(dist[a[i]] && !dist[b[i]]) + dist[b[i]]=dist[a[i]]+c[i]; + else if(!dist[a[i]] && dist[b[i]]) + dist[a[i]]=dist[b[i]]-c[i]; + + + } + } + printf("%d\n",abs(dist[y])-1); + return 0; +} diff --git a/villages/bfs/1cce43e9-40ad-11ec-aa6d-dc7196033e92.cpp b/villages/bfs/1cce43e9-40ad-11ec-aa6d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..367c87ac960b113f6027c5a9731a67775cdb868d --- /dev/null +++ b/villages/bfs/1cce43e9-40ad-11ec-aa6d-dc7196033e92.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +struct nod +{ + int x,d; + nod *urm; +}; +nod *gr[30002],*q; +int n,m,X,Y,c[30005],ds[30005],viz[30005],i,x,y,d; +int inf=2000000000; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +void adaug(int x,int y,int d) +{ + nod *p=new nod; + p->x=y; + p->d=d; + p->urm=gr[x]; + gr[x]=p; +} + +int bfs() +{ + int pr=1,ul=1; + c[ul]=X; + for(int i=1;i<=n;i++) + ds[i]=inf; + ds[X]=0; + viz[X]=1; + while(pr<=ul) + { + for(q=gr[c[pr]];q!=NULL;q=q->urm) + { + if(viz[q->x]==0) + { + if(q->x>c[pr]) ds[q->x]=min(ds[q->x],ds[c[pr]]+q->d); + else ds[q->x]=min(ds[q->x],ds[c[pr]]-q->d); + viz[q->x]=1; + ul++; + c[ul]=q->x; + } + } + pr++; + } + return ds[Y]; +} + +int main() +{ + f>>n>>m>>X>>Y; + for(i=1;i<=m;i++) + { + f>>x>>y>>d; + adaug(x,y,d); + adaug(y,x,d); + } + + g< +#include +#include +#include +#include + +#define sat_dist std::pair + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +vector vec[100002]; +queue coada; +int distante[100002]; + +int main() +{ + memset(distante,-1,sizeof(distante)); + int sat,sat_conect, distance,nod; + fin>>n>>m>>x>>y; + for(int i=0;i>sat>>sat_conect>>distance; + vec[sat].push_back(make_pair(sat_conect,distance)); + vec[sat_conect].push_back(make_pair(sat,distance)); + } + distante[x]=0; + coada.push(x); + while(!coada.empty()) + { + nod=coada.front(); + for(int i=0;i +#include +#include +#define nmax 30002 +#define ll long long +#define infinit 30000010 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int dist[nmax]; +int start,x,y,n,m,sfarsit,c; +vector > graf[nmax]; +queue > coada; + +void determinare() +{ + for(int i=0;inod) + { + dist[graf[nod][i].first]=cost+graf[nod][i].second; + } + else + { + dist[graf[nod][i].first]=cost-graf[nod][i].second; + } + coada.push(make_pair(graf[nod][i].first,dist[graf[nod][i].first])); + } + } + } +} + +int main() +{ + fin>>n>>m>>start>>sfarsit; + for(int i=1;i<=m;i++) + { + fin>>x>>y>>c; + pair m; + m.first=y; + m.second=c; + graf[x].push_back(m); + m.first=x; + graf[y].push_back(m); + } + for(int i=1;i<=n;i++) + { + dist[i]=infinit; + } + dist[start]=0; + determinare(); + fout< + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +struct muchie +{ + int d; + int c; +}; +const int nx= 30002; +vector < muchie > v[nx]; +int n,m,start,stop; +int dist[nx]; +void bfs () +{ + queue < int > q; + dist[start]=0; + q.push(start); + while(!q.empty()) + { + int i=q.front(); + int d=dist[i]; + q.pop(); + for(vector < muchie > :: iterator it=v[i].begin(); it!=v[i].end(); it++) + if(dist[it->d]==0) + { + if(it->d < i) + dist[it->d]=d-(it->c); + else dist[it->d]=d+(it->c); + if(it->d==stop) + { + out<d); + } + } +} +int main() +{ + in>>n>>m>>start>>stop; + for(;m;m--) + { + int i,j,c; + in>>i>>j>>c; + v[i].push_back({j,c}); + v[j].push_back({i,c}); + } + bfs(); + return 0; +} diff --git a/villages/bfs/1e6613bd-40ad-11ec-a036-dc7196033e92.cpp b/villages/bfs/1e6613bd-40ad-11ec-a036-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dcd909493d5616955fd8fa3724b292924ae32b4a --- /dev/null +++ b/villages/bfs/1e6613bd-40ad-11ec-a036-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +const int N = 30002; +int d[N], c[N]; +vector < pair > v[N]; +void bfs(int ns){ + int st = 1, dr = 1, sz, nc; + d[ns] = 1; + c[1] = ns; + while(st <= dr){ + nc = c[st]; + sz = v[nc].size(); + for(int i=0;i>n>>m>>xi>>yi; + for(int i=1;i<=m;i++){ + in>>x>>y>>z; + v[x].push_back(make_pair(y,z)); + v[y].push_back(make_pair(x,z)); + } + in.close(); + bfs(xi); + if(xi > yi) + out<<1 - d[yi]<<"\n"; + else + out< +#include +#include +#include +#include + +using namespace std; +#define SatDist pair +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector arr[100030]; +int n, m, x, y, sat, _sat, dist; +queue coada; +int vec[100000]; + +int nod; + +int main(){ + //for(int i = 0; i < 100; i++)vec[i] = -1; + memset(vec,-1,sizeof(vec)); + fin >> n >> m >> x >> y; + for(int i = 0; i < m; i++){ + fin >> sat >> _sat >> dist; + arr[sat].push_back({_sat, dist}); + arr[_sat].push_back({sat, dist}); + } + + //for(int i = 0; i < m; ) + + + vec[x] = 0; + coada.push(x); + while(!coada.empty()){ + nod = coada.front(); + for(int i = 0; i < arr[nod].size(); i++){ + if(vec[arr[nod][i].first] == -1){ + if(nod < arr[nod][i].first) + vec[arr[nod][i].first] = vec[nod] + arr[nod][i].second; + else + vec[arr[nod][i].first] = vec[nod] - arr[nod][i].second; + coada.push(arr[nod][i].first); + } + } + coada.pop(); + } + + fout << vec[y]; + fout.close(); + fin.close(); + return 0; +} diff --git a/villages/bfs/1ecca32f-40ad-11ec-92e1-dc7196033e92.cpp b/villages/bfs/1ecca32f-40ad-11ec-92e1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bfd75ad39cbe95bf1afffc1cbb53e5713068424 --- /dev/null +++ b/villages/bfs/1ecca32f-40ad-11ec-92e1-dc7196033e92.cpp @@ -0,0 +1,20 @@ +#include +#define DIM 100025 +using namespace std; +int sol[30001],st[DIM],dr[DIM],x[DIM]; +int main() +{ int n,i,a,b,m; + ifstream f("sate.in"); + ofstream g("sate.out"); + f>>n>>m>>a>>b; + sol[a]=1; + for (i=1;i<=m;++i) + f>>st[i]>>dr[i]>>x[i]; + while (!sol[b]) { + for (i=1;i<=m;++i) + if (sol[st[i]] && !sol[dr[i]]) sol[dr[i]]=sol[st[i]]+x[i]; + else if (!sol[st[i]] && sol[dr[i]]) sol[st[i]]=sol[dr[i]]-x[i]; + } + g< +#define Nmax 30001 +#define ll long long +#define DIM 70000 +using namespace std; +//ifstream f("sate.in"); +ofstream g("sate.out"); +char buffer[DIM]; +int cursor=DIM-1; +void read(int &x) +{ + x=0; + while(!isdigit(buffer[cursor])) + if(++cursor==DIM) + { + cursor=0; + fread(buffer,1,DIM,stdin); + } + while(isdigit(buffer[cursor])) + { + x=x*10+buffer[cursor]-'0'; + if(++cursor==DIM) + { + cursor=0; + fread(buffer,1,DIM,stdin); + } + } +} +queue q; +ll d[Nmax]; +int n,m,X,Y; +bitset inq; +list > G[Nmax]; +void BF() +{ + int x; + list > :: iterator it; + fill(d+1,d+n+1,LONG_LONG_MAX); + d[X]=0; + q.push(X); + while(!q.empty()) + { + x=q.front(); + q.pop(); + inq[x]=false; + for(it=G[x].begin();it!=G[x].end();++it) + if(d[it->first]>d[x]+it->second) + { + d[it->first]=d[x]+it->second; + if(!inq[it->first]) + { + inq[it->first]=true; + q.push({it->first}); + } + } + } +} +int main() +{ + freopen("sate.in","r",stdin); + read(n); + read(m); + read(X); + read(Y); + int i,x,y,k; + for(i=1;i<=m;i++) + { + read(x); + read(y); + read(k); + G[x].push_back({y,k}); + G[y].push_back({x,-k}); + } + BF(); + g< +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +const int nmax=30005; +int n,m,i,SEEN[nmax],a,b,x,y,c,PATH[nmax]; +vector > V[nmax]; +queue Q; +int main() +{ + fi>>n>>m>>a>>b; + for(i=1;i<=m;i++) + { + fi>>x>>y>>c; + V[x].push_back(make_pair(y,c)); + V[y].push_back(make_pair(x,-c)); + if(x==a) + PATH[y]=c; + } + Q.push(a); + SEEN[a]=1; + PATH[a]=0; + while(!Q.empty()) + { + x=Q.front(); + Q.pop(); + for(i=0;ia) + fo< +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +queue S; +vector > A[30001]; +int n,m,X,Y,x,y,i,d,vf,Viz[30001]; +vector >::iterator it; +int main() +{ + fi>>n>>m>>X>>Y; + for(i=1; i<=m; i++) + { + fi>>x>>y>>d; + A[x].push_back(make_pair(y,d)); + A[y].push_back(make_pair(x,d)); + } + S.push(X); + while(!S.empty()) + { + vf=S.front(); + for(it=A[vf].begin(); it!=A[vf].end(); it++) + { + if(Viz[(*it).first]==0) + { + if((*it).first +#include +#include +#include + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector >v[30001]; +int dist[30001]; +int main() +{ + int p,r,d; + queue q; + int n,m,x,y; + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>p>>r>>d; + v[p].push_back({r,d}); + v[r].push_back({p,d}); + } + dist[x]=0; + q.push(x); + while(!q.empty()) + { + x=q.front(); + q.pop(); + for(int i=0;i +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int dist[100001]; +vector >vec[100001]; +queue < int > q; + +int main() +{ + int N,M,X,Y,i,j,D; + f>>N>>M>>X>>Y; + for(int h=1;h<=M;h++) + { + f>>i>>j>>D; + vec[i].push_back({j,D}); + vec[j].push_back({i,D}); + } + q.push(X); + while(!q.empty()) + { + X=q.front(); + q.pop(); + for(int h=0;h +#include +#include +#include +#include +#define NN 30010 +#include +using namespace std; + +ifstream in("sate.in");ofstream out("sate.out"); + int n , m , ni, nf; +struct nod{ +int dist, info; +nod *next; +}*L[NN]; + + +void adaug(int x , int y,int cost) +{ + //adaug pe y la lista lui x + +nod * aux = new nod; +aux -> info = y; +aux->dist=cost; +aux-> next = L[x]; +L[x]=aux; +} + +void citire() +{ + in >> n >> m>> ni >>nf; + + for(int i = 0 ; i< m ; ++i ) + { + int x, y,c; + in >> x>> y>>c; + adaug(x,y,c); + adaug(y,x,c); + } +} +bool viz[NN]; + +void bfs(int NOD, int D) +{viz[NOD]=true; + if(NOD==nf){out< next) + if(!viz[aux->info]) + { + if(NOD- aux->info >0) + { + D-=aux->dist; + bfs(aux->info,D); + D+=aux->dist;} + else { + D+=aux->dist; + bfs(aux->info,D); + D-=aux->dist; + + } + + + } + } +} + + + + + +int main() +{ citire(); + bfs(ni,0); + + + return 0; +} diff --git a/villages/bfs/20ff83cb-40ad-11ec-89d7-dc7196033e92.cpp b/villages/bfs/20ff83cb-40ad-11ec-89d7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..519d75b2e8b0d17bf4df68352a6a31f33282ca76 --- /dev/null +++ b/villages/bfs/20ff83cb-40ad-11ec-89d7-dc7196033e92.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, X, Y; +vector >> v; +vector d; +vector seen; + +void Read (); +void BFS (int node); + +int main() +{ + Read (); + BFS(X); + fout << d[Y]; + + fin.close(); + fout.close(); + + return 0; +} + +void Read () +{ + fin >> n >> m >> X >> Y; + v = vector >> (n + 1); + seen = vector (n + 1); + d = vector (n + 1); + for (int i = 1; i <= m; i++) + { + int x, y, cost; + fin >> x >> y >> cost; + v[x].push_back({y, cost}); + v[y].push_back({x, -cost}); + } +} + +void BFS (int node) +{ + queue Q; + seen[node] = true; + Q.push(node); + + while (!Q.empty()) + { + int x = Q.front(); + + for (auto it : v[x]) + { + if (!seen[it.first]) + { + seen[it.first] = true; + Q.push(it.first); + d[it.first] = d[x] + it.second; + } + } + Q.pop(); + } +} diff --git a/villages/bfs/219bce1e-40ad-11ec-846d-dc7196033e92.cpp b/villages/bfs/219bce1e-40ad-11ec-846d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df18d6864c842a81f5caa4fa48098515a4fa9e6e --- /dev/null +++ b/villages/bfs/219bce1e-40ad-11ec-846d-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector< pair > graf[30005]; +int dist[30005]; +queue noduri; +int n,m,x,y; + +void bfs() +{ + while(!noduri.empty()) + { + int a=noduri.front(); + noduri.pop(); + for(auto dest:graf[a]) + { + if(dist[dest.first]==0) + { + if(dest.firsta) dist[dest.first] = dist[a]+dest.second; + if(dest.first==y) return; + noduri.push(dest.first); + } + } + } +} + +int main() +{ + int sat1, sat2, d; + f >> n >> m >> x >> y; + + for(int i=1; i<=m; i++) + { + f >> sat1 >> sat2 >> d; + graf[sat1].push_back({sat2,d}); + graf[sat2].push_back({sat1,d}); + } + + for(int i=1; i<=n; i++) + { + dist[i] = 0; + } + dist[x] = 0; + noduri.push(x); + bfs(); + g << dist[y]; + + return 0; +} diff --git a/villages/bfs/222f94d8-40ad-11ec-8810-dc7196033e92.cpp b/villages/bfs/222f94d8-40ad-11ec-8810-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10b285f6c4f4980673b9db1e7d10e6346bb0e809 --- /dev/null +++ b/villages/bfs/222f94d8-40ad-11ec-8810-dc7196033e92.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; + +int nSate, nMuchii, startNod, stopNod, tempNod1, tempNod2, tempD; +vector < pair > graf[30001]; +int distNod[30001]; +bool parcurs[30001]; +queue < int > lee; +ifstream f("sate.in"); +ofstream g("sate.out"); + +void read(){ + f>>nSate>>nMuchii>>startNod>>stopNod; + + for(int i = 0; i>tempNod1>>tempNod2>>tempD; + graf[tempNod1].push_back({tempNod2, tempD}); + graf[tempNod2].push_back({tempNod1, tempD}); + } +} + + + + +void SRCH(int x) +{ + int goThrough = 0, current, nodCurrent, distCurrent; + lee.push(x); + + while( !lee.empty() ){ + current = lee.front(); + lee.pop(); + while(goThrough < graf[current].size()){ + + nodCurrent = graf[current][goThrough].first; + distCurrent = graf[current][goThrough].second; + + if(!parcurs[nodCurrent]){ + parcurs[nodCurrent] = true; + + lee.push(nodCurrent); + + if(current > nodCurrent) + distNod[nodCurrent] = distNod[current] - distCurrent; + else + distNod[nodCurrent] = distNod[current] + distCurrent; + + } + ++goThrough; + } + goThrough = 0; + } +} + +int main() +{ + + + read(); + SRCH(startNod); + + cout< +#include +#include +using namespace std; +#define NMAX 30005 + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct elem{ + int nod, dist; +}; +int n, m, x, y, d[NMAX]; +vector g[NMAX]; +bool viz[NMAX]; + +void citire(){ + int v1, v2, lungime; + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++){ + fin>>v1>>v2>>lungime; + if(v1>v2)lungime=-lungime; + g[v1].push_back({v2,lungime}); + g[v2].push_back({v1,-lungime}); + + } +} + +int bfs(int x,int y){ + queue q; + q.push(x); + viz[x]=1; + while(!q.empty()){ + int v2, v1 = q.front(); + q.pop(); + for(unsigned int i=0;i +#include +#include +#include +#include + +#define INF 0x3f3f3f3f +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +queue q; +vector > ways[30005]; +vector graph[30005]; +int dist[30005]; +int a,b,c; +int n,m,X,Y; +int GetDistance(int a,int b) +{ + for(auto y:ways[a]) + { + if(b== y.first) + return y.second; + } +} + +void lee(int d) +{ + q.push(d); + dist[d] = 0; + while(!q.empty()) + { + int x = q.front(); + q.pop(); + for(auto y:ways[x]) + { + if(dist[y.first] == INF) + { + int k = dist[x] - GetDistance(x,y.first); + int mk = dist[x] + GetDistance(x,y.first); + if(x>y.first) dist[y.first] = k; + else if(x>n>>m>>X>>Y; + for(int i=1;i<=m;++i) + { + fi>>a>>b>>c; + ways[a].push_back({b,c}); + ways[b].push_back({a,c}); + } + for(int i=1;i<=n;++i) + dist[i] = INF; + lee(X); + fo< +#include +#include +#include + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector >v[30001]; +int dist[30001]; +int main() +{ + int p,r,d; + queue q; + int n,m,x,y; + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>p>>r>>d; + v[p].push_back({r,d}); + v[r].push_back({p,d}); + } + dist[x]=0; + q.push(x); + while(!q.empty()) + { + x=q.front(); + q.pop(); + for(int i=0;i +#include +#include +#define DIM 10000 +#define DN 30001 +using namespace std; +int n,m,x,y; +char buff[DIM]; +int poz=0; +vector > V[DN]; +queue Q; +int VIZ[DN]; +FILE *fi,*fo; +void citeste(int &numar) +{ + numar = 0; + //cat timp caracterul din buffer nu e cifra ignor + while (buff[poz] < '0' || buff[poz] > '9') + //daca am "golit" bufferul atunci il umplu + if (++poz == DIM) + fread(buff,1,DIM,fi),poz=0; + //cat timp dau de o cifra recalculez numarul + while ('0'<=buff[poz] && buff[poz]<='9') + { + numar = numar*10 + buff[poz] - '0'; + if (++poz == DIM) + fread(buff,1,DIM,fi),poz=0; + } +} +void bfs(int s) +{ + Q.push(s); + VIZ[s]=1; + while(!Q.empty()) + { + int nod=Q.front(); + Q.pop(); + for(auto to:V[nod]) + if(!VIZ[to.first]) + { + VIZ[to.first]=VIZ[nod]+to.second; + if(to.first +#include +#include +#include +#define NMAX 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n, m, x, y, a, b, d, dist[NMAX], viz[NMAX], inq[NMAX]; +vector < pair > g[NMAX]; +queue q; + +void bfs(int st){ + q.push(st); + dist[st] = 0; + viz[st] = 1; + while(!q.empty()){ + int nod = q.front(); + q.pop(); + inq[nod] = 0; + for(auto it : g[nod]){ + if(!viz[it.first] && !inq[it.first]){ + viz[it.first] = 1; + dist[it.first] = dist[nod] + it.second; + q.push(it.first); + inq[it.first] = 1; + } + } + } +} +int main() +{ + fin >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + fin >> a >> b >> d; + g[a].push_back({b, d}); + g[b].push_back({a, -d}); + } + bfs(x); + fout << dist[y]; + return 0; +} diff --git a/villages/bfs/24d5314e-40ad-11ec-8e5d-dc7196033e92.cpp b/villages/bfs/24d5314e-40ad-11ec-8e5d-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f7639ec71c51c4a12e3d1d9b2fc986935024081 --- /dev/null +++ b/villages/bfs/24d5314e-40ad-11ec-8e5d-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#define NMAX 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n, m, x, y, a, b, d, dist[NMAX], viz[NMAX], inq[NMAX]; +vector < pair > g[NMAX]; +queue q; + +void bfs(int st){ + q.push(st); + dist[st] = 0; + viz[st] = 1; + while(!q.empty()){ + int nod = q.front(); + q.pop(); + inq[nod] = 0; + for(auto it : g[nod]){ + if(!viz[it.first] && !inq[it.first]){ + viz[it.first] = 1; + dist[it.first] = dist[nod] + it.second; + q.push(it.first); + inq[it.first] = 1; + } + } + } +} +int main() +{ + fin >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + fin >> a >> b >> d; + g[a].push_back({b, d}); + g[b].push_back({a, -d}); + } + bfs(x); + fout << dist[y]; + return 0; +} diff --git a/villages/bfs/250c233e-40ad-11ec-8574-dc7196033e92.cpp b/villages/bfs/250c233e-40ad-11ec-8574-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3b3dc910a5dd66d31b8610adcc526b727df2b22e --- /dev/null +++ b/villages/bfs/250c233e-40ad-11ec-8574-dc7196033e92.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +using namespace std; +#define nmax 30005 +queue q; +vector > adiacenta[nmax]; +int n,m,x,y,dist[nmax]; +bool viz[nmax]; + +void input(){ + ifstream fin("sate.in"); + fin>>n>>m>>x>>y; + int n1,n2,d; + while(fin>>n1>>n2>>d){ + adiacenta[n1].push_back({n2,d}); + adiacenta[n2].push_back({n1,d}); + } + fin.close(); +} + +void bfs(int step){ + viz[step] = 1; + dist[step] = 0; + q.push(step); + while(!q.empty()){ + int nod = q.front(); + q.pop(); + for(auto i : adiacenta[nod]){ + if(!viz[i.first]){ + viz[i.first] = 1; + if(nod < i.first){ + dist[i.first] = dist[nod] + i.second; + q.push(i.first); + } + else { + dist[i.first] = dist[nod] - i.second; + q.push(i.first); + } + } + } + + } +} + +int main() +{ + ofstream fout("sate.out"); + input(); + bfs(x); + fout< +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAX = 30005; + +int n, m, x, y, a, b, c; +int dist[MAX]; +bool viz[MAX]; + +vector < pair > G[MAX]; +queue Q; + +void bfs(){ + + Q.push(x); + viz[x] = 1; + dist[x] = 0; + while(!Q.empty()){ + int nod = Q.front(); + Q.pop(); + for(auto it: G[nod]){ + + if(!viz[it.first]){ + + viz[it.first] = 1; + if(nod < it.first){ + Q.push(it.first); + dist[it.first] = dist[nod] + it.second; + } + else{ + Q.push(it.first); + dist[it.first] = dist[nod] - it.second; + } + } + } + } +} + +int main() +{ + fin >> n >> m >> x >> y; + + for(int i = 1; i <= m; ++i){ + fin >> a >> b >> c; + G[a].push_back(make_pair(b, c)); + G[b].push_back(make_pair(a, c)); + } + + bfs(); + + fout << dist[y]; + + return 0; +} diff --git a/villages/bfs/25b5dbf3-40ad-11ec-8b1c-dc7196033e92.cpp b/villages/bfs/25b5dbf3-40ad-11ec-8b1c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b85ef3f8a9e7c06a0e28e8486c86a02dd5cf8aa2 --- /dev/null +++ b/villages/bfs/25b5dbf3-40ad-11ec-8b1c-dc7196033e92.cpp @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#define MAXN 30005 +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +vector > G[MAXN]; +queue + + Q; +int n, m, dist[MAXN]; +bool viz[MAXN]; + +void bfs(int start) +{ + viz[start] = 1; + dist[start] = 0; + Q.push(start); + while(!Q.empty()) + { + int nod = Q.front(); + Q.pop(); + for(auto it:G[nod]) + { + if(!viz[it.first]) + { + viz[it.first] = 1; + if(nod < it.first) + { + Q.push(it.first); + dist[it.first] = dist[nod] + it.second; + } + else + { + Q.push(it.first); + dist[it.first] = dist[nod] - it.second; + } + } + + } + } +} + +int main() +{ + int n, m, X, Y, d, x, y; + f >> n >> m >> X >> Y; + for(int i = 1; i <= m; i++) + { + f >> x >> y >> d; + G[x].push_back(make_pair(y, d)); + G[y].push_back(make_pair(x, d)); + } + bfs(X); + + g << dist[Y]; + return 0; +} diff --git a/villages/bfs/25dd9137-e9d7-48d2-beeb-869693360676.cpp b/villages/bfs/25dd9137-e9d7-48d2-beeb-869693360676.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bfd75ad39cbe95bf1afffc1cbb53e5713068424 --- /dev/null +++ b/villages/bfs/25dd9137-e9d7-48d2-beeb-869693360676.cpp @@ -0,0 +1,20 @@ +#include +#define DIM 100025 +using namespace std; +int sol[30001],st[DIM],dr[DIM],x[DIM]; +int main() +{ int n,i,a,b,m; + ifstream f("sate.in"); + ofstream g("sate.out"); + f>>n>>m>>a>>b; + sol[a]=1; + for (i=1;i<=m;++i) + f>>st[i]>>dr[i]>>x[i]; + while (!sol[b]) { + for (i=1;i<=m;++i) + if (sol[st[i]] && !sol[dr[i]]) sol[dr[i]]=sol[st[i]]+x[i]; + else if (!sol[st[i]] && sol[dr[i]]) sol[st[i]]=sol[dr[i]]-x[i]; + } + g< +#include +#include +using namespace std; + +ifstream fi ("sate.in"); +ofstream fo ("sate.out"); +int n, m, x, y, dist[DM], a, b, d; +queue q; +vector > v[DM]; + +int main() +{ + fi >> n >> m >> x >> y; + for (int i = 1; i <= m; ++i) + { + fi >> a >> b >> d; + if (a > b) + swap(a, b); + v[a].push_back(make_pair(b, d)); + v[b].push_back(make_pair(a, -d)); + } + for (int i = 1; i <= n; ++i) + dist[i] = (1<<30); + dist[x] = 0; + q.push(x); + while(!q.empty()) + { + a = q.front(); + q.pop(); + for (int i = 0; i < v[a].size(); ++i) + if (dist[v[a][i].first] > dist[a] + v[a][i].second) + { + dist[v[a][i].first] = dist[a] + v[a][i].second; + q.push(v[a][i].first); + } + } + if (dist[y] < 0) + fo << -dist[y]; + else + fo << dist[y]; + return 0; +} \ No newline at end of file diff --git a/villages/bfs/26f613f8-40ad-11ec-b937-dc7196033e92.cpp b/villages/bfs/26f613f8-40ad-11ec-b937-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0232dcad963c8e0e034dd81162bdd53fa1f4cff4 --- /dev/null +++ b/villages/bfs/26f613f8-40ad-11ec-b937-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +#define Nmax 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int d[Nmax],viz[Nmax],a,b,D,x,y,N,M,inq[Nmax]; +vector >v[Nmax]; +queueq; +void parc(int nod) +{ + viz[nod] = 1; + for(int i = 0 ; i < v[nod].size(); i++) + { + if(viz[v[nod][i].first] != 1) + { + int o = v[nod][i].first;//de unde pot ajunge in i + if(o < nod) + d[o] = d[nod] - v[nod][i].second; + else d[o] = d[nod] + v[nod][i].second; + if(!inq[v[nod][i].first]) + { + q.push(v[nod][i].first); + inq[v[nod][i].first] = 1; + } + } + } +} +int main() +{ + fin >> N >> M >> x >> y; + for(int i = 1; i <= M; i++) + { + fin >> a >> b >> D; + v[b].push_back({a,D}); + v[a].push_back({b,D}); + + } + q.push(x); + inq[x] = 1; + while(!q.empty()) + { + int p = q.front(); + parc(p); + q.pop(); + inq[p] = 0; + } + fout << d[y]; + return 0; +} diff --git a/villages/bfs/273e1dde-40ad-11ec-b869-dc7196033e92.cpp b/villages/bfs/273e1dde-40ad-11ec-b869-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7758a37b962ebfd41bffe182ff6e25184598a3c --- /dev/null +++ b/villages/bfs/273e1dde-40ad-11ec-b869-dc7196033e92.cpp @@ -0,0 +1,57 @@ +#include + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +#define pb push_back +#define pii pair +#define x first +#define y second +const int Nmax = 30000 + 5; +int n, m, src, fnl; +int dist[Nmax]; +bool viz[Nmax], inq[Nmax]; +queueq; +vector v[Nmax]; +int main() +{ + fin >> n >> m >> src >> fnl; + for(int i = 1, a, b ,c; i <= m; ++i) + { + fin >> a >> b >> c; + v[a].pb({b,c}); + v[b].pb({a,c}); + } + for(int i = 1; i <=n; ++i)dist[i] = 1 << 30; + dist[src] = 0;inq[src] = 1; + q.push(src); + while(!q.empty()) + { + int nod = q.front(); + q.pop(); + inq[nod] = 0; + for(auto i : v[nod]) + { + if(i.x > nod && dist[i.x] > dist[nod] + i.y) + { + dist[i.x] = dist[nod] + i.y; + if(!inq[i.x]) + { + inq[i.x] = 1; + q.push(i.x); + } + } + else if(i.x < nod && dist[i.x] > dist[nod] - i.y) + { + dist[i.x] =dist[nod] - i.y; + if(!inq[i.x]) + { + inq[i.x] = 1; + q.push(i.x); + } + } + } + } + fout << dist[fnl]; + return 0; +} diff --git a/villages/bfs/276ef5ae-40ad-11ec-9755-dc7196033e92.cpp b/villages/bfs/276ef5ae-40ad-11ec-9755-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2edb7c5357a3ba227fa4a7cfbc788b7601b0da6f --- /dev/null +++ b/villages/bfs/276ef5ae-40ad-11ec-9755-dc7196033e92.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +using namespace std; +FILE *f=fopen("sate.in","r"); +FILE *g=fopen("sate.out","w"); +vector > G[30005]; +int M,N; +queue Q; +int dist[30005]; +int X,Y; +const int LEN=10000; +char buff[LEN]; +int ind=LEN-1; +int i32() +{ + int rez=0; + while(buff[ind]<'0'||buff[ind]>'9') + { + if(++ind>=LEN) + { + ind=0; + fread(buff,1,LEN,f); + } + } + while(buff[ind]>='0'&&buff[ind]<='9') + { + rez=rez*10+buff[ind]-'0'; + if(++ind>=LEN) + { + ind=0; + fread(buff,1,LEN,f); + } + } + return rez; +} +int main() +{ + N=i32();M=i32();X=i32();Y=i32(); + for(int i=1;i<=N;i++)dist[i]=1<<30; + for(int i=1;i<=M;i++) + { + int x,y,z; + x=i32();y=i32();z=i32(); + G[x].push_back({y,z}); + G[y].push_back({x,-z}); + } + dist[X]=0; + Q.push(X); + while(!Q.empty()) + { + int nod=Q.front();Q.pop(); + for(auto it:G[nod]) + if(dist[it.first]>dist[nod]+it.second) + {dist[it.first]=dist[nod]+it.second;Q.push(it.first);} + } + fprintf(g,"%d",dist[Y]); + fclose(f); + fclose(g); + return 0; +} diff --git a/villages/bfs/27a37622-40ad-11ec-8eb3-dc7196033e92.cpp b/villages/bfs/27a37622-40ad-11ec-8eb3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2110210a06595afe50c0fac60f1eda401fcee382 --- /dev/null +++ b/villages/bfs/27a37622-40ad-11ec-8eb3-dc7196033e92.cpp @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#define pb push_back +#define mp make_pair +#define x first +#define y second +#define pii pair +#define NM 30005 +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +vector v[NM]; +queue q;///nodul si distanta incpand cu ST +int n,m,st,fn,a,b,d,dist[NM],viz[NM]; +pii fr; +int nod; +int verif(int n1,int n2,int n3) +{ + return(n1<=n2 && n2<=n3); +} +///dist[i] distanta de la st la i +int main() +{ + in>>n>>m>>st>>fn; + for(int i=1;i<=m;++i) + { + in>>a>>b>>d; + v[a].pb(mp(b,d)); + v[b].pb(mp(a,d)); + } + q.push(st); + viz[st]=1; + while(!viz[fn]) + { + nod=q.front(); + q.pop(); + for(auto i:v[nod]) + { + if(!viz[i.x]) + { + if(verif(nod,i.x,st) || verif(st,i.x,nod))dist[i.x]=dist[nod]-i.y; + else + { + if(verif(nod,st,i.x) || verif(i.x,st,nod))dist[i.x]=i.y-dist[nod]; + else dist[i.x]=dist[nod]+i.y; + } + q.push(i.x); + viz[i.x]=1; + } + } + } + out<diferenta 2 +///nod i.x st---->diferenta 1 +///st nod i.x--->suma +///st i.x nod--->diferenta 1 +///i.x st nod--->diferenta 2 +///i.x nod st---->suma diff --git a/villages/bfs/27da6747-40ad-11ec-9424-dc7196033e92.cpp b/villages/bfs/27da6747-40ad-11ec-9424-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fd874b9f218143e3491199c59dda5a310c590e39 --- /dev/null +++ b/villages/bfs/27da6747-40ad-11ec-9424-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +using namespace std; +vector > G[30005]; +int M,N; +queue Q; +int dist[30005]; +int X,Y; +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + cin.sync_with_stdio(false);cin.tie(0); + cout.sync_with_stdio(false);cout.tie(0); + cin>>N>>M>>X>>Y; + for(int i=1;i<=N;i++)dist[i]=1<<30; + for(int i=1;i<=M;i++) + { + int x,y,z; + cin>>x>>y>>z; + G[x].push_back({y,z}); + G[y].push_back({x,-z}); + } + dist[X]=0; + Q.push(X); + while(!Q.empty()) + { + int nod=Q.front();Q.pop(); + for(auto it:G[nod]) + if(dist[it.first]>dist[nod]+it.second) + {dist[it.first]=dist[nod]+it.second;Q.push(it.first);} + } + cout< + +using namespace std; + +const int MAXN = 3e4; + +vector > g[MAXN + 1]; +queue q; +int d[MAXN + 1]; + +int main() +{ + int n, m, x, y; + ifstream fin("sate.in"); + fin >> n >> m >> x >> y; + for (int i = 0; i < m; ++i) { + int a, b, c; + fin >> a >> b >> c; + g[a].push_back({b, c}); + g[b].push_back({a, -c}); + } + fin.close(); + for (int i = 1; i <= n; ++i) + d[i] = -1; + d[x] = 0; + q.push(x); + while (q.empty() == 0) { + int node = q.front(); + q.pop(); + for (auto it : g[node]) + if (d[it.first] == -1) { + d[it.first] = d[node] + it.second; + q.push(it.first); + } + } + ofstream fout("sate.out"); + fout << d[y]; + fout.close(); + return 0; +} diff --git a/villages/bfs/29c8e809-40ad-11ec-80ba-dc7196033e92.cpp b/villages/bfs/29c8e809-40ad-11ec-80ba-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a215f5588b8ebc4aab1b501a6ec1164773fb8ee --- /dev/null +++ b/villages/bfs/29c8e809-40ad-11ec-80ba-dc7196033e92.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + +int n, m, x, y, d; +vector > v[30005]; +bool f[30005], viz[30005]; +queue > q; +inline int bfs(int nod){ +// if(nod == d) return cost; +// for(vector > :: iterator it = v[nod].begin() ; it != v[nod].end() ; ++it){ +// if(viz[it->first]) continue ; +// if(it->first > nod){ +// viz[it->first] = 1; +// int k = bfs(it->first, cost + it->second); +// viz[it->first] = 0; +// if(k == -1) continue ; +// return k; +// } +// else{ +// viz[it->first] = 1; +// int k = bfs(it->first, cost - it->second); +// viz[it->first] = 0; +// if(k == -1) continue ; +// return k; +// } +// } + q.push(make_pair(nod, 0)); + while(!q.empty()){ + int nod = q.front().first, cost = q.front().second; + q.pop(); + if(nod == d) return cost; + for(vector > :: iterator it = v[nod].begin() ; it != v[nod].end() ; ++it){ + if(viz[it->first]) continue ; + if(it->first > nod){ + viz[it->first] = 1; + q.push(make_pair(it->first, cost + it->second)); + } + else{ + viz[it->first] = 1; + q.push(make_pair(it->first, cost - it->second)); + } + } + } + return -1; +} +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + scanf("%d%d%d%d", &n, &m, &x, &y); + int a, b, dist; + for(int i = 1; i <= m ; ++i){ + scanf("%d%d%d", &a, &b, &dist); + v[a].push_back(make_pair(b, dist)); + v[b].push_back(make_pair(a, dist)); + f[a] = f[b] = 1; + } + for(int i = 1; i <= n ; ++i){ + if(!f[i]) continue ; + d = x; + viz[i] = 1; + a = bfs(i); + d = y; + memset(viz, 0, sizeof(viz)); + viz[i] = 1; + b = bfs(i); + if(a == -1 || b == -1) continue ; + printf("%d", b - a); return 0; + } + return 0; +} + diff --git a/villages/bfs/2ab5c8c8-40ad-11ec-99b8-dc7196033e92.cpp b/villages/bfs/2ab5c8c8-40ad-11ec-99b8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87268ef026a1f335d54ba827c8d63193d8d7538c --- /dev/null +++ b/villages/bfs/2ab5c8c8-40ad-11ec-99b8-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include + +#define f first +#define s second + +using namespace std; + +ifstream F("sate.in"); +ofstream G("sate.out"); + +int n, m, x, y, c, X, Y, d[30005]; +bitset<30005> v; +vector > a[30005]; +queue q; + +void bfs(int nod) +{ + vector > :: iterator it; + v[nod] = 1; + q.push(nod); + while(!q.empty()) + { + x = q.front();q.pop(); + for(it = a[x].begin(); it != a[x].end(); ++ it) + if(!v[(*it).f] && !d[(*it).f]) + v[(*it).f] = 1, d[(*it).f] = d[x] + (*it).s, q.push((*it).f); + } + G << d[Y]; +} + +int main() +{ + F >> n >> m >> X >> Y; + for(int i = 0; i < m; ++ i) + { + F >> x >> y >> c; + a[x].push_back({y, c}); + a[y].push_back({x, -c}); + } + bfs(X); + return 0; +} diff --git a/villages/bfs/2ae86451-40ad-11ec-be56-dc7196033e92.cpp b/villages/bfs/2ae86451-40ad-11ec-be56-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1ef39d3eb68312af8252261eede227cfb9240de --- /dev/null +++ b/villages/bfs/2ae86451-40ad-11ec-be56-dc7196033e92.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +using namespace std; + +vector > graf[30001] ; +queue Q ; +int dist[300001] ; +bool viz[300001] ; +int x , y , m , n ; + +ifstream fin ("sate.in") ; +ofstream fout("sate.out") ; + +void citire () +{ + int x1 , y1 , c1 , i ; + fin >> n >> m >> x >> y ; + for ( i = 1 ; i <= m ; i++ ) + { + fin >> x1 >> y1 >> c1 ; + if ( x1 > y1 ) + { + graf[x1].push_back(make_pair(y1,-c1)) ; + graf[y1].push_back(make_pair(x1,c1)) ; + } + else + { + graf[x1].push_back(make_pair(y1,c1)) ; + graf[y1].push_back(make_pair(x1,-c1)) ; + } + } +} + +void lee ( int a , int b ) +{ + int i ; + int punct ; + Q.push(a) ; + memset(dist,-1,100*sizeof(int)) ; + memset(viz,false,100*sizeof(bool)) ; + dist[a] = 0 ; + viz[a] = true ; + while ( !Q.empty() ) + { + punct = Q.front() ; + Q.pop() ; + for ( i = 0 ; i < graf[punct].size() ; i++ ) + { + if ( viz[graf[punct][i].first] == false ) + { + viz[graf[punct][i].first] = true ; + dist[graf[punct][i].first] = dist[punct] + graf[punct][i].second ; + Q.push(graf[punct][i].first) ; + } + } + } +} + +int main() +{ + citire() ; + lee(x,y) ; + fout << dist[y] ; +} diff --git a/villages/bfs/2b1f18ec-40ad-11ec-8d98-dc7196033e92.cpp b/villages/bfs/2b1f18ec-40ad-11ec-8d98-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d7009664f3ff8fdd7ce5c0e50de11e8e20b2a6f --- /dev/null +++ b/villages/bfs/2b1f18ec-40ad-11ec-8d98-dc7196033e92.cpp @@ -0,0 +1,64 @@ +//#include "stdafx.h" +#include +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +typedef struct _NODE +{ + vector> neighbours; + bool visited = false; + int cost = 0; +}NODE, *PNODE; + +NODE G[30005]; + +int main() +{ + int n, m, x, y, i, a, b, d; + queue Q; + + fin >> n >> m >> x >> y; + for (i = 0; i < m; ++i) + { + fin >> a >> b >> d; + G[a].neighbours.push_back(make_pair(b, d)); + G[b].neighbours.push_back(make_pair(a, d)); + } + + Q.push(x); + G[x].visited = true; + while (!Q.empty()) + { + a = Q.front(); + if (a == y) break; + for (pair neighbour : G[a].neighbours) + { + if (!G[neighbour.first].visited) + { + if (neighbour.first > a) + { + G[neighbour.first].cost = G[a].cost + neighbour.second; + } + else + { + G[neighbour.first].cost = G[a].cost - neighbour.second; + } + G[neighbour.first].visited = true; + Q.push(neighbour.first); + } + } + Q.pop(); + } + + fout << G[y].cost << "\n"; + + return 0; +} \ No newline at end of file diff --git a/villages/bfs/2b57966f-40ad-11ec-8f00-dc7196033e92.cpp b/villages/bfs/2b57966f-40ad-11ec-8f00-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cc6be98a2ad558bd540b4bea5a0f0f24e7db58a7 --- /dev/null +++ b/villages/bfs/2b57966f-40ad-11ec-8f00-dc7196033e92.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +using namespace std; + +#define maxn 300005 + +int n,m,x,y,D[maxn]; +bool viz[maxn]; + +queue < int > q; + +vector < pair < int , int > > G[maxn]; + +void Read() +{ + int aux1,aux2,co; + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%i %i %i %i",&n,&m,&x,&y); + for(int i=1;i<=m;i++) + { + scanf("%i %i %i",&aux1,&aux2,&co); + G[aux1].push_back(make_pair(aux2,co)); + G[aux2].push_back(make_pair(aux1,co)); + } +} + +void BFS(int nodP) +{ + D[nodP] = 0; + q.push(nodP); + while(!q.empty()) + { + int nod = q.front(); + q.pop(); + if(nod == y) + return; + viz[nod] = 1; + for(int i=0;i y) + { + aux = x; + x = y; + y = aux; + } + BFS(x); + printf("%i",D[y]); + return 0; +} diff --git a/villages/bfs/2b5fa2f4-17ad-4afb-8dd5-ef6a9747bfda.cpp b/villages/bfs/2b5fa2f4-17ad-4afb-8dd5-ef6a9747bfda.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6ed7e7781ed982f300a85dc8637c5a21e913321a --- /dev/null +++ b/villages/bfs/2b5fa2f4-17ad-4afb-8dd5-ef6a9747bfda.cpp @@ -0,0 +1,37 @@ +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair >g[30001]; +int dist[30001],q[30001]; +bool viz[30001]; +int n,m,x,y,a,b,d,st,dr; +int main() +{ + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>a>>b>>d; + g[a].push_back(make_pair(b,d)), + g[b].push_back(make_pair(a,-d)); + } + st=dr=1; + q[1]=x; + viz[x]=1; + while(st<=dr) + { + for(int i=0;i +#include +#include +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +const int NMax=300005; +int D[NMax],N,M,X,Y; +bool Use[NMax]; +vector < pair > G[NMax]; +queue Q; + +void Read() +{ + fin>>N>>M>>X>>Y; + for(int i=1;i<=M;++i) + { + int x,y,z; + fin>>x>>y>>z; + G[x].push_back(make_pair(y,z)); + G[y].push_back(make_pair(x,z)); + } +} + +void BFS(int Start) +{ + D[Start]=0; + Q.push(Start); + while(!Q.empty()) + { + int Nod=Q.front(); + Q.pop(); + if(Nod==Y) + return; + Use[Nod]=1; + for(unsigned int j = 0 ; j < G[Nod].size(); j++) + { + int Vecin = G[Nod][j].first; + if(Use[Vecin] == 0) + { + if(Vecin>Nod) + D[Vecin] = D[Nod] + G[Nod][j].second; + else + D[Vecin]=D[Nod] - G[Nod][j].second; + Q.push(Vecin); + + } + } + } + + +} + + +int main() +{ + Read(); + if(Y +#include +using namespace std; +class InputReader { +private: + FILE *inputFile; + static const int SIZE = 1 << 16; + char buffer[SIZE]; int cursor; + + inline void advance(void) { + if (++cursor == SIZE) { + cursor = 0; + fread(buffer, SIZE, sizeof(char), inputFile); + } + } + + inline char current(void) { + return buffer[cursor]; + } +public: + InputReader(const char* fileName) : cursor(0) { + inputFile = fopen(fileName, "r"); + fread(buffer, SIZE, 1, inputFile); + } + + ~InputReader(void) { + fclose(inputFile); + } + + template + InputReader &operator >>(type &value) { + value = 0; + while (current() < '0' or current() > '9') + advance(); + while (current() >= '0' and current() <= '9') { + value = value * 10 + current() - '0'; + advance(); + } + return *this; + } +} in("sate.in"); +ofstream out("sate.out"); +int a,b,c,n,m,x,y,v[30001],go,st,dr,i,hz[30001],distanta[30001]; +vector< pair >h[30001]; +int main(){ + in >> n >> m >> x >> y; + for( i = 1; i <= m; i ++ ){ + in >> a >> b >> c; + h[a].push_back( make_pair( b,c ) ); + h[b].push_back( make_pair( a,c ) ); + } + v[1] = x; + hz[x] = 1; + for( st = 1, dr = 1; st <= dr; st ++ ){ + for( i = 0; i < h[v[st]].size(); i ++ ){ + if( v[st] < h[v[st]][i].first ) + distanta[ h[v[st]][i].first ] = distanta[v[st]] + h[v[st]][i].second; + else + distanta[ h[v[st]][i].first ] = distanta[v[st]] - h[v[st]][i].second; + + if( hz[ h[v[st]][i].first ] == 0 ){ + hz[ h[v[st]][i].first ] = 1; + dr++; + v[dr] = h[v[st]][i].first; + } + } + } + out< +#include + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +const int N = 30002; + +struct pereche +{ + int nod, cost; +}; + +vector g[N]; +int c[N], d[N]; +bool viz[N]; +int n, m, X, Y; + +void Aranjare() +{ + in >> n >> m >> X >> Y; + int x, y, d; + for(int i = 1; i <= m; i++) + { + in >> x >> y >> d; + g[x].push_back({y, d}); + g[y].push_back({x, d}); + } +} + +void Bfs(int a, int b) +{ + int p = 1, u = 1, now; + c[1] = a; + viz[a] = true; + while(p <= u) + { + now = c[p]; + p++; + for(int i = 0; i < g[now].size(); ++i) + { + if(viz[g[now][i].nod] == false) + { + if(g[now][i].nod > now) + d[g[now][i].nod] = d[now] + g[now][i].cost; + else + d[g[now][i].nod] = d[now] - g[now][i].cost; + u++; + c[u] = g[now][i].nod; + viz[g[now][i].nod] = true; + } + } + + } + out << d[b]; +} + +int main() +{ + Aranjare(); + Bfs(X, Y); + return 0; +} diff --git a/villages/bfs/2c427a7d-7c5e-4a64-870a-712077530204.cpp b/villages/bfs/2c427a7d-7c5e-4a64-870a-712077530204.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a637ef5941be14abd3d48c212a06a147f39fd6f --- /dev/null +++ b/villages/bfs/2c427a7d-7c5e-4a64-870a-712077530204.cpp @@ -0,0 +1,52 @@ +#include +#include +#define DEF 100100 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, start, dest, p, u, viz[DEF]; + +pair < int, int > c[DEF]; + +vector < pair < int, int > > D[DEF]; + +int main () { + fin >> n >> m >> start >> dest; + + if (start > dest) + swap (start, dest); + + for (int i = 1; i <= m; ++ i) { + int x, y, c; + fin >> x >> y >> c; + D[x].push_back (make_pair (y, c)); + D[y].push_back (make_pair (x, - c)); + } + + c[++ u] = make_pair (start, 0); + viz[start] = 1; + while (p <= u) { + int nod = c[p].first; + int cost = c[p].second; + + for (int i = 0; i < D[nod].size (); ++ i) { + if (viz[D[nod][i].first] == 0) { + viz[D[nod][i].first] = 1; + int new_cost = cost + D[nod][i].second; + + c[++ u] = make_pair (D[nod][i].first, new_cost); + + if (D[nod][i].first == dest) { + fout << new_cost; + return 0; + } + } + } + ++ p; + } + + return 0; +} diff --git a/villages/bfs/2c4a90d2-40ad-11ec-8b2e-dc7196033e92.cpp b/villages/bfs/2c4a90d2-40ad-11ec-8b2e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..87268ef026a1f335d54ba827c8d63193d8d7538c --- /dev/null +++ b/villages/bfs/2c4a90d2-40ad-11ec-8b2e-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include + +#define f first +#define s second + +using namespace std; + +ifstream F("sate.in"); +ofstream G("sate.out"); + +int n, m, x, y, c, X, Y, d[30005]; +bitset<30005> v; +vector > a[30005]; +queue q; + +void bfs(int nod) +{ + vector > :: iterator it; + v[nod] = 1; + q.push(nod); + while(!q.empty()) + { + x = q.front();q.pop(); + for(it = a[x].begin(); it != a[x].end(); ++ it) + if(!v[(*it).f] && !d[(*it).f]) + v[(*it).f] = 1, d[(*it).f] = d[x] + (*it).s, q.push((*it).f); + } + G << d[Y]; +} + +int main() +{ + F >> n >> m >> X >> Y; + for(int i = 0; i < m; ++ i) + { + F >> x >> y >> c; + a[x].push_back({y, c}); + a[y].push_back({x, -c}); + } + bfs(X); + return 0; +} diff --git a/villages/bfs/2c86547e-7140-41da-9b6a-1b6a5c4bc009.cpp b/villages/bfs/2c86547e-7140-41da-9b6a-1b6a5c4bc009.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00c06866a272768df62289301ac872a8c490da7d --- /dev/null +++ b/villages/bfs/2c86547e-7140-41da-9b6a-1b6a5c4bc009.cpp @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +vector < pair > v[30001]; +bool viz[30001]; +char s[1001]; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n,m,st,fin; + +void read() +{ in>>n>>m>>st>>fin; + in.get(); + int x, y, c, nr, cnt; + for (int k = 1; k <= m; ++k) + { cnt = 0; + in.getline(s, 1001); + for (int i = 0; s[i] != '\0'; ++i) { + if (s[i] >= '0' && s[i] <= '9') { + nr = 0; + cnt++; + while (s[i] >= '0' && s[i] <= '9') { + nr = nr * 10 + s[i] - '0'; + ++i; + } + if (cnt == 1) x = nr; + if (cnt == 2) y = nr; + if (cnt == 3) c = nr; + } + } + v[x].push_back({y,c}); + v[y].push_back({x,c}); + } +} + +void drum(int nod,int dist) +{ + viz[nod]=1; + if (nod==fin) + { + out<nod) + drum(nod1,dist+d); + } + } +} + +int main() +{ + read(); + drum(st,0); + return 0; +} diff --git a/villages/bfs/2cc853e9-40ad-11ec-a44b-dc7196033e92.cpp b/villages/bfs/2cc853e9-40ad-11ec-a44b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1ef39d3eb68312af8252261eede227cfb9240de --- /dev/null +++ b/villages/bfs/2cc853e9-40ad-11ec-a44b-dc7196033e92.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +using namespace std; + +vector > graf[30001] ; +queue Q ; +int dist[300001] ; +bool viz[300001] ; +int x , y , m , n ; + +ifstream fin ("sate.in") ; +ofstream fout("sate.out") ; + +void citire () +{ + int x1 , y1 , c1 , i ; + fin >> n >> m >> x >> y ; + for ( i = 1 ; i <= m ; i++ ) + { + fin >> x1 >> y1 >> c1 ; + if ( x1 > y1 ) + { + graf[x1].push_back(make_pair(y1,-c1)) ; + graf[y1].push_back(make_pair(x1,c1)) ; + } + else + { + graf[x1].push_back(make_pair(y1,c1)) ; + graf[y1].push_back(make_pair(x1,-c1)) ; + } + } +} + +void lee ( int a , int b ) +{ + int i ; + int punct ; + Q.push(a) ; + memset(dist,-1,100*sizeof(int)) ; + memset(viz,false,100*sizeof(bool)) ; + dist[a] = 0 ; + viz[a] = true ; + while ( !Q.empty() ) + { + punct = Q.front() ; + Q.pop() ; + for ( i = 0 ; i < graf[punct].size() ; i++ ) + { + if ( viz[graf[punct][i].first] == false ) + { + viz[graf[punct][i].first] = true ; + dist[graf[punct][i].first] = dist[punct] + graf[punct][i].second ; + Q.push(graf[punct][i].first) ; + } + } + } +} + +int main() +{ + citire() ; + lee(x,y) ; + fout << dist[y] ; +} diff --git a/villages/bfs/2d363bea-40ad-11ec-91b2-dc7196033e92.cpp b/villages/bfs/2d363bea-40ad-11ec-91b2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff6a0a310b686f12a67519efa7f5cc86442ed52b --- /dev/null +++ b/villages/bfs/2d363bea-40ad-11ec-91b2-dc7196033e92.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +using namespace std; + +vector< pair > v[30005]; +int n, m, home, town, d[30005]; + +int main() +{ + ifstream fin ("sate.in"); + ofstream fout ("sate.out"); + fin >> n >> m >> home >> town; + while(m--){ + int x, y, z; + fin >> x >> y >> z; + if(x > y) + swap(x, y); + v[x].push_back(make_pair(y, z)); + v[y].push_back(make_pair(x, -z)); + } + memset(d, -1, sizeof(d)); + queue q; + q.push(home); + d[home] = 0; + while(!q.empty()){ + int k = q.front(); + q.pop(); + for (unsigned i = 0; i < v[k].size(); ++i) + if((d[v[k][i].first] == -1 || d[v[k][i].first] > d[k] + v[k][i].second) && v[k][i].first != home){ + d[v[k][i].first] = d[k] + v[k][i].second; + q.push(v[k][i].first); + } + } + fout << d[town] << "\n"; + fin.close(); + fout.close(); + return 0; +} diff --git a/villages/bfs/2d65d62e-40ad-11ec-acf8-dc7196033e92.cpp b/villages/bfs/2d65d62e-40ad-11ec-acf8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d09b904f5e10d019753b6bb2b98348086856f2f4 --- /dev/null +++ b/villages/bfs/2d65d62e-40ad-11ec-acf8-dc7196033e92.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +const int N = 30010, M = 100025; + +int n, m, a, b, v[N], x[M], y[M], z[M], i; + +int main() { + fin >> n >> m >> a >> b; + v[a] = 1; + for (i = 1; i <= m; ++i) { + fin >> x[i] >> y[i] >> z[i]; + } + while (!v[b]) { + for (i = 1; i <= m; ++i) { + if (v[ x[i] ] && !v[ y[i] ]) { + v[ y[i] ] = v[ x[i] ] + z[i]; + } + else if (!v[ x[i] ] && v[ y[i] ]) { + v[ x[i] ] = v[ y[i] ] - z[i]; + } + } + } + fout << v[b] - 1; +} diff --git a/villages/bfs/2d9a5700-40ad-11ec-95bb-dc7196033e92.cpp b/villages/bfs/2d9a5700-40ad-11ec-95bb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2db64b95eac2114786728370398ddac8f1fb5625 --- /dev/null +++ b/villages/bfs/2d9a5700-40ad-11ec-95bb-dc7196033e92.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +vector vecini[30005]; +vector distanta[30005]; +short coada[30005]; +string s; +int k,n,m,x,y,a,b,start,d,cost[30005],deparcurs,c; + +int next(int &c) +{ + int nr=0,cif; + while(s[c]!=' ' && c>n>>m>>x>>y; getline(f,s); + for(int i=1;i<=m;++i) + { + getline(f,s); + c=0; + a=next(c); b=next(c); d=next(c); + if(b +#include +#include +#include +#include + +using namespace std; + +const int NodMax = 30005; +const int MuchMax = 100030; + +vector < pair < int, int > > G[MuchMax]; +queue Q; +bitset < NodMax > viz; +int N,M,X,Y; +int dist[NodMax]; + +void Read() +{ + scanf("%d %d %d %d", &N, &M, &X, &Y); + + for(int i=1; i<=M; ++i) + { + int x, y, c; + scanf("%d %d %d", &x, &y, &c); + G[x].push_back(make_pair(y,c)); + G[y].push_back(make_pair(x,c)); + + } + + if(X > Y) + swap(X,Y); +} + +void Solve() +{ + Q.push(X); + + while(!Q.empty()) + { + int nodc = Q.front(); + Q.pop(); + + viz[nodc] = true; + + if(nodc == Y) + return; + + for(int i=0; i G[nodc][i].first) + dist[G[nodc][i].first] = dist[nodc] - G[nodc][i].second; + + else + dist[G[nodc][i].first] = dist[nodc] + G[nodc][i].second; + + Q.push(G[nodc][i].first); + } + } + } +} + +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + Read(); + Solve(); + + printf("%d", dist[Y]); + + return 0; +} diff --git a/villages/bfs/2dffae9b-40ad-11ec-842f-dc7196033e92.cpp b/villages/bfs/2dffae9b-40ad-11ec-842f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e68aa53815cdacff7bc249ee4e17510db069ba22 --- /dev/null +++ b/villages/bfs/2dffae9b-40ad-11ec-842f-dc7196033e92.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +#define lim 30010 +int n,m,x,y,a,b,c; +int dist[lim]; +bool viz[lim]; +struct loc{int sat,d;}; +vector G[lim]; + + + +void bfs(int nod) +{ + queue q; + q.push(nod); + dist[nod]=0; + + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(auto it:G[nod]) + { + if(!viz[it.sat]) + { + viz[it.sat]=true; + if(it.sat>nod) + { + dist[it.sat]=dist[nod]+it.d; + q.push(it.sat); + } + if(it.sat>n>>m>>x>>y; + + for(int i=1; i<=m; i++) + { + fin>>a>>b>>c; + G[a].push_back({b,c}); + G[b].push_back({a,c}); + } + + bfs(x); + + fin.close(); + fout.close(); + return 0; +} diff --git a/villages/bfs/2e3567c1-40ad-11ec-8a25-dc7196033e92.cpp b/villages/bfs/2e3567c1-40ad-11ec-8a25-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..797679744fb3cfc5fdcfb92988a6fe023d9aec27 --- /dev/null +++ b/villages/bfs/2e3567c1-40ad-11ec-8a25-dc7196033e92.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,b[30005],c[30005]; +vector >a[30005]; +queueq; +void ve() +{ + int nod; + q.push(x); + while(!q.empty()) + { + nod=q.front(); + q.pop(); + if(nod==y) + return; + c[nod]=1; + for(int i=0;ia[nod][i].first) + b[a[nod][i].first]=b[nod]-a[nod][i].second; + else + b[a[nod][i].first]=b[nod]+a[nod][i].second; + q.push(a[nod][i].first); + } + } + +} +int main() +{ + fin>>n>>m>>x>>y; + for(int h=1;h<=m;h++) + { + int i,j,d; + fin>>i>>j>>d; + a[i].push_back(make_pair(j,d)); + a[j].push_back(make_pair(i,d)); + } + if(y +#include +#include + +using namespace std; + +const int nmax=30005; +typedef pair ii; +vector g[nmax]; +queue q; +int lng[nmax]; + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + int x,y,n,start,finish,cost,m,i; + bool a=0; + scanf("%d%d%d%d",&n,&m,&start,&finish); + for(i=1;i<=m;i++) + { + scanf("%d%d%d",&x,&y,&cost); + g[x].push_back(ii(y,cost)); + g[y].push_back(ii(x,-cost)); + } + for(i=1;i<=n;i++) + lng[i]=-21000000; + lng[start]=0; + q.push(start); + int node,sz; + while(!q.empty()) + { + node=q.front(); + sz=g[node].size(); + q.pop(); + for(i=0;i +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair >g[30001]; +int dist[30001],q[30001]; +bool viz[30001]; +int n,m,x,y,a,b,d,st,dr; +int main() +{ + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>a>>b>>d; + g[a].push_back(make_pair(b,d)), + g[b].push_back(make_pair(a,-d)); + } + st=dr=1; + q[1]=x; + viz[x]=1; + while(st<=dr) + { + for(int i=0;i +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +struct {int a,b,d;} v[100025]; +int n,m,x,y,viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>v[i].a>>v[i].b>>v[i].d; + viz[x]=1; + while(!viz[y]) + { for(int i=1;i<=m;i++) + { if(viz[v[i].a] && !viz[v[i].b]) + viz[v[i].b]= viz[v[i].a]+v[i].d; + else + if(!viz[v[i].a] && viz[v[i].b]) + viz[v[i].a]= viz[v[i].b]-v[i].d; + } + } + g< +using namespace std; +ifstream f ("sate.in"); +ofstream g ("sate.out"); +int a,b,t[30001],x[100025],y[100025],c[100025],n,m; +int main() +{ + f>>n>>m>>a>>b; + t[a]=1; + for(int i=1;i<=m;++i) f>>x[i]>>y[i]>>c[i]; + while(!t[b]) + { + for (int i=1;i<=m;++i) + { + if(t[x[i]]&&!t[y[i]]) t[y[i]]=t[x[i]]+c[i]; + else if(!t[x[i]]&&t[y[i]]) t[x[i]]=t[y[i]]-c[i]; + } + } + g< +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/2f8a10ad-40ad-11ec-baef-dc7196033e92.cpp b/villages/bfs/2f8a10ad-40ad-11ec-baef-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ed57cd0054534fb23766f0b594911d6ccb9f442 --- /dev/null +++ b/villages/bfs/2f8a10ad-40ad-11ec-baef-dc7196033e92.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int a, b, s[30001], x[100001], y[100001], c[100001], n, m; + +int main(){ + int i; + fin>>n>>m>>a>>b; + s[a]=1; + for (i=1;i<=m;i++) + fin>>x[i]>>y[i]>>c[i]; + while(!s[b]){ + for (int i=1; i<=m; i++){ + if (s[x[i]] && !s[y[i]]) + s[y[i]]=s[x[i]]+c[i]; + else if (!s[x[i]] && s[y[i]]) + s[x[i]]=s[y[i]]-c[i];} + } + fout< +#include +#include +#include +using namespace std; +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +} in ("sate.in"); +ofstream out("sate.out"); +vector > v[30005]; +int q[30005]; +int d[30005],sz=1,cur=1; +int main() +{ + int n,m,x,y,a,b,di; + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++){ + in>>a>>b>>di; + v[a].push_back(make_pair(b,di)); + v[b].push_back(make_pair(a,-di)); + } + for(int i=1;i<=n;i++){ + d[i]=30000000; + } + q[1]=x; + d[x]=0; + while(cur<=sz){ + a=q[cur]; + cur++; + for(auto u:v[a]){ + if(d[u.first]==30000000){ + d[u.first]=d[a]+u.second; + if(u.first==y){ + out< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,c,viz[30001],d[30001],v[30001],nod1,nod2; +vector< pair < int , int > >my[30001]; +queue q; +inline void bfs(int st){ + int nod,vecin,cost; + vector< pair >::iterator it; + q.push(st); + v[st]=1; + viz[st]=1; + while (!q.empty()&&!v[nod2]){ + nod=q.front(); + viz[nod]=1; + q.pop(); + for (it=my[nod].begin();it!=my[nod].end();it++){ + vecin=(*it).first; + cost=(*it).second; + if (!viz[vecin]){ + if (!v[vecin]) if (vecin>n>>m>>nod1>>nod2; +for (int i(1);i<=m;i++){ + fin>>x>>y>>c; + my[x].push_back(make_pair(y,c)); + my[y].push_back(make_pair(x,c)); + } + bfs(nod1); +fout< +#include +#include +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int NMAX = 30000 + 5; + +vector > g[NMAX]; +queue q; + +int n, m, x, y; +int dist[NMAX]; +bool vis[NMAX]; + +void read() +{ + int j, k, l; + fin >> n >> m >> x >> y; + for (int i = 1; i <= m; ++i) + { + fin >> j >> k >> l; + g[j].push_back(make_pair(k, l)); + g[k].push_back(make_pair(j, -l)); + } +} + +void bfs() +{ + int k, kn; + dist[x] = 1; + q.push(x); + while (!q.empty()) + { + k = q.front(); + q.pop(); + for (int i = 0; i < g[k].size(); ++i) + { + kn = g[k][i].first; + if (!dist[kn]) + { + dist[kn] = dist[k] + g[k][i].second; + q.push(kn); + } + } + } +} + +int main() +{ + read(); + bfs(); + fout << dist[y] - 1; + return 0; +} diff --git a/villages/bfs/30b8de42-40ad-11ec-9f0e-dc7196033e92.cpp b/villages/bfs/30b8de42-40ad-11ec-9f0e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ca2fe24bdac5ccef2be27e37df9963142ea4001 --- /dev/null +++ b/villages/bfs/30b8de42-40ad-11ec-9f0e-dc7196033e92.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +vector < pair < int, int > > graf[30002]; +queue < int > q; +int cost [30002]; + + +void BFS (int $$, int $$$){ + + vector < pair < int, int > > :: iterator it; + q.push($$); + cost[$$] = 1; + while (!q.empty()){ + int nod = q.front(); + q.pop(); + for (it = graf[nod].begin(); it != graf[nod].end(); ++it){ + if (cost[it->first] == 0) { + cost[it->first] = cost[nod] + it->second; + + if (it->first == $$$) return ; + q.push(it->first); + } + } + } +} + +int main (){ + + + int n,m,x,y; + int $$, $$$, $$$$; + fin >> n >> m >> x >> y; + for (int i = 1; i <= m; ++i){ + fin >> $$ >> $$$ >> $$$$; + graf[$$].push_back(make_pair($$$,$$$$)); + graf[$$$].push_back(make_pair($$,-$$$$)); + } + BFS(x,y); + fout << cost[y]-1 << "\n"; + +} diff --git a/villages/bfs/30e7442f-40ad-11ec-8404-dc7196033e92.cpp b/villages/bfs/30e7442f-40ad-11ec-8404-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3075b113414311cf2efb51ed905fa97bf05a09f8 --- /dev/null +++ b/villages/bfs/30e7442f-40ad-11ec-8404-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/3198d0d5-e50c-43a5-9517-b8d53b71cbec.cpp b/villages/bfs/3198d0d5-e50c-43a5-9517-b8d53b71cbec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..634d4c0751e6350b03380b6d686669d0146a33a6 --- /dev/null +++ b/villages/bfs/3198d0d5-e50c-43a5-9517-b8d53b71cbec.cpp @@ -0,0 +1,96 @@ +//#include +#include +#include +#include +using namespace std; +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +} in ("sate.in"); +ofstream out("sate.out"); +vector > v[30005]; +int q[30005]; +int d[30005],sz=1,cur=1; +int main() +{ + int n,m,x,y,a,b,di; + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++){ + in>>a>>b>>di; + v[a].push_back(make_pair(b,di)); + v[b].push_back(make_pair(a,-di)); + } + for(int i=1;i<=n;i++){ + d[i]=30000000; + } + q[1]=x; + d[x]=0; + while(cur<=sz){ + a=q[cur]; + cur++; + for(auto u:v[a]){ + if(d[u.first]==30000000){ + d[u.first]=d[a]+u.second; + if(u.first==y){ + out< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int sol[30010],x[100050],y[100050],km[100050]; +int a,b,n,m; +int main(){ + fin >> n >> m >> a >> b; + sol[a]=1; + for (int i=1;i<=m;i++) + fin >> x[i] >> y[i] >> km[i]; + while (!sol[b]){ + for (int i=1;i<=m;i++){ + if (sol[x[i]] && !sol[y[i]]) sol[y[i]]=sol[x[i]]+km[i]; + else if (!sol[x[i]] && sol[y[i]]) sol[x[i]]=sol[y[i]]-km[i]; + } + } + fout << sol[b]-1; +} diff --git a/villages/bfs/32f1dd48-07b8-4a4c-981d-0a3eb9788668.cpp b/villages/bfs/32f1dd48-07b8-4a4c-981d-0a3eb9788668.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb0e3ec2e875d3508ed7bb3f7eb8eba8a88ff553 --- /dev/null +++ b/villages/bfs/32f1dd48-07b8-4a4c-981d-0a3eb9788668.cpp @@ -0,0 +1,36 @@ +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,S,F,p,u,x,y,cost; +int c[900010],dist[30010],v[30010]; +vector < pair > L[30010]; +int main(){ + fin>>n>>m>>S>>F; + for(int i=1;i<=m;i++){ + fin>>x>>y>>cost; + L[x].push_back(pair(y,cost)); + L[y].push_back(pair(x,cost)); + } + c[1]=S; + p=u=1; + v[S]=1; + while(p<=u){ + if(c[p]==F) + break; + for(int i=0;i +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int a, b, s[30001], x[100001], y[100001], c[100001], n, m; + +int main(){ + int i; + fin>>n>>m>>a>>b; + s[a]=1; + for (i=1;i<=m;i++) + fin>>x[i]>>y[i]>>c[i]; + while(!s[b]){ + for (int i=1; i<=m; i++){ + if (s[x[i]] && !s[y[i]]) + s[y[i]]=s[x[i]]+c[i]; + else if (!s[x[i]] && s[y[i]]) + s[x[i]]=s[y[i]]-c[i];} + } + fout< +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int a, b, s[30001], x[100001], y[100001], c[100001], n, m; + +int main(){ + int i; + fin>>n>>m>>a>>b; + s[a]=1; + for (i=1;i<=m;i++) + fin>>x[i]>>y[i]>>c[i]; + while(!s[b]){ + for (int i=1; i<=m; i++){ + if (s[x[i]] && !s[y[i]]) + s[y[i]]=s[x[i]]+c[i]; + else if (!s[x[i]] && s[y[i]]) + s[x[i]]=s[y[i]]-c[i];} + } + fout< +using namespace std; + +struct nod +{ + int x,d; + nod *urm; +}; +nod *gr[30002],*q; +int n,m,X,Y,c[30005],ds[30005],viz[30005],i,x,y,d; +int inf=2000000000; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +void adaug(int x,int y,int d) +{ + nod *p=new nod; + p->x=y; + p->d=d; + p->urm=gr[x]; + gr[x]=p; +} + +int bfs() +{ + int pr=1,ul=1; + c[ul]=X; + for(int i=1;i<=n;i++) + ds[i]=inf; + ds[X]=0; + viz[X]=1; + while(pr<=ul) + { + for(q=gr[c[pr]];q!=NULL;q=q->urm) + { + if(viz[q->x]==0) + { + if(q->x>c[pr]) ds[q->x]=min(ds[q->x],ds[c[pr]]+q->d); + else ds[q->x]=min(ds[q->x],ds[c[pr]]-q->d); + viz[q->x]=1; + ul++; + c[ul]=q->x; + } + } + pr++; + } + return ds[Y]; +} + +int main() +{ + f>>n>>m>>X>>Y; + for(i=1;i<=m;i++) + { + f>>x>>y>>d; + adaug(x,y,d); + adaug(y,x,d); + } + + g< +#define x first +#define y second +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int N, M, X, Y; +vector > nod[30005]; +bool viz[30005]; +queue > coada; +void bfs(){ + while(!coada.empty()){ + int node = coada.front().x; + long suma = coada.front().y; + coada.pop(); + for(int i=0; inode) coada.push(make_pair(nod[node].at(i).x,suma+nod[node].at(i).y)); + else coada.push(make_pair(nod[node].at(i).x,suma-nod[node].at(i).y)); + if(nod[node].at(i).x==Y){ + if(nod[node].at(i).x>node) fout<>N>>M>>X>>Y; + + while(M--){ + int a, b, c; fin>>a>>b>>c; + nod[a].push_back(make_pair(b,c)); + nod[b].push_back(make_pair(a,c)); + } + coada.push(make_pair(X,0)); viz[X]=1; + bfs(); + + return 0; +} diff --git a/villages/bfs/35f0a2f5-40ad-11ec-af6b-dc7196033e92.cpp b/villages/bfs/35f0a2f5-40ad-11ec-af6b-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c1587925888b8d06351577cbd90af0de3296d952 --- /dev/null +++ b/villages/bfs/35f0a2f5-40ad-11ec-af6b-dc7196033e92.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n,m,xs,xf; +vector> L[30005]; +int D[30005],U[30005]; + +void read(){ + int x,y,t; + in>>n>>m>>xs>>xf; + for(int i=1;i<=m;i++){ + in>>x>>y>>t; + L[x].push_back({y,t}); + L[y].push_back({x,-t}); + } +} +void bfs(int st){ + int nod; + queue Q; + Q.push(st); + U[st]=1; + while(!Q.empty()){ + nod=Q.front(); + Q.pop(); + for(auto x : L[nod]){ + if(U[x.first]==0){ + U[x.first]=1; + D[x.first]=D[nod]+x.second; + Q.push(x.first); + } + } + } +} +void solve(){ + bfs(xs); + out< + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int a[100025],b[100025],d[100025]; +int s[100025];///daca stim dist de la x la i + +void read() +{ + int i; + fin>>n>>m>>x>>y; + for(i=1; i<=m; ++i)fin>>a[i]>>b[i]>>d[i]; + fin.close(); +} + +void solve() +{int i; + s[x]=1; + while(!s[y]) + + for(i=1;i<=m;++i) + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + else if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + + fout< +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; +vector< pair > L[30005]; +char S[1010]; + +int main() +{ + fin >> n >> m >> x >> y; + for (i=1; i<=m; i++) + { + //fin >> x1 >> y1 >> cost; + fin.get(); fin.get(S, 1005); + x1 = y1 = cost = 0; j = 0; + while (S[j] >= '0' && S[j] <= '9') + { + x1 = x1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + y1 = y1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + cost = cost*10+(S[j]-'0'); + j++; + } + L[x1].push_back(make_pair(y1, cost)); + L[y1].push_back(make_pair(x1, -cost)); + } + int p = 1; int u = 1; c[1] = x; v[x] = 1; + while (p <= u) + { + int nod = c[p]; + if (v[y]) + break; + for (i=0; i +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +const int N = 30002; +int d[N], c[N]; +vector < pair > v[N]; +void bfs(int ns){ + int st = 1, dr = 1, sz, nc; + d[ns] = 1; + c[1] = ns; + while(st <= dr){ + nc = c[st]; + sz = v[nc].size(); + for(int i=0;i>n>>m>>xi>>yi; + for(int i=1;i<=m;i++){ + in>>x>>y>>z; + v[x].push_back(make_pair(y,z)); + v[y].push_back(make_pair(x,z)); + } + in.close(); + bfs(xi); + if(xi > yi) + out<<1 - d[yi]<<"\n"; + else + out< +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y,cnt,k,i,j,p,u,c[30001],f[30001],drum[30001]; + +vector < pair > l[30001]; + +int main(){ + fin>>n>>m>>x>>y; + for(cnt=1;cnt<=m;cnt++){ + fin>>i>>j>>k; + l[i].push_back(make_pair(j,k)); + l[j].push_back(make_pair(i,0-k)); + } + + c[1]=x; + f[x]=1; + p=1; u=1; + + while(f[y]==0 && p<=u){ + for(k=0;k +#include +#define DEF 100100 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, start, dest, p, u, viz[DEF]; + +pair < int, int > c[DEF]; + +vector < pair < int, int > > D[DEF]; + +int main () { + fin >> n >> m >> start >> dest; + + if (start > dest) + swap (start, dest); + + for (int i = 1; i <= m; ++ i) { + int x, y, c; + fin >> x >> y >> c; + D[x].push_back (make_pair (y, c)); + D[y].push_back (make_pair (x, - c)); + } + + c[++ u] = make_pair (start, 0); + viz[start] = 1; + while (p <= u) { + int nod = c[p].first; + int cost = c[p].second; + + for (int i = 0; i < D[nod].size (); ++ i) { + if (viz[D[nod][i].first] == 0) { + viz[D[nod][i].first] = 1; + int new_cost = cost + D[nod][i].second; + + c[++ u] = make_pair (D[nod][i].first, new_cost); + + if (D[nod][i].first == dest) { + fout << new_cost; + return 0; + } + } + } + ++ p; + } + + return 0; +} diff --git a/villages/bfs/4b1b0683-df4f-4a57-ade2-023086debaf0.cpp b/villages/bfs/4b1b0683-df4f-4a57-ade2-023086debaf0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..36f8f299513c9af48e6a021ca44234e3da5912c9 --- /dev/null +++ b/villages/bfs/4b1b0683-df4f-4a57-ade2-023086debaf0.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +ifstream f ("sate.in"); +ofstream g ("sate.out"); +int a,b,t[30001],x[100025],y[100025],c[100025],n,m; +int main() +{ + f>>n>>m>>a>>b; + t[a]=1; + for(int i=1;i<=m;++i) f>>x[i]>>y[i]>>c[i]; + while(!t[b]) + { + for (int i=1;i<=m;++i) + { + if(t[x[i]]&&!t[y[i]]) t[y[i]]=t[x[i]]+c[i]; + else if(!t[x[i]]&&t[y[i]]) t[x[i]]=t[y[i]]-c[i]; + } + } + g< +using namespace std; + + + +int x,y,a[100050],b[100051],dist[30002],c[100051],n,m; +int main(){ + + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + scanf("%d%d%d%d",&n,&m,&x,&y); + + for(int i=1;i<=m;i++) + scanf("%d%d%d",&a[i],&b[i],&c[i]); + + dist[x]=1; + while(!dist[y]){ + for(int i=1;i<=m;i++) + { + if(dist[a[i]] && !dist[b[i]]) + dist[b[i]]=dist[a[i]]+c[i]; + else if(!dist[a[i]] && dist[b[i]]) + dist[a[i]]=dist[b[i]]-c[i]; + + + } + } + printf("%d\n",abs(dist[y])-1); + return 0; +} diff --git a/villages/bfs/4d522bf3-cc20-4475-a1a4-c2aff2a3df52.cpp b/villages/bfs/4d522bf3-cc20-4475-a1a4-c2aff2a3df52.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62549c60780780c448da2561cc312a4b21e25587 --- /dev/null +++ b/villages/bfs/4d522bf3-cc20-4475-a1a4-c2aff2a3df52.cpp @@ -0,0 +1,70 @@ +#include +#include +using namespace std; +class InputReader { +private: + FILE *inputFile; + static const int SIZE = 1 << 16; + char buffer[SIZE]; int cursor; + + inline void advance(void) { + if (++cursor == SIZE) { + cursor = 0; + fread(buffer, SIZE, sizeof(char), inputFile); + } + } + + inline char current(void) { + return buffer[cursor]; + } +public: + InputReader(const char* fileName) : cursor(0) { + inputFile = fopen(fileName, "r"); + fread(buffer, SIZE, 1, inputFile); + } + + ~InputReader(void) { + fclose(inputFile); + } + + template + InputReader &operator >>(type &value) { + value = 0; + while (current() < '0' or current() > '9') + advance(); + while (current() >= '0' and current() <= '9') { + value = value * 10 + current() - '0'; + advance(); + } + return *this; + } +} in("sate.in"); +ofstream out("sate.out"); +int a,b,c,n,m,x,y,v[30001],go,st,dr,i,hz[30001],distanta[30001]; +vector< pair >h[30001]; +int main(){ + in >> n >> m >> x >> y; + for( i = 1; i <= m; i ++ ){ + in >> a >> b >> c; + h[a].push_back( make_pair( b,c ) ); + h[b].push_back( make_pair( a,c ) ); + } + v[1] = x; + hz[x] = 1; + for( st = 1, dr = 1; st <= dr; st ++ ){ + for( i = 0; i < h[v[st]].size(); i ++ ){ + if( v[st] < h[v[st]][i].first ) + distanta[ h[v[st]][i].first ] = distanta[v[st]] + h[v[st]][i].second; + else + distanta[ h[v[st]][i].first ] = distanta[v[st]] - h[v[st]][i].second; + + if( hz[ h[v[st]][i].first ] == 0 ){ + hz[ h[v[st]][i].first ] = 1; + dr++; + v[dr] = h[v[st]][i].first; + } + } + } + out< +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/50c1887a-0295-4515-941d-f23c8c035dd5.cpp b/villages/bfs/50c1887a-0295-4515-941d-f23c8c035dd5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3d8c92901373b463f415a66e594bf9811f5d208 --- /dev/null +++ b/villages/bfs/50c1887a-0295-4515-941d-f23c8c035dd5.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +typedef pair p; +const int NMAX = 30005; +vector

G[NMAX]; +int N, M, X, Y; +bool use[NMAX]; +queue

q; + +void Read(){ + in >> N >> M >> X >> Y; + for(int i = 1; i <= M; ++i){ + int a, b, d; + in >> a >> b >> d; + G[a].push_back(make_pair(b, d)); + G[b].push_back(make_pair(a, d)); + } + q.push(make_pair(X, 0)); + use[X] = true; +} + +void SolveAndPrint(){ + while(!q.empty()){ + int node = q.front().first; + int cost = q.front().second; + if(node == Y){ + out << cost << "\n"; + break; + } + q.pop(); + for(int i = 0; i < G[node].size(); ++i){ + p neighbour = G[node][i]; + if(!use[neighbour.first]){ + use[neighbour.first] = true; + q.push(make_pair(neighbour.first, + cost + (neighbour.second * (node < neighbour.first ? + 1 : -1)))); + } + } + } +} + +int main(){ + Read(); + SolveAndPrint(); +} diff --git a/villages/bfs/50feef63-7089-45c0-be6f-dfbcc282c6df.cpp b/villages/bfs/50feef63-7089-45c0-be6f-dfbcc282c6df.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142493c543a15d87788cf97802b90084ec2f3204 --- /dev/null +++ b/villages/bfs/50feef63-7089-45c0-be6f-dfbcc282c6df.cpp @@ -0,0 +1,119 @@ +#include + +#include + + + +using namespace std; + + + +ifstream fin("sate.in"); + +ofstream fout("sate.out"); + + + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; + +vector< pair > L[30005]; + +char S[1010]; + + + +int main() + +{ + + fin >> n >> m >> x >> y; + + for (i=1; i<=m; i++) + + { + + //fin >> x1 >> y1 >> cost; + + fin.get(); fin.get(S, 1005); + + x1 = y1 = cost = 0; j = 0; + + while (S[j] >= '0' && S[j] <= '9') + + { + + x1 = x1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + y1 = y1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + cost = cost*10+(S[j]-'0'); + + j++; + + } + + L[x1].push_back(make_pair(y1, cost)); + + L[y1].push_back(make_pair(x1, -cost)); + + } + + int p = 1; int u = 1; c[1] = x; v[x] = 1; + + while (p <= u) + + { + + int nod = c[p]; + + if (v[y]) + + break; + + for (i=0; i +#define DIM 100025 +using namespace std; +int sol[30001],st[DIM],dr[DIM],x[DIM]; +int main() +{ int n,i,a,b,m; + ifstream f("sate.in"); + ofstream g("sate.out"); + f>>n>>m>>a>>b; + sol[a]=1; + for (i=1;i<=m;++i) + f>>st[i]>>dr[i]>>x[i]; + while (!sol[b]) { + for (i=1;i<=m;++i) + if (sol[st[i]] && !sol[dr[i]]) sol[dr[i]]=sol[st[i]]+x[i]; + else if (!sol[st[i]] && sol[dr[i]]) sol[st[i]]=sol[dr[i]]-x[i]; + } + g< +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y; +long int s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/bfs/543d4c48-728f-40dd-87f4-38b78d031070.cpp b/villages/bfs/543d4c48-728f-40dd-87f4-38b78d031070.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3075b113414311cf2efb51ed905fa97bf05a09f8 --- /dev/null +++ b/villages/bfs/543d4c48-728f-40dd-87f4-38b78d031070.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/54549d72-95ac-4740-bb39-9c3f72e05580.cpp b/villages/bfs/54549d72-95ac-4740-bb39-9c3f72e05580.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a69625bee54b10dd9c6782ace43a8b173f6a702f --- /dev/null +++ b/villages/bfs/54549d72-95ac-4740-bb39-9c3f72e05580.cpp @@ -0,0 +1,37 @@ +#include +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,X,Y,s[30002],a[100025],b[100025],d[100025]; + +int main() +{ + f>>N>>M>>X>>Y; + s[X]=1; + for(int i=1;i<=M;++i) + { + f>>a[i]>>b[i]>>d[i]; + } + + f.close(); + while(!s[Y]) + { + for(int i=1;i<=M;++i) + { + if(s[a[i]] && !s[b[i]]) + { + s[b[i]]=s[a[i]]+d[i]; + } + else + if(!s[a[i]] && s[b[i]]) + { + s[a[i]]=s[b[i]]-d[i]; + } + } + } + + g< +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< +#include +#define DEF 100100 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, start, dest, p, u, viz[DEF]; + +pair < int, int > c[DEF]; + +vector < pair < int, int > > D[DEF]; + +int main () { + fin >> n >> m >> start >> dest; + + if (start > dest) + swap (start, dest); + + for (int i = 1; i <= m; ++ i) { + int x, y, c; + fin >> x >> y >> c; + D[x].push_back (make_pair (y, c)); + D[y].push_back (make_pair (x, - c)); + } + + c[++ u] = make_pair (start, 0); + viz[start] = 1; + while (p <= u) { + int nod = c[p].first; + int cost = c[p].second; + + for (int i = 0; i < D[nod].size (); ++ i) { + if (viz[D[nod][i].first] == 0) { + viz[D[nod][i].first] = 1; + int new_cost = cost + D[nod][i].second; + + c[++ u] = make_pair (D[nod][i].first, new_cost); + + if (D[nod][i].first == dest) { + fout << new_cost; + return 0; + } + } + } + ++ p; + } + + return 0; +} diff --git a/villages/bfs/5a8dd1ed-40ad-11ec-b431-dc7196033e92.cpp b/villages/bfs/5a8dd1ed-40ad-11ec-b431-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..53146cb98790924190af01f7f61ae449abf1e620 --- /dev/null +++ b/villages/bfs/5a8dd1ed-40ad-11ec-b431-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int X, Y, n, m, a, b, c; +int f[30005]; +vector< pair > v[30005]; +queue< pair > q; +int main(){ + fin >> n >> m >> X >> Y; + for( int i = 1; i <= m; i++ ){ + fin >> a >> b >> c; + v[a].push_back( make_pair( b, c ) ); + v[b].push_back( make_pair( a, c ) ); + } + q.push( make_pair( X, 0 ) ); + f[X] = 1; + while( !q.empty() ){ + int nod = q.front().first; + int D = q.front().second; + for( int i = 0; i < v[nod].size(); i++ ){ + int vecin = v[nod][i].first; + int cost = v[nod][i].second; + if( f[vecin] == 0 ){ + if( vecin == Y ){ + if( vecin > nod ){ + fout << D + cost << "\n"; + }else{ + fout << D - cost << "\n"; + } + return 0; + } + if( vecin > nod ){ + q.push( make_pair( vecin, D + cost ) ); + }else{ + if( vecin < nod ){ + q.push( make_pair( vecin, D - cost ) ); + } + } + f[vecin] = 1; + } + } + q.pop(); + } + return 0; +} diff --git a/villages/bfs/5b2d88af-18b0-48ce-b0b8-4a67b9f3de73.cpp b/villages/bfs/5b2d88af-18b0-48ce-b0b8-4a67b9f3de73.cpp new file mode 100644 index 0000000000000000000000000000000000000000..999d4ddd77fe3e1616cbc6064f4ae71fe6bd5044 --- /dev/null +++ b/villages/bfs/5b2d88af-18b0-48ce-b0b8-4a67b9f3de73.cpp @@ -0,0 +1,30 @@ +#include +#include +#define DIM 30010 +#define X first +#define Y second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int c[DIM],f[DIM],p,u,i,j,nod,n,m,x,y,tot,d,w[DIM]; +vector< pair >L[DIM]; +int main(){ + fin>>n>>m>>x>>y; + while(fin>>i>>j>>d){ + L[i].push_back(make_pair(j,d)); + L[j].push_back(make_pair(i,-d)); + } + p=1; u=1; c[1]=x; f[x]=1; + while(p<=u){ + nod=c[p]; + for(i=0;i +#define x first +#define y second +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int N, M, X, Y; +vector > nod[30005]; +bool viz[30005]; +queue > coada; +void bfs(){ + while(!coada.empty()){ + int node = coada.front().x; + long suma = coada.front().y; + coada.pop(); + for(int i=0; inode) coada.push(make_pair(nod[node].at(i).x,suma+nod[node].at(i).y)); + else coada.push(make_pair(nod[node].at(i).x,suma-nod[node].at(i).y)); + if(nod[node].at(i).x==Y){ + if(nod[node].at(i).x>node) fout<>N>>M>>X>>Y; + + while(M--){ + int a, b, c; fin>>a>>b>>c; + nod[a].push_back(make_pair(b,c)); + nod[b].push_back(make_pair(a,c)); + } + coada.push(make_pair(X,0)); viz[X]=1; + bfs(); + + return 0; +} diff --git a/villages/bfs/5b6dfbd7-c33c-4fb3-a4bb-64f7f06f9a6a.cpp b/villages/bfs/5b6dfbd7-c33c-4fb3-a4bb-64f7f06f9a6a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142493c543a15d87788cf97802b90084ec2f3204 --- /dev/null +++ b/villages/bfs/5b6dfbd7-c33c-4fb3-a4bb-64f7f06f9a6a.cpp @@ -0,0 +1,119 @@ +#include + +#include + + + +using namespace std; + + + +ifstream fin("sate.in"); + +ofstream fout("sate.out"); + + + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; + +vector< pair > L[30005]; + +char S[1010]; + + + +int main() + +{ + + fin >> n >> m >> x >> y; + + for (i=1; i<=m; i++) + + { + + //fin >> x1 >> y1 >> cost; + + fin.get(); fin.get(S, 1005); + + x1 = y1 = cost = 0; j = 0; + + while (S[j] >= '0' && S[j] <= '9') + + { + + x1 = x1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + y1 = y1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + cost = cost*10+(S[j]-'0'); + + j++; + + } + + L[x1].push_back(make_pair(y1, cost)); + + L[y1].push_back(make_pair(x1, -cost)); + + } + + int p = 1; int u = 1; c[1] = x; v[x] = 1; + + while (p <= u) + + { + + int nod = c[p]; + + if (v[y]) + + break; + + for (i=0; i +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +#define lim 30010 +#define maxb 8192 + +int n,m,x,y,a,b,c; +int dist[lim]; +bool viz[lim]; +struct loc{int sat,d;}; +vector G[lim]; + +char buff[maxb]; +int pos=0; + +int get_int() +{ + int nr=0; + while(buff[pos]<'0' || buff[pos]>'9') + if(++pos==maxb) fin.read(buff,maxb),pos=0; + + while(buff[pos]>='0' && buff[pos]<='9') + { + nr=nr*10+buff[pos]-'0'; + if(++pos==maxb) fin.read(buff,maxb),pos=0; + } + return nr; +} + + +void bfs(int nod) +{ + queue q; + q.push(nod); + dist[nod]=0; + + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(auto it:G[nod]) + { + if(!viz[it.sat]) + { + viz[it.sat]=true; + if(it.sat>nod) + { + dist[it.sat]=dist[nod]+it.d; + q.push(it.sat); + } + if(it.sat +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +#define lim 30010 +#define maxb 8192 + +int n,m,x,y,a,b,c; +int dist[lim]; +bool viz[lim]; +struct loc{int sat,d;}; +vector G[lim]; + +char buff[maxb]; +int pos=0; + +int get_int() +{ + int nr=0; + while(buff[pos]<'0' || buff[pos]>'9') + if(++pos==maxb) fin.read(buff,maxb),pos=0; + + while(buff[pos]>='0' && buff[pos]<='9') + { + nr=nr*10+buff[pos]-'0'; + if(++pos==maxb) fin.read(buff,maxb),pos=0; + } + return nr; +} + + +void bfs(int nod) +{ + queue q; + q.push(nod); + dist[nod]=0; + + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(auto it:G[nod]) + { + if(!viz[it.sat]) + { + viz[it.sat]=true; + if(it.sat>nod) + { + dist[it.sat]=dist[nod]+it.d; + q.push(it.sat); + } + if(it.sat +#include +#include +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +vector > v[30001]; +queue q; +int d[30001]; +const int inf=2000000001; +int n,m,x,y; +void bfs() +{ + d[x]=0; + q.push(x); + while(!q.empty()){ + int pr=q.front(); + q.pop(); + for(int i=0;ik? d[pr]-v[pr][i].second : d[pr]+v[pr][i].second); + q.push(k); + } + } + } +} +int main() +{ + f>>n>>m>>x>>y; + int a,b,c; + for(int i=1;i<=m;i++){ + f>>a>>b>>c; + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + for(int i=1;i<=n;i++) + d[i]=inf; + bfs(); + g< +#define N 30020 +using namespace std; +vector lda[N]; +int v[N], l[N], rs[N], dr=1, d; +void bfs(int st){ + if(st<=dr){ + int n=lda[l[st]].size(); + for(int i=0;i x)rs[x]=rs[l[st]]-lda[l[st]][i+1]; else rs[x]=rs[l[st]]+lda[l[st]][i+1]; + if(x==d){ + st=dr; + break; + } + } + } + bfs(st+1); + } +} +int main(){ + int n, m, x, y, s, ds; + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + scanf("%d%d%d%d", &n, &m, &s, &d); + while(m--){ + scanf("%d%d%d", &x, &y, &ds); + lda[x].push_back(y); + lda[x].push_back(ds); + lda[y].push_back(x); + lda[y].push_back(ds); + } + l[1]=s; + bfs(1); + printf("%d", rs[d]); + return 0; +} diff --git a/villages/bfs/5df32bcf-40ad-11ec-8c70-dc7196033e92.cpp b/villages/bfs/5df32bcf-40ad-11ec-8c70-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d2da7661f7a373219db14d2b2127b74346dc64bf --- /dev/null +++ b/villages/bfs/5df32bcf-40ad-11ec-8c70-dc7196033e92.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +using namespace std; +const int maxim = 30007; +int n,m,vaz[maxim],x,y; +long long d[maxim]; +vector > drum[maxim]; +queue q; +void bfs() +{ +vaz[x]=true; +q.push(x); +while(!q.empty()) +{ +x=q.front(); +q.pop(); +if(x==y) +{ +break; +} +for(int i=0;id[x]+drum[x][i].second) +{ +vaz[drum[x][i].first]=true; +d[drum[x][i].first]=d[x]+drum[x][i].second; +q.push(drum[x][i].first); +} +} +} +} +int main() +{ +int sat1,sat2,l; +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); +fin>>n>>m>>x>>y; +if(x>y) swap(x,y); +for(int i=1;i<=m;++i) +{ +fin>>sat1>>sat2>>l; +drum[sat1].push_back(make_pair(sat2, l)); +l*= -1; +drum[sat2].push_back(make_pair(sat1, l)); +} +bfs(); +fout< +#include +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +#define lim 30010 +#define maxb 200000 + +int n,m,x,y,a,b,c; +int dist[lim]; +bool viz[lim]; +struct loc{int sat,d;}; +vector G[lim]; + +char buff[maxb]; +int pos=0; + +int get_int() +{ + int nr=0; + while(buff[pos]<'0' || buff[pos]>'9') + if(++pos==maxb) fin.read(buff,maxb),pos=0; + + while(buff[pos]>='0' && buff[pos]<='9') + { + nr=nr*10+buff[pos]-'0'; + if(++pos==maxb) fin.read(buff,maxb),pos=0; + } + return nr; +} + + +void bfs(int nod) +{ + queue q; + q.push(nod); + dist[nod]=0; + + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(auto it:G[nod]) + { + if(!viz[it.sat]) + { + viz[it.sat]=true; + if(it.sat>nod) + { + dist[it.sat]=dist[nod]+it.d; + q.push(it.sat); + } + if(it.sat +#include + +#define FIN "sate.in" +#define FOUT "sate.out" +#define MAXN 30001 +#define MAXM 100100 + +int n, m, x, y; +bool Used[MAXN]; + +struct elem +{ + int x, c; + elem *r; +} * L[MAXN]; + +void push(int a, int b, int c) +{ + elem *temp = new elem; + temp->x = b; + temp->c = c; + temp->r = L[a]; + L[a] = temp; +} + +void citire() +{ + int i, a, b, c; + freopen(FIN, "r", stdin); + freopen(FOUT, "w", stdout); + scanf("%d%d%d%d", &n, &m, &x, &y); + for (i = 0; i < m; ++i) + { + scanf("%d %d %d", &a, &b, &c); + push(a, b, c); + push(b, a, -c); + } +} + +struct coada +{ + int x; + long long d; +} C[MAXN]; + +void bf(int s) +{ + int p, u = 0; + C[u++] = (coada){s, 0}; + Used[s] = 1; + + for (p = 0; p < u; ++p) + { + if (C[p].x == y) + break; + for (elem *k = L[C[p].x]; k; k = k->r) + if (!Used[k->x]) + C[u++] = (coada){k->x, C[p].d + k->c}, Used[k->x] = 1; + } + printf("%lld\n", C[p].d); +} + +int main() +{ + citire(); + bf(x); + return 0; +} \ No newline at end of file diff --git a/villages/bfs/5f05021e-40ad-11ec-95ca-dc7196033e92.cpp b/villages/bfs/5f05021e-40ad-11ec-95ca-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb216a9a55d4c81bc80881acea942ed6afba8af8 --- /dev/null +++ b/villages/bfs/5f05021e-40ad-11ec-95ca-dc7196033e92.cpp @@ -0,0 +1,69 @@ +#include +#include +#define N_MAX 30005 +using namespace std; + +int n, m, X, Y, d[N_MAX]; +queueQ; +bool used[N_MAX]; +struct nod +{ + int val, cost; + nod *next; +}; + +nod *L[N_MAX]; + +inline void add(int x, int y, int c) +{ + nod *elem = new nod; + elem->val = y; + elem->cost = c; + elem->next = L[x]; + L[x] = elem; +} + +void BFS(int x) +{ + int Nod; + nod *p; + used[x] = 1; + Q.push(x); + while(!Q.empty() && !used[Y]) + { + Nod = Q.front(); + Q.pop(); + p = L[Nod]; + while(p != NULL) + { + if(!used[p->val]) + { + used[p->val] = 1; + d[p->val] = d[Nod] + p->cost; + Q.push(p->val); + } + p = p->next; + } + } +} + + +int main() +{ + int i, a, b, c; + FILE *fin, *fout; + fin = fopen("sate.in","r"); + fout = fopen("sate.out","w"); + fscanf(fin,"%d%d%d%d",&n,&m,&X,&Y); + for(i=1; i<=m; i++) + { + fscanf(fin,"%d%d%d",&a,&b,&c); + add(a,b,c); + add(b,a,-c); + } + fclose(fin); + BFS(X); + fprintf(fout,"%d\n",d[Y]); + fclose(fout); + return 0; +} diff --git a/villages/bfs/5f371173-40ad-11ec-84dc-dc7196033e92.cpp b/villages/bfs/5f371173-40ad-11ec-84dc-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9c5185e5eb264e115adb24da4ce14cead6e7893b --- /dev/null +++ b/villages/bfs/5f371173-40ad-11ec-84dc-dc7196033e92.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#define N_MAX 30005 + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int N, M , X, Y, drum[N_MAX]; +bool viz[N_MAX]; + +vector < pair > v[N_MAX]; +queue q; + +void citire() +{ + int i, sat1, sat2, dist; + + for(i = 1; i <= M; i++) + { + fin>>sat1>>sat2>>dist; + + v[sat1].push_back(make_pair(sat2, dist)); + v[sat2].push_back(make_pair(sat1, -dist)); + } +} + +void BFS() +{ + int aux, i; + + while(!q.empty()) + { + aux = q.front(); + q.pop(); + + for(i = 0 ; i < v[aux].size(); i++) + { + if(viz[v[aux][i].first] == false || drum[aux] + v[aux][i].second < drum[v[aux][i].first]) + { + viz[v[aux][i].first] = true; + drum[v[aux][i].first] = drum[aux] + v[aux][i].second; + q.push(v[aux][i].first); + } + } + } +} + +int main() +{ + fin>>N>>M>>X>>Y; + + citire(); + + q.push(X); + viz[X] = true; + + BFS(); + + fout< +#include +#include +#include +#include + +using namespace std; +#define SatDist pair +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector arr[100030]; +int n, m, x, y, sat, _sat, dist; +queue coada; +int vec[100000]; + +int nod; + +int main(){ + //for(int i = 0; i < 100; i++)vec[i] = -1; + memset(vec,-1,sizeof(vec)); + fin >> n >> m >> x >> y; + for(int i = 0; i < m; i++){ + fin >> sat >> _sat >> dist; + arr[sat].push_back({_sat, dist}); + arr[_sat].push_back({sat, dist}); + } + + //for(int i = 0; i < m; ) + + + vec[x] = 0; + coada.push(x); + while(!coada.empty()){ + nod = coada.front(); + for(int i = 0; i < arr[nod].size(); i++){ + if(vec[arr[nod][i].first] == -1){ + if(nod < arr[nod][i].first) + vec[arr[nod][i].first] = vec[nod] + arr[nod][i].second; + else + vec[arr[nod][i].first] = vec[nod] - arr[nod][i].second; + coada.push(arr[nod][i].first); + } + } + coada.pop(); + } + + fout << vec[y]; + fout.close(); + fin.close(); + return 0; +} diff --git a/villages/bfs/5f755693-40ad-11ec-8df1-dc7196033e92.cpp b/villages/bfs/5f755693-40ad-11ec-8df1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a9cf548d77af31a33cad9d0ec7cecef33247978 --- /dev/null +++ b/villages/bfs/5f755693-40ad-11ec-8df1-dc7196033e92.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +using namespace std; +const int MAXN = 30005; + +int N, M, X, Y; +vector > g[MAXN]; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int main() { + fin >> N >> M >> X >> Y; + while(M --) { + int x, y, z; + fin >> x >> y >> z; + g[x].push_back(make_pair(y, z)); + g[y].push_back(make_pair(x, -z)); + } + vector dp(N + 1, 1 << 30); + dp[X] = 0; + queue q; + q.push(X); + while(!q.empty()) { + int nr,node = q.front(); + nr=g[node].size(); + for(int i=0;i dp[node] + g[node][i].second) { + dp[g[node][i].first] = dp[node] + g[node][i].second; + q.push(g[node][i].first); + } + } + q.pop(); + } + fout << dp[Y] << '\n'; +} diff --git a/villages/bfs/6086456c-8cb8-4389-a767-6966c298a69f.cpp b/villages/bfs/6086456c-8cb8-4389-a767-6966c298a69f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d09b904f5e10d019753b6bb2b98348086856f2f4 --- /dev/null +++ b/villages/bfs/6086456c-8cb8-4389-a767-6966c298a69f.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +const int N = 30010, M = 100025; + +int n, m, a, b, v[N], x[M], y[M], z[M], i; + +int main() { + fin >> n >> m >> a >> b; + v[a] = 1; + for (i = 1; i <= m; ++i) { + fin >> x[i] >> y[i] >> z[i]; + } + while (!v[b]) { + for (i = 1; i <= m; ++i) { + if (v[ x[i] ] && !v[ y[i] ]) { + v[ y[i] ] = v[ x[i] ] + z[i]; + } + else if (!v[ x[i] ] && v[ y[i] ]) { + v[ x[i] ] = v[ y[i] ] - z[i]; + } + } + } + fout << v[b] - 1; +} diff --git a/villages/bfs/61f6684f-40ad-11ec-877e-dc7196033e92.cpp b/villages/bfs/61f6684f-40ad-11ec-877e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f2d7c9de9fd98f1e51fe75a319e1d9e28f092f24 --- /dev/null +++ b/villages/bfs/61f6684f-40ad-11ec-877e-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +using namespace std; +vector < pair > g[30010]; +int ans[30010],viz[30010]; +queue q; +int main() +{ + ifstream fin ("sate.in"); + ofstream fout ("sate.out"); + int n,m,x,y,a,b,c,i,nod; + fin>>n>>m>>x>>y; + for(i=1; i<=m; i++) + { + fin>>a>>b>>c; + g[a].push_back(make_pair(b,c)); + g[b].push_back(make_pair(a,c)); + } + ans[x]=0; + q.push(x); + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(i=0; inod) + ans[g[nod][i].first]=ans[nod]+g[nod][i].second; + else + ans[g[nod][i].first]=ans[nod]-g[nod][i].second; + q.push(g[nod][i].first); + } + } + } + fout< +#include +#include + +using namespace std; + +#define DIM 10000 +char buff[DIM]; +int poz=0; + +void citeste(int &numar) +{ + numar = 0; + while (buff[poz] < '0' || buff[poz] > '9') + if (++poz == DIM) + fread(buff,1,DIM,stdin),poz=0; + while ('0'<=buff[poz] && buff[poz]<='9') + { + numar = numar*10 + buff[poz] - '0'; + if (++poz == DIM) + fread(buff,1,DIM,stdin),poz=0; + } +} + +int n,m,x,y,s,d,c; +int cs[30001]; +bool viz[30001]; +queue q; + +struct sat +{ + vector vecini; + vector cost; +} a[30001]; + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + citeste(n); + citeste(m); + citeste(x); + citeste(y); + if(x>y) + { + int t=y; + y=x; + x=t; + } + + for(int i=1;i<=m;i++) + { + citeste(s); + citeste(d); + citeste(c); + if(s>d) + { + int t=d; + d=s; + s=t; + } + a[s].vecini.push_back(d); + a[s].cost.push_back(c); + a[d].vecini.push_back(s); + a[d].cost.push_back(-c); + } + + q.push(x); + int current; + while(!q.empty()) + { + current=q.front(); + q.pop(); + if(current==y) + { + break; + } + int vc; + int dst; + while(!a[current].vecini.empty()) + { + vc=a[current].vecini.back(); + dst=a[current].cost.back(); + if(viz[vc]==0) + { + viz[vc]=1; + cs[vc]=cs[current]+dst; + q.push(vc); + } + a[current].vecini.pop_back(); + a[current].cost.pop_back(); + } + } + printf("%d",cs[y]); +} diff --git a/villages/bfs/637d1f5f-40ad-11ec-b002-dc7196033e92.cpp b/villages/bfs/637d1f5f-40ad-11ec-b002-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ecdbfcf77afcf55f3bbf954c31f87f3ff66e0073 --- /dev/null +++ b/villages/bfs/637d1f5f-40ad-11ec-b002-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#define NMAX 30001 +using namespace std; +vector < pair > v[NMAX]; +long long viz[NMAX]; +int stiv [NMAX]; +int main (){ + FILE *in,*out; + in = fopen ("sate.in","r"); + out = fopen ("sate.out","w"); + int n,m,i,nods,nodf,inc,sf,pp,a,b,l,nod,DIM; + fscanf(in,"%d%d%d%d",&n,&m,&nods,&nodf); + for (i=1;i<=m;i++){ + fscanf(in,"%d%d%d",&a,&b,&l); + v[a].push_back(make_pair(b,l)); + v[b].push_back(make_pair(a,l));/// reciproca + } + + for (i=1;i<=n;i++) + viz[i] = -1LL; + inc = sf = 1; + stiv[inc] = nods; + viz[nods] = 0LL; + pp = 0; + while (inc <= sf && pp == 0){ + nod = stiv[inc]; + DIM = v[nod].size(); + for (i = 0; i < DIM;i ++){ + if (viz[v[nod][i].first] == -1){ + if (v[nod][i].first > nod) + viz[v[nod][i].first] = viz[nod] + v[nod][i].second; + else + viz[v[nod][i].first] = viz[nod] - v[nod][i].second; + stiv[++sf] = v[nod][i].first; + ///if (v[nod][i].first == nodf) + /// pp = 1; + } + } + inc ++; + } + + fprintf(out,"%d",viz[nodf]); + + + + fclose (in); + fclose (out); + return 0; +} + + diff --git a/villages/bfs/63b68245-40ad-11ec-84e6-dc7196033e92.cpp b/villages/bfs/63b68245-40ad-11ec-84e6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26544ccb702fbe08c440466f49db9a1b15a5e05b --- /dev/null +++ b/villages/bfs/63b68245-40ad-11ec-84e6-dc7196033e92.cpp @@ -0,0 +1,60 @@ +# include +# include +# include +# include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAX = 30005; +const int INF = (1<<30); +int n, m, x, y; +int dist[MAX]; +struct inf { int v, c; }; +vector L[MAX]; +queue q; +bitset viz; + +inf per(int a, int b) { + inf X; + X.v = a; + X.c = b; + return X; +} + +int main() { + int px, py, pc, nod; + fin >> n >> m >> x >> y; + for (int i=1; i<=m; i++) { + fin >> px >> py >> pc; + L[px].push_back(per(py, pc)); + L[py].push_back(per(px, -pc)); + } + + viz[x] = 1; + vector::iterator it; + for (it=L[x].begin(); it!=L[x].end(); it++) { + dist[it->v] = it->c; + q.push(it->v); + viz[it->v] = 1; + } + + + while (!q.empty()) { + nod = q.front(); + for (it=L[nod].begin(); it!=L[nod].end(); it++) { + if (viz[it->v] == 0) { + dist[it->v] = dist[nod] + it->c; + viz[it->v] = 1; + q.push(it->v); + } + } + q.pop(); + } + + fout << dist[y]; + + return 0; +} diff --git a/villages/bfs/66e81145-c7ac-4644-a965-5330ee041e61.cpp b/villages/bfs/66e81145-c7ac-4644-a965-5330ee041e61.cpp new file mode 100644 index 0000000000000000000000000000000000000000..588dfb9b1dd9e0f795f9632188f1b4613c78150e --- /dev/null +++ b/villages/bfs/66e81145-c7ac-4644-a965-5330ee041e61.cpp @@ -0,0 +1,31 @@ +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); +const int MAX=100006; +int n,m,x,y,st[MAX],dr[MAX],c[MAX]; +int d[MAX]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + in>>st[i]>>dr[i]>>c[i]; + } + d[x]=1; + + + while(!d[y]) + { + for(int i=1;i<=m;i++) + { + if(!d[st[i]] && d[dr[i]]) d[st[i]]=d[dr[i]]-c[i]; + else if(d[st[i]] && !d[dr[i]]) d[dr[i]]=d[st[i]]+c[i]; + } + } + out< +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int NOD1,NOD2; +vector < int > noduri[30005]; +vector < int > dt[30005]; +int vizitat[30005]; +int distanta[30005]; +int distnc; + +void graf(int NOD, int distnc) { + + distanta[NOD]= distnc; + vizitat[NOD]=1; + + for (int i=0;i> n; + fin>> m; + fin >> NOD1; + fin >> NOD2; + + for (int i=1;i<=m;i++) { + fin >> x >> y >> distnc; + noduri[x].push_back(y); + noduri[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graf(NOD1,0); + fout << distanta[NOD2]; + return 0; +} diff --git a/villages/bfs/693a7fa8-af86-43c0-9e19-3f0276e10ffb.cpp b/villages/bfs/693a7fa8-af86-43c0-9e19-3f0276e10ffb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c695fcc6717aea006d2d521361da06d428d614c4 --- /dev/null +++ b/villages/bfs/693a7fa8-af86-43c0-9e19-3f0276e10ffb.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +int nodes, edges, X, Y, u, v, cost, visited[30001]; + +vector > adj[30001]; + +int BFS(int start) +{ + queue > q; q.push({start, 0}); + + while(!q.empty()) + { + int current = q.front().first, + cost = q.front().second; + + if(current == Y) return cost; + + q.pop(); + + for(auto child : adj[current]) + { + if(!visited[child.first]) + { + q.push({child.first, child.second + cost}); + + visited[child.first] = 1; + } + } + } +} + +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + scanf("%d %d %d %d", &nodes, &edges, &X, &Y); + + for(int i = 1; i <= edges; i++) + { + scanf("%d %d %d", &u, &v, &cost); + + adj[u].push_back({v, cost}); + + adj[v].push_back({u, -cost}); + } + + printf("%d", BFS(X)); + + return 0; +} diff --git a/villages/bfs/6b4c478b-0429-4bfb-85ba-727d94d98a3e.cpp b/villages/bfs/6b4c478b-0429-4bfb-85ba-727d94d98a3e.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62dac8544b481b88a0d06beb00a2406a385a869a --- /dev/null +++ b/villages/bfs/6b4c478b-0429-4bfb-85ba-727d94d98a3e.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int a[100025],b[100025],d[100025]; +int s[100025];///daca stim dist de la x la i + +void read() +{ + int i; + fin>>n>>m>>x>>y; + for(i=1; i<=m; ++i)fin>>a[i]>>b[i]>>d[i]; + fin.close(); +} + +void solve() +{int i; + s[x]=1; + while(!s[y]) + + for(i=1;i<=m;++i) + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + else if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + + fout< +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int dist[100001]; +vector >vec[100001]; +queue < int > q; + +int main() +{ + int N,M,X,Y,i,j,D; + f>>N>>M>>X>>Y; + for(int h=1;h<=M;h++) + { + f>>i>>j>>D; + vec[i].push_back({j,D}); + vec[j].push_back({i,D}); + } + q.push(X); + while(!q.empty()) + { + X=q.front(); + q.pop(); + for(int h=0;h +#include +#include +#include +#include + +#define sat_dist std::pair + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +vector vec[100002]; +queue coada; +int distante[100002]; + +int main() +{ + memset(distante,-1,sizeof(distante)); + int sat,sat_conect, distance,nod; + fin>>n>>m>>x>>y; + for(int i=0;i>sat>>sat_conect>>distance; + vec[sat].push_back(make_pair(sat_conect,distance)); + vec[sat_conect].push_back(make_pair(sat,distance)); + } + distante[x]=0; + coada.push(x); + while(!coada.empty()) + { + nod=coada.front(); + for(int i=0;i +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; +vector< pair > L[30005]; +char S[1010]; + +int main() +{ + fin >> n >> m >> x >> y; + for (i=1; i<=m; i++) + { + //fin >> x1 >> y1 >> cost; + fin.get(); fin.get(S, 1005); + x1 = y1 = cost = 0; j = 0; + while (S[j] >= '0' && S[j] <= '9') + { + x1 = x1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + y1 = y1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + cost = cost*10+(S[j]-'0'); + j++; + } + L[x1].push_back(make_pair(y1, cost)); + L[y1].push_back(make_pair(x1, -cost)); + } + int p = 1; int u = 1; c[1] = x; v[x] = 1; + while (p <= u) + { + int nod = c[p]; + if (v[y]) + break; + for (i=0; i +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,X,Y,s[30002],a[100025],b[100025],d[100025]; + +int main() +{ + f>>N>>M>>X>>Y; + s[X]=1; + for(int i=1;i<=M;++i) + { + f>>a[i]>>b[i]>>d[i]; + } + + f.close(); + while(!s[Y]) + { + for(int i=1;i<=M;++i) + { + if(s[a[i]] && !s[b[i]]) + { + s[b[i]]=s[a[i]]+d[i]; + } + else + if(!s[a[i]] && s[b[i]]) + { + s[a[i]]=s[b[i]]-d[i]; + } + } + } + + g< +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,p,y; + +vector > g[30100]; +int v[30100]; + + +void bfs(int x){ + + list q; + + q.push_back(x); + v[x] = 0; + + while(!q.empty()){ + + x = q.front(); + q.pop_front(); + + for(int i = 0; i < g[x].size(); i++){ + if(v[(g[x][i]).first] == -1){ + q.push_back((g[x][i]).first); + + if((g[x][i]).first > x) + v[(g[x][i]).first] = v[x] + (g[x][i]).second; + else + v[(g[x][i]).first] = v[x] - (g[x][i]).second; + } + } + + } + +} + + +int main(){ + fin >> n>>m >>p>>y; + int t1,t2, d; + for(int i = 0; i>t1>>t2>>d; + g[t1].push_back(make_pair(t2, d)); + g[t2].push_back(make_pair(t1, d)); + } + memset(v, -1, sizeof(v)); + bfs(p); + fout << v[y]; +} diff --git a/villages/bfs/7869aa86-cda2-43f8-b22c-23e78bcde8d3.cpp b/villages/bfs/7869aa86-cda2-43f8-b22c-23e78bcde8d3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bfd75ad39cbe95bf1afffc1cbb53e5713068424 --- /dev/null +++ b/villages/bfs/7869aa86-cda2-43f8-b22c-23e78bcde8d3.cpp @@ -0,0 +1,20 @@ +#include +#define DIM 100025 +using namespace std; +int sol[30001],st[DIM],dr[DIM],x[DIM]; +int main() +{ int n,i,a,b,m; + ifstream f("sate.in"); + ofstream g("sate.out"); + f>>n>>m>>a>>b; + sol[a]=1; + for (i=1;i<=m;++i) + f>>st[i]>>dr[i]>>x[i]; + while (!sol[b]) { + for (i=1;i<=m;++i) + if (sol[st[i]] && !sol[dr[i]]) sol[dr[i]]=sol[st[i]]+x[i]; + else if (!sol[st[i]] && sol[dr[i]]) sol[st[i]]=sol[dr[i]]-x[i]; + } + g< +#define Nmax 100025 +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +int n,m,x,y,a[Nmax],b[Nmax],d[Nmax],viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>a[i]>>b[i]>>d[i]; + viz[x]=1; + while(!viz[y]) + for(int i=1;i<=m;i++) + if(viz[a[i]] && !viz[b[i]]) viz[b[i]]= viz[a[i]]+d[i]; + else + if(!viz[a[i]] && viz[b[i]]) viz[a[i]]= viz[b[i]]-d[i]; + g< +#include +#include +#include +using namespace std; +vector < pair > g[30010]; +int ans[30010],viz[30010]; +queue q; +int main() +{ + ifstream fin ("sate.in"); + ofstream fout ("sate.out"); + int n,m,x,y,a,b,c,i,nod; + fin>>n>>m>>x>>y; + for(i=1; i<=m; i++) + { + fin>>a>>b>>c; + g[a].push_back(make_pair(b,c)); + g[b].push_back(make_pair(a,c)); + } + ans[x]=0; + q.push(x); + while(!q.empty()) + { + nod=q.front(); + q.pop(); + for(i=0; inod) + ans[g[nod][i].first]=ans[nod]+g[nod][i].second; + else + ans[g[nod][i].first]=ans[nod]-g[nod][i].second; + q.push(g[nod][i].first); + } + } + } + fout< +#include +#include +#define NMAX 30001 +using namespace std; +vector < pair > v[NMAX]; +long long viz[NMAX]; +int stiv [NMAX]; +int main (){ + FILE *in,*out; + in = fopen ("sate.in","r"); + out = fopen ("sate.out","w"); + int n,m,i,nods,nodf,inc,sf,pp,a,b,l,nod,DIM; + fscanf(in,"%d%d%d%d",&n,&m,&nods,&nodf); + for (i=1;i<=m;i++){ + fscanf(in,"%d%d%d",&a,&b,&l); + v[a].push_back(make_pair(b,l)); + v[b].push_back(make_pair(a,l));/// reciproca + } + + for (i=1;i<=n;i++) + viz[i] = -1LL; + inc = sf = 1; + stiv[inc] = nods; + viz[nods] = 0LL; + pp = 0; + while (inc <= sf && pp == 0){ + nod = stiv[inc]; + DIM = v[nod].size(); + for (i = 0; i < DIM;i ++){ + if (viz[v[nod][i].first] == -1){ + if (v[nod][i].first > nod) + viz[v[nod][i].first] = viz[nod] + v[nod][i].second; + else + viz[v[nod][i].first] = viz[nod] - v[nod][i].second; + stiv[++sf] = v[nod][i].first; + ///if (v[nod][i].first == nodf) + /// pp = 1; + } + } + inc ++; + } + + fprintf(out,"%d",viz[nodf]); + + + + fclose (in); + fclose (out); + return 0; +} + + diff --git a/villages/bfs/7d66f212-9935-46aa-9810-bd882d1ff4e7.cpp b/villages/bfs/7d66f212-9935-46aa-9810-bd882d1ff4e7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a84083f5425290ede8cd6a621b6894dcf5090238 --- /dev/null +++ b/villages/bfs/7d66f212-9935-46aa-9810-bd882d1ff4e7.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,i; +int a[100100] , b[100100] , d[100100]; +int drum[100100]; +int main() +{ + fin>>x>>m>>x>>y; + for(i=1;i<=m;i++) + fin>>a[i]>>b[i]>>d[i]; + drum[x]=1; + while(!drum[y]) + for(i=1;i<=m;i++) + if(!drum[a[i]] && drum[b[i]]) + drum[a[i]]=drum[b[i]]-d[i]; + else if(drum[a[i]] && !drum[b[i]]) + drum[b[i]]=drum[a[i]]+d[i]; + fout< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int sol[30010],x[100050],y[100050],km[100050]; +int a,b,n,m; +int main(){ + fin >> n >> m >> a >> b; + sol[a]=1; + for (int i=1;i<=m;i++) + fin >> x[i] >> y[i] >> km[i]; + while (!sol[b]){ + for (int i=1;i<=m;i++){ + if (sol[x[i]] && !sol[y[i]]) sol[y[i]]=sol[x[i]]+km[i]; + else if (!sol[x[i]] && sol[y[i]]) sol[x[i]]=sol[y[i]]-km[i]; + } + } + fout << sol[b]-1; +} diff --git a/villages/bfs/7ea107d6-7fbc-423c-b7b2-cb22616226e8.cpp b/villages/bfs/7ea107d6-7fbc-423c-b7b2-cb22616226e8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5ed56fdc07792720526807379beb0476af66f3d --- /dev/null +++ b/villages/bfs/7ea107d6-7fbc-423c-b7b2-cb22616226e8.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +const int NMAX = 30002; +const int MMAX = 100026; +int N,M,X,Y; +int a,b,c; +int dist[NMAX]; +int x[MMAX],y[MMAX],d[MMAX]; +void Read() +{ + f>>N>>M>>X>>Y; + for(int i=1; i<=M; ++i) + { + f>>a>>b>>c; + x[i]=a; + y[i]=b; + d[i]=c; + } +} +void Do() +{ + dist[X]=1; + while(!dist[Y]) + { + for(int i=1; i<=M; ++i) + { + if(dist[x[i]] && !dist[y[i]]) + dist[y[i]]=dist[x[i]]+d[i]; + if(!dist[x[i]] && dist[y[i]]) + dist[x[i]]=dist[y[i]]-d[i]; + } + } + g< +using namespace std; + + + +int x,y,a[100050],b[100051],dist[30002],c[100051],n,m; +int main(){ + + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + scanf("%d%d%d%d",&n,&m,&x,&y); + + for(int i=1;i<=m;i++) + scanf("%d%d%d",&a[i],&b[i],&c[i]); + + dist[x]=1; + while(!dist[y]){ + for(int i=1;i<=m;i++) + { + if(dist[a[i]] && !dist[b[i]]) + dist[b[i]]=dist[a[i]]+c[i]; + else if(!dist[a[i]] && dist[b[i]]) + dist[a[i]]=dist[b[i]]-c[i]; + + + } + } + printf("%d\n",abs(dist[y])-1); + return 0; +} diff --git a/villages/bfs/80745d94-003e-4b53-a39d-75362b17e571.cpp b/villages/bfs/80745d94-003e-4b53-a39d-75362b17e571.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a84083f5425290ede8cd6a621b6894dcf5090238 --- /dev/null +++ b/villages/bfs/80745d94-003e-4b53-a39d-75362b17e571.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,i; +int a[100100] , b[100100] , d[100100]; +int drum[100100]; +int main() +{ + fin>>x>>m>>x>>y; + for(i=1;i<=m;i++) + fin>>a[i]>>b[i]>>d[i]; + drum[x]=1; + while(!drum[y]) + for(i=1;i<=m;i++) + if(!drum[a[i]] && drum[b[i]]) + drum[a[i]]=drum[b[i]]-d[i]; + else if(drum[a[i]] && !drum[b[i]]) + drum[b[i]]=drum[a[i]]+d[i]; + fout< +#define nr first +#define cost second +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y; + +int a, b, c; +vector< pair > l[30005]; +/// (vecin, cost) +bool vizitat[30005]; +long long d[30005]; + +void bfs( int start ) +{ + queue coada; + int nod; + pair vecin; + + coada.push(start); + vizitat[start] = true; + + while( !coada.empty() ) + { + nod = coada.front(); + coada.pop(); + + for( unsigned int i = 0; i < l[nod].size(); i++ ) + { + vecin = l[nod][i]; + + if( vizitat[ vecin.nr ] == false ) + { + coada.push( vecin.nr ); + vizitat[ vecin.nr ] = true; + d[ vecin.nr ] = d[nod] + vecin.cost * ( (vecin.nr > nod)? 1 : -1 ); + } + } + } +} + +int main() +{ + in>>n>>m>>x>>y; + for( int i = 1; i <= m; i++ ) + { + in>>a>>b>>c; + l[a].push_back( make_pair(b, c) ); + l[b].push_back( make_pair(a, c) ); + } + + bfs(x); + + out< +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/823ddf12-2b4a-4967-9c2c-05ff733e5b50.cpp b/villages/bfs/823ddf12-2b4a-4967-9c2c-05ff733e5b50.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ed57cd0054534fb23766f0b594911d6ccb9f442 --- /dev/null +++ b/villages/bfs/823ddf12-2b4a-4967-9c2c-05ff733e5b50.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int a, b, s[30001], x[100001], y[100001], c[100001], n, m; + +int main(){ + int i; + fin>>n>>m>>a>>b; + s[a]=1; + for (i=1;i<=m;i++) + fin>>x[i]>>y[i]>>c[i]; + while(!s[b]){ + for (int i=1; i<=m; i++){ + if (s[x[i]] && !s[y[i]]) + s[y[i]]=s[x[i]]+c[i]; + else if (!s[x[i]] && s[y[i]]) + s[x[i]]=s[y[i]]-c[i];} + } + fout< +#include +using namespace std; +ifstream cin("sate.in"); +ofstream cout("sate.out"); +vector>G; +vector>H; +int main() +{ + int N, M, X, Y; + cin >> N >> M >> X >> Y; + G = vector>(N + 1); + H = vector>(N + 1); + int x, y, cost; + for(int i = 1; i <= M; i++){ + cin >> x >> y >> cost; + G[x].push_back(y); H[x].push_back(cost); + G[y].push_back(x); H[y].push_back(cost); + } + const int k = 30010; + int coada[k] = {}, viz[k] = {}, inc = 1, Sf = 1, l = 1, sol[k] = {}; + coada[1] = X; + viz[X] = 1; + while(inc <= Sf){ + int nc = coada[inc]; + //for(int j: G[nc]) + for(int i = 0; i < G[nc].size(); i++){ + int j = G[nc][i]; + if(viz[j] == 0){ + Sf++; + coada[Sf] = j; + if(nc > j) + sol[j] = sol[nc] - H[nc][i]; + else + sol[j] = sol[nc] + H[nc][i]; + viz[j] = 1; + } + } + inc++; + } + + cout << sol[Y]; + return 0; +} diff --git a/villages/bfs/84f00134-7951-417d-b3f9-5acbc45754da.cpp b/villages/bfs/84f00134-7951-417d-b3f9-5acbc45754da.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3075b113414311cf2efb51ed905fa97bf05a09f8 --- /dev/null +++ b/villages/bfs/84f00134-7951-417d-b3f9-5acbc45754da.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/861d5fb4-d74b-406c-b0df-d39d716fd582.cpp b/villages/bfs/861d5fb4-d74b-406c-b0df-d39d716fd582.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed03220e5c19f024ad7c2e38eb24623308766d56 --- /dev/null +++ b/villages/bfs/861d5fb4-d74b-406c-b0df-d39d716fd582.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +ifstream cin("sate.in"); +ofstream cout("sate.out"); +vector>G; +vector>H; +int main() +{ + int N, M, X, Y; + cin >> N >> M >> X >> Y; + G = vector>(N + 1); + H = vector>(N + 1); + int x, y, cost; + for(int i = 1; i <= M; i++){ + cin >> x >> y >> cost; + G[x].push_back(y); H[x].push_back(cost); + G[y].push_back(x); H[y].push_back(cost); + } + const int k = 30010; + int coada[k] = {}, viz[k] = {}, inc = 1, Sf = 1, l = 1, sol[k] = {}; + coada[1] = X; + viz[X] = 1; + while(inc <= Sf){ + int nc = coada[inc]; + //for(int j: G[nc]) + for(int i = 0; i < G[nc].size(); i++){ + int j = G[nc][i]; + if(viz[j] == 0){ + Sf++; + coada[Sf] = j; + if(nc > j) + sol[j] = sol[nc] - H[nc][i]; + else + sol[j] = sol[nc] + H[nc][i]; + viz[j] = 1; + } + } + inc++; + } + + cout << sol[Y]; + return 0; +} diff --git a/villages/bfs/8667897f-2313-463f-ae2b-9581a56585ff.cpp b/villages/bfs/8667897f-2313-463f-ae2b-9581a56585ff.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a637ef5941be14abd3d48c212a06a147f39fd6f --- /dev/null +++ b/villages/bfs/8667897f-2313-463f-ae2b-9581a56585ff.cpp @@ -0,0 +1,52 @@ +#include +#include +#define DEF 100100 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +int n, m, start, dest, p, u, viz[DEF]; + +pair < int, int > c[DEF]; + +vector < pair < int, int > > D[DEF]; + +int main () { + fin >> n >> m >> start >> dest; + + if (start > dest) + swap (start, dest); + + for (int i = 1; i <= m; ++ i) { + int x, y, c; + fin >> x >> y >> c; + D[x].push_back (make_pair (y, c)); + D[y].push_back (make_pair (x, - c)); + } + + c[++ u] = make_pair (start, 0); + viz[start] = 1; + while (p <= u) { + int nod = c[p].first; + int cost = c[p].second; + + for (int i = 0; i < D[nod].size (); ++ i) { + if (viz[D[nod][i].first] == 0) { + viz[D[nod][i].first] = 1; + int new_cost = cost + D[nod][i].second; + + c[++ u] = make_pair (D[nod][i].first, new_cost); + + if (D[nod][i].first == dest) { + fout << new_cost; + return 0; + } + } + } + ++ p; + } + + return 0; +} diff --git a/villages/bfs/868b76d2-e8d6-4106-be07-5e73a4e53824.cpp b/villages/bfs/868b76d2-e8d6-4106-be07-5e73a4e53824.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f5ed56fdc07792720526807379beb0476af66f3d --- /dev/null +++ b/villages/bfs/868b76d2-e8d6-4106-be07-5e73a4e53824.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include + + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +const int NMAX = 30002; +const int MMAX = 100026; +int N,M,X,Y; +int a,b,c; +int dist[NMAX]; +int x[MMAX],y[MMAX],d[MMAX]; +void Read() +{ + f>>N>>M>>X>>Y; + for(int i=1; i<=M; ++i) + { + f>>a>>b>>c; + x[i]=a; + y[i]=b; + d[i]=c; + } +} +void Do() +{ + dist[X]=1; + while(!dist[Y]) + { + for(int i=1; i<=M; ++i) + { + if(dist[x[i]] && !dist[y[i]]) + dist[y[i]]=dist[x[i]]+d[i]; + if(!dist[x[i]] && dist[y[i]]) + dist[x[i]]=dist[y[i]]-d[i]; + } + } + g< +#include +#include +using namespace std; + +const int MaxNodesCnt = 3e4 + 5, buffSZ = 4e4 + 5; + +char buff[buffSZ]; +int buffPointer = buffSZ - 1; + +inline void read(int &no){ + + while(!isdigit(buff[buffPointer])) + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + no = 0; + while(isdigit(buff[buffPointer])){ + + no = no * 10 + buff[buffPointer] - '0'; + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + } +} + +struct Graph{ + + int newNode, cost; +}; + +vector adjList[MaxNodesCnt]; +int dist[MaxNodesCnt]; +int nodesCnt, edgesCnt, startNode, endNode; + +inline void readData(){ + + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + read(nodesCnt); read(edgesCnt); read(startNode); read(endNode); + + int from, to, cost; + while(edgesCnt--){ + + read(from); read(to); read(cost); + adjList[from].push_back({to, cost}); + adjList[to].push_back({from, -cost}); + } +} + +inline void BFS(int node){ + + deque Queue; + Queue.push_back(node); + + int newNode, cost; + + while(!Queue.empty()){ + + node = Queue.front(); + Queue.pop_front(); + + for(const Graph &nextNode : adjList[node]){ + + newNode = nextNode.newNode; + cost = nextNode.cost; + if(dist[newNode] == 0 and newNode != startNode){ + + dist[newNode] = dist[node] + cost; + Queue.push_back(newNode); + } + } + } +} + +int main(){ + + readData(); + BFS(startNode); + printf("%d", dist[endNode]); +} diff --git a/villages/bfs/8a68eb48-2992-4976-9e8a-aa87c4774adc.cpp b/villages/bfs/8a68eb48-2992-4976-9e8a-aa87c4774adc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45eb2db6b42b89de5e3792e4966c2938a6bf0b20 --- /dev/null +++ b/villages/bfs/8a68eb48-2992-4976-9e8a-aa87c4774adc.cpp @@ -0,0 +1,20 @@ +#include +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +struct {int a,b,d;} v[100025]; +int n,m,x,y,viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>v[i].a>>v[i].b>>v[i].d; + viz[x]=1; + while(!viz[y]) + { for(int i=1;i<=m;i++) + { if(viz[v[i].a] && !viz[v[i].b]) + viz[v[i].b]= viz[v[i].a]+v[i].d; + else + if(!viz[v[i].a] && viz[v[i].b]) + viz[v[i].a]= viz[v[i].b]-v[i].d; + } + } + g< + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +const int N = 30010, M = 100025; + +int n, m, a, b, v[N], x[M], y[M], z[M], i; + +int main() { + fin >> n >> m >> a >> b; + v[a] = 1; + for (i = 1; i <= m; ++i) { + fin >> x[i] >> y[i] >> z[i]; + } + while (!v[b]) { + for (i = 1; i <= m; ++i) { + if (v[ x[i] ] && !v[ y[i] ]) { + v[ y[i] ] = v[ x[i] ] + z[i]; + } + else if (!v[ x[i] ] && v[ y[i] ]) { + v[ x[i] ] = v[ y[i] ] - z[i]; + } + } + } + fout << v[b] - 1; +} diff --git a/villages/bfs/914a6f9b-d5a3-466e-b1bd-c2fa1c3f2d1a.cpp b/villages/bfs/914a6f9b-d5a3-466e-b1bd-c2fa1c3f2d1a.cpp new file mode 100644 index 0000000000000000000000000000000000000000..62549c60780780c448da2561cc312a4b21e25587 --- /dev/null +++ b/villages/bfs/914a6f9b-d5a3-466e-b1bd-c2fa1c3f2d1a.cpp @@ -0,0 +1,70 @@ +#include +#include +using namespace std; +class InputReader { +private: + FILE *inputFile; + static const int SIZE = 1 << 16; + char buffer[SIZE]; int cursor; + + inline void advance(void) { + if (++cursor == SIZE) { + cursor = 0; + fread(buffer, SIZE, sizeof(char), inputFile); + } + } + + inline char current(void) { + return buffer[cursor]; + } +public: + InputReader(const char* fileName) : cursor(0) { + inputFile = fopen(fileName, "r"); + fread(buffer, SIZE, 1, inputFile); + } + + ~InputReader(void) { + fclose(inputFile); + } + + template + InputReader &operator >>(type &value) { + value = 0; + while (current() < '0' or current() > '9') + advance(); + while (current() >= '0' and current() <= '9') { + value = value * 10 + current() - '0'; + advance(); + } + return *this; + } +} in("sate.in"); +ofstream out("sate.out"); +int a,b,c,n,m,x,y,v[30001],go,st,dr,i,hz[30001],distanta[30001]; +vector< pair >h[30001]; +int main(){ + in >> n >> m >> x >> y; + for( i = 1; i <= m; i ++ ){ + in >> a >> b >> c; + h[a].push_back( make_pair( b,c ) ); + h[b].push_back( make_pair( a,c ) ); + } + v[1] = x; + hz[x] = 1; + for( st = 1, dr = 1; st <= dr; st ++ ){ + for( i = 0; i < h[v[st]].size(); i ++ ){ + if( v[st] < h[v[st]][i].first ) + distanta[ h[v[st]][i].first ] = distanta[v[st]] + h[v[st]][i].second; + else + distanta[ h[v[st]][i].first ] = distanta[v[st]] - h[v[st]][i].second; + + if( hz[ h[v[st]][i].first ] == 0 ){ + hz[ h[v[st]][i].first ] = 1; + dr++; + v[dr] = h[v[st]][i].first; + } + } + } + out< +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,S,F,p,u,x,y,cost; +int c[900010],dist[30010],v[30010]; +vector < pair > L[30010]; +int main(){ + fin>>n>>m>>S>>F; + for(int i=1;i<=m;i++){ + fin>>x>>y>>cost; + L[x].push_back(pair(y,cost)); + L[y].push_back(pair(x,cost)); + } + c[1]=S; + p=u=1; + v[S]=1; + while(p<=u){ + if(c[p]==F) + break; + for(int i=0;i +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,S,F,p,u,x,y,cost; +int c[900010],dist[30010],v[30010]; +vector < pair > L[30010]; +int main(){ + fin>>n>>m>>S>>F; + for(int i=1;i<=m;i++){ + fin>>x>>y>>cost; + L[x].push_back(pair(y,cost)); + L[y].push_back(pair(x,cost)); + } + c[1]=S; + p=u=1; + v[S]=1; + while(p<=u){ + if(c[p]==F) + break; + for(int i=0;i +#include + +#define FIN "sate.in" +#define FOUT "sate.out" +#define MAXN 30001 +#define MAXM 100100 + +int n, m, x, y; +bool Used[MAXN]; + +struct elem +{ + int x, c; + elem *r; +} * L[MAXN]; + +void push(int a, int b, int c) +{ + elem *temp = new elem; + temp->x = b; + temp->c = c; + temp->r = L[a]; + L[a] = temp; +} + +void citire() +{ + int i, a, b, c; + freopen(FIN, "r", stdin); + freopen(FOUT, "w", stdout); + scanf("%d%d%d%d", &n, &m, &x, &y); + for (i = 0; i < m; ++i) + { + scanf("%d %d %d", &a, &b, &c); + push(a, b, c); + push(b, a, -c); + } +} + +struct coada +{ + int x; + long long d; +} C[MAXN]; + +void bf(int s) +{ + int p, u = 0; + C[u++] = (coada){s, 0}; + Used[s] = 1; + + for (p = 0; p < u; ++p) + { + if (C[p].x == y) + break; + for (elem *k = L[C[p].x]; k; k = k->r) + if (!Used[k->x]) + C[u++] = (coada){k->x, C[p].d + k->c}, Used[k->x] = 1; + } + printf("%lld\n", C[p].d); +} + +int main() +{ + citire(); + bf(x); + return 0; +} \ No newline at end of file diff --git a/villages/bfs/9b1e6d10-f9e2-485a-bef6-e798fb84bcf3.cpp b/villages/bfs/9b1e6d10-f9e2-485a-bef6-e798fb84bcf3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f446dd0c7127935681433fd00d79d9a732a673c --- /dev/null +++ b/villages/bfs/9b1e6d10-f9e2-485a-bef6-e798fb84bcf3.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#define nmax 30002 +#define ll long long +#define infinit 30000010 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int dist[nmax]; +int start,x,y,n,m,sfarsit,c; +vector > graf[nmax]; +queue > coada; + +void determinare() +{ + for(int i=0;inod) + { + dist[graf[nod][i].first]=cost+graf[nod][i].second; + } + else + { + dist[graf[nod][i].first]=cost-graf[nod][i].second; + } + coada.push(make_pair(graf[nod][i].first,dist[graf[nod][i].first])); + } + } + } +} + +int main() +{ + fin>>n>>m>>start>>sfarsit; + for(int i=1;i<=m;i++) + { + fin>>x>>y>>c; + pair m; + m.first=y; + m.second=c; + graf[x].push_back(m); + m.first=x; + graf[y].push_back(m); + } + for(int i=1;i<=n;i++) + { + dist[i]=infinit; + } + dist[start]=0; + determinare(); + fout< +#include +#include +#include +#include + +#define INF 0x3f3f3f3f +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +queue q; +vector > ways[30005]; +vector graph[30005]; +int dist[30005]; +int a,b,c; +int n,m,X,Y; +int GetDistance(int a,int b) +{ + for(auto y:ways[a]) + { + if(b== y.first) + return y.second; + } +} + +void lee(int d) +{ + q.push(d); + dist[d] = 0; + while(!q.empty()) + { + int x = q.front(); + q.pop(); + for(auto y:ways[x]) + { + if(dist[y.first] == INF) + { + int k = dist[x] - GetDistance(x,y.first); + int mk = dist[x] + GetDistance(x,y.first); + if(x>y.first) dist[y.first] = k; + else if(x>n>>m>>X>>Y; + for(int i=1;i<=m;++i) + { + fi>>a>>b>>c; + ways[a].push_back({b,c}); + ways[b].push_back({a,c}); + } + for(int i=1;i<=n;++i) + dist[i] = INF; + lee(X); + fo< +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100030],b[100030],d[100030],i; +int main() +{ + for(f>>N>>M>>x>>y,s[x]=i=1;i<=M;++i) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + for(i=1;i<=M;++i) + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + g< + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n, m; +int x, y; +int ans[30303]; + +vector> v[30303]; +queue coada; + +int main() +{ + fin >> n >> m; + fin >> x >> y; + while(m--) + { + int a, b, c; + fin >> a >> b >> c; + v[a].push_back({b, c}); + v[b].push_back({a, -c}); + /// orasele sunt coliniare si de exemplu daca de la orasul 1 la orasul 7 face 8 km + /// si de la orasul 7 la orasul 5 face 3 km + /// eu ca sa ajung la orasul prefer sa merg direct de la 7 la 5 + /// adica sa tin minte costul de la orasul 7 la orasul 5 ca -3 + } + for(int i = 1; i <= n; i ++) + { + ans[i] = 1e9; + } + coada.push(x); + ans[x] = 0; + while(!coada.empty()) + { + int nod = coada.front(); + coada.pop(); + for(auto it : v[nod]) + { + if(ans[nod] + it.second < ans[it.first]) + { + ans[it.first] = ans[nod] + it.second; + coada.push(it.first); + } + } + } + fout << ans[y] << '\n'; +} diff --git a/villages/bfs/aab27c9b-40ac-11ec-801a-dc7196033e92.cpp b/villages/bfs/aab27c9b-40ac-11ec-801a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5f58392c299a3f063b0133bff658976870837347 --- /dev/null +++ b/villages/bfs/aab27c9b-40ac-11ec-801a-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#define nmax 100030 + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y; + +int a[nmax], b[nmax], dist[nmax], pasi[30005]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + in>>a[i]>>b[i]>>dist[i]; + } + pasi[x]=1; + while(!pasi[y]) + { + for(int i=1; i<=m; i++) + { + if(pasi[a[i]] && !pasi[b[i]]) + { + pasi[b[i]] = pasi[a[i]] + dist[i]; + } + else + if(!pasi[a[i]] && pasi[b[i]]) + { + pasi[a[i]] = pasi[b[i]] - dist[i]; + } + } + } + out< +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y,cnt,k,i,j,p,u,c[30001],f[30001],drum[30001]; + +vector < pair > l[30001]; + +int main(){ + fin>>n>>m>>x>>y; + for(cnt=1;cnt<=m;cnt++){ + fin>>i>>j>>k; + l[i].push_back(make_pair(j,k)); + l[j].push_back(make_pair(i,0-k)); + } + + c[1]=x; + f[x]=1; + p=1; u=1; + + while(f[y]==0 && p<=u){ + for(k=0;k +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +typedef pair p; +const int NMAX = 30005; +vector

G[NMAX]; +int N, M, X, Y; +bool use[NMAX]; +queue

q; + +void Read(){ + in >> N >> M >> X >> Y; + for(int i = 1; i <= M; ++i){ + int a, b, d; + in >> a >> b >> d; + G[a].push_back(make_pair(b, d)); + G[b].push_back(make_pair(a, d)); + } + q.push(make_pair(X, 0)); + use[X] = true; +} + +void SolveAndPrint(){ + while(!q.empty()){ + int node = q.front().first; + int cost = q.front().second; + if(node == Y){ + out << cost << "\n"; + break; + } + q.pop(); + for(int i = 0; i < G[node].size(); ++i){ + p neighbour = G[node][i]; + if(!use[neighbour.first]){ + use[neighbour.first] = true; + q.push(make_pair(neighbour.first, + cost + (neighbour.second * (node < neighbour.first ? + 1 : -1)))); + } + } + } +} + +int main(){ + Read(); + SolveAndPrint(); +} diff --git a/villages/bfs/b24c38c7-40ac-11ec-8da5-dc7196033e92.cpp b/villages/bfs/b24c38c7-40ac-11ec-8da5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0ee99886048a912b77400aafaa3698a1c58e58f --- /dev/null +++ b/villages/bfs/b24c38c7-40ac-11ec-8da5-dc7196033e92.cpp @@ -0,0 +1,16 @@ +#include +#define Nmax 100025 +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +int n,m,x,y,a[Nmax],b[Nmax],d[Nmax],viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>a[i]>>b[i]>>d[i]; + viz[x]=1; + while(!viz[y]) + for(int i=1;i<=m;i++) + if(viz[a[i]] && !viz[b[i]]) viz[b[i]]= viz[a[i]]+d[i]; + else + if(!viz[a[i]] && viz[b[i]]) viz[a[i]]= viz[b[i]]-d[i]; + g< +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +struct {int a,b,d;} v[100025]; +int n,m,x,y,viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>v[i].a>>v[i].b>>v[i].d; + viz[x]=1; + while(!viz[y]) + { for(int i=1;i<=m;i++) + { if(viz[v[i].a] && !viz[v[i].b]) + viz[v[i].b]= viz[v[i].a]+v[i].d; + else + if(!viz[v[i].a] && viz[v[i].b]) + viz[v[i].a]= viz[v[i].b]-v[i].d; + } + } + g< + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); +const int MAX=100006; +int n,m,x,y,st[MAX],dr[MAX],c[MAX]; +int d[MAX]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + in>>st[i]>>dr[i]>>c[i]; + } + d[x]=1; + + + while(!d[y]) + { + for(int i=1;i<=m;i++) + { + if(!d[st[i]] && d[dr[i]]) d[st[i]]=d[dr[i]]-c[i]; + else if(d[st[i]] && !d[dr[i]]) d[dr[i]]=d[st[i]]+c[i]; + } + } + out< +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,S,F,p,u,x,y,cost; +int c[900010],dist[30010],v[30010]; +vector < pair > L[30010]; +int main(){ + fin>>n>>m>>S>>F; + for(int i=1;i<=m;i++){ + fin>>x>>y>>cost; + L[x].push_back(pair(y,cost)); + L[y].push_back(pair(x,cost)); + } + c[1]=S; + p=u=1; + v[S]=1; + while(p<=u){ + if(c[p]==F) + break; + for(int i=0;i +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +struct{ + int a,b,d; +}v[100025]; +int n,m,x,y,viz[30003]; +int main() +{ + f>>n>>m>>x>>y; + viz[x]=1; + for(int i=1;i<=m;i++) + f>>v[i].a>>v[i].b>>v[i].d; + while(!viz[y]) + { + for(int i=1;i<=m;i++) + { + if(viz[v[i].a] && !viz[v[i].b] ) + viz[v[i].b] = viz[v[i].a] + v[i].d; + else + if(!viz[v[i].a] && viz[v[i].b]) + viz[v[i].a] = viz[v[i].b] - v[i].d; + } + } + g< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int sol[30010],x[100050],y[100050],km[100050]; +int a,b,n,m; +int main(){ + fin >> n >> m >> a >> b; + sol[a]=1; + for (int i=1;i<=m;i++) + fin >> x[i] >> y[i] >> km[i]; + while (!sol[b]){ + for (int i=1;i<=m;i++){ + if (sol[x[i]] && !sol[y[i]]) sol[y[i]]=sol[x[i]]+km[i]; + else if (!sol[x[i]] && sol[y[i]]) sol[x[i]]=sol[y[i]]-km[i]; + } + } + fout << sol[b]-1; +} diff --git a/villages/bfs/c004461b-40ac-11ec-a1a0-dc7196033e92.cpp b/villages/bfs/c004461b-40ac-11ec-a1a0-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5600f4edd609a08bd4c2a9aad6739b577e5fff2a --- /dev/null +++ b/villages/bfs/c004461b-40ac-11ec-a1a0-dc7196033e92.cpp @@ -0,0 +1,69 @@ +#include +#include + +#define FIN "sate.in" +#define FOUT "sate.out" +#define MAXN 30001 +#define MAXM 100100 + +int n, m, x, y; +bool Used[MAXN]; + +struct elem +{ + int x, c; + elem *r; +} * L[MAXN]; + +void push(int a, int b, int c) +{ + elem *temp = new elem; + temp->x = b; + temp->c = c; + temp->r = L[a]; + L[a] = temp; +} + +void citire() +{ + int i, a, b, c; + freopen(FIN, "r", stdin); + freopen(FOUT, "w", stdout); + scanf("%d%d%d%d", &n, &m, &x, &y); + for (i = 0; i < m; ++i) + { + scanf("%d %d %d", &a, &b, &c); + push(a, b, c); + push(b, a, -c); + } +} + +struct coada +{ + int x; + long long d; +} C[MAXN]; + +void bf(int s) +{ + int p, u = 0; + C[u++] = (coada){s, 0}; + Used[s] = 1; + + for (p = 0; p < u; ++p) + { + if (C[p].x == y) + break; + for (elem *k = L[C[p].x]; k; k = k->r) + if (!Used[k->x]) + C[u++] = (coada){k->x, C[p].d + k->c}, Used[k->x] = 1; + } + printf("%lld\n", C[p].d); +} + +int main() +{ + citire(); + bf(x); + return 0; +} \ No newline at end of file diff --git a/villages/bfs/c0614e04-e562-4ae3-8916-ac9061c2f179.cpp b/villages/bfs/c0614e04-e562-4ae3-8916-ac9061c2f179.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d0ee99886048a912b77400aafaa3698a1c58e58f --- /dev/null +++ b/villages/bfs/c0614e04-e562-4ae3-8916-ac9061c2f179.cpp @@ -0,0 +1,16 @@ +#include +#define Nmax 100025 +using namespace std; +ifstream f("sate.in"); ofstream g("sate.out"); +int n,m,x,y,a[Nmax],b[Nmax],d[Nmax],viz[30003]; +int main() +{ f>>n>>m>>x>>y; + for(int i=1;i<=m;i++) f>>a[i]>>b[i]>>d[i]; + viz[x]=1; + while(!viz[y]) + for(int i=1;i<=m;i++) + if(viz[a[i]] && !viz[b[i]]) viz[b[i]]= viz[a[i]]+d[i]; + else + if(!viz[a[i]] && viz[b[i]]) viz[a[i]]= viz[b[i]]-d[i]; + g< + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > graf[100100]; +queue Q; +int n,m,x1,ye,x,y,d,viz[30100],dist[30100]; + +void bfs(int q){ + viz[q] = 1; + Q.push(q); + while(!Q.empty()){ + int t = Q.front(); + Q.pop(); + viz[t] = 1; + for (int i=0; i nodc) //daca satul e mai la stanga de precedent, scadem dist + dist[nodc] = dist[t] - dis; + else + dist[nodc] = dist[t] + dis; + Q.push(nodc); + } + if (nodc == ye){ + fout << dist[nodc]; + return; + } + } + } +} + +int main() +{ + fin >> n >> m >> x1 >> ye; + for (int i=0; i> x >> y >> d; + graf[y].push_back(make_pair(x,d)); + graf[x].push_back(make_pair(y,d)); + } + + bfs(x1); + + + return 0; +} diff --git a/villages/bfs/c112c5ff-f4c3-4c69-af48-e8e13d579461.cpp b/villages/bfs/c112c5ff-f4c3-4c69-af48-e8e13d579461.cpp new file mode 100644 index 0000000000000000000000000000000000000000..999d4ddd77fe3e1616cbc6064f4ae71fe6bd5044 --- /dev/null +++ b/villages/bfs/c112c5ff-f4c3-4c69-af48-e8e13d579461.cpp @@ -0,0 +1,30 @@ +#include +#include +#define DIM 30010 +#define X first +#define Y second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int c[DIM],f[DIM],p,u,i,j,nod,n,m,x,y,tot,d,w[DIM]; +vector< pair >L[DIM]; +int main(){ + fin>>n>>m>>x>>y; + while(fin>>i>>j>>d){ + L[i].push_back(make_pair(j,d)); + L[j].push_back(make_pair(i,-d)); + } + p=1; u=1; c[1]=x; f[x]=1; + while(p<=u){ + nod=c[p]; + for(i=0;i +#define Nmax 30001 +#define ll long long +#define DIM 70000 +using namespace std; +//ifstream f("sate.in"); +ofstream g("sate.out"); +char buffer[DIM]; +int cursor=DIM-1; +void read(int &x) +{ + x=0; + while(!isdigit(buffer[cursor])) + if(++cursor==DIM) + { + cursor=0; + fread(buffer,1,DIM,stdin); + } + while(isdigit(buffer[cursor])) + { + x=x*10+buffer[cursor]-'0'; + if(++cursor==DIM) + { + cursor=0; + fread(buffer,1,DIM,stdin); + } + } +} +queue q; +ll d[Nmax]; +int n,m,X,Y; +bitset inq; +list > G[Nmax]; +void BF() +{ + int x; + list > :: iterator it; + fill(d+1,d+n+1,LONG_LONG_MAX); + d[X]=0; + q.push(X); + while(!q.empty()) + { + x=q.front(); + q.pop(); + inq[x]=false; + for(it=G[x].begin();it!=G[x].end();++it) + if(d[it->first]>d[x]+it->second) + { + d[it->first]=d[x]+it->second; + if(!inq[it->first]) + { + inq[it->first]=true; + q.push({it->first}); + } + } + } +} +int main() +{ + freopen("sate.in","r",stdin); + read(n); + read(m); + read(X); + read(Y); + int i,x,y,k; + for(i=1;i<=m;i++) + { + read(x); + read(y); + read(k); + G[x].push_back({y,k}); + G[y].push_back({x,-k}); + } + BF(); + g< +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector< pair > graf[30005]; +int dist[30005]; +queue noduri; +int n,m,x,y; + +void bfs() +{ + while(!noduri.empty()) + { + int a=noduri.front(); + noduri.pop(); + for(auto dest:graf[a]) + { + if(dist[dest.first]==0) + { + if(dest.firsta) dist[dest.first] = dist[a]+dest.second; + if(dest.first==y) return; + noduri.push(dest.first); + } + } + } +} + +int main() +{ + int sat1, sat2, d; + f >> n >> m >> x >> y; + + for(int i=1; i<=m; i++) + { + f >> sat1 >> sat2 >> d; + graf[sat1].push_back({sat2,d}); + graf[sat2].push_back({sat1,d}); + } + + for(int i=1; i<=n; i++) + { + dist[i] = 0; + } + dist[x] = 0; + noduri.push(x); + bfs(); + g << dist[y]; + + return 0; +} diff --git a/villages/bfs/c4d518c5-6c2b-4758-9508-8c1221660057.cpp b/villages/bfs/c4d518c5-6c2b-4758-9508-8c1221660057.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed26b18d5e096aaa2d165bb8e4a876eb825cb969 --- /dev/null +++ b/villages/bfs/c4d518c5-6c2b-4758-9508-8c1221660057.cpp @@ -0,0 +1,57 @@ +#include +#include +using namespace std; +const int maxn=30001; +const int maxm=100025; +queue q; +bool viz[maxn]; +int d[maxn],cap[maxn],nxt[2*maxm],nr; +struct muchie +{ + int nod,cost; +}val[2*maxm]; +inline void add(int a,int b,int c) +{ + val[++nr].nod=b;val[nr].cost=c; + nxt[nr]=cap[a]; + cap[a]=nr; +} +inline void bfs(int x,int y) +{ + int a,b,c,p; + q.push(x);viz[x]=true; + while(!viz[y]) + { + a=q.front();q.pop(); + p=cap[a]; + while(p) + { + b=val[p].nod;c=val[p].cost; + if(!viz[b]) + { + viz[b]=true; + d[b]=d[a]+c; + q.push(b); + } + p=nxt[p]; + } + } +} +int main() +{ + FILE *fin,*fout; + fin=fopen("sate.in","r"); + fout=fopen("sate.out","w"); + int n,m,x,y,a,b,c; + fscanf(fin,"%d%d%d%d",&n,&m,&x,&y); + for(int i=0;i +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int n, m, A, B, dist[30002], c[30002]; +bool viz[30002]; +vector > l[100030]; +void bfs(int nod) +{ + viz[nod]=1; + int p=1; + int u=1; + c[1]=nod; + while(p<=u) + { + int nodc=c[p]; + for(int i=0;i>n>>m>>A>>B; + for(int i=1;i<=m;i++) + { + int x, y, c; + f>>x>>y>>c; + l[x].push_back(make_pair(y, c)); + l[y].push_back(make_pair(x, c)); + } + bfs(A); + g< + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); +const int MAX=100006; +int n,m,x,y,st[MAX],dr[MAX],c[MAX]; +int d[MAX]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + in>>st[i]>>dr[i]>>c[i]; + } + d[x]=1; + + + while(!d[y]) + { + for(int i=1;i<=m;i++) + { + if(!d[st[i]] && d[dr[i]]) d[st[i]]=d[dr[i]]-c[i]; + else if(d[st[i]] && !d[dr[i]]) d[dr[i]]=d[st[i]]+c[i]; + } + } + out< +using namespace std; +ifstream f ("sate.in"); +ofstream g ("sate.out"); +int a,b,t[30001],x[100025],y[100025],c[100025],n,m; +int main() +{ + f>>n>>m>>a>>b; + t[a]=1; + for(int i=1;i<=m;++i) f>>x[i]>>y[i]>>c[i]; + while(!t[b]) + { + for (int i=1;i<=m;++i) + { + if(t[x[i]]&&!t[y[i]]) t[y[i]]=t[x[i]]+c[i]; + else if(!t[x[i]]&&t[y[i]]) t[x[i]]=t[y[i]]-c[i]; + } + } + g< +#include +#include + +using namespace std; + +#define DIM 10000 +char buff[DIM]; +int poz=0; + +void citeste(int &numar) +{ + numar = 0; + while (buff[poz] < '0' || buff[poz] > '9') + if (++poz == DIM) + fread(buff,1,DIM,stdin),poz=0; + while ('0'<=buff[poz] && buff[poz]<='9') + { + numar = numar*10 + buff[poz] - '0'; + if (++poz == DIM) + fread(buff,1,DIM,stdin),poz=0; + } +} + +int n,m,x,y,s,d,c; +int cs[30001]; +bool viz[30001]; +queue q; + +struct sat +{ + vector vecini; + vector cost; +} a[30001]; + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + + citeste(n); + citeste(m); + citeste(x); + citeste(y); + if(x>y) + { + int t=y; + y=x; + x=t; + } + + for(int i=1;i<=m;i++) + { + citeste(s); + citeste(d); + citeste(c); + if(s>d) + { + int t=d; + d=s; + s=t; + } + a[s].vecini.push_back(d); + a[s].cost.push_back(c); + a[d].vecini.push_back(s); + a[d].cost.push_back(-c); + } + + q.push(x); + int current; + while(!q.empty()) + { + current=q.front(); + q.pop(); + if(current==y) + { + break; + } + int vc; + int dst; + while(!a[current].vecini.empty()) + { + vc=a[current].vecini.back(); + dst=a[current].cost.back(); + if(viz[vc]==0) + { + viz[vc]=1; + cs[vc]=cs[current]+dst; + q.push(vc); + } + a[current].vecini.pop_back(); + a[current].cost.pop_back(); + } + } + printf("%d",cs[y]); +} diff --git a/villages/bfs/d3e76f06-40ac-11ec-bb20-dc7196033e92.cpp b/villages/bfs/d3e76f06-40ac-11ec-bb20-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed03220e5c19f024ad7c2e38eb24623308766d56 --- /dev/null +++ b/villages/bfs/d3e76f06-40ac-11ec-bb20-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +ifstream cin("sate.in"); +ofstream cout("sate.out"); +vector>G; +vector>H; +int main() +{ + int N, M, X, Y; + cin >> N >> M >> X >> Y; + G = vector>(N + 1); + H = vector>(N + 1); + int x, y, cost; + for(int i = 1; i <= M; i++){ + cin >> x >> y >> cost; + G[x].push_back(y); H[x].push_back(cost); + G[y].push_back(x); H[y].push_back(cost); + } + const int k = 30010; + int coada[k] = {}, viz[k] = {}, inc = 1, Sf = 1, l = 1, sol[k] = {}; + coada[1] = X; + viz[X] = 1; + while(inc <= Sf){ + int nc = coada[inc]; + //for(int j: G[nc]) + for(int i = 0; i < G[nc].size(); i++){ + int j = G[nc][i]; + if(viz[j] == 0){ + Sf++; + coada[Sf] = j; + if(nc > j) + sol[j] = sol[nc] - H[nc][i]; + else + sol[j] = sol[nc] + H[nc][i]; + viz[j] = 1; + } + } + inc++; + } + + cout << sol[Y]; + return 0; +} diff --git a/villages/bfs/d5d54f25-40ac-11ec-9240-dc7196033e92.cpp b/villages/bfs/d5d54f25-40ac-11ec-9240-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..634d4c0751e6350b03380b6d686669d0146a33a6 --- /dev/null +++ b/villages/bfs/d5d54f25-40ac-11ec-9240-dc7196033e92.cpp @@ -0,0 +1,96 @@ +//#include +#include +#include +#include +using namespace std; +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +} in ("sate.in"); +ofstream out("sate.out"); +vector > v[30005]; +int q[30005]; +int d[30005],sz=1,cur=1; +int main() +{ + int n,m,x,y,a,b,di; + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++){ + in>>a>>b>>di; + v[a].push_back(make_pair(b,di)); + v[b].push_back(make_pair(a,-di)); + } + for(int i=1;i<=n;i++){ + d[i]=30000000; + } + q[1]=x; + d[x]=0; + while(cur<=sz){ + a=q[cur]; + cur++; + for(auto u:v[a]){ + if(d[u.first]==30000000){ + d[u.first]=d[a]+u.second; + if(u.first==y){ + out< +# include +# include +# include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAX = 30005; +const int INF = (1<<30); +int n, m, x, y; +int dist[MAX]; +struct inf { int v, c; }; +vector L[MAX]; +queue q; +bitset viz; + +inf per(int a, int b) { + inf X; + X.v = a; + X.c = b; + return X; +} + +int main() { + int px, py, pc, nod; + fin >> n >> m >> x >> y; + for (int i=1; i<=m; i++) { + fin >> px >> py >> pc; + L[px].push_back(per(py, pc)); + L[py].push_back(per(px, -pc)); + } + + viz[x] = 1; + vector::iterator it; + for (it=L[x].begin(); it!=L[x].end(); it++) { + dist[it->v] = it->c; + q.push(it->v); + viz[it->v] = 1; + } + + + while (!q.empty()) { + nod = q.front(); + for (it=L[nod].begin(); it!=L[nod].end(); it++) { + if (viz[it->v] == 0) { + dist[it->v] = dist[nod] + it->c; + viz[it->v] = 1; + q.push(it->v); + } + } + q.pop(); + } + + fout << dist[y]; + + return 0; +} diff --git a/villages/bfs/d6625e20-8d45-42cd-ba7c-fe4e3889123c.cpp b/villages/bfs/d6625e20-8d45-42cd-ba7c-fe4e3889123c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..10b285f6c4f4980673b9db1e7d10e6346bb0e809 --- /dev/null +++ b/villages/bfs/d6625e20-8d45-42cd-ba7c-fe4e3889123c.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +using namespace std; + +int nSate, nMuchii, startNod, stopNod, tempNod1, tempNod2, tempD; +vector < pair > graf[30001]; +int distNod[30001]; +bool parcurs[30001]; +queue < int > lee; +ifstream f("sate.in"); +ofstream g("sate.out"); + +void read(){ + f>>nSate>>nMuchii>>startNod>>stopNod; + + for(int i = 0; i>tempNod1>>tempNod2>>tempD; + graf[tempNod1].push_back({tempNod2, tempD}); + graf[tempNod2].push_back({tempNod1, tempD}); + } +} + + + + +void SRCH(int x) +{ + int goThrough = 0, current, nodCurrent, distCurrent; + lee.push(x); + + while( !lee.empty() ){ + current = lee.front(); + lee.pop(); + while(goThrough < graf[current].size()){ + + nodCurrent = graf[current][goThrough].first; + distCurrent = graf[current][goThrough].second; + + if(!parcurs[nodCurrent]){ + parcurs[nodCurrent] = true; + + lee.push(nodCurrent); + + if(current > nodCurrent) + distNod[nodCurrent] = distNod[current] - distCurrent; + else + distNod[nodCurrent] = distNod[current] + distCurrent; + + } + ++goThrough; + } + goThrough = 0; + } +} + +int main() +{ + + + read(); + SRCH(startNod); + + cout< + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + + s[x]=1; + + for( int i=1; i<=M; i++ ) + f>>a[i]>>b[i]>>d[i]; + + while(!s[y]) + { + for( int i=1; i<=M; i++ ) + { + if( s[a[i]]&&!s[b[i]] ) + s[b[i]]=s[a[i]]+d[i]; + else + if( !s[a[i]]&&s[b[i]] ) + s[a[i]]=s[b[i]]-d[i]; + } + } + + g< + +#include + + + +using namespace std; + + + +ifstream fin("sate.in"); + +ofstream fout("sate.out"); + + + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; + +vector< pair > L[30005]; + +char S[1010]; + + + +int main() + +{ + + fin >> n >> m >> x >> y; + + for (i=1; i<=m; i++) + + { + + //fin >> x1 >> y1 >> cost; + + fin.get(); fin.get(S, 1005); + + x1 = y1 = cost = 0; j = 0; + + while (S[j] >= '0' && S[j] <= '9') + + { + + x1 = x1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + y1 = y1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + cost = cost*10+(S[j]-'0'); + + j++; + + } + + L[x1].push_back(make_pair(y1, cost)); + + L[y1].push_back(make_pair(x1, -cost)); + + } + + int p = 1; int u = 1; c[1] = x; v[x] = 1; + + while (p <= u) + + { + + int nod = c[p]; + + if (v[y]) + + break; + + for (i=0; i +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100030],b[100030],d[100030],i; +int main() +{ + for(f>>N>>M>>x>>y,s[x]=i=1;i<=M;++i) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + for(i=1;i<=M;++i) + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + g< +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; +vector< pair > L[30005]; +char S[1010]; + +int main() +{ + fin >> n >> m >> x >> y; + for (i=1; i<=m; i++) + { + //fin >> x1 >> y1 >> cost; + fin.get(); fin.get(S, 1005); + x1 = y1 = cost = 0; j = 0; + while (S[j] >= '0' && S[j] <= '9') + { + x1 = x1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + y1 = y1*10+(S[j]-'0'); + j++; + } + j++; + while (S[j] >= '0' && S[j] <= '9') + { + cost = cost*10+(S[j]-'0'); + j++; + } + L[x1].push_back(make_pair(y1, cost)); + L[y1].push_back(make_pair(x1, -cost)); + } + int p = 1; int u = 1; c[1] = x; v[x] = 1; + while (p <= u) + { + int nod = c[p]; + if (v[y]) + break; + for (i=0; i +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int a, b, s[30001], x[100001], y[100001], c[100001], n, m; + +int main(){ + int i; + fin>>n>>m>>a>>b; + s[a]=1; + for (i=1;i<=m;i++) + fin>>x[i]>>y[i]>>c[i]; + while(!s[b]){ + for (int i=1; i<=m; i++){ + if (s[x[i]] && !s[y[i]]) + s[y[i]]=s[x[i]]+c[i]; + else if (!s[x[i]] && s[y[i]]) + s[x[i]]=s[y[i]]-c[i];} + } + fout< +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/e704279d-0e29-4366-9d1d-94885e376939.cpp b/villages/bfs/e704279d-0e29-4366-9d1d-94885e376939.cpp new file mode 100644 index 0000000000000000000000000000000000000000..142493c543a15d87788cf97802b90084ec2f3204 --- /dev/null +++ b/villages/bfs/e704279d-0e29-4366-9d1d-94885e376939.cpp @@ -0,0 +1,119 @@ +#include + +#include + + + +using namespace std; + + + +ifstream fin("sate.in"); + +ofstream fout("sate.out"); + + + +int n,m,i,j,x,y,x1,y1,cost,sol,s,ok,v[30005],d[30005],c[30005]; + +vector< pair > L[30005]; + +char S[1010]; + + + +int main() + +{ + + fin >> n >> m >> x >> y; + + for (i=1; i<=m; i++) + + { + + //fin >> x1 >> y1 >> cost; + + fin.get(); fin.get(S, 1005); + + x1 = y1 = cost = 0; j = 0; + + while (S[j] >= '0' && S[j] <= '9') + + { + + x1 = x1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + y1 = y1*10+(S[j]-'0'); + + j++; + + } + + j++; + + while (S[j] >= '0' && S[j] <= '9') + + { + + cost = cost*10+(S[j]-'0'); + + j++; + + } + + L[x1].push_back(make_pair(y1, cost)); + + L[y1].push_back(make_pair(x1, -cost)); + + } + + int p = 1; int u = 1; c[1] = x; v[x] = 1; + + while (p <= u) + + { + + int nod = c[p]; + + if (v[y]) + + break; + + for (i=0; i +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,X,Y,s[30002],a[100025],b[100025],d[100025]; + +int main() +{ + f>>N>>M>>X>>Y; + s[X]=1; + for(int i=1;i<=M;++i) + { + f>>a[i]>>b[i]>>d[i]; + } + + f.close(); + while(!s[Y]) + { + for(int i=1;i<=M;++i) + { + if(s[a[i]] && !s[b[i]]) + { + s[b[i]]=s[a[i]]+d[i]; + } + else + if(!s[a[i]] && s[b[i]]) + { + s[a[i]]=s[b[i]]-d[i]; + } + } + } + + g< +#include +#include +#define NMAX 30001 +using namespace std; +vector < pair > v[NMAX]; +long long viz[NMAX]; +int stiv [NMAX]; +int main (){ + FILE *in,*out; + in = fopen ("sate.in","r"); + out = fopen ("sate.out","w"); + int n,m,i,nods,nodf,inc,sf,pp,a,b,l,nod,DIM; + fscanf(in,"%d%d%d%d",&n,&m,&nods,&nodf); + for (i=1;i<=m;i++){ + fscanf(in,"%d%d%d",&a,&b,&l); + v[a].push_back(make_pair(b,l)); + v[b].push_back(make_pair(a,l));/// reciproca + } + + for (i=1;i<=n;i++) + viz[i] = -1LL; + inc = sf = 1; + stiv[inc] = nods; + viz[nods] = 0LL; + pp = 0; + while (inc <= sf && pp == 0){ + nod = stiv[inc]; + DIM = v[nod].size(); + for (i = 0; i < DIM;i ++){ + if (viz[v[nod][i].first] == -1){ + if (v[nod][i].first > nod) + viz[v[nod][i].first] = viz[nod] + v[nod][i].second; + else + viz[v[nod][i].first] = viz[nod] - v[nod][i].second; + stiv[++sf] = v[nod][i].first; + ///if (v[nod][i].first == nodf) + /// pp = 1; + } + } + inc ++; + } + + fprintf(out,"%d",viz[nodf]); + + + + fclose (in); + fclose (out); + return 0; +} + + diff --git a/villages/bfs/eef09c68-40ac-11ec-affd-dc7196033e92.cpp b/villages/bfs/eef09c68-40ac-11ec-affd-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a84083f5425290ede8cd6a621b6894dcf5090238 --- /dev/null +++ b/villages/bfs/eef09c68-40ac-11ec-affd-dc7196033e92.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,i; +int a[100100] , b[100100] , d[100100]; +int drum[100100]; +int main() +{ + fin>>x>>m>>x>>y; + for(i=1;i<=m;i++) + fin>>a[i]>>b[i]>>d[i]; + drum[x]=1; + while(!drum[y]) + for(i=1;i<=m;i++) + if(!drum[a[i]] && drum[b[i]]) + drum[a[i]]=drum[b[i]]-d[i]; + else if(drum[a[i]] && !drum[b[i]]) + drum[b[i]]=drum[a[i]]+d[i]; + fout< +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair >g[30001]; +int dist[30001],q[30001]; +bool viz[30001]; +int n,m,x,y,a,b,d,st,dr; +int main() +{ + fin>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + fin>>a>>b>>d; + g[a].push_back(make_pair(b,d)), + g[b].push_back(make_pair(a,-d)); + } + st=dr=1; + q[1]=x; + viz[x]=1; + while(st<=dr) + { + for(int i=0;i +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +struct{ + int a,b,d; +}v[100025]; +int n,m,x,y,viz[30003]; +int main() +{ + f>>n>>m>>x>>y; + viz[x]=1; + for(int i=1;i<=m;i++) + f>>v[i].a>>v[i].b>>v[i].d; + while(!viz[y]) + { + for(int i=1;i<=m;i++) + { + if(viz[v[i].a] && !viz[v[i].b] ) + viz[v[i].b] = viz[v[i].a] + v[i].d; + else + if(!viz[v[i].a] && viz[v[i].b]) + viz[v[i].a] = viz[v[i].b] - v[i].d; + } + } + g< + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); +const int MAX=100006; +int n,m,x,y,st[MAX],dr[MAX],c[MAX]; +int d[MAX]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1;i<=m;i++) + { + in>>st[i]>>dr[i]>>c[i]; + } + d[x]=1; + + + while(!d[y]) + { + for(int i=1;i<=m;i++) + { + if(!d[st[i]] && d[dr[i]]) d[st[i]]=d[dr[i]]-c[i]; + else if(d[st[i]] && !d[dr[i]]) d[dr[i]]=d[st[i]]+c[i]; + } + } + out< +#include +using namespace std; +class InputReader { +private: + FILE *inputFile; + static const int SIZE = 1 << 16; + char buffer[SIZE]; int cursor; + + inline void advance(void) { + if (++cursor == SIZE) { + cursor = 0; + fread(buffer, SIZE, sizeof(char), inputFile); + } + } + + inline char current(void) { + return buffer[cursor]; + } +public: + InputReader(const char* fileName) : cursor(0) { + inputFile = fopen(fileName, "r"); + fread(buffer, SIZE, 1, inputFile); + } + + ~InputReader(void) { + fclose(inputFile); + } + + template + InputReader &operator >>(type &value) { + value = 0; + while (current() < '0' or current() > '9') + advance(); + while (current() >= '0' and current() <= '9') { + value = value * 10 + current() - '0'; + advance(); + } + return *this; + } +} in("sate.in"); +ofstream out("sate.out"); +int a,b,c,n,m,x,y,v[30001],go,st,dr,i,hz[30001],distanta[30001]; +vector< pair >h[30001]; +int main(){ + in >> n >> m >> x >> y; + for( i = 1; i <= m; i ++ ){ + in >> a >> b >> c; + h[a].push_back( make_pair( b,c ) ); + h[b].push_back( make_pair( a,c ) ); + } + v[1] = x; + hz[x] = 1; + for( st = 1, dr = 1; st <= dr; st ++ ){ + for( i = 0; i < h[v[st]].size(); i ++ ){ + if( v[st] < h[v[st]][i].first ) + distanta[ h[v[st]][i].first ] = distanta[v[st]] + h[v[st]][i].second; + else + distanta[ h[v[st]][i].first ] = distanta[v[st]] - h[v[st]][i].second; + + if( hz[ h[v[st]][i].first ] == 0 ){ + hz[ h[v[st]][i].first ] = 1; + dr++; + v[dr] = h[v[st]][i].first; + } + } + } + out< +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +const int N = 30001, M = 100025, INF=1<<31; +int lst[N], vf[2*M], urm[2*M], w[2*M], q[N], dist[N], nr=0; + +void Add(int x, int y, int d); + +int main() +{ + int n, m, s, fn; + in >> n >> m >> s >> fn; + for(int i=1; i<=n; i++) dist[i]=INF; + for(int i=1; i<=m; i++) + { + int x, y, d; + in >> x >> y >> d; + Add(x, y, d); + Add(y, x, d); + } + + int st=0, dr=-1; + q[++dr]=s; + dist[s]=0; + + while(st<=dr) + { + int x=q[st++]; + for(int p=lst[x];p;p=urm[p]) + { + int y=vf[p]; + if(dist[y]==INF) + { + q[++dr]=y; + if(x +#include +#include +using namespace std; + +const int NMAX=100100; +int n,m,x,y,dist[NMAX],A[NMAX],B[NMAX],C[NMAX]; +void citire() +{ + scanf("%d%d%d%d",&n,&m,&x,&y); + int a,b,c; + for(int i=1;i<=m;i++) + scanf("%d%d%d",&A[i],&B[i],&C[i]); +} +void rezolv() +{ + dist[x]=1; + if(x==y) return ; + while(!dist[y]) + { + for(int i=1;i<=m;i++) + if(dist[A[i]]!=0 && dist[B[i]]==0) dist[B[i]]=dist[A[i]]+C[i]; + else if(dist[A[i]]==0 && dist[B[i]]!=0) dist[A[i]]=dist[B[i]]-C[i]; + } +} +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + citire(); + rezolv(); + printf("%d",dist[y]-1); + return 0; +} diff --git a/villages/bfs/f32f4bc2-40ac-11ec-9ab6-dc7196033e92.cpp b/villages/bfs/f32f4bc2-40ac-11ec-9ab6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c87a3f11ddb0fc9b3dbcd44bd704e476a8cf0437 --- /dev/null +++ b/villages/bfs/f32f4bc2-40ac-11ec-9ab6-dc7196033e92.cpp @@ -0,0 +1,27 @@ +#include +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int a[100100] , b[100100] , d[100100]; +int drum[100100]; + +int main() +{ + fin >> n >> m >> x >> y; + for(int i=1 ; i<=m ; i++) + fin >> a[i] >> b[i] >> d[i]; + drum[x] = 1; + + while(!drum[y]) + for(int i=1 ; i<=m ; i++) + if(!drum[a[i]] && drum[b[i]]) + drum[a[i]] = drum[b[i]] - d[i]; + else if(drum[a[i]] && !drum[b[i]]) + drum[b[i]] = drum[a[i]] + d[i]; + + fout << drum[y] - 1; +} \ No newline at end of file diff --git a/villages/bfs/f4a48b90-40ac-11ec-a5d7-dc7196033e92.cpp b/villages/bfs/f4a48b90-40ac-11ec-a5d7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b2b5ec6f00d797062c42f4afe8055d0d6288387d --- /dev/null +++ b/villages/bfs/f4a48b90-40ac-11ec-a5d7-dc7196033e92.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int a[100025],b[100025],d[100025]; +int s[100025];///daca stim dist de la x la i + +void read() +{ + int i; + fin>>n>>m>>x>>y; + for(i=1; i<=m; ++i)fin>>a[i]>>b[i]>>d[i]; + fin.close(); +} + +void solve() +{int i; + s[x]=1; + while(!s[y]) + + for(i=1;i<=m;++i) + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + else if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + + fout< +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,X,Y,s[30002],a[100025],b[100025],d[100025]; + +int main() +{ + f>>N>>M>>X>>Y; + s[X]=1; + for(int i=1;i<=M;++i) + { + f>>a[i]>>b[i]>>d[i]; + } + + f.close(); + while(!s[Y]) + { + for(int i=1;i<=M;++i) + { + if(s[a[i]] && !s[b[i]]) + { + s[b[i]]=s[a[i]]+d[i]; + } + else + if(!s[a[i]] && s[b[i]]) + { + s[a[i]]=s[b[i]]-d[i]; + } + } + } + + g< +#include +#include +#include + + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int NMAX = 30002; +const int MMAX = 100026; +int N,M,X,Y; +int a,b,c; +int dist[NMAX]; +int x[MMAX],y[MMAX],d[MMAX]; +void Read() +{ + fin>>N>>M>>X>>Y; + for(int i=1; i<=M; ++i) + { + fin>>a>>b>>c; + x[i]=a; + y[i]=b; + d[i]=c; + } +} +void Do() +{ + dist[X]=1; + while(!dist[Y]) + { + for(int i=1; i<=M; ++i) + { + if(dist[x[i]] && !dist[y[i]]) + dist[y[i]]=dist[x[i]]+d[i]; + if(!dist[x[i]] && dist[y[i]]) + dist[x[i]]=dist[y[i]]-d[i]; + } + } + fout< +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,x,y,s[30002],a[100025],b[100025],d[100025]; + +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;++i) + { + f>>a[i]>>b[i]>>d[i]; + } + + f.close(); + while(!s[y]) + { + for(int i=1;i<=M;++i) + { + if(s[a[i]] && !s[b[i]]) + { + s[b[i]]=s[a[i]]+d[i]; + } + else + if(!s[a[i]] && s[b[i]]) + { + s[a[i]]=s[b[i]]-d[i]; + } + } + } + + g< +#include +#include +#include + + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +const int NMAX = 30002; +const int MMAX = 100026; +int N,M,X,Y; +int a,b,c; +int dist[NMAX]; +int x[MMAX],y[MMAX],d[MMAX]; +void Read() +{ + f>>N>>M>>X>>Y; + for(int i=1; i<=M; ++i) + { + f>>a>>b>>c; + x[i]=a; + y[i]=b; + d[i]=c; + } +} +void Do() +{ + dist[X]=1; + while(!dist[Y]) + { + for(int i=1; i<=M; ++i) + { + if(dist[x[i]] && !dist[y[i]]) + dist[y[i]]=dist[x[i]]+d[i]; + if(!dist[x[i]] && dist[y[i]]) + dist[x[i]]=dist[y[i]]-d[i]; + } + } + g< +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int n,m,x,i,y,d[30003],a[100025],b[100025],ld[100025]; +int main() +{ + f>>n>>m>>x>>y; + d[x]=1; + for(i=1;i<=m;i++) + f>>a[i]>>b[i]>>ld[i]; + while(d[y]==0) + { + for(i=1; i<=m; i++ ) + { + if(d[a[i]]!=0&&d[b[i]]==0) + d[b[i]]=d[a[i]]+ld[i]; + else if(d[a[i]]==0&&d[b[i]]!=0) + d[a[i]]=d[b[i]]-ld[i]; + } + } + g< +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + s[x]=1; + for(int i=1;i<=M;i++) + f>>a[i]>>b[i]>>d[i]; + while(!s[y]) + { + for(int i=1;i<=M;i++) + { + if(s[a[i]]&&!s[b[i]]) + s[b[i]]=s[a[i]]+d[i]; + else + if(!s[a[i]]&&s[b[i]]) + s[a[i]]=s[b[i]]-d[i]; + } + } + g< + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int N,M,x,y,s[30003],a[100025],b[100025],d[100025]; +int main() +{ + f>>N>>M>>x>>y; + + s[x]=1; + + for( int i=1; i<=M; i++ ) + f>>a[i]>>b[i]>>d[i]; + + while(!s[y]) + { + for( int i=1; i<=M; i++ ) + { + if( s[a[i]]&&!s[b[i]] ) + s[b[i]]=s[a[i]]+d[i]; + else + if( !s[a[i]]&&s[b[i]] ) + s[a[i]]=s[b[i]]-d[i]; + } + } + + g< +#include +#include +using namespace std; + +const int MaxNodesCnt = 3e4 + 5, buffSZ = 4e4 + 5; + +char buff[buffSZ]; +int buffPointer = buffSZ - 1; + +inline void read(int &no){ + + while(!isdigit(buff[buffPointer])) + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + no = 0; + while(isdigit(buff[buffPointer])){ + + no = no * 10 + buff[buffPointer] - '0'; + if(++buffPointer == buffSZ){ + + buffPointer = 0; + fread(buff, 1, buffSZ, stdin); + } + } +} + +struct Graph{ + + int newNode, cost; +}; + +list adjList[MaxNodesCnt]; +int dist[MaxNodesCnt]; +int nodesCnt, edgesCnt, startNode, endNode; + +inline void readData(){ + + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + + read(nodesCnt); read(edgesCnt); read(startNode); read(endNode); + + int from, to, cost; + while(edgesCnt--){ + + read(from); read(to); read(cost); + adjList[from].push_back({to, cost}); + adjList[to].push_back({from, -cost}); + } +} + +inline void BFS(int node){ + + deque Queue; + Queue.push_back(node); + + int newNode, cost; + + while(!Queue.empty()){ + + node = Queue.front(); + Queue.pop_front(); + + for(const Graph &nextNode : adjList[node]){ + + newNode = nextNode.newNode; + cost = nextNode.cost; + if(dist[newNode] == 0 and newNode != startNode){ + + dist[newNode] = dist[node] + cost; + Queue.push_back(newNode); + } + } + } +} + +int main(){ + + readData(); + BFS(startNode); + printf("%d", dist[endNode]); +} diff --git a/villages/bfs/f92cde14-2e08-4d4d-b256-258ab4ccc615.cpp b/villages/bfs/f92cde14-2e08-4d4d-b256-258ab4ccc615.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2bfd75ad39cbe95bf1afffc1cbb53e5713068424 --- /dev/null +++ b/villages/bfs/f92cde14-2e08-4d4d-b256-258ab4ccc615.cpp @@ -0,0 +1,20 @@ +#include +#define DIM 100025 +using namespace std; +int sol[30001],st[DIM],dr[DIM],x[DIM]; +int main() +{ int n,i,a,b,m; + ifstream f("sate.in"); + ofstream g("sate.out"); + f>>n>>m>>a>>b; + sol[a]=1; + for (i=1;i<=m;++i) + f>>st[i]>>dr[i]>>x[i]; + while (!sol[b]) { + for (i=1;i<=m;++i) + if (sol[st[i]] && !sol[dr[i]]) sol[dr[i]]=sol[st[i]]+x[i]; + else if (!sol[st[i]] && sol[dr[i]]) sol[st[i]]=sol[dr[i]]-x[i]; + } + g< +#define nmax 100030 + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y; + +int a[nmax], b[nmax], dist[nmax], pasi[30005]; + +int main() +{ + in>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + in>>a[i]>>b[i]>>dist[i]; + } + pasi[x]=1; + while(!pasi[y]) + { + for(int i=1; i<=m; i++) + { + if(pasi[a[i]] && !pasi[b[i]]) + { + pasi[b[i]] = pasi[a[i]] + dist[i]; + } + else + if(!pasi[a[i]] && pasi[b[i]]) + { + pasi[a[i]] = pasi[b[i]] - dist[i]; + } + } + } + out< +#include +#include +#include + +using namespace std; + +const int n_max = 30009; +vector < pair < int, int> > v[n_max]; +int cost[n_max], + vaz[n_max], + s[n_max]; +int i, m, n, left, right, x, y, a, b, c; +void process(int x) +{ + vector < pair < int, int> >::iterator it; + int t; + for (it = v[x].begin(); it != v[x].end(); ++ it) + if (!vaz[(*it).first]) + { + s[++right] = (*it).first; + if (x > (*it).first) + t = -1; + else + t = 1; + cost[(*it).first] = cost[x] + ((*it).second * t); + vaz[(*it).first] = 1; + } +} + +int main() +{ + freopen("sate.in","r",stdin); + freopen("sate.out","w",stdout); + scanf("%d %d %d %d", &n, &m, &x, &y); + for (i = 1; i <= m; ++ i) + { + scanf("%d %d %d", &a, &b, &c); + v[a].push_back(make_pair(b,c)); + v[b].push_back(make_pair(a,c)); + } + left = 0; + right = 1; + s[right] = x; + vaz[x] = 1; + while (left < right && !vaz[y]) + { + ++left; + process(s[left]); + } + printf("%d\n",cost[y]); + return 0; +} diff --git a/villages/bfs/fda73f05-40ac-11ec-9fe6-dc7196033e92.cpp b/villages/bfs/fda73f05-40ac-11ec-9fe6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fcf5d65fe7dd8bbb7dc5015adca836452642ec7 --- /dev/null +++ b/villages/bfs/fda73f05-40ac-11ec-9fe6-dc7196033e92.cpp @@ -0,0 +1,104 @@ +#include +#include +using namespace std; +struct muchie{ + int x,c; +}; +vector < muchie > v[30001]; +int a,b,n,m,x,y,k1,k2,q[90000001],viz[30001],j,S,i,d; +char s[1001]; +void coada(int x){ + int p,u,i,l; + p=u=1; + q[p]=x; + viz[x]=1; + while(p<=u && viz[y]==0){ + l=q[p]; + p++; + for(i=0;i='0' && s[i]<='9'){ + n=n*10+(s[i]-'0'); + i++; + } + i++; + //pt m + m=s[i]-'0'; + i++; + while(s[i]>='0' && s[i]<='9'){ + m=m*10+(s[i]-'0'); + i++; + } + i++; + //pt x + x=s[i]-'0'; + i++; + while(s[i]>='0' && s[i]<='9'){ + x=x*10+(s[i]-'0'); + i++; + } + i++; + //pt y + y=s[i]-'0'; + i++; + while(s[i]>='0' && s[i]<='9'){ + y=y*10+(s[i]-'0'); + i++; + } + for(i=1;i<=m;i++){ + cin.getline(s,999); + //pt a + a=s[0]-'0'; + j=1; + while(s[j]>='0' && s[j]<='9'){ + a=a*10+(s[j]-'0'); + j++; + } + j++; + //pt b + b=s[j]-'0'; + j++; + while(s[j]>='0' && s[j]<='9'){ + b=b*10+(s[j]-'0'); + j++; + } + j++; + //pt d + d=s[j]-'0'; + j++; + while(s[j]>='0' && s[j]<='9'){ + d=d*10+(s[j]-'0'); + j++; + } + v[a].push_back({b,d}); + v[b].push_back({a,d}); + } + S=0; + coada(x); + viz[y]--; + cout< +#include +#include +#include +#define f in +#define g out +#define sat first +#define dist second + +using namespace std; +ifstream in ( "sate.in" ); +ofstream out( "sate.out" ); +vector< pair > L[30010]; +int n, m, i, j, start, finish, x, y, cost, p, u, vecin, nod; +bitset<30030>fr; +int v[30009], d[30009]; +char s[50]; + +int main() { + f>>n>>m>>start>>finish; f.get(); + for ( ; m--; ){ + f.getline(s, 30); + + i = 0; x = 0; + while ( s[i] != ' ' ) + x = x*10 + s[i++] - '0'; + + i++; y = 0; + while ( s[i] != ' ' ) + y = y*10 + s[i++] - '0'; + + i++; cost = 0; + while ( isdigit(s[i]) ) + cost = cost*10 + s[i++] - '0'; + + L[x].push_back({y, cost}); + L[y].push_back({x, -cost}); + } + p = u = 1; + v[1] = start; + while ( p <= u ){ + nod = v[p]; + for ( i=0; i < L[nod].size(); ++i ){ + vecin = L[nod][i].sat; + cost = L[nod][i].dist; + if ( !fr[vecin] ){ + fr[vecin] = 1; + d[vecin] = d[nod]+cost; + v[++u] = vecin; + if ( vecin == finish ){ + g< + +#include + +#include + +#include + +using namespace std; + +ifstream fin("sate.in"); + +ofstream fout("sate.out"); + +struct muchie{ + + int x,c; + +}; + +vector v[30001]; + +int a,b,n,m,x,y,k1,k2,q[90000001],viz[30001],j,S,i,d,ns; + +char s[1001]; + +void coada(int x){ + + int p,u,i,l; + + p=u=1; + + q[p]=x; + + viz[x]=1; + + while(p<=u){ + + l=q[p]; + + p++; + + for(i=0;i='0'&&s[i]<='9'){ + + n=n*10+(s[i]-'0'); + + i++;} + + i++; + + m=s[i]-'0'; + + i++; + + while(s[i]>='0'&&s[i]<='9'){ + + m=m*10+(s[i]-'0'); + + i++;} + + i++; + + x=s[i]-'0'; + + i++; + + while(s[i]>='0'&&s[i]<='9'){ + + x=x*10+(s[i]-'0'); + + i++;} + + i++; + + y=s[i]-'0'; + + i++; + + while(s[i]>='0'&&s[i]<='9'&&i='0'&&s[j]<='9'){ + + a=a*10+(s[j]-'0'); + + j++; + + } + + j++; + + b=s[j]-'0'; + + j++; + + while(s[j]>='0'&&s[j]<='9'){ + + b=b*10+(s[j]-'0'); + + j++; + + } + + j++; + + d=s[j]-'0'; + + j++; + + while(s[j]>='0' && s[j]<='9' && j +#include +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +struct muchie{ + int x,c; +}; +vector v[30001]; +int a,b,n,m,x,y,k1,k2,q[90000001],viz[30001],j,S,i,d,ns; +char s[1001]; +void coada(int x){ + int p,u,i,l; + p=u=1; + q[p]=x; + viz[x]=1; + while(p<=u){ + l=q[p]; + p++; + for(i=0;i='0'&&s[i]<='9'){ + n=n*10+(s[i]-'0'); + i++;} + i++; + m=s[i]-'0'; + i++; + while(s[i]>='0'&&s[i]<='9'){ + m=m*10+(s[i]-'0'); + i++;} + i++; + x=s[i]-'0'; + i++; + while(s[i]>='0'&&s[i]<='9'){ + x=x*10+(s[i]-'0'); + i++;} + i++; + y=s[i]-'0'; + i++; + while(s[i]>='0'&&s[i]<='9'&&i='0'&&s[j]<='9'){ + a=a*10+(s[j]-'0'); + j++; + } + j++; + b=s[j]-'0'; + j++; + while(s[j]>='0'&&s[j]<='9'){ + b=b*10+(s[j]-'0'); + j++; + } + j++; + d=s[j]-'0'; + j++; + while(s[j]>='0' && s[j]<='9' && j +#include +#include +using namespace std; + +typedef pair edge; + +int n, m, s, d; +bool viz[30001], done; +vector g[30001]; + +void citire(); +void afisare(int); +void dfs(int, int); + + +int main() { + citire(); + + dfs(s, 0); + + return 0; +} + + +void dfs(int x, int cost) { + viz[x] = true; + if (x == d) { + afisare(cost); + done = true; + return; + } + + for (int i = 0; i < g[x].size(); ++i) { + int y = g[x][i].second; + if (viz[y]) continue; + dfs(y, cost + g[x][i].first); + if (done) return; + } +} + +void afisare(int cost) { + ofstream out("sate.out"); + out << cost; + out.close(); +} + +void citire() { + ifstream in("sate.in"); + in >> n >> m >> s >> d; + + for (int i = 1, x, y, c; i <= m; ++i) { + in >> x >> y >> c; + g[x].push_back(make_pair(c, y)); + g[y].push_back(make_pair(-c, x)); + } + + in.close(); +} diff --git a/villages/dfs/00e6bd2e-40ad-11ec-95f7-dc7196033e92.cpp b/villages/dfs/00e6bd2e-40ad-11ec-95f7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4702232f01f742b1acf4812f3446d316ab4da2be --- /dev/null +++ b/villages/dfs/00e6bd2e-40ad-11ec-95f7-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); +struct drum +{ + int nod,cost; +} varaux1,varaux2; + +int seen [40001]; +int n,x,y,distanta; +vectorv[100050]; +void dfs ( int nod ) +{ + if(nod==y) + { + fout<>n>>m>>x>>y; + for(i=0; i>a>>b>>dist; + varaux1= {b,dist}; + varaux2= {a,-dist}; + v[a].push_back(varaux1); + v[b].push_back(varaux2); + } + dfs(x); + return 0; +} diff --git a/villages/dfs/01990038-40ad-11ec-891a-dc7196033e92.cpp b/villages/dfs/01990038-40ad-11ec-891a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..854951e5ee2655c734f877fc8682b419eeac573c --- /dev/null +++ b/villages/dfs/01990038-40ad-11ec-891a-dc7196033e92.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int seen[40000], d = 0, y; + +struct drumuri +{ + int vecin; + int dis; +}; + + + +void DFS(int nod, vector v[40000]) +{ + if(nod == y) + { + out< v[40000]; + int n, m, a, b, i, x, dist; + in>>n>>m>>x>>y; + for(i = 0; i>a>>b>>dist; + + drumuri dr = {b, dist}; + v[a].push_back(dr); + + drumuri dr2 = {a, -dist}; + v[b].push_back(dr2); + } + DFS(x, v); + return 0; +} diff --git a/villages/dfs/02ae651d-4667-4639-b4ec-029ef74815ee.cpp b/villages/dfs/02ae651d-4667-4639-b4ec-029ef74815ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5aedc1cdd3d4f0c76b32ffcee5d28c209bf537a8 --- /dev/null +++ b/villages/dfs/02ae651d-4667-4639-b4ec-029ef74815ee.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +std::ifstream fin("sate.in"); +std::ofstream fout("sate.out"); + +const int mxn = 30e3 + 5; + +int n, m, start, finish; +std::vector < std::pair < int, int > > mat[mxn]; +int viz[mxn]; + +inline void DFS(int nod){ + for (auto i : mat[nod]){ + if (!viz[i.second]){ + viz[i.second] = viz[nod] + i.first; + DFS(i.second); + } + } +} + +inline void solve(){ + fin >> n >> m >> start >> finish; + for (int i = 1; i <= m; ++i){ + int x, y, d; + fin >> x >> y >> d; + mat[x].push_back({d, y}); + mat[y].push_back({-d, x}); + } + viz[start] = 1; + DFS(start); + fout << std::max(viz[start] - viz[finish], -viz[start] + viz[finish]); +} + +int main(){ + solve(); + return 0; +} diff --git a/villages/dfs/077b7778-cc65-4405-a170-6faa261eba4b.cpp b/villages/dfs/077b7778-cc65-4405-a170-6faa261eba4b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98fc21947b13fd9640850642ddd37e82f7653010 --- /dev/null +++ b/villages/dfs/077b7778-cc65-4405-a170-6faa261eba4b.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + pii *it = &muchii[itt]; + if (dst[it->first] != 0x3F3F3F3F) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0823dac0-40ad-11ec-88b5-dc7196033e92.cpp b/villages/dfs/0823dac0-40ad-11ec-88b5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e983bb137ecda6e22812ae9527cbd4101778ed --- /dev/null +++ b/villages/dfs/0823dac0-40ad-11ec-88b5-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include +const int MAX_N = 30000; +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct Muchie { + int v, c; +}; +Muchie x, y; +long long n, m, s, ok; +bool vizitat[MAX_N + 1]; +int dp[MAX_N + 1]; + +vector vecini[MAX_N + 1]; + +void dfs(Muchie u) { + vizitat[u.v] = 1; + if (ok == 1) + return; + if(u.v == y.v) { + ok = 1; + return; + } + for(auto v : vecini[u.v]) + if(!vizitat[v.v]) { + dp[v.v]=dp[u.v]+v.c; + dfs(v); + } +} +int main() +{ + fin >> n >> m >> x.v >> y.v; + for (int i = 1; i <= m; i++) { + int u, w, c; + fin >> u >> w >> c; + vecini[u].push_back({w, c}); + vecini[w].push_back({u, -c}); + } + dfs(x); + fout << dp[y.v] << '\n'; + return 0; +} diff --git a/villages/dfs/088aab13-8c38-455c-8d67-b42cf8aee38d.cpp b/villages/dfs/088aab13-8c38-455c-8d67-b42cf8aee38d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbecdda6999c9e73e0c353f4a3bb17e81d4184d3 --- /dev/null +++ b/villages/dfs/088aab13-8c38-455c-8d67-b42cf8aee38d.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#define Nmax 30005 + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector >lista[Nmax]; +int viz[Nmax]; +int sol[Nmax]; +int n,m; +int X,Y; + +void citire() +{ + int x,y,z,zprim; + + fin>>n>>m>>X>>Y; + + while(m--) + { + fin>>x>>y>>z; + zprim=z*-1; + + lista[x].push_back(make_pair(y,z)); + lista[y].push_back(make_pair(x,zprim)); + + } + + fin.close(); +} + + +void DFS(int a) +{ viz[a]=1; + if(!sol[Y]) + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0a5be255-40ad-11ec-bf80-dc7196033e92.cpp b/villages/dfs/0a5be255-40ad-11ec-bf80-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..949e36d9c7e3d4ff0f82d35c0f85efc13657c0f2 --- /dev/null +++ b/villages/dfs/0a5be255-40ad-11ec-bf80-dc7196033e92.cpp @@ -0,0 +1,36 @@ +#include + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +const int NMAX = 30005; +vector< pair > v[NMAX]; +int viz[NMAX],x,y; +void dfs(int now, int s) +{ + viz[now] = s; + if (!viz[y]) + for (auto it: v[now]) + { + int next = it.first, dist = it.second; + if (!viz[next]) + dfs(next,s+dist); + } +} +int main() +{ + ios::sync_with_stdio(0); + in.tie(0); + out.tie(0); + int n,m; + in >> n >> m >> x >> y; + for (int i = 1; i<=m; i++) + { + int a,b,val; + in >> a >> b >> val; + v[a].push_back({b,val}); + v[b].push_back({a,-val}); + } + dfs(x,1); + out << viz[y]-1; +} diff --git a/villages/dfs/0ba51e63-9afb-4d18-9793-954b4c17b9e4.cpp b/villages/dfs/0ba51e63-9afb-4d18-9793-954b4c17b9e4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00c06866a272768df62289301ac872a8c490da7d --- /dev/null +++ b/villages/dfs/0ba51e63-9afb-4d18-9793-954b4c17b9e4.cpp @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +vector < pair > v[30001]; +bool viz[30001]; +char s[1001]; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n,m,st,fin; + +void read() +{ in>>n>>m>>st>>fin; + in.get(); + int x, y, c, nr, cnt; + for (int k = 1; k <= m; ++k) + { cnt = 0; + in.getline(s, 1001); + for (int i = 0; s[i] != '\0'; ++i) { + if (s[i] >= '0' && s[i] <= '9') { + nr = 0; + cnt++; + while (s[i] >= '0' && s[i] <= '9') { + nr = nr * 10 + s[i] - '0'; + ++i; + } + if (cnt == 1) x = nr; + if (cnt == 2) y = nr; + if (cnt == 3) c = nr; + } + } + v[x].push_back({y,c}); + v[y].push_back({x,c}); + } +} + +void drum(int nod,int dist) +{ + viz[nod]=1; + if (nod==fin) + { + out<nod) + drum(nod1,dist+d); + } + } +} + +int main() +{ + read(); + drum(st,0); + return 0; +} diff --git a/villages/dfs/0ba7a976-c598-4acc-852a-b4b4fc6d687c.cpp b/villages/dfs/0ba7a976-c598-4acc-852a-b4b4fc6d687c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053e18fa5562bb95ab91a2d3805ab4b71c318226 --- /dev/null +++ b/villages/dfs/0ba7a976-c598-4acc-852a-b4b4fc6d687c.cpp @@ -0,0 +1,48 @@ +#include +#define IOS ios_base::sync_with_stdio(0); +#define tie cin.tie(0); +#define mp make_pair +#define ll long long +#define PII pair +#define PLL pair +#define inf 1000000000 +#define zeros(x) ( (x ^ (x - 1)) & x ) + +using namespace std; + +int n, m, x, y, z, in, sf; + +vector < PII > V[100100]; +int cost[100100]; + +void dfs(int x) +{ + for (auto it : V[x]) + { + if (!cost[it.first]) + { + cost[it.first] = cost[x] + it.second; + if (it.first == sf) break; + dfs(it.first); + } + } +} + + +int main(){ + IOS tie + ifstream cin("sate.in"); + ofstream cout("sate.out"); + cin >> n >> m >> in >> sf; + if (in > sf) swap(in, sf); + for (int i = 1; i <= m; i++) + { + cin >> x >> y >> z; + V[x].push_back(mp(y, z)); + V[y].push_back(mp(x, -z)); + } + dfs(in); + cout << cost[sf]; + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + return 0; +} \ No newline at end of file diff --git a/villages/dfs/0cebb811-1227-447f-81f6-2c34179e630d.cpp b/villages/dfs/0cebb811-1227-447f-81f6-2c34179e630d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1819b649b48b2a49279e14ec29be6141d3f38f27 --- /dev/null +++ b/villages/dfs/0cebb811-1227-447f-81f6-2c34179e630d.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; +#define nmax 30005 + + +vector>Q[nmax]; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y,dist[nmax]; +bool passed=0; + +void read() +{ + f>>n>>m>>x>>y; + for (int i=1; i<=m; ++i) + { + int r1,r2,r3; + f>>r1>>r2>>r3; + Q[r1].push_back({r2,r3}); + Q[r2].push_back({r1,-r3}); + } + +} + +void dfs(int nod) +{ + if (nod==y) + passed=true; + if (passed) + return; + for (auto w:Q[nod]) + { + if (!dist[w.first]) + { + dist[w.first]=dist[nod]+w.second; + dfs(w.first); + } + } +} + +void solve() +{ + dist[x]=1; + dfs(x); + g< +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + register node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, X, Y; + register int M; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + register int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0ea3cefc-40ad-11ec-9bbb-dc7196033e92.cpp b/villages/dfs/0ea3cefc-40ad-11ec-9bbb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5db8d95def74a39e6f102093b51b67e013cf2f44 --- /dev/null +++ b/villages/dfs/0ea3cefc-40ad-11ec-9bbb-dc7196033e92.cpp @@ -0,0 +1,113 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, X, Y; + register int M; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0ef6d608-40ad-11ec-b548-dc7196033e92.cpp b/villages/dfs/0ef6d608-40ad-11ec-b548-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df2700901f071a3312526991819a2fec88c7c8e5 --- /dev/null +++ b/villages/dfs/0ef6d608-40ad-11ec-b548-dc7196033e92.cpp @@ -0,0 +1,112 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0fadfcf8-40ad-11ec-a31a-dc7196033e92.cpp b/villages/dfs/0fadfcf8-40ad-11ec-a31a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddf0b563337261bbdaf3435f617ed9a23a8e2ebd --- /dev/null +++ b/villages/dfs/0fadfcf8-40ad-11ec-a31a-dc7196033e92.cpp @@ -0,0 +1,102 @@ +#pragma GCC optimize("Ofast") +#include +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/0fe76083-40ad-11ec-b816-dc7196033e92.cpp b/villages/dfs/0fe76083-40ad-11ec-b816-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bc4ec9dc6690605f36d79668f957af8cf7aa37c --- /dev/null +++ b/villages/dfs/0fe76083-40ad-11ec-b816-dc7196033e92.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef unsigned long long hsh; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/1025a4a9-40ad-11ec-baf8-dc7196033e92.cpp b/villages/dfs/1025a4a9-40ad-11ec-baf8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..620bb1bff377d7d7942dc0960acdb78ee5e25d49 --- /dev/null +++ b/villages/dfs/1025a4a9-40ad-11ec-baf8-dc7196033e92.cpp @@ -0,0 +1,111 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/10b20f6e-40ad-11ec-b29e-dc7196033e92.cpp b/villages/dfs/10b20f6e-40ad-11ec-b29e-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..272f0e80a4ace0cddf2201135507cbbe79fc82e2 --- /dev/null +++ b/villages/dfs/10b20f6e-40ad-11ec-b29e-dc7196033e92.cpp @@ -0,0 +1,106 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/10c980b1-10f1-4472-a930-72786e1e31ec.cpp b/villages/dfs/10c980b1-10f1-4472-a930-72786e1e31ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/10c980b1-10f1-4472-a930-72786e1e31ec.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + pii *it = &muchii[itt]; + if (dst[it->first] != 0x3F3F3F3F) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/11a2bec6-062b-40e2-805d-e89e127381c2.cpp b/villages/dfs/11a2bec6-062b-40e2-805d-e89e127381c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/11a2bec6-062b-40e2-805d-e89e127381c2.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#include +#define N 30002 + +using namespace std; + +int n, m, x, y, d[N]; +bool ok; +vector>A[N]; + +void df(int nod) +{ + if(nod==y) + ok=1; + + if(!ok) + { + for(auto i:A[nod]) + { + if(!d[i.first]) + { + d[i.first]=d[nod]+i.second; + df(i.first); + } + } + } +} + +int main() +{ + //freopen("sate.in", "r", stdin); + //freopen("sate.out", "w", stdout); + ifstream fin("sate.in"); + ofstream fout("sate.out"); + //scanf("%d %d %d %d", &n, &m, &x, &y); + fin>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + int n1,n2,n3; + //scanf("%d %d %d", &n1, &n2, &n3); + fin>>n1>>n2>>n3; + A[n1].push_back({n2,n3}); + A[n2].push_back({n1,-n3}); + } + d[x]=1; + df(x); + //printf("%d\n",d[y]-1); + fout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back({b,cost}); + A[b].push_back({a,-cost}); + } + + DFS(x); + fout< +#include +#define N 30005 +using namespace std; + +struct elem{ + int node, cost; + }; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, st, fi, dist[N]; +bool viz[N]; + +inline void Read() +{ + int m, x, y, d; + elem e; + + fin>>n>>m>>st>>fi; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.node = y; + G[x].push_back(e); + + e.cost = -d; + e.node = x; + G[y].push_back(e); + } +} + +inline void Dfs(int node) +{ + elem vec; + int i; + + if (!viz[fi]) + + for (i = 0; i < G[node].size(); ++i) + { + vec=G[node][i]; + + if (!viz[vec.node]) + { + dist[vec.node] = dist[node] + vec.cost; + viz[node] = 1; + Dfs(vec.node); + } + } +} + +void Write() +{ + fout< +#include + +#define N 30005 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +int dist[N]; +vector > G[N]; +bool viz[N]; + +inline void DFS(const int &nod) +{ + viz[nod] = 1; + for(auto i : G[nod]) + if(!viz[i.first]) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } +} + +int main() +{ + int n, m, X, Y; + fin >> n >> m >> X >> Y; + for(int i = 1, x, y, c; i <= m; ++i) + { + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, -c)); + } + fin.close(); + + DFS(X); + fout << dist[Y]; + fout.close(); + return 0; +} diff --git a/villages/dfs/17ae2e05-40ad-11ec-a2cd-dc7196033e92.cpp b/villages/dfs/17ae2e05-40ad-11ec-a2cd-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1819b649b48b2a49279e14ec29be6141d3f38f27 --- /dev/null +++ b/villages/dfs/17ae2e05-40ad-11ec-a2cd-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; +#define nmax 30005 + + +vector>Q[nmax]; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y,dist[nmax]; +bool passed=0; + +void read() +{ + f>>n>>m>>x>>y; + for (int i=1; i<=m; ++i) + { + int r1,r2,r3; + f>>r1>>r2>>r3; + Q[r1].push_back({r2,r3}); + Q[r2].push_back({r1,-r3}); + } + +} + +void dfs(int nod) +{ + if (nod==y) + passed=true; + if (passed) + return; + for (auto w:Q[nod]) + { + if (!dist[w.first]) + { + dist[w.first]=dist[nod]+w.second; + dfs(w.first); + } + } +} + +void solve() +{ + dist[x]=1; + dfs(x); + g< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#include + +using namespace std; + + +ifstream in("sate.in"); +ofstream out("sate.out"); + + +const int MAX = 30005; + + +vector < pair < int , int > > myVector[MAX]; + +bool beenThere[MAX]; + +int N,M,Plec,Ajung; +int D[MAX]; + +void Read() +{ + in >> N >> M >> Plec >> Ajung; + + for ( int i = 1; i <= M ; ++i) + { + int x,y,cost; + int costnegativ; + in >> x >> y >> cost; + costnegativ = cost*-1; + + myVector[x].push_back(make_pair(y,cost)); + myVector[y].push_back(make_pair(x,costnegativ)); + } +} + +void DFS(int nod) +{ + beenThere[nod] = true; + + for ( size_t k = 0 ; k < myVector[nod].size(); k++) + { + int Vecin = myVector[nod][k].first; + int cost = myVector[nod][k].second; + + if(beenThere[Vecin] == false) + { + + D[Vecin] = D[nod] + cost; + + + DFS(Vecin); + } + } + +} + + +void Rezolv() +{ + + + DFS(Plec); + + out << D[Ajung]; + + + +} + + +int main() +{ + Read(); + Rezolv(); + + return 0; +} diff --git a/villages/dfs/1c89e2d4-40ad-11ec-9ef2-dc7196033e92.cpp b/villages/dfs/1c89e2d4-40ad-11ec-9ef2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbecdda6999c9e73e0c353f4a3bb17e81d4184d3 --- /dev/null +++ b/villages/dfs/1c89e2d4-40ad-11ec-9ef2-dc7196033e92.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#define Nmax 30005 + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector >lista[Nmax]; +int viz[Nmax]; +int sol[Nmax]; +int n,m; +int X,Y; + +void citire() +{ + int x,y,z,zprim; + + fin>>n>>m>>X>>Y; + + while(m--) + { + fin>>x>>y>>z; + zprim=z*-1; + + lista[x].push_back(make_pair(y,z)); + lista[y].push_back(make_pair(x,zprim)); + + } + + fin.close(); +} + + +void DFS(int a) +{ viz[a]=1; + if(!sol[Y]) + for(int i=0;i +using namespace std; + + +#define MAXN 30001 +vector < pair > v[MAXN]; +bool viz[MAXN]; +int drum[MAXN]; +int b, a, ans; +int dfs(int nod) { + viz[nod] = 1; + for(int i = 0; i < v[nod].size(); i++) { + pair it = v[nod][i]; + if(viz[it.first] == 0){ + drum[it.first]=drum[nod]+it.second; + //printf("%d %d\n", it.first, drum[it.first]); + if(it.first==b){ + return 0; + } + dfs(it.first); + } + + } +} + +int main(){ + FILE*fin=fopen("sate.in", "r"); + FILE*fout=fopen("sate.out", "w"); + int n, m, a, b, i, x, y, c; + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for(i=1; i<=m; i++){ + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back({y, c}); + v[y].push_back({x, -c}); + } + ans=0; + dfs(a); + if(b>a) + fprintf(fout, "%d", drum[b]); + else + fprintf(fout, "%d", -drum[b]); + return 0; +} diff --git a/villages/dfs/1e34035e-40ad-11ec-8ded-dc7196033e92.cpp b/villages/dfs/1e34035e-40ad-11ec-8ded-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6af10ccd6caeef4c08247508f029c9bbdcd0f08 --- /dev/null +++ b/villages/dfs/1e34035e-40ad-11ec-8ded-dc7196033e92.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#define NN 30010 +#include +using namespace std; + +ifstream in("sate.in");ofstream out("sate.out"); + int n , m , ni, nf; +struct nod{ +int dist, info; +nod *next; +}*L[NN]; + + +void adaug(int x , int y,int cost) +{ + //adaug pe y la lista lui x + +nod * aux = new nod; +aux -> info = y; +aux->dist=cost; +aux-> next = L[x]; +L[x]=aux; +} + +void citire() +{ + in >> n >> m>> ni >>nf; + + for(int i = 0 ; i< m ; ++i ) + { + int x, y,c; + in >> x>> y>>c; + adaug(x,y,c); + adaug(y,x,c); + } +} +bool viz[NN]; + +void bfs(int NOD, int D) +{viz[NOD]=true; + if(NOD==nf)out< next) + if(!viz[aux->info]) + { + if(NOD- aux->info >0) + { + bfs(aux->info,D-aux->dist); + } + else { + bfs(aux->info,D+aux->dist); + + + } + + + } + } +} + + + + + +int main() +{ citire(); +bfs(ni,0); + + + return 0; +} diff --git a/villages/dfs/1f17688b-5668-4b18-a653-b8c5cd577da4.cpp b/villages/dfs/1f17688b-5668-4b18-a653-b8c5cd577da4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/1f17688b-5668-4b18-a653-b8c5cd577da4.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< + +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +const int NMAX = 30005; +vector< pair > v[NMAX]; +int viz[NMAX],x,y; +void dfs(int now, int s) +{ + viz[now] = s; + if (!viz[y]) + for (auto it: v[now]) + { + int next = it.first, dist = it.second; + if (!viz[next]) + dfs(next,s+dist); + } +} +int main() +{ + ios::sync_with_stdio(0); + in.tie(0); + out.tie(0); + int n,m; + in >> n >> m >> x >> y; + for (int i = 1; i<=m; i++) + { + int a,b,val; + in >> a >> b >> val; + v[a].push_back({b,val}); + v[b].push_back({a,-val}); + } + dfs(x,1); + out << viz[y]-1; +} diff --git a/villages/dfs/1faa869d-7d79-41a9-8e3b-bed94f8e24a5.cpp b/villages/dfs/1faa869d-7d79-41a9-8e3b-bed94f8e24a5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b91cd025a70d9adbb8b7069dc2774e7a6c542db --- /dev/null +++ b/villages/dfs/1faa869d-7d79-41a9-8e3b-bed94f8e24a5.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +typedef long long ll; +const int lim=3e4+5; +vector > vec[lim]; +int n,m,st,dr,x,y; ll d; +ll dist[lim]; +bool ok[lim]; +void df(int nod) +{ + ok[nod]=true; + for(auto x:vec[nod]) + if(!ok[x.first]) + dist[x.first]=dist[nod]+x.second, + df(x.first); +} +int main() +{ + in>>n>>m>>st>>dr; + for(int i=1;i<=m;++i) + { + in>>x>>y>>d; + vec[x].push_back({y,d}); + vec[y].push_back({x,-d}); + } + df(st); + out< +#include + +using namespace std; + +ifstream fin("sate.in") ; +ofstream fout("sate.out"); + +struct str { + int x, y; +}; + +const int nmax=30000; +vector g[nmax+1]; +int sol[nmax+1]; + +void dfs (int x) { + for (vector ::iterator it = g[x].begin(); it != g[x].end(); ++ it ) { + if (sol[it->x]==0) { + sol[it->x]=sol[x]+it->y; + dfs(it->x); + } + } +} + +int main () { + int n, m, a, b; + fin>>n>>m>>a>>b; + + for (int i=1; i<=m; i++) { + int x, y, c; + fin>>x>>y>>c; + str aux; + aux.x=y; + aux.y=c; + g[x].push_back(aux); + aux.x=x; + aux.y=-c; + g[y].push_back(aux); + } + + sol[a]=3e7; + dfs(a); + + fout< +#include + +#define N 30005 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +int dist[N]; +vector > G[N]; +bool viz[N]; + +inline void DFS(const int &nod) +{ + viz[nod] = 1; + for(auto i : G[nod]) + if(!viz[i.first]) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } +} + +int main() +{ + int n, m, X, Y; + fin >> n >> m >> X >> Y; + for(int i = 1, x, y, c; i <= m; ++i) + { + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, -c)); + } + fin.close(); + + DFS(X); + fout << dist[Y]; + fout.close(); + return 0; +} diff --git a/villages/dfs/212297f7-bafd-44f9-8642-1f56e767ba80.cpp b/villages/dfs/212297f7-bafd-44f9-8642-1f56e767ba80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/212297f7-bafd-44f9-8642-1f56e767ba80.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +using namespace std; +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +}f("sate.in"); +ofstream g("sate.out"); +int n,m,x,y; +int dmin=1e8; +vectorv[30002]; +bool afa[30002]; +void dfs(int nod,int dsf) +{ + if(nod==y) + { + dmin=min(dmin,dsf); + return; + } + for(int i=0;i>n>>m>>x>>y; + for(int i=1;i<=m;++i) + { + int a,b,c; + f>>a>>b>>c; + v[a].push_back(b); + v[a].push_back(c); + v[b].push_back(a); + v[b].push_back(-c); + } + dfs(x,0); + g< +#include +#include +#define DN 30050 +using namespace std; + +fstream fin("sate.in",ios::in),fout("sate.out",ios::out); +vector< pair > v[DN]; +int N,M,X,Y,ok=0; +int dist[DN]; + + +void dfs(int nod) +{ + if(nod==Y) + { + fout<<(dist[nod]-1)<<"\n"; + ok=1; + } + if(ok==1) return ; + for(auto i:v[nod]) + { + if(dist[i.first]==0) + { + dist[i.first]=dist[nod]+i.second; + dfs(i.first); + } + } +} +int main() +{ + int i,j,x,y,d; + fin>>N>>M>>X>>Y; + for(i=1;i<=M;i++) + { + fin>>x>>y>>d; + if(x +#include + +#define MAXN 100002 + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct two{ + int node, c; +}; + +vector G[MAXN]; + +int cost[MAXN], x, y, n, m, aux, l, ok = 0; + +inline void DFS(int nod) { + if (nod == y) { + fout << cost[nod] - 1; + ok = 1; + return; + } + + if (ok) + return; + + for (vector :: iterator i = G[nod].begin(); i != G[nod].end(); i++) { + if (cost[(*i).node] == 0) { + cost[(*i).node] = cost[nod] + (*i).c; + + DFS((*i).node); + } + } +} + +inline void Read() { + int a, b, cc; + + fin >> n >> m >> x >> y; + + for (int i = 1; i <= m; i++) { + fin >> a >> b >> cc; + + if (a > b) { + G[a].push_back({b, -cc}); + G[b].push_back({a, cc}); + } + else + G[a].push_back({b, cc}), + G[b].push_back({a, -cc}); + } + + cost[x] = 1; + + DFS(x); +} + +int main () { + Read(); + + return 0; +} diff --git a/villages/dfs/256b75be-6a5f-4aab-9164-ae2991c2fdb3.cpp b/villages/dfs/256b75be-6a5f-4aab-9164-ae2991c2fdb3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dbecdda6999c9e73e0c353f4a3bb17e81d4184d3 --- /dev/null +++ b/villages/dfs/256b75be-6a5f-4aab-9164-ae2991c2fdb3.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#define Nmax 30005 + +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector >lista[Nmax]; +int viz[Nmax]; +int sol[Nmax]; +int n,m; +int X,Y; + +void citire() +{ + int x,y,z,zprim; + + fin>>n>>m>>X>>Y; + + while(m--) + { + fin>>x>>y>>z; + zprim=z*-1; + + lista[x].push_back(make_pair(y,z)); + lista[y].push_back(make_pair(x,zprim)); + + } + + fin.close(); +} + + +void DFS(int a) +{ viz[a]=1; + if(!sol[Y]) + for(int i=0;i +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +const int N_MAX = 30000 + 5; + +int x, y, n,m; + +vector > vec[N_MAX]; + +bitset viz; + +void dfs(int nod, int sum){ + + if(viz[nod]) + return; + + viz[nod] = true; + + + if(nod == y){ + fout << sum; + exit(0); + }else{ + for(auto v : vec[nod]){ + dfs(v.first, sum + v.second); + } + } + +} + +int main() +{ + + fin >> n >> m >> x >> y; + + while(m--){ + int a,b,d; + fin >> a >> b >> d; + if(a > b) + swap(a,b); + vec[a].push_back({b,d}); + vec[b].push_back({a,-d}); + } + + dfs(x, 0); + + return 0; +} diff --git a/villages/dfs/28b9d88c-40ad-11ec-a0a1-dc7196033e92.cpp b/villages/dfs/28b9d88c-40ad-11ec-a0a1-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796ec072ff3d1ee4680a3d86ec29cf9aa9354bd5 --- /dev/null +++ b/villages/dfs/28b9d88c-40ad-11ec-a0a1-dc7196033e92.cpp @@ -0,0 +1,91 @@ +#include +#include +#define MAXV 30000 +#define MAXE 100024 +#define MASK 32767 +#define MAXBUF 100000 +struct Edge +{ + int MIN,MAX,COST; +}; +void read(int& n); +void dfs(int nod,int dist); +FILE*fin,*fout; +char buf[MAXBUF+1]; +int pos=0; +Edge edges[MAXE+1]; +bool visited[MAXE+1]; +std::vector neighbors[MAXV+1]; +int V,E,X,Y; +int ans; +int main() +{ + fin=fopen("sate.in","r"); + fout=fopen("sate.out","w"); + read(V); + read(E); + read(X); + read(Y); + for(int i=1;i<=E;i++) + { + int left,right,dist; + read(left); + read(right); + read(dist); + neighbors[left].push_back(i); + neighbors[right].push_back(i); + edges[i].MIN=left; + edges[i].MAX=right; + edges[i].COST=dist; + } + dfs(X,0); + fprintf(fout,"%d",ans); + fclose(fin); + fclose(fout); +} +void dfs(int nod,int dist) +{ + if(nod==Y) + { + ans=dist; + } + for(int i=0;i buf[pos] || '9' < buf[pos]) + { + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } + while('0'<=buf[pos] && buf[pos]<='9') + { + n=n*10+buf[pos]-'0'; + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } +} diff --git a/villages/dfs/28ef91b9-40ad-11ec-866f-dc7196033e92.cpp b/villages/dfs/28ef91b9-40ad-11ec-866f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f43742de5739112de1c42f955efb15170657b5a --- /dev/null +++ b/villages/dfs/28ef91b9-40ad-11ec-866f-dc7196033e92.cpp @@ -0,0 +1,42 @@ +#include +#include + +FILE *fin = fopen("sate.in", "r"); +FILE *fout = fopen("sate.out", "w"); + +#define maxn 120000 +#define ll long long + +std::vector> v[maxn + 1]; +int viz[maxn + 1]; +int a, b; +ll dp[maxn + 1]; +void dfs(int t) { + viz[t] = 1; + int p, c; + for (auto it : v[t]) { + p = it.first; + c = it.second; + if (viz[p] != 1) { + dp[p] = dp[t] + c; + dfs(p); + } + + } +} + +int main() +{ + + int n, m, x, y, c; + + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for (int i = 1; i <= m; i++) { + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back (std::make_pair(y, c)); + v[y].push_back (std::make_pair(x, -c)); + } + dfs(a); + fprintf(fout, "%lld", dp[b]); + return 0; +} diff --git a/villages/dfs/2b322a27-ca86-4e73-86cd-59c34f26e822.cpp b/villages/dfs/2b322a27-ca86-4e73-86cd-59c34f26e822.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b91cd025a70d9adbb8b7069dc2774e7a6c542db --- /dev/null +++ b/villages/dfs/2b322a27-ca86-4e73-86cd-59c34f26e822.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +typedef long long ll; +const int lim=3e4+5; +vector > vec[lim]; +int n,m,st,dr,x,y; ll d; +ll dist[lim]; +bool ok[lim]; +void df(int nod) +{ + ok[nod]=true; + for(auto x:vec[nod]) + if(!ok[x.first]) + dist[x.first]=dist[nod]+x.second, + df(x.first); +} +int main() +{ + in>>n>>m>>st>>dr; + for(int i=1;i<=m;++i) + { + in>>x>>y>>d; + vec[x].push_back({y,d}); + vec[y].push_back({x,-d}); + } + df(st); + out< +#include +#define MAXV 30000 +#define MAXE 100024 +#define MASK 32767 +#define MAXBUF 100000 +struct Edge +{ + int MIN,MAX,COST; +}; +void read(int& n); +void dfs(int nod,int dist); +FILE*fin,*fout; +char buf[MAXBUF+1]; +int pos=0; +Edge edges[MAXE+1]; +bool visited[MAXE+1]; +std::vector neighbors[MAXV+1]; +int V,E,X,Y; +int ans; +int main() +{ + fin=fopen("sate.in","r"); + fout=fopen("sate.out","w"); + read(V); + read(E); + read(X); + read(Y); + for(int i=1;i<=E;i++) + { + int left,right,dist; + read(left); + read(right); + read(dist); + neighbors[left].push_back(i); + neighbors[right].push_back(i); + edges[i].MIN=left; + edges[i].MAX=right; + edges[i].COST=dist; + } + dfs(X,0); + fprintf(fout,"%d",ans); + fclose(fin); + fclose(fout); +} +void dfs(int nod,int dist) +{ + if(nod==Y) + { + ans=dist; + } + for(int i=0;i buf[pos] || '9' < buf[pos]) + { + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } + while('0'<=buf[pos] && buf[pos]<='9') + { + n=n*10+buf[pos]-'0'; + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } +} diff --git a/villages/dfs/2cff46d8-40ad-11ec-b3c2-dc7196033e92.cpp b/villages/dfs/2cff46d8-40ad-11ec-b3c2-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ed2effb1c3fb08d6a03cc2db6583316850cee41b --- /dev/null +++ b/villages/dfs/2cff46d8-40ad-11ec-b3c2-dc7196033e92.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +struct dist +{ + int s,d; +}; + +vector v[30001]; +bool visit[30001]; + +int findDist(int x,int y,int d = 0) +{ + //cout<>n>>m>>x>>y; + + int i; + for(i=0; i>a>>b>>l; + + dist dst; + dst.d = l; + dst.s = b; + v[a].push_back(dst); + dst.s = a; + v[b].push_back(dst); + } + + out< +using namespace std; +ifstream f("sate.in");ofstream g("sate.out"); +vector > G[30005];int d[30005], c[30005]; +void dfs(int u){ +int v, j;c[u] = 1; +for(j=0;j>n>>m>>s>>t; +for(i=1;i<=m;i++){ +f>>x>>y>>z;G[x].push_back({y,z});G[y].push_back({x,-z});} +dfs(s);g< +#define N 30100 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > V[N]; +int n, m, c, a , b, x, y; +int viz[N]; + +void DFS(int x) { + + for (int i=0; i>n>>m>>x>>y; + if(y>a>>b>>c; + V[a].push_back(make_pair(b,c)); + V[b].push_back(make_pair(a,-c)); + } + DFS(x); + fout << viz[y]; + return 0; +} + diff --git a/villages/dfs/32224926-40ad-11ec-9705-dc7196033e92.cpp b/villages/dfs/32224926-40ad-11ec-9705-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1bc02aeafedf8936e8a3d9511fa0bbb017ecf2ad --- /dev/null +++ b/villages/dfs/32224926-40ad-11ec-9705-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include +#define Nmax 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +bool Use[Nmax]; +int D[Nmax],N,M,X,Y; + +vector < pair > G[Nmax]; + +void Read() +{ + fin>>N>>M>>X>>Y; + while(M--) + { + int i,j,D; + fin>>i>>j>>D; + G[i].push_back(make_pair(j,D)); + G[j].push_back(make_pair(i,D)); + } +} + +void DFS(int Node) +{ + Use[Node]=1; + for(int i=0;i<(int)G[Node].size();++i) + { + int Vecin=G[Node][i].first; + int Dist=G[Node][i].second; + + if(!Use[Vecin]) + { + if(Vecin>Node) D[Vecin]=D[Node]+Dist; + else D[Vecin]=D[Node]-Dist; + DFS(Vecin); + } + } +} + +void Print() +{ + DFS(X); + fout< +using namespace std; +#define pb push_back + +int n,m,X,Y,D[30010]; +vector > V[30010]; +bool u[30010]; + +void dfs(int q, int c){ + D[q]=c; + u[q]=true; + for (int i=0; i>n>>m>>X>>Y; + for (int i=1; i<=m; i++){ + int x,y,c; + cin>>x>>y>>c; + V[x].pb(make_pair(y,c)); + V[y].pb(make_pair(x,c)); + } + dfs(X,0); + cout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y; +int nod1,nod2; +vector < int > knots[30005]; +vector < int > dt[30005]; +int viz[30005]; +int dist[30005]; +int distnc; + +void graph(int nod, int distnc) { + dist[nod]= distnc; + viz[nod]=1; + for (int k=0;k> n >> m >> nod1 >> nod2; + for (int i=1;i<=m;i++) { + f >> x >> y >> distnc; + knots[x].push_back(y); + knots[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graph(nod1,0); + g << dist[nod2]; + return 0; +} diff --git a/villages/dfs/367486bf-8b28-4d09-b34f-4eee8731663f.cpp b/villages/dfs/367486bf-8b28-4d09-b34f-4eee8731663f.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0226b1d78ed487d24ba0ad29616e9311ca5824a --- /dev/null +++ b/villages/dfs/367486bf-8b28-4d09-b34f-4eee8731663f.cpp @@ -0,0 +1,103 @@ +#pragma GCC optimize("Ofast") +#include +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + register node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, X, Y; + register int M; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + register int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/3680617d-aa8b-4fd1-b78c-7e23399a3208.cpp b/villages/dfs/3680617d-aa8b-4fd1-b78c-7e23399a3208.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7211fa1f73f394346fdb112ede3451798f7bccb7 --- /dev/null +++ b/villages/dfs/3680617d-aa8b-4fd1-b78c-7e23399a3208.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +#define pb push_back + +int n,m,X,Y,D[30010]; +vector > V[30010]; +bool u[30010]; + +void dfs(int q, int c){ + D[q]=c; + u[q]=true; + for (int i=0; i>n>>m>>X>>Y; + for (int i=1; i<=m; i++){ + int x,y,c; + cin>>x>>y>>c; + V[x].pb(make_pair(y,c)); + V[y].pb(make_pair(x,c)); + } + dfs(X,0); + cout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/41603024-956d-4f90-acf2-100dfe5cb6a7.cpp b/villages/dfs/41603024-956d-4f90-acf2-100dfe5cb6a7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..796ec072ff3d1ee4680a3d86ec29cf9aa9354bd5 --- /dev/null +++ b/villages/dfs/41603024-956d-4f90-acf2-100dfe5cb6a7.cpp @@ -0,0 +1,91 @@ +#include +#include +#define MAXV 30000 +#define MAXE 100024 +#define MASK 32767 +#define MAXBUF 100000 +struct Edge +{ + int MIN,MAX,COST; +}; +void read(int& n); +void dfs(int nod,int dist); +FILE*fin,*fout; +char buf[MAXBUF+1]; +int pos=0; +Edge edges[MAXE+1]; +bool visited[MAXE+1]; +std::vector neighbors[MAXV+1]; +int V,E,X,Y; +int ans; +int main() +{ + fin=fopen("sate.in","r"); + fout=fopen("sate.out","w"); + read(V); + read(E); + read(X); + read(Y); + for(int i=1;i<=E;i++) + { + int left,right,dist; + read(left); + read(right); + read(dist); + neighbors[left].push_back(i); + neighbors[right].push_back(i); + edges[i].MIN=left; + edges[i].MAX=right; + edges[i].COST=dist; + } + dfs(X,0); + fprintf(fout,"%d",ans); + fclose(fin); + fclose(fout); +} +void dfs(int nod,int dist) +{ + if(nod==Y) + { + ans=dist; + } + for(int i=0;i buf[pos] || '9' < buf[pos]) + { + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } + while('0'<=buf[pos] && buf[pos]<='9') + { + n=n*10+buf[pos]-'0'; + pos++; + if(pos==MAXBUF) + { + fread(buf,1,MAXBUF,fin); + pos=0; + } + } +} diff --git a/villages/dfs/425ea66d-695e-448e-8f7a-c6c016acf5b0.cpp b/villages/dfs/425ea66d-695e-448e-8f7a-c6c016acf5b0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e983bb137ecda6e22812ae9527cbd4101778ed --- /dev/null +++ b/villages/dfs/425ea66d-695e-448e-8f7a-c6c016acf5b0.cpp @@ -0,0 +1,45 @@ +#include +#include +const int MAX_N = 30000; +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct Muchie { + int v, c; +}; +Muchie x, y; +long long n, m, s, ok; +bool vizitat[MAX_N + 1]; +int dp[MAX_N + 1]; + +vector vecini[MAX_N + 1]; + +void dfs(Muchie u) { + vizitat[u.v] = 1; + if (ok == 1) + return; + if(u.v == y.v) { + ok = 1; + return; + } + for(auto v : vecini[u.v]) + if(!vizitat[v.v]) { + dp[v.v]=dp[u.v]+v.c; + dfs(v); + } +} +int main() +{ + fin >> n >> m >> x.v >> y.v; + for (int i = 1; i <= m; i++) { + int u, w, c; + fin >> u >> w >> c; + vecini[u].push_back({w, c}); + vecini[w].push_back({u, -c}); + } + dfs(x); + fout << dp[y.v] << '\n'; + return 0; +} diff --git a/villages/dfs/4423e808-d1ec-40e7-a016-bd4c582271c2.cpp b/villages/dfs/4423e808-d1ec-40e7-a016-bd4c582271c2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..620bb1bff377d7d7942dc0960acdb78ee5e25d49 --- /dev/null +++ b/villages/dfs/4423e808-d1ec-40e7-a016-bd4c582271c2.cpp @@ -0,0 +1,111 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/4e236db3-85cb-40d2-b812-b974289fa531.cpp b/villages/dfs/4e236db3-85cb-40d2-b812-b974289fa531.cpp new file mode 100644 index 0000000000000000000000000000000000000000..272f0e80a4ace0cddf2201135507cbbe79fc82e2 --- /dev/null +++ b/villages/dfs/4e236db3-85cb-40d2-b812-b974289fa531.cpp @@ -0,0 +1,106 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/50431d3c-48d7-4d36-94f7-03d73b4ce590.cpp b/villages/dfs/50431d3c-48d7-4d36-94f7-03d73b4ce590.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1819b649b48b2a49279e14ec29be6141d3f38f27 --- /dev/null +++ b/villages/dfs/50431d3c-48d7-4d36-94f7-03d73b4ce590.cpp @@ -0,0 +1,56 @@ +#include +#include +using namespace std; +#define nmax 30005 + + +vector>Q[nmax]; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y,dist[nmax]; +bool passed=0; + +void read() +{ + f>>n>>m>>x>>y; + for (int i=1; i<=m; ++i) + { + int r1,r2,r3; + f>>r1>>r2>>r3; + Q[r1].push_back({r2,r3}); + Q[r2].push_back({r1,-r3}); + } + +} + +void dfs(int nod) +{ + if (nod==y) + passed=true; + if (passed) + return; + for (auto w:Q[nod]) + { + if (!dist[w.first]) + { + dist[w.first]=dist[nod]+w.second; + dfs(w.first); + } + } +} + +void solve() +{ + dist[x]=1; + dfs(x); + g< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/55574556-b890-45e6-9fbc-e095e94c4447.cpp b/villages/dfs/55574556-b890-45e6-9fbc-e095e94c4447.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6d1fe36278c68d1f20282df3824f686505f77b57 --- /dev/null +++ b/villages/dfs/55574556-b890-45e6-9fbc-e095e94c4447.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair < int, int > > graf[30005]; +bool vizitat[30005]; +int n,m,X,y,ok=1; +long int s; +void citire() +{ + fin>>n>>m>>X>>y; + for(int i=1; i<=m; i++) + { + int a,b,d; + fin>>a>>b>>d; + graf[a].push_back(make_pair(b,d)); + graf[b].push_back(make_pair(a,-d)); + } +} +void drum(int x,int s) +{ + + if(x==y) + { + fout< +#include +#include +using namespace std; + +typedef pair edge; + +int n, m, s, d; +bool viz[30001], done; +vector g[30001]; + +void citire(); +void afisare(int); +void dfs(int, int); + + +int main() { + citire(); + + dfs(s, 0); + + return 0; +} + + +void dfs(int x, int cost) { + viz[x] = true; + if (x == d) { + afisare(cost); + done = true; + return; + } + + for (int i = 0; i < g[x].size(); ++i) { + int y = g[x][i].second; + if (viz[y]) continue; + dfs(y, cost + g[x][i].first); + if (done) return; + } +} + +void afisare(int cost) { + ofstream out("sate.out"); + out << cost; + out.close(); +} + +void citire() { + ifstream in("sate.in"); + in >> n >> m >> s >> d; + + for (int i = 1, x, y, c; i <= m; ++i) { + in >> x >> y >> c; + g[x].push_back(make_pair(c, y)); + g[y].push_back(make_pair(-c, x)); + } + + in.close(); +} diff --git a/villages/dfs/5a891476-43eb-4db5-aeaf-da142c609864.cpp b/villages/dfs/5a891476-43eb-4db5-aeaf-da142c609864.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/5a891476-43eb-4db5-aeaf-da142c609864.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#define mp make_pair +#define pb push_back +#define st first +#define nd second +#define pii pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y, a, b, c, d[30100]; bool viz[30100]; +vector v[100100]; + +void dfs(int nod){ + for(auto it : v[nod]){ + if(!viz[it.st]){ + viz[it.st] = 1; + d[it.st] = d[nod] + it.nd; + if(it.st == y) return; + dfs(it.st); + } + } +} + +int main(){ + in >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + in >> a >> b >> c; + v[a].pb(mp(b, c)); + v[b].pb(mp(a, -c)); + } + dfs(x); + return out << d[y], 0; +} diff --git a/villages/dfs/5bfc1e25-40ad-11ec-87a4-dc7196033e92.cpp b/villages/dfs/5bfc1e25-40ad-11ec-87a4-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/5bfc1e25-40ad-11ec-87a4-dc7196033e92.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include +#include + +using namespace std; + + +ifstream in("sate.in"); +ofstream out("sate.out"); + + +const int MAX = 30005; + + +vector < pair < int , int > > myVector[MAX]; + +bool beenThere[MAX]; + +int N,M,Plec,Ajung; +int D[MAX]; + +void Read() +{ + in >> N >> M >> Plec >> Ajung; + + for ( int i = 1; i <= M ; ++i) + { + int x,y,cost; + int costnegativ; + in >> x >> y >> cost; + costnegativ = cost*-1; + + myVector[x].push_back(make_pair(y,cost)); + myVector[y].push_back(make_pair(x,costnegativ)); + } +} + +void DFS(int nod) +{ + beenThere[nod] = true; + + for ( size_t k = 0 ; k < myVector[nod].size(); k++) + { + int Vecin = myVector[nod][k].first; + int cost = myVector[nod][k].second; + + if(beenThere[Vecin] == false) + { + + D[Vecin] = D[nod] + cost; + + + DFS(Vecin); + } + } + +} + + +void Rezolv() +{ + + + DFS(Plec); + + out << D[Ajung]; + + + +} + + +int main() +{ + Read(); + Rezolv(); + + return 0; +} diff --git a/villages/dfs/5d5cfc86-40ad-11ec-b38a-dc7196033e92.cpp b/villages/dfs/5d5cfc86-40ad-11ec-b38a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053e18fa5562bb95ab91a2d3805ab4b71c318226 --- /dev/null +++ b/villages/dfs/5d5cfc86-40ad-11ec-b38a-dc7196033e92.cpp @@ -0,0 +1,48 @@ +#include +#define IOS ios_base::sync_with_stdio(0); +#define tie cin.tie(0); +#define mp make_pair +#define ll long long +#define PII pair +#define PLL pair +#define inf 1000000000 +#define zeros(x) ( (x ^ (x - 1)) & x ) + +using namespace std; + +int n, m, x, y, z, in, sf; + +vector < PII > V[100100]; +int cost[100100]; + +void dfs(int x) +{ + for (auto it : V[x]) + { + if (!cost[it.first]) + { + cost[it.first] = cost[x] + it.second; + if (it.first == sf) break; + dfs(it.first); + } + } +} + + +int main(){ + IOS tie + ifstream cin("sate.in"); + ofstream cout("sate.out"); + cin >> n >> m >> in >> sf; + if (in > sf) swap(in, sf); + for (int i = 1; i <= m; i++) + { + cin >> x >> y >> z; + V[x].push_back(mp(y, z)); + V[y].push_back(mp(x, -z)); + } + dfs(in); + cout << cost[sf]; + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + return 0; +} \ No newline at end of file diff --git a/villages/dfs/5f73126f-1b77-492c-9881-492144098473.cpp b/villages/dfs/5f73126f-1b77-492c-9881-492144098473.cpp new file mode 100644 index 0000000000000000000000000000000000000000..709ac33cf1e1ab7f44cba5b4474904b083a1ebbf --- /dev/null +++ b/villages/dfs/5f73126f-1b77-492c-9881-492144098473.cpp @@ -0,0 +1,42 @@ +#include +#include + +#define N 30005 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +int dist[N]; +vector > G[N]; +bool viz[N]; + +inline void DFS(const int &nod) +{ + viz[nod] = 1; + for(auto i : G[nod]) + if(!viz[i.first]) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } +} + +int main() +{ + int n, m, X, Y; + fin >> n >> m >> X >> Y; + for(int i = 1, x, y, c; i <= m; ++i) + { + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, -c)); + } + fin.close(); + + DFS(X); + fout << dist[Y]; + fout.close(); + return 0; +} diff --git a/villages/dfs/5fbd6065-40ad-11ec-b2d3-dc7196033e92.cpp b/villages/dfs/5fbd6065-40ad-11ec-b2d3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/5fbd6065-40ad-11ec-b2d3-dc7196033e92.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back({b,cost}); + A[b].push_back({a,-cost}); + } + + DFS(x); + fout< +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b,cost)); + A[b].push_back(make_pair(a,-cost)); + } + + DFS(x); + fout< +#include +#include +#define Nmax 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +bool Use[Nmax]; +int D[Nmax],N,M,X,Y; + +vector < pair > G[Nmax]; + +void Read() +{ + fin>>N>>M>>X>>Y; + while(M--) + { + int i,j,D; + fin>>i>>j>>D; + G[i].push_back(make_pair(j,D)); + G[j].push_back(make_pair(i,D)); + } +} + +void DFS(int Node) +{ + Use[Node]=1; + for(int i=0;i<(int)G[Node].size();++i) + { + int Vecin=G[Node][i].first; + int Dist=G[Node][i].second; + + if(!Use[Vecin]) + { + if(Vecin>Node) D[Vecin]=D[Node]+Dist; + else D[Vecin]=D[Node]-Dist; + DFS(Vecin); + } + } +} + +void Print() +{ + DFS(X); + fout< +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include + +using namespace std; + +ifstream fin("sate.in") ; +ofstream fout("sate.out"); + +struct str { + int x, y; +}; + +const int nmax=30000; +vector g[nmax+1]; +int sol[nmax+1]; + +void dfs (int x) { + for (vector ::iterator it = g[x].begin(); it != g[x].end(); ++ it ) { + if (sol[it->x]==0) { + sol[it->x]=sol[x]+it->y; + dfs(it->x); + } + } +} + +int main () { + int n, m, a, b; + fin>>n>>m>>a>>b; + + for (int i=1; i<=m; i++) { + int x, y, c; + fin>>x>>y>>c; + str aux; + aux.x=y; + aux.y=c; + g[x].push_back(aux); + aux.x=x; + aux.y=-c; + g[y].push_back(aux); + } + + sol[a]=3e7; + dfs(a); + + fout< +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + pii *it = &muchii[itt]; + if (dst[it->first] != 0x3F3F3F3F) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/6713f291-c5ec-48e6-8438-3e3a3e7c7e76.cpp b/villages/dfs/6713f291-c5ec-48e6-8438-3e3a3e7c7e76.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/6713f291-c5ec-48e6-8438-3e3a3e7c7e76.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#define N 30100 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > V[N]; +int n, m, c, a , b, x, y; +int viz[N]; + +void DFS(int x) { + + for (int i=0; i>n>>m>>x>>y; + if(y>a>>b>>c; + V[a].push_back(make_pair(b,c)); + V[b].push_back(make_pair(a,-c)); + } + DFS(x); + fout << viz[y]; + return 0; +} + diff --git a/villages/dfs/69da32e5-d77f-4ff1-abc1-747037a5dbfb.cpp b/villages/dfs/69da32e5-d77f-4ff1-abc1-747037a5dbfb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..854951e5ee2655c734f877fc8682b419eeac573c --- /dev/null +++ b/villages/dfs/69da32e5-d77f-4ff1-abc1-747037a5dbfb.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int seen[40000], d = 0, y; + +struct drumuri +{ + int vecin; + int dis; +}; + + + +void DFS(int nod, vector v[40000]) +{ + if(nod == y) + { + out< v[40000]; + int n, m, a, b, i, x, dist; + in>>n>>m>>x>>y; + for(i = 0; i>a>>b>>dist; + + drumuri dr = {b, dist}; + v[a].push_back(dr); + + drumuri dr2 = {a, -dist}; + v[b].push_back(dr2); + } + DFS(x, v); + return 0; +} diff --git a/villages/dfs/6a4217cc-2f55-40f0-9557-1733f17967c5.cpp b/villages/dfs/6a4217cc-2f55-40f0-9557-1733f17967c5.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e983bb137ecda6e22812ae9527cbd4101778ed --- /dev/null +++ b/villages/dfs/6a4217cc-2f55-40f0-9557-1733f17967c5.cpp @@ -0,0 +1,45 @@ +#include +#include +const int MAX_N = 30000; +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct Muchie { + int v, c; +}; +Muchie x, y; +long long n, m, s, ok; +bool vizitat[MAX_N + 1]; +int dp[MAX_N + 1]; + +vector vecini[MAX_N + 1]; + +void dfs(Muchie u) { + vizitat[u.v] = 1; + if (ok == 1) + return; + if(u.v == y.v) { + ok = 1; + return; + } + for(auto v : vecini[u.v]) + if(!vizitat[v.v]) { + dp[v.v]=dp[u.v]+v.c; + dfs(v); + } +} +int main() +{ + fin >> n >> m >> x.v >> y.v; + for (int i = 1; i <= m; i++) { + int u, w, c; + fin >> u >> w >> c; + vecini[u].push_back({w, c}); + vecini[w].push_back({u, -c}); + } + dfs(x); + fout << dp[y.v] << '\n'; + return 0; +} diff --git a/villages/dfs/6b347cfe-c21e-4300-b837-cf2636fb1ce8.cpp b/villages/dfs/6b347cfe-c21e-4300-b837-cf2636fb1ce8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23ab8f778e700acba948149d98ab126a663637c7 --- /dev/null +++ b/villages/dfs/6b347cfe-c21e-4300-b837-cf2636fb1ce8.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +const int N_MAX = 30000 + 5; + +int x, y, n,m; + +vector > vec[N_MAX]; + +bitset viz; + +void dfs(int nod, int sum){ + + if(viz[nod]) + return; + + viz[nod] = true; + + + if(nod == y){ + fout << sum; + exit(0); + }else{ + for(auto v : vec[nod]){ + dfs(v.first, sum + v.second); + } + } + +} + +int main() +{ + + fin >> n >> m >> x >> y; + + while(m--){ + int a,b,d; + fin >> a >> b >> d; + if(a > b) + swap(a,b); + vec[a].push_back({b,d}); + vec[b].push_back({a,-d}); + } + + dfs(x, 0); + + return 0; +} diff --git a/villages/dfs/6bcd055f-f947-4e31-9a93-6fbf45aa423b.cpp b/villages/dfs/6bcd055f-f947-4e31-9a93-6fbf45aa423b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f0226b1d78ed487d24ba0ad29616e9311ca5824a --- /dev/null +++ b/villages/dfs/6bcd055f-f947-4e31-9a93-6fbf45aa423b.cpp @@ -0,0 +1,103 @@ +#pragma GCC optimize("Ofast") +#include +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + register node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, X, Y; + register int M; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + register int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/6c3beeea-eba9-44f7-85a1-118ddf4328d9.cpp b/villages/dfs/6c3beeea-eba9-44f7-85a1-118ddf4328d9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/6c3beeea-eba9-44f7-85a1-118ddf4328d9.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/71808c24-c8a9-42e9-9945-0a6f69456d81.cpp b/villages/dfs/71808c24-c8a9-42e9-9945-0a6f69456d81.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/71808c24-c8a9-42e9-9945-0a6f69456d81.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include + +using namespace std; + +vector < pair > v[30001]; +bool viz[30001]; +char s[1001]; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n,m,st,fin; + +void read() +{ in>>n>>m>>st>>fin; + in.get(); + int x, y, c, nr, cnt; + for (int k = 1; k <= m; ++k) + { cnt = 0; + in.getline(s, 1001); + for (int i = 0; s[i] != '\0'; ++i) { + if (s[i] >= '0' && s[i] <= '9') { + nr = 0; + cnt++; + while (s[i] >= '0' && s[i] <= '9') { + nr = nr * 10 + s[i] - '0'; + ++i; + } + if (cnt == 1) x = nr; + if (cnt == 2) y = nr; + if (cnt == 3) c = nr; + } + } + v[x].push_back({y,c}); + v[y].push_back({x,c}); + } +} + +void drum(int nod,int dist) +{ + viz[nod]=1; + if (nod==fin) + { + out<nod) + drum(nod1,dist+d); + } + } +} + +int main() +{ + read(); + drum(st,0); + return 0; +} diff --git a/villages/dfs/73b88d6e-5690-4880-bea9-d9aaa7710423.cpp b/villages/dfs/73b88d6e-5690-4880-bea9-d9aaa7710423.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a5c5a7e5e19921b81eb49b028f281d50d1ec6f94 --- /dev/null +++ b/villages/dfs/73b88d6e-5690-4880-bea9-d9aaa7710423.cpp @@ -0,0 +1,53 @@ +#include +#define ll long long +#define pb push_back +#define pf push_front +#define nmax 1000000 +#define MMAX 100000 +#define inf 1e18 +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +bool viz[30005]; +int dist[30005]; +vector > adj[30005]; +int n,m,x,y; +void dfs(int node,int d) +{ + viz[node]=1; + dist[node]=d; + for(auto x:adj[node]) + { + if(viz[x.first]==0) + { + if(node>n>>m>>x>>y; +if(x>y) swap(x,y); +for(int i=1;i<=m;i++) +{ + int a,b,c; + f>>a>>b>>c; + adj[a].pb({b,c}); + adj[b].pb({a,c}); +} +dfs(x,0); +g< +#include +#define nod first +#define dist second +using namespace std; +typedef pair ii; +const int NMAX = 30000; +vector G[NMAX + 5]; +int d[NMAX + 5], viz[NMAX + 5]; +void dfs(int u) +{ + int v, j; + viz[u] = 1; + for(j = 0 ; j < G[u].size() ; j ++) + { + v = G[u][j].nod; + if(!viz[v]) + { + d[v] = d[u] + G[u][j].dist; + dfs(v); + } + } +} +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + int n, m, i, x, y, z, s, t; + scanf("%d%d%d%d", &n, &m, &s, &t); + for(i = 1 ; i <= m ; i ++) + { + scanf("%d%d%d", &x, &y, &z); + G[x].push_back({y, z}); + G[y].push_back({x, -z}); + } + dfs(s); + printf("%d\n", d[t] - d[s]); + return 0; +} diff --git a/villages/dfs/77929bbd-4f44-41f9-9393-c0fc83b7ea55.cpp b/villages/dfs/77929bbd-4f44-41f9-9393-c0fc83b7ea55.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/77929bbd-4f44-41f9-9393-c0fc83b7ea55.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#define STOP fout.close(); exit(0); +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +///************************ +const int NMAX = 3e4 + 3; +int n, m, x, y, dis[NMAX];//dis[i]-distance from x to i; +#define fi first +#define se second +vector> edges[NMAX]; + +void read() { + fin >> n >> m >> x >> y; + for (int a, b, c, i = 0; i < m; i++) { + fin >> a >> b >> c; + edges[a].push_back(make_pair(b, c)); + edges[b].push_back(make_pair(a, c)); + } +} + +void dfs(int node) { + for (auto it : edges[node]) { + if (dis[it.fi] == -1) { + if (node < it.fi) + dis[it.fi] = dis[node] + it.se; + else + dis[it.fi] = dis[node] - it.se; + dfs(it.fi); + } + } +} + +int main() { + read(); + fill(dis + 1, dis + n + 1, -1); + dis[x] = 0; + dfs(x); + fout << dis[y] << endl; + STOP +} diff --git a/villages/dfs/7b235fef-e5d8-4c7f-bc0c-d8ea69a55957.cpp b/villages/dfs/7b235fef-e5d8-4c7f-bc0c-d8ea69a55957.cpp new file mode 100644 index 0000000000000000000000000000000000000000..709ac33cf1e1ab7f44cba5b4474904b083a1ebbf --- /dev/null +++ b/villages/dfs/7b235fef-e5d8-4c7f-bc0c-d8ea69a55957.cpp @@ -0,0 +1,42 @@ +#include +#include + +#define N 30005 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +int dist[N]; +vector > G[N]; +bool viz[N]; + +inline void DFS(const int &nod) +{ + viz[nod] = 1; + for(auto i : G[nod]) + if(!viz[i.first]) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } +} + +int main() +{ + int n, m, X, Y; + fin >> n >> m >> X >> Y; + for(int i = 1, x, y, c; i <= m; ++i) + { + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, -c)); + } + fin.close(); + + DFS(X); + fout << dist[Y]; + fout.close(); + return 0; +} diff --git a/villages/dfs/7d06dfc9-0751-4852-a731-49ddc08a22ca.cpp b/villages/dfs/7d06dfc9-0751-4852-a731-49ddc08a22ca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..994ae596bbf1f5b149d1315d861217acc69e9b4a --- /dev/null +++ b/villages/dfs/7d06dfc9-0751-4852-a731-49ddc08a22ca.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +int n,m; +int X,Y; +vector >vec[30005]; +int parcurs[30005]; +int sol[30005]; +void componenta_conexa(int x,int value) +{ + sol[x]=value; + for(int i=0;i<=vec[x].size()-1;i++) + { + if(parcurs[vec[x][i].first]==0) + { + parcurs[vec[x][i].first]=1; + ///daca in vector distanta paan la nodul vec[x][i] este mai mica decat una calculata + /// se scade altfe se insumeaza + if(x>vec[x][i].first) + componenta_conexa(vec[x][i].first,value-vec[x][i].second); + else + componenta_conexa(vec[x][i].first,value+vec[x][i].second); + } + } +} +int main() +{ + fi>>n>>m; + fi>>X>>Y; + int x,y,dist; + for(int i=1;i<=m;i++) + { + fi>>x>>y>>dist; + vec[x].push_back(make_pair(y,dist)); + vec[y].push_back(make_pair(x,dist)); + } + parcurs[X]=1; + componenta_conexa(X,0); + fo< +#include + +#define MAXN 100002 + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct two{ + int node, c; +}; + +vector G[MAXN]; + +int cost[MAXN], x, y, n, m, aux, l, ok = 0; + +inline void DFS(int nod) { + if (nod == y) { + fout << cost[nod] - 1; + ok = 1; + return; + } + + if (ok) + return; + + for (vector :: iterator i = G[nod].begin(); i != G[nod].end(); i++) { + if (cost[(*i).node] == 0) { + cost[(*i).node] = cost[nod] + (*i).c; + + DFS((*i).node); + } + } +} + +inline void Read() { + int a, b, cc; + + fin >> n >> m >> x >> y; + + for (int i = 1; i <= m; i++) { + fin >> a >> b >> cc; + + if (a > b) { + G[a].push_back({b, -cc}); + G[b].push_back({a, cc}); + } + else + G[a].push_back({b, cc}), + G[b].push_back({a, -cc}); + } + + cost[x] = 1; + + DFS(x); +} + +int main () { + Read(); + + return 0; +} diff --git a/villages/dfs/8007db38-125e-4991-88e5-5b93e57f5464.cpp b/villages/dfs/8007db38-125e-4991-88e5-5b93e57f5464.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46d7ea4b254f6eece68ec00ef3826d27ca1a40ca --- /dev/null +++ b/villages/dfs/8007db38-125e-4991-88e5-5b93e57f5464.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +using namespace std; + +typedef pair edge; + +int n, m, s, d; +bool viz[30001], done; +vector g[30001]; + +void citire(); +void afisare(int); +void dfs(int, int); + + +int main() { + citire(); + + dfs(s, 0); + + return 0; +} + + +void dfs(int x, int cost) { + viz[x] = true; + if (x == d) { + afisare(cost); + done = true; + return; + } + + for (int i = 0; i < g[x].size(); ++i) { + int y = g[x][i].second; + if (viz[y]) continue; + dfs(y, cost + g[x][i].first); + if (done) return; + } +} + +void afisare(int cost) { + ofstream out("sate.out"); + out << cost; + out.close(); +} + +void citire() { + ifstream in("sate.in"); + in >> n >> m >> s >> d; + + for (int i = 1, x, y, c; i <= m; ++i) { + in >> x >> y >> c; + g[x].push_back(make_pair(c, y)); + g[y].push_back(make_pair(-c, x)); + } + + in.close(); +} diff --git a/villages/dfs/80291b87-9c07-418d-9b05-eef3f8657746.cpp b/villages/dfs/80291b87-9c07-418d-9b05-eef3f8657746.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e983bb137ecda6e22812ae9527cbd4101778ed --- /dev/null +++ b/villages/dfs/80291b87-9c07-418d-9b05-eef3f8657746.cpp @@ -0,0 +1,45 @@ +#include +#include +const int MAX_N = 30000; +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct Muchie { + int v, c; +}; +Muchie x, y; +long long n, m, s, ok; +bool vizitat[MAX_N + 1]; +int dp[MAX_N + 1]; + +vector vecini[MAX_N + 1]; + +void dfs(Muchie u) { + vizitat[u.v] = 1; + if (ok == 1) + return; + if(u.v == y.v) { + ok = 1; + return; + } + for(auto v : vecini[u.v]) + if(!vizitat[v.v]) { + dp[v.v]=dp[u.v]+v.c; + dfs(v); + } +} +int main() +{ + fin >> n >> m >> x.v >> y.v; + for (int i = 1; i <= m; i++) { + int u, w, c; + fin >> u >> w >> c; + vecini[u].push_back({w, c}); + vecini[w].push_back({u, -c}); + } + dfs(x); + fout << dp[y.v] << '\n'; + return 0; +} diff --git a/villages/dfs/8241a9c5-3c61-4ec4-80b1-146988795447.cpp b/villages/dfs/8241a9c5-3c61-4ec4-80b1-146988795447.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/8241a9c5-3c61-4ec4-80b1-146988795447.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair < int, int > > graf[30005]; +bool vizitat[30005]; +int n,m,X,y,ok=1; +long int s; +void citire() +{ + fin>>n>>m>>X>>y; + for(int i=1; i<=m; i++) + { + int a,b,d; + fin>>a>>b>>d; + graf[a].push_back(make_pair(b,d)); + graf[b].push_back(make_pair(a,-d)); + } +} +void drum(int x,int s) +{ + + if(x==y) + { + fout< +#include + +#define N 30005 + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +int dist[N]; +vector > G[N]; +bool viz[N]; + +inline void DFS(const int &nod) +{ + viz[nod] = 1; + for(auto i : G[nod]) + if(!viz[i.first]) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } +} + +int main() +{ + int n, m, X, Y; + fin >> n >> m >> X >> Y; + for(int i = 1, x, y, c; i <= m; ++i) + { + fin >> x >> y >> c; + G[x].push_back(make_pair(y, c)); + G[y].push_back(make_pair(x, -c)); + } + fin.close(); + + DFS(X); + fout << dist[Y]; + fout.close(); + return 0; +} diff --git a/villages/dfs/8a386a85-2794-4846-9a0c-89fb0fc2e9ee.cpp b/villages/dfs/8a386a85-2794-4846-9a0c-89fb0fc2e9ee.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a7d686a276b762214aa13531122a32d7eadb81a7 --- /dev/null +++ b/villages/dfs/8a386a85-2794-4846-9a0c-89fb0fc2e9ee.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y; +long int s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/dfs/8e04f684-3dc9-4469-8a7a-2e50e608ead2.cpp b/villages/dfs/8e04f684-3dc9-4469-8a7a-2e50e608ead2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7211fa1f73f394346fdb112ede3451798f7bccb7 --- /dev/null +++ b/villages/dfs/8e04f684-3dc9-4469-8a7a-2e50e608ead2.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +#define pb push_back + +int n,m,X,Y,D[30010]; +vector > V[30010]; +bool u[30010]; + +void dfs(int q, int c){ + D[q]=c; + u[q]=true; + for (int i=0; i>n>>m>>X>>Y; + for (int i=1; i<=m; i++){ + int x,y,c; + cin>>x>>y>>c; + V[x].pb(make_pair(y,c)); + V[y].pb(make_pair(x,c)); + } + dfs(X,0); + cout< +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +int n,m; +int X,Y; +vector >vec[30005]; +int parcurs[30005]; +int sol[30005]; +void componenta_conexa(int x,int value) +{ + sol[x]=value; + for(int i=0;i<=vec[x].size()-1;i++) + { + if(parcurs[vec[x][i].first]==0) + { + parcurs[vec[x][i].first]=1; + ///daca in vector distanta paan la nodul vec[x][i] este mai mica decat una calculata + /// se scade altfe se insumeaza + if(x>vec[x][i].first) + componenta_conexa(vec[x][i].first,value-vec[x][i].second); + else + componenta_conexa(vec[x][i].first,value+vec[x][i].second); + } + } +} +int main() +{ + fi>>n>>m; + fi>>X>>Y; + int x,y,dist; + for(int i=1;i<=m;i++) + { + fi>>x>>y>>dist; + vec[x].push_back(make_pair(y,dist)); + vec[y].push_back(make_pair(x,dist)); + } + parcurs[X]=1; + componenta_conexa(X,0); + fo< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#define IOS ios_base::sync_with_stdio(0); +#define tie cin.tie(0); +#define mp make_pair +#define ll long long +#define PII pair +#define PLL pair +#define inf 1000000000 +#define zeros(x) ( (x ^ (x - 1)) & x ) + +using namespace std; + +int n, m, x, y, z, in, sf; + +vector < PII > V[100100]; +int cost[100100]; + +void dfs(int x) +{ + for (auto it : V[x]) + { + if (!cost[it.first]) + { + cost[it.first] = cost[x] + it.second; + if (it.first == sf) break; + dfs(it.first); + } + } +} + + +int main(){ + IOS tie + ifstream cin("sate.in"); + ofstream cout("sate.out"); + cin >> n >> m >> in >> sf; + if (in > sf) swap(in, sf); + for (int i = 1; i <= m; i++) + { + cin >> x >> y >> z; + V[x].push_back(mp(y, z)); + V[y].push_back(mp(x, -z)); + } + dfs(in); + cout << cost[sf]; + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + return 0; +} \ No newline at end of file diff --git a/villages/dfs/919f2405-c344-4605-b7d6-012305ec931d.cpp b/villages/dfs/919f2405-c344-4605-b7d6-012305ec931d.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2a6f9e4003dae5ee8a953b9dd6e67150c064485 --- /dev/null +++ b/villages/dfs/919f2405-c344-4605-b7d6-012305ec931d.cpp @@ -0,0 +1,69 @@ +#include +#include +#define N 30005 +using namespace std; + +struct elem{ + int node, cost; + }; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, st, fi, dist[N]; +bool viz[N]; + +inline void Read() +{ + int m, x, y, d; + elem e; + + fin>>n>>m>>st>>fi; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.node = y; + G[x].push_back(e); + + e.cost = -d; + e.node = x; + G[y].push_back(e); + } +} + +inline void Dfs(int node) +{ + elem vec; + int i; + + if (!viz[fi]) + + for (i = 0; i < G[node].size(); ++i) + { + vec=G[node][i]; + + if (!viz[vec.node]) + { + dist[vec.node] = dist[node] + vec.cost; + viz[node] = 1; + Dfs(vec.node); + } + } +} + +void Write() +{ + fout< +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +#include +#include +#define DN 30050 +using namespace std; + +fstream fin("sate.in",ios::in),fout("sate.out",ios::out); +vector< pair > v[DN]; +int N,M,X,Y,ok=0; +int dist[DN]; + + +void dfs(int nod) +{ + if(nod==Y) + { + fout<<(dist[nod]-1)<<"\n"; + ok=1; + } + if(ok==1) return ; + for(auto i:v[nod]) + { + if(dist[i.first]==0) + { + dist[i.first]=dist[nod]+i.second; + dfs(i.first); + } + } +} +int main() +{ + int i,j,x,y,d; + fin>>N>>M>>X>>Y; + for(i=1;i<=M;i++) + { + fin>>x>>y>>d; + if(x +#include +#include +#include +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int seen[40000], d = 0, y; + +struct drumuri +{ + int vecin; + int dis; +}; + + + +void DFS(int nod, vector v[40000]) +{ + if(nod == y) + { + out< v[40000]; + int n, m, a, b, i, x, dist; + in>>n>>m>>x>>y; + for(i = 0; i>a>>b>>dist; + + drumuri dr = {b, dist}; + v[a].push_back(dr); + + drumuri dr2 = {a, -dist}; + v[b].push_back(dr2); + } + DFS(x, v); + return 0; +} diff --git a/villages/dfs/94d3ee6d-901b-409a-bf3a-aaee653c6dd8.cpp b/villages/dfs/94d3ee6d-901b-409a-bf3a-aaee653c6dd8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..98fc21947b13fd9640850642ddd37e82f7653010 --- /dev/null +++ b/villages/dfs/94d3ee6d-901b-409a-bf3a-aaee653c6dd8.cpp @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + pii *it = &muchii[itt]; + if (dst[it->first] != 0x3F3F3F3F) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/955464d6-7a86-45b7-9d02-d32cc121b565.cpp b/villages/dfs/955464d6-7a86-45b7-9d02-d32cc121b565.cpp new file mode 100644 index 0000000000000000000000000000000000000000..272f0e80a4ace0cddf2201135507cbbe79fc82e2 --- /dev/null +++ b/villages/dfs/955464d6-7a86-45b7-9d02-d32cc121b565.cpp @@ -0,0 +1,106 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/98997f74-2e5c-4ab2-8df7-206c57786940.cpp b/villages/dfs/98997f74-2e5c-4ab2-8df7-206c57786940.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81afa63893c3c34f1b13c6a36e9bb1a4f2664dc5 --- /dev/null +++ b/villages/dfs/98997f74-2e5c-4ab2-8df7-206c57786940.cpp @@ -0,0 +1,38 @@ +#include +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back({b,cost}); + A[b].push_back({a,-cost}); + } + + DFS(x); + fout< +using namespace std; +#define pb push_back + +int n,m,X,Y,D[30010]; +vector > V[30010]; +bool u[30010]; + +void dfs(int q, int c){ + D[q]=c; + u[q]=true; + for (int i=0; i>n>>m>>X>>Y; + for (int i=1; i<=m; i++){ + int x,y,c; + cin>>x>>y>>c; + V[x].pb(make_pair(y,c)); + V[y].pb(make_pair(x,c)); + } + dfs(X,0); + cout< +#include + +using namespace std; + +#define N_MAX 30000 + +struct SATE{ + int nod, cost; +}; + +vector muchii[N_MAX + 1]; +int dist[N_MAX + 1]; + +void dfs( int node ) { + for ( auto copil: muchii[node] ) { + if ( dist[copil.nod] == 0 ) { + if ( copil.nod < node ) + dist[copil.nod] = dist[node] - copil.cost; + else + dist[copil.nod] = dist[node] + copil.cost; + dfs(copil.nod); + } + } +} + +ifstream cin ( "sate.in" ); +ofstream cout ( "sate.out" ); + +int main() { + int n, m, x, y, i, a, b, d; + cin >> n >> m >> x >> y; + for ( i = 0; i < m; i++ ) { + cin >> a >> b >> d; + muchii[a].push_back({b, d}); + muchii[b].push_back({a, d}); + } + dist[x] = 1; + dfs(x); + cout << dist[y] - 1; + return 0; +} diff --git a/villages/dfs/a0f07747-40ac-11ec-b4c5-dc7196033e92.cpp b/villages/dfs/a0f07747-40ac-11ec-b4c5-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3cf7a7847952e2c74ab1364988964c35bb79cb38 --- /dev/null +++ b/villages/dfs/a0f07747-40ac-11ec-b4c5-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include + +#define MAX_N 100000 + +using namespace std; + +struct muchie { + int to, d; +}; + +int viz[MAX_N], dist[MAX_N]; +vector muchii[MAX_N]; + +void dfs( int nod ) { + int nextNod, d, i; + + viz[nod] = 1; + for ( i = 0; i < muchii[nod].size(); i++ ) { + nextNod = muchii[nod][i].to; + d = muchii[nod][i].d; + if ( !viz[nextNod] ) { + if ( nod < nextNod ) + dist[nextNod] = dist[nod] + d; + else + dist[nextNod] = dist[nod] - d; + dfs( nextNod ); + } + } +} + +int main() { + FILE *fin, *fout; + int n, m, x, y, a, b, d, i; + + fin = fopen( "sate.in", "r" ); + fscanf( fin, "%d%d%d%d", &n, &m, &x, &y ); + x--; + y--; + for ( i = 0; i < m; i++ ) { + fscanf( fin, "%d%d%d", &a, &b, &d ); + a--; + b--; + muchii[a].push_back( { b, d } ); + muchii[b].push_back( { a, d } ); + } + fclose( fin ); + + dfs( x ); + + fout = fopen( "sate.out", "w" ); + fprintf( fout, "%d", dist[y] ); + fclose( fout ); + + return 0; +} diff --git a/villages/dfs/a159ab8d-1d56-4cdc-acaf-de55376ac0bb.cpp b/villages/dfs/a159ab8d-1d56-4cdc-acaf-de55376ac0bb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..860d728e81d2f0f8e6082249f217f05f4bf48e68 --- /dev/null +++ b/villages/dfs/a159ab8d-1d56-4cdc-acaf-de55376ac0bb.cpp @@ -0,0 +1,79 @@ +#include +#include +#include + +using namespace std; + + +ifstream in("sate.in"); +ofstream out("sate.out"); + + +const int MAX = 30005; + + +vector < pair < int , int > > myVector[MAX]; + +bool beenThere[MAX]; + +int N,M,Plec,Ajung; +int D[MAX]; + +void Read() +{ + in >> N >> M >> Plec >> Ajung; + + for ( int i = 1; i <= M ; ++i) + { + int x,y,cost; + int costnegativ; + in >> x >> y >> cost; + costnegativ = cost*-1; + + myVector[x].push_back(make_pair(y,cost)); + myVector[y].push_back(make_pair(x,costnegativ)); + } +} + +void DFS(int nod) +{ + beenThere[nod] = true; + + for ( size_t k = 0 ; k < myVector[nod].size(); k++) + { + int Vecin = myVector[nod][k].first; + int cost = myVector[nod][k].second; + + if(beenThere[Vecin] == false) + { + + D[Vecin] = D[nod] + cost; + + + DFS(Vecin); + } + } + +} + + +void Rezolv() +{ + + + DFS(Plec); + + out << D[Ajung]; + + + +} + + +int main() +{ + Read(); + Rezolv(); + + return 0; +} diff --git a/villages/dfs/a195ddf9-130f-4324-b247-fc8db6cd3ca8.cpp b/villages/dfs/a195ddf9-130f-4324-b247-fc8db6cd3ca8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f23f9f3927e26aced13ac1abe1d804f3e93de234 --- /dev/null +++ b/villages/dfs/a195ddf9-130f-4324-b247-fc8db6cd3ca8.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +const int N = 30005; + +vector> gr[N]; +int d[N]; +bool viz[N]; + +void dfs(int nod) { + viz[nod] = true; + for (auto e : gr[nod]) + if (!viz[e.first]) { + d[e.first] = d[nod] + e.second; + dfs(e.first); + } +} + +int main() { + ifstream cin("sate.in"); + ofstream cout("sate.out"); + int n, m, x, y; + cin >> n >> m >> x >> y; + while (m--) { + int a, b, dist; + cin >> a >> b >> dist; + gr[a].emplace_back(b, dist); + gr[b].emplace_back(a, -dist); + } + cin.close(); + dfs(x); + cout << d[y] << "\n"; + cout.close(); + return 0; +} \ No newline at end of file diff --git a/villages/dfs/a287a0e6-2bda-4e4e-bdb6-19e0f85e91ec.cpp b/villages/dfs/a287a0e6-2bda-4e4e-bdb6-19e0f85e91ec.cpp new file mode 100644 index 0000000000000000000000000000000000000000..74e983bb137ecda6e22812ae9527cbd4101778ed --- /dev/null +++ b/villages/dfs/a287a0e6-2bda-4e4e-bdb6-19e0f85e91ec.cpp @@ -0,0 +1,45 @@ +#include +#include +const int MAX_N = 30000; +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct Muchie { + int v, c; +}; +Muchie x, y; +long long n, m, s, ok; +bool vizitat[MAX_N + 1]; +int dp[MAX_N + 1]; + +vector vecini[MAX_N + 1]; + +void dfs(Muchie u) { + vizitat[u.v] = 1; + if (ok == 1) + return; + if(u.v == y.v) { + ok = 1; + return; + } + for(auto v : vecini[u.v]) + if(!vizitat[v.v]) { + dp[v.v]=dp[u.v]+v.c; + dfs(v); + } +} +int main() +{ + fin >> n >> m >> x.v >> y.v; + for (int i = 1; i <= m; i++) { + int u, w, c; + fin >> u >> w >> c; + vecini[u].push_back({w, c}); + vecini[w].push_back({u, -c}); + } + dfs(x); + fout << dp[y.v] << '\n'; + return 0; +} diff --git a/villages/dfs/a34aafb6-575c-44e1-a951-aab6de01abdb.cpp b/villages/dfs/a34aafb6-575c-44e1-a951-aab6de01abdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a2a6f9e4003dae5ee8a953b9dd6e67150c064485 --- /dev/null +++ b/villages/dfs/a34aafb6-575c-44e1-a951-aab6de01abdb.cpp @@ -0,0 +1,69 @@ +#include +#include +#define N 30005 +using namespace std; + +struct elem{ + int node, cost; + }; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, st, fi, dist[N]; +bool viz[N]; + +inline void Read() +{ + int m, x, y, d; + elem e; + + fin>>n>>m>>st>>fi; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.node = y; + G[x].push_back(e); + + e.cost = -d; + e.node = x; + G[y].push_back(e); + } +} + +inline void Dfs(int node) +{ + elem vec; + int i; + + if (!viz[fi]) + + for (i = 0; i < G[node].size(); ++i) + { + vec=G[node][i]; + + if (!viz[vec.node]) + { + dist[vec.node] = dist[node] + vec.cost; + viz[node] = 1; + Dfs(vec.node); + } + } +} + +void Write() +{ + fout< +#include +#define N 30005 +using namespace std; + +struct elem{ + int node, cost; + }; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, st, fi, dist[N]; +bool viz[N]; + +inline void Read() +{ + int m, x, y, d; + elem e; + + fin>>n>>m>>st>>fi; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.node = y; + G[x].push_back(e); + + e.cost = -d; + e.node = x; + G[y].push_back(e); + } +} + +inline void Dfs(int node) +{ + elem vec; + int i; + + if (!viz[fi]) + + for (i = 0; i < G[node].size(); ++i) + { + vec=G[node][i]; + + if (!viz[vec.node]) + { + dist[vec.node] = dist[node] + vec.cost; + viz[node] = 1; + Dfs(vec.node); + } + } +} + +void Write() +{ + fout< + +using namespace std; + +vector > v[30001]; +bool verif[30001]; +int dist[30001]; +void dfs(int nod) +{ + verif[nod] = 1; + for ( auto x:v[nod]) + { + if (!verif[x.first]) + { + dist[x.first] = dist[nod] + x.second; + dfs(x.first); + } + } +} + +int main() +{ + ifstream cin ("sate.in"); + ofstream cout ("sate.out"); + int n, m, x, y; + cin >> n >> m >> x >> y; + for ( int i = 1; i <= m; i++) + { + int a, b, d; + cin >> a >> b >> d; + v[a].push_back({b, d}); + v[b].push_back({a, -d}); + } + dfs(x); + cout << dist[y]; + return 0; +} diff --git a/villages/dfs/a6dbb900-40ac-11ec-a95a-dc7196033e92.cpp b/villages/dfs/a6dbb900-40ac-11ec-a95a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b91cd025a70d9adbb8b7069dc2774e7a6c542db --- /dev/null +++ b/villages/dfs/a6dbb900-40ac-11ec-a95a-dc7196033e92.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +typedef long long ll; +const int lim=3e4+5; +vector > vec[lim]; +int n,m,st,dr,x,y; ll d; +ll dist[lim]; +bool ok[lim]; +void df(int nod) +{ + ok[nod]=true; + for(auto x:vec[nod]) + if(!ok[x.first]) + dist[x.first]=dist[nod]+x.second, + df(x.first); +} +int main() +{ + in>>n>>m>>st>>dr; + for(int i=1;i<=m;++i) + { + in>>x>>y>>d; + vec[x].push_back({y,d}); + vec[y].push_back({x,-d}); + } + df(st); + out< +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y, verificat[30001], a, b, c; +vector > graf[30001]; + +void calc(int sat, int dist) { + verificat[sat] = 1; + + if (sat == y) { + out << dist; + return; + } + + for (auto edge : graf[sat]) + if (!verificat[edge.first]) + calc(edge.first, edge.second + dist); +} + +int main() { + in >> n >> m >> x >> y; + while (m--) { + in >> a >> b >> c; + graf[a].push_back({b, c}); + graf[b]. push_back({a, -1 * c}); + } + + calc(x, 0); + return 0; +} diff --git a/villages/dfs/a9934ae9-394a-469e-aa21-184450f69aca.cpp b/villages/dfs/a9934ae9-394a-469e-aa21-184450f69aca.cpp new file mode 100644 index 0000000000000000000000000000000000000000..620bb1bff377d7d7942dc0960acdb78ee5e25d49 --- /dev/null +++ b/villages/dfs/a9934ae9-394a-469e-aa21-184450f69aca.cpp @@ -0,0 +1,111 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/a9a36d54-40ac-11ec-88a6-dc7196033e92.cpp b/villages/dfs/a9a36d54-40ac-11ec-88a6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f23f9f3927e26aced13ac1abe1d804f3e93de234 --- /dev/null +++ b/villages/dfs/a9a36d54-40ac-11ec-88a6-dc7196033e92.cpp @@ -0,0 +1,37 @@ +#include +#include + +using namespace std; + +const int N = 30005; + +vector> gr[N]; +int d[N]; +bool viz[N]; + +void dfs(int nod) { + viz[nod] = true; + for (auto e : gr[nod]) + if (!viz[e.first]) { + d[e.first] = d[nod] + e.second; + dfs(e.first); + } +} + +int main() { + ifstream cin("sate.in"); + ofstream cout("sate.out"); + int n, m, x, y; + cin >> n >> m >> x >> y; + while (m--) { + int a, b, dist; + cin >> a >> b >> dist; + gr[a].emplace_back(b, dist); + gr[b].emplace_back(a, -dist); + } + cin.close(); + dfs(x); + cout << d[y] << "\n"; + cout.close(); + return 0; +} \ No newline at end of file diff --git a/villages/dfs/a9bef56c-35a4-4e9a-9166-33a7db82b65b.cpp b/villages/dfs/a9bef56c-35a4-4e9a-9166-33a7db82b65b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e5bfdf16ce1f4b151019f859b1f62bde10849a7 --- /dev/null +++ b/villages/dfs/a9bef56c-35a4-4e9a-9166-33a7db82b65b.cpp @@ -0,0 +1,41 @@ +#include +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +using namespace std; + + +#define MAXN 30001 +vector < pair > v[MAXN]; +bool viz[MAXN]; +int drum[MAXN]; +int b, a, ans; +int dfs(int nod) { + viz[nod] = 1; + for(int i = 0; i < v[nod].size(); i++) { + pair it = v[nod][i]; + if(viz[it.first] == 0){ + drum[it.first]=drum[nod]+it.second; + //printf("%d %d\n", it.first, drum[it.first]); + if(it.first==b){ + return 0; + } + dfs(it.first); + } + + } +} + +int main(){ + FILE*fin=fopen("sate.in", "r"); + FILE*fout=fopen("sate.out", "w"); + int n, m, a, b, i, x, y, c; + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for(i=1; i<=m; i++){ + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back({y, c}); + v[y].push_back({x, -c}); + } + ans=0; + dfs(a); + if(b>a) + fprintf(fout, "%d", drum[b]); + else + fprintf(fout, "%d", -drum[b]); + return 0; +} diff --git a/villages/dfs/acc9484f-40ac-11ec-9bbb-dc7196033e92.cpp b/villages/dfs/acc9484f-40ac-11ec-9bbb-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e2e785ebd8e71b5ba8a6b6c12f2b16d668d938e3 --- /dev/null +++ b/villages/dfs/acc9484f-40ac-11ec-9bbb-dc7196033e92.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +using namespace std; + +ifstream cin ("sate.in"); +ofstream cout ("sate.out"); + +vector > G[30002]; +int v[30002]; + +void fct (int nod, int dist); + +int ok,x,y; +int main() +{ + int n,i,m,a,b,d; + cin>>n>>m>>x>>y; + i=1; + while(i<=m) + { + cin>>a>>b>>d; + G[a].push_back({b,d}); + G[b].push_back({a,d}); + i++; + } + fct(x,0); + return 0; +} +//facem o functie pt nod, distanta de la x la nod +// x nod vecin -> d=dist+second +// x vecin nod -> d=dist-second +// nod x vecin -> d=second-dist +// nod vecin x -> d=dist-second +// vecin nod x -> d=dist+second + +void fct (int nod, int dist) +{ + int dis,vecin,i; + if(ok) return; + v[nod]=1; + if(nod==y) {cout<nod && nod>vecin) fct(vecin, dist+dis); + else fct(vecin, abs(dist-dis)); + } +} diff --git a/villages/dfs/ad3d4615-40ac-11ec-b00c-dc7196033e92.cpp b/villages/dfs/ad3d4615-40ac-11ec-b00c-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d03f5eba29408504f0d3fecac1e7dee77ef942d5 --- /dev/null +++ b/villages/dfs/ad3d4615-40ac-11ec-b00c-dc7196033e92.cpp @@ -0,0 +1,39 @@ +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAXN = 30001; + +vector > v[MAXN]; + +int dist[MAXN]; +bool seen[MAXN]; + +void dfs(int node, int current) { + seen[node] = 1; + dist[node] = current; + for (auto elem: v[ node ]) { + if (!seen[elem.first]) + dfs(elem.first, current + elem.second); + } +} + +int main() +{ + int n, m, x, y; + fin >> n >> m >> x >> y; + for (int i = 1; i <= m; i++) { + int a, b, c; + fin >> a >> b >> c; + v[ a ].push_back({b, c}); + v[ b ].push_back({a, -c}); + } + + dfs(x, 0); + fout << dist[ y ]; + + return 0; +} diff --git a/villages/dfs/ad6a3519-2c09-4c3a-bcdf-6cccdea2d01b.cpp b/villages/dfs/ad6a3519-2c09-4c3a-bcdf-6cccdea2d01b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/ad6a3519-2c09-4c3a-bcdf-6cccdea2d01b.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include +#define N 30005 +using namespace std; + +struct elem{ + int node, cost; + }; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, st, fi, dist[N]; +bool viz[N]; + +inline void Read() +{ + int m, x, y, d; + elem e; + + fin>>n>>m>>st>>fi; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.node = y; + G[x].push_back(e); + + e.cost = -d; + e.node = x; + G[y].push_back(e); + } +} + +inline void Dfs(int node) +{ + elem vec; + int i; + + if (!viz[fi]) + + for (i = 0; i < G[node].size(); ++i) + { + vec=G[node][i]; + + if (!viz[vec.node]) + { + dist[vec.node] = dist[node] + vec.cost; + viz[node] = 1; + Dfs(vec.node); + } + } +} + +void Write() +{ + fout< +#include +#include +#define N 30002 + +using namespace std; + +int n, m, x, y, d[N]; +bool ok; +vector>A[N]; + +void df(int nod) +{ + if(nod==y) + ok=1; + + if(!ok) + { + for(auto i:A[nod]) + { + if(!d[i.first]) + { + d[i.first]=d[nod]+i.second; + df(i.first); + } + } + } +} + +int main() +{ + //freopen("sate.in", "r", stdin); + //freopen("sate.out", "w", stdout); + ifstream fin("sate.in"); + ofstream fout("sate.out"); + //scanf("%d %d %d %d", &n, &m, &x, &y); + fin>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + int n1,n2,n3; + //scanf("%d %d %d", &n1, &n2, &n3); + fin>>n1>>n2>>n3; + A[n1].push_back({n2,n3}); + A[n2].push_back({n1,-n3}); + } + d[x]=1; + df(x); + //printf("%d\n",d[y]-1); + fout< + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int MAXN = 30001; + +vector > v[MAXN]; + +int answer; +bool seen[MAXN]; + +void dfs(int node, int sink, int current) { + seen[node] = 1; + if (node == sink) + answer = current; + for (auto elem: v[ node ]) { + if (!seen[elem.first]) { + current += elem.second; + dfs(elem.first, sink, current); + current -= elem.second; + } + } +} + +int main() +{ + int n, m, x, y; + fin >> n >> m >> x >> y; + for (int i = 1; i <= m; i++) { + int a, b, c; + fin >> a >> b >> c; + v[ a ].push_back({b, c}); + v[ b ].push_back({a, -c}); + } + + dfs(x, y, 0); + fout << answer; + + return 0; +} diff --git a/villages/dfs/b0425e8e-629b-4009-a83e-2c939fe32384.cpp b/villages/dfs/b0425e8e-629b-4009-a83e-2c939fe32384.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b91cd025a70d9adbb8b7069dc2774e7a6c542db --- /dev/null +++ b/villages/dfs/b0425e8e-629b-4009-a83e-2c939fe32384.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +typedef long long ll; +const int lim=3e4+5; +vector > vec[lim]; +int n,m,st,dr,x,y; ll d; +ll dist[lim]; +bool ok[lim]; +void df(int nod) +{ + ok[nod]=true; + for(auto x:vec[nod]) + if(!ok[x.first]) + dist[x.first]=dist[nod]+x.second, + df(x.first); +} +int main() +{ + in>>n>>m>>st>>dr; + for(int i=1;i<=m;++i) + { + in>>x>>y>>d; + vec[x].push_back({y,d}); + vec[y].push_back({x,-d}); + } + df(st); + out< + +#define NMAX 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector > G[NMAX]; +int dp[NMAX], viz[NMAX]; +void DFS(int nod, int tata) +{ + for(auto it:G[nod]) + if(it.first != tata && !viz[it.first]) + { + viz[it.first] = 1; + dp[it.first] += dp[nod]; + if(it.first < nod) + dp[it.first] -= it.second; + else dp[it.first] += it.second; + DFS(it.first, nod); + } +} + +int main() +{ + int n, m, x, y; + fin >> n >> m >> x >> y; + for(int i = 1; i <= m; ++i) + { + int x, y, c; + fin >> x >> y >> c; + + G[x].push_back({y, c}); + G[y].push_back({x, c}); + } + + viz[x] = 1; + DFS(x, 0); + + fout << dp[y] << '\n'; + return 0; +} diff --git a/villages/dfs/b187a350-40ac-11ec-9c95-dc7196033e92.cpp b/villages/dfs/b187a350-40ac-11ec-9c95-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..994ae596bbf1f5b149d1315d861217acc69e9b4a --- /dev/null +++ b/villages/dfs/b187a350-40ac-11ec-9c95-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +int n,m; +int X,Y; +vector >vec[30005]; +int parcurs[30005]; +int sol[30005]; +void componenta_conexa(int x,int value) +{ + sol[x]=value; + for(int i=0;i<=vec[x].size()-1;i++) + { + if(parcurs[vec[x][i].first]==0) + { + parcurs[vec[x][i].first]=1; + ///daca in vector distanta paan la nodul vec[x][i] este mai mica decat una calculata + /// se scade altfe se insumeaza + if(x>vec[x][i].first) + componenta_conexa(vec[x][i].first,value-vec[x][i].second); + else + componenta_conexa(vec[x][i].first,value+vec[x][i].second); + } + } +} +int main() +{ + fi>>n>>m; + fi>>X>>Y; + int x,y,dist; + for(int i=1;i<=m;i++) + { + fi>>x>>y>>dist; + vec[x].push_back(make_pair(y,dist)); + vec[y].push_back(make_pair(x,dist)); + } + parcurs[X]=1; + componenta_conexa(X,0); + fo< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/b238cb32-33e4-4e43-81e1-877b69596df0.cpp b/villages/dfs/b238cb32-33e4-4e43-81e1-877b69596df0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0d727a13dd5131b2e401e8486c5bddb6d867299 --- /dev/null +++ b/villages/dfs/b238cb32-33e4-4e43-81e1-877b69596df0.cpp @@ -0,0 +1,36 @@ +#include +#define mp make_pair +#define pb push_back +#define st first +#define nd second +#define pii pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y, a, b, c, d[30100]; bool viz[30100]; +vector v[100100]; + +void dfs(int nod){ + for(auto it : v[nod]){ + if(!viz[it.st]){ + viz[it.st] = 1; + d[it.st] = d[nod] + it.nd; + if(it.st == y) return; + dfs(it.st); + } + } +} + +int main(){ + in >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + in >> a >> b >> c; + v[a].pb(mp(b, c)); + v[b].pb(mp(a, -c)); + } + dfs(x); + return out << d[y], 0; +} diff --git a/villages/dfs/b23a8bd1-1590-40dd-b16a-137a4b1f41c4.cpp b/villages/dfs/b23a8bd1-1590-40dd-b16a-137a4b1f41c4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f43742de5739112de1c42f955efb15170657b5a --- /dev/null +++ b/villages/dfs/b23a8bd1-1590-40dd-b16a-137a4b1f41c4.cpp @@ -0,0 +1,42 @@ +#include +#include + +FILE *fin = fopen("sate.in", "r"); +FILE *fout = fopen("sate.out", "w"); + +#define maxn 120000 +#define ll long long + +std::vector> v[maxn + 1]; +int viz[maxn + 1]; +int a, b; +ll dp[maxn + 1]; +void dfs(int t) { + viz[t] = 1; + int p, c; + for (auto it : v[t]) { + p = it.first; + c = it.second; + if (viz[p] != 1) { + dp[p] = dp[t] + c; + dfs(p); + } + + } +} + +int main() +{ + + int n, m, x, y, c; + + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for (int i = 1; i <= m; i++) { + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back (std::make_pair(y, c)); + v[y].push_back (std::make_pair(x, -c)); + } + dfs(a); + fprintf(fout, "%lld", dp[b]); + return 0; +} diff --git a/villages/dfs/b2cf3b5b-794e-429b-bf94-cba485a38c14.cpp b/villages/dfs/b2cf3b5b-794e-429b-bf94-cba485a38c14.cpp new file mode 100644 index 0000000000000000000000000000000000000000..26306a6d6f84337853c9a5f5df6582d787b23f9a --- /dev/null +++ b/villages/dfs/b2cf3b5b-794e-429b-bf94-cba485a38c14.cpp @@ -0,0 +1,38 @@ +#include +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b,cost)); + A[b].push_back(make_pair(a,-cost)); + } + + DFS(x); + fout< +#define ll long long +#define cin fin +#define cout fout +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int n, m, start, end, x, y, result, cost; +vector > adj[30005]; +bitset <30005> reached; + +void dfs(int currNode, int costFromStart) { + if (currNode == ::end) { + result = costFromStart; + return; + } + + reached[currNode] = 1; + for (pair edge : adj[currNode]) + if (!reached[edge.first]) + if (edge.first < currNode) + dfs(edge.first, costFromStart - edge.second); + else + dfs(edge.first, costFromStart + edge.second); +} + +int main() { + cin >> n >> m >> start >> ::end; + for (int i = 1; i <= m; i++) { + cin >> x >> y >> cost; + adj[x].push_back(make_pair(y, cost)); + adj[y].push_back(make_pair(x, cost)); + } + + dfs(start, 0); + + cout << result; + + return 0; +} diff --git a/villages/dfs/b3108068-0d61-4ad1-a004-921ce53a6aa7.cpp b/villages/dfs/b3108068-0d61-4ad1-a004-921ce53a6aa7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..620bb1bff377d7d7942dc0960acdb78ee5e25d49 --- /dev/null +++ b/villages/dfs/b3108068-0d61-4ad1-a004-921ce53a6aa7.cpp @@ -0,0 +1,111 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int Q[MAXN], qs, qe; + +void DFS(int nod, int tar) { + Q[qe++] = nod; + dst[nod] = 0; + while (qs < qe) { + int x = Q[qs++]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + Q[qe++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/b34685b8-40ac-11ec-a74a-dc7196033e92.cpp b/villages/dfs/b34685b8-40ac-11ec-a74a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b121b065eecb65910a1b36e8f482d3cc6c9d092f --- /dev/null +++ b/villages/dfs/b34685b8-40ac-11ec-a74a-dc7196033e92.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +const int N=3e4+10; +struct pereche{ + int nod; + int cost; +}; +vector mat[N]; +bitset vizitat; +int n,start,stop; +bool FINAL=false; +void dfs(int x,int c) +{ + if(x==stop) + { + g<>n>>m>>start>>stop; + for(int i=1;i<=m;i++) + { + int x,y,z; + f>>x>>y>>z; + pereche aux; + aux.nod=y; + aux.cost=z; + mat[x].push_back(aux); + aux.nod=x; + aux.cost=-z; + mat[y].push_back(aux); + } + dfs(start,0); + return 0; +} diff --git a/villages/dfs/b3f3f0bc-40ac-11ec-9e0f-dc7196033e92.cpp b/villages/dfs/b3f3f0bc-40ac-11ec-9e0f-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fe5db632bc773e8fc156f186dce22e428ebe6da4 --- /dev/null +++ b/villages/dfs/b3f3f0bc-40ac-11ec-9e0f-dc7196033e92.cpp @@ -0,0 +1,85 @@ +#include + +using namespace std; + +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +}; + +InParser fin("sate.in"); +ofstream fout("sate.out"); + +const int NMAX = 3e4 + 4; +int N, M, X, Y, dp[NMAX]; +bool vis[NMAX]; +vector> G[NMAX]; + +void min_self(int &a, int b) { + a = min(a, b); +} + +void dfs(int u) { + vis[u] = true; + for(const pair &v : G[u]) + if(!vis[v.first]) { + if(v.first > u) + min_self(dp[v.first], dp[u] + v.second); + else + min_self(dp[v.first], dp[u] - v.second); + dfs(v.first); + } +} + +int main() { + fin >> N >> M >> X >> Y; + if(X > Y) + swap(X, Y); + memset(dp, 0x3f, sizeof(dp)); + dp[X] = 0; + while(M--) { + int u, v, w; + fin >> u >> v >> w; + G[u].emplace_back(v, w); + G[v].emplace_back(u, w); + if(u == X) + dp[v] = w; + } + dfs(X); + fout << dp[Y]; +} diff --git a/villages/dfs/b42af045-40ac-11ec-a152-dc7196033e92.cpp b/villages/dfs/b42af045-40ac-11ec-a152-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e7aad160b06dfdf4293f15d0492dd18d4c2802c1 --- /dev/null +++ b/villages/dfs/b42af045-40ac-11ec-a152-dc7196033e92.cpp @@ -0,0 +1,63 @@ +#include +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout ("sate.out"); + +typedef struct { + int to; + int km; +} edge; + +vector < edge > graf[30001]; + +int n, m, x, y, dis[30001], viz[30001]; + +void DFS ( int node ) { + viz[ node ] = 1; + + for ( unsigned int l = 0; l < graf[node].size(); l++ ) { + + int to = graf[node][l].to, km = graf[node][l].km; + if ( ! viz[to] ) { + + if ( !dis[to] ) { + + if ( to > node ) + dis[to] = dis[node] + km; + else + dis[to] = dis[node] - km; + } + DFS ( to ); + } + } +} + +void read () { + fin >> n >> m >> x >> y; + + int from; + for (int i = 1; i <= m; i++) { + edge e1, e2; + fin >> from >> e1.to >> e1.km; + e2.to = from, e2.km = e1.km; + graf[ from ].push_back( e1 ); + graf[ e1.to ].push_back ( e2 ); + + if ( from == x ) + dis[ e1.to ] = e1.km; + } +} + +int main() +{ + read (); + + dis[x] = 0; + DFS ( x ); + + fout << dis[y] << "\n"; + return 0; +} diff --git a/villages/dfs/b4b76c17-40ac-11ec-b04a-dc7196033e92.cpp b/villages/dfs/b4b76c17-40ac-11ec-b04a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..aa9ddb69013049800ab99d662f8c02dd1921e9d4 --- /dev/null +++ b/villages/dfs/b4b76c17-40ac-11ec-b04a-dc7196033e92.cpp @@ -0,0 +1,53 @@ +#include +#include + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int nmax=100000; + +struct str{ + int z,k; +}; + +vector g[nmax+1]; + +bool u[nmax+1]; + +int d[nmax+1]; + +int x1,y1; + +void dfs(int x){ + u[x]=1; + if(x==y1){ + return; + } + for(int i=0;ixn&&u[xn]==0){ + d[xn]=d[x]-g[x][i].k; + } + if(u[xn]==0){ + dfs(xn); + } + } +} + +int main(){ + int n,m; + fin>>n>>m>>x1>>y1; + for(int i=1;i<=m;i++){ + int x,y,k; + fin>>x>>y>>k; + g[x].push_back({y,k}); + g[y].push_back({x,k}); + } + dfs(x1); + fout< +using namespace std; +ifstream f("sate.in");ofstream g("sate.out"); +vector > G[30005];int d[30005], c[30005]; +void dfs(int u){ +int v, j;c[u] = 1; +for(j=0;j>n>>m>>s>>t; +for(i=1;i<=m;i++){ +f>>x>>y>>z;G[x].push_back({y,z});G[y].push_back({x,-z});} +dfs(s);g< +#include +#define nod first +#define dist second +using namespace std; +typedef pair ii; +const int NMAX = 30000; +vector G[NMAX + 5]; +int d[NMAX + 5], viz[NMAX + 5]; +void dfs(int u) +{ + int v, j; + viz[u] = 1; + for(j = 0 ; j < G[u].size() ; j ++) + { + v = G[u][j].nod; + if(!viz[v]) + { + d[v] = d[u] + G[u][j].dist; + dfs(v); + } + } +} +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + int n, m, i, x, y, z, s, t; + scanf("%d%d%d%d", &n, &m, &s, &t); + for(i = 1 ; i <= m ; i ++) + { + scanf("%d%d%d", &x, &y, &z); + G[x].push_back({y, z}); + G[y].push_back({x, -z}); + } + dfs(s); + printf("%d\n", d[t] - d[s]); + return 0; +} diff --git a/villages/dfs/b56fe34c-c0ce-4f5c-a005-ea83be3a2ae9.cpp b/villages/dfs/b56fe34c-c0ce-4f5c-a005-ea83be3a2ae9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c2074e93f6b266e4c70029ff79dde5d86a4dafb --- /dev/null +++ b/villages/dfs/b56fe34c-c0ce-4f5c-a005-ea83be3a2ae9.cpp @@ -0,0 +1,41 @@ +#include +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< + +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +int viz[30005],val[30005],n,m,x,y,i,x2,y2,dist; +vector > v[30005]; +void dfs (int x,int dist) +{ + int i,nod; + viz[x]=1; + val[x]=dist; + for (i=0;inod) + { + dfs(nod,dist-v[x][i].second); + } + else + { + dfs(nod,dist+v[x][i].second); + } + } + } +} +int main() +{ + f>>n>>m>>x>>y; + if (x>y)swap(x,y); + for (i=1;i<=m;i++) + { + f>>x2>>y2>>dist; + v[x2].push_back({y2,dist}); + v[y2].push_back({x2,dist}); + } + dfs(x,0); + g< +#include +#define NMAX 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct ura +{ + int nod,cost; +}aux; + +vector adj[NMAX]; +int n,m,x,y; +int viz[NMAX]; + +void dfs(int cv) +{ + for(vector:: iterator it=adj[cv].begin();it!=adj[cv].end();it++) + { + aux=*it; + if(!viz[aux.nod]) + { + viz[aux.nod]=viz[cv]+aux.cost; + dfs(aux.nod); + } + } +} + +int main() +{ + fin>>n>>m>>x>>y; + int i,j,d; + for(int bla=1;bla<=m;bla++) + { + fin>>i>>j>>d; + aux.cost=d; + aux.nod=j; + adj[i].push_back(aux); + aux.cost=-d; + aux.nod=i; + adj[j].push_back(aux); + } + viz[x]=1; + dfs(x); + fout< +#define STOP fout.close(); exit(0); +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +///************************ +const int NMAX = 3e4 + 3; +int n, m, x, y, dis[NMAX];//dis[i]-distance from x to i; +#define fi first +#define se second +vector> edges[NMAX]; + +void read() { + fin >> n >> m >> x >> y; + for (int a, b, c, i = 0; i < m; i++) { + fin >> a >> b >> c; + edges[a].push_back(make_pair(b, c)); + edges[b].push_back(make_pair(a, c)); + } +} + +void dfs(int node) { + for (auto it : edges[node]) { + if (dis[it.fi] == -1) { + if (node < it.fi) + dis[it.fi] = dis[node] + it.se; + else + dis[it.fi] = dis[node] - it.se; + dfs(it.fi); + } + } +} + +int main() { + read(); + fill(dis + 1, dis + n + 1, -1); + dis[x] = 0; + dfs(x); + fout << dis[y] << endl; + STOP +} diff --git a/villages/dfs/b679e78c-40ac-11ec-a94a-dc7196033e92.cpp b/villages/dfs/b679e78c-40ac-11ec-a94a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1aaca756a591292772b20eca5193fd109e91f736 --- /dev/null +++ b/villages/dfs/b679e78c-40ac-11ec-a94a-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#define pb push_back +#define MAXN 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int verific[MAXN], sate, distante, start, stop, total; +vector> urmatorul_sat[MAXN]; + +void calculeaza_distanta(int sat, int distanta) { + verific[sat] = 1; + if (sat == stop) + total = distanta; + if (total) + return; + for (auto urmatorul : urmatorul_sat[sat]) + if (!verific[urmatorul.first]) + calculeaza_distanta(urmatorul.first, distanta + urmatorul.second); +} + +int main() { + fin >> sate >> distante >> start >> stop; + int x, y, c; + while (distante--) { + fin >> x >> y >> c; + urmatorul_sat[x].pb(make_pair(y, c)); + urmatorul_sat[y].pb(make_pair(x, c * -1)); + } + calculeaza_distanta(start, 0); + fout << total; +} diff --git a/villages/dfs/b6cb6756-d502-482b-b1d2-a3414606b9a1.cpp b/villages/dfs/b6cb6756-d502-482b-b1d2-a3414606b9a1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1aaca756a591292772b20eca5193fd109e91f736 --- /dev/null +++ b/villages/dfs/b6cb6756-d502-482b-b1d2-a3414606b9a1.cpp @@ -0,0 +1,33 @@ +#include +#define pb push_back +#define MAXN 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int verific[MAXN], sate, distante, start, stop, total; +vector> urmatorul_sat[MAXN]; + +void calculeaza_distanta(int sat, int distanta) { + verific[sat] = 1; + if (sat == stop) + total = distanta; + if (total) + return; + for (auto urmatorul : urmatorul_sat[sat]) + if (!verific[urmatorul.first]) + calculeaza_distanta(urmatorul.first, distanta + urmatorul.second); +} + +int main() { + fin >> sate >> distante >> start >> stop; + int x, y, c; + while (distante--) { + fin >> x >> y >> c; + urmatorul_sat[x].pb(make_pair(y, c)); + urmatorul_sat[y].pb(make_pair(x, c * -1)); + } + calculeaza_distanta(start, 0); + fout << total; +} diff --git a/villages/dfs/b6d243ed-2098-4b0b-a750-22fcf8ff13cf.cpp b/villages/dfs/b6d243ed-2098-4b0b-a750-22fcf8ff13cf.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053e18fa5562bb95ab91a2d3805ab4b71c318226 --- /dev/null +++ b/villages/dfs/b6d243ed-2098-4b0b-a750-22fcf8ff13cf.cpp @@ -0,0 +1,48 @@ +#include +#define IOS ios_base::sync_with_stdio(0); +#define tie cin.tie(0); +#define mp make_pair +#define ll long long +#define PII pair +#define PLL pair +#define inf 1000000000 +#define zeros(x) ( (x ^ (x - 1)) & x ) + +using namespace std; + +int n, m, x, y, z, in, sf; + +vector < PII > V[100100]; +int cost[100100]; + +void dfs(int x) +{ + for (auto it : V[x]) + { + if (!cost[it.first]) + { + cost[it.first] = cost[x] + it.second; + if (it.first == sf) break; + dfs(it.first); + } + } +} + + +int main(){ + IOS tie + ifstream cin("sate.in"); + ofstream cout("sate.out"); + cin >> n >> m >> in >> sf; + if (in > sf) swap(in, sf); + for (int i = 1; i <= m; i++) + { + cin >> x >> y >> z; + V[x].push_back(mp(y, z)); + V[y].push_back(mp(x, -z)); + } + dfs(in); + cout << cost[sf]; + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + return 0; +} \ No newline at end of file diff --git a/villages/dfs/b76a7058-40ac-11ec-aa66-dc7196033e92.cpp b/villages/dfs/b76a7058-40ac-11ec-aa66-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5b4f163818b758475d4294adcaa5e01656d1d070 --- /dev/null +++ b/villages/dfs/b76a7058-40ac-11ec-aa66-dc7196033e92.cpp @@ -0,0 +1,26 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +typedef pair ii; +vector G[30005]; +int d[30005], c[30005]; +void dfs(int u){ +int v, j; +c[u] = 1; +for(j=0;j>n>>m>>s>>t; +for(i=1;i<=m;i++){ +f>>x>>y>>z; +G[x].push_back({y,z}); +G[y].push_back({x,-z});} +dfs(s); +g< +#define ll long long +#define pb push_back +#define pf push_front +#define nmax 1000000 +#define MMAX 100000 +#define inf 1e18 +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +bool viz[30005]; +int dist[30005]; +vector > adj[30005]; +int n,m,x,y; +void dfs(int node,int d) +{ + viz[node]=1; + dist[node]=d; + for(auto x:adj[node]) + { + if(viz[x.first]==0) + { + if(node>n>>m>>x>>y; +if(x>y) swap(x,y); +for(int i=1;i<=m;i++) +{ + int a,b,c; + f>>a>>b>>c; + adj[a].pb({b,c}); + adj[b].pb({a,c}); +} +dfs(x,0); +g< +#include +#include + +std::ifstream fin("sate.in"); +std::ofstream fout("sate.out"); + +const int mxn = 30e3 + 5; + +int n, m, start, finish; +std::vector < std::pair < int, int > > mat[mxn]; +int viz[mxn]; + +inline void DFS(int nod){ + for (auto i : mat[nod]){ + if (!viz[i.second]){ + viz[i.second] = viz[nod] + i.first; + DFS(i.second); + } + } +} + +inline void solve(){ + fin >> n >> m >> start >> finish; + for (int i = 1; i <= m; ++i){ + int x, y, d; + fin >> x >> y >> d; + mat[x].push_back({d, y}); + mat[y].push_back({-d, x}); + } + viz[start] = 1; + DFS(start); + fout << std::max(viz[start] - viz[finish], -viz[start] + viz[finish]); +} + +int main(){ + solve(); + return 0; +} diff --git a/villages/dfs/b884df51-8064-49f7-a962-e277229e3ee0.cpp b/villages/dfs/b884df51-8064-49f7-a962-e277229e3ee0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81afa63893c3c34f1b13c6a36e9bb1a4f2664dc5 --- /dev/null +++ b/villages/dfs/b884df51-8064-49f7-a962-e277229e3ee0.cpp @@ -0,0 +1,38 @@ +#include +#include +#define f first +#define s second +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; + +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back({b,cost}); + A[b].push_back({a,-cost}); + } + + DFS(x); + fout< +#include +#define N 30001 + +using namespace std; + +struct elem +{ + int nod, cost; +}; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, m, start, finish, dist[N]; +bool viz[N]; + +void Citire() +{ + int x, y, d; + elem e; + + fin>>n>>m>>start>>finish; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.nod = y; + G[x].push_back(e); + + e.cost = -d; + e.nod = x; + G[y].push_back(e); + } +} + +void Dfs(int nod) +{ + elem el; + int i; + + if (!viz[finish]) + + for (i = 0; i < G[nod].size(); ++i) + { + el=G[nod][i]; + + if (!viz[el.nod]) + { + dist[el.nod] = dist[nod] + el.cost; + viz[nod] = 1; + Dfs(el.nod); + } + } +} + +void Afisare() +{ + fout< +#include +#define N 30001 + +using namespace std; + +struct elem +{ + int nod, cost; +}; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, m, start, finish, dist[N]; +bool viz[N]; + +void Citire() +{ + int x, y, d; + elem e; + + fin>>n>>m>>start>>finish; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.nod = y; + G[x].push_back(e); + + e.cost = -d; + e.nod = x; + G[y].push_back(e); + } +} + +void Dfs(int nod) +{ + elem el; + int i; + + if (!viz[finish]) + + for (i = 0; i < G[nod].size(); ++i) + { + el=G[nod][i]; + + if (!viz[el.nod]) + { + dist[el.nod] = dist[nod] + el.cost; + viz[nod] = 1; + Dfs(el.nod); + } + } +} + +void Afisare() +{ + fout< +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +struct dist +{ + int s,d; +}; + +vector v[30001]; +bool visit[30001]; + +int findDist(int x,int y,int d = 0) +{ + //cout<>n>>m>>x>>y; + + int i; + for(i=0; i>a>>b>>l; + + dist dst; + dst.d = l; + dst.s = b; + v[a].push_back(dst); + dst.s = a; + v[b].push_back(dst); + } + + out< +#define mp make_pair +#define pb push_back +#define st first +#define nd second +#define pii pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y, a, b, c, d[30100]; bool viz[30100]; +vector v[100100]; + +void dfs(int nod){ + for(auto it : v[nod]){ + if(!viz[it.st]){ + viz[it.st] = 1; + d[it.st] = d[nod] + it.nd; + if(it.st == y) return; + dfs(it.st); + } + } +} + +int main(){ + in >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + in >> a >> b >> c; + v[a].pb(mp(b, c)); + v[b].pb(mp(a, -c)); + } + dfs(x); + return out << d[y], 0; +} diff --git a/villages/dfs/bb21f9b3-40ac-11ec-9637-dc7196033e92.cpp b/villages/dfs/bb21f9b3-40ac-11ec-9637-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7f4bb88260f1185d41bbcbb3a3b4f0cdc6fe3281 --- /dev/null +++ b/villages/dfs/bb21f9b3-40ac-11ec-9637-dc7196033e92.cpp @@ -0,0 +1,71 @@ +#include +#include +#define N 30001 + +using namespace std; + +struct elem +{ + int nod, cost; +}; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, m, start, finish, dist[N]; +bool viz[N]; + +void Citire() +{ + int x, y, d; + elem e; + + fin>>n>>m>>start>>finish; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.nod = y; + G[x].push_back(e); + + e.cost = -d; + e.nod = x; + G[y].push_back(e); + } +} + +void Dfs(int nod) +{ + elem el; + int i; + + if (!viz[finish]) + + for (i = 0; i < G[nod].size(); ++i) + { + el=G[nod][i]; + + if (!viz[el.nod]) + { + dist[el.nod] = dist[nod] + el.cost; + viz[nod] = 1; + Dfs(el.nod); + } + } +} + +void Afisare() +{ + fout< +#include +#define N 30001 + +using namespace std; + +struct elem +{ + int nod, cost; +}; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, m, start, finish, dist[N]; +bool viz[N]; + +void Citire() +{ + int x, y, d; + elem e; + + fin>>n>>m>>start>>finish; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.nod = y; + G[x].push_back(e); + + e.cost = -d; + e.nod = x; + G[y].push_back(e); + } +} + +void Dfs(int nod) +{ + elem el; + int i; + + if (!viz[finish]) + + for (i = 0; i < G[nod].size(); ++i) + { + el=G[nod][i]; + + if (!viz[el.nod]) + { + dist[el.nod] = dist[nod] + el.cost; + viz[nod] = 1; + Dfs(el.nod); + } + } +} + +void Afisare() +{ + fout< + +using namespace std; + +#define sz(x) (int)(x).size() + +typedef pair < int, int > pii; +typedef long long ll; +typedef long double ld; +typedef unsigned int ui; +typedef unsigned long long ull; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int x, y; + +int dfs(int node, vector < vector < pii > > &gr, vector < bool > &used, int curD) { + + used[node] = true; + if (node == y) return curD; + + for (auto pa : gr[node]) { + if (used[pa.first]) continue; + + int ret = dfs(pa.first, gr, used, curD + pa.second); + if (ret >= 0) return ret; + } + + return -1; +} + +int main() { + +#ifdef STEF + freopen("input", "r", stdin); + freopen("output", "w", stdout); +#endif + + // ios::sync_with_stdio(false); + // cin.tie(0);cout.tie(0); + + int n, m; + fin >> n >> m >> x >> y; + + vector < vector < pii > > gr(n + 1); + for (int i = 1; i <= m; ++ i) { + int a, b, c; + fin >> a >> b >> c; + gr[a].push_back({b, c}); + gr[b].push_back({a, -c}); + } + + vector < bool > used(n + 1); + fout << dfs(x, gr, used, 0) << '\n'; +} diff --git a/villages/dfs/be80f8aa-31b6-4561-80a8-d9e7daafa2dd.cpp b/villages/dfs/be80f8aa-31b6-4561-80a8-d9e7daafa2dd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1aaca756a591292772b20eca5193fd109e91f736 --- /dev/null +++ b/villages/dfs/be80f8aa-31b6-4561-80a8-d9e7daafa2dd.cpp @@ -0,0 +1,33 @@ +#include +#define pb push_back +#define MAXN 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int verific[MAXN], sate, distante, start, stop, total; +vector> urmatorul_sat[MAXN]; + +void calculeaza_distanta(int sat, int distanta) { + verific[sat] = 1; + if (sat == stop) + total = distanta; + if (total) + return; + for (auto urmatorul : urmatorul_sat[sat]) + if (!verific[urmatorul.first]) + calculeaza_distanta(urmatorul.first, distanta + urmatorul.second); +} + +int main() { + fin >> sate >> distante >> start >> stop; + int x, y, c; + while (distante--) { + fin >> x >> y >> c; + urmatorul_sat[x].pb(make_pair(y, c)); + urmatorul_sat[y].pb(make_pair(x, c * -1)); + } + calculeaza_distanta(start, 0); + fout << total; +} diff --git a/villages/dfs/be8d6d63-40ac-11ec-b403-dc7196033e92.cpp b/villages/dfs/be8d6d63-40ac-11ec-b403-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7d9c0e0e3ce0698a3c78876f77580e898d54a4a8 --- /dev/null +++ b/villages/dfs/be8d6d63-40ac-11ec-b403-dc7196033e92.cpp @@ -0,0 +1,44 @@ +#include +#include + +using namespace std; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n, m, x, y; + +vector < pair < int, int > > v[30001]; + +int dp[30001]; + +int a, b, d; + +void DFS ( int nod ) +{ + for ( auto i : v[nod] ) + { + int nod2 = i.first; + int cost = i.second; + if ( !dp[nod2] ) + { + dp[nod2] = dp[nod] + cost; + DFS(nod2); + } + } +} + +int main() +{ + in >> n >> m >> x >> y; + for ( register int i = 1 ; i <= m ; ++i ) + { + in >> a >> b >> d; + v[a].push_back ( { b, d } ); + v[b].push_back ( { a, -d } ); + } + DFS(x); + out << dp[y]; + return 0; +} + diff --git a/villages/dfs/bf8c9e50-40ac-11ec-a5a8-dc7196033e92.cpp b/villages/dfs/bf8c9e50-40ac-11ec-a5a8-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76d5d1da76c3c4a2466891e04ffc12f0352e992a --- /dev/null +++ b/villages/dfs/bf8c9e50-40ac-11ec-a5a8-dc7196033e92.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int NOD1,NOD2; +vector < int > noduri[30005]; +vector < int > dt[30005]; +int vizitat[30005]; +int distanta[30005]; +int distnc; + +void graf(int NOD, int distnc) { + + distanta[NOD]= distnc; + vizitat[NOD]=1; + + for (int i=0;i> n; + fin>> m; + fin >> NOD1; + fin >> NOD2; + + for (int i=1;i<=m;i++) { + fin >> x >> y >> distnc; + noduri[x].push_back(y); + noduri[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graf(NOD1,0); + fout << distanta[NOD2]; + return 0; +} diff --git a/villages/dfs/c2d1a193-8b79-42da-b1aa-887a733ad0d7.cpp b/villages/dfs/c2d1a193-8b79-42da-b1aa-887a733ad0d7.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d7a13e202d3ae41e98c4df116b9def4811e267d2 --- /dev/null +++ b/villages/dfs/c2d1a193-8b79-42da-b1aa-887a733ad0d7.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#define Nmax 30005 +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +bool Use[Nmax]; +int D[Nmax],N,M,X,Y; + +vector < pair > G[Nmax]; + +void Read() +{ + fin>>N>>M>>X>>Y; + while(M--) + { + int i,j,D; + fin>>i>>j>>D; + G[i].push_back(make_pair(j,D)); + G[j].push_back(make_pair(i,D)); + } +} + +void DFS(int Node) +{ + Use[Node]=1; + for(int i=0;i<(int)G[Node].size();++i) + { + int Vecin=G[Node][i].first; + int Dist=G[Node][i].second; + + if(!Use[Vecin]) + { + if(Vecin>Node) D[Vecin]=D[Node]+Dist; + else D[Vecin]=D[Node]-Dist; + DFS(Vecin); + } + } +} + +void Print() +{ + DFS(X); + fout< +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y; +long int s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/dfs/c49f1804-a4cd-4064-99d5-f7900033fdf0.cpp b/villages/dfs/c49f1804-a4cd-4064-99d5-f7900033fdf0.cpp new file mode 100644 index 0000000000000000000000000000000000000000..053e18fa5562bb95ab91a2d3805ab4b71c318226 --- /dev/null +++ b/villages/dfs/c49f1804-a4cd-4064-99d5-f7900033fdf0.cpp @@ -0,0 +1,48 @@ +#include +#define IOS ios_base::sync_with_stdio(0); +#define tie cin.tie(0); +#define mp make_pair +#define ll long long +#define PII pair +#define PLL pair +#define inf 1000000000 +#define zeros(x) ( (x ^ (x - 1)) & x ) + +using namespace std; + +int n, m, x, y, z, in, sf; + +vector < PII > V[100100]; +int cost[100100]; + +void dfs(int x) +{ + for (auto it : V[x]) + { + if (!cost[it.first]) + { + cost[it.first] = cost[x] + it.second; + if (it.first == sf) break; + dfs(it.first); + } + } +} + + +int main(){ + IOS tie + ifstream cin("sate.in"); + ofstream cout("sate.out"); + cin >> n >> m >> in >> sf; + if (in > sf) swap(in, sf); + for (int i = 1; i <= m; i++) + { + cin >> x >> y >> z; + V[x].push_back(mp(y, z)); + V[y].push_back(mp(x, -z)); + } + dfs(in); + cout << cost[sf]; + cerr << "Fucking time elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; + return 0; +} \ No newline at end of file diff --git a/villages/dfs/c4a80bb0-40ac-11ec-8e77-dc7196033e92.cpp b/villages/dfs/c4a80bb0-40ac-11ec-8e77-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb5f559c1e2bae95470d93e1f2b117d78e761e72 --- /dev/null +++ b/villages/dfs/c4a80bb0-40ac-11ec-8e77-dc7196033e92.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y, s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/dfs/c4e50067-bc18-49af-8ba0-28ff4fbf449c.cpp b/villages/dfs/c4e50067-bc18-49af-8ba0-28ff4fbf449c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/c4e50067-bc18-49af-8ba0-28ff4fbf449c.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair < int, int > > graf[30005]; +bool vizitat[30005]; +int n,m,X,y,ok=1; +long int s; +void citire() +{ + fin>>n>>m>>X>>y; + for(int i=1; i<=m; i++) + { + int a,b,d; + fin>>a>>b>>d; + graf[a].push_back(make_pair(b,d)); + graf[b].push_back(make_pair(a,-d)); + } +} +void drum(int x,int s) +{ + + if(x==y) + { + fout< +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y; +int nod1,nod2; +vector < int > knots[30005]; +vector < int > dt[30005]; +int viz[30005]; +int dist[30005]; +int distnc; + +void graph(int nod, int distnc) { + dist[nod]= distnc; + viz[nod]=1; + for (int k=0;k> n >> m >> nod1 >> nod2; + for (int i=1;i<=m;i++) { + f >> x >> y >> distnc; + knots[x].push_back(y); + knots[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graph(nod1,0); + g << dist[nod2]; + return 0; +} diff --git a/villages/dfs/c7682b1c-64e9-4ba3-8554-9c48a7387e80.cpp b/villages/dfs/c7682b1c-64e9-4ba3-8554-9c48a7387e80.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/c7682b1c-64e9-4ba3-8554-9c48a7387e80.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout< +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +int n,m,x,y; +int nod1,nod2; +vector < int > knots[30005]; +vector < int > dt[30005]; +int viz[30005]; +int dist[30005]; +int distnc; + +void graph(int nod, int distnc) { + dist[nod]= distnc; + viz[nod]=1; + for (int k=0;k> n >> m >> nod1 >> nod2; + for (int i=1;i<=m;i++) { + f >> x >> y >> distnc; + knots[x].push_back(y); + knots[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graph(nod1,0); + g << dist[nod2]; + return 0; +} diff --git a/villages/dfs/c91a5c74-24ee-46be-98b6-266e55f4100c.cpp b/villages/dfs/c91a5c74-24ee-46be-98b6-266e55f4100c.cpp new file mode 100644 index 0000000000000000000000000000000000000000..162d8c01eb8782e8fbf68aaff2408ebac22ddbe6 --- /dev/null +++ b/villages/dfs/c91a5c74-24ee-46be-98b6-266e55f4100c.cpp @@ -0,0 +1,49 @@ +#include +#include +#define NMAX 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +struct ura +{ + int nod,cost; +}aux; + +vector adj[NMAX]; +int n,m,x,y; +int viz[NMAX]; + +void dfs(int cv) +{ + for(vector:: iterator it=adj[cv].begin();it!=adj[cv].end();it++) + { + aux=*it; + if(!viz[aux.nod]) + { + viz[aux.nod]=viz[cv]+aux.cost; + dfs(aux.nod); + } + } +} + +int main() +{ + fin>>n>>m>>x>>y; + int i,j,d; + for(int bla=1;bla<=m;bla++) + { + fin>>i>>j>>d; + aux.cost=d; + aux.nod=j; + adj[i].push_back(aux); + aux.cost=-d; + aux.nod=i; + adj[j].push_back(aux); + } + viz[x]=1; + dfs(x); + fout< +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y, s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/dfs/ce4d6a96-8004-4e47-8ddd-b54bc3ec60d8.cpp b/villages/dfs/ce4d6a96-8004-4e47-8ddd-b54bc3ec60d8.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46d7ea4b254f6eece68ec00ef3826d27ca1a40ca --- /dev/null +++ b/villages/dfs/ce4d6a96-8004-4e47-8ddd-b54bc3ec60d8.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +using namespace std; + +typedef pair edge; + +int n, m, s, d; +bool viz[30001], done; +vector g[30001]; + +void citire(); +void afisare(int); +void dfs(int, int); + + +int main() { + citire(); + + dfs(s, 0); + + return 0; +} + + +void dfs(int x, int cost) { + viz[x] = true; + if (x == d) { + afisare(cost); + done = true; + return; + } + + for (int i = 0; i < g[x].size(); ++i) { + int y = g[x][i].second; + if (viz[y]) continue; + dfs(y, cost + g[x][i].first); + if (done) return; + } +} + +void afisare(int cost) { + ofstream out("sate.out"); + out << cost; + out.close(); +} + +void citire() { + ifstream in("sate.in"); + in >> n >> m >> s >> d; + + for (int i = 1, x, y, c; i <= m; ++i) { + in >> x >> y >> c; + g[x].push_back(make_pair(c, y)); + g[y].push_back(make_pair(-c, x)); + } + + in.close(); +} diff --git a/villages/dfs/cf007867-40ac-11ec-82bf-dc7196033e92.cpp b/villages/dfs/cf007867-40ac-11ec-82bf-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7df0d117b68f24f412026d912618cd4a140c92b7 --- /dev/null +++ b/villages/dfs/cf007867-40ac-11ec-82bf-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +void dfs ( int nod ); + +int n, m, x, y; + +vector < pair < int, int > > v[30137]; + +int dp[30137]; + +int a, b, d; + +int main() +{ + in >> n >> m >> x >> y; + for ( register int i = 1 ; i <= m ; ++i ) + { + in >> a >> b >> d; + v[a].push_back ( { b, d } ); + v[b].push_back ( { a, -d } ); + } + dfs(x); + out << dp[y]; + return 0; +} + +void dfs ( int nod ) +{ + for ( auto i : v[nod] ) + { + int nod2 = i.first; + int cost = i.second; + if ( !dp[nod2] ) + { + dp[nod2] = dp[nod] + cost; + dfs(nod2); + } + } +} diff --git a/villages/dfs/cf12441b-60c3-4f69-a64d-1fcadfb67911.cpp b/villages/dfs/cf12441b-60c3-4f69-a64d-1fcadfb67911.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9a48668dd05886cd5c060af4588f8bb2fc05e8ea --- /dev/null +++ b/villages/dfs/cf12441b-60c3-4f69-a64d-1fcadfb67911.cpp @@ -0,0 +1,36 @@ +#include +#define N 30100 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector < pair > V[N]; +int n, m, c, a , b, x, y; +int viz[N]; + +void DFS(int x) { + + for (int i=0; i>n>>m>>x>>y; + if(y>a>>b>>c; + V[a].push_back(make_pair(b,c)); + V[b].push_back(make_pair(a,-c)); + } + DFS(x); + fout << viz[y]; + return 0; +} + diff --git a/villages/dfs/cf32cc91-40ac-11ec-9fb9-dc7196033e92.cpp b/villages/dfs/cf32cc91-40ac-11ec-9fb9-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7df0d117b68f24f412026d912618cd4a140c92b7 --- /dev/null +++ b/villages/dfs/cf32cc91-40ac-11ec-9fb9-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +void dfs ( int nod ); + +int n, m, x, y; + +vector < pair < int, int > > v[30137]; + +int dp[30137]; + +int a, b, d; + +int main() +{ + in >> n >> m >> x >> y; + for ( register int i = 1 ; i <= m ; ++i ) + { + in >> a >> b >> d; + v[a].push_back ( { b, d } ); + v[b].push_back ( { a, -d } ); + } + dfs(x); + out << dp[y]; + return 0; +} + +void dfs ( int nod ) +{ + for ( auto i : v[nod] ) + { + int nod2 = i.first; + int cost = i.second; + if ( !dp[nod2] ) + { + dp[nod2] = dp[nod] + cost; + dfs(nod2); + } + } +} diff --git a/villages/dfs/cf6885e1-40ac-11ec-a3b7-dc7196033e92.cpp b/villages/dfs/cf6885e1-40ac-11ec-a3b7-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..366fc2f0bbf56950c802c5615975a62c00375aca --- /dev/null +++ b/villages/dfs/cf6885e1-40ac-11ec-a3b7-dc7196033e92.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector < pair >v[30005]; +int dist[30005]; +int dfs(int nod) +{ + int i; + for(i=0; inod) dist[v[nod][i].first]=dist[nod]+v[nod][i].second; + else dist[v[nod][i].first]=dist[nod]-v[nod][i].second; + dfs(v[nod][i].first); + } +} +int main() +{ + int n, m, nodi, nodf, a, b, c, i; + fin >> n >> m >> nodi >> nodf; + for(i=1; i<=m; i++) + { + fin >> a >> b >> c; + v[a].push_back(make_pair(b, c)); + v[b].push_back(make_pair(a, c)); + } + dfs(nodi); + fout << dist[nodf]; + return 0; +} diff --git a/villages/dfs/d1220965-40ac-11ec-ada6-dc7196033e92.cpp b/villages/dfs/d1220965-40ac-11ec-ada6-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7df0d117b68f24f412026d912618cd4a140c92b7 --- /dev/null +++ b/villages/dfs/d1220965-40ac-11ec-ada6-dc7196033e92.cpp @@ -0,0 +1,45 @@ +#include +#include + +using namespace std; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +void dfs ( int nod ); + +int n, m, x, y; + +vector < pair < int, int > > v[30137]; + +int dp[30137]; + +int a, b, d; + +int main() +{ + in >> n >> m >> x >> y; + for ( register int i = 1 ; i <= m ; ++i ) + { + in >> a >> b >> d; + v[a].push_back ( { b, d } ); + v[b].push_back ( { a, -d } ); + } + dfs(x); + out << dp[y]; + return 0; +} + +void dfs ( int nod ) +{ + for ( auto i : v[nod] ) + { + int nod2 = i.first; + int cost = i.second; + if ( !dp[nod2] ) + { + dp[nod2] = dp[nod] + cost; + dfs(nod2); + } + } +} diff --git a/villages/dfs/d161875f-40ac-11ec-aa93-dc7196033e92.cpp b/villages/dfs/d161875f-40ac-11ec-aa93-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3042632af745ca717a245f180aec60b96654c1e8 --- /dev/null +++ b/villages/dfs/d161875f-40ac-11ec-aa93-dc7196033e92.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +#define x first +#define y second + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +const int NMAX = 30001; +vector < pair > g[NMAX]; +int dist[NMAX]; + +void dfs(int node) { + for (unsigned int i = 0; i < g[node].size(); ++i) { + pair n = g[node][i]; + if (!dist[n.x]) { + dist[n.x] = dist[node] + n.y; + dfs(n.x); + } + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M, st, en; + fin >> N >> M >> st >> en; + while (M--) { + int x, y, d; + fin >> x >> y >> d; + g[x].push_back({y, d}); + g[y].push_back({x, -d}); + } + dfs(st); + fout << dist[en]; +} + + diff --git a/villages/dfs/d209f462-ff2a-4692-87ce-12354872c2cd.cpp b/villages/dfs/d209f462-ff2a-4692-87ce-12354872c2cd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5db8d95def74a39e6f102093b51b67e013cf2f44 --- /dev/null +++ b/villages/dfs/d209f462-ff2a-4692-87ce-12354872c2cd.cpp @@ -0,0 +1,113 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, X, Y; + register int M; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/d597ce4b-3c85-4ebb-b728-7f97837ec187.cpp b/villages/dfs/d597ce4b-3c85-4ebb-b728-7f97837ec187.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/d597ce4b-3c85-4ebb-b728-7f97837ec187.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include + +using namespace std; + +//ifstream cin("sate.in"); +//ofstream cout("sate.out"); + +vector>vecini[100205]; +int viz[30005],dist[30005],x1,y1,d,n,m,x,y; +const int Lim = 10000000; +int u = Lim - 1; +char s[Lim]; + +void Next () { + if (++u == Lim) + fread(s, 1, Lim, stdin), u = 0; +} + +void Get (int &x) { + for (; s[u] < '0' || s[u] > '9'; Next()); + for (x = 0; s[u] >= '0' && s[u] <= '9'; Next()) + x = x * 10 + s[u] - '0'; +} + +void dfs(int nod){ + viz[nod]=1; + for(auto i:vecini[nod]){ + if(!viz[i.first]){ + dist[i.first]=dist[nod]+i.second; + dfs(i.first); + } + } +} + +int main() +{ + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + Get(n),Get(m),Get(x),Get(y); + for(int i=1;i<=m;i++){ + Get(x1),Get(y1),Get(d); + vecini[x1].push_back({y1,d}); + vecini[y1].push_back({x1,-d}); + } + viz[x]=1; + dfs(x); + if(viz[y]!=1){ + printf("%d",-1); + return 0; + } + if(dist[y]<0){ + dist[y]*=-1; + } + printf("%d",dist[y]); + return 0; +} diff --git a/villages/dfs/d96f63ae-5baa-4a5b-b96c-a7ecc89fd916.cpp b/villages/dfs/d96f63ae-5baa-4a5b-b96c-a7ecc89fd916.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76d5d1da76c3c4a2466891e04ffc12f0352e992a --- /dev/null +++ b/villages/dfs/d96f63ae-5baa-4a5b-b96c-a7ecc89fd916.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int n,m,x,y; +int NOD1,NOD2; +vector < int > noduri[30005]; +vector < int > dt[30005]; +int vizitat[30005]; +int distanta[30005]; +int distnc; + +void graf(int NOD, int distnc) { + + distanta[NOD]= distnc; + vizitat[NOD]=1; + + for (int i=0;i> n; + fin>> m; + fin >> NOD1; + fin >> NOD2; + + for (int i=1;i<=m;i++) { + fin >> x >> y >> distnc; + noduri[x].push_back(y); + noduri[y].push_back(x); + dt[x].push_back(distnc); + dt[y].push_back(distnc); + } + graf(NOD1,0); + fout << distanta[NOD2]; + return 0; +} diff --git a/villages/dfs/d9cbd88c-0658-422f-b56e-0799178d5ca2.cpp b/villages/dfs/d9cbd88c-0658-422f-b56e-0799178d5ca2.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8f43742de5739112de1c42f955efb15170657b5a --- /dev/null +++ b/villages/dfs/d9cbd88c-0658-422f-b56e-0799178d5ca2.cpp @@ -0,0 +1,42 @@ +#include +#include + +FILE *fin = fopen("sate.in", "r"); +FILE *fout = fopen("sate.out", "w"); + +#define maxn 120000 +#define ll long long + +std::vector> v[maxn + 1]; +int viz[maxn + 1]; +int a, b; +ll dp[maxn + 1]; +void dfs(int t) { + viz[t] = 1; + int p, c; + for (auto it : v[t]) { + p = it.first; + c = it.second; + if (viz[p] != 1) { + dp[p] = dp[t] + c; + dfs(p); + } + + } +} + +int main() +{ + + int n, m, x, y, c; + + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for (int i = 1; i <= m; i++) { + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back (std::make_pair(y, c)); + v[y].push_back (std::make_pair(x, -c)); + } + dfs(a); + fprintf(fout, "%lld", dp[b]); + return 0; +} diff --git a/villages/dfs/db15e5f0-a4de-4634-bc45-9403cc9e43c9.cpp b/villages/dfs/db15e5f0-a4de-4634-bc45-9403cc9e43c9.cpp new file mode 100644 index 0000000000000000000000000000000000000000..272f0e80a4ace0cddf2201135507cbbe79fc82e2 --- /dev/null +++ b/villages/dfs/db15e5f0-a4de-4634-bc45-9403cc9e43c9.cpp @@ -0,0 +1,106 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) + swap(a, b); + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/dba08f7a-1288-412f-8803-d1f91431b234.cpp b/villages/dfs/dba08f7a-1288-412f-8803-d1f91431b234.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b121b065eecb65910a1b36e8f482d3cc6c9d092f --- /dev/null +++ b/villages/dfs/dba08f7a-1288-412f-8803-d1f91431b234.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +const int N=3e4+10; +struct pereche{ + int nod; + int cost; +}; +vector mat[N]; +bitset vizitat; +int n,start,stop; +bool FINAL=false; +void dfs(int x,int c) +{ + if(x==stop) + { + g<>n>>m>>start>>stop; + for(int i=1;i<=m;i++) + { + int x,y,z; + f>>x>>y>>z; + pereche aux; + aux.nod=y; + aux.cost=z; + mat[x].push_back(aux); + aux.nod=x; + aux.cost=-z; + mat[y].push_back(aux); + } + dfs(start,0); + return 0; +} diff --git a/villages/dfs/dcff3e4b-40ac-11ec-86ff-dc7196033e92.cpp b/villages/dfs/dcff3e4b-40ac-11ec-86ff-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c76baf405c4049f5e694f5dd90216fb2422a34e --- /dev/null +++ b/villages/dfs/dcff3e4b-40ac-11ec-86ff-dc7196033e92.cpp @@ -0,0 +1,107 @@ +#pragma GCC optimize("O3") + +#include +#include +#include + +using namespace std; + +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +} in ("sate.in"); + +const int N = 30009; + +bitset < N > viz; +vector < pair < short int, int > > adia[N]; + +int dist[N]; + +void dfs(int nod) { + for (auto i : adia[nod]) { + if (!viz[i.first]) { + dist[i.first] = dist[nod] + i.second; + viz[i.first] = 1; + dfs(i.first); + } + } +} + +int main() +{ + ofstream fout("sate.out"); + ios_base::sync_with_stdio(NULL); + fout.tie(0); + int m; + int n, x, y; + in >> n >> m >> x >> y; + if (x > y) + swap(x, y); + int a, b; + int c; + while (m--) { + in >> a >> b >> c; + adia[a].push_back({b, c}); + adia[b].push_back({a, -c}); + } + viz[x] = 1; + dfs(x); + if (!viz[y]) + return fout << -1, 0; + fout << dist[y]; + return 0; +} diff --git a/villages/dfs/dd6be97e-40ac-11ec-9504-dc7196033e92.cpp b/villages/dfs/dd6be97e-40ac-11ec-9504-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..40c0bbad997cdaff14c838df08da8e09dc1bb14d --- /dev/null +++ b/villages/dfs/dd6be97e-40ac-11ec-9504-dc7196033e92.cpp @@ -0,0 +1,61 @@ +#include +#include + +using namespace std; + +struct Nod { + int et, cost; + Nod *adr; +} *L[30005]; + +int n, m, X, Y, x, y, c; +int marcat[30005]; + +const int Lim = 10000000; +int u = Lim - 1; +char s[Lim]; + +void Next () { + if (++u == Lim) + fread(s, 1, Lim, stdin), u = 0; +} + +void Get (int &x) { + for (; s[u] < '0' || s[u] > '9'; Next()); + for (x = 0; s[u] >= '0' && s[u] <= '9'; Next()) + x = x * 10 + s[u] - '0'; +} + +void dfs(int nod) { + for (Nod *a = L[nod]; a != NULL; a = a -> adr) { + if (marcat[a -> et] || a -> et == X) + continue; + marcat[a -> et] = marcat[nod] + a -> cost; + dfs(a -> et); + if (a -> et == Y) { + printf("%d", marcat[a -> et]); + exit(0); + } + } +} + +int main() { + freopen("sate.in", "r", stdin); + freopen("sate.out", "w", stdout); + Get(n); Get(m); Get(X); Get(Y); + for (int i = 1; i <= m; ++i) { + Get(x); Get(y); Get(c); + Nod *p = new Nod; + p -> et = y; + p -> cost = c; + p -> adr = L[x]; + L[x] = p; + p = new Nod; + p -> et = x; + p -> cost = - c; + p -> adr = L[y]; + L[y] = p; + } + dfs(X); + return 0; +} diff --git a/villages/dfs/dd9876c3-af48-4904-b3f0-167937ea9bdb.cpp b/villages/dfs/dd9876c3-af48-4904-b3f0-167937ea9bdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2b91cd025a70d9adbb8b7069dc2774e7a6c542db --- /dev/null +++ b/villages/dfs/dd9876c3-af48-4904-b3f0-167937ea9bdb.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +ifstream in("sate.in"); +ofstream out("sate.out"); +typedef long long ll; +const int lim=3e4+5; +vector > vec[lim]; +int n,m,st,dr,x,y; ll d; +ll dist[lim]; +bool ok[lim]; +void df(int nod) +{ + ok[nod]=true; + for(auto x:vec[nod]) + if(!ok[x.first]) + dist[x.first]=dist[nod]+x.second, + df(x.first); +} +int main() +{ + in>>n>>m>>st>>dr; + for(int i=1;i<=m;++i) + { + in>>x>>y>>d; + vec[x].push_back({y,d}); + vec[y].push_back({x,-d}); + } + df(st); + out< +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/e17a47aa-313f-430e-b626-8f6334b428ea.cpp b/villages/dfs/e17a47aa-313f-430e-b626-8f6334b428ea.cpp new file mode 100644 index 0000000000000000000000000000000000000000..df2700901f071a3312526991819a2fec88c7c8e5 --- /dev/null +++ b/villages/dfs/e17a47aa-313f-430e-b626-8f6334b428ea.cpp @@ -0,0 +1,112 @@ +#pragma GCC optimize("Ofast") +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/e38020b6-88dc-4102-9797-c5e5d80fa603.cpp b/villages/dfs/e38020b6-88dc-4102-9797-c5e5d80fa603.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4398040188fd6ae71c3c9d2b3be95d38917a5e85 --- /dev/null +++ b/villages/dfs/e38020b6-88dc-4102-9797-c5e5d80fa603.cpp @@ -0,0 +1,34 @@ +#include +#include +#define f first +#define s second +#define mp make_pair +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +int viz[30010]; +vector > A[30010]; +int n,m,a,b,x,y,start,cost; +void DFS(int nod){ + for(int i=0; i>n>>m>>x>>y; + if(x>y) swap(x,y); + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(mp(b,cost)); + A[b].push_back(mp(a,-cost)); + } + DFS(x); + fout< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef pair pii; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +const int MAXN = 30010; +pii muchii[MAXM]; +int offsetOf[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (register int itt = offsetOf[x], tarr = offsetOf[x + 1]; itt != tarr; ++itt) { + register int v = muchii[itt].first; + if (dst[v] != 0x3F3F3F3F) + continue; + dst[v] = dst[x] + muchii[itt].second; + if (v == tar) + return; + S[ssz++] = v; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + memset(dst, 0x3F, sizeof dst); + if (X > Y) + swap(X, Y); + while (M--) { + ++offsetOf[nextInt()]; + ++offsetOf[nextInt()]; + nextInt(); + } + for (register int i = 1; i <= N; ++i) + offsetOf[i] += offsetOf[i - 1]; + head = parsebuf; + nextInt(); M = nextInt(); nextInt(); nextInt(); + offsetOf[N + 1] = 2 * M; + while (M--) { + register int a = nextInt(), b = nextInt(), c = nextInt(); + if (a > b) { + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = c; + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = -c; + } + else { + muchii[--offsetOf[a]].first = b; + muchii[offsetOf[a]].second = c; + muchii[--offsetOf[b]].first = a; + muchii[offsetOf[b]].second = -c; + } + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/e5288305-7cd3-4c0d-b208-12b0af9f0762.cpp b/villages/dfs/e5288305-7cd3-4c0d-b208-12b0af9f0762.cpp new file mode 100644 index 0000000000000000000000000000000000000000..854951e5ee2655c734f877fc8682b419eeac573c --- /dev/null +++ b/villages/dfs/e5288305-7cd3-4c0d-b208-12b0af9f0762.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int seen[40000], d = 0, y; + +struct drumuri +{ + int vecin; + int dis; +}; + + + +void DFS(int nod, vector v[40000]) +{ + if(nod == y) + { + out< v[40000]; + int n, m, a, b, i, x, dist; + in>>n>>m>>x>>y; + for(i = 0; i>a>>b>>dist; + + drumuri dr = {b, dist}; + v[a].push_back(dr); + + drumuri dr2 = {a, -dist}; + v[b].push_back(dr2); + } + DFS(x, v); + return 0; +} diff --git a/villages/dfs/e6c3f924-07d8-4b00-8089-4c41ad98e4d4.cpp b/villages/dfs/e6c3f924-07d8-4b00-8089-4c41ad98e4d4.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23ab8f778e700acba948149d98ab126a663637c7 --- /dev/null +++ b/villages/dfs/e6c3f924-07d8-4b00-8089-4c41ad98e4d4.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +const int N_MAX = 30000 + 5; + +int x, y, n,m; + +vector > vec[N_MAX]; + +bitset viz; + +void dfs(int nod, int sum){ + + if(viz[nod]) + return; + + viz[nod] = true; + + + if(nod == y){ + fout << sum; + exit(0); + }else{ + for(auto v : vec[nod]){ + dfs(v.first, sum + v.second); + } + } + +} + +int main() +{ + + fin >> n >> m >> x >> y; + + while(m--){ + int a,b,d; + fin >> a >> b >> d; + if(a > b) + swap(a,b); + vec[a].push_back({b,d}); + vec[b].push_back({a,-d}); + } + + dfs(x, 0); + + return 0; +} diff --git a/villages/dfs/e802959f-9f20-4ff4-bf90-0ade09398d12.cpp b/villages/dfs/e802959f-9f20-4ff4-bf90-0ade09398d12.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c3aceabefe65e82277a98c75d43ad628e235fae2 --- /dev/null +++ b/villages/dfs/e802959f-9f20-4ff4-bf90-0ade09398d12.cpp @@ -0,0 +1,43 @@ +# include +# include +# define f first +# define s second + +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector< pair > A[30010]; + +int viz[30010], n, m, x, y, a, b, cost; + +void dfs(int nod){ + for(int i=0; i>n>>m>>x>>y; + + if(x>y) swap(x,y); + + for(int i=1; i<=m; i++){ + fin>>a>>b>>cost; + A[a].push_back(make_pair(b, cost)); + A[b].push_back(make_pair(a, -cost)); + } + + dfs(x); + + fout< +#include + +using namespace std; + +vector < pair > v[30001]; +bool viz[30001]; +char s[1001]; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n,m,st,fin; + +void read() +{ in>>n>>m>>st>>fin; + in.get(); + int x, y, c, nr, cnt; + for (int k = 1; k <= m; ++k) + { cnt = 0; + in.getline(s, 1001); + for (int i = 0; s[i] != '\0'; ++i) { + if (s[i] >= '0' && s[i] <= '9') { + nr = 0; + cnt++; + while (s[i] >= '0' && s[i] <= '9') { + nr = nr * 10 + s[i] - '0'; + ++i; + } + if (cnt == 1) x = nr; + if (cnt == 2) y = nr; + if (cnt == 3) c = nr; + } + } + v[x].push_back({y,c}); + v[y].push_back({x,c}); + } +} + +void drum(int nod,int dist) +{ + viz[nod]=1; + if (nod==fin) + { + out<nod) + drum(nod1,dist+d); + } + } +} + +int main() +{ + read(); + drum(st,0); + return 0; +} diff --git a/villages/dfs/e9e6febf-d65b-4dcf-8cab-b21bfcc9e088.cpp b/villages/dfs/e9e6febf-d65b-4dcf-8cab-b21bfcc9e088.cpp new file mode 100644 index 0000000000000000000000000000000000000000..994ae596bbf1f5b149d1315d861217acc69e9b4a --- /dev/null +++ b/villages/dfs/e9e6febf-d65b-4dcf-8cab-b21bfcc9e088.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +using namespace std; +ifstream fi("sate.in"); +ofstream fo("sate.out"); +int n,m; +int X,Y; +vector >vec[30005]; +int parcurs[30005]; +int sol[30005]; +void componenta_conexa(int x,int value) +{ + sol[x]=value; + for(int i=0;i<=vec[x].size()-1;i++) + { + if(parcurs[vec[x][i].first]==0) + { + parcurs[vec[x][i].first]=1; + ///daca in vector distanta paan la nodul vec[x][i] este mai mica decat una calculata + /// se scade altfe se insumeaza + if(x>vec[x][i].first) + componenta_conexa(vec[x][i].first,value-vec[x][i].second); + else + componenta_conexa(vec[x][i].first,value+vec[x][i].second); + } + } +} +int main() +{ + fi>>n>>m; + fi>>X>>Y; + int x,y,dist; + for(int i=1;i<=m;i++) + { + fi>>x>>y>>dist; + vec[x].push_back(make_pair(y,dist)); + vec[y].push_back(make_pair(x,dist)); + } + parcurs[X]=1; + componenta_conexa(X,0); + fo< +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef unsigned long long hsh; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/ec3b007d-6657-49bf-8488-a406c94b1a79.cpp b/villages/dfs/ec3b007d-6657-49bf-8488-a406c94b1a79.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00c06866a272768df62289301ac872a8c490da7d --- /dev/null +++ b/villages/dfs/ec3b007d-6657-49bf-8488-a406c94b1a79.cpp @@ -0,0 +1,67 @@ +#include +#include + +using namespace std; + +vector < pair > v[30001]; +bool viz[30001]; +char s[1001]; + +ifstream in ("sate.in"); +ofstream out ("sate.out"); + +int n,m,st,fin; + +void read() +{ in>>n>>m>>st>>fin; + in.get(); + int x, y, c, nr, cnt; + for (int k = 1; k <= m; ++k) + { cnt = 0; + in.getline(s, 1001); + for (int i = 0; s[i] != '\0'; ++i) { + if (s[i] >= '0' && s[i] <= '9') { + nr = 0; + cnt++; + while (s[i] >= '0' && s[i] <= '9') { + nr = nr * 10 + s[i] - '0'; + ++i; + } + if (cnt == 1) x = nr; + if (cnt == 2) y = nr; + if (cnt == 3) c = nr; + } + } + v[x].push_back({y,c}); + v[y].push_back({x,c}); + } +} + +void drum(int nod,int dist) +{ + viz[nod]=1; + if (nod==fin) + { + out<nod) + drum(nod1,dist+d); + } + } +} + +int main() +{ + read(); + drum(st,0); + return 0; +} diff --git a/villages/dfs/edff918b-c854-4bf6-8bb1-d64d907133fd.cpp b/villages/dfs/edff918b-c854-4bf6-8bb1-d64d907133fd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c432ba0c61d17750f77d2a455d93c6a1d158f494 --- /dev/null +++ b/villages/dfs/edff918b-c854-4bf6-8bb1-d64d907133fd.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#define N 30002 + +using namespace std; + +int n, m, x, y, d[N]; +bool ok; +vector>A[N]; + +void df(int nod) +{ + if(nod==y) + ok=1; + + if(!ok) + { + for(auto i:A[nod]) + { + if(!d[i.first]) + { + d[i.first]=d[nod]+i.second; + df(i.first); + } + } + } +} + +int main() +{ + //freopen("sate.in", "r", stdin); + //freopen("sate.out", "w", stdout); + ifstream fin("sate.in"); + ofstream fout("sate.out"); + //scanf("%d %d %d %d", &n, &m, &x, &y); + fin>>n>>m>>x>>y; + for(int i=1; i<=m; i++) + { + int n1,n2,n3; + //scanf("%d %d %d", &n1, &n2, &n3); + fin>>n1>>n2>>n3; + A[n1].push_back({n2,n3}); + A[n2].push_back({n1,-n3}); + } + d[x]=1; + df(x); + //printf("%d\n",d[y]-1); + fout< +#include +#define N 30001 + +using namespace std; + +struct elem +{ + int nod, cost; +}; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +vector G[N]; +int n, m, start, finish, dist[N]; +bool viz[N]; + +void Citire() +{ + int x, y, d; + elem e; + + fin>>n>>m>>start>>finish; + + while (m--) + { + fin>>x>>y>>d; + + e.cost = d; + e.nod = y; + G[x].push_back(e); + + e.cost = -d; + e.nod = x; + G[y].push_back(e); + } +} + +void Dfs(int nod) +{ + elem el; + int i; + + if (!viz[finish]) + + for (i = 0; i < G[nod].size(); ++i) + { + el=G[nod][i]; + + if (!viz[el.nod]) + { + dist[el.nod] = dist[nod] + el.cost; + viz[nod] = 1; + Dfs(el.nod); + } + } +} + +void Afisare() +{ + fout< +#include +#define pp pair +#define inf 9999999 +#include +using namespace std; +ifstream fin("sate.in"); +ofstream fout("sate.out"); +vector a[30001]; +int n,m,x,y,d[100001]; +bool v[30001]; +void cit(){ + int i,j,w,h; + fin>>n>>m>>x>>y; + for(h=1;h<=m;h++){ + fin>>i>>j>>w; + a[i].push_back(pp(j,w)); + a[j].push_back(pp(i,-w)); + } + fin.close(); +} + +void dfs(int x){ + v[x]=true; + for(vector::iterator it=a[x].begin();it!=a[x].end();it++) + if(v[it->first]==false){ + d[it->first]=d[x]+it->second; + dfs(it->first); + if(it->first==y){ + fout< +#define mp make_pair +#define pb push_back +#define st first +#define nd second +#define pii pair + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +int n, m, x, y, a, b, c, d[30100]; bool viz[30100]; +vector v[100100]; + +void dfs(int nod){ + for(auto it : v[nod]){ + if(!viz[it.st]){ + viz[it.st] = 1; + d[it.st] = d[nod] + it.nd; + if(it.st == y) return; + dfs(it.st); + } + } +} + +int main(){ + in >> n >> m >> x >> y; + for(int i = 1; i <= m; i++){ + in >> a >> b >> c; + v[a].pb(mp(b, c)); + v[b].pb(mp(a, -c)); + } + dfs(x); + return out << d[y], 0; +} diff --git a/villages/dfs/ef90d582-49d6-471a-9df0-4ca102052124.cpp b/villages/dfs/ef90d582-49d6-471a-9df0-4ca102052124.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e26f3c7692c738a5ccb184db14b6903111d28dce --- /dev/null +++ b/villages/dfs/ef90d582-49d6-471a-9df0-4ca102052124.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + + +#define MAXN 30001 +vector < pair > v[MAXN]; +bool viz[MAXN]; +int drum[MAXN]; +int b, a, ans; +int dfs(int nod) { + viz[nod] = 1; + for(int i = 0; i < v[nod].size(); i++) { + pair it = v[nod][i]; + if(viz[it.first] == 0){ + drum[it.first]=drum[nod]+it.second; + //printf("%d %d\n", it.first, drum[it.first]); + if(it.first==b){ + return 0; + } + dfs(it.first); + } + + } +} + +int main(){ + FILE*fin=fopen("sate.in", "r"); + FILE*fout=fopen("sate.out", "w"); + int n, m, a, b, i, x, y, c; + fscanf(fin, "%d%d%d%d", &n, &m, &a, &b); + for(i=1; i<=m; i++){ + fscanf(fin, "%d%d%d", &x, &y, &c); + v[x].push_back({y, c}); + v[y].push_back({x, -c}); + } + ans=0; + dfs(a); + if(b>a) + fprintf(fout, "%d", drum[b]); + else + fprintf(fout, "%d", -drum[b]); + return 0; +} diff --git a/villages/dfs/efa7c1ea-40ac-11ec-b43a-dc7196033e92.cpp b/villages/dfs/efa7c1ea-40ac-11ec-b43a-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3968d48dd624a7c5b42ce2735e4be7426bc16eb0 --- /dev/null +++ b/villages/dfs/efa7c1ea-40ac-11ec-b43a-dc7196033e92.cpp @@ -0,0 +1,56 @@ +#include +#include + +using namespace std; + +ifstream in("sate.in"); +ofstream out("sate.out"); + +const int NMAX = 30001, MMAX = 100024, INF=1<<31; +int lst[NMAX], vf[2*MMAX], urm[2*MMAX], w[2*MMAX], dist[NMAX], nr=0; + +void Add(int x, int y, int weight) +{ + ++nr; + vf[nr]=y; + urm[nr]=lst[x]; + w[nr]=weight; + lst[x]=nr; +} + +void Dfs(int x); + +int main() +{ + int n, m, st, fn; + in >> n >> m >> st >> fn; + for(int i=1; i<=n; i++) dist[i]=INF; + + for(int i=1; i<=m; i++) + { + int x, y, weight; + in >> x >> y >> weight; + Add(x, y, weight); + Add(y, x, weight); + } + + dist[st]=0; + Dfs(st); + + out << dist[fn]; + return 0; +} + +void Dfs(int x) +{ + for(int p=lst[x];p;p=urm[p]) + { + int y=vf[p]; + if(dist[y]==INF) + { + if(y +#include +#include +using namespace std; +ifstream f("sate.in"); +ofstream g("sate.out"); +vector < pair > graf[30001]; +int D, dis, x, y, Nods, Arcs, Origin, End, i, a[4], nr, k, j; +char S[31]; +bool ok, viz[30001]; +void dfs(int node){ + viz[node] = 1; + for(int i = 0; i < graf[node].size(); i++){ + if(ok == 1) + return; + if(viz[graf[node][i].first] == 0){ + D = D + graf[node][i].second; + if(graf[node][i].first == End){ + g << D; + ok = 1; + } + dfs(graf[node][i].first); + D = D - graf[node][i].second; + } + } +} +int main() +{ f >> Nods >> Arcs >> Origin >> End; + f.get(); + for(i = 1; i <= Arcs; i++){ + f.getline(S, 31); + k = 0; + for(j = 1; j <= 3; j++){ + nr = 0; + while(S[k] >= '0' && S[k] <= '9'){ + nr = nr * 10 + (S[k] - '0'); + k++; + } + while(S[k] == ' ') + k++; + a[j] = nr; + } + if(a[1] > a[2]) + swap(a[1], a[2]); + graf[a[1]].push_back({a[2], a[3]}); + graf[a[2]].push_back({a[1], -a[3]}); + } + dfs(Origin); + return 0; +} diff --git a/villages/dfs/f43c9dcd-3670-443c-a652-bba47498ba19.cpp b/villages/dfs/f43c9dcd-3670-443c-a652-bba47498ba19.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7bc4ec9dc6690605f36d79668f957af8cf7aa37c --- /dev/null +++ b/villages/dfs/f43c9dcd-3670-443c-a652-bba47498ba19.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef unsigned long long hsh; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/f451a752-4493-43d9-bd0c-0c2da006bbcb.cpp b/villages/dfs/f451a752-4493-43d9-bd0c-0c2da006bbcb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1aaca756a591292772b20eca5193fd109e91f736 --- /dev/null +++ b/villages/dfs/f451a752-4493-43d9-bd0c-0c2da006bbcb.cpp @@ -0,0 +1,33 @@ +#include +#define pb push_back +#define MAXN 30005 +using namespace std; + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +int verific[MAXN], sate, distante, start, stop, total; +vector> urmatorul_sat[MAXN]; + +void calculeaza_distanta(int sat, int distanta) { + verific[sat] = 1; + if (sat == stop) + total = distanta; + if (total) + return; + for (auto urmatorul : urmatorul_sat[sat]) + if (!verific[urmatorul.first]) + calculeaza_distanta(urmatorul.first, distanta + urmatorul.second); +} + +int main() { + fin >> sate >> distante >> start >> stop; + int x, y, c; + while (distante--) { + fin >> x >> y >> c; + urmatorul_sat[x].pb(make_pair(y, c)); + urmatorul_sat[y].pb(make_pair(x, c * -1)); + } + calculeaza_distanta(start, 0); + fout << total; +} diff --git a/villages/dfs/f461266e-2538-4774-89cb-03f1ac4c8ccd.cpp b/villages/dfs/f461266e-2538-4774-89cb-03f1ac4c8ccd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bb5f559c1e2bae95470d93e1f2b117d78e761e72 --- /dev/null +++ b/villages/dfs/f461266e-2538-4774-89cb-03f1ac4c8ccd.cpp @@ -0,0 +1,52 @@ +#include +#include +#include + +using namespace std; + +ifstream f("sate.in"); +ofstream g("sate.out"); + +vector < vector< pair < int, int > > > w; + +bool b[30002]; +int Y, s; + +void parcurgere(int x,int s) +{ + // cout<>n>>m>>X>>Y; + + w.resize(n+1); + + for(int i = 1; i <= m; i++) + { + int x, y, d; + + f>>x>>y>>d; + + w[x].push_back(make_pair(y, d)); + w[y].push_back(make_pair(x, -d)); + } + parcurgere(X,0); +} diff --git a/villages/dfs/f62f4c28-78d4-43bd-958a-829ee2d74369.cpp b/villages/dfs/f62f4c28-78d4-43bd-958a-829ee2d74369.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23ab8f778e700acba948149d98ab126a663637c7 --- /dev/null +++ b/villages/dfs/f62f4c28-78d4-43bd-958a-829ee2d74369.cpp @@ -0,0 +1,52 @@ +#include + +using namespace std; + +ifstream fin ("sate.in"); +ofstream fout("sate.out"); + +const int N_MAX = 30000 + 5; + +int x, y, n,m; + +vector > vec[N_MAX]; + +bitset viz; + +void dfs(int nod, int sum){ + + if(viz[nod]) + return; + + viz[nod] = true; + + + if(nod == y){ + fout << sum; + exit(0); + }else{ + for(auto v : vec[nod]){ + dfs(v.first, sum + v.second); + } + } + +} + +int main() +{ + + fin >> n >> m >> x >> y; + + while(m--){ + int a,b,d; + fin >> a >> b >> d; + if(a > b) + swap(a,b); + vec[a].push_back({b,d}); + vec[b].push_back({a,-d}); + } + + dfs(x, 0); + + return 0; +} diff --git a/villages/dfs/f81563ae-40ac-11ec-b4f3-dc7196033e92.cpp b/villages/dfs/f81563ae-40ac-11ec-b4f3-dc7196033e92.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6bc7826ad7ded75937955a4a0be47518916e1b01 --- /dev/null +++ b/villages/dfs/f81563ae-40ac-11ec-b4f3-dc7196033e92.cpp @@ -0,0 +1,97 @@ +#include +#define pb push_back +#define mp make_pair +using namespace std; +class InParser { +private: + FILE *fin; + char *buff; + int sp; + + char read_ch() { + ++sp; + if (sp == 4096) { + sp = 0; + fread(buff, 1, 4096, fin); + } + return buff[sp]; + } + +public: + InParser(const char* nume) { + fin = fopen(nume, "r"); + buff = new char[4096](); + sp = 4095; + } + + InParser& operator >> (int &n) { + char c; + while (!isdigit(c = read_ch()) && c != '-'); + int sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } + + InParser& operator >> (long long &n) { + char c; + n = 0; + while (!isdigit(c = read_ch()) && c != '-'); + long long sgn = 1; + if (c == '-') { + n = 0; + sgn = -1; + } else { + n = c - '0'; + } + while (isdigit(c = read_ch())) { + n = 10 * n + c - '0'; + } + n *= sgn; + return *this; + } +}; +InParser fin("sate.in"); +ofstream fout("sate.out"); +int n,m,x,y,i,dist,stg,s,xi,yi; +int d[30009]; +vector >v[30009]; +void dfs(int nod) +{ + if(nod==yi) + { + fout<>n>>m>>xi>>yi; + for(i=1;i<=m;i++) + { + fin>>x>>y>>s; + v[x].pb(mp(s,y)); + v[y].pb(mp(-s,x)); + } + dfs(xi); + return 0; +} diff --git a/villages/dfs/fc6fb953-bade-49b7-ae6e-559ad81017d6.cpp b/villages/dfs/fc6fb953-bade-49b7-ae6e-559ad81017d6.cpp new file mode 100644 index 0000000000000000000000000000000000000000..46d7ea4b254f6eece68ec00ef3826d27ca1a40ca --- /dev/null +++ b/villages/dfs/fc6fb953-bade-49b7-ae6e-559ad81017d6.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +using namespace std; + +typedef pair edge; + +int n, m, s, d; +bool viz[30001], done; +vector g[30001]; + +void citire(); +void afisare(int); +void dfs(int, int); + + +int main() { + citire(); + + dfs(s, 0); + + return 0; +} + + +void dfs(int x, int cost) { + viz[x] = true; + if (x == d) { + afisare(cost); + done = true; + return; + } + + for (int i = 0; i < g[x].size(); ++i) { + int y = g[x][i].second; + if (viz[y]) continue; + dfs(y, cost + g[x][i].first); + if (done) return; + } +} + +void afisare(int cost) { + ofstream out("sate.out"); + out << cost; + out.close(); +} + +void citire() { + ifstream in("sate.in"); + in >> n >> m >> s >> d; + + for (int i = 1, x, y, c; i <= m; ++i) { + in >> x >> y >> c; + g[x].push_back(make_pair(c, y)); + g[y].push_back(make_pair(-c, x)); + } + + in.close(); +} diff --git a/villages/dfs/fce9b9ec-2aaa-4438-8529-d0fceec5a5e3.cpp b/villages/dfs/fce9b9ec-2aaa-4438-8529-d0fceec5a5e3.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ddf0b563337261bbdaf3435f617ed9a23a8e2ebd --- /dev/null +++ b/villages/dfs/fce9b9ec-2aaa-4438-8529-d0fceec5a5e3.cpp @@ -0,0 +1,102 @@ +#pragma GCC optimize("Ofast") +#include +using namespace std; +typedef long long LL; + +#ifdef INFOARENA +#define ProblemName "sate" +#endif + +#define MCONCAT(A, B) A B +#ifdef ProblemName +#define InFile MCONCAT(ProblemName, ".in") +#define OuFile MCONCAT(ProblemName, ".out") +#else +#define InFile "fis.in" +#define OuFile "fis.out" +#endif + +const int MAXM = 200124; +const int MAXBUF = 10000010; +char parsebuf[MAXBUF], *head; +bool numeric[255]; + +void parseInit() { + int off = fread(parsebuf, 1, MAXBUF, stdin); + assert(off < MAXBUF - 10); + parsebuf[off] = 0; + head = parsebuf; + memset(numeric, 0, sizeof(numeric)); + for (int i = '0'; i <= '9'; ++i) + numeric[i] = true; +} + +int nextInt() { + for (; !numeric[*head]; ++head); + int ans = 0; + for (; numeric[*head]; ++head) + ans = ans * 10 + (*head) - '0'; + return ans; +} + +struct node { + int first; int second; + int nxt; +} physical[MAXM]; +int nodecounter = 0; + +int linsert(int l, int f, int s) { + physical[nodecounter].first = f; + physical[nodecounter].second = s; + physical[nodecounter].nxt = l; + return (nodecounter++); +} + +const int MAXN = 30010; +int G[MAXN]; +int dst[MAXN]; +int S[MAXN], ssz; + +void DFS(int nod, int tar) { + ssz = 0; + S[ssz++] = nod; + dst[nod] = 0; + while (ssz) { + int x = S[--ssz]; + for (int itt = G[x]; itt != -1; itt = physical[itt].nxt) { + node *it = &physical[itt]; + if (dst[it->first] != INT_MAX) + continue; + dst[it->first] = dst[x] + it->second; + if (it->first == tar) + return; + S[ssz++] = it->first; + } + } +} + +int main() { + assert(freopen(InFile, "r", stdin)); + assert(freopen(OuFile, "w", stdout)); + parseInit(); + int N, M, X, Y; + N = nextInt(); M = nextInt(); X = nextInt(); Y = nextInt(); + for (int i = 0; i < N; ++i) + dst[i] = INT_MAX; + --X, --Y; + if (X > Y) + swap(X, Y); + memset(G, 0xFF, sizeof(G)); + while (M--) { + int a, b, c; + a = nextInt(); b = nextInt(); c = nextInt(); + --a, --b; + if (a > b) + swap(a, b); + G[a] = linsert(G[a], b, c); + G[b] = linsert(G[b], a, -c); + } + DFS(X, Y); + printf("%d\n", dst[Y]); + return 0; +} diff --git a/villages/dfs/ffba6ba6-c5ef-4c16-b3cf-0518564de17b.cpp b/villages/dfs/ffba6ba6-c5ef-4c16-b3cf-0518564de17b.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6c2606dbe8651b2cfa301515b0556a3951bd402 --- /dev/null +++ b/villages/dfs/ffba6ba6-c5ef-4c16-b3cf-0518564de17b.cpp @@ -0,0 +1,45 @@ +#include +#define N 30003 +#include +#define oo 1e7 +using namespace std; + +int n,m,A,B,x,y,c; +int dist[N]; +bool f[N]; + +vector > v[N]; //.first.second ,nod.cost + +ifstream fin("sate.in"); +ofstream fout("sate.out"); + +inline void DFS(const int & nod) +{ + f[nod] = 1; + for(auto i : v[nod]) + if(!f[i.first] && dist[i.first] == 0) + { + dist[i.first] = dist[nod] + i.second; + DFS(i.first); + } + f[nod] = 0; +} + +int main() +{ + fin>>n>>m>>A>>B; + + while(m--) + { + fin>>x>>y>>c; + v[min(x,y)].push_back(make_pair(max(x,y),c)); + v[max(x,y)].push_back(make_pair(min(x,y),-c)); + } + + DFS(A); + fout<